(move_it_to): Backtrack if past the edge of a wrapped line.
[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 MAC_OS
200 #include "macterm.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) || defined (MAC_OS) \
212 || 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 \f
856 /* Function prototypes. */
857
858 static void setup_for_ellipsis P_ ((struct it *, int));
859 static void mark_window_display_accurate_1 P_ ((struct window *, int));
860 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
861 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
862 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
863 static int redisplay_mode_lines P_ ((Lisp_Object, int));
864 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
865
866 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
867
868 static void handle_line_prefix P_ ((struct it *));
869
870 #if 0
871 static int invisible_text_between_p P_ ((struct it *, int, int));
872 #endif
873
874 static void pint2str P_ ((char *, int, int));
875 static void pint2hrstr P_ ((char *, int, int));
876 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
877 struct text_pos));
878 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
879 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
880 static void store_mode_line_noprop_char P_ ((char));
881 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
882 static void x_consider_frame_title P_ ((Lisp_Object));
883 static void handle_stop P_ ((struct it *));
884 static int tool_bar_lines_needed P_ ((struct frame *, int *));
885 static int single_display_spec_intangible_p P_ ((Lisp_Object));
886 static void ensure_echo_area_buffers P_ ((void));
887 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
888 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
889 static int with_echo_area_buffer P_ ((struct window *, int,
890 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
891 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
892 static void clear_garbaged_frames P_ ((void));
893 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
894 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
895 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
896 static int display_echo_area P_ ((struct window *));
897 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
898 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
899 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
900 static int string_char_and_length P_ ((const unsigned char *, int, int *));
901 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
902 struct text_pos));
903 static int compute_window_start_on_continuation_line P_ ((struct window *));
904 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
905 static void insert_left_trunc_glyphs P_ ((struct it *));
906 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
907 Lisp_Object));
908 static void extend_face_to_end_of_line P_ ((struct it *));
909 static int append_space_for_newline P_ ((struct it *, int));
910 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
911 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
912 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
913 static int trailing_whitespace_p P_ ((int));
914 static int message_log_check_duplicate P_ ((int, int, int, int));
915 static void push_it P_ ((struct it *));
916 static void pop_it P_ ((struct it *));
917 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
918 static void select_frame_for_redisplay P_ ((Lisp_Object));
919 static void redisplay_internal P_ ((int));
920 static int echo_area_display P_ ((int));
921 static void redisplay_windows P_ ((Lisp_Object));
922 static void redisplay_window P_ ((Lisp_Object, int));
923 static Lisp_Object redisplay_window_error ();
924 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
925 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
926 static int update_menu_bar P_ ((struct frame *, int, int));
927 static int try_window_reusing_current_matrix P_ ((struct window *));
928 static int try_window_id P_ ((struct window *));
929 static int display_line P_ ((struct it *));
930 static int display_mode_lines P_ ((struct window *));
931 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
932 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
933 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
934 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
935 static void display_menu_bar P_ ((struct window *));
936 static int display_count_lines P_ ((int, int, int, int, int *));
937 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
938 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
939 static void compute_line_metrics P_ ((struct it *));
940 static void run_redisplay_end_trigger_hook P_ ((struct it *));
941 static int get_overlay_strings P_ ((struct it *, int));
942 static int get_overlay_strings_1 P_ ((struct it *, int, int));
943 static void next_overlay_string P_ ((struct it *));
944 static void reseat P_ ((struct it *, struct text_pos, int));
945 static void reseat_1 P_ ((struct it *, struct text_pos, int));
946 static void back_to_previous_visible_line_start P_ ((struct it *));
947 void reseat_at_previous_visible_line_start P_ ((struct it *));
948 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
949 static int next_element_from_ellipsis P_ ((struct it *));
950 static int next_element_from_display_vector P_ ((struct it *));
951 static int next_element_from_string P_ ((struct it *));
952 static int next_element_from_c_string P_ ((struct it *));
953 static int next_element_from_buffer P_ ((struct it *));
954 static int next_element_from_composition P_ ((struct it *));
955 static int next_element_from_image P_ ((struct it *));
956 static int next_element_from_stretch P_ ((struct it *));
957 static void load_overlay_strings P_ ((struct it *, int));
958 static int init_from_display_pos P_ ((struct it *, struct window *,
959 struct display_pos *));
960 static void reseat_to_string P_ ((struct it *, unsigned char *,
961 Lisp_Object, int, int, int, int));
962 static enum move_it_result
963 move_it_in_display_line_to (struct it *, EMACS_INT, int,
964 enum move_operation_enum);
965 void move_it_vertically_backward P_ ((struct it *, int));
966 static void init_to_row_start P_ ((struct it *, struct window *,
967 struct glyph_row *));
968 static int init_to_row_end P_ ((struct it *, struct window *,
969 struct glyph_row *));
970 static void back_to_previous_line_start P_ ((struct it *));
971 static int forward_to_next_line_start P_ ((struct it *, int *));
972 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
973 Lisp_Object, int));
974 static struct text_pos string_pos P_ ((int, Lisp_Object));
975 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
976 static int number_of_chars P_ ((unsigned char *, int));
977 static void compute_stop_pos P_ ((struct it *));
978 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
979 Lisp_Object));
980 static int face_before_or_after_it_pos P_ ((struct it *, int));
981 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
982 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
983 Lisp_Object, Lisp_Object,
984 struct text_pos *, int));
985 static int underlying_face_id P_ ((struct it *));
986 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
987 struct window *));
988
989 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
990 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
991
992 #ifdef HAVE_WINDOW_SYSTEM
993
994 static void update_tool_bar P_ ((struct frame *, int));
995 static void build_desired_tool_bar_string P_ ((struct frame *f));
996 static int redisplay_tool_bar P_ ((struct frame *));
997 static void display_tool_bar_line P_ ((struct it *, int));
998 static void notice_overwritten_cursor P_ ((struct window *,
999 enum glyph_row_area,
1000 int, int, int, int));
1001
1002
1003
1004 #endif /* HAVE_WINDOW_SYSTEM */
1005
1006 \f
1007 /***********************************************************************
1008 Window display dimensions
1009 ***********************************************************************/
1010
1011 /* Return the bottom boundary y-position for text lines in window W.
1012 This is the first y position at which a line cannot start.
1013 It is relative to the top of the window.
1014
1015 This is the height of W minus the height of a mode line, if any. */
1016
1017 INLINE int
1018 window_text_bottom_y (w)
1019 struct window *w;
1020 {
1021 int height = WINDOW_TOTAL_HEIGHT (w);
1022
1023 if (WINDOW_WANTS_MODELINE_P (w))
1024 height -= CURRENT_MODE_LINE_HEIGHT (w);
1025 return height;
1026 }
1027
1028 /* Return the pixel width of display area AREA of window W. AREA < 0
1029 means return the total width of W, not including fringes to
1030 the left and right of the window. */
1031
1032 INLINE int
1033 window_box_width (w, area)
1034 struct window *w;
1035 int area;
1036 {
1037 int cols = XFASTINT (w->total_cols);
1038 int pixels = 0;
1039
1040 if (!w->pseudo_window_p)
1041 {
1042 cols -= WINDOW_SCROLL_BAR_COLS (w);
1043
1044 if (area == TEXT_AREA)
1045 {
1046 if (INTEGERP (w->left_margin_cols))
1047 cols -= XFASTINT (w->left_margin_cols);
1048 if (INTEGERP (w->right_margin_cols))
1049 cols -= XFASTINT (w->right_margin_cols);
1050 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1051 }
1052 else if (area == LEFT_MARGIN_AREA)
1053 {
1054 cols = (INTEGERP (w->left_margin_cols)
1055 ? XFASTINT (w->left_margin_cols) : 0);
1056 pixels = 0;
1057 }
1058 else if (area == RIGHT_MARGIN_AREA)
1059 {
1060 cols = (INTEGERP (w->right_margin_cols)
1061 ? XFASTINT (w->right_margin_cols) : 0);
1062 pixels = 0;
1063 }
1064 }
1065
1066 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1067 }
1068
1069
1070 /* Return the pixel height of the display area of window W, not
1071 including mode lines of W, if any. */
1072
1073 INLINE int
1074 window_box_height (w)
1075 struct window *w;
1076 {
1077 struct frame *f = XFRAME (w->frame);
1078 int height = WINDOW_TOTAL_HEIGHT (w);
1079
1080 xassert (height >= 0);
1081
1082 /* Note: the code below that determines the mode-line/header-line
1083 height is essentially the same as that contained in the macro
1084 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1085 the appropriate glyph row has its `mode_line_p' flag set,
1086 and if it doesn't, uses estimate_mode_line_height instead. */
1087
1088 if (WINDOW_WANTS_MODELINE_P (w))
1089 {
1090 struct glyph_row *ml_row
1091 = (w->current_matrix && w->current_matrix->rows
1092 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1093 : 0);
1094 if (ml_row && ml_row->mode_line_p)
1095 height -= ml_row->height;
1096 else
1097 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1098 }
1099
1100 if (WINDOW_WANTS_HEADER_LINE_P (w))
1101 {
1102 struct glyph_row *hl_row
1103 = (w->current_matrix && w->current_matrix->rows
1104 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1105 : 0);
1106 if (hl_row && hl_row->mode_line_p)
1107 height -= hl_row->height;
1108 else
1109 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1110 }
1111
1112 /* With a very small font and a mode-line that's taller than
1113 default, we might end up with a negative height. */
1114 return max (0, height);
1115 }
1116
1117 /* Return the window-relative coordinate of the left edge of display
1118 area AREA of window W. AREA < 0 means return the left edge of the
1119 whole window, to the right of the left fringe of W. */
1120
1121 INLINE int
1122 window_box_left_offset (w, area)
1123 struct window *w;
1124 int area;
1125 {
1126 int x;
1127
1128 if (w->pseudo_window_p)
1129 return 0;
1130
1131 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1132
1133 if (area == TEXT_AREA)
1134 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1135 + window_box_width (w, LEFT_MARGIN_AREA));
1136 else if (area == RIGHT_MARGIN_AREA)
1137 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1138 + window_box_width (w, LEFT_MARGIN_AREA)
1139 + window_box_width (w, TEXT_AREA)
1140 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1141 ? 0
1142 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1143 else if (area == LEFT_MARGIN_AREA
1144 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1145 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1146
1147 return x;
1148 }
1149
1150
1151 /* Return the window-relative coordinate of the right edge of display
1152 area AREA of window W. AREA < 0 means return the left edge of the
1153 whole window, to the left of the right fringe of W. */
1154
1155 INLINE int
1156 window_box_right_offset (w, area)
1157 struct window *w;
1158 int area;
1159 {
1160 return window_box_left_offset (w, area) + window_box_width (w, area);
1161 }
1162
1163 /* Return the frame-relative coordinate of the left edge of display
1164 area AREA of window W. AREA < 0 means return the left edge of the
1165 whole window, to the right of the left fringe of W. */
1166
1167 INLINE int
1168 window_box_left (w, area)
1169 struct window *w;
1170 int area;
1171 {
1172 struct frame *f = XFRAME (w->frame);
1173 int x;
1174
1175 if (w->pseudo_window_p)
1176 return FRAME_INTERNAL_BORDER_WIDTH (f);
1177
1178 x = (WINDOW_LEFT_EDGE_X (w)
1179 + window_box_left_offset (w, area));
1180
1181 return x;
1182 }
1183
1184
1185 /* Return the frame-relative coordinate of the right edge of display
1186 area AREA of window W. AREA < 0 means return the left edge of the
1187 whole window, to the left of the right fringe of W. */
1188
1189 INLINE int
1190 window_box_right (w, area)
1191 struct window *w;
1192 int area;
1193 {
1194 return window_box_left (w, area) + window_box_width (w, area);
1195 }
1196
1197 /* Get the bounding box of the display area AREA of window W, without
1198 mode lines, in frame-relative coordinates. AREA < 0 means the
1199 whole window, not including the left and right fringes of
1200 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1201 coordinates of the upper-left corner of the box. Return in
1202 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1203
1204 INLINE void
1205 window_box (w, area, box_x, box_y, box_width, box_height)
1206 struct window *w;
1207 int area;
1208 int *box_x, *box_y, *box_width, *box_height;
1209 {
1210 if (box_width)
1211 *box_width = window_box_width (w, area);
1212 if (box_height)
1213 *box_height = window_box_height (w);
1214 if (box_x)
1215 *box_x = window_box_left (w, area);
1216 if (box_y)
1217 {
1218 *box_y = WINDOW_TOP_EDGE_Y (w);
1219 if (WINDOW_WANTS_HEADER_LINE_P (w))
1220 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1221 }
1222 }
1223
1224
1225 /* Get the bounding box of the display area AREA of window W, without
1226 mode lines. AREA < 0 means the whole window, not including the
1227 left and right fringe of the window. Return in *TOP_LEFT_X
1228 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1229 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1230 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1231 box. */
1232
1233 INLINE void
1234 window_box_edges (w, area, top_left_x, top_left_y,
1235 bottom_right_x, bottom_right_y)
1236 struct window *w;
1237 int area;
1238 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1239 {
1240 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1241 bottom_right_y);
1242 *bottom_right_x += *top_left_x;
1243 *bottom_right_y += *top_left_y;
1244 }
1245
1246
1247 \f
1248 /***********************************************************************
1249 Utilities
1250 ***********************************************************************/
1251
1252 /* Return the bottom y-position of the line the iterator IT is in.
1253 This can modify IT's settings. */
1254
1255 int
1256 line_bottom_y (it)
1257 struct it *it;
1258 {
1259 int line_height = it->max_ascent + it->max_descent;
1260 int line_top_y = it->current_y;
1261
1262 if (line_height == 0)
1263 {
1264 if (last_height)
1265 line_height = last_height;
1266 else if (IT_CHARPOS (*it) < ZV)
1267 {
1268 move_it_by_lines (it, 1, 1);
1269 line_height = (it->max_ascent || it->max_descent
1270 ? it->max_ascent + it->max_descent
1271 : last_height);
1272 }
1273 else
1274 {
1275 struct glyph_row *row = it->glyph_row;
1276
1277 /* Use the default character height. */
1278 it->glyph_row = NULL;
1279 it->what = IT_CHARACTER;
1280 it->c = ' ';
1281 it->len = 1;
1282 PRODUCE_GLYPHS (it);
1283 line_height = it->ascent + it->descent;
1284 it->glyph_row = row;
1285 }
1286 }
1287
1288 return line_top_y + line_height;
1289 }
1290
1291
1292 /* Return 1 if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1297
1298 int
1299 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1300 struct window *w;
1301 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1302 {
1303 struct it it;
1304 struct text_pos top;
1305 int visible_p = 0;
1306 struct buffer *old_buffer = NULL;
1307
1308 if (noninteractive)
1309 return visible_p;
1310
1311 if (XBUFFER (w->buffer) != current_buffer)
1312 {
1313 old_buffer = current_buffer;
1314 set_buffer_internal_1 (XBUFFER (w->buffer));
1315 }
1316
1317 SET_TEXT_POS_FROM_MARKER (top, w->start);
1318
1319 /* Compute exact mode line heights. */
1320 if (WINDOW_WANTS_MODELINE_P (w))
1321 current_mode_line_height
1322 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1323 current_buffer->mode_line_format);
1324
1325 if (WINDOW_WANTS_HEADER_LINE_P (w))
1326 current_header_line_height
1327 = display_mode_line (w, HEADER_LINE_FACE_ID,
1328 current_buffer->header_line_format);
1329
1330 start_display (&it, w, top);
1331 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1332 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1333
1334 /* Note that we may overshoot because of invisible text. */
1335 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1336 {
1337 int top_x = it.current_x;
1338 int top_y = it.current_y;
1339 int bottom_y = (last_height = 0, line_bottom_y (&it));
1340 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1341
1342 if (top_y < window_top_y)
1343 visible_p = bottom_y > window_top_y;
1344 else if (top_y < it.last_visible_y)
1345 visible_p = 1;
1346 if (visible_p)
1347 {
1348 if (it.method == GET_FROM_BUFFER)
1349 {
1350 Lisp_Object window, prop;
1351
1352 XSETWINDOW (window, w);
1353 prop = Fget_char_property (make_number (it.position.charpos),
1354 Qinvisible, window);
1355
1356 /* If charpos coincides with invisible text covered with an
1357 ellipsis, use the first glyph of the ellipsis to compute
1358 the pixel positions. */
1359 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1360 {
1361 struct glyph_row *row = it.glyph_row;
1362 struct glyph *glyph = row->glyphs[TEXT_AREA];
1363 struct glyph *end = glyph + row->used[TEXT_AREA];
1364 int x = row->x;
1365
1366 for (; glyph < end && glyph->charpos < charpos; glyph++)
1367 x += glyph->pixel_width;
1368
1369 top_x = x;
1370 }
1371 }
1372
1373 *x = top_x;
1374 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1375 *rtop = max (0, window_top_y - top_y);
1376 *rbot = max (0, bottom_y - it.last_visible_y);
1377 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1378 - max (top_y, window_top_y)));
1379 *vpos = it.vpos;
1380 }
1381 }
1382 else
1383 {
1384 struct it it2;
1385
1386 it2 = it;
1387 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1388 move_it_by_lines (&it, 1, 0);
1389 if (charpos < IT_CHARPOS (it)
1390 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1391 {
1392 visible_p = 1;
1393 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1394 *x = it2.current_x;
1395 *y = it2.current_y + it2.max_ascent - it2.ascent;
1396 *rtop = max (0, -it2.current_y);
1397 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1398 - it.last_visible_y));
1399 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1400 it.last_visible_y)
1401 - max (it2.current_y,
1402 WINDOW_HEADER_LINE_HEIGHT (w))));
1403 *vpos = it2.vpos;
1404 }
1405 }
1406
1407 if (old_buffer)
1408 set_buffer_internal_1 (old_buffer);
1409
1410 current_header_line_height = current_mode_line_height = -1;
1411
1412 if (visible_p && XFASTINT (w->hscroll) > 0)
1413 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1414
1415 #if 0
1416 /* Debugging code. */
1417 if (visible_p)
1418 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1419 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1420 else
1421 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1422 #endif
1423
1424 return visible_p;
1425 }
1426
1427
1428 /* Return the next character from STR which is MAXLEN bytes long.
1429 Return in *LEN the length of the character. This is like
1430 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1431 we find one, we return a `?', but with the length of the invalid
1432 character. */
1433
1434 static INLINE int
1435 string_char_and_length (str, maxlen, len)
1436 const unsigned char *str;
1437 int maxlen, *len;
1438 {
1439 int c;
1440
1441 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1442 if (!CHAR_VALID_P (c, 1))
1443 /* We may not change the length here because other places in Emacs
1444 don't use this function, i.e. they silently accept invalid
1445 characters. */
1446 c = '?';
1447
1448 return c;
1449 }
1450
1451
1452
1453 /* Given a position POS containing a valid character and byte position
1454 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1455
1456 static struct text_pos
1457 string_pos_nchars_ahead (pos, string, nchars)
1458 struct text_pos pos;
1459 Lisp_Object string;
1460 int nchars;
1461 {
1462 xassert (STRINGP (string) && nchars >= 0);
1463
1464 if (STRING_MULTIBYTE (string))
1465 {
1466 int rest = SBYTES (string) - BYTEPOS (pos);
1467 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1468 int len;
1469
1470 while (nchars--)
1471 {
1472 string_char_and_length (p, rest, &len);
1473 p += len, rest -= len;
1474 xassert (rest >= 0);
1475 CHARPOS (pos) += 1;
1476 BYTEPOS (pos) += len;
1477 }
1478 }
1479 else
1480 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1481
1482 return pos;
1483 }
1484
1485
1486 /* Value is the text position, i.e. character and byte position,
1487 for character position CHARPOS in STRING. */
1488
1489 static INLINE struct text_pos
1490 string_pos (charpos, string)
1491 int charpos;
1492 Lisp_Object string;
1493 {
1494 struct text_pos pos;
1495 xassert (STRINGP (string));
1496 xassert (charpos >= 0);
1497 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1498 return pos;
1499 }
1500
1501
1502 /* Value is a text position, i.e. character and byte position, for
1503 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1504 means recognize multibyte characters. */
1505
1506 static struct text_pos
1507 c_string_pos (charpos, s, multibyte_p)
1508 int charpos;
1509 unsigned char *s;
1510 int multibyte_p;
1511 {
1512 struct text_pos pos;
1513
1514 xassert (s != NULL);
1515 xassert (charpos >= 0);
1516
1517 if (multibyte_p)
1518 {
1519 int rest = strlen (s), len;
1520
1521 SET_TEXT_POS (pos, 0, 0);
1522 while (charpos--)
1523 {
1524 string_char_and_length (s, rest, &len);
1525 s += len, rest -= len;
1526 xassert (rest >= 0);
1527 CHARPOS (pos) += 1;
1528 BYTEPOS (pos) += len;
1529 }
1530 }
1531 else
1532 SET_TEXT_POS (pos, charpos, charpos);
1533
1534 return pos;
1535 }
1536
1537
1538 /* Value is the number of characters in C string S. MULTIBYTE_P
1539 non-zero means recognize multibyte characters. */
1540
1541 static int
1542 number_of_chars (s, multibyte_p)
1543 unsigned char *s;
1544 int multibyte_p;
1545 {
1546 int nchars;
1547
1548 if (multibyte_p)
1549 {
1550 int rest = strlen (s), len;
1551 unsigned char *p = (unsigned char *) s;
1552
1553 for (nchars = 0; rest > 0; ++nchars)
1554 {
1555 string_char_and_length (p, rest, &len);
1556 rest -= len, p += len;
1557 }
1558 }
1559 else
1560 nchars = strlen (s);
1561
1562 return nchars;
1563 }
1564
1565
1566 /* Compute byte position NEWPOS->bytepos corresponding to
1567 NEWPOS->charpos. POS is a known position in string STRING.
1568 NEWPOS->charpos must be >= POS.charpos. */
1569
1570 static void
1571 compute_string_pos (newpos, pos, string)
1572 struct text_pos *newpos, pos;
1573 Lisp_Object string;
1574 {
1575 xassert (STRINGP (string));
1576 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1577
1578 if (STRING_MULTIBYTE (string))
1579 *newpos = string_pos_nchars_ahead (pos, string,
1580 CHARPOS (*newpos) - CHARPOS (pos));
1581 else
1582 BYTEPOS (*newpos) = CHARPOS (*newpos);
1583 }
1584
1585 /* EXPORT:
1586 Return an estimation of the pixel height of mode or top lines on
1587 frame F. FACE_ID specifies what line's height to estimate. */
1588
1589 int
1590 estimate_mode_line_height (f, face_id)
1591 struct frame *f;
1592 enum face_id face_id;
1593 {
1594 #ifdef HAVE_WINDOW_SYSTEM
1595 if (FRAME_WINDOW_P (f))
1596 {
1597 int height = FONT_HEIGHT (FRAME_FONT (f));
1598
1599 /* This function is called so early when Emacs starts that the face
1600 cache and mode line face are not yet initialized. */
1601 if (FRAME_FACE_CACHE (f))
1602 {
1603 struct face *face = FACE_FROM_ID (f, face_id);
1604 if (face)
1605 {
1606 if (face->font)
1607 height = FONT_HEIGHT (face->font);
1608 if (face->box_line_width > 0)
1609 height += 2 * face->box_line_width;
1610 }
1611 }
1612
1613 return height;
1614 }
1615 #endif
1616
1617 return 1;
1618 }
1619
1620 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1621 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1622 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1623 not force the value into range. */
1624
1625 void
1626 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1627 FRAME_PTR f;
1628 register int pix_x, pix_y;
1629 int *x, *y;
1630 NativeRectangle *bounds;
1631 int noclip;
1632 {
1633
1634 #ifdef HAVE_WINDOW_SYSTEM
1635 if (FRAME_WINDOW_P (f))
1636 {
1637 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1638 even for negative values. */
1639 if (pix_x < 0)
1640 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1641 if (pix_y < 0)
1642 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1643
1644 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1645 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1646
1647 if (bounds)
1648 STORE_NATIVE_RECT (*bounds,
1649 FRAME_COL_TO_PIXEL_X (f, pix_x),
1650 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1651 FRAME_COLUMN_WIDTH (f) - 1,
1652 FRAME_LINE_HEIGHT (f) - 1);
1653
1654 if (!noclip)
1655 {
1656 if (pix_x < 0)
1657 pix_x = 0;
1658 else if (pix_x > FRAME_TOTAL_COLS (f))
1659 pix_x = FRAME_TOTAL_COLS (f);
1660
1661 if (pix_y < 0)
1662 pix_y = 0;
1663 else if (pix_y > FRAME_LINES (f))
1664 pix_y = FRAME_LINES (f);
1665 }
1666 }
1667 #endif
1668
1669 *x = pix_x;
1670 *y = pix_y;
1671 }
1672
1673
1674 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1675 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1676 can't tell the positions because W's display is not up to date,
1677 return 0. */
1678
1679 int
1680 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1681 struct window *w;
1682 int hpos, vpos;
1683 int *frame_x, *frame_y;
1684 {
1685 #ifdef HAVE_WINDOW_SYSTEM
1686 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1687 {
1688 int success_p;
1689
1690 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1691 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1692
1693 if (display_completed)
1694 {
1695 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1696 struct glyph *glyph = row->glyphs[TEXT_AREA];
1697 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1698
1699 hpos = row->x;
1700 vpos = row->y;
1701 while (glyph < end)
1702 {
1703 hpos += glyph->pixel_width;
1704 ++glyph;
1705 }
1706
1707 /* If first glyph is partially visible, its first visible position is still 0. */
1708 if (hpos < 0)
1709 hpos = 0;
1710
1711 success_p = 1;
1712 }
1713 else
1714 {
1715 hpos = vpos = 0;
1716 success_p = 0;
1717 }
1718
1719 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1720 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1721 return success_p;
1722 }
1723 #endif
1724
1725 *frame_x = hpos;
1726 *frame_y = vpos;
1727 return 1;
1728 }
1729
1730
1731 #ifdef HAVE_WINDOW_SYSTEM
1732
1733 /* Find the glyph under window-relative coordinates X/Y in window W.
1734 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1735 strings. Return in *HPOS and *VPOS the row and column number of
1736 the glyph found. Return in *AREA the glyph area containing X.
1737 Value is a pointer to the glyph found or null if X/Y is not on
1738 text, or we can't tell because W's current matrix is not up to
1739 date. */
1740
1741 #ifndef HAVE_CARBON
1742 static
1743 #endif
1744 struct glyph *
1745 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1746 struct window *w;
1747 int x, y;
1748 int *hpos, *vpos, *dx, *dy, *area;
1749 {
1750 struct glyph *glyph, *end;
1751 struct glyph_row *row = NULL;
1752 int x0, i;
1753
1754 /* Find row containing Y. Give up if some row is not enabled. */
1755 for (i = 0; i < w->current_matrix->nrows; ++i)
1756 {
1757 row = MATRIX_ROW (w->current_matrix, i);
1758 if (!row->enabled_p)
1759 return NULL;
1760 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1761 break;
1762 }
1763
1764 *vpos = i;
1765 *hpos = 0;
1766
1767 /* Give up if Y is not in the window. */
1768 if (i == w->current_matrix->nrows)
1769 return NULL;
1770
1771 /* Get the glyph area containing X. */
1772 if (w->pseudo_window_p)
1773 {
1774 *area = TEXT_AREA;
1775 x0 = 0;
1776 }
1777 else
1778 {
1779 if (x < window_box_left_offset (w, TEXT_AREA))
1780 {
1781 *area = LEFT_MARGIN_AREA;
1782 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1783 }
1784 else if (x < window_box_right_offset (w, TEXT_AREA))
1785 {
1786 *area = TEXT_AREA;
1787 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1788 }
1789 else
1790 {
1791 *area = RIGHT_MARGIN_AREA;
1792 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1793 }
1794 }
1795
1796 /* Find glyph containing X. */
1797 glyph = row->glyphs[*area];
1798 end = glyph + row->used[*area];
1799 x -= x0;
1800 while (glyph < end && x >= glyph->pixel_width)
1801 {
1802 x -= glyph->pixel_width;
1803 ++glyph;
1804 }
1805
1806 if (glyph == end)
1807 return NULL;
1808
1809 if (dx)
1810 {
1811 *dx = x;
1812 *dy = y - (row->y + row->ascent - glyph->ascent);
1813 }
1814
1815 *hpos = glyph - row->glyphs[*area];
1816 return glyph;
1817 }
1818
1819
1820 /* EXPORT:
1821 Convert frame-relative x/y to coordinates relative to window W.
1822 Takes pseudo-windows into account. */
1823
1824 void
1825 frame_to_window_pixel_xy (w, x, y)
1826 struct window *w;
1827 int *x, *y;
1828 {
1829 if (w->pseudo_window_p)
1830 {
1831 /* A pseudo-window is always full-width, and starts at the
1832 left edge of the frame, plus a frame border. */
1833 struct frame *f = XFRAME (w->frame);
1834 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1835 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1836 }
1837 else
1838 {
1839 *x -= WINDOW_LEFT_EDGE_X (w);
1840 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1841 }
1842 }
1843
1844 /* EXPORT:
1845 Return in RECTS[] at most N clipping rectangles for glyph string S.
1846 Return the number of stored rectangles. */
1847
1848 int
1849 get_glyph_string_clip_rects (s, rects, n)
1850 struct glyph_string *s;
1851 NativeRectangle *rects;
1852 int n;
1853 {
1854 XRectangle r;
1855
1856 if (n <= 0)
1857 return 0;
1858
1859 if (s->row->full_width_p)
1860 {
1861 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1862 r.x = WINDOW_LEFT_EDGE_X (s->w);
1863 r.width = WINDOW_TOTAL_WIDTH (s->w);
1864
1865 /* Unless displaying a mode or menu bar line, which are always
1866 fully visible, clip to the visible part of the row. */
1867 if (s->w->pseudo_window_p)
1868 r.height = s->row->visible_height;
1869 else
1870 r.height = s->height;
1871 }
1872 else
1873 {
1874 /* This is a text line that may be partially visible. */
1875 r.x = window_box_left (s->w, s->area);
1876 r.width = window_box_width (s->w, s->area);
1877 r.height = s->row->visible_height;
1878 }
1879
1880 if (s->clip_head)
1881 if (r.x < s->clip_head->x)
1882 {
1883 if (r.width >= s->clip_head->x - r.x)
1884 r.width -= s->clip_head->x - r.x;
1885 else
1886 r.width = 0;
1887 r.x = s->clip_head->x;
1888 }
1889 if (s->clip_tail)
1890 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1891 {
1892 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1893 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1894 else
1895 r.width = 0;
1896 }
1897
1898 /* If S draws overlapping rows, it's sufficient to use the top and
1899 bottom of the window for clipping because this glyph string
1900 intentionally draws over other lines. */
1901 if (s->for_overlaps)
1902 {
1903 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1904 r.height = window_text_bottom_y (s->w) - r.y;
1905
1906 /* Alas, the above simple strategy does not work for the
1907 environments with anti-aliased text: if the same text is
1908 drawn onto the same place multiple times, it gets thicker.
1909 If the overlap we are processing is for the erased cursor, we
1910 take the intersection with the rectagle of the cursor. */
1911 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1912 {
1913 XRectangle rc, r_save = r;
1914
1915 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1916 rc.y = s->w->phys_cursor.y;
1917 rc.width = s->w->phys_cursor_width;
1918 rc.height = s->w->phys_cursor_height;
1919
1920 x_intersect_rectangles (&r_save, &rc, &r);
1921 }
1922 }
1923 else
1924 {
1925 /* Don't use S->y for clipping because it doesn't take partially
1926 visible lines into account. For example, it can be negative for
1927 partially visible lines at the top of a window. */
1928 if (!s->row->full_width_p
1929 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1930 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1931 else
1932 r.y = max (0, s->row->y);
1933
1934 /* If drawing a tool-bar window, draw it over the internal border
1935 at the top of the window. */
1936 if (WINDOWP (s->f->tool_bar_window)
1937 && s->w == XWINDOW (s->f->tool_bar_window))
1938 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1939 }
1940
1941 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1942
1943 /* If drawing the cursor, don't let glyph draw outside its
1944 advertised boundaries. Cleartype does this under some circumstances. */
1945 if (s->hl == DRAW_CURSOR)
1946 {
1947 struct glyph *glyph = s->first_glyph;
1948 int height, max_y;
1949
1950 if (s->x > r.x)
1951 {
1952 r.width -= s->x - r.x;
1953 r.x = s->x;
1954 }
1955 r.width = min (r.width, glyph->pixel_width);
1956
1957 /* If r.y is below window bottom, ensure that we still see a cursor. */
1958 height = min (glyph->ascent + glyph->descent,
1959 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1960 max_y = window_text_bottom_y (s->w) - height;
1961 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1962 if (s->ybase - glyph->ascent > max_y)
1963 {
1964 r.y = max_y;
1965 r.height = height;
1966 }
1967 else
1968 {
1969 /* Don't draw cursor glyph taller than our actual glyph. */
1970 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1971 if (height < r.height)
1972 {
1973 max_y = r.y + r.height;
1974 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1975 r.height = min (max_y - r.y, height);
1976 }
1977 }
1978 }
1979
1980 if (s->row->clip)
1981 {
1982 XRectangle r_save = r;
1983
1984 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1985 r.width = 0;
1986 }
1987
1988 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1989 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1990 {
1991 #ifdef CONVERT_FROM_XRECT
1992 CONVERT_FROM_XRECT (r, *rects);
1993 #else
1994 *rects = r;
1995 #endif
1996 return 1;
1997 }
1998 else
1999 {
2000 /* If we are processing overlapping and allowed to return
2001 multiple clipping rectangles, we exclude the row of the glyph
2002 string from the clipping rectangle. This is to avoid drawing
2003 the same text on the environment with anti-aliasing. */
2004 #ifdef CONVERT_FROM_XRECT
2005 XRectangle rs[2];
2006 #else
2007 XRectangle *rs = rects;
2008 #endif
2009 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2010
2011 if (s->for_overlaps & OVERLAPS_PRED)
2012 {
2013 rs[i] = r;
2014 if (r.y + r.height > row_y)
2015 {
2016 if (r.y < row_y)
2017 rs[i].height = row_y - r.y;
2018 else
2019 rs[i].height = 0;
2020 }
2021 i++;
2022 }
2023 if (s->for_overlaps & OVERLAPS_SUCC)
2024 {
2025 rs[i] = r;
2026 if (r.y < row_y + s->row->visible_height)
2027 {
2028 if (r.y + r.height > row_y + s->row->visible_height)
2029 {
2030 rs[i].y = row_y + s->row->visible_height;
2031 rs[i].height = r.y + r.height - rs[i].y;
2032 }
2033 else
2034 rs[i].height = 0;
2035 }
2036 i++;
2037 }
2038
2039 n = i;
2040 #ifdef CONVERT_FROM_XRECT
2041 for (i = 0; i < n; i++)
2042 CONVERT_FROM_XRECT (rs[i], rects[i]);
2043 #endif
2044 return n;
2045 }
2046 }
2047
2048 /* EXPORT:
2049 Return in *NR the clipping rectangle for glyph string S. */
2050
2051 void
2052 get_glyph_string_clip_rect (s, nr)
2053 struct glyph_string *s;
2054 NativeRectangle *nr;
2055 {
2056 get_glyph_string_clip_rects (s, nr, 1);
2057 }
2058
2059
2060 /* EXPORT:
2061 Return the position and height of the phys cursor in window W.
2062 Set w->phys_cursor_width to width of phys cursor.
2063 */
2064
2065 void
2066 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2067 struct window *w;
2068 struct glyph_row *row;
2069 struct glyph *glyph;
2070 int *xp, *yp, *heightp;
2071 {
2072 struct frame *f = XFRAME (WINDOW_FRAME (w));
2073 int x, y, wd, h, h0, y0;
2074
2075 /* Compute the width of the rectangle to draw. If on a stretch
2076 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2077 rectangle as wide as the glyph, but use a canonical character
2078 width instead. */
2079 wd = glyph->pixel_width - 1;
2080 #ifdef HAVE_NTGUI
2081 wd++; /* Why? */
2082 #endif
2083
2084 x = w->phys_cursor.x;
2085 if (x < 0)
2086 {
2087 wd += x;
2088 x = 0;
2089 }
2090
2091 if (glyph->type == STRETCH_GLYPH
2092 && !x_stretch_cursor_p)
2093 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2094 w->phys_cursor_width = wd;
2095
2096 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2097
2098 /* If y is below window bottom, ensure that we still see a cursor. */
2099 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2100
2101 h = max (h0, glyph->ascent + glyph->descent);
2102 h0 = min (h0, glyph->ascent + glyph->descent);
2103
2104 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2105 if (y < y0)
2106 {
2107 h = max (h - (y0 - y) + 1, h0);
2108 y = y0 - 1;
2109 }
2110 else
2111 {
2112 y0 = window_text_bottom_y (w) - h0;
2113 if (y > y0)
2114 {
2115 h += y - y0;
2116 y = y0;
2117 }
2118 }
2119
2120 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2121 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2122 *heightp = h;
2123 }
2124
2125 /*
2126 * Remember which glyph the mouse is over.
2127 */
2128
2129 void
2130 remember_mouse_glyph (f, gx, gy, rect)
2131 struct frame *f;
2132 int gx, gy;
2133 NativeRectangle *rect;
2134 {
2135 Lisp_Object window;
2136 struct window *w;
2137 struct glyph_row *r, *gr, *end_row;
2138 enum window_part part;
2139 enum glyph_row_area area;
2140 int x, y, width, height;
2141
2142 /* Try to determine frame pixel position and size of the glyph under
2143 frame pixel coordinates X/Y on frame F. */
2144
2145 if (!f->glyphs_initialized_p
2146 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2147 NILP (window)))
2148 {
2149 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2150 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2151 goto virtual_glyph;
2152 }
2153
2154 w = XWINDOW (window);
2155 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2156 height = WINDOW_FRAME_LINE_HEIGHT (w);
2157
2158 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2159 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2160
2161 if (w->pseudo_window_p)
2162 {
2163 area = TEXT_AREA;
2164 part = ON_MODE_LINE; /* Don't adjust margin. */
2165 goto text_glyph;
2166 }
2167
2168 switch (part)
2169 {
2170 case ON_LEFT_MARGIN:
2171 area = LEFT_MARGIN_AREA;
2172 goto text_glyph;
2173
2174 case ON_RIGHT_MARGIN:
2175 area = RIGHT_MARGIN_AREA;
2176 goto text_glyph;
2177
2178 case ON_HEADER_LINE:
2179 case ON_MODE_LINE:
2180 gr = (part == ON_HEADER_LINE
2181 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2182 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2183 gy = gr->y;
2184 area = TEXT_AREA;
2185 goto text_glyph_row_found;
2186
2187 case ON_TEXT:
2188 area = TEXT_AREA;
2189
2190 text_glyph:
2191 gr = 0; gy = 0;
2192 for (; r <= end_row && r->enabled_p; ++r)
2193 if (r->y + r->height > y)
2194 {
2195 gr = r; gy = r->y;
2196 break;
2197 }
2198
2199 text_glyph_row_found:
2200 if (gr && gy <= y)
2201 {
2202 struct glyph *g = gr->glyphs[area];
2203 struct glyph *end = g + gr->used[area];
2204
2205 height = gr->height;
2206 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2207 if (gx + g->pixel_width > x)
2208 break;
2209
2210 if (g < end)
2211 {
2212 if (g->type == IMAGE_GLYPH)
2213 {
2214 /* Don't remember when mouse is over image, as
2215 image may have hot-spots. */
2216 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2217 return;
2218 }
2219 width = g->pixel_width;
2220 }
2221 else
2222 {
2223 /* Use nominal char spacing at end of line. */
2224 x -= gx;
2225 gx += (x / width) * width;
2226 }
2227
2228 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2229 gx += window_box_left_offset (w, area);
2230 }
2231 else
2232 {
2233 /* Use nominal line height at end of window. */
2234 gx = (x / width) * width;
2235 y -= gy;
2236 gy += (y / height) * height;
2237 }
2238 break;
2239
2240 case ON_LEFT_FRINGE:
2241 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2242 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2243 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2244 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2245 goto row_glyph;
2246
2247 case ON_RIGHT_FRINGE:
2248 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2249 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2250 : window_box_right_offset (w, TEXT_AREA));
2251 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2252 goto row_glyph;
2253
2254 case ON_SCROLL_BAR:
2255 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2256 ? 0
2257 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2258 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2259 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2260 : 0)));
2261 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2262
2263 row_glyph:
2264 gr = 0, gy = 0;
2265 for (; r <= end_row && r->enabled_p; ++r)
2266 if (r->y + r->height > y)
2267 {
2268 gr = r; gy = r->y;
2269 break;
2270 }
2271
2272 if (gr && gy <= y)
2273 height = gr->height;
2274 else
2275 {
2276 /* Use nominal line height at end of window. */
2277 y -= gy;
2278 gy += (y / height) * height;
2279 }
2280 break;
2281
2282 default:
2283 ;
2284 virtual_glyph:
2285 /* If there is no glyph under the mouse, then we divide the screen
2286 into a grid of the smallest glyph in the frame, and use that
2287 as our "glyph". */
2288
2289 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2290 round down even for negative values. */
2291 if (gx < 0)
2292 gx -= width - 1;
2293 if (gy < 0)
2294 gy -= height - 1;
2295
2296 gx = (gx / width) * width;
2297 gy = (gy / height) * height;
2298
2299 goto store_rect;
2300 }
2301
2302 gx += WINDOW_LEFT_EDGE_X (w);
2303 gy += WINDOW_TOP_EDGE_Y (w);
2304
2305 store_rect:
2306 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2307
2308 /* Visible feedback for debugging. */
2309 #if 0
2310 #if HAVE_X_WINDOWS
2311 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2312 f->output_data.x->normal_gc,
2313 gx, gy, width, height);
2314 #endif
2315 #endif
2316 }
2317
2318
2319 #endif /* HAVE_WINDOW_SYSTEM */
2320
2321 \f
2322 /***********************************************************************
2323 Lisp form evaluation
2324 ***********************************************************************/
2325
2326 /* Error handler for safe_eval and safe_call. */
2327
2328 static Lisp_Object
2329 safe_eval_handler (arg)
2330 Lisp_Object arg;
2331 {
2332 add_to_log ("Error during redisplay: %s", arg, Qnil);
2333 return Qnil;
2334 }
2335
2336
2337 /* Evaluate SEXPR and return the result, or nil if something went
2338 wrong. Prevent redisplay during the evaluation. */
2339
2340 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2341 Return the result, or nil if something went wrong. Prevent
2342 redisplay during the evaluation. */
2343
2344 Lisp_Object
2345 safe_call (nargs, args)
2346 int nargs;
2347 Lisp_Object *args;
2348 {
2349 Lisp_Object val;
2350
2351 if (inhibit_eval_during_redisplay)
2352 val = Qnil;
2353 else
2354 {
2355 int count = SPECPDL_INDEX ();
2356 struct gcpro gcpro1;
2357
2358 GCPRO1 (args[0]);
2359 gcpro1.nvars = nargs;
2360 specbind (Qinhibit_redisplay, Qt);
2361 /* Use Qt to ensure debugger does not run,
2362 so there is no possibility of wanting to redisplay. */
2363 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2364 safe_eval_handler);
2365 UNGCPRO;
2366 val = unbind_to (count, val);
2367 }
2368
2369 return val;
2370 }
2371
2372
2373 /* Call function FN with one argument ARG.
2374 Return the result, or nil if something went wrong. */
2375
2376 Lisp_Object
2377 safe_call1 (fn, arg)
2378 Lisp_Object fn, arg;
2379 {
2380 Lisp_Object args[2];
2381 args[0] = fn;
2382 args[1] = arg;
2383 return safe_call (2, args);
2384 }
2385
2386 static Lisp_Object Qeval;
2387
2388 Lisp_Object
2389 safe_eval (Lisp_Object sexpr)
2390 {
2391 return safe_call1 (Qeval, sexpr);
2392 }
2393
2394 /* Call function FN with one argument ARG.
2395 Return the result, or nil if something went wrong. */
2396
2397 Lisp_Object
2398 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2399 {
2400 Lisp_Object args[3];
2401 args[0] = fn;
2402 args[1] = arg1;
2403 args[2] = arg2;
2404 return safe_call (3, args);
2405 }
2406
2407
2408 \f
2409 /***********************************************************************
2410 Debugging
2411 ***********************************************************************/
2412
2413 #if 0
2414
2415 /* Define CHECK_IT to perform sanity checks on iterators.
2416 This is for debugging. It is too slow to do unconditionally. */
2417
2418 static void
2419 check_it (it)
2420 struct it *it;
2421 {
2422 if (it->method == GET_FROM_STRING)
2423 {
2424 xassert (STRINGP (it->string));
2425 xassert (IT_STRING_CHARPOS (*it) >= 0);
2426 }
2427 else
2428 {
2429 xassert (IT_STRING_CHARPOS (*it) < 0);
2430 if (it->method == GET_FROM_BUFFER)
2431 {
2432 /* Check that character and byte positions agree. */
2433 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2434 }
2435 }
2436
2437 if (it->dpvec)
2438 xassert (it->current.dpvec_index >= 0);
2439 else
2440 xassert (it->current.dpvec_index < 0);
2441 }
2442
2443 #define CHECK_IT(IT) check_it ((IT))
2444
2445 #else /* not 0 */
2446
2447 #define CHECK_IT(IT) (void) 0
2448
2449 #endif /* not 0 */
2450
2451
2452 #if GLYPH_DEBUG
2453
2454 /* Check that the window end of window W is what we expect it
2455 to be---the last row in the current matrix displaying text. */
2456
2457 static void
2458 check_window_end (w)
2459 struct window *w;
2460 {
2461 if (!MINI_WINDOW_P (w)
2462 && !NILP (w->window_end_valid))
2463 {
2464 struct glyph_row *row;
2465 xassert ((row = MATRIX_ROW (w->current_matrix,
2466 XFASTINT (w->window_end_vpos)),
2467 !row->enabled_p
2468 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2469 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2470 }
2471 }
2472
2473 #define CHECK_WINDOW_END(W) check_window_end ((W))
2474
2475 #else /* not GLYPH_DEBUG */
2476
2477 #define CHECK_WINDOW_END(W) (void) 0
2478
2479 #endif /* not GLYPH_DEBUG */
2480
2481
2482 \f
2483 /***********************************************************************
2484 Iterator initialization
2485 ***********************************************************************/
2486
2487 /* Initialize IT for displaying current_buffer in window W, starting
2488 at character position CHARPOS. CHARPOS < 0 means that no buffer
2489 position is specified which is useful when the iterator is assigned
2490 a position later. BYTEPOS is the byte position corresponding to
2491 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2492
2493 If ROW is not null, calls to produce_glyphs with IT as parameter
2494 will produce glyphs in that row.
2495
2496 BASE_FACE_ID is the id of a base face to use. It must be one of
2497 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2498 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2499 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2500
2501 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2502 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2503 will be initialized to use the corresponding mode line glyph row of
2504 the desired matrix of W. */
2505
2506 void
2507 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2508 struct it *it;
2509 struct window *w;
2510 int charpos, bytepos;
2511 struct glyph_row *row;
2512 enum face_id base_face_id;
2513 {
2514 int highlight_region_p;
2515 enum face_id remapped_base_face_id = base_face_id;
2516
2517 /* Some precondition checks. */
2518 xassert (w != NULL && it != NULL);
2519 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2520 && charpos <= ZV));
2521
2522 /* If face attributes have been changed since the last redisplay,
2523 free realized faces now because they depend on face definitions
2524 that might have changed. Don't free faces while there might be
2525 desired matrices pending which reference these faces. */
2526 if (face_change_count && !inhibit_free_realized_faces)
2527 {
2528 face_change_count = 0;
2529 free_all_realized_faces (Qnil);
2530 }
2531
2532 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2533 if (! NILP (Vface_remapping_alist))
2534 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2535
2536 /* Use one of the mode line rows of W's desired matrix if
2537 appropriate. */
2538 if (row == NULL)
2539 {
2540 if (base_face_id == MODE_LINE_FACE_ID
2541 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2542 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2543 else if (base_face_id == HEADER_LINE_FACE_ID)
2544 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2545 }
2546
2547 /* Clear IT. */
2548 bzero (it, sizeof *it);
2549 it->current.overlay_string_index = -1;
2550 it->current.dpvec_index = -1;
2551 it->base_face_id = remapped_base_face_id;
2552 it->string = Qnil;
2553 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2554
2555 /* The window in which we iterate over current_buffer: */
2556 XSETWINDOW (it->window, w);
2557 it->w = w;
2558 it->f = XFRAME (w->frame);
2559
2560 /* Extra space between lines (on window systems only). */
2561 if (base_face_id == DEFAULT_FACE_ID
2562 && FRAME_WINDOW_P (it->f))
2563 {
2564 if (NATNUMP (current_buffer->extra_line_spacing))
2565 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2566 else if (FLOATP (current_buffer->extra_line_spacing))
2567 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2568 * FRAME_LINE_HEIGHT (it->f));
2569 else if (it->f->extra_line_spacing > 0)
2570 it->extra_line_spacing = it->f->extra_line_spacing;
2571 it->max_extra_line_spacing = 0;
2572 }
2573
2574 /* If realized faces have been removed, e.g. because of face
2575 attribute changes of named faces, recompute them. When running
2576 in batch mode, the face cache of the initial frame is null. If
2577 we happen to get called, make a dummy face cache. */
2578 if (FRAME_FACE_CACHE (it->f) == NULL)
2579 init_frame_faces (it->f);
2580 if (FRAME_FACE_CACHE (it->f)->used == 0)
2581 recompute_basic_faces (it->f);
2582
2583 /* Current value of the `slice', `space-width', and 'height' properties. */
2584 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2585 it->space_width = Qnil;
2586 it->font_height = Qnil;
2587 it->override_ascent = -1;
2588
2589 /* Are control characters displayed as `^C'? */
2590 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2591
2592 /* -1 means everything between a CR and the following line end
2593 is invisible. >0 means lines indented more than this value are
2594 invisible. */
2595 it->selective = (INTEGERP (current_buffer->selective_display)
2596 ? XFASTINT (current_buffer->selective_display)
2597 : (!NILP (current_buffer->selective_display)
2598 ? -1 : 0));
2599 it->selective_display_ellipsis_p
2600 = !NILP (current_buffer->selective_display_ellipses);
2601
2602 /* Display table to use. */
2603 it->dp = window_display_table (w);
2604
2605 /* Are multibyte characters enabled in current_buffer? */
2606 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2607
2608 /* Non-zero if we should highlight the region. */
2609 highlight_region_p
2610 = (!NILP (Vtransient_mark_mode)
2611 && !NILP (current_buffer->mark_active)
2612 && XMARKER (current_buffer->mark)->buffer != 0);
2613
2614 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2615 start and end of a visible region in window IT->w. Set both to
2616 -1 to indicate no region. */
2617 if (highlight_region_p
2618 /* Maybe highlight only in selected window. */
2619 && (/* Either show region everywhere. */
2620 highlight_nonselected_windows
2621 /* Or show region in the selected window. */
2622 || w == XWINDOW (selected_window)
2623 /* Or show the region if we are in the mini-buffer and W is
2624 the window the mini-buffer refers to. */
2625 || (MINI_WINDOW_P (XWINDOW (selected_window))
2626 && WINDOWP (minibuf_selected_window)
2627 && w == XWINDOW (minibuf_selected_window))))
2628 {
2629 int charpos = marker_position (current_buffer->mark);
2630 it->region_beg_charpos = min (PT, charpos);
2631 it->region_end_charpos = max (PT, charpos);
2632 }
2633 else
2634 it->region_beg_charpos = it->region_end_charpos = -1;
2635
2636 /* Get the position at which the redisplay_end_trigger hook should
2637 be run, if it is to be run at all. */
2638 if (MARKERP (w->redisplay_end_trigger)
2639 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2640 it->redisplay_end_trigger_charpos
2641 = marker_position (w->redisplay_end_trigger);
2642 else if (INTEGERP (w->redisplay_end_trigger))
2643 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2644
2645 /* Correct bogus values of tab_width. */
2646 it->tab_width = XINT (current_buffer->tab_width);
2647 if (it->tab_width <= 0 || it->tab_width > 1000)
2648 it->tab_width = 8;
2649
2650 /* Are lines in the display truncated? */
2651 if (base_face_id != DEFAULT_FACE_ID
2652 || XINT (it->w->hscroll)
2653 || (! WINDOW_FULL_WIDTH_P (it->w)
2654 && ((!NILP (Vtruncate_partial_width_windows)
2655 && !INTEGERP (Vtruncate_partial_width_windows))
2656 || (INTEGERP (Vtruncate_partial_width_windows)
2657 && (WINDOW_TOTAL_COLS (it->w)
2658 < XINT (Vtruncate_partial_width_windows))))))
2659 it->line_wrap = TRUNCATE;
2660 else if (NILP (current_buffer->truncate_lines))
2661 it->line_wrap = NILP (current_buffer->word_wrap)
2662 ? WINDOW_WRAP : WORD_WRAP;
2663 else
2664 it->line_wrap = TRUNCATE;
2665
2666 /* Get dimensions of truncation and continuation glyphs. These are
2667 displayed as fringe bitmaps under X, so we don't need them for such
2668 frames. */
2669 if (!FRAME_WINDOW_P (it->f))
2670 {
2671 if (it->line_wrap == TRUNCATE)
2672 {
2673 /* We will need the truncation glyph. */
2674 xassert (it->glyph_row == NULL);
2675 produce_special_glyphs (it, IT_TRUNCATION);
2676 it->truncation_pixel_width = it->pixel_width;
2677 }
2678 else
2679 {
2680 /* We will need the continuation glyph. */
2681 xassert (it->glyph_row == NULL);
2682 produce_special_glyphs (it, IT_CONTINUATION);
2683 it->continuation_pixel_width = it->pixel_width;
2684 }
2685
2686 /* Reset these values to zero because the produce_special_glyphs
2687 above has changed them. */
2688 it->pixel_width = it->ascent = it->descent = 0;
2689 it->phys_ascent = it->phys_descent = 0;
2690 }
2691
2692 /* Set this after getting the dimensions of truncation and
2693 continuation glyphs, so that we don't produce glyphs when calling
2694 produce_special_glyphs, above. */
2695 it->glyph_row = row;
2696 it->area = TEXT_AREA;
2697
2698 /* Get the dimensions of the display area. The display area
2699 consists of the visible window area plus a horizontally scrolled
2700 part to the left of the window. All x-values are relative to the
2701 start of this total display area. */
2702 if (base_face_id != DEFAULT_FACE_ID)
2703 {
2704 /* Mode lines, menu bar in terminal frames. */
2705 it->first_visible_x = 0;
2706 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2707 }
2708 else
2709 {
2710 it->first_visible_x
2711 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2712 it->last_visible_x = (it->first_visible_x
2713 + window_box_width (w, TEXT_AREA));
2714
2715 /* If we truncate lines, leave room for the truncator glyph(s) at
2716 the right margin. Otherwise, leave room for the continuation
2717 glyph(s). Truncation and continuation glyphs are not inserted
2718 for window-based redisplay. */
2719 if (!FRAME_WINDOW_P (it->f))
2720 {
2721 if (it->line_wrap == TRUNCATE)
2722 it->last_visible_x -= it->truncation_pixel_width;
2723 else
2724 it->last_visible_x -= it->continuation_pixel_width;
2725 }
2726
2727 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2728 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2729 }
2730
2731 /* Leave room for a border glyph. */
2732 if (!FRAME_WINDOW_P (it->f)
2733 && !WINDOW_RIGHTMOST_P (it->w))
2734 it->last_visible_x -= 1;
2735
2736 it->last_visible_y = window_text_bottom_y (w);
2737
2738 /* For mode lines and alike, arrange for the first glyph having a
2739 left box line if the face specifies a box. */
2740 if (base_face_id != DEFAULT_FACE_ID)
2741 {
2742 struct face *face;
2743
2744 it->face_id = remapped_base_face_id;
2745
2746 /* If we have a boxed mode line, make the first character appear
2747 with a left box line. */
2748 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2749 if (face->box != FACE_NO_BOX)
2750 it->start_of_box_run_p = 1;
2751 }
2752
2753 /* If a buffer position was specified, set the iterator there,
2754 getting overlays and face properties from that position. */
2755 if (charpos >= BUF_BEG (current_buffer))
2756 {
2757 it->end_charpos = ZV;
2758 it->face_id = -1;
2759 IT_CHARPOS (*it) = charpos;
2760
2761 /* Compute byte position if not specified. */
2762 if (bytepos < charpos)
2763 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2764 else
2765 IT_BYTEPOS (*it) = bytepos;
2766
2767 it->start = it->current;
2768
2769 /* Compute faces etc. */
2770 reseat (it, it->current.pos, 1);
2771 }
2772
2773 CHECK_IT (it);
2774 }
2775
2776
2777 /* Initialize IT for the display of window W with window start POS. */
2778
2779 void
2780 start_display (it, w, pos)
2781 struct it *it;
2782 struct window *w;
2783 struct text_pos pos;
2784 {
2785 struct glyph_row *row;
2786 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2787
2788 row = w->desired_matrix->rows + first_vpos;
2789 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2790 it->first_vpos = first_vpos;
2791
2792 /* Don't reseat to previous visible line start if current start
2793 position is in a string or image. */
2794 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2795 {
2796 int start_at_line_beg_p;
2797 int first_y = it->current_y;
2798
2799 /* If window start is not at a line start, skip forward to POS to
2800 get the correct continuation lines width. */
2801 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2802 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2803 if (!start_at_line_beg_p)
2804 {
2805 int new_x;
2806
2807 reseat_at_previous_visible_line_start (it);
2808 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2809
2810 new_x = it->current_x + it->pixel_width;
2811
2812 /* If lines are continued, this line may end in the middle
2813 of a multi-glyph character (e.g. a control character
2814 displayed as \003, or in the middle of an overlay
2815 string). In this case move_it_to above will not have
2816 taken us to the start of the continuation line but to the
2817 end of the continued line. */
2818 if (it->current_x > 0
2819 && it->line_wrap != TRUNCATE /* Lines are continued. */
2820 && (/* And glyph doesn't fit on the line. */
2821 new_x > it->last_visible_x
2822 /* Or it fits exactly and we're on a window
2823 system frame. */
2824 || (new_x == it->last_visible_x
2825 && FRAME_WINDOW_P (it->f))))
2826 {
2827 if (it->current.dpvec_index >= 0
2828 || it->current.overlay_string_index >= 0)
2829 {
2830 set_iterator_to_next (it, 1);
2831 move_it_in_display_line_to (it, -1, -1, 0);
2832 }
2833
2834 it->continuation_lines_width += it->current_x;
2835 }
2836
2837 /* We're starting a new display line, not affected by the
2838 height of the continued line, so clear the appropriate
2839 fields in the iterator structure. */
2840 it->max_ascent = it->max_descent = 0;
2841 it->max_phys_ascent = it->max_phys_descent = 0;
2842
2843 it->current_y = first_y;
2844 it->vpos = 0;
2845 it->current_x = it->hpos = 0;
2846 }
2847 }
2848
2849 #if 0 /* Don't assert the following because start_display is sometimes
2850 called intentionally with a window start that is not at a
2851 line start. Please leave this code in as a comment. */
2852
2853 /* Window start should be on a line start, now. */
2854 xassert (it->continuation_lines_width
2855 || IT_CHARPOS (it) == BEGV
2856 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2857 #endif /* 0 */
2858 }
2859
2860
2861 /* Return 1 if POS is a position in ellipses displayed for invisible
2862 text. W is the window we display, for text property lookup. */
2863
2864 static int
2865 in_ellipses_for_invisible_text_p (pos, w)
2866 struct display_pos *pos;
2867 struct window *w;
2868 {
2869 Lisp_Object prop, window;
2870 int ellipses_p = 0;
2871 int charpos = CHARPOS (pos->pos);
2872
2873 /* If POS specifies a position in a display vector, this might
2874 be for an ellipsis displayed for invisible text. We won't
2875 get the iterator set up for delivering that ellipsis unless
2876 we make sure that it gets aware of the invisible text. */
2877 if (pos->dpvec_index >= 0
2878 && pos->overlay_string_index < 0
2879 && CHARPOS (pos->string_pos) < 0
2880 && charpos > BEGV
2881 && (XSETWINDOW (window, w),
2882 prop = Fget_char_property (make_number (charpos),
2883 Qinvisible, window),
2884 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2885 {
2886 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2887 window);
2888 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2889 }
2890
2891 return ellipses_p;
2892 }
2893
2894
2895 /* Initialize IT for stepping through current_buffer in window W,
2896 starting at position POS that includes overlay string and display
2897 vector/ control character translation position information. Value
2898 is zero if there are overlay strings with newlines at POS. */
2899
2900 static int
2901 init_from_display_pos (it, w, pos)
2902 struct it *it;
2903 struct window *w;
2904 struct display_pos *pos;
2905 {
2906 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2907 int i, overlay_strings_with_newlines = 0;
2908
2909 /* If POS specifies a position in a display vector, this might
2910 be for an ellipsis displayed for invisible text. We won't
2911 get the iterator set up for delivering that ellipsis unless
2912 we make sure that it gets aware of the invisible text. */
2913 if (in_ellipses_for_invisible_text_p (pos, w))
2914 {
2915 --charpos;
2916 bytepos = 0;
2917 }
2918
2919 /* Keep in mind: the call to reseat in init_iterator skips invisible
2920 text, so we might end up at a position different from POS. This
2921 is only a problem when POS is a row start after a newline and an
2922 overlay starts there with an after-string, and the overlay has an
2923 invisible property. Since we don't skip invisible text in
2924 display_line and elsewhere immediately after consuming the
2925 newline before the row start, such a POS will not be in a string,
2926 but the call to init_iterator below will move us to the
2927 after-string. */
2928 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2929
2930 /* This only scans the current chunk -- it should scan all chunks.
2931 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2932 to 16 in 22.1 to make this a lesser problem. */
2933 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2934 {
2935 const char *s = SDATA (it->overlay_strings[i]);
2936 const char *e = s + SBYTES (it->overlay_strings[i]);
2937
2938 while (s < e && *s != '\n')
2939 ++s;
2940
2941 if (s < e)
2942 {
2943 overlay_strings_with_newlines = 1;
2944 break;
2945 }
2946 }
2947
2948 /* If position is within an overlay string, set up IT to the right
2949 overlay string. */
2950 if (pos->overlay_string_index >= 0)
2951 {
2952 int relative_index;
2953
2954 /* If the first overlay string happens to have a `display'
2955 property for an image, the iterator will be set up for that
2956 image, and we have to undo that setup first before we can
2957 correct the overlay string index. */
2958 if (it->method == GET_FROM_IMAGE)
2959 pop_it (it);
2960
2961 /* We already have the first chunk of overlay strings in
2962 IT->overlay_strings. Load more until the one for
2963 pos->overlay_string_index is in IT->overlay_strings. */
2964 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2965 {
2966 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2967 it->current.overlay_string_index = 0;
2968 while (n--)
2969 {
2970 load_overlay_strings (it, 0);
2971 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2972 }
2973 }
2974
2975 it->current.overlay_string_index = pos->overlay_string_index;
2976 relative_index = (it->current.overlay_string_index
2977 % OVERLAY_STRING_CHUNK_SIZE);
2978 it->string = it->overlay_strings[relative_index];
2979 xassert (STRINGP (it->string));
2980 it->current.string_pos = pos->string_pos;
2981 it->method = GET_FROM_STRING;
2982 }
2983
2984 #if 0 /* This is bogus because POS not having an overlay string
2985 position does not mean it's after the string. Example: A
2986 line starting with a before-string and initialization of IT
2987 to the previous row's end position. */
2988 else if (it->current.overlay_string_index >= 0)
2989 {
2990 /* If POS says we're already after an overlay string ending at
2991 POS, make sure to pop the iterator because it will be in
2992 front of that overlay string. When POS is ZV, we've thereby
2993 also ``processed'' overlay strings at ZV. */
2994 while (it->sp)
2995 pop_it (it);
2996 xassert (it->current.overlay_string_index == -1);
2997 xassert (it->method == GET_FROM_BUFFER);
2998 if (CHARPOS (pos->pos) == ZV)
2999 it->overlay_strings_at_end_processed_p = 1;
3000 }
3001 #endif /* 0 */
3002
3003 if (CHARPOS (pos->string_pos) >= 0)
3004 {
3005 /* Recorded position is not in an overlay string, but in another
3006 string. This can only be a string from a `display' property.
3007 IT should already be filled with that string. */
3008 it->current.string_pos = pos->string_pos;
3009 xassert (STRINGP (it->string));
3010 }
3011
3012 /* Restore position in display vector translations, control
3013 character translations or ellipses. */
3014 if (pos->dpvec_index >= 0)
3015 {
3016 if (it->dpvec == NULL)
3017 get_next_display_element (it);
3018 xassert (it->dpvec && it->current.dpvec_index == 0);
3019 it->current.dpvec_index = pos->dpvec_index;
3020 }
3021
3022 CHECK_IT (it);
3023 return !overlay_strings_with_newlines;
3024 }
3025
3026
3027 /* Initialize IT for stepping through current_buffer in window W
3028 starting at ROW->start. */
3029
3030 static void
3031 init_to_row_start (it, w, row)
3032 struct it *it;
3033 struct window *w;
3034 struct glyph_row *row;
3035 {
3036 init_from_display_pos (it, w, &row->start);
3037 it->start = row->start;
3038 it->continuation_lines_width = row->continuation_lines_width;
3039 CHECK_IT (it);
3040 }
3041
3042
3043 /* Initialize IT for stepping through current_buffer in window W
3044 starting in the line following ROW, i.e. starting at ROW->end.
3045 Value is zero if there are overlay strings with newlines at ROW's
3046 end position. */
3047
3048 static int
3049 init_to_row_end (it, w, row)
3050 struct it *it;
3051 struct window *w;
3052 struct glyph_row *row;
3053 {
3054 int success = 0;
3055
3056 if (init_from_display_pos (it, w, &row->end))
3057 {
3058 if (row->continued_p)
3059 it->continuation_lines_width
3060 = row->continuation_lines_width + row->pixel_width;
3061 CHECK_IT (it);
3062 success = 1;
3063 }
3064
3065 return success;
3066 }
3067
3068
3069
3070 \f
3071 /***********************************************************************
3072 Text properties
3073 ***********************************************************************/
3074
3075 /* Called when IT reaches IT->stop_charpos. Handle text property and
3076 overlay changes. Set IT->stop_charpos to the next position where
3077 to stop. */
3078
3079 static void
3080 handle_stop (it)
3081 struct it *it;
3082 {
3083 enum prop_handled handled;
3084 int handle_overlay_change_p;
3085 struct props *p;
3086
3087 it->dpvec = NULL;
3088 it->current.dpvec_index = -1;
3089 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3090 it->ignore_overlay_strings_at_pos_p = 0;
3091
3092 /* Use face of preceding text for ellipsis (if invisible) */
3093 if (it->selective_display_ellipsis_p)
3094 it->saved_face_id = it->face_id;
3095
3096 do
3097 {
3098 handled = HANDLED_NORMALLY;
3099
3100 /* Call text property handlers. */
3101 for (p = it_props; p->handler; ++p)
3102 {
3103 handled = p->handler (it);
3104
3105 if (handled == HANDLED_RECOMPUTE_PROPS)
3106 break;
3107 else if (handled == HANDLED_RETURN)
3108 {
3109 /* We still want to show before and after strings from
3110 overlays even if the actual buffer text is replaced. */
3111 if (!handle_overlay_change_p || it->sp > 1)
3112 return;
3113 if (!get_overlay_strings_1 (it, 0, 0))
3114 return;
3115 it->ignore_overlay_strings_at_pos_p = 1;
3116 it->string_from_display_prop_p = 0;
3117 handle_overlay_change_p = 0;
3118 handled = HANDLED_RECOMPUTE_PROPS;
3119 break;
3120 }
3121 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3122 handle_overlay_change_p = 0;
3123 }
3124
3125 if (handled != HANDLED_RECOMPUTE_PROPS)
3126 {
3127 /* Don't check for overlay strings below when set to deliver
3128 characters from a display vector. */
3129 if (it->method == GET_FROM_DISPLAY_VECTOR)
3130 handle_overlay_change_p = 0;
3131
3132 /* Handle overlay changes.
3133 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3134 if it finds overlays. */
3135 if (handle_overlay_change_p)
3136 handled = handle_overlay_change (it);
3137 }
3138 }
3139 while (handled == HANDLED_RECOMPUTE_PROPS);
3140
3141 /* Determine where to stop next. */
3142 if (handled == HANDLED_NORMALLY)
3143 compute_stop_pos (it);
3144 }
3145
3146
3147 /* Compute IT->stop_charpos from text property and overlay change
3148 information for IT's current position. */
3149
3150 static void
3151 compute_stop_pos (it)
3152 struct it *it;
3153 {
3154 register INTERVAL iv, next_iv;
3155 Lisp_Object object, limit, position;
3156
3157 /* If nowhere else, stop at the end. */
3158 it->stop_charpos = it->end_charpos;
3159
3160 if (STRINGP (it->string))
3161 {
3162 /* Strings are usually short, so don't limit the search for
3163 properties. */
3164 object = it->string;
3165 limit = Qnil;
3166 position = make_number (IT_STRING_CHARPOS (*it));
3167 }
3168 else
3169 {
3170 int charpos;
3171
3172 /* If next overlay change is in front of the current stop pos
3173 (which is IT->end_charpos), stop there. Note: value of
3174 next_overlay_change is point-max if no overlay change
3175 follows. */
3176 charpos = next_overlay_change (IT_CHARPOS (*it));
3177 if (charpos < it->stop_charpos)
3178 it->stop_charpos = charpos;
3179
3180 /* If showing the region, we have to stop at the region
3181 start or end because the face might change there. */
3182 if (it->region_beg_charpos > 0)
3183 {
3184 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3185 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3186 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3187 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3188 }
3189
3190 /* Set up variables for computing the stop position from text
3191 property changes. */
3192 XSETBUFFER (object, current_buffer);
3193 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3194 position = make_number (IT_CHARPOS (*it));
3195
3196 }
3197
3198 /* Get the interval containing IT's position. Value is a null
3199 interval if there isn't such an interval. */
3200 iv = validate_interval_range (object, &position, &position, 0);
3201 if (!NULL_INTERVAL_P (iv))
3202 {
3203 Lisp_Object values_here[LAST_PROP_IDX];
3204 struct props *p;
3205
3206 /* Get properties here. */
3207 for (p = it_props; p->handler; ++p)
3208 values_here[p->idx] = textget (iv->plist, *p->name);
3209
3210 /* Look for an interval following iv that has different
3211 properties. */
3212 for (next_iv = next_interval (iv);
3213 (!NULL_INTERVAL_P (next_iv)
3214 && (NILP (limit)
3215 || XFASTINT (limit) > next_iv->position));
3216 next_iv = next_interval (next_iv))
3217 {
3218 for (p = it_props; p->handler; ++p)
3219 {
3220 Lisp_Object new_value;
3221
3222 new_value = textget (next_iv->plist, *p->name);
3223 if (!EQ (values_here[p->idx], new_value))
3224 break;
3225 }
3226
3227 if (p->handler)
3228 break;
3229 }
3230
3231 if (!NULL_INTERVAL_P (next_iv))
3232 {
3233 if (INTEGERP (limit)
3234 && next_iv->position >= XFASTINT (limit))
3235 /* No text property change up to limit. */
3236 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3237 else
3238 /* Text properties change in next_iv. */
3239 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3240 }
3241 }
3242
3243 xassert (STRINGP (it->string)
3244 || (it->stop_charpos >= BEGV
3245 && it->stop_charpos >= IT_CHARPOS (*it)));
3246 }
3247
3248
3249 /* Return the position of the next overlay change after POS in
3250 current_buffer. Value is point-max if no overlay change
3251 follows. This is like `next-overlay-change' but doesn't use
3252 xmalloc. */
3253
3254 static EMACS_INT
3255 next_overlay_change (pos)
3256 EMACS_INT pos;
3257 {
3258 int noverlays;
3259 EMACS_INT endpos;
3260 Lisp_Object *overlays;
3261 int i;
3262
3263 /* Get all overlays at the given position. */
3264 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3265
3266 /* If any of these overlays ends before endpos,
3267 use its ending point instead. */
3268 for (i = 0; i < noverlays; ++i)
3269 {
3270 Lisp_Object oend;
3271 EMACS_INT oendpos;
3272
3273 oend = OVERLAY_END (overlays[i]);
3274 oendpos = OVERLAY_POSITION (oend);
3275 endpos = min (endpos, oendpos);
3276 }
3277
3278 return endpos;
3279 }
3280
3281
3282 \f
3283 /***********************************************************************
3284 Fontification
3285 ***********************************************************************/
3286
3287 /* Handle changes in the `fontified' property of the current buffer by
3288 calling hook functions from Qfontification_functions to fontify
3289 regions of text. */
3290
3291 static enum prop_handled
3292 handle_fontified_prop (it)
3293 struct it *it;
3294 {
3295 Lisp_Object prop, pos;
3296 enum prop_handled handled = HANDLED_NORMALLY;
3297
3298 if (!NILP (Vmemory_full))
3299 return handled;
3300
3301 /* Get the value of the `fontified' property at IT's current buffer
3302 position. (The `fontified' property doesn't have a special
3303 meaning in strings.) If the value is nil, call functions from
3304 Qfontification_functions. */
3305 if (!STRINGP (it->string)
3306 && it->s == NULL
3307 && !NILP (Vfontification_functions)
3308 && !NILP (Vrun_hooks)
3309 && (pos = make_number (IT_CHARPOS (*it)),
3310 prop = Fget_char_property (pos, Qfontified, Qnil),
3311 /* Ignore the special cased nil value always present at EOB since
3312 no amount of fontifying will be able to change it. */
3313 NILP (prop) && IT_CHARPOS (*it) < Z))
3314 {
3315 int count = SPECPDL_INDEX ();
3316 Lisp_Object val;
3317
3318 val = Vfontification_functions;
3319 specbind (Qfontification_functions, Qnil);
3320
3321 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3322 safe_call1 (val, pos);
3323 else
3324 {
3325 Lisp_Object globals, fn;
3326 struct gcpro gcpro1, gcpro2;
3327
3328 globals = Qnil;
3329 GCPRO2 (val, globals);
3330
3331 for (; CONSP (val); val = XCDR (val))
3332 {
3333 fn = XCAR (val);
3334
3335 if (EQ (fn, Qt))
3336 {
3337 /* A value of t indicates this hook has a local
3338 binding; it means to run the global binding too.
3339 In a global value, t should not occur. If it
3340 does, we must ignore it to avoid an endless
3341 loop. */
3342 for (globals = Fdefault_value (Qfontification_functions);
3343 CONSP (globals);
3344 globals = XCDR (globals))
3345 {
3346 fn = XCAR (globals);
3347 if (!EQ (fn, Qt))
3348 safe_call1 (fn, pos);
3349 }
3350 }
3351 else
3352 safe_call1 (fn, pos);
3353 }
3354
3355 UNGCPRO;
3356 }
3357
3358 unbind_to (count, Qnil);
3359
3360 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3361 something. This avoids an endless loop if they failed to
3362 fontify the text for which reason ever. */
3363 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3364 handled = HANDLED_RECOMPUTE_PROPS;
3365 }
3366
3367 return handled;
3368 }
3369
3370
3371 \f
3372 /***********************************************************************
3373 Faces
3374 ***********************************************************************/
3375
3376 /* Set up iterator IT from face properties at its current position.
3377 Called from handle_stop. */
3378
3379 static enum prop_handled
3380 handle_face_prop (it)
3381 struct it *it;
3382 {
3383 int new_face_id;
3384 EMACS_INT next_stop;
3385
3386 if (!STRINGP (it->string))
3387 {
3388 new_face_id
3389 = face_at_buffer_position (it->w,
3390 IT_CHARPOS (*it),
3391 it->region_beg_charpos,
3392 it->region_end_charpos,
3393 &next_stop,
3394 (IT_CHARPOS (*it)
3395 + TEXT_PROP_DISTANCE_LIMIT),
3396 0);
3397
3398 /* Is this a start of a run of characters with box face?
3399 Caveat: this can be called for a freshly initialized
3400 iterator; face_id is -1 in this case. We know that the new
3401 face will not change until limit, i.e. if the new face has a
3402 box, all characters up to limit will have one. But, as
3403 usual, we don't know whether limit is really the end. */
3404 if (new_face_id != it->face_id)
3405 {
3406 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3407
3408 /* If new face has a box but old face has not, this is
3409 the start of a run of characters with box, i.e. it has
3410 a shadow on the left side. The value of face_id of the
3411 iterator will be -1 if this is the initial call that gets
3412 the face. In this case, we have to look in front of IT's
3413 position and see whether there is a face != new_face_id. */
3414 it->start_of_box_run_p
3415 = (new_face->box != FACE_NO_BOX
3416 && (it->face_id >= 0
3417 || IT_CHARPOS (*it) == BEG
3418 || new_face_id != face_before_it_pos (it)));
3419 it->face_box_p = new_face->box != FACE_NO_BOX;
3420 }
3421 }
3422 else
3423 {
3424 int base_face_id, bufpos;
3425 int i;
3426 Lisp_Object from_overlay
3427 = (it->current.overlay_string_index >= 0
3428 ? it->string_overlays[it->current.overlay_string_index]
3429 : Qnil);
3430
3431 /* See if we got to this string directly or indirectly from
3432 an overlay property. That includes the before-string or
3433 after-string of an overlay, strings in display properties
3434 provided by an overlay, their text properties, etc.
3435
3436 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3437 if (! NILP (from_overlay))
3438 for (i = it->sp - 1; i >= 0; i--)
3439 {
3440 if (it->stack[i].current.overlay_string_index >= 0)
3441 from_overlay
3442 = it->string_overlays[it->stack[i].current.overlay_string_index];
3443 else if (! NILP (it->stack[i].from_overlay))
3444 from_overlay = it->stack[i].from_overlay;
3445
3446 if (!NILP (from_overlay))
3447 break;
3448 }
3449
3450 if (! NILP (from_overlay))
3451 {
3452 bufpos = IT_CHARPOS (*it);
3453 /* For a string from an overlay, the base face depends
3454 only on text properties and ignores overlays. */
3455 base_face_id
3456 = face_for_overlay_string (it->w,
3457 IT_CHARPOS (*it),
3458 it->region_beg_charpos,
3459 it->region_end_charpos,
3460 &next_stop,
3461 (IT_CHARPOS (*it)
3462 + TEXT_PROP_DISTANCE_LIMIT),
3463 0,
3464 from_overlay);
3465 }
3466 else
3467 {
3468 bufpos = 0;
3469
3470 /* For strings from a `display' property, use the face at
3471 IT's current buffer position as the base face to merge
3472 with, so that overlay strings appear in the same face as
3473 surrounding text, unless they specify their own
3474 faces. */
3475 base_face_id = underlying_face_id (it);
3476 }
3477
3478 new_face_id = face_at_string_position (it->w,
3479 it->string,
3480 IT_STRING_CHARPOS (*it),
3481 bufpos,
3482 it->region_beg_charpos,
3483 it->region_end_charpos,
3484 &next_stop,
3485 base_face_id, 0);
3486
3487 #if 0 /* This shouldn't be neccessary. Let's check it. */
3488 /* If IT is used to display a mode line we would really like to
3489 use the mode line face instead of the frame's default face. */
3490 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3491 && new_face_id == DEFAULT_FACE_ID)
3492 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3493 #endif
3494
3495 /* Is this a start of a run of characters with box? Caveat:
3496 this can be called for a freshly allocated iterator; face_id
3497 is -1 is this case. We know that the new face will not
3498 change until the next check pos, i.e. if the new face has a
3499 box, all characters up to that position will have a
3500 box. But, as usual, we don't know whether that position
3501 is really the end. */
3502 if (new_face_id != it->face_id)
3503 {
3504 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3505 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3506
3507 /* If new face has a box but old face hasn't, this is the
3508 start of a run of characters with box, i.e. it has a
3509 shadow on the left side. */
3510 it->start_of_box_run_p
3511 = new_face->box && (old_face == NULL || !old_face->box);
3512 it->face_box_p = new_face->box != FACE_NO_BOX;
3513 }
3514 }
3515
3516 it->face_id = new_face_id;
3517 return HANDLED_NORMALLY;
3518 }
3519
3520
3521 /* Return the ID of the face ``underlying'' IT's current position,
3522 which is in a string. If the iterator is associated with a
3523 buffer, return the face at IT's current buffer position.
3524 Otherwise, use the iterator's base_face_id. */
3525
3526 static int
3527 underlying_face_id (it)
3528 struct it *it;
3529 {
3530 int face_id = it->base_face_id, i;
3531
3532 xassert (STRINGP (it->string));
3533
3534 for (i = it->sp - 1; i >= 0; --i)
3535 if (NILP (it->stack[i].string))
3536 face_id = it->stack[i].face_id;
3537
3538 return face_id;
3539 }
3540
3541
3542 /* Compute the face one character before or after the current position
3543 of IT. BEFORE_P non-zero means get the face in front of IT's
3544 position. Value is the id of the face. */
3545
3546 static int
3547 face_before_or_after_it_pos (it, before_p)
3548 struct it *it;
3549 int before_p;
3550 {
3551 int face_id, limit;
3552 EMACS_INT next_check_charpos;
3553 struct text_pos pos;
3554
3555 xassert (it->s == NULL);
3556
3557 if (STRINGP (it->string))
3558 {
3559 int bufpos, base_face_id;
3560
3561 /* No face change past the end of the string (for the case
3562 we are padding with spaces). No face change before the
3563 string start. */
3564 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3565 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3566 return it->face_id;
3567
3568 /* Set pos to the position before or after IT's current position. */
3569 if (before_p)
3570 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3571 else
3572 /* For composition, we must check the character after the
3573 composition. */
3574 pos = (it->what == IT_COMPOSITION
3575 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3576 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3577
3578 if (it->current.overlay_string_index >= 0)
3579 bufpos = IT_CHARPOS (*it);
3580 else
3581 bufpos = 0;
3582
3583 base_face_id = underlying_face_id (it);
3584
3585 /* Get the face for ASCII, or unibyte. */
3586 face_id = face_at_string_position (it->w,
3587 it->string,
3588 CHARPOS (pos),
3589 bufpos,
3590 it->region_beg_charpos,
3591 it->region_end_charpos,
3592 &next_check_charpos,
3593 base_face_id, 0);
3594
3595 /* Correct the face for charsets different from ASCII. Do it
3596 for the multibyte case only. The face returned above is
3597 suitable for unibyte text if IT->string is unibyte. */
3598 if (STRING_MULTIBYTE (it->string))
3599 {
3600 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3601 int rest = SBYTES (it->string) - BYTEPOS (pos);
3602 int c, len;
3603 struct face *face = FACE_FROM_ID (it->f, face_id);
3604
3605 c = string_char_and_length (p, rest, &len);
3606 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3607 }
3608 }
3609 else
3610 {
3611 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3612 || (IT_CHARPOS (*it) <= BEGV && before_p))
3613 return it->face_id;
3614
3615 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3616 pos = it->current.pos;
3617
3618 if (before_p)
3619 DEC_TEXT_POS (pos, it->multibyte_p);
3620 else
3621 {
3622 if (it->what == IT_COMPOSITION)
3623 /* For composition, we must check the position after the
3624 composition. */
3625 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3626 else
3627 INC_TEXT_POS (pos, it->multibyte_p);
3628 }
3629
3630 /* Determine face for CHARSET_ASCII, or unibyte. */
3631 face_id = face_at_buffer_position (it->w,
3632 CHARPOS (pos),
3633 it->region_beg_charpos,
3634 it->region_end_charpos,
3635 &next_check_charpos,
3636 limit, 0);
3637
3638 /* Correct the face for charsets different from ASCII. Do it
3639 for the multibyte case only. The face returned above is
3640 suitable for unibyte text if current_buffer is unibyte. */
3641 if (it->multibyte_p)
3642 {
3643 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3644 struct face *face = FACE_FROM_ID (it->f, face_id);
3645 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3646 }
3647 }
3648
3649 return face_id;
3650 }
3651
3652
3653 \f
3654 /***********************************************************************
3655 Invisible text
3656 ***********************************************************************/
3657
3658 /* Set up iterator IT from invisible properties at its current
3659 position. Called from handle_stop. */
3660
3661 static enum prop_handled
3662 handle_invisible_prop (it)
3663 struct it *it;
3664 {
3665 enum prop_handled handled = HANDLED_NORMALLY;
3666
3667 if (STRINGP (it->string))
3668 {
3669 extern Lisp_Object Qinvisible;
3670 Lisp_Object prop, end_charpos, limit, charpos;
3671
3672 /* Get the value of the invisible text property at the
3673 current position. Value will be nil if there is no such
3674 property. */
3675 charpos = make_number (IT_STRING_CHARPOS (*it));
3676 prop = Fget_text_property (charpos, Qinvisible, it->string);
3677
3678 if (!NILP (prop)
3679 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3680 {
3681 handled = HANDLED_RECOMPUTE_PROPS;
3682
3683 /* Get the position at which the next change of the
3684 invisible text property can be found in IT->string.
3685 Value will be nil if the property value is the same for
3686 all the rest of IT->string. */
3687 XSETINT (limit, SCHARS (it->string));
3688 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3689 it->string, limit);
3690
3691 /* Text at current position is invisible. The next
3692 change in the property is at position end_charpos.
3693 Move IT's current position to that position. */
3694 if (INTEGERP (end_charpos)
3695 && XFASTINT (end_charpos) < XFASTINT (limit))
3696 {
3697 struct text_pos old;
3698 old = it->current.string_pos;
3699 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3700 compute_string_pos (&it->current.string_pos, old, it->string);
3701 }
3702 else
3703 {
3704 /* The rest of the string is invisible. If this is an
3705 overlay string, proceed with the next overlay string
3706 or whatever comes and return a character from there. */
3707 if (it->current.overlay_string_index >= 0)
3708 {
3709 next_overlay_string (it);
3710 /* Don't check for overlay strings when we just
3711 finished processing them. */
3712 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3713 }
3714 else
3715 {
3716 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3717 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3718 }
3719 }
3720 }
3721 }
3722 else
3723 {
3724 int invis_p;
3725 EMACS_INT newpos, next_stop, start_charpos;
3726 Lisp_Object pos, prop, overlay;
3727
3728 /* First of all, is there invisible text at this position? */
3729 start_charpos = IT_CHARPOS (*it);
3730 pos = make_number (IT_CHARPOS (*it));
3731 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3732 &overlay);
3733 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3734
3735 /* If we are on invisible text, skip over it. */
3736 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3737 {
3738 /* Record whether we have to display an ellipsis for the
3739 invisible text. */
3740 int display_ellipsis_p = invis_p == 2;
3741
3742 handled = HANDLED_RECOMPUTE_PROPS;
3743
3744 /* Loop skipping over invisible text. The loop is left at
3745 ZV or with IT on the first char being visible again. */
3746 do
3747 {
3748 /* Try to skip some invisible text. Return value is the
3749 position reached which can be equal to IT's position
3750 if there is nothing invisible here. This skips both
3751 over invisible text properties and overlays with
3752 invisible property. */
3753 newpos = skip_invisible (IT_CHARPOS (*it),
3754 &next_stop, ZV, it->window);
3755
3756 /* If we skipped nothing at all we weren't at invisible
3757 text in the first place. If everything to the end of
3758 the buffer was skipped, end the loop. */
3759 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3760 invis_p = 0;
3761 else
3762 {
3763 /* We skipped some characters but not necessarily
3764 all there are. Check if we ended up on visible
3765 text. Fget_char_property returns the property of
3766 the char before the given position, i.e. if we
3767 get invis_p = 0, this means that the char at
3768 newpos is visible. */
3769 pos = make_number (newpos);
3770 prop = Fget_char_property (pos, Qinvisible, it->window);
3771 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3772 }
3773
3774 /* If we ended up on invisible text, proceed to
3775 skip starting with next_stop. */
3776 if (invis_p)
3777 IT_CHARPOS (*it) = next_stop;
3778
3779 /* If there are adjacent invisible texts, don't lose the
3780 second one's ellipsis. */
3781 if (invis_p == 2)
3782 display_ellipsis_p = 1;
3783 }
3784 while (invis_p);
3785
3786 /* The position newpos is now either ZV or on visible text. */
3787 IT_CHARPOS (*it) = newpos;
3788 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3789
3790 /* If there are before-strings at the start of invisible
3791 text, and the text is invisible because of a text
3792 property, arrange to show before-strings because 20.x did
3793 it that way. (If the text is invisible because of an
3794 overlay property instead of a text property, this is
3795 already handled in the overlay code.) */
3796 if (NILP (overlay)
3797 && get_overlay_strings (it, start_charpos))
3798 {
3799 handled = HANDLED_RECOMPUTE_PROPS;
3800 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3801 }
3802 else if (display_ellipsis_p)
3803 {
3804 /* Make sure that the glyphs of the ellipsis will get
3805 correct `charpos' values. If we would not update
3806 it->position here, the glyphs would belong to the
3807 last visible character _before_ the invisible
3808 text, which confuses `set_cursor_from_row'.
3809
3810 We use the last invisible position instead of the
3811 first because this way the cursor is always drawn on
3812 the first "." of the ellipsis, whenever PT is inside
3813 the invisible text. Otherwise the cursor would be
3814 placed _after_ the ellipsis when the point is after the
3815 first invisible character. */
3816 if (!STRINGP (it->object))
3817 {
3818 it->position.charpos = IT_CHARPOS (*it) - 1;
3819 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3820 }
3821 setup_for_ellipsis (it, 0);
3822 /* Let the ellipsis display before
3823 considering any properties of the following char.
3824 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3825 handled = HANDLED_RETURN;
3826 }
3827 }
3828 }
3829
3830 return handled;
3831 }
3832
3833
3834 /* Make iterator IT return `...' next.
3835 Replaces LEN characters from buffer. */
3836
3837 static void
3838 setup_for_ellipsis (it, len)
3839 struct it *it;
3840 int len;
3841 {
3842 /* Use the display table definition for `...'. Invalid glyphs
3843 will be handled by the method returning elements from dpvec. */
3844 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3845 {
3846 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3847 it->dpvec = v->contents;
3848 it->dpend = v->contents + v->size;
3849 }
3850 else
3851 {
3852 /* Default `...'. */
3853 it->dpvec = default_invis_vector;
3854 it->dpend = default_invis_vector + 3;
3855 }
3856
3857 it->dpvec_char_len = len;
3858 it->current.dpvec_index = 0;
3859 it->dpvec_face_id = -1;
3860
3861 /* Remember the current face id in case glyphs specify faces.
3862 IT's face is restored in set_iterator_to_next.
3863 saved_face_id was set to preceding char's face in handle_stop. */
3864 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3865 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3866
3867 it->method = GET_FROM_DISPLAY_VECTOR;
3868 it->ellipsis_p = 1;
3869 }
3870
3871
3872 \f
3873 /***********************************************************************
3874 'display' property
3875 ***********************************************************************/
3876
3877 /* Set up iterator IT from `display' property at its current position.
3878 Called from handle_stop.
3879 We return HANDLED_RETURN if some part of the display property
3880 overrides the display of the buffer text itself.
3881 Otherwise we return HANDLED_NORMALLY. */
3882
3883 static enum prop_handled
3884 handle_display_prop (it)
3885 struct it *it;
3886 {
3887 Lisp_Object prop, object, overlay;
3888 struct text_pos *position;
3889 /* Nonzero if some property replaces the display of the text itself. */
3890 int display_replaced_p = 0;
3891
3892 if (STRINGP (it->string))
3893 {
3894 object = it->string;
3895 position = &it->current.string_pos;
3896 }
3897 else
3898 {
3899 XSETWINDOW (object, it->w);
3900 position = &it->current.pos;
3901 }
3902
3903 /* Reset those iterator values set from display property values. */
3904 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3905 it->space_width = Qnil;
3906 it->font_height = Qnil;
3907 it->voffset = 0;
3908
3909 /* We don't support recursive `display' properties, i.e. string
3910 values that have a string `display' property, that have a string
3911 `display' property etc. */
3912 if (!it->string_from_display_prop_p)
3913 it->area = TEXT_AREA;
3914
3915 prop = get_char_property_and_overlay (make_number (position->charpos),
3916 Qdisplay, object, &overlay);
3917 if (NILP (prop))
3918 return HANDLED_NORMALLY;
3919 /* Now OVERLAY is the overlay that gave us this property, or nil
3920 if it was a text property. */
3921
3922 if (!STRINGP (it->string))
3923 object = it->w->buffer;
3924
3925 if (CONSP (prop)
3926 /* Simple properties. */
3927 && !EQ (XCAR (prop), Qimage)
3928 && !EQ (XCAR (prop), Qspace)
3929 && !EQ (XCAR (prop), Qwhen)
3930 && !EQ (XCAR (prop), Qslice)
3931 && !EQ (XCAR (prop), Qspace_width)
3932 && !EQ (XCAR (prop), Qheight)
3933 && !EQ (XCAR (prop), Qraise)
3934 /* Marginal area specifications. */
3935 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3936 && !EQ (XCAR (prop), Qleft_fringe)
3937 && !EQ (XCAR (prop), Qright_fringe)
3938 && !NILP (XCAR (prop)))
3939 {
3940 for (; CONSP (prop); prop = XCDR (prop))
3941 {
3942 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3943 position, display_replaced_p))
3944 {
3945 display_replaced_p = 1;
3946 /* If some text in a string is replaced, `position' no
3947 longer points to the position of `object'. */
3948 if (STRINGP (object))
3949 break;
3950 }
3951 }
3952 }
3953 else if (VECTORP (prop))
3954 {
3955 int i;
3956 for (i = 0; i < ASIZE (prop); ++i)
3957 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3958 position, display_replaced_p))
3959 {
3960 display_replaced_p = 1;
3961 /* If some text in a string is replaced, `position' no
3962 longer points to the position of `object'. */
3963 if (STRINGP (object))
3964 break;
3965 }
3966 }
3967 else
3968 {
3969 int ret = handle_single_display_spec (it, prop, object, overlay,
3970 position, 0);
3971 if (ret < 0) /* Replaced by "", i.e. nothing. */
3972 return HANDLED_RECOMPUTE_PROPS;
3973 if (ret)
3974 display_replaced_p = 1;
3975 }
3976
3977 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3978 }
3979
3980
3981 /* Value is the position of the end of the `display' property starting
3982 at START_POS in OBJECT. */
3983
3984 static struct text_pos
3985 display_prop_end (it, object, start_pos)
3986 struct it *it;
3987 Lisp_Object object;
3988 struct text_pos start_pos;
3989 {
3990 Lisp_Object end;
3991 struct text_pos end_pos;
3992
3993 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3994 Qdisplay, object, Qnil);
3995 CHARPOS (end_pos) = XFASTINT (end);
3996 if (STRINGP (object))
3997 compute_string_pos (&end_pos, start_pos, it->string);
3998 else
3999 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4000
4001 return end_pos;
4002 }
4003
4004
4005 /* Set up IT from a single `display' specification PROP. OBJECT
4006 is the object in which the `display' property was found. *POSITION
4007 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4008 means that we previously saw a display specification which already
4009 replaced text display with something else, for example an image;
4010 we ignore such properties after the first one has been processed.
4011
4012 OVERLAY is the overlay this `display' property came from,
4013 or nil if it was a text property.
4014
4015 If PROP is a `space' or `image' specification, and in some other
4016 cases too, set *POSITION to the position where the `display'
4017 property ends.
4018
4019 Value is non-zero if something was found which replaces the display
4020 of buffer or string text. Specifically, the value is -1 if that
4021 "something" is "nothing". */
4022
4023 static int
4024 handle_single_display_spec (it, spec, object, overlay, position,
4025 display_replaced_before_p)
4026 struct it *it;
4027 Lisp_Object spec;
4028 Lisp_Object object;
4029 Lisp_Object overlay;
4030 struct text_pos *position;
4031 int display_replaced_before_p;
4032 {
4033 Lisp_Object form;
4034 Lisp_Object location, value;
4035 struct text_pos start_pos, save_pos;
4036 int valid_p;
4037
4038 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4039 If the result is non-nil, use VALUE instead of SPEC. */
4040 form = Qt;
4041 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4042 {
4043 spec = XCDR (spec);
4044 if (!CONSP (spec))
4045 return 0;
4046 form = XCAR (spec);
4047 spec = XCDR (spec);
4048 }
4049
4050 if (!NILP (form) && !EQ (form, Qt))
4051 {
4052 int count = SPECPDL_INDEX ();
4053 struct gcpro gcpro1;
4054
4055 /* Bind `object' to the object having the `display' property, a
4056 buffer or string. Bind `position' to the position in the
4057 object where the property was found, and `buffer-position'
4058 to the current position in the buffer. */
4059 specbind (Qobject, object);
4060 specbind (Qposition, make_number (CHARPOS (*position)));
4061 specbind (Qbuffer_position,
4062 make_number (STRINGP (object)
4063 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4064 GCPRO1 (form);
4065 form = safe_eval (form);
4066 UNGCPRO;
4067 unbind_to (count, Qnil);
4068 }
4069
4070 if (NILP (form))
4071 return 0;
4072
4073 /* Handle `(height HEIGHT)' specifications. */
4074 if (CONSP (spec)
4075 && EQ (XCAR (spec), Qheight)
4076 && CONSP (XCDR (spec)))
4077 {
4078 if (!FRAME_WINDOW_P (it->f))
4079 return 0;
4080
4081 it->font_height = XCAR (XCDR (spec));
4082 if (!NILP (it->font_height))
4083 {
4084 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4085 int new_height = -1;
4086
4087 if (CONSP (it->font_height)
4088 && (EQ (XCAR (it->font_height), Qplus)
4089 || EQ (XCAR (it->font_height), Qminus))
4090 && CONSP (XCDR (it->font_height))
4091 && INTEGERP (XCAR (XCDR (it->font_height))))
4092 {
4093 /* `(+ N)' or `(- N)' where N is an integer. */
4094 int steps = XINT (XCAR (XCDR (it->font_height)));
4095 if (EQ (XCAR (it->font_height), Qplus))
4096 steps = - steps;
4097 it->face_id = smaller_face (it->f, it->face_id, steps);
4098 }
4099 else if (FUNCTIONP (it->font_height))
4100 {
4101 /* Call function with current height as argument.
4102 Value is the new height. */
4103 Lisp_Object height;
4104 height = safe_call1 (it->font_height,
4105 face->lface[LFACE_HEIGHT_INDEX]);
4106 if (NUMBERP (height))
4107 new_height = XFLOATINT (height);
4108 }
4109 else if (NUMBERP (it->font_height))
4110 {
4111 /* Value is a multiple of the canonical char height. */
4112 struct face *face;
4113
4114 face = FACE_FROM_ID (it->f,
4115 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4116 new_height = (XFLOATINT (it->font_height)
4117 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4118 }
4119 else
4120 {
4121 /* Evaluate IT->font_height with `height' bound to the
4122 current specified height to get the new height. */
4123 int count = SPECPDL_INDEX ();
4124
4125 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4126 value = safe_eval (it->font_height);
4127 unbind_to (count, Qnil);
4128
4129 if (NUMBERP (value))
4130 new_height = XFLOATINT (value);
4131 }
4132
4133 if (new_height > 0)
4134 it->face_id = face_with_height (it->f, it->face_id, new_height);
4135 }
4136
4137 return 0;
4138 }
4139
4140 /* Handle `(space-width WIDTH)'. */
4141 if (CONSP (spec)
4142 && EQ (XCAR (spec), Qspace_width)
4143 && CONSP (XCDR (spec)))
4144 {
4145 if (!FRAME_WINDOW_P (it->f))
4146 return 0;
4147
4148 value = XCAR (XCDR (spec));
4149 if (NUMBERP (value) && XFLOATINT (value) > 0)
4150 it->space_width = value;
4151
4152 return 0;
4153 }
4154
4155 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4156 if (CONSP (spec)
4157 && EQ (XCAR (spec), Qslice))
4158 {
4159 Lisp_Object tem;
4160
4161 if (!FRAME_WINDOW_P (it->f))
4162 return 0;
4163
4164 if (tem = XCDR (spec), CONSP (tem))
4165 {
4166 it->slice.x = XCAR (tem);
4167 if (tem = XCDR (tem), CONSP (tem))
4168 {
4169 it->slice.y = XCAR (tem);
4170 if (tem = XCDR (tem), CONSP (tem))
4171 {
4172 it->slice.width = XCAR (tem);
4173 if (tem = XCDR (tem), CONSP (tem))
4174 it->slice.height = XCAR (tem);
4175 }
4176 }
4177 }
4178
4179 return 0;
4180 }
4181
4182 /* Handle `(raise FACTOR)'. */
4183 if (CONSP (spec)
4184 && EQ (XCAR (spec), Qraise)
4185 && CONSP (XCDR (spec)))
4186 {
4187 if (!FRAME_WINDOW_P (it->f))
4188 return 0;
4189
4190 #ifdef HAVE_WINDOW_SYSTEM
4191 value = XCAR (XCDR (spec));
4192 if (NUMBERP (value))
4193 {
4194 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4195 it->voffset = - (XFLOATINT (value)
4196 * (FONT_HEIGHT (face->font)));
4197 }
4198 #endif /* HAVE_WINDOW_SYSTEM */
4199
4200 return 0;
4201 }
4202
4203 /* Don't handle the other kinds of display specifications
4204 inside a string that we got from a `display' property. */
4205 if (it->string_from_display_prop_p)
4206 return 0;
4207
4208 /* Characters having this form of property are not displayed, so
4209 we have to find the end of the property. */
4210 start_pos = *position;
4211 *position = display_prop_end (it, object, start_pos);
4212 value = Qnil;
4213
4214 /* Stop the scan at that end position--we assume that all
4215 text properties change there. */
4216 it->stop_charpos = position->charpos;
4217
4218 /* Handle `(left-fringe BITMAP [FACE])'
4219 and `(right-fringe BITMAP [FACE])'. */
4220 if (CONSP (spec)
4221 && (EQ (XCAR (spec), Qleft_fringe)
4222 || EQ (XCAR (spec), Qright_fringe))
4223 && CONSP (XCDR (spec)))
4224 {
4225 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4226 int fringe_bitmap;
4227
4228 if (!FRAME_WINDOW_P (it->f))
4229 /* If we return here, POSITION has been advanced
4230 across the text with this property. */
4231 return 0;
4232
4233 #ifdef HAVE_WINDOW_SYSTEM
4234 value = XCAR (XCDR (spec));
4235 if (!SYMBOLP (value)
4236 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4237 /* If we return here, POSITION has been advanced
4238 across the text with this property. */
4239 return 0;
4240
4241 if (CONSP (XCDR (XCDR (spec))))
4242 {
4243 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4244 int face_id2 = lookup_derived_face (it->f, face_name,
4245 FRINGE_FACE_ID, 0);
4246 if (face_id2 >= 0)
4247 face_id = face_id2;
4248 }
4249
4250 /* Save current settings of IT so that we can restore them
4251 when we are finished with the glyph property value. */
4252
4253 save_pos = it->position;
4254 it->position = *position;
4255 push_it (it);
4256 it->position = save_pos;
4257
4258 it->area = TEXT_AREA;
4259 it->what = IT_IMAGE;
4260 it->image_id = -1; /* no image */
4261 it->position = start_pos;
4262 it->object = NILP (object) ? it->w->buffer : object;
4263 it->method = GET_FROM_IMAGE;
4264 it->from_overlay = Qnil;
4265 it->face_id = face_id;
4266
4267 /* Say that we haven't consumed the characters with
4268 `display' property yet. The call to pop_it in
4269 set_iterator_to_next will clean this up. */
4270 *position = start_pos;
4271
4272 if (EQ (XCAR (spec), Qleft_fringe))
4273 {
4274 it->left_user_fringe_bitmap = fringe_bitmap;
4275 it->left_user_fringe_face_id = face_id;
4276 }
4277 else
4278 {
4279 it->right_user_fringe_bitmap = fringe_bitmap;
4280 it->right_user_fringe_face_id = face_id;
4281 }
4282 #endif /* HAVE_WINDOW_SYSTEM */
4283 return 1;
4284 }
4285
4286 /* Prepare to handle `((margin left-margin) ...)',
4287 `((margin right-margin) ...)' and `((margin nil) ...)'
4288 prefixes for display specifications. */
4289 location = Qunbound;
4290 if (CONSP (spec) && CONSP (XCAR (spec)))
4291 {
4292 Lisp_Object tem;
4293
4294 value = XCDR (spec);
4295 if (CONSP (value))
4296 value = XCAR (value);
4297
4298 tem = XCAR (spec);
4299 if (EQ (XCAR (tem), Qmargin)
4300 && (tem = XCDR (tem),
4301 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4302 (NILP (tem)
4303 || EQ (tem, Qleft_margin)
4304 || EQ (tem, Qright_margin))))
4305 location = tem;
4306 }
4307
4308 if (EQ (location, Qunbound))
4309 {
4310 location = Qnil;
4311 value = spec;
4312 }
4313
4314 /* After this point, VALUE is the property after any
4315 margin prefix has been stripped. It must be a string,
4316 an image specification, or `(space ...)'.
4317
4318 LOCATION specifies where to display: `left-margin',
4319 `right-margin' or nil. */
4320
4321 valid_p = (STRINGP (value)
4322 #ifdef HAVE_WINDOW_SYSTEM
4323 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4324 #endif /* not HAVE_WINDOW_SYSTEM */
4325 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4326
4327 if (valid_p && !display_replaced_before_p)
4328 {
4329 /* Save current settings of IT so that we can restore them
4330 when we are finished with the glyph property value. */
4331 save_pos = it->position;
4332 it->position = *position;
4333 push_it (it);
4334 it->position = save_pos;
4335 it->from_overlay = overlay;
4336
4337 if (NILP (location))
4338 it->area = TEXT_AREA;
4339 else if (EQ (location, Qleft_margin))
4340 it->area = LEFT_MARGIN_AREA;
4341 else
4342 it->area = RIGHT_MARGIN_AREA;
4343
4344 if (STRINGP (value))
4345 {
4346 if (SCHARS (value) == 0)
4347 {
4348 pop_it (it);
4349 return -1; /* Replaced by "", i.e. nothing. */
4350 }
4351 it->string = value;
4352 it->multibyte_p = STRING_MULTIBYTE (it->string);
4353 it->current.overlay_string_index = -1;
4354 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4355 it->end_charpos = it->string_nchars = SCHARS (it->string);
4356 it->method = GET_FROM_STRING;
4357 it->stop_charpos = 0;
4358 it->string_from_display_prop_p = 1;
4359 /* Say that we haven't consumed the characters with
4360 `display' property yet. The call to pop_it in
4361 set_iterator_to_next will clean this up. */
4362 if (BUFFERP (object))
4363 *position = start_pos;
4364 }
4365 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4366 {
4367 it->method = GET_FROM_STRETCH;
4368 it->object = value;
4369 *position = it->position = start_pos;
4370 }
4371 #ifdef HAVE_WINDOW_SYSTEM
4372 else
4373 {
4374 it->what = IT_IMAGE;
4375 it->image_id = lookup_image (it->f, value);
4376 it->position = start_pos;
4377 it->object = NILP (object) ? it->w->buffer : object;
4378 it->method = GET_FROM_IMAGE;
4379
4380 /* Say that we haven't consumed the characters with
4381 `display' property yet. The call to pop_it in
4382 set_iterator_to_next will clean this up. */
4383 *position = start_pos;
4384 }
4385 #endif /* HAVE_WINDOW_SYSTEM */
4386
4387 return 1;
4388 }
4389
4390 /* Invalid property or property not supported. Restore
4391 POSITION to what it was before. */
4392 *position = start_pos;
4393 return 0;
4394 }
4395
4396
4397 /* Check if SPEC is a display sub-property value whose text should be
4398 treated as intangible. */
4399
4400 static int
4401 single_display_spec_intangible_p (prop)
4402 Lisp_Object prop;
4403 {
4404 /* Skip over `when FORM'. */
4405 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4406 {
4407 prop = XCDR (prop);
4408 if (!CONSP (prop))
4409 return 0;
4410 prop = XCDR (prop);
4411 }
4412
4413 if (STRINGP (prop))
4414 return 1;
4415
4416 if (!CONSP (prop))
4417 return 0;
4418
4419 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4420 we don't need to treat text as intangible. */
4421 if (EQ (XCAR (prop), Qmargin))
4422 {
4423 prop = XCDR (prop);
4424 if (!CONSP (prop))
4425 return 0;
4426
4427 prop = XCDR (prop);
4428 if (!CONSP (prop)
4429 || EQ (XCAR (prop), Qleft_margin)
4430 || EQ (XCAR (prop), Qright_margin))
4431 return 0;
4432 }
4433
4434 return (CONSP (prop)
4435 && (EQ (XCAR (prop), Qimage)
4436 || EQ (XCAR (prop), Qspace)));
4437 }
4438
4439
4440 /* Check if PROP is a display property value whose text should be
4441 treated as intangible. */
4442
4443 int
4444 display_prop_intangible_p (prop)
4445 Lisp_Object prop;
4446 {
4447 if (CONSP (prop)
4448 && CONSP (XCAR (prop))
4449 && !EQ (Qmargin, XCAR (XCAR (prop))))
4450 {
4451 /* A list of sub-properties. */
4452 while (CONSP (prop))
4453 {
4454 if (single_display_spec_intangible_p (XCAR (prop)))
4455 return 1;
4456 prop = XCDR (prop);
4457 }
4458 }
4459 else if (VECTORP (prop))
4460 {
4461 /* A vector of sub-properties. */
4462 int i;
4463 for (i = 0; i < ASIZE (prop); ++i)
4464 if (single_display_spec_intangible_p (AREF (prop, i)))
4465 return 1;
4466 }
4467 else
4468 return single_display_spec_intangible_p (prop);
4469
4470 return 0;
4471 }
4472
4473
4474 /* Return 1 if PROP is a display sub-property value containing STRING. */
4475
4476 static int
4477 single_display_spec_string_p (prop, string)
4478 Lisp_Object prop, string;
4479 {
4480 if (EQ (string, prop))
4481 return 1;
4482
4483 /* Skip over `when FORM'. */
4484 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4485 {
4486 prop = XCDR (prop);
4487 if (!CONSP (prop))
4488 return 0;
4489 prop = XCDR (prop);
4490 }
4491
4492 if (CONSP (prop))
4493 /* Skip over `margin LOCATION'. */
4494 if (EQ (XCAR (prop), Qmargin))
4495 {
4496 prop = XCDR (prop);
4497 if (!CONSP (prop))
4498 return 0;
4499
4500 prop = XCDR (prop);
4501 if (!CONSP (prop))
4502 return 0;
4503 }
4504
4505 return CONSP (prop) && EQ (XCAR (prop), string);
4506 }
4507
4508
4509 /* Return 1 if STRING appears in the `display' property PROP. */
4510
4511 static int
4512 display_prop_string_p (prop, string)
4513 Lisp_Object prop, string;
4514 {
4515 if (CONSP (prop)
4516 && CONSP (XCAR (prop))
4517 && !EQ (Qmargin, XCAR (XCAR (prop))))
4518 {
4519 /* A list of sub-properties. */
4520 while (CONSP (prop))
4521 {
4522 if (single_display_spec_string_p (XCAR (prop), string))
4523 return 1;
4524 prop = XCDR (prop);
4525 }
4526 }
4527 else if (VECTORP (prop))
4528 {
4529 /* A vector of sub-properties. */
4530 int i;
4531 for (i = 0; i < ASIZE (prop); ++i)
4532 if (single_display_spec_string_p (AREF (prop, i), string))
4533 return 1;
4534 }
4535 else
4536 return single_display_spec_string_p (prop, string);
4537
4538 return 0;
4539 }
4540
4541
4542 /* Determine from which buffer position in W's buffer STRING comes
4543 from. AROUND_CHARPOS is an approximate position where it could
4544 be from. Value is the buffer position or 0 if it couldn't be
4545 determined.
4546
4547 W's buffer must be current.
4548
4549 This function is necessary because we don't record buffer positions
4550 in glyphs generated from strings (to keep struct glyph small).
4551 This function may only use code that doesn't eval because it is
4552 called asynchronously from note_mouse_highlight. */
4553
4554 int
4555 string_buffer_position (w, string, around_charpos)
4556 struct window *w;
4557 Lisp_Object string;
4558 int around_charpos;
4559 {
4560 Lisp_Object limit, prop, pos;
4561 const int MAX_DISTANCE = 1000;
4562 int found = 0;
4563
4564 pos = make_number (around_charpos);
4565 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4566 while (!found && !EQ (pos, limit))
4567 {
4568 prop = Fget_char_property (pos, Qdisplay, Qnil);
4569 if (!NILP (prop) && display_prop_string_p (prop, string))
4570 found = 1;
4571 else
4572 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4573 }
4574
4575 if (!found)
4576 {
4577 pos = make_number (around_charpos);
4578 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4579 while (!found && !EQ (pos, limit))
4580 {
4581 prop = Fget_char_property (pos, Qdisplay, Qnil);
4582 if (!NILP (prop) && display_prop_string_p (prop, string))
4583 found = 1;
4584 else
4585 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4586 limit);
4587 }
4588 }
4589
4590 return found ? XINT (pos) : 0;
4591 }
4592
4593
4594 \f
4595 /***********************************************************************
4596 `composition' property
4597 ***********************************************************************/
4598
4599 static enum prop_handled
4600 handle_auto_composed_prop (it)
4601 struct it *it;
4602 {
4603 enum prop_handled handled = HANDLED_NORMALLY;
4604
4605 if (FUNCTIONP (Vauto_composition_function))
4606 {
4607 Lisp_Object val = Qnil;
4608 EMACS_INT pos, limit = -1;
4609
4610 if (STRINGP (it->string))
4611 pos = IT_STRING_CHARPOS (*it);
4612 else
4613 pos = IT_CHARPOS (*it);
4614
4615 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4616 if (! NILP (val))
4617 {
4618 Lisp_Object cmp_prop;
4619 EMACS_INT cmp_start, cmp_end;
4620
4621 if (get_property_and_range (pos, Qcomposition, &cmp_prop,
4622 &cmp_start, &cmp_end, it->string)
4623 && cmp_start == pos
4624 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4625 {
4626 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4627 Lisp_Object font_object = LGSTRING_FONT (gstring);
4628
4629 if (! EQ (font_object,
4630 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4631 it->w, it->string)))
4632 /* We must re-compute the composition for the
4633 different font. */
4634 val = Qnil;
4635 }
4636
4637 if (! NILP (val))
4638 {
4639 Lisp_Object end;
4640
4641 /* As Fnext_single_char_property_change is very slow, we
4642 limit the search to the current line. */
4643 if (STRINGP (it->string))
4644 limit = SCHARS (it->string);
4645 else
4646 limit = find_next_newline_no_quit (pos, 1);
4647 end = Fnext_single_char_property_change (make_number (pos),
4648 Qauto_composed,
4649 it->string,
4650 make_number (limit));
4651
4652 if (XINT (end) < limit)
4653 /* The current point is auto-composed, but there exist
4654 characters not yet composed beyond the
4655 auto-composed region. There's a possiblity that
4656 the last characters in the region may be newly
4657 composed. */
4658 val = Qnil;
4659 }
4660 }
4661 if (NILP (val) && ! STRINGP (it->string))
4662 {
4663 if (limit < 0)
4664 limit = (STRINGP (it->string) ? SCHARS (it->string)
4665 : find_next_newline_no_quit (pos, 1));
4666 if (pos < limit)
4667 {
4668 int count = SPECPDL_INDEX ();
4669 Lisp_Object args[5];
4670
4671 if (FRAME_WINDOW_P (it->f))
4672 limit = font_range (pos, limit,
4673 FACE_FROM_ID (it->f, it->face_id),
4674 it->f, it->string);
4675 args[0] = Vauto_composition_function;
4676 specbind (Qauto_composition_function, Qnil);
4677 args[1] = make_number (pos);
4678 args[2] = make_number (limit);
4679 args[3] = it->window;
4680 args[4] = it->string;
4681 safe_call (5, args);
4682 unbind_to (count, Qnil);
4683 }
4684 }
4685 }
4686
4687 return handled;
4688 }
4689
4690 /* Set up iterator IT from `composition' property at its current
4691 position. Called from handle_stop. */
4692
4693 static enum prop_handled
4694 handle_composition_prop (it)
4695 struct it *it;
4696 {
4697 Lisp_Object prop, string;
4698 EMACS_INT pos, pos_byte, start, end;
4699 enum prop_handled handled = HANDLED_NORMALLY;
4700
4701 if (STRINGP (it->string))
4702 {
4703 unsigned char *s;
4704
4705 pos = IT_STRING_CHARPOS (*it);
4706 pos_byte = IT_STRING_BYTEPOS (*it);
4707 string = it->string;
4708 s = SDATA (string) + pos_byte;
4709 it->c = STRING_CHAR (s, 0);
4710 }
4711 else
4712 {
4713 pos = IT_CHARPOS (*it);
4714 pos_byte = IT_BYTEPOS (*it);
4715 string = Qnil;
4716 it->c = FETCH_CHAR (pos_byte);
4717 }
4718
4719 /* If there's a valid composition and point is not inside of the
4720 composition (in the case that the composition is from the current
4721 buffer), draw a glyph composed from the composition components. */
4722 if (find_composition (pos, -1, &start, &end, &prop, string)
4723 && COMPOSITION_VALID_P (start, end, prop)
4724 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4725 {
4726 int id;
4727
4728 if (start != pos)
4729 {
4730 if (STRINGP (it->string))
4731 pos_byte = string_char_to_byte (it->string, start);
4732 else
4733 pos_byte = CHAR_TO_BYTE (start);
4734 }
4735 id = get_composition_id (start, pos_byte, end - start, prop, string);
4736
4737 if (id >= 0)
4738 {
4739 struct composition *cmp = composition_table[id];
4740
4741 if (cmp->glyph_len == 0)
4742 {
4743 /* No glyph. */
4744 if (STRINGP (it->string))
4745 {
4746 IT_STRING_CHARPOS (*it) = end;
4747 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4748 end);
4749 }
4750 else
4751 {
4752 IT_CHARPOS (*it) = end;
4753 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4754 }
4755 return HANDLED_RECOMPUTE_PROPS;
4756 }
4757
4758 it->stop_charpos = end;
4759 push_it (it);
4760
4761 it->method = GET_FROM_COMPOSITION;
4762 it->cmp_id = id;
4763 it->cmp_len = COMPOSITION_LENGTH (prop);
4764 /* For a terminal, draw only the first (non-TAB) character
4765 of the components. */
4766 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4767 {
4768 /* FIXME: This doesn't do anything!?! */
4769 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4770 ->key_and_value,
4771 cmp->hash_index * 2);
4772 }
4773 else
4774 {
4775 int i;
4776
4777 for (i = 0; i < cmp->glyph_len; i++)
4778 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4779 != '\t')
4780 break;
4781 }
4782 if (it->c == '\t')
4783 it->c = ' ';
4784 it->len = (STRINGP (it->string)
4785 ? string_char_to_byte (it->string, end)
4786 : CHAR_TO_BYTE (end)) - pos_byte;
4787 handled = HANDLED_RETURN;
4788 }
4789 }
4790
4791 return handled;
4792 }
4793
4794
4795 \f
4796 /***********************************************************************
4797 Overlay strings
4798 ***********************************************************************/
4799
4800 /* The following structure is used to record overlay strings for
4801 later sorting in load_overlay_strings. */
4802
4803 struct overlay_entry
4804 {
4805 Lisp_Object overlay;
4806 Lisp_Object string;
4807 int priority;
4808 int after_string_p;
4809 };
4810
4811
4812 /* Set up iterator IT from overlay strings at its current position.
4813 Called from handle_stop. */
4814
4815 static enum prop_handled
4816 handle_overlay_change (it)
4817 struct it *it;
4818 {
4819 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4820 return HANDLED_RECOMPUTE_PROPS;
4821 else
4822 return HANDLED_NORMALLY;
4823 }
4824
4825
4826 /* Set up the next overlay string for delivery by IT, if there is an
4827 overlay string to deliver. Called by set_iterator_to_next when the
4828 end of the current overlay string is reached. If there are more
4829 overlay strings to display, IT->string and
4830 IT->current.overlay_string_index are set appropriately here.
4831 Otherwise IT->string is set to nil. */
4832
4833 static void
4834 next_overlay_string (it)
4835 struct it *it;
4836 {
4837 ++it->current.overlay_string_index;
4838 if (it->current.overlay_string_index == it->n_overlay_strings)
4839 {
4840 /* No more overlay strings. Restore IT's settings to what
4841 they were before overlay strings were processed, and
4842 continue to deliver from current_buffer. */
4843 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4844
4845 pop_it (it);
4846 xassert (it->sp > 0
4847 || it->method == GET_FROM_COMPOSITION
4848 || (NILP (it->string)
4849 && it->method == GET_FROM_BUFFER
4850 && it->stop_charpos >= BEGV
4851 && it->stop_charpos <= it->end_charpos));
4852 it->current.overlay_string_index = -1;
4853 it->n_overlay_strings = 0;
4854
4855 /* If we're at the end of the buffer, record that we have
4856 processed the overlay strings there already, so that
4857 next_element_from_buffer doesn't try it again. */
4858 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4859 it->overlay_strings_at_end_processed_p = 1;
4860
4861 /* If we have to display `...' for invisible text, set
4862 the iterator up for that. */
4863 if (display_ellipsis_p)
4864 setup_for_ellipsis (it, 0);
4865 }
4866 else
4867 {
4868 /* There are more overlay strings to process. If
4869 IT->current.overlay_string_index has advanced to a position
4870 where we must load IT->overlay_strings with more strings, do
4871 it. */
4872 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4873
4874 if (it->current.overlay_string_index && i == 0)
4875 load_overlay_strings (it, 0);
4876
4877 /* Initialize IT to deliver display elements from the overlay
4878 string. */
4879 it->string = it->overlay_strings[i];
4880 it->multibyte_p = STRING_MULTIBYTE (it->string);
4881 SET_TEXT_POS (it->current.string_pos, 0, 0);
4882 it->method = GET_FROM_STRING;
4883 it->stop_charpos = 0;
4884 }
4885
4886 CHECK_IT (it);
4887 }
4888
4889
4890 /* Compare two overlay_entry structures E1 and E2. Used as a
4891 comparison function for qsort in load_overlay_strings. Overlay
4892 strings for the same position are sorted so that
4893
4894 1. All after-strings come in front of before-strings, except
4895 when they come from the same overlay.
4896
4897 2. Within after-strings, strings are sorted so that overlay strings
4898 from overlays with higher priorities come first.
4899
4900 2. Within before-strings, strings are sorted so that overlay
4901 strings from overlays with higher priorities come last.
4902
4903 Value is analogous to strcmp. */
4904
4905
4906 static int
4907 compare_overlay_entries (e1, e2)
4908 void *e1, *e2;
4909 {
4910 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4911 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4912 int result;
4913
4914 if (entry1->after_string_p != entry2->after_string_p)
4915 {
4916 /* Let after-strings appear in front of before-strings if
4917 they come from different overlays. */
4918 if (EQ (entry1->overlay, entry2->overlay))
4919 result = entry1->after_string_p ? 1 : -1;
4920 else
4921 result = entry1->after_string_p ? -1 : 1;
4922 }
4923 else if (entry1->after_string_p)
4924 /* After-strings sorted in order of decreasing priority. */
4925 result = entry2->priority - entry1->priority;
4926 else
4927 /* Before-strings sorted in order of increasing priority. */
4928 result = entry1->priority - entry2->priority;
4929
4930 return result;
4931 }
4932
4933
4934 /* Load the vector IT->overlay_strings with overlay strings from IT's
4935 current buffer position, or from CHARPOS if that is > 0. Set
4936 IT->n_overlays to the total number of overlay strings found.
4937
4938 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4939 a time. On entry into load_overlay_strings,
4940 IT->current.overlay_string_index gives the number of overlay
4941 strings that have already been loaded by previous calls to this
4942 function.
4943
4944 IT->add_overlay_start contains an additional overlay start
4945 position to consider for taking overlay strings from, if non-zero.
4946 This position comes into play when the overlay has an `invisible'
4947 property, and both before and after-strings. When we've skipped to
4948 the end of the overlay, because of its `invisible' property, we
4949 nevertheless want its before-string to appear.
4950 IT->add_overlay_start will contain the overlay start position
4951 in this case.
4952
4953 Overlay strings are sorted so that after-string strings come in
4954 front of before-string strings. Within before and after-strings,
4955 strings are sorted by overlay priority. See also function
4956 compare_overlay_entries. */
4957
4958 static void
4959 load_overlay_strings (it, charpos)
4960 struct it *it;
4961 int charpos;
4962 {
4963 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4964 Lisp_Object overlay, window, str, invisible;
4965 struct Lisp_Overlay *ov;
4966 int start, end;
4967 int size = 20;
4968 int n = 0, i, j, invis_p;
4969 struct overlay_entry *entries
4970 = (struct overlay_entry *) alloca (size * sizeof *entries);
4971
4972 if (charpos <= 0)
4973 charpos = IT_CHARPOS (*it);
4974
4975 /* Append the overlay string STRING of overlay OVERLAY to vector
4976 `entries' which has size `size' and currently contains `n'
4977 elements. AFTER_P non-zero means STRING is an after-string of
4978 OVERLAY. */
4979 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4980 do \
4981 { \
4982 Lisp_Object priority; \
4983 \
4984 if (n == size) \
4985 { \
4986 int new_size = 2 * size; \
4987 struct overlay_entry *old = entries; \
4988 entries = \
4989 (struct overlay_entry *) alloca (new_size \
4990 * sizeof *entries); \
4991 bcopy (old, entries, size * sizeof *entries); \
4992 size = new_size; \
4993 } \
4994 \
4995 entries[n].string = (STRING); \
4996 entries[n].overlay = (OVERLAY); \
4997 priority = Foverlay_get ((OVERLAY), Qpriority); \
4998 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4999 entries[n].after_string_p = (AFTER_P); \
5000 ++n; \
5001 } \
5002 while (0)
5003
5004 /* Process overlay before the overlay center. */
5005 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5006 {
5007 XSETMISC (overlay, ov);
5008 xassert (OVERLAYP (overlay));
5009 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5010 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5011
5012 if (end < charpos)
5013 break;
5014
5015 /* Skip this overlay if it doesn't start or end at IT's current
5016 position. */
5017 if (end != charpos && start != charpos)
5018 continue;
5019
5020 /* Skip this overlay if it doesn't apply to IT->w. */
5021 window = Foverlay_get (overlay, Qwindow);
5022 if (WINDOWP (window) && XWINDOW (window) != it->w)
5023 continue;
5024
5025 /* If the text ``under'' the overlay is invisible, both before-
5026 and after-strings from this overlay are visible; start and
5027 end position are indistinguishable. */
5028 invisible = Foverlay_get (overlay, Qinvisible);
5029 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5030
5031 /* If overlay has a non-empty before-string, record it. */
5032 if ((start == charpos || (end == charpos && invis_p))
5033 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5034 && SCHARS (str))
5035 RECORD_OVERLAY_STRING (overlay, str, 0);
5036
5037 /* If overlay has a non-empty after-string, record it. */
5038 if ((end == charpos || (start == charpos && invis_p))
5039 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5040 && SCHARS (str))
5041 RECORD_OVERLAY_STRING (overlay, str, 1);
5042 }
5043
5044 /* Process overlays after the overlay center. */
5045 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5046 {
5047 XSETMISC (overlay, ov);
5048 xassert (OVERLAYP (overlay));
5049 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5050 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5051
5052 if (start > charpos)
5053 break;
5054
5055 /* Skip this overlay if it doesn't start or end at IT's current
5056 position. */
5057 if (end != charpos && start != charpos)
5058 continue;
5059
5060 /* Skip this overlay if it doesn't apply to IT->w. */
5061 window = Foverlay_get (overlay, Qwindow);
5062 if (WINDOWP (window) && XWINDOW (window) != it->w)
5063 continue;
5064
5065 /* If the text ``under'' the overlay is invisible, it has a zero
5066 dimension, and both before- and after-strings apply. */
5067 invisible = Foverlay_get (overlay, Qinvisible);
5068 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5069
5070 /* If overlay has a non-empty before-string, record it. */
5071 if ((start == charpos || (end == charpos && invis_p))
5072 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5073 && SCHARS (str))
5074 RECORD_OVERLAY_STRING (overlay, str, 0);
5075
5076 /* If overlay has a non-empty after-string, record it. */
5077 if ((end == charpos || (start == charpos && invis_p))
5078 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5079 && SCHARS (str))
5080 RECORD_OVERLAY_STRING (overlay, str, 1);
5081 }
5082
5083 #undef RECORD_OVERLAY_STRING
5084
5085 /* Sort entries. */
5086 if (n > 1)
5087 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5088
5089 /* Record the total number of strings to process. */
5090 it->n_overlay_strings = n;
5091
5092 /* IT->current.overlay_string_index is the number of overlay strings
5093 that have already been consumed by IT. Copy some of the
5094 remaining overlay strings to IT->overlay_strings. */
5095 i = 0;
5096 j = it->current.overlay_string_index;
5097 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5098 {
5099 it->overlay_strings[i] = entries[j].string;
5100 it->string_overlays[i++] = entries[j++].overlay;
5101 }
5102
5103 CHECK_IT (it);
5104 }
5105
5106
5107 /* Get the first chunk of overlay strings at IT's current buffer
5108 position, or at CHARPOS if that is > 0. Value is non-zero if at
5109 least one overlay string was found. */
5110
5111 static int
5112 get_overlay_strings_1 (it, charpos, compute_stop_p)
5113 struct it *it;
5114 int charpos;
5115 int compute_stop_p;
5116 {
5117 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5118 process. This fills IT->overlay_strings with strings, and sets
5119 IT->n_overlay_strings to the total number of strings to process.
5120 IT->pos.overlay_string_index has to be set temporarily to zero
5121 because load_overlay_strings needs this; it must be set to -1
5122 when no overlay strings are found because a zero value would
5123 indicate a position in the first overlay string. */
5124 it->current.overlay_string_index = 0;
5125 load_overlay_strings (it, charpos);
5126
5127 /* If we found overlay strings, set up IT to deliver display
5128 elements from the first one. Otherwise set up IT to deliver
5129 from current_buffer. */
5130 if (it->n_overlay_strings)
5131 {
5132 /* Make sure we know settings in current_buffer, so that we can
5133 restore meaningful values when we're done with the overlay
5134 strings. */
5135 if (compute_stop_p)
5136 compute_stop_pos (it);
5137 xassert (it->face_id >= 0);
5138
5139 /* Save IT's settings. They are restored after all overlay
5140 strings have been processed. */
5141 xassert (!compute_stop_p || it->sp == 0);
5142 push_it (it);
5143
5144 /* Set up IT to deliver display elements from the first overlay
5145 string. */
5146 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5147 it->string = it->overlay_strings[0];
5148 it->from_overlay = Qnil;
5149 it->stop_charpos = 0;
5150 xassert (STRINGP (it->string));
5151 it->end_charpos = SCHARS (it->string);
5152 it->multibyte_p = STRING_MULTIBYTE (it->string);
5153 it->method = GET_FROM_STRING;
5154 return 1;
5155 }
5156
5157 it->current.overlay_string_index = -1;
5158 return 0;
5159 }
5160
5161 static int
5162 get_overlay_strings (it, charpos)
5163 struct it *it;
5164 int charpos;
5165 {
5166 it->string = Qnil;
5167 it->method = GET_FROM_BUFFER;
5168
5169 (void) get_overlay_strings_1 (it, charpos, 1);
5170
5171 CHECK_IT (it);
5172
5173 /* Value is non-zero if we found at least one overlay string. */
5174 return STRINGP (it->string);
5175 }
5176
5177
5178 \f
5179 /***********************************************************************
5180 Saving and restoring state
5181 ***********************************************************************/
5182
5183 /* Save current settings of IT on IT->stack. Called, for example,
5184 before setting up IT for an overlay string, to be able to restore
5185 IT's settings to what they were after the overlay string has been
5186 processed. */
5187
5188 static void
5189 push_it (it)
5190 struct it *it;
5191 {
5192 struct iterator_stack_entry *p;
5193
5194 xassert (it->sp < IT_STACK_SIZE);
5195 p = it->stack + it->sp;
5196
5197 p->stop_charpos = it->stop_charpos;
5198 xassert (it->face_id >= 0);
5199 p->face_id = it->face_id;
5200 p->string = it->string;
5201 p->method = it->method;
5202 p->from_overlay = it->from_overlay;
5203 switch (p->method)
5204 {
5205 case GET_FROM_IMAGE:
5206 p->u.image.object = it->object;
5207 p->u.image.image_id = it->image_id;
5208 p->u.image.slice = it->slice;
5209 break;
5210 case GET_FROM_COMPOSITION:
5211 p->u.comp.object = it->object;
5212 p->u.comp.c = it->c;
5213 p->u.comp.len = it->len;
5214 p->u.comp.cmp_id = it->cmp_id;
5215 p->u.comp.cmp_len = it->cmp_len;
5216 break;
5217 case GET_FROM_STRETCH:
5218 p->u.stretch.object = it->object;
5219 break;
5220 }
5221 p->position = it->position;
5222 p->current = it->current;
5223 p->end_charpos = it->end_charpos;
5224 p->string_nchars = it->string_nchars;
5225 p->area = it->area;
5226 p->multibyte_p = it->multibyte_p;
5227 p->avoid_cursor_p = it->avoid_cursor_p;
5228 p->space_width = it->space_width;
5229 p->font_height = it->font_height;
5230 p->voffset = it->voffset;
5231 p->string_from_display_prop_p = it->string_from_display_prop_p;
5232 p->display_ellipsis_p = 0;
5233 ++it->sp;
5234 }
5235
5236
5237 /* Restore IT's settings from IT->stack. Called, for example, when no
5238 more overlay strings must be processed, and we return to delivering
5239 display elements from a buffer, or when the end of a string from a
5240 `display' property is reached and we return to delivering display
5241 elements from an overlay string, or from a buffer. */
5242
5243 static void
5244 pop_it (it)
5245 struct it *it;
5246 {
5247 struct iterator_stack_entry *p;
5248
5249 xassert (it->sp > 0);
5250 --it->sp;
5251 p = it->stack + it->sp;
5252 it->stop_charpos = p->stop_charpos;
5253 it->face_id = p->face_id;
5254 it->current = p->current;
5255 it->position = p->position;
5256 it->string = p->string;
5257 it->from_overlay = p->from_overlay;
5258 if (NILP (it->string))
5259 SET_TEXT_POS (it->current.string_pos, -1, -1);
5260 it->method = p->method;
5261 switch (it->method)
5262 {
5263 case GET_FROM_IMAGE:
5264 it->image_id = p->u.image.image_id;
5265 it->object = p->u.image.object;
5266 it->slice = p->u.image.slice;
5267 break;
5268 case GET_FROM_COMPOSITION:
5269 it->object = p->u.comp.object;
5270 it->c = p->u.comp.c;
5271 it->len = p->u.comp.len;
5272 it->cmp_id = p->u.comp.cmp_id;
5273 it->cmp_len = p->u.comp.cmp_len;
5274 break;
5275 case GET_FROM_STRETCH:
5276 it->object = p->u.comp.object;
5277 break;
5278 case GET_FROM_BUFFER:
5279 it->object = it->w->buffer;
5280 break;
5281 case GET_FROM_STRING:
5282 it->object = it->string;
5283 break;
5284 }
5285 it->end_charpos = p->end_charpos;
5286 it->string_nchars = p->string_nchars;
5287 it->area = p->area;
5288 it->multibyte_p = p->multibyte_p;
5289 it->avoid_cursor_p = p->avoid_cursor_p;
5290 it->space_width = p->space_width;
5291 it->font_height = p->font_height;
5292 it->voffset = p->voffset;
5293 it->string_from_display_prop_p = p->string_from_display_prop_p;
5294 }
5295
5296
5297 \f
5298 /***********************************************************************
5299 Moving over lines
5300 ***********************************************************************/
5301
5302 /* Set IT's current position to the previous line start. */
5303
5304 static void
5305 back_to_previous_line_start (it)
5306 struct it *it;
5307 {
5308 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5309 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5310 }
5311
5312
5313 /* Move IT to the next line start.
5314
5315 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5316 we skipped over part of the text (as opposed to moving the iterator
5317 continuously over the text). Otherwise, don't change the value
5318 of *SKIPPED_P.
5319
5320 Newlines may come from buffer text, overlay strings, or strings
5321 displayed via the `display' property. That's the reason we can't
5322 simply use find_next_newline_no_quit.
5323
5324 Note that this function may not skip over invisible text that is so
5325 because of text properties and immediately follows a newline. If
5326 it would, function reseat_at_next_visible_line_start, when called
5327 from set_iterator_to_next, would effectively make invisible
5328 characters following a newline part of the wrong glyph row, which
5329 leads to wrong cursor motion. */
5330
5331 static int
5332 forward_to_next_line_start (it, skipped_p)
5333 struct it *it;
5334 int *skipped_p;
5335 {
5336 int old_selective, newline_found_p, n;
5337 const int MAX_NEWLINE_DISTANCE = 500;
5338
5339 /* If already on a newline, just consume it to avoid unintended
5340 skipping over invisible text below. */
5341 if (it->what == IT_CHARACTER
5342 && it->c == '\n'
5343 && CHARPOS (it->position) == IT_CHARPOS (*it))
5344 {
5345 set_iterator_to_next (it, 0);
5346 it->c = 0;
5347 return 1;
5348 }
5349
5350 /* Don't handle selective display in the following. It's (a)
5351 unnecessary because it's done by the caller, and (b) leads to an
5352 infinite recursion because next_element_from_ellipsis indirectly
5353 calls this function. */
5354 old_selective = it->selective;
5355 it->selective = 0;
5356
5357 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5358 from buffer text. */
5359 for (n = newline_found_p = 0;
5360 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5361 n += STRINGP (it->string) ? 0 : 1)
5362 {
5363 if (!get_next_display_element (it))
5364 return 0;
5365 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5366 set_iterator_to_next (it, 0);
5367 }
5368
5369 /* If we didn't find a newline near enough, see if we can use a
5370 short-cut. */
5371 if (!newline_found_p)
5372 {
5373 int start = IT_CHARPOS (*it);
5374 int limit = find_next_newline_no_quit (start, 1);
5375 Lisp_Object pos;
5376
5377 xassert (!STRINGP (it->string));
5378
5379 /* If there isn't any `display' property in sight, and no
5380 overlays, we can just use the position of the newline in
5381 buffer text. */
5382 if (it->stop_charpos >= limit
5383 || ((pos = Fnext_single_property_change (make_number (start),
5384 Qdisplay,
5385 Qnil, make_number (limit)),
5386 NILP (pos))
5387 && next_overlay_change (start) == ZV))
5388 {
5389 IT_CHARPOS (*it) = limit;
5390 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5391 *skipped_p = newline_found_p = 1;
5392 }
5393 else
5394 {
5395 while (get_next_display_element (it)
5396 && !newline_found_p)
5397 {
5398 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5399 set_iterator_to_next (it, 0);
5400 }
5401 }
5402 }
5403
5404 it->selective = old_selective;
5405 return newline_found_p;
5406 }
5407
5408
5409 /* Set IT's current position to the previous visible line start. Skip
5410 invisible text that is so either due to text properties or due to
5411 selective display. Caution: this does not change IT->current_x and
5412 IT->hpos. */
5413
5414 static void
5415 back_to_previous_visible_line_start (it)
5416 struct it *it;
5417 {
5418 while (IT_CHARPOS (*it) > BEGV)
5419 {
5420 back_to_previous_line_start (it);
5421
5422 if (IT_CHARPOS (*it) <= BEGV)
5423 break;
5424
5425 /* If selective > 0, then lines indented more than that values
5426 are invisible. */
5427 if (it->selective > 0
5428 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5429 (double) it->selective)) /* iftc */
5430 continue;
5431
5432 /* Check the newline before point for invisibility. */
5433 {
5434 Lisp_Object prop;
5435 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5436 Qinvisible, it->window);
5437 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5438 continue;
5439 }
5440
5441 if (IT_CHARPOS (*it) <= BEGV)
5442 break;
5443
5444 {
5445 struct it it2;
5446 int pos;
5447 EMACS_INT beg, end;
5448 Lisp_Object val, overlay;
5449
5450 /* If newline is part of a composition, continue from start of composition */
5451 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5452 && beg < IT_CHARPOS (*it))
5453 goto replaced;
5454
5455 /* If newline is replaced by a display property, find start of overlay
5456 or interval and continue search from that point. */
5457 it2 = *it;
5458 pos = --IT_CHARPOS (it2);
5459 --IT_BYTEPOS (it2);
5460 it2.sp = 0;
5461 it2.string_from_display_prop_p = 0;
5462 if (handle_display_prop (&it2) == HANDLED_RETURN
5463 && !NILP (val = get_char_property_and_overlay
5464 (make_number (pos), Qdisplay, Qnil, &overlay))
5465 && (OVERLAYP (overlay)
5466 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5467 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5468 goto replaced;
5469
5470 /* Newline is not replaced by anything -- so we are done. */
5471 break;
5472
5473 replaced:
5474 if (beg < BEGV)
5475 beg = BEGV;
5476 IT_CHARPOS (*it) = beg;
5477 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5478 }
5479 }
5480
5481 it->continuation_lines_width = 0;
5482
5483 xassert (IT_CHARPOS (*it) >= BEGV);
5484 xassert (IT_CHARPOS (*it) == BEGV
5485 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5486 CHECK_IT (it);
5487 }
5488
5489
5490 /* Reseat iterator IT at the previous visible line start. Skip
5491 invisible text that is so either due to text properties or due to
5492 selective display. At the end, update IT's overlay information,
5493 face information etc. */
5494
5495 void
5496 reseat_at_previous_visible_line_start (it)
5497 struct it *it;
5498 {
5499 back_to_previous_visible_line_start (it);
5500 reseat (it, it->current.pos, 1);
5501 CHECK_IT (it);
5502 }
5503
5504
5505 /* Reseat iterator IT on the next visible line start in the current
5506 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5507 preceding the line start. Skip over invisible text that is so
5508 because of selective display. Compute faces, overlays etc at the
5509 new position. Note that this function does not skip over text that
5510 is invisible because of text properties. */
5511
5512 static void
5513 reseat_at_next_visible_line_start (it, on_newline_p)
5514 struct it *it;
5515 int on_newline_p;
5516 {
5517 int newline_found_p, skipped_p = 0;
5518
5519 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5520
5521 /* Skip over lines that are invisible because they are indented
5522 more than the value of IT->selective. */
5523 if (it->selective > 0)
5524 while (IT_CHARPOS (*it) < ZV
5525 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5526 (double) it->selective)) /* iftc */
5527 {
5528 xassert (IT_BYTEPOS (*it) == BEGV
5529 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5530 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5531 }
5532
5533 /* Position on the newline if that's what's requested. */
5534 if (on_newline_p && newline_found_p)
5535 {
5536 if (STRINGP (it->string))
5537 {
5538 if (IT_STRING_CHARPOS (*it) > 0)
5539 {
5540 --IT_STRING_CHARPOS (*it);
5541 --IT_STRING_BYTEPOS (*it);
5542 }
5543 }
5544 else if (IT_CHARPOS (*it) > BEGV)
5545 {
5546 --IT_CHARPOS (*it);
5547 --IT_BYTEPOS (*it);
5548 reseat (it, it->current.pos, 0);
5549 }
5550 }
5551 else if (skipped_p)
5552 reseat (it, it->current.pos, 0);
5553
5554 CHECK_IT (it);
5555 }
5556
5557
5558 \f
5559 /***********************************************************************
5560 Changing an iterator's position
5561 ***********************************************************************/
5562
5563 /* Change IT's current position to POS in current_buffer. If FORCE_P
5564 is non-zero, always check for text properties at the new position.
5565 Otherwise, text properties are only looked up if POS >=
5566 IT->check_charpos of a property. */
5567
5568 static void
5569 reseat (it, pos, force_p)
5570 struct it *it;
5571 struct text_pos pos;
5572 int force_p;
5573 {
5574 int original_pos = IT_CHARPOS (*it);
5575
5576 reseat_1 (it, pos, 0);
5577
5578 /* Determine where to check text properties. Avoid doing it
5579 where possible because text property lookup is very expensive. */
5580 if (force_p
5581 || CHARPOS (pos) > it->stop_charpos
5582 || CHARPOS (pos) < original_pos)
5583 handle_stop (it);
5584
5585 CHECK_IT (it);
5586 }
5587
5588
5589 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5590 IT->stop_pos to POS, also. */
5591
5592 static void
5593 reseat_1 (it, pos, set_stop_p)
5594 struct it *it;
5595 struct text_pos pos;
5596 int set_stop_p;
5597 {
5598 /* Don't call this function when scanning a C string. */
5599 xassert (it->s == NULL);
5600
5601 /* POS must be a reasonable value. */
5602 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5603
5604 it->current.pos = it->position = pos;
5605 it->end_charpos = ZV;
5606 it->dpvec = NULL;
5607 it->current.dpvec_index = -1;
5608 it->current.overlay_string_index = -1;
5609 IT_STRING_CHARPOS (*it) = -1;
5610 IT_STRING_BYTEPOS (*it) = -1;
5611 it->string = Qnil;
5612 it->string_from_display_prop_p = 0;
5613 it->method = GET_FROM_BUFFER;
5614 it->object = it->w->buffer;
5615 it->area = TEXT_AREA;
5616 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5617 it->sp = 0;
5618 it->string_from_display_prop_p = 0;
5619 it->face_before_selective_p = 0;
5620
5621 if (set_stop_p)
5622 it->stop_charpos = CHARPOS (pos);
5623 }
5624
5625
5626 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5627 If S is non-null, it is a C string to iterate over. Otherwise,
5628 STRING gives a Lisp string to iterate over.
5629
5630 If PRECISION > 0, don't return more then PRECISION number of
5631 characters from the string.
5632
5633 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5634 characters have been returned. FIELD_WIDTH < 0 means an infinite
5635 field width.
5636
5637 MULTIBYTE = 0 means disable processing of multibyte characters,
5638 MULTIBYTE > 0 means enable it,
5639 MULTIBYTE < 0 means use IT->multibyte_p.
5640
5641 IT must be initialized via a prior call to init_iterator before
5642 calling this function. */
5643
5644 static void
5645 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5646 struct it *it;
5647 unsigned char *s;
5648 Lisp_Object string;
5649 int charpos;
5650 int precision, field_width, multibyte;
5651 {
5652 /* No region in strings. */
5653 it->region_beg_charpos = it->region_end_charpos = -1;
5654
5655 /* No text property checks performed by default, but see below. */
5656 it->stop_charpos = -1;
5657
5658 /* Set iterator position and end position. */
5659 bzero (&it->current, sizeof it->current);
5660 it->current.overlay_string_index = -1;
5661 it->current.dpvec_index = -1;
5662 xassert (charpos >= 0);
5663
5664 /* If STRING is specified, use its multibyteness, otherwise use the
5665 setting of MULTIBYTE, if specified. */
5666 if (multibyte >= 0)
5667 it->multibyte_p = multibyte > 0;
5668
5669 if (s == NULL)
5670 {
5671 xassert (STRINGP (string));
5672 it->string = string;
5673 it->s = NULL;
5674 it->end_charpos = it->string_nchars = SCHARS (string);
5675 it->method = GET_FROM_STRING;
5676 it->current.string_pos = string_pos (charpos, string);
5677 }
5678 else
5679 {
5680 it->s = s;
5681 it->string = Qnil;
5682
5683 /* Note that we use IT->current.pos, not it->current.string_pos,
5684 for displaying C strings. */
5685 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5686 if (it->multibyte_p)
5687 {
5688 it->current.pos = c_string_pos (charpos, s, 1);
5689 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5690 }
5691 else
5692 {
5693 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5694 it->end_charpos = it->string_nchars = strlen (s);
5695 }
5696
5697 it->method = GET_FROM_C_STRING;
5698 }
5699
5700 /* PRECISION > 0 means don't return more than PRECISION characters
5701 from the string. */
5702 if (precision > 0 && it->end_charpos - charpos > precision)
5703 it->end_charpos = it->string_nchars = charpos + precision;
5704
5705 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5706 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5707 FIELD_WIDTH < 0 means infinite field width. This is useful for
5708 padding with `-' at the end of a mode line. */
5709 if (field_width < 0)
5710 field_width = INFINITY;
5711 if (field_width > it->end_charpos - charpos)
5712 it->end_charpos = charpos + field_width;
5713
5714 /* Use the standard display table for displaying strings. */
5715 if (DISP_TABLE_P (Vstandard_display_table))
5716 it->dp = XCHAR_TABLE (Vstandard_display_table);
5717
5718 it->stop_charpos = charpos;
5719 CHECK_IT (it);
5720 }
5721
5722
5723 \f
5724 /***********************************************************************
5725 Iteration
5726 ***********************************************************************/
5727
5728 /* Map enum it_method value to corresponding next_element_from_* function. */
5729
5730 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5731 {
5732 next_element_from_buffer,
5733 next_element_from_display_vector,
5734 next_element_from_composition,
5735 next_element_from_string,
5736 next_element_from_c_string,
5737 next_element_from_image,
5738 next_element_from_stretch
5739 };
5740
5741 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5742
5743 /* Load IT's display element fields with information about the next
5744 display element from the current position of IT. Value is zero if
5745 end of buffer (or C string) is reached. */
5746
5747 static struct frame *last_escape_glyph_frame = NULL;
5748 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5749 static int last_escape_glyph_merged_face_id = 0;
5750
5751 int
5752 get_next_display_element (it)
5753 struct it *it;
5754 {
5755 /* Non-zero means that we found a display element. Zero means that
5756 we hit the end of what we iterate over. Performance note: the
5757 function pointer `method' used here turns out to be faster than
5758 using a sequence of if-statements. */
5759 int success_p;
5760
5761 get_next:
5762 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5763
5764 if (it->what == IT_CHARACTER)
5765 {
5766 /* Map via display table or translate control characters.
5767 IT->c, IT->len etc. have been set to the next character by
5768 the function call above. If we have a display table, and it
5769 contains an entry for IT->c, translate it. Don't do this if
5770 IT->c itself comes from a display table, otherwise we could
5771 end up in an infinite recursion. (An alternative could be to
5772 count the recursion depth of this function and signal an
5773 error when a certain maximum depth is reached.) Is it worth
5774 it? */
5775 if (success_p && it->dpvec == NULL)
5776 {
5777 Lisp_Object dv;
5778
5779 if (it->dp
5780 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5781 VECTORP (dv)))
5782 {
5783 struct Lisp_Vector *v = XVECTOR (dv);
5784
5785 /* Return the first character from the display table
5786 entry, if not empty. If empty, don't display the
5787 current character. */
5788 if (v->size)
5789 {
5790 it->dpvec_char_len = it->len;
5791 it->dpvec = v->contents;
5792 it->dpend = v->contents + v->size;
5793 it->current.dpvec_index = 0;
5794 it->dpvec_face_id = -1;
5795 it->saved_face_id = it->face_id;
5796 it->method = GET_FROM_DISPLAY_VECTOR;
5797 it->ellipsis_p = 0;
5798 }
5799 else
5800 {
5801 set_iterator_to_next (it, 0);
5802 }
5803 goto get_next;
5804 }
5805
5806 /* Translate control characters into `\003' or `^C' form.
5807 Control characters coming from a display table entry are
5808 currently not translated because we use IT->dpvec to hold
5809 the translation. This could easily be changed but I
5810 don't believe that it is worth doing.
5811
5812 If it->multibyte_p is nonzero, non-printable non-ASCII
5813 characters are also translated to octal form.
5814
5815 If it->multibyte_p is zero, eight-bit characters that
5816 don't have corresponding multibyte char code are also
5817 translated to octal form. */
5818 else if ((it->c < ' '
5819 ? (it->area != TEXT_AREA
5820 /* In mode line, treat \n, \t like other crl chars. */
5821 || (it->c != '\t'
5822 && it->glyph_row && it->glyph_row->mode_line_p)
5823 || (it->c != '\n' && it->c != '\t'))
5824 : (it->multibyte_p
5825 ? (!CHAR_PRINTABLE_P (it->c)
5826 || (!NILP (Vnobreak_char_display)
5827 && (it->c == 0xA0 /* NO-BREAK SPACE */
5828 || it->c == 0xAD /* SOFT HYPHEN */)))
5829 : (it->c >= 127
5830 && (! unibyte_display_via_language_environment
5831 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5832 {
5833 /* IT->c is a control character which must be displayed
5834 either as '\003' or as `^C' where the '\\' and '^'
5835 can be defined in the display table. Fill
5836 IT->ctl_chars with glyphs for what we have to
5837 display. Then, set IT->dpvec to these glyphs. */
5838 Lisp_Object gc;
5839 int ctl_len;
5840 int face_id, lface_id = 0 ;
5841 int escape_glyph;
5842
5843 /* Handle control characters with ^. */
5844
5845 if (it->c < 128 && it->ctl_arrow_p)
5846 {
5847 int g;
5848
5849 g = '^'; /* default glyph for Control */
5850 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5851 if (it->dp
5852 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5853 && GLYPH_CODE_CHAR_VALID_P (gc))
5854 {
5855 g = GLYPH_CODE_CHAR (gc);
5856 lface_id = GLYPH_CODE_FACE (gc);
5857 }
5858 if (lface_id)
5859 {
5860 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5861 }
5862 else if (it->f == last_escape_glyph_frame
5863 && it->face_id == last_escape_glyph_face_id)
5864 {
5865 face_id = last_escape_glyph_merged_face_id;
5866 }
5867 else
5868 {
5869 /* Merge the escape-glyph face into the current face. */
5870 face_id = merge_faces (it->f, Qescape_glyph, 0,
5871 it->face_id);
5872 last_escape_glyph_frame = it->f;
5873 last_escape_glyph_face_id = it->face_id;
5874 last_escape_glyph_merged_face_id = face_id;
5875 }
5876
5877 XSETINT (it->ctl_chars[0], g);
5878 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5879 ctl_len = 2;
5880 goto display_control;
5881 }
5882
5883 /* Handle non-break space in the mode where it only gets
5884 highlighting. */
5885
5886 if (EQ (Vnobreak_char_display, Qt)
5887 && it->c == 0xA0)
5888 {
5889 /* Merge the no-break-space face into the current face. */
5890 face_id = merge_faces (it->f, Qnobreak_space, 0,
5891 it->face_id);
5892
5893 it->c = ' ';
5894 XSETINT (it->ctl_chars[0], ' ');
5895 ctl_len = 1;
5896 goto display_control;
5897 }
5898
5899 /* Handle sequences that start with the "escape glyph". */
5900
5901 /* the default escape glyph is \. */
5902 escape_glyph = '\\';
5903
5904 if (it->dp
5905 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5906 && GLYPH_CODE_CHAR_VALID_P (gc))
5907 {
5908 escape_glyph = GLYPH_CODE_CHAR (gc);
5909 lface_id = GLYPH_CODE_FACE (gc);
5910 }
5911 if (lface_id)
5912 {
5913 /* The display table specified a face.
5914 Merge it into face_id and also into escape_glyph. */
5915 face_id = merge_faces (it->f, Qt, lface_id,
5916 it->face_id);
5917 }
5918 else if (it->f == last_escape_glyph_frame
5919 && it->face_id == last_escape_glyph_face_id)
5920 {
5921 face_id = last_escape_glyph_merged_face_id;
5922 }
5923 else
5924 {
5925 /* Merge the escape-glyph face into the current face. */
5926 face_id = merge_faces (it->f, Qescape_glyph, 0,
5927 it->face_id);
5928 last_escape_glyph_frame = it->f;
5929 last_escape_glyph_face_id = it->face_id;
5930 last_escape_glyph_merged_face_id = face_id;
5931 }
5932
5933 /* Handle soft hyphens in the mode where they only get
5934 highlighting. */
5935
5936 if (EQ (Vnobreak_char_display, Qt)
5937 && it->c == 0xAD)
5938 {
5939 it->c = '-';
5940 XSETINT (it->ctl_chars[0], '-');
5941 ctl_len = 1;
5942 goto display_control;
5943 }
5944
5945 /* Handle non-break space and soft hyphen
5946 with the escape glyph. */
5947
5948 if (it->c == 0xA0 || it->c == 0xAD)
5949 {
5950 XSETINT (it->ctl_chars[0], escape_glyph);
5951 it->c = (it->c == 0xA0 ? ' ' : '-');
5952 XSETINT (it->ctl_chars[1], it->c);
5953 ctl_len = 2;
5954 goto display_control;
5955 }
5956
5957 {
5958 unsigned char str[MAX_MULTIBYTE_LENGTH];
5959 int len;
5960 int i;
5961
5962 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5963 if (CHAR_BYTE8_P (it->c))
5964 {
5965 str[0] = CHAR_TO_BYTE8 (it->c);
5966 len = 1;
5967 }
5968 else if (it->c < 256)
5969 {
5970 str[0] = it->c;
5971 len = 1;
5972 }
5973 else
5974 {
5975 /* It's an invalid character, which shouldn't
5976 happen actually, but due to bugs it may
5977 happen. Let's print the char as is, there's
5978 not much meaningful we can do with it. */
5979 str[0] = it->c;
5980 str[1] = it->c >> 8;
5981 str[2] = it->c >> 16;
5982 str[3] = it->c >> 24;
5983 len = 4;
5984 }
5985
5986 for (i = 0; i < len; i++)
5987 {
5988 int g;
5989 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5990 /* Insert three more glyphs into IT->ctl_chars for
5991 the octal display of the character. */
5992 g = ((str[i] >> 6) & 7) + '0';
5993 XSETINT (it->ctl_chars[i * 4 + 1], g);
5994 g = ((str[i] >> 3) & 7) + '0';
5995 XSETINT (it->ctl_chars[i * 4 + 2], g);
5996 g = (str[i] & 7) + '0';
5997 XSETINT (it->ctl_chars[i * 4 + 3], g);
5998 }
5999 ctl_len = len * 4;
6000 }
6001
6002 display_control:
6003 /* Set up IT->dpvec and return first character from it. */
6004 it->dpvec_char_len = it->len;
6005 it->dpvec = it->ctl_chars;
6006 it->dpend = it->dpvec + ctl_len;
6007 it->current.dpvec_index = 0;
6008 it->dpvec_face_id = face_id;
6009 it->saved_face_id = it->face_id;
6010 it->method = GET_FROM_DISPLAY_VECTOR;
6011 it->ellipsis_p = 0;
6012 goto get_next;
6013 }
6014 }
6015 }
6016
6017 /* Adjust face id for a multibyte character. There are no multibyte
6018 character in unibyte text. */
6019 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6020 && it->multibyte_p
6021 && success_p
6022 && FRAME_WINDOW_P (it->f))
6023 {
6024 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6025 int pos = (it->s ? -1
6026 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6027 : IT_CHARPOS (*it));
6028
6029 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6030 }
6031
6032 /* Is this character the last one of a run of characters with
6033 box? If yes, set IT->end_of_box_run_p to 1. */
6034 if (it->face_box_p
6035 && it->s == NULL)
6036 {
6037 int face_id;
6038 struct face *face;
6039
6040 it->end_of_box_run_p
6041 = ((face_id = face_after_it_pos (it),
6042 face_id != it->face_id)
6043 && (face = FACE_FROM_ID (it->f, face_id),
6044 face->box == FACE_NO_BOX));
6045 }
6046
6047 /* Value is 0 if end of buffer or string reached. */
6048 return success_p;
6049 }
6050
6051
6052 /* Move IT to the next display element.
6053
6054 RESEAT_P non-zero means if called on a newline in buffer text,
6055 skip to the next visible line start.
6056
6057 Functions get_next_display_element and set_iterator_to_next are
6058 separate because I find this arrangement easier to handle than a
6059 get_next_display_element function that also increments IT's
6060 position. The way it is we can first look at an iterator's current
6061 display element, decide whether it fits on a line, and if it does,
6062 increment the iterator position. The other way around we probably
6063 would either need a flag indicating whether the iterator has to be
6064 incremented the next time, or we would have to implement a
6065 decrement position function which would not be easy to write. */
6066
6067 void
6068 set_iterator_to_next (it, reseat_p)
6069 struct it *it;
6070 int reseat_p;
6071 {
6072 /* Reset flags indicating start and end of a sequence of characters
6073 with box. Reset them at the start of this function because
6074 moving the iterator to a new position might set them. */
6075 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6076
6077 switch (it->method)
6078 {
6079 case GET_FROM_BUFFER:
6080 /* The current display element of IT is a character from
6081 current_buffer. Advance in the buffer, and maybe skip over
6082 invisible lines that are so because of selective display. */
6083 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6084 reseat_at_next_visible_line_start (it, 0);
6085 else
6086 {
6087 xassert (it->len != 0);
6088 IT_BYTEPOS (*it) += it->len;
6089 IT_CHARPOS (*it) += 1;
6090 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6091 }
6092 break;
6093
6094 case GET_FROM_COMPOSITION:
6095 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6096 xassert (it->sp > 0);
6097 pop_it (it);
6098 if (it->method == GET_FROM_STRING)
6099 {
6100 IT_STRING_BYTEPOS (*it) += it->len;
6101 IT_STRING_CHARPOS (*it) += it->cmp_len;
6102 goto consider_string_end;
6103 }
6104 else if (it->method == GET_FROM_BUFFER)
6105 {
6106 IT_BYTEPOS (*it) += it->len;
6107 IT_CHARPOS (*it) += it->cmp_len;
6108 }
6109 break;
6110
6111 case GET_FROM_C_STRING:
6112 /* Current display element of IT is from a C string. */
6113 IT_BYTEPOS (*it) += it->len;
6114 IT_CHARPOS (*it) += 1;
6115 break;
6116
6117 case GET_FROM_DISPLAY_VECTOR:
6118 /* Current display element of IT is from a display table entry.
6119 Advance in the display table definition. Reset it to null if
6120 end reached, and continue with characters from buffers/
6121 strings. */
6122 ++it->current.dpvec_index;
6123
6124 /* Restore face of the iterator to what they were before the
6125 display vector entry (these entries may contain faces). */
6126 it->face_id = it->saved_face_id;
6127
6128 if (it->dpvec + it->current.dpvec_index == it->dpend)
6129 {
6130 int recheck_faces = it->ellipsis_p;
6131
6132 if (it->s)
6133 it->method = GET_FROM_C_STRING;
6134 else if (STRINGP (it->string))
6135 it->method = GET_FROM_STRING;
6136 else
6137 {
6138 it->method = GET_FROM_BUFFER;
6139 it->object = it->w->buffer;
6140 }
6141
6142 it->dpvec = NULL;
6143 it->current.dpvec_index = -1;
6144
6145 /* Skip over characters which were displayed via IT->dpvec. */
6146 if (it->dpvec_char_len < 0)
6147 reseat_at_next_visible_line_start (it, 1);
6148 else if (it->dpvec_char_len > 0)
6149 {
6150 if (it->method == GET_FROM_STRING
6151 && it->n_overlay_strings > 0)
6152 it->ignore_overlay_strings_at_pos_p = 1;
6153 it->len = it->dpvec_char_len;
6154 set_iterator_to_next (it, reseat_p);
6155 }
6156
6157 /* Maybe recheck faces after display vector */
6158 if (recheck_faces)
6159 it->stop_charpos = IT_CHARPOS (*it);
6160 }
6161 break;
6162
6163 case GET_FROM_STRING:
6164 /* Current display element is a character from a Lisp string. */
6165 xassert (it->s == NULL && STRINGP (it->string));
6166 IT_STRING_BYTEPOS (*it) += it->len;
6167 IT_STRING_CHARPOS (*it) += 1;
6168
6169 consider_string_end:
6170
6171 if (it->current.overlay_string_index >= 0)
6172 {
6173 /* IT->string is an overlay string. Advance to the
6174 next, if there is one. */
6175 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6176 next_overlay_string (it);
6177 }
6178 else
6179 {
6180 /* IT->string is not an overlay string. If we reached
6181 its end, and there is something on IT->stack, proceed
6182 with what is on the stack. This can be either another
6183 string, this time an overlay string, or a buffer. */
6184 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6185 && it->sp > 0)
6186 {
6187 pop_it (it);
6188 if (it->method == GET_FROM_STRING)
6189 goto consider_string_end;
6190 }
6191 }
6192 break;
6193
6194 case GET_FROM_IMAGE:
6195 case GET_FROM_STRETCH:
6196 /* The position etc with which we have to proceed are on
6197 the stack. The position may be at the end of a string,
6198 if the `display' property takes up the whole string. */
6199 xassert (it->sp > 0);
6200 pop_it (it);
6201 if (it->method == GET_FROM_STRING)
6202 goto consider_string_end;
6203 break;
6204
6205 default:
6206 /* There are no other methods defined, so this should be a bug. */
6207 abort ();
6208 }
6209
6210 xassert (it->method != GET_FROM_STRING
6211 || (STRINGP (it->string)
6212 && IT_STRING_CHARPOS (*it) >= 0));
6213 }
6214
6215 /* Load IT's display element fields with information about the next
6216 display element which comes from a display table entry or from the
6217 result of translating a control character to one of the forms `^C'
6218 or `\003'.
6219
6220 IT->dpvec holds the glyphs to return as characters.
6221 IT->saved_face_id holds the face id before the display vector--
6222 it is restored into IT->face_idin set_iterator_to_next. */
6223
6224 static int
6225 next_element_from_display_vector (it)
6226 struct it *it;
6227 {
6228 Lisp_Object gc;
6229
6230 /* Precondition. */
6231 xassert (it->dpvec && it->current.dpvec_index >= 0);
6232
6233 it->face_id = it->saved_face_id;
6234
6235 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6236 That seemed totally bogus - so I changed it... */
6237 gc = it->dpvec[it->current.dpvec_index];
6238
6239 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6240 {
6241 it->c = GLYPH_CODE_CHAR (gc);
6242 it->len = CHAR_BYTES (it->c);
6243
6244 /* The entry may contain a face id to use. Such a face id is
6245 the id of a Lisp face, not a realized face. A face id of
6246 zero means no face is specified. */
6247 if (it->dpvec_face_id >= 0)
6248 it->face_id = it->dpvec_face_id;
6249 else
6250 {
6251 int lface_id = GLYPH_CODE_FACE (gc);
6252 if (lface_id > 0)
6253 it->face_id = merge_faces (it->f, Qt, lface_id,
6254 it->saved_face_id);
6255 }
6256 }
6257 else
6258 /* Display table entry is invalid. Return a space. */
6259 it->c = ' ', it->len = 1;
6260
6261 /* Don't change position and object of the iterator here. They are
6262 still the values of the character that had this display table
6263 entry or was translated, and that's what we want. */
6264 it->what = IT_CHARACTER;
6265 return 1;
6266 }
6267
6268
6269 /* Load IT with the next display element from Lisp string IT->string.
6270 IT->current.string_pos is the current position within the string.
6271 If IT->current.overlay_string_index >= 0, the Lisp string is an
6272 overlay string. */
6273
6274 static int
6275 next_element_from_string (it)
6276 struct it *it;
6277 {
6278 struct text_pos position;
6279
6280 xassert (STRINGP (it->string));
6281 xassert (IT_STRING_CHARPOS (*it) >= 0);
6282 position = it->current.string_pos;
6283
6284 /* Time to check for invisible text? */
6285 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6286 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6287 {
6288 handle_stop (it);
6289
6290 /* Since a handler may have changed IT->method, we must
6291 recurse here. */
6292 return GET_NEXT_DISPLAY_ELEMENT (it);
6293 }
6294
6295 if (it->current.overlay_string_index >= 0)
6296 {
6297 /* Get the next character from an overlay string. In overlay
6298 strings, There is no field width or padding with spaces to
6299 do. */
6300 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6301 {
6302 it->what = IT_EOB;
6303 return 0;
6304 }
6305 else if (STRING_MULTIBYTE (it->string))
6306 {
6307 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6308 const unsigned char *s = (SDATA (it->string)
6309 + IT_STRING_BYTEPOS (*it));
6310 it->c = string_char_and_length (s, remaining, &it->len);
6311 }
6312 else
6313 {
6314 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6315 it->len = 1;
6316 }
6317 }
6318 else
6319 {
6320 /* Get the next character from a Lisp string that is not an
6321 overlay string. Such strings come from the mode line, for
6322 example. We may have to pad with spaces, or truncate the
6323 string. See also next_element_from_c_string. */
6324 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6325 {
6326 it->what = IT_EOB;
6327 return 0;
6328 }
6329 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6330 {
6331 /* Pad with spaces. */
6332 it->c = ' ', it->len = 1;
6333 CHARPOS (position) = BYTEPOS (position) = -1;
6334 }
6335 else if (STRING_MULTIBYTE (it->string))
6336 {
6337 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6338 const unsigned char *s = (SDATA (it->string)
6339 + IT_STRING_BYTEPOS (*it));
6340 it->c = string_char_and_length (s, maxlen, &it->len);
6341 }
6342 else
6343 {
6344 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6345 it->len = 1;
6346 }
6347 }
6348
6349 /* Record what we have and where it came from. */
6350 it->what = IT_CHARACTER;
6351 it->object = it->string;
6352 it->position = position;
6353 return 1;
6354 }
6355
6356
6357 /* Load IT with next display element from C string IT->s.
6358 IT->string_nchars is the maximum number of characters to return
6359 from the string. IT->end_charpos may be greater than
6360 IT->string_nchars when this function is called, in which case we
6361 may have to return padding spaces. Value is zero if end of string
6362 reached, including padding spaces. */
6363
6364 static int
6365 next_element_from_c_string (it)
6366 struct it *it;
6367 {
6368 int success_p = 1;
6369
6370 xassert (it->s);
6371 it->what = IT_CHARACTER;
6372 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6373 it->object = Qnil;
6374
6375 /* IT's position can be greater IT->string_nchars in case a field
6376 width or precision has been specified when the iterator was
6377 initialized. */
6378 if (IT_CHARPOS (*it) >= it->end_charpos)
6379 {
6380 /* End of the game. */
6381 it->what = IT_EOB;
6382 success_p = 0;
6383 }
6384 else if (IT_CHARPOS (*it) >= it->string_nchars)
6385 {
6386 /* Pad with spaces. */
6387 it->c = ' ', it->len = 1;
6388 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6389 }
6390 else if (it->multibyte_p)
6391 {
6392 /* Implementation note: The calls to strlen apparently aren't a
6393 performance problem because there is no noticeable performance
6394 difference between Emacs running in unibyte or multibyte mode. */
6395 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6396 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6397 maxlen, &it->len);
6398 }
6399 else
6400 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6401
6402 return success_p;
6403 }
6404
6405
6406 /* Set up IT to return characters from an ellipsis, if appropriate.
6407 The definition of the ellipsis glyphs may come from a display table
6408 entry. This function Fills IT with the first glyph from the
6409 ellipsis if an ellipsis is to be displayed. */
6410
6411 static int
6412 next_element_from_ellipsis (it)
6413 struct it *it;
6414 {
6415 if (it->selective_display_ellipsis_p)
6416 setup_for_ellipsis (it, it->len);
6417 else
6418 {
6419 /* The face at the current position may be different from the
6420 face we find after the invisible text. Remember what it
6421 was in IT->saved_face_id, and signal that it's there by
6422 setting face_before_selective_p. */
6423 it->saved_face_id = it->face_id;
6424 it->method = GET_FROM_BUFFER;
6425 it->object = it->w->buffer;
6426 reseat_at_next_visible_line_start (it, 1);
6427 it->face_before_selective_p = 1;
6428 }
6429
6430 return GET_NEXT_DISPLAY_ELEMENT (it);
6431 }
6432
6433
6434 /* Deliver an image display element. The iterator IT is already
6435 filled with image information (done in handle_display_prop). Value
6436 is always 1. */
6437
6438
6439 static int
6440 next_element_from_image (it)
6441 struct it *it;
6442 {
6443 it->what = IT_IMAGE;
6444 return 1;
6445 }
6446
6447
6448 /* Fill iterator IT with next display element from a stretch glyph
6449 property. IT->object is the value of the text property. Value is
6450 always 1. */
6451
6452 static int
6453 next_element_from_stretch (it)
6454 struct it *it;
6455 {
6456 it->what = IT_STRETCH;
6457 return 1;
6458 }
6459
6460
6461 /* Load IT with the next display element from current_buffer. Value
6462 is zero if end of buffer reached. IT->stop_charpos is the next
6463 position at which to stop and check for text properties or buffer
6464 end. */
6465
6466 static int
6467 next_element_from_buffer (it)
6468 struct it *it;
6469 {
6470 int success_p = 1;
6471
6472 /* Check this assumption, otherwise, we would never enter the
6473 if-statement, below. */
6474 xassert (IT_CHARPOS (*it) >= BEGV
6475 && IT_CHARPOS (*it) <= it->stop_charpos);
6476
6477 if (IT_CHARPOS (*it) >= it->stop_charpos)
6478 {
6479 if (IT_CHARPOS (*it) >= it->end_charpos)
6480 {
6481 int overlay_strings_follow_p;
6482
6483 /* End of the game, except when overlay strings follow that
6484 haven't been returned yet. */
6485 if (it->overlay_strings_at_end_processed_p)
6486 overlay_strings_follow_p = 0;
6487 else
6488 {
6489 it->overlay_strings_at_end_processed_p = 1;
6490 overlay_strings_follow_p = get_overlay_strings (it, 0);
6491 }
6492
6493 if (overlay_strings_follow_p)
6494 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6495 else
6496 {
6497 it->what = IT_EOB;
6498 it->position = it->current.pos;
6499 success_p = 0;
6500 }
6501 }
6502 else
6503 {
6504 handle_stop (it);
6505 return GET_NEXT_DISPLAY_ELEMENT (it);
6506 }
6507 }
6508 else
6509 {
6510 /* No face changes, overlays etc. in sight, so just return a
6511 character from current_buffer. */
6512 unsigned char *p;
6513
6514 /* Maybe run the redisplay end trigger hook. Performance note:
6515 This doesn't seem to cost measurable time. */
6516 if (it->redisplay_end_trigger_charpos
6517 && it->glyph_row
6518 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6519 run_redisplay_end_trigger_hook (it);
6520
6521 /* Get the next character, maybe multibyte. */
6522 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6523 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6524 {
6525 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6526 - IT_BYTEPOS (*it));
6527 it->c = string_char_and_length (p, maxlen, &it->len);
6528 }
6529 else
6530 it->c = *p, it->len = 1;
6531
6532 /* Record what we have and where it came from. */
6533 it->what = IT_CHARACTER;
6534 it->object = it->w->buffer;
6535 it->position = it->current.pos;
6536
6537 /* Normally we return the character found above, except when we
6538 really want to return an ellipsis for selective display. */
6539 if (it->selective)
6540 {
6541 if (it->c == '\n')
6542 {
6543 /* A value of selective > 0 means hide lines indented more
6544 than that number of columns. */
6545 if (it->selective > 0
6546 && IT_CHARPOS (*it) + 1 < ZV
6547 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6548 IT_BYTEPOS (*it) + 1,
6549 (double) it->selective)) /* iftc */
6550 {
6551 success_p = next_element_from_ellipsis (it);
6552 it->dpvec_char_len = -1;
6553 }
6554 }
6555 else if (it->c == '\r' && it->selective == -1)
6556 {
6557 /* A value of selective == -1 means that everything from the
6558 CR to the end of the line is invisible, with maybe an
6559 ellipsis displayed for it. */
6560 success_p = next_element_from_ellipsis (it);
6561 it->dpvec_char_len = -1;
6562 }
6563 }
6564 }
6565
6566 /* Value is zero if end of buffer reached. */
6567 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6568 return success_p;
6569 }
6570
6571
6572 /* Run the redisplay end trigger hook for IT. */
6573
6574 static void
6575 run_redisplay_end_trigger_hook (it)
6576 struct it *it;
6577 {
6578 Lisp_Object args[3];
6579
6580 /* IT->glyph_row should be non-null, i.e. we should be actually
6581 displaying something, or otherwise we should not run the hook. */
6582 xassert (it->glyph_row);
6583
6584 /* Set up hook arguments. */
6585 args[0] = Qredisplay_end_trigger_functions;
6586 args[1] = it->window;
6587 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6588 it->redisplay_end_trigger_charpos = 0;
6589
6590 /* Since we are *trying* to run these functions, don't try to run
6591 them again, even if they get an error. */
6592 it->w->redisplay_end_trigger = Qnil;
6593 Frun_hook_with_args (3, args);
6594
6595 /* Notice if it changed the face of the character we are on. */
6596 handle_face_prop (it);
6597 }
6598
6599
6600 /* Deliver a composition display element. The iterator IT is already
6601 filled with composition information (done in
6602 handle_composition_prop). Value is always 1. */
6603
6604 static int
6605 next_element_from_composition (it)
6606 struct it *it;
6607 {
6608 it->what = IT_COMPOSITION;
6609 it->position = (STRINGP (it->string)
6610 ? it->current.string_pos
6611 : it->current.pos);
6612 if (STRINGP (it->string))
6613 it->object = it->string;
6614 else
6615 it->object = it->w->buffer;
6616 return 1;
6617 }
6618
6619
6620 \f
6621 /***********************************************************************
6622 Moving an iterator without producing glyphs
6623 ***********************************************************************/
6624
6625 /* Check if iterator is at a position corresponding to a valid buffer
6626 position after some move_it_ call. */
6627
6628 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6629 ((it)->method == GET_FROM_STRING \
6630 ? IT_STRING_CHARPOS (*it) == 0 \
6631 : 1)
6632
6633
6634 /* Move iterator IT to a specified buffer or X position within one
6635 line on the display without producing glyphs.
6636
6637 OP should be a bit mask including some or all of these bits:
6638 MOVE_TO_X: Stop on reaching x-position TO_X.
6639 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6640 Regardless of OP's value, stop in reaching the end of the display line.
6641
6642 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6643 This means, in particular, that TO_X includes window's horizontal
6644 scroll amount.
6645
6646 The return value has several possible values that
6647 say what condition caused the scan to stop:
6648
6649 MOVE_POS_MATCH_OR_ZV
6650 - when TO_POS or ZV was reached.
6651
6652 MOVE_X_REACHED
6653 -when TO_X was reached before TO_POS or ZV were reached.
6654
6655 MOVE_LINE_CONTINUED
6656 - when we reached the end of the display area and the line must
6657 be continued.
6658
6659 MOVE_LINE_TRUNCATED
6660 - when we reached the end of the display area and the line is
6661 truncated.
6662
6663 MOVE_NEWLINE_OR_CR
6664 - when we stopped at a line end, i.e. a newline or a CR and selective
6665 display is on. */
6666
6667 static enum move_it_result
6668 move_it_in_display_line_to (struct it *it,
6669 EMACS_INT to_charpos, int to_x,
6670 enum move_operation_enum op)
6671 {
6672 enum move_it_result result = MOVE_UNDEFINED;
6673 struct glyph_row *saved_glyph_row;
6674 struct it wrap_it, atpos_it, atx_it;
6675 int may_wrap = 0;
6676
6677 /* Don't produce glyphs in produce_glyphs. */
6678 saved_glyph_row = it->glyph_row;
6679 it->glyph_row = NULL;
6680
6681 /* Use wrap_it to save a copy of IT wherever a word wrap could
6682 occur. Use atpos_it to save a copy of IT at the desired buffer
6683 position, if found, so that we can scan ahead and check if the
6684 word later overshoots the window edge. Use atx_it similarly, for
6685 pixel positions. */
6686 wrap_it.sp = -1;
6687 atpos_it.sp = -1;
6688 atx_it.sp = -1;
6689
6690 #define BUFFER_POS_REACHED_P() \
6691 ((op & MOVE_TO_POS) != 0 \
6692 && BUFFERP (it->object) \
6693 && IT_CHARPOS (*it) >= to_charpos \
6694 && (it->method == GET_FROM_BUFFER \
6695 || (it->method == GET_FROM_DISPLAY_VECTOR \
6696 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6697
6698 /* If there's a line-/wrap-prefix, handle it. */
6699 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6700 && it->current_y < it->last_visible_y)
6701 handle_line_prefix (it);
6702
6703 while (1)
6704 {
6705 int x, i, ascent = 0, descent = 0;
6706
6707 /* Utility macro to reset an iterator with x, ascent, and descent. */
6708 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6709 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6710 (IT)->max_descent = descent)
6711
6712 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6713 glyph). */
6714 if ((op & MOVE_TO_POS) != 0
6715 && BUFFERP (it->object)
6716 && it->method == GET_FROM_BUFFER
6717 && IT_CHARPOS (*it) > to_charpos)
6718 {
6719 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6720 {
6721 result = MOVE_POS_MATCH_OR_ZV;
6722 break;
6723 }
6724 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6725 /* If wrap_it is valid, the current position might be in a
6726 word that is wrapped. So, save the iterator in
6727 atpos_it and continue to see if wrapping happens. */
6728 atpos_it = *it;
6729 }
6730
6731 /* Stop when ZV reached.
6732 We used to stop here when TO_CHARPOS reached as well, but that is
6733 too soon if this glyph does not fit on this line. So we handle it
6734 explicitly below. */
6735 if (!get_next_display_element (it))
6736 {
6737 result = MOVE_POS_MATCH_OR_ZV;
6738 break;
6739 }
6740
6741 if (it->line_wrap == TRUNCATE)
6742 {
6743 if (BUFFER_POS_REACHED_P ())
6744 {
6745 result = MOVE_POS_MATCH_OR_ZV;
6746 break;
6747 }
6748 }
6749 else
6750 {
6751 if (it->line_wrap == WORD_WRAP)
6752 {
6753 if (IT_DISPLAYING_WHITESPACE (it))
6754 may_wrap = 1;
6755 else if (may_wrap)
6756 {
6757 /* We have reached a glyph that follows one or more
6758 whitespace characters. If the position is
6759 already found, we are done. */
6760 if (atpos_it.sp >= 0)
6761 {
6762 *it = atpos_it;
6763 result = MOVE_POS_MATCH_OR_ZV;
6764 goto done;
6765 }
6766 if (atx_it.sp >= 0)
6767 {
6768 *it = atx_it;
6769 result = MOVE_X_REACHED;
6770 goto done;
6771 }
6772 /* Otherwise, we can wrap here. */
6773 wrap_it = *it;
6774 may_wrap = 0;
6775 }
6776 }
6777 }
6778
6779 /* Remember the line height for the current line, in case
6780 the next element doesn't fit on the line. */
6781 ascent = it->max_ascent;
6782 descent = it->max_descent;
6783
6784 /* The call to produce_glyphs will get the metrics of the
6785 display element IT is loaded with. Record the x-position
6786 before this display element, in case it doesn't fit on the
6787 line. */
6788 x = it->current_x;
6789
6790 PRODUCE_GLYPHS (it);
6791
6792 if (it->area != TEXT_AREA)
6793 {
6794 set_iterator_to_next (it, 1);
6795 continue;
6796 }
6797
6798 /* The number of glyphs we get back in IT->nglyphs will normally
6799 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6800 character on a terminal frame, or (iii) a line end. For the
6801 second case, IT->nglyphs - 1 padding glyphs will be present
6802 (on X frames, there is only one glyph produced for a
6803 composite character.
6804
6805 The behavior implemented below means, for continuation lines,
6806 that as many spaces of a TAB as fit on the current line are
6807 displayed there. For terminal frames, as many glyphs of a
6808 multi-glyph character are displayed in the current line, too.
6809 This is what the old redisplay code did, and we keep it that
6810 way. Under X, the whole shape of a complex character must
6811 fit on the line or it will be completely displayed in the
6812 next line.
6813
6814 Note that both for tabs and padding glyphs, all glyphs have
6815 the same width. */
6816 if (it->nglyphs)
6817 {
6818 /* More than one glyph or glyph doesn't fit on line. All
6819 glyphs have the same width. */
6820 int single_glyph_width = it->pixel_width / it->nglyphs;
6821 int new_x;
6822 int x_before_this_char = x;
6823 int hpos_before_this_char = it->hpos;
6824
6825 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6826 {
6827 new_x = x + single_glyph_width;
6828
6829 /* We want to leave anything reaching TO_X to the caller. */
6830 if ((op & MOVE_TO_X) && new_x > to_x)
6831 {
6832 if (BUFFER_POS_REACHED_P ())
6833 {
6834 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6835 goto buffer_pos_reached;
6836 if (atpos_it.sp < 0)
6837 {
6838 atpos_it = *it;
6839 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6840 }
6841 }
6842 else
6843 {
6844 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6845 {
6846 it->current_x = x;
6847 result = MOVE_X_REACHED;
6848 break;
6849 }
6850 if (atx_it.sp < 0)
6851 {
6852 atx_it = *it;
6853 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6854 }
6855 }
6856 }
6857
6858 if (/* Lines are continued. */
6859 it->line_wrap != TRUNCATE
6860 && (/* And glyph doesn't fit on the line. */
6861 new_x > it->last_visible_x
6862 /* Or it fits exactly and we're on a window
6863 system frame. */
6864 || (new_x == it->last_visible_x
6865 && FRAME_WINDOW_P (it->f))))
6866 {
6867 if (/* IT->hpos == 0 means the very first glyph
6868 doesn't fit on the line, e.g. a wide image. */
6869 it->hpos == 0
6870 || (new_x == it->last_visible_x
6871 && FRAME_WINDOW_P (it->f)))
6872 {
6873 ++it->hpos;
6874 it->current_x = new_x;
6875
6876 /* The character's last glyph just barely fits
6877 in this row. */
6878 if (i == it->nglyphs - 1)
6879 {
6880 /* If this is the destination position,
6881 return a position *before* it in this row,
6882 now that we know it fits in this row. */
6883 if (BUFFER_POS_REACHED_P ())
6884 {
6885 if (it->line_wrap != WORD_WRAP
6886 || wrap_it.sp < 0)
6887 {
6888 it->hpos = hpos_before_this_char;
6889 it->current_x = x_before_this_char;
6890 result = MOVE_POS_MATCH_OR_ZV;
6891 break;
6892 }
6893 if (it->line_wrap == WORD_WRAP
6894 && atpos_it.sp < 0)
6895 {
6896 atpos_it = *it;
6897 atpos_it.current_x = x_before_this_char;
6898 atpos_it.hpos = hpos_before_this_char;
6899 }
6900 }
6901
6902 set_iterator_to_next (it, 1);
6903 #ifdef HAVE_WINDOW_SYSTEM
6904 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6905 {
6906 if (!get_next_display_element (it))
6907 {
6908 result = MOVE_POS_MATCH_OR_ZV;
6909 break;
6910 }
6911 if (BUFFER_POS_REACHED_P ())
6912 {
6913 if (ITERATOR_AT_END_OF_LINE_P (it))
6914 result = MOVE_POS_MATCH_OR_ZV;
6915 else
6916 result = MOVE_LINE_CONTINUED;
6917 break;
6918 }
6919 if (ITERATOR_AT_END_OF_LINE_P (it))
6920 {
6921 result = MOVE_NEWLINE_OR_CR;
6922 break;
6923 }
6924 }
6925 #endif /* HAVE_WINDOW_SYSTEM */
6926 }
6927 }
6928 else
6929 IT_RESET_X_ASCENT_DESCENT (it);
6930
6931 if (wrap_it.sp >= 0)
6932 {
6933 *it = wrap_it;
6934 atpos_it.sp = -1;
6935 atx_it.sp = -1;
6936 }
6937
6938 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6939 IT_CHARPOS (*it)));
6940 result = MOVE_LINE_CONTINUED;
6941 break;
6942 }
6943
6944 if (BUFFER_POS_REACHED_P ())
6945 {
6946 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6947 goto buffer_pos_reached;
6948 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6949 {
6950 atpos_it = *it;
6951 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6952 }
6953 }
6954
6955 if (new_x > it->first_visible_x)
6956 {
6957 /* Glyph is visible. Increment number of glyphs that
6958 would be displayed. */
6959 ++it->hpos;
6960 }
6961 }
6962
6963 if (result != MOVE_UNDEFINED)
6964 break;
6965 }
6966 else if (BUFFER_POS_REACHED_P ())
6967 {
6968 buffer_pos_reached:
6969 IT_RESET_X_ASCENT_DESCENT (it);
6970 result = MOVE_POS_MATCH_OR_ZV;
6971 break;
6972 }
6973 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6974 {
6975 /* Stop when TO_X specified and reached. This check is
6976 necessary here because of lines consisting of a line end,
6977 only. The line end will not produce any glyphs and we
6978 would never get MOVE_X_REACHED. */
6979 xassert (it->nglyphs == 0);
6980 result = MOVE_X_REACHED;
6981 break;
6982 }
6983
6984 /* Is this a line end? If yes, we're done. */
6985 if (ITERATOR_AT_END_OF_LINE_P (it))
6986 {
6987 result = MOVE_NEWLINE_OR_CR;
6988 break;
6989 }
6990
6991 /* The current display element has been consumed. Advance
6992 to the next. */
6993 set_iterator_to_next (it, 1);
6994
6995 /* Stop if lines are truncated and IT's current x-position is
6996 past the right edge of the window now. */
6997 if (it->line_wrap == TRUNCATE
6998 && it->current_x >= it->last_visible_x)
6999 {
7000 #ifdef HAVE_WINDOW_SYSTEM
7001 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7002 {
7003 if (!get_next_display_element (it)
7004 || BUFFER_POS_REACHED_P ())
7005 {
7006 result = MOVE_POS_MATCH_OR_ZV;
7007 break;
7008 }
7009 if (ITERATOR_AT_END_OF_LINE_P (it))
7010 {
7011 result = MOVE_NEWLINE_OR_CR;
7012 break;
7013 }
7014 }
7015 #endif /* HAVE_WINDOW_SYSTEM */
7016 result = MOVE_LINE_TRUNCATED;
7017 break;
7018 }
7019 #undef IT_RESET_X_ASCENT_DESCENT
7020 }
7021
7022 #undef BUFFER_POS_REACHED_P
7023
7024 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7025 restore the saved iterator. */
7026 if (atpos_it.sp >= 0)
7027 *it = atpos_it;
7028 else if (atx_it.sp >= 0)
7029 *it = atx_it;
7030
7031 done:
7032
7033 /* Restore the iterator settings altered at the beginning of this
7034 function. */
7035 it->glyph_row = saved_glyph_row;
7036 return result;
7037 }
7038
7039 /* For external use. */
7040 void
7041 move_it_in_display_line (struct it *it,
7042 EMACS_INT to_charpos, int to_x,
7043 enum move_operation_enum op)
7044 {
7045 move_it_in_display_line_to (it, to_charpos, to_x, op);
7046 }
7047
7048
7049 /* Move IT forward until it satisfies one or more of the criteria in
7050 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7051
7052 OP is a bit-mask that specifies where to stop, and in particular,
7053 which of those four position arguments makes a difference. See the
7054 description of enum move_operation_enum.
7055
7056 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7057 screen line, this function will set IT to the next position >
7058 TO_CHARPOS. */
7059
7060 void
7061 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7062 struct it *it;
7063 int to_charpos, to_x, to_y, to_vpos;
7064 int op;
7065 {
7066 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7067 int line_height;
7068 int reached = 0;
7069
7070 for (;;)
7071 {
7072 if (op & MOVE_TO_VPOS)
7073 {
7074 /* If no TO_CHARPOS and no TO_X specified, stop at the
7075 start of the line TO_VPOS. */
7076 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7077 {
7078 if (it->vpos == to_vpos)
7079 {
7080 reached = 1;
7081 break;
7082 }
7083 else
7084 skip = move_it_in_display_line_to (it, -1, -1, 0);
7085 }
7086 else
7087 {
7088 /* TO_VPOS >= 0 means stop at TO_X in the line at
7089 TO_VPOS, or at TO_POS, whichever comes first. */
7090 if (it->vpos == to_vpos)
7091 {
7092 reached = 2;
7093 break;
7094 }
7095
7096 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7097
7098 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7099 {
7100 reached = 3;
7101 break;
7102 }
7103 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7104 {
7105 /* We have reached TO_X but not in the line we want. */
7106 skip = move_it_in_display_line_to (it, to_charpos,
7107 -1, MOVE_TO_POS);
7108 if (skip == MOVE_POS_MATCH_OR_ZV)
7109 {
7110 reached = 4;
7111 break;
7112 }
7113 }
7114 }
7115 }
7116 else if (op & MOVE_TO_Y)
7117 {
7118 struct it it_backup;
7119
7120 if (it->line_wrap == WORD_WRAP)
7121 it_backup = *it;
7122
7123 /* TO_Y specified means stop at TO_X in the line containing
7124 TO_Y---or at TO_CHARPOS if this is reached first. The
7125 problem is that we can't really tell whether the line
7126 contains TO_Y before we have completely scanned it, and
7127 this may skip past TO_X. What we do is to first scan to
7128 TO_X.
7129
7130 If TO_X is not specified, use a TO_X of zero. The reason
7131 is to make the outcome of this function more predictable.
7132 If we didn't use TO_X == 0, we would stop at the end of
7133 the line which is probably not what a caller would expect
7134 to happen. */
7135 skip = move_it_in_display_line_to
7136 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7137 (MOVE_TO_X | (op & MOVE_TO_POS)));
7138
7139 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7140 if (skip == MOVE_POS_MATCH_OR_ZV)
7141 reached = 5;
7142 else if (skip == MOVE_X_REACHED)
7143 {
7144 /* If TO_X was reached, we want to know whether TO_Y is
7145 in the line. We know this is the case if the already
7146 scanned glyphs make the line tall enough. Otherwise,
7147 we must check by scanning the rest of the line. */
7148 line_height = it->max_ascent + it->max_descent;
7149 if (to_y >= it->current_y
7150 && to_y < it->current_y + line_height)
7151 {
7152 reached = 6;
7153 break;
7154 }
7155 it_backup = *it;
7156 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7157 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7158 op & MOVE_TO_POS);
7159 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7160 line_height = it->max_ascent + it->max_descent;
7161 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7162
7163 if (to_y >= it->current_y
7164 && to_y < it->current_y + line_height)
7165 {
7166 /* If TO_Y is in this line and TO_X was reached
7167 above, we scanned too far. We have to restore
7168 IT's settings to the ones before skipping. */
7169 *it = it_backup;
7170 reached = 6;
7171 }
7172 else
7173 {
7174 skip = skip2;
7175 if (skip == MOVE_POS_MATCH_OR_ZV)
7176 reached = 7;
7177 }
7178 }
7179 else
7180 {
7181 /* Check whether TO_Y is in this line. */
7182 line_height = it->max_ascent + it->max_descent;
7183 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7184
7185 if (to_y >= it->current_y
7186 && to_y < it->current_y + line_height)
7187 {
7188 /* When word-wrap is on, TO_X may lie past the end
7189 of a wrapped line. Then it->current is the
7190 character on the next line, so backtrack to the
7191 space before the wrap point. */
7192 if (skip == MOVE_LINE_CONTINUED
7193 && it->line_wrap == WORD_WRAP)
7194 {
7195 int prev_x = max (it->current_x - 1, 0);
7196 *it = it_backup;
7197 skip = move_it_in_display_line_to
7198 (it, -1, prev_x, MOVE_TO_X);
7199 }
7200 reached = 6;
7201 }
7202 }
7203
7204 if (reached)
7205 break;
7206 }
7207 else if (BUFFERP (it->object)
7208 && it->method == GET_FROM_BUFFER
7209 && IT_CHARPOS (*it) >= to_charpos)
7210 skip = MOVE_POS_MATCH_OR_ZV;
7211 else
7212 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7213
7214 switch (skip)
7215 {
7216 case MOVE_POS_MATCH_OR_ZV:
7217 reached = 8;
7218 goto out;
7219
7220 case MOVE_NEWLINE_OR_CR:
7221 set_iterator_to_next (it, 1);
7222 it->continuation_lines_width = 0;
7223 break;
7224
7225 case MOVE_LINE_TRUNCATED:
7226 it->continuation_lines_width = 0;
7227 reseat_at_next_visible_line_start (it, 0);
7228 if ((op & MOVE_TO_POS) != 0
7229 && IT_CHARPOS (*it) > to_charpos)
7230 {
7231 reached = 9;
7232 goto out;
7233 }
7234 break;
7235
7236 case MOVE_LINE_CONTINUED:
7237 /* For continued lines ending in a tab, some of the glyphs
7238 associated with the tab are displayed on the current
7239 line. Since it->current_x does not include these glyphs,
7240 we use it->last_visible_x instead. */
7241 it->continuation_lines_width +=
7242 (it->c == '\t') ? it->last_visible_x : it->current_x;
7243 break;
7244
7245 default:
7246 abort ();
7247 }
7248
7249 /* Reset/increment for the next run. */
7250 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7251 it->current_x = it->hpos = 0;
7252 it->current_y += it->max_ascent + it->max_descent;
7253 ++it->vpos;
7254 last_height = it->max_ascent + it->max_descent;
7255 last_max_ascent = it->max_ascent;
7256 it->max_ascent = it->max_descent = 0;
7257 }
7258
7259 out:
7260
7261 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7262 }
7263
7264
7265 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7266
7267 If DY > 0, move IT backward at least that many pixels. DY = 0
7268 means move IT backward to the preceding line start or BEGV. This
7269 function may move over more than DY pixels if IT->current_y - DY
7270 ends up in the middle of a line; in this case IT->current_y will be
7271 set to the top of the line moved to. */
7272
7273 void
7274 move_it_vertically_backward (it, dy)
7275 struct it *it;
7276 int dy;
7277 {
7278 int nlines, h;
7279 struct it it2, it3;
7280 int start_pos;
7281
7282 move_further_back:
7283 xassert (dy >= 0);
7284
7285 start_pos = IT_CHARPOS (*it);
7286
7287 /* Estimate how many newlines we must move back. */
7288 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7289
7290 /* Set the iterator's position that many lines back. */
7291 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7292 back_to_previous_visible_line_start (it);
7293
7294 /* Reseat the iterator here. When moving backward, we don't want
7295 reseat to skip forward over invisible text, set up the iterator
7296 to deliver from overlay strings at the new position etc. So,
7297 use reseat_1 here. */
7298 reseat_1 (it, it->current.pos, 1);
7299
7300 /* We are now surely at a line start. */
7301 it->current_x = it->hpos = 0;
7302 it->continuation_lines_width = 0;
7303
7304 /* Move forward and see what y-distance we moved. First move to the
7305 start of the next line so that we get its height. We need this
7306 height to be able to tell whether we reached the specified
7307 y-distance. */
7308 it2 = *it;
7309 it2.max_ascent = it2.max_descent = 0;
7310 do
7311 {
7312 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7313 MOVE_TO_POS | MOVE_TO_VPOS);
7314 }
7315 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7316 xassert (IT_CHARPOS (*it) >= BEGV);
7317 it3 = it2;
7318
7319 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7320 xassert (IT_CHARPOS (*it) >= BEGV);
7321 /* H is the actual vertical distance from the position in *IT
7322 and the starting position. */
7323 h = it2.current_y - it->current_y;
7324 /* NLINES is the distance in number of lines. */
7325 nlines = it2.vpos - it->vpos;
7326
7327 /* Correct IT's y and vpos position
7328 so that they are relative to the starting point. */
7329 it->vpos -= nlines;
7330 it->current_y -= h;
7331
7332 if (dy == 0)
7333 {
7334 /* DY == 0 means move to the start of the screen line. The
7335 value of nlines is > 0 if continuation lines were involved. */
7336 if (nlines > 0)
7337 move_it_by_lines (it, nlines, 1);
7338 #if 0
7339 /* I think this assert is bogus if buffer contains
7340 invisible text or images. KFS. */
7341 xassert (IT_CHARPOS (*it) <= start_pos);
7342 #endif
7343 }
7344 else
7345 {
7346 /* The y-position we try to reach, relative to *IT.
7347 Note that H has been subtracted in front of the if-statement. */
7348 int target_y = it->current_y + h - dy;
7349 int y0 = it3.current_y;
7350 int y1 = line_bottom_y (&it3);
7351 int line_height = y1 - y0;
7352
7353 /* If we did not reach target_y, try to move further backward if
7354 we can. If we moved too far backward, try to move forward. */
7355 if (target_y < it->current_y
7356 /* This is heuristic. In a window that's 3 lines high, with
7357 a line height of 13 pixels each, recentering with point
7358 on the bottom line will try to move -39/2 = 19 pixels
7359 backward. Try to avoid moving into the first line. */
7360 && (it->current_y - target_y
7361 > min (window_box_height (it->w), line_height * 2 / 3))
7362 && IT_CHARPOS (*it) > BEGV)
7363 {
7364 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7365 target_y - it->current_y));
7366 dy = it->current_y - target_y;
7367 goto move_further_back;
7368 }
7369 else if (target_y >= it->current_y + line_height
7370 && IT_CHARPOS (*it) < ZV)
7371 {
7372 /* Should move forward by at least one line, maybe more.
7373
7374 Note: Calling move_it_by_lines can be expensive on
7375 terminal frames, where compute_motion is used (via
7376 vmotion) to do the job, when there are very long lines
7377 and truncate-lines is nil. That's the reason for
7378 treating terminal frames specially here. */
7379
7380 if (!FRAME_WINDOW_P (it->f))
7381 move_it_vertically (it, target_y - (it->current_y + line_height));
7382 else
7383 {
7384 do
7385 {
7386 move_it_by_lines (it, 1, 1);
7387 }
7388 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7389 }
7390
7391 #if 0
7392 /* I think this assert is bogus if buffer contains
7393 invisible text or images. KFS. */
7394 xassert (IT_CHARPOS (*it) >= BEGV);
7395 #endif
7396 }
7397 }
7398 }
7399
7400
7401 /* Move IT by a specified amount of pixel lines DY. DY negative means
7402 move backwards. DY = 0 means move to start of screen line. At the
7403 end, IT will be on the start of a screen line. */
7404
7405 void
7406 move_it_vertically (it, dy)
7407 struct it *it;
7408 int dy;
7409 {
7410 if (dy <= 0)
7411 move_it_vertically_backward (it, -dy);
7412 else
7413 {
7414 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7415 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7416 MOVE_TO_POS | MOVE_TO_Y);
7417 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7418
7419 /* If buffer ends in ZV without a newline, move to the start of
7420 the line to satisfy the post-condition. */
7421 if (IT_CHARPOS (*it) == ZV
7422 && ZV > BEGV
7423 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7424 move_it_by_lines (it, 0, 0);
7425 }
7426 }
7427
7428
7429 /* Move iterator IT past the end of the text line it is in. */
7430
7431 void
7432 move_it_past_eol (it)
7433 struct it *it;
7434 {
7435 enum move_it_result rc;
7436
7437 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7438 if (rc == MOVE_NEWLINE_OR_CR)
7439 set_iterator_to_next (it, 0);
7440 }
7441
7442
7443 #if 0 /* Currently not used. */
7444
7445 /* Return non-zero if some text between buffer positions START_CHARPOS
7446 and END_CHARPOS is invisible. IT->window is the window for text
7447 property lookup. */
7448
7449 static int
7450 invisible_text_between_p (it, start_charpos, end_charpos)
7451 struct it *it;
7452 int start_charpos, end_charpos;
7453 {
7454 Lisp_Object prop, limit;
7455 int invisible_found_p;
7456
7457 xassert (it != NULL && start_charpos <= end_charpos);
7458
7459 /* Is text at START invisible? */
7460 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7461 it->window);
7462 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7463 invisible_found_p = 1;
7464 else
7465 {
7466 limit = Fnext_single_char_property_change (make_number (start_charpos),
7467 Qinvisible, Qnil,
7468 make_number (end_charpos));
7469 invisible_found_p = XFASTINT (limit) < end_charpos;
7470 }
7471
7472 return invisible_found_p;
7473 }
7474
7475 #endif /* 0 */
7476
7477
7478 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7479 negative means move up. DVPOS == 0 means move to the start of the
7480 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7481 NEED_Y_P is zero, IT->current_y will be left unchanged.
7482
7483 Further optimization ideas: If we would know that IT->f doesn't use
7484 a face with proportional font, we could be faster for
7485 truncate-lines nil. */
7486
7487 void
7488 move_it_by_lines (it, dvpos, need_y_p)
7489 struct it *it;
7490 int dvpos, need_y_p;
7491 {
7492 struct position pos;
7493
7494 /* The commented-out optimization uses vmotion on terminals. This
7495 gives bad results, because elements like it->what, on which
7496 callers such as pos_visible_p rely, aren't updated. */
7497 /* if (!FRAME_WINDOW_P (it->f))
7498 {
7499 struct text_pos textpos;
7500
7501 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7502 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7503 reseat (it, textpos, 1);
7504 it->vpos += pos.vpos;
7505 it->current_y += pos.vpos;
7506 }
7507 else */
7508
7509 if (dvpos == 0)
7510 {
7511 /* DVPOS == 0 means move to the start of the screen line. */
7512 move_it_vertically_backward (it, 0);
7513 xassert (it->current_x == 0 && it->hpos == 0);
7514 /* Let next call to line_bottom_y calculate real line height */
7515 last_height = 0;
7516 }
7517 else if (dvpos > 0)
7518 {
7519 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7520 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7521 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7522 }
7523 else
7524 {
7525 struct it it2;
7526 int start_charpos, i;
7527
7528 /* Start at the beginning of the screen line containing IT's
7529 position. This may actually move vertically backwards,
7530 in case of overlays, so adjust dvpos accordingly. */
7531 dvpos += it->vpos;
7532 move_it_vertically_backward (it, 0);
7533 dvpos -= it->vpos;
7534
7535 /* Go back -DVPOS visible lines and reseat the iterator there. */
7536 start_charpos = IT_CHARPOS (*it);
7537 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7538 back_to_previous_visible_line_start (it);
7539 reseat (it, it->current.pos, 1);
7540
7541 /* Move further back if we end up in a string or an image. */
7542 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7543 {
7544 /* First try to move to start of display line. */
7545 dvpos += it->vpos;
7546 move_it_vertically_backward (it, 0);
7547 dvpos -= it->vpos;
7548 if (IT_POS_VALID_AFTER_MOVE_P (it))
7549 break;
7550 /* If start of line is still in string or image,
7551 move further back. */
7552 back_to_previous_visible_line_start (it);
7553 reseat (it, it->current.pos, 1);
7554 dvpos--;
7555 }
7556
7557 it->current_x = it->hpos = 0;
7558
7559 /* Above call may have moved too far if continuation lines
7560 are involved. Scan forward and see if it did. */
7561 it2 = *it;
7562 it2.vpos = it2.current_y = 0;
7563 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7564 it->vpos -= it2.vpos;
7565 it->current_y -= it2.current_y;
7566 it->current_x = it->hpos = 0;
7567
7568 /* If we moved too far back, move IT some lines forward. */
7569 if (it2.vpos > -dvpos)
7570 {
7571 int delta = it2.vpos + dvpos;
7572 it2 = *it;
7573 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7574 /* Move back again if we got too far ahead. */
7575 if (IT_CHARPOS (*it) >= start_charpos)
7576 *it = it2;
7577 }
7578 }
7579 }
7580
7581 /* Return 1 if IT points into the middle of a display vector. */
7582
7583 int
7584 in_display_vector_p (it)
7585 struct it *it;
7586 {
7587 return (it->method == GET_FROM_DISPLAY_VECTOR
7588 && it->current.dpvec_index > 0
7589 && it->dpvec + it->current.dpvec_index != it->dpend);
7590 }
7591
7592 \f
7593 /***********************************************************************
7594 Messages
7595 ***********************************************************************/
7596
7597
7598 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7599 to *Messages*. */
7600
7601 void
7602 add_to_log (format, arg1, arg2)
7603 char *format;
7604 Lisp_Object arg1, arg2;
7605 {
7606 Lisp_Object args[3];
7607 Lisp_Object msg, fmt;
7608 char *buffer;
7609 int len;
7610 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7611 USE_SAFE_ALLOCA;
7612
7613 /* Do nothing if called asynchronously. Inserting text into
7614 a buffer may call after-change-functions and alike and
7615 that would means running Lisp asynchronously. */
7616 if (handling_signal)
7617 return;
7618
7619 fmt = msg = Qnil;
7620 GCPRO4 (fmt, msg, arg1, arg2);
7621
7622 args[0] = fmt = build_string (format);
7623 args[1] = arg1;
7624 args[2] = arg2;
7625 msg = Fformat (3, args);
7626
7627 len = SBYTES (msg) + 1;
7628 SAFE_ALLOCA (buffer, char *, len);
7629 bcopy (SDATA (msg), buffer, len);
7630
7631 message_dolog (buffer, len - 1, 1, 0);
7632 SAFE_FREE ();
7633
7634 UNGCPRO;
7635 }
7636
7637
7638 /* Output a newline in the *Messages* buffer if "needs" one. */
7639
7640 void
7641 message_log_maybe_newline ()
7642 {
7643 if (message_log_need_newline)
7644 message_dolog ("", 0, 1, 0);
7645 }
7646
7647
7648 /* Add a string M of length NBYTES to the message log, optionally
7649 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7650 nonzero, means interpret the contents of M as multibyte. This
7651 function calls low-level routines in order to bypass text property
7652 hooks, etc. which might not be safe to run.
7653
7654 This may GC (insert may run before/after change hooks),
7655 so the buffer M must NOT point to a Lisp string. */
7656
7657 void
7658 message_dolog (m, nbytes, nlflag, multibyte)
7659 const char *m;
7660 int nbytes, nlflag, multibyte;
7661 {
7662 if (!NILP (Vmemory_full))
7663 return;
7664
7665 if (!NILP (Vmessage_log_max))
7666 {
7667 struct buffer *oldbuf;
7668 Lisp_Object oldpoint, oldbegv, oldzv;
7669 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7670 int point_at_end = 0;
7671 int zv_at_end = 0;
7672 Lisp_Object old_deactivate_mark, tem;
7673 struct gcpro gcpro1;
7674
7675 old_deactivate_mark = Vdeactivate_mark;
7676 oldbuf = current_buffer;
7677 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7678 current_buffer->undo_list = Qt;
7679
7680 oldpoint = message_dolog_marker1;
7681 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7682 oldbegv = message_dolog_marker2;
7683 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7684 oldzv = message_dolog_marker3;
7685 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7686 GCPRO1 (old_deactivate_mark);
7687
7688 if (PT == Z)
7689 point_at_end = 1;
7690 if (ZV == Z)
7691 zv_at_end = 1;
7692
7693 BEGV = BEG;
7694 BEGV_BYTE = BEG_BYTE;
7695 ZV = Z;
7696 ZV_BYTE = Z_BYTE;
7697 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7698
7699 /* Insert the string--maybe converting multibyte to single byte
7700 or vice versa, so that all the text fits the buffer. */
7701 if (multibyte
7702 && NILP (current_buffer->enable_multibyte_characters))
7703 {
7704 int i, c, char_bytes;
7705 unsigned char work[1];
7706
7707 /* Convert a multibyte string to single-byte
7708 for the *Message* buffer. */
7709 for (i = 0; i < nbytes; i += char_bytes)
7710 {
7711 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7712 work[0] = (ASCII_CHAR_P (c)
7713 ? c
7714 : multibyte_char_to_unibyte (c, Qnil));
7715 insert_1_both (work, 1, 1, 1, 0, 0);
7716 }
7717 }
7718 else if (! multibyte
7719 && ! NILP (current_buffer->enable_multibyte_characters))
7720 {
7721 int i, c, char_bytes;
7722 unsigned char *msg = (unsigned char *) m;
7723 unsigned char str[MAX_MULTIBYTE_LENGTH];
7724 /* Convert a single-byte string to multibyte
7725 for the *Message* buffer. */
7726 for (i = 0; i < nbytes; i++)
7727 {
7728 c = msg[i];
7729 c = unibyte_char_to_multibyte (c);
7730 char_bytes = CHAR_STRING (c, str);
7731 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7732 }
7733 }
7734 else if (nbytes)
7735 insert_1 (m, nbytes, 1, 0, 0);
7736
7737 if (nlflag)
7738 {
7739 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7740 insert_1 ("\n", 1, 1, 0, 0);
7741
7742 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7743 this_bol = PT;
7744 this_bol_byte = PT_BYTE;
7745
7746 /* See if this line duplicates the previous one.
7747 If so, combine duplicates. */
7748 if (this_bol > BEG)
7749 {
7750 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7751 prev_bol = PT;
7752 prev_bol_byte = PT_BYTE;
7753
7754 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7755 this_bol, this_bol_byte);
7756 if (dup)
7757 {
7758 del_range_both (prev_bol, prev_bol_byte,
7759 this_bol, this_bol_byte, 0);
7760 if (dup > 1)
7761 {
7762 char dupstr[40];
7763 int duplen;
7764
7765 /* If you change this format, don't forget to also
7766 change message_log_check_duplicate. */
7767 sprintf (dupstr, " [%d times]", dup);
7768 duplen = strlen (dupstr);
7769 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7770 insert_1 (dupstr, duplen, 1, 0, 1);
7771 }
7772 }
7773 }
7774
7775 /* If we have more than the desired maximum number of lines
7776 in the *Messages* buffer now, delete the oldest ones.
7777 This is safe because we don't have undo in this buffer. */
7778
7779 if (NATNUMP (Vmessage_log_max))
7780 {
7781 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7782 -XFASTINT (Vmessage_log_max) - 1, 0);
7783 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7784 }
7785 }
7786 BEGV = XMARKER (oldbegv)->charpos;
7787 BEGV_BYTE = marker_byte_position (oldbegv);
7788
7789 if (zv_at_end)
7790 {
7791 ZV = Z;
7792 ZV_BYTE = Z_BYTE;
7793 }
7794 else
7795 {
7796 ZV = XMARKER (oldzv)->charpos;
7797 ZV_BYTE = marker_byte_position (oldzv);
7798 }
7799
7800 if (point_at_end)
7801 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7802 else
7803 /* We can't do Fgoto_char (oldpoint) because it will run some
7804 Lisp code. */
7805 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7806 XMARKER (oldpoint)->bytepos);
7807
7808 UNGCPRO;
7809 unchain_marker (XMARKER (oldpoint));
7810 unchain_marker (XMARKER (oldbegv));
7811 unchain_marker (XMARKER (oldzv));
7812
7813 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7814 set_buffer_internal (oldbuf);
7815 if (NILP (tem))
7816 windows_or_buffers_changed = old_windows_or_buffers_changed;
7817 message_log_need_newline = !nlflag;
7818 Vdeactivate_mark = old_deactivate_mark;
7819 }
7820 }
7821
7822
7823 /* We are at the end of the buffer after just having inserted a newline.
7824 (Note: We depend on the fact we won't be crossing the gap.)
7825 Check to see if the most recent message looks a lot like the previous one.
7826 Return 0 if different, 1 if the new one should just replace it, or a
7827 value N > 1 if we should also append " [N times]". */
7828
7829 static int
7830 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7831 int prev_bol, this_bol;
7832 int prev_bol_byte, this_bol_byte;
7833 {
7834 int i;
7835 int len = Z_BYTE - 1 - this_bol_byte;
7836 int seen_dots = 0;
7837 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7838 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7839
7840 for (i = 0; i < len; i++)
7841 {
7842 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7843 seen_dots = 1;
7844 if (p1[i] != p2[i])
7845 return seen_dots;
7846 }
7847 p1 += len;
7848 if (*p1 == '\n')
7849 return 2;
7850 if (*p1++ == ' ' && *p1++ == '[')
7851 {
7852 int n = 0;
7853 while (*p1 >= '0' && *p1 <= '9')
7854 n = n * 10 + *p1++ - '0';
7855 if (strncmp (p1, " times]\n", 8) == 0)
7856 return n+1;
7857 }
7858 return 0;
7859 }
7860 \f
7861
7862 /* Display an echo area message M with a specified length of NBYTES
7863 bytes. The string may include null characters. If M is 0, clear
7864 out any existing message, and let the mini-buffer text show
7865 through.
7866
7867 This may GC, so the buffer M must NOT point to a Lisp string. */
7868
7869 void
7870 message2 (m, nbytes, multibyte)
7871 const char *m;
7872 int nbytes;
7873 int multibyte;
7874 {
7875 /* First flush out any partial line written with print. */
7876 message_log_maybe_newline ();
7877 if (m)
7878 message_dolog (m, nbytes, 1, multibyte);
7879 message2_nolog (m, nbytes, multibyte);
7880 }
7881
7882
7883 /* The non-logging counterpart of message2. */
7884
7885 void
7886 message2_nolog (m, nbytes, multibyte)
7887 const char *m;
7888 int nbytes, multibyte;
7889 {
7890 struct frame *sf = SELECTED_FRAME ();
7891 message_enable_multibyte = multibyte;
7892
7893 if (noninteractive)
7894 {
7895 if (noninteractive_need_newline)
7896 putc ('\n', stderr);
7897 noninteractive_need_newline = 0;
7898 if (m)
7899 fwrite (m, nbytes, 1, stderr);
7900 if (cursor_in_echo_area == 0)
7901 fprintf (stderr, "\n");
7902 fflush (stderr);
7903 }
7904 /* A null message buffer means that the frame hasn't really been
7905 initialized yet. Error messages get reported properly by
7906 cmd_error, so this must be just an informative message; toss it. */
7907 else if (INTERACTIVE
7908 && sf->glyphs_initialized_p
7909 && FRAME_MESSAGE_BUF (sf))
7910 {
7911 Lisp_Object mini_window;
7912 struct frame *f;
7913
7914 /* Get the frame containing the mini-buffer
7915 that the selected frame is using. */
7916 mini_window = FRAME_MINIBUF_WINDOW (sf);
7917 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7918
7919 FRAME_SAMPLE_VISIBILITY (f);
7920 if (FRAME_VISIBLE_P (sf)
7921 && ! FRAME_VISIBLE_P (f))
7922 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7923
7924 if (m)
7925 {
7926 set_message (m, Qnil, nbytes, multibyte);
7927 if (minibuffer_auto_raise)
7928 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7929 }
7930 else
7931 clear_message (1, 1);
7932
7933 do_pending_window_change (0);
7934 echo_area_display (1);
7935 do_pending_window_change (0);
7936 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7937 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7938 }
7939 }
7940
7941
7942 /* Display an echo area message M with a specified length of NBYTES
7943 bytes. The string may include null characters. If M is not a
7944 string, clear out any existing message, and let the mini-buffer
7945 text show through.
7946
7947 This function cancels echoing. */
7948
7949 void
7950 message3 (m, nbytes, multibyte)
7951 Lisp_Object m;
7952 int nbytes;
7953 int multibyte;
7954 {
7955 struct gcpro gcpro1;
7956
7957 GCPRO1 (m);
7958 clear_message (1,1);
7959 cancel_echoing ();
7960
7961 /* First flush out any partial line written with print. */
7962 message_log_maybe_newline ();
7963 if (STRINGP (m))
7964 {
7965 char *buffer;
7966 USE_SAFE_ALLOCA;
7967
7968 SAFE_ALLOCA (buffer, char *, nbytes);
7969 bcopy (SDATA (m), buffer, nbytes);
7970 message_dolog (buffer, nbytes, 1, multibyte);
7971 SAFE_FREE ();
7972 }
7973 message3_nolog (m, nbytes, multibyte);
7974
7975 UNGCPRO;
7976 }
7977
7978
7979 /* The non-logging version of message3.
7980 This does not cancel echoing, because it is used for echoing.
7981 Perhaps we need to make a separate function for echoing
7982 and make this cancel echoing. */
7983
7984 void
7985 message3_nolog (m, nbytes, multibyte)
7986 Lisp_Object m;
7987 int nbytes, multibyte;
7988 {
7989 struct frame *sf = SELECTED_FRAME ();
7990 message_enable_multibyte = multibyte;
7991
7992 if (noninteractive)
7993 {
7994 if (noninteractive_need_newline)
7995 putc ('\n', stderr);
7996 noninteractive_need_newline = 0;
7997 if (STRINGP (m))
7998 fwrite (SDATA (m), nbytes, 1, stderr);
7999 if (cursor_in_echo_area == 0)
8000 fprintf (stderr, "\n");
8001 fflush (stderr);
8002 }
8003 /* A null message buffer means that the frame hasn't really been
8004 initialized yet. Error messages get reported properly by
8005 cmd_error, so this must be just an informative message; toss it. */
8006 else if (INTERACTIVE
8007 && sf->glyphs_initialized_p
8008 && FRAME_MESSAGE_BUF (sf))
8009 {
8010 Lisp_Object mini_window;
8011 Lisp_Object frame;
8012 struct frame *f;
8013
8014 /* Get the frame containing the mini-buffer
8015 that the selected frame is using. */
8016 mini_window = FRAME_MINIBUF_WINDOW (sf);
8017 frame = XWINDOW (mini_window)->frame;
8018 f = XFRAME (frame);
8019
8020 FRAME_SAMPLE_VISIBILITY (f);
8021 if (FRAME_VISIBLE_P (sf)
8022 && !FRAME_VISIBLE_P (f))
8023 Fmake_frame_visible (frame);
8024
8025 if (STRINGP (m) && SCHARS (m) > 0)
8026 {
8027 set_message (NULL, m, nbytes, multibyte);
8028 if (minibuffer_auto_raise)
8029 Fraise_frame (frame);
8030 /* Assume we are not echoing.
8031 (If we are, echo_now will override this.) */
8032 echo_message_buffer = Qnil;
8033 }
8034 else
8035 clear_message (1, 1);
8036
8037 do_pending_window_change (0);
8038 echo_area_display (1);
8039 do_pending_window_change (0);
8040 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8041 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8042 }
8043 }
8044
8045
8046 /* Display a null-terminated echo area message M. If M is 0, clear
8047 out any existing message, and let the mini-buffer text show through.
8048
8049 The buffer M must continue to exist until after the echo area gets
8050 cleared or some other message gets displayed there. Do not pass
8051 text that is stored in a Lisp string. Do not pass text in a buffer
8052 that was alloca'd. */
8053
8054 void
8055 message1 (m)
8056 char *m;
8057 {
8058 message2 (m, (m ? strlen (m) : 0), 0);
8059 }
8060
8061
8062 /* The non-logging counterpart of message1. */
8063
8064 void
8065 message1_nolog (m)
8066 char *m;
8067 {
8068 message2_nolog (m, (m ? strlen (m) : 0), 0);
8069 }
8070
8071 /* Display a message M which contains a single %s
8072 which gets replaced with STRING. */
8073
8074 void
8075 message_with_string (m, string, log)
8076 char *m;
8077 Lisp_Object string;
8078 int log;
8079 {
8080 CHECK_STRING (string);
8081
8082 if (noninteractive)
8083 {
8084 if (m)
8085 {
8086 if (noninteractive_need_newline)
8087 putc ('\n', stderr);
8088 noninteractive_need_newline = 0;
8089 fprintf (stderr, m, SDATA (string));
8090 if (cursor_in_echo_area == 0)
8091 fprintf (stderr, "\n");
8092 fflush (stderr);
8093 }
8094 }
8095 else if (INTERACTIVE)
8096 {
8097 /* The frame whose minibuffer we're going to display the message on.
8098 It may be larger than the selected frame, so we need
8099 to use its buffer, not the selected frame's buffer. */
8100 Lisp_Object mini_window;
8101 struct frame *f, *sf = SELECTED_FRAME ();
8102
8103 /* Get the frame containing the minibuffer
8104 that the selected frame is using. */
8105 mini_window = FRAME_MINIBUF_WINDOW (sf);
8106 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8107
8108 /* A null message buffer means that the frame hasn't really been
8109 initialized yet. Error messages get reported properly by
8110 cmd_error, so this must be just an informative message; toss it. */
8111 if (FRAME_MESSAGE_BUF (f))
8112 {
8113 Lisp_Object args[2], message;
8114 struct gcpro gcpro1, gcpro2;
8115
8116 args[0] = build_string (m);
8117 args[1] = message = string;
8118 GCPRO2 (args[0], message);
8119 gcpro1.nvars = 2;
8120
8121 message = Fformat (2, args);
8122
8123 if (log)
8124 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8125 else
8126 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8127
8128 UNGCPRO;
8129
8130 /* Print should start at the beginning of the message
8131 buffer next time. */
8132 message_buf_print = 0;
8133 }
8134 }
8135 }
8136
8137
8138 /* Dump an informative message to the minibuf. If M is 0, clear out
8139 any existing message, and let the mini-buffer text show through. */
8140
8141 /* VARARGS 1 */
8142 void
8143 message (m, a1, a2, a3)
8144 char *m;
8145 EMACS_INT a1, a2, a3;
8146 {
8147 if (noninteractive)
8148 {
8149 if (m)
8150 {
8151 if (noninteractive_need_newline)
8152 putc ('\n', stderr);
8153 noninteractive_need_newline = 0;
8154 fprintf (stderr, m, a1, a2, a3);
8155 if (cursor_in_echo_area == 0)
8156 fprintf (stderr, "\n");
8157 fflush (stderr);
8158 }
8159 }
8160 else if (INTERACTIVE)
8161 {
8162 /* The frame whose mini-buffer we're going to display the message
8163 on. It may be larger than the selected frame, so we need to
8164 use its buffer, not the selected frame's buffer. */
8165 Lisp_Object mini_window;
8166 struct frame *f, *sf = SELECTED_FRAME ();
8167
8168 /* Get the frame containing the mini-buffer
8169 that the selected frame is using. */
8170 mini_window = FRAME_MINIBUF_WINDOW (sf);
8171 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8172
8173 /* A null message buffer means that the frame hasn't really been
8174 initialized yet. Error messages get reported properly by
8175 cmd_error, so this must be just an informative message; toss
8176 it. */
8177 if (FRAME_MESSAGE_BUF (f))
8178 {
8179 if (m)
8180 {
8181 int len;
8182 #ifdef NO_ARG_ARRAY
8183 char *a[3];
8184 a[0] = (char *) a1;
8185 a[1] = (char *) a2;
8186 a[2] = (char *) a3;
8187
8188 len = doprnt (FRAME_MESSAGE_BUF (f),
8189 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8190 #else
8191 len = doprnt (FRAME_MESSAGE_BUF (f),
8192 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8193 (char **) &a1);
8194 #endif /* NO_ARG_ARRAY */
8195
8196 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8197 }
8198 else
8199 message1 (0);
8200
8201 /* Print should start at the beginning of the message
8202 buffer next time. */
8203 message_buf_print = 0;
8204 }
8205 }
8206 }
8207
8208
8209 /* The non-logging version of message. */
8210
8211 void
8212 message_nolog (m, a1, a2, a3)
8213 char *m;
8214 EMACS_INT a1, a2, a3;
8215 {
8216 Lisp_Object old_log_max;
8217 old_log_max = Vmessage_log_max;
8218 Vmessage_log_max = Qnil;
8219 message (m, a1, a2, a3);
8220 Vmessage_log_max = old_log_max;
8221 }
8222
8223
8224 /* Display the current message in the current mini-buffer. This is
8225 only called from error handlers in process.c, and is not time
8226 critical. */
8227
8228 void
8229 update_echo_area ()
8230 {
8231 if (!NILP (echo_area_buffer[0]))
8232 {
8233 Lisp_Object string;
8234 string = Fcurrent_message ();
8235 message3 (string, SBYTES (string),
8236 !NILP (current_buffer->enable_multibyte_characters));
8237 }
8238 }
8239
8240
8241 /* Make sure echo area buffers in `echo_buffers' are live.
8242 If they aren't, make new ones. */
8243
8244 static void
8245 ensure_echo_area_buffers ()
8246 {
8247 int i;
8248
8249 for (i = 0; i < 2; ++i)
8250 if (!BUFFERP (echo_buffer[i])
8251 || NILP (XBUFFER (echo_buffer[i])->name))
8252 {
8253 char name[30];
8254 Lisp_Object old_buffer;
8255 int j;
8256
8257 old_buffer = echo_buffer[i];
8258 sprintf (name, " *Echo Area %d*", i);
8259 echo_buffer[i] = Fget_buffer_create (build_string (name));
8260 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8261
8262 for (j = 0; j < 2; ++j)
8263 if (EQ (old_buffer, echo_area_buffer[j]))
8264 echo_area_buffer[j] = echo_buffer[i];
8265 }
8266 }
8267
8268
8269 /* Call FN with args A1..A4 with either the current or last displayed
8270 echo_area_buffer as current buffer.
8271
8272 WHICH zero means use the current message buffer
8273 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8274 from echo_buffer[] and clear it.
8275
8276 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8277 suitable buffer from echo_buffer[] and clear it.
8278
8279 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8280 that the current message becomes the last displayed one, make
8281 choose a suitable buffer for echo_area_buffer[0], and clear it.
8282
8283 Value is what FN returns. */
8284
8285 static int
8286 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8287 struct window *w;
8288 int which;
8289 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8290 EMACS_INT a1;
8291 Lisp_Object a2;
8292 EMACS_INT a3, a4;
8293 {
8294 Lisp_Object buffer;
8295 int this_one, the_other, clear_buffer_p, rc;
8296 int count = SPECPDL_INDEX ();
8297
8298 /* If buffers aren't live, make new ones. */
8299 ensure_echo_area_buffers ();
8300
8301 clear_buffer_p = 0;
8302
8303 if (which == 0)
8304 this_one = 0, the_other = 1;
8305 else if (which > 0)
8306 this_one = 1, the_other = 0;
8307 else
8308 {
8309 this_one = 0, the_other = 1;
8310 clear_buffer_p = 1;
8311
8312 /* We need a fresh one in case the current echo buffer equals
8313 the one containing the last displayed echo area message. */
8314 if (!NILP (echo_area_buffer[this_one])
8315 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8316 echo_area_buffer[this_one] = Qnil;
8317 }
8318
8319 /* Choose a suitable buffer from echo_buffer[] is we don't
8320 have one. */
8321 if (NILP (echo_area_buffer[this_one]))
8322 {
8323 echo_area_buffer[this_one]
8324 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8325 ? echo_buffer[the_other]
8326 : echo_buffer[this_one]);
8327 clear_buffer_p = 1;
8328 }
8329
8330 buffer = echo_area_buffer[this_one];
8331
8332 /* Don't get confused by reusing the buffer used for echoing
8333 for a different purpose. */
8334 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8335 cancel_echoing ();
8336
8337 record_unwind_protect (unwind_with_echo_area_buffer,
8338 with_echo_area_buffer_unwind_data (w));
8339
8340 /* Make the echo area buffer current. Note that for display
8341 purposes, it is not necessary that the displayed window's buffer
8342 == current_buffer, except for text property lookup. So, let's
8343 only set that buffer temporarily here without doing a full
8344 Fset_window_buffer. We must also change w->pointm, though,
8345 because otherwise an assertions in unshow_buffer fails, and Emacs
8346 aborts. */
8347 set_buffer_internal_1 (XBUFFER (buffer));
8348 if (w)
8349 {
8350 w->buffer = buffer;
8351 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8352 }
8353
8354 current_buffer->undo_list = Qt;
8355 current_buffer->read_only = Qnil;
8356 specbind (Qinhibit_read_only, Qt);
8357 specbind (Qinhibit_modification_hooks, Qt);
8358
8359 if (clear_buffer_p && Z > BEG)
8360 del_range (BEG, Z);
8361
8362 xassert (BEGV >= BEG);
8363 xassert (ZV <= Z && ZV >= BEGV);
8364
8365 rc = fn (a1, a2, a3, a4);
8366
8367 xassert (BEGV >= BEG);
8368 xassert (ZV <= Z && ZV >= BEGV);
8369
8370 unbind_to (count, Qnil);
8371 return rc;
8372 }
8373
8374
8375 /* Save state that should be preserved around the call to the function
8376 FN called in with_echo_area_buffer. */
8377
8378 static Lisp_Object
8379 with_echo_area_buffer_unwind_data (w)
8380 struct window *w;
8381 {
8382 int i = 0;
8383 Lisp_Object vector, tmp;
8384
8385 /* Reduce consing by keeping one vector in
8386 Vwith_echo_area_save_vector. */
8387 vector = Vwith_echo_area_save_vector;
8388 Vwith_echo_area_save_vector = Qnil;
8389
8390 if (NILP (vector))
8391 vector = Fmake_vector (make_number (7), Qnil);
8392
8393 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8394 ASET (vector, i, Vdeactivate_mark); ++i;
8395 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8396
8397 if (w)
8398 {
8399 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8400 ASET (vector, i, w->buffer); ++i;
8401 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8402 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8403 }
8404 else
8405 {
8406 int end = i + 4;
8407 for (; i < end; ++i)
8408 ASET (vector, i, Qnil);
8409 }
8410
8411 xassert (i == ASIZE (vector));
8412 return vector;
8413 }
8414
8415
8416 /* Restore global state from VECTOR which was created by
8417 with_echo_area_buffer_unwind_data. */
8418
8419 static Lisp_Object
8420 unwind_with_echo_area_buffer (vector)
8421 Lisp_Object vector;
8422 {
8423 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8424 Vdeactivate_mark = AREF (vector, 1);
8425 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8426
8427 if (WINDOWP (AREF (vector, 3)))
8428 {
8429 struct window *w;
8430 Lisp_Object buffer, charpos, bytepos;
8431
8432 w = XWINDOW (AREF (vector, 3));
8433 buffer = AREF (vector, 4);
8434 charpos = AREF (vector, 5);
8435 bytepos = AREF (vector, 6);
8436
8437 w->buffer = buffer;
8438 set_marker_both (w->pointm, buffer,
8439 XFASTINT (charpos), XFASTINT (bytepos));
8440 }
8441
8442 Vwith_echo_area_save_vector = vector;
8443 return Qnil;
8444 }
8445
8446
8447 /* Set up the echo area for use by print functions. MULTIBYTE_P
8448 non-zero means we will print multibyte. */
8449
8450 void
8451 setup_echo_area_for_printing (multibyte_p)
8452 int multibyte_p;
8453 {
8454 /* If we can't find an echo area any more, exit. */
8455 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8456 Fkill_emacs (Qnil);
8457
8458 ensure_echo_area_buffers ();
8459
8460 if (!message_buf_print)
8461 {
8462 /* A message has been output since the last time we printed.
8463 Choose a fresh echo area buffer. */
8464 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8465 echo_area_buffer[0] = echo_buffer[1];
8466 else
8467 echo_area_buffer[0] = echo_buffer[0];
8468
8469 /* Switch to that buffer and clear it. */
8470 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8471 current_buffer->truncate_lines = Qnil;
8472
8473 if (Z > BEG)
8474 {
8475 int count = SPECPDL_INDEX ();
8476 specbind (Qinhibit_read_only, Qt);
8477 /* Note that undo recording is always disabled. */
8478 del_range (BEG, Z);
8479 unbind_to (count, Qnil);
8480 }
8481 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8482
8483 /* Set up the buffer for the multibyteness we need. */
8484 if (multibyte_p
8485 != !NILP (current_buffer->enable_multibyte_characters))
8486 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8487
8488 /* Raise the frame containing the echo area. */
8489 if (minibuffer_auto_raise)
8490 {
8491 struct frame *sf = SELECTED_FRAME ();
8492 Lisp_Object mini_window;
8493 mini_window = FRAME_MINIBUF_WINDOW (sf);
8494 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8495 }
8496
8497 message_log_maybe_newline ();
8498 message_buf_print = 1;
8499 }
8500 else
8501 {
8502 if (NILP (echo_area_buffer[0]))
8503 {
8504 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8505 echo_area_buffer[0] = echo_buffer[1];
8506 else
8507 echo_area_buffer[0] = echo_buffer[0];
8508 }
8509
8510 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8511 {
8512 /* Someone switched buffers between print requests. */
8513 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8514 current_buffer->truncate_lines = Qnil;
8515 }
8516 }
8517 }
8518
8519
8520 /* Display an echo area message in window W. Value is non-zero if W's
8521 height is changed. If display_last_displayed_message_p is
8522 non-zero, display the message that was last displayed, otherwise
8523 display the current message. */
8524
8525 static int
8526 display_echo_area (w)
8527 struct window *w;
8528 {
8529 int i, no_message_p, window_height_changed_p, count;
8530
8531 /* Temporarily disable garbage collections while displaying the echo
8532 area. This is done because a GC can print a message itself.
8533 That message would modify the echo area buffer's contents while a
8534 redisplay of the buffer is going on, and seriously confuse
8535 redisplay. */
8536 count = inhibit_garbage_collection ();
8537
8538 /* If there is no message, we must call display_echo_area_1
8539 nevertheless because it resizes the window. But we will have to
8540 reset the echo_area_buffer in question to nil at the end because
8541 with_echo_area_buffer will sets it to an empty buffer. */
8542 i = display_last_displayed_message_p ? 1 : 0;
8543 no_message_p = NILP (echo_area_buffer[i]);
8544
8545 window_height_changed_p
8546 = with_echo_area_buffer (w, display_last_displayed_message_p,
8547 display_echo_area_1,
8548 (EMACS_INT) w, Qnil, 0, 0);
8549
8550 if (no_message_p)
8551 echo_area_buffer[i] = Qnil;
8552
8553 unbind_to (count, Qnil);
8554 return window_height_changed_p;
8555 }
8556
8557
8558 /* Helper for display_echo_area. Display the current buffer which
8559 contains the current echo area message in window W, a mini-window,
8560 a pointer to which is passed in A1. A2..A4 are currently not used.
8561 Change the height of W so that all of the message is displayed.
8562 Value is non-zero if height of W was changed. */
8563
8564 static int
8565 display_echo_area_1 (a1, a2, a3, a4)
8566 EMACS_INT a1;
8567 Lisp_Object a2;
8568 EMACS_INT a3, a4;
8569 {
8570 struct window *w = (struct window *) a1;
8571 Lisp_Object window;
8572 struct text_pos start;
8573 int window_height_changed_p = 0;
8574
8575 /* Do this before displaying, so that we have a large enough glyph
8576 matrix for the display. If we can't get enough space for the
8577 whole text, display the last N lines. That works by setting w->start. */
8578 window_height_changed_p = resize_mini_window (w, 0);
8579
8580 /* Use the starting position chosen by resize_mini_window. */
8581 SET_TEXT_POS_FROM_MARKER (start, w->start);
8582
8583 /* Display. */
8584 clear_glyph_matrix (w->desired_matrix);
8585 XSETWINDOW (window, w);
8586 try_window (window, start, 0);
8587
8588 return window_height_changed_p;
8589 }
8590
8591
8592 /* Resize the echo area window to exactly the size needed for the
8593 currently displayed message, if there is one. If a mini-buffer
8594 is active, don't shrink it. */
8595
8596 void
8597 resize_echo_area_exactly ()
8598 {
8599 if (BUFFERP (echo_area_buffer[0])
8600 && WINDOWP (echo_area_window))
8601 {
8602 struct window *w = XWINDOW (echo_area_window);
8603 int resized_p;
8604 Lisp_Object resize_exactly;
8605
8606 if (minibuf_level == 0)
8607 resize_exactly = Qt;
8608 else
8609 resize_exactly = Qnil;
8610
8611 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8612 (EMACS_INT) w, resize_exactly, 0, 0);
8613 if (resized_p)
8614 {
8615 ++windows_or_buffers_changed;
8616 ++update_mode_lines;
8617 redisplay_internal (0);
8618 }
8619 }
8620 }
8621
8622
8623 /* Callback function for with_echo_area_buffer, when used from
8624 resize_echo_area_exactly. A1 contains a pointer to the window to
8625 resize, EXACTLY non-nil means resize the mini-window exactly to the
8626 size of the text displayed. A3 and A4 are not used. Value is what
8627 resize_mini_window returns. */
8628
8629 static int
8630 resize_mini_window_1 (a1, exactly, a3, a4)
8631 EMACS_INT a1;
8632 Lisp_Object exactly;
8633 EMACS_INT a3, a4;
8634 {
8635 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8636 }
8637
8638
8639 /* Resize mini-window W to fit the size of its contents. EXACT_P
8640 means size the window exactly to the size needed. Otherwise, it's
8641 only enlarged until W's buffer is empty.
8642
8643 Set W->start to the right place to begin display. If the whole
8644 contents fit, start at the beginning. Otherwise, start so as
8645 to make the end of the contents appear. This is particularly
8646 important for y-or-n-p, but seems desirable generally.
8647
8648 Value is non-zero if the window height has been changed. */
8649
8650 int
8651 resize_mini_window (w, exact_p)
8652 struct window *w;
8653 int exact_p;
8654 {
8655 struct frame *f = XFRAME (w->frame);
8656 int window_height_changed_p = 0;
8657
8658 xassert (MINI_WINDOW_P (w));
8659
8660 /* By default, start display at the beginning. */
8661 set_marker_both (w->start, w->buffer,
8662 BUF_BEGV (XBUFFER (w->buffer)),
8663 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8664
8665 /* Don't resize windows while redisplaying a window; it would
8666 confuse redisplay functions when the size of the window they are
8667 displaying changes from under them. Such a resizing can happen,
8668 for instance, when which-func prints a long message while
8669 we are running fontification-functions. We're running these
8670 functions with safe_call which binds inhibit-redisplay to t. */
8671 if (!NILP (Vinhibit_redisplay))
8672 return 0;
8673
8674 /* Nil means don't try to resize. */
8675 if (NILP (Vresize_mini_windows)
8676 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8677 return 0;
8678
8679 if (!FRAME_MINIBUF_ONLY_P (f))
8680 {
8681 struct it it;
8682 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8683 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8684 int height, max_height;
8685 int unit = FRAME_LINE_HEIGHT (f);
8686 struct text_pos start;
8687 struct buffer *old_current_buffer = NULL;
8688
8689 if (current_buffer != XBUFFER (w->buffer))
8690 {
8691 old_current_buffer = current_buffer;
8692 set_buffer_internal (XBUFFER (w->buffer));
8693 }
8694
8695 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8696
8697 /* Compute the max. number of lines specified by the user. */
8698 if (FLOATP (Vmax_mini_window_height))
8699 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8700 else if (INTEGERP (Vmax_mini_window_height))
8701 max_height = XINT (Vmax_mini_window_height);
8702 else
8703 max_height = total_height / 4;
8704
8705 /* Correct that max. height if it's bogus. */
8706 max_height = max (1, max_height);
8707 max_height = min (total_height, max_height);
8708
8709 /* Find out the height of the text in the window. */
8710 if (it.line_wrap == TRUNCATE)
8711 height = 1;
8712 else
8713 {
8714 last_height = 0;
8715 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8716 if (it.max_ascent == 0 && it.max_descent == 0)
8717 height = it.current_y + last_height;
8718 else
8719 height = it.current_y + it.max_ascent + it.max_descent;
8720 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8721 height = (height + unit - 1) / unit;
8722 }
8723
8724 /* Compute a suitable window start. */
8725 if (height > max_height)
8726 {
8727 height = max_height;
8728 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8729 move_it_vertically_backward (&it, (height - 1) * unit);
8730 start = it.current.pos;
8731 }
8732 else
8733 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8734 SET_MARKER_FROM_TEXT_POS (w->start, start);
8735
8736 if (EQ (Vresize_mini_windows, Qgrow_only))
8737 {
8738 /* Let it grow only, until we display an empty message, in which
8739 case the window shrinks again. */
8740 if (height > WINDOW_TOTAL_LINES (w))
8741 {
8742 int old_height = WINDOW_TOTAL_LINES (w);
8743 freeze_window_starts (f, 1);
8744 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8745 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8746 }
8747 else if (height < WINDOW_TOTAL_LINES (w)
8748 && (exact_p || BEGV == ZV))
8749 {
8750 int old_height = WINDOW_TOTAL_LINES (w);
8751 freeze_window_starts (f, 0);
8752 shrink_mini_window (w);
8753 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8754 }
8755 }
8756 else
8757 {
8758 /* Always resize to exact size needed. */
8759 if (height > WINDOW_TOTAL_LINES (w))
8760 {
8761 int old_height = WINDOW_TOTAL_LINES (w);
8762 freeze_window_starts (f, 1);
8763 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8764 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8765 }
8766 else if (height < WINDOW_TOTAL_LINES (w))
8767 {
8768 int old_height = WINDOW_TOTAL_LINES (w);
8769 freeze_window_starts (f, 0);
8770 shrink_mini_window (w);
8771
8772 if (height)
8773 {
8774 freeze_window_starts (f, 1);
8775 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8776 }
8777
8778 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8779 }
8780 }
8781
8782 if (old_current_buffer)
8783 set_buffer_internal (old_current_buffer);
8784 }
8785
8786 return window_height_changed_p;
8787 }
8788
8789
8790 /* Value is the current message, a string, or nil if there is no
8791 current message. */
8792
8793 Lisp_Object
8794 current_message ()
8795 {
8796 Lisp_Object msg;
8797
8798 if (!BUFFERP (echo_area_buffer[0]))
8799 msg = Qnil;
8800 else
8801 {
8802 with_echo_area_buffer (0, 0, current_message_1,
8803 (EMACS_INT) &msg, Qnil, 0, 0);
8804 if (NILP (msg))
8805 echo_area_buffer[0] = Qnil;
8806 }
8807
8808 return msg;
8809 }
8810
8811
8812 static int
8813 current_message_1 (a1, a2, a3, a4)
8814 EMACS_INT a1;
8815 Lisp_Object a2;
8816 EMACS_INT a3, a4;
8817 {
8818 Lisp_Object *msg = (Lisp_Object *) a1;
8819
8820 if (Z > BEG)
8821 *msg = make_buffer_string (BEG, Z, 1);
8822 else
8823 *msg = Qnil;
8824 return 0;
8825 }
8826
8827
8828 /* Push the current message on Vmessage_stack for later restauration
8829 by restore_message. Value is non-zero if the current message isn't
8830 empty. This is a relatively infrequent operation, so it's not
8831 worth optimizing. */
8832
8833 int
8834 push_message ()
8835 {
8836 Lisp_Object msg;
8837 msg = current_message ();
8838 Vmessage_stack = Fcons (msg, Vmessage_stack);
8839 return STRINGP (msg);
8840 }
8841
8842
8843 /* Restore message display from the top of Vmessage_stack. */
8844
8845 void
8846 restore_message ()
8847 {
8848 Lisp_Object msg;
8849
8850 xassert (CONSP (Vmessage_stack));
8851 msg = XCAR (Vmessage_stack);
8852 if (STRINGP (msg))
8853 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8854 else
8855 message3_nolog (msg, 0, 0);
8856 }
8857
8858
8859 /* Handler for record_unwind_protect calling pop_message. */
8860
8861 Lisp_Object
8862 pop_message_unwind (dummy)
8863 Lisp_Object dummy;
8864 {
8865 pop_message ();
8866 return Qnil;
8867 }
8868
8869 /* Pop the top-most entry off Vmessage_stack. */
8870
8871 void
8872 pop_message ()
8873 {
8874 xassert (CONSP (Vmessage_stack));
8875 Vmessage_stack = XCDR (Vmessage_stack);
8876 }
8877
8878
8879 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8880 exits. If the stack is not empty, we have a missing pop_message
8881 somewhere. */
8882
8883 void
8884 check_message_stack ()
8885 {
8886 if (!NILP (Vmessage_stack))
8887 abort ();
8888 }
8889
8890
8891 /* Truncate to NCHARS what will be displayed in the echo area the next
8892 time we display it---but don't redisplay it now. */
8893
8894 void
8895 truncate_echo_area (nchars)
8896 int nchars;
8897 {
8898 if (nchars == 0)
8899 echo_area_buffer[0] = Qnil;
8900 /* A null message buffer means that the frame hasn't really been
8901 initialized yet. Error messages get reported properly by
8902 cmd_error, so this must be just an informative message; toss it. */
8903 else if (!noninteractive
8904 && INTERACTIVE
8905 && !NILP (echo_area_buffer[0]))
8906 {
8907 struct frame *sf = SELECTED_FRAME ();
8908 if (FRAME_MESSAGE_BUF (sf))
8909 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8910 }
8911 }
8912
8913
8914 /* Helper function for truncate_echo_area. Truncate the current
8915 message to at most NCHARS characters. */
8916
8917 static int
8918 truncate_message_1 (nchars, a2, a3, a4)
8919 EMACS_INT nchars;
8920 Lisp_Object a2;
8921 EMACS_INT a3, a4;
8922 {
8923 if (BEG + nchars < Z)
8924 del_range (BEG + nchars, Z);
8925 if (Z == BEG)
8926 echo_area_buffer[0] = Qnil;
8927 return 0;
8928 }
8929
8930
8931 /* Set the current message to a substring of S or STRING.
8932
8933 If STRING is a Lisp string, set the message to the first NBYTES
8934 bytes from STRING. NBYTES zero means use the whole string. If
8935 STRING is multibyte, the message will be displayed multibyte.
8936
8937 If S is not null, set the message to the first LEN bytes of S. LEN
8938 zero means use the whole string. MULTIBYTE_P non-zero means S is
8939 multibyte. Display the message multibyte in that case.
8940
8941 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8942 to t before calling set_message_1 (which calls insert).
8943 */
8944
8945 void
8946 set_message (s, string, nbytes, multibyte_p)
8947 const char *s;
8948 Lisp_Object string;
8949 int nbytes, multibyte_p;
8950 {
8951 message_enable_multibyte
8952 = ((s && multibyte_p)
8953 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8954
8955 with_echo_area_buffer (0, -1, set_message_1,
8956 (EMACS_INT) s, string, nbytes, multibyte_p);
8957 message_buf_print = 0;
8958 help_echo_showing_p = 0;
8959 }
8960
8961
8962 /* Helper function for set_message. Arguments have the same meaning
8963 as there, with A1 corresponding to S and A2 corresponding to STRING
8964 This function is called with the echo area buffer being
8965 current. */
8966
8967 static int
8968 set_message_1 (a1, a2, nbytes, multibyte_p)
8969 EMACS_INT a1;
8970 Lisp_Object a2;
8971 EMACS_INT nbytes, multibyte_p;
8972 {
8973 const char *s = (const char *) a1;
8974 Lisp_Object string = a2;
8975
8976 /* Change multibyteness of the echo buffer appropriately. */
8977 if (message_enable_multibyte
8978 != !NILP (current_buffer->enable_multibyte_characters))
8979 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8980
8981 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8982
8983 /* Insert new message at BEG. */
8984 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8985
8986 if (STRINGP (string))
8987 {
8988 int nchars;
8989
8990 if (nbytes == 0)
8991 nbytes = SBYTES (string);
8992 nchars = string_byte_to_char (string, nbytes);
8993
8994 /* This function takes care of single/multibyte conversion. We
8995 just have to ensure that the echo area buffer has the right
8996 setting of enable_multibyte_characters. */
8997 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8998 }
8999 else if (s)
9000 {
9001 if (nbytes == 0)
9002 nbytes = strlen (s);
9003
9004 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9005 {
9006 /* Convert from multi-byte to single-byte. */
9007 int i, c, n;
9008 unsigned char work[1];
9009
9010 /* Convert a multibyte string to single-byte. */
9011 for (i = 0; i < nbytes; i += n)
9012 {
9013 c = string_char_and_length (s + i, nbytes - i, &n);
9014 work[0] = (ASCII_CHAR_P (c)
9015 ? c
9016 : multibyte_char_to_unibyte (c, Qnil));
9017 insert_1_both (work, 1, 1, 1, 0, 0);
9018 }
9019 }
9020 else if (!multibyte_p
9021 && !NILP (current_buffer->enable_multibyte_characters))
9022 {
9023 /* Convert from single-byte to multi-byte. */
9024 int i, c, n;
9025 const unsigned char *msg = (const unsigned char *) s;
9026 unsigned char str[MAX_MULTIBYTE_LENGTH];
9027
9028 /* Convert a single-byte string to multibyte. */
9029 for (i = 0; i < nbytes; i++)
9030 {
9031 c = msg[i];
9032 c = unibyte_char_to_multibyte (c);
9033 n = CHAR_STRING (c, str);
9034 insert_1_both (str, 1, n, 1, 0, 0);
9035 }
9036 }
9037 else
9038 insert_1 (s, nbytes, 1, 0, 0);
9039 }
9040
9041 return 0;
9042 }
9043
9044
9045 /* Clear messages. CURRENT_P non-zero means clear the current
9046 message. LAST_DISPLAYED_P non-zero means clear the message
9047 last displayed. */
9048
9049 void
9050 clear_message (current_p, last_displayed_p)
9051 int current_p, last_displayed_p;
9052 {
9053 if (current_p)
9054 {
9055 echo_area_buffer[0] = Qnil;
9056 message_cleared_p = 1;
9057 }
9058
9059 if (last_displayed_p)
9060 echo_area_buffer[1] = Qnil;
9061
9062 message_buf_print = 0;
9063 }
9064
9065 /* Clear garbaged frames.
9066
9067 This function is used where the old redisplay called
9068 redraw_garbaged_frames which in turn called redraw_frame which in
9069 turn called clear_frame. The call to clear_frame was a source of
9070 flickering. I believe a clear_frame is not necessary. It should
9071 suffice in the new redisplay to invalidate all current matrices,
9072 and ensure a complete redisplay of all windows. */
9073
9074 static void
9075 clear_garbaged_frames ()
9076 {
9077 if (frame_garbaged)
9078 {
9079 Lisp_Object tail, frame;
9080 int changed_count = 0;
9081
9082 FOR_EACH_FRAME (tail, frame)
9083 {
9084 struct frame *f = XFRAME (frame);
9085
9086 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9087 {
9088 if (f->resized_p)
9089 {
9090 Fredraw_frame (frame);
9091 f->force_flush_display_p = 1;
9092 }
9093 clear_current_matrices (f);
9094 changed_count++;
9095 f->garbaged = 0;
9096 f->resized_p = 0;
9097 }
9098 }
9099
9100 frame_garbaged = 0;
9101 if (changed_count)
9102 ++windows_or_buffers_changed;
9103 }
9104 }
9105
9106
9107 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9108 is non-zero update selected_frame. Value is non-zero if the
9109 mini-windows height has been changed. */
9110
9111 static int
9112 echo_area_display (update_frame_p)
9113 int update_frame_p;
9114 {
9115 Lisp_Object mini_window;
9116 struct window *w;
9117 struct frame *f;
9118 int window_height_changed_p = 0;
9119 struct frame *sf = SELECTED_FRAME ();
9120
9121 mini_window = FRAME_MINIBUF_WINDOW (sf);
9122 w = XWINDOW (mini_window);
9123 f = XFRAME (WINDOW_FRAME (w));
9124
9125 /* Don't display if frame is invisible or not yet initialized. */
9126 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9127 return 0;
9128
9129 #ifdef HAVE_WINDOW_SYSTEM
9130 /* When Emacs starts, selected_frame may be the initial terminal
9131 frame. If we let this through, a message would be displayed on
9132 the terminal. */
9133 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9134 return 0;
9135 #endif /* HAVE_WINDOW_SYSTEM */
9136
9137 /* Redraw garbaged frames. */
9138 if (frame_garbaged)
9139 clear_garbaged_frames ();
9140
9141 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9142 {
9143 echo_area_window = mini_window;
9144 window_height_changed_p = display_echo_area (w);
9145 w->must_be_updated_p = 1;
9146
9147 /* Update the display, unless called from redisplay_internal.
9148 Also don't update the screen during redisplay itself. The
9149 update will happen at the end of redisplay, and an update
9150 here could cause confusion. */
9151 if (update_frame_p && !redisplaying_p)
9152 {
9153 int n = 0;
9154
9155 /* If the display update has been interrupted by pending
9156 input, update mode lines in the frame. Due to the
9157 pending input, it might have been that redisplay hasn't
9158 been called, so that mode lines above the echo area are
9159 garbaged. This looks odd, so we prevent it here. */
9160 if (!display_completed)
9161 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9162
9163 if (window_height_changed_p
9164 /* Don't do this if Emacs is shutting down. Redisplay
9165 needs to run hooks. */
9166 && !NILP (Vrun_hooks))
9167 {
9168 /* Must update other windows. Likewise as in other
9169 cases, don't let this update be interrupted by
9170 pending input. */
9171 int count = SPECPDL_INDEX ();
9172 specbind (Qredisplay_dont_pause, Qt);
9173 windows_or_buffers_changed = 1;
9174 redisplay_internal (0);
9175 unbind_to (count, Qnil);
9176 }
9177 else if (FRAME_WINDOW_P (f) && n == 0)
9178 {
9179 /* Window configuration is the same as before.
9180 Can do with a display update of the echo area,
9181 unless we displayed some mode lines. */
9182 update_single_window (w, 1);
9183 FRAME_RIF (f)->flush_display (f);
9184 }
9185 else
9186 update_frame (f, 1, 1);
9187
9188 /* If cursor is in the echo area, make sure that the next
9189 redisplay displays the minibuffer, so that the cursor will
9190 be replaced with what the minibuffer wants. */
9191 if (cursor_in_echo_area)
9192 ++windows_or_buffers_changed;
9193 }
9194 }
9195 else if (!EQ (mini_window, selected_window))
9196 windows_or_buffers_changed++;
9197
9198 /* Last displayed message is now the current message. */
9199 echo_area_buffer[1] = echo_area_buffer[0];
9200 /* Inform read_char that we're not echoing. */
9201 echo_message_buffer = Qnil;
9202
9203 /* Prevent redisplay optimization in redisplay_internal by resetting
9204 this_line_start_pos. This is done because the mini-buffer now
9205 displays the message instead of its buffer text. */
9206 if (EQ (mini_window, selected_window))
9207 CHARPOS (this_line_start_pos) = 0;
9208
9209 return window_height_changed_p;
9210 }
9211
9212
9213 \f
9214 /***********************************************************************
9215 Mode Lines and Frame Titles
9216 ***********************************************************************/
9217
9218 /* A buffer for constructing non-propertized mode-line strings and
9219 frame titles in it; allocated from the heap in init_xdisp and
9220 resized as needed in store_mode_line_noprop_char. */
9221
9222 static char *mode_line_noprop_buf;
9223
9224 /* The buffer's end, and a current output position in it. */
9225
9226 static char *mode_line_noprop_buf_end;
9227 static char *mode_line_noprop_ptr;
9228
9229 #define MODE_LINE_NOPROP_LEN(start) \
9230 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9231
9232 static enum {
9233 MODE_LINE_DISPLAY = 0,
9234 MODE_LINE_TITLE,
9235 MODE_LINE_NOPROP,
9236 MODE_LINE_STRING
9237 } mode_line_target;
9238
9239 /* Alist that caches the results of :propertize.
9240 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9241 static Lisp_Object mode_line_proptrans_alist;
9242
9243 /* List of strings making up the mode-line. */
9244 static Lisp_Object mode_line_string_list;
9245
9246 /* Base face property when building propertized mode line string. */
9247 static Lisp_Object mode_line_string_face;
9248 static Lisp_Object mode_line_string_face_prop;
9249
9250
9251 /* Unwind data for mode line strings */
9252
9253 static Lisp_Object Vmode_line_unwind_vector;
9254
9255 static Lisp_Object
9256 format_mode_line_unwind_data (struct buffer *obuf,
9257 Lisp_Object owin,
9258 int save_proptrans)
9259 {
9260 Lisp_Object vector, tmp;
9261
9262 /* Reduce consing by keeping one vector in
9263 Vwith_echo_area_save_vector. */
9264 vector = Vmode_line_unwind_vector;
9265 Vmode_line_unwind_vector = Qnil;
9266
9267 if (NILP (vector))
9268 vector = Fmake_vector (make_number (8), Qnil);
9269
9270 ASET (vector, 0, make_number (mode_line_target));
9271 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9272 ASET (vector, 2, mode_line_string_list);
9273 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9274 ASET (vector, 4, mode_line_string_face);
9275 ASET (vector, 5, mode_line_string_face_prop);
9276
9277 if (obuf)
9278 XSETBUFFER (tmp, obuf);
9279 else
9280 tmp = Qnil;
9281 ASET (vector, 6, tmp);
9282 ASET (vector, 7, owin);
9283
9284 return vector;
9285 }
9286
9287 static Lisp_Object
9288 unwind_format_mode_line (vector)
9289 Lisp_Object vector;
9290 {
9291 mode_line_target = XINT (AREF (vector, 0));
9292 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9293 mode_line_string_list = AREF (vector, 2);
9294 if (! EQ (AREF (vector, 3), Qt))
9295 mode_line_proptrans_alist = AREF (vector, 3);
9296 mode_line_string_face = AREF (vector, 4);
9297 mode_line_string_face_prop = AREF (vector, 5);
9298
9299 if (!NILP (AREF (vector, 7)))
9300 /* Select window before buffer, since it may change the buffer. */
9301 Fselect_window (AREF (vector, 7), Qt);
9302
9303 if (!NILP (AREF (vector, 6)))
9304 {
9305 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9306 ASET (vector, 6, Qnil);
9307 }
9308
9309 Vmode_line_unwind_vector = vector;
9310 return Qnil;
9311 }
9312
9313
9314 /* Store a single character C for the frame title in mode_line_noprop_buf.
9315 Re-allocate mode_line_noprop_buf if necessary. */
9316
9317 static void
9318 #ifdef PROTOTYPES
9319 store_mode_line_noprop_char (char c)
9320 #else
9321 store_mode_line_noprop_char (c)
9322 char c;
9323 #endif
9324 {
9325 /* If output position has reached the end of the allocated buffer,
9326 double the buffer's size. */
9327 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9328 {
9329 int len = MODE_LINE_NOPROP_LEN (0);
9330 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9331 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9332 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9333 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9334 }
9335
9336 *mode_line_noprop_ptr++ = c;
9337 }
9338
9339
9340 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9341 mode_line_noprop_ptr. STR is the string to store. Do not copy
9342 characters that yield more columns than PRECISION; PRECISION <= 0
9343 means copy the whole string. Pad with spaces until FIELD_WIDTH
9344 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9345 pad. Called from display_mode_element when it is used to build a
9346 frame title. */
9347
9348 static int
9349 store_mode_line_noprop (str, field_width, precision)
9350 const unsigned char *str;
9351 int field_width, precision;
9352 {
9353 int n = 0;
9354 int dummy, nbytes;
9355
9356 /* Copy at most PRECISION chars from STR. */
9357 nbytes = strlen (str);
9358 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9359 while (nbytes--)
9360 store_mode_line_noprop_char (*str++);
9361
9362 /* Fill up with spaces until FIELD_WIDTH reached. */
9363 while (field_width > 0
9364 && n < field_width)
9365 {
9366 store_mode_line_noprop_char (' ');
9367 ++n;
9368 }
9369
9370 return n;
9371 }
9372
9373 /***********************************************************************
9374 Frame Titles
9375 ***********************************************************************/
9376
9377 #ifdef HAVE_WINDOW_SYSTEM
9378
9379 /* Set the title of FRAME, if it has changed. The title format is
9380 Vicon_title_format if FRAME is iconified, otherwise it is
9381 frame_title_format. */
9382
9383 static void
9384 x_consider_frame_title (frame)
9385 Lisp_Object frame;
9386 {
9387 struct frame *f = XFRAME (frame);
9388
9389 if (FRAME_WINDOW_P (f)
9390 || FRAME_MINIBUF_ONLY_P (f)
9391 || f->explicit_name)
9392 {
9393 /* Do we have more than one visible frame on this X display? */
9394 Lisp_Object tail;
9395 Lisp_Object fmt;
9396 int title_start;
9397 char *title;
9398 int len;
9399 struct it it;
9400 int count = SPECPDL_INDEX ();
9401
9402 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9403 {
9404 Lisp_Object other_frame = XCAR (tail);
9405 struct frame *tf = XFRAME (other_frame);
9406
9407 if (tf != f
9408 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9409 && !FRAME_MINIBUF_ONLY_P (tf)
9410 && !EQ (other_frame, tip_frame)
9411 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9412 break;
9413 }
9414
9415 /* Set global variable indicating that multiple frames exist. */
9416 multiple_frames = CONSP (tail);
9417
9418 /* Switch to the buffer of selected window of the frame. Set up
9419 mode_line_target so that display_mode_element will output into
9420 mode_line_noprop_buf; then display the title. */
9421 record_unwind_protect (unwind_format_mode_line,
9422 format_mode_line_unwind_data
9423 (current_buffer, selected_window, 0));
9424
9425 Fselect_window (f->selected_window, Qt);
9426 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9427 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9428
9429 mode_line_target = MODE_LINE_TITLE;
9430 title_start = MODE_LINE_NOPROP_LEN (0);
9431 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9432 NULL, DEFAULT_FACE_ID);
9433 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9434 len = MODE_LINE_NOPROP_LEN (title_start);
9435 title = mode_line_noprop_buf + title_start;
9436 unbind_to (count, Qnil);
9437
9438 /* Set the title only if it's changed. This avoids consing in
9439 the common case where it hasn't. (If it turns out that we've
9440 already wasted too much time by walking through the list with
9441 display_mode_element, then we might need to optimize at a
9442 higher level than this.) */
9443 if (! STRINGP (f->name)
9444 || SBYTES (f->name) != len
9445 || bcmp (title, SDATA (f->name), len) != 0)
9446 x_implicitly_set_name (f, make_string (title, len), Qnil);
9447 }
9448 }
9449
9450 #endif /* not HAVE_WINDOW_SYSTEM */
9451
9452
9453
9454 \f
9455 /***********************************************************************
9456 Menu Bars
9457 ***********************************************************************/
9458
9459
9460 /* Prepare for redisplay by updating menu-bar item lists when
9461 appropriate. This can call eval. */
9462
9463 void
9464 prepare_menu_bars ()
9465 {
9466 int all_windows;
9467 struct gcpro gcpro1, gcpro2;
9468 struct frame *f;
9469 Lisp_Object tooltip_frame;
9470
9471 #ifdef HAVE_WINDOW_SYSTEM
9472 tooltip_frame = tip_frame;
9473 #else
9474 tooltip_frame = Qnil;
9475 #endif
9476
9477 /* Update all frame titles based on their buffer names, etc. We do
9478 this before the menu bars so that the buffer-menu will show the
9479 up-to-date frame titles. */
9480 #ifdef HAVE_WINDOW_SYSTEM
9481 if (windows_or_buffers_changed || update_mode_lines)
9482 {
9483 Lisp_Object tail, frame;
9484
9485 FOR_EACH_FRAME (tail, frame)
9486 {
9487 f = XFRAME (frame);
9488 if (!EQ (frame, tooltip_frame)
9489 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9490 x_consider_frame_title (frame);
9491 }
9492 }
9493 #endif /* HAVE_WINDOW_SYSTEM */
9494
9495 /* Update the menu bar item lists, if appropriate. This has to be
9496 done before any actual redisplay or generation of display lines. */
9497 all_windows = (update_mode_lines
9498 || buffer_shared > 1
9499 || windows_or_buffers_changed);
9500 if (all_windows)
9501 {
9502 Lisp_Object tail, frame;
9503 int count = SPECPDL_INDEX ();
9504 /* 1 means that update_menu_bar has run its hooks
9505 so any further calls to update_menu_bar shouldn't do so again. */
9506 int menu_bar_hooks_run = 0;
9507
9508 record_unwind_save_match_data ();
9509
9510 FOR_EACH_FRAME (tail, frame)
9511 {
9512 f = XFRAME (frame);
9513
9514 /* Ignore tooltip frame. */
9515 if (EQ (frame, tooltip_frame))
9516 continue;
9517
9518 /* If a window on this frame changed size, report that to
9519 the user and clear the size-change flag. */
9520 if (FRAME_WINDOW_SIZES_CHANGED (f))
9521 {
9522 Lisp_Object functions;
9523
9524 /* Clear flag first in case we get an error below. */
9525 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9526 functions = Vwindow_size_change_functions;
9527 GCPRO2 (tail, functions);
9528
9529 while (CONSP (functions))
9530 {
9531 call1 (XCAR (functions), frame);
9532 functions = XCDR (functions);
9533 }
9534 UNGCPRO;
9535 }
9536
9537 GCPRO1 (tail);
9538 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9539 #ifdef HAVE_WINDOW_SYSTEM
9540 update_tool_bar (f, 0);
9541 #ifdef MAC_OS
9542 mac_update_title_bar (f, 0);
9543 #endif
9544 #endif
9545 UNGCPRO;
9546 }
9547
9548 unbind_to (count, Qnil);
9549 }
9550 else
9551 {
9552 struct frame *sf = SELECTED_FRAME ();
9553 update_menu_bar (sf, 1, 0);
9554 #ifdef HAVE_WINDOW_SYSTEM
9555 update_tool_bar (sf, 1);
9556 #ifdef MAC_OS
9557 mac_update_title_bar (sf, 1);
9558 #endif
9559 #endif
9560 }
9561
9562 /* Motif needs this. See comment in xmenu.c. Turn it off when
9563 pending_menu_activation is not defined. */
9564 #ifdef USE_X_TOOLKIT
9565 pending_menu_activation = 0;
9566 #endif
9567 }
9568
9569
9570 /* Update the menu bar item list for frame F. This has to be done
9571 before we start to fill in any display lines, because it can call
9572 eval.
9573
9574 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9575
9576 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9577 already ran the menu bar hooks for this redisplay, so there
9578 is no need to run them again. The return value is the
9579 updated value of this flag, to pass to the next call. */
9580
9581 static int
9582 update_menu_bar (f, save_match_data, hooks_run)
9583 struct frame *f;
9584 int save_match_data;
9585 int hooks_run;
9586 {
9587 Lisp_Object window;
9588 register struct window *w;
9589
9590 /* If called recursively during a menu update, do nothing. This can
9591 happen when, for instance, an activate-menubar-hook causes a
9592 redisplay. */
9593 if (inhibit_menubar_update)
9594 return hooks_run;
9595
9596 window = FRAME_SELECTED_WINDOW (f);
9597 w = XWINDOW (window);
9598
9599 #if 0 /* The if statement below this if statement used to include the
9600 condition !NILP (w->update_mode_line), rather than using
9601 update_mode_lines directly, and this if statement may have
9602 been added to make that condition work. Now the if
9603 statement below matches its comment, this isn't needed. */
9604 if (update_mode_lines)
9605 w->update_mode_line = Qt;
9606 #endif
9607
9608 if (FRAME_WINDOW_P (f)
9609 ?
9610 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9611 || defined (USE_GTK)
9612 FRAME_EXTERNAL_MENU_BAR (f)
9613 #else
9614 FRAME_MENU_BAR_LINES (f) > 0
9615 #endif
9616 : FRAME_MENU_BAR_LINES (f) > 0)
9617 {
9618 /* If the user has switched buffers or windows, we need to
9619 recompute to reflect the new bindings. But we'll
9620 recompute when update_mode_lines is set too; that means
9621 that people can use force-mode-line-update to request
9622 that the menu bar be recomputed. The adverse effect on
9623 the rest of the redisplay algorithm is about the same as
9624 windows_or_buffers_changed anyway. */
9625 if (windows_or_buffers_changed
9626 /* This used to test w->update_mode_line, but we believe
9627 there is no need to recompute the menu in that case. */
9628 || update_mode_lines
9629 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9630 < BUF_MODIFF (XBUFFER (w->buffer)))
9631 != !NILP (w->last_had_star))
9632 || ((!NILP (Vtransient_mark_mode)
9633 && !NILP (XBUFFER (w->buffer)->mark_active))
9634 != !NILP (w->region_showing)))
9635 {
9636 struct buffer *prev = current_buffer;
9637 int count = SPECPDL_INDEX ();
9638
9639 specbind (Qinhibit_menubar_update, Qt);
9640
9641 set_buffer_internal_1 (XBUFFER (w->buffer));
9642 if (save_match_data)
9643 record_unwind_save_match_data ();
9644 if (NILP (Voverriding_local_map_menu_flag))
9645 {
9646 specbind (Qoverriding_terminal_local_map, Qnil);
9647 specbind (Qoverriding_local_map, Qnil);
9648 }
9649
9650 if (!hooks_run)
9651 {
9652 /* Run the Lucid hook. */
9653 safe_run_hooks (Qactivate_menubar_hook);
9654
9655 /* If it has changed current-menubar from previous value,
9656 really recompute the menu-bar from the value. */
9657 if (! NILP (Vlucid_menu_bar_dirty_flag))
9658 call0 (Qrecompute_lucid_menubar);
9659
9660 safe_run_hooks (Qmenu_bar_update_hook);
9661
9662 hooks_run = 1;
9663 }
9664
9665 XSETFRAME (Vmenu_updating_frame, f);
9666 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9667
9668 /* Redisplay the menu bar in case we changed it. */
9669 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9670 || defined (USE_GTK)
9671 if (FRAME_WINDOW_P (f))
9672 {
9673 #ifdef MAC_OS
9674 /* All frames on Mac OS share the same menubar. So only
9675 the selected frame should be allowed to set it. */
9676 if (f == SELECTED_FRAME ())
9677 #endif
9678 set_frame_menubar (f, 0, 0);
9679 }
9680 else
9681 /* On a terminal screen, the menu bar is an ordinary screen
9682 line, and this makes it get updated. */
9683 w->update_mode_line = Qt;
9684 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9685 /* In the non-toolkit version, the menu bar is an ordinary screen
9686 line, and this makes it get updated. */
9687 w->update_mode_line = Qt;
9688 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9689
9690 unbind_to (count, Qnil);
9691 set_buffer_internal_1 (prev);
9692 }
9693 }
9694
9695 return hooks_run;
9696 }
9697
9698
9699 \f
9700 /***********************************************************************
9701 Output Cursor
9702 ***********************************************************************/
9703
9704 #ifdef HAVE_WINDOW_SYSTEM
9705
9706 /* EXPORT:
9707 Nominal cursor position -- where to draw output.
9708 HPOS and VPOS are window relative glyph matrix coordinates.
9709 X and Y are window relative pixel coordinates. */
9710
9711 struct cursor_pos output_cursor;
9712
9713
9714 /* EXPORT:
9715 Set the global variable output_cursor to CURSOR. All cursor
9716 positions are relative to updated_window. */
9717
9718 void
9719 set_output_cursor (cursor)
9720 struct cursor_pos *cursor;
9721 {
9722 output_cursor.hpos = cursor->hpos;
9723 output_cursor.vpos = cursor->vpos;
9724 output_cursor.x = cursor->x;
9725 output_cursor.y = cursor->y;
9726 }
9727
9728
9729 /* EXPORT for RIF:
9730 Set a nominal cursor position.
9731
9732 HPOS and VPOS are column/row positions in a window glyph matrix. X
9733 and Y are window text area relative pixel positions.
9734
9735 If this is done during an update, updated_window will contain the
9736 window that is being updated and the position is the future output
9737 cursor position for that window. If updated_window is null, use
9738 selected_window and display the cursor at the given position. */
9739
9740 void
9741 x_cursor_to (vpos, hpos, y, x)
9742 int vpos, hpos, y, x;
9743 {
9744 struct window *w;
9745
9746 /* If updated_window is not set, work on selected_window. */
9747 if (updated_window)
9748 w = updated_window;
9749 else
9750 w = XWINDOW (selected_window);
9751
9752 /* Set the output cursor. */
9753 output_cursor.hpos = hpos;
9754 output_cursor.vpos = vpos;
9755 output_cursor.x = x;
9756 output_cursor.y = y;
9757
9758 /* If not called as part of an update, really display the cursor.
9759 This will also set the cursor position of W. */
9760 if (updated_window == NULL)
9761 {
9762 BLOCK_INPUT;
9763 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9764 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9765 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9766 UNBLOCK_INPUT;
9767 }
9768 }
9769
9770 #endif /* HAVE_WINDOW_SYSTEM */
9771
9772 \f
9773 /***********************************************************************
9774 Tool-bars
9775 ***********************************************************************/
9776
9777 #ifdef HAVE_WINDOW_SYSTEM
9778
9779 /* Where the mouse was last time we reported a mouse event. */
9780
9781 FRAME_PTR last_mouse_frame;
9782
9783 /* Tool-bar item index of the item on which a mouse button was pressed
9784 or -1. */
9785
9786 int last_tool_bar_item;
9787
9788
9789 /* Update the tool-bar item list for frame F. This has to be done
9790 before we start to fill in any display lines. Called from
9791 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9792 and restore it here. */
9793
9794 static void
9795 update_tool_bar (f, save_match_data)
9796 struct frame *f;
9797 int save_match_data;
9798 {
9799 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9800 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9801 #else
9802 int do_update = WINDOWP (f->tool_bar_window)
9803 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9804 #endif
9805
9806 if (do_update)
9807 {
9808 Lisp_Object window;
9809 struct window *w;
9810
9811 window = FRAME_SELECTED_WINDOW (f);
9812 w = XWINDOW (window);
9813
9814 /* If the user has switched buffers or windows, we need to
9815 recompute to reflect the new bindings. But we'll
9816 recompute when update_mode_lines is set too; that means
9817 that people can use force-mode-line-update to request
9818 that the menu bar be recomputed. The adverse effect on
9819 the rest of the redisplay algorithm is about the same as
9820 windows_or_buffers_changed anyway. */
9821 if (windows_or_buffers_changed
9822 || !NILP (w->update_mode_line)
9823 || update_mode_lines
9824 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9825 < BUF_MODIFF (XBUFFER (w->buffer)))
9826 != !NILP (w->last_had_star))
9827 || ((!NILP (Vtransient_mark_mode)
9828 && !NILP (XBUFFER (w->buffer)->mark_active))
9829 != !NILP (w->region_showing)))
9830 {
9831 struct buffer *prev = current_buffer;
9832 int count = SPECPDL_INDEX ();
9833 Lisp_Object new_tool_bar;
9834 int new_n_tool_bar;
9835 struct gcpro gcpro1;
9836
9837 /* Set current_buffer to the buffer of the selected
9838 window of the frame, so that we get the right local
9839 keymaps. */
9840 set_buffer_internal_1 (XBUFFER (w->buffer));
9841
9842 /* Save match data, if we must. */
9843 if (save_match_data)
9844 record_unwind_save_match_data ();
9845
9846 /* Make sure that we don't accidentally use bogus keymaps. */
9847 if (NILP (Voverriding_local_map_menu_flag))
9848 {
9849 specbind (Qoverriding_terminal_local_map, Qnil);
9850 specbind (Qoverriding_local_map, Qnil);
9851 }
9852
9853 GCPRO1 (new_tool_bar);
9854
9855 /* Build desired tool-bar items from keymaps. */
9856 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9857 &new_n_tool_bar);
9858
9859 /* Redisplay the tool-bar if we changed it. */
9860 if (new_n_tool_bar != f->n_tool_bar_items
9861 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9862 {
9863 /* Redisplay that happens asynchronously due to an expose event
9864 may access f->tool_bar_items. Make sure we update both
9865 variables within BLOCK_INPUT so no such event interrupts. */
9866 BLOCK_INPUT;
9867 f->tool_bar_items = new_tool_bar;
9868 f->n_tool_bar_items = new_n_tool_bar;
9869 w->update_mode_line = Qt;
9870 UNBLOCK_INPUT;
9871 }
9872
9873 UNGCPRO;
9874
9875 unbind_to (count, Qnil);
9876 set_buffer_internal_1 (prev);
9877 }
9878 }
9879 }
9880
9881
9882 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9883 F's desired tool-bar contents. F->tool_bar_items must have
9884 been set up previously by calling prepare_menu_bars. */
9885
9886 static void
9887 build_desired_tool_bar_string (f)
9888 struct frame *f;
9889 {
9890 int i, size, size_needed;
9891 struct gcpro gcpro1, gcpro2, gcpro3;
9892 Lisp_Object image, plist, props;
9893
9894 image = plist = props = Qnil;
9895 GCPRO3 (image, plist, props);
9896
9897 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9898 Otherwise, make a new string. */
9899
9900 /* The size of the string we might be able to reuse. */
9901 size = (STRINGP (f->desired_tool_bar_string)
9902 ? SCHARS (f->desired_tool_bar_string)
9903 : 0);
9904
9905 /* We need one space in the string for each image. */
9906 size_needed = f->n_tool_bar_items;
9907
9908 /* Reuse f->desired_tool_bar_string, if possible. */
9909 if (size < size_needed || NILP (f->desired_tool_bar_string))
9910 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9911 make_number (' '));
9912 else
9913 {
9914 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9915 Fremove_text_properties (make_number (0), make_number (size),
9916 props, f->desired_tool_bar_string);
9917 }
9918
9919 /* Put a `display' property on the string for the images to display,
9920 put a `menu_item' property on tool-bar items with a value that
9921 is the index of the item in F's tool-bar item vector. */
9922 for (i = 0; i < f->n_tool_bar_items; ++i)
9923 {
9924 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9925
9926 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9927 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9928 int hmargin, vmargin, relief, idx, end;
9929 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9930
9931 /* If image is a vector, choose the image according to the
9932 button state. */
9933 image = PROP (TOOL_BAR_ITEM_IMAGES);
9934 if (VECTORP (image))
9935 {
9936 if (enabled_p)
9937 idx = (selected_p
9938 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9939 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9940 else
9941 idx = (selected_p
9942 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9943 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9944
9945 xassert (ASIZE (image) >= idx);
9946 image = AREF (image, idx);
9947 }
9948 else
9949 idx = -1;
9950
9951 /* Ignore invalid image specifications. */
9952 if (!valid_image_p (image))
9953 continue;
9954
9955 /* Display the tool-bar button pressed, or depressed. */
9956 plist = Fcopy_sequence (XCDR (image));
9957
9958 /* Compute margin and relief to draw. */
9959 relief = (tool_bar_button_relief >= 0
9960 ? tool_bar_button_relief
9961 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9962 hmargin = vmargin = relief;
9963
9964 if (INTEGERP (Vtool_bar_button_margin)
9965 && XINT (Vtool_bar_button_margin) > 0)
9966 {
9967 hmargin += XFASTINT (Vtool_bar_button_margin);
9968 vmargin += XFASTINT (Vtool_bar_button_margin);
9969 }
9970 else if (CONSP (Vtool_bar_button_margin))
9971 {
9972 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9973 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9974 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9975
9976 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9977 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9978 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9979 }
9980
9981 if (auto_raise_tool_bar_buttons_p)
9982 {
9983 /* Add a `:relief' property to the image spec if the item is
9984 selected. */
9985 if (selected_p)
9986 {
9987 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9988 hmargin -= relief;
9989 vmargin -= relief;
9990 }
9991 }
9992 else
9993 {
9994 /* If image is selected, display it pressed, i.e. with a
9995 negative relief. If it's not selected, display it with a
9996 raised relief. */
9997 plist = Fplist_put (plist, QCrelief,
9998 (selected_p
9999 ? make_number (-relief)
10000 : make_number (relief)));
10001 hmargin -= relief;
10002 vmargin -= relief;
10003 }
10004
10005 /* Put a margin around the image. */
10006 if (hmargin || vmargin)
10007 {
10008 if (hmargin == vmargin)
10009 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10010 else
10011 plist = Fplist_put (plist, QCmargin,
10012 Fcons (make_number (hmargin),
10013 make_number (vmargin)));
10014 }
10015
10016 /* If button is not enabled, and we don't have special images
10017 for the disabled state, make the image appear disabled by
10018 applying an appropriate algorithm to it. */
10019 if (!enabled_p && idx < 0)
10020 plist = Fplist_put (plist, QCconversion, Qdisabled);
10021
10022 /* Put a `display' text property on the string for the image to
10023 display. Put a `menu-item' property on the string that gives
10024 the start of this item's properties in the tool-bar items
10025 vector. */
10026 image = Fcons (Qimage, plist);
10027 props = list4 (Qdisplay, image,
10028 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10029
10030 /* Let the last image hide all remaining spaces in the tool bar
10031 string. The string can be longer than needed when we reuse a
10032 previous string. */
10033 if (i + 1 == f->n_tool_bar_items)
10034 end = SCHARS (f->desired_tool_bar_string);
10035 else
10036 end = i + 1;
10037 Fadd_text_properties (make_number (i), make_number (end),
10038 props, f->desired_tool_bar_string);
10039 #undef PROP
10040 }
10041
10042 UNGCPRO;
10043 }
10044
10045
10046 /* Display one line of the tool-bar of frame IT->f.
10047
10048 HEIGHT specifies the desired height of the tool-bar line.
10049 If the actual height of the glyph row is less than HEIGHT, the
10050 row's height is increased to HEIGHT, and the icons are centered
10051 vertically in the new height.
10052
10053 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10054 count a final empty row in case the tool-bar width exactly matches
10055 the window width.
10056 */
10057
10058 static void
10059 display_tool_bar_line (it, height)
10060 struct it *it;
10061 int height;
10062 {
10063 struct glyph_row *row = it->glyph_row;
10064 int max_x = it->last_visible_x;
10065 struct glyph *last;
10066
10067 prepare_desired_row (row);
10068 row->y = it->current_y;
10069
10070 /* Note that this isn't made use of if the face hasn't a box,
10071 so there's no need to check the face here. */
10072 it->start_of_box_run_p = 1;
10073
10074 while (it->current_x < max_x)
10075 {
10076 int x, n_glyphs_before, i, nglyphs;
10077 struct it it_before;
10078
10079 /* Get the next display element. */
10080 if (!get_next_display_element (it))
10081 {
10082 /* Don't count empty row if we are counting needed tool-bar lines. */
10083 if (height < 0 && !it->hpos)
10084 return;
10085 break;
10086 }
10087
10088 /* Produce glyphs. */
10089 n_glyphs_before = row->used[TEXT_AREA];
10090 it_before = *it;
10091
10092 PRODUCE_GLYPHS (it);
10093
10094 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10095 i = 0;
10096 x = it_before.current_x;
10097 while (i < nglyphs)
10098 {
10099 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10100
10101 if (x + glyph->pixel_width > max_x)
10102 {
10103 /* Glyph doesn't fit on line. Backtrack. */
10104 row->used[TEXT_AREA] = n_glyphs_before;
10105 *it = it_before;
10106 /* If this is the only glyph on this line, it will never fit on the
10107 toolbar, so skip it. But ensure there is at least one glyph,
10108 so we don't accidentally disable the tool-bar. */
10109 if (n_glyphs_before == 0
10110 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10111 break;
10112 goto out;
10113 }
10114
10115 ++it->hpos;
10116 x += glyph->pixel_width;
10117 ++i;
10118 }
10119
10120 /* Stop at line ends. */
10121 if (ITERATOR_AT_END_OF_LINE_P (it))
10122 break;
10123
10124 set_iterator_to_next (it, 1);
10125 }
10126
10127 out:;
10128
10129 row->displays_text_p = row->used[TEXT_AREA] != 0;
10130
10131 /* Use default face for the border below the tool bar.
10132
10133 FIXME: When auto-resize-tool-bars is grow-only, there is
10134 no additional border below the possibly empty tool-bar lines.
10135 So to make the extra empty lines look "normal", we have to
10136 use the tool-bar face for the border too. */
10137 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10138 it->face_id = DEFAULT_FACE_ID;
10139
10140 extend_face_to_end_of_line (it);
10141 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10142 last->right_box_line_p = 1;
10143 if (last == row->glyphs[TEXT_AREA])
10144 last->left_box_line_p = 1;
10145
10146 /* Make line the desired height and center it vertically. */
10147 if ((height -= it->max_ascent + it->max_descent) > 0)
10148 {
10149 /* Don't add more than one line height. */
10150 height %= FRAME_LINE_HEIGHT (it->f);
10151 it->max_ascent += height / 2;
10152 it->max_descent += (height + 1) / 2;
10153 }
10154
10155 compute_line_metrics (it);
10156
10157 /* If line is empty, make it occupy the rest of the tool-bar. */
10158 if (!row->displays_text_p)
10159 {
10160 row->height = row->phys_height = it->last_visible_y - row->y;
10161 row->visible_height = row->height;
10162 row->ascent = row->phys_ascent = 0;
10163 row->extra_line_spacing = 0;
10164 }
10165
10166 row->full_width_p = 1;
10167 row->continued_p = 0;
10168 row->truncated_on_left_p = 0;
10169 row->truncated_on_right_p = 0;
10170
10171 it->current_x = it->hpos = 0;
10172 it->current_y += row->height;
10173 ++it->vpos;
10174 ++it->glyph_row;
10175 }
10176
10177
10178 /* Max tool-bar height. */
10179
10180 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10181 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10182
10183 /* Value is the number of screen lines needed to make all tool-bar
10184 items of frame F visible. The number of actual rows needed is
10185 returned in *N_ROWS if non-NULL. */
10186
10187 static int
10188 tool_bar_lines_needed (f, n_rows)
10189 struct frame *f;
10190 int *n_rows;
10191 {
10192 struct window *w = XWINDOW (f->tool_bar_window);
10193 struct it it;
10194 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10195 the desired matrix, so use (unused) mode-line row as temporary row to
10196 avoid destroying the first tool-bar row. */
10197 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10198
10199 /* Initialize an iterator for iteration over
10200 F->desired_tool_bar_string in the tool-bar window of frame F. */
10201 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10202 it.first_visible_x = 0;
10203 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10204 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10205
10206 while (!ITERATOR_AT_END_P (&it))
10207 {
10208 clear_glyph_row (temp_row);
10209 it.glyph_row = temp_row;
10210 display_tool_bar_line (&it, -1);
10211 }
10212 clear_glyph_row (temp_row);
10213
10214 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10215 if (n_rows)
10216 *n_rows = it.vpos > 0 ? it.vpos : -1;
10217
10218 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10219 }
10220
10221
10222 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10223 0, 1, 0,
10224 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10225 (frame)
10226 Lisp_Object frame;
10227 {
10228 struct frame *f;
10229 struct window *w;
10230 int nlines = 0;
10231
10232 if (NILP (frame))
10233 frame = selected_frame;
10234 else
10235 CHECK_FRAME (frame);
10236 f = XFRAME (frame);
10237
10238 if (WINDOWP (f->tool_bar_window)
10239 || (w = XWINDOW (f->tool_bar_window),
10240 WINDOW_TOTAL_LINES (w) > 0))
10241 {
10242 update_tool_bar (f, 1);
10243 if (f->n_tool_bar_items)
10244 {
10245 build_desired_tool_bar_string (f);
10246 nlines = tool_bar_lines_needed (f, NULL);
10247 }
10248 }
10249
10250 return make_number (nlines);
10251 }
10252
10253
10254 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10255 height should be changed. */
10256
10257 static int
10258 redisplay_tool_bar (f)
10259 struct frame *f;
10260 {
10261 struct window *w;
10262 struct it it;
10263 struct glyph_row *row;
10264
10265 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10266 if (FRAME_EXTERNAL_TOOL_BAR (f))
10267 update_frame_tool_bar (f);
10268 return 0;
10269 #endif
10270
10271 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10272 do anything. This means you must start with tool-bar-lines
10273 non-zero to get the auto-sizing effect. Or in other words, you
10274 can turn off tool-bars by specifying tool-bar-lines zero. */
10275 if (!WINDOWP (f->tool_bar_window)
10276 || (w = XWINDOW (f->tool_bar_window),
10277 WINDOW_TOTAL_LINES (w) == 0))
10278 return 0;
10279
10280 /* Set up an iterator for the tool-bar window. */
10281 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10282 it.first_visible_x = 0;
10283 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10284 row = it.glyph_row;
10285
10286 /* Build a string that represents the contents of the tool-bar. */
10287 build_desired_tool_bar_string (f);
10288 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10289
10290 if (f->n_tool_bar_rows == 0)
10291 {
10292 int nlines;
10293
10294 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10295 nlines != WINDOW_TOTAL_LINES (w)))
10296 {
10297 extern Lisp_Object Qtool_bar_lines;
10298 Lisp_Object frame;
10299 int old_height = WINDOW_TOTAL_LINES (w);
10300
10301 XSETFRAME (frame, f);
10302 Fmodify_frame_parameters (frame,
10303 Fcons (Fcons (Qtool_bar_lines,
10304 make_number (nlines)),
10305 Qnil));
10306 if (WINDOW_TOTAL_LINES (w) != old_height)
10307 {
10308 clear_glyph_matrix (w->desired_matrix);
10309 fonts_changed_p = 1;
10310 return 1;
10311 }
10312 }
10313 }
10314
10315 /* Display as many lines as needed to display all tool-bar items. */
10316
10317 if (f->n_tool_bar_rows > 0)
10318 {
10319 int border, rows, height, extra;
10320
10321 if (INTEGERP (Vtool_bar_border))
10322 border = XINT (Vtool_bar_border);
10323 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10324 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10325 else if (EQ (Vtool_bar_border, Qborder_width))
10326 border = f->border_width;
10327 else
10328 border = 0;
10329 if (border < 0)
10330 border = 0;
10331
10332 rows = f->n_tool_bar_rows;
10333 height = max (1, (it.last_visible_y - border) / rows);
10334 extra = it.last_visible_y - border - height * rows;
10335
10336 while (it.current_y < it.last_visible_y)
10337 {
10338 int h = 0;
10339 if (extra > 0 && rows-- > 0)
10340 {
10341 h = (extra + rows - 1) / rows;
10342 extra -= h;
10343 }
10344 display_tool_bar_line (&it, height + h);
10345 }
10346 }
10347 else
10348 {
10349 while (it.current_y < it.last_visible_y)
10350 display_tool_bar_line (&it, 0);
10351 }
10352
10353 /* It doesn't make much sense to try scrolling in the tool-bar
10354 window, so don't do it. */
10355 w->desired_matrix->no_scrolling_p = 1;
10356 w->must_be_updated_p = 1;
10357
10358 if (!NILP (Vauto_resize_tool_bars))
10359 {
10360 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10361 int change_height_p = 0;
10362
10363 /* If we couldn't display everything, change the tool-bar's
10364 height if there is room for more. */
10365 if (IT_STRING_CHARPOS (it) < it.end_charpos
10366 && it.current_y < max_tool_bar_height)
10367 change_height_p = 1;
10368
10369 row = it.glyph_row - 1;
10370
10371 /* If there are blank lines at the end, except for a partially
10372 visible blank line at the end that is smaller than
10373 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10374 if (!row->displays_text_p
10375 && row->height >= FRAME_LINE_HEIGHT (f))
10376 change_height_p = 1;
10377
10378 /* If row displays tool-bar items, but is partially visible,
10379 change the tool-bar's height. */
10380 if (row->displays_text_p
10381 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10382 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10383 change_height_p = 1;
10384
10385 /* Resize windows as needed by changing the `tool-bar-lines'
10386 frame parameter. */
10387 if (change_height_p)
10388 {
10389 extern Lisp_Object Qtool_bar_lines;
10390 Lisp_Object frame;
10391 int old_height = WINDOW_TOTAL_LINES (w);
10392 int nrows;
10393 int nlines = tool_bar_lines_needed (f, &nrows);
10394
10395 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10396 && !f->minimize_tool_bar_window_p)
10397 ? (nlines > old_height)
10398 : (nlines != old_height));
10399 f->minimize_tool_bar_window_p = 0;
10400
10401 if (change_height_p)
10402 {
10403 XSETFRAME (frame, f);
10404 Fmodify_frame_parameters (frame,
10405 Fcons (Fcons (Qtool_bar_lines,
10406 make_number (nlines)),
10407 Qnil));
10408 if (WINDOW_TOTAL_LINES (w) != old_height)
10409 {
10410 clear_glyph_matrix (w->desired_matrix);
10411 f->n_tool_bar_rows = nrows;
10412 fonts_changed_p = 1;
10413 return 1;
10414 }
10415 }
10416 }
10417 }
10418
10419 f->minimize_tool_bar_window_p = 0;
10420 return 0;
10421 }
10422
10423
10424 /* Get information about the tool-bar item which is displayed in GLYPH
10425 on frame F. Return in *PROP_IDX the index where tool-bar item
10426 properties start in F->tool_bar_items. Value is zero if
10427 GLYPH doesn't display a tool-bar item. */
10428
10429 static int
10430 tool_bar_item_info (f, glyph, prop_idx)
10431 struct frame *f;
10432 struct glyph *glyph;
10433 int *prop_idx;
10434 {
10435 Lisp_Object prop;
10436 int success_p;
10437 int charpos;
10438
10439 /* This function can be called asynchronously, which means we must
10440 exclude any possibility that Fget_text_property signals an
10441 error. */
10442 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10443 charpos = max (0, charpos);
10444
10445 /* Get the text property `menu-item' at pos. The value of that
10446 property is the start index of this item's properties in
10447 F->tool_bar_items. */
10448 prop = Fget_text_property (make_number (charpos),
10449 Qmenu_item, f->current_tool_bar_string);
10450 if (INTEGERP (prop))
10451 {
10452 *prop_idx = XINT (prop);
10453 success_p = 1;
10454 }
10455 else
10456 success_p = 0;
10457
10458 return success_p;
10459 }
10460
10461 \f
10462 /* Get information about the tool-bar item at position X/Y on frame F.
10463 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10464 the current matrix of the tool-bar window of F, or NULL if not
10465 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10466 item in F->tool_bar_items. Value is
10467
10468 -1 if X/Y is not on a tool-bar item
10469 0 if X/Y is on the same item that was highlighted before.
10470 1 otherwise. */
10471
10472 static int
10473 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10474 struct frame *f;
10475 int x, y;
10476 struct glyph **glyph;
10477 int *hpos, *vpos, *prop_idx;
10478 {
10479 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10480 struct window *w = XWINDOW (f->tool_bar_window);
10481 int area;
10482
10483 /* Find the glyph under X/Y. */
10484 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10485 if (*glyph == NULL)
10486 return -1;
10487
10488 /* Get the start of this tool-bar item's properties in
10489 f->tool_bar_items. */
10490 if (!tool_bar_item_info (f, *glyph, prop_idx))
10491 return -1;
10492
10493 /* Is mouse on the highlighted item? */
10494 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10495 && *vpos >= dpyinfo->mouse_face_beg_row
10496 && *vpos <= dpyinfo->mouse_face_end_row
10497 && (*vpos > dpyinfo->mouse_face_beg_row
10498 || *hpos >= dpyinfo->mouse_face_beg_col)
10499 && (*vpos < dpyinfo->mouse_face_end_row
10500 || *hpos < dpyinfo->mouse_face_end_col
10501 || dpyinfo->mouse_face_past_end))
10502 return 0;
10503
10504 return 1;
10505 }
10506
10507
10508 /* EXPORT:
10509 Handle mouse button event on the tool-bar of frame F, at
10510 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10511 0 for button release. MODIFIERS is event modifiers for button
10512 release. */
10513
10514 void
10515 handle_tool_bar_click (f, x, y, down_p, modifiers)
10516 struct frame *f;
10517 int x, y, down_p;
10518 unsigned int modifiers;
10519 {
10520 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10521 struct window *w = XWINDOW (f->tool_bar_window);
10522 int hpos, vpos, prop_idx;
10523 struct glyph *glyph;
10524 Lisp_Object enabled_p;
10525
10526 /* If not on the highlighted tool-bar item, return. */
10527 frame_to_window_pixel_xy (w, &x, &y);
10528 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10529 return;
10530
10531 /* If item is disabled, do nothing. */
10532 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10533 if (NILP (enabled_p))
10534 return;
10535
10536 if (down_p)
10537 {
10538 /* Show item in pressed state. */
10539 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10540 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10541 last_tool_bar_item = prop_idx;
10542 }
10543 else
10544 {
10545 Lisp_Object key, frame;
10546 struct input_event event;
10547 EVENT_INIT (event);
10548
10549 /* Show item in released state. */
10550 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10551 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10552
10553 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10554
10555 XSETFRAME (frame, f);
10556 event.kind = TOOL_BAR_EVENT;
10557 event.frame_or_window = frame;
10558 event.arg = frame;
10559 kbd_buffer_store_event (&event);
10560
10561 event.kind = TOOL_BAR_EVENT;
10562 event.frame_or_window = frame;
10563 event.arg = key;
10564 event.modifiers = modifiers;
10565 kbd_buffer_store_event (&event);
10566 last_tool_bar_item = -1;
10567 }
10568 }
10569
10570
10571 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10572 tool-bar window-relative coordinates X/Y. Called from
10573 note_mouse_highlight. */
10574
10575 static void
10576 note_tool_bar_highlight (f, x, y)
10577 struct frame *f;
10578 int x, y;
10579 {
10580 Lisp_Object window = f->tool_bar_window;
10581 struct window *w = XWINDOW (window);
10582 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10583 int hpos, vpos;
10584 struct glyph *glyph;
10585 struct glyph_row *row;
10586 int i;
10587 Lisp_Object enabled_p;
10588 int prop_idx;
10589 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10590 int mouse_down_p, rc;
10591
10592 /* Function note_mouse_highlight is called with negative x(y
10593 values when mouse moves outside of the frame. */
10594 if (x <= 0 || y <= 0)
10595 {
10596 clear_mouse_face (dpyinfo);
10597 return;
10598 }
10599
10600 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10601 if (rc < 0)
10602 {
10603 /* Not on tool-bar item. */
10604 clear_mouse_face (dpyinfo);
10605 return;
10606 }
10607 else if (rc == 0)
10608 /* On same tool-bar item as before. */
10609 goto set_help_echo;
10610
10611 clear_mouse_face (dpyinfo);
10612
10613 /* Mouse is down, but on different tool-bar item? */
10614 mouse_down_p = (dpyinfo->grabbed
10615 && f == last_mouse_frame
10616 && FRAME_LIVE_P (f));
10617 if (mouse_down_p
10618 && last_tool_bar_item != prop_idx)
10619 return;
10620
10621 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10622 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10623
10624 /* If tool-bar item is not enabled, don't highlight it. */
10625 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10626 if (!NILP (enabled_p))
10627 {
10628 /* Compute the x-position of the glyph. In front and past the
10629 image is a space. We include this in the highlighted area. */
10630 row = MATRIX_ROW (w->current_matrix, vpos);
10631 for (i = x = 0; i < hpos; ++i)
10632 x += row->glyphs[TEXT_AREA][i].pixel_width;
10633
10634 /* Record this as the current active region. */
10635 dpyinfo->mouse_face_beg_col = hpos;
10636 dpyinfo->mouse_face_beg_row = vpos;
10637 dpyinfo->mouse_face_beg_x = x;
10638 dpyinfo->mouse_face_beg_y = row->y;
10639 dpyinfo->mouse_face_past_end = 0;
10640
10641 dpyinfo->mouse_face_end_col = hpos + 1;
10642 dpyinfo->mouse_face_end_row = vpos;
10643 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10644 dpyinfo->mouse_face_end_y = row->y;
10645 dpyinfo->mouse_face_window = window;
10646 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10647
10648 /* Display it as active. */
10649 show_mouse_face (dpyinfo, draw);
10650 dpyinfo->mouse_face_image_state = draw;
10651 }
10652
10653 set_help_echo:
10654
10655 /* Set help_echo_string to a help string to display for this tool-bar item.
10656 XTread_socket does the rest. */
10657 help_echo_object = help_echo_window = Qnil;
10658 help_echo_pos = -1;
10659 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10660 if (NILP (help_echo_string))
10661 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10662 }
10663
10664 #endif /* HAVE_WINDOW_SYSTEM */
10665
10666
10667 \f
10668 /************************************************************************
10669 Horizontal scrolling
10670 ************************************************************************/
10671
10672 static int hscroll_window_tree P_ ((Lisp_Object));
10673 static int hscroll_windows P_ ((Lisp_Object));
10674
10675 /* For all leaf windows in the window tree rooted at WINDOW, set their
10676 hscroll value so that PT is (i) visible in the window, and (ii) so
10677 that it is not within a certain margin at the window's left and
10678 right border. Value is non-zero if any window's hscroll has been
10679 changed. */
10680
10681 static int
10682 hscroll_window_tree (window)
10683 Lisp_Object window;
10684 {
10685 int hscrolled_p = 0;
10686 int hscroll_relative_p = FLOATP (Vhscroll_step);
10687 int hscroll_step_abs = 0;
10688 double hscroll_step_rel = 0;
10689
10690 if (hscroll_relative_p)
10691 {
10692 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10693 if (hscroll_step_rel < 0)
10694 {
10695 hscroll_relative_p = 0;
10696 hscroll_step_abs = 0;
10697 }
10698 }
10699 else if (INTEGERP (Vhscroll_step))
10700 {
10701 hscroll_step_abs = XINT (Vhscroll_step);
10702 if (hscroll_step_abs < 0)
10703 hscroll_step_abs = 0;
10704 }
10705 else
10706 hscroll_step_abs = 0;
10707
10708 while (WINDOWP (window))
10709 {
10710 struct window *w = XWINDOW (window);
10711
10712 if (WINDOWP (w->hchild))
10713 hscrolled_p |= hscroll_window_tree (w->hchild);
10714 else if (WINDOWP (w->vchild))
10715 hscrolled_p |= hscroll_window_tree (w->vchild);
10716 else if (w->cursor.vpos >= 0)
10717 {
10718 int h_margin;
10719 int text_area_width;
10720 struct glyph_row *current_cursor_row
10721 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10722 struct glyph_row *desired_cursor_row
10723 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10724 struct glyph_row *cursor_row
10725 = (desired_cursor_row->enabled_p
10726 ? desired_cursor_row
10727 : current_cursor_row);
10728
10729 text_area_width = window_box_width (w, TEXT_AREA);
10730
10731 /* Scroll when cursor is inside this scroll margin. */
10732 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10733
10734 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10735 && ((XFASTINT (w->hscroll)
10736 && w->cursor.x <= h_margin)
10737 || (cursor_row->enabled_p
10738 && cursor_row->truncated_on_right_p
10739 && (w->cursor.x >= text_area_width - h_margin))))
10740 {
10741 struct it it;
10742 int hscroll;
10743 struct buffer *saved_current_buffer;
10744 int pt;
10745 int wanted_x;
10746
10747 /* Find point in a display of infinite width. */
10748 saved_current_buffer = current_buffer;
10749 current_buffer = XBUFFER (w->buffer);
10750
10751 if (w == XWINDOW (selected_window))
10752 pt = BUF_PT (current_buffer);
10753 else
10754 {
10755 pt = marker_position (w->pointm);
10756 pt = max (BEGV, pt);
10757 pt = min (ZV, pt);
10758 }
10759
10760 /* Move iterator to pt starting at cursor_row->start in
10761 a line with infinite width. */
10762 init_to_row_start (&it, w, cursor_row);
10763 it.last_visible_x = INFINITY;
10764 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10765 current_buffer = saved_current_buffer;
10766
10767 /* Position cursor in window. */
10768 if (!hscroll_relative_p && hscroll_step_abs == 0)
10769 hscroll = max (0, (it.current_x
10770 - (ITERATOR_AT_END_OF_LINE_P (&it)
10771 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10772 : (text_area_width / 2))))
10773 / FRAME_COLUMN_WIDTH (it.f);
10774 else if (w->cursor.x >= text_area_width - h_margin)
10775 {
10776 if (hscroll_relative_p)
10777 wanted_x = text_area_width * (1 - hscroll_step_rel)
10778 - h_margin;
10779 else
10780 wanted_x = text_area_width
10781 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10782 - h_margin;
10783 hscroll
10784 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10785 }
10786 else
10787 {
10788 if (hscroll_relative_p)
10789 wanted_x = text_area_width * hscroll_step_rel
10790 + h_margin;
10791 else
10792 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10793 + h_margin;
10794 hscroll
10795 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10796 }
10797 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10798
10799 /* Don't call Fset_window_hscroll if value hasn't
10800 changed because it will prevent redisplay
10801 optimizations. */
10802 if (XFASTINT (w->hscroll) != hscroll)
10803 {
10804 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10805 w->hscroll = make_number (hscroll);
10806 hscrolled_p = 1;
10807 }
10808 }
10809 }
10810
10811 window = w->next;
10812 }
10813
10814 /* Value is non-zero if hscroll of any leaf window has been changed. */
10815 return hscrolled_p;
10816 }
10817
10818
10819 /* Set hscroll so that cursor is visible and not inside horizontal
10820 scroll margins for all windows in the tree rooted at WINDOW. See
10821 also hscroll_window_tree above. Value is non-zero if any window's
10822 hscroll has been changed. If it has, desired matrices on the frame
10823 of WINDOW are cleared. */
10824
10825 static int
10826 hscroll_windows (window)
10827 Lisp_Object window;
10828 {
10829 int hscrolled_p = hscroll_window_tree (window);
10830 if (hscrolled_p)
10831 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10832 return hscrolled_p;
10833 }
10834
10835
10836 \f
10837 /************************************************************************
10838 Redisplay
10839 ************************************************************************/
10840
10841 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10842 to a non-zero value. This is sometimes handy to have in a debugger
10843 session. */
10844
10845 #if GLYPH_DEBUG
10846
10847 /* First and last unchanged row for try_window_id. */
10848
10849 int debug_first_unchanged_at_end_vpos;
10850 int debug_last_unchanged_at_beg_vpos;
10851
10852 /* Delta vpos and y. */
10853
10854 int debug_dvpos, debug_dy;
10855
10856 /* Delta in characters and bytes for try_window_id. */
10857
10858 int debug_delta, debug_delta_bytes;
10859
10860 /* Values of window_end_pos and window_end_vpos at the end of
10861 try_window_id. */
10862
10863 EMACS_INT debug_end_pos, debug_end_vpos;
10864
10865 /* Append a string to W->desired_matrix->method. FMT is a printf
10866 format string. A1...A9 are a supplement for a variable-length
10867 argument list. If trace_redisplay_p is non-zero also printf the
10868 resulting string to stderr. */
10869
10870 static void
10871 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10872 struct window *w;
10873 char *fmt;
10874 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10875 {
10876 char buffer[512];
10877 char *method = w->desired_matrix->method;
10878 int len = strlen (method);
10879 int size = sizeof w->desired_matrix->method;
10880 int remaining = size - len - 1;
10881
10882 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10883 if (len && remaining)
10884 {
10885 method[len] = '|';
10886 --remaining, ++len;
10887 }
10888
10889 strncpy (method + len, buffer, remaining);
10890
10891 if (trace_redisplay_p)
10892 fprintf (stderr, "%p (%s): %s\n",
10893 w,
10894 ((BUFFERP (w->buffer)
10895 && STRINGP (XBUFFER (w->buffer)->name))
10896 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10897 : "no buffer"),
10898 buffer);
10899 }
10900
10901 #endif /* GLYPH_DEBUG */
10902
10903
10904 /* Value is non-zero if all changes in window W, which displays
10905 current_buffer, are in the text between START and END. START is a
10906 buffer position, END is given as a distance from Z. Used in
10907 redisplay_internal for display optimization. */
10908
10909 static INLINE int
10910 text_outside_line_unchanged_p (w, start, end)
10911 struct window *w;
10912 int start, end;
10913 {
10914 int unchanged_p = 1;
10915
10916 /* If text or overlays have changed, see where. */
10917 if (XFASTINT (w->last_modified) < MODIFF
10918 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10919 {
10920 /* Gap in the line? */
10921 if (GPT < start || Z - GPT < end)
10922 unchanged_p = 0;
10923
10924 /* Changes start in front of the line, or end after it? */
10925 if (unchanged_p
10926 && (BEG_UNCHANGED < start - 1
10927 || END_UNCHANGED < end))
10928 unchanged_p = 0;
10929
10930 /* If selective display, can't optimize if changes start at the
10931 beginning of the line. */
10932 if (unchanged_p
10933 && INTEGERP (current_buffer->selective_display)
10934 && XINT (current_buffer->selective_display) > 0
10935 && (BEG_UNCHANGED < start || GPT <= start))
10936 unchanged_p = 0;
10937
10938 /* If there are overlays at the start or end of the line, these
10939 may have overlay strings with newlines in them. A change at
10940 START, for instance, may actually concern the display of such
10941 overlay strings as well, and they are displayed on different
10942 lines. So, quickly rule out this case. (For the future, it
10943 might be desirable to implement something more telling than
10944 just BEG/END_UNCHANGED.) */
10945 if (unchanged_p)
10946 {
10947 if (BEG + BEG_UNCHANGED == start
10948 && overlay_touches_p (start))
10949 unchanged_p = 0;
10950 if (END_UNCHANGED == end
10951 && overlay_touches_p (Z - end))
10952 unchanged_p = 0;
10953 }
10954 }
10955
10956 return unchanged_p;
10957 }
10958
10959
10960 /* Do a frame update, taking possible shortcuts into account. This is
10961 the main external entry point for redisplay.
10962
10963 If the last redisplay displayed an echo area message and that message
10964 is no longer requested, we clear the echo area or bring back the
10965 mini-buffer if that is in use. */
10966
10967 void
10968 redisplay ()
10969 {
10970 redisplay_internal (0);
10971 }
10972
10973
10974 static Lisp_Object
10975 overlay_arrow_string_or_property (var)
10976 Lisp_Object var;
10977 {
10978 Lisp_Object val;
10979
10980 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10981 return val;
10982
10983 return Voverlay_arrow_string;
10984 }
10985
10986 /* Return 1 if there are any overlay-arrows in current_buffer. */
10987 static int
10988 overlay_arrow_in_current_buffer_p ()
10989 {
10990 Lisp_Object vlist;
10991
10992 for (vlist = Voverlay_arrow_variable_list;
10993 CONSP (vlist);
10994 vlist = XCDR (vlist))
10995 {
10996 Lisp_Object var = XCAR (vlist);
10997 Lisp_Object val;
10998
10999 if (!SYMBOLP (var))
11000 continue;
11001 val = find_symbol_value (var);
11002 if (MARKERP (val)
11003 && current_buffer == XMARKER (val)->buffer)
11004 return 1;
11005 }
11006 return 0;
11007 }
11008
11009
11010 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11011 has changed. */
11012
11013 static int
11014 overlay_arrows_changed_p ()
11015 {
11016 Lisp_Object vlist;
11017
11018 for (vlist = Voverlay_arrow_variable_list;
11019 CONSP (vlist);
11020 vlist = XCDR (vlist))
11021 {
11022 Lisp_Object var = XCAR (vlist);
11023 Lisp_Object val, pstr;
11024
11025 if (!SYMBOLP (var))
11026 continue;
11027 val = find_symbol_value (var);
11028 if (!MARKERP (val))
11029 continue;
11030 if (! EQ (COERCE_MARKER (val),
11031 Fget (var, Qlast_arrow_position))
11032 || ! (pstr = overlay_arrow_string_or_property (var),
11033 EQ (pstr, Fget (var, Qlast_arrow_string))))
11034 return 1;
11035 }
11036 return 0;
11037 }
11038
11039 /* Mark overlay arrows to be updated on next redisplay. */
11040
11041 static void
11042 update_overlay_arrows (up_to_date)
11043 int up_to_date;
11044 {
11045 Lisp_Object vlist;
11046
11047 for (vlist = Voverlay_arrow_variable_list;
11048 CONSP (vlist);
11049 vlist = XCDR (vlist))
11050 {
11051 Lisp_Object var = XCAR (vlist);
11052
11053 if (!SYMBOLP (var))
11054 continue;
11055
11056 if (up_to_date > 0)
11057 {
11058 Lisp_Object val = find_symbol_value (var);
11059 Fput (var, Qlast_arrow_position,
11060 COERCE_MARKER (val));
11061 Fput (var, Qlast_arrow_string,
11062 overlay_arrow_string_or_property (var));
11063 }
11064 else if (up_to_date < 0
11065 || !NILP (Fget (var, Qlast_arrow_position)))
11066 {
11067 Fput (var, Qlast_arrow_position, Qt);
11068 Fput (var, Qlast_arrow_string, Qt);
11069 }
11070 }
11071 }
11072
11073
11074 /* Return overlay arrow string to display at row.
11075 Return integer (bitmap number) for arrow bitmap in left fringe.
11076 Return nil if no overlay arrow. */
11077
11078 static Lisp_Object
11079 overlay_arrow_at_row (it, row)
11080 struct it *it;
11081 struct glyph_row *row;
11082 {
11083 Lisp_Object vlist;
11084
11085 for (vlist = Voverlay_arrow_variable_list;
11086 CONSP (vlist);
11087 vlist = XCDR (vlist))
11088 {
11089 Lisp_Object var = XCAR (vlist);
11090 Lisp_Object val;
11091
11092 if (!SYMBOLP (var))
11093 continue;
11094
11095 val = find_symbol_value (var);
11096
11097 if (MARKERP (val)
11098 && current_buffer == XMARKER (val)->buffer
11099 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11100 {
11101 if (FRAME_WINDOW_P (it->f)
11102 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11103 {
11104 #ifdef HAVE_WINDOW_SYSTEM
11105 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11106 {
11107 int fringe_bitmap;
11108 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11109 return make_number (fringe_bitmap);
11110 }
11111 #endif
11112 return make_number (-1); /* Use default arrow bitmap */
11113 }
11114 return overlay_arrow_string_or_property (var);
11115 }
11116 }
11117
11118 return Qnil;
11119 }
11120
11121 /* Return 1 if point moved out of or into a composition. Otherwise
11122 return 0. PREV_BUF and PREV_PT are the last point buffer and
11123 position. BUF and PT are the current point buffer and position. */
11124
11125 int
11126 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11127 struct buffer *prev_buf, *buf;
11128 int prev_pt, pt;
11129 {
11130 EMACS_INT start, end;
11131 Lisp_Object prop;
11132 Lisp_Object buffer;
11133
11134 XSETBUFFER (buffer, buf);
11135 /* Check a composition at the last point if point moved within the
11136 same buffer. */
11137 if (prev_buf == buf)
11138 {
11139 if (prev_pt == pt)
11140 /* Point didn't move. */
11141 return 0;
11142
11143 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11144 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11145 && COMPOSITION_VALID_P (start, end, prop)
11146 && start < prev_pt && end > prev_pt)
11147 /* The last point was within the composition. Return 1 iff
11148 point moved out of the composition. */
11149 return (pt <= start || pt >= end);
11150 }
11151
11152 /* Check a composition at the current point. */
11153 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11154 && find_composition (pt, -1, &start, &end, &prop, buffer)
11155 && COMPOSITION_VALID_P (start, end, prop)
11156 && start < pt && end > pt);
11157 }
11158
11159
11160 /* Reconsider the setting of B->clip_changed which is displayed
11161 in window W. */
11162
11163 static INLINE void
11164 reconsider_clip_changes (w, b)
11165 struct window *w;
11166 struct buffer *b;
11167 {
11168 if (b->clip_changed
11169 && !NILP (w->window_end_valid)
11170 && w->current_matrix->buffer == b
11171 && w->current_matrix->zv == BUF_ZV (b)
11172 && w->current_matrix->begv == BUF_BEGV (b))
11173 b->clip_changed = 0;
11174
11175 /* If display wasn't paused, and W is not a tool bar window, see if
11176 point has been moved into or out of a composition. In that case,
11177 we set b->clip_changed to 1 to force updating the screen. If
11178 b->clip_changed has already been set to 1, we can skip this
11179 check. */
11180 if (!b->clip_changed
11181 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11182 {
11183 int pt;
11184
11185 if (w == XWINDOW (selected_window))
11186 pt = BUF_PT (current_buffer);
11187 else
11188 pt = marker_position (w->pointm);
11189
11190 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11191 || pt != XINT (w->last_point))
11192 && check_point_in_composition (w->current_matrix->buffer,
11193 XINT (w->last_point),
11194 XBUFFER (w->buffer), pt))
11195 b->clip_changed = 1;
11196 }
11197 }
11198 \f
11199
11200 /* Select FRAME to forward the values of frame-local variables into C
11201 variables so that the redisplay routines can access those values
11202 directly. */
11203
11204 static void
11205 select_frame_for_redisplay (frame)
11206 Lisp_Object frame;
11207 {
11208 Lisp_Object tail, symbol, val;
11209 Lisp_Object old = selected_frame;
11210 struct Lisp_Symbol *sym;
11211
11212 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11213
11214 selected_frame = frame;
11215
11216 do
11217 {
11218 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11219 if (CONSP (XCAR (tail))
11220 && (symbol = XCAR (XCAR (tail)),
11221 SYMBOLP (symbol))
11222 && (sym = indirect_variable (XSYMBOL (symbol)),
11223 val = sym->value,
11224 (BUFFER_LOCAL_VALUEP (val)))
11225 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11226 /* Use find_symbol_value rather than Fsymbol_value
11227 to avoid an error if it is void. */
11228 find_symbol_value (symbol);
11229 } while (!EQ (frame, old) && (frame = old, 1));
11230 }
11231
11232
11233 #define STOP_POLLING \
11234 do { if (! polling_stopped_here) stop_polling (); \
11235 polling_stopped_here = 1; } while (0)
11236
11237 #define RESUME_POLLING \
11238 do { if (polling_stopped_here) start_polling (); \
11239 polling_stopped_here = 0; } while (0)
11240
11241
11242 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11243 response to any user action; therefore, we should preserve the echo
11244 area. (Actually, our caller does that job.) Perhaps in the future
11245 avoid recentering windows if it is not necessary; currently that
11246 causes some problems. */
11247
11248 static void
11249 redisplay_internal (preserve_echo_area)
11250 int preserve_echo_area;
11251 {
11252 struct window *w = XWINDOW (selected_window);
11253 struct frame *f;
11254 int pause;
11255 int must_finish = 0;
11256 struct text_pos tlbufpos, tlendpos;
11257 int number_of_visible_frames;
11258 int count, count1;
11259 struct frame *sf;
11260 int polling_stopped_here = 0;
11261 Lisp_Object old_frame = selected_frame;
11262
11263 /* Non-zero means redisplay has to consider all windows on all
11264 frames. Zero means, only selected_window is considered. */
11265 int consider_all_windows_p;
11266
11267 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11268
11269 /* No redisplay if running in batch mode or frame is not yet fully
11270 initialized, or redisplay is explicitly turned off by setting
11271 Vinhibit_redisplay. */
11272 if (noninteractive
11273 || !NILP (Vinhibit_redisplay))
11274 return;
11275
11276 /* Don't examine these until after testing Vinhibit_redisplay.
11277 When Emacs is shutting down, perhaps because its connection to
11278 X has dropped, we should not look at them at all. */
11279 f = XFRAME (w->frame);
11280 sf = SELECTED_FRAME ();
11281
11282 if (!f->glyphs_initialized_p)
11283 return;
11284
11285 /* The flag redisplay_performed_directly_p is set by
11286 direct_output_for_insert when it already did the whole screen
11287 update necessary. */
11288 if (redisplay_performed_directly_p)
11289 {
11290 redisplay_performed_directly_p = 0;
11291 if (!hscroll_windows (selected_window))
11292 return;
11293 }
11294
11295 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11296 if (popup_activated ())
11297 return;
11298 #endif
11299
11300 /* I don't think this happens but let's be paranoid. */
11301 if (redisplaying_p)
11302 return;
11303
11304 /* Record a function that resets redisplaying_p to its old value
11305 when we leave this function. */
11306 count = SPECPDL_INDEX ();
11307 record_unwind_protect (unwind_redisplay,
11308 Fcons (make_number (redisplaying_p), selected_frame));
11309 ++redisplaying_p;
11310 specbind (Qinhibit_free_realized_faces, Qnil);
11311
11312 {
11313 Lisp_Object tail, frame;
11314
11315 FOR_EACH_FRAME (tail, frame)
11316 {
11317 struct frame *f = XFRAME (frame);
11318 f->already_hscrolled_p = 0;
11319 }
11320 }
11321
11322 retry:
11323 if (!EQ (old_frame, selected_frame)
11324 && FRAME_LIVE_P (XFRAME (old_frame)))
11325 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11326 selected_frame and selected_window to be temporarily out-of-sync so
11327 when we come back here via `goto retry', we need to resync because we
11328 may need to run Elisp code (via prepare_menu_bars). */
11329 select_frame_for_redisplay (old_frame);
11330
11331 pause = 0;
11332 reconsider_clip_changes (w, current_buffer);
11333 last_escape_glyph_frame = NULL;
11334 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11335
11336 /* If new fonts have been loaded that make a glyph matrix adjustment
11337 necessary, do it. */
11338 if (fonts_changed_p)
11339 {
11340 adjust_glyphs (NULL);
11341 ++windows_or_buffers_changed;
11342 fonts_changed_p = 0;
11343 }
11344
11345 /* If face_change_count is non-zero, init_iterator will free all
11346 realized faces, which includes the faces referenced from current
11347 matrices. So, we can't reuse current matrices in this case. */
11348 if (face_change_count)
11349 ++windows_or_buffers_changed;
11350
11351 if (FRAME_TERMCAP_P (sf)
11352 && FRAME_TTY (sf)->previous_frame != sf)
11353 {
11354 /* Since frames on a single ASCII terminal share the same
11355 display area, displaying a different frame means redisplay
11356 the whole thing. */
11357 windows_or_buffers_changed++;
11358 SET_FRAME_GARBAGED (sf);
11359 #ifndef WINDOWSNT
11360 set_tty_color_mode (FRAME_TTY (sf), sf);
11361 #endif
11362 FRAME_TTY (sf)->previous_frame = sf;
11363 }
11364
11365 /* Set the visible flags for all frames. Do this before checking
11366 for resized or garbaged frames; they want to know if their frames
11367 are visible. See the comment in frame.h for
11368 FRAME_SAMPLE_VISIBILITY. */
11369 {
11370 Lisp_Object tail, frame;
11371
11372 number_of_visible_frames = 0;
11373
11374 FOR_EACH_FRAME (tail, frame)
11375 {
11376 struct frame *f = XFRAME (frame);
11377
11378 FRAME_SAMPLE_VISIBILITY (f);
11379 if (FRAME_VISIBLE_P (f))
11380 ++number_of_visible_frames;
11381 clear_desired_matrices (f);
11382 }
11383 }
11384
11385
11386 /* Notice any pending interrupt request to change frame size. */
11387 do_pending_window_change (1);
11388
11389 /* Clear frames marked as garbaged. */
11390 if (frame_garbaged)
11391 clear_garbaged_frames ();
11392
11393 /* Build menubar and tool-bar items. */
11394 if (NILP (Vmemory_full))
11395 prepare_menu_bars ();
11396
11397 if (windows_or_buffers_changed)
11398 update_mode_lines++;
11399
11400 /* Detect case that we need to write or remove a star in the mode line. */
11401 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11402 {
11403 w->update_mode_line = Qt;
11404 if (buffer_shared > 1)
11405 update_mode_lines++;
11406 }
11407
11408 /* Avoid invocation of point motion hooks by `current_column' below. */
11409 count1 = SPECPDL_INDEX ();
11410 specbind (Qinhibit_point_motion_hooks, Qt);
11411
11412 /* If %c is in the mode line, update it if needed. */
11413 if (!NILP (w->column_number_displayed)
11414 /* This alternative quickly identifies a common case
11415 where no change is needed. */
11416 && !(PT == XFASTINT (w->last_point)
11417 && XFASTINT (w->last_modified) >= MODIFF
11418 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11419 && (XFASTINT (w->column_number_displayed)
11420 != (int) current_column ())) /* iftc */
11421 w->update_mode_line = Qt;
11422
11423 unbind_to (count1, Qnil);
11424
11425 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11426
11427 /* The variable buffer_shared is set in redisplay_window and
11428 indicates that we redisplay a buffer in different windows. See
11429 there. */
11430 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11431 || cursor_type_changed);
11432
11433 /* If specs for an arrow have changed, do thorough redisplay
11434 to ensure we remove any arrow that should no longer exist. */
11435 if (overlay_arrows_changed_p ())
11436 consider_all_windows_p = windows_or_buffers_changed = 1;
11437
11438 /* Normally the message* functions will have already displayed and
11439 updated the echo area, but the frame may have been trashed, or
11440 the update may have been preempted, so display the echo area
11441 again here. Checking message_cleared_p captures the case that
11442 the echo area should be cleared. */
11443 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11444 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11445 || (message_cleared_p
11446 && minibuf_level == 0
11447 /* If the mini-window is currently selected, this means the
11448 echo-area doesn't show through. */
11449 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11450 {
11451 int window_height_changed_p = echo_area_display (0);
11452 must_finish = 1;
11453
11454 /* If we don't display the current message, don't clear the
11455 message_cleared_p flag, because, if we did, we wouldn't clear
11456 the echo area in the next redisplay which doesn't preserve
11457 the echo area. */
11458 if (!display_last_displayed_message_p)
11459 message_cleared_p = 0;
11460
11461 if (fonts_changed_p)
11462 goto retry;
11463 else if (window_height_changed_p)
11464 {
11465 consider_all_windows_p = 1;
11466 ++update_mode_lines;
11467 ++windows_or_buffers_changed;
11468
11469 /* If window configuration was changed, frames may have been
11470 marked garbaged. Clear them or we will experience
11471 surprises wrt scrolling. */
11472 if (frame_garbaged)
11473 clear_garbaged_frames ();
11474 }
11475 }
11476 else if (EQ (selected_window, minibuf_window)
11477 && (current_buffer->clip_changed
11478 || XFASTINT (w->last_modified) < MODIFF
11479 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11480 && resize_mini_window (w, 0))
11481 {
11482 /* Resized active mini-window to fit the size of what it is
11483 showing if its contents might have changed. */
11484 must_finish = 1;
11485 consider_all_windows_p = 1;
11486 ++windows_or_buffers_changed;
11487 ++update_mode_lines;
11488
11489 /* If window configuration was changed, frames may have been
11490 marked garbaged. Clear them or we will experience
11491 surprises wrt scrolling. */
11492 if (frame_garbaged)
11493 clear_garbaged_frames ();
11494 }
11495
11496
11497 /* If showing the region, and mark has changed, we must redisplay
11498 the whole window. The assignment to this_line_start_pos prevents
11499 the optimization directly below this if-statement. */
11500 if (((!NILP (Vtransient_mark_mode)
11501 && !NILP (XBUFFER (w->buffer)->mark_active))
11502 != !NILP (w->region_showing))
11503 || (!NILP (w->region_showing)
11504 && !EQ (w->region_showing,
11505 Fmarker_position (XBUFFER (w->buffer)->mark))))
11506 CHARPOS (this_line_start_pos) = 0;
11507
11508 /* Optimize the case that only the line containing the cursor in the
11509 selected window has changed. Variables starting with this_ are
11510 set in display_line and record information about the line
11511 containing the cursor. */
11512 tlbufpos = this_line_start_pos;
11513 tlendpos = this_line_end_pos;
11514 if (!consider_all_windows_p
11515 && CHARPOS (tlbufpos) > 0
11516 && NILP (w->update_mode_line)
11517 && !current_buffer->clip_changed
11518 && !current_buffer->prevent_redisplay_optimizations_p
11519 && FRAME_VISIBLE_P (XFRAME (w->frame))
11520 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11521 /* Make sure recorded data applies to current buffer, etc. */
11522 && this_line_buffer == current_buffer
11523 && current_buffer == XBUFFER (w->buffer)
11524 && NILP (w->force_start)
11525 && NILP (w->optional_new_start)
11526 /* Point must be on the line that we have info recorded about. */
11527 && PT >= CHARPOS (tlbufpos)
11528 && PT <= Z - CHARPOS (tlendpos)
11529 /* All text outside that line, including its final newline,
11530 must be unchanged */
11531 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11532 CHARPOS (tlendpos)))
11533 {
11534 if (CHARPOS (tlbufpos) > BEGV
11535 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11536 && (CHARPOS (tlbufpos) == ZV
11537 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11538 /* Former continuation line has disappeared by becoming empty */
11539 goto cancel;
11540 else if (XFASTINT (w->last_modified) < MODIFF
11541 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11542 || MINI_WINDOW_P (w))
11543 {
11544 /* We have to handle the case of continuation around a
11545 wide-column character (See the comment in indent.c around
11546 line 885).
11547
11548 For instance, in the following case:
11549
11550 -------- Insert --------
11551 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11552 J_I_ ==> J_I_ `^^' are cursors.
11553 ^^ ^^
11554 -------- --------
11555
11556 As we have to redraw the line above, we should goto cancel. */
11557
11558 struct it it;
11559 int line_height_before = this_line_pixel_height;
11560
11561 /* Note that start_display will handle the case that the
11562 line starting at tlbufpos is a continuation lines. */
11563 start_display (&it, w, tlbufpos);
11564
11565 /* Implementation note: It this still necessary? */
11566 if (it.current_x != this_line_start_x)
11567 goto cancel;
11568
11569 TRACE ((stderr, "trying display optimization 1\n"));
11570 w->cursor.vpos = -1;
11571 overlay_arrow_seen = 0;
11572 it.vpos = this_line_vpos;
11573 it.current_y = this_line_y;
11574 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11575 display_line (&it);
11576
11577 /* If line contains point, is not continued,
11578 and ends at same distance from eob as before, we win */
11579 if (w->cursor.vpos >= 0
11580 /* Line is not continued, otherwise this_line_start_pos
11581 would have been set to 0 in display_line. */
11582 && CHARPOS (this_line_start_pos)
11583 /* Line ends as before. */
11584 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11585 /* Line has same height as before. Otherwise other lines
11586 would have to be shifted up or down. */
11587 && this_line_pixel_height == line_height_before)
11588 {
11589 /* If this is not the window's last line, we must adjust
11590 the charstarts of the lines below. */
11591 if (it.current_y < it.last_visible_y)
11592 {
11593 struct glyph_row *row
11594 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11595 int delta, delta_bytes;
11596
11597 if (Z - CHARPOS (tlendpos) == ZV)
11598 {
11599 /* This line ends at end of (accessible part of)
11600 buffer. There is no newline to count. */
11601 delta = (Z
11602 - CHARPOS (tlendpos)
11603 - MATRIX_ROW_START_CHARPOS (row));
11604 delta_bytes = (Z_BYTE
11605 - BYTEPOS (tlendpos)
11606 - MATRIX_ROW_START_BYTEPOS (row));
11607 }
11608 else
11609 {
11610 /* This line ends in a newline. Must take
11611 account of the newline and the rest of the
11612 text that follows. */
11613 delta = (Z
11614 - CHARPOS (tlendpos)
11615 - MATRIX_ROW_START_CHARPOS (row));
11616 delta_bytes = (Z_BYTE
11617 - BYTEPOS (tlendpos)
11618 - MATRIX_ROW_START_BYTEPOS (row));
11619 }
11620
11621 increment_matrix_positions (w->current_matrix,
11622 this_line_vpos + 1,
11623 w->current_matrix->nrows,
11624 delta, delta_bytes);
11625 }
11626
11627 /* If this row displays text now but previously didn't,
11628 or vice versa, w->window_end_vpos may have to be
11629 adjusted. */
11630 if ((it.glyph_row - 1)->displays_text_p)
11631 {
11632 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11633 XSETINT (w->window_end_vpos, this_line_vpos);
11634 }
11635 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11636 && this_line_vpos > 0)
11637 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11638 w->window_end_valid = Qnil;
11639
11640 /* Update hint: No need to try to scroll in update_window. */
11641 w->desired_matrix->no_scrolling_p = 1;
11642
11643 #if GLYPH_DEBUG
11644 *w->desired_matrix->method = 0;
11645 debug_method_add (w, "optimization 1");
11646 #endif
11647 #ifdef HAVE_WINDOW_SYSTEM
11648 update_window_fringes (w, 0);
11649 #endif
11650 goto update;
11651 }
11652 else
11653 goto cancel;
11654 }
11655 else if (/* Cursor position hasn't changed. */
11656 PT == XFASTINT (w->last_point)
11657 /* Make sure the cursor was last displayed
11658 in this window. Otherwise we have to reposition it. */
11659 && 0 <= w->cursor.vpos
11660 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11661 {
11662 if (!must_finish)
11663 {
11664 do_pending_window_change (1);
11665
11666 /* We used to always goto end_of_redisplay here, but this
11667 isn't enough if we have a blinking cursor. */
11668 if (w->cursor_off_p == w->last_cursor_off_p)
11669 goto end_of_redisplay;
11670 }
11671 goto update;
11672 }
11673 /* If highlighting the region, or if the cursor is in the echo area,
11674 then we can't just move the cursor. */
11675 else if (! (!NILP (Vtransient_mark_mode)
11676 && !NILP (current_buffer->mark_active))
11677 && (EQ (selected_window, current_buffer->last_selected_window)
11678 || highlight_nonselected_windows)
11679 && NILP (w->region_showing)
11680 && NILP (Vshow_trailing_whitespace)
11681 && !cursor_in_echo_area)
11682 {
11683 struct it it;
11684 struct glyph_row *row;
11685
11686 /* Skip from tlbufpos to PT and see where it is. Note that
11687 PT may be in invisible text. If so, we will end at the
11688 next visible position. */
11689 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11690 NULL, DEFAULT_FACE_ID);
11691 it.current_x = this_line_start_x;
11692 it.current_y = this_line_y;
11693 it.vpos = this_line_vpos;
11694
11695 /* The call to move_it_to stops in front of PT, but
11696 moves over before-strings. */
11697 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11698
11699 if (it.vpos == this_line_vpos
11700 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11701 row->enabled_p))
11702 {
11703 xassert (this_line_vpos == it.vpos);
11704 xassert (this_line_y == it.current_y);
11705 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11706 #if GLYPH_DEBUG
11707 *w->desired_matrix->method = 0;
11708 debug_method_add (w, "optimization 3");
11709 #endif
11710 goto update;
11711 }
11712 else
11713 goto cancel;
11714 }
11715
11716 cancel:
11717 /* Text changed drastically or point moved off of line. */
11718 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11719 }
11720
11721 CHARPOS (this_line_start_pos) = 0;
11722 consider_all_windows_p |= buffer_shared > 1;
11723 ++clear_face_cache_count;
11724 #ifdef HAVE_WINDOW_SYSTEM
11725 ++clear_image_cache_count;
11726 #endif
11727
11728 /* Build desired matrices, and update the display. If
11729 consider_all_windows_p is non-zero, do it for all windows on all
11730 frames. Otherwise do it for selected_window, only. */
11731
11732 if (consider_all_windows_p)
11733 {
11734 Lisp_Object tail, frame;
11735
11736 FOR_EACH_FRAME (tail, frame)
11737 XFRAME (frame)->updated_p = 0;
11738
11739 /* Recompute # windows showing selected buffer. This will be
11740 incremented each time such a window is displayed. */
11741 buffer_shared = 0;
11742
11743 FOR_EACH_FRAME (tail, frame)
11744 {
11745 struct frame *f = XFRAME (frame);
11746
11747 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11748 {
11749 if (! EQ (frame, selected_frame))
11750 /* Select the frame, for the sake of frame-local
11751 variables. */
11752 select_frame_for_redisplay (frame);
11753
11754 /* Mark all the scroll bars to be removed; we'll redeem
11755 the ones we want when we redisplay their windows. */
11756 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11757 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11758
11759 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11760 redisplay_windows (FRAME_ROOT_WINDOW (f));
11761
11762 /* Any scroll bars which redisplay_windows should have
11763 nuked should now go away. */
11764 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11765 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11766
11767 /* If fonts changed, display again. */
11768 /* ??? rms: I suspect it is a mistake to jump all the way
11769 back to retry here. It should just retry this frame. */
11770 if (fonts_changed_p)
11771 goto retry;
11772
11773 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11774 {
11775 /* See if we have to hscroll. */
11776 if (!f->already_hscrolled_p)
11777 {
11778 f->already_hscrolled_p = 1;
11779 if (hscroll_windows (f->root_window))
11780 goto retry;
11781 }
11782
11783 /* Prevent various kinds of signals during display
11784 update. stdio is not robust about handling
11785 signals, which can cause an apparent I/O
11786 error. */
11787 if (interrupt_input)
11788 unrequest_sigio ();
11789 STOP_POLLING;
11790
11791 /* Update the display. */
11792 set_window_update_flags (XWINDOW (f->root_window), 1);
11793 pause |= update_frame (f, 0, 0);
11794 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11795 if (pause)
11796 break;
11797 #endif
11798
11799 f->updated_p = 1;
11800 }
11801 }
11802 }
11803
11804 if (!EQ (old_frame, selected_frame)
11805 && FRAME_LIVE_P (XFRAME (old_frame)))
11806 /* We played a bit fast-and-loose above and allowed selected_frame
11807 and selected_window to be temporarily out-of-sync but let's make
11808 sure this stays contained. */
11809 select_frame_for_redisplay (old_frame);
11810 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11811
11812 if (!pause)
11813 {
11814 /* Do the mark_window_display_accurate after all windows have
11815 been redisplayed because this call resets flags in buffers
11816 which are needed for proper redisplay. */
11817 FOR_EACH_FRAME (tail, frame)
11818 {
11819 struct frame *f = XFRAME (frame);
11820 if (f->updated_p)
11821 {
11822 mark_window_display_accurate (f->root_window, 1);
11823 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11824 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11825 }
11826 }
11827 }
11828 }
11829 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11830 {
11831 Lisp_Object mini_window;
11832 struct frame *mini_frame;
11833
11834 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11835 /* Use list_of_error, not Qerror, so that
11836 we catch only errors and don't run the debugger. */
11837 internal_condition_case_1 (redisplay_window_1, selected_window,
11838 list_of_error,
11839 redisplay_window_error);
11840
11841 /* Compare desired and current matrices, perform output. */
11842
11843 update:
11844 /* If fonts changed, display again. */
11845 if (fonts_changed_p)
11846 goto retry;
11847
11848 /* Prevent various kinds of signals during display update.
11849 stdio is not robust about handling signals,
11850 which can cause an apparent I/O error. */
11851 if (interrupt_input)
11852 unrequest_sigio ();
11853 STOP_POLLING;
11854
11855 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11856 {
11857 if (hscroll_windows (selected_window))
11858 goto retry;
11859
11860 XWINDOW (selected_window)->must_be_updated_p = 1;
11861 pause = update_frame (sf, 0, 0);
11862 }
11863
11864 /* We may have called echo_area_display at the top of this
11865 function. If the echo area is on another frame, that may
11866 have put text on a frame other than the selected one, so the
11867 above call to update_frame would not have caught it. Catch
11868 it here. */
11869 mini_window = FRAME_MINIBUF_WINDOW (sf);
11870 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11871
11872 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11873 {
11874 XWINDOW (mini_window)->must_be_updated_p = 1;
11875 pause |= update_frame (mini_frame, 0, 0);
11876 if (!pause && hscroll_windows (mini_window))
11877 goto retry;
11878 }
11879 }
11880
11881 /* If display was paused because of pending input, make sure we do a
11882 thorough update the next time. */
11883 if (pause)
11884 {
11885 /* Prevent the optimization at the beginning of
11886 redisplay_internal that tries a single-line update of the
11887 line containing the cursor in the selected window. */
11888 CHARPOS (this_line_start_pos) = 0;
11889
11890 /* Let the overlay arrow be updated the next time. */
11891 update_overlay_arrows (0);
11892
11893 /* If we pause after scrolling, some rows in the current
11894 matrices of some windows are not valid. */
11895 if (!WINDOW_FULL_WIDTH_P (w)
11896 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11897 update_mode_lines = 1;
11898 }
11899 else
11900 {
11901 if (!consider_all_windows_p)
11902 {
11903 /* This has already been done above if
11904 consider_all_windows_p is set. */
11905 mark_window_display_accurate_1 (w, 1);
11906
11907 /* Say overlay arrows are up to date. */
11908 update_overlay_arrows (1);
11909
11910 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11911 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11912 }
11913
11914 update_mode_lines = 0;
11915 windows_or_buffers_changed = 0;
11916 cursor_type_changed = 0;
11917 }
11918
11919 /* Start SIGIO interrupts coming again. Having them off during the
11920 code above makes it less likely one will discard output, but not
11921 impossible, since there might be stuff in the system buffer here.
11922 But it is much hairier to try to do anything about that. */
11923 if (interrupt_input)
11924 request_sigio ();
11925 RESUME_POLLING;
11926
11927 /* If a frame has become visible which was not before, redisplay
11928 again, so that we display it. Expose events for such a frame
11929 (which it gets when becoming visible) don't call the parts of
11930 redisplay constructing glyphs, so simply exposing a frame won't
11931 display anything in this case. So, we have to display these
11932 frames here explicitly. */
11933 if (!pause)
11934 {
11935 Lisp_Object tail, frame;
11936 int new_count = 0;
11937
11938 FOR_EACH_FRAME (tail, frame)
11939 {
11940 int this_is_visible = 0;
11941
11942 if (XFRAME (frame)->visible)
11943 this_is_visible = 1;
11944 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11945 if (XFRAME (frame)->visible)
11946 this_is_visible = 1;
11947
11948 if (this_is_visible)
11949 new_count++;
11950 }
11951
11952 if (new_count != number_of_visible_frames)
11953 windows_or_buffers_changed++;
11954 }
11955
11956 /* Change frame size now if a change is pending. */
11957 do_pending_window_change (1);
11958
11959 /* If we just did a pending size change, or have additional
11960 visible frames, redisplay again. */
11961 if (windows_or_buffers_changed && !pause)
11962 goto retry;
11963
11964 /* Clear the face cache eventually. */
11965 if (consider_all_windows_p)
11966 {
11967 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11968 {
11969 clear_face_cache (0);
11970 clear_face_cache_count = 0;
11971 }
11972 #ifdef HAVE_WINDOW_SYSTEM
11973 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11974 {
11975 clear_image_caches (Qnil);
11976 clear_image_cache_count = 0;
11977 }
11978 #endif /* HAVE_WINDOW_SYSTEM */
11979 }
11980
11981 end_of_redisplay:
11982 unbind_to (count, Qnil);
11983 RESUME_POLLING;
11984 }
11985
11986
11987 /* Redisplay, but leave alone any recent echo area message unless
11988 another message has been requested in its place.
11989
11990 This is useful in situations where you need to redisplay but no
11991 user action has occurred, making it inappropriate for the message
11992 area to be cleared. See tracking_off and
11993 wait_reading_process_output for examples of these situations.
11994
11995 FROM_WHERE is an integer saying from where this function was
11996 called. This is useful for debugging. */
11997
11998 void
11999 redisplay_preserve_echo_area (from_where)
12000 int from_where;
12001 {
12002 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12003
12004 if (!NILP (echo_area_buffer[1]))
12005 {
12006 /* We have a previously displayed message, but no current
12007 message. Redisplay the previous message. */
12008 display_last_displayed_message_p = 1;
12009 redisplay_internal (1);
12010 display_last_displayed_message_p = 0;
12011 }
12012 else
12013 redisplay_internal (1);
12014
12015 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12016 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12017 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12018 }
12019
12020
12021 /* Function registered with record_unwind_protect in
12022 redisplay_internal. Reset redisplaying_p to the value it had
12023 before redisplay_internal was called, and clear
12024 prevent_freeing_realized_faces_p. It also selects the previously
12025 selected frame, unless it has been deleted (by an X connection
12026 failure during redisplay, for example). */
12027
12028 static Lisp_Object
12029 unwind_redisplay (val)
12030 Lisp_Object val;
12031 {
12032 Lisp_Object old_redisplaying_p, old_frame;
12033
12034 old_redisplaying_p = XCAR (val);
12035 redisplaying_p = XFASTINT (old_redisplaying_p);
12036 old_frame = XCDR (val);
12037 if (! EQ (old_frame, selected_frame)
12038 && FRAME_LIVE_P (XFRAME (old_frame)))
12039 select_frame_for_redisplay (old_frame);
12040 return Qnil;
12041 }
12042
12043
12044 /* Mark the display of window W as accurate or inaccurate. If
12045 ACCURATE_P is non-zero mark display of W as accurate. If
12046 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12047 redisplay_internal is called. */
12048
12049 static void
12050 mark_window_display_accurate_1 (w, accurate_p)
12051 struct window *w;
12052 int accurate_p;
12053 {
12054 if (BUFFERP (w->buffer))
12055 {
12056 struct buffer *b = XBUFFER (w->buffer);
12057
12058 w->last_modified
12059 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12060 w->last_overlay_modified
12061 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12062 w->last_had_star
12063 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12064
12065 if (accurate_p)
12066 {
12067 b->clip_changed = 0;
12068 b->prevent_redisplay_optimizations_p = 0;
12069
12070 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12071 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12072 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12073 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12074
12075 w->current_matrix->buffer = b;
12076 w->current_matrix->begv = BUF_BEGV (b);
12077 w->current_matrix->zv = BUF_ZV (b);
12078
12079 w->last_cursor = w->cursor;
12080 w->last_cursor_off_p = w->cursor_off_p;
12081
12082 if (w == XWINDOW (selected_window))
12083 w->last_point = make_number (BUF_PT (b));
12084 else
12085 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12086 }
12087 }
12088
12089 if (accurate_p)
12090 {
12091 w->window_end_valid = w->buffer;
12092 #if 0 /* This is incorrect with variable-height lines. */
12093 xassert (XINT (w->window_end_vpos)
12094 < (WINDOW_TOTAL_LINES (w)
12095 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
12096 #endif
12097 w->update_mode_line = Qnil;
12098 }
12099 }
12100
12101
12102 /* Mark the display of windows in the window tree rooted at WINDOW as
12103 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12104 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12105 be redisplayed the next time redisplay_internal is called. */
12106
12107 void
12108 mark_window_display_accurate (window, accurate_p)
12109 Lisp_Object window;
12110 int accurate_p;
12111 {
12112 struct window *w;
12113
12114 for (; !NILP (window); window = w->next)
12115 {
12116 w = XWINDOW (window);
12117 mark_window_display_accurate_1 (w, accurate_p);
12118
12119 if (!NILP (w->vchild))
12120 mark_window_display_accurate (w->vchild, accurate_p);
12121 if (!NILP (w->hchild))
12122 mark_window_display_accurate (w->hchild, accurate_p);
12123 }
12124
12125 if (accurate_p)
12126 {
12127 update_overlay_arrows (1);
12128 }
12129 else
12130 {
12131 /* Force a thorough redisplay the next time by setting
12132 last_arrow_position and last_arrow_string to t, which is
12133 unequal to any useful value of Voverlay_arrow_... */
12134 update_overlay_arrows (-1);
12135 }
12136 }
12137
12138
12139 /* Return value in display table DP (Lisp_Char_Table *) for character
12140 C. Since a display table doesn't have any parent, we don't have to
12141 follow parent. Do not call this function directly but use the
12142 macro DISP_CHAR_VECTOR. */
12143
12144 Lisp_Object
12145 disp_char_vector (dp, c)
12146 struct Lisp_Char_Table *dp;
12147 int c;
12148 {
12149 Lisp_Object val;
12150
12151 if (ASCII_CHAR_P (c))
12152 {
12153 val = dp->ascii;
12154 if (SUB_CHAR_TABLE_P (val))
12155 val = XSUB_CHAR_TABLE (val)->contents[c];
12156 }
12157 else
12158 {
12159 Lisp_Object table;
12160
12161 XSETCHAR_TABLE (table, dp);
12162 val = char_table_ref (table, c);
12163 }
12164 if (NILP (val))
12165 val = dp->defalt;
12166 return val;
12167 }
12168
12169
12170 \f
12171 /***********************************************************************
12172 Window Redisplay
12173 ***********************************************************************/
12174
12175 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12176
12177 static void
12178 redisplay_windows (window)
12179 Lisp_Object window;
12180 {
12181 while (!NILP (window))
12182 {
12183 struct window *w = XWINDOW (window);
12184
12185 if (!NILP (w->hchild))
12186 redisplay_windows (w->hchild);
12187 else if (!NILP (w->vchild))
12188 redisplay_windows (w->vchild);
12189 else
12190 {
12191 displayed_buffer = XBUFFER (w->buffer);
12192 /* Use list_of_error, not Qerror, so that
12193 we catch only errors and don't run the debugger. */
12194 internal_condition_case_1 (redisplay_window_0, window,
12195 list_of_error,
12196 redisplay_window_error);
12197 }
12198
12199 window = w->next;
12200 }
12201 }
12202
12203 static Lisp_Object
12204 redisplay_window_error ()
12205 {
12206 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12207 return Qnil;
12208 }
12209
12210 static Lisp_Object
12211 redisplay_window_0 (window)
12212 Lisp_Object window;
12213 {
12214 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12215 redisplay_window (window, 0);
12216 return Qnil;
12217 }
12218
12219 static Lisp_Object
12220 redisplay_window_1 (window)
12221 Lisp_Object window;
12222 {
12223 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12224 redisplay_window (window, 1);
12225 return Qnil;
12226 }
12227 \f
12228
12229 /* Increment GLYPH until it reaches END or CONDITION fails while
12230 adding (GLYPH)->pixel_width to X. */
12231
12232 #define SKIP_GLYPHS(glyph, end, x, condition) \
12233 do \
12234 { \
12235 (x) += (glyph)->pixel_width; \
12236 ++(glyph); \
12237 } \
12238 while ((glyph) < (end) && (condition))
12239
12240
12241 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12242 DELTA is the number of bytes by which positions recorded in ROW
12243 differ from current buffer positions.
12244
12245 Return 0 if cursor is not on this row. 1 otherwise. */
12246
12247 int
12248 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12249 struct window *w;
12250 struct glyph_row *row;
12251 struct glyph_matrix *matrix;
12252 int delta, delta_bytes, dy, dvpos;
12253 {
12254 struct glyph *glyph = row->glyphs[TEXT_AREA];
12255 struct glyph *end = glyph + row->used[TEXT_AREA];
12256 struct glyph *cursor = NULL;
12257 /* The first glyph that starts a sequence of glyphs from string. */
12258 struct glyph *string_start;
12259 /* The X coordinate of string_start. */
12260 int string_start_x;
12261 /* The last known character position. */
12262 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12263 /* The last known character position before string_start. */
12264 int string_before_pos;
12265 int x = row->x;
12266 int cursor_x = x;
12267 int cursor_from_overlay_pos = 0;
12268 int pt_old = PT - delta;
12269
12270 /* Skip over glyphs not having an object at the start of the row.
12271 These are special glyphs like truncation marks on terminal
12272 frames. */
12273 if (row->displays_text_p)
12274 while (glyph < end
12275 && INTEGERP (glyph->object)
12276 && glyph->charpos < 0)
12277 {
12278 x += glyph->pixel_width;
12279 ++glyph;
12280 }
12281
12282 string_start = NULL;
12283 while (glyph < end
12284 && !INTEGERP (glyph->object)
12285 && (!BUFFERP (glyph->object)
12286 || (last_pos = glyph->charpos) < pt_old
12287 || glyph->avoid_cursor_p))
12288 {
12289 if (! STRINGP (glyph->object))
12290 {
12291 string_start = NULL;
12292 x += glyph->pixel_width;
12293 ++glyph;
12294 if (cursor_from_overlay_pos
12295 && last_pos >= cursor_from_overlay_pos)
12296 {
12297 cursor_from_overlay_pos = 0;
12298 cursor = 0;
12299 }
12300 }
12301 else
12302 {
12303 if (string_start == NULL)
12304 {
12305 string_before_pos = last_pos;
12306 string_start = glyph;
12307 string_start_x = x;
12308 }
12309 /* Skip all glyphs from string. */
12310 do
12311 {
12312 Lisp_Object cprop;
12313 int pos;
12314 if ((cursor == NULL || glyph > cursor)
12315 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12316 Qcursor, (glyph)->object),
12317 !NILP (cprop))
12318 && (pos = string_buffer_position (w, glyph->object,
12319 string_before_pos),
12320 (pos == 0 /* From overlay */
12321 || pos == pt_old)))
12322 {
12323 /* Estimate overlay buffer position from the buffer
12324 positions of the glyphs before and after the overlay.
12325 Add 1 to last_pos so that if point corresponds to the
12326 glyph right after the overlay, we still use a 'cursor'
12327 property found in that overlay. */
12328 cursor_from_overlay_pos = (pos ? 0 : last_pos
12329 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12330 cursor = glyph;
12331 cursor_x = x;
12332 }
12333 x += glyph->pixel_width;
12334 ++glyph;
12335 }
12336 while (glyph < end && EQ (glyph->object, string_start->object));
12337 }
12338 }
12339
12340 if (cursor != NULL)
12341 {
12342 glyph = cursor;
12343 x = cursor_x;
12344 }
12345 else if (row->ends_in_ellipsis_p && glyph == end)
12346 {
12347 /* Scan back over the ellipsis glyphs, decrementing positions. */
12348 while (glyph > row->glyphs[TEXT_AREA]
12349 && (glyph - 1)->charpos == last_pos)
12350 glyph--, x -= glyph->pixel_width;
12351 /* That loop always goes one position too far,
12352 including the glyph before the ellipsis.
12353 So scan forward over that one. */
12354 x += glyph->pixel_width;
12355 glyph++;
12356 }
12357 else if (string_start
12358 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12359 {
12360 /* We may have skipped over point because the previous glyphs
12361 are from string. As there's no easy way to know the
12362 character position of the current glyph, find the correct
12363 glyph on point by scanning from string_start again. */
12364 Lisp_Object limit;
12365 Lisp_Object string;
12366 struct glyph *stop = glyph;
12367 int pos;
12368
12369 limit = make_number (pt_old + 1);
12370 glyph = string_start;
12371 x = string_start_x;
12372 string = glyph->object;
12373 pos = string_buffer_position (w, string, string_before_pos);
12374 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12375 because we always put cursor after overlay strings. */
12376 while (pos == 0 && glyph < stop)
12377 {
12378 string = glyph->object;
12379 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12380 if (glyph < stop)
12381 pos = string_buffer_position (w, glyph->object, string_before_pos);
12382 }
12383
12384 while (glyph < stop)
12385 {
12386 pos = XINT (Fnext_single_char_property_change
12387 (make_number (pos), Qdisplay, Qnil, limit));
12388 if (pos > pt_old)
12389 break;
12390 /* Skip glyphs from the same string. */
12391 string = glyph->object;
12392 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12393 /* Skip glyphs from an overlay. */
12394 while (glyph < stop
12395 && ! string_buffer_position (w, glyph->object, pos))
12396 {
12397 string = glyph->object;
12398 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12399 }
12400 }
12401
12402 /* If we reached the end of the line, and end was from a string,
12403 cursor is not on this line. */
12404 if (glyph == end && row->continued_p)
12405 return 0;
12406 }
12407
12408 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12409 w->cursor.x = x;
12410 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12411 w->cursor.y = row->y + dy;
12412
12413 if (w == XWINDOW (selected_window))
12414 {
12415 if (!row->continued_p
12416 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12417 && row->x == 0)
12418 {
12419 this_line_buffer = XBUFFER (w->buffer);
12420
12421 CHARPOS (this_line_start_pos)
12422 = MATRIX_ROW_START_CHARPOS (row) + delta;
12423 BYTEPOS (this_line_start_pos)
12424 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12425
12426 CHARPOS (this_line_end_pos)
12427 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12428 BYTEPOS (this_line_end_pos)
12429 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12430
12431 this_line_y = w->cursor.y;
12432 this_line_pixel_height = row->height;
12433 this_line_vpos = w->cursor.vpos;
12434 this_line_start_x = row->x;
12435 }
12436 else
12437 CHARPOS (this_line_start_pos) = 0;
12438 }
12439
12440 return 1;
12441 }
12442
12443
12444 /* Run window scroll functions, if any, for WINDOW with new window
12445 start STARTP. Sets the window start of WINDOW to that position.
12446
12447 We assume that the window's buffer is really current. */
12448
12449 static INLINE struct text_pos
12450 run_window_scroll_functions (window, startp)
12451 Lisp_Object window;
12452 struct text_pos startp;
12453 {
12454 struct window *w = XWINDOW (window);
12455 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12456
12457 if (current_buffer != XBUFFER (w->buffer))
12458 abort ();
12459
12460 if (!NILP (Vwindow_scroll_functions))
12461 {
12462 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12463 make_number (CHARPOS (startp)));
12464 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12465 /* In case the hook functions switch buffers. */
12466 if (current_buffer != XBUFFER (w->buffer))
12467 set_buffer_internal_1 (XBUFFER (w->buffer));
12468 }
12469
12470 return startp;
12471 }
12472
12473
12474 /* Make sure the line containing the cursor is fully visible.
12475 A value of 1 means there is nothing to be done.
12476 (Either the line is fully visible, or it cannot be made so,
12477 or we cannot tell.)
12478
12479 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12480 is higher than window.
12481
12482 A value of 0 means the caller should do scrolling
12483 as if point had gone off the screen. */
12484
12485 static int
12486 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12487 struct window *w;
12488 int force_p;
12489 int current_matrix_p;
12490 {
12491 struct glyph_matrix *matrix;
12492 struct glyph_row *row;
12493 int window_height;
12494
12495 if (!make_cursor_line_fully_visible_p)
12496 return 1;
12497
12498 /* It's not always possible to find the cursor, e.g, when a window
12499 is full of overlay strings. Don't do anything in that case. */
12500 if (w->cursor.vpos < 0)
12501 return 1;
12502
12503 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12504 row = MATRIX_ROW (matrix, w->cursor.vpos);
12505
12506 /* If the cursor row is not partially visible, there's nothing to do. */
12507 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12508 return 1;
12509
12510 /* If the row the cursor is in is taller than the window's height,
12511 it's not clear what to do, so do nothing. */
12512 window_height = window_box_height (w);
12513 if (row->height >= window_height)
12514 {
12515 if (!force_p || MINI_WINDOW_P (w)
12516 || w->vscroll || w->cursor.vpos == 0)
12517 return 1;
12518 }
12519 return 0;
12520
12521 #if 0
12522 /* This code used to try to scroll the window just enough to make
12523 the line visible. It returned 0 to say that the caller should
12524 allocate larger glyph matrices. */
12525
12526 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12527 {
12528 int dy = row->height - row->visible_height;
12529 w->vscroll = 0;
12530 w->cursor.y += dy;
12531 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12532 }
12533 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12534 {
12535 int dy = - (row->height - row->visible_height);
12536 w->vscroll = dy;
12537 w->cursor.y += dy;
12538 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12539 }
12540
12541 /* When we change the cursor y-position of the selected window,
12542 change this_line_y as well so that the display optimization for
12543 the cursor line of the selected window in redisplay_internal uses
12544 the correct y-position. */
12545 if (w == XWINDOW (selected_window))
12546 this_line_y = w->cursor.y;
12547
12548 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12549 redisplay with larger matrices. */
12550 if (matrix->nrows < required_matrix_height (w))
12551 {
12552 fonts_changed_p = 1;
12553 return 0;
12554 }
12555
12556 return 1;
12557 #endif /* 0 */
12558 }
12559
12560
12561 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12562 non-zero means only WINDOW is redisplayed in redisplay_internal.
12563 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12564 in redisplay_window to bring a partially visible line into view in
12565 the case that only the cursor has moved.
12566
12567 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12568 last screen line's vertical height extends past the end of the screen.
12569
12570 Value is
12571
12572 1 if scrolling succeeded
12573
12574 0 if scrolling didn't find point.
12575
12576 -1 if new fonts have been loaded so that we must interrupt
12577 redisplay, adjust glyph matrices, and try again. */
12578
12579 enum
12580 {
12581 SCROLLING_SUCCESS,
12582 SCROLLING_FAILED,
12583 SCROLLING_NEED_LARGER_MATRICES
12584 };
12585
12586 static int
12587 try_scrolling (window, just_this_one_p, scroll_conservatively,
12588 scroll_step, temp_scroll_step, last_line_misfit)
12589 Lisp_Object window;
12590 int just_this_one_p;
12591 EMACS_INT scroll_conservatively, scroll_step;
12592 int temp_scroll_step;
12593 int last_line_misfit;
12594 {
12595 struct window *w = XWINDOW (window);
12596 struct frame *f = XFRAME (w->frame);
12597 struct text_pos scroll_margin_pos;
12598 struct text_pos pos;
12599 struct text_pos startp;
12600 struct it it;
12601 Lisp_Object window_end;
12602 int this_scroll_margin;
12603 int dy = 0;
12604 int scroll_max;
12605 int rc;
12606 int amount_to_scroll = 0;
12607 Lisp_Object aggressive;
12608 int height;
12609 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12610
12611 #if GLYPH_DEBUG
12612 debug_method_add (w, "try_scrolling");
12613 #endif
12614
12615 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12616
12617 /* Compute scroll margin height in pixels. We scroll when point is
12618 within this distance from the top or bottom of the window. */
12619 if (scroll_margin > 0)
12620 {
12621 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12622 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12623 }
12624 else
12625 this_scroll_margin = 0;
12626
12627 /* Force scroll_conservatively to have a reasonable value so it doesn't
12628 cause an overflow while computing how much to scroll. */
12629 if (scroll_conservatively)
12630 scroll_conservatively = min (scroll_conservatively,
12631 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12632
12633 /* Compute how much we should try to scroll maximally to bring point
12634 into view. */
12635 if (scroll_step || scroll_conservatively || temp_scroll_step)
12636 scroll_max = max (scroll_step,
12637 max (scroll_conservatively, temp_scroll_step));
12638 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12639 || NUMBERP (current_buffer->scroll_up_aggressively))
12640 /* We're trying to scroll because of aggressive scrolling
12641 but no scroll_step is set. Choose an arbitrary one. Maybe
12642 there should be a variable for this. */
12643 scroll_max = 10;
12644 else
12645 scroll_max = 0;
12646 scroll_max *= FRAME_LINE_HEIGHT (f);
12647
12648 /* Decide whether we have to scroll down. Start at the window end
12649 and move this_scroll_margin up to find the position of the scroll
12650 margin. */
12651 window_end = Fwindow_end (window, Qt);
12652
12653 too_near_end:
12654
12655 CHARPOS (scroll_margin_pos) = XINT (window_end);
12656 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12657
12658 if (this_scroll_margin || extra_scroll_margin_lines)
12659 {
12660 start_display (&it, w, scroll_margin_pos);
12661 if (this_scroll_margin)
12662 move_it_vertically_backward (&it, this_scroll_margin);
12663 if (extra_scroll_margin_lines)
12664 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12665 scroll_margin_pos = it.current.pos;
12666 }
12667
12668 if (PT >= CHARPOS (scroll_margin_pos))
12669 {
12670 int y0;
12671
12672 /* Point is in the scroll margin at the bottom of the window, or
12673 below. Compute a new window start that makes point visible. */
12674
12675 /* Compute the distance from the scroll margin to PT.
12676 Give up if the distance is greater than scroll_max. */
12677 start_display (&it, w, scroll_margin_pos);
12678 y0 = it.current_y;
12679 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12680 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12681
12682 /* To make point visible, we have to move the window start
12683 down so that the line the cursor is in is visible, which
12684 means we have to add in the height of the cursor line. */
12685 dy = line_bottom_y (&it) - y0;
12686
12687 if (dy > scroll_max)
12688 return SCROLLING_FAILED;
12689
12690 /* Move the window start down. If scrolling conservatively,
12691 move it just enough down to make point visible. If
12692 scroll_step is set, move it down by scroll_step. */
12693 start_display (&it, w, startp);
12694
12695 if (scroll_conservatively)
12696 /* Set AMOUNT_TO_SCROLL to at least one line,
12697 and at most scroll_conservatively lines. */
12698 amount_to_scroll
12699 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12700 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12701 else if (scroll_step || temp_scroll_step)
12702 amount_to_scroll = scroll_max;
12703 else
12704 {
12705 aggressive = current_buffer->scroll_up_aggressively;
12706 height = WINDOW_BOX_TEXT_HEIGHT (w);
12707 if (NUMBERP (aggressive))
12708 {
12709 double float_amount = XFLOATINT (aggressive) * height;
12710 amount_to_scroll = float_amount;
12711 if (amount_to_scroll == 0 && float_amount > 0)
12712 amount_to_scroll = 1;
12713 }
12714 }
12715
12716 if (amount_to_scroll <= 0)
12717 return SCROLLING_FAILED;
12718
12719 /* If moving by amount_to_scroll leaves STARTP unchanged,
12720 move it down one screen line. */
12721
12722 move_it_vertically (&it, amount_to_scroll);
12723 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12724 move_it_by_lines (&it, 1, 1);
12725 startp = it.current.pos;
12726 }
12727 else
12728 {
12729 /* See if point is inside the scroll margin at the top of the
12730 window. */
12731 scroll_margin_pos = startp;
12732 if (this_scroll_margin)
12733 {
12734 start_display (&it, w, startp);
12735 move_it_vertically (&it, this_scroll_margin);
12736 scroll_margin_pos = it.current.pos;
12737 }
12738
12739 if (PT < CHARPOS (scroll_margin_pos))
12740 {
12741 /* Point is in the scroll margin at the top of the window or
12742 above what is displayed in the window. */
12743 int y0;
12744
12745 /* Compute the vertical distance from PT to the scroll
12746 margin position. Give up if distance is greater than
12747 scroll_max. */
12748 SET_TEXT_POS (pos, PT, PT_BYTE);
12749 start_display (&it, w, pos);
12750 y0 = it.current_y;
12751 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12752 it.last_visible_y, -1,
12753 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12754 dy = it.current_y - y0;
12755 if (dy > scroll_max)
12756 return SCROLLING_FAILED;
12757
12758 /* Compute new window start. */
12759 start_display (&it, w, startp);
12760
12761 if (scroll_conservatively)
12762 amount_to_scroll
12763 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12764 else if (scroll_step || temp_scroll_step)
12765 amount_to_scroll = scroll_max;
12766 else
12767 {
12768 aggressive = current_buffer->scroll_down_aggressively;
12769 height = WINDOW_BOX_TEXT_HEIGHT (w);
12770 if (NUMBERP (aggressive))
12771 {
12772 double float_amount = XFLOATINT (aggressive) * height;
12773 amount_to_scroll = float_amount;
12774 if (amount_to_scroll == 0 && float_amount > 0)
12775 amount_to_scroll = 1;
12776 }
12777 }
12778
12779 if (amount_to_scroll <= 0)
12780 return SCROLLING_FAILED;
12781
12782 move_it_vertically_backward (&it, amount_to_scroll);
12783 startp = it.current.pos;
12784 }
12785 }
12786
12787 /* Run window scroll functions. */
12788 startp = run_window_scroll_functions (window, startp);
12789
12790 /* Display the window. Give up if new fonts are loaded, or if point
12791 doesn't appear. */
12792 if (!try_window (window, startp, 0))
12793 rc = SCROLLING_NEED_LARGER_MATRICES;
12794 else if (w->cursor.vpos < 0)
12795 {
12796 clear_glyph_matrix (w->desired_matrix);
12797 rc = SCROLLING_FAILED;
12798 }
12799 else
12800 {
12801 /* Maybe forget recorded base line for line number display. */
12802 if (!just_this_one_p
12803 || current_buffer->clip_changed
12804 || BEG_UNCHANGED < CHARPOS (startp))
12805 w->base_line_number = Qnil;
12806
12807 /* If cursor ends up on a partially visible line,
12808 treat that as being off the bottom of the screen. */
12809 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12810 {
12811 clear_glyph_matrix (w->desired_matrix);
12812 ++extra_scroll_margin_lines;
12813 goto too_near_end;
12814 }
12815 rc = SCROLLING_SUCCESS;
12816 }
12817
12818 return rc;
12819 }
12820
12821
12822 /* Compute a suitable window start for window W if display of W starts
12823 on a continuation line. Value is non-zero if a new window start
12824 was computed.
12825
12826 The new window start will be computed, based on W's width, starting
12827 from the start of the continued line. It is the start of the
12828 screen line with the minimum distance from the old start W->start. */
12829
12830 static int
12831 compute_window_start_on_continuation_line (w)
12832 struct window *w;
12833 {
12834 struct text_pos pos, start_pos;
12835 int window_start_changed_p = 0;
12836
12837 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12838
12839 /* If window start is on a continuation line... Window start may be
12840 < BEGV in case there's invisible text at the start of the
12841 buffer (M-x rmail, for example). */
12842 if (CHARPOS (start_pos) > BEGV
12843 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12844 {
12845 struct it it;
12846 struct glyph_row *row;
12847
12848 /* Handle the case that the window start is out of range. */
12849 if (CHARPOS (start_pos) < BEGV)
12850 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12851 else if (CHARPOS (start_pos) > ZV)
12852 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12853
12854 /* Find the start of the continued line. This should be fast
12855 because scan_buffer is fast (newline cache). */
12856 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12857 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12858 row, DEFAULT_FACE_ID);
12859 reseat_at_previous_visible_line_start (&it);
12860
12861 /* If the line start is "too far" away from the window start,
12862 say it takes too much time to compute a new window start. */
12863 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12864 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12865 {
12866 int min_distance, distance;
12867
12868 /* Move forward by display lines to find the new window
12869 start. If window width was enlarged, the new start can
12870 be expected to be > the old start. If window width was
12871 decreased, the new window start will be < the old start.
12872 So, we're looking for the display line start with the
12873 minimum distance from the old window start. */
12874 pos = it.current.pos;
12875 min_distance = INFINITY;
12876 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12877 distance < min_distance)
12878 {
12879 min_distance = distance;
12880 pos = it.current.pos;
12881 move_it_by_lines (&it, 1, 0);
12882 }
12883
12884 /* Set the window start there. */
12885 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12886 window_start_changed_p = 1;
12887 }
12888 }
12889
12890 return window_start_changed_p;
12891 }
12892
12893
12894 /* Try cursor movement in case text has not changed in window WINDOW,
12895 with window start STARTP. Value is
12896
12897 CURSOR_MOVEMENT_SUCCESS if successful
12898
12899 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12900
12901 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12902 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12903 we want to scroll as if scroll-step were set to 1. See the code.
12904
12905 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12906 which case we have to abort this redisplay, and adjust matrices
12907 first. */
12908
12909 enum
12910 {
12911 CURSOR_MOVEMENT_SUCCESS,
12912 CURSOR_MOVEMENT_CANNOT_BE_USED,
12913 CURSOR_MOVEMENT_MUST_SCROLL,
12914 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12915 };
12916
12917 static int
12918 try_cursor_movement (window, startp, scroll_step)
12919 Lisp_Object window;
12920 struct text_pos startp;
12921 int *scroll_step;
12922 {
12923 struct window *w = XWINDOW (window);
12924 struct frame *f = XFRAME (w->frame);
12925 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12926
12927 #if GLYPH_DEBUG
12928 if (inhibit_try_cursor_movement)
12929 return rc;
12930 #endif
12931
12932 /* Handle case where text has not changed, only point, and it has
12933 not moved off the frame. */
12934 if (/* Point may be in this window. */
12935 PT >= CHARPOS (startp)
12936 /* Selective display hasn't changed. */
12937 && !current_buffer->clip_changed
12938 /* Function force-mode-line-update is used to force a thorough
12939 redisplay. It sets either windows_or_buffers_changed or
12940 update_mode_lines. So don't take a shortcut here for these
12941 cases. */
12942 && !update_mode_lines
12943 && !windows_or_buffers_changed
12944 && !cursor_type_changed
12945 /* Can't use this case if highlighting a region. When a
12946 region exists, cursor movement has to do more than just
12947 set the cursor. */
12948 && !(!NILP (Vtransient_mark_mode)
12949 && !NILP (current_buffer->mark_active))
12950 && NILP (w->region_showing)
12951 && NILP (Vshow_trailing_whitespace)
12952 /* Right after splitting windows, last_point may be nil. */
12953 && INTEGERP (w->last_point)
12954 /* This code is not used for mini-buffer for the sake of the case
12955 of redisplaying to replace an echo area message; since in
12956 that case the mini-buffer contents per se are usually
12957 unchanged. This code is of no real use in the mini-buffer
12958 since the handling of this_line_start_pos, etc., in redisplay
12959 handles the same cases. */
12960 && !EQ (window, minibuf_window)
12961 /* When splitting windows or for new windows, it happens that
12962 redisplay is called with a nil window_end_vpos or one being
12963 larger than the window. This should really be fixed in
12964 window.c. I don't have this on my list, now, so we do
12965 approximately the same as the old redisplay code. --gerd. */
12966 && INTEGERP (w->window_end_vpos)
12967 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12968 && (FRAME_WINDOW_P (f)
12969 || !overlay_arrow_in_current_buffer_p ()))
12970 {
12971 int this_scroll_margin, top_scroll_margin;
12972 struct glyph_row *row = NULL;
12973
12974 #if GLYPH_DEBUG
12975 debug_method_add (w, "cursor movement");
12976 #endif
12977
12978 /* Scroll if point within this distance from the top or bottom
12979 of the window. This is a pixel value. */
12980 this_scroll_margin = max (0, scroll_margin);
12981 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12982 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12983
12984 top_scroll_margin = this_scroll_margin;
12985 if (WINDOW_WANTS_HEADER_LINE_P (w))
12986 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12987
12988 /* Start with the row the cursor was displayed during the last
12989 not paused redisplay. Give up if that row is not valid. */
12990 if (w->last_cursor.vpos < 0
12991 || w->last_cursor.vpos >= w->current_matrix->nrows)
12992 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12993 else
12994 {
12995 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12996 if (row->mode_line_p)
12997 ++row;
12998 if (!row->enabled_p)
12999 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13000 }
13001
13002 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13003 {
13004 int scroll_p = 0;
13005 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13006
13007 if (PT > XFASTINT (w->last_point))
13008 {
13009 /* Point has moved forward. */
13010 while (MATRIX_ROW_END_CHARPOS (row) < PT
13011 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13012 {
13013 xassert (row->enabled_p);
13014 ++row;
13015 }
13016
13017 /* The end position of a row equals the start position
13018 of the next row. If PT is there, we would rather
13019 display it in the next line. */
13020 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13021 && MATRIX_ROW_END_CHARPOS (row) == PT
13022 && !cursor_row_p (w, row))
13023 ++row;
13024
13025 /* If within the scroll margin, scroll. Note that
13026 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13027 the next line would be drawn, and that
13028 this_scroll_margin can be zero. */
13029 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13030 || PT > MATRIX_ROW_END_CHARPOS (row)
13031 /* Line is completely visible last line in window
13032 and PT is to be set in the next line. */
13033 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13034 && PT == MATRIX_ROW_END_CHARPOS (row)
13035 && !row->ends_at_zv_p
13036 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13037 scroll_p = 1;
13038 }
13039 else if (PT < XFASTINT (w->last_point))
13040 {
13041 /* Cursor has to be moved backward. Note that PT >=
13042 CHARPOS (startp) because of the outer if-statement. */
13043 while (!row->mode_line_p
13044 && (MATRIX_ROW_START_CHARPOS (row) > PT
13045 || (MATRIX_ROW_START_CHARPOS (row) == PT
13046 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13047 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13048 row > w->current_matrix->rows
13049 && (row-1)->ends_in_newline_from_string_p))))
13050 && (row->y > top_scroll_margin
13051 || CHARPOS (startp) == BEGV))
13052 {
13053 xassert (row->enabled_p);
13054 --row;
13055 }
13056
13057 /* Consider the following case: Window starts at BEGV,
13058 there is invisible, intangible text at BEGV, so that
13059 display starts at some point START > BEGV. It can
13060 happen that we are called with PT somewhere between
13061 BEGV and START. Try to handle that case. */
13062 if (row < w->current_matrix->rows
13063 || row->mode_line_p)
13064 {
13065 row = w->current_matrix->rows;
13066 if (row->mode_line_p)
13067 ++row;
13068 }
13069
13070 /* Due to newlines in overlay strings, we may have to
13071 skip forward over overlay strings. */
13072 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13073 && MATRIX_ROW_END_CHARPOS (row) == PT
13074 && !cursor_row_p (w, row))
13075 ++row;
13076
13077 /* If within the scroll margin, scroll. */
13078 if (row->y < top_scroll_margin
13079 && CHARPOS (startp) != BEGV)
13080 scroll_p = 1;
13081 }
13082 else
13083 {
13084 /* Cursor did not move. So don't scroll even if cursor line
13085 is partially visible, as it was so before. */
13086 rc = CURSOR_MOVEMENT_SUCCESS;
13087 }
13088
13089 if (PT < MATRIX_ROW_START_CHARPOS (row)
13090 || PT > MATRIX_ROW_END_CHARPOS (row))
13091 {
13092 /* if PT is not in the glyph row, give up. */
13093 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13094 }
13095 else if (rc != CURSOR_MOVEMENT_SUCCESS
13096 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13097 && make_cursor_line_fully_visible_p)
13098 {
13099 if (PT == MATRIX_ROW_END_CHARPOS (row)
13100 && !row->ends_at_zv_p
13101 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13102 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13103 else if (row->height > window_box_height (w))
13104 {
13105 /* If we end up in a partially visible line, let's
13106 make it fully visible, except when it's taller
13107 than the window, in which case we can't do much
13108 about it. */
13109 *scroll_step = 1;
13110 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13111 }
13112 else
13113 {
13114 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13115 if (!cursor_row_fully_visible_p (w, 0, 1))
13116 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13117 else
13118 rc = CURSOR_MOVEMENT_SUCCESS;
13119 }
13120 }
13121 else if (scroll_p)
13122 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13123 else
13124 {
13125 do
13126 {
13127 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13128 {
13129 rc = CURSOR_MOVEMENT_SUCCESS;
13130 break;
13131 }
13132 ++row;
13133 }
13134 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13135 && MATRIX_ROW_START_CHARPOS (row) == PT
13136 && cursor_row_p (w, row));
13137 }
13138 }
13139 }
13140
13141 return rc;
13142 }
13143
13144 void
13145 set_vertical_scroll_bar (w)
13146 struct window *w;
13147 {
13148 int start, end, whole;
13149
13150 /* Calculate the start and end positions for the current window.
13151 At some point, it would be nice to choose between scrollbars
13152 which reflect the whole buffer size, with special markers
13153 indicating narrowing, and scrollbars which reflect only the
13154 visible region.
13155
13156 Note that mini-buffers sometimes aren't displaying any text. */
13157 if (!MINI_WINDOW_P (w)
13158 || (w == XWINDOW (minibuf_window)
13159 && NILP (echo_area_buffer[0])))
13160 {
13161 struct buffer *buf = XBUFFER (w->buffer);
13162 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13163 start = marker_position (w->start) - BUF_BEGV (buf);
13164 /* I don't think this is guaranteed to be right. For the
13165 moment, we'll pretend it is. */
13166 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13167
13168 if (end < start)
13169 end = start;
13170 if (whole < (end - start))
13171 whole = end - start;
13172 }
13173 else
13174 start = end = whole = 0;
13175
13176 /* Indicate what this scroll bar ought to be displaying now. */
13177 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13178 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13179 (w, end - start, whole, start);
13180 }
13181
13182
13183 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13184 selected_window is redisplayed.
13185
13186 We can return without actually redisplaying the window if
13187 fonts_changed_p is nonzero. In that case, redisplay_internal will
13188 retry. */
13189
13190 static void
13191 redisplay_window (window, just_this_one_p)
13192 Lisp_Object window;
13193 int just_this_one_p;
13194 {
13195 struct window *w = XWINDOW (window);
13196 struct frame *f = XFRAME (w->frame);
13197 struct buffer *buffer = XBUFFER (w->buffer);
13198 struct buffer *old = current_buffer;
13199 struct text_pos lpoint, opoint, startp;
13200 int update_mode_line;
13201 int tem;
13202 struct it it;
13203 /* Record it now because it's overwritten. */
13204 int current_matrix_up_to_date_p = 0;
13205 int used_current_matrix_p = 0;
13206 /* This is less strict than current_matrix_up_to_date_p.
13207 It indictes that the buffer contents and narrowing are unchanged. */
13208 int buffer_unchanged_p = 0;
13209 int temp_scroll_step = 0;
13210 int count = SPECPDL_INDEX ();
13211 int rc;
13212 int centering_position = -1;
13213 int last_line_misfit = 0;
13214 int beg_unchanged, end_unchanged;
13215
13216 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13217 opoint = lpoint;
13218
13219 /* W must be a leaf window here. */
13220 xassert (!NILP (w->buffer));
13221 #if GLYPH_DEBUG
13222 *w->desired_matrix->method = 0;
13223 #endif
13224
13225 restart:
13226 reconsider_clip_changes (w, buffer);
13227
13228 /* Has the mode line to be updated? */
13229 update_mode_line = (!NILP (w->update_mode_line)
13230 || update_mode_lines
13231 || buffer->clip_changed
13232 || buffer->prevent_redisplay_optimizations_p);
13233
13234 if (MINI_WINDOW_P (w))
13235 {
13236 if (w == XWINDOW (echo_area_window)
13237 && !NILP (echo_area_buffer[0]))
13238 {
13239 if (update_mode_line)
13240 /* We may have to update a tty frame's menu bar or a
13241 tool-bar. Example `M-x C-h C-h C-g'. */
13242 goto finish_menu_bars;
13243 else
13244 /* We've already displayed the echo area glyphs in this window. */
13245 goto finish_scroll_bars;
13246 }
13247 else if ((w != XWINDOW (minibuf_window)
13248 || minibuf_level == 0)
13249 /* When buffer is nonempty, redisplay window normally. */
13250 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13251 /* Quail displays non-mini buffers in minibuffer window.
13252 In that case, redisplay the window normally. */
13253 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13254 {
13255 /* W is a mini-buffer window, but it's not active, so clear
13256 it. */
13257 int yb = window_text_bottom_y (w);
13258 struct glyph_row *row;
13259 int y;
13260
13261 for (y = 0, row = w->desired_matrix->rows;
13262 y < yb;
13263 y += row->height, ++row)
13264 blank_row (w, row, y);
13265 goto finish_scroll_bars;
13266 }
13267
13268 clear_glyph_matrix (w->desired_matrix);
13269 }
13270
13271 /* Otherwise set up data on this window; select its buffer and point
13272 value. */
13273 /* Really select the buffer, for the sake of buffer-local
13274 variables. */
13275 set_buffer_internal_1 (XBUFFER (w->buffer));
13276
13277 current_matrix_up_to_date_p
13278 = (!NILP (w->window_end_valid)
13279 && !current_buffer->clip_changed
13280 && !current_buffer->prevent_redisplay_optimizations_p
13281 && XFASTINT (w->last_modified) >= MODIFF
13282 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13283
13284 /* Run the window-bottom-change-functions
13285 if it is possible that the text on the screen has changed
13286 (either due to modification of the text, or any other reason). */
13287 if (!current_matrix_up_to_date_p
13288 && !NILP (Vwindow_text_change_functions))
13289 {
13290 safe_run_hooks (Qwindow_text_change_functions);
13291 goto restart;
13292 }
13293
13294 beg_unchanged = BEG_UNCHANGED;
13295 end_unchanged = END_UNCHANGED;
13296
13297 SET_TEXT_POS (opoint, PT, PT_BYTE);
13298
13299 specbind (Qinhibit_point_motion_hooks, Qt);
13300
13301 buffer_unchanged_p
13302 = (!NILP (w->window_end_valid)
13303 && !current_buffer->clip_changed
13304 && XFASTINT (w->last_modified) >= MODIFF
13305 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13306
13307 /* When windows_or_buffers_changed is non-zero, we can't rely on
13308 the window end being valid, so set it to nil there. */
13309 if (windows_or_buffers_changed)
13310 {
13311 /* If window starts on a continuation line, maybe adjust the
13312 window start in case the window's width changed. */
13313 if (XMARKER (w->start)->buffer == current_buffer)
13314 compute_window_start_on_continuation_line (w);
13315
13316 w->window_end_valid = Qnil;
13317 }
13318
13319 /* Some sanity checks. */
13320 CHECK_WINDOW_END (w);
13321 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13322 abort ();
13323 if (BYTEPOS (opoint) < CHARPOS (opoint))
13324 abort ();
13325
13326 /* If %c is in mode line, update it if needed. */
13327 if (!NILP (w->column_number_displayed)
13328 /* This alternative quickly identifies a common case
13329 where no change is needed. */
13330 && !(PT == XFASTINT (w->last_point)
13331 && XFASTINT (w->last_modified) >= MODIFF
13332 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13333 && (XFASTINT (w->column_number_displayed)
13334 != (int) current_column ())) /* iftc */
13335 update_mode_line = 1;
13336
13337 /* Count number of windows showing the selected buffer. An indirect
13338 buffer counts as its base buffer. */
13339 if (!just_this_one_p)
13340 {
13341 struct buffer *current_base, *window_base;
13342 current_base = current_buffer;
13343 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13344 if (current_base->base_buffer)
13345 current_base = current_base->base_buffer;
13346 if (window_base->base_buffer)
13347 window_base = window_base->base_buffer;
13348 if (current_base == window_base)
13349 buffer_shared++;
13350 }
13351
13352 /* Point refers normally to the selected window. For any other
13353 window, set up appropriate value. */
13354 if (!EQ (window, selected_window))
13355 {
13356 int new_pt = XMARKER (w->pointm)->charpos;
13357 int new_pt_byte = marker_byte_position (w->pointm);
13358 if (new_pt < BEGV)
13359 {
13360 new_pt = BEGV;
13361 new_pt_byte = BEGV_BYTE;
13362 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13363 }
13364 else if (new_pt > (ZV - 1))
13365 {
13366 new_pt = ZV;
13367 new_pt_byte = ZV_BYTE;
13368 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13369 }
13370
13371 /* We don't use SET_PT so that the point-motion hooks don't run. */
13372 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13373 }
13374
13375 /* If any of the character widths specified in the display table
13376 have changed, invalidate the width run cache. It's true that
13377 this may be a bit late to catch such changes, but the rest of
13378 redisplay goes (non-fatally) haywire when the display table is
13379 changed, so why should we worry about doing any better? */
13380 if (current_buffer->width_run_cache)
13381 {
13382 struct Lisp_Char_Table *disptab = buffer_display_table ();
13383
13384 if (! disptab_matches_widthtab (disptab,
13385 XVECTOR (current_buffer->width_table)))
13386 {
13387 invalidate_region_cache (current_buffer,
13388 current_buffer->width_run_cache,
13389 BEG, Z);
13390 recompute_width_table (current_buffer, disptab);
13391 }
13392 }
13393
13394 /* If window-start is screwed up, choose a new one. */
13395 if (XMARKER (w->start)->buffer != current_buffer)
13396 goto recenter;
13397
13398 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13399
13400 /* If someone specified a new starting point but did not insist,
13401 check whether it can be used. */
13402 if (!NILP (w->optional_new_start)
13403 && CHARPOS (startp) >= BEGV
13404 && CHARPOS (startp) <= ZV)
13405 {
13406 w->optional_new_start = Qnil;
13407 start_display (&it, w, startp);
13408 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13409 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13410 if (IT_CHARPOS (it) == PT)
13411 w->force_start = Qt;
13412 /* IT may overshoot PT if text at PT is invisible. */
13413 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13414 w->force_start = Qt;
13415 }
13416
13417 force_start:
13418
13419 /* Handle case where place to start displaying has been specified,
13420 unless the specified location is outside the accessible range. */
13421 if (!NILP (w->force_start)
13422 || w->frozen_window_start_p)
13423 {
13424 /* We set this later on if we have to adjust point. */
13425 int new_vpos = -1;
13426 int val;
13427
13428 w->force_start = Qnil;
13429 w->vscroll = 0;
13430 w->window_end_valid = Qnil;
13431
13432 /* Forget any recorded base line for line number display. */
13433 if (!buffer_unchanged_p)
13434 w->base_line_number = Qnil;
13435
13436 /* Redisplay the mode line. Select the buffer properly for that.
13437 Also, run the hook window-scroll-functions
13438 because we have scrolled. */
13439 /* Note, we do this after clearing force_start because
13440 if there's an error, it is better to forget about force_start
13441 than to get into an infinite loop calling the hook functions
13442 and having them get more errors. */
13443 if (!update_mode_line
13444 || ! NILP (Vwindow_scroll_functions))
13445 {
13446 update_mode_line = 1;
13447 w->update_mode_line = Qt;
13448 startp = run_window_scroll_functions (window, startp);
13449 }
13450
13451 w->last_modified = make_number (0);
13452 w->last_overlay_modified = make_number (0);
13453 if (CHARPOS (startp) < BEGV)
13454 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13455 else if (CHARPOS (startp) > ZV)
13456 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13457
13458 /* Redisplay, then check if cursor has been set during the
13459 redisplay. Give up if new fonts were loaded. */
13460 val = try_window (window, startp, 1);
13461 if (!val)
13462 {
13463 w->force_start = Qt;
13464 clear_glyph_matrix (w->desired_matrix);
13465 goto need_larger_matrices;
13466 }
13467 /* Point was outside the scroll margins. */
13468 if (val < 0)
13469 new_vpos = window_box_height (w) / 2;
13470
13471 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13472 {
13473 /* If point does not appear, try to move point so it does
13474 appear. The desired matrix has been built above, so we
13475 can use it here. */
13476 new_vpos = window_box_height (w) / 2;
13477 }
13478
13479 if (!cursor_row_fully_visible_p (w, 0, 0))
13480 {
13481 /* Point does appear, but on a line partly visible at end of window.
13482 Move it back to a fully-visible line. */
13483 new_vpos = window_box_height (w);
13484 }
13485
13486 /* If we need to move point for either of the above reasons,
13487 now actually do it. */
13488 if (new_vpos >= 0)
13489 {
13490 struct glyph_row *row;
13491
13492 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13493 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13494 ++row;
13495
13496 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13497 MATRIX_ROW_START_BYTEPOS (row));
13498
13499 if (w != XWINDOW (selected_window))
13500 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13501 else if (current_buffer == old)
13502 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13503
13504 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13505
13506 /* If we are highlighting the region, then we just changed
13507 the region, so redisplay to show it. */
13508 if (!NILP (Vtransient_mark_mode)
13509 && !NILP (current_buffer->mark_active))
13510 {
13511 clear_glyph_matrix (w->desired_matrix);
13512 if (!try_window (window, startp, 0))
13513 goto need_larger_matrices;
13514 }
13515 }
13516
13517 #if GLYPH_DEBUG
13518 debug_method_add (w, "forced window start");
13519 #endif
13520 goto done;
13521 }
13522
13523 /* Handle case where text has not changed, only point, and it has
13524 not moved off the frame, and we are not retrying after hscroll.
13525 (current_matrix_up_to_date_p is nonzero when retrying.) */
13526 if (current_matrix_up_to_date_p
13527 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13528 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13529 {
13530 switch (rc)
13531 {
13532 case CURSOR_MOVEMENT_SUCCESS:
13533 used_current_matrix_p = 1;
13534 goto done;
13535
13536 #if 0 /* try_cursor_movement never returns this value. */
13537 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13538 goto need_larger_matrices;
13539 #endif
13540
13541 case CURSOR_MOVEMENT_MUST_SCROLL:
13542 goto try_to_scroll;
13543
13544 default:
13545 abort ();
13546 }
13547 }
13548 /* If current starting point was originally the beginning of a line
13549 but no longer is, find a new starting point. */
13550 else if (!NILP (w->start_at_line_beg)
13551 && !(CHARPOS (startp) <= BEGV
13552 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13553 {
13554 #if GLYPH_DEBUG
13555 debug_method_add (w, "recenter 1");
13556 #endif
13557 goto recenter;
13558 }
13559
13560 /* Try scrolling with try_window_id. Value is > 0 if update has
13561 been done, it is -1 if we know that the same window start will
13562 not work. It is 0 if unsuccessful for some other reason. */
13563 else if ((tem = try_window_id (w)) != 0)
13564 {
13565 #if GLYPH_DEBUG
13566 debug_method_add (w, "try_window_id %d", tem);
13567 #endif
13568
13569 if (fonts_changed_p)
13570 goto need_larger_matrices;
13571 if (tem > 0)
13572 goto done;
13573
13574 /* Otherwise try_window_id has returned -1 which means that we
13575 don't want the alternative below this comment to execute. */
13576 }
13577 else if (CHARPOS (startp) >= BEGV
13578 && CHARPOS (startp) <= ZV
13579 && PT >= CHARPOS (startp)
13580 && (CHARPOS (startp) < ZV
13581 /* Avoid starting at end of buffer. */
13582 || CHARPOS (startp) == BEGV
13583 || (XFASTINT (w->last_modified) >= MODIFF
13584 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13585 {
13586
13587 /* If first window line is a continuation line, and window start
13588 is inside the modified region, but the first change is before
13589 current window start, we must select a new window start.
13590
13591 However, if this is the result of a down-mouse event (e.g. by
13592 extending the mouse-drag-overlay), we don't want to select a
13593 new window start, since that would change the position under
13594 the mouse, resulting in an unwanted mouse-movement rather
13595 than a simple mouse-click. */
13596 if (NILP (w->start_at_line_beg)
13597 && NILP (do_mouse_tracking)
13598 && CHARPOS (startp) > BEGV
13599 && CHARPOS (startp) > BEG + beg_unchanged
13600 && CHARPOS (startp) <= Z - end_unchanged)
13601 {
13602 w->force_start = Qt;
13603 if (XMARKER (w->start)->buffer == current_buffer)
13604 compute_window_start_on_continuation_line (w);
13605 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13606 goto force_start;
13607 }
13608
13609 #if GLYPH_DEBUG
13610 debug_method_add (w, "same window start");
13611 #endif
13612
13613 /* Try to redisplay starting at same place as before.
13614 If point has not moved off frame, accept the results. */
13615 if (!current_matrix_up_to_date_p
13616 /* Don't use try_window_reusing_current_matrix in this case
13617 because a window scroll function can have changed the
13618 buffer. */
13619 || !NILP (Vwindow_scroll_functions)
13620 || MINI_WINDOW_P (w)
13621 || !(used_current_matrix_p
13622 = try_window_reusing_current_matrix (w)))
13623 {
13624 IF_DEBUG (debug_method_add (w, "1"));
13625 if (try_window (window, startp, 1) < 0)
13626 /* -1 means we need to scroll.
13627 0 means we need new matrices, but fonts_changed_p
13628 is set in that case, so we will detect it below. */
13629 goto try_to_scroll;
13630 }
13631
13632 if (fonts_changed_p)
13633 goto need_larger_matrices;
13634
13635 if (w->cursor.vpos >= 0)
13636 {
13637 if (!just_this_one_p
13638 || current_buffer->clip_changed
13639 || BEG_UNCHANGED < CHARPOS (startp))
13640 /* Forget any recorded base line for line number display. */
13641 w->base_line_number = Qnil;
13642
13643 if (!cursor_row_fully_visible_p (w, 1, 0))
13644 {
13645 clear_glyph_matrix (w->desired_matrix);
13646 last_line_misfit = 1;
13647 }
13648 /* Drop through and scroll. */
13649 else
13650 goto done;
13651 }
13652 else
13653 clear_glyph_matrix (w->desired_matrix);
13654 }
13655
13656 try_to_scroll:
13657
13658 w->last_modified = make_number (0);
13659 w->last_overlay_modified = make_number (0);
13660
13661 /* Redisplay the mode line. Select the buffer properly for that. */
13662 if (!update_mode_line)
13663 {
13664 update_mode_line = 1;
13665 w->update_mode_line = Qt;
13666 }
13667
13668 /* Try to scroll by specified few lines. */
13669 if ((scroll_conservatively
13670 || scroll_step
13671 || temp_scroll_step
13672 || NUMBERP (current_buffer->scroll_up_aggressively)
13673 || NUMBERP (current_buffer->scroll_down_aggressively))
13674 && !current_buffer->clip_changed
13675 && CHARPOS (startp) >= BEGV
13676 && CHARPOS (startp) <= ZV)
13677 {
13678 /* The function returns -1 if new fonts were loaded, 1 if
13679 successful, 0 if not successful. */
13680 int rc = try_scrolling (window, just_this_one_p,
13681 scroll_conservatively,
13682 scroll_step,
13683 temp_scroll_step, last_line_misfit);
13684 switch (rc)
13685 {
13686 case SCROLLING_SUCCESS:
13687 goto done;
13688
13689 case SCROLLING_NEED_LARGER_MATRICES:
13690 goto need_larger_matrices;
13691
13692 case SCROLLING_FAILED:
13693 break;
13694
13695 default:
13696 abort ();
13697 }
13698 }
13699
13700 /* Finally, just choose place to start which centers point */
13701
13702 recenter:
13703 if (centering_position < 0)
13704 centering_position = window_box_height (w) / 2;
13705
13706 #if GLYPH_DEBUG
13707 debug_method_add (w, "recenter");
13708 #endif
13709
13710 /* w->vscroll = 0; */
13711
13712 /* Forget any previously recorded base line for line number display. */
13713 if (!buffer_unchanged_p)
13714 w->base_line_number = Qnil;
13715
13716 /* Move backward half the height of the window. */
13717 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13718 it.current_y = it.last_visible_y;
13719 move_it_vertically_backward (&it, centering_position);
13720 xassert (IT_CHARPOS (it) >= BEGV);
13721
13722 /* The function move_it_vertically_backward may move over more
13723 than the specified y-distance. If it->w is small, e.g. a
13724 mini-buffer window, we may end up in front of the window's
13725 display area. Start displaying at the start of the line
13726 containing PT in this case. */
13727 if (it.current_y <= 0)
13728 {
13729 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13730 move_it_vertically_backward (&it, 0);
13731 #if 0
13732 /* I think this assert is bogus if buffer contains
13733 invisible text or images. KFS. */
13734 xassert (IT_CHARPOS (it) <= PT);
13735 #endif
13736 it.current_y = 0;
13737 }
13738
13739 it.current_x = it.hpos = 0;
13740
13741 /* Set startp here explicitly in case that helps avoid an infinite loop
13742 in case the window-scroll-functions functions get errors. */
13743 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13744
13745 /* Run scroll hooks. */
13746 startp = run_window_scroll_functions (window, it.current.pos);
13747
13748 /* Redisplay the window. */
13749 if (!current_matrix_up_to_date_p
13750 || windows_or_buffers_changed
13751 || cursor_type_changed
13752 /* Don't use try_window_reusing_current_matrix in this case
13753 because it can have changed the buffer. */
13754 || !NILP (Vwindow_scroll_functions)
13755 || !just_this_one_p
13756 || MINI_WINDOW_P (w)
13757 || !(used_current_matrix_p
13758 = try_window_reusing_current_matrix (w)))
13759 try_window (window, startp, 0);
13760
13761 /* If new fonts have been loaded (due to fontsets), give up. We
13762 have to start a new redisplay since we need to re-adjust glyph
13763 matrices. */
13764 if (fonts_changed_p)
13765 goto need_larger_matrices;
13766
13767 /* If cursor did not appear assume that the middle of the window is
13768 in the first line of the window. Do it again with the next line.
13769 (Imagine a window of height 100, displaying two lines of height
13770 60. Moving back 50 from it->last_visible_y will end in the first
13771 line.) */
13772 if (w->cursor.vpos < 0)
13773 {
13774 if (!NILP (w->window_end_valid)
13775 && PT >= Z - XFASTINT (w->window_end_pos))
13776 {
13777 clear_glyph_matrix (w->desired_matrix);
13778 move_it_by_lines (&it, 1, 0);
13779 try_window (window, it.current.pos, 0);
13780 }
13781 else if (PT < IT_CHARPOS (it))
13782 {
13783 clear_glyph_matrix (w->desired_matrix);
13784 move_it_by_lines (&it, -1, 0);
13785 try_window (window, it.current.pos, 0);
13786 }
13787 else
13788 {
13789 /* Not much we can do about it. */
13790 }
13791 }
13792
13793 /* Consider the following case: Window starts at BEGV, there is
13794 invisible, intangible text at BEGV, so that display starts at
13795 some point START > BEGV. It can happen that we are called with
13796 PT somewhere between BEGV and START. Try to handle that case. */
13797 if (w->cursor.vpos < 0)
13798 {
13799 struct glyph_row *row = w->current_matrix->rows;
13800 if (row->mode_line_p)
13801 ++row;
13802 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13803 }
13804
13805 if (!cursor_row_fully_visible_p (w, 0, 0))
13806 {
13807 /* If vscroll is enabled, disable it and try again. */
13808 if (w->vscroll)
13809 {
13810 w->vscroll = 0;
13811 clear_glyph_matrix (w->desired_matrix);
13812 goto recenter;
13813 }
13814
13815 /* If centering point failed to make the whole line visible,
13816 put point at the top instead. That has to make the whole line
13817 visible, if it can be done. */
13818 if (centering_position == 0)
13819 goto done;
13820
13821 clear_glyph_matrix (w->desired_matrix);
13822 centering_position = 0;
13823 goto recenter;
13824 }
13825
13826 done:
13827
13828 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13829 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13830 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13831 ? Qt : Qnil);
13832
13833 /* Display the mode line, if we must. */
13834 if ((update_mode_line
13835 /* If window not full width, must redo its mode line
13836 if (a) the window to its side is being redone and
13837 (b) we do a frame-based redisplay. This is a consequence
13838 of how inverted lines are drawn in frame-based redisplay. */
13839 || (!just_this_one_p
13840 && !FRAME_WINDOW_P (f)
13841 && !WINDOW_FULL_WIDTH_P (w))
13842 /* Line number to display. */
13843 || INTEGERP (w->base_line_pos)
13844 /* Column number is displayed and different from the one displayed. */
13845 || (!NILP (w->column_number_displayed)
13846 && (XFASTINT (w->column_number_displayed)
13847 != (int) current_column ()))) /* iftc */
13848 /* This means that the window has a mode line. */
13849 && (WINDOW_WANTS_MODELINE_P (w)
13850 || WINDOW_WANTS_HEADER_LINE_P (w)))
13851 {
13852 display_mode_lines (w);
13853
13854 /* If mode line height has changed, arrange for a thorough
13855 immediate redisplay using the correct mode line height. */
13856 if (WINDOW_WANTS_MODELINE_P (w)
13857 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13858 {
13859 fonts_changed_p = 1;
13860 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13861 = DESIRED_MODE_LINE_HEIGHT (w);
13862 }
13863
13864 /* If top line height has changed, arrange for a thorough
13865 immediate redisplay using the correct mode line height. */
13866 if (WINDOW_WANTS_HEADER_LINE_P (w)
13867 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13868 {
13869 fonts_changed_p = 1;
13870 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13871 = DESIRED_HEADER_LINE_HEIGHT (w);
13872 }
13873
13874 if (fonts_changed_p)
13875 goto need_larger_matrices;
13876 }
13877
13878 if (!line_number_displayed
13879 && !BUFFERP (w->base_line_pos))
13880 {
13881 w->base_line_pos = Qnil;
13882 w->base_line_number = Qnil;
13883 }
13884
13885 finish_menu_bars:
13886
13887 /* When we reach a frame's selected window, redo the frame's menu bar. */
13888 if (update_mode_line
13889 && EQ (FRAME_SELECTED_WINDOW (f), window))
13890 {
13891 int redisplay_menu_p = 0;
13892 int redisplay_tool_bar_p = 0;
13893
13894 if (FRAME_WINDOW_P (f))
13895 {
13896 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13897 || defined (USE_GTK)
13898 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13899 #else
13900 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13901 #endif
13902 }
13903 else
13904 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13905
13906 if (redisplay_menu_p)
13907 display_menu_bar (w);
13908
13909 #ifdef HAVE_WINDOW_SYSTEM
13910 if (FRAME_WINDOW_P (f))
13911 {
13912 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13913 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13914 #else
13915 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13916 && (FRAME_TOOL_BAR_LINES (f) > 0
13917 || !NILP (Vauto_resize_tool_bars));
13918 #endif
13919
13920 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13921 {
13922 extern int ignore_mouse_drag_p;
13923 ignore_mouse_drag_p = 1;
13924 }
13925 }
13926 #endif
13927 }
13928
13929 #ifdef HAVE_WINDOW_SYSTEM
13930 if (FRAME_WINDOW_P (f)
13931 && update_window_fringes (w, (just_this_one_p
13932 || (!used_current_matrix_p && !overlay_arrow_seen)
13933 || w->pseudo_window_p)))
13934 {
13935 update_begin (f);
13936 BLOCK_INPUT;
13937 if (draw_window_fringes (w, 1))
13938 x_draw_vertical_border (w);
13939 UNBLOCK_INPUT;
13940 update_end (f);
13941 }
13942 #endif /* HAVE_WINDOW_SYSTEM */
13943
13944 /* We go to this label, with fonts_changed_p nonzero,
13945 if it is necessary to try again using larger glyph matrices.
13946 We have to redeem the scroll bar even in this case,
13947 because the loop in redisplay_internal expects that. */
13948 need_larger_matrices:
13949 ;
13950 finish_scroll_bars:
13951
13952 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13953 {
13954 /* Set the thumb's position and size. */
13955 set_vertical_scroll_bar (w);
13956
13957 /* Note that we actually used the scroll bar attached to this
13958 window, so it shouldn't be deleted at the end of redisplay. */
13959 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13960 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13961 }
13962
13963 /* Restore current_buffer and value of point in it. */
13964 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13965 set_buffer_internal_1 (old);
13966 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13967 shorter. This can be caused by log truncation in *Messages*. */
13968 if (CHARPOS (lpoint) <= ZV)
13969 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13970
13971 unbind_to (count, Qnil);
13972 }
13973
13974
13975 /* Build the complete desired matrix of WINDOW with a window start
13976 buffer position POS.
13977
13978 Value is 1 if successful. It is zero if fonts were loaded during
13979 redisplay which makes re-adjusting glyph matrices necessary, and -1
13980 if point would appear in the scroll margins.
13981 (We check that only if CHECK_MARGINS is nonzero. */
13982
13983 int
13984 try_window (window, pos, check_margins)
13985 Lisp_Object window;
13986 struct text_pos pos;
13987 int check_margins;
13988 {
13989 struct window *w = XWINDOW (window);
13990 struct it it;
13991 struct glyph_row *last_text_row = NULL;
13992 struct frame *f = XFRAME (w->frame);
13993
13994 /* Make POS the new window start. */
13995 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13996
13997 /* Mark cursor position as unknown. No overlay arrow seen. */
13998 w->cursor.vpos = -1;
13999 overlay_arrow_seen = 0;
14000
14001 /* Initialize iterator and info to start at POS. */
14002 start_display (&it, w, pos);
14003
14004 /* Display all lines of W. */
14005 while (it.current_y < it.last_visible_y)
14006 {
14007 if (display_line (&it))
14008 last_text_row = it.glyph_row - 1;
14009 if (fonts_changed_p)
14010 return 0;
14011 }
14012
14013 /* Don't let the cursor end in the scroll margins. */
14014 if (check_margins
14015 && !MINI_WINDOW_P (w))
14016 {
14017 int this_scroll_margin;
14018
14019 this_scroll_margin = max (0, scroll_margin);
14020 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14021 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14022
14023 if ((w->cursor.y >= 0 /* not vscrolled */
14024 && w->cursor.y < this_scroll_margin
14025 && CHARPOS (pos) > BEGV
14026 && IT_CHARPOS (it) < ZV)
14027 /* rms: considering make_cursor_line_fully_visible_p here
14028 seems to give wrong results. We don't want to recenter
14029 when the last line is partly visible, we want to allow
14030 that case to be handled in the usual way. */
14031 || (w->cursor.y + 1) > it.last_visible_y)
14032 {
14033 w->cursor.vpos = -1;
14034 clear_glyph_matrix (w->desired_matrix);
14035 return -1;
14036 }
14037 }
14038
14039 /* If bottom moved off end of frame, change mode line percentage. */
14040 if (XFASTINT (w->window_end_pos) <= 0
14041 && Z != IT_CHARPOS (it))
14042 w->update_mode_line = Qt;
14043
14044 /* Set window_end_pos to the offset of the last character displayed
14045 on the window from the end of current_buffer. Set
14046 window_end_vpos to its row number. */
14047 if (last_text_row)
14048 {
14049 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14050 w->window_end_bytepos
14051 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14052 w->window_end_pos
14053 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14054 w->window_end_vpos
14055 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14056 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14057 ->displays_text_p);
14058 }
14059 else
14060 {
14061 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14062 w->window_end_pos = make_number (Z - ZV);
14063 w->window_end_vpos = make_number (0);
14064 }
14065
14066 /* But that is not valid info until redisplay finishes. */
14067 w->window_end_valid = Qnil;
14068 return 1;
14069 }
14070
14071
14072 \f
14073 /************************************************************************
14074 Window redisplay reusing current matrix when buffer has not changed
14075 ************************************************************************/
14076
14077 /* Try redisplay of window W showing an unchanged buffer with a
14078 different window start than the last time it was displayed by
14079 reusing its current matrix. Value is non-zero if successful.
14080 W->start is the new window start. */
14081
14082 static int
14083 try_window_reusing_current_matrix (w)
14084 struct window *w;
14085 {
14086 struct frame *f = XFRAME (w->frame);
14087 struct glyph_row *row, *bottom_row;
14088 struct it it;
14089 struct run run;
14090 struct text_pos start, new_start;
14091 int nrows_scrolled, i;
14092 struct glyph_row *last_text_row;
14093 struct glyph_row *last_reused_text_row;
14094 struct glyph_row *start_row;
14095 int start_vpos, min_y, max_y;
14096
14097 #if GLYPH_DEBUG
14098 if (inhibit_try_window_reusing)
14099 return 0;
14100 #endif
14101
14102 if (/* This function doesn't handle terminal frames. */
14103 !FRAME_WINDOW_P (f)
14104 /* Don't try to reuse the display if windows have been split
14105 or such. */
14106 || windows_or_buffers_changed
14107 || cursor_type_changed)
14108 return 0;
14109
14110 /* Can't do this if region may have changed. */
14111 if ((!NILP (Vtransient_mark_mode)
14112 && !NILP (current_buffer->mark_active))
14113 || !NILP (w->region_showing)
14114 || !NILP (Vshow_trailing_whitespace))
14115 return 0;
14116
14117 /* If top-line visibility has changed, give up. */
14118 if (WINDOW_WANTS_HEADER_LINE_P (w)
14119 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14120 return 0;
14121
14122 /* Give up if old or new display is scrolled vertically. We could
14123 make this function handle this, but right now it doesn't. */
14124 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14125 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14126 return 0;
14127
14128 /* The variable new_start now holds the new window start. The old
14129 start `start' can be determined from the current matrix. */
14130 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14131 start = start_row->start.pos;
14132 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14133
14134 /* Clear the desired matrix for the display below. */
14135 clear_glyph_matrix (w->desired_matrix);
14136
14137 if (CHARPOS (new_start) <= CHARPOS (start))
14138 {
14139 int first_row_y;
14140
14141 /* Don't use this method if the display starts with an ellipsis
14142 displayed for invisible text. It's not easy to handle that case
14143 below, and it's certainly not worth the effort since this is
14144 not a frequent case. */
14145 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14146 return 0;
14147
14148 IF_DEBUG (debug_method_add (w, "twu1"));
14149
14150 /* Display up to a row that can be reused. The variable
14151 last_text_row is set to the last row displayed that displays
14152 text. Note that it.vpos == 0 if or if not there is a
14153 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14154 start_display (&it, w, new_start);
14155 first_row_y = it.current_y;
14156 w->cursor.vpos = -1;
14157 last_text_row = last_reused_text_row = NULL;
14158
14159 while (it.current_y < it.last_visible_y
14160 && !fonts_changed_p)
14161 {
14162 /* If we have reached into the characters in the START row,
14163 that means the line boundaries have changed. So we
14164 can't start copying with the row START. Maybe it will
14165 work to start copying with the following row. */
14166 while (IT_CHARPOS (it) > CHARPOS (start))
14167 {
14168 /* Advance to the next row as the "start". */
14169 start_row++;
14170 start = start_row->start.pos;
14171 /* If there are no more rows to try, or just one, give up. */
14172 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14173 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14174 || CHARPOS (start) == ZV)
14175 {
14176 clear_glyph_matrix (w->desired_matrix);
14177 return 0;
14178 }
14179
14180 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14181 }
14182 /* If we have reached alignment,
14183 we can copy the rest of the rows. */
14184 if (IT_CHARPOS (it) == CHARPOS (start))
14185 break;
14186
14187 if (display_line (&it))
14188 last_text_row = it.glyph_row - 1;
14189 }
14190
14191 /* A value of current_y < last_visible_y means that we stopped
14192 at the previous window start, which in turn means that we
14193 have at least one reusable row. */
14194 if (it.current_y < it.last_visible_y)
14195 {
14196 /* IT.vpos always starts from 0; it counts text lines. */
14197 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14198
14199 /* Find PT if not already found in the lines displayed. */
14200 if (w->cursor.vpos < 0)
14201 {
14202 int dy = it.current_y - start_row->y;
14203
14204 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14205 row = row_containing_pos (w, PT, row, NULL, dy);
14206 if (row)
14207 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14208 dy, nrows_scrolled);
14209 else
14210 {
14211 clear_glyph_matrix (w->desired_matrix);
14212 return 0;
14213 }
14214 }
14215
14216 /* Scroll the display. Do it before the current matrix is
14217 changed. The problem here is that update has not yet
14218 run, i.e. part of the current matrix is not up to date.
14219 scroll_run_hook will clear the cursor, and use the
14220 current matrix to get the height of the row the cursor is
14221 in. */
14222 run.current_y = start_row->y;
14223 run.desired_y = it.current_y;
14224 run.height = it.last_visible_y - it.current_y;
14225
14226 if (run.height > 0 && run.current_y != run.desired_y)
14227 {
14228 update_begin (f);
14229 FRAME_RIF (f)->update_window_begin_hook (w);
14230 FRAME_RIF (f)->clear_window_mouse_face (w);
14231 FRAME_RIF (f)->scroll_run_hook (w, &run);
14232 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14233 update_end (f);
14234 }
14235
14236 /* Shift current matrix down by nrows_scrolled lines. */
14237 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14238 rotate_matrix (w->current_matrix,
14239 start_vpos,
14240 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14241 nrows_scrolled);
14242
14243 /* Disable lines that must be updated. */
14244 for (i = 0; i < nrows_scrolled; ++i)
14245 (start_row + i)->enabled_p = 0;
14246
14247 /* Re-compute Y positions. */
14248 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14249 max_y = it.last_visible_y;
14250 for (row = start_row + nrows_scrolled;
14251 row < bottom_row;
14252 ++row)
14253 {
14254 row->y = it.current_y;
14255 row->visible_height = row->height;
14256
14257 if (row->y < min_y)
14258 row->visible_height -= min_y - row->y;
14259 if (row->y + row->height > max_y)
14260 row->visible_height -= row->y + row->height - max_y;
14261 row->redraw_fringe_bitmaps_p = 1;
14262
14263 it.current_y += row->height;
14264
14265 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14266 last_reused_text_row = row;
14267 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14268 break;
14269 }
14270
14271 /* Disable lines in the current matrix which are now
14272 below the window. */
14273 for (++row; row < bottom_row; ++row)
14274 row->enabled_p = row->mode_line_p = 0;
14275 }
14276
14277 /* Update window_end_pos etc.; last_reused_text_row is the last
14278 reused row from the current matrix containing text, if any.
14279 The value of last_text_row is the last displayed line
14280 containing text. */
14281 if (last_reused_text_row)
14282 {
14283 w->window_end_bytepos
14284 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14285 w->window_end_pos
14286 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14287 w->window_end_vpos
14288 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14289 w->current_matrix));
14290 }
14291 else if (last_text_row)
14292 {
14293 w->window_end_bytepos
14294 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14295 w->window_end_pos
14296 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14297 w->window_end_vpos
14298 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14299 }
14300 else
14301 {
14302 /* This window must be completely empty. */
14303 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14304 w->window_end_pos = make_number (Z - ZV);
14305 w->window_end_vpos = make_number (0);
14306 }
14307 w->window_end_valid = Qnil;
14308
14309 /* Update hint: don't try scrolling again in update_window. */
14310 w->desired_matrix->no_scrolling_p = 1;
14311
14312 #if GLYPH_DEBUG
14313 debug_method_add (w, "try_window_reusing_current_matrix 1");
14314 #endif
14315 return 1;
14316 }
14317 else if (CHARPOS (new_start) > CHARPOS (start))
14318 {
14319 struct glyph_row *pt_row, *row;
14320 struct glyph_row *first_reusable_row;
14321 struct glyph_row *first_row_to_display;
14322 int dy;
14323 int yb = window_text_bottom_y (w);
14324
14325 /* Find the row starting at new_start, if there is one. Don't
14326 reuse a partially visible line at the end. */
14327 first_reusable_row = start_row;
14328 while (first_reusable_row->enabled_p
14329 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14330 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14331 < CHARPOS (new_start)))
14332 ++first_reusable_row;
14333
14334 /* Give up if there is no row to reuse. */
14335 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14336 || !first_reusable_row->enabled_p
14337 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14338 != CHARPOS (new_start)))
14339 return 0;
14340
14341 /* We can reuse fully visible rows beginning with
14342 first_reusable_row to the end of the window. Set
14343 first_row_to_display to the first row that cannot be reused.
14344 Set pt_row to the row containing point, if there is any. */
14345 pt_row = NULL;
14346 for (first_row_to_display = first_reusable_row;
14347 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14348 ++first_row_to_display)
14349 {
14350 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14351 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14352 pt_row = first_row_to_display;
14353 }
14354
14355 /* Start displaying at the start of first_row_to_display. */
14356 xassert (first_row_to_display->y < yb);
14357 init_to_row_start (&it, w, first_row_to_display);
14358
14359 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14360 - start_vpos);
14361 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14362 - nrows_scrolled);
14363 it.current_y = (first_row_to_display->y - first_reusable_row->y
14364 + WINDOW_HEADER_LINE_HEIGHT (w));
14365
14366 /* Display lines beginning with first_row_to_display in the
14367 desired matrix. Set last_text_row to the last row displayed
14368 that displays text. */
14369 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14370 if (pt_row == NULL)
14371 w->cursor.vpos = -1;
14372 last_text_row = NULL;
14373 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14374 if (display_line (&it))
14375 last_text_row = it.glyph_row - 1;
14376
14377 /* Give up If point isn't in a row displayed or reused. */
14378 if (w->cursor.vpos < 0)
14379 {
14380 clear_glyph_matrix (w->desired_matrix);
14381 return 0;
14382 }
14383
14384 /* If point is in a reused row, adjust y and vpos of the cursor
14385 position. */
14386 if (pt_row)
14387 {
14388 w->cursor.vpos -= nrows_scrolled;
14389 w->cursor.y -= first_reusable_row->y - start_row->y;
14390 }
14391
14392 /* Scroll the display. */
14393 run.current_y = first_reusable_row->y;
14394 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14395 run.height = it.last_visible_y - run.current_y;
14396 dy = run.current_y - run.desired_y;
14397
14398 if (run.height)
14399 {
14400 update_begin (f);
14401 FRAME_RIF (f)->update_window_begin_hook (w);
14402 FRAME_RIF (f)->clear_window_mouse_face (w);
14403 FRAME_RIF (f)->scroll_run_hook (w, &run);
14404 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14405 update_end (f);
14406 }
14407
14408 /* Adjust Y positions of reused rows. */
14409 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14410 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14411 max_y = it.last_visible_y;
14412 for (row = first_reusable_row; row < first_row_to_display; ++row)
14413 {
14414 row->y -= dy;
14415 row->visible_height = row->height;
14416 if (row->y < min_y)
14417 row->visible_height -= min_y - row->y;
14418 if (row->y + row->height > max_y)
14419 row->visible_height -= row->y + row->height - max_y;
14420 row->redraw_fringe_bitmaps_p = 1;
14421 }
14422
14423 /* Scroll the current matrix. */
14424 xassert (nrows_scrolled > 0);
14425 rotate_matrix (w->current_matrix,
14426 start_vpos,
14427 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14428 -nrows_scrolled);
14429
14430 /* Disable rows not reused. */
14431 for (row -= nrows_scrolled; row < bottom_row; ++row)
14432 row->enabled_p = 0;
14433
14434 /* Point may have moved to a different line, so we cannot assume that
14435 the previous cursor position is valid; locate the correct row. */
14436 if (pt_row)
14437 {
14438 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14439 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14440 row++)
14441 {
14442 w->cursor.vpos++;
14443 w->cursor.y = row->y;
14444 }
14445 if (row < bottom_row)
14446 {
14447 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14448 while (glyph->charpos < PT)
14449 {
14450 w->cursor.hpos++;
14451 w->cursor.x += glyph->pixel_width;
14452 glyph++;
14453 }
14454 }
14455 }
14456
14457 /* Adjust window end. A null value of last_text_row means that
14458 the window end is in reused rows which in turn means that
14459 only its vpos can have changed. */
14460 if (last_text_row)
14461 {
14462 w->window_end_bytepos
14463 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14464 w->window_end_pos
14465 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14466 w->window_end_vpos
14467 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14468 }
14469 else
14470 {
14471 w->window_end_vpos
14472 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14473 }
14474
14475 w->window_end_valid = Qnil;
14476 w->desired_matrix->no_scrolling_p = 1;
14477
14478 #if GLYPH_DEBUG
14479 debug_method_add (w, "try_window_reusing_current_matrix 2");
14480 #endif
14481 return 1;
14482 }
14483
14484 return 0;
14485 }
14486
14487
14488 \f
14489 /************************************************************************
14490 Window redisplay reusing current matrix when buffer has changed
14491 ************************************************************************/
14492
14493 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14494 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14495 int *, int *));
14496 static struct glyph_row *
14497 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14498 struct glyph_row *));
14499
14500
14501 /* Return the last row in MATRIX displaying text. If row START is
14502 non-null, start searching with that row. IT gives the dimensions
14503 of the display. Value is null if matrix is empty; otherwise it is
14504 a pointer to the row found. */
14505
14506 static struct glyph_row *
14507 find_last_row_displaying_text (matrix, it, start)
14508 struct glyph_matrix *matrix;
14509 struct it *it;
14510 struct glyph_row *start;
14511 {
14512 struct glyph_row *row, *row_found;
14513
14514 /* Set row_found to the last row in IT->w's current matrix
14515 displaying text. The loop looks funny but think of partially
14516 visible lines. */
14517 row_found = NULL;
14518 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14519 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14520 {
14521 xassert (row->enabled_p);
14522 row_found = row;
14523 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14524 break;
14525 ++row;
14526 }
14527
14528 return row_found;
14529 }
14530
14531
14532 /* Return the last row in the current matrix of W that is not affected
14533 by changes at the start of current_buffer that occurred since W's
14534 current matrix was built. Value is null if no such row exists.
14535
14536 BEG_UNCHANGED us the number of characters unchanged at the start of
14537 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14538 first changed character in current_buffer. Characters at positions <
14539 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14540 when the current matrix was built. */
14541
14542 static struct glyph_row *
14543 find_last_unchanged_at_beg_row (w)
14544 struct window *w;
14545 {
14546 int first_changed_pos = BEG + BEG_UNCHANGED;
14547 struct glyph_row *row;
14548 struct glyph_row *row_found = NULL;
14549 int yb = window_text_bottom_y (w);
14550
14551 /* Find the last row displaying unchanged text. */
14552 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14553 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14554 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14555 ++row)
14556 {
14557 if (/* If row ends before first_changed_pos, it is unchanged,
14558 except in some case. */
14559 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14560 /* When row ends in ZV and we write at ZV it is not
14561 unchanged. */
14562 && !row->ends_at_zv_p
14563 /* When first_changed_pos is the end of a continued line,
14564 row is not unchanged because it may be no longer
14565 continued. */
14566 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14567 && (row->continued_p
14568 || row->exact_window_width_line_p)))
14569 row_found = row;
14570
14571 /* Stop if last visible row. */
14572 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14573 break;
14574 }
14575
14576 return row_found;
14577 }
14578
14579
14580 /* Find the first glyph row in the current matrix of W that is not
14581 affected by changes at the end of current_buffer since the
14582 time W's current matrix was built.
14583
14584 Return in *DELTA the number of chars by which buffer positions in
14585 unchanged text at the end of current_buffer must be adjusted.
14586
14587 Return in *DELTA_BYTES the corresponding number of bytes.
14588
14589 Value is null if no such row exists, i.e. all rows are affected by
14590 changes. */
14591
14592 static struct glyph_row *
14593 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14594 struct window *w;
14595 int *delta, *delta_bytes;
14596 {
14597 struct glyph_row *row;
14598 struct glyph_row *row_found = NULL;
14599
14600 *delta = *delta_bytes = 0;
14601
14602 /* Display must not have been paused, otherwise the current matrix
14603 is not up to date. */
14604 eassert (!NILP (w->window_end_valid));
14605
14606 /* A value of window_end_pos >= END_UNCHANGED means that the window
14607 end is in the range of changed text. If so, there is no
14608 unchanged row at the end of W's current matrix. */
14609 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14610 return NULL;
14611
14612 /* Set row to the last row in W's current matrix displaying text. */
14613 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14614
14615 /* If matrix is entirely empty, no unchanged row exists. */
14616 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14617 {
14618 /* The value of row is the last glyph row in the matrix having a
14619 meaningful buffer position in it. The end position of row
14620 corresponds to window_end_pos. This allows us to translate
14621 buffer positions in the current matrix to current buffer
14622 positions for characters not in changed text. */
14623 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14624 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14625 int last_unchanged_pos, last_unchanged_pos_old;
14626 struct glyph_row *first_text_row
14627 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14628
14629 *delta = Z - Z_old;
14630 *delta_bytes = Z_BYTE - Z_BYTE_old;
14631
14632 /* Set last_unchanged_pos to the buffer position of the last
14633 character in the buffer that has not been changed. Z is the
14634 index + 1 of the last character in current_buffer, i.e. by
14635 subtracting END_UNCHANGED we get the index of the last
14636 unchanged character, and we have to add BEG to get its buffer
14637 position. */
14638 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14639 last_unchanged_pos_old = last_unchanged_pos - *delta;
14640
14641 /* Search backward from ROW for a row displaying a line that
14642 starts at a minimum position >= last_unchanged_pos_old. */
14643 for (; row > first_text_row; --row)
14644 {
14645 /* This used to abort, but it can happen.
14646 It is ok to just stop the search instead here. KFS. */
14647 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14648 break;
14649
14650 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14651 row_found = row;
14652 }
14653 }
14654
14655 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14656
14657 return row_found;
14658 }
14659
14660
14661 /* Make sure that glyph rows in the current matrix of window W
14662 reference the same glyph memory as corresponding rows in the
14663 frame's frame matrix. This function is called after scrolling W's
14664 current matrix on a terminal frame in try_window_id and
14665 try_window_reusing_current_matrix. */
14666
14667 static void
14668 sync_frame_with_window_matrix_rows (w)
14669 struct window *w;
14670 {
14671 struct frame *f = XFRAME (w->frame);
14672 struct glyph_row *window_row, *window_row_end, *frame_row;
14673
14674 /* Preconditions: W must be a leaf window and full-width. Its frame
14675 must have a frame matrix. */
14676 xassert (NILP (w->hchild) && NILP (w->vchild));
14677 xassert (WINDOW_FULL_WIDTH_P (w));
14678 xassert (!FRAME_WINDOW_P (f));
14679
14680 /* If W is a full-width window, glyph pointers in W's current matrix
14681 have, by definition, to be the same as glyph pointers in the
14682 corresponding frame matrix. Note that frame matrices have no
14683 marginal areas (see build_frame_matrix). */
14684 window_row = w->current_matrix->rows;
14685 window_row_end = window_row + w->current_matrix->nrows;
14686 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14687 while (window_row < window_row_end)
14688 {
14689 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14690 struct glyph *end = window_row->glyphs[LAST_AREA];
14691
14692 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14693 frame_row->glyphs[TEXT_AREA] = start;
14694 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14695 frame_row->glyphs[LAST_AREA] = end;
14696
14697 /* Disable frame rows whose corresponding window rows have
14698 been disabled in try_window_id. */
14699 if (!window_row->enabled_p)
14700 frame_row->enabled_p = 0;
14701
14702 ++window_row, ++frame_row;
14703 }
14704 }
14705
14706
14707 /* Find the glyph row in window W containing CHARPOS. Consider all
14708 rows between START and END (not inclusive). END null means search
14709 all rows to the end of the display area of W. Value is the row
14710 containing CHARPOS or null. */
14711
14712 struct glyph_row *
14713 row_containing_pos (w, charpos, start, end, dy)
14714 struct window *w;
14715 int charpos;
14716 struct glyph_row *start, *end;
14717 int dy;
14718 {
14719 struct glyph_row *row = start;
14720 int last_y;
14721
14722 /* If we happen to start on a header-line, skip that. */
14723 if (row->mode_line_p)
14724 ++row;
14725
14726 if ((end && row >= end) || !row->enabled_p)
14727 return NULL;
14728
14729 last_y = window_text_bottom_y (w) - dy;
14730
14731 while (1)
14732 {
14733 /* Give up if we have gone too far. */
14734 if (end && row >= end)
14735 return NULL;
14736 /* This formerly returned if they were equal.
14737 I think that both quantities are of a "last plus one" type;
14738 if so, when they are equal, the row is within the screen. -- rms. */
14739 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14740 return NULL;
14741
14742 /* If it is in this row, return this row. */
14743 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14744 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14745 /* The end position of a row equals the start
14746 position of the next row. If CHARPOS is there, we
14747 would rather display it in the next line, except
14748 when this line ends in ZV. */
14749 && !row->ends_at_zv_p
14750 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14751 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14752 return row;
14753 ++row;
14754 }
14755 }
14756
14757
14758 /* Try to redisplay window W by reusing its existing display. W's
14759 current matrix must be up to date when this function is called,
14760 i.e. window_end_valid must not be nil.
14761
14762 Value is
14763
14764 1 if display has been updated
14765 0 if otherwise unsuccessful
14766 -1 if redisplay with same window start is known not to succeed
14767
14768 The following steps are performed:
14769
14770 1. Find the last row in the current matrix of W that is not
14771 affected by changes at the start of current_buffer. If no such row
14772 is found, give up.
14773
14774 2. Find the first row in W's current matrix that is not affected by
14775 changes at the end of current_buffer. Maybe there is no such row.
14776
14777 3. Display lines beginning with the row + 1 found in step 1 to the
14778 row found in step 2 or, if step 2 didn't find a row, to the end of
14779 the window.
14780
14781 4. If cursor is not known to appear on the window, give up.
14782
14783 5. If display stopped at the row found in step 2, scroll the
14784 display and current matrix as needed.
14785
14786 6. Maybe display some lines at the end of W, if we must. This can
14787 happen under various circumstances, like a partially visible line
14788 becoming fully visible, or because newly displayed lines are displayed
14789 in smaller font sizes.
14790
14791 7. Update W's window end information. */
14792
14793 static int
14794 try_window_id (w)
14795 struct window *w;
14796 {
14797 struct frame *f = XFRAME (w->frame);
14798 struct glyph_matrix *current_matrix = w->current_matrix;
14799 struct glyph_matrix *desired_matrix = w->desired_matrix;
14800 struct glyph_row *last_unchanged_at_beg_row;
14801 struct glyph_row *first_unchanged_at_end_row;
14802 struct glyph_row *row;
14803 struct glyph_row *bottom_row;
14804 int bottom_vpos;
14805 struct it it;
14806 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14807 struct text_pos start_pos;
14808 struct run run;
14809 int first_unchanged_at_end_vpos = 0;
14810 struct glyph_row *last_text_row, *last_text_row_at_end;
14811 struct text_pos start;
14812 int first_changed_charpos, last_changed_charpos;
14813
14814 #if GLYPH_DEBUG
14815 if (inhibit_try_window_id)
14816 return 0;
14817 #endif
14818
14819 /* This is handy for debugging. */
14820 #if 0
14821 #define GIVE_UP(X) \
14822 do { \
14823 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14824 return 0; \
14825 } while (0)
14826 #else
14827 #define GIVE_UP(X) return 0
14828 #endif
14829
14830 SET_TEXT_POS_FROM_MARKER (start, w->start);
14831
14832 /* Don't use this for mini-windows because these can show
14833 messages and mini-buffers, and we don't handle that here. */
14834 if (MINI_WINDOW_P (w))
14835 GIVE_UP (1);
14836
14837 /* This flag is used to prevent redisplay optimizations. */
14838 if (windows_or_buffers_changed || cursor_type_changed)
14839 GIVE_UP (2);
14840
14841 /* Verify that narrowing has not changed.
14842 Also verify that we were not told to prevent redisplay optimizations.
14843 It would be nice to further
14844 reduce the number of cases where this prevents try_window_id. */
14845 if (current_buffer->clip_changed
14846 || current_buffer->prevent_redisplay_optimizations_p)
14847 GIVE_UP (3);
14848
14849 /* Window must either use window-based redisplay or be full width. */
14850 if (!FRAME_WINDOW_P (f)
14851 && (!FRAME_LINE_INS_DEL_OK (f)
14852 || !WINDOW_FULL_WIDTH_P (w)))
14853 GIVE_UP (4);
14854
14855 /* Give up if point is not known NOT to appear in W. */
14856 if (PT < CHARPOS (start))
14857 GIVE_UP (5);
14858
14859 /* Another way to prevent redisplay optimizations. */
14860 if (XFASTINT (w->last_modified) == 0)
14861 GIVE_UP (6);
14862
14863 /* Verify that window is not hscrolled. */
14864 if (XFASTINT (w->hscroll) != 0)
14865 GIVE_UP (7);
14866
14867 /* Verify that display wasn't paused. */
14868 if (NILP (w->window_end_valid))
14869 GIVE_UP (8);
14870
14871 /* Can't use this if highlighting a region because a cursor movement
14872 will do more than just set the cursor. */
14873 if (!NILP (Vtransient_mark_mode)
14874 && !NILP (current_buffer->mark_active))
14875 GIVE_UP (9);
14876
14877 /* Likewise if highlighting trailing whitespace. */
14878 if (!NILP (Vshow_trailing_whitespace))
14879 GIVE_UP (11);
14880
14881 /* Likewise if showing a region. */
14882 if (!NILP (w->region_showing))
14883 GIVE_UP (10);
14884
14885 /* Can use this if overlay arrow position and or string have changed. */
14886 if (overlay_arrows_changed_p ())
14887 GIVE_UP (12);
14888
14889 /* When word-wrap is on, adding a space to the first word of a
14890 wrapped line can change the wrap position, altering the line
14891 above it. It might be worthwhile to handle this more
14892 intelligently, but for now just redisplay from scratch. */
14893 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14894 GIVE_UP (21);
14895
14896 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14897 only if buffer has really changed. The reason is that the gap is
14898 initially at Z for freshly visited files. The code below would
14899 set end_unchanged to 0 in that case. */
14900 if (MODIFF > SAVE_MODIFF
14901 /* This seems to happen sometimes after saving a buffer. */
14902 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14903 {
14904 if (GPT - BEG < BEG_UNCHANGED)
14905 BEG_UNCHANGED = GPT - BEG;
14906 if (Z - GPT < END_UNCHANGED)
14907 END_UNCHANGED = Z - GPT;
14908 }
14909
14910 /* The position of the first and last character that has been changed. */
14911 first_changed_charpos = BEG + BEG_UNCHANGED;
14912 last_changed_charpos = Z - END_UNCHANGED;
14913
14914 /* If window starts after a line end, and the last change is in
14915 front of that newline, then changes don't affect the display.
14916 This case happens with stealth-fontification. Note that although
14917 the display is unchanged, glyph positions in the matrix have to
14918 be adjusted, of course. */
14919 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14920 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14921 && ((last_changed_charpos < CHARPOS (start)
14922 && CHARPOS (start) == BEGV)
14923 || (last_changed_charpos < CHARPOS (start) - 1
14924 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14925 {
14926 int Z_old, delta, Z_BYTE_old, delta_bytes;
14927 struct glyph_row *r0;
14928
14929 /* Compute how many chars/bytes have been added to or removed
14930 from the buffer. */
14931 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14932 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14933 delta = Z - Z_old;
14934 delta_bytes = Z_BYTE - Z_BYTE_old;
14935
14936 /* Give up if PT is not in the window. Note that it already has
14937 been checked at the start of try_window_id that PT is not in
14938 front of the window start. */
14939 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14940 GIVE_UP (13);
14941
14942 /* If window start is unchanged, we can reuse the whole matrix
14943 as is, after adjusting glyph positions. No need to compute
14944 the window end again, since its offset from Z hasn't changed. */
14945 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14946 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14947 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14948 /* PT must not be in a partially visible line. */
14949 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14950 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14951 {
14952 /* Adjust positions in the glyph matrix. */
14953 if (delta || delta_bytes)
14954 {
14955 struct glyph_row *r1
14956 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14957 increment_matrix_positions (w->current_matrix,
14958 MATRIX_ROW_VPOS (r0, current_matrix),
14959 MATRIX_ROW_VPOS (r1, current_matrix),
14960 delta, delta_bytes);
14961 }
14962
14963 /* Set the cursor. */
14964 row = row_containing_pos (w, PT, r0, NULL, 0);
14965 if (row)
14966 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14967 else
14968 abort ();
14969 return 1;
14970 }
14971 }
14972
14973 /* Handle the case that changes are all below what is displayed in
14974 the window, and that PT is in the window. This shortcut cannot
14975 be taken if ZV is visible in the window, and text has been added
14976 there that is visible in the window. */
14977 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14978 /* ZV is not visible in the window, or there are no
14979 changes at ZV, actually. */
14980 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14981 || first_changed_charpos == last_changed_charpos))
14982 {
14983 struct glyph_row *r0;
14984
14985 /* Give up if PT is not in the window. Note that it already has
14986 been checked at the start of try_window_id that PT is not in
14987 front of the window start. */
14988 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14989 GIVE_UP (14);
14990
14991 /* If window start is unchanged, we can reuse the whole matrix
14992 as is, without changing glyph positions since no text has
14993 been added/removed in front of the window end. */
14994 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14995 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14996 /* PT must not be in a partially visible line. */
14997 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14998 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14999 {
15000 /* We have to compute the window end anew since text
15001 can have been added/removed after it. */
15002 w->window_end_pos
15003 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15004 w->window_end_bytepos
15005 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15006
15007 /* Set the cursor. */
15008 row = row_containing_pos (w, PT, r0, NULL, 0);
15009 if (row)
15010 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15011 else
15012 abort ();
15013 return 2;
15014 }
15015 }
15016
15017 /* Give up if window start is in the changed area.
15018
15019 The condition used to read
15020
15021 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15022
15023 but why that was tested escapes me at the moment. */
15024 if (CHARPOS (start) >= first_changed_charpos
15025 && CHARPOS (start) <= last_changed_charpos)
15026 GIVE_UP (15);
15027
15028 /* Check that window start agrees with the start of the first glyph
15029 row in its current matrix. Check this after we know the window
15030 start is not in changed text, otherwise positions would not be
15031 comparable. */
15032 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15033 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15034 GIVE_UP (16);
15035
15036 /* Give up if the window ends in strings. Overlay strings
15037 at the end are difficult to handle, so don't try. */
15038 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15039 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15040 GIVE_UP (20);
15041
15042 /* Compute the position at which we have to start displaying new
15043 lines. Some of the lines at the top of the window might be
15044 reusable because they are not displaying changed text. Find the
15045 last row in W's current matrix not affected by changes at the
15046 start of current_buffer. Value is null if changes start in the
15047 first line of window. */
15048 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15049 if (last_unchanged_at_beg_row)
15050 {
15051 /* Avoid starting to display in the moddle of a character, a TAB
15052 for instance. This is easier than to set up the iterator
15053 exactly, and it's not a frequent case, so the additional
15054 effort wouldn't really pay off. */
15055 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15056 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15057 && last_unchanged_at_beg_row > w->current_matrix->rows)
15058 --last_unchanged_at_beg_row;
15059
15060 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15061 GIVE_UP (17);
15062
15063 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15064 GIVE_UP (18);
15065 start_pos = it.current.pos;
15066
15067 /* Start displaying new lines in the desired matrix at the same
15068 vpos we would use in the current matrix, i.e. below
15069 last_unchanged_at_beg_row. */
15070 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15071 current_matrix);
15072 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15073 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15074
15075 xassert (it.hpos == 0 && it.current_x == 0);
15076 }
15077 else
15078 {
15079 /* There are no reusable lines at the start of the window.
15080 Start displaying in the first text line. */
15081 start_display (&it, w, start);
15082 it.vpos = it.first_vpos;
15083 start_pos = it.current.pos;
15084 }
15085
15086 /* Find the first row that is not affected by changes at the end of
15087 the buffer. Value will be null if there is no unchanged row, in
15088 which case we must redisplay to the end of the window. delta
15089 will be set to the value by which buffer positions beginning with
15090 first_unchanged_at_end_row have to be adjusted due to text
15091 changes. */
15092 first_unchanged_at_end_row
15093 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15094 IF_DEBUG (debug_delta = delta);
15095 IF_DEBUG (debug_delta_bytes = delta_bytes);
15096
15097 /* Set stop_pos to the buffer position up to which we will have to
15098 display new lines. If first_unchanged_at_end_row != NULL, this
15099 is the buffer position of the start of the line displayed in that
15100 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15101 that we don't stop at a buffer position. */
15102 stop_pos = 0;
15103 if (first_unchanged_at_end_row)
15104 {
15105 xassert (last_unchanged_at_beg_row == NULL
15106 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15107
15108 /* If this is a continuation line, move forward to the next one
15109 that isn't. Changes in lines above affect this line.
15110 Caution: this may move first_unchanged_at_end_row to a row
15111 not displaying text. */
15112 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15113 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15114 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15115 < it.last_visible_y))
15116 ++first_unchanged_at_end_row;
15117
15118 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15119 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15120 >= it.last_visible_y))
15121 first_unchanged_at_end_row = NULL;
15122 else
15123 {
15124 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15125 + delta);
15126 first_unchanged_at_end_vpos
15127 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15128 xassert (stop_pos >= Z - END_UNCHANGED);
15129 }
15130 }
15131 else if (last_unchanged_at_beg_row == NULL)
15132 GIVE_UP (19);
15133
15134
15135 #if GLYPH_DEBUG
15136
15137 /* Either there is no unchanged row at the end, or the one we have
15138 now displays text. This is a necessary condition for the window
15139 end pos calculation at the end of this function. */
15140 xassert (first_unchanged_at_end_row == NULL
15141 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15142
15143 debug_last_unchanged_at_beg_vpos
15144 = (last_unchanged_at_beg_row
15145 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15146 : -1);
15147 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15148
15149 #endif /* GLYPH_DEBUG != 0 */
15150
15151
15152 /* Display new lines. Set last_text_row to the last new line
15153 displayed which has text on it, i.e. might end up as being the
15154 line where the window_end_vpos is. */
15155 w->cursor.vpos = -1;
15156 last_text_row = NULL;
15157 overlay_arrow_seen = 0;
15158 while (it.current_y < it.last_visible_y
15159 && !fonts_changed_p
15160 && (first_unchanged_at_end_row == NULL
15161 || IT_CHARPOS (it) < stop_pos))
15162 {
15163 if (display_line (&it))
15164 last_text_row = it.glyph_row - 1;
15165 }
15166
15167 if (fonts_changed_p)
15168 return -1;
15169
15170
15171 /* Compute differences in buffer positions, y-positions etc. for
15172 lines reused at the bottom of the window. Compute what we can
15173 scroll. */
15174 if (first_unchanged_at_end_row
15175 /* No lines reused because we displayed everything up to the
15176 bottom of the window. */
15177 && it.current_y < it.last_visible_y)
15178 {
15179 dvpos = (it.vpos
15180 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15181 current_matrix));
15182 dy = it.current_y - first_unchanged_at_end_row->y;
15183 run.current_y = first_unchanged_at_end_row->y;
15184 run.desired_y = run.current_y + dy;
15185 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15186 }
15187 else
15188 {
15189 delta = delta_bytes = dvpos = dy
15190 = run.current_y = run.desired_y = run.height = 0;
15191 first_unchanged_at_end_row = NULL;
15192 }
15193 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15194
15195
15196 /* Find the cursor if not already found. We have to decide whether
15197 PT will appear on this window (it sometimes doesn't, but this is
15198 not a very frequent case.) This decision has to be made before
15199 the current matrix is altered. A value of cursor.vpos < 0 means
15200 that PT is either in one of the lines beginning at
15201 first_unchanged_at_end_row or below the window. Don't care for
15202 lines that might be displayed later at the window end; as
15203 mentioned, this is not a frequent case. */
15204 if (w->cursor.vpos < 0)
15205 {
15206 /* Cursor in unchanged rows at the top? */
15207 if (PT < CHARPOS (start_pos)
15208 && last_unchanged_at_beg_row)
15209 {
15210 row = row_containing_pos (w, PT,
15211 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15212 last_unchanged_at_beg_row + 1, 0);
15213 if (row)
15214 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15215 }
15216
15217 /* Start from first_unchanged_at_end_row looking for PT. */
15218 else if (first_unchanged_at_end_row)
15219 {
15220 row = row_containing_pos (w, PT - delta,
15221 first_unchanged_at_end_row, NULL, 0);
15222 if (row)
15223 set_cursor_from_row (w, row, w->current_matrix, delta,
15224 delta_bytes, dy, dvpos);
15225 }
15226
15227 /* Give up if cursor was not found. */
15228 if (w->cursor.vpos < 0)
15229 {
15230 clear_glyph_matrix (w->desired_matrix);
15231 return -1;
15232 }
15233 }
15234
15235 /* Don't let the cursor end in the scroll margins. */
15236 {
15237 int this_scroll_margin, cursor_height;
15238
15239 this_scroll_margin = max (0, scroll_margin);
15240 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15241 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15242 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15243
15244 if ((w->cursor.y < this_scroll_margin
15245 && CHARPOS (start) > BEGV)
15246 /* Old redisplay didn't take scroll margin into account at the bottom,
15247 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15248 || (w->cursor.y + (make_cursor_line_fully_visible_p
15249 ? cursor_height + this_scroll_margin
15250 : 1)) > it.last_visible_y)
15251 {
15252 w->cursor.vpos = -1;
15253 clear_glyph_matrix (w->desired_matrix);
15254 return -1;
15255 }
15256 }
15257
15258 /* Scroll the display. Do it before changing the current matrix so
15259 that xterm.c doesn't get confused about where the cursor glyph is
15260 found. */
15261 if (dy && run.height)
15262 {
15263 update_begin (f);
15264
15265 if (FRAME_WINDOW_P (f))
15266 {
15267 FRAME_RIF (f)->update_window_begin_hook (w);
15268 FRAME_RIF (f)->clear_window_mouse_face (w);
15269 FRAME_RIF (f)->scroll_run_hook (w, &run);
15270 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15271 }
15272 else
15273 {
15274 /* Terminal frame. In this case, dvpos gives the number of
15275 lines to scroll by; dvpos < 0 means scroll up. */
15276 int first_unchanged_at_end_vpos
15277 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15278 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15279 int end = (WINDOW_TOP_EDGE_LINE (w)
15280 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15281 + window_internal_height (w));
15282
15283 /* Perform the operation on the screen. */
15284 if (dvpos > 0)
15285 {
15286 /* Scroll last_unchanged_at_beg_row to the end of the
15287 window down dvpos lines. */
15288 set_terminal_window (f, end);
15289
15290 /* On dumb terminals delete dvpos lines at the end
15291 before inserting dvpos empty lines. */
15292 if (!FRAME_SCROLL_REGION_OK (f))
15293 ins_del_lines (f, end - dvpos, -dvpos);
15294
15295 /* Insert dvpos empty lines in front of
15296 last_unchanged_at_beg_row. */
15297 ins_del_lines (f, from, dvpos);
15298 }
15299 else if (dvpos < 0)
15300 {
15301 /* Scroll up last_unchanged_at_beg_vpos to the end of
15302 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15303 set_terminal_window (f, end);
15304
15305 /* Delete dvpos lines in front of
15306 last_unchanged_at_beg_vpos. ins_del_lines will set
15307 the cursor to the given vpos and emit |dvpos| delete
15308 line sequences. */
15309 ins_del_lines (f, from + dvpos, dvpos);
15310
15311 /* On a dumb terminal insert dvpos empty lines at the
15312 end. */
15313 if (!FRAME_SCROLL_REGION_OK (f))
15314 ins_del_lines (f, end + dvpos, -dvpos);
15315 }
15316
15317 set_terminal_window (f, 0);
15318 }
15319
15320 update_end (f);
15321 }
15322
15323 /* Shift reused rows of the current matrix to the right position.
15324 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15325 text. */
15326 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15327 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15328 if (dvpos < 0)
15329 {
15330 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15331 bottom_vpos, dvpos);
15332 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15333 bottom_vpos, 0);
15334 }
15335 else if (dvpos > 0)
15336 {
15337 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15338 bottom_vpos, dvpos);
15339 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15340 first_unchanged_at_end_vpos + dvpos, 0);
15341 }
15342
15343 /* For frame-based redisplay, make sure that current frame and window
15344 matrix are in sync with respect to glyph memory. */
15345 if (!FRAME_WINDOW_P (f))
15346 sync_frame_with_window_matrix_rows (w);
15347
15348 /* Adjust buffer positions in reused rows. */
15349 if (delta || delta_bytes)
15350 increment_matrix_positions (current_matrix,
15351 first_unchanged_at_end_vpos + dvpos,
15352 bottom_vpos, delta, delta_bytes);
15353
15354 /* Adjust Y positions. */
15355 if (dy)
15356 shift_glyph_matrix (w, current_matrix,
15357 first_unchanged_at_end_vpos + dvpos,
15358 bottom_vpos, dy);
15359
15360 if (first_unchanged_at_end_row)
15361 {
15362 first_unchanged_at_end_row += dvpos;
15363 if (first_unchanged_at_end_row->y >= it.last_visible_y
15364 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15365 first_unchanged_at_end_row = NULL;
15366 }
15367
15368 /* If scrolling up, there may be some lines to display at the end of
15369 the window. */
15370 last_text_row_at_end = NULL;
15371 if (dy < 0)
15372 {
15373 /* Scrolling up can leave for example a partially visible line
15374 at the end of the window to be redisplayed. */
15375 /* Set last_row to the glyph row in the current matrix where the
15376 window end line is found. It has been moved up or down in
15377 the matrix by dvpos. */
15378 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15379 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15380
15381 /* If last_row is the window end line, it should display text. */
15382 xassert (last_row->displays_text_p);
15383
15384 /* If window end line was partially visible before, begin
15385 displaying at that line. Otherwise begin displaying with the
15386 line following it. */
15387 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15388 {
15389 init_to_row_start (&it, w, last_row);
15390 it.vpos = last_vpos;
15391 it.current_y = last_row->y;
15392 }
15393 else
15394 {
15395 init_to_row_end (&it, w, last_row);
15396 it.vpos = 1 + last_vpos;
15397 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15398 ++last_row;
15399 }
15400
15401 /* We may start in a continuation line. If so, we have to
15402 get the right continuation_lines_width and current_x. */
15403 it.continuation_lines_width = last_row->continuation_lines_width;
15404 it.hpos = it.current_x = 0;
15405
15406 /* Display the rest of the lines at the window end. */
15407 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15408 while (it.current_y < it.last_visible_y
15409 && !fonts_changed_p)
15410 {
15411 /* Is it always sure that the display agrees with lines in
15412 the current matrix? I don't think so, so we mark rows
15413 displayed invalid in the current matrix by setting their
15414 enabled_p flag to zero. */
15415 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15416 if (display_line (&it))
15417 last_text_row_at_end = it.glyph_row - 1;
15418 }
15419 }
15420
15421 /* Update window_end_pos and window_end_vpos. */
15422 if (first_unchanged_at_end_row
15423 && !last_text_row_at_end)
15424 {
15425 /* Window end line if one of the preserved rows from the current
15426 matrix. Set row to the last row displaying text in current
15427 matrix starting at first_unchanged_at_end_row, after
15428 scrolling. */
15429 xassert (first_unchanged_at_end_row->displays_text_p);
15430 row = find_last_row_displaying_text (w->current_matrix, &it,
15431 first_unchanged_at_end_row);
15432 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15433
15434 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15435 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15436 w->window_end_vpos
15437 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15438 xassert (w->window_end_bytepos >= 0);
15439 IF_DEBUG (debug_method_add (w, "A"));
15440 }
15441 else if (last_text_row_at_end)
15442 {
15443 w->window_end_pos
15444 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15445 w->window_end_bytepos
15446 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15447 w->window_end_vpos
15448 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15449 xassert (w->window_end_bytepos >= 0);
15450 IF_DEBUG (debug_method_add (w, "B"));
15451 }
15452 else if (last_text_row)
15453 {
15454 /* We have displayed either to the end of the window or at the
15455 end of the window, i.e. the last row with text is to be found
15456 in the desired matrix. */
15457 w->window_end_pos
15458 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15459 w->window_end_bytepos
15460 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15461 w->window_end_vpos
15462 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15463 xassert (w->window_end_bytepos >= 0);
15464 }
15465 else if (first_unchanged_at_end_row == NULL
15466 && last_text_row == NULL
15467 && last_text_row_at_end == NULL)
15468 {
15469 /* Displayed to end of window, but no line containing text was
15470 displayed. Lines were deleted at the end of the window. */
15471 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15472 int vpos = XFASTINT (w->window_end_vpos);
15473 struct glyph_row *current_row = current_matrix->rows + vpos;
15474 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15475
15476 for (row = NULL;
15477 row == NULL && vpos >= first_vpos;
15478 --vpos, --current_row, --desired_row)
15479 {
15480 if (desired_row->enabled_p)
15481 {
15482 if (desired_row->displays_text_p)
15483 row = desired_row;
15484 }
15485 else if (current_row->displays_text_p)
15486 row = current_row;
15487 }
15488
15489 xassert (row != NULL);
15490 w->window_end_vpos = make_number (vpos + 1);
15491 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15492 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15493 xassert (w->window_end_bytepos >= 0);
15494 IF_DEBUG (debug_method_add (w, "C"));
15495 }
15496 else
15497 abort ();
15498
15499 #if 0 /* This leads to problems, for instance when the cursor is
15500 at ZV, and the cursor line displays no text. */
15501 /* Disable rows below what's displayed in the window. This makes
15502 debugging easier. */
15503 enable_glyph_matrix_rows (current_matrix,
15504 XFASTINT (w->window_end_vpos) + 1,
15505 bottom_vpos, 0);
15506 #endif
15507
15508 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15509 debug_end_vpos = XFASTINT (w->window_end_vpos));
15510
15511 /* Record that display has not been completed. */
15512 w->window_end_valid = Qnil;
15513 w->desired_matrix->no_scrolling_p = 1;
15514 return 3;
15515
15516 #undef GIVE_UP
15517 }
15518
15519
15520 \f
15521 /***********************************************************************
15522 More debugging support
15523 ***********************************************************************/
15524
15525 #if GLYPH_DEBUG
15526
15527 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15528 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15529 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15530
15531
15532 /* Dump the contents of glyph matrix MATRIX on stderr.
15533
15534 GLYPHS 0 means don't show glyph contents.
15535 GLYPHS 1 means show glyphs in short form
15536 GLYPHS > 1 means show glyphs in long form. */
15537
15538 void
15539 dump_glyph_matrix (matrix, glyphs)
15540 struct glyph_matrix *matrix;
15541 int glyphs;
15542 {
15543 int i;
15544 for (i = 0; i < matrix->nrows; ++i)
15545 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15546 }
15547
15548
15549 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15550 the glyph row and area where the glyph comes from. */
15551
15552 void
15553 dump_glyph (row, glyph, area)
15554 struct glyph_row *row;
15555 struct glyph *glyph;
15556 int area;
15557 {
15558 if (glyph->type == CHAR_GLYPH)
15559 {
15560 fprintf (stderr,
15561 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15562 glyph - row->glyphs[TEXT_AREA],
15563 'C',
15564 glyph->charpos,
15565 (BUFFERP (glyph->object)
15566 ? 'B'
15567 : (STRINGP (glyph->object)
15568 ? 'S'
15569 : '-')),
15570 glyph->pixel_width,
15571 glyph->u.ch,
15572 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15573 ? glyph->u.ch
15574 : '.'),
15575 glyph->face_id,
15576 glyph->left_box_line_p,
15577 glyph->right_box_line_p);
15578 }
15579 else if (glyph->type == STRETCH_GLYPH)
15580 {
15581 fprintf (stderr,
15582 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15583 glyph - row->glyphs[TEXT_AREA],
15584 'S',
15585 glyph->charpos,
15586 (BUFFERP (glyph->object)
15587 ? 'B'
15588 : (STRINGP (glyph->object)
15589 ? 'S'
15590 : '-')),
15591 glyph->pixel_width,
15592 0,
15593 '.',
15594 glyph->face_id,
15595 glyph->left_box_line_p,
15596 glyph->right_box_line_p);
15597 }
15598 else if (glyph->type == IMAGE_GLYPH)
15599 {
15600 fprintf (stderr,
15601 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15602 glyph - row->glyphs[TEXT_AREA],
15603 'I',
15604 glyph->charpos,
15605 (BUFFERP (glyph->object)
15606 ? 'B'
15607 : (STRINGP (glyph->object)
15608 ? 'S'
15609 : '-')),
15610 glyph->pixel_width,
15611 glyph->u.img_id,
15612 '.',
15613 glyph->face_id,
15614 glyph->left_box_line_p,
15615 glyph->right_box_line_p);
15616 }
15617 else if (glyph->type == COMPOSITE_GLYPH)
15618 {
15619 fprintf (stderr,
15620 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15621 glyph - row->glyphs[TEXT_AREA],
15622 '+',
15623 glyph->charpos,
15624 (BUFFERP (glyph->object)
15625 ? 'B'
15626 : (STRINGP (glyph->object)
15627 ? 'S'
15628 : '-')),
15629 glyph->pixel_width,
15630 glyph->u.cmp_id,
15631 '.',
15632 glyph->face_id,
15633 glyph->left_box_line_p,
15634 glyph->right_box_line_p);
15635 }
15636 }
15637
15638
15639 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15640 GLYPHS 0 means don't show glyph contents.
15641 GLYPHS 1 means show glyphs in short form
15642 GLYPHS > 1 means show glyphs in long form. */
15643
15644 void
15645 dump_glyph_row (row, vpos, glyphs)
15646 struct glyph_row *row;
15647 int vpos, glyphs;
15648 {
15649 if (glyphs != 1)
15650 {
15651 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15652 fprintf (stderr, "======================================================================\n");
15653
15654 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15655 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15656 vpos,
15657 MATRIX_ROW_START_CHARPOS (row),
15658 MATRIX_ROW_END_CHARPOS (row),
15659 row->used[TEXT_AREA],
15660 row->contains_overlapping_glyphs_p,
15661 row->enabled_p,
15662 row->truncated_on_left_p,
15663 row->truncated_on_right_p,
15664 row->continued_p,
15665 MATRIX_ROW_CONTINUATION_LINE_P (row),
15666 row->displays_text_p,
15667 row->ends_at_zv_p,
15668 row->fill_line_p,
15669 row->ends_in_middle_of_char_p,
15670 row->starts_in_middle_of_char_p,
15671 row->mouse_face_p,
15672 row->x,
15673 row->y,
15674 row->pixel_width,
15675 row->height,
15676 row->visible_height,
15677 row->ascent,
15678 row->phys_ascent);
15679 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15680 row->end.overlay_string_index,
15681 row->continuation_lines_width);
15682 fprintf (stderr, "%9d %5d\n",
15683 CHARPOS (row->start.string_pos),
15684 CHARPOS (row->end.string_pos));
15685 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15686 row->end.dpvec_index);
15687 }
15688
15689 if (glyphs > 1)
15690 {
15691 int area;
15692
15693 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15694 {
15695 struct glyph *glyph = row->glyphs[area];
15696 struct glyph *glyph_end = glyph + row->used[area];
15697
15698 /* Glyph for a line end in text. */
15699 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15700 ++glyph_end;
15701
15702 if (glyph < glyph_end)
15703 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15704
15705 for (; glyph < glyph_end; ++glyph)
15706 dump_glyph (row, glyph, area);
15707 }
15708 }
15709 else if (glyphs == 1)
15710 {
15711 int area;
15712
15713 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15714 {
15715 char *s = (char *) alloca (row->used[area] + 1);
15716 int i;
15717
15718 for (i = 0; i < row->used[area]; ++i)
15719 {
15720 struct glyph *glyph = row->glyphs[area] + i;
15721 if (glyph->type == CHAR_GLYPH
15722 && glyph->u.ch < 0x80
15723 && glyph->u.ch >= ' ')
15724 s[i] = glyph->u.ch;
15725 else
15726 s[i] = '.';
15727 }
15728
15729 s[i] = '\0';
15730 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15731 }
15732 }
15733 }
15734
15735
15736 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15737 Sdump_glyph_matrix, 0, 1, "p",
15738 doc: /* Dump the current matrix of the selected window to stderr.
15739 Shows contents of glyph row structures. With non-nil
15740 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15741 glyphs in short form, otherwise show glyphs in long form. */)
15742 (glyphs)
15743 Lisp_Object glyphs;
15744 {
15745 struct window *w = XWINDOW (selected_window);
15746 struct buffer *buffer = XBUFFER (w->buffer);
15747
15748 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15749 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15750 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15751 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15752 fprintf (stderr, "=============================================\n");
15753 dump_glyph_matrix (w->current_matrix,
15754 NILP (glyphs) ? 0 : XINT (glyphs));
15755 return Qnil;
15756 }
15757
15758
15759 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15760 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15761 ()
15762 {
15763 struct frame *f = XFRAME (selected_frame);
15764 dump_glyph_matrix (f->current_matrix, 1);
15765 return Qnil;
15766 }
15767
15768
15769 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15770 doc: /* Dump glyph row ROW to stderr.
15771 GLYPH 0 means don't dump glyphs.
15772 GLYPH 1 means dump glyphs in short form.
15773 GLYPH > 1 or omitted means dump glyphs in long form. */)
15774 (row, glyphs)
15775 Lisp_Object row, glyphs;
15776 {
15777 struct glyph_matrix *matrix;
15778 int vpos;
15779
15780 CHECK_NUMBER (row);
15781 matrix = XWINDOW (selected_window)->current_matrix;
15782 vpos = XINT (row);
15783 if (vpos >= 0 && vpos < matrix->nrows)
15784 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15785 vpos,
15786 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15787 return Qnil;
15788 }
15789
15790
15791 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15792 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15793 GLYPH 0 means don't dump glyphs.
15794 GLYPH 1 means dump glyphs in short form.
15795 GLYPH > 1 or omitted means dump glyphs in long form. */)
15796 (row, glyphs)
15797 Lisp_Object row, glyphs;
15798 {
15799 struct frame *sf = SELECTED_FRAME ();
15800 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15801 int vpos;
15802
15803 CHECK_NUMBER (row);
15804 vpos = XINT (row);
15805 if (vpos >= 0 && vpos < m->nrows)
15806 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15807 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15808 return Qnil;
15809 }
15810
15811
15812 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15813 doc: /* Toggle tracing of redisplay.
15814 With ARG, turn tracing on if and only if ARG is positive. */)
15815 (arg)
15816 Lisp_Object arg;
15817 {
15818 if (NILP (arg))
15819 trace_redisplay_p = !trace_redisplay_p;
15820 else
15821 {
15822 arg = Fprefix_numeric_value (arg);
15823 trace_redisplay_p = XINT (arg) > 0;
15824 }
15825
15826 return Qnil;
15827 }
15828
15829
15830 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15831 doc: /* Like `format', but print result to stderr.
15832 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15833 (nargs, args)
15834 int nargs;
15835 Lisp_Object *args;
15836 {
15837 Lisp_Object s = Fformat (nargs, args);
15838 fprintf (stderr, "%s", SDATA (s));
15839 return Qnil;
15840 }
15841
15842 #endif /* GLYPH_DEBUG */
15843
15844
15845 \f
15846 /***********************************************************************
15847 Building Desired Matrix Rows
15848 ***********************************************************************/
15849
15850 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15851 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15852
15853 static struct glyph_row *
15854 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15855 struct window *w;
15856 Lisp_Object overlay_arrow_string;
15857 {
15858 struct frame *f = XFRAME (WINDOW_FRAME (w));
15859 struct buffer *buffer = XBUFFER (w->buffer);
15860 struct buffer *old = current_buffer;
15861 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15862 int arrow_len = SCHARS (overlay_arrow_string);
15863 const unsigned char *arrow_end = arrow_string + arrow_len;
15864 const unsigned char *p;
15865 struct it it;
15866 int multibyte_p;
15867 int n_glyphs_before;
15868
15869 set_buffer_temp (buffer);
15870 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15871 it.glyph_row->used[TEXT_AREA] = 0;
15872 SET_TEXT_POS (it.position, 0, 0);
15873
15874 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15875 p = arrow_string;
15876 while (p < arrow_end)
15877 {
15878 Lisp_Object face, ilisp;
15879
15880 /* Get the next character. */
15881 if (multibyte_p)
15882 it.c = string_char_and_length (p, arrow_len, &it.len);
15883 else
15884 it.c = *p, it.len = 1;
15885 p += it.len;
15886
15887 /* Get its face. */
15888 ilisp = make_number (p - arrow_string);
15889 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15890 it.face_id = compute_char_face (f, it.c, face);
15891
15892 /* Compute its width, get its glyphs. */
15893 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15894 SET_TEXT_POS (it.position, -1, -1);
15895 PRODUCE_GLYPHS (&it);
15896
15897 /* If this character doesn't fit any more in the line, we have
15898 to remove some glyphs. */
15899 if (it.current_x > it.last_visible_x)
15900 {
15901 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15902 break;
15903 }
15904 }
15905
15906 set_buffer_temp (old);
15907 return it.glyph_row;
15908 }
15909
15910
15911 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15912 glyphs are only inserted for terminal frames since we can't really
15913 win with truncation glyphs when partially visible glyphs are
15914 involved. Which glyphs to insert is determined by
15915 produce_special_glyphs. */
15916
15917 static void
15918 insert_left_trunc_glyphs (it)
15919 struct it *it;
15920 {
15921 struct it truncate_it;
15922 struct glyph *from, *end, *to, *toend;
15923
15924 xassert (!FRAME_WINDOW_P (it->f));
15925
15926 /* Get the truncation glyphs. */
15927 truncate_it = *it;
15928 truncate_it.current_x = 0;
15929 truncate_it.face_id = DEFAULT_FACE_ID;
15930 truncate_it.glyph_row = &scratch_glyph_row;
15931 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15932 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15933 truncate_it.object = make_number (0);
15934 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15935
15936 /* Overwrite glyphs from IT with truncation glyphs. */
15937 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15938 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15939 to = it->glyph_row->glyphs[TEXT_AREA];
15940 toend = to + it->glyph_row->used[TEXT_AREA];
15941
15942 while (from < end)
15943 *to++ = *from++;
15944
15945 /* There may be padding glyphs left over. Overwrite them too. */
15946 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15947 {
15948 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15949 while (from < end)
15950 *to++ = *from++;
15951 }
15952
15953 if (to > toend)
15954 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15955 }
15956
15957
15958 /* Compute the pixel height and width of IT->glyph_row.
15959
15960 Most of the time, ascent and height of a display line will be equal
15961 to the max_ascent and max_height values of the display iterator
15962 structure. This is not the case if
15963
15964 1. We hit ZV without displaying anything. In this case, max_ascent
15965 and max_height will be zero.
15966
15967 2. We have some glyphs that don't contribute to the line height.
15968 (The glyph row flag contributes_to_line_height_p is for future
15969 pixmap extensions).
15970
15971 The first case is easily covered by using default values because in
15972 these cases, the line height does not really matter, except that it
15973 must not be zero. */
15974
15975 static void
15976 compute_line_metrics (it)
15977 struct it *it;
15978 {
15979 struct glyph_row *row = it->glyph_row;
15980 int area, i;
15981
15982 if (FRAME_WINDOW_P (it->f))
15983 {
15984 int i, min_y, max_y;
15985
15986 /* The line may consist of one space only, that was added to
15987 place the cursor on it. If so, the row's height hasn't been
15988 computed yet. */
15989 if (row->height == 0)
15990 {
15991 if (it->max_ascent + it->max_descent == 0)
15992 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15993 row->ascent = it->max_ascent;
15994 row->height = it->max_ascent + it->max_descent;
15995 row->phys_ascent = it->max_phys_ascent;
15996 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15997 row->extra_line_spacing = it->max_extra_line_spacing;
15998 }
15999
16000 /* Compute the width of this line. */
16001 row->pixel_width = row->x;
16002 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16003 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16004
16005 xassert (row->pixel_width >= 0);
16006 xassert (row->ascent >= 0 && row->height > 0);
16007
16008 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16009 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16010
16011 /* If first line's physical ascent is larger than its logical
16012 ascent, use the physical ascent, and make the row taller.
16013 This makes accented characters fully visible. */
16014 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16015 && row->phys_ascent > row->ascent)
16016 {
16017 row->height += row->phys_ascent - row->ascent;
16018 row->ascent = row->phys_ascent;
16019 }
16020
16021 /* Compute how much of the line is visible. */
16022 row->visible_height = row->height;
16023
16024 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16025 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16026
16027 if (row->y < min_y)
16028 row->visible_height -= min_y - row->y;
16029 if (row->y + row->height > max_y)
16030 row->visible_height -= row->y + row->height - max_y;
16031 }
16032 else
16033 {
16034 row->pixel_width = row->used[TEXT_AREA];
16035 if (row->continued_p)
16036 row->pixel_width -= it->continuation_pixel_width;
16037 else if (row->truncated_on_right_p)
16038 row->pixel_width -= it->truncation_pixel_width;
16039 row->ascent = row->phys_ascent = 0;
16040 row->height = row->phys_height = row->visible_height = 1;
16041 row->extra_line_spacing = 0;
16042 }
16043
16044 /* Compute a hash code for this row. */
16045 row->hash = 0;
16046 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16047 for (i = 0; i < row->used[area]; ++i)
16048 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16049 + row->glyphs[area][i].u.val
16050 + row->glyphs[area][i].face_id
16051 + row->glyphs[area][i].padding_p
16052 + (row->glyphs[area][i].type << 2));
16053
16054 it->max_ascent = it->max_descent = 0;
16055 it->max_phys_ascent = it->max_phys_descent = 0;
16056 }
16057
16058
16059 /* Append one space to the glyph row of iterator IT if doing a
16060 window-based redisplay. The space has the same face as
16061 IT->face_id. Value is non-zero if a space was added.
16062
16063 This function is called to make sure that there is always one glyph
16064 at the end of a glyph row that the cursor can be set on under
16065 window-systems. (If there weren't such a glyph we would not know
16066 how wide and tall a box cursor should be displayed).
16067
16068 At the same time this space let's a nicely handle clearing to the
16069 end of the line if the row ends in italic text. */
16070
16071 static int
16072 append_space_for_newline (it, default_face_p)
16073 struct it *it;
16074 int default_face_p;
16075 {
16076 if (FRAME_WINDOW_P (it->f))
16077 {
16078 int n = it->glyph_row->used[TEXT_AREA];
16079
16080 if (it->glyph_row->glyphs[TEXT_AREA] + n
16081 < it->glyph_row->glyphs[1 + TEXT_AREA])
16082 {
16083 /* Save some values that must not be changed.
16084 Must save IT->c and IT->len because otherwise
16085 ITERATOR_AT_END_P wouldn't work anymore after
16086 append_space_for_newline has been called. */
16087 enum display_element_type saved_what = it->what;
16088 int saved_c = it->c, saved_len = it->len;
16089 int saved_x = it->current_x;
16090 int saved_face_id = it->face_id;
16091 struct text_pos saved_pos;
16092 Lisp_Object saved_object;
16093 struct face *face;
16094
16095 saved_object = it->object;
16096 saved_pos = it->position;
16097
16098 it->what = IT_CHARACTER;
16099 bzero (&it->position, sizeof it->position);
16100 it->object = make_number (0);
16101 it->c = ' ';
16102 it->len = 1;
16103
16104 if (default_face_p)
16105 it->face_id = DEFAULT_FACE_ID;
16106 else if (it->face_before_selective_p)
16107 it->face_id = it->saved_face_id;
16108 face = FACE_FROM_ID (it->f, it->face_id);
16109 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16110
16111 PRODUCE_GLYPHS (it);
16112
16113 it->override_ascent = -1;
16114 it->constrain_row_ascent_descent_p = 0;
16115 it->current_x = saved_x;
16116 it->object = saved_object;
16117 it->position = saved_pos;
16118 it->what = saved_what;
16119 it->face_id = saved_face_id;
16120 it->len = saved_len;
16121 it->c = saved_c;
16122 return 1;
16123 }
16124 }
16125
16126 return 0;
16127 }
16128
16129
16130 /* Extend the face of the last glyph in the text area of IT->glyph_row
16131 to the end of the display line. Called from display_line.
16132 If the glyph row is empty, add a space glyph to it so that we
16133 know the face to draw. Set the glyph row flag fill_line_p. */
16134
16135 static void
16136 extend_face_to_end_of_line (it)
16137 struct it *it;
16138 {
16139 struct face *face;
16140 struct frame *f = it->f;
16141
16142 /* If line is already filled, do nothing. */
16143 if (it->current_x >= it->last_visible_x)
16144 return;
16145
16146 /* Face extension extends the background and box of IT->face_id
16147 to the end of the line. If the background equals the background
16148 of the frame, we don't have to do anything. */
16149 if (it->face_before_selective_p)
16150 face = FACE_FROM_ID (it->f, it->saved_face_id);
16151 else
16152 face = FACE_FROM_ID (f, it->face_id);
16153
16154 if (FRAME_WINDOW_P (f)
16155 && it->glyph_row->displays_text_p
16156 && face->box == FACE_NO_BOX
16157 && face->background == FRAME_BACKGROUND_PIXEL (f)
16158 && !face->stipple)
16159 return;
16160
16161 /* Set the glyph row flag indicating that the face of the last glyph
16162 in the text area has to be drawn to the end of the text area. */
16163 it->glyph_row->fill_line_p = 1;
16164
16165 /* If current character of IT is not ASCII, make sure we have the
16166 ASCII face. This will be automatically undone the next time
16167 get_next_display_element returns a multibyte character. Note
16168 that the character will always be single byte in unibyte text. */
16169 if (!ASCII_CHAR_P (it->c))
16170 {
16171 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16172 }
16173
16174 if (FRAME_WINDOW_P (f))
16175 {
16176 /* If the row is empty, add a space with the current face of IT,
16177 so that we know which face to draw. */
16178 if (it->glyph_row->used[TEXT_AREA] == 0)
16179 {
16180 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16181 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16182 it->glyph_row->used[TEXT_AREA] = 1;
16183 }
16184 }
16185 else
16186 {
16187 /* Save some values that must not be changed. */
16188 int saved_x = it->current_x;
16189 struct text_pos saved_pos;
16190 Lisp_Object saved_object;
16191 enum display_element_type saved_what = it->what;
16192 int saved_face_id = it->face_id;
16193
16194 saved_object = it->object;
16195 saved_pos = it->position;
16196
16197 it->what = IT_CHARACTER;
16198 bzero (&it->position, sizeof it->position);
16199 it->object = make_number (0);
16200 it->c = ' ';
16201 it->len = 1;
16202 it->face_id = face->id;
16203
16204 PRODUCE_GLYPHS (it);
16205
16206 while (it->current_x <= it->last_visible_x)
16207 PRODUCE_GLYPHS (it);
16208
16209 /* Don't count these blanks really. It would let us insert a left
16210 truncation glyph below and make us set the cursor on them, maybe. */
16211 it->current_x = saved_x;
16212 it->object = saved_object;
16213 it->position = saved_pos;
16214 it->what = saved_what;
16215 it->face_id = saved_face_id;
16216 }
16217 }
16218
16219
16220 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16221 trailing whitespace. */
16222
16223 static int
16224 trailing_whitespace_p (charpos)
16225 int charpos;
16226 {
16227 int bytepos = CHAR_TO_BYTE (charpos);
16228 int c = 0;
16229
16230 while (bytepos < ZV_BYTE
16231 && (c = FETCH_CHAR (bytepos),
16232 c == ' ' || c == '\t'))
16233 ++bytepos;
16234
16235 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16236 {
16237 if (bytepos != PT_BYTE)
16238 return 1;
16239 }
16240 return 0;
16241 }
16242
16243
16244 /* Highlight trailing whitespace, if any, in ROW. */
16245
16246 void
16247 highlight_trailing_whitespace (f, row)
16248 struct frame *f;
16249 struct glyph_row *row;
16250 {
16251 int used = row->used[TEXT_AREA];
16252
16253 if (used)
16254 {
16255 struct glyph *start = row->glyphs[TEXT_AREA];
16256 struct glyph *glyph = start + used - 1;
16257
16258 /* Skip over glyphs inserted to display the cursor at the
16259 end of a line, for extending the face of the last glyph
16260 to the end of the line on terminals, and for truncation
16261 and continuation glyphs. */
16262 while (glyph >= start
16263 && glyph->type == CHAR_GLYPH
16264 && INTEGERP (glyph->object))
16265 --glyph;
16266
16267 /* If last glyph is a space or stretch, and it's trailing
16268 whitespace, set the face of all trailing whitespace glyphs in
16269 IT->glyph_row to `trailing-whitespace'. */
16270 if (glyph >= start
16271 && BUFFERP (glyph->object)
16272 && (glyph->type == STRETCH_GLYPH
16273 || (glyph->type == CHAR_GLYPH
16274 && glyph->u.ch == ' '))
16275 && trailing_whitespace_p (glyph->charpos))
16276 {
16277 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16278 if (face_id < 0)
16279 return;
16280
16281 while (glyph >= start
16282 && BUFFERP (glyph->object)
16283 && (glyph->type == STRETCH_GLYPH
16284 || (glyph->type == CHAR_GLYPH
16285 && glyph->u.ch == ' ')))
16286 (glyph--)->face_id = face_id;
16287 }
16288 }
16289 }
16290
16291
16292 /* Value is non-zero if glyph row ROW in window W should be
16293 used to hold the cursor. */
16294
16295 static int
16296 cursor_row_p (w, row)
16297 struct window *w;
16298 struct glyph_row *row;
16299 {
16300 int cursor_row_p = 1;
16301
16302 if (PT == MATRIX_ROW_END_CHARPOS (row))
16303 {
16304 /* Suppose the row ends on a string.
16305 Unless the row is continued, that means it ends on a newline
16306 in the string. If it's anything other than a display string
16307 (e.g. a before-string from an overlay), we don't want the
16308 cursor there. (This heuristic seems to give the optimal
16309 behavior for the various types of multi-line strings.) */
16310 if (CHARPOS (row->end.string_pos) >= 0)
16311 {
16312 if (row->continued_p)
16313 cursor_row_p = 1;
16314 else
16315 {
16316 /* Check for `display' property. */
16317 struct glyph *beg = row->glyphs[TEXT_AREA];
16318 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16319 struct glyph *glyph;
16320
16321 cursor_row_p = 0;
16322 for (glyph = end; glyph >= beg; --glyph)
16323 if (STRINGP (glyph->object))
16324 {
16325 Lisp_Object prop
16326 = Fget_char_property (make_number (PT),
16327 Qdisplay, Qnil);
16328 cursor_row_p =
16329 (!NILP (prop)
16330 && display_prop_string_p (prop, glyph->object));
16331 break;
16332 }
16333 }
16334 }
16335 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16336 {
16337 /* If the row ends in middle of a real character,
16338 and the line is continued, we want the cursor here.
16339 That's because MATRIX_ROW_END_CHARPOS would equal
16340 PT if PT is before the character. */
16341 if (!row->ends_in_ellipsis_p)
16342 cursor_row_p = row->continued_p;
16343 else
16344 /* If the row ends in an ellipsis, then
16345 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16346 We want that position to be displayed after the ellipsis. */
16347 cursor_row_p = 0;
16348 }
16349 /* If the row ends at ZV, display the cursor at the end of that
16350 row instead of at the start of the row below. */
16351 else if (row->ends_at_zv_p)
16352 cursor_row_p = 1;
16353 else
16354 cursor_row_p = 0;
16355 }
16356
16357 return cursor_row_p;
16358 }
16359
16360 \f
16361
16362 /* Push the display property PROP so that it will be rendered at the
16363 current position in IT. */
16364
16365 static void
16366 push_display_prop (struct it *it, Lisp_Object prop)
16367 {
16368 push_it (it);
16369
16370 /* Never display a cursor on the prefix. */
16371 it->avoid_cursor_p = 1;
16372
16373 if (STRINGP (prop))
16374 {
16375 if (SCHARS (prop) == 0)
16376 {
16377 pop_it (it);
16378 return;
16379 }
16380
16381 it->string = prop;
16382 it->multibyte_p = STRING_MULTIBYTE (it->string);
16383 it->current.overlay_string_index = -1;
16384 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16385 it->end_charpos = it->string_nchars = SCHARS (it->string);
16386 it->method = GET_FROM_STRING;
16387 it->stop_charpos = 0;
16388 }
16389 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16390 {
16391 it->method = GET_FROM_STRETCH;
16392 it->object = prop;
16393 }
16394 #ifdef HAVE_WINDOW_SYSTEM
16395 else if (IMAGEP (prop))
16396 {
16397 it->what = IT_IMAGE;
16398 it->image_id = lookup_image (it->f, prop);
16399 it->method = GET_FROM_IMAGE;
16400 }
16401 #endif /* HAVE_WINDOW_SYSTEM */
16402 else
16403 {
16404 pop_it (it); /* bogus display property, give up */
16405 return;
16406 }
16407 }
16408
16409 /* Return the character-property PROP at the current position in IT. */
16410
16411 static Lisp_Object
16412 get_it_property (it, prop)
16413 struct it *it;
16414 Lisp_Object prop;
16415 {
16416 Lisp_Object position;
16417
16418 if (STRINGP (it->object))
16419 position = make_number (IT_STRING_CHARPOS (*it));
16420 else if (BUFFERP (it->object))
16421 position = make_number (IT_CHARPOS (*it));
16422 else
16423 return Qnil;
16424
16425 return Fget_char_property (position, prop, it->object);
16426 }
16427
16428 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16429
16430 static void
16431 handle_line_prefix (struct it *it)
16432 {
16433 Lisp_Object prefix;
16434 if (it->continuation_lines_width > 0)
16435 {
16436 prefix = get_it_property (it, Qwrap_prefix);
16437 if (NILP (prefix))
16438 prefix = Vwrap_prefix;
16439 }
16440 else
16441 {
16442 prefix = get_it_property (it, Qline_prefix);
16443 if (NILP (prefix))
16444 prefix = Vline_prefix;
16445 }
16446 if (! NILP (prefix))
16447 push_display_prop (it, prefix);
16448 }
16449
16450 \f
16451
16452 /* Construct the glyph row IT->glyph_row in the desired matrix of
16453 IT->w from text at the current position of IT. See dispextern.h
16454 for an overview of struct it. Value is non-zero if
16455 IT->glyph_row displays text, as opposed to a line displaying ZV
16456 only. */
16457
16458 static int
16459 display_line (it)
16460 struct it *it;
16461 {
16462 struct glyph_row *row = it->glyph_row;
16463 Lisp_Object overlay_arrow_string;
16464 struct it wrap_it;
16465 int may_wrap = 0, wrap_x;
16466 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16467 int wrap_row_phys_ascent, wrap_row_phys_height;
16468 int wrap_row_extra_line_spacing;
16469
16470 /* We always start displaying at hpos zero even if hscrolled. */
16471 xassert (it->hpos == 0 && it->current_x == 0);
16472
16473 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16474 >= it->w->desired_matrix->nrows)
16475 {
16476 it->w->nrows_scale_factor++;
16477 fonts_changed_p = 1;
16478 return 0;
16479 }
16480
16481 /* Is IT->w showing the region? */
16482 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16483
16484 /* Clear the result glyph row and enable it. */
16485 prepare_desired_row (row);
16486
16487 row->y = it->current_y;
16488 row->start = it->start;
16489 row->continuation_lines_width = it->continuation_lines_width;
16490 row->displays_text_p = 1;
16491 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16492 it->starts_in_middle_of_char_p = 0;
16493
16494 /* Arrange the overlays nicely for our purposes. Usually, we call
16495 display_line on only one line at a time, in which case this
16496 can't really hurt too much, or we call it on lines which appear
16497 one after another in the buffer, in which case all calls to
16498 recenter_overlay_lists but the first will be pretty cheap. */
16499 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16500
16501 /* Move over display elements that are not visible because we are
16502 hscrolled. This may stop at an x-position < IT->first_visible_x
16503 if the first glyph is partially visible or if we hit a line end. */
16504 if (it->current_x < it->first_visible_x)
16505 {
16506 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16507 MOVE_TO_POS | MOVE_TO_X);
16508 }
16509 else
16510 {
16511 /* We only do this when not calling `move_it_in_display_line_to'
16512 above, because move_it_in_display_line_to calls
16513 handle_line_prefix itself. */
16514 handle_line_prefix (it);
16515 }
16516
16517 /* Get the initial row height. This is either the height of the
16518 text hscrolled, if there is any, or zero. */
16519 row->ascent = it->max_ascent;
16520 row->height = it->max_ascent + it->max_descent;
16521 row->phys_ascent = it->max_phys_ascent;
16522 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16523 row->extra_line_spacing = it->max_extra_line_spacing;
16524
16525 /* Loop generating characters. The loop is left with IT on the next
16526 character to display. */
16527 while (1)
16528 {
16529 int n_glyphs_before, hpos_before, x_before;
16530 int x, i, nglyphs;
16531 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16532
16533 /* Retrieve the next thing to display. Value is zero if end of
16534 buffer reached. */
16535 if (!get_next_display_element (it))
16536 {
16537 /* Maybe add a space at the end of this line that is used to
16538 display the cursor there under X. Set the charpos of the
16539 first glyph of blank lines not corresponding to any text
16540 to -1. */
16541 #ifdef HAVE_WINDOW_SYSTEM
16542 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16543 row->exact_window_width_line_p = 1;
16544 else
16545 #endif /* HAVE_WINDOW_SYSTEM */
16546 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16547 || row->used[TEXT_AREA] == 0)
16548 {
16549 row->glyphs[TEXT_AREA]->charpos = -1;
16550 row->displays_text_p = 0;
16551
16552 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16553 && (!MINI_WINDOW_P (it->w)
16554 || (minibuf_level && EQ (it->window, minibuf_window))))
16555 row->indicate_empty_line_p = 1;
16556 }
16557
16558 it->continuation_lines_width = 0;
16559 row->ends_at_zv_p = 1;
16560 break;
16561 }
16562
16563 /* Now, get the metrics of what we want to display. This also
16564 generates glyphs in `row' (which is IT->glyph_row). */
16565 n_glyphs_before = row->used[TEXT_AREA];
16566 x = it->current_x;
16567
16568 /* Remember the line height so far in case the next element doesn't
16569 fit on the line. */
16570 if (it->line_wrap != TRUNCATE)
16571 {
16572 ascent = it->max_ascent;
16573 descent = it->max_descent;
16574 phys_ascent = it->max_phys_ascent;
16575 phys_descent = it->max_phys_descent;
16576
16577 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16578 {
16579 if (IT_DISPLAYING_WHITESPACE (it))
16580 may_wrap = 1;
16581 else if (may_wrap)
16582 {
16583 wrap_it = *it;
16584 wrap_x = x;
16585 wrap_row_used = row->used[TEXT_AREA];
16586 wrap_row_ascent = row->ascent;
16587 wrap_row_height = row->height;
16588 wrap_row_phys_ascent = row->phys_ascent;
16589 wrap_row_phys_height = row->phys_height;
16590 wrap_row_extra_line_spacing = row->extra_line_spacing;
16591 may_wrap = 0;
16592 }
16593 }
16594 }
16595
16596 PRODUCE_GLYPHS (it);
16597
16598 /* If this display element was in marginal areas, continue with
16599 the next one. */
16600 if (it->area != TEXT_AREA)
16601 {
16602 row->ascent = max (row->ascent, it->max_ascent);
16603 row->height = max (row->height, it->max_ascent + it->max_descent);
16604 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16605 row->phys_height = max (row->phys_height,
16606 it->max_phys_ascent + it->max_phys_descent);
16607 row->extra_line_spacing = max (row->extra_line_spacing,
16608 it->max_extra_line_spacing);
16609 set_iterator_to_next (it, 1);
16610 continue;
16611 }
16612
16613 /* Does the display element fit on the line? If we truncate
16614 lines, we should draw past the right edge of the window. If
16615 we don't truncate, we want to stop so that we can display the
16616 continuation glyph before the right margin. If lines are
16617 continued, there are two possible strategies for characters
16618 resulting in more than 1 glyph (e.g. tabs): Display as many
16619 glyphs as possible in this line and leave the rest for the
16620 continuation line, or display the whole element in the next
16621 line. Original redisplay did the former, so we do it also. */
16622 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16623 hpos_before = it->hpos;
16624 x_before = x;
16625
16626 if (/* Not a newline. */
16627 nglyphs > 0
16628 /* Glyphs produced fit entirely in the line. */
16629 && it->current_x < it->last_visible_x)
16630 {
16631 it->hpos += nglyphs;
16632 row->ascent = max (row->ascent, it->max_ascent);
16633 row->height = max (row->height, it->max_ascent + it->max_descent);
16634 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16635 row->phys_height = max (row->phys_height,
16636 it->max_phys_ascent + it->max_phys_descent);
16637 row->extra_line_spacing = max (row->extra_line_spacing,
16638 it->max_extra_line_spacing);
16639 if (it->current_x - it->pixel_width < it->first_visible_x)
16640 row->x = x - it->first_visible_x;
16641 }
16642 else
16643 {
16644 int new_x;
16645 struct glyph *glyph;
16646
16647 for (i = 0; i < nglyphs; ++i, x = new_x)
16648 {
16649 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16650 new_x = x + glyph->pixel_width;
16651
16652 if (/* Lines are continued. */
16653 it->line_wrap != TRUNCATE
16654 && (/* Glyph doesn't fit on the line. */
16655 new_x > it->last_visible_x
16656 /* Or it fits exactly on a window system frame. */
16657 || (new_x == it->last_visible_x
16658 && FRAME_WINDOW_P (it->f))))
16659 {
16660 /* End of a continued line. */
16661
16662 if (it->hpos == 0
16663 || (new_x == it->last_visible_x
16664 && FRAME_WINDOW_P (it->f)))
16665 {
16666 /* Current glyph is the only one on the line or
16667 fits exactly on the line. We must continue
16668 the line because we can't draw the cursor
16669 after the glyph. */
16670 row->continued_p = 1;
16671 it->current_x = new_x;
16672 it->continuation_lines_width += new_x;
16673 ++it->hpos;
16674 if (i == nglyphs - 1)
16675 {
16676 /* If line-wrap is on, check if a previous
16677 wrap point was found. */
16678 if (wrap_row_used > 0
16679 /* Even if there is a previous wrap
16680 point, continue the line here as
16681 usual, if (i) the previous character
16682 was a space or tab AND (ii) the
16683 current character is not. */
16684 && (!may_wrap
16685 || IT_DISPLAYING_WHITESPACE (it)))
16686 goto back_to_wrap;
16687
16688 set_iterator_to_next (it, 1);
16689 #ifdef HAVE_WINDOW_SYSTEM
16690 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16691 {
16692 if (!get_next_display_element (it))
16693 {
16694 row->exact_window_width_line_p = 1;
16695 it->continuation_lines_width = 0;
16696 row->continued_p = 0;
16697 row->ends_at_zv_p = 1;
16698 }
16699 else if (ITERATOR_AT_END_OF_LINE_P (it))
16700 {
16701 row->continued_p = 0;
16702 row->exact_window_width_line_p = 1;
16703 }
16704 }
16705 #endif /* HAVE_WINDOW_SYSTEM */
16706 }
16707 }
16708 else if (CHAR_GLYPH_PADDING_P (*glyph)
16709 && !FRAME_WINDOW_P (it->f))
16710 {
16711 /* A padding glyph that doesn't fit on this line.
16712 This means the whole character doesn't fit
16713 on the line. */
16714 row->used[TEXT_AREA] = n_glyphs_before;
16715
16716 /* Fill the rest of the row with continuation
16717 glyphs like in 20.x. */
16718 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16719 < row->glyphs[1 + TEXT_AREA])
16720 produce_special_glyphs (it, IT_CONTINUATION);
16721
16722 row->continued_p = 1;
16723 it->current_x = x_before;
16724 it->continuation_lines_width += x_before;
16725
16726 /* Restore the height to what it was before the
16727 element not fitting on the line. */
16728 it->max_ascent = ascent;
16729 it->max_descent = descent;
16730 it->max_phys_ascent = phys_ascent;
16731 it->max_phys_descent = phys_descent;
16732 }
16733 else if (wrap_row_used > 0)
16734 {
16735 back_to_wrap:
16736 *it = wrap_it;
16737 it->continuation_lines_width += wrap_x;
16738 row->used[TEXT_AREA] = wrap_row_used;
16739 row->ascent = wrap_row_ascent;
16740 row->height = wrap_row_height;
16741 row->phys_ascent = wrap_row_phys_ascent;
16742 row->phys_height = wrap_row_phys_height;
16743 row->extra_line_spacing = wrap_row_extra_line_spacing;
16744 row->continued_p = 1;
16745 row->ends_at_zv_p = 0;
16746 row->exact_window_width_line_p = 0;
16747 it->continuation_lines_width += x;
16748
16749 /* Make sure that a non-default face is extended
16750 up to the right margin of the window. */
16751 extend_face_to_end_of_line (it);
16752 }
16753 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16754 {
16755 /* A TAB that extends past the right edge of the
16756 window. This produces a single glyph on
16757 window system frames. We leave the glyph in
16758 this row and let it fill the row, but don't
16759 consume the TAB. */
16760 it->continuation_lines_width += it->last_visible_x;
16761 row->ends_in_middle_of_char_p = 1;
16762 row->continued_p = 1;
16763 glyph->pixel_width = it->last_visible_x - x;
16764 it->starts_in_middle_of_char_p = 1;
16765 }
16766 else
16767 {
16768 /* Something other than a TAB that draws past
16769 the right edge of the window. Restore
16770 positions to values before the element. */
16771 row->used[TEXT_AREA] = n_glyphs_before + i;
16772
16773 /* Display continuation glyphs. */
16774 if (!FRAME_WINDOW_P (it->f))
16775 produce_special_glyphs (it, IT_CONTINUATION);
16776 row->continued_p = 1;
16777
16778 it->current_x = x_before;
16779 it->continuation_lines_width += x;
16780 extend_face_to_end_of_line (it);
16781
16782 if (nglyphs > 1 && i > 0)
16783 {
16784 row->ends_in_middle_of_char_p = 1;
16785 it->starts_in_middle_of_char_p = 1;
16786 }
16787
16788 /* Restore the height to what it was before the
16789 element not fitting on the line. */
16790 it->max_ascent = ascent;
16791 it->max_descent = descent;
16792 it->max_phys_ascent = phys_ascent;
16793 it->max_phys_descent = phys_descent;
16794 }
16795
16796 break;
16797 }
16798 else if (new_x > it->first_visible_x)
16799 {
16800 /* Increment number of glyphs actually displayed. */
16801 ++it->hpos;
16802
16803 if (x < it->first_visible_x)
16804 /* Glyph is partially visible, i.e. row starts at
16805 negative X position. */
16806 row->x = x - it->first_visible_x;
16807 }
16808 else
16809 {
16810 /* Glyph is completely off the left margin of the
16811 window. This should not happen because of the
16812 move_it_in_display_line at the start of this
16813 function, unless the text display area of the
16814 window is empty. */
16815 xassert (it->first_visible_x <= it->last_visible_x);
16816 }
16817 }
16818
16819 row->ascent = max (row->ascent, it->max_ascent);
16820 row->height = max (row->height, it->max_ascent + it->max_descent);
16821 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16822 row->phys_height = max (row->phys_height,
16823 it->max_phys_ascent + it->max_phys_descent);
16824 row->extra_line_spacing = max (row->extra_line_spacing,
16825 it->max_extra_line_spacing);
16826
16827 /* End of this display line if row is continued. */
16828 if (row->continued_p || row->ends_at_zv_p)
16829 break;
16830 }
16831
16832 at_end_of_line:
16833 /* Is this a line end? If yes, we're also done, after making
16834 sure that a non-default face is extended up to the right
16835 margin of the window. */
16836 if (ITERATOR_AT_END_OF_LINE_P (it))
16837 {
16838 int used_before = row->used[TEXT_AREA];
16839
16840 row->ends_in_newline_from_string_p = STRINGP (it->object);
16841
16842 #ifdef HAVE_WINDOW_SYSTEM
16843 /* Add a space at the end of the line that is used to
16844 display the cursor there. */
16845 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16846 append_space_for_newline (it, 0);
16847 #endif /* HAVE_WINDOW_SYSTEM */
16848
16849 /* Extend the face to the end of the line. */
16850 extend_face_to_end_of_line (it);
16851
16852 /* Make sure we have the position. */
16853 if (used_before == 0)
16854 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16855
16856 /* Consume the line end. This skips over invisible lines. */
16857 set_iterator_to_next (it, 1);
16858 it->continuation_lines_width = 0;
16859 break;
16860 }
16861
16862 /* Proceed with next display element. Note that this skips
16863 over lines invisible because of selective display. */
16864 set_iterator_to_next (it, 1);
16865
16866 /* If we truncate lines, we are done when the last displayed
16867 glyphs reach past the right margin of the window. */
16868 if (it->line_wrap == TRUNCATE
16869 && (FRAME_WINDOW_P (it->f)
16870 ? (it->current_x >= it->last_visible_x)
16871 : (it->current_x > it->last_visible_x)))
16872 {
16873 /* Maybe add truncation glyphs. */
16874 if (!FRAME_WINDOW_P (it->f))
16875 {
16876 int i, n;
16877
16878 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16879 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16880 break;
16881
16882 for (n = row->used[TEXT_AREA]; i < n; ++i)
16883 {
16884 row->used[TEXT_AREA] = i;
16885 produce_special_glyphs (it, IT_TRUNCATION);
16886 }
16887 }
16888 #ifdef HAVE_WINDOW_SYSTEM
16889 else
16890 {
16891 /* Don't truncate if we can overflow newline into fringe. */
16892 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16893 {
16894 if (!get_next_display_element (it))
16895 {
16896 it->continuation_lines_width = 0;
16897 row->ends_at_zv_p = 1;
16898 row->exact_window_width_line_p = 1;
16899 break;
16900 }
16901 if (ITERATOR_AT_END_OF_LINE_P (it))
16902 {
16903 row->exact_window_width_line_p = 1;
16904 goto at_end_of_line;
16905 }
16906 }
16907 }
16908 #endif /* HAVE_WINDOW_SYSTEM */
16909
16910 row->truncated_on_right_p = 1;
16911 it->continuation_lines_width = 0;
16912 reseat_at_next_visible_line_start (it, 0);
16913 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16914 it->hpos = hpos_before;
16915 it->current_x = x_before;
16916 break;
16917 }
16918 }
16919
16920 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16921 at the left window margin. */
16922 if (it->first_visible_x
16923 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16924 {
16925 if (!FRAME_WINDOW_P (it->f))
16926 insert_left_trunc_glyphs (it);
16927 row->truncated_on_left_p = 1;
16928 }
16929
16930 /* If the start of this line is the overlay arrow-position, then
16931 mark this glyph row as the one containing the overlay arrow.
16932 This is clearly a mess with variable size fonts. It would be
16933 better to let it be displayed like cursors under X. */
16934 if ((row->displays_text_p || !overlay_arrow_seen)
16935 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16936 !NILP (overlay_arrow_string)))
16937 {
16938 /* Overlay arrow in window redisplay is a fringe bitmap. */
16939 if (STRINGP (overlay_arrow_string))
16940 {
16941 struct glyph_row *arrow_row
16942 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16943 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16944 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16945 struct glyph *p = row->glyphs[TEXT_AREA];
16946 struct glyph *p2, *end;
16947
16948 /* Copy the arrow glyphs. */
16949 while (glyph < arrow_end)
16950 *p++ = *glyph++;
16951
16952 /* Throw away padding glyphs. */
16953 p2 = p;
16954 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16955 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16956 ++p2;
16957 if (p2 > p)
16958 {
16959 while (p2 < end)
16960 *p++ = *p2++;
16961 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16962 }
16963 }
16964 else
16965 {
16966 xassert (INTEGERP (overlay_arrow_string));
16967 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16968 }
16969 overlay_arrow_seen = 1;
16970 }
16971
16972 /* Compute pixel dimensions of this line. */
16973 compute_line_metrics (it);
16974
16975 /* Remember the position at which this line ends. */
16976 row->end = it->current;
16977
16978 /* Record whether this row ends inside an ellipsis. */
16979 row->ends_in_ellipsis_p
16980 = (it->method == GET_FROM_DISPLAY_VECTOR
16981 && it->ellipsis_p);
16982
16983 /* Save fringe bitmaps in this row. */
16984 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16985 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16986 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16987 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16988
16989 it->left_user_fringe_bitmap = 0;
16990 it->left_user_fringe_face_id = 0;
16991 it->right_user_fringe_bitmap = 0;
16992 it->right_user_fringe_face_id = 0;
16993
16994 /* Maybe set the cursor. */
16995 if (it->w->cursor.vpos < 0
16996 && PT >= MATRIX_ROW_START_CHARPOS (row)
16997 && PT <= MATRIX_ROW_END_CHARPOS (row)
16998 && cursor_row_p (it->w, row))
16999 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17000
17001 /* Highlight trailing whitespace. */
17002 if (!NILP (Vshow_trailing_whitespace))
17003 highlight_trailing_whitespace (it->f, it->glyph_row);
17004
17005 /* Prepare for the next line. This line starts horizontally at (X
17006 HPOS) = (0 0). Vertical positions are incremented. As a
17007 convenience for the caller, IT->glyph_row is set to the next
17008 row to be used. */
17009 it->current_x = it->hpos = 0;
17010 it->current_y += row->height;
17011 ++it->vpos;
17012 ++it->glyph_row;
17013 it->start = it->current;
17014 return row->displays_text_p;
17015 }
17016
17017
17018 \f
17019 /***********************************************************************
17020 Menu Bar
17021 ***********************************************************************/
17022
17023 /* Redisplay the menu bar in the frame for window W.
17024
17025 The menu bar of X frames that don't have X toolkit support is
17026 displayed in a special window W->frame->menu_bar_window.
17027
17028 The menu bar of terminal frames is treated specially as far as
17029 glyph matrices are concerned. Menu bar lines are not part of
17030 windows, so the update is done directly on the frame matrix rows
17031 for the menu bar. */
17032
17033 static void
17034 display_menu_bar (w)
17035 struct window *w;
17036 {
17037 struct frame *f = XFRAME (WINDOW_FRAME (w));
17038 struct it it;
17039 Lisp_Object items;
17040 int i;
17041
17042 /* Don't do all this for graphical frames. */
17043 #ifdef HAVE_NTGUI
17044 if (FRAME_W32_P (f))
17045 return;
17046 #endif
17047 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17048 if (FRAME_X_P (f))
17049 return;
17050 #endif
17051 #ifdef MAC_OS
17052 if (FRAME_MAC_P (f))
17053 return;
17054 #endif
17055
17056 #ifdef USE_X_TOOLKIT
17057 xassert (!FRAME_WINDOW_P (f));
17058 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17059 it.first_visible_x = 0;
17060 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17061 #else /* not USE_X_TOOLKIT */
17062 if (FRAME_WINDOW_P (f))
17063 {
17064 /* Menu bar lines are displayed in the desired matrix of the
17065 dummy window menu_bar_window. */
17066 struct window *menu_w;
17067 xassert (WINDOWP (f->menu_bar_window));
17068 menu_w = XWINDOW (f->menu_bar_window);
17069 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17070 MENU_FACE_ID);
17071 it.first_visible_x = 0;
17072 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17073 }
17074 else
17075 {
17076 /* This is a TTY frame, i.e. character hpos/vpos are used as
17077 pixel x/y. */
17078 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17079 MENU_FACE_ID);
17080 it.first_visible_x = 0;
17081 it.last_visible_x = FRAME_COLS (f);
17082 }
17083 #endif /* not USE_X_TOOLKIT */
17084
17085 if (! mode_line_inverse_video)
17086 /* Force the menu-bar to be displayed in the default face. */
17087 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17088
17089 /* Clear all rows of the menu bar. */
17090 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17091 {
17092 struct glyph_row *row = it.glyph_row + i;
17093 clear_glyph_row (row);
17094 row->enabled_p = 1;
17095 row->full_width_p = 1;
17096 }
17097
17098 /* Display all items of the menu bar. */
17099 items = FRAME_MENU_BAR_ITEMS (it.f);
17100 for (i = 0; i < XVECTOR (items)->size; i += 4)
17101 {
17102 Lisp_Object string;
17103
17104 /* Stop at nil string. */
17105 string = AREF (items, i + 1);
17106 if (NILP (string))
17107 break;
17108
17109 /* Remember where item was displayed. */
17110 ASET (items, i + 3, make_number (it.hpos));
17111
17112 /* Display the item, pad with one space. */
17113 if (it.current_x < it.last_visible_x)
17114 display_string (NULL, string, Qnil, 0, 0, &it,
17115 SCHARS (string) + 1, 0, 0, -1);
17116 }
17117
17118 /* Fill out the line with spaces. */
17119 if (it.current_x < it.last_visible_x)
17120 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17121
17122 /* Compute the total height of the lines. */
17123 compute_line_metrics (&it);
17124 }
17125
17126
17127 \f
17128 /***********************************************************************
17129 Mode Line
17130 ***********************************************************************/
17131
17132 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17133 FORCE is non-zero, redisplay mode lines unconditionally.
17134 Otherwise, redisplay only mode lines that are garbaged. Value is
17135 the number of windows whose mode lines were redisplayed. */
17136
17137 static int
17138 redisplay_mode_lines (window, force)
17139 Lisp_Object window;
17140 int force;
17141 {
17142 int nwindows = 0;
17143
17144 while (!NILP (window))
17145 {
17146 struct window *w = XWINDOW (window);
17147
17148 if (WINDOWP (w->hchild))
17149 nwindows += redisplay_mode_lines (w->hchild, force);
17150 else if (WINDOWP (w->vchild))
17151 nwindows += redisplay_mode_lines (w->vchild, force);
17152 else if (force
17153 || FRAME_GARBAGED_P (XFRAME (w->frame))
17154 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17155 {
17156 struct text_pos lpoint;
17157 struct buffer *old = current_buffer;
17158
17159 /* Set the window's buffer for the mode line display. */
17160 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17161 set_buffer_internal_1 (XBUFFER (w->buffer));
17162
17163 /* Point refers normally to the selected window. For any
17164 other window, set up appropriate value. */
17165 if (!EQ (window, selected_window))
17166 {
17167 struct text_pos pt;
17168
17169 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17170 if (CHARPOS (pt) < BEGV)
17171 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17172 else if (CHARPOS (pt) > (ZV - 1))
17173 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17174 else
17175 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17176 }
17177
17178 /* Display mode lines. */
17179 clear_glyph_matrix (w->desired_matrix);
17180 if (display_mode_lines (w))
17181 {
17182 ++nwindows;
17183 w->must_be_updated_p = 1;
17184 }
17185
17186 /* Restore old settings. */
17187 set_buffer_internal_1 (old);
17188 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17189 }
17190
17191 window = w->next;
17192 }
17193
17194 return nwindows;
17195 }
17196
17197
17198 /* Display the mode and/or top line of window W. Value is the number
17199 of mode lines displayed. */
17200
17201 static int
17202 display_mode_lines (w)
17203 struct window *w;
17204 {
17205 Lisp_Object old_selected_window, old_selected_frame;
17206 int n = 0;
17207
17208 old_selected_frame = selected_frame;
17209 selected_frame = w->frame;
17210 old_selected_window = selected_window;
17211 XSETWINDOW (selected_window, w);
17212
17213 /* These will be set while the mode line specs are processed. */
17214 line_number_displayed = 0;
17215 w->column_number_displayed = Qnil;
17216
17217 if (WINDOW_WANTS_MODELINE_P (w))
17218 {
17219 struct window *sel_w = XWINDOW (old_selected_window);
17220
17221 /* Select mode line face based on the real selected window. */
17222 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17223 current_buffer->mode_line_format);
17224 ++n;
17225 }
17226
17227 if (WINDOW_WANTS_HEADER_LINE_P (w))
17228 {
17229 display_mode_line (w, HEADER_LINE_FACE_ID,
17230 current_buffer->header_line_format);
17231 ++n;
17232 }
17233
17234 selected_frame = old_selected_frame;
17235 selected_window = old_selected_window;
17236 return n;
17237 }
17238
17239
17240 /* Display mode or top line of window W. FACE_ID specifies which line
17241 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17242 FORMAT is the mode line format to display. Value is the pixel
17243 height of the mode line displayed. */
17244
17245 static int
17246 display_mode_line (w, face_id, format)
17247 struct window *w;
17248 enum face_id face_id;
17249 Lisp_Object format;
17250 {
17251 struct it it;
17252 struct face *face;
17253 int count = SPECPDL_INDEX ();
17254
17255 init_iterator (&it, w, -1, -1, NULL, face_id);
17256 /* Don't extend on a previously drawn mode-line.
17257 This may happen if called from pos_visible_p. */
17258 it.glyph_row->enabled_p = 0;
17259 prepare_desired_row (it.glyph_row);
17260
17261 it.glyph_row->mode_line_p = 1;
17262
17263 if (! mode_line_inverse_video)
17264 /* Force the mode-line to be displayed in the default face. */
17265 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17266
17267 record_unwind_protect (unwind_format_mode_line,
17268 format_mode_line_unwind_data (NULL, Qnil, 0));
17269
17270 mode_line_target = MODE_LINE_DISPLAY;
17271
17272 /* Temporarily make frame's keyboard the current kboard so that
17273 kboard-local variables in the mode_line_format will get the right
17274 values. */
17275 push_kboard (FRAME_KBOARD (it.f));
17276 record_unwind_save_match_data ();
17277 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17278 pop_kboard ();
17279
17280 unbind_to (count, Qnil);
17281
17282 /* Fill up with spaces. */
17283 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17284
17285 compute_line_metrics (&it);
17286 it.glyph_row->full_width_p = 1;
17287 it.glyph_row->continued_p = 0;
17288 it.glyph_row->truncated_on_left_p = 0;
17289 it.glyph_row->truncated_on_right_p = 0;
17290
17291 /* Make a 3D mode-line have a shadow at its right end. */
17292 face = FACE_FROM_ID (it.f, face_id);
17293 extend_face_to_end_of_line (&it);
17294 if (face->box != FACE_NO_BOX)
17295 {
17296 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17297 + it.glyph_row->used[TEXT_AREA] - 1);
17298 last->right_box_line_p = 1;
17299 }
17300
17301 return it.glyph_row->height;
17302 }
17303
17304 /* Move element ELT in LIST to the front of LIST.
17305 Return the updated list. */
17306
17307 static Lisp_Object
17308 move_elt_to_front (elt, list)
17309 Lisp_Object elt, list;
17310 {
17311 register Lisp_Object tail, prev;
17312 register Lisp_Object tem;
17313
17314 tail = list;
17315 prev = Qnil;
17316 while (CONSP (tail))
17317 {
17318 tem = XCAR (tail);
17319
17320 if (EQ (elt, tem))
17321 {
17322 /* Splice out the link TAIL. */
17323 if (NILP (prev))
17324 list = XCDR (tail);
17325 else
17326 Fsetcdr (prev, XCDR (tail));
17327
17328 /* Now make it the first. */
17329 Fsetcdr (tail, list);
17330 return tail;
17331 }
17332 else
17333 prev = tail;
17334 tail = XCDR (tail);
17335 QUIT;
17336 }
17337
17338 /* Not found--return unchanged LIST. */
17339 return list;
17340 }
17341
17342 /* Contribute ELT to the mode line for window IT->w. How it
17343 translates into text depends on its data type.
17344
17345 IT describes the display environment in which we display, as usual.
17346
17347 DEPTH is the depth in recursion. It is used to prevent
17348 infinite recursion here.
17349
17350 FIELD_WIDTH is the number of characters the display of ELT should
17351 occupy in the mode line, and PRECISION is the maximum number of
17352 characters to display from ELT's representation. See
17353 display_string for details.
17354
17355 Returns the hpos of the end of the text generated by ELT.
17356
17357 PROPS is a property list to add to any string we encounter.
17358
17359 If RISKY is nonzero, remove (disregard) any properties in any string
17360 we encounter, and ignore :eval and :propertize.
17361
17362 The global variable `mode_line_target' determines whether the
17363 output is passed to `store_mode_line_noprop',
17364 `store_mode_line_string', or `display_string'. */
17365
17366 static int
17367 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17368 struct it *it;
17369 int depth;
17370 int field_width, precision;
17371 Lisp_Object elt, props;
17372 int risky;
17373 {
17374 int n = 0, field, prec;
17375 int literal = 0;
17376
17377 tail_recurse:
17378 if (depth > 100)
17379 elt = build_string ("*too-deep*");
17380
17381 depth++;
17382
17383 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17384 {
17385 case Lisp_String:
17386 {
17387 /* A string: output it and check for %-constructs within it. */
17388 unsigned char c;
17389 int offset = 0;
17390
17391 if (SCHARS (elt) > 0
17392 && (!NILP (props) || risky))
17393 {
17394 Lisp_Object oprops, aelt;
17395 oprops = Ftext_properties_at (make_number (0), elt);
17396
17397 /* If the starting string's properties are not what
17398 we want, translate the string. Also, if the string
17399 is risky, do that anyway. */
17400
17401 if (NILP (Fequal (props, oprops)) || risky)
17402 {
17403 /* If the starting string has properties,
17404 merge the specified ones onto the existing ones. */
17405 if (! NILP (oprops) && !risky)
17406 {
17407 Lisp_Object tem;
17408
17409 oprops = Fcopy_sequence (oprops);
17410 tem = props;
17411 while (CONSP (tem))
17412 {
17413 oprops = Fplist_put (oprops, XCAR (tem),
17414 XCAR (XCDR (tem)));
17415 tem = XCDR (XCDR (tem));
17416 }
17417 props = oprops;
17418 }
17419
17420 aelt = Fassoc (elt, mode_line_proptrans_alist);
17421 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17422 {
17423 /* AELT is what we want. Move it to the front
17424 without consing. */
17425 elt = XCAR (aelt);
17426 mode_line_proptrans_alist
17427 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17428 }
17429 else
17430 {
17431 Lisp_Object tem;
17432
17433 /* If AELT has the wrong props, it is useless.
17434 so get rid of it. */
17435 if (! NILP (aelt))
17436 mode_line_proptrans_alist
17437 = Fdelq (aelt, mode_line_proptrans_alist);
17438
17439 elt = Fcopy_sequence (elt);
17440 Fset_text_properties (make_number (0), Flength (elt),
17441 props, elt);
17442 /* Add this item to mode_line_proptrans_alist. */
17443 mode_line_proptrans_alist
17444 = Fcons (Fcons (elt, props),
17445 mode_line_proptrans_alist);
17446 /* Truncate mode_line_proptrans_alist
17447 to at most 50 elements. */
17448 tem = Fnthcdr (make_number (50),
17449 mode_line_proptrans_alist);
17450 if (! NILP (tem))
17451 XSETCDR (tem, Qnil);
17452 }
17453 }
17454 }
17455
17456 offset = 0;
17457
17458 if (literal)
17459 {
17460 prec = precision - n;
17461 switch (mode_line_target)
17462 {
17463 case MODE_LINE_NOPROP:
17464 case MODE_LINE_TITLE:
17465 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17466 break;
17467 case MODE_LINE_STRING:
17468 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17469 break;
17470 case MODE_LINE_DISPLAY:
17471 n += display_string (NULL, elt, Qnil, 0, 0, it,
17472 0, prec, 0, STRING_MULTIBYTE (elt));
17473 break;
17474 }
17475
17476 break;
17477 }
17478
17479 /* Handle the non-literal case. */
17480
17481 while ((precision <= 0 || n < precision)
17482 && SREF (elt, offset) != 0
17483 && (mode_line_target != MODE_LINE_DISPLAY
17484 || it->current_x < it->last_visible_x))
17485 {
17486 int last_offset = offset;
17487
17488 /* Advance to end of string or next format specifier. */
17489 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17490 ;
17491
17492 if (offset - 1 != last_offset)
17493 {
17494 int nchars, nbytes;
17495
17496 /* Output to end of string or up to '%'. Field width
17497 is length of string. Don't output more than
17498 PRECISION allows us. */
17499 offset--;
17500
17501 prec = c_string_width (SDATA (elt) + last_offset,
17502 offset - last_offset, precision - n,
17503 &nchars, &nbytes);
17504
17505 switch (mode_line_target)
17506 {
17507 case MODE_LINE_NOPROP:
17508 case MODE_LINE_TITLE:
17509 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17510 break;
17511 case MODE_LINE_STRING:
17512 {
17513 int bytepos = last_offset;
17514 int charpos = string_byte_to_char (elt, bytepos);
17515 int endpos = (precision <= 0
17516 ? string_byte_to_char (elt, offset)
17517 : charpos + nchars);
17518
17519 n += store_mode_line_string (NULL,
17520 Fsubstring (elt, make_number (charpos),
17521 make_number (endpos)),
17522 0, 0, 0, Qnil);
17523 }
17524 break;
17525 case MODE_LINE_DISPLAY:
17526 {
17527 int bytepos = last_offset;
17528 int charpos = string_byte_to_char (elt, bytepos);
17529
17530 if (precision <= 0)
17531 nchars = string_byte_to_char (elt, offset) - charpos;
17532 n += display_string (NULL, elt, Qnil, 0, charpos,
17533 it, 0, nchars, 0,
17534 STRING_MULTIBYTE (elt));
17535 }
17536 break;
17537 }
17538 }
17539 else /* c == '%' */
17540 {
17541 int percent_position = offset;
17542
17543 /* Get the specified minimum width. Zero means
17544 don't pad. */
17545 field = 0;
17546 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17547 field = field * 10 + c - '0';
17548
17549 /* Don't pad beyond the total padding allowed. */
17550 if (field_width - n > 0 && field > field_width - n)
17551 field = field_width - n;
17552
17553 /* Note that either PRECISION <= 0 or N < PRECISION. */
17554 prec = precision - n;
17555
17556 if (c == 'M')
17557 n += display_mode_element (it, depth, field, prec,
17558 Vglobal_mode_string, props,
17559 risky);
17560 else if (c != 0)
17561 {
17562 int multibyte;
17563 int bytepos, charpos;
17564 unsigned char *spec;
17565
17566 bytepos = percent_position;
17567 charpos = (STRING_MULTIBYTE (elt)
17568 ? string_byte_to_char (elt, bytepos)
17569 : bytepos);
17570 spec
17571 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17572
17573 switch (mode_line_target)
17574 {
17575 case MODE_LINE_NOPROP:
17576 case MODE_LINE_TITLE:
17577 n += store_mode_line_noprop (spec, field, prec);
17578 break;
17579 case MODE_LINE_STRING:
17580 {
17581 int len = strlen (spec);
17582 Lisp_Object tem = make_string (spec, len);
17583 props = Ftext_properties_at (make_number (charpos), elt);
17584 /* Should only keep face property in props */
17585 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17586 }
17587 break;
17588 case MODE_LINE_DISPLAY:
17589 {
17590 int nglyphs_before, nwritten;
17591
17592 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17593 nwritten = display_string (spec, Qnil, elt,
17594 charpos, 0, it,
17595 field, prec, 0,
17596 multibyte);
17597
17598 /* Assign to the glyphs written above the
17599 string where the `%x' came from, position
17600 of the `%'. */
17601 if (nwritten > 0)
17602 {
17603 struct glyph *glyph
17604 = (it->glyph_row->glyphs[TEXT_AREA]
17605 + nglyphs_before);
17606 int i;
17607
17608 for (i = 0; i < nwritten; ++i)
17609 {
17610 glyph[i].object = elt;
17611 glyph[i].charpos = charpos;
17612 }
17613
17614 n += nwritten;
17615 }
17616 }
17617 break;
17618 }
17619 }
17620 else /* c == 0 */
17621 break;
17622 }
17623 }
17624 }
17625 break;
17626
17627 case Lisp_Symbol:
17628 /* A symbol: process the value of the symbol recursively
17629 as if it appeared here directly. Avoid error if symbol void.
17630 Special case: if value of symbol is a string, output the string
17631 literally. */
17632 {
17633 register Lisp_Object tem;
17634
17635 /* If the variable is not marked as risky to set
17636 then its contents are risky to use. */
17637 if (NILP (Fget (elt, Qrisky_local_variable)))
17638 risky = 1;
17639
17640 tem = Fboundp (elt);
17641 if (!NILP (tem))
17642 {
17643 tem = Fsymbol_value (elt);
17644 /* If value is a string, output that string literally:
17645 don't check for % within it. */
17646 if (STRINGP (tem))
17647 literal = 1;
17648
17649 if (!EQ (tem, elt))
17650 {
17651 /* Give up right away for nil or t. */
17652 elt = tem;
17653 goto tail_recurse;
17654 }
17655 }
17656 }
17657 break;
17658
17659 case Lisp_Cons:
17660 {
17661 register Lisp_Object car, tem;
17662
17663 /* A cons cell: five distinct cases.
17664 If first element is :eval or :propertize, do something special.
17665 If first element is a string or a cons, process all the elements
17666 and effectively concatenate them.
17667 If first element is a negative number, truncate displaying cdr to
17668 at most that many characters. If positive, pad (with spaces)
17669 to at least that many characters.
17670 If first element is a symbol, process the cadr or caddr recursively
17671 according to whether the symbol's value is non-nil or nil. */
17672 car = XCAR (elt);
17673 if (EQ (car, QCeval))
17674 {
17675 /* An element of the form (:eval FORM) means evaluate FORM
17676 and use the result as mode line elements. */
17677
17678 if (risky)
17679 break;
17680
17681 if (CONSP (XCDR (elt)))
17682 {
17683 Lisp_Object spec;
17684 spec = safe_eval (XCAR (XCDR (elt)));
17685 n += display_mode_element (it, depth, field_width - n,
17686 precision - n, spec, props,
17687 risky);
17688 }
17689 }
17690 else if (EQ (car, QCpropertize))
17691 {
17692 /* An element of the form (:propertize ELT PROPS...)
17693 means display ELT but applying properties PROPS. */
17694
17695 if (risky)
17696 break;
17697
17698 if (CONSP (XCDR (elt)))
17699 n += display_mode_element (it, depth, field_width - n,
17700 precision - n, XCAR (XCDR (elt)),
17701 XCDR (XCDR (elt)), risky);
17702 }
17703 else if (SYMBOLP (car))
17704 {
17705 tem = Fboundp (car);
17706 elt = XCDR (elt);
17707 if (!CONSP (elt))
17708 goto invalid;
17709 /* elt is now the cdr, and we know it is a cons cell.
17710 Use its car if CAR has a non-nil value. */
17711 if (!NILP (tem))
17712 {
17713 tem = Fsymbol_value (car);
17714 if (!NILP (tem))
17715 {
17716 elt = XCAR (elt);
17717 goto tail_recurse;
17718 }
17719 }
17720 /* Symbol's value is nil (or symbol is unbound)
17721 Get the cddr of the original list
17722 and if possible find the caddr and use that. */
17723 elt = XCDR (elt);
17724 if (NILP (elt))
17725 break;
17726 else if (!CONSP (elt))
17727 goto invalid;
17728 elt = XCAR (elt);
17729 goto tail_recurse;
17730 }
17731 else if (INTEGERP (car))
17732 {
17733 register int lim = XINT (car);
17734 elt = XCDR (elt);
17735 if (lim < 0)
17736 {
17737 /* Negative int means reduce maximum width. */
17738 if (precision <= 0)
17739 precision = -lim;
17740 else
17741 precision = min (precision, -lim);
17742 }
17743 else if (lim > 0)
17744 {
17745 /* Padding specified. Don't let it be more than
17746 current maximum. */
17747 if (precision > 0)
17748 lim = min (precision, lim);
17749
17750 /* If that's more padding than already wanted, queue it.
17751 But don't reduce padding already specified even if
17752 that is beyond the current truncation point. */
17753 field_width = max (lim, field_width);
17754 }
17755 goto tail_recurse;
17756 }
17757 else if (STRINGP (car) || CONSP (car))
17758 {
17759 register int limit = 50;
17760 /* Limit is to protect against circular lists. */
17761 while (CONSP (elt)
17762 && --limit > 0
17763 && (precision <= 0 || n < precision))
17764 {
17765 n += display_mode_element (it, depth,
17766 /* Do padding only after the last
17767 element in the list. */
17768 (! CONSP (XCDR (elt))
17769 ? field_width - n
17770 : 0),
17771 precision - n, XCAR (elt),
17772 props, risky);
17773 elt = XCDR (elt);
17774 }
17775 }
17776 }
17777 break;
17778
17779 default:
17780 invalid:
17781 elt = build_string ("*invalid*");
17782 goto tail_recurse;
17783 }
17784
17785 /* Pad to FIELD_WIDTH. */
17786 if (field_width > 0 && n < field_width)
17787 {
17788 switch (mode_line_target)
17789 {
17790 case MODE_LINE_NOPROP:
17791 case MODE_LINE_TITLE:
17792 n += store_mode_line_noprop ("", field_width - n, 0);
17793 break;
17794 case MODE_LINE_STRING:
17795 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17796 break;
17797 case MODE_LINE_DISPLAY:
17798 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17799 0, 0, 0);
17800 break;
17801 }
17802 }
17803
17804 return n;
17805 }
17806
17807 /* Store a mode-line string element in mode_line_string_list.
17808
17809 If STRING is non-null, display that C string. Otherwise, the Lisp
17810 string LISP_STRING is displayed.
17811
17812 FIELD_WIDTH is the minimum number of output glyphs to produce.
17813 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17814 with spaces. FIELD_WIDTH <= 0 means don't pad.
17815
17816 PRECISION is the maximum number of characters to output from
17817 STRING. PRECISION <= 0 means don't truncate the string.
17818
17819 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17820 properties to the string.
17821
17822 PROPS are the properties to add to the string.
17823 The mode_line_string_face face property is always added to the string.
17824 */
17825
17826 static int
17827 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17828 char *string;
17829 Lisp_Object lisp_string;
17830 int copy_string;
17831 int field_width;
17832 int precision;
17833 Lisp_Object props;
17834 {
17835 int len;
17836 int n = 0;
17837
17838 if (string != NULL)
17839 {
17840 len = strlen (string);
17841 if (precision > 0 && len > precision)
17842 len = precision;
17843 lisp_string = make_string (string, len);
17844 if (NILP (props))
17845 props = mode_line_string_face_prop;
17846 else if (!NILP (mode_line_string_face))
17847 {
17848 Lisp_Object face = Fplist_get (props, Qface);
17849 props = Fcopy_sequence (props);
17850 if (NILP (face))
17851 face = mode_line_string_face;
17852 else
17853 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17854 props = Fplist_put (props, Qface, face);
17855 }
17856 Fadd_text_properties (make_number (0), make_number (len),
17857 props, lisp_string);
17858 }
17859 else
17860 {
17861 len = XFASTINT (Flength (lisp_string));
17862 if (precision > 0 && len > precision)
17863 {
17864 len = precision;
17865 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17866 precision = -1;
17867 }
17868 if (!NILP (mode_line_string_face))
17869 {
17870 Lisp_Object face;
17871 if (NILP (props))
17872 props = Ftext_properties_at (make_number (0), lisp_string);
17873 face = Fplist_get (props, Qface);
17874 if (NILP (face))
17875 face = mode_line_string_face;
17876 else
17877 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17878 props = Fcons (Qface, Fcons (face, Qnil));
17879 if (copy_string)
17880 lisp_string = Fcopy_sequence (lisp_string);
17881 }
17882 if (!NILP (props))
17883 Fadd_text_properties (make_number (0), make_number (len),
17884 props, lisp_string);
17885 }
17886
17887 if (len > 0)
17888 {
17889 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17890 n += len;
17891 }
17892
17893 if (field_width > len)
17894 {
17895 field_width -= len;
17896 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17897 if (!NILP (props))
17898 Fadd_text_properties (make_number (0), make_number (field_width),
17899 props, lisp_string);
17900 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17901 n += field_width;
17902 }
17903
17904 return n;
17905 }
17906
17907
17908 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17909 1, 4, 0,
17910 doc: /* Format a string out of a mode line format specification.
17911 First arg FORMAT specifies the mode line format (see `mode-line-format'
17912 for details) to use.
17913
17914 Optional second arg FACE specifies the face property to put
17915 on all characters for which no face is specified.
17916 The value t means whatever face the window's mode line currently uses
17917 \(either `mode-line' or `mode-line-inactive', depending).
17918 A value of nil means the default is no face property.
17919 If FACE is an integer, the value string has no text properties.
17920
17921 Optional third and fourth args WINDOW and BUFFER specify the window
17922 and buffer to use as the context for the formatting (defaults
17923 are the selected window and the window's buffer). */)
17924 (format, face, window, buffer)
17925 Lisp_Object format, face, window, buffer;
17926 {
17927 struct it it;
17928 int len;
17929 struct window *w;
17930 struct buffer *old_buffer = NULL;
17931 int face_id = -1;
17932 int no_props = INTEGERP (face);
17933 int count = SPECPDL_INDEX ();
17934 Lisp_Object str;
17935 int string_start = 0;
17936
17937 if (NILP (window))
17938 window = selected_window;
17939 CHECK_WINDOW (window);
17940 w = XWINDOW (window);
17941
17942 if (NILP (buffer))
17943 buffer = w->buffer;
17944 CHECK_BUFFER (buffer);
17945
17946 /* Make formatting the modeline a non-op when noninteractive, otherwise
17947 there will be problems later caused by a partially initialized frame. */
17948 if (NILP (format) || noninteractive)
17949 return empty_unibyte_string;
17950
17951 if (no_props)
17952 face = Qnil;
17953
17954 if (!NILP (face))
17955 {
17956 if (EQ (face, Qt))
17957 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17958 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17959 }
17960
17961 if (face_id < 0)
17962 face_id = DEFAULT_FACE_ID;
17963
17964 if (XBUFFER (buffer) != current_buffer)
17965 old_buffer = current_buffer;
17966
17967 /* Save things including mode_line_proptrans_alist,
17968 and set that to nil so that we don't alter the outer value. */
17969 record_unwind_protect (unwind_format_mode_line,
17970 format_mode_line_unwind_data
17971 (old_buffer, selected_window, 1));
17972 mode_line_proptrans_alist = Qnil;
17973
17974 Fselect_window (window, Qt);
17975 if (old_buffer)
17976 set_buffer_internal_1 (XBUFFER (buffer));
17977
17978 init_iterator (&it, w, -1, -1, NULL, face_id);
17979
17980 if (no_props)
17981 {
17982 mode_line_target = MODE_LINE_NOPROP;
17983 mode_line_string_face_prop = Qnil;
17984 mode_line_string_list = Qnil;
17985 string_start = MODE_LINE_NOPROP_LEN (0);
17986 }
17987 else
17988 {
17989 mode_line_target = MODE_LINE_STRING;
17990 mode_line_string_list = Qnil;
17991 mode_line_string_face = face;
17992 mode_line_string_face_prop
17993 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17994 }
17995
17996 push_kboard (FRAME_KBOARD (it.f));
17997 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17998 pop_kboard ();
17999
18000 if (no_props)
18001 {
18002 len = MODE_LINE_NOPROP_LEN (string_start);
18003 str = make_string (mode_line_noprop_buf + string_start, len);
18004 }
18005 else
18006 {
18007 mode_line_string_list = Fnreverse (mode_line_string_list);
18008 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18009 empty_unibyte_string);
18010 }
18011
18012 unbind_to (count, Qnil);
18013 return str;
18014 }
18015
18016 /* Write a null-terminated, right justified decimal representation of
18017 the positive integer D to BUF using a minimal field width WIDTH. */
18018
18019 static void
18020 pint2str (buf, width, d)
18021 register char *buf;
18022 register int width;
18023 register int d;
18024 {
18025 register char *p = buf;
18026
18027 if (d <= 0)
18028 *p++ = '0';
18029 else
18030 {
18031 while (d > 0)
18032 {
18033 *p++ = d % 10 + '0';
18034 d /= 10;
18035 }
18036 }
18037
18038 for (width -= (int) (p - buf); width > 0; --width)
18039 *p++ = ' ';
18040 *p-- = '\0';
18041 while (p > buf)
18042 {
18043 d = *buf;
18044 *buf++ = *p;
18045 *p-- = d;
18046 }
18047 }
18048
18049 /* Write a null-terminated, right justified decimal and "human
18050 readable" representation of the nonnegative integer D to BUF using
18051 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18052
18053 static const char power_letter[] =
18054 {
18055 0, /* not used */
18056 'k', /* kilo */
18057 'M', /* mega */
18058 'G', /* giga */
18059 'T', /* tera */
18060 'P', /* peta */
18061 'E', /* exa */
18062 'Z', /* zetta */
18063 'Y' /* yotta */
18064 };
18065
18066 static void
18067 pint2hrstr (buf, width, d)
18068 char *buf;
18069 int width;
18070 int d;
18071 {
18072 /* We aim to represent the nonnegative integer D as
18073 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18074 int quotient = d;
18075 int remainder = 0;
18076 /* -1 means: do not use TENTHS. */
18077 int tenths = -1;
18078 int exponent = 0;
18079
18080 /* Length of QUOTIENT.TENTHS as a string. */
18081 int length;
18082
18083 char * psuffix;
18084 char * p;
18085
18086 if (1000 <= quotient)
18087 {
18088 /* Scale to the appropriate EXPONENT. */
18089 do
18090 {
18091 remainder = quotient % 1000;
18092 quotient /= 1000;
18093 exponent++;
18094 }
18095 while (1000 <= quotient);
18096
18097 /* Round to nearest and decide whether to use TENTHS or not. */
18098 if (quotient <= 9)
18099 {
18100 tenths = remainder / 100;
18101 if (50 <= remainder % 100)
18102 {
18103 if (tenths < 9)
18104 tenths++;
18105 else
18106 {
18107 quotient++;
18108 if (quotient == 10)
18109 tenths = -1;
18110 else
18111 tenths = 0;
18112 }
18113 }
18114 }
18115 else
18116 if (500 <= remainder)
18117 {
18118 if (quotient < 999)
18119 quotient++;
18120 else
18121 {
18122 quotient = 1;
18123 exponent++;
18124 tenths = 0;
18125 }
18126 }
18127 }
18128
18129 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18130 if (tenths == -1 && quotient <= 99)
18131 if (quotient <= 9)
18132 length = 1;
18133 else
18134 length = 2;
18135 else
18136 length = 3;
18137 p = psuffix = buf + max (width, length);
18138
18139 /* Print EXPONENT. */
18140 if (exponent)
18141 *psuffix++ = power_letter[exponent];
18142 *psuffix = '\0';
18143
18144 /* Print TENTHS. */
18145 if (tenths >= 0)
18146 {
18147 *--p = '0' + tenths;
18148 *--p = '.';
18149 }
18150
18151 /* Print QUOTIENT. */
18152 do
18153 {
18154 int digit = quotient % 10;
18155 *--p = '0' + digit;
18156 }
18157 while ((quotient /= 10) != 0);
18158
18159 /* Print leading spaces. */
18160 while (buf < p)
18161 *--p = ' ';
18162 }
18163
18164 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18165 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18166 type of CODING_SYSTEM. Return updated pointer into BUF. */
18167
18168 static unsigned char invalid_eol_type[] = "(*invalid*)";
18169
18170 static char *
18171 decode_mode_spec_coding (coding_system, buf, eol_flag)
18172 Lisp_Object coding_system;
18173 register char *buf;
18174 int eol_flag;
18175 {
18176 Lisp_Object val;
18177 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18178 const unsigned char *eol_str;
18179 int eol_str_len;
18180 /* The EOL conversion we are using. */
18181 Lisp_Object eoltype;
18182
18183 val = CODING_SYSTEM_SPEC (coding_system);
18184 eoltype = Qnil;
18185
18186 if (!VECTORP (val)) /* Not yet decided. */
18187 {
18188 if (multibyte)
18189 *buf++ = '-';
18190 if (eol_flag)
18191 eoltype = eol_mnemonic_undecided;
18192 /* Don't mention EOL conversion if it isn't decided. */
18193 }
18194 else
18195 {
18196 Lisp_Object attrs;
18197 Lisp_Object eolvalue;
18198
18199 attrs = AREF (val, 0);
18200 eolvalue = AREF (val, 2);
18201
18202 if (multibyte)
18203 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18204
18205 if (eol_flag)
18206 {
18207 /* The EOL conversion that is normal on this system. */
18208
18209 if (NILP (eolvalue)) /* Not yet decided. */
18210 eoltype = eol_mnemonic_undecided;
18211 else if (VECTORP (eolvalue)) /* Not yet decided. */
18212 eoltype = eol_mnemonic_undecided;
18213 else /* eolvalue is Qunix, Qdos, or Qmac. */
18214 eoltype = (EQ (eolvalue, Qunix)
18215 ? eol_mnemonic_unix
18216 : (EQ (eolvalue, Qdos) == 1
18217 ? eol_mnemonic_dos : eol_mnemonic_mac));
18218 }
18219 }
18220
18221 if (eol_flag)
18222 {
18223 /* Mention the EOL conversion if it is not the usual one. */
18224 if (STRINGP (eoltype))
18225 {
18226 eol_str = SDATA (eoltype);
18227 eol_str_len = SBYTES (eoltype);
18228 }
18229 else if (CHARACTERP (eoltype))
18230 {
18231 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18232 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18233 eol_str = tmp;
18234 }
18235 else
18236 {
18237 eol_str = invalid_eol_type;
18238 eol_str_len = sizeof (invalid_eol_type) - 1;
18239 }
18240 bcopy (eol_str, buf, eol_str_len);
18241 buf += eol_str_len;
18242 }
18243
18244 return buf;
18245 }
18246
18247 /* Return a string for the output of a mode line %-spec for window W,
18248 generated by character C. PRECISION >= 0 means don't return a
18249 string longer than that value. FIELD_WIDTH > 0 means pad the
18250 string returned with spaces to that value. Return 1 in *MULTIBYTE
18251 if the result is multibyte text.
18252
18253 Note we operate on the current buffer for most purposes,
18254 the exception being w->base_line_pos. */
18255
18256 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18257
18258 static char *
18259 decode_mode_spec (w, c, field_width, precision, multibyte)
18260 struct window *w;
18261 register int c;
18262 int field_width, precision;
18263 int *multibyte;
18264 {
18265 Lisp_Object obj;
18266 struct frame *f = XFRAME (WINDOW_FRAME (w));
18267 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18268 struct buffer *b = current_buffer;
18269
18270 obj = Qnil;
18271 *multibyte = 0;
18272
18273 switch (c)
18274 {
18275 case '*':
18276 if (!NILP (b->read_only))
18277 return "%";
18278 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18279 return "*";
18280 return "-";
18281
18282 case '+':
18283 /* This differs from %* only for a modified read-only buffer. */
18284 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18285 return "*";
18286 if (!NILP (b->read_only))
18287 return "%";
18288 return "-";
18289
18290 case '&':
18291 /* This differs from %* in ignoring read-only-ness. */
18292 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18293 return "*";
18294 return "-";
18295
18296 case '%':
18297 return "%";
18298
18299 case '[':
18300 {
18301 int i;
18302 char *p;
18303
18304 if (command_loop_level > 5)
18305 return "[[[... ";
18306 p = decode_mode_spec_buf;
18307 for (i = 0; i < command_loop_level; i++)
18308 *p++ = '[';
18309 *p = 0;
18310 return decode_mode_spec_buf;
18311 }
18312
18313 case ']':
18314 {
18315 int i;
18316 char *p;
18317
18318 if (command_loop_level > 5)
18319 return " ...]]]";
18320 p = decode_mode_spec_buf;
18321 for (i = 0; i < command_loop_level; i++)
18322 *p++ = ']';
18323 *p = 0;
18324 return decode_mode_spec_buf;
18325 }
18326
18327 case '-':
18328 {
18329 register int i;
18330
18331 /* Let lots_of_dashes be a string of infinite length. */
18332 if (mode_line_target == MODE_LINE_NOPROP ||
18333 mode_line_target == MODE_LINE_STRING)
18334 return "--";
18335 if (field_width <= 0
18336 || field_width > sizeof (lots_of_dashes))
18337 {
18338 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18339 decode_mode_spec_buf[i] = '-';
18340 decode_mode_spec_buf[i] = '\0';
18341 return decode_mode_spec_buf;
18342 }
18343 else
18344 return lots_of_dashes;
18345 }
18346
18347 case 'b':
18348 obj = b->name;
18349 break;
18350
18351 case 'c':
18352 /* %c and %l are ignored in `frame-title-format'.
18353 (In redisplay_internal, the frame title is drawn _before_ the
18354 windows are updated, so the stuff which depends on actual
18355 window contents (such as %l) may fail to render properly, or
18356 even crash emacs.) */
18357 if (mode_line_target == MODE_LINE_TITLE)
18358 return "";
18359 else
18360 {
18361 int col = (int) current_column (); /* iftc */
18362 w->column_number_displayed = make_number (col);
18363 pint2str (decode_mode_spec_buf, field_width, col);
18364 return decode_mode_spec_buf;
18365 }
18366
18367 case 'e':
18368 #ifndef SYSTEM_MALLOC
18369 {
18370 if (NILP (Vmemory_full))
18371 return "";
18372 else
18373 return "!MEM FULL! ";
18374 }
18375 #else
18376 return "";
18377 #endif
18378
18379 case 'F':
18380 /* %F displays the frame name. */
18381 if (!NILP (f->title))
18382 return (char *) SDATA (f->title);
18383 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18384 return (char *) SDATA (f->name);
18385 return "Emacs";
18386
18387 case 'f':
18388 obj = b->filename;
18389 break;
18390
18391 case 'i':
18392 {
18393 int size = ZV - BEGV;
18394 pint2str (decode_mode_spec_buf, field_width, size);
18395 return decode_mode_spec_buf;
18396 }
18397
18398 case 'I':
18399 {
18400 int size = ZV - BEGV;
18401 pint2hrstr (decode_mode_spec_buf, field_width, size);
18402 return decode_mode_spec_buf;
18403 }
18404
18405 case 'l':
18406 {
18407 int startpos, startpos_byte, line, linepos, linepos_byte;
18408 int topline, nlines, junk, height;
18409
18410 /* %c and %l are ignored in `frame-title-format'. */
18411 if (mode_line_target == MODE_LINE_TITLE)
18412 return "";
18413
18414 startpos = XMARKER (w->start)->charpos;
18415 startpos_byte = marker_byte_position (w->start);
18416 height = WINDOW_TOTAL_LINES (w);
18417
18418 /* If we decided that this buffer isn't suitable for line numbers,
18419 don't forget that too fast. */
18420 if (EQ (w->base_line_pos, w->buffer))
18421 goto no_value;
18422 /* But do forget it, if the window shows a different buffer now. */
18423 else if (BUFFERP (w->base_line_pos))
18424 w->base_line_pos = Qnil;
18425
18426 /* If the buffer is very big, don't waste time. */
18427 if (INTEGERP (Vline_number_display_limit)
18428 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18429 {
18430 w->base_line_pos = Qnil;
18431 w->base_line_number = Qnil;
18432 goto no_value;
18433 }
18434
18435 if (INTEGERP (w->base_line_number)
18436 && INTEGERP (w->base_line_pos)
18437 && XFASTINT (w->base_line_pos) <= startpos)
18438 {
18439 line = XFASTINT (w->base_line_number);
18440 linepos = XFASTINT (w->base_line_pos);
18441 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18442 }
18443 else
18444 {
18445 line = 1;
18446 linepos = BUF_BEGV (b);
18447 linepos_byte = BUF_BEGV_BYTE (b);
18448 }
18449
18450 /* Count lines from base line to window start position. */
18451 nlines = display_count_lines (linepos, linepos_byte,
18452 startpos_byte,
18453 startpos, &junk);
18454
18455 topline = nlines + line;
18456
18457 /* Determine a new base line, if the old one is too close
18458 or too far away, or if we did not have one.
18459 "Too close" means it's plausible a scroll-down would
18460 go back past it. */
18461 if (startpos == BUF_BEGV (b))
18462 {
18463 w->base_line_number = make_number (topline);
18464 w->base_line_pos = make_number (BUF_BEGV (b));
18465 }
18466 else if (nlines < height + 25 || nlines > height * 3 + 50
18467 || linepos == BUF_BEGV (b))
18468 {
18469 int limit = BUF_BEGV (b);
18470 int limit_byte = BUF_BEGV_BYTE (b);
18471 int position;
18472 int distance = (height * 2 + 30) * line_number_display_limit_width;
18473
18474 if (startpos - distance > limit)
18475 {
18476 limit = startpos - distance;
18477 limit_byte = CHAR_TO_BYTE (limit);
18478 }
18479
18480 nlines = display_count_lines (startpos, startpos_byte,
18481 limit_byte,
18482 - (height * 2 + 30),
18483 &position);
18484 /* If we couldn't find the lines we wanted within
18485 line_number_display_limit_width chars per line,
18486 give up on line numbers for this window. */
18487 if (position == limit_byte && limit == startpos - distance)
18488 {
18489 w->base_line_pos = w->buffer;
18490 w->base_line_number = Qnil;
18491 goto no_value;
18492 }
18493
18494 w->base_line_number = make_number (topline - nlines);
18495 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18496 }
18497
18498 /* Now count lines from the start pos to point. */
18499 nlines = display_count_lines (startpos, startpos_byte,
18500 PT_BYTE, PT, &junk);
18501
18502 /* Record that we did display the line number. */
18503 line_number_displayed = 1;
18504
18505 /* Make the string to show. */
18506 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18507 return decode_mode_spec_buf;
18508 no_value:
18509 {
18510 char* p = decode_mode_spec_buf;
18511 int pad = field_width - 2;
18512 while (pad-- > 0)
18513 *p++ = ' ';
18514 *p++ = '?';
18515 *p++ = '?';
18516 *p = '\0';
18517 return decode_mode_spec_buf;
18518 }
18519 }
18520 break;
18521
18522 case 'm':
18523 obj = b->mode_name;
18524 break;
18525
18526 case 'n':
18527 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18528 return " Narrow";
18529 break;
18530
18531 case 'p':
18532 {
18533 int pos = marker_position (w->start);
18534 int total = BUF_ZV (b) - BUF_BEGV (b);
18535
18536 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18537 {
18538 if (pos <= BUF_BEGV (b))
18539 return "All";
18540 else
18541 return "Bottom";
18542 }
18543 else if (pos <= BUF_BEGV (b))
18544 return "Top";
18545 else
18546 {
18547 if (total > 1000000)
18548 /* Do it differently for a large value, to avoid overflow. */
18549 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18550 else
18551 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18552 /* We can't normally display a 3-digit number,
18553 so get us a 2-digit number that is close. */
18554 if (total == 100)
18555 total = 99;
18556 sprintf (decode_mode_spec_buf, "%2d%%", total);
18557 return decode_mode_spec_buf;
18558 }
18559 }
18560
18561 /* Display percentage of size above the bottom of the screen. */
18562 case 'P':
18563 {
18564 int toppos = marker_position (w->start);
18565 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18566 int total = BUF_ZV (b) - BUF_BEGV (b);
18567
18568 if (botpos >= BUF_ZV (b))
18569 {
18570 if (toppos <= BUF_BEGV (b))
18571 return "All";
18572 else
18573 return "Bottom";
18574 }
18575 else
18576 {
18577 if (total > 1000000)
18578 /* Do it differently for a large value, to avoid overflow. */
18579 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18580 else
18581 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18582 /* We can't normally display a 3-digit number,
18583 so get us a 2-digit number that is close. */
18584 if (total == 100)
18585 total = 99;
18586 if (toppos <= BUF_BEGV (b))
18587 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18588 else
18589 sprintf (decode_mode_spec_buf, "%2d%%", total);
18590 return decode_mode_spec_buf;
18591 }
18592 }
18593
18594 case 's':
18595 /* status of process */
18596 obj = Fget_buffer_process (Fcurrent_buffer ());
18597 if (NILP (obj))
18598 return "no process";
18599 #ifdef subprocesses
18600 obj = Fsymbol_name (Fprocess_status (obj));
18601 #endif
18602 break;
18603
18604 case '@':
18605 {
18606 Lisp_Object val;
18607 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18608 if (NILP (val))
18609 return "-";
18610 else
18611 return "@";
18612 }
18613
18614 case 't': /* indicate TEXT or BINARY */
18615 #ifdef MODE_LINE_BINARY_TEXT
18616 return MODE_LINE_BINARY_TEXT (b);
18617 #else
18618 return "T";
18619 #endif
18620
18621 case 'z':
18622 /* coding-system (not including end-of-line format) */
18623 case 'Z':
18624 /* coding-system (including end-of-line type) */
18625 {
18626 int eol_flag = (c == 'Z');
18627 char *p = decode_mode_spec_buf;
18628
18629 if (! FRAME_WINDOW_P (f))
18630 {
18631 /* No need to mention EOL here--the terminal never needs
18632 to do EOL conversion. */
18633 p = decode_mode_spec_coding (CODING_ID_NAME
18634 (FRAME_KEYBOARD_CODING (f)->id),
18635 p, 0);
18636 p = decode_mode_spec_coding (CODING_ID_NAME
18637 (FRAME_TERMINAL_CODING (f)->id),
18638 p, 0);
18639 }
18640 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18641 p, eol_flag);
18642
18643 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18644 #ifdef subprocesses
18645 obj = Fget_buffer_process (Fcurrent_buffer ());
18646 if (PROCESSP (obj))
18647 {
18648 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18649 p, eol_flag);
18650 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18651 p, eol_flag);
18652 }
18653 #endif /* subprocesses */
18654 #endif /* 0 */
18655 *p = 0;
18656 return decode_mode_spec_buf;
18657 }
18658 }
18659
18660 if (STRINGP (obj))
18661 {
18662 *multibyte = STRING_MULTIBYTE (obj);
18663 return (char *) SDATA (obj);
18664 }
18665 else
18666 return "";
18667 }
18668
18669
18670 /* Count up to COUNT lines starting from START / START_BYTE.
18671 But don't go beyond LIMIT_BYTE.
18672 Return the number of lines thus found (always nonnegative).
18673
18674 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18675
18676 static int
18677 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18678 int start, start_byte, limit_byte, count;
18679 int *byte_pos_ptr;
18680 {
18681 register unsigned char *cursor;
18682 unsigned char *base;
18683
18684 register int ceiling;
18685 register unsigned char *ceiling_addr;
18686 int orig_count = count;
18687
18688 /* If we are not in selective display mode,
18689 check only for newlines. */
18690 int selective_display = (!NILP (current_buffer->selective_display)
18691 && !INTEGERP (current_buffer->selective_display));
18692
18693 if (count > 0)
18694 {
18695 while (start_byte < limit_byte)
18696 {
18697 ceiling = BUFFER_CEILING_OF (start_byte);
18698 ceiling = min (limit_byte - 1, ceiling);
18699 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18700 base = (cursor = BYTE_POS_ADDR (start_byte));
18701 while (1)
18702 {
18703 if (selective_display)
18704 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18705 ;
18706 else
18707 while (*cursor != '\n' && ++cursor != ceiling_addr)
18708 ;
18709
18710 if (cursor != ceiling_addr)
18711 {
18712 if (--count == 0)
18713 {
18714 start_byte += cursor - base + 1;
18715 *byte_pos_ptr = start_byte;
18716 return orig_count;
18717 }
18718 else
18719 if (++cursor == ceiling_addr)
18720 break;
18721 }
18722 else
18723 break;
18724 }
18725 start_byte += cursor - base;
18726 }
18727 }
18728 else
18729 {
18730 while (start_byte > limit_byte)
18731 {
18732 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18733 ceiling = max (limit_byte, ceiling);
18734 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18735 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18736 while (1)
18737 {
18738 if (selective_display)
18739 while (--cursor != ceiling_addr
18740 && *cursor != '\n' && *cursor != 015)
18741 ;
18742 else
18743 while (--cursor != ceiling_addr && *cursor != '\n')
18744 ;
18745
18746 if (cursor != ceiling_addr)
18747 {
18748 if (++count == 0)
18749 {
18750 start_byte += cursor - base + 1;
18751 *byte_pos_ptr = start_byte;
18752 /* When scanning backwards, we should
18753 not count the newline posterior to which we stop. */
18754 return - orig_count - 1;
18755 }
18756 }
18757 else
18758 break;
18759 }
18760 /* Here we add 1 to compensate for the last decrement
18761 of CURSOR, which took it past the valid range. */
18762 start_byte += cursor - base + 1;
18763 }
18764 }
18765
18766 *byte_pos_ptr = limit_byte;
18767
18768 if (count < 0)
18769 return - orig_count + count;
18770 return orig_count - count;
18771
18772 }
18773
18774
18775 \f
18776 /***********************************************************************
18777 Displaying strings
18778 ***********************************************************************/
18779
18780 /* Display a NUL-terminated string, starting with index START.
18781
18782 If STRING is non-null, display that C string. Otherwise, the Lisp
18783 string LISP_STRING is displayed.
18784
18785 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18786 FACE_STRING. Display STRING or LISP_STRING with the face at
18787 FACE_STRING_POS in FACE_STRING:
18788
18789 Display the string in the environment given by IT, but use the
18790 standard display table, temporarily.
18791
18792 FIELD_WIDTH is the minimum number of output glyphs to produce.
18793 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18794 with spaces. If STRING has more characters, more than FIELD_WIDTH
18795 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18796
18797 PRECISION is the maximum number of characters to output from
18798 STRING. PRECISION < 0 means don't truncate the string.
18799
18800 This is roughly equivalent to printf format specifiers:
18801
18802 FIELD_WIDTH PRECISION PRINTF
18803 ----------------------------------------
18804 -1 -1 %s
18805 -1 10 %.10s
18806 10 -1 %10s
18807 20 10 %20.10s
18808
18809 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18810 display them, and < 0 means obey the current buffer's value of
18811 enable_multibyte_characters.
18812
18813 Value is the number of columns displayed. */
18814
18815 static int
18816 display_string (string, lisp_string, face_string, face_string_pos,
18817 start, it, field_width, precision, max_x, multibyte)
18818 unsigned char *string;
18819 Lisp_Object lisp_string;
18820 Lisp_Object face_string;
18821 EMACS_INT face_string_pos;
18822 EMACS_INT start;
18823 struct it *it;
18824 int field_width, precision, max_x;
18825 int multibyte;
18826 {
18827 int hpos_at_start = it->hpos;
18828 int saved_face_id = it->face_id;
18829 struct glyph_row *row = it->glyph_row;
18830
18831 /* Initialize the iterator IT for iteration over STRING beginning
18832 with index START. */
18833 reseat_to_string (it, string, lisp_string, start,
18834 precision, field_width, multibyte);
18835
18836 /* If displaying STRING, set up the face of the iterator
18837 from LISP_STRING, if that's given. */
18838 if (STRINGP (face_string))
18839 {
18840 EMACS_INT endptr;
18841 struct face *face;
18842
18843 it->face_id
18844 = face_at_string_position (it->w, face_string, face_string_pos,
18845 0, it->region_beg_charpos,
18846 it->region_end_charpos,
18847 &endptr, it->base_face_id, 0);
18848 face = FACE_FROM_ID (it->f, it->face_id);
18849 it->face_box_p = face->box != FACE_NO_BOX;
18850 }
18851
18852 /* Set max_x to the maximum allowed X position. Don't let it go
18853 beyond the right edge of the window. */
18854 if (max_x <= 0)
18855 max_x = it->last_visible_x;
18856 else
18857 max_x = min (max_x, it->last_visible_x);
18858
18859 /* Skip over display elements that are not visible. because IT->w is
18860 hscrolled. */
18861 if (it->current_x < it->first_visible_x)
18862 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18863 MOVE_TO_POS | MOVE_TO_X);
18864
18865 row->ascent = it->max_ascent;
18866 row->height = it->max_ascent + it->max_descent;
18867 row->phys_ascent = it->max_phys_ascent;
18868 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18869 row->extra_line_spacing = it->max_extra_line_spacing;
18870
18871 /* This condition is for the case that we are called with current_x
18872 past last_visible_x. */
18873 while (it->current_x < max_x)
18874 {
18875 int x_before, x, n_glyphs_before, i, nglyphs;
18876
18877 /* Get the next display element. */
18878 if (!get_next_display_element (it))
18879 break;
18880
18881 /* Produce glyphs. */
18882 x_before = it->current_x;
18883 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18884 PRODUCE_GLYPHS (it);
18885
18886 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18887 i = 0;
18888 x = x_before;
18889 while (i < nglyphs)
18890 {
18891 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18892
18893 if (it->line_wrap != TRUNCATE
18894 && x + glyph->pixel_width > max_x)
18895 {
18896 /* End of continued line or max_x reached. */
18897 if (CHAR_GLYPH_PADDING_P (*glyph))
18898 {
18899 /* A wide character is unbreakable. */
18900 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18901 it->current_x = x_before;
18902 }
18903 else
18904 {
18905 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18906 it->current_x = x;
18907 }
18908 break;
18909 }
18910 else if (x + glyph->pixel_width >= it->first_visible_x)
18911 {
18912 /* Glyph is at least partially visible. */
18913 ++it->hpos;
18914 if (x < it->first_visible_x)
18915 it->glyph_row->x = x - it->first_visible_x;
18916 }
18917 else
18918 {
18919 /* Glyph is off the left margin of the display area.
18920 Should not happen. */
18921 abort ();
18922 }
18923
18924 row->ascent = max (row->ascent, it->max_ascent);
18925 row->height = max (row->height, it->max_ascent + it->max_descent);
18926 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18927 row->phys_height = max (row->phys_height,
18928 it->max_phys_ascent + it->max_phys_descent);
18929 row->extra_line_spacing = max (row->extra_line_spacing,
18930 it->max_extra_line_spacing);
18931 x += glyph->pixel_width;
18932 ++i;
18933 }
18934
18935 /* Stop if max_x reached. */
18936 if (i < nglyphs)
18937 break;
18938
18939 /* Stop at line ends. */
18940 if (ITERATOR_AT_END_OF_LINE_P (it))
18941 {
18942 it->continuation_lines_width = 0;
18943 break;
18944 }
18945
18946 set_iterator_to_next (it, 1);
18947
18948 /* Stop if truncating at the right edge. */
18949 if (it->line_wrap == TRUNCATE
18950 && it->current_x >= it->last_visible_x)
18951 {
18952 /* Add truncation mark, but don't do it if the line is
18953 truncated at a padding space. */
18954 if (IT_CHARPOS (*it) < it->string_nchars)
18955 {
18956 if (!FRAME_WINDOW_P (it->f))
18957 {
18958 int i, n;
18959
18960 if (it->current_x > it->last_visible_x)
18961 {
18962 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18963 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18964 break;
18965 for (n = row->used[TEXT_AREA]; i < n; ++i)
18966 {
18967 row->used[TEXT_AREA] = i;
18968 produce_special_glyphs (it, IT_TRUNCATION);
18969 }
18970 }
18971 produce_special_glyphs (it, IT_TRUNCATION);
18972 }
18973 it->glyph_row->truncated_on_right_p = 1;
18974 }
18975 break;
18976 }
18977 }
18978
18979 /* Maybe insert a truncation at the left. */
18980 if (it->first_visible_x
18981 && IT_CHARPOS (*it) > 0)
18982 {
18983 if (!FRAME_WINDOW_P (it->f))
18984 insert_left_trunc_glyphs (it);
18985 it->glyph_row->truncated_on_left_p = 1;
18986 }
18987
18988 it->face_id = saved_face_id;
18989
18990 /* Value is number of columns displayed. */
18991 return it->hpos - hpos_at_start;
18992 }
18993
18994
18995 \f
18996 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18997 appears as an element of LIST or as the car of an element of LIST.
18998 If PROPVAL is a list, compare each element against LIST in that
18999 way, and return 1/2 if any element of PROPVAL is found in LIST.
19000 Otherwise return 0. This function cannot quit.
19001 The return value is 2 if the text is invisible but with an ellipsis
19002 and 1 if it's invisible and without an ellipsis. */
19003
19004 int
19005 invisible_p (propval, list)
19006 register Lisp_Object propval;
19007 Lisp_Object list;
19008 {
19009 register Lisp_Object tail, proptail;
19010
19011 for (tail = list; CONSP (tail); tail = XCDR (tail))
19012 {
19013 register Lisp_Object tem;
19014 tem = XCAR (tail);
19015 if (EQ (propval, tem))
19016 return 1;
19017 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19018 return NILP (XCDR (tem)) ? 1 : 2;
19019 }
19020
19021 if (CONSP (propval))
19022 {
19023 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19024 {
19025 Lisp_Object propelt;
19026 propelt = XCAR (proptail);
19027 for (tail = list; CONSP (tail); tail = XCDR (tail))
19028 {
19029 register Lisp_Object tem;
19030 tem = XCAR (tail);
19031 if (EQ (propelt, tem))
19032 return 1;
19033 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19034 return NILP (XCDR (tem)) ? 1 : 2;
19035 }
19036 }
19037 }
19038
19039 return 0;
19040 }
19041
19042 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19043 doc: /* Non-nil if the property makes the text invisible.
19044 POS-OR-PROP can be a marker or number, in which case it is taken to be
19045 a position in the current buffer and the value of the `invisible' property
19046 is checked; or it can be some other value, which is then presumed to be the
19047 value of the `invisible' property of the text of interest.
19048 The non-nil value returned can be t for truly invisible text or something
19049 else if the text is replaced by an ellipsis. */)
19050 (pos_or_prop)
19051 Lisp_Object pos_or_prop;
19052 {
19053 Lisp_Object prop
19054 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19055 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19056 : pos_or_prop);
19057 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19058 return (invis == 0 ? Qnil
19059 : invis == 1 ? Qt
19060 : make_number (invis));
19061 }
19062
19063 /* Calculate a width or height in pixels from a specification using
19064 the following elements:
19065
19066 SPEC ::=
19067 NUM - a (fractional) multiple of the default font width/height
19068 (NUM) - specifies exactly NUM pixels
19069 UNIT - a fixed number of pixels, see below.
19070 ELEMENT - size of a display element in pixels, see below.
19071 (NUM . SPEC) - equals NUM * SPEC
19072 (+ SPEC SPEC ...) - add pixel values
19073 (- SPEC SPEC ...) - subtract pixel values
19074 (- SPEC) - negate pixel value
19075
19076 NUM ::=
19077 INT or FLOAT - a number constant
19078 SYMBOL - use symbol's (buffer local) variable binding.
19079
19080 UNIT ::=
19081 in - pixels per inch *)
19082 mm - pixels per 1/1000 meter *)
19083 cm - pixels per 1/100 meter *)
19084 width - width of current font in pixels.
19085 height - height of current font in pixels.
19086
19087 *) using the ratio(s) defined in display-pixels-per-inch.
19088
19089 ELEMENT ::=
19090
19091 left-fringe - left fringe width in pixels
19092 right-fringe - right fringe width in pixels
19093
19094 left-margin - left margin width in pixels
19095 right-margin - right margin width in pixels
19096
19097 scroll-bar - scroll-bar area width in pixels
19098
19099 Examples:
19100
19101 Pixels corresponding to 5 inches:
19102 (5 . in)
19103
19104 Total width of non-text areas on left side of window (if scroll-bar is on left):
19105 '(space :width (+ left-fringe left-margin scroll-bar))
19106
19107 Align to first text column (in header line):
19108 '(space :align-to 0)
19109
19110 Align to middle of text area minus half the width of variable `my-image'
19111 containing a loaded image:
19112 '(space :align-to (0.5 . (- text my-image)))
19113
19114 Width of left margin minus width of 1 character in the default font:
19115 '(space :width (- left-margin 1))
19116
19117 Width of left margin minus width of 2 characters in the current font:
19118 '(space :width (- left-margin (2 . width)))
19119
19120 Center 1 character over left-margin (in header line):
19121 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19122
19123 Different ways to express width of left fringe plus left margin minus one pixel:
19124 '(space :width (- (+ left-fringe left-margin) (1)))
19125 '(space :width (+ left-fringe left-margin (- (1))))
19126 '(space :width (+ left-fringe left-margin (-1)))
19127
19128 */
19129
19130 #define NUMVAL(X) \
19131 ((INTEGERP (X) || FLOATP (X)) \
19132 ? XFLOATINT (X) \
19133 : - 1)
19134
19135 int
19136 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19137 double *res;
19138 struct it *it;
19139 Lisp_Object prop;
19140 struct font *font;
19141 int width_p, *align_to;
19142 {
19143 double pixels;
19144
19145 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19146 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19147
19148 if (NILP (prop))
19149 return OK_PIXELS (0);
19150
19151 xassert (FRAME_LIVE_P (it->f));
19152
19153 if (SYMBOLP (prop))
19154 {
19155 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19156 {
19157 char *unit = SDATA (SYMBOL_NAME (prop));
19158
19159 if (unit[0] == 'i' && unit[1] == 'n')
19160 pixels = 1.0;
19161 else if (unit[0] == 'm' && unit[1] == 'm')
19162 pixels = 25.4;
19163 else if (unit[0] == 'c' && unit[1] == 'm')
19164 pixels = 2.54;
19165 else
19166 pixels = 0;
19167 if (pixels > 0)
19168 {
19169 double ppi;
19170 #ifdef HAVE_WINDOW_SYSTEM
19171 if (FRAME_WINDOW_P (it->f)
19172 && (ppi = (width_p
19173 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19174 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19175 ppi > 0))
19176 return OK_PIXELS (ppi / pixels);
19177 #endif
19178
19179 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19180 || (CONSP (Vdisplay_pixels_per_inch)
19181 && (ppi = (width_p
19182 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19183 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19184 ppi > 0)))
19185 return OK_PIXELS (ppi / pixels);
19186
19187 return 0;
19188 }
19189 }
19190
19191 #ifdef HAVE_WINDOW_SYSTEM
19192 if (EQ (prop, Qheight))
19193 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19194 if (EQ (prop, Qwidth))
19195 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19196 #else
19197 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19198 return OK_PIXELS (1);
19199 #endif
19200
19201 if (EQ (prop, Qtext))
19202 return OK_PIXELS (width_p
19203 ? window_box_width (it->w, TEXT_AREA)
19204 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19205
19206 if (align_to && *align_to < 0)
19207 {
19208 *res = 0;
19209 if (EQ (prop, Qleft))
19210 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19211 if (EQ (prop, Qright))
19212 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19213 if (EQ (prop, Qcenter))
19214 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19215 + window_box_width (it->w, TEXT_AREA) / 2);
19216 if (EQ (prop, Qleft_fringe))
19217 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19218 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19219 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19220 if (EQ (prop, Qright_fringe))
19221 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19222 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19223 : window_box_right_offset (it->w, TEXT_AREA));
19224 if (EQ (prop, Qleft_margin))
19225 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19226 if (EQ (prop, Qright_margin))
19227 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19228 if (EQ (prop, Qscroll_bar))
19229 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19230 ? 0
19231 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19232 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19233 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19234 : 0)));
19235 }
19236 else
19237 {
19238 if (EQ (prop, Qleft_fringe))
19239 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19240 if (EQ (prop, Qright_fringe))
19241 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19242 if (EQ (prop, Qleft_margin))
19243 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19244 if (EQ (prop, Qright_margin))
19245 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19246 if (EQ (prop, Qscroll_bar))
19247 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19248 }
19249
19250 prop = Fbuffer_local_value (prop, it->w->buffer);
19251 }
19252
19253 if (INTEGERP (prop) || FLOATP (prop))
19254 {
19255 int base_unit = (width_p
19256 ? FRAME_COLUMN_WIDTH (it->f)
19257 : FRAME_LINE_HEIGHT (it->f));
19258 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19259 }
19260
19261 if (CONSP (prop))
19262 {
19263 Lisp_Object car = XCAR (prop);
19264 Lisp_Object cdr = XCDR (prop);
19265
19266 if (SYMBOLP (car))
19267 {
19268 #ifdef HAVE_WINDOW_SYSTEM
19269 if (FRAME_WINDOW_P (it->f)
19270 && valid_image_p (prop))
19271 {
19272 int id = lookup_image (it->f, prop);
19273 struct image *img = IMAGE_FROM_ID (it->f, id);
19274
19275 return OK_PIXELS (width_p ? img->width : img->height);
19276 }
19277 #endif
19278 if (EQ (car, Qplus) || EQ (car, Qminus))
19279 {
19280 int first = 1;
19281 double px;
19282
19283 pixels = 0;
19284 while (CONSP (cdr))
19285 {
19286 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19287 font, width_p, align_to))
19288 return 0;
19289 if (first)
19290 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19291 else
19292 pixels += px;
19293 cdr = XCDR (cdr);
19294 }
19295 if (EQ (car, Qminus))
19296 pixels = -pixels;
19297 return OK_PIXELS (pixels);
19298 }
19299
19300 car = Fbuffer_local_value (car, it->w->buffer);
19301 }
19302
19303 if (INTEGERP (car) || FLOATP (car))
19304 {
19305 double fact;
19306 pixels = XFLOATINT (car);
19307 if (NILP (cdr))
19308 return OK_PIXELS (pixels);
19309 if (calc_pixel_width_or_height (&fact, it, cdr,
19310 font, width_p, align_to))
19311 return OK_PIXELS (pixels * fact);
19312 return 0;
19313 }
19314
19315 return 0;
19316 }
19317
19318 return 0;
19319 }
19320
19321 \f
19322 /***********************************************************************
19323 Glyph Display
19324 ***********************************************************************/
19325
19326 #ifdef HAVE_WINDOW_SYSTEM
19327
19328 #if GLYPH_DEBUG
19329
19330 void
19331 dump_glyph_string (s)
19332 struct glyph_string *s;
19333 {
19334 fprintf (stderr, "glyph string\n");
19335 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19336 s->x, s->y, s->width, s->height);
19337 fprintf (stderr, " ybase = %d\n", s->ybase);
19338 fprintf (stderr, " hl = %d\n", s->hl);
19339 fprintf (stderr, " left overhang = %d, right = %d\n",
19340 s->left_overhang, s->right_overhang);
19341 fprintf (stderr, " nchars = %d\n", s->nchars);
19342 fprintf (stderr, " extends to end of line = %d\n",
19343 s->extends_to_end_of_line_p);
19344 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19345 fprintf (stderr, " bg width = %d\n", s->background_width);
19346 }
19347
19348 #endif /* GLYPH_DEBUG */
19349
19350 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19351 of XChar2b structures for S; it can't be allocated in
19352 init_glyph_string because it must be allocated via `alloca'. W
19353 is the window on which S is drawn. ROW and AREA are the glyph row
19354 and area within the row from which S is constructed. START is the
19355 index of the first glyph structure covered by S. HL is a
19356 face-override for drawing S. */
19357
19358 #ifdef HAVE_NTGUI
19359 #define OPTIONAL_HDC(hdc) hdc,
19360 #define DECLARE_HDC(hdc) HDC hdc;
19361 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19362 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19363 #endif
19364
19365 #ifndef OPTIONAL_HDC
19366 #define OPTIONAL_HDC(hdc)
19367 #define DECLARE_HDC(hdc)
19368 #define ALLOCATE_HDC(hdc, f)
19369 #define RELEASE_HDC(hdc, f)
19370 #endif
19371
19372 static void
19373 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19374 struct glyph_string *s;
19375 DECLARE_HDC (hdc)
19376 XChar2b *char2b;
19377 struct window *w;
19378 struct glyph_row *row;
19379 enum glyph_row_area area;
19380 int start;
19381 enum draw_glyphs_face hl;
19382 {
19383 bzero (s, sizeof *s);
19384 s->w = w;
19385 s->f = XFRAME (w->frame);
19386 #ifdef HAVE_NTGUI
19387 s->hdc = hdc;
19388 #endif
19389 s->display = FRAME_X_DISPLAY (s->f);
19390 s->window = FRAME_X_WINDOW (s->f);
19391 s->char2b = char2b;
19392 s->hl = hl;
19393 s->row = row;
19394 s->area = area;
19395 s->first_glyph = row->glyphs[area] + start;
19396 s->height = row->height;
19397 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19398
19399 /* Display the internal border below the tool-bar window. */
19400 if (WINDOWP (s->f->tool_bar_window)
19401 && s->w == XWINDOW (s->f->tool_bar_window))
19402 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19403
19404 s->ybase = s->y + row->ascent;
19405 }
19406
19407
19408 /* Append the list of glyph strings with head H and tail T to the list
19409 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19410
19411 static INLINE void
19412 append_glyph_string_lists (head, tail, h, t)
19413 struct glyph_string **head, **tail;
19414 struct glyph_string *h, *t;
19415 {
19416 if (h)
19417 {
19418 if (*head)
19419 (*tail)->next = h;
19420 else
19421 *head = h;
19422 h->prev = *tail;
19423 *tail = t;
19424 }
19425 }
19426
19427
19428 /* Prepend the list of glyph strings with head H and tail T to the
19429 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19430 result. */
19431
19432 static INLINE void
19433 prepend_glyph_string_lists (head, tail, h, t)
19434 struct glyph_string **head, **tail;
19435 struct glyph_string *h, *t;
19436 {
19437 if (h)
19438 {
19439 if (*head)
19440 (*head)->prev = t;
19441 else
19442 *tail = t;
19443 t->next = *head;
19444 *head = h;
19445 }
19446 }
19447
19448
19449 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19450 Set *HEAD and *TAIL to the resulting list. */
19451
19452 static INLINE void
19453 append_glyph_string (head, tail, s)
19454 struct glyph_string **head, **tail;
19455 struct glyph_string *s;
19456 {
19457 s->next = s->prev = NULL;
19458 append_glyph_string_lists (head, tail, s, s);
19459 }
19460
19461
19462 /* Get face and two-byte form of character C in face FACE_ID on frame
19463 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19464 means we want to display multibyte text. DISPLAY_P non-zero means
19465 make sure that X resources for the face returned are allocated.
19466 Value is a pointer to a realized face that is ready for display if
19467 DISPLAY_P is non-zero. */
19468
19469 static INLINE struct face *
19470 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19471 struct frame *f;
19472 int c, face_id;
19473 XChar2b *char2b;
19474 int multibyte_p, display_p;
19475 {
19476 struct face *face = FACE_FROM_ID (f, face_id);
19477
19478 if (face->font)
19479 {
19480 unsigned code = face->font->driver->encode_char (face->font, c);
19481
19482 if (code != FONT_INVALID_CODE)
19483 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19484 else
19485 STORE_XCHAR2B (char2b, 0, 0);
19486 }
19487
19488 /* Make sure X resources of the face are allocated. */
19489 #ifdef HAVE_X_WINDOWS
19490 if (display_p)
19491 #endif
19492 {
19493 xassert (face != NULL);
19494 PREPARE_FACE_FOR_DISPLAY (f, face);
19495 }
19496
19497 return face;
19498 }
19499
19500
19501 /* Get face and two-byte form of character glyph GLYPH on frame F.
19502 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19503 a pointer to a realized face that is ready for display. */
19504
19505 static INLINE struct face *
19506 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19507 struct frame *f;
19508 struct glyph *glyph;
19509 XChar2b *char2b;
19510 int *two_byte_p;
19511 {
19512 struct face *face;
19513
19514 xassert (glyph->type == CHAR_GLYPH);
19515 face = FACE_FROM_ID (f, glyph->face_id);
19516
19517 if (two_byte_p)
19518 *two_byte_p = 0;
19519
19520 if (face->font)
19521 {
19522 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19523
19524 if (code != FONT_INVALID_CODE)
19525 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19526 else
19527 STORE_XCHAR2B (char2b, 0, 0);
19528 }
19529
19530 /* Make sure X resources of the face are allocated. */
19531 xassert (face != NULL);
19532 PREPARE_FACE_FOR_DISPLAY (f, face);
19533 return face;
19534 }
19535
19536
19537 /* Fill glyph string S with composition components specified by S->cmp.
19538
19539 BASE_FACE is the base face of the composition.
19540 S->gidx is the index of the first component for S.
19541
19542 OVERLAPS non-zero means S should draw the foreground only, and use
19543 its physical height for clipping. See also draw_glyphs.
19544
19545 Value is the index of a component not in S. */
19546
19547 static int
19548 fill_composite_glyph_string (s, base_face, overlaps)
19549 struct glyph_string *s;
19550 struct face *base_face;
19551 int overlaps;
19552 {
19553 int i;
19554
19555 xassert (s);
19556
19557 s->for_overlaps = overlaps;
19558
19559 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19560 {
19561 Lisp_Object gstring
19562 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19563 s->cmp->hash_index * 2);
19564
19565 s->face = base_face;
19566 s->font = base_face->font;
19567 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19568 {
19569 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19570 unsigned code;
19571 XChar2b * store_pos;
19572 if (NILP (g))
19573 break;
19574 code = LGLYPH_CODE (g);
19575 store_pos = s->char2b + i;
19576 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19577 }
19578 s->width = s->cmp->pixel_width;
19579 }
19580 else
19581 {
19582 /* For all glyphs of this composition, starting at the offset
19583 S->gidx, until we reach the end of the definition or encounter a
19584 glyph that requires the different face, add it to S. */
19585 struct face *face;
19586
19587 s->face = NULL;
19588 s->font = NULL;
19589 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19590 {
19591 int c = COMPOSITION_GLYPH (s->cmp, i);
19592
19593 if (c != '\t')
19594 {
19595 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19596 -1, Qnil);
19597
19598 face = get_char_face_and_encoding (s->f, c, face_id,
19599 s->char2b + i, 1, 1);
19600 if (face)
19601 {
19602 if (! s->face)
19603 {
19604 s->face = face;
19605 s->font = s->face->font;
19606 }
19607 else if (s->face != face)
19608 break;
19609 }
19610 }
19611 ++s->nchars;
19612 }
19613
19614 /* All glyph strings for the same composition has the same width,
19615 i.e. the width set for the first component of the composition. */
19616 s->width = s->first_glyph->pixel_width;
19617 }
19618
19619 /* If the specified font could not be loaded, use the frame's
19620 default font, but record the fact that we couldn't load it in
19621 the glyph string so that we can draw rectangles for the
19622 characters of the glyph string. */
19623 if (s->font == NULL)
19624 {
19625 s->font_not_found_p = 1;
19626 s->font = FRAME_FONT (s->f);
19627 }
19628
19629 /* Adjust base line for subscript/superscript text. */
19630 s->ybase += s->first_glyph->voffset;
19631
19632 /* This glyph string must always be drawn with 16-bit functions. */
19633 s->two_byte_p = 1;
19634
19635 return s->gidx + s->nchars;
19636 }
19637
19638
19639 /* Fill glyph string S from a sequence of character glyphs.
19640
19641 FACE_ID is the face id of the string. START is the index of the
19642 first glyph to consider, END is the index of the last + 1.
19643 OVERLAPS non-zero means S should draw the foreground only, and use
19644 its physical height for clipping. See also draw_glyphs.
19645
19646 Value is the index of the first glyph not in S. */
19647
19648 static int
19649 fill_glyph_string (s, face_id, start, end, overlaps)
19650 struct glyph_string *s;
19651 int face_id;
19652 int start, end, overlaps;
19653 {
19654 struct glyph *glyph, *last;
19655 int voffset;
19656 int glyph_not_available_p;
19657
19658 xassert (s->f == XFRAME (s->w->frame));
19659 xassert (s->nchars == 0);
19660 xassert (start >= 0 && end > start);
19661
19662 s->for_overlaps = overlaps,
19663 glyph = s->row->glyphs[s->area] + start;
19664 last = s->row->glyphs[s->area] + end;
19665 voffset = glyph->voffset;
19666 s->padding_p = glyph->padding_p;
19667 glyph_not_available_p = glyph->glyph_not_available_p;
19668
19669 while (glyph < last
19670 && glyph->type == CHAR_GLYPH
19671 && glyph->voffset == voffset
19672 /* Same face id implies same font, nowadays. */
19673 && glyph->face_id == face_id
19674 && glyph->glyph_not_available_p == glyph_not_available_p)
19675 {
19676 int two_byte_p;
19677
19678 s->face = get_glyph_face_and_encoding (s->f, glyph,
19679 s->char2b + s->nchars,
19680 &two_byte_p);
19681 s->two_byte_p = two_byte_p;
19682 ++s->nchars;
19683 xassert (s->nchars <= end - start);
19684 s->width += glyph->pixel_width;
19685 if (glyph++->padding_p != s->padding_p)
19686 break;
19687 }
19688
19689 s->font = s->face->font;
19690
19691 /* If the specified font could not be loaded, use the frame's font,
19692 but record the fact that we couldn't load it in
19693 S->font_not_found_p so that we can draw rectangles for the
19694 characters of the glyph string. */
19695 if (s->font == NULL || glyph_not_available_p)
19696 {
19697 s->font_not_found_p = 1;
19698 s->font = FRAME_FONT (s->f);
19699 }
19700
19701 /* Adjust base line for subscript/superscript text. */
19702 s->ybase += voffset;
19703
19704 xassert (s->face && s->face->gc);
19705 return glyph - s->row->glyphs[s->area];
19706 }
19707
19708
19709 /* Fill glyph string S from image glyph S->first_glyph. */
19710
19711 static void
19712 fill_image_glyph_string (s)
19713 struct glyph_string *s;
19714 {
19715 xassert (s->first_glyph->type == IMAGE_GLYPH);
19716 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19717 xassert (s->img);
19718 s->slice = s->first_glyph->slice;
19719 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19720 s->font = s->face->font;
19721 s->width = s->first_glyph->pixel_width;
19722
19723 /* Adjust base line for subscript/superscript text. */
19724 s->ybase += s->first_glyph->voffset;
19725 }
19726
19727
19728 /* Fill glyph string S from a sequence of stretch glyphs.
19729
19730 ROW is the glyph row in which the glyphs are found, AREA is the
19731 area within the row. START is the index of the first glyph to
19732 consider, END is the index of the last + 1.
19733
19734 Value is the index of the first glyph not in S. */
19735
19736 static int
19737 fill_stretch_glyph_string (s, row, area, start, end)
19738 struct glyph_string *s;
19739 struct glyph_row *row;
19740 enum glyph_row_area area;
19741 int start, end;
19742 {
19743 struct glyph *glyph, *last;
19744 int voffset, face_id;
19745
19746 xassert (s->first_glyph->type == STRETCH_GLYPH);
19747
19748 glyph = s->row->glyphs[s->area] + start;
19749 last = s->row->glyphs[s->area] + end;
19750 face_id = glyph->face_id;
19751 s->face = FACE_FROM_ID (s->f, face_id);
19752 s->font = s->face->font;
19753 s->width = glyph->pixel_width;
19754 s->nchars = 1;
19755 voffset = glyph->voffset;
19756
19757 for (++glyph;
19758 (glyph < last
19759 && glyph->type == STRETCH_GLYPH
19760 && glyph->voffset == voffset
19761 && glyph->face_id == face_id);
19762 ++glyph)
19763 s->width += glyph->pixel_width;
19764
19765 /* Adjust base line for subscript/superscript text. */
19766 s->ybase += voffset;
19767
19768 /* The case that face->gc == 0 is handled when drawing the glyph
19769 string by calling PREPARE_FACE_FOR_DISPLAY. */
19770 xassert (s->face);
19771 return glyph - s->row->glyphs[s->area];
19772 }
19773
19774 static struct font_metrics *
19775 get_per_char_metric (f, font, char2b)
19776 struct frame *f;
19777 struct font *font;
19778 XChar2b *char2b;
19779 {
19780 static struct font_metrics metrics;
19781 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19782 struct font *fontp;
19783
19784 if (! font || code == FONT_INVALID_CODE)
19785 return NULL;
19786 font->driver->text_extents (font, &code, 1, &metrics);
19787 return &metrics;
19788 }
19789
19790 /* EXPORT for RIF:
19791 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19792 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19793 assumed to be zero. */
19794
19795 void
19796 x_get_glyph_overhangs (glyph, f, left, right)
19797 struct glyph *glyph;
19798 struct frame *f;
19799 int *left, *right;
19800 {
19801 *left = *right = 0;
19802
19803 if (glyph->type == CHAR_GLYPH)
19804 {
19805 struct face *face;
19806 XChar2b char2b;
19807 struct font_metrics *pcm;
19808
19809 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19810 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19811 {
19812 if (pcm->rbearing > pcm->width)
19813 *right = pcm->rbearing - pcm->width;
19814 if (pcm->lbearing < 0)
19815 *left = -pcm->lbearing;
19816 }
19817 }
19818 else if (glyph->type == COMPOSITE_GLYPH)
19819 {
19820 struct composition *cmp = composition_table[glyph->u.cmp_id];
19821
19822 *right = cmp->rbearing - cmp->pixel_width;
19823 *left = - cmp->lbearing;
19824 }
19825 }
19826
19827
19828 /* Return the index of the first glyph preceding glyph string S that
19829 is overwritten by S because of S's left overhang. Value is -1
19830 if no glyphs are overwritten. */
19831
19832 static int
19833 left_overwritten (s)
19834 struct glyph_string *s;
19835 {
19836 int k;
19837
19838 if (s->left_overhang)
19839 {
19840 int x = 0, i;
19841 struct glyph *glyphs = s->row->glyphs[s->area];
19842 int first = s->first_glyph - glyphs;
19843
19844 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19845 x -= glyphs[i].pixel_width;
19846
19847 k = i + 1;
19848 }
19849 else
19850 k = -1;
19851
19852 return k;
19853 }
19854
19855
19856 /* Return the index of the first glyph preceding glyph string S that
19857 is overwriting S because of its right overhang. Value is -1 if no
19858 glyph in front of S overwrites S. */
19859
19860 static int
19861 left_overwriting (s)
19862 struct glyph_string *s;
19863 {
19864 int i, k, x;
19865 struct glyph *glyphs = s->row->glyphs[s->area];
19866 int first = s->first_glyph - glyphs;
19867
19868 k = -1;
19869 x = 0;
19870 for (i = first - 1; i >= 0; --i)
19871 {
19872 int left, right;
19873 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19874 if (x + right > 0)
19875 k = i;
19876 x -= glyphs[i].pixel_width;
19877 }
19878
19879 return k;
19880 }
19881
19882
19883 /* Return the index of the last glyph following glyph string S that is
19884 not overwritten by S because of S's right overhang. Value is -1 if
19885 no such glyph is found. */
19886
19887 static int
19888 right_overwritten (s)
19889 struct glyph_string *s;
19890 {
19891 int k = -1;
19892
19893 if (s->right_overhang)
19894 {
19895 int x = 0, i;
19896 struct glyph *glyphs = s->row->glyphs[s->area];
19897 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19898 int end = s->row->used[s->area];
19899
19900 for (i = first; i < end && s->right_overhang > x; ++i)
19901 x += glyphs[i].pixel_width;
19902
19903 k = i;
19904 }
19905
19906 return k;
19907 }
19908
19909
19910 /* Return the index of the last glyph following glyph string S that
19911 overwrites S because of its left overhang. Value is negative
19912 if no such glyph is found. */
19913
19914 static int
19915 right_overwriting (s)
19916 struct glyph_string *s;
19917 {
19918 int i, k, x;
19919 int end = s->row->used[s->area];
19920 struct glyph *glyphs = s->row->glyphs[s->area];
19921 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19922
19923 k = -1;
19924 x = 0;
19925 for (i = first; i < end; ++i)
19926 {
19927 int left, right;
19928 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19929 if (x - left < 0)
19930 k = i;
19931 x += glyphs[i].pixel_width;
19932 }
19933
19934 return k;
19935 }
19936
19937
19938 /* Set background width of glyph string S. START is the index of the
19939 first glyph following S. LAST_X is the right-most x-position + 1
19940 in the drawing area. */
19941
19942 static INLINE void
19943 set_glyph_string_background_width (s, start, last_x)
19944 struct glyph_string *s;
19945 int start;
19946 int last_x;
19947 {
19948 /* If the face of this glyph string has to be drawn to the end of
19949 the drawing area, set S->extends_to_end_of_line_p. */
19950
19951 if (start == s->row->used[s->area]
19952 && s->area == TEXT_AREA
19953 && ((s->row->fill_line_p
19954 && (s->hl == DRAW_NORMAL_TEXT
19955 || s->hl == DRAW_IMAGE_RAISED
19956 || s->hl == DRAW_IMAGE_SUNKEN))
19957 || s->hl == DRAW_MOUSE_FACE))
19958 s->extends_to_end_of_line_p = 1;
19959
19960 /* If S extends its face to the end of the line, set its
19961 background_width to the distance to the right edge of the drawing
19962 area. */
19963 if (s->extends_to_end_of_line_p)
19964 s->background_width = last_x - s->x + 1;
19965 else
19966 s->background_width = s->width;
19967 }
19968
19969
19970 /* Compute overhangs and x-positions for glyph string S and its
19971 predecessors, or successors. X is the starting x-position for S.
19972 BACKWARD_P non-zero means process predecessors. */
19973
19974 static void
19975 compute_overhangs_and_x (s, x, backward_p)
19976 struct glyph_string *s;
19977 int x;
19978 int backward_p;
19979 {
19980 if (backward_p)
19981 {
19982 while (s)
19983 {
19984 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19985 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19986 x -= s->width;
19987 s->x = x;
19988 s = s->prev;
19989 }
19990 }
19991 else
19992 {
19993 while (s)
19994 {
19995 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19996 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19997 s->x = x;
19998 x += s->width;
19999 s = s->next;
20000 }
20001 }
20002 }
20003
20004
20005
20006 /* The following macros are only called from draw_glyphs below.
20007 They reference the following parameters of that function directly:
20008 `w', `row', `area', and `overlap_p'
20009 as well as the following local variables:
20010 `s', `f', and `hdc' (in W32) */
20011
20012 #ifdef HAVE_NTGUI
20013 /* On W32, silently add local `hdc' variable to argument list of
20014 init_glyph_string. */
20015 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20016 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20017 #else
20018 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20019 init_glyph_string (s, char2b, w, row, area, start, hl)
20020 #endif
20021
20022 /* Add a glyph string for a stretch glyph to the list of strings
20023 between HEAD and TAIL. START is the index of the stretch glyph in
20024 row area AREA of glyph row ROW. END is the index of the last glyph
20025 in that glyph row area. X is the current output position assigned
20026 to the new glyph string constructed. HL overrides that face of the
20027 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20028 is the right-most x-position of the drawing area. */
20029
20030 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20031 and below -- keep them on one line. */
20032 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20033 do \
20034 { \
20035 s = (struct glyph_string *) alloca (sizeof *s); \
20036 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20037 START = fill_stretch_glyph_string (s, row, area, START, END); \
20038 append_glyph_string (&HEAD, &TAIL, s); \
20039 s->x = (X); \
20040 } \
20041 while (0)
20042
20043
20044 /* Add a glyph string for an image glyph to the list of strings
20045 between HEAD and TAIL. START is the index of the image glyph in
20046 row area AREA of glyph row ROW. END is the index of the last glyph
20047 in that glyph row area. X is the current output position assigned
20048 to the new glyph string constructed. HL overrides that face of the
20049 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20050 is the right-most x-position of the drawing area. */
20051
20052 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20053 do \
20054 { \
20055 s = (struct glyph_string *) alloca (sizeof *s); \
20056 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20057 fill_image_glyph_string (s); \
20058 append_glyph_string (&HEAD, &TAIL, s); \
20059 ++START; \
20060 s->x = (X); \
20061 } \
20062 while (0)
20063
20064
20065 /* Add a glyph string for a sequence of character glyphs to the list
20066 of strings between HEAD and TAIL. START is the index of the first
20067 glyph in row area AREA of glyph row ROW that is part of the new
20068 glyph string. END is the index of the last glyph in that glyph row
20069 area. X is the current output position assigned to the new glyph
20070 string constructed. HL overrides that face of the glyph; e.g. it
20071 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20072 right-most x-position of the drawing area. */
20073
20074 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20075 do \
20076 { \
20077 int face_id; \
20078 XChar2b *char2b; \
20079 \
20080 face_id = (row)->glyphs[area][START].face_id; \
20081 \
20082 s = (struct glyph_string *) alloca (sizeof *s); \
20083 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20084 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20085 append_glyph_string (&HEAD, &TAIL, s); \
20086 s->x = (X); \
20087 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20088 } \
20089 while (0)
20090
20091
20092 /* Add a glyph string for a composite sequence to the list of strings
20093 between HEAD and TAIL. START is the index of the first glyph in
20094 row area AREA of glyph row ROW that is part of the new glyph
20095 string. END is the index of the last glyph in that glyph row area.
20096 X is the current output position assigned to the new glyph string
20097 constructed. HL overrides that face of the glyph; e.g. it is
20098 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20099 x-position of the drawing area. */
20100
20101 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20102 do { \
20103 int face_id = (row)->glyphs[area][START].face_id; \
20104 struct face *base_face = FACE_FROM_ID (f, face_id); \
20105 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
20106 struct composition *cmp = composition_table[cmp_id]; \
20107 XChar2b *char2b; \
20108 struct glyph_string *first_s; \
20109 int n; \
20110 \
20111 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20112 \
20113 /* Make glyph_strings for each glyph sequence that is drawable by \
20114 the same face, and append them to HEAD/TAIL. */ \
20115 for (n = 0; n < cmp->glyph_len;) \
20116 { \
20117 s = (struct glyph_string *) alloca (sizeof *s); \
20118 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20119 append_glyph_string (&(HEAD), &(TAIL), s); \
20120 s->cmp = cmp; \
20121 s->gidx = n; \
20122 s->x = (X); \
20123 if (n == 0) \
20124 first_s = s; \
20125 n = fill_composite_glyph_string (s, base_face, overlaps); \
20126 } \
20127 \
20128 ++START; \
20129 s = first_s; \
20130 } while (0)
20131
20132
20133 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20134 of AREA of glyph row ROW on window W between indices START and END.
20135 HL overrides the face for drawing glyph strings, e.g. it is
20136 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20137 x-positions of the drawing area.
20138
20139 This is an ugly monster macro construct because we must use alloca
20140 to allocate glyph strings (because draw_glyphs can be called
20141 asynchronously). */
20142
20143 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20144 do \
20145 { \
20146 HEAD = TAIL = NULL; \
20147 while (START < END) \
20148 { \
20149 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20150 switch (first_glyph->type) \
20151 { \
20152 case CHAR_GLYPH: \
20153 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20154 HL, X, LAST_X); \
20155 break; \
20156 \
20157 case COMPOSITE_GLYPH: \
20158 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20159 HL, X, LAST_X); \
20160 break; \
20161 \
20162 case STRETCH_GLYPH: \
20163 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20164 HL, X, LAST_X); \
20165 break; \
20166 \
20167 case IMAGE_GLYPH: \
20168 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20169 HL, X, LAST_X); \
20170 break; \
20171 \
20172 default: \
20173 abort (); \
20174 } \
20175 \
20176 if (s) \
20177 { \
20178 set_glyph_string_background_width (s, START, LAST_X); \
20179 (X) += s->width; \
20180 } \
20181 } \
20182 } \
20183 while (0)
20184
20185
20186 /* Draw glyphs between START and END in AREA of ROW on window W,
20187 starting at x-position X. X is relative to AREA in W. HL is a
20188 face-override with the following meaning:
20189
20190 DRAW_NORMAL_TEXT draw normally
20191 DRAW_CURSOR draw in cursor face
20192 DRAW_MOUSE_FACE draw in mouse face.
20193 DRAW_INVERSE_VIDEO draw in mode line face
20194 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20195 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20196
20197 If OVERLAPS is non-zero, draw only the foreground of characters and
20198 clip to the physical height of ROW. Non-zero value also defines
20199 the overlapping part to be drawn:
20200
20201 OVERLAPS_PRED overlap with preceding rows
20202 OVERLAPS_SUCC overlap with succeeding rows
20203 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20204 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20205
20206 Value is the x-position reached, relative to AREA of W. */
20207
20208 static int
20209 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20210 struct window *w;
20211 int x;
20212 struct glyph_row *row;
20213 enum glyph_row_area area;
20214 EMACS_INT start, end;
20215 enum draw_glyphs_face hl;
20216 int overlaps;
20217 {
20218 struct glyph_string *head, *tail;
20219 struct glyph_string *s;
20220 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20221 int i, j, x_reached, last_x, area_left = 0;
20222 struct frame *f = XFRAME (WINDOW_FRAME (w));
20223 DECLARE_HDC (hdc);
20224
20225 ALLOCATE_HDC (hdc, f);
20226
20227 /* Let's rather be paranoid than getting a SEGV. */
20228 end = min (end, row->used[area]);
20229 start = max (0, start);
20230 start = min (end, start);
20231
20232 /* Translate X to frame coordinates. Set last_x to the right
20233 end of the drawing area. */
20234 if (row->full_width_p)
20235 {
20236 /* X is relative to the left edge of W, without scroll bars
20237 or fringes. */
20238 area_left = WINDOW_LEFT_EDGE_X (w);
20239 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20240 }
20241 else
20242 {
20243 area_left = window_box_left (w, area);
20244 last_x = area_left + window_box_width (w, area);
20245 }
20246 x += area_left;
20247
20248 /* Build a doubly-linked list of glyph_string structures between
20249 head and tail from what we have to draw. Note that the macro
20250 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20251 the reason we use a separate variable `i'. */
20252 i = start;
20253 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20254 if (tail)
20255 x_reached = tail->x + tail->background_width;
20256 else
20257 x_reached = x;
20258
20259 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20260 the row, redraw some glyphs in front or following the glyph
20261 strings built above. */
20262 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20263 {
20264 struct glyph_string *h, *t;
20265 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20266 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20267 int dummy_x = 0;
20268
20269 /* If mouse highlighting is on, we may need to draw adjacent
20270 glyphs using mouse-face highlighting. */
20271 if (area == TEXT_AREA && row->mouse_face_p)
20272 {
20273 struct glyph_row *mouse_beg_row, *mouse_end_row;
20274
20275 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20276 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20277
20278 if (row >= mouse_beg_row && row <= mouse_end_row)
20279 {
20280 check_mouse_face = 1;
20281 mouse_beg_col = (row == mouse_beg_row)
20282 ? dpyinfo->mouse_face_beg_col : 0;
20283 mouse_end_col = (row == mouse_end_row)
20284 ? dpyinfo->mouse_face_end_col
20285 : row->used[TEXT_AREA];
20286 }
20287 }
20288
20289 /* Compute overhangs for all glyph strings. */
20290 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20291 for (s = head; s; s = s->next)
20292 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20293
20294 /* Prepend glyph strings for glyphs in front of the first glyph
20295 string that are overwritten because of the first glyph
20296 string's left overhang. The background of all strings
20297 prepended must be drawn because the first glyph string
20298 draws over it. */
20299 i = left_overwritten (head);
20300 if (i >= 0)
20301 {
20302 enum draw_glyphs_face overlap_hl;
20303
20304 /* If this row contains mouse highlighting, attempt to draw
20305 the overlapped glyphs with the correct highlight. This
20306 code fails if the overlap encompasses more than one glyph
20307 and mouse-highlight spans only some of these glyphs.
20308 However, making it work perfectly involves a lot more
20309 code, and I don't know if the pathological case occurs in
20310 practice, so we'll stick to this for now. --- cyd */
20311 if (check_mouse_face
20312 && mouse_beg_col < start && mouse_end_col > i)
20313 overlap_hl = DRAW_MOUSE_FACE;
20314 else
20315 overlap_hl = DRAW_NORMAL_TEXT;
20316
20317 j = i;
20318 BUILD_GLYPH_STRINGS (j, start, h, t,
20319 overlap_hl, dummy_x, last_x);
20320 compute_overhangs_and_x (t, head->x, 1);
20321 prepend_glyph_string_lists (&head, &tail, h, t);
20322 clip_head = head;
20323 }
20324
20325 /* Prepend glyph strings for glyphs in front of the first glyph
20326 string that overwrite that glyph string because of their
20327 right overhang. For these strings, only the foreground must
20328 be drawn, because it draws over the glyph string at `head'.
20329 The background must not be drawn because this would overwrite
20330 right overhangs of preceding glyphs for which no glyph
20331 strings exist. */
20332 i = left_overwriting (head);
20333 if (i >= 0)
20334 {
20335 enum draw_glyphs_face overlap_hl;
20336
20337 if (check_mouse_face
20338 && mouse_beg_col < start && mouse_end_col > i)
20339 overlap_hl = DRAW_MOUSE_FACE;
20340 else
20341 overlap_hl = DRAW_NORMAL_TEXT;
20342
20343 clip_head = head;
20344 BUILD_GLYPH_STRINGS (i, start, h, t,
20345 overlap_hl, dummy_x, last_x);
20346 for (s = h; s; s = s->next)
20347 s->background_filled_p = 1;
20348 compute_overhangs_and_x (t, head->x, 1);
20349 prepend_glyph_string_lists (&head, &tail, h, t);
20350 }
20351
20352 /* Append glyphs strings for glyphs following the last glyph
20353 string tail that are overwritten by tail. The background of
20354 these strings has to be drawn because tail's foreground draws
20355 over it. */
20356 i = right_overwritten (tail);
20357 if (i >= 0)
20358 {
20359 enum draw_glyphs_face overlap_hl;
20360
20361 if (check_mouse_face
20362 && mouse_beg_col < i && mouse_end_col > end)
20363 overlap_hl = DRAW_MOUSE_FACE;
20364 else
20365 overlap_hl = DRAW_NORMAL_TEXT;
20366
20367 BUILD_GLYPH_STRINGS (end, i, h, t,
20368 overlap_hl, x, last_x);
20369 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20370 append_glyph_string_lists (&head, &tail, h, t);
20371 clip_tail = tail;
20372 }
20373
20374 /* Append glyph strings for glyphs following the last glyph
20375 string tail that overwrite tail. The foreground of such
20376 glyphs has to be drawn because it writes into the background
20377 of tail. The background must not be drawn because it could
20378 paint over the foreground of following glyphs. */
20379 i = right_overwriting (tail);
20380 if (i >= 0)
20381 {
20382 enum draw_glyphs_face overlap_hl;
20383 if (check_mouse_face
20384 && mouse_beg_col < i && mouse_end_col > end)
20385 overlap_hl = DRAW_MOUSE_FACE;
20386 else
20387 overlap_hl = DRAW_NORMAL_TEXT;
20388
20389 clip_tail = tail;
20390 i++; /* We must include the Ith glyph. */
20391 BUILD_GLYPH_STRINGS (end, i, h, t,
20392 overlap_hl, x, last_x);
20393 for (s = h; s; s = s->next)
20394 s->background_filled_p = 1;
20395 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20396 append_glyph_string_lists (&head, &tail, h, t);
20397 }
20398 if (clip_head || clip_tail)
20399 for (s = head; s; s = s->next)
20400 {
20401 s->clip_head = clip_head;
20402 s->clip_tail = clip_tail;
20403 }
20404 }
20405
20406 /* Draw all strings. */
20407 for (s = head; s; s = s->next)
20408 FRAME_RIF (f)->draw_glyph_string (s);
20409
20410 if (area == TEXT_AREA
20411 && !row->full_width_p
20412 /* When drawing overlapping rows, only the glyph strings'
20413 foreground is drawn, which doesn't erase a cursor
20414 completely. */
20415 && !overlaps)
20416 {
20417 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20418 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20419 : (tail ? tail->x + tail->background_width : x));
20420 x0 -= area_left;
20421 x1 -= area_left;
20422
20423 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20424 row->y, MATRIX_ROW_BOTTOM_Y (row));
20425 }
20426
20427 /* Value is the x-position up to which drawn, relative to AREA of W.
20428 This doesn't include parts drawn because of overhangs. */
20429 if (row->full_width_p)
20430 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20431 else
20432 x_reached -= area_left;
20433
20434 RELEASE_HDC (hdc, f);
20435
20436 return x_reached;
20437 }
20438
20439 /* Expand row matrix if too narrow. Don't expand if area
20440 is not present. */
20441
20442 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20443 { \
20444 if (!fonts_changed_p \
20445 && (it->glyph_row->glyphs[area] \
20446 < it->glyph_row->glyphs[area + 1])) \
20447 { \
20448 it->w->ncols_scale_factor++; \
20449 fonts_changed_p = 1; \
20450 } \
20451 }
20452
20453 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20454 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20455
20456 static INLINE void
20457 append_glyph (it)
20458 struct it *it;
20459 {
20460 struct glyph *glyph;
20461 enum glyph_row_area area = it->area;
20462
20463 xassert (it->glyph_row);
20464 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20465
20466 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20467 if (glyph < it->glyph_row->glyphs[area + 1])
20468 {
20469 glyph->charpos = CHARPOS (it->position);
20470 glyph->object = it->object;
20471 if (it->pixel_width > 0)
20472 {
20473 glyph->pixel_width = it->pixel_width;
20474 glyph->padding_p = 0;
20475 }
20476 else
20477 {
20478 /* Assure at least 1-pixel width. Otherwise, cursor can't
20479 be displayed correctly. */
20480 glyph->pixel_width = 1;
20481 glyph->padding_p = 1;
20482 }
20483 glyph->ascent = it->ascent;
20484 glyph->descent = it->descent;
20485 glyph->voffset = it->voffset;
20486 glyph->type = CHAR_GLYPH;
20487 glyph->avoid_cursor_p = it->avoid_cursor_p;
20488 glyph->multibyte_p = it->multibyte_p;
20489 glyph->left_box_line_p = it->start_of_box_run_p;
20490 glyph->right_box_line_p = it->end_of_box_run_p;
20491 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20492 || it->phys_descent > it->descent);
20493 glyph->glyph_not_available_p = it->glyph_not_available_p;
20494 glyph->face_id = it->face_id;
20495 glyph->u.ch = it->char_to_display;
20496 glyph->slice = null_glyph_slice;
20497 glyph->font_type = FONT_TYPE_UNKNOWN;
20498 ++it->glyph_row->used[area];
20499 }
20500 else
20501 IT_EXPAND_MATRIX_WIDTH (it, area);
20502 }
20503
20504 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20505 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20506
20507 static INLINE void
20508 append_composite_glyph (it)
20509 struct it *it;
20510 {
20511 struct glyph *glyph;
20512 enum glyph_row_area area = it->area;
20513
20514 xassert (it->glyph_row);
20515
20516 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20517 if (glyph < it->glyph_row->glyphs[area + 1])
20518 {
20519 glyph->charpos = CHARPOS (it->position);
20520 glyph->object = it->object;
20521 glyph->pixel_width = it->pixel_width;
20522 glyph->ascent = it->ascent;
20523 glyph->descent = it->descent;
20524 glyph->voffset = it->voffset;
20525 glyph->type = COMPOSITE_GLYPH;
20526 glyph->avoid_cursor_p = it->avoid_cursor_p;
20527 glyph->multibyte_p = it->multibyte_p;
20528 glyph->left_box_line_p = it->start_of_box_run_p;
20529 glyph->right_box_line_p = it->end_of_box_run_p;
20530 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20531 || it->phys_descent > it->descent);
20532 glyph->padding_p = 0;
20533 glyph->glyph_not_available_p = 0;
20534 glyph->face_id = it->face_id;
20535 glyph->u.cmp_id = it->cmp_id;
20536 glyph->slice = null_glyph_slice;
20537 glyph->font_type = FONT_TYPE_UNKNOWN;
20538 ++it->glyph_row->used[area];
20539 }
20540 else
20541 IT_EXPAND_MATRIX_WIDTH (it, area);
20542 }
20543
20544
20545 /* Change IT->ascent and IT->height according to the setting of
20546 IT->voffset. */
20547
20548 static INLINE void
20549 take_vertical_position_into_account (it)
20550 struct it *it;
20551 {
20552 if (it->voffset)
20553 {
20554 if (it->voffset < 0)
20555 /* Increase the ascent so that we can display the text higher
20556 in the line. */
20557 it->ascent -= it->voffset;
20558 else
20559 /* Increase the descent so that we can display the text lower
20560 in the line. */
20561 it->descent += it->voffset;
20562 }
20563 }
20564
20565
20566 /* Produce glyphs/get display metrics for the image IT is loaded with.
20567 See the description of struct display_iterator in dispextern.h for
20568 an overview of struct display_iterator. */
20569
20570 static void
20571 produce_image_glyph (it)
20572 struct it *it;
20573 {
20574 struct image *img;
20575 struct face *face;
20576 int glyph_ascent, crop;
20577 struct glyph_slice slice;
20578
20579 xassert (it->what == IT_IMAGE);
20580
20581 face = FACE_FROM_ID (it->f, it->face_id);
20582 xassert (face);
20583 /* Make sure X resources of the face is loaded. */
20584 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20585
20586 if (it->image_id < 0)
20587 {
20588 /* Fringe bitmap. */
20589 it->ascent = it->phys_ascent = 0;
20590 it->descent = it->phys_descent = 0;
20591 it->pixel_width = 0;
20592 it->nglyphs = 0;
20593 return;
20594 }
20595
20596 img = IMAGE_FROM_ID (it->f, it->image_id);
20597 xassert (img);
20598 /* Make sure X resources of the image is loaded. */
20599 prepare_image_for_display (it->f, img);
20600
20601 slice.x = slice.y = 0;
20602 slice.width = img->width;
20603 slice.height = img->height;
20604
20605 if (INTEGERP (it->slice.x))
20606 slice.x = XINT (it->slice.x);
20607 else if (FLOATP (it->slice.x))
20608 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20609
20610 if (INTEGERP (it->slice.y))
20611 slice.y = XINT (it->slice.y);
20612 else if (FLOATP (it->slice.y))
20613 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20614
20615 if (INTEGERP (it->slice.width))
20616 slice.width = XINT (it->slice.width);
20617 else if (FLOATP (it->slice.width))
20618 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20619
20620 if (INTEGERP (it->slice.height))
20621 slice.height = XINT (it->slice.height);
20622 else if (FLOATP (it->slice.height))
20623 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20624
20625 if (slice.x >= img->width)
20626 slice.x = img->width;
20627 if (slice.y >= img->height)
20628 slice.y = img->height;
20629 if (slice.x + slice.width >= img->width)
20630 slice.width = img->width - slice.x;
20631 if (slice.y + slice.height > img->height)
20632 slice.height = img->height - slice.y;
20633
20634 if (slice.width == 0 || slice.height == 0)
20635 return;
20636
20637 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20638
20639 it->descent = slice.height - glyph_ascent;
20640 if (slice.y == 0)
20641 it->descent += img->vmargin;
20642 if (slice.y + slice.height == img->height)
20643 it->descent += img->vmargin;
20644 it->phys_descent = it->descent;
20645
20646 it->pixel_width = slice.width;
20647 if (slice.x == 0)
20648 it->pixel_width += img->hmargin;
20649 if (slice.x + slice.width == img->width)
20650 it->pixel_width += img->hmargin;
20651
20652 /* It's quite possible for images to have an ascent greater than
20653 their height, so don't get confused in that case. */
20654 if (it->descent < 0)
20655 it->descent = 0;
20656
20657 #if 0 /* this breaks image tiling */
20658 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20659 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20660 if (face_ascent > it->ascent)
20661 it->ascent = it->phys_ascent = face_ascent;
20662 #endif
20663
20664 it->nglyphs = 1;
20665
20666 if (face->box != FACE_NO_BOX)
20667 {
20668 if (face->box_line_width > 0)
20669 {
20670 if (slice.y == 0)
20671 it->ascent += face->box_line_width;
20672 if (slice.y + slice.height == img->height)
20673 it->descent += face->box_line_width;
20674 }
20675
20676 if (it->start_of_box_run_p && slice.x == 0)
20677 it->pixel_width += eabs (face->box_line_width);
20678 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20679 it->pixel_width += eabs (face->box_line_width);
20680 }
20681
20682 take_vertical_position_into_account (it);
20683
20684 /* Automatically crop wide image glyphs at right edge so we can
20685 draw the cursor on same display row. */
20686 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20687 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20688 {
20689 it->pixel_width -= crop;
20690 slice.width -= crop;
20691 }
20692
20693 if (it->glyph_row)
20694 {
20695 struct glyph *glyph;
20696 enum glyph_row_area area = it->area;
20697
20698 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20699 if (glyph < it->glyph_row->glyphs[area + 1])
20700 {
20701 glyph->charpos = CHARPOS (it->position);
20702 glyph->object = it->object;
20703 glyph->pixel_width = it->pixel_width;
20704 glyph->ascent = glyph_ascent;
20705 glyph->descent = it->descent;
20706 glyph->voffset = it->voffset;
20707 glyph->type = IMAGE_GLYPH;
20708 glyph->avoid_cursor_p = it->avoid_cursor_p;
20709 glyph->multibyte_p = it->multibyte_p;
20710 glyph->left_box_line_p = it->start_of_box_run_p;
20711 glyph->right_box_line_p = it->end_of_box_run_p;
20712 glyph->overlaps_vertically_p = 0;
20713 glyph->padding_p = 0;
20714 glyph->glyph_not_available_p = 0;
20715 glyph->face_id = it->face_id;
20716 glyph->u.img_id = img->id;
20717 glyph->slice = slice;
20718 glyph->font_type = FONT_TYPE_UNKNOWN;
20719 ++it->glyph_row->used[area];
20720 }
20721 else
20722 IT_EXPAND_MATRIX_WIDTH (it, area);
20723 }
20724 }
20725
20726
20727 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20728 of the glyph, WIDTH and HEIGHT are the width and height of the
20729 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20730
20731 static void
20732 append_stretch_glyph (it, object, width, height, ascent)
20733 struct it *it;
20734 Lisp_Object object;
20735 int width, height;
20736 int ascent;
20737 {
20738 struct glyph *glyph;
20739 enum glyph_row_area area = it->area;
20740
20741 xassert (ascent >= 0 && ascent <= height);
20742
20743 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20744 if (glyph < it->glyph_row->glyphs[area + 1])
20745 {
20746 glyph->charpos = CHARPOS (it->position);
20747 glyph->object = object;
20748 glyph->pixel_width = width;
20749 glyph->ascent = ascent;
20750 glyph->descent = height - ascent;
20751 glyph->voffset = it->voffset;
20752 glyph->type = STRETCH_GLYPH;
20753 glyph->avoid_cursor_p = it->avoid_cursor_p;
20754 glyph->multibyte_p = it->multibyte_p;
20755 glyph->left_box_line_p = it->start_of_box_run_p;
20756 glyph->right_box_line_p = it->end_of_box_run_p;
20757 glyph->overlaps_vertically_p = 0;
20758 glyph->padding_p = 0;
20759 glyph->glyph_not_available_p = 0;
20760 glyph->face_id = it->face_id;
20761 glyph->u.stretch.ascent = ascent;
20762 glyph->u.stretch.height = height;
20763 glyph->slice = null_glyph_slice;
20764 glyph->font_type = FONT_TYPE_UNKNOWN;
20765 ++it->glyph_row->used[area];
20766 }
20767 else
20768 IT_EXPAND_MATRIX_WIDTH (it, area);
20769 }
20770
20771
20772 /* Produce a stretch glyph for iterator IT. IT->object is the value
20773 of the glyph property displayed. The value must be a list
20774 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20775 being recognized:
20776
20777 1. `:width WIDTH' specifies that the space should be WIDTH *
20778 canonical char width wide. WIDTH may be an integer or floating
20779 point number.
20780
20781 2. `:relative-width FACTOR' specifies that the width of the stretch
20782 should be computed from the width of the first character having the
20783 `glyph' property, and should be FACTOR times that width.
20784
20785 3. `:align-to HPOS' specifies that the space should be wide enough
20786 to reach HPOS, a value in canonical character units.
20787
20788 Exactly one of the above pairs must be present.
20789
20790 4. `:height HEIGHT' specifies that the height of the stretch produced
20791 should be HEIGHT, measured in canonical character units.
20792
20793 5. `:relative-height FACTOR' specifies that the height of the
20794 stretch should be FACTOR times the height of the characters having
20795 the glyph property.
20796
20797 Either none or exactly one of 4 or 5 must be present.
20798
20799 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20800 of the stretch should be used for the ascent of the stretch.
20801 ASCENT must be in the range 0 <= ASCENT <= 100. */
20802
20803 static void
20804 produce_stretch_glyph (it)
20805 struct it *it;
20806 {
20807 /* (space :width WIDTH :height HEIGHT ...) */
20808 Lisp_Object prop, plist;
20809 int width = 0, height = 0, align_to = -1;
20810 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20811 int ascent = 0;
20812 double tem;
20813 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20814 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20815
20816 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20817
20818 /* List should start with `space'. */
20819 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20820 plist = XCDR (it->object);
20821
20822 /* Compute the width of the stretch. */
20823 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20824 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20825 {
20826 /* Absolute width `:width WIDTH' specified and valid. */
20827 zero_width_ok_p = 1;
20828 width = (int)tem;
20829 }
20830 else if (prop = Fplist_get (plist, QCrelative_width),
20831 NUMVAL (prop) > 0)
20832 {
20833 /* Relative width `:relative-width FACTOR' specified and valid.
20834 Compute the width of the characters having the `glyph'
20835 property. */
20836 struct it it2;
20837 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20838
20839 it2 = *it;
20840 if (it->multibyte_p)
20841 {
20842 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20843 - IT_BYTEPOS (*it));
20844 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20845 }
20846 else
20847 it2.c = *p, it2.len = 1;
20848
20849 it2.glyph_row = NULL;
20850 it2.what = IT_CHARACTER;
20851 x_produce_glyphs (&it2);
20852 width = NUMVAL (prop) * it2.pixel_width;
20853 }
20854 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20855 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20856 {
20857 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20858 align_to = (align_to < 0
20859 ? 0
20860 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20861 else if (align_to < 0)
20862 align_to = window_box_left_offset (it->w, TEXT_AREA);
20863 width = max (0, (int)tem + align_to - it->current_x);
20864 zero_width_ok_p = 1;
20865 }
20866 else
20867 /* Nothing specified -> width defaults to canonical char width. */
20868 width = FRAME_COLUMN_WIDTH (it->f);
20869
20870 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20871 width = 1;
20872
20873 /* Compute height. */
20874 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20875 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20876 {
20877 height = (int)tem;
20878 zero_height_ok_p = 1;
20879 }
20880 else if (prop = Fplist_get (plist, QCrelative_height),
20881 NUMVAL (prop) > 0)
20882 height = FONT_HEIGHT (font) * NUMVAL (prop);
20883 else
20884 height = FONT_HEIGHT (font);
20885
20886 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20887 height = 1;
20888
20889 /* Compute percentage of height used for ascent. If
20890 `:ascent ASCENT' is present and valid, use that. Otherwise,
20891 derive the ascent from the font in use. */
20892 if (prop = Fplist_get (plist, QCascent),
20893 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20894 ascent = height * NUMVAL (prop) / 100.0;
20895 else if (!NILP (prop)
20896 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20897 ascent = min (max (0, (int)tem), height);
20898 else
20899 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20900
20901 if (width > 0 && it->line_wrap != TRUNCATE
20902 && it->current_x + width > it->last_visible_x)
20903 width = it->last_visible_x - it->current_x - 1;
20904
20905 if (width > 0 && height > 0 && it->glyph_row)
20906 {
20907 Lisp_Object object = it->stack[it->sp - 1].string;
20908 if (!STRINGP (object))
20909 object = it->w->buffer;
20910 append_stretch_glyph (it, object, width, height, ascent);
20911 }
20912
20913 it->pixel_width = width;
20914 it->ascent = it->phys_ascent = ascent;
20915 it->descent = it->phys_descent = height - it->ascent;
20916 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20917
20918 take_vertical_position_into_account (it);
20919 }
20920
20921 /* Calculate line-height and line-spacing properties.
20922 An integer value specifies explicit pixel value.
20923 A float value specifies relative value to current face height.
20924 A cons (float . face-name) specifies relative value to
20925 height of specified face font.
20926
20927 Returns height in pixels, or nil. */
20928
20929
20930 static Lisp_Object
20931 calc_line_height_property (it, val, font, boff, override)
20932 struct it *it;
20933 Lisp_Object val;
20934 struct font *font;
20935 int boff, override;
20936 {
20937 Lisp_Object face_name = Qnil;
20938 int ascent, descent, height;
20939
20940 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20941 return val;
20942
20943 if (CONSP (val))
20944 {
20945 face_name = XCAR (val);
20946 val = XCDR (val);
20947 if (!NUMBERP (val))
20948 val = make_number (1);
20949 if (NILP (face_name))
20950 {
20951 height = it->ascent + it->descent;
20952 goto scale;
20953 }
20954 }
20955
20956 if (NILP (face_name))
20957 {
20958 font = FRAME_FONT (it->f);
20959 boff = FRAME_BASELINE_OFFSET (it->f);
20960 }
20961 else if (EQ (face_name, Qt))
20962 {
20963 override = 0;
20964 }
20965 else
20966 {
20967 int face_id;
20968 struct face *face;
20969
20970 face_id = lookup_named_face (it->f, face_name, 0);
20971 if (face_id < 0)
20972 return make_number (-1);
20973
20974 face = FACE_FROM_ID (it->f, face_id);
20975 font = face->font;
20976 if (font == NULL)
20977 return make_number (-1);
20978 boff = font->baseline_offset;
20979 if (font->vertical_centering)
20980 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20981 }
20982
20983 ascent = FONT_BASE (font) + boff;
20984 descent = FONT_DESCENT (font) - boff;
20985
20986 if (override)
20987 {
20988 it->override_ascent = ascent;
20989 it->override_descent = descent;
20990 it->override_boff = boff;
20991 }
20992
20993 height = ascent + descent;
20994
20995 scale:
20996 if (FLOATP (val))
20997 height = (int)(XFLOAT_DATA (val) * height);
20998 else if (INTEGERP (val))
20999 height *= XINT (val);
21000
21001 return make_number (height);
21002 }
21003
21004
21005 /* RIF:
21006 Produce glyphs/get display metrics for the display element IT is
21007 loaded with. See the description of struct it in dispextern.h
21008 for an overview of struct it. */
21009
21010 void
21011 x_produce_glyphs (it)
21012 struct it *it;
21013 {
21014 int extra_line_spacing = it->extra_line_spacing;
21015
21016 it->glyph_not_available_p = 0;
21017
21018 if (it->what == IT_CHARACTER)
21019 {
21020 XChar2b char2b;
21021 struct font *font;
21022 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21023 struct font_metrics *pcm;
21024 int font_not_found_p;
21025 int boff; /* baseline offset */
21026 /* We may change it->multibyte_p upon unibyte<->multibyte
21027 conversion. So, save the current value now and restore it
21028 later.
21029
21030 Note: It seems that we don't have to record multibyte_p in
21031 struct glyph because the character code itself tells if or
21032 not the character is multibyte. Thus, in the future, we must
21033 consider eliminating the field `multibyte_p' in the struct
21034 glyph. */
21035 int saved_multibyte_p = it->multibyte_p;
21036
21037 /* Maybe translate single-byte characters to multibyte, or the
21038 other way. */
21039 it->char_to_display = it->c;
21040 if (!ASCII_BYTE_P (it->c)
21041 && ! it->multibyte_p)
21042 {
21043 if (SINGLE_BYTE_CHAR_P (it->c)
21044 && unibyte_display_via_language_environment)
21045 it->char_to_display = unibyte_char_to_multibyte (it->c);
21046 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
21047 {
21048 it->multibyte_p = 1;
21049 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21050 -1, Qnil);
21051 face = FACE_FROM_ID (it->f, it->face_id);
21052 }
21053 }
21054
21055 /* Get font to use. Encode IT->char_to_display. */
21056 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21057 &char2b, it->multibyte_p, 0);
21058 font = face->font;
21059
21060 /* When no suitable font found, use the default font. */
21061 font_not_found_p = font == NULL;
21062 if (font_not_found_p)
21063 {
21064 font = FRAME_FONT (it->f);
21065 boff = FRAME_BASELINE_OFFSET (it->f);
21066 }
21067 else
21068 {
21069 boff = font->baseline_offset;
21070 if (font->vertical_centering)
21071 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21072 }
21073
21074 if (it->char_to_display >= ' '
21075 && (!it->multibyte_p || it->char_to_display < 128))
21076 {
21077 /* Either unibyte or ASCII. */
21078 int stretched_p;
21079
21080 it->nglyphs = 1;
21081
21082 pcm = get_per_char_metric (it->f, font, &char2b);
21083
21084 if (it->override_ascent >= 0)
21085 {
21086 it->ascent = it->override_ascent;
21087 it->descent = it->override_descent;
21088 boff = it->override_boff;
21089 }
21090 else
21091 {
21092 it->ascent = FONT_BASE (font) + boff;
21093 it->descent = FONT_DESCENT (font) - boff;
21094 }
21095
21096 if (pcm)
21097 {
21098 it->phys_ascent = pcm->ascent + boff;
21099 it->phys_descent = pcm->descent - boff;
21100 it->pixel_width = pcm->width;
21101 }
21102 else
21103 {
21104 it->glyph_not_available_p = 1;
21105 it->phys_ascent = it->ascent;
21106 it->phys_descent = it->descent;
21107 it->pixel_width = FONT_WIDTH (font);
21108 }
21109
21110 if (it->constrain_row_ascent_descent_p)
21111 {
21112 if (it->descent > it->max_descent)
21113 {
21114 it->ascent += it->descent - it->max_descent;
21115 it->descent = it->max_descent;
21116 }
21117 if (it->ascent > it->max_ascent)
21118 {
21119 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21120 it->ascent = it->max_ascent;
21121 }
21122 it->phys_ascent = min (it->phys_ascent, it->ascent);
21123 it->phys_descent = min (it->phys_descent, it->descent);
21124 extra_line_spacing = 0;
21125 }
21126
21127 /* If this is a space inside a region of text with
21128 `space-width' property, change its width. */
21129 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21130 if (stretched_p)
21131 it->pixel_width *= XFLOATINT (it->space_width);
21132
21133 /* If face has a box, add the box thickness to the character
21134 height. If character has a box line to the left and/or
21135 right, add the box line width to the character's width. */
21136 if (face->box != FACE_NO_BOX)
21137 {
21138 int thick = face->box_line_width;
21139
21140 if (thick > 0)
21141 {
21142 it->ascent += thick;
21143 it->descent += thick;
21144 }
21145 else
21146 thick = -thick;
21147
21148 if (it->start_of_box_run_p)
21149 it->pixel_width += thick;
21150 if (it->end_of_box_run_p)
21151 it->pixel_width += thick;
21152 }
21153
21154 /* If face has an overline, add the height of the overline
21155 (1 pixel) and a 1 pixel margin to the character height. */
21156 if (face->overline_p)
21157 it->ascent += overline_margin;
21158
21159 if (it->constrain_row_ascent_descent_p)
21160 {
21161 if (it->ascent > it->max_ascent)
21162 it->ascent = it->max_ascent;
21163 if (it->descent > it->max_descent)
21164 it->descent = it->max_descent;
21165 }
21166
21167 take_vertical_position_into_account (it);
21168
21169 /* If we have to actually produce glyphs, do it. */
21170 if (it->glyph_row)
21171 {
21172 if (stretched_p)
21173 {
21174 /* Translate a space with a `space-width' property
21175 into a stretch glyph. */
21176 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21177 / FONT_HEIGHT (font));
21178 append_stretch_glyph (it, it->object, it->pixel_width,
21179 it->ascent + it->descent, ascent);
21180 }
21181 else
21182 append_glyph (it);
21183
21184 /* If characters with lbearing or rbearing are displayed
21185 in this line, record that fact in a flag of the
21186 glyph row. This is used to optimize X output code. */
21187 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21188 it->glyph_row->contains_overlapping_glyphs_p = 1;
21189 }
21190 if (! stretched_p && it->pixel_width == 0)
21191 /* We assure that all visible glyphs have at least 1-pixel
21192 width. */
21193 it->pixel_width = 1;
21194 }
21195 else if (it->char_to_display == '\n')
21196 {
21197 /* A newline has no width but we need the height of the line.
21198 But if previous part of the line set a height, don't
21199 increase that height */
21200
21201 Lisp_Object height;
21202 Lisp_Object total_height = Qnil;
21203
21204 it->override_ascent = -1;
21205 it->pixel_width = 0;
21206 it->nglyphs = 0;
21207
21208 height = get_it_property(it, Qline_height);
21209 /* Split (line-height total-height) list */
21210 if (CONSP (height)
21211 && CONSP (XCDR (height))
21212 && NILP (XCDR (XCDR (height))))
21213 {
21214 total_height = XCAR (XCDR (height));
21215 height = XCAR (height);
21216 }
21217 height = calc_line_height_property(it, height, font, boff, 1);
21218
21219 if (it->override_ascent >= 0)
21220 {
21221 it->ascent = it->override_ascent;
21222 it->descent = it->override_descent;
21223 boff = it->override_boff;
21224 }
21225 else
21226 {
21227 it->ascent = FONT_BASE (font) + boff;
21228 it->descent = FONT_DESCENT (font) - boff;
21229 }
21230
21231 if (EQ (height, Qt))
21232 {
21233 if (it->descent > it->max_descent)
21234 {
21235 it->ascent += it->descent - it->max_descent;
21236 it->descent = it->max_descent;
21237 }
21238 if (it->ascent > it->max_ascent)
21239 {
21240 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21241 it->ascent = it->max_ascent;
21242 }
21243 it->phys_ascent = min (it->phys_ascent, it->ascent);
21244 it->phys_descent = min (it->phys_descent, it->descent);
21245 it->constrain_row_ascent_descent_p = 1;
21246 extra_line_spacing = 0;
21247 }
21248 else
21249 {
21250 Lisp_Object spacing;
21251
21252 it->phys_ascent = it->ascent;
21253 it->phys_descent = it->descent;
21254
21255 if ((it->max_ascent > 0 || it->max_descent > 0)
21256 && face->box != FACE_NO_BOX
21257 && face->box_line_width > 0)
21258 {
21259 it->ascent += face->box_line_width;
21260 it->descent += face->box_line_width;
21261 }
21262 if (!NILP (height)
21263 && XINT (height) > it->ascent + it->descent)
21264 it->ascent = XINT (height) - it->descent;
21265
21266 if (!NILP (total_height))
21267 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21268 else
21269 {
21270 spacing = get_it_property(it, Qline_spacing);
21271 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21272 }
21273 if (INTEGERP (spacing))
21274 {
21275 extra_line_spacing = XINT (spacing);
21276 if (!NILP (total_height))
21277 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21278 }
21279 }
21280 }
21281 else if (it->char_to_display == '\t')
21282 {
21283 int tab_width = it->tab_width * font->space_width;
21284 int x = it->current_x + it->continuation_lines_width;
21285 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21286
21287 /* If the distance from the current position to the next tab
21288 stop is less than a space character width, use the
21289 tab stop after that. */
21290 if (next_tab_x - x < font->space_width)
21291 next_tab_x += tab_width;
21292
21293 it->pixel_width = next_tab_x - x;
21294 it->nglyphs = 1;
21295 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21296 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21297
21298 if (it->glyph_row)
21299 {
21300 append_stretch_glyph (it, it->object, it->pixel_width,
21301 it->ascent + it->descent, it->ascent);
21302 }
21303 }
21304 else
21305 {
21306 /* A multi-byte character. Assume that the display width of the
21307 character is the width of the character multiplied by the
21308 width of the font. */
21309
21310 /* If we found a font, this font should give us the right
21311 metrics. If we didn't find a font, use the frame's
21312 default font and calculate the width of the character by
21313 multiplying the width of font by the width of the
21314 character. */
21315
21316 pcm = get_per_char_metric (it->f, font, &char2b);
21317
21318 if (font_not_found_p || !pcm)
21319 {
21320 int char_width = CHAR_WIDTH (it->char_to_display);
21321
21322 if (char_width == 0)
21323 /* This is a non spacing character. But, as we are
21324 going to display an empty box, the box must occupy
21325 at least one column. */
21326 char_width = 1;
21327 it->glyph_not_available_p = 1;
21328 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21329 it->phys_ascent = FONT_BASE (font) + boff;
21330 it->phys_descent = FONT_DESCENT (font) - boff;
21331 }
21332 else
21333 {
21334 it->pixel_width = pcm->width;
21335 it->phys_ascent = pcm->ascent + boff;
21336 it->phys_descent = pcm->descent - boff;
21337 if (it->glyph_row
21338 && (pcm->lbearing < 0
21339 || pcm->rbearing > pcm->width))
21340 it->glyph_row->contains_overlapping_glyphs_p = 1;
21341 }
21342 it->nglyphs = 1;
21343 it->ascent = FONT_BASE (font) + boff;
21344 it->descent = FONT_DESCENT (font) - boff;
21345 if (face->box != FACE_NO_BOX)
21346 {
21347 int thick = face->box_line_width;
21348
21349 if (thick > 0)
21350 {
21351 it->ascent += thick;
21352 it->descent += thick;
21353 }
21354 else
21355 thick = - thick;
21356
21357 if (it->start_of_box_run_p)
21358 it->pixel_width += thick;
21359 if (it->end_of_box_run_p)
21360 it->pixel_width += thick;
21361 }
21362
21363 /* If face has an overline, add the height of the overline
21364 (1 pixel) and a 1 pixel margin to the character height. */
21365 if (face->overline_p)
21366 it->ascent += overline_margin;
21367
21368 take_vertical_position_into_account (it);
21369
21370 if (it->ascent < 0)
21371 it->ascent = 0;
21372 if (it->descent < 0)
21373 it->descent = 0;
21374
21375 if (it->glyph_row)
21376 append_glyph (it);
21377 if (it->pixel_width == 0)
21378 /* We assure that all visible glyphs have at least 1-pixel
21379 width. */
21380 it->pixel_width = 1;
21381 }
21382 it->multibyte_p = saved_multibyte_p;
21383 }
21384 else if (it->what == IT_COMPOSITION)
21385 {
21386 /* Note: A composition is represented as one glyph in the
21387 glyph matrix. There are no padding glyphs.
21388
21389 Important is that pixel_width, ascent, and descent are the
21390 values of what is drawn by draw_glyphs (i.e. the values of
21391 the overall glyphs composed). */
21392 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21393 int boff; /* baseline offset */
21394 struct composition *cmp = composition_table[it->cmp_id];
21395 int glyph_len = cmp->glyph_len;
21396 struct font *font = face->font;
21397
21398 it->nglyphs = 1;
21399
21400 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21401 {
21402 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21403 font_prepare_composition (cmp, it->f);
21404 }
21405 else
21406 /* If we have not yet calculated pixel size data of glyphs of
21407 the composition for the current face font, calculate them
21408 now. Theoretically, we have to check all fonts for the
21409 glyphs, but that requires much time and memory space. So,
21410 here we check only the font of the first glyph. This leads
21411 to incorrect display, but it's very rare, and C-l (recenter)
21412 can correct the display anyway. */
21413 if (! cmp->font || cmp->font != font)
21414 {
21415 /* Ascent and descent of the font of the first character
21416 of this composition (adjusted by baseline offset).
21417 Ascent and descent of overall glyphs should not be less
21418 than them respectively. */
21419 int font_ascent, font_descent, font_height;
21420 /* Bounding box of the overall glyphs. */
21421 int leftmost, rightmost, lowest, highest;
21422 int lbearing, rbearing;
21423 int i, width, ascent, descent;
21424 int left_padded = 0, right_padded = 0;
21425 int face_id;
21426 int c;
21427 XChar2b char2b;
21428 struct font_metrics *pcm;
21429 int font_not_found_p;
21430 int pos;
21431
21432 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21433 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21434 break;
21435 if (glyph_len < cmp->glyph_len)
21436 right_padded = 1;
21437 for (i = 0; i < glyph_len; i++)
21438 {
21439 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21440 break;
21441 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21442 }
21443 if (i > 0)
21444 left_padded = 1;
21445
21446 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21447 : IT_CHARPOS (*it));
21448 /* When no suitable font found, use the default font. */
21449 font_not_found_p = font == NULL;
21450 if (font_not_found_p)
21451 {
21452 face = face->ascii_face;
21453 font = face->font;
21454 }
21455 boff = font->baseline_offset;
21456 if (font->vertical_centering)
21457 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21458 font_ascent = FONT_BASE (font) + boff;
21459 font_descent = FONT_DESCENT (font) - boff;
21460 font_height = FONT_HEIGHT (font);
21461
21462 cmp->font = (void *) font;
21463
21464 pcm = NULL;
21465 if (! font_not_found_p)
21466 {
21467 get_char_face_and_encoding (it->f, c, it->face_id,
21468 &char2b, it->multibyte_p, 0);
21469 pcm = get_per_char_metric (it->f, font, &char2b);
21470 }
21471
21472 /* Initialize the bounding box. */
21473 if (pcm)
21474 {
21475 width = pcm->width;
21476 ascent = pcm->ascent;
21477 descent = pcm->descent;
21478 lbearing = pcm->lbearing;
21479 rbearing = pcm->rbearing;
21480 }
21481 else
21482 {
21483 width = FONT_WIDTH (font);
21484 ascent = FONT_BASE (font);
21485 descent = FONT_DESCENT (font);
21486 lbearing = 0;
21487 rbearing = width;
21488 }
21489
21490 rightmost = width;
21491 leftmost = 0;
21492 lowest = - descent + boff;
21493 highest = ascent + boff;
21494
21495 if (! font_not_found_p
21496 && font->default_ascent
21497 && CHAR_TABLE_P (Vuse_default_ascent)
21498 && !NILP (Faref (Vuse_default_ascent,
21499 make_number (it->char_to_display))))
21500 highest = font->default_ascent + boff;
21501
21502 /* Draw the first glyph at the normal position. It may be
21503 shifted to right later if some other glyphs are drawn
21504 at the left. */
21505 cmp->offsets[i * 2] = 0;
21506 cmp->offsets[i * 2 + 1] = boff;
21507 cmp->lbearing = lbearing;
21508 cmp->rbearing = rbearing;
21509
21510 /* Set cmp->offsets for the remaining glyphs. */
21511 for (i++; i < glyph_len; i++)
21512 {
21513 int left, right, btm, top;
21514 int ch = COMPOSITION_GLYPH (cmp, i);
21515 int face_id;
21516 struct face *this_face;
21517 int this_boff;
21518
21519 if (ch == '\t')
21520 ch = ' ';
21521 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21522 this_face = FACE_FROM_ID (it->f, face_id);
21523 font = this_face->font;
21524
21525 if (font == NULL)
21526 pcm = NULL;
21527 else
21528 {
21529 this_boff = font->baseline_offset;
21530 if (font->vertical_centering)
21531 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21532 get_char_face_and_encoding (it->f, ch, face_id,
21533 &char2b, it->multibyte_p, 0);
21534 pcm = get_per_char_metric (it->f, font, &char2b);
21535 }
21536 if (! pcm)
21537 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21538 else
21539 {
21540 width = pcm->width;
21541 ascent = pcm->ascent;
21542 descent = pcm->descent;
21543 lbearing = pcm->lbearing;
21544 rbearing = pcm->rbearing;
21545 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21546 {
21547 /* Relative composition with or without
21548 alternate chars. */
21549 left = (leftmost + rightmost - width) / 2;
21550 btm = - descent + boff;
21551 if (font->relative_compose
21552 && (! CHAR_TABLE_P (Vignore_relative_composition)
21553 || NILP (Faref (Vignore_relative_composition,
21554 make_number (ch)))))
21555 {
21556
21557 if (- descent >= font->relative_compose)
21558 /* One extra pixel between two glyphs. */
21559 btm = highest + 1;
21560 else if (ascent <= 0)
21561 /* One extra pixel between two glyphs. */
21562 btm = lowest - 1 - ascent - descent;
21563 }
21564 }
21565 else
21566 {
21567 /* A composition rule is specified by an integer
21568 value that encodes global and new reference
21569 points (GREF and NREF). GREF and NREF are
21570 specified by numbers as below:
21571
21572 0---1---2 -- ascent
21573 | |
21574 | |
21575 | |
21576 9--10--11 -- center
21577 | |
21578 ---3---4---5--- baseline
21579 | |
21580 6---7---8 -- descent
21581 */
21582 int rule = COMPOSITION_RULE (cmp, i);
21583 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21584
21585 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21586 grefx = gref % 3, nrefx = nref % 3;
21587 grefy = gref / 3, nrefy = nref / 3;
21588 if (xoff)
21589 xoff = font_height * (xoff - 128) / 256;
21590 if (yoff)
21591 yoff = font_height * (yoff - 128) / 256;
21592
21593 left = (leftmost
21594 + grefx * (rightmost - leftmost) / 2
21595 - nrefx * width / 2
21596 + xoff);
21597
21598 btm = ((grefy == 0 ? highest
21599 : grefy == 1 ? 0
21600 : grefy == 2 ? lowest
21601 : (highest + lowest) / 2)
21602 - (nrefy == 0 ? ascent + descent
21603 : nrefy == 1 ? descent - boff
21604 : nrefy == 2 ? 0
21605 : (ascent + descent) / 2)
21606 + yoff);
21607 }
21608
21609 cmp->offsets[i * 2] = left;
21610 cmp->offsets[i * 2 + 1] = btm + descent;
21611
21612 /* Update the bounding box of the overall glyphs. */
21613 if (width > 0)
21614 {
21615 right = left + width;
21616 if (left < leftmost)
21617 leftmost = left;
21618 if (right > rightmost)
21619 rightmost = right;
21620 }
21621 top = btm + descent + ascent;
21622 if (top > highest)
21623 highest = top;
21624 if (btm < lowest)
21625 lowest = btm;
21626
21627 if (cmp->lbearing > left + lbearing)
21628 cmp->lbearing = left + lbearing;
21629 if (cmp->rbearing < left + rbearing)
21630 cmp->rbearing = left + rbearing;
21631 }
21632 }
21633
21634 /* If there are glyphs whose x-offsets are negative,
21635 shift all glyphs to the right and make all x-offsets
21636 non-negative. */
21637 if (leftmost < 0)
21638 {
21639 for (i = 0; i < cmp->glyph_len; i++)
21640 cmp->offsets[i * 2] -= leftmost;
21641 rightmost -= leftmost;
21642 cmp->lbearing -= leftmost;
21643 cmp->rbearing -= leftmost;
21644 }
21645
21646 if (left_padded && cmp->lbearing < 0)
21647 {
21648 for (i = 0; i < cmp->glyph_len; i++)
21649 cmp->offsets[i * 2] -= cmp->lbearing;
21650 rightmost -= cmp->lbearing;
21651 cmp->rbearing -= cmp->lbearing;
21652 cmp->lbearing = 0;
21653 }
21654 if (right_padded && rightmost < cmp->rbearing)
21655 {
21656 rightmost = cmp->rbearing;
21657 }
21658
21659 cmp->pixel_width = rightmost;
21660 cmp->ascent = highest;
21661 cmp->descent = - lowest;
21662 if (cmp->ascent < font_ascent)
21663 cmp->ascent = font_ascent;
21664 if (cmp->descent < font_descent)
21665 cmp->descent = font_descent;
21666 }
21667
21668 if (it->glyph_row
21669 && (cmp->lbearing < 0
21670 || cmp->rbearing > cmp->pixel_width))
21671 it->glyph_row->contains_overlapping_glyphs_p = 1;
21672
21673 it->pixel_width = cmp->pixel_width;
21674 it->ascent = it->phys_ascent = cmp->ascent;
21675 it->descent = it->phys_descent = cmp->descent;
21676 if (face->box != FACE_NO_BOX)
21677 {
21678 int thick = face->box_line_width;
21679
21680 if (thick > 0)
21681 {
21682 it->ascent += thick;
21683 it->descent += thick;
21684 }
21685 else
21686 thick = - thick;
21687
21688 if (it->start_of_box_run_p)
21689 it->pixel_width += thick;
21690 if (it->end_of_box_run_p)
21691 it->pixel_width += thick;
21692 }
21693
21694 /* If face has an overline, add the height of the overline
21695 (1 pixel) and a 1 pixel margin to the character height. */
21696 if (face->overline_p)
21697 it->ascent += overline_margin;
21698
21699 take_vertical_position_into_account (it);
21700 if (it->ascent < 0)
21701 it->ascent = 0;
21702 if (it->descent < 0)
21703 it->descent = 0;
21704
21705 if (it->glyph_row)
21706 append_composite_glyph (it);
21707 }
21708 else if (it->what == IT_IMAGE)
21709 produce_image_glyph (it);
21710 else if (it->what == IT_STRETCH)
21711 produce_stretch_glyph (it);
21712
21713 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21714 because this isn't true for images with `:ascent 100'. */
21715 xassert (it->ascent >= 0 && it->descent >= 0);
21716 if (it->area == TEXT_AREA)
21717 it->current_x += it->pixel_width;
21718
21719 if (extra_line_spacing > 0)
21720 {
21721 it->descent += extra_line_spacing;
21722 if (extra_line_spacing > it->max_extra_line_spacing)
21723 it->max_extra_line_spacing = extra_line_spacing;
21724 }
21725
21726 it->max_ascent = max (it->max_ascent, it->ascent);
21727 it->max_descent = max (it->max_descent, it->descent);
21728 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21729 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21730 }
21731
21732 /* EXPORT for RIF:
21733 Output LEN glyphs starting at START at the nominal cursor position.
21734 Advance the nominal cursor over the text. The global variable
21735 updated_window contains the window being updated, updated_row is
21736 the glyph row being updated, and updated_area is the area of that
21737 row being updated. */
21738
21739 void
21740 x_write_glyphs (start, len)
21741 struct glyph *start;
21742 int len;
21743 {
21744 int x, hpos;
21745
21746 xassert (updated_window && updated_row);
21747 BLOCK_INPUT;
21748
21749 /* Write glyphs. */
21750
21751 hpos = start - updated_row->glyphs[updated_area];
21752 x = draw_glyphs (updated_window, output_cursor.x,
21753 updated_row, updated_area,
21754 hpos, hpos + len,
21755 DRAW_NORMAL_TEXT, 0);
21756
21757 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21758 if (updated_area == TEXT_AREA
21759 && updated_window->phys_cursor_on_p
21760 && updated_window->phys_cursor.vpos == output_cursor.vpos
21761 && updated_window->phys_cursor.hpos >= hpos
21762 && updated_window->phys_cursor.hpos < hpos + len)
21763 updated_window->phys_cursor_on_p = 0;
21764
21765 UNBLOCK_INPUT;
21766
21767 /* Advance the output cursor. */
21768 output_cursor.hpos += len;
21769 output_cursor.x = x;
21770 }
21771
21772
21773 /* EXPORT for RIF:
21774 Insert LEN glyphs from START at the nominal cursor position. */
21775
21776 void
21777 x_insert_glyphs (start, len)
21778 struct glyph *start;
21779 int len;
21780 {
21781 struct frame *f;
21782 struct window *w;
21783 int line_height, shift_by_width, shifted_region_width;
21784 struct glyph_row *row;
21785 struct glyph *glyph;
21786 int frame_x, frame_y;
21787 EMACS_INT hpos;
21788
21789 xassert (updated_window && updated_row);
21790 BLOCK_INPUT;
21791 w = updated_window;
21792 f = XFRAME (WINDOW_FRAME (w));
21793
21794 /* Get the height of the line we are in. */
21795 row = updated_row;
21796 line_height = row->height;
21797
21798 /* Get the width of the glyphs to insert. */
21799 shift_by_width = 0;
21800 for (glyph = start; glyph < start + len; ++glyph)
21801 shift_by_width += glyph->pixel_width;
21802
21803 /* Get the width of the region to shift right. */
21804 shifted_region_width = (window_box_width (w, updated_area)
21805 - output_cursor.x
21806 - shift_by_width);
21807
21808 /* Shift right. */
21809 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21810 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21811
21812 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21813 line_height, shift_by_width);
21814
21815 /* Write the glyphs. */
21816 hpos = start - row->glyphs[updated_area];
21817 draw_glyphs (w, output_cursor.x, row, updated_area,
21818 hpos, hpos + len,
21819 DRAW_NORMAL_TEXT, 0);
21820
21821 /* Advance the output cursor. */
21822 output_cursor.hpos += len;
21823 output_cursor.x += shift_by_width;
21824 UNBLOCK_INPUT;
21825 }
21826
21827
21828 /* EXPORT for RIF:
21829 Erase the current text line from the nominal cursor position
21830 (inclusive) to pixel column TO_X (exclusive). The idea is that
21831 everything from TO_X onward is already erased.
21832
21833 TO_X is a pixel position relative to updated_area of
21834 updated_window. TO_X == -1 means clear to the end of this area. */
21835
21836 void
21837 x_clear_end_of_line (to_x)
21838 int to_x;
21839 {
21840 struct frame *f;
21841 struct window *w = updated_window;
21842 int max_x, min_y, max_y;
21843 int from_x, from_y, to_y;
21844
21845 xassert (updated_window && updated_row);
21846 f = XFRAME (w->frame);
21847
21848 if (updated_row->full_width_p)
21849 max_x = WINDOW_TOTAL_WIDTH (w);
21850 else
21851 max_x = window_box_width (w, updated_area);
21852 max_y = window_text_bottom_y (w);
21853
21854 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21855 of window. For TO_X > 0, truncate to end of drawing area. */
21856 if (to_x == 0)
21857 return;
21858 else if (to_x < 0)
21859 to_x = max_x;
21860 else
21861 to_x = min (to_x, max_x);
21862
21863 to_y = min (max_y, output_cursor.y + updated_row->height);
21864
21865 /* Notice if the cursor will be cleared by this operation. */
21866 if (!updated_row->full_width_p)
21867 notice_overwritten_cursor (w, updated_area,
21868 output_cursor.x, -1,
21869 updated_row->y,
21870 MATRIX_ROW_BOTTOM_Y (updated_row));
21871
21872 from_x = output_cursor.x;
21873
21874 /* Translate to frame coordinates. */
21875 if (updated_row->full_width_p)
21876 {
21877 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21878 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21879 }
21880 else
21881 {
21882 int area_left = window_box_left (w, updated_area);
21883 from_x += area_left;
21884 to_x += area_left;
21885 }
21886
21887 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21888 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21889 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21890
21891 /* Prevent inadvertently clearing to end of the X window. */
21892 if (to_x > from_x && to_y > from_y)
21893 {
21894 BLOCK_INPUT;
21895 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21896 to_x - from_x, to_y - from_y);
21897 UNBLOCK_INPUT;
21898 }
21899 }
21900
21901 #endif /* HAVE_WINDOW_SYSTEM */
21902
21903
21904 \f
21905 /***********************************************************************
21906 Cursor types
21907 ***********************************************************************/
21908
21909 /* Value is the internal representation of the specified cursor type
21910 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21911 of the bar cursor. */
21912
21913 static enum text_cursor_kinds
21914 get_specified_cursor_type (arg, width)
21915 Lisp_Object arg;
21916 int *width;
21917 {
21918 enum text_cursor_kinds type;
21919
21920 if (NILP (arg))
21921 return NO_CURSOR;
21922
21923 if (EQ (arg, Qbox))
21924 return FILLED_BOX_CURSOR;
21925
21926 if (EQ (arg, Qhollow))
21927 return HOLLOW_BOX_CURSOR;
21928
21929 if (EQ (arg, Qbar))
21930 {
21931 *width = 2;
21932 return BAR_CURSOR;
21933 }
21934
21935 if (CONSP (arg)
21936 && EQ (XCAR (arg), Qbar)
21937 && INTEGERP (XCDR (arg))
21938 && XINT (XCDR (arg)) >= 0)
21939 {
21940 *width = XINT (XCDR (arg));
21941 return BAR_CURSOR;
21942 }
21943
21944 if (EQ (arg, Qhbar))
21945 {
21946 *width = 2;
21947 return HBAR_CURSOR;
21948 }
21949
21950 if (CONSP (arg)
21951 && EQ (XCAR (arg), Qhbar)
21952 && INTEGERP (XCDR (arg))
21953 && XINT (XCDR (arg)) >= 0)
21954 {
21955 *width = XINT (XCDR (arg));
21956 return HBAR_CURSOR;
21957 }
21958
21959 /* Treat anything unknown as "hollow box cursor".
21960 It was bad to signal an error; people have trouble fixing
21961 .Xdefaults with Emacs, when it has something bad in it. */
21962 type = HOLLOW_BOX_CURSOR;
21963
21964 return type;
21965 }
21966
21967 /* Set the default cursor types for specified frame. */
21968 void
21969 set_frame_cursor_types (f, arg)
21970 struct frame *f;
21971 Lisp_Object arg;
21972 {
21973 int width;
21974 Lisp_Object tem;
21975
21976 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21977 FRAME_CURSOR_WIDTH (f) = width;
21978
21979 /* By default, set up the blink-off state depending on the on-state. */
21980
21981 tem = Fassoc (arg, Vblink_cursor_alist);
21982 if (!NILP (tem))
21983 {
21984 FRAME_BLINK_OFF_CURSOR (f)
21985 = get_specified_cursor_type (XCDR (tem), &width);
21986 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21987 }
21988 else
21989 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21990 }
21991
21992
21993 /* Return the cursor we want to be displayed in window W. Return
21994 width of bar/hbar cursor through WIDTH arg. Return with
21995 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21996 (i.e. if the `system caret' should track this cursor).
21997
21998 In a mini-buffer window, we want the cursor only to appear if we
21999 are reading input from this window. For the selected window, we
22000 want the cursor type given by the frame parameter or buffer local
22001 setting of cursor-type. If explicitly marked off, draw no cursor.
22002 In all other cases, we want a hollow box cursor. */
22003
22004 static enum text_cursor_kinds
22005 get_window_cursor_type (w, glyph, width, active_cursor)
22006 struct window *w;
22007 struct glyph *glyph;
22008 int *width;
22009 int *active_cursor;
22010 {
22011 struct frame *f = XFRAME (w->frame);
22012 struct buffer *b = XBUFFER (w->buffer);
22013 int cursor_type = DEFAULT_CURSOR;
22014 Lisp_Object alt_cursor;
22015 int non_selected = 0;
22016
22017 *active_cursor = 1;
22018
22019 /* Echo area */
22020 if (cursor_in_echo_area
22021 && FRAME_HAS_MINIBUF_P (f)
22022 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22023 {
22024 if (w == XWINDOW (echo_area_window))
22025 {
22026 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22027 {
22028 *width = FRAME_CURSOR_WIDTH (f);
22029 return FRAME_DESIRED_CURSOR (f);
22030 }
22031 else
22032 return get_specified_cursor_type (b->cursor_type, width);
22033 }
22034
22035 *active_cursor = 0;
22036 non_selected = 1;
22037 }
22038
22039 /* Detect a nonselected window or nonselected frame. */
22040 else if (w != XWINDOW (f->selected_window)
22041 #ifdef HAVE_WINDOW_SYSTEM
22042 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22043 #endif
22044 )
22045 {
22046 *active_cursor = 0;
22047
22048 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22049 return NO_CURSOR;
22050
22051 non_selected = 1;
22052 }
22053
22054 /* Never display a cursor in a window in which cursor-type is nil. */
22055 if (NILP (b->cursor_type))
22056 return NO_CURSOR;
22057
22058 /* Get the normal cursor type for this window. */
22059 if (EQ (b->cursor_type, Qt))
22060 {
22061 cursor_type = FRAME_DESIRED_CURSOR (f);
22062 *width = FRAME_CURSOR_WIDTH (f);
22063 }
22064 else
22065 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22066
22067 /* Use cursor-in-non-selected-windows instead
22068 for non-selected window or frame. */
22069 if (non_selected)
22070 {
22071 alt_cursor = b->cursor_in_non_selected_windows;
22072 if (!EQ (Qt, alt_cursor))
22073 return get_specified_cursor_type (alt_cursor, width);
22074 /* t means modify the normal cursor type. */
22075 if (cursor_type == FILLED_BOX_CURSOR)
22076 cursor_type = HOLLOW_BOX_CURSOR;
22077 else if (cursor_type == BAR_CURSOR && *width > 1)
22078 --*width;
22079 return cursor_type;
22080 }
22081
22082 /* Use normal cursor if not blinked off. */
22083 if (!w->cursor_off_p)
22084 {
22085 #ifdef HAVE_WINDOW_SYSTEM
22086 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22087 {
22088 if (cursor_type == FILLED_BOX_CURSOR)
22089 {
22090 /* Using a block cursor on large images can be very annoying.
22091 So use a hollow cursor for "large" images.
22092 If image is not transparent (no mask), also use hollow cursor. */
22093 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22094 if (img != NULL && IMAGEP (img->spec))
22095 {
22096 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22097 where N = size of default frame font size.
22098 This should cover most of the "tiny" icons people may use. */
22099 if (!img->mask
22100 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22101 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22102 cursor_type = HOLLOW_BOX_CURSOR;
22103 }
22104 }
22105 else if (cursor_type != NO_CURSOR)
22106 {
22107 /* Display current only supports BOX and HOLLOW cursors for images.
22108 So for now, unconditionally use a HOLLOW cursor when cursor is
22109 not a solid box cursor. */
22110 cursor_type = HOLLOW_BOX_CURSOR;
22111 }
22112 }
22113 #endif
22114 return cursor_type;
22115 }
22116
22117 /* Cursor is blinked off, so determine how to "toggle" it. */
22118
22119 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22120 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22121 return get_specified_cursor_type (XCDR (alt_cursor), width);
22122
22123 /* Then see if frame has specified a specific blink off cursor type. */
22124 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22125 {
22126 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22127 return FRAME_BLINK_OFF_CURSOR (f);
22128 }
22129
22130 #if 0
22131 /* Some people liked having a permanently visible blinking cursor,
22132 while others had very strong opinions against it. So it was
22133 decided to remove it. KFS 2003-09-03 */
22134
22135 /* Finally perform built-in cursor blinking:
22136 filled box <-> hollow box
22137 wide [h]bar <-> narrow [h]bar
22138 narrow [h]bar <-> no cursor
22139 other type <-> no cursor */
22140
22141 if (cursor_type == FILLED_BOX_CURSOR)
22142 return HOLLOW_BOX_CURSOR;
22143
22144 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22145 {
22146 *width = 1;
22147 return cursor_type;
22148 }
22149 #endif
22150
22151 return NO_CURSOR;
22152 }
22153
22154
22155 #ifdef HAVE_WINDOW_SYSTEM
22156
22157 /* Notice when the text cursor of window W has been completely
22158 overwritten by a drawing operation that outputs glyphs in AREA
22159 starting at X0 and ending at X1 in the line starting at Y0 and
22160 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22161 the rest of the line after X0 has been written. Y coordinates
22162 are window-relative. */
22163
22164 static void
22165 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22166 struct window *w;
22167 enum glyph_row_area area;
22168 int x0, y0, x1, y1;
22169 {
22170 int cx0, cx1, cy0, cy1;
22171 struct glyph_row *row;
22172
22173 if (!w->phys_cursor_on_p)
22174 return;
22175 if (area != TEXT_AREA)
22176 return;
22177
22178 if (w->phys_cursor.vpos < 0
22179 || w->phys_cursor.vpos >= w->current_matrix->nrows
22180 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22181 !(row->enabled_p && row->displays_text_p)))
22182 return;
22183
22184 if (row->cursor_in_fringe_p)
22185 {
22186 row->cursor_in_fringe_p = 0;
22187 draw_fringe_bitmap (w, row, 0);
22188 w->phys_cursor_on_p = 0;
22189 return;
22190 }
22191
22192 cx0 = w->phys_cursor.x;
22193 cx1 = cx0 + w->phys_cursor_width;
22194 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22195 return;
22196
22197 /* The cursor image will be completely removed from the
22198 screen if the output area intersects the cursor area in
22199 y-direction. When we draw in [y0 y1[, and some part of
22200 the cursor is at y < y0, that part must have been drawn
22201 before. When scrolling, the cursor is erased before
22202 actually scrolling, so we don't come here. When not
22203 scrolling, the rows above the old cursor row must have
22204 changed, and in this case these rows must have written
22205 over the cursor image.
22206
22207 Likewise if part of the cursor is below y1, with the
22208 exception of the cursor being in the first blank row at
22209 the buffer and window end because update_text_area
22210 doesn't draw that row. (Except when it does, but
22211 that's handled in update_text_area.) */
22212
22213 cy0 = w->phys_cursor.y;
22214 cy1 = cy0 + w->phys_cursor_height;
22215 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22216 return;
22217
22218 w->phys_cursor_on_p = 0;
22219 }
22220
22221 #endif /* HAVE_WINDOW_SYSTEM */
22222
22223 \f
22224 /************************************************************************
22225 Mouse Face
22226 ************************************************************************/
22227
22228 #ifdef HAVE_WINDOW_SYSTEM
22229
22230 /* EXPORT for RIF:
22231 Fix the display of area AREA of overlapping row ROW in window W
22232 with respect to the overlapping part OVERLAPS. */
22233
22234 void
22235 x_fix_overlapping_area (w, row, area, overlaps)
22236 struct window *w;
22237 struct glyph_row *row;
22238 enum glyph_row_area area;
22239 int overlaps;
22240 {
22241 int i, x;
22242
22243 BLOCK_INPUT;
22244
22245 x = 0;
22246 for (i = 0; i < row->used[area];)
22247 {
22248 if (row->glyphs[area][i].overlaps_vertically_p)
22249 {
22250 int start = i, start_x = x;
22251
22252 do
22253 {
22254 x += row->glyphs[area][i].pixel_width;
22255 ++i;
22256 }
22257 while (i < row->used[area]
22258 && row->glyphs[area][i].overlaps_vertically_p);
22259
22260 draw_glyphs (w, start_x, row, area,
22261 start, i,
22262 DRAW_NORMAL_TEXT, overlaps);
22263 }
22264 else
22265 {
22266 x += row->glyphs[area][i].pixel_width;
22267 ++i;
22268 }
22269 }
22270
22271 UNBLOCK_INPUT;
22272 }
22273
22274
22275 /* EXPORT:
22276 Draw the cursor glyph of window W in glyph row ROW. See the
22277 comment of draw_glyphs for the meaning of HL. */
22278
22279 void
22280 draw_phys_cursor_glyph (w, row, hl)
22281 struct window *w;
22282 struct glyph_row *row;
22283 enum draw_glyphs_face hl;
22284 {
22285 /* If cursor hpos is out of bounds, don't draw garbage. This can
22286 happen in mini-buffer windows when switching between echo area
22287 glyphs and mini-buffer. */
22288 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22289 {
22290 int on_p = w->phys_cursor_on_p;
22291 int x1;
22292 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22293 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22294 hl, 0);
22295 w->phys_cursor_on_p = on_p;
22296
22297 if (hl == DRAW_CURSOR)
22298 w->phys_cursor_width = x1 - w->phys_cursor.x;
22299 /* When we erase the cursor, and ROW is overlapped by other
22300 rows, make sure that these overlapping parts of other rows
22301 are redrawn. */
22302 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22303 {
22304 w->phys_cursor_width = x1 - w->phys_cursor.x;
22305
22306 if (row > w->current_matrix->rows
22307 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22308 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22309 OVERLAPS_ERASED_CURSOR);
22310
22311 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22312 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22313 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22314 OVERLAPS_ERASED_CURSOR);
22315 }
22316 }
22317 }
22318
22319
22320 /* EXPORT:
22321 Erase the image of a cursor of window W from the screen. */
22322
22323 void
22324 erase_phys_cursor (w)
22325 struct window *w;
22326 {
22327 struct frame *f = XFRAME (w->frame);
22328 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22329 int hpos = w->phys_cursor.hpos;
22330 int vpos = w->phys_cursor.vpos;
22331 int mouse_face_here_p = 0;
22332 struct glyph_matrix *active_glyphs = w->current_matrix;
22333 struct glyph_row *cursor_row;
22334 struct glyph *cursor_glyph;
22335 enum draw_glyphs_face hl;
22336
22337 /* No cursor displayed or row invalidated => nothing to do on the
22338 screen. */
22339 if (w->phys_cursor_type == NO_CURSOR)
22340 goto mark_cursor_off;
22341
22342 /* VPOS >= active_glyphs->nrows means that window has been resized.
22343 Don't bother to erase the cursor. */
22344 if (vpos >= active_glyphs->nrows)
22345 goto mark_cursor_off;
22346
22347 /* If row containing cursor is marked invalid, there is nothing we
22348 can do. */
22349 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22350 if (!cursor_row->enabled_p)
22351 goto mark_cursor_off;
22352
22353 /* If line spacing is > 0, old cursor may only be partially visible in
22354 window after split-window. So adjust visible height. */
22355 cursor_row->visible_height = min (cursor_row->visible_height,
22356 window_text_bottom_y (w) - cursor_row->y);
22357
22358 /* If row is completely invisible, don't attempt to delete a cursor which
22359 isn't there. This can happen if cursor is at top of a window, and
22360 we switch to a buffer with a header line in that window. */
22361 if (cursor_row->visible_height <= 0)
22362 goto mark_cursor_off;
22363
22364 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22365 if (cursor_row->cursor_in_fringe_p)
22366 {
22367 cursor_row->cursor_in_fringe_p = 0;
22368 draw_fringe_bitmap (w, cursor_row, 0);
22369 goto mark_cursor_off;
22370 }
22371
22372 /* This can happen when the new row is shorter than the old one.
22373 In this case, either draw_glyphs or clear_end_of_line
22374 should have cleared the cursor. Note that we wouldn't be
22375 able to erase the cursor in this case because we don't have a
22376 cursor glyph at hand. */
22377 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22378 goto mark_cursor_off;
22379
22380 /* If the cursor is in the mouse face area, redisplay that when
22381 we clear the cursor. */
22382 if (! NILP (dpyinfo->mouse_face_window)
22383 && w == XWINDOW (dpyinfo->mouse_face_window)
22384 && (vpos > dpyinfo->mouse_face_beg_row
22385 || (vpos == dpyinfo->mouse_face_beg_row
22386 && hpos >= dpyinfo->mouse_face_beg_col))
22387 && (vpos < dpyinfo->mouse_face_end_row
22388 || (vpos == dpyinfo->mouse_face_end_row
22389 && hpos < dpyinfo->mouse_face_end_col))
22390 /* Don't redraw the cursor's spot in mouse face if it is at the
22391 end of a line (on a newline). The cursor appears there, but
22392 mouse highlighting does not. */
22393 && cursor_row->used[TEXT_AREA] > hpos)
22394 mouse_face_here_p = 1;
22395
22396 /* Maybe clear the display under the cursor. */
22397 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22398 {
22399 int x, y, left_x;
22400 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22401 int width;
22402
22403 cursor_glyph = get_phys_cursor_glyph (w);
22404 if (cursor_glyph == NULL)
22405 goto mark_cursor_off;
22406
22407 width = cursor_glyph->pixel_width;
22408 left_x = window_box_left_offset (w, TEXT_AREA);
22409 x = w->phys_cursor.x;
22410 if (x < left_x)
22411 width -= left_x - x;
22412 width = min (width, window_box_width (w, TEXT_AREA) - x);
22413 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22414 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22415
22416 if (width > 0)
22417 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22418 }
22419
22420 /* Erase the cursor by redrawing the character underneath it. */
22421 if (mouse_face_here_p)
22422 hl = DRAW_MOUSE_FACE;
22423 else
22424 hl = DRAW_NORMAL_TEXT;
22425 draw_phys_cursor_glyph (w, cursor_row, hl);
22426
22427 mark_cursor_off:
22428 w->phys_cursor_on_p = 0;
22429 w->phys_cursor_type = NO_CURSOR;
22430 }
22431
22432
22433 /* EXPORT:
22434 Display or clear cursor of window W. If ON is zero, clear the
22435 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22436 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22437
22438 void
22439 display_and_set_cursor (w, on, hpos, vpos, x, y)
22440 struct window *w;
22441 int on, hpos, vpos, x, y;
22442 {
22443 struct frame *f = XFRAME (w->frame);
22444 int new_cursor_type;
22445 int new_cursor_width;
22446 int active_cursor;
22447 struct glyph_row *glyph_row;
22448 struct glyph *glyph;
22449
22450 /* This is pointless on invisible frames, and dangerous on garbaged
22451 windows and frames; in the latter case, the frame or window may
22452 be in the midst of changing its size, and x and y may be off the
22453 window. */
22454 if (! FRAME_VISIBLE_P (f)
22455 || FRAME_GARBAGED_P (f)
22456 || vpos >= w->current_matrix->nrows
22457 || hpos >= w->current_matrix->matrix_w)
22458 return;
22459
22460 /* If cursor is off and we want it off, return quickly. */
22461 if (!on && !w->phys_cursor_on_p)
22462 return;
22463
22464 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22465 /* If cursor row is not enabled, we don't really know where to
22466 display the cursor. */
22467 if (!glyph_row->enabled_p)
22468 {
22469 w->phys_cursor_on_p = 0;
22470 return;
22471 }
22472
22473 glyph = NULL;
22474 if (!glyph_row->exact_window_width_line_p
22475 || hpos < glyph_row->used[TEXT_AREA])
22476 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22477
22478 xassert (interrupt_input_blocked);
22479
22480 /* Set new_cursor_type to the cursor we want to be displayed. */
22481 new_cursor_type = get_window_cursor_type (w, glyph,
22482 &new_cursor_width, &active_cursor);
22483
22484 /* If cursor is currently being shown and we don't want it to be or
22485 it is in the wrong place, or the cursor type is not what we want,
22486 erase it. */
22487 if (w->phys_cursor_on_p
22488 && (!on
22489 || w->phys_cursor.x != x
22490 || w->phys_cursor.y != y
22491 || new_cursor_type != w->phys_cursor_type
22492 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22493 && new_cursor_width != w->phys_cursor_width)))
22494 erase_phys_cursor (w);
22495
22496 /* Don't check phys_cursor_on_p here because that flag is only set
22497 to zero in some cases where we know that the cursor has been
22498 completely erased, to avoid the extra work of erasing the cursor
22499 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22500 still not be visible, or it has only been partly erased. */
22501 if (on)
22502 {
22503 w->phys_cursor_ascent = glyph_row->ascent;
22504 w->phys_cursor_height = glyph_row->height;
22505
22506 /* Set phys_cursor_.* before x_draw_.* is called because some
22507 of them may need the information. */
22508 w->phys_cursor.x = x;
22509 w->phys_cursor.y = glyph_row->y;
22510 w->phys_cursor.hpos = hpos;
22511 w->phys_cursor.vpos = vpos;
22512 }
22513
22514 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22515 new_cursor_type, new_cursor_width,
22516 on, active_cursor);
22517 }
22518
22519
22520 /* Switch the display of W's cursor on or off, according to the value
22521 of ON. */
22522
22523 static void
22524 update_window_cursor (w, on)
22525 struct window *w;
22526 int on;
22527 {
22528 /* Don't update cursor in windows whose frame is in the process
22529 of being deleted. */
22530 if (w->current_matrix)
22531 {
22532 BLOCK_INPUT;
22533 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22534 w->phys_cursor.x, w->phys_cursor.y);
22535 UNBLOCK_INPUT;
22536 }
22537 }
22538
22539
22540 /* Call update_window_cursor with parameter ON_P on all leaf windows
22541 in the window tree rooted at W. */
22542
22543 static void
22544 update_cursor_in_window_tree (w, on_p)
22545 struct window *w;
22546 int on_p;
22547 {
22548 while (w)
22549 {
22550 if (!NILP (w->hchild))
22551 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22552 else if (!NILP (w->vchild))
22553 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22554 else
22555 update_window_cursor (w, on_p);
22556
22557 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22558 }
22559 }
22560
22561
22562 /* EXPORT:
22563 Display the cursor on window W, or clear it, according to ON_P.
22564 Don't change the cursor's position. */
22565
22566 void
22567 x_update_cursor (f, on_p)
22568 struct frame *f;
22569 int on_p;
22570 {
22571 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22572 }
22573
22574
22575 /* EXPORT:
22576 Clear the cursor of window W to background color, and mark the
22577 cursor as not shown. This is used when the text where the cursor
22578 is is about to be rewritten. */
22579
22580 void
22581 x_clear_cursor (w)
22582 struct window *w;
22583 {
22584 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22585 update_window_cursor (w, 0);
22586 }
22587
22588
22589 /* EXPORT:
22590 Display the active region described by mouse_face_* according to DRAW. */
22591
22592 void
22593 show_mouse_face (dpyinfo, draw)
22594 Display_Info *dpyinfo;
22595 enum draw_glyphs_face draw;
22596 {
22597 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22598 struct frame *f = XFRAME (WINDOW_FRAME (w));
22599
22600 if (/* If window is in the process of being destroyed, don't bother
22601 to do anything. */
22602 w->current_matrix != NULL
22603 /* Don't update mouse highlight if hidden */
22604 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22605 /* Recognize when we are called to operate on rows that don't exist
22606 anymore. This can happen when a window is split. */
22607 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22608 {
22609 int phys_cursor_on_p = w->phys_cursor_on_p;
22610 struct glyph_row *row, *first, *last;
22611
22612 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22613 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22614
22615 for (row = first; row <= last && row->enabled_p; ++row)
22616 {
22617 int start_hpos, end_hpos, start_x;
22618
22619 /* For all but the first row, the highlight starts at column 0. */
22620 if (row == first)
22621 {
22622 start_hpos = dpyinfo->mouse_face_beg_col;
22623 start_x = dpyinfo->mouse_face_beg_x;
22624 }
22625 else
22626 {
22627 start_hpos = 0;
22628 start_x = 0;
22629 }
22630
22631 if (row == last)
22632 end_hpos = dpyinfo->mouse_face_end_col;
22633 else
22634 {
22635 end_hpos = row->used[TEXT_AREA];
22636 if (draw == DRAW_NORMAL_TEXT)
22637 row->fill_line_p = 1; /* Clear to end of line */
22638 }
22639
22640 if (end_hpos > start_hpos)
22641 {
22642 draw_glyphs (w, start_x, row, TEXT_AREA,
22643 start_hpos, end_hpos,
22644 draw, 0);
22645
22646 row->mouse_face_p
22647 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22648 }
22649 }
22650
22651 /* When we've written over the cursor, arrange for it to
22652 be displayed again. */
22653 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22654 {
22655 BLOCK_INPUT;
22656 display_and_set_cursor (w, 1,
22657 w->phys_cursor.hpos, w->phys_cursor.vpos,
22658 w->phys_cursor.x, w->phys_cursor.y);
22659 UNBLOCK_INPUT;
22660 }
22661 }
22662
22663 /* Change the mouse cursor. */
22664 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22665 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22666 else if (draw == DRAW_MOUSE_FACE)
22667 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22668 else
22669 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22670 }
22671
22672 /* EXPORT:
22673 Clear out the mouse-highlighted active region.
22674 Redraw it un-highlighted first. Value is non-zero if mouse
22675 face was actually drawn unhighlighted. */
22676
22677 int
22678 clear_mouse_face (dpyinfo)
22679 Display_Info *dpyinfo;
22680 {
22681 int cleared = 0;
22682
22683 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22684 {
22685 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22686 cleared = 1;
22687 }
22688
22689 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22690 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22691 dpyinfo->mouse_face_window = Qnil;
22692 dpyinfo->mouse_face_overlay = Qnil;
22693 return cleared;
22694 }
22695
22696
22697 /* EXPORT:
22698 Non-zero if physical cursor of window W is within mouse face. */
22699
22700 int
22701 cursor_in_mouse_face_p (w)
22702 struct window *w;
22703 {
22704 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22705 int in_mouse_face = 0;
22706
22707 if (WINDOWP (dpyinfo->mouse_face_window)
22708 && XWINDOW (dpyinfo->mouse_face_window) == w)
22709 {
22710 int hpos = w->phys_cursor.hpos;
22711 int vpos = w->phys_cursor.vpos;
22712
22713 if (vpos >= dpyinfo->mouse_face_beg_row
22714 && vpos <= dpyinfo->mouse_face_end_row
22715 && (vpos > dpyinfo->mouse_face_beg_row
22716 || hpos >= dpyinfo->mouse_face_beg_col)
22717 && (vpos < dpyinfo->mouse_face_end_row
22718 || hpos < dpyinfo->mouse_face_end_col
22719 || dpyinfo->mouse_face_past_end))
22720 in_mouse_face = 1;
22721 }
22722
22723 return in_mouse_face;
22724 }
22725
22726
22727
22728 \f
22729 /* Find the glyph matrix position of buffer position CHARPOS in window
22730 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22731 current glyphs must be up to date. If CHARPOS is above window
22732 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22733 of last line in W. In the row containing CHARPOS, stop before glyphs
22734 having STOP as object. */
22735
22736 #if 1 /* This is a version of fast_find_position that's more correct
22737 in the presence of hscrolling, for example. I didn't install
22738 it right away because the problem fixed is minor, it failed
22739 in 20.x as well, and I think it's too risky to install
22740 so near the release of 21.1. 2001-09-25 gerd. */
22741
22742 #ifndef HAVE_CARBON
22743 static
22744 #endif
22745 int
22746 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22747 struct window *w;
22748 EMACS_INT charpos;
22749 int *hpos, *vpos, *x, *y;
22750 Lisp_Object stop;
22751 {
22752 struct glyph_row *row, *first;
22753 struct glyph *glyph, *end;
22754 int past_end = 0;
22755
22756 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22757 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22758 {
22759 *x = first->x;
22760 *y = first->y;
22761 *hpos = 0;
22762 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22763 return 1;
22764 }
22765
22766 row = row_containing_pos (w, charpos, first, NULL, 0);
22767 if (row == NULL)
22768 {
22769 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22770 past_end = 1;
22771 }
22772
22773 /* If whole rows or last part of a row came from a display overlay,
22774 row_containing_pos will skip over such rows because their end pos
22775 equals the start pos of the overlay or interval.
22776
22777 Move back if we have a STOP object and previous row's
22778 end glyph came from STOP. */
22779 if (!NILP (stop))
22780 {
22781 struct glyph_row *prev;
22782 while ((prev = row - 1, prev >= first)
22783 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22784 && prev->used[TEXT_AREA] > 0)
22785 {
22786 struct glyph *beg = prev->glyphs[TEXT_AREA];
22787 glyph = beg + prev->used[TEXT_AREA];
22788 while (--glyph >= beg
22789 && INTEGERP (glyph->object));
22790 if (glyph < beg
22791 || !EQ (stop, glyph->object))
22792 break;
22793 row = prev;
22794 }
22795 }
22796
22797 *x = row->x;
22798 *y = row->y;
22799 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22800
22801 glyph = row->glyphs[TEXT_AREA];
22802 end = glyph + row->used[TEXT_AREA];
22803
22804 /* Skip over glyphs not having an object at the start of the row.
22805 These are special glyphs like truncation marks on terminal
22806 frames. */
22807 if (row->displays_text_p)
22808 while (glyph < end
22809 && INTEGERP (glyph->object)
22810 && !EQ (stop, glyph->object)
22811 && glyph->charpos < 0)
22812 {
22813 *x += glyph->pixel_width;
22814 ++glyph;
22815 }
22816
22817 while (glyph < end
22818 && !INTEGERP (glyph->object)
22819 && !EQ (stop, glyph->object)
22820 && (!BUFFERP (glyph->object)
22821 || glyph->charpos < charpos))
22822 {
22823 *x += glyph->pixel_width;
22824 ++glyph;
22825 }
22826
22827 *hpos = glyph - row->glyphs[TEXT_AREA];
22828 return !past_end;
22829 }
22830
22831 #else /* not 1 */
22832
22833 static int
22834 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22835 struct window *w;
22836 EMACS_INT pos;
22837 int *hpos, *vpos, *x, *y;
22838 Lisp_Object stop;
22839 {
22840 int i;
22841 int lastcol;
22842 int maybe_next_line_p = 0;
22843 int line_start_position;
22844 int yb = window_text_bottom_y (w);
22845 struct glyph_row *row, *best_row;
22846 int row_vpos, best_row_vpos;
22847 int current_x;
22848
22849 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22850 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22851
22852 while (row->y < yb)
22853 {
22854 if (row->used[TEXT_AREA])
22855 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22856 else
22857 line_start_position = 0;
22858
22859 if (line_start_position > pos)
22860 break;
22861 /* If the position sought is the end of the buffer,
22862 don't include the blank lines at the bottom of the window. */
22863 else if (line_start_position == pos
22864 && pos == BUF_ZV (XBUFFER (w->buffer)))
22865 {
22866 maybe_next_line_p = 1;
22867 break;
22868 }
22869 else if (line_start_position > 0)
22870 {
22871 best_row = row;
22872 best_row_vpos = row_vpos;
22873 }
22874
22875 if (row->y + row->height >= yb)
22876 break;
22877
22878 ++row;
22879 ++row_vpos;
22880 }
22881
22882 /* Find the right column within BEST_ROW. */
22883 lastcol = 0;
22884 current_x = best_row->x;
22885 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22886 {
22887 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22888 int charpos = glyph->charpos;
22889
22890 if (BUFFERP (glyph->object))
22891 {
22892 if (charpos == pos)
22893 {
22894 *hpos = i;
22895 *vpos = best_row_vpos;
22896 *x = current_x;
22897 *y = best_row->y;
22898 return 1;
22899 }
22900 else if (charpos > pos)
22901 break;
22902 }
22903 else if (EQ (glyph->object, stop))
22904 break;
22905
22906 if (charpos > 0)
22907 lastcol = i;
22908 current_x += glyph->pixel_width;
22909 }
22910
22911 /* If we're looking for the end of the buffer,
22912 and we didn't find it in the line we scanned,
22913 use the start of the following line. */
22914 if (maybe_next_line_p)
22915 {
22916 ++best_row;
22917 ++best_row_vpos;
22918 lastcol = 0;
22919 current_x = best_row->x;
22920 }
22921
22922 *vpos = best_row_vpos;
22923 *hpos = lastcol + 1;
22924 *x = current_x;
22925 *y = best_row->y;
22926 return 0;
22927 }
22928
22929 #endif /* not 1 */
22930
22931
22932 /* Find the position of the glyph for position POS in OBJECT in
22933 window W's current matrix, and return in *X, *Y the pixel
22934 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22935
22936 RIGHT_P non-zero means return the position of the right edge of the
22937 glyph, RIGHT_P zero means return the left edge position.
22938
22939 If no glyph for POS exists in the matrix, return the position of
22940 the glyph with the next smaller position that is in the matrix, if
22941 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22942 exists in the matrix, return the position of the glyph with the
22943 next larger position in OBJECT.
22944
22945 Value is non-zero if a glyph was found. */
22946
22947 static int
22948 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22949 struct window *w;
22950 EMACS_INT pos;
22951 Lisp_Object object;
22952 int *hpos, *vpos, *x, *y;
22953 int right_p;
22954 {
22955 int yb = window_text_bottom_y (w);
22956 struct glyph_row *r;
22957 struct glyph *best_glyph = NULL;
22958 struct glyph_row *best_row = NULL;
22959 int best_x = 0;
22960
22961 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22962 r->enabled_p && r->y < yb;
22963 ++r)
22964 {
22965 struct glyph *g = r->glyphs[TEXT_AREA];
22966 struct glyph *e = g + r->used[TEXT_AREA];
22967 int gx;
22968
22969 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22970 if (EQ (g->object, object))
22971 {
22972 if (g->charpos == pos)
22973 {
22974 best_glyph = g;
22975 best_x = gx;
22976 best_row = r;
22977 goto found;
22978 }
22979 else if (best_glyph == NULL
22980 || ((eabs (g->charpos - pos)
22981 < eabs (best_glyph->charpos - pos))
22982 && (right_p
22983 ? g->charpos < pos
22984 : g->charpos > pos)))
22985 {
22986 best_glyph = g;
22987 best_x = gx;
22988 best_row = r;
22989 }
22990 }
22991 }
22992
22993 found:
22994
22995 if (best_glyph)
22996 {
22997 *x = best_x;
22998 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22999
23000 if (right_p)
23001 {
23002 *x += best_glyph->pixel_width;
23003 ++*hpos;
23004 }
23005
23006 *y = best_row->y;
23007 *vpos = best_row - w->current_matrix->rows;
23008 }
23009
23010 return best_glyph != NULL;
23011 }
23012
23013
23014 /* See if position X, Y is within a hot-spot of an image. */
23015
23016 static int
23017 on_hot_spot_p (hot_spot, x, y)
23018 Lisp_Object hot_spot;
23019 int x, y;
23020 {
23021 if (!CONSP (hot_spot))
23022 return 0;
23023
23024 if (EQ (XCAR (hot_spot), Qrect))
23025 {
23026 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23027 Lisp_Object rect = XCDR (hot_spot);
23028 Lisp_Object tem;
23029 if (!CONSP (rect))
23030 return 0;
23031 if (!CONSP (XCAR (rect)))
23032 return 0;
23033 if (!CONSP (XCDR (rect)))
23034 return 0;
23035 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23036 return 0;
23037 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23038 return 0;
23039 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23040 return 0;
23041 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23042 return 0;
23043 return 1;
23044 }
23045 else if (EQ (XCAR (hot_spot), Qcircle))
23046 {
23047 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23048 Lisp_Object circ = XCDR (hot_spot);
23049 Lisp_Object lr, lx0, ly0;
23050 if (CONSP (circ)
23051 && CONSP (XCAR (circ))
23052 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23053 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23054 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23055 {
23056 double r = XFLOATINT (lr);
23057 double dx = XINT (lx0) - x;
23058 double dy = XINT (ly0) - y;
23059 return (dx * dx + dy * dy <= r * r);
23060 }
23061 }
23062 else if (EQ (XCAR (hot_spot), Qpoly))
23063 {
23064 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23065 if (VECTORP (XCDR (hot_spot)))
23066 {
23067 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23068 Lisp_Object *poly = v->contents;
23069 int n = v->size;
23070 int i;
23071 int inside = 0;
23072 Lisp_Object lx, ly;
23073 int x0, y0;
23074
23075 /* Need an even number of coordinates, and at least 3 edges. */
23076 if (n < 6 || n & 1)
23077 return 0;
23078
23079 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23080 If count is odd, we are inside polygon. Pixels on edges
23081 may or may not be included depending on actual geometry of the
23082 polygon. */
23083 if ((lx = poly[n-2], !INTEGERP (lx))
23084 || (ly = poly[n-1], !INTEGERP (lx)))
23085 return 0;
23086 x0 = XINT (lx), y0 = XINT (ly);
23087 for (i = 0; i < n; i += 2)
23088 {
23089 int x1 = x0, y1 = y0;
23090 if ((lx = poly[i], !INTEGERP (lx))
23091 || (ly = poly[i+1], !INTEGERP (ly)))
23092 return 0;
23093 x0 = XINT (lx), y0 = XINT (ly);
23094
23095 /* Does this segment cross the X line? */
23096 if (x0 >= x)
23097 {
23098 if (x1 >= x)
23099 continue;
23100 }
23101 else if (x1 < x)
23102 continue;
23103 if (y > y0 && y > y1)
23104 continue;
23105 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23106 inside = !inside;
23107 }
23108 return inside;
23109 }
23110 }
23111 return 0;
23112 }
23113
23114 Lisp_Object
23115 find_hot_spot (map, x, y)
23116 Lisp_Object map;
23117 int x, y;
23118 {
23119 while (CONSP (map))
23120 {
23121 if (CONSP (XCAR (map))
23122 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23123 return XCAR (map);
23124 map = XCDR (map);
23125 }
23126
23127 return Qnil;
23128 }
23129
23130 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23131 3, 3, 0,
23132 doc: /* Lookup in image map MAP coordinates X and Y.
23133 An image map is an alist where each element has the format (AREA ID PLIST).
23134 An AREA is specified as either a rectangle, a circle, or a polygon:
23135 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23136 pixel coordinates of the upper left and bottom right corners.
23137 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23138 and the radius of the circle; r may be a float or integer.
23139 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23140 vector describes one corner in the polygon.
23141 Returns the alist element for the first matching AREA in MAP. */)
23142 (map, x, y)
23143 Lisp_Object map;
23144 Lisp_Object x, y;
23145 {
23146 if (NILP (map))
23147 return Qnil;
23148
23149 CHECK_NUMBER (x);
23150 CHECK_NUMBER (y);
23151
23152 return find_hot_spot (map, XINT (x), XINT (y));
23153 }
23154
23155
23156 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23157 static void
23158 define_frame_cursor1 (f, cursor, pointer)
23159 struct frame *f;
23160 Cursor cursor;
23161 Lisp_Object pointer;
23162 {
23163 /* Do not change cursor shape while dragging mouse. */
23164 if (!NILP (do_mouse_tracking))
23165 return;
23166
23167 if (!NILP (pointer))
23168 {
23169 if (EQ (pointer, Qarrow))
23170 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23171 else if (EQ (pointer, Qhand))
23172 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23173 else if (EQ (pointer, Qtext))
23174 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23175 else if (EQ (pointer, intern ("hdrag")))
23176 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23177 #ifdef HAVE_X_WINDOWS
23178 else if (EQ (pointer, intern ("vdrag")))
23179 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23180 #endif
23181 else if (EQ (pointer, intern ("hourglass")))
23182 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23183 else if (EQ (pointer, Qmodeline))
23184 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23185 else
23186 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23187 }
23188
23189 if (cursor != No_Cursor)
23190 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23191 }
23192
23193 /* Take proper action when mouse has moved to the mode or header line
23194 or marginal area AREA of window W, x-position X and y-position Y.
23195 X is relative to the start of the text display area of W, so the
23196 width of bitmap areas and scroll bars must be subtracted to get a
23197 position relative to the start of the mode line. */
23198
23199 static void
23200 note_mode_line_or_margin_highlight (window, x, y, area)
23201 Lisp_Object window;
23202 int x, y;
23203 enum window_part area;
23204 {
23205 struct window *w = XWINDOW (window);
23206 struct frame *f = XFRAME (w->frame);
23207 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23208 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23209 Lisp_Object pointer = Qnil;
23210 int charpos, dx, dy, width, height;
23211 Lisp_Object string, object = Qnil;
23212 Lisp_Object pos, help;
23213
23214 Lisp_Object mouse_face;
23215 int original_x_pixel = x;
23216 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23217 struct glyph_row *row;
23218
23219 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23220 {
23221 int x0;
23222 struct glyph *end;
23223
23224 string = mode_line_string (w, area, &x, &y, &charpos,
23225 &object, &dx, &dy, &width, &height);
23226
23227 row = (area == ON_MODE_LINE
23228 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23229 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23230
23231 /* Find glyph */
23232 if (row->mode_line_p && row->enabled_p)
23233 {
23234 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23235 end = glyph + row->used[TEXT_AREA];
23236
23237 for (x0 = original_x_pixel;
23238 glyph < end && x0 >= glyph->pixel_width;
23239 ++glyph)
23240 x0 -= glyph->pixel_width;
23241
23242 if (glyph >= end)
23243 glyph = NULL;
23244 }
23245 }
23246 else
23247 {
23248 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23249 string = marginal_area_string (w, area, &x, &y, &charpos,
23250 &object, &dx, &dy, &width, &height);
23251 }
23252
23253 help = Qnil;
23254
23255 if (IMAGEP (object))
23256 {
23257 Lisp_Object image_map, hotspot;
23258 if ((image_map = Fplist_get (XCDR (object), QCmap),
23259 !NILP (image_map))
23260 && (hotspot = find_hot_spot (image_map, dx, dy),
23261 CONSP (hotspot))
23262 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23263 {
23264 Lisp_Object area_id, plist;
23265
23266 area_id = XCAR (hotspot);
23267 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23268 If so, we could look for mouse-enter, mouse-leave
23269 properties in PLIST (and do something...). */
23270 hotspot = XCDR (hotspot);
23271 if (CONSP (hotspot)
23272 && (plist = XCAR (hotspot), CONSP (plist)))
23273 {
23274 pointer = Fplist_get (plist, Qpointer);
23275 if (NILP (pointer))
23276 pointer = Qhand;
23277 help = Fplist_get (plist, Qhelp_echo);
23278 if (!NILP (help))
23279 {
23280 help_echo_string = help;
23281 /* Is this correct? ++kfs */
23282 XSETWINDOW (help_echo_window, w);
23283 help_echo_object = w->buffer;
23284 help_echo_pos = charpos;
23285 }
23286 }
23287 }
23288 if (NILP (pointer))
23289 pointer = Fplist_get (XCDR (object), QCpointer);
23290 }
23291
23292 if (STRINGP (string))
23293 {
23294 pos = make_number (charpos);
23295 /* If we're on a string with `help-echo' text property, arrange
23296 for the help to be displayed. This is done by setting the
23297 global variable help_echo_string to the help string. */
23298 if (NILP (help))
23299 {
23300 help = Fget_text_property (pos, Qhelp_echo, string);
23301 if (!NILP (help))
23302 {
23303 help_echo_string = help;
23304 XSETWINDOW (help_echo_window, w);
23305 help_echo_object = string;
23306 help_echo_pos = charpos;
23307 }
23308 }
23309
23310 if (NILP (pointer))
23311 pointer = Fget_text_property (pos, Qpointer, string);
23312
23313 /* Change the mouse pointer according to what is under X/Y. */
23314 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23315 {
23316 Lisp_Object map;
23317 map = Fget_text_property (pos, Qlocal_map, string);
23318 if (!KEYMAPP (map))
23319 map = Fget_text_property (pos, Qkeymap, string);
23320 if (!KEYMAPP (map))
23321 cursor = dpyinfo->vertical_scroll_bar_cursor;
23322 }
23323
23324 /* Change the mouse face according to what is under X/Y. */
23325 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23326 if (!NILP (mouse_face)
23327 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23328 && glyph)
23329 {
23330 Lisp_Object b, e;
23331
23332 struct glyph * tmp_glyph;
23333
23334 int gpos;
23335 int gseq_length;
23336 int total_pixel_width;
23337 EMACS_INT ignore;
23338
23339 int vpos, hpos;
23340
23341 b = Fprevious_single_property_change (make_number (charpos + 1),
23342 Qmouse_face, string, Qnil);
23343 if (NILP (b))
23344 b = make_number (0);
23345
23346 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23347 if (NILP (e))
23348 e = make_number (SCHARS (string));
23349
23350 /* Calculate the position(glyph position: GPOS) of GLYPH in
23351 displayed string. GPOS is different from CHARPOS.
23352
23353 CHARPOS is the position of glyph in internal string
23354 object. A mode line string format has structures which
23355 is converted to a flatten by emacs lisp interpreter.
23356 The internal string is an element of the structures.
23357 The displayed string is the flatten string. */
23358 gpos = 0;
23359 if (glyph > row_start_glyph)
23360 {
23361 tmp_glyph = glyph - 1;
23362 while (tmp_glyph >= row_start_glyph
23363 && tmp_glyph->charpos >= XINT (b)
23364 && EQ (tmp_glyph->object, glyph->object))
23365 {
23366 tmp_glyph--;
23367 gpos++;
23368 }
23369 }
23370
23371 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23372 displayed string holding GLYPH.
23373
23374 GSEQ_LENGTH is different from SCHARS (STRING).
23375 SCHARS (STRING) returns the length of the internal string. */
23376 for (tmp_glyph = glyph, gseq_length = gpos;
23377 tmp_glyph->charpos < XINT (e);
23378 tmp_glyph++, gseq_length++)
23379 {
23380 if (!EQ (tmp_glyph->object, glyph->object))
23381 break;
23382 }
23383
23384 total_pixel_width = 0;
23385 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23386 total_pixel_width += tmp_glyph->pixel_width;
23387
23388 /* Pre calculation of re-rendering position */
23389 vpos = (x - gpos);
23390 hpos = (area == ON_MODE_LINE
23391 ? (w->current_matrix)->nrows - 1
23392 : 0);
23393
23394 /* If the re-rendering position is included in the last
23395 re-rendering area, we should do nothing. */
23396 if ( EQ (window, dpyinfo->mouse_face_window)
23397 && dpyinfo->mouse_face_beg_col <= vpos
23398 && vpos < dpyinfo->mouse_face_end_col
23399 && dpyinfo->mouse_face_beg_row == hpos )
23400 return;
23401
23402 if (clear_mouse_face (dpyinfo))
23403 cursor = No_Cursor;
23404
23405 dpyinfo->mouse_face_beg_col = vpos;
23406 dpyinfo->mouse_face_beg_row = hpos;
23407
23408 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23409 dpyinfo->mouse_face_beg_y = 0;
23410
23411 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23412 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23413
23414 dpyinfo->mouse_face_end_x = 0;
23415 dpyinfo->mouse_face_end_y = 0;
23416
23417 dpyinfo->mouse_face_past_end = 0;
23418 dpyinfo->mouse_face_window = window;
23419
23420 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23421 charpos,
23422 0, 0, 0, &ignore,
23423 glyph->face_id, 1);
23424 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23425
23426 if (NILP (pointer))
23427 pointer = Qhand;
23428 }
23429 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23430 clear_mouse_face (dpyinfo);
23431 }
23432 define_frame_cursor1 (f, cursor, pointer);
23433 }
23434
23435
23436 /* EXPORT:
23437 Take proper action when the mouse has moved to position X, Y on
23438 frame F as regards highlighting characters that have mouse-face
23439 properties. Also de-highlighting chars where the mouse was before.
23440 X and Y can be negative or out of range. */
23441
23442 void
23443 note_mouse_highlight (f, x, y)
23444 struct frame *f;
23445 int x, y;
23446 {
23447 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23448 enum window_part part;
23449 Lisp_Object window;
23450 struct window *w;
23451 Cursor cursor = No_Cursor;
23452 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23453 struct buffer *b;
23454
23455 /* When a menu is active, don't highlight because this looks odd. */
23456 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23457 if (popup_activated ())
23458 return;
23459 #endif
23460
23461 if (NILP (Vmouse_highlight)
23462 || !f->glyphs_initialized_p)
23463 return;
23464
23465 dpyinfo->mouse_face_mouse_x = x;
23466 dpyinfo->mouse_face_mouse_y = y;
23467 dpyinfo->mouse_face_mouse_frame = f;
23468
23469 if (dpyinfo->mouse_face_defer)
23470 return;
23471
23472 if (gc_in_progress)
23473 {
23474 dpyinfo->mouse_face_deferred_gc = 1;
23475 return;
23476 }
23477
23478 /* Which window is that in? */
23479 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23480
23481 /* If we were displaying active text in another window, clear that.
23482 Also clear if we move out of text area in same window. */
23483 if (! EQ (window, dpyinfo->mouse_face_window)
23484 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23485 && !NILP (dpyinfo->mouse_face_window)))
23486 clear_mouse_face (dpyinfo);
23487
23488 /* Not on a window -> return. */
23489 if (!WINDOWP (window))
23490 return;
23491
23492 /* Reset help_echo_string. It will get recomputed below. */
23493 help_echo_string = Qnil;
23494
23495 /* Convert to window-relative pixel coordinates. */
23496 w = XWINDOW (window);
23497 frame_to_window_pixel_xy (w, &x, &y);
23498
23499 /* Handle tool-bar window differently since it doesn't display a
23500 buffer. */
23501 if (EQ (window, f->tool_bar_window))
23502 {
23503 note_tool_bar_highlight (f, x, y);
23504 return;
23505 }
23506
23507 /* Mouse is on the mode, header line or margin? */
23508 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23509 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23510 {
23511 note_mode_line_or_margin_highlight (window, x, y, part);
23512 return;
23513 }
23514
23515 if (part == ON_VERTICAL_BORDER)
23516 {
23517 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23518 help_echo_string = build_string ("drag-mouse-1: resize");
23519 }
23520 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23521 || part == ON_SCROLL_BAR)
23522 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23523 else
23524 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23525
23526 /* Are we in a window whose display is up to date?
23527 And verify the buffer's text has not changed. */
23528 b = XBUFFER (w->buffer);
23529 if (part == ON_TEXT
23530 && EQ (w->window_end_valid, w->buffer)
23531 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23532 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23533 {
23534 int hpos, vpos, pos, i, dx, dy, area;
23535 struct glyph *glyph;
23536 Lisp_Object object;
23537 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23538 Lisp_Object *overlay_vec = NULL;
23539 int noverlays;
23540 struct buffer *obuf;
23541 int obegv, ozv, same_region;
23542
23543 /* Find the glyph under X/Y. */
23544 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23545
23546 /* Look for :pointer property on image. */
23547 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23548 {
23549 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23550 if (img != NULL && IMAGEP (img->spec))
23551 {
23552 Lisp_Object image_map, hotspot;
23553 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23554 !NILP (image_map))
23555 && (hotspot = find_hot_spot (image_map,
23556 glyph->slice.x + dx,
23557 glyph->slice.y + dy),
23558 CONSP (hotspot))
23559 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23560 {
23561 Lisp_Object area_id, plist;
23562
23563 area_id = XCAR (hotspot);
23564 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23565 If so, we could look for mouse-enter, mouse-leave
23566 properties in PLIST (and do something...). */
23567 hotspot = XCDR (hotspot);
23568 if (CONSP (hotspot)
23569 && (plist = XCAR (hotspot), CONSP (plist)))
23570 {
23571 pointer = Fplist_get (plist, Qpointer);
23572 if (NILP (pointer))
23573 pointer = Qhand;
23574 help_echo_string = Fplist_get (plist, Qhelp_echo);
23575 if (!NILP (help_echo_string))
23576 {
23577 help_echo_window = window;
23578 help_echo_object = glyph->object;
23579 help_echo_pos = glyph->charpos;
23580 }
23581 }
23582 }
23583 if (NILP (pointer))
23584 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23585 }
23586 }
23587
23588 /* Clear mouse face if X/Y not over text. */
23589 if (glyph == NULL
23590 || area != TEXT_AREA
23591 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23592 {
23593 if (clear_mouse_face (dpyinfo))
23594 cursor = No_Cursor;
23595 if (NILP (pointer))
23596 {
23597 if (area != TEXT_AREA)
23598 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23599 else
23600 pointer = Vvoid_text_area_pointer;
23601 }
23602 goto set_cursor;
23603 }
23604
23605 pos = glyph->charpos;
23606 object = glyph->object;
23607 if (!STRINGP (object) && !BUFFERP (object))
23608 goto set_cursor;
23609
23610 /* If we get an out-of-range value, return now; avoid an error. */
23611 if (BUFFERP (object) && pos > BUF_Z (b))
23612 goto set_cursor;
23613
23614 /* Make the window's buffer temporarily current for
23615 overlays_at and compute_char_face. */
23616 obuf = current_buffer;
23617 current_buffer = b;
23618 obegv = BEGV;
23619 ozv = ZV;
23620 BEGV = BEG;
23621 ZV = Z;
23622
23623 /* Is this char mouse-active or does it have help-echo? */
23624 position = make_number (pos);
23625
23626 if (BUFFERP (object))
23627 {
23628 /* Put all the overlays we want in a vector in overlay_vec. */
23629 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23630 /* Sort overlays into increasing priority order. */
23631 noverlays = sort_overlays (overlay_vec, noverlays, w);
23632 }
23633 else
23634 noverlays = 0;
23635
23636 same_region = (EQ (window, dpyinfo->mouse_face_window)
23637 && vpos >= dpyinfo->mouse_face_beg_row
23638 && vpos <= dpyinfo->mouse_face_end_row
23639 && (vpos > dpyinfo->mouse_face_beg_row
23640 || hpos >= dpyinfo->mouse_face_beg_col)
23641 && (vpos < dpyinfo->mouse_face_end_row
23642 || hpos < dpyinfo->mouse_face_end_col
23643 || dpyinfo->mouse_face_past_end));
23644
23645 if (same_region)
23646 cursor = No_Cursor;
23647
23648 /* Check mouse-face highlighting. */
23649 if (! same_region
23650 /* If there exists an overlay with mouse-face overlapping
23651 the one we are currently highlighting, we have to
23652 check if we enter the overlapping overlay, and then
23653 highlight only that. */
23654 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23655 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23656 {
23657 /* Find the highest priority overlay that has a mouse-face
23658 property. */
23659 overlay = Qnil;
23660 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23661 {
23662 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23663 if (!NILP (mouse_face))
23664 overlay = overlay_vec[i];
23665 }
23666
23667 /* If we're actually highlighting the same overlay as
23668 before, there's no need to do that again. */
23669 if (!NILP (overlay)
23670 && EQ (overlay, dpyinfo->mouse_face_overlay))
23671 goto check_help_echo;
23672
23673 dpyinfo->mouse_face_overlay = overlay;
23674
23675 /* Clear the display of the old active region, if any. */
23676 if (clear_mouse_face (dpyinfo))
23677 cursor = No_Cursor;
23678
23679 /* If no overlay applies, get a text property. */
23680 if (NILP (overlay))
23681 mouse_face = Fget_text_property (position, Qmouse_face, object);
23682
23683 /* Handle the overlay case. */
23684 if (!NILP (overlay))
23685 {
23686 /* Find the range of text around this char that
23687 should be active. */
23688 Lisp_Object before, after;
23689 EMACS_INT ignore;
23690
23691 before = Foverlay_start (overlay);
23692 after = Foverlay_end (overlay);
23693 /* Record this as the current active region. */
23694 fast_find_position (w, XFASTINT (before),
23695 &dpyinfo->mouse_face_beg_col,
23696 &dpyinfo->mouse_face_beg_row,
23697 &dpyinfo->mouse_face_beg_x,
23698 &dpyinfo->mouse_face_beg_y, Qnil);
23699
23700 dpyinfo->mouse_face_past_end
23701 = !fast_find_position (w, XFASTINT (after),
23702 &dpyinfo->mouse_face_end_col,
23703 &dpyinfo->mouse_face_end_row,
23704 &dpyinfo->mouse_face_end_x,
23705 &dpyinfo->mouse_face_end_y, Qnil);
23706 dpyinfo->mouse_face_window = window;
23707
23708 dpyinfo->mouse_face_face_id
23709 = face_at_buffer_position (w, pos, 0, 0,
23710 &ignore, pos + 1,
23711 !dpyinfo->mouse_face_hidden);
23712
23713 /* Display it as active. */
23714 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23715 cursor = No_Cursor;
23716 }
23717 /* Handle the text property case. */
23718 else if (!NILP (mouse_face) && BUFFERP (object))
23719 {
23720 /* Find the range of text around this char that
23721 should be active. */
23722 Lisp_Object before, after, beginning, end;
23723 EMACS_INT ignore;
23724
23725 beginning = Fmarker_position (w->start);
23726 end = make_number (BUF_Z (XBUFFER (object))
23727 - XFASTINT (w->window_end_pos));
23728 before
23729 = Fprevious_single_property_change (make_number (pos + 1),
23730 Qmouse_face,
23731 object, beginning);
23732 after
23733 = Fnext_single_property_change (position, Qmouse_face,
23734 object, end);
23735
23736 /* Record this as the current active region. */
23737 fast_find_position (w, XFASTINT (before),
23738 &dpyinfo->mouse_face_beg_col,
23739 &dpyinfo->mouse_face_beg_row,
23740 &dpyinfo->mouse_face_beg_x,
23741 &dpyinfo->mouse_face_beg_y, Qnil);
23742 dpyinfo->mouse_face_past_end
23743 = !fast_find_position (w, XFASTINT (after),
23744 &dpyinfo->mouse_face_end_col,
23745 &dpyinfo->mouse_face_end_row,
23746 &dpyinfo->mouse_face_end_x,
23747 &dpyinfo->mouse_face_end_y, Qnil);
23748 dpyinfo->mouse_face_window = window;
23749
23750 if (BUFFERP (object))
23751 dpyinfo->mouse_face_face_id
23752 = face_at_buffer_position (w, pos, 0, 0,
23753 &ignore, pos + 1,
23754 !dpyinfo->mouse_face_hidden);
23755
23756 /* Display it as active. */
23757 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23758 cursor = No_Cursor;
23759 }
23760 else if (!NILP (mouse_face) && STRINGP (object))
23761 {
23762 Lisp_Object b, e;
23763 EMACS_INT ignore;
23764
23765 b = Fprevious_single_property_change (make_number (pos + 1),
23766 Qmouse_face,
23767 object, Qnil);
23768 e = Fnext_single_property_change (position, Qmouse_face,
23769 object, Qnil);
23770 if (NILP (b))
23771 b = make_number (0);
23772 if (NILP (e))
23773 e = make_number (SCHARS (object) - 1);
23774
23775 fast_find_string_pos (w, XINT (b), object,
23776 &dpyinfo->mouse_face_beg_col,
23777 &dpyinfo->mouse_face_beg_row,
23778 &dpyinfo->mouse_face_beg_x,
23779 &dpyinfo->mouse_face_beg_y, 0);
23780 fast_find_string_pos (w, XINT (e), object,
23781 &dpyinfo->mouse_face_end_col,
23782 &dpyinfo->mouse_face_end_row,
23783 &dpyinfo->mouse_face_end_x,
23784 &dpyinfo->mouse_face_end_y, 1);
23785 dpyinfo->mouse_face_past_end = 0;
23786 dpyinfo->mouse_face_window = window;
23787 dpyinfo->mouse_face_face_id
23788 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23789 glyph->face_id, 1);
23790 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23791 cursor = No_Cursor;
23792 }
23793 else if (STRINGP (object) && NILP (mouse_face))
23794 {
23795 /* A string which doesn't have mouse-face, but
23796 the text ``under'' it might have. */
23797 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23798 int start = MATRIX_ROW_START_CHARPOS (r);
23799
23800 pos = string_buffer_position (w, object, start);
23801 if (pos > 0)
23802 mouse_face = get_char_property_and_overlay (make_number (pos),
23803 Qmouse_face,
23804 w->buffer,
23805 &overlay);
23806 if (!NILP (mouse_face) && !NILP (overlay))
23807 {
23808 Lisp_Object before = Foverlay_start (overlay);
23809 Lisp_Object after = Foverlay_end (overlay);
23810 EMACS_INT ignore;
23811
23812 /* Note that we might not be able to find position
23813 BEFORE in the glyph matrix if the overlay is
23814 entirely covered by a `display' property. In
23815 this case, we overshoot. So let's stop in
23816 the glyph matrix before glyphs for OBJECT. */
23817 fast_find_position (w, XFASTINT (before),
23818 &dpyinfo->mouse_face_beg_col,
23819 &dpyinfo->mouse_face_beg_row,
23820 &dpyinfo->mouse_face_beg_x,
23821 &dpyinfo->mouse_face_beg_y,
23822 object);
23823
23824 dpyinfo->mouse_face_past_end
23825 = !fast_find_position (w, XFASTINT (after),
23826 &dpyinfo->mouse_face_end_col,
23827 &dpyinfo->mouse_face_end_row,
23828 &dpyinfo->mouse_face_end_x,
23829 &dpyinfo->mouse_face_end_y,
23830 Qnil);
23831 dpyinfo->mouse_face_window = window;
23832 dpyinfo->mouse_face_face_id
23833 = face_at_buffer_position (w, pos, 0, 0,
23834 &ignore, pos + 1,
23835 !dpyinfo->mouse_face_hidden);
23836
23837 /* Display it as active. */
23838 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23839 cursor = No_Cursor;
23840 }
23841 }
23842 }
23843
23844 check_help_echo:
23845
23846 /* Look for a `help-echo' property. */
23847 if (NILP (help_echo_string)) {
23848 Lisp_Object help, overlay;
23849
23850 /* Check overlays first. */
23851 help = overlay = Qnil;
23852 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23853 {
23854 overlay = overlay_vec[i];
23855 help = Foverlay_get (overlay, Qhelp_echo);
23856 }
23857
23858 if (!NILP (help))
23859 {
23860 help_echo_string = help;
23861 help_echo_window = window;
23862 help_echo_object = overlay;
23863 help_echo_pos = pos;
23864 }
23865 else
23866 {
23867 Lisp_Object object = glyph->object;
23868 int charpos = glyph->charpos;
23869
23870 /* Try text properties. */
23871 if (STRINGP (object)
23872 && charpos >= 0
23873 && charpos < SCHARS (object))
23874 {
23875 help = Fget_text_property (make_number (charpos),
23876 Qhelp_echo, object);
23877 if (NILP (help))
23878 {
23879 /* If the string itself doesn't specify a help-echo,
23880 see if the buffer text ``under'' it does. */
23881 struct glyph_row *r
23882 = MATRIX_ROW (w->current_matrix, vpos);
23883 int start = MATRIX_ROW_START_CHARPOS (r);
23884 int pos = string_buffer_position (w, object, start);
23885 if (pos > 0)
23886 {
23887 help = Fget_char_property (make_number (pos),
23888 Qhelp_echo, w->buffer);
23889 if (!NILP (help))
23890 {
23891 charpos = pos;
23892 object = w->buffer;
23893 }
23894 }
23895 }
23896 }
23897 else if (BUFFERP (object)
23898 && charpos >= BEGV
23899 && charpos < ZV)
23900 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23901 object);
23902
23903 if (!NILP (help))
23904 {
23905 help_echo_string = help;
23906 help_echo_window = window;
23907 help_echo_object = object;
23908 help_echo_pos = charpos;
23909 }
23910 }
23911 }
23912
23913 /* Look for a `pointer' property. */
23914 if (NILP (pointer))
23915 {
23916 /* Check overlays first. */
23917 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23918 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23919
23920 if (NILP (pointer))
23921 {
23922 Lisp_Object object = glyph->object;
23923 int charpos = glyph->charpos;
23924
23925 /* Try text properties. */
23926 if (STRINGP (object)
23927 && charpos >= 0
23928 && charpos < SCHARS (object))
23929 {
23930 pointer = Fget_text_property (make_number (charpos),
23931 Qpointer, object);
23932 if (NILP (pointer))
23933 {
23934 /* If the string itself doesn't specify a pointer,
23935 see if the buffer text ``under'' it does. */
23936 struct glyph_row *r
23937 = MATRIX_ROW (w->current_matrix, vpos);
23938 int start = MATRIX_ROW_START_CHARPOS (r);
23939 int pos = string_buffer_position (w, object, start);
23940 if (pos > 0)
23941 pointer = Fget_char_property (make_number (pos),
23942 Qpointer, w->buffer);
23943 }
23944 }
23945 else if (BUFFERP (object)
23946 && charpos >= BEGV
23947 && charpos < ZV)
23948 pointer = Fget_text_property (make_number (charpos),
23949 Qpointer, object);
23950 }
23951 }
23952
23953 BEGV = obegv;
23954 ZV = ozv;
23955 current_buffer = obuf;
23956 }
23957
23958 set_cursor:
23959
23960 define_frame_cursor1 (f, cursor, pointer);
23961 }
23962
23963
23964 /* EXPORT for RIF:
23965 Clear any mouse-face on window W. This function is part of the
23966 redisplay interface, and is called from try_window_id and similar
23967 functions to ensure the mouse-highlight is off. */
23968
23969 void
23970 x_clear_window_mouse_face (w)
23971 struct window *w;
23972 {
23973 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23974 Lisp_Object window;
23975
23976 BLOCK_INPUT;
23977 XSETWINDOW (window, w);
23978 if (EQ (window, dpyinfo->mouse_face_window))
23979 clear_mouse_face (dpyinfo);
23980 UNBLOCK_INPUT;
23981 }
23982
23983
23984 /* EXPORT:
23985 Just discard the mouse face information for frame F, if any.
23986 This is used when the size of F is changed. */
23987
23988 void
23989 cancel_mouse_face (f)
23990 struct frame *f;
23991 {
23992 Lisp_Object window;
23993 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23994
23995 window = dpyinfo->mouse_face_window;
23996 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23997 {
23998 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23999 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24000 dpyinfo->mouse_face_window = Qnil;
24001 }
24002 }
24003
24004
24005 #endif /* HAVE_WINDOW_SYSTEM */
24006
24007 \f
24008 /***********************************************************************
24009 Exposure Events
24010 ***********************************************************************/
24011
24012 #ifdef HAVE_WINDOW_SYSTEM
24013
24014 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24015 which intersects rectangle R. R is in window-relative coordinates. */
24016
24017 static void
24018 expose_area (w, row, r, area)
24019 struct window *w;
24020 struct glyph_row *row;
24021 XRectangle *r;
24022 enum glyph_row_area area;
24023 {
24024 struct glyph *first = row->glyphs[area];
24025 struct glyph *end = row->glyphs[area] + row->used[area];
24026 struct glyph *last;
24027 int first_x, start_x, x;
24028
24029 if (area == TEXT_AREA && row->fill_line_p)
24030 /* If row extends face to end of line write the whole line. */
24031 draw_glyphs (w, 0, row, area,
24032 0, row->used[area],
24033 DRAW_NORMAL_TEXT, 0);
24034 else
24035 {
24036 /* Set START_X to the window-relative start position for drawing glyphs of
24037 AREA. The first glyph of the text area can be partially visible.
24038 The first glyphs of other areas cannot. */
24039 start_x = window_box_left_offset (w, area);
24040 x = start_x;
24041 if (area == TEXT_AREA)
24042 x += row->x;
24043
24044 /* Find the first glyph that must be redrawn. */
24045 while (first < end
24046 && x + first->pixel_width < r->x)
24047 {
24048 x += first->pixel_width;
24049 ++first;
24050 }
24051
24052 /* Find the last one. */
24053 last = first;
24054 first_x = x;
24055 while (last < end
24056 && x < r->x + r->width)
24057 {
24058 x += last->pixel_width;
24059 ++last;
24060 }
24061
24062 /* Repaint. */
24063 if (last > first)
24064 draw_glyphs (w, first_x - start_x, row, area,
24065 first - row->glyphs[area], last - row->glyphs[area],
24066 DRAW_NORMAL_TEXT, 0);
24067 }
24068 }
24069
24070
24071 /* Redraw the parts of the glyph row ROW on window W intersecting
24072 rectangle R. R is in window-relative coordinates. Value is
24073 non-zero if mouse-face was overwritten. */
24074
24075 static int
24076 expose_line (w, row, r)
24077 struct window *w;
24078 struct glyph_row *row;
24079 XRectangle *r;
24080 {
24081 xassert (row->enabled_p);
24082
24083 if (row->mode_line_p || w->pseudo_window_p)
24084 draw_glyphs (w, 0, row, TEXT_AREA,
24085 0, row->used[TEXT_AREA],
24086 DRAW_NORMAL_TEXT, 0);
24087 else
24088 {
24089 if (row->used[LEFT_MARGIN_AREA])
24090 expose_area (w, row, r, LEFT_MARGIN_AREA);
24091 if (row->used[TEXT_AREA])
24092 expose_area (w, row, r, TEXT_AREA);
24093 if (row->used[RIGHT_MARGIN_AREA])
24094 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24095 draw_row_fringe_bitmaps (w, row);
24096 }
24097
24098 return row->mouse_face_p;
24099 }
24100
24101
24102 /* Redraw those parts of glyphs rows during expose event handling that
24103 overlap other rows. Redrawing of an exposed line writes over parts
24104 of lines overlapping that exposed line; this function fixes that.
24105
24106 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24107 row in W's current matrix that is exposed and overlaps other rows.
24108 LAST_OVERLAPPING_ROW is the last such row. */
24109
24110 static void
24111 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24112 struct window *w;
24113 struct glyph_row *first_overlapping_row;
24114 struct glyph_row *last_overlapping_row;
24115 XRectangle *r;
24116 {
24117 struct glyph_row *row;
24118
24119 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24120 if (row->overlapping_p)
24121 {
24122 xassert (row->enabled_p && !row->mode_line_p);
24123
24124 row->clip = r;
24125 if (row->used[LEFT_MARGIN_AREA])
24126 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24127
24128 if (row->used[TEXT_AREA])
24129 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24130
24131 if (row->used[RIGHT_MARGIN_AREA])
24132 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24133 row->clip = NULL;
24134 }
24135 }
24136
24137
24138 /* Return non-zero if W's cursor intersects rectangle R. */
24139
24140 static int
24141 phys_cursor_in_rect_p (w, r)
24142 struct window *w;
24143 XRectangle *r;
24144 {
24145 XRectangle cr, result;
24146 struct glyph *cursor_glyph;
24147 struct glyph_row *row;
24148
24149 if (w->phys_cursor.vpos >= 0
24150 && w->phys_cursor.vpos < w->current_matrix->nrows
24151 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24152 row->enabled_p)
24153 && row->cursor_in_fringe_p)
24154 {
24155 /* Cursor is in the fringe. */
24156 cr.x = window_box_right_offset (w,
24157 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24158 ? RIGHT_MARGIN_AREA
24159 : TEXT_AREA));
24160 cr.y = row->y;
24161 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24162 cr.height = row->height;
24163 return x_intersect_rectangles (&cr, r, &result);
24164 }
24165
24166 cursor_glyph = get_phys_cursor_glyph (w);
24167 if (cursor_glyph)
24168 {
24169 /* r is relative to W's box, but w->phys_cursor.x is relative
24170 to left edge of W's TEXT area. Adjust it. */
24171 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24172 cr.y = w->phys_cursor.y;
24173 cr.width = cursor_glyph->pixel_width;
24174 cr.height = w->phys_cursor_height;
24175 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24176 I assume the effect is the same -- and this is portable. */
24177 return x_intersect_rectangles (&cr, r, &result);
24178 }
24179 /* If we don't understand the format, pretend we're not in the hot-spot. */
24180 return 0;
24181 }
24182
24183
24184 /* EXPORT:
24185 Draw a vertical window border to the right of window W if W doesn't
24186 have vertical scroll bars. */
24187
24188 void
24189 x_draw_vertical_border (w)
24190 struct window *w;
24191 {
24192 struct frame *f = XFRAME (WINDOW_FRAME (w));
24193
24194 /* We could do better, if we knew what type of scroll-bar the adjacent
24195 windows (on either side) have... But we don't :-(
24196 However, I think this works ok. ++KFS 2003-04-25 */
24197
24198 /* Redraw borders between horizontally adjacent windows. Don't
24199 do it for frames with vertical scroll bars because either the
24200 right scroll bar of a window, or the left scroll bar of its
24201 neighbor will suffice as a border. */
24202 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24203 return;
24204
24205 if (!WINDOW_RIGHTMOST_P (w)
24206 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24207 {
24208 int x0, x1, y0, y1;
24209
24210 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24211 y1 -= 1;
24212
24213 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24214 x1 -= 1;
24215
24216 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24217 }
24218 else if (!WINDOW_LEFTMOST_P (w)
24219 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24220 {
24221 int x0, x1, y0, y1;
24222
24223 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24224 y1 -= 1;
24225
24226 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24227 x0 -= 1;
24228
24229 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24230 }
24231 }
24232
24233
24234 /* Redraw the part of window W intersection rectangle FR. Pixel
24235 coordinates in FR are frame-relative. Call this function with
24236 input blocked. Value is non-zero if the exposure overwrites
24237 mouse-face. */
24238
24239 static int
24240 expose_window (w, fr)
24241 struct window *w;
24242 XRectangle *fr;
24243 {
24244 struct frame *f = XFRAME (w->frame);
24245 XRectangle wr, r;
24246 int mouse_face_overwritten_p = 0;
24247
24248 /* If window is not yet fully initialized, do nothing. This can
24249 happen when toolkit scroll bars are used and a window is split.
24250 Reconfiguring the scroll bar will generate an expose for a newly
24251 created window. */
24252 if (w->current_matrix == NULL)
24253 return 0;
24254
24255 /* When we're currently updating the window, display and current
24256 matrix usually don't agree. Arrange for a thorough display
24257 later. */
24258 if (w == updated_window)
24259 {
24260 SET_FRAME_GARBAGED (f);
24261 return 0;
24262 }
24263
24264 /* Frame-relative pixel rectangle of W. */
24265 wr.x = WINDOW_LEFT_EDGE_X (w);
24266 wr.y = WINDOW_TOP_EDGE_Y (w);
24267 wr.width = WINDOW_TOTAL_WIDTH (w);
24268 wr.height = WINDOW_TOTAL_HEIGHT (w);
24269
24270 if (x_intersect_rectangles (fr, &wr, &r))
24271 {
24272 int yb = window_text_bottom_y (w);
24273 struct glyph_row *row;
24274 int cursor_cleared_p;
24275 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24276
24277 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24278 r.x, r.y, r.width, r.height));
24279
24280 /* Convert to window coordinates. */
24281 r.x -= WINDOW_LEFT_EDGE_X (w);
24282 r.y -= WINDOW_TOP_EDGE_Y (w);
24283
24284 /* Turn off the cursor. */
24285 if (!w->pseudo_window_p
24286 && phys_cursor_in_rect_p (w, &r))
24287 {
24288 x_clear_cursor (w);
24289 cursor_cleared_p = 1;
24290 }
24291 else
24292 cursor_cleared_p = 0;
24293
24294 /* Update lines intersecting rectangle R. */
24295 first_overlapping_row = last_overlapping_row = NULL;
24296 for (row = w->current_matrix->rows;
24297 row->enabled_p;
24298 ++row)
24299 {
24300 int y0 = row->y;
24301 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24302
24303 if ((y0 >= r.y && y0 < r.y + r.height)
24304 || (y1 > r.y && y1 < r.y + r.height)
24305 || (r.y >= y0 && r.y < y1)
24306 || (r.y + r.height > y0 && r.y + r.height < y1))
24307 {
24308 /* A header line may be overlapping, but there is no need
24309 to fix overlapping areas for them. KFS 2005-02-12 */
24310 if (row->overlapping_p && !row->mode_line_p)
24311 {
24312 if (first_overlapping_row == NULL)
24313 first_overlapping_row = row;
24314 last_overlapping_row = row;
24315 }
24316
24317 row->clip = fr;
24318 if (expose_line (w, row, &r))
24319 mouse_face_overwritten_p = 1;
24320 row->clip = NULL;
24321 }
24322 else if (row->overlapping_p)
24323 {
24324 /* We must redraw a row overlapping the exposed area. */
24325 if (y0 < r.y
24326 ? y0 + row->phys_height > r.y
24327 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24328 {
24329 if (first_overlapping_row == NULL)
24330 first_overlapping_row = row;
24331 last_overlapping_row = row;
24332 }
24333 }
24334
24335 if (y1 >= yb)
24336 break;
24337 }
24338
24339 /* Display the mode line if there is one. */
24340 if (WINDOW_WANTS_MODELINE_P (w)
24341 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24342 row->enabled_p)
24343 && row->y < r.y + r.height)
24344 {
24345 if (expose_line (w, row, &r))
24346 mouse_face_overwritten_p = 1;
24347 }
24348
24349 if (!w->pseudo_window_p)
24350 {
24351 /* Fix the display of overlapping rows. */
24352 if (first_overlapping_row)
24353 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24354 fr);
24355
24356 /* Draw border between windows. */
24357 x_draw_vertical_border (w);
24358
24359 /* Turn the cursor on again. */
24360 if (cursor_cleared_p)
24361 update_window_cursor (w, 1);
24362 }
24363 }
24364
24365 return mouse_face_overwritten_p;
24366 }
24367
24368
24369
24370 /* Redraw (parts) of all windows in the window tree rooted at W that
24371 intersect R. R contains frame pixel coordinates. Value is
24372 non-zero if the exposure overwrites mouse-face. */
24373
24374 static int
24375 expose_window_tree (w, r)
24376 struct window *w;
24377 XRectangle *r;
24378 {
24379 struct frame *f = XFRAME (w->frame);
24380 int mouse_face_overwritten_p = 0;
24381
24382 while (w && !FRAME_GARBAGED_P (f))
24383 {
24384 if (!NILP (w->hchild))
24385 mouse_face_overwritten_p
24386 |= expose_window_tree (XWINDOW (w->hchild), r);
24387 else if (!NILP (w->vchild))
24388 mouse_face_overwritten_p
24389 |= expose_window_tree (XWINDOW (w->vchild), r);
24390 else
24391 mouse_face_overwritten_p |= expose_window (w, r);
24392
24393 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24394 }
24395
24396 return mouse_face_overwritten_p;
24397 }
24398
24399
24400 /* EXPORT:
24401 Redisplay an exposed area of frame F. X and Y are the upper-left
24402 corner of the exposed rectangle. W and H are width and height of
24403 the exposed area. All are pixel values. W or H zero means redraw
24404 the entire frame. */
24405
24406 void
24407 expose_frame (f, x, y, w, h)
24408 struct frame *f;
24409 int x, y, w, h;
24410 {
24411 XRectangle r;
24412 int mouse_face_overwritten_p = 0;
24413
24414 TRACE ((stderr, "expose_frame "));
24415
24416 /* No need to redraw if frame will be redrawn soon. */
24417 if (FRAME_GARBAGED_P (f))
24418 {
24419 TRACE ((stderr, " garbaged\n"));
24420 return;
24421 }
24422
24423 /* If basic faces haven't been realized yet, there is no point in
24424 trying to redraw anything. This can happen when we get an expose
24425 event while Emacs is starting, e.g. by moving another window. */
24426 if (FRAME_FACE_CACHE (f) == NULL
24427 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24428 {
24429 TRACE ((stderr, " no faces\n"));
24430 return;
24431 }
24432
24433 if (w == 0 || h == 0)
24434 {
24435 r.x = r.y = 0;
24436 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24437 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24438 }
24439 else
24440 {
24441 r.x = x;
24442 r.y = y;
24443 r.width = w;
24444 r.height = h;
24445 }
24446
24447 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24448 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24449
24450 if (WINDOWP (f->tool_bar_window))
24451 mouse_face_overwritten_p
24452 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24453
24454 #ifdef HAVE_X_WINDOWS
24455 #ifndef MSDOS
24456 #ifndef USE_X_TOOLKIT
24457 if (WINDOWP (f->menu_bar_window))
24458 mouse_face_overwritten_p
24459 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24460 #endif /* not USE_X_TOOLKIT */
24461 #endif
24462 #endif
24463
24464 /* Some window managers support a focus-follows-mouse style with
24465 delayed raising of frames. Imagine a partially obscured frame,
24466 and moving the mouse into partially obscured mouse-face on that
24467 frame. The visible part of the mouse-face will be highlighted,
24468 then the WM raises the obscured frame. With at least one WM, KDE
24469 2.1, Emacs is not getting any event for the raising of the frame
24470 (even tried with SubstructureRedirectMask), only Expose events.
24471 These expose events will draw text normally, i.e. not
24472 highlighted. Which means we must redo the highlight here.
24473 Subsume it under ``we love X''. --gerd 2001-08-15 */
24474 /* Included in Windows version because Windows most likely does not
24475 do the right thing if any third party tool offers
24476 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24477 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24478 {
24479 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24480 if (f == dpyinfo->mouse_face_mouse_frame)
24481 {
24482 int x = dpyinfo->mouse_face_mouse_x;
24483 int y = dpyinfo->mouse_face_mouse_y;
24484 clear_mouse_face (dpyinfo);
24485 note_mouse_highlight (f, x, y);
24486 }
24487 }
24488 }
24489
24490
24491 /* EXPORT:
24492 Determine the intersection of two rectangles R1 and R2. Return
24493 the intersection in *RESULT. Value is non-zero if RESULT is not
24494 empty. */
24495
24496 int
24497 x_intersect_rectangles (r1, r2, result)
24498 XRectangle *r1, *r2, *result;
24499 {
24500 XRectangle *left, *right;
24501 XRectangle *upper, *lower;
24502 int intersection_p = 0;
24503
24504 /* Rearrange so that R1 is the left-most rectangle. */
24505 if (r1->x < r2->x)
24506 left = r1, right = r2;
24507 else
24508 left = r2, right = r1;
24509
24510 /* X0 of the intersection is right.x0, if this is inside R1,
24511 otherwise there is no intersection. */
24512 if (right->x <= left->x + left->width)
24513 {
24514 result->x = right->x;
24515
24516 /* The right end of the intersection is the minimum of the
24517 the right ends of left and right. */
24518 result->width = (min (left->x + left->width, right->x + right->width)
24519 - result->x);
24520
24521 /* Same game for Y. */
24522 if (r1->y < r2->y)
24523 upper = r1, lower = r2;
24524 else
24525 upper = r2, lower = r1;
24526
24527 /* The upper end of the intersection is lower.y0, if this is inside
24528 of upper. Otherwise, there is no intersection. */
24529 if (lower->y <= upper->y + upper->height)
24530 {
24531 result->y = lower->y;
24532
24533 /* The lower end of the intersection is the minimum of the lower
24534 ends of upper and lower. */
24535 result->height = (min (lower->y + lower->height,
24536 upper->y + upper->height)
24537 - result->y);
24538 intersection_p = 1;
24539 }
24540 }
24541
24542 return intersection_p;
24543 }
24544
24545 #endif /* HAVE_WINDOW_SYSTEM */
24546
24547 \f
24548 /***********************************************************************
24549 Initialization
24550 ***********************************************************************/
24551
24552 void
24553 syms_of_xdisp ()
24554 {
24555 Vwith_echo_area_save_vector = Qnil;
24556 staticpro (&Vwith_echo_area_save_vector);
24557
24558 Vmessage_stack = Qnil;
24559 staticpro (&Vmessage_stack);
24560
24561 Qinhibit_redisplay = intern ("inhibit-redisplay");
24562 staticpro (&Qinhibit_redisplay);
24563
24564 message_dolog_marker1 = Fmake_marker ();
24565 staticpro (&message_dolog_marker1);
24566 message_dolog_marker2 = Fmake_marker ();
24567 staticpro (&message_dolog_marker2);
24568 message_dolog_marker3 = Fmake_marker ();
24569 staticpro (&message_dolog_marker3);
24570
24571 #if GLYPH_DEBUG
24572 defsubr (&Sdump_frame_glyph_matrix);
24573 defsubr (&Sdump_glyph_matrix);
24574 defsubr (&Sdump_glyph_row);
24575 defsubr (&Sdump_tool_bar_row);
24576 defsubr (&Strace_redisplay);
24577 defsubr (&Strace_to_stderr);
24578 #endif
24579 #ifdef HAVE_WINDOW_SYSTEM
24580 defsubr (&Stool_bar_lines_needed);
24581 defsubr (&Slookup_image_map);
24582 #endif
24583 defsubr (&Sformat_mode_line);
24584 defsubr (&Sinvisible_p);
24585
24586 staticpro (&Qmenu_bar_update_hook);
24587 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24588
24589 staticpro (&Qoverriding_terminal_local_map);
24590 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24591
24592 staticpro (&Qoverriding_local_map);
24593 Qoverriding_local_map = intern ("overriding-local-map");
24594
24595 staticpro (&Qwindow_scroll_functions);
24596 Qwindow_scroll_functions = intern ("window-scroll-functions");
24597
24598 staticpro (&Qwindow_text_change_functions);
24599 Qwindow_text_change_functions = intern ("window-text-change-functions");
24600
24601 staticpro (&Qredisplay_end_trigger_functions);
24602 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24603
24604 staticpro (&Qinhibit_point_motion_hooks);
24605 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24606
24607 Qeval = intern ("eval");
24608 staticpro (&Qeval);
24609
24610 QCdata = intern (":data");
24611 staticpro (&QCdata);
24612 Qdisplay = intern ("display");
24613 staticpro (&Qdisplay);
24614 Qspace_width = intern ("space-width");
24615 staticpro (&Qspace_width);
24616 Qraise = intern ("raise");
24617 staticpro (&Qraise);
24618 Qslice = intern ("slice");
24619 staticpro (&Qslice);
24620 Qspace = intern ("space");
24621 staticpro (&Qspace);
24622 Qmargin = intern ("margin");
24623 staticpro (&Qmargin);
24624 Qpointer = intern ("pointer");
24625 staticpro (&Qpointer);
24626 Qleft_margin = intern ("left-margin");
24627 staticpro (&Qleft_margin);
24628 Qright_margin = intern ("right-margin");
24629 staticpro (&Qright_margin);
24630 Qcenter = intern ("center");
24631 staticpro (&Qcenter);
24632 Qline_height = intern ("line-height");
24633 staticpro (&Qline_height);
24634 QCalign_to = intern (":align-to");
24635 staticpro (&QCalign_to);
24636 QCrelative_width = intern (":relative-width");
24637 staticpro (&QCrelative_width);
24638 QCrelative_height = intern (":relative-height");
24639 staticpro (&QCrelative_height);
24640 QCeval = intern (":eval");
24641 staticpro (&QCeval);
24642 QCpropertize = intern (":propertize");
24643 staticpro (&QCpropertize);
24644 QCfile = intern (":file");
24645 staticpro (&QCfile);
24646 Qfontified = intern ("fontified");
24647 staticpro (&Qfontified);
24648 Qfontification_functions = intern ("fontification-functions");
24649 staticpro (&Qfontification_functions);
24650 Qtrailing_whitespace = intern ("trailing-whitespace");
24651 staticpro (&Qtrailing_whitespace);
24652 Qescape_glyph = intern ("escape-glyph");
24653 staticpro (&Qescape_glyph);
24654 Qnobreak_space = intern ("nobreak-space");
24655 staticpro (&Qnobreak_space);
24656 Qimage = intern ("image");
24657 staticpro (&Qimage);
24658 QCmap = intern (":map");
24659 staticpro (&QCmap);
24660 QCpointer = intern (":pointer");
24661 staticpro (&QCpointer);
24662 Qrect = intern ("rect");
24663 staticpro (&Qrect);
24664 Qcircle = intern ("circle");
24665 staticpro (&Qcircle);
24666 Qpoly = intern ("poly");
24667 staticpro (&Qpoly);
24668 Qmessage_truncate_lines = intern ("message-truncate-lines");
24669 staticpro (&Qmessage_truncate_lines);
24670 Qgrow_only = intern ("grow-only");
24671 staticpro (&Qgrow_only);
24672 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24673 staticpro (&Qinhibit_menubar_update);
24674 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24675 staticpro (&Qinhibit_eval_during_redisplay);
24676 Qposition = intern ("position");
24677 staticpro (&Qposition);
24678 Qbuffer_position = intern ("buffer-position");
24679 staticpro (&Qbuffer_position);
24680 Qobject = intern ("object");
24681 staticpro (&Qobject);
24682 Qbar = intern ("bar");
24683 staticpro (&Qbar);
24684 Qhbar = intern ("hbar");
24685 staticpro (&Qhbar);
24686 Qbox = intern ("box");
24687 staticpro (&Qbox);
24688 Qhollow = intern ("hollow");
24689 staticpro (&Qhollow);
24690 Qhand = intern ("hand");
24691 staticpro (&Qhand);
24692 Qarrow = intern ("arrow");
24693 staticpro (&Qarrow);
24694 Qtext = intern ("text");
24695 staticpro (&Qtext);
24696 Qrisky_local_variable = intern ("risky-local-variable");
24697 staticpro (&Qrisky_local_variable);
24698 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24699 staticpro (&Qinhibit_free_realized_faces);
24700
24701 list_of_error = Fcons (Fcons (intern ("error"),
24702 Fcons (intern ("void-variable"), Qnil)),
24703 Qnil);
24704 staticpro (&list_of_error);
24705
24706 Qlast_arrow_position = intern ("last-arrow-position");
24707 staticpro (&Qlast_arrow_position);
24708 Qlast_arrow_string = intern ("last-arrow-string");
24709 staticpro (&Qlast_arrow_string);
24710
24711 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24712 staticpro (&Qoverlay_arrow_string);
24713 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24714 staticpro (&Qoverlay_arrow_bitmap);
24715
24716 echo_buffer[0] = echo_buffer[1] = Qnil;
24717 staticpro (&echo_buffer[0]);
24718 staticpro (&echo_buffer[1]);
24719
24720 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24721 staticpro (&echo_area_buffer[0]);
24722 staticpro (&echo_area_buffer[1]);
24723
24724 Vmessages_buffer_name = build_string ("*Messages*");
24725 staticpro (&Vmessages_buffer_name);
24726
24727 mode_line_proptrans_alist = Qnil;
24728 staticpro (&mode_line_proptrans_alist);
24729 mode_line_string_list = Qnil;
24730 staticpro (&mode_line_string_list);
24731 mode_line_string_face = Qnil;
24732 staticpro (&mode_line_string_face);
24733 mode_line_string_face_prop = Qnil;
24734 staticpro (&mode_line_string_face_prop);
24735 Vmode_line_unwind_vector = Qnil;
24736 staticpro (&Vmode_line_unwind_vector);
24737
24738 help_echo_string = Qnil;
24739 staticpro (&help_echo_string);
24740 help_echo_object = Qnil;
24741 staticpro (&help_echo_object);
24742 help_echo_window = Qnil;
24743 staticpro (&help_echo_window);
24744 previous_help_echo_string = Qnil;
24745 staticpro (&previous_help_echo_string);
24746 help_echo_pos = -1;
24747
24748 #ifdef HAVE_WINDOW_SYSTEM
24749 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24750 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24751 For example, if a block cursor is over a tab, it will be drawn as
24752 wide as that tab on the display. */);
24753 x_stretch_cursor_p = 0;
24754 #endif
24755
24756 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24757 doc: /* *Non-nil means highlight trailing whitespace.
24758 The face used for trailing whitespace is `trailing-whitespace'. */);
24759 Vshow_trailing_whitespace = Qnil;
24760
24761 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24762 doc: /* *Control highlighting of nobreak space and soft hyphen.
24763 A value of t means highlight the character itself (for nobreak space,
24764 use face `nobreak-space').
24765 A value of nil means no highlighting.
24766 Other values mean display the escape glyph followed by an ordinary
24767 space or ordinary hyphen. */);
24768 Vnobreak_char_display = Qt;
24769
24770 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24771 doc: /* *The pointer shape to show in void text areas.
24772 A value of nil means to show the text pointer. Other options are `arrow',
24773 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24774 Vvoid_text_area_pointer = Qarrow;
24775
24776 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24777 doc: /* Non-nil means don't actually do any redisplay.
24778 This is used for internal purposes. */);
24779 Vinhibit_redisplay = Qnil;
24780
24781 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24782 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24783 Vglobal_mode_string = Qnil;
24784
24785 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24786 doc: /* Marker for where to display an arrow on top of the buffer text.
24787 This must be the beginning of a line in order to work.
24788 See also `overlay-arrow-string'. */);
24789 Voverlay_arrow_position = Qnil;
24790
24791 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24792 doc: /* String to display as an arrow in non-window frames.
24793 See also `overlay-arrow-position'. */);
24794 Voverlay_arrow_string = build_string ("=>");
24795
24796 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24797 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24798 The symbols on this list are examined during redisplay to determine
24799 where to display overlay arrows. */);
24800 Voverlay_arrow_variable_list
24801 = Fcons (intern ("overlay-arrow-position"), Qnil);
24802
24803 DEFVAR_INT ("scroll-step", &scroll_step,
24804 doc: /* *The number of lines to try scrolling a window by when point moves out.
24805 If that fails to bring point back on frame, point is centered instead.
24806 If this is zero, point is always centered after it moves off frame.
24807 If you want scrolling to always be a line at a time, you should set
24808 `scroll-conservatively' to a large value rather than set this to 1. */);
24809
24810 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24811 doc: /* *Scroll up to this many lines, to bring point back on screen.
24812 If point moves off-screen, redisplay will scroll by up to
24813 `scroll-conservatively' lines in order to bring point just barely
24814 onto the screen again. If that cannot be done, then redisplay
24815 recenters point as usual.
24816
24817 A value of zero means always recenter point if it moves off screen. */);
24818 scroll_conservatively = 0;
24819
24820 DEFVAR_INT ("scroll-margin", &scroll_margin,
24821 doc: /* *Number of lines of margin at the top and bottom of a window.
24822 Recenter the window whenever point gets within this many lines
24823 of the top or bottom of the window. */);
24824 scroll_margin = 0;
24825
24826 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24827 doc: /* Pixels per inch value for non-window system displays.
24828 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24829 Vdisplay_pixels_per_inch = make_float (72.0);
24830
24831 #if GLYPH_DEBUG
24832 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24833 #endif
24834
24835 DEFVAR_LISP ("truncate-partial-width-windows",
24836 &Vtruncate_partial_width_windows,
24837 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24838 For an integer value, truncate lines in each window with less than the
24839 full frame width, provided the window width is less than that integer;
24840 otherwise, respect the value of `truncate-lines'.
24841
24842 For any other non-nil value, truncate lines in all windows with
24843 less than the full frame width.
24844
24845 A value of nil means to respect the value of `truncate-lines'. */);
24846 Vtruncate_partial_width_windows = make_number (30);
24847
24848 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24849 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24850 Any other value means to use the appropriate face, `mode-line',
24851 `header-line', or `menu' respectively. */);
24852 mode_line_inverse_video = 1;
24853
24854 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24855 doc: /* *Maximum buffer size for which line number should be displayed.
24856 If the buffer is bigger than this, the line number does not appear
24857 in the mode line. A value of nil means no limit. */);
24858 Vline_number_display_limit = Qnil;
24859
24860 DEFVAR_INT ("line-number-display-limit-width",
24861 &line_number_display_limit_width,
24862 doc: /* *Maximum line width (in characters) for line number display.
24863 If the average length of the lines near point is bigger than this, then the
24864 line number may be omitted from the mode line. */);
24865 line_number_display_limit_width = 200;
24866
24867 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24868 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24869 highlight_nonselected_windows = 0;
24870
24871 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24872 doc: /* Non-nil if more than one frame is visible on this display.
24873 Minibuffer-only frames don't count, but iconified frames do.
24874 This variable is not guaranteed to be accurate except while processing
24875 `frame-title-format' and `icon-title-format'. */);
24876
24877 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24878 doc: /* Template for displaying the title bar of visible frames.
24879 \(Assuming the window manager supports this feature.)
24880
24881 This variable has the same structure as `mode-line-format', except that
24882 the %c and %l constructs are ignored. It is used only on frames for
24883 which no explicit name has been set \(see `modify-frame-parameters'). */);
24884
24885 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24886 doc: /* Template for displaying the title bar of an iconified frame.
24887 \(Assuming the window manager supports this feature.)
24888 This variable has the same structure as `mode-line-format' (which see),
24889 and is used only on frames for which no explicit name has been set
24890 \(see `modify-frame-parameters'). */);
24891 Vicon_title_format
24892 = Vframe_title_format
24893 = Fcons (intern ("multiple-frames"),
24894 Fcons (build_string ("%b"),
24895 Fcons (Fcons (empty_unibyte_string,
24896 Fcons (intern ("invocation-name"),
24897 Fcons (build_string ("@"),
24898 Fcons (intern ("system-name"),
24899 Qnil)))),
24900 Qnil)));
24901
24902 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24903 doc: /* Maximum number of lines to keep in the message log buffer.
24904 If nil, disable message logging. If t, log messages but don't truncate
24905 the buffer when it becomes large. */);
24906 Vmessage_log_max = make_number (100);
24907
24908 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24909 doc: /* Functions called before redisplay, if window sizes have changed.
24910 The value should be a list of functions that take one argument.
24911 Just before redisplay, for each frame, if any of its windows have changed
24912 size since the last redisplay, or have been split or deleted,
24913 all the functions in the list are called, with the frame as argument. */);
24914 Vwindow_size_change_functions = Qnil;
24915
24916 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24917 doc: /* List of functions to call before redisplaying a window with scrolling.
24918 Each function is called with two arguments, the window
24919 and its new display-start position. Note that the value of `window-end'
24920 is not valid when these functions are called. */);
24921 Vwindow_scroll_functions = Qnil;
24922
24923 DEFVAR_LISP ("window-text-change-functions",
24924 &Vwindow_text_change_functions,
24925 doc: /* Functions to call in redisplay when text in the window might change. */);
24926 Vwindow_text_change_functions = Qnil;
24927
24928 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24929 doc: /* Functions called when redisplay of a window reaches the end trigger.
24930 Each function is called with two arguments, the window and the end trigger value.
24931 See `set-window-redisplay-end-trigger'. */);
24932 Vredisplay_end_trigger_functions = Qnil;
24933
24934 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24935 doc: /* *Non-nil means autoselect window with mouse pointer.
24936 If nil, do not autoselect windows.
24937 A positive number means delay autoselection by that many seconds: a
24938 window is autoselected only after the mouse has remained in that
24939 window for the duration of the delay.
24940 A negative number has a similar effect, but causes windows to be
24941 autoselected only after the mouse has stopped moving. \(Because of
24942 the way Emacs compares mouse events, you will occasionally wait twice
24943 that time before the window gets selected.\)
24944 Any other value means to autoselect window instantaneously when the
24945 mouse pointer enters it.
24946
24947 Autoselection selects the minibuffer only if it is active, and never
24948 unselects the minibuffer if it is active.
24949
24950 When customizing this variable make sure that the actual value of
24951 `focus-follows-mouse' matches the behavior of your window manager. */);
24952 Vmouse_autoselect_window = Qnil;
24953
24954 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24955 doc: /* *Non-nil means automatically resize tool-bars.
24956 This dynamically changes the tool-bar's height to the minimum height
24957 that is needed to make all tool-bar items visible.
24958 If value is `grow-only', the tool-bar's height is only increased
24959 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24960 Vauto_resize_tool_bars = Qt;
24961
24962 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24963 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24964 auto_raise_tool_bar_buttons_p = 1;
24965
24966 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24967 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24968 make_cursor_line_fully_visible_p = 1;
24969
24970 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24971 doc: /* *Border below tool-bar in pixels.
24972 If an integer, use it as the height of the border.
24973 If it is one of `internal-border-width' or `border-width', use the
24974 value of the corresponding frame parameter.
24975 Otherwise, no border is added below the tool-bar. */);
24976 Vtool_bar_border = Qinternal_border_width;
24977
24978 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24979 doc: /* *Margin around tool-bar buttons in pixels.
24980 If an integer, use that for both horizontal and vertical margins.
24981 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24982 HORZ specifying the horizontal margin, and VERT specifying the
24983 vertical margin. */);
24984 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24985
24986 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24987 doc: /* *Relief thickness of tool-bar buttons. */);
24988 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24989
24990 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24991 doc: /* List of functions to call to fontify regions of text.
24992 Each function is called with one argument POS. Functions must
24993 fontify a region starting at POS in the current buffer, and give
24994 fontified regions the property `fontified'. */);
24995 Vfontification_functions = Qnil;
24996 Fmake_variable_buffer_local (Qfontification_functions);
24997
24998 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24999 &unibyte_display_via_language_environment,
25000 doc: /* *Non-nil means display unibyte text according to language environment.
25001 Specifically this means that unibyte non-ASCII characters
25002 are displayed by converting them to the equivalent multibyte characters
25003 according to the current language environment. As a result, they are
25004 displayed according to the current fontset. */);
25005 unibyte_display_via_language_environment = 0;
25006
25007 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25008 doc: /* *Maximum height for resizing mini-windows.
25009 If a float, it specifies a fraction of the mini-window frame's height.
25010 If an integer, it specifies a number of lines. */);
25011 Vmax_mini_window_height = make_float (0.25);
25012
25013 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25014 doc: /* *How to resize mini-windows.
25015 A value of nil means don't automatically resize mini-windows.
25016 A value of t means resize them to fit the text displayed in them.
25017 A value of `grow-only', the default, means let mini-windows grow
25018 only, until their display becomes empty, at which point the windows
25019 go back to their normal size. */);
25020 Vresize_mini_windows = Qgrow_only;
25021
25022 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25023 doc: /* Alist specifying how to blink the cursor off.
25024 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25025 `cursor-type' frame-parameter or variable equals ON-STATE,
25026 comparing using `equal', Emacs uses OFF-STATE to specify
25027 how to blink it off. ON-STATE and OFF-STATE are values for
25028 the `cursor-type' frame parameter.
25029
25030 If a frame's ON-STATE has no entry in this list,
25031 the frame's other specifications determine how to blink the cursor off. */);
25032 Vblink_cursor_alist = Qnil;
25033
25034 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25035 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25036 automatic_hscrolling_p = 1;
25037 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25038 staticpro (&Qauto_hscroll_mode);
25039
25040 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25041 doc: /* *How many columns away from the window edge point is allowed to get
25042 before automatic hscrolling will horizontally scroll the window. */);
25043 hscroll_margin = 5;
25044
25045 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25046 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25047 When point is less than `hscroll-margin' columns from the window
25048 edge, automatic hscrolling will scroll the window by the amount of columns
25049 determined by this variable. If its value is a positive integer, scroll that
25050 many columns. If it's a positive floating-point number, it specifies the
25051 fraction of the window's width to scroll. If it's nil or zero, point will be
25052 centered horizontally after the scroll. Any other value, including negative
25053 numbers, are treated as if the value were zero.
25054
25055 Automatic hscrolling always moves point outside the scroll margin, so if
25056 point was more than scroll step columns inside the margin, the window will
25057 scroll more than the value given by the scroll step.
25058
25059 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25060 and `scroll-right' overrides this variable's effect. */);
25061 Vhscroll_step = make_number (0);
25062
25063 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25064 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25065 Bind this around calls to `message' to let it take effect. */);
25066 message_truncate_lines = 0;
25067
25068 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25069 doc: /* Normal hook run to update the menu bar definitions.
25070 Redisplay runs this hook before it redisplays the menu bar.
25071 This is used to update submenus such as Buffers,
25072 whose contents depend on various data. */);
25073 Vmenu_bar_update_hook = Qnil;
25074
25075 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25076 doc: /* Frame for which we are updating a menu.
25077 The enable predicate for a menu binding should check this variable. */);
25078 Vmenu_updating_frame = Qnil;
25079
25080 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25081 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25082 inhibit_menubar_update = 0;
25083
25084 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25085 doc: /* Prefix added to the beginning of all continuation lines at display-time.
25086 May be a string, an image, or a stretch-glyph such as used by the
25087 `display' text-property.
25088
25089 This variable is overridden by any `wrap-prefix' text-property.
25090
25091 To add a prefix to non-continuation lines, use the `line-prefix' variable. */);
25092 Vwrap_prefix = Qnil;
25093 staticpro (&Qwrap_prefix);
25094 Qwrap_prefix = intern ("wrap-prefix");
25095 Fmake_variable_buffer_local (Qwrap_prefix);
25096
25097 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25098 doc: /* Prefix added to the beginning of all non-continuation lines at display-time.
25099 May be a string, an image, or a stretch-glyph such as used by the
25100 `display' text-property.
25101
25102 This variable is overridden by any `line-prefix' text-property.
25103
25104 To add a prefix to continuation lines, use the `wrap-prefix' variable. */);
25105 Vline_prefix = Qnil;
25106 staticpro (&Qline_prefix);
25107 Qline_prefix = intern ("line-prefix");
25108 Fmake_variable_buffer_local (Qline_prefix);
25109
25110 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25111 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25112 inhibit_eval_during_redisplay = 0;
25113
25114 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25115 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25116 inhibit_free_realized_faces = 0;
25117
25118 #if GLYPH_DEBUG
25119 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25120 doc: /* Inhibit try_window_id display optimization. */);
25121 inhibit_try_window_id = 0;
25122
25123 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25124 doc: /* Inhibit try_window_reusing display optimization. */);
25125 inhibit_try_window_reusing = 0;
25126
25127 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25128 doc: /* Inhibit try_cursor_movement display optimization. */);
25129 inhibit_try_cursor_movement = 0;
25130 #endif /* GLYPH_DEBUG */
25131
25132 DEFVAR_INT ("overline-margin", &overline_margin,
25133 doc: /* *Space between overline and text, in pixels.
25134 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25135 margin to the caracter height. */);
25136 overline_margin = 2;
25137
25138 DEFVAR_INT ("underline-minimum-offset",
25139 &underline_minimum_offset,
25140 doc: /* Minimum distance between baseline and underline.
25141 This can improve legibility of underlined text at small font sizes,
25142 particularly when using variable `x-use-underline-position-properties'
25143 with fonts that specify an UNDERLINE_POSITION relatively close to the
25144 baseline. The default value is 1. */);
25145 underline_minimum_offset = 1;
25146 }
25147
25148
25149 /* Initialize this module when Emacs starts. */
25150
25151 void
25152 init_xdisp ()
25153 {
25154 Lisp_Object root_window;
25155 struct window *mini_w;
25156
25157 current_header_line_height = current_mode_line_height = -1;
25158
25159 CHARPOS (this_line_start_pos) = 0;
25160
25161 mini_w = XWINDOW (minibuf_window);
25162 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25163
25164 if (!noninteractive)
25165 {
25166 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25167 int i;
25168
25169 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25170 set_window_height (root_window,
25171 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25172 0);
25173 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25174 set_window_height (minibuf_window, 1, 0);
25175
25176 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25177 mini_w->total_cols = make_number (FRAME_COLS (f));
25178
25179 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25180 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25181 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25182
25183 /* The default ellipsis glyphs `...'. */
25184 for (i = 0; i < 3; ++i)
25185 default_invis_vector[i] = make_number ('.');
25186 }
25187
25188 {
25189 /* Allocate the buffer for frame titles.
25190 Also used for `format-mode-line'. */
25191 int size = 100;
25192 mode_line_noprop_buf = (char *) xmalloc (size);
25193 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25194 mode_line_noprop_ptr = mode_line_noprop_buf;
25195 mode_line_target = MODE_LINE_DISPLAY;
25196 }
25197
25198 help_echo_showing_p = 0;
25199 }
25200
25201
25202 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25203 (do not change this comment) */