]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from mainline.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171 #include <limits.h>
172 #include <setjmp.h>
173
174 #include "lisp.h"
175 #include "keyboard.h"
176 #include "frame.h"
177 #include "window.h"
178 #include "termchar.h"
179 #include "dispextern.h"
180 #include "buffer.h"
181 #include "character.h"
182 #include "charset.h"
183 #include "indent.h"
184 #include "commands.h"
185 #include "keymap.h"
186 #include "macros.h"
187 #include "disptab.h"
188 #include "termhooks.h"
189 #include "intervals.h"
190 #include "coding.h"
191 #include "process.h"
192 #include "region-cache.h"
193 #include "font.h"
194 #include "fontset.h"
195 #include "blockinput.h"
196
197 #ifdef HAVE_X_WINDOWS
198 #include "xterm.h"
199 #endif
200 #ifdef WINDOWSNT
201 #include "w32term.h"
202 #endif
203 #ifdef HAVE_NS
204 #include "nsterm.h"
205 #endif
206 #ifdef USE_GTK
207 #include "gtkutil.h"
208 #endif
209
210 #include "font.h"
211
212 #ifndef FRAME_X_OUTPUT
213 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
214 #endif
215
216 #define INFINITY 10000000
217
218 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
219 || defined(HAVE_NS) || defined (USE_GTK)
220 extern void set_frame_menubar P_ ((struct frame *f, int, int));
221 extern int pending_menu_activation;
222 #endif
223
224 extern int interrupt_input;
225 extern int command_loop_level;
226
227 extern Lisp_Object do_mouse_tracking;
228
229 extern int minibuffer_auto_raise;
230 extern Lisp_Object Vminibuffer_list;
231
232 extern Lisp_Object Qface;
233 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
234
235 extern Lisp_Object Voverriding_local_map;
236 extern Lisp_Object Voverriding_local_map_menu_flag;
237 extern Lisp_Object Qmenu_item;
238 extern Lisp_Object Qwhen;
239 extern Lisp_Object Qhelp_echo;
240 extern Lisp_Object Qbefore_string, Qafter_string;
241
242 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
243 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
244 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
245 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
246 Lisp_Object Qinhibit_point_motion_hooks;
247 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
248 Lisp_Object Qfontified;
249 Lisp_Object Qgrow_only;
250 Lisp_Object Qinhibit_eval_during_redisplay;
251 Lisp_Object Qbuffer_position, Qposition, Qobject;
252 Lisp_Object Qright_to_left, Qleft_to_right;
253
254 /* Cursor shapes */
255 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
256
257 /* Pointer shapes */
258 Lisp_Object Qarrow, Qhand, Qtext;
259
260 Lisp_Object Qrisky_local_variable;
261
262 /* Holds the list (error). */
263 Lisp_Object list_of_error;
264
265 /* Functions called to fontify regions of text. */
266
267 Lisp_Object Vfontification_functions;
268 Lisp_Object Qfontification_functions;
269
270 /* Non-nil means automatically select any window when the mouse
271 cursor moves into it. */
272 Lisp_Object Vmouse_autoselect_window;
273
274 Lisp_Object Vwrap_prefix, Qwrap_prefix;
275 Lisp_Object Vline_prefix, Qline_prefix;
276
277 /* Non-zero means draw tool bar buttons raised when the mouse moves
278 over them. */
279
280 int auto_raise_tool_bar_buttons_p;
281
282 /* Non-zero means to reposition window if cursor line is only partially visible. */
283
284 int make_cursor_line_fully_visible_p;
285
286 /* Margin below tool bar in pixels. 0 or nil means no margin.
287 If value is `internal-border-width' or `border-width',
288 the corresponding frame parameter is used. */
289
290 Lisp_Object Vtool_bar_border;
291
292 /* Margin around tool bar buttons in pixels. */
293
294 Lisp_Object Vtool_bar_button_margin;
295
296 /* Thickness of shadow to draw around tool bar buttons. */
297
298 EMACS_INT tool_bar_button_relief;
299
300 /* Non-nil means automatically resize tool-bars so that all tool-bar
301 items are visible, and no blank lines remain.
302
303 If value is `grow-only', only make tool-bar bigger. */
304
305 Lisp_Object Vauto_resize_tool_bars;
306
307 /* Non-zero means draw block and hollow cursor as wide as the glyph
308 under it. For example, if a block cursor is over a tab, it will be
309 drawn as wide as that tab on the display. */
310
311 int x_stretch_cursor_p;
312
313 /* Non-nil means don't actually do any redisplay. */
314
315 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
316
317 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
318
319 int inhibit_eval_during_redisplay;
320
321 /* Names of text properties relevant for redisplay. */
322
323 Lisp_Object Qdisplay;
324 extern Lisp_Object Qface, Qinvisible, Qwidth;
325
326 /* Symbols used in text property values. */
327
328 Lisp_Object Vdisplay_pixels_per_inch;
329 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
330 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
331 Lisp_Object Qslice;
332 Lisp_Object Qcenter;
333 Lisp_Object Qmargin, Qpointer;
334 Lisp_Object Qline_height;
335 extern Lisp_Object Qheight;
336 extern Lisp_Object QCwidth, QCheight, QCascent;
337 extern Lisp_Object Qscroll_bar;
338 extern Lisp_Object Qcursor;
339
340 /* Non-nil means highlight trailing whitespace. */
341
342 Lisp_Object Vshow_trailing_whitespace;
343
344 /* Non-nil means escape non-break space and hyphens. */
345
346 Lisp_Object Vnobreak_char_display;
347
348 #ifdef HAVE_WINDOW_SYSTEM
349 extern Lisp_Object Voverflow_newline_into_fringe;
350
351 /* Test if overflow newline into fringe. Called with iterator IT
352 at or past right window margin, and with IT->current_x set. */
353
354 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
355 (!NILP (Voverflow_newline_into_fringe) \
356 && FRAME_WINDOW_P (it->f) \
357 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
358 && it->current_x == it->last_visible_x \
359 && it->line_wrap != WORD_WRAP)
360
361 #else /* !HAVE_WINDOW_SYSTEM */
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
363 #endif /* HAVE_WINDOW_SYSTEM */
364
365 /* Test if the display element loaded in IT is a space or tab
366 character. This is used to determine word wrapping. */
367
368 #define IT_DISPLAYING_WHITESPACE(it) \
369 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
370
371 /* Non-nil means show the text cursor in void text areas
372 i.e. in blank areas after eol and eob. This used to be
373 the default in 21.3. */
374
375 Lisp_Object Vvoid_text_area_pointer;
376
377 /* Name of the face used to highlight trailing whitespace. */
378
379 Lisp_Object Qtrailing_whitespace;
380
381 /* Name and number of the face used to highlight escape glyphs. */
382
383 Lisp_Object Qescape_glyph;
384
385 /* Name and number of the face used to highlight non-breaking spaces. */
386
387 Lisp_Object Qnobreak_space;
388
389 /* The symbol `image' which is the car of the lists used to represent
390 images in Lisp. */
391
392 Lisp_Object Qimage;
393
394 /* The image map types. */
395 Lisp_Object QCmap, QCpointer;
396 Lisp_Object Qrect, Qcircle, Qpoly;
397
398 /* Non-zero means print newline to stdout before next mini-buffer
399 message. */
400
401 int noninteractive_need_newline;
402
403 /* Non-zero means print newline to message log before next message. */
404
405 static int message_log_need_newline;
406
407 /* Three markers that message_dolog uses.
408 It could allocate them itself, but that causes trouble
409 in handling memory-full errors. */
410 static Lisp_Object message_dolog_marker1;
411 static Lisp_Object message_dolog_marker2;
412 static Lisp_Object message_dolog_marker3;
413 \f
414 /* The buffer position of the first character appearing entirely or
415 partially on the line of the selected window which contains the
416 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
417 redisplay optimization in redisplay_internal. */
418
419 static struct text_pos this_line_start_pos;
420
421 /* Number of characters past the end of the line above, including the
422 terminating newline. */
423
424 static struct text_pos this_line_end_pos;
425
426 /* The vertical positions and the height of this line. */
427
428 static int this_line_vpos;
429 static int this_line_y;
430 static int this_line_pixel_height;
431
432 /* X position at which this display line starts. Usually zero;
433 negative if first character is partially visible. */
434
435 static int this_line_start_x;
436
437 /* Buffer that this_line_.* variables are referring to. */
438
439 static struct buffer *this_line_buffer;
440
441 /* Nonzero means truncate lines in all windows less wide than the
442 frame. */
443
444 Lisp_Object Vtruncate_partial_width_windows;
445
446 /* A flag to control how to display unibyte 8-bit character. */
447
448 int unibyte_display_via_language_environment;
449
450 /* Nonzero means we have more than one non-mini-buffer-only frame.
451 Not guaranteed to be accurate except while parsing
452 frame-title-format. */
453
454 int multiple_frames;
455
456 Lisp_Object Vglobal_mode_string;
457
458
459 /* List of variables (symbols) which hold markers for overlay arrows.
460 The symbols on this list are examined during redisplay to determine
461 where to display overlay arrows. */
462
463 Lisp_Object Voverlay_arrow_variable_list;
464
465 /* Marker for where to display an arrow on top of the buffer text. */
466
467 Lisp_Object Voverlay_arrow_position;
468
469 /* String to display for the arrow. Only used on terminal frames. */
470
471 Lisp_Object Voverlay_arrow_string;
472
473 /* Values of those variables at last redisplay are stored as
474 properties on `overlay-arrow-position' symbol. However, if
475 Voverlay_arrow_position is a marker, last-arrow-position is its
476 numerical position. */
477
478 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
479
480 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
481 properties on a symbol in overlay-arrow-variable-list. */
482
483 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
484
485 /* Like mode-line-format, but for the title bar on a visible frame. */
486
487 Lisp_Object Vframe_title_format;
488
489 /* Like mode-line-format, but for the title bar on an iconified frame. */
490
491 Lisp_Object Vicon_title_format;
492
493 /* List of functions to call when a window's size changes. These
494 functions get one arg, a frame on which one or more windows' sizes
495 have changed. */
496
497 static Lisp_Object Vwindow_size_change_functions;
498
499 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
500
501 /* Nonzero if an overlay arrow has been displayed in this window. */
502
503 static int overlay_arrow_seen;
504
505 /* Nonzero means highlight the region even in nonselected windows. */
506
507 int highlight_nonselected_windows;
508
509 /* If cursor motion alone moves point off frame, try scrolling this
510 many lines up or down if that will bring it back. */
511
512 static EMACS_INT scroll_step;
513
514 /* Nonzero means scroll just far enough to bring point back on the
515 screen, when appropriate. */
516
517 static EMACS_INT scroll_conservatively;
518
519 /* Recenter the window whenever point gets within this many lines of
520 the top or bottom of the window. This value is translated into a
521 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
522 that there is really a fixed pixel height scroll margin. */
523
524 EMACS_INT scroll_margin;
525
526 /* Number of windows showing the buffer of the selected window (or
527 another buffer with the same base buffer). keyboard.c refers to
528 this. */
529
530 int buffer_shared;
531
532 /* Vector containing glyphs for an ellipsis `...'. */
533
534 static Lisp_Object default_invis_vector[3];
535
536 /* Zero means display the mode-line/header-line/menu-bar in the default face
537 (this slightly odd definition is for compatibility with previous versions
538 of emacs), non-zero means display them using their respective faces.
539
540 This variable is deprecated. */
541
542 int mode_line_inverse_video;
543
544 /* Prompt to display in front of the mini-buffer contents. */
545
546 Lisp_Object minibuf_prompt;
547
548 /* Width of current mini-buffer prompt. Only set after display_line
549 of the line that contains the prompt. */
550
551 int minibuf_prompt_width;
552
553 /* This is the window where the echo area message was displayed. It
554 is always a mini-buffer window, but it may not be the same window
555 currently active as a mini-buffer. */
556
557 Lisp_Object echo_area_window;
558
559 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
560 pushes the current message and the value of
561 message_enable_multibyte on the stack, the function restore_message
562 pops the stack and displays MESSAGE again. */
563
564 Lisp_Object Vmessage_stack;
565
566 /* Nonzero means multibyte characters were enabled when the echo area
567 message was specified. */
568
569 int message_enable_multibyte;
570
571 /* Nonzero if we should redraw the mode lines on the next redisplay. */
572
573 int update_mode_lines;
574
575 /* Nonzero if window sizes or contents have changed since last
576 redisplay that finished. */
577
578 int windows_or_buffers_changed;
579
580 /* Nonzero means a frame's cursor type has been changed. */
581
582 int cursor_type_changed;
583
584 /* Nonzero after display_mode_line if %l was used and it displayed a
585 line number. */
586
587 int line_number_displayed;
588
589 /* Maximum buffer size for which to display line numbers. */
590
591 Lisp_Object Vline_number_display_limit;
592
593 /* Line width to consider when repositioning for line number display. */
594
595 static EMACS_INT line_number_display_limit_width;
596
597 /* Number of lines to keep in the message log buffer. t means
598 infinite. nil means don't log at all. */
599
600 Lisp_Object Vmessage_log_max;
601
602 /* The name of the *Messages* buffer, a string. */
603
604 static Lisp_Object Vmessages_buffer_name;
605
606 /* Current, index 0, and last displayed echo area message. Either
607 buffers from echo_buffers, or nil to indicate no message. */
608
609 Lisp_Object echo_area_buffer[2];
610
611 /* The buffers referenced from echo_area_buffer. */
612
613 static Lisp_Object echo_buffer[2];
614
615 /* A vector saved used in with_area_buffer to reduce consing. */
616
617 static Lisp_Object Vwith_echo_area_save_vector;
618
619 /* Non-zero means display_echo_area should display the last echo area
620 message again. Set by redisplay_preserve_echo_area. */
621
622 static int display_last_displayed_message_p;
623
624 /* Nonzero if echo area is being used by print; zero if being used by
625 message. */
626
627 int message_buf_print;
628
629 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
630
631 Lisp_Object Qinhibit_menubar_update;
632 int inhibit_menubar_update;
633
634 /* When evaluating expressions from menu bar items (enable conditions,
635 for instance), this is the frame they are being processed for. */
636
637 Lisp_Object Vmenu_updating_frame;
638
639 /* Maximum height for resizing mini-windows. Either a float
640 specifying a fraction of the available height, or an integer
641 specifying a number of lines. */
642
643 Lisp_Object Vmax_mini_window_height;
644
645 /* Non-zero means messages should be displayed with truncated
646 lines instead of being continued. */
647
648 int message_truncate_lines;
649 Lisp_Object Qmessage_truncate_lines;
650
651 /* Set to 1 in clear_message to make redisplay_internal aware
652 of an emptied echo area. */
653
654 static int message_cleared_p;
655
656 /* How to blink the default frame cursor off. */
657 Lisp_Object Vblink_cursor_alist;
658
659 /* A scratch glyph row with contents used for generating truncation
660 glyphs. Also used in direct_output_for_insert. */
661
662 #define MAX_SCRATCH_GLYPHS 100
663 struct glyph_row scratch_glyph_row;
664 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
665
666 /* Ascent and height of the last line processed by move_it_to. */
667
668 static int last_max_ascent, last_height;
669
670 /* Non-zero if there's a help-echo in the echo area. */
671
672 int help_echo_showing_p;
673
674 /* If >= 0, computed, exact values of mode-line and header-line height
675 to use in the macros CURRENT_MODE_LINE_HEIGHT and
676 CURRENT_HEADER_LINE_HEIGHT. */
677
678 int current_mode_line_height, current_header_line_height;
679
680 /* The maximum distance to look ahead for text properties. Values
681 that are too small let us call compute_char_face and similar
682 functions too often which is expensive. Values that are too large
683 let us call compute_char_face and alike too often because we
684 might not be interested in text properties that far away. */
685
686 #define TEXT_PROP_DISTANCE_LIMIT 100
687
688 #if GLYPH_DEBUG
689
690 /* Variables to turn off display optimizations from Lisp. */
691
692 int inhibit_try_window_id, inhibit_try_window_reusing;
693 int inhibit_try_cursor_movement;
694
695 /* Non-zero means print traces of redisplay if compiled with
696 GLYPH_DEBUG != 0. */
697
698 int trace_redisplay_p;
699
700 #endif /* GLYPH_DEBUG */
701
702 #ifdef DEBUG_TRACE_MOVE
703 /* Non-zero means trace with TRACE_MOVE to stderr. */
704 int trace_move;
705
706 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
707 #else
708 #define TRACE_MOVE(x) (void) 0
709 #endif
710
711 /* Non-zero means automatically scroll windows horizontally to make
712 point visible. */
713
714 int automatic_hscrolling_p;
715 Lisp_Object Qauto_hscroll_mode;
716
717 /* How close to the margin can point get before the window is scrolled
718 horizontally. */
719 EMACS_INT hscroll_margin;
720
721 /* How much to scroll horizontally when point is inside the above margin. */
722 Lisp_Object Vhscroll_step;
723
724 /* The variable `resize-mini-windows'. If nil, don't resize
725 mini-windows. If t, always resize them to fit the text they
726 display. If `grow-only', let mini-windows grow only until they
727 become empty. */
728
729 Lisp_Object Vresize_mini_windows;
730
731 /* Buffer being redisplayed -- for redisplay_window_error. */
732
733 struct buffer *displayed_buffer;
734
735 /* Space between overline and text. */
736
737 EMACS_INT overline_margin;
738
739 /* Require underline to be at least this many screen pixels below baseline
740 This to avoid underline "merging" with the base of letters at small
741 font sizes, particularly when x_use_underline_position_properties is on. */
742
743 EMACS_INT underline_minimum_offset;
744
745 /* Value returned from text property handlers (see below). */
746
747 enum prop_handled
748 {
749 HANDLED_NORMALLY,
750 HANDLED_RECOMPUTE_PROPS,
751 HANDLED_OVERLAY_STRING_CONSUMED,
752 HANDLED_RETURN
753 };
754
755 /* A description of text properties that redisplay is interested
756 in. */
757
758 struct props
759 {
760 /* The name of the property. */
761 Lisp_Object *name;
762
763 /* A unique index for the property. */
764 enum prop_idx idx;
765
766 /* A handler function called to set up iterator IT from the property
767 at IT's current position. Value is used to steer handle_stop. */
768 enum prop_handled (*handler) P_ ((struct it *it));
769 };
770
771 static enum prop_handled handle_face_prop P_ ((struct it *));
772 static enum prop_handled handle_invisible_prop P_ ((struct it *));
773 static enum prop_handled handle_display_prop P_ ((struct it *));
774 static enum prop_handled handle_composition_prop P_ ((struct it *));
775 static enum prop_handled handle_overlay_change P_ ((struct it *));
776 static enum prop_handled handle_fontified_prop P_ ((struct it *));
777
778 /* Properties handled by iterators. */
779
780 static struct props it_props[] =
781 {
782 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
783 /* Handle `face' before `display' because some sub-properties of
784 `display' need to know the face. */
785 {&Qface, FACE_PROP_IDX, handle_face_prop},
786 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
787 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
788 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
789 {NULL, 0, NULL}
790 };
791
792 /* Value is the position described by X. If X is a marker, value is
793 the marker_position of X. Otherwise, value is X. */
794
795 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
796
797 /* Enumeration returned by some move_it_.* functions internally. */
798
799 enum move_it_result
800 {
801 /* Not used. Undefined value. */
802 MOVE_UNDEFINED,
803
804 /* Move ended at the requested buffer position or ZV. */
805 MOVE_POS_MATCH_OR_ZV,
806
807 /* Move ended at the requested X pixel position. */
808 MOVE_X_REACHED,
809
810 /* Move within a line ended at the end of a line that must be
811 continued. */
812 MOVE_LINE_CONTINUED,
813
814 /* Move within a line ended at the end of a line that would
815 be displayed truncated. */
816 MOVE_LINE_TRUNCATED,
817
818 /* Move within a line ended at a line end. */
819 MOVE_NEWLINE_OR_CR
820 };
821
822 /* This counter is used to clear the face cache every once in a while
823 in redisplay_internal. It is incremented for each redisplay.
824 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
825 cleared. */
826
827 #define CLEAR_FACE_CACHE_COUNT 500
828 static int clear_face_cache_count;
829
830 /* Similarly for the image cache. */
831
832 #ifdef HAVE_WINDOW_SYSTEM
833 #define CLEAR_IMAGE_CACHE_COUNT 101
834 static int clear_image_cache_count;
835 #endif
836
837 /* Non-zero while redisplay_internal is in progress. */
838
839 int redisplaying_p;
840
841 /* Non-zero means don't free realized faces. Bound while freeing
842 realized faces is dangerous because glyph matrices might still
843 reference them. */
844
845 int inhibit_free_realized_faces;
846 Lisp_Object Qinhibit_free_realized_faces;
847
848 /* If a string, XTread_socket generates an event to display that string.
849 (The display is done in read_char.) */
850
851 Lisp_Object help_echo_string;
852 Lisp_Object help_echo_window;
853 Lisp_Object help_echo_object;
854 int help_echo_pos;
855
856 /* Temporary variable for XTread_socket. */
857
858 Lisp_Object previous_help_echo_string;
859
860 /* Null glyph slice */
861
862 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
863
864 /* Platform-independent portion of hourglass implementation. */
865
866 /* Non-zero means we're allowed to display a hourglass pointer. */
867 int display_hourglass_p;
868
869 /* Non-zero means an hourglass cursor is currently shown. */
870 int hourglass_shown_p;
871
872 /* If non-null, an asynchronous timer that, when it expires, displays
873 an hourglass cursor on all frames. */
874 struct atimer *hourglass_atimer;
875
876 /* Number of seconds to wait before displaying an hourglass cursor. */
877 Lisp_Object Vhourglass_delay;
878
879 /* Default number of seconds to wait before displaying an hourglass
880 cursor. */
881 #define DEFAULT_HOURGLASS_DELAY 1
882
883 \f
884 /* Function prototypes. */
885
886 static void setup_for_ellipsis P_ ((struct it *, int));
887 static void mark_window_display_accurate_1 P_ ((struct window *, int));
888 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
889 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
890 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
891 static int redisplay_mode_lines P_ ((Lisp_Object, int));
892 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
893
894 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
895
896 static void handle_line_prefix P_ ((struct it *));
897
898 static void pint2str P_ ((char *, int, int));
899 static void pint2hrstr P_ ((char *, int, int));
900 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
901 struct text_pos));
902 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
903 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
904 static void store_mode_line_noprop_char P_ ((char));
905 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
906 static void x_consider_frame_title P_ ((Lisp_Object));
907 static void handle_stop P_ ((struct it *));
908 static void handle_stop_backwards P_ ((struct it *, EMACS_INT));
909 static int tool_bar_lines_needed P_ ((struct frame *, int *));
910 static int single_display_spec_intangible_p P_ ((Lisp_Object));
911 static void ensure_echo_area_buffers P_ ((void));
912 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
913 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
914 static int with_echo_area_buffer P_ ((struct window *, int,
915 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
916 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static void clear_garbaged_frames P_ ((void));
918 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
920 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
921 static int display_echo_area P_ ((struct window *));
922 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
923 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
924 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
925 static int string_char_and_length P_ ((const unsigned char *, int *));
926 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
927 struct text_pos));
928 static int compute_window_start_on_continuation_line P_ ((struct window *));
929 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
930 static void insert_left_trunc_glyphs P_ ((struct it *));
931 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
932 Lisp_Object));
933 static void extend_face_to_end_of_line P_ ((struct it *));
934 static int append_space_for_newline P_ ((struct it *, int));
935 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
936 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
937 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
938 static int trailing_whitespace_p P_ ((int));
939 static int message_log_check_duplicate P_ ((int, int, int, int));
940 static void push_it P_ ((struct it *));
941 static void pop_it P_ ((struct it *));
942 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
943 static void select_frame_for_redisplay P_ ((Lisp_Object));
944 static void redisplay_internal P_ ((int));
945 static int echo_area_display P_ ((int));
946 static void redisplay_windows P_ ((Lisp_Object));
947 static void redisplay_window P_ ((Lisp_Object, int));
948 static Lisp_Object redisplay_window_error ();
949 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
950 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
951 static int update_menu_bar P_ ((struct frame *, int, int));
952 static int try_window_reusing_current_matrix P_ ((struct window *));
953 static int try_window_id P_ ((struct window *));
954 static int display_line P_ ((struct it *));
955 static int display_mode_lines P_ ((struct window *));
956 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
957 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
958 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
959 static char *decode_mode_spec P_ ((struct window *, int, int, int,
960 Lisp_Object *));
961 static void display_menu_bar P_ ((struct window *));
962 static int display_count_lines P_ ((int, int, int, int, int *));
963 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
964 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
965 static void compute_line_metrics P_ ((struct it *));
966 static void run_redisplay_end_trigger_hook P_ ((struct it *));
967 static int get_overlay_strings P_ ((struct it *, int));
968 static int get_overlay_strings_1 P_ ((struct it *, int, int));
969 static void next_overlay_string P_ ((struct it *));
970 static void reseat P_ ((struct it *, struct text_pos, int));
971 static void reseat_1 P_ ((struct it *, struct text_pos, int));
972 static void back_to_previous_visible_line_start P_ ((struct it *));
973 void reseat_at_previous_visible_line_start P_ ((struct it *));
974 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
975 static int next_element_from_ellipsis P_ ((struct it *));
976 static int next_element_from_display_vector P_ ((struct it *));
977 static int next_element_from_string P_ ((struct it *));
978 static int next_element_from_c_string P_ ((struct it *));
979 static int next_element_from_buffer P_ ((struct it *));
980 static int next_element_from_composition P_ ((struct it *));
981 static int next_element_from_image P_ ((struct it *));
982 static int next_element_from_stretch P_ ((struct it *));
983 static void load_overlay_strings P_ ((struct it *, int));
984 static int init_from_display_pos P_ ((struct it *, struct window *,
985 struct display_pos *));
986 static void reseat_to_string P_ ((struct it *, unsigned char *,
987 Lisp_Object, int, int, int, int));
988 static enum move_it_result
989 move_it_in_display_line_to (struct it *, EMACS_INT, int,
990 enum move_operation_enum);
991 void move_it_vertically_backward P_ ((struct it *, int));
992 static void init_to_row_start P_ ((struct it *, struct window *,
993 struct glyph_row *));
994 static int init_to_row_end P_ ((struct it *, struct window *,
995 struct glyph_row *));
996 static void back_to_previous_line_start P_ ((struct it *));
997 static int forward_to_next_line_start P_ ((struct it *, int *));
998 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
999 Lisp_Object, int));
1000 static struct text_pos string_pos P_ ((int, Lisp_Object));
1001 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
1002 static int number_of_chars P_ ((unsigned char *, int));
1003 static void compute_stop_pos P_ ((struct it *));
1004 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1005 Lisp_Object));
1006 static int face_before_or_after_it_pos P_ ((struct it *, int));
1007 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1008 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1009 Lisp_Object, Lisp_Object,
1010 struct text_pos *, int));
1011 static int underlying_face_id P_ ((struct it *));
1012 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1013 struct window *));
1014
1015 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1016 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1017
1018 #ifdef HAVE_WINDOW_SYSTEM
1019
1020 static void update_tool_bar P_ ((struct frame *, int));
1021 static void build_desired_tool_bar_string P_ ((struct frame *f));
1022 static int redisplay_tool_bar P_ ((struct frame *));
1023 static void display_tool_bar_line P_ ((struct it *, int));
1024 static void notice_overwritten_cursor P_ ((struct window *,
1025 enum glyph_row_area,
1026 int, int, int, int));
1027
1028
1029
1030 #endif /* HAVE_WINDOW_SYSTEM */
1031
1032 \f
1033 /***********************************************************************
1034 Window display dimensions
1035 ***********************************************************************/
1036
1037 /* Return the bottom boundary y-position for text lines in window W.
1038 This is the first y position at which a line cannot start.
1039 It is relative to the top of the window.
1040
1041 This is the height of W minus the height of a mode line, if any. */
1042
1043 INLINE int
1044 window_text_bottom_y (w)
1045 struct window *w;
1046 {
1047 int height = WINDOW_TOTAL_HEIGHT (w);
1048
1049 if (WINDOW_WANTS_MODELINE_P (w))
1050 height -= CURRENT_MODE_LINE_HEIGHT (w);
1051 return height;
1052 }
1053
1054 /* Return the pixel width of display area AREA of window W. AREA < 0
1055 means return the total width of W, not including fringes to
1056 the left and right of the window. */
1057
1058 INLINE int
1059 window_box_width (w, area)
1060 struct window *w;
1061 int area;
1062 {
1063 int cols = XFASTINT (w->total_cols);
1064 int pixels = 0;
1065
1066 if (!w->pseudo_window_p)
1067 {
1068 cols -= WINDOW_SCROLL_BAR_COLS (w);
1069
1070 if (area == TEXT_AREA)
1071 {
1072 if (INTEGERP (w->left_margin_cols))
1073 cols -= XFASTINT (w->left_margin_cols);
1074 if (INTEGERP (w->right_margin_cols))
1075 cols -= XFASTINT (w->right_margin_cols);
1076 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1077 }
1078 else if (area == LEFT_MARGIN_AREA)
1079 {
1080 cols = (INTEGERP (w->left_margin_cols)
1081 ? XFASTINT (w->left_margin_cols) : 0);
1082 pixels = 0;
1083 }
1084 else if (area == RIGHT_MARGIN_AREA)
1085 {
1086 cols = (INTEGERP (w->right_margin_cols)
1087 ? XFASTINT (w->right_margin_cols) : 0);
1088 pixels = 0;
1089 }
1090 }
1091
1092 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1093 }
1094
1095
1096 /* Return the pixel height of the display area of window W, not
1097 including mode lines of W, if any. */
1098
1099 INLINE int
1100 window_box_height (w)
1101 struct window *w;
1102 {
1103 struct frame *f = XFRAME (w->frame);
1104 int height = WINDOW_TOTAL_HEIGHT (w);
1105
1106 xassert (height >= 0);
1107
1108 /* Note: the code below that determines the mode-line/header-line
1109 height is essentially the same as that contained in the macro
1110 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1111 the appropriate glyph row has its `mode_line_p' flag set,
1112 and if it doesn't, uses estimate_mode_line_height instead. */
1113
1114 if (WINDOW_WANTS_MODELINE_P (w))
1115 {
1116 struct glyph_row *ml_row
1117 = (w->current_matrix && w->current_matrix->rows
1118 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1119 : 0);
1120 if (ml_row && ml_row->mode_line_p)
1121 height -= ml_row->height;
1122 else
1123 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1124 }
1125
1126 if (WINDOW_WANTS_HEADER_LINE_P (w))
1127 {
1128 struct glyph_row *hl_row
1129 = (w->current_matrix && w->current_matrix->rows
1130 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1131 : 0);
1132 if (hl_row && hl_row->mode_line_p)
1133 height -= hl_row->height;
1134 else
1135 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1136 }
1137
1138 /* With a very small font and a mode-line that's taller than
1139 default, we might end up with a negative height. */
1140 return max (0, height);
1141 }
1142
1143 /* Return the window-relative coordinate of the left edge of display
1144 area AREA of window W. AREA < 0 means return the left edge of the
1145 whole window, to the right of the left fringe of W. */
1146
1147 INLINE int
1148 window_box_left_offset (w, area)
1149 struct window *w;
1150 int area;
1151 {
1152 int x;
1153
1154 if (w->pseudo_window_p)
1155 return 0;
1156
1157 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1158
1159 if (area == TEXT_AREA)
1160 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1161 + window_box_width (w, LEFT_MARGIN_AREA));
1162 else if (area == RIGHT_MARGIN_AREA)
1163 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1164 + window_box_width (w, LEFT_MARGIN_AREA)
1165 + window_box_width (w, TEXT_AREA)
1166 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1167 ? 0
1168 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1169 else if (area == LEFT_MARGIN_AREA
1170 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1171 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1172
1173 return x;
1174 }
1175
1176
1177 /* Return the window-relative coordinate of the right edge of display
1178 area AREA of window W. AREA < 0 means return the left edge of the
1179 whole window, to the left of the right fringe of W. */
1180
1181 INLINE int
1182 window_box_right_offset (w, area)
1183 struct window *w;
1184 int area;
1185 {
1186 return window_box_left_offset (w, area) + window_box_width (w, area);
1187 }
1188
1189 /* Return the frame-relative coordinate of the left edge of display
1190 area AREA of window W. AREA < 0 means return the left edge of the
1191 whole window, to the right of the left fringe of W. */
1192
1193 INLINE int
1194 window_box_left (w, area)
1195 struct window *w;
1196 int area;
1197 {
1198 struct frame *f = XFRAME (w->frame);
1199 int x;
1200
1201 if (w->pseudo_window_p)
1202 return FRAME_INTERNAL_BORDER_WIDTH (f);
1203
1204 x = (WINDOW_LEFT_EDGE_X (w)
1205 + window_box_left_offset (w, area));
1206
1207 return x;
1208 }
1209
1210
1211 /* Return the frame-relative coordinate of the right edge of display
1212 area AREA of window W. AREA < 0 means return the left edge of the
1213 whole window, to the left of the right fringe of W. */
1214
1215 INLINE int
1216 window_box_right (w, area)
1217 struct window *w;
1218 int area;
1219 {
1220 return window_box_left (w, area) + window_box_width (w, area);
1221 }
1222
1223 /* Get the bounding box of the display area AREA of window W, without
1224 mode lines, in frame-relative coordinates. AREA < 0 means the
1225 whole window, not including the left and right fringes of
1226 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1227 coordinates of the upper-left corner of the box. Return in
1228 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1229
1230 INLINE void
1231 window_box (w, area, box_x, box_y, box_width, box_height)
1232 struct window *w;
1233 int area;
1234 int *box_x, *box_y, *box_width, *box_height;
1235 {
1236 if (box_width)
1237 *box_width = window_box_width (w, area);
1238 if (box_height)
1239 *box_height = window_box_height (w);
1240 if (box_x)
1241 *box_x = window_box_left (w, area);
1242 if (box_y)
1243 {
1244 *box_y = WINDOW_TOP_EDGE_Y (w);
1245 if (WINDOW_WANTS_HEADER_LINE_P (w))
1246 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1247 }
1248 }
1249
1250
1251 /* Get the bounding box of the display area AREA of window W, without
1252 mode lines. AREA < 0 means the whole window, not including the
1253 left and right fringe of the window. Return in *TOP_LEFT_X
1254 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1255 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1256 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1257 box. */
1258
1259 INLINE void
1260 window_box_edges (w, area, top_left_x, top_left_y,
1261 bottom_right_x, bottom_right_y)
1262 struct window *w;
1263 int area;
1264 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1265 {
1266 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1267 bottom_right_y);
1268 *bottom_right_x += *top_left_x;
1269 *bottom_right_y += *top_left_y;
1270 }
1271
1272
1273 \f
1274 /***********************************************************************
1275 Utilities
1276 ***********************************************************************/
1277
1278 /* Return the bottom y-position of the line the iterator IT is in.
1279 This can modify IT's settings. */
1280
1281 int
1282 line_bottom_y (it)
1283 struct it *it;
1284 {
1285 int line_height = it->max_ascent + it->max_descent;
1286 int line_top_y = it->current_y;
1287
1288 if (line_height == 0)
1289 {
1290 if (last_height)
1291 line_height = last_height;
1292 else if (IT_CHARPOS (*it) < ZV)
1293 {
1294 move_it_by_lines (it, 1, 1);
1295 line_height = (it->max_ascent || it->max_descent
1296 ? it->max_ascent + it->max_descent
1297 : last_height);
1298 }
1299 else
1300 {
1301 struct glyph_row *row = it->glyph_row;
1302
1303 /* Use the default character height. */
1304 it->glyph_row = NULL;
1305 it->what = IT_CHARACTER;
1306 it->c = ' ';
1307 it->len = 1;
1308 PRODUCE_GLYPHS (it);
1309 line_height = it->ascent + it->descent;
1310 it->glyph_row = row;
1311 }
1312 }
1313
1314 return line_top_y + line_height;
1315 }
1316
1317
1318 /* Return 1 if position CHARPOS is visible in window W.
1319 CHARPOS < 0 means return info about WINDOW_END position.
1320 If visible, set *X and *Y to pixel coordinates of top left corner.
1321 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1322 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1323
1324 int
1325 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1326 struct window *w;
1327 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1328 {
1329 struct it it;
1330 struct text_pos top;
1331 int visible_p = 0;
1332 struct buffer *old_buffer = NULL;
1333
1334 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1335 return visible_p;
1336
1337 if (XBUFFER (w->buffer) != current_buffer)
1338 {
1339 old_buffer = current_buffer;
1340 set_buffer_internal_1 (XBUFFER (w->buffer));
1341 }
1342
1343 SET_TEXT_POS_FROM_MARKER (top, w->start);
1344
1345 /* Compute exact mode line heights. */
1346 if (WINDOW_WANTS_MODELINE_P (w))
1347 current_mode_line_height
1348 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1349 current_buffer->mode_line_format);
1350
1351 if (WINDOW_WANTS_HEADER_LINE_P (w))
1352 current_header_line_height
1353 = display_mode_line (w, HEADER_LINE_FACE_ID,
1354 current_buffer->header_line_format);
1355
1356 start_display (&it, w, top);
1357 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1358 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1359
1360 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1361 {
1362 /* We have reached CHARPOS, or passed it. How the call to
1363 move_it_to can overshoot: (i) If CHARPOS is on invisible
1364 text, move_it_to stops at the end of the invisible text,
1365 after CHARPOS. (ii) If CHARPOS is in a display vector,
1366 move_it_to stops on its last glyph. */
1367 int top_x = it.current_x;
1368 int top_y = it.current_y;
1369 enum it_method it_method = it.method;
1370 /* Calling line_bottom_y may change it.method, it.position, etc. */
1371 int bottom_y = (last_height = 0, line_bottom_y (&it));
1372 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1373
1374 if (top_y < window_top_y)
1375 visible_p = bottom_y > window_top_y;
1376 else if (top_y < it.last_visible_y)
1377 visible_p = 1;
1378 if (visible_p)
1379 {
1380 if (it_method == GET_FROM_BUFFER)
1381 {
1382 Lisp_Object window, prop;
1383
1384 XSETWINDOW (window, w);
1385 prop = Fget_char_property (make_number (charpos),
1386 Qinvisible, window);
1387
1388 /* If charpos coincides with invisible text covered with an
1389 ellipsis, use the first glyph of the ellipsis to compute
1390 the pixel positions. */
1391 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1392 {
1393 struct glyph_row *row = it.glyph_row;
1394 struct glyph *glyph = row->glyphs[TEXT_AREA];
1395 struct glyph *end = glyph + row->used[TEXT_AREA];
1396 int x = row->x;
1397
1398 for (; glyph < end
1399 && (!BUFFERP (glyph->object)
1400 || glyph->charpos < charpos);
1401 glyph++)
1402 x += glyph->pixel_width;
1403 top_x = x;
1404 }
1405 }
1406 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1407 {
1408 /* We stopped on the last glyph of a display vector.
1409 Try and recompute. Hack alert! */
1410 if (charpos < 2 || top.charpos >= charpos)
1411 top_x = it.glyph_row->x;
1412 else
1413 {
1414 struct it it2;
1415 start_display (&it2, w, top);
1416 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1417 get_next_display_element (&it2);
1418 PRODUCE_GLYPHS (&it2);
1419 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1420 || it2.current_x > it2.last_visible_x)
1421 top_x = it.glyph_row->x;
1422 else
1423 {
1424 top_x = it2.current_x;
1425 top_y = it2.current_y;
1426 }
1427 }
1428 }
1429
1430 *x = top_x;
1431 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1432 *rtop = max (0, window_top_y - top_y);
1433 *rbot = max (0, bottom_y - it.last_visible_y);
1434 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1435 - max (top_y, window_top_y)));
1436 *vpos = it.vpos;
1437 }
1438 }
1439 else
1440 {
1441 struct it it2;
1442
1443 it2 = it;
1444 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1445 move_it_by_lines (&it, 1, 0);
1446 if (charpos < IT_CHARPOS (it)
1447 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1448 {
1449 visible_p = 1;
1450 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1451 *x = it2.current_x;
1452 *y = it2.current_y + it2.max_ascent - it2.ascent;
1453 *rtop = max (0, -it2.current_y);
1454 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1455 - it.last_visible_y));
1456 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1457 it.last_visible_y)
1458 - max (it2.current_y,
1459 WINDOW_HEADER_LINE_HEIGHT (w))));
1460 *vpos = it2.vpos;
1461 }
1462 }
1463
1464 if (old_buffer)
1465 set_buffer_internal_1 (old_buffer);
1466
1467 current_header_line_height = current_mode_line_height = -1;
1468
1469 if (visible_p && XFASTINT (w->hscroll) > 0)
1470 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1471
1472 #if 0
1473 /* Debugging code. */
1474 if (visible_p)
1475 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1476 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1477 else
1478 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1479 #endif
1480
1481 return visible_p;
1482 }
1483
1484
1485 /* Return the next character from STR which is MAXLEN bytes long.
1486 Return in *LEN the length of the character. This is like
1487 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1488 we find one, we return a `?', but with the length of the invalid
1489 character. */
1490
1491 static INLINE int
1492 string_char_and_length (str, len)
1493 const unsigned char *str;
1494 int *len;
1495 {
1496 int c;
1497
1498 c = STRING_CHAR_AND_LENGTH (str, *len);
1499 if (!CHAR_VALID_P (c, 1))
1500 /* We may not change the length here because other places in Emacs
1501 don't use this function, i.e. they silently accept invalid
1502 characters. */
1503 c = '?';
1504
1505 return c;
1506 }
1507
1508
1509
1510 /* Given a position POS containing a valid character and byte position
1511 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1512
1513 static struct text_pos
1514 string_pos_nchars_ahead (pos, string, nchars)
1515 struct text_pos pos;
1516 Lisp_Object string;
1517 int nchars;
1518 {
1519 xassert (STRINGP (string) && nchars >= 0);
1520
1521 if (STRING_MULTIBYTE (string))
1522 {
1523 int rest = SBYTES (string) - BYTEPOS (pos);
1524 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1525 int len;
1526
1527 while (nchars--)
1528 {
1529 string_char_and_length (p, &len);
1530 p += len, rest -= len;
1531 xassert (rest >= 0);
1532 CHARPOS (pos) += 1;
1533 BYTEPOS (pos) += len;
1534 }
1535 }
1536 else
1537 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1538
1539 return pos;
1540 }
1541
1542
1543 /* Value is the text position, i.e. character and byte position,
1544 for character position CHARPOS in STRING. */
1545
1546 static INLINE struct text_pos
1547 string_pos (charpos, string)
1548 int charpos;
1549 Lisp_Object string;
1550 {
1551 struct text_pos pos;
1552 xassert (STRINGP (string));
1553 xassert (charpos >= 0);
1554 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1555 return pos;
1556 }
1557
1558
1559 /* Value is a text position, i.e. character and byte position, for
1560 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1561 means recognize multibyte characters. */
1562
1563 static struct text_pos
1564 c_string_pos (charpos, s, multibyte_p)
1565 int charpos;
1566 unsigned char *s;
1567 int multibyte_p;
1568 {
1569 struct text_pos pos;
1570
1571 xassert (s != NULL);
1572 xassert (charpos >= 0);
1573
1574 if (multibyte_p)
1575 {
1576 int rest = strlen (s), len;
1577
1578 SET_TEXT_POS (pos, 0, 0);
1579 while (charpos--)
1580 {
1581 string_char_and_length (s, &len);
1582 s += len, rest -= len;
1583 xassert (rest >= 0);
1584 CHARPOS (pos) += 1;
1585 BYTEPOS (pos) += len;
1586 }
1587 }
1588 else
1589 SET_TEXT_POS (pos, charpos, charpos);
1590
1591 return pos;
1592 }
1593
1594
1595 /* Value is the number of characters in C string S. MULTIBYTE_P
1596 non-zero means recognize multibyte characters. */
1597
1598 static int
1599 number_of_chars (s, multibyte_p)
1600 unsigned char *s;
1601 int multibyte_p;
1602 {
1603 int nchars;
1604
1605 if (multibyte_p)
1606 {
1607 int rest = strlen (s), len;
1608 unsigned char *p = (unsigned char *) s;
1609
1610 for (nchars = 0; rest > 0; ++nchars)
1611 {
1612 string_char_and_length (p, &len);
1613 rest -= len, p += len;
1614 }
1615 }
1616 else
1617 nchars = strlen (s);
1618
1619 return nchars;
1620 }
1621
1622
1623 /* Compute byte position NEWPOS->bytepos corresponding to
1624 NEWPOS->charpos. POS is a known position in string STRING.
1625 NEWPOS->charpos must be >= POS.charpos. */
1626
1627 static void
1628 compute_string_pos (newpos, pos, string)
1629 struct text_pos *newpos, pos;
1630 Lisp_Object string;
1631 {
1632 xassert (STRINGP (string));
1633 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1634
1635 if (STRING_MULTIBYTE (string))
1636 *newpos = string_pos_nchars_ahead (pos, string,
1637 CHARPOS (*newpos) - CHARPOS (pos));
1638 else
1639 BYTEPOS (*newpos) = CHARPOS (*newpos);
1640 }
1641
1642 /* EXPORT:
1643 Return an estimation of the pixel height of mode or header lines on
1644 frame F. FACE_ID specifies what line's height to estimate. */
1645
1646 int
1647 estimate_mode_line_height (f, face_id)
1648 struct frame *f;
1649 enum face_id face_id;
1650 {
1651 #ifdef HAVE_WINDOW_SYSTEM
1652 if (FRAME_WINDOW_P (f))
1653 {
1654 int height = FONT_HEIGHT (FRAME_FONT (f));
1655
1656 /* This function is called so early when Emacs starts that the face
1657 cache and mode line face are not yet initialized. */
1658 if (FRAME_FACE_CACHE (f))
1659 {
1660 struct face *face = FACE_FROM_ID (f, face_id);
1661 if (face)
1662 {
1663 if (face->font)
1664 height = FONT_HEIGHT (face->font);
1665 if (face->box_line_width > 0)
1666 height += 2 * face->box_line_width;
1667 }
1668 }
1669
1670 return height;
1671 }
1672 #endif
1673
1674 return 1;
1675 }
1676
1677 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1678 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1679 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1680 not force the value into range. */
1681
1682 void
1683 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1684 FRAME_PTR f;
1685 register int pix_x, pix_y;
1686 int *x, *y;
1687 NativeRectangle *bounds;
1688 int noclip;
1689 {
1690
1691 #ifdef HAVE_WINDOW_SYSTEM
1692 if (FRAME_WINDOW_P (f))
1693 {
1694 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1695 even for negative values. */
1696 if (pix_x < 0)
1697 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1698 if (pix_y < 0)
1699 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1700
1701 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1702 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1703
1704 if (bounds)
1705 STORE_NATIVE_RECT (*bounds,
1706 FRAME_COL_TO_PIXEL_X (f, pix_x),
1707 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1708 FRAME_COLUMN_WIDTH (f) - 1,
1709 FRAME_LINE_HEIGHT (f) - 1);
1710
1711 if (!noclip)
1712 {
1713 if (pix_x < 0)
1714 pix_x = 0;
1715 else if (pix_x > FRAME_TOTAL_COLS (f))
1716 pix_x = FRAME_TOTAL_COLS (f);
1717
1718 if (pix_y < 0)
1719 pix_y = 0;
1720 else if (pix_y > FRAME_LINES (f))
1721 pix_y = FRAME_LINES (f);
1722 }
1723 }
1724 #endif
1725
1726 *x = pix_x;
1727 *y = pix_y;
1728 }
1729
1730
1731 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1732 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1733 can't tell the positions because W's display is not up to date,
1734 return 0. */
1735
1736 int
1737 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1738 struct window *w;
1739 int hpos, vpos;
1740 int *frame_x, *frame_y;
1741 {
1742 #ifdef HAVE_WINDOW_SYSTEM
1743 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1744 {
1745 int success_p;
1746
1747 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1748 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1749
1750 if (display_completed)
1751 {
1752 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1753 struct glyph *glyph = row->glyphs[TEXT_AREA];
1754 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1755
1756 hpos = row->x;
1757 vpos = row->y;
1758 while (glyph < end)
1759 {
1760 hpos += glyph->pixel_width;
1761 ++glyph;
1762 }
1763
1764 /* If first glyph is partially visible, its first visible position is still 0. */
1765 if (hpos < 0)
1766 hpos = 0;
1767
1768 success_p = 1;
1769 }
1770 else
1771 {
1772 hpos = vpos = 0;
1773 success_p = 0;
1774 }
1775
1776 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1777 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1778 return success_p;
1779 }
1780 #endif
1781
1782 *frame_x = hpos;
1783 *frame_y = vpos;
1784 return 1;
1785 }
1786
1787
1788 #ifdef HAVE_WINDOW_SYSTEM
1789
1790 /* Find the glyph under window-relative coordinates X/Y in window W.
1791 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1792 strings. Return in *HPOS and *VPOS the row and column number of
1793 the glyph found. Return in *AREA the glyph area containing X.
1794 Value is a pointer to the glyph found or null if X/Y is not on
1795 text, or we can't tell because W's current matrix is not up to
1796 date. */
1797
1798 static
1799 struct glyph *
1800 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1801 struct window *w;
1802 int x, y;
1803 int *hpos, *vpos, *dx, *dy, *area;
1804 {
1805 struct glyph *glyph, *end;
1806 struct glyph_row *row = NULL;
1807 int x0, i;
1808
1809 /* Find row containing Y. Give up if some row is not enabled. */
1810 for (i = 0; i < w->current_matrix->nrows; ++i)
1811 {
1812 row = MATRIX_ROW (w->current_matrix, i);
1813 if (!row->enabled_p)
1814 return NULL;
1815 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1816 break;
1817 }
1818
1819 *vpos = i;
1820 *hpos = 0;
1821
1822 /* Give up if Y is not in the window. */
1823 if (i == w->current_matrix->nrows)
1824 return NULL;
1825
1826 /* Get the glyph area containing X. */
1827 if (w->pseudo_window_p)
1828 {
1829 *area = TEXT_AREA;
1830 x0 = 0;
1831 }
1832 else
1833 {
1834 if (x < window_box_left_offset (w, TEXT_AREA))
1835 {
1836 *area = LEFT_MARGIN_AREA;
1837 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1838 }
1839 else if (x < window_box_right_offset (w, TEXT_AREA))
1840 {
1841 *area = TEXT_AREA;
1842 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1843 }
1844 else
1845 {
1846 *area = RIGHT_MARGIN_AREA;
1847 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1848 }
1849 }
1850
1851 /* Find glyph containing X. */
1852 glyph = row->glyphs[*area];
1853 end = glyph + row->used[*area];
1854 x -= x0;
1855 while (glyph < end && x >= glyph->pixel_width)
1856 {
1857 x -= glyph->pixel_width;
1858 ++glyph;
1859 }
1860
1861 if (glyph == end)
1862 return NULL;
1863
1864 if (dx)
1865 {
1866 *dx = x;
1867 *dy = y - (row->y + row->ascent - glyph->ascent);
1868 }
1869
1870 *hpos = glyph - row->glyphs[*area];
1871 return glyph;
1872 }
1873
1874
1875 /* EXPORT:
1876 Convert frame-relative x/y to coordinates relative to window W.
1877 Takes pseudo-windows into account. */
1878
1879 void
1880 frame_to_window_pixel_xy (w, x, y)
1881 struct window *w;
1882 int *x, *y;
1883 {
1884 if (w->pseudo_window_p)
1885 {
1886 /* A pseudo-window is always full-width, and starts at the
1887 left edge of the frame, plus a frame border. */
1888 struct frame *f = XFRAME (w->frame);
1889 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1890 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1891 }
1892 else
1893 {
1894 *x -= WINDOW_LEFT_EDGE_X (w);
1895 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1896 }
1897 }
1898
1899 /* EXPORT:
1900 Return in RECTS[] at most N clipping rectangles for glyph string S.
1901 Return the number of stored rectangles. */
1902
1903 int
1904 get_glyph_string_clip_rects (s, rects, n)
1905 struct glyph_string *s;
1906 NativeRectangle *rects;
1907 int n;
1908 {
1909 XRectangle r;
1910
1911 if (n <= 0)
1912 return 0;
1913
1914 if (s->row->full_width_p)
1915 {
1916 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1917 r.x = WINDOW_LEFT_EDGE_X (s->w);
1918 r.width = WINDOW_TOTAL_WIDTH (s->w);
1919
1920 /* Unless displaying a mode or menu bar line, which are always
1921 fully visible, clip to the visible part of the row. */
1922 if (s->w->pseudo_window_p)
1923 r.height = s->row->visible_height;
1924 else
1925 r.height = s->height;
1926 }
1927 else
1928 {
1929 /* This is a text line that may be partially visible. */
1930 r.x = window_box_left (s->w, s->area);
1931 r.width = window_box_width (s->w, s->area);
1932 r.height = s->row->visible_height;
1933 }
1934
1935 if (s->clip_head)
1936 if (r.x < s->clip_head->x)
1937 {
1938 if (r.width >= s->clip_head->x - r.x)
1939 r.width -= s->clip_head->x - r.x;
1940 else
1941 r.width = 0;
1942 r.x = s->clip_head->x;
1943 }
1944 if (s->clip_tail)
1945 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1946 {
1947 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1948 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1949 else
1950 r.width = 0;
1951 }
1952
1953 /* If S draws overlapping rows, it's sufficient to use the top and
1954 bottom of the window for clipping because this glyph string
1955 intentionally draws over other lines. */
1956 if (s->for_overlaps)
1957 {
1958 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1959 r.height = window_text_bottom_y (s->w) - r.y;
1960
1961 /* Alas, the above simple strategy does not work for the
1962 environments with anti-aliased text: if the same text is
1963 drawn onto the same place multiple times, it gets thicker.
1964 If the overlap we are processing is for the erased cursor, we
1965 take the intersection with the rectagle of the cursor. */
1966 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1967 {
1968 XRectangle rc, r_save = r;
1969
1970 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1971 rc.y = s->w->phys_cursor.y;
1972 rc.width = s->w->phys_cursor_width;
1973 rc.height = s->w->phys_cursor_height;
1974
1975 x_intersect_rectangles (&r_save, &rc, &r);
1976 }
1977 }
1978 else
1979 {
1980 /* Don't use S->y for clipping because it doesn't take partially
1981 visible lines into account. For example, it can be negative for
1982 partially visible lines at the top of a window. */
1983 if (!s->row->full_width_p
1984 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1985 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1986 else
1987 r.y = max (0, s->row->y);
1988 }
1989
1990 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1991
1992 /* If drawing the cursor, don't let glyph draw outside its
1993 advertised boundaries. Cleartype does this under some circumstances. */
1994 if (s->hl == DRAW_CURSOR)
1995 {
1996 struct glyph *glyph = s->first_glyph;
1997 int height, max_y;
1998
1999 if (s->x > r.x)
2000 {
2001 r.width -= s->x - r.x;
2002 r.x = s->x;
2003 }
2004 r.width = min (r.width, glyph->pixel_width);
2005
2006 /* If r.y is below window bottom, ensure that we still see a cursor. */
2007 height = min (glyph->ascent + glyph->descent,
2008 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2009 max_y = window_text_bottom_y (s->w) - height;
2010 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2011 if (s->ybase - glyph->ascent > max_y)
2012 {
2013 r.y = max_y;
2014 r.height = height;
2015 }
2016 else
2017 {
2018 /* Don't draw cursor glyph taller than our actual glyph. */
2019 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2020 if (height < r.height)
2021 {
2022 max_y = r.y + r.height;
2023 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2024 r.height = min (max_y - r.y, height);
2025 }
2026 }
2027 }
2028
2029 if (s->row->clip)
2030 {
2031 XRectangle r_save = r;
2032
2033 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2034 r.width = 0;
2035 }
2036
2037 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2038 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2039 {
2040 #ifdef CONVERT_FROM_XRECT
2041 CONVERT_FROM_XRECT (r, *rects);
2042 #else
2043 *rects = r;
2044 #endif
2045 return 1;
2046 }
2047 else
2048 {
2049 /* If we are processing overlapping and allowed to return
2050 multiple clipping rectangles, we exclude the row of the glyph
2051 string from the clipping rectangle. This is to avoid drawing
2052 the same text on the environment with anti-aliasing. */
2053 #ifdef CONVERT_FROM_XRECT
2054 XRectangle rs[2];
2055 #else
2056 XRectangle *rs = rects;
2057 #endif
2058 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2059
2060 if (s->for_overlaps & OVERLAPS_PRED)
2061 {
2062 rs[i] = r;
2063 if (r.y + r.height > row_y)
2064 {
2065 if (r.y < row_y)
2066 rs[i].height = row_y - r.y;
2067 else
2068 rs[i].height = 0;
2069 }
2070 i++;
2071 }
2072 if (s->for_overlaps & OVERLAPS_SUCC)
2073 {
2074 rs[i] = r;
2075 if (r.y < row_y + s->row->visible_height)
2076 {
2077 if (r.y + r.height > row_y + s->row->visible_height)
2078 {
2079 rs[i].y = row_y + s->row->visible_height;
2080 rs[i].height = r.y + r.height - rs[i].y;
2081 }
2082 else
2083 rs[i].height = 0;
2084 }
2085 i++;
2086 }
2087
2088 n = i;
2089 #ifdef CONVERT_FROM_XRECT
2090 for (i = 0; i < n; i++)
2091 CONVERT_FROM_XRECT (rs[i], rects[i]);
2092 #endif
2093 return n;
2094 }
2095 }
2096
2097 /* EXPORT:
2098 Return in *NR the clipping rectangle for glyph string S. */
2099
2100 void
2101 get_glyph_string_clip_rect (s, nr)
2102 struct glyph_string *s;
2103 NativeRectangle *nr;
2104 {
2105 get_glyph_string_clip_rects (s, nr, 1);
2106 }
2107
2108
2109 /* EXPORT:
2110 Return the position and height of the phys cursor in window W.
2111 Set w->phys_cursor_width to width of phys cursor.
2112 */
2113
2114 void
2115 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2116 struct window *w;
2117 struct glyph_row *row;
2118 struct glyph *glyph;
2119 int *xp, *yp, *heightp;
2120 {
2121 struct frame *f = XFRAME (WINDOW_FRAME (w));
2122 int x, y, wd, h, h0, y0;
2123
2124 /* Compute the width of the rectangle to draw. If on a stretch
2125 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2126 rectangle as wide as the glyph, but use a canonical character
2127 width instead. */
2128 wd = glyph->pixel_width - 1;
2129 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2130 wd++; /* Why? */
2131 #endif
2132
2133 x = w->phys_cursor.x;
2134 if (x < 0)
2135 {
2136 wd += x;
2137 x = 0;
2138 }
2139
2140 if (glyph->type == STRETCH_GLYPH
2141 && !x_stretch_cursor_p)
2142 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2143 w->phys_cursor_width = wd;
2144
2145 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2146
2147 /* If y is below window bottom, ensure that we still see a cursor. */
2148 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2149
2150 h = max (h0, glyph->ascent + glyph->descent);
2151 h0 = min (h0, glyph->ascent + glyph->descent);
2152
2153 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2154 if (y < y0)
2155 {
2156 h = max (h - (y0 - y) + 1, h0);
2157 y = y0 - 1;
2158 }
2159 else
2160 {
2161 y0 = window_text_bottom_y (w) - h0;
2162 if (y > y0)
2163 {
2164 h += y - y0;
2165 y = y0;
2166 }
2167 }
2168
2169 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2170 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2171 *heightp = h;
2172 }
2173
2174 /*
2175 * Remember which glyph the mouse is over.
2176 */
2177
2178 void
2179 remember_mouse_glyph (f, gx, gy, rect)
2180 struct frame *f;
2181 int gx, gy;
2182 NativeRectangle *rect;
2183 {
2184 Lisp_Object window;
2185 struct window *w;
2186 struct glyph_row *r, *gr, *end_row;
2187 enum window_part part;
2188 enum glyph_row_area area;
2189 int x, y, width, height;
2190
2191 /* Try to determine frame pixel position and size of the glyph under
2192 frame pixel coordinates X/Y on frame F. */
2193
2194 if (!f->glyphs_initialized_p
2195 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2196 NILP (window)))
2197 {
2198 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2199 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2200 goto virtual_glyph;
2201 }
2202
2203 w = XWINDOW (window);
2204 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2205 height = WINDOW_FRAME_LINE_HEIGHT (w);
2206
2207 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2208 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2209
2210 if (w->pseudo_window_p)
2211 {
2212 area = TEXT_AREA;
2213 part = ON_MODE_LINE; /* Don't adjust margin. */
2214 goto text_glyph;
2215 }
2216
2217 switch (part)
2218 {
2219 case ON_LEFT_MARGIN:
2220 area = LEFT_MARGIN_AREA;
2221 goto text_glyph;
2222
2223 case ON_RIGHT_MARGIN:
2224 area = RIGHT_MARGIN_AREA;
2225 goto text_glyph;
2226
2227 case ON_HEADER_LINE:
2228 case ON_MODE_LINE:
2229 gr = (part == ON_HEADER_LINE
2230 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2231 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2232 gy = gr->y;
2233 area = TEXT_AREA;
2234 goto text_glyph_row_found;
2235
2236 case ON_TEXT:
2237 area = TEXT_AREA;
2238
2239 text_glyph:
2240 gr = 0; gy = 0;
2241 for (; r <= end_row && r->enabled_p; ++r)
2242 if (r->y + r->height > y)
2243 {
2244 gr = r; gy = r->y;
2245 break;
2246 }
2247
2248 text_glyph_row_found:
2249 if (gr && gy <= y)
2250 {
2251 struct glyph *g = gr->glyphs[area];
2252 struct glyph *end = g + gr->used[area];
2253
2254 height = gr->height;
2255 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2256 if (gx + g->pixel_width > x)
2257 break;
2258
2259 if (g < end)
2260 {
2261 if (g->type == IMAGE_GLYPH)
2262 {
2263 /* Don't remember when mouse is over image, as
2264 image may have hot-spots. */
2265 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2266 return;
2267 }
2268 width = g->pixel_width;
2269 }
2270 else
2271 {
2272 /* Use nominal char spacing at end of line. */
2273 x -= gx;
2274 gx += (x / width) * width;
2275 }
2276
2277 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2278 gx += window_box_left_offset (w, area);
2279 }
2280 else
2281 {
2282 /* Use nominal line height at end of window. */
2283 gx = (x / width) * width;
2284 y -= gy;
2285 gy += (y / height) * height;
2286 }
2287 break;
2288
2289 case ON_LEFT_FRINGE:
2290 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2291 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2292 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2293 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2294 goto row_glyph;
2295
2296 case ON_RIGHT_FRINGE:
2297 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2298 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2299 : window_box_right_offset (w, TEXT_AREA));
2300 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2301 goto row_glyph;
2302
2303 case ON_SCROLL_BAR:
2304 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2305 ? 0
2306 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2307 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2308 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2309 : 0)));
2310 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2311
2312 row_glyph:
2313 gr = 0, gy = 0;
2314 for (; r <= end_row && r->enabled_p; ++r)
2315 if (r->y + r->height > y)
2316 {
2317 gr = r; gy = r->y;
2318 break;
2319 }
2320
2321 if (gr && gy <= y)
2322 height = gr->height;
2323 else
2324 {
2325 /* Use nominal line height at end of window. */
2326 y -= gy;
2327 gy += (y / height) * height;
2328 }
2329 break;
2330
2331 default:
2332 ;
2333 virtual_glyph:
2334 /* If there is no glyph under the mouse, then we divide the screen
2335 into a grid of the smallest glyph in the frame, and use that
2336 as our "glyph". */
2337
2338 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2339 round down even for negative values. */
2340 if (gx < 0)
2341 gx -= width - 1;
2342 if (gy < 0)
2343 gy -= height - 1;
2344
2345 gx = (gx / width) * width;
2346 gy = (gy / height) * height;
2347
2348 goto store_rect;
2349 }
2350
2351 gx += WINDOW_LEFT_EDGE_X (w);
2352 gy += WINDOW_TOP_EDGE_Y (w);
2353
2354 store_rect:
2355 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2356
2357 /* Visible feedback for debugging. */
2358 #if 0
2359 #if HAVE_X_WINDOWS
2360 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2361 f->output_data.x->normal_gc,
2362 gx, gy, width, height);
2363 #endif
2364 #endif
2365 }
2366
2367
2368 #endif /* HAVE_WINDOW_SYSTEM */
2369
2370 \f
2371 /***********************************************************************
2372 Lisp form evaluation
2373 ***********************************************************************/
2374
2375 /* Error handler for safe_eval and safe_call. */
2376
2377 static Lisp_Object
2378 safe_eval_handler (arg)
2379 Lisp_Object arg;
2380 {
2381 add_to_log ("Error during redisplay: %s", arg, Qnil);
2382 return Qnil;
2383 }
2384
2385
2386 /* Evaluate SEXPR and return the result, or nil if something went
2387 wrong. Prevent redisplay during the evaluation. */
2388
2389 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2390 Return the result, or nil if something went wrong. Prevent
2391 redisplay during the evaluation. */
2392
2393 Lisp_Object
2394 safe_call (nargs, args)
2395 int nargs;
2396 Lisp_Object *args;
2397 {
2398 Lisp_Object val;
2399
2400 if (inhibit_eval_during_redisplay)
2401 val = Qnil;
2402 else
2403 {
2404 int count = SPECPDL_INDEX ();
2405 struct gcpro gcpro1;
2406
2407 GCPRO1 (args[0]);
2408 gcpro1.nvars = nargs;
2409 specbind (Qinhibit_redisplay, Qt);
2410 /* Use Qt to ensure debugger does not run,
2411 so there is no possibility of wanting to redisplay. */
2412 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2413 safe_eval_handler);
2414 UNGCPRO;
2415 val = unbind_to (count, val);
2416 }
2417
2418 return val;
2419 }
2420
2421
2422 /* Call function FN with one argument ARG.
2423 Return the result, or nil if something went wrong. */
2424
2425 Lisp_Object
2426 safe_call1 (fn, arg)
2427 Lisp_Object fn, arg;
2428 {
2429 Lisp_Object args[2];
2430 args[0] = fn;
2431 args[1] = arg;
2432 return safe_call (2, args);
2433 }
2434
2435 static Lisp_Object Qeval;
2436
2437 Lisp_Object
2438 safe_eval (Lisp_Object sexpr)
2439 {
2440 return safe_call1 (Qeval, sexpr);
2441 }
2442
2443 /* Call function FN with one argument ARG.
2444 Return the result, or nil if something went wrong. */
2445
2446 Lisp_Object
2447 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2448 {
2449 Lisp_Object args[3];
2450 args[0] = fn;
2451 args[1] = arg1;
2452 args[2] = arg2;
2453 return safe_call (3, args);
2454 }
2455
2456
2457 \f
2458 /***********************************************************************
2459 Debugging
2460 ***********************************************************************/
2461
2462 #if 0
2463
2464 /* Define CHECK_IT to perform sanity checks on iterators.
2465 This is for debugging. It is too slow to do unconditionally. */
2466
2467 static void
2468 check_it (it)
2469 struct it *it;
2470 {
2471 if (it->method == GET_FROM_STRING)
2472 {
2473 xassert (STRINGP (it->string));
2474 xassert (IT_STRING_CHARPOS (*it) >= 0);
2475 }
2476 else
2477 {
2478 xassert (IT_STRING_CHARPOS (*it) < 0);
2479 if (it->method == GET_FROM_BUFFER)
2480 {
2481 /* Check that character and byte positions agree. */
2482 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2483 }
2484 }
2485
2486 if (it->dpvec)
2487 xassert (it->current.dpvec_index >= 0);
2488 else
2489 xassert (it->current.dpvec_index < 0);
2490 }
2491
2492 #define CHECK_IT(IT) check_it ((IT))
2493
2494 #else /* not 0 */
2495
2496 #define CHECK_IT(IT) (void) 0
2497
2498 #endif /* not 0 */
2499
2500
2501 #if GLYPH_DEBUG
2502
2503 /* Check that the window end of window W is what we expect it
2504 to be---the last row in the current matrix displaying text. */
2505
2506 static void
2507 check_window_end (w)
2508 struct window *w;
2509 {
2510 if (!MINI_WINDOW_P (w)
2511 && !NILP (w->window_end_valid))
2512 {
2513 struct glyph_row *row;
2514 xassert ((row = MATRIX_ROW (w->current_matrix,
2515 XFASTINT (w->window_end_vpos)),
2516 !row->enabled_p
2517 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2518 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2519 }
2520 }
2521
2522 #define CHECK_WINDOW_END(W) check_window_end ((W))
2523
2524 #else /* not GLYPH_DEBUG */
2525
2526 #define CHECK_WINDOW_END(W) (void) 0
2527
2528 #endif /* not GLYPH_DEBUG */
2529
2530
2531 \f
2532 /***********************************************************************
2533 Iterator initialization
2534 ***********************************************************************/
2535
2536 /* Initialize IT for displaying current_buffer in window W, starting
2537 at character position CHARPOS. CHARPOS < 0 means that no buffer
2538 position is specified which is useful when the iterator is assigned
2539 a position later. BYTEPOS is the byte position corresponding to
2540 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2541
2542 If ROW is not null, calls to produce_glyphs with IT as parameter
2543 will produce glyphs in that row.
2544
2545 BASE_FACE_ID is the id of a base face to use. It must be one of
2546 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2547 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2548 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2549
2550 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2551 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2552 will be initialized to use the corresponding mode line glyph row of
2553 the desired matrix of W. */
2554
2555 void
2556 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2557 struct it *it;
2558 struct window *w;
2559 int charpos, bytepos;
2560 struct glyph_row *row;
2561 enum face_id base_face_id;
2562 {
2563 int highlight_region_p;
2564 enum face_id remapped_base_face_id = base_face_id;
2565
2566 /* Some precondition checks. */
2567 xassert (w != NULL && it != NULL);
2568 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2569 && charpos <= ZV));
2570
2571 /* If face attributes have been changed since the last redisplay,
2572 free realized faces now because they depend on face definitions
2573 that might have changed. Don't free faces while there might be
2574 desired matrices pending which reference these faces. */
2575 if (face_change_count && !inhibit_free_realized_faces)
2576 {
2577 face_change_count = 0;
2578 free_all_realized_faces (Qnil);
2579 }
2580
2581 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2582 if (! NILP (Vface_remapping_alist))
2583 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2584
2585 /* Use one of the mode line rows of W's desired matrix if
2586 appropriate. */
2587 if (row == NULL)
2588 {
2589 if (base_face_id == MODE_LINE_FACE_ID
2590 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2591 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2592 else if (base_face_id == HEADER_LINE_FACE_ID)
2593 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2594 }
2595
2596 /* Clear IT. */
2597 bzero (it, sizeof *it);
2598 it->current.overlay_string_index = -1;
2599 it->current.dpvec_index = -1;
2600 it->base_face_id = remapped_base_face_id;
2601 it->string = Qnil;
2602 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2603
2604 /* The window in which we iterate over current_buffer: */
2605 XSETWINDOW (it->window, w);
2606 it->w = w;
2607 it->f = XFRAME (w->frame);
2608
2609 it->cmp_it.id = -1;
2610
2611 /* Extra space between lines (on window systems only). */
2612 if (base_face_id == DEFAULT_FACE_ID
2613 && FRAME_WINDOW_P (it->f))
2614 {
2615 if (NATNUMP (current_buffer->extra_line_spacing))
2616 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2617 else if (FLOATP (current_buffer->extra_line_spacing))
2618 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2619 * FRAME_LINE_HEIGHT (it->f));
2620 else if (it->f->extra_line_spacing > 0)
2621 it->extra_line_spacing = it->f->extra_line_spacing;
2622 it->max_extra_line_spacing = 0;
2623 }
2624
2625 /* If realized faces have been removed, e.g. because of face
2626 attribute changes of named faces, recompute them. When running
2627 in batch mode, the face cache of the initial frame is null. If
2628 we happen to get called, make a dummy face cache. */
2629 if (FRAME_FACE_CACHE (it->f) == NULL)
2630 init_frame_faces (it->f);
2631 if (FRAME_FACE_CACHE (it->f)->used == 0)
2632 recompute_basic_faces (it->f);
2633
2634 /* Current value of the `slice', `space-width', and 'height' properties. */
2635 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2636 it->space_width = Qnil;
2637 it->font_height = Qnil;
2638 it->override_ascent = -1;
2639
2640 /* Are control characters displayed as `^C'? */
2641 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2642
2643 /* -1 means everything between a CR and the following line end
2644 is invisible. >0 means lines indented more than this value are
2645 invisible. */
2646 it->selective = (INTEGERP (current_buffer->selective_display)
2647 ? XFASTINT (current_buffer->selective_display)
2648 : (!NILP (current_buffer->selective_display)
2649 ? -1 : 0));
2650 it->selective_display_ellipsis_p
2651 = !NILP (current_buffer->selective_display_ellipses);
2652
2653 /* Display table to use. */
2654 it->dp = window_display_table (w);
2655
2656 /* Are multibyte characters enabled in current_buffer? */
2657 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2658
2659 /* Do we need to reorder bidirectional text? */
2660 it->bidi_p = !NILP (current_buffer->bidi_display_reordering);
2661
2662 /* Non-zero if we should highlight the region. */
2663 highlight_region_p
2664 = (!NILP (Vtransient_mark_mode)
2665 && !NILP (current_buffer->mark_active)
2666 && XMARKER (current_buffer->mark)->buffer != 0);
2667
2668 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2669 start and end of a visible region in window IT->w. Set both to
2670 -1 to indicate no region. */
2671 if (highlight_region_p
2672 /* Maybe highlight only in selected window. */
2673 && (/* Either show region everywhere. */
2674 highlight_nonselected_windows
2675 /* Or show region in the selected window. */
2676 || w == XWINDOW (selected_window)
2677 /* Or show the region if we are in the mini-buffer and W is
2678 the window the mini-buffer refers to. */
2679 || (MINI_WINDOW_P (XWINDOW (selected_window))
2680 && WINDOWP (minibuf_selected_window)
2681 && w == XWINDOW (minibuf_selected_window))))
2682 {
2683 int charpos = marker_position (current_buffer->mark);
2684 it->region_beg_charpos = min (PT, charpos);
2685 it->region_end_charpos = max (PT, charpos);
2686 }
2687 else
2688 it->region_beg_charpos = it->region_end_charpos = -1;
2689
2690 /* Get the position at which the redisplay_end_trigger hook should
2691 be run, if it is to be run at all. */
2692 if (MARKERP (w->redisplay_end_trigger)
2693 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2694 it->redisplay_end_trigger_charpos
2695 = marker_position (w->redisplay_end_trigger);
2696 else if (INTEGERP (w->redisplay_end_trigger))
2697 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2698
2699 /* Correct bogus values of tab_width. */
2700 it->tab_width = XINT (current_buffer->tab_width);
2701 if (it->tab_width <= 0 || it->tab_width > 1000)
2702 it->tab_width = 8;
2703
2704 /* Are lines in the display truncated? */
2705 if (base_face_id != DEFAULT_FACE_ID
2706 || XINT (it->w->hscroll)
2707 || (! WINDOW_FULL_WIDTH_P (it->w)
2708 && ((!NILP (Vtruncate_partial_width_windows)
2709 && !INTEGERP (Vtruncate_partial_width_windows))
2710 || (INTEGERP (Vtruncate_partial_width_windows)
2711 && (WINDOW_TOTAL_COLS (it->w)
2712 < XINT (Vtruncate_partial_width_windows))))))
2713 it->line_wrap = TRUNCATE;
2714 else if (NILP (current_buffer->truncate_lines))
2715 it->line_wrap = NILP (current_buffer->word_wrap)
2716 ? WINDOW_WRAP : WORD_WRAP;
2717 else
2718 it->line_wrap = TRUNCATE;
2719
2720 /* Get dimensions of truncation and continuation glyphs. These are
2721 displayed as fringe bitmaps under X, so we don't need them for such
2722 frames. */
2723 if (!FRAME_WINDOW_P (it->f))
2724 {
2725 if (it->line_wrap == TRUNCATE)
2726 {
2727 /* We will need the truncation glyph. */
2728 xassert (it->glyph_row == NULL);
2729 produce_special_glyphs (it, IT_TRUNCATION);
2730 it->truncation_pixel_width = it->pixel_width;
2731 }
2732 else
2733 {
2734 /* We will need the continuation glyph. */
2735 xassert (it->glyph_row == NULL);
2736 produce_special_glyphs (it, IT_CONTINUATION);
2737 it->continuation_pixel_width = it->pixel_width;
2738 }
2739
2740 /* Reset these values to zero because the produce_special_glyphs
2741 above has changed them. */
2742 it->pixel_width = it->ascent = it->descent = 0;
2743 it->phys_ascent = it->phys_descent = 0;
2744 }
2745
2746 /* Set this after getting the dimensions of truncation and
2747 continuation glyphs, so that we don't produce glyphs when calling
2748 produce_special_glyphs, above. */
2749 it->glyph_row = row;
2750 it->area = TEXT_AREA;
2751
2752 /* Get the dimensions of the display area. The display area
2753 consists of the visible window area plus a horizontally scrolled
2754 part to the left of the window. All x-values are relative to the
2755 start of this total display area. */
2756 if (base_face_id != DEFAULT_FACE_ID)
2757 {
2758 /* Mode lines, menu bar in terminal frames. */
2759 it->first_visible_x = 0;
2760 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2761 }
2762 else
2763 {
2764 it->first_visible_x
2765 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2766 it->last_visible_x = (it->first_visible_x
2767 + window_box_width (w, TEXT_AREA));
2768
2769 /* If we truncate lines, leave room for the truncator glyph(s) at
2770 the right margin. Otherwise, leave room for the continuation
2771 glyph(s). Truncation and continuation glyphs are not inserted
2772 for window-based redisplay. */
2773 if (!FRAME_WINDOW_P (it->f))
2774 {
2775 if (it->line_wrap == TRUNCATE)
2776 it->last_visible_x -= it->truncation_pixel_width;
2777 else
2778 it->last_visible_x -= it->continuation_pixel_width;
2779 }
2780
2781 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2782 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2783 }
2784
2785 /* Leave room for a border glyph. */
2786 if (!FRAME_WINDOW_P (it->f)
2787 && !WINDOW_RIGHTMOST_P (it->w))
2788 it->last_visible_x -= 1;
2789
2790 it->last_visible_y = window_text_bottom_y (w);
2791
2792 /* For mode lines and alike, arrange for the first glyph having a
2793 left box line if the face specifies a box. */
2794 if (base_face_id != DEFAULT_FACE_ID)
2795 {
2796 struct face *face;
2797
2798 it->face_id = remapped_base_face_id;
2799
2800 /* If we have a boxed mode line, make the first character appear
2801 with a left box line. */
2802 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2803 if (face->box != FACE_NO_BOX)
2804 it->start_of_box_run_p = 1;
2805 }
2806
2807 /* If we are to reorder bidirectional text, init the bidi
2808 iterator. */
2809 if (it->bidi_p)
2810 {
2811 /* Note the paragraph direction that this buffer wants to
2812 use. */
2813 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2814 it->paragraph_embedding = L2R;
2815 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2816 it->paragraph_embedding = R2L;
2817 else
2818 it->paragraph_embedding = NEUTRAL_DIR;
2819 bidi_init_it (charpos, bytepos, &it->bidi_it);
2820 }
2821
2822 /* If a buffer position was specified, set the iterator there,
2823 getting overlays and face properties from that position. */
2824 if (charpos >= BUF_BEG (current_buffer))
2825 {
2826 it->end_charpos = ZV;
2827 it->face_id = -1;
2828 IT_CHARPOS (*it) = charpos;
2829
2830 /* Compute byte position if not specified. */
2831 if (bytepos < charpos)
2832 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2833 else
2834 IT_BYTEPOS (*it) = bytepos;
2835
2836 it->start = it->current;
2837
2838 /* Compute faces etc. */
2839 reseat (it, it->current.pos, 1);
2840 }
2841
2842 CHECK_IT (it);
2843 }
2844
2845
2846 /* Initialize IT for the display of window W with window start POS. */
2847
2848 void
2849 start_display (it, w, pos)
2850 struct it *it;
2851 struct window *w;
2852 struct text_pos pos;
2853 {
2854 struct glyph_row *row;
2855 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2856
2857 row = w->desired_matrix->rows + first_vpos;
2858 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2859 it->first_vpos = first_vpos;
2860
2861 /* Don't reseat to previous visible line start if current start
2862 position is in a string or image. */
2863 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2864 {
2865 int start_at_line_beg_p;
2866 int first_y = it->current_y;
2867
2868 /* If window start is not at a line start, skip forward to POS to
2869 get the correct continuation lines width. */
2870 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2871 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2872 if (!start_at_line_beg_p)
2873 {
2874 int new_x;
2875
2876 reseat_at_previous_visible_line_start (it);
2877 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2878
2879 new_x = it->current_x + it->pixel_width;
2880
2881 /* If lines are continued, this line may end in the middle
2882 of a multi-glyph character (e.g. a control character
2883 displayed as \003, or in the middle of an overlay
2884 string). In this case move_it_to above will not have
2885 taken us to the start of the continuation line but to the
2886 end of the continued line. */
2887 if (it->current_x > 0
2888 && it->line_wrap != TRUNCATE /* Lines are continued. */
2889 && (/* And glyph doesn't fit on the line. */
2890 new_x > it->last_visible_x
2891 /* Or it fits exactly and we're on a window
2892 system frame. */
2893 || (new_x == it->last_visible_x
2894 && FRAME_WINDOW_P (it->f))))
2895 {
2896 if (it->current.dpvec_index >= 0
2897 || it->current.overlay_string_index >= 0)
2898 {
2899 set_iterator_to_next (it, 1);
2900 move_it_in_display_line_to (it, -1, -1, 0);
2901 }
2902
2903 it->continuation_lines_width += it->current_x;
2904 }
2905
2906 /* We're starting a new display line, not affected by the
2907 height of the continued line, so clear the appropriate
2908 fields in the iterator structure. */
2909 it->max_ascent = it->max_descent = 0;
2910 it->max_phys_ascent = it->max_phys_descent = 0;
2911
2912 it->current_y = first_y;
2913 it->vpos = 0;
2914 it->current_x = it->hpos = 0;
2915 }
2916 }
2917 }
2918
2919
2920 /* Return 1 if POS is a position in ellipses displayed for invisible
2921 text. W is the window we display, for text property lookup. */
2922
2923 static int
2924 in_ellipses_for_invisible_text_p (pos, w)
2925 struct display_pos *pos;
2926 struct window *w;
2927 {
2928 Lisp_Object prop, window;
2929 int ellipses_p = 0;
2930 int charpos = CHARPOS (pos->pos);
2931
2932 /* If POS specifies a position in a display vector, this might
2933 be for an ellipsis displayed for invisible text. We won't
2934 get the iterator set up for delivering that ellipsis unless
2935 we make sure that it gets aware of the invisible text. */
2936 if (pos->dpvec_index >= 0
2937 && pos->overlay_string_index < 0
2938 && CHARPOS (pos->string_pos) < 0
2939 && charpos > BEGV
2940 && (XSETWINDOW (window, w),
2941 prop = Fget_char_property (make_number (charpos),
2942 Qinvisible, window),
2943 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2944 {
2945 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2946 window);
2947 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2948 }
2949
2950 return ellipses_p;
2951 }
2952
2953
2954 /* Initialize IT for stepping through current_buffer in window W,
2955 starting at position POS that includes overlay string and display
2956 vector/ control character translation position information. Value
2957 is zero if there are overlay strings with newlines at POS. */
2958
2959 static int
2960 init_from_display_pos (it, w, pos)
2961 struct it *it;
2962 struct window *w;
2963 struct display_pos *pos;
2964 {
2965 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2966 int i, overlay_strings_with_newlines = 0;
2967
2968 /* If POS specifies a position in a display vector, this might
2969 be for an ellipsis displayed for invisible text. We won't
2970 get the iterator set up for delivering that ellipsis unless
2971 we make sure that it gets aware of the invisible text. */
2972 if (in_ellipses_for_invisible_text_p (pos, w))
2973 {
2974 --charpos;
2975 bytepos = 0;
2976 }
2977
2978 /* Keep in mind: the call to reseat in init_iterator skips invisible
2979 text, so we might end up at a position different from POS. This
2980 is only a problem when POS is a row start after a newline and an
2981 overlay starts there with an after-string, and the overlay has an
2982 invisible property. Since we don't skip invisible text in
2983 display_line and elsewhere immediately after consuming the
2984 newline before the row start, such a POS will not be in a string,
2985 but the call to init_iterator below will move us to the
2986 after-string. */
2987 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2988
2989 /* This only scans the current chunk -- it should scan all chunks.
2990 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2991 to 16 in 22.1 to make this a lesser problem. */
2992 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2993 {
2994 const char *s = SDATA (it->overlay_strings[i]);
2995 const char *e = s + SBYTES (it->overlay_strings[i]);
2996
2997 while (s < e && *s != '\n')
2998 ++s;
2999
3000 if (s < e)
3001 {
3002 overlay_strings_with_newlines = 1;
3003 break;
3004 }
3005 }
3006
3007 /* If position is within an overlay string, set up IT to the right
3008 overlay string. */
3009 if (pos->overlay_string_index >= 0)
3010 {
3011 int relative_index;
3012
3013 /* If the first overlay string happens to have a `display'
3014 property for an image, the iterator will be set up for that
3015 image, and we have to undo that setup first before we can
3016 correct the overlay string index. */
3017 if (it->method == GET_FROM_IMAGE)
3018 pop_it (it);
3019
3020 /* We already have the first chunk of overlay strings in
3021 IT->overlay_strings. Load more until the one for
3022 pos->overlay_string_index is in IT->overlay_strings. */
3023 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3024 {
3025 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3026 it->current.overlay_string_index = 0;
3027 while (n--)
3028 {
3029 load_overlay_strings (it, 0);
3030 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3031 }
3032 }
3033
3034 it->current.overlay_string_index = pos->overlay_string_index;
3035 relative_index = (it->current.overlay_string_index
3036 % OVERLAY_STRING_CHUNK_SIZE);
3037 it->string = it->overlay_strings[relative_index];
3038 xassert (STRINGP (it->string));
3039 it->current.string_pos = pos->string_pos;
3040 it->method = GET_FROM_STRING;
3041 }
3042
3043 if (CHARPOS (pos->string_pos) >= 0)
3044 {
3045 /* Recorded position is not in an overlay string, but in another
3046 string. This can only be a string from a `display' property.
3047 IT should already be filled with that string. */
3048 it->current.string_pos = pos->string_pos;
3049 xassert (STRINGP (it->string));
3050 }
3051
3052 /* Restore position in display vector translations, control
3053 character translations or ellipses. */
3054 if (pos->dpvec_index >= 0)
3055 {
3056 if (it->dpvec == NULL)
3057 get_next_display_element (it);
3058 xassert (it->dpvec && it->current.dpvec_index == 0);
3059 it->current.dpvec_index = pos->dpvec_index;
3060 }
3061
3062 CHECK_IT (it);
3063 return !overlay_strings_with_newlines;
3064 }
3065
3066
3067 /* Initialize IT for stepping through current_buffer in window W
3068 starting at ROW->start. */
3069
3070 static void
3071 init_to_row_start (it, w, row)
3072 struct it *it;
3073 struct window *w;
3074 struct glyph_row *row;
3075 {
3076 init_from_display_pos (it, w, &row->start);
3077 it->start = row->start;
3078 it->continuation_lines_width = row->continuation_lines_width;
3079 CHECK_IT (it);
3080 }
3081
3082
3083 /* Initialize IT for stepping through current_buffer in window W
3084 starting in the line following ROW, i.e. starting at ROW->end.
3085 Value is zero if there are overlay strings with newlines at ROW's
3086 end position. */
3087
3088 static int
3089 init_to_row_end (it, w, row)
3090 struct it *it;
3091 struct window *w;
3092 struct glyph_row *row;
3093 {
3094 int success = 0;
3095
3096 if (init_from_display_pos (it, w, &row->end))
3097 {
3098 if (row->continued_p)
3099 it->continuation_lines_width
3100 = row->continuation_lines_width + row->pixel_width;
3101 CHECK_IT (it);
3102 success = 1;
3103 }
3104
3105 return success;
3106 }
3107
3108
3109
3110 \f
3111 /***********************************************************************
3112 Text properties
3113 ***********************************************************************/
3114
3115 /* Called when IT reaches IT->stop_charpos. Handle text property and
3116 overlay changes. Set IT->stop_charpos to the next position where
3117 to stop. */
3118
3119 static void
3120 handle_stop (it)
3121 struct it *it;
3122 {
3123 enum prop_handled handled;
3124 int handle_overlay_change_p;
3125 struct props *p;
3126
3127 it->dpvec = NULL;
3128 it->current.dpvec_index = -1;
3129 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3130 it->ignore_overlay_strings_at_pos_p = 0;
3131 it->ellipsis_p = 0;
3132
3133 /* Use face of preceding text for ellipsis (if invisible) */
3134 if (it->selective_display_ellipsis_p)
3135 it->saved_face_id = it->face_id;
3136
3137 do
3138 {
3139 handled = HANDLED_NORMALLY;
3140
3141 /* Call text property handlers. */
3142 for (p = it_props; p->handler; ++p)
3143 {
3144 handled = p->handler (it);
3145
3146 if (handled == HANDLED_RECOMPUTE_PROPS)
3147 break;
3148 else if (handled == HANDLED_RETURN)
3149 {
3150 /* We still want to show before and after strings from
3151 overlays even if the actual buffer text is replaced. */
3152 if (!handle_overlay_change_p
3153 || it->sp > 1
3154 || !get_overlay_strings_1 (it, 0, 0))
3155 {
3156 if (it->ellipsis_p)
3157 setup_for_ellipsis (it, 0);
3158 /* When handling a display spec, we might load an
3159 empty string. In that case, discard it here. We
3160 used to discard it in handle_single_display_spec,
3161 but that causes get_overlay_strings_1, above, to
3162 ignore overlay strings that we must check. */
3163 if (STRINGP (it->string) && !SCHARS (it->string))
3164 pop_it (it);
3165 return;
3166 }
3167 else if (STRINGP (it->string) && !SCHARS (it->string))
3168 pop_it (it);
3169 else
3170 {
3171 it->ignore_overlay_strings_at_pos_p = 1;
3172 it->string_from_display_prop_p = 0;
3173 handle_overlay_change_p = 0;
3174 }
3175 handled = HANDLED_RECOMPUTE_PROPS;
3176 break;
3177 }
3178 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3179 handle_overlay_change_p = 0;
3180 }
3181
3182 if (handled != HANDLED_RECOMPUTE_PROPS)
3183 {
3184 /* Don't check for overlay strings below when set to deliver
3185 characters from a display vector. */
3186 if (it->method == GET_FROM_DISPLAY_VECTOR)
3187 handle_overlay_change_p = 0;
3188
3189 /* Handle overlay changes.
3190 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3191 if it finds overlays. */
3192 if (handle_overlay_change_p)
3193 handled = handle_overlay_change (it);
3194 }
3195
3196 if (it->ellipsis_p)
3197 {
3198 setup_for_ellipsis (it, 0);
3199 break;
3200 }
3201 }
3202 while (handled == HANDLED_RECOMPUTE_PROPS);
3203
3204 /* Determine where to stop next. */
3205 if (handled == HANDLED_NORMALLY)
3206 compute_stop_pos (it);
3207 }
3208
3209
3210 /* Compute IT->stop_charpos from text property and overlay change
3211 information for IT's current position. */
3212
3213 static void
3214 compute_stop_pos (it)
3215 struct it *it;
3216 {
3217 register INTERVAL iv, next_iv;
3218 Lisp_Object object, limit, position;
3219 EMACS_INT charpos, bytepos;
3220
3221 /* If nowhere else, stop at the end. */
3222 it->stop_charpos = it->end_charpos;
3223
3224 if (STRINGP (it->string))
3225 {
3226 /* Strings are usually short, so don't limit the search for
3227 properties. */
3228 object = it->string;
3229 limit = Qnil;
3230 charpos = IT_STRING_CHARPOS (*it);
3231 bytepos = IT_STRING_BYTEPOS (*it);
3232 }
3233 else
3234 {
3235 EMACS_INT pos;
3236
3237 /* If next overlay change is in front of the current stop pos
3238 (which is IT->end_charpos), stop there. Note: value of
3239 next_overlay_change is point-max if no overlay change
3240 follows. */
3241 charpos = IT_CHARPOS (*it);
3242 bytepos = IT_BYTEPOS (*it);
3243 pos = next_overlay_change (charpos);
3244 if (pos < it->stop_charpos)
3245 it->stop_charpos = pos;
3246
3247 /* If showing the region, we have to stop at the region
3248 start or end because the face might change there. */
3249 if (it->region_beg_charpos > 0)
3250 {
3251 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3252 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3253 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3254 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3255 }
3256
3257 /* Set up variables for computing the stop position from text
3258 property changes. */
3259 XSETBUFFER (object, current_buffer);
3260 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3261 }
3262
3263 /* Get the interval containing IT's position. Value is a null
3264 interval if there isn't such an interval. */
3265 position = make_number (charpos);
3266 iv = validate_interval_range (object, &position, &position, 0);
3267 if (!NULL_INTERVAL_P (iv))
3268 {
3269 Lisp_Object values_here[LAST_PROP_IDX];
3270 struct props *p;
3271
3272 /* Get properties here. */
3273 for (p = it_props; p->handler; ++p)
3274 values_here[p->idx] = textget (iv->plist, *p->name);
3275
3276 /* Look for an interval following iv that has different
3277 properties. */
3278 for (next_iv = next_interval (iv);
3279 (!NULL_INTERVAL_P (next_iv)
3280 && (NILP (limit)
3281 || XFASTINT (limit) > next_iv->position));
3282 next_iv = next_interval (next_iv))
3283 {
3284 for (p = it_props; p->handler; ++p)
3285 {
3286 Lisp_Object new_value;
3287
3288 new_value = textget (next_iv->plist, *p->name);
3289 if (!EQ (values_here[p->idx], new_value))
3290 break;
3291 }
3292
3293 if (p->handler)
3294 break;
3295 }
3296
3297 if (!NULL_INTERVAL_P (next_iv))
3298 {
3299 if (INTEGERP (limit)
3300 && next_iv->position >= XFASTINT (limit))
3301 /* No text property change up to limit. */
3302 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3303 else
3304 /* Text properties change in next_iv. */
3305 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3306 }
3307 }
3308
3309 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3310 it->stop_charpos, it->string);
3311
3312 xassert (STRINGP (it->string)
3313 || (it->stop_charpos >= BEGV
3314 && it->stop_charpos >= IT_CHARPOS (*it)));
3315 }
3316
3317
3318 /* Return the position of the next overlay change after POS in
3319 current_buffer. Value is point-max if no overlay change
3320 follows. This is like `next-overlay-change' but doesn't use
3321 xmalloc. */
3322
3323 static EMACS_INT
3324 next_overlay_change (pos)
3325 EMACS_INT pos;
3326 {
3327 int noverlays;
3328 EMACS_INT endpos;
3329 Lisp_Object *overlays;
3330 int i;
3331
3332 /* Get all overlays at the given position. */
3333 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3334
3335 /* If any of these overlays ends before endpos,
3336 use its ending point instead. */
3337 for (i = 0; i < noverlays; ++i)
3338 {
3339 Lisp_Object oend;
3340 EMACS_INT oendpos;
3341
3342 oend = OVERLAY_END (overlays[i]);
3343 oendpos = OVERLAY_POSITION (oend);
3344 endpos = min (endpos, oendpos);
3345 }
3346
3347 return endpos;
3348 }
3349
3350
3351 \f
3352 /***********************************************************************
3353 Fontification
3354 ***********************************************************************/
3355
3356 /* Handle changes in the `fontified' property of the current buffer by
3357 calling hook functions from Qfontification_functions to fontify
3358 regions of text. */
3359
3360 static enum prop_handled
3361 handle_fontified_prop (it)
3362 struct it *it;
3363 {
3364 Lisp_Object prop, pos;
3365 enum prop_handled handled = HANDLED_NORMALLY;
3366
3367 if (!NILP (Vmemory_full))
3368 return handled;
3369
3370 /* Get the value of the `fontified' property at IT's current buffer
3371 position. (The `fontified' property doesn't have a special
3372 meaning in strings.) If the value is nil, call functions from
3373 Qfontification_functions. */
3374 if (!STRINGP (it->string)
3375 && it->s == NULL
3376 && !NILP (Vfontification_functions)
3377 && !NILP (Vrun_hooks)
3378 && (pos = make_number (IT_CHARPOS (*it)),
3379 prop = Fget_char_property (pos, Qfontified, Qnil),
3380 /* Ignore the special cased nil value always present at EOB since
3381 no amount of fontifying will be able to change it. */
3382 NILP (prop) && IT_CHARPOS (*it) < Z))
3383 {
3384 int count = SPECPDL_INDEX ();
3385 Lisp_Object val;
3386
3387 val = Vfontification_functions;
3388 specbind (Qfontification_functions, Qnil);
3389
3390 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3391 safe_call1 (val, pos);
3392 else
3393 {
3394 Lisp_Object globals, fn;
3395 struct gcpro gcpro1, gcpro2;
3396
3397 globals = Qnil;
3398 GCPRO2 (val, globals);
3399
3400 for (; CONSP (val); val = XCDR (val))
3401 {
3402 fn = XCAR (val);
3403
3404 if (EQ (fn, Qt))
3405 {
3406 /* A value of t indicates this hook has a local
3407 binding; it means to run the global binding too.
3408 In a global value, t should not occur. If it
3409 does, we must ignore it to avoid an endless
3410 loop. */
3411 for (globals = Fdefault_value (Qfontification_functions);
3412 CONSP (globals);
3413 globals = XCDR (globals))
3414 {
3415 fn = XCAR (globals);
3416 if (!EQ (fn, Qt))
3417 safe_call1 (fn, pos);
3418 }
3419 }
3420 else
3421 safe_call1 (fn, pos);
3422 }
3423
3424 UNGCPRO;
3425 }
3426
3427 unbind_to (count, Qnil);
3428
3429 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3430 something. This avoids an endless loop if they failed to
3431 fontify the text for which reason ever. */
3432 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3433 handled = HANDLED_RECOMPUTE_PROPS;
3434 }
3435
3436 return handled;
3437 }
3438
3439
3440 \f
3441 /***********************************************************************
3442 Faces
3443 ***********************************************************************/
3444
3445 /* Set up iterator IT from face properties at its current position.
3446 Called from handle_stop. */
3447
3448 static enum prop_handled
3449 handle_face_prop (it)
3450 struct it *it;
3451 {
3452 int new_face_id;
3453 EMACS_INT next_stop;
3454
3455 if (!STRINGP (it->string))
3456 {
3457 new_face_id
3458 = face_at_buffer_position (it->w,
3459 IT_CHARPOS (*it),
3460 it->region_beg_charpos,
3461 it->region_end_charpos,
3462 &next_stop,
3463 (IT_CHARPOS (*it)
3464 + TEXT_PROP_DISTANCE_LIMIT),
3465 0, it->base_face_id);
3466
3467 /* Is this a start of a run of characters with box face?
3468 Caveat: this can be called for a freshly initialized
3469 iterator; face_id is -1 in this case. We know that the new
3470 face will not change until limit, i.e. if the new face has a
3471 box, all characters up to limit will have one. But, as
3472 usual, we don't know whether limit is really the end. */
3473 if (new_face_id != it->face_id)
3474 {
3475 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3476
3477 /* If new face has a box but old face has not, this is
3478 the start of a run of characters with box, i.e. it has
3479 a shadow on the left side. The value of face_id of the
3480 iterator will be -1 if this is the initial call that gets
3481 the face. In this case, we have to look in front of IT's
3482 position and see whether there is a face != new_face_id. */
3483 it->start_of_box_run_p
3484 = (new_face->box != FACE_NO_BOX
3485 && (it->face_id >= 0
3486 || IT_CHARPOS (*it) == BEG
3487 || new_face_id != face_before_it_pos (it)));
3488 it->face_box_p = new_face->box != FACE_NO_BOX;
3489 }
3490 }
3491 else
3492 {
3493 int base_face_id, bufpos;
3494 int i;
3495 Lisp_Object from_overlay
3496 = (it->current.overlay_string_index >= 0
3497 ? it->string_overlays[it->current.overlay_string_index]
3498 : Qnil);
3499
3500 /* See if we got to this string directly or indirectly from
3501 an overlay property. That includes the before-string or
3502 after-string of an overlay, strings in display properties
3503 provided by an overlay, their text properties, etc.
3504
3505 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3506 if (! NILP (from_overlay))
3507 for (i = it->sp - 1; i >= 0; i--)
3508 {
3509 if (it->stack[i].current.overlay_string_index >= 0)
3510 from_overlay
3511 = it->string_overlays[it->stack[i].current.overlay_string_index];
3512 else if (! NILP (it->stack[i].from_overlay))
3513 from_overlay = it->stack[i].from_overlay;
3514
3515 if (!NILP (from_overlay))
3516 break;
3517 }
3518
3519 if (! NILP (from_overlay))
3520 {
3521 bufpos = IT_CHARPOS (*it);
3522 /* For a string from an overlay, the base face depends
3523 only on text properties and ignores overlays. */
3524 base_face_id
3525 = face_for_overlay_string (it->w,
3526 IT_CHARPOS (*it),
3527 it->region_beg_charpos,
3528 it->region_end_charpos,
3529 &next_stop,
3530 (IT_CHARPOS (*it)
3531 + TEXT_PROP_DISTANCE_LIMIT),
3532 0,
3533 from_overlay);
3534 }
3535 else
3536 {
3537 bufpos = 0;
3538
3539 /* For strings from a `display' property, use the face at
3540 IT's current buffer position as the base face to merge
3541 with, so that overlay strings appear in the same face as
3542 surrounding text, unless they specify their own
3543 faces. */
3544 base_face_id = underlying_face_id (it);
3545 }
3546
3547 new_face_id = face_at_string_position (it->w,
3548 it->string,
3549 IT_STRING_CHARPOS (*it),
3550 bufpos,
3551 it->region_beg_charpos,
3552 it->region_end_charpos,
3553 &next_stop,
3554 base_face_id, 0);
3555
3556 /* Is this a start of a run of characters with box? Caveat:
3557 this can be called for a freshly allocated iterator; face_id
3558 is -1 is this case. We know that the new face will not
3559 change until the next check pos, i.e. if the new face has a
3560 box, all characters up to that position will have a
3561 box. But, as usual, we don't know whether that position
3562 is really the end. */
3563 if (new_face_id != it->face_id)
3564 {
3565 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3566 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3567
3568 /* If new face has a box but old face hasn't, this is the
3569 start of a run of characters with box, i.e. it has a
3570 shadow on the left side. */
3571 it->start_of_box_run_p
3572 = new_face->box && (old_face == NULL || !old_face->box);
3573 it->face_box_p = new_face->box != FACE_NO_BOX;
3574 }
3575 }
3576
3577 it->face_id = new_face_id;
3578 return HANDLED_NORMALLY;
3579 }
3580
3581
3582 /* Return the ID of the face ``underlying'' IT's current position,
3583 which is in a string. If the iterator is associated with a
3584 buffer, return the face at IT's current buffer position.
3585 Otherwise, use the iterator's base_face_id. */
3586
3587 static int
3588 underlying_face_id (it)
3589 struct it *it;
3590 {
3591 int face_id = it->base_face_id, i;
3592
3593 xassert (STRINGP (it->string));
3594
3595 for (i = it->sp - 1; i >= 0; --i)
3596 if (NILP (it->stack[i].string))
3597 face_id = it->stack[i].face_id;
3598
3599 return face_id;
3600 }
3601
3602
3603 /* Compute the face one character before or after the current position
3604 of IT. BEFORE_P non-zero means get the face in front of IT's
3605 position. Value is the id of the face. */
3606
3607 static int
3608 face_before_or_after_it_pos (it, before_p)
3609 struct it *it;
3610 int before_p;
3611 {
3612 int face_id, limit;
3613 EMACS_INT next_check_charpos;
3614 struct text_pos pos;
3615
3616 xassert (it->s == NULL);
3617
3618 if (STRINGP (it->string))
3619 {
3620 int bufpos, base_face_id;
3621
3622 /* No face change past the end of the string (for the case
3623 we are padding with spaces). No face change before the
3624 string start. */
3625 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3626 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3627 return it->face_id;
3628
3629 /* Set pos to the position before or after IT's current position. */
3630 if (before_p)
3631 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3632 else
3633 /* For composition, we must check the character after the
3634 composition. */
3635 pos = (it->what == IT_COMPOSITION
3636 ? string_pos (IT_STRING_CHARPOS (*it)
3637 + it->cmp_it.nchars, it->string)
3638 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3639
3640 if (it->current.overlay_string_index >= 0)
3641 bufpos = IT_CHARPOS (*it);
3642 else
3643 bufpos = 0;
3644
3645 base_face_id = underlying_face_id (it);
3646
3647 /* Get the face for ASCII, or unibyte. */
3648 face_id = face_at_string_position (it->w,
3649 it->string,
3650 CHARPOS (pos),
3651 bufpos,
3652 it->region_beg_charpos,
3653 it->region_end_charpos,
3654 &next_check_charpos,
3655 base_face_id, 0);
3656
3657 /* Correct the face for charsets different from ASCII. Do it
3658 for the multibyte case only. The face returned above is
3659 suitable for unibyte text if IT->string is unibyte. */
3660 if (STRING_MULTIBYTE (it->string))
3661 {
3662 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3663 int rest = SBYTES (it->string) - BYTEPOS (pos);
3664 int c, len;
3665 struct face *face = FACE_FROM_ID (it->f, face_id);
3666
3667 c = string_char_and_length (p, &len);
3668 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3669 }
3670 }
3671 else
3672 {
3673 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3674 || (IT_CHARPOS (*it) <= BEGV && before_p))
3675 return it->face_id;
3676
3677 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3678 pos = it->current.pos;
3679
3680 if (before_p)
3681 DEC_TEXT_POS (pos, it->multibyte_p);
3682 else
3683 {
3684 if (it->what == IT_COMPOSITION)
3685 /* For composition, we must check the position after the
3686 composition. */
3687 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3688 else
3689 INC_TEXT_POS (pos, it->multibyte_p);
3690 }
3691
3692 /* Determine face for CHARSET_ASCII, or unibyte. */
3693 face_id = face_at_buffer_position (it->w,
3694 CHARPOS (pos),
3695 it->region_beg_charpos,
3696 it->region_end_charpos,
3697 &next_check_charpos,
3698 limit, 0, -1);
3699
3700 /* Correct the face for charsets different from ASCII. Do it
3701 for the multibyte case only. The face returned above is
3702 suitable for unibyte text if current_buffer is unibyte. */
3703 if (it->multibyte_p)
3704 {
3705 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3706 struct face *face = FACE_FROM_ID (it->f, face_id);
3707 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3708 }
3709 }
3710
3711 return face_id;
3712 }
3713
3714
3715 \f
3716 /***********************************************************************
3717 Invisible text
3718 ***********************************************************************/
3719
3720 /* Set up iterator IT from invisible properties at its current
3721 position. Called from handle_stop. */
3722
3723 static enum prop_handled
3724 handle_invisible_prop (it)
3725 struct it *it;
3726 {
3727 enum prop_handled handled = HANDLED_NORMALLY;
3728
3729 if (STRINGP (it->string))
3730 {
3731 extern Lisp_Object Qinvisible;
3732 Lisp_Object prop, end_charpos, limit, charpos;
3733
3734 /* Get the value of the invisible text property at the
3735 current position. Value will be nil if there is no such
3736 property. */
3737 charpos = make_number (IT_STRING_CHARPOS (*it));
3738 prop = Fget_text_property (charpos, Qinvisible, it->string);
3739
3740 if (!NILP (prop)
3741 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3742 {
3743 handled = HANDLED_RECOMPUTE_PROPS;
3744
3745 /* Get the position at which the next change of the
3746 invisible text property can be found in IT->string.
3747 Value will be nil if the property value is the same for
3748 all the rest of IT->string. */
3749 XSETINT (limit, SCHARS (it->string));
3750 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3751 it->string, limit);
3752
3753 /* Text at current position is invisible. The next
3754 change in the property is at position end_charpos.
3755 Move IT's current position to that position. */
3756 if (INTEGERP (end_charpos)
3757 && XFASTINT (end_charpos) < XFASTINT (limit))
3758 {
3759 struct text_pos old;
3760 old = it->current.string_pos;
3761 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3762 compute_string_pos (&it->current.string_pos, old, it->string);
3763 }
3764 else
3765 {
3766 /* The rest of the string is invisible. If this is an
3767 overlay string, proceed with the next overlay string
3768 or whatever comes and return a character from there. */
3769 if (it->current.overlay_string_index >= 0)
3770 {
3771 next_overlay_string (it);
3772 /* Don't check for overlay strings when we just
3773 finished processing them. */
3774 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3775 }
3776 else
3777 {
3778 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3779 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3780 }
3781 }
3782 }
3783 }
3784 else
3785 {
3786 int invis_p;
3787 EMACS_INT newpos, next_stop, start_charpos, tem;
3788 Lisp_Object pos, prop, overlay;
3789
3790 /* First of all, is there invisible text at this position? */
3791 tem = start_charpos = IT_CHARPOS (*it);
3792 pos = make_number (IT_CHARPOS (*it));
3793 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3794 &overlay);
3795 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3796
3797 /* If we are on invisible text, skip over it. */
3798 if (invis_p && start_charpos < it->end_charpos)
3799 {
3800 /* Record whether we have to display an ellipsis for the
3801 invisible text. */
3802 int display_ellipsis_p = invis_p == 2;
3803
3804 handled = HANDLED_RECOMPUTE_PROPS;
3805
3806 /* Loop skipping over invisible text. The loop is left at
3807 ZV or with IT on the first char being visible again. */
3808 do
3809 {
3810 /* Try to skip some invisible text. Return value is the
3811 position reached which can be equal to where we start
3812 if there is nothing invisible there. This skips both
3813 over invisible text properties and overlays with
3814 invisible property. */
3815 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3816
3817 /* If we skipped nothing at all we weren't at invisible
3818 text in the first place. If everything to the end of
3819 the buffer was skipped, end the loop. */
3820 if (newpos == tem || newpos >= ZV)
3821 invis_p = 0;
3822 else
3823 {
3824 /* We skipped some characters but not necessarily
3825 all there are. Check if we ended up on visible
3826 text. Fget_char_property returns the property of
3827 the char before the given position, i.e. if we
3828 get invis_p = 0, this means that the char at
3829 newpos is visible. */
3830 pos = make_number (newpos);
3831 prop = Fget_char_property (pos, Qinvisible, it->window);
3832 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3833 }
3834
3835 /* If we ended up on invisible text, proceed to
3836 skip starting with next_stop. */
3837 if (invis_p)
3838 tem = next_stop;
3839
3840 /* If there are adjacent invisible texts, don't lose the
3841 second one's ellipsis. */
3842 if (invis_p == 2)
3843 display_ellipsis_p = 1;
3844 }
3845 while (invis_p);
3846
3847 /* The position newpos is now either ZV or on visible text. */
3848 if (it->bidi_p && newpos < ZV)
3849 {
3850 /* With bidi iteration, the region of invisible text
3851 could start and/or end in the middle of a non-base
3852 embedding level. Therefore, we need to skip
3853 invisible text using the bidi iterator, starting at
3854 IT's current position, until we find ourselves
3855 outside the invisible text. This avoids affecting
3856 the visual order of the displayed text when invisible
3857 properties are added or removed. */
3858 do
3859 {
3860 bidi_get_next_char_visually (&it->bidi_it);
3861 }
3862 while (start_charpos <= it->bidi_it.charpos
3863 && it->bidi_it.charpos < newpos);
3864 IT_CHARPOS (*it) = it->bidi_it.charpos;
3865 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3866 }
3867 else
3868 {
3869 IT_CHARPOS (*it) = newpos;
3870 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3871 }
3872
3873 /* If there are before-strings at the start of invisible
3874 text, and the text is invisible because of a text
3875 property, arrange to show before-strings because 20.x did
3876 it that way. (If the text is invisible because of an
3877 overlay property instead of a text property, this is
3878 already handled in the overlay code.) */
3879 if (NILP (overlay)
3880 && get_overlay_strings (it, start_charpos))
3881 {
3882 handled = HANDLED_RECOMPUTE_PROPS;
3883 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3884 }
3885 else if (display_ellipsis_p)
3886 {
3887 /* Make sure that the glyphs of the ellipsis will get
3888 correct `charpos' values. If we would not update
3889 it->position here, the glyphs would belong to the
3890 last visible character _before_ the invisible
3891 text, which confuses `set_cursor_from_row'.
3892
3893 We use the last invisible position instead of the
3894 first because this way the cursor is always drawn on
3895 the first "." of the ellipsis, whenever PT is inside
3896 the invisible text. Otherwise the cursor would be
3897 placed _after_ the ellipsis when the point is after the
3898 first invisible character. */
3899 if (!STRINGP (it->object))
3900 {
3901 it->position.charpos = newpos - 1;
3902 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3903 }
3904 it->ellipsis_p = 1;
3905 /* Let the ellipsis display before
3906 considering any properties of the following char.
3907 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3908 handled = HANDLED_RETURN;
3909 }
3910 }
3911 }
3912
3913 return handled;
3914 }
3915
3916
3917 /* Make iterator IT return `...' next.
3918 Replaces LEN characters from buffer. */
3919
3920 static void
3921 setup_for_ellipsis (it, len)
3922 struct it *it;
3923 int len;
3924 {
3925 /* Use the display table definition for `...'. Invalid glyphs
3926 will be handled by the method returning elements from dpvec. */
3927 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3928 {
3929 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3930 it->dpvec = v->contents;
3931 it->dpend = v->contents + v->size;
3932 }
3933 else
3934 {
3935 /* Default `...'. */
3936 it->dpvec = default_invis_vector;
3937 it->dpend = default_invis_vector + 3;
3938 }
3939
3940 it->dpvec_char_len = len;
3941 it->current.dpvec_index = 0;
3942 it->dpvec_face_id = -1;
3943
3944 /* Remember the current face id in case glyphs specify faces.
3945 IT's face is restored in set_iterator_to_next.
3946 saved_face_id was set to preceding char's face in handle_stop. */
3947 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3948 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3949
3950 it->method = GET_FROM_DISPLAY_VECTOR;
3951 it->ellipsis_p = 1;
3952 }
3953
3954
3955 \f
3956 /***********************************************************************
3957 'display' property
3958 ***********************************************************************/
3959
3960 /* Set up iterator IT from `display' property at its current position.
3961 Called from handle_stop.
3962 We return HANDLED_RETURN if some part of the display property
3963 overrides the display of the buffer text itself.
3964 Otherwise we return HANDLED_NORMALLY. */
3965
3966 static enum prop_handled
3967 handle_display_prop (it)
3968 struct it *it;
3969 {
3970 Lisp_Object prop, object, overlay;
3971 struct text_pos *position;
3972 /* Nonzero if some property replaces the display of the text itself. */
3973 int display_replaced_p = 0;
3974
3975 if (STRINGP (it->string))
3976 {
3977 object = it->string;
3978 position = &it->current.string_pos;
3979 }
3980 else
3981 {
3982 XSETWINDOW (object, it->w);
3983 position = &it->current.pos;
3984 }
3985
3986 /* Reset those iterator values set from display property values. */
3987 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3988 it->space_width = Qnil;
3989 it->font_height = Qnil;
3990 it->voffset = 0;
3991
3992 /* We don't support recursive `display' properties, i.e. string
3993 values that have a string `display' property, that have a string
3994 `display' property etc. */
3995 if (!it->string_from_display_prop_p)
3996 it->area = TEXT_AREA;
3997
3998 prop = get_char_property_and_overlay (make_number (position->charpos),
3999 Qdisplay, object, &overlay);
4000 if (NILP (prop))
4001 return HANDLED_NORMALLY;
4002 /* Now OVERLAY is the overlay that gave us this property, or nil
4003 if it was a text property. */
4004
4005 if (!STRINGP (it->string))
4006 object = it->w->buffer;
4007
4008 if (CONSP (prop)
4009 /* Simple properties. */
4010 && !EQ (XCAR (prop), Qimage)
4011 && !EQ (XCAR (prop), Qspace)
4012 && !EQ (XCAR (prop), Qwhen)
4013 && !EQ (XCAR (prop), Qslice)
4014 && !EQ (XCAR (prop), Qspace_width)
4015 && !EQ (XCAR (prop), Qheight)
4016 && !EQ (XCAR (prop), Qraise)
4017 /* Marginal area specifications. */
4018 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4019 && !EQ (XCAR (prop), Qleft_fringe)
4020 && !EQ (XCAR (prop), Qright_fringe)
4021 && !NILP (XCAR (prop)))
4022 {
4023 for (; CONSP (prop); prop = XCDR (prop))
4024 {
4025 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4026 position, display_replaced_p))
4027 {
4028 display_replaced_p = 1;
4029 /* If some text in a string is replaced, `position' no
4030 longer points to the position of `object'. */
4031 if (STRINGP (object))
4032 break;
4033 }
4034 }
4035 }
4036 else if (VECTORP (prop))
4037 {
4038 int i;
4039 for (i = 0; i < ASIZE (prop); ++i)
4040 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4041 position, display_replaced_p))
4042 {
4043 display_replaced_p = 1;
4044 /* If some text in a string is replaced, `position' no
4045 longer points to the position of `object'. */
4046 if (STRINGP (object))
4047 break;
4048 }
4049 }
4050 else
4051 {
4052 if (handle_single_display_spec (it, prop, object, overlay,
4053 position, 0))
4054 display_replaced_p = 1;
4055 }
4056
4057 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4058 }
4059
4060
4061 /* Value is the position of the end of the `display' property starting
4062 at START_POS in OBJECT. */
4063
4064 static struct text_pos
4065 display_prop_end (it, object, start_pos)
4066 struct it *it;
4067 Lisp_Object object;
4068 struct text_pos start_pos;
4069 {
4070 Lisp_Object end;
4071 struct text_pos end_pos;
4072
4073 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4074 Qdisplay, object, Qnil);
4075 CHARPOS (end_pos) = XFASTINT (end);
4076 if (STRINGP (object))
4077 compute_string_pos (&end_pos, start_pos, it->string);
4078 else
4079 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4080
4081 return end_pos;
4082 }
4083
4084
4085 /* Set up IT from a single `display' specification PROP. OBJECT
4086 is the object in which the `display' property was found. *POSITION
4087 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4088 means that we previously saw a display specification which already
4089 replaced text display with something else, for example an image;
4090 we ignore such properties after the first one has been processed.
4091
4092 OVERLAY is the overlay this `display' property came from,
4093 or nil if it was a text property.
4094
4095 If PROP is a `space' or `image' specification, and in some other
4096 cases too, set *POSITION to the position where the `display'
4097 property ends.
4098
4099 Value is non-zero if something was found which replaces the display
4100 of buffer or string text. */
4101
4102 static int
4103 handle_single_display_spec (it, spec, object, overlay, position,
4104 display_replaced_before_p)
4105 struct it *it;
4106 Lisp_Object spec;
4107 Lisp_Object object;
4108 Lisp_Object overlay;
4109 struct text_pos *position;
4110 int display_replaced_before_p;
4111 {
4112 Lisp_Object form;
4113 Lisp_Object location, value;
4114 struct text_pos start_pos, save_pos;
4115 int valid_p;
4116
4117 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4118 If the result is non-nil, use VALUE instead of SPEC. */
4119 form = Qt;
4120 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4121 {
4122 spec = XCDR (spec);
4123 if (!CONSP (spec))
4124 return 0;
4125 form = XCAR (spec);
4126 spec = XCDR (spec);
4127 }
4128
4129 if (!NILP (form) && !EQ (form, Qt))
4130 {
4131 int count = SPECPDL_INDEX ();
4132 struct gcpro gcpro1;
4133
4134 /* Bind `object' to the object having the `display' property, a
4135 buffer or string. Bind `position' to the position in the
4136 object where the property was found, and `buffer-position'
4137 to the current position in the buffer. */
4138 specbind (Qobject, object);
4139 specbind (Qposition, make_number (CHARPOS (*position)));
4140 specbind (Qbuffer_position,
4141 make_number (STRINGP (object)
4142 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4143 GCPRO1 (form);
4144 form = safe_eval (form);
4145 UNGCPRO;
4146 unbind_to (count, Qnil);
4147 }
4148
4149 if (NILP (form))
4150 return 0;
4151
4152 /* Handle `(height HEIGHT)' specifications. */
4153 if (CONSP (spec)
4154 && EQ (XCAR (spec), Qheight)
4155 && CONSP (XCDR (spec)))
4156 {
4157 if (!FRAME_WINDOW_P (it->f))
4158 return 0;
4159
4160 it->font_height = XCAR (XCDR (spec));
4161 if (!NILP (it->font_height))
4162 {
4163 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4164 int new_height = -1;
4165
4166 if (CONSP (it->font_height)
4167 && (EQ (XCAR (it->font_height), Qplus)
4168 || EQ (XCAR (it->font_height), Qminus))
4169 && CONSP (XCDR (it->font_height))
4170 && INTEGERP (XCAR (XCDR (it->font_height))))
4171 {
4172 /* `(+ N)' or `(- N)' where N is an integer. */
4173 int steps = XINT (XCAR (XCDR (it->font_height)));
4174 if (EQ (XCAR (it->font_height), Qplus))
4175 steps = - steps;
4176 it->face_id = smaller_face (it->f, it->face_id, steps);
4177 }
4178 else if (FUNCTIONP (it->font_height))
4179 {
4180 /* Call function with current height as argument.
4181 Value is the new height. */
4182 Lisp_Object height;
4183 height = safe_call1 (it->font_height,
4184 face->lface[LFACE_HEIGHT_INDEX]);
4185 if (NUMBERP (height))
4186 new_height = XFLOATINT (height);
4187 }
4188 else if (NUMBERP (it->font_height))
4189 {
4190 /* Value is a multiple of the canonical char height. */
4191 struct face *face;
4192
4193 face = FACE_FROM_ID (it->f,
4194 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4195 new_height = (XFLOATINT (it->font_height)
4196 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4197 }
4198 else
4199 {
4200 /* Evaluate IT->font_height with `height' bound to the
4201 current specified height to get the new height. */
4202 int count = SPECPDL_INDEX ();
4203
4204 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4205 value = safe_eval (it->font_height);
4206 unbind_to (count, Qnil);
4207
4208 if (NUMBERP (value))
4209 new_height = XFLOATINT (value);
4210 }
4211
4212 if (new_height > 0)
4213 it->face_id = face_with_height (it->f, it->face_id, new_height);
4214 }
4215
4216 return 0;
4217 }
4218
4219 /* Handle `(space-width WIDTH)'. */
4220 if (CONSP (spec)
4221 && EQ (XCAR (spec), Qspace_width)
4222 && CONSP (XCDR (spec)))
4223 {
4224 if (!FRAME_WINDOW_P (it->f))
4225 return 0;
4226
4227 value = XCAR (XCDR (spec));
4228 if (NUMBERP (value) && XFLOATINT (value) > 0)
4229 it->space_width = value;
4230
4231 return 0;
4232 }
4233
4234 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4235 if (CONSP (spec)
4236 && EQ (XCAR (spec), Qslice))
4237 {
4238 Lisp_Object tem;
4239
4240 if (!FRAME_WINDOW_P (it->f))
4241 return 0;
4242
4243 if (tem = XCDR (spec), CONSP (tem))
4244 {
4245 it->slice.x = XCAR (tem);
4246 if (tem = XCDR (tem), CONSP (tem))
4247 {
4248 it->slice.y = XCAR (tem);
4249 if (tem = XCDR (tem), CONSP (tem))
4250 {
4251 it->slice.width = XCAR (tem);
4252 if (tem = XCDR (tem), CONSP (tem))
4253 it->slice.height = XCAR (tem);
4254 }
4255 }
4256 }
4257
4258 return 0;
4259 }
4260
4261 /* Handle `(raise FACTOR)'. */
4262 if (CONSP (spec)
4263 && EQ (XCAR (spec), Qraise)
4264 && CONSP (XCDR (spec)))
4265 {
4266 if (!FRAME_WINDOW_P (it->f))
4267 return 0;
4268
4269 #ifdef HAVE_WINDOW_SYSTEM
4270 value = XCAR (XCDR (spec));
4271 if (NUMBERP (value))
4272 {
4273 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4274 it->voffset = - (XFLOATINT (value)
4275 * (FONT_HEIGHT (face->font)));
4276 }
4277 #endif /* HAVE_WINDOW_SYSTEM */
4278
4279 return 0;
4280 }
4281
4282 /* Don't handle the other kinds of display specifications
4283 inside a string that we got from a `display' property. */
4284 if (it->string_from_display_prop_p)
4285 return 0;
4286
4287 /* Characters having this form of property are not displayed, so
4288 we have to find the end of the property. */
4289 start_pos = *position;
4290 *position = display_prop_end (it, object, start_pos);
4291 value = Qnil;
4292
4293 /* Stop the scan at that end position--we assume that all
4294 text properties change there. */
4295 it->stop_charpos = position->charpos;
4296
4297 /* Handle `(left-fringe BITMAP [FACE])'
4298 and `(right-fringe BITMAP [FACE])'. */
4299 if (CONSP (spec)
4300 && (EQ (XCAR (spec), Qleft_fringe)
4301 || EQ (XCAR (spec), Qright_fringe))
4302 && CONSP (XCDR (spec)))
4303 {
4304 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4305 int fringe_bitmap;
4306
4307 if (!FRAME_WINDOW_P (it->f))
4308 /* If we return here, POSITION has been advanced
4309 across the text with this property. */
4310 return 0;
4311
4312 #ifdef HAVE_WINDOW_SYSTEM
4313 value = XCAR (XCDR (spec));
4314 if (!SYMBOLP (value)
4315 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4316 /* If we return here, POSITION has been advanced
4317 across the text with this property. */
4318 return 0;
4319
4320 if (CONSP (XCDR (XCDR (spec))))
4321 {
4322 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4323 int face_id2 = lookup_derived_face (it->f, face_name,
4324 FRINGE_FACE_ID, 0);
4325 if (face_id2 >= 0)
4326 face_id = face_id2;
4327 }
4328
4329 /* Save current settings of IT so that we can restore them
4330 when we are finished with the glyph property value. */
4331
4332 save_pos = it->position;
4333 it->position = *position;
4334 push_it (it);
4335 it->position = save_pos;
4336
4337 it->area = TEXT_AREA;
4338 it->what = IT_IMAGE;
4339 it->image_id = -1; /* no image */
4340 it->position = start_pos;
4341 it->object = NILP (object) ? it->w->buffer : object;
4342 it->method = GET_FROM_IMAGE;
4343 it->from_overlay = Qnil;
4344 it->face_id = face_id;
4345
4346 /* Say that we haven't consumed the characters with
4347 `display' property yet. The call to pop_it in
4348 set_iterator_to_next will clean this up. */
4349 *position = start_pos;
4350
4351 if (EQ (XCAR (spec), Qleft_fringe))
4352 {
4353 it->left_user_fringe_bitmap = fringe_bitmap;
4354 it->left_user_fringe_face_id = face_id;
4355 }
4356 else
4357 {
4358 it->right_user_fringe_bitmap = fringe_bitmap;
4359 it->right_user_fringe_face_id = face_id;
4360 }
4361 #endif /* HAVE_WINDOW_SYSTEM */
4362 return 1;
4363 }
4364
4365 /* Prepare to handle `((margin left-margin) ...)',
4366 `((margin right-margin) ...)' and `((margin nil) ...)'
4367 prefixes for display specifications. */
4368 location = Qunbound;
4369 if (CONSP (spec) && CONSP (XCAR (spec)))
4370 {
4371 Lisp_Object tem;
4372
4373 value = XCDR (spec);
4374 if (CONSP (value))
4375 value = XCAR (value);
4376
4377 tem = XCAR (spec);
4378 if (EQ (XCAR (tem), Qmargin)
4379 && (tem = XCDR (tem),
4380 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4381 (NILP (tem)
4382 || EQ (tem, Qleft_margin)
4383 || EQ (tem, Qright_margin))))
4384 location = tem;
4385 }
4386
4387 if (EQ (location, Qunbound))
4388 {
4389 location = Qnil;
4390 value = spec;
4391 }
4392
4393 /* After this point, VALUE is the property after any
4394 margin prefix has been stripped. It must be a string,
4395 an image specification, or `(space ...)'.
4396
4397 LOCATION specifies where to display: `left-margin',
4398 `right-margin' or nil. */
4399
4400 valid_p = (STRINGP (value)
4401 #ifdef HAVE_WINDOW_SYSTEM
4402 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4403 #endif /* not HAVE_WINDOW_SYSTEM */
4404 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4405
4406 if (valid_p && !display_replaced_before_p)
4407 {
4408 /* Save current settings of IT so that we can restore them
4409 when we are finished with the glyph property value. */
4410 save_pos = it->position;
4411 it->position = *position;
4412 push_it (it);
4413 it->position = save_pos;
4414 it->from_overlay = overlay;
4415
4416 if (NILP (location))
4417 it->area = TEXT_AREA;
4418 else if (EQ (location, Qleft_margin))
4419 it->area = LEFT_MARGIN_AREA;
4420 else
4421 it->area = RIGHT_MARGIN_AREA;
4422
4423 if (STRINGP (value))
4424 {
4425 it->string = value;
4426 it->multibyte_p = STRING_MULTIBYTE (it->string);
4427 it->current.overlay_string_index = -1;
4428 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4429 it->end_charpos = it->string_nchars = SCHARS (it->string);
4430 it->method = GET_FROM_STRING;
4431 it->stop_charpos = 0;
4432 it->string_from_display_prop_p = 1;
4433 /* Say that we haven't consumed the characters with
4434 `display' property yet. The call to pop_it in
4435 set_iterator_to_next will clean this up. */
4436 if (BUFFERP (object))
4437 *position = start_pos;
4438 }
4439 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4440 {
4441 it->method = GET_FROM_STRETCH;
4442 it->object = value;
4443 *position = it->position = start_pos;
4444 }
4445 #ifdef HAVE_WINDOW_SYSTEM
4446 else
4447 {
4448 it->what = IT_IMAGE;
4449 it->image_id = lookup_image (it->f, value);
4450 it->position = start_pos;
4451 it->object = NILP (object) ? it->w->buffer : object;
4452 it->method = GET_FROM_IMAGE;
4453
4454 /* Say that we haven't consumed the characters with
4455 `display' property yet. The call to pop_it in
4456 set_iterator_to_next will clean this up. */
4457 *position = start_pos;
4458 }
4459 #endif /* HAVE_WINDOW_SYSTEM */
4460
4461 return 1;
4462 }
4463
4464 /* Invalid property or property not supported. Restore
4465 POSITION to what it was before. */
4466 *position = start_pos;
4467 return 0;
4468 }
4469
4470
4471 /* Check if SPEC is a display sub-property value whose text should be
4472 treated as intangible. */
4473
4474 static int
4475 single_display_spec_intangible_p (prop)
4476 Lisp_Object prop;
4477 {
4478 /* Skip over `when FORM'. */
4479 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4480 {
4481 prop = XCDR (prop);
4482 if (!CONSP (prop))
4483 return 0;
4484 prop = XCDR (prop);
4485 }
4486
4487 if (STRINGP (prop))
4488 return 1;
4489
4490 if (!CONSP (prop))
4491 return 0;
4492
4493 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4494 we don't need to treat text as intangible. */
4495 if (EQ (XCAR (prop), Qmargin))
4496 {
4497 prop = XCDR (prop);
4498 if (!CONSP (prop))
4499 return 0;
4500
4501 prop = XCDR (prop);
4502 if (!CONSP (prop)
4503 || EQ (XCAR (prop), Qleft_margin)
4504 || EQ (XCAR (prop), Qright_margin))
4505 return 0;
4506 }
4507
4508 return (CONSP (prop)
4509 && (EQ (XCAR (prop), Qimage)
4510 || EQ (XCAR (prop), Qspace)));
4511 }
4512
4513
4514 /* Check if PROP is a display property value whose text should be
4515 treated as intangible. */
4516
4517 int
4518 display_prop_intangible_p (prop)
4519 Lisp_Object prop;
4520 {
4521 if (CONSP (prop)
4522 && CONSP (XCAR (prop))
4523 && !EQ (Qmargin, XCAR (XCAR (prop))))
4524 {
4525 /* A list of sub-properties. */
4526 while (CONSP (prop))
4527 {
4528 if (single_display_spec_intangible_p (XCAR (prop)))
4529 return 1;
4530 prop = XCDR (prop);
4531 }
4532 }
4533 else if (VECTORP (prop))
4534 {
4535 /* A vector of sub-properties. */
4536 int i;
4537 for (i = 0; i < ASIZE (prop); ++i)
4538 if (single_display_spec_intangible_p (AREF (prop, i)))
4539 return 1;
4540 }
4541 else
4542 return single_display_spec_intangible_p (prop);
4543
4544 return 0;
4545 }
4546
4547
4548 /* Return 1 if PROP is a display sub-property value containing STRING. */
4549
4550 static int
4551 single_display_spec_string_p (prop, string)
4552 Lisp_Object prop, string;
4553 {
4554 if (EQ (string, prop))
4555 return 1;
4556
4557 /* Skip over `when FORM'. */
4558 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4559 {
4560 prop = XCDR (prop);
4561 if (!CONSP (prop))
4562 return 0;
4563 prop = XCDR (prop);
4564 }
4565
4566 if (CONSP (prop))
4567 /* Skip over `margin LOCATION'. */
4568 if (EQ (XCAR (prop), Qmargin))
4569 {
4570 prop = XCDR (prop);
4571 if (!CONSP (prop))
4572 return 0;
4573
4574 prop = XCDR (prop);
4575 if (!CONSP (prop))
4576 return 0;
4577 }
4578
4579 return CONSP (prop) && EQ (XCAR (prop), string);
4580 }
4581
4582
4583 /* Return 1 if STRING appears in the `display' property PROP. */
4584
4585 static int
4586 display_prop_string_p (prop, string)
4587 Lisp_Object prop, string;
4588 {
4589 if (CONSP (prop)
4590 && CONSP (XCAR (prop))
4591 && !EQ (Qmargin, XCAR (XCAR (prop))))
4592 {
4593 /* A list of sub-properties. */
4594 while (CONSP (prop))
4595 {
4596 if (single_display_spec_string_p (XCAR (prop), string))
4597 return 1;
4598 prop = XCDR (prop);
4599 }
4600 }
4601 else if (VECTORP (prop))
4602 {
4603 /* A vector of sub-properties. */
4604 int i;
4605 for (i = 0; i < ASIZE (prop); ++i)
4606 if (single_display_spec_string_p (AREF (prop, i), string))
4607 return 1;
4608 }
4609 else
4610 return single_display_spec_string_p (prop, string);
4611
4612 return 0;
4613 }
4614
4615 /* Look for STRING in overlays and text properties in W's buffer,
4616 between character positions FROM and TO (excluding TO).
4617 BACK_P non-zero means look back (in this case, TO is supposed to be
4618 less than FROM).
4619 Value is the first character position where STRING was found, or
4620 zero if it wasn't found before hitting TO.
4621
4622 W's buffer must be current.
4623
4624 This function may only use code that doesn't eval because it is
4625 called asynchronously from note_mouse_highlight. */
4626
4627 static EMACS_INT
4628 string_buffer_position_lim (w, string, from, to, back_p)
4629 struct window *w;
4630 Lisp_Object string;
4631 EMACS_INT from, to;
4632 int back_p;
4633 {
4634 Lisp_Object limit, prop, pos;
4635 int found = 0;
4636
4637 pos = make_number (from);
4638
4639 if (!back_p) /* looking forward */
4640 {
4641 limit = make_number (min (to, ZV));
4642 while (!found && !EQ (pos, limit))
4643 {
4644 prop = Fget_char_property (pos, Qdisplay, Qnil);
4645 if (!NILP (prop) && display_prop_string_p (prop, string))
4646 found = 1;
4647 else
4648 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4649 limit);
4650 }
4651 }
4652 else /* looking back */
4653 {
4654 limit = make_number (max (to, BEGV));
4655 while (!found && !EQ (pos, limit))
4656 {
4657 prop = Fget_char_property (pos, Qdisplay, Qnil);
4658 if (!NILP (prop) && display_prop_string_p (prop, string))
4659 found = 1;
4660 else
4661 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4662 limit);
4663 }
4664 }
4665
4666 return found ? XINT (pos) : 0;
4667 }
4668
4669 /* Determine which buffer position in W's buffer STRING comes from.
4670 AROUND_CHARPOS is an approximate position where it could come from.
4671 Value is the buffer position or 0 if it couldn't be determined.
4672
4673 W's buffer must be current.
4674
4675 This function is necessary because we don't record buffer positions
4676 in glyphs generated from strings (to keep struct glyph small).
4677 This function may only use code that doesn't eval because it is
4678 called asynchronously from note_mouse_highlight. */
4679
4680 EMACS_INT
4681 string_buffer_position (w, string, around_charpos)
4682 struct window *w;
4683 Lisp_Object string;
4684 EMACS_INT around_charpos;
4685 {
4686 Lisp_Object limit, prop, pos;
4687 const int MAX_DISTANCE = 1000;
4688 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4689 around_charpos + MAX_DISTANCE,
4690 0);
4691
4692 if (!found)
4693 found = string_buffer_position_lim (w, string, around_charpos,
4694 around_charpos - MAX_DISTANCE, 1);
4695 return found;
4696 }
4697
4698
4699 \f
4700 /***********************************************************************
4701 `composition' property
4702 ***********************************************************************/
4703
4704 /* Set up iterator IT from `composition' property at its current
4705 position. Called from handle_stop. */
4706
4707 static enum prop_handled
4708 handle_composition_prop (it)
4709 struct it *it;
4710 {
4711 Lisp_Object prop, string;
4712 EMACS_INT pos, pos_byte, start, end;
4713
4714 if (STRINGP (it->string))
4715 {
4716 unsigned char *s;
4717
4718 pos = IT_STRING_CHARPOS (*it);
4719 pos_byte = IT_STRING_BYTEPOS (*it);
4720 string = it->string;
4721 s = SDATA (string) + pos_byte;
4722 it->c = STRING_CHAR (s);
4723 }
4724 else
4725 {
4726 pos = IT_CHARPOS (*it);
4727 pos_byte = IT_BYTEPOS (*it);
4728 string = Qnil;
4729 it->c = FETCH_CHAR (pos_byte);
4730 }
4731
4732 /* If there's a valid composition and point is not inside of the
4733 composition (in the case that the composition is from the current
4734 buffer), draw a glyph composed from the composition components. */
4735 if (find_composition (pos, -1, &start, &end, &prop, string)
4736 && COMPOSITION_VALID_P (start, end, prop)
4737 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4738 {
4739 if (start != pos)
4740 {
4741 if (STRINGP (it->string))
4742 pos_byte = string_char_to_byte (it->string, start);
4743 else
4744 pos_byte = CHAR_TO_BYTE (start);
4745 }
4746 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4747 prop, string);
4748
4749 if (it->cmp_it.id >= 0)
4750 {
4751 it->cmp_it.ch = -1;
4752 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4753 it->cmp_it.nglyphs = -1;
4754 }
4755 }
4756
4757 return HANDLED_NORMALLY;
4758 }
4759
4760
4761 \f
4762 /***********************************************************************
4763 Overlay strings
4764 ***********************************************************************/
4765
4766 /* The following structure is used to record overlay strings for
4767 later sorting in load_overlay_strings. */
4768
4769 struct overlay_entry
4770 {
4771 Lisp_Object overlay;
4772 Lisp_Object string;
4773 int priority;
4774 int after_string_p;
4775 };
4776
4777
4778 /* Set up iterator IT from overlay strings at its current position.
4779 Called from handle_stop. */
4780
4781 static enum prop_handled
4782 handle_overlay_change (it)
4783 struct it *it;
4784 {
4785 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4786 return HANDLED_RECOMPUTE_PROPS;
4787 else
4788 return HANDLED_NORMALLY;
4789 }
4790
4791
4792 /* Set up the next overlay string for delivery by IT, if there is an
4793 overlay string to deliver. Called by set_iterator_to_next when the
4794 end of the current overlay string is reached. If there are more
4795 overlay strings to display, IT->string and
4796 IT->current.overlay_string_index are set appropriately here.
4797 Otherwise IT->string is set to nil. */
4798
4799 static void
4800 next_overlay_string (it)
4801 struct it *it;
4802 {
4803 ++it->current.overlay_string_index;
4804 if (it->current.overlay_string_index == it->n_overlay_strings)
4805 {
4806 /* No more overlay strings. Restore IT's settings to what
4807 they were before overlay strings were processed, and
4808 continue to deliver from current_buffer. */
4809
4810 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4811 pop_it (it);
4812 xassert (it->sp > 0
4813 || (NILP (it->string)
4814 && it->method == GET_FROM_BUFFER
4815 && it->stop_charpos >= BEGV
4816 && it->stop_charpos <= it->end_charpos));
4817 it->current.overlay_string_index = -1;
4818 it->n_overlay_strings = 0;
4819
4820 /* If we're at the end of the buffer, record that we have
4821 processed the overlay strings there already, so that
4822 next_element_from_buffer doesn't try it again. */
4823 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4824 it->overlay_strings_at_end_processed_p = 1;
4825 }
4826 else
4827 {
4828 /* There are more overlay strings to process. If
4829 IT->current.overlay_string_index has advanced to a position
4830 where we must load IT->overlay_strings with more strings, do
4831 it. */
4832 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4833
4834 if (it->current.overlay_string_index && i == 0)
4835 load_overlay_strings (it, 0);
4836
4837 /* Initialize IT to deliver display elements from the overlay
4838 string. */
4839 it->string = it->overlay_strings[i];
4840 it->multibyte_p = STRING_MULTIBYTE (it->string);
4841 SET_TEXT_POS (it->current.string_pos, 0, 0);
4842 it->method = GET_FROM_STRING;
4843 it->stop_charpos = 0;
4844 if (it->cmp_it.stop_pos >= 0)
4845 it->cmp_it.stop_pos = 0;
4846 }
4847
4848 CHECK_IT (it);
4849 }
4850
4851
4852 /* Compare two overlay_entry structures E1 and E2. Used as a
4853 comparison function for qsort in load_overlay_strings. Overlay
4854 strings for the same position are sorted so that
4855
4856 1. All after-strings come in front of before-strings, except
4857 when they come from the same overlay.
4858
4859 2. Within after-strings, strings are sorted so that overlay strings
4860 from overlays with higher priorities come first.
4861
4862 2. Within before-strings, strings are sorted so that overlay
4863 strings from overlays with higher priorities come last.
4864
4865 Value is analogous to strcmp. */
4866
4867
4868 static int
4869 compare_overlay_entries (e1, e2)
4870 void *e1, *e2;
4871 {
4872 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4873 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4874 int result;
4875
4876 if (entry1->after_string_p != entry2->after_string_p)
4877 {
4878 /* Let after-strings appear in front of before-strings if
4879 they come from different overlays. */
4880 if (EQ (entry1->overlay, entry2->overlay))
4881 result = entry1->after_string_p ? 1 : -1;
4882 else
4883 result = entry1->after_string_p ? -1 : 1;
4884 }
4885 else if (entry1->after_string_p)
4886 /* After-strings sorted in order of decreasing priority. */
4887 result = entry2->priority - entry1->priority;
4888 else
4889 /* Before-strings sorted in order of increasing priority. */
4890 result = entry1->priority - entry2->priority;
4891
4892 return result;
4893 }
4894
4895
4896 /* Load the vector IT->overlay_strings with overlay strings from IT's
4897 current buffer position, or from CHARPOS if that is > 0. Set
4898 IT->n_overlays to the total number of overlay strings found.
4899
4900 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4901 a time. On entry into load_overlay_strings,
4902 IT->current.overlay_string_index gives the number of overlay
4903 strings that have already been loaded by previous calls to this
4904 function.
4905
4906 IT->add_overlay_start contains an additional overlay start
4907 position to consider for taking overlay strings from, if non-zero.
4908 This position comes into play when the overlay has an `invisible'
4909 property, and both before and after-strings. When we've skipped to
4910 the end of the overlay, because of its `invisible' property, we
4911 nevertheless want its before-string to appear.
4912 IT->add_overlay_start will contain the overlay start position
4913 in this case.
4914
4915 Overlay strings are sorted so that after-string strings come in
4916 front of before-string strings. Within before and after-strings,
4917 strings are sorted by overlay priority. See also function
4918 compare_overlay_entries. */
4919
4920 static void
4921 load_overlay_strings (it, charpos)
4922 struct it *it;
4923 int charpos;
4924 {
4925 extern Lisp_Object Qwindow, Qpriority;
4926 Lisp_Object overlay, window, str, invisible;
4927 struct Lisp_Overlay *ov;
4928 int start, end;
4929 int size = 20;
4930 int n = 0, i, j, invis_p;
4931 struct overlay_entry *entries
4932 = (struct overlay_entry *) alloca (size * sizeof *entries);
4933
4934 if (charpos <= 0)
4935 charpos = IT_CHARPOS (*it);
4936
4937 /* Append the overlay string STRING of overlay OVERLAY to vector
4938 `entries' which has size `size' and currently contains `n'
4939 elements. AFTER_P non-zero means STRING is an after-string of
4940 OVERLAY. */
4941 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4942 do \
4943 { \
4944 Lisp_Object priority; \
4945 \
4946 if (n == size) \
4947 { \
4948 int new_size = 2 * size; \
4949 struct overlay_entry *old = entries; \
4950 entries = \
4951 (struct overlay_entry *) alloca (new_size \
4952 * sizeof *entries); \
4953 bcopy (old, entries, size * sizeof *entries); \
4954 size = new_size; \
4955 } \
4956 \
4957 entries[n].string = (STRING); \
4958 entries[n].overlay = (OVERLAY); \
4959 priority = Foverlay_get ((OVERLAY), Qpriority); \
4960 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4961 entries[n].after_string_p = (AFTER_P); \
4962 ++n; \
4963 } \
4964 while (0)
4965
4966 /* Process overlay before the overlay center. */
4967 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4968 {
4969 XSETMISC (overlay, ov);
4970 xassert (OVERLAYP (overlay));
4971 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4972 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4973
4974 if (end < charpos)
4975 break;
4976
4977 /* Skip this overlay if it doesn't start or end at IT's current
4978 position. */
4979 if (end != charpos && start != charpos)
4980 continue;
4981
4982 /* Skip this overlay if it doesn't apply to IT->w. */
4983 window = Foverlay_get (overlay, Qwindow);
4984 if (WINDOWP (window) && XWINDOW (window) != it->w)
4985 continue;
4986
4987 /* If the text ``under'' the overlay is invisible, both before-
4988 and after-strings from this overlay are visible; start and
4989 end position are indistinguishable. */
4990 invisible = Foverlay_get (overlay, Qinvisible);
4991 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4992
4993 /* If overlay has a non-empty before-string, record it. */
4994 if ((start == charpos || (end == charpos && invis_p))
4995 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4996 && SCHARS (str))
4997 RECORD_OVERLAY_STRING (overlay, str, 0);
4998
4999 /* If overlay has a non-empty after-string, record it. */
5000 if ((end == charpos || (start == charpos && invis_p))
5001 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5002 && SCHARS (str))
5003 RECORD_OVERLAY_STRING (overlay, str, 1);
5004 }
5005
5006 /* Process overlays after the overlay center. */
5007 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5008 {
5009 XSETMISC (overlay, ov);
5010 xassert (OVERLAYP (overlay));
5011 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5012 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5013
5014 if (start > charpos)
5015 break;
5016
5017 /* Skip this overlay if it doesn't start or end at IT's current
5018 position. */
5019 if (end != charpos && start != charpos)
5020 continue;
5021
5022 /* Skip this overlay if it doesn't apply to IT->w. */
5023 window = Foverlay_get (overlay, Qwindow);
5024 if (WINDOWP (window) && XWINDOW (window) != it->w)
5025 continue;
5026
5027 /* If the text ``under'' the overlay is invisible, it has a zero
5028 dimension, and both before- and after-strings apply. */
5029 invisible = Foverlay_get (overlay, Qinvisible);
5030 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5031
5032 /* If overlay has a non-empty before-string, record it. */
5033 if ((start == charpos || (end == charpos && invis_p))
5034 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5035 && SCHARS (str))
5036 RECORD_OVERLAY_STRING (overlay, str, 0);
5037
5038 /* If overlay has a non-empty after-string, record it. */
5039 if ((end == charpos || (start == charpos && invis_p))
5040 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5041 && SCHARS (str))
5042 RECORD_OVERLAY_STRING (overlay, str, 1);
5043 }
5044
5045 #undef RECORD_OVERLAY_STRING
5046
5047 /* Sort entries. */
5048 if (n > 1)
5049 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5050
5051 /* Record the total number of strings to process. */
5052 it->n_overlay_strings = n;
5053
5054 /* IT->current.overlay_string_index is the number of overlay strings
5055 that have already been consumed by IT. Copy some of the
5056 remaining overlay strings to IT->overlay_strings. */
5057 i = 0;
5058 j = it->current.overlay_string_index;
5059 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5060 {
5061 it->overlay_strings[i] = entries[j].string;
5062 it->string_overlays[i++] = entries[j++].overlay;
5063 }
5064
5065 CHECK_IT (it);
5066 }
5067
5068
5069 /* Get the first chunk of overlay strings at IT's current buffer
5070 position, or at CHARPOS if that is > 0. Value is non-zero if at
5071 least one overlay string was found. */
5072
5073 static int
5074 get_overlay_strings_1 (it, charpos, compute_stop_p)
5075 struct it *it;
5076 int charpos;
5077 int compute_stop_p;
5078 {
5079 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5080 process. This fills IT->overlay_strings with strings, and sets
5081 IT->n_overlay_strings to the total number of strings to process.
5082 IT->pos.overlay_string_index has to be set temporarily to zero
5083 because load_overlay_strings needs this; it must be set to -1
5084 when no overlay strings are found because a zero value would
5085 indicate a position in the first overlay string. */
5086 it->current.overlay_string_index = 0;
5087 load_overlay_strings (it, charpos);
5088
5089 /* If we found overlay strings, set up IT to deliver display
5090 elements from the first one. Otherwise set up IT to deliver
5091 from current_buffer. */
5092 if (it->n_overlay_strings)
5093 {
5094 /* Make sure we know settings in current_buffer, so that we can
5095 restore meaningful values when we're done with the overlay
5096 strings. */
5097 if (compute_stop_p)
5098 compute_stop_pos (it);
5099 xassert (it->face_id >= 0);
5100
5101 /* Save IT's settings. They are restored after all overlay
5102 strings have been processed. */
5103 xassert (!compute_stop_p || it->sp == 0);
5104
5105 /* When called from handle_stop, there might be an empty display
5106 string loaded. In that case, don't bother saving it. */
5107 if (!STRINGP (it->string) || SCHARS (it->string))
5108 push_it (it);
5109
5110 /* Set up IT to deliver display elements from the first overlay
5111 string. */
5112 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5113 it->string = it->overlay_strings[0];
5114 it->from_overlay = Qnil;
5115 it->stop_charpos = 0;
5116 xassert (STRINGP (it->string));
5117 it->end_charpos = SCHARS (it->string);
5118 it->multibyte_p = STRING_MULTIBYTE (it->string);
5119 it->method = GET_FROM_STRING;
5120 return 1;
5121 }
5122
5123 it->current.overlay_string_index = -1;
5124 return 0;
5125 }
5126
5127 static int
5128 get_overlay_strings (it, charpos)
5129 struct it *it;
5130 int charpos;
5131 {
5132 it->string = Qnil;
5133 it->method = GET_FROM_BUFFER;
5134
5135 (void) get_overlay_strings_1 (it, charpos, 1);
5136
5137 CHECK_IT (it);
5138
5139 /* Value is non-zero if we found at least one overlay string. */
5140 return STRINGP (it->string);
5141 }
5142
5143
5144 \f
5145 /***********************************************************************
5146 Saving and restoring state
5147 ***********************************************************************/
5148
5149 /* Save current settings of IT on IT->stack. Called, for example,
5150 before setting up IT for an overlay string, to be able to restore
5151 IT's settings to what they were after the overlay string has been
5152 processed. */
5153
5154 static void
5155 push_it (it)
5156 struct it *it;
5157 {
5158 struct iterator_stack_entry *p;
5159
5160 xassert (it->sp < IT_STACK_SIZE);
5161 p = it->stack + it->sp;
5162
5163 p->stop_charpos = it->stop_charpos;
5164 p->prev_stop = it->prev_stop;
5165 p->base_level_stop = it->base_level_stop;
5166 p->cmp_it = it->cmp_it;
5167 xassert (it->face_id >= 0);
5168 p->face_id = it->face_id;
5169 p->string = it->string;
5170 p->method = it->method;
5171 p->from_overlay = it->from_overlay;
5172 switch (p->method)
5173 {
5174 case GET_FROM_IMAGE:
5175 p->u.image.object = it->object;
5176 p->u.image.image_id = it->image_id;
5177 p->u.image.slice = it->slice;
5178 break;
5179 case GET_FROM_STRETCH:
5180 p->u.stretch.object = it->object;
5181 break;
5182 }
5183 p->position = it->position;
5184 p->current = it->current;
5185 p->end_charpos = it->end_charpos;
5186 p->string_nchars = it->string_nchars;
5187 p->area = it->area;
5188 p->multibyte_p = it->multibyte_p;
5189 p->avoid_cursor_p = it->avoid_cursor_p;
5190 p->space_width = it->space_width;
5191 p->font_height = it->font_height;
5192 p->voffset = it->voffset;
5193 p->string_from_display_prop_p = it->string_from_display_prop_p;
5194 p->display_ellipsis_p = 0;
5195 p->line_wrap = it->line_wrap;
5196 ++it->sp;
5197 }
5198
5199
5200 /* Restore IT's settings from IT->stack. Called, for example, when no
5201 more overlay strings must be processed, and we return to delivering
5202 display elements from a buffer, or when the end of a string from a
5203 `display' property is reached and we return to delivering display
5204 elements from an overlay string, or from a buffer. */
5205
5206 static void
5207 pop_it (it)
5208 struct it *it;
5209 {
5210 struct iterator_stack_entry *p;
5211
5212 xassert (it->sp > 0);
5213 --it->sp;
5214 p = it->stack + it->sp;
5215 it->stop_charpos = p->stop_charpos;
5216 it->prev_stop = p->prev_stop;
5217 it->base_level_stop = p->base_level_stop;
5218 it->cmp_it = p->cmp_it;
5219 it->face_id = p->face_id;
5220 it->current = p->current;
5221 it->position = p->position;
5222 it->string = p->string;
5223 it->from_overlay = p->from_overlay;
5224 if (NILP (it->string))
5225 SET_TEXT_POS (it->current.string_pos, -1, -1);
5226 it->method = p->method;
5227 switch (it->method)
5228 {
5229 case GET_FROM_IMAGE:
5230 it->image_id = p->u.image.image_id;
5231 it->object = p->u.image.object;
5232 it->slice = p->u.image.slice;
5233 break;
5234 case GET_FROM_STRETCH:
5235 it->object = p->u.comp.object;
5236 break;
5237 case GET_FROM_BUFFER:
5238 it->object = it->w->buffer;
5239 break;
5240 case GET_FROM_STRING:
5241 it->object = it->string;
5242 break;
5243 case GET_FROM_DISPLAY_VECTOR:
5244 if (it->s)
5245 it->method = GET_FROM_C_STRING;
5246 else if (STRINGP (it->string))
5247 it->method = GET_FROM_STRING;
5248 else
5249 {
5250 it->method = GET_FROM_BUFFER;
5251 it->object = it->w->buffer;
5252 }
5253 }
5254 it->end_charpos = p->end_charpos;
5255 it->string_nchars = p->string_nchars;
5256 it->area = p->area;
5257 it->multibyte_p = p->multibyte_p;
5258 it->avoid_cursor_p = p->avoid_cursor_p;
5259 it->space_width = p->space_width;
5260 it->font_height = p->font_height;
5261 it->voffset = p->voffset;
5262 it->string_from_display_prop_p = p->string_from_display_prop_p;
5263 it->line_wrap = p->line_wrap;
5264 }
5265
5266
5267 \f
5268 /***********************************************************************
5269 Moving over lines
5270 ***********************************************************************/
5271
5272 /* Set IT's current position to the previous line start. */
5273
5274 static void
5275 back_to_previous_line_start (it)
5276 struct it *it;
5277 {
5278 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5279 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5280 }
5281
5282
5283 /* Move IT to the next line start.
5284
5285 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5286 we skipped over part of the text (as opposed to moving the iterator
5287 continuously over the text). Otherwise, don't change the value
5288 of *SKIPPED_P.
5289
5290 Newlines may come from buffer text, overlay strings, or strings
5291 displayed via the `display' property. That's the reason we can't
5292 simply use find_next_newline_no_quit.
5293
5294 Note that this function may not skip over invisible text that is so
5295 because of text properties and immediately follows a newline. If
5296 it would, function reseat_at_next_visible_line_start, when called
5297 from set_iterator_to_next, would effectively make invisible
5298 characters following a newline part of the wrong glyph row, which
5299 leads to wrong cursor motion. */
5300
5301 static int
5302 forward_to_next_line_start (it, skipped_p)
5303 struct it *it;
5304 int *skipped_p;
5305 {
5306 int old_selective, newline_found_p, n;
5307 const int MAX_NEWLINE_DISTANCE = 500;
5308
5309 /* If already on a newline, just consume it to avoid unintended
5310 skipping over invisible text below. */
5311 if (it->what == IT_CHARACTER
5312 && it->c == '\n'
5313 && CHARPOS (it->position) == IT_CHARPOS (*it))
5314 {
5315 set_iterator_to_next (it, 0);
5316 it->c = 0;
5317 return 1;
5318 }
5319
5320 /* Don't handle selective display in the following. It's (a)
5321 unnecessary because it's done by the caller, and (b) leads to an
5322 infinite recursion because next_element_from_ellipsis indirectly
5323 calls this function. */
5324 old_selective = it->selective;
5325 it->selective = 0;
5326
5327 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5328 from buffer text. */
5329 for (n = newline_found_p = 0;
5330 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5331 n += STRINGP (it->string) ? 0 : 1)
5332 {
5333 if (!get_next_display_element (it))
5334 return 0;
5335 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5336 set_iterator_to_next (it, 0);
5337 }
5338
5339 /* If we didn't find a newline near enough, see if we can use a
5340 short-cut. */
5341 if (!newline_found_p)
5342 {
5343 int start = IT_CHARPOS (*it);
5344 int limit = find_next_newline_no_quit (start, 1);
5345 Lisp_Object pos;
5346
5347 xassert (!STRINGP (it->string));
5348
5349 /* If there isn't any `display' property in sight, and no
5350 overlays, we can just use the position of the newline in
5351 buffer text. */
5352 if (it->stop_charpos >= limit
5353 || ((pos = Fnext_single_property_change (make_number (start),
5354 Qdisplay,
5355 Qnil, make_number (limit)),
5356 NILP (pos))
5357 && next_overlay_change (start) == ZV))
5358 {
5359 IT_CHARPOS (*it) = limit;
5360 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5361 *skipped_p = newline_found_p = 1;
5362 }
5363 else
5364 {
5365 while (get_next_display_element (it)
5366 && !newline_found_p)
5367 {
5368 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5369 set_iterator_to_next (it, 0);
5370 }
5371 }
5372 }
5373
5374 it->selective = old_selective;
5375 return newline_found_p;
5376 }
5377
5378
5379 /* Set IT's current position to the previous visible line start. Skip
5380 invisible text that is so either due to text properties or due to
5381 selective display. Caution: this does not change IT->current_x and
5382 IT->hpos. */
5383
5384 static void
5385 back_to_previous_visible_line_start (it)
5386 struct it *it;
5387 {
5388 while (IT_CHARPOS (*it) > BEGV)
5389 {
5390 back_to_previous_line_start (it);
5391
5392 if (IT_CHARPOS (*it) <= BEGV)
5393 break;
5394
5395 /* If selective > 0, then lines indented more than its value are
5396 invisible. */
5397 if (it->selective > 0
5398 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5399 (double) it->selective)) /* iftc */
5400 continue;
5401
5402 /* Check the newline before point for invisibility. */
5403 {
5404 Lisp_Object prop;
5405 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5406 Qinvisible, it->window);
5407 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5408 continue;
5409 }
5410
5411 if (IT_CHARPOS (*it) <= BEGV)
5412 break;
5413
5414 {
5415 struct it it2;
5416 int pos;
5417 EMACS_INT beg, end;
5418 Lisp_Object val, overlay;
5419
5420 /* If newline is part of a composition, continue from start of composition */
5421 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5422 && beg < IT_CHARPOS (*it))
5423 goto replaced;
5424
5425 /* If newline is replaced by a display property, find start of overlay
5426 or interval and continue search from that point. */
5427 it2 = *it;
5428 pos = --IT_CHARPOS (it2);
5429 --IT_BYTEPOS (it2);
5430 it2.sp = 0;
5431 it2.string_from_display_prop_p = 0;
5432 if (handle_display_prop (&it2) == HANDLED_RETURN
5433 && !NILP (val = get_char_property_and_overlay
5434 (make_number (pos), Qdisplay, Qnil, &overlay))
5435 && (OVERLAYP (overlay)
5436 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5437 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5438 goto replaced;
5439
5440 /* Newline is not replaced by anything -- so we are done. */
5441 break;
5442
5443 replaced:
5444 if (beg < BEGV)
5445 beg = BEGV;
5446 IT_CHARPOS (*it) = beg;
5447 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5448 }
5449 }
5450
5451 it->continuation_lines_width = 0;
5452
5453 xassert (IT_CHARPOS (*it) >= BEGV);
5454 xassert (IT_CHARPOS (*it) == BEGV
5455 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5456 CHECK_IT (it);
5457 }
5458
5459
5460 /* Reseat iterator IT at the previous visible line start. Skip
5461 invisible text that is so either due to text properties or due to
5462 selective display. At the end, update IT's overlay information,
5463 face information etc. */
5464
5465 void
5466 reseat_at_previous_visible_line_start (it)
5467 struct it *it;
5468 {
5469 back_to_previous_visible_line_start (it);
5470 reseat (it, it->current.pos, 1);
5471 CHECK_IT (it);
5472 }
5473
5474
5475 /* Reseat iterator IT on the next visible line start in the current
5476 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5477 preceding the line start. Skip over invisible text that is so
5478 because of selective display. Compute faces, overlays etc at the
5479 new position. Note that this function does not skip over text that
5480 is invisible because of text properties. */
5481
5482 static void
5483 reseat_at_next_visible_line_start (it, on_newline_p)
5484 struct it *it;
5485 int on_newline_p;
5486 {
5487 int newline_found_p, skipped_p = 0;
5488
5489 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5490
5491 /* Skip over lines that are invisible because they are indented
5492 more than the value of IT->selective. */
5493 if (it->selective > 0)
5494 while (IT_CHARPOS (*it) < ZV
5495 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5496 (double) it->selective)) /* iftc */
5497 {
5498 xassert (IT_BYTEPOS (*it) == BEGV
5499 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5500 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5501 }
5502
5503 /* Position on the newline if that's what's requested. */
5504 if (on_newline_p && newline_found_p)
5505 {
5506 if (STRINGP (it->string))
5507 {
5508 if (IT_STRING_CHARPOS (*it) > 0)
5509 {
5510 --IT_STRING_CHARPOS (*it);
5511 --IT_STRING_BYTEPOS (*it);
5512 }
5513 }
5514 else if (IT_CHARPOS (*it) > BEGV)
5515 {
5516 --IT_CHARPOS (*it);
5517 --IT_BYTEPOS (*it);
5518 reseat (it, it->current.pos, 0);
5519 }
5520 }
5521 else if (skipped_p)
5522 reseat (it, it->current.pos, 0);
5523
5524 CHECK_IT (it);
5525 }
5526
5527
5528 \f
5529 /***********************************************************************
5530 Changing an iterator's position
5531 ***********************************************************************/
5532
5533 /* Change IT's current position to POS in current_buffer. If FORCE_P
5534 is non-zero, always check for text properties at the new position.
5535 Otherwise, text properties are only looked up if POS >=
5536 IT->check_charpos of a property. */
5537
5538 static void
5539 reseat (it, pos, force_p)
5540 struct it *it;
5541 struct text_pos pos;
5542 int force_p;
5543 {
5544 int original_pos = IT_CHARPOS (*it);
5545
5546 reseat_1 (it, pos, 0);
5547
5548 /* Determine where to check text properties. Avoid doing it
5549 where possible because text property lookup is very expensive. */
5550 if (force_p
5551 || CHARPOS (pos) > it->stop_charpos
5552 || CHARPOS (pos) < original_pos)
5553 {
5554 if (it->bidi_p)
5555 {
5556 /* For bidi iteration, we need to prime prev_stop and
5557 base_level_stop with our best estimations. */
5558 if (CHARPOS (pos) < it->prev_stop)
5559 {
5560 handle_stop_backwards (it, BEGV);
5561 if (CHARPOS (pos) < it->base_level_stop)
5562 it->base_level_stop = 0;
5563 }
5564 else if (CHARPOS (pos) > it->stop_charpos
5565 && it->stop_charpos >= BEGV)
5566 handle_stop_backwards (it, it->stop_charpos);
5567 else /* force_p */
5568 handle_stop (it);
5569 }
5570 else
5571 {
5572 handle_stop (it);
5573 it->prev_stop = it->base_level_stop = 0;
5574 }
5575
5576 }
5577
5578 CHECK_IT (it);
5579 }
5580
5581
5582 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5583 IT->stop_pos to POS, also. */
5584
5585 static void
5586 reseat_1 (it, pos, set_stop_p)
5587 struct it *it;
5588 struct text_pos pos;
5589 int set_stop_p;
5590 {
5591 /* Don't call this function when scanning a C string. */
5592 xassert (it->s == NULL);
5593
5594 /* POS must be a reasonable value. */
5595 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5596
5597 it->current.pos = it->position = pos;
5598 it->end_charpos = ZV;
5599 it->dpvec = NULL;
5600 it->current.dpvec_index = -1;
5601 it->current.overlay_string_index = -1;
5602 IT_STRING_CHARPOS (*it) = -1;
5603 IT_STRING_BYTEPOS (*it) = -1;
5604 it->string = Qnil;
5605 it->string_from_display_prop_p = 0;
5606 it->method = GET_FROM_BUFFER;
5607 it->object = it->w->buffer;
5608 it->area = TEXT_AREA;
5609 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5610 it->sp = 0;
5611 it->string_from_display_prop_p = 0;
5612 it->face_before_selective_p = 0;
5613 if (it->bidi_p)
5614 it->bidi_it.first_elt = 1;
5615
5616 if (set_stop_p)
5617 {
5618 it->stop_charpos = CHARPOS (pos);
5619 it->base_level_stop = CHARPOS (pos);
5620 }
5621 }
5622
5623
5624 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5625 If S is non-null, it is a C string to iterate over. Otherwise,
5626 STRING gives a Lisp string to iterate over.
5627
5628 If PRECISION > 0, don't return more then PRECISION number of
5629 characters from the string.
5630
5631 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5632 characters have been returned. FIELD_WIDTH < 0 means an infinite
5633 field width.
5634
5635 MULTIBYTE = 0 means disable processing of multibyte characters,
5636 MULTIBYTE > 0 means enable it,
5637 MULTIBYTE < 0 means use IT->multibyte_p.
5638
5639 IT must be initialized via a prior call to init_iterator before
5640 calling this function. */
5641
5642 static void
5643 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5644 struct it *it;
5645 unsigned char *s;
5646 Lisp_Object string;
5647 int charpos;
5648 int precision, field_width, multibyte;
5649 {
5650 /* No region in strings. */
5651 it->region_beg_charpos = it->region_end_charpos = -1;
5652
5653 /* No text property checks performed by default, but see below. */
5654 it->stop_charpos = -1;
5655
5656 /* Set iterator position and end position. */
5657 bzero (&it->current, sizeof it->current);
5658 it->current.overlay_string_index = -1;
5659 it->current.dpvec_index = -1;
5660 xassert (charpos >= 0);
5661
5662 /* If STRING is specified, use its multibyteness, otherwise use the
5663 setting of MULTIBYTE, if specified. */
5664 if (multibyte >= 0)
5665 it->multibyte_p = multibyte > 0;
5666
5667 if (s == NULL)
5668 {
5669 xassert (STRINGP (string));
5670 it->string = string;
5671 it->s = NULL;
5672 it->end_charpos = it->string_nchars = SCHARS (string);
5673 it->method = GET_FROM_STRING;
5674 it->current.string_pos = string_pos (charpos, string);
5675 }
5676 else
5677 {
5678 it->s = s;
5679 it->string = Qnil;
5680
5681 /* Note that we use IT->current.pos, not it->current.string_pos,
5682 for displaying C strings. */
5683 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5684 if (it->multibyte_p)
5685 {
5686 it->current.pos = c_string_pos (charpos, s, 1);
5687 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5688 }
5689 else
5690 {
5691 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5692 it->end_charpos = it->string_nchars = strlen (s);
5693 }
5694
5695 it->method = GET_FROM_C_STRING;
5696 }
5697
5698 /* PRECISION > 0 means don't return more than PRECISION characters
5699 from the string. */
5700 if (precision > 0 && it->end_charpos - charpos > precision)
5701 it->end_charpos = it->string_nchars = charpos + precision;
5702
5703 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5704 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5705 FIELD_WIDTH < 0 means infinite field width. This is useful for
5706 padding with `-' at the end of a mode line. */
5707 if (field_width < 0)
5708 field_width = INFINITY;
5709 if (field_width > it->end_charpos - charpos)
5710 it->end_charpos = charpos + field_width;
5711
5712 /* Use the standard display table for displaying strings. */
5713 if (DISP_TABLE_P (Vstandard_display_table))
5714 it->dp = XCHAR_TABLE (Vstandard_display_table);
5715
5716 it->stop_charpos = charpos;
5717 if (s == NULL && it->multibyte_p)
5718 composition_compute_stop_pos (&it->cmp_it, charpos, -1, it->end_charpos,
5719 it->string);
5720 CHECK_IT (it);
5721 }
5722
5723
5724 \f
5725 /***********************************************************************
5726 Iteration
5727 ***********************************************************************/
5728
5729 /* Map enum it_method value to corresponding next_element_from_* function. */
5730
5731 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5732 {
5733 next_element_from_buffer,
5734 next_element_from_display_vector,
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
5744 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5745 (possibly with the following characters). */
5746
5747 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5748 ((IT)->cmp_it.id >= 0 \
5749 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5750 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5751 END_CHARPOS, (IT)->w, \
5752 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5753 (IT)->string)))
5754
5755
5756 /* Load IT's display element fields with information about the next
5757 display element from the current position of IT. Value is zero if
5758 end of buffer (or C string) is reached. */
5759
5760 static struct frame *last_escape_glyph_frame = NULL;
5761 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5762 static int last_escape_glyph_merged_face_id = 0;
5763
5764 int
5765 get_next_display_element (it)
5766 struct it *it;
5767 {
5768 /* Non-zero means that we found a display element. Zero means that
5769 we hit the end of what we iterate over. Performance note: the
5770 function pointer `method' used here turns out to be faster than
5771 using a sequence of if-statements. */
5772 int success_p;
5773
5774 get_next:
5775 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5776
5777 if (it->what == IT_CHARACTER)
5778 {
5779 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5780 and only if (a) the resolved directionality of that character
5781 is R..." */
5782 /* FIXME: Do we need an exception for characters from display
5783 tables? */
5784 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5785 it->c = bidi_mirror_char (it->c);
5786 /* Map via display table or translate control characters.
5787 IT->c, IT->len etc. have been set to the next character by
5788 the function call above. If we have a display table, and it
5789 contains an entry for IT->c, translate it. Don't do this if
5790 IT->c itself comes from a display table, otherwise we could
5791 end up in an infinite recursion. (An alternative could be to
5792 count the recursion depth of this function and signal an
5793 error when a certain maximum depth is reached.) Is it worth
5794 it? */
5795 if (success_p && it->dpvec == NULL)
5796 {
5797 Lisp_Object dv;
5798 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5799 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5800 nbsp_or_shy = char_is_other;
5801 int decoded = it->c;
5802
5803 if (it->dp
5804 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5805 VECTORP (dv)))
5806 {
5807 struct Lisp_Vector *v = XVECTOR (dv);
5808
5809 /* Return the first character from the display table
5810 entry, if not empty. If empty, don't display the
5811 current character. */
5812 if (v->size)
5813 {
5814 it->dpvec_char_len = it->len;
5815 it->dpvec = v->contents;
5816 it->dpend = v->contents + v->size;
5817 it->current.dpvec_index = 0;
5818 it->dpvec_face_id = -1;
5819 it->saved_face_id = it->face_id;
5820 it->method = GET_FROM_DISPLAY_VECTOR;
5821 it->ellipsis_p = 0;
5822 }
5823 else
5824 {
5825 set_iterator_to_next (it, 0);
5826 }
5827 goto get_next;
5828 }
5829
5830 if (unibyte_display_via_language_environment
5831 && !ASCII_CHAR_P (it->c))
5832 decoded = DECODE_CHAR (unibyte, it->c);
5833
5834 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5835 {
5836 if (it->multibyte_p)
5837 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5838 : it->c == 0xAD ? char_is_soft_hyphen
5839 : char_is_other);
5840 else if (unibyte_display_via_language_environment)
5841 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5842 : decoded == 0xAD ? char_is_soft_hyphen
5843 : char_is_other);
5844 }
5845
5846 /* Translate control characters into `\003' or `^C' form.
5847 Control characters coming from a display table entry are
5848 currently not translated because we use IT->dpvec to hold
5849 the translation. This could easily be changed but I
5850 don't believe that it is worth doing.
5851
5852 If it->multibyte_p is nonzero, non-printable non-ASCII
5853 characters are also translated to octal form.
5854
5855 If it->multibyte_p is zero, eight-bit characters that
5856 don't have corresponding multibyte char code are also
5857 translated to octal form. */
5858 if ((it->c < ' '
5859 ? (it->area != TEXT_AREA
5860 /* In mode line, treat \n, \t like other crl chars. */
5861 || (it->c != '\t'
5862 && it->glyph_row
5863 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5864 || (it->c != '\n' && it->c != '\t'))
5865 : (nbsp_or_shy
5866 || (it->multibyte_p
5867 ? ! CHAR_PRINTABLE_P (it->c)
5868 : (! unibyte_display_via_language_environment
5869 ? it->c >= 0x80
5870 : (decoded >= 0x80 && decoded < 0xA0))))))
5871 {
5872 /* IT->c is a control character which must be displayed
5873 either as '\003' or as `^C' where the '\\' and '^'
5874 can be defined in the display table. Fill
5875 IT->ctl_chars with glyphs for what we have to
5876 display. Then, set IT->dpvec to these glyphs. */
5877 Lisp_Object gc;
5878 int ctl_len;
5879 int face_id, lface_id = 0 ;
5880 int escape_glyph;
5881
5882 /* Handle control characters with ^. */
5883
5884 if (it->c < 128 && it->ctl_arrow_p)
5885 {
5886 int g;
5887
5888 g = '^'; /* default glyph for Control */
5889 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5890 if (it->dp
5891 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5892 && GLYPH_CODE_CHAR_VALID_P (gc))
5893 {
5894 g = GLYPH_CODE_CHAR (gc);
5895 lface_id = GLYPH_CODE_FACE (gc);
5896 }
5897 if (lface_id)
5898 {
5899 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5900 }
5901 else if (it->f == last_escape_glyph_frame
5902 && it->face_id == last_escape_glyph_face_id)
5903 {
5904 face_id = last_escape_glyph_merged_face_id;
5905 }
5906 else
5907 {
5908 /* Merge the escape-glyph face into the current face. */
5909 face_id = merge_faces (it->f, Qescape_glyph, 0,
5910 it->face_id);
5911 last_escape_glyph_frame = it->f;
5912 last_escape_glyph_face_id = it->face_id;
5913 last_escape_glyph_merged_face_id = face_id;
5914 }
5915
5916 XSETINT (it->ctl_chars[0], g);
5917 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5918 ctl_len = 2;
5919 goto display_control;
5920 }
5921
5922 /* Handle non-break space in the mode where it only gets
5923 highlighting. */
5924
5925 if (EQ (Vnobreak_char_display, Qt)
5926 && nbsp_or_shy == char_is_nbsp)
5927 {
5928 /* Merge the no-break-space face into the current face. */
5929 face_id = merge_faces (it->f, Qnobreak_space, 0,
5930 it->face_id);
5931
5932 it->c = ' ';
5933 XSETINT (it->ctl_chars[0], ' ');
5934 ctl_len = 1;
5935 goto display_control;
5936 }
5937
5938 /* Handle sequences that start with the "escape glyph". */
5939
5940 /* the default escape glyph is \. */
5941 escape_glyph = '\\';
5942
5943 if (it->dp
5944 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5945 && GLYPH_CODE_CHAR_VALID_P (gc))
5946 {
5947 escape_glyph = GLYPH_CODE_CHAR (gc);
5948 lface_id = GLYPH_CODE_FACE (gc);
5949 }
5950 if (lface_id)
5951 {
5952 /* The display table specified a face.
5953 Merge it into face_id and also into escape_glyph. */
5954 face_id = merge_faces (it->f, Qt, lface_id,
5955 it->face_id);
5956 }
5957 else if (it->f == last_escape_glyph_frame
5958 && it->face_id == last_escape_glyph_face_id)
5959 {
5960 face_id = last_escape_glyph_merged_face_id;
5961 }
5962 else
5963 {
5964 /* Merge the escape-glyph face into the current face. */
5965 face_id = merge_faces (it->f, Qescape_glyph, 0,
5966 it->face_id);
5967 last_escape_glyph_frame = it->f;
5968 last_escape_glyph_face_id = it->face_id;
5969 last_escape_glyph_merged_face_id = face_id;
5970 }
5971
5972 /* Handle soft hyphens in the mode where they only get
5973 highlighting. */
5974
5975 if (EQ (Vnobreak_char_display, Qt)
5976 && nbsp_or_shy == char_is_soft_hyphen)
5977 {
5978 it->c = '-';
5979 XSETINT (it->ctl_chars[0], '-');
5980 ctl_len = 1;
5981 goto display_control;
5982 }
5983
5984 /* Handle non-break space and soft hyphen
5985 with the escape glyph. */
5986
5987 if (nbsp_or_shy)
5988 {
5989 XSETINT (it->ctl_chars[0], escape_glyph);
5990 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5991 XSETINT (it->ctl_chars[1], it->c);
5992 ctl_len = 2;
5993 goto display_control;
5994 }
5995
5996 {
5997 unsigned char str[MAX_MULTIBYTE_LENGTH];
5998 int len;
5999 int i;
6000
6001 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
6002 if (CHAR_BYTE8_P (it->c))
6003 {
6004 str[0] = CHAR_TO_BYTE8 (it->c);
6005 len = 1;
6006 }
6007 else if (it->c < 256)
6008 {
6009 str[0] = it->c;
6010 len = 1;
6011 }
6012 else
6013 {
6014 /* It's an invalid character, which shouldn't
6015 happen actually, but due to bugs it may
6016 happen. Let's print the char as is, there's
6017 not much meaningful we can do with it. */
6018 str[0] = it->c;
6019 str[1] = it->c >> 8;
6020 str[2] = it->c >> 16;
6021 str[3] = it->c >> 24;
6022 len = 4;
6023 }
6024
6025 for (i = 0; i < len; i++)
6026 {
6027 int g;
6028 XSETINT (it->ctl_chars[i * 4], escape_glyph);
6029 /* Insert three more glyphs into IT->ctl_chars for
6030 the octal display of the character. */
6031 g = ((str[i] >> 6) & 7) + '0';
6032 XSETINT (it->ctl_chars[i * 4 + 1], g);
6033 g = ((str[i] >> 3) & 7) + '0';
6034 XSETINT (it->ctl_chars[i * 4 + 2], g);
6035 g = (str[i] & 7) + '0';
6036 XSETINT (it->ctl_chars[i * 4 + 3], g);
6037 }
6038 ctl_len = len * 4;
6039 }
6040
6041 display_control:
6042 /* Set up IT->dpvec and return first character from it. */
6043 it->dpvec_char_len = it->len;
6044 it->dpvec = it->ctl_chars;
6045 it->dpend = it->dpvec + ctl_len;
6046 it->current.dpvec_index = 0;
6047 it->dpvec_face_id = face_id;
6048 it->saved_face_id = it->face_id;
6049 it->method = GET_FROM_DISPLAY_VECTOR;
6050 it->ellipsis_p = 0;
6051 goto get_next;
6052 }
6053 }
6054 }
6055
6056 #ifdef HAVE_WINDOW_SYSTEM
6057 /* Adjust face id for a multibyte character. There are no multibyte
6058 character in unibyte text. */
6059 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6060 && it->multibyte_p
6061 && success_p
6062 && FRAME_WINDOW_P (it->f))
6063 {
6064 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6065
6066 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6067 {
6068 /* Automatic composition with glyph-string. */
6069 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6070
6071 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6072 }
6073 else
6074 {
6075 int pos = (it->s ? -1
6076 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6077 : IT_CHARPOS (*it));
6078
6079 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6080 }
6081 }
6082 #endif
6083
6084 /* Is this character the last one of a run of characters with
6085 box? If yes, set IT->end_of_box_run_p to 1. */
6086 if (it->face_box_p
6087 && it->s == NULL)
6088 {
6089 if (it->method == GET_FROM_STRING && it->sp)
6090 {
6091 int face_id = underlying_face_id (it);
6092 struct face *face = FACE_FROM_ID (it->f, face_id);
6093
6094 if (face)
6095 {
6096 if (face->box == FACE_NO_BOX)
6097 {
6098 /* If the box comes from face properties in a
6099 display string, check faces in that string. */
6100 int string_face_id = face_after_it_pos (it);
6101 it->end_of_box_run_p
6102 = (FACE_FROM_ID (it->f, string_face_id)->box
6103 == FACE_NO_BOX);
6104 }
6105 /* Otherwise, the box comes from the underlying face.
6106 If this is the last string character displayed, check
6107 the next buffer location. */
6108 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6109 && (it->current.overlay_string_index
6110 == it->n_overlay_strings - 1))
6111 {
6112 EMACS_INT ignore;
6113 int next_face_id;
6114 struct text_pos pos = it->current.pos;
6115 INC_TEXT_POS (pos, it->multibyte_p);
6116
6117 next_face_id = face_at_buffer_position
6118 (it->w, CHARPOS (pos), it->region_beg_charpos,
6119 it->region_end_charpos, &ignore,
6120 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6121 -1);
6122 it->end_of_box_run_p
6123 = (FACE_FROM_ID (it->f, next_face_id)->box
6124 == FACE_NO_BOX);
6125 }
6126 }
6127 }
6128 else
6129 {
6130 int face_id = face_after_it_pos (it);
6131 it->end_of_box_run_p
6132 = (face_id != it->face_id
6133 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6134 }
6135 }
6136
6137 /* Value is 0 if end of buffer or string reached. */
6138 return success_p;
6139 }
6140
6141
6142 /* Move IT to the next display element.
6143
6144 RESEAT_P non-zero means if called on a newline in buffer text,
6145 skip to the next visible line start.
6146
6147 Functions get_next_display_element and set_iterator_to_next are
6148 separate because I find this arrangement easier to handle than a
6149 get_next_display_element function that also increments IT's
6150 position. The way it is we can first look at an iterator's current
6151 display element, decide whether it fits on a line, and if it does,
6152 increment the iterator position. The other way around we probably
6153 would either need a flag indicating whether the iterator has to be
6154 incremented the next time, or we would have to implement a
6155 decrement position function which would not be easy to write. */
6156
6157 void
6158 set_iterator_to_next (it, reseat_p)
6159 struct it *it;
6160 int reseat_p;
6161 {
6162 /* Reset flags indicating start and end of a sequence of characters
6163 with box. Reset them at the start of this function because
6164 moving the iterator to a new position might set them. */
6165 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6166
6167 switch (it->method)
6168 {
6169 case GET_FROM_BUFFER:
6170 /* The current display element of IT is a character from
6171 current_buffer. Advance in the buffer, and maybe skip over
6172 invisible lines that are so because of selective display. */
6173 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6174 reseat_at_next_visible_line_start (it, 0);
6175 else if (it->cmp_it.id >= 0)
6176 {
6177 IT_CHARPOS (*it) += it->cmp_it.nchars;
6178 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6179 if (it->cmp_it.to < it->cmp_it.nglyphs)
6180 it->cmp_it.from = it->cmp_it.to;
6181 else
6182 {
6183 it->cmp_it.id = -1;
6184 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6185 IT_BYTEPOS (*it), it->stop_charpos,
6186 Qnil);
6187 }
6188 }
6189 else
6190 {
6191 xassert (it->len != 0);
6192
6193 if (!it->bidi_p)
6194 {
6195 IT_BYTEPOS (*it) += it->len;
6196 IT_CHARPOS (*it) += 1;
6197 }
6198 else
6199 {
6200 /* If this is a new paragraph, determine its base
6201 direction (a.k.a. its base embedding level). */
6202 if (it->bidi_it.new_paragraph)
6203 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6204 bidi_get_next_char_visually (&it->bidi_it);
6205 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6206 IT_CHARPOS (*it) = it->bidi_it.charpos;
6207 }
6208 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6209 }
6210 break;
6211
6212 case GET_FROM_C_STRING:
6213 /* Current display element of IT is from a C string. */
6214 IT_BYTEPOS (*it) += it->len;
6215 IT_CHARPOS (*it) += 1;
6216 break;
6217
6218 case GET_FROM_DISPLAY_VECTOR:
6219 /* Current display element of IT is from a display table entry.
6220 Advance in the display table definition. Reset it to null if
6221 end reached, and continue with characters from buffers/
6222 strings. */
6223 ++it->current.dpvec_index;
6224
6225 /* Restore face of the iterator to what they were before the
6226 display vector entry (these entries may contain faces). */
6227 it->face_id = it->saved_face_id;
6228
6229 if (it->dpvec + it->current.dpvec_index == it->dpend)
6230 {
6231 int recheck_faces = it->ellipsis_p;
6232
6233 if (it->s)
6234 it->method = GET_FROM_C_STRING;
6235 else if (STRINGP (it->string))
6236 it->method = GET_FROM_STRING;
6237 else
6238 {
6239 it->method = GET_FROM_BUFFER;
6240 it->object = it->w->buffer;
6241 }
6242
6243 it->dpvec = NULL;
6244 it->current.dpvec_index = -1;
6245
6246 /* Skip over characters which were displayed via IT->dpvec. */
6247 if (it->dpvec_char_len < 0)
6248 reseat_at_next_visible_line_start (it, 1);
6249 else if (it->dpvec_char_len > 0)
6250 {
6251 if (it->method == GET_FROM_STRING
6252 && it->n_overlay_strings > 0)
6253 it->ignore_overlay_strings_at_pos_p = 1;
6254 it->len = it->dpvec_char_len;
6255 set_iterator_to_next (it, reseat_p);
6256 }
6257
6258 /* Maybe recheck faces after display vector */
6259 if (recheck_faces)
6260 it->stop_charpos = IT_CHARPOS (*it);
6261 }
6262 break;
6263
6264 case GET_FROM_STRING:
6265 /* Current display element is a character from a Lisp string. */
6266 xassert (it->s == NULL && STRINGP (it->string));
6267 if (it->cmp_it.id >= 0)
6268 {
6269 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6270 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6271 if (it->cmp_it.to < it->cmp_it.nglyphs)
6272 it->cmp_it.from = it->cmp_it.to;
6273 else
6274 {
6275 it->cmp_it.id = -1;
6276 composition_compute_stop_pos (&it->cmp_it,
6277 IT_STRING_CHARPOS (*it),
6278 IT_STRING_BYTEPOS (*it),
6279 it->stop_charpos, it->string);
6280 }
6281 }
6282 else
6283 {
6284 IT_STRING_BYTEPOS (*it) += it->len;
6285 IT_STRING_CHARPOS (*it) += 1;
6286 }
6287
6288 consider_string_end:
6289
6290 if (it->current.overlay_string_index >= 0)
6291 {
6292 /* IT->string is an overlay string. Advance to the
6293 next, if there is one. */
6294 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6295 {
6296 it->ellipsis_p = 0;
6297 next_overlay_string (it);
6298 if (it->ellipsis_p)
6299 setup_for_ellipsis (it, 0);
6300 }
6301 }
6302 else
6303 {
6304 /* IT->string is not an overlay string. If we reached
6305 its end, and there is something on IT->stack, proceed
6306 with what is on the stack. This can be either another
6307 string, this time an overlay string, or a buffer. */
6308 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6309 && it->sp > 0)
6310 {
6311 pop_it (it);
6312 if (it->method == GET_FROM_STRING)
6313 goto consider_string_end;
6314 }
6315 }
6316 break;
6317
6318 case GET_FROM_IMAGE:
6319 case GET_FROM_STRETCH:
6320 /* The position etc with which we have to proceed are on
6321 the stack. The position may be at the end of a string,
6322 if the `display' property takes up the whole string. */
6323 xassert (it->sp > 0);
6324 pop_it (it);
6325 if (it->method == GET_FROM_STRING)
6326 goto consider_string_end;
6327 break;
6328
6329 default:
6330 /* There are no other methods defined, so this should be a bug. */
6331 abort ();
6332 }
6333
6334 xassert (it->method != GET_FROM_STRING
6335 || (STRINGP (it->string)
6336 && IT_STRING_CHARPOS (*it) >= 0));
6337 }
6338
6339 /* Load IT's display element fields with information about the next
6340 display element which comes from a display table entry or from the
6341 result of translating a control character to one of the forms `^C'
6342 or `\003'.
6343
6344 IT->dpvec holds the glyphs to return as characters.
6345 IT->saved_face_id holds the face id before the display vector--it
6346 is restored into IT->face_id in set_iterator_to_next. */
6347
6348 static int
6349 next_element_from_display_vector (it)
6350 struct it *it;
6351 {
6352 Lisp_Object gc;
6353
6354 /* Precondition. */
6355 xassert (it->dpvec && it->current.dpvec_index >= 0);
6356
6357 it->face_id = it->saved_face_id;
6358
6359 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6360 That seemed totally bogus - so I changed it... */
6361 gc = it->dpvec[it->current.dpvec_index];
6362
6363 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6364 {
6365 it->c = GLYPH_CODE_CHAR (gc);
6366 it->len = CHAR_BYTES (it->c);
6367
6368 /* The entry may contain a face id to use. Such a face id is
6369 the id of a Lisp face, not a realized face. A face id of
6370 zero means no face is specified. */
6371 if (it->dpvec_face_id >= 0)
6372 it->face_id = it->dpvec_face_id;
6373 else
6374 {
6375 int lface_id = GLYPH_CODE_FACE (gc);
6376 if (lface_id > 0)
6377 it->face_id = merge_faces (it->f, Qt, lface_id,
6378 it->saved_face_id);
6379 }
6380 }
6381 else
6382 /* Display table entry is invalid. Return a space. */
6383 it->c = ' ', it->len = 1;
6384
6385 /* Don't change position and object of the iterator here. They are
6386 still the values of the character that had this display table
6387 entry or was translated, and that's what we want. */
6388 it->what = IT_CHARACTER;
6389 return 1;
6390 }
6391
6392
6393 /* Load IT with the next display element from Lisp string IT->string.
6394 IT->current.string_pos is the current position within the string.
6395 If IT->current.overlay_string_index >= 0, the Lisp string is an
6396 overlay string. */
6397
6398 static int
6399 next_element_from_string (it)
6400 struct it *it;
6401 {
6402 struct text_pos position;
6403
6404 xassert (STRINGP (it->string));
6405 xassert (IT_STRING_CHARPOS (*it) >= 0);
6406 position = it->current.string_pos;
6407
6408 /* Time to check for invisible text? */
6409 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6410 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6411 {
6412 handle_stop (it);
6413
6414 /* Since a handler may have changed IT->method, we must
6415 recurse here. */
6416 return GET_NEXT_DISPLAY_ELEMENT (it);
6417 }
6418
6419 if (it->current.overlay_string_index >= 0)
6420 {
6421 /* Get the next character from an overlay string. In overlay
6422 strings, There is no field width or padding with spaces to
6423 do. */
6424 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6425 {
6426 it->what = IT_EOB;
6427 return 0;
6428 }
6429 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6430 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6431 && next_element_from_composition (it))
6432 {
6433 return 1;
6434 }
6435 else if (STRING_MULTIBYTE (it->string))
6436 {
6437 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6438 const unsigned char *s = (SDATA (it->string)
6439 + IT_STRING_BYTEPOS (*it));
6440 it->c = string_char_and_length (s, &it->len);
6441 }
6442 else
6443 {
6444 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6445 it->len = 1;
6446 }
6447 }
6448 else
6449 {
6450 /* Get the next character from a Lisp string that is not an
6451 overlay string. Such strings come from the mode line, for
6452 example. We may have to pad with spaces, or truncate the
6453 string. See also next_element_from_c_string. */
6454 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6455 {
6456 it->what = IT_EOB;
6457 return 0;
6458 }
6459 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6460 {
6461 /* Pad with spaces. */
6462 it->c = ' ', it->len = 1;
6463 CHARPOS (position) = BYTEPOS (position) = -1;
6464 }
6465 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6466 IT_STRING_BYTEPOS (*it), it->string_nchars)
6467 && next_element_from_composition (it))
6468 {
6469 return 1;
6470 }
6471 else if (STRING_MULTIBYTE (it->string))
6472 {
6473 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6474 const unsigned char *s = (SDATA (it->string)
6475 + IT_STRING_BYTEPOS (*it));
6476 it->c = string_char_and_length (s, &it->len);
6477 }
6478 else
6479 {
6480 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6481 it->len = 1;
6482 }
6483 }
6484
6485 /* Record what we have and where it came from. */
6486 it->what = IT_CHARACTER;
6487 it->object = it->string;
6488 it->position = position;
6489 return 1;
6490 }
6491
6492
6493 /* Load IT with next display element from C string IT->s.
6494 IT->string_nchars is the maximum number of characters to return
6495 from the string. IT->end_charpos may be greater than
6496 IT->string_nchars when this function is called, in which case we
6497 may have to return padding spaces. Value is zero if end of string
6498 reached, including padding spaces. */
6499
6500 static int
6501 next_element_from_c_string (it)
6502 struct it *it;
6503 {
6504 int success_p = 1;
6505
6506 xassert (it->s);
6507 it->what = IT_CHARACTER;
6508 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6509 it->object = Qnil;
6510
6511 /* IT's position can be greater IT->string_nchars in case a field
6512 width or precision has been specified when the iterator was
6513 initialized. */
6514 if (IT_CHARPOS (*it) >= it->end_charpos)
6515 {
6516 /* End of the game. */
6517 it->what = IT_EOB;
6518 success_p = 0;
6519 }
6520 else if (IT_CHARPOS (*it) >= it->string_nchars)
6521 {
6522 /* Pad with spaces. */
6523 it->c = ' ', it->len = 1;
6524 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6525 }
6526 else if (it->multibyte_p)
6527 {
6528 /* Implementation note: The calls to strlen apparently aren't a
6529 performance problem because there is no noticeable performance
6530 difference between Emacs running in unibyte or multibyte mode. */
6531 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6532 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6533 }
6534 else
6535 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6536
6537 return success_p;
6538 }
6539
6540
6541 /* Set up IT to return characters from an ellipsis, if appropriate.
6542 The definition of the ellipsis glyphs may come from a display table
6543 entry. This function fills IT with the first glyph from the
6544 ellipsis if an ellipsis is to be displayed. */
6545
6546 static int
6547 next_element_from_ellipsis (it)
6548 struct it *it;
6549 {
6550 if (it->selective_display_ellipsis_p)
6551 setup_for_ellipsis (it, it->len);
6552 else
6553 {
6554 /* The face at the current position may be different from the
6555 face we find after the invisible text. Remember what it
6556 was in IT->saved_face_id, and signal that it's there by
6557 setting face_before_selective_p. */
6558 it->saved_face_id = it->face_id;
6559 it->method = GET_FROM_BUFFER;
6560 it->object = it->w->buffer;
6561 reseat_at_next_visible_line_start (it, 1);
6562 it->face_before_selective_p = 1;
6563 }
6564
6565 return GET_NEXT_DISPLAY_ELEMENT (it);
6566 }
6567
6568
6569 /* Deliver an image display element. The iterator IT is already
6570 filled with image information (done in handle_display_prop). Value
6571 is always 1. */
6572
6573
6574 static int
6575 next_element_from_image (it)
6576 struct it *it;
6577 {
6578 it->what = IT_IMAGE;
6579 return 1;
6580 }
6581
6582
6583 /* Fill iterator IT with next display element from a stretch glyph
6584 property. IT->object is the value of the text property. Value is
6585 always 1. */
6586
6587 static int
6588 next_element_from_stretch (it)
6589 struct it *it;
6590 {
6591 it->what = IT_STRETCH;
6592 return 1;
6593 }
6594
6595 /* Scan forward from CHARPOS in the current buffer, until we find a
6596 stop position > current IT's position. Then handle the stop
6597 position before that. This is called when we bump into a stop
6598 position while reordering bidirectional text. */
6599
6600 static void
6601 handle_stop_backwards (it, charpos)
6602 struct it *it;
6603 EMACS_INT charpos;
6604 {
6605 EMACS_INT where_we_are = IT_CHARPOS (*it);
6606 struct display_pos save_current = it->current;
6607 struct text_pos save_position = it->position;
6608 struct text_pos pos1;
6609 EMACS_INT next_stop;
6610
6611 /* Scan in strict logical order. */
6612 it->bidi_p = 0;
6613 do
6614 {
6615 it->prev_stop = charpos;
6616 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6617 reseat_1 (it, pos1, 0);
6618 compute_stop_pos (it);
6619 /* We must advance forward, right? */
6620 if (it->stop_charpos <= it->prev_stop)
6621 abort ();
6622 charpos = it->stop_charpos;
6623 }
6624 while (charpos <= where_we_are);
6625
6626 next_stop = it->stop_charpos;
6627 it->stop_charpos = it->prev_stop;
6628 it->bidi_p = 1;
6629 it->current = save_current;
6630 it->position = save_position;
6631 handle_stop (it);
6632 it->stop_charpos = next_stop;
6633 }
6634
6635 /* Load IT with the next display element from current_buffer. Value
6636 is zero if end of buffer reached. IT->stop_charpos is the next
6637 position at which to stop and check for text properties or buffer
6638 end. */
6639
6640 static int
6641 next_element_from_buffer (it)
6642 struct it *it;
6643 {
6644 int success_p = 1;
6645
6646 xassert (IT_CHARPOS (*it) >= BEGV);
6647
6648 /* With bidi reordering, the character to display might not be the
6649 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6650 we were reseat()ed to a new buffer position, which is potentially
6651 a different paragraph. */
6652 if (it->bidi_p && it->bidi_it.first_elt)
6653 {
6654 it->bidi_it.charpos = IT_CHARPOS (*it);
6655 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6656 /* If we are at the beginning of a line, we can produce the next
6657 element right away. */
6658 if (it->bidi_it.bytepos == BEGV_BYTE
6659 /* FIXME: Should support all Unicode line separators. */
6660 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6661 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6662 {
6663 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6664 /* If the paragraph base direction is R2L, its glyphs should
6665 be reversed. */
6666 if (it->glyph_row && (it->bidi_it.level_stack[0].level & 1) != 0)
6667 it->glyph_row->reversed_p = 1;
6668 bidi_get_next_char_visually (&it->bidi_it);
6669 }
6670 else
6671 {
6672 int orig_bytepos = IT_BYTEPOS (*it);
6673
6674 /* We need to prime the bidi iterator starting at the line's
6675 beginning, before we will be able to produce the next
6676 element. */
6677 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6678 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6679 it->bidi_it.charpos = IT_CHARPOS (*it);
6680 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6681 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6682 if (it->glyph_row && (it->bidi_it.level_stack[0].level & 1) != 0)
6683 it->glyph_row->reversed_p = 1;
6684 do
6685 {
6686 /* Now return to buffer position where we were asked to
6687 get the next display element, and produce that. */
6688 bidi_get_next_char_visually (&it->bidi_it);
6689 }
6690 while (it->bidi_it.bytepos != orig_bytepos
6691 && it->bidi_it.bytepos < ZV_BYTE);
6692 }
6693
6694 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6695 /* Adjust IT's position information to where we ended up. */
6696 IT_CHARPOS (*it) = it->bidi_it.charpos;
6697 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6698 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6699 }
6700
6701 if (IT_CHARPOS (*it) >= it->stop_charpos)
6702 {
6703 if (IT_CHARPOS (*it) >= it->end_charpos)
6704 {
6705 int overlay_strings_follow_p;
6706
6707 /* End of the game, except when overlay strings follow that
6708 haven't been returned yet. */
6709 if (it->overlay_strings_at_end_processed_p)
6710 overlay_strings_follow_p = 0;
6711 else
6712 {
6713 it->overlay_strings_at_end_processed_p = 1;
6714 overlay_strings_follow_p = get_overlay_strings (it, 0);
6715 }
6716
6717 if (overlay_strings_follow_p)
6718 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6719 else
6720 {
6721 it->what = IT_EOB;
6722 it->position = it->current.pos;
6723 success_p = 0;
6724 }
6725 }
6726 else if (!(!it->bidi_p
6727 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6728 || IT_CHARPOS (*it) == it->stop_charpos))
6729 {
6730 /* With bidi non-linear iteration, we could find ourselves
6731 far beyond the last computed stop_charpos, with several
6732 other stop positions in between that we missed. Scan
6733 them all now, in buffer's logical order, until we find
6734 and handle the last stop_charpos that precedes our
6735 current position. */
6736 handle_stop_backwards (it, it->stop_charpos);
6737 return GET_NEXT_DISPLAY_ELEMENT (it);
6738 }
6739 else
6740 {
6741 if (it->bidi_p)
6742 {
6743 /* Take note of the stop position we just moved across,
6744 for when we will move back across it. */
6745 it->prev_stop = it->stop_charpos;
6746 /* If we are at base paragraph embedding level, take
6747 note of the last stop position seen at this
6748 level. */
6749 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6750 it->base_level_stop = it->stop_charpos;
6751 }
6752 handle_stop (it);
6753 return GET_NEXT_DISPLAY_ELEMENT (it);
6754 }
6755 }
6756 else if (it->bidi_p
6757 /* We can sometimes back up for reasons that have nothing
6758 to do with bidi reordering. E.g., compositions. The
6759 code below is only needed when we are above the base
6760 embedding level, so test for that explicitly. */
6761 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6762 && IT_CHARPOS (*it) < it->prev_stop)
6763 {
6764 if (it->base_level_stop <= 0)
6765 it->base_level_stop = BEGV;
6766 if (IT_CHARPOS (*it) < it->base_level_stop)
6767 abort ();
6768 handle_stop_backwards (it, it->base_level_stop);
6769 return GET_NEXT_DISPLAY_ELEMENT (it);
6770 }
6771 else
6772 {
6773 /* No face changes, overlays etc. in sight, so just return a
6774 character from current_buffer. */
6775 unsigned char *p;
6776
6777 /* Maybe run the redisplay end trigger hook. Performance note:
6778 This doesn't seem to cost measurable time. */
6779 if (it->redisplay_end_trigger_charpos
6780 && it->glyph_row
6781 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6782 run_redisplay_end_trigger_hook (it);
6783
6784 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6785 it->end_charpos)
6786 && next_element_from_composition (it))
6787 {
6788 return 1;
6789 }
6790
6791 /* Get the next character, maybe multibyte. */
6792 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6793 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6794 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6795 else
6796 it->c = *p, it->len = 1;
6797
6798 /* Record what we have and where it came from. */
6799 it->what = IT_CHARACTER;
6800 it->object = it->w->buffer;
6801 it->position = it->current.pos;
6802
6803 /* Normally we return the character found above, except when we
6804 really want to return an ellipsis for selective display. */
6805 if (it->selective)
6806 {
6807 if (it->c == '\n')
6808 {
6809 /* A value of selective > 0 means hide lines indented more
6810 than that number of columns. */
6811 if (it->selective > 0
6812 && IT_CHARPOS (*it) + 1 < ZV
6813 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6814 IT_BYTEPOS (*it) + 1,
6815 (double) it->selective)) /* iftc */
6816 {
6817 success_p = next_element_from_ellipsis (it);
6818 it->dpvec_char_len = -1;
6819 }
6820 }
6821 else if (it->c == '\r' && it->selective == -1)
6822 {
6823 /* A value of selective == -1 means that everything from the
6824 CR to the end of the line is invisible, with maybe an
6825 ellipsis displayed for it. */
6826 success_p = next_element_from_ellipsis (it);
6827 it->dpvec_char_len = -1;
6828 }
6829 }
6830 }
6831
6832 /* Value is zero if end of buffer reached. */
6833 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6834 return success_p;
6835 }
6836
6837
6838 /* Run the redisplay end trigger hook for IT. */
6839
6840 static void
6841 run_redisplay_end_trigger_hook (it)
6842 struct it *it;
6843 {
6844 Lisp_Object args[3];
6845
6846 /* IT->glyph_row should be non-null, i.e. we should be actually
6847 displaying something, or otherwise we should not run the hook. */
6848 xassert (it->glyph_row);
6849
6850 /* Set up hook arguments. */
6851 args[0] = Qredisplay_end_trigger_functions;
6852 args[1] = it->window;
6853 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6854 it->redisplay_end_trigger_charpos = 0;
6855
6856 /* Since we are *trying* to run these functions, don't try to run
6857 them again, even if they get an error. */
6858 it->w->redisplay_end_trigger = Qnil;
6859 Frun_hook_with_args (3, args);
6860
6861 /* Notice if it changed the face of the character we are on. */
6862 handle_face_prop (it);
6863 }
6864
6865
6866 /* Deliver a composition display element. Unlike the other
6867 next_element_from_XXX, this function is not registered in the array
6868 get_next_element[]. It is called from next_element_from_buffer and
6869 next_element_from_string when necessary. */
6870
6871 static int
6872 next_element_from_composition (it)
6873 struct it *it;
6874 {
6875 it->what = IT_COMPOSITION;
6876 it->len = it->cmp_it.nbytes;
6877 if (STRINGP (it->string))
6878 {
6879 if (it->c < 0)
6880 {
6881 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6882 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6883 return 0;
6884 }
6885 it->position = it->current.string_pos;
6886 it->object = it->string;
6887 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6888 IT_STRING_BYTEPOS (*it), it->string);
6889 }
6890 else
6891 {
6892 if (it->c < 0)
6893 {
6894 IT_CHARPOS (*it) += it->cmp_it.nchars;
6895 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6896 return 0;
6897 }
6898 it->position = it->current.pos;
6899 it->object = it->w->buffer;
6900 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6901 IT_BYTEPOS (*it), Qnil);
6902 }
6903 return 1;
6904 }
6905
6906
6907 \f
6908 /***********************************************************************
6909 Moving an iterator without producing glyphs
6910 ***********************************************************************/
6911
6912 /* Check if iterator is at a position corresponding to a valid buffer
6913 position after some move_it_ call. */
6914
6915 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6916 ((it)->method == GET_FROM_STRING \
6917 ? IT_STRING_CHARPOS (*it) == 0 \
6918 : 1)
6919
6920
6921 /* Move iterator IT to a specified buffer or X position within one
6922 line on the display without producing glyphs.
6923
6924 OP should be a bit mask including some or all of these bits:
6925 MOVE_TO_X: Stop upon reaching x-position TO_X.
6926 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6927 Regardless of OP's value, stop upon reaching the end of the display line.
6928
6929 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6930 This means, in particular, that TO_X includes window's horizontal
6931 scroll amount.
6932
6933 The return value has several possible values that
6934 say what condition caused the scan to stop:
6935
6936 MOVE_POS_MATCH_OR_ZV
6937 - when TO_POS or ZV was reached.
6938
6939 MOVE_X_REACHED
6940 -when TO_X was reached before TO_POS or ZV were reached.
6941
6942 MOVE_LINE_CONTINUED
6943 - when we reached the end of the display area and the line must
6944 be continued.
6945
6946 MOVE_LINE_TRUNCATED
6947 - when we reached the end of the display area and the line is
6948 truncated.
6949
6950 MOVE_NEWLINE_OR_CR
6951 - when we stopped at a line end, i.e. a newline or a CR and selective
6952 display is on. */
6953
6954 static enum move_it_result
6955 move_it_in_display_line_to (struct it *it,
6956 EMACS_INT to_charpos, int to_x,
6957 enum move_operation_enum op)
6958 {
6959 enum move_it_result result = MOVE_UNDEFINED;
6960 struct glyph_row *saved_glyph_row;
6961 struct it wrap_it, atpos_it, atx_it;
6962 int may_wrap = 0;
6963
6964 /* Don't produce glyphs in produce_glyphs. */
6965 saved_glyph_row = it->glyph_row;
6966 it->glyph_row = NULL;
6967
6968 /* Use wrap_it to save a copy of IT wherever a word wrap could
6969 occur. Use atpos_it to save a copy of IT at the desired buffer
6970 position, if found, so that we can scan ahead and check if the
6971 word later overshoots the window edge. Use atx_it similarly, for
6972 pixel positions. */
6973 wrap_it.sp = -1;
6974 atpos_it.sp = -1;
6975 atx_it.sp = -1;
6976
6977 #define BUFFER_POS_REACHED_P() \
6978 ((op & MOVE_TO_POS) != 0 \
6979 && BUFFERP (it->object) \
6980 && IT_CHARPOS (*it) >= to_charpos \
6981 && (it->method == GET_FROM_BUFFER \
6982 || (it->method == GET_FROM_DISPLAY_VECTOR \
6983 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6984
6985 /* If there's a line-/wrap-prefix, handle it. */
6986 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6987 && it->current_y < it->last_visible_y)
6988 handle_line_prefix (it);
6989
6990 while (1)
6991 {
6992 int x, i, ascent = 0, descent = 0;
6993
6994 /* Utility macro to reset an iterator with x, ascent, and descent. */
6995 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6996 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6997 (IT)->max_descent = descent)
6998
6999 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7000 glyph). */
7001 if ((op & MOVE_TO_POS) != 0
7002 && BUFFERP (it->object)
7003 && it->method == GET_FROM_BUFFER
7004 && IT_CHARPOS (*it) > to_charpos)
7005 {
7006 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7007 {
7008 result = MOVE_POS_MATCH_OR_ZV;
7009 break;
7010 }
7011 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7012 /* If wrap_it is valid, the current position might be in a
7013 word that is wrapped. So, save the iterator in
7014 atpos_it and continue to see if wrapping happens. */
7015 atpos_it = *it;
7016 }
7017
7018 /* Stop when ZV reached.
7019 We used to stop here when TO_CHARPOS reached as well, but that is
7020 too soon if this glyph does not fit on this line. So we handle it
7021 explicitly below. */
7022 if (!get_next_display_element (it))
7023 {
7024 result = MOVE_POS_MATCH_OR_ZV;
7025 break;
7026 }
7027
7028 if (it->line_wrap == TRUNCATE)
7029 {
7030 if (BUFFER_POS_REACHED_P ())
7031 {
7032 result = MOVE_POS_MATCH_OR_ZV;
7033 break;
7034 }
7035 }
7036 else
7037 {
7038 if (it->line_wrap == WORD_WRAP)
7039 {
7040 if (IT_DISPLAYING_WHITESPACE (it))
7041 may_wrap = 1;
7042 else if (may_wrap)
7043 {
7044 /* We have reached a glyph that follows one or more
7045 whitespace characters. If the position is
7046 already found, we are done. */
7047 if (atpos_it.sp >= 0)
7048 {
7049 *it = atpos_it;
7050 result = MOVE_POS_MATCH_OR_ZV;
7051 goto done;
7052 }
7053 if (atx_it.sp >= 0)
7054 {
7055 *it = atx_it;
7056 result = MOVE_X_REACHED;
7057 goto done;
7058 }
7059 /* Otherwise, we can wrap here. */
7060 wrap_it = *it;
7061 may_wrap = 0;
7062 }
7063 }
7064 }
7065
7066 /* Remember the line height for the current line, in case
7067 the next element doesn't fit on the line. */
7068 ascent = it->max_ascent;
7069 descent = it->max_descent;
7070
7071 /* The call to produce_glyphs will get the metrics of the
7072 display element IT is loaded with. Record the x-position
7073 before this display element, in case it doesn't fit on the
7074 line. */
7075 x = it->current_x;
7076
7077 PRODUCE_GLYPHS (it);
7078
7079 if (it->area != TEXT_AREA)
7080 {
7081 set_iterator_to_next (it, 1);
7082 continue;
7083 }
7084
7085 /* The number of glyphs we get back in IT->nglyphs will normally
7086 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7087 character on a terminal frame, or (iii) a line end. For the
7088 second case, IT->nglyphs - 1 padding glyphs will be present.
7089 (On X frames, there is only one glyph produced for a
7090 composite character.)
7091
7092 The behavior implemented below means, for continuation lines,
7093 that as many spaces of a TAB as fit on the current line are
7094 displayed there. For terminal frames, as many glyphs of a
7095 multi-glyph character are displayed in the current line, too.
7096 This is what the old redisplay code did, and we keep it that
7097 way. Under X, the whole shape of a complex character must
7098 fit on the line or it will be completely displayed in the
7099 next line.
7100
7101 Note that both for tabs and padding glyphs, all glyphs have
7102 the same width. */
7103 if (it->nglyphs)
7104 {
7105 /* More than one glyph or glyph doesn't fit on line. All
7106 glyphs have the same width. */
7107 int single_glyph_width = it->pixel_width / it->nglyphs;
7108 int new_x;
7109 int x_before_this_char = x;
7110 int hpos_before_this_char = it->hpos;
7111
7112 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7113 {
7114 new_x = x + single_glyph_width;
7115
7116 /* We want to leave anything reaching TO_X to the caller. */
7117 if ((op & MOVE_TO_X) && new_x > to_x)
7118 {
7119 if (BUFFER_POS_REACHED_P ())
7120 {
7121 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7122 goto buffer_pos_reached;
7123 if (atpos_it.sp < 0)
7124 {
7125 atpos_it = *it;
7126 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7127 }
7128 }
7129 else
7130 {
7131 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7132 {
7133 it->current_x = x;
7134 result = MOVE_X_REACHED;
7135 break;
7136 }
7137 if (atx_it.sp < 0)
7138 {
7139 atx_it = *it;
7140 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7141 }
7142 }
7143 }
7144
7145 if (/* Lines are continued. */
7146 it->line_wrap != TRUNCATE
7147 && (/* And glyph doesn't fit on the line. */
7148 new_x > it->last_visible_x
7149 /* Or it fits exactly and we're on a window
7150 system frame. */
7151 || (new_x == it->last_visible_x
7152 && FRAME_WINDOW_P (it->f))))
7153 {
7154 if (/* IT->hpos == 0 means the very first glyph
7155 doesn't fit on the line, e.g. a wide image. */
7156 it->hpos == 0
7157 || (new_x == it->last_visible_x
7158 && FRAME_WINDOW_P (it->f)))
7159 {
7160 ++it->hpos;
7161 it->current_x = new_x;
7162
7163 /* The character's last glyph just barely fits
7164 in this row. */
7165 if (i == it->nglyphs - 1)
7166 {
7167 /* If this is the destination position,
7168 return a position *before* it in this row,
7169 now that we know it fits in this row. */
7170 if (BUFFER_POS_REACHED_P ())
7171 {
7172 if (it->line_wrap != WORD_WRAP
7173 || wrap_it.sp < 0)
7174 {
7175 it->hpos = hpos_before_this_char;
7176 it->current_x = x_before_this_char;
7177 result = MOVE_POS_MATCH_OR_ZV;
7178 break;
7179 }
7180 if (it->line_wrap == WORD_WRAP
7181 && atpos_it.sp < 0)
7182 {
7183 atpos_it = *it;
7184 atpos_it.current_x = x_before_this_char;
7185 atpos_it.hpos = hpos_before_this_char;
7186 }
7187 }
7188
7189 set_iterator_to_next (it, 1);
7190 /* On graphical terminals, newlines may
7191 "overflow" into the fringe if
7192 overflow-newline-into-fringe is non-nil.
7193 On text-only terminals, newlines may
7194 overflow into the last glyph on the
7195 display line.*/
7196 if (!FRAME_WINDOW_P (it->f)
7197 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7198 {
7199 if (!get_next_display_element (it))
7200 {
7201 result = MOVE_POS_MATCH_OR_ZV;
7202 break;
7203 }
7204 if (BUFFER_POS_REACHED_P ())
7205 {
7206 if (ITERATOR_AT_END_OF_LINE_P (it))
7207 result = MOVE_POS_MATCH_OR_ZV;
7208 else
7209 result = MOVE_LINE_CONTINUED;
7210 break;
7211 }
7212 if (ITERATOR_AT_END_OF_LINE_P (it))
7213 {
7214 result = MOVE_NEWLINE_OR_CR;
7215 break;
7216 }
7217 }
7218 }
7219 }
7220 else
7221 IT_RESET_X_ASCENT_DESCENT (it);
7222
7223 if (wrap_it.sp >= 0)
7224 {
7225 *it = wrap_it;
7226 atpos_it.sp = -1;
7227 atx_it.sp = -1;
7228 }
7229
7230 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7231 IT_CHARPOS (*it)));
7232 result = MOVE_LINE_CONTINUED;
7233 break;
7234 }
7235
7236 if (BUFFER_POS_REACHED_P ())
7237 {
7238 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7239 goto buffer_pos_reached;
7240 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7241 {
7242 atpos_it = *it;
7243 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7244 }
7245 }
7246
7247 if (new_x > it->first_visible_x)
7248 {
7249 /* Glyph is visible. Increment number of glyphs that
7250 would be displayed. */
7251 ++it->hpos;
7252 }
7253 }
7254
7255 if (result != MOVE_UNDEFINED)
7256 break;
7257 }
7258 else if (BUFFER_POS_REACHED_P ())
7259 {
7260 buffer_pos_reached:
7261 IT_RESET_X_ASCENT_DESCENT (it);
7262 result = MOVE_POS_MATCH_OR_ZV;
7263 break;
7264 }
7265 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7266 {
7267 /* Stop when TO_X specified and reached. This check is
7268 necessary here because of lines consisting of a line end,
7269 only. The line end will not produce any glyphs and we
7270 would never get MOVE_X_REACHED. */
7271 xassert (it->nglyphs == 0);
7272 result = MOVE_X_REACHED;
7273 break;
7274 }
7275
7276 /* Is this a line end? If yes, we're done. */
7277 if (ITERATOR_AT_END_OF_LINE_P (it))
7278 {
7279 result = MOVE_NEWLINE_OR_CR;
7280 break;
7281 }
7282
7283 /* The current display element has been consumed. Advance
7284 to the next. */
7285 set_iterator_to_next (it, 1);
7286
7287 /* Stop if lines are truncated and IT's current x-position is
7288 past the right edge of the window now. */
7289 if (it->line_wrap == TRUNCATE
7290 && it->current_x >= it->last_visible_x)
7291 {
7292 if (!FRAME_WINDOW_P (it->f)
7293 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7294 {
7295 if (!get_next_display_element (it)
7296 || BUFFER_POS_REACHED_P ())
7297 {
7298 result = MOVE_POS_MATCH_OR_ZV;
7299 break;
7300 }
7301 if (ITERATOR_AT_END_OF_LINE_P (it))
7302 {
7303 result = MOVE_NEWLINE_OR_CR;
7304 break;
7305 }
7306 }
7307 result = MOVE_LINE_TRUNCATED;
7308 break;
7309 }
7310 #undef IT_RESET_X_ASCENT_DESCENT
7311 }
7312
7313 #undef BUFFER_POS_REACHED_P
7314
7315 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7316 restore the saved iterator. */
7317 if (atpos_it.sp >= 0)
7318 *it = atpos_it;
7319 else if (atx_it.sp >= 0)
7320 *it = atx_it;
7321
7322 done:
7323
7324 /* Restore the iterator settings altered at the beginning of this
7325 function. */
7326 it->glyph_row = saved_glyph_row;
7327 return result;
7328 }
7329
7330 /* For external use. */
7331 void
7332 move_it_in_display_line (struct it *it,
7333 EMACS_INT to_charpos, int to_x,
7334 enum move_operation_enum op)
7335 {
7336 if (it->line_wrap == WORD_WRAP
7337 && (op & MOVE_TO_X))
7338 {
7339 struct it save_it = *it;
7340 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7341 /* When word-wrap is on, TO_X may lie past the end
7342 of a wrapped line. Then it->current is the
7343 character on the next line, so backtrack to the
7344 space before the wrap point. */
7345 if (skip == MOVE_LINE_CONTINUED)
7346 {
7347 int prev_x = max (it->current_x - 1, 0);
7348 *it = save_it;
7349 move_it_in_display_line_to
7350 (it, -1, prev_x, MOVE_TO_X);
7351 }
7352 }
7353 else
7354 move_it_in_display_line_to (it, to_charpos, to_x, op);
7355 }
7356
7357
7358 /* Move IT forward until it satisfies one or more of the criteria in
7359 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7360
7361 OP is a bit-mask that specifies where to stop, and in particular,
7362 which of those four position arguments makes a difference. See the
7363 description of enum move_operation_enum.
7364
7365 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7366 screen line, this function will set IT to the next position >
7367 TO_CHARPOS. */
7368
7369 void
7370 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7371 struct it *it;
7372 int to_charpos, to_x, to_y, to_vpos;
7373 int op;
7374 {
7375 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7376 int line_height, line_start_x = 0, reached = 0;
7377
7378 for (;;)
7379 {
7380 if (op & MOVE_TO_VPOS)
7381 {
7382 /* If no TO_CHARPOS and no TO_X specified, stop at the
7383 start of the line TO_VPOS. */
7384 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7385 {
7386 if (it->vpos == to_vpos)
7387 {
7388 reached = 1;
7389 break;
7390 }
7391 else
7392 skip = move_it_in_display_line_to (it, -1, -1, 0);
7393 }
7394 else
7395 {
7396 /* TO_VPOS >= 0 means stop at TO_X in the line at
7397 TO_VPOS, or at TO_POS, whichever comes first. */
7398 if (it->vpos == to_vpos)
7399 {
7400 reached = 2;
7401 break;
7402 }
7403
7404 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7405
7406 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7407 {
7408 reached = 3;
7409 break;
7410 }
7411 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7412 {
7413 /* We have reached TO_X but not in the line we want. */
7414 skip = move_it_in_display_line_to (it, to_charpos,
7415 -1, MOVE_TO_POS);
7416 if (skip == MOVE_POS_MATCH_OR_ZV)
7417 {
7418 reached = 4;
7419 break;
7420 }
7421 }
7422 }
7423 }
7424 else if (op & MOVE_TO_Y)
7425 {
7426 struct it it_backup;
7427
7428 if (it->line_wrap == WORD_WRAP)
7429 it_backup = *it;
7430
7431 /* TO_Y specified means stop at TO_X in the line containing
7432 TO_Y---or at TO_CHARPOS if this is reached first. The
7433 problem is that we can't really tell whether the line
7434 contains TO_Y before we have completely scanned it, and
7435 this may skip past TO_X. What we do is to first scan to
7436 TO_X.
7437
7438 If TO_X is not specified, use a TO_X of zero. The reason
7439 is to make the outcome of this function more predictable.
7440 If we didn't use TO_X == 0, we would stop at the end of
7441 the line which is probably not what a caller would expect
7442 to happen. */
7443 skip = move_it_in_display_line_to
7444 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7445 (MOVE_TO_X | (op & MOVE_TO_POS)));
7446
7447 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7448 if (skip == MOVE_POS_MATCH_OR_ZV)
7449 reached = 5;
7450 else if (skip == MOVE_X_REACHED)
7451 {
7452 /* If TO_X was reached, we want to know whether TO_Y is
7453 in the line. We know this is the case if the already
7454 scanned glyphs make the line tall enough. Otherwise,
7455 we must check by scanning the rest of the line. */
7456 line_height = it->max_ascent + it->max_descent;
7457 if (to_y >= it->current_y
7458 && to_y < it->current_y + line_height)
7459 {
7460 reached = 6;
7461 break;
7462 }
7463 it_backup = *it;
7464 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7465 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7466 op & MOVE_TO_POS);
7467 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7468 line_height = it->max_ascent + it->max_descent;
7469 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7470
7471 if (to_y >= it->current_y
7472 && to_y < it->current_y + line_height)
7473 {
7474 /* If TO_Y is in this line and TO_X was reached
7475 above, we scanned too far. We have to restore
7476 IT's settings to the ones before skipping. */
7477 *it = it_backup;
7478 reached = 6;
7479 }
7480 else
7481 {
7482 skip = skip2;
7483 if (skip == MOVE_POS_MATCH_OR_ZV)
7484 reached = 7;
7485 }
7486 }
7487 else
7488 {
7489 /* Check whether TO_Y is in this line. */
7490 line_height = it->max_ascent + it->max_descent;
7491 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7492
7493 if (to_y >= it->current_y
7494 && to_y < it->current_y + line_height)
7495 {
7496 /* When word-wrap is on, TO_X may lie past the end
7497 of a wrapped line. Then it->current is the
7498 character on the next line, so backtrack to the
7499 space before the wrap point. */
7500 if (skip == MOVE_LINE_CONTINUED
7501 && it->line_wrap == WORD_WRAP)
7502 {
7503 int prev_x = max (it->current_x - 1, 0);
7504 *it = it_backup;
7505 skip = move_it_in_display_line_to
7506 (it, -1, prev_x, MOVE_TO_X);
7507 }
7508 reached = 6;
7509 }
7510 }
7511
7512 if (reached)
7513 break;
7514 }
7515 else if (BUFFERP (it->object)
7516 && (it->method == GET_FROM_BUFFER
7517 || it->method == GET_FROM_STRETCH)
7518 && IT_CHARPOS (*it) >= to_charpos)
7519 skip = MOVE_POS_MATCH_OR_ZV;
7520 else
7521 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7522
7523 switch (skip)
7524 {
7525 case MOVE_POS_MATCH_OR_ZV:
7526 reached = 8;
7527 goto out;
7528
7529 case MOVE_NEWLINE_OR_CR:
7530 set_iterator_to_next (it, 1);
7531 it->continuation_lines_width = 0;
7532 break;
7533
7534 case MOVE_LINE_TRUNCATED:
7535 it->continuation_lines_width = 0;
7536 reseat_at_next_visible_line_start (it, 0);
7537 if ((op & MOVE_TO_POS) != 0
7538 && IT_CHARPOS (*it) > to_charpos)
7539 {
7540 reached = 9;
7541 goto out;
7542 }
7543 break;
7544
7545 case MOVE_LINE_CONTINUED:
7546 /* For continued lines ending in a tab, some of the glyphs
7547 associated with the tab are displayed on the current
7548 line. Since it->current_x does not include these glyphs,
7549 we use it->last_visible_x instead. */
7550 if (it->c == '\t')
7551 {
7552 it->continuation_lines_width += it->last_visible_x;
7553 /* When moving by vpos, ensure that the iterator really
7554 advances to the next line (bug#847, bug#969). Fixme:
7555 do we need to do this in other circumstances? */
7556 if (it->current_x != it->last_visible_x
7557 && (op & MOVE_TO_VPOS)
7558 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7559 {
7560 line_start_x = it->current_x + it->pixel_width
7561 - it->last_visible_x;
7562 set_iterator_to_next (it, 0);
7563 }
7564 }
7565 else
7566 it->continuation_lines_width += it->current_x;
7567 break;
7568
7569 default:
7570 abort ();
7571 }
7572
7573 /* Reset/increment for the next run. */
7574 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7575 it->current_x = line_start_x;
7576 line_start_x = 0;
7577 it->hpos = 0;
7578 it->current_y += it->max_ascent + it->max_descent;
7579 ++it->vpos;
7580 last_height = it->max_ascent + it->max_descent;
7581 last_max_ascent = it->max_ascent;
7582 it->max_ascent = it->max_descent = 0;
7583 }
7584
7585 out:
7586
7587 /* On text terminals, we may stop at the end of a line in the middle
7588 of a multi-character glyph. If the glyph itself is continued,
7589 i.e. it is actually displayed on the next line, don't treat this
7590 stopping point as valid; move to the next line instead (unless
7591 that brings us offscreen). */
7592 if (!FRAME_WINDOW_P (it->f)
7593 && op & MOVE_TO_POS
7594 && IT_CHARPOS (*it) == to_charpos
7595 && it->what == IT_CHARACTER
7596 && it->nglyphs > 1
7597 && it->line_wrap == WINDOW_WRAP
7598 && it->current_x == it->last_visible_x - 1
7599 && it->c != '\n'
7600 && it->c != '\t'
7601 && it->vpos < XFASTINT (it->w->window_end_vpos))
7602 {
7603 it->continuation_lines_width += it->current_x;
7604 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7605 it->current_y += it->max_ascent + it->max_descent;
7606 ++it->vpos;
7607 last_height = it->max_ascent + it->max_descent;
7608 last_max_ascent = it->max_ascent;
7609 }
7610
7611 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7612 }
7613
7614
7615 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7616
7617 If DY > 0, move IT backward at least that many pixels. DY = 0
7618 means move IT backward to the preceding line start or BEGV. This
7619 function may move over more than DY pixels if IT->current_y - DY
7620 ends up in the middle of a line; in this case IT->current_y will be
7621 set to the top of the line moved to. */
7622
7623 void
7624 move_it_vertically_backward (it, dy)
7625 struct it *it;
7626 int dy;
7627 {
7628 int nlines, h;
7629 struct it it2, it3;
7630 int start_pos;
7631
7632 move_further_back:
7633 xassert (dy >= 0);
7634
7635 start_pos = IT_CHARPOS (*it);
7636
7637 /* Estimate how many newlines we must move back. */
7638 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7639
7640 /* Set the iterator's position that many lines back. */
7641 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7642 back_to_previous_visible_line_start (it);
7643
7644 /* Reseat the iterator here. When moving backward, we don't want
7645 reseat to skip forward over invisible text, set up the iterator
7646 to deliver from overlay strings at the new position etc. So,
7647 use reseat_1 here. */
7648 reseat_1 (it, it->current.pos, 1);
7649
7650 /* We are now surely at a line start. */
7651 it->current_x = it->hpos = 0;
7652 it->continuation_lines_width = 0;
7653
7654 /* Move forward and see what y-distance we moved. First move to the
7655 start of the next line so that we get its height. We need this
7656 height to be able to tell whether we reached the specified
7657 y-distance. */
7658 it2 = *it;
7659 it2.max_ascent = it2.max_descent = 0;
7660 do
7661 {
7662 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7663 MOVE_TO_POS | MOVE_TO_VPOS);
7664 }
7665 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7666 xassert (IT_CHARPOS (*it) >= BEGV);
7667 it3 = it2;
7668
7669 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7670 xassert (IT_CHARPOS (*it) >= BEGV);
7671 /* H is the actual vertical distance from the position in *IT
7672 and the starting position. */
7673 h = it2.current_y - it->current_y;
7674 /* NLINES is the distance in number of lines. */
7675 nlines = it2.vpos - it->vpos;
7676
7677 /* Correct IT's y and vpos position
7678 so that they are relative to the starting point. */
7679 it->vpos -= nlines;
7680 it->current_y -= h;
7681
7682 if (dy == 0)
7683 {
7684 /* DY == 0 means move to the start of the screen line. The
7685 value of nlines is > 0 if continuation lines were involved. */
7686 if (nlines > 0)
7687 move_it_by_lines (it, nlines, 1);
7688 }
7689 else
7690 {
7691 /* The y-position we try to reach, relative to *IT.
7692 Note that H has been subtracted in front of the if-statement. */
7693 int target_y = it->current_y + h - dy;
7694 int y0 = it3.current_y;
7695 int y1 = line_bottom_y (&it3);
7696 int line_height = y1 - y0;
7697
7698 /* If we did not reach target_y, try to move further backward if
7699 we can. If we moved too far backward, try to move forward. */
7700 if (target_y < it->current_y
7701 /* This is heuristic. In a window that's 3 lines high, with
7702 a line height of 13 pixels each, recentering with point
7703 on the bottom line will try to move -39/2 = 19 pixels
7704 backward. Try to avoid moving into the first line. */
7705 && (it->current_y - target_y
7706 > min (window_box_height (it->w), line_height * 2 / 3))
7707 && IT_CHARPOS (*it) > BEGV)
7708 {
7709 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7710 target_y - it->current_y));
7711 dy = it->current_y - target_y;
7712 goto move_further_back;
7713 }
7714 else if (target_y >= it->current_y + line_height
7715 && IT_CHARPOS (*it) < ZV)
7716 {
7717 /* Should move forward by at least one line, maybe more.
7718
7719 Note: Calling move_it_by_lines can be expensive on
7720 terminal frames, where compute_motion is used (via
7721 vmotion) to do the job, when there are very long lines
7722 and truncate-lines is nil. That's the reason for
7723 treating terminal frames specially here. */
7724
7725 if (!FRAME_WINDOW_P (it->f))
7726 move_it_vertically (it, target_y - (it->current_y + line_height));
7727 else
7728 {
7729 do
7730 {
7731 move_it_by_lines (it, 1, 1);
7732 }
7733 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7734 }
7735 }
7736 }
7737 }
7738
7739
7740 /* Move IT by a specified amount of pixel lines DY. DY negative means
7741 move backwards. DY = 0 means move to start of screen line. At the
7742 end, IT will be on the start of a screen line. */
7743
7744 void
7745 move_it_vertically (it, dy)
7746 struct it *it;
7747 int dy;
7748 {
7749 if (dy <= 0)
7750 move_it_vertically_backward (it, -dy);
7751 else
7752 {
7753 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7754 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7755 MOVE_TO_POS | MOVE_TO_Y);
7756 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7757
7758 /* If buffer ends in ZV without a newline, move to the start of
7759 the line to satisfy the post-condition. */
7760 if (IT_CHARPOS (*it) == ZV
7761 && ZV > BEGV
7762 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7763 move_it_by_lines (it, 0, 0);
7764 }
7765 }
7766
7767
7768 /* Move iterator IT past the end of the text line it is in. */
7769
7770 void
7771 move_it_past_eol (it)
7772 struct it *it;
7773 {
7774 enum move_it_result rc;
7775
7776 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7777 if (rc == MOVE_NEWLINE_OR_CR)
7778 set_iterator_to_next (it, 0);
7779 }
7780
7781
7782 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7783 negative means move up. DVPOS == 0 means move to the start of the
7784 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7785 NEED_Y_P is zero, IT->current_y will be left unchanged.
7786
7787 Further optimization ideas: If we would know that IT->f doesn't use
7788 a face with proportional font, we could be faster for
7789 truncate-lines nil. */
7790
7791 void
7792 move_it_by_lines (it, dvpos, need_y_p)
7793 struct it *it;
7794 int dvpos, need_y_p;
7795 {
7796 struct position pos;
7797
7798 /* The commented-out optimization uses vmotion on terminals. This
7799 gives bad results, because elements like it->what, on which
7800 callers such as pos_visible_p rely, aren't updated. */
7801 /* if (!FRAME_WINDOW_P (it->f))
7802 {
7803 struct text_pos textpos;
7804
7805 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7806 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7807 reseat (it, textpos, 1);
7808 it->vpos += pos.vpos;
7809 it->current_y += pos.vpos;
7810 }
7811 else */
7812
7813 if (dvpos == 0)
7814 {
7815 /* DVPOS == 0 means move to the start of the screen line. */
7816 move_it_vertically_backward (it, 0);
7817 xassert (it->current_x == 0 && it->hpos == 0);
7818 /* Let next call to line_bottom_y calculate real line height */
7819 last_height = 0;
7820 }
7821 else if (dvpos > 0)
7822 {
7823 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7824 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7825 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7826 }
7827 else
7828 {
7829 struct it it2;
7830 int start_charpos, i;
7831
7832 /* Start at the beginning of the screen line containing IT's
7833 position. This may actually move vertically backwards,
7834 in case of overlays, so adjust dvpos accordingly. */
7835 dvpos += it->vpos;
7836 move_it_vertically_backward (it, 0);
7837 dvpos -= it->vpos;
7838
7839 /* Go back -DVPOS visible lines and reseat the iterator there. */
7840 start_charpos = IT_CHARPOS (*it);
7841 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7842 back_to_previous_visible_line_start (it);
7843 reseat (it, it->current.pos, 1);
7844
7845 /* Move further back if we end up in a string or an image. */
7846 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7847 {
7848 /* First try to move to start of display line. */
7849 dvpos += it->vpos;
7850 move_it_vertically_backward (it, 0);
7851 dvpos -= it->vpos;
7852 if (IT_POS_VALID_AFTER_MOVE_P (it))
7853 break;
7854 /* If start of line is still in string or image,
7855 move further back. */
7856 back_to_previous_visible_line_start (it);
7857 reseat (it, it->current.pos, 1);
7858 dvpos--;
7859 }
7860
7861 it->current_x = it->hpos = 0;
7862
7863 /* Above call may have moved too far if continuation lines
7864 are involved. Scan forward and see if it did. */
7865 it2 = *it;
7866 it2.vpos = it2.current_y = 0;
7867 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7868 it->vpos -= it2.vpos;
7869 it->current_y -= it2.current_y;
7870 it->current_x = it->hpos = 0;
7871
7872 /* If we moved too far back, move IT some lines forward. */
7873 if (it2.vpos > -dvpos)
7874 {
7875 int delta = it2.vpos + dvpos;
7876 it2 = *it;
7877 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7878 /* Move back again if we got too far ahead. */
7879 if (IT_CHARPOS (*it) >= start_charpos)
7880 *it = it2;
7881 }
7882 }
7883 }
7884
7885 /* Return 1 if IT points into the middle of a display vector. */
7886
7887 int
7888 in_display_vector_p (it)
7889 struct it *it;
7890 {
7891 return (it->method == GET_FROM_DISPLAY_VECTOR
7892 && it->current.dpvec_index > 0
7893 && it->dpvec + it->current.dpvec_index != it->dpend);
7894 }
7895
7896 \f
7897 /***********************************************************************
7898 Messages
7899 ***********************************************************************/
7900
7901
7902 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7903 to *Messages*. */
7904
7905 void
7906 add_to_log (format, arg1, arg2)
7907 char *format;
7908 Lisp_Object arg1, arg2;
7909 {
7910 Lisp_Object args[3];
7911 Lisp_Object msg, fmt;
7912 char *buffer;
7913 int len;
7914 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7915 USE_SAFE_ALLOCA;
7916
7917 /* Do nothing if called asynchronously. Inserting text into
7918 a buffer may call after-change-functions and alike and
7919 that would means running Lisp asynchronously. */
7920 if (handling_signal)
7921 return;
7922
7923 fmt = msg = Qnil;
7924 GCPRO4 (fmt, msg, arg1, arg2);
7925
7926 args[0] = fmt = build_string (format);
7927 args[1] = arg1;
7928 args[2] = arg2;
7929 msg = Fformat (3, args);
7930
7931 len = SBYTES (msg) + 1;
7932 SAFE_ALLOCA (buffer, char *, len);
7933 bcopy (SDATA (msg), buffer, len);
7934
7935 message_dolog (buffer, len - 1, 1, 0);
7936 SAFE_FREE ();
7937
7938 UNGCPRO;
7939 }
7940
7941
7942 /* Output a newline in the *Messages* buffer if "needs" one. */
7943
7944 void
7945 message_log_maybe_newline ()
7946 {
7947 if (message_log_need_newline)
7948 message_dolog ("", 0, 1, 0);
7949 }
7950
7951
7952 /* Add a string M of length NBYTES to the message log, optionally
7953 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7954 nonzero, means interpret the contents of M as multibyte. This
7955 function calls low-level routines in order to bypass text property
7956 hooks, etc. which might not be safe to run.
7957
7958 This may GC (insert may run before/after change hooks),
7959 so the buffer M must NOT point to a Lisp string. */
7960
7961 void
7962 message_dolog (m, nbytes, nlflag, multibyte)
7963 const char *m;
7964 int nbytes, nlflag, multibyte;
7965 {
7966 if (!NILP (Vmemory_full))
7967 return;
7968
7969 if (!NILP (Vmessage_log_max))
7970 {
7971 struct buffer *oldbuf;
7972 Lisp_Object oldpoint, oldbegv, oldzv;
7973 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7974 int point_at_end = 0;
7975 int zv_at_end = 0;
7976 Lisp_Object old_deactivate_mark, tem;
7977 struct gcpro gcpro1;
7978
7979 old_deactivate_mark = Vdeactivate_mark;
7980 oldbuf = current_buffer;
7981 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7982 current_buffer->undo_list = Qt;
7983
7984 oldpoint = message_dolog_marker1;
7985 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7986 oldbegv = message_dolog_marker2;
7987 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7988 oldzv = message_dolog_marker3;
7989 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7990 GCPRO1 (old_deactivate_mark);
7991
7992 if (PT == Z)
7993 point_at_end = 1;
7994 if (ZV == Z)
7995 zv_at_end = 1;
7996
7997 BEGV = BEG;
7998 BEGV_BYTE = BEG_BYTE;
7999 ZV = Z;
8000 ZV_BYTE = Z_BYTE;
8001 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8002
8003 /* Insert the string--maybe converting multibyte to single byte
8004 or vice versa, so that all the text fits the buffer. */
8005 if (multibyte
8006 && NILP (current_buffer->enable_multibyte_characters))
8007 {
8008 int i, c, char_bytes;
8009 unsigned char work[1];
8010
8011 /* Convert a multibyte string to single-byte
8012 for the *Message* buffer. */
8013 for (i = 0; i < nbytes; i += char_bytes)
8014 {
8015 c = string_char_and_length (m + i, &char_bytes);
8016 work[0] = (ASCII_CHAR_P (c)
8017 ? c
8018 : multibyte_char_to_unibyte (c, Qnil));
8019 insert_1_both (work, 1, 1, 1, 0, 0);
8020 }
8021 }
8022 else if (! multibyte
8023 && ! NILP (current_buffer->enable_multibyte_characters))
8024 {
8025 int i, c, char_bytes;
8026 unsigned char *msg = (unsigned char *) m;
8027 unsigned char str[MAX_MULTIBYTE_LENGTH];
8028 /* Convert a single-byte string to multibyte
8029 for the *Message* buffer. */
8030 for (i = 0; i < nbytes; i++)
8031 {
8032 c = msg[i];
8033 MAKE_CHAR_MULTIBYTE (c);
8034 char_bytes = CHAR_STRING (c, str);
8035 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8036 }
8037 }
8038 else if (nbytes)
8039 insert_1 (m, nbytes, 1, 0, 0);
8040
8041 if (nlflag)
8042 {
8043 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
8044 insert_1 ("\n", 1, 1, 0, 0);
8045
8046 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8047 this_bol = PT;
8048 this_bol_byte = PT_BYTE;
8049
8050 /* See if this line duplicates the previous one.
8051 If so, combine duplicates. */
8052 if (this_bol > BEG)
8053 {
8054 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8055 prev_bol = PT;
8056 prev_bol_byte = PT_BYTE;
8057
8058 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8059 this_bol, this_bol_byte);
8060 if (dup)
8061 {
8062 del_range_both (prev_bol, prev_bol_byte,
8063 this_bol, this_bol_byte, 0);
8064 if (dup > 1)
8065 {
8066 char dupstr[40];
8067 int duplen;
8068
8069 /* If you change this format, don't forget to also
8070 change message_log_check_duplicate. */
8071 sprintf (dupstr, " [%d times]", dup);
8072 duplen = strlen (dupstr);
8073 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8074 insert_1 (dupstr, duplen, 1, 0, 1);
8075 }
8076 }
8077 }
8078
8079 /* If we have more than the desired maximum number of lines
8080 in the *Messages* buffer now, delete the oldest ones.
8081 This is safe because we don't have undo in this buffer. */
8082
8083 if (NATNUMP (Vmessage_log_max))
8084 {
8085 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8086 -XFASTINT (Vmessage_log_max) - 1, 0);
8087 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8088 }
8089 }
8090 BEGV = XMARKER (oldbegv)->charpos;
8091 BEGV_BYTE = marker_byte_position (oldbegv);
8092
8093 if (zv_at_end)
8094 {
8095 ZV = Z;
8096 ZV_BYTE = Z_BYTE;
8097 }
8098 else
8099 {
8100 ZV = XMARKER (oldzv)->charpos;
8101 ZV_BYTE = marker_byte_position (oldzv);
8102 }
8103
8104 if (point_at_end)
8105 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8106 else
8107 /* We can't do Fgoto_char (oldpoint) because it will run some
8108 Lisp code. */
8109 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8110 XMARKER (oldpoint)->bytepos);
8111
8112 UNGCPRO;
8113 unchain_marker (XMARKER (oldpoint));
8114 unchain_marker (XMARKER (oldbegv));
8115 unchain_marker (XMARKER (oldzv));
8116
8117 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8118 set_buffer_internal (oldbuf);
8119 if (NILP (tem))
8120 windows_or_buffers_changed = old_windows_or_buffers_changed;
8121 message_log_need_newline = !nlflag;
8122 Vdeactivate_mark = old_deactivate_mark;
8123 }
8124 }
8125
8126
8127 /* We are at the end of the buffer after just having inserted a newline.
8128 (Note: We depend on the fact we won't be crossing the gap.)
8129 Check to see if the most recent message looks a lot like the previous one.
8130 Return 0 if different, 1 if the new one should just replace it, or a
8131 value N > 1 if we should also append " [N times]". */
8132
8133 static int
8134 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
8135 int prev_bol, this_bol;
8136 int prev_bol_byte, this_bol_byte;
8137 {
8138 int i;
8139 int len = Z_BYTE - 1 - this_bol_byte;
8140 int seen_dots = 0;
8141 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8142 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8143
8144 for (i = 0; i < len; i++)
8145 {
8146 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8147 seen_dots = 1;
8148 if (p1[i] != p2[i])
8149 return seen_dots;
8150 }
8151 p1 += len;
8152 if (*p1 == '\n')
8153 return 2;
8154 if (*p1++ == ' ' && *p1++ == '[')
8155 {
8156 int n = 0;
8157 while (*p1 >= '0' && *p1 <= '9')
8158 n = n * 10 + *p1++ - '0';
8159 if (strncmp (p1, " times]\n", 8) == 0)
8160 return n+1;
8161 }
8162 return 0;
8163 }
8164 \f
8165
8166 /* Display an echo area message M with a specified length of NBYTES
8167 bytes. The string may include null characters. If M is 0, clear
8168 out any existing message, and let the mini-buffer text show
8169 through.
8170
8171 This may GC, so the buffer M must NOT point to a Lisp string. */
8172
8173 void
8174 message2 (m, nbytes, multibyte)
8175 const char *m;
8176 int nbytes;
8177 int multibyte;
8178 {
8179 /* First flush out any partial line written with print. */
8180 message_log_maybe_newline ();
8181 if (m)
8182 message_dolog (m, nbytes, 1, multibyte);
8183 message2_nolog (m, nbytes, multibyte);
8184 }
8185
8186
8187 /* The non-logging counterpart of message2. */
8188
8189 void
8190 message2_nolog (m, nbytes, multibyte)
8191 const char *m;
8192 int nbytes, multibyte;
8193 {
8194 struct frame *sf = SELECTED_FRAME ();
8195 message_enable_multibyte = multibyte;
8196
8197 if (FRAME_INITIAL_P (sf))
8198 {
8199 if (noninteractive_need_newline)
8200 putc ('\n', stderr);
8201 noninteractive_need_newline = 0;
8202 if (m)
8203 fwrite (m, nbytes, 1, stderr);
8204 if (cursor_in_echo_area == 0)
8205 fprintf (stderr, "\n");
8206 fflush (stderr);
8207 }
8208 /* A null message buffer means that the frame hasn't really been
8209 initialized yet. Error messages get reported properly by
8210 cmd_error, so this must be just an informative message; toss it. */
8211 else if (INTERACTIVE
8212 && sf->glyphs_initialized_p
8213 && FRAME_MESSAGE_BUF (sf))
8214 {
8215 Lisp_Object mini_window;
8216 struct frame *f;
8217
8218 /* Get the frame containing the mini-buffer
8219 that the selected frame is using. */
8220 mini_window = FRAME_MINIBUF_WINDOW (sf);
8221 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8222
8223 FRAME_SAMPLE_VISIBILITY (f);
8224 if (FRAME_VISIBLE_P (sf)
8225 && ! FRAME_VISIBLE_P (f))
8226 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8227
8228 if (m)
8229 {
8230 set_message (m, Qnil, nbytes, multibyte);
8231 if (minibuffer_auto_raise)
8232 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8233 }
8234 else
8235 clear_message (1, 1);
8236
8237 do_pending_window_change (0);
8238 echo_area_display (1);
8239 do_pending_window_change (0);
8240 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8241 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8242 }
8243 }
8244
8245
8246 /* Display an echo area message M with a specified length of NBYTES
8247 bytes. The string may include null characters. If M is not a
8248 string, clear out any existing message, and let the mini-buffer
8249 text show through.
8250
8251 This function cancels echoing. */
8252
8253 void
8254 message3 (m, nbytes, multibyte)
8255 Lisp_Object m;
8256 int nbytes;
8257 int multibyte;
8258 {
8259 struct gcpro gcpro1;
8260
8261 GCPRO1 (m);
8262 clear_message (1,1);
8263 cancel_echoing ();
8264
8265 /* First flush out any partial line written with print. */
8266 message_log_maybe_newline ();
8267 if (STRINGP (m))
8268 {
8269 char *buffer;
8270 USE_SAFE_ALLOCA;
8271
8272 SAFE_ALLOCA (buffer, char *, nbytes);
8273 bcopy (SDATA (m), buffer, nbytes);
8274 message_dolog (buffer, nbytes, 1, multibyte);
8275 SAFE_FREE ();
8276 }
8277 message3_nolog (m, nbytes, multibyte);
8278
8279 UNGCPRO;
8280 }
8281
8282
8283 /* The non-logging version of message3.
8284 This does not cancel echoing, because it is used for echoing.
8285 Perhaps we need to make a separate function for echoing
8286 and make this cancel echoing. */
8287
8288 void
8289 message3_nolog (m, nbytes, multibyte)
8290 Lisp_Object m;
8291 int nbytes, multibyte;
8292 {
8293 struct frame *sf = SELECTED_FRAME ();
8294 message_enable_multibyte = multibyte;
8295
8296 if (FRAME_INITIAL_P (sf))
8297 {
8298 if (noninteractive_need_newline)
8299 putc ('\n', stderr);
8300 noninteractive_need_newline = 0;
8301 if (STRINGP (m))
8302 fwrite (SDATA (m), nbytes, 1, stderr);
8303 if (cursor_in_echo_area == 0)
8304 fprintf (stderr, "\n");
8305 fflush (stderr);
8306 }
8307 /* A null message buffer means that the frame hasn't really been
8308 initialized yet. Error messages get reported properly by
8309 cmd_error, so this must be just an informative message; toss it. */
8310 else if (INTERACTIVE
8311 && sf->glyphs_initialized_p
8312 && FRAME_MESSAGE_BUF (sf))
8313 {
8314 Lisp_Object mini_window;
8315 Lisp_Object frame;
8316 struct frame *f;
8317
8318 /* Get the frame containing the mini-buffer
8319 that the selected frame is using. */
8320 mini_window = FRAME_MINIBUF_WINDOW (sf);
8321 frame = XWINDOW (mini_window)->frame;
8322 f = XFRAME (frame);
8323
8324 FRAME_SAMPLE_VISIBILITY (f);
8325 if (FRAME_VISIBLE_P (sf)
8326 && !FRAME_VISIBLE_P (f))
8327 Fmake_frame_visible (frame);
8328
8329 if (STRINGP (m) && SCHARS (m) > 0)
8330 {
8331 set_message (NULL, m, nbytes, multibyte);
8332 if (minibuffer_auto_raise)
8333 Fraise_frame (frame);
8334 /* Assume we are not echoing.
8335 (If we are, echo_now will override this.) */
8336 echo_message_buffer = Qnil;
8337 }
8338 else
8339 clear_message (1, 1);
8340
8341 do_pending_window_change (0);
8342 echo_area_display (1);
8343 do_pending_window_change (0);
8344 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8345 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8346 }
8347 }
8348
8349
8350 /* Display a null-terminated echo area message M. If M is 0, clear
8351 out any existing message, and let the mini-buffer text show through.
8352
8353 The buffer M must continue to exist until after the echo area gets
8354 cleared or some other message gets displayed there. Do not pass
8355 text that is stored in a Lisp string. Do not pass text in a buffer
8356 that was alloca'd. */
8357
8358 void
8359 message1 (m)
8360 char *m;
8361 {
8362 message2 (m, (m ? strlen (m) : 0), 0);
8363 }
8364
8365
8366 /* The non-logging counterpart of message1. */
8367
8368 void
8369 message1_nolog (m)
8370 char *m;
8371 {
8372 message2_nolog (m, (m ? strlen (m) : 0), 0);
8373 }
8374
8375 /* Display a message M which contains a single %s
8376 which gets replaced with STRING. */
8377
8378 void
8379 message_with_string (m, string, log)
8380 char *m;
8381 Lisp_Object string;
8382 int log;
8383 {
8384 CHECK_STRING (string);
8385
8386 if (noninteractive)
8387 {
8388 if (m)
8389 {
8390 if (noninteractive_need_newline)
8391 putc ('\n', stderr);
8392 noninteractive_need_newline = 0;
8393 fprintf (stderr, m, SDATA (string));
8394 if (!cursor_in_echo_area)
8395 fprintf (stderr, "\n");
8396 fflush (stderr);
8397 }
8398 }
8399 else if (INTERACTIVE)
8400 {
8401 /* The frame whose minibuffer we're going to display the message on.
8402 It may be larger than the selected frame, so we need
8403 to use its buffer, not the selected frame's buffer. */
8404 Lisp_Object mini_window;
8405 struct frame *f, *sf = SELECTED_FRAME ();
8406
8407 /* Get the frame containing the minibuffer
8408 that the selected frame is using. */
8409 mini_window = FRAME_MINIBUF_WINDOW (sf);
8410 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8411
8412 /* A null message buffer means that the frame hasn't really been
8413 initialized yet. Error messages get reported properly by
8414 cmd_error, so this must be just an informative message; toss it. */
8415 if (FRAME_MESSAGE_BUF (f))
8416 {
8417 Lisp_Object args[2], message;
8418 struct gcpro gcpro1, gcpro2;
8419
8420 args[0] = build_string (m);
8421 args[1] = message = string;
8422 GCPRO2 (args[0], message);
8423 gcpro1.nvars = 2;
8424
8425 message = Fformat (2, args);
8426
8427 if (log)
8428 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8429 else
8430 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8431
8432 UNGCPRO;
8433
8434 /* Print should start at the beginning of the message
8435 buffer next time. */
8436 message_buf_print = 0;
8437 }
8438 }
8439 }
8440
8441
8442 /* Dump an informative message to the minibuf. If M is 0, clear out
8443 any existing message, and let the mini-buffer text show through. */
8444
8445 /* VARARGS 1 */
8446 void
8447 message (m, a1, a2, a3)
8448 char *m;
8449 EMACS_INT a1, a2, a3;
8450 {
8451 if (noninteractive)
8452 {
8453 if (m)
8454 {
8455 if (noninteractive_need_newline)
8456 putc ('\n', stderr);
8457 noninteractive_need_newline = 0;
8458 fprintf (stderr, m, a1, a2, a3);
8459 if (cursor_in_echo_area == 0)
8460 fprintf (stderr, "\n");
8461 fflush (stderr);
8462 }
8463 }
8464 else if (INTERACTIVE)
8465 {
8466 /* The frame whose mini-buffer we're going to display the message
8467 on. It may be larger than the selected frame, so we need to
8468 use its buffer, not the selected frame's buffer. */
8469 Lisp_Object mini_window;
8470 struct frame *f, *sf = SELECTED_FRAME ();
8471
8472 /* Get the frame containing the mini-buffer
8473 that the selected frame is using. */
8474 mini_window = FRAME_MINIBUF_WINDOW (sf);
8475 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8476
8477 /* A null message buffer means that the frame hasn't really been
8478 initialized yet. Error messages get reported properly by
8479 cmd_error, so this must be just an informative message; toss
8480 it. */
8481 if (FRAME_MESSAGE_BUF (f))
8482 {
8483 if (m)
8484 {
8485 int len;
8486 #ifdef NO_ARG_ARRAY
8487 char *a[3];
8488 a[0] = (char *) a1;
8489 a[1] = (char *) a2;
8490 a[2] = (char *) a3;
8491
8492 len = doprnt (FRAME_MESSAGE_BUF (f),
8493 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8494 #else
8495 len = doprnt (FRAME_MESSAGE_BUF (f),
8496 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8497 (char **) &a1);
8498 #endif /* NO_ARG_ARRAY */
8499
8500 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8501 }
8502 else
8503 message1 (0);
8504
8505 /* Print should start at the beginning of the message
8506 buffer next time. */
8507 message_buf_print = 0;
8508 }
8509 }
8510 }
8511
8512
8513 /* The non-logging version of message. */
8514
8515 void
8516 message_nolog (m, a1, a2, a3)
8517 char *m;
8518 EMACS_INT a1, a2, a3;
8519 {
8520 Lisp_Object old_log_max;
8521 old_log_max = Vmessage_log_max;
8522 Vmessage_log_max = Qnil;
8523 message (m, a1, a2, a3);
8524 Vmessage_log_max = old_log_max;
8525 }
8526
8527
8528 /* Display the current message in the current mini-buffer. This is
8529 only called from error handlers in process.c, and is not time
8530 critical. */
8531
8532 void
8533 update_echo_area ()
8534 {
8535 if (!NILP (echo_area_buffer[0]))
8536 {
8537 Lisp_Object string;
8538 string = Fcurrent_message ();
8539 message3 (string, SBYTES (string),
8540 !NILP (current_buffer->enable_multibyte_characters));
8541 }
8542 }
8543
8544
8545 /* Make sure echo area buffers in `echo_buffers' are live.
8546 If they aren't, make new ones. */
8547
8548 static void
8549 ensure_echo_area_buffers ()
8550 {
8551 int i;
8552
8553 for (i = 0; i < 2; ++i)
8554 if (!BUFFERP (echo_buffer[i])
8555 || NILP (XBUFFER (echo_buffer[i])->name))
8556 {
8557 char name[30];
8558 Lisp_Object old_buffer;
8559 int j;
8560
8561 old_buffer = echo_buffer[i];
8562 sprintf (name, " *Echo Area %d*", i);
8563 echo_buffer[i] = Fget_buffer_create (build_string (name));
8564 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8565 /* to force word wrap in echo area -
8566 it was decided to postpone this*/
8567 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8568
8569 for (j = 0; j < 2; ++j)
8570 if (EQ (old_buffer, echo_area_buffer[j]))
8571 echo_area_buffer[j] = echo_buffer[i];
8572 }
8573 }
8574
8575
8576 /* Call FN with args A1..A4 with either the current or last displayed
8577 echo_area_buffer as current buffer.
8578
8579 WHICH zero means use the current message buffer
8580 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8581 from echo_buffer[] and clear it.
8582
8583 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8584 suitable buffer from echo_buffer[] and clear it.
8585
8586 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8587 that the current message becomes the last displayed one, make
8588 choose a suitable buffer for echo_area_buffer[0], and clear it.
8589
8590 Value is what FN returns. */
8591
8592 static int
8593 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8594 struct window *w;
8595 int which;
8596 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8597 EMACS_INT a1;
8598 Lisp_Object a2;
8599 EMACS_INT a3, a4;
8600 {
8601 Lisp_Object buffer;
8602 int this_one, the_other, clear_buffer_p, rc;
8603 int count = SPECPDL_INDEX ();
8604
8605 /* If buffers aren't live, make new ones. */
8606 ensure_echo_area_buffers ();
8607
8608 clear_buffer_p = 0;
8609
8610 if (which == 0)
8611 this_one = 0, the_other = 1;
8612 else if (which > 0)
8613 this_one = 1, the_other = 0;
8614 else
8615 {
8616 this_one = 0, the_other = 1;
8617 clear_buffer_p = 1;
8618
8619 /* We need a fresh one in case the current echo buffer equals
8620 the one containing the last displayed echo area message. */
8621 if (!NILP (echo_area_buffer[this_one])
8622 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8623 echo_area_buffer[this_one] = Qnil;
8624 }
8625
8626 /* Choose a suitable buffer from echo_buffer[] is we don't
8627 have one. */
8628 if (NILP (echo_area_buffer[this_one]))
8629 {
8630 echo_area_buffer[this_one]
8631 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8632 ? echo_buffer[the_other]
8633 : echo_buffer[this_one]);
8634 clear_buffer_p = 1;
8635 }
8636
8637 buffer = echo_area_buffer[this_one];
8638
8639 /* Don't get confused by reusing the buffer used for echoing
8640 for a different purpose. */
8641 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8642 cancel_echoing ();
8643
8644 record_unwind_protect (unwind_with_echo_area_buffer,
8645 with_echo_area_buffer_unwind_data (w));
8646
8647 /* Make the echo area buffer current. Note that for display
8648 purposes, it is not necessary that the displayed window's buffer
8649 == current_buffer, except for text property lookup. So, let's
8650 only set that buffer temporarily here without doing a full
8651 Fset_window_buffer. We must also change w->pointm, though,
8652 because otherwise an assertions in unshow_buffer fails, and Emacs
8653 aborts. */
8654 set_buffer_internal_1 (XBUFFER (buffer));
8655 if (w)
8656 {
8657 w->buffer = buffer;
8658 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8659 }
8660
8661 current_buffer->undo_list = Qt;
8662 current_buffer->read_only = Qnil;
8663 specbind (Qinhibit_read_only, Qt);
8664 specbind (Qinhibit_modification_hooks, Qt);
8665
8666 if (clear_buffer_p && Z > BEG)
8667 del_range (BEG, Z);
8668
8669 xassert (BEGV >= BEG);
8670 xassert (ZV <= Z && ZV >= BEGV);
8671
8672 rc = fn (a1, a2, a3, a4);
8673
8674 xassert (BEGV >= BEG);
8675 xassert (ZV <= Z && ZV >= BEGV);
8676
8677 unbind_to (count, Qnil);
8678 return rc;
8679 }
8680
8681
8682 /* Save state that should be preserved around the call to the function
8683 FN called in with_echo_area_buffer. */
8684
8685 static Lisp_Object
8686 with_echo_area_buffer_unwind_data (w)
8687 struct window *w;
8688 {
8689 int i = 0;
8690 Lisp_Object vector, tmp;
8691
8692 /* Reduce consing by keeping one vector in
8693 Vwith_echo_area_save_vector. */
8694 vector = Vwith_echo_area_save_vector;
8695 Vwith_echo_area_save_vector = Qnil;
8696
8697 if (NILP (vector))
8698 vector = Fmake_vector (make_number (7), Qnil);
8699
8700 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8701 ASET (vector, i, Vdeactivate_mark); ++i;
8702 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8703
8704 if (w)
8705 {
8706 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8707 ASET (vector, i, w->buffer); ++i;
8708 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8709 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8710 }
8711 else
8712 {
8713 int end = i + 4;
8714 for (; i < end; ++i)
8715 ASET (vector, i, Qnil);
8716 }
8717
8718 xassert (i == ASIZE (vector));
8719 return vector;
8720 }
8721
8722
8723 /* Restore global state from VECTOR which was created by
8724 with_echo_area_buffer_unwind_data. */
8725
8726 static Lisp_Object
8727 unwind_with_echo_area_buffer (vector)
8728 Lisp_Object vector;
8729 {
8730 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8731 Vdeactivate_mark = AREF (vector, 1);
8732 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8733
8734 if (WINDOWP (AREF (vector, 3)))
8735 {
8736 struct window *w;
8737 Lisp_Object buffer, charpos, bytepos;
8738
8739 w = XWINDOW (AREF (vector, 3));
8740 buffer = AREF (vector, 4);
8741 charpos = AREF (vector, 5);
8742 bytepos = AREF (vector, 6);
8743
8744 w->buffer = buffer;
8745 set_marker_both (w->pointm, buffer,
8746 XFASTINT (charpos), XFASTINT (bytepos));
8747 }
8748
8749 Vwith_echo_area_save_vector = vector;
8750 return Qnil;
8751 }
8752
8753
8754 /* Set up the echo area for use by print functions. MULTIBYTE_P
8755 non-zero means we will print multibyte. */
8756
8757 void
8758 setup_echo_area_for_printing (multibyte_p)
8759 int multibyte_p;
8760 {
8761 /* If we can't find an echo area any more, exit. */
8762 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8763 Fkill_emacs (Qnil);
8764
8765 ensure_echo_area_buffers ();
8766
8767 if (!message_buf_print)
8768 {
8769 /* A message has been output since the last time we printed.
8770 Choose a fresh echo area buffer. */
8771 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8772 echo_area_buffer[0] = echo_buffer[1];
8773 else
8774 echo_area_buffer[0] = echo_buffer[0];
8775
8776 /* Switch to that buffer and clear it. */
8777 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8778 current_buffer->truncate_lines = Qnil;
8779
8780 if (Z > BEG)
8781 {
8782 int count = SPECPDL_INDEX ();
8783 specbind (Qinhibit_read_only, Qt);
8784 /* Note that undo recording is always disabled. */
8785 del_range (BEG, Z);
8786 unbind_to (count, Qnil);
8787 }
8788 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8789
8790 /* Set up the buffer for the multibyteness we need. */
8791 if (multibyte_p
8792 != !NILP (current_buffer->enable_multibyte_characters))
8793 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8794
8795 /* Raise the frame containing the echo area. */
8796 if (minibuffer_auto_raise)
8797 {
8798 struct frame *sf = SELECTED_FRAME ();
8799 Lisp_Object mini_window;
8800 mini_window = FRAME_MINIBUF_WINDOW (sf);
8801 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8802 }
8803
8804 message_log_maybe_newline ();
8805 message_buf_print = 1;
8806 }
8807 else
8808 {
8809 if (NILP (echo_area_buffer[0]))
8810 {
8811 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8812 echo_area_buffer[0] = echo_buffer[1];
8813 else
8814 echo_area_buffer[0] = echo_buffer[0];
8815 }
8816
8817 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8818 {
8819 /* Someone switched buffers between print requests. */
8820 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8821 current_buffer->truncate_lines = Qnil;
8822 }
8823 }
8824 }
8825
8826
8827 /* Display an echo area message in window W. Value is non-zero if W's
8828 height is changed. If display_last_displayed_message_p is
8829 non-zero, display the message that was last displayed, otherwise
8830 display the current message. */
8831
8832 static int
8833 display_echo_area (w)
8834 struct window *w;
8835 {
8836 int i, no_message_p, window_height_changed_p, count;
8837
8838 /* Temporarily disable garbage collections while displaying the echo
8839 area. This is done because a GC can print a message itself.
8840 That message would modify the echo area buffer's contents while a
8841 redisplay of the buffer is going on, and seriously confuse
8842 redisplay. */
8843 count = inhibit_garbage_collection ();
8844
8845 /* If there is no message, we must call display_echo_area_1
8846 nevertheless because it resizes the window. But we will have to
8847 reset the echo_area_buffer in question to nil at the end because
8848 with_echo_area_buffer will sets it to an empty buffer. */
8849 i = display_last_displayed_message_p ? 1 : 0;
8850 no_message_p = NILP (echo_area_buffer[i]);
8851
8852 window_height_changed_p
8853 = with_echo_area_buffer (w, display_last_displayed_message_p,
8854 display_echo_area_1,
8855 (EMACS_INT) w, Qnil, 0, 0);
8856
8857 if (no_message_p)
8858 echo_area_buffer[i] = Qnil;
8859
8860 unbind_to (count, Qnil);
8861 return window_height_changed_p;
8862 }
8863
8864
8865 /* Helper for display_echo_area. Display the current buffer which
8866 contains the current echo area message in window W, a mini-window,
8867 a pointer to which is passed in A1. A2..A4 are currently not used.
8868 Change the height of W so that all of the message is displayed.
8869 Value is non-zero if height of W was changed. */
8870
8871 static int
8872 display_echo_area_1 (a1, a2, a3, a4)
8873 EMACS_INT a1;
8874 Lisp_Object a2;
8875 EMACS_INT a3, a4;
8876 {
8877 struct window *w = (struct window *) a1;
8878 Lisp_Object window;
8879 struct text_pos start;
8880 int window_height_changed_p = 0;
8881
8882 /* Do this before displaying, so that we have a large enough glyph
8883 matrix for the display. If we can't get enough space for the
8884 whole text, display the last N lines. That works by setting w->start. */
8885 window_height_changed_p = resize_mini_window (w, 0);
8886
8887 /* Use the starting position chosen by resize_mini_window. */
8888 SET_TEXT_POS_FROM_MARKER (start, w->start);
8889
8890 /* Display. */
8891 clear_glyph_matrix (w->desired_matrix);
8892 XSETWINDOW (window, w);
8893 try_window (window, start, 0);
8894
8895 return window_height_changed_p;
8896 }
8897
8898
8899 /* Resize the echo area window to exactly the size needed for the
8900 currently displayed message, if there is one. If a mini-buffer
8901 is active, don't shrink it. */
8902
8903 void
8904 resize_echo_area_exactly ()
8905 {
8906 if (BUFFERP (echo_area_buffer[0])
8907 && WINDOWP (echo_area_window))
8908 {
8909 struct window *w = XWINDOW (echo_area_window);
8910 int resized_p;
8911 Lisp_Object resize_exactly;
8912
8913 if (minibuf_level == 0)
8914 resize_exactly = Qt;
8915 else
8916 resize_exactly = Qnil;
8917
8918 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8919 (EMACS_INT) w, resize_exactly, 0, 0);
8920 if (resized_p)
8921 {
8922 ++windows_or_buffers_changed;
8923 ++update_mode_lines;
8924 redisplay_internal (0);
8925 }
8926 }
8927 }
8928
8929
8930 /* Callback function for with_echo_area_buffer, when used from
8931 resize_echo_area_exactly. A1 contains a pointer to the window to
8932 resize, EXACTLY non-nil means resize the mini-window exactly to the
8933 size of the text displayed. A3 and A4 are not used. Value is what
8934 resize_mini_window returns. */
8935
8936 static int
8937 resize_mini_window_1 (a1, exactly, a3, a4)
8938 EMACS_INT a1;
8939 Lisp_Object exactly;
8940 EMACS_INT a3, a4;
8941 {
8942 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8943 }
8944
8945
8946 /* Resize mini-window W to fit the size of its contents. EXACT_P
8947 means size the window exactly to the size needed. Otherwise, it's
8948 only enlarged until W's buffer is empty.
8949
8950 Set W->start to the right place to begin display. If the whole
8951 contents fit, start at the beginning. Otherwise, start so as
8952 to make the end of the contents appear. This is particularly
8953 important for y-or-n-p, but seems desirable generally.
8954
8955 Value is non-zero if the window height has been changed. */
8956
8957 int
8958 resize_mini_window (w, exact_p)
8959 struct window *w;
8960 int exact_p;
8961 {
8962 struct frame *f = XFRAME (w->frame);
8963 int window_height_changed_p = 0;
8964
8965 xassert (MINI_WINDOW_P (w));
8966
8967 /* By default, start display at the beginning. */
8968 set_marker_both (w->start, w->buffer,
8969 BUF_BEGV (XBUFFER (w->buffer)),
8970 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8971
8972 /* Don't resize windows while redisplaying a window; it would
8973 confuse redisplay functions when the size of the window they are
8974 displaying changes from under them. Such a resizing can happen,
8975 for instance, when which-func prints a long message while
8976 we are running fontification-functions. We're running these
8977 functions with safe_call which binds inhibit-redisplay to t. */
8978 if (!NILP (Vinhibit_redisplay))
8979 return 0;
8980
8981 /* Nil means don't try to resize. */
8982 if (NILP (Vresize_mini_windows)
8983 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8984 return 0;
8985
8986 if (!FRAME_MINIBUF_ONLY_P (f))
8987 {
8988 struct it it;
8989 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8990 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8991 int height, max_height;
8992 int unit = FRAME_LINE_HEIGHT (f);
8993 struct text_pos start;
8994 struct buffer *old_current_buffer = NULL;
8995
8996 if (current_buffer != XBUFFER (w->buffer))
8997 {
8998 old_current_buffer = current_buffer;
8999 set_buffer_internal (XBUFFER (w->buffer));
9000 }
9001
9002 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9003
9004 /* Compute the max. number of lines specified by the user. */
9005 if (FLOATP (Vmax_mini_window_height))
9006 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9007 else if (INTEGERP (Vmax_mini_window_height))
9008 max_height = XINT (Vmax_mini_window_height);
9009 else
9010 max_height = total_height / 4;
9011
9012 /* Correct that max. height if it's bogus. */
9013 max_height = max (1, max_height);
9014 max_height = min (total_height, max_height);
9015
9016 /* Find out the height of the text in the window. */
9017 if (it.line_wrap == TRUNCATE)
9018 height = 1;
9019 else
9020 {
9021 last_height = 0;
9022 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9023 if (it.max_ascent == 0 && it.max_descent == 0)
9024 height = it.current_y + last_height;
9025 else
9026 height = it.current_y + it.max_ascent + it.max_descent;
9027 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9028 height = (height + unit - 1) / unit;
9029 }
9030
9031 /* Compute a suitable window start. */
9032 if (height > max_height)
9033 {
9034 height = max_height;
9035 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9036 move_it_vertically_backward (&it, (height - 1) * unit);
9037 start = it.current.pos;
9038 }
9039 else
9040 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9041 SET_MARKER_FROM_TEXT_POS (w->start, start);
9042
9043 if (EQ (Vresize_mini_windows, Qgrow_only))
9044 {
9045 /* Let it grow only, until we display an empty message, in which
9046 case the window shrinks again. */
9047 if (height > WINDOW_TOTAL_LINES (w))
9048 {
9049 int old_height = WINDOW_TOTAL_LINES (w);
9050 freeze_window_starts (f, 1);
9051 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9052 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9053 }
9054 else if (height < WINDOW_TOTAL_LINES (w)
9055 && (exact_p || BEGV == ZV))
9056 {
9057 int old_height = WINDOW_TOTAL_LINES (w);
9058 freeze_window_starts (f, 0);
9059 shrink_mini_window (w);
9060 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9061 }
9062 }
9063 else
9064 {
9065 /* Always resize to exact size needed. */
9066 if (height > WINDOW_TOTAL_LINES (w))
9067 {
9068 int old_height = WINDOW_TOTAL_LINES (w);
9069 freeze_window_starts (f, 1);
9070 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9071 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9072 }
9073 else if (height < WINDOW_TOTAL_LINES (w))
9074 {
9075 int old_height = WINDOW_TOTAL_LINES (w);
9076 freeze_window_starts (f, 0);
9077 shrink_mini_window (w);
9078
9079 if (height)
9080 {
9081 freeze_window_starts (f, 1);
9082 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9083 }
9084
9085 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9086 }
9087 }
9088
9089 if (old_current_buffer)
9090 set_buffer_internal (old_current_buffer);
9091 }
9092
9093 return window_height_changed_p;
9094 }
9095
9096
9097 /* Value is the current message, a string, or nil if there is no
9098 current message. */
9099
9100 Lisp_Object
9101 current_message ()
9102 {
9103 Lisp_Object msg;
9104
9105 if (!BUFFERP (echo_area_buffer[0]))
9106 msg = Qnil;
9107 else
9108 {
9109 with_echo_area_buffer (0, 0, current_message_1,
9110 (EMACS_INT) &msg, Qnil, 0, 0);
9111 if (NILP (msg))
9112 echo_area_buffer[0] = Qnil;
9113 }
9114
9115 return msg;
9116 }
9117
9118
9119 static int
9120 current_message_1 (a1, a2, a3, a4)
9121 EMACS_INT a1;
9122 Lisp_Object a2;
9123 EMACS_INT a3, a4;
9124 {
9125 Lisp_Object *msg = (Lisp_Object *) a1;
9126
9127 if (Z > BEG)
9128 *msg = make_buffer_string (BEG, Z, 1);
9129 else
9130 *msg = Qnil;
9131 return 0;
9132 }
9133
9134
9135 /* Push the current message on Vmessage_stack for later restauration
9136 by restore_message. Value is non-zero if the current message isn't
9137 empty. This is a relatively infrequent operation, so it's not
9138 worth optimizing. */
9139
9140 int
9141 push_message ()
9142 {
9143 Lisp_Object msg;
9144 msg = current_message ();
9145 Vmessage_stack = Fcons (msg, Vmessage_stack);
9146 return STRINGP (msg);
9147 }
9148
9149
9150 /* Restore message display from the top of Vmessage_stack. */
9151
9152 void
9153 restore_message ()
9154 {
9155 Lisp_Object msg;
9156
9157 xassert (CONSP (Vmessage_stack));
9158 msg = XCAR (Vmessage_stack);
9159 if (STRINGP (msg))
9160 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9161 else
9162 message3_nolog (msg, 0, 0);
9163 }
9164
9165
9166 /* Handler for record_unwind_protect calling pop_message. */
9167
9168 Lisp_Object
9169 pop_message_unwind (dummy)
9170 Lisp_Object dummy;
9171 {
9172 pop_message ();
9173 return Qnil;
9174 }
9175
9176 /* Pop the top-most entry off Vmessage_stack. */
9177
9178 void
9179 pop_message ()
9180 {
9181 xassert (CONSP (Vmessage_stack));
9182 Vmessage_stack = XCDR (Vmessage_stack);
9183 }
9184
9185
9186 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9187 exits. If the stack is not empty, we have a missing pop_message
9188 somewhere. */
9189
9190 void
9191 check_message_stack ()
9192 {
9193 if (!NILP (Vmessage_stack))
9194 abort ();
9195 }
9196
9197
9198 /* Truncate to NCHARS what will be displayed in the echo area the next
9199 time we display it---but don't redisplay it now. */
9200
9201 void
9202 truncate_echo_area (nchars)
9203 int nchars;
9204 {
9205 if (nchars == 0)
9206 echo_area_buffer[0] = Qnil;
9207 /* A null message buffer means that the frame hasn't really been
9208 initialized yet. Error messages get reported properly by
9209 cmd_error, so this must be just an informative message; toss it. */
9210 else if (!noninteractive
9211 && INTERACTIVE
9212 && !NILP (echo_area_buffer[0]))
9213 {
9214 struct frame *sf = SELECTED_FRAME ();
9215 if (FRAME_MESSAGE_BUF (sf))
9216 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9217 }
9218 }
9219
9220
9221 /* Helper function for truncate_echo_area. Truncate the current
9222 message to at most NCHARS characters. */
9223
9224 static int
9225 truncate_message_1 (nchars, a2, a3, a4)
9226 EMACS_INT nchars;
9227 Lisp_Object a2;
9228 EMACS_INT a3, a4;
9229 {
9230 if (BEG + nchars < Z)
9231 del_range (BEG + nchars, Z);
9232 if (Z == BEG)
9233 echo_area_buffer[0] = Qnil;
9234 return 0;
9235 }
9236
9237
9238 /* Set the current message to a substring of S or STRING.
9239
9240 If STRING is a Lisp string, set the message to the first NBYTES
9241 bytes from STRING. NBYTES zero means use the whole string. If
9242 STRING is multibyte, the message will be displayed multibyte.
9243
9244 If S is not null, set the message to the first LEN bytes of S. LEN
9245 zero means use the whole string. MULTIBYTE_P non-zero means S is
9246 multibyte. Display the message multibyte in that case.
9247
9248 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9249 to t before calling set_message_1 (which calls insert).
9250 */
9251
9252 void
9253 set_message (s, string, nbytes, multibyte_p)
9254 const char *s;
9255 Lisp_Object string;
9256 int nbytes, multibyte_p;
9257 {
9258 message_enable_multibyte
9259 = ((s && multibyte_p)
9260 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9261
9262 with_echo_area_buffer (0, -1, set_message_1,
9263 (EMACS_INT) s, string, nbytes, multibyte_p);
9264 message_buf_print = 0;
9265 help_echo_showing_p = 0;
9266 }
9267
9268
9269 /* Helper function for set_message. Arguments have the same meaning
9270 as there, with A1 corresponding to S and A2 corresponding to STRING
9271 This function is called with the echo area buffer being
9272 current. */
9273
9274 static int
9275 set_message_1 (a1, a2, nbytes, multibyte_p)
9276 EMACS_INT a1;
9277 Lisp_Object a2;
9278 EMACS_INT nbytes, multibyte_p;
9279 {
9280 const char *s = (const char *) a1;
9281 Lisp_Object string = a2;
9282
9283 /* Change multibyteness of the echo buffer appropriately. */
9284 if (message_enable_multibyte
9285 != !NILP (current_buffer->enable_multibyte_characters))
9286 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9287
9288 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9289
9290 /* Insert new message at BEG. */
9291 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9292
9293 if (STRINGP (string))
9294 {
9295 int nchars;
9296
9297 if (nbytes == 0)
9298 nbytes = SBYTES (string);
9299 nchars = string_byte_to_char (string, nbytes);
9300
9301 /* This function takes care of single/multibyte conversion. We
9302 just have to ensure that the echo area buffer has the right
9303 setting of enable_multibyte_characters. */
9304 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9305 }
9306 else if (s)
9307 {
9308 if (nbytes == 0)
9309 nbytes = strlen (s);
9310
9311 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9312 {
9313 /* Convert from multi-byte to single-byte. */
9314 int i, c, n;
9315 unsigned char work[1];
9316
9317 /* Convert a multibyte string to single-byte. */
9318 for (i = 0; i < nbytes; i += n)
9319 {
9320 c = string_char_and_length (s + i, &n);
9321 work[0] = (ASCII_CHAR_P (c)
9322 ? c
9323 : multibyte_char_to_unibyte (c, Qnil));
9324 insert_1_both (work, 1, 1, 1, 0, 0);
9325 }
9326 }
9327 else if (!multibyte_p
9328 && !NILP (current_buffer->enable_multibyte_characters))
9329 {
9330 /* Convert from single-byte to multi-byte. */
9331 int i, c, n;
9332 const unsigned char *msg = (const unsigned char *) s;
9333 unsigned char str[MAX_MULTIBYTE_LENGTH];
9334
9335 /* Convert a single-byte string to multibyte. */
9336 for (i = 0; i < nbytes; i++)
9337 {
9338 c = msg[i];
9339 MAKE_CHAR_MULTIBYTE (c);
9340 n = CHAR_STRING (c, str);
9341 insert_1_both (str, 1, n, 1, 0, 0);
9342 }
9343 }
9344 else
9345 insert_1 (s, nbytes, 1, 0, 0);
9346 }
9347
9348 return 0;
9349 }
9350
9351
9352 /* Clear messages. CURRENT_P non-zero means clear the current
9353 message. LAST_DISPLAYED_P non-zero means clear the message
9354 last displayed. */
9355
9356 void
9357 clear_message (current_p, last_displayed_p)
9358 int current_p, last_displayed_p;
9359 {
9360 if (current_p)
9361 {
9362 echo_area_buffer[0] = Qnil;
9363 message_cleared_p = 1;
9364 }
9365
9366 if (last_displayed_p)
9367 echo_area_buffer[1] = Qnil;
9368
9369 message_buf_print = 0;
9370 }
9371
9372 /* Clear garbaged frames.
9373
9374 This function is used where the old redisplay called
9375 redraw_garbaged_frames which in turn called redraw_frame which in
9376 turn called clear_frame. The call to clear_frame was a source of
9377 flickering. I believe a clear_frame is not necessary. It should
9378 suffice in the new redisplay to invalidate all current matrices,
9379 and ensure a complete redisplay of all windows. */
9380
9381 static void
9382 clear_garbaged_frames ()
9383 {
9384 if (frame_garbaged)
9385 {
9386 Lisp_Object tail, frame;
9387 int changed_count = 0;
9388
9389 FOR_EACH_FRAME (tail, frame)
9390 {
9391 struct frame *f = XFRAME (frame);
9392
9393 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9394 {
9395 if (f->resized_p)
9396 {
9397 Fredraw_frame (frame);
9398 f->force_flush_display_p = 1;
9399 }
9400 clear_current_matrices (f);
9401 changed_count++;
9402 f->garbaged = 0;
9403 f->resized_p = 0;
9404 }
9405 }
9406
9407 frame_garbaged = 0;
9408 if (changed_count)
9409 ++windows_or_buffers_changed;
9410 }
9411 }
9412
9413
9414 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9415 is non-zero update selected_frame. Value is non-zero if the
9416 mini-windows height has been changed. */
9417
9418 static int
9419 echo_area_display (update_frame_p)
9420 int update_frame_p;
9421 {
9422 Lisp_Object mini_window;
9423 struct window *w;
9424 struct frame *f;
9425 int window_height_changed_p = 0;
9426 struct frame *sf = SELECTED_FRAME ();
9427
9428 mini_window = FRAME_MINIBUF_WINDOW (sf);
9429 w = XWINDOW (mini_window);
9430 f = XFRAME (WINDOW_FRAME (w));
9431
9432 /* Don't display if frame is invisible or not yet initialized. */
9433 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9434 return 0;
9435
9436 #ifdef HAVE_WINDOW_SYSTEM
9437 /* When Emacs starts, selected_frame may be the initial terminal
9438 frame. If we let this through, a message would be displayed on
9439 the terminal. */
9440 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9441 return 0;
9442 #endif /* HAVE_WINDOW_SYSTEM */
9443
9444 /* Redraw garbaged frames. */
9445 if (frame_garbaged)
9446 clear_garbaged_frames ();
9447
9448 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9449 {
9450 echo_area_window = mini_window;
9451 window_height_changed_p = display_echo_area (w);
9452 w->must_be_updated_p = 1;
9453
9454 /* Update the display, unless called from redisplay_internal.
9455 Also don't update the screen during redisplay itself. The
9456 update will happen at the end of redisplay, and an update
9457 here could cause confusion. */
9458 if (update_frame_p && !redisplaying_p)
9459 {
9460 int n = 0;
9461
9462 /* If the display update has been interrupted by pending
9463 input, update mode lines in the frame. Due to the
9464 pending input, it might have been that redisplay hasn't
9465 been called, so that mode lines above the echo area are
9466 garbaged. This looks odd, so we prevent it here. */
9467 if (!display_completed)
9468 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9469
9470 if (window_height_changed_p
9471 /* Don't do this if Emacs is shutting down. Redisplay
9472 needs to run hooks. */
9473 && !NILP (Vrun_hooks))
9474 {
9475 /* Must update other windows. Likewise as in other
9476 cases, don't let this update be interrupted by
9477 pending input. */
9478 int count = SPECPDL_INDEX ();
9479 specbind (Qredisplay_dont_pause, Qt);
9480 windows_or_buffers_changed = 1;
9481 redisplay_internal (0);
9482 unbind_to (count, Qnil);
9483 }
9484 else if (FRAME_WINDOW_P (f) && n == 0)
9485 {
9486 /* Window configuration is the same as before.
9487 Can do with a display update of the echo area,
9488 unless we displayed some mode lines. */
9489 update_single_window (w, 1);
9490 FRAME_RIF (f)->flush_display (f);
9491 }
9492 else
9493 update_frame (f, 1, 1);
9494
9495 /* If cursor is in the echo area, make sure that the next
9496 redisplay displays the minibuffer, so that the cursor will
9497 be replaced with what the minibuffer wants. */
9498 if (cursor_in_echo_area)
9499 ++windows_or_buffers_changed;
9500 }
9501 }
9502 else if (!EQ (mini_window, selected_window))
9503 windows_or_buffers_changed++;
9504
9505 /* Last displayed message is now the current message. */
9506 echo_area_buffer[1] = echo_area_buffer[0];
9507 /* Inform read_char that we're not echoing. */
9508 echo_message_buffer = Qnil;
9509
9510 /* Prevent redisplay optimization in redisplay_internal by resetting
9511 this_line_start_pos. This is done because the mini-buffer now
9512 displays the message instead of its buffer text. */
9513 if (EQ (mini_window, selected_window))
9514 CHARPOS (this_line_start_pos) = 0;
9515
9516 return window_height_changed_p;
9517 }
9518
9519
9520 \f
9521 /***********************************************************************
9522 Mode Lines and Frame Titles
9523 ***********************************************************************/
9524
9525 /* A buffer for constructing non-propertized mode-line strings and
9526 frame titles in it; allocated from the heap in init_xdisp and
9527 resized as needed in store_mode_line_noprop_char. */
9528
9529 static char *mode_line_noprop_buf;
9530
9531 /* The buffer's end, and a current output position in it. */
9532
9533 static char *mode_line_noprop_buf_end;
9534 static char *mode_line_noprop_ptr;
9535
9536 #define MODE_LINE_NOPROP_LEN(start) \
9537 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9538
9539 static enum {
9540 MODE_LINE_DISPLAY = 0,
9541 MODE_LINE_TITLE,
9542 MODE_LINE_NOPROP,
9543 MODE_LINE_STRING
9544 } mode_line_target;
9545
9546 /* Alist that caches the results of :propertize.
9547 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9548 static Lisp_Object mode_line_proptrans_alist;
9549
9550 /* List of strings making up the mode-line. */
9551 static Lisp_Object mode_line_string_list;
9552
9553 /* Base face property when building propertized mode line string. */
9554 static Lisp_Object mode_line_string_face;
9555 static Lisp_Object mode_line_string_face_prop;
9556
9557
9558 /* Unwind data for mode line strings */
9559
9560 static Lisp_Object Vmode_line_unwind_vector;
9561
9562 static Lisp_Object
9563 format_mode_line_unwind_data (struct buffer *obuf,
9564 Lisp_Object owin,
9565 int save_proptrans)
9566 {
9567 Lisp_Object vector, tmp;
9568
9569 /* Reduce consing by keeping one vector in
9570 Vwith_echo_area_save_vector. */
9571 vector = Vmode_line_unwind_vector;
9572 Vmode_line_unwind_vector = Qnil;
9573
9574 if (NILP (vector))
9575 vector = Fmake_vector (make_number (8), Qnil);
9576
9577 ASET (vector, 0, make_number (mode_line_target));
9578 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9579 ASET (vector, 2, mode_line_string_list);
9580 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9581 ASET (vector, 4, mode_line_string_face);
9582 ASET (vector, 5, mode_line_string_face_prop);
9583
9584 if (obuf)
9585 XSETBUFFER (tmp, obuf);
9586 else
9587 tmp = Qnil;
9588 ASET (vector, 6, tmp);
9589 ASET (vector, 7, owin);
9590
9591 return vector;
9592 }
9593
9594 static Lisp_Object
9595 unwind_format_mode_line (vector)
9596 Lisp_Object vector;
9597 {
9598 mode_line_target = XINT (AREF (vector, 0));
9599 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9600 mode_line_string_list = AREF (vector, 2);
9601 if (! EQ (AREF (vector, 3), Qt))
9602 mode_line_proptrans_alist = AREF (vector, 3);
9603 mode_line_string_face = AREF (vector, 4);
9604 mode_line_string_face_prop = AREF (vector, 5);
9605
9606 if (!NILP (AREF (vector, 7)))
9607 /* Select window before buffer, since it may change the buffer. */
9608 Fselect_window (AREF (vector, 7), Qt);
9609
9610 if (!NILP (AREF (vector, 6)))
9611 {
9612 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9613 ASET (vector, 6, Qnil);
9614 }
9615
9616 Vmode_line_unwind_vector = vector;
9617 return Qnil;
9618 }
9619
9620
9621 /* Store a single character C for the frame title in mode_line_noprop_buf.
9622 Re-allocate mode_line_noprop_buf if necessary. */
9623
9624 static void
9625 #ifdef PROTOTYPES
9626 store_mode_line_noprop_char (char c)
9627 #else
9628 store_mode_line_noprop_char (c)
9629 char c;
9630 #endif
9631 {
9632 /* If output position has reached the end of the allocated buffer,
9633 double the buffer's size. */
9634 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9635 {
9636 int len = MODE_LINE_NOPROP_LEN (0);
9637 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9638 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9639 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9640 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9641 }
9642
9643 *mode_line_noprop_ptr++ = c;
9644 }
9645
9646
9647 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9648 mode_line_noprop_ptr. STR is the string to store. Do not copy
9649 characters that yield more columns than PRECISION; PRECISION <= 0
9650 means copy the whole string. Pad with spaces until FIELD_WIDTH
9651 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9652 pad. Called from display_mode_element when it is used to build a
9653 frame title. */
9654
9655 static int
9656 store_mode_line_noprop (str, field_width, precision)
9657 const unsigned char *str;
9658 int field_width, precision;
9659 {
9660 int n = 0;
9661 int dummy, nbytes;
9662
9663 /* Copy at most PRECISION chars from STR. */
9664 nbytes = strlen (str);
9665 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9666 while (nbytes--)
9667 store_mode_line_noprop_char (*str++);
9668
9669 /* Fill up with spaces until FIELD_WIDTH reached. */
9670 while (field_width > 0
9671 && n < field_width)
9672 {
9673 store_mode_line_noprop_char (' ');
9674 ++n;
9675 }
9676
9677 return n;
9678 }
9679
9680 /***********************************************************************
9681 Frame Titles
9682 ***********************************************************************/
9683
9684 #ifdef HAVE_WINDOW_SYSTEM
9685
9686 /* Set the title of FRAME, if it has changed. The title format is
9687 Vicon_title_format if FRAME is iconified, otherwise it is
9688 frame_title_format. */
9689
9690 static void
9691 x_consider_frame_title (frame)
9692 Lisp_Object frame;
9693 {
9694 struct frame *f = XFRAME (frame);
9695
9696 if (FRAME_WINDOW_P (f)
9697 || FRAME_MINIBUF_ONLY_P (f)
9698 || f->explicit_name)
9699 {
9700 /* Do we have more than one visible frame on this X display? */
9701 Lisp_Object tail;
9702 Lisp_Object fmt;
9703 int title_start;
9704 char *title;
9705 int len;
9706 struct it it;
9707 int count = SPECPDL_INDEX ();
9708
9709 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9710 {
9711 Lisp_Object other_frame = XCAR (tail);
9712 struct frame *tf = XFRAME (other_frame);
9713
9714 if (tf != f
9715 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9716 && !FRAME_MINIBUF_ONLY_P (tf)
9717 && !EQ (other_frame, tip_frame)
9718 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9719 break;
9720 }
9721
9722 /* Set global variable indicating that multiple frames exist. */
9723 multiple_frames = CONSP (tail);
9724
9725 /* Switch to the buffer of selected window of the frame. Set up
9726 mode_line_target so that display_mode_element will output into
9727 mode_line_noprop_buf; then display the title. */
9728 record_unwind_protect (unwind_format_mode_line,
9729 format_mode_line_unwind_data
9730 (current_buffer, selected_window, 0));
9731
9732 Fselect_window (f->selected_window, Qt);
9733 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9734 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9735
9736 mode_line_target = MODE_LINE_TITLE;
9737 title_start = MODE_LINE_NOPROP_LEN (0);
9738 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9739 NULL, DEFAULT_FACE_ID);
9740 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9741 len = MODE_LINE_NOPROP_LEN (title_start);
9742 title = mode_line_noprop_buf + title_start;
9743 unbind_to (count, Qnil);
9744
9745 /* Set the title only if it's changed. This avoids consing in
9746 the common case where it hasn't. (If it turns out that we've
9747 already wasted too much time by walking through the list with
9748 display_mode_element, then we might need to optimize at a
9749 higher level than this.) */
9750 if (! STRINGP (f->name)
9751 || SBYTES (f->name) != len
9752 || bcmp (title, SDATA (f->name), len) != 0)
9753 {
9754 #ifdef HAVE_NS
9755 if (FRAME_NS_P (f))
9756 {
9757 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9758 {
9759 if (EQ (fmt, Qt))
9760 ns_set_name_as_filename (f);
9761 else
9762 x_implicitly_set_name (f, make_string(title, len),
9763 Qnil);
9764 }
9765 }
9766 else
9767 #endif
9768 x_implicitly_set_name (f, make_string (title, len), Qnil);
9769 }
9770 #ifdef HAVE_NS
9771 if (FRAME_NS_P (f))
9772 {
9773 /* do this also for frames with explicit names */
9774 ns_implicitly_set_icon_type(f);
9775 ns_set_doc_edited(f, Fbuffer_modified_p
9776 (XWINDOW (f->selected_window)->buffer), Qnil);
9777 }
9778 #endif
9779 }
9780 }
9781
9782 #endif /* not HAVE_WINDOW_SYSTEM */
9783
9784
9785
9786 \f
9787 /***********************************************************************
9788 Menu Bars
9789 ***********************************************************************/
9790
9791
9792 /* Prepare for redisplay by updating menu-bar item lists when
9793 appropriate. This can call eval. */
9794
9795 void
9796 prepare_menu_bars ()
9797 {
9798 int all_windows;
9799 struct gcpro gcpro1, gcpro2;
9800 struct frame *f;
9801 Lisp_Object tooltip_frame;
9802
9803 #ifdef HAVE_WINDOW_SYSTEM
9804 tooltip_frame = tip_frame;
9805 #else
9806 tooltip_frame = Qnil;
9807 #endif
9808
9809 /* Update all frame titles based on their buffer names, etc. We do
9810 this before the menu bars so that the buffer-menu will show the
9811 up-to-date frame titles. */
9812 #ifdef HAVE_WINDOW_SYSTEM
9813 if (windows_or_buffers_changed || update_mode_lines)
9814 {
9815 Lisp_Object tail, frame;
9816
9817 FOR_EACH_FRAME (tail, frame)
9818 {
9819 f = XFRAME (frame);
9820 if (!EQ (frame, tooltip_frame)
9821 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9822 x_consider_frame_title (frame);
9823 }
9824 }
9825 #endif /* HAVE_WINDOW_SYSTEM */
9826
9827 /* Update the menu bar item lists, if appropriate. This has to be
9828 done before any actual redisplay or generation of display lines. */
9829 all_windows = (update_mode_lines
9830 || buffer_shared > 1
9831 || windows_or_buffers_changed);
9832 if (all_windows)
9833 {
9834 Lisp_Object tail, frame;
9835 int count = SPECPDL_INDEX ();
9836 /* 1 means that update_menu_bar has run its hooks
9837 so any further calls to update_menu_bar shouldn't do so again. */
9838 int menu_bar_hooks_run = 0;
9839
9840 record_unwind_save_match_data ();
9841
9842 FOR_EACH_FRAME (tail, frame)
9843 {
9844 f = XFRAME (frame);
9845
9846 /* Ignore tooltip frame. */
9847 if (EQ (frame, tooltip_frame))
9848 continue;
9849
9850 /* If a window on this frame changed size, report that to
9851 the user and clear the size-change flag. */
9852 if (FRAME_WINDOW_SIZES_CHANGED (f))
9853 {
9854 Lisp_Object functions;
9855
9856 /* Clear flag first in case we get an error below. */
9857 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9858 functions = Vwindow_size_change_functions;
9859 GCPRO2 (tail, functions);
9860
9861 while (CONSP (functions))
9862 {
9863 if (!EQ (XCAR (functions), Qt))
9864 call1 (XCAR (functions), frame);
9865 functions = XCDR (functions);
9866 }
9867 UNGCPRO;
9868 }
9869
9870 GCPRO1 (tail);
9871 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9872 #ifdef HAVE_WINDOW_SYSTEM
9873 update_tool_bar (f, 0);
9874 #endif
9875 UNGCPRO;
9876 }
9877
9878 unbind_to (count, Qnil);
9879 }
9880 else
9881 {
9882 struct frame *sf = SELECTED_FRAME ();
9883 update_menu_bar (sf, 1, 0);
9884 #ifdef HAVE_WINDOW_SYSTEM
9885 update_tool_bar (sf, 1);
9886 #endif
9887 }
9888
9889 /* Motif needs this. See comment in xmenu.c. Turn it off when
9890 pending_menu_activation is not defined. */
9891 #ifdef USE_X_TOOLKIT
9892 pending_menu_activation = 0;
9893 #endif
9894 }
9895
9896
9897 /* Update the menu bar item list for frame F. This has to be done
9898 before we start to fill in any display lines, because it can call
9899 eval.
9900
9901 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9902
9903 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9904 already ran the menu bar hooks for this redisplay, so there
9905 is no need to run them again. The return value is the
9906 updated value of this flag, to pass to the next call. */
9907
9908 static int
9909 update_menu_bar (f, save_match_data, hooks_run)
9910 struct frame *f;
9911 int save_match_data;
9912 int hooks_run;
9913 {
9914 Lisp_Object window;
9915 register struct window *w;
9916
9917 /* If called recursively during a menu update, do nothing. This can
9918 happen when, for instance, an activate-menubar-hook causes a
9919 redisplay. */
9920 if (inhibit_menubar_update)
9921 return hooks_run;
9922
9923 window = FRAME_SELECTED_WINDOW (f);
9924 w = XWINDOW (window);
9925
9926 if (FRAME_WINDOW_P (f)
9927 ?
9928 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9929 || defined (HAVE_NS) || defined (USE_GTK)
9930 FRAME_EXTERNAL_MENU_BAR (f)
9931 #else
9932 FRAME_MENU_BAR_LINES (f) > 0
9933 #endif
9934 : FRAME_MENU_BAR_LINES (f) > 0)
9935 {
9936 /* If the user has switched buffers or windows, we need to
9937 recompute to reflect the new bindings. But we'll
9938 recompute when update_mode_lines is set too; that means
9939 that people can use force-mode-line-update to request
9940 that the menu bar be recomputed. The adverse effect on
9941 the rest of the redisplay algorithm is about the same as
9942 windows_or_buffers_changed anyway. */
9943 if (windows_or_buffers_changed
9944 /* This used to test w->update_mode_line, but we believe
9945 there is no need to recompute the menu in that case. */
9946 || update_mode_lines
9947 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9948 < BUF_MODIFF (XBUFFER (w->buffer)))
9949 != !NILP (w->last_had_star))
9950 || ((!NILP (Vtransient_mark_mode)
9951 && !NILP (XBUFFER (w->buffer)->mark_active))
9952 != !NILP (w->region_showing)))
9953 {
9954 struct buffer *prev = current_buffer;
9955 int count = SPECPDL_INDEX ();
9956
9957 specbind (Qinhibit_menubar_update, Qt);
9958
9959 set_buffer_internal_1 (XBUFFER (w->buffer));
9960 if (save_match_data)
9961 record_unwind_save_match_data ();
9962 if (NILP (Voverriding_local_map_menu_flag))
9963 {
9964 specbind (Qoverriding_terminal_local_map, Qnil);
9965 specbind (Qoverriding_local_map, Qnil);
9966 }
9967
9968 if (!hooks_run)
9969 {
9970 /* Run the Lucid hook. */
9971 safe_run_hooks (Qactivate_menubar_hook);
9972
9973 /* If it has changed current-menubar from previous value,
9974 really recompute the menu-bar from the value. */
9975 if (! NILP (Vlucid_menu_bar_dirty_flag))
9976 call0 (Qrecompute_lucid_menubar);
9977
9978 safe_run_hooks (Qmenu_bar_update_hook);
9979
9980 hooks_run = 1;
9981 }
9982
9983 XSETFRAME (Vmenu_updating_frame, f);
9984 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9985
9986 /* Redisplay the menu bar in case we changed it. */
9987 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9988 || defined (HAVE_NS) || defined (USE_GTK)
9989 if (FRAME_WINDOW_P (f))
9990 {
9991 #if defined (HAVE_NS)
9992 /* All frames on Mac OS share the same menubar. So only
9993 the selected frame should be allowed to set it. */
9994 if (f == SELECTED_FRAME ())
9995 #endif
9996 set_frame_menubar (f, 0, 0);
9997 }
9998 else
9999 /* On a terminal screen, the menu bar is an ordinary screen
10000 line, and this makes it get updated. */
10001 w->update_mode_line = Qt;
10002 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10003 /* In the non-toolkit version, the menu bar is an ordinary screen
10004 line, and this makes it get updated. */
10005 w->update_mode_line = Qt;
10006 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10007
10008 unbind_to (count, Qnil);
10009 set_buffer_internal_1 (prev);
10010 }
10011 }
10012
10013 return hooks_run;
10014 }
10015
10016
10017 \f
10018 /***********************************************************************
10019 Output Cursor
10020 ***********************************************************************/
10021
10022 #ifdef HAVE_WINDOW_SYSTEM
10023
10024 /* EXPORT:
10025 Nominal cursor position -- where to draw output.
10026 HPOS and VPOS are window relative glyph matrix coordinates.
10027 X and Y are window relative pixel coordinates. */
10028
10029 struct cursor_pos output_cursor;
10030
10031
10032 /* EXPORT:
10033 Set the global variable output_cursor to CURSOR. All cursor
10034 positions are relative to updated_window. */
10035
10036 void
10037 set_output_cursor (cursor)
10038 struct cursor_pos *cursor;
10039 {
10040 output_cursor.hpos = cursor->hpos;
10041 output_cursor.vpos = cursor->vpos;
10042 output_cursor.x = cursor->x;
10043 output_cursor.y = cursor->y;
10044 }
10045
10046
10047 /* EXPORT for RIF:
10048 Set a nominal cursor position.
10049
10050 HPOS and VPOS are column/row positions in a window glyph matrix. X
10051 and Y are window text area relative pixel positions.
10052
10053 If this is done during an update, updated_window will contain the
10054 window that is being updated and the position is the future output
10055 cursor position for that window. If updated_window is null, use
10056 selected_window and display the cursor at the given position. */
10057
10058 void
10059 x_cursor_to (vpos, hpos, y, x)
10060 int vpos, hpos, y, x;
10061 {
10062 struct window *w;
10063
10064 /* If updated_window is not set, work on selected_window. */
10065 if (updated_window)
10066 w = updated_window;
10067 else
10068 w = XWINDOW (selected_window);
10069
10070 /* Set the output cursor. */
10071 output_cursor.hpos = hpos;
10072 output_cursor.vpos = vpos;
10073 output_cursor.x = x;
10074 output_cursor.y = y;
10075
10076 /* If not called as part of an update, really display the cursor.
10077 This will also set the cursor position of W. */
10078 if (updated_window == NULL)
10079 {
10080 BLOCK_INPUT;
10081 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10082 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10083 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10084 UNBLOCK_INPUT;
10085 }
10086 }
10087
10088 #endif /* HAVE_WINDOW_SYSTEM */
10089
10090 \f
10091 /***********************************************************************
10092 Tool-bars
10093 ***********************************************************************/
10094
10095 #ifdef HAVE_WINDOW_SYSTEM
10096
10097 /* Where the mouse was last time we reported a mouse event. */
10098
10099 FRAME_PTR last_mouse_frame;
10100
10101 /* Tool-bar item index of the item on which a mouse button was pressed
10102 or -1. */
10103
10104 int last_tool_bar_item;
10105
10106
10107 static Lisp_Object
10108 update_tool_bar_unwind (frame)
10109 Lisp_Object frame;
10110 {
10111 selected_frame = frame;
10112 return Qnil;
10113 }
10114
10115 /* Update the tool-bar item list for frame F. This has to be done
10116 before we start to fill in any display lines. Called from
10117 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10118 and restore it here. */
10119
10120 static void
10121 update_tool_bar (f, save_match_data)
10122 struct frame *f;
10123 int save_match_data;
10124 {
10125 #if defined (USE_GTK) || defined (HAVE_NS)
10126 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10127 #else
10128 int do_update = WINDOWP (f->tool_bar_window)
10129 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10130 #endif
10131
10132 if (do_update)
10133 {
10134 Lisp_Object window;
10135 struct window *w;
10136
10137 window = FRAME_SELECTED_WINDOW (f);
10138 w = XWINDOW (window);
10139
10140 /* If the user has switched buffers or windows, we need to
10141 recompute to reflect the new bindings. But we'll
10142 recompute when update_mode_lines is set too; that means
10143 that people can use force-mode-line-update to request
10144 that the menu bar be recomputed. The adverse effect on
10145 the rest of the redisplay algorithm is about the same as
10146 windows_or_buffers_changed anyway. */
10147 if (windows_or_buffers_changed
10148 || !NILP (w->update_mode_line)
10149 || update_mode_lines
10150 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10151 < BUF_MODIFF (XBUFFER (w->buffer)))
10152 != !NILP (w->last_had_star))
10153 || ((!NILP (Vtransient_mark_mode)
10154 && !NILP (XBUFFER (w->buffer)->mark_active))
10155 != !NILP (w->region_showing)))
10156 {
10157 struct buffer *prev = current_buffer;
10158 int count = SPECPDL_INDEX ();
10159 Lisp_Object frame, new_tool_bar;
10160 int new_n_tool_bar;
10161 struct gcpro gcpro1;
10162
10163 /* Set current_buffer to the buffer of the selected
10164 window of the frame, so that we get the right local
10165 keymaps. */
10166 set_buffer_internal_1 (XBUFFER (w->buffer));
10167
10168 /* Save match data, if we must. */
10169 if (save_match_data)
10170 record_unwind_save_match_data ();
10171
10172 /* Make sure that we don't accidentally use bogus keymaps. */
10173 if (NILP (Voverriding_local_map_menu_flag))
10174 {
10175 specbind (Qoverriding_terminal_local_map, Qnil);
10176 specbind (Qoverriding_local_map, Qnil);
10177 }
10178
10179 GCPRO1 (new_tool_bar);
10180
10181 /* We must temporarily set the selected frame to this frame
10182 before calling tool_bar_items, because the calculation of
10183 the tool-bar keymap uses the selected frame (see
10184 `tool-bar-make-keymap' in tool-bar.el). */
10185 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10186 XSETFRAME (frame, f);
10187 selected_frame = frame;
10188
10189 /* Build desired tool-bar items from keymaps. */
10190 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10191 &new_n_tool_bar);
10192
10193 /* Redisplay the tool-bar if we changed it. */
10194 if (new_n_tool_bar != f->n_tool_bar_items
10195 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10196 {
10197 /* Redisplay that happens asynchronously due to an expose event
10198 may access f->tool_bar_items. Make sure we update both
10199 variables within BLOCK_INPUT so no such event interrupts. */
10200 BLOCK_INPUT;
10201 f->tool_bar_items = new_tool_bar;
10202 f->n_tool_bar_items = new_n_tool_bar;
10203 w->update_mode_line = Qt;
10204 UNBLOCK_INPUT;
10205 }
10206
10207 UNGCPRO;
10208
10209 unbind_to (count, Qnil);
10210 set_buffer_internal_1 (prev);
10211 }
10212 }
10213 }
10214
10215
10216 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10217 F's desired tool-bar contents. F->tool_bar_items must have
10218 been set up previously by calling prepare_menu_bars. */
10219
10220 static void
10221 build_desired_tool_bar_string (f)
10222 struct frame *f;
10223 {
10224 int i, size, size_needed;
10225 struct gcpro gcpro1, gcpro2, gcpro3;
10226 Lisp_Object image, plist, props;
10227
10228 image = plist = props = Qnil;
10229 GCPRO3 (image, plist, props);
10230
10231 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10232 Otherwise, make a new string. */
10233
10234 /* The size of the string we might be able to reuse. */
10235 size = (STRINGP (f->desired_tool_bar_string)
10236 ? SCHARS (f->desired_tool_bar_string)
10237 : 0);
10238
10239 /* We need one space in the string for each image. */
10240 size_needed = f->n_tool_bar_items;
10241
10242 /* Reuse f->desired_tool_bar_string, if possible. */
10243 if (size < size_needed || NILP (f->desired_tool_bar_string))
10244 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10245 make_number (' '));
10246 else
10247 {
10248 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10249 Fremove_text_properties (make_number (0), make_number (size),
10250 props, f->desired_tool_bar_string);
10251 }
10252
10253 /* Put a `display' property on the string for the images to display,
10254 put a `menu_item' property on tool-bar items with a value that
10255 is the index of the item in F's tool-bar item vector. */
10256 for (i = 0; i < f->n_tool_bar_items; ++i)
10257 {
10258 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10259
10260 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10261 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10262 int hmargin, vmargin, relief, idx, end;
10263 extern Lisp_Object QCrelief, QCmargin, QCconversion;
10264
10265 /* If image is a vector, choose the image according to the
10266 button state. */
10267 image = PROP (TOOL_BAR_ITEM_IMAGES);
10268 if (VECTORP (image))
10269 {
10270 if (enabled_p)
10271 idx = (selected_p
10272 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10273 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10274 else
10275 idx = (selected_p
10276 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10277 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10278
10279 xassert (ASIZE (image) >= idx);
10280 image = AREF (image, idx);
10281 }
10282 else
10283 idx = -1;
10284
10285 /* Ignore invalid image specifications. */
10286 if (!valid_image_p (image))
10287 continue;
10288
10289 /* Display the tool-bar button pressed, or depressed. */
10290 plist = Fcopy_sequence (XCDR (image));
10291
10292 /* Compute margin and relief to draw. */
10293 relief = (tool_bar_button_relief >= 0
10294 ? tool_bar_button_relief
10295 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10296 hmargin = vmargin = relief;
10297
10298 if (INTEGERP (Vtool_bar_button_margin)
10299 && XINT (Vtool_bar_button_margin) > 0)
10300 {
10301 hmargin += XFASTINT (Vtool_bar_button_margin);
10302 vmargin += XFASTINT (Vtool_bar_button_margin);
10303 }
10304 else if (CONSP (Vtool_bar_button_margin))
10305 {
10306 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10307 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10308 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10309
10310 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10311 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10312 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10313 }
10314
10315 if (auto_raise_tool_bar_buttons_p)
10316 {
10317 /* Add a `:relief' property to the image spec if the item is
10318 selected. */
10319 if (selected_p)
10320 {
10321 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10322 hmargin -= relief;
10323 vmargin -= relief;
10324 }
10325 }
10326 else
10327 {
10328 /* If image is selected, display it pressed, i.e. with a
10329 negative relief. If it's not selected, display it with a
10330 raised relief. */
10331 plist = Fplist_put (plist, QCrelief,
10332 (selected_p
10333 ? make_number (-relief)
10334 : make_number (relief)));
10335 hmargin -= relief;
10336 vmargin -= relief;
10337 }
10338
10339 /* Put a margin around the image. */
10340 if (hmargin || vmargin)
10341 {
10342 if (hmargin == vmargin)
10343 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10344 else
10345 plist = Fplist_put (plist, QCmargin,
10346 Fcons (make_number (hmargin),
10347 make_number (vmargin)));
10348 }
10349
10350 /* If button is not enabled, and we don't have special images
10351 for the disabled state, make the image appear disabled by
10352 applying an appropriate algorithm to it. */
10353 if (!enabled_p && idx < 0)
10354 plist = Fplist_put (plist, QCconversion, Qdisabled);
10355
10356 /* Put a `display' text property on the string for the image to
10357 display. Put a `menu-item' property on the string that gives
10358 the start of this item's properties in the tool-bar items
10359 vector. */
10360 image = Fcons (Qimage, plist);
10361 props = list4 (Qdisplay, image,
10362 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10363
10364 /* Let the last image hide all remaining spaces in the tool bar
10365 string. The string can be longer than needed when we reuse a
10366 previous string. */
10367 if (i + 1 == f->n_tool_bar_items)
10368 end = SCHARS (f->desired_tool_bar_string);
10369 else
10370 end = i + 1;
10371 Fadd_text_properties (make_number (i), make_number (end),
10372 props, f->desired_tool_bar_string);
10373 #undef PROP
10374 }
10375
10376 UNGCPRO;
10377 }
10378
10379
10380 /* Display one line of the tool-bar of frame IT->f.
10381
10382 HEIGHT specifies the desired height of the tool-bar line.
10383 If the actual height of the glyph row is less than HEIGHT, the
10384 row's height is increased to HEIGHT, and the icons are centered
10385 vertically in the new height.
10386
10387 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10388 count a final empty row in case the tool-bar width exactly matches
10389 the window width.
10390 */
10391
10392 static void
10393 display_tool_bar_line (it, height)
10394 struct it *it;
10395 int height;
10396 {
10397 struct glyph_row *row = it->glyph_row;
10398 int max_x = it->last_visible_x;
10399 struct glyph *last;
10400
10401 prepare_desired_row (row);
10402 row->y = it->current_y;
10403
10404 /* Note that this isn't made use of if the face hasn't a box,
10405 so there's no need to check the face here. */
10406 it->start_of_box_run_p = 1;
10407
10408 while (it->current_x < max_x)
10409 {
10410 int x, n_glyphs_before, i, nglyphs;
10411 struct it it_before;
10412
10413 /* Get the next display element. */
10414 if (!get_next_display_element (it))
10415 {
10416 /* Don't count empty row if we are counting needed tool-bar lines. */
10417 if (height < 0 && !it->hpos)
10418 return;
10419 break;
10420 }
10421
10422 /* Produce glyphs. */
10423 n_glyphs_before = row->used[TEXT_AREA];
10424 it_before = *it;
10425
10426 PRODUCE_GLYPHS (it);
10427
10428 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10429 i = 0;
10430 x = it_before.current_x;
10431 while (i < nglyphs)
10432 {
10433 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10434
10435 if (x + glyph->pixel_width > max_x)
10436 {
10437 /* Glyph doesn't fit on line. Backtrack. */
10438 row->used[TEXT_AREA] = n_glyphs_before;
10439 *it = it_before;
10440 /* If this is the only glyph on this line, it will never fit on the
10441 toolbar, so skip it. But ensure there is at least one glyph,
10442 so we don't accidentally disable the tool-bar. */
10443 if (n_glyphs_before == 0
10444 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10445 break;
10446 goto out;
10447 }
10448
10449 ++it->hpos;
10450 x += glyph->pixel_width;
10451 ++i;
10452 }
10453
10454 /* Stop at line ends. */
10455 if (ITERATOR_AT_END_OF_LINE_P (it))
10456 break;
10457
10458 set_iterator_to_next (it, 1);
10459 }
10460
10461 out:;
10462
10463 row->displays_text_p = row->used[TEXT_AREA] != 0;
10464
10465 /* Use default face for the border below the tool bar.
10466
10467 FIXME: When auto-resize-tool-bars is grow-only, there is
10468 no additional border below the possibly empty tool-bar lines.
10469 So to make the extra empty lines look "normal", we have to
10470 use the tool-bar face for the border too. */
10471 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10472 it->face_id = DEFAULT_FACE_ID;
10473
10474 extend_face_to_end_of_line (it);
10475 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10476 last->right_box_line_p = 1;
10477 if (last == row->glyphs[TEXT_AREA])
10478 last->left_box_line_p = 1;
10479
10480 /* Make line the desired height and center it vertically. */
10481 if ((height -= it->max_ascent + it->max_descent) > 0)
10482 {
10483 /* Don't add more than one line height. */
10484 height %= FRAME_LINE_HEIGHT (it->f);
10485 it->max_ascent += height / 2;
10486 it->max_descent += (height + 1) / 2;
10487 }
10488
10489 compute_line_metrics (it);
10490
10491 /* If line is empty, make it occupy the rest of the tool-bar. */
10492 if (!row->displays_text_p)
10493 {
10494 row->height = row->phys_height = it->last_visible_y - row->y;
10495 row->visible_height = row->height;
10496 row->ascent = row->phys_ascent = 0;
10497 row->extra_line_spacing = 0;
10498 }
10499
10500 row->full_width_p = 1;
10501 row->continued_p = 0;
10502 row->truncated_on_left_p = 0;
10503 row->truncated_on_right_p = 0;
10504
10505 it->current_x = it->hpos = 0;
10506 it->current_y += row->height;
10507 ++it->vpos;
10508 ++it->glyph_row;
10509 }
10510
10511
10512 /* Max tool-bar height. */
10513
10514 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10515 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10516
10517 /* Value is the number of screen lines needed to make all tool-bar
10518 items of frame F visible. The number of actual rows needed is
10519 returned in *N_ROWS if non-NULL. */
10520
10521 static int
10522 tool_bar_lines_needed (f, n_rows)
10523 struct frame *f;
10524 int *n_rows;
10525 {
10526 struct window *w = XWINDOW (f->tool_bar_window);
10527 struct it it;
10528 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10529 the desired matrix, so use (unused) mode-line row as temporary row to
10530 avoid destroying the first tool-bar row. */
10531 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10532
10533 /* Initialize an iterator for iteration over
10534 F->desired_tool_bar_string in the tool-bar window of frame F. */
10535 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10536 it.first_visible_x = 0;
10537 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10538 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10539
10540 while (!ITERATOR_AT_END_P (&it))
10541 {
10542 clear_glyph_row (temp_row);
10543 it.glyph_row = temp_row;
10544 display_tool_bar_line (&it, -1);
10545 }
10546 clear_glyph_row (temp_row);
10547
10548 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10549 if (n_rows)
10550 *n_rows = it.vpos > 0 ? it.vpos : -1;
10551
10552 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10553 }
10554
10555
10556 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10557 0, 1, 0,
10558 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10559 (frame)
10560 Lisp_Object frame;
10561 {
10562 struct frame *f;
10563 struct window *w;
10564 int nlines = 0;
10565
10566 if (NILP (frame))
10567 frame = selected_frame;
10568 else
10569 CHECK_FRAME (frame);
10570 f = XFRAME (frame);
10571
10572 if (WINDOWP (f->tool_bar_window)
10573 || (w = XWINDOW (f->tool_bar_window),
10574 WINDOW_TOTAL_LINES (w) > 0))
10575 {
10576 update_tool_bar (f, 1);
10577 if (f->n_tool_bar_items)
10578 {
10579 build_desired_tool_bar_string (f);
10580 nlines = tool_bar_lines_needed (f, NULL);
10581 }
10582 }
10583
10584 return make_number (nlines);
10585 }
10586
10587
10588 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10589 height should be changed. */
10590
10591 static int
10592 redisplay_tool_bar (f)
10593 struct frame *f;
10594 {
10595 struct window *w;
10596 struct it it;
10597 struct glyph_row *row;
10598
10599 #if defined (USE_GTK) || defined (HAVE_NS)
10600 if (FRAME_EXTERNAL_TOOL_BAR (f))
10601 update_frame_tool_bar (f);
10602 return 0;
10603 #endif
10604
10605 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10606 do anything. This means you must start with tool-bar-lines
10607 non-zero to get the auto-sizing effect. Or in other words, you
10608 can turn off tool-bars by specifying tool-bar-lines zero. */
10609 if (!WINDOWP (f->tool_bar_window)
10610 || (w = XWINDOW (f->tool_bar_window),
10611 WINDOW_TOTAL_LINES (w) == 0))
10612 return 0;
10613
10614 /* Set up an iterator for the tool-bar window. */
10615 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10616 it.first_visible_x = 0;
10617 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10618 row = it.glyph_row;
10619
10620 /* Build a string that represents the contents of the tool-bar. */
10621 build_desired_tool_bar_string (f);
10622 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10623
10624 if (f->n_tool_bar_rows == 0)
10625 {
10626 int nlines;
10627
10628 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10629 nlines != WINDOW_TOTAL_LINES (w)))
10630 {
10631 extern Lisp_Object Qtool_bar_lines;
10632 Lisp_Object frame;
10633 int old_height = WINDOW_TOTAL_LINES (w);
10634
10635 XSETFRAME (frame, f);
10636 Fmodify_frame_parameters (frame,
10637 Fcons (Fcons (Qtool_bar_lines,
10638 make_number (nlines)),
10639 Qnil));
10640 if (WINDOW_TOTAL_LINES (w) != old_height)
10641 {
10642 clear_glyph_matrix (w->desired_matrix);
10643 fonts_changed_p = 1;
10644 return 1;
10645 }
10646 }
10647 }
10648
10649 /* Display as many lines as needed to display all tool-bar items. */
10650
10651 if (f->n_tool_bar_rows > 0)
10652 {
10653 int border, rows, height, extra;
10654
10655 if (INTEGERP (Vtool_bar_border))
10656 border = XINT (Vtool_bar_border);
10657 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10658 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10659 else if (EQ (Vtool_bar_border, Qborder_width))
10660 border = f->border_width;
10661 else
10662 border = 0;
10663 if (border < 0)
10664 border = 0;
10665
10666 rows = f->n_tool_bar_rows;
10667 height = max (1, (it.last_visible_y - border) / rows);
10668 extra = it.last_visible_y - border - height * rows;
10669
10670 while (it.current_y < it.last_visible_y)
10671 {
10672 int h = 0;
10673 if (extra > 0 && rows-- > 0)
10674 {
10675 h = (extra + rows - 1) / rows;
10676 extra -= h;
10677 }
10678 display_tool_bar_line (&it, height + h);
10679 }
10680 }
10681 else
10682 {
10683 while (it.current_y < it.last_visible_y)
10684 display_tool_bar_line (&it, 0);
10685 }
10686
10687 /* It doesn't make much sense to try scrolling in the tool-bar
10688 window, so don't do it. */
10689 w->desired_matrix->no_scrolling_p = 1;
10690 w->must_be_updated_p = 1;
10691
10692 if (!NILP (Vauto_resize_tool_bars))
10693 {
10694 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10695 int change_height_p = 0;
10696
10697 /* If we couldn't display everything, change the tool-bar's
10698 height if there is room for more. */
10699 if (IT_STRING_CHARPOS (it) < it.end_charpos
10700 && it.current_y < max_tool_bar_height)
10701 change_height_p = 1;
10702
10703 row = it.glyph_row - 1;
10704
10705 /* If there are blank lines at the end, except for a partially
10706 visible blank line at the end that is smaller than
10707 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10708 if (!row->displays_text_p
10709 && row->height >= FRAME_LINE_HEIGHT (f))
10710 change_height_p = 1;
10711
10712 /* If row displays tool-bar items, but is partially visible,
10713 change the tool-bar's height. */
10714 if (row->displays_text_p
10715 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10716 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10717 change_height_p = 1;
10718
10719 /* Resize windows as needed by changing the `tool-bar-lines'
10720 frame parameter. */
10721 if (change_height_p)
10722 {
10723 extern Lisp_Object Qtool_bar_lines;
10724 Lisp_Object frame;
10725 int old_height = WINDOW_TOTAL_LINES (w);
10726 int nrows;
10727 int nlines = tool_bar_lines_needed (f, &nrows);
10728
10729 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10730 && !f->minimize_tool_bar_window_p)
10731 ? (nlines > old_height)
10732 : (nlines != old_height));
10733 f->minimize_tool_bar_window_p = 0;
10734
10735 if (change_height_p)
10736 {
10737 XSETFRAME (frame, f);
10738 Fmodify_frame_parameters (frame,
10739 Fcons (Fcons (Qtool_bar_lines,
10740 make_number (nlines)),
10741 Qnil));
10742 if (WINDOW_TOTAL_LINES (w) != old_height)
10743 {
10744 clear_glyph_matrix (w->desired_matrix);
10745 f->n_tool_bar_rows = nrows;
10746 fonts_changed_p = 1;
10747 return 1;
10748 }
10749 }
10750 }
10751 }
10752
10753 f->minimize_tool_bar_window_p = 0;
10754 return 0;
10755 }
10756
10757
10758 /* Get information about the tool-bar item which is displayed in GLYPH
10759 on frame F. Return in *PROP_IDX the index where tool-bar item
10760 properties start in F->tool_bar_items. Value is zero if
10761 GLYPH doesn't display a tool-bar item. */
10762
10763 static int
10764 tool_bar_item_info (f, glyph, prop_idx)
10765 struct frame *f;
10766 struct glyph *glyph;
10767 int *prop_idx;
10768 {
10769 Lisp_Object prop;
10770 int success_p;
10771 int charpos;
10772
10773 /* This function can be called asynchronously, which means we must
10774 exclude any possibility that Fget_text_property signals an
10775 error. */
10776 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10777 charpos = max (0, charpos);
10778
10779 /* Get the text property `menu-item' at pos. The value of that
10780 property is the start index of this item's properties in
10781 F->tool_bar_items. */
10782 prop = Fget_text_property (make_number (charpos),
10783 Qmenu_item, f->current_tool_bar_string);
10784 if (INTEGERP (prop))
10785 {
10786 *prop_idx = XINT (prop);
10787 success_p = 1;
10788 }
10789 else
10790 success_p = 0;
10791
10792 return success_p;
10793 }
10794
10795 \f
10796 /* Get information about the tool-bar item at position X/Y on frame F.
10797 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10798 the current matrix of the tool-bar window of F, or NULL if not
10799 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10800 item in F->tool_bar_items. Value is
10801
10802 -1 if X/Y is not on a tool-bar item
10803 0 if X/Y is on the same item that was highlighted before.
10804 1 otherwise. */
10805
10806 static int
10807 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10808 struct frame *f;
10809 int x, y;
10810 struct glyph **glyph;
10811 int *hpos, *vpos, *prop_idx;
10812 {
10813 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10814 struct window *w = XWINDOW (f->tool_bar_window);
10815 int area;
10816
10817 /* Find the glyph under X/Y. */
10818 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10819 if (*glyph == NULL)
10820 return -1;
10821
10822 /* Get the start of this tool-bar item's properties in
10823 f->tool_bar_items. */
10824 if (!tool_bar_item_info (f, *glyph, prop_idx))
10825 return -1;
10826
10827 /* Is mouse on the highlighted item? */
10828 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10829 && *vpos >= dpyinfo->mouse_face_beg_row
10830 && *vpos <= dpyinfo->mouse_face_end_row
10831 && (*vpos > dpyinfo->mouse_face_beg_row
10832 || *hpos >= dpyinfo->mouse_face_beg_col)
10833 && (*vpos < dpyinfo->mouse_face_end_row
10834 || *hpos < dpyinfo->mouse_face_end_col
10835 || dpyinfo->mouse_face_past_end))
10836 return 0;
10837
10838 return 1;
10839 }
10840
10841
10842 /* EXPORT:
10843 Handle mouse button event on the tool-bar of frame F, at
10844 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10845 0 for button release. MODIFIERS is event modifiers for button
10846 release. */
10847
10848 void
10849 handle_tool_bar_click (f, x, y, down_p, modifiers)
10850 struct frame *f;
10851 int x, y, down_p;
10852 unsigned int modifiers;
10853 {
10854 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10855 struct window *w = XWINDOW (f->tool_bar_window);
10856 int hpos, vpos, prop_idx;
10857 struct glyph *glyph;
10858 Lisp_Object enabled_p;
10859
10860 /* If not on the highlighted tool-bar item, return. */
10861 frame_to_window_pixel_xy (w, &x, &y);
10862 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10863 return;
10864
10865 /* If item is disabled, do nothing. */
10866 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10867 if (NILP (enabled_p))
10868 return;
10869
10870 if (down_p)
10871 {
10872 /* Show item in pressed state. */
10873 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10874 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10875 last_tool_bar_item = prop_idx;
10876 }
10877 else
10878 {
10879 Lisp_Object key, frame;
10880 struct input_event event;
10881 EVENT_INIT (event);
10882
10883 /* Show item in released state. */
10884 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10885 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10886
10887 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10888
10889 XSETFRAME (frame, f);
10890 event.kind = TOOL_BAR_EVENT;
10891 event.frame_or_window = frame;
10892 event.arg = frame;
10893 kbd_buffer_store_event (&event);
10894
10895 event.kind = TOOL_BAR_EVENT;
10896 event.frame_or_window = frame;
10897 event.arg = key;
10898 event.modifiers = modifiers;
10899 kbd_buffer_store_event (&event);
10900 last_tool_bar_item = -1;
10901 }
10902 }
10903
10904
10905 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10906 tool-bar window-relative coordinates X/Y. Called from
10907 note_mouse_highlight. */
10908
10909 static void
10910 note_tool_bar_highlight (f, x, y)
10911 struct frame *f;
10912 int x, y;
10913 {
10914 Lisp_Object window = f->tool_bar_window;
10915 struct window *w = XWINDOW (window);
10916 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10917 int hpos, vpos;
10918 struct glyph *glyph;
10919 struct glyph_row *row;
10920 int i;
10921 Lisp_Object enabled_p;
10922 int prop_idx;
10923 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10924 int mouse_down_p, rc;
10925
10926 /* Function note_mouse_highlight is called with negative x(y
10927 values when mouse moves outside of the frame. */
10928 if (x <= 0 || y <= 0)
10929 {
10930 clear_mouse_face (dpyinfo);
10931 return;
10932 }
10933
10934 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10935 if (rc < 0)
10936 {
10937 /* Not on tool-bar item. */
10938 clear_mouse_face (dpyinfo);
10939 return;
10940 }
10941 else if (rc == 0)
10942 /* On same tool-bar item as before. */
10943 goto set_help_echo;
10944
10945 clear_mouse_face (dpyinfo);
10946
10947 /* Mouse is down, but on different tool-bar item? */
10948 mouse_down_p = (dpyinfo->grabbed
10949 && f == last_mouse_frame
10950 && FRAME_LIVE_P (f));
10951 if (mouse_down_p
10952 && last_tool_bar_item != prop_idx)
10953 return;
10954
10955 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10956 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10957
10958 /* If tool-bar item is not enabled, don't highlight it. */
10959 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10960 if (!NILP (enabled_p))
10961 {
10962 /* Compute the x-position of the glyph. In front and past the
10963 image is a space. We include this in the highlighted area. */
10964 row = MATRIX_ROW (w->current_matrix, vpos);
10965 for (i = x = 0; i < hpos; ++i)
10966 x += row->glyphs[TEXT_AREA][i].pixel_width;
10967
10968 /* Record this as the current active region. */
10969 dpyinfo->mouse_face_beg_col = hpos;
10970 dpyinfo->mouse_face_beg_row = vpos;
10971 dpyinfo->mouse_face_beg_x = x;
10972 dpyinfo->mouse_face_beg_y = row->y;
10973 dpyinfo->mouse_face_past_end = 0;
10974
10975 dpyinfo->mouse_face_end_col = hpos + 1;
10976 dpyinfo->mouse_face_end_row = vpos;
10977 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10978 dpyinfo->mouse_face_end_y = row->y;
10979 dpyinfo->mouse_face_window = window;
10980 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10981
10982 /* Display it as active. */
10983 show_mouse_face (dpyinfo, draw);
10984 dpyinfo->mouse_face_image_state = draw;
10985 }
10986
10987 set_help_echo:
10988
10989 /* Set help_echo_string to a help string to display for this tool-bar item.
10990 XTread_socket does the rest. */
10991 help_echo_object = help_echo_window = Qnil;
10992 help_echo_pos = -1;
10993 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10994 if (NILP (help_echo_string))
10995 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10996 }
10997
10998 #endif /* HAVE_WINDOW_SYSTEM */
10999
11000
11001 \f
11002 /************************************************************************
11003 Horizontal scrolling
11004 ************************************************************************/
11005
11006 static int hscroll_window_tree P_ ((Lisp_Object));
11007 static int hscroll_windows P_ ((Lisp_Object));
11008
11009 /* For all leaf windows in the window tree rooted at WINDOW, set their
11010 hscroll value so that PT is (i) visible in the window, and (ii) so
11011 that it is not within a certain margin at the window's left and
11012 right border. Value is non-zero if any window's hscroll has been
11013 changed. */
11014
11015 static int
11016 hscroll_window_tree (window)
11017 Lisp_Object window;
11018 {
11019 int hscrolled_p = 0;
11020 int hscroll_relative_p = FLOATP (Vhscroll_step);
11021 int hscroll_step_abs = 0;
11022 double hscroll_step_rel = 0;
11023
11024 if (hscroll_relative_p)
11025 {
11026 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11027 if (hscroll_step_rel < 0)
11028 {
11029 hscroll_relative_p = 0;
11030 hscroll_step_abs = 0;
11031 }
11032 }
11033 else if (INTEGERP (Vhscroll_step))
11034 {
11035 hscroll_step_abs = XINT (Vhscroll_step);
11036 if (hscroll_step_abs < 0)
11037 hscroll_step_abs = 0;
11038 }
11039 else
11040 hscroll_step_abs = 0;
11041
11042 while (WINDOWP (window))
11043 {
11044 struct window *w = XWINDOW (window);
11045
11046 if (WINDOWP (w->hchild))
11047 hscrolled_p |= hscroll_window_tree (w->hchild);
11048 else if (WINDOWP (w->vchild))
11049 hscrolled_p |= hscroll_window_tree (w->vchild);
11050 else if (w->cursor.vpos >= 0)
11051 {
11052 int h_margin;
11053 int text_area_width;
11054 struct glyph_row *current_cursor_row
11055 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11056 struct glyph_row *desired_cursor_row
11057 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11058 struct glyph_row *cursor_row
11059 = (desired_cursor_row->enabled_p
11060 ? desired_cursor_row
11061 : current_cursor_row);
11062
11063 text_area_width = window_box_width (w, TEXT_AREA);
11064
11065 /* Scroll when cursor is inside this scroll margin. */
11066 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11067
11068 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11069 && ((XFASTINT (w->hscroll)
11070 && w->cursor.x <= h_margin)
11071 || (cursor_row->enabled_p
11072 && cursor_row->truncated_on_right_p
11073 && (w->cursor.x >= text_area_width - h_margin))))
11074 {
11075 struct it it;
11076 int hscroll;
11077 struct buffer *saved_current_buffer;
11078 int pt;
11079 int wanted_x;
11080
11081 /* Find point in a display of infinite width. */
11082 saved_current_buffer = current_buffer;
11083 current_buffer = XBUFFER (w->buffer);
11084
11085 if (w == XWINDOW (selected_window))
11086 pt = BUF_PT (current_buffer);
11087 else
11088 {
11089 pt = marker_position (w->pointm);
11090 pt = max (BEGV, pt);
11091 pt = min (ZV, pt);
11092 }
11093
11094 /* Move iterator to pt starting at cursor_row->start in
11095 a line with infinite width. */
11096 init_to_row_start (&it, w, cursor_row);
11097 it.last_visible_x = INFINITY;
11098 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11099 current_buffer = saved_current_buffer;
11100
11101 /* Position cursor in window. */
11102 if (!hscroll_relative_p && hscroll_step_abs == 0)
11103 hscroll = max (0, (it.current_x
11104 - (ITERATOR_AT_END_OF_LINE_P (&it)
11105 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11106 : (text_area_width / 2))))
11107 / FRAME_COLUMN_WIDTH (it.f);
11108 else if (w->cursor.x >= text_area_width - h_margin)
11109 {
11110 if (hscroll_relative_p)
11111 wanted_x = text_area_width * (1 - hscroll_step_rel)
11112 - h_margin;
11113 else
11114 wanted_x = text_area_width
11115 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11116 - h_margin;
11117 hscroll
11118 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11119 }
11120 else
11121 {
11122 if (hscroll_relative_p)
11123 wanted_x = text_area_width * hscroll_step_rel
11124 + h_margin;
11125 else
11126 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11127 + h_margin;
11128 hscroll
11129 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11130 }
11131 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11132
11133 /* Don't call Fset_window_hscroll if value hasn't
11134 changed because it will prevent redisplay
11135 optimizations. */
11136 if (XFASTINT (w->hscroll) != hscroll)
11137 {
11138 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11139 w->hscroll = make_number (hscroll);
11140 hscrolled_p = 1;
11141 }
11142 }
11143 }
11144
11145 window = w->next;
11146 }
11147
11148 /* Value is non-zero if hscroll of any leaf window has been changed. */
11149 return hscrolled_p;
11150 }
11151
11152
11153 /* Set hscroll so that cursor is visible and not inside horizontal
11154 scroll margins for all windows in the tree rooted at WINDOW. See
11155 also hscroll_window_tree above. Value is non-zero if any window's
11156 hscroll has been changed. If it has, desired matrices on the frame
11157 of WINDOW are cleared. */
11158
11159 static int
11160 hscroll_windows (window)
11161 Lisp_Object window;
11162 {
11163 int hscrolled_p = hscroll_window_tree (window);
11164 if (hscrolled_p)
11165 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11166 return hscrolled_p;
11167 }
11168
11169
11170 \f
11171 /************************************************************************
11172 Redisplay
11173 ************************************************************************/
11174
11175 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11176 to a non-zero value. This is sometimes handy to have in a debugger
11177 session. */
11178
11179 #if GLYPH_DEBUG
11180
11181 /* First and last unchanged row for try_window_id. */
11182
11183 int debug_first_unchanged_at_end_vpos;
11184 int debug_last_unchanged_at_beg_vpos;
11185
11186 /* Delta vpos and y. */
11187
11188 int debug_dvpos, debug_dy;
11189
11190 /* Delta in characters and bytes for try_window_id. */
11191
11192 int debug_delta, debug_delta_bytes;
11193
11194 /* Values of window_end_pos and window_end_vpos at the end of
11195 try_window_id. */
11196
11197 EMACS_INT debug_end_pos, debug_end_vpos;
11198
11199 /* Append a string to W->desired_matrix->method. FMT is a printf
11200 format string. A1...A9 are a supplement for a variable-length
11201 argument list. If trace_redisplay_p is non-zero also printf the
11202 resulting string to stderr. */
11203
11204 static void
11205 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11206 struct window *w;
11207 char *fmt;
11208 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11209 {
11210 char buffer[512];
11211 char *method = w->desired_matrix->method;
11212 int len = strlen (method);
11213 int size = sizeof w->desired_matrix->method;
11214 int remaining = size - len - 1;
11215
11216 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11217 if (len && remaining)
11218 {
11219 method[len] = '|';
11220 --remaining, ++len;
11221 }
11222
11223 strncpy (method + len, buffer, remaining);
11224
11225 if (trace_redisplay_p)
11226 fprintf (stderr, "%p (%s): %s\n",
11227 w,
11228 ((BUFFERP (w->buffer)
11229 && STRINGP (XBUFFER (w->buffer)->name))
11230 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11231 : "no buffer"),
11232 buffer);
11233 }
11234
11235 #endif /* GLYPH_DEBUG */
11236
11237
11238 /* Value is non-zero if all changes in window W, which displays
11239 current_buffer, are in the text between START and END. START is a
11240 buffer position, END is given as a distance from Z. Used in
11241 redisplay_internal for display optimization. */
11242
11243 static INLINE int
11244 text_outside_line_unchanged_p (w, start, end)
11245 struct window *w;
11246 int start, end;
11247 {
11248 int unchanged_p = 1;
11249
11250 /* If text or overlays have changed, see where. */
11251 if (XFASTINT (w->last_modified) < MODIFF
11252 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11253 {
11254 /* Gap in the line? */
11255 if (GPT < start || Z - GPT < end)
11256 unchanged_p = 0;
11257
11258 /* Changes start in front of the line, or end after it? */
11259 if (unchanged_p
11260 && (BEG_UNCHANGED < start - 1
11261 || END_UNCHANGED < end))
11262 unchanged_p = 0;
11263
11264 /* If selective display, can't optimize if changes start at the
11265 beginning of the line. */
11266 if (unchanged_p
11267 && INTEGERP (current_buffer->selective_display)
11268 && XINT (current_buffer->selective_display) > 0
11269 && (BEG_UNCHANGED < start || GPT <= start))
11270 unchanged_p = 0;
11271
11272 /* If there are overlays at the start or end of the line, these
11273 may have overlay strings with newlines in them. A change at
11274 START, for instance, may actually concern the display of such
11275 overlay strings as well, and they are displayed on different
11276 lines. So, quickly rule out this case. (For the future, it
11277 might be desirable to implement something more telling than
11278 just BEG/END_UNCHANGED.) */
11279 if (unchanged_p)
11280 {
11281 if (BEG + BEG_UNCHANGED == start
11282 && overlay_touches_p (start))
11283 unchanged_p = 0;
11284 if (END_UNCHANGED == end
11285 && overlay_touches_p (Z - end))
11286 unchanged_p = 0;
11287 }
11288
11289 /* Under bidi reordering, adding or deleting a character in the
11290 beginning of a paragraph, before the first strong directional
11291 character, can change the base direction of the paragraph (unless
11292 the buffer specifies a fixed paragraph direction), which will
11293 require to redisplay the whole paragraph. It might be worthwhile
11294 to find the paragraph limits and widen the range of redisplayed
11295 lines to that, but for now just give up this optimization. */
11296 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11297 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11298 unchanged_p = 0;
11299 }
11300
11301 return unchanged_p;
11302 }
11303
11304
11305 /* Do a frame update, taking possible shortcuts into account. This is
11306 the main external entry point for redisplay.
11307
11308 If the last redisplay displayed an echo area message and that message
11309 is no longer requested, we clear the echo area or bring back the
11310 mini-buffer if that is in use. */
11311
11312 void
11313 redisplay ()
11314 {
11315 redisplay_internal (0);
11316 }
11317
11318
11319 static Lisp_Object
11320 overlay_arrow_string_or_property (var)
11321 Lisp_Object var;
11322 {
11323 Lisp_Object val;
11324
11325 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11326 return val;
11327
11328 return Voverlay_arrow_string;
11329 }
11330
11331 /* Return 1 if there are any overlay-arrows in current_buffer. */
11332 static int
11333 overlay_arrow_in_current_buffer_p ()
11334 {
11335 Lisp_Object vlist;
11336
11337 for (vlist = Voverlay_arrow_variable_list;
11338 CONSP (vlist);
11339 vlist = XCDR (vlist))
11340 {
11341 Lisp_Object var = XCAR (vlist);
11342 Lisp_Object val;
11343
11344 if (!SYMBOLP (var))
11345 continue;
11346 val = find_symbol_value (var);
11347 if (MARKERP (val)
11348 && current_buffer == XMARKER (val)->buffer)
11349 return 1;
11350 }
11351 return 0;
11352 }
11353
11354
11355 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11356 has changed. */
11357
11358 static int
11359 overlay_arrows_changed_p ()
11360 {
11361 Lisp_Object vlist;
11362
11363 for (vlist = Voverlay_arrow_variable_list;
11364 CONSP (vlist);
11365 vlist = XCDR (vlist))
11366 {
11367 Lisp_Object var = XCAR (vlist);
11368 Lisp_Object val, pstr;
11369
11370 if (!SYMBOLP (var))
11371 continue;
11372 val = find_symbol_value (var);
11373 if (!MARKERP (val))
11374 continue;
11375 if (! EQ (COERCE_MARKER (val),
11376 Fget (var, Qlast_arrow_position))
11377 || ! (pstr = overlay_arrow_string_or_property (var),
11378 EQ (pstr, Fget (var, Qlast_arrow_string))))
11379 return 1;
11380 }
11381 return 0;
11382 }
11383
11384 /* Mark overlay arrows to be updated on next redisplay. */
11385
11386 static void
11387 update_overlay_arrows (up_to_date)
11388 int up_to_date;
11389 {
11390 Lisp_Object vlist;
11391
11392 for (vlist = Voverlay_arrow_variable_list;
11393 CONSP (vlist);
11394 vlist = XCDR (vlist))
11395 {
11396 Lisp_Object var = XCAR (vlist);
11397
11398 if (!SYMBOLP (var))
11399 continue;
11400
11401 if (up_to_date > 0)
11402 {
11403 Lisp_Object val = find_symbol_value (var);
11404 Fput (var, Qlast_arrow_position,
11405 COERCE_MARKER (val));
11406 Fput (var, Qlast_arrow_string,
11407 overlay_arrow_string_or_property (var));
11408 }
11409 else if (up_to_date < 0
11410 || !NILP (Fget (var, Qlast_arrow_position)))
11411 {
11412 Fput (var, Qlast_arrow_position, Qt);
11413 Fput (var, Qlast_arrow_string, Qt);
11414 }
11415 }
11416 }
11417
11418
11419 /* Return overlay arrow string to display at row.
11420 Return integer (bitmap number) for arrow bitmap in left fringe.
11421 Return nil if no overlay arrow. */
11422
11423 static Lisp_Object
11424 overlay_arrow_at_row (it, row)
11425 struct it *it;
11426 struct glyph_row *row;
11427 {
11428 Lisp_Object vlist;
11429
11430 for (vlist = Voverlay_arrow_variable_list;
11431 CONSP (vlist);
11432 vlist = XCDR (vlist))
11433 {
11434 Lisp_Object var = XCAR (vlist);
11435 Lisp_Object val;
11436
11437 if (!SYMBOLP (var))
11438 continue;
11439
11440 val = find_symbol_value (var);
11441
11442 if (MARKERP (val)
11443 && current_buffer == XMARKER (val)->buffer
11444 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11445 {
11446 if (FRAME_WINDOW_P (it->f)
11447 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11448 {
11449 #ifdef HAVE_WINDOW_SYSTEM
11450 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11451 {
11452 int fringe_bitmap;
11453 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11454 return make_number (fringe_bitmap);
11455 }
11456 #endif
11457 return make_number (-1); /* Use default arrow bitmap */
11458 }
11459 return overlay_arrow_string_or_property (var);
11460 }
11461 }
11462
11463 return Qnil;
11464 }
11465
11466 /* Return 1 if point moved out of or into a composition. Otherwise
11467 return 0. PREV_BUF and PREV_PT are the last point buffer and
11468 position. BUF and PT are the current point buffer and position. */
11469
11470 int
11471 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11472 struct buffer *prev_buf, *buf;
11473 int prev_pt, pt;
11474 {
11475 EMACS_INT start, end;
11476 Lisp_Object prop;
11477 Lisp_Object buffer;
11478
11479 XSETBUFFER (buffer, buf);
11480 /* Check a composition at the last point if point moved within the
11481 same buffer. */
11482 if (prev_buf == buf)
11483 {
11484 if (prev_pt == pt)
11485 /* Point didn't move. */
11486 return 0;
11487
11488 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11489 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11490 && COMPOSITION_VALID_P (start, end, prop)
11491 && start < prev_pt && end > prev_pt)
11492 /* The last point was within the composition. Return 1 iff
11493 point moved out of the composition. */
11494 return (pt <= start || pt >= end);
11495 }
11496
11497 /* Check a composition at the current point. */
11498 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11499 && find_composition (pt, -1, &start, &end, &prop, buffer)
11500 && COMPOSITION_VALID_P (start, end, prop)
11501 && start < pt && end > pt);
11502 }
11503
11504
11505 /* Reconsider the setting of B->clip_changed which is displayed
11506 in window W. */
11507
11508 static INLINE void
11509 reconsider_clip_changes (w, b)
11510 struct window *w;
11511 struct buffer *b;
11512 {
11513 if (b->clip_changed
11514 && !NILP (w->window_end_valid)
11515 && w->current_matrix->buffer == b
11516 && w->current_matrix->zv == BUF_ZV (b)
11517 && w->current_matrix->begv == BUF_BEGV (b))
11518 b->clip_changed = 0;
11519
11520 /* If display wasn't paused, and W is not a tool bar window, see if
11521 point has been moved into or out of a composition. In that case,
11522 we set b->clip_changed to 1 to force updating the screen. If
11523 b->clip_changed has already been set to 1, we can skip this
11524 check. */
11525 if (!b->clip_changed
11526 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11527 {
11528 int pt;
11529
11530 if (w == XWINDOW (selected_window))
11531 pt = BUF_PT (current_buffer);
11532 else
11533 pt = marker_position (w->pointm);
11534
11535 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11536 || pt != XINT (w->last_point))
11537 && check_point_in_composition (w->current_matrix->buffer,
11538 XINT (w->last_point),
11539 XBUFFER (w->buffer), pt))
11540 b->clip_changed = 1;
11541 }
11542 }
11543 \f
11544
11545 /* Select FRAME to forward the values of frame-local variables into C
11546 variables so that the redisplay routines can access those values
11547 directly. */
11548
11549 static void
11550 select_frame_for_redisplay (frame)
11551 Lisp_Object frame;
11552 {
11553 Lisp_Object tail, symbol, val;
11554 Lisp_Object old = selected_frame;
11555 struct Lisp_Symbol *sym;
11556
11557 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11558
11559 selected_frame = frame;
11560
11561 do
11562 {
11563 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11564 if (CONSP (XCAR (tail))
11565 && (symbol = XCAR (XCAR (tail)),
11566 SYMBOLP (symbol))
11567 && (sym = indirect_variable (XSYMBOL (symbol)),
11568 val = sym->value,
11569 (BUFFER_LOCAL_VALUEP (val)))
11570 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11571 /* Use find_symbol_value rather than Fsymbol_value
11572 to avoid an error if it is void. */
11573 find_symbol_value (symbol);
11574 } while (!EQ (frame, old) && (frame = old, 1));
11575 }
11576
11577
11578 #define STOP_POLLING \
11579 do { if (! polling_stopped_here) stop_polling (); \
11580 polling_stopped_here = 1; } while (0)
11581
11582 #define RESUME_POLLING \
11583 do { if (polling_stopped_here) start_polling (); \
11584 polling_stopped_here = 0; } while (0)
11585
11586
11587 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11588 response to any user action; therefore, we should preserve the echo
11589 area. (Actually, our caller does that job.) Perhaps in the future
11590 avoid recentering windows if it is not necessary; currently that
11591 causes some problems. */
11592
11593 static void
11594 redisplay_internal (preserve_echo_area)
11595 int preserve_echo_area;
11596 {
11597 struct window *w = XWINDOW (selected_window);
11598 struct frame *f;
11599 int pause;
11600 int must_finish = 0;
11601 struct text_pos tlbufpos, tlendpos;
11602 int number_of_visible_frames;
11603 int count, count1;
11604 struct frame *sf;
11605 int polling_stopped_here = 0;
11606 Lisp_Object old_frame = selected_frame;
11607
11608 /* Non-zero means redisplay has to consider all windows on all
11609 frames. Zero means, only selected_window is considered. */
11610 int consider_all_windows_p;
11611
11612 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11613
11614 /* No redisplay if running in batch mode or frame is not yet fully
11615 initialized, or redisplay is explicitly turned off by setting
11616 Vinhibit_redisplay. */
11617 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11618 || !NILP (Vinhibit_redisplay))
11619 return;
11620
11621 /* Don't examine these until after testing Vinhibit_redisplay.
11622 When Emacs is shutting down, perhaps because its connection to
11623 X has dropped, we should not look at them at all. */
11624 f = XFRAME (w->frame);
11625 sf = SELECTED_FRAME ();
11626
11627 if (!f->glyphs_initialized_p)
11628 return;
11629
11630 /* The flag redisplay_performed_directly_p is set by
11631 direct_output_for_insert when it already did the whole screen
11632 update necessary. */
11633 if (redisplay_performed_directly_p)
11634 {
11635 redisplay_performed_directly_p = 0;
11636 if (!hscroll_windows (selected_window))
11637 return;
11638 }
11639
11640 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11641 if (popup_activated ())
11642 return;
11643 #endif
11644
11645 /* I don't think this happens but let's be paranoid. */
11646 if (redisplaying_p)
11647 return;
11648
11649 /* Record a function that resets redisplaying_p to its old value
11650 when we leave this function. */
11651 count = SPECPDL_INDEX ();
11652 record_unwind_protect (unwind_redisplay,
11653 Fcons (make_number (redisplaying_p), selected_frame));
11654 ++redisplaying_p;
11655 specbind (Qinhibit_free_realized_faces, Qnil);
11656
11657 {
11658 Lisp_Object tail, frame;
11659
11660 FOR_EACH_FRAME (tail, frame)
11661 {
11662 struct frame *f = XFRAME (frame);
11663 f->already_hscrolled_p = 0;
11664 }
11665 }
11666
11667 retry:
11668 if (!EQ (old_frame, selected_frame)
11669 && FRAME_LIVE_P (XFRAME (old_frame)))
11670 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11671 selected_frame and selected_window to be temporarily out-of-sync so
11672 when we come back here via `goto retry', we need to resync because we
11673 may need to run Elisp code (via prepare_menu_bars). */
11674 select_frame_for_redisplay (old_frame);
11675
11676 pause = 0;
11677 reconsider_clip_changes (w, current_buffer);
11678 last_escape_glyph_frame = NULL;
11679 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11680
11681 /* If new fonts have been loaded that make a glyph matrix adjustment
11682 necessary, do it. */
11683 if (fonts_changed_p)
11684 {
11685 adjust_glyphs (NULL);
11686 ++windows_or_buffers_changed;
11687 fonts_changed_p = 0;
11688 }
11689
11690 /* If face_change_count is non-zero, init_iterator will free all
11691 realized faces, which includes the faces referenced from current
11692 matrices. So, we can't reuse current matrices in this case. */
11693 if (face_change_count)
11694 ++windows_or_buffers_changed;
11695
11696 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11697 && FRAME_TTY (sf)->previous_frame != sf)
11698 {
11699 /* Since frames on a single ASCII terminal share the same
11700 display area, displaying a different frame means redisplay
11701 the whole thing. */
11702 windows_or_buffers_changed++;
11703 SET_FRAME_GARBAGED (sf);
11704 #ifndef DOS_NT
11705 set_tty_color_mode (FRAME_TTY (sf), sf);
11706 #endif
11707 FRAME_TTY (sf)->previous_frame = sf;
11708 }
11709
11710 /* Set the visible flags for all frames. Do this before checking
11711 for resized or garbaged frames; they want to know if their frames
11712 are visible. See the comment in frame.h for
11713 FRAME_SAMPLE_VISIBILITY. */
11714 {
11715 Lisp_Object tail, frame;
11716
11717 number_of_visible_frames = 0;
11718
11719 FOR_EACH_FRAME (tail, frame)
11720 {
11721 struct frame *f = XFRAME (frame);
11722
11723 FRAME_SAMPLE_VISIBILITY (f);
11724 if (FRAME_VISIBLE_P (f))
11725 ++number_of_visible_frames;
11726 clear_desired_matrices (f);
11727 }
11728 }
11729
11730 /* Notice any pending interrupt request to change frame size. */
11731 do_pending_window_change (1);
11732
11733 /* Clear frames marked as garbaged. */
11734 if (frame_garbaged)
11735 clear_garbaged_frames ();
11736
11737 /* Build menubar and tool-bar items. */
11738 if (NILP (Vmemory_full))
11739 prepare_menu_bars ();
11740
11741 if (windows_or_buffers_changed)
11742 update_mode_lines++;
11743
11744 /* Detect case that we need to write or remove a star in the mode line. */
11745 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11746 {
11747 w->update_mode_line = Qt;
11748 if (buffer_shared > 1)
11749 update_mode_lines++;
11750 }
11751
11752 /* Avoid invocation of point motion hooks by `current_column' below. */
11753 count1 = SPECPDL_INDEX ();
11754 specbind (Qinhibit_point_motion_hooks, Qt);
11755
11756 /* If %c is in the mode line, update it if needed. */
11757 if (!NILP (w->column_number_displayed)
11758 /* This alternative quickly identifies a common case
11759 where no change is needed. */
11760 && !(PT == XFASTINT (w->last_point)
11761 && XFASTINT (w->last_modified) >= MODIFF
11762 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11763 && (XFASTINT (w->column_number_displayed)
11764 != (int) current_column ())) /* iftc */
11765 w->update_mode_line = Qt;
11766
11767 unbind_to (count1, Qnil);
11768
11769 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11770
11771 /* The variable buffer_shared is set in redisplay_window and
11772 indicates that we redisplay a buffer in different windows. See
11773 there. */
11774 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11775 || cursor_type_changed);
11776
11777 /* If specs for an arrow have changed, do thorough redisplay
11778 to ensure we remove any arrow that should no longer exist. */
11779 if (overlay_arrows_changed_p ())
11780 consider_all_windows_p = windows_or_buffers_changed = 1;
11781
11782 /* Normally the message* functions will have already displayed and
11783 updated the echo area, but the frame may have been trashed, or
11784 the update may have been preempted, so display the echo area
11785 again here. Checking message_cleared_p captures the case that
11786 the echo area should be cleared. */
11787 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11788 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11789 || (message_cleared_p
11790 && minibuf_level == 0
11791 /* If the mini-window is currently selected, this means the
11792 echo-area doesn't show through. */
11793 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11794 {
11795 int window_height_changed_p = echo_area_display (0);
11796 must_finish = 1;
11797
11798 /* If we don't display the current message, don't clear the
11799 message_cleared_p flag, because, if we did, we wouldn't clear
11800 the echo area in the next redisplay which doesn't preserve
11801 the echo area. */
11802 if (!display_last_displayed_message_p)
11803 message_cleared_p = 0;
11804
11805 if (fonts_changed_p)
11806 goto retry;
11807 else if (window_height_changed_p)
11808 {
11809 consider_all_windows_p = 1;
11810 ++update_mode_lines;
11811 ++windows_or_buffers_changed;
11812
11813 /* If window configuration was changed, frames may have been
11814 marked garbaged. Clear them or we will experience
11815 surprises wrt scrolling. */
11816 if (frame_garbaged)
11817 clear_garbaged_frames ();
11818 }
11819 }
11820 else if (EQ (selected_window, minibuf_window)
11821 && (current_buffer->clip_changed
11822 || XFASTINT (w->last_modified) < MODIFF
11823 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11824 && resize_mini_window (w, 0))
11825 {
11826 /* Resized active mini-window to fit the size of what it is
11827 showing if its contents might have changed. */
11828 must_finish = 1;
11829 /* FIXME: this causes all frames to be updated, which seems unnecessary
11830 since only the current frame needs to be considered. This function needs
11831 to be rewritten with two variables, consider_all_windows and
11832 consider_all_frames. */
11833 consider_all_windows_p = 1;
11834 ++windows_or_buffers_changed;
11835 ++update_mode_lines;
11836
11837 /* If window configuration was changed, frames may have been
11838 marked garbaged. Clear them or we will experience
11839 surprises wrt scrolling. */
11840 if (frame_garbaged)
11841 clear_garbaged_frames ();
11842 }
11843
11844
11845 /* If showing the region, and mark has changed, we must redisplay
11846 the whole window. The assignment to this_line_start_pos prevents
11847 the optimization directly below this if-statement. */
11848 if (((!NILP (Vtransient_mark_mode)
11849 && !NILP (XBUFFER (w->buffer)->mark_active))
11850 != !NILP (w->region_showing))
11851 || (!NILP (w->region_showing)
11852 && !EQ (w->region_showing,
11853 Fmarker_position (XBUFFER (w->buffer)->mark))))
11854 CHARPOS (this_line_start_pos) = 0;
11855
11856 /* Optimize the case that only the line containing the cursor in the
11857 selected window has changed. Variables starting with this_ are
11858 set in display_line and record information about the line
11859 containing the cursor. */
11860 tlbufpos = this_line_start_pos;
11861 tlendpos = this_line_end_pos;
11862 if (!consider_all_windows_p
11863 && CHARPOS (tlbufpos) > 0
11864 && NILP (w->update_mode_line)
11865 && !current_buffer->clip_changed
11866 && !current_buffer->prevent_redisplay_optimizations_p
11867 && FRAME_VISIBLE_P (XFRAME (w->frame))
11868 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11869 /* Make sure recorded data applies to current buffer, etc. */
11870 && this_line_buffer == current_buffer
11871 && current_buffer == XBUFFER (w->buffer)
11872 && NILP (w->force_start)
11873 && NILP (w->optional_new_start)
11874 /* Point must be on the line that we have info recorded about. */
11875 && PT >= CHARPOS (tlbufpos)
11876 && PT <= Z - CHARPOS (tlendpos)
11877 /* All text outside that line, including its final newline,
11878 must be unchanged. */
11879 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11880 CHARPOS (tlendpos)))
11881 {
11882 if (CHARPOS (tlbufpos) > BEGV
11883 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11884 && (CHARPOS (tlbufpos) == ZV
11885 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11886 /* Former continuation line has disappeared by becoming empty. */
11887 goto cancel;
11888 else if (XFASTINT (w->last_modified) < MODIFF
11889 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11890 || MINI_WINDOW_P (w))
11891 {
11892 /* We have to handle the case of continuation around a
11893 wide-column character (see the comment in indent.c around
11894 line 1340).
11895
11896 For instance, in the following case:
11897
11898 -------- Insert --------
11899 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11900 J_I_ ==> J_I_ `^^' are cursors.
11901 ^^ ^^
11902 -------- --------
11903
11904 As we have to redraw the line above, we cannot use this
11905 optimization. */
11906
11907 struct it it;
11908 int line_height_before = this_line_pixel_height;
11909
11910 /* Note that start_display will handle the case that the
11911 line starting at tlbufpos is a continuation line. */
11912 start_display (&it, w, tlbufpos);
11913
11914 /* Implementation note: It this still necessary? */
11915 if (it.current_x != this_line_start_x)
11916 goto cancel;
11917
11918 TRACE ((stderr, "trying display optimization 1\n"));
11919 w->cursor.vpos = -1;
11920 overlay_arrow_seen = 0;
11921 it.vpos = this_line_vpos;
11922 it.current_y = this_line_y;
11923 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11924 display_line (&it);
11925
11926 /* If line contains point, is not continued,
11927 and ends at same distance from eob as before, we win. */
11928 if (w->cursor.vpos >= 0
11929 /* Line is not continued, otherwise this_line_start_pos
11930 would have been set to 0 in display_line. */
11931 && CHARPOS (this_line_start_pos)
11932 /* Line ends as before. */
11933 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11934 /* Line has same height as before. Otherwise other lines
11935 would have to be shifted up or down. */
11936 && this_line_pixel_height == line_height_before)
11937 {
11938 /* If this is not the window's last line, we must adjust
11939 the charstarts of the lines below. */
11940 if (it.current_y < it.last_visible_y)
11941 {
11942 struct glyph_row *row
11943 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11944 int delta, delta_bytes;
11945
11946 /* We used to distinguish between two cases here,
11947 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11948 when the line ends in a newline or the end of the
11949 buffer's accessible portion. But both cases did
11950 the same, so they were collapsed. */
11951 delta = (Z
11952 - CHARPOS (tlendpos)
11953 - MATRIX_ROW_START_CHARPOS (row));
11954 delta_bytes = (Z_BYTE
11955 - BYTEPOS (tlendpos)
11956 - MATRIX_ROW_START_BYTEPOS (row));
11957
11958 increment_matrix_positions (w->current_matrix,
11959 this_line_vpos + 1,
11960 w->current_matrix->nrows,
11961 delta, delta_bytes);
11962 }
11963
11964 /* If this row displays text now but previously didn't,
11965 or vice versa, w->window_end_vpos may have to be
11966 adjusted. */
11967 if ((it.glyph_row - 1)->displays_text_p)
11968 {
11969 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11970 XSETINT (w->window_end_vpos, this_line_vpos);
11971 }
11972 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11973 && this_line_vpos > 0)
11974 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11975 w->window_end_valid = Qnil;
11976
11977 /* Update hint: No need to try to scroll in update_window. */
11978 w->desired_matrix->no_scrolling_p = 1;
11979
11980 #if GLYPH_DEBUG
11981 *w->desired_matrix->method = 0;
11982 debug_method_add (w, "optimization 1");
11983 #endif
11984 #ifdef HAVE_WINDOW_SYSTEM
11985 update_window_fringes (w, 0);
11986 #endif
11987 goto update;
11988 }
11989 else
11990 goto cancel;
11991 }
11992 else if (/* Cursor position hasn't changed. */
11993 PT == XFASTINT (w->last_point)
11994 /* Make sure the cursor was last displayed
11995 in this window. Otherwise we have to reposition it. */
11996 && 0 <= w->cursor.vpos
11997 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11998 {
11999 if (!must_finish)
12000 {
12001 do_pending_window_change (1);
12002
12003 /* We used to always goto end_of_redisplay here, but this
12004 isn't enough if we have a blinking cursor. */
12005 if (w->cursor_off_p == w->last_cursor_off_p)
12006 goto end_of_redisplay;
12007 }
12008 goto update;
12009 }
12010 /* If highlighting the region, or if the cursor is in the echo area,
12011 then we can't just move the cursor. */
12012 else if (! (!NILP (Vtransient_mark_mode)
12013 && !NILP (current_buffer->mark_active))
12014 && (EQ (selected_window, current_buffer->last_selected_window)
12015 || highlight_nonselected_windows)
12016 && NILP (w->region_showing)
12017 && NILP (Vshow_trailing_whitespace)
12018 && !cursor_in_echo_area)
12019 {
12020 struct it it;
12021 struct glyph_row *row;
12022
12023 /* Skip from tlbufpos to PT and see where it is. Note that
12024 PT may be in invisible text. If so, we will end at the
12025 next visible position. */
12026 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12027 NULL, DEFAULT_FACE_ID);
12028 it.current_x = this_line_start_x;
12029 it.current_y = this_line_y;
12030 it.vpos = this_line_vpos;
12031
12032 /* The call to move_it_to stops in front of PT, but
12033 moves over before-strings. */
12034 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12035
12036 if (it.vpos == this_line_vpos
12037 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12038 row->enabled_p))
12039 {
12040 xassert (this_line_vpos == it.vpos);
12041 xassert (this_line_y == it.current_y);
12042 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12043 #if GLYPH_DEBUG
12044 *w->desired_matrix->method = 0;
12045 debug_method_add (w, "optimization 3");
12046 #endif
12047 goto update;
12048 }
12049 else
12050 goto cancel;
12051 }
12052
12053 cancel:
12054 /* Text changed drastically or point moved off of line. */
12055 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12056 }
12057
12058 CHARPOS (this_line_start_pos) = 0;
12059 consider_all_windows_p |= buffer_shared > 1;
12060 ++clear_face_cache_count;
12061 #ifdef HAVE_WINDOW_SYSTEM
12062 ++clear_image_cache_count;
12063 #endif
12064
12065 /* Build desired matrices, and update the display. If
12066 consider_all_windows_p is non-zero, do it for all windows on all
12067 frames. Otherwise do it for selected_window, only. */
12068
12069 if (consider_all_windows_p)
12070 {
12071 Lisp_Object tail, frame;
12072
12073 FOR_EACH_FRAME (tail, frame)
12074 XFRAME (frame)->updated_p = 0;
12075
12076 /* Recompute # windows showing selected buffer. This will be
12077 incremented each time such a window is displayed. */
12078 buffer_shared = 0;
12079
12080 FOR_EACH_FRAME (tail, frame)
12081 {
12082 struct frame *f = XFRAME (frame);
12083
12084 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12085 {
12086 if (! EQ (frame, selected_frame))
12087 /* Select the frame, for the sake of frame-local
12088 variables. */
12089 select_frame_for_redisplay (frame);
12090
12091 /* Mark all the scroll bars to be removed; we'll redeem
12092 the ones we want when we redisplay their windows. */
12093 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12094 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12095
12096 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12097 redisplay_windows (FRAME_ROOT_WINDOW (f));
12098
12099 /* The X error handler may have deleted that frame. */
12100 if (!FRAME_LIVE_P (f))
12101 continue;
12102
12103 /* Any scroll bars which redisplay_windows should have
12104 nuked should now go away. */
12105 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12106 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12107
12108 /* If fonts changed, display again. */
12109 /* ??? rms: I suspect it is a mistake to jump all the way
12110 back to retry here. It should just retry this frame. */
12111 if (fonts_changed_p)
12112 goto retry;
12113
12114 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12115 {
12116 /* See if we have to hscroll. */
12117 if (!f->already_hscrolled_p)
12118 {
12119 f->already_hscrolled_p = 1;
12120 if (hscroll_windows (f->root_window))
12121 goto retry;
12122 }
12123
12124 /* Prevent various kinds of signals during display
12125 update. stdio is not robust about handling
12126 signals, which can cause an apparent I/O
12127 error. */
12128 if (interrupt_input)
12129 unrequest_sigio ();
12130 STOP_POLLING;
12131
12132 /* Update the display. */
12133 set_window_update_flags (XWINDOW (f->root_window), 1);
12134 pause |= update_frame (f, 0, 0);
12135 f->updated_p = 1;
12136 }
12137 }
12138 }
12139
12140 if (!EQ (old_frame, selected_frame)
12141 && FRAME_LIVE_P (XFRAME (old_frame)))
12142 /* We played a bit fast-and-loose above and allowed selected_frame
12143 and selected_window to be temporarily out-of-sync but let's make
12144 sure this stays contained. */
12145 select_frame_for_redisplay (old_frame);
12146 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12147
12148 if (!pause)
12149 {
12150 /* Do the mark_window_display_accurate after all windows have
12151 been redisplayed because this call resets flags in buffers
12152 which are needed for proper redisplay. */
12153 FOR_EACH_FRAME (tail, frame)
12154 {
12155 struct frame *f = XFRAME (frame);
12156 if (f->updated_p)
12157 {
12158 mark_window_display_accurate (f->root_window, 1);
12159 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12160 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12161 }
12162 }
12163 }
12164 }
12165 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12166 {
12167 Lisp_Object mini_window;
12168 struct frame *mini_frame;
12169
12170 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12171 /* Use list_of_error, not Qerror, so that
12172 we catch only errors and don't run the debugger. */
12173 internal_condition_case_1 (redisplay_window_1, selected_window,
12174 list_of_error,
12175 redisplay_window_error);
12176
12177 /* Compare desired and current matrices, perform output. */
12178
12179 update:
12180 /* If fonts changed, display again. */
12181 if (fonts_changed_p)
12182 goto retry;
12183
12184 /* Prevent various kinds of signals during display update.
12185 stdio is not robust about handling signals,
12186 which can cause an apparent I/O error. */
12187 if (interrupt_input)
12188 unrequest_sigio ();
12189 STOP_POLLING;
12190
12191 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12192 {
12193 if (hscroll_windows (selected_window))
12194 goto retry;
12195
12196 XWINDOW (selected_window)->must_be_updated_p = 1;
12197 pause = update_frame (sf, 0, 0);
12198 }
12199
12200 /* We may have called echo_area_display at the top of this
12201 function. If the echo area is on another frame, that may
12202 have put text on a frame other than the selected one, so the
12203 above call to update_frame would not have caught it. Catch
12204 it here. */
12205 mini_window = FRAME_MINIBUF_WINDOW (sf);
12206 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12207
12208 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12209 {
12210 XWINDOW (mini_window)->must_be_updated_p = 1;
12211 pause |= update_frame (mini_frame, 0, 0);
12212 if (!pause && hscroll_windows (mini_window))
12213 goto retry;
12214 }
12215 }
12216
12217 /* If display was paused because of pending input, make sure we do a
12218 thorough update the next time. */
12219 if (pause)
12220 {
12221 /* Prevent the optimization at the beginning of
12222 redisplay_internal that tries a single-line update of the
12223 line containing the cursor in the selected window. */
12224 CHARPOS (this_line_start_pos) = 0;
12225
12226 /* Let the overlay arrow be updated the next time. */
12227 update_overlay_arrows (0);
12228
12229 /* If we pause after scrolling, some rows in the current
12230 matrices of some windows are not valid. */
12231 if (!WINDOW_FULL_WIDTH_P (w)
12232 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12233 update_mode_lines = 1;
12234 }
12235 else
12236 {
12237 if (!consider_all_windows_p)
12238 {
12239 /* This has already been done above if
12240 consider_all_windows_p is set. */
12241 mark_window_display_accurate_1 (w, 1);
12242
12243 /* Say overlay arrows are up to date. */
12244 update_overlay_arrows (1);
12245
12246 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12247 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12248 }
12249
12250 update_mode_lines = 0;
12251 windows_or_buffers_changed = 0;
12252 cursor_type_changed = 0;
12253 }
12254
12255 /* Start SIGIO interrupts coming again. Having them off during the
12256 code above makes it less likely one will discard output, but not
12257 impossible, since there might be stuff in the system buffer here.
12258 But it is much hairier to try to do anything about that. */
12259 if (interrupt_input)
12260 request_sigio ();
12261 RESUME_POLLING;
12262
12263 /* If a frame has become visible which was not before, redisplay
12264 again, so that we display it. Expose events for such a frame
12265 (which it gets when becoming visible) don't call the parts of
12266 redisplay constructing glyphs, so simply exposing a frame won't
12267 display anything in this case. So, we have to display these
12268 frames here explicitly. */
12269 if (!pause)
12270 {
12271 Lisp_Object tail, frame;
12272 int new_count = 0;
12273
12274 FOR_EACH_FRAME (tail, frame)
12275 {
12276 int this_is_visible = 0;
12277
12278 if (XFRAME (frame)->visible)
12279 this_is_visible = 1;
12280 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12281 if (XFRAME (frame)->visible)
12282 this_is_visible = 1;
12283
12284 if (this_is_visible)
12285 new_count++;
12286 }
12287
12288 if (new_count != number_of_visible_frames)
12289 windows_or_buffers_changed++;
12290 }
12291
12292 /* Change frame size now if a change is pending. */
12293 do_pending_window_change (1);
12294
12295 /* If we just did a pending size change, or have additional
12296 visible frames, redisplay again. */
12297 if (windows_or_buffers_changed && !pause)
12298 goto retry;
12299
12300 /* Clear the face cache eventually. */
12301 if (consider_all_windows_p)
12302 {
12303 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12304 {
12305 clear_face_cache (0);
12306 clear_face_cache_count = 0;
12307 }
12308 #ifdef HAVE_WINDOW_SYSTEM
12309 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12310 {
12311 clear_image_caches (Qnil);
12312 clear_image_cache_count = 0;
12313 }
12314 #endif /* HAVE_WINDOW_SYSTEM */
12315 }
12316
12317 end_of_redisplay:
12318 unbind_to (count, Qnil);
12319 RESUME_POLLING;
12320 }
12321
12322
12323 /* Redisplay, but leave alone any recent echo area message unless
12324 another message has been requested in its place.
12325
12326 This is useful in situations where you need to redisplay but no
12327 user action has occurred, making it inappropriate for the message
12328 area to be cleared. See tracking_off and
12329 wait_reading_process_output for examples of these situations.
12330
12331 FROM_WHERE is an integer saying from where this function was
12332 called. This is useful for debugging. */
12333
12334 void
12335 redisplay_preserve_echo_area (from_where)
12336 int from_where;
12337 {
12338 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12339
12340 if (!NILP (echo_area_buffer[1]))
12341 {
12342 /* We have a previously displayed message, but no current
12343 message. Redisplay the previous message. */
12344 display_last_displayed_message_p = 1;
12345 redisplay_internal (1);
12346 display_last_displayed_message_p = 0;
12347 }
12348 else
12349 redisplay_internal (1);
12350
12351 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12352 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12353 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12354 }
12355
12356
12357 /* Function registered with record_unwind_protect in
12358 redisplay_internal. Reset redisplaying_p to the value it had
12359 before redisplay_internal was called, and clear
12360 prevent_freeing_realized_faces_p. It also selects the previously
12361 selected frame, unless it has been deleted (by an X connection
12362 failure during redisplay, for example). */
12363
12364 static Lisp_Object
12365 unwind_redisplay (val)
12366 Lisp_Object val;
12367 {
12368 Lisp_Object old_redisplaying_p, old_frame;
12369
12370 old_redisplaying_p = XCAR (val);
12371 redisplaying_p = XFASTINT (old_redisplaying_p);
12372 old_frame = XCDR (val);
12373 if (! EQ (old_frame, selected_frame)
12374 && FRAME_LIVE_P (XFRAME (old_frame)))
12375 select_frame_for_redisplay (old_frame);
12376 return Qnil;
12377 }
12378
12379
12380 /* Mark the display of window W as accurate or inaccurate. If
12381 ACCURATE_P is non-zero mark display of W as accurate. If
12382 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12383 redisplay_internal is called. */
12384
12385 static void
12386 mark_window_display_accurate_1 (w, accurate_p)
12387 struct window *w;
12388 int accurate_p;
12389 {
12390 if (BUFFERP (w->buffer))
12391 {
12392 struct buffer *b = XBUFFER (w->buffer);
12393
12394 w->last_modified
12395 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12396 w->last_overlay_modified
12397 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12398 w->last_had_star
12399 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12400
12401 if (accurate_p)
12402 {
12403 b->clip_changed = 0;
12404 b->prevent_redisplay_optimizations_p = 0;
12405
12406 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12407 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12408 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12409 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12410
12411 w->current_matrix->buffer = b;
12412 w->current_matrix->begv = BUF_BEGV (b);
12413 w->current_matrix->zv = BUF_ZV (b);
12414
12415 w->last_cursor = w->cursor;
12416 w->last_cursor_off_p = w->cursor_off_p;
12417
12418 if (w == XWINDOW (selected_window))
12419 w->last_point = make_number (BUF_PT (b));
12420 else
12421 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12422 }
12423 }
12424
12425 if (accurate_p)
12426 {
12427 w->window_end_valid = w->buffer;
12428 w->update_mode_line = Qnil;
12429 }
12430 }
12431
12432
12433 /* Mark the display of windows in the window tree rooted at WINDOW as
12434 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12435 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12436 be redisplayed the next time redisplay_internal is called. */
12437
12438 void
12439 mark_window_display_accurate (window, accurate_p)
12440 Lisp_Object window;
12441 int accurate_p;
12442 {
12443 struct window *w;
12444
12445 for (; !NILP (window); window = w->next)
12446 {
12447 w = XWINDOW (window);
12448 mark_window_display_accurate_1 (w, accurate_p);
12449
12450 if (!NILP (w->vchild))
12451 mark_window_display_accurate (w->vchild, accurate_p);
12452 if (!NILP (w->hchild))
12453 mark_window_display_accurate (w->hchild, accurate_p);
12454 }
12455
12456 if (accurate_p)
12457 {
12458 update_overlay_arrows (1);
12459 }
12460 else
12461 {
12462 /* Force a thorough redisplay the next time by setting
12463 last_arrow_position and last_arrow_string to t, which is
12464 unequal to any useful value of Voverlay_arrow_... */
12465 update_overlay_arrows (-1);
12466 }
12467 }
12468
12469
12470 /* Return value in display table DP (Lisp_Char_Table *) for character
12471 C. Since a display table doesn't have any parent, we don't have to
12472 follow parent. Do not call this function directly but use the
12473 macro DISP_CHAR_VECTOR. */
12474
12475 Lisp_Object
12476 disp_char_vector (dp, c)
12477 struct Lisp_Char_Table *dp;
12478 int c;
12479 {
12480 Lisp_Object val;
12481
12482 if (ASCII_CHAR_P (c))
12483 {
12484 val = dp->ascii;
12485 if (SUB_CHAR_TABLE_P (val))
12486 val = XSUB_CHAR_TABLE (val)->contents[c];
12487 }
12488 else
12489 {
12490 Lisp_Object table;
12491
12492 XSETCHAR_TABLE (table, dp);
12493 val = char_table_ref (table, c);
12494 }
12495 if (NILP (val))
12496 val = dp->defalt;
12497 return val;
12498 }
12499
12500
12501 \f
12502 /***********************************************************************
12503 Window Redisplay
12504 ***********************************************************************/
12505
12506 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12507
12508 static void
12509 redisplay_windows (window)
12510 Lisp_Object window;
12511 {
12512 while (!NILP (window))
12513 {
12514 struct window *w = XWINDOW (window);
12515
12516 if (!NILP (w->hchild))
12517 redisplay_windows (w->hchild);
12518 else if (!NILP (w->vchild))
12519 redisplay_windows (w->vchild);
12520 else if (!NILP (w->buffer))
12521 {
12522 displayed_buffer = XBUFFER (w->buffer);
12523 /* Use list_of_error, not Qerror, so that
12524 we catch only errors and don't run the debugger. */
12525 internal_condition_case_1 (redisplay_window_0, window,
12526 list_of_error,
12527 redisplay_window_error);
12528 }
12529
12530 window = w->next;
12531 }
12532 }
12533
12534 static Lisp_Object
12535 redisplay_window_error ()
12536 {
12537 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12538 return Qnil;
12539 }
12540
12541 static Lisp_Object
12542 redisplay_window_0 (window)
12543 Lisp_Object window;
12544 {
12545 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12546 redisplay_window (window, 0);
12547 return Qnil;
12548 }
12549
12550 static Lisp_Object
12551 redisplay_window_1 (window)
12552 Lisp_Object window;
12553 {
12554 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12555 redisplay_window (window, 1);
12556 return Qnil;
12557 }
12558 \f
12559
12560 /* Increment GLYPH until it reaches END or CONDITION fails while
12561 adding (GLYPH)->pixel_width to X. */
12562
12563 #define SKIP_GLYPHS(glyph, end, x, condition) \
12564 do \
12565 { \
12566 (x) += (glyph)->pixel_width; \
12567 ++(glyph); \
12568 } \
12569 while ((glyph) < (end) && (condition))
12570
12571
12572 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12573 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12574 which positions recorded in ROW differ from current buffer
12575 positions.
12576
12577 Return 0 if cursor is not on this row, 1 otherwise. */
12578
12579 int
12580 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12581 struct window *w;
12582 struct glyph_row *row;
12583 struct glyph_matrix *matrix;
12584 int delta, delta_bytes, dy, dvpos;
12585 {
12586 struct glyph *glyph = row->glyphs[TEXT_AREA];
12587 struct glyph *end = glyph + row->used[TEXT_AREA];
12588 struct glyph *cursor = NULL;
12589 /* The last known character position in row. */
12590 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12591 int x = row->x;
12592 int cursor_x = x;
12593 EMACS_INT pt_old = PT - delta;
12594 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12595 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12596 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12597 /* Non-zero means we've found a match for cursor position, but that
12598 glyph has the avoid_cursor_p flag set. */
12599 int match_with_avoid_cursor = 0;
12600 /* Non-zero means we've seen at least one glyph that came from a
12601 display string. */
12602 int string_seen = 0;
12603 /* Largest buffer position seen so far during scan of glyph row. */
12604 EMACS_INT bpos_max = last_pos;
12605 /* Last buffer position covered by an overlay string with an integer
12606 `cursor' property. */
12607 EMACS_INT bpos_covered = 0;
12608
12609 /* Skip over glyphs not having an object at the start and the end of
12610 the row. These are special glyphs like truncation marks on
12611 terminal frames. */
12612 if (row->displays_text_p)
12613 {
12614 if (!row->reversed_p)
12615 {
12616 while (glyph < end
12617 && INTEGERP (glyph->object)
12618 && glyph->charpos < 0)
12619 {
12620 x += glyph->pixel_width;
12621 ++glyph;
12622 }
12623 while (end > glyph
12624 && INTEGERP ((end - 1)->object)
12625 /* CHARPOS is zero for blanks inserted by
12626 extend_face_to_end_of_line. */
12627 && (end - 1)->charpos <= 0)
12628 --end;
12629 glyph_before = glyph - 1;
12630 glyph_after = end;
12631 }
12632 else
12633 {
12634 struct glyph *g;
12635
12636 /* If the glyph row is reversed, we need to process it from back
12637 to front, so swap the edge pointers. */
12638 end = glyph - 1;
12639 glyph += row->used[TEXT_AREA] - 1;
12640 /* Reverse the known positions in the row. */
12641 last_pos = pos_after = MATRIX_ROW_START_CHARPOS (row) + delta;
12642 pos_before = MATRIX_ROW_END_CHARPOS (row) + delta;
12643
12644 while (glyph > end + 1
12645 && INTEGERP (glyph->object)
12646 && glyph->charpos < 0)
12647 {
12648 --glyph;
12649 x -= glyph->pixel_width;
12650 }
12651 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12652 --glyph;
12653 /* By default, put the cursor on the rightmost glyph. */
12654 for (g = end + 1; g < glyph; g++)
12655 x += g->pixel_width;
12656 cursor_x = x;
12657 while (end < glyph
12658 && INTEGERP ((end + 1)->object)
12659 && (end + 1)->charpos <= 0)
12660 ++end;
12661 glyph_before = glyph + 1;
12662 glyph_after = end;
12663 }
12664 }
12665 else if (row->reversed_p)
12666 {
12667 /* In R2L rows that don't display text, put the cursor on the
12668 rightmost glyph. Case in point: an empty last line that is
12669 part of an R2L paragraph. */
12670 cursor = end - 1;
12671 x = -1; /* will be computed below, at lable compute_x */
12672 }
12673
12674 /* Step 1: Try to find the glyph whose character position
12675 corresponds to point. If that's not possible, find 2 glyphs
12676 whose character positions are the closest to point, one before
12677 point, the other after it. */
12678 if (!row->reversed_p)
12679 while (/* not marched to end of glyph row */
12680 glyph < end
12681 /* glyph was not inserted by redisplay for internal purposes */
12682 && !INTEGERP (glyph->object))
12683 {
12684 if (BUFFERP (glyph->object))
12685 {
12686 EMACS_INT dpos = glyph->charpos - pt_old;
12687
12688 if (glyph->charpos > bpos_max)
12689 bpos_max = glyph->charpos;
12690 if (!glyph->avoid_cursor_p)
12691 {
12692 /* If we hit point, we've found the glyph on which to
12693 display the cursor. */
12694 if (dpos == 0)
12695 {
12696 match_with_avoid_cursor = 0;
12697 break;
12698 }
12699 /* See if we've found a better approximation to
12700 POS_BEFORE or to POS_AFTER. Note that we want the
12701 first (leftmost) glyph of all those that are the
12702 closest from below, and the last (rightmost) of all
12703 those from above. */
12704 if (0 > dpos && dpos > pos_before - pt_old)
12705 {
12706 pos_before = glyph->charpos;
12707 glyph_before = glyph;
12708 }
12709 else if (0 < dpos && dpos <= pos_after - pt_old)
12710 {
12711 pos_after = glyph->charpos;
12712 glyph_after = glyph;
12713 }
12714 }
12715 else if (dpos == 0)
12716 match_with_avoid_cursor = 1;
12717 }
12718 else if (STRINGP (glyph->object))
12719 {
12720 Lisp_Object chprop;
12721 int glyph_pos = glyph->charpos;
12722
12723 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12724 glyph->object);
12725 if (INTEGERP (chprop))
12726 {
12727 bpos_covered = bpos_max + XINT (chprop);
12728 /* If the `cursor' property covers buffer positions up
12729 to and including point, we should display cursor on
12730 this glyph. */
12731 /* Implementation note: bpos_max == pt_old when, e.g.,
12732 we are in an empty line, where bpos_max is set to
12733 MATRIX_ROW_START_CHARPOS, see above. */
12734 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12735 {
12736 cursor = glyph;
12737 break;
12738 }
12739 }
12740
12741 string_seen = 1;
12742 }
12743 x += glyph->pixel_width;
12744 ++glyph;
12745 }
12746 else if (glyph > end) /* row is reversed */
12747 while (!INTEGERP (glyph->object))
12748 {
12749 if (BUFFERP (glyph->object))
12750 {
12751 EMACS_INT dpos = glyph->charpos - pt_old;
12752
12753 if (glyph->charpos > bpos_max)
12754 bpos_max = glyph->charpos;
12755 if (!glyph->avoid_cursor_p)
12756 {
12757 if (dpos == 0)
12758 {
12759 match_with_avoid_cursor = 0;
12760 break;
12761 }
12762 if (0 > dpos && dpos > pos_before - pt_old)
12763 {
12764 pos_before = glyph->charpos;
12765 glyph_before = glyph;
12766 }
12767 else if (0 < dpos && dpos <= pos_after - pt_old)
12768 {
12769 pos_after = glyph->charpos;
12770 glyph_after = glyph;
12771 }
12772 }
12773 else if (dpos == 0)
12774 match_with_avoid_cursor = 1;
12775 }
12776 else if (STRINGP (glyph->object))
12777 {
12778 Lisp_Object chprop;
12779 int glyph_pos = glyph->charpos;
12780
12781 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12782 glyph->object);
12783 if (INTEGERP (chprop))
12784 {
12785 bpos_covered = bpos_max + XINT (chprop);
12786 /* If the `cursor' property covers buffer positions up
12787 to and including point, we should display cursor on
12788 this glyph. */
12789 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12790 {
12791 cursor = glyph;
12792 break;
12793 }
12794 }
12795 string_seen = 1;
12796 }
12797 --glyph;
12798 if (glyph == end)
12799 break;
12800 x -= glyph->pixel_width;
12801 }
12802
12803 /* Step 2: If we didn't find an exact match for point, we need to
12804 look for a proper place to put the cursor among glyphs between
12805 GLYPH_BEFORE and GLYPH_AFTER. */
12806 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12807 && bpos_covered < pt_old)
12808 {
12809 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12810 {
12811 EMACS_INT ellipsis_pos;
12812
12813 /* Scan back over the ellipsis glyphs. */
12814 if (!row->reversed_p)
12815 {
12816 ellipsis_pos = (glyph - 1)->charpos;
12817 while (glyph > row->glyphs[TEXT_AREA]
12818 && (glyph - 1)->charpos == ellipsis_pos)
12819 glyph--, x -= glyph->pixel_width;
12820 /* That loop always goes one position too far, including
12821 the glyph before the ellipsis. So scan forward over
12822 that one. */
12823 x += glyph->pixel_width;
12824 glyph++;
12825 }
12826 else /* row is reversed */
12827 {
12828 ellipsis_pos = (glyph + 1)->charpos;
12829 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12830 && (glyph + 1)->charpos == ellipsis_pos)
12831 glyph++, x += glyph->pixel_width;
12832 x -= glyph->pixel_width;
12833 glyph--;
12834 }
12835 }
12836 else if (match_with_avoid_cursor
12837 /* zero-width characters produce no glyphs */
12838 || eabs (glyph_after - glyph_before) == 1)
12839 {
12840 cursor = glyph_after;
12841 x = -1;
12842 }
12843 else if (string_seen)
12844 {
12845 int incr = row->reversed_p ? -1 : +1;
12846
12847 /* Need to find the glyph that came out of a string which is
12848 present at point. That glyph is somewhere between
12849 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12850 positioned between POS_BEFORE and POS_AFTER in the
12851 buffer. */
12852 struct glyph *stop = glyph_after;
12853 EMACS_INT pos = pos_before;
12854
12855 x = -1;
12856 for (glyph = glyph_before + incr;
12857 row->reversed_p ? glyph > stop : glyph < stop; )
12858 {
12859
12860 /* Any glyphs that come from the buffer are here because
12861 of bidi reordering. Skip them, and only pay
12862 attention to glyphs that came from some string. */
12863 if (STRINGP (glyph->object))
12864 {
12865 Lisp_Object str;
12866 EMACS_INT tem;
12867
12868 str = glyph->object;
12869 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12870 if (pos <= tem)
12871 {
12872 /* If the string from which this glyph came is
12873 found in the buffer at point, then we've
12874 found the glyph we've been looking for. */
12875 if (tem == pt_old)
12876 {
12877 /* The glyphs from this string could have
12878 been reordered. Find the one with the
12879 smallest string position. Or there could
12880 be a character in the string with the
12881 `cursor' property, which means display
12882 cursor on that character's glyph. */
12883 int strpos = glyph->charpos;
12884
12885 cursor = glyph;
12886 for (glyph += incr;
12887 EQ (glyph->object, str);
12888 glyph += incr)
12889 {
12890 Lisp_Object cprop;
12891 int gpos = glyph->charpos;
12892
12893 cprop = Fget_char_property (make_number (gpos),
12894 Qcursor,
12895 glyph->object);
12896 if (!NILP (cprop))
12897 {
12898 cursor = glyph;
12899 break;
12900 }
12901 if (glyph->charpos < strpos)
12902 {
12903 strpos = glyph->charpos;
12904 cursor = glyph;
12905 }
12906 }
12907
12908 goto compute_x;
12909 }
12910 pos = tem + 1; /* don't find previous instances */
12911 }
12912 /* This string is not what we want; skip all of the
12913 glyphs that came from it. */
12914 do
12915 glyph += incr;
12916 while ((row->reversed_p ? glyph > stop : glyph < stop)
12917 && EQ (glyph->object, str));
12918 }
12919 else
12920 glyph += incr;
12921 }
12922
12923 /* If we reached the end of the line, and END was from a string,
12924 the cursor is not on this line. */
12925 if (glyph == end
12926 && STRINGP ((glyph - incr)->object)
12927 && row->continued_p)
12928 return 0;
12929 }
12930 }
12931
12932 compute_x:
12933 if (cursor != NULL)
12934 glyph = cursor;
12935 if (x < 0)
12936 {
12937 struct glyph *g;
12938
12939 /* Need to compute x that corresponds to GLYPH. */
12940 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12941 {
12942 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12943 abort ();
12944 x += g->pixel_width;
12945 }
12946 }
12947
12948 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12949 w->cursor.x = x;
12950 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12951 w->cursor.y = row->y + dy;
12952
12953 if (w == XWINDOW (selected_window))
12954 {
12955 if (!row->continued_p
12956 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12957 && row->x == 0)
12958 {
12959 this_line_buffer = XBUFFER (w->buffer);
12960
12961 CHARPOS (this_line_start_pos)
12962 = MATRIX_ROW_START_CHARPOS (row) + delta;
12963 BYTEPOS (this_line_start_pos)
12964 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12965
12966 CHARPOS (this_line_end_pos)
12967 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12968 BYTEPOS (this_line_end_pos)
12969 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12970
12971 this_line_y = w->cursor.y;
12972 this_line_pixel_height = row->height;
12973 this_line_vpos = w->cursor.vpos;
12974 this_line_start_x = row->x;
12975 }
12976 else
12977 CHARPOS (this_line_start_pos) = 0;
12978 }
12979
12980 return 1;
12981 }
12982
12983
12984 /* Run window scroll functions, if any, for WINDOW with new window
12985 start STARTP. Sets the window start of WINDOW to that position.
12986
12987 We assume that the window's buffer is really current. */
12988
12989 static INLINE struct text_pos
12990 run_window_scroll_functions (window, startp)
12991 Lisp_Object window;
12992 struct text_pos startp;
12993 {
12994 struct window *w = XWINDOW (window);
12995 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12996
12997 if (current_buffer != XBUFFER (w->buffer))
12998 abort ();
12999
13000 if (!NILP (Vwindow_scroll_functions))
13001 {
13002 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13003 make_number (CHARPOS (startp)));
13004 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13005 /* In case the hook functions switch buffers. */
13006 if (current_buffer != XBUFFER (w->buffer))
13007 set_buffer_internal_1 (XBUFFER (w->buffer));
13008 }
13009
13010 return startp;
13011 }
13012
13013
13014 /* Make sure the line containing the cursor is fully visible.
13015 A value of 1 means there is nothing to be done.
13016 (Either the line is fully visible, or it cannot be made so,
13017 or we cannot tell.)
13018
13019 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13020 is higher than window.
13021
13022 A value of 0 means the caller should do scrolling
13023 as if point had gone off the screen. */
13024
13025 static int
13026 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
13027 struct window *w;
13028 int force_p;
13029 int current_matrix_p;
13030 {
13031 struct glyph_matrix *matrix;
13032 struct glyph_row *row;
13033 int window_height;
13034
13035 if (!make_cursor_line_fully_visible_p)
13036 return 1;
13037
13038 /* It's not always possible to find the cursor, e.g, when a window
13039 is full of overlay strings. Don't do anything in that case. */
13040 if (w->cursor.vpos < 0)
13041 return 1;
13042
13043 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13044 row = MATRIX_ROW (matrix, w->cursor.vpos);
13045
13046 /* If the cursor row is not partially visible, there's nothing to do. */
13047 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13048 return 1;
13049
13050 /* If the row the cursor is in is taller than the window's height,
13051 it's not clear what to do, so do nothing. */
13052 window_height = window_box_height (w);
13053 if (row->height >= window_height)
13054 {
13055 if (!force_p || MINI_WINDOW_P (w)
13056 || w->vscroll || w->cursor.vpos == 0)
13057 return 1;
13058 }
13059 return 0;
13060 }
13061
13062
13063 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13064 non-zero means only WINDOW is redisplayed in redisplay_internal.
13065 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13066 in redisplay_window to bring a partially visible line into view in
13067 the case that only the cursor has moved.
13068
13069 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13070 last screen line's vertical height extends past the end of the screen.
13071
13072 Value is
13073
13074 1 if scrolling succeeded
13075
13076 0 if scrolling didn't find point.
13077
13078 -1 if new fonts have been loaded so that we must interrupt
13079 redisplay, adjust glyph matrices, and try again. */
13080
13081 enum
13082 {
13083 SCROLLING_SUCCESS,
13084 SCROLLING_FAILED,
13085 SCROLLING_NEED_LARGER_MATRICES
13086 };
13087
13088 static int
13089 try_scrolling (window, just_this_one_p, scroll_conservatively,
13090 scroll_step, temp_scroll_step, last_line_misfit)
13091 Lisp_Object window;
13092 int just_this_one_p;
13093 EMACS_INT scroll_conservatively, scroll_step;
13094 int temp_scroll_step;
13095 int last_line_misfit;
13096 {
13097 struct window *w = XWINDOW (window);
13098 struct frame *f = XFRAME (w->frame);
13099 struct text_pos pos, startp;
13100 struct it it;
13101 int this_scroll_margin, scroll_max, rc, height;
13102 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13103 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13104 Lisp_Object aggressive;
13105 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13106
13107 #if GLYPH_DEBUG
13108 debug_method_add (w, "try_scrolling");
13109 #endif
13110
13111 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13112
13113 /* Compute scroll margin height in pixels. We scroll when point is
13114 within this distance from the top or bottom of the window. */
13115 if (scroll_margin > 0)
13116 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13117 * FRAME_LINE_HEIGHT (f);
13118 else
13119 this_scroll_margin = 0;
13120
13121 /* Force scroll_conservatively to have a reasonable value, to avoid
13122 overflow while computing how much to scroll. Note that the user
13123 can supply scroll-conservatively equal to `most-positive-fixnum',
13124 which can be larger than INT_MAX. */
13125 if (scroll_conservatively > scroll_limit)
13126 {
13127 scroll_conservatively = scroll_limit;
13128 scroll_max = INT_MAX;
13129 }
13130 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13131 /* Compute how much we should try to scroll maximally to bring
13132 point into view. */
13133 scroll_max = (max (scroll_step,
13134 max (scroll_conservatively, temp_scroll_step))
13135 * FRAME_LINE_HEIGHT (f));
13136 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13137 || NUMBERP (current_buffer->scroll_up_aggressively))
13138 /* We're trying to scroll because of aggressive scrolling but no
13139 scroll_step is set. Choose an arbitrary one. */
13140 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13141 else
13142 scroll_max = 0;
13143
13144 too_near_end:
13145
13146 /* Decide whether to scroll down. */
13147 if (PT > CHARPOS (startp))
13148 {
13149 int scroll_margin_y;
13150
13151 /* Compute the pixel ypos of the scroll margin, then move it to
13152 either that ypos or PT, whichever comes first. */
13153 start_display (&it, w, startp);
13154 scroll_margin_y = it.last_visible_y - this_scroll_margin
13155 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13156 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13157 (MOVE_TO_POS | MOVE_TO_Y));
13158
13159 if (PT > CHARPOS (it.current.pos))
13160 {
13161 int y0 = line_bottom_y (&it);
13162
13163 /* Compute the distance from the scroll margin to PT
13164 (including the height of the cursor line). Moving the
13165 iterator unconditionally to PT can be slow if PT is far
13166 away, so stop 10 lines past the window bottom (is there a
13167 way to do the right thing quickly?). */
13168 move_it_to (&it, PT, -1,
13169 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
13170 -1, MOVE_TO_POS | MOVE_TO_Y);
13171 dy = line_bottom_y (&it) - y0;
13172
13173 if (dy > scroll_max)
13174 return SCROLLING_FAILED;
13175
13176 scroll_down_p = 1;
13177 }
13178 }
13179
13180 if (scroll_down_p)
13181 {
13182 /* Point is in or below the bottom scroll margin, so move the
13183 window start down. If scrolling conservatively, move it just
13184 enough down to make point visible. If scroll_step is set,
13185 move it down by scroll_step. */
13186 if (scroll_conservatively)
13187 amount_to_scroll
13188 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13189 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13190 else if (scroll_step || temp_scroll_step)
13191 amount_to_scroll = scroll_max;
13192 else
13193 {
13194 aggressive = current_buffer->scroll_up_aggressively;
13195 height = WINDOW_BOX_TEXT_HEIGHT (w);
13196 if (NUMBERP (aggressive))
13197 {
13198 double float_amount = XFLOATINT (aggressive) * height;
13199 amount_to_scroll = float_amount;
13200 if (amount_to_scroll == 0 && float_amount > 0)
13201 amount_to_scroll = 1;
13202 }
13203 }
13204
13205 if (amount_to_scroll <= 0)
13206 return SCROLLING_FAILED;
13207
13208 start_display (&it, w, startp);
13209 move_it_vertically (&it, amount_to_scroll);
13210
13211 /* If STARTP is unchanged, move it down another screen line. */
13212 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13213 move_it_by_lines (&it, 1, 1);
13214 startp = it.current.pos;
13215 }
13216 else
13217 {
13218 struct text_pos scroll_margin_pos = startp;
13219
13220 /* See if point is inside the scroll margin at the top of the
13221 window. */
13222 if (this_scroll_margin)
13223 {
13224 start_display (&it, w, startp);
13225 move_it_vertically (&it, this_scroll_margin);
13226 scroll_margin_pos = it.current.pos;
13227 }
13228
13229 if (PT < CHARPOS (scroll_margin_pos))
13230 {
13231 /* Point is in the scroll margin at the top of the window or
13232 above what is displayed in the window. */
13233 int y0;
13234
13235 /* Compute the vertical distance from PT to the scroll
13236 margin position. Give up if distance is greater than
13237 scroll_max. */
13238 SET_TEXT_POS (pos, PT, PT_BYTE);
13239 start_display (&it, w, pos);
13240 y0 = it.current_y;
13241 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13242 it.last_visible_y, -1,
13243 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13244 dy = it.current_y - y0;
13245 if (dy > scroll_max)
13246 return SCROLLING_FAILED;
13247
13248 /* Compute new window start. */
13249 start_display (&it, w, startp);
13250
13251 if (scroll_conservatively)
13252 amount_to_scroll
13253 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13254 else if (scroll_step || temp_scroll_step)
13255 amount_to_scroll = scroll_max;
13256 else
13257 {
13258 aggressive = current_buffer->scroll_down_aggressively;
13259 height = WINDOW_BOX_TEXT_HEIGHT (w);
13260 if (NUMBERP (aggressive))
13261 {
13262 double float_amount = XFLOATINT (aggressive) * height;
13263 amount_to_scroll = float_amount;
13264 if (amount_to_scroll == 0 && float_amount > 0)
13265 amount_to_scroll = 1;
13266 }
13267 }
13268
13269 if (amount_to_scroll <= 0)
13270 return SCROLLING_FAILED;
13271
13272 move_it_vertically_backward (&it, amount_to_scroll);
13273 startp = it.current.pos;
13274 }
13275 }
13276
13277 /* Run window scroll functions. */
13278 startp = run_window_scroll_functions (window, startp);
13279
13280 /* Display the window. Give up if new fonts are loaded, or if point
13281 doesn't appear. */
13282 if (!try_window (window, startp, 0))
13283 rc = SCROLLING_NEED_LARGER_MATRICES;
13284 else if (w->cursor.vpos < 0)
13285 {
13286 clear_glyph_matrix (w->desired_matrix);
13287 rc = SCROLLING_FAILED;
13288 }
13289 else
13290 {
13291 /* Maybe forget recorded base line for line number display. */
13292 if (!just_this_one_p
13293 || current_buffer->clip_changed
13294 || BEG_UNCHANGED < CHARPOS (startp))
13295 w->base_line_number = Qnil;
13296
13297 /* If cursor ends up on a partially visible line,
13298 treat that as being off the bottom of the screen. */
13299 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13300 {
13301 clear_glyph_matrix (w->desired_matrix);
13302 ++extra_scroll_margin_lines;
13303 goto too_near_end;
13304 }
13305 rc = SCROLLING_SUCCESS;
13306 }
13307
13308 return rc;
13309 }
13310
13311
13312 /* Compute a suitable window start for window W if display of W starts
13313 on a continuation line. Value is non-zero if a new window start
13314 was computed.
13315
13316 The new window start will be computed, based on W's width, starting
13317 from the start of the continued line. It is the start of the
13318 screen line with the minimum distance from the old start W->start. */
13319
13320 static int
13321 compute_window_start_on_continuation_line (w)
13322 struct window *w;
13323 {
13324 struct text_pos pos, start_pos;
13325 int window_start_changed_p = 0;
13326
13327 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13328
13329 /* If window start is on a continuation line... Window start may be
13330 < BEGV in case there's invisible text at the start of the
13331 buffer (M-x rmail, for example). */
13332 if (CHARPOS (start_pos) > BEGV
13333 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13334 {
13335 struct it it;
13336 struct glyph_row *row;
13337
13338 /* Handle the case that the window start is out of range. */
13339 if (CHARPOS (start_pos) < BEGV)
13340 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13341 else if (CHARPOS (start_pos) > ZV)
13342 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13343
13344 /* Find the start of the continued line. This should be fast
13345 because scan_buffer is fast (newline cache). */
13346 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13347 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13348 row, DEFAULT_FACE_ID);
13349 reseat_at_previous_visible_line_start (&it);
13350
13351 /* If the line start is "too far" away from the window start,
13352 say it takes too much time to compute a new window start. */
13353 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13354 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13355 {
13356 int min_distance, distance;
13357
13358 /* Move forward by display lines to find the new window
13359 start. If window width was enlarged, the new start can
13360 be expected to be > the old start. If window width was
13361 decreased, the new window start will be < the old start.
13362 So, we're looking for the display line start with the
13363 minimum distance from the old window start. */
13364 pos = it.current.pos;
13365 min_distance = INFINITY;
13366 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13367 distance < min_distance)
13368 {
13369 min_distance = distance;
13370 pos = it.current.pos;
13371 move_it_by_lines (&it, 1, 0);
13372 }
13373
13374 /* Set the window start there. */
13375 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13376 window_start_changed_p = 1;
13377 }
13378 }
13379
13380 return window_start_changed_p;
13381 }
13382
13383
13384 /* Try cursor movement in case text has not changed in window WINDOW,
13385 with window start STARTP. Value is
13386
13387 CURSOR_MOVEMENT_SUCCESS if successful
13388
13389 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13390
13391 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13392 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13393 we want to scroll as if scroll-step were set to 1. See the code.
13394
13395 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13396 which case we have to abort this redisplay, and adjust matrices
13397 first. */
13398
13399 enum
13400 {
13401 CURSOR_MOVEMENT_SUCCESS,
13402 CURSOR_MOVEMENT_CANNOT_BE_USED,
13403 CURSOR_MOVEMENT_MUST_SCROLL,
13404 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13405 };
13406
13407 static int
13408 try_cursor_movement (window, startp, scroll_step)
13409 Lisp_Object window;
13410 struct text_pos startp;
13411 int *scroll_step;
13412 {
13413 struct window *w = XWINDOW (window);
13414 struct frame *f = XFRAME (w->frame);
13415 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13416
13417 #if GLYPH_DEBUG
13418 if (inhibit_try_cursor_movement)
13419 return rc;
13420 #endif
13421
13422 /* Handle case where text has not changed, only point, and it has
13423 not moved off the frame. */
13424 if (/* Point may be in this window. */
13425 PT >= CHARPOS (startp)
13426 /* Selective display hasn't changed. */
13427 && !current_buffer->clip_changed
13428 /* Function force-mode-line-update is used to force a thorough
13429 redisplay. It sets either windows_or_buffers_changed or
13430 update_mode_lines. So don't take a shortcut here for these
13431 cases. */
13432 && !update_mode_lines
13433 && !windows_or_buffers_changed
13434 && !cursor_type_changed
13435 /* Can't use this case if highlighting a region. When a
13436 region exists, cursor movement has to do more than just
13437 set the cursor. */
13438 && !(!NILP (Vtransient_mark_mode)
13439 && !NILP (current_buffer->mark_active))
13440 && NILP (w->region_showing)
13441 && NILP (Vshow_trailing_whitespace)
13442 /* Right after splitting windows, last_point may be nil. */
13443 && INTEGERP (w->last_point)
13444 /* This code is not used for mini-buffer for the sake of the case
13445 of redisplaying to replace an echo area message; since in
13446 that case the mini-buffer contents per se are usually
13447 unchanged. This code is of no real use in the mini-buffer
13448 since the handling of this_line_start_pos, etc., in redisplay
13449 handles the same cases. */
13450 && !EQ (window, minibuf_window)
13451 /* When splitting windows or for new windows, it happens that
13452 redisplay is called with a nil window_end_vpos or one being
13453 larger than the window. This should really be fixed in
13454 window.c. I don't have this on my list, now, so we do
13455 approximately the same as the old redisplay code. --gerd. */
13456 && INTEGERP (w->window_end_vpos)
13457 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13458 && (FRAME_WINDOW_P (f)
13459 || !overlay_arrow_in_current_buffer_p ()))
13460 {
13461 int this_scroll_margin, top_scroll_margin;
13462 struct glyph_row *row = NULL;
13463
13464 #if GLYPH_DEBUG
13465 debug_method_add (w, "cursor movement");
13466 #endif
13467
13468 /* Scroll if point within this distance from the top or bottom
13469 of the window. This is a pixel value. */
13470 if (scroll_margin > 0)
13471 {
13472 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13473 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13474 }
13475 else
13476 this_scroll_margin = 0;
13477
13478 top_scroll_margin = this_scroll_margin;
13479 if (WINDOW_WANTS_HEADER_LINE_P (w))
13480 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13481
13482 /* Start with the row the cursor was displayed during the last
13483 not paused redisplay. Give up if that row is not valid. */
13484 if (w->last_cursor.vpos < 0
13485 || w->last_cursor.vpos >= w->current_matrix->nrows)
13486 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13487 else
13488 {
13489 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13490 if (row->mode_line_p)
13491 ++row;
13492 if (!row->enabled_p)
13493 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13494 }
13495
13496 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13497 {
13498 int scroll_p = 0;
13499 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13500
13501 if (PT > XFASTINT (w->last_point))
13502 {
13503 /* Point has moved forward. */
13504 while (MATRIX_ROW_END_CHARPOS (row) < PT
13505 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13506 {
13507 xassert (row->enabled_p);
13508 ++row;
13509 }
13510
13511 /* The end position of a row equals the start position
13512 of the next row. If PT is there, we would rather
13513 display it in the next line. */
13514 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13515 && MATRIX_ROW_END_CHARPOS (row) == PT
13516 && !cursor_row_p (w, row))
13517 ++row;
13518
13519 /* If within the scroll margin, scroll. Note that
13520 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13521 the next line would be drawn, and that
13522 this_scroll_margin can be zero. */
13523 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13524 || PT > MATRIX_ROW_END_CHARPOS (row)
13525 /* Line is completely visible last line in window
13526 and PT is to be set in the next line. */
13527 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13528 && PT == MATRIX_ROW_END_CHARPOS (row)
13529 && !row->ends_at_zv_p
13530 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13531 scroll_p = 1;
13532 }
13533 else if (PT < XFASTINT (w->last_point))
13534 {
13535 /* Cursor has to be moved backward. Note that PT >=
13536 CHARPOS (startp) because of the outer if-statement. */
13537 while (!row->mode_line_p
13538 && (MATRIX_ROW_START_CHARPOS (row) > PT
13539 || (MATRIX_ROW_START_CHARPOS (row) == PT
13540 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13541 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13542 row > w->current_matrix->rows
13543 && (row-1)->ends_in_newline_from_string_p))))
13544 && (row->y > top_scroll_margin
13545 || CHARPOS (startp) == BEGV))
13546 {
13547 xassert (row->enabled_p);
13548 --row;
13549 }
13550
13551 /* Consider the following case: Window starts at BEGV,
13552 there is invisible, intangible text at BEGV, so that
13553 display starts at some point START > BEGV. It can
13554 happen that we are called with PT somewhere between
13555 BEGV and START. Try to handle that case. */
13556 if (row < w->current_matrix->rows
13557 || row->mode_line_p)
13558 {
13559 row = w->current_matrix->rows;
13560 if (row->mode_line_p)
13561 ++row;
13562 }
13563
13564 /* Due to newlines in overlay strings, we may have to
13565 skip forward over overlay strings. */
13566 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13567 && MATRIX_ROW_END_CHARPOS (row) == PT
13568 && !cursor_row_p (w, row))
13569 ++row;
13570
13571 /* If within the scroll margin, scroll. */
13572 if (row->y < top_scroll_margin
13573 && CHARPOS (startp) != BEGV)
13574 scroll_p = 1;
13575 }
13576 else
13577 {
13578 /* Cursor did not move. So don't scroll even if cursor line
13579 is partially visible, as it was so before. */
13580 rc = CURSOR_MOVEMENT_SUCCESS;
13581 }
13582
13583 if (PT < MATRIX_ROW_START_CHARPOS (row)
13584 || PT > MATRIX_ROW_END_CHARPOS (row))
13585 {
13586 /* if PT is not in the glyph row, give up. */
13587 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13588 }
13589 else if (rc != CURSOR_MOVEMENT_SUCCESS
13590 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13591 && make_cursor_line_fully_visible_p)
13592 {
13593 if (PT == MATRIX_ROW_END_CHARPOS (row)
13594 && !row->ends_at_zv_p
13595 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13596 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13597 else if (row->height > window_box_height (w))
13598 {
13599 /* If we end up in a partially visible line, let's
13600 make it fully visible, except when it's taller
13601 than the window, in which case we can't do much
13602 about it. */
13603 *scroll_step = 1;
13604 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13605 }
13606 else
13607 {
13608 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13609 if (!cursor_row_fully_visible_p (w, 0, 1))
13610 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13611 else
13612 rc = CURSOR_MOVEMENT_SUCCESS;
13613 }
13614 }
13615 else if (scroll_p)
13616 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13617 else
13618 {
13619 do
13620 {
13621 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13622 {
13623 rc = CURSOR_MOVEMENT_SUCCESS;
13624 break;
13625 }
13626 ++row;
13627 }
13628 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13629 && MATRIX_ROW_START_CHARPOS (row) == PT
13630 && cursor_row_p (w, row));
13631 }
13632 }
13633 }
13634
13635 return rc;
13636 }
13637
13638 void
13639 set_vertical_scroll_bar (w)
13640 struct window *w;
13641 {
13642 int start, end, whole;
13643
13644 /* Calculate the start and end positions for the current window.
13645 At some point, it would be nice to choose between scrollbars
13646 which reflect the whole buffer size, with special markers
13647 indicating narrowing, and scrollbars which reflect only the
13648 visible region.
13649
13650 Note that mini-buffers sometimes aren't displaying any text. */
13651 if (!MINI_WINDOW_P (w)
13652 || (w == XWINDOW (minibuf_window)
13653 && NILP (echo_area_buffer[0])))
13654 {
13655 struct buffer *buf = XBUFFER (w->buffer);
13656 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13657 start = marker_position (w->start) - BUF_BEGV (buf);
13658 /* I don't think this is guaranteed to be right. For the
13659 moment, we'll pretend it is. */
13660 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13661
13662 if (end < start)
13663 end = start;
13664 if (whole < (end - start))
13665 whole = end - start;
13666 }
13667 else
13668 start = end = whole = 0;
13669
13670 /* Indicate what this scroll bar ought to be displaying now. */
13671 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13672 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13673 (w, end - start, whole, start);
13674 }
13675
13676
13677 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13678 selected_window is redisplayed.
13679
13680 We can return without actually redisplaying the window if
13681 fonts_changed_p is nonzero. In that case, redisplay_internal will
13682 retry. */
13683
13684 static void
13685 redisplay_window (window, just_this_one_p)
13686 Lisp_Object window;
13687 int just_this_one_p;
13688 {
13689 struct window *w = XWINDOW (window);
13690 struct frame *f = XFRAME (w->frame);
13691 struct buffer *buffer = XBUFFER (w->buffer);
13692 struct buffer *old = current_buffer;
13693 struct text_pos lpoint, opoint, startp;
13694 int update_mode_line;
13695 int tem;
13696 struct it it;
13697 /* Record it now because it's overwritten. */
13698 int current_matrix_up_to_date_p = 0;
13699 int used_current_matrix_p = 0;
13700 /* This is less strict than current_matrix_up_to_date_p.
13701 It indictes that the buffer contents and narrowing are unchanged. */
13702 int buffer_unchanged_p = 0;
13703 int temp_scroll_step = 0;
13704 int count = SPECPDL_INDEX ();
13705 int rc;
13706 int centering_position = -1;
13707 int last_line_misfit = 0;
13708 int beg_unchanged, end_unchanged;
13709
13710 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13711 opoint = lpoint;
13712
13713 /* W must be a leaf window here. */
13714 xassert (!NILP (w->buffer));
13715 #if GLYPH_DEBUG
13716 *w->desired_matrix->method = 0;
13717 #endif
13718
13719 restart:
13720 reconsider_clip_changes (w, buffer);
13721
13722 /* Has the mode line to be updated? */
13723 update_mode_line = (!NILP (w->update_mode_line)
13724 || update_mode_lines
13725 || buffer->clip_changed
13726 || buffer->prevent_redisplay_optimizations_p);
13727
13728 if (MINI_WINDOW_P (w))
13729 {
13730 if (w == XWINDOW (echo_area_window)
13731 && !NILP (echo_area_buffer[0]))
13732 {
13733 if (update_mode_line)
13734 /* We may have to update a tty frame's menu bar or a
13735 tool-bar. Example `M-x C-h C-h C-g'. */
13736 goto finish_menu_bars;
13737 else
13738 /* We've already displayed the echo area glyphs in this window. */
13739 goto finish_scroll_bars;
13740 }
13741 else if ((w != XWINDOW (minibuf_window)
13742 || minibuf_level == 0)
13743 /* When buffer is nonempty, redisplay window normally. */
13744 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13745 /* Quail displays non-mini buffers in minibuffer window.
13746 In that case, redisplay the window normally. */
13747 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13748 {
13749 /* W is a mini-buffer window, but it's not active, so clear
13750 it. */
13751 int yb = window_text_bottom_y (w);
13752 struct glyph_row *row;
13753 int y;
13754
13755 for (y = 0, row = w->desired_matrix->rows;
13756 y < yb;
13757 y += row->height, ++row)
13758 blank_row (w, row, y);
13759 goto finish_scroll_bars;
13760 }
13761
13762 clear_glyph_matrix (w->desired_matrix);
13763 }
13764
13765 /* Otherwise set up data on this window; select its buffer and point
13766 value. */
13767 /* Really select the buffer, for the sake of buffer-local
13768 variables. */
13769 set_buffer_internal_1 (XBUFFER (w->buffer));
13770
13771 current_matrix_up_to_date_p
13772 = (!NILP (w->window_end_valid)
13773 && !current_buffer->clip_changed
13774 && !current_buffer->prevent_redisplay_optimizations_p
13775 && XFASTINT (w->last_modified) >= MODIFF
13776 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13777
13778 /* Run the window-bottom-change-functions
13779 if it is possible that the text on the screen has changed
13780 (either due to modification of the text, or any other reason). */
13781 if (!current_matrix_up_to_date_p
13782 && !NILP (Vwindow_text_change_functions))
13783 {
13784 safe_run_hooks (Qwindow_text_change_functions);
13785 goto restart;
13786 }
13787
13788 beg_unchanged = BEG_UNCHANGED;
13789 end_unchanged = END_UNCHANGED;
13790
13791 SET_TEXT_POS (opoint, PT, PT_BYTE);
13792
13793 specbind (Qinhibit_point_motion_hooks, Qt);
13794
13795 buffer_unchanged_p
13796 = (!NILP (w->window_end_valid)
13797 && !current_buffer->clip_changed
13798 && XFASTINT (w->last_modified) >= MODIFF
13799 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13800
13801 /* When windows_or_buffers_changed is non-zero, we can't rely on
13802 the window end being valid, so set it to nil there. */
13803 if (windows_or_buffers_changed)
13804 {
13805 /* If window starts on a continuation line, maybe adjust the
13806 window start in case the window's width changed. */
13807 if (XMARKER (w->start)->buffer == current_buffer)
13808 compute_window_start_on_continuation_line (w);
13809
13810 w->window_end_valid = Qnil;
13811 }
13812
13813 /* Some sanity checks. */
13814 CHECK_WINDOW_END (w);
13815 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13816 abort ();
13817 if (BYTEPOS (opoint) < CHARPOS (opoint))
13818 abort ();
13819
13820 /* If %c is in mode line, update it if needed. */
13821 if (!NILP (w->column_number_displayed)
13822 /* This alternative quickly identifies a common case
13823 where no change is needed. */
13824 && !(PT == XFASTINT (w->last_point)
13825 && XFASTINT (w->last_modified) >= MODIFF
13826 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13827 && (XFASTINT (w->column_number_displayed)
13828 != (int) current_column ())) /* iftc */
13829 update_mode_line = 1;
13830
13831 /* Count number of windows showing the selected buffer. An indirect
13832 buffer counts as its base buffer. */
13833 if (!just_this_one_p)
13834 {
13835 struct buffer *current_base, *window_base;
13836 current_base = current_buffer;
13837 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13838 if (current_base->base_buffer)
13839 current_base = current_base->base_buffer;
13840 if (window_base->base_buffer)
13841 window_base = window_base->base_buffer;
13842 if (current_base == window_base)
13843 buffer_shared++;
13844 }
13845
13846 /* Point refers normally to the selected window. For any other
13847 window, set up appropriate value. */
13848 if (!EQ (window, selected_window))
13849 {
13850 int new_pt = XMARKER (w->pointm)->charpos;
13851 int new_pt_byte = marker_byte_position (w->pointm);
13852 if (new_pt < BEGV)
13853 {
13854 new_pt = BEGV;
13855 new_pt_byte = BEGV_BYTE;
13856 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13857 }
13858 else if (new_pt > (ZV - 1))
13859 {
13860 new_pt = ZV;
13861 new_pt_byte = ZV_BYTE;
13862 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13863 }
13864
13865 /* We don't use SET_PT so that the point-motion hooks don't run. */
13866 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13867 }
13868
13869 /* If any of the character widths specified in the display table
13870 have changed, invalidate the width run cache. It's true that
13871 this may be a bit late to catch such changes, but the rest of
13872 redisplay goes (non-fatally) haywire when the display table is
13873 changed, so why should we worry about doing any better? */
13874 if (current_buffer->width_run_cache)
13875 {
13876 struct Lisp_Char_Table *disptab = buffer_display_table ();
13877
13878 if (! disptab_matches_widthtab (disptab,
13879 XVECTOR (current_buffer->width_table)))
13880 {
13881 invalidate_region_cache (current_buffer,
13882 current_buffer->width_run_cache,
13883 BEG, Z);
13884 recompute_width_table (current_buffer, disptab);
13885 }
13886 }
13887
13888 /* If window-start is screwed up, choose a new one. */
13889 if (XMARKER (w->start)->buffer != current_buffer)
13890 goto recenter;
13891
13892 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13893
13894 /* If someone specified a new starting point but did not insist,
13895 check whether it can be used. */
13896 if (!NILP (w->optional_new_start)
13897 && CHARPOS (startp) >= BEGV
13898 && CHARPOS (startp) <= ZV)
13899 {
13900 w->optional_new_start = Qnil;
13901 start_display (&it, w, startp);
13902 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13903 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13904 if (IT_CHARPOS (it) == PT)
13905 w->force_start = Qt;
13906 /* IT may overshoot PT if text at PT is invisible. */
13907 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13908 w->force_start = Qt;
13909 }
13910
13911 force_start:
13912
13913 /* Handle case where place to start displaying has been specified,
13914 unless the specified location is outside the accessible range. */
13915 if (!NILP (w->force_start)
13916 || w->frozen_window_start_p)
13917 {
13918 /* We set this later on if we have to adjust point. */
13919 int new_vpos = -1;
13920
13921 w->force_start = Qnil;
13922 w->vscroll = 0;
13923 w->window_end_valid = Qnil;
13924
13925 /* Forget any recorded base line for line number display. */
13926 if (!buffer_unchanged_p)
13927 w->base_line_number = Qnil;
13928
13929 /* Redisplay the mode line. Select the buffer properly for that.
13930 Also, run the hook window-scroll-functions
13931 because we have scrolled. */
13932 /* Note, we do this after clearing force_start because
13933 if there's an error, it is better to forget about force_start
13934 than to get into an infinite loop calling the hook functions
13935 and having them get more errors. */
13936 if (!update_mode_line
13937 || ! NILP (Vwindow_scroll_functions))
13938 {
13939 update_mode_line = 1;
13940 w->update_mode_line = Qt;
13941 startp = run_window_scroll_functions (window, startp);
13942 }
13943
13944 w->last_modified = make_number (0);
13945 w->last_overlay_modified = make_number (0);
13946 if (CHARPOS (startp) < BEGV)
13947 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13948 else if (CHARPOS (startp) > ZV)
13949 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13950
13951 /* Redisplay, then check if cursor has been set during the
13952 redisplay. Give up if new fonts were loaded. */
13953 /* We used to issue a CHECK_MARGINS argument to try_window here,
13954 but this causes scrolling to fail when point begins inside
13955 the scroll margin (bug#148) -- cyd */
13956 if (!try_window (window, startp, 0))
13957 {
13958 w->force_start = Qt;
13959 clear_glyph_matrix (w->desired_matrix);
13960 goto need_larger_matrices;
13961 }
13962
13963 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13964 {
13965 /* If point does not appear, try to move point so it does
13966 appear. The desired matrix has been built above, so we
13967 can use it here. */
13968 new_vpos = window_box_height (w) / 2;
13969 }
13970
13971 if (!cursor_row_fully_visible_p (w, 0, 0))
13972 {
13973 /* Point does appear, but on a line partly visible at end of window.
13974 Move it back to a fully-visible line. */
13975 new_vpos = window_box_height (w);
13976 }
13977
13978 /* If we need to move point for either of the above reasons,
13979 now actually do it. */
13980 if (new_vpos >= 0)
13981 {
13982 struct glyph_row *row;
13983
13984 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13985 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13986 ++row;
13987
13988 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13989 MATRIX_ROW_START_BYTEPOS (row));
13990
13991 if (w != XWINDOW (selected_window))
13992 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13993 else if (current_buffer == old)
13994 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13995
13996 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13997
13998 /* If we are highlighting the region, then we just changed
13999 the region, so redisplay to show it. */
14000 if (!NILP (Vtransient_mark_mode)
14001 && !NILP (current_buffer->mark_active))
14002 {
14003 clear_glyph_matrix (w->desired_matrix);
14004 if (!try_window (window, startp, 0))
14005 goto need_larger_matrices;
14006 }
14007 }
14008
14009 #if GLYPH_DEBUG
14010 debug_method_add (w, "forced window start");
14011 #endif
14012 goto done;
14013 }
14014
14015 /* Handle case where text has not changed, only point, and it has
14016 not moved off the frame, and we are not retrying after hscroll.
14017 (current_matrix_up_to_date_p is nonzero when retrying.) */
14018 if (current_matrix_up_to_date_p
14019 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14020 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14021 {
14022 switch (rc)
14023 {
14024 case CURSOR_MOVEMENT_SUCCESS:
14025 used_current_matrix_p = 1;
14026 goto done;
14027
14028 case CURSOR_MOVEMENT_MUST_SCROLL:
14029 goto try_to_scroll;
14030
14031 default:
14032 abort ();
14033 }
14034 }
14035 /* If current starting point was originally the beginning of a line
14036 but no longer is, find a new starting point. */
14037 else if (!NILP (w->start_at_line_beg)
14038 && !(CHARPOS (startp) <= BEGV
14039 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14040 {
14041 #if GLYPH_DEBUG
14042 debug_method_add (w, "recenter 1");
14043 #endif
14044 goto recenter;
14045 }
14046
14047 /* Try scrolling with try_window_id. Value is > 0 if update has
14048 been done, it is -1 if we know that the same window start will
14049 not work. It is 0 if unsuccessful for some other reason. */
14050 else if ((tem = try_window_id (w)) != 0)
14051 {
14052 #if GLYPH_DEBUG
14053 debug_method_add (w, "try_window_id %d", tem);
14054 #endif
14055
14056 if (fonts_changed_p)
14057 goto need_larger_matrices;
14058 if (tem > 0)
14059 goto done;
14060
14061 /* Otherwise try_window_id has returned -1 which means that we
14062 don't want the alternative below this comment to execute. */
14063 }
14064 else if (CHARPOS (startp) >= BEGV
14065 && CHARPOS (startp) <= ZV
14066 && PT >= CHARPOS (startp)
14067 && (CHARPOS (startp) < ZV
14068 /* Avoid starting at end of buffer. */
14069 || CHARPOS (startp) == BEGV
14070 || (XFASTINT (w->last_modified) >= MODIFF
14071 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14072 {
14073
14074 /* If first window line is a continuation line, and window start
14075 is inside the modified region, but the first change is before
14076 current window start, we must select a new window start.
14077
14078 However, if this is the result of a down-mouse event (e.g. by
14079 extending the mouse-drag-overlay), we don't want to select a
14080 new window start, since that would change the position under
14081 the mouse, resulting in an unwanted mouse-movement rather
14082 than a simple mouse-click. */
14083 if (NILP (w->start_at_line_beg)
14084 && NILP (do_mouse_tracking)
14085 && CHARPOS (startp) > BEGV
14086 && CHARPOS (startp) > BEG + beg_unchanged
14087 && CHARPOS (startp) <= Z - end_unchanged
14088 /* Even if w->start_at_line_beg is nil, a new window may
14089 start at a line_beg, since that's how set_buffer_window
14090 sets it. So, we need to check the return value of
14091 compute_window_start_on_continuation_line. (See also
14092 bug#197). */
14093 && XMARKER (w->start)->buffer == current_buffer
14094 && compute_window_start_on_continuation_line (w))
14095 {
14096 w->force_start = Qt;
14097 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14098 goto force_start;
14099 }
14100
14101 #if GLYPH_DEBUG
14102 debug_method_add (w, "same window start");
14103 #endif
14104
14105 /* Try to redisplay starting at same place as before.
14106 If point has not moved off frame, accept the results. */
14107 if (!current_matrix_up_to_date_p
14108 /* Don't use try_window_reusing_current_matrix in this case
14109 because a window scroll function can have changed the
14110 buffer. */
14111 || !NILP (Vwindow_scroll_functions)
14112 || MINI_WINDOW_P (w)
14113 || !(used_current_matrix_p
14114 = try_window_reusing_current_matrix (w)))
14115 {
14116 IF_DEBUG (debug_method_add (w, "1"));
14117 if (try_window (window, startp, 1) < 0)
14118 /* -1 means we need to scroll.
14119 0 means we need new matrices, but fonts_changed_p
14120 is set in that case, so we will detect it below. */
14121 goto try_to_scroll;
14122 }
14123
14124 if (fonts_changed_p)
14125 goto need_larger_matrices;
14126
14127 if (w->cursor.vpos >= 0)
14128 {
14129 if (!just_this_one_p
14130 || current_buffer->clip_changed
14131 || BEG_UNCHANGED < CHARPOS (startp))
14132 /* Forget any recorded base line for line number display. */
14133 w->base_line_number = Qnil;
14134
14135 if (!cursor_row_fully_visible_p (w, 1, 0))
14136 {
14137 clear_glyph_matrix (w->desired_matrix);
14138 last_line_misfit = 1;
14139 }
14140 /* Drop through and scroll. */
14141 else
14142 goto done;
14143 }
14144 else
14145 clear_glyph_matrix (w->desired_matrix);
14146 }
14147
14148 try_to_scroll:
14149
14150 w->last_modified = make_number (0);
14151 w->last_overlay_modified = make_number (0);
14152
14153 /* Redisplay the mode line. Select the buffer properly for that. */
14154 if (!update_mode_line)
14155 {
14156 update_mode_line = 1;
14157 w->update_mode_line = Qt;
14158 }
14159
14160 /* Try to scroll by specified few lines. */
14161 if ((scroll_conservatively
14162 || scroll_step
14163 || temp_scroll_step
14164 || NUMBERP (current_buffer->scroll_up_aggressively)
14165 || NUMBERP (current_buffer->scroll_down_aggressively))
14166 && !current_buffer->clip_changed
14167 && CHARPOS (startp) >= BEGV
14168 && CHARPOS (startp) <= ZV)
14169 {
14170 /* The function returns -1 if new fonts were loaded, 1 if
14171 successful, 0 if not successful. */
14172 int rc = try_scrolling (window, just_this_one_p,
14173 scroll_conservatively,
14174 scroll_step,
14175 temp_scroll_step, last_line_misfit);
14176 switch (rc)
14177 {
14178 case SCROLLING_SUCCESS:
14179 goto done;
14180
14181 case SCROLLING_NEED_LARGER_MATRICES:
14182 goto need_larger_matrices;
14183
14184 case SCROLLING_FAILED:
14185 break;
14186
14187 default:
14188 abort ();
14189 }
14190 }
14191
14192 /* Finally, just choose place to start which centers point */
14193
14194 recenter:
14195 if (centering_position < 0)
14196 centering_position = window_box_height (w) / 2;
14197
14198 #if GLYPH_DEBUG
14199 debug_method_add (w, "recenter");
14200 #endif
14201
14202 /* w->vscroll = 0; */
14203
14204 /* Forget any previously recorded base line for line number display. */
14205 if (!buffer_unchanged_p)
14206 w->base_line_number = Qnil;
14207
14208 /* Move backward half the height of the window. */
14209 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14210 it.current_y = it.last_visible_y;
14211 move_it_vertically_backward (&it, centering_position);
14212 xassert (IT_CHARPOS (it) >= BEGV);
14213
14214 /* The function move_it_vertically_backward may move over more
14215 than the specified y-distance. If it->w is small, e.g. a
14216 mini-buffer window, we may end up in front of the window's
14217 display area. Start displaying at the start of the line
14218 containing PT in this case. */
14219 if (it.current_y <= 0)
14220 {
14221 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14222 move_it_vertically_backward (&it, 0);
14223 it.current_y = 0;
14224 }
14225
14226 it.current_x = it.hpos = 0;
14227
14228 /* Set startp here explicitly in case that helps avoid an infinite loop
14229 in case the window-scroll-functions functions get errors. */
14230 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14231
14232 /* Run scroll hooks. */
14233 startp = run_window_scroll_functions (window, it.current.pos);
14234
14235 /* Redisplay the window. */
14236 if (!current_matrix_up_to_date_p
14237 || windows_or_buffers_changed
14238 || cursor_type_changed
14239 /* Don't use try_window_reusing_current_matrix in this case
14240 because it can have changed the buffer. */
14241 || !NILP (Vwindow_scroll_functions)
14242 || !just_this_one_p
14243 || MINI_WINDOW_P (w)
14244 || !(used_current_matrix_p
14245 = try_window_reusing_current_matrix (w)))
14246 try_window (window, startp, 0);
14247
14248 /* If new fonts have been loaded (due to fontsets), give up. We
14249 have to start a new redisplay since we need to re-adjust glyph
14250 matrices. */
14251 if (fonts_changed_p)
14252 goto need_larger_matrices;
14253
14254 /* If cursor did not appear assume that the middle of the window is
14255 in the first line of the window. Do it again with the next line.
14256 (Imagine a window of height 100, displaying two lines of height
14257 60. Moving back 50 from it->last_visible_y will end in the first
14258 line.) */
14259 if (w->cursor.vpos < 0)
14260 {
14261 if (!NILP (w->window_end_valid)
14262 && PT >= Z - XFASTINT (w->window_end_pos))
14263 {
14264 clear_glyph_matrix (w->desired_matrix);
14265 move_it_by_lines (&it, 1, 0);
14266 try_window (window, it.current.pos, 0);
14267 }
14268 else if (PT < IT_CHARPOS (it))
14269 {
14270 clear_glyph_matrix (w->desired_matrix);
14271 move_it_by_lines (&it, -1, 0);
14272 try_window (window, it.current.pos, 0);
14273 }
14274 else
14275 {
14276 /* Not much we can do about it. */
14277 }
14278 }
14279
14280 /* Consider the following case: Window starts at BEGV, there is
14281 invisible, intangible text at BEGV, so that display starts at
14282 some point START > BEGV. It can happen that we are called with
14283 PT somewhere between BEGV and START. Try to handle that case. */
14284 if (w->cursor.vpos < 0)
14285 {
14286 struct glyph_row *row = w->current_matrix->rows;
14287 if (row->mode_line_p)
14288 ++row;
14289 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14290 }
14291
14292 if (!cursor_row_fully_visible_p (w, 0, 0))
14293 {
14294 /* If vscroll is enabled, disable it and try again. */
14295 if (w->vscroll)
14296 {
14297 w->vscroll = 0;
14298 clear_glyph_matrix (w->desired_matrix);
14299 goto recenter;
14300 }
14301
14302 /* If centering point failed to make the whole line visible,
14303 put point at the top instead. That has to make the whole line
14304 visible, if it can be done. */
14305 if (centering_position == 0)
14306 goto done;
14307
14308 clear_glyph_matrix (w->desired_matrix);
14309 centering_position = 0;
14310 goto recenter;
14311 }
14312
14313 done:
14314
14315 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14316 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14317 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14318 ? Qt : Qnil);
14319
14320 /* Display the mode line, if we must. */
14321 if ((update_mode_line
14322 /* If window not full width, must redo its mode line
14323 if (a) the window to its side is being redone and
14324 (b) we do a frame-based redisplay. This is a consequence
14325 of how inverted lines are drawn in frame-based redisplay. */
14326 || (!just_this_one_p
14327 && !FRAME_WINDOW_P (f)
14328 && !WINDOW_FULL_WIDTH_P (w))
14329 /* Line number to display. */
14330 || INTEGERP (w->base_line_pos)
14331 /* Column number is displayed and different from the one displayed. */
14332 || (!NILP (w->column_number_displayed)
14333 && (XFASTINT (w->column_number_displayed)
14334 != (int) current_column ()))) /* iftc */
14335 /* This means that the window has a mode line. */
14336 && (WINDOW_WANTS_MODELINE_P (w)
14337 || WINDOW_WANTS_HEADER_LINE_P (w)))
14338 {
14339 display_mode_lines (w);
14340
14341 /* If mode line height has changed, arrange for a thorough
14342 immediate redisplay using the correct mode line height. */
14343 if (WINDOW_WANTS_MODELINE_P (w)
14344 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14345 {
14346 fonts_changed_p = 1;
14347 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14348 = DESIRED_MODE_LINE_HEIGHT (w);
14349 }
14350
14351 /* If header line height has changed, arrange for a thorough
14352 immediate redisplay using the correct header line height. */
14353 if (WINDOW_WANTS_HEADER_LINE_P (w)
14354 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14355 {
14356 fonts_changed_p = 1;
14357 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14358 = DESIRED_HEADER_LINE_HEIGHT (w);
14359 }
14360
14361 if (fonts_changed_p)
14362 goto need_larger_matrices;
14363 }
14364
14365 if (!line_number_displayed
14366 && !BUFFERP (w->base_line_pos))
14367 {
14368 w->base_line_pos = Qnil;
14369 w->base_line_number = Qnil;
14370 }
14371
14372 finish_menu_bars:
14373
14374 /* When we reach a frame's selected window, redo the frame's menu bar. */
14375 if (update_mode_line
14376 && EQ (FRAME_SELECTED_WINDOW (f), window))
14377 {
14378 int redisplay_menu_p = 0;
14379 int redisplay_tool_bar_p = 0;
14380
14381 if (FRAME_WINDOW_P (f))
14382 {
14383 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14384 || defined (HAVE_NS) || defined (USE_GTK)
14385 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14386 #else
14387 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14388 #endif
14389 }
14390 else
14391 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14392
14393 if (redisplay_menu_p)
14394 display_menu_bar (w);
14395
14396 #ifdef HAVE_WINDOW_SYSTEM
14397 if (FRAME_WINDOW_P (f))
14398 {
14399 #if defined (USE_GTK) || defined (HAVE_NS)
14400 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14401 #else
14402 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14403 && (FRAME_TOOL_BAR_LINES (f) > 0
14404 || !NILP (Vauto_resize_tool_bars));
14405 #endif
14406
14407 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14408 {
14409 extern int ignore_mouse_drag_p;
14410 ignore_mouse_drag_p = 1;
14411 }
14412 }
14413 #endif
14414 }
14415
14416 #ifdef HAVE_WINDOW_SYSTEM
14417 if (FRAME_WINDOW_P (f)
14418 && update_window_fringes (w, (just_this_one_p
14419 || (!used_current_matrix_p && !overlay_arrow_seen)
14420 || w->pseudo_window_p)))
14421 {
14422 update_begin (f);
14423 BLOCK_INPUT;
14424 if (draw_window_fringes (w, 1))
14425 x_draw_vertical_border (w);
14426 UNBLOCK_INPUT;
14427 update_end (f);
14428 }
14429 #endif /* HAVE_WINDOW_SYSTEM */
14430
14431 /* We go to this label, with fonts_changed_p nonzero,
14432 if it is necessary to try again using larger glyph matrices.
14433 We have to redeem the scroll bar even in this case,
14434 because the loop in redisplay_internal expects that. */
14435 need_larger_matrices:
14436 ;
14437 finish_scroll_bars:
14438
14439 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14440 {
14441 /* Set the thumb's position and size. */
14442 set_vertical_scroll_bar (w);
14443
14444 /* Note that we actually used the scroll bar attached to this
14445 window, so it shouldn't be deleted at the end of redisplay. */
14446 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14447 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14448 }
14449
14450 /* Restore current_buffer and value of point in it. */
14451 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14452 set_buffer_internal_1 (old);
14453 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14454 shorter. This can be caused by log truncation in *Messages*. */
14455 if (CHARPOS (lpoint) <= ZV)
14456 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14457
14458 unbind_to (count, Qnil);
14459 }
14460
14461
14462 /* Build the complete desired matrix of WINDOW with a window start
14463 buffer position POS.
14464
14465 Value is 1 if successful. It is zero if fonts were loaded during
14466 redisplay which makes re-adjusting glyph matrices necessary, and -1
14467 if point would appear in the scroll margins.
14468 (We check that only if CHECK_MARGINS is nonzero. */
14469
14470 int
14471 try_window (window, pos, check_margins)
14472 Lisp_Object window;
14473 struct text_pos pos;
14474 int check_margins;
14475 {
14476 struct window *w = XWINDOW (window);
14477 struct it it;
14478 struct glyph_row *last_text_row = NULL;
14479 struct frame *f = XFRAME (w->frame);
14480
14481 /* Make POS the new window start. */
14482 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14483
14484 /* Mark cursor position as unknown. No overlay arrow seen. */
14485 w->cursor.vpos = -1;
14486 overlay_arrow_seen = 0;
14487
14488 /* Initialize iterator and info to start at POS. */
14489 start_display (&it, w, pos);
14490
14491 /* Display all lines of W. */
14492 while (it.current_y < it.last_visible_y)
14493 {
14494 if (display_line (&it))
14495 last_text_row = it.glyph_row - 1;
14496 if (fonts_changed_p)
14497 return 0;
14498 }
14499
14500 /* Don't let the cursor end in the scroll margins. */
14501 if (check_margins
14502 && !MINI_WINDOW_P (w))
14503 {
14504 int this_scroll_margin;
14505
14506 if (scroll_margin > 0)
14507 {
14508 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14509 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14510 }
14511 else
14512 this_scroll_margin = 0;
14513
14514 if ((w->cursor.y >= 0 /* not vscrolled */
14515 && w->cursor.y < this_scroll_margin
14516 && CHARPOS (pos) > BEGV
14517 && IT_CHARPOS (it) < ZV)
14518 /* rms: considering make_cursor_line_fully_visible_p here
14519 seems to give wrong results. We don't want to recenter
14520 when the last line is partly visible, we want to allow
14521 that case to be handled in the usual way. */
14522 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14523 {
14524 w->cursor.vpos = -1;
14525 clear_glyph_matrix (w->desired_matrix);
14526 return -1;
14527 }
14528 }
14529
14530 /* If bottom moved off end of frame, change mode line percentage. */
14531 if (XFASTINT (w->window_end_pos) <= 0
14532 && Z != IT_CHARPOS (it))
14533 w->update_mode_line = Qt;
14534
14535 /* Set window_end_pos to the offset of the last character displayed
14536 on the window from the end of current_buffer. Set
14537 window_end_vpos to its row number. */
14538 if (last_text_row)
14539 {
14540 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14541 w->window_end_bytepos
14542 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14543 w->window_end_pos
14544 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14545 w->window_end_vpos
14546 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14547 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14548 ->displays_text_p);
14549 }
14550 else
14551 {
14552 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14553 w->window_end_pos = make_number (Z - ZV);
14554 w->window_end_vpos = make_number (0);
14555 }
14556
14557 /* But that is not valid info until redisplay finishes. */
14558 w->window_end_valid = Qnil;
14559 return 1;
14560 }
14561
14562
14563 \f
14564 /************************************************************************
14565 Window redisplay reusing current matrix when buffer has not changed
14566 ************************************************************************/
14567
14568 /* Try redisplay of window W showing an unchanged buffer with a
14569 different window start than the last time it was displayed by
14570 reusing its current matrix. Value is non-zero if successful.
14571 W->start is the new window start. */
14572
14573 static int
14574 try_window_reusing_current_matrix (w)
14575 struct window *w;
14576 {
14577 struct frame *f = XFRAME (w->frame);
14578 struct glyph_row *row, *bottom_row;
14579 struct it it;
14580 struct run run;
14581 struct text_pos start, new_start;
14582 int nrows_scrolled, i;
14583 struct glyph_row *last_text_row;
14584 struct glyph_row *last_reused_text_row;
14585 struct glyph_row *start_row;
14586 int start_vpos, min_y, max_y;
14587
14588 #if GLYPH_DEBUG
14589 if (inhibit_try_window_reusing)
14590 return 0;
14591 #endif
14592
14593 if (/* This function doesn't handle terminal frames. */
14594 !FRAME_WINDOW_P (f)
14595 /* Don't try to reuse the display if windows have been split
14596 or such. */
14597 || windows_or_buffers_changed
14598 || cursor_type_changed)
14599 return 0;
14600
14601 /* Can't do this if region may have changed. */
14602 if ((!NILP (Vtransient_mark_mode)
14603 && !NILP (current_buffer->mark_active))
14604 || !NILP (w->region_showing)
14605 || !NILP (Vshow_trailing_whitespace))
14606 return 0;
14607
14608 /* If top-line visibility has changed, give up. */
14609 if (WINDOW_WANTS_HEADER_LINE_P (w)
14610 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14611 return 0;
14612
14613 /* Give up if old or new display is scrolled vertically. We could
14614 make this function handle this, but right now it doesn't. */
14615 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14616 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14617 return 0;
14618
14619 /* The variable new_start now holds the new window start. The old
14620 start `start' can be determined from the current matrix. */
14621 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14622 start = start_row->start.pos;
14623 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14624
14625 /* Clear the desired matrix for the display below. */
14626 clear_glyph_matrix (w->desired_matrix);
14627
14628 if (CHARPOS (new_start) <= CHARPOS (start))
14629 {
14630 int first_row_y;
14631
14632 /* Don't use this method if the display starts with an ellipsis
14633 displayed for invisible text. It's not easy to handle that case
14634 below, and it's certainly not worth the effort since this is
14635 not a frequent case. */
14636 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14637 return 0;
14638
14639 IF_DEBUG (debug_method_add (w, "twu1"));
14640
14641 /* Display up to a row that can be reused. The variable
14642 last_text_row is set to the last row displayed that displays
14643 text. Note that it.vpos == 0 if or if not there is a
14644 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14645 start_display (&it, w, new_start);
14646 first_row_y = it.current_y;
14647 w->cursor.vpos = -1;
14648 last_text_row = last_reused_text_row = NULL;
14649
14650 while (it.current_y < it.last_visible_y
14651 && !fonts_changed_p)
14652 {
14653 /* If we have reached into the characters in the START row,
14654 that means the line boundaries have changed. So we
14655 can't start copying with the row START. Maybe it will
14656 work to start copying with the following row. */
14657 while (IT_CHARPOS (it) > CHARPOS (start))
14658 {
14659 /* Advance to the next row as the "start". */
14660 start_row++;
14661 start = start_row->start.pos;
14662 /* If there are no more rows to try, or just one, give up. */
14663 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14664 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14665 || CHARPOS (start) == ZV)
14666 {
14667 clear_glyph_matrix (w->desired_matrix);
14668 return 0;
14669 }
14670
14671 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14672 }
14673 /* If we have reached alignment,
14674 we can copy the rest of the rows. */
14675 if (IT_CHARPOS (it) == CHARPOS (start))
14676 break;
14677
14678 if (display_line (&it))
14679 last_text_row = it.glyph_row - 1;
14680 }
14681
14682 /* A value of current_y < last_visible_y means that we stopped
14683 at the previous window start, which in turn means that we
14684 have at least one reusable row. */
14685 if (it.current_y < it.last_visible_y)
14686 {
14687 /* IT.vpos always starts from 0; it counts text lines. */
14688 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14689
14690 /* Find PT if not already found in the lines displayed. */
14691 if (w->cursor.vpos < 0)
14692 {
14693 int dy = it.current_y - start_row->y;
14694
14695 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14696 row = row_containing_pos (w, PT, row, NULL, dy);
14697 if (row)
14698 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14699 dy, nrows_scrolled);
14700 else
14701 {
14702 clear_glyph_matrix (w->desired_matrix);
14703 return 0;
14704 }
14705 }
14706
14707 /* Scroll the display. Do it before the current matrix is
14708 changed. The problem here is that update has not yet
14709 run, i.e. part of the current matrix is not up to date.
14710 scroll_run_hook will clear the cursor, and use the
14711 current matrix to get the height of the row the cursor is
14712 in. */
14713 run.current_y = start_row->y;
14714 run.desired_y = it.current_y;
14715 run.height = it.last_visible_y - it.current_y;
14716
14717 if (run.height > 0 && run.current_y != run.desired_y)
14718 {
14719 update_begin (f);
14720 FRAME_RIF (f)->update_window_begin_hook (w);
14721 FRAME_RIF (f)->clear_window_mouse_face (w);
14722 FRAME_RIF (f)->scroll_run_hook (w, &run);
14723 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14724 update_end (f);
14725 }
14726
14727 /* Shift current matrix down by nrows_scrolled lines. */
14728 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14729 rotate_matrix (w->current_matrix,
14730 start_vpos,
14731 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14732 nrows_scrolled);
14733
14734 /* Disable lines that must be updated. */
14735 for (i = 0; i < nrows_scrolled; ++i)
14736 (start_row + i)->enabled_p = 0;
14737
14738 /* Re-compute Y positions. */
14739 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14740 max_y = it.last_visible_y;
14741 for (row = start_row + nrows_scrolled;
14742 row < bottom_row;
14743 ++row)
14744 {
14745 row->y = it.current_y;
14746 row->visible_height = row->height;
14747
14748 if (row->y < min_y)
14749 row->visible_height -= min_y - row->y;
14750 if (row->y + row->height > max_y)
14751 row->visible_height -= row->y + row->height - max_y;
14752 row->redraw_fringe_bitmaps_p = 1;
14753
14754 it.current_y += row->height;
14755
14756 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14757 last_reused_text_row = row;
14758 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14759 break;
14760 }
14761
14762 /* Disable lines in the current matrix which are now
14763 below the window. */
14764 for (++row; row < bottom_row; ++row)
14765 row->enabled_p = row->mode_line_p = 0;
14766 }
14767
14768 /* Update window_end_pos etc.; last_reused_text_row is the last
14769 reused row from the current matrix containing text, if any.
14770 The value of last_text_row is the last displayed line
14771 containing text. */
14772 if (last_reused_text_row)
14773 {
14774 w->window_end_bytepos
14775 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14776 w->window_end_pos
14777 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14778 w->window_end_vpos
14779 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14780 w->current_matrix));
14781 }
14782 else if (last_text_row)
14783 {
14784 w->window_end_bytepos
14785 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14786 w->window_end_pos
14787 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14788 w->window_end_vpos
14789 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14790 }
14791 else
14792 {
14793 /* This window must be completely empty. */
14794 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14795 w->window_end_pos = make_number (Z - ZV);
14796 w->window_end_vpos = make_number (0);
14797 }
14798 w->window_end_valid = Qnil;
14799
14800 /* Update hint: don't try scrolling again in update_window. */
14801 w->desired_matrix->no_scrolling_p = 1;
14802
14803 #if GLYPH_DEBUG
14804 debug_method_add (w, "try_window_reusing_current_matrix 1");
14805 #endif
14806 return 1;
14807 }
14808 else if (CHARPOS (new_start) > CHARPOS (start))
14809 {
14810 struct glyph_row *pt_row, *row;
14811 struct glyph_row *first_reusable_row;
14812 struct glyph_row *first_row_to_display;
14813 int dy;
14814 int yb = window_text_bottom_y (w);
14815
14816 /* Find the row starting at new_start, if there is one. Don't
14817 reuse a partially visible line at the end. */
14818 first_reusable_row = start_row;
14819 while (first_reusable_row->enabled_p
14820 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14821 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14822 < CHARPOS (new_start)))
14823 ++first_reusable_row;
14824
14825 /* Give up if there is no row to reuse. */
14826 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14827 || !first_reusable_row->enabled_p
14828 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14829 != CHARPOS (new_start)))
14830 return 0;
14831
14832 /* We can reuse fully visible rows beginning with
14833 first_reusable_row to the end of the window. Set
14834 first_row_to_display to the first row that cannot be reused.
14835 Set pt_row to the row containing point, if there is any. */
14836 pt_row = NULL;
14837 for (first_row_to_display = first_reusable_row;
14838 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14839 ++first_row_to_display)
14840 {
14841 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14842 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14843 pt_row = first_row_to_display;
14844 }
14845
14846 /* Start displaying at the start of first_row_to_display. */
14847 xassert (first_row_to_display->y < yb);
14848 init_to_row_start (&it, w, first_row_to_display);
14849
14850 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14851 - start_vpos);
14852 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14853 - nrows_scrolled);
14854 it.current_y = (first_row_to_display->y - first_reusable_row->y
14855 + WINDOW_HEADER_LINE_HEIGHT (w));
14856
14857 /* Display lines beginning with first_row_to_display in the
14858 desired matrix. Set last_text_row to the last row displayed
14859 that displays text. */
14860 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14861 if (pt_row == NULL)
14862 w->cursor.vpos = -1;
14863 last_text_row = NULL;
14864 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14865 if (display_line (&it))
14866 last_text_row = it.glyph_row - 1;
14867
14868 /* If point is in a reused row, adjust y and vpos of the cursor
14869 position. */
14870 if (pt_row)
14871 {
14872 w->cursor.vpos -= nrows_scrolled;
14873 w->cursor.y -= first_reusable_row->y - start_row->y;
14874 }
14875
14876 /* Give up if point isn't in a row displayed or reused. (This
14877 also handles the case where w->cursor.vpos < nrows_scrolled
14878 after the calls to display_line, which can happen with scroll
14879 margins. See bug#1295.) */
14880 if (w->cursor.vpos < 0)
14881 {
14882 clear_glyph_matrix (w->desired_matrix);
14883 return 0;
14884 }
14885
14886 /* Scroll the display. */
14887 run.current_y = first_reusable_row->y;
14888 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14889 run.height = it.last_visible_y - run.current_y;
14890 dy = run.current_y - run.desired_y;
14891
14892 if (run.height)
14893 {
14894 update_begin (f);
14895 FRAME_RIF (f)->update_window_begin_hook (w);
14896 FRAME_RIF (f)->clear_window_mouse_face (w);
14897 FRAME_RIF (f)->scroll_run_hook (w, &run);
14898 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14899 update_end (f);
14900 }
14901
14902 /* Adjust Y positions of reused rows. */
14903 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14904 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14905 max_y = it.last_visible_y;
14906 for (row = first_reusable_row; row < first_row_to_display; ++row)
14907 {
14908 row->y -= dy;
14909 row->visible_height = row->height;
14910 if (row->y < min_y)
14911 row->visible_height -= min_y - row->y;
14912 if (row->y + row->height > max_y)
14913 row->visible_height -= row->y + row->height - max_y;
14914 row->redraw_fringe_bitmaps_p = 1;
14915 }
14916
14917 /* Scroll the current matrix. */
14918 xassert (nrows_scrolled > 0);
14919 rotate_matrix (w->current_matrix,
14920 start_vpos,
14921 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14922 -nrows_scrolled);
14923
14924 /* Disable rows not reused. */
14925 for (row -= nrows_scrolled; row < bottom_row; ++row)
14926 row->enabled_p = 0;
14927
14928 /* Point may have moved to a different line, so we cannot assume that
14929 the previous cursor position is valid; locate the correct row. */
14930 if (pt_row)
14931 {
14932 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14933 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14934 row++)
14935 {
14936 w->cursor.vpos++;
14937 w->cursor.y = row->y;
14938 }
14939 if (row < bottom_row)
14940 {
14941 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14942 struct glyph *end = glyph + row->used[TEXT_AREA];
14943 struct glyph *orig_glyph = glyph;
14944 struct cursor_pos orig_cursor = w->cursor;
14945
14946 for (; glyph < end
14947 && (!BUFFERP (glyph->object)
14948 || glyph->charpos != PT);
14949 glyph++)
14950 {
14951 w->cursor.hpos++;
14952 w->cursor.x += glyph->pixel_width;
14953 }
14954 /* With bidi reordering, charpos changes non-linearly
14955 with hpos, so the right glyph could be to the
14956 left. */
14957 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
14958 && (!BUFFERP (glyph->object) || glyph->charpos != PT))
14959 {
14960 struct glyph *start_glyph = row->glyphs[TEXT_AREA];
14961
14962 glyph = orig_glyph - 1;
14963 orig_cursor.hpos--;
14964 orig_cursor.x -= glyph->pixel_width;
14965 for (; glyph >= start_glyph
14966 && (!BUFFERP (glyph->object)
14967 || glyph->charpos != PT);
14968 glyph--)
14969 {
14970 w->cursor.hpos--;
14971 w->cursor.x -= glyph->pixel_width;
14972 }
14973 if (BUFFERP (glyph->object) && glyph->charpos == PT)
14974 w->cursor = orig_cursor;
14975 }
14976 }
14977 }
14978
14979 /* Adjust window end. A null value of last_text_row means that
14980 the window end is in reused rows which in turn means that
14981 only its vpos can have changed. */
14982 if (last_text_row)
14983 {
14984 w->window_end_bytepos
14985 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14986 w->window_end_pos
14987 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14988 w->window_end_vpos
14989 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14990 }
14991 else
14992 {
14993 w->window_end_vpos
14994 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14995 }
14996
14997 w->window_end_valid = Qnil;
14998 w->desired_matrix->no_scrolling_p = 1;
14999
15000 #if GLYPH_DEBUG
15001 debug_method_add (w, "try_window_reusing_current_matrix 2");
15002 #endif
15003 return 1;
15004 }
15005
15006 return 0;
15007 }
15008
15009
15010 \f
15011 /************************************************************************
15012 Window redisplay reusing current matrix when buffer has changed
15013 ************************************************************************/
15014
15015 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
15016 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
15017 int *, int *));
15018 static struct glyph_row *
15019 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
15020 struct glyph_row *));
15021
15022
15023 /* Return the last row in MATRIX displaying text. If row START is
15024 non-null, start searching with that row. IT gives the dimensions
15025 of the display. Value is null if matrix is empty; otherwise it is
15026 a pointer to the row found. */
15027
15028 static struct glyph_row *
15029 find_last_row_displaying_text (matrix, it, start)
15030 struct glyph_matrix *matrix;
15031 struct it *it;
15032 struct glyph_row *start;
15033 {
15034 struct glyph_row *row, *row_found;
15035
15036 /* Set row_found to the last row in IT->w's current matrix
15037 displaying text. The loop looks funny but think of partially
15038 visible lines. */
15039 row_found = NULL;
15040 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15041 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15042 {
15043 xassert (row->enabled_p);
15044 row_found = row;
15045 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15046 break;
15047 ++row;
15048 }
15049
15050 return row_found;
15051 }
15052
15053
15054 /* Return the last row in the current matrix of W that is not affected
15055 by changes at the start of current_buffer that occurred since W's
15056 current matrix was built. Value is null if no such row exists.
15057
15058 BEG_UNCHANGED us the number of characters unchanged at the start of
15059 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15060 first changed character in current_buffer. Characters at positions <
15061 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15062 when the current matrix was built. */
15063
15064 static struct glyph_row *
15065 find_last_unchanged_at_beg_row (w)
15066 struct window *w;
15067 {
15068 int first_changed_pos = BEG + BEG_UNCHANGED;
15069 struct glyph_row *row;
15070 struct glyph_row *row_found = NULL;
15071 int yb = window_text_bottom_y (w);
15072
15073 /* Find the last row displaying unchanged text. */
15074 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15075 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15076 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15077 ++row)
15078 {
15079 if (/* If row ends before first_changed_pos, it is unchanged,
15080 except in some case. */
15081 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15082 /* When row ends in ZV and we write at ZV it is not
15083 unchanged. */
15084 && !row->ends_at_zv_p
15085 /* When first_changed_pos is the end of a continued line,
15086 row is not unchanged because it may be no longer
15087 continued. */
15088 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15089 && (row->continued_p
15090 || row->exact_window_width_line_p)))
15091 row_found = row;
15092
15093 /* Stop if last visible row. */
15094 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15095 break;
15096 }
15097
15098 return row_found;
15099 }
15100
15101
15102 /* Find the first glyph row in the current matrix of W that is not
15103 affected by changes at the end of current_buffer since the
15104 time W's current matrix was built.
15105
15106 Return in *DELTA the number of chars by which buffer positions in
15107 unchanged text at the end of current_buffer must be adjusted.
15108
15109 Return in *DELTA_BYTES the corresponding number of bytes.
15110
15111 Value is null if no such row exists, i.e. all rows are affected by
15112 changes. */
15113
15114 static struct glyph_row *
15115 find_first_unchanged_at_end_row (w, delta, delta_bytes)
15116 struct window *w;
15117 int *delta, *delta_bytes;
15118 {
15119 struct glyph_row *row;
15120 struct glyph_row *row_found = NULL;
15121
15122 *delta = *delta_bytes = 0;
15123
15124 /* Display must not have been paused, otherwise the current matrix
15125 is not up to date. */
15126 eassert (!NILP (w->window_end_valid));
15127
15128 /* A value of window_end_pos >= END_UNCHANGED means that the window
15129 end is in the range of changed text. If so, there is no
15130 unchanged row at the end of W's current matrix. */
15131 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15132 return NULL;
15133
15134 /* Set row to the last row in W's current matrix displaying text. */
15135 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15136
15137 /* If matrix is entirely empty, no unchanged row exists. */
15138 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15139 {
15140 /* The value of row is the last glyph row in the matrix having a
15141 meaningful buffer position in it. The end position of row
15142 corresponds to window_end_pos. This allows us to translate
15143 buffer positions in the current matrix to current buffer
15144 positions for characters not in changed text. */
15145 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15146 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15147 int last_unchanged_pos, last_unchanged_pos_old;
15148 struct glyph_row *first_text_row
15149 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15150
15151 *delta = Z - Z_old;
15152 *delta_bytes = Z_BYTE - Z_BYTE_old;
15153
15154 /* Set last_unchanged_pos to the buffer position of the last
15155 character in the buffer that has not been changed. Z is the
15156 index + 1 of the last character in current_buffer, i.e. by
15157 subtracting END_UNCHANGED we get the index of the last
15158 unchanged character, and we have to add BEG to get its buffer
15159 position. */
15160 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15161 last_unchanged_pos_old = last_unchanged_pos - *delta;
15162
15163 /* Search backward from ROW for a row displaying a line that
15164 starts at a minimum position >= last_unchanged_pos_old. */
15165 for (; row > first_text_row; --row)
15166 {
15167 /* This used to abort, but it can happen.
15168 It is ok to just stop the search instead here. KFS. */
15169 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15170 break;
15171
15172 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15173 row_found = row;
15174 }
15175 }
15176
15177 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15178
15179 return row_found;
15180 }
15181
15182
15183 /* Make sure that glyph rows in the current matrix of window W
15184 reference the same glyph memory as corresponding rows in the
15185 frame's frame matrix. This function is called after scrolling W's
15186 current matrix on a terminal frame in try_window_id and
15187 try_window_reusing_current_matrix. */
15188
15189 static void
15190 sync_frame_with_window_matrix_rows (w)
15191 struct window *w;
15192 {
15193 struct frame *f = XFRAME (w->frame);
15194 struct glyph_row *window_row, *window_row_end, *frame_row;
15195
15196 /* Preconditions: W must be a leaf window and full-width. Its frame
15197 must have a frame matrix. */
15198 xassert (NILP (w->hchild) && NILP (w->vchild));
15199 xassert (WINDOW_FULL_WIDTH_P (w));
15200 xassert (!FRAME_WINDOW_P (f));
15201
15202 /* If W is a full-width window, glyph pointers in W's current matrix
15203 have, by definition, to be the same as glyph pointers in the
15204 corresponding frame matrix. Note that frame matrices have no
15205 marginal areas (see build_frame_matrix). */
15206 window_row = w->current_matrix->rows;
15207 window_row_end = window_row + w->current_matrix->nrows;
15208 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15209 while (window_row < window_row_end)
15210 {
15211 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15212 struct glyph *end = window_row->glyphs[LAST_AREA];
15213
15214 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15215 frame_row->glyphs[TEXT_AREA] = start;
15216 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15217 frame_row->glyphs[LAST_AREA] = end;
15218
15219 /* Disable frame rows whose corresponding window rows have
15220 been disabled in try_window_id. */
15221 if (!window_row->enabled_p)
15222 frame_row->enabled_p = 0;
15223
15224 ++window_row, ++frame_row;
15225 }
15226 }
15227
15228
15229 /* Find the glyph row in window W containing CHARPOS. Consider all
15230 rows between START and END (not inclusive). END null means search
15231 all rows to the end of the display area of W. Value is the row
15232 containing CHARPOS or null. */
15233
15234 struct glyph_row *
15235 row_containing_pos (w, charpos, start, end, dy)
15236 struct window *w;
15237 int charpos;
15238 struct glyph_row *start, *end;
15239 int dy;
15240 {
15241 struct glyph_row *row = start;
15242 int last_y;
15243
15244 /* If we happen to start on a header-line, skip that. */
15245 if (row->mode_line_p)
15246 ++row;
15247
15248 if ((end && row >= end) || !row->enabled_p)
15249 return NULL;
15250
15251 last_y = window_text_bottom_y (w) - dy;
15252
15253 while (1)
15254 {
15255 /* Give up if we have gone too far. */
15256 if (end && row >= end)
15257 return NULL;
15258 /* This formerly returned if they were equal.
15259 I think that both quantities are of a "last plus one" type;
15260 if so, when they are equal, the row is within the screen. -- rms. */
15261 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15262 return NULL;
15263
15264 /* If it is in this row, return this row. */
15265 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15266 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15267 /* The end position of a row equals the start
15268 position of the next row. If CHARPOS is there, we
15269 would rather display it in the next line, except
15270 when this line ends in ZV. */
15271 && !row->ends_at_zv_p
15272 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15273 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15274 return row;
15275 ++row;
15276 }
15277 }
15278
15279
15280 /* Try to redisplay window W by reusing its existing display. W's
15281 current matrix must be up to date when this function is called,
15282 i.e. window_end_valid must not be nil.
15283
15284 Value is
15285
15286 1 if display has been updated
15287 0 if otherwise unsuccessful
15288 -1 if redisplay with same window start is known not to succeed
15289
15290 The following steps are performed:
15291
15292 1. Find the last row in the current matrix of W that is not
15293 affected by changes at the start of current_buffer. If no such row
15294 is found, give up.
15295
15296 2. Find the first row in W's current matrix that is not affected by
15297 changes at the end of current_buffer. Maybe there is no such row.
15298
15299 3. Display lines beginning with the row + 1 found in step 1 to the
15300 row found in step 2 or, if step 2 didn't find a row, to the end of
15301 the window.
15302
15303 4. If cursor is not known to appear on the window, give up.
15304
15305 5. If display stopped at the row found in step 2, scroll the
15306 display and current matrix as needed.
15307
15308 6. Maybe display some lines at the end of W, if we must. This can
15309 happen under various circumstances, like a partially visible line
15310 becoming fully visible, or because newly displayed lines are displayed
15311 in smaller font sizes.
15312
15313 7. Update W's window end information. */
15314
15315 static int
15316 try_window_id (w)
15317 struct window *w;
15318 {
15319 struct frame *f = XFRAME (w->frame);
15320 struct glyph_matrix *current_matrix = w->current_matrix;
15321 struct glyph_matrix *desired_matrix = w->desired_matrix;
15322 struct glyph_row *last_unchanged_at_beg_row;
15323 struct glyph_row *first_unchanged_at_end_row;
15324 struct glyph_row *row;
15325 struct glyph_row *bottom_row;
15326 int bottom_vpos;
15327 struct it it;
15328 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
15329 struct text_pos start_pos;
15330 struct run run;
15331 int first_unchanged_at_end_vpos = 0;
15332 struct glyph_row *last_text_row, *last_text_row_at_end;
15333 struct text_pos start;
15334 int first_changed_charpos, last_changed_charpos;
15335
15336 #if GLYPH_DEBUG
15337 if (inhibit_try_window_id)
15338 return 0;
15339 #endif
15340
15341 /* This is handy for debugging. */
15342 #if 0
15343 #define GIVE_UP(X) \
15344 do { \
15345 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15346 return 0; \
15347 } while (0)
15348 #else
15349 #define GIVE_UP(X) return 0
15350 #endif
15351
15352 SET_TEXT_POS_FROM_MARKER (start, w->start);
15353
15354 /* Don't use this for mini-windows because these can show
15355 messages and mini-buffers, and we don't handle that here. */
15356 if (MINI_WINDOW_P (w))
15357 GIVE_UP (1);
15358
15359 /* This flag is used to prevent redisplay optimizations. */
15360 if (windows_or_buffers_changed || cursor_type_changed)
15361 GIVE_UP (2);
15362
15363 /* Verify that narrowing has not changed.
15364 Also verify that we were not told to prevent redisplay optimizations.
15365 It would be nice to further
15366 reduce the number of cases where this prevents try_window_id. */
15367 if (current_buffer->clip_changed
15368 || current_buffer->prevent_redisplay_optimizations_p)
15369 GIVE_UP (3);
15370
15371 /* Window must either use window-based redisplay or be full width. */
15372 if (!FRAME_WINDOW_P (f)
15373 && (!FRAME_LINE_INS_DEL_OK (f)
15374 || !WINDOW_FULL_WIDTH_P (w)))
15375 GIVE_UP (4);
15376
15377 /* Give up if point is known NOT to appear in W. */
15378 if (PT < CHARPOS (start))
15379 GIVE_UP (5);
15380
15381 /* Another way to prevent redisplay optimizations. */
15382 if (XFASTINT (w->last_modified) == 0)
15383 GIVE_UP (6);
15384
15385 /* Verify that window is not hscrolled. */
15386 if (XFASTINT (w->hscroll) != 0)
15387 GIVE_UP (7);
15388
15389 /* Verify that display wasn't paused. */
15390 if (NILP (w->window_end_valid))
15391 GIVE_UP (8);
15392
15393 /* Can't use this if highlighting a region because a cursor movement
15394 will do more than just set the cursor. */
15395 if (!NILP (Vtransient_mark_mode)
15396 && !NILP (current_buffer->mark_active))
15397 GIVE_UP (9);
15398
15399 /* Likewise if highlighting trailing whitespace. */
15400 if (!NILP (Vshow_trailing_whitespace))
15401 GIVE_UP (11);
15402
15403 /* Likewise if showing a region. */
15404 if (!NILP (w->region_showing))
15405 GIVE_UP (10);
15406
15407 /* Can't use this if overlay arrow position and/or string have
15408 changed. */
15409 if (overlay_arrows_changed_p ())
15410 GIVE_UP (12);
15411
15412 /* When word-wrap is on, adding a space to the first word of a
15413 wrapped line can change the wrap position, altering the line
15414 above it. It might be worthwhile to handle this more
15415 intelligently, but for now just redisplay from scratch. */
15416 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15417 GIVE_UP (21);
15418
15419 /* Under bidi reordering, adding or deleting a character in the
15420 beginning of a paragraph, before the first strong directional
15421 character, can change the base direction of the paragraph (unless
15422 the buffer specifies a fixed paragraph direction), which will
15423 require to redisplay the whole paragraph. It might be worthwhile
15424 to find the paragraph limits and widen the range of redisplayed
15425 lines to that, but for now just give up this optimization and
15426 redisplay from scratch. */
15427 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15428 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15429 GIVE_UP (22);
15430
15431 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15432 only if buffer has really changed. The reason is that the gap is
15433 initially at Z for freshly visited files. The code below would
15434 set end_unchanged to 0 in that case. */
15435 if (MODIFF > SAVE_MODIFF
15436 /* This seems to happen sometimes after saving a buffer. */
15437 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15438 {
15439 if (GPT - BEG < BEG_UNCHANGED)
15440 BEG_UNCHANGED = GPT - BEG;
15441 if (Z - GPT < END_UNCHANGED)
15442 END_UNCHANGED = Z - GPT;
15443 }
15444
15445 /* The position of the first and last character that has been changed. */
15446 first_changed_charpos = BEG + BEG_UNCHANGED;
15447 last_changed_charpos = Z - END_UNCHANGED;
15448
15449 /* If window starts after a line end, and the last change is in
15450 front of that newline, then changes don't affect the display.
15451 This case happens with stealth-fontification. Note that although
15452 the display is unchanged, glyph positions in the matrix have to
15453 be adjusted, of course. */
15454 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15455 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15456 && ((last_changed_charpos < CHARPOS (start)
15457 && CHARPOS (start) == BEGV)
15458 || (last_changed_charpos < CHARPOS (start) - 1
15459 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15460 {
15461 int Z_old, delta, Z_BYTE_old, delta_bytes;
15462 struct glyph_row *r0;
15463
15464 /* Compute how many chars/bytes have been added to or removed
15465 from the buffer. */
15466 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15467 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15468 delta = Z - Z_old;
15469 delta_bytes = Z_BYTE - Z_BYTE_old;
15470
15471 /* Give up if PT is not in the window. Note that it already has
15472 been checked at the start of try_window_id that PT is not in
15473 front of the window start. */
15474 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15475 GIVE_UP (13);
15476
15477 /* If window start is unchanged, we can reuse the whole matrix
15478 as is, after adjusting glyph positions. No need to compute
15479 the window end again, since its offset from Z hasn't changed. */
15480 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15481 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15482 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15483 /* PT must not be in a partially visible line. */
15484 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15485 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15486 {
15487 /* Adjust positions in the glyph matrix. */
15488 if (delta || delta_bytes)
15489 {
15490 struct glyph_row *r1
15491 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15492 increment_matrix_positions (w->current_matrix,
15493 MATRIX_ROW_VPOS (r0, current_matrix),
15494 MATRIX_ROW_VPOS (r1, current_matrix),
15495 delta, delta_bytes);
15496 }
15497
15498 /* Set the cursor. */
15499 row = row_containing_pos (w, PT, r0, NULL, 0);
15500 if (row)
15501 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15502 else
15503 abort ();
15504 return 1;
15505 }
15506 }
15507
15508 /* Handle the case that changes are all below what is displayed in
15509 the window, and that PT is in the window. This shortcut cannot
15510 be taken if ZV is visible in the window, and text has been added
15511 there that is visible in the window. */
15512 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15513 /* ZV is not visible in the window, or there are no
15514 changes at ZV, actually. */
15515 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15516 || first_changed_charpos == last_changed_charpos))
15517 {
15518 struct glyph_row *r0;
15519
15520 /* Give up if PT is not in the window. Note that it already has
15521 been checked at the start of try_window_id that PT is not in
15522 front of the window start. */
15523 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15524 GIVE_UP (14);
15525
15526 /* If window start is unchanged, we can reuse the whole matrix
15527 as is, without changing glyph positions since no text has
15528 been added/removed in front of the window end. */
15529 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15530 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15531 /* PT must not be in a partially visible line. */
15532 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15533 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15534 {
15535 /* We have to compute the window end anew since text
15536 can have been added/removed after it. */
15537 w->window_end_pos
15538 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15539 w->window_end_bytepos
15540 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15541
15542 /* Set the cursor. */
15543 row = row_containing_pos (w, PT, r0, NULL, 0);
15544 if (row)
15545 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15546 else
15547 abort ();
15548 return 2;
15549 }
15550 }
15551
15552 /* Give up if window start is in the changed area.
15553
15554 The condition used to read
15555
15556 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15557
15558 but why that was tested escapes me at the moment. */
15559 if (CHARPOS (start) >= first_changed_charpos
15560 && CHARPOS (start) <= last_changed_charpos)
15561 GIVE_UP (15);
15562
15563 /* Check that window start agrees with the start of the first glyph
15564 row in its current matrix. Check this after we know the window
15565 start is not in changed text, otherwise positions would not be
15566 comparable. */
15567 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15568 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15569 GIVE_UP (16);
15570
15571 /* Give up if the window ends in strings. Overlay strings
15572 at the end are difficult to handle, so don't try. */
15573 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15574 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15575 GIVE_UP (20);
15576
15577 /* Compute the position at which we have to start displaying new
15578 lines. Some of the lines at the top of the window might be
15579 reusable because they are not displaying changed text. Find the
15580 last row in W's current matrix not affected by changes at the
15581 start of current_buffer. Value is null if changes start in the
15582 first line of window. */
15583 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15584 if (last_unchanged_at_beg_row)
15585 {
15586 /* Avoid starting to display in the moddle of a character, a TAB
15587 for instance. This is easier than to set up the iterator
15588 exactly, and it's not a frequent case, so the additional
15589 effort wouldn't really pay off. */
15590 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15591 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15592 && last_unchanged_at_beg_row > w->current_matrix->rows)
15593 --last_unchanged_at_beg_row;
15594
15595 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15596 GIVE_UP (17);
15597
15598 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15599 GIVE_UP (18);
15600 start_pos = it.current.pos;
15601
15602 /* Start displaying new lines in the desired matrix at the same
15603 vpos we would use in the current matrix, i.e. below
15604 last_unchanged_at_beg_row. */
15605 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15606 current_matrix);
15607 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15608 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15609
15610 xassert (it.hpos == 0 && it.current_x == 0);
15611 }
15612 else
15613 {
15614 /* There are no reusable lines at the start of the window.
15615 Start displaying in the first text line. */
15616 start_display (&it, w, start);
15617 it.vpos = it.first_vpos;
15618 start_pos = it.current.pos;
15619 }
15620
15621 /* Find the first row that is not affected by changes at the end of
15622 the buffer. Value will be null if there is no unchanged row, in
15623 which case we must redisplay to the end of the window. delta
15624 will be set to the value by which buffer positions beginning with
15625 first_unchanged_at_end_row have to be adjusted due to text
15626 changes. */
15627 first_unchanged_at_end_row
15628 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15629 IF_DEBUG (debug_delta = delta);
15630 IF_DEBUG (debug_delta_bytes = delta_bytes);
15631
15632 /* Set stop_pos to the buffer position up to which we will have to
15633 display new lines. If first_unchanged_at_end_row != NULL, this
15634 is the buffer position of the start of the line displayed in that
15635 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15636 that we don't stop at a buffer position. */
15637 stop_pos = 0;
15638 if (first_unchanged_at_end_row)
15639 {
15640 xassert (last_unchanged_at_beg_row == NULL
15641 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15642
15643 /* If this is a continuation line, move forward to the next one
15644 that isn't. Changes in lines above affect this line.
15645 Caution: this may move first_unchanged_at_end_row to a row
15646 not displaying text. */
15647 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15648 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15649 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15650 < it.last_visible_y))
15651 ++first_unchanged_at_end_row;
15652
15653 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15654 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15655 >= it.last_visible_y))
15656 first_unchanged_at_end_row = NULL;
15657 else
15658 {
15659 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15660 + delta);
15661 first_unchanged_at_end_vpos
15662 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15663 xassert (stop_pos >= Z - END_UNCHANGED);
15664 }
15665 }
15666 else if (last_unchanged_at_beg_row == NULL)
15667 GIVE_UP (19);
15668
15669
15670 #if GLYPH_DEBUG
15671
15672 /* Either there is no unchanged row at the end, or the one we have
15673 now displays text. This is a necessary condition for the window
15674 end pos calculation at the end of this function. */
15675 xassert (first_unchanged_at_end_row == NULL
15676 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15677
15678 debug_last_unchanged_at_beg_vpos
15679 = (last_unchanged_at_beg_row
15680 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15681 : -1);
15682 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15683
15684 #endif /* GLYPH_DEBUG != 0 */
15685
15686
15687 /* Display new lines. Set last_text_row to the last new line
15688 displayed which has text on it, i.e. might end up as being the
15689 line where the window_end_vpos is. */
15690 w->cursor.vpos = -1;
15691 last_text_row = NULL;
15692 overlay_arrow_seen = 0;
15693 while (it.current_y < it.last_visible_y
15694 && !fonts_changed_p
15695 && (first_unchanged_at_end_row == NULL
15696 || IT_CHARPOS (it) < stop_pos))
15697 {
15698 if (display_line (&it))
15699 last_text_row = it.glyph_row - 1;
15700 }
15701
15702 if (fonts_changed_p)
15703 return -1;
15704
15705
15706 /* Compute differences in buffer positions, y-positions etc. for
15707 lines reused at the bottom of the window. Compute what we can
15708 scroll. */
15709 if (first_unchanged_at_end_row
15710 /* No lines reused because we displayed everything up to the
15711 bottom of the window. */
15712 && it.current_y < it.last_visible_y)
15713 {
15714 dvpos = (it.vpos
15715 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15716 current_matrix));
15717 dy = it.current_y - first_unchanged_at_end_row->y;
15718 run.current_y = first_unchanged_at_end_row->y;
15719 run.desired_y = run.current_y + dy;
15720 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15721 }
15722 else
15723 {
15724 delta = delta_bytes = dvpos = dy
15725 = run.current_y = run.desired_y = run.height = 0;
15726 first_unchanged_at_end_row = NULL;
15727 }
15728 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15729
15730
15731 /* Find the cursor if not already found. We have to decide whether
15732 PT will appear on this window (it sometimes doesn't, but this is
15733 not a very frequent case.) This decision has to be made before
15734 the current matrix is altered. A value of cursor.vpos < 0 means
15735 that PT is either in one of the lines beginning at
15736 first_unchanged_at_end_row or below the window. Don't care for
15737 lines that might be displayed later at the window end; as
15738 mentioned, this is not a frequent case. */
15739 if (w->cursor.vpos < 0)
15740 {
15741 /* Cursor in unchanged rows at the top? */
15742 if (PT < CHARPOS (start_pos)
15743 && last_unchanged_at_beg_row)
15744 {
15745 row = row_containing_pos (w, PT,
15746 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15747 last_unchanged_at_beg_row + 1, 0);
15748 if (row)
15749 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15750 }
15751
15752 /* Start from first_unchanged_at_end_row looking for PT. */
15753 else if (first_unchanged_at_end_row)
15754 {
15755 row = row_containing_pos (w, PT - delta,
15756 first_unchanged_at_end_row, NULL, 0);
15757 if (row)
15758 set_cursor_from_row (w, row, w->current_matrix, delta,
15759 delta_bytes, dy, dvpos);
15760 }
15761
15762 /* Give up if cursor was not found. */
15763 if (w->cursor.vpos < 0)
15764 {
15765 clear_glyph_matrix (w->desired_matrix);
15766 return -1;
15767 }
15768 }
15769
15770 /* Don't let the cursor end in the scroll margins. */
15771 {
15772 int this_scroll_margin, cursor_height;
15773
15774 this_scroll_margin = max (0, scroll_margin);
15775 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15776 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15777 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15778
15779 if ((w->cursor.y < this_scroll_margin
15780 && CHARPOS (start) > BEGV)
15781 /* Old redisplay didn't take scroll margin into account at the bottom,
15782 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15783 || (w->cursor.y + (make_cursor_line_fully_visible_p
15784 ? cursor_height + this_scroll_margin
15785 : 1)) > it.last_visible_y)
15786 {
15787 w->cursor.vpos = -1;
15788 clear_glyph_matrix (w->desired_matrix);
15789 return -1;
15790 }
15791 }
15792
15793 /* Scroll the display. Do it before changing the current matrix so
15794 that xterm.c doesn't get confused about where the cursor glyph is
15795 found. */
15796 if (dy && run.height)
15797 {
15798 update_begin (f);
15799
15800 if (FRAME_WINDOW_P (f))
15801 {
15802 FRAME_RIF (f)->update_window_begin_hook (w);
15803 FRAME_RIF (f)->clear_window_mouse_face (w);
15804 FRAME_RIF (f)->scroll_run_hook (w, &run);
15805 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15806 }
15807 else
15808 {
15809 /* Terminal frame. In this case, dvpos gives the number of
15810 lines to scroll by; dvpos < 0 means scroll up. */
15811 int first_unchanged_at_end_vpos
15812 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15813 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15814 int end = (WINDOW_TOP_EDGE_LINE (w)
15815 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15816 + window_internal_height (w));
15817
15818 /* Perform the operation on the screen. */
15819 if (dvpos > 0)
15820 {
15821 /* Scroll last_unchanged_at_beg_row to the end of the
15822 window down dvpos lines. */
15823 set_terminal_window (f, end);
15824
15825 /* On dumb terminals delete dvpos lines at the end
15826 before inserting dvpos empty lines. */
15827 if (!FRAME_SCROLL_REGION_OK (f))
15828 ins_del_lines (f, end - dvpos, -dvpos);
15829
15830 /* Insert dvpos empty lines in front of
15831 last_unchanged_at_beg_row. */
15832 ins_del_lines (f, from, dvpos);
15833 }
15834 else if (dvpos < 0)
15835 {
15836 /* Scroll up last_unchanged_at_beg_vpos to the end of
15837 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15838 set_terminal_window (f, end);
15839
15840 /* Delete dvpos lines in front of
15841 last_unchanged_at_beg_vpos. ins_del_lines will set
15842 the cursor to the given vpos and emit |dvpos| delete
15843 line sequences. */
15844 ins_del_lines (f, from + dvpos, dvpos);
15845
15846 /* On a dumb terminal insert dvpos empty lines at the
15847 end. */
15848 if (!FRAME_SCROLL_REGION_OK (f))
15849 ins_del_lines (f, end + dvpos, -dvpos);
15850 }
15851
15852 set_terminal_window (f, 0);
15853 }
15854
15855 update_end (f);
15856 }
15857
15858 /* Shift reused rows of the current matrix to the right position.
15859 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15860 text. */
15861 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15862 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15863 if (dvpos < 0)
15864 {
15865 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15866 bottom_vpos, dvpos);
15867 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15868 bottom_vpos, 0);
15869 }
15870 else if (dvpos > 0)
15871 {
15872 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15873 bottom_vpos, dvpos);
15874 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15875 first_unchanged_at_end_vpos + dvpos, 0);
15876 }
15877
15878 /* For frame-based redisplay, make sure that current frame and window
15879 matrix are in sync with respect to glyph memory. */
15880 if (!FRAME_WINDOW_P (f))
15881 sync_frame_with_window_matrix_rows (w);
15882
15883 /* Adjust buffer positions in reused rows. */
15884 if (delta || delta_bytes)
15885 increment_matrix_positions (current_matrix,
15886 first_unchanged_at_end_vpos + dvpos,
15887 bottom_vpos, delta, delta_bytes);
15888
15889 /* Adjust Y positions. */
15890 if (dy)
15891 shift_glyph_matrix (w, current_matrix,
15892 first_unchanged_at_end_vpos + dvpos,
15893 bottom_vpos, dy);
15894
15895 if (first_unchanged_at_end_row)
15896 {
15897 first_unchanged_at_end_row += dvpos;
15898 if (first_unchanged_at_end_row->y >= it.last_visible_y
15899 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15900 first_unchanged_at_end_row = NULL;
15901 }
15902
15903 /* If scrolling up, there may be some lines to display at the end of
15904 the window. */
15905 last_text_row_at_end = NULL;
15906 if (dy < 0)
15907 {
15908 /* Scrolling up can leave for example a partially visible line
15909 at the end of the window to be redisplayed. */
15910 /* Set last_row to the glyph row in the current matrix where the
15911 window end line is found. It has been moved up or down in
15912 the matrix by dvpos. */
15913 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15914 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15915
15916 /* If last_row is the window end line, it should display text. */
15917 xassert (last_row->displays_text_p);
15918
15919 /* If window end line was partially visible before, begin
15920 displaying at that line. Otherwise begin displaying with the
15921 line following it. */
15922 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15923 {
15924 init_to_row_start (&it, w, last_row);
15925 it.vpos = last_vpos;
15926 it.current_y = last_row->y;
15927 }
15928 else
15929 {
15930 init_to_row_end (&it, w, last_row);
15931 it.vpos = 1 + last_vpos;
15932 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15933 ++last_row;
15934 }
15935
15936 /* We may start in a continuation line. If so, we have to
15937 get the right continuation_lines_width and current_x. */
15938 it.continuation_lines_width = last_row->continuation_lines_width;
15939 it.hpos = it.current_x = 0;
15940
15941 /* Display the rest of the lines at the window end. */
15942 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15943 while (it.current_y < it.last_visible_y
15944 && !fonts_changed_p)
15945 {
15946 /* Is it always sure that the display agrees with lines in
15947 the current matrix? I don't think so, so we mark rows
15948 displayed invalid in the current matrix by setting their
15949 enabled_p flag to zero. */
15950 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15951 if (display_line (&it))
15952 last_text_row_at_end = it.glyph_row - 1;
15953 }
15954 }
15955
15956 /* Update window_end_pos and window_end_vpos. */
15957 if (first_unchanged_at_end_row
15958 && !last_text_row_at_end)
15959 {
15960 /* Window end line if one of the preserved rows from the current
15961 matrix. Set row to the last row displaying text in current
15962 matrix starting at first_unchanged_at_end_row, after
15963 scrolling. */
15964 xassert (first_unchanged_at_end_row->displays_text_p);
15965 row = find_last_row_displaying_text (w->current_matrix, &it,
15966 first_unchanged_at_end_row);
15967 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15968
15969 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15970 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15971 w->window_end_vpos
15972 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15973 xassert (w->window_end_bytepos >= 0);
15974 IF_DEBUG (debug_method_add (w, "A"));
15975 }
15976 else if (last_text_row_at_end)
15977 {
15978 w->window_end_pos
15979 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15980 w->window_end_bytepos
15981 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15982 w->window_end_vpos
15983 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15984 xassert (w->window_end_bytepos >= 0);
15985 IF_DEBUG (debug_method_add (w, "B"));
15986 }
15987 else if (last_text_row)
15988 {
15989 /* We have displayed either to the end of the window or at the
15990 end of the window, i.e. the last row with text is to be found
15991 in the desired matrix. */
15992 w->window_end_pos
15993 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15994 w->window_end_bytepos
15995 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15996 w->window_end_vpos
15997 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15998 xassert (w->window_end_bytepos >= 0);
15999 }
16000 else if (first_unchanged_at_end_row == NULL
16001 && last_text_row == NULL
16002 && last_text_row_at_end == NULL)
16003 {
16004 /* Displayed to end of window, but no line containing text was
16005 displayed. Lines were deleted at the end of the window. */
16006 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16007 int vpos = XFASTINT (w->window_end_vpos);
16008 struct glyph_row *current_row = current_matrix->rows + vpos;
16009 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16010
16011 for (row = NULL;
16012 row == NULL && vpos >= first_vpos;
16013 --vpos, --current_row, --desired_row)
16014 {
16015 if (desired_row->enabled_p)
16016 {
16017 if (desired_row->displays_text_p)
16018 row = desired_row;
16019 }
16020 else if (current_row->displays_text_p)
16021 row = current_row;
16022 }
16023
16024 xassert (row != NULL);
16025 w->window_end_vpos = make_number (vpos + 1);
16026 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16027 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16028 xassert (w->window_end_bytepos >= 0);
16029 IF_DEBUG (debug_method_add (w, "C"));
16030 }
16031 else
16032 abort ();
16033
16034 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16035 debug_end_vpos = XFASTINT (w->window_end_vpos));
16036
16037 /* Record that display has not been completed. */
16038 w->window_end_valid = Qnil;
16039 w->desired_matrix->no_scrolling_p = 1;
16040 return 3;
16041
16042 #undef GIVE_UP
16043 }
16044
16045
16046 \f
16047 /***********************************************************************
16048 More debugging support
16049 ***********************************************************************/
16050
16051 #if GLYPH_DEBUG
16052
16053 void dump_glyph_row P_ ((struct glyph_row *, int, int));
16054 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
16055 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
16056
16057
16058 /* Dump the contents of glyph matrix MATRIX on stderr.
16059
16060 GLYPHS 0 means don't show glyph contents.
16061 GLYPHS 1 means show glyphs in short form
16062 GLYPHS > 1 means show glyphs in long form. */
16063
16064 void
16065 dump_glyph_matrix (matrix, glyphs)
16066 struct glyph_matrix *matrix;
16067 int glyphs;
16068 {
16069 int i;
16070 for (i = 0; i < matrix->nrows; ++i)
16071 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16072 }
16073
16074
16075 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16076 the glyph row and area where the glyph comes from. */
16077
16078 void
16079 dump_glyph (row, glyph, area)
16080 struct glyph_row *row;
16081 struct glyph *glyph;
16082 int area;
16083 {
16084 if (glyph->type == CHAR_GLYPH)
16085 {
16086 fprintf (stderr,
16087 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16088 glyph - row->glyphs[TEXT_AREA],
16089 'C',
16090 glyph->charpos,
16091 (BUFFERP (glyph->object)
16092 ? 'B'
16093 : (STRINGP (glyph->object)
16094 ? 'S'
16095 : '-')),
16096 glyph->pixel_width,
16097 glyph->u.ch,
16098 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16099 ? glyph->u.ch
16100 : '.'),
16101 glyph->face_id,
16102 glyph->left_box_line_p,
16103 glyph->right_box_line_p);
16104 }
16105 else if (glyph->type == STRETCH_GLYPH)
16106 {
16107 fprintf (stderr,
16108 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16109 glyph - row->glyphs[TEXT_AREA],
16110 'S',
16111 glyph->charpos,
16112 (BUFFERP (glyph->object)
16113 ? 'B'
16114 : (STRINGP (glyph->object)
16115 ? 'S'
16116 : '-')),
16117 glyph->pixel_width,
16118 0,
16119 '.',
16120 glyph->face_id,
16121 glyph->left_box_line_p,
16122 glyph->right_box_line_p);
16123 }
16124 else if (glyph->type == IMAGE_GLYPH)
16125 {
16126 fprintf (stderr,
16127 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16128 glyph - row->glyphs[TEXT_AREA],
16129 'I',
16130 glyph->charpos,
16131 (BUFFERP (glyph->object)
16132 ? 'B'
16133 : (STRINGP (glyph->object)
16134 ? 'S'
16135 : '-')),
16136 glyph->pixel_width,
16137 glyph->u.img_id,
16138 '.',
16139 glyph->face_id,
16140 glyph->left_box_line_p,
16141 glyph->right_box_line_p);
16142 }
16143 else if (glyph->type == COMPOSITE_GLYPH)
16144 {
16145 fprintf (stderr,
16146 " %5d %4c %6d %c %3d 0x%05x",
16147 glyph - row->glyphs[TEXT_AREA],
16148 '+',
16149 glyph->charpos,
16150 (BUFFERP (glyph->object)
16151 ? 'B'
16152 : (STRINGP (glyph->object)
16153 ? 'S'
16154 : '-')),
16155 glyph->pixel_width,
16156 glyph->u.cmp.id);
16157 if (glyph->u.cmp.automatic)
16158 fprintf (stderr,
16159 "[%d-%d]",
16160 glyph->u.cmp.from, glyph->u.cmp.to);
16161 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16162 glyph->face_id,
16163 glyph->left_box_line_p,
16164 glyph->right_box_line_p);
16165 }
16166 }
16167
16168
16169 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16170 GLYPHS 0 means don't show glyph contents.
16171 GLYPHS 1 means show glyphs in short form
16172 GLYPHS > 1 means show glyphs in long form. */
16173
16174 void
16175 dump_glyph_row (row, vpos, glyphs)
16176 struct glyph_row *row;
16177 int vpos, glyphs;
16178 {
16179 if (glyphs != 1)
16180 {
16181 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16182 fprintf (stderr, "======================================================================\n");
16183
16184 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16185 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16186 vpos,
16187 MATRIX_ROW_START_CHARPOS (row),
16188 MATRIX_ROW_END_CHARPOS (row),
16189 row->used[TEXT_AREA],
16190 row->contains_overlapping_glyphs_p,
16191 row->enabled_p,
16192 row->truncated_on_left_p,
16193 row->truncated_on_right_p,
16194 row->continued_p,
16195 MATRIX_ROW_CONTINUATION_LINE_P (row),
16196 row->displays_text_p,
16197 row->ends_at_zv_p,
16198 row->fill_line_p,
16199 row->ends_in_middle_of_char_p,
16200 row->starts_in_middle_of_char_p,
16201 row->mouse_face_p,
16202 row->x,
16203 row->y,
16204 row->pixel_width,
16205 row->height,
16206 row->visible_height,
16207 row->ascent,
16208 row->phys_ascent);
16209 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16210 row->end.overlay_string_index,
16211 row->continuation_lines_width);
16212 fprintf (stderr, "%9d %5d\n",
16213 CHARPOS (row->start.string_pos),
16214 CHARPOS (row->end.string_pos));
16215 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16216 row->end.dpvec_index);
16217 }
16218
16219 if (glyphs > 1)
16220 {
16221 int area;
16222
16223 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16224 {
16225 struct glyph *glyph = row->glyphs[area];
16226 struct glyph *glyph_end = glyph + row->used[area];
16227
16228 /* Glyph for a line end in text. */
16229 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16230 ++glyph_end;
16231
16232 if (glyph < glyph_end)
16233 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16234
16235 for (; glyph < glyph_end; ++glyph)
16236 dump_glyph (row, glyph, area);
16237 }
16238 }
16239 else if (glyphs == 1)
16240 {
16241 int area;
16242
16243 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16244 {
16245 char *s = (char *) alloca (row->used[area] + 1);
16246 int i;
16247
16248 for (i = 0; i < row->used[area]; ++i)
16249 {
16250 struct glyph *glyph = row->glyphs[area] + i;
16251 if (glyph->type == CHAR_GLYPH
16252 && glyph->u.ch < 0x80
16253 && glyph->u.ch >= ' ')
16254 s[i] = glyph->u.ch;
16255 else
16256 s[i] = '.';
16257 }
16258
16259 s[i] = '\0';
16260 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16261 }
16262 }
16263 }
16264
16265
16266 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16267 Sdump_glyph_matrix, 0, 1, "p",
16268 doc: /* Dump the current matrix of the selected window to stderr.
16269 Shows contents of glyph row structures. With non-nil
16270 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16271 glyphs in short form, otherwise show glyphs in long form. */)
16272 (glyphs)
16273 Lisp_Object glyphs;
16274 {
16275 struct window *w = XWINDOW (selected_window);
16276 struct buffer *buffer = XBUFFER (w->buffer);
16277
16278 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16279 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16280 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16281 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16282 fprintf (stderr, "=============================================\n");
16283 dump_glyph_matrix (w->current_matrix,
16284 NILP (glyphs) ? 0 : XINT (glyphs));
16285 return Qnil;
16286 }
16287
16288
16289 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16290 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16291 ()
16292 {
16293 struct frame *f = XFRAME (selected_frame);
16294 dump_glyph_matrix (f->current_matrix, 1);
16295 return Qnil;
16296 }
16297
16298
16299 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16300 doc: /* Dump glyph row ROW to stderr.
16301 GLYPH 0 means don't dump glyphs.
16302 GLYPH 1 means dump glyphs in short form.
16303 GLYPH > 1 or omitted means dump glyphs in long form. */)
16304 (row, glyphs)
16305 Lisp_Object row, glyphs;
16306 {
16307 struct glyph_matrix *matrix;
16308 int vpos;
16309
16310 CHECK_NUMBER (row);
16311 matrix = XWINDOW (selected_window)->current_matrix;
16312 vpos = XINT (row);
16313 if (vpos >= 0 && vpos < matrix->nrows)
16314 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16315 vpos,
16316 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16317 return Qnil;
16318 }
16319
16320
16321 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16322 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16323 GLYPH 0 means don't dump glyphs.
16324 GLYPH 1 means dump glyphs in short form.
16325 GLYPH > 1 or omitted means dump glyphs in long form. */)
16326 (row, glyphs)
16327 Lisp_Object row, glyphs;
16328 {
16329 struct frame *sf = SELECTED_FRAME ();
16330 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16331 int vpos;
16332
16333 CHECK_NUMBER (row);
16334 vpos = XINT (row);
16335 if (vpos >= 0 && vpos < m->nrows)
16336 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16337 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16338 return Qnil;
16339 }
16340
16341
16342 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16343 doc: /* Toggle tracing of redisplay.
16344 With ARG, turn tracing on if and only if ARG is positive. */)
16345 (arg)
16346 Lisp_Object arg;
16347 {
16348 if (NILP (arg))
16349 trace_redisplay_p = !trace_redisplay_p;
16350 else
16351 {
16352 arg = Fprefix_numeric_value (arg);
16353 trace_redisplay_p = XINT (arg) > 0;
16354 }
16355
16356 return Qnil;
16357 }
16358
16359
16360 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16361 doc: /* Like `format', but print result to stderr.
16362 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16363 (nargs, args)
16364 int nargs;
16365 Lisp_Object *args;
16366 {
16367 Lisp_Object s = Fformat (nargs, args);
16368 fprintf (stderr, "%s", SDATA (s));
16369 return Qnil;
16370 }
16371
16372 #endif /* GLYPH_DEBUG */
16373
16374
16375 \f
16376 /***********************************************************************
16377 Building Desired Matrix Rows
16378 ***********************************************************************/
16379
16380 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16381 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16382
16383 static struct glyph_row *
16384 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
16385 struct window *w;
16386 Lisp_Object overlay_arrow_string;
16387 {
16388 struct frame *f = XFRAME (WINDOW_FRAME (w));
16389 struct buffer *buffer = XBUFFER (w->buffer);
16390 struct buffer *old = current_buffer;
16391 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16392 int arrow_len = SCHARS (overlay_arrow_string);
16393 const unsigned char *arrow_end = arrow_string + arrow_len;
16394 const unsigned char *p;
16395 struct it it;
16396 int multibyte_p;
16397 int n_glyphs_before;
16398
16399 set_buffer_temp (buffer);
16400 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16401 it.glyph_row->used[TEXT_AREA] = 0;
16402 SET_TEXT_POS (it.position, 0, 0);
16403
16404 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16405 p = arrow_string;
16406 while (p < arrow_end)
16407 {
16408 Lisp_Object face, ilisp;
16409
16410 /* Get the next character. */
16411 if (multibyte_p)
16412 it.c = string_char_and_length (p, &it.len);
16413 else
16414 it.c = *p, it.len = 1;
16415 p += it.len;
16416
16417 /* Get its face. */
16418 ilisp = make_number (p - arrow_string);
16419 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16420 it.face_id = compute_char_face (f, it.c, face);
16421
16422 /* Compute its width, get its glyphs. */
16423 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16424 SET_TEXT_POS (it.position, -1, -1);
16425 PRODUCE_GLYPHS (&it);
16426
16427 /* If this character doesn't fit any more in the line, we have
16428 to remove some glyphs. */
16429 if (it.current_x > it.last_visible_x)
16430 {
16431 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16432 break;
16433 }
16434 }
16435
16436 set_buffer_temp (old);
16437 return it.glyph_row;
16438 }
16439
16440
16441 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16442 glyphs are only inserted for terminal frames since we can't really
16443 win with truncation glyphs when partially visible glyphs are
16444 involved. Which glyphs to insert is determined by
16445 produce_special_glyphs. */
16446
16447 static void
16448 insert_left_trunc_glyphs (it)
16449 struct it *it;
16450 {
16451 struct it truncate_it;
16452 struct glyph *from, *end, *to, *toend;
16453
16454 xassert (!FRAME_WINDOW_P (it->f));
16455
16456 /* Get the truncation glyphs. */
16457 truncate_it = *it;
16458 truncate_it.current_x = 0;
16459 truncate_it.face_id = DEFAULT_FACE_ID;
16460 truncate_it.glyph_row = &scratch_glyph_row;
16461 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16462 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16463 truncate_it.object = make_number (0);
16464 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16465
16466 /* Overwrite glyphs from IT with truncation glyphs. */
16467 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16468 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16469 to = it->glyph_row->glyphs[TEXT_AREA];
16470 toend = to + it->glyph_row->used[TEXT_AREA];
16471
16472 while (from < end)
16473 *to++ = *from++;
16474
16475 /* There may be padding glyphs left over. Overwrite them too. */
16476 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16477 {
16478 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16479 while (from < end)
16480 *to++ = *from++;
16481 }
16482
16483 if (to > toend)
16484 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16485 }
16486
16487
16488 /* Compute the pixel height and width of IT->glyph_row.
16489
16490 Most of the time, ascent and height of a display line will be equal
16491 to the max_ascent and max_height values of the display iterator
16492 structure. This is not the case if
16493
16494 1. We hit ZV without displaying anything. In this case, max_ascent
16495 and max_height will be zero.
16496
16497 2. We have some glyphs that don't contribute to the line height.
16498 (The glyph row flag contributes_to_line_height_p is for future
16499 pixmap extensions).
16500
16501 The first case is easily covered by using default values because in
16502 these cases, the line height does not really matter, except that it
16503 must not be zero. */
16504
16505 static void
16506 compute_line_metrics (it)
16507 struct it *it;
16508 {
16509 struct glyph_row *row = it->glyph_row;
16510 int area, i;
16511
16512 if (FRAME_WINDOW_P (it->f))
16513 {
16514 int i, min_y, max_y;
16515
16516 /* The line may consist of one space only, that was added to
16517 place the cursor on it. If so, the row's height hasn't been
16518 computed yet. */
16519 if (row->height == 0)
16520 {
16521 if (it->max_ascent + it->max_descent == 0)
16522 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16523 row->ascent = it->max_ascent;
16524 row->height = it->max_ascent + it->max_descent;
16525 row->phys_ascent = it->max_phys_ascent;
16526 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16527 row->extra_line_spacing = it->max_extra_line_spacing;
16528 }
16529
16530 /* Compute the width of this line. */
16531 row->pixel_width = row->x;
16532 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16533 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16534
16535 xassert (row->pixel_width >= 0);
16536 xassert (row->ascent >= 0 && row->height > 0);
16537
16538 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16539 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16540
16541 /* If first line's physical ascent is larger than its logical
16542 ascent, use the physical ascent, and make the row taller.
16543 This makes accented characters fully visible. */
16544 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16545 && row->phys_ascent > row->ascent)
16546 {
16547 row->height += row->phys_ascent - row->ascent;
16548 row->ascent = row->phys_ascent;
16549 }
16550
16551 /* Compute how much of the line is visible. */
16552 row->visible_height = row->height;
16553
16554 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16555 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16556
16557 if (row->y < min_y)
16558 row->visible_height -= min_y - row->y;
16559 if (row->y + row->height > max_y)
16560 row->visible_height -= row->y + row->height - max_y;
16561 }
16562 else
16563 {
16564 row->pixel_width = row->used[TEXT_AREA];
16565 if (row->continued_p)
16566 row->pixel_width -= it->continuation_pixel_width;
16567 else if (row->truncated_on_right_p)
16568 row->pixel_width -= it->truncation_pixel_width;
16569 row->ascent = row->phys_ascent = 0;
16570 row->height = row->phys_height = row->visible_height = 1;
16571 row->extra_line_spacing = 0;
16572 }
16573
16574 /* Compute a hash code for this row. */
16575 row->hash = 0;
16576 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16577 for (i = 0; i < row->used[area]; ++i)
16578 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16579 + row->glyphs[area][i].u.val
16580 + row->glyphs[area][i].face_id
16581 + row->glyphs[area][i].padding_p
16582 + (row->glyphs[area][i].type << 2));
16583
16584 it->max_ascent = it->max_descent = 0;
16585 it->max_phys_ascent = it->max_phys_descent = 0;
16586 }
16587
16588
16589 /* Append one space to the glyph row of iterator IT if doing a
16590 window-based redisplay. The space has the same face as
16591 IT->face_id. Value is non-zero if a space was added.
16592
16593 This function is called to make sure that there is always one glyph
16594 at the end of a glyph row that the cursor can be set on under
16595 window-systems. (If there weren't such a glyph we would not know
16596 how wide and tall a box cursor should be displayed).
16597
16598 At the same time this space let's a nicely handle clearing to the
16599 end of the line if the row ends in italic text. */
16600
16601 static int
16602 append_space_for_newline (it, default_face_p)
16603 struct it *it;
16604 int default_face_p;
16605 {
16606 if (FRAME_WINDOW_P (it->f))
16607 {
16608 int n = it->glyph_row->used[TEXT_AREA];
16609
16610 if (it->glyph_row->glyphs[TEXT_AREA] + n
16611 < it->glyph_row->glyphs[1 + TEXT_AREA])
16612 {
16613 /* Save some values that must not be changed.
16614 Must save IT->c and IT->len because otherwise
16615 ITERATOR_AT_END_P wouldn't work anymore after
16616 append_space_for_newline has been called. */
16617 enum display_element_type saved_what = it->what;
16618 int saved_c = it->c, saved_len = it->len;
16619 int saved_x = it->current_x;
16620 int saved_face_id = it->face_id;
16621 struct text_pos saved_pos;
16622 Lisp_Object saved_object;
16623 struct face *face;
16624
16625 saved_object = it->object;
16626 saved_pos = it->position;
16627
16628 it->what = IT_CHARACTER;
16629 bzero (&it->position, sizeof it->position);
16630 it->object = make_number (0);
16631 it->c = ' ';
16632 it->len = 1;
16633
16634 if (default_face_p)
16635 it->face_id = DEFAULT_FACE_ID;
16636 else if (it->face_before_selective_p)
16637 it->face_id = it->saved_face_id;
16638 face = FACE_FROM_ID (it->f, it->face_id);
16639 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16640
16641 PRODUCE_GLYPHS (it);
16642
16643 it->override_ascent = -1;
16644 it->constrain_row_ascent_descent_p = 0;
16645 it->current_x = saved_x;
16646 it->object = saved_object;
16647 it->position = saved_pos;
16648 it->what = saved_what;
16649 it->face_id = saved_face_id;
16650 it->len = saved_len;
16651 it->c = saved_c;
16652 return 1;
16653 }
16654 }
16655
16656 return 0;
16657 }
16658
16659
16660 /* Extend the face of the last glyph in the text area of IT->glyph_row
16661 to the end of the display line. Called from display_line.
16662 If the glyph row is empty, add a space glyph to it so that we
16663 know the face to draw. Set the glyph row flag fill_line_p. */
16664
16665 static void
16666 extend_face_to_end_of_line (it)
16667 struct it *it;
16668 {
16669 struct face *face;
16670 struct frame *f = it->f;
16671
16672 /* If line is already filled, do nothing. */
16673 if (it->current_x >= it->last_visible_x)
16674 return;
16675
16676 /* Face extension extends the background and box of IT->face_id
16677 to the end of the line. If the background equals the background
16678 of the frame, we don't have to do anything. */
16679 if (it->face_before_selective_p)
16680 face = FACE_FROM_ID (it->f, it->saved_face_id);
16681 else
16682 face = FACE_FROM_ID (f, it->face_id);
16683
16684 if (FRAME_WINDOW_P (f)
16685 && it->glyph_row->displays_text_p
16686 && face->box == FACE_NO_BOX
16687 && face->background == FRAME_BACKGROUND_PIXEL (f)
16688 && !face->stipple)
16689 return;
16690
16691 /* Set the glyph row flag indicating that the face of the last glyph
16692 in the text area has to be drawn to the end of the text area. */
16693 it->glyph_row->fill_line_p = 1;
16694
16695 /* If current character of IT is not ASCII, make sure we have the
16696 ASCII face. This will be automatically undone the next time
16697 get_next_display_element returns a multibyte character. Note
16698 that the character will always be single byte in unibyte
16699 text. */
16700 if (!ASCII_CHAR_P (it->c))
16701 {
16702 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16703 }
16704
16705 if (FRAME_WINDOW_P (f))
16706 {
16707 /* If the row is empty, add a space with the current face of IT,
16708 so that we know which face to draw. */
16709 if (it->glyph_row->used[TEXT_AREA] == 0)
16710 {
16711 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16712 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16713 it->glyph_row->used[TEXT_AREA] = 1;
16714 }
16715 }
16716 else
16717 {
16718 /* Save some values that must not be changed. */
16719 int saved_x = it->current_x;
16720 struct text_pos saved_pos;
16721 Lisp_Object saved_object;
16722 enum display_element_type saved_what = it->what;
16723 int saved_face_id = it->face_id;
16724
16725 saved_object = it->object;
16726 saved_pos = it->position;
16727
16728 it->what = IT_CHARACTER;
16729 bzero (&it->position, sizeof it->position);
16730 it->object = make_number (0);
16731 it->c = ' ';
16732 it->len = 1;
16733 it->face_id = face->id;
16734
16735 PRODUCE_GLYPHS (it);
16736
16737 while (it->current_x <= it->last_visible_x)
16738 PRODUCE_GLYPHS (it);
16739
16740 /* Don't count these blanks really. It would let us insert a left
16741 truncation glyph below and make us set the cursor on them, maybe. */
16742 it->current_x = saved_x;
16743 it->object = saved_object;
16744 it->position = saved_pos;
16745 it->what = saved_what;
16746 it->face_id = saved_face_id;
16747 }
16748 }
16749
16750
16751 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16752 trailing whitespace. */
16753
16754 static int
16755 trailing_whitespace_p (charpos)
16756 int charpos;
16757 {
16758 int bytepos = CHAR_TO_BYTE (charpos);
16759 int c = 0;
16760
16761 while (bytepos < ZV_BYTE
16762 && (c = FETCH_CHAR (bytepos),
16763 c == ' ' || c == '\t'))
16764 ++bytepos;
16765
16766 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16767 {
16768 if (bytepos != PT_BYTE)
16769 return 1;
16770 }
16771 return 0;
16772 }
16773
16774
16775 /* Highlight trailing whitespace, if any, in ROW. */
16776
16777 void
16778 highlight_trailing_whitespace (f, row)
16779 struct frame *f;
16780 struct glyph_row *row;
16781 {
16782 int used = row->used[TEXT_AREA];
16783
16784 if (used)
16785 {
16786 struct glyph *start = row->glyphs[TEXT_AREA];
16787 struct glyph *glyph = start + used - 1;
16788
16789 /* Skip over glyphs inserted to display the cursor at the
16790 end of a line, for extending the face of the last glyph
16791 to the end of the line on terminals, and for truncation
16792 and continuation glyphs. */
16793 while (glyph >= start
16794 && glyph->type == CHAR_GLYPH
16795 && INTEGERP (glyph->object))
16796 --glyph;
16797
16798 /* If last glyph is a space or stretch, and it's trailing
16799 whitespace, set the face of all trailing whitespace glyphs in
16800 IT->glyph_row to `trailing-whitespace'. */
16801 if (glyph >= start
16802 && BUFFERP (glyph->object)
16803 && (glyph->type == STRETCH_GLYPH
16804 || (glyph->type == CHAR_GLYPH
16805 && glyph->u.ch == ' '))
16806 && trailing_whitespace_p (glyph->charpos))
16807 {
16808 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16809 if (face_id < 0)
16810 return;
16811
16812 while (glyph >= start
16813 && BUFFERP (glyph->object)
16814 && (glyph->type == STRETCH_GLYPH
16815 || (glyph->type == CHAR_GLYPH
16816 && glyph->u.ch == ' ')))
16817 (glyph--)->face_id = face_id;
16818 }
16819 }
16820 }
16821
16822
16823 /* Value is non-zero if glyph row ROW in window W should be
16824 used to hold the cursor. */
16825
16826 static int
16827 cursor_row_p (w, row)
16828 struct window *w;
16829 struct glyph_row *row;
16830 {
16831 int cursor_row_p = 1;
16832
16833 if (PT == MATRIX_ROW_END_CHARPOS (row))
16834 {
16835 /* Suppose the row ends on a string.
16836 Unless the row is continued, that means it ends on a newline
16837 in the string. If it's anything other than a display string
16838 (e.g. a before-string from an overlay), we don't want the
16839 cursor there. (This heuristic seems to give the optimal
16840 behavior for the various types of multi-line strings.) */
16841 if (CHARPOS (row->end.string_pos) >= 0)
16842 {
16843 if (row->continued_p)
16844 cursor_row_p = 1;
16845 else
16846 {
16847 /* Check for `display' property. */
16848 struct glyph *beg = row->glyphs[TEXT_AREA];
16849 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16850 struct glyph *glyph;
16851
16852 cursor_row_p = 0;
16853 for (glyph = end; glyph >= beg; --glyph)
16854 if (STRINGP (glyph->object))
16855 {
16856 Lisp_Object prop
16857 = Fget_char_property (make_number (PT),
16858 Qdisplay, Qnil);
16859 cursor_row_p =
16860 (!NILP (prop)
16861 && display_prop_string_p (prop, glyph->object));
16862 break;
16863 }
16864 }
16865 }
16866 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16867 {
16868 /* If the row ends in middle of a real character,
16869 and the line is continued, we want the cursor here.
16870 That's because MATRIX_ROW_END_CHARPOS would equal
16871 PT if PT is before the character. */
16872 if (!row->ends_in_ellipsis_p)
16873 cursor_row_p = row->continued_p;
16874 else
16875 /* If the row ends in an ellipsis, then
16876 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16877 We want that position to be displayed after the ellipsis. */
16878 cursor_row_p = 0;
16879 }
16880 /* If the row ends at ZV, display the cursor at the end of that
16881 row instead of at the start of the row below. */
16882 else if (row->ends_at_zv_p)
16883 cursor_row_p = 1;
16884 else
16885 cursor_row_p = 0;
16886 }
16887
16888 return cursor_row_p;
16889 }
16890
16891 \f
16892
16893 /* Push the display property PROP so that it will be rendered at the
16894 current position in IT. Return 1 if PROP was successfully pushed,
16895 0 otherwise. */
16896
16897 static int
16898 push_display_prop (struct it *it, Lisp_Object prop)
16899 {
16900 push_it (it);
16901
16902 if (STRINGP (prop))
16903 {
16904 if (SCHARS (prop) == 0)
16905 {
16906 pop_it (it);
16907 return 0;
16908 }
16909
16910 it->string = prop;
16911 it->multibyte_p = STRING_MULTIBYTE (it->string);
16912 it->current.overlay_string_index = -1;
16913 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16914 it->end_charpos = it->string_nchars = SCHARS (it->string);
16915 it->method = GET_FROM_STRING;
16916 it->stop_charpos = 0;
16917 }
16918 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16919 {
16920 it->method = GET_FROM_STRETCH;
16921 it->object = prop;
16922 }
16923 #ifdef HAVE_WINDOW_SYSTEM
16924 else if (IMAGEP (prop))
16925 {
16926 it->what = IT_IMAGE;
16927 it->image_id = lookup_image (it->f, prop);
16928 it->method = GET_FROM_IMAGE;
16929 }
16930 #endif /* HAVE_WINDOW_SYSTEM */
16931 else
16932 {
16933 pop_it (it); /* bogus display property, give up */
16934 return 0;
16935 }
16936
16937 return 1;
16938 }
16939
16940 /* Return the character-property PROP at the current position in IT. */
16941
16942 static Lisp_Object
16943 get_it_property (it, prop)
16944 struct it *it;
16945 Lisp_Object prop;
16946 {
16947 Lisp_Object position;
16948
16949 if (STRINGP (it->object))
16950 position = make_number (IT_STRING_CHARPOS (*it));
16951 else if (BUFFERP (it->object))
16952 position = make_number (IT_CHARPOS (*it));
16953 else
16954 return Qnil;
16955
16956 return Fget_char_property (position, prop, it->object);
16957 }
16958
16959 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16960
16961 static void
16962 handle_line_prefix (struct it *it)
16963 {
16964 Lisp_Object prefix;
16965 if (it->continuation_lines_width > 0)
16966 {
16967 prefix = get_it_property (it, Qwrap_prefix);
16968 if (NILP (prefix))
16969 prefix = Vwrap_prefix;
16970 }
16971 else
16972 {
16973 prefix = get_it_property (it, Qline_prefix);
16974 if (NILP (prefix))
16975 prefix = Vline_prefix;
16976 }
16977 if (! NILP (prefix) && push_display_prop (it, prefix))
16978 {
16979 /* If the prefix is wider than the window, and we try to wrap
16980 it, it would acquire its own wrap prefix, and so on till the
16981 iterator stack overflows. So, don't wrap the prefix. */
16982 it->line_wrap = TRUNCATE;
16983 it->avoid_cursor_p = 1;
16984 }
16985 }
16986
16987 \f
16988
16989 /* Construct the glyph row IT->glyph_row in the desired matrix of
16990 IT->w from text at the current position of IT. See dispextern.h
16991 for an overview of struct it. Value is non-zero if
16992 IT->glyph_row displays text, as opposed to a line displaying ZV
16993 only. */
16994
16995 static int
16996 display_line (it)
16997 struct it *it;
16998 {
16999 struct glyph_row *row = it->glyph_row;
17000 Lisp_Object overlay_arrow_string;
17001 struct it wrap_it;
17002 int may_wrap = 0, wrap_x;
17003 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17004 int wrap_row_phys_ascent, wrap_row_phys_height;
17005 int wrap_row_extra_line_spacing;
17006 struct display_pos row_end;
17007
17008 /* We always start displaying at hpos zero even if hscrolled. */
17009 xassert (it->hpos == 0 && it->current_x == 0);
17010
17011 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17012 >= it->w->desired_matrix->nrows)
17013 {
17014 it->w->nrows_scale_factor++;
17015 fonts_changed_p = 1;
17016 return 0;
17017 }
17018
17019 /* Is IT->w showing the region? */
17020 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17021
17022 /* Clear the result glyph row and enable it. */
17023 prepare_desired_row (row);
17024
17025 row->y = it->current_y;
17026 row->start = it->start;
17027 row->continuation_lines_width = it->continuation_lines_width;
17028 row->displays_text_p = 1;
17029 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17030 it->starts_in_middle_of_char_p = 0;
17031 /* If the paragraph base direction is R2L, its glyphs should be
17032 reversed. */
17033 if (it->bidi_p && (it->bidi_it.level_stack[0].level & 1) != 0)
17034 row->reversed_p = 1;
17035
17036 /* Arrange the overlays nicely for our purposes. Usually, we call
17037 display_line on only one line at a time, in which case this
17038 can't really hurt too much, or we call it on lines which appear
17039 one after another in the buffer, in which case all calls to
17040 recenter_overlay_lists but the first will be pretty cheap. */
17041 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17042
17043 /* Move over display elements that are not visible because we are
17044 hscrolled. This may stop at an x-position < IT->first_visible_x
17045 if the first glyph is partially visible or if we hit a line end. */
17046 if (it->current_x < it->first_visible_x)
17047 {
17048 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17049 MOVE_TO_POS | MOVE_TO_X);
17050 }
17051 else
17052 {
17053 /* We only do this when not calling `move_it_in_display_line_to'
17054 above, because move_it_in_display_line_to calls
17055 handle_line_prefix itself. */
17056 handle_line_prefix (it);
17057 }
17058
17059 /* Get the initial row height. This is either the height of the
17060 text hscrolled, if there is any, or zero. */
17061 row->ascent = it->max_ascent;
17062 row->height = it->max_ascent + it->max_descent;
17063 row->phys_ascent = it->max_phys_ascent;
17064 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17065 row->extra_line_spacing = it->max_extra_line_spacing;
17066
17067 /* Loop generating characters. The loop is left with IT on the next
17068 character to display. */
17069 while (1)
17070 {
17071 int n_glyphs_before, hpos_before, x_before;
17072 int x, i, nglyphs;
17073 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17074
17075 /* Retrieve the next thing to display. Value is zero if end of
17076 buffer reached. */
17077 if (!get_next_display_element (it))
17078 {
17079 /* Maybe add a space at the end of this line that is used to
17080 display the cursor there under X. Set the charpos of the
17081 first glyph of blank lines not corresponding to any text
17082 to -1. */
17083 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17084 row->exact_window_width_line_p = 1;
17085 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17086 || row->used[TEXT_AREA] == 0)
17087 {
17088 row->glyphs[TEXT_AREA]->charpos = -1;
17089 row->displays_text_p = 0;
17090
17091 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17092 && (!MINI_WINDOW_P (it->w)
17093 || (minibuf_level && EQ (it->window, minibuf_window))))
17094 row->indicate_empty_line_p = 1;
17095 }
17096
17097 it->continuation_lines_width = 0;
17098 row->ends_at_zv_p = 1;
17099 /* A row that displays right-to-left text must always have
17100 its last face extended all the way to the end of line,
17101 even if this row ends in ZV. */
17102 if (row->reversed_p)
17103 extend_face_to_end_of_line (it);
17104 row_end = it->current;
17105 break;
17106 }
17107
17108 /* Now, get the metrics of what we want to display. This also
17109 generates glyphs in `row' (which is IT->glyph_row). */
17110 n_glyphs_before = row->used[TEXT_AREA];
17111 x = it->current_x;
17112
17113 /* Remember the line height so far in case the next element doesn't
17114 fit on the line. */
17115 if (it->line_wrap != TRUNCATE)
17116 {
17117 ascent = it->max_ascent;
17118 descent = it->max_descent;
17119 phys_ascent = it->max_phys_ascent;
17120 phys_descent = it->max_phys_descent;
17121
17122 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17123 {
17124 if (IT_DISPLAYING_WHITESPACE (it))
17125 may_wrap = 1;
17126 else if (may_wrap)
17127 {
17128 wrap_it = *it;
17129 wrap_x = x;
17130 wrap_row_used = row->used[TEXT_AREA];
17131 wrap_row_ascent = row->ascent;
17132 wrap_row_height = row->height;
17133 wrap_row_phys_ascent = row->phys_ascent;
17134 wrap_row_phys_height = row->phys_height;
17135 wrap_row_extra_line_spacing = row->extra_line_spacing;
17136 may_wrap = 0;
17137 }
17138 }
17139 }
17140
17141 PRODUCE_GLYPHS (it);
17142
17143 /* If this display element was in marginal areas, continue with
17144 the next one. */
17145 if (it->area != TEXT_AREA)
17146 {
17147 row->ascent = max (row->ascent, it->max_ascent);
17148 row->height = max (row->height, it->max_ascent + it->max_descent);
17149 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17150 row->phys_height = max (row->phys_height,
17151 it->max_phys_ascent + it->max_phys_descent);
17152 row->extra_line_spacing = max (row->extra_line_spacing,
17153 it->max_extra_line_spacing);
17154 set_iterator_to_next (it, 1);
17155 continue;
17156 }
17157
17158 /* Does the display element fit on the line? If we truncate
17159 lines, we should draw past the right edge of the window. If
17160 we don't truncate, we want to stop so that we can display the
17161 continuation glyph before the right margin. If lines are
17162 continued, there are two possible strategies for characters
17163 resulting in more than 1 glyph (e.g. tabs): Display as many
17164 glyphs as possible in this line and leave the rest for the
17165 continuation line, or display the whole element in the next
17166 line. Original redisplay did the former, so we do it also. */
17167 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17168 hpos_before = it->hpos;
17169 x_before = x;
17170
17171 if (/* Not a newline. */
17172 nglyphs > 0
17173 /* Glyphs produced fit entirely in the line. */
17174 && it->current_x < it->last_visible_x)
17175 {
17176 it->hpos += nglyphs;
17177 row->ascent = max (row->ascent, it->max_ascent);
17178 row->height = max (row->height, it->max_ascent + it->max_descent);
17179 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17180 row->phys_height = max (row->phys_height,
17181 it->max_phys_ascent + it->max_phys_descent);
17182 row->extra_line_spacing = max (row->extra_line_spacing,
17183 it->max_extra_line_spacing);
17184 if (it->current_x - it->pixel_width < it->first_visible_x)
17185 row->x = x - it->first_visible_x;
17186 }
17187 else
17188 {
17189 int new_x;
17190 struct glyph *glyph;
17191
17192 for (i = 0; i < nglyphs; ++i, x = new_x)
17193 {
17194 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17195 new_x = x + glyph->pixel_width;
17196
17197 if (/* Lines are continued. */
17198 it->line_wrap != TRUNCATE
17199 && (/* Glyph doesn't fit on the line. */
17200 new_x > it->last_visible_x
17201 /* Or it fits exactly on a window system frame. */
17202 || (new_x == it->last_visible_x
17203 && FRAME_WINDOW_P (it->f))))
17204 {
17205 /* End of a continued line. */
17206
17207 if (it->hpos == 0
17208 || (new_x == it->last_visible_x
17209 && FRAME_WINDOW_P (it->f)))
17210 {
17211 /* Current glyph is the only one on the line or
17212 fits exactly on the line. We must continue
17213 the line because we can't draw the cursor
17214 after the glyph. */
17215 row->continued_p = 1;
17216 it->current_x = new_x;
17217 it->continuation_lines_width += new_x;
17218 ++it->hpos;
17219 if (i == nglyphs - 1)
17220 {
17221 /* If line-wrap is on, check if a previous
17222 wrap point was found. */
17223 if (wrap_row_used > 0
17224 /* Even if there is a previous wrap
17225 point, continue the line here as
17226 usual, if (i) the previous character
17227 was a space or tab AND (ii) the
17228 current character is not. */
17229 && (!may_wrap
17230 || IT_DISPLAYING_WHITESPACE (it)))
17231 goto back_to_wrap;
17232
17233 set_iterator_to_next (it, 1);
17234 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17235 {
17236 if (!get_next_display_element (it))
17237 {
17238 row->exact_window_width_line_p = 1;
17239 it->continuation_lines_width = 0;
17240 row->continued_p = 0;
17241 row->ends_at_zv_p = 1;
17242 }
17243 else if (ITERATOR_AT_END_OF_LINE_P (it))
17244 {
17245 row->continued_p = 0;
17246 row->exact_window_width_line_p = 1;
17247 }
17248 }
17249 }
17250 }
17251 else if (CHAR_GLYPH_PADDING_P (*glyph)
17252 && !FRAME_WINDOW_P (it->f))
17253 {
17254 /* A padding glyph that doesn't fit on this line.
17255 This means the whole character doesn't fit
17256 on the line. */
17257 row->used[TEXT_AREA] = n_glyphs_before;
17258
17259 /* Fill the rest of the row with continuation
17260 glyphs like in 20.x. */
17261 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17262 < row->glyphs[1 + TEXT_AREA])
17263 produce_special_glyphs (it, IT_CONTINUATION);
17264
17265 row->continued_p = 1;
17266 it->current_x = x_before;
17267 it->continuation_lines_width += x_before;
17268
17269 /* Restore the height to what it was before the
17270 element not fitting on the line. */
17271 it->max_ascent = ascent;
17272 it->max_descent = descent;
17273 it->max_phys_ascent = phys_ascent;
17274 it->max_phys_descent = phys_descent;
17275 }
17276 else if (wrap_row_used > 0)
17277 {
17278 back_to_wrap:
17279 *it = wrap_it;
17280 it->continuation_lines_width += wrap_x;
17281 row->used[TEXT_AREA] = wrap_row_used;
17282 row->ascent = wrap_row_ascent;
17283 row->height = wrap_row_height;
17284 row->phys_ascent = wrap_row_phys_ascent;
17285 row->phys_height = wrap_row_phys_height;
17286 row->extra_line_spacing = wrap_row_extra_line_spacing;
17287 row->continued_p = 1;
17288 row->ends_at_zv_p = 0;
17289 row->exact_window_width_line_p = 0;
17290 it->continuation_lines_width += x;
17291
17292 /* Make sure that a non-default face is extended
17293 up to the right margin of the window. */
17294 extend_face_to_end_of_line (it);
17295 }
17296 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17297 {
17298 /* A TAB that extends past the right edge of the
17299 window. This produces a single glyph on
17300 window system frames. We leave the glyph in
17301 this row and let it fill the row, but don't
17302 consume the TAB. */
17303 it->continuation_lines_width += it->last_visible_x;
17304 row->ends_in_middle_of_char_p = 1;
17305 row->continued_p = 1;
17306 glyph->pixel_width = it->last_visible_x - x;
17307 it->starts_in_middle_of_char_p = 1;
17308 }
17309 else
17310 {
17311 /* Something other than a TAB that draws past
17312 the right edge of the window. Restore
17313 positions to values before the element. */
17314 row->used[TEXT_AREA] = n_glyphs_before + i;
17315
17316 /* Display continuation glyphs. */
17317 if (!FRAME_WINDOW_P (it->f))
17318 produce_special_glyphs (it, IT_CONTINUATION);
17319 row->continued_p = 1;
17320
17321 it->current_x = x_before;
17322 it->continuation_lines_width += x;
17323 extend_face_to_end_of_line (it);
17324
17325 if (nglyphs > 1 && i > 0)
17326 {
17327 row->ends_in_middle_of_char_p = 1;
17328 it->starts_in_middle_of_char_p = 1;
17329 }
17330
17331 /* Restore the height to what it was before the
17332 element not fitting on the line. */
17333 it->max_ascent = ascent;
17334 it->max_descent = descent;
17335 it->max_phys_ascent = phys_ascent;
17336 it->max_phys_descent = phys_descent;
17337 }
17338
17339 row_end = it->current;
17340 break;
17341 }
17342 else if (new_x > it->first_visible_x)
17343 {
17344 /* Increment number of glyphs actually displayed. */
17345 ++it->hpos;
17346
17347 if (x < it->first_visible_x)
17348 /* Glyph is partially visible, i.e. row starts at
17349 negative X position. */
17350 row->x = x - it->first_visible_x;
17351 }
17352 else
17353 {
17354 /* Glyph is completely off the left margin of the
17355 window. This should not happen because of the
17356 move_it_in_display_line at the start of this
17357 function, unless the text display area of the
17358 window is empty. */
17359 xassert (it->first_visible_x <= it->last_visible_x);
17360 }
17361 }
17362
17363 row->ascent = max (row->ascent, it->max_ascent);
17364 row->height = max (row->height, it->max_ascent + it->max_descent);
17365 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17366 row->phys_height = max (row->phys_height,
17367 it->max_phys_ascent + it->max_phys_descent);
17368 row->extra_line_spacing = max (row->extra_line_spacing,
17369 it->max_extra_line_spacing);
17370
17371 /* End of this display line if row is continued. */
17372 if (row->continued_p || row->ends_at_zv_p)
17373 {
17374 row_end = it->current;
17375 break;
17376 }
17377 }
17378
17379 at_end_of_line:
17380 /* Is this a line end? If yes, we're also done, after making
17381 sure that a non-default face is extended up to the right
17382 margin of the window. */
17383 if (ITERATOR_AT_END_OF_LINE_P (it))
17384 {
17385 int used_before = row->used[TEXT_AREA];
17386
17387 row->ends_in_newline_from_string_p = STRINGP (it->object);
17388
17389 /* Add a space at the end of the line that is used to
17390 display the cursor there. */
17391 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17392 append_space_for_newline (it, 0);
17393
17394 /* Extend the face to the end of the line. */
17395 extend_face_to_end_of_line (it);
17396
17397 /* Make sure we have the position. */
17398 if (used_before == 0)
17399 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17400
17401 /* Consume the line end. This skips over invisible lines. */
17402 if (it->bidi_p)
17403 {
17404 /* When we are reordering bidi text, we still need the
17405 next character in logical order, to set row->end
17406 correctly below. */
17407 push_it (it);
17408 it->bidi_p = 0;
17409 set_iterator_to_next (it, 1);
17410 row_end = it->current;
17411 pop_it (it);
17412 it->bidi_p = 1;
17413 }
17414 set_iterator_to_next (it, 1);
17415 it->continuation_lines_width = 0;
17416 if (!it->bidi_p)
17417 row_end = it->current;
17418 break;
17419 }
17420
17421 /* Proceed with next display element. Note that this skips
17422 over lines invisible because of selective display. */
17423 set_iterator_to_next (it, 1);
17424
17425 /* If we truncate lines, we are done when the last displayed
17426 glyphs reach past the right margin of the window. */
17427 if (it->line_wrap == TRUNCATE
17428 && (FRAME_WINDOW_P (it->f)
17429 ? (it->current_x >= it->last_visible_x)
17430 : (it->current_x > it->last_visible_x)))
17431 {
17432 /* Maybe add truncation glyphs. */
17433 if (!FRAME_WINDOW_P (it->f))
17434 {
17435 int i, n;
17436
17437 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17438 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17439 break;
17440
17441 for (n = row->used[TEXT_AREA]; i < n; ++i)
17442 {
17443 row->used[TEXT_AREA] = i;
17444 produce_special_glyphs (it, IT_TRUNCATION);
17445 }
17446 }
17447 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17448 {
17449 /* Don't truncate if we can overflow newline into fringe. */
17450 if (!get_next_display_element (it))
17451 {
17452 it->continuation_lines_width = 0;
17453 row->ends_at_zv_p = 1;
17454 row->exact_window_width_line_p = 1;
17455 row_end = it->current;
17456 break;
17457 }
17458 if (ITERATOR_AT_END_OF_LINE_P (it))
17459 {
17460 row->exact_window_width_line_p = 1;
17461 goto at_end_of_line;
17462 }
17463 }
17464
17465 row->truncated_on_right_p = 1;
17466 it->continuation_lines_width = 0;
17467 reseat_at_next_visible_line_start (it, 0);
17468 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17469 it->hpos = hpos_before;
17470 it->current_x = x_before;
17471 row_end = it->current;
17472 break;
17473 }
17474 }
17475
17476 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17477 at the left window margin. */
17478 if (it->first_visible_x
17479 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
17480 {
17481 if (!FRAME_WINDOW_P (it->f))
17482 insert_left_trunc_glyphs (it);
17483 row->truncated_on_left_p = 1;
17484 }
17485
17486 /* If the start of this line is the overlay arrow-position, then
17487 mark this glyph row as the one containing the overlay arrow.
17488 This is clearly a mess with variable size fonts. It would be
17489 better to let it be displayed like cursors under X. */
17490 if ((row->displays_text_p || !overlay_arrow_seen)
17491 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17492 !NILP (overlay_arrow_string)))
17493 {
17494 /* Overlay arrow in window redisplay is a fringe bitmap. */
17495 if (STRINGP (overlay_arrow_string))
17496 {
17497 struct glyph_row *arrow_row
17498 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17499 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17500 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17501 struct glyph *p = row->glyphs[TEXT_AREA];
17502 struct glyph *p2, *end;
17503
17504 /* Copy the arrow glyphs. */
17505 while (glyph < arrow_end)
17506 *p++ = *glyph++;
17507
17508 /* Throw away padding glyphs. */
17509 p2 = p;
17510 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17511 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17512 ++p2;
17513 if (p2 > p)
17514 {
17515 while (p2 < end)
17516 *p++ = *p2++;
17517 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17518 }
17519 }
17520 else
17521 {
17522 xassert (INTEGERP (overlay_arrow_string));
17523 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17524 }
17525 overlay_arrow_seen = 1;
17526 }
17527
17528 /* Compute pixel dimensions of this line. */
17529 compute_line_metrics (it);
17530
17531 /* Remember the position at which this line ends. */
17532 row->end = row_end;
17533
17534 /* Record whether this row ends inside an ellipsis. */
17535 row->ends_in_ellipsis_p
17536 = (it->method == GET_FROM_DISPLAY_VECTOR
17537 && it->ellipsis_p);
17538
17539 /* Save fringe bitmaps in this row. */
17540 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17541 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17542 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17543 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17544
17545 it->left_user_fringe_bitmap = 0;
17546 it->left_user_fringe_face_id = 0;
17547 it->right_user_fringe_bitmap = 0;
17548 it->right_user_fringe_face_id = 0;
17549
17550 /* Maybe set the cursor. */
17551 if (it->w->cursor.vpos < 0
17552 && PT >= MATRIX_ROW_START_CHARPOS (row)
17553 && PT <= MATRIX_ROW_END_CHARPOS (row)
17554 && cursor_row_p (it->w, row))
17555 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17556
17557 /* Highlight trailing whitespace. */
17558 if (!NILP (Vshow_trailing_whitespace))
17559 highlight_trailing_whitespace (it->f, it->glyph_row);
17560
17561 /* Prepare for the next line. This line starts horizontally at (X
17562 HPOS) = (0 0). Vertical positions are incremented. As a
17563 convenience for the caller, IT->glyph_row is set to the next
17564 row to be used. */
17565 it->current_x = it->hpos = 0;
17566 it->current_y += row->height;
17567 ++it->vpos;
17568 ++it->glyph_row;
17569 it->start = row_end;
17570 return row->displays_text_p;
17571 }
17572
17573
17574 \f
17575 /***********************************************************************
17576 Menu Bar
17577 ***********************************************************************/
17578
17579 /* Redisplay the menu bar in the frame for window W.
17580
17581 The menu bar of X frames that don't have X toolkit support is
17582 displayed in a special window W->frame->menu_bar_window.
17583
17584 The menu bar of terminal frames is treated specially as far as
17585 glyph matrices are concerned. Menu bar lines are not part of
17586 windows, so the update is done directly on the frame matrix rows
17587 for the menu bar. */
17588
17589 static void
17590 display_menu_bar (w)
17591 struct window *w;
17592 {
17593 struct frame *f = XFRAME (WINDOW_FRAME (w));
17594 struct it it;
17595 Lisp_Object items;
17596 int i;
17597
17598 /* Don't do all this for graphical frames. */
17599 #ifdef HAVE_NTGUI
17600 if (FRAME_W32_P (f))
17601 return;
17602 #endif
17603 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17604 if (FRAME_X_P (f))
17605 return;
17606 #endif
17607
17608 #ifdef HAVE_NS
17609 if (FRAME_NS_P (f))
17610 return;
17611 #endif /* HAVE_NS */
17612
17613 #ifdef USE_X_TOOLKIT
17614 xassert (!FRAME_WINDOW_P (f));
17615 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17616 it.first_visible_x = 0;
17617 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17618 #else /* not USE_X_TOOLKIT */
17619 if (FRAME_WINDOW_P (f))
17620 {
17621 /* Menu bar lines are displayed in the desired matrix of the
17622 dummy window menu_bar_window. */
17623 struct window *menu_w;
17624 xassert (WINDOWP (f->menu_bar_window));
17625 menu_w = XWINDOW (f->menu_bar_window);
17626 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17627 MENU_FACE_ID);
17628 it.first_visible_x = 0;
17629 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17630 }
17631 else
17632 {
17633 /* This is a TTY frame, i.e. character hpos/vpos are used as
17634 pixel x/y. */
17635 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17636 MENU_FACE_ID);
17637 it.first_visible_x = 0;
17638 it.last_visible_x = FRAME_COLS (f);
17639 }
17640 #endif /* not USE_X_TOOLKIT */
17641
17642 if (! mode_line_inverse_video)
17643 /* Force the menu-bar to be displayed in the default face. */
17644 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17645
17646 /* Clear all rows of the menu bar. */
17647 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17648 {
17649 struct glyph_row *row = it.glyph_row + i;
17650 clear_glyph_row (row);
17651 row->enabled_p = 1;
17652 row->full_width_p = 1;
17653 }
17654
17655 /* Display all items of the menu bar. */
17656 items = FRAME_MENU_BAR_ITEMS (it.f);
17657 for (i = 0; i < XVECTOR (items)->size; i += 4)
17658 {
17659 Lisp_Object string;
17660
17661 /* Stop at nil string. */
17662 string = AREF (items, i + 1);
17663 if (NILP (string))
17664 break;
17665
17666 /* Remember where item was displayed. */
17667 ASET (items, i + 3, make_number (it.hpos));
17668
17669 /* Display the item, pad with one space. */
17670 if (it.current_x < it.last_visible_x)
17671 display_string (NULL, string, Qnil, 0, 0, &it,
17672 SCHARS (string) + 1, 0, 0, -1);
17673 }
17674
17675 /* Fill out the line with spaces. */
17676 if (it.current_x < it.last_visible_x)
17677 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17678
17679 /* Compute the total height of the lines. */
17680 compute_line_metrics (&it);
17681 }
17682
17683
17684 \f
17685 /***********************************************************************
17686 Mode Line
17687 ***********************************************************************/
17688
17689 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17690 FORCE is non-zero, redisplay mode lines unconditionally.
17691 Otherwise, redisplay only mode lines that are garbaged. Value is
17692 the number of windows whose mode lines were redisplayed. */
17693
17694 static int
17695 redisplay_mode_lines (window, force)
17696 Lisp_Object window;
17697 int force;
17698 {
17699 int nwindows = 0;
17700
17701 while (!NILP (window))
17702 {
17703 struct window *w = XWINDOW (window);
17704
17705 if (WINDOWP (w->hchild))
17706 nwindows += redisplay_mode_lines (w->hchild, force);
17707 else if (WINDOWP (w->vchild))
17708 nwindows += redisplay_mode_lines (w->vchild, force);
17709 else if (force
17710 || FRAME_GARBAGED_P (XFRAME (w->frame))
17711 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17712 {
17713 struct text_pos lpoint;
17714 struct buffer *old = current_buffer;
17715
17716 /* Set the window's buffer for the mode line display. */
17717 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17718 set_buffer_internal_1 (XBUFFER (w->buffer));
17719
17720 /* Point refers normally to the selected window. For any
17721 other window, set up appropriate value. */
17722 if (!EQ (window, selected_window))
17723 {
17724 struct text_pos pt;
17725
17726 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17727 if (CHARPOS (pt) < BEGV)
17728 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17729 else if (CHARPOS (pt) > (ZV - 1))
17730 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17731 else
17732 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17733 }
17734
17735 /* Display mode lines. */
17736 clear_glyph_matrix (w->desired_matrix);
17737 if (display_mode_lines (w))
17738 {
17739 ++nwindows;
17740 w->must_be_updated_p = 1;
17741 }
17742
17743 /* Restore old settings. */
17744 set_buffer_internal_1 (old);
17745 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17746 }
17747
17748 window = w->next;
17749 }
17750
17751 return nwindows;
17752 }
17753
17754
17755 /* Display the mode and/or header line of window W. Value is the
17756 sum number of mode lines and header lines displayed. */
17757
17758 static int
17759 display_mode_lines (w)
17760 struct window *w;
17761 {
17762 Lisp_Object old_selected_window, old_selected_frame;
17763 int n = 0;
17764
17765 old_selected_frame = selected_frame;
17766 selected_frame = w->frame;
17767 old_selected_window = selected_window;
17768 XSETWINDOW (selected_window, w);
17769
17770 /* These will be set while the mode line specs are processed. */
17771 line_number_displayed = 0;
17772 w->column_number_displayed = Qnil;
17773
17774 if (WINDOW_WANTS_MODELINE_P (w))
17775 {
17776 struct window *sel_w = XWINDOW (old_selected_window);
17777
17778 /* Select mode line face based on the real selected window. */
17779 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17780 current_buffer->mode_line_format);
17781 ++n;
17782 }
17783
17784 if (WINDOW_WANTS_HEADER_LINE_P (w))
17785 {
17786 display_mode_line (w, HEADER_LINE_FACE_ID,
17787 current_buffer->header_line_format);
17788 ++n;
17789 }
17790
17791 selected_frame = old_selected_frame;
17792 selected_window = old_selected_window;
17793 return n;
17794 }
17795
17796
17797 /* Display mode or header line of window W. FACE_ID specifies which
17798 line to display; it is either MODE_LINE_FACE_ID or
17799 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17800 display. Value is the pixel height of the mode/header line
17801 displayed. */
17802
17803 static int
17804 display_mode_line (w, face_id, format)
17805 struct window *w;
17806 enum face_id face_id;
17807 Lisp_Object format;
17808 {
17809 struct it it;
17810 struct face *face;
17811 int count = SPECPDL_INDEX ();
17812
17813 init_iterator (&it, w, -1, -1, NULL, face_id);
17814 /* Don't extend on a previously drawn mode-line.
17815 This may happen if called from pos_visible_p. */
17816 it.glyph_row->enabled_p = 0;
17817 prepare_desired_row (it.glyph_row);
17818
17819 it.glyph_row->mode_line_p = 1;
17820
17821 if (! mode_line_inverse_video)
17822 /* Force the mode-line to be displayed in the default face. */
17823 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17824
17825 record_unwind_protect (unwind_format_mode_line,
17826 format_mode_line_unwind_data (NULL, Qnil, 0));
17827
17828 mode_line_target = MODE_LINE_DISPLAY;
17829
17830 /* Temporarily make frame's keyboard the current kboard so that
17831 kboard-local variables in the mode_line_format will get the right
17832 values. */
17833 push_kboard (FRAME_KBOARD (it.f));
17834 record_unwind_save_match_data ();
17835 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17836 pop_kboard ();
17837
17838 unbind_to (count, Qnil);
17839
17840 /* Fill up with spaces. */
17841 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17842
17843 compute_line_metrics (&it);
17844 it.glyph_row->full_width_p = 1;
17845 it.glyph_row->continued_p = 0;
17846 it.glyph_row->truncated_on_left_p = 0;
17847 it.glyph_row->truncated_on_right_p = 0;
17848
17849 /* Make a 3D mode-line have a shadow at its right end. */
17850 face = FACE_FROM_ID (it.f, face_id);
17851 extend_face_to_end_of_line (&it);
17852 if (face->box != FACE_NO_BOX)
17853 {
17854 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17855 + it.glyph_row->used[TEXT_AREA] - 1);
17856 last->right_box_line_p = 1;
17857 }
17858
17859 return it.glyph_row->height;
17860 }
17861
17862 /* Move element ELT in LIST to the front of LIST.
17863 Return the updated list. */
17864
17865 static Lisp_Object
17866 move_elt_to_front (elt, list)
17867 Lisp_Object elt, list;
17868 {
17869 register Lisp_Object tail, prev;
17870 register Lisp_Object tem;
17871
17872 tail = list;
17873 prev = Qnil;
17874 while (CONSP (tail))
17875 {
17876 tem = XCAR (tail);
17877
17878 if (EQ (elt, tem))
17879 {
17880 /* Splice out the link TAIL. */
17881 if (NILP (prev))
17882 list = XCDR (tail);
17883 else
17884 Fsetcdr (prev, XCDR (tail));
17885
17886 /* Now make it the first. */
17887 Fsetcdr (tail, list);
17888 return tail;
17889 }
17890 else
17891 prev = tail;
17892 tail = XCDR (tail);
17893 QUIT;
17894 }
17895
17896 /* Not found--return unchanged LIST. */
17897 return list;
17898 }
17899
17900 /* Contribute ELT to the mode line for window IT->w. How it
17901 translates into text depends on its data type.
17902
17903 IT describes the display environment in which we display, as usual.
17904
17905 DEPTH is the depth in recursion. It is used to prevent
17906 infinite recursion here.
17907
17908 FIELD_WIDTH is the number of characters the display of ELT should
17909 occupy in the mode line, and PRECISION is the maximum number of
17910 characters to display from ELT's representation. See
17911 display_string for details.
17912
17913 Returns the hpos of the end of the text generated by ELT.
17914
17915 PROPS is a property list to add to any string we encounter.
17916
17917 If RISKY is nonzero, remove (disregard) any properties in any string
17918 we encounter, and ignore :eval and :propertize.
17919
17920 The global variable `mode_line_target' determines whether the
17921 output is passed to `store_mode_line_noprop',
17922 `store_mode_line_string', or `display_string'. */
17923
17924 static int
17925 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17926 struct it *it;
17927 int depth;
17928 int field_width, precision;
17929 Lisp_Object elt, props;
17930 int risky;
17931 {
17932 int n = 0, field, prec;
17933 int literal = 0;
17934
17935 tail_recurse:
17936 if (depth > 100)
17937 elt = build_string ("*too-deep*");
17938
17939 depth++;
17940
17941 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17942 {
17943 case Lisp_String:
17944 {
17945 /* A string: output it and check for %-constructs within it. */
17946 unsigned char c;
17947 int offset = 0;
17948
17949 if (SCHARS (elt) > 0
17950 && (!NILP (props) || risky))
17951 {
17952 Lisp_Object oprops, aelt;
17953 oprops = Ftext_properties_at (make_number (0), elt);
17954
17955 /* If the starting string's properties are not what
17956 we want, translate the string. Also, if the string
17957 is risky, do that anyway. */
17958
17959 if (NILP (Fequal (props, oprops)) || risky)
17960 {
17961 /* If the starting string has properties,
17962 merge the specified ones onto the existing ones. */
17963 if (! NILP (oprops) && !risky)
17964 {
17965 Lisp_Object tem;
17966
17967 oprops = Fcopy_sequence (oprops);
17968 tem = props;
17969 while (CONSP (tem))
17970 {
17971 oprops = Fplist_put (oprops, XCAR (tem),
17972 XCAR (XCDR (tem)));
17973 tem = XCDR (XCDR (tem));
17974 }
17975 props = oprops;
17976 }
17977
17978 aelt = Fassoc (elt, mode_line_proptrans_alist);
17979 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17980 {
17981 /* AELT is what we want. Move it to the front
17982 without consing. */
17983 elt = XCAR (aelt);
17984 mode_line_proptrans_alist
17985 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17986 }
17987 else
17988 {
17989 Lisp_Object tem;
17990
17991 /* If AELT has the wrong props, it is useless.
17992 so get rid of it. */
17993 if (! NILP (aelt))
17994 mode_line_proptrans_alist
17995 = Fdelq (aelt, mode_line_proptrans_alist);
17996
17997 elt = Fcopy_sequence (elt);
17998 Fset_text_properties (make_number (0), Flength (elt),
17999 props, elt);
18000 /* Add this item to mode_line_proptrans_alist. */
18001 mode_line_proptrans_alist
18002 = Fcons (Fcons (elt, props),
18003 mode_line_proptrans_alist);
18004 /* Truncate mode_line_proptrans_alist
18005 to at most 50 elements. */
18006 tem = Fnthcdr (make_number (50),
18007 mode_line_proptrans_alist);
18008 if (! NILP (tem))
18009 XSETCDR (tem, Qnil);
18010 }
18011 }
18012 }
18013
18014 offset = 0;
18015
18016 if (literal)
18017 {
18018 prec = precision - n;
18019 switch (mode_line_target)
18020 {
18021 case MODE_LINE_NOPROP:
18022 case MODE_LINE_TITLE:
18023 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18024 break;
18025 case MODE_LINE_STRING:
18026 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18027 break;
18028 case MODE_LINE_DISPLAY:
18029 n += display_string (NULL, elt, Qnil, 0, 0, it,
18030 0, prec, 0, STRING_MULTIBYTE (elt));
18031 break;
18032 }
18033
18034 break;
18035 }
18036
18037 /* Handle the non-literal case. */
18038
18039 while ((precision <= 0 || n < precision)
18040 && SREF (elt, offset) != 0
18041 && (mode_line_target != MODE_LINE_DISPLAY
18042 || it->current_x < it->last_visible_x))
18043 {
18044 int last_offset = offset;
18045
18046 /* Advance to end of string or next format specifier. */
18047 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18048 ;
18049
18050 if (offset - 1 != last_offset)
18051 {
18052 int nchars, nbytes;
18053
18054 /* Output to end of string or up to '%'. Field width
18055 is length of string. Don't output more than
18056 PRECISION allows us. */
18057 offset--;
18058
18059 prec = c_string_width (SDATA (elt) + last_offset,
18060 offset - last_offset, precision - n,
18061 &nchars, &nbytes);
18062
18063 switch (mode_line_target)
18064 {
18065 case MODE_LINE_NOPROP:
18066 case MODE_LINE_TITLE:
18067 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18068 break;
18069 case MODE_LINE_STRING:
18070 {
18071 int bytepos = last_offset;
18072 int charpos = string_byte_to_char (elt, bytepos);
18073 int endpos = (precision <= 0
18074 ? string_byte_to_char (elt, offset)
18075 : charpos + nchars);
18076
18077 n += store_mode_line_string (NULL,
18078 Fsubstring (elt, make_number (charpos),
18079 make_number (endpos)),
18080 0, 0, 0, Qnil);
18081 }
18082 break;
18083 case MODE_LINE_DISPLAY:
18084 {
18085 int bytepos = last_offset;
18086 int charpos = string_byte_to_char (elt, bytepos);
18087
18088 if (precision <= 0)
18089 nchars = string_byte_to_char (elt, offset) - charpos;
18090 n += display_string (NULL, elt, Qnil, 0, charpos,
18091 it, 0, nchars, 0,
18092 STRING_MULTIBYTE (elt));
18093 }
18094 break;
18095 }
18096 }
18097 else /* c == '%' */
18098 {
18099 int percent_position = offset;
18100
18101 /* Get the specified minimum width. Zero means
18102 don't pad. */
18103 field = 0;
18104 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18105 field = field * 10 + c - '0';
18106
18107 /* Don't pad beyond the total padding allowed. */
18108 if (field_width - n > 0 && field > field_width - n)
18109 field = field_width - n;
18110
18111 /* Note that either PRECISION <= 0 or N < PRECISION. */
18112 prec = precision - n;
18113
18114 if (c == 'M')
18115 n += display_mode_element (it, depth, field, prec,
18116 Vglobal_mode_string, props,
18117 risky);
18118 else if (c != 0)
18119 {
18120 int multibyte;
18121 int bytepos, charpos;
18122 unsigned char *spec;
18123 Lisp_Object string;
18124
18125 bytepos = percent_position;
18126 charpos = (STRING_MULTIBYTE (elt)
18127 ? string_byte_to_char (elt, bytepos)
18128 : bytepos);
18129 spec = decode_mode_spec (it->w, c, field, prec, &string);
18130 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18131
18132 switch (mode_line_target)
18133 {
18134 case MODE_LINE_NOPROP:
18135 case MODE_LINE_TITLE:
18136 n += store_mode_line_noprop (spec, field, prec);
18137 break;
18138 case MODE_LINE_STRING:
18139 {
18140 int len = strlen (spec);
18141 Lisp_Object tem = make_string (spec, len);
18142 props = Ftext_properties_at (make_number (charpos), elt);
18143 /* Should only keep face property in props */
18144 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18145 }
18146 break;
18147 case MODE_LINE_DISPLAY:
18148 {
18149 int nglyphs_before, nwritten;
18150
18151 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18152 nwritten = display_string (spec, string, elt,
18153 charpos, 0, it,
18154 field, prec, 0,
18155 multibyte);
18156
18157 /* Assign to the glyphs written above the
18158 string where the `%x' came from, position
18159 of the `%'. */
18160 if (nwritten > 0)
18161 {
18162 struct glyph *glyph
18163 = (it->glyph_row->glyphs[TEXT_AREA]
18164 + nglyphs_before);
18165 int i;
18166
18167 for (i = 0; i < nwritten; ++i)
18168 {
18169 glyph[i].object = elt;
18170 glyph[i].charpos = charpos;
18171 }
18172
18173 n += nwritten;
18174 }
18175 }
18176 break;
18177 }
18178 }
18179 else /* c == 0 */
18180 break;
18181 }
18182 }
18183 }
18184 break;
18185
18186 case Lisp_Symbol:
18187 /* A symbol: process the value of the symbol recursively
18188 as if it appeared here directly. Avoid error if symbol void.
18189 Special case: if value of symbol is a string, output the string
18190 literally. */
18191 {
18192 register Lisp_Object tem;
18193
18194 /* If the variable is not marked as risky to set
18195 then its contents are risky to use. */
18196 if (NILP (Fget (elt, Qrisky_local_variable)))
18197 risky = 1;
18198
18199 tem = Fboundp (elt);
18200 if (!NILP (tem))
18201 {
18202 tem = Fsymbol_value (elt);
18203 /* If value is a string, output that string literally:
18204 don't check for % within it. */
18205 if (STRINGP (tem))
18206 literal = 1;
18207
18208 if (!EQ (tem, elt))
18209 {
18210 /* Give up right away for nil or t. */
18211 elt = tem;
18212 goto tail_recurse;
18213 }
18214 }
18215 }
18216 break;
18217
18218 case Lisp_Cons:
18219 {
18220 register Lisp_Object car, tem;
18221
18222 /* A cons cell: five distinct cases.
18223 If first element is :eval or :propertize, do something special.
18224 If first element is a string or a cons, process all the elements
18225 and effectively concatenate them.
18226 If first element is a negative number, truncate displaying cdr to
18227 at most that many characters. If positive, pad (with spaces)
18228 to at least that many characters.
18229 If first element is a symbol, process the cadr or caddr recursively
18230 according to whether the symbol's value is non-nil or nil. */
18231 car = XCAR (elt);
18232 if (EQ (car, QCeval))
18233 {
18234 /* An element of the form (:eval FORM) means evaluate FORM
18235 and use the result as mode line elements. */
18236
18237 if (risky)
18238 break;
18239
18240 if (CONSP (XCDR (elt)))
18241 {
18242 Lisp_Object spec;
18243 spec = safe_eval (XCAR (XCDR (elt)));
18244 n += display_mode_element (it, depth, field_width - n,
18245 precision - n, spec, props,
18246 risky);
18247 }
18248 }
18249 else if (EQ (car, QCpropertize))
18250 {
18251 /* An element of the form (:propertize ELT PROPS...)
18252 means display ELT but applying properties PROPS. */
18253
18254 if (risky)
18255 break;
18256
18257 if (CONSP (XCDR (elt)))
18258 n += display_mode_element (it, depth, field_width - n,
18259 precision - n, XCAR (XCDR (elt)),
18260 XCDR (XCDR (elt)), risky);
18261 }
18262 else if (SYMBOLP (car))
18263 {
18264 tem = Fboundp (car);
18265 elt = XCDR (elt);
18266 if (!CONSP (elt))
18267 goto invalid;
18268 /* elt is now the cdr, and we know it is a cons cell.
18269 Use its car if CAR has a non-nil value. */
18270 if (!NILP (tem))
18271 {
18272 tem = Fsymbol_value (car);
18273 if (!NILP (tem))
18274 {
18275 elt = XCAR (elt);
18276 goto tail_recurse;
18277 }
18278 }
18279 /* Symbol's value is nil (or symbol is unbound)
18280 Get the cddr of the original list
18281 and if possible find the caddr and use that. */
18282 elt = XCDR (elt);
18283 if (NILP (elt))
18284 break;
18285 else if (!CONSP (elt))
18286 goto invalid;
18287 elt = XCAR (elt);
18288 goto tail_recurse;
18289 }
18290 else if (INTEGERP (car))
18291 {
18292 register int lim = XINT (car);
18293 elt = XCDR (elt);
18294 if (lim < 0)
18295 {
18296 /* Negative int means reduce maximum width. */
18297 if (precision <= 0)
18298 precision = -lim;
18299 else
18300 precision = min (precision, -lim);
18301 }
18302 else if (lim > 0)
18303 {
18304 /* Padding specified. Don't let it be more than
18305 current maximum. */
18306 if (precision > 0)
18307 lim = min (precision, lim);
18308
18309 /* If that's more padding than already wanted, queue it.
18310 But don't reduce padding already specified even if
18311 that is beyond the current truncation point. */
18312 field_width = max (lim, field_width);
18313 }
18314 goto tail_recurse;
18315 }
18316 else if (STRINGP (car) || CONSP (car))
18317 {
18318 Lisp_Object halftail = elt;
18319 int len = 0;
18320
18321 while (CONSP (elt)
18322 && (precision <= 0 || n < precision))
18323 {
18324 n += display_mode_element (it, depth,
18325 /* Do padding only after the last
18326 element in the list. */
18327 (! CONSP (XCDR (elt))
18328 ? field_width - n
18329 : 0),
18330 precision - n, XCAR (elt),
18331 props, risky);
18332 elt = XCDR (elt);
18333 len++;
18334 if ((len & 1) == 0)
18335 halftail = XCDR (halftail);
18336 /* Check for cycle. */
18337 if (EQ (halftail, elt))
18338 break;
18339 }
18340 }
18341 }
18342 break;
18343
18344 default:
18345 invalid:
18346 elt = build_string ("*invalid*");
18347 goto tail_recurse;
18348 }
18349
18350 /* Pad to FIELD_WIDTH. */
18351 if (field_width > 0 && n < field_width)
18352 {
18353 switch (mode_line_target)
18354 {
18355 case MODE_LINE_NOPROP:
18356 case MODE_LINE_TITLE:
18357 n += store_mode_line_noprop ("", field_width - n, 0);
18358 break;
18359 case MODE_LINE_STRING:
18360 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18361 break;
18362 case MODE_LINE_DISPLAY:
18363 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18364 0, 0, 0);
18365 break;
18366 }
18367 }
18368
18369 return n;
18370 }
18371
18372 /* Store a mode-line string element in mode_line_string_list.
18373
18374 If STRING is non-null, display that C string. Otherwise, the Lisp
18375 string LISP_STRING is displayed.
18376
18377 FIELD_WIDTH is the minimum number of output glyphs to produce.
18378 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18379 with spaces. FIELD_WIDTH <= 0 means don't pad.
18380
18381 PRECISION is the maximum number of characters to output from
18382 STRING. PRECISION <= 0 means don't truncate the string.
18383
18384 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18385 properties to the string.
18386
18387 PROPS are the properties to add to the string.
18388 The mode_line_string_face face property is always added to the string.
18389 */
18390
18391 static int
18392 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
18393 char *string;
18394 Lisp_Object lisp_string;
18395 int copy_string;
18396 int field_width;
18397 int precision;
18398 Lisp_Object props;
18399 {
18400 int len;
18401 int n = 0;
18402
18403 if (string != NULL)
18404 {
18405 len = strlen (string);
18406 if (precision > 0 && len > precision)
18407 len = precision;
18408 lisp_string = make_string (string, len);
18409 if (NILP (props))
18410 props = mode_line_string_face_prop;
18411 else if (!NILP (mode_line_string_face))
18412 {
18413 Lisp_Object face = Fplist_get (props, Qface);
18414 props = Fcopy_sequence (props);
18415 if (NILP (face))
18416 face = mode_line_string_face;
18417 else
18418 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18419 props = Fplist_put (props, Qface, face);
18420 }
18421 Fadd_text_properties (make_number (0), make_number (len),
18422 props, lisp_string);
18423 }
18424 else
18425 {
18426 len = XFASTINT (Flength (lisp_string));
18427 if (precision > 0 && len > precision)
18428 {
18429 len = precision;
18430 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18431 precision = -1;
18432 }
18433 if (!NILP (mode_line_string_face))
18434 {
18435 Lisp_Object face;
18436 if (NILP (props))
18437 props = Ftext_properties_at (make_number (0), lisp_string);
18438 face = Fplist_get (props, Qface);
18439 if (NILP (face))
18440 face = mode_line_string_face;
18441 else
18442 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18443 props = Fcons (Qface, Fcons (face, Qnil));
18444 if (copy_string)
18445 lisp_string = Fcopy_sequence (lisp_string);
18446 }
18447 if (!NILP (props))
18448 Fadd_text_properties (make_number (0), make_number (len),
18449 props, lisp_string);
18450 }
18451
18452 if (len > 0)
18453 {
18454 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18455 n += len;
18456 }
18457
18458 if (field_width > len)
18459 {
18460 field_width -= len;
18461 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18462 if (!NILP (props))
18463 Fadd_text_properties (make_number (0), make_number (field_width),
18464 props, lisp_string);
18465 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18466 n += field_width;
18467 }
18468
18469 return n;
18470 }
18471
18472
18473 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18474 1, 4, 0,
18475 doc: /* Format a string out of a mode line format specification.
18476 First arg FORMAT specifies the mode line format (see `mode-line-format'
18477 for details) to use.
18478
18479 Optional second arg FACE specifies the face property to put
18480 on all characters for which no face is specified.
18481 The value t means whatever face the window's mode line currently uses
18482 \(either `mode-line' or `mode-line-inactive', depending).
18483 A value of nil means the default is no face property.
18484 If FACE is an integer, the value string has no text properties.
18485
18486 Optional third and fourth args WINDOW and BUFFER specify the window
18487 and buffer to use as the context for the formatting (defaults
18488 are the selected window and the window's buffer). */)
18489 (format, face, window, buffer)
18490 Lisp_Object format, face, window, buffer;
18491 {
18492 struct it it;
18493 int len;
18494 struct window *w;
18495 struct buffer *old_buffer = NULL;
18496 int face_id = -1;
18497 int no_props = INTEGERP (face);
18498 int count = SPECPDL_INDEX ();
18499 Lisp_Object str;
18500 int string_start = 0;
18501
18502 if (NILP (window))
18503 window = selected_window;
18504 CHECK_WINDOW (window);
18505 w = XWINDOW (window);
18506
18507 if (NILP (buffer))
18508 buffer = w->buffer;
18509 CHECK_BUFFER (buffer);
18510
18511 /* Make formatting the modeline a non-op when noninteractive, otherwise
18512 there will be problems later caused by a partially initialized frame. */
18513 if (NILP (format) || noninteractive)
18514 return empty_unibyte_string;
18515
18516 if (no_props)
18517 face = Qnil;
18518
18519 if (!NILP (face))
18520 {
18521 if (EQ (face, Qt))
18522 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18523 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18524 }
18525
18526 if (face_id < 0)
18527 face_id = DEFAULT_FACE_ID;
18528
18529 if (XBUFFER (buffer) != current_buffer)
18530 old_buffer = current_buffer;
18531
18532 /* Save things including mode_line_proptrans_alist,
18533 and set that to nil so that we don't alter the outer value. */
18534 record_unwind_protect (unwind_format_mode_line,
18535 format_mode_line_unwind_data
18536 (old_buffer, selected_window, 1));
18537 mode_line_proptrans_alist = Qnil;
18538
18539 Fselect_window (window, Qt);
18540 if (old_buffer)
18541 set_buffer_internal_1 (XBUFFER (buffer));
18542
18543 init_iterator (&it, w, -1, -1, NULL, face_id);
18544
18545 if (no_props)
18546 {
18547 mode_line_target = MODE_LINE_NOPROP;
18548 mode_line_string_face_prop = Qnil;
18549 mode_line_string_list = Qnil;
18550 string_start = MODE_LINE_NOPROP_LEN (0);
18551 }
18552 else
18553 {
18554 mode_line_target = MODE_LINE_STRING;
18555 mode_line_string_list = Qnil;
18556 mode_line_string_face = face;
18557 mode_line_string_face_prop
18558 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18559 }
18560
18561 push_kboard (FRAME_KBOARD (it.f));
18562 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18563 pop_kboard ();
18564
18565 if (no_props)
18566 {
18567 len = MODE_LINE_NOPROP_LEN (string_start);
18568 str = make_string (mode_line_noprop_buf + string_start, len);
18569 }
18570 else
18571 {
18572 mode_line_string_list = Fnreverse (mode_line_string_list);
18573 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18574 empty_unibyte_string);
18575 }
18576
18577 unbind_to (count, Qnil);
18578 return str;
18579 }
18580
18581 /* Write a null-terminated, right justified decimal representation of
18582 the positive integer D to BUF using a minimal field width WIDTH. */
18583
18584 static void
18585 pint2str (buf, width, d)
18586 register char *buf;
18587 register int width;
18588 register int d;
18589 {
18590 register char *p = buf;
18591
18592 if (d <= 0)
18593 *p++ = '0';
18594 else
18595 {
18596 while (d > 0)
18597 {
18598 *p++ = d % 10 + '0';
18599 d /= 10;
18600 }
18601 }
18602
18603 for (width -= (int) (p - buf); width > 0; --width)
18604 *p++ = ' ';
18605 *p-- = '\0';
18606 while (p > buf)
18607 {
18608 d = *buf;
18609 *buf++ = *p;
18610 *p-- = d;
18611 }
18612 }
18613
18614 /* Write a null-terminated, right justified decimal and "human
18615 readable" representation of the nonnegative integer D to BUF using
18616 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18617
18618 static const char power_letter[] =
18619 {
18620 0, /* not used */
18621 'k', /* kilo */
18622 'M', /* mega */
18623 'G', /* giga */
18624 'T', /* tera */
18625 'P', /* peta */
18626 'E', /* exa */
18627 'Z', /* zetta */
18628 'Y' /* yotta */
18629 };
18630
18631 static void
18632 pint2hrstr (buf, width, d)
18633 char *buf;
18634 int width;
18635 int d;
18636 {
18637 /* We aim to represent the nonnegative integer D as
18638 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18639 int quotient = d;
18640 int remainder = 0;
18641 /* -1 means: do not use TENTHS. */
18642 int tenths = -1;
18643 int exponent = 0;
18644
18645 /* Length of QUOTIENT.TENTHS as a string. */
18646 int length;
18647
18648 char * psuffix;
18649 char * p;
18650
18651 if (1000 <= quotient)
18652 {
18653 /* Scale to the appropriate EXPONENT. */
18654 do
18655 {
18656 remainder = quotient % 1000;
18657 quotient /= 1000;
18658 exponent++;
18659 }
18660 while (1000 <= quotient);
18661
18662 /* Round to nearest and decide whether to use TENTHS or not. */
18663 if (quotient <= 9)
18664 {
18665 tenths = remainder / 100;
18666 if (50 <= remainder % 100)
18667 {
18668 if (tenths < 9)
18669 tenths++;
18670 else
18671 {
18672 quotient++;
18673 if (quotient == 10)
18674 tenths = -1;
18675 else
18676 tenths = 0;
18677 }
18678 }
18679 }
18680 else
18681 if (500 <= remainder)
18682 {
18683 if (quotient < 999)
18684 quotient++;
18685 else
18686 {
18687 quotient = 1;
18688 exponent++;
18689 tenths = 0;
18690 }
18691 }
18692 }
18693
18694 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18695 if (tenths == -1 && quotient <= 99)
18696 if (quotient <= 9)
18697 length = 1;
18698 else
18699 length = 2;
18700 else
18701 length = 3;
18702 p = psuffix = buf + max (width, length);
18703
18704 /* Print EXPONENT. */
18705 if (exponent)
18706 *psuffix++ = power_letter[exponent];
18707 *psuffix = '\0';
18708
18709 /* Print TENTHS. */
18710 if (tenths >= 0)
18711 {
18712 *--p = '0' + tenths;
18713 *--p = '.';
18714 }
18715
18716 /* Print QUOTIENT. */
18717 do
18718 {
18719 int digit = quotient % 10;
18720 *--p = '0' + digit;
18721 }
18722 while ((quotient /= 10) != 0);
18723
18724 /* Print leading spaces. */
18725 while (buf < p)
18726 *--p = ' ';
18727 }
18728
18729 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18730 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18731 type of CODING_SYSTEM. Return updated pointer into BUF. */
18732
18733 static unsigned char invalid_eol_type[] = "(*invalid*)";
18734
18735 static char *
18736 decode_mode_spec_coding (coding_system, buf, eol_flag)
18737 Lisp_Object coding_system;
18738 register char *buf;
18739 int eol_flag;
18740 {
18741 Lisp_Object val;
18742 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18743 const unsigned char *eol_str;
18744 int eol_str_len;
18745 /* The EOL conversion we are using. */
18746 Lisp_Object eoltype;
18747
18748 val = CODING_SYSTEM_SPEC (coding_system);
18749 eoltype = Qnil;
18750
18751 if (!VECTORP (val)) /* Not yet decided. */
18752 {
18753 if (multibyte)
18754 *buf++ = '-';
18755 if (eol_flag)
18756 eoltype = eol_mnemonic_undecided;
18757 /* Don't mention EOL conversion if it isn't decided. */
18758 }
18759 else
18760 {
18761 Lisp_Object attrs;
18762 Lisp_Object eolvalue;
18763
18764 attrs = AREF (val, 0);
18765 eolvalue = AREF (val, 2);
18766
18767 if (multibyte)
18768 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18769
18770 if (eol_flag)
18771 {
18772 /* The EOL conversion that is normal on this system. */
18773
18774 if (NILP (eolvalue)) /* Not yet decided. */
18775 eoltype = eol_mnemonic_undecided;
18776 else if (VECTORP (eolvalue)) /* Not yet decided. */
18777 eoltype = eol_mnemonic_undecided;
18778 else /* eolvalue is Qunix, Qdos, or Qmac. */
18779 eoltype = (EQ (eolvalue, Qunix)
18780 ? eol_mnemonic_unix
18781 : (EQ (eolvalue, Qdos) == 1
18782 ? eol_mnemonic_dos : eol_mnemonic_mac));
18783 }
18784 }
18785
18786 if (eol_flag)
18787 {
18788 /* Mention the EOL conversion if it is not the usual one. */
18789 if (STRINGP (eoltype))
18790 {
18791 eol_str = SDATA (eoltype);
18792 eol_str_len = SBYTES (eoltype);
18793 }
18794 else if (CHARACTERP (eoltype))
18795 {
18796 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18797 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18798 eol_str = tmp;
18799 }
18800 else
18801 {
18802 eol_str = invalid_eol_type;
18803 eol_str_len = sizeof (invalid_eol_type) - 1;
18804 }
18805 bcopy (eol_str, buf, eol_str_len);
18806 buf += eol_str_len;
18807 }
18808
18809 return buf;
18810 }
18811
18812 /* Return a string for the output of a mode line %-spec for window W,
18813 generated by character C. PRECISION >= 0 means don't return a
18814 string longer than that value. FIELD_WIDTH > 0 means pad the
18815 string returned with spaces to that value. Return a Lisp string in
18816 *STRING if the resulting string is taken from that Lisp string.
18817
18818 Note we operate on the current buffer for most purposes,
18819 the exception being w->base_line_pos. */
18820
18821 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18822
18823 static char *
18824 decode_mode_spec (w, c, field_width, precision, string)
18825 struct window *w;
18826 register int c;
18827 int field_width, precision;
18828 Lisp_Object *string;
18829 {
18830 Lisp_Object obj;
18831 struct frame *f = XFRAME (WINDOW_FRAME (w));
18832 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18833 struct buffer *b = current_buffer;
18834
18835 obj = Qnil;
18836 *string = Qnil;
18837
18838 switch (c)
18839 {
18840 case '*':
18841 if (!NILP (b->read_only))
18842 return "%";
18843 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18844 return "*";
18845 return "-";
18846
18847 case '+':
18848 /* This differs from %* only for a modified read-only buffer. */
18849 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18850 return "*";
18851 if (!NILP (b->read_only))
18852 return "%";
18853 return "-";
18854
18855 case '&':
18856 /* This differs from %* in ignoring read-only-ness. */
18857 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18858 return "*";
18859 return "-";
18860
18861 case '%':
18862 return "%";
18863
18864 case '[':
18865 {
18866 int i;
18867 char *p;
18868
18869 if (command_loop_level > 5)
18870 return "[[[... ";
18871 p = decode_mode_spec_buf;
18872 for (i = 0; i < command_loop_level; i++)
18873 *p++ = '[';
18874 *p = 0;
18875 return decode_mode_spec_buf;
18876 }
18877
18878 case ']':
18879 {
18880 int i;
18881 char *p;
18882
18883 if (command_loop_level > 5)
18884 return " ...]]]";
18885 p = decode_mode_spec_buf;
18886 for (i = 0; i < command_loop_level; i++)
18887 *p++ = ']';
18888 *p = 0;
18889 return decode_mode_spec_buf;
18890 }
18891
18892 case '-':
18893 {
18894 register int i;
18895
18896 /* Let lots_of_dashes be a string of infinite length. */
18897 if (mode_line_target == MODE_LINE_NOPROP ||
18898 mode_line_target == MODE_LINE_STRING)
18899 return "--";
18900 if (field_width <= 0
18901 || field_width > sizeof (lots_of_dashes))
18902 {
18903 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18904 decode_mode_spec_buf[i] = '-';
18905 decode_mode_spec_buf[i] = '\0';
18906 return decode_mode_spec_buf;
18907 }
18908 else
18909 return lots_of_dashes;
18910 }
18911
18912 case 'b':
18913 obj = b->name;
18914 break;
18915
18916 case 'c':
18917 /* %c and %l are ignored in `frame-title-format'.
18918 (In redisplay_internal, the frame title is drawn _before_ the
18919 windows are updated, so the stuff which depends on actual
18920 window contents (such as %l) may fail to render properly, or
18921 even crash emacs.) */
18922 if (mode_line_target == MODE_LINE_TITLE)
18923 return "";
18924 else
18925 {
18926 int col = (int) current_column (); /* iftc */
18927 w->column_number_displayed = make_number (col);
18928 pint2str (decode_mode_spec_buf, field_width, col);
18929 return decode_mode_spec_buf;
18930 }
18931
18932 case 'e':
18933 #ifndef SYSTEM_MALLOC
18934 {
18935 if (NILP (Vmemory_full))
18936 return "";
18937 else
18938 return "!MEM FULL! ";
18939 }
18940 #else
18941 return "";
18942 #endif
18943
18944 case 'F':
18945 /* %F displays the frame name. */
18946 if (!NILP (f->title))
18947 return (char *) SDATA (f->title);
18948 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18949 return (char *) SDATA (f->name);
18950 return "Emacs";
18951
18952 case 'f':
18953 obj = b->filename;
18954 break;
18955
18956 case 'i':
18957 {
18958 int size = ZV - BEGV;
18959 pint2str (decode_mode_spec_buf, field_width, size);
18960 return decode_mode_spec_buf;
18961 }
18962
18963 case 'I':
18964 {
18965 int size = ZV - BEGV;
18966 pint2hrstr (decode_mode_spec_buf, field_width, size);
18967 return decode_mode_spec_buf;
18968 }
18969
18970 case 'l':
18971 {
18972 int startpos, startpos_byte, line, linepos, linepos_byte;
18973 int topline, nlines, junk, height;
18974
18975 /* %c and %l are ignored in `frame-title-format'. */
18976 if (mode_line_target == MODE_LINE_TITLE)
18977 return "";
18978
18979 startpos = XMARKER (w->start)->charpos;
18980 startpos_byte = marker_byte_position (w->start);
18981 height = WINDOW_TOTAL_LINES (w);
18982
18983 /* If we decided that this buffer isn't suitable for line numbers,
18984 don't forget that too fast. */
18985 if (EQ (w->base_line_pos, w->buffer))
18986 goto no_value;
18987 /* But do forget it, if the window shows a different buffer now. */
18988 else if (BUFFERP (w->base_line_pos))
18989 w->base_line_pos = Qnil;
18990
18991 /* If the buffer is very big, don't waste time. */
18992 if (INTEGERP (Vline_number_display_limit)
18993 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18994 {
18995 w->base_line_pos = Qnil;
18996 w->base_line_number = Qnil;
18997 goto no_value;
18998 }
18999
19000 if (INTEGERP (w->base_line_number)
19001 && INTEGERP (w->base_line_pos)
19002 && XFASTINT (w->base_line_pos) <= startpos)
19003 {
19004 line = XFASTINT (w->base_line_number);
19005 linepos = XFASTINT (w->base_line_pos);
19006 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19007 }
19008 else
19009 {
19010 line = 1;
19011 linepos = BUF_BEGV (b);
19012 linepos_byte = BUF_BEGV_BYTE (b);
19013 }
19014
19015 /* Count lines from base line to window start position. */
19016 nlines = display_count_lines (linepos, linepos_byte,
19017 startpos_byte,
19018 startpos, &junk);
19019
19020 topline = nlines + line;
19021
19022 /* Determine a new base line, if the old one is too close
19023 or too far away, or if we did not have one.
19024 "Too close" means it's plausible a scroll-down would
19025 go back past it. */
19026 if (startpos == BUF_BEGV (b))
19027 {
19028 w->base_line_number = make_number (topline);
19029 w->base_line_pos = make_number (BUF_BEGV (b));
19030 }
19031 else if (nlines < height + 25 || nlines > height * 3 + 50
19032 || linepos == BUF_BEGV (b))
19033 {
19034 int limit = BUF_BEGV (b);
19035 int limit_byte = BUF_BEGV_BYTE (b);
19036 int position;
19037 int distance = (height * 2 + 30) * line_number_display_limit_width;
19038
19039 if (startpos - distance > limit)
19040 {
19041 limit = startpos - distance;
19042 limit_byte = CHAR_TO_BYTE (limit);
19043 }
19044
19045 nlines = display_count_lines (startpos, startpos_byte,
19046 limit_byte,
19047 - (height * 2 + 30),
19048 &position);
19049 /* If we couldn't find the lines we wanted within
19050 line_number_display_limit_width chars per line,
19051 give up on line numbers for this window. */
19052 if (position == limit_byte && limit == startpos - distance)
19053 {
19054 w->base_line_pos = w->buffer;
19055 w->base_line_number = Qnil;
19056 goto no_value;
19057 }
19058
19059 w->base_line_number = make_number (topline - nlines);
19060 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19061 }
19062
19063 /* Now count lines from the start pos to point. */
19064 nlines = display_count_lines (startpos, startpos_byte,
19065 PT_BYTE, PT, &junk);
19066
19067 /* Record that we did display the line number. */
19068 line_number_displayed = 1;
19069
19070 /* Make the string to show. */
19071 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19072 return decode_mode_spec_buf;
19073 no_value:
19074 {
19075 char* p = decode_mode_spec_buf;
19076 int pad = field_width - 2;
19077 while (pad-- > 0)
19078 *p++ = ' ';
19079 *p++ = '?';
19080 *p++ = '?';
19081 *p = '\0';
19082 return decode_mode_spec_buf;
19083 }
19084 }
19085 break;
19086
19087 case 'm':
19088 obj = b->mode_name;
19089 break;
19090
19091 case 'n':
19092 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19093 return " Narrow";
19094 break;
19095
19096 case 'p':
19097 {
19098 int pos = marker_position (w->start);
19099 int total = BUF_ZV (b) - BUF_BEGV (b);
19100
19101 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19102 {
19103 if (pos <= BUF_BEGV (b))
19104 return "All";
19105 else
19106 return "Bottom";
19107 }
19108 else if (pos <= BUF_BEGV (b))
19109 return "Top";
19110 else
19111 {
19112 if (total > 1000000)
19113 /* Do it differently for a large value, to avoid overflow. */
19114 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19115 else
19116 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19117 /* We can't normally display a 3-digit number,
19118 so get us a 2-digit number that is close. */
19119 if (total == 100)
19120 total = 99;
19121 sprintf (decode_mode_spec_buf, "%2d%%", total);
19122 return decode_mode_spec_buf;
19123 }
19124 }
19125
19126 /* Display percentage of size above the bottom of the screen. */
19127 case 'P':
19128 {
19129 int toppos = marker_position (w->start);
19130 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19131 int total = BUF_ZV (b) - BUF_BEGV (b);
19132
19133 if (botpos >= BUF_ZV (b))
19134 {
19135 if (toppos <= BUF_BEGV (b))
19136 return "All";
19137 else
19138 return "Bottom";
19139 }
19140 else
19141 {
19142 if (total > 1000000)
19143 /* Do it differently for a large value, to avoid overflow. */
19144 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19145 else
19146 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19147 /* We can't normally display a 3-digit number,
19148 so get us a 2-digit number that is close. */
19149 if (total == 100)
19150 total = 99;
19151 if (toppos <= BUF_BEGV (b))
19152 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
19153 else
19154 sprintf (decode_mode_spec_buf, "%2d%%", total);
19155 return decode_mode_spec_buf;
19156 }
19157 }
19158
19159 case 's':
19160 /* status of process */
19161 obj = Fget_buffer_process (Fcurrent_buffer ());
19162 if (NILP (obj))
19163 return "no process";
19164 #ifdef subprocesses
19165 obj = Fsymbol_name (Fprocess_status (obj));
19166 #endif
19167 break;
19168
19169 case '@':
19170 {
19171 int count = inhibit_garbage_collection ();
19172 Lisp_Object val = call1 (intern ("file-remote-p"),
19173 current_buffer->directory);
19174 unbind_to (count, Qnil);
19175
19176 if (NILP (val))
19177 return "-";
19178 else
19179 return "@";
19180 }
19181
19182 case 't': /* indicate TEXT or BINARY */
19183 #ifdef MODE_LINE_BINARY_TEXT
19184 return MODE_LINE_BINARY_TEXT (b);
19185 #else
19186 return "T";
19187 #endif
19188
19189 case 'z':
19190 /* coding-system (not including end-of-line format) */
19191 case 'Z':
19192 /* coding-system (including end-of-line type) */
19193 {
19194 int eol_flag = (c == 'Z');
19195 char *p = decode_mode_spec_buf;
19196
19197 if (! FRAME_WINDOW_P (f))
19198 {
19199 /* No need to mention EOL here--the terminal never needs
19200 to do EOL conversion. */
19201 p = decode_mode_spec_coding (CODING_ID_NAME
19202 (FRAME_KEYBOARD_CODING (f)->id),
19203 p, 0);
19204 p = decode_mode_spec_coding (CODING_ID_NAME
19205 (FRAME_TERMINAL_CODING (f)->id),
19206 p, 0);
19207 }
19208 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19209 p, eol_flag);
19210
19211 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19212 #ifdef subprocesses
19213 obj = Fget_buffer_process (Fcurrent_buffer ());
19214 if (PROCESSP (obj))
19215 {
19216 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19217 p, eol_flag);
19218 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19219 p, eol_flag);
19220 }
19221 #endif /* subprocesses */
19222 #endif /* 0 */
19223 *p = 0;
19224 return decode_mode_spec_buf;
19225 }
19226 }
19227
19228 if (STRINGP (obj))
19229 {
19230 *string = obj;
19231 return (char *) SDATA (obj);
19232 }
19233 else
19234 return "";
19235 }
19236
19237
19238 /* Count up to COUNT lines starting from START / START_BYTE.
19239 But don't go beyond LIMIT_BYTE.
19240 Return the number of lines thus found (always nonnegative).
19241
19242 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19243
19244 static int
19245 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
19246 int start, start_byte, limit_byte, count;
19247 int *byte_pos_ptr;
19248 {
19249 register unsigned char *cursor;
19250 unsigned char *base;
19251
19252 register int ceiling;
19253 register unsigned char *ceiling_addr;
19254 int orig_count = count;
19255
19256 /* If we are not in selective display mode,
19257 check only for newlines. */
19258 int selective_display = (!NILP (current_buffer->selective_display)
19259 && !INTEGERP (current_buffer->selective_display));
19260
19261 if (count > 0)
19262 {
19263 while (start_byte < limit_byte)
19264 {
19265 ceiling = BUFFER_CEILING_OF (start_byte);
19266 ceiling = min (limit_byte - 1, ceiling);
19267 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19268 base = (cursor = BYTE_POS_ADDR (start_byte));
19269 while (1)
19270 {
19271 if (selective_display)
19272 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19273 ;
19274 else
19275 while (*cursor != '\n' && ++cursor != ceiling_addr)
19276 ;
19277
19278 if (cursor != ceiling_addr)
19279 {
19280 if (--count == 0)
19281 {
19282 start_byte += cursor - base + 1;
19283 *byte_pos_ptr = start_byte;
19284 return orig_count;
19285 }
19286 else
19287 if (++cursor == ceiling_addr)
19288 break;
19289 }
19290 else
19291 break;
19292 }
19293 start_byte += cursor - base;
19294 }
19295 }
19296 else
19297 {
19298 while (start_byte > limit_byte)
19299 {
19300 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19301 ceiling = max (limit_byte, ceiling);
19302 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19303 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19304 while (1)
19305 {
19306 if (selective_display)
19307 while (--cursor != ceiling_addr
19308 && *cursor != '\n' && *cursor != 015)
19309 ;
19310 else
19311 while (--cursor != ceiling_addr && *cursor != '\n')
19312 ;
19313
19314 if (cursor != ceiling_addr)
19315 {
19316 if (++count == 0)
19317 {
19318 start_byte += cursor - base + 1;
19319 *byte_pos_ptr = start_byte;
19320 /* When scanning backwards, we should
19321 not count the newline posterior to which we stop. */
19322 return - orig_count - 1;
19323 }
19324 }
19325 else
19326 break;
19327 }
19328 /* Here we add 1 to compensate for the last decrement
19329 of CURSOR, which took it past the valid range. */
19330 start_byte += cursor - base + 1;
19331 }
19332 }
19333
19334 *byte_pos_ptr = limit_byte;
19335
19336 if (count < 0)
19337 return - orig_count + count;
19338 return orig_count - count;
19339
19340 }
19341
19342
19343 \f
19344 /***********************************************************************
19345 Displaying strings
19346 ***********************************************************************/
19347
19348 /* Display a NUL-terminated string, starting with index START.
19349
19350 If STRING is non-null, display that C string. Otherwise, the Lisp
19351 string LISP_STRING is displayed. There's a case that STRING is
19352 non-null and LISP_STRING is not nil. It means STRING is a string
19353 data of LISP_STRING. In that case, we display LISP_STRING while
19354 ignoring its text properties.
19355
19356 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19357 FACE_STRING. Display STRING or LISP_STRING with the face at
19358 FACE_STRING_POS in FACE_STRING:
19359
19360 Display the string in the environment given by IT, but use the
19361 standard display table, temporarily.
19362
19363 FIELD_WIDTH is the minimum number of output glyphs to produce.
19364 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19365 with spaces. If STRING has more characters, more than FIELD_WIDTH
19366 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19367
19368 PRECISION is the maximum number of characters to output from
19369 STRING. PRECISION < 0 means don't truncate the string.
19370
19371 This is roughly equivalent to printf format specifiers:
19372
19373 FIELD_WIDTH PRECISION PRINTF
19374 ----------------------------------------
19375 -1 -1 %s
19376 -1 10 %.10s
19377 10 -1 %10s
19378 20 10 %20.10s
19379
19380 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19381 display them, and < 0 means obey the current buffer's value of
19382 enable_multibyte_characters.
19383
19384 Value is the number of columns displayed. */
19385
19386 static int
19387 display_string (string, lisp_string, face_string, face_string_pos,
19388 start, it, field_width, precision, max_x, multibyte)
19389 unsigned char *string;
19390 Lisp_Object lisp_string;
19391 Lisp_Object face_string;
19392 EMACS_INT face_string_pos;
19393 EMACS_INT start;
19394 struct it *it;
19395 int field_width, precision, max_x;
19396 int multibyte;
19397 {
19398 int hpos_at_start = it->hpos;
19399 int saved_face_id = it->face_id;
19400 struct glyph_row *row = it->glyph_row;
19401
19402 /* Initialize the iterator IT for iteration over STRING beginning
19403 with index START. */
19404 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19405 precision, field_width, multibyte);
19406 if (string && STRINGP (lisp_string))
19407 /* LISP_STRING is the one returned by decode_mode_spec. We should
19408 ignore its text properties. */
19409 it->stop_charpos = -1;
19410
19411 /* If displaying STRING, set up the face of the iterator
19412 from LISP_STRING, if that's given. */
19413 if (STRINGP (face_string))
19414 {
19415 EMACS_INT endptr;
19416 struct face *face;
19417
19418 it->face_id
19419 = face_at_string_position (it->w, face_string, face_string_pos,
19420 0, it->region_beg_charpos,
19421 it->region_end_charpos,
19422 &endptr, it->base_face_id, 0);
19423 face = FACE_FROM_ID (it->f, it->face_id);
19424 it->face_box_p = face->box != FACE_NO_BOX;
19425 }
19426
19427 /* Set max_x to the maximum allowed X position. Don't let it go
19428 beyond the right edge of the window. */
19429 if (max_x <= 0)
19430 max_x = it->last_visible_x;
19431 else
19432 max_x = min (max_x, it->last_visible_x);
19433
19434 /* Skip over display elements that are not visible. because IT->w is
19435 hscrolled. */
19436 if (it->current_x < it->first_visible_x)
19437 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19438 MOVE_TO_POS | MOVE_TO_X);
19439
19440 row->ascent = it->max_ascent;
19441 row->height = it->max_ascent + it->max_descent;
19442 row->phys_ascent = it->max_phys_ascent;
19443 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19444 row->extra_line_spacing = it->max_extra_line_spacing;
19445
19446 /* This condition is for the case that we are called with current_x
19447 past last_visible_x. */
19448 while (it->current_x < max_x)
19449 {
19450 int x_before, x, n_glyphs_before, i, nglyphs;
19451
19452 /* Get the next display element. */
19453 if (!get_next_display_element (it))
19454 break;
19455
19456 /* Produce glyphs. */
19457 x_before = it->current_x;
19458 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19459 PRODUCE_GLYPHS (it);
19460
19461 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19462 i = 0;
19463 x = x_before;
19464 while (i < nglyphs)
19465 {
19466 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19467
19468 if (it->line_wrap != TRUNCATE
19469 && x + glyph->pixel_width > max_x)
19470 {
19471 /* End of continued line or max_x reached. */
19472 if (CHAR_GLYPH_PADDING_P (*glyph))
19473 {
19474 /* A wide character is unbreakable. */
19475 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19476 it->current_x = x_before;
19477 }
19478 else
19479 {
19480 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19481 it->current_x = x;
19482 }
19483 break;
19484 }
19485 else if (x + glyph->pixel_width >= it->first_visible_x)
19486 {
19487 /* Glyph is at least partially visible. */
19488 ++it->hpos;
19489 if (x < it->first_visible_x)
19490 it->glyph_row->x = x - it->first_visible_x;
19491 }
19492 else
19493 {
19494 /* Glyph is off the left margin of the display area.
19495 Should not happen. */
19496 abort ();
19497 }
19498
19499 row->ascent = max (row->ascent, it->max_ascent);
19500 row->height = max (row->height, it->max_ascent + it->max_descent);
19501 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19502 row->phys_height = max (row->phys_height,
19503 it->max_phys_ascent + it->max_phys_descent);
19504 row->extra_line_spacing = max (row->extra_line_spacing,
19505 it->max_extra_line_spacing);
19506 x += glyph->pixel_width;
19507 ++i;
19508 }
19509
19510 /* Stop if max_x reached. */
19511 if (i < nglyphs)
19512 break;
19513
19514 /* Stop at line ends. */
19515 if (ITERATOR_AT_END_OF_LINE_P (it))
19516 {
19517 it->continuation_lines_width = 0;
19518 break;
19519 }
19520
19521 set_iterator_to_next (it, 1);
19522
19523 /* Stop if truncating at the right edge. */
19524 if (it->line_wrap == TRUNCATE
19525 && it->current_x >= it->last_visible_x)
19526 {
19527 /* Add truncation mark, but don't do it if the line is
19528 truncated at a padding space. */
19529 if (IT_CHARPOS (*it) < it->string_nchars)
19530 {
19531 if (!FRAME_WINDOW_P (it->f))
19532 {
19533 int i, n;
19534
19535 if (it->current_x > it->last_visible_x)
19536 {
19537 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19538 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19539 break;
19540 for (n = row->used[TEXT_AREA]; i < n; ++i)
19541 {
19542 row->used[TEXT_AREA] = i;
19543 produce_special_glyphs (it, IT_TRUNCATION);
19544 }
19545 }
19546 produce_special_glyphs (it, IT_TRUNCATION);
19547 }
19548 it->glyph_row->truncated_on_right_p = 1;
19549 }
19550 break;
19551 }
19552 }
19553
19554 /* Maybe insert a truncation at the left. */
19555 if (it->first_visible_x
19556 && IT_CHARPOS (*it) > 0)
19557 {
19558 if (!FRAME_WINDOW_P (it->f))
19559 insert_left_trunc_glyphs (it);
19560 it->glyph_row->truncated_on_left_p = 1;
19561 }
19562
19563 it->face_id = saved_face_id;
19564
19565 /* Value is number of columns displayed. */
19566 return it->hpos - hpos_at_start;
19567 }
19568
19569
19570 \f
19571 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19572 appears as an element of LIST or as the car of an element of LIST.
19573 If PROPVAL is a list, compare each element against LIST in that
19574 way, and return 1/2 if any element of PROPVAL is found in LIST.
19575 Otherwise return 0. This function cannot quit.
19576 The return value is 2 if the text is invisible but with an ellipsis
19577 and 1 if it's invisible and without an ellipsis. */
19578
19579 int
19580 invisible_p (propval, list)
19581 register Lisp_Object propval;
19582 Lisp_Object list;
19583 {
19584 register Lisp_Object tail, proptail;
19585
19586 for (tail = list; CONSP (tail); tail = XCDR (tail))
19587 {
19588 register Lisp_Object tem;
19589 tem = XCAR (tail);
19590 if (EQ (propval, tem))
19591 return 1;
19592 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19593 return NILP (XCDR (tem)) ? 1 : 2;
19594 }
19595
19596 if (CONSP (propval))
19597 {
19598 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19599 {
19600 Lisp_Object propelt;
19601 propelt = XCAR (proptail);
19602 for (tail = list; CONSP (tail); tail = XCDR (tail))
19603 {
19604 register Lisp_Object tem;
19605 tem = XCAR (tail);
19606 if (EQ (propelt, tem))
19607 return 1;
19608 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19609 return NILP (XCDR (tem)) ? 1 : 2;
19610 }
19611 }
19612 }
19613
19614 return 0;
19615 }
19616
19617 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19618 doc: /* Non-nil if the property makes the text invisible.
19619 POS-OR-PROP can be a marker or number, in which case it is taken to be
19620 a position in the current buffer and the value of the `invisible' property
19621 is checked; or it can be some other value, which is then presumed to be the
19622 value of the `invisible' property of the text of interest.
19623 The non-nil value returned can be t for truly invisible text or something
19624 else if the text is replaced by an ellipsis. */)
19625 (pos_or_prop)
19626 Lisp_Object pos_or_prop;
19627 {
19628 Lisp_Object prop
19629 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19630 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19631 : pos_or_prop);
19632 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19633 return (invis == 0 ? Qnil
19634 : invis == 1 ? Qt
19635 : make_number (invis));
19636 }
19637
19638 /* Calculate a width or height in pixels from a specification using
19639 the following elements:
19640
19641 SPEC ::=
19642 NUM - a (fractional) multiple of the default font width/height
19643 (NUM) - specifies exactly NUM pixels
19644 UNIT - a fixed number of pixels, see below.
19645 ELEMENT - size of a display element in pixels, see below.
19646 (NUM . SPEC) - equals NUM * SPEC
19647 (+ SPEC SPEC ...) - add pixel values
19648 (- SPEC SPEC ...) - subtract pixel values
19649 (- SPEC) - negate pixel value
19650
19651 NUM ::=
19652 INT or FLOAT - a number constant
19653 SYMBOL - use symbol's (buffer local) variable binding.
19654
19655 UNIT ::=
19656 in - pixels per inch *)
19657 mm - pixels per 1/1000 meter *)
19658 cm - pixels per 1/100 meter *)
19659 width - width of current font in pixels.
19660 height - height of current font in pixels.
19661
19662 *) using the ratio(s) defined in display-pixels-per-inch.
19663
19664 ELEMENT ::=
19665
19666 left-fringe - left fringe width in pixels
19667 right-fringe - right fringe width in pixels
19668
19669 left-margin - left margin width in pixels
19670 right-margin - right margin width in pixels
19671
19672 scroll-bar - scroll-bar area width in pixels
19673
19674 Examples:
19675
19676 Pixels corresponding to 5 inches:
19677 (5 . in)
19678
19679 Total width of non-text areas on left side of window (if scroll-bar is on left):
19680 '(space :width (+ left-fringe left-margin scroll-bar))
19681
19682 Align to first text column (in header line):
19683 '(space :align-to 0)
19684
19685 Align to middle of text area minus half the width of variable `my-image'
19686 containing a loaded image:
19687 '(space :align-to (0.5 . (- text my-image)))
19688
19689 Width of left margin minus width of 1 character in the default font:
19690 '(space :width (- left-margin 1))
19691
19692 Width of left margin minus width of 2 characters in the current font:
19693 '(space :width (- left-margin (2 . width)))
19694
19695 Center 1 character over left-margin (in header line):
19696 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19697
19698 Different ways to express width of left fringe plus left margin minus one pixel:
19699 '(space :width (- (+ left-fringe left-margin) (1)))
19700 '(space :width (+ left-fringe left-margin (- (1))))
19701 '(space :width (+ left-fringe left-margin (-1)))
19702
19703 */
19704
19705 #define NUMVAL(X) \
19706 ((INTEGERP (X) || FLOATP (X)) \
19707 ? XFLOATINT (X) \
19708 : - 1)
19709
19710 int
19711 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19712 double *res;
19713 struct it *it;
19714 Lisp_Object prop;
19715 struct font *font;
19716 int width_p, *align_to;
19717 {
19718 double pixels;
19719
19720 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19721 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19722
19723 if (NILP (prop))
19724 return OK_PIXELS (0);
19725
19726 xassert (FRAME_LIVE_P (it->f));
19727
19728 if (SYMBOLP (prop))
19729 {
19730 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19731 {
19732 char *unit = SDATA (SYMBOL_NAME (prop));
19733
19734 if (unit[0] == 'i' && unit[1] == 'n')
19735 pixels = 1.0;
19736 else if (unit[0] == 'm' && unit[1] == 'm')
19737 pixels = 25.4;
19738 else if (unit[0] == 'c' && unit[1] == 'm')
19739 pixels = 2.54;
19740 else
19741 pixels = 0;
19742 if (pixels > 0)
19743 {
19744 double ppi;
19745 #ifdef HAVE_WINDOW_SYSTEM
19746 if (FRAME_WINDOW_P (it->f)
19747 && (ppi = (width_p
19748 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19749 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19750 ppi > 0))
19751 return OK_PIXELS (ppi / pixels);
19752 #endif
19753
19754 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19755 || (CONSP (Vdisplay_pixels_per_inch)
19756 && (ppi = (width_p
19757 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19758 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19759 ppi > 0)))
19760 return OK_PIXELS (ppi / pixels);
19761
19762 return 0;
19763 }
19764 }
19765
19766 #ifdef HAVE_WINDOW_SYSTEM
19767 if (EQ (prop, Qheight))
19768 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19769 if (EQ (prop, Qwidth))
19770 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19771 #else
19772 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19773 return OK_PIXELS (1);
19774 #endif
19775
19776 if (EQ (prop, Qtext))
19777 return OK_PIXELS (width_p
19778 ? window_box_width (it->w, TEXT_AREA)
19779 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19780
19781 if (align_to && *align_to < 0)
19782 {
19783 *res = 0;
19784 if (EQ (prop, Qleft))
19785 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19786 if (EQ (prop, Qright))
19787 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19788 if (EQ (prop, Qcenter))
19789 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19790 + window_box_width (it->w, TEXT_AREA) / 2);
19791 if (EQ (prop, Qleft_fringe))
19792 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19793 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19794 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19795 if (EQ (prop, Qright_fringe))
19796 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19797 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19798 : window_box_right_offset (it->w, TEXT_AREA));
19799 if (EQ (prop, Qleft_margin))
19800 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19801 if (EQ (prop, Qright_margin))
19802 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19803 if (EQ (prop, Qscroll_bar))
19804 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19805 ? 0
19806 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19807 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19808 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19809 : 0)));
19810 }
19811 else
19812 {
19813 if (EQ (prop, Qleft_fringe))
19814 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19815 if (EQ (prop, Qright_fringe))
19816 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19817 if (EQ (prop, Qleft_margin))
19818 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19819 if (EQ (prop, Qright_margin))
19820 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19821 if (EQ (prop, Qscroll_bar))
19822 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19823 }
19824
19825 prop = Fbuffer_local_value (prop, it->w->buffer);
19826 }
19827
19828 if (INTEGERP (prop) || FLOATP (prop))
19829 {
19830 int base_unit = (width_p
19831 ? FRAME_COLUMN_WIDTH (it->f)
19832 : FRAME_LINE_HEIGHT (it->f));
19833 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19834 }
19835
19836 if (CONSP (prop))
19837 {
19838 Lisp_Object car = XCAR (prop);
19839 Lisp_Object cdr = XCDR (prop);
19840
19841 if (SYMBOLP (car))
19842 {
19843 #ifdef HAVE_WINDOW_SYSTEM
19844 if (FRAME_WINDOW_P (it->f)
19845 && valid_image_p (prop))
19846 {
19847 int id = lookup_image (it->f, prop);
19848 struct image *img = IMAGE_FROM_ID (it->f, id);
19849
19850 return OK_PIXELS (width_p ? img->width : img->height);
19851 }
19852 #endif
19853 if (EQ (car, Qplus) || EQ (car, Qminus))
19854 {
19855 int first = 1;
19856 double px;
19857
19858 pixels = 0;
19859 while (CONSP (cdr))
19860 {
19861 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19862 font, width_p, align_to))
19863 return 0;
19864 if (first)
19865 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19866 else
19867 pixels += px;
19868 cdr = XCDR (cdr);
19869 }
19870 if (EQ (car, Qminus))
19871 pixels = -pixels;
19872 return OK_PIXELS (pixels);
19873 }
19874
19875 car = Fbuffer_local_value (car, it->w->buffer);
19876 }
19877
19878 if (INTEGERP (car) || FLOATP (car))
19879 {
19880 double fact;
19881 pixels = XFLOATINT (car);
19882 if (NILP (cdr))
19883 return OK_PIXELS (pixels);
19884 if (calc_pixel_width_or_height (&fact, it, cdr,
19885 font, width_p, align_to))
19886 return OK_PIXELS (pixels * fact);
19887 return 0;
19888 }
19889
19890 return 0;
19891 }
19892
19893 return 0;
19894 }
19895
19896 \f
19897 /***********************************************************************
19898 Glyph Display
19899 ***********************************************************************/
19900
19901 #ifdef HAVE_WINDOW_SYSTEM
19902
19903 #if GLYPH_DEBUG
19904
19905 void
19906 dump_glyph_string (s)
19907 struct glyph_string *s;
19908 {
19909 fprintf (stderr, "glyph string\n");
19910 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19911 s->x, s->y, s->width, s->height);
19912 fprintf (stderr, " ybase = %d\n", s->ybase);
19913 fprintf (stderr, " hl = %d\n", s->hl);
19914 fprintf (stderr, " left overhang = %d, right = %d\n",
19915 s->left_overhang, s->right_overhang);
19916 fprintf (stderr, " nchars = %d\n", s->nchars);
19917 fprintf (stderr, " extends to end of line = %d\n",
19918 s->extends_to_end_of_line_p);
19919 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19920 fprintf (stderr, " bg width = %d\n", s->background_width);
19921 }
19922
19923 #endif /* GLYPH_DEBUG */
19924
19925 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19926 of XChar2b structures for S; it can't be allocated in
19927 init_glyph_string because it must be allocated via `alloca'. W
19928 is the window on which S is drawn. ROW and AREA are the glyph row
19929 and area within the row from which S is constructed. START is the
19930 index of the first glyph structure covered by S. HL is a
19931 face-override for drawing S. */
19932
19933 #ifdef HAVE_NTGUI
19934 #define OPTIONAL_HDC(hdc) hdc,
19935 #define DECLARE_HDC(hdc) HDC hdc;
19936 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19937 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19938 #endif
19939
19940 #ifndef OPTIONAL_HDC
19941 #define OPTIONAL_HDC(hdc)
19942 #define DECLARE_HDC(hdc)
19943 #define ALLOCATE_HDC(hdc, f)
19944 #define RELEASE_HDC(hdc, f)
19945 #endif
19946
19947 static void
19948 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19949 struct glyph_string *s;
19950 DECLARE_HDC (hdc)
19951 XChar2b *char2b;
19952 struct window *w;
19953 struct glyph_row *row;
19954 enum glyph_row_area area;
19955 int start;
19956 enum draw_glyphs_face hl;
19957 {
19958 bzero (s, sizeof *s);
19959 s->w = w;
19960 s->f = XFRAME (w->frame);
19961 #ifdef HAVE_NTGUI
19962 s->hdc = hdc;
19963 #endif
19964 s->display = FRAME_X_DISPLAY (s->f);
19965 s->window = FRAME_X_WINDOW (s->f);
19966 s->char2b = char2b;
19967 s->hl = hl;
19968 s->row = row;
19969 s->area = area;
19970 s->first_glyph = row->glyphs[area] + start;
19971 s->height = row->height;
19972 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19973 s->ybase = s->y + row->ascent;
19974 }
19975
19976
19977 /* Append the list of glyph strings with head H and tail T to the list
19978 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19979
19980 static INLINE void
19981 append_glyph_string_lists (head, tail, h, t)
19982 struct glyph_string **head, **tail;
19983 struct glyph_string *h, *t;
19984 {
19985 if (h)
19986 {
19987 if (*head)
19988 (*tail)->next = h;
19989 else
19990 *head = h;
19991 h->prev = *tail;
19992 *tail = t;
19993 }
19994 }
19995
19996
19997 /* Prepend the list of glyph strings with head H and tail T to the
19998 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19999 result. */
20000
20001 static INLINE void
20002 prepend_glyph_string_lists (head, tail, h, t)
20003 struct glyph_string **head, **tail;
20004 struct glyph_string *h, *t;
20005 {
20006 if (h)
20007 {
20008 if (*head)
20009 (*head)->prev = t;
20010 else
20011 *tail = t;
20012 t->next = *head;
20013 *head = h;
20014 }
20015 }
20016
20017
20018 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20019 Set *HEAD and *TAIL to the resulting list. */
20020
20021 static INLINE void
20022 append_glyph_string (head, tail, s)
20023 struct glyph_string **head, **tail;
20024 struct glyph_string *s;
20025 {
20026 s->next = s->prev = NULL;
20027 append_glyph_string_lists (head, tail, s, s);
20028 }
20029
20030
20031 /* Get face and two-byte form of character C in face FACE_ID on frame
20032 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20033 means we want to display multibyte text. DISPLAY_P non-zero means
20034 make sure that X resources for the face returned are allocated.
20035 Value is a pointer to a realized face that is ready for display if
20036 DISPLAY_P is non-zero. */
20037
20038 static INLINE struct face *
20039 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
20040 struct frame *f;
20041 int c, face_id;
20042 XChar2b *char2b;
20043 int multibyte_p, display_p;
20044 {
20045 struct face *face = FACE_FROM_ID (f, face_id);
20046
20047 if (face->font)
20048 {
20049 unsigned code = face->font->driver->encode_char (face->font, c);
20050
20051 if (code != FONT_INVALID_CODE)
20052 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20053 else
20054 STORE_XCHAR2B (char2b, 0, 0);
20055 }
20056
20057 /* Make sure X resources of the face are allocated. */
20058 #ifdef HAVE_X_WINDOWS
20059 if (display_p)
20060 #endif
20061 {
20062 xassert (face != NULL);
20063 PREPARE_FACE_FOR_DISPLAY (f, face);
20064 }
20065
20066 return face;
20067 }
20068
20069
20070 /* Get face and two-byte form of character glyph GLYPH on frame F.
20071 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20072 a pointer to a realized face that is ready for display. */
20073
20074 static INLINE struct face *
20075 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
20076 struct frame *f;
20077 struct glyph *glyph;
20078 XChar2b *char2b;
20079 int *two_byte_p;
20080 {
20081 struct face *face;
20082
20083 xassert (glyph->type == CHAR_GLYPH);
20084 face = FACE_FROM_ID (f, glyph->face_id);
20085
20086 if (two_byte_p)
20087 *two_byte_p = 0;
20088
20089 if (face->font)
20090 {
20091 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
20092
20093 if (code != FONT_INVALID_CODE)
20094 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20095 else
20096 STORE_XCHAR2B (char2b, 0, 0);
20097 }
20098
20099 /* Make sure X resources of the face are allocated. */
20100 xassert (face != NULL);
20101 PREPARE_FACE_FOR_DISPLAY (f, face);
20102 return face;
20103 }
20104
20105
20106 /* Fill glyph string S with composition components specified by S->cmp.
20107
20108 BASE_FACE is the base face of the composition.
20109 S->cmp_from is the index of the first component for S.
20110
20111 OVERLAPS non-zero means S should draw the foreground only, and use
20112 its physical height for clipping. See also draw_glyphs.
20113
20114 Value is the index of a component not in S. */
20115
20116 static int
20117 fill_composite_glyph_string (s, base_face, overlaps)
20118 struct glyph_string *s;
20119 struct face *base_face;
20120 int overlaps;
20121 {
20122 int i;
20123 /* For all glyphs of this composition, starting at the offset
20124 S->cmp_from, until we reach the end of the definition or encounter a
20125 glyph that requires the different face, add it to S. */
20126 struct face *face;
20127
20128 xassert (s);
20129
20130 s->for_overlaps = overlaps;
20131 s->face = NULL;
20132 s->font = NULL;
20133 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20134 {
20135 int c = COMPOSITION_GLYPH (s->cmp, i);
20136
20137 if (c != '\t')
20138 {
20139 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20140 -1, Qnil);
20141
20142 face = get_char_face_and_encoding (s->f, c, face_id,
20143 s->char2b + i, 1, 1);
20144 if (face)
20145 {
20146 if (! s->face)
20147 {
20148 s->face = face;
20149 s->font = s->face->font;
20150 }
20151 else if (s->face != face)
20152 break;
20153 }
20154 }
20155 ++s->nchars;
20156 }
20157 s->cmp_to = i;
20158
20159 /* All glyph strings for the same composition has the same width,
20160 i.e. the width set for the first component of the composition. */
20161 s->width = s->first_glyph->pixel_width;
20162
20163 /* If the specified font could not be loaded, use the frame's
20164 default font, but record the fact that we couldn't load it in
20165 the glyph string so that we can draw rectangles for the
20166 characters of the glyph string. */
20167 if (s->font == NULL)
20168 {
20169 s->font_not_found_p = 1;
20170 s->font = FRAME_FONT (s->f);
20171 }
20172
20173 /* Adjust base line for subscript/superscript text. */
20174 s->ybase += s->first_glyph->voffset;
20175
20176 /* This glyph string must always be drawn with 16-bit functions. */
20177 s->two_byte_p = 1;
20178
20179 return s->cmp_to;
20180 }
20181
20182 static int
20183 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
20184 struct glyph_string *s;
20185 int face_id;
20186 int start, end, overlaps;
20187 {
20188 struct glyph *glyph, *last;
20189 Lisp_Object lgstring;
20190 int i;
20191
20192 s->for_overlaps = overlaps;
20193 glyph = s->row->glyphs[s->area] + start;
20194 last = s->row->glyphs[s->area] + end;
20195 s->cmp_id = glyph->u.cmp.id;
20196 s->cmp_from = glyph->u.cmp.from;
20197 s->cmp_to = glyph->u.cmp.to + 1;
20198 s->face = FACE_FROM_ID (s->f, face_id);
20199 lgstring = composition_gstring_from_id (s->cmp_id);
20200 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20201 glyph++;
20202 while (glyph < last
20203 && glyph->u.cmp.automatic
20204 && glyph->u.cmp.id == s->cmp_id
20205 && s->cmp_to == glyph->u.cmp.from)
20206 s->cmp_to = (glyph++)->u.cmp.to + 1;
20207
20208 for (i = s->cmp_from; i < s->cmp_to; i++)
20209 {
20210 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20211 unsigned code = LGLYPH_CODE (lglyph);
20212
20213 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20214 }
20215 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20216 return glyph - s->row->glyphs[s->area];
20217 }
20218
20219
20220 /* Fill glyph string S from a sequence of character glyphs.
20221
20222 FACE_ID is the face id of the string. START is the index of the
20223 first glyph to consider, END is the index of the last + 1.
20224 OVERLAPS non-zero means S should draw the foreground only, and use
20225 its physical height for clipping. See also draw_glyphs.
20226
20227 Value is the index of the first glyph not in S. */
20228
20229 static int
20230 fill_glyph_string (s, face_id, start, end, overlaps)
20231 struct glyph_string *s;
20232 int face_id;
20233 int start, end, overlaps;
20234 {
20235 struct glyph *glyph, *last;
20236 int voffset;
20237 int glyph_not_available_p;
20238
20239 xassert (s->f == XFRAME (s->w->frame));
20240 xassert (s->nchars == 0);
20241 xassert (start >= 0 && end > start);
20242
20243 s->for_overlaps = overlaps;
20244 glyph = s->row->glyphs[s->area] + start;
20245 last = s->row->glyphs[s->area] + end;
20246 voffset = glyph->voffset;
20247 s->padding_p = glyph->padding_p;
20248 glyph_not_available_p = glyph->glyph_not_available_p;
20249
20250 while (glyph < last
20251 && glyph->type == CHAR_GLYPH
20252 && glyph->voffset == voffset
20253 /* Same face id implies same font, nowadays. */
20254 && glyph->face_id == face_id
20255 && glyph->glyph_not_available_p == glyph_not_available_p)
20256 {
20257 int two_byte_p;
20258
20259 s->face = get_glyph_face_and_encoding (s->f, glyph,
20260 s->char2b + s->nchars,
20261 &two_byte_p);
20262 s->two_byte_p = two_byte_p;
20263 ++s->nchars;
20264 xassert (s->nchars <= end - start);
20265 s->width += glyph->pixel_width;
20266 if (glyph++->padding_p != s->padding_p)
20267 break;
20268 }
20269
20270 s->font = s->face->font;
20271
20272 /* If the specified font could not be loaded, use the frame's font,
20273 but record the fact that we couldn't load it in
20274 S->font_not_found_p so that we can draw rectangles for the
20275 characters of the glyph string. */
20276 if (s->font == NULL || glyph_not_available_p)
20277 {
20278 s->font_not_found_p = 1;
20279 s->font = FRAME_FONT (s->f);
20280 }
20281
20282 /* Adjust base line for subscript/superscript text. */
20283 s->ybase += voffset;
20284
20285 xassert (s->face && s->face->gc);
20286 return glyph - s->row->glyphs[s->area];
20287 }
20288
20289
20290 /* Fill glyph string S from image glyph S->first_glyph. */
20291
20292 static void
20293 fill_image_glyph_string (s)
20294 struct glyph_string *s;
20295 {
20296 xassert (s->first_glyph->type == IMAGE_GLYPH);
20297 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20298 xassert (s->img);
20299 s->slice = s->first_glyph->slice;
20300 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20301 s->font = s->face->font;
20302 s->width = s->first_glyph->pixel_width;
20303
20304 /* Adjust base line for subscript/superscript text. */
20305 s->ybase += s->first_glyph->voffset;
20306 }
20307
20308
20309 /* Fill glyph string S from a sequence of stretch glyphs.
20310
20311 ROW is the glyph row in which the glyphs are found, AREA is the
20312 area within the row. START is the index of the first glyph to
20313 consider, END is the index of the last + 1.
20314
20315 Value is the index of the first glyph not in S. */
20316
20317 static int
20318 fill_stretch_glyph_string (s, row, area, start, end)
20319 struct glyph_string *s;
20320 struct glyph_row *row;
20321 enum glyph_row_area area;
20322 int start, end;
20323 {
20324 struct glyph *glyph, *last;
20325 int voffset, face_id;
20326
20327 xassert (s->first_glyph->type == STRETCH_GLYPH);
20328
20329 glyph = s->row->glyphs[s->area] + start;
20330 last = s->row->glyphs[s->area] + end;
20331 face_id = glyph->face_id;
20332 s->face = FACE_FROM_ID (s->f, face_id);
20333 s->font = s->face->font;
20334 s->width = glyph->pixel_width;
20335 s->nchars = 1;
20336 voffset = glyph->voffset;
20337
20338 for (++glyph;
20339 (glyph < last
20340 && glyph->type == STRETCH_GLYPH
20341 && glyph->voffset == voffset
20342 && glyph->face_id == face_id);
20343 ++glyph)
20344 s->width += glyph->pixel_width;
20345
20346 /* Adjust base line for subscript/superscript text. */
20347 s->ybase += voffset;
20348
20349 /* The case that face->gc == 0 is handled when drawing the glyph
20350 string by calling PREPARE_FACE_FOR_DISPLAY. */
20351 xassert (s->face);
20352 return glyph - s->row->glyphs[s->area];
20353 }
20354
20355 static struct font_metrics *
20356 get_per_char_metric (f, font, char2b)
20357 struct frame *f;
20358 struct font *font;
20359 XChar2b *char2b;
20360 {
20361 static struct font_metrics metrics;
20362 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20363
20364 if (! font || code == FONT_INVALID_CODE)
20365 return NULL;
20366 font->driver->text_extents (font, &code, 1, &metrics);
20367 return &metrics;
20368 }
20369
20370 /* EXPORT for RIF:
20371 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20372 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20373 assumed to be zero. */
20374
20375 void
20376 x_get_glyph_overhangs (glyph, f, left, right)
20377 struct glyph *glyph;
20378 struct frame *f;
20379 int *left, *right;
20380 {
20381 *left = *right = 0;
20382
20383 if (glyph->type == CHAR_GLYPH)
20384 {
20385 struct face *face;
20386 XChar2b char2b;
20387 struct font_metrics *pcm;
20388
20389 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20390 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20391 {
20392 if (pcm->rbearing > pcm->width)
20393 *right = pcm->rbearing - pcm->width;
20394 if (pcm->lbearing < 0)
20395 *left = -pcm->lbearing;
20396 }
20397 }
20398 else if (glyph->type == COMPOSITE_GLYPH)
20399 {
20400 if (! glyph->u.cmp.automatic)
20401 {
20402 struct composition *cmp = composition_table[glyph->u.cmp.id];
20403
20404 if (cmp->rbearing > cmp->pixel_width)
20405 *right = cmp->rbearing - cmp->pixel_width;
20406 if (cmp->lbearing < 0)
20407 *left = - cmp->lbearing;
20408 }
20409 else
20410 {
20411 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20412 struct font_metrics metrics;
20413
20414 composition_gstring_width (gstring, glyph->u.cmp.from,
20415 glyph->u.cmp.to + 1, &metrics);
20416 if (metrics.rbearing > metrics.width)
20417 *right = metrics.rbearing - metrics.width;
20418 if (metrics.lbearing < 0)
20419 *left = - metrics.lbearing;
20420 }
20421 }
20422 }
20423
20424
20425 /* Return the index of the first glyph preceding glyph string S that
20426 is overwritten by S because of S's left overhang. Value is -1
20427 if no glyphs are overwritten. */
20428
20429 static int
20430 left_overwritten (s)
20431 struct glyph_string *s;
20432 {
20433 int k;
20434
20435 if (s->left_overhang)
20436 {
20437 int x = 0, i;
20438 struct glyph *glyphs = s->row->glyphs[s->area];
20439 int first = s->first_glyph - glyphs;
20440
20441 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20442 x -= glyphs[i].pixel_width;
20443
20444 k = i + 1;
20445 }
20446 else
20447 k = -1;
20448
20449 return k;
20450 }
20451
20452
20453 /* Return the index of the first glyph preceding glyph string S that
20454 is overwriting S because of its right overhang. Value is -1 if no
20455 glyph in front of S overwrites S. */
20456
20457 static int
20458 left_overwriting (s)
20459 struct glyph_string *s;
20460 {
20461 int i, k, x;
20462 struct glyph *glyphs = s->row->glyphs[s->area];
20463 int first = s->first_glyph - glyphs;
20464
20465 k = -1;
20466 x = 0;
20467 for (i = first - 1; i >= 0; --i)
20468 {
20469 int left, right;
20470 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20471 if (x + right > 0)
20472 k = i;
20473 x -= glyphs[i].pixel_width;
20474 }
20475
20476 return k;
20477 }
20478
20479
20480 /* Return the index of the last glyph following glyph string S that is
20481 overwritten by S because of S's right overhang. Value is -1 if
20482 no such glyph is found. */
20483
20484 static int
20485 right_overwritten (s)
20486 struct glyph_string *s;
20487 {
20488 int k = -1;
20489
20490 if (s->right_overhang)
20491 {
20492 int x = 0, i;
20493 struct glyph *glyphs = s->row->glyphs[s->area];
20494 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20495 int end = s->row->used[s->area];
20496
20497 for (i = first; i < end && s->right_overhang > x; ++i)
20498 x += glyphs[i].pixel_width;
20499
20500 k = i;
20501 }
20502
20503 return k;
20504 }
20505
20506
20507 /* Return the index of the last glyph following glyph string S that
20508 overwrites S because of its left overhang. Value is negative
20509 if no such glyph is found. */
20510
20511 static int
20512 right_overwriting (s)
20513 struct glyph_string *s;
20514 {
20515 int i, k, x;
20516 int end = s->row->used[s->area];
20517 struct glyph *glyphs = s->row->glyphs[s->area];
20518 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20519
20520 k = -1;
20521 x = 0;
20522 for (i = first; i < end; ++i)
20523 {
20524 int left, right;
20525 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20526 if (x - left < 0)
20527 k = i;
20528 x += glyphs[i].pixel_width;
20529 }
20530
20531 return k;
20532 }
20533
20534
20535 /* Set background width of glyph string S. START is the index of the
20536 first glyph following S. LAST_X is the right-most x-position + 1
20537 in the drawing area. */
20538
20539 static INLINE void
20540 set_glyph_string_background_width (s, start, last_x)
20541 struct glyph_string *s;
20542 int start;
20543 int last_x;
20544 {
20545 /* If the face of this glyph string has to be drawn to the end of
20546 the drawing area, set S->extends_to_end_of_line_p. */
20547
20548 if (start == s->row->used[s->area]
20549 && s->area == TEXT_AREA
20550 && ((s->row->fill_line_p
20551 && (s->hl == DRAW_NORMAL_TEXT
20552 || s->hl == DRAW_IMAGE_RAISED
20553 || s->hl == DRAW_IMAGE_SUNKEN))
20554 || s->hl == DRAW_MOUSE_FACE))
20555 s->extends_to_end_of_line_p = 1;
20556
20557 /* If S extends its face to the end of the line, set its
20558 background_width to the distance to the right edge of the drawing
20559 area. */
20560 if (s->extends_to_end_of_line_p)
20561 s->background_width = last_x - s->x + 1;
20562 else
20563 s->background_width = s->width;
20564 }
20565
20566
20567 /* Compute overhangs and x-positions for glyph string S and its
20568 predecessors, or successors. X is the starting x-position for S.
20569 BACKWARD_P non-zero means process predecessors. */
20570
20571 static void
20572 compute_overhangs_and_x (s, x, backward_p)
20573 struct glyph_string *s;
20574 int x;
20575 int backward_p;
20576 {
20577 if (backward_p)
20578 {
20579 while (s)
20580 {
20581 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20582 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20583 x -= s->width;
20584 s->x = x;
20585 s = s->prev;
20586 }
20587 }
20588 else
20589 {
20590 while (s)
20591 {
20592 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20593 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20594 s->x = x;
20595 x += s->width;
20596 s = s->next;
20597 }
20598 }
20599 }
20600
20601
20602
20603 /* The following macros are only called from draw_glyphs below.
20604 They reference the following parameters of that function directly:
20605 `w', `row', `area', and `overlap_p'
20606 as well as the following local variables:
20607 `s', `f', and `hdc' (in W32) */
20608
20609 #ifdef HAVE_NTGUI
20610 /* On W32, silently add local `hdc' variable to argument list of
20611 init_glyph_string. */
20612 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20613 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20614 #else
20615 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20616 init_glyph_string (s, char2b, w, row, area, start, hl)
20617 #endif
20618
20619 /* Add a glyph string for a stretch glyph to the list of strings
20620 between HEAD and TAIL. START is the index of the stretch glyph in
20621 row area AREA of glyph row ROW. END is the index of the last glyph
20622 in that glyph row area. X is the current output position assigned
20623 to the new glyph string constructed. HL overrides that face of the
20624 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20625 is the right-most x-position of the drawing area. */
20626
20627 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20628 and below -- keep them on one line. */
20629 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20630 do \
20631 { \
20632 s = (struct glyph_string *) alloca (sizeof *s); \
20633 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20634 START = fill_stretch_glyph_string (s, row, area, START, END); \
20635 append_glyph_string (&HEAD, &TAIL, s); \
20636 s->x = (X); \
20637 } \
20638 while (0)
20639
20640
20641 /* Add a glyph string for an image glyph to the list of strings
20642 between HEAD and TAIL. START is the index of the image glyph in
20643 row area AREA of glyph row ROW. END is the index of the last glyph
20644 in that glyph row area. X is the current output position assigned
20645 to the new glyph string constructed. HL overrides that face of the
20646 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20647 is the right-most x-position of the drawing area. */
20648
20649 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20650 do \
20651 { \
20652 s = (struct glyph_string *) alloca (sizeof *s); \
20653 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20654 fill_image_glyph_string (s); \
20655 append_glyph_string (&HEAD, &TAIL, s); \
20656 ++START; \
20657 s->x = (X); \
20658 } \
20659 while (0)
20660
20661
20662 /* Add a glyph string for a sequence of character glyphs to the list
20663 of strings between HEAD and TAIL. START is the index of the first
20664 glyph in row area AREA of glyph row ROW that is part of the new
20665 glyph string. END is the index of the last glyph in that glyph row
20666 area. X is the current output position assigned to the new glyph
20667 string constructed. HL overrides that face of the glyph; e.g. it
20668 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20669 right-most x-position of the drawing area. */
20670
20671 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20672 do \
20673 { \
20674 int face_id; \
20675 XChar2b *char2b; \
20676 \
20677 face_id = (row)->glyphs[area][START].face_id; \
20678 \
20679 s = (struct glyph_string *) alloca (sizeof *s); \
20680 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20681 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20682 append_glyph_string (&HEAD, &TAIL, s); \
20683 s->x = (X); \
20684 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20685 } \
20686 while (0)
20687
20688
20689 /* Add a glyph string for a composite sequence to the list of strings
20690 between HEAD and TAIL. START is the index of the first glyph in
20691 row area AREA of glyph row ROW that is part of the new glyph
20692 string. END is the index of the last glyph in that glyph row area.
20693 X is the current output position assigned to the new glyph string
20694 constructed. HL overrides that face of the glyph; e.g. it is
20695 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20696 x-position of the drawing area. */
20697
20698 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20699 do { \
20700 int face_id = (row)->glyphs[area][START].face_id; \
20701 struct face *base_face = FACE_FROM_ID (f, face_id); \
20702 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20703 struct composition *cmp = composition_table[cmp_id]; \
20704 XChar2b *char2b; \
20705 struct glyph_string *first_s; \
20706 int n; \
20707 \
20708 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20709 \
20710 /* Make glyph_strings for each glyph sequence that is drawable by \
20711 the same face, and append them to HEAD/TAIL. */ \
20712 for (n = 0; n < cmp->glyph_len;) \
20713 { \
20714 s = (struct glyph_string *) alloca (sizeof *s); \
20715 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20716 append_glyph_string (&(HEAD), &(TAIL), s); \
20717 s->cmp = cmp; \
20718 s->cmp_from = n; \
20719 s->x = (X); \
20720 if (n == 0) \
20721 first_s = s; \
20722 n = fill_composite_glyph_string (s, base_face, overlaps); \
20723 } \
20724 \
20725 ++START; \
20726 s = first_s; \
20727 } while (0)
20728
20729
20730 /* Add a glyph string for a glyph-string sequence to the list of strings
20731 between HEAD and TAIL. */
20732
20733 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20734 do { \
20735 int face_id; \
20736 XChar2b *char2b; \
20737 Lisp_Object gstring; \
20738 \
20739 face_id = (row)->glyphs[area][START].face_id; \
20740 gstring = (composition_gstring_from_id \
20741 ((row)->glyphs[area][START].u.cmp.id)); \
20742 s = (struct glyph_string *) alloca (sizeof *s); \
20743 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20744 * LGSTRING_GLYPH_LEN (gstring)); \
20745 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20746 append_glyph_string (&(HEAD), &(TAIL), s); \
20747 s->x = (X); \
20748 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20749 } while (0)
20750
20751
20752 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20753 of AREA of glyph row ROW on window W between indices START and END.
20754 HL overrides the face for drawing glyph strings, e.g. it is
20755 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20756 x-positions of the drawing area.
20757
20758 This is an ugly monster macro construct because we must use alloca
20759 to allocate glyph strings (because draw_glyphs can be called
20760 asynchronously). */
20761
20762 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20763 do \
20764 { \
20765 HEAD = TAIL = NULL; \
20766 while (START < END) \
20767 { \
20768 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20769 switch (first_glyph->type) \
20770 { \
20771 case CHAR_GLYPH: \
20772 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20773 HL, X, LAST_X); \
20774 break; \
20775 \
20776 case COMPOSITE_GLYPH: \
20777 if (first_glyph->u.cmp.automatic) \
20778 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20779 HL, X, LAST_X); \
20780 else \
20781 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20782 HL, X, LAST_X); \
20783 break; \
20784 \
20785 case STRETCH_GLYPH: \
20786 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20787 HL, X, LAST_X); \
20788 break; \
20789 \
20790 case IMAGE_GLYPH: \
20791 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20792 HL, X, LAST_X); \
20793 break; \
20794 \
20795 default: \
20796 abort (); \
20797 } \
20798 \
20799 if (s) \
20800 { \
20801 set_glyph_string_background_width (s, START, LAST_X); \
20802 (X) += s->width; \
20803 } \
20804 } \
20805 } while (0)
20806
20807
20808 /* Draw glyphs between START and END in AREA of ROW on window W,
20809 starting at x-position X. X is relative to AREA in W. HL is a
20810 face-override with the following meaning:
20811
20812 DRAW_NORMAL_TEXT draw normally
20813 DRAW_CURSOR draw in cursor face
20814 DRAW_MOUSE_FACE draw in mouse face.
20815 DRAW_INVERSE_VIDEO draw in mode line face
20816 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20817 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20818
20819 If OVERLAPS is non-zero, draw only the foreground of characters and
20820 clip to the physical height of ROW. Non-zero value also defines
20821 the overlapping part to be drawn:
20822
20823 OVERLAPS_PRED overlap with preceding rows
20824 OVERLAPS_SUCC overlap with succeeding rows
20825 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20826 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20827
20828 Value is the x-position reached, relative to AREA of W. */
20829
20830 static int
20831 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20832 struct window *w;
20833 int x;
20834 struct glyph_row *row;
20835 enum glyph_row_area area;
20836 EMACS_INT start, end;
20837 enum draw_glyphs_face hl;
20838 int overlaps;
20839 {
20840 struct glyph_string *head, *tail;
20841 struct glyph_string *s;
20842 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20843 int i, j, x_reached, last_x, area_left = 0;
20844 struct frame *f = XFRAME (WINDOW_FRAME (w));
20845 DECLARE_HDC (hdc);
20846
20847 ALLOCATE_HDC (hdc, f);
20848
20849 /* Let's rather be paranoid than getting a SEGV. */
20850 end = min (end, row->used[area]);
20851 start = max (0, start);
20852 start = min (end, start);
20853
20854 /* Translate X to frame coordinates. Set last_x to the right
20855 end of the drawing area. */
20856 if (row->full_width_p)
20857 {
20858 /* X is relative to the left edge of W, without scroll bars
20859 or fringes. */
20860 area_left = WINDOW_LEFT_EDGE_X (w);
20861 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20862 }
20863 else
20864 {
20865 area_left = window_box_left (w, area);
20866 last_x = area_left + window_box_width (w, area);
20867 }
20868 x += area_left;
20869
20870 /* Build a doubly-linked list of glyph_string structures between
20871 head and tail from what we have to draw. Note that the macro
20872 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20873 the reason we use a separate variable `i'. */
20874 i = start;
20875 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20876 if (tail)
20877 x_reached = tail->x + tail->background_width;
20878 else
20879 x_reached = x;
20880
20881 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20882 the row, redraw some glyphs in front or following the glyph
20883 strings built above. */
20884 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20885 {
20886 struct glyph_string *h, *t;
20887 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20888 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20889 int dummy_x = 0;
20890
20891 /* If mouse highlighting is on, we may need to draw adjacent
20892 glyphs using mouse-face highlighting. */
20893 if (area == TEXT_AREA && row->mouse_face_p)
20894 {
20895 struct glyph_row *mouse_beg_row, *mouse_end_row;
20896
20897 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20898 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20899
20900 if (row >= mouse_beg_row && row <= mouse_end_row)
20901 {
20902 check_mouse_face = 1;
20903 mouse_beg_col = (row == mouse_beg_row)
20904 ? dpyinfo->mouse_face_beg_col : 0;
20905 mouse_end_col = (row == mouse_end_row)
20906 ? dpyinfo->mouse_face_end_col
20907 : row->used[TEXT_AREA];
20908 }
20909 }
20910
20911 /* Compute overhangs for all glyph strings. */
20912 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20913 for (s = head; s; s = s->next)
20914 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20915
20916 /* Prepend glyph strings for glyphs in front of the first glyph
20917 string that are overwritten because of the first glyph
20918 string's left overhang. The background of all strings
20919 prepended must be drawn because the first glyph string
20920 draws over it. */
20921 i = left_overwritten (head);
20922 if (i >= 0)
20923 {
20924 enum draw_glyphs_face overlap_hl;
20925
20926 /* If this row contains mouse highlighting, attempt to draw
20927 the overlapped glyphs with the correct highlight. This
20928 code fails if the overlap encompasses more than one glyph
20929 and mouse-highlight spans only some of these glyphs.
20930 However, making it work perfectly involves a lot more
20931 code, and I don't know if the pathological case occurs in
20932 practice, so we'll stick to this for now. --- cyd */
20933 if (check_mouse_face
20934 && mouse_beg_col < start && mouse_end_col > i)
20935 overlap_hl = DRAW_MOUSE_FACE;
20936 else
20937 overlap_hl = DRAW_NORMAL_TEXT;
20938
20939 j = i;
20940 BUILD_GLYPH_STRINGS (j, start, h, t,
20941 overlap_hl, dummy_x, last_x);
20942 compute_overhangs_and_x (t, head->x, 1);
20943 prepend_glyph_string_lists (&head, &tail, h, t);
20944 clip_head = head;
20945 }
20946
20947 /* Prepend glyph strings for glyphs in front of the first glyph
20948 string that overwrite that glyph string because of their
20949 right overhang. For these strings, only the foreground must
20950 be drawn, because it draws over the glyph string at `head'.
20951 The background must not be drawn because this would overwrite
20952 right overhangs of preceding glyphs for which no glyph
20953 strings exist. */
20954 i = left_overwriting (head);
20955 if (i >= 0)
20956 {
20957 enum draw_glyphs_face overlap_hl;
20958
20959 if (check_mouse_face
20960 && mouse_beg_col < start && mouse_end_col > i)
20961 overlap_hl = DRAW_MOUSE_FACE;
20962 else
20963 overlap_hl = DRAW_NORMAL_TEXT;
20964
20965 clip_head = head;
20966 BUILD_GLYPH_STRINGS (i, start, h, t,
20967 overlap_hl, dummy_x, last_x);
20968 for (s = h; s; s = s->next)
20969 s->background_filled_p = 1;
20970 compute_overhangs_and_x (t, head->x, 1);
20971 prepend_glyph_string_lists (&head, &tail, h, t);
20972 }
20973
20974 /* Append glyphs strings for glyphs following the last glyph
20975 string tail that are overwritten by tail. The background of
20976 these strings has to be drawn because tail's foreground draws
20977 over it. */
20978 i = right_overwritten (tail);
20979 if (i >= 0)
20980 {
20981 enum draw_glyphs_face overlap_hl;
20982
20983 if (check_mouse_face
20984 && mouse_beg_col < i && mouse_end_col > end)
20985 overlap_hl = DRAW_MOUSE_FACE;
20986 else
20987 overlap_hl = DRAW_NORMAL_TEXT;
20988
20989 BUILD_GLYPH_STRINGS (end, i, h, t,
20990 overlap_hl, x, last_x);
20991 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20992 append_glyph_string_lists (&head, &tail, h, t);
20993 clip_tail = tail;
20994 }
20995
20996 /* Append glyph strings for glyphs following the last glyph
20997 string tail that overwrite tail. The foreground of such
20998 glyphs has to be drawn because it writes into the background
20999 of tail. The background must not be drawn because it could
21000 paint over the foreground of following glyphs. */
21001 i = right_overwriting (tail);
21002 if (i >= 0)
21003 {
21004 enum draw_glyphs_face overlap_hl;
21005 if (check_mouse_face
21006 && mouse_beg_col < i && mouse_end_col > end)
21007 overlap_hl = DRAW_MOUSE_FACE;
21008 else
21009 overlap_hl = DRAW_NORMAL_TEXT;
21010
21011 clip_tail = tail;
21012 i++; /* We must include the Ith glyph. */
21013 BUILD_GLYPH_STRINGS (end, i, h, t,
21014 overlap_hl, x, last_x);
21015 for (s = h; s; s = s->next)
21016 s->background_filled_p = 1;
21017 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21018 append_glyph_string_lists (&head, &tail, h, t);
21019 }
21020 if (clip_head || clip_tail)
21021 for (s = head; s; s = s->next)
21022 {
21023 s->clip_head = clip_head;
21024 s->clip_tail = clip_tail;
21025 }
21026 }
21027
21028 /* Draw all strings. */
21029 for (s = head; s; s = s->next)
21030 FRAME_RIF (f)->draw_glyph_string (s);
21031
21032 #ifndef HAVE_NS
21033 /* When focus a sole frame and move horizontally, this sets on_p to 0
21034 causing a failure to erase prev cursor position. */
21035 if (area == TEXT_AREA
21036 && !row->full_width_p
21037 /* When drawing overlapping rows, only the glyph strings'
21038 foreground is drawn, which doesn't erase a cursor
21039 completely. */
21040 && !overlaps)
21041 {
21042 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21043 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21044 : (tail ? tail->x + tail->background_width : x));
21045 x0 -= area_left;
21046 x1 -= area_left;
21047
21048 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21049 row->y, MATRIX_ROW_BOTTOM_Y (row));
21050 }
21051 #endif
21052
21053 /* Value is the x-position up to which drawn, relative to AREA of W.
21054 This doesn't include parts drawn because of overhangs. */
21055 if (row->full_width_p)
21056 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21057 else
21058 x_reached -= area_left;
21059
21060 RELEASE_HDC (hdc, f);
21061
21062 return x_reached;
21063 }
21064
21065 /* Expand row matrix if too narrow. Don't expand if area
21066 is not present. */
21067
21068 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21069 { \
21070 if (!fonts_changed_p \
21071 && (it->glyph_row->glyphs[area] \
21072 < it->glyph_row->glyphs[area + 1])) \
21073 { \
21074 it->w->ncols_scale_factor++; \
21075 fonts_changed_p = 1; \
21076 } \
21077 }
21078
21079 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21080 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21081
21082 static INLINE void
21083 append_glyph (it)
21084 struct it *it;
21085 {
21086 struct glyph *glyph;
21087 enum glyph_row_area area = it->area;
21088
21089 xassert (it->glyph_row);
21090 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21091
21092 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21093 if (glyph < it->glyph_row->glyphs[area + 1])
21094 {
21095 glyph->charpos = CHARPOS (it->position);
21096 glyph->object = it->object;
21097 if (it->pixel_width > 0)
21098 {
21099 glyph->pixel_width = it->pixel_width;
21100 glyph->padding_p = 0;
21101 }
21102 else
21103 {
21104 /* Assure at least 1-pixel width. Otherwise, cursor can't
21105 be displayed correctly. */
21106 glyph->pixel_width = 1;
21107 glyph->padding_p = 1;
21108 }
21109 glyph->ascent = it->ascent;
21110 glyph->descent = it->descent;
21111 glyph->voffset = it->voffset;
21112 glyph->type = CHAR_GLYPH;
21113 glyph->avoid_cursor_p = it->avoid_cursor_p;
21114 glyph->multibyte_p = it->multibyte_p;
21115 glyph->left_box_line_p = it->start_of_box_run_p;
21116 glyph->right_box_line_p = it->end_of_box_run_p;
21117 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21118 || it->phys_descent > it->descent);
21119 glyph->glyph_not_available_p = it->glyph_not_available_p;
21120 glyph->face_id = it->face_id;
21121 glyph->u.ch = it->char_to_display;
21122 glyph->slice = null_glyph_slice;
21123 glyph->font_type = FONT_TYPE_UNKNOWN;
21124 if (it->bidi_p)
21125 {
21126 glyph->resolved_level = it->bidi_it.resolved_level;
21127 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21128 abort ();
21129 glyph->bidi_type = it->bidi_it.type;
21130 }
21131 ++it->glyph_row->used[area];
21132 }
21133 else
21134 IT_EXPAND_MATRIX_WIDTH (it, area);
21135 }
21136
21137 /* Store one glyph for the composition IT->cmp_it.id in
21138 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21139 non-null. */
21140
21141 static INLINE void
21142 append_composite_glyph (it)
21143 struct it *it;
21144 {
21145 struct glyph *glyph;
21146 enum glyph_row_area area = it->area;
21147
21148 xassert (it->glyph_row);
21149
21150 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21151 if (glyph < it->glyph_row->glyphs[area + 1])
21152 {
21153 glyph->charpos = CHARPOS (it->position);
21154 glyph->object = it->object;
21155 glyph->pixel_width = it->pixel_width;
21156 glyph->ascent = it->ascent;
21157 glyph->descent = it->descent;
21158 glyph->voffset = it->voffset;
21159 glyph->type = COMPOSITE_GLYPH;
21160 if (it->cmp_it.ch < 0)
21161 {
21162 glyph->u.cmp.automatic = 0;
21163 glyph->u.cmp.id = it->cmp_it.id;
21164 }
21165 else
21166 {
21167 glyph->u.cmp.automatic = 1;
21168 glyph->u.cmp.id = it->cmp_it.id;
21169 glyph->u.cmp.from = it->cmp_it.from;
21170 glyph->u.cmp.to = it->cmp_it.to - 1;
21171 }
21172 glyph->avoid_cursor_p = it->avoid_cursor_p;
21173 glyph->multibyte_p = it->multibyte_p;
21174 glyph->left_box_line_p = it->start_of_box_run_p;
21175 glyph->right_box_line_p = it->end_of_box_run_p;
21176 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21177 || it->phys_descent > it->descent);
21178 glyph->padding_p = 0;
21179 glyph->glyph_not_available_p = 0;
21180 glyph->face_id = it->face_id;
21181 glyph->slice = null_glyph_slice;
21182 glyph->font_type = FONT_TYPE_UNKNOWN;
21183 if (it->bidi_p)
21184 {
21185 glyph->resolved_level = it->bidi_it.resolved_level;
21186 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21187 abort ();
21188 glyph->bidi_type = it->bidi_it.type;
21189 }
21190 ++it->glyph_row->used[area];
21191 }
21192 else
21193 IT_EXPAND_MATRIX_WIDTH (it, area);
21194 }
21195
21196
21197 /* Change IT->ascent and IT->height according to the setting of
21198 IT->voffset. */
21199
21200 static INLINE void
21201 take_vertical_position_into_account (it)
21202 struct it *it;
21203 {
21204 if (it->voffset)
21205 {
21206 if (it->voffset < 0)
21207 /* Increase the ascent so that we can display the text higher
21208 in the line. */
21209 it->ascent -= it->voffset;
21210 else
21211 /* Increase the descent so that we can display the text lower
21212 in the line. */
21213 it->descent += it->voffset;
21214 }
21215 }
21216
21217
21218 /* Produce glyphs/get display metrics for the image IT is loaded with.
21219 See the description of struct display_iterator in dispextern.h for
21220 an overview of struct display_iterator. */
21221
21222 static void
21223 produce_image_glyph (it)
21224 struct it *it;
21225 {
21226 struct image *img;
21227 struct face *face;
21228 int glyph_ascent, crop;
21229 struct glyph_slice slice;
21230
21231 xassert (it->what == IT_IMAGE);
21232
21233 face = FACE_FROM_ID (it->f, it->face_id);
21234 xassert (face);
21235 /* Make sure X resources of the face is loaded. */
21236 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21237
21238 if (it->image_id < 0)
21239 {
21240 /* Fringe bitmap. */
21241 it->ascent = it->phys_ascent = 0;
21242 it->descent = it->phys_descent = 0;
21243 it->pixel_width = 0;
21244 it->nglyphs = 0;
21245 return;
21246 }
21247
21248 img = IMAGE_FROM_ID (it->f, it->image_id);
21249 xassert (img);
21250 /* Make sure X resources of the image is loaded. */
21251 prepare_image_for_display (it->f, img);
21252
21253 slice.x = slice.y = 0;
21254 slice.width = img->width;
21255 slice.height = img->height;
21256
21257 if (INTEGERP (it->slice.x))
21258 slice.x = XINT (it->slice.x);
21259 else if (FLOATP (it->slice.x))
21260 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21261
21262 if (INTEGERP (it->slice.y))
21263 slice.y = XINT (it->slice.y);
21264 else if (FLOATP (it->slice.y))
21265 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21266
21267 if (INTEGERP (it->slice.width))
21268 slice.width = XINT (it->slice.width);
21269 else if (FLOATP (it->slice.width))
21270 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21271
21272 if (INTEGERP (it->slice.height))
21273 slice.height = XINT (it->slice.height);
21274 else if (FLOATP (it->slice.height))
21275 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21276
21277 if (slice.x >= img->width)
21278 slice.x = img->width;
21279 if (slice.y >= img->height)
21280 slice.y = img->height;
21281 if (slice.x + slice.width >= img->width)
21282 slice.width = img->width - slice.x;
21283 if (slice.y + slice.height > img->height)
21284 slice.height = img->height - slice.y;
21285
21286 if (slice.width == 0 || slice.height == 0)
21287 return;
21288
21289 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21290
21291 it->descent = slice.height - glyph_ascent;
21292 if (slice.y == 0)
21293 it->descent += img->vmargin;
21294 if (slice.y + slice.height == img->height)
21295 it->descent += img->vmargin;
21296 it->phys_descent = it->descent;
21297
21298 it->pixel_width = slice.width;
21299 if (slice.x == 0)
21300 it->pixel_width += img->hmargin;
21301 if (slice.x + slice.width == img->width)
21302 it->pixel_width += img->hmargin;
21303
21304 /* It's quite possible for images to have an ascent greater than
21305 their height, so don't get confused in that case. */
21306 if (it->descent < 0)
21307 it->descent = 0;
21308
21309 it->nglyphs = 1;
21310
21311 if (face->box != FACE_NO_BOX)
21312 {
21313 if (face->box_line_width > 0)
21314 {
21315 if (slice.y == 0)
21316 it->ascent += face->box_line_width;
21317 if (slice.y + slice.height == img->height)
21318 it->descent += face->box_line_width;
21319 }
21320
21321 if (it->start_of_box_run_p && slice.x == 0)
21322 it->pixel_width += eabs (face->box_line_width);
21323 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21324 it->pixel_width += eabs (face->box_line_width);
21325 }
21326
21327 take_vertical_position_into_account (it);
21328
21329 /* Automatically crop wide image glyphs at right edge so we can
21330 draw the cursor on same display row. */
21331 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21332 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21333 {
21334 it->pixel_width -= crop;
21335 slice.width -= crop;
21336 }
21337
21338 if (it->glyph_row)
21339 {
21340 struct glyph *glyph;
21341 enum glyph_row_area area = it->area;
21342
21343 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21344 if (glyph < it->glyph_row->glyphs[area + 1])
21345 {
21346 glyph->charpos = CHARPOS (it->position);
21347 glyph->object = it->object;
21348 glyph->pixel_width = it->pixel_width;
21349 glyph->ascent = glyph_ascent;
21350 glyph->descent = it->descent;
21351 glyph->voffset = it->voffset;
21352 glyph->type = IMAGE_GLYPH;
21353 glyph->avoid_cursor_p = it->avoid_cursor_p;
21354 glyph->multibyte_p = it->multibyte_p;
21355 glyph->left_box_line_p = it->start_of_box_run_p;
21356 glyph->right_box_line_p = it->end_of_box_run_p;
21357 glyph->overlaps_vertically_p = 0;
21358 glyph->padding_p = 0;
21359 glyph->glyph_not_available_p = 0;
21360 glyph->face_id = it->face_id;
21361 glyph->u.img_id = img->id;
21362 glyph->slice = slice;
21363 glyph->font_type = FONT_TYPE_UNKNOWN;
21364 if (it->bidi_p)
21365 {
21366 glyph->resolved_level = it->bidi_it.resolved_level;
21367 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21368 abort ();
21369 glyph->bidi_type = it->bidi_it.type;
21370 }
21371 ++it->glyph_row->used[area];
21372 }
21373 else
21374 IT_EXPAND_MATRIX_WIDTH (it, area);
21375 }
21376 }
21377
21378
21379 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21380 of the glyph, WIDTH and HEIGHT are the width and height of the
21381 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21382
21383 static void
21384 append_stretch_glyph (it, object, width, height, ascent)
21385 struct it *it;
21386 Lisp_Object object;
21387 int width, height;
21388 int ascent;
21389 {
21390 struct glyph *glyph;
21391 enum glyph_row_area area = it->area;
21392
21393 xassert (ascent >= 0 && ascent <= height);
21394
21395 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21396 if (glyph < it->glyph_row->glyphs[area + 1])
21397 {
21398 glyph->charpos = CHARPOS (it->position);
21399 glyph->object = object;
21400 glyph->pixel_width = width;
21401 glyph->ascent = ascent;
21402 glyph->descent = height - ascent;
21403 glyph->voffset = it->voffset;
21404 glyph->type = STRETCH_GLYPH;
21405 glyph->avoid_cursor_p = it->avoid_cursor_p;
21406 glyph->multibyte_p = it->multibyte_p;
21407 glyph->left_box_line_p = it->start_of_box_run_p;
21408 glyph->right_box_line_p = it->end_of_box_run_p;
21409 glyph->overlaps_vertically_p = 0;
21410 glyph->padding_p = 0;
21411 glyph->glyph_not_available_p = 0;
21412 glyph->face_id = it->face_id;
21413 glyph->u.stretch.ascent = ascent;
21414 glyph->u.stretch.height = height;
21415 glyph->slice = null_glyph_slice;
21416 glyph->font_type = FONT_TYPE_UNKNOWN;
21417 if (it->bidi_p)
21418 {
21419 glyph->resolved_level = it->bidi_it.resolved_level;
21420 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21421 abort ();
21422 glyph->bidi_type = it->bidi_it.type;
21423 }
21424 ++it->glyph_row->used[area];
21425 }
21426 else
21427 IT_EXPAND_MATRIX_WIDTH (it, area);
21428 }
21429
21430
21431 /* Produce a stretch glyph for iterator IT. IT->object is the value
21432 of the glyph property displayed. The value must be a list
21433 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21434 being recognized:
21435
21436 1. `:width WIDTH' specifies that the space should be WIDTH *
21437 canonical char width wide. WIDTH may be an integer or floating
21438 point number.
21439
21440 2. `:relative-width FACTOR' specifies that the width of the stretch
21441 should be computed from the width of the first character having the
21442 `glyph' property, and should be FACTOR times that width.
21443
21444 3. `:align-to HPOS' specifies that the space should be wide enough
21445 to reach HPOS, a value in canonical character units.
21446
21447 Exactly one of the above pairs must be present.
21448
21449 4. `:height HEIGHT' specifies that the height of the stretch produced
21450 should be HEIGHT, measured in canonical character units.
21451
21452 5. `:relative-height FACTOR' specifies that the height of the
21453 stretch should be FACTOR times the height of the characters having
21454 the glyph property.
21455
21456 Either none or exactly one of 4 or 5 must be present.
21457
21458 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21459 of the stretch should be used for the ascent of the stretch.
21460 ASCENT must be in the range 0 <= ASCENT <= 100. */
21461
21462 static void
21463 produce_stretch_glyph (it)
21464 struct it *it;
21465 {
21466 /* (space :width WIDTH :height HEIGHT ...) */
21467 Lisp_Object prop, plist;
21468 int width = 0, height = 0, align_to = -1;
21469 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21470 int ascent = 0;
21471 double tem;
21472 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21473 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21474
21475 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21476
21477 /* List should start with `space'. */
21478 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21479 plist = XCDR (it->object);
21480
21481 /* Compute the width of the stretch. */
21482 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21483 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21484 {
21485 /* Absolute width `:width WIDTH' specified and valid. */
21486 zero_width_ok_p = 1;
21487 width = (int)tem;
21488 }
21489 else if (prop = Fplist_get (plist, QCrelative_width),
21490 NUMVAL (prop) > 0)
21491 {
21492 /* Relative width `:relative-width FACTOR' specified and valid.
21493 Compute the width of the characters having the `glyph'
21494 property. */
21495 struct it it2;
21496 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21497
21498 it2 = *it;
21499 if (it->multibyte_p)
21500 {
21501 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
21502 - IT_BYTEPOS (*it));
21503 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
21504 }
21505 else
21506 it2.c = *p, it2.len = 1;
21507
21508 it2.glyph_row = NULL;
21509 it2.what = IT_CHARACTER;
21510 x_produce_glyphs (&it2);
21511 width = NUMVAL (prop) * it2.pixel_width;
21512 }
21513 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21514 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21515 {
21516 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21517 align_to = (align_to < 0
21518 ? 0
21519 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21520 else if (align_to < 0)
21521 align_to = window_box_left_offset (it->w, TEXT_AREA);
21522 width = max (0, (int)tem + align_to - it->current_x);
21523 zero_width_ok_p = 1;
21524 }
21525 else
21526 /* Nothing specified -> width defaults to canonical char width. */
21527 width = FRAME_COLUMN_WIDTH (it->f);
21528
21529 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21530 width = 1;
21531
21532 /* Compute height. */
21533 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21534 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21535 {
21536 height = (int)tem;
21537 zero_height_ok_p = 1;
21538 }
21539 else if (prop = Fplist_get (plist, QCrelative_height),
21540 NUMVAL (prop) > 0)
21541 height = FONT_HEIGHT (font) * NUMVAL (prop);
21542 else
21543 height = FONT_HEIGHT (font);
21544
21545 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21546 height = 1;
21547
21548 /* Compute percentage of height used for ascent. If
21549 `:ascent ASCENT' is present and valid, use that. Otherwise,
21550 derive the ascent from the font in use. */
21551 if (prop = Fplist_get (plist, QCascent),
21552 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21553 ascent = height * NUMVAL (prop) / 100.0;
21554 else if (!NILP (prop)
21555 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21556 ascent = min (max (0, (int)tem), height);
21557 else
21558 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21559
21560 if (width > 0 && it->line_wrap != TRUNCATE
21561 && it->current_x + width > it->last_visible_x)
21562 width = it->last_visible_x - it->current_x - 1;
21563
21564 if (width > 0 && height > 0 && it->glyph_row)
21565 {
21566 Lisp_Object object = it->stack[it->sp - 1].string;
21567 if (!STRINGP (object))
21568 object = it->w->buffer;
21569 append_stretch_glyph (it, object, width, height, ascent);
21570 }
21571
21572 it->pixel_width = width;
21573 it->ascent = it->phys_ascent = ascent;
21574 it->descent = it->phys_descent = height - it->ascent;
21575 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21576
21577 take_vertical_position_into_account (it);
21578 }
21579
21580 /* Calculate line-height and line-spacing properties.
21581 An integer value specifies explicit pixel value.
21582 A float value specifies relative value to current face height.
21583 A cons (float . face-name) specifies relative value to
21584 height of specified face font.
21585
21586 Returns height in pixels, or nil. */
21587
21588
21589 static Lisp_Object
21590 calc_line_height_property (it, val, font, boff, override)
21591 struct it *it;
21592 Lisp_Object val;
21593 struct font *font;
21594 int boff, override;
21595 {
21596 Lisp_Object face_name = Qnil;
21597 int ascent, descent, height;
21598
21599 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21600 return val;
21601
21602 if (CONSP (val))
21603 {
21604 face_name = XCAR (val);
21605 val = XCDR (val);
21606 if (!NUMBERP (val))
21607 val = make_number (1);
21608 if (NILP (face_name))
21609 {
21610 height = it->ascent + it->descent;
21611 goto scale;
21612 }
21613 }
21614
21615 if (NILP (face_name))
21616 {
21617 font = FRAME_FONT (it->f);
21618 boff = FRAME_BASELINE_OFFSET (it->f);
21619 }
21620 else if (EQ (face_name, Qt))
21621 {
21622 override = 0;
21623 }
21624 else
21625 {
21626 int face_id;
21627 struct face *face;
21628
21629 face_id = lookup_named_face (it->f, face_name, 0);
21630 if (face_id < 0)
21631 return make_number (-1);
21632
21633 face = FACE_FROM_ID (it->f, face_id);
21634 font = face->font;
21635 if (font == NULL)
21636 return make_number (-1);
21637 boff = font->baseline_offset;
21638 if (font->vertical_centering)
21639 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21640 }
21641
21642 ascent = FONT_BASE (font) + boff;
21643 descent = FONT_DESCENT (font) - boff;
21644
21645 if (override)
21646 {
21647 it->override_ascent = ascent;
21648 it->override_descent = descent;
21649 it->override_boff = boff;
21650 }
21651
21652 height = ascent + descent;
21653
21654 scale:
21655 if (FLOATP (val))
21656 height = (int)(XFLOAT_DATA (val) * height);
21657 else if (INTEGERP (val))
21658 height *= XINT (val);
21659
21660 return make_number (height);
21661 }
21662
21663
21664 /* RIF:
21665 Produce glyphs/get display metrics for the display element IT is
21666 loaded with. See the description of struct it in dispextern.h
21667 for an overview of struct it. */
21668
21669 void
21670 x_produce_glyphs (it)
21671 struct it *it;
21672 {
21673 int extra_line_spacing = it->extra_line_spacing;
21674
21675 it->glyph_not_available_p = 0;
21676
21677 if (it->what == IT_CHARACTER)
21678 {
21679 XChar2b char2b;
21680 struct font *font;
21681 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21682 struct font_metrics *pcm;
21683 int font_not_found_p;
21684 int boff; /* baseline offset */
21685 /* We may change it->multibyte_p upon unibyte<->multibyte
21686 conversion. So, save the current value now and restore it
21687 later.
21688
21689 Note: It seems that we don't have to record multibyte_p in
21690 struct glyph because the character code itself tells whether
21691 or not the character is multibyte. Thus, in the future, we
21692 must consider eliminating the field `multibyte_p' in the
21693 struct glyph. */
21694 int saved_multibyte_p = it->multibyte_p;
21695
21696 /* Maybe translate single-byte characters to multibyte, or the
21697 other way. */
21698 it->char_to_display = it->c;
21699 if (!ASCII_BYTE_P (it->c)
21700 && ! it->multibyte_p)
21701 {
21702 if (SINGLE_BYTE_CHAR_P (it->c)
21703 && unibyte_display_via_language_environment)
21704 {
21705 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21706
21707 /* get_next_display_element assures that this decoding
21708 never fails. */
21709 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21710 it->multibyte_p = 1;
21711 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21712 -1, Qnil);
21713 face = FACE_FROM_ID (it->f, it->face_id);
21714 }
21715 }
21716
21717 /* Get font to use. Encode IT->char_to_display. */
21718 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21719 &char2b, it->multibyte_p, 0);
21720 font = face->font;
21721
21722 font_not_found_p = font == NULL;
21723 if (font_not_found_p)
21724 {
21725 /* When no suitable font found, display an empty box based
21726 on the metrics of the font of the default face (or what
21727 remapped). */
21728 struct face *no_font_face
21729 = FACE_FROM_ID (it->f,
21730 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21731 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21732 font = no_font_face->font;
21733 boff = font->baseline_offset;
21734 }
21735 else
21736 {
21737 boff = font->baseline_offset;
21738 if (font->vertical_centering)
21739 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21740 }
21741
21742 if (it->char_to_display >= ' '
21743 && (!it->multibyte_p || it->char_to_display < 128))
21744 {
21745 /* Either unibyte or ASCII. */
21746 int stretched_p;
21747
21748 it->nglyphs = 1;
21749
21750 pcm = get_per_char_metric (it->f, font, &char2b);
21751
21752 if (it->override_ascent >= 0)
21753 {
21754 it->ascent = it->override_ascent;
21755 it->descent = it->override_descent;
21756 boff = it->override_boff;
21757 }
21758 else
21759 {
21760 it->ascent = FONT_BASE (font) + boff;
21761 it->descent = FONT_DESCENT (font) - boff;
21762 }
21763
21764 if (pcm)
21765 {
21766 it->phys_ascent = pcm->ascent + boff;
21767 it->phys_descent = pcm->descent - boff;
21768 it->pixel_width = pcm->width;
21769 }
21770 else
21771 {
21772 it->glyph_not_available_p = 1;
21773 it->phys_ascent = it->ascent;
21774 it->phys_descent = it->descent;
21775 it->pixel_width = FONT_WIDTH (font);
21776 }
21777
21778 if (it->constrain_row_ascent_descent_p)
21779 {
21780 if (it->descent > it->max_descent)
21781 {
21782 it->ascent += it->descent - it->max_descent;
21783 it->descent = it->max_descent;
21784 }
21785 if (it->ascent > it->max_ascent)
21786 {
21787 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21788 it->ascent = it->max_ascent;
21789 }
21790 it->phys_ascent = min (it->phys_ascent, it->ascent);
21791 it->phys_descent = min (it->phys_descent, it->descent);
21792 extra_line_spacing = 0;
21793 }
21794
21795 /* If this is a space inside a region of text with
21796 `space-width' property, change its width. */
21797 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21798 if (stretched_p)
21799 it->pixel_width *= XFLOATINT (it->space_width);
21800
21801 /* If face has a box, add the box thickness to the character
21802 height. If character has a box line to the left and/or
21803 right, add the box line width to the character's width. */
21804 if (face->box != FACE_NO_BOX)
21805 {
21806 int thick = face->box_line_width;
21807
21808 if (thick > 0)
21809 {
21810 it->ascent += thick;
21811 it->descent += thick;
21812 }
21813 else
21814 thick = -thick;
21815
21816 if (it->start_of_box_run_p)
21817 it->pixel_width += thick;
21818 if (it->end_of_box_run_p)
21819 it->pixel_width += thick;
21820 }
21821
21822 /* If face has an overline, add the height of the overline
21823 (1 pixel) and a 1 pixel margin to the character height. */
21824 if (face->overline_p)
21825 it->ascent += overline_margin;
21826
21827 if (it->constrain_row_ascent_descent_p)
21828 {
21829 if (it->ascent > it->max_ascent)
21830 it->ascent = it->max_ascent;
21831 if (it->descent > it->max_descent)
21832 it->descent = it->max_descent;
21833 }
21834
21835 take_vertical_position_into_account (it);
21836
21837 /* If we have to actually produce glyphs, do it. */
21838 if (it->glyph_row)
21839 {
21840 if (stretched_p)
21841 {
21842 /* Translate a space with a `space-width' property
21843 into a stretch glyph. */
21844 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21845 / FONT_HEIGHT (font));
21846 append_stretch_glyph (it, it->object, it->pixel_width,
21847 it->ascent + it->descent, ascent);
21848 }
21849 else
21850 append_glyph (it);
21851
21852 /* If characters with lbearing or rbearing are displayed
21853 in this line, record that fact in a flag of the
21854 glyph row. This is used to optimize X output code. */
21855 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21856 it->glyph_row->contains_overlapping_glyphs_p = 1;
21857 }
21858 if (! stretched_p && it->pixel_width == 0)
21859 /* We assure that all visible glyphs have at least 1-pixel
21860 width. */
21861 it->pixel_width = 1;
21862 }
21863 else if (it->char_to_display == '\n')
21864 {
21865 /* A newline has no width, but we need the height of the
21866 line. But if previous part of the line sets a height,
21867 don't increase that height */
21868
21869 Lisp_Object height;
21870 Lisp_Object total_height = Qnil;
21871
21872 it->override_ascent = -1;
21873 it->pixel_width = 0;
21874 it->nglyphs = 0;
21875
21876 height = get_it_property(it, Qline_height);
21877 /* Split (line-height total-height) list */
21878 if (CONSP (height)
21879 && CONSP (XCDR (height))
21880 && NILP (XCDR (XCDR (height))))
21881 {
21882 total_height = XCAR (XCDR (height));
21883 height = XCAR (height);
21884 }
21885 height = calc_line_height_property(it, height, font, boff, 1);
21886
21887 if (it->override_ascent >= 0)
21888 {
21889 it->ascent = it->override_ascent;
21890 it->descent = it->override_descent;
21891 boff = it->override_boff;
21892 }
21893 else
21894 {
21895 it->ascent = FONT_BASE (font) + boff;
21896 it->descent = FONT_DESCENT (font) - boff;
21897 }
21898
21899 if (EQ (height, Qt))
21900 {
21901 if (it->descent > it->max_descent)
21902 {
21903 it->ascent += it->descent - it->max_descent;
21904 it->descent = it->max_descent;
21905 }
21906 if (it->ascent > it->max_ascent)
21907 {
21908 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21909 it->ascent = it->max_ascent;
21910 }
21911 it->phys_ascent = min (it->phys_ascent, it->ascent);
21912 it->phys_descent = min (it->phys_descent, it->descent);
21913 it->constrain_row_ascent_descent_p = 1;
21914 extra_line_spacing = 0;
21915 }
21916 else
21917 {
21918 Lisp_Object spacing;
21919
21920 it->phys_ascent = it->ascent;
21921 it->phys_descent = it->descent;
21922
21923 if ((it->max_ascent > 0 || it->max_descent > 0)
21924 && face->box != FACE_NO_BOX
21925 && face->box_line_width > 0)
21926 {
21927 it->ascent += face->box_line_width;
21928 it->descent += face->box_line_width;
21929 }
21930 if (!NILP (height)
21931 && XINT (height) > it->ascent + it->descent)
21932 it->ascent = XINT (height) - it->descent;
21933
21934 if (!NILP (total_height))
21935 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21936 else
21937 {
21938 spacing = get_it_property(it, Qline_spacing);
21939 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21940 }
21941 if (INTEGERP (spacing))
21942 {
21943 extra_line_spacing = XINT (spacing);
21944 if (!NILP (total_height))
21945 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21946 }
21947 }
21948 }
21949 else if (it->char_to_display == '\t')
21950 {
21951 if (font->space_width > 0)
21952 {
21953 int tab_width = it->tab_width * font->space_width;
21954 int x = it->current_x + it->continuation_lines_width;
21955 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21956
21957 /* If the distance from the current position to the next tab
21958 stop is less than a space character width, use the
21959 tab stop after that. */
21960 if (next_tab_x - x < font->space_width)
21961 next_tab_x += tab_width;
21962
21963 it->pixel_width = next_tab_x - x;
21964 it->nglyphs = 1;
21965 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21966 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21967
21968 if (it->glyph_row)
21969 {
21970 append_stretch_glyph (it, it->object, it->pixel_width,
21971 it->ascent + it->descent, it->ascent);
21972 }
21973 }
21974 else
21975 {
21976 it->pixel_width = 0;
21977 it->nglyphs = 1;
21978 }
21979 }
21980 else
21981 {
21982 /* A multi-byte character. Assume that the display width of the
21983 character is the width of the character multiplied by the
21984 width of the font. */
21985
21986 /* If we found a font, this font should give us the right
21987 metrics. If we didn't find a font, use the frame's
21988 default font and calculate the width of the character by
21989 multiplying the width of font by the width of the
21990 character. */
21991
21992 pcm = get_per_char_metric (it->f, font, &char2b);
21993
21994 if (font_not_found_p || !pcm)
21995 {
21996 int char_width = CHAR_WIDTH (it->char_to_display);
21997
21998 if (char_width == 0)
21999 /* This is a non spacing character. But, as we are
22000 going to display an empty box, the box must occupy
22001 at least one column. */
22002 char_width = 1;
22003 it->glyph_not_available_p = 1;
22004 it->pixel_width = font->space_width * char_width;
22005 it->phys_ascent = FONT_BASE (font) + boff;
22006 it->phys_descent = FONT_DESCENT (font) - boff;
22007 }
22008 else
22009 {
22010 it->pixel_width = pcm->width;
22011 it->phys_ascent = pcm->ascent + boff;
22012 it->phys_descent = pcm->descent - boff;
22013 if (it->glyph_row
22014 && (pcm->lbearing < 0
22015 || pcm->rbearing > pcm->width))
22016 it->glyph_row->contains_overlapping_glyphs_p = 1;
22017 }
22018 it->nglyphs = 1;
22019 it->ascent = FONT_BASE (font) + boff;
22020 it->descent = FONT_DESCENT (font) - boff;
22021 if (face->box != FACE_NO_BOX)
22022 {
22023 int thick = face->box_line_width;
22024
22025 if (thick > 0)
22026 {
22027 it->ascent += thick;
22028 it->descent += thick;
22029 }
22030 else
22031 thick = - thick;
22032
22033 if (it->start_of_box_run_p)
22034 it->pixel_width += thick;
22035 if (it->end_of_box_run_p)
22036 it->pixel_width += thick;
22037 }
22038
22039 /* If face has an overline, add the height of the overline
22040 (1 pixel) and a 1 pixel margin to the character height. */
22041 if (face->overline_p)
22042 it->ascent += overline_margin;
22043
22044 take_vertical_position_into_account (it);
22045
22046 if (it->ascent < 0)
22047 it->ascent = 0;
22048 if (it->descent < 0)
22049 it->descent = 0;
22050
22051 if (it->glyph_row)
22052 append_glyph (it);
22053 if (it->pixel_width == 0)
22054 /* We assure that all visible glyphs have at least 1-pixel
22055 width. */
22056 it->pixel_width = 1;
22057 }
22058 it->multibyte_p = saved_multibyte_p;
22059 }
22060 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22061 {
22062 /* A static composition.
22063
22064 Note: A composition is represented as one glyph in the
22065 glyph matrix. There are no padding glyphs.
22066
22067 Important note: pixel_width, ascent, and descent are the
22068 values of what is drawn by draw_glyphs (i.e. the values of
22069 the overall glyphs composed). */
22070 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22071 int boff; /* baseline offset */
22072 struct composition *cmp = composition_table[it->cmp_it.id];
22073 int glyph_len = cmp->glyph_len;
22074 struct font *font = face->font;
22075
22076 it->nglyphs = 1;
22077
22078 /* If we have not yet calculated pixel size data of glyphs of
22079 the composition for the current face font, calculate them
22080 now. Theoretically, we have to check all fonts for the
22081 glyphs, but that requires much time and memory space. So,
22082 here we check only the font of the first glyph. This may
22083 lead to incorrect display, but it's very rare, and C-l
22084 (recenter-top-bottom) can correct the display anyway. */
22085 if (! cmp->font || cmp->font != font)
22086 {
22087 /* Ascent and descent of the font of the first character
22088 of this composition (adjusted by baseline offset).
22089 Ascent and descent of overall glyphs should not be less
22090 than these, respectively. */
22091 int font_ascent, font_descent, font_height;
22092 /* Bounding box of the overall glyphs. */
22093 int leftmost, rightmost, lowest, highest;
22094 int lbearing, rbearing;
22095 int i, width, ascent, descent;
22096 int left_padded = 0, right_padded = 0;
22097 int c;
22098 XChar2b char2b;
22099 struct font_metrics *pcm;
22100 int font_not_found_p;
22101 int pos;
22102
22103 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22104 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22105 break;
22106 if (glyph_len < cmp->glyph_len)
22107 right_padded = 1;
22108 for (i = 0; i < glyph_len; i++)
22109 {
22110 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22111 break;
22112 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22113 }
22114 if (i > 0)
22115 left_padded = 1;
22116
22117 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22118 : IT_CHARPOS (*it));
22119 /* If no suitable font is found, use the default font. */
22120 font_not_found_p = font == NULL;
22121 if (font_not_found_p)
22122 {
22123 face = face->ascii_face;
22124 font = face->font;
22125 }
22126 boff = font->baseline_offset;
22127 if (font->vertical_centering)
22128 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22129 font_ascent = FONT_BASE (font) + boff;
22130 font_descent = FONT_DESCENT (font) - boff;
22131 font_height = FONT_HEIGHT (font);
22132
22133 cmp->font = (void *) font;
22134
22135 pcm = NULL;
22136 if (! font_not_found_p)
22137 {
22138 get_char_face_and_encoding (it->f, c, it->face_id,
22139 &char2b, it->multibyte_p, 0);
22140 pcm = get_per_char_metric (it->f, font, &char2b);
22141 }
22142
22143 /* Initialize the bounding box. */
22144 if (pcm)
22145 {
22146 width = pcm->width;
22147 ascent = pcm->ascent;
22148 descent = pcm->descent;
22149 lbearing = pcm->lbearing;
22150 rbearing = pcm->rbearing;
22151 }
22152 else
22153 {
22154 width = FONT_WIDTH (font);
22155 ascent = FONT_BASE (font);
22156 descent = FONT_DESCENT (font);
22157 lbearing = 0;
22158 rbearing = width;
22159 }
22160
22161 rightmost = width;
22162 leftmost = 0;
22163 lowest = - descent + boff;
22164 highest = ascent + boff;
22165
22166 if (! font_not_found_p
22167 && font->default_ascent
22168 && CHAR_TABLE_P (Vuse_default_ascent)
22169 && !NILP (Faref (Vuse_default_ascent,
22170 make_number (it->char_to_display))))
22171 highest = font->default_ascent + boff;
22172
22173 /* Draw the first glyph at the normal position. It may be
22174 shifted to right later if some other glyphs are drawn
22175 at the left. */
22176 cmp->offsets[i * 2] = 0;
22177 cmp->offsets[i * 2 + 1] = boff;
22178 cmp->lbearing = lbearing;
22179 cmp->rbearing = rbearing;
22180
22181 /* Set cmp->offsets for the remaining glyphs. */
22182 for (i++; i < glyph_len; i++)
22183 {
22184 int left, right, btm, top;
22185 int ch = COMPOSITION_GLYPH (cmp, i);
22186 int face_id;
22187 struct face *this_face;
22188 int this_boff;
22189
22190 if (ch == '\t')
22191 ch = ' ';
22192 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22193 this_face = FACE_FROM_ID (it->f, face_id);
22194 font = this_face->font;
22195
22196 if (font == NULL)
22197 pcm = NULL;
22198 else
22199 {
22200 this_boff = font->baseline_offset;
22201 if (font->vertical_centering)
22202 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22203 get_char_face_and_encoding (it->f, ch, face_id,
22204 &char2b, it->multibyte_p, 0);
22205 pcm = get_per_char_metric (it->f, font, &char2b);
22206 }
22207 if (! pcm)
22208 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22209 else
22210 {
22211 width = pcm->width;
22212 ascent = pcm->ascent;
22213 descent = pcm->descent;
22214 lbearing = pcm->lbearing;
22215 rbearing = pcm->rbearing;
22216 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22217 {
22218 /* Relative composition with or without
22219 alternate chars. */
22220 left = (leftmost + rightmost - width) / 2;
22221 btm = - descent + boff;
22222 if (font->relative_compose
22223 && (! CHAR_TABLE_P (Vignore_relative_composition)
22224 || NILP (Faref (Vignore_relative_composition,
22225 make_number (ch)))))
22226 {
22227
22228 if (- descent >= font->relative_compose)
22229 /* One extra pixel between two glyphs. */
22230 btm = highest + 1;
22231 else if (ascent <= 0)
22232 /* One extra pixel between two glyphs. */
22233 btm = lowest - 1 - ascent - descent;
22234 }
22235 }
22236 else
22237 {
22238 /* A composition rule is specified by an integer
22239 value that encodes global and new reference
22240 points (GREF and NREF). GREF and NREF are
22241 specified by numbers as below:
22242
22243 0---1---2 -- ascent
22244 | |
22245 | |
22246 | |
22247 9--10--11 -- center
22248 | |
22249 ---3---4---5--- baseline
22250 | |
22251 6---7---8 -- descent
22252 */
22253 int rule = COMPOSITION_RULE (cmp, i);
22254 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22255
22256 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22257 grefx = gref % 3, nrefx = nref % 3;
22258 grefy = gref / 3, nrefy = nref / 3;
22259 if (xoff)
22260 xoff = font_height * (xoff - 128) / 256;
22261 if (yoff)
22262 yoff = font_height * (yoff - 128) / 256;
22263
22264 left = (leftmost
22265 + grefx * (rightmost - leftmost) / 2
22266 - nrefx * width / 2
22267 + xoff);
22268
22269 btm = ((grefy == 0 ? highest
22270 : grefy == 1 ? 0
22271 : grefy == 2 ? lowest
22272 : (highest + lowest) / 2)
22273 - (nrefy == 0 ? ascent + descent
22274 : nrefy == 1 ? descent - boff
22275 : nrefy == 2 ? 0
22276 : (ascent + descent) / 2)
22277 + yoff);
22278 }
22279
22280 cmp->offsets[i * 2] = left;
22281 cmp->offsets[i * 2 + 1] = btm + descent;
22282
22283 /* Update the bounding box of the overall glyphs. */
22284 if (width > 0)
22285 {
22286 right = left + width;
22287 if (left < leftmost)
22288 leftmost = left;
22289 if (right > rightmost)
22290 rightmost = right;
22291 }
22292 top = btm + descent + ascent;
22293 if (top > highest)
22294 highest = top;
22295 if (btm < lowest)
22296 lowest = btm;
22297
22298 if (cmp->lbearing > left + lbearing)
22299 cmp->lbearing = left + lbearing;
22300 if (cmp->rbearing < left + rbearing)
22301 cmp->rbearing = left + rbearing;
22302 }
22303 }
22304
22305 /* If there are glyphs whose x-offsets are negative,
22306 shift all glyphs to the right and make all x-offsets
22307 non-negative. */
22308 if (leftmost < 0)
22309 {
22310 for (i = 0; i < cmp->glyph_len; i++)
22311 cmp->offsets[i * 2] -= leftmost;
22312 rightmost -= leftmost;
22313 cmp->lbearing -= leftmost;
22314 cmp->rbearing -= leftmost;
22315 }
22316
22317 if (left_padded && cmp->lbearing < 0)
22318 {
22319 for (i = 0; i < cmp->glyph_len; i++)
22320 cmp->offsets[i * 2] -= cmp->lbearing;
22321 rightmost -= cmp->lbearing;
22322 cmp->rbearing -= cmp->lbearing;
22323 cmp->lbearing = 0;
22324 }
22325 if (right_padded && rightmost < cmp->rbearing)
22326 {
22327 rightmost = cmp->rbearing;
22328 }
22329
22330 cmp->pixel_width = rightmost;
22331 cmp->ascent = highest;
22332 cmp->descent = - lowest;
22333 if (cmp->ascent < font_ascent)
22334 cmp->ascent = font_ascent;
22335 if (cmp->descent < font_descent)
22336 cmp->descent = font_descent;
22337 }
22338
22339 if (it->glyph_row
22340 && (cmp->lbearing < 0
22341 || cmp->rbearing > cmp->pixel_width))
22342 it->glyph_row->contains_overlapping_glyphs_p = 1;
22343
22344 it->pixel_width = cmp->pixel_width;
22345 it->ascent = it->phys_ascent = cmp->ascent;
22346 it->descent = it->phys_descent = cmp->descent;
22347 if (face->box != FACE_NO_BOX)
22348 {
22349 int thick = face->box_line_width;
22350
22351 if (thick > 0)
22352 {
22353 it->ascent += thick;
22354 it->descent += thick;
22355 }
22356 else
22357 thick = - thick;
22358
22359 if (it->start_of_box_run_p)
22360 it->pixel_width += thick;
22361 if (it->end_of_box_run_p)
22362 it->pixel_width += thick;
22363 }
22364
22365 /* If face has an overline, add the height of the overline
22366 (1 pixel) and a 1 pixel margin to the character height. */
22367 if (face->overline_p)
22368 it->ascent += overline_margin;
22369
22370 take_vertical_position_into_account (it);
22371 if (it->ascent < 0)
22372 it->ascent = 0;
22373 if (it->descent < 0)
22374 it->descent = 0;
22375
22376 if (it->glyph_row)
22377 append_composite_glyph (it);
22378 }
22379 else if (it->what == IT_COMPOSITION)
22380 {
22381 /* A dynamic (automatic) composition. */
22382 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22383 Lisp_Object gstring;
22384 struct font_metrics metrics;
22385
22386 gstring = composition_gstring_from_id (it->cmp_it.id);
22387 it->pixel_width
22388 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22389 &metrics);
22390 if (it->glyph_row
22391 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22392 it->glyph_row->contains_overlapping_glyphs_p = 1;
22393 it->ascent = it->phys_ascent = metrics.ascent;
22394 it->descent = it->phys_descent = metrics.descent;
22395 if (face->box != FACE_NO_BOX)
22396 {
22397 int thick = face->box_line_width;
22398
22399 if (thick > 0)
22400 {
22401 it->ascent += thick;
22402 it->descent += thick;
22403 }
22404 else
22405 thick = - thick;
22406
22407 if (it->start_of_box_run_p)
22408 it->pixel_width += thick;
22409 if (it->end_of_box_run_p)
22410 it->pixel_width += thick;
22411 }
22412 /* If face has an overline, add the height of the overline
22413 (1 pixel) and a 1 pixel margin to the character height. */
22414 if (face->overline_p)
22415 it->ascent += overline_margin;
22416 take_vertical_position_into_account (it);
22417 if (it->ascent < 0)
22418 it->ascent = 0;
22419 if (it->descent < 0)
22420 it->descent = 0;
22421
22422 if (it->glyph_row)
22423 append_composite_glyph (it);
22424 }
22425 else if (it->what == IT_IMAGE)
22426 produce_image_glyph (it);
22427 else if (it->what == IT_STRETCH)
22428 produce_stretch_glyph (it);
22429
22430 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22431 because this isn't true for images with `:ascent 100'. */
22432 xassert (it->ascent >= 0 && it->descent >= 0);
22433 if (it->area == TEXT_AREA)
22434 it->current_x += it->pixel_width;
22435
22436 if (extra_line_spacing > 0)
22437 {
22438 it->descent += extra_line_spacing;
22439 if (extra_line_spacing > it->max_extra_line_spacing)
22440 it->max_extra_line_spacing = extra_line_spacing;
22441 }
22442
22443 it->max_ascent = max (it->max_ascent, it->ascent);
22444 it->max_descent = max (it->max_descent, it->descent);
22445 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22446 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22447 }
22448
22449 /* EXPORT for RIF:
22450 Output LEN glyphs starting at START at the nominal cursor position.
22451 Advance the nominal cursor over the text. The global variable
22452 updated_window contains the window being updated, updated_row is
22453 the glyph row being updated, and updated_area is the area of that
22454 row being updated. */
22455
22456 void
22457 x_write_glyphs (start, len)
22458 struct glyph *start;
22459 int len;
22460 {
22461 int x, hpos;
22462
22463 xassert (updated_window && updated_row);
22464 BLOCK_INPUT;
22465
22466 /* Write glyphs. */
22467
22468 hpos = start - updated_row->glyphs[updated_area];
22469 x = draw_glyphs (updated_window, output_cursor.x,
22470 updated_row, updated_area,
22471 hpos, hpos + len,
22472 DRAW_NORMAL_TEXT, 0);
22473
22474 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22475 if (updated_area == TEXT_AREA
22476 && updated_window->phys_cursor_on_p
22477 && updated_window->phys_cursor.vpos == output_cursor.vpos
22478 && updated_window->phys_cursor.hpos >= hpos
22479 && updated_window->phys_cursor.hpos < hpos + len)
22480 updated_window->phys_cursor_on_p = 0;
22481
22482 UNBLOCK_INPUT;
22483
22484 /* Advance the output cursor. */
22485 output_cursor.hpos += len;
22486 output_cursor.x = x;
22487 }
22488
22489
22490 /* EXPORT for RIF:
22491 Insert LEN glyphs from START at the nominal cursor position. */
22492
22493 void
22494 x_insert_glyphs (start, len)
22495 struct glyph *start;
22496 int len;
22497 {
22498 struct frame *f;
22499 struct window *w;
22500 int line_height, shift_by_width, shifted_region_width;
22501 struct glyph_row *row;
22502 struct glyph *glyph;
22503 int frame_x, frame_y;
22504 EMACS_INT hpos;
22505
22506 xassert (updated_window && updated_row);
22507 BLOCK_INPUT;
22508 w = updated_window;
22509 f = XFRAME (WINDOW_FRAME (w));
22510
22511 /* Get the height of the line we are in. */
22512 row = updated_row;
22513 line_height = row->height;
22514
22515 /* Get the width of the glyphs to insert. */
22516 shift_by_width = 0;
22517 for (glyph = start; glyph < start + len; ++glyph)
22518 shift_by_width += glyph->pixel_width;
22519
22520 /* Get the width of the region to shift right. */
22521 shifted_region_width = (window_box_width (w, updated_area)
22522 - output_cursor.x
22523 - shift_by_width);
22524
22525 /* Shift right. */
22526 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22527 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22528
22529 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22530 line_height, shift_by_width);
22531
22532 /* Write the glyphs. */
22533 hpos = start - row->glyphs[updated_area];
22534 draw_glyphs (w, output_cursor.x, row, updated_area,
22535 hpos, hpos + len,
22536 DRAW_NORMAL_TEXT, 0);
22537
22538 /* Advance the output cursor. */
22539 output_cursor.hpos += len;
22540 output_cursor.x += shift_by_width;
22541 UNBLOCK_INPUT;
22542 }
22543
22544
22545 /* EXPORT for RIF:
22546 Erase the current text line from the nominal cursor position
22547 (inclusive) to pixel column TO_X (exclusive). The idea is that
22548 everything from TO_X onward is already erased.
22549
22550 TO_X is a pixel position relative to updated_area of
22551 updated_window. TO_X == -1 means clear to the end of this area. */
22552
22553 void
22554 x_clear_end_of_line (to_x)
22555 int to_x;
22556 {
22557 struct frame *f;
22558 struct window *w = updated_window;
22559 int max_x, min_y, max_y;
22560 int from_x, from_y, to_y;
22561
22562 xassert (updated_window && updated_row);
22563 f = XFRAME (w->frame);
22564
22565 if (updated_row->full_width_p)
22566 max_x = WINDOW_TOTAL_WIDTH (w);
22567 else
22568 max_x = window_box_width (w, updated_area);
22569 max_y = window_text_bottom_y (w);
22570
22571 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22572 of window. For TO_X > 0, truncate to end of drawing area. */
22573 if (to_x == 0)
22574 return;
22575 else if (to_x < 0)
22576 to_x = max_x;
22577 else
22578 to_x = min (to_x, max_x);
22579
22580 to_y = min (max_y, output_cursor.y + updated_row->height);
22581
22582 /* Notice if the cursor will be cleared by this operation. */
22583 if (!updated_row->full_width_p)
22584 notice_overwritten_cursor (w, updated_area,
22585 output_cursor.x, -1,
22586 updated_row->y,
22587 MATRIX_ROW_BOTTOM_Y (updated_row));
22588
22589 from_x = output_cursor.x;
22590
22591 /* Translate to frame coordinates. */
22592 if (updated_row->full_width_p)
22593 {
22594 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22595 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22596 }
22597 else
22598 {
22599 int area_left = window_box_left (w, updated_area);
22600 from_x += area_left;
22601 to_x += area_left;
22602 }
22603
22604 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22605 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22606 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22607
22608 /* Prevent inadvertently clearing to end of the X window. */
22609 if (to_x > from_x && to_y > from_y)
22610 {
22611 BLOCK_INPUT;
22612 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22613 to_x - from_x, to_y - from_y);
22614 UNBLOCK_INPUT;
22615 }
22616 }
22617
22618 #endif /* HAVE_WINDOW_SYSTEM */
22619
22620
22621 \f
22622 /***********************************************************************
22623 Cursor types
22624 ***********************************************************************/
22625
22626 /* Value is the internal representation of the specified cursor type
22627 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22628 of the bar cursor. */
22629
22630 static enum text_cursor_kinds
22631 get_specified_cursor_type (arg, width)
22632 Lisp_Object arg;
22633 int *width;
22634 {
22635 enum text_cursor_kinds type;
22636
22637 if (NILP (arg))
22638 return NO_CURSOR;
22639
22640 if (EQ (arg, Qbox))
22641 return FILLED_BOX_CURSOR;
22642
22643 if (EQ (arg, Qhollow))
22644 return HOLLOW_BOX_CURSOR;
22645
22646 if (EQ (arg, Qbar))
22647 {
22648 *width = 2;
22649 return BAR_CURSOR;
22650 }
22651
22652 if (CONSP (arg)
22653 && EQ (XCAR (arg), Qbar)
22654 && INTEGERP (XCDR (arg))
22655 && XINT (XCDR (arg)) >= 0)
22656 {
22657 *width = XINT (XCDR (arg));
22658 return BAR_CURSOR;
22659 }
22660
22661 if (EQ (arg, Qhbar))
22662 {
22663 *width = 2;
22664 return HBAR_CURSOR;
22665 }
22666
22667 if (CONSP (arg)
22668 && EQ (XCAR (arg), Qhbar)
22669 && INTEGERP (XCDR (arg))
22670 && XINT (XCDR (arg)) >= 0)
22671 {
22672 *width = XINT (XCDR (arg));
22673 return HBAR_CURSOR;
22674 }
22675
22676 /* Treat anything unknown as "hollow box cursor".
22677 It was bad to signal an error; people have trouble fixing
22678 .Xdefaults with Emacs, when it has something bad in it. */
22679 type = HOLLOW_BOX_CURSOR;
22680
22681 return type;
22682 }
22683
22684 /* Set the default cursor types for specified frame. */
22685 void
22686 set_frame_cursor_types (f, arg)
22687 struct frame *f;
22688 Lisp_Object arg;
22689 {
22690 int width;
22691 Lisp_Object tem;
22692
22693 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22694 FRAME_CURSOR_WIDTH (f) = width;
22695
22696 /* By default, set up the blink-off state depending on the on-state. */
22697
22698 tem = Fassoc (arg, Vblink_cursor_alist);
22699 if (!NILP (tem))
22700 {
22701 FRAME_BLINK_OFF_CURSOR (f)
22702 = get_specified_cursor_type (XCDR (tem), &width);
22703 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22704 }
22705 else
22706 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22707 }
22708
22709
22710 /* Return the cursor we want to be displayed in window W. Return
22711 width of bar/hbar cursor through WIDTH arg. Return with
22712 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22713 (i.e. if the `system caret' should track this cursor).
22714
22715 In a mini-buffer window, we want the cursor only to appear if we
22716 are reading input from this window. For the selected window, we
22717 want the cursor type given by the frame parameter or buffer local
22718 setting of cursor-type. If explicitly marked off, draw no cursor.
22719 In all other cases, we want a hollow box cursor. */
22720
22721 static enum text_cursor_kinds
22722 get_window_cursor_type (w, glyph, width, active_cursor)
22723 struct window *w;
22724 struct glyph *glyph;
22725 int *width;
22726 int *active_cursor;
22727 {
22728 struct frame *f = XFRAME (w->frame);
22729 struct buffer *b = XBUFFER (w->buffer);
22730 int cursor_type = DEFAULT_CURSOR;
22731 Lisp_Object alt_cursor;
22732 int non_selected = 0;
22733
22734 *active_cursor = 1;
22735
22736 /* Echo area */
22737 if (cursor_in_echo_area
22738 && FRAME_HAS_MINIBUF_P (f)
22739 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22740 {
22741 if (w == XWINDOW (echo_area_window))
22742 {
22743 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22744 {
22745 *width = FRAME_CURSOR_WIDTH (f);
22746 return FRAME_DESIRED_CURSOR (f);
22747 }
22748 else
22749 return get_specified_cursor_type (b->cursor_type, width);
22750 }
22751
22752 *active_cursor = 0;
22753 non_selected = 1;
22754 }
22755
22756 /* Detect a nonselected window or nonselected frame. */
22757 else if (w != XWINDOW (f->selected_window)
22758 #ifdef HAVE_WINDOW_SYSTEM
22759 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22760 #endif
22761 )
22762 {
22763 *active_cursor = 0;
22764
22765 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22766 return NO_CURSOR;
22767
22768 non_selected = 1;
22769 }
22770
22771 /* Never display a cursor in a window in which cursor-type is nil. */
22772 if (NILP (b->cursor_type))
22773 return NO_CURSOR;
22774
22775 /* Get the normal cursor type for this window. */
22776 if (EQ (b->cursor_type, Qt))
22777 {
22778 cursor_type = FRAME_DESIRED_CURSOR (f);
22779 *width = FRAME_CURSOR_WIDTH (f);
22780 }
22781 else
22782 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22783
22784 /* Use cursor-in-non-selected-windows instead
22785 for non-selected window or frame. */
22786 if (non_selected)
22787 {
22788 alt_cursor = b->cursor_in_non_selected_windows;
22789 if (!EQ (Qt, alt_cursor))
22790 return get_specified_cursor_type (alt_cursor, width);
22791 /* t means modify the normal cursor type. */
22792 if (cursor_type == FILLED_BOX_CURSOR)
22793 cursor_type = HOLLOW_BOX_CURSOR;
22794 else if (cursor_type == BAR_CURSOR && *width > 1)
22795 --*width;
22796 return cursor_type;
22797 }
22798
22799 /* Use normal cursor if not blinked off. */
22800 if (!w->cursor_off_p)
22801 {
22802 #ifdef HAVE_WINDOW_SYSTEM
22803 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22804 {
22805 if (cursor_type == FILLED_BOX_CURSOR)
22806 {
22807 /* Using a block cursor on large images can be very annoying.
22808 So use a hollow cursor for "large" images.
22809 If image is not transparent (no mask), also use hollow cursor. */
22810 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22811 if (img != NULL && IMAGEP (img->spec))
22812 {
22813 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22814 where N = size of default frame font size.
22815 This should cover most of the "tiny" icons people may use. */
22816 if (!img->mask
22817 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22818 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22819 cursor_type = HOLLOW_BOX_CURSOR;
22820 }
22821 }
22822 else if (cursor_type != NO_CURSOR)
22823 {
22824 /* Display current only supports BOX and HOLLOW cursors for images.
22825 So for now, unconditionally use a HOLLOW cursor when cursor is
22826 not a solid box cursor. */
22827 cursor_type = HOLLOW_BOX_CURSOR;
22828 }
22829 }
22830 #endif
22831 return cursor_type;
22832 }
22833
22834 /* Cursor is blinked off, so determine how to "toggle" it. */
22835
22836 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22837 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22838 return get_specified_cursor_type (XCDR (alt_cursor), width);
22839
22840 /* Then see if frame has specified a specific blink off cursor type. */
22841 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22842 {
22843 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22844 return FRAME_BLINK_OFF_CURSOR (f);
22845 }
22846
22847 #if 0
22848 /* Some people liked having a permanently visible blinking cursor,
22849 while others had very strong opinions against it. So it was
22850 decided to remove it. KFS 2003-09-03 */
22851
22852 /* Finally perform built-in cursor blinking:
22853 filled box <-> hollow box
22854 wide [h]bar <-> narrow [h]bar
22855 narrow [h]bar <-> no cursor
22856 other type <-> no cursor */
22857
22858 if (cursor_type == FILLED_BOX_CURSOR)
22859 return HOLLOW_BOX_CURSOR;
22860
22861 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22862 {
22863 *width = 1;
22864 return cursor_type;
22865 }
22866 #endif
22867
22868 return NO_CURSOR;
22869 }
22870
22871
22872 #ifdef HAVE_WINDOW_SYSTEM
22873
22874 /* Notice when the text cursor of window W has been completely
22875 overwritten by a drawing operation that outputs glyphs in AREA
22876 starting at X0 and ending at X1 in the line starting at Y0 and
22877 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22878 the rest of the line after X0 has been written. Y coordinates
22879 are window-relative. */
22880
22881 static void
22882 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22883 struct window *w;
22884 enum glyph_row_area area;
22885 int x0, y0, x1, y1;
22886 {
22887 int cx0, cx1, cy0, cy1;
22888 struct glyph_row *row;
22889
22890 if (!w->phys_cursor_on_p)
22891 return;
22892 if (area != TEXT_AREA)
22893 return;
22894
22895 if (w->phys_cursor.vpos < 0
22896 || w->phys_cursor.vpos >= w->current_matrix->nrows
22897 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22898 !(row->enabled_p && row->displays_text_p)))
22899 return;
22900
22901 if (row->cursor_in_fringe_p)
22902 {
22903 row->cursor_in_fringe_p = 0;
22904 draw_fringe_bitmap (w, row, 0);
22905 w->phys_cursor_on_p = 0;
22906 return;
22907 }
22908
22909 cx0 = w->phys_cursor.x;
22910 cx1 = cx0 + w->phys_cursor_width;
22911 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22912 return;
22913
22914 /* The cursor image will be completely removed from the
22915 screen if the output area intersects the cursor area in
22916 y-direction. When we draw in [y0 y1[, and some part of
22917 the cursor is at y < y0, that part must have been drawn
22918 before. When scrolling, the cursor is erased before
22919 actually scrolling, so we don't come here. When not
22920 scrolling, the rows above the old cursor row must have
22921 changed, and in this case these rows must have written
22922 over the cursor image.
22923
22924 Likewise if part of the cursor is below y1, with the
22925 exception of the cursor being in the first blank row at
22926 the buffer and window end because update_text_area
22927 doesn't draw that row. (Except when it does, but
22928 that's handled in update_text_area.) */
22929
22930 cy0 = w->phys_cursor.y;
22931 cy1 = cy0 + w->phys_cursor_height;
22932 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22933 return;
22934
22935 w->phys_cursor_on_p = 0;
22936 }
22937
22938 #endif /* HAVE_WINDOW_SYSTEM */
22939
22940 \f
22941 /************************************************************************
22942 Mouse Face
22943 ************************************************************************/
22944
22945 #ifdef HAVE_WINDOW_SYSTEM
22946
22947 /* EXPORT for RIF:
22948 Fix the display of area AREA of overlapping row ROW in window W
22949 with respect to the overlapping part OVERLAPS. */
22950
22951 void
22952 x_fix_overlapping_area (w, row, area, overlaps)
22953 struct window *w;
22954 struct glyph_row *row;
22955 enum glyph_row_area area;
22956 int overlaps;
22957 {
22958 int i, x;
22959
22960 BLOCK_INPUT;
22961
22962 x = 0;
22963 for (i = 0; i < row->used[area];)
22964 {
22965 if (row->glyphs[area][i].overlaps_vertically_p)
22966 {
22967 int start = i, start_x = x;
22968
22969 do
22970 {
22971 x += row->glyphs[area][i].pixel_width;
22972 ++i;
22973 }
22974 while (i < row->used[area]
22975 && row->glyphs[area][i].overlaps_vertically_p);
22976
22977 draw_glyphs (w, start_x, row, area,
22978 start, i,
22979 DRAW_NORMAL_TEXT, overlaps);
22980 }
22981 else
22982 {
22983 x += row->glyphs[area][i].pixel_width;
22984 ++i;
22985 }
22986 }
22987
22988 UNBLOCK_INPUT;
22989 }
22990
22991
22992 /* EXPORT:
22993 Draw the cursor glyph of window W in glyph row ROW. See the
22994 comment of draw_glyphs for the meaning of HL. */
22995
22996 void
22997 draw_phys_cursor_glyph (w, row, hl)
22998 struct window *w;
22999 struct glyph_row *row;
23000 enum draw_glyphs_face hl;
23001 {
23002 /* If cursor hpos is out of bounds, don't draw garbage. This can
23003 happen in mini-buffer windows when switching between echo area
23004 glyphs and mini-buffer. */
23005 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
23006 {
23007 int on_p = w->phys_cursor_on_p;
23008 int x1;
23009 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23010 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23011 hl, 0);
23012 w->phys_cursor_on_p = on_p;
23013
23014 if (hl == DRAW_CURSOR)
23015 w->phys_cursor_width = x1 - w->phys_cursor.x;
23016 /* When we erase the cursor, and ROW is overlapped by other
23017 rows, make sure that these overlapping parts of other rows
23018 are redrawn. */
23019 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23020 {
23021 w->phys_cursor_width = x1 - w->phys_cursor.x;
23022
23023 if (row > w->current_matrix->rows
23024 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23025 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23026 OVERLAPS_ERASED_CURSOR);
23027
23028 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23029 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23030 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23031 OVERLAPS_ERASED_CURSOR);
23032 }
23033 }
23034 }
23035
23036
23037 /* EXPORT:
23038 Erase the image of a cursor of window W from the screen. */
23039
23040 void
23041 erase_phys_cursor (w)
23042 struct window *w;
23043 {
23044 struct frame *f = XFRAME (w->frame);
23045 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23046 int hpos = w->phys_cursor.hpos;
23047 int vpos = w->phys_cursor.vpos;
23048 int mouse_face_here_p = 0;
23049 struct glyph_matrix *active_glyphs = w->current_matrix;
23050 struct glyph_row *cursor_row;
23051 struct glyph *cursor_glyph;
23052 enum draw_glyphs_face hl;
23053
23054 /* No cursor displayed or row invalidated => nothing to do on the
23055 screen. */
23056 if (w->phys_cursor_type == NO_CURSOR)
23057 goto mark_cursor_off;
23058
23059 /* VPOS >= active_glyphs->nrows means that window has been resized.
23060 Don't bother to erase the cursor. */
23061 if (vpos >= active_glyphs->nrows)
23062 goto mark_cursor_off;
23063
23064 /* If row containing cursor is marked invalid, there is nothing we
23065 can do. */
23066 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23067 if (!cursor_row->enabled_p)
23068 goto mark_cursor_off;
23069
23070 /* If line spacing is > 0, old cursor may only be partially visible in
23071 window after split-window. So adjust visible height. */
23072 cursor_row->visible_height = min (cursor_row->visible_height,
23073 window_text_bottom_y (w) - cursor_row->y);
23074
23075 /* If row is completely invisible, don't attempt to delete a cursor which
23076 isn't there. This can happen if cursor is at top of a window, and
23077 we switch to a buffer with a header line in that window. */
23078 if (cursor_row->visible_height <= 0)
23079 goto mark_cursor_off;
23080
23081 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23082 if (cursor_row->cursor_in_fringe_p)
23083 {
23084 cursor_row->cursor_in_fringe_p = 0;
23085 draw_fringe_bitmap (w, cursor_row, 0);
23086 goto mark_cursor_off;
23087 }
23088
23089 /* This can happen when the new row is shorter than the old one.
23090 In this case, either draw_glyphs or clear_end_of_line
23091 should have cleared the cursor. Note that we wouldn't be
23092 able to erase the cursor in this case because we don't have a
23093 cursor glyph at hand. */
23094 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
23095 goto mark_cursor_off;
23096
23097 /* If the cursor is in the mouse face area, redisplay that when
23098 we clear the cursor. */
23099 if (! NILP (dpyinfo->mouse_face_window)
23100 && w == XWINDOW (dpyinfo->mouse_face_window)
23101 && (vpos > dpyinfo->mouse_face_beg_row
23102 || (vpos == dpyinfo->mouse_face_beg_row
23103 && hpos >= dpyinfo->mouse_face_beg_col))
23104 && (vpos < dpyinfo->mouse_face_end_row
23105 || (vpos == dpyinfo->mouse_face_end_row
23106 && hpos < dpyinfo->mouse_face_end_col))
23107 /* Don't redraw the cursor's spot in mouse face if it is at the
23108 end of a line (on a newline). The cursor appears there, but
23109 mouse highlighting does not. */
23110 && cursor_row->used[TEXT_AREA] > hpos)
23111 mouse_face_here_p = 1;
23112
23113 /* Maybe clear the display under the cursor. */
23114 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23115 {
23116 int x, y, left_x;
23117 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23118 int width;
23119
23120 cursor_glyph = get_phys_cursor_glyph (w);
23121 if (cursor_glyph == NULL)
23122 goto mark_cursor_off;
23123
23124 width = cursor_glyph->pixel_width;
23125 left_x = window_box_left_offset (w, TEXT_AREA);
23126 x = w->phys_cursor.x;
23127 if (x < left_x)
23128 width -= left_x - x;
23129 width = min (width, window_box_width (w, TEXT_AREA) - x);
23130 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23131 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23132
23133 if (width > 0)
23134 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23135 }
23136
23137 /* Erase the cursor by redrawing the character underneath it. */
23138 if (mouse_face_here_p)
23139 hl = DRAW_MOUSE_FACE;
23140 else
23141 hl = DRAW_NORMAL_TEXT;
23142 draw_phys_cursor_glyph (w, cursor_row, hl);
23143
23144 mark_cursor_off:
23145 w->phys_cursor_on_p = 0;
23146 w->phys_cursor_type = NO_CURSOR;
23147 }
23148
23149
23150 /* EXPORT:
23151 Display or clear cursor of window W. If ON is zero, clear the
23152 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23153 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23154
23155 void
23156 display_and_set_cursor (w, on, hpos, vpos, x, y)
23157 struct window *w;
23158 int on, hpos, vpos, x, y;
23159 {
23160 struct frame *f = XFRAME (w->frame);
23161 int new_cursor_type;
23162 int new_cursor_width;
23163 int active_cursor;
23164 struct glyph_row *glyph_row;
23165 struct glyph *glyph;
23166
23167 /* This is pointless on invisible frames, and dangerous on garbaged
23168 windows and frames; in the latter case, the frame or window may
23169 be in the midst of changing its size, and x and y may be off the
23170 window. */
23171 if (! FRAME_VISIBLE_P (f)
23172 || FRAME_GARBAGED_P (f)
23173 || vpos >= w->current_matrix->nrows
23174 || hpos >= w->current_matrix->matrix_w)
23175 return;
23176
23177 /* If cursor is off and we want it off, return quickly. */
23178 if (!on && !w->phys_cursor_on_p)
23179 return;
23180
23181 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23182 /* If cursor row is not enabled, we don't really know where to
23183 display the cursor. */
23184 if (!glyph_row->enabled_p)
23185 {
23186 w->phys_cursor_on_p = 0;
23187 return;
23188 }
23189
23190 glyph = NULL;
23191 if (!glyph_row->exact_window_width_line_p
23192 || hpos < glyph_row->used[TEXT_AREA])
23193 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23194
23195 xassert (interrupt_input_blocked);
23196
23197 /* Set new_cursor_type to the cursor we want to be displayed. */
23198 new_cursor_type = get_window_cursor_type (w, glyph,
23199 &new_cursor_width, &active_cursor);
23200
23201 /* If cursor is currently being shown and we don't want it to be or
23202 it is in the wrong place, or the cursor type is not what we want,
23203 erase it. */
23204 if (w->phys_cursor_on_p
23205 && (!on
23206 || w->phys_cursor.x != x
23207 || w->phys_cursor.y != y
23208 || new_cursor_type != w->phys_cursor_type
23209 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23210 && new_cursor_width != w->phys_cursor_width)))
23211 erase_phys_cursor (w);
23212
23213 /* Don't check phys_cursor_on_p here because that flag is only set
23214 to zero in some cases where we know that the cursor has been
23215 completely erased, to avoid the extra work of erasing the cursor
23216 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23217 still not be visible, or it has only been partly erased. */
23218 if (on)
23219 {
23220 w->phys_cursor_ascent = glyph_row->ascent;
23221 w->phys_cursor_height = glyph_row->height;
23222
23223 /* Set phys_cursor_.* before x_draw_.* is called because some
23224 of them may need the information. */
23225 w->phys_cursor.x = x;
23226 w->phys_cursor.y = glyph_row->y;
23227 w->phys_cursor.hpos = hpos;
23228 w->phys_cursor.vpos = vpos;
23229 }
23230
23231 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23232 new_cursor_type, new_cursor_width,
23233 on, active_cursor);
23234 }
23235
23236
23237 /* Switch the display of W's cursor on or off, according to the value
23238 of ON. */
23239
23240 #ifndef HAVE_NS
23241 static
23242 #endif
23243 void
23244 update_window_cursor (w, on)
23245 struct window *w;
23246 int on;
23247 {
23248 /* Don't update cursor in windows whose frame is in the process
23249 of being deleted. */
23250 if (w->current_matrix)
23251 {
23252 BLOCK_INPUT;
23253 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23254 w->phys_cursor.x, w->phys_cursor.y);
23255 UNBLOCK_INPUT;
23256 }
23257 }
23258
23259
23260 /* Call update_window_cursor with parameter ON_P on all leaf windows
23261 in the window tree rooted at W. */
23262
23263 static void
23264 update_cursor_in_window_tree (w, on_p)
23265 struct window *w;
23266 int on_p;
23267 {
23268 while (w)
23269 {
23270 if (!NILP (w->hchild))
23271 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23272 else if (!NILP (w->vchild))
23273 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23274 else
23275 update_window_cursor (w, on_p);
23276
23277 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23278 }
23279 }
23280
23281
23282 /* EXPORT:
23283 Display the cursor on window W, or clear it, according to ON_P.
23284 Don't change the cursor's position. */
23285
23286 void
23287 x_update_cursor (f, on_p)
23288 struct frame *f;
23289 int on_p;
23290 {
23291 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23292 }
23293
23294
23295 /* EXPORT:
23296 Clear the cursor of window W to background color, and mark the
23297 cursor as not shown. This is used when the text where the cursor
23298 is about to be rewritten. */
23299
23300 void
23301 x_clear_cursor (w)
23302 struct window *w;
23303 {
23304 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23305 update_window_cursor (w, 0);
23306 }
23307
23308
23309 /* EXPORT:
23310 Display the active region described by mouse_face_* according to DRAW. */
23311
23312 void
23313 show_mouse_face (dpyinfo, draw)
23314 Display_Info *dpyinfo;
23315 enum draw_glyphs_face draw;
23316 {
23317 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23318 struct frame *f = XFRAME (WINDOW_FRAME (w));
23319
23320 if (/* If window is in the process of being destroyed, don't bother
23321 to do anything. */
23322 w->current_matrix != NULL
23323 /* Don't update mouse highlight if hidden */
23324 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23325 /* Recognize when we are called to operate on rows that don't exist
23326 anymore. This can happen when a window is split. */
23327 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23328 {
23329 int phys_cursor_on_p = w->phys_cursor_on_p;
23330 struct glyph_row *row, *first, *last;
23331
23332 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23333 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23334
23335 for (row = first; row <= last && row->enabled_p; ++row)
23336 {
23337 int start_hpos, end_hpos, start_x;
23338
23339 /* For all but the first row, the highlight starts at column 0. */
23340 if (row == first)
23341 {
23342 start_hpos = dpyinfo->mouse_face_beg_col;
23343 start_x = dpyinfo->mouse_face_beg_x;
23344 }
23345 else
23346 {
23347 start_hpos = 0;
23348 start_x = 0;
23349 }
23350
23351 if (row == last)
23352 end_hpos = dpyinfo->mouse_face_end_col;
23353 else
23354 {
23355 end_hpos = row->used[TEXT_AREA];
23356 if (draw == DRAW_NORMAL_TEXT)
23357 row->fill_line_p = 1; /* Clear to end of line */
23358 }
23359
23360 if (end_hpos > start_hpos)
23361 {
23362 draw_glyphs (w, start_x, row, TEXT_AREA,
23363 start_hpos, end_hpos,
23364 draw, 0);
23365
23366 row->mouse_face_p
23367 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23368 }
23369 }
23370
23371 /* When we've written over the cursor, arrange for it to
23372 be displayed again. */
23373 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23374 {
23375 BLOCK_INPUT;
23376 display_and_set_cursor (w, 1,
23377 w->phys_cursor.hpos, w->phys_cursor.vpos,
23378 w->phys_cursor.x, w->phys_cursor.y);
23379 UNBLOCK_INPUT;
23380 }
23381 }
23382
23383 /* Change the mouse cursor. */
23384 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23385 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23386 else if (draw == DRAW_MOUSE_FACE)
23387 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23388 else
23389 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23390 }
23391
23392 /* EXPORT:
23393 Clear out the mouse-highlighted active region.
23394 Redraw it un-highlighted first. Value is non-zero if mouse
23395 face was actually drawn unhighlighted. */
23396
23397 int
23398 clear_mouse_face (dpyinfo)
23399 Display_Info *dpyinfo;
23400 {
23401 int cleared = 0;
23402
23403 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23404 {
23405 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23406 cleared = 1;
23407 }
23408
23409 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23410 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23411 dpyinfo->mouse_face_window = Qnil;
23412 dpyinfo->mouse_face_overlay = Qnil;
23413 return cleared;
23414 }
23415
23416
23417 /* EXPORT:
23418 Non-zero if physical cursor of window W is within mouse face. */
23419
23420 int
23421 cursor_in_mouse_face_p (w)
23422 struct window *w;
23423 {
23424 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23425 int in_mouse_face = 0;
23426
23427 if (WINDOWP (dpyinfo->mouse_face_window)
23428 && XWINDOW (dpyinfo->mouse_face_window) == w)
23429 {
23430 int hpos = w->phys_cursor.hpos;
23431 int vpos = w->phys_cursor.vpos;
23432
23433 if (vpos >= dpyinfo->mouse_face_beg_row
23434 && vpos <= dpyinfo->mouse_face_end_row
23435 && (vpos > dpyinfo->mouse_face_beg_row
23436 || hpos >= dpyinfo->mouse_face_beg_col)
23437 && (vpos < dpyinfo->mouse_face_end_row
23438 || hpos < dpyinfo->mouse_face_end_col
23439 || dpyinfo->mouse_face_past_end))
23440 in_mouse_face = 1;
23441 }
23442
23443 return in_mouse_face;
23444 }
23445
23446
23447
23448 \f
23449 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23450 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23451 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23452 for the overlay or run of text properties specifying the mouse
23453 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23454 before-string and after-string that must also be highlighted.
23455 DISPLAY_STRING, if non-nil, is a display string that may cover some
23456 or all of the highlighted text. */
23457
23458 static void
23459 mouse_face_from_buffer_pos (Lisp_Object window,
23460 Display_Info *dpyinfo,
23461 EMACS_INT mouse_charpos,
23462 EMACS_INT start_charpos,
23463 EMACS_INT end_charpos,
23464 Lisp_Object before_string,
23465 Lisp_Object after_string,
23466 Lisp_Object display_string)
23467 {
23468 struct window *w = XWINDOW (window);
23469 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23470 struct glyph_row *row;
23471 struct glyph *glyph, *end;
23472 EMACS_INT ignore;
23473 int x;
23474
23475 xassert (NILP (display_string) || STRINGP (display_string));
23476 xassert (NILP (before_string) || STRINGP (before_string));
23477 xassert (NILP (after_string) || STRINGP (after_string));
23478
23479 /* Find the first highlighted glyph. */
23480 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
23481 {
23482 dpyinfo->mouse_face_beg_col = 0;
23483 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23484 dpyinfo->mouse_face_beg_x = first->x;
23485 dpyinfo->mouse_face_beg_y = first->y;
23486 }
23487 else
23488 {
23489 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23490 if (row == NULL)
23491 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23492
23493 /* If the before-string or display-string contains newlines,
23494 row_containing_pos skips to its last row. Move back. */
23495 if (!NILP (before_string) || !NILP (display_string))
23496 {
23497 struct glyph_row *prev;
23498 while ((prev = row - 1, prev >= first)
23499 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23500 && prev->used[TEXT_AREA] > 0)
23501 {
23502 struct glyph *beg = prev->glyphs[TEXT_AREA];
23503 glyph = beg + prev->used[TEXT_AREA];
23504 while (--glyph >= beg && INTEGERP (glyph->object));
23505 if (glyph < beg
23506 || !(EQ (glyph->object, before_string)
23507 || EQ (glyph->object, display_string)))
23508 break;
23509 row = prev;
23510 }
23511 }
23512
23513 glyph = row->glyphs[TEXT_AREA];
23514 end = glyph + row->used[TEXT_AREA];
23515 x = row->x;
23516 dpyinfo->mouse_face_beg_y = row->y;
23517 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23518
23519 /* Skip truncation glyphs at the start of the glyph row. */
23520 if (row->displays_text_p)
23521 for (; glyph < end
23522 && INTEGERP (glyph->object)
23523 && glyph->charpos < 0;
23524 ++glyph)
23525 x += glyph->pixel_width;
23526
23527 /* Scan the glyph row, stopping before BEFORE_STRING or
23528 DISPLAY_STRING or START_CHARPOS. */
23529 for (; glyph < end
23530 && !INTEGERP (glyph->object)
23531 && !EQ (glyph->object, before_string)
23532 && !EQ (glyph->object, display_string)
23533 && !(BUFFERP (glyph->object)
23534 && glyph->charpos >= start_charpos);
23535 ++glyph)
23536 x += glyph->pixel_width;
23537
23538 dpyinfo->mouse_face_beg_x = x;
23539 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23540 }
23541
23542 /* Find the last highlighted glyph. */
23543 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23544 if (row == NULL)
23545 {
23546 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23547 dpyinfo->mouse_face_past_end = 1;
23548 }
23549 else if (!NILP (after_string))
23550 {
23551 /* If the after-string has newlines, advance to its last row. */
23552 struct glyph_row *next;
23553 struct glyph_row *last
23554 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23555
23556 for (next = row + 1;
23557 next <= last
23558 && next->used[TEXT_AREA] > 0
23559 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23560 ++next)
23561 row = next;
23562 }
23563
23564 glyph = row->glyphs[TEXT_AREA];
23565 end = glyph + row->used[TEXT_AREA];
23566 x = row->x;
23567 dpyinfo->mouse_face_end_y = row->y;
23568 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23569
23570 /* Skip truncation glyphs at the start of the row. */
23571 if (row->displays_text_p)
23572 for (; glyph < end
23573 && INTEGERP (glyph->object)
23574 && glyph->charpos < 0;
23575 ++glyph)
23576 x += glyph->pixel_width;
23577
23578 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23579 AFTER_STRING. */
23580 for (; glyph < end
23581 && !INTEGERP (glyph->object)
23582 && !EQ (glyph->object, after_string)
23583 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23584 ++glyph)
23585 x += glyph->pixel_width;
23586
23587 /* If we found AFTER_STRING, consume it and stop. */
23588 if (EQ (glyph->object, after_string))
23589 {
23590 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23591 x += glyph->pixel_width;
23592 }
23593 else
23594 {
23595 /* If there's no after-string, we must check if we overshot,
23596 which might be the case if we stopped after a string glyph.
23597 That glyph may belong to a before-string or display-string
23598 associated with the end position, which must not be
23599 highlighted. */
23600 Lisp_Object prev_object;
23601 EMACS_INT pos;
23602
23603 while (glyph > row->glyphs[TEXT_AREA])
23604 {
23605 prev_object = (glyph - 1)->object;
23606 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23607 break;
23608
23609 pos = string_buffer_position (w, prev_object, end_charpos);
23610 if (pos && pos < end_charpos)
23611 break;
23612
23613 for (; glyph > row->glyphs[TEXT_AREA]
23614 && EQ ((glyph - 1)->object, prev_object);
23615 --glyph)
23616 x -= (glyph - 1)->pixel_width;
23617 }
23618 }
23619
23620 dpyinfo->mouse_face_end_x = x;
23621 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23622 dpyinfo->mouse_face_window = window;
23623 dpyinfo->mouse_face_face_id
23624 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23625 mouse_charpos + 1,
23626 !dpyinfo->mouse_face_hidden, -1);
23627 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23628 }
23629
23630
23631 /* Find the position of the glyph for position POS in OBJECT in
23632 window W's current matrix, and return in *X, *Y the pixel
23633 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23634
23635 RIGHT_P non-zero means return the position of the right edge of the
23636 glyph, RIGHT_P zero means return the left edge position.
23637
23638 If no glyph for POS exists in the matrix, return the position of
23639 the glyph with the next smaller position that is in the matrix, if
23640 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23641 exists in the matrix, return the position of the glyph with the
23642 next larger position in OBJECT.
23643
23644 Value is non-zero if a glyph was found. */
23645
23646 static int
23647 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23648 struct window *w;
23649 EMACS_INT pos;
23650 Lisp_Object object;
23651 int *hpos, *vpos, *x, *y;
23652 int right_p;
23653 {
23654 int yb = window_text_bottom_y (w);
23655 struct glyph_row *r;
23656 struct glyph *best_glyph = NULL;
23657 struct glyph_row *best_row = NULL;
23658 int best_x = 0;
23659
23660 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23661 r->enabled_p && r->y < yb;
23662 ++r)
23663 {
23664 struct glyph *g = r->glyphs[TEXT_AREA];
23665 struct glyph *e = g + r->used[TEXT_AREA];
23666 int gx;
23667
23668 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23669 if (EQ (g->object, object))
23670 {
23671 if (g->charpos == pos)
23672 {
23673 best_glyph = g;
23674 best_x = gx;
23675 best_row = r;
23676 goto found;
23677 }
23678 else if (best_glyph == NULL
23679 || ((eabs (g->charpos - pos)
23680 < eabs (best_glyph->charpos - pos))
23681 && (right_p
23682 ? g->charpos < pos
23683 : g->charpos > pos)))
23684 {
23685 best_glyph = g;
23686 best_x = gx;
23687 best_row = r;
23688 }
23689 }
23690 }
23691
23692 found:
23693
23694 if (best_glyph)
23695 {
23696 *x = best_x;
23697 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23698
23699 if (right_p)
23700 {
23701 *x += best_glyph->pixel_width;
23702 ++*hpos;
23703 }
23704
23705 *y = best_row->y;
23706 *vpos = best_row - w->current_matrix->rows;
23707 }
23708
23709 return best_glyph != NULL;
23710 }
23711
23712
23713 /* See if position X, Y is within a hot-spot of an image. */
23714
23715 static int
23716 on_hot_spot_p (hot_spot, x, y)
23717 Lisp_Object hot_spot;
23718 int x, y;
23719 {
23720 if (!CONSP (hot_spot))
23721 return 0;
23722
23723 if (EQ (XCAR (hot_spot), Qrect))
23724 {
23725 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23726 Lisp_Object rect = XCDR (hot_spot);
23727 Lisp_Object tem;
23728 if (!CONSP (rect))
23729 return 0;
23730 if (!CONSP (XCAR (rect)))
23731 return 0;
23732 if (!CONSP (XCDR (rect)))
23733 return 0;
23734 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23735 return 0;
23736 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23737 return 0;
23738 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23739 return 0;
23740 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23741 return 0;
23742 return 1;
23743 }
23744 else if (EQ (XCAR (hot_spot), Qcircle))
23745 {
23746 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23747 Lisp_Object circ = XCDR (hot_spot);
23748 Lisp_Object lr, lx0, ly0;
23749 if (CONSP (circ)
23750 && CONSP (XCAR (circ))
23751 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23752 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23753 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23754 {
23755 double r = XFLOATINT (lr);
23756 double dx = XINT (lx0) - x;
23757 double dy = XINT (ly0) - y;
23758 return (dx * dx + dy * dy <= r * r);
23759 }
23760 }
23761 else if (EQ (XCAR (hot_spot), Qpoly))
23762 {
23763 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23764 if (VECTORP (XCDR (hot_spot)))
23765 {
23766 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23767 Lisp_Object *poly = v->contents;
23768 int n = v->size;
23769 int i;
23770 int inside = 0;
23771 Lisp_Object lx, ly;
23772 int x0, y0;
23773
23774 /* Need an even number of coordinates, and at least 3 edges. */
23775 if (n < 6 || n & 1)
23776 return 0;
23777
23778 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23779 If count is odd, we are inside polygon. Pixels on edges
23780 may or may not be included depending on actual geometry of the
23781 polygon. */
23782 if ((lx = poly[n-2], !INTEGERP (lx))
23783 || (ly = poly[n-1], !INTEGERP (lx)))
23784 return 0;
23785 x0 = XINT (lx), y0 = XINT (ly);
23786 for (i = 0; i < n; i += 2)
23787 {
23788 int x1 = x0, y1 = y0;
23789 if ((lx = poly[i], !INTEGERP (lx))
23790 || (ly = poly[i+1], !INTEGERP (ly)))
23791 return 0;
23792 x0 = XINT (lx), y0 = XINT (ly);
23793
23794 /* Does this segment cross the X line? */
23795 if (x0 >= x)
23796 {
23797 if (x1 >= x)
23798 continue;
23799 }
23800 else if (x1 < x)
23801 continue;
23802 if (y > y0 && y > y1)
23803 continue;
23804 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23805 inside = !inside;
23806 }
23807 return inside;
23808 }
23809 }
23810 return 0;
23811 }
23812
23813 Lisp_Object
23814 find_hot_spot (map, x, y)
23815 Lisp_Object map;
23816 int x, y;
23817 {
23818 while (CONSP (map))
23819 {
23820 if (CONSP (XCAR (map))
23821 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23822 return XCAR (map);
23823 map = XCDR (map);
23824 }
23825
23826 return Qnil;
23827 }
23828
23829 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23830 3, 3, 0,
23831 doc: /* Lookup in image map MAP coordinates X and Y.
23832 An image map is an alist where each element has the format (AREA ID PLIST).
23833 An AREA is specified as either a rectangle, a circle, or a polygon:
23834 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23835 pixel coordinates of the upper left and bottom right corners.
23836 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23837 and the radius of the circle; r may be a float or integer.
23838 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23839 vector describes one corner in the polygon.
23840 Returns the alist element for the first matching AREA in MAP. */)
23841 (map, x, y)
23842 Lisp_Object map;
23843 Lisp_Object x, y;
23844 {
23845 if (NILP (map))
23846 return Qnil;
23847
23848 CHECK_NUMBER (x);
23849 CHECK_NUMBER (y);
23850
23851 return find_hot_spot (map, XINT (x), XINT (y));
23852 }
23853
23854
23855 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23856 static void
23857 define_frame_cursor1 (f, cursor, pointer)
23858 struct frame *f;
23859 Cursor cursor;
23860 Lisp_Object pointer;
23861 {
23862 /* Do not change cursor shape while dragging mouse. */
23863 if (!NILP (do_mouse_tracking))
23864 return;
23865
23866 if (!NILP (pointer))
23867 {
23868 if (EQ (pointer, Qarrow))
23869 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23870 else if (EQ (pointer, Qhand))
23871 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23872 else if (EQ (pointer, Qtext))
23873 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23874 else if (EQ (pointer, intern ("hdrag")))
23875 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23876 #ifdef HAVE_X_WINDOWS
23877 else if (EQ (pointer, intern ("vdrag")))
23878 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23879 #endif
23880 else if (EQ (pointer, intern ("hourglass")))
23881 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23882 else if (EQ (pointer, Qmodeline))
23883 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23884 else
23885 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23886 }
23887
23888 if (cursor != No_Cursor)
23889 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23890 }
23891
23892 /* Take proper action when mouse has moved to the mode or header line
23893 or marginal area AREA of window W, x-position X and y-position Y.
23894 X is relative to the start of the text display area of W, so the
23895 width of bitmap areas and scroll bars must be subtracted to get a
23896 position relative to the start of the mode line. */
23897
23898 static void
23899 note_mode_line_or_margin_highlight (window, x, y, area)
23900 Lisp_Object window;
23901 int x, y;
23902 enum window_part area;
23903 {
23904 struct window *w = XWINDOW (window);
23905 struct frame *f = XFRAME (w->frame);
23906 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23907 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23908 Lisp_Object pointer = Qnil;
23909 int charpos, dx, dy, width, height;
23910 Lisp_Object string, object = Qnil;
23911 Lisp_Object pos, help;
23912
23913 Lisp_Object mouse_face;
23914 int original_x_pixel = x;
23915 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23916 struct glyph_row *row;
23917
23918 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23919 {
23920 int x0;
23921 struct glyph *end;
23922
23923 string = mode_line_string (w, area, &x, &y, &charpos,
23924 &object, &dx, &dy, &width, &height);
23925
23926 row = (area == ON_MODE_LINE
23927 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23928 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23929
23930 /* Find glyph */
23931 if (row->mode_line_p && row->enabled_p)
23932 {
23933 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23934 end = glyph + row->used[TEXT_AREA];
23935
23936 for (x0 = original_x_pixel;
23937 glyph < end && x0 >= glyph->pixel_width;
23938 ++glyph)
23939 x0 -= glyph->pixel_width;
23940
23941 if (glyph >= end)
23942 glyph = NULL;
23943 }
23944 }
23945 else
23946 {
23947 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23948 string = marginal_area_string (w, area, &x, &y, &charpos,
23949 &object, &dx, &dy, &width, &height);
23950 }
23951
23952 help = Qnil;
23953
23954 if (IMAGEP (object))
23955 {
23956 Lisp_Object image_map, hotspot;
23957 if ((image_map = Fplist_get (XCDR (object), QCmap),
23958 !NILP (image_map))
23959 && (hotspot = find_hot_spot (image_map, dx, dy),
23960 CONSP (hotspot))
23961 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23962 {
23963 Lisp_Object area_id, plist;
23964
23965 area_id = XCAR (hotspot);
23966 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23967 If so, we could look for mouse-enter, mouse-leave
23968 properties in PLIST (and do something...). */
23969 hotspot = XCDR (hotspot);
23970 if (CONSP (hotspot)
23971 && (plist = XCAR (hotspot), CONSP (plist)))
23972 {
23973 pointer = Fplist_get (plist, Qpointer);
23974 if (NILP (pointer))
23975 pointer = Qhand;
23976 help = Fplist_get (plist, Qhelp_echo);
23977 if (!NILP (help))
23978 {
23979 help_echo_string = help;
23980 /* Is this correct? ++kfs */
23981 XSETWINDOW (help_echo_window, w);
23982 help_echo_object = w->buffer;
23983 help_echo_pos = charpos;
23984 }
23985 }
23986 }
23987 if (NILP (pointer))
23988 pointer = Fplist_get (XCDR (object), QCpointer);
23989 }
23990
23991 if (STRINGP (string))
23992 {
23993 pos = make_number (charpos);
23994 /* If we're on a string with `help-echo' text property, arrange
23995 for the help to be displayed. This is done by setting the
23996 global variable help_echo_string to the help string. */
23997 if (NILP (help))
23998 {
23999 help = Fget_text_property (pos, Qhelp_echo, string);
24000 if (!NILP (help))
24001 {
24002 help_echo_string = help;
24003 XSETWINDOW (help_echo_window, w);
24004 help_echo_object = string;
24005 help_echo_pos = charpos;
24006 }
24007 }
24008
24009 if (NILP (pointer))
24010 pointer = Fget_text_property (pos, Qpointer, string);
24011
24012 /* Change the mouse pointer according to what is under X/Y. */
24013 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24014 {
24015 Lisp_Object map;
24016 map = Fget_text_property (pos, Qlocal_map, string);
24017 if (!KEYMAPP (map))
24018 map = Fget_text_property (pos, Qkeymap, string);
24019 if (!KEYMAPP (map))
24020 cursor = dpyinfo->vertical_scroll_bar_cursor;
24021 }
24022
24023 /* Change the mouse face according to what is under X/Y. */
24024 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24025 if (!NILP (mouse_face)
24026 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24027 && glyph)
24028 {
24029 Lisp_Object b, e;
24030
24031 struct glyph * tmp_glyph;
24032
24033 int gpos;
24034 int gseq_length;
24035 int total_pixel_width;
24036 EMACS_INT ignore;
24037
24038 int vpos, hpos;
24039
24040 b = Fprevious_single_property_change (make_number (charpos + 1),
24041 Qmouse_face, string, Qnil);
24042 if (NILP (b))
24043 b = make_number (0);
24044
24045 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24046 if (NILP (e))
24047 e = make_number (SCHARS (string));
24048
24049 /* Calculate the position(glyph position: GPOS) of GLYPH in
24050 displayed string. GPOS is different from CHARPOS.
24051
24052 CHARPOS is the position of glyph in internal string
24053 object. A mode line string format has structures which
24054 is converted to a flatten by emacs lisp interpreter.
24055 The internal string is an element of the structures.
24056 The displayed string is the flatten string. */
24057 gpos = 0;
24058 if (glyph > row_start_glyph)
24059 {
24060 tmp_glyph = glyph - 1;
24061 while (tmp_glyph >= row_start_glyph
24062 && tmp_glyph->charpos >= XINT (b)
24063 && EQ (tmp_glyph->object, glyph->object))
24064 {
24065 tmp_glyph--;
24066 gpos++;
24067 }
24068 }
24069
24070 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
24071 displayed string holding GLYPH.
24072
24073 GSEQ_LENGTH is different from SCHARS (STRING).
24074 SCHARS (STRING) returns the length of the internal string. */
24075 for (tmp_glyph = glyph, gseq_length = gpos;
24076 tmp_glyph->charpos < XINT (e);
24077 tmp_glyph++, gseq_length++)
24078 {
24079 if (!EQ (tmp_glyph->object, glyph->object))
24080 break;
24081 }
24082
24083 total_pixel_width = 0;
24084 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24085 total_pixel_width += tmp_glyph->pixel_width;
24086
24087 /* Pre calculation of re-rendering position */
24088 vpos = (x - gpos);
24089 hpos = (area == ON_MODE_LINE
24090 ? (w->current_matrix)->nrows - 1
24091 : 0);
24092
24093 /* If the re-rendering position is included in the last
24094 re-rendering area, we should do nothing. */
24095 if ( EQ (window, dpyinfo->mouse_face_window)
24096 && dpyinfo->mouse_face_beg_col <= vpos
24097 && vpos < dpyinfo->mouse_face_end_col
24098 && dpyinfo->mouse_face_beg_row == hpos )
24099 return;
24100
24101 if (clear_mouse_face (dpyinfo))
24102 cursor = No_Cursor;
24103
24104 dpyinfo->mouse_face_beg_col = vpos;
24105 dpyinfo->mouse_face_beg_row = hpos;
24106
24107 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
24108 dpyinfo->mouse_face_beg_y = 0;
24109
24110 dpyinfo->mouse_face_end_col = vpos + gseq_length;
24111 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24112
24113 dpyinfo->mouse_face_end_x = 0;
24114 dpyinfo->mouse_face_end_y = 0;
24115
24116 dpyinfo->mouse_face_past_end = 0;
24117 dpyinfo->mouse_face_window = window;
24118
24119 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24120 charpos,
24121 0, 0, 0, &ignore,
24122 glyph->face_id, 1);
24123 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24124
24125 if (NILP (pointer))
24126 pointer = Qhand;
24127 }
24128 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24129 clear_mouse_face (dpyinfo);
24130 }
24131 define_frame_cursor1 (f, cursor, pointer);
24132 }
24133
24134
24135 /* EXPORT:
24136 Take proper action when the mouse has moved to position X, Y on
24137 frame F as regards highlighting characters that have mouse-face
24138 properties. Also de-highlighting chars where the mouse was before.
24139 X and Y can be negative or out of range. */
24140
24141 void
24142 note_mouse_highlight (f, x, y)
24143 struct frame *f;
24144 int x, y;
24145 {
24146 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24147 enum window_part part;
24148 Lisp_Object window;
24149 struct window *w;
24150 Cursor cursor = No_Cursor;
24151 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24152 struct buffer *b;
24153
24154 /* When a menu is active, don't highlight because this looks odd. */
24155 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24156 if (popup_activated ())
24157 return;
24158 #endif
24159
24160 if (NILP (Vmouse_highlight)
24161 || !f->glyphs_initialized_p)
24162 return;
24163
24164 dpyinfo->mouse_face_mouse_x = x;
24165 dpyinfo->mouse_face_mouse_y = y;
24166 dpyinfo->mouse_face_mouse_frame = f;
24167
24168 if (dpyinfo->mouse_face_defer)
24169 return;
24170
24171 if (gc_in_progress)
24172 {
24173 dpyinfo->mouse_face_deferred_gc = 1;
24174 return;
24175 }
24176
24177 /* Which window is that in? */
24178 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24179
24180 /* If we were displaying active text in another window, clear that.
24181 Also clear if we move out of text area in same window. */
24182 if (! EQ (window, dpyinfo->mouse_face_window)
24183 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24184 && !NILP (dpyinfo->mouse_face_window)))
24185 clear_mouse_face (dpyinfo);
24186
24187 /* Not on a window -> return. */
24188 if (!WINDOWP (window))
24189 return;
24190
24191 /* Reset help_echo_string. It will get recomputed below. */
24192 help_echo_string = Qnil;
24193
24194 /* Convert to window-relative pixel coordinates. */
24195 w = XWINDOW (window);
24196 frame_to_window_pixel_xy (w, &x, &y);
24197
24198 /* Handle tool-bar window differently since it doesn't display a
24199 buffer. */
24200 if (EQ (window, f->tool_bar_window))
24201 {
24202 note_tool_bar_highlight (f, x, y);
24203 return;
24204 }
24205
24206 /* Mouse is on the mode, header line or margin? */
24207 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24208 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24209 {
24210 note_mode_line_or_margin_highlight (window, x, y, part);
24211 return;
24212 }
24213
24214 if (part == ON_VERTICAL_BORDER)
24215 {
24216 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24217 help_echo_string = build_string ("drag-mouse-1: resize");
24218 }
24219 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24220 || part == ON_SCROLL_BAR)
24221 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24222 else
24223 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24224
24225 /* Are we in a window whose display is up to date?
24226 And verify the buffer's text has not changed. */
24227 b = XBUFFER (w->buffer);
24228 if (part == ON_TEXT
24229 && EQ (w->window_end_valid, w->buffer)
24230 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24231 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24232 {
24233 int hpos, vpos, i, dx, dy, area;
24234 EMACS_INT pos;
24235 struct glyph *glyph;
24236 Lisp_Object object;
24237 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24238 Lisp_Object *overlay_vec = NULL;
24239 int noverlays;
24240 struct buffer *obuf;
24241 int obegv, ozv, same_region;
24242
24243 /* Find the glyph under X/Y. */
24244 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24245
24246 /* Look for :pointer property on image. */
24247 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24248 {
24249 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24250 if (img != NULL && IMAGEP (img->spec))
24251 {
24252 Lisp_Object image_map, hotspot;
24253 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24254 !NILP (image_map))
24255 && (hotspot = find_hot_spot (image_map,
24256 glyph->slice.x + dx,
24257 glyph->slice.y + dy),
24258 CONSP (hotspot))
24259 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24260 {
24261 Lisp_Object area_id, plist;
24262
24263 area_id = XCAR (hotspot);
24264 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24265 If so, we could look for mouse-enter, mouse-leave
24266 properties in PLIST (and do something...). */
24267 hotspot = XCDR (hotspot);
24268 if (CONSP (hotspot)
24269 && (plist = XCAR (hotspot), CONSP (plist)))
24270 {
24271 pointer = Fplist_get (plist, Qpointer);
24272 if (NILP (pointer))
24273 pointer = Qhand;
24274 help_echo_string = Fplist_get (plist, Qhelp_echo);
24275 if (!NILP (help_echo_string))
24276 {
24277 help_echo_window = window;
24278 help_echo_object = glyph->object;
24279 help_echo_pos = glyph->charpos;
24280 }
24281 }
24282 }
24283 if (NILP (pointer))
24284 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24285 }
24286 }
24287
24288 /* Clear mouse face if X/Y not over text. */
24289 if (glyph == NULL
24290 || area != TEXT_AREA
24291 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24292 {
24293 if (clear_mouse_face (dpyinfo))
24294 cursor = No_Cursor;
24295 if (NILP (pointer))
24296 {
24297 if (area != TEXT_AREA)
24298 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24299 else
24300 pointer = Vvoid_text_area_pointer;
24301 }
24302 goto set_cursor;
24303 }
24304
24305 pos = glyph->charpos;
24306 object = glyph->object;
24307 if (!STRINGP (object) && !BUFFERP (object))
24308 goto set_cursor;
24309
24310 /* If we get an out-of-range value, return now; avoid an error. */
24311 if (BUFFERP (object) && pos > BUF_Z (b))
24312 goto set_cursor;
24313
24314 /* Make the window's buffer temporarily current for
24315 overlays_at and compute_char_face. */
24316 obuf = current_buffer;
24317 current_buffer = b;
24318 obegv = BEGV;
24319 ozv = ZV;
24320 BEGV = BEG;
24321 ZV = Z;
24322
24323 /* Is this char mouse-active or does it have help-echo? */
24324 position = make_number (pos);
24325
24326 if (BUFFERP (object))
24327 {
24328 /* Put all the overlays we want in a vector in overlay_vec. */
24329 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24330 /* Sort overlays into increasing priority order. */
24331 noverlays = sort_overlays (overlay_vec, noverlays, w);
24332 }
24333 else
24334 noverlays = 0;
24335
24336 same_region = (EQ (window, dpyinfo->mouse_face_window)
24337 && vpos >= dpyinfo->mouse_face_beg_row
24338 && vpos <= dpyinfo->mouse_face_end_row
24339 && (vpos > dpyinfo->mouse_face_beg_row
24340 || hpos >= dpyinfo->mouse_face_beg_col)
24341 && (vpos < dpyinfo->mouse_face_end_row
24342 || hpos < dpyinfo->mouse_face_end_col
24343 || dpyinfo->mouse_face_past_end));
24344
24345 if (same_region)
24346 cursor = No_Cursor;
24347
24348 /* Check mouse-face highlighting. */
24349 if (! same_region
24350 /* If there exists an overlay with mouse-face overlapping
24351 the one we are currently highlighting, we have to
24352 check if we enter the overlapping overlay, and then
24353 highlight only that. */
24354 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24355 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24356 {
24357 /* Find the highest priority overlay with a mouse-face. */
24358 overlay = Qnil;
24359 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24360 {
24361 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24362 if (!NILP (mouse_face))
24363 overlay = overlay_vec[i];
24364 }
24365
24366 /* If we're highlighting the same overlay as before, there's
24367 no need to do that again. */
24368 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24369 goto check_help_echo;
24370 dpyinfo->mouse_face_overlay = overlay;
24371
24372 /* Clear the display of the old active region, if any. */
24373 if (clear_mouse_face (dpyinfo))
24374 cursor = No_Cursor;
24375
24376 /* If no overlay applies, get a text property. */
24377 if (NILP (overlay))
24378 mouse_face = Fget_text_property (position, Qmouse_face, object);
24379
24380 /* Next, compute the bounds of the mouse highlighting and
24381 display it. */
24382 if (!NILP (mouse_face) && STRINGP (object))
24383 {
24384 /* The mouse-highlighting comes from a display string
24385 with a mouse-face. */
24386 Lisp_Object b, e;
24387 EMACS_INT ignore;
24388
24389 b = Fprevious_single_property_change
24390 (make_number (pos + 1), Qmouse_face, object, Qnil);
24391 e = Fnext_single_property_change
24392 (position, Qmouse_face, object, Qnil);
24393 if (NILP (b))
24394 b = make_number (0);
24395 if (NILP (e))
24396 e = make_number (SCHARS (object) - 1);
24397
24398 fast_find_string_pos (w, XINT (b), object,
24399 &dpyinfo->mouse_face_beg_col,
24400 &dpyinfo->mouse_face_beg_row,
24401 &dpyinfo->mouse_face_beg_x,
24402 &dpyinfo->mouse_face_beg_y, 0);
24403 fast_find_string_pos (w, XINT (e), object,
24404 &dpyinfo->mouse_face_end_col,
24405 &dpyinfo->mouse_face_end_row,
24406 &dpyinfo->mouse_face_end_x,
24407 &dpyinfo->mouse_face_end_y, 1);
24408 dpyinfo->mouse_face_past_end = 0;
24409 dpyinfo->mouse_face_window = window;
24410 dpyinfo->mouse_face_face_id
24411 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24412 glyph->face_id, 1);
24413 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24414 cursor = No_Cursor;
24415 }
24416 else
24417 {
24418 /* The mouse-highlighting, if any, comes from an overlay
24419 or text property in the buffer. */
24420 Lisp_Object buffer, display_string;
24421
24422 if (STRINGP (object))
24423 {
24424 /* If we are on a display string with no mouse-face,
24425 check if the text under it has one. */
24426 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24427 int start = MATRIX_ROW_START_CHARPOS (r);
24428 pos = string_buffer_position (w, object, start);
24429 if (pos > 0)
24430 {
24431 mouse_face = get_char_property_and_overlay
24432 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24433 buffer = w->buffer;
24434 display_string = object;
24435 }
24436 }
24437 else
24438 {
24439 buffer = object;
24440 display_string = Qnil;
24441 }
24442
24443 if (!NILP (mouse_face))
24444 {
24445 Lisp_Object before, after;
24446 Lisp_Object before_string, after_string;
24447
24448 if (NILP (overlay))
24449 {
24450 /* Handle the text property case. */
24451 before = Fprevious_single_property_change
24452 (make_number (pos + 1), Qmouse_face, buffer,
24453 Fmarker_position (w->start));
24454 after = Fnext_single_property_change
24455 (make_number (pos), Qmouse_face, buffer,
24456 make_number (BUF_Z (XBUFFER (buffer))
24457 - XFASTINT (w->window_end_pos)));
24458 before_string = after_string = Qnil;
24459 }
24460 else
24461 {
24462 /* Handle the overlay case. */
24463 before = Foverlay_start (overlay);
24464 after = Foverlay_end (overlay);
24465 before_string = Foverlay_get (overlay, Qbefore_string);
24466 after_string = Foverlay_get (overlay, Qafter_string);
24467
24468 if (!STRINGP (before_string)) before_string = Qnil;
24469 if (!STRINGP (after_string)) after_string = Qnil;
24470 }
24471
24472 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24473 XFASTINT (before),
24474 XFASTINT (after),
24475 before_string, after_string,
24476 display_string);
24477 cursor = No_Cursor;
24478 }
24479 }
24480 }
24481
24482 check_help_echo:
24483
24484 /* Look for a `help-echo' property. */
24485 if (NILP (help_echo_string)) {
24486 Lisp_Object help, overlay;
24487
24488 /* Check overlays first. */
24489 help = overlay = Qnil;
24490 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24491 {
24492 overlay = overlay_vec[i];
24493 help = Foverlay_get (overlay, Qhelp_echo);
24494 }
24495
24496 if (!NILP (help))
24497 {
24498 help_echo_string = help;
24499 help_echo_window = window;
24500 help_echo_object = overlay;
24501 help_echo_pos = pos;
24502 }
24503 else
24504 {
24505 Lisp_Object object = glyph->object;
24506 int charpos = glyph->charpos;
24507
24508 /* Try text properties. */
24509 if (STRINGP (object)
24510 && charpos >= 0
24511 && charpos < SCHARS (object))
24512 {
24513 help = Fget_text_property (make_number (charpos),
24514 Qhelp_echo, object);
24515 if (NILP (help))
24516 {
24517 /* If the string itself doesn't specify a help-echo,
24518 see if the buffer text ``under'' it does. */
24519 struct glyph_row *r
24520 = MATRIX_ROW (w->current_matrix, vpos);
24521 int start = MATRIX_ROW_START_CHARPOS (r);
24522 EMACS_INT pos = string_buffer_position (w, object, start);
24523 if (pos > 0)
24524 {
24525 help = Fget_char_property (make_number (pos),
24526 Qhelp_echo, w->buffer);
24527 if (!NILP (help))
24528 {
24529 charpos = pos;
24530 object = w->buffer;
24531 }
24532 }
24533 }
24534 }
24535 else if (BUFFERP (object)
24536 && charpos >= BEGV
24537 && charpos < ZV)
24538 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24539 object);
24540
24541 if (!NILP (help))
24542 {
24543 help_echo_string = help;
24544 help_echo_window = window;
24545 help_echo_object = object;
24546 help_echo_pos = charpos;
24547 }
24548 }
24549 }
24550
24551 /* Look for a `pointer' property. */
24552 if (NILP (pointer))
24553 {
24554 /* Check overlays first. */
24555 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24556 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24557
24558 if (NILP (pointer))
24559 {
24560 Lisp_Object object = glyph->object;
24561 int charpos = glyph->charpos;
24562
24563 /* Try text properties. */
24564 if (STRINGP (object)
24565 && charpos >= 0
24566 && charpos < SCHARS (object))
24567 {
24568 pointer = Fget_text_property (make_number (charpos),
24569 Qpointer, object);
24570 if (NILP (pointer))
24571 {
24572 /* If the string itself doesn't specify a pointer,
24573 see if the buffer text ``under'' it does. */
24574 struct glyph_row *r
24575 = MATRIX_ROW (w->current_matrix, vpos);
24576 int start = MATRIX_ROW_START_CHARPOS (r);
24577 EMACS_INT pos = string_buffer_position (w, object,
24578 start);
24579 if (pos > 0)
24580 pointer = Fget_char_property (make_number (pos),
24581 Qpointer, w->buffer);
24582 }
24583 }
24584 else if (BUFFERP (object)
24585 && charpos >= BEGV
24586 && charpos < ZV)
24587 pointer = Fget_text_property (make_number (charpos),
24588 Qpointer, object);
24589 }
24590 }
24591
24592 BEGV = obegv;
24593 ZV = ozv;
24594 current_buffer = obuf;
24595 }
24596
24597 set_cursor:
24598
24599 define_frame_cursor1 (f, cursor, pointer);
24600 }
24601
24602
24603 /* EXPORT for RIF:
24604 Clear any mouse-face on window W. This function is part of the
24605 redisplay interface, and is called from try_window_id and similar
24606 functions to ensure the mouse-highlight is off. */
24607
24608 void
24609 x_clear_window_mouse_face (w)
24610 struct window *w;
24611 {
24612 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24613 Lisp_Object window;
24614
24615 BLOCK_INPUT;
24616 XSETWINDOW (window, w);
24617 if (EQ (window, dpyinfo->mouse_face_window))
24618 clear_mouse_face (dpyinfo);
24619 UNBLOCK_INPUT;
24620 }
24621
24622
24623 /* EXPORT:
24624 Just discard the mouse face information for frame F, if any.
24625 This is used when the size of F is changed. */
24626
24627 void
24628 cancel_mouse_face (f)
24629 struct frame *f;
24630 {
24631 Lisp_Object window;
24632 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24633
24634 window = dpyinfo->mouse_face_window;
24635 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24636 {
24637 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24638 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24639 dpyinfo->mouse_face_window = Qnil;
24640 }
24641 }
24642
24643
24644 #endif /* HAVE_WINDOW_SYSTEM */
24645
24646 \f
24647 /***********************************************************************
24648 Exposure Events
24649 ***********************************************************************/
24650
24651 #ifdef HAVE_WINDOW_SYSTEM
24652
24653 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24654 which intersects rectangle R. R is in window-relative coordinates. */
24655
24656 static void
24657 expose_area (w, row, r, area)
24658 struct window *w;
24659 struct glyph_row *row;
24660 XRectangle *r;
24661 enum glyph_row_area area;
24662 {
24663 struct glyph *first = row->glyphs[area];
24664 struct glyph *end = row->glyphs[area] + row->used[area];
24665 struct glyph *last;
24666 int first_x, start_x, x;
24667
24668 if (area == TEXT_AREA && row->fill_line_p)
24669 /* If row extends face to end of line write the whole line. */
24670 draw_glyphs (w, 0, row, area,
24671 0, row->used[area],
24672 DRAW_NORMAL_TEXT, 0);
24673 else
24674 {
24675 /* Set START_X to the window-relative start position for drawing glyphs of
24676 AREA. The first glyph of the text area can be partially visible.
24677 The first glyphs of other areas cannot. */
24678 start_x = window_box_left_offset (w, area);
24679 x = start_x;
24680 if (area == TEXT_AREA)
24681 x += row->x;
24682
24683 /* Find the first glyph that must be redrawn. */
24684 while (first < end
24685 && x + first->pixel_width < r->x)
24686 {
24687 x += first->pixel_width;
24688 ++first;
24689 }
24690
24691 /* Find the last one. */
24692 last = first;
24693 first_x = x;
24694 while (last < end
24695 && x < r->x + r->width)
24696 {
24697 x += last->pixel_width;
24698 ++last;
24699 }
24700
24701 /* Repaint. */
24702 if (last > first)
24703 draw_glyphs (w, first_x - start_x, row, area,
24704 first - row->glyphs[area], last - row->glyphs[area],
24705 DRAW_NORMAL_TEXT, 0);
24706 }
24707 }
24708
24709
24710 /* Redraw the parts of the glyph row ROW on window W intersecting
24711 rectangle R. R is in window-relative coordinates. Value is
24712 non-zero if mouse-face was overwritten. */
24713
24714 static int
24715 expose_line (w, row, r)
24716 struct window *w;
24717 struct glyph_row *row;
24718 XRectangle *r;
24719 {
24720 xassert (row->enabled_p);
24721
24722 if (row->mode_line_p || w->pseudo_window_p)
24723 draw_glyphs (w, 0, row, TEXT_AREA,
24724 0, row->used[TEXT_AREA],
24725 DRAW_NORMAL_TEXT, 0);
24726 else
24727 {
24728 if (row->used[LEFT_MARGIN_AREA])
24729 expose_area (w, row, r, LEFT_MARGIN_AREA);
24730 if (row->used[TEXT_AREA])
24731 expose_area (w, row, r, TEXT_AREA);
24732 if (row->used[RIGHT_MARGIN_AREA])
24733 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24734 draw_row_fringe_bitmaps (w, row);
24735 }
24736
24737 return row->mouse_face_p;
24738 }
24739
24740
24741 /* Redraw those parts of glyphs rows during expose event handling that
24742 overlap other rows. Redrawing of an exposed line writes over parts
24743 of lines overlapping that exposed line; this function fixes that.
24744
24745 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24746 row in W's current matrix that is exposed and overlaps other rows.
24747 LAST_OVERLAPPING_ROW is the last such row. */
24748
24749 static void
24750 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24751 struct window *w;
24752 struct glyph_row *first_overlapping_row;
24753 struct glyph_row *last_overlapping_row;
24754 XRectangle *r;
24755 {
24756 struct glyph_row *row;
24757
24758 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24759 if (row->overlapping_p)
24760 {
24761 xassert (row->enabled_p && !row->mode_line_p);
24762
24763 row->clip = r;
24764 if (row->used[LEFT_MARGIN_AREA])
24765 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24766
24767 if (row->used[TEXT_AREA])
24768 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24769
24770 if (row->used[RIGHT_MARGIN_AREA])
24771 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24772 row->clip = NULL;
24773 }
24774 }
24775
24776
24777 /* Return non-zero if W's cursor intersects rectangle R. */
24778
24779 static int
24780 phys_cursor_in_rect_p (w, r)
24781 struct window *w;
24782 XRectangle *r;
24783 {
24784 XRectangle cr, result;
24785 struct glyph *cursor_glyph;
24786 struct glyph_row *row;
24787
24788 if (w->phys_cursor.vpos >= 0
24789 && w->phys_cursor.vpos < w->current_matrix->nrows
24790 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24791 row->enabled_p)
24792 && row->cursor_in_fringe_p)
24793 {
24794 /* Cursor is in the fringe. */
24795 cr.x = window_box_right_offset (w,
24796 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24797 ? RIGHT_MARGIN_AREA
24798 : TEXT_AREA));
24799 cr.y = row->y;
24800 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24801 cr.height = row->height;
24802 return x_intersect_rectangles (&cr, r, &result);
24803 }
24804
24805 cursor_glyph = get_phys_cursor_glyph (w);
24806 if (cursor_glyph)
24807 {
24808 /* r is relative to W's box, but w->phys_cursor.x is relative
24809 to left edge of W's TEXT area. Adjust it. */
24810 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24811 cr.y = w->phys_cursor.y;
24812 cr.width = cursor_glyph->pixel_width;
24813 cr.height = w->phys_cursor_height;
24814 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24815 I assume the effect is the same -- and this is portable. */
24816 return x_intersect_rectangles (&cr, r, &result);
24817 }
24818 /* If we don't understand the format, pretend we're not in the hot-spot. */
24819 return 0;
24820 }
24821
24822
24823 /* EXPORT:
24824 Draw a vertical window border to the right of window W if W doesn't
24825 have vertical scroll bars. */
24826
24827 void
24828 x_draw_vertical_border (w)
24829 struct window *w;
24830 {
24831 struct frame *f = XFRAME (WINDOW_FRAME (w));
24832
24833 /* We could do better, if we knew what type of scroll-bar the adjacent
24834 windows (on either side) have... But we don't :-(
24835 However, I think this works ok. ++KFS 2003-04-25 */
24836
24837 /* Redraw borders between horizontally adjacent windows. Don't
24838 do it for frames with vertical scroll bars because either the
24839 right scroll bar of a window, or the left scroll bar of its
24840 neighbor will suffice as a border. */
24841 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24842 return;
24843
24844 if (!WINDOW_RIGHTMOST_P (w)
24845 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24846 {
24847 int x0, x1, y0, y1;
24848
24849 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24850 y1 -= 1;
24851
24852 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24853 x1 -= 1;
24854
24855 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24856 }
24857 else if (!WINDOW_LEFTMOST_P (w)
24858 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24859 {
24860 int x0, x1, y0, y1;
24861
24862 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24863 y1 -= 1;
24864
24865 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24866 x0 -= 1;
24867
24868 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24869 }
24870 }
24871
24872
24873 /* Redraw the part of window W intersection rectangle FR. Pixel
24874 coordinates in FR are frame-relative. Call this function with
24875 input blocked. Value is non-zero if the exposure overwrites
24876 mouse-face. */
24877
24878 static int
24879 expose_window (w, fr)
24880 struct window *w;
24881 XRectangle *fr;
24882 {
24883 struct frame *f = XFRAME (w->frame);
24884 XRectangle wr, r;
24885 int mouse_face_overwritten_p = 0;
24886
24887 /* If window is not yet fully initialized, do nothing. This can
24888 happen when toolkit scroll bars are used and a window is split.
24889 Reconfiguring the scroll bar will generate an expose for a newly
24890 created window. */
24891 if (w->current_matrix == NULL)
24892 return 0;
24893
24894 /* When we're currently updating the window, display and current
24895 matrix usually don't agree. Arrange for a thorough display
24896 later. */
24897 if (w == updated_window)
24898 {
24899 SET_FRAME_GARBAGED (f);
24900 return 0;
24901 }
24902
24903 /* Frame-relative pixel rectangle of W. */
24904 wr.x = WINDOW_LEFT_EDGE_X (w);
24905 wr.y = WINDOW_TOP_EDGE_Y (w);
24906 wr.width = WINDOW_TOTAL_WIDTH (w);
24907 wr.height = WINDOW_TOTAL_HEIGHT (w);
24908
24909 if (x_intersect_rectangles (fr, &wr, &r))
24910 {
24911 int yb = window_text_bottom_y (w);
24912 struct glyph_row *row;
24913 int cursor_cleared_p;
24914 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24915
24916 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24917 r.x, r.y, r.width, r.height));
24918
24919 /* Convert to window coordinates. */
24920 r.x -= WINDOW_LEFT_EDGE_X (w);
24921 r.y -= WINDOW_TOP_EDGE_Y (w);
24922
24923 /* Turn off the cursor. */
24924 if (!w->pseudo_window_p
24925 && phys_cursor_in_rect_p (w, &r))
24926 {
24927 x_clear_cursor (w);
24928 cursor_cleared_p = 1;
24929 }
24930 else
24931 cursor_cleared_p = 0;
24932
24933 /* Update lines intersecting rectangle R. */
24934 first_overlapping_row = last_overlapping_row = NULL;
24935 for (row = w->current_matrix->rows;
24936 row->enabled_p;
24937 ++row)
24938 {
24939 int y0 = row->y;
24940 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24941
24942 if ((y0 >= r.y && y0 < r.y + r.height)
24943 || (y1 > r.y && y1 < r.y + r.height)
24944 || (r.y >= y0 && r.y < y1)
24945 || (r.y + r.height > y0 && r.y + r.height < y1))
24946 {
24947 /* A header line may be overlapping, but there is no need
24948 to fix overlapping areas for them. KFS 2005-02-12 */
24949 if (row->overlapping_p && !row->mode_line_p)
24950 {
24951 if (first_overlapping_row == NULL)
24952 first_overlapping_row = row;
24953 last_overlapping_row = row;
24954 }
24955
24956 row->clip = fr;
24957 if (expose_line (w, row, &r))
24958 mouse_face_overwritten_p = 1;
24959 row->clip = NULL;
24960 }
24961 else if (row->overlapping_p)
24962 {
24963 /* We must redraw a row overlapping the exposed area. */
24964 if (y0 < r.y
24965 ? y0 + row->phys_height > r.y
24966 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24967 {
24968 if (first_overlapping_row == NULL)
24969 first_overlapping_row = row;
24970 last_overlapping_row = row;
24971 }
24972 }
24973
24974 if (y1 >= yb)
24975 break;
24976 }
24977
24978 /* Display the mode line if there is one. */
24979 if (WINDOW_WANTS_MODELINE_P (w)
24980 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24981 row->enabled_p)
24982 && row->y < r.y + r.height)
24983 {
24984 if (expose_line (w, row, &r))
24985 mouse_face_overwritten_p = 1;
24986 }
24987
24988 if (!w->pseudo_window_p)
24989 {
24990 /* Fix the display of overlapping rows. */
24991 if (first_overlapping_row)
24992 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24993 fr);
24994
24995 /* Draw border between windows. */
24996 x_draw_vertical_border (w);
24997
24998 /* Turn the cursor on again. */
24999 if (cursor_cleared_p)
25000 update_window_cursor (w, 1);
25001 }
25002 }
25003
25004 return mouse_face_overwritten_p;
25005 }
25006
25007
25008
25009 /* Redraw (parts) of all windows in the window tree rooted at W that
25010 intersect R. R contains frame pixel coordinates. Value is
25011 non-zero if the exposure overwrites mouse-face. */
25012
25013 static int
25014 expose_window_tree (w, r)
25015 struct window *w;
25016 XRectangle *r;
25017 {
25018 struct frame *f = XFRAME (w->frame);
25019 int mouse_face_overwritten_p = 0;
25020
25021 while (w && !FRAME_GARBAGED_P (f))
25022 {
25023 if (!NILP (w->hchild))
25024 mouse_face_overwritten_p
25025 |= expose_window_tree (XWINDOW (w->hchild), r);
25026 else if (!NILP (w->vchild))
25027 mouse_face_overwritten_p
25028 |= expose_window_tree (XWINDOW (w->vchild), r);
25029 else
25030 mouse_face_overwritten_p |= expose_window (w, r);
25031
25032 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25033 }
25034
25035 return mouse_face_overwritten_p;
25036 }
25037
25038
25039 /* EXPORT:
25040 Redisplay an exposed area of frame F. X and Y are the upper-left
25041 corner of the exposed rectangle. W and H are width and height of
25042 the exposed area. All are pixel values. W or H zero means redraw
25043 the entire frame. */
25044
25045 void
25046 expose_frame (f, x, y, w, h)
25047 struct frame *f;
25048 int x, y, w, h;
25049 {
25050 XRectangle r;
25051 int mouse_face_overwritten_p = 0;
25052
25053 TRACE ((stderr, "expose_frame "));
25054
25055 /* No need to redraw if frame will be redrawn soon. */
25056 if (FRAME_GARBAGED_P (f))
25057 {
25058 TRACE ((stderr, " garbaged\n"));
25059 return;
25060 }
25061
25062 /* If basic faces haven't been realized yet, there is no point in
25063 trying to redraw anything. This can happen when we get an expose
25064 event while Emacs is starting, e.g. by moving another window. */
25065 if (FRAME_FACE_CACHE (f) == NULL
25066 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25067 {
25068 TRACE ((stderr, " no faces\n"));
25069 return;
25070 }
25071
25072 if (w == 0 || h == 0)
25073 {
25074 r.x = r.y = 0;
25075 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25076 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25077 }
25078 else
25079 {
25080 r.x = x;
25081 r.y = y;
25082 r.width = w;
25083 r.height = h;
25084 }
25085
25086 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25087 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25088
25089 if (WINDOWP (f->tool_bar_window))
25090 mouse_face_overwritten_p
25091 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25092
25093 #ifdef HAVE_X_WINDOWS
25094 #ifndef MSDOS
25095 #ifndef USE_X_TOOLKIT
25096 if (WINDOWP (f->menu_bar_window))
25097 mouse_face_overwritten_p
25098 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25099 #endif /* not USE_X_TOOLKIT */
25100 #endif
25101 #endif
25102
25103 /* Some window managers support a focus-follows-mouse style with
25104 delayed raising of frames. Imagine a partially obscured frame,
25105 and moving the mouse into partially obscured mouse-face on that
25106 frame. The visible part of the mouse-face will be highlighted,
25107 then the WM raises the obscured frame. With at least one WM, KDE
25108 2.1, Emacs is not getting any event for the raising of the frame
25109 (even tried with SubstructureRedirectMask), only Expose events.
25110 These expose events will draw text normally, i.e. not
25111 highlighted. Which means we must redo the highlight here.
25112 Subsume it under ``we love X''. --gerd 2001-08-15 */
25113 /* Included in Windows version because Windows most likely does not
25114 do the right thing if any third party tool offers
25115 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25116 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25117 {
25118 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25119 if (f == dpyinfo->mouse_face_mouse_frame)
25120 {
25121 int x = dpyinfo->mouse_face_mouse_x;
25122 int y = dpyinfo->mouse_face_mouse_y;
25123 clear_mouse_face (dpyinfo);
25124 note_mouse_highlight (f, x, y);
25125 }
25126 }
25127 }
25128
25129
25130 /* EXPORT:
25131 Determine the intersection of two rectangles R1 and R2. Return
25132 the intersection in *RESULT. Value is non-zero if RESULT is not
25133 empty. */
25134
25135 int
25136 x_intersect_rectangles (r1, r2, result)
25137 XRectangle *r1, *r2, *result;
25138 {
25139 XRectangle *left, *right;
25140 XRectangle *upper, *lower;
25141 int intersection_p = 0;
25142
25143 /* Rearrange so that R1 is the left-most rectangle. */
25144 if (r1->x < r2->x)
25145 left = r1, right = r2;
25146 else
25147 left = r2, right = r1;
25148
25149 /* X0 of the intersection is right.x0, if this is inside R1,
25150 otherwise there is no intersection. */
25151 if (right->x <= left->x + left->width)
25152 {
25153 result->x = right->x;
25154
25155 /* The right end of the intersection is the minimum of the
25156 the right ends of left and right. */
25157 result->width = (min (left->x + left->width, right->x + right->width)
25158 - result->x);
25159
25160 /* Same game for Y. */
25161 if (r1->y < r2->y)
25162 upper = r1, lower = r2;
25163 else
25164 upper = r2, lower = r1;
25165
25166 /* The upper end of the intersection is lower.y0, if this is inside
25167 of upper. Otherwise, there is no intersection. */
25168 if (lower->y <= upper->y + upper->height)
25169 {
25170 result->y = lower->y;
25171
25172 /* The lower end of the intersection is the minimum of the lower
25173 ends of upper and lower. */
25174 result->height = (min (lower->y + lower->height,
25175 upper->y + upper->height)
25176 - result->y);
25177 intersection_p = 1;
25178 }
25179 }
25180
25181 return intersection_p;
25182 }
25183
25184 #endif /* HAVE_WINDOW_SYSTEM */
25185
25186 \f
25187 /***********************************************************************
25188 Initialization
25189 ***********************************************************************/
25190
25191 void
25192 syms_of_xdisp ()
25193 {
25194 Vwith_echo_area_save_vector = Qnil;
25195 staticpro (&Vwith_echo_area_save_vector);
25196
25197 Vmessage_stack = Qnil;
25198 staticpro (&Vmessage_stack);
25199
25200 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25201 staticpro (&Qinhibit_redisplay);
25202
25203 message_dolog_marker1 = Fmake_marker ();
25204 staticpro (&message_dolog_marker1);
25205 message_dolog_marker2 = Fmake_marker ();
25206 staticpro (&message_dolog_marker2);
25207 message_dolog_marker3 = Fmake_marker ();
25208 staticpro (&message_dolog_marker3);
25209
25210 #if GLYPH_DEBUG
25211 defsubr (&Sdump_frame_glyph_matrix);
25212 defsubr (&Sdump_glyph_matrix);
25213 defsubr (&Sdump_glyph_row);
25214 defsubr (&Sdump_tool_bar_row);
25215 defsubr (&Strace_redisplay);
25216 defsubr (&Strace_to_stderr);
25217 #endif
25218 #ifdef HAVE_WINDOW_SYSTEM
25219 defsubr (&Stool_bar_lines_needed);
25220 defsubr (&Slookup_image_map);
25221 #endif
25222 defsubr (&Sformat_mode_line);
25223 defsubr (&Sinvisible_p);
25224
25225 staticpro (&Qmenu_bar_update_hook);
25226 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25227
25228 staticpro (&Qoverriding_terminal_local_map);
25229 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25230
25231 staticpro (&Qoverriding_local_map);
25232 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25233
25234 staticpro (&Qwindow_scroll_functions);
25235 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25236
25237 staticpro (&Qwindow_text_change_functions);
25238 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25239
25240 staticpro (&Qredisplay_end_trigger_functions);
25241 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25242
25243 staticpro (&Qinhibit_point_motion_hooks);
25244 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25245
25246 Qeval = intern_c_string ("eval");
25247 staticpro (&Qeval);
25248
25249 QCdata = intern_c_string (":data");
25250 staticpro (&QCdata);
25251 Qdisplay = intern_c_string ("display");
25252 staticpro (&Qdisplay);
25253 Qspace_width = intern_c_string ("space-width");
25254 staticpro (&Qspace_width);
25255 Qraise = intern_c_string ("raise");
25256 staticpro (&Qraise);
25257 Qslice = intern_c_string ("slice");
25258 staticpro (&Qslice);
25259 Qspace = intern_c_string ("space");
25260 staticpro (&Qspace);
25261 Qmargin = intern_c_string ("margin");
25262 staticpro (&Qmargin);
25263 Qpointer = intern_c_string ("pointer");
25264 staticpro (&Qpointer);
25265 Qleft_margin = intern_c_string ("left-margin");
25266 staticpro (&Qleft_margin);
25267 Qright_margin = intern_c_string ("right-margin");
25268 staticpro (&Qright_margin);
25269 Qcenter = intern_c_string ("center");
25270 staticpro (&Qcenter);
25271 Qline_height = intern_c_string ("line-height");
25272 staticpro (&Qline_height);
25273 QCalign_to = intern_c_string (":align-to");
25274 staticpro (&QCalign_to);
25275 QCrelative_width = intern_c_string (":relative-width");
25276 staticpro (&QCrelative_width);
25277 QCrelative_height = intern_c_string (":relative-height");
25278 staticpro (&QCrelative_height);
25279 QCeval = intern_c_string (":eval");
25280 staticpro (&QCeval);
25281 QCpropertize = intern_c_string (":propertize");
25282 staticpro (&QCpropertize);
25283 QCfile = intern_c_string (":file");
25284 staticpro (&QCfile);
25285 Qfontified = intern_c_string ("fontified");
25286 staticpro (&Qfontified);
25287 Qfontification_functions = intern_c_string ("fontification-functions");
25288 staticpro (&Qfontification_functions);
25289 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25290 staticpro (&Qtrailing_whitespace);
25291 Qescape_glyph = intern_c_string ("escape-glyph");
25292 staticpro (&Qescape_glyph);
25293 Qnobreak_space = intern_c_string ("nobreak-space");
25294 staticpro (&Qnobreak_space);
25295 Qimage = intern_c_string ("image");
25296 staticpro (&Qimage);
25297 QCmap = intern_c_string (":map");
25298 staticpro (&QCmap);
25299 QCpointer = intern_c_string (":pointer");
25300 staticpro (&QCpointer);
25301 Qrect = intern_c_string ("rect");
25302 staticpro (&Qrect);
25303 Qcircle = intern_c_string ("circle");
25304 staticpro (&Qcircle);
25305 Qpoly = intern_c_string ("poly");
25306 staticpro (&Qpoly);
25307 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25308 staticpro (&Qmessage_truncate_lines);
25309 Qgrow_only = intern_c_string ("grow-only");
25310 staticpro (&Qgrow_only);
25311 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25312 staticpro (&Qinhibit_menubar_update);
25313 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25314 staticpro (&Qinhibit_eval_during_redisplay);
25315 Qposition = intern_c_string ("position");
25316 staticpro (&Qposition);
25317 Qbuffer_position = intern_c_string ("buffer-position");
25318 staticpro (&Qbuffer_position);
25319 Qobject = intern_c_string ("object");
25320 staticpro (&Qobject);
25321 Qbar = intern_c_string ("bar");
25322 staticpro (&Qbar);
25323 Qhbar = intern_c_string ("hbar");
25324 staticpro (&Qhbar);
25325 Qbox = intern_c_string ("box");
25326 staticpro (&Qbox);
25327 Qhollow = intern_c_string ("hollow");
25328 staticpro (&Qhollow);
25329 Qhand = intern_c_string ("hand");
25330 staticpro (&Qhand);
25331 Qarrow = intern_c_string ("arrow");
25332 staticpro (&Qarrow);
25333 Qtext = intern_c_string ("text");
25334 staticpro (&Qtext);
25335 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25336 staticpro (&Qrisky_local_variable);
25337 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25338 staticpro (&Qinhibit_free_realized_faces);
25339
25340 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25341 Fcons (intern_c_string ("void-variable"), Qnil)),
25342 Qnil);
25343 staticpro (&list_of_error);
25344
25345 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25346 staticpro (&Qlast_arrow_position);
25347 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25348 staticpro (&Qlast_arrow_string);
25349
25350 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25351 staticpro (&Qoverlay_arrow_string);
25352 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25353 staticpro (&Qoverlay_arrow_bitmap);
25354
25355 echo_buffer[0] = echo_buffer[1] = Qnil;
25356 staticpro (&echo_buffer[0]);
25357 staticpro (&echo_buffer[1]);
25358
25359 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25360 staticpro (&echo_area_buffer[0]);
25361 staticpro (&echo_area_buffer[1]);
25362
25363 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25364 staticpro (&Vmessages_buffer_name);
25365
25366 mode_line_proptrans_alist = Qnil;
25367 staticpro (&mode_line_proptrans_alist);
25368 mode_line_string_list = Qnil;
25369 staticpro (&mode_line_string_list);
25370 mode_line_string_face = Qnil;
25371 staticpro (&mode_line_string_face);
25372 mode_line_string_face_prop = Qnil;
25373 staticpro (&mode_line_string_face_prop);
25374 Vmode_line_unwind_vector = Qnil;
25375 staticpro (&Vmode_line_unwind_vector);
25376
25377 help_echo_string = Qnil;
25378 staticpro (&help_echo_string);
25379 help_echo_object = Qnil;
25380 staticpro (&help_echo_object);
25381 help_echo_window = Qnil;
25382 staticpro (&help_echo_window);
25383 previous_help_echo_string = Qnil;
25384 staticpro (&previous_help_echo_string);
25385 help_echo_pos = -1;
25386
25387 Qright_to_left = intern ("right-to-left");
25388 staticpro (&Qright_to_left);
25389 Qleft_to_right = intern ("left-to-right");
25390 staticpro (&Qleft_to_right);
25391
25392 #ifdef HAVE_WINDOW_SYSTEM
25393 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25394 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25395 For example, if a block cursor is over a tab, it will be drawn as
25396 wide as that tab on the display. */);
25397 x_stretch_cursor_p = 0;
25398 #endif
25399
25400 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25401 doc: /* *Non-nil means highlight trailing whitespace.
25402 The face used for trailing whitespace is `trailing-whitespace'. */);
25403 Vshow_trailing_whitespace = Qnil;
25404
25405 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25406 doc: /* *Control highlighting of nobreak space and soft hyphen.
25407 A value of t means highlight the character itself (for nobreak space,
25408 use face `nobreak-space').
25409 A value of nil means no highlighting.
25410 Other values mean display the escape glyph followed by an ordinary
25411 space or ordinary hyphen. */);
25412 Vnobreak_char_display = Qt;
25413
25414 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25415 doc: /* *The pointer shape to show in void text areas.
25416 A value of nil means to show the text pointer. Other options are `arrow',
25417 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25418 Vvoid_text_area_pointer = Qarrow;
25419
25420 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25421 doc: /* Non-nil means don't actually do any redisplay.
25422 This is used for internal purposes. */);
25423 Vinhibit_redisplay = Qnil;
25424
25425 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25426 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25427 Vglobal_mode_string = Qnil;
25428
25429 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25430 doc: /* Marker for where to display an arrow on top of the buffer text.
25431 This must be the beginning of a line in order to work.
25432 See also `overlay-arrow-string'. */);
25433 Voverlay_arrow_position = Qnil;
25434
25435 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25436 doc: /* String to display as an arrow in non-window frames.
25437 See also `overlay-arrow-position'. */);
25438 Voverlay_arrow_string = make_pure_c_string ("=>");
25439
25440 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25441 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25442 The symbols on this list are examined during redisplay to determine
25443 where to display overlay arrows. */);
25444 Voverlay_arrow_variable_list
25445 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25446
25447 DEFVAR_INT ("scroll-step", &scroll_step,
25448 doc: /* *The number of lines to try scrolling a window by when point moves out.
25449 If that fails to bring point back on frame, point is centered instead.
25450 If this is zero, point is always centered after it moves off frame.
25451 If you want scrolling to always be a line at a time, you should set
25452 `scroll-conservatively' to a large value rather than set this to 1. */);
25453
25454 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25455 doc: /* *Scroll up to this many lines, to bring point back on screen.
25456 If point moves off-screen, redisplay will scroll by up to
25457 `scroll-conservatively' lines in order to bring point just barely
25458 onto the screen again. If that cannot be done, then redisplay
25459 recenters point as usual.
25460
25461 A value of zero means always recenter point if it moves off screen. */);
25462 scroll_conservatively = 0;
25463
25464 DEFVAR_INT ("scroll-margin", &scroll_margin,
25465 doc: /* *Number of lines of margin at the top and bottom of a window.
25466 Recenter the window whenever point gets within this many lines
25467 of the top or bottom of the window. */);
25468 scroll_margin = 0;
25469
25470 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25471 doc: /* Pixels per inch value for non-window system displays.
25472 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25473 Vdisplay_pixels_per_inch = make_float (72.0);
25474
25475 #if GLYPH_DEBUG
25476 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25477 #endif
25478
25479 DEFVAR_LISP ("truncate-partial-width-windows",
25480 &Vtruncate_partial_width_windows,
25481 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25482 For an integer value, truncate lines in each window narrower than the
25483 full frame width, provided the window width is less than that integer;
25484 otherwise, respect the value of `truncate-lines'.
25485
25486 For any other non-nil value, truncate lines in all windows that do
25487 not span the full frame width.
25488
25489 A value of nil means to respect the value of `truncate-lines'.
25490
25491 If `word-wrap' is enabled, you might want to reduce this. */);
25492 Vtruncate_partial_width_windows = make_number (50);
25493
25494 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25495 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25496 Any other value means to use the appropriate face, `mode-line',
25497 `header-line', or `menu' respectively. */);
25498 mode_line_inverse_video = 1;
25499
25500 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25501 doc: /* *Maximum buffer size for which line number should be displayed.
25502 If the buffer is bigger than this, the line number does not appear
25503 in the mode line. A value of nil means no limit. */);
25504 Vline_number_display_limit = Qnil;
25505
25506 DEFVAR_INT ("line-number-display-limit-width",
25507 &line_number_display_limit_width,
25508 doc: /* *Maximum line width (in characters) for line number display.
25509 If the average length of the lines near point is bigger than this, then the
25510 line number may be omitted from the mode line. */);
25511 line_number_display_limit_width = 200;
25512
25513 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25514 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25515 highlight_nonselected_windows = 0;
25516
25517 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25518 doc: /* Non-nil if more than one frame is visible on this display.
25519 Minibuffer-only frames don't count, but iconified frames do.
25520 This variable is not guaranteed to be accurate except while processing
25521 `frame-title-format' and `icon-title-format'. */);
25522
25523 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25524 doc: /* Template for displaying the title bar of visible frames.
25525 \(Assuming the window manager supports this feature.)
25526
25527 This variable has the same structure as `mode-line-format', except that
25528 the %c and %l constructs are ignored. It is used only on frames for
25529 which no explicit name has been set \(see `modify-frame-parameters'). */);
25530
25531 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25532 doc: /* Template for displaying the title bar of an iconified frame.
25533 \(Assuming the window manager supports this feature.)
25534 This variable has the same structure as `mode-line-format' (which see),
25535 and is used only on frames for which no explicit name has been set
25536 \(see `modify-frame-parameters'). */);
25537 Vicon_title_format
25538 = Vframe_title_format
25539 = pure_cons (intern_c_string ("multiple-frames"),
25540 pure_cons (make_pure_c_string ("%b"),
25541 pure_cons (pure_cons (empty_unibyte_string,
25542 pure_cons (intern_c_string ("invocation-name"),
25543 pure_cons (make_pure_c_string ("@"),
25544 pure_cons (intern_c_string ("system-name"),
25545 Qnil)))),
25546 Qnil)));
25547
25548 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25549 doc: /* Maximum number of lines to keep in the message log buffer.
25550 If nil, disable message logging. If t, log messages but don't truncate
25551 the buffer when it becomes large. */);
25552 Vmessage_log_max = make_number (100);
25553
25554 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25555 doc: /* Functions called before redisplay, if window sizes have changed.
25556 The value should be a list of functions that take one argument.
25557 Just before redisplay, for each frame, if any of its windows have changed
25558 size since the last redisplay, or have been split or deleted,
25559 all the functions in the list are called, with the frame as argument. */);
25560 Vwindow_size_change_functions = Qnil;
25561
25562 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25563 doc: /* List of functions to call before redisplaying a window with scrolling.
25564 Each function is called with two arguments, the window and its new
25565 display-start position. Note that these functions are also called by
25566 `set-window-buffer'. Also note that the value of `window-end' is not
25567 valid when these functions are called. */);
25568 Vwindow_scroll_functions = Qnil;
25569
25570 DEFVAR_LISP ("window-text-change-functions",
25571 &Vwindow_text_change_functions,
25572 doc: /* Functions to call in redisplay when text in the window might change. */);
25573 Vwindow_text_change_functions = Qnil;
25574
25575 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25576 doc: /* Functions called when redisplay of a window reaches the end trigger.
25577 Each function is called with two arguments, the window and the end trigger value.
25578 See `set-window-redisplay-end-trigger'. */);
25579 Vredisplay_end_trigger_functions = Qnil;
25580
25581 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25582 doc: /* *Non-nil means autoselect window with mouse pointer.
25583 If nil, do not autoselect windows.
25584 A positive number means delay autoselection by that many seconds: a
25585 window is autoselected only after the mouse has remained in that
25586 window for the duration of the delay.
25587 A negative number has a similar effect, but causes windows to be
25588 autoselected only after the mouse has stopped moving. \(Because of
25589 the way Emacs compares mouse events, you will occasionally wait twice
25590 that time before the window gets selected.\)
25591 Any other value means to autoselect window instantaneously when the
25592 mouse pointer enters it.
25593
25594 Autoselection selects the minibuffer only if it is active, and never
25595 unselects the minibuffer if it is active.
25596
25597 When customizing this variable make sure that the actual value of
25598 `focus-follows-mouse' matches the behavior of your window manager. */);
25599 Vmouse_autoselect_window = Qnil;
25600
25601 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25602 doc: /* *Non-nil means automatically resize tool-bars.
25603 This dynamically changes the tool-bar's height to the minimum height
25604 that is needed to make all tool-bar items visible.
25605 If value is `grow-only', the tool-bar's height is only increased
25606 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25607 Vauto_resize_tool_bars = Qt;
25608
25609 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25610 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25611 auto_raise_tool_bar_buttons_p = 1;
25612
25613 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25614 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25615 make_cursor_line_fully_visible_p = 1;
25616
25617 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25618 doc: /* *Border below tool-bar in pixels.
25619 If an integer, use it as the height of the border.
25620 If it is one of `internal-border-width' or `border-width', use the
25621 value of the corresponding frame parameter.
25622 Otherwise, no border is added below the tool-bar. */);
25623 Vtool_bar_border = Qinternal_border_width;
25624
25625 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25626 doc: /* *Margin around tool-bar buttons in pixels.
25627 If an integer, use that for both horizontal and vertical margins.
25628 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25629 HORZ specifying the horizontal margin, and VERT specifying the
25630 vertical margin. */);
25631 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25632
25633 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25634 doc: /* *Relief thickness of tool-bar buttons. */);
25635 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25636
25637 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25638 doc: /* List of functions to call to fontify regions of text.
25639 Each function is called with one argument POS. Functions must
25640 fontify a region starting at POS in the current buffer, and give
25641 fontified regions the property `fontified'. */);
25642 Vfontification_functions = Qnil;
25643 Fmake_variable_buffer_local (Qfontification_functions);
25644
25645 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25646 &unibyte_display_via_language_environment,
25647 doc: /* *Non-nil means display unibyte text according to language environment.
25648 Specifically, this means that raw bytes in the range 160-255 decimal
25649 are displayed by converting them to the equivalent multibyte characters
25650 according to the current language environment. As a result, they are
25651 displayed according to the current fontset.
25652
25653 Note that this variable affects only how these bytes are displayed,
25654 but does not change the fact they are interpreted as raw bytes. */);
25655 unibyte_display_via_language_environment = 0;
25656
25657 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25658 doc: /* *Maximum height for resizing mini-windows.
25659 If a float, it specifies a fraction of the mini-window frame's height.
25660 If an integer, it specifies a number of lines. */);
25661 Vmax_mini_window_height = make_float (0.25);
25662
25663 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25664 doc: /* *How to resize mini-windows.
25665 A value of nil means don't automatically resize mini-windows.
25666 A value of t means resize them to fit the text displayed in them.
25667 A value of `grow-only', the default, means let mini-windows grow
25668 only, until their display becomes empty, at which point the windows
25669 go back to their normal size. */);
25670 Vresize_mini_windows = Qgrow_only;
25671
25672 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25673 doc: /* Alist specifying how to blink the cursor off.
25674 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25675 `cursor-type' frame-parameter or variable equals ON-STATE,
25676 comparing using `equal', Emacs uses OFF-STATE to specify
25677 how to blink it off. ON-STATE and OFF-STATE are values for
25678 the `cursor-type' frame parameter.
25679
25680 If a frame's ON-STATE has no entry in this list,
25681 the frame's other specifications determine how to blink the cursor off. */);
25682 Vblink_cursor_alist = Qnil;
25683
25684 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25685 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25686 automatic_hscrolling_p = 1;
25687 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25688 staticpro (&Qauto_hscroll_mode);
25689
25690 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25691 doc: /* *How many columns away from the window edge point is allowed to get
25692 before automatic hscrolling will horizontally scroll the window. */);
25693 hscroll_margin = 5;
25694
25695 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25696 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25697 When point is less than `hscroll-margin' columns from the window
25698 edge, automatic hscrolling will scroll the window by the amount of columns
25699 determined by this variable. If its value is a positive integer, scroll that
25700 many columns. If it's a positive floating-point number, it specifies the
25701 fraction of the window's width to scroll. If it's nil or zero, point will be
25702 centered horizontally after the scroll. Any other value, including negative
25703 numbers, are treated as if the value were zero.
25704
25705 Automatic hscrolling always moves point outside the scroll margin, so if
25706 point was more than scroll step columns inside the margin, the window will
25707 scroll more than the value given by the scroll step.
25708
25709 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25710 and `scroll-right' overrides this variable's effect. */);
25711 Vhscroll_step = make_number (0);
25712
25713 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25714 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25715 Bind this around calls to `message' to let it take effect. */);
25716 message_truncate_lines = 0;
25717
25718 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25719 doc: /* Normal hook run to update the menu bar definitions.
25720 Redisplay runs this hook before it redisplays the menu bar.
25721 This is used to update submenus such as Buffers,
25722 whose contents depend on various data. */);
25723 Vmenu_bar_update_hook = Qnil;
25724
25725 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25726 doc: /* Frame for which we are updating a menu.
25727 The enable predicate for a menu binding should check this variable. */);
25728 Vmenu_updating_frame = Qnil;
25729
25730 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25731 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25732 inhibit_menubar_update = 0;
25733
25734 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25735 doc: /* Prefix prepended to all continuation lines at display time.
25736 The value may be a string, an image, or a stretch-glyph; it is
25737 interpreted in the same way as the value of a `display' text property.
25738
25739 This variable is overridden by any `wrap-prefix' text or overlay
25740 property.
25741
25742 To add a prefix to non-continuation lines, use `line-prefix'. */);
25743 Vwrap_prefix = Qnil;
25744 staticpro (&Qwrap_prefix);
25745 Qwrap_prefix = intern_c_string ("wrap-prefix");
25746 Fmake_variable_buffer_local (Qwrap_prefix);
25747
25748 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25749 doc: /* Prefix prepended to all non-continuation lines at display time.
25750 The value may be a string, an image, or a stretch-glyph; it is
25751 interpreted in the same way as the value of a `display' text property.
25752
25753 This variable is overridden by any `line-prefix' text or overlay
25754 property.
25755
25756 To add a prefix to continuation lines, use `wrap-prefix'. */);
25757 Vline_prefix = Qnil;
25758 staticpro (&Qline_prefix);
25759 Qline_prefix = intern_c_string ("line-prefix");
25760 Fmake_variable_buffer_local (Qline_prefix);
25761
25762 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25763 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25764 inhibit_eval_during_redisplay = 0;
25765
25766 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25767 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25768 inhibit_free_realized_faces = 0;
25769
25770 #if GLYPH_DEBUG
25771 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25772 doc: /* Inhibit try_window_id display optimization. */);
25773 inhibit_try_window_id = 0;
25774
25775 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25776 doc: /* Inhibit try_window_reusing display optimization. */);
25777 inhibit_try_window_reusing = 0;
25778
25779 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25780 doc: /* Inhibit try_cursor_movement display optimization. */);
25781 inhibit_try_cursor_movement = 0;
25782 #endif /* GLYPH_DEBUG */
25783
25784 DEFVAR_INT ("overline-margin", &overline_margin,
25785 doc: /* *Space between overline and text, in pixels.
25786 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25787 margin to the caracter height. */);
25788 overline_margin = 2;
25789
25790 DEFVAR_INT ("underline-minimum-offset",
25791 &underline_minimum_offset,
25792 doc: /* Minimum distance between baseline and underline.
25793 This can improve legibility of underlined text at small font sizes,
25794 particularly when using variable `x-use-underline-position-properties'
25795 with fonts that specify an UNDERLINE_POSITION relatively close to the
25796 baseline. The default value is 1. */);
25797 underline_minimum_offset = 1;
25798
25799 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25800 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25801 display_hourglass_p = 1;
25802
25803 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25804 doc: /* *Seconds to wait before displaying an hourglass pointer.
25805 Value must be an integer or float. */);
25806 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25807
25808 hourglass_atimer = NULL;
25809 hourglass_shown_p = 0;
25810 }
25811
25812
25813 /* Initialize this module when Emacs starts. */
25814
25815 void
25816 init_xdisp ()
25817 {
25818 Lisp_Object root_window;
25819 struct window *mini_w;
25820
25821 current_header_line_height = current_mode_line_height = -1;
25822
25823 CHARPOS (this_line_start_pos) = 0;
25824
25825 mini_w = XWINDOW (minibuf_window);
25826 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25827
25828 if (!noninteractive)
25829 {
25830 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25831 int i;
25832
25833 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25834 set_window_height (root_window,
25835 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25836 0);
25837 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25838 set_window_height (minibuf_window, 1, 0);
25839
25840 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25841 mini_w->total_cols = make_number (FRAME_COLS (f));
25842
25843 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25844 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25845 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25846
25847 /* The default ellipsis glyphs `...'. */
25848 for (i = 0; i < 3; ++i)
25849 default_invis_vector[i] = make_number ('.');
25850 }
25851
25852 {
25853 /* Allocate the buffer for frame titles.
25854 Also used for `format-mode-line'. */
25855 int size = 100;
25856 mode_line_noprop_buf = (char *) xmalloc (size);
25857 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25858 mode_line_noprop_ptr = mode_line_noprop_buf;
25859 mode_line_target = MODE_LINE_DISPLAY;
25860 }
25861
25862 help_echo_showing_p = 0;
25863 }
25864
25865 /* Since w32 does not support atimers, it defines its own implementation of
25866 the following three functions in w32fns.c. */
25867 #ifndef WINDOWSNT
25868
25869 /* Platform-independent portion of hourglass implementation. */
25870
25871 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25872 int
25873 hourglass_started ()
25874 {
25875 return hourglass_shown_p || hourglass_atimer != NULL;
25876 }
25877
25878 /* Cancel a currently active hourglass timer, and start a new one. */
25879 void
25880 start_hourglass ()
25881 {
25882 #if defined (HAVE_WINDOW_SYSTEM)
25883 EMACS_TIME delay;
25884 int secs, usecs = 0;
25885
25886 cancel_hourglass ();
25887
25888 if (INTEGERP (Vhourglass_delay)
25889 && XINT (Vhourglass_delay) > 0)
25890 secs = XFASTINT (Vhourglass_delay);
25891 else if (FLOATP (Vhourglass_delay)
25892 && XFLOAT_DATA (Vhourglass_delay) > 0)
25893 {
25894 Lisp_Object tem;
25895 tem = Ftruncate (Vhourglass_delay, Qnil);
25896 secs = XFASTINT (tem);
25897 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25898 }
25899 else
25900 secs = DEFAULT_HOURGLASS_DELAY;
25901
25902 EMACS_SET_SECS_USECS (delay, secs, usecs);
25903 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25904 show_hourglass, NULL);
25905 #endif
25906 }
25907
25908
25909 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25910 shown. */
25911 void
25912 cancel_hourglass ()
25913 {
25914 #if defined (HAVE_WINDOW_SYSTEM)
25915 if (hourglass_atimer)
25916 {
25917 cancel_atimer (hourglass_atimer);
25918 hourglass_atimer = NULL;
25919 }
25920
25921 if (hourglass_shown_p)
25922 hide_hourglass ();
25923 #endif
25924 }
25925 #endif /* ! WINDOWSNT */
25926
25927 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25928 (do not change this comment) */