Remove support for Mac Carbon.
[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 of the License, or
11 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
37
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
47
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
61 |
62 expose_window (asynchronous) |
63 |
64 X expose events -----+
65
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
70
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
80
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
86
87
88 Direct operations.
89
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
94
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
101
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
107
108
109 Desired matrices.
110
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
117
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
124
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
130
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
137
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
145
146
147 Frame matrices.
148
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
155
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
167
168 #include <config.h>
169 #include <stdio.h>
170
171 #include "lisp.h"
172 #include "keyboard.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "character.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef HAVE_NS
200 #include "nsterm.h"
201 #endif
202
203 #include "font.h"
204
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
208
209 #define INFINITY 10000000
210
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
212 || defined(HAVE_NS) || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
216
217 extern int interrupt_input;
218 extern int command_loop_level;
219
220 extern Lisp_Object do_mouse_tracking;
221
222 extern int minibuffer_auto_raise;
223 extern Lisp_Object Vminibuffer_list;
224
225 extern Lisp_Object Qface;
226 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227
228 extern Lisp_Object Voverriding_local_map;
229 extern Lisp_Object Voverriding_local_map_menu_flag;
230 extern Lisp_Object Qmenu_item;
231 extern Lisp_Object Qwhen;
232 extern Lisp_Object Qhelp_echo;
233
234 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
235 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
236 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
237 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
238 Lisp_Object Qinhibit_point_motion_hooks;
239 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
240 Lisp_Object Qfontified;
241 Lisp_Object Qgrow_only;
242 Lisp_Object Qinhibit_eval_during_redisplay;
243 Lisp_Object Qbuffer_position, Qposition, Qobject;
244
245 /* Cursor shapes */
246 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
247
248 /* Pointer shapes */
249 Lisp_Object Qarrow, Qhand, Qtext;
250
251 Lisp_Object Qrisky_local_variable;
252
253 /* Holds the list (error). */
254 Lisp_Object list_of_error;
255
256 /* Functions called to fontify regions of text. */
257
258 Lisp_Object Vfontification_functions;
259 Lisp_Object Qfontification_functions;
260
261 /* Non-nil means automatically select any window when the mouse
262 cursor moves into it. */
263 Lisp_Object Vmouse_autoselect_window;
264
265 Lisp_Object Vwrap_prefix, Qwrap_prefix;
266 Lisp_Object Vline_prefix, Qline_prefix;
267
268 /* Non-zero means draw tool bar buttons raised when the mouse moves
269 over them. */
270
271 int auto_raise_tool_bar_buttons_p;
272
273 /* Non-zero means to reposition window if cursor line is only partially visible. */
274
275 int make_cursor_line_fully_visible_p;
276
277 /* Margin below tool bar in pixels. 0 or nil means no margin.
278 If value is `internal-border-width' or `border-width',
279 the corresponding frame parameter is used. */
280
281 Lisp_Object Vtool_bar_border;
282
283 /* Margin around tool bar buttons in pixels. */
284
285 Lisp_Object Vtool_bar_button_margin;
286
287 /* Thickness of shadow to draw around tool bar buttons. */
288
289 EMACS_INT tool_bar_button_relief;
290
291 /* Non-nil means automatically resize tool-bars so that all tool-bar
292 items are visible, and no blank lines remain.
293
294 If value is `grow-only', only make tool-bar bigger. */
295
296 Lisp_Object Vauto_resize_tool_bars;
297
298 /* Non-zero means draw block and hollow cursor as wide as the glyph
299 under it. For example, if a block cursor is over a tab, it will be
300 drawn as wide as that tab on the display. */
301
302 int x_stretch_cursor_p;
303
304 /* Non-nil means don't actually do any redisplay. */
305
306 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
307
308 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
309
310 int inhibit_eval_during_redisplay;
311
312 /* Names of text properties relevant for redisplay. */
313
314 Lisp_Object Qdisplay;
315 extern Lisp_Object Qface, Qinvisible, Qwidth;
316
317 /* Symbols used in text property values. */
318
319 Lisp_Object Vdisplay_pixels_per_inch;
320 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
321 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
322 Lisp_Object Qslice;
323 Lisp_Object Qcenter;
324 Lisp_Object Qmargin, Qpointer;
325 Lisp_Object Qline_height;
326 extern Lisp_Object Qheight;
327 extern Lisp_Object QCwidth, QCheight, QCascent;
328 extern Lisp_Object Qscroll_bar;
329 extern Lisp_Object Qcursor;
330
331 /* Non-nil means highlight trailing whitespace. */
332
333 Lisp_Object Vshow_trailing_whitespace;
334
335 /* Non-nil means escape non-break space and hyphens. */
336
337 Lisp_Object Vnobreak_char_display;
338
339 #ifdef HAVE_WINDOW_SYSTEM
340 extern Lisp_Object Voverflow_newline_into_fringe;
341
342 /* Test if overflow newline into fringe. Called with iterator IT
343 at or past right window margin, and with IT->current_x set. */
344
345 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
346 (!NILP (Voverflow_newline_into_fringe) \
347 && FRAME_WINDOW_P (it->f) \
348 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
349 && it->current_x == it->last_visible_x \
350 && it->line_wrap != WORD_WRAP)
351
352 #endif /* HAVE_WINDOW_SYSTEM */
353
354 /* Test if the display element loaded in IT is a space or tab
355 character. This is used to determine word wrapping. */
356
357 #define IT_DISPLAYING_WHITESPACE(it) \
358 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
359
360 /* Non-nil means show the text cursor in void text areas
361 i.e. in blank areas after eol and eob. This used to be
362 the default in 21.3. */
363
364 Lisp_Object Vvoid_text_area_pointer;
365
366 /* Name of the face used to highlight trailing whitespace. */
367
368 Lisp_Object Qtrailing_whitespace;
369
370 /* Name and number of the face used to highlight escape glyphs. */
371
372 Lisp_Object Qescape_glyph;
373
374 /* Name and number of the face used to highlight non-breaking spaces. */
375
376 Lisp_Object Qnobreak_space;
377
378 /* The symbol `image' which is the car of the lists used to represent
379 images in Lisp. */
380
381 Lisp_Object Qimage;
382
383 /* The image map types. */
384 Lisp_Object QCmap, QCpointer;
385 Lisp_Object Qrect, Qcircle, Qpoly;
386
387 /* Non-zero means print newline to stdout before next mini-buffer
388 message. */
389
390 int noninteractive_need_newline;
391
392 /* Non-zero means print newline to message log before next message. */
393
394 static int message_log_need_newline;
395
396 /* Three markers that message_dolog uses.
397 It could allocate them itself, but that causes trouble
398 in handling memory-full errors. */
399 static Lisp_Object message_dolog_marker1;
400 static Lisp_Object message_dolog_marker2;
401 static Lisp_Object message_dolog_marker3;
402 \f
403 /* The buffer position of the first character appearing entirely or
404 partially on the line of the selected window which contains the
405 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
406 redisplay optimization in redisplay_internal. */
407
408 static struct text_pos this_line_start_pos;
409
410 /* Number of characters past the end of the line above, including the
411 terminating newline. */
412
413 static struct text_pos this_line_end_pos;
414
415 /* The vertical positions and the height of this line. */
416
417 static int this_line_vpos;
418 static int this_line_y;
419 static int this_line_pixel_height;
420
421 /* X position at which this display line starts. Usually zero;
422 negative if first character is partially visible. */
423
424 static int this_line_start_x;
425
426 /* Buffer that this_line_.* variables are referring to. */
427
428 static struct buffer *this_line_buffer;
429
430 /* Nonzero means truncate lines in all windows less wide than the
431 frame. */
432
433 Lisp_Object Vtruncate_partial_width_windows;
434
435 /* A flag to control how to display unibyte 8-bit character. */
436
437 int unibyte_display_via_language_environment;
438
439 /* Nonzero means we have more than one non-mini-buffer-only frame.
440 Not guaranteed to be accurate except while parsing
441 frame-title-format. */
442
443 int multiple_frames;
444
445 Lisp_Object Vglobal_mode_string;
446
447
448 /* List of variables (symbols) which hold markers for overlay arrows.
449 The symbols on this list are examined during redisplay to determine
450 where to display overlay arrows. */
451
452 Lisp_Object Voverlay_arrow_variable_list;
453
454 /* Marker for where to display an arrow on top of the buffer text. */
455
456 Lisp_Object Voverlay_arrow_position;
457
458 /* String to display for the arrow. Only used on terminal frames. */
459
460 Lisp_Object Voverlay_arrow_string;
461
462 /* Values of those variables at last redisplay are stored as
463 properties on `overlay-arrow-position' symbol. However, if
464 Voverlay_arrow_position is a marker, last-arrow-position is its
465 numerical position. */
466
467 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
468
469 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
470 properties on a symbol in overlay-arrow-variable-list. */
471
472 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
473
474 /* Like mode-line-format, but for the title bar on a visible frame. */
475
476 Lisp_Object Vframe_title_format;
477
478 /* Like mode-line-format, but for the title bar on an iconified frame. */
479
480 Lisp_Object Vicon_title_format;
481
482 /* List of functions to call when a window's size changes. These
483 functions get one arg, a frame on which one or more windows' sizes
484 have changed. */
485
486 static Lisp_Object Vwindow_size_change_functions;
487
488 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
489
490 /* Nonzero if an overlay arrow has been displayed in this window. */
491
492 static int overlay_arrow_seen;
493
494 /* Nonzero means highlight the region even in nonselected windows. */
495
496 int highlight_nonselected_windows;
497
498 /* If cursor motion alone moves point off frame, try scrolling this
499 many lines up or down if that will bring it back. */
500
501 static EMACS_INT scroll_step;
502
503 /* Nonzero means scroll just far enough to bring point back on the
504 screen, when appropriate. */
505
506 static EMACS_INT scroll_conservatively;
507
508 /* Recenter the window whenever point gets within this many lines of
509 the top or bottom of the window. This value is translated into a
510 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
511 that there is really a fixed pixel height scroll margin. */
512
513 EMACS_INT scroll_margin;
514
515 /* Number of windows showing the buffer of the selected window (or
516 another buffer with the same base buffer). keyboard.c refers to
517 this. */
518
519 int buffer_shared;
520
521 /* Vector containing glyphs for an ellipsis `...'. */
522
523 static Lisp_Object default_invis_vector[3];
524
525 /* Zero means display the mode-line/header-line/menu-bar in the default face
526 (this slightly odd definition is for compatibility with previous versions
527 of emacs), non-zero means display them using their respective faces.
528
529 This variable is deprecated. */
530
531 int mode_line_inverse_video;
532
533 /* Prompt to display in front of the mini-buffer contents. */
534
535 Lisp_Object minibuf_prompt;
536
537 /* Width of current mini-buffer prompt. Only set after display_line
538 of the line that contains the prompt. */
539
540 int minibuf_prompt_width;
541
542 /* This is the window where the echo area message was displayed. It
543 is always a mini-buffer window, but it may not be the same window
544 currently active as a mini-buffer. */
545
546 Lisp_Object echo_area_window;
547
548 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
549 pushes the current message and the value of
550 message_enable_multibyte on the stack, the function restore_message
551 pops the stack and displays MESSAGE again. */
552
553 Lisp_Object Vmessage_stack;
554
555 /* Nonzero means multibyte characters were enabled when the echo area
556 message was specified. */
557
558 int message_enable_multibyte;
559
560 /* Nonzero if we should redraw the mode lines on the next redisplay. */
561
562 int update_mode_lines;
563
564 /* Nonzero if window sizes or contents have changed since last
565 redisplay that finished. */
566
567 int windows_or_buffers_changed;
568
569 /* Nonzero means a frame's cursor type has been changed. */
570
571 int cursor_type_changed;
572
573 /* Nonzero after display_mode_line if %l was used and it displayed a
574 line number. */
575
576 int line_number_displayed;
577
578 /* Maximum buffer size for which to display line numbers. */
579
580 Lisp_Object Vline_number_display_limit;
581
582 /* Line width to consider when repositioning for line number display. */
583
584 static EMACS_INT line_number_display_limit_width;
585
586 /* Number of lines to keep in the message log buffer. t means
587 infinite. nil means don't log at all. */
588
589 Lisp_Object Vmessage_log_max;
590
591 /* The name of the *Messages* buffer, a string. */
592
593 static Lisp_Object Vmessages_buffer_name;
594
595 /* Current, index 0, and last displayed echo area message. Either
596 buffers from echo_buffers, or nil to indicate no message. */
597
598 Lisp_Object echo_area_buffer[2];
599
600 /* The buffers referenced from echo_area_buffer. */
601
602 static Lisp_Object echo_buffer[2];
603
604 /* A vector saved used in with_area_buffer to reduce consing. */
605
606 static Lisp_Object Vwith_echo_area_save_vector;
607
608 /* Non-zero means display_echo_area should display the last echo area
609 message again. Set by redisplay_preserve_echo_area. */
610
611 static int display_last_displayed_message_p;
612
613 /* Nonzero if echo area is being used by print; zero if being used by
614 message. */
615
616 int message_buf_print;
617
618 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
619
620 Lisp_Object Qinhibit_menubar_update;
621 int inhibit_menubar_update;
622
623 /* When evaluating expressions from menu bar items (enable conditions,
624 for instance), this is the frame they are being processed for. */
625
626 Lisp_Object Vmenu_updating_frame;
627
628 /* Maximum height for resizing mini-windows. Either a float
629 specifying a fraction of the available height, or an integer
630 specifying a number of lines. */
631
632 Lisp_Object Vmax_mini_window_height;
633
634 /* Non-zero means messages should be displayed with truncated
635 lines instead of being continued. */
636
637 int message_truncate_lines;
638 Lisp_Object Qmessage_truncate_lines;
639
640 /* Set to 1 in clear_message to make redisplay_internal aware
641 of an emptied echo area. */
642
643 static int message_cleared_p;
644
645 /* How to blink the default frame cursor off. */
646 Lisp_Object Vblink_cursor_alist;
647
648 /* A scratch glyph row with contents used for generating truncation
649 glyphs. Also used in direct_output_for_insert. */
650
651 #define MAX_SCRATCH_GLYPHS 100
652 struct glyph_row scratch_glyph_row;
653 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
654
655 /* Ascent and height of the last line processed by move_it_to. */
656
657 static int last_max_ascent, last_height;
658
659 /* Non-zero if there's a help-echo in the echo area. */
660
661 int help_echo_showing_p;
662
663 /* If >= 0, computed, exact values of mode-line and header-line height
664 to use in the macros CURRENT_MODE_LINE_HEIGHT and
665 CURRENT_HEADER_LINE_HEIGHT. */
666
667 int current_mode_line_height, current_header_line_height;
668
669 /* The maximum distance to look ahead for text properties. Values
670 that are too small let us call compute_char_face and similar
671 functions too often which is expensive. Values that are too large
672 let us call compute_char_face and alike too often because we
673 might not be interested in text properties that far away. */
674
675 #define TEXT_PROP_DISTANCE_LIMIT 100
676
677 #if GLYPH_DEBUG
678
679 /* Variables to turn off display optimizations from Lisp. */
680
681 int inhibit_try_window_id, inhibit_try_window_reusing;
682 int inhibit_try_cursor_movement;
683
684 /* Non-zero means print traces of redisplay if compiled with
685 GLYPH_DEBUG != 0. */
686
687 int trace_redisplay_p;
688
689 #endif /* GLYPH_DEBUG */
690
691 #ifdef DEBUG_TRACE_MOVE
692 /* Non-zero means trace with TRACE_MOVE to stderr. */
693 int trace_move;
694
695 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
696 #else
697 #define TRACE_MOVE(x) (void) 0
698 #endif
699
700 /* Non-zero means automatically scroll windows horizontally to make
701 point visible. */
702
703 int automatic_hscrolling_p;
704 Lisp_Object Qauto_hscroll_mode;
705
706 /* How close to the margin can point get before the window is scrolled
707 horizontally. */
708 EMACS_INT hscroll_margin;
709
710 /* How much to scroll horizontally when point is inside the above margin. */
711 Lisp_Object Vhscroll_step;
712
713 /* The variable `resize-mini-windows'. If nil, don't resize
714 mini-windows. If t, always resize them to fit the text they
715 display. If `grow-only', let mini-windows grow only until they
716 become empty. */
717
718 Lisp_Object Vresize_mini_windows;
719
720 /* Buffer being redisplayed -- for redisplay_window_error. */
721
722 struct buffer *displayed_buffer;
723
724 /* Space between overline and text. */
725
726 EMACS_INT overline_margin;
727
728 /* Require underline to be at least this many screen pixels below baseline
729 This to avoid underline "merging" with the base of letters at small
730 font sizes, particularly when x_use_underline_position_properties is on. */
731
732 EMACS_INT underline_minimum_offset;
733
734 /* Value returned from text property handlers (see below). */
735
736 enum prop_handled
737 {
738 HANDLED_NORMALLY,
739 HANDLED_RECOMPUTE_PROPS,
740 HANDLED_OVERLAY_STRING_CONSUMED,
741 HANDLED_RETURN
742 };
743
744 /* A description of text properties that redisplay is interested
745 in. */
746
747 struct props
748 {
749 /* The name of the property. */
750 Lisp_Object *name;
751
752 /* A unique index for the property. */
753 enum prop_idx idx;
754
755 /* A handler function called to set up iterator IT from the property
756 at IT's current position. Value is used to steer handle_stop. */
757 enum prop_handled (*handler) P_ ((struct it *it));
758 };
759
760 static enum prop_handled handle_face_prop P_ ((struct it *));
761 static enum prop_handled handle_invisible_prop P_ ((struct it *));
762 static enum prop_handled handle_display_prop P_ ((struct it *));
763 static enum prop_handled handle_composition_prop P_ ((struct it *));
764 static enum prop_handled handle_overlay_change P_ ((struct it *));
765 static enum prop_handled handle_fontified_prop P_ ((struct it *));
766 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
767
768 /* Properties handled by iterators. */
769
770 static struct props it_props[] =
771 {
772 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
773 /* Handle `face' before `display' because some sub-properties of
774 `display' need to know the face. */
775 {&Qface, FACE_PROP_IDX, handle_face_prop},
776 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
777 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
778 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
779 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
780 {NULL, 0, NULL}
781 };
782
783 /* Value is the position described by X. If X is a marker, value is
784 the marker_position of X. Otherwise, value is X. */
785
786 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
787
788 /* Enumeration returned by some move_it_.* functions internally. */
789
790 enum move_it_result
791 {
792 /* Not used. Undefined value. */
793 MOVE_UNDEFINED,
794
795 /* Move ended at the requested buffer position or ZV. */
796 MOVE_POS_MATCH_OR_ZV,
797
798 /* Move ended at the requested X pixel position. */
799 MOVE_X_REACHED,
800
801 /* Move within a line ended at the end of a line that must be
802 continued. */
803 MOVE_LINE_CONTINUED,
804
805 /* Move within a line ended at the end of a line that would
806 be displayed truncated. */
807 MOVE_LINE_TRUNCATED,
808
809 /* Move within a line ended at a line end. */
810 MOVE_NEWLINE_OR_CR
811 };
812
813 /* This counter is used to clear the face cache every once in a while
814 in redisplay_internal. It is incremented for each redisplay.
815 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
816 cleared. */
817
818 #define CLEAR_FACE_CACHE_COUNT 500
819 static int clear_face_cache_count;
820
821 /* Similarly for the image cache. */
822
823 #ifdef HAVE_WINDOW_SYSTEM
824 #define CLEAR_IMAGE_CACHE_COUNT 101
825 static int clear_image_cache_count;
826 #endif
827
828 /* Non-zero while redisplay_internal is in progress. */
829
830 int redisplaying_p;
831
832 /* Non-zero means don't free realized faces. Bound while freeing
833 realized faces is dangerous because glyph matrices might still
834 reference them. */
835
836 int inhibit_free_realized_faces;
837 Lisp_Object Qinhibit_free_realized_faces;
838
839 /* If a string, XTread_socket generates an event to display that string.
840 (The display is done in read_char.) */
841
842 Lisp_Object help_echo_string;
843 Lisp_Object help_echo_window;
844 Lisp_Object help_echo_object;
845 int help_echo_pos;
846
847 /* Temporary variable for XTread_socket. */
848
849 Lisp_Object previous_help_echo_string;
850
851 /* Null glyph slice */
852
853 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
854
855 /* Platform-independent portion of hourglass implementation. */
856
857 /* Non-zero means we're allowed to display a hourglass pointer. */
858 int display_hourglass_p;
859
860 /* Non-zero means an hourglass cursor is currently shown. */
861 int hourglass_shown_p;
862
863 /* If non-null, an asynchronous timer that, when it expires, displays
864 an hourglass cursor on all frames. */
865 struct atimer *hourglass_atimer;
866
867 /* Number of seconds to wait before displaying an hourglass cursor. */
868 Lisp_Object Vhourglass_delay;
869
870 /* Default number of seconds to wait before displaying an hourglass
871 cursor. */
872 #define DEFAULT_HOURGLASS_DELAY 1
873
874 \f
875 /* Function prototypes. */
876
877 static void setup_for_ellipsis P_ ((struct it *, int));
878 static void mark_window_display_accurate_1 P_ ((struct window *, int));
879 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
880 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
881 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
882 static int redisplay_mode_lines P_ ((Lisp_Object, int));
883 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
884
885 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
886
887 static void handle_line_prefix P_ ((struct it *));
888
889 #if 0
890 static int invisible_text_between_p P_ ((struct it *, int, int));
891 #endif
892
893 static void pint2str P_ ((char *, int, int));
894 static void pint2hrstr P_ ((char *, int, int));
895 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
896 struct text_pos));
897 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
898 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
899 static void store_mode_line_noprop_char P_ ((char));
900 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
901 static void x_consider_frame_title P_ ((Lisp_Object));
902 static void handle_stop P_ ((struct it *));
903 static int tool_bar_lines_needed P_ ((struct frame *, int *));
904 static int single_display_spec_intangible_p P_ ((Lisp_Object));
905 static void ensure_echo_area_buffers P_ ((void));
906 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
907 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
908 static int with_echo_area_buffer P_ ((struct window *, int,
909 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
910 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
911 static void clear_garbaged_frames P_ ((void));
912 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
913 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
914 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static int display_echo_area P_ ((struct window *));
916 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
918 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
919 static int string_char_and_length P_ ((const unsigned char *, int, int *));
920 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
921 struct text_pos));
922 static int compute_window_start_on_continuation_line P_ ((struct window *));
923 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
924 static void insert_left_trunc_glyphs P_ ((struct it *));
925 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
926 Lisp_Object));
927 static void extend_face_to_end_of_line P_ ((struct it *));
928 static int append_space_for_newline P_ ((struct it *, int));
929 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
930 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
931 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
932 static int trailing_whitespace_p P_ ((int));
933 static int message_log_check_duplicate P_ ((int, int, int, int));
934 static void push_it P_ ((struct it *));
935 static void pop_it P_ ((struct it *));
936 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
937 static void select_frame_for_redisplay P_ ((Lisp_Object));
938 static void redisplay_internal P_ ((int));
939 static int echo_area_display P_ ((int));
940 static void redisplay_windows P_ ((Lisp_Object));
941 static void redisplay_window P_ ((Lisp_Object, int));
942 static Lisp_Object redisplay_window_error ();
943 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
944 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
945 static int update_menu_bar P_ ((struct frame *, int, int));
946 static int try_window_reusing_current_matrix P_ ((struct window *));
947 static int try_window_id P_ ((struct window *));
948 static int display_line P_ ((struct it *));
949 static int display_mode_lines P_ ((struct window *));
950 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
951 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
952 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
953 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
954 static void display_menu_bar P_ ((struct window *));
955 static int display_count_lines P_ ((int, int, int, int, int *));
956 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
957 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
958 static void compute_line_metrics P_ ((struct it *));
959 static void run_redisplay_end_trigger_hook P_ ((struct it *));
960 static int get_overlay_strings P_ ((struct it *, int));
961 static int get_overlay_strings_1 P_ ((struct it *, int, int));
962 static void next_overlay_string P_ ((struct it *));
963 static void reseat P_ ((struct it *, struct text_pos, int));
964 static void reseat_1 P_ ((struct it *, struct text_pos, int));
965 static void back_to_previous_visible_line_start P_ ((struct it *));
966 void reseat_at_previous_visible_line_start P_ ((struct it *));
967 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
968 static int next_element_from_ellipsis P_ ((struct it *));
969 static int next_element_from_display_vector P_ ((struct it *));
970 static int next_element_from_string P_ ((struct it *));
971 static int next_element_from_c_string P_ ((struct it *));
972 static int next_element_from_buffer P_ ((struct it *));
973 static int next_element_from_composition P_ ((struct it *));
974 static int next_element_from_image P_ ((struct it *));
975 static int next_element_from_stretch P_ ((struct it *));
976 static void load_overlay_strings P_ ((struct it *, int));
977 static int init_from_display_pos P_ ((struct it *, struct window *,
978 struct display_pos *));
979 static void reseat_to_string P_ ((struct it *, unsigned char *,
980 Lisp_Object, int, int, int, int));
981 static enum move_it_result
982 move_it_in_display_line_to (struct it *, EMACS_INT, int,
983 enum move_operation_enum);
984 void move_it_vertically_backward P_ ((struct it *, int));
985 static void init_to_row_start P_ ((struct it *, struct window *,
986 struct glyph_row *));
987 static int init_to_row_end P_ ((struct it *, struct window *,
988 struct glyph_row *));
989 static void back_to_previous_line_start P_ ((struct it *));
990 static int forward_to_next_line_start P_ ((struct it *, int *));
991 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
992 Lisp_Object, int));
993 static struct text_pos string_pos P_ ((int, Lisp_Object));
994 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
995 static int number_of_chars P_ ((unsigned char *, int));
996 static void compute_stop_pos P_ ((struct it *));
997 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
998 Lisp_Object));
999 static int face_before_or_after_it_pos P_ ((struct it *, int));
1000 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1001 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1002 Lisp_Object, Lisp_Object,
1003 struct text_pos *, int));
1004 static int underlying_face_id P_ ((struct it *));
1005 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1006 struct window *));
1007
1008 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1009 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1010
1011 #ifdef HAVE_WINDOW_SYSTEM
1012
1013 static void update_tool_bar P_ ((struct frame *, int));
1014 static void build_desired_tool_bar_string P_ ((struct frame *f));
1015 static int redisplay_tool_bar P_ ((struct frame *));
1016 static void display_tool_bar_line P_ ((struct it *, int));
1017 static void notice_overwritten_cursor P_ ((struct window *,
1018 enum glyph_row_area,
1019 int, int, int, int));
1020
1021
1022
1023 #endif /* HAVE_WINDOW_SYSTEM */
1024
1025 \f
1026 /***********************************************************************
1027 Window display dimensions
1028 ***********************************************************************/
1029
1030 /* Return the bottom boundary y-position for text lines in window W.
1031 This is the first y position at which a line cannot start.
1032 It is relative to the top of the window.
1033
1034 This is the height of W minus the height of a mode line, if any. */
1035
1036 INLINE int
1037 window_text_bottom_y (w)
1038 struct window *w;
1039 {
1040 int height = WINDOW_TOTAL_HEIGHT (w);
1041
1042 if (WINDOW_WANTS_MODELINE_P (w))
1043 height -= CURRENT_MODE_LINE_HEIGHT (w);
1044 return height;
1045 }
1046
1047 /* Return the pixel width of display area AREA of window W. AREA < 0
1048 means return the total width of W, not including fringes to
1049 the left and right of the window. */
1050
1051 INLINE int
1052 window_box_width (w, area)
1053 struct window *w;
1054 int area;
1055 {
1056 int cols = XFASTINT (w->total_cols);
1057 int pixels = 0;
1058
1059 if (!w->pseudo_window_p)
1060 {
1061 cols -= WINDOW_SCROLL_BAR_COLS (w);
1062
1063 if (area == TEXT_AREA)
1064 {
1065 if (INTEGERP (w->left_margin_cols))
1066 cols -= XFASTINT (w->left_margin_cols);
1067 if (INTEGERP (w->right_margin_cols))
1068 cols -= XFASTINT (w->right_margin_cols);
1069 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1070 }
1071 else if (area == LEFT_MARGIN_AREA)
1072 {
1073 cols = (INTEGERP (w->left_margin_cols)
1074 ? XFASTINT (w->left_margin_cols) : 0);
1075 pixels = 0;
1076 }
1077 else if (area == RIGHT_MARGIN_AREA)
1078 {
1079 cols = (INTEGERP (w->right_margin_cols)
1080 ? XFASTINT (w->right_margin_cols) : 0);
1081 pixels = 0;
1082 }
1083 }
1084
1085 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1086 }
1087
1088
1089 /* Return the pixel height of the display area of window W, not
1090 including mode lines of W, if any. */
1091
1092 INLINE int
1093 window_box_height (w)
1094 struct window *w;
1095 {
1096 struct frame *f = XFRAME (w->frame);
1097 int height = WINDOW_TOTAL_HEIGHT (w);
1098
1099 xassert (height >= 0);
1100
1101 /* Note: the code below that determines the mode-line/header-line
1102 height is essentially the same as that contained in the macro
1103 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1104 the appropriate glyph row has its `mode_line_p' flag set,
1105 and if it doesn't, uses estimate_mode_line_height instead. */
1106
1107 if (WINDOW_WANTS_MODELINE_P (w))
1108 {
1109 struct glyph_row *ml_row
1110 = (w->current_matrix && w->current_matrix->rows
1111 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1112 : 0);
1113 if (ml_row && ml_row->mode_line_p)
1114 height -= ml_row->height;
1115 else
1116 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1117 }
1118
1119 if (WINDOW_WANTS_HEADER_LINE_P (w))
1120 {
1121 struct glyph_row *hl_row
1122 = (w->current_matrix && w->current_matrix->rows
1123 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1124 : 0);
1125 if (hl_row && hl_row->mode_line_p)
1126 height -= hl_row->height;
1127 else
1128 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1129 }
1130
1131 /* With a very small font and a mode-line that's taller than
1132 default, we might end up with a negative height. */
1133 return max (0, height);
1134 }
1135
1136 /* Return the window-relative coordinate of the left edge of display
1137 area AREA of window W. AREA < 0 means return the left edge of the
1138 whole window, to the right of the left fringe of W. */
1139
1140 INLINE int
1141 window_box_left_offset (w, area)
1142 struct window *w;
1143 int area;
1144 {
1145 int x;
1146
1147 if (w->pseudo_window_p)
1148 return 0;
1149
1150 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1151
1152 if (area == TEXT_AREA)
1153 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1154 + window_box_width (w, LEFT_MARGIN_AREA));
1155 else if (area == RIGHT_MARGIN_AREA)
1156 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1157 + window_box_width (w, LEFT_MARGIN_AREA)
1158 + window_box_width (w, TEXT_AREA)
1159 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1160 ? 0
1161 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1162 else if (area == LEFT_MARGIN_AREA
1163 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1164 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1165
1166 return x;
1167 }
1168
1169
1170 /* Return the window-relative coordinate of the right edge of display
1171 area AREA of window W. AREA < 0 means return the left edge of the
1172 whole window, to the left of the right fringe of W. */
1173
1174 INLINE int
1175 window_box_right_offset (w, area)
1176 struct window *w;
1177 int area;
1178 {
1179 return window_box_left_offset (w, area) + window_box_width (w, area);
1180 }
1181
1182 /* Return the frame-relative coordinate of the left edge of display
1183 area AREA of window W. AREA < 0 means return the left edge of the
1184 whole window, to the right of the left fringe of W. */
1185
1186 INLINE int
1187 window_box_left (w, area)
1188 struct window *w;
1189 int area;
1190 {
1191 struct frame *f = XFRAME (w->frame);
1192 int x;
1193
1194 if (w->pseudo_window_p)
1195 return FRAME_INTERNAL_BORDER_WIDTH (f);
1196
1197 x = (WINDOW_LEFT_EDGE_X (w)
1198 + window_box_left_offset (w, area));
1199
1200 return x;
1201 }
1202
1203
1204 /* Return the frame-relative coordinate of the right edge of display
1205 area AREA of window W. AREA < 0 means return the left edge of the
1206 whole window, to the left of the right fringe of W. */
1207
1208 INLINE int
1209 window_box_right (w, area)
1210 struct window *w;
1211 int area;
1212 {
1213 return window_box_left (w, area) + window_box_width (w, area);
1214 }
1215
1216 /* Get the bounding box of the display area AREA of window W, without
1217 mode lines, in frame-relative coordinates. AREA < 0 means the
1218 whole window, not including the left and right fringes of
1219 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1220 coordinates of the upper-left corner of the box. Return in
1221 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1222
1223 INLINE void
1224 window_box (w, area, box_x, box_y, box_width, box_height)
1225 struct window *w;
1226 int area;
1227 int *box_x, *box_y, *box_width, *box_height;
1228 {
1229 if (box_width)
1230 *box_width = window_box_width (w, area);
1231 if (box_height)
1232 *box_height = window_box_height (w);
1233 if (box_x)
1234 *box_x = window_box_left (w, area);
1235 if (box_y)
1236 {
1237 *box_y = WINDOW_TOP_EDGE_Y (w);
1238 if (WINDOW_WANTS_HEADER_LINE_P (w))
1239 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1240 }
1241 }
1242
1243
1244 /* Get the bounding box of the display area AREA of window W, without
1245 mode lines. AREA < 0 means the whole window, not including the
1246 left and right fringe of the window. Return in *TOP_LEFT_X
1247 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1248 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1249 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1250 box. */
1251
1252 INLINE void
1253 window_box_edges (w, area, top_left_x, top_left_y,
1254 bottom_right_x, bottom_right_y)
1255 struct window *w;
1256 int area;
1257 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1258 {
1259 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1260 bottom_right_y);
1261 *bottom_right_x += *top_left_x;
1262 *bottom_right_y += *top_left_y;
1263 }
1264
1265
1266 \f
1267 /***********************************************************************
1268 Utilities
1269 ***********************************************************************/
1270
1271 /* Return the bottom y-position of the line the iterator IT is in.
1272 This can modify IT's settings. */
1273
1274 int
1275 line_bottom_y (it)
1276 struct it *it;
1277 {
1278 int line_height = it->max_ascent + it->max_descent;
1279 int line_top_y = it->current_y;
1280
1281 if (line_height == 0)
1282 {
1283 if (last_height)
1284 line_height = last_height;
1285 else if (IT_CHARPOS (*it) < ZV)
1286 {
1287 move_it_by_lines (it, 1, 1);
1288 line_height = (it->max_ascent || it->max_descent
1289 ? it->max_ascent + it->max_descent
1290 : last_height);
1291 }
1292 else
1293 {
1294 struct glyph_row *row = it->glyph_row;
1295
1296 /* Use the default character height. */
1297 it->glyph_row = NULL;
1298 it->what = IT_CHARACTER;
1299 it->c = ' ';
1300 it->len = 1;
1301 PRODUCE_GLYPHS (it);
1302 line_height = it->ascent + it->descent;
1303 it->glyph_row = row;
1304 }
1305 }
1306
1307 return line_top_y + line_height;
1308 }
1309
1310
1311 /* Return 1 if position CHARPOS is visible in window W.
1312 CHARPOS < 0 means return info about WINDOW_END position.
1313 If visible, set *X and *Y to pixel coordinates of top left corner.
1314 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1315 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1316
1317 int
1318 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1319 struct window *w;
1320 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1321 {
1322 struct it it;
1323 struct text_pos top;
1324 int visible_p = 0;
1325 struct buffer *old_buffer = NULL;
1326
1327 if (noninteractive)
1328 return visible_p;
1329
1330 if (XBUFFER (w->buffer) != current_buffer)
1331 {
1332 old_buffer = current_buffer;
1333 set_buffer_internal_1 (XBUFFER (w->buffer));
1334 }
1335
1336 SET_TEXT_POS_FROM_MARKER (top, w->start);
1337
1338 /* Compute exact mode line heights. */
1339 if (WINDOW_WANTS_MODELINE_P (w))
1340 current_mode_line_height
1341 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1342 current_buffer->mode_line_format);
1343
1344 if (WINDOW_WANTS_HEADER_LINE_P (w))
1345 current_header_line_height
1346 = display_mode_line (w, HEADER_LINE_FACE_ID,
1347 current_buffer->header_line_format);
1348
1349 start_display (&it, w, top);
1350 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1351 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1352
1353 /* Note that we may overshoot because of invisible text. */
1354 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1355 {
1356 int top_x = it.current_x;
1357 int top_y = it.current_y;
1358 int bottom_y = (last_height = 0, line_bottom_y (&it));
1359 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1360
1361 if (top_y < window_top_y)
1362 visible_p = bottom_y > window_top_y;
1363 else if (top_y < it.last_visible_y)
1364 visible_p = 1;
1365 if (visible_p)
1366 {
1367 if (it.method == GET_FROM_BUFFER)
1368 {
1369 Lisp_Object window, prop;
1370
1371 XSETWINDOW (window, w);
1372 prop = Fget_char_property (make_number (it.position.charpos),
1373 Qinvisible, window);
1374
1375 /* If charpos coincides with invisible text covered with an
1376 ellipsis, use the first glyph of the ellipsis to compute
1377 the pixel positions. */
1378 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1379 {
1380 struct glyph_row *row = it.glyph_row;
1381 struct glyph *glyph = row->glyphs[TEXT_AREA];
1382 struct glyph *end = glyph + row->used[TEXT_AREA];
1383 int x = row->x;
1384
1385 for (; glyph < end && glyph->charpos < charpos; glyph++)
1386 x += glyph->pixel_width;
1387
1388 top_x = x;
1389 }
1390 }
1391
1392 *x = top_x;
1393 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1394 *rtop = max (0, window_top_y - top_y);
1395 *rbot = max (0, bottom_y - it.last_visible_y);
1396 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1397 - max (top_y, window_top_y)));
1398 *vpos = it.vpos;
1399 }
1400 }
1401 else
1402 {
1403 struct it it2;
1404
1405 it2 = it;
1406 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1407 move_it_by_lines (&it, 1, 0);
1408 if (charpos < IT_CHARPOS (it)
1409 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1410 {
1411 visible_p = 1;
1412 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1413 *x = it2.current_x;
1414 *y = it2.current_y + it2.max_ascent - it2.ascent;
1415 *rtop = max (0, -it2.current_y);
1416 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1417 - it.last_visible_y));
1418 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1419 it.last_visible_y)
1420 - max (it2.current_y,
1421 WINDOW_HEADER_LINE_HEIGHT (w))));
1422 *vpos = it2.vpos;
1423 }
1424 }
1425
1426 if (old_buffer)
1427 set_buffer_internal_1 (old_buffer);
1428
1429 current_header_line_height = current_mode_line_height = -1;
1430
1431 if (visible_p && XFASTINT (w->hscroll) > 0)
1432 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1433
1434 #if 0
1435 /* Debugging code. */
1436 if (visible_p)
1437 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1438 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1439 else
1440 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1441 #endif
1442
1443 return visible_p;
1444 }
1445
1446
1447 /* Return the next character from STR which is MAXLEN bytes long.
1448 Return in *LEN the length of the character. This is like
1449 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1450 we find one, we return a `?', but with the length of the invalid
1451 character. */
1452
1453 static INLINE int
1454 string_char_and_length (str, maxlen, len)
1455 const unsigned char *str;
1456 int maxlen, *len;
1457 {
1458 int c;
1459
1460 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1461 if (!CHAR_VALID_P (c, 1))
1462 /* We may not change the length here because other places in Emacs
1463 don't use this function, i.e. they silently accept invalid
1464 characters. */
1465 c = '?';
1466
1467 return c;
1468 }
1469
1470
1471
1472 /* Given a position POS containing a valid character and byte position
1473 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1474
1475 static struct text_pos
1476 string_pos_nchars_ahead (pos, string, nchars)
1477 struct text_pos pos;
1478 Lisp_Object string;
1479 int nchars;
1480 {
1481 xassert (STRINGP (string) && nchars >= 0);
1482
1483 if (STRING_MULTIBYTE (string))
1484 {
1485 int rest = SBYTES (string) - BYTEPOS (pos);
1486 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1487 int len;
1488
1489 while (nchars--)
1490 {
1491 string_char_and_length (p, rest, &len);
1492 p += len, rest -= len;
1493 xassert (rest >= 0);
1494 CHARPOS (pos) += 1;
1495 BYTEPOS (pos) += len;
1496 }
1497 }
1498 else
1499 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1500
1501 return pos;
1502 }
1503
1504
1505 /* Value is the text position, i.e. character and byte position,
1506 for character position CHARPOS in STRING. */
1507
1508 static INLINE struct text_pos
1509 string_pos (charpos, string)
1510 int charpos;
1511 Lisp_Object string;
1512 {
1513 struct text_pos pos;
1514 xassert (STRINGP (string));
1515 xassert (charpos >= 0);
1516 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1517 return pos;
1518 }
1519
1520
1521 /* Value is a text position, i.e. character and byte position, for
1522 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1523 means recognize multibyte characters. */
1524
1525 static struct text_pos
1526 c_string_pos (charpos, s, multibyte_p)
1527 int charpos;
1528 unsigned char *s;
1529 int multibyte_p;
1530 {
1531 struct text_pos pos;
1532
1533 xassert (s != NULL);
1534 xassert (charpos >= 0);
1535
1536 if (multibyte_p)
1537 {
1538 int rest = strlen (s), len;
1539
1540 SET_TEXT_POS (pos, 0, 0);
1541 while (charpos--)
1542 {
1543 string_char_and_length (s, rest, &len);
1544 s += len, rest -= len;
1545 xassert (rest >= 0);
1546 CHARPOS (pos) += 1;
1547 BYTEPOS (pos) += len;
1548 }
1549 }
1550 else
1551 SET_TEXT_POS (pos, charpos, charpos);
1552
1553 return pos;
1554 }
1555
1556
1557 /* Value is the number of characters in C string S. MULTIBYTE_P
1558 non-zero means recognize multibyte characters. */
1559
1560 static int
1561 number_of_chars (s, multibyte_p)
1562 unsigned char *s;
1563 int multibyte_p;
1564 {
1565 int nchars;
1566
1567 if (multibyte_p)
1568 {
1569 int rest = strlen (s), len;
1570 unsigned char *p = (unsigned char *) s;
1571
1572 for (nchars = 0; rest > 0; ++nchars)
1573 {
1574 string_char_and_length (p, rest, &len);
1575 rest -= len, p += len;
1576 }
1577 }
1578 else
1579 nchars = strlen (s);
1580
1581 return nchars;
1582 }
1583
1584
1585 /* Compute byte position NEWPOS->bytepos corresponding to
1586 NEWPOS->charpos. POS is a known position in string STRING.
1587 NEWPOS->charpos must be >= POS.charpos. */
1588
1589 static void
1590 compute_string_pos (newpos, pos, string)
1591 struct text_pos *newpos, pos;
1592 Lisp_Object string;
1593 {
1594 xassert (STRINGP (string));
1595 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1596
1597 if (STRING_MULTIBYTE (string))
1598 *newpos = string_pos_nchars_ahead (pos, string,
1599 CHARPOS (*newpos) - CHARPOS (pos));
1600 else
1601 BYTEPOS (*newpos) = CHARPOS (*newpos);
1602 }
1603
1604 /* EXPORT:
1605 Return an estimation of the pixel height of mode or top lines on
1606 frame F. FACE_ID specifies what line's height to estimate. */
1607
1608 int
1609 estimate_mode_line_height (f, face_id)
1610 struct frame *f;
1611 enum face_id face_id;
1612 {
1613 #ifdef HAVE_WINDOW_SYSTEM
1614 if (FRAME_WINDOW_P (f))
1615 {
1616 int height = FONT_HEIGHT (FRAME_FONT (f));
1617
1618 /* This function is called so early when Emacs starts that the face
1619 cache and mode line face are not yet initialized. */
1620 if (FRAME_FACE_CACHE (f))
1621 {
1622 struct face *face = FACE_FROM_ID (f, face_id);
1623 if (face)
1624 {
1625 if (face->font)
1626 height = FONT_HEIGHT (face->font);
1627 if (face->box_line_width > 0)
1628 height += 2 * face->box_line_width;
1629 }
1630 }
1631
1632 return height;
1633 }
1634 #endif
1635
1636 return 1;
1637 }
1638
1639 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1640 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1641 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1642 not force the value into range. */
1643
1644 void
1645 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1646 FRAME_PTR f;
1647 register int pix_x, pix_y;
1648 int *x, *y;
1649 NativeRectangle *bounds;
1650 int noclip;
1651 {
1652
1653 #ifdef HAVE_WINDOW_SYSTEM
1654 if (FRAME_WINDOW_P (f))
1655 {
1656 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1657 even for negative values. */
1658 if (pix_x < 0)
1659 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1660 if (pix_y < 0)
1661 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1662
1663 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1664 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1665
1666 if (bounds)
1667 STORE_NATIVE_RECT (*bounds,
1668 FRAME_COL_TO_PIXEL_X (f, pix_x),
1669 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1670 FRAME_COLUMN_WIDTH (f) - 1,
1671 FRAME_LINE_HEIGHT (f) - 1);
1672
1673 if (!noclip)
1674 {
1675 if (pix_x < 0)
1676 pix_x = 0;
1677 else if (pix_x > FRAME_TOTAL_COLS (f))
1678 pix_x = FRAME_TOTAL_COLS (f);
1679
1680 if (pix_y < 0)
1681 pix_y = 0;
1682 else if (pix_y > FRAME_LINES (f))
1683 pix_y = FRAME_LINES (f);
1684 }
1685 }
1686 #endif
1687
1688 *x = pix_x;
1689 *y = pix_y;
1690 }
1691
1692
1693 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1694 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1695 can't tell the positions because W's display is not up to date,
1696 return 0. */
1697
1698 int
1699 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1700 struct window *w;
1701 int hpos, vpos;
1702 int *frame_x, *frame_y;
1703 {
1704 #ifdef HAVE_WINDOW_SYSTEM
1705 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1706 {
1707 int success_p;
1708
1709 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1710 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1711
1712 if (display_completed)
1713 {
1714 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1715 struct glyph *glyph = row->glyphs[TEXT_AREA];
1716 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1717
1718 hpos = row->x;
1719 vpos = row->y;
1720 while (glyph < end)
1721 {
1722 hpos += glyph->pixel_width;
1723 ++glyph;
1724 }
1725
1726 /* If first glyph is partially visible, its first visible position is still 0. */
1727 if (hpos < 0)
1728 hpos = 0;
1729
1730 success_p = 1;
1731 }
1732 else
1733 {
1734 hpos = vpos = 0;
1735 success_p = 0;
1736 }
1737
1738 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1739 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1740 return success_p;
1741 }
1742 #endif
1743
1744 *frame_x = hpos;
1745 *frame_y = vpos;
1746 return 1;
1747 }
1748
1749
1750 #ifdef HAVE_WINDOW_SYSTEM
1751
1752 /* Find the glyph under window-relative coordinates X/Y in window W.
1753 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1754 strings. Return in *HPOS and *VPOS the row and column number of
1755 the glyph found. Return in *AREA the glyph area containing X.
1756 Value is a pointer to the glyph found or null if X/Y is not on
1757 text, or we can't tell because W's current matrix is not up to
1758 date. */
1759
1760 static
1761 struct glyph *
1762 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1763 struct window *w;
1764 int x, y;
1765 int *hpos, *vpos, *dx, *dy, *area;
1766 {
1767 struct glyph *glyph, *end;
1768 struct glyph_row *row = NULL;
1769 int x0, i;
1770
1771 /* Find row containing Y. Give up if some row is not enabled. */
1772 for (i = 0; i < w->current_matrix->nrows; ++i)
1773 {
1774 row = MATRIX_ROW (w->current_matrix, i);
1775 if (!row->enabled_p)
1776 return NULL;
1777 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1778 break;
1779 }
1780
1781 *vpos = i;
1782 *hpos = 0;
1783
1784 /* Give up if Y is not in the window. */
1785 if (i == w->current_matrix->nrows)
1786 return NULL;
1787
1788 /* Get the glyph area containing X. */
1789 if (w->pseudo_window_p)
1790 {
1791 *area = TEXT_AREA;
1792 x0 = 0;
1793 }
1794 else
1795 {
1796 if (x < window_box_left_offset (w, TEXT_AREA))
1797 {
1798 *area = LEFT_MARGIN_AREA;
1799 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1800 }
1801 else if (x < window_box_right_offset (w, TEXT_AREA))
1802 {
1803 *area = TEXT_AREA;
1804 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1805 }
1806 else
1807 {
1808 *area = RIGHT_MARGIN_AREA;
1809 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1810 }
1811 }
1812
1813 /* Find glyph containing X. */
1814 glyph = row->glyphs[*area];
1815 end = glyph + row->used[*area];
1816 x -= x0;
1817 while (glyph < end && x >= glyph->pixel_width)
1818 {
1819 x -= glyph->pixel_width;
1820 ++glyph;
1821 }
1822
1823 if (glyph == end)
1824 return NULL;
1825
1826 if (dx)
1827 {
1828 *dx = x;
1829 *dy = y - (row->y + row->ascent - glyph->ascent);
1830 }
1831
1832 *hpos = glyph - row->glyphs[*area];
1833 return glyph;
1834 }
1835
1836
1837 /* EXPORT:
1838 Convert frame-relative x/y to coordinates relative to window W.
1839 Takes pseudo-windows into account. */
1840
1841 void
1842 frame_to_window_pixel_xy (w, x, y)
1843 struct window *w;
1844 int *x, *y;
1845 {
1846 if (w->pseudo_window_p)
1847 {
1848 /* A pseudo-window is always full-width, and starts at the
1849 left edge of the frame, plus a frame border. */
1850 struct frame *f = XFRAME (w->frame);
1851 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1852 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1853 }
1854 else
1855 {
1856 *x -= WINDOW_LEFT_EDGE_X (w);
1857 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1858 }
1859 }
1860
1861 /* EXPORT:
1862 Return in RECTS[] at most N clipping rectangles for glyph string S.
1863 Return the number of stored rectangles. */
1864
1865 int
1866 get_glyph_string_clip_rects (s, rects, n)
1867 struct glyph_string *s;
1868 NativeRectangle *rects;
1869 int n;
1870 {
1871 XRectangle r;
1872
1873 if (n <= 0)
1874 return 0;
1875
1876 if (s->row->full_width_p)
1877 {
1878 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1879 r.x = WINDOW_LEFT_EDGE_X (s->w);
1880 r.width = WINDOW_TOTAL_WIDTH (s->w);
1881
1882 /* Unless displaying a mode or menu bar line, which are always
1883 fully visible, clip to the visible part of the row. */
1884 if (s->w->pseudo_window_p)
1885 r.height = s->row->visible_height;
1886 else
1887 r.height = s->height;
1888 }
1889 else
1890 {
1891 /* This is a text line that may be partially visible. */
1892 r.x = window_box_left (s->w, s->area);
1893 r.width = window_box_width (s->w, s->area);
1894 r.height = s->row->visible_height;
1895 }
1896
1897 if (s->clip_head)
1898 if (r.x < s->clip_head->x)
1899 {
1900 if (r.width >= s->clip_head->x - r.x)
1901 r.width -= s->clip_head->x - r.x;
1902 else
1903 r.width = 0;
1904 r.x = s->clip_head->x;
1905 }
1906 if (s->clip_tail)
1907 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1908 {
1909 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1910 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1911 else
1912 r.width = 0;
1913 }
1914
1915 /* If S draws overlapping rows, it's sufficient to use the top and
1916 bottom of the window for clipping because this glyph string
1917 intentionally draws over other lines. */
1918 if (s->for_overlaps)
1919 {
1920 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1921 r.height = window_text_bottom_y (s->w) - r.y;
1922
1923 /* Alas, the above simple strategy does not work for the
1924 environments with anti-aliased text: if the same text is
1925 drawn onto the same place multiple times, it gets thicker.
1926 If the overlap we are processing is for the erased cursor, we
1927 take the intersection with the rectagle of the cursor. */
1928 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1929 {
1930 XRectangle rc, r_save = r;
1931
1932 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1933 rc.y = s->w->phys_cursor.y;
1934 rc.width = s->w->phys_cursor_width;
1935 rc.height = s->w->phys_cursor_height;
1936
1937 x_intersect_rectangles (&r_save, &rc, &r);
1938 }
1939 }
1940 else
1941 {
1942 /* Don't use S->y for clipping because it doesn't take partially
1943 visible lines into account. For example, it can be negative for
1944 partially visible lines at the top of a window. */
1945 if (!s->row->full_width_p
1946 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1947 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1948 else
1949 r.y = max (0, s->row->y);
1950
1951 /* If drawing a tool-bar window, draw it over the internal border
1952 at the top of the window. */
1953 if (WINDOWP (s->f->tool_bar_window)
1954 && s->w == XWINDOW (s->f->tool_bar_window))
1955 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1956 }
1957
1958 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1959
1960 /* If drawing the cursor, don't let glyph draw outside its
1961 advertised boundaries. Cleartype does this under some circumstances. */
1962 if (s->hl == DRAW_CURSOR)
1963 {
1964 struct glyph *glyph = s->first_glyph;
1965 int height, max_y;
1966
1967 if (s->x > r.x)
1968 {
1969 r.width -= s->x - r.x;
1970 r.x = s->x;
1971 }
1972 r.width = min (r.width, glyph->pixel_width);
1973
1974 /* If r.y is below window bottom, ensure that we still see a cursor. */
1975 height = min (glyph->ascent + glyph->descent,
1976 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1977 max_y = window_text_bottom_y (s->w) - height;
1978 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1979 if (s->ybase - glyph->ascent > max_y)
1980 {
1981 r.y = max_y;
1982 r.height = height;
1983 }
1984 else
1985 {
1986 /* Don't draw cursor glyph taller than our actual glyph. */
1987 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1988 if (height < r.height)
1989 {
1990 max_y = r.y + r.height;
1991 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1992 r.height = min (max_y - r.y, height);
1993 }
1994 }
1995 }
1996
1997 if (s->row->clip)
1998 {
1999 XRectangle r_save = r;
2000
2001 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2002 r.width = 0;
2003 }
2004
2005 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2006 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2007 {
2008 #ifdef CONVERT_FROM_XRECT
2009 CONVERT_FROM_XRECT (r, *rects);
2010 #else
2011 *rects = r;
2012 #endif
2013 return 1;
2014 }
2015 else
2016 {
2017 /* If we are processing overlapping and allowed to return
2018 multiple clipping rectangles, we exclude the row of the glyph
2019 string from the clipping rectangle. This is to avoid drawing
2020 the same text on the environment with anti-aliasing. */
2021 #ifdef CONVERT_FROM_XRECT
2022 XRectangle rs[2];
2023 #else
2024 XRectangle *rs = rects;
2025 #endif
2026 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2027
2028 if (s->for_overlaps & OVERLAPS_PRED)
2029 {
2030 rs[i] = r;
2031 if (r.y + r.height > row_y)
2032 {
2033 if (r.y < row_y)
2034 rs[i].height = row_y - r.y;
2035 else
2036 rs[i].height = 0;
2037 }
2038 i++;
2039 }
2040 if (s->for_overlaps & OVERLAPS_SUCC)
2041 {
2042 rs[i] = r;
2043 if (r.y < row_y + s->row->visible_height)
2044 {
2045 if (r.y + r.height > row_y + s->row->visible_height)
2046 {
2047 rs[i].y = row_y + s->row->visible_height;
2048 rs[i].height = r.y + r.height - rs[i].y;
2049 }
2050 else
2051 rs[i].height = 0;
2052 }
2053 i++;
2054 }
2055
2056 n = i;
2057 #ifdef CONVERT_FROM_XRECT
2058 for (i = 0; i < n; i++)
2059 CONVERT_FROM_XRECT (rs[i], rects[i]);
2060 #endif
2061 return n;
2062 }
2063 }
2064
2065 /* EXPORT:
2066 Return in *NR the clipping rectangle for glyph string S. */
2067
2068 void
2069 get_glyph_string_clip_rect (s, nr)
2070 struct glyph_string *s;
2071 NativeRectangle *nr;
2072 {
2073 get_glyph_string_clip_rects (s, nr, 1);
2074 }
2075
2076
2077 /* EXPORT:
2078 Return the position and height of the phys cursor in window W.
2079 Set w->phys_cursor_width to width of phys cursor.
2080 */
2081
2082 void
2083 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2084 struct window *w;
2085 struct glyph_row *row;
2086 struct glyph *glyph;
2087 int *xp, *yp, *heightp;
2088 {
2089 struct frame *f = XFRAME (WINDOW_FRAME (w));
2090 int x, y, wd, h, h0, y0;
2091
2092 /* Compute the width of the rectangle to draw. If on a stretch
2093 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2094 rectangle as wide as the glyph, but use a canonical character
2095 width instead. */
2096 wd = glyph->pixel_width - 1;
2097 #ifdef HAVE_NTGUI
2098 wd++; /* Why? */
2099 #endif
2100
2101 x = w->phys_cursor.x;
2102 if (x < 0)
2103 {
2104 wd += x;
2105 x = 0;
2106 }
2107
2108 if (glyph->type == STRETCH_GLYPH
2109 && !x_stretch_cursor_p)
2110 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2111 w->phys_cursor_width = wd;
2112
2113 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2114
2115 /* If y is below window bottom, ensure that we still see a cursor. */
2116 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2117
2118 h = max (h0, glyph->ascent + glyph->descent);
2119 h0 = min (h0, glyph->ascent + glyph->descent);
2120
2121 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2122 if (y < y0)
2123 {
2124 h = max (h - (y0 - y) + 1, h0);
2125 y = y0 - 1;
2126 }
2127 else
2128 {
2129 y0 = window_text_bottom_y (w) - h0;
2130 if (y > y0)
2131 {
2132 h += y - y0;
2133 y = y0;
2134 }
2135 }
2136
2137 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2138 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2139 *heightp = h;
2140 }
2141
2142 /*
2143 * Remember which glyph the mouse is over.
2144 */
2145
2146 void
2147 remember_mouse_glyph (f, gx, gy, rect)
2148 struct frame *f;
2149 int gx, gy;
2150 NativeRectangle *rect;
2151 {
2152 Lisp_Object window;
2153 struct window *w;
2154 struct glyph_row *r, *gr, *end_row;
2155 enum window_part part;
2156 enum glyph_row_area area;
2157 int x, y, width, height;
2158
2159 /* Try to determine frame pixel position and size of the glyph under
2160 frame pixel coordinates X/Y on frame F. */
2161
2162 if (!f->glyphs_initialized_p
2163 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2164 NILP (window)))
2165 {
2166 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2167 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2168 goto virtual_glyph;
2169 }
2170
2171 w = XWINDOW (window);
2172 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2173 height = WINDOW_FRAME_LINE_HEIGHT (w);
2174
2175 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2176 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2177
2178 if (w->pseudo_window_p)
2179 {
2180 area = TEXT_AREA;
2181 part = ON_MODE_LINE; /* Don't adjust margin. */
2182 goto text_glyph;
2183 }
2184
2185 switch (part)
2186 {
2187 case ON_LEFT_MARGIN:
2188 area = LEFT_MARGIN_AREA;
2189 goto text_glyph;
2190
2191 case ON_RIGHT_MARGIN:
2192 area = RIGHT_MARGIN_AREA;
2193 goto text_glyph;
2194
2195 case ON_HEADER_LINE:
2196 case ON_MODE_LINE:
2197 gr = (part == ON_HEADER_LINE
2198 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2199 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2200 gy = gr->y;
2201 area = TEXT_AREA;
2202 goto text_glyph_row_found;
2203
2204 case ON_TEXT:
2205 area = TEXT_AREA;
2206
2207 text_glyph:
2208 gr = 0; gy = 0;
2209 for (; r <= end_row && r->enabled_p; ++r)
2210 if (r->y + r->height > y)
2211 {
2212 gr = r; gy = r->y;
2213 break;
2214 }
2215
2216 text_glyph_row_found:
2217 if (gr && gy <= y)
2218 {
2219 struct glyph *g = gr->glyphs[area];
2220 struct glyph *end = g + gr->used[area];
2221
2222 height = gr->height;
2223 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2224 if (gx + g->pixel_width > x)
2225 break;
2226
2227 if (g < end)
2228 {
2229 if (g->type == IMAGE_GLYPH)
2230 {
2231 /* Don't remember when mouse is over image, as
2232 image may have hot-spots. */
2233 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2234 return;
2235 }
2236 width = g->pixel_width;
2237 }
2238 else
2239 {
2240 /* Use nominal char spacing at end of line. */
2241 x -= gx;
2242 gx += (x / width) * width;
2243 }
2244
2245 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2246 gx += window_box_left_offset (w, area);
2247 }
2248 else
2249 {
2250 /* Use nominal line height at end of window. */
2251 gx = (x / width) * width;
2252 y -= gy;
2253 gy += (y / height) * height;
2254 }
2255 break;
2256
2257 case ON_LEFT_FRINGE:
2258 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2259 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2260 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2261 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2262 goto row_glyph;
2263
2264 case ON_RIGHT_FRINGE:
2265 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2266 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2267 : window_box_right_offset (w, TEXT_AREA));
2268 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2269 goto row_glyph;
2270
2271 case ON_SCROLL_BAR:
2272 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2273 ? 0
2274 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2275 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2276 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2277 : 0)));
2278 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2279
2280 row_glyph:
2281 gr = 0, gy = 0;
2282 for (; r <= end_row && r->enabled_p; ++r)
2283 if (r->y + r->height > y)
2284 {
2285 gr = r; gy = r->y;
2286 break;
2287 }
2288
2289 if (gr && gy <= y)
2290 height = gr->height;
2291 else
2292 {
2293 /* Use nominal line height at end of window. */
2294 y -= gy;
2295 gy += (y / height) * height;
2296 }
2297 break;
2298
2299 default:
2300 ;
2301 virtual_glyph:
2302 /* If there is no glyph under the mouse, then we divide the screen
2303 into a grid of the smallest glyph in the frame, and use that
2304 as our "glyph". */
2305
2306 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2307 round down even for negative values. */
2308 if (gx < 0)
2309 gx -= width - 1;
2310 if (gy < 0)
2311 gy -= height - 1;
2312
2313 gx = (gx / width) * width;
2314 gy = (gy / height) * height;
2315
2316 goto store_rect;
2317 }
2318
2319 gx += WINDOW_LEFT_EDGE_X (w);
2320 gy += WINDOW_TOP_EDGE_Y (w);
2321
2322 store_rect:
2323 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2324
2325 /* Visible feedback for debugging. */
2326 #if 0
2327 #if HAVE_X_WINDOWS
2328 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2329 f->output_data.x->normal_gc,
2330 gx, gy, width, height);
2331 #endif
2332 #endif
2333 }
2334
2335
2336 #endif /* HAVE_WINDOW_SYSTEM */
2337
2338 \f
2339 /***********************************************************************
2340 Lisp form evaluation
2341 ***********************************************************************/
2342
2343 /* Error handler for safe_eval and safe_call. */
2344
2345 static Lisp_Object
2346 safe_eval_handler (arg)
2347 Lisp_Object arg;
2348 {
2349 add_to_log ("Error during redisplay: %s", arg, Qnil);
2350 return Qnil;
2351 }
2352
2353
2354 /* Evaluate SEXPR and return the result, or nil if something went
2355 wrong. Prevent redisplay during the evaluation. */
2356
2357 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2358 Return the result, or nil if something went wrong. Prevent
2359 redisplay during the evaluation. */
2360
2361 Lisp_Object
2362 safe_call (nargs, args)
2363 int nargs;
2364 Lisp_Object *args;
2365 {
2366 Lisp_Object val;
2367
2368 if (inhibit_eval_during_redisplay)
2369 val = Qnil;
2370 else
2371 {
2372 int count = SPECPDL_INDEX ();
2373 struct gcpro gcpro1;
2374
2375 GCPRO1 (args[0]);
2376 gcpro1.nvars = nargs;
2377 specbind (Qinhibit_redisplay, Qt);
2378 /* Use Qt to ensure debugger does not run,
2379 so there is no possibility of wanting to redisplay. */
2380 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2381 safe_eval_handler);
2382 UNGCPRO;
2383 val = unbind_to (count, val);
2384 }
2385
2386 return val;
2387 }
2388
2389
2390 /* Call function FN with one argument ARG.
2391 Return the result, or nil if something went wrong. */
2392
2393 Lisp_Object
2394 safe_call1 (fn, arg)
2395 Lisp_Object fn, arg;
2396 {
2397 Lisp_Object args[2];
2398 args[0] = fn;
2399 args[1] = arg;
2400 return safe_call (2, args);
2401 }
2402
2403 static Lisp_Object Qeval;
2404
2405 Lisp_Object
2406 safe_eval (Lisp_Object sexpr)
2407 {
2408 return safe_call1 (Qeval, sexpr);
2409 }
2410
2411 /* Call function FN with one argument ARG.
2412 Return the result, or nil if something went wrong. */
2413
2414 Lisp_Object
2415 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2416 {
2417 Lisp_Object args[3];
2418 args[0] = fn;
2419 args[1] = arg1;
2420 args[2] = arg2;
2421 return safe_call (3, args);
2422 }
2423
2424
2425 \f
2426 /***********************************************************************
2427 Debugging
2428 ***********************************************************************/
2429
2430 #if 0
2431
2432 /* Define CHECK_IT to perform sanity checks on iterators.
2433 This is for debugging. It is too slow to do unconditionally. */
2434
2435 static void
2436 check_it (it)
2437 struct it *it;
2438 {
2439 if (it->method == GET_FROM_STRING)
2440 {
2441 xassert (STRINGP (it->string));
2442 xassert (IT_STRING_CHARPOS (*it) >= 0);
2443 }
2444 else
2445 {
2446 xassert (IT_STRING_CHARPOS (*it) < 0);
2447 if (it->method == GET_FROM_BUFFER)
2448 {
2449 /* Check that character and byte positions agree. */
2450 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2451 }
2452 }
2453
2454 if (it->dpvec)
2455 xassert (it->current.dpvec_index >= 0);
2456 else
2457 xassert (it->current.dpvec_index < 0);
2458 }
2459
2460 #define CHECK_IT(IT) check_it ((IT))
2461
2462 #else /* not 0 */
2463
2464 #define CHECK_IT(IT) (void) 0
2465
2466 #endif /* not 0 */
2467
2468
2469 #if GLYPH_DEBUG
2470
2471 /* Check that the window end of window W is what we expect it
2472 to be---the last row in the current matrix displaying text. */
2473
2474 static void
2475 check_window_end (w)
2476 struct window *w;
2477 {
2478 if (!MINI_WINDOW_P (w)
2479 && !NILP (w->window_end_valid))
2480 {
2481 struct glyph_row *row;
2482 xassert ((row = MATRIX_ROW (w->current_matrix,
2483 XFASTINT (w->window_end_vpos)),
2484 !row->enabled_p
2485 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2486 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2487 }
2488 }
2489
2490 #define CHECK_WINDOW_END(W) check_window_end ((W))
2491
2492 #else /* not GLYPH_DEBUG */
2493
2494 #define CHECK_WINDOW_END(W) (void) 0
2495
2496 #endif /* not GLYPH_DEBUG */
2497
2498
2499 \f
2500 /***********************************************************************
2501 Iterator initialization
2502 ***********************************************************************/
2503
2504 /* Initialize IT for displaying current_buffer in window W, starting
2505 at character position CHARPOS. CHARPOS < 0 means that no buffer
2506 position is specified which is useful when the iterator is assigned
2507 a position later. BYTEPOS is the byte position corresponding to
2508 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2509
2510 If ROW is not null, calls to produce_glyphs with IT as parameter
2511 will produce glyphs in that row.
2512
2513 BASE_FACE_ID is the id of a base face to use. It must be one of
2514 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2515 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2516 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2517
2518 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2520 will be initialized to use the corresponding mode line glyph row of
2521 the desired matrix of W. */
2522
2523 void
2524 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2525 struct it *it;
2526 struct window *w;
2527 int charpos, bytepos;
2528 struct glyph_row *row;
2529 enum face_id base_face_id;
2530 {
2531 int highlight_region_p;
2532 enum face_id remapped_base_face_id = base_face_id;
2533
2534 /* Some precondition checks. */
2535 xassert (w != NULL && it != NULL);
2536 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2537 && charpos <= ZV));
2538
2539 /* If face attributes have been changed since the last redisplay,
2540 free realized faces now because they depend on face definitions
2541 that might have changed. Don't free faces while there might be
2542 desired matrices pending which reference these faces. */
2543 if (face_change_count && !inhibit_free_realized_faces)
2544 {
2545 face_change_count = 0;
2546 free_all_realized_faces (Qnil);
2547 }
2548
2549 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2550 if (! NILP (Vface_remapping_alist))
2551 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2552
2553 /* Use one of the mode line rows of W's desired matrix if
2554 appropriate. */
2555 if (row == NULL)
2556 {
2557 if (base_face_id == MODE_LINE_FACE_ID
2558 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2559 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2560 else if (base_face_id == HEADER_LINE_FACE_ID)
2561 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2562 }
2563
2564 /* Clear IT. */
2565 bzero (it, sizeof *it);
2566 it->current.overlay_string_index = -1;
2567 it->current.dpvec_index = -1;
2568 it->base_face_id = remapped_base_face_id;
2569 it->string = Qnil;
2570 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2571
2572 /* The window in which we iterate over current_buffer: */
2573 XSETWINDOW (it->window, w);
2574 it->w = w;
2575 it->f = XFRAME (w->frame);
2576
2577 /* Extra space between lines (on window systems only). */
2578 if (base_face_id == DEFAULT_FACE_ID
2579 && FRAME_WINDOW_P (it->f))
2580 {
2581 if (NATNUMP (current_buffer->extra_line_spacing))
2582 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2583 else if (FLOATP (current_buffer->extra_line_spacing))
2584 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2585 * FRAME_LINE_HEIGHT (it->f));
2586 else if (it->f->extra_line_spacing > 0)
2587 it->extra_line_spacing = it->f->extra_line_spacing;
2588 it->max_extra_line_spacing = 0;
2589 }
2590
2591 /* If realized faces have been removed, e.g. because of face
2592 attribute changes of named faces, recompute them. When running
2593 in batch mode, the face cache of the initial frame is null. If
2594 we happen to get called, make a dummy face cache. */
2595 if (FRAME_FACE_CACHE (it->f) == NULL)
2596 init_frame_faces (it->f);
2597 if (FRAME_FACE_CACHE (it->f)->used == 0)
2598 recompute_basic_faces (it->f);
2599
2600 /* Current value of the `slice', `space-width', and 'height' properties. */
2601 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2602 it->space_width = Qnil;
2603 it->font_height = Qnil;
2604 it->override_ascent = -1;
2605
2606 /* Are control characters displayed as `^C'? */
2607 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2608
2609 /* -1 means everything between a CR and the following line end
2610 is invisible. >0 means lines indented more than this value are
2611 invisible. */
2612 it->selective = (INTEGERP (current_buffer->selective_display)
2613 ? XFASTINT (current_buffer->selective_display)
2614 : (!NILP (current_buffer->selective_display)
2615 ? -1 : 0));
2616 it->selective_display_ellipsis_p
2617 = !NILP (current_buffer->selective_display_ellipses);
2618
2619 /* Display table to use. */
2620 it->dp = window_display_table (w);
2621
2622 /* Are multibyte characters enabled in current_buffer? */
2623 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2624
2625 /* Non-zero if we should highlight the region. */
2626 highlight_region_p
2627 = (!NILP (Vtransient_mark_mode)
2628 && !NILP (current_buffer->mark_active)
2629 && XMARKER (current_buffer->mark)->buffer != 0);
2630
2631 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2632 start and end of a visible region in window IT->w. Set both to
2633 -1 to indicate no region. */
2634 if (highlight_region_p
2635 /* Maybe highlight only in selected window. */
2636 && (/* Either show region everywhere. */
2637 highlight_nonselected_windows
2638 /* Or show region in the selected window. */
2639 || w == XWINDOW (selected_window)
2640 /* Or show the region if we are in the mini-buffer and W is
2641 the window the mini-buffer refers to. */
2642 || (MINI_WINDOW_P (XWINDOW (selected_window))
2643 && WINDOWP (minibuf_selected_window)
2644 && w == XWINDOW (minibuf_selected_window))))
2645 {
2646 int charpos = marker_position (current_buffer->mark);
2647 it->region_beg_charpos = min (PT, charpos);
2648 it->region_end_charpos = max (PT, charpos);
2649 }
2650 else
2651 it->region_beg_charpos = it->region_end_charpos = -1;
2652
2653 /* Get the position at which the redisplay_end_trigger hook should
2654 be run, if it is to be run at all. */
2655 if (MARKERP (w->redisplay_end_trigger)
2656 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2657 it->redisplay_end_trigger_charpos
2658 = marker_position (w->redisplay_end_trigger);
2659 else if (INTEGERP (w->redisplay_end_trigger))
2660 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2661
2662 /* Correct bogus values of tab_width. */
2663 it->tab_width = XINT (current_buffer->tab_width);
2664 if (it->tab_width <= 0 || it->tab_width > 1000)
2665 it->tab_width = 8;
2666
2667 /* Are lines in the display truncated? */
2668 if (base_face_id != DEFAULT_FACE_ID
2669 || XINT (it->w->hscroll)
2670 || (! WINDOW_FULL_WIDTH_P (it->w)
2671 && ((!NILP (Vtruncate_partial_width_windows)
2672 && !INTEGERP (Vtruncate_partial_width_windows))
2673 || (INTEGERP (Vtruncate_partial_width_windows)
2674 && (WINDOW_TOTAL_COLS (it->w)
2675 < XINT (Vtruncate_partial_width_windows))))))
2676 it->line_wrap = TRUNCATE;
2677 else if (NILP (current_buffer->truncate_lines))
2678 it->line_wrap = NILP (current_buffer->word_wrap)
2679 ? WINDOW_WRAP : WORD_WRAP;
2680 else
2681 it->line_wrap = TRUNCATE;
2682
2683 /* Get dimensions of truncation and continuation glyphs. These are
2684 displayed as fringe bitmaps under X, so we don't need them for such
2685 frames. */
2686 if (!FRAME_WINDOW_P (it->f))
2687 {
2688 if (it->line_wrap == TRUNCATE)
2689 {
2690 /* We will need the truncation glyph. */
2691 xassert (it->glyph_row == NULL);
2692 produce_special_glyphs (it, IT_TRUNCATION);
2693 it->truncation_pixel_width = it->pixel_width;
2694 }
2695 else
2696 {
2697 /* We will need the continuation glyph. */
2698 xassert (it->glyph_row == NULL);
2699 produce_special_glyphs (it, IT_CONTINUATION);
2700 it->continuation_pixel_width = it->pixel_width;
2701 }
2702
2703 /* Reset these values to zero because the produce_special_glyphs
2704 above has changed them. */
2705 it->pixel_width = it->ascent = it->descent = 0;
2706 it->phys_ascent = it->phys_descent = 0;
2707 }
2708
2709 /* Set this after getting the dimensions of truncation and
2710 continuation glyphs, so that we don't produce glyphs when calling
2711 produce_special_glyphs, above. */
2712 it->glyph_row = row;
2713 it->area = TEXT_AREA;
2714
2715 /* Get the dimensions of the display area. The display area
2716 consists of the visible window area plus a horizontally scrolled
2717 part to the left of the window. All x-values are relative to the
2718 start of this total display area. */
2719 if (base_face_id != DEFAULT_FACE_ID)
2720 {
2721 /* Mode lines, menu bar in terminal frames. */
2722 it->first_visible_x = 0;
2723 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2724 }
2725 else
2726 {
2727 it->first_visible_x
2728 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2729 it->last_visible_x = (it->first_visible_x
2730 + window_box_width (w, TEXT_AREA));
2731
2732 /* If we truncate lines, leave room for the truncator glyph(s) at
2733 the right margin. Otherwise, leave room for the continuation
2734 glyph(s). Truncation and continuation glyphs are not inserted
2735 for window-based redisplay. */
2736 if (!FRAME_WINDOW_P (it->f))
2737 {
2738 if (it->line_wrap == TRUNCATE)
2739 it->last_visible_x -= it->truncation_pixel_width;
2740 else
2741 it->last_visible_x -= it->continuation_pixel_width;
2742 }
2743
2744 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2745 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2746 }
2747
2748 /* Leave room for a border glyph. */
2749 if (!FRAME_WINDOW_P (it->f)
2750 && !WINDOW_RIGHTMOST_P (it->w))
2751 it->last_visible_x -= 1;
2752
2753 it->last_visible_y = window_text_bottom_y (w);
2754
2755 /* For mode lines and alike, arrange for the first glyph having a
2756 left box line if the face specifies a box. */
2757 if (base_face_id != DEFAULT_FACE_ID)
2758 {
2759 struct face *face;
2760
2761 it->face_id = remapped_base_face_id;
2762
2763 /* If we have a boxed mode line, make the first character appear
2764 with a left box line. */
2765 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2766 if (face->box != FACE_NO_BOX)
2767 it->start_of_box_run_p = 1;
2768 }
2769
2770 /* If a buffer position was specified, set the iterator there,
2771 getting overlays and face properties from that position. */
2772 if (charpos >= BUF_BEG (current_buffer))
2773 {
2774 it->end_charpos = ZV;
2775 it->face_id = -1;
2776 IT_CHARPOS (*it) = charpos;
2777
2778 /* Compute byte position if not specified. */
2779 if (bytepos < charpos)
2780 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2781 else
2782 IT_BYTEPOS (*it) = bytepos;
2783
2784 it->start = it->current;
2785
2786 /* Compute faces etc. */
2787 reseat (it, it->current.pos, 1);
2788 }
2789
2790 CHECK_IT (it);
2791 }
2792
2793
2794 /* Initialize IT for the display of window W with window start POS. */
2795
2796 void
2797 start_display (it, w, pos)
2798 struct it *it;
2799 struct window *w;
2800 struct text_pos pos;
2801 {
2802 struct glyph_row *row;
2803 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2804
2805 row = w->desired_matrix->rows + first_vpos;
2806 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2807 it->first_vpos = first_vpos;
2808
2809 /* Don't reseat to previous visible line start if current start
2810 position is in a string or image. */
2811 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2812 {
2813 int start_at_line_beg_p;
2814 int first_y = it->current_y;
2815
2816 /* If window start is not at a line start, skip forward to POS to
2817 get the correct continuation lines width. */
2818 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2819 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2820 if (!start_at_line_beg_p)
2821 {
2822 int new_x;
2823
2824 reseat_at_previous_visible_line_start (it);
2825 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2826
2827 new_x = it->current_x + it->pixel_width;
2828
2829 /* If lines are continued, this line may end in the middle
2830 of a multi-glyph character (e.g. a control character
2831 displayed as \003, or in the middle of an overlay
2832 string). In this case move_it_to above will not have
2833 taken us to the start of the continuation line but to the
2834 end of the continued line. */
2835 if (it->current_x > 0
2836 && it->line_wrap != TRUNCATE /* Lines are continued. */
2837 && (/* And glyph doesn't fit on the line. */
2838 new_x > it->last_visible_x
2839 /* Or it fits exactly and we're on a window
2840 system frame. */
2841 || (new_x == it->last_visible_x
2842 && FRAME_WINDOW_P (it->f))))
2843 {
2844 if (it->current.dpvec_index >= 0
2845 || it->current.overlay_string_index >= 0)
2846 {
2847 set_iterator_to_next (it, 1);
2848 move_it_in_display_line_to (it, -1, -1, 0);
2849 }
2850
2851 it->continuation_lines_width += it->current_x;
2852 }
2853
2854 /* We're starting a new display line, not affected by the
2855 height of the continued line, so clear the appropriate
2856 fields in the iterator structure. */
2857 it->max_ascent = it->max_descent = 0;
2858 it->max_phys_ascent = it->max_phys_descent = 0;
2859
2860 it->current_y = first_y;
2861 it->vpos = 0;
2862 it->current_x = it->hpos = 0;
2863 }
2864 }
2865
2866 #if 0 /* Don't assert the following because start_display is sometimes
2867 called intentionally with a window start that is not at a
2868 line start. Please leave this code in as a comment. */
2869
2870 /* Window start should be on a line start, now. */
2871 xassert (it->continuation_lines_width
2872 || IT_CHARPOS (it) == BEGV
2873 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2874 #endif /* 0 */
2875 }
2876
2877
2878 /* Return 1 if POS is a position in ellipses displayed for invisible
2879 text. W is the window we display, for text property lookup. */
2880
2881 static int
2882 in_ellipses_for_invisible_text_p (pos, w)
2883 struct display_pos *pos;
2884 struct window *w;
2885 {
2886 Lisp_Object prop, window;
2887 int ellipses_p = 0;
2888 int charpos = CHARPOS (pos->pos);
2889
2890 /* If POS specifies a position in a display vector, this might
2891 be for an ellipsis displayed for invisible text. We won't
2892 get the iterator set up for delivering that ellipsis unless
2893 we make sure that it gets aware of the invisible text. */
2894 if (pos->dpvec_index >= 0
2895 && pos->overlay_string_index < 0
2896 && CHARPOS (pos->string_pos) < 0
2897 && charpos > BEGV
2898 && (XSETWINDOW (window, w),
2899 prop = Fget_char_property (make_number (charpos),
2900 Qinvisible, window),
2901 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2902 {
2903 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2904 window);
2905 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2906 }
2907
2908 return ellipses_p;
2909 }
2910
2911
2912 /* Initialize IT for stepping through current_buffer in window W,
2913 starting at position POS that includes overlay string and display
2914 vector/ control character translation position information. Value
2915 is zero if there are overlay strings with newlines at POS. */
2916
2917 static int
2918 init_from_display_pos (it, w, pos)
2919 struct it *it;
2920 struct window *w;
2921 struct display_pos *pos;
2922 {
2923 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2924 int i, overlay_strings_with_newlines = 0;
2925
2926 /* If POS specifies a position in a display vector, this might
2927 be for an ellipsis displayed for invisible text. We won't
2928 get the iterator set up for delivering that ellipsis unless
2929 we make sure that it gets aware of the invisible text. */
2930 if (in_ellipses_for_invisible_text_p (pos, w))
2931 {
2932 --charpos;
2933 bytepos = 0;
2934 }
2935
2936 /* Keep in mind: the call to reseat in init_iterator skips invisible
2937 text, so we might end up at a position different from POS. This
2938 is only a problem when POS is a row start after a newline and an
2939 overlay starts there with an after-string, and the overlay has an
2940 invisible property. Since we don't skip invisible text in
2941 display_line and elsewhere immediately after consuming the
2942 newline before the row start, such a POS will not be in a string,
2943 but the call to init_iterator below will move us to the
2944 after-string. */
2945 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2946
2947 /* This only scans the current chunk -- it should scan all chunks.
2948 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2949 to 16 in 22.1 to make this a lesser problem. */
2950 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2951 {
2952 const char *s = SDATA (it->overlay_strings[i]);
2953 const char *e = s + SBYTES (it->overlay_strings[i]);
2954
2955 while (s < e && *s != '\n')
2956 ++s;
2957
2958 if (s < e)
2959 {
2960 overlay_strings_with_newlines = 1;
2961 break;
2962 }
2963 }
2964
2965 /* If position is within an overlay string, set up IT to the right
2966 overlay string. */
2967 if (pos->overlay_string_index >= 0)
2968 {
2969 int relative_index;
2970
2971 /* If the first overlay string happens to have a `display'
2972 property for an image, the iterator will be set up for that
2973 image, and we have to undo that setup first before we can
2974 correct the overlay string index. */
2975 if (it->method == GET_FROM_IMAGE)
2976 pop_it (it);
2977
2978 /* We already have the first chunk of overlay strings in
2979 IT->overlay_strings. Load more until the one for
2980 pos->overlay_string_index is in IT->overlay_strings. */
2981 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2982 {
2983 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2984 it->current.overlay_string_index = 0;
2985 while (n--)
2986 {
2987 load_overlay_strings (it, 0);
2988 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2989 }
2990 }
2991
2992 it->current.overlay_string_index = pos->overlay_string_index;
2993 relative_index = (it->current.overlay_string_index
2994 % OVERLAY_STRING_CHUNK_SIZE);
2995 it->string = it->overlay_strings[relative_index];
2996 xassert (STRINGP (it->string));
2997 it->current.string_pos = pos->string_pos;
2998 it->method = GET_FROM_STRING;
2999 }
3000
3001 #if 0 /* This is bogus because POS not having an overlay string
3002 position does not mean it's after the string. Example: A
3003 line starting with a before-string and initialization of IT
3004 to the previous row's end position. */
3005 else if (it->current.overlay_string_index >= 0)
3006 {
3007 /* If POS says we're already after an overlay string ending at
3008 POS, make sure to pop the iterator because it will be in
3009 front of that overlay string. When POS is ZV, we've thereby
3010 also ``processed'' overlay strings at ZV. */
3011 while (it->sp)
3012 pop_it (it);
3013 xassert (it->current.overlay_string_index == -1);
3014 xassert (it->method == GET_FROM_BUFFER);
3015 if (CHARPOS (pos->pos) == ZV)
3016 it->overlay_strings_at_end_processed_p = 1;
3017 }
3018 #endif /* 0 */
3019
3020 if (CHARPOS (pos->string_pos) >= 0)
3021 {
3022 /* Recorded position is not in an overlay string, but in another
3023 string. This can only be a string from a `display' property.
3024 IT should already be filled with that string. */
3025 it->current.string_pos = pos->string_pos;
3026 xassert (STRINGP (it->string));
3027 }
3028
3029 /* Restore position in display vector translations, control
3030 character translations or ellipses. */
3031 if (pos->dpvec_index >= 0)
3032 {
3033 if (it->dpvec == NULL)
3034 get_next_display_element (it);
3035 xassert (it->dpvec && it->current.dpvec_index == 0);
3036 it->current.dpvec_index = pos->dpvec_index;
3037 }
3038
3039 CHECK_IT (it);
3040 return !overlay_strings_with_newlines;
3041 }
3042
3043
3044 /* Initialize IT for stepping through current_buffer in window W
3045 starting at ROW->start. */
3046
3047 static void
3048 init_to_row_start (it, w, row)
3049 struct it *it;
3050 struct window *w;
3051 struct glyph_row *row;
3052 {
3053 init_from_display_pos (it, w, &row->start);
3054 it->start = row->start;
3055 it->continuation_lines_width = row->continuation_lines_width;
3056 CHECK_IT (it);
3057 }
3058
3059
3060 /* Initialize IT for stepping through current_buffer in window W
3061 starting in the line following ROW, i.e. starting at ROW->end.
3062 Value is zero if there are overlay strings with newlines at ROW's
3063 end position. */
3064
3065 static int
3066 init_to_row_end (it, w, row)
3067 struct it *it;
3068 struct window *w;
3069 struct glyph_row *row;
3070 {
3071 int success = 0;
3072
3073 if (init_from_display_pos (it, w, &row->end))
3074 {
3075 if (row->continued_p)
3076 it->continuation_lines_width
3077 = row->continuation_lines_width + row->pixel_width;
3078 CHECK_IT (it);
3079 success = 1;
3080 }
3081
3082 return success;
3083 }
3084
3085
3086
3087 \f
3088 /***********************************************************************
3089 Text properties
3090 ***********************************************************************/
3091
3092 /* Called when IT reaches IT->stop_charpos. Handle text property and
3093 overlay changes. Set IT->stop_charpos to the next position where
3094 to stop. */
3095
3096 static void
3097 handle_stop (it)
3098 struct it *it;
3099 {
3100 enum prop_handled handled;
3101 int handle_overlay_change_p;
3102 struct props *p;
3103
3104 it->dpvec = NULL;
3105 it->current.dpvec_index = -1;
3106 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3107 it->ignore_overlay_strings_at_pos_p = 0;
3108
3109 /* Use face of preceding text for ellipsis (if invisible) */
3110 if (it->selective_display_ellipsis_p)
3111 it->saved_face_id = it->face_id;
3112
3113 do
3114 {
3115 handled = HANDLED_NORMALLY;
3116
3117 /* Call text property handlers. */
3118 for (p = it_props; p->handler; ++p)
3119 {
3120 handled = p->handler (it);
3121
3122 if (handled == HANDLED_RECOMPUTE_PROPS)
3123 break;
3124 else if (handled == HANDLED_RETURN)
3125 {
3126 /* We still want to show before and after strings from
3127 overlays even if the actual buffer text is replaced. */
3128 if (!handle_overlay_change_p || it->sp > 1)
3129 return;
3130 if (!get_overlay_strings_1 (it, 0, 0))
3131 return;
3132 it->ignore_overlay_strings_at_pos_p = 1;
3133 it->string_from_display_prop_p = 0;
3134 handle_overlay_change_p = 0;
3135 handled = HANDLED_RECOMPUTE_PROPS;
3136 break;
3137 }
3138 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3139 handle_overlay_change_p = 0;
3140 }
3141
3142 if (handled != HANDLED_RECOMPUTE_PROPS)
3143 {
3144 /* Don't check for overlay strings below when set to deliver
3145 characters from a display vector. */
3146 if (it->method == GET_FROM_DISPLAY_VECTOR)
3147 handle_overlay_change_p = 0;
3148
3149 /* Handle overlay changes.
3150 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3151 if it finds overlays. */
3152 if (handle_overlay_change_p)
3153 handled = handle_overlay_change (it);
3154 }
3155 }
3156 while (handled == HANDLED_RECOMPUTE_PROPS);
3157
3158 /* Determine where to stop next. */
3159 if (handled == HANDLED_NORMALLY)
3160 compute_stop_pos (it);
3161 }
3162
3163
3164 /* Compute IT->stop_charpos from text property and overlay change
3165 information for IT's current position. */
3166
3167 static void
3168 compute_stop_pos (it)
3169 struct it *it;
3170 {
3171 register INTERVAL iv, next_iv;
3172 Lisp_Object object, limit, position;
3173
3174 /* If nowhere else, stop at the end. */
3175 it->stop_charpos = it->end_charpos;
3176
3177 if (STRINGP (it->string))
3178 {
3179 /* Strings are usually short, so don't limit the search for
3180 properties. */
3181 object = it->string;
3182 limit = Qnil;
3183 position = make_number (IT_STRING_CHARPOS (*it));
3184 }
3185 else
3186 {
3187 int charpos;
3188
3189 /* If next overlay change is in front of the current stop pos
3190 (which is IT->end_charpos), stop there. Note: value of
3191 next_overlay_change is point-max if no overlay change
3192 follows. */
3193 charpos = next_overlay_change (IT_CHARPOS (*it));
3194 if (charpos < it->stop_charpos)
3195 it->stop_charpos = charpos;
3196
3197 /* If showing the region, we have to stop at the region
3198 start or end because the face might change there. */
3199 if (it->region_beg_charpos > 0)
3200 {
3201 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3202 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3203 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3204 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3205 }
3206
3207 /* Set up variables for computing the stop position from text
3208 property changes. */
3209 XSETBUFFER (object, current_buffer);
3210 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3211 position = make_number (IT_CHARPOS (*it));
3212
3213 }
3214
3215 /* Get the interval containing IT's position. Value is a null
3216 interval if there isn't such an interval. */
3217 iv = validate_interval_range (object, &position, &position, 0);
3218 if (!NULL_INTERVAL_P (iv))
3219 {
3220 Lisp_Object values_here[LAST_PROP_IDX];
3221 struct props *p;
3222
3223 /* Get properties here. */
3224 for (p = it_props; p->handler; ++p)
3225 values_here[p->idx] = textget (iv->plist, *p->name);
3226
3227 /* Look for an interval following iv that has different
3228 properties. */
3229 for (next_iv = next_interval (iv);
3230 (!NULL_INTERVAL_P (next_iv)
3231 && (NILP (limit)
3232 || XFASTINT (limit) > next_iv->position));
3233 next_iv = next_interval (next_iv))
3234 {
3235 for (p = it_props; p->handler; ++p)
3236 {
3237 Lisp_Object new_value;
3238
3239 new_value = textget (next_iv->plist, *p->name);
3240 if (!EQ (values_here[p->idx], new_value))
3241 break;
3242 }
3243
3244 if (p->handler)
3245 break;
3246 }
3247
3248 if (!NULL_INTERVAL_P (next_iv))
3249 {
3250 if (INTEGERP (limit)
3251 && next_iv->position >= XFASTINT (limit))
3252 /* No text property change up to limit. */
3253 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3254 else
3255 /* Text properties change in next_iv. */
3256 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3257 }
3258 }
3259
3260 xassert (STRINGP (it->string)
3261 || (it->stop_charpos >= BEGV
3262 && it->stop_charpos >= IT_CHARPOS (*it)));
3263 }
3264
3265
3266 /* Return the position of the next overlay change after POS in
3267 current_buffer. Value is point-max if no overlay change
3268 follows. This is like `next-overlay-change' but doesn't use
3269 xmalloc. */
3270
3271 static EMACS_INT
3272 next_overlay_change (pos)
3273 EMACS_INT pos;
3274 {
3275 int noverlays;
3276 EMACS_INT endpos;
3277 Lisp_Object *overlays;
3278 int i;
3279
3280 /* Get all overlays at the given position. */
3281 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3282
3283 /* If any of these overlays ends before endpos,
3284 use its ending point instead. */
3285 for (i = 0; i < noverlays; ++i)
3286 {
3287 Lisp_Object oend;
3288 EMACS_INT oendpos;
3289
3290 oend = OVERLAY_END (overlays[i]);
3291 oendpos = OVERLAY_POSITION (oend);
3292 endpos = min (endpos, oendpos);
3293 }
3294
3295 return endpos;
3296 }
3297
3298
3299 \f
3300 /***********************************************************************
3301 Fontification
3302 ***********************************************************************/
3303
3304 /* Handle changes in the `fontified' property of the current buffer by
3305 calling hook functions from Qfontification_functions to fontify
3306 regions of text. */
3307
3308 static enum prop_handled
3309 handle_fontified_prop (it)
3310 struct it *it;
3311 {
3312 Lisp_Object prop, pos;
3313 enum prop_handled handled = HANDLED_NORMALLY;
3314
3315 if (!NILP (Vmemory_full))
3316 return handled;
3317
3318 /* Get the value of the `fontified' property at IT's current buffer
3319 position. (The `fontified' property doesn't have a special
3320 meaning in strings.) If the value is nil, call functions from
3321 Qfontification_functions. */
3322 if (!STRINGP (it->string)
3323 && it->s == NULL
3324 && !NILP (Vfontification_functions)
3325 && !NILP (Vrun_hooks)
3326 && (pos = make_number (IT_CHARPOS (*it)),
3327 prop = Fget_char_property (pos, Qfontified, Qnil),
3328 /* Ignore the special cased nil value always present at EOB since
3329 no amount of fontifying will be able to change it. */
3330 NILP (prop) && IT_CHARPOS (*it) < Z))
3331 {
3332 int count = SPECPDL_INDEX ();
3333 Lisp_Object val;
3334
3335 val = Vfontification_functions;
3336 specbind (Qfontification_functions, Qnil);
3337
3338 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3339 safe_call1 (val, pos);
3340 else
3341 {
3342 Lisp_Object globals, fn;
3343 struct gcpro gcpro1, gcpro2;
3344
3345 globals = Qnil;
3346 GCPRO2 (val, globals);
3347
3348 for (; CONSP (val); val = XCDR (val))
3349 {
3350 fn = XCAR (val);
3351
3352 if (EQ (fn, Qt))
3353 {
3354 /* A value of t indicates this hook has a local
3355 binding; it means to run the global binding too.
3356 In a global value, t should not occur. If it
3357 does, we must ignore it to avoid an endless
3358 loop. */
3359 for (globals = Fdefault_value (Qfontification_functions);
3360 CONSP (globals);
3361 globals = XCDR (globals))
3362 {
3363 fn = XCAR (globals);
3364 if (!EQ (fn, Qt))
3365 safe_call1 (fn, pos);
3366 }
3367 }
3368 else
3369 safe_call1 (fn, pos);
3370 }
3371
3372 UNGCPRO;
3373 }
3374
3375 unbind_to (count, Qnil);
3376
3377 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3378 something. This avoids an endless loop if they failed to
3379 fontify the text for which reason ever. */
3380 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3381 handled = HANDLED_RECOMPUTE_PROPS;
3382 }
3383
3384 return handled;
3385 }
3386
3387
3388 \f
3389 /***********************************************************************
3390 Faces
3391 ***********************************************************************/
3392
3393 /* Set up iterator IT from face properties at its current position.
3394 Called from handle_stop. */
3395
3396 static enum prop_handled
3397 handle_face_prop (it)
3398 struct it *it;
3399 {
3400 int new_face_id;
3401 EMACS_INT next_stop;
3402
3403 if (!STRINGP (it->string))
3404 {
3405 new_face_id
3406 = face_at_buffer_position (it->w,
3407 IT_CHARPOS (*it),
3408 it->region_beg_charpos,
3409 it->region_end_charpos,
3410 &next_stop,
3411 (IT_CHARPOS (*it)
3412 + TEXT_PROP_DISTANCE_LIMIT),
3413 0);
3414
3415 /* Is this a start of a run of characters with box face?
3416 Caveat: this can be called for a freshly initialized
3417 iterator; face_id is -1 in this case. We know that the new
3418 face will not change until limit, i.e. if the new face has a
3419 box, all characters up to limit will have one. But, as
3420 usual, we don't know whether limit is really the end. */
3421 if (new_face_id != it->face_id)
3422 {
3423 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3424
3425 /* If new face has a box but old face has not, this is
3426 the start of a run of characters with box, i.e. it has
3427 a shadow on the left side. The value of face_id of the
3428 iterator will be -1 if this is the initial call that gets
3429 the face. In this case, we have to look in front of IT's
3430 position and see whether there is a face != new_face_id. */
3431 it->start_of_box_run_p
3432 = (new_face->box != FACE_NO_BOX
3433 && (it->face_id >= 0
3434 || IT_CHARPOS (*it) == BEG
3435 || new_face_id != face_before_it_pos (it)));
3436 it->face_box_p = new_face->box != FACE_NO_BOX;
3437 }
3438 }
3439 else
3440 {
3441 int base_face_id, bufpos;
3442 int i;
3443 Lisp_Object from_overlay
3444 = (it->current.overlay_string_index >= 0
3445 ? it->string_overlays[it->current.overlay_string_index]
3446 : Qnil);
3447
3448 /* See if we got to this string directly or indirectly from
3449 an overlay property. That includes the before-string or
3450 after-string of an overlay, strings in display properties
3451 provided by an overlay, their text properties, etc.
3452
3453 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3454 if (! NILP (from_overlay))
3455 for (i = it->sp - 1; i >= 0; i--)
3456 {
3457 if (it->stack[i].current.overlay_string_index >= 0)
3458 from_overlay
3459 = it->string_overlays[it->stack[i].current.overlay_string_index];
3460 else if (! NILP (it->stack[i].from_overlay))
3461 from_overlay = it->stack[i].from_overlay;
3462
3463 if (!NILP (from_overlay))
3464 break;
3465 }
3466
3467 if (! NILP (from_overlay))
3468 {
3469 bufpos = IT_CHARPOS (*it);
3470 /* For a string from an overlay, the base face depends
3471 only on text properties and ignores overlays. */
3472 base_face_id
3473 = face_for_overlay_string (it->w,
3474 IT_CHARPOS (*it),
3475 it->region_beg_charpos,
3476 it->region_end_charpos,
3477 &next_stop,
3478 (IT_CHARPOS (*it)
3479 + TEXT_PROP_DISTANCE_LIMIT),
3480 0,
3481 from_overlay);
3482 }
3483 else
3484 {
3485 bufpos = 0;
3486
3487 /* For strings from a `display' property, use the face at
3488 IT's current buffer position as the base face to merge
3489 with, so that overlay strings appear in the same face as
3490 surrounding text, unless they specify their own
3491 faces. */
3492 base_face_id = underlying_face_id (it);
3493 }
3494
3495 new_face_id = face_at_string_position (it->w,
3496 it->string,
3497 IT_STRING_CHARPOS (*it),
3498 bufpos,
3499 it->region_beg_charpos,
3500 it->region_end_charpos,
3501 &next_stop,
3502 base_face_id, 0);
3503
3504 #if 0 /* This shouldn't be neccessary. Let's check it. */
3505 /* If IT is used to display a mode line we would really like to
3506 use the mode line face instead of the frame's default face. */
3507 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3508 && new_face_id == DEFAULT_FACE_ID)
3509 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3510 #endif
3511
3512 /* Is this a start of a run of characters with box? Caveat:
3513 this can be called for a freshly allocated iterator; face_id
3514 is -1 is this case. We know that the new face will not
3515 change until the next check pos, i.e. if the new face has a
3516 box, all characters up to that position will have a
3517 box. But, as usual, we don't know whether that position
3518 is really the end. */
3519 if (new_face_id != it->face_id)
3520 {
3521 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3522 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3523
3524 /* If new face has a box but old face hasn't, this is the
3525 start of a run of characters with box, i.e. it has a
3526 shadow on the left side. */
3527 it->start_of_box_run_p
3528 = new_face->box && (old_face == NULL || !old_face->box);
3529 it->face_box_p = new_face->box != FACE_NO_BOX;
3530 }
3531 }
3532
3533 it->face_id = new_face_id;
3534 return HANDLED_NORMALLY;
3535 }
3536
3537
3538 /* Return the ID of the face ``underlying'' IT's current position,
3539 which is in a string. If the iterator is associated with a
3540 buffer, return the face at IT's current buffer position.
3541 Otherwise, use the iterator's base_face_id. */
3542
3543 static int
3544 underlying_face_id (it)
3545 struct it *it;
3546 {
3547 int face_id = it->base_face_id, i;
3548
3549 xassert (STRINGP (it->string));
3550
3551 for (i = it->sp - 1; i >= 0; --i)
3552 if (NILP (it->stack[i].string))
3553 face_id = it->stack[i].face_id;
3554
3555 return face_id;
3556 }
3557
3558
3559 /* Compute the face one character before or after the current position
3560 of IT. BEFORE_P non-zero means get the face in front of IT's
3561 position. Value is the id of the face. */
3562
3563 static int
3564 face_before_or_after_it_pos (it, before_p)
3565 struct it *it;
3566 int before_p;
3567 {
3568 int face_id, limit;
3569 EMACS_INT next_check_charpos;
3570 struct text_pos pos;
3571
3572 xassert (it->s == NULL);
3573
3574 if (STRINGP (it->string))
3575 {
3576 int bufpos, base_face_id;
3577
3578 /* No face change past the end of the string (for the case
3579 we are padding with spaces). No face change before the
3580 string start. */
3581 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3582 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3583 return it->face_id;
3584
3585 /* Set pos to the position before or after IT's current position. */
3586 if (before_p)
3587 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3588 else
3589 /* For composition, we must check the character after the
3590 composition. */
3591 pos = (it->what == IT_COMPOSITION
3592 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3593 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3594
3595 if (it->current.overlay_string_index >= 0)
3596 bufpos = IT_CHARPOS (*it);
3597 else
3598 bufpos = 0;
3599
3600 base_face_id = underlying_face_id (it);
3601
3602 /* Get the face for ASCII, or unibyte. */
3603 face_id = face_at_string_position (it->w,
3604 it->string,
3605 CHARPOS (pos),
3606 bufpos,
3607 it->region_beg_charpos,
3608 it->region_end_charpos,
3609 &next_check_charpos,
3610 base_face_id, 0);
3611
3612 /* Correct the face for charsets different from ASCII. Do it
3613 for the multibyte case only. The face returned above is
3614 suitable for unibyte text if IT->string is unibyte. */
3615 if (STRING_MULTIBYTE (it->string))
3616 {
3617 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3618 int rest = SBYTES (it->string) - BYTEPOS (pos);
3619 int c, len;
3620 struct face *face = FACE_FROM_ID (it->f, face_id);
3621
3622 c = string_char_and_length (p, rest, &len);
3623 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3624 }
3625 }
3626 else
3627 {
3628 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3629 || (IT_CHARPOS (*it) <= BEGV && before_p))
3630 return it->face_id;
3631
3632 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3633 pos = it->current.pos;
3634
3635 if (before_p)
3636 DEC_TEXT_POS (pos, it->multibyte_p);
3637 else
3638 {
3639 if (it->what == IT_COMPOSITION)
3640 /* For composition, we must check the position after the
3641 composition. */
3642 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3643 else
3644 INC_TEXT_POS (pos, it->multibyte_p);
3645 }
3646
3647 /* Determine face for CHARSET_ASCII, or unibyte. */
3648 face_id = face_at_buffer_position (it->w,
3649 CHARPOS (pos),
3650 it->region_beg_charpos,
3651 it->region_end_charpos,
3652 &next_check_charpos,
3653 limit, 0);
3654
3655 /* Correct the face for charsets different from ASCII. Do it
3656 for the multibyte case only. The face returned above is
3657 suitable for unibyte text if current_buffer is unibyte. */
3658 if (it->multibyte_p)
3659 {
3660 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3661 struct face *face = FACE_FROM_ID (it->f, face_id);
3662 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3663 }
3664 }
3665
3666 return face_id;
3667 }
3668
3669
3670 \f
3671 /***********************************************************************
3672 Invisible text
3673 ***********************************************************************/
3674
3675 /* Set up iterator IT from invisible properties at its current
3676 position. Called from handle_stop. */
3677
3678 static enum prop_handled
3679 handle_invisible_prop (it)
3680 struct it *it;
3681 {
3682 enum prop_handled handled = HANDLED_NORMALLY;
3683
3684 if (STRINGP (it->string))
3685 {
3686 extern Lisp_Object Qinvisible;
3687 Lisp_Object prop, end_charpos, limit, charpos;
3688
3689 /* Get the value of the invisible text property at the
3690 current position. Value will be nil if there is no such
3691 property. */
3692 charpos = make_number (IT_STRING_CHARPOS (*it));
3693 prop = Fget_text_property (charpos, Qinvisible, it->string);
3694
3695 if (!NILP (prop)
3696 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3697 {
3698 handled = HANDLED_RECOMPUTE_PROPS;
3699
3700 /* Get the position at which the next change of the
3701 invisible text property can be found in IT->string.
3702 Value will be nil if the property value is the same for
3703 all the rest of IT->string. */
3704 XSETINT (limit, SCHARS (it->string));
3705 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3706 it->string, limit);
3707
3708 /* Text at current position is invisible. The next
3709 change in the property is at position end_charpos.
3710 Move IT's current position to that position. */
3711 if (INTEGERP (end_charpos)
3712 && XFASTINT (end_charpos) < XFASTINT (limit))
3713 {
3714 struct text_pos old;
3715 old = it->current.string_pos;
3716 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3717 compute_string_pos (&it->current.string_pos, old, it->string);
3718 }
3719 else
3720 {
3721 /* The rest of the string is invisible. If this is an
3722 overlay string, proceed with the next overlay string
3723 or whatever comes and return a character from there. */
3724 if (it->current.overlay_string_index >= 0)
3725 {
3726 next_overlay_string (it);
3727 /* Don't check for overlay strings when we just
3728 finished processing them. */
3729 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3730 }
3731 else
3732 {
3733 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3734 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3735 }
3736 }
3737 }
3738 }
3739 else
3740 {
3741 int invis_p;
3742 EMACS_INT newpos, next_stop, start_charpos;
3743 Lisp_Object pos, prop, overlay;
3744
3745 /* First of all, is there invisible text at this position? */
3746 start_charpos = IT_CHARPOS (*it);
3747 pos = make_number (IT_CHARPOS (*it));
3748 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3749 &overlay);
3750 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3751
3752 /* If we are on invisible text, skip over it. */
3753 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3754 {
3755 /* Record whether we have to display an ellipsis for the
3756 invisible text. */
3757 int display_ellipsis_p = invis_p == 2;
3758
3759 handled = HANDLED_RECOMPUTE_PROPS;
3760
3761 /* Loop skipping over invisible text. The loop is left at
3762 ZV or with IT on the first char being visible again. */
3763 do
3764 {
3765 /* Try to skip some invisible text. Return value is the
3766 position reached which can be equal to IT's position
3767 if there is nothing invisible here. This skips both
3768 over invisible text properties and overlays with
3769 invisible property. */
3770 newpos = skip_invisible (IT_CHARPOS (*it),
3771 &next_stop, ZV, it->window);
3772
3773 /* If we skipped nothing at all we weren't at invisible
3774 text in the first place. If everything to the end of
3775 the buffer was skipped, end the loop. */
3776 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3777 invis_p = 0;
3778 else
3779 {
3780 /* We skipped some characters but not necessarily
3781 all there are. Check if we ended up on visible
3782 text. Fget_char_property returns the property of
3783 the char before the given position, i.e. if we
3784 get invis_p = 0, this means that the char at
3785 newpos is visible. */
3786 pos = make_number (newpos);
3787 prop = Fget_char_property (pos, Qinvisible, it->window);
3788 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3789 }
3790
3791 /* If we ended up on invisible text, proceed to
3792 skip starting with next_stop. */
3793 if (invis_p)
3794 IT_CHARPOS (*it) = next_stop;
3795
3796 /* If there are adjacent invisible texts, don't lose the
3797 second one's ellipsis. */
3798 if (invis_p == 2)
3799 display_ellipsis_p = 1;
3800 }
3801 while (invis_p);
3802
3803 /* The position newpos is now either ZV or on visible text. */
3804 IT_CHARPOS (*it) = newpos;
3805 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3806
3807 /* If there are before-strings at the start of invisible
3808 text, and the text is invisible because of a text
3809 property, arrange to show before-strings because 20.x did
3810 it that way. (If the text is invisible because of an
3811 overlay property instead of a text property, this is
3812 already handled in the overlay code.) */
3813 if (NILP (overlay)
3814 && get_overlay_strings (it, start_charpos))
3815 {
3816 handled = HANDLED_RECOMPUTE_PROPS;
3817 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3818 }
3819 else if (display_ellipsis_p)
3820 {
3821 /* Make sure that the glyphs of the ellipsis will get
3822 correct `charpos' values. If we would not update
3823 it->position here, the glyphs would belong to the
3824 last visible character _before_ the invisible
3825 text, which confuses `set_cursor_from_row'.
3826
3827 We use the last invisible position instead of the
3828 first because this way the cursor is always drawn on
3829 the first "." of the ellipsis, whenever PT is inside
3830 the invisible text. Otherwise the cursor would be
3831 placed _after_ the ellipsis when the point is after the
3832 first invisible character. */
3833 if (!STRINGP (it->object))
3834 {
3835 it->position.charpos = IT_CHARPOS (*it) - 1;
3836 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3837 }
3838 setup_for_ellipsis (it, 0);
3839 /* Let the ellipsis display before
3840 considering any properties of the following char.
3841 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3842 handled = HANDLED_RETURN;
3843 }
3844 }
3845 }
3846
3847 return handled;
3848 }
3849
3850
3851 /* Make iterator IT return `...' next.
3852 Replaces LEN characters from buffer. */
3853
3854 static void
3855 setup_for_ellipsis (it, len)
3856 struct it *it;
3857 int len;
3858 {
3859 /* Use the display table definition for `...'. Invalid glyphs
3860 will be handled by the method returning elements from dpvec. */
3861 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3862 {
3863 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3864 it->dpvec = v->contents;
3865 it->dpend = v->contents + v->size;
3866 }
3867 else
3868 {
3869 /* Default `...'. */
3870 it->dpvec = default_invis_vector;
3871 it->dpend = default_invis_vector + 3;
3872 }
3873
3874 it->dpvec_char_len = len;
3875 it->current.dpvec_index = 0;
3876 it->dpvec_face_id = -1;
3877
3878 /* Remember the current face id in case glyphs specify faces.
3879 IT's face is restored in set_iterator_to_next.
3880 saved_face_id was set to preceding char's face in handle_stop. */
3881 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3882 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3883
3884 it->method = GET_FROM_DISPLAY_VECTOR;
3885 it->ellipsis_p = 1;
3886 }
3887
3888
3889 \f
3890 /***********************************************************************
3891 'display' property
3892 ***********************************************************************/
3893
3894 /* Set up iterator IT from `display' property at its current position.
3895 Called from handle_stop.
3896 We return HANDLED_RETURN if some part of the display property
3897 overrides the display of the buffer text itself.
3898 Otherwise we return HANDLED_NORMALLY. */
3899
3900 static enum prop_handled
3901 handle_display_prop (it)
3902 struct it *it;
3903 {
3904 Lisp_Object prop, object, overlay;
3905 struct text_pos *position;
3906 /* Nonzero if some property replaces the display of the text itself. */
3907 int display_replaced_p = 0;
3908
3909 if (STRINGP (it->string))
3910 {
3911 object = it->string;
3912 position = &it->current.string_pos;
3913 }
3914 else
3915 {
3916 XSETWINDOW (object, it->w);
3917 position = &it->current.pos;
3918 }
3919
3920 /* Reset those iterator values set from display property values. */
3921 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3922 it->space_width = Qnil;
3923 it->font_height = Qnil;
3924 it->voffset = 0;
3925
3926 /* We don't support recursive `display' properties, i.e. string
3927 values that have a string `display' property, that have a string
3928 `display' property etc. */
3929 if (!it->string_from_display_prop_p)
3930 it->area = TEXT_AREA;
3931
3932 prop = get_char_property_and_overlay (make_number (position->charpos),
3933 Qdisplay, object, &overlay);
3934 if (NILP (prop))
3935 return HANDLED_NORMALLY;
3936 /* Now OVERLAY is the overlay that gave us this property, or nil
3937 if it was a text property. */
3938
3939 if (!STRINGP (it->string))
3940 object = it->w->buffer;
3941
3942 if (CONSP (prop)
3943 /* Simple properties. */
3944 && !EQ (XCAR (prop), Qimage)
3945 && !EQ (XCAR (prop), Qspace)
3946 && !EQ (XCAR (prop), Qwhen)
3947 && !EQ (XCAR (prop), Qslice)
3948 && !EQ (XCAR (prop), Qspace_width)
3949 && !EQ (XCAR (prop), Qheight)
3950 && !EQ (XCAR (prop), Qraise)
3951 /* Marginal area specifications. */
3952 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3953 && !EQ (XCAR (prop), Qleft_fringe)
3954 && !EQ (XCAR (prop), Qright_fringe)
3955 && !NILP (XCAR (prop)))
3956 {
3957 for (; CONSP (prop); prop = XCDR (prop))
3958 {
3959 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3960 position, display_replaced_p))
3961 {
3962 display_replaced_p = 1;
3963 /* If some text in a string is replaced, `position' no
3964 longer points to the position of `object'. */
3965 if (STRINGP (object))
3966 break;
3967 }
3968 }
3969 }
3970 else if (VECTORP (prop))
3971 {
3972 int i;
3973 for (i = 0; i < ASIZE (prop); ++i)
3974 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3975 position, display_replaced_p))
3976 {
3977 display_replaced_p = 1;
3978 /* If some text in a string is replaced, `position' no
3979 longer points to the position of `object'. */
3980 if (STRINGP (object))
3981 break;
3982 }
3983 }
3984 else
3985 {
3986 int ret = handle_single_display_spec (it, prop, object, overlay,
3987 position, 0);
3988 if (ret < 0) /* Replaced by "", i.e. nothing. */
3989 return HANDLED_RECOMPUTE_PROPS;
3990 if (ret)
3991 display_replaced_p = 1;
3992 }
3993
3994 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3995 }
3996
3997
3998 /* Value is the position of the end of the `display' property starting
3999 at START_POS in OBJECT. */
4000
4001 static struct text_pos
4002 display_prop_end (it, object, start_pos)
4003 struct it *it;
4004 Lisp_Object object;
4005 struct text_pos start_pos;
4006 {
4007 Lisp_Object end;
4008 struct text_pos end_pos;
4009
4010 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4011 Qdisplay, object, Qnil);
4012 CHARPOS (end_pos) = XFASTINT (end);
4013 if (STRINGP (object))
4014 compute_string_pos (&end_pos, start_pos, it->string);
4015 else
4016 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4017
4018 return end_pos;
4019 }
4020
4021
4022 /* Set up IT from a single `display' specification PROP. OBJECT
4023 is the object in which the `display' property was found. *POSITION
4024 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4025 means that we previously saw a display specification which already
4026 replaced text display with something else, for example an image;
4027 we ignore such properties after the first one has been processed.
4028
4029 OVERLAY is the overlay this `display' property came from,
4030 or nil if it was a text property.
4031
4032 If PROP is a `space' or `image' specification, and in some other
4033 cases too, set *POSITION to the position where the `display'
4034 property ends.
4035
4036 Value is non-zero if something was found which replaces the display
4037 of buffer or string text. Specifically, the value is -1 if that
4038 "something" is "nothing". */
4039
4040 static int
4041 handle_single_display_spec (it, spec, object, overlay, position,
4042 display_replaced_before_p)
4043 struct it *it;
4044 Lisp_Object spec;
4045 Lisp_Object object;
4046 Lisp_Object overlay;
4047 struct text_pos *position;
4048 int display_replaced_before_p;
4049 {
4050 Lisp_Object form;
4051 Lisp_Object location, value;
4052 struct text_pos start_pos, save_pos;
4053 int valid_p;
4054
4055 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4056 If the result is non-nil, use VALUE instead of SPEC. */
4057 form = Qt;
4058 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4059 {
4060 spec = XCDR (spec);
4061 if (!CONSP (spec))
4062 return 0;
4063 form = XCAR (spec);
4064 spec = XCDR (spec);
4065 }
4066
4067 if (!NILP (form) && !EQ (form, Qt))
4068 {
4069 int count = SPECPDL_INDEX ();
4070 struct gcpro gcpro1;
4071
4072 /* Bind `object' to the object having the `display' property, a
4073 buffer or string. Bind `position' to the position in the
4074 object where the property was found, and `buffer-position'
4075 to the current position in the buffer. */
4076 specbind (Qobject, object);
4077 specbind (Qposition, make_number (CHARPOS (*position)));
4078 specbind (Qbuffer_position,
4079 make_number (STRINGP (object)
4080 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4081 GCPRO1 (form);
4082 form = safe_eval (form);
4083 UNGCPRO;
4084 unbind_to (count, Qnil);
4085 }
4086
4087 if (NILP (form))
4088 return 0;
4089
4090 /* Handle `(height HEIGHT)' specifications. */
4091 if (CONSP (spec)
4092 && EQ (XCAR (spec), Qheight)
4093 && CONSP (XCDR (spec)))
4094 {
4095 if (!FRAME_WINDOW_P (it->f))
4096 return 0;
4097
4098 it->font_height = XCAR (XCDR (spec));
4099 if (!NILP (it->font_height))
4100 {
4101 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4102 int new_height = -1;
4103
4104 if (CONSP (it->font_height)
4105 && (EQ (XCAR (it->font_height), Qplus)
4106 || EQ (XCAR (it->font_height), Qminus))
4107 && CONSP (XCDR (it->font_height))
4108 && INTEGERP (XCAR (XCDR (it->font_height))))
4109 {
4110 /* `(+ N)' or `(- N)' where N is an integer. */
4111 int steps = XINT (XCAR (XCDR (it->font_height)));
4112 if (EQ (XCAR (it->font_height), Qplus))
4113 steps = - steps;
4114 it->face_id = smaller_face (it->f, it->face_id, steps);
4115 }
4116 else if (FUNCTIONP (it->font_height))
4117 {
4118 /* Call function with current height as argument.
4119 Value is the new height. */
4120 Lisp_Object height;
4121 height = safe_call1 (it->font_height,
4122 face->lface[LFACE_HEIGHT_INDEX]);
4123 if (NUMBERP (height))
4124 new_height = XFLOATINT (height);
4125 }
4126 else if (NUMBERP (it->font_height))
4127 {
4128 /* Value is a multiple of the canonical char height. */
4129 struct face *face;
4130
4131 face = FACE_FROM_ID (it->f,
4132 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4133 new_height = (XFLOATINT (it->font_height)
4134 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4135 }
4136 else
4137 {
4138 /* Evaluate IT->font_height with `height' bound to the
4139 current specified height to get the new height. */
4140 int count = SPECPDL_INDEX ();
4141
4142 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4143 value = safe_eval (it->font_height);
4144 unbind_to (count, Qnil);
4145
4146 if (NUMBERP (value))
4147 new_height = XFLOATINT (value);
4148 }
4149
4150 if (new_height > 0)
4151 it->face_id = face_with_height (it->f, it->face_id, new_height);
4152 }
4153
4154 return 0;
4155 }
4156
4157 /* Handle `(space-width WIDTH)'. */
4158 if (CONSP (spec)
4159 && EQ (XCAR (spec), Qspace_width)
4160 && CONSP (XCDR (spec)))
4161 {
4162 if (!FRAME_WINDOW_P (it->f))
4163 return 0;
4164
4165 value = XCAR (XCDR (spec));
4166 if (NUMBERP (value) && XFLOATINT (value) > 0)
4167 it->space_width = value;
4168
4169 return 0;
4170 }
4171
4172 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4173 if (CONSP (spec)
4174 && EQ (XCAR (spec), Qslice))
4175 {
4176 Lisp_Object tem;
4177
4178 if (!FRAME_WINDOW_P (it->f))
4179 return 0;
4180
4181 if (tem = XCDR (spec), CONSP (tem))
4182 {
4183 it->slice.x = XCAR (tem);
4184 if (tem = XCDR (tem), CONSP (tem))
4185 {
4186 it->slice.y = XCAR (tem);
4187 if (tem = XCDR (tem), CONSP (tem))
4188 {
4189 it->slice.width = XCAR (tem);
4190 if (tem = XCDR (tem), CONSP (tem))
4191 it->slice.height = XCAR (tem);
4192 }
4193 }
4194 }
4195
4196 return 0;
4197 }
4198
4199 /* Handle `(raise FACTOR)'. */
4200 if (CONSP (spec)
4201 && EQ (XCAR (spec), Qraise)
4202 && CONSP (XCDR (spec)))
4203 {
4204 if (!FRAME_WINDOW_P (it->f))
4205 return 0;
4206
4207 #ifdef HAVE_WINDOW_SYSTEM
4208 value = XCAR (XCDR (spec));
4209 if (NUMBERP (value))
4210 {
4211 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4212 it->voffset = - (XFLOATINT (value)
4213 * (FONT_HEIGHT (face->font)));
4214 }
4215 #endif /* HAVE_WINDOW_SYSTEM */
4216
4217 return 0;
4218 }
4219
4220 /* Don't handle the other kinds of display specifications
4221 inside a string that we got from a `display' property. */
4222 if (it->string_from_display_prop_p)
4223 return 0;
4224
4225 /* Characters having this form of property are not displayed, so
4226 we have to find the end of the property. */
4227 start_pos = *position;
4228 *position = display_prop_end (it, object, start_pos);
4229 value = Qnil;
4230
4231 /* Stop the scan at that end position--we assume that all
4232 text properties change there. */
4233 it->stop_charpos = position->charpos;
4234
4235 /* Handle `(left-fringe BITMAP [FACE])'
4236 and `(right-fringe BITMAP [FACE])'. */
4237 if (CONSP (spec)
4238 && (EQ (XCAR (spec), Qleft_fringe)
4239 || EQ (XCAR (spec), Qright_fringe))
4240 && CONSP (XCDR (spec)))
4241 {
4242 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4243 int fringe_bitmap;
4244
4245 if (!FRAME_WINDOW_P (it->f))
4246 /* If we return here, POSITION has been advanced
4247 across the text with this property. */
4248 return 0;
4249
4250 #ifdef HAVE_WINDOW_SYSTEM
4251 value = XCAR (XCDR (spec));
4252 if (!SYMBOLP (value)
4253 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4254 /* If we return here, POSITION has been advanced
4255 across the text with this property. */
4256 return 0;
4257
4258 if (CONSP (XCDR (XCDR (spec))))
4259 {
4260 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4261 int face_id2 = lookup_derived_face (it->f, face_name,
4262 FRINGE_FACE_ID, 0);
4263 if (face_id2 >= 0)
4264 face_id = face_id2;
4265 }
4266
4267 /* Save current settings of IT so that we can restore them
4268 when we are finished with the glyph property value. */
4269
4270 save_pos = it->position;
4271 it->position = *position;
4272 push_it (it);
4273 it->position = save_pos;
4274
4275 it->area = TEXT_AREA;
4276 it->what = IT_IMAGE;
4277 it->image_id = -1; /* no image */
4278 it->position = start_pos;
4279 it->object = NILP (object) ? it->w->buffer : object;
4280 it->method = GET_FROM_IMAGE;
4281 it->from_overlay = Qnil;
4282 it->face_id = face_id;
4283
4284 /* Say that we haven't consumed the characters with
4285 `display' property yet. The call to pop_it in
4286 set_iterator_to_next will clean this up. */
4287 *position = start_pos;
4288
4289 if (EQ (XCAR (spec), Qleft_fringe))
4290 {
4291 it->left_user_fringe_bitmap = fringe_bitmap;
4292 it->left_user_fringe_face_id = face_id;
4293 }
4294 else
4295 {
4296 it->right_user_fringe_bitmap = fringe_bitmap;
4297 it->right_user_fringe_face_id = face_id;
4298 }
4299 #endif /* HAVE_WINDOW_SYSTEM */
4300 return 1;
4301 }
4302
4303 /* Prepare to handle `((margin left-margin) ...)',
4304 `((margin right-margin) ...)' and `((margin nil) ...)'
4305 prefixes for display specifications. */
4306 location = Qunbound;
4307 if (CONSP (spec) && CONSP (XCAR (spec)))
4308 {
4309 Lisp_Object tem;
4310
4311 value = XCDR (spec);
4312 if (CONSP (value))
4313 value = XCAR (value);
4314
4315 tem = XCAR (spec);
4316 if (EQ (XCAR (tem), Qmargin)
4317 && (tem = XCDR (tem),
4318 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4319 (NILP (tem)
4320 || EQ (tem, Qleft_margin)
4321 || EQ (tem, Qright_margin))))
4322 location = tem;
4323 }
4324
4325 if (EQ (location, Qunbound))
4326 {
4327 location = Qnil;
4328 value = spec;
4329 }
4330
4331 /* After this point, VALUE is the property after any
4332 margin prefix has been stripped. It must be a string,
4333 an image specification, or `(space ...)'.
4334
4335 LOCATION specifies where to display: `left-margin',
4336 `right-margin' or nil. */
4337
4338 valid_p = (STRINGP (value)
4339 #ifdef HAVE_WINDOW_SYSTEM
4340 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4341 #endif /* not HAVE_WINDOW_SYSTEM */
4342 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4343
4344 if (valid_p && !display_replaced_before_p)
4345 {
4346 /* Save current settings of IT so that we can restore them
4347 when we are finished with the glyph property value. */
4348 save_pos = it->position;
4349 it->position = *position;
4350 push_it (it);
4351 it->position = save_pos;
4352 it->from_overlay = overlay;
4353
4354 if (NILP (location))
4355 it->area = TEXT_AREA;
4356 else if (EQ (location, Qleft_margin))
4357 it->area = LEFT_MARGIN_AREA;
4358 else
4359 it->area = RIGHT_MARGIN_AREA;
4360
4361 if (STRINGP (value))
4362 {
4363 if (SCHARS (value) == 0)
4364 {
4365 pop_it (it);
4366 return -1; /* Replaced by "", i.e. nothing. */
4367 }
4368 it->string = value;
4369 it->multibyte_p = STRING_MULTIBYTE (it->string);
4370 it->current.overlay_string_index = -1;
4371 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4372 it->end_charpos = it->string_nchars = SCHARS (it->string);
4373 it->method = GET_FROM_STRING;
4374 it->stop_charpos = 0;
4375 it->string_from_display_prop_p = 1;
4376 /* Say that we haven't consumed the characters with
4377 `display' property yet. The call to pop_it in
4378 set_iterator_to_next will clean this up. */
4379 if (BUFFERP (object))
4380 *position = start_pos;
4381 }
4382 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4383 {
4384 it->method = GET_FROM_STRETCH;
4385 it->object = value;
4386 *position = it->position = start_pos;
4387 }
4388 #ifdef HAVE_WINDOW_SYSTEM
4389 else
4390 {
4391 it->what = IT_IMAGE;
4392 it->image_id = lookup_image (it->f, value);
4393 it->position = start_pos;
4394 it->object = NILP (object) ? it->w->buffer : object;
4395 it->method = GET_FROM_IMAGE;
4396
4397 /* Say that we haven't consumed the characters with
4398 `display' property yet. The call to pop_it in
4399 set_iterator_to_next will clean this up. */
4400 *position = start_pos;
4401 }
4402 #endif /* HAVE_WINDOW_SYSTEM */
4403
4404 return 1;
4405 }
4406
4407 /* Invalid property or property not supported. Restore
4408 POSITION to what it was before. */
4409 *position = start_pos;
4410 return 0;
4411 }
4412
4413
4414 /* Check if SPEC is a display sub-property value whose text should be
4415 treated as intangible. */
4416
4417 static int
4418 single_display_spec_intangible_p (prop)
4419 Lisp_Object prop;
4420 {
4421 /* Skip over `when FORM'. */
4422 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4423 {
4424 prop = XCDR (prop);
4425 if (!CONSP (prop))
4426 return 0;
4427 prop = XCDR (prop);
4428 }
4429
4430 if (STRINGP (prop))
4431 return 1;
4432
4433 if (!CONSP (prop))
4434 return 0;
4435
4436 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4437 we don't need to treat text as intangible. */
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 || EQ (XCAR (prop), Qleft_margin)
4447 || EQ (XCAR (prop), Qright_margin))
4448 return 0;
4449 }
4450
4451 return (CONSP (prop)
4452 && (EQ (XCAR (prop), Qimage)
4453 || EQ (XCAR (prop), Qspace)));
4454 }
4455
4456
4457 /* Check if PROP is a display property value whose text should be
4458 treated as intangible. */
4459
4460 int
4461 display_prop_intangible_p (prop)
4462 Lisp_Object prop;
4463 {
4464 if (CONSP (prop)
4465 && CONSP (XCAR (prop))
4466 && !EQ (Qmargin, XCAR (XCAR (prop))))
4467 {
4468 /* A list of sub-properties. */
4469 while (CONSP (prop))
4470 {
4471 if (single_display_spec_intangible_p (XCAR (prop)))
4472 return 1;
4473 prop = XCDR (prop);
4474 }
4475 }
4476 else if (VECTORP (prop))
4477 {
4478 /* A vector of sub-properties. */
4479 int i;
4480 for (i = 0; i < ASIZE (prop); ++i)
4481 if (single_display_spec_intangible_p (AREF (prop, i)))
4482 return 1;
4483 }
4484 else
4485 return single_display_spec_intangible_p (prop);
4486
4487 return 0;
4488 }
4489
4490
4491 /* Return 1 if PROP is a display sub-property value containing STRING. */
4492
4493 static int
4494 single_display_spec_string_p (prop, string)
4495 Lisp_Object prop, string;
4496 {
4497 if (EQ (string, prop))
4498 return 1;
4499
4500 /* Skip over `when FORM'. */
4501 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4502 {
4503 prop = XCDR (prop);
4504 if (!CONSP (prop))
4505 return 0;
4506 prop = XCDR (prop);
4507 }
4508
4509 if (CONSP (prop))
4510 /* Skip over `margin LOCATION'. */
4511 if (EQ (XCAR (prop), Qmargin))
4512 {
4513 prop = XCDR (prop);
4514 if (!CONSP (prop))
4515 return 0;
4516
4517 prop = XCDR (prop);
4518 if (!CONSP (prop))
4519 return 0;
4520 }
4521
4522 return CONSP (prop) && EQ (XCAR (prop), string);
4523 }
4524
4525
4526 /* Return 1 if STRING appears in the `display' property PROP. */
4527
4528 static int
4529 display_prop_string_p (prop, string)
4530 Lisp_Object prop, string;
4531 {
4532 if (CONSP (prop)
4533 && CONSP (XCAR (prop))
4534 && !EQ (Qmargin, XCAR (XCAR (prop))))
4535 {
4536 /* A list of sub-properties. */
4537 while (CONSP (prop))
4538 {
4539 if (single_display_spec_string_p (XCAR (prop), string))
4540 return 1;
4541 prop = XCDR (prop);
4542 }
4543 }
4544 else if (VECTORP (prop))
4545 {
4546 /* A vector of sub-properties. */
4547 int i;
4548 for (i = 0; i < ASIZE (prop); ++i)
4549 if (single_display_spec_string_p (AREF (prop, i), string))
4550 return 1;
4551 }
4552 else
4553 return single_display_spec_string_p (prop, string);
4554
4555 return 0;
4556 }
4557
4558
4559 /* Determine from which buffer position in W's buffer STRING comes
4560 from. AROUND_CHARPOS is an approximate position where it could
4561 be from. Value is the buffer position or 0 if it couldn't be
4562 determined.
4563
4564 W's buffer must be current.
4565
4566 This function is necessary because we don't record buffer positions
4567 in glyphs generated from strings (to keep struct glyph small).
4568 This function may only use code that doesn't eval because it is
4569 called asynchronously from note_mouse_highlight. */
4570
4571 int
4572 string_buffer_position (w, string, around_charpos)
4573 struct window *w;
4574 Lisp_Object string;
4575 int around_charpos;
4576 {
4577 Lisp_Object limit, prop, pos;
4578 const int MAX_DISTANCE = 1000;
4579 int found = 0;
4580
4581 pos = make_number (around_charpos);
4582 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4583 while (!found && !EQ (pos, limit))
4584 {
4585 prop = Fget_char_property (pos, Qdisplay, Qnil);
4586 if (!NILP (prop) && display_prop_string_p (prop, string))
4587 found = 1;
4588 else
4589 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4590 }
4591
4592 if (!found)
4593 {
4594 pos = make_number (around_charpos);
4595 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4596 while (!found && !EQ (pos, limit))
4597 {
4598 prop = Fget_char_property (pos, Qdisplay, Qnil);
4599 if (!NILP (prop) && display_prop_string_p (prop, string))
4600 found = 1;
4601 else
4602 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4603 limit);
4604 }
4605 }
4606
4607 return found ? XINT (pos) : 0;
4608 }
4609
4610
4611 \f
4612 /***********************************************************************
4613 `composition' property
4614 ***********************************************************************/
4615
4616 static enum prop_handled
4617 handle_auto_composed_prop (it)
4618 struct it *it;
4619 {
4620 enum prop_handled handled = HANDLED_NORMALLY;
4621
4622 if (FUNCTIONP (Vauto_composition_function))
4623 {
4624 Lisp_Object val = Qnil;
4625 EMACS_INT pos, limit = -1;
4626
4627 if (STRINGP (it->string))
4628 pos = IT_STRING_CHARPOS (*it);
4629 else
4630 pos = IT_CHARPOS (*it);
4631
4632 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4633 if (! NILP (val))
4634 {
4635 Lisp_Object cmp_prop;
4636 EMACS_INT cmp_start, cmp_end;
4637
4638 if (get_property_and_range (pos, Qcomposition, &cmp_prop,
4639 &cmp_start, &cmp_end, it->string)
4640 && cmp_start == pos
4641 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4642 {
4643 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4644 Lisp_Object font_object = LGSTRING_FONT (gstring);
4645
4646 if (! EQ (font_object,
4647 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4648 it->w, it->string)))
4649 /* We must re-compute the composition for the
4650 different font. */
4651 val = Qnil;
4652 }
4653
4654 if (! NILP (val))
4655 {
4656 Lisp_Object end;
4657
4658 /* As Fnext_single_char_property_change is very slow, we
4659 limit the search to the current line. */
4660 if (STRINGP (it->string))
4661 limit = SCHARS (it->string);
4662 else
4663 limit = find_next_newline_no_quit (pos, 1);
4664 end = Fnext_single_char_property_change (make_number (pos),
4665 Qauto_composed,
4666 it->string,
4667 make_number (limit));
4668
4669 if (XINT (end) < limit)
4670 /* The current point is auto-composed, but there exist
4671 characters not yet composed beyond the
4672 auto-composed region. There's a possiblity that
4673 the last characters in the region may be newly
4674 composed. */
4675 val = Qnil;
4676 }
4677 }
4678 if (NILP (val) && ! STRINGP (it->string))
4679 {
4680 if (limit < 0)
4681 limit = (STRINGP (it->string) ? SCHARS (it->string)
4682 : find_next_newline_no_quit (pos, 1));
4683 if (pos < limit)
4684 {
4685 int count = SPECPDL_INDEX ();
4686 Lisp_Object args[5];
4687
4688 if (FRAME_WINDOW_P (it->f))
4689 limit = font_range (pos, limit,
4690 FACE_FROM_ID (it->f, it->face_id),
4691 it->f, it->string);
4692 args[0] = Vauto_composition_function;
4693 specbind (Qauto_composition_function, Qnil);
4694 args[1] = make_number (pos);
4695 args[2] = make_number (limit);
4696 args[3] = it->window;
4697 args[4] = it->string;
4698 safe_call (5, args);
4699 unbind_to (count, Qnil);
4700 }
4701 }
4702 }
4703
4704 return handled;
4705 }
4706
4707 /* Set up iterator IT from `composition' property at its current
4708 position. Called from handle_stop. */
4709
4710 static enum prop_handled
4711 handle_composition_prop (it)
4712 struct it *it;
4713 {
4714 Lisp_Object prop, string;
4715 EMACS_INT pos, pos_byte, start, end;
4716 enum prop_handled handled = HANDLED_NORMALLY;
4717
4718 if (STRINGP (it->string))
4719 {
4720 unsigned char *s;
4721
4722 pos = IT_STRING_CHARPOS (*it);
4723 pos_byte = IT_STRING_BYTEPOS (*it);
4724 string = it->string;
4725 s = SDATA (string) + pos_byte;
4726 it->c = STRING_CHAR (s, 0);
4727 }
4728 else
4729 {
4730 pos = IT_CHARPOS (*it);
4731 pos_byte = IT_BYTEPOS (*it);
4732 string = Qnil;
4733 it->c = FETCH_CHAR (pos_byte);
4734 }
4735
4736 /* If there's a valid composition and point is not inside of the
4737 composition (in the case that the composition is from the current
4738 buffer), draw a glyph composed from the composition components. */
4739 if (find_composition (pos, -1, &start, &end, &prop, string)
4740 && COMPOSITION_VALID_P (start, end, prop)
4741 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4742 {
4743 int id;
4744
4745 if (start != pos)
4746 {
4747 if (STRINGP (it->string))
4748 pos_byte = string_char_to_byte (it->string, start);
4749 else
4750 pos_byte = CHAR_TO_BYTE (start);
4751 }
4752 id = get_composition_id (start, pos_byte, end - start, prop, string);
4753
4754 if (id >= 0)
4755 {
4756 struct composition *cmp = composition_table[id];
4757
4758 if (cmp->glyph_len == 0)
4759 {
4760 /* No glyph. */
4761 if (STRINGP (it->string))
4762 {
4763 IT_STRING_CHARPOS (*it) = end;
4764 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4765 end);
4766 }
4767 else
4768 {
4769 IT_CHARPOS (*it) = end;
4770 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4771 }
4772 return HANDLED_RECOMPUTE_PROPS;
4773 }
4774
4775 it->stop_charpos = end;
4776 push_it (it);
4777
4778 it->method = GET_FROM_COMPOSITION;
4779 it->cmp_id = id;
4780 it->cmp_len = COMPOSITION_LENGTH (prop);
4781 /* For a terminal, draw only the first (non-TAB) character
4782 of the components. */
4783 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4784 {
4785 /* FIXME: This doesn't do anything!?! */
4786 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4787 ->key_and_value,
4788 cmp->hash_index * 2);
4789 }
4790 else
4791 {
4792 int i;
4793
4794 for (i = 0; i < cmp->glyph_len; i++)
4795 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4796 != '\t')
4797 break;
4798 }
4799 if (it->c == '\t')
4800 it->c = ' ';
4801 it->len = (STRINGP (it->string)
4802 ? string_char_to_byte (it->string, end)
4803 : CHAR_TO_BYTE (end)) - pos_byte;
4804 handled = HANDLED_RETURN;
4805 }
4806 }
4807
4808 return handled;
4809 }
4810
4811
4812 \f
4813 /***********************************************************************
4814 Overlay strings
4815 ***********************************************************************/
4816
4817 /* The following structure is used to record overlay strings for
4818 later sorting in load_overlay_strings. */
4819
4820 struct overlay_entry
4821 {
4822 Lisp_Object overlay;
4823 Lisp_Object string;
4824 int priority;
4825 int after_string_p;
4826 };
4827
4828
4829 /* Set up iterator IT from overlay strings at its current position.
4830 Called from handle_stop. */
4831
4832 static enum prop_handled
4833 handle_overlay_change (it)
4834 struct it *it;
4835 {
4836 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4837 return HANDLED_RECOMPUTE_PROPS;
4838 else
4839 return HANDLED_NORMALLY;
4840 }
4841
4842
4843 /* Set up the next overlay string for delivery by IT, if there is an
4844 overlay string to deliver. Called by set_iterator_to_next when the
4845 end of the current overlay string is reached. If there are more
4846 overlay strings to display, IT->string and
4847 IT->current.overlay_string_index are set appropriately here.
4848 Otherwise IT->string is set to nil. */
4849
4850 static void
4851 next_overlay_string (it)
4852 struct it *it;
4853 {
4854 ++it->current.overlay_string_index;
4855 if (it->current.overlay_string_index == it->n_overlay_strings)
4856 {
4857 /* No more overlay strings. Restore IT's settings to what
4858 they were before overlay strings were processed, and
4859 continue to deliver from current_buffer. */
4860 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4861
4862 pop_it (it);
4863 xassert (it->sp > 0
4864 || it->method == GET_FROM_COMPOSITION
4865 || (NILP (it->string)
4866 && it->method == GET_FROM_BUFFER
4867 && it->stop_charpos >= BEGV
4868 && it->stop_charpos <= it->end_charpos));
4869 it->current.overlay_string_index = -1;
4870 it->n_overlay_strings = 0;
4871
4872 /* If we're at the end of the buffer, record that we have
4873 processed the overlay strings there already, so that
4874 next_element_from_buffer doesn't try it again. */
4875 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4876 it->overlay_strings_at_end_processed_p = 1;
4877
4878 /* If we have to display `...' for invisible text, set
4879 the iterator up for that. */
4880 if (display_ellipsis_p)
4881 setup_for_ellipsis (it, 0);
4882 }
4883 else
4884 {
4885 /* There are more overlay strings to process. If
4886 IT->current.overlay_string_index has advanced to a position
4887 where we must load IT->overlay_strings with more strings, do
4888 it. */
4889 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4890
4891 if (it->current.overlay_string_index && i == 0)
4892 load_overlay_strings (it, 0);
4893
4894 /* Initialize IT to deliver display elements from the overlay
4895 string. */
4896 it->string = it->overlay_strings[i];
4897 it->multibyte_p = STRING_MULTIBYTE (it->string);
4898 SET_TEXT_POS (it->current.string_pos, 0, 0);
4899 it->method = GET_FROM_STRING;
4900 it->stop_charpos = 0;
4901 }
4902
4903 CHECK_IT (it);
4904 }
4905
4906
4907 /* Compare two overlay_entry structures E1 and E2. Used as a
4908 comparison function for qsort in load_overlay_strings. Overlay
4909 strings for the same position are sorted so that
4910
4911 1. All after-strings come in front of before-strings, except
4912 when they come from the same overlay.
4913
4914 2. Within after-strings, strings are sorted so that overlay strings
4915 from overlays with higher priorities come first.
4916
4917 2. Within before-strings, strings are sorted so that overlay
4918 strings from overlays with higher priorities come last.
4919
4920 Value is analogous to strcmp. */
4921
4922
4923 static int
4924 compare_overlay_entries (e1, e2)
4925 void *e1, *e2;
4926 {
4927 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4928 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4929 int result;
4930
4931 if (entry1->after_string_p != entry2->after_string_p)
4932 {
4933 /* Let after-strings appear in front of before-strings if
4934 they come from different overlays. */
4935 if (EQ (entry1->overlay, entry2->overlay))
4936 result = entry1->after_string_p ? 1 : -1;
4937 else
4938 result = entry1->after_string_p ? -1 : 1;
4939 }
4940 else if (entry1->after_string_p)
4941 /* After-strings sorted in order of decreasing priority. */
4942 result = entry2->priority - entry1->priority;
4943 else
4944 /* Before-strings sorted in order of increasing priority. */
4945 result = entry1->priority - entry2->priority;
4946
4947 return result;
4948 }
4949
4950
4951 /* Load the vector IT->overlay_strings with overlay strings from IT's
4952 current buffer position, or from CHARPOS if that is > 0. Set
4953 IT->n_overlays to the total number of overlay strings found.
4954
4955 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4956 a time. On entry into load_overlay_strings,
4957 IT->current.overlay_string_index gives the number of overlay
4958 strings that have already been loaded by previous calls to this
4959 function.
4960
4961 IT->add_overlay_start contains an additional overlay start
4962 position to consider for taking overlay strings from, if non-zero.
4963 This position comes into play when the overlay has an `invisible'
4964 property, and both before and after-strings. When we've skipped to
4965 the end of the overlay, because of its `invisible' property, we
4966 nevertheless want its before-string to appear.
4967 IT->add_overlay_start will contain the overlay start position
4968 in this case.
4969
4970 Overlay strings are sorted so that after-string strings come in
4971 front of before-string strings. Within before and after-strings,
4972 strings are sorted by overlay priority. See also function
4973 compare_overlay_entries. */
4974
4975 static void
4976 load_overlay_strings (it, charpos)
4977 struct it *it;
4978 int charpos;
4979 {
4980 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4981 Lisp_Object overlay, window, str, invisible;
4982 struct Lisp_Overlay *ov;
4983 int start, end;
4984 int size = 20;
4985 int n = 0, i, j, invis_p;
4986 struct overlay_entry *entries
4987 = (struct overlay_entry *) alloca (size * sizeof *entries);
4988
4989 if (charpos <= 0)
4990 charpos = IT_CHARPOS (*it);
4991
4992 /* Append the overlay string STRING of overlay OVERLAY to vector
4993 `entries' which has size `size' and currently contains `n'
4994 elements. AFTER_P non-zero means STRING is an after-string of
4995 OVERLAY. */
4996 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4997 do \
4998 { \
4999 Lisp_Object priority; \
5000 \
5001 if (n == size) \
5002 { \
5003 int new_size = 2 * size; \
5004 struct overlay_entry *old = entries; \
5005 entries = \
5006 (struct overlay_entry *) alloca (new_size \
5007 * sizeof *entries); \
5008 bcopy (old, entries, size * sizeof *entries); \
5009 size = new_size; \
5010 } \
5011 \
5012 entries[n].string = (STRING); \
5013 entries[n].overlay = (OVERLAY); \
5014 priority = Foverlay_get ((OVERLAY), Qpriority); \
5015 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5016 entries[n].after_string_p = (AFTER_P); \
5017 ++n; \
5018 } \
5019 while (0)
5020
5021 /* Process overlay before the overlay center. */
5022 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5023 {
5024 XSETMISC (overlay, ov);
5025 xassert (OVERLAYP (overlay));
5026 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5027 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5028
5029 if (end < charpos)
5030 break;
5031
5032 /* Skip this overlay if it doesn't start or end at IT's current
5033 position. */
5034 if (end != charpos && start != charpos)
5035 continue;
5036
5037 /* Skip this overlay if it doesn't apply to IT->w. */
5038 window = Foverlay_get (overlay, Qwindow);
5039 if (WINDOWP (window) && XWINDOW (window) != it->w)
5040 continue;
5041
5042 /* If the text ``under'' the overlay is invisible, both before-
5043 and after-strings from this overlay are visible; start and
5044 end position are indistinguishable. */
5045 invisible = Foverlay_get (overlay, Qinvisible);
5046 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5047
5048 /* If overlay has a non-empty before-string, record it. */
5049 if ((start == charpos || (end == charpos && invis_p))
5050 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5051 && SCHARS (str))
5052 RECORD_OVERLAY_STRING (overlay, str, 0);
5053
5054 /* If overlay has a non-empty after-string, record it. */
5055 if ((end == charpos || (start == charpos && invis_p))
5056 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5057 && SCHARS (str))
5058 RECORD_OVERLAY_STRING (overlay, str, 1);
5059 }
5060
5061 /* Process overlays after the overlay center. */
5062 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5063 {
5064 XSETMISC (overlay, ov);
5065 xassert (OVERLAYP (overlay));
5066 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5067 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5068
5069 if (start > charpos)
5070 break;
5071
5072 /* Skip this overlay if it doesn't start or end at IT's current
5073 position. */
5074 if (end != charpos && start != charpos)
5075 continue;
5076
5077 /* Skip this overlay if it doesn't apply to IT->w. */
5078 window = Foverlay_get (overlay, Qwindow);
5079 if (WINDOWP (window) && XWINDOW (window) != it->w)
5080 continue;
5081
5082 /* If the text ``under'' the overlay is invisible, it has a zero
5083 dimension, and both before- and after-strings apply. */
5084 invisible = Foverlay_get (overlay, Qinvisible);
5085 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5086
5087 /* If overlay has a non-empty before-string, record it. */
5088 if ((start == charpos || (end == charpos && invis_p))
5089 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5090 && SCHARS (str))
5091 RECORD_OVERLAY_STRING (overlay, str, 0);
5092
5093 /* If overlay has a non-empty after-string, record it. */
5094 if ((end == charpos || (start == charpos && invis_p))
5095 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5096 && SCHARS (str))
5097 RECORD_OVERLAY_STRING (overlay, str, 1);
5098 }
5099
5100 #undef RECORD_OVERLAY_STRING
5101
5102 /* Sort entries. */
5103 if (n > 1)
5104 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5105
5106 /* Record the total number of strings to process. */
5107 it->n_overlay_strings = n;
5108
5109 /* IT->current.overlay_string_index is the number of overlay strings
5110 that have already been consumed by IT. Copy some of the
5111 remaining overlay strings to IT->overlay_strings. */
5112 i = 0;
5113 j = it->current.overlay_string_index;
5114 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5115 {
5116 it->overlay_strings[i] = entries[j].string;
5117 it->string_overlays[i++] = entries[j++].overlay;
5118 }
5119
5120 CHECK_IT (it);
5121 }
5122
5123
5124 /* Get the first chunk of overlay strings at IT's current buffer
5125 position, or at CHARPOS if that is > 0. Value is non-zero if at
5126 least one overlay string was found. */
5127
5128 static int
5129 get_overlay_strings_1 (it, charpos, compute_stop_p)
5130 struct it *it;
5131 int charpos;
5132 int compute_stop_p;
5133 {
5134 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5135 process. This fills IT->overlay_strings with strings, and sets
5136 IT->n_overlay_strings to the total number of strings to process.
5137 IT->pos.overlay_string_index has to be set temporarily to zero
5138 because load_overlay_strings needs this; it must be set to -1
5139 when no overlay strings are found because a zero value would
5140 indicate a position in the first overlay string. */
5141 it->current.overlay_string_index = 0;
5142 load_overlay_strings (it, charpos);
5143
5144 /* If we found overlay strings, set up IT to deliver display
5145 elements from the first one. Otherwise set up IT to deliver
5146 from current_buffer. */
5147 if (it->n_overlay_strings)
5148 {
5149 /* Make sure we know settings in current_buffer, so that we can
5150 restore meaningful values when we're done with the overlay
5151 strings. */
5152 if (compute_stop_p)
5153 compute_stop_pos (it);
5154 xassert (it->face_id >= 0);
5155
5156 /* Save IT's settings. They are restored after all overlay
5157 strings have been processed. */
5158 xassert (!compute_stop_p || it->sp == 0);
5159 push_it (it);
5160
5161 /* Set up IT to deliver display elements from the first overlay
5162 string. */
5163 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5164 it->string = it->overlay_strings[0];
5165 it->from_overlay = Qnil;
5166 it->stop_charpos = 0;
5167 xassert (STRINGP (it->string));
5168 it->end_charpos = SCHARS (it->string);
5169 it->multibyte_p = STRING_MULTIBYTE (it->string);
5170 it->method = GET_FROM_STRING;
5171 return 1;
5172 }
5173
5174 it->current.overlay_string_index = -1;
5175 return 0;
5176 }
5177
5178 static int
5179 get_overlay_strings (it, charpos)
5180 struct it *it;
5181 int charpos;
5182 {
5183 it->string = Qnil;
5184 it->method = GET_FROM_BUFFER;
5185
5186 (void) get_overlay_strings_1 (it, charpos, 1);
5187
5188 CHECK_IT (it);
5189
5190 /* Value is non-zero if we found at least one overlay string. */
5191 return STRINGP (it->string);
5192 }
5193
5194
5195 \f
5196 /***********************************************************************
5197 Saving and restoring state
5198 ***********************************************************************/
5199
5200 /* Save current settings of IT on IT->stack. Called, for example,
5201 before setting up IT for an overlay string, to be able to restore
5202 IT's settings to what they were after the overlay string has been
5203 processed. */
5204
5205 static void
5206 push_it (it)
5207 struct it *it;
5208 {
5209 struct iterator_stack_entry *p;
5210
5211 xassert (it->sp < IT_STACK_SIZE);
5212 p = it->stack + it->sp;
5213
5214 p->stop_charpos = it->stop_charpos;
5215 xassert (it->face_id >= 0);
5216 p->face_id = it->face_id;
5217 p->string = it->string;
5218 p->method = it->method;
5219 p->from_overlay = it->from_overlay;
5220 switch (p->method)
5221 {
5222 case GET_FROM_IMAGE:
5223 p->u.image.object = it->object;
5224 p->u.image.image_id = it->image_id;
5225 p->u.image.slice = it->slice;
5226 break;
5227 case GET_FROM_COMPOSITION:
5228 p->u.comp.object = it->object;
5229 p->u.comp.c = it->c;
5230 p->u.comp.len = it->len;
5231 p->u.comp.cmp_id = it->cmp_id;
5232 p->u.comp.cmp_len = it->cmp_len;
5233 break;
5234 case GET_FROM_STRETCH:
5235 p->u.stretch.object = it->object;
5236 break;
5237 }
5238 p->position = it->position;
5239 p->current = it->current;
5240 p->end_charpos = it->end_charpos;
5241 p->string_nchars = it->string_nchars;
5242 p->area = it->area;
5243 p->multibyte_p = it->multibyte_p;
5244 p->avoid_cursor_p = it->avoid_cursor_p;
5245 p->space_width = it->space_width;
5246 p->font_height = it->font_height;
5247 p->voffset = it->voffset;
5248 p->string_from_display_prop_p = it->string_from_display_prop_p;
5249 p->display_ellipsis_p = 0;
5250 ++it->sp;
5251 }
5252
5253
5254 /* Restore IT's settings from IT->stack. Called, for example, when no
5255 more overlay strings must be processed, and we return to delivering
5256 display elements from a buffer, or when the end of a string from a
5257 `display' property is reached and we return to delivering display
5258 elements from an overlay string, or from a buffer. */
5259
5260 static void
5261 pop_it (it)
5262 struct it *it;
5263 {
5264 struct iterator_stack_entry *p;
5265
5266 xassert (it->sp > 0);
5267 --it->sp;
5268 p = it->stack + it->sp;
5269 it->stop_charpos = p->stop_charpos;
5270 it->face_id = p->face_id;
5271 it->current = p->current;
5272 it->position = p->position;
5273 it->string = p->string;
5274 it->from_overlay = p->from_overlay;
5275 if (NILP (it->string))
5276 SET_TEXT_POS (it->current.string_pos, -1, -1);
5277 it->method = p->method;
5278 switch (it->method)
5279 {
5280 case GET_FROM_IMAGE:
5281 it->image_id = p->u.image.image_id;
5282 it->object = p->u.image.object;
5283 it->slice = p->u.image.slice;
5284 break;
5285 case GET_FROM_COMPOSITION:
5286 it->object = p->u.comp.object;
5287 it->c = p->u.comp.c;
5288 it->len = p->u.comp.len;
5289 it->cmp_id = p->u.comp.cmp_id;
5290 it->cmp_len = p->u.comp.cmp_len;
5291 break;
5292 case GET_FROM_STRETCH:
5293 it->object = p->u.comp.object;
5294 break;
5295 case GET_FROM_BUFFER:
5296 it->object = it->w->buffer;
5297 break;
5298 case GET_FROM_STRING:
5299 it->object = it->string;
5300 break;
5301 }
5302 it->end_charpos = p->end_charpos;
5303 it->string_nchars = p->string_nchars;
5304 it->area = p->area;
5305 it->multibyte_p = p->multibyte_p;
5306 it->avoid_cursor_p = p->avoid_cursor_p;
5307 it->space_width = p->space_width;
5308 it->font_height = p->font_height;
5309 it->voffset = p->voffset;
5310 it->string_from_display_prop_p = p->string_from_display_prop_p;
5311 }
5312
5313
5314 \f
5315 /***********************************************************************
5316 Moving over lines
5317 ***********************************************************************/
5318
5319 /* Set IT's current position to the previous line start. */
5320
5321 static void
5322 back_to_previous_line_start (it)
5323 struct it *it;
5324 {
5325 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5326 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5327 }
5328
5329
5330 /* Move IT to the next line start.
5331
5332 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5333 we skipped over part of the text (as opposed to moving the iterator
5334 continuously over the text). Otherwise, don't change the value
5335 of *SKIPPED_P.
5336
5337 Newlines may come from buffer text, overlay strings, or strings
5338 displayed via the `display' property. That's the reason we can't
5339 simply use find_next_newline_no_quit.
5340
5341 Note that this function may not skip over invisible text that is so
5342 because of text properties and immediately follows a newline. If
5343 it would, function reseat_at_next_visible_line_start, when called
5344 from set_iterator_to_next, would effectively make invisible
5345 characters following a newline part of the wrong glyph row, which
5346 leads to wrong cursor motion. */
5347
5348 static int
5349 forward_to_next_line_start (it, skipped_p)
5350 struct it *it;
5351 int *skipped_p;
5352 {
5353 int old_selective, newline_found_p, n;
5354 const int MAX_NEWLINE_DISTANCE = 500;
5355
5356 /* If already on a newline, just consume it to avoid unintended
5357 skipping over invisible text below. */
5358 if (it->what == IT_CHARACTER
5359 && it->c == '\n'
5360 && CHARPOS (it->position) == IT_CHARPOS (*it))
5361 {
5362 set_iterator_to_next (it, 0);
5363 it->c = 0;
5364 return 1;
5365 }
5366
5367 /* Don't handle selective display in the following. It's (a)
5368 unnecessary because it's done by the caller, and (b) leads to an
5369 infinite recursion because next_element_from_ellipsis indirectly
5370 calls this function. */
5371 old_selective = it->selective;
5372 it->selective = 0;
5373
5374 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5375 from buffer text. */
5376 for (n = newline_found_p = 0;
5377 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5378 n += STRINGP (it->string) ? 0 : 1)
5379 {
5380 if (!get_next_display_element (it))
5381 return 0;
5382 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5383 set_iterator_to_next (it, 0);
5384 }
5385
5386 /* If we didn't find a newline near enough, see if we can use a
5387 short-cut. */
5388 if (!newline_found_p)
5389 {
5390 int start = IT_CHARPOS (*it);
5391 int limit = find_next_newline_no_quit (start, 1);
5392 Lisp_Object pos;
5393
5394 xassert (!STRINGP (it->string));
5395
5396 /* If there isn't any `display' property in sight, and no
5397 overlays, we can just use the position of the newline in
5398 buffer text. */
5399 if (it->stop_charpos >= limit
5400 || ((pos = Fnext_single_property_change (make_number (start),
5401 Qdisplay,
5402 Qnil, make_number (limit)),
5403 NILP (pos))
5404 && next_overlay_change (start) == ZV))
5405 {
5406 IT_CHARPOS (*it) = limit;
5407 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5408 *skipped_p = newline_found_p = 1;
5409 }
5410 else
5411 {
5412 while (get_next_display_element (it)
5413 && !newline_found_p)
5414 {
5415 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5416 set_iterator_to_next (it, 0);
5417 }
5418 }
5419 }
5420
5421 it->selective = old_selective;
5422 return newline_found_p;
5423 }
5424
5425
5426 /* Set IT's current position to the previous visible line start. Skip
5427 invisible text that is so either due to text properties or due to
5428 selective display. Caution: this does not change IT->current_x and
5429 IT->hpos. */
5430
5431 static void
5432 back_to_previous_visible_line_start (it)
5433 struct it *it;
5434 {
5435 while (IT_CHARPOS (*it) > BEGV)
5436 {
5437 back_to_previous_line_start (it);
5438
5439 if (IT_CHARPOS (*it) <= BEGV)
5440 break;
5441
5442 /* If selective > 0, then lines indented more than that values
5443 are invisible. */
5444 if (it->selective > 0
5445 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5446 (double) it->selective)) /* iftc */
5447 continue;
5448
5449 /* Check the newline before point for invisibility. */
5450 {
5451 Lisp_Object prop;
5452 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5453 Qinvisible, it->window);
5454 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5455 continue;
5456 }
5457
5458 if (IT_CHARPOS (*it) <= BEGV)
5459 break;
5460
5461 {
5462 struct it it2;
5463 int pos;
5464 EMACS_INT beg, end;
5465 Lisp_Object val, overlay;
5466
5467 /* If newline is part of a composition, continue from start of composition */
5468 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5469 && beg < IT_CHARPOS (*it))
5470 goto replaced;
5471
5472 /* If newline is replaced by a display property, find start of overlay
5473 or interval and continue search from that point. */
5474 it2 = *it;
5475 pos = --IT_CHARPOS (it2);
5476 --IT_BYTEPOS (it2);
5477 it2.sp = 0;
5478 it2.string_from_display_prop_p = 0;
5479 if (handle_display_prop (&it2) == HANDLED_RETURN
5480 && !NILP (val = get_char_property_and_overlay
5481 (make_number (pos), Qdisplay, Qnil, &overlay))
5482 && (OVERLAYP (overlay)
5483 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5484 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5485 goto replaced;
5486
5487 /* Newline is not replaced by anything -- so we are done. */
5488 break;
5489
5490 replaced:
5491 if (beg < BEGV)
5492 beg = BEGV;
5493 IT_CHARPOS (*it) = beg;
5494 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5495 }
5496 }
5497
5498 it->continuation_lines_width = 0;
5499
5500 xassert (IT_CHARPOS (*it) >= BEGV);
5501 xassert (IT_CHARPOS (*it) == BEGV
5502 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5503 CHECK_IT (it);
5504 }
5505
5506
5507 /* Reseat iterator IT at the previous visible line start. Skip
5508 invisible text that is so either due to text properties or due to
5509 selective display. At the end, update IT's overlay information,
5510 face information etc. */
5511
5512 void
5513 reseat_at_previous_visible_line_start (it)
5514 struct it *it;
5515 {
5516 back_to_previous_visible_line_start (it);
5517 reseat (it, it->current.pos, 1);
5518 CHECK_IT (it);
5519 }
5520
5521
5522 /* Reseat iterator IT on the next visible line start in the current
5523 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5524 preceding the line start. Skip over invisible text that is so
5525 because of selective display. Compute faces, overlays etc at the
5526 new position. Note that this function does not skip over text that
5527 is invisible because of text properties. */
5528
5529 static void
5530 reseat_at_next_visible_line_start (it, on_newline_p)
5531 struct it *it;
5532 int on_newline_p;
5533 {
5534 int newline_found_p, skipped_p = 0;
5535
5536 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5537
5538 /* Skip over lines that are invisible because they are indented
5539 more than the value of IT->selective. */
5540 if (it->selective > 0)
5541 while (IT_CHARPOS (*it) < ZV
5542 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5543 (double) it->selective)) /* iftc */
5544 {
5545 xassert (IT_BYTEPOS (*it) == BEGV
5546 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5547 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5548 }
5549
5550 /* Position on the newline if that's what's requested. */
5551 if (on_newline_p && newline_found_p)
5552 {
5553 if (STRINGP (it->string))
5554 {
5555 if (IT_STRING_CHARPOS (*it) > 0)
5556 {
5557 --IT_STRING_CHARPOS (*it);
5558 --IT_STRING_BYTEPOS (*it);
5559 }
5560 }
5561 else if (IT_CHARPOS (*it) > BEGV)
5562 {
5563 --IT_CHARPOS (*it);
5564 --IT_BYTEPOS (*it);
5565 reseat (it, it->current.pos, 0);
5566 }
5567 }
5568 else if (skipped_p)
5569 reseat (it, it->current.pos, 0);
5570
5571 CHECK_IT (it);
5572 }
5573
5574
5575 \f
5576 /***********************************************************************
5577 Changing an iterator's position
5578 ***********************************************************************/
5579
5580 /* Change IT's current position to POS in current_buffer. If FORCE_P
5581 is non-zero, always check for text properties at the new position.
5582 Otherwise, text properties are only looked up if POS >=
5583 IT->check_charpos of a property. */
5584
5585 static void
5586 reseat (it, pos, force_p)
5587 struct it *it;
5588 struct text_pos pos;
5589 int force_p;
5590 {
5591 int original_pos = IT_CHARPOS (*it);
5592
5593 reseat_1 (it, pos, 0);
5594
5595 /* Determine where to check text properties. Avoid doing it
5596 where possible because text property lookup is very expensive. */
5597 if (force_p
5598 || CHARPOS (pos) > it->stop_charpos
5599 || CHARPOS (pos) < original_pos)
5600 handle_stop (it);
5601
5602 CHECK_IT (it);
5603 }
5604
5605
5606 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5607 IT->stop_pos to POS, also. */
5608
5609 static void
5610 reseat_1 (it, pos, set_stop_p)
5611 struct it *it;
5612 struct text_pos pos;
5613 int set_stop_p;
5614 {
5615 /* Don't call this function when scanning a C string. */
5616 xassert (it->s == NULL);
5617
5618 /* POS must be a reasonable value. */
5619 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5620
5621 it->current.pos = it->position = pos;
5622 it->end_charpos = ZV;
5623 it->dpvec = NULL;
5624 it->current.dpvec_index = -1;
5625 it->current.overlay_string_index = -1;
5626 IT_STRING_CHARPOS (*it) = -1;
5627 IT_STRING_BYTEPOS (*it) = -1;
5628 it->string = Qnil;
5629 it->string_from_display_prop_p = 0;
5630 it->method = GET_FROM_BUFFER;
5631 it->object = it->w->buffer;
5632 it->area = TEXT_AREA;
5633 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5634 it->sp = 0;
5635 it->string_from_display_prop_p = 0;
5636 it->face_before_selective_p = 0;
5637
5638 if (set_stop_p)
5639 it->stop_charpos = CHARPOS (pos);
5640 }
5641
5642
5643 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5644 If S is non-null, it is a C string to iterate over. Otherwise,
5645 STRING gives a Lisp string to iterate over.
5646
5647 If PRECISION > 0, don't return more then PRECISION number of
5648 characters from the string.
5649
5650 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5651 characters have been returned. FIELD_WIDTH < 0 means an infinite
5652 field width.
5653
5654 MULTIBYTE = 0 means disable processing of multibyte characters,
5655 MULTIBYTE > 0 means enable it,
5656 MULTIBYTE < 0 means use IT->multibyte_p.
5657
5658 IT must be initialized via a prior call to init_iterator before
5659 calling this function. */
5660
5661 static void
5662 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5663 struct it *it;
5664 unsigned char *s;
5665 Lisp_Object string;
5666 int charpos;
5667 int precision, field_width, multibyte;
5668 {
5669 /* No region in strings. */
5670 it->region_beg_charpos = it->region_end_charpos = -1;
5671
5672 /* No text property checks performed by default, but see below. */
5673 it->stop_charpos = -1;
5674
5675 /* Set iterator position and end position. */
5676 bzero (&it->current, sizeof it->current);
5677 it->current.overlay_string_index = -1;
5678 it->current.dpvec_index = -1;
5679 xassert (charpos >= 0);
5680
5681 /* If STRING is specified, use its multibyteness, otherwise use the
5682 setting of MULTIBYTE, if specified. */
5683 if (multibyte >= 0)
5684 it->multibyte_p = multibyte > 0;
5685
5686 if (s == NULL)
5687 {
5688 xassert (STRINGP (string));
5689 it->string = string;
5690 it->s = NULL;
5691 it->end_charpos = it->string_nchars = SCHARS (string);
5692 it->method = GET_FROM_STRING;
5693 it->current.string_pos = string_pos (charpos, string);
5694 }
5695 else
5696 {
5697 it->s = s;
5698 it->string = Qnil;
5699
5700 /* Note that we use IT->current.pos, not it->current.string_pos,
5701 for displaying C strings. */
5702 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5703 if (it->multibyte_p)
5704 {
5705 it->current.pos = c_string_pos (charpos, s, 1);
5706 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5707 }
5708 else
5709 {
5710 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5711 it->end_charpos = it->string_nchars = strlen (s);
5712 }
5713
5714 it->method = GET_FROM_C_STRING;
5715 }
5716
5717 /* PRECISION > 0 means don't return more than PRECISION characters
5718 from the string. */
5719 if (precision > 0 && it->end_charpos - charpos > precision)
5720 it->end_charpos = it->string_nchars = charpos + precision;
5721
5722 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5723 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5724 FIELD_WIDTH < 0 means infinite field width. This is useful for
5725 padding with `-' at the end of a mode line. */
5726 if (field_width < 0)
5727 field_width = INFINITY;
5728 if (field_width > it->end_charpos - charpos)
5729 it->end_charpos = charpos + field_width;
5730
5731 /* Use the standard display table for displaying strings. */
5732 if (DISP_TABLE_P (Vstandard_display_table))
5733 it->dp = XCHAR_TABLE (Vstandard_display_table);
5734
5735 it->stop_charpos = charpos;
5736 CHECK_IT (it);
5737 }
5738
5739
5740 \f
5741 /***********************************************************************
5742 Iteration
5743 ***********************************************************************/
5744
5745 /* Map enum it_method value to corresponding next_element_from_* function. */
5746
5747 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5748 {
5749 next_element_from_buffer,
5750 next_element_from_display_vector,
5751 next_element_from_composition,
5752 next_element_from_string,
5753 next_element_from_c_string,
5754 next_element_from_image,
5755 next_element_from_stretch
5756 };
5757
5758 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5759
5760 /* Load IT's display element fields with information about the next
5761 display element from the current position of IT. Value is zero if
5762 end of buffer (or C string) is reached. */
5763
5764 static struct frame *last_escape_glyph_frame = NULL;
5765 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5766 static int last_escape_glyph_merged_face_id = 0;
5767
5768 int
5769 get_next_display_element (it)
5770 struct it *it;
5771 {
5772 /* Non-zero means that we found a display element. Zero means that
5773 we hit the end of what we iterate over. Performance note: the
5774 function pointer `method' used here turns out to be faster than
5775 using a sequence of if-statements. */
5776 int success_p;
5777
5778 get_next:
5779 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5780
5781 if (it->what == IT_CHARACTER)
5782 {
5783 /* Map via display table or translate control characters.
5784 IT->c, IT->len etc. have been set to the next character by
5785 the function call above. If we have a display table, and it
5786 contains an entry for IT->c, translate it. Don't do this if
5787 IT->c itself comes from a display table, otherwise we could
5788 end up in an infinite recursion. (An alternative could be to
5789 count the recursion depth of this function and signal an
5790 error when a certain maximum depth is reached.) Is it worth
5791 it? */
5792 if (success_p && it->dpvec == NULL)
5793 {
5794 Lisp_Object dv;
5795
5796 if (it->dp
5797 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5798 VECTORP (dv)))
5799 {
5800 struct Lisp_Vector *v = XVECTOR (dv);
5801
5802 /* Return the first character from the display table
5803 entry, if not empty. If empty, don't display the
5804 current character. */
5805 if (v->size)
5806 {
5807 it->dpvec_char_len = it->len;
5808 it->dpvec = v->contents;
5809 it->dpend = v->contents + v->size;
5810 it->current.dpvec_index = 0;
5811 it->dpvec_face_id = -1;
5812 it->saved_face_id = it->face_id;
5813 it->method = GET_FROM_DISPLAY_VECTOR;
5814 it->ellipsis_p = 0;
5815 }
5816 else
5817 {
5818 set_iterator_to_next (it, 0);
5819 }
5820 goto get_next;
5821 }
5822
5823 /* Translate control characters into `\003' or `^C' form.
5824 Control characters coming from a display table entry are
5825 currently not translated because we use IT->dpvec to hold
5826 the translation. This could easily be changed but I
5827 don't believe that it is worth doing.
5828
5829 If it->multibyte_p is nonzero, non-printable non-ASCII
5830 characters are also translated to octal form.
5831
5832 If it->multibyte_p is zero, eight-bit characters that
5833 don't have corresponding multibyte char code are also
5834 translated to octal form. */
5835 else if ((it->c < ' '
5836 ? (it->area != TEXT_AREA
5837 /* In mode line, treat \n, \t like other crl chars. */
5838 || (it->c != '\t'
5839 && it->glyph_row && it->glyph_row->mode_line_p)
5840 || (it->c != '\n' && it->c != '\t'))
5841 : (it->multibyte_p
5842 ? (!CHAR_PRINTABLE_P (it->c)
5843 || (!NILP (Vnobreak_char_display)
5844 && (it->c == 0xA0 /* NO-BREAK SPACE */
5845 || it->c == 0xAD /* SOFT HYPHEN */)))
5846 : (it->c >= 127
5847 && (! unibyte_display_via_language_environment
5848 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5849 {
5850 /* IT->c is a control character which must be displayed
5851 either as '\003' or as `^C' where the '\\' and '^'
5852 can be defined in the display table. Fill
5853 IT->ctl_chars with glyphs for what we have to
5854 display. Then, set IT->dpvec to these glyphs. */
5855 Lisp_Object gc;
5856 int ctl_len;
5857 int face_id, lface_id = 0 ;
5858 int escape_glyph;
5859
5860 /* Handle control characters with ^. */
5861
5862 if (it->c < 128 && it->ctl_arrow_p)
5863 {
5864 int g;
5865
5866 g = '^'; /* default glyph for Control */
5867 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5868 if (it->dp
5869 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5870 && GLYPH_CODE_CHAR_VALID_P (gc))
5871 {
5872 g = GLYPH_CODE_CHAR (gc);
5873 lface_id = GLYPH_CODE_FACE (gc);
5874 }
5875 if (lface_id)
5876 {
5877 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5878 }
5879 else if (it->f == last_escape_glyph_frame
5880 && it->face_id == last_escape_glyph_face_id)
5881 {
5882 face_id = last_escape_glyph_merged_face_id;
5883 }
5884 else
5885 {
5886 /* Merge the escape-glyph face into the current face. */
5887 face_id = merge_faces (it->f, Qescape_glyph, 0,
5888 it->face_id);
5889 last_escape_glyph_frame = it->f;
5890 last_escape_glyph_face_id = it->face_id;
5891 last_escape_glyph_merged_face_id = face_id;
5892 }
5893
5894 XSETINT (it->ctl_chars[0], g);
5895 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5896 ctl_len = 2;
5897 goto display_control;
5898 }
5899
5900 /* Handle non-break space in the mode where it only gets
5901 highlighting. */
5902
5903 if (EQ (Vnobreak_char_display, Qt)
5904 && it->c == 0xA0)
5905 {
5906 /* Merge the no-break-space face into the current face. */
5907 face_id = merge_faces (it->f, Qnobreak_space, 0,
5908 it->face_id);
5909
5910 it->c = ' ';
5911 XSETINT (it->ctl_chars[0], ' ');
5912 ctl_len = 1;
5913 goto display_control;
5914 }
5915
5916 /* Handle sequences that start with the "escape glyph". */
5917
5918 /* the default escape glyph is \. */
5919 escape_glyph = '\\';
5920
5921 if (it->dp
5922 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5923 && GLYPH_CODE_CHAR_VALID_P (gc))
5924 {
5925 escape_glyph = GLYPH_CODE_CHAR (gc);
5926 lface_id = GLYPH_CODE_FACE (gc);
5927 }
5928 if (lface_id)
5929 {
5930 /* The display table specified a face.
5931 Merge it into face_id and also into escape_glyph. */
5932 face_id = merge_faces (it->f, Qt, lface_id,
5933 it->face_id);
5934 }
5935 else if (it->f == last_escape_glyph_frame
5936 && it->face_id == last_escape_glyph_face_id)
5937 {
5938 face_id = last_escape_glyph_merged_face_id;
5939 }
5940 else
5941 {
5942 /* Merge the escape-glyph face into the current face. */
5943 face_id = merge_faces (it->f, Qescape_glyph, 0,
5944 it->face_id);
5945 last_escape_glyph_frame = it->f;
5946 last_escape_glyph_face_id = it->face_id;
5947 last_escape_glyph_merged_face_id = face_id;
5948 }
5949
5950 /* Handle soft hyphens in the mode where they only get
5951 highlighting. */
5952
5953 if (EQ (Vnobreak_char_display, Qt)
5954 && it->c == 0xAD)
5955 {
5956 it->c = '-';
5957 XSETINT (it->ctl_chars[0], '-');
5958 ctl_len = 1;
5959 goto display_control;
5960 }
5961
5962 /* Handle non-break space and soft hyphen
5963 with the escape glyph. */
5964
5965 if (it->c == 0xA0 || it->c == 0xAD)
5966 {
5967 XSETINT (it->ctl_chars[0], escape_glyph);
5968 it->c = (it->c == 0xA0 ? ' ' : '-');
5969 XSETINT (it->ctl_chars[1], it->c);
5970 ctl_len = 2;
5971 goto display_control;
5972 }
5973
5974 {
5975 unsigned char str[MAX_MULTIBYTE_LENGTH];
5976 int len;
5977 int i;
5978
5979 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5980 if (CHAR_BYTE8_P (it->c))
5981 {
5982 str[0] = CHAR_TO_BYTE8 (it->c);
5983 len = 1;
5984 }
5985 else if (it->c < 256)
5986 {
5987 str[0] = it->c;
5988 len = 1;
5989 }
5990 else
5991 {
5992 /* It's an invalid character, which shouldn't
5993 happen actually, but due to bugs it may
5994 happen. Let's print the char as is, there's
5995 not much meaningful we can do with it. */
5996 str[0] = it->c;
5997 str[1] = it->c >> 8;
5998 str[2] = it->c >> 16;
5999 str[3] = it->c >> 24;
6000 len = 4;
6001 }
6002
6003 for (i = 0; i < len; i++)
6004 {
6005 int g;
6006 XSETINT (it->ctl_chars[i * 4], escape_glyph);
6007 /* Insert three more glyphs into IT->ctl_chars for
6008 the octal display of the character. */
6009 g = ((str[i] >> 6) & 7) + '0';
6010 XSETINT (it->ctl_chars[i * 4 + 1], g);
6011 g = ((str[i] >> 3) & 7) + '0';
6012 XSETINT (it->ctl_chars[i * 4 + 2], g);
6013 g = (str[i] & 7) + '0';
6014 XSETINT (it->ctl_chars[i * 4 + 3], g);
6015 }
6016 ctl_len = len * 4;
6017 }
6018
6019 display_control:
6020 /* Set up IT->dpvec and return first character from it. */
6021 it->dpvec_char_len = it->len;
6022 it->dpvec = it->ctl_chars;
6023 it->dpend = it->dpvec + ctl_len;
6024 it->current.dpvec_index = 0;
6025 it->dpvec_face_id = face_id;
6026 it->saved_face_id = it->face_id;
6027 it->method = GET_FROM_DISPLAY_VECTOR;
6028 it->ellipsis_p = 0;
6029 goto get_next;
6030 }
6031 }
6032 }
6033
6034 /* Adjust face id for a multibyte character. There are no multibyte
6035 character in unibyte text. */
6036 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6037 && it->multibyte_p
6038 && success_p
6039 && FRAME_WINDOW_P (it->f))
6040 {
6041 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6042 int pos = (it->s ? -1
6043 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6044 : IT_CHARPOS (*it));
6045
6046 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6047 }
6048
6049 /* Is this character the last one of a run of characters with
6050 box? If yes, set IT->end_of_box_run_p to 1. */
6051 if (it->face_box_p
6052 && it->s == NULL)
6053 {
6054 int face_id;
6055 struct face *face;
6056
6057 it->end_of_box_run_p
6058 = ((face_id = face_after_it_pos (it),
6059 face_id != it->face_id)
6060 && (face = FACE_FROM_ID (it->f, face_id),
6061 face->box == FACE_NO_BOX));
6062 }
6063
6064 /* Value is 0 if end of buffer or string reached. */
6065 return success_p;
6066 }
6067
6068
6069 /* Move IT to the next display element.
6070
6071 RESEAT_P non-zero means if called on a newline in buffer text,
6072 skip to the next visible line start.
6073
6074 Functions get_next_display_element and set_iterator_to_next are
6075 separate because I find this arrangement easier to handle than a
6076 get_next_display_element function that also increments IT's
6077 position. The way it is we can first look at an iterator's current
6078 display element, decide whether it fits on a line, and if it does,
6079 increment the iterator position. The other way around we probably
6080 would either need a flag indicating whether the iterator has to be
6081 incremented the next time, or we would have to implement a
6082 decrement position function which would not be easy to write. */
6083
6084 void
6085 set_iterator_to_next (it, reseat_p)
6086 struct it *it;
6087 int reseat_p;
6088 {
6089 /* Reset flags indicating start and end of a sequence of characters
6090 with box. Reset them at the start of this function because
6091 moving the iterator to a new position might set them. */
6092 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6093
6094 switch (it->method)
6095 {
6096 case GET_FROM_BUFFER:
6097 /* The current display element of IT is a character from
6098 current_buffer. Advance in the buffer, and maybe skip over
6099 invisible lines that are so because of selective display. */
6100 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6101 reseat_at_next_visible_line_start (it, 0);
6102 else
6103 {
6104 xassert (it->len != 0);
6105 IT_BYTEPOS (*it) += it->len;
6106 IT_CHARPOS (*it) += 1;
6107 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6108 }
6109 break;
6110
6111 case GET_FROM_COMPOSITION:
6112 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6113 xassert (it->sp > 0);
6114 pop_it (it);
6115 if (it->method == GET_FROM_STRING)
6116 {
6117 IT_STRING_BYTEPOS (*it) += it->len;
6118 IT_STRING_CHARPOS (*it) += it->cmp_len;
6119 goto consider_string_end;
6120 }
6121 else if (it->method == GET_FROM_BUFFER)
6122 {
6123 IT_BYTEPOS (*it) += it->len;
6124 IT_CHARPOS (*it) += it->cmp_len;
6125 }
6126 break;
6127
6128 case GET_FROM_C_STRING:
6129 /* Current display element of IT is from a C string. */
6130 IT_BYTEPOS (*it) += it->len;
6131 IT_CHARPOS (*it) += 1;
6132 break;
6133
6134 case GET_FROM_DISPLAY_VECTOR:
6135 /* Current display element of IT is from a display table entry.
6136 Advance in the display table definition. Reset it to null if
6137 end reached, and continue with characters from buffers/
6138 strings. */
6139 ++it->current.dpvec_index;
6140
6141 /* Restore face of the iterator to what they were before the
6142 display vector entry (these entries may contain faces). */
6143 it->face_id = it->saved_face_id;
6144
6145 if (it->dpvec + it->current.dpvec_index == it->dpend)
6146 {
6147 int recheck_faces = it->ellipsis_p;
6148
6149 if (it->s)
6150 it->method = GET_FROM_C_STRING;
6151 else if (STRINGP (it->string))
6152 it->method = GET_FROM_STRING;
6153 else
6154 {
6155 it->method = GET_FROM_BUFFER;
6156 it->object = it->w->buffer;
6157 }
6158
6159 it->dpvec = NULL;
6160 it->current.dpvec_index = -1;
6161
6162 /* Skip over characters which were displayed via IT->dpvec. */
6163 if (it->dpvec_char_len < 0)
6164 reseat_at_next_visible_line_start (it, 1);
6165 else if (it->dpvec_char_len > 0)
6166 {
6167 if (it->method == GET_FROM_STRING
6168 && it->n_overlay_strings > 0)
6169 it->ignore_overlay_strings_at_pos_p = 1;
6170 it->len = it->dpvec_char_len;
6171 set_iterator_to_next (it, reseat_p);
6172 }
6173
6174 /* Maybe recheck faces after display vector */
6175 if (recheck_faces)
6176 it->stop_charpos = IT_CHARPOS (*it);
6177 }
6178 break;
6179
6180 case GET_FROM_STRING:
6181 /* Current display element is a character from a Lisp string. */
6182 xassert (it->s == NULL && STRINGP (it->string));
6183 IT_STRING_BYTEPOS (*it) += it->len;
6184 IT_STRING_CHARPOS (*it) += 1;
6185
6186 consider_string_end:
6187
6188 if (it->current.overlay_string_index >= 0)
6189 {
6190 /* IT->string is an overlay string. Advance to the
6191 next, if there is one. */
6192 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6193 next_overlay_string (it);
6194 }
6195 else
6196 {
6197 /* IT->string is not an overlay string. If we reached
6198 its end, and there is something on IT->stack, proceed
6199 with what is on the stack. This can be either another
6200 string, this time an overlay string, or a buffer. */
6201 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6202 && it->sp > 0)
6203 {
6204 pop_it (it);
6205 if (it->method == GET_FROM_STRING)
6206 goto consider_string_end;
6207 }
6208 }
6209 break;
6210
6211 case GET_FROM_IMAGE:
6212 case GET_FROM_STRETCH:
6213 /* The position etc with which we have to proceed are on
6214 the stack. The position may be at the end of a string,
6215 if the `display' property takes up the whole string. */
6216 xassert (it->sp > 0);
6217 pop_it (it);
6218 if (it->method == GET_FROM_STRING)
6219 goto consider_string_end;
6220 break;
6221
6222 default:
6223 /* There are no other methods defined, so this should be a bug. */
6224 abort ();
6225 }
6226
6227 xassert (it->method != GET_FROM_STRING
6228 || (STRINGP (it->string)
6229 && IT_STRING_CHARPOS (*it) >= 0));
6230 }
6231
6232 /* Load IT's display element fields with information about the next
6233 display element which comes from a display table entry or from the
6234 result of translating a control character to one of the forms `^C'
6235 or `\003'.
6236
6237 IT->dpvec holds the glyphs to return as characters.
6238 IT->saved_face_id holds the face id before the display vector--
6239 it is restored into IT->face_idin set_iterator_to_next. */
6240
6241 static int
6242 next_element_from_display_vector (it)
6243 struct it *it;
6244 {
6245 Lisp_Object gc;
6246
6247 /* Precondition. */
6248 xassert (it->dpvec && it->current.dpvec_index >= 0);
6249
6250 it->face_id = it->saved_face_id;
6251
6252 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6253 That seemed totally bogus - so I changed it... */
6254 gc = it->dpvec[it->current.dpvec_index];
6255
6256 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6257 {
6258 it->c = GLYPH_CODE_CHAR (gc);
6259 it->len = CHAR_BYTES (it->c);
6260
6261 /* The entry may contain a face id to use. Such a face id is
6262 the id of a Lisp face, not a realized face. A face id of
6263 zero means no face is specified. */
6264 if (it->dpvec_face_id >= 0)
6265 it->face_id = it->dpvec_face_id;
6266 else
6267 {
6268 int lface_id = GLYPH_CODE_FACE (gc);
6269 if (lface_id > 0)
6270 it->face_id = merge_faces (it->f, Qt, lface_id,
6271 it->saved_face_id);
6272 }
6273 }
6274 else
6275 /* Display table entry is invalid. Return a space. */
6276 it->c = ' ', it->len = 1;
6277
6278 /* Don't change position and object of the iterator here. They are
6279 still the values of the character that had this display table
6280 entry or was translated, and that's what we want. */
6281 it->what = IT_CHARACTER;
6282 return 1;
6283 }
6284
6285
6286 /* Load IT with the next display element from Lisp string IT->string.
6287 IT->current.string_pos is the current position within the string.
6288 If IT->current.overlay_string_index >= 0, the Lisp string is an
6289 overlay string. */
6290
6291 static int
6292 next_element_from_string (it)
6293 struct it *it;
6294 {
6295 struct text_pos position;
6296
6297 xassert (STRINGP (it->string));
6298 xassert (IT_STRING_CHARPOS (*it) >= 0);
6299 position = it->current.string_pos;
6300
6301 /* Time to check for invisible text? */
6302 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6303 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6304 {
6305 handle_stop (it);
6306
6307 /* Since a handler may have changed IT->method, we must
6308 recurse here. */
6309 return GET_NEXT_DISPLAY_ELEMENT (it);
6310 }
6311
6312 if (it->current.overlay_string_index >= 0)
6313 {
6314 /* Get the next character from an overlay string. In overlay
6315 strings, There is no field width or padding with spaces to
6316 do. */
6317 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6318 {
6319 it->what = IT_EOB;
6320 return 0;
6321 }
6322 else if (STRING_MULTIBYTE (it->string))
6323 {
6324 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6325 const unsigned char *s = (SDATA (it->string)
6326 + IT_STRING_BYTEPOS (*it));
6327 it->c = string_char_and_length (s, remaining, &it->len);
6328 }
6329 else
6330 {
6331 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6332 it->len = 1;
6333 }
6334 }
6335 else
6336 {
6337 /* Get the next character from a Lisp string that is not an
6338 overlay string. Such strings come from the mode line, for
6339 example. We may have to pad with spaces, or truncate the
6340 string. See also next_element_from_c_string. */
6341 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6342 {
6343 it->what = IT_EOB;
6344 return 0;
6345 }
6346 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6347 {
6348 /* Pad with spaces. */
6349 it->c = ' ', it->len = 1;
6350 CHARPOS (position) = BYTEPOS (position) = -1;
6351 }
6352 else if (STRING_MULTIBYTE (it->string))
6353 {
6354 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6355 const unsigned char *s = (SDATA (it->string)
6356 + IT_STRING_BYTEPOS (*it));
6357 it->c = string_char_and_length (s, maxlen, &it->len);
6358 }
6359 else
6360 {
6361 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6362 it->len = 1;
6363 }
6364 }
6365
6366 /* Record what we have and where it came from. */
6367 it->what = IT_CHARACTER;
6368 it->object = it->string;
6369 it->position = position;
6370 return 1;
6371 }
6372
6373
6374 /* Load IT with next display element from C string IT->s.
6375 IT->string_nchars is the maximum number of characters to return
6376 from the string. IT->end_charpos may be greater than
6377 IT->string_nchars when this function is called, in which case we
6378 may have to return padding spaces. Value is zero if end of string
6379 reached, including padding spaces. */
6380
6381 static int
6382 next_element_from_c_string (it)
6383 struct it *it;
6384 {
6385 int success_p = 1;
6386
6387 xassert (it->s);
6388 it->what = IT_CHARACTER;
6389 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6390 it->object = Qnil;
6391
6392 /* IT's position can be greater IT->string_nchars in case a field
6393 width or precision has been specified when the iterator was
6394 initialized. */
6395 if (IT_CHARPOS (*it) >= it->end_charpos)
6396 {
6397 /* End of the game. */
6398 it->what = IT_EOB;
6399 success_p = 0;
6400 }
6401 else if (IT_CHARPOS (*it) >= it->string_nchars)
6402 {
6403 /* Pad with spaces. */
6404 it->c = ' ', it->len = 1;
6405 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6406 }
6407 else if (it->multibyte_p)
6408 {
6409 /* Implementation note: The calls to strlen apparently aren't a
6410 performance problem because there is no noticeable performance
6411 difference between Emacs running in unibyte or multibyte mode. */
6412 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6413 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6414 maxlen, &it->len);
6415 }
6416 else
6417 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6418
6419 return success_p;
6420 }
6421
6422
6423 /* Set up IT to return characters from an ellipsis, if appropriate.
6424 The definition of the ellipsis glyphs may come from a display table
6425 entry. This function Fills IT with the first glyph from the
6426 ellipsis if an ellipsis is to be displayed. */
6427
6428 static int
6429 next_element_from_ellipsis (it)
6430 struct it *it;
6431 {
6432 if (it->selective_display_ellipsis_p)
6433 setup_for_ellipsis (it, it->len);
6434 else
6435 {
6436 /* The face at the current position may be different from the
6437 face we find after the invisible text. Remember what it
6438 was in IT->saved_face_id, and signal that it's there by
6439 setting face_before_selective_p. */
6440 it->saved_face_id = it->face_id;
6441 it->method = GET_FROM_BUFFER;
6442 it->object = it->w->buffer;
6443 reseat_at_next_visible_line_start (it, 1);
6444 it->face_before_selective_p = 1;
6445 }
6446
6447 return GET_NEXT_DISPLAY_ELEMENT (it);
6448 }
6449
6450
6451 /* Deliver an image display element. The iterator IT is already
6452 filled with image information (done in handle_display_prop). Value
6453 is always 1. */
6454
6455
6456 static int
6457 next_element_from_image (it)
6458 struct it *it;
6459 {
6460 it->what = IT_IMAGE;
6461 return 1;
6462 }
6463
6464
6465 /* Fill iterator IT with next display element from a stretch glyph
6466 property. IT->object is the value of the text property. Value is
6467 always 1. */
6468
6469 static int
6470 next_element_from_stretch (it)
6471 struct it *it;
6472 {
6473 it->what = IT_STRETCH;
6474 return 1;
6475 }
6476
6477
6478 /* Load IT with the next display element from current_buffer. Value
6479 is zero if end of buffer reached. IT->stop_charpos is the next
6480 position at which to stop and check for text properties or buffer
6481 end. */
6482
6483 static int
6484 next_element_from_buffer (it)
6485 struct it *it;
6486 {
6487 int success_p = 1;
6488
6489 /* Check this assumption, otherwise, we would never enter the
6490 if-statement, below. */
6491 xassert (IT_CHARPOS (*it) >= BEGV
6492 && IT_CHARPOS (*it) <= it->stop_charpos);
6493
6494 if (IT_CHARPOS (*it) >= it->stop_charpos)
6495 {
6496 if (IT_CHARPOS (*it) >= it->end_charpos)
6497 {
6498 int overlay_strings_follow_p;
6499
6500 /* End of the game, except when overlay strings follow that
6501 haven't been returned yet. */
6502 if (it->overlay_strings_at_end_processed_p)
6503 overlay_strings_follow_p = 0;
6504 else
6505 {
6506 it->overlay_strings_at_end_processed_p = 1;
6507 overlay_strings_follow_p = get_overlay_strings (it, 0);
6508 }
6509
6510 if (overlay_strings_follow_p)
6511 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6512 else
6513 {
6514 it->what = IT_EOB;
6515 it->position = it->current.pos;
6516 success_p = 0;
6517 }
6518 }
6519 else
6520 {
6521 handle_stop (it);
6522 return GET_NEXT_DISPLAY_ELEMENT (it);
6523 }
6524 }
6525 else
6526 {
6527 /* No face changes, overlays etc. in sight, so just return a
6528 character from current_buffer. */
6529 unsigned char *p;
6530
6531 /* Maybe run the redisplay end trigger hook. Performance note:
6532 This doesn't seem to cost measurable time. */
6533 if (it->redisplay_end_trigger_charpos
6534 && it->glyph_row
6535 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6536 run_redisplay_end_trigger_hook (it);
6537
6538 /* Get the next character, maybe multibyte. */
6539 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6540 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6541 {
6542 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6543 - IT_BYTEPOS (*it));
6544 it->c = string_char_and_length (p, maxlen, &it->len);
6545 }
6546 else
6547 it->c = *p, it->len = 1;
6548
6549 /* Record what we have and where it came from. */
6550 it->what = IT_CHARACTER;
6551 it->object = it->w->buffer;
6552 it->position = it->current.pos;
6553
6554 /* Normally we return the character found above, except when we
6555 really want to return an ellipsis for selective display. */
6556 if (it->selective)
6557 {
6558 if (it->c == '\n')
6559 {
6560 /* A value of selective > 0 means hide lines indented more
6561 than that number of columns. */
6562 if (it->selective > 0
6563 && IT_CHARPOS (*it) + 1 < ZV
6564 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6565 IT_BYTEPOS (*it) + 1,
6566 (double) it->selective)) /* iftc */
6567 {
6568 success_p = next_element_from_ellipsis (it);
6569 it->dpvec_char_len = -1;
6570 }
6571 }
6572 else if (it->c == '\r' && it->selective == -1)
6573 {
6574 /* A value of selective == -1 means that everything from the
6575 CR to the end of the line is invisible, with maybe an
6576 ellipsis displayed for it. */
6577 success_p = next_element_from_ellipsis (it);
6578 it->dpvec_char_len = -1;
6579 }
6580 }
6581 }
6582
6583 /* Value is zero if end of buffer reached. */
6584 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6585 return success_p;
6586 }
6587
6588
6589 /* Run the redisplay end trigger hook for IT. */
6590
6591 static void
6592 run_redisplay_end_trigger_hook (it)
6593 struct it *it;
6594 {
6595 Lisp_Object args[3];
6596
6597 /* IT->glyph_row should be non-null, i.e. we should be actually
6598 displaying something, or otherwise we should not run the hook. */
6599 xassert (it->glyph_row);
6600
6601 /* Set up hook arguments. */
6602 args[0] = Qredisplay_end_trigger_functions;
6603 args[1] = it->window;
6604 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6605 it->redisplay_end_trigger_charpos = 0;
6606
6607 /* Since we are *trying* to run these functions, don't try to run
6608 them again, even if they get an error. */
6609 it->w->redisplay_end_trigger = Qnil;
6610 Frun_hook_with_args (3, args);
6611
6612 /* Notice if it changed the face of the character we are on. */
6613 handle_face_prop (it);
6614 }
6615
6616
6617 /* Deliver a composition display element. The iterator IT is already
6618 filled with composition information (done in
6619 handle_composition_prop). Value is always 1. */
6620
6621 static int
6622 next_element_from_composition (it)
6623 struct it *it;
6624 {
6625 it->what = IT_COMPOSITION;
6626 it->position = (STRINGP (it->string)
6627 ? it->current.string_pos
6628 : it->current.pos);
6629 if (STRINGP (it->string))
6630 it->object = it->string;
6631 else
6632 it->object = it->w->buffer;
6633 return 1;
6634 }
6635
6636
6637 \f
6638 /***********************************************************************
6639 Moving an iterator without producing glyphs
6640 ***********************************************************************/
6641
6642 /* Check if iterator is at a position corresponding to a valid buffer
6643 position after some move_it_ call. */
6644
6645 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6646 ((it)->method == GET_FROM_STRING \
6647 ? IT_STRING_CHARPOS (*it) == 0 \
6648 : 1)
6649
6650
6651 /* Move iterator IT to a specified buffer or X position within one
6652 line on the display without producing glyphs.
6653
6654 OP should be a bit mask including some or all of these bits:
6655 MOVE_TO_X: Stop on reaching x-position TO_X.
6656 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6657 Regardless of OP's value, stop in reaching the end of the display line.
6658
6659 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6660 This means, in particular, that TO_X includes window's horizontal
6661 scroll amount.
6662
6663 The return value has several possible values that
6664 say what condition caused the scan to stop:
6665
6666 MOVE_POS_MATCH_OR_ZV
6667 - when TO_POS or ZV was reached.
6668
6669 MOVE_X_REACHED
6670 -when TO_X was reached before TO_POS or ZV were reached.
6671
6672 MOVE_LINE_CONTINUED
6673 - when we reached the end of the display area and the line must
6674 be continued.
6675
6676 MOVE_LINE_TRUNCATED
6677 - when we reached the end of the display area and the line is
6678 truncated.
6679
6680 MOVE_NEWLINE_OR_CR
6681 - when we stopped at a line end, i.e. a newline or a CR and selective
6682 display is on. */
6683
6684 static enum move_it_result
6685 move_it_in_display_line_to (struct it *it,
6686 EMACS_INT to_charpos, int to_x,
6687 enum move_operation_enum op)
6688 {
6689 enum move_it_result result = MOVE_UNDEFINED;
6690 struct glyph_row *saved_glyph_row;
6691 struct it wrap_it, atpos_it, atx_it;
6692 int may_wrap = 0;
6693
6694 /* Don't produce glyphs in produce_glyphs. */
6695 saved_glyph_row = it->glyph_row;
6696 it->glyph_row = NULL;
6697
6698 /* Use wrap_it to save a copy of IT wherever a word wrap could
6699 occur. Use atpos_it to save a copy of IT at the desired buffer
6700 position, if found, so that we can scan ahead and check if the
6701 word later overshoots the window edge. Use atx_it similarly, for
6702 pixel positions. */
6703 wrap_it.sp = -1;
6704 atpos_it.sp = -1;
6705 atx_it.sp = -1;
6706
6707 #define BUFFER_POS_REACHED_P() \
6708 ((op & MOVE_TO_POS) != 0 \
6709 && BUFFERP (it->object) \
6710 && IT_CHARPOS (*it) >= to_charpos \
6711 && (it->method == GET_FROM_BUFFER \
6712 || (it->method == GET_FROM_DISPLAY_VECTOR \
6713 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6714
6715 /* If there's a line-/wrap-prefix, handle it. */
6716 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6717 && it->current_y < it->last_visible_y)
6718 handle_line_prefix (it);
6719
6720 while (1)
6721 {
6722 int x, i, ascent = 0, descent = 0;
6723
6724 /* Utility macro to reset an iterator with x, ascent, and descent. */
6725 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6726 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6727 (IT)->max_descent = descent)
6728
6729 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6730 glyph). */
6731 if ((op & MOVE_TO_POS) != 0
6732 && BUFFERP (it->object)
6733 && it->method == GET_FROM_BUFFER
6734 && IT_CHARPOS (*it) > to_charpos)
6735 {
6736 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6737 {
6738 result = MOVE_POS_MATCH_OR_ZV;
6739 break;
6740 }
6741 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6742 /* If wrap_it is valid, the current position might be in a
6743 word that is wrapped. So, save the iterator in
6744 atpos_it and continue to see if wrapping happens. */
6745 atpos_it = *it;
6746 }
6747
6748 /* Stop when ZV reached.
6749 We used to stop here when TO_CHARPOS reached as well, but that is
6750 too soon if this glyph does not fit on this line. So we handle it
6751 explicitly below. */
6752 if (!get_next_display_element (it))
6753 {
6754 result = MOVE_POS_MATCH_OR_ZV;
6755 break;
6756 }
6757
6758 if (it->line_wrap == TRUNCATE)
6759 {
6760 if (BUFFER_POS_REACHED_P ())
6761 {
6762 result = MOVE_POS_MATCH_OR_ZV;
6763 break;
6764 }
6765 }
6766 else
6767 {
6768 if (it->line_wrap == WORD_WRAP)
6769 {
6770 if (IT_DISPLAYING_WHITESPACE (it))
6771 may_wrap = 1;
6772 else if (may_wrap)
6773 {
6774 /* We have reached a glyph that follows one or more
6775 whitespace characters. If the position is
6776 already found, we are done. */
6777 if (atpos_it.sp >= 0)
6778 {
6779 *it = atpos_it;
6780 result = MOVE_POS_MATCH_OR_ZV;
6781 goto done;
6782 }
6783 if (atx_it.sp >= 0)
6784 {
6785 *it = atx_it;
6786 result = MOVE_X_REACHED;
6787 goto done;
6788 }
6789 /* Otherwise, we can wrap here. */
6790 wrap_it = *it;
6791 may_wrap = 0;
6792 }
6793 }
6794 }
6795
6796 /* Remember the line height for the current line, in case
6797 the next element doesn't fit on the line. */
6798 ascent = it->max_ascent;
6799 descent = it->max_descent;
6800
6801 /* The call to produce_glyphs will get the metrics of the
6802 display element IT is loaded with. Record the x-position
6803 before this display element, in case it doesn't fit on the
6804 line. */
6805 x = it->current_x;
6806
6807 PRODUCE_GLYPHS (it);
6808
6809 if (it->area != TEXT_AREA)
6810 {
6811 set_iterator_to_next (it, 1);
6812 continue;
6813 }
6814
6815 /* The number of glyphs we get back in IT->nglyphs will normally
6816 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6817 character on a terminal frame, or (iii) a line end. For the
6818 second case, IT->nglyphs - 1 padding glyphs will be present
6819 (on X frames, there is only one glyph produced for a
6820 composite character.
6821
6822 The behavior implemented below means, for continuation lines,
6823 that as many spaces of a TAB as fit on the current line are
6824 displayed there. For terminal frames, as many glyphs of a
6825 multi-glyph character are displayed in the current line, too.
6826 This is what the old redisplay code did, and we keep it that
6827 way. Under X, the whole shape of a complex character must
6828 fit on the line or it will be completely displayed in the
6829 next line.
6830
6831 Note that both for tabs and padding glyphs, all glyphs have
6832 the same width. */
6833 if (it->nglyphs)
6834 {
6835 /* More than one glyph or glyph doesn't fit on line. All
6836 glyphs have the same width. */
6837 int single_glyph_width = it->pixel_width / it->nglyphs;
6838 int new_x;
6839 int x_before_this_char = x;
6840 int hpos_before_this_char = it->hpos;
6841
6842 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6843 {
6844 new_x = x + single_glyph_width;
6845
6846 /* We want to leave anything reaching TO_X to the caller. */
6847 if ((op & MOVE_TO_X) && new_x > to_x)
6848 {
6849 if (BUFFER_POS_REACHED_P ())
6850 {
6851 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6852 goto buffer_pos_reached;
6853 if (atpos_it.sp < 0)
6854 {
6855 atpos_it = *it;
6856 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6857 }
6858 }
6859 else
6860 {
6861 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6862 {
6863 it->current_x = x;
6864 result = MOVE_X_REACHED;
6865 break;
6866 }
6867 if (atx_it.sp < 0)
6868 {
6869 atx_it = *it;
6870 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6871 }
6872 }
6873 }
6874
6875 if (/* Lines are continued. */
6876 it->line_wrap != TRUNCATE
6877 && (/* And glyph doesn't fit on the line. */
6878 new_x > it->last_visible_x
6879 /* Or it fits exactly and we're on a window
6880 system frame. */
6881 || (new_x == it->last_visible_x
6882 && FRAME_WINDOW_P (it->f))))
6883 {
6884 if (/* IT->hpos == 0 means the very first glyph
6885 doesn't fit on the line, e.g. a wide image. */
6886 it->hpos == 0
6887 || (new_x == it->last_visible_x
6888 && FRAME_WINDOW_P (it->f)))
6889 {
6890 ++it->hpos;
6891 it->current_x = new_x;
6892
6893 /* The character's last glyph just barely fits
6894 in this row. */
6895 if (i == it->nglyphs - 1)
6896 {
6897 /* If this is the destination position,
6898 return a position *before* it in this row,
6899 now that we know it fits in this row. */
6900 if (BUFFER_POS_REACHED_P ())
6901 {
6902 if (it->line_wrap != WORD_WRAP
6903 || wrap_it.sp < 0)
6904 {
6905 it->hpos = hpos_before_this_char;
6906 it->current_x = x_before_this_char;
6907 result = MOVE_POS_MATCH_OR_ZV;
6908 break;
6909 }
6910 if (it->line_wrap == WORD_WRAP
6911 && atpos_it.sp < 0)
6912 {
6913 atpos_it = *it;
6914 atpos_it.current_x = x_before_this_char;
6915 atpos_it.hpos = hpos_before_this_char;
6916 }
6917 }
6918
6919 set_iterator_to_next (it, 1);
6920 #ifdef HAVE_WINDOW_SYSTEM
6921 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6922 {
6923 if (!get_next_display_element (it))
6924 {
6925 result = MOVE_POS_MATCH_OR_ZV;
6926 break;
6927 }
6928 if (BUFFER_POS_REACHED_P ())
6929 {
6930 if (ITERATOR_AT_END_OF_LINE_P (it))
6931 result = MOVE_POS_MATCH_OR_ZV;
6932 else
6933 result = MOVE_LINE_CONTINUED;
6934 break;
6935 }
6936 if (ITERATOR_AT_END_OF_LINE_P (it))
6937 {
6938 result = MOVE_NEWLINE_OR_CR;
6939 break;
6940 }
6941 }
6942 #endif /* HAVE_WINDOW_SYSTEM */
6943 }
6944 }
6945 else
6946 IT_RESET_X_ASCENT_DESCENT (it);
6947
6948 if (wrap_it.sp >= 0)
6949 {
6950 *it = wrap_it;
6951 atpos_it.sp = -1;
6952 atx_it.sp = -1;
6953 }
6954
6955 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6956 IT_CHARPOS (*it)));
6957 result = MOVE_LINE_CONTINUED;
6958 break;
6959 }
6960
6961 if (BUFFER_POS_REACHED_P ())
6962 {
6963 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6964 goto buffer_pos_reached;
6965 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6966 {
6967 atpos_it = *it;
6968 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6969 }
6970 }
6971
6972 if (new_x > it->first_visible_x)
6973 {
6974 /* Glyph is visible. Increment number of glyphs that
6975 would be displayed. */
6976 ++it->hpos;
6977 }
6978 }
6979
6980 if (result != MOVE_UNDEFINED)
6981 break;
6982 }
6983 else if (BUFFER_POS_REACHED_P ())
6984 {
6985 buffer_pos_reached:
6986 IT_RESET_X_ASCENT_DESCENT (it);
6987 result = MOVE_POS_MATCH_OR_ZV;
6988 break;
6989 }
6990 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6991 {
6992 /* Stop when TO_X specified and reached. This check is
6993 necessary here because of lines consisting of a line end,
6994 only. The line end will not produce any glyphs and we
6995 would never get MOVE_X_REACHED. */
6996 xassert (it->nglyphs == 0);
6997 result = MOVE_X_REACHED;
6998 break;
6999 }
7000
7001 /* Is this a line end? If yes, we're done. */
7002 if (ITERATOR_AT_END_OF_LINE_P (it))
7003 {
7004 result = MOVE_NEWLINE_OR_CR;
7005 break;
7006 }
7007
7008 /* The current display element has been consumed. Advance
7009 to the next. */
7010 set_iterator_to_next (it, 1);
7011
7012 /* Stop if lines are truncated and IT's current x-position is
7013 past the right edge of the window now. */
7014 if (it->line_wrap == TRUNCATE
7015 && it->current_x >= it->last_visible_x)
7016 {
7017 #ifdef HAVE_WINDOW_SYSTEM
7018 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7019 {
7020 if (!get_next_display_element (it)
7021 || BUFFER_POS_REACHED_P ())
7022 {
7023 result = MOVE_POS_MATCH_OR_ZV;
7024 break;
7025 }
7026 if (ITERATOR_AT_END_OF_LINE_P (it))
7027 {
7028 result = MOVE_NEWLINE_OR_CR;
7029 break;
7030 }
7031 }
7032 #endif /* HAVE_WINDOW_SYSTEM */
7033 result = MOVE_LINE_TRUNCATED;
7034 break;
7035 }
7036 #undef IT_RESET_X_ASCENT_DESCENT
7037 }
7038
7039 #undef BUFFER_POS_REACHED_P
7040
7041 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7042 restore the saved iterator. */
7043 if (atpos_it.sp >= 0)
7044 *it = atpos_it;
7045 else if (atx_it.sp >= 0)
7046 *it = atx_it;
7047
7048 done:
7049
7050 /* Restore the iterator settings altered at the beginning of this
7051 function. */
7052 it->glyph_row = saved_glyph_row;
7053 return result;
7054 }
7055
7056 /* For external use. */
7057 void
7058 move_it_in_display_line (struct it *it,
7059 EMACS_INT to_charpos, int to_x,
7060 enum move_operation_enum op)
7061 {
7062 if (it->line_wrap == WORD_WRAP
7063 && (op & MOVE_TO_X))
7064 {
7065 struct it save_it = *it;
7066 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7067 /* When word-wrap is on, TO_X may lie past the end
7068 of a wrapped line. Then it->current is the
7069 character on the next line, so backtrack to the
7070 space before the wrap point. */
7071 if (skip == MOVE_LINE_CONTINUED)
7072 {
7073 int prev_x = max (it->current_x - 1, 0);
7074 *it = save_it;
7075 move_it_in_display_line_to
7076 (it, -1, prev_x, MOVE_TO_X);
7077 }
7078 }
7079 else
7080 move_it_in_display_line_to (it, to_charpos, to_x, op);
7081 }
7082
7083
7084 /* Move IT forward until it satisfies one or more of the criteria in
7085 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7086
7087 OP is a bit-mask that specifies where to stop, and in particular,
7088 which of those four position arguments makes a difference. See the
7089 description of enum move_operation_enum.
7090
7091 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7092 screen line, this function will set IT to the next position >
7093 TO_CHARPOS. */
7094
7095 void
7096 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7097 struct it *it;
7098 int to_charpos, to_x, to_y, to_vpos;
7099 int op;
7100 {
7101 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7102 int line_height;
7103 int reached = 0;
7104
7105 for (;;)
7106 {
7107 if (op & MOVE_TO_VPOS)
7108 {
7109 /* If no TO_CHARPOS and no TO_X specified, stop at the
7110 start of the line TO_VPOS. */
7111 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7112 {
7113 if (it->vpos == to_vpos)
7114 {
7115 reached = 1;
7116 break;
7117 }
7118 else
7119 skip = move_it_in_display_line_to (it, -1, -1, 0);
7120 }
7121 else
7122 {
7123 /* TO_VPOS >= 0 means stop at TO_X in the line at
7124 TO_VPOS, or at TO_POS, whichever comes first. */
7125 if (it->vpos == to_vpos)
7126 {
7127 reached = 2;
7128 break;
7129 }
7130
7131 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7132
7133 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7134 {
7135 reached = 3;
7136 break;
7137 }
7138 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7139 {
7140 /* We have reached TO_X but not in the line we want. */
7141 skip = move_it_in_display_line_to (it, to_charpos,
7142 -1, MOVE_TO_POS);
7143 if (skip == MOVE_POS_MATCH_OR_ZV)
7144 {
7145 reached = 4;
7146 break;
7147 }
7148 }
7149 }
7150 }
7151 else if (op & MOVE_TO_Y)
7152 {
7153 struct it it_backup;
7154
7155 if (it->line_wrap == WORD_WRAP)
7156 it_backup = *it;
7157
7158 /* TO_Y specified means stop at TO_X in the line containing
7159 TO_Y---or at TO_CHARPOS if this is reached first. The
7160 problem is that we can't really tell whether the line
7161 contains TO_Y before we have completely scanned it, and
7162 this may skip past TO_X. What we do is to first scan to
7163 TO_X.
7164
7165 If TO_X is not specified, use a TO_X of zero. The reason
7166 is to make the outcome of this function more predictable.
7167 If we didn't use TO_X == 0, we would stop at the end of
7168 the line which is probably not what a caller would expect
7169 to happen. */
7170 skip = move_it_in_display_line_to
7171 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7172 (MOVE_TO_X | (op & MOVE_TO_POS)));
7173
7174 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7175 if (skip == MOVE_POS_MATCH_OR_ZV)
7176 reached = 5;
7177 else if (skip == MOVE_X_REACHED)
7178 {
7179 /* If TO_X was reached, we want to know whether TO_Y is
7180 in the line. We know this is the case if the already
7181 scanned glyphs make the line tall enough. Otherwise,
7182 we must check by scanning the rest of the line. */
7183 line_height = it->max_ascent + it->max_descent;
7184 if (to_y >= it->current_y
7185 && to_y < it->current_y + line_height)
7186 {
7187 reached = 6;
7188 break;
7189 }
7190 it_backup = *it;
7191 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7192 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7193 op & MOVE_TO_POS);
7194 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7195 line_height = it->max_ascent + it->max_descent;
7196 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7197
7198 if (to_y >= it->current_y
7199 && to_y < it->current_y + line_height)
7200 {
7201 /* If TO_Y is in this line and TO_X was reached
7202 above, we scanned too far. We have to restore
7203 IT's settings to the ones before skipping. */
7204 *it = it_backup;
7205 reached = 6;
7206 }
7207 else
7208 {
7209 skip = skip2;
7210 if (skip == MOVE_POS_MATCH_OR_ZV)
7211 reached = 7;
7212 }
7213 }
7214 else
7215 {
7216 /* Check whether TO_Y is in this line. */
7217 line_height = it->max_ascent + it->max_descent;
7218 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7219
7220 if (to_y >= it->current_y
7221 && to_y < it->current_y + line_height)
7222 {
7223 /* When word-wrap is on, TO_X may lie past the end
7224 of a wrapped line. Then it->current is the
7225 character on the next line, so backtrack to the
7226 space before the wrap point. */
7227 if (skip == MOVE_LINE_CONTINUED
7228 && it->line_wrap == WORD_WRAP)
7229 {
7230 int prev_x = max (it->current_x - 1, 0);
7231 *it = it_backup;
7232 skip = move_it_in_display_line_to
7233 (it, -1, prev_x, MOVE_TO_X);
7234 }
7235 reached = 6;
7236 }
7237 }
7238
7239 if (reached)
7240 break;
7241 }
7242 else if (BUFFERP (it->object)
7243 && it->method == GET_FROM_BUFFER
7244 && IT_CHARPOS (*it) >= to_charpos)
7245 skip = MOVE_POS_MATCH_OR_ZV;
7246 else
7247 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7248
7249 switch (skip)
7250 {
7251 case MOVE_POS_MATCH_OR_ZV:
7252 reached = 8;
7253 goto out;
7254
7255 case MOVE_NEWLINE_OR_CR:
7256 set_iterator_to_next (it, 1);
7257 it->continuation_lines_width = 0;
7258 break;
7259
7260 case MOVE_LINE_TRUNCATED:
7261 it->continuation_lines_width = 0;
7262 reseat_at_next_visible_line_start (it, 0);
7263 if ((op & MOVE_TO_POS) != 0
7264 && IT_CHARPOS (*it) > to_charpos)
7265 {
7266 reached = 9;
7267 goto out;
7268 }
7269 break;
7270
7271 case MOVE_LINE_CONTINUED:
7272 /* For continued lines ending in a tab, some of the glyphs
7273 associated with the tab are displayed on the current
7274 line. Since it->current_x does not include these glyphs,
7275 we use it->last_visible_x instead. */
7276 it->continuation_lines_width +=
7277 (it->c == '\t') ? it->last_visible_x : it->current_x;
7278 break;
7279
7280 default:
7281 abort ();
7282 }
7283
7284 /* Reset/increment for the next run. */
7285 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7286 it->current_x = it->hpos = 0;
7287 it->current_y += it->max_ascent + it->max_descent;
7288 ++it->vpos;
7289 last_height = it->max_ascent + it->max_descent;
7290 last_max_ascent = it->max_ascent;
7291 it->max_ascent = it->max_descent = 0;
7292 }
7293
7294 out:
7295
7296 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7297 }
7298
7299
7300 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7301
7302 If DY > 0, move IT backward at least that many pixels. DY = 0
7303 means move IT backward to the preceding line start or BEGV. This
7304 function may move over more than DY pixels if IT->current_y - DY
7305 ends up in the middle of a line; in this case IT->current_y will be
7306 set to the top of the line moved to. */
7307
7308 void
7309 move_it_vertically_backward (it, dy)
7310 struct it *it;
7311 int dy;
7312 {
7313 int nlines, h;
7314 struct it it2, it3;
7315 int start_pos;
7316
7317 move_further_back:
7318 xassert (dy >= 0);
7319
7320 start_pos = IT_CHARPOS (*it);
7321
7322 /* Estimate how many newlines we must move back. */
7323 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7324
7325 /* Set the iterator's position that many lines back. */
7326 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7327 back_to_previous_visible_line_start (it);
7328
7329 /* Reseat the iterator here. When moving backward, we don't want
7330 reseat to skip forward over invisible text, set up the iterator
7331 to deliver from overlay strings at the new position etc. So,
7332 use reseat_1 here. */
7333 reseat_1 (it, it->current.pos, 1);
7334
7335 /* We are now surely at a line start. */
7336 it->current_x = it->hpos = 0;
7337 it->continuation_lines_width = 0;
7338
7339 /* Move forward and see what y-distance we moved. First move to the
7340 start of the next line so that we get its height. We need this
7341 height to be able to tell whether we reached the specified
7342 y-distance. */
7343 it2 = *it;
7344 it2.max_ascent = it2.max_descent = 0;
7345 do
7346 {
7347 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7348 MOVE_TO_POS | MOVE_TO_VPOS);
7349 }
7350 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7351 xassert (IT_CHARPOS (*it) >= BEGV);
7352 it3 = it2;
7353
7354 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7355 xassert (IT_CHARPOS (*it) >= BEGV);
7356 /* H is the actual vertical distance from the position in *IT
7357 and the starting position. */
7358 h = it2.current_y - it->current_y;
7359 /* NLINES is the distance in number of lines. */
7360 nlines = it2.vpos - it->vpos;
7361
7362 /* Correct IT's y and vpos position
7363 so that they are relative to the starting point. */
7364 it->vpos -= nlines;
7365 it->current_y -= h;
7366
7367 if (dy == 0)
7368 {
7369 /* DY == 0 means move to the start of the screen line. The
7370 value of nlines is > 0 if continuation lines were involved. */
7371 if (nlines > 0)
7372 move_it_by_lines (it, nlines, 1);
7373 #if 0
7374 /* I think this assert is bogus if buffer contains
7375 invisible text or images. KFS. */
7376 xassert (IT_CHARPOS (*it) <= start_pos);
7377 #endif
7378 }
7379 else
7380 {
7381 /* The y-position we try to reach, relative to *IT.
7382 Note that H has been subtracted in front of the if-statement. */
7383 int target_y = it->current_y + h - dy;
7384 int y0 = it3.current_y;
7385 int y1 = line_bottom_y (&it3);
7386 int line_height = y1 - y0;
7387
7388 /* If we did not reach target_y, try to move further backward if
7389 we can. If we moved too far backward, try to move forward. */
7390 if (target_y < it->current_y
7391 /* This is heuristic. In a window that's 3 lines high, with
7392 a line height of 13 pixels each, recentering with point
7393 on the bottom line will try to move -39/2 = 19 pixels
7394 backward. Try to avoid moving into the first line. */
7395 && (it->current_y - target_y
7396 > min (window_box_height (it->w), line_height * 2 / 3))
7397 && IT_CHARPOS (*it) > BEGV)
7398 {
7399 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7400 target_y - it->current_y));
7401 dy = it->current_y - target_y;
7402 goto move_further_back;
7403 }
7404 else if (target_y >= it->current_y + line_height
7405 && IT_CHARPOS (*it) < ZV)
7406 {
7407 /* Should move forward by at least one line, maybe more.
7408
7409 Note: Calling move_it_by_lines can be expensive on
7410 terminal frames, where compute_motion is used (via
7411 vmotion) to do the job, when there are very long lines
7412 and truncate-lines is nil. That's the reason for
7413 treating terminal frames specially here. */
7414
7415 if (!FRAME_WINDOW_P (it->f))
7416 move_it_vertically (it, target_y - (it->current_y + line_height));
7417 else
7418 {
7419 do
7420 {
7421 move_it_by_lines (it, 1, 1);
7422 }
7423 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7424 }
7425
7426 #if 0
7427 /* I think this assert is bogus if buffer contains
7428 invisible text or images. KFS. */
7429 xassert (IT_CHARPOS (*it) >= BEGV);
7430 #endif
7431 }
7432 }
7433 }
7434
7435
7436 /* Move IT by a specified amount of pixel lines DY. DY negative means
7437 move backwards. DY = 0 means move to start of screen line. At the
7438 end, IT will be on the start of a screen line. */
7439
7440 void
7441 move_it_vertically (it, dy)
7442 struct it *it;
7443 int dy;
7444 {
7445 if (dy <= 0)
7446 move_it_vertically_backward (it, -dy);
7447 else
7448 {
7449 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7450 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7451 MOVE_TO_POS | MOVE_TO_Y);
7452 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7453
7454 /* If buffer ends in ZV without a newline, move to the start of
7455 the line to satisfy the post-condition. */
7456 if (IT_CHARPOS (*it) == ZV
7457 && ZV > BEGV
7458 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7459 move_it_by_lines (it, 0, 0);
7460 }
7461 }
7462
7463
7464 /* Move iterator IT past the end of the text line it is in. */
7465
7466 void
7467 move_it_past_eol (it)
7468 struct it *it;
7469 {
7470 enum move_it_result rc;
7471
7472 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7473 if (rc == MOVE_NEWLINE_OR_CR)
7474 set_iterator_to_next (it, 0);
7475 }
7476
7477
7478 #if 0 /* Currently not used. */
7479
7480 /* Return non-zero if some text between buffer positions START_CHARPOS
7481 and END_CHARPOS is invisible. IT->window is the window for text
7482 property lookup. */
7483
7484 static int
7485 invisible_text_between_p (it, start_charpos, end_charpos)
7486 struct it *it;
7487 int start_charpos, end_charpos;
7488 {
7489 Lisp_Object prop, limit;
7490 int invisible_found_p;
7491
7492 xassert (it != NULL && start_charpos <= end_charpos);
7493
7494 /* Is text at START invisible? */
7495 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7496 it->window);
7497 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7498 invisible_found_p = 1;
7499 else
7500 {
7501 limit = Fnext_single_char_property_change (make_number (start_charpos),
7502 Qinvisible, Qnil,
7503 make_number (end_charpos));
7504 invisible_found_p = XFASTINT (limit) < end_charpos;
7505 }
7506
7507 return invisible_found_p;
7508 }
7509
7510 #endif /* 0 */
7511
7512
7513 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7514 negative means move up. DVPOS == 0 means move to the start of the
7515 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7516 NEED_Y_P is zero, IT->current_y will be left unchanged.
7517
7518 Further optimization ideas: If we would know that IT->f doesn't use
7519 a face with proportional font, we could be faster for
7520 truncate-lines nil. */
7521
7522 void
7523 move_it_by_lines (it, dvpos, need_y_p)
7524 struct it *it;
7525 int dvpos, need_y_p;
7526 {
7527 struct position pos;
7528
7529 /* The commented-out optimization uses vmotion on terminals. This
7530 gives bad results, because elements like it->what, on which
7531 callers such as pos_visible_p rely, aren't updated. */
7532 /* if (!FRAME_WINDOW_P (it->f))
7533 {
7534 struct text_pos textpos;
7535
7536 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7537 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7538 reseat (it, textpos, 1);
7539 it->vpos += pos.vpos;
7540 it->current_y += pos.vpos;
7541 }
7542 else */
7543
7544 if (dvpos == 0)
7545 {
7546 /* DVPOS == 0 means move to the start of the screen line. */
7547 move_it_vertically_backward (it, 0);
7548 xassert (it->current_x == 0 && it->hpos == 0);
7549 /* Let next call to line_bottom_y calculate real line height */
7550 last_height = 0;
7551 }
7552 else if (dvpos > 0)
7553 {
7554 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7555 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7556 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7557 }
7558 else
7559 {
7560 struct it it2;
7561 int start_charpos, i;
7562
7563 /* Start at the beginning of the screen line containing IT's
7564 position. This may actually move vertically backwards,
7565 in case of overlays, so adjust dvpos accordingly. */
7566 dvpos += it->vpos;
7567 move_it_vertically_backward (it, 0);
7568 dvpos -= it->vpos;
7569
7570 /* Go back -DVPOS visible lines and reseat the iterator there. */
7571 start_charpos = IT_CHARPOS (*it);
7572 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7573 back_to_previous_visible_line_start (it);
7574 reseat (it, it->current.pos, 1);
7575
7576 /* Move further back if we end up in a string or an image. */
7577 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7578 {
7579 /* First try to move to start of display line. */
7580 dvpos += it->vpos;
7581 move_it_vertically_backward (it, 0);
7582 dvpos -= it->vpos;
7583 if (IT_POS_VALID_AFTER_MOVE_P (it))
7584 break;
7585 /* If start of line is still in string or image,
7586 move further back. */
7587 back_to_previous_visible_line_start (it);
7588 reseat (it, it->current.pos, 1);
7589 dvpos--;
7590 }
7591
7592 it->current_x = it->hpos = 0;
7593
7594 /* Above call may have moved too far if continuation lines
7595 are involved. Scan forward and see if it did. */
7596 it2 = *it;
7597 it2.vpos = it2.current_y = 0;
7598 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7599 it->vpos -= it2.vpos;
7600 it->current_y -= it2.current_y;
7601 it->current_x = it->hpos = 0;
7602
7603 /* If we moved too far back, move IT some lines forward. */
7604 if (it2.vpos > -dvpos)
7605 {
7606 int delta = it2.vpos + dvpos;
7607 it2 = *it;
7608 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7609 /* Move back again if we got too far ahead. */
7610 if (IT_CHARPOS (*it) >= start_charpos)
7611 *it = it2;
7612 }
7613 }
7614 }
7615
7616 /* Return 1 if IT points into the middle of a display vector. */
7617
7618 int
7619 in_display_vector_p (it)
7620 struct it *it;
7621 {
7622 return (it->method == GET_FROM_DISPLAY_VECTOR
7623 && it->current.dpvec_index > 0
7624 && it->dpvec + it->current.dpvec_index != it->dpend);
7625 }
7626
7627 \f
7628 /***********************************************************************
7629 Messages
7630 ***********************************************************************/
7631
7632
7633 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7634 to *Messages*. */
7635
7636 void
7637 add_to_log (format, arg1, arg2)
7638 char *format;
7639 Lisp_Object arg1, arg2;
7640 {
7641 Lisp_Object args[3];
7642 Lisp_Object msg, fmt;
7643 char *buffer;
7644 int len;
7645 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7646 USE_SAFE_ALLOCA;
7647
7648 /* Do nothing if called asynchronously. Inserting text into
7649 a buffer may call after-change-functions and alike and
7650 that would means running Lisp asynchronously. */
7651 if (handling_signal)
7652 return;
7653
7654 fmt = msg = Qnil;
7655 GCPRO4 (fmt, msg, arg1, arg2);
7656
7657 args[0] = fmt = build_string (format);
7658 args[1] = arg1;
7659 args[2] = arg2;
7660 msg = Fformat (3, args);
7661
7662 len = SBYTES (msg) + 1;
7663 SAFE_ALLOCA (buffer, char *, len);
7664 bcopy (SDATA (msg), buffer, len);
7665
7666 message_dolog (buffer, len - 1, 1, 0);
7667 SAFE_FREE ();
7668
7669 UNGCPRO;
7670 }
7671
7672
7673 /* Output a newline in the *Messages* buffer if "needs" one. */
7674
7675 void
7676 message_log_maybe_newline ()
7677 {
7678 if (message_log_need_newline)
7679 message_dolog ("", 0, 1, 0);
7680 }
7681
7682
7683 /* Add a string M of length NBYTES to the message log, optionally
7684 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7685 nonzero, means interpret the contents of M as multibyte. This
7686 function calls low-level routines in order to bypass text property
7687 hooks, etc. which might not be safe to run.
7688
7689 This may GC (insert may run before/after change hooks),
7690 so the buffer M must NOT point to a Lisp string. */
7691
7692 void
7693 message_dolog (m, nbytes, nlflag, multibyte)
7694 const char *m;
7695 int nbytes, nlflag, multibyte;
7696 {
7697 if (!NILP (Vmemory_full))
7698 return;
7699
7700 if (!NILP (Vmessage_log_max))
7701 {
7702 struct buffer *oldbuf;
7703 Lisp_Object oldpoint, oldbegv, oldzv;
7704 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7705 int point_at_end = 0;
7706 int zv_at_end = 0;
7707 Lisp_Object old_deactivate_mark, tem;
7708 struct gcpro gcpro1;
7709
7710 old_deactivate_mark = Vdeactivate_mark;
7711 oldbuf = current_buffer;
7712 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7713 current_buffer->undo_list = Qt;
7714
7715 oldpoint = message_dolog_marker1;
7716 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7717 oldbegv = message_dolog_marker2;
7718 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7719 oldzv = message_dolog_marker3;
7720 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7721 GCPRO1 (old_deactivate_mark);
7722
7723 if (PT == Z)
7724 point_at_end = 1;
7725 if (ZV == Z)
7726 zv_at_end = 1;
7727
7728 BEGV = BEG;
7729 BEGV_BYTE = BEG_BYTE;
7730 ZV = Z;
7731 ZV_BYTE = Z_BYTE;
7732 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7733
7734 /* Insert the string--maybe converting multibyte to single byte
7735 or vice versa, so that all the text fits the buffer. */
7736 if (multibyte
7737 && NILP (current_buffer->enable_multibyte_characters))
7738 {
7739 int i, c, char_bytes;
7740 unsigned char work[1];
7741
7742 /* Convert a multibyte string to single-byte
7743 for the *Message* buffer. */
7744 for (i = 0; i < nbytes; i += char_bytes)
7745 {
7746 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7747 work[0] = (ASCII_CHAR_P (c)
7748 ? c
7749 : multibyte_char_to_unibyte (c, Qnil));
7750 insert_1_both (work, 1, 1, 1, 0, 0);
7751 }
7752 }
7753 else if (! multibyte
7754 && ! NILP (current_buffer->enable_multibyte_characters))
7755 {
7756 int i, c, char_bytes;
7757 unsigned char *msg = (unsigned char *) m;
7758 unsigned char str[MAX_MULTIBYTE_LENGTH];
7759 /* Convert a single-byte string to multibyte
7760 for the *Message* buffer. */
7761 for (i = 0; i < nbytes; i++)
7762 {
7763 c = msg[i];
7764 c = unibyte_char_to_multibyte (c);
7765 char_bytes = CHAR_STRING (c, str);
7766 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7767 }
7768 }
7769 else if (nbytes)
7770 insert_1 (m, nbytes, 1, 0, 0);
7771
7772 if (nlflag)
7773 {
7774 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7775 insert_1 ("\n", 1, 1, 0, 0);
7776
7777 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7778 this_bol = PT;
7779 this_bol_byte = PT_BYTE;
7780
7781 /* See if this line duplicates the previous one.
7782 If so, combine duplicates. */
7783 if (this_bol > BEG)
7784 {
7785 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7786 prev_bol = PT;
7787 prev_bol_byte = PT_BYTE;
7788
7789 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7790 this_bol, this_bol_byte);
7791 if (dup)
7792 {
7793 del_range_both (prev_bol, prev_bol_byte,
7794 this_bol, this_bol_byte, 0);
7795 if (dup > 1)
7796 {
7797 char dupstr[40];
7798 int duplen;
7799
7800 /* If you change this format, don't forget to also
7801 change message_log_check_duplicate. */
7802 sprintf (dupstr, " [%d times]", dup);
7803 duplen = strlen (dupstr);
7804 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7805 insert_1 (dupstr, duplen, 1, 0, 1);
7806 }
7807 }
7808 }
7809
7810 /* If we have more than the desired maximum number of lines
7811 in the *Messages* buffer now, delete the oldest ones.
7812 This is safe because we don't have undo in this buffer. */
7813
7814 if (NATNUMP (Vmessage_log_max))
7815 {
7816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7817 -XFASTINT (Vmessage_log_max) - 1, 0);
7818 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7819 }
7820 }
7821 BEGV = XMARKER (oldbegv)->charpos;
7822 BEGV_BYTE = marker_byte_position (oldbegv);
7823
7824 if (zv_at_end)
7825 {
7826 ZV = Z;
7827 ZV_BYTE = Z_BYTE;
7828 }
7829 else
7830 {
7831 ZV = XMARKER (oldzv)->charpos;
7832 ZV_BYTE = marker_byte_position (oldzv);
7833 }
7834
7835 if (point_at_end)
7836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7837 else
7838 /* We can't do Fgoto_char (oldpoint) because it will run some
7839 Lisp code. */
7840 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7841 XMARKER (oldpoint)->bytepos);
7842
7843 UNGCPRO;
7844 unchain_marker (XMARKER (oldpoint));
7845 unchain_marker (XMARKER (oldbegv));
7846 unchain_marker (XMARKER (oldzv));
7847
7848 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7849 set_buffer_internal (oldbuf);
7850 if (NILP (tem))
7851 windows_or_buffers_changed = old_windows_or_buffers_changed;
7852 message_log_need_newline = !nlflag;
7853 Vdeactivate_mark = old_deactivate_mark;
7854 }
7855 }
7856
7857
7858 /* We are at the end of the buffer after just having inserted a newline.
7859 (Note: We depend on the fact we won't be crossing the gap.)
7860 Check to see if the most recent message looks a lot like the previous one.
7861 Return 0 if different, 1 if the new one should just replace it, or a
7862 value N > 1 if we should also append " [N times]". */
7863
7864 static int
7865 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7866 int prev_bol, this_bol;
7867 int prev_bol_byte, this_bol_byte;
7868 {
7869 int i;
7870 int len = Z_BYTE - 1 - this_bol_byte;
7871 int seen_dots = 0;
7872 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7873 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7874
7875 for (i = 0; i < len; i++)
7876 {
7877 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7878 seen_dots = 1;
7879 if (p1[i] != p2[i])
7880 return seen_dots;
7881 }
7882 p1 += len;
7883 if (*p1 == '\n')
7884 return 2;
7885 if (*p1++ == ' ' && *p1++ == '[')
7886 {
7887 int n = 0;
7888 while (*p1 >= '0' && *p1 <= '9')
7889 n = n * 10 + *p1++ - '0';
7890 if (strncmp (p1, " times]\n", 8) == 0)
7891 return n+1;
7892 }
7893 return 0;
7894 }
7895 \f
7896
7897 /* Display an echo area message M with a specified length of NBYTES
7898 bytes. The string may include null characters. If M is 0, clear
7899 out any existing message, and let the mini-buffer text show
7900 through.
7901
7902 This may GC, so the buffer M must NOT point to a Lisp string. */
7903
7904 void
7905 message2 (m, nbytes, multibyte)
7906 const char *m;
7907 int nbytes;
7908 int multibyte;
7909 {
7910 /* First flush out any partial line written with print. */
7911 message_log_maybe_newline ();
7912 if (m)
7913 message_dolog (m, nbytes, 1, multibyte);
7914 message2_nolog (m, nbytes, multibyte);
7915 }
7916
7917
7918 /* The non-logging counterpart of message2. */
7919
7920 void
7921 message2_nolog (m, nbytes, multibyte)
7922 const char *m;
7923 int nbytes, multibyte;
7924 {
7925 struct frame *sf = SELECTED_FRAME ();
7926 message_enable_multibyte = multibyte;
7927
7928 if (noninteractive)
7929 {
7930 if (noninteractive_need_newline)
7931 putc ('\n', stderr);
7932 noninteractive_need_newline = 0;
7933 if (m)
7934 fwrite (m, nbytes, 1, stderr);
7935 if (cursor_in_echo_area == 0)
7936 fprintf (stderr, "\n");
7937 fflush (stderr);
7938 }
7939 /* A null message buffer means that the frame hasn't really been
7940 initialized yet. Error messages get reported properly by
7941 cmd_error, so this must be just an informative message; toss it. */
7942 else if (INTERACTIVE
7943 && sf->glyphs_initialized_p
7944 && FRAME_MESSAGE_BUF (sf))
7945 {
7946 Lisp_Object mini_window;
7947 struct frame *f;
7948
7949 /* Get the frame containing the mini-buffer
7950 that the selected frame is using. */
7951 mini_window = FRAME_MINIBUF_WINDOW (sf);
7952 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7953
7954 FRAME_SAMPLE_VISIBILITY (f);
7955 if (FRAME_VISIBLE_P (sf)
7956 && ! FRAME_VISIBLE_P (f))
7957 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7958
7959 if (m)
7960 {
7961 set_message (m, Qnil, nbytes, multibyte);
7962 if (minibuffer_auto_raise)
7963 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7964 }
7965 else
7966 clear_message (1, 1);
7967
7968 do_pending_window_change (0);
7969 echo_area_display (1);
7970 do_pending_window_change (0);
7971 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7972 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7973 }
7974 }
7975
7976
7977 /* Display an echo area message M with a specified length of NBYTES
7978 bytes. The string may include null characters. If M is not a
7979 string, clear out any existing message, and let the mini-buffer
7980 text show through.
7981
7982 This function cancels echoing. */
7983
7984 void
7985 message3 (m, nbytes, multibyte)
7986 Lisp_Object m;
7987 int nbytes;
7988 int multibyte;
7989 {
7990 struct gcpro gcpro1;
7991
7992 GCPRO1 (m);
7993 clear_message (1,1);
7994 cancel_echoing ();
7995
7996 /* First flush out any partial line written with print. */
7997 message_log_maybe_newline ();
7998 if (STRINGP (m))
7999 {
8000 char *buffer;
8001 USE_SAFE_ALLOCA;
8002
8003 SAFE_ALLOCA (buffer, char *, nbytes);
8004 bcopy (SDATA (m), buffer, nbytes);
8005 message_dolog (buffer, nbytes, 1, multibyte);
8006 SAFE_FREE ();
8007 }
8008 message3_nolog (m, nbytes, multibyte);
8009
8010 UNGCPRO;
8011 }
8012
8013
8014 /* The non-logging version of message3.
8015 This does not cancel echoing, because it is used for echoing.
8016 Perhaps we need to make a separate function for echoing
8017 and make this cancel echoing. */
8018
8019 void
8020 message3_nolog (m, nbytes, multibyte)
8021 Lisp_Object m;
8022 int nbytes, multibyte;
8023 {
8024 struct frame *sf = SELECTED_FRAME ();
8025 message_enable_multibyte = multibyte;
8026
8027 if (noninteractive)
8028 {
8029 if (noninteractive_need_newline)
8030 putc ('\n', stderr);
8031 noninteractive_need_newline = 0;
8032 if (STRINGP (m))
8033 fwrite (SDATA (m), nbytes, 1, stderr);
8034 if (cursor_in_echo_area == 0)
8035 fprintf (stderr, "\n");
8036 fflush (stderr);
8037 }
8038 /* A null message buffer means that the frame hasn't really been
8039 initialized yet. Error messages get reported properly by
8040 cmd_error, so this must be just an informative message; toss it. */
8041 else if (INTERACTIVE
8042 && sf->glyphs_initialized_p
8043 && FRAME_MESSAGE_BUF (sf))
8044 {
8045 Lisp_Object mini_window;
8046 Lisp_Object frame;
8047 struct frame *f;
8048
8049 /* Get the frame containing the mini-buffer
8050 that the selected frame is using. */
8051 mini_window = FRAME_MINIBUF_WINDOW (sf);
8052 frame = XWINDOW (mini_window)->frame;
8053 f = XFRAME (frame);
8054
8055 FRAME_SAMPLE_VISIBILITY (f);
8056 if (FRAME_VISIBLE_P (sf)
8057 && !FRAME_VISIBLE_P (f))
8058 Fmake_frame_visible (frame);
8059
8060 if (STRINGP (m) && SCHARS (m) > 0)
8061 {
8062 set_message (NULL, m, nbytes, multibyte);
8063 if (minibuffer_auto_raise)
8064 Fraise_frame (frame);
8065 /* Assume we are not echoing.
8066 (If we are, echo_now will override this.) */
8067 echo_message_buffer = Qnil;
8068 }
8069 else
8070 clear_message (1, 1);
8071
8072 do_pending_window_change (0);
8073 echo_area_display (1);
8074 do_pending_window_change (0);
8075 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8076 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8077 }
8078 }
8079
8080
8081 /* Display a null-terminated echo area message M. If M is 0, clear
8082 out any existing message, and let the mini-buffer text show through.
8083
8084 The buffer M must continue to exist until after the echo area gets
8085 cleared or some other message gets displayed there. Do not pass
8086 text that is stored in a Lisp string. Do not pass text in a buffer
8087 that was alloca'd. */
8088
8089 void
8090 message1 (m)
8091 char *m;
8092 {
8093 message2 (m, (m ? strlen (m) : 0), 0);
8094 }
8095
8096
8097 /* The non-logging counterpart of message1. */
8098
8099 void
8100 message1_nolog (m)
8101 char *m;
8102 {
8103 message2_nolog (m, (m ? strlen (m) : 0), 0);
8104 }
8105
8106 /* Display a message M which contains a single %s
8107 which gets replaced with STRING. */
8108
8109 void
8110 message_with_string (m, string, log)
8111 char *m;
8112 Lisp_Object string;
8113 int log;
8114 {
8115 CHECK_STRING (string);
8116
8117 if (noninteractive)
8118 {
8119 if (m)
8120 {
8121 if (noninteractive_need_newline)
8122 putc ('\n', stderr);
8123 noninteractive_need_newline = 0;
8124 fprintf (stderr, m, SDATA (string));
8125 if (cursor_in_echo_area == 0)
8126 fprintf (stderr, "\n");
8127 fflush (stderr);
8128 }
8129 }
8130 else if (INTERACTIVE)
8131 {
8132 /* The frame whose minibuffer we're going to display the message on.
8133 It may be larger than the selected frame, so we need
8134 to use its buffer, not the selected frame's buffer. */
8135 Lisp_Object mini_window;
8136 struct frame *f, *sf = SELECTED_FRAME ();
8137
8138 /* Get the frame containing the minibuffer
8139 that the selected frame is using. */
8140 mini_window = FRAME_MINIBUF_WINDOW (sf);
8141 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8142
8143 /* A null message buffer means that the frame hasn't really been
8144 initialized yet. Error messages get reported properly by
8145 cmd_error, so this must be just an informative message; toss it. */
8146 if (FRAME_MESSAGE_BUF (f))
8147 {
8148 Lisp_Object args[2], message;
8149 struct gcpro gcpro1, gcpro2;
8150
8151 args[0] = build_string (m);
8152 args[1] = message = string;
8153 GCPRO2 (args[0], message);
8154 gcpro1.nvars = 2;
8155
8156 message = Fformat (2, args);
8157
8158 if (log)
8159 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8160 else
8161 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8162
8163 UNGCPRO;
8164
8165 /* Print should start at the beginning of the message
8166 buffer next time. */
8167 message_buf_print = 0;
8168 }
8169 }
8170 }
8171
8172
8173 /* Dump an informative message to the minibuf. If M is 0, clear out
8174 any existing message, and let the mini-buffer text show through. */
8175
8176 /* VARARGS 1 */
8177 void
8178 message (m, a1, a2, a3)
8179 char *m;
8180 EMACS_INT a1, a2, a3;
8181 {
8182 if (noninteractive)
8183 {
8184 if (m)
8185 {
8186 if (noninteractive_need_newline)
8187 putc ('\n', stderr);
8188 noninteractive_need_newline = 0;
8189 fprintf (stderr, m, a1, a2, a3);
8190 if (cursor_in_echo_area == 0)
8191 fprintf (stderr, "\n");
8192 fflush (stderr);
8193 }
8194 }
8195 else if (INTERACTIVE)
8196 {
8197 /* The frame whose mini-buffer we're going to display the message
8198 on. It may be larger than the selected frame, so we need to
8199 use its buffer, not the selected frame's buffer. */
8200 Lisp_Object mini_window;
8201 struct frame *f, *sf = SELECTED_FRAME ();
8202
8203 /* Get the frame containing the mini-buffer
8204 that the selected frame is using. */
8205 mini_window = FRAME_MINIBUF_WINDOW (sf);
8206 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8207
8208 /* A null message buffer means that the frame hasn't really been
8209 initialized yet. Error messages get reported properly by
8210 cmd_error, so this must be just an informative message; toss
8211 it. */
8212 if (FRAME_MESSAGE_BUF (f))
8213 {
8214 if (m)
8215 {
8216 int len;
8217 #ifdef NO_ARG_ARRAY
8218 char *a[3];
8219 a[0] = (char *) a1;
8220 a[1] = (char *) a2;
8221 a[2] = (char *) a3;
8222
8223 len = doprnt (FRAME_MESSAGE_BUF (f),
8224 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8225 #else
8226 len = doprnt (FRAME_MESSAGE_BUF (f),
8227 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8228 (char **) &a1);
8229 #endif /* NO_ARG_ARRAY */
8230
8231 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8232 }
8233 else
8234 message1 (0);
8235
8236 /* Print should start at the beginning of the message
8237 buffer next time. */
8238 message_buf_print = 0;
8239 }
8240 }
8241 }
8242
8243
8244 /* The non-logging version of message. */
8245
8246 void
8247 message_nolog (m, a1, a2, a3)
8248 char *m;
8249 EMACS_INT a1, a2, a3;
8250 {
8251 Lisp_Object old_log_max;
8252 old_log_max = Vmessage_log_max;
8253 Vmessage_log_max = Qnil;
8254 message (m, a1, a2, a3);
8255 Vmessage_log_max = old_log_max;
8256 }
8257
8258
8259 /* Display the current message in the current mini-buffer. This is
8260 only called from error handlers in process.c, and is not time
8261 critical. */
8262
8263 void
8264 update_echo_area ()
8265 {
8266 if (!NILP (echo_area_buffer[0]))
8267 {
8268 Lisp_Object string;
8269 string = Fcurrent_message ();
8270 message3 (string, SBYTES (string),
8271 !NILP (current_buffer->enable_multibyte_characters));
8272 }
8273 }
8274
8275
8276 /* Make sure echo area buffers in `echo_buffers' are live.
8277 If they aren't, make new ones. */
8278
8279 static void
8280 ensure_echo_area_buffers ()
8281 {
8282 int i;
8283
8284 for (i = 0; i < 2; ++i)
8285 if (!BUFFERP (echo_buffer[i])
8286 || NILP (XBUFFER (echo_buffer[i])->name))
8287 {
8288 char name[30];
8289 Lisp_Object old_buffer;
8290 int j;
8291
8292 old_buffer = echo_buffer[i];
8293 sprintf (name, " *Echo Area %d*", i);
8294 echo_buffer[i] = Fget_buffer_create (build_string (name));
8295 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8296
8297 for (j = 0; j < 2; ++j)
8298 if (EQ (old_buffer, echo_area_buffer[j]))
8299 echo_area_buffer[j] = echo_buffer[i];
8300 }
8301 }
8302
8303
8304 /* Call FN with args A1..A4 with either the current or last displayed
8305 echo_area_buffer as current buffer.
8306
8307 WHICH zero means use the current message buffer
8308 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8309 from echo_buffer[] and clear it.
8310
8311 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8312 suitable buffer from echo_buffer[] and clear it.
8313
8314 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8315 that the current message becomes the last displayed one, make
8316 choose a suitable buffer for echo_area_buffer[0], and clear it.
8317
8318 Value is what FN returns. */
8319
8320 static int
8321 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8322 struct window *w;
8323 int which;
8324 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8325 EMACS_INT a1;
8326 Lisp_Object a2;
8327 EMACS_INT a3, a4;
8328 {
8329 Lisp_Object buffer;
8330 int this_one, the_other, clear_buffer_p, rc;
8331 int count = SPECPDL_INDEX ();
8332
8333 /* If buffers aren't live, make new ones. */
8334 ensure_echo_area_buffers ();
8335
8336 clear_buffer_p = 0;
8337
8338 if (which == 0)
8339 this_one = 0, the_other = 1;
8340 else if (which > 0)
8341 this_one = 1, the_other = 0;
8342 else
8343 {
8344 this_one = 0, the_other = 1;
8345 clear_buffer_p = 1;
8346
8347 /* We need a fresh one in case the current echo buffer equals
8348 the one containing the last displayed echo area message. */
8349 if (!NILP (echo_area_buffer[this_one])
8350 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8351 echo_area_buffer[this_one] = Qnil;
8352 }
8353
8354 /* Choose a suitable buffer from echo_buffer[] is we don't
8355 have one. */
8356 if (NILP (echo_area_buffer[this_one]))
8357 {
8358 echo_area_buffer[this_one]
8359 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8360 ? echo_buffer[the_other]
8361 : echo_buffer[this_one]);
8362 clear_buffer_p = 1;
8363 }
8364
8365 buffer = echo_area_buffer[this_one];
8366
8367 /* Don't get confused by reusing the buffer used for echoing
8368 for a different purpose. */
8369 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8370 cancel_echoing ();
8371
8372 record_unwind_protect (unwind_with_echo_area_buffer,
8373 with_echo_area_buffer_unwind_data (w));
8374
8375 /* Make the echo area buffer current. Note that for display
8376 purposes, it is not necessary that the displayed window's buffer
8377 == current_buffer, except for text property lookup. So, let's
8378 only set that buffer temporarily here without doing a full
8379 Fset_window_buffer. We must also change w->pointm, though,
8380 because otherwise an assertions in unshow_buffer fails, and Emacs
8381 aborts. */
8382 set_buffer_internal_1 (XBUFFER (buffer));
8383 if (w)
8384 {
8385 w->buffer = buffer;
8386 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8387 }
8388
8389 current_buffer->undo_list = Qt;
8390 current_buffer->read_only = Qnil;
8391 specbind (Qinhibit_read_only, Qt);
8392 specbind (Qinhibit_modification_hooks, Qt);
8393
8394 if (clear_buffer_p && Z > BEG)
8395 del_range (BEG, Z);
8396
8397 xassert (BEGV >= BEG);
8398 xassert (ZV <= Z && ZV >= BEGV);
8399
8400 rc = fn (a1, a2, a3, a4);
8401
8402 xassert (BEGV >= BEG);
8403 xassert (ZV <= Z && ZV >= BEGV);
8404
8405 unbind_to (count, Qnil);
8406 return rc;
8407 }
8408
8409
8410 /* Save state that should be preserved around the call to the function
8411 FN called in with_echo_area_buffer. */
8412
8413 static Lisp_Object
8414 with_echo_area_buffer_unwind_data (w)
8415 struct window *w;
8416 {
8417 int i = 0;
8418 Lisp_Object vector, tmp;
8419
8420 /* Reduce consing by keeping one vector in
8421 Vwith_echo_area_save_vector. */
8422 vector = Vwith_echo_area_save_vector;
8423 Vwith_echo_area_save_vector = Qnil;
8424
8425 if (NILP (vector))
8426 vector = Fmake_vector (make_number (7), Qnil);
8427
8428 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8429 ASET (vector, i, Vdeactivate_mark); ++i;
8430 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8431
8432 if (w)
8433 {
8434 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8435 ASET (vector, i, w->buffer); ++i;
8436 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8437 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8438 }
8439 else
8440 {
8441 int end = i + 4;
8442 for (; i < end; ++i)
8443 ASET (vector, i, Qnil);
8444 }
8445
8446 xassert (i == ASIZE (vector));
8447 return vector;
8448 }
8449
8450
8451 /* Restore global state from VECTOR which was created by
8452 with_echo_area_buffer_unwind_data. */
8453
8454 static Lisp_Object
8455 unwind_with_echo_area_buffer (vector)
8456 Lisp_Object vector;
8457 {
8458 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8459 Vdeactivate_mark = AREF (vector, 1);
8460 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8461
8462 if (WINDOWP (AREF (vector, 3)))
8463 {
8464 struct window *w;
8465 Lisp_Object buffer, charpos, bytepos;
8466
8467 w = XWINDOW (AREF (vector, 3));
8468 buffer = AREF (vector, 4);
8469 charpos = AREF (vector, 5);
8470 bytepos = AREF (vector, 6);
8471
8472 w->buffer = buffer;
8473 set_marker_both (w->pointm, buffer,
8474 XFASTINT (charpos), XFASTINT (bytepos));
8475 }
8476
8477 Vwith_echo_area_save_vector = vector;
8478 return Qnil;
8479 }
8480
8481
8482 /* Set up the echo area for use by print functions. MULTIBYTE_P
8483 non-zero means we will print multibyte. */
8484
8485 void
8486 setup_echo_area_for_printing (multibyte_p)
8487 int multibyte_p;
8488 {
8489 /* If we can't find an echo area any more, exit. */
8490 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8491 Fkill_emacs (Qnil);
8492
8493 ensure_echo_area_buffers ();
8494
8495 if (!message_buf_print)
8496 {
8497 /* A message has been output since the last time we printed.
8498 Choose a fresh echo area buffer. */
8499 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8500 echo_area_buffer[0] = echo_buffer[1];
8501 else
8502 echo_area_buffer[0] = echo_buffer[0];
8503
8504 /* Switch to that buffer and clear it. */
8505 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8506 current_buffer->truncate_lines = Qnil;
8507
8508 if (Z > BEG)
8509 {
8510 int count = SPECPDL_INDEX ();
8511 specbind (Qinhibit_read_only, Qt);
8512 /* Note that undo recording is always disabled. */
8513 del_range (BEG, Z);
8514 unbind_to (count, Qnil);
8515 }
8516 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8517
8518 /* Set up the buffer for the multibyteness we need. */
8519 if (multibyte_p
8520 != !NILP (current_buffer->enable_multibyte_characters))
8521 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8522
8523 /* Raise the frame containing the echo area. */
8524 if (minibuffer_auto_raise)
8525 {
8526 struct frame *sf = SELECTED_FRAME ();
8527 Lisp_Object mini_window;
8528 mini_window = FRAME_MINIBUF_WINDOW (sf);
8529 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8530 }
8531
8532 message_log_maybe_newline ();
8533 message_buf_print = 1;
8534 }
8535 else
8536 {
8537 if (NILP (echo_area_buffer[0]))
8538 {
8539 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8540 echo_area_buffer[0] = echo_buffer[1];
8541 else
8542 echo_area_buffer[0] = echo_buffer[0];
8543 }
8544
8545 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8546 {
8547 /* Someone switched buffers between print requests. */
8548 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8549 current_buffer->truncate_lines = Qnil;
8550 }
8551 }
8552 }
8553
8554
8555 /* Display an echo area message in window W. Value is non-zero if W's
8556 height is changed. If display_last_displayed_message_p is
8557 non-zero, display the message that was last displayed, otherwise
8558 display the current message. */
8559
8560 static int
8561 display_echo_area (w)
8562 struct window *w;
8563 {
8564 int i, no_message_p, window_height_changed_p, count;
8565
8566 /* Temporarily disable garbage collections while displaying the echo
8567 area. This is done because a GC can print a message itself.
8568 That message would modify the echo area buffer's contents while a
8569 redisplay of the buffer is going on, and seriously confuse
8570 redisplay. */
8571 count = inhibit_garbage_collection ();
8572
8573 /* If there is no message, we must call display_echo_area_1
8574 nevertheless because it resizes the window. But we will have to
8575 reset the echo_area_buffer in question to nil at the end because
8576 with_echo_area_buffer will sets it to an empty buffer. */
8577 i = display_last_displayed_message_p ? 1 : 0;
8578 no_message_p = NILP (echo_area_buffer[i]);
8579
8580 window_height_changed_p
8581 = with_echo_area_buffer (w, display_last_displayed_message_p,
8582 display_echo_area_1,
8583 (EMACS_INT) w, Qnil, 0, 0);
8584
8585 if (no_message_p)
8586 echo_area_buffer[i] = Qnil;
8587
8588 unbind_to (count, Qnil);
8589 return window_height_changed_p;
8590 }
8591
8592
8593 /* Helper for display_echo_area. Display the current buffer which
8594 contains the current echo area message in window W, a mini-window,
8595 a pointer to which is passed in A1. A2..A4 are currently not used.
8596 Change the height of W so that all of the message is displayed.
8597 Value is non-zero if height of W was changed. */
8598
8599 static int
8600 display_echo_area_1 (a1, a2, a3, a4)
8601 EMACS_INT a1;
8602 Lisp_Object a2;
8603 EMACS_INT a3, a4;
8604 {
8605 struct window *w = (struct window *) a1;
8606 Lisp_Object window;
8607 struct text_pos start;
8608 int window_height_changed_p = 0;
8609
8610 /* Do this before displaying, so that we have a large enough glyph
8611 matrix for the display. If we can't get enough space for the
8612 whole text, display the last N lines. That works by setting w->start. */
8613 window_height_changed_p = resize_mini_window (w, 0);
8614
8615 /* Use the starting position chosen by resize_mini_window. */
8616 SET_TEXT_POS_FROM_MARKER (start, w->start);
8617
8618 /* Display. */
8619 clear_glyph_matrix (w->desired_matrix);
8620 XSETWINDOW (window, w);
8621 try_window (window, start, 0);
8622
8623 return window_height_changed_p;
8624 }
8625
8626
8627 /* Resize the echo area window to exactly the size needed for the
8628 currently displayed message, if there is one. If a mini-buffer
8629 is active, don't shrink it. */
8630
8631 void
8632 resize_echo_area_exactly ()
8633 {
8634 if (BUFFERP (echo_area_buffer[0])
8635 && WINDOWP (echo_area_window))
8636 {
8637 struct window *w = XWINDOW (echo_area_window);
8638 int resized_p;
8639 Lisp_Object resize_exactly;
8640
8641 if (minibuf_level == 0)
8642 resize_exactly = Qt;
8643 else
8644 resize_exactly = Qnil;
8645
8646 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8647 (EMACS_INT) w, resize_exactly, 0, 0);
8648 if (resized_p)
8649 {
8650 ++windows_or_buffers_changed;
8651 ++update_mode_lines;
8652 redisplay_internal (0);
8653 }
8654 }
8655 }
8656
8657
8658 /* Callback function for with_echo_area_buffer, when used from
8659 resize_echo_area_exactly. A1 contains a pointer to the window to
8660 resize, EXACTLY non-nil means resize the mini-window exactly to the
8661 size of the text displayed. A3 and A4 are not used. Value is what
8662 resize_mini_window returns. */
8663
8664 static int
8665 resize_mini_window_1 (a1, exactly, a3, a4)
8666 EMACS_INT a1;
8667 Lisp_Object exactly;
8668 EMACS_INT a3, a4;
8669 {
8670 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8671 }
8672
8673
8674 /* Resize mini-window W to fit the size of its contents. EXACT_P
8675 means size the window exactly to the size needed. Otherwise, it's
8676 only enlarged until W's buffer is empty.
8677
8678 Set W->start to the right place to begin display. If the whole
8679 contents fit, start at the beginning. Otherwise, start so as
8680 to make the end of the contents appear. This is particularly
8681 important for y-or-n-p, but seems desirable generally.
8682
8683 Value is non-zero if the window height has been changed. */
8684
8685 int
8686 resize_mini_window (w, exact_p)
8687 struct window *w;
8688 int exact_p;
8689 {
8690 struct frame *f = XFRAME (w->frame);
8691 int window_height_changed_p = 0;
8692
8693 xassert (MINI_WINDOW_P (w));
8694
8695 /* By default, start display at the beginning. */
8696 set_marker_both (w->start, w->buffer,
8697 BUF_BEGV (XBUFFER (w->buffer)),
8698 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8699
8700 /* Don't resize windows while redisplaying a window; it would
8701 confuse redisplay functions when the size of the window they are
8702 displaying changes from under them. Such a resizing can happen,
8703 for instance, when which-func prints a long message while
8704 we are running fontification-functions. We're running these
8705 functions with safe_call which binds inhibit-redisplay to t. */
8706 if (!NILP (Vinhibit_redisplay))
8707 return 0;
8708
8709 /* Nil means don't try to resize. */
8710 if (NILP (Vresize_mini_windows)
8711 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8712 return 0;
8713
8714 if (!FRAME_MINIBUF_ONLY_P (f))
8715 {
8716 struct it it;
8717 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8718 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8719 int height, max_height;
8720 int unit = FRAME_LINE_HEIGHT (f);
8721 struct text_pos start;
8722 struct buffer *old_current_buffer = NULL;
8723
8724 if (current_buffer != XBUFFER (w->buffer))
8725 {
8726 old_current_buffer = current_buffer;
8727 set_buffer_internal (XBUFFER (w->buffer));
8728 }
8729
8730 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8731
8732 /* Compute the max. number of lines specified by the user. */
8733 if (FLOATP (Vmax_mini_window_height))
8734 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8735 else if (INTEGERP (Vmax_mini_window_height))
8736 max_height = XINT (Vmax_mini_window_height);
8737 else
8738 max_height = total_height / 4;
8739
8740 /* Correct that max. height if it's bogus. */
8741 max_height = max (1, max_height);
8742 max_height = min (total_height, max_height);
8743
8744 /* Find out the height of the text in the window. */
8745 if (it.line_wrap == TRUNCATE)
8746 height = 1;
8747 else
8748 {
8749 last_height = 0;
8750 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8751 if (it.max_ascent == 0 && it.max_descent == 0)
8752 height = it.current_y + last_height;
8753 else
8754 height = it.current_y + it.max_ascent + it.max_descent;
8755 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8756 height = (height + unit - 1) / unit;
8757 }
8758
8759 /* Compute a suitable window start. */
8760 if (height > max_height)
8761 {
8762 height = max_height;
8763 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8764 move_it_vertically_backward (&it, (height - 1) * unit);
8765 start = it.current.pos;
8766 }
8767 else
8768 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8769 SET_MARKER_FROM_TEXT_POS (w->start, start);
8770
8771 if (EQ (Vresize_mini_windows, Qgrow_only))
8772 {
8773 /* Let it grow only, until we display an empty message, in which
8774 case the window shrinks again. */
8775 if (height > WINDOW_TOTAL_LINES (w))
8776 {
8777 int old_height = WINDOW_TOTAL_LINES (w);
8778 freeze_window_starts (f, 1);
8779 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8780 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8781 }
8782 else if (height < WINDOW_TOTAL_LINES (w)
8783 && (exact_p || BEGV == ZV))
8784 {
8785 int old_height = WINDOW_TOTAL_LINES (w);
8786 freeze_window_starts (f, 0);
8787 shrink_mini_window (w);
8788 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8789 }
8790 }
8791 else
8792 {
8793 /* Always resize to exact size needed. */
8794 if (height > WINDOW_TOTAL_LINES (w))
8795 {
8796 int old_height = WINDOW_TOTAL_LINES (w);
8797 freeze_window_starts (f, 1);
8798 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8799 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8800 }
8801 else if (height < WINDOW_TOTAL_LINES (w))
8802 {
8803 int old_height = WINDOW_TOTAL_LINES (w);
8804 freeze_window_starts (f, 0);
8805 shrink_mini_window (w);
8806
8807 if (height)
8808 {
8809 freeze_window_starts (f, 1);
8810 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8811 }
8812
8813 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8814 }
8815 }
8816
8817 if (old_current_buffer)
8818 set_buffer_internal (old_current_buffer);
8819 }
8820
8821 return window_height_changed_p;
8822 }
8823
8824
8825 /* Value is the current message, a string, or nil if there is no
8826 current message. */
8827
8828 Lisp_Object
8829 current_message ()
8830 {
8831 Lisp_Object msg;
8832
8833 if (!BUFFERP (echo_area_buffer[0]))
8834 msg = Qnil;
8835 else
8836 {
8837 with_echo_area_buffer (0, 0, current_message_1,
8838 (EMACS_INT) &msg, Qnil, 0, 0);
8839 if (NILP (msg))
8840 echo_area_buffer[0] = Qnil;
8841 }
8842
8843 return msg;
8844 }
8845
8846
8847 static int
8848 current_message_1 (a1, a2, a3, a4)
8849 EMACS_INT a1;
8850 Lisp_Object a2;
8851 EMACS_INT a3, a4;
8852 {
8853 Lisp_Object *msg = (Lisp_Object *) a1;
8854
8855 if (Z > BEG)
8856 *msg = make_buffer_string (BEG, Z, 1);
8857 else
8858 *msg = Qnil;
8859 return 0;
8860 }
8861
8862
8863 /* Push the current message on Vmessage_stack for later restauration
8864 by restore_message. Value is non-zero if the current message isn't
8865 empty. This is a relatively infrequent operation, so it's not
8866 worth optimizing. */
8867
8868 int
8869 push_message ()
8870 {
8871 Lisp_Object msg;
8872 msg = current_message ();
8873 Vmessage_stack = Fcons (msg, Vmessage_stack);
8874 return STRINGP (msg);
8875 }
8876
8877
8878 /* Restore message display from the top of Vmessage_stack. */
8879
8880 void
8881 restore_message ()
8882 {
8883 Lisp_Object msg;
8884
8885 xassert (CONSP (Vmessage_stack));
8886 msg = XCAR (Vmessage_stack);
8887 if (STRINGP (msg))
8888 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8889 else
8890 message3_nolog (msg, 0, 0);
8891 }
8892
8893
8894 /* Handler for record_unwind_protect calling pop_message. */
8895
8896 Lisp_Object
8897 pop_message_unwind (dummy)
8898 Lisp_Object dummy;
8899 {
8900 pop_message ();
8901 return Qnil;
8902 }
8903
8904 /* Pop the top-most entry off Vmessage_stack. */
8905
8906 void
8907 pop_message ()
8908 {
8909 xassert (CONSP (Vmessage_stack));
8910 Vmessage_stack = XCDR (Vmessage_stack);
8911 }
8912
8913
8914 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8915 exits. If the stack is not empty, we have a missing pop_message
8916 somewhere. */
8917
8918 void
8919 check_message_stack ()
8920 {
8921 if (!NILP (Vmessage_stack))
8922 abort ();
8923 }
8924
8925
8926 /* Truncate to NCHARS what will be displayed in the echo area the next
8927 time we display it---but don't redisplay it now. */
8928
8929 void
8930 truncate_echo_area (nchars)
8931 int nchars;
8932 {
8933 if (nchars == 0)
8934 echo_area_buffer[0] = Qnil;
8935 /* A null message buffer means that the frame hasn't really been
8936 initialized yet. Error messages get reported properly by
8937 cmd_error, so this must be just an informative message; toss it. */
8938 else if (!noninteractive
8939 && INTERACTIVE
8940 && !NILP (echo_area_buffer[0]))
8941 {
8942 struct frame *sf = SELECTED_FRAME ();
8943 if (FRAME_MESSAGE_BUF (sf))
8944 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8945 }
8946 }
8947
8948
8949 /* Helper function for truncate_echo_area. Truncate the current
8950 message to at most NCHARS characters. */
8951
8952 static int
8953 truncate_message_1 (nchars, a2, a3, a4)
8954 EMACS_INT nchars;
8955 Lisp_Object a2;
8956 EMACS_INT a3, a4;
8957 {
8958 if (BEG + nchars < Z)
8959 del_range (BEG + nchars, Z);
8960 if (Z == BEG)
8961 echo_area_buffer[0] = Qnil;
8962 return 0;
8963 }
8964
8965
8966 /* Set the current message to a substring of S or STRING.
8967
8968 If STRING is a Lisp string, set the message to the first NBYTES
8969 bytes from STRING. NBYTES zero means use the whole string. If
8970 STRING is multibyte, the message will be displayed multibyte.
8971
8972 If S is not null, set the message to the first LEN bytes of S. LEN
8973 zero means use the whole string. MULTIBYTE_P non-zero means S is
8974 multibyte. Display the message multibyte in that case.
8975
8976 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8977 to t before calling set_message_1 (which calls insert).
8978 */
8979
8980 void
8981 set_message (s, string, nbytes, multibyte_p)
8982 const char *s;
8983 Lisp_Object string;
8984 int nbytes, multibyte_p;
8985 {
8986 message_enable_multibyte
8987 = ((s && multibyte_p)
8988 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8989
8990 with_echo_area_buffer (0, -1, set_message_1,
8991 (EMACS_INT) s, string, nbytes, multibyte_p);
8992 message_buf_print = 0;
8993 help_echo_showing_p = 0;
8994 }
8995
8996
8997 /* Helper function for set_message. Arguments have the same meaning
8998 as there, with A1 corresponding to S and A2 corresponding to STRING
8999 This function is called with the echo area buffer being
9000 current. */
9001
9002 static int
9003 set_message_1 (a1, a2, nbytes, multibyte_p)
9004 EMACS_INT a1;
9005 Lisp_Object a2;
9006 EMACS_INT nbytes, multibyte_p;
9007 {
9008 const char *s = (const char *) a1;
9009 Lisp_Object string = a2;
9010
9011 /* Change multibyteness of the echo buffer appropriately. */
9012 if (message_enable_multibyte
9013 != !NILP (current_buffer->enable_multibyte_characters))
9014 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9015
9016 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9017
9018 /* Insert new message at BEG. */
9019 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9020
9021 if (STRINGP (string))
9022 {
9023 int nchars;
9024
9025 if (nbytes == 0)
9026 nbytes = SBYTES (string);
9027 nchars = string_byte_to_char (string, nbytes);
9028
9029 /* This function takes care of single/multibyte conversion. We
9030 just have to ensure that the echo area buffer has the right
9031 setting of enable_multibyte_characters. */
9032 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9033 }
9034 else if (s)
9035 {
9036 if (nbytes == 0)
9037 nbytes = strlen (s);
9038
9039 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9040 {
9041 /* Convert from multi-byte to single-byte. */
9042 int i, c, n;
9043 unsigned char work[1];
9044
9045 /* Convert a multibyte string to single-byte. */
9046 for (i = 0; i < nbytes; i += n)
9047 {
9048 c = string_char_and_length (s + i, nbytes - i, &n);
9049 work[0] = (ASCII_CHAR_P (c)
9050 ? c
9051 : multibyte_char_to_unibyte (c, Qnil));
9052 insert_1_both (work, 1, 1, 1, 0, 0);
9053 }
9054 }
9055 else if (!multibyte_p
9056 && !NILP (current_buffer->enable_multibyte_characters))
9057 {
9058 /* Convert from single-byte to multi-byte. */
9059 int i, c, n;
9060 const unsigned char *msg = (const unsigned char *) s;
9061 unsigned char str[MAX_MULTIBYTE_LENGTH];
9062
9063 /* Convert a single-byte string to multibyte. */
9064 for (i = 0; i < nbytes; i++)
9065 {
9066 c = msg[i];
9067 c = unibyte_char_to_multibyte (c);
9068 n = CHAR_STRING (c, str);
9069 insert_1_both (str, 1, n, 1, 0, 0);
9070 }
9071 }
9072 else
9073 insert_1 (s, nbytes, 1, 0, 0);
9074 }
9075
9076 return 0;
9077 }
9078
9079
9080 /* Clear messages. CURRENT_P non-zero means clear the current
9081 message. LAST_DISPLAYED_P non-zero means clear the message
9082 last displayed. */
9083
9084 void
9085 clear_message (current_p, last_displayed_p)
9086 int current_p, last_displayed_p;
9087 {
9088 if (current_p)
9089 {
9090 echo_area_buffer[0] = Qnil;
9091 message_cleared_p = 1;
9092 }
9093
9094 if (last_displayed_p)
9095 echo_area_buffer[1] = Qnil;
9096
9097 message_buf_print = 0;
9098 }
9099
9100 /* Clear garbaged frames.
9101
9102 This function is used where the old redisplay called
9103 redraw_garbaged_frames which in turn called redraw_frame which in
9104 turn called clear_frame. The call to clear_frame was a source of
9105 flickering. I believe a clear_frame is not necessary. It should
9106 suffice in the new redisplay to invalidate all current matrices,
9107 and ensure a complete redisplay of all windows. */
9108
9109 static void
9110 clear_garbaged_frames ()
9111 {
9112 if (frame_garbaged)
9113 {
9114 Lisp_Object tail, frame;
9115 int changed_count = 0;
9116
9117 FOR_EACH_FRAME (tail, frame)
9118 {
9119 struct frame *f = XFRAME (frame);
9120
9121 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9122 {
9123 if (f->resized_p)
9124 {
9125 Fredraw_frame (frame);
9126 f->force_flush_display_p = 1;
9127 }
9128 clear_current_matrices (f);
9129 changed_count++;
9130 f->garbaged = 0;
9131 f->resized_p = 0;
9132 }
9133 }
9134
9135 frame_garbaged = 0;
9136 if (changed_count)
9137 ++windows_or_buffers_changed;
9138 }
9139 }
9140
9141
9142 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9143 is non-zero update selected_frame. Value is non-zero if the
9144 mini-windows height has been changed. */
9145
9146 static int
9147 echo_area_display (update_frame_p)
9148 int update_frame_p;
9149 {
9150 Lisp_Object mini_window;
9151 struct window *w;
9152 struct frame *f;
9153 int window_height_changed_p = 0;
9154 struct frame *sf = SELECTED_FRAME ();
9155
9156 mini_window = FRAME_MINIBUF_WINDOW (sf);
9157 w = XWINDOW (mini_window);
9158 f = XFRAME (WINDOW_FRAME (w));
9159
9160 /* Don't display if frame is invisible or not yet initialized. */
9161 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9162 return 0;
9163
9164 #ifdef HAVE_WINDOW_SYSTEM
9165 /* When Emacs starts, selected_frame may be the initial terminal
9166 frame. If we let this through, a message would be displayed on
9167 the terminal. */
9168 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9169 return 0;
9170 #endif /* HAVE_WINDOW_SYSTEM */
9171
9172 /* Redraw garbaged frames. */
9173 if (frame_garbaged)
9174 clear_garbaged_frames ();
9175
9176 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9177 {
9178 echo_area_window = mini_window;
9179 window_height_changed_p = display_echo_area (w);
9180 w->must_be_updated_p = 1;
9181
9182 /* Update the display, unless called from redisplay_internal.
9183 Also don't update the screen during redisplay itself. The
9184 update will happen at the end of redisplay, and an update
9185 here could cause confusion. */
9186 if (update_frame_p && !redisplaying_p)
9187 {
9188 int n = 0;
9189
9190 /* If the display update has been interrupted by pending
9191 input, update mode lines in the frame. Due to the
9192 pending input, it might have been that redisplay hasn't
9193 been called, so that mode lines above the echo area are
9194 garbaged. This looks odd, so we prevent it here. */
9195 if (!display_completed)
9196 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9197
9198 if (window_height_changed_p
9199 /* Don't do this if Emacs is shutting down. Redisplay
9200 needs to run hooks. */
9201 && !NILP (Vrun_hooks))
9202 {
9203 /* Must update other windows. Likewise as in other
9204 cases, don't let this update be interrupted by
9205 pending input. */
9206 int count = SPECPDL_INDEX ();
9207 specbind (Qredisplay_dont_pause, Qt);
9208 windows_or_buffers_changed = 1;
9209 redisplay_internal (0);
9210 unbind_to (count, Qnil);
9211 }
9212 else if (FRAME_WINDOW_P (f) && n == 0)
9213 {
9214 /* Window configuration is the same as before.
9215 Can do with a display update of the echo area,
9216 unless we displayed some mode lines. */
9217 update_single_window (w, 1);
9218 FRAME_RIF (f)->flush_display (f);
9219 }
9220 else
9221 update_frame (f, 1, 1);
9222
9223 /* If cursor is in the echo area, make sure that the next
9224 redisplay displays the minibuffer, so that the cursor will
9225 be replaced with what the minibuffer wants. */
9226 if (cursor_in_echo_area)
9227 ++windows_or_buffers_changed;
9228 }
9229 }
9230 else if (!EQ (mini_window, selected_window))
9231 windows_or_buffers_changed++;
9232
9233 /* Last displayed message is now the current message. */
9234 echo_area_buffer[1] = echo_area_buffer[0];
9235 /* Inform read_char that we're not echoing. */
9236 echo_message_buffer = Qnil;
9237
9238 /* Prevent redisplay optimization in redisplay_internal by resetting
9239 this_line_start_pos. This is done because the mini-buffer now
9240 displays the message instead of its buffer text. */
9241 if (EQ (mini_window, selected_window))
9242 CHARPOS (this_line_start_pos) = 0;
9243
9244 return window_height_changed_p;
9245 }
9246
9247
9248 \f
9249 /***********************************************************************
9250 Mode Lines and Frame Titles
9251 ***********************************************************************/
9252
9253 /* A buffer for constructing non-propertized mode-line strings and
9254 frame titles in it; allocated from the heap in init_xdisp and
9255 resized as needed in store_mode_line_noprop_char. */
9256
9257 static char *mode_line_noprop_buf;
9258
9259 /* The buffer's end, and a current output position in it. */
9260
9261 static char *mode_line_noprop_buf_end;
9262 static char *mode_line_noprop_ptr;
9263
9264 #define MODE_LINE_NOPROP_LEN(start) \
9265 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9266
9267 static enum {
9268 MODE_LINE_DISPLAY = 0,
9269 MODE_LINE_TITLE,
9270 MODE_LINE_NOPROP,
9271 MODE_LINE_STRING
9272 } mode_line_target;
9273
9274 /* Alist that caches the results of :propertize.
9275 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9276 static Lisp_Object mode_line_proptrans_alist;
9277
9278 /* List of strings making up the mode-line. */
9279 static Lisp_Object mode_line_string_list;
9280
9281 /* Base face property when building propertized mode line string. */
9282 static Lisp_Object mode_line_string_face;
9283 static Lisp_Object mode_line_string_face_prop;
9284
9285
9286 /* Unwind data for mode line strings */
9287
9288 static Lisp_Object Vmode_line_unwind_vector;
9289
9290 static Lisp_Object
9291 format_mode_line_unwind_data (struct buffer *obuf,
9292 Lisp_Object owin,
9293 int save_proptrans)
9294 {
9295 Lisp_Object vector, tmp;
9296
9297 /* Reduce consing by keeping one vector in
9298 Vwith_echo_area_save_vector. */
9299 vector = Vmode_line_unwind_vector;
9300 Vmode_line_unwind_vector = Qnil;
9301
9302 if (NILP (vector))
9303 vector = Fmake_vector (make_number (8), Qnil);
9304
9305 ASET (vector, 0, make_number (mode_line_target));
9306 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9307 ASET (vector, 2, mode_line_string_list);
9308 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9309 ASET (vector, 4, mode_line_string_face);
9310 ASET (vector, 5, mode_line_string_face_prop);
9311
9312 if (obuf)
9313 XSETBUFFER (tmp, obuf);
9314 else
9315 tmp = Qnil;
9316 ASET (vector, 6, tmp);
9317 ASET (vector, 7, owin);
9318
9319 return vector;
9320 }
9321
9322 static Lisp_Object
9323 unwind_format_mode_line (vector)
9324 Lisp_Object vector;
9325 {
9326 mode_line_target = XINT (AREF (vector, 0));
9327 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9328 mode_line_string_list = AREF (vector, 2);
9329 if (! EQ (AREF (vector, 3), Qt))
9330 mode_line_proptrans_alist = AREF (vector, 3);
9331 mode_line_string_face = AREF (vector, 4);
9332 mode_line_string_face_prop = AREF (vector, 5);
9333
9334 if (!NILP (AREF (vector, 7)))
9335 /* Select window before buffer, since it may change the buffer. */
9336 Fselect_window (AREF (vector, 7), Qt);
9337
9338 if (!NILP (AREF (vector, 6)))
9339 {
9340 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9341 ASET (vector, 6, Qnil);
9342 }
9343
9344 Vmode_line_unwind_vector = vector;
9345 return Qnil;
9346 }
9347
9348
9349 /* Store a single character C for the frame title in mode_line_noprop_buf.
9350 Re-allocate mode_line_noprop_buf if necessary. */
9351
9352 static void
9353 #ifdef PROTOTYPES
9354 store_mode_line_noprop_char (char c)
9355 #else
9356 store_mode_line_noprop_char (c)
9357 char c;
9358 #endif
9359 {
9360 /* If output position has reached the end of the allocated buffer,
9361 double the buffer's size. */
9362 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9363 {
9364 int len = MODE_LINE_NOPROP_LEN (0);
9365 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9366 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9367 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9368 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9369 }
9370
9371 *mode_line_noprop_ptr++ = c;
9372 }
9373
9374
9375 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9376 mode_line_noprop_ptr. STR is the string to store. Do not copy
9377 characters that yield more columns than PRECISION; PRECISION <= 0
9378 means copy the whole string. Pad with spaces until FIELD_WIDTH
9379 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9380 pad. Called from display_mode_element when it is used to build a
9381 frame title. */
9382
9383 static int
9384 store_mode_line_noprop (str, field_width, precision)
9385 const unsigned char *str;
9386 int field_width, precision;
9387 {
9388 int n = 0;
9389 int dummy, nbytes;
9390
9391 /* Copy at most PRECISION chars from STR. */
9392 nbytes = strlen (str);
9393 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9394 while (nbytes--)
9395 store_mode_line_noprop_char (*str++);
9396
9397 /* Fill up with spaces until FIELD_WIDTH reached. */
9398 while (field_width > 0
9399 && n < field_width)
9400 {
9401 store_mode_line_noprop_char (' ');
9402 ++n;
9403 }
9404
9405 return n;
9406 }
9407
9408 /***********************************************************************
9409 Frame Titles
9410 ***********************************************************************/
9411
9412 #ifdef HAVE_WINDOW_SYSTEM
9413
9414 /* Set the title of FRAME, if it has changed. The title format is
9415 Vicon_title_format if FRAME is iconified, otherwise it is
9416 frame_title_format. */
9417
9418 static void
9419 x_consider_frame_title (frame)
9420 Lisp_Object frame;
9421 {
9422 struct frame *f = XFRAME (frame);
9423
9424 if (FRAME_WINDOW_P (f)
9425 || FRAME_MINIBUF_ONLY_P (f)
9426 || f->explicit_name)
9427 {
9428 /* Do we have more than one visible frame on this X display? */
9429 Lisp_Object tail;
9430 Lisp_Object fmt;
9431 int title_start;
9432 char *title;
9433 int len;
9434 struct it it;
9435 int count = SPECPDL_INDEX ();
9436
9437 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9438 {
9439 Lisp_Object other_frame = XCAR (tail);
9440 struct frame *tf = XFRAME (other_frame);
9441
9442 if (tf != f
9443 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9444 && !FRAME_MINIBUF_ONLY_P (tf)
9445 && !EQ (other_frame, tip_frame)
9446 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9447 break;
9448 }
9449
9450 /* Set global variable indicating that multiple frames exist. */
9451 multiple_frames = CONSP (tail);
9452
9453 /* Switch to the buffer of selected window of the frame. Set up
9454 mode_line_target so that display_mode_element will output into
9455 mode_line_noprop_buf; then display the title. */
9456 record_unwind_protect (unwind_format_mode_line,
9457 format_mode_line_unwind_data
9458 (current_buffer, selected_window, 0));
9459
9460 Fselect_window (f->selected_window, Qt);
9461 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9462 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9463
9464 mode_line_target = MODE_LINE_TITLE;
9465 title_start = MODE_LINE_NOPROP_LEN (0);
9466 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9467 NULL, DEFAULT_FACE_ID);
9468 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9469 len = MODE_LINE_NOPROP_LEN (title_start);
9470 title = mode_line_noprop_buf + title_start;
9471 unbind_to (count, Qnil);
9472
9473 /* Set the title only if it's changed. This avoids consing in
9474 the common case where it hasn't. (If it turns out that we've
9475 already wasted too much time by walking through the list with
9476 display_mode_element, then we might need to optimize at a
9477 higher level than this.) */
9478 if (! STRINGP (f->name)
9479 || SBYTES (f->name) != len
9480 || bcmp (title, SDATA (f->name), len) != 0)
9481 {
9482 #ifdef HAVE_NS
9483 if (FRAME_NS_P (f))
9484 {
9485 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9486 {
9487 if (EQ (fmt, Qt))
9488 ns_set_name_as_filename (f);
9489 else
9490 x_implicitly_set_name (f, make_string(title, len),
9491 Qnil);
9492 }
9493 }
9494 else
9495 #endif
9496 x_implicitly_set_name (f, make_string (title, len), Qnil);
9497 }
9498 #ifdef HAVE_NS
9499 if (FRAME_NS_P (f))
9500 {
9501 /* do this also for frames with explicit names */
9502 ns_implicitly_set_icon_type(f);
9503 ns_set_doc_edited(f, Fbuffer_modified_p
9504 (XWINDOW (f->selected_window)->buffer), Qnil);
9505 }
9506 #endif
9507 }
9508 }
9509
9510 #endif /* not HAVE_WINDOW_SYSTEM */
9511
9512
9513
9514 \f
9515 /***********************************************************************
9516 Menu Bars
9517 ***********************************************************************/
9518
9519
9520 /* Prepare for redisplay by updating menu-bar item lists when
9521 appropriate. This can call eval. */
9522
9523 void
9524 prepare_menu_bars ()
9525 {
9526 int all_windows;
9527 struct gcpro gcpro1, gcpro2;
9528 struct frame *f;
9529 Lisp_Object tooltip_frame;
9530
9531 #ifdef HAVE_WINDOW_SYSTEM
9532 tooltip_frame = tip_frame;
9533 #else
9534 tooltip_frame = Qnil;
9535 #endif
9536
9537 /* Update all frame titles based on their buffer names, etc. We do
9538 this before the menu bars so that the buffer-menu will show the
9539 up-to-date frame titles. */
9540 #ifdef HAVE_WINDOW_SYSTEM
9541 if (windows_or_buffers_changed || update_mode_lines)
9542 {
9543 Lisp_Object tail, frame;
9544
9545 FOR_EACH_FRAME (tail, frame)
9546 {
9547 f = XFRAME (frame);
9548 if (!EQ (frame, tooltip_frame)
9549 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9550 x_consider_frame_title (frame);
9551 }
9552 }
9553 #endif /* HAVE_WINDOW_SYSTEM */
9554
9555 /* Update the menu bar item lists, if appropriate. This has to be
9556 done before any actual redisplay or generation of display lines. */
9557 all_windows = (update_mode_lines
9558 || buffer_shared > 1
9559 || windows_or_buffers_changed);
9560 if (all_windows)
9561 {
9562 Lisp_Object tail, frame;
9563 int count = SPECPDL_INDEX ();
9564 /* 1 means that update_menu_bar has run its hooks
9565 so any further calls to update_menu_bar shouldn't do so again. */
9566 int menu_bar_hooks_run = 0;
9567
9568 record_unwind_save_match_data ();
9569
9570 FOR_EACH_FRAME (tail, frame)
9571 {
9572 f = XFRAME (frame);
9573
9574 /* Ignore tooltip frame. */
9575 if (EQ (frame, tooltip_frame))
9576 continue;
9577
9578 /* If a window on this frame changed size, report that to
9579 the user and clear the size-change flag. */
9580 if (FRAME_WINDOW_SIZES_CHANGED (f))
9581 {
9582 Lisp_Object functions;
9583
9584 /* Clear flag first in case we get an error below. */
9585 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9586 functions = Vwindow_size_change_functions;
9587 GCPRO2 (tail, functions);
9588
9589 while (CONSP (functions))
9590 {
9591 call1 (XCAR (functions), frame);
9592 functions = XCDR (functions);
9593 }
9594 UNGCPRO;
9595 }
9596
9597 GCPRO1 (tail);
9598 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9599 #ifdef HAVE_WINDOW_SYSTEM
9600 update_tool_bar (f, 0);
9601 #endif
9602 UNGCPRO;
9603 }
9604
9605 unbind_to (count, Qnil);
9606 }
9607 else
9608 {
9609 struct frame *sf = SELECTED_FRAME ();
9610 update_menu_bar (sf, 1, 0);
9611 #ifdef HAVE_WINDOW_SYSTEM
9612 update_tool_bar (sf, 1);
9613 #endif
9614 }
9615
9616 /* Motif needs this. See comment in xmenu.c. Turn it off when
9617 pending_menu_activation is not defined. */
9618 #ifdef USE_X_TOOLKIT
9619 pending_menu_activation = 0;
9620 #endif
9621 }
9622
9623
9624 /* Update the menu bar item list for frame F. This has to be done
9625 before we start to fill in any display lines, because it can call
9626 eval.
9627
9628 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9629
9630 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9631 already ran the menu bar hooks for this redisplay, so there
9632 is no need to run them again. The return value is the
9633 updated value of this flag, to pass to the next call. */
9634
9635 static int
9636 update_menu_bar (f, save_match_data, hooks_run)
9637 struct frame *f;
9638 int save_match_data;
9639 int hooks_run;
9640 {
9641 Lisp_Object window;
9642 register struct window *w;
9643
9644 /* If called recursively during a menu update, do nothing. This can
9645 happen when, for instance, an activate-menubar-hook causes a
9646 redisplay. */
9647 if (inhibit_menubar_update)
9648 return hooks_run;
9649
9650 window = FRAME_SELECTED_WINDOW (f);
9651 w = XWINDOW (window);
9652
9653 #if 0 /* The if statement below this if statement used to include the
9654 condition !NILP (w->update_mode_line), rather than using
9655 update_mode_lines directly, and this if statement may have
9656 been added to make that condition work. Now the if
9657 statement below matches its comment, this isn't needed. */
9658 if (update_mode_lines)
9659 w->update_mode_line = Qt;
9660 #endif
9661
9662 if (FRAME_WINDOW_P (f)
9663 ?
9664 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9665 || defined (HAVE_NS) || defined (USE_GTK)
9666 FRAME_EXTERNAL_MENU_BAR (f)
9667 #else
9668 FRAME_MENU_BAR_LINES (f) > 0
9669 #endif
9670 : FRAME_MENU_BAR_LINES (f) > 0)
9671 {
9672 /* If the user has switched buffers or windows, we need to
9673 recompute to reflect the new bindings. But we'll
9674 recompute when update_mode_lines is set too; that means
9675 that people can use force-mode-line-update to request
9676 that the menu bar be recomputed. The adverse effect on
9677 the rest of the redisplay algorithm is about the same as
9678 windows_or_buffers_changed anyway. */
9679 if (windows_or_buffers_changed
9680 /* This used to test w->update_mode_line, but we believe
9681 there is no need to recompute the menu in that case. */
9682 || update_mode_lines
9683 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9684 < BUF_MODIFF (XBUFFER (w->buffer)))
9685 != !NILP (w->last_had_star))
9686 || ((!NILP (Vtransient_mark_mode)
9687 && !NILP (XBUFFER (w->buffer)->mark_active))
9688 != !NILP (w->region_showing)))
9689 {
9690 struct buffer *prev = current_buffer;
9691 int count = SPECPDL_INDEX ();
9692
9693 specbind (Qinhibit_menubar_update, Qt);
9694
9695 set_buffer_internal_1 (XBUFFER (w->buffer));
9696 if (save_match_data)
9697 record_unwind_save_match_data ();
9698 if (NILP (Voverriding_local_map_menu_flag))
9699 {
9700 specbind (Qoverriding_terminal_local_map, Qnil);
9701 specbind (Qoverriding_local_map, Qnil);
9702 }
9703
9704 if (!hooks_run)
9705 {
9706 /* Run the Lucid hook. */
9707 safe_run_hooks (Qactivate_menubar_hook);
9708
9709 /* If it has changed current-menubar from previous value,
9710 really recompute the menu-bar from the value. */
9711 if (! NILP (Vlucid_menu_bar_dirty_flag))
9712 call0 (Qrecompute_lucid_menubar);
9713
9714 safe_run_hooks (Qmenu_bar_update_hook);
9715
9716 hooks_run = 1;
9717 }
9718
9719 XSETFRAME (Vmenu_updating_frame, f);
9720 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9721
9722 /* Redisplay the menu bar in case we changed it. */
9723 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9724 || defined (HAVE_NS) || defined (USE_GTK)
9725 if (FRAME_WINDOW_P (f))
9726 {
9727 #if defined (HAVE_NS)
9728 /* All frames on Mac OS share the same menubar. So only
9729 the selected frame should be allowed to set it. */
9730 if (f == SELECTED_FRAME ())
9731 #endif
9732 set_frame_menubar (f, 0, 0);
9733 }
9734 else
9735 /* On a terminal screen, the menu bar is an ordinary screen
9736 line, and this makes it get updated. */
9737 w->update_mode_line = Qt;
9738 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9739 /* In the non-toolkit version, the menu bar is an ordinary screen
9740 line, and this makes it get updated. */
9741 w->update_mode_line = Qt;
9742 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9743
9744 unbind_to (count, Qnil);
9745 set_buffer_internal_1 (prev);
9746 }
9747 }
9748
9749 return hooks_run;
9750 }
9751
9752
9753 \f
9754 /***********************************************************************
9755 Output Cursor
9756 ***********************************************************************/
9757
9758 #ifdef HAVE_WINDOW_SYSTEM
9759
9760 /* EXPORT:
9761 Nominal cursor position -- where to draw output.
9762 HPOS and VPOS are window relative glyph matrix coordinates.
9763 X and Y are window relative pixel coordinates. */
9764
9765 struct cursor_pos output_cursor;
9766
9767
9768 /* EXPORT:
9769 Set the global variable output_cursor to CURSOR. All cursor
9770 positions are relative to updated_window. */
9771
9772 void
9773 set_output_cursor (cursor)
9774 struct cursor_pos *cursor;
9775 {
9776 output_cursor.hpos = cursor->hpos;
9777 output_cursor.vpos = cursor->vpos;
9778 output_cursor.x = cursor->x;
9779 output_cursor.y = cursor->y;
9780 }
9781
9782
9783 /* EXPORT for RIF:
9784 Set a nominal cursor position.
9785
9786 HPOS and VPOS are column/row positions in a window glyph matrix. X
9787 and Y are window text area relative pixel positions.
9788
9789 If this is done during an update, updated_window will contain the
9790 window that is being updated and the position is the future output
9791 cursor position for that window. If updated_window is null, use
9792 selected_window and display the cursor at the given position. */
9793
9794 void
9795 x_cursor_to (vpos, hpos, y, x)
9796 int vpos, hpos, y, x;
9797 {
9798 struct window *w;
9799
9800 /* If updated_window is not set, work on selected_window. */
9801 if (updated_window)
9802 w = updated_window;
9803 else
9804 w = XWINDOW (selected_window);
9805
9806 /* Set the output cursor. */
9807 output_cursor.hpos = hpos;
9808 output_cursor.vpos = vpos;
9809 output_cursor.x = x;
9810 output_cursor.y = y;
9811
9812 /* If not called as part of an update, really display the cursor.
9813 This will also set the cursor position of W. */
9814 if (updated_window == NULL)
9815 {
9816 BLOCK_INPUT;
9817 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9818 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9819 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9820 UNBLOCK_INPUT;
9821 }
9822 }
9823
9824 #endif /* HAVE_WINDOW_SYSTEM */
9825
9826 \f
9827 /***********************************************************************
9828 Tool-bars
9829 ***********************************************************************/
9830
9831 #ifdef HAVE_WINDOW_SYSTEM
9832
9833 /* Where the mouse was last time we reported a mouse event. */
9834
9835 FRAME_PTR last_mouse_frame;
9836
9837 /* Tool-bar item index of the item on which a mouse button was pressed
9838 or -1. */
9839
9840 int last_tool_bar_item;
9841
9842
9843 /* Update the tool-bar item list for frame F. This has to be done
9844 before we start to fill in any display lines. Called from
9845 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9846 and restore it here. */
9847
9848 static void
9849 update_tool_bar (f, save_match_data)
9850 struct frame *f;
9851 int save_match_data;
9852 {
9853 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
9854 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9855 #else
9856 int do_update = WINDOWP (f->tool_bar_window)
9857 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9858 #endif
9859
9860 if (do_update)
9861 {
9862 Lisp_Object window;
9863 struct window *w;
9864
9865 window = FRAME_SELECTED_WINDOW (f);
9866 w = XWINDOW (window);
9867
9868 /* If the user has switched buffers or windows, we need to
9869 recompute to reflect the new bindings. But we'll
9870 recompute when update_mode_lines is set too; that means
9871 that people can use force-mode-line-update to request
9872 that the menu bar be recomputed. The adverse effect on
9873 the rest of the redisplay algorithm is about the same as
9874 windows_or_buffers_changed anyway. */
9875 if (windows_or_buffers_changed
9876 || !NILP (w->update_mode_line)
9877 || update_mode_lines
9878 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9879 < BUF_MODIFF (XBUFFER (w->buffer)))
9880 != !NILP (w->last_had_star))
9881 || ((!NILP (Vtransient_mark_mode)
9882 && !NILP (XBUFFER (w->buffer)->mark_active))
9883 != !NILP (w->region_showing)))
9884 {
9885 struct buffer *prev = current_buffer;
9886 int count = SPECPDL_INDEX ();
9887 Lisp_Object new_tool_bar;
9888 int new_n_tool_bar;
9889 struct gcpro gcpro1;
9890
9891 /* Set current_buffer to the buffer of the selected
9892 window of the frame, so that we get the right local
9893 keymaps. */
9894 set_buffer_internal_1 (XBUFFER (w->buffer));
9895
9896 /* Save match data, if we must. */
9897 if (save_match_data)
9898 record_unwind_save_match_data ();
9899
9900 /* Make sure that we don't accidentally use bogus keymaps. */
9901 if (NILP (Voverriding_local_map_menu_flag))
9902 {
9903 specbind (Qoverriding_terminal_local_map, Qnil);
9904 specbind (Qoverriding_local_map, Qnil);
9905 }
9906
9907 GCPRO1 (new_tool_bar);
9908
9909 /* Build desired tool-bar items from keymaps. */
9910 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9911 &new_n_tool_bar);
9912
9913 /* Redisplay the tool-bar if we changed it. */
9914 if (new_n_tool_bar != f->n_tool_bar_items
9915 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9916 {
9917 /* Redisplay that happens asynchronously due to an expose event
9918 may access f->tool_bar_items. Make sure we update both
9919 variables within BLOCK_INPUT so no such event interrupts. */
9920 BLOCK_INPUT;
9921 f->tool_bar_items = new_tool_bar;
9922 f->n_tool_bar_items = new_n_tool_bar;
9923 w->update_mode_line = Qt;
9924 UNBLOCK_INPUT;
9925 }
9926
9927 UNGCPRO;
9928
9929 unbind_to (count, Qnil);
9930 set_buffer_internal_1 (prev);
9931 }
9932 }
9933 }
9934
9935
9936 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9937 F's desired tool-bar contents. F->tool_bar_items must have
9938 been set up previously by calling prepare_menu_bars. */
9939
9940 static void
9941 build_desired_tool_bar_string (f)
9942 struct frame *f;
9943 {
9944 int i, size, size_needed;
9945 struct gcpro gcpro1, gcpro2, gcpro3;
9946 Lisp_Object image, plist, props;
9947
9948 image = plist = props = Qnil;
9949 GCPRO3 (image, plist, props);
9950
9951 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9952 Otherwise, make a new string. */
9953
9954 /* The size of the string we might be able to reuse. */
9955 size = (STRINGP (f->desired_tool_bar_string)
9956 ? SCHARS (f->desired_tool_bar_string)
9957 : 0);
9958
9959 /* We need one space in the string for each image. */
9960 size_needed = f->n_tool_bar_items;
9961
9962 /* Reuse f->desired_tool_bar_string, if possible. */
9963 if (size < size_needed || NILP (f->desired_tool_bar_string))
9964 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9965 make_number (' '));
9966 else
9967 {
9968 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9969 Fremove_text_properties (make_number (0), make_number (size),
9970 props, f->desired_tool_bar_string);
9971 }
9972
9973 /* Put a `display' property on the string for the images to display,
9974 put a `menu_item' property on tool-bar items with a value that
9975 is the index of the item in F's tool-bar item vector. */
9976 for (i = 0; i < f->n_tool_bar_items; ++i)
9977 {
9978 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9979
9980 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9981 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9982 int hmargin, vmargin, relief, idx, end;
9983 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9984
9985 /* If image is a vector, choose the image according to the
9986 button state. */
9987 image = PROP (TOOL_BAR_ITEM_IMAGES);
9988 if (VECTORP (image))
9989 {
9990 if (enabled_p)
9991 idx = (selected_p
9992 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9993 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9994 else
9995 idx = (selected_p
9996 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9997 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9998
9999 xassert (ASIZE (image) >= idx);
10000 image = AREF (image, idx);
10001 }
10002 else
10003 idx = -1;
10004
10005 /* Ignore invalid image specifications. */
10006 if (!valid_image_p (image))
10007 continue;
10008
10009 /* Display the tool-bar button pressed, or depressed. */
10010 plist = Fcopy_sequence (XCDR (image));
10011
10012 /* Compute margin and relief to draw. */
10013 relief = (tool_bar_button_relief >= 0
10014 ? tool_bar_button_relief
10015 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10016 hmargin = vmargin = relief;
10017
10018 if (INTEGERP (Vtool_bar_button_margin)
10019 && XINT (Vtool_bar_button_margin) > 0)
10020 {
10021 hmargin += XFASTINT (Vtool_bar_button_margin);
10022 vmargin += XFASTINT (Vtool_bar_button_margin);
10023 }
10024 else if (CONSP (Vtool_bar_button_margin))
10025 {
10026 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10027 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10028 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10029
10030 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10031 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10032 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10033 }
10034
10035 if (auto_raise_tool_bar_buttons_p)
10036 {
10037 /* Add a `:relief' property to the image spec if the item is
10038 selected. */
10039 if (selected_p)
10040 {
10041 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10042 hmargin -= relief;
10043 vmargin -= relief;
10044 }
10045 }
10046 else
10047 {
10048 /* If image is selected, display it pressed, i.e. with a
10049 negative relief. If it's not selected, display it with a
10050 raised relief. */
10051 plist = Fplist_put (plist, QCrelief,
10052 (selected_p
10053 ? make_number (-relief)
10054 : make_number (relief)));
10055 hmargin -= relief;
10056 vmargin -= relief;
10057 }
10058
10059 /* Put a margin around the image. */
10060 if (hmargin || vmargin)
10061 {
10062 if (hmargin == vmargin)
10063 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10064 else
10065 plist = Fplist_put (plist, QCmargin,
10066 Fcons (make_number (hmargin),
10067 make_number (vmargin)));
10068 }
10069
10070 /* If button is not enabled, and we don't have special images
10071 for the disabled state, make the image appear disabled by
10072 applying an appropriate algorithm to it. */
10073 if (!enabled_p && idx < 0)
10074 plist = Fplist_put (plist, QCconversion, Qdisabled);
10075
10076 /* Put a `display' text property on the string for the image to
10077 display. Put a `menu-item' property on the string that gives
10078 the start of this item's properties in the tool-bar items
10079 vector. */
10080 image = Fcons (Qimage, plist);
10081 props = list4 (Qdisplay, image,
10082 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10083
10084 /* Let the last image hide all remaining spaces in the tool bar
10085 string. The string can be longer than needed when we reuse a
10086 previous string. */
10087 if (i + 1 == f->n_tool_bar_items)
10088 end = SCHARS (f->desired_tool_bar_string);
10089 else
10090 end = i + 1;
10091 Fadd_text_properties (make_number (i), make_number (end),
10092 props, f->desired_tool_bar_string);
10093 #undef PROP
10094 }
10095
10096 UNGCPRO;
10097 }
10098
10099
10100 /* Display one line of the tool-bar of frame IT->f.
10101
10102 HEIGHT specifies the desired height of the tool-bar line.
10103 If the actual height of the glyph row is less than HEIGHT, the
10104 row's height is increased to HEIGHT, and the icons are centered
10105 vertically in the new height.
10106
10107 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10108 count a final empty row in case the tool-bar width exactly matches
10109 the window width.
10110 */
10111
10112 static void
10113 display_tool_bar_line (it, height)
10114 struct it *it;
10115 int height;
10116 {
10117 struct glyph_row *row = it->glyph_row;
10118 int max_x = it->last_visible_x;
10119 struct glyph *last;
10120
10121 prepare_desired_row (row);
10122 row->y = it->current_y;
10123
10124 /* Note that this isn't made use of if the face hasn't a box,
10125 so there's no need to check the face here. */
10126 it->start_of_box_run_p = 1;
10127
10128 while (it->current_x < max_x)
10129 {
10130 int x, n_glyphs_before, i, nglyphs;
10131 struct it it_before;
10132
10133 /* Get the next display element. */
10134 if (!get_next_display_element (it))
10135 {
10136 /* Don't count empty row if we are counting needed tool-bar lines. */
10137 if (height < 0 && !it->hpos)
10138 return;
10139 break;
10140 }
10141
10142 /* Produce glyphs. */
10143 n_glyphs_before = row->used[TEXT_AREA];
10144 it_before = *it;
10145
10146 PRODUCE_GLYPHS (it);
10147
10148 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10149 i = 0;
10150 x = it_before.current_x;
10151 while (i < nglyphs)
10152 {
10153 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10154
10155 if (x + glyph->pixel_width > max_x)
10156 {
10157 /* Glyph doesn't fit on line. Backtrack. */
10158 row->used[TEXT_AREA] = n_glyphs_before;
10159 *it = it_before;
10160 /* If this is the only glyph on this line, it will never fit on the
10161 toolbar, so skip it. But ensure there is at least one glyph,
10162 so we don't accidentally disable the tool-bar. */
10163 if (n_glyphs_before == 0
10164 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10165 break;
10166 goto out;
10167 }
10168
10169 ++it->hpos;
10170 x += glyph->pixel_width;
10171 ++i;
10172 }
10173
10174 /* Stop at line ends. */
10175 if (ITERATOR_AT_END_OF_LINE_P (it))
10176 break;
10177
10178 set_iterator_to_next (it, 1);
10179 }
10180
10181 out:;
10182
10183 row->displays_text_p = row->used[TEXT_AREA] != 0;
10184
10185 /* Use default face for the border below the tool bar.
10186
10187 FIXME: When auto-resize-tool-bars is grow-only, there is
10188 no additional border below the possibly empty tool-bar lines.
10189 So to make the extra empty lines look "normal", we have to
10190 use the tool-bar face for the border too. */
10191 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10192 it->face_id = DEFAULT_FACE_ID;
10193
10194 extend_face_to_end_of_line (it);
10195 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10196 last->right_box_line_p = 1;
10197 if (last == row->glyphs[TEXT_AREA])
10198 last->left_box_line_p = 1;
10199
10200 /* Make line the desired height and center it vertically. */
10201 if ((height -= it->max_ascent + it->max_descent) > 0)
10202 {
10203 /* Don't add more than one line height. */
10204 height %= FRAME_LINE_HEIGHT (it->f);
10205 it->max_ascent += height / 2;
10206 it->max_descent += (height + 1) / 2;
10207 }
10208
10209 compute_line_metrics (it);
10210
10211 /* If line is empty, make it occupy the rest of the tool-bar. */
10212 if (!row->displays_text_p)
10213 {
10214 row->height = row->phys_height = it->last_visible_y - row->y;
10215 row->visible_height = row->height;
10216 row->ascent = row->phys_ascent = 0;
10217 row->extra_line_spacing = 0;
10218 }
10219
10220 row->full_width_p = 1;
10221 row->continued_p = 0;
10222 row->truncated_on_left_p = 0;
10223 row->truncated_on_right_p = 0;
10224
10225 it->current_x = it->hpos = 0;
10226 it->current_y += row->height;
10227 ++it->vpos;
10228 ++it->glyph_row;
10229 }
10230
10231
10232 /* Max tool-bar height. */
10233
10234 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10235 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10236
10237 /* Value is the number of screen lines needed to make all tool-bar
10238 items of frame F visible. The number of actual rows needed is
10239 returned in *N_ROWS if non-NULL. */
10240
10241 static int
10242 tool_bar_lines_needed (f, n_rows)
10243 struct frame *f;
10244 int *n_rows;
10245 {
10246 struct window *w = XWINDOW (f->tool_bar_window);
10247 struct it it;
10248 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10249 the desired matrix, so use (unused) mode-line row as temporary row to
10250 avoid destroying the first tool-bar row. */
10251 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10252
10253 /* Initialize an iterator for iteration over
10254 F->desired_tool_bar_string in the tool-bar window of frame F. */
10255 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10256 it.first_visible_x = 0;
10257 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10258 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10259
10260 while (!ITERATOR_AT_END_P (&it))
10261 {
10262 clear_glyph_row (temp_row);
10263 it.glyph_row = temp_row;
10264 display_tool_bar_line (&it, -1);
10265 }
10266 clear_glyph_row (temp_row);
10267
10268 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10269 if (n_rows)
10270 *n_rows = it.vpos > 0 ? it.vpos : -1;
10271
10272 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10273 }
10274
10275
10276 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10277 0, 1, 0,
10278 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10279 (frame)
10280 Lisp_Object frame;
10281 {
10282 struct frame *f;
10283 struct window *w;
10284 int nlines = 0;
10285
10286 if (NILP (frame))
10287 frame = selected_frame;
10288 else
10289 CHECK_FRAME (frame);
10290 f = XFRAME (frame);
10291
10292 if (WINDOWP (f->tool_bar_window)
10293 || (w = XWINDOW (f->tool_bar_window),
10294 WINDOW_TOTAL_LINES (w) > 0))
10295 {
10296 update_tool_bar (f, 1);
10297 if (f->n_tool_bar_items)
10298 {
10299 build_desired_tool_bar_string (f);
10300 nlines = tool_bar_lines_needed (f, NULL);
10301 }
10302 }
10303
10304 return make_number (nlines);
10305 }
10306
10307
10308 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10309 height should be changed. */
10310
10311 static int
10312 redisplay_tool_bar (f)
10313 struct frame *f;
10314 {
10315 struct window *w;
10316 struct it it;
10317 struct glyph_row *row;
10318
10319 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
10320 if (FRAME_EXTERNAL_TOOL_BAR (f))
10321 update_frame_tool_bar (f);
10322 return 0;
10323 #endif
10324
10325 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10326 do anything. This means you must start with tool-bar-lines
10327 non-zero to get the auto-sizing effect. Or in other words, you
10328 can turn off tool-bars by specifying tool-bar-lines zero. */
10329 if (!WINDOWP (f->tool_bar_window)
10330 || (w = XWINDOW (f->tool_bar_window),
10331 WINDOW_TOTAL_LINES (w) == 0))
10332 return 0;
10333
10334 /* Set up an iterator for the tool-bar window. */
10335 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10336 it.first_visible_x = 0;
10337 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10338 row = it.glyph_row;
10339
10340 /* Build a string that represents the contents of the tool-bar. */
10341 build_desired_tool_bar_string (f);
10342 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10343
10344 if (f->n_tool_bar_rows == 0)
10345 {
10346 int nlines;
10347
10348 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10349 nlines != WINDOW_TOTAL_LINES (w)))
10350 {
10351 extern Lisp_Object Qtool_bar_lines;
10352 Lisp_Object frame;
10353 int old_height = WINDOW_TOTAL_LINES (w);
10354
10355 XSETFRAME (frame, f);
10356 Fmodify_frame_parameters (frame,
10357 Fcons (Fcons (Qtool_bar_lines,
10358 make_number (nlines)),
10359 Qnil));
10360 if (WINDOW_TOTAL_LINES (w) != old_height)
10361 {
10362 clear_glyph_matrix (w->desired_matrix);
10363 fonts_changed_p = 1;
10364 return 1;
10365 }
10366 }
10367 }
10368
10369 /* Display as many lines as needed to display all tool-bar items. */
10370
10371 if (f->n_tool_bar_rows > 0)
10372 {
10373 int border, rows, height, extra;
10374
10375 if (INTEGERP (Vtool_bar_border))
10376 border = XINT (Vtool_bar_border);
10377 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10378 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10379 else if (EQ (Vtool_bar_border, Qborder_width))
10380 border = f->border_width;
10381 else
10382 border = 0;
10383 if (border < 0)
10384 border = 0;
10385
10386 rows = f->n_tool_bar_rows;
10387 height = max (1, (it.last_visible_y - border) / rows);
10388 extra = it.last_visible_y - border - height * rows;
10389
10390 while (it.current_y < it.last_visible_y)
10391 {
10392 int h = 0;
10393 if (extra > 0 && rows-- > 0)
10394 {
10395 h = (extra + rows - 1) / rows;
10396 extra -= h;
10397 }
10398 display_tool_bar_line (&it, height + h);
10399 }
10400 }
10401 else
10402 {
10403 while (it.current_y < it.last_visible_y)
10404 display_tool_bar_line (&it, 0);
10405 }
10406
10407 /* It doesn't make much sense to try scrolling in the tool-bar
10408 window, so don't do it. */
10409 w->desired_matrix->no_scrolling_p = 1;
10410 w->must_be_updated_p = 1;
10411
10412 if (!NILP (Vauto_resize_tool_bars))
10413 {
10414 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10415 int change_height_p = 0;
10416
10417 /* If we couldn't display everything, change the tool-bar's
10418 height if there is room for more. */
10419 if (IT_STRING_CHARPOS (it) < it.end_charpos
10420 && it.current_y < max_tool_bar_height)
10421 change_height_p = 1;
10422
10423 row = it.glyph_row - 1;
10424
10425 /* If there are blank lines at the end, except for a partially
10426 visible blank line at the end that is smaller than
10427 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10428 if (!row->displays_text_p
10429 && row->height >= FRAME_LINE_HEIGHT (f))
10430 change_height_p = 1;
10431
10432 /* If row displays tool-bar items, but is partially visible,
10433 change the tool-bar's height. */
10434 if (row->displays_text_p
10435 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10436 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10437 change_height_p = 1;
10438
10439 /* Resize windows as needed by changing the `tool-bar-lines'
10440 frame parameter. */
10441 if (change_height_p)
10442 {
10443 extern Lisp_Object Qtool_bar_lines;
10444 Lisp_Object frame;
10445 int old_height = WINDOW_TOTAL_LINES (w);
10446 int nrows;
10447 int nlines = tool_bar_lines_needed (f, &nrows);
10448
10449 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10450 && !f->minimize_tool_bar_window_p)
10451 ? (nlines > old_height)
10452 : (nlines != old_height));
10453 f->minimize_tool_bar_window_p = 0;
10454
10455 if (change_height_p)
10456 {
10457 XSETFRAME (frame, f);
10458 Fmodify_frame_parameters (frame,
10459 Fcons (Fcons (Qtool_bar_lines,
10460 make_number (nlines)),
10461 Qnil));
10462 if (WINDOW_TOTAL_LINES (w) != old_height)
10463 {
10464 clear_glyph_matrix (w->desired_matrix);
10465 f->n_tool_bar_rows = nrows;
10466 fonts_changed_p = 1;
10467 return 1;
10468 }
10469 }
10470 }
10471 }
10472
10473 f->minimize_tool_bar_window_p = 0;
10474 return 0;
10475 }
10476
10477
10478 /* Get information about the tool-bar item which is displayed in GLYPH
10479 on frame F. Return in *PROP_IDX the index where tool-bar item
10480 properties start in F->tool_bar_items. Value is zero if
10481 GLYPH doesn't display a tool-bar item. */
10482
10483 static int
10484 tool_bar_item_info (f, glyph, prop_idx)
10485 struct frame *f;
10486 struct glyph *glyph;
10487 int *prop_idx;
10488 {
10489 Lisp_Object prop;
10490 int success_p;
10491 int charpos;
10492
10493 /* This function can be called asynchronously, which means we must
10494 exclude any possibility that Fget_text_property signals an
10495 error. */
10496 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10497 charpos = max (0, charpos);
10498
10499 /* Get the text property `menu-item' at pos. The value of that
10500 property is the start index of this item's properties in
10501 F->tool_bar_items. */
10502 prop = Fget_text_property (make_number (charpos),
10503 Qmenu_item, f->current_tool_bar_string);
10504 if (INTEGERP (prop))
10505 {
10506 *prop_idx = XINT (prop);
10507 success_p = 1;
10508 }
10509 else
10510 success_p = 0;
10511
10512 return success_p;
10513 }
10514
10515 \f
10516 /* Get information about the tool-bar item at position X/Y on frame F.
10517 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10518 the current matrix of the tool-bar window of F, or NULL if not
10519 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10520 item in F->tool_bar_items. Value is
10521
10522 -1 if X/Y is not on a tool-bar item
10523 0 if X/Y is on the same item that was highlighted before.
10524 1 otherwise. */
10525
10526 static int
10527 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10528 struct frame *f;
10529 int x, y;
10530 struct glyph **glyph;
10531 int *hpos, *vpos, *prop_idx;
10532 {
10533 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10534 struct window *w = XWINDOW (f->tool_bar_window);
10535 int area;
10536
10537 /* Find the glyph under X/Y. */
10538 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10539 if (*glyph == NULL)
10540 return -1;
10541
10542 /* Get the start of this tool-bar item's properties in
10543 f->tool_bar_items. */
10544 if (!tool_bar_item_info (f, *glyph, prop_idx))
10545 return -1;
10546
10547 /* Is mouse on the highlighted item? */
10548 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10549 && *vpos >= dpyinfo->mouse_face_beg_row
10550 && *vpos <= dpyinfo->mouse_face_end_row
10551 && (*vpos > dpyinfo->mouse_face_beg_row
10552 || *hpos >= dpyinfo->mouse_face_beg_col)
10553 && (*vpos < dpyinfo->mouse_face_end_row
10554 || *hpos < dpyinfo->mouse_face_end_col
10555 || dpyinfo->mouse_face_past_end))
10556 return 0;
10557
10558 return 1;
10559 }
10560
10561
10562 /* EXPORT:
10563 Handle mouse button event on the tool-bar of frame F, at
10564 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10565 0 for button release. MODIFIERS is event modifiers for button
10566 release. */
10567
10568 void
10569 handle_tool_bar_click (f, x, y, down_p, modifiers)
10570 struct frame *f;
10571 int x, y, down_p;
10572 unsigned int modifiers;
10573 {
10574 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10575 struct window *w = XWINDOW (f->tool_bar_window);
10576 int hpos, vpos, prop_idx;
10577 struct glyph *glyph;
10578 Lisp_Object enabled_p;
10579
10580 /* If not on the highlighted tool-bar item, return. */
10581 frame_to_window_pixel_xy (w, &x, &y);
10582 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10583 return;
10584
10585 /* If item is disabled, do nothing. */
10586 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10587 if (NILP (enabled_p))
10588 return;
10589
10590 if (down_p)
10591 {
10592 /* Show item in pressed state. */
10593 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10594 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10595 last_tool_bar_item = prop_idx;
10596 }
10597 else
10598 {
10599 Lisp_Object key, frame;
10600 struct input_event event;
10601 EVENT_INIT (event);
10602
10603 /* Show item in released state. */
10604 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10605 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10606
10607 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10608
10609 XSETFRAME (frame, f);
10610 event.kind = TOOL_BAR_EVENT;
10611 event.frame_or_window = frame;
10612 event.arg = frame;
10613 kbd_buffer_store_event (&event);
10614
10615 event.kind = TOOL_BAR_EVENT;
10616 event.frame_or_window = frame;
10617 event.arg = key;
10618 event.modifiers = modifiers;
10619 kbd_buffer_store_event (&event);
10620 last_tool_bar_item = -1;
10621 }
10622 }
10623
10624
10625 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10626 tool-bar window-relative coordinates X/Y. Called from
10627 note_mouse_highlight. */
10628
10629 static void
10630 note_tool_bar_highlight (f, x, y)
10631 struct frame *f;
10632 int x, y;
10633 {
10634 Lisp_Object window = f->tool_bar_window;
10635 struct window *w = XWINDOW (window);
10636 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10637 int hpos, vpos;
10638 struct glyph *glyph;
10639 struct glyph_row *row;
10640 int i;
10641 Lisp_Object enabled_p;
10642 int prop_idx;
10643 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10644 int mouse_down_p, rc;
10645
10646 /* Function note_mouse_highlight is called with negative x(y
10647 values when mouse moves outside of the frame. */
10648 if (x <= 0 || y <= 0)
10649 {
10650 clear_mouse_face (dpyinfo);
10651 return;
10652 }
10653
10654 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10655 if (rc < 0)
10656 {
10657 /* Not on tool-bar item. */
10658 clear_mouse_face (dpyinfo);
10659 return;
10660 }
10661 else if (rc == 0)
10662 /* On same tool-bar item as before. */
10663 goto set_help_echo;
10664
10665 clear_mouse_face (dpyinfo);
10666
10667 /* Mouse is down, but on different tool-bar item? */
10668 mouse_down_p = (dpyinfo->grabbed
10669 && f == last_mouse_frame
10670 && FRAME_LIVE_P (f));
10671 if (mouse_down_p
10672 && last_tool_bar_item != prop_idx)
10673 return;
10674
10675 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10676 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10677
10678 /* If tool-bar item is not enabled, don't highlight it. */
10679 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10680 if (!NILP (enabled_p))
10681 {
10682 /* Compute the x-position of the glyph. In front and past the
10683 image is a space. We include this in the highlighted area. */
10684 row = MATRIX_ROW (w->current_matrix, vpos);
10685 for (i = x = 0; i < hpos; ++i)
10686 x += row->glyphs[TEXT_AREA][i].pixel_width;
10687
10688 /* Record this as the current active region. */
10689 dpyinfo->mouse_face_beg_col = hpos;
10690 dpyinfo->mouse_face_beg_row = vpos;
10691 dpyinfo->mouse_face_beg_x = x;
10692 dpyinfo->mouse_face_beg_y = row->y;
10693 dpyinfo->mouse_face_past_end = 0;
10694
10695 dpyinfo->mouse_face_end_col = hpos + 1;
10696 dpyinfo->mouse_face_end_row = vpos;
10697 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10698 dpyinfo->mouse_face_end_y = row->y;
10699 dpyinfo->mouse_face_window = window;
10700 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10701
10702 /* Display it as active. */
10703 show_mouse_face (dpyinfo, draw);
10704 dpyinfo->mouse_face_image_state = draw;
10705 }
10706
10707 set_help_echo:
10708
10709 /* Set help_echo_string to a help string to display for this tool-bar item.
10710 XTread_socket does the rest. */
10711 help_echo_object = help_echo_window = Qnil;
10712 help_echo_pos = -1;
10713 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10714 if (NILP (help_echo_string))
10715 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10716 }
10717
10718 #endif /* HAVE_WINDOW_SYSTEM */
10719
10720
10721 \f
10722 /************************************************************************
10723 Horizontal scrolling
10724 ************************************************************************/
10725
10726 static int hscroll_window_tree P_ ((Lisp_Object));
10727 static int hscroll_windows P_ ((Lisp_Object));
10728
10729 /* For all leaf windows in the window tree rooted at WINDOW, set their
10730 hscroll value so that PT is (i) visible in the window, and (ii) so
10731 that it is not within a certain margin at the window's left and
10732 right border. Value is non-zero if any window's hscroll has been
10733 changed. */
10734
10735 static int
10736 hscroll_window_tree (window)
10737 Lisp_Object window;
10738 {
10739 int hscrolled_p = 0;
10740 int hscroll_relative_p = FLOATP (Vhscroll_step);
10741 int hscroll_step_abs = 0;
10742 double hscroll_step_rel = 0;
10743
10744 if (hscroll_relative_p)
10745 {
10746 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10747 if (hscroll_step_rel < 0)
10748 {
10749 hscroll_relative_p = 0;
10750 hscroll_step_abs = 0;
10751 }
10752 }
10753 else if (INTEGERP (Vhscroll_step))
10754 {
10755 hscroll_step_abs = XINT (Vhscroll_step);
10756 if (hscroll_step_abs < 0)
10757 hscroll_step_abs = 0;
10758 }
10759 else
10760 hscroll_step_abs = 0;
10761
10762 while (WINDOWP (window))
10763 {
10764 struct window *w = XWINDOW (window);
10765
10766 if (WINDOWP (w->hchild))
10767 hscrolled_p |= hscroll_window_tree (w->hchild);
10768 else if (WINDOWP (w->vchild))
10769 hscrolled_p |= hscroll_window_tree (w->vchild);
10770 else if (w->cursor.vpos >= 0)
10771 {
10772 int h_margin;
10773 int text_area_width;
10774 struct glyph_row *current_cursor_row
10775 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10776 struct glyph_row *desired_cursor_row
10777 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10778 struct glyph_row *cursor_row
10779 = (desired_cursor_row->enabled_p
10780 ? desired_cursor_row
10781 : current_cursor_row);
10782
10783 text_area_width = window_box_width (w, TEXT_AREA);
10784
10785 /* Scroll when cursor is inside this scroll margin. */
10786 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10787
10788 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10789 && ((XFASTINT (w->hscroll)
10790 && w->cursor.x <= h_margin)
10791 || (cursor_row->enabled_p
10792 && cursor_row->truncated_on_right_p
10793 && (w->cursor.x >= text_area_width - h_margin))))
10794 {
10795 struct it it;
10796 int hscroll;
10797 struct buffer *saved_current_buffer;
10798 int pt;
10799 int wanted_x;
10800
10801 /* Find point in a display of infinite width. */
10802 saved_current_buffer = current_buffer;
10803 current_buffer = XBUFFER (w->buffer);
10804
10805 if (w == XWINDOW (selected_window))
10806 pt = BUF_PT (current_buffer);
10807 else
10808 {
10809 pt = marker_position (w->pointm);
10810 pt = max (BEGV, pt);
10811 pt = min (ZV, pt);
10812 }
10813
10814 /* Move iterator to pt starting at cursor_row->start in
10815 a line with infinite width. */
10816 init_to_row_start (&it, w, cursor_row);
10817 it.last_visible_x = INFINITY;
10818 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10819 current_buffer = saved_current_buffer;
10820
10821 /* Position cursor in window. */
10822 if (!hscroll_relative_p && hscroll_step_abs == 0)
10823 hscroll = max (0, (it.current_x
10824 - (ITERATOR_AT_END_OF_LINE_P (&it)
10825 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10826 : (text_area_width / 2))))
10827 / FRAME_COLUMN_WIDTH (it.f);
10828 else if (w->cursor.x >= text_area_width - h_margin)
10829 {
10830 if (hscroll_relative_p)
10831 wanted_x = text_area_width * (1 - hscroll_step_rel)
10832 - h_margin;
10833 else
10834 wanted_x = text_area_width
10835 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10836 - h_margin;
10837 hscroll
10838 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10839 }
10840 else
10841 {
10842 if (hscroll_relative_p)
10843 wanted_x = text_area_width * hscroll_step_rel
10844 + h_margin;
10845 else
10846 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10847 + h_margin;
10848 hscroll
10849 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10850 }
10851 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10852
10853 /* Don't call Fset_window_hscroll if value hasn't
10854 changed because it will prevent redisplay
10855 optimizations. */
10856 if (XFASTINT (w->hscroll) != hscroll)
10857 {
10858 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10859 w->hscroll = make_number (hscroll);
10860 hscrolled_p = 1;
10861 }
10862 }
10863 }
10864
10865 window = w->next;
10866 }
10867
10868 /* Value is non-zero if hscroll of any leaf window has been changed. */
10869 return hscrolled_p;
10870 }
10871
10872
10873 /* Set hscroll so that cursor is visible and not inside horizontal
10874 scroll margins for all windows in the tree rooted at WINDOW. See
10875 also hscroll_window_tree above. Value is non-zero if any window's
10876 hscroll has been changed. If it has, desired matrices on the frame
10877 of WINDOW are cleared. */
10878
10879 static int
10880 hscroll_windows (window)
10881 Lisp_Object window;
10882 {
10883 int hscrolled_p = hscroll_window_tree (window);
10884 if (hscrolled_p)
10885 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10886 return hscrolled_p;
10887 }
10888
10889
10890 \f
10891 /************************************************************************
10892 Redisplay
10893 ************************************************************************/
10894
10895 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10896 to a non-zero value. This is sometimes handy to have in a debugger
10897 session. */
10898
10899 #if GLYPH_DEBUG
10900
10901 /* First and last unchanged row for try_window_id. */
10902
10903 int debug_first_unchanged_at_end_vpos;
10904 int debug_last_unchanged_at_beg_vpos;
10905
10906 /* Delta vpos and y. */
10907
10908 int debug_dvpos, debug_dy;
10909
10910 /* Delta in characters and bytes for try_window_id. */
10911
10912 int debug_delta, debug_delta_bytes;
10913
10914 /* Values of window_end_pos and window_end_vpos at the end of
10915 try_window_id. */
10916
10917 EMACS_INT debug_end_pos, debug_end_vpos;
10918
10919 /* Append a string to W->desired_matrix->method. FMT is a printf
10920 format string. A1...A9 are a supplement for a variable-length
10921 argument list. If trace_redisplay_p is non-zero also printf the
10922 resulting string to stderr. */
10923
10924 static void
10925 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10926 struct window *w;
10927 char *fmt;
10928 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10929 {
10930 char buffer[512];
10931 char *method = w->desired_matrix->method;
10932 int len = strlen (method);
10933 int size = sizeof w->desired_matrix->method;
10934 int remaining = size - len - 1;
10935
10936 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10937 if (len && remaining)
10938 {
10939 method[len] = '|';
10940 --remaining, ++len;
10941 }
10942
10943 strncpy (method + len, buffer, remaining);
10944
10945 if (trace_redisplay_p)
10946 fprintf (stderr, "%p (%s): %s\n",
10947 w,
10948 ((BUFFERP (w->buffer)
10949 && STRINGP (XBUFFER (w->buffer)->name))
10950 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10951 : "no buffer"),
10952 buffer);
10953 }
10954
10955 #endif /* GLYPH_DEBUG */
10956
10957
10958 /* Value is non-zero if all changes in window W, which displays
10959 current_buffer, are in the text between START and END. START is a
10960 buffer position, END is given as a distance from Z. Used in
10961 redisplay_internal for display optimization. */
10962
10963 static INLINE int
10964 text_outside_line_unchanged_p (w, start, end)
10965 struct window *w;
10966 int start, end;
10967 {
10968 int unchanged_p = 1;
10969
10970 /* If text or overlays have changed, see where. */
10971 if (XFASTINT (w->last_modified) < MODIFF
10972 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10973 {
10974 /* Gap in the line? */
10975 if (GPT < start || Z - GPT < end)
10976 unchanged_p = 0;
10977
10978 /* Changes start in front of the line, or end after it? */
10979 if (unchanged_p
10980 && (BEG_UNCHANGED < start - 1
10981 || END_UNCHANGED < end))
10982 unchanged_p = 0;
10983
10984 /* If selective display, can't optimize if changes start at the
10985 beginning of the line. */
10986 if (unchanged_p
10987 && INTEGERP (current_buffer->selective_display)
10988 && XINT (current_buffer->selective_display) > 0
10989 && (BEG_UNCHANGED < start || GPT <= start))
10990 unchanged_p = 0;
10991
10992 /* If there are overlays at the start or end of the line, these
10993 may have overlay strings with newlines in them. A change at
10994 START, for instance, may actually concern the display of such
10995 overlay strings as well, and they are displayed on different
10996 lines. So, quickly rule out this case. (For the future, it
10997 might be desirable to implement something more telling than
10998 just BEG/END_UNCHANGED.) */
10999 if (unchanged_p)
11000 {
11001 if (BEG + BEG_UNCHANGED == start
11002 && overlay_touches_p (start))
11003 unchanged_p = 0;
11004 if (END_UNCHANGED == end
11005 && overlay_touches_p (Z - end))
11006 unchanged_p = 0;
11007 }
11008 }
11009
11010 return unchanged_p;
11011 }
11012
11013
11014 /* Do a frame update, taking possible shortcuts into account. This is
11015 the main external entry point for redisplay.
11016
11017 If the last redisplay displayed an echo area message and that message
11018 is no longer requested, we clear the echo area or bring back the
11019 mini-buffer if that is in use. */
11020
11021 void
11022 redisplay ()
11023 {
11024 redisplay_internal (0);
11025 }
11026
11027
11028 static Lisp_Object
11029 overlay_arrow_string_or_property (var)
11030 Lisp_Object var;
11031 {
11032 Lisp_Object val;
11033
11034 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11035 return val;
11036
11037 return Voverlay_arrow_string;
11038 }
11039
11040 /* Return 1 if there are any overlay-arrows in current_buffer. */
11041 static int
11042 overlay_arrow_in_current_buffer_p ()
11043 {
11044 Lisp_Object vlist;
11045
11046 for (vlist = Voverlay_arrow_variable_list;
11047 CONSP (vlist);
11048 vlist = XCDR (vlist))
11049 {
11050 Lisp_Object var = XCAR (vlist);
11051 Lisp_Object val;
11052
11053 if (!SYMBOLP (var))
11054 continue;
11055 val = find_symbol_value (var);
11056 if (MARKERP (val)
11057 && current_buffer == XMARKER (val)->buffer)
11058 return 1;
11059 }
11060 return 0;
11061 }
11062
11063
11064 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11065 has changed. */
11066
11067 static int
11068 overlay_arrows_changed_p ()
11069 {
11070 Lisp_Object vlist;
11071
11072 for (vlist = Voverlay_arrow_variable_list;
11073 CONSP (vlist);
11074 vlist = XCDR (vlist))
11075 {
11076 Lisp_Object var = XCAR (vlist);
11077 Lisp_Object val, pstr;
11078
11079 if (!SYMBOLP (var))
11080 continue;
11081 val = find_symbol_value (var);
11082 if (!MARKERP (val))
11083 continue;
11084 if (! EQ (COERCE_MARKER (val),
11085 Fget (var, Qlast_arrow_position))
11086 || ! (pstr = overlay_arrow_string_or_property (var),
11087 EQ (pstr, Fget (var, Qlast_arrow_string))))
11088 return 1;
11089 }
11090 return 0;
11091 }
11092
11093 /* Mark overlay arrows to be updated on next redisplay. */
11094
11095 static void
11096 update_overlay_arrows (up_to_date)
11097 int up_to_date;
11098 {
11099 Lisp_Object vlist;
11100
11101 for (vlist = Voverlay_arrow_variable_list;
11102 CONSP (vlist);
11103 vlist = XCDR (vlist))
11104 {
11105 Lisp_Object var = XCAR (vlist);
11106
11107 if (!SYMBOLP (var))
11108 continue;
11109
11110 if (up_to_date > 0)
11111 {
11112 Lisp_Object val = find_symbol_value (var);
11113 Fput (var, Qlast_arrow_position,
11114 COERCE_MARKER (val));
11115 Fput (var, Qlast_arrow_string,
11116 overlay_arrow_string_or_property (var));
11117 }
11118 else if (up_to_date < 0
11119 || !NILP (Fget (var, Qlast_arrow_position)))
11120 {
11121 Fput (var, Qlast_arrow_position, Qt);
11122 Fput (var, Qlast_arrow_string, Qt);
11123 }
11124 }
11125 }
11126
11127
11128 /* Return overlay arrow string to display at row.
11129 Return integer (bitmap number) for arrow bitmap in left fringe.
11130 Return nil if no overlay arrow. */
11131
11132 static Lisp_Object
11133 overlay_arrow_at_row (it, row)
11134 struct it *it;
11135 struct glyph_row *row;
11136 {
11137 Lisp_Object vlist;
11138
11139 for (vlist = Voverlay_arrow_variable_list;
11140 CONSP (vlist);
11141 vlist = XCDR (vlist))
11142 {
11143 Lisp_Object var = XCAR (vlist);
11144 Lisp_Object val;
11145
11146 if (!SYMBOLP (var))
11147 continue;
11148
11149 val = find_symbol_value (var);
11150
11151 if (MARKERP (val)
11152 && current_buffer == XMARKER (val)->buffer
11153 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11154 {
11155 if (FRAME_WINDOW_P (it->f)
11156 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11157 {
11158 #ifdef HAVE_WINDOW_SYSTEM
11159 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11160 {
11161 int fringe_bitmap;
11162 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11163 return make_number (fringe_bitmap);
11164 }
11165 #endif
11166 return make_number (-1); /* Use default arrow bitmap */
11167 }
11168 return overlay_arrow_string_or_property (var);
11169 }
11170 }
11171
11172 return Qnil;
11173 }
11174
11175 /* Return 1 if point moved out of or into a composition. Otherwise
11176 return 0. PREV_BUF and PREV_PT are the last point buffer and
11177 position. BUF and PT are the current point buffer and position. */
11178
11179 int
11180 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11181 struct buffer *prev_buf, *buf;
11182 int prev_pt, pt;
11183 {
11184 EMACS_INT start, end;
11185 Lisp_Object prop;
11186 Lisp_Object buffer;
11187
11188 XSETBUFFER (buffer, buf);
11189 /* Check a composition at the last point if point moved within the
11190 same buffer. */
11191 if (prev_buf == buf)
11192 {
11193 if (prev_pt == pt)
11194 /* Point didn't move. */
11195 return 0;
11196
11197 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11198 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11199 && COMPOSITION_VALID_P (start, end, prop)
11200 && start < prev_pt && end > prev_pt)
11201 /* The last point was within the composition. Return 1 iff
11202 point moved out of the composition. */
11203 return (pt <= start || pt >= end);
11204 }
11205
11206 /* Check a composition at the current point. */
11207 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11208 && find_composition (pt, -1, &start, &end, &prop, buffer)
11209 && COMPOSITION_VALID_P (start, end, prop)
11210 && start < pt && end > pt);
11211 }
11212
11213
11214 /* Reconsider the setting of B->clip_changed which is displayed
11215 in window W. */
11216
11217 static INLINE void
11218 reconsider_clip_changes (w, b)
11219 struct window *w;
11220 struct buffer *b;
11221 {
11222 if (b->clip_changed
11223 && !NILP (w->window_end_valid)
11224 && w->current_matrix->buffer == b
11225 && w->current_matrix->zv == BUF_ZV (b)
11226 && w->current_matrix->begv == BUF_BEGV (b))
11227 b->clip_changed = 0;
11228
11229 /* If display wasn't paused, and W is not a tool bar window, see if
11230 point has been moved into or out of a composition. In that case,
11231 we set b->clip_changed to 1 to force updating the screen. If
11232 b->clip_changed has already been set to 1, we can skip this
11233 check. */
11234 if (!b->clip_changed
11235 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11236 {
11237 int pt;
11238
11239 if (w == XWINDOW (selected_window))
11240 pt = BUF_PT (current_buffer);
11241 else
11242 pt = marker_position (w->pointm);
11243
11244 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11245 || pt != XINT (w->last_point))
11246 && check_point_in_composition (w->current_matrix->buffer,
11247 XINT (w->last_point),
11248 XBUFFER (w->buffer), pt))
11249 b->clip_changed = 1;
11250 }
11251 }
11252 \f
11253
11254 /* Select FRAME to forward the values of frame-local variables into C
11255 variables so that the redisplay routines can access those values
11256 directly. */
11257
11258 static void
11259 select_frame_for_redisplay (frame)
11260 Lisp_Object frame;
11261 {
11262 Lisp_Object tail, symbol, val;
11263 Lisp_Object old = selected_frame;
11264 struct Lisp_Symbol *sym;
11265
11266 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11267
11268 selected_frame = frame;
11269
11270 do
11271 {
11272 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11273 if (CONSP (XCAR (tail))
11274 && (symbol = XCAR (XCAR (tail)),
11275 SYMBOLP (symbol))
11276 && (sym = indirect_variable (XSYMBOL (symbol)),
11277 val = sym->value,
11278 (BUFFER_LOCAL_VALUEP (val)))
11279 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11280 /* Use find_symbol_value rather than Fsymbol_value
11281 to avoid an error if it is void. */
11282 find_symbol_value (symbol);
11283 } while (!EQ (frame, old) && (frame = old, 1));
11284 }
11285
11286
11287 #define STOP_POLLING \
11288 do { if (! polling_stopped_here) stop_polling (); \
11289 polling_stopped_here = 1; } while (0)
11290
11291 #define RESUME_POLLING \
11292 do { if (polling_stopped_here) start_polling (); \
11293 polling_stopped_here = 0; } while (0)
11294
11295
11296 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11297 response to any user action; therefore, we should preserve the echo
11298 area. (Actually, our caller does that job.) Perhaps in the future
11299 avoid recentering windows if it is not necessary; currently that
11300 causes some problems. */
11301
11302 static void
11303 redisplay_internal (preserve_echo_area)
11304 int preserve_echo_area;
11305 {
11306 struct window *w = XWINDOW (selected_window);
11307 struct frame *f;
11308 int pause;
11309 int must_finish = 0;
11310 struct text_pos tlbufpos, tlendpos;
11311 int number_of_visible_frames;
11312 int count, count1;
11313 struct frame *sf;
11314 int polling_stopped_here = 0;
11315 Lisp_Object old_frame = selected_frame;
11316
11317 /* Non-zero means redisplay has to consider all windows on all
11318 frames. Zero means, only selected_window is considered. */
11319 int consider_all_windows_p;
11320
11321 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11322
11323 /* No redisplay if running in batch mode or frame is not yet fully
11324 initialized, or redisplay is explicitly turned off by setting
11325 Vinhibit_redisplay. */
11326 if (noninteractive
11327 || !NILP (Vinhibit_redisplay))
11328 return;
11329
11330 /* Don't examine these until after testing Vinhibit_redisplay.
11331 When Emacs is shutting down, perhaps because its connection to
11332 X has dropped, we should not look at them at all. */
11333 f = XFRAME (w->frame);
11334 sf = SELECTED_FRAME ();
11335
11336 if (!f->glyphs_initialized_p)
11337 return;
11338
11339 /* The flag redisplay_performed_directly_p is set by
11340 direct_output_for_insert when it already did the whole screen
11341 update necessary. */
11342 if (redisplay_performed_directly_p)
11343 {
11344 redisplay_performed_directly_p = 0;
11345 if (!hscroll_windows (selected_window))
11346 return;
11347 }
11348
11349 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
11350 if (popup_activated ())
11351 return;
11352 #endif
11353
11354 /* I don't think this happens but let's be paranoid. */
11355 if (redisplaying_p)
11356 return;
11357
11358 /* Record a function that resets redisplaying_p to its old value
11359 when we leave this function. */
11360 count = SPECPDL_INDEX ();
11361 record_unwind_protect (unwind_redisplay,
11362 Fcons (make_number (redisplaying_p), selected_frame));
11363 ++redisplaying_p;
11364 specbind (Qinhibit_free_realized_faces, Qnil);
11365
11366 {
11367 Lisp_Object tail, frame;
11368
11369 FOR_EACH_FRAME (tail, frame)
11370 {
11371 struct frame *f = XFRAME (frame);
11372 f->already_hscrolled_p = 0;
11373 }
11374 }
11375
11376 retry:
11377 if (!EQ (old_frame, selected_frame)
11378 && FRAME_LIVE_P (XFRAME (old_frame)))
11379 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11380 selected_frame and selected_window to be temporarily out-of-sync so
11381 when we come back here via `goto retry', we need to resync because we
11382 may need to run Elisp code (via prepare_menu_bars). */
11383 select_frame_for_redisplay (old_frame);
11384
11385 pause = 0;
11386 reconsider_clip_changes (w, current_buffer);
11387 last_escape_glyph_frame = NULL;
11388 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11389
11390 /* If new fonts have been loaded that make a glyph matrix adjustment
11391 necessary, do it. */
11392 if (fonts_changed_p)
11393 {
11394 adjust_glyphs (NULL);
11395 ++windows_or_buffers_changed;
11396 fonts_changed_p = 0;
11397 }
11398
11399 /* If face_change_count is non-zero, init_iterator will free all
11400 realized faces, which includes the faces referenced from current
11401 matrices. So, we can't reuse current matrices in this case. */
11402 if (face_change_count)
11403 ++windows_or_buffers_changed;
11404
11405 if (FRAME_TERMCAP_P (sf)
11406 && FRAME_TTY (sf)->previous_frame != sf)
11407 {
11408 /* Since frames on a single ASCII terminal share the same
11409 display area, displaying a different frame means redisplay
11410 the whole thing. */
11411 windows_or_buffers_changed++;
11412 SET_FRAME_GARBAGED (sf);
11413 #ifndef WINDOWSNT
11414 set_tty_color_mode (FRAME_TTY (sf), sf);
11415 #endif
11416 FRAME_TTY (sf)->previous_frame = sf;
11417 }
11418
11419 /* Set the visible flags for all frames. Do this before checking
11420 for resized or garbaged frames; they want to know if their frames
11421 are visible. See the comment in frame.h for
11422 FRAME_SAMPLE_VISIBILITY. */
11423 {
11424 Lisp_Object tail, frame;
11425
11426 number_of_visible_frames = 0;
11427
11428 FOR_EACH_FRAME (tail, frame)
11429 {
11430 struct frame *f = XFRAME (frame);
11431
11432 FRAME_SAMPLE_VISIBILITY (f);
11433 if (FRAME_VISIBLE_P (f))
11434 ++number_of_visible_frames;
11435 clear_desired_matrices (f);
11436 }
11437 }
11438
11439
11440 /* Notice any pending interrupt request to change frame size. */
11441 do_pending_window_change (1);
11442
11443 /* Clear frames marked as garbaged. */
11444 if (frame_garbaged)
11445 clear_garbaged_frames ();
11446
11447 /* Build menubar and tool-bar items. */
11448 if (NILP (Vmemory_full))
11449 prepare_menu_bars ();
11450
11451 if (windows_or_buffers_changed)
11452 update_mode_lines++;
11453
11454 /* Detect case that we need to write or remove a star in the mode line. */
11455 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11456 {
11457 w->update_mode_line = Qt;
11458 if (buffer_shared > 1)
11459 update_mode_lines++;
11460 }
11461
11462 /* Avoid invocation of point motion hooks by `current_column' below. */
11463 count1 = SPECPDL_INDEX ();
11464 specbind (Qinhibit_point_motion_hooks, Qt);
11465
11466 /* If %c is in the mode line, update it if needed. */
11467 if (!NILP (w->column_number_displayed)
11468 /* This alternative quickly identifies a common case
11469 where no change is needed. */
11470 && !(PT == XFASTINT (w->last_point)
11471 && XFASTINT (w->last_modified) >= MODIFF
11472 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11473 && (XFASTINT (w->column_number_displayed)
11474 != (int) current_column ())) /* iftc */
11475 w->update_mode_line = Qt;
11476
11477 unbind_to (count1, Qnil);
11478
11479 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11480
11481 /* The variable buffer_shared is set in redisplay_window and
11482 indicates that we redisplay a buffer in different windows. See
11483 there. */
11484 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11485 || cursor_type_changed);
11486
11487 /* If specs for an arrow have changed, do thorough redisplay
11488 to ensure we remove any arrow that should no longer exist. */
11489 if (overlay_arrows_changed_p ())
11490 consider_all_windows_p = windows_or_buffers_changed = 1;
11491
11492 /* Normally the message* functions will have already displayed and
11493 updated the echo area, but the frame may have been trashed, or
11494 the update may have been preempted, so display the echo area
11495 again here. Checking message_cleared_p captures the case that
11496 the echo area should be cleared. */
11497 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11498 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11499 || (message_cleared_p
11500 && minibuf_level == 0
11501 /* If the mini-window is currently selected, this means the
11502 echo-area doesn't show through. */
11503 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11504 {
11505 int window_height_changed_p = echo_area_display (0);
11506 must_finish = 1;
11507
11508 /* If we don't display the current message, don't clear the
11509 message_cleared_p flag, because, if we did, we wouldn't clear
11510 the echo area in the next redisplay which doesn't preserve
11511 the echo area. */
11512 if (!display_last_displayed_message_p)
11513 message_cleared_p = 0;
11514
11515 if (fonts_changed_p)
11516 goto retry;
11517 else if (window_height_changed_p)
11518 {
11519 consider_all_windows_p = 1;
11520 ++update_mode_lines;
11521 ++windows_or_buffers_changed;
11522
11523 /* If window configuration was changed, frames may have been
11524 marked garbaged. Clear them or we will experience
11525 surprises wrt scrolling. */
11526 if (frame_garbaged)
11527 clear_garbaged_frames ();
11528 }
11529 }
11530 else if (EQ (selected_window, minibuf_window)
11531 && (current_buffer->clip_changed
11532 || XFASTINT (w->last_modified) < MODIFF
11533 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11534 && resize_mini_window (w, 0))
11535 {
11536 /* Resized active mini-window to fit the size of what it is
11537 showing if its contents might have changed. */
11538 must_finish = 1;
11539 /* FIXME: this causes all frames to be updated, which seems unnecessary
11540 since only the current frame needs to be considered. This function needs
11541 to be rewritten with two variables, consider_all_windows and
11542 consider_all_frames. */
11543 consider_all_windows_p = 1;
11544 ++windows_or_buffers_changed;
11545 ++update_mode_lines;
11546
11547 /* If window configuration was changed, frames may have been
11548 marked garbaged. Clear them or we will experience
11549 surprises wrt scrolling. */
11550 if (frame_garbaged)
11551 clear_garbaged_frames ();
11552 }
11553
11554
11555 /* If showing the region, and mark has changed, we must redisplay
11556 the whole window. The assignment to this_line_start_pos prevents
11557 the optimization directly below this if-statement. */
11558 if (((!NILP (Vtransient_mark_mode)
11559 && !NILP (XBUFFER (w->buffer)->mark_active))
11560 != !NILP (w->region_showing))
11561 || (!NILP (w->region_showing)
11562 && !EQ (w->region_showing,
11563 Fmarker_position (XBUFFER (w->buffer)->mark))))
11564 CHARPOS (this_line_start_pos) = 0;
11565
11566 /* Optimize the case that only the line containing the cursor in the
11567 selected window has changed. Variables starting with this_ are
11568 set in display_line and record information about the line
11569 containing the cursor. */
11570 tlbufpos = this_line_start_pos;
11571 tlendpos = this_line_end_pos;
11572 if (!consider_all_windows_p
11573 && CHARPOS (tlbufpos) > 0
11574 && NILP (w->update_mode_line)
11575 && !current_buffer->clip_changed
11576 && !current_buffer->prevent_redisplay_optimizations_p
11577 && FRAME_VISIBLE_P (XFRAME (w->frame))
11578 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11579 /* Make sure recorded data applies to current buffer, etc. */
11580 && this_line_buffer == current_buffer
11581 && current_buffer == XBUFFER (w->buffer)
11582 && NILP (w->force_start)
11583 && NILP (w->optional_new_start)
11584 /* Point must be on the line that we have info recorded about. */
11585 && PT >= CHARPOS (tlbufpos)
11586 && PT <= Z - CHARPOS (tlendpos)
11587 /* All text outside that line, including its final newline,
11588 must be unchanged */
11589 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11590 CHARPOS (tlendpos)))
11591 {
11592 if (CHARPOS (tlbufpos) > BEGV
11593 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11594 && (CHARPOS (tlbufpos) == ZV
11595 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11596 /* Former continuation line has disappeared by becoming empty */
11597 goto cancel;
11598 else if (XFASTINT (w->last_modified) < MODIFF
11599 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11600 || MINI_WINDOW_P (w))
11601 {
11602 /* We have to handle the case of continuation around a
11603 wide-column character (See the comment in indent.c around
11604 line 885).
11605
11606 For instance, in the following case:
11607
11608 -------- Insert --------
11609 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11610 J_I_ ==> J_I_ `^^' are cursors.
11611 ^^ ^^
11612 -------- --------
11613
11614 As we have to redraw the line above, we should goto cancel. */
11615
11616 struct it it;
11617 int line_height_before = this_line_pixel_height;
11618
11619 /* Note that start_display will handle the case that the
11620 line starting at tlbufpos is a continuation lines. */
11621 start_display (&it, w, tlbufpos);
11622
11623 /* Implementation note: It this still necessary? */
11624 if (it.current_x != this_line_start_x)
11625 goto cancel;
11626
11627 TRACE ((stderr, "trying display optimization 1\n"));
11628 w->cursor.vpos = -1;
11629 overlay_arrow_seen = 0;
11630 it.vpos = this_line_vpos;
11631 it.current_y = this_line_y;
11632 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11633 display_line (&it);
11634
11635 /* If line contains point, is not continued,
11636 and ends at same distance from eob as before, we win */
11637 if (w->cursor.vpos >= 0
11638 /* Line is not continued, otherwise this_line_start_pos
11639 would have been set to 0 in display_line. */
11640 && CHARPOS (this_line_start_pos)
11641 /* Line ends as before. */
11642 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11643 /* Line has same height as before. Otherwise other lines
11644 would have to be shifted up or down. */
11645 && this_line_pixel_height == line_height_before)
11646 {
11647 /* If this is not the window's last line, we must adjust
11648 the charstarts of the lines below. */
11649 if (it.current_y < it.last_visible_y)
11650 {
11651 struct glyph_row *row
11652 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11653 int delta, delta_bytes;
11654
11655 if (Z - CHARPOS (tlendpos) == ZV)
11656 {
11657 /* This line ends at end of (accessible part of)
11658 buffer. There is no newline to count. */
11659 delta = (Z
11660 - CHARPOS (tlendpos)
11661 - MATRIX_ROW_START_CHARPOS (row));
11662 delta_bytes = (Z_BYTE
11663 - BYTEPOS (tlendpos)
11664 - MATRIX_ROW_START_BYTEPOS (row));
11665 }
11666 else
11667 {
11668 /* This line ends in a newline. Must take
11669 account of the newline and the rest of the
11670 text that follows. */
11671 delta = (Z
11672 - CHARPOS (tlendpos)
11673 - MATRIX_ROW_START_CHARPOS (row));
11674 delta_bytes = (Z_BYTE
11675 - BYTEPOS (tlendpos)
11676 - MATRIX_ROW_START_BYTEPOS (row));
11677 }
11678
11679 increment_matrix_positions (w->current_matrix,
11680 this_line_vpos + 1,
11681 w->current_matrix->nrows,
11682 delta, delta_bytes);
11683 }
11684
11685 /* If this row displays text now but previously didn't,
11686 or vice versa, w->window_end_vpos may have to be
11687 adjusted. */
11688 if ((it.glyph_row - 1)->displays_text_p)
11689 {
11690 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11691 XSETINT (w->window_end_vpos, this_line_vpos);
11692 }
11693 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11694 && this_line_vpos > 0)
11695 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11696 w->window_end_valid = Qnil;
11697
11698 /* Update hint: No need to try to scroll in update_window. */
11699 w->desired_matrix->no_scrolling_p = 1;
11700
11701 #if GLYPH_DEBUG
11702 *w->desired_matrix->method = 0;
11703 debug_method_add (w, "optimization 1");
11704 #endif
11705 #ifdef HAVE_WINDOW_SYSTEM
11706 update_window_fringes (w, 0);
11707 #endif
11708 goto update;
11709 }
11710 else
11711 goto cancel;
11712 }
11713 else if (/* Cursor position hasn't changed. */
11714 PT == XFASTINT (w->last_point)
11715 /* Make sure the cursor was last displayed
11716 in this window. Otherwise we have to reposition it. */
11717 && 0 <= w->cursor.vpos
11718 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11719 {
11720 if (!must_finish)
11721 {
11722 do_pending_window_change (1);
11723
11724 /* We used to always goto end_of_redisplay here, but this
11725 isn't enough if we have a blinking cursor. */
11726 if (w->cursor_off_p == w->last_cursor_off_p)
11727 goto end_of_redisplay;
11728 }
11729 goto update;
11730 }
11731 /* If highlighting the region, or if the cursor is in the echo area,
11732 then we can't just move the cursor. */
11733 else if (! (!NILP (Vtransient_mark_mode)
11734 && !NILP (current_buffer->mark_active))
11735 && (EQ (selected_window, current_buffer->last_selected_window)
11736 || highlight_nonselected_windows)
11737 && NILP (w->region_showing)
11738 && NILP (Vshow_trailing_whitespace)
11739 && !cursor_in_echo_area)
11740 {
11741 struct it it;
11742 struct glyph_row *row;
11743
11744 /* Skip from tlbufpos to PT and see where it is. Note that
11745 PT may be in invisible text. If so, we will end at the
11746 next visible position. */
11747 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11748 NULL, DEFAULT_FACE_ID);
11749 it.current_x = this_line_start_x;
11750 it.current_y = this_line_y;
11751 it.vpos = this_line_vpos;
11752
11753 /* The call to move_it_to stops in front of PT, but
11754 moves over before-strings. */
11755 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11756
11757 if (it.vpos == this_line_vpos
11758 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11759 row->enabled_p))
11760 {
11761 xassert (this_line_vpos == it.vpos);
11762 xassert (this_line_y == it.current_y);
11763 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11764 #if GLYPH_DEBUG
11765 *w->desired_matrix->method = 0;
11766 debug_method_add (w, "optimization 3");
11767 #endif
11768 goto update;
11769 }
11770 else
11771 goto cancel;
11772 }
11773
11774 cancel:
11775 /* Text changed drastically or point moved off of line. */
11776 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11777 }
11778
11779 CHARPOS (this_line_start_pos) = 0;
11780 consider_all_windows_p |= buffer_shared > 1;
11781 ++clear_face_cache_count;
11782 #ifdef HAVE_WINDOW_SYSTEM
11783 ++clear_image_cache_count;
11784 #endif
11785
11786 /* Build desired matrices, and update the display. If
11787 consider_all_windows_p is non-zero, do it for all windows on all
11788 frames. Otherwise do it for selected_window, only. */
11789
11790 if (consider_all_windows_p)
11791 {
11792 Lisp_Object tail, frame;
11793
11794 FOR_EACH_FRAME (tail, frame)
11795 XFRAME (frame)->updated_p = 0;
11796
11797 /* Recompute # windows showing selected buffer. This will be
11798 incremented each time such a window is displayed. */
11799 buffer_shared = 0;
11800
11801 FOR_EACH_FRAME (tail, frame)
11802 {
11803 struct frame *f = XFRAME (frame);
11804
11805 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11806 {
11807 if (! EQ (frame, selected_frame))
11808 /* Select the frame, for the sake of frame-local
11809 variables. */
11810 select_frame_for_redisplay (frame);
11811
11812 /* Mark all the scroll bars to be removed; we'll redeem
11813 the ones we want when we redisplay their windows. */
11814 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11815 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11816
11817 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11818 redisplay_windows (FRAME_ROOT_WINDOW (f));
11819
11820 /* Any scroll bars which redisplay_windows should have
11821 nuked should now go away. */
11822 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11823 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11824
11825 /* If fonts changed, display again. */
11826 /* ??? rms: I suspect it is a mistake to jump all the way
11827 back to retry here. It should just retry this frame. */
11828 if (fonts_changed_p)
11829 goto retry;
11830
11831 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11832 {
11833 /* See if we have to hscroll. */
11834 if (!f->already_hscrolled_p)
11835 {
11836 f->already_hscrolled_p = 1;
11837 if (hscroll_windows (f->root_window))
11838 goto retry;
11839 }
11840
11841 /* Prevent various kinds of signals during display
11842 update. stdio is not robust about handling
11843 signals, which can cause an apparent I/O
11844 error. */
11845 if (interrupt_input)
11846 unrequest_sigio ();
11847 STOP_POLLING;
11848
11849 /* Update the display. */
11850 set_window_update_flags (XWINDOW (f->root_window), 1);
11851 pause |= update_frame (f, 0, 0);
11852 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11853 if (pause)
11854 break;
11855 #endif
11856
11857 f->updated_p = 1;
11858 }
11859 }
11860 }
11861
11862 if (!EQ (old_frame, selected_frame)
11863 && FRAME_LIVE_P (XFRAME (old_frame)))
11864 /* We played a bit fast-and-loose above and allowed selected_frame
11865 and selected_window to be temporarily out-of-sync but let's make
11866 sure this stays contained. */
11867 select_frame_for_redisplay (old_frame);
11868 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11869
11870 if (!pause)
11871 {
11872 /* Do the mark_window_display_accurate after all windows have
11873 been redisplayed because this call resets flags in buffers
11874 which are needed for proper redisplay. */
11875 FOR_EACH_FRAME (tail, frame)
11876 {
11877 struct frame *f = XFRAME (frame);
11878 if (f->updated_p)
11879 {
11880 mark_window_display_accurate (f->root_window, 1);
11881 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11882 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11883 }
11884 }
11885 }
11886 }
11887 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11888 {
11889 Lisp_Object mini_window;
11890 struct frame *mini_frame;
11891
11892 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11893 /* Use list_of_error, not Qerror, so that
11894 we catch only errors and don't run the debugger. */
11895 internal_condition_case_1 (redisplay_window_1, selected_window,
11896 list_of_error,
11897 redisplay_window_error);
11898
11899 /* Compare desired and current matrices, perform output. */
11900
11901 update:
11902 /* If fonts changed, display again. */
11903 if (fonts_changed_p)
11904 goto retry;
11905
11906 /* Prevent various kinds of signals during display update.
11907 stdio is not robust about handling signals,
11908 which can cause an apparent I/O error. */
11909 if (interrupt_input)
11910 unrequest_sigio ();
11911 STOP_POLLING;
11912
11913 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11914 {
11915 if (hscroll_windows (selected_window))
11916 goto retry;
11917
11918 XWINDOW (selected_window)->must_be_updated_p = 1;
11919 pause = update_frame (sf, 0, 0);
11920 }
11921
11922 /* We may have called echo_area_display at the top of this
11923 function. If the echo area is on another frame, that may
11924 have put text on a frame other than the selected one, so the
11925 above call to update_frame would not have caught it. Catch
11926 it here. */
11927 mini_window = FRAME_MINIBUF_WINDOW (sf);
11928 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11929
11930 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11931 {
11932 XWINDOW (mini_window)->must_be_updated_p = 1;
11933 pause |= update_frame (mini_frame, 0, 0);
11934 if (!pause && hscroll_windows (mini_window))
11935 goto retry;
11936 }
11937 }
11938
11939 /* If display was paused because of pending input, make sure we do a
11940 thorough update the next time. */
11941 if (pause)
11942 {
11943 /* Prevent the optimization at the beginning of
11944 redisplay_internal that tries a single-line update of the
11945 line containing the cursor in the selected window. */
11946 CHARPOS (this_line_start_pos) = 0;
11947
11948 /* Let the overlay arrow be updated the next time. */
11949 update_overlay_arrows (0);
11950
11951 /* If we pause after scrolling, some rows in the current
11952 matrices of some windows are not valid. */
11953 if (!WINDOW_FULL_WIDTH_P (w)
11954 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11955 update_mode_lines = 1;
11956 }
11957 else
11958 {
11959 if (!consider_all_windows_p)
11960 {
11961 /* This has already been done above if
11962 consider_all_windows_p is set. */
11963 mark_window_display_accurate_1 (w, 1);
11964
11965 /* Say overlay arrows are up to date. */
11966 update_overlay_arrows (1);
11967
11968 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11969 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11970 }
11971
11972 update_mode_lines = 0;
11973 windows_or_buffers_changed = 0;
11974 cursor_type_changed = 0;
11975 }
11976
11977 /* Start SIGIO interrupts coming again. Having them off during the
11978 code above makes it less likely one will discard output, but not
11979 impossible, since there might be stuff in the system buffer here.
11980 But it is much hairier to try to do anything about that. */
11981 if (interrupt_input)
11982 request_sigio ();
11983 RESUME_POLLING;
11984
11985 /* If a frame has become visible which was not before, redisplay
11986 again, so that we display it. Expose events for such a frame
11987 (which it gets when becoming visible) don't call the parts of
11988 redisplay constructing glyphs, so simply exposing a frame won't
11989 display anything in this case. So, we have to display these
11990 frames here explicitly. */
11991 if (!pause)
11992 {
11993 Lisp_Object tail, frame;
11994 int new_count = 0;
11995
11996 FOR_EACH_FRAME (tail, frame)
11997 {
11998 int this_is_visible = 0;
11999
12000 if (XFRAME (frame)->visible)
12001 this_is_visible = 1;
12002 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12003 if (XFRAME (frame)->visible)
12004 this_is_visible = 1;
12005
12006 if (this_is_visible)
12007 new_count++;
12008 }
12009
12010 if (new_count != number_of_visible_frames)
12011 windows_or_buffers_changed++;
12012 }
12013
12014 /* Change frame size now if a change is pending. */
12015 do_pending_window_change (1);
12016
12017 /* If we just did a pending size change, or have additional
12018 visible frames, redisplay again. */
12019 if (windows_or_buffers_changed && !pause)
12020 goto retry;
12021
12022 /* Clear the face cache eventually. */
12023 if (consider_all_windows_p)
12024 {
12025 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12026 {
12027 clear_face_cache (0);
12028 clear_face_cache_count = 0;
12029 }
12030 #ifdef HAVE_WINDOW_SYSTEM
12031 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12032 {
12033 clear_image_caches (Qnil);
12034 clear_image_cache_count = 0;
12035 }
12036 #endif /* HAVE_WINDOW_SYSTEM */
12037 }
12038
12039 end_of_redisplay:
12040 unbind_to (count, Qnil);
12041 RESUME_POLLING;
12042 }
12043
12044
12045 /* Redisplay, but leave alone any recent echo area message unless
12046 another message has been requested in its place.
12047
12048 This is useful in situations where you need to redisplay but no
12049 user action has occurred, making it inappropriate for the message
12050 area to be cleared. See tracking_off and
12051 wait_reading_process_output for examples of these situations.
12052
12053 FROM_WHERE is an integer saying from where this function was
12054 called. This is useful for debugging. */
12055
12056 void
12057 redisplay_preserve_echo_area (from_where)
12058 int from_where;
12059 {
12060 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12061
12062 if (!NILP (echo_area_buffer[1]))
12063 {
12064 /* We have a previously displayed message, but no current
12065 message. Redisplay the previous message. */
12066 display_last_displayed_message_p = 1;
12067 redisplay_internal (1);
12068 display_last_displayed_message_p = 0;
12069 }
12070 else
12071 redisplay_internal (1);
12072
12073 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12074 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12075 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12076 }
12077
12078
12079 /* Function registered with record_unwind_protect in
12080 redisplay_internal. Reset redisplaying_p to the value it had
12081 before redisplay_internal was called, and clear
12082 prevent_freeing_realized_faces_p. It also selects the previously
12083 selected frame, unless it has been deleted (by an X connection
12084 failure during redisplay, for example). */
12085
12086 static Lisp_Object
12087 unwind_redisplay (val)
12088 Lisp_Object val;
12089 {
12090 Lisp_Object old_redisplaying_p, old_frame;
12091
12092 old_redisplaying_p = XCAR (val);
12093 redisplaying_p = XFASTINT (old_redisplaying_p);
12094 old_frame = XCDR (val);
12095 if (! EQ (old_frame, selected_frame)
12096 && FRAME_LIVE_P (XFRAME (old_frame)))
12097 select_frame_for_redisplay (old_frame);
12098 return Qnil;
12099 }
12100
12101
12102 /* Mark the display of window W as accurate or inaccurate. If
12103 ACCURATE_P is non-zero mark display of W as accurate. If
12104 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12105 redisplay_internal is called. */
12106
12107 static void
12108 mark_window_display_accurate_1 (w, accurate_p)
12109 struct window *w;
12110 int accurate_p;
12111 {
12112 if (BUFFERP (w->buffer))
12113 {
12114 struct buffer *b = XBUFFER (w->buffer);
12115
12116 w->last_modified
12117 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12118 w->last_overlay_modified
12119 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12120 w->last_had_star
12121 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12122
12123 if (accurate_p)
12124 {
12125 b->clip_changed = 0;
12126 b->prevent_redisplay_optimizations_p = 0;
12127
12128 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12129 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12130 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12131 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12132
12133 w->current_matrix->buffer = b;
12134 w->current_matrix->begv = BUF_BEGV (b);
12135 w->current_matrix->zv = BUF_ZV (b);
12136
12137 w->last_cursor = w->cursor;
12138 w->last_cursor_off_p = w->cursor_off_p;
12139
12140 if (w == XWINDOW (selected_window))
12141 w->last_point = make_number (BUF_PT (b));
12142 else
12143 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12144 }
12145 }
12146
12147 if (accurate_p)
12148 {
12149 w->window_end_valid = w->buffer;
12150 #if 0 /* This is incorrect with variable-height lines. */
12151 xassert (XINT (w->window_end_vpos)
12152 < (WINDOW_TOTAL_LINES (w)
12153 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
12154 #endif
12155 w->update_mode_line = Qnil;
12156 }
12157 }
12158
12159
12160 /* Mark the display of windows in the window tree rooted at WINDOW as
12161 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12162 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12163 be redisplayed the next time redisplay_internal is called. */
12164
12165 void
12166 mark_window_display_accurate (window, accurate_p)
12167 Lisp_Object window;
12168 int accurate_p;
12169 {
12170 struct window *w;
12171
12172 for (; !NILP (window); window = w->next)
12173 {
12174 w = XWINDOW (window);
12175 mark_window_display_accurate_1 (w, accurate_p);
12176
12177 if (!NILP (w->vchild))
12178 mark_window_display_accurate (w->vchild, accurate_p);
12179 if (!NILP (w->hchild))
12180 mark_window_display_accurate (w->hchild, accurate_p);
12181 }
12182
12183 if (accurate_p)
12184 {
12185 update_overlay_arrows (1);
12186 }
12187 else
12188 {
12189 /* Force a thorough redisplay the next time by setting
12190 last_arrow_position and last_arrow_string to t, which is
12191 unequal to any useful value of Voverlay_arrow_... */
12192 update_overlay_arrows (-1);
12193 }
12194 }
12195
12196
12197 /* Return value in display table DP (Lisp_Char_Table *) for character
12198 C. Since a display table doesn't have any parent, we don't have to
12199 follow parent. Do not call this function directly but use the
12200 macro DISP_CHAR_VECTOR. */
12201
12202 Lisp_Object
12203 disp_char_vector (dp, c)
12204 struct Lisp_Char_Table *dp;
12205 int c;
12206 {
12207 Lisp_Object val;
12208
12209 if (ASCII_CHAR_P (c))
12210 {
12211 val = dp->ascii;
12212 if (SUB_CHAR_TABLE_P (val))
12213 val = XSUB_CHAR_TABLE (val)->contents[c];
12214 }
12215 else
12216 {
12217 Lisp_Object table;
12218
12219 XSETCHAR_TABLE (table, dp);
12220 val = char_table_ref (table, c);
12221 }
12222 if (NILP (val))
12223 val = dp->defalt;
12224 return val;
12225 }
12226
12227
12228 \f
12229 /***********************************************************************
12230 Window Redisplay
12231 ***********************************************************************/
12232
12233 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12234
12235 static void
12236 redisplay_windows (window)
12237 Lisp_Object window;
12238 {
12239 while (!NILP (window))
12240 {
12241 struct window *w = XWINDOW (window);
12242
12243 if (!NILP (w->hchild))
12244 redisplay_windows (w->hchild);
12245 else if (!NILP (w->vchild))
12246 redisplay_windows (w->vchild);
12247 else
12248 {
12249 displayed_buffer = XBUFFER (w->buffer);
12250 /* Use list_of_error, not Qerror, so that
12251 we catch only errors and don't run the debugger. */
12252 internal_condition_case_1 (redisplay_window_0, window,
12253 list_of_error,
12254 redisplay_window_error);
12255 }
12256
12257 window = w->next;
12258 }
12259 }
12260
12261 static Lisp_Object
12262 redisplay_window_error ()
12263 {
12264 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12265 return Qnil;
12266 }
12267
12268 static Lisp_Object
12269 redisplay_window_0 (window)
12270 Lisp_Object window;
12271 {
12272 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12273 redisplay_window (window, 0);
12274 return Qnil;
12275 }
12276
12277 static Lisp_Object
12278 redisplay_window_1 (window)
12279 Lisp_Object window;
12280 {
12281 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12282 redisplay_window (window, 1);
12283 return Qnil;
12284 }
12285 \f
12286
12287 /* Increment GLYPH until it reaches END or CONDITION fails while
12288 adding (GLYPH)->pixel_width to X. */
12289
12290 #define SKIP_GLYPHS(glyph, end, x, condition) \
12291 do \
12292 { \
12293 (x) += (glyph)->pixel_width; \
12294 ++(glyph); \
12295 } \
12296 while ((glyph) < (end) && (condition))
12297
12298
12299 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12300 DELTA is the number of bytes by which positions recorded in ROW
12301 differ from current buffer positions.
12302
12303 Return 0 if cursor is not on this row. 1 otherwise. */
12304
12305 int
12306 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12307 struct window *w;
12308 struct glyph_row *row;
12309 struct glyph_matrix *matrix;
12310 int delta, delta_bytes, dy, dvpos;
12311 {
12312 struct glyph *glyph = row->glyphs[TEXT_AREA];
12313 struct glyph *end = glyph + row->used[TEXT_AREA];
12314 struct glyph *cursor = NULL;
12315 /* The first glyph that starts a sequence of glyphs from string. */
12316 struct glyph *string_start;
12317 /* The X coordinate of string_start. */
12318 int string_start_x;
12319 /* The last known character position. */
12320 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12321 /* The last known character position before string_start. */
12322 int string_before_pos;
12323 int x = row->x;
12324 int cursor_x = x;
12325 int cursor_from_overlay_pos = 0;
12326 int pt_old = PT - delta;
12327
12328 /* Skip over glyphs not having an object at the start of the row.
12329 These are special glyphs like truncation marks on terminal
12330 frames. */
12331 if (row->displays_text_p)
12332 while (glyph < end
12333 && INTEGERP (glyph->object)
12334 && glyph->charpos < 0)
12335 {
12336 x += glyph->pixel_width;
12337 ++glyph;
12338 }
12339
12340 string_start = NULL;
12341 while (glyph < end
12342 && !INTEGERP (glyph->object)
12343 && (!BUFFERP (glyph->object)
12344 || (last_pos = glyph->charpos) < pt_old
12345 || glyph->avoid_cursor_p))
12346 {
12347 if (! STRINGP (glyph->object))
12348 {
12349 string_start = NULL;
12350 x += glyph->pixel_width;
12351 ++glyph;
12352 if (cursor_from_overlay_pos
12353 && last_pos >= cursor_from_overlay_pos)
12354 {
12355 cursor_from_overlay_pos = 0;
12356 cursor = 0;
12357 }
12358 }
12359 else
12360 {
12361 if (string_start == NULL)
12362 {
12363 string_before_pos = last_pos;
12364 string_start = glyph;
12365 string_start_x = x;
12366 }
12367 /* Skip all glyphs from string. */
12368 do
12369 {
12370 Lisp_Object cprop;
12371 int pos;
12372 if ((cursor == NULL || glyph > cursor)
12373 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12374 Qcursor, (glyph)->object),
12375 !NILP (cprop))
12376 && (pos = string_buffer_position (w, glyph->object,
12377 string_before_pos),
12378 (pos == 0 /* From overlay */
12379 || pos == pt_old)))
12380 {
12381 /* Estimate overlay buffer position from the buffer
12382 positions of the glyphs before and after the overlay.
12383 Add 1 to last_pos so that if point corresponds to the
12384 glyph right after the overlay, we still use a 'cursor'
12385 property found in that overlay. */
12386 cursor_from_overlay_pos = (pos ? 0 : last_pos
12387 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12388 cursor = glyph;
12389 cursor_x = x;
12390 }
12391 x += glyph->pixel_width;
12392 ++glyph;
12393 }
12394 while (glyph < end && EQ (glyph->object, string_start->object));
12395 }
12396 }
12397
12398 if (cursor != NULL)
12399 {
12400 glyph = cursor;
12401 x = cursor_x;
12402 }
12403 else if (row->ends_in_ellipsis_p && glyph == end)
12404 {
12405 /* Scan back over the ellipsis glyphs, decrementing positions. */
12406 while (glyph > row->glyphs[TEXT_AREA]
12407 && (glyph - 1)->charpos == last_pos)
12408 glyph--, x -= glyph->pixel_width;
12409 /* That loop always goes one position too far,
12410 including the glyph before the ellipsis.
12411 So scan forward over that one. */
12412 x += glyph->pixel_width;
12413 glyph++;
12414 }
12415 else if (string_start
12416 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12417 {
12418 /* We may have skipped over point because the previous glyphs
12419 are from string. As there's no easy way to know the
12420 character position of the current glyph, find the correct
12421 glyph on point by scanning from string_start again. */
12422 Lisp_Object limit;
12423 Lisp_Object string;
12424 struct glyph *stop = glyph;
12425 int pos;
12426
12427 limit = make_number (pt_old + 1);
12428 glyph = string_start;
12429 x = string_start_x;
12430 string = glyph->object;
12431 pos = string_buffer_position (w, string, string_before_pos);
12432 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12433 because we always put cursor after overlay strings. */
12434 while (pos == 0 && glyph < stop)
12435 {
12436 string = glyph->object;
12437 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12438 if (glyph < stop)
12439 pos = string_buffer_position (w, glyph->object, string_before_pos);
12440 }
12441
12442 while (glyph < stop)
12443 {
12444 pos = XINT (Fnext_single_char_property_change
12445 (make_number (pos), Qdisplay, Qnil, limit));
12446 if (pos > pt_old)
12447 break;
12448 /* Skip glyphs from the same string. */
12449 string = glyph->object;
12450 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12451 /* Skip glyphs from an overlay. */
12452 while (glyph < stop
12453 && ! string_buffer_position (w, glyph->object, pos))
12454 {
12455 string = glyph->object;
12456 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12457 }
12458 }
12459
12460 /* If we reached the end of the line, and end was from a string,
12461 cursor is not on this line. */
12462 if (glyph == end && row->continued_p)
12463 return 0;
12464 }
12465
12466 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12467 w->cursor.x = x;
12468 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12469 w->cursor.y = row->y + dy;
12470
12471 if (w == XWINDOW (selected_window))
12472 {
12473 if (!row->continued_p
12474 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12475 && row->x == 0)
12476 {
12477 this_line_buffer = XBUFFER (w->buffer);
12478
12479 CHARPOS (this_line_start_pos)
12480 = MATRIX_ROW_START_CHARPOS (row) + delta;
12481 BYTEPOS (this_line_start_pos)
12482 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12483
12484 CHARPOS (this_line_end_pos)
12485 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12486 BYTEPOS (this_line_end_pos)
12487 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12488
12489 this_line_y = w->cursor.y;
12490 this_line_pixel_height = row->height;
12491 this_line_vpos = w->cursor.vpos;
12492 this_line_start_x = row->x;
12493 }
12494 else
12495 CHARPOS (this_line_start_pos) = 0;
12496 }
12497
12498 return 1;
12499 }
12500
12501
12502 /* Run window scroll functions, if any, for WINDOW with new window
12503 start STARTP. Sets the window start of WINDOW to that position.
12504
12505 We assume that the window's buffer is really current. */
12506
12507 static INLINE struct text_pos
12508 run_window_scroll_functions (window, startp)
12509 Lisp_Object window;
12510 struct text_pos startp;
12511 {
12512 struct window *w = XWINDOW (window);
12513 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12514
12515 if (current_buffer != XBUFFER (w->buffer))
12516 abort ();
12517
12518 if (!NILP (Vwindow_scroll_functions))
12519 {
12520 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12521 make_number (CHARPOS (startp)));
12522 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12523 /* In case the hook functions switch buffers. */
12524 if (current_buffer != XBUFFER (w->buffer))
12525 set_buffer_internal_1 (XBUFFER (w->buffer));
12526 }
12527
12528 return startp;
12529 }
12530
12531
12532 /* Make sure the line containing the cursor is fully visible.
12533 A value of 1 means there is nothing to be done.
12534 (Either the line is fully visible, or it cannot be made so,
12535 or we cannot tell.)
12536
12537 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12538 is higher than window.
12539
12540 A value of 0 means the caller should do scrolling
12541 as if point had gone off the screen. */
12542
12543 static int
12544 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12545 struct window *w;
12546 int force_p;
12547 int current_matrix_p;
12548 {
12549 struct glyph_matrix *matrix;
12550 struct glyph_row *row;
12551 int window_height;
12552
12553 if (!make_cursor_line_fully_visible_p)
12554 return 1;
12555
12556 /* It's not always possible to find the cursor, e.g, when a window
12557 is full of overlay strings. Don't do anything in that case. */
12558 if (w->cursor.vpos < 0)
12559 return 1;
12560
12561 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12562 row = MATRIX_ROW (matrix, w->cursor.vpos);
12563
12564 /* If the cursor row is not partially visible, there's nothing to do. */
12565 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12566 return 1;
12567
12568 /* If the row the cursor is in is taller than the window's height,
12569 it's not clear what to do, so do nothing. */
12570 window_height = window_box_height (w);
12571 if (row->height >= window_height)
12572 {
12573 if (!force_p || MINI_WINDOW_P (w)
12574 || w->vscroll || w->cursor.vpos == 0)
12575 return 1;
12576 }
12577 return 0;
12578
12579 #if 0
12580 /* This code used to try to scroll the window just enough to make
12581 the line visible. It returned 0 to say that the caller should
12582 allocate larger glyph matrices. */
12583
12584 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12585 {
12586 int dy = row->height - row->visible_height;
12587 w->vscroll = 0;
12588 w->cursor.y += dy;
12589 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12590 }
12591 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12592 {
12593 int dy = - (row->height - row->visible_height);
12594 w->vscroll = dy;
12595 w->cursor.y += dy;
12596 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12597 }
12598
12599 /* When we change the cursor y-position of the selected window,
12600 change this_line_y as well so that the display optimization for
12601 the cursor line of the selected window in redisplay_internal uses
12602 the correct y-position. */
12603 if (w == XWINDOW (selected_window))
12604 this_line_y = w->cursor.y;
12605
12606 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12607 redisplay with larger matrices. */
12608 if (matrix->nrows < required_matrix_height (w))
12609 {
12610 fonts_changed_p = 1;
12611 return 0;
12612 }
12613
12614 return 1;
12615 #endif /* 0 */
12616 }
12617
12618
12619 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12620 non-zero means only WINDOW is redisplayed in redisplay_internal.
12621 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12622 in redisplay_window to bring a partially visible line into view in
12623 the case that only the cursor has moved.
12624
12625 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12626 last screen line's vertical height extends past the end of the screen.
12627
12628 Value is
12629
12630 1 if scrolling succeeded
12631
12632 0 if scrolling didn't find point.
12633
12634 -1 if new fonts have been loaded so that we must interrupt
12635 redisplay, adjust glyph matrices, and try again. */
12636
12637 enum
12638 {
12639 SCROLLING_SUCCESS,
12640 SCROLLING_FAILED,
12641 SCROLLING_NEED_LARGER_MATRICES
12642 };
12643
12644 static int
12645 try_scrolling (window, just_this_one_p, scroll_conservatively,
12646 scroll_step, temp_scroll_step, last_line_misfit)
12647 Lisp_Object window;
12648 int just_this_one_p;
12649 EMACS_INT scroll_conservatively, scroll_step;
12650 int temp_scroll_step;
12651 int last_line_misfit;
12652 {
12653 struct window *w = XWINDOW (window);
12654 struct frame *f = XFRAME (w->frame);
12655 struct text_pos scroll_margin_pos;
12656 struct text_pos pos;
12657 struct text_pos startp;
12658 struct it it;
12659 Lisp_Object window_end;
12660 int this_scroll_margin;
12661 int dy = 0;
12662 int scroll_max;
12663 int rc;
12664 int amount_to_scroll = 0;
12665 Lisp_Object aggressive;
12666 int height;
12667 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12668
12669 #if GLYPH_DEBUG
12670 debug_method_add (w, "try_scrolling");
12671 #endif
12672
12673 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12674
12675 /* Compute scroll margin height in pixels. We scroll when point is
12676 within this distance from the top or bottom of the window. */
12677 if (scroll_margin > 0)
12678 {
12679 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12680 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12681 }
12682 else
12683 this_scroll_margin = 0;
12684
12685 /* Force scroll_conservatively to have a reasonable value so it doesn't
12686 cause an overflow while computing how much to scroll. */
12687 if (scroll_conservatively)
12688 scroll_conservatively = min (scroll_conservatively,
12689 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12690
12691 /* Compute how much we should try to scroll maximally to bring point
12692 into view. */
12693 if (scroll_step || scroll_conservatively || temp_scroll_step)
12694 scroll_max = max (scroll_step,
12695 max (scroll_conservatively, temp_scroll_step));
12696 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12697 || NUMBERP (current_buffer->scroll_up_aggressively))
12698 /* We're trying to scroll because of aggressive scrolling
12699 but no scroll_step is set. Choose an arbitrary one. Maybe
12700 there should be a variable for this. */
12701 scroll_max = 10;
12702 else
12703 scroll_max = 0;
12704 scroll_max *= FRAME_LINE_HEIGHT (f);
12705
12706 /* Decide whether we have to scroll down. Start at the window end
12707 and move this_scroll_margin up to find the position of the scroll
12708 margin. */
12709 window_end = Fwindow_end (window, Qt);
12710
12711 too_near_end:
12712
12713 CHARPOS (scroll_margin_pos) = XINT (window_end);
12714 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12715
12716 if (this_scroll_margin || extra_scroll_margin_lines)
12717 {
12718 start_display (&it, w, scroll_margin_pos);
12719 if (this_scroll_margin)
12720 move_it_vertically_backward (&it, this_scroll_margin);
12721 if (extra_scroll_margin_lines)
12722 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12723 scroll_margin_pos = it.current.pos;
12724 }
12725
12726 if (PT >= CHARPOS (scroll_margin_pos))
12727 {
12728 int y0;
12729
12730 /* Point is in the scroll margin at the bottom of the window, or
12731 below. Compute a new window start that makes point visible. */
12732
12733 /* Compute the distance from the scroll margin to PT.
12734 Give up if the distance is greater than scroll_max. */
12735 start_display (&it, w, scroll_margin_pos);
12736 y0 = it.current_y;
12737 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12738 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12739
12740 /* To make point visible, we have to move the window start
12741 down so that the line the cursor is in is visible, which
12742 means we have to add in the height of the cursor line. */
12743 dy = line_bottom_y (&it) - y0;
12744
12745 if (dy > scroll_max)
12746 return SCROLLING_FAILED;
12747
12748 /* Move the window start down. If scrolling conservatively,
12749 move it just enough down to make point visible. If
12750 scroll_step is set, move it down by scroll_step. */
12751 start_display (&it, w, startp);
12752
12753 if (scroll_conservatively)
12754 /* Set AMOUNT_TO_SCROLL to at least one line,
12755 and at most scroll_conservatively lines. */
12756 amount_to_scroll
12757 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12758 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12759 else if (scroll_step || temp_scroll_step)
12760 amount_to_scroll = scroll_max;
12761 else
12762 {
12763 aggressive = current_buffer->scroll_up_aggressively;
12764 height = WINDOW_BOX_TEXT_HEIGHT (w);
12765 if (NUMBERP (aggressive))
12766 {
12767 double float_amount = XFLOATINT (aggressive) * height;
12768 amount_to_scroll = float_amount;
12769 if (amount_to_scroll == 0 && float_amount > 0)
12770 amount_to_scroll = 1;
12771 }
12772 }
12773
12774 if (amount_to_scroll <= 0)
12775 return SCROLLING_FAILED;
12776
12777 /* If moving by amount_to_scroll leaves STARTP unchanged,
12778 move it down one screen line. */
12779
12780 move_it_vertically (&it, amount_to_scroll);
12781 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12782 move_it_by_lines (&it, 1, 1);
12783 startp = it.current.pos;
12784 }
12785 else
12786 {
12787 /* See if point is inside the scroll margin at the top of the
12788 window. */
12789 scroll_margin_pos = startp;
12790 if (this_scroll_margin)
12791 {
12792 start_display (&it, w, startp);
12793 move_it_vertically (&it, this_scroll_margin);
12794 scroll_margin_pos = it.current.pos;
12795 }
12796
12797 if (PT < CHARPOS (scroll_margin_pos))
12798 {
12799 /* Point is in the scroll margin at the top of the window or
12800 above what is displayed in the window. */
12801 int y0;
12802
12803 /* Compute the vertical distance from PT to the scroll
12804 margin position. Give up if distance is greater than
12805 scroll_max. */
12806 SET_TEXT_POS (pos, PT, PT_BYTE);
12807 start_display (&it, w, pos);
12808 y0 = it.current_y;
12809 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12810 it.last_visible_y, -1,
12811 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12812 dy = it.current_y - y0;
12813 if (dy > scroll_max)
12814 return SCROLLING_FAILED;
12815
12816 /* Compute new window start. */
12817 start_display (&it, w, startp);
12818
12819 if (scroll_conservatively)
12820 amount_to_scroll
12821 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12822 else if (scroll_step || temp_scroll_step)
12823 amount_to_scroll = scroll_max;
12824 else
12825 {
12826 aggressive = current_buffer->scroll_down_aggressively;
12827 height = WINDOW_BOX_TEXT_HEIGHT (w);
12828 if (NUMBERP (aggressive))
12829 {
12830 double float_amount = XFLOATINT (aggressive) * height;
12831 amount_to_scroll = float_amount;
12832 if (amount_to_scroll == 0 && float_amount > 0)
12833 amount_to_scroll = 1;
12834 }
12835 }
12836
12837 if (amount_to_scroll <= 0)
12838 return SCROLLING_FAILED;
12839
12840 move_it_vertically_backward (&it, amount_to_scroll);
12841 startp = it.current.pos;
12842 }
12843 }
12844
12845 /* Run window scroll functions. */
12846 startp = run_window_scroll_functions (window, startp);
12847
12848 /* Display the window. Give up if new fonts are loaded, or if point
12849 doesn't appear. */
12850 if (!try_window (window, startp, 0))
12851 rc = SCROLLING_NEED_LARGER_MATRICES;
12852 else if (w->cursor.vpos < 0)
12853 {
12854 clear_glyph_matrix (w->desired_matrix);
12855 rc = SCROLLING_FAILED;
12856 }
12857 else
12858 {
12859 /* Maybe forget recorded base line for line number display. */
12860 if (!just_this_one_p
12861 || current_buffer->clip_changed
12862 || BEG_UNCHANGED < CHARPOS (startp))
12863 w->base_line_number = Qnil;
12864
12865 /* If cursor ends up on a partially visible line,
12866 treat that as being off the bottom of the screen. */
12867 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12868 {
12869 clear_glyph_matrix (w->desired_matrix);
12870 ++extra_scroll_margin_lines;
12871 goto too_near_end;
12872 }
12873 rc = SCROLLING_SUCCESS;
12874 }
12875
12876 return rc;
12877 }
12878
12879
12880 /* Compute a suitable window start for window W if display of W starts
12881 on a continuation line. Value is non-zero if a new window start
12882 was computed.
12883
12884 The new window start will be computed, based on W's width, starting
12885 from the start of the continued line. It is the start of the
12886 screen line with the minimum distance from the old start W->start. */
12887
12888 static int
12889 compute_window_start_on_continuation_line (w)
12890 struct window *w;
12891 {
12892 struct text_pos pos, start_pos;
12893 int window_start_changed_p = 0;
12894
12895 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12896
12897 /* If window start is on a continuation line... Window start may be
12898 < BEGV in case there's invisible text at the start of the
12899 buffer (M-x rmail, for example). */
12900 if (CHARPOS (start_pos) > BEGV
12901 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12902 {
12903 struct it it;
12904 struct glyph_row *row;
12905
12906 /* Handle the case that the window start is out of range. */
12907 if (CHARPOS (start_pos) < BEGV)
12908 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12909 else if (CHARPOS (start_pos) > ZV)
12910 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12911
12912 /* Find the start of the continued line. This should be fast
12913 because scan_buffer is fast (newline cache). */
12914 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12915 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12916 row, DEFAULT_FACE_ID);
12917 reseat_at_previous_visible_line_start (&it);
12918
12919 /* If the line start is "too far" away from the window start,
12920 say it takes too much time to compute a new window start. */
12921 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12922 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12923 {
12924 int min_distance, distance;
12925
12926 /* Move forward by display lines to find the new window
12927 start. If window width was enlarged, the new start can
12928 be expected to be > the old start. If window width was
12929 decreased, the new window start will be < the old start.
12930 So, we're looking for the display line start with the
12931 minimum distance from the old window start. */
12932 pos = it.current.pos;
12933 min_distance = INFINITY;
12934 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12935 distance < min_distance)
12936 {
12937 min_distance = distance;
12938 pos = it.current.pos;
12939 move_it_by_lines (&it, 1, 0);
12940 }
12941
12942 /* Set the window start there. */
12943 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12944 window_start_changed_p = 1;
12945 }
12946 }
12947
12948 return window_start_changed_p;
12949 }
12950
12951
12952 /* Try cursor movement in case text has not changed in window WINDOW,
12953 with window start STARTP. Value is
12954
12955 CURSOR_MOVEMENT_SUCCESS if successful
12956
12957 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12958
12959 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12960 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12961 we want to scroll as if scroll-step were set to 1. See the code.
12962
12963 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12964 which case we have to abort this redisplay, and adjust matrices
12965 first. */
12966
12967 enum
12968 {
12969 CURSOR_MOVEMENT_SUCCESS,
12970 CURSOR_MOVEMENT_CANNOT_BE_USED,
12971 CURSOR_MOVEMENT_MUST_SCROLL,
12972 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12973 };
12974
12975 static int
12976 try_cursor_movement (window, startp, scroll_step)
12977 Lisp_Object window;
12978 struct text_pos startp;
12979 int *scroll_step;
12980 {
12981 struct window *w = XWINDOW (window);
12982 struct frame *f = XFRAME (w->frame);
12983 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12984
12985 #if GLYPH_DEBUG
12986 if (inhibit_try_cursor_movement)
12987 return rc;
12988 #endif
12989
12990 /* Handle case where text has not changed, only point, and it has
12991 not moved off the frame. */
12992 if (/* Point may be in this window. */
12993 PT >= CHARPOS (startp)
12994 /* Selective display hasn't changed. */
12995 && !current_buffer->clip_changed
12996 /* Function force-mode-line-update is used to force a thorough
12997 redisplay. It sets either windows_or_buffers_changed or
12998 update_mode_lines. So don't take a shortcut here for these
12999 cases. */
13000 && !update_mode_lines
13001 && !windows_or_buffers_changed
13002 && !cursor_type_changed
13003 /* Can't use this case if highlighting a region. When a
13004 region exists, cursor movement has to do more than just
13005 set the cursor. */
13006 && !(!NILP (Vtransient_mark_mode)
13007 && !NILP (current_buffer->mark_active))
13008 && NILP (w->region_showing)
13009 && NILP (Vshow_trailing_whitespace)
13010 /* Right after splitting windows, last_point may be nil. */
13011 && INTEGERP (w->last_point)
13012 /* This code is not used for mini-buffer for the sake of the case
13013 of redisplaying to replace an echo area message; since in
13014 that case the mini-buffer contents per se are usually
13015 unchanged. This code is of no real use in the mini-buffer
13016 since the handling of this_line_start_pos, etc., in redisplay
13017 handles the same cases. */
13018 && !EQ (window, minibuf_window)
13019 /* When splitting windows or for new windows, it happens that
13020 redisplay is called with a nil window_end_vpos or one being
13021 larger than the window. This should really be fixed in
13022 window.c. I don't have this on my list, now, so we do
13023 approximately the same as the old redisplay code. --gerd. */
13024 && INTEGERP (w->window_end_vpos)
13025 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13026 && (FRAME_WINDOW_P (f)
13027 || !overlay_arrow_in_current_buffer_p ()))
13028 {
13029 int this_scroll_margin, top_scroll_margin;
13030 struct glyph_row *row = NULL;
13031
13032 #if GLYPH_DEBUG
13033 debug_method_add (w, "cursor movement");
13034 #endif
13035
13036 /* Scroll if point within this distance from the top or bottom
13037 of the window. This is a pixel value. */
13038 this_scroll_margin = max (0, scroll_margin);
13039 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13040 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13041
13042 top_scroll_margin = this_scroll_margin;
13043 if (WINDOW_WANTS_HEADER_LINE_P (w))
13044 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13045
13046 /* Start with the row the cursor was displayed during the last
13047 not paused redisplay. Give up if that row is not valid. */
13048 if (w->last_cursor.vpos < 0
13049 || w->last_cursor.vpos >= w->current_matrix->nrows)
13050 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13051 else
13052 {
13053 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13054 if (row->mode_line_p)
13055 ++row;
13056 if (!row->enabled_p)
13057 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13058 }
13059
13060 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13061 {
13062 int scroll_p = 0;
13063 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13064
13065 if (PT > XFASTINT (w->last_point))
13066 {
13067 /* Point has moved forward. */
13068 while (MATRIX_ROW_END_CHARPOS (row) < PT
13069 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13070 {
13071 xassert (row->enabled_p);
13072 ++row;
13073 }
13074
13075 /* The end position of a row equals the start position
13076 of the next row. If PT is there, we would rather
13077 display it in the next line. */
13078 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13079 && MATRIX_ROW_END_CHARPOS (row) == PT
13080 && !cursor_row_p (w, row))
13081 ++row;
13082
13083 /* If within the scroll margin, scroll. Note that
13084 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13085 the next line would be drawn, and that
13086 this_scroll_margin can be zero. */
13087 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13088 || PT > MATRIX_ROW_END_CHARPOS (row)
13089 /* Line is completely visible last line in window
13090 and PT is to be set in the next line. */
13091 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13092 && PT == MATRIX_ROW_END_CHARPOS (row)
13093 && !row->ends_at_zv_p
13094 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13095 scroll_p = 1;
13096 }
13097 else if (PT < XFASTINT (w->last_point))
13098 {
13099 /* Cursor has to be moved backward. Note that PT >=
13100 CHARPOS (startp) because of the outer if-statement. */
13101 while (!row->mode_line_p
13102 && (MATRIX_ROW_START_CHARPOS (row) > PT
13103 || (MATRIX_ROW_START_CHARPOS (row) == PT
13104 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13105 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13106 row > w->current_matrix->rows
13107 && (row-1)->ends_in_newline_from_string_p))))
13108 && (row->y > top_scroll_margin
13109 || CHARPOS (startp) == BEGV))
13110 {
13111 xassert (row->enabled_p);
13112 --row;
13113 }
13114
13115 /* Consider the following case: Window starts at BEGV,
13116 there is invisible, intangible text at BEGV, so that
13117 display starts at some point START > BEGV. It can
13118 happen that we are called with PT somewhere between
13119 BEGV and START. Try to handle that case. */
13120 if (row < w->current_matrix->rows
13121 || row->mode_line_p)
13122 {
13123 row = w->current_matrix->rows;
13124 if (row->mode_line_p)
13125 ++row;
13126 }
13127
13128 /* Due to newlines in overlay strings, we may have to
13129 skip forward over overlay strings. */
13130 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13131 && MATRIX_ROW_END_CHARPOS (row) == PT
13132 && !cursor_row_p (w, row))
13133 ++row;
13134
13135 /* If within the scroll margin, scroll. */
13136 if (row->y < top_scroll_margin
13137 && CHARPOS (startp) != BEGV)
13138 scroll_p = 1;
13139 }
13140 else
13141 {
13142 /* Cursor did not move. So don't scroll even if cursor line
13143 is partially visible, as it was so before. */
13144 rc = CURSOR_MOVEMENT_SUCCESS;
13145 }
13146
13147 if (PT < MATRIX_ROW_START_CHARPOS (row)
13148 || PT > MATRIX_ROW_END_CHARPOS (row))
13149 {
13150 /* if PT is not in the glyph row, give up. */
13151 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13152 }
13153 else if (rc != CURSOR_MOVEMENT_SUCCESS
13154 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13155 && make_cursor_line_fully_visible_p)
13156 {
13157 if (PT == MATRIX_ROW_END_CHARPOS (row)
13158 && !row->ends_at_zv_p
13159 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13160 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13161 else if (row->height > window_box_height (w))
13162 {
13163 /* If we end up in a partially visible line, let's
13164 make it fully visible, except when it's taller
13165 than the window, in which case we can't do much
13166 about it. */
13167 *scroll_step = 1;
13168 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13169 }
13170 else
13171 {
13172 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13173 if (!cursor_row_fully_visible_p (w, 0, 1))
13174 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13175 else
13176 rc = CURSOR_MOVEMENT_SUCCESS;
13177 }
13178 }
13179 else if (scroll_p)
13180 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13181 else
13182 {
13183 do
13184 {
13185 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13186 {
13187 rc = CURSOR_MOVEMENT_SUCCESS;
13188 break;
13189 }
13190 ++row;
13191 }
13192 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13193 && MATRIX_ROW_START_CHARPOS (row) == PT
13194 && cursor_row_p (w, row));
13195 }
13196 }
13197 }
13198
13199 return rc;
13200 }
13201
13202 void
13203 set_vertical_scroll_bar (w)
13204 struct window *w;
13205 {
13206 int start, end, whole;
13207
13208 /* Calculate the start and end positions for the current window.
13209 At some point, it would be nice to choose between scrollbars
13210 which reflect the whole buffer size, with special markers
13211 indicating narrowing, and scrollbars which reflect only the
13212 visible region.
13213
13214 Note that mini-buffers sometimes aren't displaying any text. */
13215 if (!MINI_WINDOW_P (w)
13216 || (w == XWINDOW (minibuf_window)
13217 && NILP (echo_area_buffer[0])))
13218 {
13219 struct buffer *buf = XBUFFER (w->buffer);
13220 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13221 start = marker_position (w->start) - BUF_BEGV (buf);
13222 /* I don't think this is guaranteed to be right. For the
13223 moment, we'll pretend it is. */
13224 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13225
13226 if (end < start)
13227 end = start;
13228 if (whole < (end - start))
13229 whole = end - start;
13230 }
13231 else
13232 start = end = whole = 0;
13233
13234 /* Indicate what this scroll bar ought to be displaying now. */
13235 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13236 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13237 (w, end - start, whole, start);
13238 }
13239
13240
13241 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13242 selected_window is redisplayed.
13243
13244 We can return without actually redisplaying the window if
13245 fonts_changed_p is nonzero. In that case, redisplay_internal will
13246 retry. */
13247
13248 static void
13249 redisplay_window (window, just_this_one_p)
13250 Lisp_Object window;
13251 int just_this_one_p;
13252 {
13253 struct window *w = XWINDOW (window);
13254 struct frame *f = XFRAME (w->frame);
13255 struct buffer *buffer = XBUFFER (w->buffer);
13256 struct buffer *old = current_buffer;
13257 struct text_pos lpoint, opoint, startp;
13258 int update_mode_line;
13259 int tem;
13260 struct it it;
13261 /* Record it now because it's overwritten. */
13262 int current_matrix_up_to_date_p = 0;
13263 int used_current_matrix_p = 0;
13264 /* This is less strict than current_matrix_up_to_date_p.
13265 It indictes that the buffer contents and narrowing are unchanged. */
13266 int buffer_unchanged_p = 0;
13267 int temp_scroll_step = 0;
13268 int count = SPECPDL_INDEX ();
13269 int rc;
13270 int centering_position = -1;
13271 int last_line_misfit = 0;
13272 int beg_unchanged, end_unchanged;
13273
13274 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13275 opoint = lpoint;
13276
13277 /* W must be a leaf window here. */
13278 xassert (!NILP (w->buffer));
13279 #if GLYPH_DEBUG
13280 *w->desired_matrix->method = 0;
13281 #endif
13282
13283 restart:
13284 reconsider_clip_changes (w, buffer);
13285
13286 /* Has the mode line to be updated? */
13287 update_mode_line = (!NILP (w->update_mode_line)
13288 || update_mode_lines
13289 || buffer->clip_changed
13290 || buffer->prevent_redisplay_optimizations_p);
13291
13292 if (MINI_WINDOW_P (w))
13293 {
13294 if (w == XWINDOW (echo_area_window)
13295 && !NILP (echo_area_buffer[0]))
13296 {
13297 if (update_mode_line)
13298 /* We may have to update a tty frame's menu bar or a
13299 tool-bar. Example `M-x C-h C-h C-g'. */
13300 goto finish_menu_bars;
13301 else
13302 /* We've already displayed the echo area glyphs in this window. */
13303 goto finish_scroll_bars;
13304 }
13305 else if ((w != XWINDOW (minibuf_window)
13306 || minibuf_level == 0)
13307 /* When buffer is nonempty, redisplay window normally. */
13308 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13309 /* Quail displays non-mini buffers in minibuffer window.
13310 In that case, redisplay the window normally. */
13311 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13312 {
13313 /* W is a mini-buffer window, but it's not active, so clear
13314 it. */
13315 int yb = window_text_bottom_y (w);
13316 struct glyph_row *row;
13317 int y;
13318
13319 for (y = 0, row = w->desired_matrix->rows;
13320 y < yb;
13321 y += row->height, ++row)
13322 blank_row (w, row, y);
13323 goto finish_scroll_bars;
13324 }
13325
13326 clear_glyph_matrix (w->desired_matrix);
13327 }
13328
13329 /* Otherwise set up data on this window; select its buffer and point
13330 value. */
13331 /* Really select the buffer, for the sake of buffer-local
13332 variables. */
13333 set_buffer_internal_1 (XBUFFER (w->buffer));
13334
13335 current_matrix_up_to_date_p
13336 = (!NILP (w->window_end_valid)
13337 && !current_buffer->clip_changed
13338 && !current_buffer->prevent_redisplay_optimizations_p
13339 && XFASTINT (w->last_modified) >= MODIFF
13340 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13341
13342 /* Run the window-bottom-change-functions
13343 if it is possible that the text on the screen has changed
13344 (either due to modification of the text, or any other reason). */
13345 if (!current_matrix_up_to_date_p
13346 && !NILP (Vwindow_text_change_functions))
13347 {
13348 safe_run_hooks (Qwindow_text_change_functions);
13349 goto restart;
13350 }
13351
13352 beg_unchanged = BEG_UNCHANGED;
13353 end_unchanged = END_UNCHANGED;
13354
13355 SET_TEXT_POS (opoint, PT, PT_BYTE);
13356
13357 specbind (Qinhibit_point_motion_hooks, Qt);
13358
13359 buffer_unchanged_p
13360 = (!NILP (w->window_end_valid)
13361 && !current_buffer->clip_changed
13362 && XFASTINT (w->last_modified) >= MODIFF
13363 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13364
13365 /* When windows_or_buffers_changed is non-zero, we can't rely on
13366 the window end being valid, so set it to nil there. */
13367 if (windows_or_buffers_changed)
13368 {
13369 /* If window starts on a continuation line, maybe adjust the
13370 window start in case the window's width changed. */
13371 if (XMARKER (w->start)->buffer == current_buffer)
13372 compute_window_start_on_continuation_line (w);
13373
13374 w->window_end_valid = Qnil;
13375 }
13376
13377 /* Some sanity checks. */
13378 CHECK_WINDOW_END (w);
13379 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13380 abort ();
13381 if (BYTEPOS (opoint) < CHARPOS (opoint))
13382 abort ();
13383
13384 /* If %c is in mode line, update it if needed. */
13385 if (!NILP (w->column_number_displayed)
13386 /* This alternative quickly identifies a common case
13387 where no change is needed. */
13388 && !(PT == XFASTINT (w->last_point)
13389 && XFASTINT (w->last_modified) >= MODIFF
13390 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13391 && (XFASTINT (w->column_number_displayed)
13392 != (int) current_column ())) /* iftc */
13393 update_mode_line = 1;
13394
13395 /* Count number of windows showing the selected buffer. An indirect
13396 buffer counts as its base buffer. */
13397 if (!just_this_one_p)
13398 {
13399 struct buffer *current_base, *window_base;
13400 current_base = current_buffer;
13401 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13402 if (current_base->base_buffer)
13403 current_base = current_base->base_buffer;
13404 if (window_base->base_buffer)
13405 window_base = window_base->base_buffer;
13406 if (current_base == window_base)
13407 buffer_shared++;
13408 }
13409
13410 /* Point refers normally to the selected window. For any other
13411 window, set up appropriate value. */
13412 if (!EQ (window, selected_window))
13413 {
13414 int new_pt = XMARKER (w->pointm)->charpos;
13415 int new_pt_byte = marker_byte_position (w->pointm);
13416 if (new_pt < BEGV)
13417 {
13418 new_pt = BEGV;
13419 new_pt_byte = BEGV_BYTE;
13420 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13421 }
13422 else if (new_pt > (ZV - 1))
13423 {
13424 new_pt = ZV;
13425 new_pt_byte = ZV_BYTE;
13426 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13427 }
13428
13429 /* We don't use SET_PT so that the point-motion hooks don't run. */
13430 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13431 }
13432
13433 /* If any of the character widths specified in the display table
13434 have changed, invalidate the width run cache. It's true that
13435 this may be a bit late to catch such changes, but the rest of
13436 redisplay goes (non-fatally) haywire when the display table is
13437 changed, so why should we worry about doing any better? */
13438 if (current_buffer->width_run_cache)
13439 {
13440 struct Lisp_Char_Table *disptab = buffer_display_table ();
13441
13442 if (! disptab_matches_widthtab (disptab,
13443 XVECTOR (current_buffer->width_table)))
13444 {
13445 invalidate_region_cache (current_buffer,
13446 current_buffer->width_run_cache,
13447 BEG, Z);
13448 recompute_width_table (current_buffer, disptab);
13449 }
13450 }
13451
13452 /* If window-start is screwed up, choose a new one. */
13453 if (XMARKER (w->start)->buffer != current_buffer)
13454 goto recenter;
13455
13456 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13457
13458 /* If someone specified a new starting point but did not insist,
13459 check whether it can be used. */
13460 if (!NILP (w->optional_new_start)
13461 && CHARPOS (startp) >= BEGV
13462 && CHARPOS (startp) <= ZV)
13463 {
13464 w->optional_new_start = Qnil;
13465 start_display (&it, w, startp);
13466 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13467 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13468 if (IT_CHARPOS (it) == PT)
13469 w->force_start = Qt;
13470 /* IT may overshoot PT if text at PT is invisible. */
13471 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13472 w->force_start = Qt;
13473 }
13474
13475 force_start:
13476
13477 /* Handle case where place to start displaying has been specified,
13478 unless the specified location is outside the accessible range. */
13479 if (!NILP (w->force_start)
13480 || w->frozen_window_start_p)
13481 {
13482 /* We set this later on if we have to adjust point. */
13483 int new_vpos = -1;
13484 int val;
13485
13486 w->force_start = Qnil;
13487 w->vscroll = 0;
13488 w->window_end_valid = Qnil;
13489
13490 /* Forget any recorded base line for line number display. */
13491 if (!buffer_unchanged_p)
13492 w->base_line_number = Qnil;
13493
13494 /* Redisplay the mode line. Select the buffer properly for that.
13495 Also, run the hook window-scroll-functions
13496 because we have scrolled. */
13497 /* Note, we do this after clearing force_start because
13498 if there's an error, it is better to forget about force_start
13499 than to get into an infinite loop calling the hook functions
13500 and having them get more errors. */
13501 if (!update_mode_line
13502 || ! NILP (Vwindow_scroll_functions))
13503 {
13504 update_mode_line = 1;
13505 w->update_mode_line = Qt;
13506 startp = run_window_scroll_functions (window, startp);
13507 }
13508
13509 w->last_modified = make_number (0);
13510 w->last_overlay_modified = make_number (0);
13511 if (CHARPOS (startp) < BEGV)
13512 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13513 else if (CHARPOS (startp) > ZV)
13514 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13515
13516 /* Redisplay, then check if cursor has been set during the
13517 redisplay. Give up if new fonts were loaded. */
13518 val = try_window (window, startp, 1);
13519 if (!val)
13520 {
13521 w->force_start = Qt;
13522 clear_glyph_matrix (w->desired_matrix);
13523 goto need_larger_matrices;
13524 }
13525 /* Point was outside the scroll margins. */
13526 if (val < 0)
13527 new_vpos = window_box_height (w) / 2;
13528
13529 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13530 {
13531 /* If point does not appear, try to move point so it does
13532 appear. The desired matrix has been built above, so we
13533 can use it here. */
13534 new_vpos = window_box_height (w) / 2;
13535 }
13536
13537 if (!cursor_row_fully_visible_p (w, 0, 0))
13538 {
13539 /* Point does appear, but on a line partly visible at end of window.
13540 Move it back to a fully-visible line. */
13541 new_vpos = window_box_height (w);
13542 }
13543
13544 /* If we need to move point for either of the above reasons,
13545 now actually do it. */
13546 if (new_vpos >= 0)
13547 {
13548 struct glyph_row *row;
13549
13550 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13551 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13552 ++row;
13553
13554 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13555 MATRIX_ROW_START_BYTEPOS (row));
13556
13557 if (w != XWINDOW (selected_window))
13558 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13559 else if (current_buffer == old)
13560 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13561
13562 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13563
13564 /* If we are highlighting the region, then we just changed
13565 the region, so redisplay to show it. */
13566 if (!NILP (Vtransient_mark_mode)
13567 && !NILP (current_buffer->mark_active))
13568 {
13569 clear_glyph_matrix (w->desired_matrix);
13570 if (!try_window (window, startp, 0))
13571 goto need_larger_matrices;
13572 }
13573 }
13574
13575 #if GLYPH_DEBUG
13576 debug_method_add (w, "forced window start");
13577 #endif
13578 goto done;
13579 }
13580
13581 /* Handle case where text has not changed, only point, and it has
13582 not moved off the frame, and we are not retrying after hscroll.
13583 (current_matrix_up_to_date_p is nonzero when retrying.) */
13584 if (current_matrix_up_to_date_p
13585 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13586 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13587 {
13588 switch (rc)
13589 {
13590 case CURSOR_MOVEMENT_SUCCESS:
13591 used_current_matrix_p = 1;
13592 goto done;
13593
13594 #if 0 /* try_cursor_movement never returns this value. */
13595 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13596 goto need_larger_matrices;
13597 #endif
13598
13599 case CURSOR_MOVEMENT_MUST_SCROLL:
13600 goto try_to_scroll;
13601
13602 default:
13603 abort ();
13604 }
13605 }
13606 /* If current starting point was originally the beginning of a line
13607 but no longer is, find a new starting point. */
13608 else if (!NILP (w->start_at_line_beg)
13609 && !(CHARPOS (startp) <= BEGV
13610 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13611 {
13612 #if GLYPH_DEBUG
13613 debug_method_add (w, "recenter 1");
13614 #endif
13615 goto recenter;
13616 }
13617
13618 /* Try scrolling with try_window_id. Value is > 0 if update has
13619 been done, it is -1 if we know that the same window start will
13620 not work. It is 0 if unsuccessful for some other reason. */
13621 else if ((tem = try_window_id (w)) != 0)
13622 {
13623 #if GLYPH_DEBUG
13624 debug_method_add (w, "try_window_id %d", tem);
13625 #endif
13626
13627 if (fonts_changed_p)
13628 goto need_larger_matrices;
13629 if (tem > 0)
13630 goto done;
13631
13632 /* Otherwise try_window_id has returned -1 which means that we
13633 don't want the alternative below this comment to execute. */
13634 }
13635 else if (CHARPOS (startp) >= BEGV
13636 && CHARPOS (startp) <= ZV
13637 && PT >= CHARPOS (startp)
13638 && (CHARPOS (startp) < ZV
13639 /* Avoid starting at end of buffer. */
13640 || CHARPOS (startp) == BEGV
13641 || (XFASTINT (w->last_modified) >= MODIFF
13642 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13643 {
13644
13645 /* If first window line is a continuation line, and window start
13646 is inside the modified region, but the first change is before
13647 current window start, we must select a new window start.
13648
13649 However, if this is the result of a down-mouse event (e.g. by
13650 extending the mouse-drag-overlay), we don't want to select a
13651 new window start, since that would change the position under
13652 the mouse, resulting in an unwanted mouse-movement rather
13653 than a simple mouse-click. */
13654 if (NILP (w->start_at_line_beg)
13655 && NILP (do_mouse_tracking)
13656 && CHARPOS (startp) > BEGV
13657 && CHARPOS (startp) > BEG + beg_unchanged
13658 && CHARPOS (startp) <= Z - end_unchanged)
13659 {
13660 w->force_start = Qt;
13661 if (XMARKER (w->start)->buffer == current_buffer)
13662 compute_window_start_on_continuation_line (w);
13663 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13664 goto force_start;
13665 }
13666
13667 #if GLYPH_DEBUG
13668 debug_method_add (w, "same window start");
13669 #endif
13670
13671 /* Try to redisplay starting at same place as before.
13672 If point has not moved off frame, accept the results. */
13673 if (!current_matrix_up_to_date_p
13674 /* Don't use try_window_reusing_current_matrix in this case
13675 because a window scroll function can have changed the
13676 buffer. */
13677 || !NILP (Vwindow_scroll_functions)
13678 || MINI_WINDOW_P (w)
13679 || !(used_current_matrix_p
13680 = try_window_reusing_current_matrix (w)))
13681 {
13682 IF_DEBUG (debug_method_add (w, "1"));
13683 if (try_window (window, startp, 1) < 0)
13684 /* -1 means we need to scroll.
13685 0 means we need new matrices, but fonts_changed_p
13686 is set in that case, so we will detect it below. */
13687 goto try_to_scroll;
13688 }
13689
13690 if (fonts_changed_p)
13691 goto need_larger_matrices;
13692
13693 if (w->cursor.vpos >= 0)
13694 {
13695 if (!just_this_one_p
13696 || current_buffer->clip_changed
13697 || BEG_UNCHANGED < CHARPOS (startp))
13698 /* Forget any recorded base line for line number display. */
13699 w->base_line_number = Qnil;
13700
13701 if (!cursor_row_fully_visible_p (w, 1, 0))
13702 {
13703 clear_glyph_matrix (w->desired_matrix);
13704 last_line_misfit = 1;
13705 }
13706 /* Drop through and scroll. */
13707 else
13708 goto done;
13709 }
13710 else
13711 clear_glyph_matrix (w->desired_matrix);
13712 }
13713
13714 try_to_scroll:
13715
13716 w->last_modified = make_number (0);
13717 w->last_overlay_modified = make_number (0);
13718
13719 /* Redisplay the mode line. Select the buffer properly for that. */
13720 if (!update_mode_line)
13721 {
13722 update_mode_line = 1;
13723 w->update_mode_line = Qt;
13724 }
13725
13726 /* Try to scroll by specified few lines. */
13727 if ((scroll_conservatively
13728 || scroll_step
13729 || temp_scroll_step
13730 || NUMBERP (current_buffer->scroll_up_aggressively)
13731 || NUMBERP (current_buffer->scroll_down_aggressively))
13732 && !current_buffer->clip_changed
13733 && CHARPOS (startp) >= BEGV
13734 && CHARPOS (startp) <= ZV)
13735 {
13736 /* The function returns -1 if new fonts were loaded, 1 if
13737 successful, 0 if not successful. */
13738 int rc = try_scrolling (window, just_this_one_p,
13739 scroll_conservatively,
13740 scroll_step,
13741 temp_scroll_step, last_line_misfit);
13742 switch (rc)
13743 {
13744 case SCROLLING_SUCCESS:
13745 goto done;
13746
13747 case SCROLLING_NEED_LARGER_MATRICES:
13748 goto need_larger_matrices;
13749
13750 case SCROLLING_FAILED:
13751 break;
13752
13753 default:
13754 abort ();
13755 }
13756 }
13757
13758 /* Finally, just choose place to start which centers point */
13759
13760 recenter:
13761 if (centering_position < 0)
13762 centering_position = window_box_height (w) / 2;
13763
13764 #if GLYPH_DEBUG
13765 debug_method_add (w, "recenter");
13766 #endif
13767
13768 /* w->vscroll = 0; */
13769
13770 /* Forget any previously recorded base line for line number display. */
13771 if (!buffer_unchanged_p)
13772 w->base_line_number = Qnil;
13773
13774 /* Move backward half the height of the window. */
13775 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13776 it.current_y = it.last_visible_y;
13777 move_it_vertically_backward (&it, centering_position);
13778 xassert (IT_CHARPOS (it) >= BEGV);
13779
13780 /* The function move_it_vertically_backward may move over more
13781 than the specified y-distance. If it->w is small, e.g. a
13782 mini-buffer window, we may end up in front of the window's
13783 display area. Start displaying at the start of the line
13784 containing PT in this case. */
13785 if (it.current_y <= 0)
13786 {
13787 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13788 move_it_vertically_backward (&it, 0);
13789 #if 0
13790 /* I think this assert is bogus if buffer contains
13791 invisible text or images. KFS. */
13792 xassert (IT_CHARPOS (it) <= PT);
13793 #endif
13794 it.current_y = 0;
13795 }
13796
13797 it.current_x = it.hpos = 0;
13798
13799 /* Set startp here explicitly in case that helps avoid an infinite loop
13800 in case the window-scroll-functions functions get errors. */
13801 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13802
13803 /* Run scroll hooks. */
13804 startp = run_window_scroll_functions (window, it.current.pos);
13805
13806 /* Redisplay the window. */
13807 if (!current_matrix_up_to_date_p
13808 || windows_or_buffers_changed
13809 || cursor_type_changed
13810 /* Don't use try_window_reusing_current_matrix in this case
13811 because it can have changed the buffer. */
13812 || !NILP (Vwindow_scroll_functions)
13813 || !just_this_one_p
13814 || MINI_WINDOW_P (w)
13815 || !(used_current_matrix_p
13816 = try_window_reusing_current_matrix (w)))
13817 try_window (window, startp, 0);
13818
13819 /* If new fonts have been loaded (due to fontsets), give up. We
13820 have to start a new redisplay since we need to re-adjust glyph
13821 matrices. */
13822 if (fonts_changed_p)
13823 goto need_larger_matrices;
13824
13825 /* If cursor did not appear assume that the middle of the window is
13826 in the first line of the window. Do it again with the next line.
13827 (Imagine a window of height 100, displaying two lines of height
13828 60. Moving back 50 from it->last_visible_y will end in the first
13829 line.) */
13830 if (w->cursor.vpos < 0)
13831 {
13832 if (!NILP (w->window_end_valid)
13833 && PT >= Z - XFASTINT (w->window_end_pos))
13834 {
13835 clear_glyph_matrix (w->desired_matrix);
13836 move_it_by_lines (&it, 1, 0);
13837 try_window (window, it.current.pos, 0);
13838 }
13839 else if (PT < IT_CHARPOS (it))
13840 {
13841 clear_glyph_matrix (w->desired_matrix);
13842 move_it_by_lines (&it, -1, 0);
13843 try_window (window, it.current.pos, 0);
13844 }
13845 else
13846 {
13847 /* Not much we can do about it. */
13848 }
13849 }
13850
13851 /* Consider the following case: Window starts at BEGV, there is
13852 invisible, intangible text at BEGV, so that display starts at
13853 some point START > BEGV. It can happen that we are called with
13854 PT somewhere between BEGV and START. Try to handle that case. */
13855 if (w->cursor.vpos < 0)
13856 {
13857 struct glyph_row *row = w->current_matrix->rows;
13858 if (row->mode_line_p)
13859 ++row;
13860 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13861 }
13862
13863 if (!cursor_row_fully_visible_p (w, 0, 0))
13864 {
13865 /* If vscroll is enabled, disable it and try again. */
13866 if (w->vscroll)
13867 {
13868 w->vscroll = 0;
13869 clear_glyph_matrix (w->desired_matrix);
13870 goto recenter;
13871 }
13872
13873 /* If centering point failed to make the whole line visible,
13874 put point at the top instead. That has to make the whole line
13875 visible, if it can be done. */
13876 if (centering_position == 0)
13877 goto done;
13878
13879 clear_glyph_matrix (w->desired_matrix);
13880 centering_position = 0;
13881 goto recenter;
13882 }
13883
13884 done:
13885
13886 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13887 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13888 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13889 ? Qt : Qnil);
13890
13891 /* Display the mode line, if we must. */
13892 if ((update_mode_line
13893 /* If window not full width, must redo its mode line
13894 if (a) the window to its side is being redone and
13895 (b) we do a frame-based redisplay. This is a consequence
13896 of how inverted lines are drawn in frame-based redisplay. */
13897 || (!just_this_one_p
13898 && !FRAME_WINDOW_P (f)
13899 && !WINDOW_FULL_WIDTH_P (w))
13900 /* Line number to display. */
13901 || INTEGERP (w->base_line_pos)
13902 /* Column number is displayed and different from the one displayed. */
13903 || (!NILP (w->column_number_displayed)
13904 && (XFASTINT (w->column_number_displayed)
13905 != (int) current_column ()))) /* iftc */
13906 /* This means that the window has a mode line. */
13907 && (WINDOW_WANTS_MODELINE_P (w)
13908 || WINDOW_WANTS_HEADER_LINE_P (w)))
13909 {
13910 display_mode_lines (w);
13911
13912 /* If mode line height has changed, arrange for a thorough
13913 immediate redisplay using the correct mode line height. */
13914 if (WINDOW_WANTS_MODELINE_P (w)
13915 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13916 {
13917 fonts_changed_p = 1;
13918 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13919 = DESIRED_MODE_LINE_HEIGHT (w);
13920 }
13921
13922 /* If top line height has changed, arrange for a thorough
13923 immediate redisplay using the correct mode line height. */
13924 if (WINDOW_WANTS_HEADER_LINE_P (w)
13925 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13926 {
13927 fonts_changed_p = 1;
13928 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13929 = DESIRED_HEADER_LINE_HEIGHT (w);
13930 }
13931
13932 if (fonts_changed_p)
13933 goto need_larger_matrices;
13934 }
13935
13936 if (!line_number_displayed
13937 && !BUFFERP (w->base_line_pos))
13938 {
13939 w->base_line_pos = Qnil;
13940 w->base_line_number = Qnil;
13941 }
13942
13943 finish_menu_bars:
13944
13945 /* When we reach a frame's selected window, redo the frame's menu bar. */
13946 if (update_mode_line
13947 && EQ (FRAME_SELECTED_WINDOW (f), window))
13948 {
13949 int redisplay_menu_p = 0;
13950 int redisplay_tool_bar_p = 0;
13951
13952 if (FRAME_WINDOW_P (f))
13953 {
13954 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13955 || defined (HAVE_NS) || defined (USE_GTK)
13956 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13957 #else
13958 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13959 #endif
13960 }
13961 else
13962 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13963
13964 if (redisplay_menu_p)
13965 display_menu_bar (w);
13966
13967 #ifdef HAVE_WINDOW_SYSTEM
13968 if (FRAME_WINDOW_P (f))
13969 {
13970 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
13971 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13972 #else
13973 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13974 && (FRAME_TOOL_BAR_LINES (f) > 0
13975 || !NILP (Vauto_resize_tool_bars));
13976 #endif
13977
13978 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13979 {
13980 extern int ignore_mouse_drag_p;
13981 ignore_mouse_drag_p = 1;
13982 }
13983 }
13984 #endif
13985 }
13986
13987 #ifdef HAVE_WINDOW_SYSTEM
13988 if (FRAME_WINDOW_P (f)
13989 && update_window_fringes (w, (just_this_one_p
13990 || (!used_current_matrix_p && !overlay_arrow_seen)
13991 || w->pseudo_window_p)))
13992 {
13993 update_begin (f);
13994 BLOCK_INPUT;
13995 if (draw_window_fringes (w, 1))
13996 x_draw_vertical_border (w);
13997 UNBLOCK_INPUT;
13998 update_end (f);
13999 }
14000 #endif /* HAVE_WINDOW_SYSTEM */
14001
14002 /* We go to this label, with fonts_changed_p nonzero,
14003 if it is necessary to try again using larger glyph matrices.
14004 We have to redeem the scroll bar even in this case,
14005 because the loop in redisplay_internal expects that. */
14006 need_larger_matrices:
14007 ;
14008 finish_scroll_bars:
14009
14010 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14011 {
14012 /* Set the thumb's position and size. */
14013 set_vertical_scroll_bar (w);
14014
14015 /* Note that we actually used the scroll bar attached to this
14016 window, so it shouldn't be deleted at the end of redisplay. */
14017 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14018 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14019 }
14020
14021 /* Restore current_buffer and value of point in it. */
14022 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14023 set_buffer_internal_1 (old);
14024 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14025 shorter. This can be caused by log truncation in *Messages*. */
14026 if (CHARPOS (lpoint) <= ZV)
14027 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14028
14029 unbind_to (count, Qnil);
14030 }
14031
14032
14033 /* Build the complete desired matrix of WINDOW with a window start
14034 buffer position POS.
14035
14036 Value is 1 if successful. It is zero if fonts were loaded during
14037 redisplay which makes re-adjusting glyph matrices necessary, and -1
14038 if point would appear in the scroll margins.
14039 (We check that only if CHECK_MARGINS is nonzero. */
14040
14041 int
14042 try_window (window, pos, check_margins)
14043 Lisp_Object window;
14044 struct text_pos pos;
14045 int check_margins;
14046 {
14047 struct window *w = XWINDOW (window);
14048 struct it it;
14049 struct glyph_row *last_text_row = NULL;
14050 struct frame *f = XFRAME (w->frame);
14051
14052 /* Make POS the new window start. */
14053 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14054
14055 /* Mark cursor position as unknown. No overlay arrow seen. */
14056 w->cursor.vpos = -1;
14057 overlay_arrow_seen = 0;
14058
14059 /* Initialize iterator and info to start at POS. */
14060 start_display (&it, w, pos);
14061
14062 /* Display all lines of W. */
14063 while (it.current_y < it.last_visible_y)
14064 {
14065 if (display_line (&it))
14066 last_text_row = it.glyph_row - 1;
14067 if (fonts_changed_p)
14068 return 0;
14069 }
14070
14071 /* Don't let the cursor end in the scroll margins. */
14072 if (check_margins
14073 && !MINI_WINDOW_P (w))
14074 {
14075 int this_scroll_margin;
14076
14077 this_scroll_margin = max (0, scroll_margin);
14078 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14079 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14080
14081 if ((w->cursor.y >= 0 /* not vscrolled */
14082 && w->cursor.y < this_scroll_margin
14083 && CHARPOS (pos) > BEGV
14084 && IT_CHARPOS (it) < ZV)
14085 /* rms: considering make_cursor_line_fully_visible_p here
14086 seems to give wrong results. We don't want to recenter
14087 when the last line is partly visible, we want to allow
14088 that case to be handled in the usual way. */
14089 || (w->cursor.y + 1) > it.last_visible_y)
14090 {
14091 w->cursor.vpos = -1;
14092 clear_glyph_matrix (w->desired_matrix);
14093 return -1;
14094 }
14095 }
14096
14097 /* If bottom moved off end of frame, change mode line percentage. */
14098 if (XFASTINT (w->window_end_pos) <= 0
14099 && Z != IT_CHARPOS (it))
14100 w->update_mode_line = Qt;
14101
14102 /* Set window_end_pos to the offset of the last character displayed
14103 on the window from the end of current_buffer. Set
14104 window_end_vpos to its row number. */
14105 if (last_text_row)
14106 {
14107 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14108 w->window_end_bytepos
14109 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14110 w->window_end_pos
14111 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14112 w->window_end_vpos
14113 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14114 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14115 ->displays_text_p);
14116 }
14117 else
14118 {
14119 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14120 w->window_end_pos = make_number (Z - ZV);
14121 w->window_end_vpos = make_number (0);
14122 }
14123
14124 /* But that is not valid info until redisplay finishes. */
14125 w->window_end_valid = Qnil;
14126 return 1;
14127 }
14128
14129
14130 \f
14131 /************************************************************************
14132 Window redisplay reusing current matrix when buffer has not changed
14133 ************************************************************************/
14134
14135 /* Try redisplay of window W showing an unchanged buffer with a
14136 different window start than the last time it was displayed by
14137 reusing its current matrix. Value is non-zero if successful.
14138 W->start is the new window start. */
14139
14140 static int
14141 try_window_reusing_current_matrix (w)
14142 struct window *w;
14143 {
14144 struct frame *f = XFRAME (w->frame);
14145 struct glyph_row *row, *bottom_row;
14146 struct it it;
14147 struct run run;
14148 struct text_pos start, new_start;
14149 int nrows_scrolled, i;
14150 struct glyph_row *last_text_row;
14151 struct glyph_row *last_reused_text_row;
14152 struct glyph_row *start_row;
14153 int start_vpos, min_y, max_y;
14154
14155 #if GLYPH_DEBUG
14156 if (inhibit_try_window_reusing)
14157 return 0;
14158 #endif
14159
14160 if (/* This function doesn't handle terminal frames. */
14161 !FRAME_WINDOW_P (f)
14162 /* Don't try to reuse the display if windows have been split
14163 or such. */
14164 || windows_or_buffers_changed
14165 || cursor_type_changed)
14166 return 0;
14167
14168 /* Can't do this if region may have changed. */
14169 if ((!NILP (Vtransient_mark_mode)
14170 && !NILP (current_buffer->mark_active))
14171 || !NILP (w->region_showing)
14172 || !NILP (Vshow_trailing_whitespace))
14173 return 0;
14174
14175 /* If top-line visibility has changed, give up. */
14176 if (WINDOW_WANTS_HEADER_LINE_P (w)
14177 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14178 return 0;
14179
14180 /* Give up if old or new display is scrolled vertically. We could
14181 make this function handle this, but right now it doesn't. */
14182 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14183 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14184 return 0;
14185
14186 /* The variable new_start now holds the new window start. The old
14187 start `start' can be determined from the current matrix. */
14188 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14189 start = start_row->start.pos;
14190 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14191
14192 /* Clear the desired matrix for the display below. */
14193 clear_glyph_matrix (w->desired_matrix);
14194
14195 if (CHARPOS (new_start) <= CHARPOS (start))
14196 {
14197 int first_row_y;
14198
14199 /* Don't use this method if the display starts with an ellipsis
14200 displayed for invisible text. It's not easy to handle that case
14201 below, and it's certainly not worth the effort since this is
14202 not a frequent case. */
14203 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14204 return 0;
14205
14206 IF_DEBUG (debug_method_add (w, "twu1"));
14207
14208 /* Display up to a row that can be reused. The variable
14209 last_text_row is set to the last row displayed that displays
14210 text. Note that it.vpos == 0 if or if not there is a
14211 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14212 start_display (&it, w, new_start);
14213 first_row_y = it.current_y;
14214 w->cursor.vpos = -1;
14215 last_text_row = last_reused_text_row = NULL;
14216
14217 while (it.current_y < it.last_visible_y
14218 && !fonts_changed_p)
14219 {
14220 /* If we have reached into the characters in the START row,
14221 that means the line boundaries have changed. So we
14222 can't start copying with the row START. Maybe it will
14223 work to start copying with the following row. */
14224 while (IT_CHARPOS (it) > CHARPOS (start))
14225 {
14226 /* Advance to the next row as the "start". */
14227 start_row++;
14228 start = start_row->start.pos;
14229 /* If there are no more rows to try, or just one, give up. */
14230 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14231 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14232 || CHARPOS (start) == ZV)
14233 {
14234 clear_glyph_matrix (w->desired_matrix);
14235 return 0;
14236 }
14237
14238 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14239 }
14240 /* If we have reached alignment,
14241 we can copy the rest of the rows. */
14242 if (IT_CHARPOS (it) == CHARPOS (start))
14243 break;
14244
14245 if (display_line (&it))
14246 last_text_row = it.glyph_row - 1;
14247 }
14248
14249 /* A value of current_y < last_visible_y means that we stopped
14250 at the previous window start, which in turn means that we
14251 have at least one reusable row. */
14252 if (it.current_y < it.last_visible_y)
14253 {
14254 /* IT.vpos always starts from 0; it counts text lines. */
14255 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14256
14257 /* Find PT if not already found in the lines displayed. */
14258 if (w->cursor.vpos < 0)
14259 {
14260 int dy = it.current_y - start_row->y;
14261
14262 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14263 row = row_containing_pos (w, PT, row, NULL, dy);
14264 if (row)
14265 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14266 dy, nrows_scrolled);
14267 else
14268 {
14269 clear_glyph_matrix (w->desired_matrix);
14270 return 0;
14271 }
14272 }
14273
14274 /* Scroll the display. Do it before the current matrix is
14275 changed. The problem here is that update has not yet
14276 run, i.e. part of the current matrix is not up to date.
14277 scroll_run_hook will clear the cursor, and use the
14278 current matrix to get the height of the row the cursor is
14279 in. */
14280 run.current_y = start_row->y;
14281 run.desired_y = it.current_y;
14282 run.height = it.last_visible_y - it.current_y;
14283
14284 if (run.height > 0 && run.current_y != run.desired_y)
14285 {
14286 update_begin (f);
14287 FRAME_RIF (f)->update_window_begin_hook (w);
14288 FRAME_RIF (f)->clear_window_mouse_face (w);
14289 FRAME_RIF (f)->scroll_run_hook (w, &run);
14290 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14291 update_end (f);
14292 }
14293
14294 /* Shift current matrix down by nrows_scrolled lines. */
14295 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14296 rotate_matrix (w->current_matrix,
14297 start_vpos,
14298 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14299 nrows_scrolled);
14300
14301 /* Disable lines that must be updated. */
14302 for (i = 0; i < nrows_scrolled; ++i)
14303 (start_row + i)->enabled_p = 0;
14304
14305 /* Re-compute Y positions. */
14306 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14307 max_y = it.last_visible_y;
14308 for (row = start_row + nrows_scrolled;
14309 row < bottom_row;
14310 ++row)
14311 {
14312 row->y = it.current_y;
14313 row->visible_height = row->height;
14314
14315 if (row->y < min_y)
14316 row->visible_height -= min_y - row->y;
14317 if (row->y + row->height > max_y)
14318 row->visible_height -= row->y + row->height - max_y;
14319 row->redraw_fringe_bitmaps_p = 1;
14320
14321 it.current_y += row->height;
14322
14323 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14324 last_reused_text_row = row;
14325 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14326 break;
14327 }
14328
14329 /* Disable lines in the current matrix which are now
14330 below the window. */
14331 for (++row; row < bottom_row; ++row)
14332 row->enabled_p = row->mode_line_p = 0;
14333 }
14334
14335 /* Update window_end_pos etc.; last_reused_text_row is the last
14336 reused row from the current matrix containing text, if any.
14337 The value of last_text_row is the last displayed line
14338 containing text. */
14339 if (last_reused_text_row)
14340 {
14341 w->window_end_bytepos
14342 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14343 w->window_end_pos
14344 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14345 w->window_end_vpos
14346 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14347 w->current_matrix));
14348 }
14349 else if (last_text_row)
14350 {
14351 w->window_end_bytepos
14352 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14353 w->window_end_pos
14354 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14355 w->window_end_vpos
14356 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14357 }
14358 else
14359 {
14360 /* This window must be completely empty. */
14361 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14362 w->window_end_pos = make_number (Z - ZV);
14363 w->window_end_vpos = make_number (0);
14364 }
14365 w->window_end_valid = Qnil;
14366
14367 /* Update hint: don't try scrolling again in update_window. */
14368 w->desired_matrix->no_scrolling_p = 1;
14369
14370 #if GLYPH_DEBUG
14371 debug_method_add (w, "try_window_reusing_current_matrix 1");
14372 #endif
14373 return 1;
14374 }
14375 else if (CHARPOS (new_start) > CHARPOS (start))
14376 {
14377 struct glyph_row *pt_row, *row;
14378 struct glyph_row *first_reusable_row;
14379 struct glyph_row *first_row_to_display;
14380 int dy;
14381 int yb = window_text_bottom_y (w);
14382
14383 /* Find the row starting at new_start, if there is one. Don't
14384 reuse a partially visible line at the end. */
14385 first_reusable_row = start_row;
14386 while (first_reusable_row->enabled_p
14387 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14388 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14389 < CHARPOS (new_start)))
14390 ++first_reusable_row;
14391
14392 /* Give up if there is no row to reuse. */
14393 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14394 || !first_reusable_row->enabled_p
14395 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14396 != CHARPOS (new_start)))
14397 return 0;
14398
14399 /* We can reuse fully visible rows beginning with
14400 first_reusable_row to the end of the window. Set
14401 first_row_to_display to the first row that cannot be reused.
14402 Set pt_row to the row containing point, if there is any. */
14403 pt_row = NULL;
14404 for (first_row_to_display = first_reusable_row;
14405 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14406 ++first_row_to_display)
14407 {
14408 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14409 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14410 pt_row = first_row_to_display;
14411 }
14412
14413 /* Start displaying at the start of first_row_to_display. */
14414 xassert (first_row_to_display->y < yb);
14415 init_to_row_start (&it, w, first_row_to_display);
14416
14417 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14418 - start_vpos);
14419 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14420 - nrows_scrolled);
14421 it.current_y = (first_row_to_display->y - first_reusable_row->y
14422 + WINDOW_HEADER_LINE_HEIGHT (w));
14423
14424 /* Display lines beginning with first_row_to_display in the
14425 desired matrix. Set last_text_row to the last row displayed
14426 that displays text. */
14427 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14428 if (pt_row == NULL)
14429 w->cursor.vpos = -1;
14430 last_text_row = NULL;
14431 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14432 if (display_line (&it))
14433 last_text_row = it.glyph_row - 1;
14434
14435 /* Give up If point isn't in a row displayed or reused. */
14436 if (w->cursor.vpos < 0)
14437 {
14438 clear_glyph_matrix (w->desired_matrix);
14439 return 0;
14440 }
14441
14442 /* If point is in a reused row, adjust y and vpos of the cursor
14443 position. */
14444 if (pt_row)
14445 {
14446 w->cursor.vpos -= nrows_scrolled;
14447 w->cursor.y -= first_reusable_row->y - start_row->y;
14448 }
14449
14450 /* Scroll the display. */
14451 run.current_y = first_reusable_row->y;
14452 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14453 run.height = it.last_visible_y - run.current_y;
14454 dy = run.current_y - run.desired_y;
14455
14456 if (run.height)
14457 {
14458 update_begin (f);
14459 FRAME_RIF (f)->update_window_begin_hook (w);
14460 FRAME_RIF (f)->clear_window_mouse_face (w);
14461 FRAME_RIF (f)->scroll_run_hook (w, &run);
14462 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14463 update_end (f);
14464 }
14465
14466 /* Adjust Y positions of reused rows. */
14467 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14468 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14469 max_y = it.last_visible_y;
14470 for (row = first_reusable_row; row < first_row_to_display; ++row)
14471 {
14472 row->y -= dy;
14473 row->visible_height = row->height;
14474 if (row->y < min_y)
14475 row->visible_height -= min_y - row->y;
14476 if (row->y + row->height > max_y)
14477 row->visible_height -= row->y + row->height - max_y;
14478 row->redraw_fringe_bitmaps_p = 1;
14479 }
14480
14481 /* Scroll the current matrix. */
14482 xassert (nrows_scrolled > 0);
14483 rotate_matrix (w->current_matrix,
14484 start_vpos,
14485 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14486 -nrows_scrolled);
14487
14488 /* Disable rows not reused. */
14489 for (row -= nrows_scrolled; row < bottom_row; ++row)
14490 row->enabled_p = 0;
14491
14492 /* Point may have moved to a different line, so we cannot assume that
14493 the previous cursor position is valid; locate the correct row. */
14494 if (pt_row)
14495 {
14496 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14497 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14498 row++)
14499 {
14500 w->cursor.vpos++;
14501 w->cursor.y = row->y;
14502 }
14503 if (row < bottom_row)
14504 {
14505 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14506 while (glyph->charpos < PT)
14507 {
14508 w->cursor.hpos++;
14509 w->cursor.x += glyph->pixel_width;
14510 glyph++;
14511 }
14512 }
14513 }
14514
14515 /* Adjust window end. A null value of last_text_row means that
14516 the window end is in reused rows which in turn means that
14517 only its vpos can have changed. */
14518 if (last_text_row)
14519 {
14520 w->window_end_bytepos
14521 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14522 w->window_end_pos
14523 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14524 w->window_end_vpos
14525 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14526 }
14527 else
14528 {
14529 w->window_end_vpos
14530 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14531 }
14532
14533 w->window_end_valid = Qnil;
14534 w->desired_matrix->no_scrolling_p = 1;
14535
14536 #if GLYPH_DEBUG
14537 debug_method_add (w, "try_window_reusing_current_matrix 2");
14538 #endif
14539 return 1;
14540 }
14541
14542 return 0;
14543 }
14544
14545
14546 \f
14547 /************************************************************************
14548 Window redisplay reusing current matrix when buffer has changed
14549 ************************************************************************/
14550
14551 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14552 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14553 int *, int *));
14554 static struct glyph_row *
14555 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14556 struct glyph_row *));
14557
14558
14559 /* Return the last row in MATRIX displaying text. If row START is
14560 non-null, start searching with that row. IT gives the dimensions
14561 of the display. Value is null if matrix is empty; otherwise it is
14562 a pointer to the row found. */
14563
14564 static struct glyph_row *
14565 find_last_row_displaying_text (matrix, it, start)
14566 struct glyph_matrix *matrix;
14567 struct it *it;
14568 struct glyph_row *start;
14569 {
14570 struct glyph_row *row, *row_found;
14571
14572 /* Set row_found to the last row in IT->w's current matrix
14573 displaying text. The loop looks funny but think of partially
14574 visible lines. */
14575 row_found = NULL;
14576 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14577 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14578 {
14579 xassert (row->enabled_p);
14580 row_found = row;
14581 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14582 break;
14583 ++row;
14584 }
14585
14586 return row_found;
14587 }
14588
14589
14590 /* Return the last row in the current matrix of W that is not affected
14591 by changes at the start of current_buffer that occurred since W's
14592 current matrix was built. Value is null if no such row exists.
14593
14594 BEG_UNCHANGED us the number of characters unchanged at the start of
14595 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14596 first changed character in current_buffer. Characters at positions <
14597 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14598 when the current matrix was built. */
14599
14600 static struct glyph_row *
14601 find_last_unchanged_at_beg_row (w)
14602 struct window *w;
14603 {
14604 int first_changed_pos = BEG + BEG_UNCHANGED;
14605 struct glyph_row *row;
14606 struct glyph_row *row_found = NULL;
14607 int yb = window_text_bottom_y (w);
14608
14609 /* Find the last row displaying unchanged text. */
14610 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14611 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14612 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14613 ++row)
14614 {
14615 if (/* If row ends before first_changed_pos, it is unchanged,
14616 except in some case. */
14617 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14618 /* When row ends in ZV and we write at ZV it is not
14619 unchanged. */
14620 && !row->ends_at_zv_p
14621 /* When first_changed_pos is the end of a continued line,
14622 row is not unchanged because it may be no longer
14623 continued. */
14624 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14625 && (row->continued_p
14626 || row->exact_window_width_line_p)))
14627 row_found = row;
14628
14629 /* Stop if last visible row. */
14630 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14631 break;
14632 }
14633
14634 return row_found;
14635 }
14636
14637
14638 /* Find the first glyph row in the current matrix of W that is not
14639 affected by changes at the end of current_buffer since the
14640 time W's current matrix was built.
14641
14642 Return in *DELTA the number of chars by which buffer positions in
14643 unchanged text at the end of current_buffer must be adjusted.
14644
14645 Return in *DELTA_BYTES the corresponding number of bytes.
14646
14647 Value is null if no such row exists, i.e. all rows are affected by
14648 changes. */
14649
14650 static struct glyph_row *
14651 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14652 struct window *w;
14653 int *delta, *delta_bytes;
14654 {
14655 struct glyph_row *row;
14656 struct glyph_row *row_found = NULL;
14657
14658 *delta = *delta_bytes = 0;
14659
14660 /* Display must not have been paused, otherwise the current matrix
14661 is not up to date. */
14662 eassert (!NILP (w->window_end_valid));
14663
14664 /* A value of window_end_pos >= END_UNCHANGED means that the window
14665 end is in the range of changed text. If so, there is no
14666 unchanged row at the end of W's current matrix. */
14667 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14668 return NULL;
14669
14670 /* Set row to the last row in W's current matrix displaying text. */
14671 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14672
14673 /* If matrix is entirely empty, no unchanged row exists. */
14674 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14675 {
14676 /* The value of row is the last glyph row in the matrix having a
14677 meaningful buffer position in it. The end position of row
14678 corresponds to window_end_pos. This allows us to translate
14679 buffer positions in the current matrix to current buffer
14680 positions for characters not in changed text. */
14681 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14682 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14683 int last_unchanged_pos, last_unchanged_pos_old;
14684 struct glyph_row *first_text_row
14685 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14686
14687 *delta = Z - Z_old;
14688 *delta_bytes = Z_BYTE - Z_BYTE_old;
14689
14690 /* Set last_unchanged_pos to the buffer position of the last
14691 character in the buffer that has not been changed. Z is the
14692 index + 1 of the last character in current_buffer, i.e. by
14693 subtracting END_UNCHANGED we get the index of the last
14694 unchanged character, and we have to add BEG to get its buffer
14695 position. */
14696 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14697 last_unchanged_pos_old = last_unchanged_pos - *delta;
14698
14699 /* Search backward from ROW for a row displaying a line that
14700 starts at a minimum position >= last_unchanged_pos_old. */
14701 for (; row > first_text_row; --row)
14702 {
14703 /* This used to abort, but it can happen.
14704 It is ok to just stop the search instead here. KFS. */
14705 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14706 break;
14707
14708 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14709 row_found = row;
14710 }
14711 }
14712
14713 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14714
14715 return row_found;
14716 }
14717
14718
14719 /* Make sure that glyph rows in the current matrix of window W
14720 reference the same glyph memory as corresponding rows in the
14721 frame's frame matrix. This function is called after scrolling W's
14722 current matrix on a terminal frame in try_window_id and
14723 try_window_reusing_current_matrix. */
14724
14725 static void
14726 sync_frame_with_window_matrix_rows (w)
14727 struct window *w;
14728 {
14729 struct frame *f = XFRAME (w->frame);
14730 struct glyph_row *window_row, *window_row_end, *frame_row;
14731
14732 /* Preconditions: W must be a leaf window and full-width. Its frame
14733 must have a frame matrix. */
14734 xassert (NILP (w->hchild) && NILP (w->vchild));
14735 xassert (WINDOW_FULL_WIDTH_P (w));
14736 xassert (!FRAME_WINDOW_P (f));
14737
14738 /* If W is a full-width window, glyph pointers in W's current matrix
14739 have, by definition, to be the same as glyph pointers in the
14740 corresponding frame matrix. Note that frame matrices have no
14741 marginal areas (see build_frame_matrix). */
14742 window_row = w->current_matrix->rows;
14743 window_row_end = window_row + w->current_matrix->nrows;
14744 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14745 while (window_row < window_row_end)
14746 {
14747 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14748 struct glyph *end = window_row->glyphs[LAST_AREA];
14749
14750 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14751 frame_row->glyphs[TEXT_AREA] = start;
14752 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14753 frame_row->glyphs[LAST_AREA] = end;
14754
14755 /* Disable frame rows whose corresponding window rows have
14756 been disabled in try_window_id. */
14757 if (!window_row->enabled_p)
14758 frame_row->enabled_p = 0;
14759
14760 ++window_row, ++frame_row;
14761 }
14762 }
14763
14764
14765 /* Find the glyph row in window W containing CHARPOS. Consider all
14766 rows between START and END (not inclusive). END null means search
14767 all rows to the end of the display area of W. Value is the row
14768 containing CHARPOS or null. */
14769
14770 struct glyph_row *
14771 row_containing_pos (w, charpos, start, end, dy)
14772 struct window *w;
14773 int charpos;
14774 struct glyph_row *start, *end;
14775 int dy;
14776 {
14777 struct glyph_row *row = start;
14778 int last_y;
14779
14780 /* If we happen to start on a header-line, skip that. */
14781 if (row->mode_line_p)
14782 ++row;
14783
14784 if ((end && row >= end) || !row->enabled_p)
14785 return NULL;
14786
14787 last_y = window_text_bottom_y (w) - dy;
14788
14789 while (1)
14790 {
14791 /* Give up if we have gone too far. */
14792 if (end && row >= end)
14793 return NULL;
14794 /* This formerly returned if they were equal.
14795 I think that both quantities are of a "last plus one" type;
14796 if so, when they are equal, the row is within the screen. -- rms. */
14797 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14798 return NULL;
14799
14800 /* If it is in this row, return this row. */
14801 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14802 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14803 /* The end position of a row equals the start
14804 position of the next row. If CHARPOS is there, we
14805 would rather display it in the next line, except
14806 when this line ends in ZV. */
14807 && !row->ends_at_zv_p
14808 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14809 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14810 return row;
14811 ++row;
14812 }
14813 }
14814
14815
14816 /* Try to redisplay window W by reusing its existing display. W's
14817 current matrix must be up to date when this function is called,
14818 i.e. window_end_valid must not be nil.
14819
14820 Value is
14821
14822 1 if display has been updated
14823 0 if otherwise unsuccessful
14824 -1 if redisplay with same window start is known not to succeed
14825
14826 The following steps are performed:
14827
14828 1. Find the last row in the current matrix of W that is not
14829 affected by changes at the start of current_buffer. If no such row
14830 is found, give up.
14831
14832 2. Find the first row in W's current matrix that is not affected by
14833 changes at the end of current_buffer. Maybe there is no such row.
14834
14835 3. Display lines beginning with the row + 1 found in step 1 to the
14836 row found in step 2 or, if step 2 didn't find a row, to the end of
14837 the window.
14838
14839 4. If cursor is not known to appear on the window, give up.
14840
14841 5. If display stopped at the row found in step 2, scroll the
14842 display and current matrix as needed.
14843
14844 6. Maybe display some lines at the end of W, if we must. This can
14845 happen under various circumstances, like a partially visible line
14846 becoming fully visible, or because newly displayed lines are displayed
14847 in smaller font sizes.
14848
14849 7. Update W's window end information. */
14850
14851 static int
14852 try_window_id (w)
14853 struct window *w;
14854 {
14855 struct frame *f = XFRAME (w->frame);
14856 struct glyph_matrix *current_matrix = w->current_matrix;
14857 struct glyph_matrix *desired_matrix = w->desired_matrix;
14858 struct glyph_row *last_unchanged_at_beg_row;
14859 struct glyph_row *first_unchanged_at_end_row;
14860 struct glyph_row *row;
14861 struct glyph_row *bottom_row;
14862 int bottom_vpos;
14863 struct it it;
14864 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14865 struct text_pos start_pos;
14866 struct run run;
14867 int first_unchanged_at_end_vpos = 0;
14868 struct glyph_row *last_text_row, *last_text_row_at_end;
14869 struct text_pos start;
14870 int first_changed_charpos, last_changed_charpos;
14871
14872 #if GLYPH_DEBUG
14873 if (inhibit_try_window_id)
14874 return 0;
14875 #endif
14876
14877 /* This is handy for debugging. */
14878 #if 0
14879 #define GIVE_UP(X) \
14880 do { \
14881 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14882 return 0; \
14883 } while (0)
14884 #else
14885 #define GIVE_UP(X) return 0
14886 #endif
14887
14888 SET_TEXT_POS_FROM_MARKER (start, w->start);
14889
14890 /* Don't use this for mini-windows because these can show
14891 messages and mini-buffers, and we don't handle that here. */
14892 if (MINI_WINDOW_P (w))
14893 GIVE_UP (1);
14894
14895 /* This flag is used to prevent redisplay optimizations. */
14896 if (windows_or_buffers_changed || cursor_type_changed)
14897 GIVE_UP (2);
14898
14899 /* Verify that narrowing has not changed.
14900 Also verify that we were not told to prevent redisplay optimizations.
14901 It would be nice to further
14902 reduce the number of cases where this prevents try_window_id. */
14903 if (current_buffer->clip_changed
14904 || current_buffer->prevent_redisplay_optimizations_p)
14905 GIVE_UP (3);
14906
14907 /* Window must either use window-based redisplay or be full width. */
14908 if (!FRAME_WINDOW_P (f)
14909 && (!FRAME_LINE_INS_DEL_OK (f)
14910 || !WINDOW_FULL_WIDTH_P (w)))
14911 GIVE_UP (4);
14912
14913 /* Give up if point is not known NOT to appear in W. */
14914 if (PT < CHARPOS (start))
14915 GIVE_UP (5);
14916
14917 /* Another way to prevent redisplay optimizations. */
14918 if (XFASTINT (w->last_modified) == 0)
14919 GIVE_UP (6);
14920
14921 /* Verify that window is not hscrolled. */
14922 if (XFASTINT (w->hscroll) != 0)
14923 GIVE_UP (7);
14924
14925 /* Verify that display wasn't paused. */
14926 if (NILP (w->window_end_valid))
14927 GIVE_UP (8);
14928
14929 /* Can't use this if highlighting a region because a cursor movement
14930 will do more than just set the cursor. */
14931 if (!NILP (Vtransient_mark_mode)
14932 && !NILP (current_buffer->mark_active))
14933 GIVE_UP (9);
14934
14935 /* Likewise if highlighting trailing whitespace. */
14936 if (!NILP (Vshow_trailing_whitespace))
14937 GIVE_UP (11);
14938
14939 /* Likewise if showing a region. */
14940 if (!NILP (w->region_showing))
14941 GIVE_UP (10);
14942
14943 /* Can use this if overlay arrow position and or string have changed. */
14944 if (overlay_arrows_changed_p ())
14945 GIVE_UP (12);
14946
14947 /* When word-wrap is on, adding a space to the first word of a
14948 wrapped line can change the wrap position, altering the line
14949 above it. It might be worthwhile to handle this more
14950 intelligently, but for now just redisplay from scratch. */
14951 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14952 GIVE_UP (21);
14953
14954 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14955 only if buffer has really changed. The reason is that the gap is
14956 initially at Z for freshly visited files. The code below would
14957 set end_unchanged to 0 in that case. */
14958 if (MODIFF > SAVE_MODIFF
14959 /* This seems to happen sometimes after saving a buffer. */
14960 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14961 {
14962 if (GPT - BEG < BEG_UNCHANGED)
14963 BEG_UNCHANGED = GPT - BEG;
14964 if (Z - GPT < END_UNCHANGED)
14965 END_UNCHANGED = Z - GPT;
14966 }
14967
14968 /* The position of the first and last character that has been changed. */
14969 first_changed_charpos = BEG + BEG_UNCHANGED;
14970 last_changed_charpos = Z - END_UNCHANGED;
14971
14972 /* If window starts after a line end, and the last change is in
14973 front of that newline, then changes don't affect the display.
14974 This case happens with stealth-fontification. Note that although
14975 the display is unchanged, glyph positions in the matrix have to
14976 be adjusted, of course. */
14977 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14978 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14979 && ((last_changed_charpos < CHARPOS (start)
14980 && CHARPOS (start) == BEGV)
14981 || (last_changed_charpos < CHARPOS (start) - 1
14982 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14983 {
14984 int Z_old, delta, Z_BYTE_old, delta_bytes;
14985 struct glyph_row *r0;
14986
14987 /* Compute how many chars/bytes have been added to or removed
14988 from the buffer. */
14989 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14990 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14991 delta = Z - Z_old;
14992 delta_bytes = Z_BYTE - Z_BYTE_old;
14993
14994 /* Give up if PT is not in the window. Note that it already has
14995 been checked at the start of try_window_id that PT is not in
14996 front of the window start. */
14997 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14998 GIVE_UP (13);
14999
15000 /* If window start is unchanged, we can reuse the whole matrix
15001 as is, after adjusting glyph positions. No need to compute
15002 the window end again, since its offset from Z hasn't changed. */
15003 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15004 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15005 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15006 /* PT must not be in a partially visible line. */
15007 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15008 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15009 {
15010 /* Adjust positions in the glyph matrix. */
15011 if (delta || delta_bytes)
15012 {
15013 struct glyph_row *r1
15014 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15015 increment_matrix_positions (w->current_matrix,
15016 MATRIX_ROW_VPOS (r0, current_matrix),
15017 MATRIX_ROW_VPOS (r1, current_matrix),
15018 delta, delta_bytes);
15019 }
15020
15021 /* Set the cursor. */
15022 row = row_containing_pos (w, PT, r0, NULL, 0);
15023 if (row)
15024 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15025 else
15026 abort ();
15027 return 1;
15028 }
15029 }
15030
15031 /* Handle the case that changes are all below what is displayed in
15032 the window, and that PT is in the window. This shortcut cannot
15033 be taken if ZV is visible in the window, and text has been added
15034 there that is visible in the window. */
15035 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15036 /* ZV is not visible in the window, or there are no
15037 changes at ZV, actually. */
15038 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15039 || first_changed_charpos == last_changed_charpos))
15040 {
15041 struct glyph_row *r0;
15042
15043 /* Give up if PT is not in the window. Note that it already has
15044 been checked at the start of try_window_id that PT is not in
15045 front of the window start. */
15046 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15047 GIVE_UP (14);
15048
15049 /* If window start is unchanged, we can reuse the whole matrix
15050 as is, without changing glyph positions since no text has
15051 been added/removed in front of the window end. */
15052 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15053 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15054 /* PT must not be in a partially visible line. */
15055 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15056 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15057 {
15058 /* We have to compute the window end anew since text
15059 can have been added/removed after it. */
15060 w->window_end_pos
15061 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15062 w->window_end_bytepos
15063 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15064
15065 /* Set the cursor. */
15066 row = row_containing_pos (w, PT, r0, NULL, 0);
15067 if (row)
15068 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15069 else
15070 abort ();
15071 return 2;
15072 }
15073 }
15074
15075 /* Give up if window start is in the changed area.
15076
15077 The condition used to read
15078
15079 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15080
15081 but why that was tested escapes me at the moment. */
15082 if (CHARPOS (start) >= first_changed_charpos
15083 && CHARPOS (start) <= last_changed_charpos)
15084 GIVE_UP (15);
15085
15086 /* Check that window start agrees with the start of the first glyph
15087 row in its current matrix. Check this after we know the window
15088 start is not in changed text, otherwise positions would not be
15089 comparable. */
15090 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15091 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15092 GIVE_UP (16);
15093
15094 /* Give up if the window ends in strings. Overlay strings
15095 at the end are difficult to handle, so don't try. */
15096 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15097 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15098 GIVE_UP (20);
15099
15100 /* Compute the position at which we have to start displaying new
15101 lines. Some of the lines at the top of the window might be
15102 reusable because they are not displaying changed text. Find the
15103 last row in W's current matrix not affected by changes at the
15104 start of current_buffer. Value is null if changes start in the
15105 first line of window. */
15106 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15107 if (last_unchanged_at_beg_row)
15108 {
15109 /* Avoid starting to display in the moddle of a character, a TAB
15110 for instance. This is easier than to set up the iterator
15111 exactly, and it's not a frequent case, so the additional
15112 effort wouldn't really pay off. */
15113 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15114 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15115 && last_unchanged_at_beg_row > w->current_matrix->rows)
15116 --last_unchanged_at_beg_row;
15117
15118 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15119 GIVE_UP (17);
15120
15121 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15122 GIVE_UP (18);
15123 start_pos = it.current.pos;
15124
15125 /* Start displaying new lines in the desired matrix at the same
15126 vpos we would use in the current matrix, i.e. below
15127 last_unchanged_at_beg_row. */
15128 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15129 current_matrix);
15130 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15131 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15132
15133 xassert (it.hpos == 0 && it.current_x == 0);
15134 }
15135 else
15136 {
15137 /* There are no reusable lines at the start of the window.
15138 Start displaying in the first text line. */
15139 start_display (&it, w, start);
15140 it.vpos = it.first_vpos;
15141 start_pos = it.current.pos;
15142 }
15143
15144 /* Find the first row that is not affected by changes at the end of
15145 the buffer. Value will be null if there is no unchanged row, in
15146 which case we must redisplay to the end of the window. delta
15147 will be set to the value by which buffer positions beginning with
15148 first_unchanged_at_end_row have to be adjusted due to text
15149 changes. */
15150 first_unchanged_at_end_row
15151 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15152 IF_DEBUG (debug_delta = delta);
15153 IF_DEBUG (debug_delta_bytes = delta_bytes);
15154
15155 /* Set stop_pos to the buffer position up to which we will have to
15156 display new lines. If first_unchanged_at_end_row != NULL, this
15157 is the buffer position of the start of the line displayed in that
15158 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15159 that we don't stop at a buffer position. */
15160 stop_pos = 0;
15161 if (first_unchanged_at_end_row)
15162 {
15163 xassert (last_unchanged_at_beg_row == NULL
15164 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15165
15166 /* If this is a continuation line, move forward to the next one
15167 that isn't. Changes in lines above affect this line.
15168 Caution: this may move first_unchanged_at_end_row to a row
15169 not displaying text. */
15170 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15171 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15172 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15173 < it.last_visible_y))
15174 ++first_unchanged_at_end_row;
15175
15176 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15177 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15178 >= it.last_visible_y))
15179 first_unchanged_at_end_row = NULL;
15180 else
15181 {
15182 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15183 + delta);
15184 first_unchanged_at_end_vpos
15185 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15186 xassert (stop_pos >= Z - END_UNCHANGED);
15187 }
15188 }
15189 else if (last_unchanged_at_beg_row == NULL)
15190 GIVE_UP (19);
15191
15192
15193 #if GLYPH_DEBUG
15194
15195 /* Either there is no unchanged row at the end, or the one we have
15196 now displays text. This is a necessary condition for the window
15197 end pos calculation at the end of this function. */
15198 xassert (first_unchanged_at_end_row == NULL
15199 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15200
15201 debug_last_unchanged_at_beg_vpos
15202 = (last_unchanged_at_beg_row
15203 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15204 : -1);
15205 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15206
15207 #endif /* GLYPH_DEBUG != 0 */
15208
15209
15210 /* Display new lines. Set last_text_row to the last new line
15211 displayed which has text on it, i.e. might end up as being the
15212 line where the window_end_vpos is. */
15213 w->cursor.vpos = -1;
15214 last_text_row = NULL;
15215 overlay_arrow_seen = 0;
15216 while (it.current_y < it.last_visible_y
15217 && !fonts_changed_p
15218 && (first_unchanged_at_end_row == NULL
15219 || IT_CHARPOS (it) < stop_pos))
15220 {
15221 if (display_line (&it))
15222 last_text_row = it.glyph_row - 1;
15223 }
15224
15225 if (fonts_changed_p)
15226 return -1;
15227
15228
15229 /* Compute differences in buffer positions, y-positions etc. for
15230 lines reused at the bottom of the window. Compute what we can
15231 scroll. */
15232 if (first_unchanged_at_end_row
15233 /* No lines reused because we displayed everything up to the
15234 bottom of the window. */
15235 && it.current_y < it.last_visible_y)
15236 {
15237 dvpos = (it.vpos
15238 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15239 current_matrix));
15240 dy = it.current_y - first_unchanged_at_end_row->y;
15241 run.current_y = first_unchanged_at_end_row->y;
15242 run.desired_y = run.current_y + dy;
15243 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15244 }
15245 else
15246 {
15247 delta = delta_bytes = dvpos = dy
15248 = run.current_y = run.desired_y = run.height = 0;
15249 first_unchanged_at_end_row = NULL;
15250 }
15251 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15252
15253
15254 /* Find the cursor if not already found. We have to decide whether
15255 PT will appear on this window (it sometimes doesn't, but this is
15256 not a very frequent case.) This decision has to be made before
15257 the current matrix is altered. A value of cursor.vpos < 0 means
15258 that PT is either in one of the lines beginning at
15259 first_unchanged_at_end_row or below the window. Don't care for
15260 lines that might be displayed later at the window end; as
15261 mentioned, this is not a frequent case. */
15262 if (w->cursor.vpos < 0)
15263 {
15264 /* Cursor in unchanged rows at the top? */
15265 if (PT < CHARPOS (start_pos)
15266 && last_unchanged_at_beg_row)
15267 {
15268 row = row_containing_pos (w, PT,
15269 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15270 last_unchanged_at_beg_row + 1, 0);
15271 if (row)
15272 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15273 }
15274
15275 /* Start from first_unchanged_at_end_row looking for PT. */
15276 else if (first_unchanged_at_end_row)
15277 {
15278 row = row_containing_pos (w, PT - delta,
15279 first_unchanged_at_end_row, NULL, 0);
15280 if (row)
15281 set_cursor_from_row (w, row, w->current_matrix, delta,
15282 delta_bytes, dy, dvpos);
15283 }
15284
15285 /* Give up if cursor was not found. */
15286 if (w->cursor.vpos < 0)
15287 {
15288 clear_glyph_matrix (w->desired_matrix);
15289 return -1;
15290 }
15291 }
15292
15293 /* Don't let the cursor end in the scroll margins. */
15294 {
15295 int this_scroll_margin, cursor_height;
15296
15297 this_scroll_margin = max (0, scroll_margin);
15298 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15299 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15300 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15301
15302 if ((w->cursor.y < this_scroll_margin
15303 && CHARPOS (start) > BEGV)
15304 /* Old redisplay didn't take scroll margin into account at the bottom,
15305 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15306 || (w->cursor.y + (make_cursor_line_fully_visible_p
15307 ? cursor_height + this_scroll_margin
15308 : 1)) > it.last_visible_y)
15309 {
15310 w->cursor.vpos = -1;
15311 clear_glyph_matrix (w->desired_matrix);
15312 return -1;
15313 }
15314 }
15315
15316 /* Scroll the display. Do it before changing the current matrix so
15317 that xterm.c doesn't get confused about where the cursor glyph is
15318 found. */
15319 if (dy && run.height)
15320 {
15321 update_begin (f);
15322
15323 if (FRAME_WINDOW_P (f))
15324 {
15325 FRAME_RIF (f)->update_window_begin_hook (w);
15326 FRAME_RIF (f)->clear_window_mouse_face (w);
15327 FRAME_RIF (f)->scroll_run_hook (w, &run);
15328 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15329 }
15330 else
15331 {
15332 /* Terminal frame. In this case, dvpos gives the number of
15333 lines to scroll by; dvpos < 0 means scroll up. */
15334 int first_unchanged_at_end_vpos
15335 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15336 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15337 int end = (WINDOW_TOP_EDGE_LINE (w)
15338 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15339 + window_internal_height (w));
15340
15341 /* Perform the operation on the screen. */
15342 if (dvpos > 0)
15343 {
15344 /* Scroll last_unchanged_at_beg_row to the end of the
15345 window down dvpos lines. */
15346 set_terminal_window (f, end);
15347
15348 /* On dumb terminals delete dvpos lines at the end
15349 before inserting dvpos empty lines. */
15350 if (!FRAME_SCROLL_REGION_OK (f))
15351 ins_del_lines (f, end - dvpos, -dvpos);
15352
15353 /* Insert dvpos empty lines in front of
15354 last_unchanged_at_beg_row. */
15355 ins_del_lines (f, from, dvpos);
15356 }
15357 else if (dvpos < 0)
15358 {
15359 /* Scroll up last_unchanged_at_beg_vpos to the end of
15360 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15361 set_terminal_window (f, end);
15362
15363 /* Delete dvpos lines in front of
15364 last_unchanged_at_beg_vpos. ins_del_lines will set
15365 the cursor to the given vpos and emit |dvpos| delete
15366 line sequences. */
15367 ins_del_lines (f, from + dvpos, dvpos);
15368
15369 /* On a dumb terminal insert dvpos empty lines at the
15370 end. */
15371 if (!FRAME_SCROLL_REGION_OK (f))
15372 ins_del_lines (f, end + dvpos, -dvpos);
15373 }
15374
15375 set_terminal_window (f, 0);
15376 }
15377
15378 update_end (f);
15379 }
15380
15381 /* Shift reused rows of the current matrix to the right position.
15382 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15383 text. */
15384 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15385 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15386 if (dvpos < 0)
15387 {
15388 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15389 bottom_vpos, dvpos);
15390 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15391 bottom_vpos, 0);
15392 }
15393 else if (dvpos > 0)
15394 {
15395 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15396 bottom_vpos, dvpos);
15397 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15398 first_unchanged_at_end_vpos + dvpos, 0);
15399 }
15400
15401 /* For frame-based redisplay, make sure that current frame and window
15402 matrix are in sync with respect to glyph memory. */
15403 if (!FRAME_WINDOW_P (f))
15404 sync_frame_with_window_matrix_rows (w);
15405
15406 /* Adjust buffer positions in reused rows. */
15407 if (delta || delta_bytes)
15408 increment_matrix_positions (current_matrix,
15409 first_unchanged_at_end_vpos + dvpos,
15410 bottom_vpos, delta, delta_bytes);
15411
15412 /* Adjust Y positions. */
15413 if (dy)
15414 shift_glyph_matrix (w, current_matrix,
15415 first_unchanged_at_end_vpos + dvpos,
15416 bottom_vpos, dy);
15417
15418 if (first_unchanged_at_end_row)
15419 {
15420 first_unchanged_at_end_row += dvpos;
15421 if (first_unchanged_at_end_row->y >= it.last_visible_y
15422 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15423 first_unchanged_at_end_row = NULL;
15424 }
15425
15426 /* If scrolling up, there may be some lines to display at the end of
15427 the window. */
15428 last_text_row_at_end = NULL;
15429 if (dy < 0)
15430 {
15431 /* Scrolling up can leave for example a partially visible line
15432 at the end of the window to be redisplayed. */
15433 /* Set last_row to the glyph row in the current matrix where the
15434 window end line is found. It has been moved up or down in
15435 the matrix by dvpos. */
15436 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15437 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15438
15439 /* If last_row is the window end line, it should display text. */
15440 xassert (last_row->displays_text_p);
15441
15442 /* If window end line was partially visible before, begin
15443 displaying at that line. Otherwise begin displaying with the
15444 line following it. */
15445 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15446 {
15447 init_to_row_start (&it, w, last_row);
15448 it.vpos = last_vpos;
15449 it.current_y = last_row->y;
15450 }
15451 else
15452 {
15453 init_to_row_end (&it, w, last_row);
15454 it.vpos = 1 + last_vpos;
15455 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15456 ++last_row;
15457 }
15458
15459 /* We may start in a continuation line. If so, we have to
15460 get the right continuation_lines_width and current_x. */
15461 it.continuation_lines_width = last_row->continuation_lines_width;
15462 it.hpos = it.current_x = 0;
15463
15464 /* Display the rest of the lines at the window end. */
15465 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15466 while (it.current_y < it.last_visible_y
15467 && !fonts_changed_p)
15468 {
15469 /* Is it always sure that the display agrees with lines in
15470 the current matrix? I don't think so, so we mark rows
15471 displayed invalid in the current matrix by setting their
15472 enabled_p flag to zero. */
15473 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15474 if (display_line (&it))
15475 last_text_row_at_end = it.glyph_row - 1;
15476 }
15477 }
15478
15479 /* Update window_end_pos and window_end_vpos. */
15480 if (first_unchanged_at_end_row
15481 && !last_text_row_at_end)
15482 {
15483 /* Window end line if one of the preserved rows from the current
15484 matrix. Set row to the last row displaying text in current
15485 matrix starting at first_unchanged_at_end_row, after
15486 scrolling. */
15487 xassert (first_unchanged_at_end_row->displays_text_p);
15488 row = find_last_row_displaying_text (w->current_matrix, &it,
15489 first_unchanged_at_end_row);
15490 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15491
15492 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15493 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15494 w->window_end_vpos
15495 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15496 xassert (w->window_end_bytepos >= 0);
15497 IF_DEBUG (debug_method_add (w, "A"));
15498 }
15499 else if (last_text_row_at_end)
15500 {
15501 w->window_end_pos
15502 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15503 w->window_end_bytepos
15504 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15505 w->window_end_vpos
15506 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15507 xassert (w->window_end_bytepos >= 0);
15508 IF_DEBUG (debug_method_add (w, "B"));
15509 }
15510 else if (last_text_row)
15511 {
15512 /* We have displayed either to the end of the window or at the
15513 end of the window, i.e. the last row with text is to be found
15514 in the desired matrix. */
15515 w->window_end_pos
15516 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15517 w->window_end_bytepos
15518 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15519 w->window_end_vpos
15520 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15521 xassert (w->window_end_bytepos >= 0);
15522 }
15523 else if (first_unchanged_at_end_row == NULL
15524 && last_text_row == NULL
15525 && last_text_row_at_end == NULL)
15526 {
15527 /* Displayed to end of window, but no line containing text was
15528 displayed. Lines were deleted at the end of the window. */
15529 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15530 int vpos = XFASTINT (w->window_end_vpos);
15531 struct glyph_row *current_row = current_matrix->rows + vpos;
15532 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15533
15534 for (row = NULL;
15535 row == NULL && vpos >= first_vpos;
15536 --vpos, --current_row, --desired_row)
15537 {
15538 if (desired_row->enabled_p)
15539 {
15540 if (desired_row->displays_text_p)
15541 row = desired_row;
15542 }
15543 else if (current_row->displays_text_p)
15544 row = current_row;
15545 }
15546
15547 xassert (row != NULL);
15548 w->window_end_vpos = make_number (vpos + 1);
15549 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15550 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15551 xassert (w->window_end_bytepos >= 0);
15552 IF_DEBUG (debug_method_add (w, "C"));
15553 }
15554 else
15555 abort ();
15556
15557 #if 0 /* This leads to problems, for instance when the cursor is
15558 at ZV, and the cursor line displays no text. */
15559 /* Disable rows below what's displayed in the window. This makes
15560 debugging easier. */
15561 enable_glyph_matrix_rows (current_matrix,
15562 XFASTINT (w->window_end_vpos) + 1,
15563 bottom_vpos, 0);
15564 #endif
15565
15566 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15567 debug_end_vpos = XFASTINT (w->window_end_vpos));
15568
15569 /* Record that display has not been completed. */
15570 w->window_end_valid = Qnil;
15571 w->desired_matrix->no_scrolling_p = 1;
15572 return 3;
15573
15574 #undef GIVE_UP
15575 }
15576
15577
15578 \f
15579 /***********************************************************************
15580 More debugging support
15581 ***********************************************************************/
15582
15583 #if GLYPH_DEBUG
15584
15585 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15586 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15587 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15588
15589
15590 /* Dump the contents of glyph matrix MATRIX on stderr.
15591
15592 GLYPHS 0 means don't show glyph contents.
15593 GLYPHS 1 means show glyphs in short form
15594 GLYPHS > 1 means show glyphs in long form. */
15595
15596 void
15597 dump_glyph_matrix (matrix, glyphs)
15598 struct glyph_matrix *matrix;
15599 int glyphs;
15600 {
15601 int i;
15602 for (i = 0; i < matrix->nrows; ++i)
15603 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15604 }
15605
15606
15607 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15608 the glyph row and area where the glyph comes from. */
15609
15610 void
15611 dump_glyph (row, glyph, area)
15612 struct glyph_row *row;
15613 struct glyph *glyph;
15614 int area;
15615 {
15616 if (glyph->type == CHAR_GLYPH)
15617 {
15618 fprintf (stderr,
15619 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15620 glyph - row->glyphs[TEXT_AREA],
15621 'C',
15622 glyph->charpos,
15623 (BUFFERP (glyph->object)
15624 ? 'B'
15625 : (STRINGP (glyph->object)
15626 ? 'S'
15627 : '-')),
15628 glyph->pixel_width,
15629 glyph->u.ch,
15630 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15631 ? glyph->u.ch
15632 : '.'),
15633 glyph->face_id,
15634 glyph->left_box_line_p,
15635 glyph->right_box_line_p);
15636 }
15637 else if (glyph->type == STRETCH_GLYPH)
15638 {
15639 fprintf (stderr,
15640 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15641 glyph - row->glyphs[TEXT_AREA],
15642 'S',
15643 glyph->charpos,
15644 (BUFFERP (glyph->object)
15645 ? 'B'
15646 : (STRINGP (glyph->object)
15647 ? 'S'
15648 : '-')),
15649 glyph->pixel_width,
15650 0,
15651 '.',
15652 glyph->face_id,
15653 glyph->left_box_line_p,
15654 glyph->right_box_line_p);
15655 }
15656 else if (glyph->type == IMAGE_GLYPH)
15657 {
15658 fprintf (stderr,
15659 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15660 glyph - row->glyphs[TEXT_AREA],
15661 'I',
15662 glyph->charpos,
15663 (BUFFERP (glyph->object)
15664 ? 'B'
15665 : (STRINGP (glyph->object)
15666 ? 'S'
15667 : '-')),
15668 glyph->pixel_width,
15669 glyph->u.img_id,
15670 '.',
15671 glyph->face_id,
15672 glyph->left_box_line_p,
15673 glyph->right_box_line_p);
15674 }
15675 else if (glyph->type == COMPOSITE_GLYPH)
15676 {
15677 fprintf (stderr,
15678 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15679 glyph - row->glyphs[TEXT_AREA],
15680 '+',
15681 glyph->charpos,
15682 (BUFFERP (glyph->object)
15683 ? 'B'
15684 : (STRINGP (glyph->object)
15685 ? 'S'
15686 : '-')),
15687 glyph->pixel_width,
15688 glyph->u.cmp_id,
15689 '.',
15690 glyph->face_id,
15691 glyph->left_box_line_p,
15692 glyph->right_box_line_p);
15693 }
15694 }
15695
15696
15697 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15698 GLYPHS 0 means don't show glyph contents.
15699 GLYPHS 1 means show glyphs in short form
15700 GLYPHS > 1 means show glyphs in long form. */
15701
15702 void
15703 dump_glyph_row (row, vpos, glyphs)
15704 struct glyph_row *row;
15705 int vpos, glyphs;
15706 {
15707 if (glyphs != 1)
15708 {
15709 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15710 fprintf (stderr, "======================================================================\n");
15711
15712 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15713 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15714 vpos,
15715 MATRIX_ROW_START_CHARPOS (row),
15716 MATRIX_ROW_END_CHARPOS (row),
15717 row->used[TEXT_AREA],
15718 row->contains_overlapping_glyphs_p,
15719 row->enabled_p,
15720 row->truncated_on_left_p,
15721 row->truncated_on_right_p,
15722 row->continued_p,
15723 MATRIX_ROW_CONTINUATION_LINE_P (row),
15724 row->displays_text_p,
15725 row->ends_at_zv_p,
15726 row->fill_line_p,
15727 row->ends_in_middle_of_char_p,
15728 row->starts_in_middle_of_char_p,
15729 row->mouse_face_p,
15730 row->x,
15731 row->y,
15732 row->pixel_width,
15733 row->height,
15734 row->visible_height,
15735 row->ascent,
15736 row->phys_ascent);
15737 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15738 row->end.overlay_string_index,
15739 row->continuation_lines_width);
15740 fprintf (stderr, "%9d %5d\n",
15741 CHARPOS (row->start.string_pos),
15742 CHARPOS (row->end.string_pos));
15743 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15744 row->end.dpvec_index);
15745 }
15746
15747 if (glyphs > 1)
15748 {
15749 int area;
15750
15751 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15752 {
15753 struct glyph *glyph = row->glyphs[area];
15754 struct glyph *glyph_end = glyph + row->used[area];
15755
15756 /* Glyph for a line end in text. */
15757 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15758 ++glyph_end;
15759
15760 if (glyph < glyph_end)
15761 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15762
15763 for (; glyph < glyph_end; ++glyph)
15764 dump_glyph (row, glyph, area);
15765 }
15766 }
15767 else if (glyphs == 1)
15768 {
15769 int area;
15770
15771 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15772 {
15773 char *s = (char *) alloca (row->used[area] + 1);
15774 int i;
15775
15776 for (i = 0; i < row->used[area]; ++i)
15777 {
15778 struct glyph *glyph = row->glyphs[area] + i;
15779 if (glyph->type == CHAR_GLYPH
15780 && glyph->u.ch < 0x80
15781 && glyph->u.ch >= ' ')
15782 s[i] = glyph->u.ch;
15783 else
15784 s[i] = '.';
15785 }
15786
15787 s[i] = '\0';
15788 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15789 }
15790 }
15791 }
15792
15793
15794 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15795 Sdump_glyph_matrix, 0, 1, "p",
15796 doc: /* Dump the current matrix of the selected window to stderr.
15797 Shows contents of glyph row structures. With non-nil
15798 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15799 glyphs in short form, otherwise show glyphs in long form. */)
15800 (glyphs)
15801 Lisp_Object glyphs;
15802 {
15803 struct window *w = XWINDOW (selected_window);
15804 struct buffer *buffer = XBUFFER (w->buffer);
15805
15806 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15807 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15808 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15809 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15810 fprintf (stderr, "=============================================\n");
15811 dump_glyph_matrix (w->current_matrix,
15812 NILP (glyphs) ? 0 : XINT (glyphs));
15813 return Qnil;
15814 }
15815
15816
15817 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15818 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15819 ()
15820 {
15821 struct frame *f = XFRAME (selected_frame);
15822 dump_glyph_matrix (f->current_matrix, 1);
15823 return Qnil;
15824 }
15825
15826
15827 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15828 doc: /* Dump glyph row ROW to stderr.
15829 GLYPH 0 means don't dump glyphs.
15830 GLYPH 1 means dump glyphs in short form.
15831 GLYPH > 1 or omitted means dump glyphs in long form. */)
15832 (row, glyphs)
15833 Lisp_Object row, glyphs;
15834 {
15835 struct glyph_matrix *matrix;
15836 int vpos;
15837
15838 CHECK_NUMBER (row);
15839 matrix = XWINDOW (selected_window)->current_matrix;
15840 vpos = XINT (row);
15841 if (vpos >= 0 && vpos < matrix->nrows)
15842 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15843 vpos,
15844 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15845 return Qnil;
15846 }
15847
15848
15849 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15850 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15851 GLYPH 0 means don't dump glyphs.
15852 GLYPH 1 means dump glyphs in short form.
15853 GLYPH > 1 or omitted means dump glyphs in long form. */)
15854 (row, glyphs)
15855 Lisp_Object row, glyphs;
15856 {
15857 struct frame *sf = SELECTED_FRAME ();
15858 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15859 int vpos;
15860
15861 CHECK_NUMBER (row);
15862 vpos = XINT (row);
15863 if (vpos >= 0 && vpos < m->nrows)
15864 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15865 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15866 return Qnil;
15867 }
15868
15869
15870 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15871 doc: /* Toggle tracing of redisplay.
15872 With ARG, turn tracing on if and only if ARG is positive. */)
15873 (arg)
15874 Lisp_Object arg;
15875 {
15876 if (NILP (arg))
15877 trace_redisplay_p = !trace_redisplay_p;
15878 else
15879 {
15880 arg = Fprefix_numeric_value (arg);
15881 trace_redisplay_p = XINT (arg) > 0;
15882 }
15883
15884 return Qnil;
15885 }
15886
15887
15888 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15889 doc: /* Like `format', but print result to stderr.
15890 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15891 (nargs, args)
15892 int nargs;
15893 Lisp_Object *args;
15894 {
15895 Lisp_Object s = Fformat (nargs, args);
15896 fprintf (stderr, "%s", SDATA (s));
15897 return Qnil;
15898 }
15899
15900 #endif /* GLYPH_DEBUG */
15901
15902
15903 \f
15904 /***********************************************************************
15905 Building Desired Matrix Rows
15906 ***********************************************************************/
15907
15908 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15909 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15910
15911 static struct glyph_row *
15912 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15913 struct window *w;
15914 Lisp_Object overlay_arrow_string;
15915 {
15916 struct frame *f = XFRAME (WINDOW_FRAME (w));
15917 struct buffer *buffer = XBUFFER (w->buffer);
15918 struct buffer *old = current_buffer;
15919 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15920 int arrow_len = SCHARS (overlay_arrow_string);
15921 const unsigned char *arrow_end = arrow_string + arrow_len;
15922 const unsigned char *p;
15923 struct it it;
15924 int multibyte_p;
15925 int n_glyphs_before;
15926
15927 set_buffer_temp (buffer);
15928 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15929 it.glyph_row->used[TEXT_AREA] = 0;
15930 SET_TEXT_POS (it.position, 0, 0);
15931
15932 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15933 p = arrow_string;
15934 while (p < arrow_end)
15935 {
15936 Lisp_Object face, ilisp;
15937
15938 /* Get the next character. */
15939 if (multibyte_p)
15940 it.c = string_char_and_length (p, arrow_len, &it.len);
15941 else
15942 it.c = *p, it.len = 1;
15943 p += it.len;
15944
15945 /* Get its face. */
15946 ilisp = make_number (p - arrow_string);
15947 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15948 it.face_id = compute_char_face (f, it.c, face);
15949
15950 /* Compute its width, get its glyphs. */
15951 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15952 SET_TEXT_POS (it.position, -1, -1);
15953 PRODUCE_GLYPHS (&it);
15954
15955 /* If this character doesn't fit any more in the line, we have
15956 to remove some glyphs. */
15957 if (it.current_x > it.last_visible_x)
15958 {
15959 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15960 break;
15961 }
15962 }
15963
15964 set_buffer_temp (old);
15965 return it.glyph_row;
15966 }
15967
15968
15969 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15970 glyphs are only inserted for terminal frames since we can't really
15971 win with truncation glyphs when partially visible glyphs are
15972 involved. Which glyphs to insert is determined by
15973 produce_special_glyphs. */
15974
15975 static void
15976 insert_left_trunc_glyphs (it)
15977 struct it *it;
15978 {
15979 struct it truncate_it;
15980 struct glyph *from, *end, *to, *toend;
15981
15982 xassert (!FRAME_WINDOW_P (it->f));
15983
15984 /* Get the truncation glyphs. */
15985 truncate_it = *it;
15986 truncate_it.current_x = 0;
15987 truncate_it.face_id = DEFAULT_FACE_ID;
15988 truncate_it.glyph_row = &scratch_glyph_row;
15989 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15990 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15991 truncate_it.object = make_number (0);
15992 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15993
15994 /* Overwrite glyphs from IT with truncation glyphs. */
15995 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15996 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15997 to = it->glyph_row->glyphs[TEXT_AREA];
15998 toend = to + it->glyph_row->used[TEXT_AREA];
15999
16000 while (from < end)
16001 *to++ = *from++;
16002
16003 /* There may be padding glyphs left over. Overwrite them too. */
16004 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16005 {
16006 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16007 while (from < end)
16008 *to++ = *from++;
16009 }
16010
16011 if (to > toend)
16012 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16013 }
16014
16015
16016 /* Compute the pixel height and width of IT->glyph_row.
16017
16018 Most of the time, ascent and height of a display line will be equal
16019 to the max_ascent and max_height values of the display iterator
16020 structure. This is not the case if
16021
16022 1. We hit ZV without displaying anything. In this case, max_ascent
16023 and max_height will be zero.
16024
16025 2. We have some glyphs that don't contribute to the line height.
16026 (The glyph row flag contributes_to_line_height_p is for future
16027 pixmap extensions).
16028
16029 The first case is easily covered by using default values because in
16030 these cases, the line height does not really matter, except that it
16031 must not be zero. */
16032
16033 static void
16034 compute_line_metrics (it)
16035 struct it *it;
16036 {
16037 struct glyph_row *row = it->glyph_row;
16038 int area, i;
16039
16040 if (FRAME_WINDOW_P (it->f))
16041 {
16042 int i, min_y, max_y;
16043
16044 /* The line may consist of one space only, that was added to
16045 place the cursor on it. If so, the row's height hasn't been
16046 computed yet. */
16047 if (row->height == 0)
16048 {
16049 if (it->max_ascent + it->max_descent == 0)
16050 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
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
16058 /* Compute the width of this line. */
16059 row->pixel_width = row->x;
16060 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16061 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16062
16063 xassert (row->pixel_width >= 0);
16064 xassert (row->ascent >= 0 && row->height > 0);
16065
16066 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16067 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16068
16069 /* If first line's physical ascent is larger than its logical
16070 ascent, use the physical ascent, and make the row taller.
16071 This makes accented characters fully visible. */
16072 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16073 && row->phys_ascent > row->ascent)
16074 {
16075 row->height += row->phys_ascent - row->ascent;
16076 row->ascent = row->phys_ascent;
16077 }
16078
16079 /* Compute how much of the line is visible. */
16080 row->visible_height = row->height;
16081
16082 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16083 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16084
16085 if (row->y < min_y)
16086 row->visible_height -= min_y - row->y;
16087 if (row->y + row->height > max_y)
16088 row->visible_height -= row->y + row->height - max_y;
16089 }
16090 else
16091 {
16092 row->pixel_width = row->used[TEXT_AREA];
16093 if (row->continued_p)
16094 row->pixel_width -= it->continuation_pixel_width;
16095 else if (row->truncated_on_right_p)
16096 row->pixel_width -= it->truncation_pixel_width;
16097 row->ascent = row->phys_ascent = 0;
16098 row->height = row->phys_height = row->visible_height = 1;
16099 row->extra_line_spacing = 0;
16100 }
16101
16102 /* Compute a hash code for this row. */
16103 row->hash = 0;
16104 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16105 for (i = 0; i < row->used[area]; ++i)
16106 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16107 + row->glyphs[area][i].u.val
16108 + row->glyphs[area][i].face_id
16109 + row->glyphs[area][i].padding_p
16110 + (row->glyphs[area][i].type << 2));
16111
16112 it->max_ascent = it->max_descent = 0;
16113 it->max_phys_ascent = it->max_phys_descent = 0;
16114 }
16115
16116
16117 /* Append one space to the glyph row of iterator IT if doing a
16118 window-based redisplay. The space has the same face as
16119 IT->face_id. Value is non-zero if a space was added.
16120
16121 This function is called to make sure that there is always one glyph
16122 at the end of a glyph row that the cursor can be set on under
16123 window-systems. (If there weren't such a glyph we would not know
16124 how wide and tall a box cursor should be displayed).
16125
16126 At the same time this space let's a nicely handle clearing to the
16127 end of the line if the row ends in italic text. */
16128
16129 static int
16130 append_space_for_newline (it, default_face_p)
16131 struct it *it;
16132 int default_face_p;
16133 {
16134 if (FRAME_WINDOW_P (it->f))
16135 {
16136 int n = it->glyph_row->used[TEXT_AREA];
16137
16138 if (it->glyph_row->glyphs[TEXT_AREA] + n
16139 < it->glyph_row->glyphs[1 + TEXT_AREA])
16140 {
16141 /* Save some values that must not be changed.
16142 Must save IT->c and IT->len because otherwise
16143 ITERATOR_AT_END_P wouldn't work anymore after
16144 append_space_for_newline has been called. */
16145 enum display_element_type saved_what = it->what;
16146 int saved_c = it->c, saved_len = it->len;
16147 int saved_x = it->current_x;
16148 int saved_face_id = it->face_id;
16149 struct text_pos saved_pos;
16150 Lisp_Object saved_object;
16151 struct face *face;
16152
16153 saved_object = it->object;
16154 saved_pos = it->position;
16155
16156 it->what = IT_CHARACTER;
16157 bzero (&it->position, sizeof it->position);
16158 it->object = make_number (0);
16159 it->c = ' ';
16160 it->len = 1;
16161
16162 if (default_face_p)
16163 it->face_id = DEFAULT_FACE_ID;
16164 else if (it->face_before_selective_p)
16165 it->face_id = it->saved_face_id;
16166 face = FACE_FROM_ID (it->f, it->face_id);
16167 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16168
16169 PRODUCE_GLYPHS (it);
16170
16171 it->override_ascent = -1;
16172 it->constrain_row_ascent_descent_p = 0;
16173 it->current_x = saved_x;
16174 it->object = saved_object;
16175 it->position = saved_pos;
16176 it->what = saved_what;
16177 it->face_id = saved_face_id;
16178 it->len = saved_len;
16179 it->c = saved_c;
16180 return 1;
16181 }
16182 }
16183
16184 return 0;
16185 }
16186
16187
16188 /* Extend the face of the last glyph in the text area of IT->glyph_row
16189 to the end of the display line. Called from display_line.
16190 If the glyph row is empty, add a space glyph to it so that we
16191 know the face to draw. Set the glyph row flag fill_line_p. */
16192
16193 static void
16194 extend_face_to_end_of_line (it)
16195 struct it *it;
16196 {
16197 struct face *face;
16198 struct frame *f = it->f;
16199
16200 /* If line is already filled, do nothing. */
16201 if (it->current_x >= it->last_visible_x)
16202 return;
16203
16204 /* Face extension extends the background and box of IT->face_id
16205 to the end of the line. If the background equals the background
16206 of the frame, we don't have to do anything. */
16207 if (it->face_before_selective_p)
16208 face = FACE_FROM_ID (it->f, it->saved_face_id);
16209 else
16210 face = FACE_FROM_ID (f, it->face_id);
16211
16212 if (FRAME_WINDOW_P (f)
16213 && it->glyph_row->displays_text_p
16214 && face->box == FACE_NO_BOX
16215 && face->background == FRAME_BACKGROUND_PIXEL (f)
16216 && !face->stipple)
16217 return;
16218
16219 /* Set the glyph row flag indicating that the face of the last glyph
16220 in the text area has to be drawn to the end of the text area. */
16221 it->glyph_row->fill_line_p = 1;
16222
16223 /* If current character of IT is not ASCII, make sure we have the
16224 ASCII face. This will be automatically undone the next time
16225 get_next_display_element returns a multibyte character. Note
16226 that the character will always be single byte in unibyte text. */
16227 if (!ASCII_CHAR_P (it->c))
16228 {
16229 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16230 }
16231
16232 if (FRAME_WINDOW_P (f))
16233 {
16234 /* If the row is empty, add a space with the current face of IT,
16235 so that we know which face to draw. */
16236 if (it->glyph_row->used[TEXT_AREA] == 0)
16237 {
16238 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16239 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16240 it->glyph_row->used[TEXT_AREA] = 1;
16241 }
16242 }
16243 else
16244 {
16245 /* Save some values that must not be changed. */
16246 int saved_x = it->current_x;
16247 struct text_pos saved_pos;
16248 Lisp_Object saved_object;
16249 enum display_element_type saved_what = it->what;
16250 int saved_face_id = it->face_id;
16251
16252 saved_object = it->object;
16253 saved_pos = it->position;
16254
16255 it->what = IT_CHARACTER;
16256 bzero (&it->position, sizeof it->position);
16257 it->object = make_number (0);
16258 it->c = ' ';
16259 it->len = 1;
16260 it->face_id = face->id;
16261
16262 PRODUCE_GLYPHS (it);
16263
16264 while (it->current_x <= it->last_visible_x)
16265 PRODUCE_GLYPHS (it);
16266
16267 /* Don't count these blanks really. It would let us insert a left
16268 truncation glyph below and make us set the cursor on them, maybe. */
16269 it->current_x = saved_x;
16270 it->object = saved_object;
16271 it->position = saved_pos;
16272 it->what = saved_what;
16273 it->face_id = saved_face_id;
16274 }
16275 }
16276
16277
16278 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16279 trailing whitespace. */
16280
16281 static int
16282 trailing_whitespace_p (charpos)
16283 int charpos;
16284 {
16285 int bytepos = CHAR_TO_BYTE (charpos);
16286 int c = 0;
16287
16288 while (bytepos < ZV_BYTE
16289 && (c = FETCH_CHAR (bytepos),
16290 c == ' ' || c == '\t'))
16291 ++bytepos;
16292
16293 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16294 {
16295 if (bytepos != PT_BYTE)
16296 return 1;
16297 }
16298 return 0;
16299 }
16300
16301
16302 /* Highlight trailing whitespace, if any, in ROW. */
16303
16304 void
16305 highlight_trailing_whitespace (f, row)
16306 struct frame *f;
16307 struct glyph_row *row;
16308 {
16309 int used = row->used[TEXT_AREA];
16310
16311 if (used)
16312 {
16313 struct glyph *start = row->glyphs[TEXT_AREA];
16314 struct glyph *glyph = start + used - 1;
16315
16316 /* Skip over glyphs inserted to display the cursor at the
16317 end of a line, for extending the face of the last glyph
16318 to the end of the line on terminals, and for truncation
16319 and continuation glyphs. */
16320 while (glyph >= start
16321 && glyph->type == CHAR_GLYPH
16322 && INTEGERP (glyph->object))
16323 --glyph;
16324
16325 /* If last glyph is a space or stretch, and it's trailing
16326 whitespace, set the face of all trailing whitespace glyphs in
16327 IT->glyph_row to `trailing-whitespace'. */
16328 if (glyph >= start
16329 && BUFFERP (glyph->object)
16330 && (glyph->type == STRETCH_GLYPH
16331 || (glyph->type == CHAR_GLYPH
16332 && glyph->u.ch == ' '))
16333 && trailing_whitespace_p (glyph->charpos))
16334 {
16335 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16336 if (face_id < 0)
16337 return;
16338
16339 while (glyph >= start
16340 && BUFFERP (glyph->object)
16341 && (glyph->type == STRETCH_GLYPH
16342 || (glyph->type == CHAR_GLYPH
16343 && glyph->u.ch == ' ')))
16344 (glyph--)->face_id = face_id;
16345 }
16346 }
16347 }
16348
16349
16350 /* Value is non-zero if glyph row ROW in window W should be
16351 used to hold the cursor. */
16352
16353 static int
16354 cursor_row_p (w, row)
16355 struct window *w;
16356 struct glyph_row *row;
16357 {
16358 int cursor_row_p = 1;
16359
16360 if (PT == MATRIX_ROW_END_CHARPOS (row))
16361 {
16362 /* Suppose the row ends on a string.
16363 Unless the row is continued, that means it ends on a newline
16364 in the string. If it's anything other than a display string
16365 (e.g. a before-string from an overlay), we don't want the
16366 cursor there. (This heuristic seems to give the optimal
16367 behavior for the various types of multi-line strings.) */
16368 if (CHARPOS (row->end.string_pos) >= 0)
16369 {
16370 if (row->continued_p)
16371 cursor_row_p = 1;
16372 else
16373 {
16374 /* Check for `display' property. */
16375 struct glyph *beg = row->glyphs[TEXT_AREA];
16376 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16377 struct glyph *glyph;
16378
16379 cursor_row_p = 0;
16380 for (glyph = end; glyph >= beg; --glyph)
16381 if (STRINGP (glyph->object))
16382 {
16383 Lisp_Object prop
16384 = Fget_char_property (make_number (PT),
16385 Qdisplay, Qnil);
16386 cursor_row_p =
16387 (!NILP (prop)
16388 && display_prop_string_p (prop, glyph->object));
16389 break;
16390 }
16391 }
16392 }
16393 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16394 {
16395 /* If the row ends in middle of a real character,
16396 and the line is continued, we want the cursor here.
16397 That's because MATRIX_ROW_END_CHARPOS would equal
16398 PT if PT is before the character. */
16399 if (!row->ends_in_ellipsis_p)
16400 cursor_row_p = row->continued_p;
16401 else
16402 /* If the row ends in an ellipsis, then
16403 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16404 We want that position to be displayed after the ellipsis. */
16405 cursor_row_p = 0;
16406 }
16407 /* If the row ends at ZV, display the cursor at the end of that
16408 row instead of at the start of the row below. */
16409 else if (row->ends_at_zv_p)
16410 cursor_row_p = 1;
16411 else
16412 cursor_row_p = 0;
16413 }
16414
16415 return cursor_row_p;
16416 }
16417
16418 \f
16419
16420 /* Push the display property PROP so that it will be rendered at the
16421 current position in IT. */
16422
16423 static void
16424 push_display_prop (struct it *it, Lisp_Object prop)
16425 {
16426 push_it (it);
16427
16428 /* Never display a cursor on the prefix. */
16429 it->avoid_cursor_p = 1;
16430
16431 if (STRINGP (prop))
16432 {
16433 if (SCHARS (prop) == 0)
16434 {
16435 pop_it (it);
16436 return;
16437 }
16438
16439 it->string = prop;
16440 it->multibyte_p = STRING_MULTIBYTE (it->string);
16441 it->current.overlay_string_index = -1;
16442 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16443 it->end_charpos = it->string_nchars = SCHARS (it->string);
16444 it->method = GET_FROM_STRING;
16445 it->stop_charpos = 0;
16446 }
16447 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16448 {
16449 it->method = GET_FROM_STRETCH;
16450 it->object = prop;
16451 }
16452 #ifdef HAVE_WINDOW_SYSTEM
16453 else if (IMAGEP (prop))
16454 {
16455 it->what = IT_IMAGE;
16456 it->image_id = lookup_image (it->f, prop);
16457 it->method = GET_FROM_IMAGE;
16458 }
16459 #endif /* HAVE_WINDOW_SYSTEM */
16460 else
16461 {
16462 pop_it (it); /* bogus display property, give up */
16463 return;
16464 }
16465 }
16466
16467 /* Return the character-property PROP at the current position in IT. */
16468
16469 static Lisp_Object
16470 get_it_property (it, prop)
16471 struct it *it;
16472 Lisp_Object prop;
16473 {
16474 Lisp_Object position;
16475
16476 if (STRINGP (it->object))
16477 position = make_number (IT_STRING_CHARPOS (*it));
16478 else if (BUFFERP (it->object))
16479 position = make_number (IT_CHARPOS (*it));
16480 else
16481 return Qnil;
16482
16483 return Fget_char_property (position, prop, it->object);
16484 }
16485
16486 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16487
16488 static void
16489 handle_line_prefix (struct it *it)
16490 {
16491 Lisp_Object prefix;
16492 if (it->continuation_lines_width > 0)
16493 {
16494 prefix = get_it_property (it, Qwrap_prefix);
16495 if (NILP (prefix))
16496 prefix = Vwrap_prefix;
16497 }
16498 else
16499 {
16500 prefix = get_it_property (it, Qline_prefix);
16501 if (NILP (prefix))
16502 prefix = Vline_prefix;
16503 }
16504 if (! NILP (prefix))
16505 push_display_prop (it, prefix);
16506 }
16507
16508 \f
16509
16510 /* Construct the glyph row IT->glyph_row in the desired matrix of
16511 IT->w from text at the current position of IT. See dispextern.h
16512 for an overview of struct it. Value is non-zero if
16513 IT->glyph_row displays text, as opposed to a line displaying ZV
16514 only. */
16515
16516 static int
16517 display_line (it)
16518 struct it *it;
16519 {
16520 struct glyph_row *row = it->glyph_row;
16521 Lisp_Object overlay_arrow_string;
16522 struct it wrap_it;
16523 int may_wrap = 0, wrap_x;
16524 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16525 int wrap_row_phys_ascent, wrap_row_phys_height;
16526 int wrap_row_extra_line_spacing;
16527
16528 /* We always start displaying at hpos zero even if hscrolled. */
16529 xassert (it->hpos == 0 && it->current_x == 0);
16530
16531 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16532 >= it->w->desired_matrix->nrows)
16533 {
16534 it->w->nrows_scale_factor++;
16535 fonts_changed_p = 1;
16536 return 0;
16537 }
16538
16539 /* Is IT->w showing the region? */
16540 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16541
16542 /* Clear the result glyph row and enable it. */
16543 prepare_desired_row (row);
16544
16545 row->y = it->current_y;
16546 row->start = it->start;
16547 row->continuation_lines_width = it->continuation_lines_width;
16548 row->displays_text_p = 1;
16549 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16550 it->starts_in_middle_of_char_p = 0;
16551
16552 /* Arrange the overlays nicely for our purposes. Usually, we call
16553 display_line on only one line at a time, in which case this
16554 can't really hurt too much, or we call it on lines which appear
16555 one after another in the buffer, in which case all calls to
16556 recenter_overlay_lists but the first will be pretty cheap. */
16557 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16558
16559 /* Move over display elements that are not visible because we are
16560 hscrolled. This may stop at an x-position < IT->first_visible_x
16561 if the first glyph is partially visible or if we hit a line end. */
16562 if (it->current_x < it->first_visible_x)
16563 {
16564 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16565 MOVE_TO_POS | MOVE_TO_X);
16566 }
16567 else
16568 {
16569 /* We only do this when not calling `move_it_in_display_line_to'
16570 above, because move_it_in_display_line_to calls
16571 handle_line_prefix itself. */
16572 handle_line_prefix (it);
16573 }
16574
16575 /* Get the initial row height. This is either the height of the
16576 text hscrolled, if there is any, or zero. */
16577 row->ascent = it->max_ascent;
16578 row->height = it->max_ascent + it->max_descent;
16579 row->phys_ascent = it->max_phys_ascent;
16580 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16581 row->extra_line_spacing = it->max_extra_line_spacing;
16582
16583 /* Loop generating characters. The loop is left with IT on the next
16584 character to display. */
16585 while (1)
16586 {
16587 int n_glyphs_before, hpos_before, x_before;
16588 int x, i, nglyphs;
16589 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16590
16591 /* Retrieve the next thing to display. Value is zero if end of
16592 buffer reached. */
16593 if (!get_next_display_element (it))
16594 {
16595 /* Maybe add a space at the end of this line that is used to
16596 display the cursor there under X. Set the charpos of the
16597 first glyph of blank lines not corresponding to any text
16598 to -1. */
16599 #ifdef HAVE_WINDOW_SYSTEM
16600 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16601 row->exact_window_width_line_p = 1;
16602 else
16603 #endif /* HAVE_WINDOW_SYSTEM */
16604 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16605 || row->used[TEXT_AREA] == 0)
16606 {
16607 row->glyphs[TEXT_AREA]->charpos = -1;
16608 row->displays_text_p = 0;
16609
16610 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16611 && (!MINI_WINDOW_P (it->w)
16612 || (minibuf_level && EQ (it->window, minibuf_window))))
16613 row->indicate_empty_line_p = 1;
16614 }
16615
16616 it->continuation_lines_width = 0;
16617 row->ends_at_zv_p = 1;
16618 break;
16619 }
16620
16621 /* Now, get the metrics of what we want to display. This also
16622 generates glyphs in `row' (which is IT->glyph_row). */
16623 n_glyphs_before = row->used[TEXT_AREA];
16624 x = it->current_x;
16625
16626 /* Remember the line height so far in case the next element doesn't
16627 fit on the line. */
16628 if (it->line_wrap != TRUNCATE)
16629 {
16630 ascent = it->max_ascent;
16631 descent = it->max_descent;
16632 phys_ascent = it->max_phys_ascent;
16633 phys_descent = it->max_phys_descent;
16634
16635 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16636 {
16637 if (IT_DISPLAYING_WHITESPACE (it))
16638 may_wrap = 1;
16639 else if (may_wrap)
16640 {
16641 wrap_it = *it;
16642 wrap_x = x;
16643 wrap_row_used = row->used[TEXT_AREA];
16644 wrap_row_ascent = row->ascent;
16645 wrap_row_height = row->height;
16646 wrap_row_phys_ascent = row->phys_ascent;
16647 wrap_row_phys_height = row->phys_height;
16648 wrap_row_extra_line_spacing = row->extra_line_spacing;
16649 may_wrap = 0;
16650 }
16651 }
16652 }
16653
16654 PRODUCE_GLYPHS (it);
16655
16656 /* If this display element was in marginal areas, continue with
16657 the next one. */
16658 if (it->area != TEXT_AREA)
16659 {
16660 row->ascent = max (row->ascent, it->max_ascent);
16661 row->height = max (row->height, it->max_ascent + it->max_descent);
16662 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16663 row->phys_height = max (row->phys_height,
16664 it->max_phys_ascent + it->max_phys_descent);
16665 row->extra_line_spacing = max (row->extra_line_spacing,
16666 it->max_extra_line_spacing);
16667 set_iterator_to_next (it, 1);
16668 continue;
16669 }
16670
16671 /* Does the display element fit on the line? If we truncate
16672 lines, we should draw past the right edge of the window. If
16673 we don't truncate, we want to stop so that we can display the
16674 continuation glyph before the right margin. If lines are
16675 continued, there are two possible strategies for characters
16676 resulting in more than 1 glyph (e.g. tabs): Display as many
16677 glyphs as possible in this line and leave the rest for the
16678 continuation line, or display the whole element in the next
16679 line. Original redisplay did the former, so we do it also. */
16680 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16681 hpos_before = it->hpos;
16682 x_before = x;
16683
16684 if (/* Not a newline. */
16685 nglyphs > 0
16686 /* Glyphs produced fit entirely in the line. */
16687 && it->current_x < it->last_visible_x)
16688 {
16689 it->hpos += nglyphs;
16690 row->ascent = max (row->ascent, it->max_ascent);
16691 row->height = max (row->height, it->max_ascent + it->max_descent);
16692 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16693 row->phys_height = max (row->phys_height,
16694 it->max_phys_ascent + it->max_phys_descent);
16695 row->extra_line_spacing = max (row->extra_line_spacing,
16696 it->max_extra_line_spacing);
16697 if (it->current_x - it->pixel_width < it->first_visible_x)
16698 row->x = x - it->first_visible_x;
16699 }
16700 else
16701 {
16702 int new_x;
16703 struct glyph *glyph;
16704
16705 for (i = 0; i < nglyphs; ++i, x = new_x)
16706 {
16707 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16708 new_x = x + glyph->pixel_width;
16709
16710 if (/* Lines are continued. */
16711 it->line_wrap != TRUNCATE
16712 && (/* Glyph doesn't fit on the line. */
16713 new_x > it->last_visible_x
16714 /* Or it fits exactly on a window system frame. */
16715 || (new_x == it->last_visible_x
16716 && FRAME_WINDOW_P (it->f))))
16717 {
16718 /* End of a continued line. */
16719
16720 if (it->hpos == 0
16721 || (new_x == it->last_visible_x
16722 && FRAME_WINDOW_P (it->f)))
16723 {
16724 /* Current glyph is the only one on the line or
16725 fits exactly on the line. We must continue
16726 the line because we can't draw the cursor
16727 after the glyph. */
16728 row->continued_p = 1;
16729 it->current_x = new_x;
16730 it->continuation_lines_width += new_x;
16731 ++it->hpos;
16732 if (i == nglyphs - 1)
16733 {
16734 /* If line-wrap is on, check if a previous
16735 wrap point was found. */
16736 if (wrap_row_used > 0
16737 /* Even if there is a previous wrap
16738 point, continue the line here as
16739 usual, if (i) the previous character
16740 was a space or tab AND (ii) the
16741 current character is not. */
16742 && (!may_wrap
16743 || IT_DISPLAYING_WHITESPACE (it)))
16744 goto back_to_wrap;
16745
16746 set_iterator_to_next (it, 1);
16747 #ifdef HAVE_WINDOW_SYSTEM
16748 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16749 {
16750 if (!get_next_display_element (it))
16751 {
16752 row->exact_window_width_line_p = 1;
16753 it->continuation_lines_width = 0;
16754 row->continued_p = 0;
16755 row->ends_at_zv_p = 1;
16756 }
16757 else if (ITERATOR_AT_END_OF_LINE_P (it))
16758 {
16759 row->continued_p = 0;
16760 row->exact_window_width_line_p = 1;
16761 }
16762 }
16763 #endif /* HAVE_WINDOW_SYSTEM */
16764 }
16765 }
16766 else if (CHAR_GLYPH_PADDING_P (*glyph)
16767 && !FRAME_WINDOW_P (it->f))
16768 {
16769 /* A padding glyph that doesn't fit on this line.
16770 This means the whole character doesn't fit
16771 on the line. */
16772 row->used[TEXT_AREA] = n_glyphs_before;
16773
16774 /* Fill the rest of the row with continuation
16775 glyphs like in 20.x. */
16776 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16777 < row->glyphs[1 + TEXT_AREA])
16778 produce_special_glyphs (it, IT_CONTINUATION);
16779
16780 row->continued_p = 1;
16781 it->current_x = x_before;
16782 it->continuation_lines_width += x_before;
16783
16784 /* Restore the height to what it was before the
16785 element not fitting on the line. */
16786 it->max_ascent = ascent;
16787 it->max_descent = descent;
16788 it->max_phys_ascent = phys_ascent;
16789 it->max_phys_descent = phys_descent;
16790 }
16791 else if (wrap_row_used > 0)
16792 {
16793 back_to_wrap:
16794 *it = wrap_it;
16795 it->continuation_lines_width += wrap_x;
16796 row->used[TEXT_AREA] = wrap_row_used;
16797 row->ascent = wrap_row_ascent;
16798 row->height = wrap_row_height;
16799 row->phys_ascent = wrap_row_phys_ascent;
16800 row->phys_height = wrap_row_phys_height;
16801 row->extra_line_spacing = wrap_row_extra_line_spacing;
16802 row->continued_p = 1;
16803 row->ends_at_zv_p = 0;
16804 row->exact_window_width_line_p = 0;
16805 it->continuation_lines_width += x;
16806
16807 /* Make sure that a non-default face is extended
16808 up to the right margin of the window. */
16809 extend_face_to_end_of_line (it);
16810 }
16811 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16812 {
16813 /* A TAB that extends past the right edge of the
16814 window. This produces a single glyph on
16815 window system frames. We leave the glyph in
16816 this row and let it fill the row, but don't
16817 consume the TAB. */
16818 it->continuation_lines_width += it->last_visible_x;
16819 row->ends_in_middle_of_char_p = 1;
16820 row->continued_p = 1;
16821 glyph->pixel_width = it->last_visible_x - x;
16822 it->starts_in_middle_of_char_p = 1;
16823 }
16824 else
16825 {
16826 /* Something other than a TAB that draws past
16827 the right edge of the window. Restore
16828 positions to values before the element. */
16829 row->used[TEXT_AREA] = n_glyphs_before + i;
16830
16831 /* Display continuation glyphs. */
16832 if (!FRAME_WINDOW_P (it->f))
16833 produce_special_glyphs (it, IT_CONTINUATION);
16834 row->continued_p = 1;
16835
16836 it->current_x = x_before;
16837 it->continuation_lines_width += x;
16838 extend_face_to_end_of_line (it);
16839
16840 if (nglyphs > 1 && i > 0)
16841 {
16842 row->ends_in_middle_of_char_p = 1;
16843 it->starts_in_middle_of_char_p = 1;
16844 }
16845
16846 /* Restore the height to what it was before the
16847 element not fitting on the line. */
16848 it->max_ascent = ascent;
16849 it->max_descent = descent;
16850 it->max_phys_ascent = phys_ascent;
16851 it->max_phys_descent = phys_descent;
16852 }
16853
16854 break;
16855 }
16856 else if (new_x > it->first_visible_x)
16857 {
16858 /* Increment number of glyphs actually displayed. */
16859 ++it->hpos;
16860
16861 if (x < it->first_visible_x)
16862 /* Glyph is partially visible, i.e. row starts at
16863 negative X position. */
16864 row->x = x - it->first_visible_x;
16865 }
16866 else
16867 {
16868 /* Glyph is completely off the left margin of the
16869 window. This should not happen because of the
16870 move_it_in_display_line at the start of this
16871 function, unless the text display area of the
16872 window is empty. */
16873 xassert (it->first_visible_x <= it->last_visible_x);
16874 }
16875 }
16876
16877 row->ascent = max (row->ascent, it->max_ascent);
16878 row->height = max (row->height, it->max_ascent + it->max_descent);
16879 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16880 row->phys_height = max (row->phys_height,
16881 it->max_phys_ascent + it->max_phys_descent);
16882 row->extra_line_spacing = max (row->extra_line_spacing,
16883 it->max_extra_line_spacing);
16884
16885 /* End of this display line if row is continued. */
16886 if (row->continued_p || row->ends_at_zv_p)
16887 break;
16888 }
16889
16890 at_end_of_line:
16891 /* Is this a line end? If yes, we're also done, after making
16892 sure that a non-default face is extended up to the right
16893 margin of the window. */
16894 if (ITERATOR_AT_END_OF_LINE_P (it))
16895 {
16896 int used_before = row->used[TEXT_AREA];
16897
16898 row->ends_in_newline_from_string_p = STRINGP (it->object);
16899
16900 #ifdef HAVE_WINDOW_SYSTEM
16901 /* Add a space at the end of the line that is used to
16902 display the cursor there. */
16903 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16904 append_space_for_newline (it, 0);
16905 #endif /* HAVE_WINDOW_SYSTEM */
16906
16907 /* Extend the face to the end of the line. */
16908 extend_face_to_end_of_line (it);
16909
16910 /* Make sure we have the position. */
16911 if (used_before == 0)
16912 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16913
16914 /* Consume the line end. This skips over invisible lines. */
16915 set_iterator_to_next (it, 1);
16916 it->continuation_lines_width = 0;
16917 break;
16918 }
16919
16920 /* Proceed with next display element. Note that this skips
16921 over lines invisible because of selective display. */
16922 set_iterator_to_next (it, 1);
16923
16924 /* If we truncate lines, we are done when the last displayed
16925 glyphs reach past the right margin of the window. */
16926 if (it->line_wrap == TRUNCATE
16927 && (FRAME_WINDOW_P (it->f)
16928 ? (it->current_x >= it->last_visible_x)
16929 : (it->current_x > it->last_visible_x)))
16930 {
16931 /* Maybe add truncation glyphs. */
16932 if (!FRAME_WINDOW_P (it->f))
16933 {
16934 int i, n;
16935
16936 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16937 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16938 break;
16939
16940 for (n = row->used[TEXT_AREA]; i < n; ++i)
16941 {
16942 row->used[TEXT_AREA] = i;
16943 produce_special_glyphs (it, IT_TRUNCATION);
16944 }
16945 }
16946 #ifdef HAVE_WINDOW_SYSTEM
16947 else
16948 {
16949 /* Don't truncate if we can overflow newline into fringe. */
16950 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16951 {
16952 if (!get_next_display_element (it))
16953 {
16954 it->continuation_lines_width = 0;
16955 row->ends_at_zv_p = 1;
16956 row->exact_window_width_line_p = 1;
16957 break;
16958 }
16959 if (ITERATOR_AT_END_OF_LINE_P (it))
16960 {
16961 row->exact_window_width_line_p = 1;
16962 goto at_end_of_line;
16963 }
16964 }
16965 }
16966 #endif /* HAVE_WINDOW_SYSTEM */
16967
16968 row->truncated_on_right_p = 1;
16969 it->continuation_lines_width = 0;
16970 reseat_at_next_visible_line_start (it, 0);
16971 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16972 it->hpos = hpos_before;
16973 it->current_x = x_before;
16974 break;
16975 }
16976 }
16977
16978 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16979 at the left window margin. */
16980 if (it->first_visible_x
16981 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16982 {
16983 if (!FRAME_WINDOW_P (it->f))
16984 insert_left_trunc_glyphs (it);
16985 row->truncated_on_left_p = 1;
16986 }
16987
16988 /* If the start of this line is the overlay arrow-position, then
16989 mark this glyph row as the one containing the overlay arrow.
16990 This is clearly a mess with variable size fonts. It would be
16991 better to let it be displayed like cursors under X. */
16992 if ((row->displays_text_p || !overlay_arrow_seen)
16993 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16994 !NILP (overlay_arrow_string)))
16995 {
16996 /* Overlay arrow in window redisplay is a fringe bitmap. */
16997 if (STRINGP (overlay_arrow_string))
16998 {
16999 struct glyph_row *arrow_row
17000 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17001 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17002 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17003 struct glyph *p = row->glyphs[TEXT_AREA];
17004 struct glyph *p2, *end;
17005
17006 /* Copy the arrow glyphs. */
17007 while (glyph < arrow_end)
17008 *p++ = *glyph++;
17009
17010 /* Throw away padding glyphs. */
17011 p2 = p;
17012 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17013 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17014 ++p2;
17015 if (p2 > p)
17016 {
17017 while (p2 < end)
17018 *p++ = *p2++;
17019 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17020 }
17021 }
17022 else
17023 {
17024 xassert (INTEGERP (overlay_arrow_string));
17025 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17026 }
17027 overlay_arrow_seen = 1;
17028 }
17029
17030 /* Compute pixel dimensions of this line. */
17031 compute_line_metrics (it);
17032
17033 /* Remember the position at which this line ends. */
17034 row->end = it->current;
17035
17036 /* Record whether this row ends inside an ellipsis. */
17037 row->ends_in_ellipsis_p
17038 = (it->method == GET_FROM_DISPLAY_VECTOR
17039 && it->ellipsis_p);
17040
17041 /* Save fringe bitmaps in this row. */
17042 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17043 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17044 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17045 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17046
17047 it->left_user_fringe_bitmap = 0;
17048 it->left_user_fringe_face_id = 0;
17049 it->right_user_fringe_bitmap = 0;
17050 it->right_user_fringe_face_id = 0;
17051
17052 /* Maybe set the cursor. */
17053 if (it->w->cursor.vpos < 0
17054 && PT >= MATRIX_ROW_START_CHARPOS (row)
17055 && PT <= MATRIX_ROW_END_CHARPOS (row)
17056 && cursor_row_p (it->w, row))
17057 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17058
17059 /* Highlight trailing whitespace. */
17060 if (!NILP (Vshow_trailing_whitespace))
17061 highlight_trailing_whitespace (it->f, it->glyph_row);
17062
17063 /* Prepare for the next line. This line starts horizontally at (X
17064 HPOS) = (0 0). Vertical positions are incremented. As a
17065 convenience for the caller, IT->glyph_row is set to the next
17066 row to be used. */
17067 it->current_x = it->hpos = 0;
17068 it->current_y += row->height;
17069 ++it->vpos;
17070 ++it->glyph_row;
17071 it->start = it->current;
17072 return row->displays_text_p;
17073 }
17074
17075
17076 \f
17077 /***********************************************************************
17078 Menu Bar
17079 ***********************************************************************/
17080
17081 /* Redisplay the menu bar in the frame for window W.
17082
17083 The menu bar of X frames that don't have X toolkit support is
17084 displayed in a special window W->frame->menu_bar_window.
17085
17086 The menu bar of terminal frames is treated specially as far as
17087 glyph matrices are concerned. Menu bar lines are not part of
17088 windows, so the update is done directly on the frame matrix rows
17089 for the menu bar. */
17090
17091 static void
17092 display_menu_bar (w)
17093 struct window *w;
17094 {
17095 struct frame *f = XFRAME (WINDOW_FRAME (w));
17096 struct it it;
17097 Lisp_Object items;
17098 int i;
17099
17100 /* Don't do all this for graphical frames. */
17101 #ifdef HAVE_NTGUI
17102 if (FRAME_W32_P (f))
17103 return;
17104 #endif
17105 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17106 if (FRAME_X_P (f))
17107 return;
17108 #endif
17109
17110 #ifdef HAVE_NS
17111 if (FRAME_NS_P (f))
17112 return;
17113 #endif /* HAVE_NS */
17114
17115 #ifdef USE_X_TOOLKIT
17116 xassert (!FRAME_WINDOW_P (f));
17117 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17118 it.first_visible_x = 0;
17119 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17120 #else /* not USE_X_TOOLKIT */
17121 if (FRAME_WINDOW_P (f))
17122 {
17123 /* Menu bar lines are displayed in the desired matrix of the
17124 dummy window menu_bar_window. */
17125 struct window *menu_w;
17126 xassert (WINDOWP (f->menu_bar_window));
17127 menu_w = XWINDOW (f->menu_bar_window);
17128 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17129 MENU_FACE_ID);
17130 it.first_visible_x = 0;
17131 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17132 }
17133 else
17134 {
17135 /* This is a TTY frame, i.e. character hpos/vpos are used as
17136 pixel x/y. */
17137 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17138 MENU_FACE_ID);
17139 it.first_visible_x = 0;
17140 it.last_visible_x = FRAME_COLS (f);
17141 }
17142 #endif /* not USE_X_TOOLKIT */
17143
17144 if (! mode_line_inverse_video)
17145 /* Force the menu-bar to be displayed in the default face. */
17146 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17147
17148 /* Clear all rows of the menu bar. */
17149 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17150 {
17151 struct glyph_row *row = it.glyph_row + i;
17152 clear_glyph_row (row);
17153 row->enabled_p = 1;
17154 row->full_width_p = 1;
17155 }
17156
17157 /* Display all items of the menu bar. */
17158 items = FRAME_MENU_BAR_ITEMS (it.f);
17159 for (i = 0; i < XVECTOR (items)->size; i += 4)
17160 {
17161 Lisp_Object string;
17162
17163 /* Stop at nil string. */
17164 string = AREF (items, i + 1);
17165 if (NILP (string))
17166 break;
17167
17168 /* Remember where item was displayed. */
17169 ASET (items, i + 3, make_number (it.hpos));
17170
17171 /* Display the item, pad with one space. */
17172 if (it.current_x < it.last_visible_x)
17173 display_string (NULL, string, Qnil, 0, 0, &it,
17174 SCHARS (string) + 1, 0, 0, -1);
17175 }
17176
17177 /* Fill out the line with spaces. */
17178 if (it.current_x < it.last_visible_x)
17179 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17180
17181 /* Compute the total height of the lines. */
17182 compute_line_metrics (&it);
17183 }
17184
17185
17186 \f
17187 /***********************************************************************
17188 Mode Line
17189 ***********************************************************************/
17190
17191 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17192 FORCE is non-zero, redisplay mode lines unconditionally.
17193 Otherwise, redisplay only mode lines that are garbaged. Value is
17194 the number of windows whose mode lines were redisplayed. */
17195
17196 static int
17197 redisplay_mode_lines (window, force)
17198 Lisp_Object window;
17199 int force;
17200 {
17201 int nwindows = 0;
17202
17203 while (!NILP (window))
17204 {
17205 struct window *w = XWINDOW (window);
17206
17207 if (WINDOWP (w->hchild))
17208 nwindows += redisplay_mode_lines (w->hchild, force);
17209 else if (WINDOWP (w->vchild))
17210 nwindows += redisplay_mode_lines (w->vchild, force);
17211 else if (force
17212 || FRAME_GARBAGED_P (XFRAME (w->frame))
17213 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17214 {
17215 struct text_pos lpoint;
17216 struct buffer *old = current_buffer;
17217
17218 /* Set the window's buffer for the mode line display. */
17219 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17220 set_buffer_internal_1 (XBUFFER (w->buffer));
17221
17222 /* Point refers normally to the selected window. For any
17223 other window, set up appropriate value. */
17224 if (!EQ (window, selected_window))
17225 {
17226 struct text_pos pt;
17227
17228 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17229 if (CHARPOS (pt) < BEGV)
17230 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17231 else if (CHARPOS (pt) > (ZV - 1))
17232 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17233 else
17234 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17235 }
17236
17237 /* Display mode lines. */
17238 clear_glyph_matrix (w->desired_matrix);
17239 if (display_mode_lines (w))
17240 {
17241 ++nwindows;
17242 w->must_be_updated_p = 1;
17243 }
17244
17245 /* Restore old settings. */
17246 set_buffer_internal_1 (old);
17247 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17248 }
17249
17250 window = w->next;
17251 }
17252
17253 return nwindows;
17254 }
17255
17256
17257 /* Display the mode and/or top line of window W. Value is the number
17258 of mode lines displayed. */
17259
17260 static int
17261 display_mode_lines (w)
17262 struct window *w;
17263 {
17264 Lisp_Object old_selected_window, old_selected_frame;
17265 int n = 0;
17266
17267 old_selected_frame = selected_frame;
17268 selected_frame = w->frame;
17269 old_selected_window = selected_window;
17270 XSETWINDOW (selected_window, w);
17271
17272 /* These will be set while the mode line specs are processed. */
17273 line_number_displayed = 0;
17274 w->column_number_displayed = Qnil;
17275
17276 if (WINDOW_WANTS_MODELINE_P (w))
17277 {
17278 struct window *sel_w = XWINDOW (old_selected_window);
17279
17280 /* Select mode line face based on the real selected window. */
17281 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17282 current_buffer->mode_line_format);
17283 ++n;
17284 }
17285
17286 if (WINDOW_WANTS_HEADER_LINE_P (w))
17287 {
17288 display_mode_line (w, HEADER_LINE_FACE_ID,
17289 current_buffer->header_line_format);
17290 ++n;
17291 }
17292
17293 selected_frame = old_selected_frame;
17294 selected_window = old_selected_window;
17295 return n;
17296 }
17297
17298
17299 /* Display mode or top line of window W. FACE_ID specifies which line
17300 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17301 FORMAT is the mode line format to display. Value is the pixel
17302 height of the mode line displayed. */
17303
17304 static int
17305 display_mode_line (w, face_id, format)
17306 struct window *w;
17307 enum face_id face_id;
17308 Lisp_Object format;
17309 {
17310 struct it it;
17311 struct face *face;
17312 int count = SPECPDL_INDEX ();
17313
17314 init_iterator (&it, w, -1, -1, NULL, face_id);
17315 /* Don't extend on a previously drawn mode-line.
17316 This may happen if called from pos_visible_p. */
17317 it.glyph_row->enabled_p = 0;
17318 prepare_desired_row (it.glyph_row);
17319
17320 it.glyph_row->mode_line_p = 1;
17321
17322 if (! mode_line_inverse_video)
17323 /* Force the mode-line to be displayed in the default face. */
17324 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17325
17326 record_unwind_protect (unwind_format_mode_line,
17327 format_mode_line_unwind_data (NULL, Qnil, 0));
17328
17329 mode_line_target = MODE_LINE_DISPLAY;
17330
17331 /* Temporarily make frame's keyboard the current kboard so that
17332 kboard-local variables in the mode_line_format will get the right
17333 values. */
17334 push_kboard (FRAME_KBOARD (it.f));
17335 record_unwind_save_match_data ();
17336 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17337 pop_kboard ();
17338
17339 unbind_to (count, Qnil);
17340
17341 /* Fill up with spaces. */
17342 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17343
17344 compute_line_metrics (&it);
17345 it.glyph_row->full_width_p = 1;
17346 it.glyph_row->continued_p = 0;
17347 it.glyph_row->truncated_on_left_p = 0;
17348 it.glyph_row->truncated_on_right_p = 0;
17349
17350 /* Make a 3D mode-line have a shadow at its right end. */
17351 face = FACE_FROM_ID (it.f, face_id);
17352 extend_face_to_end_of_line (&it);
17353 if (face->box != FACE_NO_BOX)
17354 {
17355 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17356 + it.glyph_row->used[TEXT_AREA] - 1);
17357 last->right_box_line_p = 1;
17358 }
17359
17360 return it.glyph_row->height;
17361 }
17362
17363 /* Move element ELT in LIST to the front of LIST.
17364 Return the updated list. */
17365
17366 static Lisp_Object
17367 move_elt_to_front (elt, list)
17368 Lisp_Object elt, list;
17369 {
17370 register Lisp_Object tail, prev;
17371 register Lisp_Object tem;
17372
17373 tail = list;
17374 prev = Qnil;
17375 while (CONSP (tail))
17376 {
17377 tem = XCAR (tail);
17378
17379 if (EQ (elt, tem))
17380 {
17381 /* Splice out the link TAIL. */
17382 if (NILP (prev))
17383 list = XCDR (tail);
17384 else
17385 Fsetcdr (prev, XCDR (tail));
17386
17387 /* Now make it the first. */
17388 Fsetcdr (tail, list);
17389 return tail;
17390 }
17391 else
17392 prev = tail;
17393 tail = XCDR (tail);
17394 QUIT;
17395 }
17396
17397 /* Not found--return unchanged LIST. */
17398 return list;
17399 }
17400
17401 /* Contribute ELT to the mode line for window IT->w. How it
17402 translates into text depends on its data type.
17403
17404 IT describes the display environment in which we display, as usual.
17405
17406 DEPTH is the depth in recursion. It is used to prevent
17407 infinite recursion here.
17408
17409 FIELD_WIDTH is the number of characters the display of ELT should
17410 occupy in the mode line, and PRECISION is the maximum number of
17411 characters to display from ELT's representation. See
17412 display_string for details.
17413
17414 Returns the hpos of the end of the text generated by ELT.
17415
17416 PROPS is a property list to add to any string we encounter.
17417
17418 If RISKY is nonzero, remove (disregard) any properties in any string
17419 we encounter, and ignore :eval and :propertize.
17420
17421 The global variable `mode_line_target' determines whether the
17422 output is passed to `store_mode_line_noprop',
17423 `store_mode_line_string', or `display_string'. */
17424
17425 static int
17426 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17427 struct it *it;
17428 int depth;
17429 int field_width, precision;
17430 Lisp_Object elt, props;
17431 int risky;
17432 {
17433 int n = 0, field, prec;
17434 int literal = 0;
17435
17436 tail_recurse:
17437 if (depth > 100)
17438 elt = build_string ("*too-deep*");
17439
17440 depth++;
17441
17442 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17443 {
17444 case Lisp_String:
17445 {
17446 /* A string: output it and check for %-constructs within it. */
17447 unsigned char c;
17448 int offset = 0;
17449
17450 if (SCHARS (elt) > 0
17451 && (!NILP (props) || risky))
17452 {
17453 Lisp_Object oprops, aelt;
17454 oprops = Ftext_properties_at (make_number (0), elt);
17455
17456 /* If the starting string's properties are not what
17457 we want, translate the string. Also, if the string
17458 is risky, do that anyway. */
17459
17460 if (NILP (Fequal (props, oprops)) || risky)
17461 {
17462 /* If the starting string has properties,
17463 merge the specified ones onto the existing ones. */
17464 if (! NILP (oprops) && !risky)
17465 {
17466 Lisp_Object tem;
17467
17468 oprops = Fcopy_sequence (oprops);
17469 tem = props;
17470 while (CONSP (tem))
17471 {
17472 oprops = Fplist_put (oprops, XCAR (tem),
17473 XCAR (XCDR (tem)));
17474 tem = XCDR (XCDR (tem));
17475 }
17476 props = oprops;
17477 }
17478
17479 aelt = Fassoc (elt, mode_line_proptrans_alist);
17480 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17481 {
17482 /* AELT is what we want. Move it to the front
17483 without consing. */
17484 elt = XCAR (aelt);
17485 mode_line_proptrans_alist
17486 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17487 }
17488 else
17489 {
17490 Lisp_Object tem;
17491
17492 /* If AELT has the wrong props, it is useless.
17493 so get rid of it. */
17494 if (! NILP (aelt))
17495 mode_line_proptrans_alist
17496 = Fdelq (aelt, mode_line_proptrans_alist);
17497
17498 elt = Fcopy_sequence (elt);
17499 Fset_text_properties (make_number (0), Flength (elt),
17500 props, elt);
17501 /* Add this item to mode_line_proptrans_alist. */
17502 mode_line_proptrans_alist
17503 = Fcons (Fcons (elt, props),
17504 mode_line_proptrans_alist);
17505 /* Truncate mode_line_proptrans_alist
17506 to at most 50 elements. */
17507 tem = Fnthcdr (make_number (50),
17508 mode_line_proptrans_alist);
17509 if (! NILP (tem))
17510 XSETCDR (tem, Qnil);
17511 }
17512 }
17513 }
17514
17515 offset = 0;
17516
17517 if (literal)
17518 {
17519 prec = precision - n;
17520 switch (mode_line_target)
17521 {
17522 case MODE_LINE_NOPROP:
17523 case MODE_LINE_TITLE:
17524 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17525 break;
17526 case MODE_LINE_STRING:
17527 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17528 break;
17529 case MODE_LINE_DISPLAY:
17530 n += display_string (NULL, elt, Qnil, 0, 0, it,
17531 0, prec, 0, STRING_MULTIBYTE (elt));
17532 break;
17533 }
17534
17535 break;
17536 }
17537
17538 /* Handle the non-literal case. */
17539
17540 while ((precision <= 0 || n < precision)
17541 && SREF (elt, offset) != 0
17542 && (mode_line_target != MODE_LINE_DISPLAY
17543 || it->current_x < it->last_visible_x))
17544 {
17545 int last_offset = offset;
17546
17547 /* Advance to end of string or next format specifier. */
17548 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17549 ;
17550
17551 if (offset - 1 != last_offset)
17552 {
17553 int nchars, nbytes;
17554
17555 /* Output to end of string or up to '%'. Field width
17556 is length of string. Don't output more than
17557 PRECISION allows us. */
17558 offset--;
17559
17560 prec = c_string_width (SDATA (elt) + last_offset,
17561 offset - last_offset, precision - n,
17562 &nchars, &nbytes);
17563
17564 switch (mode_line_target)
17565 {
17566 case MODE_LINE_NOPROP:
17567 case MODE_LINE_TITLE:
17568 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17569 break;
17570 case MODE_LINE_STRING:
17571 {
17572 int bytepos = last_offset;
17573 int charpos = string_byte_to_char (elt, bytepos);
17574 int endpos = (precision <= 0
17575 ? string_byte_to_char (elt, offset)
17576 : charpos + nchars);
17577
17578 n += store_mode_line_string (NULL,
17579 Fsubstring (elt, make_number (charpos),
17580 make_number (endpos)),
17581 0, 0, 0, Qnil);
17582 }
17583 break;
17584 case MODE_LINE_DISPLAY:
17585 {
17586 int bytepos = last_offset;
17587 int charpos = string_byte_to_char (elt, bytepos);
17588
17589 if (precision <= 0)
17590 nchars = string_byte_to_char (elt, offset) - charpos;
17591 n += display_string (NULL, elt, Qnil, 0, charpos,
17592 it, 0, nchars, 0,
17593 STRING_MULTIBYTE (elt));
17594 }
17595 break;
17596 }
17597 }
17598 else /* c == '%' */
17599 {
17600 int percent_position = offset;
17601
17602 /* Get the specified minimum width. Zero means
17603 don't pad. */
17604 field = 0;
17605 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17606 field = field * 10 + c - '0';
17607
17608 /* Don't pad beyond the total padding allowed. */
17609 if (field_width - n > 0 && field > field_width - n)
17610 field = field_width - n;
17611
17612 /* Note that either PRECISION <= 0 or N < PRECISION. */
17613 prec = precision - n;
17614
17615 if (c == 'M')
17616 n += display_mode_element (it, depth, field, prec,
17617 Vglobal_mode_string, props,
17618 risky);
17619 else if (c != 0)
17620 {
17621 int multibyte;
17622 int bytepos, charpos;
17623 unsigned char *spec;
17624
17625 bytepos = percent_position;
17626 charpos = (STRING_MULTIBYTE (elt)
17627 ? string_byte_to_char (elt, bytepos)
17628 : bytepos);
17629 spec
17630 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17631
17632 switch (mode_line_target)
17633 {
17634 case MODE_LINE_NOPROP:
17635 case MODE_LINE_TITLE:
17636 n += store_mode_line_noprop (spec, field, prec);
17637 break;
17638 case MODE_LINE_STRING:
17639 {
17640 int len = strlen (spec);
17641 Lisp_Object tem = make_string (spec, len);
17642 props = Ftext_properties_at (make_number (charpos), elt);
17643 /* Should only keep face property in props */
17644 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17645 }
17646 break;
17647 case MODE_LINE_DISPLAY:
17648 {
17649 int nglyphs_before, nwritten;
17650
17651 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17652 nwritten = display_string (spec, Qnil, elt,
17653 charpos, 0, it,
17654 field, prec, 0,
17655 multibyte);
17656
17657 /* Assign to the glyphs written above the
17658 string where the `%x' came from, position
17659 of the `%'. */
17660 if (nwritten > 0)
17661 {
17662 struct glyph *glyph
17663 = (it->glyph_row->glyphs[TEXT_AREA]
17664 + nglyphs_before);
17665 int i;
17666
17667 for (i = 0; i < nwritten; ++i)
17668 {
17669 glyph[i].object = elt;
17670 glyph[i].charpos = charpos;
17671 }
17672
17673 n += nwritten;
17674 }
17675 }
17676 break;
17677 }
17678 }
17679 else /* c == 0 */
17680 break;
17681 }
17682 }
17683 }
17684 break;
17685
17686 case Lisp_Symbol:
17687 /* A symbol: process the value of the symbol recursively
17688 as if it appeared here directly. Avoid error if symbol void.
17689 Special case: if value of symbol is a string, output the string
17690 literally. */
17691 {
17692 register Lisp_Object tem;
17693
17694 /* If the variable is not marked as risky to set
17695 then its contents are risky to use. */
17696 if (NILP (Fget (elt, Qrisky_local_variable)))
17697 risky = 1;
17698
17699 tem = Fboundp (elt);
17700 if (!NILP (tem))
17701 {
17702 tem = Fsymbol_value (elt);
17703 /* If value is a string, output that string literally:
17704 don't check for % within it. */
17705 if (STRINGP (tem))
17706 literal = 1;
17707
17708 if (!EQ (tem, elt))
17709 {
17710 /* Give up right away for nil or t. */
17711 elt = tem;
17712 goto tail_recurse;
17713 }
17714 }
17715 }
17716 break;
17717
17718 case Lisp_Cons:
17719 {
17720 register Lisp_Object car, tem;
17721
17722 /* A cons cell: five distinct cases.
17723 If first element is :eval or :propertize, do something special.
17724 If first element is a string or a cons, process all the elements
17725 and effectively concatenate them.
17726 If first element is a negative number, truncate displaying cdr to
17727 at most that many characters. If positive, pad (with spaces)
17728 to at least that many characters.
17729 If first element is a symbol, process the cadr or caddr recursively
17730 according to whether the symbol's value is non-nil or nil. */
17731 car = XCAR (elt);
17732 if (EQ (car, QCeval))
17733 {
17734 /* An element of the form (:eval FORM) means evaluate FORM
17735 and use the result as mode line elements. */
17736
17737 if (risky)
17738 break;
17739
17740 if (CONSP (XCDR (elt)))
17741 {
17742 Lisp_Object spec;
17743 spec = safe_eval (XCAR (XCDR (elt)));
17744 n += display_mode_element (it, depth, field_width - n,
17745 precision - n, spec, props,
17746 risky);
17747 }
17748 }
17749 else if (EQ (car, QCpropertize))
17750 {
17751 /* An element of the form (:propertize ELT PROPS...)
17752 means display ELT but applying properties PROPS. */
17753
17754 if (risky)
17755 break;
17756
17757 if (CONSP (XCDR (elt)))
17758 n += display_mode_element (it, depth, field_width - n,
17759 precision - n, XCAR (XCDR (elt)),
17760 XCDR (XCDR (elt)), risky);
17761 }
17762 else if (SYMBOLP (car))
17763 {
17764 tem = Fboundp (car);
17765 elt = XCDR (elt);
17766 if (!CONSP (elt))
17767 goto invalid;
17768 /* elt is now the cdr, and we know it is a cons cell.
17769 Use its car if CAR has a non-nil value. */
17770 if (!NILP (tem))
17771 {
17772 tem = Fsymbol_value (car);
17773 if (!NILP (tem))
17774 {
17775 elt = XCAR (elt);
17776 goto tail_recurse;
17777 }
17778 }
17779 /* Symbol's value is nil (or symbol is unbound)
17780 Get the cddr of the original list
17781 and if possible find the caddr and use that. */
17782 elt = XCDR (elt);
17783 if (NILP (elt))
17784 break;
17785 else if (!CONSP (elt))
17786 goto invalid;
17787 elt = XCAR (elt);
17788 goto tail_recurse;
17789 }
17790 else if (INTEGERP (car))
17791 {
17792 register int lim = XINT (car);
17793 elt = XCDR (elt);
17794 if (lim < 0)
17795 {
17796 /* Negative int means reduce maximum width. */
17797 if (precision <= 0)
17798 precision = -lim;
17799 else
17800 precision = min (precision, -lim);
17801 }
17802 else if (lim > 0)
17803 {
17804 /* Padding specified. Don't let it be more than
17805 current maximum. */
17806 if (precision > 0)
17807 lim = min (precision, lim);
17808
17809 /* If that's more padding than already wanted, queue it.
17810 But don't reduce padding already specified even if
17811 that is beyond the current truncation point. */
17812 field_width = max (lim, field_width);
17813 }
17814 goto tail_recurse;
17815 }
17816 else if (STRINGP (car) || CONSP (car))
17817 {
17818 register int limit = 50;
17819 /* Limit is to protect against circular lists. */
17820 while (CONSP (elt)
17821 && --limit > 0
17822 && (precision <= 0 || n < precision))
17823 {
17824 n += display_mode_element (it, depth,
17825 /* Do padding only after the last
17826 element in the list. */
17827 (! CONSP (XCDR (elt))
17828 ? field_width - n
17829 : 0),
17830 precision - n, XCAR (elt),
17831 props, risky);
17832 elt = XCDR (elt);
17833 }
17834 }
17835 }
17836 break;
17837
17838 default:
17839 invalid:
17840 elt = build_string ("*invalid*");
17841 goto tail_recurse;
17842 }
17843
17844 /* Pad to FIELD_WIDTH. */
17845 if (field_width > 0 && n < field_width)
17846 {
17847 switch (mode_line_target)
17848 {
17849 case MODE_LINE_NOPROP:
17850 case MODE_LINE_TITLE:
17851 n += store_mode_line_noprop ("", field_width - n, 0);
17852 break;
17853 case MODE_LINE_STRING:
17854 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17855 break;
17856 case MODE_LINE_DISPLAY:
17857 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17858 0, 0, 0);
17859 break;
17860 }
17861 }
17862
17863 return n;
17864 }
17865
17866 /* Store a mode-line string element in mode_line_string_list.
17867
17868 If STRING is non-null, display that C string. Otherwise, the Lisp
17869 string LISP_STRING is displayed.
17870
17871 FIELD_WIDTH is the minimum number of output glyphs to produce.
17872 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17873 with spaces. FIELD_WIDTH <= 0 means don't pad.
17874
17875 PRECISION is the maximum number of characters to output from
17876 STRING. PRECISION <= 0 means don't truncate the string.
17877
17878 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17879 properties to the string.
17880
17881 PROPS are the properties to add to the string.
17882 The mode_line_string_face face property is always added to the string.
17883 */
17884
17885 static int
17886 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17887 char *string;
17888 Lisp_Object lisp_string;
17889 int copy_string;
17890 int field_width;
17891 int precision;
17892 Lisp_Object props;
17893 {
17894 int len;
17895 int n = 0;
17896
17897 if (string != NULL)
17898 {
17899 len = strlen (string);
17900 if (precision > 0 && len > precision)
17901 len = precision;
17902 lisp_string = make_string (string, len);
17903 if (NILP (props))
17904 props = mode_line_string_face_prop;
17905 else if (!NILP (mode_line_string_face))
17906 {
17907 Lisp_Object face = Fplist_get (props, Qface);
17908 props = Fcopy_sequence (props);
17909 if (NILP (face))
17910 face = mode_line_string_face;
17911 else
17912 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17913 props = Fplist_put (props, Qface, face);
17914 }
17915 Fadd_text_properties (make_number (0), make_number (len),
17916 props, lisp_string);
17917 }
17918 else
17919 {
17920 len = XFASTINT (Flength (lisp_string));
17921 if (precision > 0 && len > precision)
17922 {
17923 len = precision;
17924 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17925 precision = -1;
17926 }
17927 if (!NILP (mode_line_string_face))
17928 {
17929 Lisp_Object face;
17930 if (NILP (props))
17931 props = Ftext_properties_at (make_number (0), lisp_string);
17932 face = Fplist_get (props, Qface);
17933 if (NILP (face))
17934 face = mode_line_string_face;
17935 else
17936 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17937 props = Fcons (Qface, Fcons (face, Qnil));
17938 if (copy_string)
17939 lisp_string = Fcopy_sequence (lisp_string);
17940 }
17941 if (!NILP (props))
17942 Fadd_text_properties (make_number (0), make_number (len),
17943 props, lisp_string);
17944 }
17945
17946 if (len > 0)
17947 {
17948 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17949 n += len;
17950 }
17951
17952 if (field_width > len)
17953 {
17954 field_width -= len;
17955 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17956 if (!NILP (props))
17957 Fadd_text_properties (make_number (0), make_number (field_width),
17958 props, lisp_string);
17959 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17960 n += field_width;
17961 }
17962
17963 return n;
17964 }
17965
17966
17967 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17968 1, 4, 0,
17969 doc: /* Format a string out of a mode line format specification.
17970 First arg FORMAT specifies the mode line format (see `mode-line-format'
17971 for details) to use.
17972
17973 Optional second arg FACE specifies the face property to put
17974 on all characters for which no face is specified.
17975 The value t means whatever face the window's mode line currently uses
17976 \(either `mode-line' or `mode-line-inactive', depending).
17977 A value of nil means the default is no face property.
17978 If FACE is an integer, the value string has no text properties.
17979
17980 Optional third and fourth args WINDOW and BUFFER specify the window
17981 and buffer to use as the context for the formatting (defaults
17982 are the selected window and the window's buffer). */)
17983 (format, face, window, buffer)
17984 Lisp_Object format, face, window, buffer;
17985 {
17986 struct it it;
17987 int len;
17988 struct window *w;
17989 struct buffer *old_buffer = NULL;
17990 int face_id = -1;
17991 int no_props = INTEGERP (face);
17992 int count = SPECPDL_INDEX ();
17993 Lisp_Object str;
17994 int string_start = 0;
17995
17996 if (NILP (window))
17997 window = selected_window;
17998 CHECK_WINDOW (window);
17999 w = XWINDOW (window);
18000
18001 if (NILP (buffer))
18002 buffer = w->buffer;
18003 CHECK_BUFFER (buffer);
18004
18005 /* Make formatting the modeline a non-op when noninteractive, otherwise
18006 there will be problems later caused by a partially initialized frame. */
18007 if (NILP (format) || noninteractive)
18008 return empty_unibyte_string;
18009
18010 if (no_props)
18011 face = Qnil;
18012
18013 if (!NILP (face))
18014 {
18015 if (EQ (face, Qt))
18016 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18017 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18018 }
18019
18020 if (face_id < 0)
18021 face_id = DEFAULT_FACE_ID;
18022
18023 if (XBUFFER (buffer) != current_buffer)
18024 old_buffer = current_buffer;
18025
18026 /* Save things including mode_line_proptrans_alist,
18027 and set that to nil so that we don't alter the outer value. */
18028 record_unwind_protect (unwind_format_mode_line,
18029 format_mode_line_unwind_data
18030 (old_buffer, selected_window, 1));
18031 mode_line_proptrans_alist = Qnil;
18032
18033 Fselect_window (window, Qt);
18034 if (old_buffer)
18035 set_buffer_internal_1 (XBUFFER (buffer));
18036
18037 init_iterator (&it, w, -1, -1, NULL, face_id);
18038
18039 if (no_props)
18040 {
18041 mode_line_target = MODE_LINE_NOPROP;
18042 mode_line_string_face_prop = Qnil;
18043 mode_line_string_list = Qnil;
18044 string_start = MODE_LINE_NOPROP_LEN (0);
18045 }
18046 else
18047 {
18048 mode_line_target = MODE_LINE_STRING;
18049 mode_line_string_list = Qnil;
18050 mode_line_string_face = face;
18051 mode_line_string_face_prop
18052 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18053 }
18054
18055 push_kboard (FRAME_KBOARD (it.f));
18056 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18057 pop_kboard ();
18058
18059 if (no_props)
18060 {
18061 len = MODE_LINE_NOPROP_LEN (string_start);
18062 str = make_string (mode_line_noprop_buf + string_start, len);
18063 }
18064 else
18065 {
18066 mode_line_string_list = Fnreverse (mode_line_string_list);
18067 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18068 empty_unibyte_string);
18069 }
18070
18071 unbind_to (count, Qnil);
18072 return str;
18073 }
18074
18075 /* Write a null-terminated, right justified decimal representation of
18076 the positive integer D to BUF using a minimal field width WIDTH. */
18077
18078 static void
18079 pint2str (buf, width, d)
18080 register char *buf;
18081 register int width;
18082 register int d;
18083 {
18084 register char *p = buf;
18085
18086 if (d <= 0)
18087 *p++ = '0';
18088 else
18089 {
18090 while (d > 0)
18091 {
18092 *p++ = d % 10 + '0';
18093 d /= 10;
18094 }
18095 }
18096
18097 for (width -= (int) (p - buf); width > 0; --width)
18098 *p++ = ' ';
18099 *p-- = '\0';
18100 while (p > buf)
18101 {
18102 d = *buf;
18103 *buf++ = *p;
18104 *p-- = d;
18105 }
18106 }
18107
18108 /* Write a null-terminated, right justified decimal and "human
18109 readable" representation of the nonnegative integer D to BUF using
18110 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18111
18112 static const char power_letter[] =
18113 {
18114 0, /* not used */
18115 'k', /* kilo */
18116 'M', /* mega */
18117 'G', /* giga */
18118 'T', /* tera */
18119 'P', /* peta */
18120 'E', /* exa */
18121 'Z', /* zetta */
18122 'Y' /* yotta */
18123 };
18124
18125 static void
18126 pint2hrstr (buf, width, d)
18127 char *buf;
18128 int width;
18129 int d;
18130 {
18131 /* We aim to represent the nonnegative integer D as
18132 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18133 int quotient = d;
18134 int remainder = 0;
18135 /* -1 means: do not use TENTHS. */
18136 int tenths = -1;
18137 int exponent = 0;
18138
18139 /* Length of QUOTIENT.TENTHS as a string. */
18140 int length;
18141
18142 char * psuffix;
18143 char * p;
18144
18145 if (1000 <= quotient)
18146 {
18147 /* Scale to the appropriate EXPONENT. */
18148 do
18149 {
18150 remainder = quotient % 1000;
18151 quotient /= 1000;
18152 exponent++;
18153 }
18154 while (1000 <= quotient);
18155
18156 /* Round to nearest and decide whether to use TENTHS or not. */
18157 if (quotient <= 9)
18158 {
18159 tenths = remainder / 100;
18160 if (50 <= remainder % 100)
18161 {
18162 if (tenths < 9)
18163 tenths++;
18164 else
18165 {
18166 quotient++;
18167 if (quotient == 10)
18168 tenths = -1;
18169 else
18170 tenths = 0;
18171 }
18172 }
18173 }
18174 else
18175 if (500 <= remainder)
18176 {
18177 if (quotient < 999)
18178 quotient++;
18179 else
18180 {
18181 quotient = 1;
18182 exponent++;
18183 tenths = 0;
18184 }
18185 }
18186 }
18187
18188 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18189 if (tenths == -1 && quotient <= 99)
18190 if (quotient <= 9)
18191 length = 1;
18192 else
18193 length = 2;
18194 else
18195 length = 3;
18196 p = psuffix = buf + max (width, length);
18197
18198 /* Print EXPONENT. */
18199 if (exponent)
18200 *psuffix++ = power_letter[exponent];
18201 *psuffix = '\0';
18202
18203 /* Print TENTHS. */
18204 if (tenths >= 0)
18205 {
18206 *--p = '0' + tenths;
18207 *--p = '.';
18208 }
18209
18210 /* Print QUOTIENT. */
18211 do
18212 {
18213 int digit = quotient % 10;
18214 *--p = '0' + digit;
18215 }
18216 while ((quotient /= 10) != 0);
18217
18218 /* Print leading spaces. */
18219 while (buf < p)
18220 *--p = ' ';
18221 }
18222
18223 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18224 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18225 type of CODING_SYSTEM. Return updated pointer into BUF. */
18226
18227 static unsigned char invalid_eol_type[] = "(*invalid*)";
18228
18229 static char *
18230 decode_mode_spec_coding (coding_system, buf, eol_flag)
18231 Lisp_Object coding_system;
18232 register char *buf;
18233 int eol_flag;
18234 {
18235 Lisp_Object val;
18236 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18237 const unsigned char *eol_str;
18238 int eol_str_len;
18239 /* The EOL conversion we are using. */
18240 Lisp_Object eoltype;
18241
18242 val = CODING_SYSTEM_SPEC (coding_system);
18243 eoltype = Qnil;
18244
18245 if (!VECTORP (val)) /* Not yet decided. */
18246 {
18247 if (multibyte)
18248 *buf++ = '-';
18249 if (eol_flag)
18250 eoltype = eol_mnemonic_undecided;
18251 /* Don't mention EOL conversion if it isn't decided. */
18252 }
18253 else
18254 {
18255 Lisp_Object attrs;
18256 Lisp_Object eolvalue;
18257
18258 attrs = AREF (val, 0);
18259 eolvalue = AREF (val, 2);
18260
18261 if (multibyte)
18262 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18263
18264 if (eol_flag)
18265 {
18266 /* The EOL conversion that is normal on this system. */
18267
18268 if (NILP (eolvalue)) /* Not yet decided. */
18269 eoltype = eol_mnemonic_undecided;
18270 else if (VECTORP (eolvalue)) /* Not yet decided. */
18271 eoltype = eol_mnemonic_undecided;
18272 else /* eolvalue is Qunix, Qdos, or Qmac. */
18273 eoltype = (EQ (eolvalue, Qunix)
18274 ? eol_mnemonic_unix
18275 : (EQ (eolvalue, Qdos) == 1
18276 ? eol_mnemonic_dos : eol_mnemonic_mac));
18277 }
18278 }
18279
18280 if (eol_flag)
18281 {
18282 /* Mention the EOL conversion if it is not the usual one. */
18283 if (STRINGP (eoltype))
18284 {
18285 eol_str = SDATA (eoltype);
18286 eol_str_len = SBYTES (eoltype);
18287 }
18288 else if (CHARACTERP (eoltype))
18289 {
18290 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18291 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18292 eol_str = tmp;
18293 }
18294 else
18295 {
18296 eol_str = invalid_eol_type;
18297 eol_str_len = sizeof (invalid_eol_type) - 1;
18298 }
18299 bcopy (eol_str, buf, eol_str_len);
18300 buf += eol_str_len;
18301 }
18302
18303 return buf;
18304 }
18305
18306 /* Return a string for the output of a mode line %-spec for window W,
18307 generated by character C. PRECISION >= 0 means don't return a
18308 string longer than that value. FIELD_WIDTH > 0 means pad the
18309 string returned with spaces to that value. Return 1 in *MULTIBYTE
18310 if the result is multibyte text.
18311
18312 Note we operate on the current buffer for most purposes,
18313 the exception being w->base_line_pos. */
18314
18315 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18316
18317 static char *
18318 decode_mode_spec (w, c, field_width, precision, multibyte)
18319 struct window *w;
18320 register int c;
18321 int field_width, precision;
18322 int *multibyte;
18323 {
18324 Lisp_Object obj;
18325 struct frame *f = XFRAME (WINDOW_FRAME (w));
18326 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18327 struct buffer *b = current_buffer;
18328
18329 obj = Qnil;
18330 *multibyte = 0;
18331
18332 switch (c)
18333 {
18334 case '*':
18335 if (!NILP (b->read_only))
18336 return "%";
18337 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18338 return "*";
18339 return "-";
18340
18341 case '+':
18342 /* This differs from %* only for a modified read-only buffer. */
18343 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18344 return "*";
18345 if (!NILP (b->read_only))
18346 return "%";
18347 return "-";
18348
18349 case '&':
18350 /* This differs from %* in ignoring read-only-ness. */
18351 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18352 return "*";
18353 return "-";
18354
18355 case '%':
18356 return "%";
18357
18358 case '[':
18359 {
18360 int i;
18361 char *p;
18362
18363 if (command_loop_level > 5)
18364 return "[[[... ";
18365 p = decode_mode_spec_buf;
18366 for (i = 0; i < command_loop_level; i++)
18367 *p++ = '[';
18368 *p = 0;
18369 return decode_mode_spec_buf;
18370 }
18371
18372 case ']':
18373 {
18374 int i;
18375 char *p;
18376
18377 if (command_loop_level > 5)
18378 return " ...]]]";
18379 p = decode_mode_spec_buf;
18380 for (i = 0; i < command_loop_level; i++)
18381 *p++ = ']';
18382 *p = 0;
18383 return decode_mode_spec_buf;
18384 }
18385
18386 case '-':
18387 {
18388 register int i;
18389
18390 /* Let lots_of_dashes be a string of infinite length. */
18391 if (mode_line_target == MODE_LINE_NOPROP ||
18392 mode_line_target == MODE_LINE_STRING)
18393 return "--";
18394 if (field_width <= 0
18395 || field_width > sizeof (lots_of_dashes))
18396 {
18397 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18398 decode_mode_spec_buf[i] = '-';
18399 decode_mode_spec_buf[i] = '\0';
18400 return decode_mode_spec_buf;
18401 }
18402 else
18403 return lots_of_dashes;
18404 }
18405
18406 case 'b':
18407 obj = b->name;
18408 break;
18409
18410 case 'c':
18411 /* %c and %l are ignored in `frame-title-format'.
18412 (In redisplay_internal, the frame title is drawn _before_ the
18413 windows are updated, so the stuff which depends on actual
18414 window contents (such as %l) may fail to render properly, or
18415 even crash emacs.) */
18416 if (mode_line_target == MODE_LINE_TITLE)
18417 return "";
18418 else
18419 {
18420 int col = (int) current_column (); /* iftc */
18421 w->column_number_displayed = make_number (col);
18422 pint2str (decode_mode_spec_buf, field_width, col);
18423 return decode_mode_spec_buf;
18424 }
18425
18426 case 'e':
18427 #ifndef SYSTEM_MALLOC
18428 {
18429 if (NILP (Vmemory_full))
18430 return "";
18431 else
18432 return "!MEM FULL! ";
18433 }
18434 #else
18435 return "";
18436 #endif
18437
18438 case 'F':
18439 /* %F displays the frame name. */
18440 if (!NILP (f->title))
18441 return (char *) SDATA (f->title);
18442 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18443 return (char *) SDATA (f->name);
18444 return "Emacs";
18445
18446 case 'f':
18447 obj = b->filename;
18448 break;
18449
18450 case 'i':
18451 {
18452 int size = ZV - BEGV;
18453 pint2str (decode_mode_spec_buf, field_width, size);
18454 return decode_mode_spec_buf;
18455 }
18456
18457 case 'I':
18458 {
18459 int size = ZV - BEGV;
18460 pint2hrstr (decode_mode_spec_buf, field_width, size);
18461 return decode_mode_spec_buf;
18462 }
18463
18464 case 'l':
18465 {
18466 int startpos, startpos_byte, line, linepos, linepos_byte;
18467 int topline, nlines, junk, height;
18468
18469 /* %c and %l are ignored in `frame-title-format'. */
18470 if (mode_line_target == MODE_LINE_TITLE)
18471 return "";
18472
18473 startpos = XMARKER (w->start)->charpos;
18474 startpos_byte = marker_byte_position (w->start);
18475 height = WINDOW_TOTAL_LINES (w);
18476
18477 /* If we decided that this buffer isn't suitable for line numbers,
18478 don't forget that too fast. */
18479 if (EQ (w->base_line_pos, w->buffer))
18480 goto no_value;
18481 /* But do forget it, if the window shows a different buffer now. */
18482 else if (BUFFERP (w->base_line_pos))
18483 w->base_line_pos = Qnil;
18484
18485 /* If the buffer is very big, don't waste time. */
18486 if (INTEGERP (Vline_number_display_limit)
18487 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18488 {
18489 w->base_line_pos = Qnil;
18490 w->base_line_number = Qnil;
18491 goto no_value;
18492 }
18493
18494 if (INTEGERP (w->base_line_number)
18495 && INTEGERP (w->base_line_pos)
18496 && XFASTINT (w->base_line_pos) <= startpos)
18497 {
18498 line = XFASTINT (w->base_line_number);
18499 linepos = XFASTINT (w->base_line_pos);
18500 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18501 }
18502 else
18503 {
18504 line = 1;
18505 linepos = BUF_BEGV (b);
18506 linepos_byte = BUF_BEGV_BYTE (b);
18507 }
18508
18509 /* Count lines from base line to window start position. */
18510 nlines = display_count_lines (linepos, linepos_byte,
18511 startpos_byte,
18512 startpos, &junk);
18513
18514 topline = nlines + line;
18515
18516 /* Determine a new base line, if the old one is too close
18517 or too far away, or if we did not have one.
18518 "Too close" means it's plausible a scroll-down would
18519 go back past it. */
18520 if (startpos == BUF_BEGV (b))
18521 {
18522 w->base_line_number = make_number (topline);
18523 w->base_line_pos = make_number (BUF_BEGV (b));
18524 }
18525 else if (nlines < height + 25 || nlines > height * 3 + 50
18526 || linepos == BUF_BEGV (b))
18527 {
18528 int limit = BUF_BEGV (b);
18529 int limit_byte = BUF_BEGV_BYTE (b);
18530 int position;
18531 int distance = (height * 2 + 30) * line_number_display_limit_width;
18532
18533 if (startpos - distance > limit)
18534 {
18535 limit = startpos - distance;
18536 limit_byte = CHAR_TO_BYTE (limit);
18537 }
18538
18539 nlines = display_count_lines (startpos, startpos_byte,
18540 limit_byte,
18541 - (height * 2 + 30),
18542 &position);
18543 /* If we couldn't find the lines we wanted within
18544 line_number_display_limit_width chars per line,
18545 give up on line numbers for this window. */
18546 if (position == limit_byte && limit == startpos - distance)
18547 {
18548 w->base_line_pos = w->buffer;
18549 w->base_line_number = Qnil;
18550 goto no_value;
18551 }
18552
18553 w->base_line_number = make_number (topline - nlines);
18554 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18555 }
18556
18557 /* Now count lines from the start pos to point. */
18558 nlines = display_count_lines (startpos, startpos_byte,
18559 PT_BYTE, PT, &junk);
18560
18561 /* Record that we did display the line number. */
18562 line_number_displayed = 1;
18563
18564 /* Make the string to show. */
18565 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18566 return decode_mode_spec_buf;
18567 no_value:
18568 {
18569 char* p = decode_mode_spec_buf;
18570 int pad = field_width - 2;
18571 while (pad-- > 0)
18572 *p++ = ' ';
18573 *p++ = '?';
18574 *p++ = '?';
18575 *p = '\0';
18576 return decode_mode_spec_buf;
18577 }
18578 }
18579 break;
18580
18581 case 'm':
18582 obj = b->mode_name;
18583 break;
18584
18585 case 'n':
18586 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18587 return " Narrow";
18588 break;
18589
18590 case 'p':
18591 {
18592 int pos = marker_position (w->start);
18593 int total = BUF_ZV (b) - BUF_BEGV (b);
18594
18595 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18596 {
18597 if (pos <= BUF_BEGV (b))
18598 return "All";
18599 else
18600 return "Bottom";
18601 }
18602 else if (pos <= BUF_BEGV (b))
18603 return "Top";
18604 else
18605 {
18606 if (total > 1000000)
18607 /* Do it differently for a large value, to avoid overflow. */
18608 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18609 else
18610 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18611 /* We can't normally display a 3-digit number,
18612 so get us a 2-digit number that is close. */
18613 if (total == 100)
18614 total = 99;
18615 sprintf (decode_mode_spec_buf, "%2d%%", total);
18616 return decode_mode_spec_buf;
18617 }
18618 }
18619
18620 /* Display percentage of size above the bottom of the screen. */
18621 case 'P':
18622 {
18623 int toppos = marker_position (w->start);
18624 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18625 int total = BUF_ZV (b) - BUF_BEGV (b);
18626
18627 if (botpos >= BUF_ZV (b))
18628 {
18629 if (toppos <= BUF_BEGV (b))
18630 return "All";
18631 else
18632 return "Bottom";
18633 }
18634 else
18635 {
18636 if (total > 1000000)
18637 /* Do it differently for a large value, to avoid overflow. */
18638 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18639 else
18640 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18641 /* We can't normally display a 3-digit number,
18642 so get us a 2-digit number that is close. */
18643 if (total == 100)
18644 total = 99;
18645 if (toppos <= BUF_BEGV (b))
18646 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18647 else
18648 sprintf (decode_mode_spec_buf, "%2d%%", total);
18649 return decode_mode_spec_buf;
18650 }
18651 }
18652
18653 case 's':
18654 /* status of process */
18655 obj = Fget_buffer_process (Fcurrent_buffer ());
18656 if (NILP (obj))
18657 return "no process";
18658 #ifdef subprocesses
18659 obj = Fsymbol_name (Fprocess_status (obj));
18660 #endif
18661 break;
18662
18663 case '@':
18664 {
18665 Lisp_Object val;
18666 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18667 if (NILP (val))
18668 return "-";
18669 else
18670 return "@";
18671 }
18672
18673 case 't': /* indicate TEXT or BINARY */
18674 #ifdef MODE_LINE_BINARY_TEXT
18675 return MODE_LINE_BINARY_TEXT (b);
18676 #else
18677 return "T";
18678 #endif
18679
18680 case 'z':
18681 /* coding-system (not including end-of-line format) */
18682 case 'Z':
18683 /* coding-system (including end-of-line type) */
18684 {
18685 int eol_flag = (c == 'Z');
18686 char *p = decode_mode_spec_buf;
18687
18688 if (! FRAME_WINDOW_P (f))
18689 {
18690 /* No need to mention EOL here--the terminal never needs
18691 to do EOL conversion. */
18692 p = decode_mode_spec_coding (CODING_ID_NAME
18693 (FRAME_KEYBOARD_CODING (f)->id),
18694 p, 0);
18695 p = decode_mode_spec_coding (CODING_ID_NAME
18696 (FRAME_TERMINAL_CODING (f)->id),
18697 p, 0);
18698 }
18699 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18700 p, eol_flag);
18701
18702 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18703 #ifdef subprocesses
18704 obj = Fget_buffer_process (Fcurrent_buffer ());
18705 if (PROCESSP (obj))
18706 {
18707 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18708 p, eol_flag);
18709 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18710 p, eol_flag);
18711 }
18712 #endif /* subprocesses */
18713 #endif /* 0 */
18714 *p = 0;
18715 return decode_mode_spec_buf;
18716 }
18717 }
18718
18719 if (STRINGP (obj))
18720 {
18721 *multibyte = STRING_MULTIBYTE (obj);
18722 return (char *) SDATA (obj);
18723 }
18724 else
18725 return "";
18726 }
18727
18728
18729 /* Count up to COUNT lines starting from START / START_BYTE.
18730 But don't go beyond LIMIT_BYTE.
18731 Return the number of lines thus found (always nonnegative).
18732
18733 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18734
18735 static int
18736 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18737 int start, start_byte, limit_byte, count;
18738 int *byte_pos_ptr;
18739 {
18740 register unsigned char *cursor;
18741 unsigned char *base;
18742
18743 register int ceiling;
18744 register unsigned char *ceiling_addr;
18745 int orig_count = count;
18746
18747 /* If we are not in selective display mode,
18748 check only for newlines. */
18749 int selective_display = (!NILP (current_buffer->selective_display)
18750 && !INTEGERP (current_buffer->selective_display));
18751
18752 if (count > 0)
18753 {
18754 while (start_byte < limit_byte)
18755 {
18756 ceiling = BUFFER_CEILING_OF (start_byte);
18757 ceiling = min (limit_byte - 1, ceiling);
18758 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18759 base = (cursor = BYTE_POS_ADDR (start_byte));
18760 while (1)
18761 {
18762 if (selective_display)
18763 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18764 ;
18765 else
18766 while (*cursor != '\n' && ++cursor != ceiling_addr)
18767 ;
18768
18769 if (cursor != ceiling_addr)
18770 {
18771 if (--count == 0)
18772 {
18773 start_byte += cursor - base + 1;
18774 *byte_pos_ptr = start_byte;
18775 return orig_count;
18776 }
18777 else
18778 if (++cursor == ceiling_addr)
18779 break;
18780 }
18781 else
18782 break;
18783 }
18784 start_byte += cursor - base;
18785 }
18786 }
18787 else
18788 {
18789 while (start_byte > limit_byte)
18790 {
18791 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18792 ceiling = max (limit_byte, ceiling);
18793 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18794 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18795 while (1)
18796 {
18797 if (selective_display)
18798 while (--cursor != ceiling_addr
18799 && *cursor != '\n' && *cursor != 015)
18800 ;
18801 else
18802 while (--cursor != ceiling_addr && *cursor != '\n')
18803 ;
18804
18805 if (cursor != ceiling_addr)
18806 {
18807 if (++count == 0)
18808 {
18809 start_byte += cursor - base + 1;
18810 *byte_pos_ptr = start_byte;
18811 /* When scanning backwards, we should
18812 not count the newline posterior to which we stop. */
18813 return - orig_count - 1;
18814 }
18815 }
18816 else
18817 break;
18818 }
18819 /* Here we add 1 to compensate for the last decrement
18820 of CURSOR, which took it past the valid range. */
18821 start_byte += cursor - base + 1;
18822 }
18823 }
18824
18825 *byte_pos_ptr = limit_byte;
18826
18827 if (count < 0)
18828 return - orig_count + count;
18829 return orig_count - count;
18830
18831 }
18832
18833
18834 \f
18835 /***********************************************************************
18836 Displaying strings
18837 ***********************************************************************/
18838
18839 /* Display a NUL-terminated string, starting with index START.
18840
18841 If STRING is non-null, display that C string. Otherwise, the Lisp
18842 string LISP_STRING is displayed.
18843
18844 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18845 FACE_STRING. Display STRING or LISP_STRING with the face at
18846 FACE_STRING_POS in FACE_STRING:
18847
18848 Display the string in the environment given by IT, but use the
18849 standard display table, temporarily.
18850
18851 FIELD_WIDTH is the minimum number of output glyphs to produce.
18852 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18853 with spaces. If STRING has more characters, more than FIELD_WIDTH
18854 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18855
18856 PRECISION is the maximum number of characters to output from
18857 STRING. PRECISION < 0 means don't truncate the string.
18858
18859 This is roughly equivalent to printf format specifiers:
18860
18861 FIELD_WIDTH PRECISION PRINTF
18862 ----------------------------------------
18863 -1 -1 %s
18864 -1 10 %.10s
18865 10 -1 %10s
18866 20 10 %20.10s
18867
18868 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18869 display them, and < 0 means obey the current buffer's value of
18870 enable_multibyte_characters.
18871
18872 Value is the number of columns displayed. */
18873
18874 static int
18875 display_string (string, lisp_string, face_string, face_string_pos,
18876 start, it, field_width, precision, max_x, multibyte)
18877 unsigned char *string;
18878 Lisp_Object lisp_string;
18879 Lisp_Object face_string;
18880 EMACS_INT face_string_pos;
18881 EMACS_INT start;
18882 struct it *it;
18883 int field_width, precision, max_x;
18884 int multibyte;
18885 {
18886 int hpos_at_start = it->hpos;
18887 int saved_face_id = it->face_id;
18888 struct glyph_row *row = it->glyph_row;
18889
18890 /* Initialize the iterator IT for iteration over STRING beginning
18891 with index START. */
18892 reseat_to_string (it, string, lisp_string, start,
18893 precision, field_width, multibyte);
18894
18895 /* If displaying STRING, set up the face of the iterator
18896 from LISP_STRING, if that's given. */
18897 if (STRINGP (face_string))
18898 {
18899 EMACS_INT endptr;
18900 struct face *face;
18901
18902 it->face_id
18903 = face_at_string_position (it->w, face_string, face_string_pos,
18904 0, it->region_beg_charpos,
18905 it->region_end_charpos,
18906 &endptr, it->base_face_id, 0);
18907 face = FACE_FROM_ID (it->f, it->face_id);
18908 it->face_box_p = face->box != FACE_NO_BOX;
18909 }
18910
18911 /* Set max_x to the maximum allowed X position. Don't let it go
18912 beyond the right edge of the window. */
18913 if (max_x <= 0)
18914 max_x = it->last_visible_x;
18915 else
18916 max_x = min (max_x, it->last_visible_x);
18917
18918 /* Skip over display elements that are not visible. because IT->w is
18919 hscrolled. */
18920 if (it->current_x < it->first_visible_x)
18921 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18922 MOVE_TO_POS | MOVE_TO_X);
18923
18924 row->ascent = it->max_ascent;
18925 row->height = it->max_ascent + it->max_descent;
18926 row->phys_ascent = it->max_phys_ascent;
18927 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18928 row->extra_line_spacing = it->max_extra_line_spacing;
18929
18930 /* This condition is for the case that we are called with current_x
18931 past last_visible_x. */
18932 while (it->current_x < max_x)
18933 {
18934 int x_before, x, n_glyphs_before, i, nglyphs;
18935
18936 /* Get the next display element. */
18937 if (!get_next_display_element (it))
18938 break;
18939
18940 /* Produce glyphs. */
18941 x_before = it->current_x;
18942 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18943 PRODUCE_GLYPHS (it);
18944
18945 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18946 i = 0;
18947 x = x_before;
18948 while (i < nglyphs)
18949 {
18950 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18951
18952 if (it->line_wrap != TRUNCATE
18953 && x + glyph->pixel_width > max_x)
18954 {
18955 /* End of continued line or max_x reached. */
18956 if (CHAR_GLYPH_PADDING_P (*glyph))
18957 {
18958 /* A wide character is unbreakable. */
18959 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18960 it->current_x = x_before;
18961 }
18962 else
18963 {
18964 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18965 it->current_x = x;
18966 }
18967 break;
18968 }
18969 else if (x + glyph->pixel_width >= it->first_visible_x)
18970 {
18971 /* Glyph is at least partially visible. */
18972 ++it->hpos;
18973 if (x < it->first_visible_x)
18974 it->glyph_row->x = x - it->first_visible_x;
18975 }
18976 else
18977 {
18978 /* Glyph is off the left margin of the display area.
18979 Should not happen. */
18980 abort ();
18981 }
18982
18983 row->ascent = max (row->ascent, it->max_ascent);
18984 row->height = max (row->height, it->max_ascent + it->max_descent);
18985 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18986 row->phys_height = max (row->phys_height,
18987 it->max_phys_ascent + it->max_phys_descent);
18988 row->extra_line_spacing = max (row->extra_line_spacing,
18989 it->max_extra_line_spacing);
18990 x += glyph->pixel_width;
18991 ++i;
18992 }
18993
18994 /* Stop if max_x reached. */
18995 if (i < nglyphs)
18996 break;
18997
18998 /* Stop at line ends. */
18999 if (ITERATOR_AT_END_OF_LINE_P (it))
19000 {
19001 it->continuation_lines_width = 0;
19002 break;
19003 }
19004
19005 set_iterator_to_next (it, 1);
19006
19007 /* Stop if truncating at the right edge. */
19008 if (it->line_wrap == TRUNCATE
19009 && it->current_x >= it->last_visible_x)
19010 {
19011 /* Add truncation mark, but don't do it if the line is
19012 truncated at a padding space. */
19013 if (IT_CHARPOS (*it) < it->string_nchars)
19014 {
19015 if (!FRAME_WINDOW_P (it->f))
19016 {
19017 int i, n;
19018
19019 if (it->current_x > it->last_visible_x)
19020 {
19021 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19022 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19023 break;
19024 for (n = row->used[TEXT_AREA]; i < n; ++i)
19025 {
19026 row->used[TEXT_AREA] = i;
19027 produce_special_glyphs (it, IT_TRUNCATION);
19028 }
19029 }
19030 produce_special_glyphs (it, IT_TRUNCATION);
19031 }
19032 it->glyph_row->truncated_on_right_p = 1;
19033 }
19034 break;
19035 }
19036 }
19037
19038 /* Maybe insert a truncation at the left. */
19039 if (it->first_visible_x
19040 && IT_CHARPOS (*it) > 0)
19041 {
19042 if (!FRAME_WINDOW_P (it->f))
19043 insert_left_trunc_glyphs (it);
19044 it->glyph_row->truncated_on_left_p = 1;
19045 }
19046
19047 it->face_id = saved_face_id;
19048
19049 /* Value is number of columns displayed. */
19050 return it->hpos - hpos_at_start;
19051 }
19052
19053
19054 \f
19055 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19056 appears as an element of LIST or as the car of an element of LIST.
19057 If PROPVAL is a list, compare each element against LIST in that
19058 way, and return 1/2 if any element of PROPVAL is found in LIST.
19059 Otherwise return 0. This function cannot quit.
19060 The return value is 2 if the text is invisible but with an ellipsis
19061 and 1 if it's invisible and without an ellipsis. */
19062
19063 int
19064 invisible_p (propval, list)
19065 register Lisp_Object propval;
19066 Lisp_Object list;
19067 {
19068 register Lisp_Object tail, proptail;
19069
19070 for (tail = list; CONSP (tail); tail = XCDR (tail))
19071 {
19072 register Lisp_Object tem;
19073 tem = XCAR (tail);
19074 if (EQ (propval, tem))
19075 return 1;
19076 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19077 return NILP (XCDR (tem)) ? 1 : 2;
19078 }
19079
19080 if (CONSP (propval))
19081 {
19082 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19083 {
19084 Lisp_Object propelt;
19085 propelt = XCAR (proptail);
19086 for (tail = list; CONSP (tail); tail = XCDR (tail))
19087 {
19088 register Lisp_Object tem;
19089 tem = XCAR (tail);
19090 if (EQ (propelt, tem))
19091 return 1;
19092 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19093 return NILP (XCDR (tem)) ? 1 : 2;
19094 }
19095 }
19096 }
19097
19098 return 0;
19099 }
19100
19101 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19102 doc: /* Non-nil if the property makes the text invisible.
19103 POS-OR-PROP can be a marker or number, in which case it is taken to be
19104 a position in the current buffer and the value of the `invisible' property
19105 is checked; or it can be some other value, which is then presumed to be the
19106 value of the `invisible' property of the text of interest.
19107 The non-nil value returned can be t for truly invisible text or something
19108 else if the text is replaced by an ellipsis. */)
19109 (pos_or_prop)
19110 Lisp_Object pos_or_prop;
19111 {
19112 Lisp_Object prop
19113 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19114 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19115 : pos_or_prop);
19116 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19117 return (invis == 0 ? Qnil
19118 : invis == 1 ? Qt
19119 : make_number (invis));
19120 }
19121
19122 /* Calculate a width or height in pixels from a specification using
19123 the following elements:
19124
19125 SPEC ::=
19126 NUM - a (fractional) multiple of the default font width/height
19127 (NUM) - specifies exactly NUM pixels
19128 UNIT - a fixed number of pixels, see below.
19129 ELEMENT - size of a display element in pixels, see below.
19130 (NUM . SPEC) - equals NUM * SPEC
19131 (+ SPEC SPEC ...) - add pixel values
19132 (- SPEC SPEC ...) - subtract pixel values
19133 (- SPEC) - negate pixel value
19134
19135 NUM ::=
19136 INT or FLOAT - a number constant
19137 SYMBOL - use symbol's (buffer local) variable binding.
19138
19139 UNIT ::=
19140 in - pixels per inch *)
19141 mm - pixels per 1/1000 meter *)
19142 cm - pixels per 1/100 meter *)
19143 width - width of current font in pixels.
19144 height - height of current font in pixels.
19145
19146 *) using the ratio(s) defined in display-pixels-per-inch.
19147
19148 ELEMENT ::=
19149
19150 left-fringe - left fringe width in pixels
19151 right-fringe - right fringe width in pixels
19152
19153 left-margin - left margin width in pixels
19154 right-margin - right margin width in pixels
19155
19156 scroll-bar - scroll-bar area width in pixels
19157
19158 Examples:
19159
19160 Pixels corresponding to 5 inches:
19161 (5 . in)
19162
19163 Total width of non-text areas on left side of window (if scroll-bar is on left):
19164 '(space :width (+ left-fringe left-margin scroll-bar))
19165
19166 Align to first text column (in header line):
19167 '(space :align-to 0)
19168
19169 Align to middle of text area minus half the width of variable `my-image'
19170 containing a loaded image:
19171 '(space :align-to (0.5 . (- text my-image)))
19172
19173 Width of left margin minus width of 1 character in the default font:
19174 '(space :width (- left-margin 1))
19175
19176 Width of left margin minus width of 2 characters in the current font:
19177 '(space :width (- left-margin (2 . width)))
19178
19179 Center 1 character over left-margin (in header line):
19180 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19181
19182 Different ways to express width of left fringe plus left margin minus one pixel:
19183 '(space :width (- (+ left-fringe left-margin) (1)))
19184 '(space :width (+ left-fringe left-margin (- (1))))
19185 '(space :width (+ left-fringe left-margin (-1)))
19186
19187 */
19188
19189 #define NUMVAL(X) \
19190 ((INTEGERP (X) || FLOATP (X)) \
19191 ? XFLOATINT (X) \
19192 : - 1)
19193
19194 int
19195 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19196 double *res;
19197 struct it *it;
19198 Lisp_Object prop;
19199 struct font *font;
19200 int width_p, *align_to;
19201 {
19202 double pixels;
19203
19204 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19205 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19206
19207 if (NILP (prop))
19208 return OK_PIXELS (0);
19209
19210 xassert (FRAME_LIVE_P (it->f));
19211
19212 if (SYMBOLP (prop))
19213 {
19214 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19215 {
19216 char *unit = SDATA (SYMBOL_NAME (prop));
19217
19218 if (unit[0] == 'i' && unit[1] == 'n')
19219 pixels = 1.0;
19220 else if (unit[0] == 'm' && unit[1] == 'm')
19221 pixels = 25.4;
19222 else if (unit[0] == 'c' && unit[1] == 'm')
19223 pixels = 2.54;
19224 else
19225 pixels = 0;
19226 if (pixels > 0)
19227 {
19228 double ppi;
19229 #ifdef HAVE_WINDOW_SYSTEM
19230 if (FRAME_WINDOW_P (it->f)
19231 && (ppi = (width_p
19232 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19233 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19234 ppi > 0))
19235 return OK_PIXELS (ppi / pixels);
19236 #endif
19237
19238 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19239 || (CONSP (Vdisplay_pixels_per_inch)
19240 && (ppi = (width_p
19241 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19242 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19243 ppi > 0)))
19244 return OK_PIXELS (ppi / pixels);
19245
19246 return 0;
19247 }
19248 }
19249
19250 #ifdef HAVE_WINDOW_SYSTEM
19251 if (EQ (prop, Qheight))
19252 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19253 if (EQ (prop, Qwidth))
19254 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19255 #else
19256 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19257 return OK_PIXELS (1);
19258 #endif
19259
19260 if (EQ (prop, Qtext))
19261 return OK_PIXELS (width_p
19262 ? window_box_width (it->w, TEXT_AREA)
19263 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19264
19265 if (align_to && *align_to < 0)
19266 {
19267 *res = 0;
19268 if (EQ (prop, Qleft))
19269 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19270 if (EQ (prop, Qright))
19271 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19272 if (EQ (prop, Qcenter))
19273 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19274 + window_box_width (it->w, TEXT_AREA) / 2);
19275 if (EQ (prop, Qleft_fringe))
19276 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19277 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19278 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19279 if (EQ (prop, Qright_fringe))
19280 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19281 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19282 : window_box_right_offset (it->w, TEXT_AREA));
19283 if (EQ (prop, Qleft_margin))
19284 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19285 if (EQ (prop, Qright_margin))
19286 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19287 if (EQ (prop, Qscroll_bar))
19288 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19289 ? 0
19290 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19291 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19292 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19293 : 0)));
19294 }
19295 else
19296 {
19297 if (EQ (prop, Qleft_fringe))
19298 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19299 if (EQ (prop, Qright_fringe))
19300 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19301 if (EQ (prop, Qleft_margin))
19302 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19303 if (EQ (prop, Qright_margin))
19304 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19305 if (EQ (prop, Qscroll_bar))
19306 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19307 }
19308
19309 prop = Fbuffer_local_value (prop, it->w->buffer);
19310 }
19311
19312 if (INTEGERP (prop) || FLOATP (prop))
19313 {
19314 int base_unit = (width_p
19315 ? FRAME_COLUMN_WIDTH (it->f)
19316 : FRAME_LINE_HEIGHT (it->f));
19317 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19318 }
19319
19320 if (CONSP (prop))
19321 {
19322 Lisp_Object car = XCAR (prop);
19323 Lisp_Object cdr = XCDR (prop);
19324
19325 if (SYMBOLP (car))
19326 {
19327 #ifdef HAVE_WINDOW_SYSTEM
19328 if (FRAME_WINDOW_P (it->f)
19329 && valid_image_p (prop))
19330 {
19331 int id = lookup_image (it->f, prop);
19332 struct image *img = IMAGE_FROM_ID (it->f, id);
19333
19334 return OK_PIXELS (width_p ? img->width : img->height);
19335 }
19336 #endif
19337 if (EQ (car, Qplus) || EQ (car, Qminus))
19338 {
19339 int first = 1;
19340 double px;
19341
19342 pixels = 0;
19343 while (CONSP (cdr))
19344 {
19345 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19346 font, width_p, align_to))
19347 return 0;
19348 if (first)
19349 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19350 else
19351 pixels += px;
19352 cdr = XCDR (cdr);
19353 }
19354 if (EQ (car, Qminus))
19355 pixels = -pixels;
19356 return OK_PIXELS (pixels);
19357 }
19358
19359 car = Fbuffer_local_value (car, it->w->buffer);
19360 }
19361
19362 if (INTEGERP (car) || FLOATP (car))
19363 {
19364 double fact;
19365 pixels = XFLOATINT (car);
19366 if (NILP (cdr))
19367 return OK_PIXELS (pixels);
19368 if (calc_pixel_width_or_height (&fact, it, cdr,
19369 font, width_p, align_to))
19370 return OK_PIXELS (pixels * fact);
19371 return 0;
19372 }
19373
19374 return 0;
19375 }
19376
19377 return 0;
19378 }
19379
19380 \f
19381 /***********************************************************************
19382 Glyph Display
19383 ***********************************************************************/
19384
19385 #ifdef HAVE_WINDOW_SYSTEM
19386
19387 #if GLYPH_DEBUG
19388
19389 void
19390 dump_glyph_string (s)
19391 struct glyph_string *s;
19392 {
19393 fprintf (stderr, "glyph string\n");
19394 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19395 s->x, s->y, s->width, s->height);
19396 fprintf (stderr, " ybase = %d\n", s->ybase);
19397 fprintf (stderr, " hl = %d\n", s->hl);
19398 fprintf (stderr, " left overhang = %d, right = %d\n",
19399 s->left_overhang, s->right_overhang);
19400 fprintf (stderr, " nchars = %d\n", s->nchars);
19401 fprintf (stderr, " extends to end of line = %d\n",
19402 s->extends_to_end_of_line_p);
19403 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19404 fprintf (stderr, " bg width = %d\n", s->background_width);
19405 }
19406
19407 #endif /* GLYPH_DEBUG */
19408
19409 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19410 of XChar2b structures for S; it can't be allocated in
19411 init_glyph_string because it must be allocated via `alloca'. W
19412 is the window on which S is drawn. ROW and AREA are the glyph row
19413 and area within the row from which S is constructed. START is the
19414 index of the first glyph structure covered by S. HL is a
19415 face-override for drawing S. */
19416
19417 #ifdef HAVE_NTGUI
19418 #define OPTIONAL_HDC(hdc) hdc,
19419 #define DECLARE_HDC(hdc) HDC hdc;
19420 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19421 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19422 #endif
19423
19424 #ifndef OPTIONAL_HDC
19425 #define OPTIONAL_HDC(hdc)
19426 #define DECLARE_HDC(hdc)
19427 #define ALLOCATE_HDC(hdc, f)
19428 #define RELEASE_HDC(hdc, f)
19429 #endif
19430
19431 static void
19432 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19433 struct glyph_string *s;
19434 DECLARE_HDC (hdc)
19435 XChar2b *char2b;
19436 struct window *w;
19437 struct glyph_row *row;
19438 enum glyph_row_area area;
19439 int start;
19440 enum draw_glyphs_face hl;
19441 {
19442 bzero (s, sizeof *s);
19443 s->w = w;
19444 s->f = XFRAME (w->frame);
19445 #ifdef HAVE_NTGUI
19446 s->hdc = hdc;
19447 #endif
19448 s->display = FRAME_X_DISPLAY (s->f);
19449 s->window = FRAME_X_WINDOW (s->f);
19450 s->char2b = char2b;
19451 s->hl = hl;
19452 s->row = row;
19453 s->area = area;
19454 s->first_glyph = row->glyphs[area] + start;
19455 s->height = row->height;
19456 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19457
19458 /* Display the internal border below the tool-bar window. */
19459 if (WINDOWP (s->f->tool_bar_window)
19460 && s->w == XWINDOW (s->f->tool_bar_window))
19461 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19462
19463 s->ybase = s->y + row->ascent;
19464 }
19465
19466
19467 /* Append the list of glyph strings with head H and tail T to the list
19468 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19469
19470 static INLINE void
19471 append_glyph_string_lists (head, tail, h, t)
19472 struct glyph_string **head, **tail;
19473 struct glyph_string *h, *t;
19474 {
19475 if (h)
19476 {
19477 if (*head)
19478 (*tail)->next = h;
19479 else
19480 *head = h;
19481 h->prev = *tail;
19482 *tail = t;
19483 }
19484 }
19485
19486
19487 /* Prepend the list of glyph strings with head H and tail T to the
19488 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19489 result. */
19490
19491 static INLINE void
19492 prepend_glyph_string_lists (head, tail, h, t)
19493 struct glyph_string **head, **tail;
19494 struct glyph_string *h, *t;
19495 {
19496 if (h)
19497 {
19498 if (*head)
19499 (*head)->prev = t;
19500 else
19501 *tail = t;
19502 t->next = *head;
19503 *head = h;
19504 }
19505 }
19506
19507
19508 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19509 Set *HEAD and *TAIL to the resulting list. */
19510
19511 static INLINE void
19512 append_glyph_string (head, tail, s)
19513 struct glyph_string **head, **tail;
19514 struct glyph_string *s;
19515 {
19516 s->next = s->prev = NULL;
19517 append_glyph_string_lists (head, tail, s, s);
19518 }
19519
19520
19521 /* Get face and two-byte form of character C in face FACE_ID on frame
19522 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19523 means we want to display multibyte text. DISPLAY_P non-zero means
19524 make sure that X resources for the face returned are allocated.
19525 Value is a pointer to a realized face that is ready for display if
19526 DISPLAY_P is non-zero. */
19527
19528 static INLINE struct face *
19529 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19530 struct frame *f;
19531 int c, face_id;
19532 XChar2b *char2b;
19533 int multibyte_p, display_p;
19534 {
19535 struct face *face = FACE_FROM_ID (f, face_id);
19536
19537 if (face->font)
19538 {
19539 unsigned code = face->font->driver->encode_char (face->font, c);
19540
19541 if (code != FONT_INVALID_CODE)
19542 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19543 else
19544 STORE_XCHAR2B (char2b, 0, 0);
19545 }
19546
19547 /* Make sure X resources of the face are allocated. */
19548 #ifdef HAVE_X_WINDOWS
19549 if (display_p)
19550 #endif
19551 {
19552 xassert (face != NULL);
19553 PREPARE_FACE_FOR_DISPLAY (f, face);
19554 }
19555
19556 return face;
19557 }
19558
19559
19560 /* Get face and two-byte form of character glyph GLYPH on frame F.
19561 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19562 a pointer to a realized face that is ready for display. */
19563
19564 static INLINE struct face *
19565 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19566 struct frame *f;
19567 struct glyph *glyph;
19568 XChar2b *char2b;
19569 int *two_byte_p;
19570 {
19571 struct face *face;
19572
19573 xassert (glyph->type == CHAR_GLYPH);
19574 face = FACE_FROM_ID (f, glyph->face_id);
19575
19576 if (two_byte_p)
19577 *two_byte_p = 0;
19578
19579 if (face->font)
19580 {
19581 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19582
19583 if (code != FONT_INVALID_CODE)
19584 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19585 else
19586 STORE_XCHAR2B (char2b, 0, 0);
19587 }
19588
19589 /* Make sure X resources of the face are allocated. */
19590 xassert (face != NULL);
19591 PREPARE_FACE_FOR_DISPLAY (f, face);
19592 return face;
19593 }
19594
19595
19596 /* Fill glyph string S with composition components specified by S->cmp.
19597
19598 BASE_FACE is the base face of the composition.
19599 S->gidx is the index of the first component for S.
19600
19601 OVERLAPS non-zero means S should draw the foreground only, and use
19602 its physical height for clipping. See also draw_glyphs.
19603
19604 Value is the index of a component not in S. */
19605
19606 static int
19607 fill_composite_glyph_string (s, base_face, overlaps)
19608 struct glyph_string *s;
19609 struct face *base_face;
19610 int overlaps;
19611 {
19612 int i;
19613
19614 xassert (s);
19615
19616 s->for_overlaps = overlaps;
19617
19618 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19619 {
19620 Lisp_Object gstring
19621 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19622 s->cmp->hash_index * 2);
19623
19624 s->face = base_face;
19625 s->font = base_face->font;
19626 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19627 {
19628 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19629 unsigned code;
19630 XChar2b * store_pos;
19631 if (NILP (g))
19632 break;
19633 code = LGLYPH_CODE (g);
19634 store_pos = s->char2b + i;
19635 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19636 }
19637 s->width = s->cmp->pixel_width;
19638 }
19639 else
19640 {
19641 /* For all glyphs of this composition, starting at the offset
19642 S->gidx, until we reach the end of the definition or encounter a
19643 glyph that requires the different face, add it to S. */
19644 struct face *face;
19645
19646 s->face = NULL;
19647 s->font = NULL;
19648 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19649 {
19650 int c = COMPOSITION_GLYPH (s->cmp, i);
19651
19652 if (c != '\t')
19653 {
19654 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19655 -1, Qnil);
19656
19657 face = get_char_face_and_encoding (s->f, c, face_id,
19658 s->char2b + i, 1, 1);
19659 if (face)
19660 {
19661 if (! s->face)
19662 {
19663 s->face = face;
19664 s->font = s->face->font;
19665 }
19666 else if (s->face != face)
19667 break;
19668 }
19669 }
19670 ++s->nchars;
19671 }
19672
19673 /* All glyph strings for the same composition has the same width,
19674 i.e. the width set for the first component of the composition. */
19675 s->width = s->first_glyph->pixel_width;
19676 }
19677
19678 /* If the specified font could not be loaded, use the frame's
19679 default font, but record the fact that we couldn't load it in
19680 the glyph string so that we can draw rectangles for the
19681 characters of the glyph string. */
19682 if (s->font == NULL)
19683 {
19684 s->font_not_found_p = 1;
19685 s->font = FRAME_FONT (s->f);
19686 }
19687
19688 /* Adjust base line for subscript/superscript text. */
19689 s->ybase += s->first_glyph->voffset;
19690
19691 /* This glyph string must always be drawn with 16-bit functions. */
19692 s->two_byte_p = 1;
19693
19694 return s->gidx + s->nchars;
19695 }
19696
19697
19698 /* Fill glyph string S from a sequence of character glyphs.
19699
19700 FACE_ID is the face id of the string. START is the index of the
19701 first glyph to consider, END is the index of the last + 1.
19702 OVERLAPS non-zero means S should draw the foreground only, and use
19703 its physical height for clipping. See also draw_glyphs.
19704
19705 Value is the index of the first glyph not in S. */
19706
19707 static int
19708 fill_glyph_string (s, face_id, start, end, overlaps)
19709 struct glyph_string *s;
19710 int face_id;
19711 int start, end, overlaps;
19712 {
19713 struct glyph *glyph, *last;
19714 int voffset;
19715 int glyph_not_available_p;
19716
19717 xassert (s->f == XFRAME (s->w->frame));
19718 xassert (s->nchars == 0);
19719 xassert (start >= 0 && end > start);
19720
19721 s->for_overlaps = overlaps,
19722 glyph = s->row->glyphs[s->area] + start;
19723 last = s->row->glyphs[s->area] + end;
19724 voffset = glyph->voffset;
19725 s->padding_p = glyph->padding_p;
19726 glyph_not_available_p = glyph->glyph_not_available_p;
19727
19728 while (glyph < last
19729 && glyph->type == CHAR_GLYPH
19730 && glyph->voffset == voffset
19731 /* Same face id implies same font, nowadays. */
19732 && glyph->face_id == face_id
19733 && glyph->glyph_not_available_p == glyph_not_available_p)
19734 {
19735 int two_byte_p;
19736
19737 s->face = get_glyph_face_and_encoding (s->f, glyph,
19738 s->char2b + s->nchars,
19739 &two_byte_p);
19740 s->two_byte_p = two_byte_p;
19741 ++s->nchars;
19742 xassert (s->nchars <= end - start);
19743 s->width += glyph->pixel_width;
19744 if (glyph++->padding_p != s->padding_p)
19745 break;
19746 }
19747
19748 s->font = s->face->font;
19749
19750 /* If the specified font could not be loaded, use the frame's font,
19751 but record the fact that we couldn't load it in
19752 S->font_not_found_p so that we can draw rectangles for the
19753 characters of the glyph string. */
19754 if (s->font == NULL || glyph_not_available_p)
19755 {
19756 s->font_not_found_p = 1;
19757 s->font = FRAME_FONT (s->f);
19758 }
19759
19760 /* Adjust base line for subscript/superscript text. */
19761 s->ybase += voffset;
19762
19763 xassert (s->face && s->face->gc);
19764 return glyph - s->row->glyphs[s->area];
19765 }
19766
19767
19768 /* Fill glyph string S from image glyph S->first_glyph. */
19769
19770 static void
19771 fill_image_glyph_string (s)
19772 struct glyph_string *s;
19773 {
19774 xassert (s->first_glyph->type == IMAGE_GLYPH);
19775 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19776 xassert (s->img);
19777 s->slice = s->first_glyph->slice;
19778 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19779 s->font = s->face->font;
19780 s->width = s->first_glyph->pixel_width;
19781
19782 /* Adjust base line for subscript/superscript text. */
19783 s->ybase += s->first_glyph->voffset;
19784 }
19785
19786
19787 /* Fill glyph string S from a sequence of stretch glyphs.
19788
19789 ROW is the glyph row in which the glyphs are found, AREA is the
19790 area within the row. START is the index of the first glyph to
19791 consider, END is the index of the last + 1.
19792
19793 Value is the index of the first glyph not in S. */
19794
19795 static int
19796 fill_stretch_glyph_string (s, row, area, start, end)
19797 struct glyph_string *s;
19798 struct glyph_row *row;
19799 enum glyph_row_area area;
19800 int start, end;
19801 {
19802 struct glyph *glyph, *last;
19803 int voffset, face_id;
19804
19805 xassert (s->first_glyph->type == STRETCH_GLYPH);
19806
19807 glyph = s->row->glyphs[s->area] + start;
19808 last = s->row->glyphs[s->area] + end;
19809 face_id = glyph->face_id;
19810 s->face = FACE_FROM_ID (s->f, face_id);
19811 s->font = s->face->font;
19812 s->width = glyph->pixel_width;
19813 s->nchars = 1;
19814 voffset = glyph->voffset;
19815
19816 for (++glyph;
19817 (glyph < last
19818 && glyph->type == STRETCH_GLYPH
19819 && glyph->voffset == voffset
19820 && glyph->face_id == face_id);
19821 ++glyph)
19822 s->width += glyph->pixel_width;
19823
19824 /* Adjust base line for subscript/superscript text. */
19825 s->ybase += voffset;
19826
19827 /* The case that face->gc == 0 is handled when drawing the glyph
19828 string by calling PREPARE_FACE_FOR_DISPLAY. */
19829 xassert (s->face);
19830 return glyph - s->row->glyphs[s->area];
19831 }
19832
19833 static struct font_metrics *
19834 get_per_char_metric (f, font, char2b)
19835 struct frame *f;
19836 struct font *font;
19837 XChar2b *char2b;
19838 {
19839 static struct font_metrics metrics;
19840 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19841 struct font *fontp;
19842
19843 if (! font || code == FONT_INVALID_CODE)
19844 return NULL;
19845 font->driver->text_extents (font, &code, 1, &metrics);
19846 return &metrics;
19847 }
19848
19849 /* EXPORT for RIF:
19850 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19851 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19852 assumed to be zero. */
19853
19854 void
19855 x_get_glyph_overhangs (glyph, f, left, right)
19856 struct glyph *glyph;
19857 struct frame *f;
19858 int *left, *right;
19859 {
19860 *left = *right = 0;
19861
19862 if (glyph->type == CHAR_GLYPH)
19863 {
19864 struct face *face;
19865 XChar2b char2b;
19866 struct font_metrics *pcm;
19867
19868 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19869 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19870 {
19871 if (pcm->rbearing > pcm->width)
19872 *right = pcm->rbearing - pcm->width;
19873 if (pcm->lbearing < 0)
19874 *left = -pcm->lbearing;
19875 }
19876 }
19877 else if (glyph->type == COMPOSITE_GLYPH)
19878 {
19879 struct composition *cmp = composition_table[glyph->u.cmp_id];
19880
19881 *right = cmp->rbearing - cmp->pixel_width;
19882 *left = - cmp->lbearing;
19883 }
19884 }
19885
19886
19887 /* Return the index of the first glyph preceding glyph string S that
19888 is overwritten by S because of S's left overhang. Value is -1
19889 if no glyphs are overwritten. */
19890
19891 static int
19892 left_overwritten (s)
19893 struct glyph_string *s;
19894 {
19895 int k;
19896
19897 if (s->left_overhang)
19898 {
19899 int x = 0, i;
19900 struct glyph *glyphs = s->row->glyphs[s->area];
19901 int first = s->first_glyph - glyphs;
19902
19903 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19904 x -= glyphs[i].pixel_width;
19905
19906 k = i + 1;
19907 }
19908 else
19909 k = -1;
19910
19911 return k;
19912 }
19913
19914
19915 /* Return the index of the first glyph preceding glyph string S that
19916 is overwriting S because of its right overhang. Value is -1 if no
19917 glyph in front of S overwrites S. */
19918
19919 static int
19920 left_overwriting (s)
19921 struct glyph_string *s;
19922 {
19923 int i, k, x;
19924 struct glyph *glyphs = s->row->glyphs[s->area];
19925 int first = s->first_glyph - glyphs;
19926
19927 k = -1;
19928 x = 0;
19929 for (i = first - 1; i >= 0; --i)
19930 {
19931 int left, right;
19932 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19933 if (x + right > 0)
19934 k = i;
19935 x -= glyphs[i].pixel_width;
19936 }
19937
19938 return k;
19939 }
19940
19941
19942 /* Return the index of the last glyph following glyph string S that is
19943 overwritten by S because of S's right overhang. Value is -1 if
19944 no such glyph is found. */
19945
19946 static int
19947 right_overwritten (s)
19948 struct glyph_string *s;
19949 {
19950 int k = -1;
19951
19952 if (s->right_overhang)
19953 {
19954 int x = 0, i;
19955 struct glyph *glyphs = s->row->glyphs[s->area];
19956 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19957 int end = s->row->used[s->area];
19958
19959 for (i = first; i < end && s->right_overhang > x; ++i)
19960 x += glyphs[i].pixel_width;
19961
19962 k = i;
19963 }
19964
19965 return k;
19966 }
19967
19968
19969 /* Return the index of the last glyph following glyph string S that
19970 overwrites S because of its left overhang. Value is negative
19971 if no such glyph is found. */
19972
19973 static int
19974 right_overwriting (s)
19975 struct glyph_string *s;
19976 {
19977 int i, k, x;
19978 int end = s->row->used[s->area];
19979 struct glyph *glyphs = s->row->glyphs[s->area];
19980 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19981
19982 k = -1;
19983 x = 0;
19984 for (i = first; i < end; ++i)
19985 {
19986 int left, right;
19987 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19988 if (x - left < 0)
19989 k = i;
19990 x += glyphs[i].pixel_width;
19991 }
19992
19993 return k;
19994 }
19995
19996
19997 /* Set background width of glyph string S. START is the index of the
19998 first glyph following S. LAST_X is the right-most x-position + 1
19999 in the drawing area. */
20000
20001 static INLINE void
20002 set_glyph_string_background_width (s, start, last_x)
20003 struct glyph_string *s;
20004 int start;
20005 int last_x;
20006 {
20007 /* If the face of this glyph string has to be drawn to the end of
20008 the drawing area, set S->extends_to_end_of_line_p. */
20009
20010 if (start == s->row->used[s->area]
20011 && s->area == TEXT_AREA
20012 && ((s->row->fill_line_p
20013 && (s->hl == DRAW_NORMAL_TEXT
20014 || s->hl == DRAW_IMAGE_RAISED
20015 || s->hl == DRAW_IMAGE_SUNKEN))
20016 || s->hl == DRAW_MOUSE_FACE))
20017 s->extends_to_end_of_line_p = 1;
20018
20019 /* If S extends its face to the end of the line, set its
20020 background_width to the distance to the right edge of the drawing
20021 area. */
20022 if (s->extends_to_end_of_line_p)
20023 s->background_width = last_x - s->x + 1;
20024 else
20025 s->background_width = s->width;
20026 }
20027
20028
20029 /* Compute overhangs and x-positions for glyph string S and its
20030 predecessors, or successors. X is the starting x-position for S.
20031 BACKWARD_P non-zero means process predecessors. */
20032
20033 static void
20034 compute_overhangs_and_x (s, x, backward_p)
20035 struct glyph_string *s;
20036 int x;
20037 int backward_p;
20038 {
20039 if (backward_p)
20040 {
20041 while (s)
20042 {
20043 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20044 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20045 x -= s->width;
20046 s->x = x;
20047 s = s->prev;
20048 }
20049 }
20050 else
20051 {
20052 while (s)
20053 {
20054 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20055 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20056 s->x = x;
20057 x += s->width;
20058 s = s->next;
20059 }
20060 }
20061 }
20062
20063
20064
20065 /* The following macros are only called from draw_glyphs below.
20066 They reference the following parameters of that function directly:
20067 `w', `row', `area', and `overlap_p'
20068 as well as the following local variables:
20069 `s', `f', and `hdc' (in W32) */
20070
20071 #ifdef HAVE_NTGUI
20072 /* On W32, silently add local `hdc' variable to argument list of
20073 init_glyph_string. */
20074 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20075 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20076 #else
20077 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20078 init_glyph_string (s, char2b, w, row, area, start, hl)
20079 #endif
20080
20081 /* Add a glyph string for a stretch glyph to the list of strings
20082 between HEAD and TAIL. START is the index of the stretch glyph in
20083 row area AREA of glyph row ROW. END is the index of the last glyph
20084 in that glyph row area. X is the current output position assigned
20085 to the new glyph string constructed. HL overrides that face of the
20086 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20087 is the right-most x-position of the drawing area. */
20088
20089 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20090 and below -- keep them on one line. */
20091 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20092 do \
20093 { \
20094 s = (struct glyph_string *) alloca (sizeof *s); \
20095 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20096 START = fill_stretch_glyph_string (s, row, area, START, END); \
20097 append_glyph_string (&HEAD, &TAIL, s); \
20098 s->x = (X); \
20099 } \
20100 while (0)
20101
20102
20103 /* Add a glyph string for an image glyph to the list of strings
20104 between HEAD and TAIL. START is the index of the image glyph in
20105 row area AREA of glyph row ROW. END is the index of the last glyph
20106 in that glyph row area. X is the current output position assigned
20107 to the new glyph string constructed. HL overrides that face of the
20108 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20109 is the right-most x-position of the drawing area. */
20110
20111 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20112 do \
20113 { \
20114 s = (struct glyph_string *) alloca (sizeof *s); \
20115 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20116 fill_image_glyph_string (s); \
20117 append_glyph_string (&HEAD, &TAIL, s); \
20118 ++START; \
20119 s->x = (X); \
20120 } \
20121 while (0)
20122
20123
20124 /* Add a glyph string for a sequence of character glyphs to the list
20125 of strings between HEAD and TAIL. START is the index of the first
20126 glyph in row area AREA of glyph row ROW that is part of the new
20127 glyph string. END is the index of the last glyph in that glyph row
20128 area. X is the current output position assigned to the new glyph
20129 string constructed. HL overrides that face of the glyph; e.g. it
20130 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20131 right-most x-position of the drawing area. */
20132
20133 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20134 do \
20135 { \
20136 int face_id; \
20137 XChar2b *char2b; \
20138 \
20139 face_id = (row)->glyphs[area][START].face_id; \
20140 \
20141 s = (struct glyph_string *) alloca (sizeof *s); \
20142 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20143 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20144 append_glyph_string (&HEAD, &TAIL, s); \
20145 s->x = (X); \
20146 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20147 } \
20148 while (0)
20149
20150
20151 /* Add a glyph string for a composite sequence to the list of strings
20152 between HEAD and TAIL. START is the index of the first glyph in
20153 row area AREA of glyph row ROW that is part of the new glyph
20154 string. END is the index of the last glyph in that glyph row area.
20155 X is the current output position assigned to the new glyph string
20156 constructed. HL overrides that face of the glyph; e.g. it is
20157 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20158 x-position of the drawing area. */
20159
20160 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20161 do { \
20162 int face_id = (row)->glyphs[area][START].face_id; \
20163 struct face *base_face = FACE_FROM_ID (f, face_id); \
20164 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
20165 struct composition *cmp = composition_table[cmp_id]; \
20166 XChar2b *char2b; \
20167 struct glyph_string *first_s; \
20168 int n; \
20169 \
20170 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20171 \
20172 /* Make glyph_strings for each glyph sequence that is drawable by \
20173 the same face, and append them to HEAD/TAIL. */ \
20174 for (n = 0; n < cmp->glyph_len;) \
20175 { \
20176 s = (struct glyph_string *) alloca (sizeof *s); \
20177 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20178 append_glyph_string (&(HEAD), &(TAIL), s); \
20179 s->cmp = cmp; \
20180 s->gidx = n; \
20181 s->x = (X); \
20182 if (n == 0) \
20183 first_s = s; \
20184 n = fill_composite_glyph_string (s, base_face, overlaps); \
20185 } \
20186 \
20187 ++START; \
20188 s = first_s; \
20189 } while (0)
20190
20191
20192 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20193 of AREA of glyph row ROW on window W between indices START and END.
20194 HL overrides the face for drawing glyph strings, e.g. it is
20195 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20196 x-positions of the drawing area.
20197
20198 This is an ugly monster macro construct because we must use alloca
20199 to allocate glyph strings (because draw_glyphs can be called
20200 asynchronously). */
20201
20202 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20203 do \
20204 { \
20205 HEAD = TAIL = NULL; \
20206 while (START < END) \
20207 { \
20208 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20209 switch (first_glyph->type) \
20210 { \
20211 case CHAR_GLYPH: \
20212 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20213 HL, X, LAST_X); \
20214 break; \
20215 \
20216 case COMPOSITE_GLYPH: \
20217 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20218 HL, X, LAST_X); \
20219 break; \
20220 \
20221 case STRETCH_GLYPH: \
20222 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20223 HL, X, LAST_X); \
20224 break; \
20225 \
20226 case IMAGE_GLYPH: \
20227 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20228 HL, X, LAST_X); \
20229 break; \
20230 \
20231 default: \
20232 abort (); \
20233 } \
20234 \
20235 if (s) \
20236 { \
20237 set_glyph_string_background_width (s, START, LAST_X); \
20238 (X) += s->width; \
20239 } \
20240 } \
20241 } \
20242 while (0)
20243
20244
20245 /* Draw glyphs between START and END in AREA of ROW on window W,
20246 starting at x-position X. X is relative to AREA in W. HL is a
20247 face-override with the following meaning:
20248
20249 DRAW_NORMAL_TEXT draw normally
20250 DRAW_CURSOR draw in cursor face
20251 DRAW_MOUSE_FACE draw in mouse face.
20252 DRAW_INVERSE_VIDEO draw in mode line face
20253 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20254 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20255
20256 If OVERLAPS is non-zero, draw only the foreground of characters and
20257 clip to the physical height of ROW. Non-zero value also defines
20258 the overlapping part to be drawn:
20259
20260 OVERLAPS_PRED overlap with preceding rows
20261 OVERLAPS_SUCC overlap with succeeding rows
20262 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20263 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20264
20265 Value is the x-position reached, relative to AREA of W. */
20266
20267 static int
20268 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20269 struct window *w;
20270 int x;
20271 struct glyph_row *row;
20272 enum glyph_row_area area;
20273 EMACS_INT start, end;
20274 enum draw_glyphs_face hl;
20275 int overlaps;
20276 {
20277 struct glyph_string *head, *tail;
20278 struct glyph_string *s;
20279 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20280 int i, j, x_reached, last_x, area_left = 0;
20281 struct frame *f = XFRAME (WINDOW_FRAME (w));
20282 DECLARE_HDC (hdc);
20283
20284 ALLOCATE_HDC (hdc, f);
20285
20286 /* Let's rather be paranoid than getting a SEGV. */
20287 end = min (end, row->used[area]);
20288 start = max (0, start);
20289 start = min (end, start);
20290
20291 /* Translate X to frame coordinates. Set last_x to the right
20292 end of the drawing area. */
20293 if (row->full_width_p)
20294 {
20295 /* X is relative to the left edge of W, without scroll bars
20296 or fringes. */
20297 area_left = WINDOW_LEFT_EDGE_X (w);
20298 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20299 }
20300 else
20301 {
20302 area_left = window_box_left (w, area);
20303 last_x = area_left + window_box_width (w, area);
20304 }
20305 x += area_left;
20306
20307 /* Build a doubly-linked list of glyph_string structures between
20308 head and tail from what we have to draw. Note that the macro
20309 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20310 the reason we use a separate variable `i'. */
20311 i = start;
20312 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20313 if (tail)
20314 x_reached = tail->x + tail->background_width;
20315 else
20316 x_reached = x;
20317
20318 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20319 the row, redraw some glyphs in front or following the glyph
20320 strings built above. */
20321 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20322 {
20323 struct glyph_string *h, *t;
20324 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20325 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20326 int dummy_x = 0;
20327
20328 /* If mouse highlighting is on, we may need to draw adjacent
20329 glyphs using mouse-face highlighting. */
20330 if (area == TEXT_AREA && row->mouse_face_p)
20331 {
20332 struct glyph_row *mouse_beg_row, *mouse_end_row;
20333
20334 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20335 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20336
20337 if (row >= mouse_beg_row && row <= mouse_end_row)
20338 {
20339 check_mouse_face = 1;
20340 mouse_beg_col = (row == mouse_beg_row)
20341 ? dpyinfo->mouse_face_beg_col : 0;
20342 mouse_end_col = (row == mouse_end_row)
20343 ? dpyinfo->mouse_face_end_col
20344 : row->used[TEXT_AREA];
20345 }
20346 }
20347
20348 /* Compute overhangs for all glyph strings. */
20349 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20350 for (s = head; s; s = s->next)
20351 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20352
20353 /* Prepend glyph strings for glyphs in front of the first glyph
20354 string that are overwritten because of the first glyph
20355 string's left overhang. The background of all strings
20356 prepended must be drawn because the first glyph string
20357 draws over it. */
20358 i = left_overwritten (head);
20359 if (i >= 0)
20360 {
20361 enum draw_glyphs_face overlap_hl;
20362
20363 /* If this row contains mouse highlighting, attempt to draw
20364 the overlapped glyphs with the correct highlight. This
20365 code fails if the overlap encompasses more than one glyph
20366 and mouse-highlight spans only some of these glyphs.
20367 However, making it work perfectly involves a lot more
20368 code, and I don't know if the pathological case occurs in
20369 practice, so we'll stick to this for now. --- cyd */
20370 if (check_mouse_face
20371 && mouse_beg_col < start && mouse_end_col > i)
20372 overlap_hl = DRAW_MOUSE_FACE;
20373 else
20374 overlap_hl = DRAW_NORMAL_TEXT;
20375
20376 j = i;
20377 BUILD_GLYPH_STRINGS (j, start, h, t,
20378 overlap_hl, dummy_x, last_x);
20379 compute_overhangs_and_x (t, head->x, 1);
20380 prepend_glyph_string_lists (&head, &tail, h, t);
20381 clip_head = head;
20382 }
20383
20384 /* Prepend glyph strings for glyphs in front of the first glyph
20385 string that overwrite that glyph string because of their
20386 right overhang. For these strings, only the foreground must
20387 be drawn, because it draws over the glyph string at `head'.
20388 The background must not be drawn because this would overwrite
20389 right overhangs of preceding glyphs for which no glyph
20390 strings exist. */
20391 i = left_overwriting (head);
20392 if (i >= 0)
20393 {
20394 enum draw_glyphs_face overlap_hl;
20395
20396 if (check_mouse_face
20397 && mouse_beg_col < start && mouse_end_col > i)
20398 overlap_hl = DRAW_MOUSE_FACE;
20399 else
20400 overlap_hl = DRAW_NORMAL_TEXT;
20401
20402 clip_head = head;
20403 BUILD_GLYPH_STRINGS (i, start, h, t,
20404 overlap_hl, dummy_x, last_x);
20405 for (s = h; s; s = s->next)
20406 s->background_filled_p = 1;
20407 compute_overhangs_and_x (t, head->x, 1);
20408 prepend_glyph_string_lists (&head, &tail, h, t);
20409 }
20410
20411 /* Append glyphs strings for glyphs following the last glyph
20412 string tail that are overwritten by tail. The background of
20413 these strings has to be drawn because tail's foreground draws
20414 over it. */
20415 i = right_overwritten (tail);
20416 if (i >= 0)
20417 {
20418 enum draw_glyphs_face overlap_hl;
20419
20420 if (check_mouse_face
20421 && mouse_beg_col < i && mouse_end_col > end)
20422 overlap_hl = DRAW_MOUSE_FACE;
20423 else
20424 overlap_hl = DRAW_NORMAL_TEXT;
20425
20426 BUILD_GLYPH_STRINGS (end, i, h, t,
20427 overlap_hl, x, last_x);
20428 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20429 append_glyph_string_lists (&head, &tail, h, t);
20430 clip_tail = tail;
20431 }
20432
20433 /* Append glyph strings for glyphs following the last glyph
20434 string tail that overwrite tail. The foreground of such
20435 glyphs has to be drawn because it writes into the background
20436 of tail. The background must not be drawn because it could
20437 paint over the foreground of following glyphs. */
20438 i = right_overwriting (tail);
20439 if (i >= 0)
20440 {
20441 enum draw_glyphs_face overlap_hl;
20442 if (check_mouse_face
20443 && mouse_beg_col < i && mouse_end_col > end)
20444 overlap_hl = DRAW_MOUSE_FACE;
20445 else
20446 overlap_hl = DRAW_NORMAL_TEXT;
20447
20448 clip_tail = tail;
20449 i++; /* We must include the Ith glyph. */
20450 BUILD_GLYPH_STRINGS (end, i, h, t,
20451 overlap_hl, x, last_x);
20452 for (s = h; s; s = s->next)
20453 s->background_filled_p = 1;
20454 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20455 append_glyph_string_lists (&head, &tail, h, t);
20456 }
20457 if (clip_head || clip_tail)
20458 for (s = head; s; s = s->next)
20459 {
20460 s->clip_head = clip_head;
20461 s->clip_tail = clip_tail;
20462 }
20463 }
20464
20465 /* Draw all strings. */
20466 for (s = head; s; s = s->next)
20467 FRAME_RIF (f)->draw_glyph_string (s);
20468
20469 if (area == TEXT_AREA
20470 && !row->full_width_p
20471 /* When drawing overlapping rows, only the glyph strings'
20472 foreground is drawn, which doesn't erase a cursor
20473 completely. */
20474 && !overlaps)
20475 {
20476 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20477 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20478 : (tail ? tail->x + tail->background_width : x));
20479 x0 -= area_left;
20480 x1 -= area_left;
20481
20482 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20483 row->y, MATRIX_ROW_BOTTOM_Y (row));
20484 }
20485
20486 /* Value is the x-position up to which drawn, relative to AREA of W.
20487 This doesn't include parts drawn because of overhangs. */
20488 if (row->full_width_p)
20489 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20490 else
20491 x_reached -= area_left;
20492
20493 RELEASE_HDC (hdc, f);
20494
20495 return x_reached;
20496 }
20497
20498 /* Expand row matrix if too narrow. Don't expand if area
20499 is not present. */
20500
20501 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20502 { \
20503 if (!fonts_changed_p \
20504 && (it->glyph_row->glyphs[area] \
20505 < it->glyph_row->glyphs[area + 1])) \
20506 { \
20507 it->w->ncols_scale_factor++; \
20508 fonts_changed_p = 1; \
20509 } \
20510 }
20511
20512 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20513 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20514
20515 static INLINE void
20516 append_glyph (it)
20517 struct it *it;
20518 {
20519 struct glyph *glyph;
20520 enum glyph_row_area area = it->area;
20521
20522 xassert (it->glyph_row);
20523 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20524
20525 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20526 if (glyph < it->glyph_row->glyphs[area + 1])
20527 {
20528 glyph->charpos = CHARPOS (it->position);
20529 glyph->object = it->object;
20530 if (it->pixel_width > 0)
20531 {
20532 glyph->pixel_width = it->pixel_width;
20533 glyph->padding_p = 0;
20534 }
20535 else
20536 {
20537 /* Assure at least 1-pixel width. Otherwise, cursor can't
20538 be displayed correctly. */
20539 glyph->pixel_width = 1;
20540 glyph->padding_p = 1;
20541 }
20542 glyph->ascent = it->ascent;
20543 glyph->descent = it->descent;
20544 glyph->voffset = it->voffset;
20545 glyph->type = CHAR_GLYPH;
20546 glyph->avoid_cursor_p = it->avoid_cursor_p;
20547 glyph->multibyte_p = it->multibyte_p;
20548 glyph->left_box_line_p = it->start_of_box_run_p;
20549 glyph->right_box_line_p = it->end_of_box_run_p;
20550 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20551 || it->phys_descent > it->descent);
20552 glyph->glyph_not_available_p = it->glyph_not_available_p;
20553 glyph->face_id = it->face_id;
20554 glyph->u.ch = it->char_to_display;
20555 glyph->slice = null_glyph_slice;
20556 glyph->font_type = FONT_TYPE_UNKNOWN;
20557 ++it->glyph_row->used[area];
20558 }
20559 else
20560 IT_EXPAND_MATRIX_WIDTH (it, area);
20561 }
20562
20563 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20564 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20565
20566 static INLINE void
20567 append_composite_glyph (it)
20568 struct it *it;
20569 {
20570 struct glyph *glyph;
20571 enum glyph_row_area area = it->area;
20572
20573 xassert (it->glyph_row);
20574
20575 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20576 if (glyph < it->glyph_row->glyphs[area + 1])
20577 {
20578 glyph->charpos = CHARPOS (it->position);
20579 glyph->object = it->object;
20580 glyph->pixel_width = it->pixel_width;
20581 glyph->ascent = it->ascent;
20582 glyph->descent = it->descent;
20583 glyph->voffset = it->voffset;
20584 glyph->type = COMPOSITE_GLYPH;
20585 glyph->avoid_cursor_p = it->avoid_cursor_p;
20586 glyph->multibyte_p = it->multibyte_p;
20587 glyph->left_box_line_p = it->start_of_box_run_p;
20588 glyph->right_box_line_p = it->end_of_box_run_p;
20589 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20590 || it->phys_descent > it->descent);
20591 glyph->padding_p = 0;
20592 glyph->glyph_not_available_p = 0;
20593 glyph->face_id = it->face_id;
20594 glyph->u.cmp_id = it->cmp_id;
20595 glyph->slice = null_glyph_slice;
20596 glyph->font_type = FONT_TYPE_UNKNOWN;
20597 ++it->glyph_row->used[area];
20598 }
20599 else
20600 IT_EXPAND_MATRIX_WIDTH (it, area);
20601 }
20602
20603
20604 /* Change IT->ascent and IT->height according to the setting of
20605 IT->voffset. */
20606
20607 static INLINE void
20608 take_vertical_position_into_account (it)
20609 struct it *it;
20610 {
20611 if (it->voffset)
20612 {
20613 if (it->voffset < 0)
20614 /* Increase the ascent so that we can display the text higher
20615 in the line. */
20616 it->ascent -= it->voffset;
20617 else
20618 /* Increase the descent so that we can display the text lower
20619 in the line. */
20620 it->descent += it->voffset;
20621 }
20622 }
20623
20624
20625 /* Produce glyphs/get display metrics for the image IT is loaded with.
20626 See the description of struct display_iterator in dispextern.h for
20627 an overview of struct display_iterator. */
20628
20629 static void
20630 produce_image_glyph (it)
20631 struct it *it;
20632 {
20633 struct image *img;
20634 struct face *face;
20635 int glyph_ascent, crop;
20636 struct glyph_slice slice;
20637
20638 xassert (it->what == IT_IMAGE);
20639
20640 face = FACE_FROM_ID (it->f, it->face_id);
20641 xassert (face);
20642 /* Make sure X resources of the face is loaded. */
20643 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20644
20645 if (it->image_id < 0)
20646 {
20647 /* Fringe bitmap. */
20648 it->ascent = it->phys_ascent = 0;
20649 it->descent = it->phys_descent = 0;
20650 it->pixel_width = 0;
20651 it->nglyphs = 0;
20652 return;
20653 }
20654
20655 img = IMAGE_FROM_ID (it->f, it->image_id);
20656 xassert (img);
20657 /* Make sure X resources of the image is loaded. */
20658 prepare_image_for_display (it->f, img);
20659
20660 slice.x = slice.y = 0;
20661 slice.width = img->width;
20662 slice.height = img->height;
20663
20664 if (INTEGERP (it->slice.x))
20665 slice.x = XINT (it->slice.x);
20666 else if (FLOATP (it->slice.x))
20667 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20668
20669 if (INTEGERP (it->slice.y))
20670 slice.y = XINT (it->slice.y);
20671 else if (FLOATP (it->slice.y))
20672 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20673
20674 if (INTEGERP (it->slice.width))
20675 slice.width = XINT (it->slice.width);
20676 else if (FLOATP (it->slice.width))
20677 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20678
20679 if (INTEGERP (it->slice.height))
20680 slice.height = XINT (it->slice.height);
20681 else if (FLOATP (it->slice.height))
20682 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20683
20684 if (slice.x >= img->width)
20685 slice.x = img->width;
20686 if (slice.y >= img->height)
20687 slice.y = img->height;
20688 if (slice.x + slice.width >= img->width)
20689 slice.width = img->width - slice.x;
20690 if (slice.y + slice.height > img->height)
20691 slice.height = img->height - slice.y;
20692
20693 if (slice.width == 0 || slice.height == 0)
20694 return;
20695
20696 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20697
20698 it->descent = slice.height - glyph_ascent;
20699 if (slice.y == 0)
20700 it->descent += img->vmargin;
20701 if (slice.y + slice.height == img->height)
20702 it->descent += img->vmargin;
20703 it->phys_descent = it->descent;
20704
20705 it->pixel_width = slice.width;
20706 if (slice.x == 0)
20707 it->pixel_width += img->hmargin;
20708 if (slice.x + slice.width == img->width)
20709 it->pixel_width += img->hmargin;
20710
20711 /* It's quite possible for images to have an ascent greater than
20712 their height, so don't get confused in that case. */
20713 if (it->descent < 0)
20714 it->descent = 0;
20715
20716 #if 0 /* this breaks image tiling */
20717 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20718 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20719 if (face_ascent > it->ascent)
20720 it->ascent = it->phys_ascent = face_ascent;
20721 #endif
20722
20723 it->nglyphs = 1;
20724
20725 if (face->box != FACE_NO_BOX)
20726 {
20727 if (face->box_line_width > 0)
20728 {
20729 if (slice.y == 0)
20730 it->ascent += face->box_line_width;
20731 if (slice.y + slice.height == img->height)
20732 it->descent += face->box_line_width;
20733 }
20734
20735 if (it->start_of_box_run_p && slice.x == 0)
20736 it->pixel_width += eabs (face->box_line_width);
20737 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20738 it->pixel_width += eabs (face->box_line_width);
20739 }
20740
20741 take_vertical_position_into_account (it);
20742
20743 /* Automatically crop wide image glyphs at right edge so we can
20744 draw the cursor on same display row. */
20745 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20746 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20747 {
20748 it->pixel_width -= crop;
20749 slice.width -= crop;
20750 }
20751
20752 if (it->glyph_row)
20753 {
20754 struct glyph *glyph;
20755 enum glyph_row_area area = it->area;
20756
20757 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20758 if (glyph < it->glyph_row->glyphs[area + 1])
20759 {
20760 glyph->charpos = CHARPOS (it->position);
20761 glyph->object = it->object;
20762 glyph->pixel_width = it->pixel_width;
20763 glyph->ascent = glyph_ascent;
20764 glyph->descent = it->descent;
20765 glyph->voffset = it->voffset;
20766 glyph->type = IMAGE_GLYPH;
20767 glyph->avoid_cursor_p = it->avoid_cursor_p;
20768 glyph->multibyte_p = it->multibyte_p;
20769 glyph->left_box_line_p = it->start_of_box_run_p;
20770 glyph->right_box_line_p = it->end_of_box_run_p;
20771 glyph->overlaps_vertically_p = 0;
20772 glyph->padding_p = 0;
20773 glyph->glyph_not_available_p = 0;
20774 glyph->face_id = it->face_id;
20775 glyph->u.img_id = img->id;
20776 glyph->slice = slice;
20777 glyph->font_type = FONT_TYPE_UNKNOWN;
20778 ++it->glyph_row->used[area];
20779 }
20780 else
20781 IT_EXPAND_MATRIX_WIDTH (it, area);
20782 }
20783 }
20784
20785
20786 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20787 of the glyph, WIDTH and HEIGHT are the width and height of the
20788 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20789
20790 static void
20791 append_stretch_glyph (it, object, width, height, ascent)
20792 struct it *it;
20793 Lisp_Object object;
20794 int width, height;
20795 int ascent;
20796 {
20797 struct glyph *glyph;
20798 enum glyph_row_area area = it->area;
20799
20800 xassert (ascent >= 0 && ascent <= height);
20801
20802 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20803 if (glyph < it->glyph_row->glyphs[area + 1])
20804 {
20805 glyph->charpos = CHARPOS (it->position);
20806 glyph->object = object;
20807 glyph->pixel_width = width;
20808 glyph->ascent = ascent;
20809 glyph->descent = height - ascent;
20810 glyph->voffset = it->voffset;
20811 glyph->type = STRETCH_GLYPH;
20812 glyph->avoid_cursor_p = it->avoid_cursor_p;
20813 glyph->multibyte_p = it->multibyte_p;
20814 glyph->left_box_line_p = it->start_of_box_run_p;
20815 glyph->right_box_line_p = it->end_of_box_run_p;
20816 glyph->overlaps_vertically_p = 0;
20817 glyph->padding_p = 0;
20818 glyph->glyph_not_available_p = 0;
20819 glyph->face_id = it->face_id;
20820 glyph->u.stretch.ascent = ascent;
20821 glyph->u.stretch.height = height;
20822 glyph->slice = null_glyph_slice;
20823 glyph->font_type = FONT_TYPE_UNKNOWN;
20824 ++it->glyph_row->used[area];
20825 }
20826 else
20827 IT_EXPAND_MATRIX_WIDTH (it, area);
20828 }
20829
20830
20831 /* Produce a stretch glyph for iterator IT. IT->object is the value
20832 of the glyph property displayed. The value must be a list
20833 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20834 being recognized:
20835
20836 1. `:width WIDTH' specifies that the space should be WIDTH *
20837 canonical char width wide. WIDTH may be an integer or floating
20838 point number.
20839
20840 2. `:relative-width FACTOR' specifies that the width of the stretch
20841 should be computed from the width of the first character having the
20842 `glyph' property, and should be FACTOR times that width.
20843
20844 3. `:align-to HPOS' specifies that the space should be wide enough
20845 to reach HPOS, a value in canonical character units.
20846
20847 Exactly one of the above pairs must be present.
20848
20849 4. `:height HEIGHT' specifies that the height of the stretch produced
20850 should be HEIGHT, measured in canonical character units.
20851
20852 5. `:relative-height FACTOR' specifies that the height of the
20853 stretch should be FACTOR times the height of the characters having
20854 the glyph property.
20855
20856 Either none or exactly one of 4 or 5 must be present.
20857
20858 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20859 of the stretch should be used for the ascent of the stretch.
20860 ASCENT must be in the range 0 <= ASCENT <= 100. */
20861
20862 static void
20863 produce_stretch_glyph (it)
20864 struct it *it;
20865 {
20866 /* (space :width WIDTH :height HEIGHT ...) */
20867 Lisp_Object prop, plist;
20868 int width = 0, height = 0, align_to = -1;
20869 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20870 int ascent = 0;
20871 double tem;
20872 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20873 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20874
20875 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20876
20877 /* List should start with `space'. */
20878 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20879 plist = XCDR (it->object);
20880
20881 /* Compute the width of the stretch. */
20882 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20883 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20884 {
20885 /* Absolute width `:width WIDTH' specified and valid. */
20886 zero_width_ok_p = 1;
20887 width = (int)tem;
20888 }
20889 else if (prop = Fplist_get (plist, QCrelative_width),
20890 NUMVAL (prop) > 0)
20891 {
20892 /* Relative width `:relative-width FACTOR' specified and valid.
20893 Compute the width of the characters having the `glyph'
20894 property. */
20895 struct it it2;
20896 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20897
20898 it2 = *it;
20899 if (it->multibyte_p)
20900 {
20901 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20902 - IT_BYTEPOS (*it));
20903 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20904 }
20905 else
20906 it2.c = *p, it2.len = 1;
20907
20908 it2.glyph_row = NULL;
20909 it2.what = IT_CHARACTER;
20910 x_produce_glyphs (&it2);
20911 width = NUMVAL (prop) * it2.pixel_width;
20912 }
20913 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20914 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20915 {
20916 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20917 align_to = (align_to < 0
20918 ? 0
20919 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20920 else if (align_to < 0)
20921 align_to = window_box_left_offset (it->w, TEXT_AREA);
20922 width = max (0, (int)tem + align_to - it->current_x);
20923 zero_width_ok_p = 1;
20924 }
20925 else
20926 /* Nothing specified -> width defaults to canonical char width. */
20927 width = FRAME_COLUMN_WIDTH (it->f);
20928
20929 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20930 width = 1;
20931
20932 /* Compute height. */
20933 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20934 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20935 {
20936 height = (int)tem;
20937 zero_height_ok_p = 1;
20938 }
20939 else if (prop = Fplist_get (plist, QCrelative_height),
20940 NUMVAL (prop) > 0)
20941 height = FONT_HEIGHT (font) * NUMVAL (prop);
20942 else
20943 height = FONT_HEIGHT (font);
20944
20945 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20946 height = 1;
20947
20948 /* Compute percentage of height used for ascent. If
20949 `:ascent ASCENT' is present and valid, use that. Otherwise,
20950 derive the ascent from the font in use. */
20951 if (prop = Fplist_get (plist, QCascent),
20952 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20953 ascent = height * NUMVAL (prop) / 100.0;
20954 else if (!NILP (prop)
20955 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20956 ascent = min (max (0, (int)tem), height);
20957 else
20958 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20959
20960 if (width > 0 && it->line_wrap != TRUNCATE
20961 && it->current_x + width > it->last_visible_x)
20962 width = it->last_visible_x - it->current_x - 1;
20963
20964 if (width > 0 && height > 0 && it->glyph_row)
20965 {
20966 Lisp_Object object = it->stack[it->sp - 1].string;
20967 if (!STRINGP (object))
20968 object = it->w->buffer;
20969 append_stretch_glyph (it, object, width, height, ascent);
20970 }
20971
20972 it->pixel_width = width;
20973 it->ascent = it->phys_ascent = ascent;
20974 it->descent = it->phys_descent = height - it->ascent;
20975 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20976
20977 take_vertical_position_into_account (it);
20978 }
20979
20980 /* Calculate line-height and line-spacing properties.
20981 An integer value specifies explicit pixel value.
20982 A float value specifies relative value to current face height.
20983 A cons (float . face-name) specifies relative value to
20984 height of specified face font.
20985
20986 Returns height in pixels, or nil. */
20987
20988
20989 static Lisp_Object
20990 calc_line_height_property (it, val, font, boff, override)
20991 struct it *it;
20992 Lisp_Object val;
20993 struct font *font;
20994 int boff, override;
20995 {
20996 Lisp_Object face_name = Qnil;
20997 int ascent, descent, height;
20998
20999 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21000 return val;
21001
21002 if (CONSP (val))
21003 {
21004 face_name = XCAR (val);
21005 val = XCDR (val);
21006 if (!NUMBERP (val))
21007 val = make_number (1);
21008 if (NILP (face_name))
21009 {
21010 height = it->ascent + it->descent;
21011 goto scale;
21012 }
21013 }
21014
21015 if (NILP (face_name))
21016 {
21017 font = FRAME_FONT (it->f);
21018 boff = FRAME_BASELINE_OFFSET (it->f);
21019 }
21020 else if (EQ (face_name, Qt))
21021 {
21022 override = 0;
21023 }
21024 else
21025 {
21026 int face_id;
21027 struct face *face;
21028
21029 face_id = lookup_named_face (it->f, face_name, 0);
21030 if (face_id < 0)
21031 return make_number (-1);
21032
21033 face = FACE_FROM_ID (it->f, face_id);
21034 font = face->font;
21035 if (font == NULL)
21036 return make_number (-1);
21037 boff = font->baseline_offset;
21038 if (font->vertical_centering)
21039 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21040 }
21041
21042 ascent = FONT_BASE (font) + boff;
21043 descent = FONT_DESCENT (font) - boff;
21044
21045 if (override)
21046 {
21047 it->override_ascent = ascent;
21048 it->override_descent = descent;
21049 it->override_boff = boff;
21050 }
21051
21052 height = ascent + descent;
21053
21054 scale:
21055 if (FLOATP (val))
21056 height = (int)(XFLOAT_DATA (val) * height);
21057 else if (INTEGERP (val))
21058 height *= XINT (val);
21059
21060 return make_number (height);
21061 }
21062
21063
21064 /* RIF:
21065 Produce glyphs/get display metrics for the display element IT is
21066 loaded with. See the description of struct it in dispextern.h
21067 for an overview of struct it. */
21068
21069 void
21070 x_produce_glyphs (it)
21071 struct it *it;
21072 {
21073 int extra_line_spacing = it->extra_line_spacing;
21074
21075 it->glyph_not_available_p = 0;
21076
21077 if (it->what == IT_CHARACTER)
21078 {
21079 XChar2b char2b;
21080 struct font *font;
21081 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21082 struct font_metrics *pcm;
21083 int font_not_found_p;
21084 int boff; /* baseline offset */
21085 /* We may change it->multibyte_p upon unibyte<->multibyte
21086 conversion. So, save the current value now and restore it
21087 later.
21088
21089 Note: It seems that we don't have to record multibyte_p in
21090 struct glyph because the character code itself tells if or
21091 not the character is multibyte. Thus, in the future, we must
21092 consider eliminating the field `multibyte_p' in the struct
21093 glyph. */
21094 int saved_multibyte_p = it->multibyte_p;
21095
21096 /* Maybe translate single-byte characters to multibyte, or the
21097 other way. */
21098 it->char_to_display = it->c;
21099 if (!ASCII_BYTE_P (it->c)
21100 && ! it->multibyte_p)
21101 {
21102 if (SINGLE_BYTE_CHAR_P (it->c)
21103 && unibyte_display_via_language_environment)
21104 it->char_to_display = unibyte_char_to_multibyte (it->c);
21105 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
21106 {
21107 it->multibyte_p = 1;
21108 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21109 -1, Qnil);
21110 face = FACE_FROM_ID (it->f, it->face_id);
21111 }
21112 }
21113
21114 /* Get font to use. Encode IT->char_to_display. */
21115 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21116 &char2b, it->multibyte_p, 0);
21117 font = face->font;
21118
21119 /* When no suitable font found, use the default font. */
21120 font_not_found_p = font == NULL;
21121 if (font_not_found_p)
21122 {
21123 font = FRAME_FONT (it->f);
21124 boff = FRAME_BASELINE_OFFSET (it->f);
21125 }
21126 else
21127 {
21128 boff = font->baseline_offset;
21129 if (font->vertical_centering)
21130 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21131 }
21132
21133 if (it->char_to_display >= ' '
21134 && (!it->multibyte_p || it->char_to_display < 128))
21135 {
21136 /* Either unibyte or ASCII. */
21137 int stretched_p;
21138
21139 it->nglyphs = 1;
21140
21141 pcm = get_per_char_metric (it->f, font, &char2b);
21142
21143 if (it->override_ascent >= 0)
21144 {
21145 it->ascent = it->override_ascent;
21146 it->descent = it->override_descent;
21147 boff = it->override_boff;
21148 }
21149 else
21150 {
21151 it->ascent = FONT_BASE (font) + boff;
21152 it->descent = FONT_DESCENT (font) - boff;
21153 }
21154
21155 if (pcm)
21156 {
21157 it->phys_ascent = pcm->ascent + boff;
21158 it->phys_descent = pcm->descent - boff;
21159 it->pixel_width = pcm->width;
21160 }
21161 else
21162 {
21163 it->glyph_not_available_p = 1;
21164 it->phys_ascent = it->ascent;
21165 it->phys_descent = it->descent;
21166 it->pixel_width = FONT_WIDTH (font);
21167 }
21168
21169 if (it->constrain_row_ascent_descent_p)
21170 {
21171 if (it->descent > it->max_descent)
21172 {
21173 it->ascent += it->descent - it->max_descent;
21174 it->descent = it->max_descent;
21175 }
21176 if (it->ascent > it->max_ascent)
21177 {
21178 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21179 it->ascent = it->max_ascent;
21180 }
21181 it->phys_ascent = min (it->phys_ascent, it->ascent);
21182 it->phys_descent = min (it->phys_descent, it->descent);
21183 extra_line_spacing = 0;
21184 }
21185
21186 /* If this is a space inside a region of text with
21187 `space-width' property, change its width. */
21188 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21189 if (stretched_p)
21190 it->pixel_width *= XFLOATINT (it->space_width);
21191
21192 /* If face has a box, add the box thickness to the character
21193 height. If character has a box line to the left and/or
21194 right, add the box line width to the character's width. */
21195 if (face->box != FACE_NO_BOX)
21196 {
21197 int thick = face->box_line_width;
21198
21199 if (thick > 0)
21200 {
21201 it->ascent += thick;
21202 it->descent += thick;
21203 }
21204 else
21205 thick = -thick;
21206
21207 if (it->start_of_box_run_p)
21208 it->pixel_width += thick;
21209 if (it->end_of_box_run_p)
21210 it->pixel_width += thick;
21211 }
21212
21213 /* If face has an overline, add the height of the overline
21214 (1 pixel) and a 1 pixel margin to the character height. */
21215 if (face->overline_p)
21216 it->ascent += overline_margin;
21217
21218 if (it->constrain_row_ascent_descent_p)
21219 {
21220 if (it->ascent > it->max_ascent)
21221 it->ascent = it->max_ascent;
21222 if (it->descent > it->max_descent)
21223 it->descent = it->max_descent;
21224 }
21225
21226 take_vertical_position_into_account (it);
21227
21228 /* If we have to actually produce glyphs, do it. */
21229 if (it->glyph_row)
21230 {
21231 if (stretched_p)
21232 {
21233 /* Translate a space with a `space-width' property
21234 into a stretch glyph. */
21235 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21236 / FONT_HEIGHT (font));
21237 append_stretch_glyph (it, it->object, it->pixel_width,
21238 it->ascent + it->descent, ascent);
21239 }
21240 else
21241 append_glyph (it);
21242
21243 /* If characters with lbearing or rbearing are displayed
21244 in this line, record that fact in a flag of the
21245 glyph row. This is used to optimize X output code. */
21246 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21247 it->glyph_row->contains_overlapping_glyphs_p = 1;
21248 }
21249 if (! stretched_p && it->pixel_width == 0)
21250 /* We assure that all visible glyphs have at least 1-pixel
21251 width. */
21252 it->pixel_width = 1;
21253 }
21254 else if (it->char_to_display == '\n')
21255 {
21256 /* A newline has no width but we need the height of the line.
21257 But if previous part of the line set a height, don't
21258 increase that height */
21259
21260 Lisp_Object height;
21261 Lisp_Object total_height = Qnil;
21262
21263 it->override_ascent = -1;
21264 it->pixel_width = 0;
21265 it->nglyphs = 0;
21266
21267 height = get_it_property(it, Qline_height);
21268 /* Split (line-height total-height) list */
21269 if (CONSP (height)
21270 && CONSP (XCDR (height))
21271 && NILP (XCDR (XCDR (height))))
21272 {
21273 total_height = XCAR (XCDR (height));
21274 height = XCAR (height);
21275 }
21276 height = calc_line_height_property(it, height, font, boff, 1);
21277
21278 if (it->override_ascent >= 0)
21279 {
21280 it->ascent = it->override_ascent;
21281 it->descent = it->override_descent;
21282 boff = it->override_boff;
21283 }
21284 else
21285 {
21286 it->ascent = FONT_BASE (font) + boff;
21287 it->descent = FONT_DESCENT (font) - boff;
21288 }
21289
21290 if (EQ (height, Qt))
21291 {
21292 if (it->descent > it->max_descent)
21293 {
21294 it->ascent += it->descent - it->max_descent;
21295 it->descent = it->max_descent;
21296 }
21297 if (it->ascent > it->max_ascent)
21298 {
21299 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21300 it->ascent = it->max_ascent;
21301 }
21302 it->phys_ascent = min (it->phys_ascent, it->ascent);
21303 it->phys_descent = min (it->phys_descent, it->descent);
21304 it->constrain_row_ascent_descent_p = 1;
21305 extra_line_spacing = 0;
21306 }
21307 else
21308 {
21309 Lisp_Object spacing;
21310
21311 it->phys_ascent = it->ascent;
21312 it->phys_descent = it->descent;
21313
21314 if ((it->max_ascent > 0 || it->max_descent > 0)
21315 && face->box != FACE_NO_BOX
21316 && face->box_line_width > 0)
21317 {
21318 it->ascent += face->box_line_width;
21319 it->descent += face->box_line_width;
21320 }
21321 if (!NILP (height)
21322 && XINT (height) > it->ascent + it->descent)
21323 it->ascent = XINT (height) - it->descent;
21324
21325 if (!NILP (total_height))
21326 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21327 else
21328 {
21329 spacing = get_it_property(it, Qline_spacing);
21330 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21331 }
21332 if (INTEGERP (spacing))
21333 {
21334 extra_line_spacing = XINT (spacing);
21335 if (!NILP (total_height))
21336 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21337 }
21338 }
21339 }
21340 else if (it->char_to_display == '\t')
21341 {
21342 int tab_width = it->tab_width * font->space_width;
21343 int x = it->current_x + it->continuation_lines_width;
21344 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21345
21346 /* If the distance from the current position to the next tab
21347 stop is less than a space character width, use the
21348 tab stop after that. */
21349 if (next_tab_x - x < font->space_width)
21350 next_tab_x += tab_width;
21351
21352 it->pixel_width = next_tab_x - x;
21353 it->nglyphs = 1;
21354 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21355 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21356
21357 if (it->glyph_row)
21358 {
21359 append_stretch_glyph (it, it->object, it->pixel_width,
21360 it->ascent + it->descent, it->ascent);
21361 }
21362 }
21363 else
21364 {
21365 /* A multi-byte character. Assume that the display width of the
21366 character is the width of the character multiplied by the
21367 width of the font. */
21368
21369 /* If we found a font, this font should give us the right
21370 metrics. If we didn't find a font, use the frame's
21371 default font and calculate the width of the character by
21372 multiplying the width of font by the width of the
21373 character. */
21374
21375 pcm = get_per_char_metric (it->f, font, &char2b);
21376
21377 if (font_not_found_p || !pcm)
21378 {
21379 int char_width = CHAR_WIDTH (it->char_to_display);
21380
21381 if (char_width == 0)
21382 /* This is a non spacing character. But, as we are
21383 going to display an empty box, the box must occupy
21384 at least one column. */
21385 char_width = 1;
21386 it->glyph_not_available_p = 1;
21387 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21388 it->phys_ascent = FONT_BASE (font) + boff;
21389 it->phys_descent = FONT_DESCENT (font) - boff;
21390 }
21391 else
21392 {
21393 it->pixel_width = pcm->width;
21394 it->phys_ascent = pcm->ascent + boff;
21395 it->phys_descent = pcm->descent - boff;
21396 if (it->glyph_row
21397 && (pcm->lbearing < 0
21398 || pcm->rbearing > pcm->width))
21399 it->glyph_row->contains_overlapping_glyphs_p = 1;
21400 }
21401 it->nglyphs = 1;
21402 it->ascent = FONT_BASE (font) + boff;
21403 it->descent = FONT_DESCENT (font) - boff;
21404 if (face->box != FACE_NO_BOX)
21405 {
21406 int thick = face->box_line_width;
21407
21408 if (thick > 0)
21409 {
21410 it->ascent += thick;
21411 it->descent += thick;
21412 }
21413 else
21414 thick = - thick;
21415
21416 if (it->start_of_box_run_p)
21417 it->pixel_width += thick;
21418 if (it->end_of_box_run_p)
21419 it->pixel_width += thick;
21420 }
21421
21422 /* If face has an overline, add the height of the overline
21423 (1 pixel) and a 1 pixel margin to the character height. */
21424 if (face->overline_p)
21425 it->ascent += overline_margin;
21426
21427 take_vertical_position_into_account (it);
21428
21429 if (it->ascent < 0)
21430 it->ascent = 0;
21431 if (it->descent < 0)
21432 it->descent = 0;
21433
21434 if (it->glyph_row)
21435 append_glyph (it);
21436 if (it->pixel_width == 0)
21437 /* We assure that all visible glyphs have at least 1-pixel
21438 width. */
21439 it->pixel_width = 1;
21440 }
21441 it->multibyte_p = saved_multibyte_p;
21442 }
21443 else if (it->what == IT_COMPOSITION)
21444 {
21445 /* Note: A composition is represented as one glyph in the
21446 glyph matrix. There are no padding glyphs.
21447
21448 Important is that pixel_width, ascent, and descent are the
21449 values of what is drawn by draw_glyphs (i.e. the values of
21450 the overall glyphs composed). */
21451 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21452 int boff; /* baseline offset */
21453 struct composition *cmp = composition_table[it->cmp_id];
21454 int glyph_len = cmp->glyph_len;
21455 struct font *font = face->font;
21456
21457 it->nglyphs = 1;
21458
21459 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21460 {
21461 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21462 font_prepare_composition (cmp, it->f);
21463 }
21464 else
21465 /* If we have not yet calculated pixel size data of glyphs of
21466 the composition for the current face font, calculate them
21467 now. Theoretically, we have to check all fonts for the
21468 glyphs, but that requires much time and memory space. So,
21469 here we check only the font of the first glyph. This leads
21470 to incorrect display, but it's very rare, and C-l (recenter)
21471 can correct the display anyway. */
21472 if (! cmp->font || cmp->font != font)
21473 {
21474 /* Ascent and descent of the font of the first character
21475 of this composition (adjusted by baseline offset).
21476 Ascent and descent of overall glyphs should not be less
21477 than them respectively. */
21478 int font_ascent, font_descent, font_height;
21479 /* Bounding box of the overall glyphs. */
21480 int leftmost, rightmost, lowest, highest;
21481 int lbearing, rbearing;
21482 int i, width, ascent, descent;
21483 int left_padded = 0, right_padded = 0;
21484 int face_id;
21485 int c;
21486 XChar2b char2b;
21487 struct font_metrics *pcm;
21488 int font_not_found_p;
21489 int pos;
21490
21491 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21492 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21493 break;
21494 if (glyph_len < cmp->glyph_len)
21495 right_padded = 1;
21496 for (i = 0; i < glyph_len; i++)
21497 {
21498 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21499 break;
21500 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21501 }
21502 if (i > 0)
21503 left_padded = 1;
21504
21505 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21506 : IT_CHARPOS (*it));
21507 /* When no suitable font found, use the default font. */
21508 font_not_found_p = font == NULL;
21509 if (font_not_found_p)
21510 {
21511 face = face->ascii_face;
21512 font = face->font;
21513 }
21514 boff = font->baseline_offset;
21515 if (font->vertical_centering)
21516 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21517 font_ascent = FONT_BASE (font) + boff;
21518 font_descent = FONT_DESCENT (font) - boff;
21519 font_height = FONT_HEIGHT (font);
21520
21521 cmp->font = (void *) font;
21522
21523 pcm = NULL;
21524 if (! font_not_found_p)
21525 {
21526 get_char_face_and_encoding (it->f, c, it->face_id,
21527 &char2b, it->multibyte_p, 0);
21528 pcm = get_per_char_metric (it->f, font, &char2b);
21529 }
21530
21531 /* Initialize the bounding box. */
21532 if (pcm)
21533 {
21534 width = pcm->width;
21535 ascent = pcm->ascent;
21536 descent = pcm->descent;
21537 lbearing = pcm->lbearing;
21538 rbearing = pcm->rbearing;
21539 }
21540 else
21541 {
21542 width = FONT_WIDTH (font);
21543 ascent = FONT_BASE (font);
21544 descent = FONT_DESCENT (font);
21545 lbearing = 0;
21546 rbearing = width;
21547 }
21548
21549 rightmost = width;
21550 leftmost = 0;
21551 lowest = - descent + boff;
21552 highest = ascent + boff;
21553
21554 if (! font_not_found_p
21555 && font->default_ascent
21556 && CHAR_TABLE_P (Vuse_default_ascent)
21557 && !NILP (Faref (Vuse_default_ascent,
21558 make_number (it->char_to_display))))
21559 highest = font->default_ascent + boff;
21560
21561 /* Draw the first glyph at the normal position. It may be
21562 shifted to right later if some other glyphs are drawn
21563 at the left. */
21564 cmp->offsets[i * 2] = 0;
21565 cmp->offsets[i * 2 + 1] = boff;
21566 cmp->lbearing = lbearing;
21567 cmp->rbearing = rbearing;
21568
21569 /* Set cmp->offsets for the remaining glyphs. */
21570 for (i++; i < glyph_len; i++)
21571 {
21572 int left, right, btm, top;
21573 int ch = COMPOSITION_GLYPH (cmp, i);
21574 int face_id;
21575 struct face *this_face;
21576 int this_boff;
21577
21578 if (ch == '\t')
21579 ch = ' ';
21580 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21581 this_face = FACE_FROM_ID (it->f, face_id);
21582 font = this_face->font;
21583
21584 if (font == NULL)
21585 pcm = NULL;
21586 else
21587 {
21588 this_boff = font->baseline_offset;
21589 if (font->vertical_centering)
21590 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21591 get_char_face_and_encoding (it->f, ch, face_id,
21592 &char2b, it->multibyte_p, 0);
21593 pcm = get_per_char_metric (it->f, font, &char2b);
21594 }
21595 if (! pcm)
21596 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21597 else
21598 {
21599 width = pcm->width;
21600 ascent = pcm->ascent;
21601 descent = pcm->descent;
21602 lbearing = pcm->lbearing;
21603 rbearing = pcm->rbearing;
21604 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21605 {
21606 /* Relative composition with or without
21607 alternate chars. */
21608 left = (leftmost + rightmost - width) / 2;
21609 btm = - descent + boff;
21610 if (font->relative_compose
21611 && (! CHAR_TABLE_P (Vignore_relative_composition)
21612 || NILP (Faref (Vignore_relative_composition,
21613 make_number (ch)))))
21614 {
21615
21616 if (- descent >= font->relative_compose)
21617 /* One extra pixel between two glyphs. */
21618 btm = highest + 1;
21619 else if (ascent <= 0)
21620 /* One extra pixel between two glyphs. */
21621 btm = lowest - 1 - ascent - descent;
21622 }
21623 }
21624 else
21625 {
21626 /* A composition rule is specified by an integer
21627 value that encodes global and new reference
21628 points (GREF and NREF). GREF and NREF are
21629 specified by numbers as below:
21630
21631 0---1---2 -- ascent
21632 | |
21633 | |
21634 | |
21635 9--10--11 -- center
21636 | |
21637 ---3---4---5--- baseline
21638 | |
21639 6---7---8 -- descent
21640 */
21641 int rule = COMPOSITION_RULE (cmp, i);
21642 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21643
21644 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21645 grefx = gref % 3, nrefx = nref % 3;
21646 grefy = gref / 3, nrefy = nref / 3;
21647 if (xoff)
21648 xoff = font_height * (xoff - 128) / 256;
21649 if (yoff)
21650 yoff = font_height * (yoff - 128) / 256;
21651
21652 left = (leftmost
21653 + grefx * (rightmost - leftmost) / 2
21654 - nrefx * width / 2
21655 + xoff);
21656
21657 btm = ((grefy == 0 ? highest
21658 : grefy == 1 ? 0
21659 : grefy == 2 ? lowest
21660 : (highest + lowest) / 2)
21661 - (nrefy == 0 ? ascent + descent
21662 : nrefy == 1 ? descent - boff
21663 : nrefy == 2 ? 0
21664 : (ascent + descent) / 2)
21665 + yoff);
21666 }
21667
21668 cmp->offsets[i * 2] = left;
21669 cmp->offsets[i * 2 + 1] = btm + descent;
21670
21671 /* Update the bounding box of the overall glyphs. */
21672 if (width > 0)
21673 {
21674 right = left + width;
21675 if (left < leftmost)
21676 leftmost = left;
21677 if (right > rightmost)
21678 rightmost = right;
21679 }
21680 top = btm + descent + ascent;
21681 if (top > highest)
21682 highest = top;
21683 if (btm < lowest)
21684 lowest = btm;
21685
21686 if (cmp->lbearing > left + lbearing)
21687 cmp->lbearing = left + lbearing;
21688 if (cmp->rbearing < left + rbearing)
21689 cmp->rbearing = left + rbearing;
21690 }
21691 }
21692
21693 /* If there are glyphs whose x-offsets are negative,
21694 shift all glyphs to the right and make all x-offsets
21695 non-negative. */
21696 if (leftmost < 0)
21697 {
21698 for (i = 0; i < cmp->glyph_len; i++)
21699 cmp->offsets[i * 2] -= leftmost;
21700 rightmost -= leftmost;
21701 cmp->lbearing -= leftmost;
21702 cmp->rbearing -= leftmost;
21703 }
21704
21705 if (left_padded && cmp->lbearing < 0)
21706 {
21707 for (i = 0; i < cmp->glyph_len; i++)
21708 cmp->offsets[i * 2] -= cmp->lbearing;
21709 rightmost -= cmp->lbearing;
21710 cmp->rbearing -= cmp->lbearing;
21711 cmp->lbearing = 0;
21712 }
21713 if (right_padded && rightmost < cmp->rbearing)
21714 {
21715 rightmost = cmp->rbearing;
21716 }
21717
21718 cmp->pixel_width = rightmost;
21719 cmp->ascent = highest;
21720 cmp->descent = - lowest;
21721 if (cmp->ascent < font_ascent)
21722 cmp->ascent = font_ascent;
21723 if (cmp->descent < font_descent)
21724 cmp->descent = font_descent;
21725 }
21726
21727 if (it->glyph_row
21728 && (cmp->lbearing < 0
21729 || cmp->rbearing > cmp->pixel_width))
21730 it->glyph_row->contains_overlapping_glyphs_p = 1;
21731
21732 it->pixel_width = cmp->pixel_width;
21733 it->ascent = it->phys_ascent = cmp->ascent;
21734 it->descent = it->phys_descent = cmp->descent;
21735 if (face->box != FACE_NO_BOX)
21736 {
21737 int thick = face->box_line_width;
21738
21739 if (thick > 0)
21740 {
21741 it->ascent += thick;
21742 it->descent += thick;
21743 }
21744 else
21745 thick = - thick;
21746
21747 if (it->start_of_box_run_p)
21748 it->pixel_width += thick;
21749 if (it->end_of_box_run_p)
21750 it->pixel_width += thick;
21751 }
21752
21753 /* If face has an overline, add the height of the overline
21754 (1 pixel) and a 1 pixel margin to the character height. */
21755 if (face->overline_p)
21756 it->ascent += overline_margin;
21757
21758 take_vertical_position_into_account (it);
21759 if (it->ascent < 0)
21760 it->ascent = 0;
21761 if (it->descent < 0)
21762 it->descent = 0;
21763
21764 if (it->glyph_row)
21765 append_composite_glyph (it);
21766 }
21767 else if (it->what == IT_IMAGE)
21768 produce_image_glyph (it);
21769 else if (it->what == IT_STRETCH)
21770 produce_stretch_glyph (it);
21771
21772 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21773 because this isn't true for images with `:ascent 100'. */
21774 xassert (it->ascent >= 0 && it->descent >= 0);
21775 if (it->area == TEXT_AREA)
21776 it->current_x += it->pixel_width;
21777
21778 if (extra_line_spacing > 0)
21779 {
21780 it->descent += extra_line_spacing;
21781 if (extra_line_spacing > it->max_extra_line_spacing)
21782 it->max_extra_line_spacing = extra_line_spacing;
21783 }
21784
21785 it->max_ascent = max (it->max_ascent, it->ascent);
21786 it->max_descent = max (it->max_descent, it->descent);
21787 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21788 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21789 }
21790
21791 /* EXPORT for RIF:
21792 Output LEN glyphs starting at START at the nominal cursor position.
21793 Advance the nominal cursor over the text. The global variable
21794 updated_window contains the window being updated, updated_row is
21795 the glyph row being updated, and updated_area is the area of that
21796 row being updated. */
21797
21798 void
21799 x_write_glyphs (start, len)
21800 struct glyph *start;
21801 int len;
21802 {
21803 int x, hpos;
21804
21805 xassert (updated_window && updated_row);
21806 BLOCK_INPUT;
21807
21808 /* Write glyphs. */
21809
21810 hpos = start - updated_row->glyphs[updated_area];
21811 x = draw_glyphs (updated_window, output_cursor.x,
21812 updated_row, updated_area,
21813 hpos, hpos + len,
21814 DRAW_NORMAL_TEXT, 0);
21815
21816 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21817 if (updated_area == TEXT_AREA
21818 && updated_window->phys_cursor_on_p
21819 && updated_window->phys_cursor.vpos == output_cursor.vpos
21820 && updated_window->phys_cursor.hpos >= hpos
21821 && updated_window->phys_cursor.hpos < hpos + len)
21822 updated_window->phys_cursor_on_p = 0;
21823
21824 UNBLOCK_INPUT;
21825
21826 /* Advance the output cursor. */
21827 output_cursor.hpos += len;
21828 output_cursor.x = x;
21829 }
21830
21831
21832 /* EXPORT for RIF:
21833 Insert LEN glyphs from START at the nominal cursor position. */
21834
21835 void
21836 x_insert_glyphs (start, len)
21837 struct glyph *start;
21838 int len;
21839 {
21840 struct frame *f;
21841 struct window *w;
21842 int line_height, shift_by_width, shifted_region_width;
21843 struct glyph_row *row;
21844 struct glyph *glyph;
21845 int frame_x, frame_y;
21846 EMACS_INT hpos;
21847
21848 xassert (updated_window && updated_row);
21849 BLOCK_INPUT;
21850 w = updated_window;
21851 f = XFRAME (WINDOW_FRAME (w));
21852
21853 /* Get the height of the line we are in. */
21854 row = updated_row;
21855 line_height = row->height;
21856
21857 /* Get the width of the glyphs to insert. */
21858 shift_by_width = 0;
21859 for (glyph = start; glyph < start + len; ++glyph)
21860 shift_by_width += glyph->pixel_width;
21861
21862 /* Get the width of the region to shift right. */
21863 shifted_region_width = (window_box_width (w, updated_area)
21864 - output_cursor.x
21865 - shift_by_width);
21866
21867 /* Shift right. */
21868 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21869 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21870
21871 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21872 line_height, shift_by_width);
21873
21874 /* Write the glyphs. */
21875 hpos = start - row->glyphs[updated_area];
21876 draw_glyphs (w, output_cursor.x, row, updated_area,
21877 hpos, hpos + len,
21878 DRAW_NORMAL_TEXT, 0);
21879
21880 /* Advance the output cursor. */
21881 output_cursor.hpos += len;
21882 output_cursor.x += shift_by_width;
21883 UNBLOCK_INPUT;
21884 }
21885
21886
21887 /* EXPORT for RIF:
21888 Erase the current text line from the nominal cursor position
21889 (inclusive) to pixel column TO_X (exclusive). The idea is that
21890 everything from TO_X onward is already erased.
21891
21892 TO_X is a pixel position relative to updated_area of
21893 updated_window. TO_X == -1 means clear to the end of this area. */
21894
21895 void
21896 x_clear_end_of_line (to_x)
21897 int to_x;
21898 {
21899 struct frame *f;
21900 struct window *w = updated_window;
21901 int max_x, min_y, max_y;
21902 int from_x, from_y, to_y;
21903
21904 xassert (updated_window && updated_row);
21905 f = XFRAME (w->frame);
21906
21907 if (updated_row->full_width_p)
21908 max_x = WINDOW_TOTAL_WIDTH (w);
21909 else
21910 max_x = window_box_width (w, updated_area);
21911 max_y = window_text_bottom_y (w);
21912
21913 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21914 of window. For TO_X > 0, truncate to end of drawing area. */
21915 if (to_x == 0)
21916 return;
21917 else if (to_x < 0)
21918 to_x = max_x;
21919 else
21920 to_x = min (to_x, max_x);
21921
21922 to_y = min (max_y, output_cursor.y + updated_row->height);
21923
21924 /* Notice if the cursor will be cleared by this operation. */
21925 if (!updated_row->full_width_p)
21926 notice_overwritten_cursor (w, updated_area,
21927 output_cursor.x, -1,
21928 updated_row->y,
21929 MATRIX_ROW_BOTTOM_Y (updated_row));
21930
21931 from_x = output_cursor.x;
21932
21933 /* Translate to frame coordinates. */
21934 if (updated_row->full_width_p)
21935 {
21936 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21937 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21938 }
21939 else
21940 {
21941 int area_left = window_box_left (w, updated_area);
21942 from_x += area_left;
21943 to_x += area_left;
21944 }
21945
21946 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21947 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21948 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21949
21950 /* Prevent inadvertently clearing to end of the X window. */
21951 if (to_x > from_x && to_y > from_y)
21952 {
21953 BLOCK_INPUT;
21954 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21955 to_x - from_x, to_y - from_y);
21956 UNBLOCK_INPUT;
21957 }
21958 }
21959
21960 #endif /* HAVE_WINDOW_SYSTEM */
21961
21962
21963 \f
21964 /***********************************************************************
21965 Cursor types
21966 ***********************************************************************/
21967
21968 /* Value is the internal representation of the specified cursor type
21969 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21970 of the bar cursor. */
21971
21972 static enum text_cursor_kinds
21973 get_specified_cursor_type (arg, width)
21974 Lisp_Object arg;
21975 int *width;
21976 {
21977 enum text_cursor_kinds type;
21978
21979 if (NILP (arg))
21980 return NO_CURSOR;
21981
21982 if (EQ (arg, Qbox))
21983 return FILLED_BOX_CURSOR;
21984
21985 if (EQ (arg, Qhollow))
21986 return HOLLOW_BOX_CURSOR;
21987
21988 if (EQ (arg, Qbar))
21989 {
21990 *width = 2;
21991 return BAR_CURSOR;
21992 }
21993
21994 if (CONSP (arg)
21995 && EQ (XCAR (arg), Qbar)
21996 && INTEGERP (XCDR (arg))
21997 && XINT (XCDR (arg)) >= 0)
21998 {
21999 *width = XINT (XCDR (arg));
22000 return BAR_CURSOR;
22001 }
22002
22003 if (EQ (arg, Qhbar))
22004 {
22005 *width = 2;
22006 return HBAR_CURSOR;
22007 }
22008
22009 if (CONSP (arg)
22010 && EQ (XCAR (arg), Qhbar)
22011 && INTEGERP (XCDR (arg))
22012 && XINT (XCDR (arg)) >= 0)
22013 {
22014 *width = XINT (XCDR (arg));
22015 return HBAR_CURSOR;
22016 }
22017
22018 /* Treat anything unknown as "hollow box cursor".
22019 It was bad to signal an error; people have trouble fixing
22020 .Xdefaults with Emacs, when it has something bad in it. */
22021 type = HOLLOW_BOX_CURSOR;
22022
22023 return type;
22024 }
22025
22026 /* Set the default cursor types for specified frame. */
22027 void
22028 set_frame_cursor_types (f, arg)
22029 struct frame *f;
22030 Lisp_Object arg;
22031 {
22032 int width;
22033 Lisp_Object tem;
22034
22035 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22036 FRAME_CURSOR_WIDTH (f) = width;
22037
22038 /* By default, set up the blink-off state depending on the on-state. */
22039
22040 tem = Fassoc (arg, Vblink_cursor_alist);
22041 if (!NILP (tem))
22042 {
22043 FRAME_BLINK_OFF_CURSOR (f)
22044 = get_specified_cursor_type (XCDR (tem), &width);
22045 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22046 }
22047 else
22048 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22049 }
22050
22051
22052 /* Return the cursor we want to be displayed in window W. Return
22053 width of bar/hbar cursor through WIDTH arg. Return with
22054 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22055 (i.e. if the `system caret' should track this cursor).
22056
22057 In a mini-buffer window, we want the cursor only to appear if we
22058 are reading input from this window. For the selected window, we
22059 want the cursor type given by the frame parameter or buffer local
22060 setting of cursor-type. If explicitly marked off, draw no cursor.
22061 In all other cases, we want a hollow box cursor. */
22062
22063 static enum text_cursor_kinds
22064 get_window_cursor_type (w, glyph, width, active_cursor)
22065 struct window *w;
22066 struct glyph *glyph;
22067 int *width;
22068 int *active_cursor;
22069 {
22070 struct frame *f = XFRAME (w->frame);
22071 struct buffer *b = XBUFFER (w->buffer);
22072 int cursor_type = DEFAULT_CURSOR;
22073 Lisp_Object alt_cursor;
22074 int non_selected = 0;
22075
22076 *active_cursor = 1;
22077
22078 /* Echo area */
22079 if (cursor_in_echo_area
22080 && FRAME_HAS_MINIBUF_P (f)
22081 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22082 {
22083 if (w == XWINDOW (echo_area_window))
22084 {
22085 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22086 {
22087 *width = FRAME_CURSOR_WIDTH (f);
22088 return FRAME_DESIRED_CURSOR (f);
22089 }
22090 else
22091 return get_specified_cursor_type (b->cursor_type, width);
22092 }
22093
22094 *active_cursor = 0;
22095 non_selected = 1;
22096 }
22097
22098 /* Detect a nonselected window or nonselected frame. */
22099 else if (w != XWINDOW (f->selected_window)
22100 #ifdef HAVE_WINDOW_SYSTEM
22101 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22102 #endif
22103 )
22104 {
22105 *active_cursor = 0;
22106
22107 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22108 return NO_CURSOR;
22109
22110 non_selected = 1;
22111 }
22112
22113 /* Never display a cursor in a window in which cursor-type is nil. */
22114 if (NILP (b->cursor_type))
22115 return NO_CURSOR;
22116
22117 /* Get the normal cursor type for this window. */
22118 if (EQ (b->cursor_type, Qt))
22119 {
22120 cursor_type = FRAME_DESIRED_CURSOR (f);
22121 *width = FRAME_CURSOR_WIDTH (f);
22122 }
22123 else
22124 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22125
22126 /* Use cursor-in-non-selected-windows instead
22127 for non-selected window or frame. */
22128 if (non_selected)
22129 {
22130 alt_cursor = b->cursor_in_non_selected_windows;
22131 if (!EQ (Qt, alt_cursor))
22132 return get_specified_cursor_type (alt_cursor, width);
22133 /* t means modify the normal cursor type. */
22134 if (cursor_type == FILLED_BOX_CURSOR)
22135 cursor_type = HOLLOW_BOX_CURSOR;
22136 else if (cursor_type == BAR_CURSOR && *width > 1)
22137 --*width;
22138 return cursor_type;
22139 }
22140
22141 /* Use normal cursor if not blinked off. */
22142 if (!w->cursor_off_p)
22143 {
22144 #ifdef HAVE_WINDOW_SYSTEM
22145 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22146 {
22147 if (cursor_type == FILLED_BOX_CURSOR)
22148 {
22149 /* Using a block cursor on large images can be very annoying.
22150 So use a hollow cursor for "large" images.
22151 If image is not transparent (no mask), also use hollow cursor. */
22152 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22153 if (img != NULL && IMAGEP (img->spec))
22154 {
22155 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22156 where N = size of default frame font size.
22157 This should cover most of the "tiny" icons people may use. */
22158 if (!img->mask
22159 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22160 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22161 cursor_type = HOLLOW_BOX_CURSOR;
22162 }
22163 }
22164 else if (cursor_type != NO_CURSOR)
22165 {
22166 /* Display current only supports BOX and HOLLOW cursors for images.
22167 So for now, unconditionally use a HOLLOW cursor when cursor is
22168 not a solid box cursor. */
22169 cursor_type = HOLLOW_BOX_CURSOR;
22170 }
22171 }
22172 #endif
22173 return cursor_type;
22174 }
22175
22176 /* Cursor is blinked off, so determine how to "toggle" it. */
22177
22178 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22179 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22180 return get_specified_cursor_type (XCDR (alt_cursor), width);
22181
22182 /* Then see if frame has specified a specific blink off cursor type. */
22183 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22184 {
22185 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22186 return FRAME_BLINK_OFF_CURSOR (f);
22187 }
22188
22189 #if 0
22190 /* Some people liked having a permanently visible blinking cursor,
22191 while others had very strong opinions against it. So it was
22192 decided to remove it. KFS 2003-09-03 */
22193
22194 /* Finally perform built-in cursor blinking:
22195 filled box <-> hollow box
22196 wide [h]bar <-> narrow [h]bar
22197 narrow [h]bar <-> no cursor
22198 other type <-> no cursor */
22199
22200 if (cursor_type == FILLED_BOX_CURSOR)
22201 return HOLLOW_BOX_CURSOR;
22202
22203 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22204 {
22205 *width = 1;
22206 return cursor_type;
22207 }
22208 #endif
22209
22210 return NO_CURSOR;
22211 }
22212
22213
22214 #ifdef HAVE_WINDOW_SYSTEM
22215
22216 /* Notice when the text cursor of window W has been completely
22217 overwritten by a drawing operation that outputs glyphs in AREA
22218 starting at X0 and ending at X1 in the line starting at Y0 and
22219 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22220 the rest of the line after X0 has been written. Y coordinates
22221 are window-relative. */
22222
22223 static void
22224 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22225 struct window *w;
22226 enum glyph_row_area area;
22227 int x0, y0, x1, y1;
22228 {
22229 int cx0, cx1, cy0, cy1;
22230 struct glyph_row *row;
22231
22232 if (!w->phys_cursor_on_p)
22233 return;
22234 if (area != TEXT_AREA)
22235 return;
22236
22237 if (w->phys_cursor.vpos < 0
22238 || w->phys_cursor.vpos >= w->current_matrix->nrows
22239 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22240 !(row->enabled_p && row->displays_text_p)))
22241 return;
22242
22243 if (row->cursor_in_fringe_p)
22244 {
22245 row->cursor_in_fringe_p = 0;
22246 draw_fringe_bitmap (w, row, 0);
22247 w->phys_cursor_on_p = 0;
22248 return;
22249 }
22250
22251 cx0 = w->phys_cursor.x;
22252 cx1 = cx0 + w->phys_cursor_width;
22253 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22254 return;
22255
22256 /* The cursor image will be completely removed from the
22257 screen if the output area intersects the cursor area in
22258 y-direction. When we draw in [y0 y1[, and some part of
22259 the cursor is at y < y0, that part must have been drawn
22260 before. When scrolling, the cursor is erased before
22261 actually scrolling, so we don't come here. When not
22262 scrolling, the rows above the old cursor row must have
22263 changed, and in this case these rows must have written
22264 over the cursor image.
22265
22266 Likewise if part of the cursor is below y1, with the
22267 exception of the cursor being in the first blank row at
22268 the buffer and window end because update_text_area
22269 doesn't draw that row. (Except when it does, but
22270 that's handled in update_text_area.) */
22271
22272 cy0 = w->phys_cursor.y;
22273 cy1 = cy0 + w->phys_cursor_height;
22274 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22275 return;
22276
22277 w->phys_cursor_on_p = 0;
22278 }
22279
22280 #endif /* HAVE_WINDOW_SYSTEM */
22281
22282 \f
22283 /************************************************************************
22284 Mouse Face
22285 ************************************************************************/
22286
22287 #ifdef HAVE_WINDOW_SYSTEM
22288
22289 /* EXPORT for RIF:
22290 Fix the display of area AREA of overlapping row ROW in window W
22291 with respect to the overlapping part OVERLAPS. */
22292
22293 void
22294 x_fix_overlapping_area (w, row, area, overlaps)
22295 struct window *w;
22296 struct glyph_row *row;
22297 enum glyph_row_area area;
22298 int overlaps;
22299 {
22300 int i, x;
22301
22302 BLOCK_INPUT;
22303
22304 x = 0;
22305 for (i = 0; i < row->used[area];)
22306 {
22307 if (row->glyphs[area][i].overlaps_vertically_p)
22308 {
22309 int start = i, start_x = x;
22310
22311 do
22312 {
22313 x += row->glyphs[area][i].pixel_width;
22314 ++i;
22315 }
22316 while (i < row->used[area]
22317 && row->glyphs[area][i].overlaps_vertically_p);
22318
22319 draw_glyphs (w, start_x, row, area,
22320 start, i,
22321 DRAW_NORMAL_TEXT, overlaps);
22322 }
22323 else
22324 {
22325 x += row->glyphs[area][i].pixel_width;
22326 ++i;
22327 }
22328 }
22329
22330 UNBLOCK_INPUT;
22331 }
22332
22333
22334 /* EXPORT:
22335 Draw the cursor glyph of window W in glyph row ROW. See the
22336 comment of draw_glyphs for the meaning of HL. */
22337
22338 void
22339 draw_phys_cursor_glyph (w, row, hl)
22340 struct window *w;
22341 struct glyph_row *row;
22342 enum draw_glyphs_face hl;
22343 {
22344 /* If cursor hpos is out of bounds, don't draw garbage. This can
22345 happen in mini-buffer windows when switching between echo area
22346 glyphs and mini-buffer. */
22347 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22348 {
22349 int on_p = w->phys_cursor_on_p;
22350 int x1;
22351 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22352 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22353 hl, 0);
22354 w->phys_cursor_on_p = on_p;
22355
22356 if (hl == DRAW_CURSOR)
22357 w->phys_cursor_width = x1 - w->phys_cursor.x;
22358 /* When we erase the cursor, and ROW is overlapped by other
22359 rows, make sure that these overlapping parts of other rows
22360 are redrawn. */
22361 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22362 {
22363 w->phys_cursor_width = x1 - w->phys_cursor.x;
22364
22365 if (row > w->current_matrix->rows
22366 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22367 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22368 OVERLAPS_ERASED_CURSOR);
22369
22370 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22371 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22372 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22373 OVERLAPS_ERASED_CURSOR);
22374 }
22375 }
22376 }
22377
22378
22379 /* EXPORT:
22380 Erase the image of a cursor of window W from the screen. */
22381
22382 void
22383 erase_phys_cursor (w)
22384 struct window *w;
22385 {
22386 struct frame *f = XFRAME (w->frame);
22387 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22388 int hpos = w->phys_cursor.hpos;
22389 int vpos = w->phys_cursor.vpos;
22390 int mouse_face_here_p = 0;
22391 struct glyph_matrix *active_glyphs = w->current_matrix;
22392 struct glyph_row *cursor_row;
22393 struct glyph *cursor_glyph;
22394 enum draw_glyphs_face hl;
22395
22396 /* No cursor displayed or row invalidated => nothing to do on the
22397 screen. */
22398 if (w->phys_cursor_type == NO_CURSOR)
22399 goto mark_cursor_off;
22400
22401 /* VPOS >= active_glyphs->nrows means that window has been resized.
22402 Don't bother to erase the cursor. */
22403 if (vpos >= active_glyphs->nrows)
22404 goto mark_cursor_off;
22405
22406 /* If row containing cursor is marked invalid, there is nothing we
22407 can do. */
22408 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22409 if (!cursor_row->enabled_p)
22410 goto mark_cursor_off;
22411
22412 /* If line spacing is > 0, old cursor may only be partially visible in
22413 window after split-window. So adjust visible height. */
22414 cursor_row->visible_height = min (cursor_row->visible_height,
22415 window_text_bottom_y (w) - cursor_row->y);
22416
22417 /* If row is completely invisible, don't attempt to delete a cursor which
22418 isn't there. This can happen if cursor is at top of a window, and
22419 we switch to a buffer with a header line in that window. */
22420 if (cursor_row->visible_height <= 0)
22421 goto mark_cursor_off;
22422
22423 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22424 if (cursor_row->cursor_in_fringe_p)
22425 {
22426 cursor_row->cursor_in_fringe_p = 0;
22427 draw_fringe_bitmap (w, cursor_row, 0);
22428 goto mark_cursor_off;
22429 }
22430
22431 /* This can happen when the new row is shorter than the old one.
22432 In this case, either draw_glyphs or clear_end_of_line
22433 should have cleared the cursor. Note that we wouldn't be
22434 able to erase the cursor in this case because we don't have a
22435 cursor glyph at hand. */
22436 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22437 goto mark_cursor_off;
22438
22439 /* If the cursor is in the mouse face area, redisplay that when
22440 we clear the cursor. */
22441 if (! NILP (dpyinfo->mouse_face_window)
22442 && w == XWINDOW (dpyinfo->mouse_face_window)
22443 && (vpos > dpyinfo->mouse_face_beg_row
22444 || (vpos == dpyinfo->mouse_face_beg_row
22445 && hpos >= dpyinfo->mouse_face_beg_col))
22446 && (vpos < dpyinfo->mouse_face_end_row
22447 || (vpos == dpyinfo->mouse_face_end_row
22448 && hpos < dpyinfo->mouse_face_end_col))
22449 /* Don't redraw the cursor's spot in mouse face if it is at the
22450 end of a line (on a newline). The cursor appears there, but
22451 mouse highlighting does not. */
22452 && cursor_row->used[TEXT_AREA] > hpos)
22453 mouse_face_here_p = 1;
22454
22455 /* Maybe clear the display under the cursor. */
22456 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22457 {
22458 int x, y, left_x;
22459 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22460 int width;
22461
22462 cursor_glyph = get_phys_cursor_glyph (w);
22463 if (cursor_glyph == NULL)
22464 goto mark_cursor_off;
22465
22466 width = cursor_glyph->pixel_width;
22467 left_x = window_box_left_offset (w, TEXT_AREA);
22468 x = w->phys_cursor.x;
22469 if (x < left_x)
22470 width -= left_x - x;
22471 width = min (width, window_box_width (w, TEXT_AREA) - x);
22472 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22473 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22474
22475 if (width > 0)
22476 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22477 }
22478
22479 /* Erase the cursor by redrawing the character underneath it. */
22480 if (mouse_face_here_p)
22481 hl = DRAW_MOUSE_FACE;
22482 else
22483 hl = DRAW_NORMAL_TEXT;
22484 draw_phys_cursor_glyph (w, cursor_row, hl);
22485
22486 mark_cursor_off:
22487 w->phys_cursor_on_p = 0;
22488 w->phys_cursor_type = NO_CURSOR;
22489 }
22490
22491
22492 /* EXPORT:
22493 Display or clear cursor of window W. If ON is zero, clear the
22494 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22495 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22496
22497 void
22498 display_and_set_cursor (w, on, hpos, vpos, x, y)
22499 struct window *w;
22500 int on, hpos, vpos, x, y;
22501 {
22502 struct frame *f = XFRAME (w->frame);
22503 int new_cursor_type;
22504 int new_cursor_width;
22505 int active_cursor;
22506 struct glyph_row *glyph_row;
22507 struct glyph *glyph;
22508
22509 /* This is pointless on invisible frames, and dangerous on garbaged
22510 windows and frames; in the latter case, the frame or window may
22511 be in the midst of changing its size, and x and y may be off the
22512 window. */
22513 if (! FRAME_VISIBLE_P (f)
22514 || FRAME_GARBAGED_P (f)
22515 || vpos >= w->current_matrix->nrows
22516 || hpos >= w->current_matrix->matrix_w)
22517 return;
22518
22519 /* If cursor is off and we want it off, return quickly. */
22520 if (!on && !w->phys_cursor_on_p)
22521 return;
22522
22523 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22524 /* If cursor row is not enabled, we don't really know where to
22525 display the cursor. */
22526 if (!glyph_row->enabled_p)
22527 {
22528 w->phys_cursor_on_p = 0;
22529 return;
22530 }
22531
22532 glyph = NULL;
22533 if (!glyph_row->exact_window_width_line_p
22534 || hpos < glyph_row->used[TEXT_AREA])
22535 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22536
22537 xassert (interrupt_input_blocked);
22538
22539 /* Set new_cursor_type to the cursor we want to be displayed. */
22540 new_cursor_type = get_window_cursor_type (w, glyph,
22541 &new_cursor_width, &active_cursor);
22542
22543 /* If cursor is currently being shown and we don't want it to be or
22544 it is in the wrong place, or the cursor type is not what we want,
22545 erase it. */
22546 if (w->phys_cursor_on_p
22547 && (!on
22548 || w->phys_cursor.x != x
22549 || w->phys_cursor.y != y
22550 || new_cursor_type != w->phys_cursor_type
22551 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22552 && new_cursor_width != w->phys_cursor_width)))
22553 erase_phys_cursor (w);
22554
22555 /* Don't check phys_cursor_on_p here because that flag is only set
22556 to zero in some cases where we know that the cursor has been
22557 completely erased, to avoid the extra work of erasing the cursor
22558 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22559 still not be visible, or it has only been partly erased. */
22560 if (on)
22561 {
22562 w->phys_cursor_ascent = glyph_row->ascent;
22563 w->phys_cursor_height = glyph_row->height;
22564
22565 /* Set phys_cursor_.* before x_draw_.* is called because some
22566 of them may need the information. */
22567 w->phys_cursor.x = x;
22568 w->phys_cursor.y = glyph_row->y;
22569 w->phys_cursor.hpos = hpos;
22570 w->phys_cursor.vpos = vpos;
22571 }
22572
22573 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22574 new_cursor_type, new_cursor_width,
22575 on, active_cursor);
22576 }
22577
22578
22579 /* Switch the display of W's cursor on or off, according to the value
22580 of ON. */
22581
22582 #ifndef HAVE_NS
22583 static
22584 #endif
22585 void
22586 update_window_cursor (w, on)
22587 struct window *w;
22588 int on;
22589 {
22590 /* Don't update cursor in windows whose frame is in the process
22591 of being deleted. */
22592 if (w->current_matrix)
22593 {
22594 BLOCK_INPUT;
22595 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22596 w->phys_cursor.x, w->phys_cursor.y);
22597 UNBLOCK_INPUT;
22598 }
22599 }
22600
22601
22602 /* Call update_window_cursor with parameter ON_P on all leaf windows
22603 in the window tree rooted at W. */
22604
22605 static void
22606 update_cursor_in_window_tree (w, on_p)
22607 struct window *w;
22608 int on_p;
22609 {
22610 while (w)
22611 {
22612 if (!NILP (w->hchild))
22613 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22614 else if (!NILP (w->vchild))
22615 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22616 else
22617 update_window_cursor (w, on_p);
22618
22619 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22620 }
22621 }
22622
22623
22624 /* EXPORT:
22625 Display the cursor on window W, or clear it, according to ON_P.
22626 Don't change the cursor's position. */
22627
22628 void
22629 x_update_cursor (f, on_p)
22630 struct frame *f;
22631 int on_p;
22632 {
22633 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22634 }
22635
22636
22637 /* EXPORT:
22638 Clear the cursor of window W to background color, and mark the
22639 cursor as not shown. This is used when the text where the cursor
22640 is is about to be rewritten. */
22641
22642 void
22643 x_clear_cursor (w)
22644 struct window *w;
22645 {
22646 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22647 update_window_cursor (w, 0);
22648 }
22649
22650
22651 /* EXPORT:
22652 Display the active region described by mouse_face_* according to DRAW. */
22653
22654 void
22655 show_mouse_face (dpyinfo, draw)
22656 Display_Info *dpyinfo;
22657 enum draw_glyphs_face draw;
22658 {
22659 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22660 struct frame *f = XFRAME (WINDOW_FRAME (w));
22661
22662 if (/* If window is in the process of being destroyed, don't bother
22663 to do anything. */
22664 w->current_matrix != NULL
22665 /* Don't update mouse highlight if hidden */
22666 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22667 /* Recognize when we are called to operate on rows that don't exist
22668 anymore. This can happen when a window is split. */
22669 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22670 {
22671 int phys_cursor_on_p = w->phys_cursor_on_p;
22672 struct glyph_row *row, *first, *last;
22673
22674 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22675 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22676
22677 for (row = first; row <= last && row->enabled_p; ++row)
22678 {
22679 int start_hpos, end_hpos, start_x;
22680
22681 /* For all but the first row, the highlight starts at column 0. */
22682 if (row == first)
22683 {
22684 start_hpos = dpyinfo->mouse_face_beg_col;
22685 start_x = dpyinfo->mouse_face_beg_x;
22686 }
22687 else
22688 {
22689 start_hpos = 0;
22690 start_x = 0;
22691 }
22692
22693 if (row == last)
22694 end_hpos = dpyinfo->mouse_face_end_col;
22695 else
22696 {
22697 end_hpos = row->used[TEXT_AREA];
22698 if (draw == DRAW_NORMAL_TEXT)
22699 row->fill_line_p = 1; /* Clear to end of line */
22700 }
22701
22702 if (end_hpos > start_hpos)
22703 {
22704 draw_glyphs (w, start_x, row, TEXT_AREA,
22705 start_hpos, end_hpos,
22706 draw, 0);
22707
22708 row->mouse_face_p
22709 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22710 }
22711 }
22712
22713 /* When we've written over the cursor, arrange for it to
22714 be displayed again. */
22715 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22716 {
22717 BLOCK_INPUT;
22718 display_and_set_cursor (w, 1,
22719 w->phys_cursor.hpos, w->phys_cursor.vpos,
22720 w->phys_cursor.x, w->phys_cursor.y);
22721 UNBLOCK_INPUT;
22722 }
22723 }
22724
22725 /* Change the mouse cursor. */
22726 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22727 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22728 else if (draw == DRAW_MOUSE_FACE)
22729 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22730 else
22731 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22732 }
22733
22734 /* EXPORT:
22735 Clear out the mouse-highlighted active region.
22736 Redraw it un-highlighted first. Value is non-zero if mouse
22737 face was actually drawn unhighlighted. */
22738
22739 int
22740 clear_mouse_face (dpyinfo)
22741 Display_Info *dpyinfo;
22742 {
22743 int cleared = 0;
22744
22745 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22746 {
22747 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22748 cleared = 1;
22749 }
22750
22751 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22752 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22753 dpyinfo->mouse_face_window = Qnil;
22754 dpyinfo->mouse_face_overlay = Qnil;
22755 return cleared;
22756 }
22757
22758
22759 /* EXPORT:
22760 Non-zero if physical cursor of window W is within mouse face. */
22761
22762 int
22763 cursor_in_mouse_face_p (w)
22764 struct window *w;
22765 {
22766 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22767 int in_mouse_face = 0;
22768
22769 if (WINDOWP (dpyinfo->mouse_face_window)
22770 && XWINDOW (dpyinfo->mouse_face_window) == w)
22771 {
22772 int hpos = w->phys_cursor.hpos;
22773 int vpos = w->phys_cursor.vpos;
22774
22775 if (vpos >= dpyinfo->mouse_face_beg_row
22776 && vpos <= dpyinfo->mouse_face_end_row
22777 && (vpos > dpyinfo->mouse_face_beg_row
22778 || hpos >= dpyinfo->mouse_face_beg_col)
22779 && (vpos < dpyinfo->mouse_face_end_row
22780 || hpos < dpyinfo->mouse_face_end_col
22781 || dpyinfo->mouse_face_past_end))
22782 in_mouse_face = 1;
22783 }
22784
22785 return in_mouse_face;
22786 }
22787
22788
22789
22790 \f
22791 /* Find the glyph matrix position of buffer position CHARPOS in window
22792 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22793 current glyphs must be up to date. If CHARPOS is above window
22794 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22795 of last line in W. In the row containing CHARPOS, stop before glyphs
22796 having STOP as object. */
22797
22798 #if 1 /* This is a version of fast_find_position that's more correct
22799 in the presence of hscrolling, for example. I didn't install
22800 it right away because the problem fixed is minor, it failed
22801 in 20.x as well, and I think it's too risky to install
22802 so near the release of 21.1. 2001-09-25 gerd. */
22803
22804 static
22805 int
22806 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22807 struct window *w;
22808 EMACS_INT charpos;
22809 int *hpos, *vpos, *x, *y;
22810 Lisp_Object stop;
22811 {
22812 struct glyph_row *row, *first;
22813 struct glyph *glyph, *end;
22814 int past_end = 0;
22815
22816 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22817 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22818 {
22819 *x = first->x;
22820 *y = first->y;
22821 *hpos = 0;
22822 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22823 return 1;
22824 }
22825
22826 row = row_containing_pos (w, charpos, first, NULL, 0);
22827 if (row == NULL)
22828 {
22829 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22830 past_end = 1;
22831 }
22832
22833 /* If whole rows or last part of a row came from a display overlay,
22834 row_containing_pos will skip over such rows because their end pos
22835 equals the start pos of the overlay or interval.
22836
22837 Move back if we have a STOP object and previous row's
22838 end glyph came from STOP. */
22839 if (!NILP (stop))
22840 {
22841 struct glyph_row *prev;
22842 while ((prev = row - 1, prev >= first)
22843 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22844 && prev->used[TEXT_AREA] > 0)
22845 {
22846 struct glyph *beg = prev->glyphs[TEXT_AREA];
22847 glyph = beg + prev->used[TEXT_AREA];
22848 while (--glyph >= beg
22849 && INTEGERP (glyph->object));
22850 if (glyph < beg
22851 || !EQ (stop, glyph->object))
22852 break;
22853 row = prev;
22854 }
22855 }
22856
22857 *x = row->x;
22858 *y = row->y;
22859 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22860
22861 glyph = row->glyphs[TEXT_AREA];
22862 end = glyph + row->used[TEXT_AREA];
22863
22864 /* Skip over glyphs not having an object at the start of the row.
22865 These are special glyphs like truncation marks on terminal
22866 frames. */
22867 if (row->displays_text_p)
22868 while (glyph < end
22869 && INTEGERP (glyph->object)
22870 && !EQ (stop, glyph->object)
22871 && glyph->charpos < 0)
22872 {
22873 *x += glyph->pixel_width;
22874 ++glyph;
22875 }
22876
22877 while (glyph < end
22878 && !INTEGERP (glyph->object)
22879 && !EQ (stop, glyph->object)
22880 && (!BUFFERP (glyph->object)
22881 || glyph->charpos < charpos))
22882 {
22883 *x += glyph->pixel_width;
22884 ++glyph;
22885 }
22886
22887 *hpos = glyph - row->glyphs[TEXT_AREA];
22888 return !past_end;
22889 }
22890
22891 #else /* not 1 */
22892
22893 static int
22894 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22895 struct window *w;
22896 EMACS_INT pos;
22897 int *hpos, *vpos, *x, *y;
22898 Lisp_Object stop;
22899 {
22900 int i;
22901 int lastcol;
22902 int maybe_next_line_p = 0;
22903 int line_start_position;
22904 int yb = window_text_bottom_y (w);
22905 struct glyph_row *row, *best_row;
22906 int row_vpos, best_row_vpos;
22907 int current_x;
22908
22909 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22910 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22911
22912 while (row->y < yb)
22913 {
22914 if (row->used[TEXT_AREA])
22915 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22916 else
22917 line_start_position = 0;
22918
22919 if (line_start_position > pos)
22920 break;
22921 /* If the position sought is the end of the buffer,
22922 don't include the blank lines at the bottom of the window. */
22923 else if (line_start_position == pos
22924 && pos == BUF_ZV (XBUFFER (w->buffer)))
22925 {
22926 maybe_next_line_p = 1;
22927 break;
22928 }
22929 else if (line_start_position > 0)
22930 {
22931 best_row = row;
22932 best_row_vpos = row_vpos;
22933 }
22934
22935 if (row->y + row->height >= yb)
22936 break;
22937
22938 ++row;
22939 ++row_vpos;
22940 }
22941
22942 /* Find the right column within BEST_ROW. */
22943 lastcol = 0;
22944 current_x = best_row->x;
22945 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22946 {
22947 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22948 int charpos = glyph->charpos;
22949
22950 if (BUFFERP (glyph->object))
22951 {
22952 if (charpos == pos)
22953 {
22954 *hpos = i;
22955 *vpos = best_row_vpos;
22956 *x = current_x;
22957 *y = best_row->y;
22958 return 1;
22959 }
22960 else if (charpos > pos)
22961 break;
22962 }
22963 else if (EQ (glyph->object, stop))
22964 break;
22965
22966 if (charpos > 0)
22967 lastcol = i;
22968 current_x += glyph->pixel_width;
22969 }
22970
22971 /* If we're looking for the end of the buffer,
22972 and we didn't find it in the line we scanned,
22973 use the start of the following line. */
22974 if (maybe_next_line_p)
22975 {
22976 ++best_row;
22977 ++best_row_vpos;
22978 lastcol = 0;
22979 current_x = best_row->x;
22980 }
22981
22982 *vpos = best_row_vpos;
22983 *hpos = lastcol + 1;
22984 *x = current_x;
22985 *y = best_row->y;
22986 return 0;
22987 }
22988
22989 #endif /* not 1 */
22990
22991
22992 /* Find the position of the glyph for position POS in OBJECT in
22993 window W's current matrix, and return in *X, *Y the pixel
22994 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22995
22996 RIGHT_P non-zero means return the position of the right edge of the
22997 glyph, RIGHT_P zero means return the left edge position.
22998
22999 If no glyph for POS exists in the matrix, return the position of
23000 the glyph with the next smaller position that is in the matrix, if
23001 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23002 exists in the matrix, return the position of the glyph with the
23003 next larger position in OBJECT.
23004
23005 Value is non-zero if a glyph was found. */
23006
23007 static int
23008 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23009 struct window *w;
23010 EMACS_INT pos;
23011 Lisp_Object object;
23012 int *hpos, *vpos, *x, *y;
23013 int right_p;
23014 {
23015 int yb = window_text_bottom_y (w);
23016 struct glyph_row *r;
23017 struct glyph *best_glyph = NULL;
23018 struct glyph_row *best_row = NULL;
23019 int best_x = 0;
23020
23021 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23022 r->enabled_p && r->y < yb;
23023 ++r)
23024 {
23025 struct glyph *g = r->glyphs[TEXT_AREA];
23026 struct glyph *e = g + r->used[TEXT_AREA];
23027 int gx;
23028
23029 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23030 if (EQ (g->object, object))
23031 {
23032 if (g->charpos == pos)
23033 {
23034 best_glyph = g;
23035 best_x = gx;
23036 best_row = r;
23037 goto found;
23038 }
23039 else if (best_glyph == NULL
23040 || ((eabs (g->charpos - pos)
23041 < eabs (best_glyph->charpos - pos))
23042 && (right_p
23043 ? g->charpos < pos
23044 : g->charpos > pos)))
23045 {
23046 best_glyph = g;
23047 best_x = gx;
23048 best_row = r;
23049 }
23050 }
23051 }
23052
23053 found:
23054
23055 if (best_glyph)
23056 {
23057 *x = best_x;
23058 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23059
23060 if (right_p)
23061 {
23062 *x += best_glyph->pixel_width;
23063 ++*hpos;
23064 }
23065
23066 *y = best_row->y;
23067 *vpos = best_row - w->current_matrix->rows;
23068 }
23069
23070 return best_glyph != NULL;
23071 }
23072
23073
23074 /* See if position X, Y is within a hot-spot of an image. */
23075
23076 static int
23077 on_hot_spot_p (hot_spot, x, y)
23078 Lisp_Object hot_spot;
23079 int x, y;
23080 {
23081 if (!CONSP (hot_spot))
23082 return 0;
23083
23084 if (EQ (XCAR (hot_spot), Qrect))
23085 {
23086 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23087 Lisp_Object rect = XCDR (hot_spot);
23088 Lisp_Object tem;
23089 if (!CONSP (rect))
23090 return 0;
23091 if (!CONSP (XCAR (rect)))
23092 return 0;
23093 if (!CONSP (XCDR (rect)))
23094 return 0;
23095 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23096 return 0;
23097 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23098 return 0;
23099 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23100 return 0;
23101 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23102 return 0;
23103 return 1;
23104 }
23105 else if (EQ (XCAR (hot_spot), Qcircle))
23106 {
23107 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23108 Lisp_Object circ = XCDR (hot_spot);
23109 Lisp_Object lr, lx0, ly0;
23110 if (CONSP (circ)
23111 && CONSP (XCAR (circ))
23112 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23113 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23114 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23115 {
23116 double r = XFLOATINT (lr);
23117 double dx = XINT (lx0) - x;
23118 double dy = XINT (ly0) - y;
23119 return (dx * dx + dy * dy <= r * r);
23120 }
23121 }
23122 else if (EQ (XCAR (hot_spot), Qpoly))
23123 {
23124 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23125 if (VECTORP (XCDR (hot_spot)))
23126 {
23127 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23128 Lisp_Object *poly = v->contents;
23129 int n = v->size;
23130 int i;
23131 int inside = 0;
23132 Lisp_Object lx, ly;
23133 int x0, y0;
23134
23135 /* Need an even number of coordinates, and at least 3 edges. */
23136 if (n < 6 || n & 1)
23137 return 0;
23138
23139 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23140 If count is odd, we are inside polygon. Pixels on edges
23141 may or may not be included depending on actual geometry of the
23142 polygon. */
23143 if ((lx = poly[n-2], !INTEGERP (lx))
23144 || (ly = poly[n-1], !INTEGERP (lx)))
23145 return 0;
23146 x0 = XINT (lx), y0 = XINT (ly);
23147 for (i = 0; i < n; i += 2)
23148 {
23149 int x1 = x0, y1 = y0;
23150 if ((lx = poly[i], !INTEGERP (lx))
23151 || (ly = poly[i+1], !INTEGERP (ly)))
23152 return 0;
23153 x0 = XINT (lx), y0 = XINT (ly);
23154
23155 /* Does this segment cross the X line? */
23156 if (x0 >= x)
23157 {
23158 if (x1 >= x)
23159 continue;
23160 }
23161 else if (x1 < x)
23162 continue;
23163 if (y > y0 && y > y1)
23164 continue;
23165 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23166 inside = !inside;
23167 }
23168 return inside;
23169 }
23170 }
23171 return 0;
23172 }
23173
23174 Lisp_Object
23175 find_hot_spot (map, x, y)
23176 Lisp_Object map;
23177 int x, y;
23178 {
23179 while (CONSP (map))
23180 {
23181 if (CONSP (XCAR (map))
23182 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23183 return XCAR (map);
23184 map = XCDR (map);
23185 }
23186
23187 return Qnil;
23188 }
23189
23190 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23191 3, 3, 0,
23192 doc: /* Lookup in image map MAP coordinates X and Y.
23193 An image map is an alist where each element has the format (AREA ID PLIST).
23194 An AREA is specified as either a rectangle, a circle, or a polygon:
23195 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23196 pixel coordinates of the upper left and bottom right corners.
23197 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23198 and the radius of the circle; r may be a float or integer.
23199 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23200 vector describes one corner in the polygon.
23201 Returns the alist element for the first matching AREA in MAP. */)
23202 (map, x, y)
23203 Lisp_Object map;
23204 Lisp_Object x, y;
23205 {
23206 if (NILP (map))
23207 return Qnil;
23208
23209 CHECK_NUMBER (x);
23210 CHECK_NUMBER (y);
23211
23212 return find_hot_spot (map, XINT (x), XINT (y));
23213 }
23214
23215
23216 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23217 static void
23218 define_frame_cursor1 (f, cursor, pointer)
23219 struct frame *f;
23220 Cursor cursor;
23221 Lisp_Object pointer;
23222 {
23223 /* Do not change cursor shape while dragging mouse. */
23224 if (!NILP (do_mouse_tracking))
23225 return;
23226
23227 if (!NILP (pointer))
23228 {
23229 if (EQ (pointer, Qarrow))
23230 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23231 else if (EQ (pointer, Qhand))
23232 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23233 else if (EQ (pointer, Qtext))
23234 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23235 else if (EQ (pointer, intern ("hdrag")))
23236 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23237 #ifdef HAVE_X_WINDOWS
23238 else if (EQ (pointer, intern ("vdrag")))
23239 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23240 #endif
23241 else if (EQ (pointer, intern ("hourglass")))
23242 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23243 else if (EQ (pointer, Qmodeline))
23244 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23245 else
23246 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23247 }
23248
23249 if (cursor != No_Cursor)
23250 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23251 }
23252
23253 /* Take proper action when mouse has moved to the mode or header line
23254 or marginal area AREA of window W, x-position X and y-position Y.
23255 X is relative to the start of the text display area of W, so the
23256 width of bitmap areas and scroll bars must be subtracted to get a
23257 position relative to the start of the mode line. */
23258
23259 static void
23260 note_mode_line_or_margin_highlight (window, x, y, area)
23261 Lisp_Object window;
23262 int x, y;
23263 enum window_part area;
23264 {
23265 struct window *w = XWINDOW (window);
23266 struct frame *f = XFRAME (w->frame);
23267 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23268 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23269 Lisp_Object pointer = Qnil;
23270 int charpos, dx, dy, width, height;
23271 Lisp_Object string, object = Qnil;
23272 Lisp_Object pos, help;
23273
23274 Lisp_Object mouse_face;
23275 int original_x_pixel = x;
23276 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23277 struct glyph_row *row;
23278
23279 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23280 {
23281 int x0;
23282 struct glyph *end;
23283
23284 string = mode_line_string (w, area, &x, &y, &charpos,
23285 &object, &dx, &dy, &width, &height);
23286
23287 row = (area == ON_MODE_LINE
23288 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23289 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23290
23291 /* Find glyph */
23292 if (row->mode_line_p && row->enabled_p)
23293 {
23294 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23295 end = glyph + row->used[TEXT_AREA];
23296
23297 for (x0 = original_x_pixel;
23298 glyph < end && x0 >= glyph->pixel_width;
23299 ++glyph)
23300 x0 -= glyph->pixel_width;
23301
23302 if (glyph >= end)
23303 glyph = NULL;
23304 }
23305 }
23306 else
23307 {
23308 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23309 string = marginal_area_string (w, area, &x, &y, &charpos,
23310 &object, &dx, &dy, &width, &height);
23311 }
23312
23313 help = Qnil;
23314
23315 if (IMAGEP (object))
23316 {
23317 Lisp_Object image_map, hotspot;
23318 if ((image_map = Fplist_get (XCDR (object), QCmap),
23319 !NILP (image_map))
23320 && (hotspot = find_hot_spot (image_map, dx, dy),
23321 CONSP (hotspot))
23322 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23323 {
23324 Lisp_Object area_id, plist;
23325
23326 area_id = XCAR (hotspot);
23327 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23328 If so, we could look for mouse-enter, mouse-leave
23329 properties in PLIST (and do something...). */
23330 hotspot = XCDR (hotspot);
23331 if (CONSP (hotspot)
23332 && (plist = XCAR (hotspot), CONSP (plist)))
23333 {
23334 pointer = Fplist_get (plist, Qpointer);
23335 if (NILP (pointer))
23336 pointer = Qhand;
23337 help = Fplist_get (plist, Qhelp_echo);
23338 if (!NILP (help))
23339 {
23340 help_echo_string = help;
23341 /* Is this correct? ++kfs */
23342 XSETWINDOW (help_echo_window, w);
23343 help_echo_object = w->buffer;
23344 help_echo_pos = charpos;
23345 }
23346 }
23347 }
23348 if (NILP (pointer))
23349 pointer = Fplist_get (XCDR (object), QCpointer);
23350 }
23351
23352 if (STRINGP (string))
23353 {
23354 pos = make_number (charpos);
23355 /* If we're on a string with `help-echo' text property, arrange
23356 for the help to be displayed. This is done by setting the
23357 global variable help_echo_string to the help string. */
23358 if (NILP (help))
23359 {
23360 help = Fget_text_property (pos, Qhelp_echo, string);
23361 if (!NILP (help))
23362 {
23363 help_echo_string = help;
23364 XSETWINDOW (help_echo_window, w);
23365 help_echo_object = string;
23366 help_echo_pos = charpos;
23367 }
23368 }
23369
23370 if (NILP (pointer))
23371 pointer = Fget_text_property (pos, Qpointer, string);
23372
23373 /* Change the mouse pointer according to what is under X/Y. */
23374 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23375 {
23376 Lisp_Object map;
23377 map = Fget_text_property (pos, Qlocal_map, string);
23378 if (!KEYMAPP (map))
23379 map = Fget_text_property (pos, Qkeymap, string);
23380 if (!KEYMAPP (map))
23381 cursor = dpyinfo->vertical_scroll_bar_cursor;
23382 }
23383
23384 /* Change the mouse face according to what is under X/Y. */
23385 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23386 if (!NILP (mouse_face)
23387 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23388 && glyph)
23389 {
23390 Lisp_Object b, e;
23391
23392 struct glyph * tmp_glyph;
23393
23394 int gpos;
23395 int gseq_length;
23396 int total_pixel_width;
23397 EMACS_INT ignore;
23398
23399 int vpos, hpos;
23400
23401 b = Fprevious_single_property_change (make_number (charpos + 1),
23402 Qmouse_face, string, Qnil);
23403 if (NILP (b))
23404 b = make_number (0);
23405
23406 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23407 if (NILP (e))
23408 e = make_number (SCHARS (string));
23409
23410 /* Calculate the position(glyph position: GPOS) of GLYPH in
23411 displayed string. GPOS is different from CHARPOS.
23412
23413 CHARPOS is the position of glyph in internal string
23414 object. A mode line string format has structures which
23415 is converted to a flatten by emacs lisp interpreter.
23416 The internal string is an element of the structures.
23417 The displayed string is the flatten string. */
23418 gpos = 0;
23419 if (glyph > row_start_glyph)
23420 {
23421 tmp_glyph = glyph - 1;
23422 while (tmp_glyph >= row_start_glyph
23423 && tmp_glyph->charpos >= XINT (b)
23424 && EQ (tmp_glyph->object, glyph->object))
23425 {
23426 tmp_glyph--;
23427 gpos++;
23428 }
23429 }
23430
23431 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23432 displayed string holding GLYPH.
23433
23434 GSEQ_LENGTH is different from SCHARS (STRING).
23435 SCHARS (STRING) returns the length of the internal string. */
23436 for (tmp_glyph = glyph, gseq_length = gpos;
23437 tmp_glyph->charpos < XINT (e);
23438 tmp_glyph++, gseq_length++)
23439 {
23440 if (!EQ (tmp_glyph->object, glyph->object))
23441 break;
23442 }
23443
23444 total_pixel_width = 0;
23445 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23446 total_pixel_width += tmp_glyph->pixel_width;
23447
23448 /* Pre calculation of re-rendering position */
23449 vpos = (x - gpos);
23450 hpos = (area == ON_MODE_LINE
23451 ? (w->current_matrix)->nrows - 1
23452 : 0);
23453
23454 /* If the re-rendering position is included in the last
23455 re-rendering area, we should do nothing. */
23456 if ( EQ (window, dpyinfo->mouse_face_window)
23457 && dpyinfo->mouse_face_beg_col <= vpos
23458 && vpos < dpyinfo->mouse_face_end_col
23459 && dpyinfo->mouse_face_beg_row == hpos )
23460 return;
23461
23462 if (clear_mouse_face (dpyinfo))
23463 cursor = No_Cursor;
23464
23465 dpyinfo->mouse_face_beg_col = vpos;
23466 dpyinfo->mouse_face_beg_row = hpos;
23467
23468 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23469 dpyinfo->mouse_face_beg_y = 0;
23470
23471 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23472 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23473
23474 dpyinfo->mouse_face_end_x = 0;
23475 dpyinfo->mouse_face_end_y = 0;
23476
23477 dpyinfo->mouse_face_past_end = 0;
23478 dpyinfo->mouse_face_window = window;
23479
23480 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23481 charpos,
23482 0, 0, 0, &ignore,
23483 glyph->face_id, 1);
23484 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23485
23486 if (NILP (pointer))
23487 pointer = Qhand;
23488 }
23489 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23490 clear_mouse_face (dpyinfo);
23491 }
23492 define_frame_cursor1 (f, cursor, pointer);
23493 }
23494
23495
23496 /* EXPORT:
23497 Take proper action when the mouse has moved to position X, Y on
23498 frame F as regards highlighting characters that have mouse-face
23499 properties. Also de-highlighting chars where the mouse was before.
23500 X and Y can be negative or out of range. */
23501
23502 void
23503 note_mouse_highlight (f, x, y)
23504 struct frame *f;
23505 int x, y;
23506 {
23507 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23508 enum window_part part;
23509 Lisp_Object window;
23510 struct window *w;
23511 Cursor cursor = No_Cursor;
23512 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23513 struct buffer *b;
23514
23515 /* When a menu is active, don't highlight because this looks odd. */
23516 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
23517 if (popup_activated ())
23518 return;
23519 #endif
23520
23521 if (NILP (Vmouse_highlight)
23522 || !f->glyphs_initialized_p)
23523 return;
23524
23525 dpyinfo->mouse_face_mouse_x = x;
23526 dpyinfo->mouse_face_mouse_y = y;
23527 dpyinfo->mouse_face_mouse_frame = f;
23528
23529 if (dpyinfo->mouse_face_defer)
23530 return;
23531
23532 if (gc_in_progress)
23533 {
23534 dpyinfo->mouse_face_deferred_gc = 1;
23535 return;
23536 }
23537
23538 /* Which window is that in? */
23539 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23540
23541 /* If we were displaying active text in another window, clear that.
23542 Also clear if we move out of text area in same window. */
23543 if (! EQ (window, dpyinfo->mouse_face_window)
23544 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23545 && !NILP (dpyinfo->mouse_face_window)))
23546 clear_mouse_face (dpyinfo);
23547
23548 /* Not on a window -> return. */
23549 if (!WINDOWP (window))
23550 return;
23551
23552 /* Reset help_echo_string. It will get recomputed below. */
23553 help_echo_string = Qnil;
23554
23555 /* Convert to window-relative pixel coordinates. */
23556 w = XWINDOW (window);
23557 frame_to_window_pixel_xy (w, &x, &y);
23558
23559 /* Handle tool-bar window differently since it doesn't display a
23560 buffer. */
23561 if (EQ (window, f->tool_bar_window))
23562 {
23563 note_tool_bar_highlight (f, x, y);
23564 return;
23565 }
23566
23567 /* Mouse is on the mode, header line or margin? */
23568 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23569 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23570 {
23571 note_mode_line_or_margin_highlight (window, x, y, part);
23572 return;
23573 }
23574
23575 if (part == ON_VERTICAL_BORDER)
23576 {
23577 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23578 help_echo_string = build_string ("drag-mouse-1: resize");
23579 }
23580 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23581 || part == ON_SCROLL_BAR)
23582 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23583 else
23584 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23585
23586 /* Are we in a window whose display is up to date?
23587 And verify the buffer's text has not changed. */
23588 b = XBUFFER (w->buffer);
23589 if (part == ON_TEXT
23590 && EQ (w->window_end_valid, w->buffer)
23591 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23592 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23593 {
23594 int hpos, vpos, pos, i, dx, dy, area;
23595 struct glyph *glyph;
23596 Lisp_Object object;
23597 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23598 Lisp_Object *overlay_vec = NULL;
23599 int noverlays;
23600 struct buffer *obuf;
23601 int obegv, ozv, same_region;
23602
23603 /* Find the glyph under X/Y. */
23604 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23605
23606 /* Look for :pointer property on image. */
23607 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23608 {
23609 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23610 if (img != NULL && IMAGEP (img->spec))
23611 {
23612 Lisp_Object image_map, hotspot;
23613 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23614 !NILP (image_map))
23615 && (hotspot = find_hot_spot (image_map,
23616 glyph->slice.x + dx,
23617 glyph->slice.y + dy),
23618 CONSP (hotspot))
23619 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23620 {
23621 Lisp_Object area_id, plist;
23622
23623 area_id = XCAR (hotspot);
23624 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23625 If so, we could look for mouse-enter, mouse-leave
23626 properties in PLIST (and do something...). */
23627 hotspot = XCDR (hotspot);
23628 if (CONSP (hotspot)
23629 && (plist = XCAR (hotspot), CONSP (plist)))
23630 {
23631 pointer = Fplist_get (plist, Qpointer);
23632 if (NILP (pointer))
23633 pointer = Qhand;
23634 help_echo_string = Fplist_get (plist, Qhelp_echo);
23635 if (!NILP (help_echo_string))
23636 {
23637 help_echo_window = window;
23638 help_echo_object = glyph->object;
23639 help_echo_pos = glyph->charpos;
23640 }
23641 }
23642 }
23643 if (NILP (pointer))
23644 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23645 }
23646 }
23647
23648 /* Clear mouse face if X/Y not over text. */
23649 if (glyph == NULL
23650 || area != TEXT_AREA
23651 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23652 {
23653 if (clear_mouse_face (dpyinfo))
23654 cursor = No_Cursor;
23655 if (NILP (pointer))
23656 {
23657 if (area != TEXT_AREA)
23658 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23659 else
23660 pointer = Vvoid_text_area_pointer;
23661 }
23662 goto set_cursor;
23663 }
23664
23665 pos = glyph->charpos;
23666 object = glyph->object;
23667 if (!STRINGP (object) && !BUFFERP (object))
23668 goto set_cursor;
23669
23670 /* If we get an out-of-range value, return now; avoid an error. */
23671 if (BUFFERP (object) && pos > BUF_Z (b))
23672 goto set_cursor;
23673
23674 /* Make the window's buffer temporarily current for
23675 overlays_at and compute_char_face. */
23676 obuf = current_buffer;
23677 current_buffer = b;
23678 obegv = BEGV;
23679 ozv = ZV;
23680 BEGV = BEG;
23681 ZV = Z;
23682
23683 /* Is this char mouse-active or does it have help-echo? */
23684 position = make_number (pos);
23685
23686 if (BUFFERP (object))
23687 {
23688 /* Put all the overlays we want in a vector in overlay_vec. */
23689 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23690 /* Sort overlays into increasing priority order. */
23691 noverlays = sort_overlays (overlay_vec, noverlays, w);
23692 }
23693 else
23694 noverlays = 0;
23695
23696 same_region = (EQ (window, dpyinfo->mouse_face_window)
23697 && vpos >= dpyinfo->mouse_face_beg_row
23698 && vpos <= dpyinfo->mouse_face_end_row
23699 && (vpos > dpyinfo->mouse_face_beg_row
23700 || hpos >= dpyinfo->mouse_face_beg_col)
23701 && (vpos < dpyinfo->mouse_face_end_row
23702 || hpos < dpyinfo->mouse_face_end_col
23703 || dpyinfo->mouse_face_past_end));
23704
23705 if (same_region)
23706 cursor = No_Cursor;
23707
23708 /* Check mouse-face highlighting. */
23709 if (! same_region
23710 /* If there exists an overlay with mouse-face overlapping
23711 the one we are currently highlighting, we have to
23712 check if we enter the overlapping overlay, and then
23713 highlight only that. */
23714 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23715 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23716 {
23717 /* Find the highest priority overlay that has a mouse-face
23718 property. */
23719 overlay = Qnil;
23720 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23721 {
23722 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23723 if (!NILP (mouse_face))
23724 overlay = overlay_vec[i];
23725 }
23726
23727 /* If we're actually highlighting the same overlay as
23728 before, there's no need to do that again. */
23729 if (!NILP (overlay)
23730 && EQ (overlay, dpyinfo->mouse_face_overlay))
23731 goto check_help_echo;
23732
23733 dpyinfo->mouse_face_overlay = overlay;
23734
23735 /* Clear the display of the old active region, if any. */
23736 if (clear_mouse_face (dpyinfo))
23737 cursor = No_Cursor;
23738
23739 /* If no overlay applies, get a text property. */
23740 if (NILP (overlay))
23741 mouse_face = Fget_text_property (position, Qmouse_face, object);
23742
23743 /* Handle the overlay case. */
23744 if (!NILP (overlay))
23745 {
23746 /* Find the range of text around this char that
23747 should be active. */
23748 Lisp_Object before, after;
23749 EMACS_INT ignore;
23750
23751 before = Foverlay_start (overlay);
23752 after = Foverlay_end (overlay);
23753 /* Record this as the current active region. */
23754 fast_find_position (w, XFASTINT (before),
23755 &dpyinfo->mouse_face_beg_col,
23756 &dpyinfo->mouse_face_beg_row,
23757 &dpyinfo->mouse_face_beg_x,
23758 &dpyinfo->mouse_face_beg_y, Qnil);
23759
23760 dpyinfo->mouse_face_past_end
23761 = !fast_find_position (w, XFASTINT (after),
23762 &dpyinfo->mouse_face_end_col,
23763 &dpyinfo->mouse_face_end_row,
23764 &dpyinfo->mouse_face_end_x,
23765 &dpyinfo->mouse_face_end_y, Qnil);
23766 dpyinfo->mouse_face_window = window;
23767
23768 dpyinfo->mouse_face_face_id
23769 = face_at_buffer_position (w, pos, 0, 0,
23770 &ignore, pos + 1,
23771 !dpyinfo->mouse_face_hidden);
23772
23773 /* Display it as active. */
23774 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23775 cursor = No_Cursor;
23776 }
23777 /* Handle the text property case. */
23778 else if (!NILP (mouse_face) && BUFFERP (object))
23779 {
23780 /* Find the range of text around this char that
23781 should be active. */
23782 Lisp_Object before, after, beginning, end;
23783 EMACS_INT ignore;
23784
23785 beginning = Fmarker_position (w->start);
23786 end = make_number (BUF_Z (XBUFFER (object))
23787 - XFASTINT (w->window_end_pos));
23788 before
23789 = Fprevious_single_property_change (make_number (pos + 1),
23790 Qmouse_face,
23791 object, beginning);
23792 after
23793 = Fnext_single_property_change (position, Qmouse_face,
23794 object, end);
23795
23796 /* Record this as the current active region. */
23797 fast_find_position (w, XFASTINT (before),
23798 &dpyinfo->mouse_face_beg_col,
23799 &dpyinfo->mouse_face_beg_row,
23800 &dpyinfo->mouse_face_beg_x,
23801 &dpyinfo->mouse_face_beg_y, Qnil);
23802 dpyinfo->mouse_face_past_end
23803 = !fast_find_position (w, XFASTINT (after),
23804 &dpyinfo->mouse_face_end_col,
23805 &dpyinfo->mouse_face_end_row,
23806 &dpyinfo->mouse_face_end_x,
23807 &dpyinfo->mouse_face_end_y, Qnil);
23808 dpyinfo->mouse_face_window = window;
23809
23810 if (BUFFERP (object))
23811 dpyinfo->mouse_face_face_id
23812 = face_at_buffer_position (w, pos, 0, 0,
23813 &ignore, pos + 1,
23814 !dpyinfo->mouse_face_hidden);
23815
23816 /* Display it as active. */
23817 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23818 cursor = No_Cursor;
23819 }
23820 else if (!NILP (mouse_face) && STRINGP (object))
23821 {
23822 Lisp_Object b, e;
23823 EMACS_INT ignore;
23824
23825 b = Fprevious_single_property_change (make_number (pos + 1),
23826 Qmouse_face,
23827 object, Qnil);
23828 e = Fnext_single_property_change (position, Qmouse_face,
23829 object, Qnil);
23830 if (NILP (b))
23831 b = make_number (0);
23832 if (NILP (e))
23833 e = make_number (SCHARS (object) - 1);
23834
23835 fast_find_string_pos (w, XINT (b), object,
23836 &dpyinfo->mouse_face_beg_col,
23837 &dpyinfo->mouse_face_beg_row,
23838 &dpyinfo->mouse_face_beg_x,
23839 &dpyinfo->mouse_face_beg_y, 0);
23840 fast_find_string_pos (w, XINT (e), object,
23841 &dpyinfo->mouse_face_end_col,
23842 &dpyinfo->mouse_face_end_row,
23843 &dpyinfo->mouse_face_end_x,
23844 &dpyinfo->mouse_face_end_y, 1);
23845 dpyinfo->mouse_face_past_end = 0;
23846 dpyinfo->mouse_face_window = window;
23847 dpyinfo->mouse_face_face_id
23848 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23849 glyph->face_id, 1);
23850 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23851 cursor = No_Cursor;
23852 }
23853 else if (STRINGP (object) && NILP (mouse_face))
23854 {
23855 /* A string which doesn't have mouse-face, but
23856 the text ``under'' it might have. */
23857 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23858 int start = MATRIX_ROW_START_CHARPOS (r);
23859
23860 pos = string_buffer_position (w, object, start);
23861 if (pos > 0)
23862 mouse_face = get_char_property_and_overlay (make_number (pos),
23863 Qmouse_face,
23864 w->buffer,
23865 &overlay);
23866 if (!NILP (mouse_face) && !NILP (overlay))
23867 {
23868 Lisp_Object before = Foverlay_start (overlay);
23869 Lisp_Object after = Foverlay_end (overlay);
23870 EMACS_INT ignore;
23871
23872 /* Note that we might not be able to find position
23873 BEFORE in the glyph matrix if the overlay is
23874 entirely covered by a `display' property. In
23875 this case, we overshoot. So let's stop in
23876 the glyph matrix before glyphs for OBJECT. */
23877 fast_find_position (w, XFASTINT (before),
23878 &dpyinfo->mouse_face_beg_col,
23879 &dpyinfo->mouse_face_beg_row,
23880 &dpyinfo->mouse_face_beg_x,
23881 &dpyinfo->mouse_face_beg_y,
23882 object);
23883
23884 dpyinfo->mouse_face_past_end
23885 = !fast_find_position (w, XFASTINT (after),
23886 &dpyinfo->mouse_face_end_col,
23887 &dpyinfo->mouse_face_end_row,
23888 &dpyinfo->mouse_face_end_x,
23889 &dpyinfo->mouse_face_end_y,
23890 Qnil);
23891 dpyinfo->mouse_face_window = window;
23892 dpyinfo->mouse_face_face_id
23893 = face_at_buffer_position (w, pos, 0, 0,
23894 &ignore, pos + 1,
23895 !dpyinfo->mouse_face_hidden);
23896
23897 /* Display it as active. */
23898 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23899 cursor = No_Cursor;
23900 }
23901 }
23902 }
23903
23904 check_help_echo:
23905
23906 /* Look for a `help-echo' property. */
23907 if (NILP (help_echo_string)) {
23908 Lisp_Object help, overlay;
23909
23910 /* Check overlays first. */
23911 help = overlay = Qnil;
23912 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23913 {
23914 overlay = overlay_vec[i];
23915 help = Foverlay_get (overlay, Qhelp_echo);
23916 }
23917
23918 if (!NILP (help))
23919 {
23920 help_echo_string = help;
23921 help_echo_window = window;
23922 help_echo_object = overlay;
23923 help_echo_pos = pos;
23924 }
23925 else
23926 {
23927 Lisp_Object object = glyph->object;
23928 int charpos = glyph->charpos;
23929
23930 /* Try text properties. */
23931 if (STRINGP (object)
23932 && charpos >= 0
23933 && charpos < SCHARS (object))
23934 {
23935 help = Fget_text_property (make_number (charpos),
23936 Qhelp_echo, object);
23937 if (NILP (help))
23938 {
23939 /* If the string itself doesn't specify a help-echo,
23940 see if the buffer text ``under'' it does. */
23941 struct glyph_row *r
23942 = MATRIX_ROW (w->current_matrix, vpos);
23943 int start = MATRIX_ROW_START_CHARPOS (r);
23944 int pos = string_buffer_position (w, object, start);
23945 if (pos > 0)
23946 {
23947 help = Fget_char_property (make_number (pos),
23948 Qhelp_echo, w->buffer);
23949 if (!NILP (help))
23950 {
23951 charpos = pos;
23952 object = w->buffer;
23953 }
23954 }
23955 }
23956 }
23957 else if (BUFFERP (object)
23958 && charpos >= BEGV
23959 && charpos < ZV)
23960 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23961 object);
23962
23963 if (!NILP (help))
23964 {
23965 help_echo_string = help;
23966 help_echo_window = window;
23967 help_echo_object = object;
23968 help_echo_pos = charpos;
23969 }
23970 }
23971 }
23972
23973 /* Look for a `pointer' property. */
23974 if (NILP (pointer))
23975 {
23976 /* Check overlays first. */
23977 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23978 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23979
23980 if (NILP (pointer))
23981 {
23982 Lisp_Object object = glyph->object;
23983 int charpos = glyph->charpos;
23984
23985 /* Try text properties. */
23986 if (STRINGP (object)
23987 && charpos >= 0
23988 && charpos < SCHARS (object))
23989 {
23990 pointer = Fget_text_property (make_number (charpos),
23991 Qpointer, object);
23992 if (NILP (pointer))
23993 {
23994 /* If the string itself doesn't specify a pointer,
23995 see if the buffer text ``under'' it does. */
23996 struct glyph_row *r
23997 = MATRIX_ROW (w->current_matrix, vpos);
23998 int start = MATRIX_ROW_START_CHARPOS (r);
23999 int pos = string_buffer_position (w, object, start);
24000 if (pos > 0)
24001 pointer = Fget_char_property (make_number (pos),
24002 Qpointer, w->buffer);
24003 }
24004 }
24005 else if (BUFFERP (object)
24006 && charpos >= BEGV
24007 && charpos < ZV)
24008 pointer = Fget_text_property (make_number (charpos),
24009 Qpointer, object);
24010 }
24011 }
24012
24013 BEGV = obegv;
24014 ZV = ozv;
24015 current_buffer = obuf;
24016 }
24017
24018 set_cursor:
24019
24020 define_frame_cursor1 (f, cursor, pointer);
24021 }
24022
24023
24024 /* EXPORT for RIF:
24025 Clear any mouse-face on window W. This function is part of the
24026 redisplay interface, and is called from try_window_id and similar
24027 functions to ensure the mouse-highlight is off. */
24028
24029 void
24030 x_clear_window_mouse_face (w)
24031 struct window *w;
24032 {
24033 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24034 Lisp_Object window;
24035
24036 BLOCK_INPUT;
24037 XSETWINDOW (window, w);
24038 if (EQ (window, dpyinfo->mouse_face_window))
24039 clear_mouse_face (dpyinfo);
24040 UNBLOCK_INPUT;
24041 }
24042
24043
24044 /* EXPORT:
24045 Just discard the mouse face information for frame F, if any.
24046 This is used when the size of F is changed. */
24047
24048 void
24049 cancel_mouse_face (f)
24050 struct frame *f;
24051 {
24052 Lisp_Object window;
24053 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24054
24055 window = dpyinfo->mouse_face_window;
24056 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24057 {
24058 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24059 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24060 dpyinfo->mouse_face_window = Qnil;
24061 }
24062 }
24063
24064
24065 #endif /* HAVE_WINDOW_SYSTEM */
24066
24067 \f
24068 /***********************************************************************
24069 Exposure Events
24070 ***********************************************************************/
24071
24072 #ifdef HAVE_WINDOW_SYSTEM
24073
24074 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24075 which intersects rectangle R. R is in window-relative coordinates. */
24076
24077 static void
24078 expose_area (w, row, r, area)
24079 struct window *w;
24080 struct glyph_row *row;
24081 XRectangle *r;
24082 enum glyph_row_area area;
24083 {
24084 struct glyph *first = row->glyphs[area];
24085 struct glyph *end = row->glyphs[area] + row->used[area];
24086 struct glyph *last;
24087 int first_x, start_x, x;
24088
24089 if (area == TEXT_AREA && row->fill_line_p)
24090 /* If row extends face to end of line write the whole line. */
24091 draw_glyphs (w, 0, row, area,
24092 0, row->used[area],
24093 DRAW_NORMAL_TEXT, 0);
24094 else
24095 {
24096 /* Set START_X to the window-relative start position for drawing glyphs of
24097 AREA. The first glyph of the text area can be partially visible.
24098 The first glyphs of other areas cannot. */
24099 start_x = window_box_left_offset (w, area);
24100 x = start_x;
24101 if (area == TEXT_AREA)
24102 x += row->x;
24103
24104 /* Find the first glyph that must be redrawn. */
24105 while (first < end
24106 && x + first->pixel_width < r->x)
24107 {
24108 x += first->pixel_width;
24109 ++first;
24110 }
24111
24112 /* Find the last one. */
24113 last = first;
24114 first_x = x;
24115 while (last < end
24116 && x < r->x + r->width)
24117 {
24118 x += last->pixel_width;
24119 ++last;
24120 }
24121
24122 /* Repaint. */
24123 if (last > first)
24124 draw_glyphs (w, first_x - start_x, row, area,
24125 first - row->glyphs[area], last - row->glyphs[area],
24126 DRAW_NORMAL_TEXT, 0);
24127 }
24128 }
24129
24130
24131 /* Redraw the parts of the glyph row ROW on window W intersecting
24132 rectangle R. R is in window-relative coordinates. Value is
24133 non-zero if mouse-face was overwritten. */
24134
24135 static int
24136 expose_line (w, row, r)
24137 struct window *w;
24138 struct glyph_row *row;
24139 XRectangle *r;
24140 {
24141 xassert (row->enabled_p);
24142
24143 if (row->mode_line_p || w->pseudo_window_p)
24144 draw_glyphs (w, 0, row, TEXT_AREA,
24145 0, row->used[TEXT_AREA],
24146 DRAW_NORMAL_TEXT, 0);
24147 else
24148 {
24149 if (row->used[LEFT_MARGIN_AREA])
24150 expose_area (w, row, r, LEFT_MARGIN_AREA);
24151 if (row->used[TEXT_AREA])
24152 expose_area (w, row, r, TEXT_AREA);
24153 if (row->used[RIGHT_MARGIN_AREA])
24154 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24155 draw_row_fringe_bitmaps (w, row);
24156 }
24157
24158 return row->mouse_face_p;
24159 }
24160
24161
24162 /* Redraw those parts of glyphs rows during expose event handling that
24163 overlap other rows. Redrawing of an exposed line writes over parts
24164 of lines overlapping that exposed line; this function fixes that.
24165
24166 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24167 row in W's current matrix that is exposed and overlaps other rows.
24168 LAST_OVERLAPPING_ROW is the last such row. */
24169
24170 static void
24171 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24172 struct window *w;
24173 struct glyph_row *first_overlapping_row;
24174 struct glyph_row *last_overlapping_row;
24175 XRectangle *r;
24176 {
24177 struct glyph_row *row;
24178
24179 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24180 if (row->overlapping_p)
24181 {
24182 xassert (row->enabled_p && !row->mode_line_p);
24183
24184 row->clip = r;
24185 if (row->used[LEFT_MARGIN_AREA])
24186 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24187
24188 if (row->used[TEXT_AREA])
24189 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24190
24191 if (row->used[RIGHT_MARGIN_AREA])
24192 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24193 row->clip = NULL;
24194 }
24195 }
24196
24197
24198 /* Return non-zero if W's cursor intersects rectangle R. */
24199
24200 static int
24201 phys_cursor_in_rect_p (w, r)
24202 struct window *w;
24203 XRectangle *r;
24204 {
24205 XRectangle cr, result;
24206 struct glyph *cursor_glyph;
24207 struct glyph_row *row;
24208
24209 if (w->phys_cursor.vpos >= 0
24210 && w->phys_cursor.vpos < w->current_matrix->nrows
24211 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24212 row->enabled_p)
24213 && row->cursor_in_fringe_p)
24214 {
24215 /* Cursor is in the fringe. */
24216 cr.x = window_box_right_offset (w,
24217 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24218 ? RIGHT_MARGIN_AREA
24219 : TEXT_AREA));
24220 cr.y = row->y;
24221 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24222 cr.height = row->height;
24223 return x_intersect_rectangles (&cr, r, &result);
24224 }
24225
24226 cursor_glyph = get_phys_cursor_glyph (w);
24227 if (cursor_glyph)
24228 {
24229 /* r is relative to W's box, but w->phys_cursor.x is relative
24230 to left edge of W's TEXT area. Adjust it. */
24231 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24232 cr.y = w->phys_cursor.y;
24233 cr.width = cursor_glyph->pixel_width;
24234 cr.height = w->phys_cursor_height;
24235 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24236 I assume the effect is the same -- and this is portable. */
24237 return x_intersect_rectangles (&cr, r, &result);
24238 }
24239 /* If we don't understand the format, pretend we're not in the hot-spot. */
24240 return 0;
24241 }
24242
24243
24244 /* EXPORT:
24245 Draw a vertical window border to the right of window W if W doesn't
24246 have vertical scroll bars. */
24247
24248 void
24249 x_draw_vertical_border (w)
24250 struct window *w;
24251 {
24252 struct frame *f = XFRAME (WINDOW_FRAME (w));
24253
24254 /* We could do better, if we knew what type of scroll-bar the adjacent
24255 windows (on either side) have... But we don't :-(
24256 However, I think this works ok. ++KFS 2003-04-25 */
24257
24258 /* Redraw borders between horizontally adjacent windows. Don't
24259 do it for frames with vertical scroll bars because either the
24260 right scroll bar of a window, or the left scroll bar of its
24261 neighbor will suffice as a border. */
24262 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24263 return;
24264
24265 if (!WINDOW_RIGHTMOST_P (w)
24266 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24267 {
24268 int x0, x1, y0, y1;
24269
24270 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24271 y1 -= 1;
24272
24273 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24274 x1 -= 1;
24275
24276 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24277 }
24278 else if (!WINDOW_LEFTMOST_P (w)
24279 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24280 {
24281 int x0, x1, y0, y1;
24282
24283 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24284 y1 -= 1;
24285
24286 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24287 x0 -= 1;
24288
24289 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24290 }
24291 }
24292
24293
24294 /* Redraw the part of window W intersection rectangle FR. Pixel
24295 coordinates in FR are frame-relative. Call this function with
24296 input blocked. Value is non-zero if the exposure overwrites
24297 mouse-face. */
24298
24299 static int
24300 expose_window (w, fr)
24301 struct window *w;
24302 XRectangle *fr;
24303 {
24304 struct frame *f = XFRAME (w->frame);
24305 XRectangle wr, r;
24306 int mouse_face_overwritten_p = 0;
24307
24308 /* If window is not yet fully initialized, do nothing. This can
24309 happen when toolkit scroll bars are used and a window is split.
24310 Reconfiguring the scroll bar will generate an expose for a newly
24311 created window. */
24312 if (w->current_matrix == NULL)
24313 return 0;
24314
24315 /* When we're currently updating the window, display and current
24316 matrix usually don't agree. Arrange for a thorough display
24317 later. */
24318 if (w == updated_window)
24319 {
24320 SET_FRAME_GARBAGED (f);
24321 return 0;
24322 }
24323
24324 /* Frame-relative pixel rectangle of W. */
24325 wr.x = WINDOW_LEFT_EDGE_X (w);
24326 wr.y = WINDOW_TOP_EDGE_Y (w);
24327 wr.width = WINDOW_TOTAL_WIDTH (w);
24328 wr.height = WINDOW_TOTAL_HEIGHT (w);
24329
24330 if (x_intersect_rectangles (fr, &wr, &r))
24331 {
24332 int yb = window_text_bottom_y (w);
24333 struct glyph_row *row;
24334 int cursor_cleared_p;
24335 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24336
24337 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24338 r.x, r.y, r.width, r.height));
24339
24340 /* Convert to window coordinates. */
24341 r.x -= WINDOW_LEFT_EDGE_X (w);
24342 r.y -= WINDOW_TOP_EDGE_Y (w);
24343
24344 /* Turn off the cursor. */
24345 if (!w->pseudo_window_p
24346 && phys_cursor_in_rect_p (w, &r))
24347 {
24348 x_clear_cursor (w);
24349 cursor_cleared_p = 1;
24350 }
24351 else
24352 cursor_cleared_p = 0;
24353
24354 /* Update lines intersecting rectangle R. */
24355 first_overlapping_row = last_overlapping_row = NULL;
24356 for (row = w->current_matrix->rows;
24357 row->enabled_p;
24358 ++row)
24359 {
24360 int y0 = row->y;
24361 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24362
24363 if ((y0 >= r.y && y0 < r.y + r.height)
24364 || (y1 > r.y && y1 < r.y + r.height)
24365 || (r.y >= y0 && r.y < y1)
24366 || (r.y + r.height > y0 && r.y + r.height < y1))
24367 {
24368 /* A header line may be overlapping, but there is no need
24369 to fix overlapping areas for them. KFS 2005-02-12 */
24370 if (row->overlapping_p && !row->mode_line_p)
24371 {
24372 if (first_overlapping_row == NULL)
24373 first_overlapping_row = row;
24374 last_overlapping_row = row;
24375 }
24376
24377 row->clip = fr;
24378 if (expose_line (w, row, &r))
24379 mouse_face_overwritten_p = 1;
24380 row->clip = NULL;
24381 }
24382 else if (row->overlapping_p)
24383 {
24384 /* We must redraw a row overlapping the exposed area. */
24385 if (y0 < r.y
24386 ? y0 + row->phys_height > r.y
24387 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24388 {
24389 if (first_overlapping_row == NULL)
24390 first_overlapping_row = row;
24391 last_overlapping_row = row;
24392 }
24393 }
24394
24395 if (y1 >= yb)
24396 break;
24397 }
24398
24399 /* Display the mode line if there is one. */
24400 if (WINDOW_WANTS_MODELINE_P (w)
24401 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24402 row->enabled_p)
24403 && row->y < r.y + r.height)
24404 {
24405 if (expose_line (w, row, &r))
24406 mouse_face_overwritten_p = 1;
24407 }
24408
24409 if (!w->pseudo_window_p)
24410 {
24411 /* Fix the display of overlapping rows. */
24412 if (first_overlapping_row)
24413 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24414 fr);
24415
24416 /* Draw border between windows. */
24417 x_draw_vertical_border (w);
24418
24419 /* Turn the cursor on again. */
24420 if (cursor_cleared_p)
24421 update_window_cursor (w, 1);
24422 }
24423 }
24424
24425 return mouse_face_overwritten_p;
24426 }
24427
24428
24429
24430 /* Redraw (parts) of all windows in the window tree rooted at W that
24431 intersect R. R contains frame pixel coordinates. Value is
24432 non-zero if the exposure overwrites mouse-face. */
24433
24434 static int
24435 expose_window_tree (w, r)
24436 struct window *w;
24437 XRectangle *r;
24438 {
24439 struct frame *f = XFRAME (w->frame);
24440 int mouse_face_overwritten_p = 0;
24441
24442 while (w && !FRAME_GARBAGED_P (f))
24443 {
24444 if (!NILP (w->hchild))
24445 mouse_face_overwritten_p
24446 |= expose_window_tree (XWINDOW (w->hchild), r);
24447 else if (!NILP (w->vchild))
24448 mouse_face_overwritten_p
24449 |= expose_window_tree (XWINDOW (w->vchild), r);
24450 else
24451 mouse_face_overwritten_p |= expose_window (w, r);
24452
24453 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24454 }
24455
24456 return mouse_face_overwritten_p;
24457 }
24458
24459
24460 /* EXPORT:
24461 Redisplay an exposed area of frame F. X and Y are the upper-left
24462 corner of the exposed rectangle. W and H are width and height of
24463 the exposed area. All are pixel values. W or H zero means redraw
24464 the entire frame. */
24465
24466 void
24467 expose_frame (f, x, y, w, h)
24468 struct frame *f;
24469 int x, y, w, h;
24470 {
24471 XRectangle r;
24472 int mouse_face_overwritten_p = 0;
24473
24474 TRACE ((stderr, "expose_frame "));
24475
24476 /* No need to redraw if frame will be redrawn soon. */
24477 if (FRAME_GARBAGED_P (f))
24478 {
24479 TRACE ((stderr, " garbaged\n"));
24480 return;
24481 }
24482
24483 /* If basic faces haven't been realized yet, there is no point in
24484 trying to redraw anything. This can happen when we get an expose
24485 event while Emacs is starting, e.g. by moving another window. */
24486 if (FRAME_FACE_CACHE (f) == NULL
24487 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24488 {
24489 TRACE ((stderr, " no faces\n"));
24490 return;
24491 }
24492
24493 if (w == 0 || h == 0)
24494 {
24495 r.x = r.y = 0;
24496 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24497 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24498 }
24499 else
24500 {
24501 r.x = x;
24502 r.y = y;
24503 r.width = w;
24504 r.height = h;
24505 }
24506
24507 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24508 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24509
24510 if (WINDOWP (f->tool_bar_window))
24511 mouse_face_overwritten_p
24512 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24513
24514 #ifdef HAVE_X_WINDOWS
24515 #ifndef MSDOS
24516 #ifndef USE_X_TOOLKIT
24517 if (WINDOWP (f->menu_bar_window))
24518 mouse_face_overwritten_p
24519 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24520 #endif /* not USE_X_TOOLKIT */
24521 #endif
24522 #endif
24523
24524 /* Some window managers support a focus-follows-mouse style with
24525 delayed raising of frames. Imagine a partially obscured frame,
24526 and moving the mouse into partially obscured mouse-face on that
24527 frame. The visible part of the mouse-face will be highlighted,
24528 then the WM raises the obscured frame. With at least one WM, KDE
24529 2.1, Emacs is not getting any event for the raising of the frame
24530 (even tried with SubstructureRedirectMask), only Expose events.
24531 These expose events will draw text normally, i.e. not
24532 highlighted. Which means we must redo the highlight here.
24533 Subsume it under ``we love X''. --gerd 2001-08-15 */
24534 /* Included in Windows version because Windows most likely does not
24535 do the right thing if any third party tool offers
24536 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24537 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24538 {
24539 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24540 if (f == dpyinfo->mouse_face_mouse_frame)
24541 {
24542 int x = dpyinfo->mouse_face_mouse_x;
24543 int y = dpyinfo->mouse_face_mouse_y;
24544 clear_mouse_face (dpyinfo);
24545 note_mouse_highlight (f, x, y);
24546 }
24547 }
24548 }
24549
24550
24551 /* EXPORT:
24552 Determine the intersection of two rectangles R1 and R2. Return
24553 the intersection in *RESULT. Value is non-zero if RESULT is not
24554 empty. */
24555
24556 int
24557 x_intersect_rectangles (r1, r2, result)
24558 XRectangle *r1, *r2, *result;
24559 {
24560 XRectangle *left, *right;
24561 XRectangle *upper, *lower;
24562 int intersection_p = 0;
24563
24564 /* Rearrange so that R1 is the left-most rectangle. */
24565 if (r1->x < r2->x)
24566 left = r1, right = r2;
24567 else
24568 left = r2, right = r1;
24569
24570 /* X0 of the intersection is right.x0, if this is inside R1,
24571 otherwise there is no intersection. */
24572 if (right->x <= left->x + left->width)
24573 {
24574 result->x = right->x;
24575
24576 /* The right end of the intersection is the minimum of the
24577 the right ends of left and right. */
24578 result->width = (min (left->x + left->width, right->x + right->width)
24579 - result->x);
24580
24581 /* Same game for Y. */
24582 if (r1->y < r2->y)
24583 upper = r1, lower = r2;
24584 else
24585 upper = r2, lower = r1;
24586
24587 /* The upper end of the intersection is lower.y0, if this is inside
24588 of upper. Otherwise, there is no intersection. */
24589 if (lower->y <= upper->y + upper->height)
24590 {
24591 result->y = lower->y;
24592
24593 /* The lower end of the intersection is the minimum of the lower
24594 ends of upper and lower. */
24595 result->height = (min (lower->y + lower->height,
24596 upper->y + upper->height)
24597 - result->y);
24598 intersection_p = 1;
24599 }
24600 }
24601
24602 return intersection_p;
24603 }
24604
24605 #endif /* HAVE_WINDOW_SYSTEM */
24606
24607 \f
24608 /***********************************************************************
24609 Initialization
24610 ***********************************************************************/
24611
24612 void
24613 syms_of_xdisp ()
24614 {
24615 Vwith_echo_area_save_vector = Qnil;
24616 staticpro (&Vwith_echo_area_save_vector);
24617
24618 Vmessage_stack = Qnil;
24619 staticpro (&Vmessage_stack);
24620
24621 Qinhibit_redisplay = intern ("inhibit-redisplay");
24622 staticpro (&Qinhibit_redisplay);
24623
24624 message_dolog_marker1 = Fmake_marker ();
24625 staticpro (&message_dolog_marker1);
24626 message_dolog_marker2 = Fmake_marker ();
24627 staticpro (&message_dolog_marker2);
24628 message_dolog_marker3 = Fmake_marker ();
24629 staticpro (&message_dolog_marker3);
24630
24631 #if GLYPH_DEBUG
24632 defsubr (&Sdump_frame_glyph_matrix);
24633 defsubr (&Sdump_glyph_matrix);
24634 defsubr (&Sdump_glyph_row);
24635 defsubr (&Sdump_tool_bar_row);
24636 defsubr (&Strace_redisplay);
24637 defsubr (&Strace_to_stderr);
24638 #endif
24639 #ifdef HAVE_WINDOW_SYSTEM
24640 defsubr (&Stool_bar_lines_needed);
24641 defsubr (&Slookup_image_map);
24642 #endif
24643 defsubr (&Sformat_mode_line);
24644 defsubr (&Sinvisible_p);
24645
24646 staticpro (&Qmenu_bar_update_hook);
24647 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24648
24649 staticpro (&Qoverriding_terminal_local_map);
24650 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24651
24652 staticpro (&Qoverriding_local_map);
24653 Qoverriding_local_map = intern ("overriding-local-map");
24654
24655 staticpro (&Qwindow_scroll_functions);
24656 Qwindow_scroll_functions = intern ("window-scroll-functions");
24657
24658 staticpro (&Qwindow_text_change_functions);
24659 Qwindow_text_change_functions = intern ("window-text-change-functions");
24660
24661 staticpro (&Qredisplay_end_trigger_functions);
24662 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24663
24664 staticpro (&Qinhibit_point_motion_hooks);
24665 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24666
24667 Qeval = intern ("eval");
24668 staticpro (&Qeval);
24669
24670 QCdata = intern (":data");
24671 staticpro (&QCdata);
24672 Qdisplay = intern ("display");
24673 staticpro (&Qdisplay);
24674 Qspace_width = intern ("space-width");
24675 staticpro (&Qspace_width);
24676 Qraise = intern ("raise");
24677 staticpro (&Qraise);
24678 Qslice = intern ("slice");
24679 staticpro (&Qslice);
24680 Qspace = intern ("space");
24681 staticpro (&Qspace);
24682 Qmargin = intern ("margin");
24683 staticpro (&Qmargin);
24684 Qpointer = intern ("pointer");
24685 staticpro (&Qpointer);
24686 Qleft_margin = intern ("left-margin");
24687 staticpro (&Qleft_margin);
24688 Qright_margin = intern ("right-margin");
24689 staticpro (&Qright_margin);
24690 Qcenter = intern ("center");
24691 staticpro (&Qcenter);
24692 Qline_height = intern ("line-height");
24693 staticpro (&Qline_height);
24694 QCalign_to = intern (":align-to");
24695 staticpro (&QCalign_to);
24696 QCrelative_width = intern (":relative-width");
24697 staticpro (&QCrelative_width);
24698 QCrelative_height = intern (":relative-height");
24699 staticpro (&QCrelative_height);
24700 QCeval = intern (":eval");
24701 staticpro (&QCeval);
24702 QCpropertize = intern (":propertize");
24703 staticpro (&QCpropertize);
24704 QCfile = intern (":file");
24705 staticpro (&QCfile);
24706 Qfontified = intern ("fontified");
24707 staticpro (&Qfontified);
24708 Qfontification_functions = intern ("fontification-functions");
24709 staticpro (&Qfontification_functions);
24710 Qtrailing_whitespace = intern ("trailing-whitespace");
24711 staticpro (&Qtrailing_whitespace);
24712 Qescape_glyph = intern ("escape-glyph");
24713 staticpro (&Qescape_glyph);
24714 Qnobreak_space = intern ("nobreak-space");
24715 staticpro (&Qnobreak_space);
24716 Qimage = intern ("image");
24717 staticpro (&Qimage);
24718 QCmap = intern (":map");
24719 staticpro (&QCmap);
24720 QCpointer = intern (":pointer");
24721 staticpro (&QCpointer);
24722 Qrect = intern ("rect");
24723 staticpro (&Qrect);
24724 Qcircle = intern ("circle");
24725 staticpro (&Qcircle);
24726 Qpoly = intern ("poly");
24727 staticpro (&Qpoly);
24728 Qmessage_truncate_lines = intern ("message-truncate-lines");
24729 staticpro (&Qmessage_truncate_lines);
24730 Qgrow_only = intern ("grow-only");
24731 staticpro (&Qgrow_only);
24732 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24733 staticpro (&Qinhibit_menubar_update);
24734 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24735 staticpro (&Qinhibit_eval_during_redisplay);
24736 Qposition = intern ("position");
24737 staticpro (&Qposition);
24738 Qbuffer_position = intern ("buffer-position");
24739 staticpro (&Qbuffer_position);
24740 Qobject = intern ("object");
24741 staticpro (&Qobject);
24742 Qbar = intern ("bar");
24743 staticpro (&Qbar);
24744 Qhbar = intern ("hbar");
24745 staticpro (&Qhbar);
24746 Qbox = intern ("box");
24747 staticpro (&Qbox);
24748 Qhollow = intern ("hollow");
24749 staticpro (&Qhollow);
24750 Qhand = intern ("hand");
24751 staticpro (&Qhand);
24752 Qarrow = intern ("arrow");
24753 staticpro (&Qarrow);
24754 Qtext = intern ("text");
24755 staticpro (&Qtext);
24756 Qrisky_local_variable = intern ("risky-local-variable");
24757 staticpro (&Qrisky_local_variable);
24758 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24759 staticpro (&Qinhibit_free_realized_faces);
24760
24761 list_of_error = Fcons (Fcons (intern ("error"),
24762 Fcons (intern ("void-variable"), Qnil)),
24763 Qnil);
24764 staticpro (&list_of_error);
24765
24766 Qlast_arrow_position = intern ("last-arrow-position");
24767 staticpro (&Qlast_arrow_position);
24768 Qlast_arrow_string = intern ("last-arrow-string");
24769 staticpro (&Qlast_arrow_string);
24770
24771 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24772 staticpro (&Qoverlay_arrow_string);
24773 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24774 staticpro (&Qoverlay_arrow_bitmap);
24775
24776 echo_buffer[0] = echo_buffer[1] = Qnil;
24777 staticpro (&echo_buffer[0]);
24778 staticpro (&echo_buffer[1]);
24779
24780 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24781 staticpro (&echo_area_buffer[0]);
24782 staticpro (&echo_area_buffer[1]);
24783
24784 Vmessages_buffer_name = build_string ("*Messages*");
24785 staticpro (&Vmessages_buffer_name);
24786
24787 mode_line_proptrans_alist = Qnil;
24788 staticpro (&mode_line_proptrans_alist);
24789 mode_line_string_list = Qnil;
24790 staticpro (&mode_line_string_list);
24791 mode_line_string_face = Qnil;
24792 staticpro (&mode_line_string_face);
24793 mode_line_string_face_prop = Qnil;
24794 staticpro (&mode_line_string_face_prop);
24795 Vmode_line_unwind_vector = Qnil;
24796 staticpro (&Vmode_line_unwind_vector);
24797
24798 help_echo_string = Qnil;
24799 staticpro (&help_echo_string);
24800 help_echo_object = Qnil;
24801 staticpro (&help_echo_object);
24802 help_echo_window = Qnil;
24803 staticpro (&help_echo_window);
24804 previous_help_echo_string = Qnil;
24805 staticpro (&previous_help_echo_string);
24806 help_echo_pos = -1;
24807
24808 #ifdef HAVE_WINDOW_SYSTEM
24809 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24810 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24811 For example, if a block cursor is over a tab, it will be drawn as
24812 wide as that tab on the display. */);
24813 x_stretch_cursor_p = 0;
24814 #endif
24815
24816 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24817 doc: /* *Non-nil means highlight trailing whitespace.
24818 The face used for trailing whitespace is `trailing-whitespace'. */);
24819 Vshow_trailing_whitespace = Qnil;
24820
24821 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24822 doc: /* *Control highlighting of nobreak space and soft hyphen.
24823 A value of t means highlight the character itself (for nobreak space,
24824 use face `nobreak-space').
24825 A value of nil means no highlighting.
24826 Other values mean display the escape glyph followed by an ordinary
24827 space or ordinary hyphen. */);
24828 Vnobreak_char_display = Qt;
24829
24830 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24831 doc: /* *The pointer shape to show in void text areas.
24832 A value of nil means to show the text pointer. Other options are `arrow',
24833 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24834 Vvoid_text_area_pointer = Qarrow;
24835
24836 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24837 doc: /* Non-nil means don't actually do any redisplay.
24838 This is used for internal purposes. */);
24839 Vinhibit_redisplay = Qnil;
24840
24841 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24842 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24843 Vglobal_mode_string = Qnil;
24844
24845 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24846 doc: /* Marker for where to display an arrow on top of the buffer text.
24847 This must be the beginning of a line in order to work.
24848 See also `overlay-arrow-string'. */);
24849 Voverlay_arrow_position = Qnil;
24850
24851 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24852 doc: /* String to display as an arrow in non-window frames.
24853 See also `overlay-arrow-position'. */);
24854 Voverlay_arrow_string = build_string ("=>");
24855
24856 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24857 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24858 The symbols on this list are examined during redisplay to determine
24859 where to display overlay arrows. */);
24860 Voverlay_arrow_variable_list
24861 = Fcons (intern ("overlay-arrow-position"), Qnil);
24862
24863 DEFVAR_INT ("scroll-step", &scroll_step,
24864 doc: /* *The number of lines to try scrolling a window by when point moves out.
24865 If that fails to bring point back on frame, point is centered instead.
24866 If this is zero, point is always centered after it moves off frame.
24867 If you want scrolling to always be a line at a time, you should set
24868 `scroll-conservatively' to a large value rather than set this to 1. */);
24869
24870 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24871 doc: /* *Scroll up to this many lines, to bring point back on screen.
24872 If point moves off-screen, redisplay will scroll by up to
24873 `scroll-conservatively' lines in order to bring point just barely
24874 onto the screen again. If that cannot be done, then redisplay
24875 recenters point as usual.
24876
24877 A value of zero means always recenter point if it moves off screen. */);
24878 scroll_conservatively = 0;
24879
24880 DEFVAR_INT ("scroll-margin", &scroll_margin,
24881 doc: /* *Number of lines of margin at the top and bottom of a window.
24882 Recenter the window whenever point gets within this many lines
24883 of the top or bottom of the window. */);
24884 scroll_margin = 0;
24885
24886 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24887 doc: /* Pixels per inch value for non-window system displays.
24888 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24889 Vdisplay_pixels_per_inch = make_float (72.0);
24890
24891 #if GLYPH_DEBUG
24892 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24893 #endif
24894
24895 DEFVAR_LISP ("truncate-partial-width-windows",
24896 &Vtruncate_partial_width_windows,
24897 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24898 For an integer value, truncate lines in each window with less than the
24899 full frame width, provided the window width is less than that integer;
24900 otherwise, respect the value of `truncate-lines'.
24901
24902 For any other non-nil value, truncate lines in all windows with
24903 less than the full frame width.
24904
24905 A value of nil means to respect the value of `truncate-lines'. */);
24906 Vtruncate_partial_width_windows = make_number (30);
24907
24908 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24909 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24910 Any other value means to use the appropriate face, `mode-line',
24911 `header-line', or `menu' respectively. */);
24912 mode_line_inverse_video = 1;
24913
24914 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24915 doc: /* *Maximum buffer size for which line number should be displayed.
24916 If the buffer is bigger than this, the line number does not appear
24917 in the mode line. A value of nil means no limit. */);
24918 Vline_number_display_limit = Qnil;
24919
24920 DEFVAR_INT ("line-number-display-limit-width",
24921 &line_number_display_limit_width,
24922 doc: /* *Maximum line width (in characters) for line number display.
24923 If the average length of the lines near point is bigger than this, then the
24924 line number may be omitted from the mode line. */);
24925 line_number_display_limit_width = 200;
24926
24927 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24928 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24929 highlight_nonselected_windows = 0;
24930
24931 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24932 doc: /* Non-nil if more than one frame is visible on this display.
24933 Minibuffer-only frames don't count, but iconified frames do.
24934 This variable is not guaranteed to be accurate except while processing
24935 `frame-title-format' and `icon-title-format'. */);
24936
24937 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24938 doc: /* Template for displaying the title bar of visible frames.
24939 \(Assuming the window manager supports this feature.)
24940
24941 This variable has the same structure as `mode-line-format', except that
24942 the %c and %l constructs are ignored. It is used only on frames for
24943 which no explicit name has been set \(see `modify-frame-parameters'). */);
24944
24945 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24946 doc: /* Template for displaying the title bar of an iconified frame.
24947 \(Assuming the window manager supports this feature.)
24948 This variable has the same structure as `mode-line-format' (which see),
24949 and is used only on frames for which no explicit name has been set
24950 \(see `modify-frame-parameters'). */);
24951 Vicon_title_format
24952 = Vframe_title_format
24953 = Fcons (intern ("multiple-frames"),
24954 Fcons (build_string ("%b"),
24955 Fcons (Fcons (empty_unibyte_string,
24956 Fcons (intern ("invocation-name"),
24957 Fcons (build_string ("@"),
24958 Fcons (intern ("system-name"),
24959 Qnil)))),
24960 Qnil)));
24961
24962 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24963 doc: /* Maximum number of lines to keep in the message log buffer.
24964 If nil, disable message logging. If t, log messages but don't truncate
24965 the buffer when it becomes large. */);
24966 Vmessage_log_max = make_number (100);
24967
24968 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24969 doc: /* Functions called before redisplay, if window sizes have changed.
24970 The value should be a list of functions that take one argument.
24971 Just before redisplay, for each frame, if any of its windows have changed
24972 size since the last redisplay, or have been split or deleted,
24973 all the functions in the list are called, with the frame as argument. */);
24974 Vwindow_size_change_functions = Qnil;
24975
24976 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24977 doc: /* List of functions to call before redisplaying a window with scrolling.
24978 Each function is called with two arguments, the window
24979 and its new display-start position. Note that the value of `window-end'
24980 is not valid when these functions are called. */);
24981 Vwindow_scroll_functions = Qnil;
24982
24983 DEFVAR_LISP ("window-text-change-functions",
24984 &Vwindow_text_change_functions,
24985 doc: /* Functions to call in redisplay when text in the window might change. */);
24986 Vwindow_text_change_functions = Qnil;
24987
24988 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24989 doc: /* Functions called when redisplay of a window reaches the end trigger.
24990 Each function is called with two arguments, the window and the end trigger value.
24991 See `set-window-redisplay-end-trigger'. */);
24992 Vredisplay_end_trigger_functions = Qnil;
24993
24994 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24995 doc: /* *Non-nil means autoselect window with mouse pointer.
24996 If nil, do not autoselect windows.
24997 A positive number means delay autoselection by that many seconds: a
24998 window is autoselected only after the mouse has remained in that
24999 window for the duration of the delay.
25000 A negative number has a similar effect, but causes windows to be
25001 autoselected only after the mouse has stopped moving. \(Because of
25002 the way Emacs compares mouse events, you will occasionally wait twice
25003 that time before the window gets selected.\)
25004 Any other value means to autoselect window instantaneously when the
25005 mouse pointer enters it.
25006
25007 Autoselection selects the minibuffer only if it is active, and never
25008 unselects the minibuffer if it is active.
25009
25010 When customizing this variable make sure that the actual value of
25011 `focus-follows-mouse' matches the behavior of your window manager. */);
25012 Vmouse_autoselect_window = Qnil;
25013
25014 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25015 doc: /* *Non-nil means automatically resize tool-bars.
25016 This dynamically changes the tool-bar's height to the minimum height
25017 that is needed to make all tool-bar items visible.
25018 If value is `grow-only', the tool-bar's height is only increased
25019 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25020 Vauto_resize_tool_bars = Qt;
25021
25022 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25023 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25024 auto_raise_tool_bar_buttons_p = 1;
25025
25026 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25027 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25028 make_cursor_line_fully_visible_p = 1;
25029
25030 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25031 doc: /* *Border below tool-bar in pixels.
25032 If an integer, use it as the height of the border.
25033 If it is one of `internal-border-width' or `border-width', use the
25034 value of the corresponding frame parameter.
25035 Otherwise, no border is added below the tool-bar. */);
25036 Vtool_bar_border = Qinternal_border_width;
25037
25038 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25039 doc: /* *Margin around tool-bar buttons in pixels.
25040 If an integer, use that for both horizontal and vertical margins.
25041 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25042 HORZ specifying the horizontal margin, and VERT specifying the
25043 vertical margin. */);
25044 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25045
25046 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25047 doc: /* *Relief thickness of tool-bar buttons. */);
25048 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25049
25050 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25051 doc: /* List of functions to call to fontify regions of text.
25052 Each function is called with one argument POS. Functions must
25053 fontify a region starting at POS in the current buffer, and give
25054 fontified regions the property `fontified'. */);
25055 Vfontification_functions = Qnil;
25056 Fmake_variable_buffer_local (Qfontification_functions);
25057
25058 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25059 &unibyte_display_via_language_environment,
25060 doc: /* *Non-nil means display unibyte text according to language environment.
25061 Specifically this means that unibyte non-ASCII characters
25062 are displayed by converting them to the equivalent multibyte characters
25063 according to the current language environment. As a result, they are
25064 displayed according to the current fontset. */);
25065 unibyte_display_via_language_environment = 0;
25066
25067 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25068 doc: /* *Maximum height for resizing mini-windows.
25069 If a float, it specifies a fraction of the mini-window frame's height.
25070 If an integer, it specifies a number of lines. */);
25071 Vmax_mini_window_height = make_float (0.25);
25072
25073 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25074 doc: /* *How to resize mini-windows.
25075 A value of nil means don't automatically resize mini-windows.
25076 A value of t means resize them to fit the text displayed in them.
25077 A value of `grow-only', the default, means let mini-windows grow
25078 only, until their display becomes empty, at which point the windows
25079 go back to their normal size. */);
25080 Vresize_mini_windows = Qgrow_only;
25081
25082 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25083 doc: /* Alist specifying how to blink the cursor off.
25084 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25085 `cursor-type' frame-parameter or variable equals ON-STATE,
25086 comparing using `equal', Emacs uses OFF-STATE to specify
25087 how to blink it off. ON-STATE and OFF-STATE are values for
25088 the `cursor-type' frame parameter.
25089
25090 If a frame's ON-STATE has no entry in this list,
25091 the frame's other specifications determine how to blink the cursor off. */);
25092 Vblink_cursor_alist = Qnil;
25093
25094 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25095 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25096 automatic_hscrolling_p = 1;
25097 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25098 staticpro (&Qauto_hscroll_mode);
25099
25100 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25101 doc: /* *How many columns away from the window edge point is allowed to get
25102 before automatic hscrolling will horizontally scroll the window. */);
25103 hscroll_margin = 5;
25104
25105 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25106 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25107 When point is less than `hscroll-margin' columns from the window
25108 edge, automatic hscrolling will scroll the window by the amount of columns
25109 determined by this variable. If its value is a positive integer, scroll that
25110 many columns. If it's a positive floating-point number, it specifies the
25111 fraction of the window's width to scroll. If it's nil or zero, point will be
25112 centered horizontally after the scroll. Any other value, including negative
25113 numbers, are treated as if the value were zero.
25114
25115 Automatic hscrolling always moves point outside the scroll margin, so if
25116 point was more than scroll step columns inside the margin, the window will
25117 scroll more than the value given by the scroll step.
25118
25119 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25120 and `scroll-right' overrides this variable's effect. */);
25121 Vhscroll_step = make_number (0);
25122
25123 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25124 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25125 Bind this around calls to `message' to let it take effect. */);
25126 message_truncate_lines = 0;
25127
25128 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25129 doc: /* Normal hook run to update the menu bar definitions.
25130 Redisplay runs this hook before it redisplays the menu bar.
25131 This is used to update submenus such as Buffers,
25132 whose contents depend on various data. */);
25133 Vmenu_bar_update_hook = Qnil;
25134
25135 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25136 doc: /* Frame for which we are updating a menu.
25137 The enable predicate for a menu binding should check this variable. */);
25138 Vmenu_updating_frame = Qnil;
25139
25140 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25141 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25142 inhibit_menubar_update = 0;
25143
25144 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25145 doc: /* Prefix added to the beginning of all continuation lines at display-time.
25146 May be a string, an image, or a stretch-glyph such as used by the
25147 `display' text-property.
25148
25149 This variable is overridden by any `wrap-prefix' text-property.
25150
25151 To add a prefix to non-continuation lines, use the `line-prefix' variable. */);
25152 Vwrap_prefix = Qnil;
25153 staticpro (&Qwrap_prefix);
25154 Qwrap_prefix = intern ("wrap-prefix");
25155 Fmake_variable_buffer_local (Qwrap_prefix);
25156
25157 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25158 doc: /* Prefix added to the beginning of all non-continuation lines at display-time.
25159 May be a string, an image, or a stretch-glyph such as used by the
25160 `display' text-property.
25161
25162 This variable is overridden by any `line-prefix' text-property.
25163
25164 To add a prefix to continuation lines, use the `wrap-prefix' variable. */);
25165 Vline_prefix = Qnil;
25166 staticpro (&Qline_prefix);
25167 Qline_prefix = intern ("line-prefix");
25168 Fmake_variable_buffer_local (Qline_prefix);
25169
25170 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25171 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25172 inhibit_eval_during_redisplay = 0;
25173
25174 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25175 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25176 inhibit_free_realized_faces = 0;
25177
25178 #if GLYPH_DEBUG
25179 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25180 doc: /* Inhibit try_window_id display optimization. */);
25181 inhibit_try_window_id = 0;
25182
25183 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25184 doc: /* Inhibit try_window_reusing display optimization. */);
25185 inhibit_try_window_reusing = 0;
25186
25187 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25188 doc: /* Inhibit try_cursor_movement display optimization. */);
25189 inhibit_try_cursor_movement = 0;
25190 #endif /* GLYPH_DEBUG */
25191
25192 DEFVAR_INT ("overline-margin", &overline_margin,
25193 doc: /* *Space between overline and text, in pixels.
25194 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25195 margin to the caracter height. */);
25196 overline_margin = 2;
25197
25198 DEFVAR_INT ("underline-minimum-offset",
25199 &underline_minimum_offset,
25200 doc: /* Minimum distance between baseline and underline.
25201 This can improve legibility of underlined text at small font sizes,
25202 particularly when using variable `x-use-underline-position-properties'
25203 with fonts that specify an UNDERLINE_POSITION relatively close to the
25204 baseline. The default value is 1. */);
25205 underline_minimum_offset = 1;
25206
25207 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25208 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25209 display_hourglass_p = 1;
25210
25211 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25212 doc: /* *Seconds to wait before displaying an hourglass pointer.
25213 Value must be an integer or float. */);
25214 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25215
25216 hourglass_atimer = NULL;
25217 hourglass_shown_p = 0;
25218 }
25219
25220
25221 /* Initialize this module when Emacs starts. */
25222
25223 void
25224 init_xdisp ()
25225 {
25226 Lisp_Object root_window;
25227 struct window *mini_w;
25228
25229 current_header_line_height = current_mode_line_height = -1;
25230
25231 CHARPOS (this_line_start_pos) = 0;
25232
25233 mini_w = XWINDOW (minibuf_window);
25234 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25235
25236 if (!noninteractive)
25237 {
25238 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25239 int i;
25240
25241 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25242 set_window_height (root_window,
25243 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25244 0);
25245 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25246 set_window_height (minibuf_window, 1, 0);
25247
25248 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25249 mini_w->total_cols = make_number (FRAME_COLS (f));
25250
25251 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25252 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25253 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25254
25255 /* The default ellipsis glyphs `...'. */
25256 for (i = 0; i < 3; ++i)
25257 default_invis_vector[i] = make_number ('.');
25258 }
25259
25260 {
25261 /* Allocate the buffer for frame titles.
25262 Also used for `format-mode-line'. */
25263 int size = 100;
25264 mode_line_noprop_buf = (char *) xmalloc (size);
25265 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25266 mode_line_noprop_ptr = mode_line_noprop_buf;
25267 mode_line_target = MODE_LINE_DISPLAY;
25268 }
25269
25270 help_echo_showing_p = 0;
25271 }
25272
25273 /* Since w32 does not support atimers, it defines its own implementation of
25274 the following three functions in w32fns.c. */
25275 #ifndef WINDOWSNT
25276
25277 /* Platform-independent portion of hourglass implementation. */
25278
25279 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25280 int
25281 hourglass_started ()
25282 {
25283 return hourglass_shown_p || hourglass_atimer != NULL;
25284 }
25285
25286 /* Cancel a currently active hourglass timer, and start a new one. */
25287 void
25288 start_hourglass ()
25289 {
25290 #if defined (HAVE_WINDOW_SYSTEM)
25291 EMACS_TIME delay;
25292 int secs, usecs = 0;
25293
25294 cancel_hourglass ();
25295
25296 if (INTEGERP (Vhourglass_delay)
25297 && XINT (Vhourglass_delay) > 0)
25298 secs = XFASTINT (Vhourglass_delay);
25299 else if (FLOATP (Vhourglass_delay)
25300 && XFLOAT_DATA (Vhourglass_delay) > 0)
25301 {
25302 Lisp_Object tem;
25303 tem = Ftruncate (Vhourglass_delay, Qnil);
25304 secs = XFASTINT (tem);
25305 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25306 }
25307 else
25308 secs = DEFAULT_HOURGLASS_DELAY;
25309
25310 EMACS_SET_SECS_USECS (delay, secs, usecs);
25311 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25312 show_hourglass, NULL);
25313 #endif
25314 }
25315
25316
25317 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25318 shown. */
25319 void
25320 cancel_hourglass ()
25321 {
25322 #if defined (HAVE_WINDOW_SYSTEM)
25323 if (hourglass_atimer)
25324 {
25325 cancel_atimer (hourglass_atimer);
25326 hourglass_atimer = NULL;
25327 }
25328
25329 if (hourglass_shown_p)
25330 hide_hourglass ();
25331 #endif
25332 }
25333 #endif /* ! WINDOWSNT */
25334
25335 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25336 (do not change this comment) */