]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs-23 branch
[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_DISPLAY_VECTOR)
1381 {
1382 /* We stopped on the last glyph of a display vector.
1383 Try and recompute. Hack alert! */
1384 if (charpos < 2 || top.charpos >= charpos)
1385 top_x = it.glyph_row->x;
1386 else
1387 {
1388 struct it it2;
1389 start_display (&it2, w, top);
1390 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1391 get_next_display_element (&it2);
1392 PRODUCE_GLYPHS (&it2);
1393 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1394 || it2.current_x > it2.last_visible_x)
1395 top_x = it.glyph_row->x;
1396 else
1397 {
1398 top_x = it2.current_x;
1399 top_y = it2.current_y;
1400 }
1401 }
1402 }
1403
1404 *x = top_x;
1405 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1406 *rtop = max (0, window_top_y - top_y);
1407 *rbot = max (0, bottom_y - it.last_visible_y);
1408 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1409 - max (top_y, window_top_y)));
1410 *vpos = it.vpos;
1411 }
1412 }
1413 else
1414 {
1415 struct it it2;
1416
1417 it2 = it;
1418 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1419 move_it_by_lines (&it, 1, 0);
1420 if (charpos < IT_CHARPOS (it)
1421 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1422 {
1423 visible_p = 1;
1424 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1425 *x = it2.current_x;
1426 *y = it2.current_y + it2.max_ascent - it2.ascent;
1427 *rtop = max (0, -it2.current_y);
1428 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1429 - it.last_visible_y));
1430 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1431 it.last_visible_y)
1432 - max (it2.current_y,
1433 WINDOW_HEADER_LINE_HEIGHT (w))));
1434 *vpos = it2.vpos;
1435 }
1436 }
1437
1438 if (old_buffer)
1439 set_buffer_internal_1 (old_buffer);
1440
1441 current_header_line_height = current_mode_line_height = -1;
1442
1443 if (visible_p && XFASTINT (w->hscroll) > 0)
1444 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1445
1446 #if 0
1447 /* Debugging code. */
1448 if (visible_p)
1449 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1450 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1451 else
1452 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1453 #endif
1454
1455 return visible_p;
1456 }
1457
1458
1459 /* Return the next character from STR which is MAXLEN bytes long.
1460 Return in *LEN the length of the character. This is like
1461 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1462 we find one, we return a `?', but with the length of the invalid
1463 character. */
1464
1465 static INLINE int
1466 string_char_and_length (str, len)
1467 const unsigned char *str;
1468 int *len;
1469 {
1470 int c;
1471
1472 c = STRING_CHAR_AND_LENGTH (str, *len);
1473 if (!CHAR_VALID_P (c, 1))
1474 /* We may not change the length here because other places in Emacs
1475 don't use this function, i.e. they silently accept invalid
1476 characters. */
1477 c = '?';
1478
1479 return c;
1480 }
1481
1482
1483
1484 /* Given a position POS containing a valid character and byte position
1485 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1486
1487 static struct text_pos
1488 string_pos_nchars_ahead (pos, string, nchars)
1489 struct text_pos pos;
1490 Lisp_Object string;
1491 int nchars;
1492 {
1493 xassert (STRINGP (string) && nchars >= 0);
1494
1495 if (STRING_MULTIBYTE (string))
1496 {
1497 int rest = SBYTES (string) - BYTEPOS (pos);
1498 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1499 int len;
1500
1501 while (nchars--)
1502 {
1503 string_char_and_length (p, &len);
1504 p += len, rest -= len;
1505 xassert (rest >= 0);
1506 CHARPOS (pos) += 1;
1507 BYTEPOS (pos) += len;
1508 }
1509 }
1510 else
1511 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1512
1513 return pos;
1514 }
1515
1516
1517 /* Value is the text position, i.e. character and byte position,
1518 for character position CHARPOS in STRING. */
1519
1520 static INLINE struct text_pos
1521 string_pos (charpos, string)
1522 int charpos;
1523 Lisp_Object string;
1524 {
1525 struct text_pos pos;
1526 xassert (STRINGP (string));
1527 xassert (charpos >= 0);
1528 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1529 return pos;
1530 }
1531
1532
1533 /* Value is a text position, i.e. character and byte position, for
1534 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1535 means recognize multibyte characters. */
1536
1537 static struct text_pos
1538 c_string_pos (charpos, s, multibyte_p)
1539 int charpos;
1540 unsigned char *s;
1541 int multibyte_p;
1542 {
1543 struct text_pos pos;
1544
1545 xassert (s != NULL);
1546 xassert (charpos >= 0);
1547
1548 if (multibyte_p)
1549 {
1550 int rest = strlen (s), len;
1551
1552 SET_TEXT_POS (pos, 0, 0);
1553 while (charpos--)
1554 {
1555 string_char_and_length (s, &len);
1556 s += len, rest -= len;
1557 xassert (rest >= 0);
1558 CHARPOS (pos) += 1;
1559 BYTEPOS (pos) += len;
1560 }
1561 }
1562 else
1563 SET_TEXT_POS (pos, charpos, charpos);
1564
1565 return pos;
1566 }
1567
1568
1569 /* Value is the number of characters in C string S. MULTIBYTE_P
1570 non-zero means recognize multibyte characters. */
1571
1572 static int
1573 number_of_chars (s, multibyte_p)
1574 unsigned char *s;
1575 int multibyte_p;
1576 {
1577 int nchars;
1578
1579 if (multibyte_p)
1580 {
1581 int rest = strlen (s), len;
1582 unsigned char *p = (unsigned char *) s;
1583
1584 for (nchars = 0; rest > 0; ++nchars)
1585 {
1586 string_char_and_length (p, &len);
1587 rest -= len, p += len;
1588 }
1589 }
1590 else
1591 nchars = strlen (s);
1592
1593 return nchars;
1594 }
1595
1596
1597 /* Compute byte position NEWPOS->bytepos corresponding to
1598 NEWPOS->charpos. POS is a known position in string STRING.
1599 NEWPOS->charpos must be >= POS.charpos. */
1600
1601 static void
1602 compute_string_pos (newpos, pos, string)
1603 struct text_pos *newpos, pos;
1604 Lisp_Object string;
1605 {
1606 xassert (STRINGP (string));
1607 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1608
1609 if (STRING_MULTIBYTE (string))
1610 *newpos = string_pos_nchars_ahead (pos, string,
1611 CHARPOS (*newpos) - CHARPOS (pos));
1612 else
1613 BYTEPOS (*newpos) = CHARPOS (*newpos);
1614 }
1615
1616 /* EXPORT:
1617 Return an estimation of the pixel height of mode or header lines on
1618 frame F. FACE_ID specifies what line's height to estimate. */
1619
1620 int
1621 estimate_mode_line_height (f, face_id)
1622 struct frame *f;
1623 enum face_id face_id;
1624 {
1625 #ifdef HAVE_WINDOW_SYSTEM
1626 if (FRAME_WINDOW_P (f))
1627 {
1628 int height = FONT_HEIGHT (FRAME_FONT (f));
1629
1630 /* This function is called so early when Emacs starts that the face
1631 cache and mode line face are not yet initialized. */
1632 if (FRAME_FACE_CACHE (f))
1633 {
1634 struct face *face = FACE_FROM_ID (f, face_id);
1635 if (face)
1636 {
1637 if (face->font)
1638 height = FONT_HEIGHT (face->font);
1639 if (face->box_line_width > 0)
1640 height += 2 * face->box_line_width;
1641 }
1642 }
1643
1644 return height;
1645 }
1646 #endif
1647
1648 return 1;
1649 }
1650
1651 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1652 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1653 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1654 not force the value into range. */
1655
1656 void
1657 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1658 FRAME_PTR f;
1659 register int pix_x, pix_y;
1660 int *x, *y;
1661 NativeRectangle *bounds;
1662 int noclip;
1663 {
1664
1665 #ifdef HAVE_WINDOW_SYSTEM
1666 if (FRAME_WINDOW_P (f))
1667 {
1668 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1669 even for negative values. */
1670 if (pix_x < 0)
1671 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1672 if (pix_y < 0)
1673 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1674
1675 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1676 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1677
1678 if (bounds)
1679 STORE_NATIVE_RECT (*bounds,
1680 FRAME_COL_TO_PIXEL_X (f, pix_x),
1681 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1682 FRAME_COLUMN_WIDTH (f) - 1,
1683 FRAME_LINE_HEIGHT (f) - 1);
1684
1685 if (!noclip)
1686 {
1687 if (pix_x < 0)
1688 pix_x = 0;
1689 else if (pix_x > FRAME_TOTAL_COLS (f))
1690 pix_x = FRAME_TOTAL_COLS (f);
1691
1692 if (pix_y < 0)
1693 pix_y = 0;
1694 else if (pix_y > FRAME_LINES (f))
1695 pix_y = FRAME_LINES (f);
1696 }
1697 }
1698 #endif
1699
1700 *x = pix_x;
1701 *y = pix_y;
1702 }
1703
1704
1705 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1706 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1707 can't tell the positions because W's display is not up to date,
1708 return 0. */
1709
1710 int
1711 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1712 struct window *w;
1713 int hpos, vpos;
1714 int *frame_x, *frame_y;
1715 {
1716 #ifdef HAVE_WINDOW_SYSTEM
1717 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1718 {
1719 int success_p;
1720
1721 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1722 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1723
1724 if (display_completed)
1725 {
1726 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1727 struct glyph *glyph = row->glyphs[TEXT_AREA];
1728 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1729
1730 hpos = row->x;
1731 vpos = row->y;
1732 while (glyph < end)
1733 {
1734 hpos += glyph->pixel_width;
1735 ++glyph;
1736 }
1737
1738 /* If first glyph is partially visible, its first visible position is still 0. */
1739 if (hpos < 0)
1740 hpos = 0;
1741
1742 success_p = 1;
1743 }
1744 else
1745 {
1746 hpos = vpos = 0;
1747 success_p = 0;
1748 }
1749
1750 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1751 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1752 return success_p;
1753 }
1754 #endif
1755
1756 *frame_x = hpos;
1757 *frame_y = vpos;
1758 return 1;
1759 }
1760
1761
1762 #ifdef HAVE_WINDOW_SYSTEM
1763
1764 /* Find the glyph under window-relative coordinates X/Y in window W.
1765 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1766 strings. Return in *HPOS and *VPOS the row and column number of
1767 the glyph found. Return in *AREA the glyph area containing X.
1768 Value is a pointer to the glyph found or null if X/Y is not on
1769 text, or we can't tell because W's current matrix is not up to
1770 date. */
1771
1772 static
1773 struct glyph *
1774 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1775 struct window *w;
1776 int x, y;
1777 int *hpos, *vpos, *dx, *dy, *area;
1778 {
1779 struct glyph *glyph, *end;
1780 struct glyph_row *row = NULL;
1781 int x0, i;
1782
1783 /* Find row containing Y. Give up if some row is not enabled. */
1784 for (i = 0; i < w->current_matrix->nrows; ++i)
1785 {
1786 row = MATRIX_ROW (w->current_matrix, i);
1787 if (!row->enabled_p)
1788 return NULL;
1789 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1790 break;
1791 }
1792
1793 *vpos = i;
1794 *hpos = 0;
1795
1796 /* Give up if Y is not in the window. */
1797 if (i == w->current_matrix->nrows)
1798 return NULL;
1799
1800 /* Get the glyph area containing X. */
1801 if (w->pseudo_window_p)
1802 {
1803 *area = TEXT_AREA;
1804 x0 = 0;
1805 }
1806 else
1807 {
1808 if (x < window_box_left_offset (w, TEXT_AREA))
1809 {
1810 *area = LEFT_MARGIN_AREA;
1811 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1812 }
1813 else if (x < window_box_right_offset (w, TEXT_AREA))
1814 {
1815 *area = TEXT_AREA;
1816 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1817 }
1818 else
1819 {
1820 *area = RIGHT_MARGIN_AREA;
1821 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1822 }
1823 }
1824
1825 /* Find glyph containing X. */
1826 glyph = row->glyphs[*area];
1827 end = glyph + row->used[*area];
1828 x -= x0;
1829 while (glyph < end && x >= glyph->pixel_width)
1830 {
1831 x -= glyph->pixel_width;
1832 ++glyph;
1833 }
1834
1835 if (glyph == end)
1836 return NULL;
1837
1838 if (dx)
1839 {
1840 *dx = x;
1841 *dy = y - (row->y + row->ascent - glyph->ascent);
1842 }
1843
1844 *hpos = glyph - row->glyphs[*area];
1845 return glyph;
1846 }
1847
1848
1849 /* EXPORT:
1850 Convert frame-relative x/y to coordinates relative to window W.
1851 Takes pseudo-windows into account. */
1852
1853 void
1854 frame_to_window_pixel_xy (w, x, y)
1855 struct window *w;
1856 int *x, *y;
1857 {
1858 if (w->pseudo_window_p)
1859 {
1860 /* A pseudo-window is always full-width, and starts at the
1861 left edge of the frame, plus a frame border. */
1862 struct frame *f = XFRAME (w->frame);
1863 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1864 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1865 }
1866 else
1867 {
1868 *x -= WINDOW_LEFT_EDGE_X (w);
1869 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1870 }
1871 }
1872
1873 /* EXPORT:
1874 Return in RECTS[] at most N clipping rectangles for glyph string S.
1875 Return the number of stored rectangles. */
1876
1877 int
1878 get_glyph_string_clip_rects (s, rects, n)
1879 struct glyph_string *s;
1880 NativeRectangle *rects;
1881 int n;
1882 {
1883 XRectangle r;
1884
1885 if (n <= 0)
1886 return 0;
1887
1888 if (s->row->full_width_p)
1889 {
1890 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1891 r.x = WINDOW_LEFT_EDGE_X (s->w);
1892 r.width = WINDOW_TOTAL_WIDTH (s->w);
1893
1894 /* Unless displaying a mode or menu bar line, which are always
1895 fully visible, clip to the visible part of the row. */
1896 if (s->w->pseudo_window_p)
1897 r.height = s->row->visible_height;
1898 else
1899 r.height = s->height;
1900 }
1901 else
1902 {
1903 /* This is a text line that may be partially visible. */
1904 r.x = window_box_left (s->w, s->area);
1905 r.width = window_box_width (s->w, s->area);
1906 r.height = s->row->visible_height;
1907 }
1908
1909 if (s->clip_head)
1910 if (r.x < s->clip_head->x)
1911 {
1912 if (r.width >= s->clip_head->x - r.x)
1913 r.width -= s->clip_head->x - r.x;
1914 else
1915 r.width = 0;
1916 r.x = s->clip_head->x;
1917 }
1918 if (s->clip_tail)
1919 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1920 {
1921 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1922 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1923 else
1924 r.width = 0;
1925 }
1926
1927 /* If S draws overlapping rows, it's sufficient to use the top and
1928 bottom of the window for clipping because this glyph string
1929 intentionally draws over other lines. */
1930 if (s->for_overlaps)
1931 {
1932 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1933 r.height = window_text_bottom_y (s->w) - r.y;
1934
1935 /* Alas, the above simple strategy does not work for the
1936 environments with anti-aliased text: if the same text is
1937 drawn onto the same place multiple times, it gets thicker.
1938 If the overlap we are processing is for the erased cursor, we
1939 take the intersection with the rectagle of the cursor. */
1940 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1941 {
1942 XRectangle rc, r_save = r;
1943
1944 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1945 rc.y = s->w->phys_cursor.y;
1946 rc.width = s->w->phys_cursor_width;
1947 rc.height = s->w->phys_cursor_height;
1948
1949 x_intersect_rectangles (&r_save, &rc, &r);
1950 }
1951 }
1952 else
1953 {
1954 /* Don't use S->y for clipping because it doesn't take partially
1955 visible lines into account. For example, it can be negative for
1956 partially visible lines at the top of a window. */
1957 if (!s->row->full_width_p
1958 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1959 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1960 else
1961 r.y = max (0, s->row->y);
1962 }
1963
1964 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1965
1966 /* If drawing the cursor, don't let glyph draw outside its
1967 advertised boundaries. Cleartype does this under some circumstances. */
1968 if (s->hl == DRAW_CURSOR)
1969 {
1970 struct glyph *glyph = s->first_glyph;
1971 int height, max_y;
1972
1973 if (s->x > r.x)
1974 {
1975 r.width -= s->x - r.x;
1976 r.x = s->x;
1977 }
1978 r.width = min (r.width, glyph->pixel_width);
1979
1980 /* If r.y is below window bottom, ensure that we still see a cursor. */
1981 height = min (glyph->ascent + glyph->descent,
1982 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1983 max_y = window_text_bottom_y (s->w) - height;
1984 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1985 if (s->ybase - glyph->ascent > max_y)
1986 {
1987 r.y = max_y;
1988 r.height = height;
1989 }
1990 else
1991 {
1992 /* Don't draw cursor glyph taller than our actual glyph. */
1993 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1994 if (height < r.height)
1995 {
1996 max_y = r.y + r.height;
1997 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1998 r.height = min (max_y - r.y, height);
1999 }
2000 }
2001 }
2002
2003 if (s->row->clip)
2004 {
2005 XRectangle r_save = r;
2006
2007 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2008 r.width = 0;
2009 }
2010
2011 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2012 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2013 {
2014 #ifdef CONVERT_FROM_XRECT
2015 CONVERT_FROM_XRECT (r, *rects);
2016 #else
2017 *rects = r;
2018 #endif
2019 return 1;
2020 }
2021 else
2022 {
2023 /* If we are processing overlapping and allowed to return
2024 multiple clipping rectangles, we exclude the row of the glyph
2025 string from the clipping rectangle. This is to avoid drawing
2026 the same text on the environment with anti-aliasing. */
2027 #ifdef CONVERT_FROM_XRECT
2028 XRectangle rs[2];
2029 #else
2030 XRectangle *rs = rects;
2031 #endif
2032 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2033
2034 if (s->for_overlaps & OVERLAPS_PRED)
2035 {
2036 rs[i] = r;
2037 if (r.y + r.height > row_y)
2038 {
2039 if (r.y < row_y)
2040 rs[i].height = row_y - r.y;
2041 else
2042 rs[i].height = 0;
2043 }
2044 i++;
2045 }
2046 if (s->for_overlaps & OVERLAPS_SUCC)
2047 {
2048 rs[i] = r;
2049 if (r.y < row_y + s->row->visible_height)
2050 {
2051 if (r.y + r.height > row_y + s->row->visible_height)
2052 {
2053 rs[i].y = row_y + s->row->visible_height;
2054 rs[i].height = r.y + r.height - rs[i].y;
2055 }
2056 else
2057 rs[i].height = 0;
2058 }
2059 i++;
2060 }
2061
2062 n = i;
2063 #ifdef CONVERT_FROM_XRECT
2064 for (i = 0; i < n; i++)
2065 CONVERT_FROM_XRECT (rs[i], rects[i]);
2066 #endif
2067 return n;
2068 }
2069 }
2070
2071 /* EXPORT:
2072 Return in *NR the clipping rectangle for glyph string S. */
2073
2074 void
2075 get_glyph_string_clip_rect (s, nr)
2076 struct glyph_string *s;
2077 NativeRectangle *nr;
2078 {
2079 get_glyph_string_clip_rects (s, nr, 1);
2080 }
2081
2082
2083 /* EXPORT:
2084 Return the position and height of the phys cursor in window W.
2085 Set w->phys_cursor_width to width of phys cursor.
2086 */
2087
2088 void
2089 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2090 struct window *w;
2091 struct glyph_row *row;
2092 struct glyph *glyph;
2093 int *xp, *yp, *heightp;
2094 {
2095 struct frame *f = XFRAME (WINDOW_FRAME (w));
2096 int x, y, wd, h, h0, y0;
2097
2098 /* Compute the width of the rectangle to draw. If on a stretch
2099 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2100 rectangle as wide as the glyph, but use a canonical character
2101 width instead. */
2102 wd = glyph->pixel_width - 1;
2103 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2104 wd++; /* Why? */
2105 #endif
2106
2107 x = w->phys_cursor.x;
2108 if (x < 0)
2109 {
2110 wd += x;
2111 x = 0;
2112 }
2113
2114 if (glyph->type == STRETCH_GLYPH
2115 && !x_stretch_cursor_p)
2116 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2117 w->phys_cursor_width = wd;
2118
2119 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2120
2121 /* If y is below window bottom, ensure that we still see a cursor. */
2122 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2123
2124 h = max (h0, glyph->ascent + glyph->descent);
2125 h0 = min (h0, glyph->ascent + glyph->descent);
2126
2127 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2128 if (y < y0)
2129 {
2130 h = max (h - (y0 - y) + 1, h0);
2131 y = y0 - 1;
2132 }
2133 else
2134 {
2135 y0 = window_text_bottom_y (w) - h0;
2136 if (y > y0)
2137 {
2138 h += y - y0;
2139 y = y0;
2140 }
2141 }
2142
2143 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2144 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2145 *heightp = h;
2146 }
2147
2148 /*
2149 * Remember which glyph the mouse is over.
2150 */
2151
2152 void
2153 remember_mouse_glyph (f, gx, gy, rect)
2154 struct frame *f;
2155 int gx, gy;
2156 NativeRectangle *rect;
2157 {
2158 Lisp_Object window;
2159 struct window *w;
2160 struct glyph_row *r, *gr, *end_row;
2161 enum window_part part;
2162 enum glyph_row_area area;
2163 int x, y, width, height;
2164
2165 /* Try to determine frame pixel position and size of the glyph under
2166 frame pixel coordinates X/Y on frame F. */
2167
2168 if (!f->glyphs_initialized_p
2169 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2170 NILP (window)))
2171 {
2172 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2173 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2174 goto virtual_glyph;
2175 }
2176
2177 w = XWINDOW (window);
2178 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2179 height = WINDOW_FRAME_LINE_HEIGHT (w);
2180
2181 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2182 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2183
2184 if (w->pseudo_window_p)
2185 {
2186 area = TEXT_AREA;
2187 part = ON_MODE_LINE; /* Don't adjust margin. */
2188 goto text_glyph;
2189 }
2190
2191 switch (part)
2192 {
2193 case ON_LEFT_MARGIN:
2194 area = LEFT_MARGIN_AREA;
2195 goto text_glyph;
2196
2197 case ON_RIGHT_MARGIN:
2198 area = RIGHT_MARGIN_AREA;
2199 goto text_glyph;
2200
2201 case ON_HEADER_LINE:
2202 case ON_MODE_LINE:
2203 gr = (part == ON_HEADER_LINE
2204 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2205 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2206 gy = gr->y;
2207 area = TEXT_AREA;
2208 goto text_glyph_row_found;
2209
2210 case ON_TEXT:
2211 area = TEXT_AREA;
2212
2213 text_glyph:
2214 gr = 0; gy = 0;
2215 for (; r <= end_row && r->enabled_p; ++r)
2216 if (r->y + r->height > y)
2217 {
2218 gr = r; gy = r->y;
2219 break;
2220 }
2221
2222 text_glyph_row_found:
2223 if (gr && gy <= y)
2224 {
2225 struct glyph *g = gr->glyphs[area];
2226 struct glyph *end = g + gr->used[area];
2227
2228 height = gr->height;
2229 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2230 if (gx + g->pixel_width > x)
2231 break;
2232
2233 if (g < end)
2234 {
2235 if (g->type == IMAGE_GLYPH)
2236 {
2237 /* Don't remember when mouse is over image, as
2238 image may have hot-spots. */
2239 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2240 return;
2241 }
2242 width = g->pixel_width;
2243 }
2244 else
2245 {
2246 /* Use nominal char spacing at end of line. */
2247 x -= gx;
2248 gx += (x / width) * width;
2249 }
2250
2251 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2252 gx += window_box_left_offset (w, area);
2253 }
2254 else
2255 {
2256 /* Use nominal line height at end of window. */
2257 gx = (x / width) * width;
2258 y -= gy;
2259 gy += (y / height) * height;
2260 }
2261 break;
2262
2263 case ON_LEFT_FRINGE:
2264 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2265 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2266 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2267 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2268 goto row_glyph;
2269
2270 case ON_RIGHT_FRINGE:
2271 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2272 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2273 : window_box_right_offset (w, TEXT_AREA));
2274 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2275 goto row_glyph;
2276
2277 case ON_SCROLL_BAR:
2278 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2279 ? 0
2280 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2281 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2282 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2283 : 0)));
2284 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2285
2286 row_glyph:
2287 gr = 0, gy = 0;
2288 for (; r <= end_row && r->enabled_p; ++r)
2289 if (r->y + r->height > y)
2290 {
2291 gr = r; gy = r->y;
2292 break;
2293 }
2294
2295 if (gr && gy <= y)
2296 height = gr->height;
2297 else
2298 {
2299 /* Use nominal line height at end of window. */
2300 y -= gy;
2301 gy += (y / height) * height;
2302 }
2303 break;
2304
2305 default:
2306 ;
2307 virtual_glyph:
2308 /* If there is no glyph under the mouse, then we divide the screen
2309 into a grid of the smallest glyph in the frame, and use that
2310 as our "glyph". */
2311
2312 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2313 round down even for negative values. */
2314 if (gx < 0)
2315 gx -= width - 1;
2316 if (gy < 0)
2317 gy -= height - 1;
2318
2319 gx = (gx / width) * width;
2320 gy = (gy / height) * height;
2321
2322 goto store_rect;
2323 }
2324
2325 gx += WINDOW_LEFT_EDGE_X (w);
2326 gy += WINDOW_TOP_EDGE_Y (w);
2327
2328 store_rect:
2329 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2330
2331 /* Visible feedback for debugging. */
2332 #if 0
2333 #if HAVE_X_WINDOWS
2334 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2335 f->output_data.x->normal_gc,
2336 gx, gy, width, height);
2337 #endif
2338 #endif
2339 }
2340
2341
2342 #endif /* HAVE_WINDOW_SYSTEM */
2343
2344 \f
2345 /***********************************************************************
2346 Lisp form evaluation
2347 ***********************************************************************/
2348
2349 /* Error handler for safe_eval and safe_call. */
2350
2351 static Lisp_Object
2352 safe_eval_handler (arg)
2353 Lisp_Object arg;
2354 {
2355 add_to_log ("Error during redisplay: %s", arg, Qnil);
2356 return Qnil;
2357 }
2358
2359
2360 /* Evaluate SEXPR and return the result, or nil if something went
2361 wrong. Prevent redisplay during the evaluation. */
2362
2363 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2364 Return the result, or nil if something went wrong. Prevent
2365 redisplay during the evaluation. */
2366
2367 Lisp_Object
2368 safe_call (nargs, args)
2369 int nargs;
2370 Lisp_Object *args;
2371 {
2372 Lisp_Object val;
2373
2374 if (inhibit_eval_during_redisplay)
2375 val = Qnil;
2376 else
2377 {
2378 int count = SPECPDL_INDEX ();
2379 struct gcpro gcpro1;
2380
2381 GCPRO1 (args[0]);
2382 gcpro1.nvars = nargs;
2383 specbind (Qinhibit_redisplay, Qt);
2384 /* Use Qt to ensure debugger does not run,
2385 so there is no possibility of wanting to redisplay. */
2386 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2387 safe_eval_handler);
2388 UNGCPRO;
2389 val = unbind_to (count, val);
2390 }
2391
2392 return val;
2393 }
2394
2395
2396 /* Call function FN with one argument ARG.
2397 Return the result, or nil if something went wrong. */
2398
2399 Lisp_Object
2400 safe_call1 (fn, arg)
2401 Lisp_Object fn, arg;
2402 {
2403 Lisp_Object args[2];
2404 args[0] = fn;
2405 args[1] = arg;
2406 return safe_call (2, args);
2407 }
2408
2409 static Lisp_Object Qeval;
2410
2411 Lisp_Object
2412 safe_eval (Lisp_Object sexpr)
2413 {
2414 return safe_call1 (Qeval, sexpr);
2415 }
2416
2417 /* Call function FN with one argument ARG.
2418 Return the result, or nil if something went wrong. */
2419
2420 Lisp_Object
2421 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2422 {
2423 Lisp_Object args[3];
2424 args[0] = fn;
2425 args[1] = arg1;
2426 args[2] = arg2;
2427 return safe_call (3, args);
2428 }
2429
2430
2431 \f
2432 /***********************************************************************
2433 Debugging
2434 ***********************************************************************/
2435
2436 #if 0
2437
2438 /* Define CHECK_IT to perform sanity checks on iterators.
2439 This is for debugging. It is too slow to do unconditionally. */
2440
2441 static void
2442 check_it (it)
2443 struct it *it;
2444 {
2445 if (it->method == GET_FROM_STRING)
2446 {
2447 xassert (STRINGP (it->string));
2448 xassert (IT_STRING_CHARPOS (*it) >= 0);
2449 }
2450 else
2451 {
2452 xassert (IT_STRING_CHARPOS (*it) < 0);
2453 if (it->method == GET_FROM_BUFFER)
2454 {
2455 /* Check that character and byte positions agree. */
2456 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2457 }
2458 }
2459
2460 if (it->dpvec)
2461 xassert (it->current.dpvec_index >= 0);
2462 else
2463 xassert (it->current.dpvec_index < 0);
2464 }
2465
2466 #define CHECK_IT(IT) check_it ((IT))
2467
2468 #else /* not 0 */
2469
2470 #define CHECK_IT(IT) (void) 0
2471
2472 #endif /* not 0 */
2473
2474
2475 #if GLYPH_DEBUG
2476
2477 /* Check that the window end of window W is what we expect it
2478 to be---the last row in the current matrix displaying text. */
2479
2480 static void
2481 check_window_end (w)
2482 struct window *w;
2483 {
2484 if (!MINI_WINDOW_P (w)
2485 && !NILP (w->window_end_valid))
2486 {
2487 struct glyph_row *row;
2488 xassert ((row = MATRIX_ROW (w->current_matrix,
2489 XFASTINT (w->window_end_vpos)),
2490 !row->enabled_p
2491 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2492 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2493 }
2494 }
2495
2496 #define CHECK_WINDOW_END(W) check_window_end ((W))
2497
2498 #else /* not GLYPH_DEBUG */
2499
2500 #define CHECK_WINDOW_END(W) (void) 0
2501
2502 #endif /* not GLYPH_DEBUG */
2503
2504
2505 \f
2506 /***********************************************************************
2507 Iterator initialization
2508 ***********************************************************************/
2509
2510 /* Initialize IT for displaying current_buffer in window W, starting
2511 at character position CHARPOS. CHARPOS < 0 means that no buffer
2512 position is specified which is useful when the iterator is assigned
2513 a position later. BYTEPOS is the byte position corresponding to
2514 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2515
2516 If ROW is not null, calls to produce_glyphs with IT as parameter
2517 will produce glyphs in that row.
2518
2519 BASE_FACE_ID is the id of a base face to use. It must be one of
2520 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2521 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2522 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2523
2524 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2525 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2526 will be initialized to use the corresponding mode line glyph row of
2527 the desired matrix of W. */
2528
2529 void
2530 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2531 struct it *it;
2532 struct window *w;
2533 int charpos, bytepos;
2534 struct glyph_row *row;
2535 enum face_id base_face_id;
2536 {
2537 int highlight_region_p;
2538 enum face_id remapped_base_face_id = base_face_id;
2539
2540 /* Some precondition checks. */
2541 xassert (w != NULL && it != NULL);
2542 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2543 && charpos <= ZV));
2544
2545 /* If face attributes have been changed since the last redisplay,
2546 free realized faces now because they depend on face definitions
2547 that might have changed. Don't free faces while there might be
2548 desired matrices pending which reference these faces. */
2549 if (face_change_count && !inhibit_free_realized_faces)
2550 {
2551 face_change_count = 0;
2552 free_all_realized_faces (Qnil);
2553 }
2554
2555 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2556 if (! NILP (Vface_remapping_alist))
2557 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2558
2559 /* Use one of the mode line rows of W's desired matrix if
2560 appropriate. */
2561 if (row == NULL)
2562 {
2563 if (base_face_id == MODE_LINE_FACE_ID
2564 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2565 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2566 else if (base_face_id == HEADER_LINE_FACE_ID)
2567 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2568 }
2569
2570 /* Clear IT. */
2571 bzero (it, sizeof *it);
2572 it->current.overlay_string_index = -1;
2573 it->current.dpvec_index = -1;
2574 it->base_face_id = remapped_base_face_id;
2575 it->string = Qnil;
2576 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2577
2578 /* The window in which we iterate over current_buffer: */
2579 XSETWINDOW (it->window, w);
2580 it->w = w;
2581 it->f = XFRAME (w->frame);
2582
2583 it->cmp_it.id = -1;
2584
2585 /* Extra space between lines (on window systems only). */
2586 if (base_face_id == DEFAULT_FACE_ID
2587 && FRAME_WINDOW_P (it->f))
2588 {
2589 if (NATNUMP (current_buffer->extra_line_spacing))
2590 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2591 else if (FLOATP (current_buffer->extra_line_spacing))
2592 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2593 * FRAME_LINE_HEIGHT (it->f));
2594 else if (it->f->extra_line_spacing > 0)
2595 it->extra_line_spacing = it->f->extra_line_spacing;
2596 it->max_extra_line_spacing = 0;
2597 }
2598
2599 /* If realized faces have been removed, e.g. because of face
2600 attribute changes of named faces, recompute them. When running
2601 in batch mode, the face cache of the initial frame is null. If
2602 we happen to get called, make a dummy face cache. */
2603 if (FRAME_FACE_CACHE (it->f) == NULL)
2604 init_frame_faces (it->f);
2605 if (FRAME_FACE_CACHE (it->f)->used == 0)
2606 recompute_basic_faces (it->f);
2607
2608 /* Current value of the `slice', `space-width', and 'height' properties. */
2609 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2610 it->space_width = Qnil;
2611 it->font_height = Qnil;
2612 it->override_ascent = -1;
2613
2614 /* Are control characters displayed as `^C'? */
2615 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2616
2617 /* -1 means everything between a CR and the following line end
2618 is invisible. >0 means lines indented more than this value are
2619 invisible. */
2620 it->selective = (INTEGERP (current_buffer->selective_display)
2621 ? XFASTINT (current_buffer->selective_display)
2622 : (!NILP (current_buffer->selective_display)
2623 ? -1 : 0));
2624 it->selective_display_ellipsis_p
2625 = !NILP (current_buffer->selective_display_ellipses);
2626
2627 /* Display table to use. */
2628 it->dp = window_display_table (w);
2629
2630 /* Are multibyte characters enabled in current_buffer? */
2631 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2632
2633 /* Do we need to reorder bidirectional text? */
2634 it->bidi_p = !NILP (current_buffer->bidi_display_reordering);
2635
2636 /* Non-zero if we should highlight the region. */
2637 highlight_region_p
2638 = (!NILP (Vtransient_mark_mode)
2639 && !NILP (current_buffer->mark_active)
2640 && XMARKER (current_buffer->mark)->buffer != 0);
2641
2642 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2643 start and end of a visible region in window IT->w. Set both to
2644 -1 to indicate no region. */
2645 if (highlight_region_p
2646 /* Maybe highlight only in selected window. */
2647 && (/* Either show region everywhere. */
2648 highlight_nonselected_windows
2649 /* Or show region in the selected window. */
2650 || w == XWINDOW (selected_window)
2651 /* Or show the region if we are in the mini-buffer and W is
2652 the window the mini-buffer refers to. */
2653 || (MINI_WINDOW_P (XWINDOW (selected_window))
2654 && WINDOWP (minibuf_selected_window)
2655 && w == XWINDOW (minibuf_selected_window))))
2656 {
2657 int charpos = marker_position (current_buffer->mark);
2658 it->region_beg_charpos = min (PT, charpos);
2659 it->region_end_charpos = max (PT, charpos);
2660 }
2661 else
2662 it->region_beg_charpos = it->region_end_charpos = -1;
2663
2664 /* Get the position at which the redisplay_end_trigger hook should
2665 be run, if it is to be run at all. */
2666 if (MARKERP (w->redisplay_end_trigger)
2667 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2668 it->redisplay_end_trigger_charpos
2669 = marker_position (w->redisplay_end_trigger);
2670 else if (INTEGERP (w->redisplay_end_trigger))
2671 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2672
2673 /* Correct bogus values of tab_width. */
2674 it->tab_width = XINT (current_buffer->tab_width);
2675 if (it->tab_width <= 0 || it->tab_width > 1000)
2676 it->tab_width = 8;
2677
2678 /* Are lines in the display truncated? */
2679 if (base_face_id != DEFAULT_FACE_ID
2680 || XINT (it->w->hscroll)
2681 || (! WINDOW_FULL_WIDTH_P (it->w)
2682 && ((!NILP (Vtruncate_partial_width_windows)
2683 && !INTEGERP (Vtruncate_partial_width_windows))
2684 || (INTEGERP (Vtruncate_partial_width_windows)
2685 && (WINDOW_TOTAL_COLS (it->w)
2686 < XINT (Vtruncate_partial_width_windows))))))
2687 it->line_wrap = TRUNCATE;
2688 else if (NILP (current_buffer->truncate_lines))
2689 it->line_wrap = NILP (current_buffer->word_wrap)
2690 ? WINDOW_WRAP : WORD_WRAP;
2691 else
2692 it->line_wrap = TRUNCATE;
2693
2694 /* Get dimensions of truncation and continuation glyphs. These are
2695 displayed as fringe bitmaps under X, so we don't need them for such
2696 frames. */
2697 if (!FRAME_WINDOW_P (it->f))
2698 {
2699 if (it->line_wrap == TRUNCATE)
2700 {
2701 /* We will need the truncation glyph. */
2702 xassert (it->glyph_row == NULL);
2703 produce_special_glyphs (it, IT_TRUNCATION);
2704 it->truncation_pixel_width = it->pixel_width;
2705 }
2706 else
2707 {
2708 /* We will need the continuation glyph. */
2709 xassert (it->glyph_row == NULL);
2710 produce_special_glyphs (it, IT_CONTINUATION);
2711 it->continuation_pixel_width = it->pixel_width;
2712 }
2713
2714 /* Reset these values to zero because the produce_special_glyphs
2715 above has changed them. */
2716 it->pixel_width = it->ascent = it->descent = 0;
2717 it->phys_ascent = it->phys_descent = 0;
2718 }
2719
2720 /* Set this after getting the dimensions of truncation and
2721 continuation glyphs, so that we don't produce glyphs when calling
2722 produce_special_glyphs, above. */
2723 it->glyph_row = row;
2724 it->area = TEXT_AREA;
2725
2726 /* Forget any previous info about this row being reversed. */
2727 if (it->glyph_row)
2728 it->glyph_row->reversed_p = 0;
2729
2730 /* Get the dimensions of the display area. The display area
2731 consists of the visible window area plus a horizontally scrolled
2732 part to the left of the window. All x-values are relative to the
2733 start of this total display area. */
2734 if (base_face_id != DEFAULT_FACE_ID)
2735 {
2736 /* Mode lines, menu bar in terminal frames. */
2737 it->first_visible_x = 0;
2738 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2739 }
2740 else
2741 {
2742 it->first_visible_x
2743 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2744 it->last_visible_x = (it->first_visible_x
2745 + window_box_width (w, TEXT_AREA));
2746
2747 /* If we truncate lines, leave room for the truncator glyph(s) at
2748 the right margin. Otherwise, leave room for the continuation
2749 glyph(s). Truncation and continuation glyphs are not inserted
2750 for window-based redisplay. */
2751 if (!FRAME_WINDOW_P (it->f))
2752 {
2753 if (it->line_wrap == TRUNCATE)
2754 it->last_visible_x -= it->truncation_pixel_width;
2755 else
2756 it->last_visible_x -= it->continuation_pixel_width;
2757 }
2758
2759 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2760 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2761 }
2762
2763 /* Leave room for a border glyph. */
2764 if (!FRAME_WINDOW_P (it->f)
2765 && !WINDOW_RIGHTMOST_P (it->w))
2766 it->last_visible_x -= 1;
2767
2768 it->last_visible_y = window_text_bottom_y (w);
2769
2770 /* For mode lines and alike, arrange for the first glyph having a
2771 left box line if the face specifies a box. */
2772 if (base_face_id != DEFAULT_FACE_ID)
2773 {
2774 struct face *face;
2775
2776 it->face_id = remapped_base_face_id;
2777
2778 /* If we have a boxed mode line, make the first character appear
2779 with a left box line. */
2780 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2781 if (face->box != FACE_NO_BOX)
2782 it->start_of_box_run_p = 1;
2783 }
2784
2785 /* If we are to reorder bidirectional text, init the bidi
2786 iterator. */
2787 if (it->bidi_p)
2788 {
2789 /* Note the paragraph direction that this buffer wants to
2790 use. */
2791 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2792 it->paragraph_embedding = L2R;
2793 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2794 it->paragraph_embedding = R2L;
2795 else
2796 it->paragraph_embedding = NEUTRAL_DIR;
2797 bidi_init_it (charpos, bytepos, &it->bidi_it);
2798 }
2799
2800 /* If a buffer position was specified, set the iterator there,
2801 getting overlays and face properties from that position. */
2802 if (charpos >= BUF_BEG (current_buffer))
2803 {
2804 it->end_charpos = ZV;
2805 it->face_id = -1;
2806 IT_CHARPOS (*it) = charpos;
2807
2808 /* Compute byte position if not specified. */
2809 if (bytepos < charpos)
2810 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2811 else
2812 IT_BYTEPOS (*it) = bytepos;
2813
2814 it->start = it->current;
2815
2816 /* Compute faces etc. */
2817 reseat (it, it->current.pos, 1);
2818 }
2819
2820 CHECK_IT (it);
2821 }
2822
2823
2824 /* Initialize IT for the display of window W with window start POS. */
2825
2826 void
2827 start_display (it, w, pos)
2828 struct it *it;
2829 struct window *w;
2830 struct text_pos pos;
2831 {
2832 struct glyph_row *row;
2833 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2834
2835 row = w->desired_matrix->rows + first_vpos;
2836 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2837 it->first_vpos = first_vpos;
2838
2839 /* Don't reseat to previous visible line start if current start
2840 position is in a string or image. */
2841 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2842 {
2843 int start_at_line_beg_p;
2844 int first_y = it->current_y;
2845
2846 /* If window start is not at a line start, skip forward to POS to
2847 get the correct continuation lines width. */
2848 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2849 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2850 if (!start_at_line_beg_p)
2851 {
2852 int new_x;
2853
2854 reseat_at_previous_visible_line_start (it);
2855 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2856
2857 new_x = it->current_x + it->pixel_width;
2858
2859 /* If lines are continued, this line may end in the middle
2860 of a multi-glyph character (e.g. a control character
2861 displayed as \003, or in the middle of an overlay
2862 string). In this case move_it_to above will not have
2863 taken us to the start of the continuation line but to the
2864 end of the continued line. */
2865 if (it->current_x > 0
2866 && it->line_wrap != TRUNCATE /* Lines are continued. */
2867 && (/* And glyph doesn't fit on the line. */
2868 new_x > it->last_visible_x
2869 /* Or it fits exactly and we're on a window
2870 system frame. */
2871 || (new_x == it->last_visible_x
2872 && FRAME_WINDOW_P (it->f))))
2873 {
2874 if (it->current.dpvec_index >= 0
2875 || it->current.overlay_string_index >= 0)
2876 {
2877 set_iterator_to_next (it, 1);
2878 move_it_in_display_line_to (it, -1, -1, 0);
2879 }
2880
2881 it->continuation_lines_width += it->current_x;
2882 }
2883
2884 /* We're starting a new display line, not affected by the
2885 height of the continued line, so clear the appropriate
2886 fields in the iterator structure. */
2887 it->max_ascent = it->max_descent = 0;
2888 it->max_phys_ascent = it->max_phys_descent = 0;
2889
2890 it->current_y = first_y;
2891 it->vpos = 0;
2892 it->current_x = it->hpos = 0;
2893 }
2894 }
2895 }
2896
2897
2898 /* Return 1 if POS is a position in ellipses displayed for invisible
2899 text. W is the window we display, for text property lookup. */
2900
2901 static int
2902 in_ellipses_for_invisible_text_p (pos, w)
2903 struct display_pos *pos;
2904 struct window *w;
2905 {
2906 Lisp_Object prop, window;
2907 int ellipses_p = 0;
2908 int charpos = CHARPOS (pos->pos);
2909
2910 /* If POS specifies a position in a display vector, this might
2911 be for an ellipsis displayed for invisible text. We won't
2912 get the iterator set up for delivering that ellipsis unless
2913 we make sure that it gets aware of the invisible text. */
2914 if (pos->dpvec_index >= 0
2915 && pos->overlay_string_index < 0
2916 && CHARPOS (pos->string_pos) < 0
2917 && charpos > BEGV
2918 && (XSETWINDOW (window, w),
2919 prop = Fget_char_property (make_number (charpos),
2920 Qinvisible, window),
2921 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2922 {
2923 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2924 window);
2925 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2926 }
2927
2928 return ellipses_p;
2929 }
2930
2931
2932 /* Initialize IT for stepping through current_buffer in window W,
2933 starting at position POS that includes overlay string and display
2934 vector/ control character translation position information. Value
2935 is zero if there are overlay strings with newlines at POS. */
2936
2937 static int
2938 init_from_display_pos (it, w, pos)
2939 struct it *it;
2940 struct window *w;
2941 struct display_pos *pos;
2942 {
2943 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2944 int i, overlay_strings_with_newlines = 0;
2945
2946 /* If POS specifies a position in a display vector, this might
2947 be for an ellipsis displayed for invisible text. We won't
2948 get the iterator set up for delivering that ellipsis unless
2949 we make sure that it gets aware of the invisible text. */
2950 if (in_ellipses_for_invisible_text_p (pos, w))
2951 {
2952 --charpos;
2953 bytepos = 0;
2954 }
2955
2956 /* Keep in mind: the call to reseat in init_iterator skips invisible
2957 text, so we might end up at a position different from POS. This
2958 is only a problem when POS is a row start after a newline and an
2959 overlay starts there with an after-string, and the overlay has an
2960 invisible property. Since we don't skip invisible text in
2961 display_line and elsewhere immediately after consuming the
2962 newline before the row start, such a POS will not be in a string,
2963 but the call to init_iterator below will move us to the
2964 after-string. */
2965 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2966
2967 /* This only scans the current chunk -- it should scan all chunks.
2968 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2969 to 16 in 22.1 to make this a lesser problem. */
2970 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2971 {
2972 const char *s = SDATA (it->overlay_strings[i]);
2973 const char *e = s + SBYTES (it->overlay_strings[i]);
2974
2975 while (s < e && *s != '\n')
2976 ++s;
2977
2978 if (s < e)
2979 {
2980 overlay_strings_with_newlines = 1;
2981 break;
2982 }
2983 }
2984
2985 /* If position is within an overlay string, set up IT to the right
2986 overlay string. */
2987 if (pos->overlay_string_index >= 0)
2988 {
2989 int relative_index;
2990
2991 /* If the first overlay string happens to have a `display'
2992 property for an image, the iterator will be set up for that
2993 image, and we have to undo that setup first before we can
2994 correct the overlay string index. */
2995 if (it->method == GET_FROM_IMAGE)
2996 pop_it (it);
2997
2998 /* We already have the first chunk of overlay strings in
2999 IT->overlay_strings. Load more until the one for
3000 pos->overlay_string_index is in IT->overlay_strings. */
3001 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3002 {
3003 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3004 it->current.overlay_string_index = 0;
3005 while (n--)
3006 {
3007 load_overlay_strings (it, 0);
3008 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3009 }
3010 }
3011
3012 it->current.overlay_string_index = pos->overlay_string_index;
3013 relative_index = (it->current.overlay_string_index
3014 % OVERLAY_STRING_CHUNK_SIZE);
3015 it->string = it->overlay_strings[relative_index];
3016 xassert (STRINGP (it->string));
3017 it->current.string_pos = pos->string_pos;
3018 it->method = GET_FROM_STRING;
3019 }
3020
3021 if (CHARPOS (pos->string_pos) >= 0)
3022 {
3023 /* Recorded position is not in an overlay string, but in another
3024 string. This can only be a string from a `display' property.
3025 IT should already be filled with that string. */
3026 it->current.string_pos = pos->string_pos;
3027 xassert (STRINGP (it->string));
3028 }
3029
3030 /* Restore position in display vector translations, control
3031 character translations or ellipses. */
3032 if (pos->dpvec_index >= 0)
3033 {
3034 if (it->dpvec == NULL)
3035 get_next_display_element (it);
3036 xassert (it->dpvec && it->current.dpvec_index == 0);
3037 it->current.dpvec_index = pos->dpvec_index;
3038 }
3039
3040 CHECK_IT (it);
3041 return !overlay_strings_with_newlines;
3042 }
3043
3044
3045 /* Initialize IT for stepping through current_buffer in window W
3046 starting at ROW->start. */
3047
3048 static void
3049 init_to_row_start (it, w, row)
3050 struct it *it;
3051 struct window *w;
3052 struct glyph_row *row;
3053 {
3054 init_from_display_pos (it, w, &row->start);
3055 it->start = row->start;
3056 it->continuation_lines_width = row->continuation_lines_width;
3057 CHECK_IT (it);
3058 }
3059
3060
3061 /* Initialize IT for stepping through current_buffer in window W
3062 starting in the line following ROW, i.e. starting at ROW->end.
3063 Value is zero if there are overlay strings with newlines at ROW's
3064 end position. */
3065
3066 static int
3067 init_to_row_end (it, w, row)
3068 struct it *it;
3069 struct window *w;
3070 struct glyph_row *row;
3071 {
3072 int success = 0;
3073
3074 if (init_from_display_pos (it, w, &row->end))
3075 {
3076 if (row->continued_p)
3077 it->continuation_lines_width
3078 = row->continuation_lines_width + row->pixel_width;
3079 CHECK_IT (it);
3080 success = 1;
3081 }
3082
3083 return success;
3084 }
3085
3086
3087
3088 \f
3089 /***********************************************************************
3090 Text properties
3091 ***********************************************************************/
3092
3093 /* Called when IT reaches IT->stop_charpos. Handle text property and
3094 overlay changes. Set IT->stop_charpos to the next position where
3095 to stop. */
3096
3097 static void
3098 handle_stop (it)
3099 struct it *it;
3100 {
3101 enum prop_handled handled;
3102 int handle_overlay_change_p;
3103 struct props *p;
3104
3105 it->dpvec = NULL;
3106 it->current.dpvec_index = -1;
3107 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3108 it->ignore_overlay_strings_at_pos_p = 0;
3109 it->ellipsis_p = 0;
3110
3111 /* Use face of preceding text for ellipsis (if invisible) */
3112 if (it->selective_display_ellipsis_p)
3113 it->saved_face_id = it->face_id;
3114
3115 do
3116 {
3117 handled = HANDLED_NORMALLY;
3118
3119 /* Call text property handlers. */
3120 for (p = it_props; p->handler; ++p)
3121 {
3122 handled = p->handler (it);
3123
3124 if (handled == HANDLED_RECOMPUTE_PROPS)
3125 break;
3126 else if (handled == HANDLED_RETURN)
3127 {
3128 /* We still want to show before and after strings from
3129 overlays even if the actual buffer text is replaced. */
3130 if (!handle_overlay_change_p
3131 || it->sp > 1
3132 || !get_overlay_strings_1 (it, 0, 0))
3133 {
3134 if (it->ellipsis_p)
3135 setup_for_ellipsis (it, 0);
3136 /* When handling a display spec, we might load an
3137 empty string. In that case, discard it here. We
3138 used to discard it in handle_single_display_spec,
3139 but that causes get_overlay_strings_1, above, to
3140 ignore overlay strings that we must check. */
3141 if (STRINGP (it->string) && !SCHARS (it->string))
3142 pop_it (it);
3143 return;
3144 }
3145 else if (STRINGP (it->string) && !SCHARS (it->string))
3146 pop_it (it);
3147 else
3148 {
3149 it->ignore_overlay_strings_at_pos_p = 1;
3150 it->string_from_display_prop_p = 0;
3151 handle_overlay_change_p = 0;
3152 }
3153 handled = HANDLED_RECOMPUTE_PROPS;
3154 break;
3155 }
3156 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3157 handle_overlay_change_p = 0;
3158 }
3159
3160 if (handled != HANDLED_RECOMPUTE_PROPS)
3161 {
3162 /* Don't check for overlay strings below when set to deliver
3163 characters from a display vector. */
3164 if (it->method == GET_FROM_DISPLAY_VECTOR)
3165 handle_overlay_change_p = 0;
3166
3167 /* Handle overlay changes.
3168 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3169 if it finds overlays. */
3170 if (handle_overlay_change_p)
3171 handled = handle_overlay_change (it);
3172 }
3173
3174 if (it->ellipsis_p)
3175 {
3176 setup_for_ellipsis (it, 0);
3177 break;
3178 }
3179 }
3180 while (handled == HANDLED_RECOMPUTE_PROPS);
3181
3182 /* Determine where to stop next. */
3183 if (handled == HANDLED_NORMALLY)
3184 compute_stop_pos (it);
3185 }
3186
3187
3188 /* Compute IT->stop_charpos from text property and overlay change
3189 information for IT's current position. */
3190
3191 static void
3192 compute_stop_pos (it)
3193 struct it *it;
3194 {
3195 register INTERVAL iv, next_iv;
3196 Lisp_Object object, limit, position;
3197 EMACS_INT charpos, bytepos;
3198
3199 /* If nowhere else, stop at the end. */
3200 it->stop_charpos = it->end_charpos;
3201
3202 if (STRINGP (it->string))
3203 {
3204 /* Strings are usually short, so don't limit the search for
3205 properties. */
3206 object = it->string;
3207 limit = Qnil;
3208 charpos = IT_STRING_CHARPOS (*it);
3209 bytepos = IT_STRING_BYTEPOS (*it);
3210 }
3211 else
3212 {
3213 EMACS_INT pos;
3214
3215 /* If next overlay change is in front of the current stop pos
3216 (which is IT->end_charpos), stop there. Note: value of
3217 next_overlay_change is point-max if no overlay change
3218 follows. */
3219 charpos = IT_CHARPOS (*it);
3220 bytepos = IT_BYTEPOS (*it);
3221 pos = next_overlay_change (charpos);
3222 if (pos < it->stop_charpos)
3223 it->stop_charpos = pos;
3224
3225 /* If showing the region, we have to stop at the region
3226 start or end because the face might change there. */
3227 if (it->region_beg_charpos > 0)
3228 {
3229 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3230 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3231 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3232 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3233 }
3234
3235 /* Set up variables for computing the stop position from text
3236 property changes. */
3237 XSETBUFFER (object, current_buffer);
3238 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3239 }
3240
3241 /* Get the interval containing IT's position. Value is a null
3242 interval if there isn't such an interval. */
3243 position = make_number (charpos);
3244 iv = validate_interval_range (object, &position, &position, 0);
3245 if (!NULL_INTERVAL_P (iv))
3246 {
3247 Lisp_Object values_here[LAST_PROP_IDX];
3248 struct props *p;
3249
3250 /* Get properties here. */
3251 for (p = it_props; p->handler; ++p)
3252 values_here[p->idx] = textget (iv->plist, *p->name);
3253
3254 /* Look for an interval following iv that has different
3255 properties. */
3256 for (next_iv = next_interval (iv);
3257 (!NULL_INTERVAL_P (next_iv)
3258 && (NILP (limit)
3259 || XFASTINT (limit) > next_iv->position));
3260 next_iv = next_interval (next_iv))
3261 {
3262 for (p = it_props; p->handler; ++p)
3263 {
3264 Lisp_Object new_value;
3265
3266 new_value = textget (next_iv->plist, *p->name);
3267 if (!EQ (values_here[p->idx], new_value))
3268 break;
3269 }
3270
3271 if (p->handler)
3272 break;
3273 }
3274
3275 if (!NULL_INTERVAL_P (next_iv))
3276 {
3277 if (INTEGERP (limit)
3278 && next_iv->position >= XFASTINT (limit))
3279 /* No text property change up to limit. */
3280 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3281 else
3282 /* Text properties change in next_iv. */
3283 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3284 }
3285 }
3286
3287 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3288 it->stop_charpos, it->string);
3289
3290 xassert (STRINGP (it->string)
3291 || (it->stop_charpos >= BEGV
3292 && it->stop_charpos >= IT_CHARPOS (*it)));
3293 }
3294
3295
3296 /* Return the position of the next overlay change after POS in
3297 current_buffer. Value is point-max if no overlay change
3298 follows. This is like `next-overlay-change' but doesn't use
3299 xmalloc. */
3300
3301 static EMACS_INT
3302 next_overlay_change (pos)
3303 EMACS_INT pos;
3304 {
3305 int noverlays;
3306 EMACS_INT endpos;
3307 Lisp_Object *overlays;
3308 int i;
3309
3310 /* Get all overlays at the given position. */
3311 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3312
3313 /* If any of these overlays ends before endpos,
3314 use its ending point instead. */
3315 for (i = 0; i < noverlays; ++i)
3316 {
3317 Lisp_Object oend;
3318 EMACS_INT oendpos;
3319
3320 oend = OVERLAY_END (overlays[i]);
3321 oendpos = OVERLAY_POSITION (oend);
3322 endpos = min (endpos, oendpos);
3323 }
3324
3325 return endpos;
3326 }
3327
3328
3329 \f
3330 /***********************************************************************
3331 Fontification
3332 ***********************************************************************/
3333
3334 /* Handle changes in the `fontified' property of the current buffer by
3335 calling hook functions from Qfontification_functions to fontify
3336 regions of text. */
3337
3338 static enum prop_handled
3339 handle_fontified_prop (it)
3340 struct it *it;
3341 {
3342 Lisp_Object prop, pos;
3343 enum prop_handled handled = HANDLED_NORMALLY;
3344
3345 if (!NILP (Vmemory_full))
3346 return handled;
3347
3348 /* Get the value of the `fontified' property at IT's current buffer
3349 position. (The `fontified' property doesn't have a special
3350 meaning in strings.) If the value is nil, call functions from
3351 Qfontification_functions. */
3352 if (!STRINGP (it->string)
3353 && it->s == NULL
3354 && !NILP (Vfontification_functions)
3355 && !NILP (Vrun_hooks)
3356 && (pos = make_number (IT_CHARPOS (*it)),
3357 prop = Fget_char_property (pos, Qfontified, Qnil),
3358 /* Ignore the special cased nil value always present at EOB since
3359 no amount of fontifying will be able to change it. */
3360 NILP (prop) && IT_CHARPOS (*it) < Z))
3361 {
3362 int count = SPECPDL_INDEX ();
3363 Lisp_Object val;
3364
3365 val = Vfontification_functions;
3366 specbind (Qfontification_functions, Qnil);
3367
3368 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3369 safe_call1 (val, pos);
3370 else
3371 {
3372 Lisp_Object globals, fn;
3373 struct gcpro gcpro1, gcpro2;
3374
3375 globals = Qnil;
3376 GCPRO2 (val, globals);
3377
3378 for (; CONSP (val); val = XCDR (val))
3379 {
3380 fn = XCAR (val);
3381
3382 if (EQ (fn, Qt))
3383 {
3384 /* A value of t indicates this hook has a local
3385 binding; it means to run the global binding too.
3386 In a global value, t should not occur. If it
3387 does, we must ignore it to avoid an endless
3388 loop. */
3389 for (globals = Fdefault_value (Qfontification_functions);
3390 CONSP (globals);
3391 globals = XCDR (globals))
3392 {
3393 fn = XCAR (globals);
3394 if (!EQ (fn, Qt))
3395 safe_call1 (fn, pos);
3396 }
3397 }
3398 else
3399 safe_call1 (fn, pos);
3400 }
3401
3402 UNGCPRO;
3403 }
3404
3405 unbind_to (count, Qnil);
3406
3407 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3408 something. This avoids an endless loop if they failed to
3409 fontify the text for which reason ever. */
3410 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3411 handled = HANDLED_RECOMPUTE_PROPS;
3412 }
3413
3414 return handled;
3415 }
3416
3417
3418 \f
3419 /***********************************************************************
3420 Faces
3421 ***********************************************************************/
3422
3423 /* Set up iterator IT from face properties at its current position.
3424 Called from handle_stop. */
3425
3426 static enum prop_handled
3427 handle_face_prop (it)
3428 struct it *it;
3429 {
3430 int new_face_id;
3431 EMACS_INT next_stop;
3432
3433 if (!STRINGP (it->string))
3434 {
3435 new_face_id
3436 = face_at_buffer_position (it->w,
3437 IT_CHARPOS (*it),
3438 it->region_beg_charpos,
3439 it->region_end_charpos,
3440 &next_stop,
3441 (IT_CHARPOS (*it)
3442 + TEXT_PROP_DISTANCE_LIMIT),
3443 0, it->base_face_id);
3444
3445 /* Is this a start of a run of characters with box face?
3446 Caveat: this can be called for a freshly initialized
3447 iterator; face_id is -1 in this case. We know that the new
3448 face will not change until limit, i.e. if the new face has a
3449 box, all characters up to limit will have one. But, as
3450 usual, we don't know whether limit is really the end. */
3451 if (new_face_id != it->face_id)
3452 {
3453 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3454
3455 /* If new face has a box but old face has not, this is
3456 the start of a run of characters with box, i.e. it has
3457 a shadow on the left side. The value of face_id of the
3458 iterator will be -1 if this is the initial call that gets
3459 the face. In this case, we have to look in front of IT's
3460 position and see whether there is a face != new_face_id. */
3461 it->start_of_box_run_p
3462 = (new_face->box != FACE_NO_BOX
3463 && (it->face_id >= 0
3464 || IT_CHARPOS (*it) == BEG
3465 || new_face_id != face_before_it_pos (it)));
3466 it->face_box_p = new_face->box != FACE_NO_BOX;
3467 }
3468 }
3469 else
3470 {
3471 int base_face_id, bufpos;
3472 int i;
3473 Lisp_Object from_overlay
3474 = (it->current.overlay_string_index >= 0
3475 ? it->string_overlays[it->current.overlay_string_index]
3476 : Qnil);
3477
3478 /* See if we got to this string directly or indirectly from
3479 an overlay property. That includes the before-string or
3480 after-string of an overlay, strings in display properties
3481 provided by an overlay, their text properties, etc.
3482
3483 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3484 if (! NILP (from_overlay))
3485 for (i = it->sp - 1; i >= 0; i--)
3486 {
3487 if (it->stack[i].current.overlay_string_index >= 0)
3488 from_overlay
3489 = it->string_overlays[it->stack[i].current.overlay_string_index];
3490 else if (! NILP (it->stack[i].from_overlay))
3491 from_overlay = it->stack[i].from_overlay;
3492
3493 if (!NILP (from_overlay))
3494 break;
3495 }
3496
3497 if (! NILP (from_overlay))
3498 {
3499 bufpos = IT_CHARPOS (*it);
3500 /* For a string from an overlay, the base face depends
3501 only on text properties and ignores overlays. */
3502 base_face_id
3503 = face_for_overlay_string (it->w,
3504 IT_CHARPOS (*it),
3505 it->region_beg_charpos,
3506 it->region_end_charpos,
3507 &next_stop,
3508 (IT_CHARPOS (*it)
3509 + TEXT_PROP_DISTANCE_LIMIT),
3510 0,
3511 from_overlay);
3512 }
3513 else
3514 {
3515 bufpos = 0;
3516
3517 /* For strings from a `display' property, use the face at
3518 IT's current buffer position as the base face to merge
3519 with, so that overlay strings appear in the same face as
3520 surrounding text, unless they specify their own
3521 faces. */
3522 base_face_id = underlying_face_id (it);
3523 }
3524
3525 new_face_id = face_at_string_position (it->w,
3526 it->string,
3527 IT_STRING_CHARPOS (*it),
3528 bufpos,
3529 it->region_beg_charpos,
3530 it->region_end_charpos,
3531 &next_stop,
3532 base_face_id, 0);
3533
3534 /* Is this a start of a run of characters with box? Caveat:
3535 this can be called for a freshly allocated iterator; face_id
3536 is -1 is this case. We know that the new face will not
3537 change until the next check pos, i.e. if the new face has a
3538 box, all characters up to that position will have a
3539 box. But, as usual, we don't know whether that position
3540 is really the end. */
3541 if (new_face_id != it->face_id)
3542 {
3543 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3544 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3545
3546 /* If new face has a box but old face hasn't, this is the
3547 start of a run of characters with box, i.e. it has a
3548 shadow on the left side. */
3549 it->start_of_box_run_p
3550 = new_face->box && (old_face == NULL || !old_face->box);
3551 it->face_box_p = new_face->box != FACE_NO_BOX;
3552 }
3553 }
3554
3555 it->face_id = new_face_id;
3556 return HANDLED_NORMALLY;
3557 }
3558
3559
3560 /* Return the ID of the face ``underlying'' IT's current position,
3561 which is in a string. If the iterator is associated with a
3562 buffer, return the face at IT's current buffer position.
3563 Otherwise, use the iterator's base_face_id. */
3564
3565 static int
3566 underlying_face_id (it)
3567 struct it *it;
3568 {
3569 int face_id = it->base_face_id, i;
3570
3571 xassert (STRINGP (it->string));
3572
3573 for (i = it->sp - 1; i >= 0; --i)
3574 if (NILP (it->stack[i].string))
3575 face_id = it->stack[i].face_id;
3576
3577 return face_id;
3578 }
3579
3580
3581 /* Compute the face one character before or after the current position
3582 of IT. BEFORE_P non-zero means get the face in front of IT's
3583 position. Value is the id of the face. */
3584
3585 static int
3586 face_before_or_after_it_pos (it, before_p)
3587 struct it *it;
3588 int before_p;
3589 {
3590 int face_id, limit;
3591 EMACS_INT next_check_charpos;
3592 struct text_pos pos;
3593
3594 xassert (it->s == NULL);
3595
3596 if (STRINGP (it->string))
3597 {
3598 int bufpos, base_face_id;
3599
3600 /* No face change past the end of the string (for the case
3601 we are padding with spaces). No face change before the
3602 string start. */
3603 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3604 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3605 return it->face_id;
3606
3607 /* Set pos to the position before or after IT's current position. */
3608 if (before_p)
3609 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3610 else
3611 /* For composition, we must check the character after the
3612 composition. */
3613 pos = (it->what == IT_COMPOSITION
3614 ? string_pos (IT_STRING_CHARPOS (*it)
3615 + it->cmp_it.nchars, it->string)
3616 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3617
3618 if (it->current.overlay_string_index >= 0)
3619 bufpos = IT_CHARPOS (*it);
3620 else
3621 bufpos = 0;
3622
3623 base_face_id = underlying_face_id (it);
3624
3625 /* Get the face for ASCII, or unibyte. */
3626 face_id = face_at_string_position (it->w,
3627 it->string,
3628 CHARPOS (pos),
3629 bufpos,
3630 it->region_beg_charpos,
3631 it->region_end_charpos,
3632 &next_check_charpos,
3633 base_face_id, 0);
3634
3635 /* Correct the face for charsets different from ASCII. Do it
3636 for the multibyte case only. The face returned above is
3637 suitable for unibyte text if IT->string is unibyte. */
3638 if (STRING_MULTIBYTE (it->string))
3639 {
3640 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3641 int rest = SBYTES (it->string) - BYTEPOS (pos);
3642 int c, len;
3643 struct face *face = FACE_FROM_ID (it->f, face_id);
3644
3645 c = string_char_and_length (p, &len);
3646 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3647 }
3648 }
3649 else
3650 {
3651 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3652 || (IT_CHARPOS (*it) <= BEGV && before_p))
3653 return it->face_id;
3654
3655 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3656 pos = it->current.pos;
3657
3658 if (before_p)
3659 DEC_TEXT_POS (pos, it->multibyte_p);
3660 else
3661 {
3662 if (it->what == IT_COMPOSITION)
3663 /* For composition, we must check the position after the
3664 composition. */
3665 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3666 else
3667 INC_TEXT_POS (pos, it->multibyte_p);
3668 }
3669
3670 /* Determine face for CHARSET_ASCII, or unibyte. */
3671 face_id = face_at_buffer_position (it->w,
3672 CHARPOS (pos),
3673 it->region_beg_charpos,
3674 it->region_end_charpos,
3675 &next_check_charpos,
3676 limit, 0, -1);
3677
3678 /* Correct the face for charsets different from ASCII. Do it
3679 for the multibyte case only. The face returned above is
3680 suitable for unibyte text if current_buffer is unibyte. */
3681 if (it->multibyte_p)
3682 {
3683 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3684 struct face *face = FACE_FROM_ID (it->f, face_id);
3685 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3686 }
3687 }
3688
3689 return face_id;
3690 }
3691
3692
3693 \f
3694 /***********************************************************************
3695 Invisible text
3696 ***********************************************************************/
3697
3698 /* Set up iterator IT from invisible properties at its current
3699 position. Called from handle_stop. */
3700
3701 static enum prop_handled
3702 handle_invisible_prop (it)
3703 struct it *it;
3704 {
3705 enum prop_handled handled = HANDLED_NORMALLY;
3706
3707 if (STRINGP (it->string))
3708 {
3709 extern Lisp_Object Qinvisible;
3710 Lisp_Object prop, end_charpos, limit, charpos;
3711
3712 /* Get the value of the invisible text property at the
3713 current position. Value will be nil if there is no such
3714 property. */
3715 charpos = make_number (IT_STRING_CHARPOS (*it));
3716 prop = Fget_text_property (charpos, Qinvisible, it->string);
3717
3718 if (!NILP (prop)
3719 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3720 {
3721 handled = HANDLED_RECOMPUTE_PROPS;
3722
3723 /* Get the position at which the next change of the
3724 invisible text property can be found in IT->string.
3725 Value will be nil if the property value is the same for
3726 all the rest of IT->string. */
3727 XSETINT (limit, SCHARS (it->string));
3728 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3729 it->string, limit);
3730
3731 /* Text at current position is invisible. The next
3732 change in the property is at position end_charpos.
3733 Move IT's current position to that position. */
3734 if (INTEGERP (end_charpos)
3735 && XFASTINT (end_charpos) < XFASTINT (limit))
3736 {
3737 struct text_pos old;
3738 old = it->current.string_pos;
3739 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3740 compute_string_pos (&it->current.string_pos, old, it->string);
3741 }
3742 else
3743 {
3744 /* The rest of the string is invisible. If this is an
3745 overlay string, proceed with the next overlay string
3746 or whatever comes and return a character from there. */
3747 if (it->current.overlay_string_index >= 0)
3748 {
3749 next_overlay_string (it);
3750 /* Don't check for overlay strings when we just
3751 finished processing them. */
3752 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3753 }
3754 else
3755 {
3756 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3757 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3758 }
3759 }
3760 }
3761 }
3762 else
3763 {
3764 int invis_p;
3765 EMACS_INT newpos, next_stop, start_charpos, tem;
3766 Lisp_Object pos, prop, overlay;
3767
3768 /* First of all, is there invisible text at this position? */
3769 tem = start_charpos = IT_CHARPOS (*it);
3770 pos = make_number (tem);
3771 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3772 &overlay);
3773 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3774
3775 /* If we are on invisible text, skip over it. */
3776 if (invis_p && start_charpos < it->end_charpos)
3777 {
3778 /* Record whether we have to display an ellipsis for the
3779 invisible text. */
3780 int display_ellipsis_p = invis_p == 2;
3781
3782 handled = HANDLED_RECOMPUTE_PROPS;
3783
3784 /* Loop skipping over invisible text. The loop is left at
3785 ZV or with IT on the first char being visible again. */
3786 do
3787 {
3788 /* Try to skip some invisible text. Return value is the
3789 position reached which can be equal to where we start
3790 if there is nothing invisible there. This skips both
3791 over invisible text properties and overlays with
3792 invisible property. */
3793 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3794
3795 /* If we skipped nothing at all we weren't at invisible
3796 text in the first place. If everything to the end of
3797 the buffer was skipped, end the loop. */
3798 if (newpos == tem || newpos >= ZV)
3799 invis_p = 0;
3800 else
3801 {
3802 /* We skipped some characters but not necessarily
3803 all there are. Check if we ended up on visible
3804 text. Fget_char_property returns the property of
3805 the char before the given position, i.e. if we
3806 get invis_p = 0, this means that the char at
3807 newpos is visible. */
3808 pos = make_number (newpos);
3809 prop = Fget_char_property (pos, Qinvisible, it->window);
3810 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3811 }
3812
3813 /* If we ended up on invisible text, proceed to
3814 skip starting with next_stop. */
3815 if (invis_p)
3816 tem = next_stop;
3817
3818 /* If there are adjacent invisible texts, don't lose the
3819 second one's ellipsis. */
3820 if (invis_p == 2)
3821 display_ellipsis_p = 1;
3822 }
3823 while (invis_p);
3824
3825 /* The position newpos is now either ZV or on visible text. */
3826 if (it->bidi_p && newpos < ZV)
3827 {
3828 /* With bidi iteration, the region of invisible text
3829 could start and/or end in the middle of a non-base
3830 embedding level. Therefore, we need to skip
3831 invisible text using the bidi iterator, starting at
3832 IT's current position, until we find ourselves
3833 outside the invisible text. Skipping invisible text
3834 _after_ bidi iteration avoids affecting the visual
3835 order of the displayed text when invisible properties
3836 are added or removed. */
3837 if (it->bidi_it.first_elt)
3838 {
3839 /* If we were `reseat'ed to a new paragraph,
3840 determine the paragraph base direction. We need
3841 to do it now because next_element_from_buffer may
3842 not have a chance to do it, if we are going to
3843 skip any text at the beginning, which resets the
3844 FIRST_ELT flag. */
3845 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
3846 }
3847 do
3848 {
3849 bidi_get_next_char_visually (&it->bidi_it);
3850 }
3851 while (it->stop_charpos <= it->bidi_it.charpos
3852 && it->bidi_it.charpos < newpos);
3853 IT_CHARPOS (*it) = it->bidi_it.charpos;
3854 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3855 /* If we overstepped NEWPOS, record its position in the
3856 iterator, so that we skip invisible text if later the
3857 bidi iteration lands us in the invisible region
3858 again. */
3859 if (IT_CHARPOS (*it) >= newpos)
3860 it->prev_stop = newpos;
3861 }
3862 else
3863 {
3864 IT_CHARPOS (*it) = newpos;
3865 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3866 }
3867
3868 /* If there are before-strings at the start of invisible
3869 text, and the text is invisible because of a text
3870 property, arrange to show before-strings because 20.x did
3871 it that way. (If the text is invisible because of an
3872 overlay property instead of a text property, this is
3873 already handled in the overlay code.) */
3874 if (NILP (overlay)
3875 && get_overlay_strings (it, it->stop_charpos))
3876 {
3877 handled = HANDLED_RECOMPUTE_PROPS;
3878 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3879 }
3880 else if (display_ellipsis_p)
3881 {
3882 /* Make sure that the glyphs of the ellipsis will get
3883 correct `charpos' values. If we would not update
3884 it->position here, the glyphs would belong to the
3885 last visible character _before_ the invisible
3886 text, which confuses `set_cursor_from_row'.
3887
3888 We use the last invisible position instead of the
3889 first because this way the cursor is always drawn on
3890 the first "." of the ellipsis, whenever PT is inside
3891 the invisible text. Otherwise the cursor would be
3892 placed _after_ the ellipsis when the point is after the
3893 first invisible character. */
3894 if (!STRINGP (it->object))
3895 {
3896 it->position.charpos = newpos - 1;
3897 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3898 }
3899 it->ellipsis_p = 1;
3900 /* Let the ellipsis display before
3901 considering any properties of the following char.
3902 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3903 handled = HANDLED_RETURN;
3904 }
3905 }
3906 }
3907
3908 return handled;
3909 }
3910
3911
3912 /* Make iterator IT return `...' next.
3913 Replaces LEN characters from buffer. */
3914
3915 static void
3916 setup_for_ellipsis (it, len)
3917 struct it *it;
3918 int len;
3919 {
3920 /* Use the display table definition for `...'. Invalid glyphs
3921 will be handled by the method returning elements from dpvec. */
3922 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3923 {
3924 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3925 it->dpvec = v->contents;
3926 it->dpend = v->contents + v->size;
3927 }
3928 else
3929 {
3930 /* Default `...'. */
3931 it->dpvec = default_invis_vector;
3932 it->dpend = default_invis_vector + 3;
3933 }
3934
3935 it->dpvec_char_len = len;
3936 it->current.dpvec_index = 0;
3937 it->dpvec_face_id = -1;
3938
3939 /* Remember the current face id in case glyphs specify faces.
3940 IT's face is restored in set_iterator_to_next.
3941 saved_face_id was set to preceding char's face in handle_stop. */
3942 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3943 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3944
3945 it->method = GET_FROM_DISPLAY_VECTOR;
3946 it->ellipsis_p = 1;
3947 }
3948
3949
3950 \f
3951 /***********************************************************************
3952 'display' property
3953 ***********************************************************************/
3954
3955 /* Set up iterator IT from `display' property at its current position.
3956 Called from handle_stop.
3957 We return HANDLED_RETURN if some part of the display property
3958 overrides the display of the buffer text itself.
3959 Otherwise we return HANDLED_NORMALLY. */
3960
3961 static enum prop_handled
3962 handle_display_prop (it)
3963 struct it *it;
3964 {
3965 Lisp_Object prop, object, overlay;
3966 struct text_pos *position;
3967 /* Nonzero if some property replaces the display of the text itself. */
3968 int display_replaced_p = 0;
3969
3970 if (STRINGP (it->string))
3971 {
3972 object = it->string;
3973 position = &it->current.string_pos;
3974 }
3975 else
3976 {
3977 XSETWINDOW (object, it->w);
3978 position = &it->current.pos;
3979 }
3980
3981 /* Reset those iterator values set from display property values. */
3982 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3983 it->space_width = Qnil;
3984 it->font_height = Qnil;
3985 it->voffset = 0;
3986
3987 /* We don't support recursive `display' properties, i.e. string
3988 values that have a string `display' property, that have a string
3989 `display' property etc. */
3990 if (!it->string_from_display_prop_p)
3991 it->area = TEXT_AREA;
3992
3993 prop = get_char_property_and_overlay (make_number (position->charpos),
3994 Qdisplay, object, &overlay);
3995 if (NILP (prop))
3996 return HANDLED_NORMALLY;
3997 /* Now OVERLAY is the overlay that gave us this property, or nil
3998 if it was a text property. */
3999
4000 if (!STRINGP (it->string))
4001 object = it->w->buffer;
4002
4003 if (CONSP (prop)
4004 /* Simple properties. */
4005 && !EQ (XCAR (prop), Qimage)
4006 && !EQ (XCAR (prop), Qspace)
4007 && !EQ (XCAR (prop), Qwhen)
4008 && !EQ (XCAR (prop), Qslice)
4009 && !EQ (XCAR (prop), Qspace_width)
4010 && !EQ (XCAR (prop), Qheight)
4011 && !EQ (XCAR (prop), Qraise)
4012 /* Marginal area specifications. */
4013 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4014 && !EQ (XCAR (prop), Qleft_fringe)
4015 && !EQ (XCAR (prop), Qright_fringe)
4016 && !NILP (XCAR (prop)))
4017 {
4018 for (; CONSP (prop); prop = XCDR (prop))
4019 {
4020 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4021 position, display_replaced_p))
4022 {
4023 display_replaced_p = 1;
4024 /* If some text in a string is replaced, `position' no
4025 longer points to the position of `object'. */
4026 if (STRINGP (object))
4027 break;
4028 }
4029 }
4030 }
4031 else if (VECTORP (prop))
4032 {
4033 int i;
4034 for (i = 0; i < ASIZE (prop); ++i)
4035 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4036 position, display_replaced_p))
4037 {
4038 display_replaced_p = 1;
4039 /* If some text in a string is replaced, `position' no
4040 longer points to the position of `object'. */
4041 if (STRINGP (object))
4042 break;
4043 }
4044 }
4045 else
4046 {
4047 if (handle_single_display_spec (it, prop, object, overlay,
4048 position, 0))
4049 display_replaced_p = 1;
4050 }
4051
4052 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4053 }
4054
4055
4056 /* Value is the position of the end of the `display' property starting
4057 at START_POS in OBJECT. */
4058
4059 static struct text_pos
4060 display_prop_end (it, object, start_pos)
4061 struct it *it;
4062 Lisp_Object object;
4063 struct text_pos start_pos;
4064 {
4065 Lisp_Object end;
4066 struct text_pos end_pos;
4067
4068 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4069 Qdisplay, object, Qnil);
4070 CHARPOS (end_pos) = XFASTINT (end);
4071 if (STRINGP (object))
4072 compute_string_pos (&end_pos, start_pos, it->string);
4073 else
4074 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4075
4076 return end_pos;
4077 }
4078
4079
4080 /* Set up IT from a single `display' specification PROP. OBJECT
4081 is the object in which the `display' property was found. *POSITION
4082 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4083 means that we previously saw a display specification which already
4084 replaced text display with something else, for example an image;
4085 we ignore such properties after the first one has been processed.
4086
4087 OVERLAY is the overlay this `display' property came from,
4088 or nil if it was a text property.
4089
4090 If PROP is a `space' or `image' specification, and in some other
4091 cases too, set *POSITION to the position where the `display'
4092 property ends.
4093
4094 Value is non-zero if something was found which replaces the display
4095 of buffer or string text. */
4096
4097 static int
4098 handle_single_display_spec (it, spec, object, overlay, position,
4099 display_replaced_before_p)
4100 struct it *it;
4101 Lisp_Object spec;
4102 Lisp_Object object;
4103 Lisp_Object overlay;
4104 struct text_pos *position;
4105 int display_replaced_before_p;
4106 {
4107 Lisp_Object form;
4108 Lisp_Object location, value;
4109 struct text_pos start_pos, save_pos;
4110 int valid_p;
4111
4112 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4113 If the result is non-nil, use VALUE instead of SPEC. */
4114 form = Qt;
4115 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4116 {
4117 spec = XCDR (spec);
4118 if (!CONSP (spec))
4119 return 0;
4120 form = XCAR (spec);
4121 spec = XCDR (spec);
4122 }
4123
4124 if (!NILP (form) && !EQ (form, Qt))
4125 {
4126 int count = SPECPDL_INDEX ();
4127 struct gcpro gcpro1;
4128
4129 /* Bind `object' to the object having the `display' property, a
4130 buffer or string. Bind `position' to the position in the
4131 object where the property was found, and `buffer-position'
4132 to the current position in the buffer. */
4133 specbind (Qobject, object);
4134 specbind (Qposition, make_number (CHARPOS (*position)));
4135 specbind (Qbuffer_position,
4136 make_number (STRINGP (object)
4137 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4138 GCPRO1 (form);
4139 form = safe_eval (form);
4140 UNGCPRO;
4141 unbind_to (count, Qnil);
4142 }
4143
4144 if (NILP (form))
4145 return 0;
4146
4147 /* Handle `(height HEIGHT)' specifications. */
4148 if (CONSP (spec)
4149 && EQ (XCAR (spec), Qheight)
4150 && CONSP (XCDR (spec)))
4151 {
4152 if (!FRAME_WINDOW_P (it->f))
4153 return 0;
4154
4155 it->font_height = XCAR (XCDR (spec));
4156 if (!NILP (it->font_height))
4157 {
4158 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4159 int new_height = -1;
4160
4161 if (CONSP (it->font_height)
4162 && (EQ (XCAR (it->font_height), Qplus)
4163 || EQ (XCAR (it->font_height), Qminus))
4164 && CONSP (XCDR (it->font_height))
4165 && INTEGERP (XCAR (XCDR (it->font_height))))
4166 {
4167 /* `(+ N)' or `(- N)' where N is an integer. */
4168 int steps = XINT (XCAR (XCDR (it->font_height)));
4169 if (EQ (XCAR (it->font_height), Qplus))
4170 steps = - steps;
4171 it->face_id = smaller_face (it->f, it->face_id, steps);
4172 }
4173 else if (FUNCTIONP (it->font_height))
4174 {
4175 /* Call function with current height as argument.
4176 Value is the new height. */
4177 Lisp_Object height;
4178 height = safe_call1 (it->font_height,
4179 face->lface[LFACE_HEIGHT_INDEX]);
4180 if (NUMBERP (height))
4181 new_height = XFLOATINT (height);
4182 }
4183 else if (NUMBERP (it->font_height))
4184 {
4185 /* Value is a multiple of the canonical char height. */
4186 struct face *face;
4187
4188 face = FACE_FROM_ID (it->f,
4189 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4190 new_height = (XFLOATINT (it->font_height)
4191 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4192 }
4193 else
4194 {
4195 /* Evaluate IT->font_height with `height' bound to the
4196 current specified height to get the new height. */
4197 int count = SPECPDL_INDEX ();
4198
4199 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4200 value = safe_eval (it->font_height);
4201 unbind_to (count, Qnil);
4202
4203 if (NUMBERP (value))
4204 new_height = XFLOATINT (value);
4205 }
4206
4207 if (new_height > 0)
4208 it->face_id = face_with_height (it->f, it->face_id, new_height);
4209 }
4210
4211 return 0;
4212 }
4213
4214 /* Handle `(space-width WIDTH)'. */
4215 if (CONSP (spec)
4216 && EQ (XCAR (spec), Qspace_width)
4217 && CONSP (XCDR (spec)))
4218 {
4219 if (!FRAME_WINDOW_P (it->f))
4220 return 0;
4221
4222 value = XCAR (XCDR (spec));
4223 if (NUMBERP (value) && XFLOATINT (value) > 0)
4224 it->space_width = value;
4225
4226 return 0;
4227 }
4228
4229 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4230 if (CONSP (spec)
4231 && EQ (XCAR (spec), Qslice))
4232 {
4233 Lisp_Object tem;
4234
4235 if (!FRAME_WINDOW_P (it->f))
4236 return 0;
4237
4238 if (tem = XCDR (spec), CONSP (tem))
4239 {
4240 it->slice.x = XCAR (tem);
4241 if (tem = XCDR (tem), CONSP (tem))
4242 {
4243 it->slice.y = XCAR (tem);
4244 if (tem = XCDR (tem), CONSP (tem))
4245 {
4246 it->slice.width = XCAR (tem);
4247 if (tem = XCDR (tem), CONSP (tem))
4248 it->slice.height = XCAR (tem);
4249 }
4250 }
4251 }
4252
4253 return 0;
4254 }
4255
4256 /* Handle `(raise FACTOR)'. */
4257 if (CONSP (spec)
4258 && EQ (XCAR (spec), Qraise)
4259 && CONSP (XCDR (spec)))
4260 {
4261 if (!FRAME_WINDOW_P (it->f))
4262 return 0;
4263
4264 #ifdef HAVE_WINDOW_SYSTEM
4265 value = XCAR (XCDR (spec));
4266 if (NUMBERP (value))
4267 {
4268 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4269 it->voffset = - (XFLOATINT (value)
4270 * (FONT_HEIGHT (face->font)));
4271 }
4272 #endif /* HAVE_WINDOW_SYSTEM */
4273
4274 return 0;
4275 }
4276
4277 /* Don't handle the other kinds of display specifications
4278 inside a string that we got from a `display' property. */
4279 if (it->string_from_display_prop_p)
4280 return 0;
4281
4282 /* Characters having this form of property are not displayed, so
4283 we have to find the end of the property. */
4284 start_pos = *position;
4285 *position = display_prop_end (it, object, start_pos);
4286 value = Qnil;
4287
4288 /* Stop the scan at that end position--we assume that all
4289 text properties change there. */
4290 it->stop_charpos = position->charpos;
4291
4292 /* Handle `(left-fringe BITMAP [FACE])'
4293 and `(right-fringe BITMAP [FACE])'. */
4294 if (CONSP (spec)
4295 && (EQ (XCAR (spec), Qleft_fringe)
4296 || EQ (XCAR (spec), Qright_fringe))
4297 && CONSP (XCDR (spec)))
4298 {
4299 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4300 int fringe_bitmap;
4301
4302 if (!FRAME_WINDOW_P (it->f))
4303 /* If we return here, POSITION has been advanced
4304 across the text with this property. */
4305 return 0;
4306
4307 #ifdef HAVE_WINDOW_SYSTEM
4308 value = XCAR (XCDR (spec));
4309 if (!SYMBOLP (value)
4310 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4311 /* If we return here, POSITION has been advanced
4312 across the text with this property. */
4313 return 0;
4314
4315 if (CONSP (XCDR (XCDR (spec))))
4316 {
4317 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4318 int face_id2 = lookup_derived_face (it->f, face_name,
4319 FRINGE_FACE_ID, 0);
4320 if (face_id2 >= 0)
4321 face_id = face_id2;
4322 }
4323
4324 /* Save current settings of IT so that we can restore them
4325 when we are finished with the glyph property value. */
4326
4327 save_pos = it->position;
4328 it->position = *position;
4329 push_it (it);
4330 it->position = save_pos;
4331
4332 it->area = TEXT_AREA;
4333 it->what = IT_IMAGE;
4334 it->image_id = -1; /* no image */
4335 it->position = start_pos;
4336 it->object = NILP (object) ? it->w->buffer : object;
4337 it->method = GET_FROM_IMAGE;
4338 it->from_overlay = Qnil;
4339 it->face_id = face_id;
4340
4341 /* Say that we haven't consumed the characters with
4342 `display' property yet. The call to pop_it in
4343 set_iterator_to_next will clean this up. */
4344 *position = start_pos;
4345
4346 if (EQ (XCAR (spec), Qleft_fringe))
4347 {
4348 it->left_user_fringe_bitmap = fringe_bitmap;
4349 it->left_user_fringe_face_id = face_id;
4350 }
4351 else
4352 {
4353 it->right_user_fringe_bitmap = fringe_bitmap;
4354 it->right_user_fringe_face_id = face_id;
4355 }
4356 #endif /* HAVE_WINDOW_SYSTEM */
4357 return 1;
4358 }
4359
4360 /* Prepare to handle `((margin left-margin) ...)',
4361 `((margin right-margin) ...)' and `((margin nil) ...)'
4362 prefixes for display specifications. */
4363 location = Qunbound;
4364 if (CONSP (spec) && CONSP (XCAR (spec)))
4365 {
4366 Lisp_Object tem;
4367
4368 value = XCDR (spec);
4369 if (CONSP (value))
4370 value = XCAR (value);
4371
4372 tem = XCAR (spec);
4373 if (EQ (XCAR (tem), Qmargin)
4374 && (tem = XCDR (tem),
4375 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4376 (NILP (tem)
4377 || EQ (tem, Qleft_margin)
4378 || EQ (tem, Qright_margin))))
4379 location = tem;
4380 }
4381
4382 if (EQ (location, Qunbound))
4383 {
4384 location = Qnil;
4385 value = spec;
4386 }
4387
4388 /* After this point, VALUE is the property after any
4389 margin prefix has been stripped. It must be a string,
4390 an image specification, or `(space ...)'.
4391
4392 LOCATION specifies where to display: `left-margin',
4393 `right-margin' or nil. */
4394
4395 valid_p = (STRINGP (value)
4396 #ifdef HAVE_WINDOW_SYSTEM
4397 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4398 #endif /* not HAVE_WINDOW_SYSTEM */
4399 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4400
4401 if (valid_p && !display_replaced_before_p)
4402 {
4403 /* Save current settings of IT so that we can restore them
4404 when we are finished with the glyph property value. */
4405 save_pos = it->position;
4406 it->position = *position;
4407 push_it (it);
4408 it->position = save_pos;
4409 it->from_overlay = overlay;
4410
4411 if (NILP (location))
4412 it->area = TEXT_AREA;
4413 else if (EQ (location, Qleft_margin))
4414 it->area = LEFT_MARGIN_AREA;
4415 else
4416 it->area = RIGHT_MARGIN_AREA;
4417
4418 if (STRINGP (value))
4419 {
4420 it->string = value;
4421 it->multibyte_p = STRING_MULTIBYTE (it->string);
4422 it->current.overlay_string_index = -1;
4423 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4424 it->end_charpos = it->string_nchars = SCHARS (it->string);
4425 it->method = GET_FROM_STRING;
4426 it->stop_charpos = 0;
4427 it->string_from_display_prop_p = 1;
4428 /* Say that we haven't consumed the characters with
4429 `display' property yet. The call to pop_it in
4430 set_iterator_to_next will clean this up. */
4431 if (BUFFERP (object))
4432 *position = start_pos;
4433 }
4434 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4435 {
4436 it->method = GET_FROM_STRETCH;
4437 it->object = value;
4438 *position = it->position = start_pos;
4439 }
4440 #ifdef HAVE_WINDOW_SYSTEM
4441 else
4442 {
4443 it->what = IT_IMAGE;
4444 it->image_id = lookup_image (it->f, value);
4445 it->position = start_pos;
4446 it->object = NILP (object) ? it->w->buffer : object;
4447 it->method = GET_FROM_IMAGE;
4448
4449 /* Say that we haven't consumed the characters with
4450 `display' property yet. The call to pop_it in
4451 set_iterator_to_next will clean this up. */
4452 *position = start_pos;
4453 }
4454 #endif /* HAVE_WINDOW_SYSTEM */
4455
4456 return 1;
4457 }
4458
4459 /* Invalid property or property not supported. Restore
4460 POSITION to what it was before. */
4461 *position = start_pos;
4462 return 0;
4463 }
4464
4465
4466 /* Check if SPEC is a display sub-property value whose text should be
4467 treated as intangible. */
4468
4469 static int
4470 single_display_spec_intangible_p (prop)
4471 Lisp_Object prop;
4472 {
4473 /* Skip over `when FORM'. */
4474 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4475 {
4476 prop = XCDR (prop);
4477 if (!CONSP (prop))
4478 return 0;
4479 prop = XCDR (prop);
4480 }
4481
4482 if (STRINGP (prop))
4483 return 1;
4484
4485 if (!CONSP (prop))
4486 return 0;
4487
4488 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4489 we don't need to treat text as intangible. */
4490 if (EQ (XCAR (prop), Qmargin))
4491 {
4492 prop = XCDR (prop);
4493 if (!CONSP (prop))
4494 return 0;
4495
4496 prop = XCDR (prop);
4497 if (!CONSP (prop)
4498 || EQ (XCAR (prop), Qleft_margin)
4499 || EQ (XCAR (prop), Qright_margin))
4500 return 0;
4501 }
4502
4503 return (CONSP (prop)
4504 && (EQ (XCAR (prop), Qimage)
4505 || EQ (XCAR (prop), Qspace)));
4506 }
4507
4508
4509 /* Check if PROP is a display property value whose text should be
4510 treated as intangible. */
4511
4512 int
4513 display_prop_intangible_p (prop)
4514 Lisp_Object prop;
4515 {
4516 if (CONSP (prop)
4517 && CONSP (XCAR (prop))
4518 && !EQ (Qmargin, XCAR (XCAR (prop))))
4519 {
4520 /* A list of sub-properties. */
4521 while (CONSP (prop))
4522 {
4523 if (single_display_spec_intangible_p (XCAR (prop)))
4524 return 1;
4525 prop = XCDR (prop);
4526 }
4527 }
4528 else if (VECTORP (prop))
4529 {
4530 /* A vector of sub-properties. */
4531 int i;
4532 for (i = 0; i < ASIZE (prop); ++i)
4533 if (single_display_spec_intangible_p (AREF (prop, i)))
4534 return 1;
4535 }
4536 else
4537 return single_display_spec_intangible_p (prop);
4538
4539 return 0;
4540 }
4541
4542
4543 /* Return 1 if PROP is a display sub-property value containing STRING. */
4544
4545 static int
4546 single_display_spec_string_p (prop, string)
4547 Lisp_Object prop, string;
4548 {
4549 if (EQ (string, prop))
4550 return 1;
4551
4552 /* Skip over `when FORM'. */
4553 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4554 {
4555 prop = XCDR (prop);
4556 if (!CONSP (prop))
4557 return 0;
4558 prop = XCDR (prop);
4559 }
4560
4561 if (CONSP (prop))
4562 /* Skip over `margin LOCATION'. */
4563 if (EQ (XCAR (prop), Qmargin))
4564 {
4565 prop = XCDR (prop);
4566 if (!CONSP (prop))
4567 return 0;
4568
4569 prop = XCDR (prop);
4570 if (!CONSP (prop))
4571 return 0;
4572 }
4573
4574 return CONSP (prop) && EQ (XCAR (prop), string);
4575 }
4576
4577
4578 /* Return 1 if STRING appears in the `display' property PROP. */
4579
4580 static int
4581 display_prop_string_p (prop, string)
4582 Lisp_Object prop, string;
4583 {
4584 if (CONSP (prop)
4585 && CONSP (XCAR (prop))
4586 && !EQ (Qmargin, XCAR (XCAR (prop))))
4587 {
4588 /* A list of sub-properties. */
4589 while (CONSP (prop))
4590 {
4591 if (single_display_spec_string_p (XCAR (prop), string))
4592 return 1;
4593 prop = XCDR (prop);
4594 }
4595 }
4596 else if (VECTORP (prop))
4597 {
4598 /* A vector of sub-properties. */
4599 int i;
4600 for (i = 0; i < ASIZE (prop); ++i)
4601 if (single_display_spec_string_p (AREF (prop, i), string))
4602 return 1;
4603 }
4604 else
4605 return single_display_spec_string_p (prop, string);
4606
4607 return 0;
4608 }
4609
4610 /* Look for STRING in overlays and text properties in W's buffer,
4611 between character positions FROM and TO (excluding TO).
4612 BACK_P non-zero means look back (in this case, TO is supposed to be
4613 less than FROM).
4614 Value is the first character position where STRING was found, or
4615 zero if it wasn't found before hitting TO.
4616
4617 W's buffer must be current.
4618
4619 This function may only use code that doesn't eval because it is
4620 called asynchronously from note_mouse_highlight. */
4621
4622 static EMACS_INT
4623 string_buffer_position_lim (w, string, from, to, back_p)
4624 struct window *w;
4625 Lisp_Object string;
4626 EMACS_INT from, to;
4627 int back_p;
4628 {
4629 Lisp_Object limit, prop, pos;
4630 int found = 0;
4631
4632 pos = make_number (from);
4633
4634 if (!back_p) /* looking forward */
4635 {
4636 limit = make_number (min (to, ZV));
4637 while (!found && !EQ (pos, limit))
4638 {
4639 prop = Fget_char_property (pos, Qdisplay, Qnil);
4640 if (!NILP (prop) && display_prop_string_p (prop, string))
4641 found = 1;
4642 else
4643 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4644 limit);
4645 }
4646 }
4647 else /* looking back */
4648 {
4649 limit = make_number (max (to, BEGV));
4650 while (!found && !EQ (pos, limit))
4651 {
4652 prop = Fget_char_property (pos, Qdisplay, Qnil);
4653 if (!NILP (prop) && display_prop_string_p (prop, string))
4654 found = 1;
4655 else
4656 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4657 limit);
4658 }
4659 }
4660
4661 return found ? XINT (pos) : 0;
4662 }
4663
4664 /* Determine which buffer position in W's buffer STRING comes from.
4665 AROUND_CHARPOS is an approximate position where it could come from.
4666 Value is the buffer position or 0 if it couldn't be determined.
4667
4668 W's buffer must be current.
4669
4670 This function is necessary because we don't record buffer positions
4671 in glyphs generated from strings (to keep struct glyph small).
4672 This function may only use code that doesn't eval because it is
4673 called asynchronously from note_mouse_highlight. */
4674
4675 EMACS_INT
4676 string_buffer_position (w, string, around_charpos)
4677 struct window *w;
4678 Lisp_Object string;
4679 EMACS_INT around_charpos;
4680 {
4681 Lisp_Object limit, prop, pos;
4682 const int MAX_DISTANCE = 1000;
4683 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4684 around_charpos + MAX_DISTANCE,
4685 0);
4686
4687 if (!found)
4688 found = string_buffer_position_lim (w, string, around_charpos,
4689 around_charpos - MAX_DISTANCE, 1);
4690 return found;
4691 }
4692
4693
4694 \f
4695 /***********************************************************************
4696 `composition' property
4697 ***********************************************************************/
4698
4699 /* Set up iterator IT from `composition' property at its current
4700 position. Called from handle_stop. */
4701
4702 static enum prop_handled
4703 handle_composition_prop (it)
4704 struct it *it;
4705 {
4706 Lisp_Object prop, string;
4707 EMACS_INT pos, pos_byte, start, end;
4708
4709 if (STRINGP (it->string))
4710 {
4711 unsigned char *s;
4712
4713 pos = IT_STRING_CHARPOS (*it);
4714 pos_byte = IT_STRING_BYTEPOS (*it);
4715 string = it->string;
4716 s = SDATA (string) + pos_byte;
4717 it->c = STRING_CHAR (s);
4718 }
4719 else
4720 {
4721 pos = IT_CHARPOS (*it);
4722 pos_byte = IT_BYTEPOS (*it);
4723 string = Qnil;
4724 it->c = FETCH_CHAR (pos_byte);
4725 }
4726
4727 /* If there's a valid composition and point is not inside of the
4728 composition (in the case that the composition is from the current
4729 buffer), draw a glyph composed from the composition components. */
4730 if (find_composition (pos, -1, &start, &end, &prop, string)
4731 && COMPOSITION_VALID_P (start, end, prop)
4732 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4733 {
4734 if (start != pos)
4735 {
4736 if (STRINGP (it->string))
4737 pos_byte = string_char_to_byte (it->string, start);
4738 else
4739 pos_byte = CHAR_TO_BYTE (start);
4740 }
4741 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4742 prop, string);
4743
4744 if (it->cmp_it.id >= 0)
4745 {
4746 it->cmp_it.ch = -1;
4747 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4748 it->cmp_it.nglyphs = -1;
4749 }
4750 }
4751
4752 return HANDLED_NORMALLY;
4753 }
4754
4755
4756 \f
4757 /***********************************************************************
4758 Overlay strings
4759 ***********************************************************************/
4760
4761 /* The following structure is used to record overlay strings for
4762 later sorting in load_overlay_strings. */
4763
4764 struct overlay_entry
4765 {
4766 Lisp_Object overlay;
4767 Lisp_Object string;
4768 int priority;
4769 int after_string_p;
4770 };
4771
4772
4773 /* Set up iterator IT from overlay strings at its current position.
4774 Called from handle_stop. */
4775
4776 static enum prop_handled
4777 handle_overlay_change (it)
4778 struct it *it;
4779 {
4780 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4781 return HANDLED_RECOMPUTE_PROPS;
4782 else
4783 return HANDLED_NORMALLY;
4784 }
4785
4786
4787 /* Set up the next overlay string for delivery by IT, if there is an
4788 overlay string to deliver. Called by set_iterator_to_next when the
4789 end of the current overlay string is reached. If there are more
4790 overlay strings to display, IT->string and
4791 IT->current.overlay_string_index are set appropriately here.
4792 Otherwise IT->string is set to nil. */
4793
4794 static void
4795 next_overlay_string (it)
4796 struct it *it;
4797 {
4798 ++it->current.overlay_string_index;
4799 if (it->current.overlay_string_index == it->n_overlay_strings)
4800 {
4801 /* No more overlay strings. Restore IT's settings to what
4802 they were before overlay strings were processed, and
4803 continue to deliver from current_buffer. */
4804
4805 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4806 pop_it (it);
4807 xassert (it->sp > 0
4808 || (NILP (it->string)
4809 && it->method == GET_FROM_BUFFER
4810 && it->stop_charpos >= BEGV
4811 && it->stop_charpos <= it->end_charpos));
4812 it->current.overlay_string_index = -1;
4813 it->n_overlay_strings = 0;
4814
4815 /* If we're at the end of the buffer, record that we have
4816 processed the overlay strings there already, so that
4817 next_element_from_buffer doesn't try it again. */
4818 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4819 it->overlay_strings_at_end_processed_p = 1;
4820 }
4821 else
4822 {
4823 /* There are more overlay strings to process. If
4824 IT->current.overlay_string_index has advanced to a position
4825 where we must load IT->overlay_strings with more strings, do
4826 it. */
4827 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4828
4829 if (it->current.overlay_string_index && i == 0)
4830 load_overlay_strings (it, 0);
4831
4832 /* Initialize IT to deliver display elements from the overlay
4833 string. */
4834 it->string = it->overlay_strings[i];
4835 it->multibyte_p = STRING_MULTIBYTE (it->string);
4836 SET_TEXT_POS (it->current.string_pos, 0, 0);
4837 it->method = GET_FROM_STRING;
4838 it->stop_charpos = 0;
4839 if (it->cmp_it.stop_pos >= 0)
4840 it->cmp_it.stop_pos = 0;
4841 }
4842
4843 CHECK_IT (it);
4844 }
4845
4846
4847 /* Compare two overlay_entry structures E1 and E2. Used as a
4848 comparison function for qsort in load_overlay_strings. Overlay
4849 strings for the same position are sorted so that
4850
4851 1. All after-strings come in front of before-strings, except
4852 when they come from the same overlay.
4853
4854 2. Within after-strings, strings are sorted so that overlay strings
4855 from overlays with higher priorities come first.
4856
4857 2. Within before-strings, strings are sorted so that overlay
4858 strings from overlays with higher priorities come last.
4859
4860 Value is analogous to strcmp. */
4861
4862
4863 static int
4864 compare_overlay_entries (e1, e2)
4865 void *e1, *e2;
4866 {
4867 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4868 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4869 int result;
4870
4871 if (entry1->after_string_p != entry2->after_string_p)
4872 {
4873 /* Let after-strings appear in front of before-strings if
4874 they come from different overlays. */
4875 if (EQ (entry1->overlay, entry2->overlay))
4876 result = entry1->after_string_p ? 1 : -1;
4877 else
4878 result = entry1->after_string_p ? -1 : 1;
4879 }
4880 else if (entry1->after_string_p)
4881 /* After-strings sorted in order of decreasing priority. */
4882 result = entry2->priority - entry1->priority;
4883 else
4884 /* Before-strings sorted in order of increasing priority. */
4885 result = entry1->priority - entry2->priority;
4886
4887 return result;
4888 }
4889
4890
4891 /* Load the vector IT->overlay_strings with overlay strings from IT's
4892 current buffer position, or from CHARPOS if that is > 0. Set
4893 IT->n_overlays to the total number of overlay strings found.
4894
4895 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4896 a time. On entry into load_overlay_strings,
4897 IT->current.overlay_string_index gives the number of overlay
4898 strings that have already been loaded by previous calls to this
4899 function.
4900
4901 IT->add_overlay_start contains an additional overlay start
4902 position to consider for taking overlay strings from, if non-zero.
4903 This position comes into play when the overlay has an `invisible'
4904 property, and both before and after-strings. When we've skipped to
4905 the end of the overlay, because of its `invisible' property, we
4906 nevertheless want its before-string to appear.
4907 IT->add_overlay_start will contain the overlay start position
4908 in this case.
4909
4910 Overlay strings are sorted so that after-string strings come in
4911 front of before-string strings. Within before and after-strings,
4912 strings are sorted by overlay priority. See also function
4913 compare_overlay_entries. */
4914
4915 static void
4916 load_overlay_strings (it, charpos)
4917 struct it *it;
4918 int charpos;
4919 {
4920 extern Lisp_Object Qwindow, Qpriority;
4921 Lisp_Object overlay, window, str, invisible;
4922 struct Lisp_Overlay *ov;
4923 int start, end;
4924 int size = 20;
4925 int n = 0, i, j, invis_p;
4926 struct overlay_entry *entries
4927 = (struct overlay_entry *) alloca (size * sizeof *entries);
4928
4929 if (charpos <= 0)
4930 charpos = IT_CHARPOS (*it);
4931
4932 /* Append the overlay string STRING of overlay OVERLAY to vector
4933 `entries' which has size `size' and currently contains `n'
4934 elements. AFTER_P non-zero means STRING is an after-string of
4935 OVERLAY. */
4936 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4937 do \
4938 { \
4939 Lisp_Object priority; \
4940 \
4941 if (n == size) \
4942 { \
4943 int new_size = 2 * size; \
4944 struct overlay_entry *old = entries; \
4945 entries = \
4946 (struct overlay_entry *) alloca (new_size \
4947 * sizeof *entries); \
4948 bcopy (old, entries, size * sizeof *entries); \
4949 size = new_size; \
4950 } \
4951 \
4952 entries[n].string = (STRING); \
4953 entries[n].overlay = (OVERLAY); \
4954 priority = Foverlay_get ((OVERLAY), Qpriority); \
4955 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4956 entries[n].after_string_p = (AFTER_P); \
4957 ++n; \
4958 } \
4959 while (0)
4960
4961 /* Process overlay before the overlay center. */
4962 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4963 {
4964 XSETMISC (overlay, ov);
4965 xassert (OVERLAYP (overlay));
4966 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4967 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4968
4969 if (end < charpos)
4970 break;
4971
4972 /* Skip this overlay if it doesn't start or end at IT's current
4973 position. */
4974 if (end != charpos && start != charpos)
4975 continue;
4976
4977 /* Skip this overlay if it doesn't apply to IT->w. */
4978 window = Foverlay_get (overlay, Qwindow);
4979 if (WINDOWP (window) && XWINDOW (window) != it->w)
4980 continue;
4981
4982 /* If the text ``under'' the overlay is invisible, both before-
4983 and after-strings from this overlay are visible; start and
4984 end position are indistinguishable. */
4985 invisible = Foverlay_get (overlay, Qinvisible);
4986 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4987
4988 /* If overlay has a non-empty before-string, record it. */
4989 if ((start == charpos || (end == charpos && invis_p))
4990 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4991 && SCHARS (str))
4992 RECORD_OVERLAY_STRING (overlay, str, 0);
4993
4994 /* If overlay has a non-empty after-string, record it. */
4995 if ((end == charpos || (start == charpos && invis_p))
4996 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4997 && SCHARS (str))
4998 RECORD_OVERLAY_STRING (overlay, str, 1);
4999 }
5000
5001 /* Process overlays after the overlay center. */
5002 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5003 {
5004 XSETMISC (overlay, ov);
5005 xassert (OVERLAYP (overlay));
5006 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5007 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5008
5009 if (start > charpos)
5010 break;
5011
5012 /* Skip this overlay if it doesn't start or end at IT's current
5013 position. */
5014 if (end != charpos && start != charpos)
5015 continue;
5016
5017 /* Skip this overlay if it doesn't apply to IT->w. */
5018 window = Foverlay_get (overlay, Qwindow);
5019 if (WINDOWP (window) && XWINDOW (window) != it->w)
5020 continue;
5021
5022 /* If the text ``under'' the overlay is invisible, it has a zero
5023 dimension, and both before- and after-strings apply. */
5024 invisible = Foverlay_get (overlay, Qinvisible);
5025 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5026
5027 /* If overlay has a non-empty before-string, record it. */
5028 if ((start == charpos || (end == charpos && invis_p))
5029 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5030 && SCHARS (str))
5031 RECORD_OVERLAY_STRING (overlay, str, 0);
5032
5033 /* If overlay has a non-empty after-string, record it. */
5034 if ((end == charpos || (start == charpos && invis_p))
5035 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5036 && SCHARS (str))
5037 RECORD_OVERLAY_STRING (overlay, str, 1);
5038 }
5039
5040 #undef RECORD_OVERLAY_STRING
5041
5042 /* Sort entries. */
5043 if (n > 1)
5044 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5045
5046 /* Record the total number of strings to process. */
5047 it->n_overlay_strings = n;
5048
5049 /* IT->current.overlay_string_index is the number of overlay strings
5050 that have already been consumed by IT. Copy some of the
5051 remaining overlay strings to IT->overlay_strings. */
5052 i = 0;
5053 j = it->current.overlay_string_index;
5054 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5055 {
5056 it->overlay_strings[i] = entries[j].string;
5057 it->string_overlays[i++] = entries[j++].overlay;
5058 }
5059
5060 CHECK_IT (it);
5061 }
5062
5063
5064 /* Get the first chunk of overlay strings at IT's current buffer
5065 position, or at CHARPOS if that is > 0. Value is non-zero if at
5066 least one overlay string was found. */
5067
5068 static int
5069 get_overlay_strings_1 (it, charpos, compute_stop_p)
5070 struct it *it;
5071 int charpos;
5072 int compute_stop_p;
5073 {
5074 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5075 process. This fills IT->overlay_strings with strings, and sets
5076 IT->n_overlay_strings to the total number of strings to process.
5077 IT->pos.overlay_string_index has to be set temporarily to zero
5078 because load_overlay_strings needs this; it must be set to -1
5079 when no overlay strings are found because a zero value would
5080 indicate a position in the first overlay string. */
5081 it->current.overlay_string_index = 0;
5082 load_overlay_strings (it, charpos);
5083
5084 /* If we found overlay strings, set up IT to deliver display
5085 elements from the first one. Otherwise set up IT to deliver
5086 from current_buffer. */
5087 if (it->n_overlay_strings)
5088 {
5089 /* Make sure we know settings in current_buffer, so that we can
5090 restore meaningful values when we're done with the overlay
5091 strings. */
5092 if (compute_stop_p)
5093 compute_stop_pos (it);
5094 xassert (it->face_id >= 0);
5095
5096 /* Save IT's settings. They are restored after all overlay
5097 strings have been processed. */
5098 xassert (!compute_stop_p || it->sp == 0);
5099
5100 /* When called from handle_stop, there might be an empty display
5101 string loaded. In that case, don't bother saving it. */
5102 if (!STRINGP (it->string) || SCHARS (it->string))
5103 push_it (it);
5104
5105 /* Set up IT to deliver display elements from the first overlay
5106 string. */
5107 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5108 it->string = it->overlay_strings[0];
5109 it->from_overlay = Qnil;
5110 it->stop_charpos = 0;
5111 xassert (STRINGP (it->string));
5112 it->end_charpos = SCHARS (it->string);
5113 it->multibyte_p = STRING_MULTIBYTE (it->string);
5114 it->method = GET_FROM_STRING;
5115 return 1;
5116 }
5117
5118 it->current.overlay_string_index = -1;
5119 return 0;
5120 }
5121
5122 static int
5123 get_overlay_strings (it, charpos)
5124 struct it *it;
5125 int charpos;
5126 {
5127 it->string = Qnil;
5128 it->method = GET_FROM_BUFFER;
5129
5130 (void) get_overlay_strings_1 (it, charpos, 1);
5131
5132 CHECK_IT (it);
5133
5134 /* Value is non-zero if we found at least one overlay string. */
5135 return STRINGP (it->string);
5136 }
5137
5138
5139 \f
5140 /***********************************************************************
5141 Saving and restoring state
5142 ***********************************************************************/
5143
5144 /* Save current settings of IT on IT->stack. Called, for example,
5145 before setting up IT for an overlay string, to be able to restore
5146 IT's settings to what they were after the overlay string has been
5147 processed. */
5148
5149 static void
5150 push_it (it)
5151 struct it *it;
5152 {
5153 struct iterator_stack_entry *p;
5154
5155 xassert (it->sp < IT_STACK_SIZE);
5156 p = it->stack + it->sp;
5157
5158 p->stop_charpos = it->stop_charpos;
5159 p->prev_stop = it->prev_stop;
5160 p->base_level_stop = it->base_level_stop;
5161 p->cmp_it = it->cmp_it;
5162 xassert (it->face_id >= 0);
5163 p->face_id = it->face_id;
5164 p->string = it->string;
5165 p->method = it->method;
5166 p->from_overlay = it->from_overlay;
5167 switch (p->method)
5168 {
5169 case GET_FROM_IMAGE:
5170 p->u.image.object = it->object;
5171 p->u.image.image_id = it->image_id;
5172 p->u.image.slice = it->slice;
5173 break;
5174 case GET_FROM_STRETCH:
5175 p->u.stretch.object = it->object;
5176 break;
5177 }
5178 p->position = it->position;
5179 p->current = it->current;
5180 p->end_charpos = it->end_charpos;
5181 p->string_nchars = it->string_nchars;
5182 p->area = it->area;
5183 p->multibyte_p = it->multibyte_p;
5184 p->avoid_cursor_p = it->avoid_cursor_p;
5185 p->space_width = it->space_width;
5186 p->font_height = it->font_height;
5187 p->voffset = it->voffset;
5188 p->string_from_display_prop_p = it->string_from_display_prop_p;
5189 p->display_ellipsis_p = 0;
5190 p->line_wrap = it->line_wrap;
5191 ++it->sp;
5192 }
5193
5194
5195 /* Restore IT's settings from IT->stack. Called, for example, when no
5196 more overlay strings must be processed, and we return to delivering
5197 display elements from a buffer, or when the end of a string from a
5198 `display' property is reached and we return to delivering display
5199 elements from an overlay string, or from a buffer. */
5200
5201 static void
5202 pop_it (it)
5203 struct it *it;
5204 {
5205 struct iterator_stack_entry *p;
5206
5207 xassert (it->sp > 0);
5208 --it->sp;
5209 p = it->stack + it->sp;
5210 it->stop_charpos = p->stop_charpos;
5211 it->prev_stop = p->prev_stop;
5212 it->base_level_stop = p->base_level_stop;
5213 it->cmp_it = p->cmp_it;
5214 it->face_id = p->face_id;
5215 it->current = p->current;
5216 it->position = p->position;
5217 it->string = p->string;
5218 it->from_overlay = p->from_overlay;
5219 if (NILP (it->string))
5220 SET_TEXT_POS (it->current.string_pos, -1, -1);
5221 it->method = p->method;
5222 switch (it->method)
5223 {
5224 case GET_FROM_IMAGE:
5225 it->image_id = p->u.image.image_id;
5226 it->object = p->u.image.object;
5227 it->slice = p->u.image.slice;
5228 break;
5229 case GET_FROM_STRETCH:
5230 it->object = p->u.comp.object;
5231 break;
5232 case GET_FROM_BUFFER:
5233 it->object = it->w->buffer;
5234 break;
5235 case GET_FROM_STRING:
5236 it->object = it->string;
5237 break;
5238 case GET_FROM_DISPLAY_VECTOR:
5239 if (it->s)
5240 it->method = GET_FROM_C_STRING;
5241 else if (STRINGP (it->string))
5242 it->method = GET_FROM_STRING;
5243 else
5244 {
5245 it->method = GET_FROM_BUFFER;
5246 it->object = it->w->buffer;
5247 }
5248 }
5249 it->end_charpos = p->end_charpos;
5250 it->string_nchars = p->string_nchars;
5251 it->area = p->area;
5252 it->multibyte_p = p->multibyte_p;
5253 it->avoid_cursor_p = p->avoid_cursor_p;
5254 it->space_width = p->space_width;
5255 it->font_height = p->font_height;
5256 it->voffset = p->voffset;
5257 it->string_from_display_prop_p = p->string_from_display_prop_p;
5258 it->line_wrap = p->line_wrap;
5259 }
5260
5261
5262 \f
5263 /***********************************************************************
5264 Moving over lines
5265 ***********************************************************************/
5266
5267 /* Set IT's current position to the previous line start. */
5268
5269 static void
5270 back_to_previous_line_start (it)
5271 struct it *it;
5272 {
5273 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5274 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5275 }
5276
5277
5278 /* Move IT to the next line start.
5279
5280 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5281 we skipped over part of the text (as opposed to moving the iterator
5282 continuously over the text). Otherwise, don't change the value
5283 of *SKIPPED_P.
5284
5285 Newlines may come from buffer text, overlay strings, or strings
5286 displayed via the `display' property. That's the reason we can't
5287 simply use find_next_newline_no_quit.
5288
5289 Note that this function may not skip over invisible text that is so
5290 because of text properties and immediately follows a newline. If
5291 it would, function reseat_at_next_visible_line_start, when called
5292 from set_iterator_to_next, would effectively make invisible
5293 characters following a newline part of the wrong glyph row, which
5294 leads to wrong cursor motion. */
5295
5296 static int
5297 forward_to_next_line_start (it, skipped_p)
5298 struct it *it;
5299 int *skipped_p;
5300 {
5301 int old_selective, newline_found_p, n;
5302 const int MAX_NEWLINE_DISTANCE = 500;
5303
5304 /* If already on a newline, just consume it to avoid unintended
5305 skipping over invisible text below. */
5306 if (it->what == IT_CHARACTER
5307 && it->c == '\n'
5308 && CHARPOS (it->position) == IT_CHARPOS (*it))
5309 {
5310 set_iterator_to_next (it, 0);
5311 it->c = 0;
5312 return 1;
5313 }
5314
5315 /* Don't handle selective display in the following. It's (a)
5316 unnecessary because it's done by the caller, and (b) leads to an
5317 infinite recursion because next_element_from_ellipsis indirectly
5318 calls this function. */
5319 old_selective = it->selective;
5320 it->selective = 0;
5321
5322 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5323 from buffer text. */
5324 for (n = newline_found_p = 0;
5325 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5326 n += STRINGP (it->string) ? 0 : 1)
5327 {
5328 if (!get_next_display_element (it))
5329 return 0;
5330 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5331 set_iterator_to_next (it, 0);
5332 }
5333
5334 /* If we didn't find a newline near enough, see if we can use a
5335 short-cut. */
5336 if (!newline_found_p)
5337 {
5338 int start = IT_CHARPOS (*it);
5339 int limit = find_next_newline_no_quit (start, 1);
5340 Lisp_Object pos;
5341
5342 xassert (!STRINGP (it->string));
5343
5344 /* If there isn't any `display' property in sight, and no
5345 overlays, we can just use the position of the newline in
5346 buffer text. */
5347 if (it->stop_charpos >= limit
5348 || ((pos = Fnext_single_property_change (make_number (start),
5349 Qdisplay,
5350 Qnil, make_number (limit)),
5351 NILP (pos))
5352 && next_overlay_change (start) == ZV))
5353 {
5354 IT_CHARPOS (*it) = limit;
5355 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5356 *skipped_p = newline_found_p = 1;
5357 }
5358 else
5359 {
5360 while (get_next_display_element (it)
5361 && !newline_found_p)
5362 {
5363 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5364 set_iterator_to_next (it, 0);
5365 }
5366 }
5367 }
5368
5369 it->selective = old_selective;
5370 return newline_found_p;
5371 }
5372
5373
5374 /* Set IT's current position to the previous visible line start. Skip
5375 invisible text that is so either due to text properties or due to
5376 selective display. Caution: this does not change IT->current_x and
5377 IT->hpos. */
5378
5379 static void
5380 back_to_previous_visible_line_start (it)
5381 struct it *it;
5382 {
5383 while (IT_CHARPOS (*it) > BEGV)
5384 {
5385 back_to_previous_line_start (it);
5386
5387 if (IT_CHARPOS (*it) <= BEGV)
5388 break;
5389
5390 /* If selective > 0, then lines indented more than its value are
5391 invisible. */
5392 if (it->selective > 0
5393 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5394 (double) it->selective)) /* iftc */
5395 continue;
5396
5397 /* Check the newline before point for invisibility. */
5398 {
5399 Lisp_Object prop;
5400 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5401 Qinvisible, it->window);
5402 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5403 continue;
5404 }
5405
5406 if (IT_CHARPOS (*it) <= BEGV)
5407 break;
5408
5409 {
5410 struct it it2;
5411 int pos;
5412 EMACS_INT beg, end;
5413 Lisp_Object val, overlay;
5414
5415 /* If newline is part of a composition, continue from start of composition */
5416 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5417 && beg < IT_CHARPOS (*it))
5418 goto replaced;
5419
5420 /* If newline is replaced by a display property, find start of overlay
5421 or interval and continue search from that point. */
5422 it2 = *it;
5423 pos = --IT_CHARPOS (it2);
5424 --IT_BYTEPOS (it2);
5425 it2.sp = 0;
5426 it2.string_from_display_prop_p = 0;
5427 if (handle_display_prop (&it2) == HANDLED_RETURN
5428 && !NILP (val = get_char_property_and_overlay
5429 (make_number (pos), Qdisplay, Qnil, &overlay))
5430 && (OVERLAYP (overlay)
5431 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5432 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5433 goto replaced;
5434
5435 /* Newline is not replaced by anything -- so we are done. */
5436 break;
5437
5438 replaced:
5439 if (beg < BEGV)
5440 beg = BEGV;
5441 IT_CHARPOS (*it) = beg;
5442 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5443 }
5444 }
5445
5446 it->continuation_lines_width = 0;
5447
5448 xassert (IT_CHARPOS (*it) >= BEGV);
5449 xassert (IT_CHARPOS (*it) == BEGV
5450 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5451 CHECK_IT (it);
5452 }
5453
5454
5455 /* Reseat iterator IT at the previous visible line start. Skip
5456 invisible text that is so either due to text properties or due to
5457 selective display. At the end, update IT's overlay information,
5458 face information etc. */
5459
5460 void
5461 reseat_at_previous_visible_line_start (it)
5462 struct it *it;
5463 {
5464 back_to_previous_visible_line_start (it);
5465 reseat (it, it->current.pos, 1);
5466 CHECK_IT (it);
5467 }
5468
5469
5470 /* Reseat iterator IT on the next visible line start in the current
5471 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5472 preceding the line start. Skip over invisible text that is so
5473 because of selective display. Compute faces, overlays etc at the
5474 new position. Note that this function does not skip over text that
5475 is invisible because of text properties. */
5476
5477 static void
5478 reseat_at_next_visible_line_start (it, on_newline_p)
5479 struct it *it;
5480 int on_newline_p;
5481 {
5482 int newline_found_p, skipped_p = 0;
5483
5484 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5485
5486 /* Skip over lines that are invisible because they are indented
5487 more than the value of IT->selective. */
5488 if (it->selective > 0)
5489 while (IT_CHARPOS (*it) < ZV
5490 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5491 (double) it->selective)) /* iftc */
5492 {
5493 xassert (IT_BYTEPOS (*it) == BEGV
5494 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5495 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5496 }
5497
5498 /* Position on the newline if that's what's requested. */
5499 if (on_newline_p && newline_found_p)
5500 {
5501 if (STRINGP (it->string))
5502 {
5503 if (IT_STRING_CHARPOS (*it) > 0)
5504 {
5505 --IT_STRING_CHARPOS (*it);
5506 --IT_STRING_BYTEPOS (*it);
5507 }
5508 }
5509 else if (IT_CHARPOS (*it) > BEGV)
5510 {
5511 --IT_CHARPOS (*it);
5512 --IT_BYTEPOS (*it);
5513 reseat (it, it->current.pos, 0);
5514 }
5515 }
5516 else if (skipped_p)
5517 reseat (it, it->current.pos, 0);
5518
5519 CHECK_IT (it);
5520 }
5521
5522
5523 \f
5524 /***********************************************************************
5525 Changing an iterator's position
5526 ***********************************************************************/
5527
5528 /* Change IT's current position to POS in current_buffer. If FORCE_P
5529 is non-zero, always check for text properties at the new position.
5530 Otherwise, text properties are only looked up if POS >=
5531 IT->check_charpos of a property. */
5532
5533 static void
5534 reseat (it, pos, force_p)
5535 struct it *it;
5536 struct text_pos pos;
5537 int force_p;
5538 {
5539 int original_pos = IT_CHARPOS (*it);
5540
5541 reseat_1 (it, pos, 0);
5542
5543 /* Determine where to check text properties. Avoid doing it
5544 where possible because text property lookup is very expensive. */
5545 if (force_p
5546 || CHARPOS (pos) > it->stop_charpos
5547 || CHARPOS (pos) < original_pos)
5548 {
5549 if (it->bidi_p)
5550 {
5551 /* For bidi iteration, we need to prime prev_stop and
5552 base_level_stop with our best estimations. */
5553 if (CHARPOS (pos) < it->prev_stop)
5554 {
5555 handle_stop_backwards (it, BEGV);
5556 if (CHARPOS (pos) < it->base_level_stop)
5557 it->base_level_stop = 0;
5558 }
5559 else if (CHARPOS (pos) > it->stop_charpos
5560 && it->stop_charpos >= BEGV)
5561 handle_stop_backwards (it, it->stop_charpos);
5562 else /* force_p */
5563 handle_stop (it);
5564 }
5565 else
5566 {
5567 handle_stop (it);
5568 it->prev_stop = it->base_level_stop = 0;
5569 }
5570
5571 }
5572
5573 CHECK_IT (it);
5574 }
5575
5576
5577 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5578 IT->stop_pos to POS, also. */
5579
5580 static void
5581 reseat_1 (it, pos, set_stop_p)
5582 struct it *it;
5583 struct text_pos pos;
5584 int set_stop_p;
5585 {
5586 /* Don't call this function when scanning a C string. */
5587 xassert (it->s == NULL);
5588
5589 /* POS must be a reasonable value. */
5590 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5591
5592 it->current.pos = it->position = pos;
5593 it->end_charpos = ZV;
5594 it->dpvec = NULL;
5595 it->current.dpvec_index = -1;
5596 it->current.overlay_string_index = -1;
5597 IT_STRING_CHARPOS (*it) = -1;
5598 IT_STRING_BYTEPOS (*it) = -1;
5599 it->string = Qnil;
5600 it->string_from_display_prop_p = 0;
5601 it->method = GET_FROM_BUFFER;
5602 it->object = it->w->buffer;
5603 it->area = TEXT_AREA;
5604 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5605 it->sp = 0;
5606 it->string_from_display_prop_p = 0;
5607 it->face_before_selective_p = 0;
5608 if (it->bidi_p)
5609 it->bidi_it.first_elt = 1;
5610
5611 if (set_stop_p)
5612 {
5613 it->stop_charpos = CHARPOS (pos);
5614 it->base_level_stop = CHARPOS (pos);
5615 }
5616 }
5617
5618
5619 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5620 If S is non-null, it is a C string to iterate over. Otherwise,
5621 STRING gives a Lisp string to iterate over.
5622
5623 If PRECISION > 0, don't return more then PRECISION number of
5624 characters from the string.
5625
5626 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5627 characters have been returned. FIELD_WIDTH < 0 means an infinite
5628 field width.
5629
5630 MULTIBYTE = 0 means disable processing of multibyte characters,
5631 MULTIBYTE > 0 means enable it,
5632 MULTIBYTE < 0 means use IT->multibyte_p.
5633
5634 IT must be initialized via a prior call to init_iterator before
5635 calling this function. */
5636
5637 static void
5638 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5639 struct it *it;
5640 unsigned char *s;
5641 Lisp_Object string;
5642 int charpos;
5643 int precision, field_width, multibyte;
5644 {
5645 /* No region in strings. */
5646 it->region_beg_charpos = it->region_end_charpos = -1;
5647
5648 /* No text property checks performed by default, but see below. */
5649 it->stop_charpos = -1;
5650
5651 /* Set iterator position and end position. */
5652 bzero (&it->current, sizeof it->current);
5653 it->current.overlay_string_index = -1;
5654 it->current.dpvec_index = -1;
5655 xassert (charpos >= 0);
5656
5657 /* If STRING is specified, use its multibyteness, otherwise use the
5658 setting of MULTIBYTE, if specified. */
5659 if (multibyte >= 0)
5660 it->multibyte_p = multibyte > 0;
5661
5662 if (s == NULL)
5663 {
5664 xassert (STRINGP (string));
5665 it->string = string;
5666 it->s = NULL;
5667 it->end_charpos = it->string_nchars = SCHARS (string);
5668 it->method = GET_FROM_STRING;
5669 it->current.string_pos = string_pos (charpos, string);
5670 }
5671 else
5672 {
5673 it->s = s;
5674 it->string = Qnil;
5675
5676 /* Note that we use IT->current.pos, not it->current.string_pos,
5677 for displaying C strings. */
5678 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5679 if (it->multibyte_p)
5680 {
5681 it->current.pos = c_string_pos (charpos, s, 1);
5682 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5683 }
5684 else
5685 {
5686 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5687 it->end_charpos = it->string_nchars = strlen (s);
5688 }
5689
5690 it->method = GET_FROM_C_STRING;
5691 }
5692
5693 /* PRECISION > 0 means don't return more than PRECISION characters
5694 from the string. */
5695 if (precision > 0 && it->end_charpos - charpos > precision)
5696 it->end_charpos = it->string_nchars = charpos + precision;
5697
5698 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5699 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5700 FIELD_WIDTH < 0 means infinite field width. This is useful for
5701 padding with `-' at the end of a mode line. */
5702 if (field_width < 0)
5703 field_width = INFINITY;
5704 if (field_width > it->end_charpos - charpos)
5705 it->end_charpos = charpos + field_width;
5706
5707 /* Use the standard display table for displaying strings. */
5708 if (DISP_TABLE_P (Vstandard_display_table))
5709 it->dp = XCHAR_TABLE (Vstandard_display_table);
5710
5711 it->stop_charpos = charpos;
5712 if (s == NULL && it->multibyte_p)
5713 {
5714 EMACS_INT endpos = SCHARS (it->string);
5715 if (endpos > it->end_charpos)
5716 endpos = it->end_charpos;
5717 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5718 it->string);
5719 }
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 bidi_get_next_char_visually (&it->bidi_it);
6665 }
6666 else
6667 {
6668 int orig_bytepos = IT_BYTEPOS (*it);
6669
6670 /* We need to prime the bidi iterator starting at the line's
6671 beginning, before we will be able to produce the next
6672 element. */
6673 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6674 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6675 it->bidi_it.charpos = IT_CHARPOS (*it);
6676 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6677 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6678 do
6679 {
6680 /* Now return to buffer position where we were asked to
6681 get the next display element, and produce that. */
6682 bidi_get_next_char_visually (&it->bidi_it);
6683 }
6684 while (it->bidi_it.bytepos != orig_bytepos
6685 && it->bidi_it.bytepos < ZV_BYTE);
6686 }
6687
6688 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6689 /* Adjust IT's position information to where we ended up. */
6690 IT_CHARPOS (*it) = it->bidi_it.charpos;
6691 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6692 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6693 }
6694
6695 if (IT_CHARPOS (*it) >= it->stop_charpos)
6696 {
6697 if (IT_CHARPOS (*it) >= it->end_charpos)
6698 {
6699 int overlay_strings_follow_p;
6700
6701 /* End of the game, except when overlay strings follow that
6702 haven't been returned yet. */
6703 if (it->overlay_strings_at_end_processed_p)
6704 overlay_strings_follow_p = 0;
6705 else
6706 {
6707 it->overlay_strings_at_end_processed_p = 1;
6708 overlay_strings_follow_p = get_overlay_strings (it, 0);
6709 }
6710
6711 if (overlay_strings_follow_p)
6712 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6713 else
6714 {
6715 it->what = IT_EOB;
6716 it->position = it->current.pos;
6717 success_p = 0;
6718 }
6719 }
6720 else if (!(!it->bidi_p
6721 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6722 || IT_CHARPOS (*it) == it->stop_charpos))
6723 {
6724 /* With bidi non-linear iteration, we could find ourselves
6725 far beyond the last computed stop_charpos, with several
6726 other stop positions in between that we missed. Scan
6727 them all now, in buffer's logical order, until we find
6728 and handle the last stop_charpos that precedes our
6729 current position. */
6730 handle_stop_backwards (it, it->stop_charpos);
6731 return GET_NEXT_DISPLAY_ELEMENT (it);
6732 }
6733 else
6734 {
6735 if (it->bidi_p)
6736 {
6737 /* Take note of the stop position we just moved across,
6738 for when we will move back across it. */
6739 it->prev_stop = it->stop_charpos;
6740 /* If we are at base paragraph embedding level, take
6741 note of the last stop position seen at this
6742 level. */
6743 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6744 it->base_level_stop = it->stop_charpos;
6745 }
6746 handle_stop (it);
6747 return GET_NEXT_DISPLAY_ELEMENT (it);
6748 }
6749 }
6750 else if (it->bidi_p
6751 /* We can sometimes back up for reasons that have nothing
6752 to do with bidi reordering. E.g., compositions. The
6753 code below is only needed when we are above the base
6754 embedding level, so test for that explicitly. */
6755 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6756 && IT_CHARPOS (*it) < it->prev_stop)
6757 {
6758 if (it->base_level_stop <= 0)
6759 it->base_level_stop = BEGV;
6760 if (IT_CHARPOS (*it) < it->base_level_stop)
6761 abort ();
6762 handle_stop_backwards (it, it->base_level_stop);
6763 return GET_NEXT_DISPLAY_ELEMENT (it);
6764 }
6765 else
6766 {
6767 /* No face changes, overlays etc. in sight, so just return a
6768 character from current_buffer. */
6769 unsigned char *p;
6770
6771 /* Maybe run the redisplay end trigger hook. Performance note:
6772 This doesn't seem to cost measurable time. */
6773 if (it->redisplay_end_trigger_charpos
6774 && it->glyph_row
6775 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6776 run_redisplay_end_trigger_hook (it);
6777
6778 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6779 it->end_charpos)
6780 && next_element_from_composition (it))
6781 {
6782 return 1;
6783 }
6784
6785 /* Get the next character, maybe multibyte. */
6786 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6787 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6788 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6789 else
6790 it->c = *p, it->len = 1;
6791
6792 /* Record what we have and where it came from. */
6793 it->what = IT_CHARACTER;
6794 it->object = it->w->buffer;
6795 it->position = it->current.pos;
6796
6797 /* Normally we return the character found above, except when we
6798 really want to return an ellipsis for selective display. */
6799 if (it->selective)
6800 {
6801 if (it->c == '\n')
6802 {
6803 /* A value of selective > 0 means hide lines indented more
6804 than that number of columns. */
6805 if (it->selective > 0
6806 && IT_CHARPOS (*it) + 1 < ZV
6807 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6808 IT_BYTEPOS (*it) + 1,
6809 (double) it->selective)) /* iftc */
6810 {
6811 success_p = next_element_from_ellipsis (it);
6812 it->dpvec_char_len = -1;
6813 }
6814 }
6815 else if (it->c == '\r' && it->selective == -1)
6816 {
6817 /* A value of selective == -1 means that everything from the
6818 CR to the end of the line is invisible, with maybe an
6819 ellipsis displayed for it. */
6820 success_p = next_element_from_ellipsis (it);
6821 it->dpvec_char_len = -1;
6822 }
6823 }
6824 }
6825
6826 /* Value is zero if end of buffer reached. */
6827 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6828 return success_p;
6829 }
6830
6831
6832 /* Run the redisplay end trigger hook for IT. */
6833
6834 static void
6835 run_redisplay_end_trigger_hook (it)
6836 struct it *it;
6837 {
6838 Lisp_Object args[3];
6839
6840 /* IT->glyph_row should be non-null, i.e. we should be actually
6841 displaying something, or otherwise we should not run the hook. */
6842 xassert (it->glyph_row);
6843
6844 /* Set up hook arguments. */
6845 args[0] = Qredisplay_end_trigger_functions;
6846 args[1] = it->window;
6847 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6848 it->redisplay_end_trigger_charpos = 0;
6849
6850 /* Since we are *trying* to run these functions, don't try to run
6851 them again, even if they get an error. */
6852 it->w->redisplay_end_trigger = Qnil;
6853 Frun_hook_with_args (3, args);
6854
6855 /* Notice if it changed the face of the character we are on. */
6856 handle_face_prop (it);
6857 }
6858
6859
6860 /* Deliver a composition display element. Unlike the other
6861 next_element_from_XXX, this function is not registered in the array
6862 get_next_element[]. It is called from next_element_from_buffer and
6863 next_element_from_string when necessary. */
6864
6865 static int
6866 next_element_from_composition (it)
6867 struct it *it;
6868 {
6869 it->what = IT_COMPOSITION;
6870 it->len = it->cmp_it.nbytes;
6871 if (STRINGP (it->string))
6872 {
6873 if (it->c < 0)
6874 {
6875 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6876 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6877 return 0;
6878 }
6879 it->position = it->current.string_pos;
6880 it->object = it->string;
6881 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6882 IT_STRING_BYTEPOS (*it), it->string);
6883 }
6884 else
6885 {
6886 if (it->c < 0)
6887 {
6888 IT_CHARPOS (*it) += it->cmp_it.nchars;
6889 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6890 return 0;
6891 }
6892 it->position = it->current.pos;
6893 it->object = it->w->buffer;
6894 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6895 IT_BYTEPOS (*it), Qnil);
6896 }
6897 return 1;
6898 }
6899
6900
6901 \f
6902 /***********************************************************************
6903 Moving an iterator without producing glyphs
6904 ***********************************************************************/
6905
6906 /* Check if iterator is at a position corresponding to a valid buffer
6907 position after some move_it_ call. */
6908
6909 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6910 ((it)->method == GET_FROM_STRING \
6911 ? IT_STRING_CHARPOS (*it) == 0 \
6912 : 1)
6913
6914
6915 /* Move iterator IT to a specified buffer or X position within one
6916 line on the display without producing glyphs.
6917
6918 OP should be a bit mask including some or all of these bits:
6919 MOVE_TO_X: Stop upon reaching x-position TO_X.
6920 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6921 Regardless of OP's value, stop upon reaching the end of the display line.
6922
6923 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6924 This means, in particular, that TO_X includes window's horizontal
6925 scroll amount.
6926
6927 The return value has several possible values that
6928 say what condition caused the scan to stop:
6929
6930 MOVE_POS_MATCH_OR_ZV
6931 - when TO_POS or ZV was reached.
6932
6933 MOVE_X_REACHED
6934 -when TO_X was reached before TO_POS or ZV were reached.
6935
6936 MOVE_LINE_CONTINUED
6937 - when we reached the end of the display area and the line must
6938 be continued.
6939
6940 MOVE_LINE_TRUNCATED
6941 - when we reached the end of the display area and the line is
6942 truncated.
6943
6944 MOVE_NEWLINE_OR_CR
6945 - when we stopped at a line end, i.e. a newline or a CR and selective
6946 display is on. */
6947
6948 static enum move_it_result
6949 move_it_in_display_line_to (struct it *it,
6950 EMACS_INT to_charpos, int to_x,
6951 enum move_operation_enum op)
6952 {
6953 enum move_it_result result = MOVE_UNDEFINED;
6954 struct glyph_row *saved_glyph_row;
6955 struct it wrap_it, atpos_it, atx_it;
6956 int may_wrap = 0;
6957 enum it_method prev_method = it->method;
6958 EMACS_INT prev_pos = IT_CHARPOS (*it);
6959
6960 /* Don't produce glyphs in produce_glyphs. */
6961 saved_glyph_row = it->glyph_row;
6962 it->glyph_row = NULL;
6963
6964 /* Use wrap_it to save a copy of IT wherever a word wrap could
6965 occur. Use atpos_it to save a copy of IT at the desired buffer
6966 position, if found, so that we can scan ahead and check if the
6967 word later overshoots the window edge. Use atx_it similarly, for
6968 pixel positions. */
6969 wrap_it.sp = -1;
6970 atpos_it.sp = -1;
6971 atx_it.sp = -1;
6972
6973 #define BUFFER_POS_REACHED_P() \
6974 ((op & MOVE_TO_POS) != 0 \
6975 && BUFFERP (it->object) \
6976 && (IT_CHARPOS (*it) == to_charpos \
6977 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
6978 && (it->method == GET_FROM_BUFFER \
6979 || (it->method == GET_FROM_DISPLAY_VECTOR \
6980 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6981
6982 /* If there's a line-/wrap-prefix, handle it. */
6983 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6984 && it->current_y < it->last_visible_y)
6985 handle_line_prefix (it);
6986
6987 while (1)
6988 {
6989 int x, i, ascent = 0, descent = 0;
6990
6991 /* Utility macro to reset an iterator with x, ascent, and descent. */
6992 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6993 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6994 (IT)->max_descent = descent)
6995
6996 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6997 glyph). */
6998 if ((op & MOVE_TO_POS) != 0
6999 && BUFFERP (it->object)
7000 && it->method == GET_FROM_BUFFER
7001 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7002 || (it->bidi_p
7003 && (prev_method == GET_FROM_IMAGE
7004 || prev_method == GET_FROM_STRETCH)
7005 /* Passed TO_CHARPOS from left to right. */
7006 && ((prev_pos < to_charpos
7007 && IT_CHARPOS (*it) > to_charpos)
7008 /* Passed TO_CHARPOS from right to left. */
7009 || (prev_pos > to_charpos
7010 && IT_CHARPOS (*it) < to_charpos)))))
7011 {
7012 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7013 {
7014 result = MOVE_POS_MATCH_OR_ZV;
7015 break;
7016 }
7017 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7018 /* If wrap_it is valid, the current position might be in a
7019 word that is wrapped. So, save the iterator in
7020 atpos_it and continue to see if wrapping happens. */
7021 atpos_it = *it;
7022 }
7023
7024 prev_method = it->method;
7025 if (it->method == GET_FROM_BUFFER)
7026 prev_pos = IT_CHARPOS (*it);
7027 /* Stop when ZV reached.
7028 We used to stop here when TO_CHARPOS reached as well, but that is
7029 too soon if this glyph does not fit on this line. So we handle it
7030 explicitly below. */
7031 if (!get_next_display_element (it))
7032 {
7033 result = MOVE_POS_MATCH_OR_ZV;
7034 break;
7035 }
7036
7037 if (it->line_wrap == TRUNCATE)
7038 {
7039 if (BUFFER_POS_REACHED_P ())
7040 {
7041 result = MOVE_POS_MATCH_OR_ZV;
7042 break;
7043 }
7044 }
7045 else
7046 {
7047 if (it->line_wrap == WORD_WRAP)
7048 {
7049 if (IT_DISPLAYING_WHITESPACE (it))
7050 may_wrap = 1;
7051 else if (may_wrap)
7052 {
7053 /* We have reached a glyph that follows one or more
7054 whitespace characters. If the position is
7055 already found, we are done. */
7056 if (atpos_it.sp >= 0)
7057 {
7058 *it = atpos_it;
7059 result = MOVE_POS_MATCH_OR_ZV;
7060 goto done;
7061 }
7062 if (atx_it.sp >= 0)
7063 {
7064 *it = atx_it;
7065 result = MOVE_X_REACHED;
7066 goto done;
7067 }
7068 /* Otherwise, we can wrap here. */
7069 wrap_it = *it;
7070 may_wrap = 0;
7071 }
7072 }
7073 }
7074
7075 /* Remember the line height for the current line, in case
7076 the next element doesn't fit on the line. */
7077 ascent = it->max_ascent;
7078 descent = it->max_descent;
7079
7080 /* The call to produce_glyphs will get the metrics of the
7081 display element IT is loaded with. Record the x-position
7082 before this display element, in case it doesn't fit on the
7083 line. */
7084 x = it->current_x;
7085
7086 PRODUCE_GLYPHS (it);
7087
7088 if (it->area != TEXT_AREA)
7089 {
7090 set_iterator_to_next (it, 1);
7091 continue;
7092 }
7093
7094 /* The number of glyphs we get back in IT->nglyphs will normally
7095 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7096 character on a terminal frame, or (iii) a line end. For the
7097 second case, IT->nglyphs - 1 padding glyphs will be present.
7098 (On X frames, there is only one glyph produced for a
7099 composite character.)
7100
7101 The behavior implemented below means, for continuation lines,
7102 that as many spaces of a TAB as fit on the current line are
7103 displayed there. For terminal frames, as many glyphs of a
7104 multi-glyph character are displayed in the current line, too.
7105 This is what the old redisplay code did, and we keep it that
7106 way. Under X, the whole shape of a complex character must
7107 fit on the line or it will be completely displayed in the
7108 next line.
7109
7110 Note that both for tabs and padding glyphs, all glyphs have
7111 the same width. */
7112 if (it->nglyphs)
7113 {
7114 /* More than one glyph or glyph doesn't fit on line. All
7115 glyphs have the same width. */
7116 int single_glyph_width = it->pixel_width / it->nglyphs;
7117 int new_x;
7118 int x_before_this_char = x;
7119 int hpos_before_this_char = it->hpos;
7120
7121 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7122 {
7123 new_x = x + single_glyph_width;
7124
7125 /* We want to leave anything reaching TO_X to the caller. */
7126 if ((op & MOVE_TO_X) && new_x > to_x)
7127 {
7128 if (BUFFER_POS_REACHED_P ())
7129 {
7130 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7131 goto buffer_pos_reached;
7132 if (atpos_it.sp < 0)
7133 {
7134 atpos_it = *it;
7135 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7136 }
7137 }
7138 else
7139 {
7140 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7141 {
7142 it->current_x = x;
7143 result = MOVE_X_REACHED;
7144 break;
7145 }
7146 if (atx_it.sp < 0)
7147 {
7148 atx_it = *it;
7149 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7150 }
7151 }
7152 }
7153
7154 if (/* Lines are continued. */
7155 it->line_wrap != TRUNCATE
7156 && (/* And glyph doesn't fit on the line. */
7157 new_x > it->last_visible_x
7158 /* Or it fits exactly and we're on a window
7159 system frame. */
7160 || (new_x == it->last_visible_x
7161 && FRAME_WINDOW_P (it->f))))
7162 {
7163 if (/* IT->hpos == 0 means the very first glyph
7164 doesn't fit on the line, e.g. a wide image. */
7165 it->hpos == 0
7166 || (new_x == it->last_visible_x
7167 && FRAME_WINDOW_P (it->f)))
7168 {
7169 ++it->hpos;
7170 it->current_x = new_x;
7171
7172 /* The character's last glyph just barely fits
7173 in this row. */
7174 if (i == it->nglyphs - 1)
7175 {
7176 /* If this is the destination position,
7177 return a position *before* it in this row,
7178 now that we know it fits in this row. */
7179 if (BUFFER_POS_REACHED_P ())
7180 {
7181 if (it->line_wrap != WORD_WRAP
7182 || wrap_it.sp < 0)
7183 {
7184 it->hpos = hpos_before_this_char;
7185 it->current_x = x_before_this_char;
7186 result = MOVE_POS_MATCH_OR_ZV;
7187 break;
7188 }
7189 if (it->line_wrap == WORD_WRAP
7190 && atpos_it.sp < 0)
7191 {
7192 atpos_it = *it;
7193 atpos_it.current_x = x_before_this_char;
7194 atpos_it.hpos = hpos_before_this_char;
7195 }
7196 }
7197
7198 set_iterator_to_next (it, 1);
7199 /* On graphical terminals, newlines may
7200 "overflow" into the fringe if
7201 overflow-newline-into-fringe is non-nil.
7202 On text-only terminals, newlines may
7203 overflow into the last glyph on the
7204 display line.*/
7205 if (!FRAME_WINDOW_P (it->f)
7206 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7207 {
7208 if (!get_next_display_element (it))
7209 {
7210 result = MOVE_POS_MATCH_OR_ZV;
7211 break;
7212 }
7213 if (BUFFER_POS_REACHED_P ())
7214 {
7215 if (ITERATOR_AT_END_OF_LINE_P (it))
7216 result = MOVE_POS_MATCH_OR_ZV;
7217 else
7218 result = MOVE_LINE_CONTINUED;
7219 break;
7220 }
7221 if (ITERATOR_AT_END_OF_LINE_P (it))
7222 {
7223 result = MOVE_NEWLINE_OR_CR;
7224 break;
7225 }
7226 }
7227 }
7228 }
7229 else
7230 IT_RESET_X_ASCENT_DESCENT (it);
7231
7232 if (wrap_it.sp >= 0)
7233 {
7234 *it = wrap_it;
7235 atpos_it.sp = -1;
7236 atx_it.sp = -1;
7237 }
7238
7239 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7240 IT_CHARPOS (*it)));
7241 result = MOVE_LINE_CONTINUED;
7242 break;
7243 }
7244
7245 if (BUFFER_POS_REACHED_P ())
7246 {
7247 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7248 goto buffer_pos_reached;
7249 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7250 {
7251 atpos_it = *it;
7252 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7253 }
7254 }
7255
7256 if (new_x > it->first_visible_x)
7257 {
7258 /* Glyph is visible. Increment number of glyphs that
7259 would be displayed. */
7260 ++it->hpos;
7261 }
7262 }
7263
7264 if (result != MOVE_UNDEFINED)
7265 break;
7266 }
7267 else if (BUFFER_POS_REACHED_P ())
7268 {
7269 buffer_pos_reached:
7270 IT_RESET_X_ASCENT_DESCENT (it);
7271 result = MOVE_POS_MATCH_OR_ZV;
7272 break;
7273 }
7274 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7275 {
7276 /* Stop when TO_X specified and reached. This check is
7277 necessary here because of lines consisting of a line end,
7278 only. The line end will not produce any glyphs and we
7279 would never get MOVE_X_REACHED. */
7280 xassert (it->nglyphs == 0);
7281 result = MOVE_X_REACHED;
7282 break;
7283 }
7284
7285 /* Is this a line end? If yes, we're done. */
7286 if (ITERATOR_AT_END_OF_LINE_P (it))
7287 {
7288 result = MOVE_NEWLINE_OR_CR;
7289 break;
7290 }
7291
7292 if (it->method == GET_FROM_BUFFER)
7293 prev_pos = IT_CHARPOS (*it);
7294 /* The current display element has been consumed. Advance
7295 to the next. */
7296 set_iterator_to_next (it, 1);
7297
7298 /* Stop if lines are truncated and IT's current x-position is
7299 past the right edge of the window now. */
7300 if (it->line_wrap == TRUNCATE
7301 && it->current_x >= it->last_visible_x)
7302 {
7303 if (!FRAME_WINDOW_P (it->f)
7304 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7305 {
7306 if (!get_next_display_element (it)
7307 || BUFFER_POS_REACHED_P ())
7308 {
7309 result = MOVE_POS_MATCH_OR_ZV;
7310 break;
7311 }
7312 if (ITERATOR_AT_END_OF_LINE_P (it))
7313 {
7314 result = MOVE_NEWLINE_OR_CR;
7315 break;
7316 }
7317 }
7318 result = MOVE_LINE_TRUNCATED;
7319 break;
7320 }
7321 #undef IT_RESET_X_ASCENT_DESCENT
7322 }
7323
7324 #undef BUFFER_POS_REACHED_P
7325
7326 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7327 restore the saved iterator. */
7328 if (atpos_it.sp >= 0)
7329 *it = atpos_it;
7330 else if (atx_it.sp >= 0)
7331 *it = atx_it;
7332
7333 done:
7334
7335 /* Restore the iterator settings altered at the beginning of this
7336 function. */
7337 it->glyph_row = saved_glyph_row;
7338 return result;
7339 }
7340
7341 /* For external use. */
7342 void
7343 move_it_in_display_line (struct it *it,
7344 EMACS_INT to_charpos, int to_x,
7345 enum move_operation_enum op)
7346 {
7347 if (it->line_wrap == WORD_WRAP
7348 && (op & MOVE_TO_X))
7349 {
7350 struct it save_it = *it;
7351 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7352 /* When word-wrap is on, TO_X may lie past the end
7353 of a wrapped line. Then it->current is the
7354 character on the next line, so backtrack to the
7355 space before the wrap point. */
7356 if (skip == MOVE_LINE_CONTINUED)
7357 {
7358 int prev_x = max (it->current_x - 1, 0);
7359 *it = save_it;
7360 move_it_in_display_line_to
7361 (it, -1, prev_x, MOVE_TO_X);
7362 }
7363 }
7364 else
7365 move_it_in_display_line_to (it, to_charpos, to_x, op);
7366 }
7367
7368
7369 /* Move IT forward until it satisfies one or more of the criteria in
7370 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7371
7372 OP is a bit-mask that specifies where to stop, and in particular,
7373 which of those four position arguments makes a difference. See the
7374 description of enum move_operation_enum.
7375
7376 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7377 screen line, this function will set IT to the next position >
7378 TO_CHARPOS. */
7379
7380 void
7381 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7382 struct it *it;
7383 int to_charpos, to_x, to_y, to_vpos;
7384 int op;
7385 {
7386 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7387 int line_height, line_start_x = 0, reached = 0;
7388
7389 for (;;)
7390 {
7391 if (op & MOVE_TO_VPOS)
7392 {
7393 /* If no TO_CHARPOS and no TO_X specified, stop at the
7394 start of the line TO_VPOS. */
7395 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7396 {
7397 if (it->vpos == to_vpos)
7398 {
7399 reached = 1;
7400 break;
7401 }
7402 else
7403 skip = move_it_in_display_line_to (it, -1, -1, 0);
7404 }
7405 else
7406 {
7407 /* TO_VPOS >= 0 means stop at TO_X in the line at
7408 TO_VPOS, or at TO_POS, whichever comes first. */
7409 if (it->vpos == to_vpos)
7410 {
7411 reached = 2;
7412 break;
7413 }
7414
7415 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7416
7417 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7418 {
7419 reached = 3;
7420 break;
7421 }
7422 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7423 {
7424 /* We have reached TO_X but not in the line we want. */
7425 skip = move_it_in_display_line_to (it, to_charpos,
7426 -1, MOVE_TO_POS);
7427 if (skip == MOVE_POS_MATCH_OR_ZV)
7428 {
7429 reached = 4;
7430 break;
7431 }
7432 }
7433 }
7434 }
7435 else if (op & MOVE_TO_Y)
7436 {
7437 struct it it_backup;
7438
7439 if (it->line_wrap == WORD_WRAP)
7440 it_backup = *it;
7441
7442 /* TO_Y specified means stop at TO_X in the line containing
7443 TO_Y---or at TO_CHARPOS if this is reached first. The
7444 problem is that we can't really tell whether the line
7445 contains TO_Y before we have completely scanned it, and
7446 this may skip past TO_X. What we do is to first scan to
7447 TO_X.
7448
7449 If TO_X is not specified, use a TO_X of zero. The reason
7450 is to make the outcome of this function more predictable.
7451 If we didn't use TO_X == 0, we would stop at the end of
7452 the line which is probably not what a caller would expect
7453 to happen. */
7454 skip = move_it_in_display_line_to
7455 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7456 (MOVE_TO_X | (op & MOVE_TO_POS)));
7457
7458 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7459 if (skip == MOVE_POS_MATCH_OR_ZV)
7460 reached = 5;
7461 else if (skip == MOVE_X_REACHED)
7462 {
7463 /* If TO_X was reached, we want to know whether TO_Y is
7464 in the line. We know this is the case if the already
7465 scanned glyphs make the line tall enough. Otherwise,
7466 we must check by scanning the rest of the line. */
7467 line_height = it->max_ascent + it->max_descent;
7468 if (to_y >= it->current_y
7469 && to_y < it->current_y + line_height)
7470 {
7471 reached = 6;
7472 break;
7473 }
7474 it_backup = *it;
7475 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7476 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7477 op & MOVE_TO_POS);
7478 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7479 line_height = it->max_ascent + it->max_descent;
7480 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7481
7482 if (to_y >= it->current_y
7483 && to_y < it->current_y + line_height)
7484 {
7485 /* If TO_Y is in this line and TO_X was reached
7486 above, we scanned too far. We have to restore
7487 IT's settings to the ones before skipping. */
7488 *it = it_backup;
7489 reached = 6;
7490 }
7491 else
7492 {
7493 skip = skip2;
7494 if (skip == MOVE_POS_MATCH_OR_ZV)
7495 reached = 7;
7496 }
7497 }
7498 else
7499 {
7500 /* Check whether TO_Y is in this line. */
7501 line_height = it->max_ascent + it->max_descent;
7502 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7503
7504 if (to_y >= it->current_y
7505 && to_y < it->current_y + line_height)
7506 {
7507 /* When word-wrap is on, TO_X may lie past the end
7508 of a wrapped line. Then it->current is the
7509 character on the next line, so backtrack to the
7510 space before the wrap point. */
7511 if (skip == MOVE_LINE_CONTINUED
7512 && it->line_wrap == WORD_WRAP)
7513 {
7514 int prev_x = max (it->current_x - 1, 0);
7515 *it = it_backup;
7516 skip = move_it_in_display_line_to
7517 (it, -1, prev_x, MOVE_TO_X);
7518 }
7519 reached = 6;
7520 }
7521 }
7522
7523 if (reached)
7524 break;
7525 }
7526 else if (BUFFERP (it->object)
7527 && (it->method == GET_FROM_BUFFER
7528 || it->method == GET_FROM_STRETCH)
7529 && IT_CHARPOS (*it) >= to_charpos)
7530 skip = MOVE_POS_MATCH_OR_ZV;
7531 else
7532 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7533
7534 switch (skip)
7535 {
7536 case MOVE_POS_MATCH_OR_ZV:
7537 reached = 8;
7538 goto out;
7539
7540 case MOVE_NEWLINE_OR_CR:
7541 set_iterator_to_next (it, 1);
7542 it->continuation_lines_width = 0;
7543 break;
7544
7545 case MOVE_LINE_TRUNCATED:
7546 it->continuation_lines_width = 0;
7547 reseat_at_next_visible_line_start (it, 0);
7548 if ((op & MOVE_TO_POS) != 0
7549 && IT_CHARPOS (*it) > to_charpos)
7550 {
7551 reached = 9;
7552 goto out;
7553 }
7554 break;
7555
7556 case MOVE_LINE_CONTINUED:
7557 /* For continued lines ending in a tab, some of the glyphs
7558 associated with the tab are displayed on the current
7559 line. Since it->current_x does not include these glyphs,
7560 we use it->last_visible_x instead. */
7561 if (it->c == '\t')
7562 {
7563 it->continuation_lines_width += it->last_visible_x;
7564 /* When moving by vpos, ensure that the iterator really
7565 advances to the next line (bug#847, bug#969). Fixme:
7566 do we need to do this in other circumstances? */
7567 if (it->current_x != it->last_visible_x
7568 && (op & MOVE_TO_VPOS)
7569 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7570 {
7571 line_start_x = it->current_x + it->pixel_width
7572 - it->last_visible_x;
7573 set_iterator_to_next (it, 0);
7574 }
7575 }
7576 else
7577 it->continuation_lines_width += it->current_x;
7578 break;
7579
7580 default:
7581 abort ();
7582 }
7583
7584 /* Reset/increment for the next run. */
7585 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7586 it->current_x = line_start_x;
7587 line_start_x = 0;
7588 it->hpos = 0;
7589 it->current_y += it->max_ascent + it->max_descent;
7590 ++it->vpos;
7591 last_height = it->max_ascent + it->max_descent;
7592 last_max_ascent = it->max_ascent;
7593 it->max_ascent = it->max_descent = 0;
7594 }
7595
7596 out:
7597
7598 /* On text terminals, we may stop at the end of a line in the middle
7599 of a multi-character glyph. If the glyph itself is continued,
7600 i.e. it is actually displayed on the next line, don't treat this
7601 stopping point as valid; move to the next line instead (unless
7602 that brings us offscreen). */
7603 if (!FRAME_WINDOW_P (it->f)
7604 && op & MOVE_TO_POS
7605 && IT_CHARPOS (*it) == to_charpos
7606 && it->what == IT_CHARACTER
7607 && it->nglyphs > 1
7608 && it->line_wrap == WINDOW_WRAP
7609 && it->current_x == it->last_visible_x - 1
7610 && it->c != '\n'
7611 && it->c != '\t'
7612 && it->vpos < XFASTINT (it->w->window_end_vpos))
7613 {
7614 it->continuation_lines_width += it->current_x;
7615 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7616 it->current_y += it->max_ascent + it->max_descent;
7617 ++it->vpos;
7618 last_height = it->max_ascent + it->max_descent;
7619 last_max_ascent = it->max_ascent;
7620 }
7621
7622 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7623 }
7624
7625
7626 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7627
7628 If DY > 0, move IT backward at least that many pixels. DY = 0
7629 means move IT backward to the preceding line start or BEGV. This
7630 function may move over more than DY pixels if IT->current_y - DY
7631 ends up in the middle of a line; in this case IT->current_y will be
7632 set to the top of the line moved to. */
7633
7634 void
7635 move_it_vertically_backward (it, dy)
7636 struct it *it;
7637 int dy;
7638 {
7639 int nlines, h;
7640 struct it it2, it3;
7641 int start_pos;
7642
7643 move_further_back:
7644 xassert (dy >= 0);
7645
7646 start_pos = IT_CHARPOS (*it);
7647
7648 /* Estimate how many newlines we must move back. */
7649 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7650
7651 /* Set the iterator's position that many lines back. */
7652 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7653 back_to_previous_visible_line_start (it);
7654
7655 /* Reseat the iterator here. When moving backward, we don't want
7656 reseat to skip forward over invisible text, set up the iterator
7657 to deliver from overlay strings at the new position etc. So,
7658 use reseat_1 here. */
7659 reseat_1 (it, it->current.pos, 1);
7660
7661 /* We are now surely at a line start. */
7662 it->current_x = it->hpos = 0;
7663 it->continuation_lines_width = 0;
7664
7665 /* Move forward and see what y-distance we moved. First move to the
7666 start of the next line so that we get its height. We need this
7667 height to be able to tell whether we reached the specified
7668 y-distance. */
7669 it2 = *it;
7670 it2.max_ascent = it2.max_descent = 0;
7671 do
7672 {
7673 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7674 MOVE_TO_POS | MOVE_TO_VPOS);
7675 }
7676 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7677 xassert (IT_CHARPOS (*it) >= BEGV);
7678 it3 = it2;
7679
7680 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7681 xassert (IT_CHARPOS (*it) >= BEGV);
7682 /* H is the actual vertical distance from the position in *IT
7683 and the starting position. */
7684 h = it2.current_y - it->current_y;
7685 /* NLINES is the distance in number of lines. */
7686 nlines = it2.vpos - it->vpos;
7687
7688 /* Correct IT's y and vpos position
7689 so that they are relative to the starting point. */
7690 it->vpos -= nlines;
7691 it->current_y -= h;
7692
7693 if (dy == 0)
7694 {
7695 /* DY == 0 means move to the start of the screen line. The
7696 value of nlines is > 0 if continuation lines were involved. */
7697 if (nlines > 0)
7698 move_it_by_lines (it, nlines, 1);
7699 }
7700 else
7701 {
7702 /* The y-position we try to reach, relative to *IT.
7703 Note that H has been subtracted in front of the if-statement. */
7704 int target_y = it->current_y + h - dy;
7705 int y0 = it3.current_y;
7706 int y1 = line_bottom_y (&it3);
7707 int line_height = y1 - y0;
7708
7709 /* If we did not reach target_y, try to move further backward if
7710 we can. If we moved too far backward, try to move forward. */
7711 if (target_y < it->current_y
7712 /* This is heuristic. In a window that's 3 lines high, with
7713 a line height of 13 pixels each, recentering with point
7714 on the bottom line will try to move -39/2 = 19 pixels
7715 backward. Try to avoid moving into the first line. */
7716 && (it->current_y - target_y
7717 > min (window_box_height (it->w), line_height * 2 / 3))
7718 && IT_CHARPOS (*it) > BEGV)
7719 {
7720 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7721 target_y - it->current_y));
7722 dy = it->current_y - target_y;
7723 goto move_further_back;
7724 }
7725 else if (target_y >= it->current_y + line_height
7726 && IT_CHARPOS (*it) < ZV)
7727 {
7728 /* Should move forward by at least one line, maybe more.
7729
7730 Note: Calling move_it_by_lines can be expensive on
7731 terminal frames, where compute_motion is used (via
7732 vmotion) to do the job, when there are very long lines
7733 and truncate-lines is nil. That's the reason for
7734 treating terminal frames specially here. */
7735
7736 if (!FRAME_WINDOW_P (it->f))
7737 move_it_vertically (it, target_y - (it->current_y + line_height));
7738 else
7739 {
7740 do
7741 {
7742 move_it_by_lines (it, 1, 1);
7743 }
7744 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7745 }
7746 }
7747 }
7748 }
7749
7750
7751 /* Move IT by a specified amount of pixel lines DY. DY negative means
7752 move backwards. DY = 0 means move to start of screen line. At the
7753 end, IT will be on the start of a screen line. */
7754
7755 void
7756 move_it_vertically (it, dy)
7757 struct it *it;
7758 int dy;
7759 {
7760 if (dy <= 0)
7761 move_it_vertically_backward (it, -dy);
7762 else
7763 {
7764 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7765 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7766 MOVE_TO_POS | MOVE_TO_Y);
7767 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7768
7769 /* If buffer ends in ZV without a newline, move to the start of
7770 the line to satisfy the post-condition. */
7771 if (IT_CHARPOS (*it) == ZV
7772 && ZV > BEGV
7773 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7774 move_it_by_lines (it, 0, 0);
7775 }
7776 }
7777
7778
7779 /* Move iterator IT past the end of the text line it is in. */
7780
7781 void
7782 move_it_past_eol (it)
7783 struct it *it;
7784 {
7785 enum move_it_result rc;
7786
7787 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7788 if (rc == MOVE_NEWLINE_OR_CR)
7789 set_iterator_to_next (it, 0);
7790 }
7791
7792
7793 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7794 negative means move up. DVPOS == 0 means move to the start of the
7795 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7796 NEED_Y_P is zero, IT->current_y will be left unchanged.
7797
7798 Further optimization ideas: If we would know that IT->f doesn't use
7799 a face with proportional font, we could be faster for
7800 truncate-lines nil. */
7801
7802 void
7803 move_it_by_lines (it, dvpos, need_y_p)
7804 struct it *it;
7805 int dvpos, need_y_p;
7806 {
7807 struct position pos;
7808
7809 /* The commented-out optimization uses vmotion on terminals. This
7810 gives bad results, because elements like it->what, on which
7811 callers such as pos_visible_p rely, aren't updated. */
7812 /* if (!FRAME_WINDOW_P (it->f))
7813 {
7814 struct text_pos textpos;
7815
7816 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7817 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7818 reseat (it, textpos, 1);
7819 it->vpos += pos.vpos;
7820 it->current_y += pos.vpos;
7821 }
7822 else */
7823
7824 if (dvpos == 0)
7825 {
7826 /* DVPOS == 0 means move to the start of the screen line. */
7827 move_it_vertically_backward (it, 0);
7828 xassert (it->current_x == 0 && it->hpos == 0);
7829 /* Let next call to line_bottom_y calculate real line height */
7830 last_height = 0;
7831 }
7832 else if (dvpos > 0)
7833 {
7834 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7835 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7836 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7837 }
7838 else
7839 {
7840 struct it it2;
7841 int start_charpos, i;
7842
7843 /* Start at the beginning of the screen line containing IT's
7844 position. This may actually move vertically backwards,
7845 in case of overlays, so adjust dvpos accordingly. */
7846 dvpos += it->vpos;
7847 move_it_vertically_backward (it, 0);
7848 dvpos -= it->vpos;
7849
7850 /* Go back -DVPOS visible lines and reseat the iterator there. */
7851 start_charpos = IT_CHARPOS (*it);
7852 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7853 back_to_previous_visible_line_start (it);
7854 reseat (it, it->current.pos, 1);
7855
7856 /* Move further back if we end up in a string or an image. */
7857 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7858 {
7859 /* First try to move to start of display line. */
7860 dvpos += it->vpos;
7861 move_it_vertically_backward (it, 0);
7862 dvpos -= it->vpos;
7863 if (IT_POS_VALID_AFTER_MOVE_P (it))
7864 break;
7865 /* If start of line is still in string or image,
7866 move further back. */
7867 back_to_previous_visible_line_start (it);
7868 reseat (it, it->current.pos, 1);
7869 dvpos--;
7870 }
7871
7872 it->current_x = it->hpos = 0;
7873
7874 /* Above call may have moved too far if continuation lines
7875 are involved. Scan forward and see if it did. */
7876 it2 = *it;
7877 it2.vpos = it2.current_y = 0;
7878 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7879 it->vpos -= it2.vpos;
7880 it->current_y -= it2.current_y;
7881 it->current_x = it->hpos = 0;
7882
7883 /* If we moved too far back, move IT some lines forward. */
7884 if (it2.vpos > -dvpos)
7885 {
7886 int delta = it2.vpos + dvpos;
7887 it2 = *it;
7888 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7889 /* Move back again if we got too far ahead. */
7890 if (IT_CHARPOS (*it) >= start_charpos)
7891 *it = it2;
7892 }
7893 }
7894 }
7895
7896 /* Return 1 if IT points into the middle of a display vector. */
7897
7898 int
7899 in_display_vector_p (it)
7900 struct it *it;
7901 {
7902 return (it->method == GET_FROM_DISPLAY_VECTOR
7903 && it->current.dpvec_index > 0
7904 && it->dpvec + it->current.dpvec_index != it->dpend);
7905 }
7906
7907 \f
7908 /***********************************************************************
7909 Messages
7910 ***********************************************************************/
7911
7912
7913 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7914 to *Messages*. */
7915
7916 void
7917 add_to_log (format, arg1, arg2)
7918 char *format;
7919 Lisp_Object arg1, arg2;
7920 {
7921 Lisp_Object args[3];
7922 Lisp_Object msg, fmt;
7923 char *buffer;
7924 int len;
7925 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7926 USE_SAFE_ALLOCA;
7927
7928 /* Do nothing if called asynchronously. Inserting text into
7929 a buffer may call after-change-functions and alike and
7930 that would means running Lisp asynchronously. */
7931 if (handling_signal)
7932 return;
7933
7934 fmt = msg = Qnil;
7935 GCPRO4 (fmt, msg, arg1, arg2);
7936
7937 args[0] = fmt = build_string (format);
7938 args[1] = arg1;
7939 args[2] = arg2;
7940 msg = Fformat (3, args);
7941
7942 len = SBYTES (msg) + 1;
7943 SAFE_ALLOCA (buffer, char *, len);
7944 bcopy (SDATA (msg), buffer, len);
7945
7946 message_dolog (buffer, len - 1, 1, 0);
7947 SAFE_FREE ();
7948
7949 UNGCPRO;
7950 }
7951
7952
7953 /* Output a newline in the *Messages* buffer if "needs" one. */
7954
7955 void
7956 message_log_maybe_newline ()
7957 {
7958 if (message_log_need_newline)
7959 message_dolog ("", 0, 1, 0);
7960 }
7961
7962
7963 /* Add a string M of length NBYTES to the message log, optionally
7964 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7965 nonzero, means interpret the contents of M as multibyte. This
7966 function calls low-level routines in order to bypass text property
7967 hooks, etc. which might not be safe to run.
7968
7969 This may GC (insert may run before/after change hooks),
7970 so the buffer M must NOT point to a Lisp string. */
7971
7972 void
7973 message_dolog (m, nbytes, nlflag, multibyte)
7974 const char *m;
7975 int nbytes, nlflag, multibyte;
7976 {
7977 if (!NILP (Vmemory_full))
7978 return;
7979
7980 if (!NILP (Vmessage_log_max))
7981 {
7982 struct buffer *oldbuf;
7983 Lisp_Object oldpoint, oldbegv, oldzv;
7984 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7985 int point_at_end = 0;
7986 int zv_at_end = 0;
7987 Lisp_Object old_deactivate_mark, tem;
7988 struct gcpro gcpro1;
7989
7990 old_deactivate_mark = Vdeactivate_mark;
7991 oldbuf = current_buffer;
7992 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7993 current_buffer->undo_list = Qt;
7994
7995 oldpoint = message_dolog_marker1;
7996 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7997 oldbegv = message_dolog_marker2;
7998 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7999 oldzv = message_dolog_marker3;
8000 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8001 GCPRO1 (old_deactivate_mark);
8002
8003 if (PT == Z)
8004 point_at_end = 1;
8005 if (ZV == Z)
8006 zv_at_end = 1;
8007
8008 BEGV = BEG;
8009 BEGV_BYTE = BEG_BYTE;
8010 ZV = Z;
8011 ZV_BYTE = Z_BYTE;
8012 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8013
8014 /* Insert the string--maybe converting multibyte to single byte
8015 or vice versa, so that all the text fits the buffer. */
8016 if (multibyte
8017 && NILP (current_buffer->enable_multibyte_characters))
8018 {
8019 int i, c, char_bytes;
8020 unsigned char work[1];
8021
8022 /* Convert a multibyte string to single-byte
8023 for the *Message* buffer. */
8024 for (i = 0; i < nbytes; i += char_bytes)
8025 {
8026 c = string_char_and_length (m + i, &char_bytes);
8027 work[0] = (ASCII_CHAR_P (c)
8028 ? c
8029 : multibyte_char_to_unibyte (c, Qnil));
8030 insert_1_both (work, 1, 1, 1, 0, 0);
8031 }
8032 }
8033 else if (! multibyte
8034 && ! NILP (current_buffer->enable_multibyte_characters))
8035 {
8036 int i, c, char_bytes;
8037 unsigned char *msg = (unsigned char *) m;
8038 unsigned char str[MAX_MULTIBYTE_LENGTH];
8039 /* Convert a single-byte string to multibyte
8040 for the *Message* buffer. */
8041 for (i = 0; i < nbytes; i++)
8042 {
8043 c = msg[i];
8044 MAKE_CHAR_MULTIBYTE (c);
8045 char_bytes = CHAR_STRING (c, str);
8046 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8047 }
8048 }
8049 else if (nbytes)
8050 insert_1 (m, nbytes, 1, 0, 0);
8051
8052 if (nlflag)
8053 {
8054 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
8055 insert_1 ("\n", 1, 1, 0, 0);
8056
8057 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8058 this_bol = PT;
8059 this_bol_byte = PT_BYTE;
8060
8061 /* See if this line duplicates the previous one.
8062 If so, combine duplicates. */
8063 if (this_bol > BEG)
8064 {
8065 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8066 prev_bol = PT;
8067 prev_bol_byte = PT_BYTE;
8068
8069 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8070 this_bol, this_bol_byte);
8071 if (dup)
8072 {
8073 del_range_both (prev_bol, prev_bol_byte,
8074 this_bol, this_bol_byte, 0);
8075 if (dup > 1)
8076 {
8077 char dupstr[40];
8078 int duplen;
8079
8080 /* If you change this format, don't forget to also
8081 change message_log_check_duplicate. */
8082 sprintf (dupstr, " [%d times]", dup);
8083 duplen = strlen (dupstr);
8084 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8085 insert_1 (dupstr, duplen, 1, 0, 1);
8086 }
8087 }
8088 }
8089
8090 /* If we have more than the desired maximum number of lines
8091 in the *Messages* buffer now, delete the oldest ones.
8092 This is safe because we don't have undo in this buffer. */
8093
8094 if (NATNUMP (Vmessage_log_max))
8095 {
8096 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8097 -XFASTINT (Vmessage_log_max) - 1, 0);
8098 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8099 }
8100 }
8101 BEGV = XMARKER (oldbegv)->charpos;
8102 BEGV_BYTE = marker_byte_position (oldbegv);
8103
8104 if (zv_at_end)
8105 {
8106 ZV = Z;
8107 ZV_BYTE = Z_BYTE;
8108 }
8109 else
8110 {
8111 ZV = XMARKER (oldzv)->charpos;
8112 ZV_BYTE = marker_byte_position (oldzv);
8113 }
8114
8115 if (point_at_end)
8116 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8117 else
8118 /* We can't do Fgoto_char (oldpoint) because it will run some
8119 Lisp code. */
8120 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8121 XMARKER (oldpoint)->bytepos);
8122
8123 UNGCPRO;
8124 unchain_marker (XMARKER (oldpoint));
8125 unchain_marker (XMARKER (oldbegv));
8126 unchain_marker (XMARKER (oldzv));
8127
8128 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8129 set_buffer_internal (oldbuf);
8130 if (NILP (tem))
8131 windows_or_buffers_changed = old_windows_or_buffers_changed;
8132 message_log_need_newline = !nlflag;
8133 Vdeactivate_mark = old_deactivate_mark;
8134 }
8135 }
8136
8137
8138 /* We are at the end of the buffer after just having inserted a newline.
8139 (Note: We depend on the fact we won't be crossing the gap.)
8140 Check to see if the most recent message looks a lot like the previous one.
8141 Return 0 if different, 1 if the new one should just replace it, or a
8142 value N > 1 if we should also append " [N times]". */
8143
8144 static int
8145 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
8146 int prev_bol, this_bol;
8147 int prev_bol_byte, this_bol_byte;
8148 {
8149 int i;
8150 int len = Z_BYTE - 1 - this_bol_byte;
8151 int seen_dots = 0;
8152 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8153 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8154
8155 for (i = 0; i < len; i++)
8156 {
8157 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8158 seen_dots = 1;
8159 if (p1[i] != p2[i])
8160 return seen_dots;
8161 }
8162 p1 += len;
8163 if (*p1 == '\n')
8164 return 2;
8165 if (*p1++ == ' ' && *p1++ == '[')
8166 {
8167 int n = 0;
8168 while (*p1 >= '0' && *p1 <= '9')
8169 n = n * 10 + *p1++ - '0';
8170 if (strncmp (p1, " times]\n", 8) == 0)
8171 return n+1;
8172 }
8173 return 0;
8174 }
8175 \f
8176
8177 /* Display an echo area message M with a specified length of NBYTES
8178 bytes. The string may include null characters. If M is 0, clear
8179 out any existing message, and let the mini-buffer text show
8180 through.
8181
8182 This may GC, so the buffer M must NOT point to a Lisp string. */
8183
8184 void
8185 message2 (m, nbytes, multibyte)
8186 const char *m;
8187 int nbytes;
8188 int multibyte;
8189 {
8190 /* First flush out any partial line written with print. */
8191 message_log_maybe_newline ();
8192 if (m)
8193 message_dolog (m, nbytes, 1, multibyte);
8194 message2_nolog (m, nbytes, multibyte);
8195 }
8196
8197
8198 /* The non-logging counterpart of message2. */
8199
8200 void
8201 message2_nolog (m, nbytes, multibyte)
8202 const char *m;
8203 int nbytes, multibyte;
8204 {
8205 struct frame *sf = SELECTED_FRAME ();
8206 message_enable_multibyte = multibyte;
8207
8208 if (FRAME_INITIAL_P (sf))
8209 {
8210 if (noninteractive_need_newline)
8211 putc ('\n', stderr);
8212 noninteractive_need_newline = 0;
8213 if (m)
8214 fwrite (m, nbytes, 1, stderr);
8215 if (cursor_in_echo_area == 0)
8216 fprintf (stderr, "\n");
8217 fflush (stderr);
8218 }
8219 /* A null message buffer means that the frame hasn't really been
8220 initialized yet. Error messages get reported properly by
8221 cmd_error, so this must be just an informative message; toss it. */
8222 else if (INTERACTIVE
8223 && sf->glyphs_initialized_p
8224 && FRAME_MESSAGE_BUF (sf))
8225 {
8226 Lisp_Object mini_window;
8227 struct frame *f;
8228
8229 /* Get the frame containing the mini-buffer
8230 that the selected frame is using. */
8231 mini_window = FRAME_MINIBUF_WINDOW (sf);
8232 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8233
8234 FRAME_SAMPLE_VISIBILITY (f);
8235 if (FRAME_VISIBLE_P (sf)
8236 && ! FRAME_VISIBLE_P (f))
8237 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8238
8239 if (m)
8240 {
8241 set_message (m, Qnil, nbytes, multibyte);
8242 if (minibuffer_auto_raise)
8243 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8244 }
8245 else
8246 clear_message (1, 1);
8247
8248 do_pending_window_change (0);
8249 echo_area_display (1);
8250 do_pending_window_change (0);
8251 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8252 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8253 }
8254 }
8255
8256
8257 /* Display an echo area message M with a specified length of NBYTES
8258 bytes. The string may include null characters. If M is not a
8259 string, clear out any existing message, and let the mini-buffer
8260 text show through.
8261
8262 This function cancels echoing. */
8263
8264 void
8265 message3 (m, nbytes, multibyte)
8266 Lisp_Object m;
8267 int nbytes;
8268 int multibyte;
8269 {
8270 struct gcpro gcpro1;
8271
8272 GCPRO1 (m);
8273 clear_message (1,1);
8274 cancel_echoing ();
8275
8276 /* First flush out any partial line written with print. */
8277 message_log_maybe_newline ();
8278 if (STRINGP (m))
8279 {
8280 char *buffer;
8281 USE_SAFE_ALLOCA;
8282
8283 SAFE_ALLOCA (buffer, char *, nbytes);
8284 bcopy (SDATA (m), buffer, nbytes);
8285 message_dolog (buffer, nbytes, 1, multibyte);
8286 SAFE_FREE ();
8287 }
8288 message3_nolog (m, nbytes, multibyte);
8289
8290 UNGCPRO;
8291 }
8292
8293
8294 /* The non-logging version of message3.
8295 This does not cancel echoing, because it is used for echoing.
8296 Perhaps we need to make a separate function for echoing
8297 and make this cancel echoing. */
8298
8299 void
8300 message3_nolog (m, nbytes, multibyte)
8301 Lisp_Object m;
8302 int nbytes, multibyte;
8303 {
8304 struct frame *sf = SELECTED_FRAME ();
8305 message_enable_multibyte = multibyte;
8306
8307 if (FRAME_INITIAL_P (sf))
8308 {
8309 if (noninteractive_need_newline)
8310 putc ('\n', stderr);
8311 noninteractive_need_newline = 0;
8312 if (STRINGP (m))
8313 fwrite (SDATA (m), nbytes, 1, stderr);
8314 if (cursor_in_echo_area == 0)
8315 fprintf (stderr, "\n");
8316 fflush (stderr);
8317 }
8318 /* A null message buffer means that the frame hasn't really been
8319 initialized yet. Error messages get reported properly by
8320 cmd_error, so this must be just an informative message; toss it. */
8321 else if (INTERACTIVE
8322 && sf->glyphs_initialized_p
8323 && FRAME_MESSAGE_BUF (sf))
8324 {
8325 Lisp_Object mini_window;
8326 Lisp_Object frame;
8327 struct frame *f;
8328
8329 /* Get the frame containing the mini-buffer
8330 that the selected frame is using. */
8331 mini_window = FRAME_MINIBUF_WINDOW (sf);
8332 frame = XWINDOW (mini_window)->frame;
8333 f = XFRAME (frame);
8334
8335 FRAME_SAMPLE_VISIBILITY (f);
8336 if (FRAME_VISIBLE_P (sf)
8337 && !FRAME_VISIBLE_P (f))
8338 Fmake_frame_visible (frame);
8339
8340 if (STRINGP (m) && SCHARS (m) > 0)
8341 {
8342 set_message (NULL, m, nbytes, multibyte);
8343 if (minibuffer_auto_raise)
8344 Fraise_frame (frame);
8345 /* Assume we are not echoing.
8346 (If we are, echo_now will override this.) */
8347 echo_message_buffer = Qnil;
8348 }
8349 else
8350 clear_message (1, 1);
8351
8352 do_pending_window_change (0);
8353 echo_area_display (1);
8354 do_pending_window_change (0);
8355 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8356 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8357 }
8358 }
8359
8360
8361 /* Display a null-terminated echo area message M. If M is 0, clear
8362 out any existing message, and let the mini-buffer text show through.
8363
8364 The buffer M must continue to exist until after the echo area gets
8365 cleared or some other message gets displayed there. Do not pass
8366 text that is stored in a Lisp string. Do not pass text in a buffer
8367 that was alloca'd. */
8368
8369 void
8370 message1 (m)
8371 char *m;
8372 {
8373 message2 (m, (m ? strlen (m) : 0), 0);
8374 }
8375
8376
8377 /* The non-logging counterpart of message1. */
8378
8379 void
8380 message1_nolog (m)
8381 char *m;
8382 {
8383 message2_nolog (m, (m ? strlen (m) : 0), 0);
8384 }
8385
8386 /* Display a message M which contains a single %s
8387 which gets replaced with STRING. */
8388
8389 void
8390 message_with_string (m, string, log)
8391 char *m;
8392 Lisp_Object string;
8393 int log;
8394 {
8395 CHECK_STRING (string);
8396
8397 if (noninteractive)
8398 {
8399 if (m)
8400 {
8401 if (noninteractive_need_newline)
8402 putc ('\n', stderr);
8403 noninteractive_need_newline = 0;
8404 fprintf (stderr, m, SDATA (string));
8405 if (!cursor_in_echo_area)
8406 fprintf (stderr, "\n");
8407 fflush (stderr);
8408 }
8409 }
8410 else if (INTERACTIVE)
8411 {
8412 /* The frame whose minibuffer we're going to display the message on.
8413 It may be larger than the selected frame, so we need
8414 to use its buffer, not the selected frame's buffer. */
8415 Lisp_Object mini_window;
8416 struct frame *f, *sf = SELECTED_FRAME ();
8417
8418 /* Get the frame containing the minibuffer
8419 that the selected frame is using. */
8420 mini_window = FRAME_MINIBUF_WINDOW (sf);
8421 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8422
8423 /* A null message buffer means that the frame hasn't really been
8424 initialized yet. Error messages get reported properly by
8425 cmd_error, so this must be just an informative message; toss it. */
8426 if (FRAME_MESSAGE_BUF (f))
8427 {
8428 Lisp_Object args[2], message;
8429 struct gcpro gcpro1, gcpro2;
8430
8431 args[0] = build_string (m);
8432 args[1] = message = string;
8433 GCPRO2 (args[0], message);
8434 gcpro1.nvars = 2;
8435
8436 message = Fformat (2, args);
8437
8438 if (log)
8439 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8440 else
8441 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8442
8443 UNGCPRO;
8444
8445 /* Print should start at the beginning of the message
8446 buffer next time. */
8447 message_buf_print = 0;
8448 }
8449 }
8450 }
8451
8452
8453 /* Dump an informative message to the minibuf. If M is 0, clear out
8454 any existing message, and let the mini-buffer text show through. */
8455
8456 /* VARARGS 1 */
8457 void
8458 message (m, a1, a2, a3)
8459 char *m;
8460 EMACS_INT a1, a2, a3;
8461 {
8462 if (noninteractive)
8463 {
8464 if (m)
8465 {
8466 if (noninteractive_need_newline)
8467 putc ('\n', stderr);
8468 noninteractive_need_newline = 0;
8469 fprintf (stderr, m, a1, a2, a3);
8470 if (cursor_in_echo_area == 0)
8471 fprintf (stderr, "\n");
8472 fflush (stderr);
8473 }
8474 }
8475 else if (INTERACTIVE)
8476 {
8477 /* The frame whose mini-buffer we're going to display the message
8478 on. It may be larger than the selected frame, so we need to
8479 use its buffer, not the selected frame's buffer. */
8480 Lisp_Object mini_window;
8481 struct frame *f, *sf = SELECTED_FRAME ();
8482
8483 /* Get the frame containing the mini-buffer
8484 that the selected frame is using. */
8485 mini_window = FRAME_MINIBUF_WINDOW (sf);
8486 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8487
8488 /* A null message buffer means that the frame hasn't really been
8489 initialized yet. Error messages get reported properly by
8490 cmd_error, so this must be just an informative message; toss
8491 it. */
8492 if (FRAME_MESSAGE_BUF (f))
8493 {
8494 if (m)
8495 {
8496 int len;
8497 #ifdef NO_ARG_ARRAY
8498 char *a[3];
8499 a[0] = (char *) a1;
8500 a[1] = (char *) a2;
8501 a[2] = (char *) a3;
8502
8503 len = doprnt (FRAME_MESSAGE_BUF (f),
8504 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8505 #else
8506 len = doprnt (FRAME_MESSAGE_BUF (f),
8507 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8508 (char **) &a1);
8509 #endif /* NO_ARG_ARRAY */
8510
8511 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8512 }
8513 else
8514 message1 (0);
8515
8516 /* Print should start at the beginning of the message
8517 buffer next time. */
8518 message_buf_print = 0;
8519 }
8520 }
8521 }
8522
8523
8524 /* The non-logging version of message. */
8525
8526 void
8527 message_nolog (m, a1, a2, a3)
8528 char *m;
8529 EMACS_INT a1, a2, a3;
8530 {
8531 Lisp_Object old_log_max;
8532 old_log_max = Vmessage_log_max;
8533 Vmessage_log_max = Qnil;
8534 message (m, a1, a2, a3);
8535 Vmessage_log_max = old_log_max;
8536 }
8537
8538
8539 /* Display the current message in the current mini-buffer. This is
8540 only called from error handlers in process.c, and is not time
8541 critical. */
8542
8543 void
8544 update_echo_area ()
8545 {
8546 if (!NILP (echo_area_buffer[0]))
8547 {
8548 Lisp_Object string;
8549 string = Fcurrent_message ();
8550 message3 (string, SBYTES (string),
8551 !NILP (current_buffer->enable_multibyte_characters));
8552 }
8553 }
8554
8555
8556 /* Make sure echo area buffers in `echo_buffers' are live.
8557 If they aren't, make new ones. */
8558
8559 static void
8560 ensure_echo_area_buffers ()
8561 {
8562 int i;
8563
8564 for (i = 0; i < 2; ++i)
8565 if (!BUFFERP (echo_buffer[i])
8566 || NILP (XBUFFER (echo_buffer[i])->name))
8567 {
8568 char name[30];
8569 Lisp_Object old_buffer;
8570 int j;
8571
8572 old_buffer = echo_buffer[i];
8573 sprintf (name, " *Echo Area %d*", i);
8574 echo_buffer[i] = Fget_buffer_create (build_string (name));
8575 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8576 /* to force word wrap in echo area -
8577 it was decided to postpone this*/
8578 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8579
8580 for (j = 0; j < 2; ++j)
8581 if (EQ (old_buffer, echo_area_buffer[j]))
8582 echo_area_buffer[j] = echo_buffer[i];
8583 }
8584 }
8585
8586
8587 /* Call FN with args A1..A4 with either the current or last displayed
8588 echo_area_buffer as current buffer.
8589
8590 WHICH zero means use the current message buffer
8591 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8592 from echo_buffer[] and clear it.
8593
8594 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8595 suitable buffer from echo_buffer[] and clear it.
8596
8597 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8598 that the current message becomes the last displayed one, make
8599 choose a suitable buffer for echo_area_buffer[0], and clear it.
8600
8601 Value is what FN returns. */
8602
8603 static int
8604 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8605 struct window *w;
8606 int which;
8607 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8608 EMACS_INT a1;
8609 Lisp_Object a2;
8610 EMACS_INT a3, a4;
8611 {
8612 Lisp_Object buffer;
8613 int this_one, the_other, clear_buffer_p, rc;
8614 int count = SPECPDL_INDEX ();
8615
8616 /* If buffers aren't live, make new ones. */
8617 ensure_echo_area_buffers ();
8618
8619 clear_buffer_p = 0;
8620
8621 if (which == 0)
8622 this_one = 0, the_other = 1;
8623 else if (which > 0)
8624 this_one = 1, the_other = 0;
8625 else
8626 {
8627 this_one = 0, the_other = 1;
8628 clear_buffer_p = 1;
8629
8630 /* We need a fresh one in case the current echo buffer equals
8631 the one containing the last displayed echo area message. */
8632 if (!NILP (echo_area_buffer[this_one])
8633 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8634 echo_area_buffer[this_one] = Qnil;
8635 }
8636
8637 /* Choose a suitable buffer from echo_buffer[] is we don't
8638 have one. */
8639 if (NILP (echo_area_buffer[this_one]))
8640 {
8641 echo_area_buffer[this_one]
8642 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8643 ? echo_buffer[the_other]
8644 : echo_buffer[this_one]);
8645 clear_buffer_p = 1;
8646 }
8647
8648 buffer = echo_area_buffer[this_one];
8649
8650 /* Don't get confused by reusing the buffer used for echoing
8651 for a different purpose. */
8652 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8653 cancel_echoing ();
8654
8655 record_unwind_protect (unwind_with_echo_area_buffer,
8656 with_echo_area_buffer_unwind_data (w));
8657
8658 /* Make the echo area buffer current. Note that for display
8659 purposes, it is not necessary that the displayed window's buffer
8660 == current_buffer, except for text property lookup. So, let's
8661 only set that buffer temporarily here without doing a full
8662 Fset_window_buffer. We must also change w->pointm, though,
8663 because otherwise an assertions in unshow_buffer fails, and Emacs
8664 aborts. */
8665 set_buffer_internal_1 (XBUFFER (buffer));
8666 if (w)
8667 {
8668 w->buffer = buffer;
8669 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8670 }
8671
8672 current_buffer->undo_list = Qt;
8673 current_buffer->read_only = Qnil;
8674 specbind (Qinhibit_read_only, Qt);
8675 specbind (Qinhibit_modification_hooks, Qt);
8676
8677 if (clear_buffer_p && Z > BEG)
8678 del_range (BEG, Z);
8679
8680 xassert (BEGV >= BEG);
8681 xassert (ZV <= Z && ZV >= BEGV);
8682
8683 rc = fn (a1, a2, a3, a4);
8684
8685 xassert (BEGV >= BEG);
8686 xassert (ZV <= Z && ZV >= BEGV);
8687
8688 unbind_to (count, Qnil);
8689 return rc;
8690 }
8691
8692
8693 /* Save state that should be preserved around the call to the function
8694 FN called in with_echo_area_buffer. */
8695
8696 static Lisp_Object
8697 with_echo_area_buffer_unwind_data (w)
8698 struct window *w;
8699 {
8700 int i = 0;
8701 Lisp_Object vector, tmp;
8702
8703 /* Reduce consing by keeping one vector in
8704 Vwith_echo_area_save_vector. */
8705 vector = Vwith_echo_area_save_vector;
8706 Vwith_echo_area_save_vector = Qnil;
8707
8708 if (NILP (vector))
8709 vector = Fmake_vector (make_number (7), Qnil);
8710
8711 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8712 ASET (vector, i, Vdeactivate_mark); ++i;
8713 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8714
8715 if (w)
8716 {
8717 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8718 ASET (vector, i, w->buffer); ++i;
8719 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8720 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8721 }
8722 else
8723 {
8724 int end = i + 4;
8725 for (; i < end; ++i)
8726 ASET (vector, i, Qnil);
8727 }
8728
8729 xassert (i == ASIZE (vector));
8730 return vector;
8731 }
8732
8733
8734 /* Restore global state from VECTOR which was created by
8735 with_echo_area_buffer_unwind_data. */
8736
8737 static Lisp_Object
8738 unwind_with_echo_area_buffer (vector)
8739 Lisp_Object vector;
8740 {
8741 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8742 Vdeactivate_mark = AREF (vector, 1);
8743 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8744
8745 if (WINDOWP (AREF (vector, 3)))
8746 {
8747 struct window *w;
8748 Lisp_Object buffer, charpos, bytepos;
8749
8750 w = XWINDOW (AREF (vector, 3));
8751 buffer = AREF (vector, 4);
8752 charpos = AREF (vector, 5);
8753 bytepos = AREF (vector, 6);
8754
8755 w->buffer = buffer;
8756 set_marker_both (w->pointm, buffer,
8757 XFASTINT (charpos), XFASTINT (bytepos));
8758 }
8759
8760 Vwith_echo_area_save_vector = vector;
8761 return Qnil;
8762 }
8763
8764
8765 /* Set up the echo area for use by print functions. MULTIBYTE_P
8766 non-zero means we will print multibyte. */
8767
8768 void
8769 setup_echo_area_for_printing (multibyte_p)
8770 int multibyte_p;
8771 {
8772 /* If we can't find an echo area any more, exit. */
8773 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8774 Fkill_emacs (Qnil);
8775
8776 ensure_echo_area_buffers ();
8777
8778 if (!message_buf_print)
8779 {
8780 /* A message has been output since the last time we printed.
8781 Choose a fresh echo area buffer. */
8782 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8783 echo_area_buffer[0] = echo_buffer[1];
8784 else
8785 echo_area_buffer[0] = echo_buffer[0];
8786
8787 /* Switch to that buffer and clear it. */
8788 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8789 current_buffer->truncate_lines = Qnil;
8790
8791 if (Z > BEG)
8792 {
8793 int count = SPECPDL_INDEX ();
8794 specbind (Qinhibit_read_only, Qt);
8795 /* Note that undo recording is always disabled. */
8796 del_range (BEG, Z);
8797 unbind_to (count, Qnil);
8798 }
8799 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8800
8801 /* Set up the buffer for the multibyteness we need. */
8802 if (multibyte_p
8803 != !NILP (current_buffer->enable_multibyte_characters))
8804 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8805
8806 /* Raise the frame containing the echo area. */
8807 if (minibuffer_auto_raise)
8808 {
8809 struct frame *sf = SELECTED_FRAME ();
8810 Lisp_Object mini_window;
8811 mini_window = FRAME_MINIBUF_WINDOW (sf);
8812 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8813 }
8814
8815 message_log_maybe_newline ();
8816 message_buf_print = 1;
8817 }
8818 else
8819 {
8820 if (NILP (echo_area_buffer[0]))
8821 {
8822 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8823 echo_area_buffer[0] = echo_buffer[1];
8824 else
8825 echo_area_buffer[0] = echo_buffer[0];
8826 }
8827
8828 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8829 {
8830 /* Someone switched buffers between print requests. */
8831 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8832 current_buffer->truncate_lines = Qnil;
8833 }
8834 }
8835 }
8836
8837
8838 /* Display an echo area message in window W. Value is non-zero if W's
8839 height is changed. If display_last_displayed_message_p is
8840 non-zero, display the message that was last displayed, otherwise
8841 display the current message. */
8842
8843 static int
8844 display_echo_area (w)
8845 struct window *w;
8846 {
8847 int i, no_message_p, window_height_changed_p, count;
8848
8849 /* Temporarily disable garbage collections while displaying the echo
8850 area. This is done because a GC can print a message itself.
8851 That message would modify the echo area buffer's contents while a
8852 redisplay of the buffer is going on, and seriously confuse
8853 redisplay. */
8854 count = inhibit_garbage_collection ();
8855
8856 /* If there is no message, we must call display_echo_area_1
8857 nevertheless because it resizes the window. But we will have to
8858 reset the echo_area_buffer in question to nil at the end because
8859 with_echo_area_buffer will sets it to an empty buffer. */
8860 i = display_last_displayed_message_p ? 1 : 0;
8861 no_message_p = NILP (echo_area_buffer[i]);
8862
8863 window_height_changed_p
8864 = with_echo_area_buffer (w, display_last_displayed_message_p,
8865 display_echo_area_1,
8866 (EMACS_INT) w, Qnil, 0, 0);
8867
8868 if (no_message_p)
8869 echo_area_buffer[i] = Qnil;
8870
8871 unbind_to (count, Qnil);
8872 return window_height_changed_p;
8873 }
8874
8875
8876 /* Helper for display_echo_area. Display the current buffer which
8877 contains the current echo area message in window W, a mini-window,
8878 a pointer to which is passed in A1. A2..A4 are currently not used.
8879 Change the height of W so that all of the message is displayed.
8880 Value is non-zero if height of W was changed. */
8881
8882 static int
8883 display_echo_area_1 (a1, a2, a3, a4)
8884 EMACS_INT a1;
8885 Lisp_Object a2;
8886 EMACS_INT a3, a4;
8887 {
8888 struct window *w = (struct window *) a1;
8889 Lisp_Object window;
8890 struct text_pos start;
8891 int window_height_changed_p = 0;
8892
8893 /* Do this before displaying, so that we have a large enough glyph
8894 matrix for the display. If we can't get enough space for the
8895 whole text, display the last N lines. That works by setting w->start. */
8896 window_height_changed_p = resize_mini_window (w, 0);
8897
8898 /* Use the starting position chosen by resize_mini_window. */
8899 SET_TEXT_POS_FROM_MARKER (start, w->start);
8900
8901 /* Display. */
8902 clear_glyph_matrix (w->desired_matrix);
8903 XSETWINDOW (window, w);
8904 try_window (window, start, 0);
8905
8906 return window_height_changed_p;
8907 }
8908
8909
8910 /* Resize the echo area window to exactly the size needed for the
8911 currently displayed message, if there is one. If a mini-buffer
8912 is active, don't shrink it. */
8913
8914 void
8915 resize_echo_area_exactly ()
8916 {
8917 if (BUFFERP (echo_area_buffer[0])
8918 && WINDOWP (echo_area_window))
8919 {
8920 struct window *w = XWINDOW (echo_area_window);
8921 int resized_p;
8922 Lisp_Object resize_exactly;
8923
8924 if (minibuf_level == 0)
8925 resize_exactly = Qt;
8926 else
8927 resize_exactly = Qnil;
8928
8929 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8930 (EMACS_INT) w, resize_exactly, 0, 0);
8931 if (resized_p)
8932 {
8933 ++windows_or_buffers_changed;
8934 ++update_mode_lines;
8935 redisplay_internal (0);
8936 }
8937 }
8938 }
8939
8940
8941 /* Callback function for with_echo_area_buffer, when used from
8942 resize_echo_area_exactly. A1 contains a pointer to the window to
8943 resize, EXACTLY non-nil means resize the mini-window exactly to the
8944 size of the text displayed. A3 and A4 are not used. Value is what
8945 resize_mini_window returns. */
8946
8947 static int
8948 resize_mini_window_1 (a1, exactly, a3, a4)
8949 EMACS_INT a1;
8950 Lisp_Object exactly;
8951 EMACS_INT a3, a4;
8952 {
8953 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8954 }
8955
8956
8957 /* Resize mini-window W to fit the size of its contents. EXACT_P
8958 means size the window exactly to the size needed. Otherwise, it's
8959 only enlarged until W's buffer is empty.
8960
8961 Set W->start to the right place to begin display. If the whole
8962 contents fit, start at the beginning. Otherwise, start so as
8963 to make the end of the contents appear. This is particularly
8964 important for y-or-n-p, but seems desirable generally.
8965
8966 Value is non-zero if the window height has been changed. */
8967
8968 int
8969 resize_mini_window (w, exact_p)
8970 struct window *w;
8971 int exact_p;
8972 {
8973 struct frame *f = XFRAME (w->frame);
8974 int window_height_changed_p = 0;
8975
8976 xassert (MINI_WINDOW_P (w));
8977
8978 /* By default, start display at the beginning. */
8979 set_marker_both (w->start, w->buffer,
8980 BUF_BEGV (XBUFFER (w->buffer)),
8981 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8982
8983 /* Don't resize windows while redisplaying a window; it would
8984 confuse redisplay functions when the size of the window they are
8985 displaying changes from under them. Such a resizing can happen,
8986 for instance, when which-func prints a long message while
8987 we are running fontification-functions. We're running these
8988 functions with safe_call which binds inhibit-redisplay to t. */
8989 if (!NILP (Vinhibit_redisplay))
8990 return 0;
8991
8992 /* Nil means don't try to resize. */
8993 if (NILP (Vresize_mini_windows)
8994 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8995 return 0;
8996
8997 if (!FRAME_MINIBUF_ONLY_P (f))
8998 {
8999 struct it it;
9000 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9001 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9002 int height, max_height;
9003 int unit = FRAME_LINE_HEIGHT (f);
9004 struct text_pos start;
9005 struct buffer *old_current_buffer = NULL;
9006
9007 if (current_buffer != XBUFFER (w->buffer))
9008 {
9009 old_current_buffer = current_buffer;
9010 set_buffer_internal (XBUFFER (w->buffer));
9011 }
9012
9013 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9014
9015 /* Compute the max. number of lines specified by the user. */
9016 if (FLOATP (Vmax_mini_window_height))
9017 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9018 else if (INTEGERP (Vmax_mini_window_height))
9019 max_height = XINT (Vmax_mini_window_height);
9020 else
9021 max_height = total_height / 4;
9022
9023 /* Correct that max. height if it's bogus. */
9024 max_height = max (1, max_height);
9025 max_height = min (total_height, max_height);
9026
9027 /* Find out the height of the text in the window. */
9028 if (it.line_wrap == TRUNCATE)
9029 height = 1;
9030 else
9031 {
9032 last_height = 0;
9033 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9034 if (it.max_ascent == 0 && it.max_descent == 0)
9035 height = it.current_y + last_height;
9036 else
9037 height = it.current_y + it.max_ascent + it.max_descent;
9038 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9039 height = (height + unit - 1) / unit;
9040 }
9041
9042 /* Compute a suitable window start. */
9043 if (height > max_height)
9044 {
9045 height = max_height;
9046 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9047 move_it_vertically_backward (&it, (height - 1) * unit);
9048 start = it.current.pos;
9049 }
9050 else
9051 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9052 SET_MARKER_FROM_TEXT_POS (w->start, start);
9053
9054 if (EQ (Vresize_mini_windows, Qgrow_only))
9055 {
9056 /* Let it grow only, until we display an empty message, in which
9057 case the window shrinks again. */
9058 if (height > WINDOW_TOTAL_LINES (w))
9059 {
9060 int old_height = WINDOW_TOTAL_LINES (w);
9061 freeze_window_starts (f, 1);
9062 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9063 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9064 }
9065 else if (height < WINDOW_TOTAL_LINES (w)
9066 && (exact_p || BEGV == ZV))
9067 {
9068 int old_height = WINDOW_TOTAL_LINES (w);
9069 freeze_window_starts (f, 0);
9070 shrink_mini_window (w);
9071 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9072 }
9073 }
9074 else
9075 {
9076 /* Always resize to exact size needed. */
9077 if (height > WINDOW_TOTAL_LINES (w))
9078 {
9079 int old_height = WINDOW_TOTAL_LINES (w);
9080 freeze_window_starts (f, 1);
9081 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9082 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9083 }
9084 else if (height < WINDOW_TOTAL_LINES (w))
9085 {
9086 int old_height = WINDOW_TOTAL_LINES (w);
9087 freeze_window_starts (f, 0);
9088 shrink_mini_window (w);
9089
9090 if (height)
9091 {
9092 freeze_window_starts (f, 1);
9093 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9094 }
9095
9096 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9097 }
9098 }
9099
9100 if (old_current_buffer)
9101 set_buffer_internal (old_current_buffer);
9102 }
9103
9104 return window_height_changed_p;
9105 }
9106
9107
9108 /* Value is the current message, a string, or nil if there is no
9109 current message. */
9110
9111 Lisp_Object
9112 current_message ()
9113 {
9114 Lisp_Object msg;
9115
9116 if (!BUFFERP (echo_area_buffer[0]))
9117 msg = Qnil;
9118 else
9119 {
9120 with_echo_area_buffer (0, 0, current_message_1,
9121 (EMACS_INT) &msg, Qnil, 0, 0);
9122 if (NILP (msg))
9123 echo_area_buffer[0] = Qnil;
9124 }
9125
9126 return msg;
9127 }
9128
9129
9130 static int
9131 current_message_1 (a1, a2, a3, a4)
9132 EMACS_INT a1;
9133 Lisp_Object a2;
9134 EMACS_INT a3, a4;
9135 {
9136 Lisp_Object *msg = (Lisp_Object *) a1;
9137
9138 if (Z > BEG)
9139 *msg = make_buffer_string (BEG, Z, 1);
9140 else
9141 *msg = Qnil;
9142 return 0;
9143 }
9144
9145
9146 /* Push the current message on Vmessage_stack for later restauration
9147 by restore_message. Value is non-zero if the current message isn't
9148 empty. This is a relatively infrequent operation, so it's not
9149 worth optimizing. */
9150
9151 int
9152 push_message ()
9153 {
9154 Lisp_Object msg;
9155 msg = current_message ();
9156 Vmessage_stack = Fcons (msg, Vmessage_stack);
9157 return STRINGP (msg);
9158 }
9159
9160
9161 /* Restore message display from the top of Vmessage_stack. */
9162
9163 void
9164 restore_message ()
9165 {
9166 Lisp_Object msg;
9167
9168 xassert (CONSP (Vmessage_stack));
9169 msg = XCAR (Vmessage_stack);
9170 if (STRINGP (msg))
9171 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9172 else
9173 message3_nolog (msg, 0, 0);
9174 }
9175
9176
9177 /* Handler for record_unwind_protect calling pop_message. */
9178
9179 Lisp_Object
9180 pop_message_unwind (dummy)
9181 Lisp_Object dummy;
9182 {
9183 pop_message ();
9184 return Qnil;
9185 }
9186
9187 /* Pop the top-most entry off Vmessage_stack. */
9188
9189 void
9190 pop_message ()
9191 {
9192 xassert (CONSP (Vmessage_stack));
9193 Vmessage_stack = XCDR (Vmessage_stack);
9194 }
9195
9196
9197 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9198 exits. If the stack is not empty, we have a missing pop_message
9199 somewhere. */
9200
9201 void
9202 check_message_stack ()
9203 {
9204 if (!NILP (Vmessage_stack))
9205 abort ();
9206 }
9207
9208
9209 /* Truncate to NCHARS what will be displayed in the echo area the next
9210 time we display it---but don't redisplay it now. */
9211
9212 void
9213 truncate_echo_area (nchars)
9214 int nchars;
9215 {
9216 if (nchars == 0)
9217 echo_area_buffer[0] = Qnil;
9218 /* A null message buffer means that the frame hasn't really been
9219 initialized yet. Error messages get reported properly by
9220 cmd_error, so this must be just an informative message; toss it. */
9221 else if (!noninteractive
9222 && INTERACTIVE
9223 && !NILP (echo_area_buffer[0]))
9224 {
9225 struct frame *sf = SELECTED_FRAME ();
9226 if (FRAME_MESSAGE_BUF (sf))
9227 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9228 }
9229 }
9230
9231
9232 /* Helper function for truncate_echo_area. Truncate the current
9233 message to at most NCHARS characters. */
9234
9235 static int
9236 truncate_message_1 (nchars, a2, a3, a4)
9237 EMACS_INT nchars;
9238 Lisp_Object a2;
9239 EMACS_INT a3, a4;
9240 {
9241 if (BEG + nchars < Z)
9242 del_range (BEG + nchars, Z);
9243 if (Z == BEG)
9244 echo_area_buffer[0] = Qnil;
9245 return 0;
9246 }
9247
9248
9249 /* Set the current message to a substring of S or STRING.
9250
9251 If STRING is a Lisp string, set the message to the first NBYTES
9252 bytes from STRING. NBYTES zero means use the whole string. If
9253 STRING is multibyte, the message will be displayed multibyte.
9254
9255 If S is not null, set the message to the first LEN bytes of S. LEN
9256 zero means use the whole string. MULTIBYTE_P non-zero means S is
9257 multibyte. Display the message multibyte in that case.
9258
9259 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9260 to t before calling set_message_1 (which calls insert).
9261 */
9262
9263 void
9264 set_message (s, string, nbytes, multibyte_p)
9265 const char *s;
9266 Lisp_Object string;
9267 int nbytes, multibyte_p;
9268 {
9269 message_enable_multibyte
9270 = ((s && multibyte_p)
9271 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9272
9273 with_echo_area_buffer (0, -1, set_message_1,
9274 (EMACS_INT) s, string, nbytes, multibyte_p);
9275 message_buf_print = 0;
9276 help_echo_showing_p = 0;
9277 }
9278
9279
9280 /* Helper function for set_message. Arguments have the same meaning
9281 as there, with A1 corresponding to S and A2 corresponding to STRING
9282 This function is called with the echo area buffer being
9283 current. */
9284
9285 static int
9286 set_message_1 (a1, a2, nbytes, multibyte_p)
9287 EMACS_INT a1;
9288 Lisp_Object a2;
9289 EMACS_INT nbytes, multibyte_p;
9290 {
9291 const char *s = (const char *) a1;
9292 Lisp_Object string = a2;
9293
9294 /* Change multibyteness of the echo buffer appropriately. */
9295 if (message_enable_multibyte
9296 != !NILP (current_buffer->enable_multibyte_characters))
9297 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9298
9299 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9300
9301 /* Insert new message at BEG. */
9302 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9303
9304 if (STRINGP (string))
9305 {
9306 int nchars;
9307
9308 if (nbytes == 0)
9309 nbytes = SBYTES (string);
9310 nchars = string_byte_to_char (string, nbytes);
9311
9312 /* This function takes care of single/multibyte conversion. We
9313 just have to ensure that the echo area buffer has the right
9314 setting of enable_multibyte_characters. */
9315 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9316 }
9317 else if (s)
9318 {
9319 if (nbytes == 0)
9320 nbytes = strlen (s);
9321
9322 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9323 {
9324 /* Convert from multi-byte to single-byte. */
9325 int i, c, n;
9326 unsigned char work[1];
9327
9328 /* Convert a multibyte string to single-byte. */
9329 for (i = 0; i < nbytes; i += n)
9330 {
9331 c = string_char_and_length (s + i, &n);
9332 work[0] = (ASCII_CHAR_P (c)
9333 ? c
9334 : multibyte_char_to_unibyte (c, Qnil));
9335 insert_1_both (work, 1, 1, 1, 0, 0);
9336 }
9337 }
9338 else if (!multibyte_p
9339 && !NILP (current_buffer->enable_multibyte_characters))
9340 {
9341 /* Convert from single-byte to multi-byte. */
9342 int i, c, n;
9343 const unsigned char *msg = (const unsigned char *) s;
9344 unsigned char str[MAX_MULTIBYTE_LENGTH];
9345
9346 /* Convert a single-byte string to multibyte. */
9347 for (i = 0; i < nbytes; i++)
9348 {
9349 c = msg[i];
9350 MAKE_CHAR_MULTIBYTE (c);
9351 n = CHAR_STRING (c, str);
9352 insert_1_both (str, 1, n, 1, 0, 0);
9353 }
9354 }
9355 else
9356 insert_1 (s, nbytes, 1, 0, 0);
9357 }
9358
9359 return 0;
9360 }
9361
9362
9363 /* Clear messages. CURRENT_P non-zero means clear the current
9364 message. LAST_DISPLAYED_P non-zero means clear the message
9365 last displayed. */
9366
9367 void
9368 clear_message (current_p, last_displayed_p)
9369 int current_p, last_displayed_p;
9370 {
9371 if (current_p)
9372 {
9373 echo_area_buffer[0] = Qnil;
9374 message_cleared_p = 1;
9375 }
9376
9377 if (last_displayed_p)
9378 echo_area_buffer[1] = Qnil;
9379
9380 message_buf_print = 0;
9381 }
9382
9383 /* Clear garbaged frames.
9384
9385 This function is used where the old redisplay called
9386 redraw_garbaged_frames which in turn called redraw_frame which in
9387 turn called clear_frame. The call to clear_frame was a source of
9388 flickering. I believe a clear_frame is not necessary. It should
9389 suffice in the new redisplay to invalidate all current matrices,
9390 and ensure a complete redisplay of all windows. */
9391
9392 static void
9393 clear_garbaged_frames ()
9394 {
9395 if (frame_garbaged)
9396 {
9397 Lisp_Object tail, frame;
9398 int changed_count = 0;
9399
9400 FOR_EACH_FRAME (tail, frame)
9401 {
9402 struct frame *f = XFRAME (frame);
9403
9404 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9405 {
9406 if (f->resized_p)
9407 {
9408 Fredraw_frame (frame);
9409 f->force_flush_display_p = 1;
9410 }
9411 clear_current_matrices (f);
9412 changed_count++;
9413 f->garbaged = 0;
9414 f->resized_p = 0;
9415 }
9416 }
9417
9418 frame_garbaged = 0;
9419 if (changed_count)
9420 ++windows_or_buffers_changed;
9421 }
9422 }
9423
9424
9425 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9426 is non-zero update selected_frame. Value is non-zero if the
9427 mini-windows height has been changed. */
9428
9429 static int
9430 echo_area_display (update_frame_p)
9431 int update_frame_p;
9432 {
9433 Lisp_Object mini_window;
9434 struct window *w;
9435 struct frame *f;
9436 int window_height_changed_p = 0;
9437 struct frame *sf = SELECTED_FRAME ();
9438
9439 mini_window = FRAME_MINIBUF_WINDOW (sf);
9440 w = XWINDOW (mini_window);
9441 f = XFRAME (WINDOW_FRAME (w));
9442
9443 /* Don't display if frame is invisible or not yet initialized. */
9444 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9445 return 0;
9446
9447 #ifdef HAVE_WINDOW_SYSTEM
9448 /* When Emacs starts, selected_frame may be the initial terminal
9449 frame. If we let this through, a message would be displayed on
9450 the terminal. */
9451 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9452 return 0;
9453 #endif /* HAVE_WINDOW_SYSTEM */
9454
9455 /* Redraw garbaged frames. */
9456 if (frame_garbaged)
9457 clear_garbaged_frames ();
9458
9459 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9460 {
9461 echo_area_window = mini_window;
9462 window_height_changed_p = display_echo_area (w);
9463 w->must_be_updated_p = 1;
9464
9465 /* Update the display, unless called from redisplay_internal.
9466 Also don't update the screen during redisplay itself. The
9467 update will happen at the end of redisplay, and an update
9468 here could cause confusion. */
9469 if (update_frame_p && !redisplaying_p)
9470 {
9471 int n = 0;
9472
9473 /* If the display update has been interrupted by pending
9474 input, update mode lines in the frame. Due to the
9475 pending input, it might have been that redisplay hasn't
9476 been called, so that mode lines above the echo area are
9477 garbaged. This looks odd, so we prevent it here. */
9478 if (!display_completed)
9479 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9480
9481 if (window_height_changed_p
9482 /* Don't do this if Emacs is shutting down. Redisplay
9483 needs to run hooks. */
9484 && !NILP (Vrun_hooks))
9485 {
9486 /* Must update other windows. Likewise as in other
9487 cases, don't let this update be interrupted by
9488 pending input. */
9489 int count = SPECPDL_INDEX ();
9490 specbind (Qredisplay_dont_pause, Qt);
9491 windows_or_buffers_changed = 1;
9492 redisplay_internal (0);
9493 unbind_to (count, Qnil);
9494 }
9495 else if (FRAME_WINDOW_P (f) && n == 0)
9496 {
9497 /* Window configuration is the same as before.
9498 Can do with a display update of the echo area,
9499 unless we displayed some mode lines. */
9500 update_single_window (w, 1);
9501 FRAME_RIF (f)->flush_display (f);
9502 }
9503 else
9504 update_frame (f, 1, 1);
9505
9506 /* If cursor is in the echo area, make sure that the next
9507 redisplay displays the minibuffer, so that the cursor will
9508 be replaced with what the minibuffer wants. */
9509 if (cursor_in_echo_area)
9510 ++windows_or_buffers_changed;
9511 }
9512 }
9513 else if (!EQ (mini_window, selected_window))
9514 windows_or_buffers_changed++;
9515
9516 /* Last displayed message is now the current message. */
9517 echo_area_buffer[1] = echo_area_buffer[0];
9518 /* Inform read_char that we're not echoing. */
9519 echo_message_buffer = Qnil;
9520
9521 /* Prevent redisplay optimization in redisplay_internal by resetting
9522 this_line_start_pos. This is done because the mini-buffer now
9523 displays the message instead of its buffer text. */
9524 if (EQ (mini_window, selected_window))
9525 CHARPOS (this_line_start_pos) = 0;
9526
9527 return window_height_changed_p;
9528 }
9529
9530
9531 \f
9532 /***********************************************************************
9533 Mode Lines and Frame Titles
9534 ***********************************************************************/
9535
9536 /* A buffer for constructing non-propertized mode-line strings and
9537 frame titles in it; allocated from the heap in init_xdisp and
9538 resized as needed in store_mode_line_noprop_char. */
9539
9540 static char *mode_line_noprop_buf;
9541
9542 /* The buffer's end, and a current output position in it. */
9543
9544 static char *mode_line_noprop_buf_end;
9545 static char *mode_line_noprop_ptr;
9546
9547 #define MODE_LINE_NOPROP_LEN(start) \
9548 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9549
9550 static enum {
9551 MODE_LINE_DISPLAY = 0,
9552 MODE_LINE_TITLE,
9553 MODE_LINE_NOPROP,
9554 MODE_LINE_STRING
9555 } mode_line_target;
9556
9557 /* Alist that caches the results of :propertize.
9558 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9559 static Lisp_Object mode_line_proptrans_alist;
9560
9561 /* List of strings making up the mode-line. */
9562 static Lisp_Object mode_line_string_list;
9563
9564 /* Base face property when building propertized mode line string. */
9565 static Lisp_Object mode_line_string_face;
9566 static Lisp_Object mode_line_string_face_prop;
9567
9568
9569 /* Unwind data for mode line strings */
9570
9571 static Lisp_Object Vmode_line_unwind_vector;
9572
9573 static Lisp_Object
9574 format_mode_line_unwind_data (struct buffer *obuf,
9575 Lisp_Object owin,
9576 int save_proptrans)
9577 {
9578 Lisp_Object vector, tmp;
9579
9580 /* Reduce consing by keeping one vector in
9581 Vwith_echo_area_save_vector. */
9582 vector = Vmode_line_unwind_vector;
9583 Vmode_line_unwind_vector = Qnil;
9584
9585 if (NILP (vector))
9586 vector = Fmake_vector (make_number (8), Qnil);
9587
9588 ASET (vector, 0, make_number (mode_line_target));
9589 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9590 ASET (vector, 2, mode_line_string_list);
9591 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9592 ASET (vector, 4, mode_line_string_face);
9593 ASET (vector, 5, mode_line_string_face_prop);
9594
9595 if (obuf)
9596 XSETBUFFER (tmp, obuf);
9597 else
9598 tmp = Qnil;
9599 ASET (vector, 6, tmp);
9600 ASET (vector, 7, owin);
9601
9602 return vector;
9603 }
9604
9605 static Lisp_Object
9606 unwind_format_mode_line (vector)
9607 Lisp_Object vector;
9608 {
9609 mode_line_target = XINT (AREF (vector, 0));
9610 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9611 mode_line_string_list = AREF (vector, 2);
9612 if (! EQ (AREF (vector, 3), Qt))
9613 mode_line_proptrans_alist = AREF (vector, 3);
9614 mode_line_string_face = AREF (vector, 4);
9615 mode_line_string_face_prop = AREF (vector, 5);
9616
9617 if (!NILP (AREF (vector, 7)))
9618 /* Select window before buffer, since it may change the buffer. */
9619 Fselect_window (AREF (vector, 7), Qt);
9620
9621 if (!NILP (AREF (vector, 6)))
9622 {
9623 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9624 ASET (vector, 6, Qnil);
9625 }
9626
9627 Vmode_line_unwind_vector = vector;
9628 return Qnil;
9629 }
9630
9631
9632 /* Store a single character C for the frame title in mode_line_noprop_buf.
9633 Re-allocate mode_line_noprop_buf if necessary. */
9634
9635 static void
9636 #ifdef PROTOTYPES
9637 store_mode_line_noprop_char (char c)
9638 #else
9639 store_mode_line_noprop_char (c)
9640 char c;
9641 #endif
9642 {
9643 /* If output position has reached the end of the allocated buffer,
9644 double the buffer's size. */
9645 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9646 {
9647 int len = MODE_LINE_NOPROP_LEN (0);
9648 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9649 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9650 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9651 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9652 }
9653
9654 *mode_line_noprop_ptr++ = c;
9655 }
9656
9657
9658 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9659 mode_line_noprop_ptr. STR is the string to store. Do not copy
9660 characters that yield more columns than PRECISION; PRECISION <= 0
9661 means copy the whole string. Pad with spaces until FIELD_WIDTH
9662 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9663 pad. Called from display_mode_element when it is used to build a
9664 frame title. */
9665
9666 static int
9667 store_mode_line_noprop (str, field_width, precision)
9668 const unsigned char *str;
9669 int field_width, precision;
9670 {
9671 int n = 0;
9672 int dummy, nbytes;
9673
9674 /* Copy at most PRECISION chars from STR. */
9675 nbytes = strlen (str);
9676 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9677 while (nbytes--)
9678 store_mode_line_noprop_char (*str++);
9679
9680 /* Fill up with spaces until FIELD_WIDTH reached. */
9681 while (field_width > 0
9682 && n < field_width)
9683 {
9684 store_mode_line_noprop_char (' ');
9685 ++n;
9686 }
9687
9688 return n;
9689 }
9690
9691 /***********************************************************************
9692 Frame Titles
9693 ***********************************************************************/
9694
9695 #ifdef HAVE_WINDOW_SYSTEM
9696
9697 /* Set the title of FRAME, if it has changed. The title format is
9698 Vicon_title_format if FRAME is iconified, otherwise it is
9699 frame_title_format. */
9700
9701 static void
9702 x_consider_frame_title (frame)
9703 Lisp_Object frame;
9704 {
9705 struct frame *f = XFRAME (frame);
9706
9707 if (FRAME_WINDOW_P (f)
9708 || FRAME_MINIBUF_ONLY_P (f)
9709 || f->explicit_name)
9710 {
9711 /* Do we have more than one visible frame on this X display? */
9712 Lisp_Object tail;
9713 Lisp_Object fmt;
9714 int title_start;
9715 char *title;
9716 int len;
9717 struct it it;
9718 int count = SPECPDL_INDEX ();
9719
9720 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9721 {
9722 Lisp_Object other_frame = XCAR (tail);
9723 struct frame *tf = XFRAME (other_frame);
9724
9725 if (tf != f
9726 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9727 && !FRAME_MINIBUF_ONLY_P (tf)
9728 && !EQ (other_frame, tip_frame)
9729 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9730 break;
9731 }
9732
9733 /* Set global variable indicating that multiple frames exist. */
9734 multiple_frames = CONSP (tail);
9735
9736 /* Switch to the buffer of selected window of the frame. Set up
9737 mode_line_target so that display_mode_element will output into
9738 mode_line_noprop_buf; then display the title. */
9739 record_unwind_protect (unwind_format_mode_line,
9740 format_mode_line_unwind_data
9741 (current_buffer, selected_window, 0));
9742
9743 Fselect_window (f->selected_window, Qt);
9744 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9745 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9746
9747 mode_line_target = MODE_LINE_TITLE;
9748 title_start = MODE_LINE_NOPROP_LEN (0);
9749 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9750 NULL, DEFAULT_FACE_ID);
9751 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9752 len = MODE_LINE_NOPROP_LEN (title_start);
9753 title = mode_line_noprop_buf + title_start;
9754 unbind_to (count, Qnil);
9755
9756 /* Set the title only if it's changed. This avoids consing in
9757 the common case where it hasn't. (If it turns out that we've
9758 already wasted too much time by walking through the list with
9759 display_mode_element, then we might need to optimize at a
9760 higher level than this.) */
9761 if (! STRINGP (f->name)
9762 || SBYTES (f->name) != len
9763 || bcmp (title, SDATA (f->name), len) != 0)
9764 x_implicitly_set_name (f, make_string (title, len), Qnil);
9765 }
9766 }
9767
9768 #endif /* not HAVE_WINDOW_SYSTEM */
9769
9770
9771
9772 \f
9773 /***********************************************************************
9774 Menu Bars
9775 ***********************************************************************/
9776
9777
9778 /* Prepare for redisplay by updating menu-bar item lists when
9779 appropriate. This can call eval. */
9780
9781 void
9782 prepare_menu_bars ()
9783 {
9784 int all_windows;
9785 struct gcpro gcpro1, gcpro2;
9786 struct frame *f;
9787 Lisp_Object tooltip_frame;
9788
9789 #ifdef HAVE_WINDOW_SYSTEM
9790 tooltip_frame = tip_frame;
9791 #else
9792 tooltip_frame = Qnil;
9793 #endif
9794
9795 /* Update all frame titles based on their buffer names, etc. We do
9796 this before the menu bars so that the buffer-menu will show the
9797 up-to-date frame titles. */
9798 #ifdef HAVE_WINDOW_SYSTEM
9799 if (windows_or_buffers_changed || update_mode_lines)
9800 {
9801 Lisp_Object tail, frame;
9802
9803 FOR_EACH_FRAME (tail, frame)
9804 {
9805 f = XFRAME (frame);
9806 if (!EQ (frame, tooltip_frame)
9807 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9808 x_consider_frame_title (frame);
9809 }
9810 }
9811 #endif /* HAVE_WINDOW_SYSTEM */
9812
9813 /* Update the menu bar item lists, if appropriate. This has to be
9814 done before any actual redisplay or generation of display lines. */
9815 all_windows = (update_mode_lines
9816 || buffer_shared > 1
9817 || windows_or_buffers_changed);
9818 if (all_windows)
9819 {
9820 Lisp_Object tail, frame;
9821 int count = SPECPDL_INDEX ();
9822 /* 1 means that update_menu_bar has run its hooks
9823 so any further calls to update_menu_bar shouldn't do so again. */
9824 int menu_bar_hooks_run = 0;
9825
9826 record_unwind_save_match_data ();
9827
9828 FOR_EACH_FRAME (tail, frame)
9829 {
9830 f = XFRAME (frame);
9831
9832 /* Ignore tooltip frame. */
9833 if (EQ (frame, tooltip_frame))
9834 continue;
9835
9836 /* If a window on this frame changed size, report that to
9837 the user and clear the size-change flag. */
9838 if (FRAME_WINDOW_SIZES_CHANGED (f))
9839 {
9840 Lisp_Object functions;
9841
9842 /* Clear flag first in case we get an error below. */
9843 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9844 functions = Vwindow_size_change_functions;
9845 GCPRO2 (tail, functions);
9846
9847 while (CONSP (functions))
9848 {
9849 if (!EQ (XCAR (functions), Qt))
9850 call1 (XCAR (functions), frame);
9851 functions = XCDR (functions);
9852 }
9853 UNGCPRO;
9854 }
9855
9856 GCPRO1 (tail);
9857 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9858 #ifdef HAVE_WINDOW_SYSTEM
9859 update_tool_bar (f, 0);
9860 #endif
9861 #ifdef HAVE_NS
9862 if (windows_or_buffers_changed)
9863 ns_set_doc_edited (f, Fbuffer_modified_p
9864 (XWINDOW (f->selected_window)->buffer));
9865 #endif
9866 UNGCPRO;
9867 }
9868
9869 unbind_to (count, Qnil);
9870 }
9871 else
9872 {
9873 struct frame *sf = SELECTED_FRAME ();
9874 update_menu_bar (sf, 1, 0);
9875 #ifdef HAVE_WINDOW_SYSTEM
9876 update_tool_bar (sf, 1);
9877 #endif
9878 }
9879
9880 /* Motif needs this. See comment in xmenu.c. Turn it off when
9881 pending_menu_activation is not defined. */
9882 #ifdef USE_X_TOOLKIT
9883 pending_menu_activation = 0;
9884 #endif
9885 }
9886
9887
9888 /* Update the menu bar item list for frame F. This has to be done
9889 before we start to fill in any display lines, because it can call
9890 eval.
9891
9892 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9893
9894 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9895 already ran the menu bar hooks for this redisplay, so there
9896 is no need to run them again. The return value is the
9897 updated value of this flag, to pass to the next call. */
9898
9899 static int
9900 update_menu_bar (f, save_match_data, hooks_run)
9901 struct frame *f;
9902 int save_match_data;
9903 int hooks_run;
9904 {
9905 Lisp_Object window;
9906 register struct window *w;
9907
9908 /* If called recursively during a menu update, do nothing. This can
9909 happen when, for instance, an activate-menubar-hook causes a
9910 redisplay. */
9911 if (inhibit_menubar_update)
9912 return hooks_run;
9913
9914 window = FRAME_SELECTED_WINDOW (f);
9915 w = XWINDOW (window);
9916
9917 if (FRAME_WINDOW_P (f)
9918 ?
9919 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9920 || defined (HAVE_NS) || defined (USE_GTK)
9921 FRAME_EXTERNAL_MENU_BAR (f)
9922 #else
9923 FRAME_MENU_BAR_LINES (f) > 0
9924 #endif
9925 : FRAME_MENU_BAR_LINES (f) > 0)
9926 {
9927 /* If the user has switched buffers or windows, we need to
9928 recompute to reflect the new bindings. But we'll
9929 recompute when update_mode_lines is set too; that means
9930 that people can use force-mode-line-update to request
9931 that the menu bar be recomputed. The adverse effect on
9932 the rest of the redisplay algorithm is about the same as
9933 windows_or_buffers_changed anyway. */
9934 if (windows_or_buffers_changed
9935 /* This used to test w->update_mode_line, but we believe
9936 there is no need to recompute the menu in that case. */
9937 || update_mode_lines
9938 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9939 < BUF_MODIFF (XBUFFER (w->buffer)))
9940 != !NILP (w->last_had_star))
9941 || ((!NILP (Vtransient_mark_mode)
9942 && !NILP (XBUFFER (w->buffer)->mark_active))
9943 != !NILP (w->region_showing)))
9944 {
9945 struct buffer *prev = current_buffer;
9946 int count = SPECPDL_INDEX ();
9947
9948 specbind (Qinhibit_menubar_update, Qt);
9949
9950 set_buffer_internal_1 (XBUFFER (w->buffer));
9951 if (save_match_data)
9952 record_unwind_save_match_data ();
9953 if (NILP (Voverriding_local_map_menu_flag))
9954 {
9955 specbind (Qoverriding_terminal_local_map, Qnil);
9956 specbind (Qoverriding_local_map, Qnil);
9957 }
9958
9959 if (!hooks_run)
9960 {
9961 /* Run the Lucid hook. */
9962 safe_run_hooks (Qactivate_menubar_hook);
9963
9964 /* If it has changed current-menubar from previous value,
9965 really recompute the menu-bar from the value. */
9966 if (! NILP (Vlucid_menu_bar_dirty_flag))
9967 call0 (Qrecompute_lucid_menubar);
9968
9969 safe_run_hooks (Qmenu_bar_update_hook);
9970
9971 hooks_run = 1;
9972 }
9973
9974 XSETFRAME (Vmenu_updating_frame, f);
9975 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9976
9977 /* Redisplay the menu bar in case we changed it. */
9978 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9979 || defined (HAVE_NS) || defined (USE_GTK)
9980 if (FRAME_WINDOW_P (f))
9981 {
9982 #if defined (HAVE_NS)
9983 /* All frames on Mac OS share the same menubar. So only
9984 the selected frame should be allowed to set it. */
9985 if (f == SELECTED_FRAME ())
9986 #endif
9987 set_frame_menubar (f, 0, 0);
9988 }
9989 else
9990 /* On a terminal screen, the menu bar is an ordinary screen
9991 line, and this makes it get updated. */
9992 w->update_mode_line = Qt;
9993 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9994 /* In the non-toolkit version, the menu bar is an ordinary screen
9995 line, and this makes it get updated. */
9996 w->update_mode_line = Qt;
9997 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9998
9999 unbind_to (count, Qnil);
10000 set_buffer_internal_1 (prev);
10001 }
10002 }
10003
10004 return hooks_run;
10005 }
10006
10007
10008 \f
10009 /***********************************************************************
10010 Output Cursor
10011 ***********************************************************************/
10012
10013 #ifdef HAVE_WINDOW_SYSTEM
10014
10015 /* EXPORT:
10016 Nominal cursor position -- where to draw output.
10017 HPOS and VPOS are window relative glyph matrix coordinates.
10018 X and Y are window relative pixel coordinates. */
10019
10020 struct cursor_pos output_cursor;
10021
10022
10023 /* EXPORT:
10024 Set the global variable output_cursor to CURSOR. All cursor
10025 positions are relative to updated_window. */
10026
10027 void
10028 set_output_cursor (cursor)
10029 struct cursor_pos *cursor;
10030 {
10031 output_cursor.hpos = cursor->hpos;
10032 output_cursor.vpos = cursor->vpos;
10033 output_cursor.x = cursor->x;
10034 output_cursor.y = cursor->y;
10035 }
10036
10037
10038 /* EXPORT for RIF:
10039 Set a nominal cursor position.
10040
10041 HPOS and VPOS are column/row positions in a window glyph matrix. X
10042 and Y are window text area relative pixel positions.
10043
10044 If this is done during an update, updated_window will contain the
10045 window that is being updated and the position is the future output
10046 cursor position for that window. If updated_window is null, use
10047 selected_window and display the cursor at the given position. */
10048
10049 void
10050 x_cursor_to (vpos, hpos, y, x)
10051 int vpos, hpos, y, x;
10052 {
10053 struct window *w;
10054
10055 /* If updated_window is not set, work on selected_window. */
10056 if (updated_window)
10057 w = updated_window;
10058 else
10059 w = XWINDOW (selected_window);
10060
10061 /* Set the output cursor. */
10062 output_cursor.hpos = hpos;
10063 output_cursor.vpos = vpos;
10064 output_cursor.x = x;
10065 output_cursor.y = y;
10066
10067 /* If not called as part of an update, really display the cursor.
10068 This will also set the cursor position of W. */
10069 if (updated_window == NULL)
10070 {
10071 BLOCK_INPUT;
10072 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10073 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10074 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10075 UNBLOCK_INPUT;
10076 }
10077 }
10078
10079 #endif /* HAVE_WINDOW_SYSTEM */
10080
10081 \f
10082 /***********************************************************************
10083 Tool-bars
10084 ***********************************************************************/
10085
10086 #ifdef HAVE_WINDOW_SYSTEM
10087
10088 /* Where the mouse was last time we reported a mouse event. */
10089
10090 FRAME_PTR last_mouse_frame;
10091
10092 /* Tool-bar item index of the item on which a mouse button was pressed
10093 or -1. */
10094
10095 int last_tool_bar_item;
10096
10097
10098 static Lisp_Object
10099 update_tool_bar_unwind (frame)
10100 Lisp_Object frame;
10101 {
10102 selected_frame = frame;
10103 return Qnil;
10104 }
10105
10106 /* Update the tool-bar item list for frame F. This has to be done
10107 before we start to fill in any display lines. Called from
10108 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10109 and restore it here. */
10110
10111 static void
10112 update_tool_bar (f, save_match_data)
10113 struct frame *f;
10114 int save_match_data;
10115 {
10116 #if defined (USE_GTK) || defined (HAVE_NS)
10117 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10118 #else
10119 int do_update = WINDOWP (f->tool_bar_window)
10120 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10121 #endif
10122
10123 if (do_update)
10124 {
10125 Lisp_Object window;
10126 struct window *w;
10127
10128 window = FRAME_SELECTED_WINDOW (f);
10129 w = XWINDOW (window);
10130
10131 /* If the user has switched buffers or windows, we need to
10132 recompute to reflect the new bindings. But we'll
10133 recompute when update_mode_lines is set too; that means
10134 that people can use force-mode-line-update to request
10135 that the menu bar be recomputed. The adverse effect on
10136 the rest of the redisplay algorithm is about the same as
10137 windows_or_buffers_changed anyway. */
10138 if (windows_or_buffers_changed
10139 || !NILP (w->update_mode_line)
10140 || update_mode_lines
10141 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10142 < BUF_MODIFF (XBUFFER (w->buffer)))
10143 != !NILP (w->last_had_star))
10144 || ((!NILP (Vtransient_mark_mode)
10145 && !NILP (XBUFFER (w->buffer)->mark_active))
10146 != !NILP (w->region_showing)))
10147 {
10148 struct buffer *prev = current_buffer;
10149 int count = SPECPDL_INDEX ();
10150 Lisp_Object frame, new_tool_bar;
10151 int new_n_tool_bar;
10152 struct gcpro gcpro1;
10153
10154 /* Set current_buffer to the buffer of the selected
10155 window of the frame, so that we get the right local
10156 keymaps. */
10157 set_buffer_internal_1 (XBUFFER (w->buffer));
10158
10159 /* Save match data, if we must. */
10160 if (save_match_data)
10161 record_unwind_save_match_data ();
10162
10163 /* Make sure that we don't accidentally use bogus keymaps. */
10164 if (NILP (Voverriding_local_map_menu_flag))
10165 {
10166 specbind (Qoverriding_terminal_local_map, Qnil);
10167 specbind (Qoverriding_local_map, Qnil);
10168 }
10169
10170 GCPRO1 (new_tool_bar);
10171
10172 /* We must temporarily set the selected frame to this frame
10173 before calling tool_bar_items, because the calculation of
10174 the tool-bar keymap uses the selected frame (see
10175 `tool-bar-make-keymap' in tool-bar.el). */
10176 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10177 XSETFRAME (frame, f);
10178 selected_frame = frame;
10179
10180 /* Build desired tool-bar items from keymaps. */
10181 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10182 &new_n_tool_bar);
10183
10184 /* Redisplay the tool-bar if we changed it. */
10185 if (new_n_tool_bar != f->n_tool_bar_items
10186 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10187 {
10188 /* Redisplay that happens asynchronously due to an expose event
10189 may access f->tool_bar_items. Make sure we update both
10190 variables within BLOCK_INPUT so no such event interrupts. */
10191 BLOCK_INPUT;
10192 f->tool_bar_items = new_tool_bar;
10193 f->n_tool_bar_items = new_n_tool_bar;
10194 w->update_mode_line = Qt;
10195 UNBLOCK_INPUT;
10196 }
10197
10198 UNGCPRO;
10199
10200 unbind_to (count, Qnil);
10201 set_buffer_internal_1 (prev);
10202 }
10203 }
10204 }
10205
10206
10207 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10208 F's desired tool-bar contents. F->tool_bar_items must have
10209 been set up previously by calling prepare_menu_bars. */
10210
10211 static void
10212 build_desired_tool_bar_string (f)
10213 struct frame *f;
10214 {
10215 int i, size, size_needed;
10216 struct gcpro gcpro1, gcpro2, gcpro3;
10217 Lisp_Object image, plist, props;
10218
10219 image = plist = props = Qnil;
10220 GCPRO3 (image, plist, props);
10221
10222 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10223 Otherwise, make a new string. */
10224
10225 /* The size of the string we might be able to reuse. */
10226 size = (STRINGP (f->desired_tool_bar_string)
10227 ? SCHARS (f->desired_tool_bar_string)
10228 : 0);
10229
10230 /* We need one space in the string for each image. */
10231 size_needed = f->n_tool_bar_items;
10232
10233 /* Reuse f->desired_tool_bar_string, if possible. */
10234 if (size < size_needed || NILP (f->desired_tool_bar_string))
10235 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10236 make_number (' '));
10237 else
10238 {
10239 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10240 Fremove_text_properties (make_number (0), make_number (size),
10241 props, f->desired_tool_bar_string);
10242 }
10243
10244 /* Put a `display' property on the string for the images to display,
10245 put a `menu_item' property on tool-bar items with a value that
10246 is the index of the item in F's tool-bar item vector. */
10247 for (i = 0; i < f->n_tool_bar_items; ++i)
10248 {
10249 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10250
10251 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10252 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10253 int hmargin, vmargin, relief, idx, end;
10254 extern Lisp_Object QCrelief, QCmargin, QCconversion;
10255
10256 /* If image is a vector, choose the image according to the
10257 button state. */
10258 image = PROP (TOOL_BAR_ITEM_IMAGES);
10259 if (VECTORP (image))
10260 {
10261 if (enabled_p)
10262 idx = (selected_p
10263 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10264 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10265 else
10266 idx = (selected_p
10267 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10268 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10269
10270 xassert (ASIZE (image) >= idx);
10271 image = AREF (image, idx);
10272 }
10273 else
10274 idx = -1;
10275
10276 /* Ignore invalid image specifications. */
10277 if (!valid_image_p (image))
10278 continue;
10279
10280 /* Display the tool-bar button pressed, or depressed. */
10281 plist = Fcopy_sequence (XCDR (image));
10282
10283 /* Compute margin and relief to draw. */
10284 relief = (tool_bar_button_relief >= 0
10285 ? tool_bar_button_relief
10286 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10287 hmargin = vmargin = relief;
10288
10289 if (INTEGERP (Vtool_bar_button_margin)
10290 && XINT (Vtool_bar_button_margin) > 0)
10291 {
10292 hmargin += XFASTINT (Vtool_bar_button_margin);
10293 vmargin += XFASTINT (Vtool_bar_button_margin);
10294 }
10295 else if (CONSP (Vtool_bar_button_margin))
10296 {
10297 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10298 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10299 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10300
10301 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10302 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10303 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10304 }
10305
10306 if (auto_raise_tool_bar_buttons_p)
10307 {
10308 /* Add a `:relief' property to the image spec if the item is
10309 selected. */
10310 if (selected_p)
10311 {
10312 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10313 hmargin -= relief;
10314 vmargin -= relief;
10315 }
10316 }
10317 else
10318 {
10319 /* If image is selected, display it pressed, i.e. with a
10320 negative relief. If it's not selected, display it with a
10321 raised relief. */
10322 plist = Fplist_put (plist, QCrelief,
10323 (selected_p
10324 ? make_number (-relief)
10325 : make_number (relief)));
10326 hmargin -= relief;
10327 vmargin -= relief;
10328 }
10329
10330 /* Put a margin around the image. */
10331 if (hmargin || vmargin)
10332 {
10333 if (hmargin == vmargin)
10334 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10335 else
10336 plist = Fplist_put (plist, QCmargin,
10337 Fcons (make_number (hmargin),
10338 make_number (vmargin)));
10339 }
10340
10341 /* If button is not enabled, and we don't have special images
10342 for the disabled state, make the image appear disabled by
10343 applying an appropriate algorithm to it. */
10344 if (!enabled_p && idx < 0)
10345 plist = Fplist_put (plist, QCconversion, Qdisabled);
10346
10347 /* Put a `display' text property on the string for the image to
10348 display. Put a `menu-item' property on the string that gives
10349 the start of this item's properties in the tool-bar items
10350 vector. */
10351 image = Fcons (Qimage, plist);
10352 props = list4 (Qdisplay, image,
10353 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10354
10355 /* Let the last image hide all remaining spaces in the tool bar
10356 string. The string can be longer than needed when we reuse a
10357 previous string. */
10358 if (i + 1 == f->n_tool_bar_items)
10359 end = SCHARS (f->desired_tool_bar_string);
10360 else
10361 end = i + 1;
10362 Fadd_text_properties (make_number (i), make_number (end),
10363 props, f->desired_tool_bar_string);
10364 #undef PROP
10365 }
10366
10367 UNGCPRO;
10368 }
10369
10370
10371 /* Display one line of the tool-bar of frame IT->f.
10372
10373 HEIGHT specifies the desired height of the tool-bar line.
10374 If the actual height of the glyph row is less than HEIGHT, the
10375 row's height is increased to HEIGHT, and the icons are centered
10376 vertically in the new height.
10377
10378 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10379 count a final empty row in case the tool-bar width exactly matches
10380 the window width.
10381 */
10382
10383 static void
10384 display_tool_bar_line (it, height)
10385 struct it *it;
10386 int height;
10387 {
10388 struct glyph_row *row = it->glyph_row;
10389 int max_x = it->last_visible_x;
10390 struct glyph *last;
10391
10392 prepare_desired_row (row);
10393 row->y = it->current_y;
10394
10395 /* Note that this isn't made use of if the face hasn't a box,
10396 so there's no need to check the face here. */
10397 it->start_of_box_run_p = 1;
10398
10399 while (it->current_x < max_x)
10400 {
10401 int x, n_glyphs_before, i, nglyphs;
10402 struct it it_before;
10403
10404 /* Get the next display element. */
10405 if (!get_next_display_element (it))
10406 {
10407 /* Don't count empty row if we are counting needed tool-bar lines. */
10408 if (height < 0 && !it->hpos)
10409 return;
10410 break;
10411 }
10412
10413 /* Produce glyphs. */
10414 n_glyphs_before = row->used[TEXT_AREA];
10415 it_before = *it;
10416
10417 PRODUCE_GLYPHS (it);
10418
10419 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10420 i = 0;
10421 x = it_before.current_x;
10422 while (i < nglyphs)
10423 {
10424 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10425
10426 if (x + glyph->pixel_width > max_x)
10427 {
10428 /* Glyph doesn't fit on line. Backtrack. */
10429 row->used[TEXT_AREA] = n_glyphs_before;
10430 *it = it_before;
10431 /* If this is the only glyph on this line, it will never fit on the
10432 toolbar, so skip it. But ensure there is at least one glyph,
10433 so we don't accidentally disable the tool-bar. */
10434 if (n_glyphs_before == 0
10435 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10436 break;
10437 goto out;
10438 }
10439
10440 ++it->hpos;
10441 x += glyph->pixel_width;
10442 ++i;
10443 }
10444
10445 /* Stop at line ends. */
10446 if (ITERATOR_AT_END_OF_LINE_P (it))
10447 break;
10448
10449 set_iterator_to_next (it, 1);
10450 }
10451
10452 out:;
10453
10454 row->displays_text_p = row->used[TEXT_AREA] != 0;
10455
10456 /* Use default face for the border below the tool bar.
10457
10458 FIXME: When auto-resize-tool-bars is grow-only, there is
10459 no additional border below the possibly empty tool-bar lines.
10460 So to make the extra empty lines look "normal", we have to
10461 use the tool-bar face for the border too. */
10462 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10463 it->face_id = DEFAULT_FACE_ID;
10464
10465 extend_face_to_end_of_line (it);
10466 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10467 last->right_box_line_p = 1;
10468 if (last == row->glyphs[TEXT_AREA])
10469 last->left_box_line_p = 1;
10470
10471 /* Make line the desired height and center it vertically. */
10472 if ((height -= it->max_ascent + it->max_descent) > 0)
10473 {
10474 /* Don't add more than one line height. */
10475 height %= FRAME_LINE_HEIGHT (it->f);
10476 it->max_ascent += height / 2;
10477 it->max_descent += (height + 1) / 2;
10478 }
10479
10480 compute_line_metrics (it);
10481
10482 /* If line is empty, make it occupy the rest of the tool-bar. */
10483 if (!row->displays_text_p)
10484 {
10485 row->height = row->phys_height = it->last_visible_y - row->y;
10486 row->visible_height = row->height;
10487 row->ascent = row->phys_ascent = 0;
10488 row->extra_line_spacing = 0;
10489 }
10490
10491 row->full_width_p = 1;
10492 row->continued_p = 0;
10493 row->truncated_on_left_p = 0;
10494 row->truncated_on_right_p = 0;
10495
10496 it->current_x = it->hpos = 0;
10497 it->current_y += row->height;
10498 ++it->vpos;
10499 ++it->glyph_row;
10500 }
10501
10502
10503 /* Max tool-bar height. */
10504
10505 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10506 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10507
10508 /* Value is the number of screen lines needed to make all tool-bar
10509 items of frame F visible. The number of actual rows needed is
10510 returned in *N_ROWS if non-NULL. */
10511
10512 static int
10513 tool_bar_lines_needed (f, n_rows)
10514 struct frame *f;
10515 int *n_rows;
10516 {
10517 struct window *w = XWINDOW (f->tool_bar_window);
10518 struct it it;
10519 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10520 the desired matrix, so use (unused) mode-line row as temporary row to
10521 avoid destroying the first tool-bar row. */
10522 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10523
10524 /* Initialize an iterator for iteration over
10525 F->desired_tool_bar_string in the tool-bar window of frame F. */
10526 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10527 it.first_visible_x = 0;
10528 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10529 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10530
10531 while (!ITERATOR_AT_END_P (&it))
10532 {
10533 clear_glyph_row (temp_row);
10534 it.glyph_row = temp_row;
10535 display_tool_bar_line (&it, -1);
10536 }
10537 clear_glyph_row (temp_row);
10538
10539 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10540 if (n_rows)
10541 *n_rows = it.vpos > 0 ? it.vpos : -1;
10542
10543 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10544 }
10545
10546
10547 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10548 0, 1, 0,
10549 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10550 (frame)
10551 Lisp_Object frame;
10552 {
10553 struct frame *f;
10554 struct window *w;
10555 int nlines = 0;
10556
10557 if (NILP (frame))
10558 frame = selected_frame;
10559 else
10560 CHECK_FRAME (frame);
10561 f = XFRAME (frame);
10562
10563 if (WINDOWP (f->tool_bar_window)
10564 || (w = XWINDOW (f->tool_bar_window),
10565 WINDOW_TOTAL_LINES (w) > 0))
10566 {
10567 update_tool_bar (f, 1);
10568 if (f->n_tool_bar_items)
10569 {
10570 build_desired_tool_bar_string (f);
10571 nlines = tool_bar_lines_needed (f, NULL);
10572 }
10573 }
10574
10575 return make_number (nlines);
10576 }
10577
10578
10579 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10580 height should be changed. */
10581
10582 static int
10583 redisplay_tool_bar (f)
10584 struct frame *f;
10585 {
10586 struct window *w;
10587 struct it it;
10588 struct glyph_row *row;
10589
10590 #if defined (USE_GTK) || defined (HAVE_NS)
10591 if (FRAME_EXTERNAL_TOOL_BAR (f))
10592 update_frame_tool_bar (f);
10593 return 0;
10594 #endif
10595
10596 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10597 do anything. This means you must start with tool-bar-lines
10598 non-zero to get the auto-sizing effect. Or in other words, you
10599 can turn off tool-bars by specifying tool-bar-lines zero. */
10600 if (!WINDOWP (f->tool_bar_window)
10601 || (w = XWINDOW (f->tool_bar_window),
10602 WINDOW_TOTAL_LINES (w) == 0))
10603 return 0;
10604
10605 /* Set up an iterator for the tool-bar window. */
10606 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10607 it.first_visible_x = 0;
10608 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10609 row = it.glyph_row;
10610
10611 /* Build a string that represents the contents of the tool-bar. */
10612 build_desired_tool_bar_string (f);
10613 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10614
10615 if (f->n_tool_bar_rows == 0)
10616 {
10617 int nlines;
10618
10619 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10620 nlines != WINDOW_TOTAL_LINES (w)))
10621 {
10622 extern Lisp_Object Qtool_bar_lines;
10623 Lisp_Object frame;
10624 int old_height = WINDOW_TOTAL_LINES (w);
10625
10626 XSETFRAME (frame, f);
10627 Fmodify_frame_parameters (frame,
10628 Fcons (Fcons (Qtool_bar_lines,
10629 make_number (nlines)),
10630 Qnil));
10631 if (WINDOW_TOTAL_LINES (w) != old_height)
10632 {
10633 clear_glyph_matrix (w->desired_matrix);
10634 fonts_changed_p = 1;
10635 return 1;
10636 }
10637 }
10638 }
10639
10640 /* Display as many lines as needed to display all tool-bar items. */
10641
10642 if (f->n_tool_bar_rows > 0)
10643 {
10644 int border, rows, height, extra;
10645
10646 if (INTEGERP (Vtool_bar_border))
10647 border = XINT (Vtool_bar_border);
10648 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10649 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10650 else if (EQ (Vtool_bar_border, Qborder_width))
10651 border = f->border_width;
10652 else
10653 border = 0;
10654 if (border < 0)
10655 border = 0;
10656
10657 rows = f->n_tool_bar_rows;
10658 height = max (1, (it.last_visible_y - border) / rows);
10659 extra = it.last_visible_y - border - height * rows;
10660
10661 while (it.current_y < it.last_visible_y)
10662 {
10663 int h = 0;
10664 if (extra > 0 && rows-- > 0)
10665 {
10666 h = (extra + rows - 1) / rows;
10667 extra -= h;
10668 }
10669 display_tool_bar_line (&it, height + h);
10670 }
10671 }
10672 else
10673 {
10674 while (it.current_y < it.last_visible_y)
10675 display_tool_bar_line (&it, 0);
10676 }
10677
10678 /* It doesn't make much sense to try scrolling in the tool-bar
10679 window, so don't do it. */
10680 w->desired_matrix->no_scrolling_p = 1;
10681 w->must_be_updated_p = 1;
10682
10683 if (!NILP (Vauto_resize_tool_bars))
10684 {
10685 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10686 int change_height_p = 0;
10687
10688 /* If we couldn't display everything, change the tool-bar's
10689 height if there is room for more. */
10690 if (IT_STRING_CHARPOS (it) < it.end_charpos
10691 && it.current_y < max_tool_bar_height)
10692 change_height_p = 1;
10693
10694 row = it.glyph_row - 1;
10695
10696 /* If there are blank lines at the end, except for a partially
10697 visible blank line at the end that is smaller than
10698 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10699 if (!row->displays_text_p
10700 && row->height >= FRAME_LINE_HEIGHT (f))
10701 change_height_p = 1;
10702
10703 /* If row displays tool-bar items, but is partially visible,
10704 change the tool-bar's height. */
10705 if (row->displays_text_p
10706 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10707 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10708 change_height_p = 1;
10709
10710 /* Resize windows as needed by changing the `tool-bar-lines'
10711 frame parameter. */
10712 if (change_height_p)
10713 {
10714 extern Lisp_Object Qtool_bar_lines;
10715 Lisp_Object frame;
10716 int old_height = WINDOW_TOTAL_LINES (w);
10717 int nrows;
10718 int nlines = tool_bar_lines_needed (f, &nrows);
10719
10720 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10721 && !f->minimize_tool_bar_window_p)
10722 ? (nlines > old_height)
10723 : (nlines != old_height));
10724 f->minimize_tool_bar_window_p = 0;
10725
10726 if (change_height_p)
10727 {
10728 XSETFRAME (frame, f);
10729 Fmodify_frame_parameters (frame,
10730 Fcons (Fcons (Qtool_bar_lines,
10731 make_number (nlines)),
10732 Qnil));
10733 if (WINDOW_TOTAL_LINES (w) != old_height)
10734 {
10735 clear_glyph_matrix (w->desired_matrix);
10736 f->n_tool_bar_rows = nrows;
10737 fonts_changed_p = 1;
10738 return 1;
10739 }
10740 }
10741 }
10742 }
10743
10744 f->minimize_tool_bar_window_p = 0;
10745 return 0;
10746 }
10747
10748
10749 /* Get information about the tool-bar item which is displayed in GLYPH
10750 on frame F. Return in *PROP_IDX the index where tool-bar item
10751 properties start in F->tool_bar_items. Value is zero if
10752 GLYPH doesn't display a tool-bar item. */
10753
10754 static int
10755 tool_bar_item_info (f, glyph, prop_idx)
10756 struct frame *f;
10757 struct glyph *glyph;
10758 int *prop_idx;
10759 {
10760 Lisp_Object prop;
10761 int success_p;
10762 int charpos;
10763
10764 /* This function can be called asynchronously, which means we must
10765 exclude any possibility that Fget_text_property signals an
10766 error. */
10767 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10768 charpos = max (0, charpos);
10769
10770 /* Get the text property `menu-item' at pos. The value of that
10771 property is the start index of this item's properties in
10772 F->tool_bar_items. */
10773 prop = Fget_text_property (make_number (charpos),
10774 Qmenu_item, f->current_tool_bar_string);
10775 if (INTEGERP (prop))
10776 {
10777 *prop_idx = XINT (prop);
10778 success_p = 1;
10779 }
10780 else
10781 success_p = 0;
10782
10783 return success_p;
10784 }
10785
10786 \f
10787 /* Get information about the tool-bar item at position X/Y on frame F.
10788 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10789 the current matrix of the tool-bar window of F, or NULL if not
10790 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10791 item in F->tool_bar_items. Value is
10792
10793 -1 if X/Y is not on a tool-bar item
10794 0 if X/Y is on the same item that was highlighted before.
10795 1 otherwise. */
10796
10797 static int
10798 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10799 struct frame *f;
10800 int x, y;
10801 struct glyph **glyph;
10802 int *hpos, *vpos, *prop_idx;
10803 {
10804 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10805 struct window *w = XWINDOW (f->tool_bar_window);
10806 int area;
10807
10808 /* Find the glyph under X/Y. */
10809 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10810 if (*glyph == NULL)
10811 return -1;
10812
10813 /* Get the start of this tool-bar item's properties in
10814 f->tool_bar_items. */
10815 if (!tool_bar_item_info (f, *glyph, prop_idx))
10816 return -1;
10817
10818 /* Is mouse on the highlighted item? */
10819 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10820 && *vpos >= dpyinfo->mouse_face_beg_row
10821 && *vpos <= dpyinfo->mouse_face_end_row
10822 && (*vpos > dpyinfo->mouse_face_beg_row
10823 || *hpos >= dpyinfo->mouse_face_beg_col)
10824 && (*vpos < dpyinfo->mouse_face_end_row
10825 || *hpos < dpyinfo->mouse_face_end_col
10826 || dpyinfo->mouse_face_past_end))
10827 return 0;
10828
10829 return 1;
10830 }
10831
10832
10833 /* EXPORT:
10834 Handle mouse button event on the tool-bar of frame F, at
10835 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10836 0 for button release. MODIFIERS is event modifiers for button
10837 release. */
10838
10839 void
10840 handle_tool_bar_click (f, x, y, down_p, modifiers)
10841 struct frame *f;
10842 int x, y, down_p;
10843 unsigned int modifiers;
10844 {
10845 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10846 struct window *w = XWINDOW (f->tool_bar_window);
10847 int hpos, vpos, prop_idx;
10848 struct glyph *glyph;
10849 Lisp_Object enabled_p;
10850
10851 /* If not on the highlighted tool-bar item, return. */
10852 frame_to_window_pixel_xy (w, &x, &y);
10853 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10854 return;
10855
10856 /* If item is disabled, do nothing. */
10857 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10858 if (NILP (enabled_p))
10859 return;
10860
10861 if (down_p)
10862 {
10863 /* Show item in pressed state. */
10864 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10865 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10866 last_tool_bar_item = prop_idx;
10867 }
10868 else
10869 {
10870 Lisp_Object key, frame;
10871 struct input_event event;
10872 EVENT_INIT (event);
10873
10874 /* Show item in released state. */
10875 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10876 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10877
10878 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10879
10880 XSETFRAME (frame, f);
10881 event.kind = TOOL_BAR_EVENT;
10882 event.frame_or_window = frame;
10883 event.arg = frame;
10884 kbd_buffer_store_event (&event);
10885
10886 event.kind = TOOL_BAR_EVENT;
10887 event.frame_or_window = frame;
10888 event.arg = key;
10889 event.modifiers = modifiers;
10890 kbd_buffer_store_event (&event);
10891 last_tool_bar_item = -1;
10892 }
10893 }
10894
10895
10896 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10897 tool-bar window-relative coordinates X/Y. Called from
10898 note_mouse_highlight. */
10899
10900 static void
10901 note_tool_bar_highlight (f, x, y)
10902 struct frame *f;
10903 int x, y;
10904 {
10905 Lisp_Object window = f->tool_bar_window;
10906 struct window *w = XWINDOW (window);
10907 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10908 int hpos, vpos;
10909 struct glyph *glyph;
10910 struct glyph_row *row;
10911 int i;
10912 Lisp_Object enabled_p;
10913 int prop_idx;
10914 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10915 int mouse_down_p, rc;
10916
10917 /* Function note_mouse_highlight is called with negative x(y
10918 values when mouse moves outside of the frame. */
10919 if (x <= 0 || y <= 0)
10920 {
10921 clear_mouse_face (dpyinfo);
10922 return;
10923 }
10924
10925 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10926 if (rc < 0)
10927 {
10928 /* Not on tool-bar item. */
10929 clear_mouse_face (dpyinfo);
10930 return;
10931 }
10932 else if (rc == 0)
10933 /* On same tool-bar item as before. */
10934 goto set_help_echo;
10935
10936 clear_mouse_face (dpyinfo);
10937
10938 /* Mouse is down, but on different tool-bar item? */
10939 mouse_down_p = (dpyinfo->grabbed
10940 && f == last_mouse_frame
10941 && FRAME_LIVE_P (f));
10942 if (mouse_down_p
10943 && last_tool_bar_item != prop_idx)
10944 return;
10945
10946 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10947 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10948
10949 /* If tool-bar item is not enabled, don't highlight it. */
10950 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10951 if (!NILP (enabled_p))
10952 {
10953 /* Compute the x-position of the glyph. In front and past the
10954 image is a space. We include this in the highlighted area. */
10955 row = MATRIX_ROW (w->current_matrix, vpos);
10956 for (i = x = 0; i < hpos; ++i)
10957 x += row->glyphs[TEXT_AREA][i].pixel_width;
10958
10959 /* Record this as the current active region. */
10960 dpyinfo->mouse_face_beg_col = hpos;
10961 dpyinfo->mouse_face_beg_row = vpos;
10962 dpyinfo->mouse_face_beg_x = x;
10963 dpyinfo->mouse_face_beg_y = row->y;
10964 dpyinfo->mouse_face_past_end = 0;
10965
10966 dpyinfo->mouse_face_end_col = hpos + 1;
10967 dpyinfo->mouse_face_end_row = vpos;
10968 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10969 dpyinfo->mouse_face_end_y = row->y;
10970 dpyinfo->mouse_face_window = window;
10971 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10972
10973 /* Display it as active. */
10974 show_mouse_face (dpyinfo, draw);
10975 dpyinfo->mouse_face_image_state = draw;
10976 }
10977
10978 set_help_echo:
10979
10980 /* Set help_echo_string to a help string to display for this tool-bar item.
10981 XTread_socket does the rest. */
10982 help_echo_object = help_echo_window = Qnil;
10983 help_echo_pos = -1;
10984 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10985 if (NILP (help_echo_string))
10986 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10987 }
10988
10989 #endif /* HAVE_WINDOW_SYSTEM */
10990
10991
10992 \f
10993 /************************************************************************
10994 Horizontal scrolling
10995 ************************************************************************/
10996
10997 static int hscroll_window_tree P_ ((Lisp_Object));
10998 static int hscroll_windows P_ ((Lisp_Object));
10999
11000 /* For all leaf windows in the window tree rooted at WINDOW, set their
11001 hscroll value so that PT is (i) visible in the window, and (ii) so
11002 that it is not within a certain margin at the window's left and
11003 right border. Value is non-zero if any window's hscroll has been
11004 changed. */
11005
11006 static int
11007 hscroll_window_tree (window)
11008 Lisp_Object window;
11009 {
11010 int hscrolled_p = 0;
11011 int hscroll_relative_p = FLOATP (Vhscroll_step);
11012 int hscroll_step_abs = 0;
11013 double hscroll_step_rel = 0;
11014
11015 if (hscroll_relative_p)
11016 {
11017 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11018 if (hscroll_step_rel < 0)
11019 {
11020 hscroll_relative_p = 0;
11021 hscroll_step_abs = 0;
11022 }
11023 }
11024 else if (INTEGERP (Vhscroll_step))
11025 {
11026 hscroll_step_abs = XINT (Vhscroll_step);
11027 if (hscroll_step_abs < 0)
11028 hscroll_step_abs = 0;
11029 }
11030 else
11031 hscroll_step_abs = 0;
11032
11033 while (WINDOWP (window))
11034 {
11035 struct window *w = XWINDOW (window);
11036
11037 if (WINDOWP (w->hchild))
11038 hscrolled_p |= hscroll_window_tree (w->hchild);
11039 else if (WINDOWP (w->vchild))
11040 hscrolled_p |= hscroll_window_tree (w->vchild);
11041 else if (w->cursor.vpos >= 0)
11042 {
11043 int h_margin;
11044 int text_area_width;
11045 struct glyph_row *current_cursor_row
11046 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11047 struct glyph_row *desired_cursor_row
11048 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11049 struct glyph_row *cursor_row
11050 = (desired_cursor_row->enabled_p
11051 ? desired_cursor_row
11052 : current_cursor_row);
11053
11054 text_area_width = window_box_width (w, TEXT_AREA);
11055
11056 /* Scroll when cursor is inside this scroll margin. */
11057 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11058
11059 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11060 && ((XFASTINT (w->hscroll)
11061 && w->cursor.x <= h_margin)
11062 || (cursor_row->enabled_p
11063 && cursor_row->truncated_on_right_p
11064 && (w->cursor.x >= text_area_width - h_margin))))
11065 {
11066 struct it it;
11067 int hscroll;
11068 struct buffer *saved_current_buffer;
11069 int pt;
11070 int wanted_x;
11071
11072 /* Find point in a display of infinite width. */
11073 saved_current_buffer = current_buffer;
11074 current_buffer = XBUFFER (w->buffer);
11075
11076 if (w == XWINDOW (selected_window))
11077 pt = BUF_PT (current_buffer);
11078 else
11079 {
11080 pt = marker_position (w->pointm);
11081 pt = max (BEGV, pt);
11082 pt = min (ZV, pt);
11083 }
11084
11085 /* Move iterator to pt starting at cursor_row->start in
11086 a line with infinite width. */
11087 init_to_row_start (&it, w, cursor_row);
11088 it.last_visible_x = INFINITY;
11089 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11090 current_buffer = saved_current_buffer;
11091
11092 /* Position cursor in window. */
11093 if (!hscroll_relative_p && hscroll_step_abs == 0)
11094 hscroll = max (0, (it.current_x
11095 - (ITERATOR_AT_END_OF_LINE_P (&it)
11096 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11097 : (text_area_width / 2))))
11098 / FRAME_COLUMN_WIDTH (it.f);
11099 else if (w->cursor.x >= text_area_width - h_margin)
11100 {
11101 if (hscroll_relative_p)
11102 wanted_x = text_area_width * (1 - hscroll_step_rel)
11103 - h_margin;
11104 else
11105 wanted_x = text_area_width
11106 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11107 - h_margin;
11108 hscroll
11109 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11110 }
11111 else
11112 {
11113 if (hscroll_relative_p)
11114 wanted_x = text_area_width * hscroll_step_rel
11115 + h_margin;
11116 else
11117 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11118 + h_margin;
11119 hscroll
11120 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11121 }
11122 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11123
11124 /* Don't call Fset_window_hscroll if value hasn't
11125 changed because it will prevent redisplay
11126 optimizations. */
11127 if (XFASTINT (w->hscroll) != hscroll)
11128 {
11129 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11130 w->hscroll = make_number (hscroll);
11131 hscrolled_p = 1;
11132 }
11133 }
11134 }
11135
11136 window = w->next;
11137 }
11138
11139 /* Value is non-zero if hscroll of any leaf window has been changed. */
11140 return hscrolled_p;
11141 }
11142
11143
11144 /* Set hscroll so that cursor is visible and not inside horizontal
11145 scroll margins for all windows in the tree rooted at WINDOW. See
11146 also hscroll_window_tree above. Value is non-zero if any window's
11147 hscroll has been changed. If it has, desired matrices on the frame
11148 of WINDOW are cleared. */
11149
11150 static int
11151 hscroll_windows (window)
11152 Lisp_Object window;
11153 {
11154 int hscrolled_p = hscroll_window_tree (window);
11155 if (hscrolled_p)
11156 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11157 return hscrolled_p;
11158 }
11159
11160
11161 \f
11162 /************************************************************************
11163 Redisplay
11164 ************************************************************************/
11165
11166 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11167 to a non-zero value. This is sometimes handy to have in a debugger
11168 session. */
11169
11170 #if GLYPH_DEBUG
11171
11172 /* First and last unchanged row for try_window_id. */
11173
11174 int debug_first_unchanged_at_end_vpos;
11175 int debug_last_unchanged_at_beg_vpos;
11176
11177 /* Delta vpos and y. */
11178
11179 int debug_dvpos, debug_dy;
11180
11181 /* Delta in characters and bytes for try_window_id. */
11182
11183 int debug_delta, debug_delta_bytes;
11184
11185 /* Values of window_end_pos and window_end_vpos at the end of
11186 try_window_id. */
11187
11188 EMACS_INT debug_end_pos, debug_end_vpos;
11189
11190 /* Append a string to W->desired_matrix->method. FMT is a printf
11191 format string. A1...A9 are a supplement for a variable-length
11192 argument list. If trace_redisplay_p is non-zero also printf the
11193 resulting string to stderr. */
11194
11195 static void
11196 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11197 struct window *w;
11198 char *fmt;
11199 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11200 {
11201 char buffer[512];
11202 char *method = w->desired_matrix->method;
11203 int len = strlen (method);
11204 int size = sizeof w->desired_matrix->method;
11205 int remaining = size - len - 1;
11206
11207 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11208 if (len && remaining)
11209 {
11210 method[len] = '|';
11211 --remaining, ++len;
11212 }
11213
11214 strncpy (method + len, buffer, remaining);
11215
11216 if (trace_redisplay_p)
11217 fprintf (stderr, "%p (%s): %s\n",
11218 w,
11219 ((BUFFERP (w->buffer)
11220 && STRINGP (XBUFFER (w->buffer)->name))
11221 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11222 : "no buffer"),
11223 buffer);
11224 }
11225
11226 #endif /* GLYPH_DEBUG */
11227
11228
11229 /* Value is non-zero if all changes in window W, which displays
11230 current_buffer, are in the text between START and END. START is a
11231 buffer position, END is given as a distance from Z. Used in
11232 redisplay_internal for display optimization. */
11233
11234 static INLINE int
11235 text_outside_line_unchanged_p (w, start, end)
11236 struct window *w;
11237 int start, end;
11238 {
11239 int unchanged_p = 1;
11240
11241 /* If text or overlays have changed, see where. */
11242 if (XFASTINT (w->last_modified) < MODIFF
11243 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11244 {
11245 /* Gap in the line? */
11246 if (GPT < start || Z - GPT < end)
11247 unchanged_p = 0;
11248
11249 /* Changes start in front of the line, or end after it? */
11250 if (unchanged_p
11251 && (BEG_UNCHANGED < start - 1
11252 || END_UNCHANGED < end))
11253 unchanged_p = 0;
11254
11255 /* If selective display, can't optimize if changes start at the
11256 beginning of the line. */
11257 if (unchanged_p
11258 && INTEGERP (current_buffer->selective_display)
11259 && XINT (current_buffer->selective_display) > 0
11260 && (BEG_UNCHANGED < start || GPT <= start))
11261 unchanged_p = 0;
11262
11263 /* If there are overlays at the start or end of the line, these
11264 may have overlay strings with newlines in them. A change at
11265 START, for instance, may actually concern the display of such
11266 overlay strings as well, and they are displayed on different
11267 lines. So, quickly rule out this case. (For the future, it
11268 might be desirable to implement something more telling than
11269 just BEG/END_UNCHANGED.) */
11270 if (unchanged_p)
11271 {
11272 if (BEG + BEG_UNCHANGED == start
11273 && overlay_touches_p (start))
11274 unchanged_p = 0;
11275 if (END_UNCHANGED == end
11276 && overlay_touches_p (Z - end))
11277 unchanged_p = 0;
11278 }
11279
11280 /* Under bidi reordering, adding or deleting a character in the
11281 beginning of a paragraph, before the first strong directional
11282 character, can change the base direction of the paragraph (unless
11283 the buffer specifies a fixed paragraph direction), which will
11284 require to redisplay the whole paragraph. It might be worthwhile
11285 to find the paragraph limits and widen the range of redisplayed
11286 lines to that, but for now just give up this optimization. */
11287 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11288 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11289 unchanged_p = 0;
11290 }
11291
11292 return unchanged_p;
11293 }
11294
11295
11296 /* Do a frame update, taking possible shortcuts into account. This is
11297 the main external entry point for redisplay.
11298
11299 If the last redisplay displayed an echo area message and that message
11300 is no longer requested, we clear the echo area or bring back the
11301 mini-buffer if that is in use. */
11302
11303 void
11304 redisplay ()
11305 {
11306 redisplay_internal (0);
11307 }
11308
11309
11310 static Lisp_Object
11311 overlay_arrow_string_or_property (var)
11312 Lisp_Object var;
11313 {
11314 Lisp_Object val;
11315
11316 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11317 return val;
11318
11319 return Voverlay_arrow_string;
11320 }
11321
11322 /* Return 1 if there are any overlay-arrows in current_buffer. */
11323 static int
11324 overlay_arrow_in_current_buffer_p ()
11325 {
11326 Lisp_Object vlist;
11327
11328 for (vlist = Voverlay_arrow_variable_list;
11329 CONSP (vlist);
11330 vlist = XCDR (vlist))
11331 {
11332 Lisp_Object var = XCAR (vlist);
11333 Lisp_Object val;
11334
11335 if (!SYMBOLP (var))
11336 continue;
11337 val = find_symbol_value (var);
11338 if (MARKERP (val)
11339 && current_buffer == XMARKER (val)->buffer)
11340 return 1;
11341 }
11342 return 0;
11343 }
11344
11345
11346 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11347 has changed. */
11348
11349 static int
11350 overlay_arrows_changed_p ()
11351 {
11352 Lisp_Object vlist;
11353
11354 for (vlist = Voverlay_arrow_variable_list;
11355 CONSP (vlist);
11356 vlist = XCDR (vlist))
11357 {
11358 Lisp_Object var = XCAR (vlist);
11359 Lisp_Object val, pstr;
11360
11361 if (!SYMBOLP (var))
11362 continue;
11363 val = find_symbol_value (var);
11364 if (!MARKERP (val))
11365 continue;
11366 if (! EQ (COERCE_MARKER (val),
11367 Fget (var, Qlast_arrow_position))
11368 || ! (pstr = overlay_arrow_string_or_property (var),
11369 EQ (pstr, Fget (var, Qlast_arrow_string))))
11370 return 1;
11371 }
11372 return 0;
11373 }
11374
11375 /* Mark overlay arrows to be updated on next redisplay. */
11376
11377 static void
11378 update_overlay_arrows (up_to_date)
11379 int up_to_date;
11380 {
11381 Lisp_Object vlist;
11382
11383 for (vlist = Voverlay_arrow_variable_list;
11384 CONSP (vlist);
11385 vlist = XCDR (vlist))
11386 {
11387 Lisp_Object var = XCAR (vlist);
11388
11389 if (!SYMBOLP (var))
11390 continue;
11391
11392 if (up_to_date > 0)
11393 {
11394 Lisp_Object val = find_symbol_value (var);
11395 Fput (var, Qlast_arrow_position,
11396 COERCE_MARKER (val));
11397 Fput (var, Qlast_arrow_string,
11398 overlay_arrow_string_or_property (var));
11399 }
11400 else if (up_to_date < 0
11401 || !NILP (Fget (var, Qlast_arrow_position)))
11402 {
11403 Fput (var, Qlast_arrow_position, Qt);
11404 Fput (var, Qlast_arrow_string, Qt);
11405 }
11406 }
11407 }
11408
11409
11410 /* Return overlay arrow string to display at row.
11411 Return integer (bitmap number) for arrow bitmap in left fringe.
11412 Return nil if no overlay arrow. */
11413
11414 static Lisp_Object
11415 overlay_arrow_at_row (it, row)
11416 struct it *it;
11417 struct glyph_row *row;
11418 {
11419 Lisp_Object vlist;
11420
11421 for (vlist = Voverlay_arrow_variable_list;
11422 CONSP (vlist);
11423 vlist = XCDR (vlist))
11424 {
11425 Lisp_Object var = XCAR (vlist);
11426 Lisp_Object val;
11427
11428 if (!SYMBOLP (var))
11429 continue;
11430
11431 val = find_symbol_value (var);
11432
11433 if (MARKERP (val)
11434 && current_buffer == XMARKER (val)->buffer
11435 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11436 {
11437 if (FRAME_WINDOW_P (it->f)
11438 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11439 {
11440 #ifdef HAVE_WINDOW_SYSTEM
11441 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11442 {
11443 int fringe_bitmap;
11444 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11445 return make_number (fringe_bitmap);
11446 }
11447 #endif
11448 return make_number (-1); /* Use default arrow bitmap */
11449 }
11450 return overlay_arrow_string_or_property (var);
11451 }
11452 }
11453
11454 return Qnil;
11455 }
11456
11457 /* Return 1 if point moved out of or into a composition. Otherwise
11458 return 0. PREV_BUF and PREV_PT are the last point buffer and
11459 position. BUF and PT are the current point buffer and position. */
11460
11461 int
11462 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11463 struct buffer *prev_buf, *buf;
11464 int prev_pt, pt;
11465 {
11466 EMACS_INT start, end;
11467 Lisp_Object prop;
11468 Lisp_Object buffer;
11469
11470 XSETBUFFER (buffer, buf);
11471 /* Check a composition at the last point if point moved within the
11472 same buffer. */
11473 if (prev_buf == buf)
11474 {
11475 if (prev_pt == pt)
11476 /* Point didn't move. */
11477 return 0;
11478
11479 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11480 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11481 && COMPOSITION_VALID_P (start, end, prop)
11482 && start < prev_pt && end > prev_pt)
11483 /* The last point was within the composition. Return 1 iff
11484 point moved out of the composition. */
11485 return (pt <= start || pt >= end);
11486 }
11487
11488 /* Check a composition at the current point. */
11489 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11490 && find_composition (pt, -1, &start, &end, &prop, buffer)
11491 && COMPOSITION_VALID_P (start, end, prop)
11492 && start < pt && end > pt);
11493 }
11494
11495
11496 /* Reconsider the setting of B->clip_changed which is displayed
11497 in window W. */
11498
11499 static INLINE void
11500 reconsider_clip_changes (w, b)
11501 struct window *w;
11502 struct buffer *b;
11503 {
11504 if (b->clip_changed
11505 && !NILP (w->window_end_valid)
11506 && w->current_matrix->buffer == b
11507 && w->current_matrix->zv == BUF_ZV (b)
11508 && w->current_matrix->begv == BUF_BEGV (b))
11509 b->clip_changed = 0;
11510
11511 /* If display wasn't paused, and W is not a tool bar window, see if
11512 point has been moved into or out of a composition. In that case,
11513 we set b->clip_changed to 1 to force updating the screen. If
11514 b->clip_changed has already been set to 1, we can skip this
11515 check. */
11516 if (!b->clip_changed
11517 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11518 {
11519 int pt;
11520
11521 if (w == XWINDOW (selected_window))
11522 pt = BUF_PT (current_buffer);
11523 else
11524 pt = marker_position (w->pointm);
11525
11526 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11527 || pt != XINT (w->last_point))
11528 && check_point_in_composition (w->current_matrix->buffer,
11529 XINT (w->last_point),
11530 XBUFFER (w->buffer), pt))
11531 b->clip_changed = 1;
11532 }
11533 }
11534 \f
11535
11536 /* Select FRAME to forward the values of frame-local variables into C
11537 variables so that the redisplay routines can access those values
11538 directly. */
11539
11540 static void
11541 select_frame_for_redisplay (frame)
11542 Lisp_Object frame;
11543 {
11544 Lisp_Object tail, symbol, val;
11545 Lisp_Object old = selected_frame;
11546 struct Lisp_Symbol *sym;
11547
11548 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11549
11550 selected_frame = frame;
11551
11552 do
11553 {
11554 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11555 if (CONSP (XCAR (tail))
11556 && (symbol = XCAR (XCAR (tail)),
11557 SYMBOLP (symbol))
11558 && (sym = indirect_variable (XSYMBOL (symbol)),
11559 val = sym->value,
11560 (BUFFER_LOCAL_VALUEP (val)))
11561 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11562 /* Use find_symbol_value rather than Fsymbol_value
11563 to avoid an error if it is void. */
11564 find_symbol_value (symbol);
11565 } while (!EQ (frame, old) && (frame = old, 1));
11566 }
11567
11568
11569 #define STOP_POLLING \
11570 do { if (! polling_stopped_here) stop_polling (); \
11571 polling_stopped_here = 1; } while (0)
11572
11573 #define RESUME_POLLING \
11574 do { if (polling_stopped_here) start_polling (); \
11575 polling_stopped_here = 0; } while (0)
11576
11577
11578 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11579 response to any user action; therefore, we should preserve the echo
11580 area. (Actually, our caller does that job.) Perhaps in the future
11581 avoid recentering windows if it is not necessary; currently that
11582 causes some problems. */
11583
11584 static void
11585 redisplay_internal (preserve_echo_area)
11586 int preserve_echo_area;
11587 {
11588 struct window *w = XWINDOW (selected_window);
11589 struct frame *f;
11590 int pause;
11591 int must_finish = 0;
11592 struct text_pos tlbufpos, tlendpos;
11593 int number_of_visible_frames;
11594 int count, count1;
11595 struct frame *sf;
11596 int polling_stopped_here = 0;
11597 Lisp_Object old_frame = selected_frame;
11598
11599 /* Non-zero means redisplay has to consider all windows on all
11600 frames. Zero means, only selected_window is considered. */
11601 int consider_all_windows_p;
11602
11603 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11604
11605 /* No redisplay if running in batch mode or frame is not yet fully
11606 initialized, or redisplay is explicitly turned off by setting
11607 Vinhibit_redisplay. */
11608 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11609 || !NILP (Vinhibit_redisplay))
11610 return;
11611
11612 /* Don't examine these until after testing Vinhibit_redisplay.
11613 When Emacs is shutting down, perhaps because its connection to
11614 X has dropped, we should not look at them at all. */
11615 f = XFRAME (w->frame);
11616 sf = SELECTED_FRAME ();
11617
11618 if (!f->glyphs_initialized_p)
11619 return;
11620
11621 /* The flag redisplay_performed_directly_p is set by
11622 direct_output_for_insert when it already did the whole screen
11623 update necessary. */
11624 if (redisplay_performed_directly_p)
11625 {
11626 redisplay_performed_directly_p = 0;
11627 if (!hscroll_windows (selected_window))
11628 return;
11629 }
11630
11631 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11632 if (popup_activated ())
11633 return;
11634 #endif
11635
11636 /* I don't think this happens but let's be paranoid. */
11637 if (redisplaying_p)
11638 return;
11639
11640 /* Record a function that resets redisplaying_p to its old value
11641 when we leave this function. */
11642 count = SPECPDL_INDEX ();
11643 record_unwind_protect (unwind_redisplay,
11644 Fcons (make_number (redisplaying_p), selected_frame));
11645 ++redisplaying_p;
11646 specbind (Qinhibit_free_realized_faces, Qnil);
11647
11648 {
11649 Lisp_Object tail, frame;
11650
11651 FOR_EACH_FRAME (tail, frame)
11652 {
11653 struct frame *f = XFRAME (frame);
11654 f->already_hscrolled_p = 0;
11655 }
11656 }
11657
11658 retry:
11659 if (!EQ (old_frame, selected_frame)
11660 && FRAME_LIVE_P (XFRAME (old_frame)))
11661 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11662 selected_frame and selected_window to be temporarily out-of-sync so
11663 when we come back here via `goto retry', we need to resync because we
11664 may need to run Elisp code (via prepare_menu_bars). */
11665 select_frame_for_redisplay (old_frame);
11666
11667 pause = 0;
11668 reconsider_clip_changes (w, current_buffer);
11669 last_escape_glyph_frame = NULL;
11670 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11671
11672 /* If new fonts have been loaded that make a glyph matrix adjustment
11673 necessary, do it. */
11674 if (fonts_changed_p)
11675 {
11676 adjust_glyphs (NULL);
11677 ++windows_or_buffers_changed;
11678 fonts_changed_p = 0;
11679 }
11680
11681 /* If face_change_count is non-zero, init_iterator will free all
11682 realized faces, which includes the faces referenced from current
11683 matrices. So, we can't reuse current matrices in this case. */
11684 if (face_change_count)
11685 ++windows_or_buffers_changed;
11686
11687 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11688 && FRAME_TTY (sf)->previous_frame != sf)
11689 {
11690 /* Since frames on a single ASCII terminal share the same
11691 display area, displaying a different frame means redisplay
11692 the whole thing. */
11693 windows_or_buffers_changed++;
11694 SET_FRAME_GARBAGED (sf);
11695 #ifndef DOS_NT
11696 set_tty_color_mode (FRAME_TTY (sf), sf);
11697 #endif
11698 FRAME_TTY (sf)->previous_frame = sf;
11699 }
11700
11701 /* Set the visible flags for all frames. Do this before checking
11702 for resized or garbaged frames; they want to know if their frames
11703 are visible. See the comment in frame.h for
11704 FRAME_SAMPLE_VISIBILITY. */
11705 {
11706 Lisp_Object tail, frame;
11707
11708 number_of_visible_frames = 0;
11709
11710 FOR_EACH_FRAME (tail, frame)
11711 {
11712 struct frame *f = XFRAME (frame);
11713
11714 FRAME_SAMPLE_VISIBILITY (f);
11715 if (FRAME_VISIBLE_P (f))
11716 ++number_of_visible_frames;
11717 clear_desired_matrices (f);
11718 }
11719 }
11720
11721 /* Notice any pending interrupt request to change frame size. */
11722 do_pending_window_change (1);
11723
11724 /* Clear frames marked as garbaged. */
11725 if (frame_garbaged)
11726 clear_garbaged_frames ();
11727
11728 /* Build menubar and tool-bar items. */
11729 if (NILP (Vmemory_full))
11730 prepare_menu_bars ();
11731
11732 if (windows_or_buffers_changed)
11733 update_mode_lines++;
11734
11735 /* Detect case that we need to write or remove a star in the mode line. */
11736 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11737 {
11738 w->update_mode_line = Qt;
11739 if (buffer_shared > 1)
11740 update_mode_lines++;
11741 }
11742
11743 /* Avoid invocation of point motion hooks by `current_column' below. */
11744 count1 = SPECPDL_INDEX ();
11745 specbind (Qinhibit_point_motion_hooks, Qt);
11746
11747 /* If %c is in the mode line, update it if needed. */
11748 if (!NILP (w->column_number_displayed)
11749 /* This alternative quickly identifies a common case
11750 where no change is needed. */
11751 && !(PT == XFASTINT (w->last_point)
11752 && XFASTINT (w->last_modified) >= MODIFF
11753 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11754 && (XFASTINT (w->column_number_displayed)
11755 != (int) current_column ())) /* iftc */
11756 w->update_mode_line = Qt;
11757
11758 unbind_to (count1, Qnil);
11759
11760 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11761
11762 /* The variable buffer_shared is set in redisplay_window and
11763 indicates that we redisplay a buffer in different windows. See
11764 there. */
11765 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11766 || cursor_type_changed);
11767
11768 /* If specs for an arrow have changed, do thorough redisplay
11769 to ensure we remove any arrow that should no longer exist. */
11770 if (overlay_arrows_changed_p ())
11771 consider_all_windows_p = windows_or_buffers_changed = 1;
11772
11773 /* Normally the message* functions will have already displayed and
11774 updated the echo area, but the frame may have been trashed, or
11775 the update may have been preempted, so display the echo area
11776 again here. Checking message_cleared_p captures the case that
11777 the echo area should be cleared. */
11778 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11779 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11780 || (message_cleared_p
11781 && minibuf_level == 0
11782 /* If the mini-window is currently selected, this means the
11783 echo-area doesn't show through. */
11784 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11785 {
11786 int window_height_changed_p = echo_area_display (0);
11787 must_finish = 1;
11788
11789 /* If we don't display the current message, don't clear the
11790 message_cleared_p flag, because, if we did, we wouldn't clear
11791 the echo area in the next redisplay which doesn't preserve
11792 the echo area. */
11793 if (!display_last_displayed_message_p)
11794 message_cleared_p = 0;
11795
11796 if (fonts_changed_p)
11797 goto retry;
11798 else if (window_height_changed_p)
11799 {
11800 consider_all_windows_p = 1;
11801 ++update_mode_lines;
11802 ++windows_or_buffers_changed;
11803
11804 /* If window configuration was changed, frames may have been
11805 marked garbaged. Clear them or we will experience
11806 surprises wrt scrolling. */
11807 if (frame_garbaged)
11808 clear_garbaged_frames ();
11809 }
11810 }
11811 else if (EQ (selected_window, minibuf_window)
11812 && (current_buffer->clip_changed
11813 || XFASTINT (w->last_modified) < MODIFF
11814 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11815 && resize_mini_window (w, 0))
11816 {
11817 /* Resized active mini-window to fit the size of what it is
11818 showing if its contents might have changed. */
11819 must_finish = 1;
11820 /* FIXME: this causes all frames to be updated, which seems unnecessary
11821 since only the current frame needs to be considered. This function needs
11822 to be rewritten with two variables, consider_all_windows and
11823 consider_all_frames. */
11824 consider_all_windows_p = 1;
11825 ++windows_or_buffers_changed;
11826 ++update_mode_lines;
11827
11828 /* If window configuration was changed, frames may have been
11829 marked garbaged. Clear them or we will experience
11830 surprises wrt scrolling. */
11831 if (frame_garbaged)
11832 clear_garbaged_frames ();
11833 }
11834
11835
11836 /* If showing the region, and mark has changed, we must redisplay
11837 the whole window. The assignment to this_line_start_pos prevents
11838 the optimization directly below this if-statement. */
11839 if (((!NILP (Vtransient_mark_mode)
11840 && !NILP (XBUFFER (w->buffer)->mark_active))
11841 != !NILP (w->region_showing))
11842 || (!NILP (w->region_showing)
11843 && !EQ (w->region_showing,
11844 Fmarker_position (XBUFFER (w->buffer)->mark))))
11845 CHARPOS (this_line_start_pos) = 0;
11846
11847 /* Optimize the case that only the line containing the cursor in the
11848 selected window has changed. Variables starting with this_ are
11849 set in display_line and record information about the line
11850 containing the cursor. */
11851 tlbufpos = this_line_start_pos;
11852 tlendpos = this_line_end_pos;
11853 if (!consider_all_windows_p
11854 && CHARPOS (tlbufpos) > 0
11855 && NILP (w->update_mode_line)
11856 && !current_buffer->clip_changed
11857 && !current_buffer->prevent_redisplay_optimizations_p
11858 && FRAME_VISIBLE_P (XFRAME (w->frame))
11859 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11860 /* Make sure recorded data applies to current buffer, etc. */
11861 && this_line_buffer == current_buffer
11862 && current_buffer == XBUFFER (w->buffer)
11863 && NILP (w->force_start)
11864 && NILP (w->optional_new_start)
11865 /* Point must be on the line that we have info recorded about. */
11866 && PT >= CHARPOS (tlbufpos)
11867 && PT <= Z - CHARPOS (tlendpos)
11868 /* All text outside that line, including its final newline,
11869 must be unchanged. */
11870 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11871 CHARPOS (tlendpos)))
11872 {
11873 if (CHARPOS (tlbufpos) > BEGV
11874 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11875 && (CHARPOS (tlbufpos) == ZV
11876 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11877 /* Former continuation line has disappeared by becoming empty. */
11878 goto cancel;
11879 else if (XFASTINT (w->last_modified) < MODIFF
11880 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11881 || MINI_WINDOW_P (w))
11882 {
11883 /* We have to handle the case of continuation around a
11884 wide-column character (see the comment in indent.c around
11885 line 1340).
11886
11887 For instance, in the following case:
11888
11889 -------- Insert --------
11890 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11891 J_I_ ==> J_I_ `^^' are cursors.
11892 ^^ ^^
11893 -------- --------
11894
11895 As we have to redraw the line above, we cannot use this
11896 optimization. */
11897
11898 struct it it;
11899 int line_height_before = this_line_pixel_height;
11900
11901 /* Note that start_display will handle the case that the
11902 line starting at tlbufpos is a continuation line. */
11903 start_display (&it, w, tlbufpos);
11904
11905 /* Implementation note: It this still necessary? */
11906 if (it.current_x != this_line_start_x)
11907 goto cancel;
11908
11909 TRACE ((stderr, "trying display optimization 1\n"));
11910 w->cursor.vpos = -1;
11911 overlay_arrow_seen = 0;
11912 it.vpos = this_line_vpos;
11913 it.current_y = this_line_y;
11914 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11915 display_line (&it);
11916
11917 /* If line contains point, is not continued,
11918 and ends at same distance from eob as before, we win. */
11919 if (w->cursor.vpos >= 0
11920 /* Line is not continued, otherwise this_line_start_pos
11921 would have been set to 0 in display_line. */
11922 && CHARPOS (this_line_start_pos)
11923 /* Line ends as before. */
11924 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11925 /* Line has same height as before. Otherwise other lines
11926 would have to be shifted up or down. */
11927 && this_line_pixel_height == line_height_before)
11928 {
11929 /* If this is not the window's last line, we must adjust
11930 the charstarts of the lines below. */
11931 if (it.current_y < it.last_visible_y)
11932 {
11933 struct glyph_row *row
11934 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11935 int delta, delta_bytes;
11936
11937 /* We used to distinguish between two cases here,
11938 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11939 when the line ends in a newline or the end of the
11940 buffer's accessible portion. But both cases did
11941 the same, so they were collapsed. */
11942 delta = (Z
11943 - CHARPOS (tlendpos)
11944 - MATRIX_ROW_START_CHARPOS (row));
11945 delta_bytes = (Z_BYTE
11946 - BYTEPOS (tlendpos)
11947 - MATRIX_ROW_START_BYTEPOS (row));
11948
11949 increment_matrix_positions (w->current_matrix,
11950 this_line_vpos + 1,
11951 w->current_matrix->nrows,
11952 delta, delta_bytes);
11953 }
11954
11955 /* If this row displays text now but previously didn't,
11956 or vice versa, w->window_end_vpos may have to be
11957 adjusted. */
11958 if ((it.glyph_row - 1)->displays_text_p)
11959 {
11960 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11961 XSETINT (w->window_end_vpos, this_line_vpos);
11962 }
11963 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11964 && this_line_vpos > 0)
11965 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11966 w->window_end_valid = Qnil;
11967
11968 /* Update hint: No need to try to scroll in update_window. */
11969 w->desired_matrix->no_scrolling_p = 1;
11970
11971 #if GLYPH_DEBUG
11972 *w->desired_matrix->method = 0;
11973 debug_method_add (w, "optimization 1");
11974 #endif
11975 #ifdef HAVE_WINDOW_SYSTEM
11976 update_window_fringes (w, 0);
11977 #endif
11978 goto update;
11979 }
11980 else
11981 goto cancel;
11982 }
11983 else if (/* Cursor position hasn't changed. */
11984 PT == XFASTINT (w->last_point)
11985 /* Make sure the cursor was last displayed
11986 in this window. Otherwise we have to reposition it. */
11987 && 0 <= w->cursor.vpos
11988 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11989 {
11990 if (!must_finish)
11991 {
11992 do_pending_window_change (1);
11993
11994 /* We used to always goto end_of_redisplay here, but this
11995 isn't enough if we have a blinking cursor. */
11996 if (w->cursor_off_p == w->last_cursor_off_p)
11997 goto end_of_redisplay;
11998 }
11999 goto update;
12000 }
12001 /* If highlighting the region, or if the cursor is in the echo area,
12002 then we can't just move the cursor. */
12003 else if (! (!NILP (Vtransient_mark_mode)
12004 && !NILP (current_buffer->mark_active))
12005 && (EQ (selected_window, current_buffer->last_selected_window)
12006 || highlight_nonselected_windows)
12007 && NILP (w->region_showing)
12008 && NILP (Vshow_trailing_whitespace)
12009 && !cursor_in_echo_area)
12010 {
12011 struct it it;
12012 struct glyph_row *row;
12013
12014 /* Skip from tlbufpos to PT and see where it is. Note that
12015 PT may be in invisible text. If so, we will end at the
12016 next visible position. */
12017 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12018 NULL, DEFAULT_FACE_ID);
12019 it.current_x = this_line_start_x;
12020 it.current_y = this_line_y;
12021 it.vpos = this_line_vpos;
12022
12023 /* The call to move_it_to stops in front of PT, but
12024 moves over before-strings. */
12025 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12026
12027 if (it.vpos == this_line_vpos
12028 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12029 row->enabled_p))
12030 {
12031 xassert (this_line_vpos == it.vpos);
12032 xassert (this_line_y == it.current_y);
12033 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12034 #if GLYPH_DEBUG
12035 *w->desired_matrix->method = 0;
12036 debug_method_add (w, "optimization 3");
12037 #endif
12038 goto update;
12039 }
12040 else
12041 goto cancel;
12042 }
12043
12044 cancel:
12045 /* Text changed drastically or point moved off of line. */
12046 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12047 }
12048
12049 CHARPOS (this_line_start_pos) = 0;
12050 consider_all_windows_p |= buffer_shared > 1;
12051 ++clear_face_cache_count;
12052 #ifdef HAVE_WINDOW_SYSTEM
12053 ++clear_image_cache_count;
12054 #endif
12055
12056 /* Build desired matrices, and update the display. If
12057 consider_all_windows_p is non-zero, do it for all windows on all
12058 frames. Otherwise do it for selected_window, only. */
12059
12060 if (consider_all_windows_p)
12061 {
12062 Lisp_Object tail, frame;
12063
12064 FOR_EACH_FRAME (tail, frame)
12065 XFRAME (frame)->updated_p = 0;
12066
12067 /* Recompute # windows showing selected buffer. This will be
12068 incremented each time such a window is displayed. */
12069 buffer_shared = 0;
12070
12071 FOR_EACH_FRAME (tail, frame)
12072 {
12073 struct frame *f = XFRAME (frame);
12074
12075 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12076 {
12077 if (! EQ (frame, selected_frame))
12078 /* Select the frame, for the sake of frame-local
12079 variables. */
12080 select_frame_for_redisplay (frame);
12081
12082 /* Mark all the scroll bars to be removed; we'll redeem
12083 the ones we want when we redisplay their windows. */
12084 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12085 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12086
12087 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12088 redisplay_windows (FRAME_ROOT_WINDOW (f));
12089
12090 /* The X error handler may have deleted that frame. */
12091 if (!FRAME_LIVE_P (f))
12092 continue;
12093
12094 /* Any scroll bars which redisplay_windows should have
12095 nuked should now go away. */
12096 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12097 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12098
12099 /* If fonts changed, display again. */
12100 /* ??? rms: I suspect it is a mistake to jump all the way
12101 back to retry here. It should just retry this frame. */
12102 if (fonts_changed_p)
12103 goto retry;
12104
12105 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12106 {
12107 /* See if we have to hscroll. */
12108 if (!f->already_hscrolled_p)
12109 {
12110 f->already_hscrolled_p = 1;
12111 if (hscroll_windows (f->root_window))
12112 goto retry;
12113 }
12114
12115 /* Prevent various kinds of signals during display
12116 update. stdio is not robust about handling
12117 signals, which can cause an apparent I/O
12118 error. */
12119 if (interrupt_input)
12120 unrequest_sigio ();
12121 STOP_POLLING;
12122
12123 /* Update the display. */
12124 set_window_update_flags (XWINDOW (f->root_window), 1);
12125 pause |= update_frame (f, 0, 0);
12126 f->updated_p = 1;
12127 }
12128 }
12129 }
12130
12131 if (!EQ (old_frame, selected_frame)
12132 && FRAME_LIVE_P (XFRAME (old_frame)))
12133 /* We played a bit fast-and-loose above and allowed selected_frame
12134 and selected_window to be temporarily out-of-sync but let's make
12135 sure this stays contained. */
12136 select_frame_for_redisplay (old_frame);
12137 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12138
12139 if (!pause)
12140 {
12141 /* Do the mark_window_display_accurate after all windows have
12142 been redisplayed because this call resets flags in buffers
12143 which are needed for proper redisplay. */
12144 FOR_EACH_FRAME (tail, frame)
12145 {
12146 struct frame *f = XFRAME (frame);
12147 if (f->updated_p)
12148 {
12149 mark_window_display_accurate (f->root_window, 1);
12150 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12151 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12152 }
12153 }
12154 }
12155 }
12156 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12157 {
12158 Lisp_Object mini_window;
12159 struct frame *mini_frame;
12160
12161 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12162 /* Use list_of_error, not Qerror, so that
12163 we catch only errors and don't run the debugger. */
12164 internal_condition_case_1 (redisplay_window_1, selected_window,
12165 list_of_error,
12166 redisplay_window_error);
12167
12168 /* Compare desired and current matrices, perform output. */
12169
12170 update:
12171 /* If fonts changed, display again. */
12172 if (fonts_changed_p)
12173 goto retry;
12174
12175 /* Prevent various kinds of signals during display update.
12176 stdio is not robust about handling signals,
12177 which can cause an apparent I/O error. */
12178 if (interrupt_input)
12179 unrequest_sigio ();
12180 STOP_POLLING;
12181
12182 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12183 {
12184 if (hscroll_windows (selected_window))
12185 goto retry;
12186
12187 XWINDOW (selected_window)->must_be_updated_p = 1;
12188 pause = update_frame (sf, 0, 0);
12189 }
12190
12191 /* We may have called echo_area_display at the top of this
12192 function. If the echo area is on another frame, that may
12193 have put text on a frame other than the selected one, so the
12194 above call to update_frame would not have caught it. Catch
12195 it here. */
12196 mini_window = FRAME_MINIBUF_WINDOW (sf);
12197 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12198
12199 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12200 {
12201 XWINDOW (mini_window)->must_be_updated_p = 1;
12202 pause |= update_frame (mini_frame, 0, 0);
12203 if (!pause && hscroll_windows (mini_window))
12204 goto retry;
12205 }
12206 }
12207
12208 /* If display was paused because of pending input, make sure we do a
12209 thorough update the next time. */
12210 if (pause)
12211 {
12212 /* Prevent the optimization at the beginning of
12213 redisplay_internal that tries a single-line update of the
12214 line containing the cursor in the selected window. */
12215 CHARPOS (this_line_start_pos) = 0;
12216
12217 /* Let the overlay arrow be updated the next time. */
12218 update_overlay_arrows (0);
12219
12220 /* If we pause after scrolling, some rows in the current
12221 matrices of some windows are not valid. */
12222 if (!WINDOW_FULL_WIDTH_P (w)
12223 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12224 update_mode_lines = 1;
12225 }
12226 else
12227 {
12228 if (!consider_all_windows_p)
12229 {
12230 /* This has already been done above if
12231 consider_all_windows_p is set. */
12232 mark_window_display_accurate_1 (w, 1);
12233
12234 /* Say overlay arrows are up to date. */
12235 update_overlay_arrows (1);
12236
12237 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12238 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12239 }
12240
12241 update_mode_lines = 0;
12242 windows_or_buffers_changed = 0;
12243 cursor_type_changed = 0;
12244 }
12245
12246 /* Start SIGIO interrupts coming again. Having them off during the
12247 code above makes it less likely one will discard output, but not
12248 impossible, since there might be stuff in the system buffer here.
12249 But it is much hairier to try to do anything about that. */
12250 if (interrupt_input)
12251 request_sigio ();
12252 RESUME_POLLING;
12253
12254 /* If a frame has become visible which was not before, redisplay
12255 again, so that we display it. Expose events for such a frame
12256 (which it gets when becoming visible) don't call the parts of
12257 redisplay constructing glyphs, so simply exposing a frame won't
12258 display anything in this case. So, we have to display these
12259 frames here explicitly. */
12260 if (!pause)
12261 {
12262 Lisp_Object tail, frame;
12263 int new_count = 0;
12264
12265 FOR_EACH_FRAME (tail, frame)
12266 {
12267 int this_is_visible = 0;
12268
12269 if (XFRAME (frame)->visible)
12270 this_is_visible = 1;
12271 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12272 if (XFRAME (frame)->visible)
12273 this_is_visible = 1;
12274
12275 if (this_is_visible)
12276 new_count++;
12277 }
12278
12279 if (new_count != number_of_visible_frames)
12280 windows_or_buffers_changed++;
12281 }
12282
12283 /* Change frame size now if a change is pending. */
12284 do_pending_window_change (1);
12285
12286 /* If we just did a pending size change, or have additional
12287 visible frames, redisplay again. */
12288 if (windows_or_buffers_changed && !pause)
12289 goto retry;
12290
12291 /* Clear the face cache eventually. */
12292 if (consider_all_windows_p)
12293 {
12294 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12295 {
12296 clear_face_cache (0);
12297 clear_face_cache_count = 0;
12298 }
12299 #ifdef HAVE_WINDOW_SYSTEM
12300 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12301 {
12302 clear_image_caches (Qnil);
12303 clear_image_cache_count = 0;
12304 }
12305 #endif /* HAVE_WINDOW_SYSTEM */
12306 }
12307
12308 end_of_redisplay:
12309 unbind_to (count, Qnil);
12310 RESUME_POLLING;
12311 }
12312
12313
12314 /* Redisplay, but leave alone any recent echo area message unless
12315 another message has been requested in its place.
12316
12317 This is useful in situations where you need to redisplay but no
12318 user action has occurred, making it inappropriate for the message
12319 area to be cleared. See tracking_off and
12320 wait_reading_process_output for examples of these situations.
12321
12322 FROM_WHERE is an integer saying from where this function was
12323 called. This is useful for debugging. */
12324
12325 void
12326 redisplay_preserve_echo_area (from_where)
12327 int from_where;
12328 {
12329 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12330
12331 if (!NILP (echo_area_buffer[1]))
12332 {
12333 /* We have a previously displayed message, but no current
12334 message. Redisplay the previous message. */
12335 display_last_displayed_message_p = 1;
12336 redisplay_internal (1);
12337 display_last_displayed_message_p = 0;
12338 }
12339 else
12340 redisplay_internal (1);
12341
12342 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12343 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12344 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12345 }
12346
12347
12348 /* Function registered with record_unwind_protect in
12349 redisplay_internal. Reset redisplaying_p to the value it had
12350 before redisplay_internal was called, and clear
12351 prevent_freeing_realized_faces_p. It also selects the previously
12352 selected frame, unless it has been deleted (by an X connection
12353 failure during redisplay, for example). */
12354
12355 static Lisp_Object
12356 unwind_redisplay (val)
12357 Lisp_Object val;
12358 {
12359 Lisp_Object old_redisplaying_p, old_frame;
12360
12361 old_redisplaying_p = XCAR (val);
12362 redisplaying_p = XFASTINT (old_redisplaying_p);
12363 old_frame = XCDR (val);
12364 if (! EQ (old_frame, selected_frame)
12365 && FRAME_LIVE_P (XFRAME (old_frame)))
12366 select_frame_for_redisplay (old_frame);
12367 return Qnil;
12368 }
12369
12370
12371 /* Mark the display of window W as accurate or inaccurate. If
12372 ACCURATE_P is non-zero mark display of W as accurate. If
12373 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12374 redisplay_internal is called. */
12375
12376 static void
12377 mark_window_display_accurate_1 (w, accurate_p)
12378 struct window *w;
12379 int accurate_p;
12380 {
12381 if (BUFFERP (w->buffer))
12382 {
12383 struct buffer *b = XBUFFER (w->buffer);
12384
12385 w->last_modified
12386 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12387 w->last_overlay_modified
12388 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12389 w->last_had_star
12390 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12391
12392 if (accurate_p)
12393 {
12394 b->clip_changed = 0;
12395 b->prevent_redisplay_optimizations_p = 0;
12396
12397 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12398 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12399 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12400 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12401
12402 w->current_matrix->buffer = b;
12403 w->current_matrix->begv = BUF_BEGV (b);
12404 w->current_matrix->zv = BUF_ZV (b);
12405
12406 w->last_cursor = w->cursor;
12407 w->last_cursor_off_p = w->cursor_off_p;
12408
12409 if (w == XWINDOW (selected_window))
12410 w->last_point = make_number (BUF_PT (b));
12411 else
12412 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12413 }
12414 }
12415
12416 if (accurate_p)
12417 {
12418 w->window_end_valid = w->buffer;
12419 w->update_mode_line = Qnil;
12420 }
12421 }
12422
12423
12424 /* Mark the display of windows in the window tree rooted at WINDOW as
12425 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12426 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12427 be redisplayed the next time redisplay_internal is called. */
12428
12429 void
12430 mark_window_display_accurate (window, accurate_p)
12431 Lisp_Object window;
12432 int accurate_p;
12433 {
12434 struct window *w;
12435
12436 for (; !NILP (window); window = w->next)
12437 {
12438 w = XWINDOW (window);
12439 mark_window_display_accurate_1 (w, accurate_p);
12440
12441 if (!NILP (w->vchild))
12442 mark_window_display_accurate (w->vchild, accurate_p);
12443 if (!NILP (w->hchild))
12444 mark_window_display_accurate (w->hchild, accurate_p);
12445 }
12446
12447 if (accurate_p)
12448 {
12449 update_overlay_arrows (1);
12450 }
12451 else
12452 {
12453 /* Force a thorough redisplay the next time by setting
12454 last_arrow_position and last_arrow_string to t, which is
12455 unequal to any useful value of Voverlay_arrow_... */
12456 update_overlay_arrows (-1);
12457 }
12458 }
12459
12460
12461 /* Return value in display table DP (Lisp_Char_Table *) for character
12462 C. Since a display table doesn't have any parent, we don't have to
12463 follow parent. Do not call this function directly but use the
12464 macro DISP_CHAR_VECTOR. */
12465
12466 Lisp_Object
12467 disp_char_vector (dp, c)
12468 struct Lisp_Char_Table *dp;
12469 int c;
12470 {
12471 Lisp_Object val;
12472
12473 if (ASCII_CHAR_P (c))
12474 {
12475 val = dp->ascii;
12476 if (SUB_CHAR_TABLE_P (val))
12477 val = XSUB_CHAR_TABLE (val)->contents[c];
12478 }
12479 else
12480 {
12481 Lisp_Object table;
12482
12483 XSETCHAR_TABLE (table, dp);
12484 val = char_table_ref (table, c);
12485 }
12486 if (NILP (val))
12487 val = dp->defalt;
12488 return val;
12489 }
12490
12491
12492 \f
12493 /***********************************************************************
12494 Window Redisplay
12495 ***********************************************************************/
12496
12497 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12498
12499 static void
12500 redisplay_windows (window)
12501 Lisp_Object window;
12502 {
12503 while (!NILP (window))
12504 {
12505 struct window *w = XWINDOW (window);
12506
12507 if (!NILP (w->hchild))
12508 redisplay_windows (w->hchild);
12509 else if (!NILP (w->vchild))
12510 redisplay_windows (w->vchild);
12511 else if (!NILP (w->buffer))
12512 {
12513 displayed_buffer = XBUFFER (w->buffer);
12514 /* Use list_of_error, not Qerror, so that
12515 we catch only errors and don't run the debugger. */
12516 internal_condition_case_1 (redisplay_window_0, window,
12517 list_of_error,
12518 redisplay_window_error);
12519 }
12520
12521 window = w->next;
12522 }
12523 }
12524
12525 static Lisp_Object
12526 redisplay_window_error ()
12527 {
12528 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12529 return Qnil;
12530 }
12531
12532 static Lisp_Object
12533 redisplay_window_0 (window)
12534 Lisp_Object window;
12535 {
12536 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12537 redisplay_window (window, 0);
12538 return Qnil;
12539 }
12540
12541 static Lisp_Object
12542 redisplay_window_1 (window)
12543 Lisp_Object window;
12544 {
12545 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12546 redisplay_window (window, 1);
12547 return Qnil;
12548 }
12549 \f
12550
12551 /* Increment GLYPH until it reaches END or CONDITION fails while
12552 adding (GLYPH)->pixel_width to X. */
12553
12554 #define SKIP_GLYPHS(glyph, end, x, condition) \
12555 do \
12556 { \
12557 (x) += (glyph)->pixel_width; \
12558 ++(glyph); \
12559 } \
12560 while ((glyph) < (end) && (condition))
12561
12562
12563 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12564 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12565 which positions recorded in ROW differ from current buffer
12566 positions.
12567
12568 Return 0 if cursor is not on this row, 1 otherwise. */
12569
12570 int
12571 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12572 struct window *w;
12573 struct glyph_row *row;
12574 struct glyph_matrix *matrix;
12575 int delta, delta_bytes, dy, dvpos;
12576 {
12577 struct glyph *glyph = row->glyphs[TEXT_AREA];
12578 struct glyph *end = glyph + row->used[TEXT_AREA];
12579 struct glyph *cursor = NULL;
12580 /* The last known character position in row. */
12581 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12582 int x = row->x;
12583 int cursor_x = x;
12584 EMACS_INT pt_old = PT - delta;
12585 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12586 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12587 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12588 /* Non-zero means we've found a match for cursor position, but that
12589 glyph has the avoid_cursor_p flag set. */
12590 int match_with_avoid_cursor = 0;
12591 /* Non-zero means we've seen at least one glyph that came from a
12592 display string. */
12593 int string_seen = 0;
12594 /* Largest buffer position seen so far during scan of glyph row. */
12595 EMACS_INT bpos_max = last_pos;
12596 /* Last buffer position covered by an overlay string with an integer
12597 `cursor' property. */
12598 EMACS_INT bpos_covered = 0;
12599
12600 /* Skip over glyphs not having an object at the start and the end of
12601 the row. These are special glyphs like truncation marks on
12602 terminal frames. */
12603 if (row->displays_text_p)
12604 {
12605 if (!row->reversed_p)
12606 {
12607 while (glyph < end
12608 && INTEGERP (glyph->object)
12609 && glyph->charpos < 0)
12610 {
12611 x += glyph->pixel_width;
12612 ++glyph;
12613 }
12614 while (end > glyph
12615 && INTEGERP ((end - 1)->object)
12616 /* CHARPOS is zero for blanks inserted by
12617 extend_face_to_end_of_line. */
12618 && (end - 1)->charpos <= 0)
12619 --end;
12620 glyph_before = glyph - 1;
12621 glyph_after = end;
12622 }
12623 else
12624 {
12625 struct glyph *g;
12626
12627 /* If the glyph row is reversed, we need to process it from back
12628 to front, so swap the edge pointers. */
12629 end = glyph - 1;
12630 glyph += row->used[TEXT_AREA] - 1;
12631 /* Reverse the known positions in the row. */
12632 last_pos = pos_after = MATRIX_ROW_START_CHARPOS (row) + delta;
12633 pos_before = MATRIX_ROW_END_CHARPOS (row) + delta;
12634
12635 while (glyph > end + 1
12636 && INTEGERP (glyph->object)
12637 && glyph->charpos < 0)
12638 {
12639 --glyph;
12640 x -= glyph->pixel_width;
12641 }
12642 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12643 --glyph;
12644 /* By default, put the cursor on the rightmost glyph. */
12645 for (g = end + 1; g < glyph; g++)
12646 x += g->pixel_width;
12647 cursor_x = x;
12648 while (end < glyph
12649 && INTEGERP ((end + 1)->object)
12650 && (end + 1)->charpos <= 0)
12651 ++end;
12652 glyph_before = glyph + 1;
12653 glyph_after = end;
12654 }
12655 }
12656 else if (row->reversed_p)
12657 {
12658 /* In R2L rows that don't display text, put the cursor on the
12659 rightmost glyph. Case in point: an empty last line that is
12660 part of an R2L paragraph. */
12661 cursor = end - 1;
12662 x = -1; /* will be computed below, at lable compute_x */
12663 }
12664
12665 /* Step 1: Try to find the glyph whose character position
12666 corresponds to point. If that's not possible, find 2 glyphs
12667 whose character positions are the closest to point, one before
12668 point, the other after it. */
12669 if (!row->reversed_p)
12670 while (/* not marched to end of glyph row */
12671 glyph < end
12672 /* glyph was not inserted by redisplay for internal purposes */
12673 && !INTEGERP (glyph->object))
12674 {
12675 if (BUFFERP (glyph->object))
12676 {
12677 EMACS_INT dpos = glyph->charpos - pt_old;
12678
12679 if (glyph->charpos > bpos_max)
12680 bpos_max = glyph->charpos;
12681 if (!glyph->avoid_cursor_p)
12682 {
12683 /* If we hit point, we've found the glyph on which to
12684 display the cursor. */
12685 if (dpos == 0)
12686 {
12687 match_with_avoid_cursor = 0;
12688 break;
12689 }
12690 /* See if we've found a better approximation to
12691 POS_BEFORE or to POS_AFTER. Note that we want the
12692 first (leftmost) glyph of all those that are the
12693 closest from below, and the last (rightmost) of all
12694 those from above. */
12695 if (0 > dpos && dpos > pos_before - pt_old)
12696 {
12697 pos_before = glyph->charpos;
12698 glyph_before = glyph;
12699 }
12700 else if (0 < dpos && dpos <= pos_after - pt_old)
12701 {
12702 pos_after = glyph->charpos;
12703 glyph_after = glyph;
12704 }
12705 }
12706 else if (dpos == 0)
12707 match_with_avoid_cursor = 1;
12708 }
12709 else if (STRINGP (glyph->object))
12710 {
12711 Lisp_Object chprop;
12712 int glyph_pos = glyph->charpos;
12713
12714 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12715 glyph->object);
12716 if (INTEGERP (chprop))
12717 {
12718 bpos_covered = bpos_max + XINT (chprop);
12719 /* If the `cursor' property covers buffer positions up
12720 to and including point, we should display cursor on
12721 this glyph. */
12722 /* Implementation note: bpos_max == pt_old when, e.g.,
12723 we are in an empty line, where bpos_max is set to
12724 MATRIX_ROW_START_CHARPOS, see above. */
12725 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12726 {
12727 cursor = glyph;
12728 break;
12729 }
12730 }
12731
12732 string_seen = 1;
12733 }
12734 x += glyph->pixel_width;
12735 ++glyph;
12736 }
12737 else if (glyph > end) /* row is reversed */
12738 while (!INTEGERP (glyph->object))
12739 {
12740 if (BUFFERP (glyph->object))
12741 {
12742 EMACS_INT dpos = glyph->charpos - pt_old;
12743
12744 if (glyph->charpos > bpos_max)
12745 bpos_max = glyph->charpos;
12746 if (!glyph->avoid_cursor_p)
12747 {
12748 if (dpos == 0)
12749 {
12750 match_with_avoid_cursor = 0;
12751 break;
12752 }
12753 if (0 > dpos && dpos > pos_before - pt_old)
12754 {
12755 pos_before = glyph->charpos;
12756 glyph_before = glyph;
12757 }
12758 else if (0 < dpos && dpos <= pos_after - pt_old)
12759 {
12760 pos_after = glyph->charpos;
12761 glyph_after = glyph;
12762 }
12763 }
12764 else if (dpos == 0)
12765 match_with_avoid_cursor = 1;
12766 }
12767 else if (STRINGP (glyph->object))
12768 {
12769 Lisp_Object chprop;
12770 int glyph_pos = glyph->charpos;
12771
12772 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12773 glyph->object);
12774 if (INTEGERP (chprop))
12775 {
12776 bpos_covered = bpos_max + XINT (chprop);
12777 /* If the `cursor' property covers buffer positions up
12778 to and including point, we should display cursor on
12779 this glyph. */
12780 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12781 {
12782 cursor = glyph;
12783 break;
12784 }
12785 }
12786 string_seen = 1;
12787 }
12788 --glyph;
12789 if (glyph == end)
12790 break;
12791 x -= glyph->pixel_width;
12792 }
12793
12794 /* Step 2: If we didn't find an exact match for point, we need to
12795 look for a proper place to put the cursor among glyphs between
12796 GLYPH_BEFORE and GLYPH_AFTER. */
12797 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12798 && bpos_covered < pt_old)
12799 {
12800 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12801 {
12802 EMACS_INT ellipsis_pos;
12803
12804 /* Scan back over the ellipsis glyphs. */
12805 if (!row->reversed_p)
12806 {
12807 ellipsis_pos = (glyph - 1)->charpos;
12808 while (glyph > row->glyphs[TEXT_AREA]
12809 && (glyph - 1)->charpos == ellipsis_pos)
12810 glyph--, x -= glyph->pixel_width;
12811 /* That loop always goes one position too far, including
12812 the glyph before the ellipsis. So scan forward over
12813 that one. */
12814 x += glyph->pixel_width;
12815 glyph++;
12816 }
12817 else /* row is reversed */
12818 {
12819 ellipsis_pos = (glyph + 1)->charpos;
12820 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12821 && (glyph + 1)->charpos == ellipsis_pos)
12822 glyph++, x += glyph->pixel_width;
12823 x -= glyph->pixel_width;
12824 glyph--;
12825 }
12826 }
12827 else if (match_with_avoid_cursor
12828 /* zero-width characters produce no glyphs */
12829 || eabs (glyph_after - glyph_before) == 1)
12830 {
12831 cursor = glyph_after;
12832 x = -1;
12833 }
12834 else if (string_seen)
12835 {
12836 int incr = row->reversed_p ? -1 : +1;
12837
12838 /* Need to find the glyph that came out of a string which is
12839 present at point. That glyph is somewhere between
12840 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12841 positioned between POS_BEFORE and POS_AFTER in the
12842 buffer. */
12843 struct glyph *stop = glyph_after;
12844 EMACS_INT pos = pos_before;
12845
12846 x = -1;
12847 for (glyph = glyph_before + incr;
12848 row->reversed_p ? glyph > stop : glyph < stop; )
12849 {
12850
12851 /* Any glyphs that come from the buffer are here because
12852 of bidi reordering. Skip them, and only pay
12853 attention to glyphs that came from some string. */
12854 if (STRINGP (glyph->object))
12855 {
12856 Lisp_Object str;
12857 EMACS_INT tem;
12858
12859 str = glyph->object;
12860 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12861 if (pos <= tem)
12862 {
12863 /* If the string from which this glyph came is
12864 found in the buffer at point, then we've
12865 found the glyph we've been looking for. */
12866 if (tem == pt_old)
12867 {
12868 /* The glyphs from this string could have
12869 been reordered. Find the one with the
12870 smallest string position. Or there could
12871 be a character in the string with the
12872 `cursor' property, which means display
12873 cursor on that character's glyph. */
12874 int strpos = glyph->charpos;
12875
12876 cursor = glyph;
12877 for (glyph += incr;
12878 EQ (glyph->object, str);
12879 glyph += incr)
12880 {
12881 Lisp_Object cprop;
12882 int gpos = glyph->charpos;
12883
12884 cprop = Fget_char_property (make_number (gpos),
12885 Qcursor,
12886 glyph->object);
12887 if (!NILP (cprop))
12888 {
12889 cursor = glyph;
12890 break;
12891 }
12892 if (glyph->charpos < strpos)
12893 {
12894 strpos = glyph->charpos;
12895 cursor = glyph;
12896 }
12897 }
12898
12899 goto compute_x;
12900 }
12901 pos = tem + 1; /* don't find previous instances */
12902 }
12903 /* This string is not what we want; skip all of the
12904 glyphs that came from it. */
12905 do
12906 glyph += incr;
12907 while ((row->reversed_p ? glyph > stop : glyph < stop)
12908 && EQ (glyph->object, str));
12909 }
12910 else
12911 glyph += incr;
12912 }
12913
12914 /* If we reached the end of the line, and END was from a string,
12915 the cursor is not on this line. */
12916 if (glyph == end
12917 && STRINGP ((glyph - incr)->object)
12918 && row->continued_p)
12919 return 0;
12920 }
12921 }
12922
12923 compute_x:
12924 if (cursor != NULL)
12925 glyph = cursor;
12926 if (x < 0)
12927 {
12928 struct glyph *g;
12929
12930 /* Need to compute x that corresponds to GLYPH. */
12931 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12932 {
12933 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12934 abort ();
12935 x += g->pixel_width;
12936 }
12937 }
12938
12939 /* ROW could be part of a continued line, which might have other
12940 rows whose start and end charpos occlude point. Only set
12941 w->cursor if we found a better approximation to the cursor
12942 position than we have from previously examined rows. */
12943 if (w->cursor.vpos >= 0
12944 /* Make sure cursor.vpos specifies a row whose start and end
12945 charpos occlude point. This is because some callers of this
12946 function leave cursor.vpos at the row where the cursor was
12947 displayed during the last redisplay cycle. */
12948 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12949 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12950 {
12951 struct glyph *g1 =
12952 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12953
12954 /* Keep the candidate whose buffer position is the closest to
12955 point. */
12956 if (BUFFERP (g1->object)
12957 && (g1->charpos == pt_old /* an exact match always wins */
12958 || (BUFFERP (glyph->object)
12959 && eabs (g1->charpos - pt_old)
12960 < eabs (glyph->charpos - pt_old))))
12961 return 0;
12962 /* If this candidate gives an exact match, use that. */
12963 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12964 /* Otherwise, keep the candidate that comes from a row
12965 spanning less buffer positions. This may win when one or
12966 both candidate positions are on glyphs that came from
12967 display strings, for which we cannot compare buffer
12968 positions. */
12969 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12970 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12971 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12972 return 0;
12973 }
12974 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12975 w->cursor.x = x;
12976 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12977 w->cursor.y = row->y + dy;
12978
12979 if (w == XWINDOW (selected_window))
12980 {
12981 if (!row->continued_p
12982 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12983 && row->x == 0)
12984 {
12985 this_line_buffer = XBUFFER (w->buffer);
12986
12987 CHARPOS (this_line_start_pos)
12988 = MATRIX_ROW_START_CHARPOS (row) + delta;
12989 BYTEPOS (this_line_start_pos)
12990 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12991
12992 CHARPOS (this_line_end_pos)
12993 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12994 BYTEPOS (this_line_end_pos)
12995 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12996
12997 this_line_y = w->cursor.y;
12998 this_line_pixel_height = row->height;
12999 this_line_vpos = w->cursor.vpos;
13000 this_line_start_x = row->x;
13001 }
13002 else
13003 CHARPOS (this_line_start_pos) = 0;
13004 }
13005
13006 return 1;
13007 }
13008
13009
13010 /* Run window scroll functions, if any, for WINDOW with new window
13011 start STARTP. Sets the window start of WINDOW to that position.
13012
13013 We assume that the window's buffer is really current. */
13014
13015 static INLINE struct text_pos
13016 run_window_scroll_functions (window, startp)
13017 Lisp_Object window;
13018 struct text_pos startp;
13019 {
13020 struct window *w = XWINDOW (window);
13021 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13022
13023 if (current_buffer != XBUFFER (w->buffer))
13024 abort ();
13025
13026 if (!NILP (Vwindow_scroll_functions))
13027 {
13028 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13029 make_number (CHARPOS (startp)));
13030 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13031 /* In case the hook functions switch buffers. */
13032 if (current_buffer != XBUFFER (w->buffer))
13033 set_buffer_internal_1 (XBUFFER (w->buffer));
13034 }
13035
13036 return startp;
13037 }
13038
13039
13040 /* Make sure the line containing the cursor is fully visible.
13041 A value of 1 means there is nothing to be done.
13042 (Either the line is fully visible, or it cannot be made so,
13043 or we cannot tell.)
13044
13045 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13046 is higher than window.
13047
13048 A value of 0 means the caller should do scrolling
13049 as if point had gone off the screen. */
13050
13051 static int
13052 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
13053 struct window *w;
13054 int force_p;
13055 int current_matrix_p;
13056 {
13057 struct glyph_matrix *matrix;
13058 struct glyph_row *row;
13059 int window_height;
13060
13061 if (!make_cursor_line_fully_visible_p)
13062 return 1;
13063
13064 /* It's not always possible to find the cursor, e.g, when a window
13065 is full of overlay strings. Don't do anything in that case. */
13066 if (w->cursor.vpos < 0)
13067 return 1;
13068
13069 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13070 row = MATRIX_ROW (matrix, w->cursor.vpos);
13071
13072 /* If the cursor row is not partially visible, there's nothing to do. */
13073 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13074 return 1;
13075
13076 /* If the row the cursor is in is taller than the window's height,
13077 it's not clear what to do, so do nothing. */
13078 window_height = window_box_height (w);
13079 if (row->height >= window_height)
13080 {
13081 if (!force_p || MINI_WINDOW_P (w)
13082 || w->vscroll || w->cursor.vpos == 0)
13083 return 1;
13084 }
13085 return 0;
13086 }
13087
13088
13089 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13090 non-zero means only WINDOW is redisplayed in redisplay_internal.
13091 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13092 in redisplay_window to bring a partially visible line into view in
13093 the case that only the cursor has moved.
13094
13095 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13096 last screen line's vertical height extends past the end of the screen.
13097
13098 Value is
13099
13100 1 if scrolling succeeded
13101
13102 0 if scrolling didn't find point.
13103
13104 -1 if new fonts have been loaded so that we must interrupt
13105 redisplay, adjust glyph matrices, and try again. */
13106
13107 enum
13108 {
13109 SCROLLING_SUCCESS,
13110 SCROLLING_FAILED,
13111 SCROLLING_NEED_LARGER_MATRICES
13112 };
13113
13114 static int
13115 try_scrolling (window, just_this_one_p, scroll_conservatively,
13116 scroll_step, temp_scroll_step, last_line_misfit)
13117 Lisp_Object window;
13118 int just_this_one_p;
13119 EMACS_INT scroll_conservatively, scroll_step;
13120 int temp_scroll_step;
13121 int last_line_misfit;
13122 {
13123 struct window *w = XWINDOW (window);
13124 struct frame *f = XFRAME (w->frame);
13125 struct text_pos pos, startp;
13126 struct it it;
13127 int this_scroll_margin, scroll_max, rc, height;
13128 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13129 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13130 Lisp_Object aggressive;
13131 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13132
13133 #if GLYPH_DEBUG
13134 debug_method_add (w, "try_scrolling");
13135 #endif
13136
13137 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13138
13139 /* Compute scroll margin height in pixels. We scroll when point is
13140 within this distance from the top or bottom of the window. */
13141 if (scroll_margin > 0)
13142 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13143 * FRAME_LINE_HEIGHT (f);
13144 else
13145 this_scroll_margin = 0;
13146
13147 /* Force scroll_conservatively to have a reasonable value, to avoid
13148 overflow while computing how much to scroll. Note that the user
13149 can supply scroll-conservatively equal to `most-positive-fixnum',
13150 which can be larger than INT_MAX. */
13151 if (scroll_conservatively > scroll_limit)
13152 {
13153 scroll_conservatively = scroll_limit;
13154 scroll_max = INT_MAX;
13155 }
13156 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13157 /* Compute how much we should try to scroll maximally to bring
13158 point into view. */
13159 scroll_max = (max (scroll_step,
13160 max (scroll_conservatively, temp_scroll_step))
13161 * FRAME_LINE_HEIGHT (f));
13162 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13163 || NUMBERP (current_buffer->scroll_up_aggressively))
13164 /* We're trying to scroll because of aggressive scrolling but no
13165 scroll_step is set. Choose an arbitrary one. */
13166 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13167 else
13168 scroll_max = 0;
13169
13170 too_near_end:
13171
13172 /* Decide whether to scroll down. */
13173 if (PT > CHARPOS (startp))
13174 {
13175 int scroll_margin_y;
13176
13177 /* Compute the pixel ypos of the scroll margin, then move it to
13178 either that ypos or PT, whichever comes first. */
13179 start_display (&it, w, startp);
13180 scroll_margin_y = it.last_visible_y - this_scroll_margin
13181 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13182 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13183 (MOVE_TO_POS | MOVE_TO_Y));
13184
13185 if (PT > CHARPOS (it.current.pos))
13186 {
13187 int y0 = line_bottom_y (&it);
13188
13189 /* Compute the distance from the scroll margin to PT
13190 (including the height of the cursor line). Moving the
13191 iterator unconditionally to PT can be slow if PT is far
13192 away, so stop 10 lines past the window bottom (is there a
13193 way to do the right thing quickly?). */
13194 move_it_to (&it, PT, -1,
13195 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
13196 -1, MOVE_TO_POS | MOVE_TO_Y);
13197 dy = line_bottom_y (&it) - y0;
13198
13199 if (dy > scroll_max)
13200 return SCROLLING_FAILED;
13201
13202 scroll_down_p = 1;
13203 }
13204 }
13205
13206 if (scroll_down_p)
13207 {
13208 /* Point is in or below the bottom scroll margin, so move the
13209 window start down. If scrolling conservatively, move it just
13210 enough down to make point visible. If scroll_step is set,
13211 move it down by scroll_step. */
13212 if (scroll_conservatively)
13213 amount_to_scroll
13214 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13215 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13216 else if (scroll_step || temp_scroll_step)
13217 amount_to_scroll = scroll_max;
13218 else
13219 {
13220 aggressive = current_buffer->scroll_up_aggressively;
13221 height = WINDOW_BOX_TEXT_HEIGHT (w);
13222 if (NUMBERP (aggressive))
13223 {
13224 double float_amount = XFLOATINT (aggressive) * height;
13225 amount_to_scroll = float_amount;
13226 if (amount_to_scroll == 0 && float_amount > 0)
13227 amount_to_scroll = 1;
13228 }
13229 }
13230
13231 if (amount_to_scroll <= 0)
13232 return SCROLLING_FAILED;
13233
13234 start_display (&it, w, startp);
13235 move_it_vertically (&it, amount_to_scroll);
13236
13237 /* If STARTP is unchanged, move it down another screen line. */
13238 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13239 move_it_by_lines (&it, 1, 1);
13240 startp = it.current.pos;
13241 }
13242 else
13243 {
13244 struct text_pos scroll_margin_pos = startp;
13245
13246 /* See if point is inside the scroll margin at the top of the
13247 window. */
13248 if (this_scroll_margin)
13249 {
13250 start_display (&it, w, startp);
13251 move_it_vertically (&it, this_scroll_margin);
13252 scroll_margin_pos = it.current.pos;
13253 }
13254
13255 if (PT < CHARPOS (scroll_margin_pos))
13256 {
13257 /* Point is in the scroll margin at the top of the window or
13258 above what is displayed in the window. */
13259 int y0;
13260
13261 /* Compute the vertical distance from PT to the scroll
13262 margin position. Give up if distance is greater than
13263 scroll_max. */
13264 SET_TEXT_POS (pos, PT, PT_BYTE);
13265 start_display (&it, w, pos);
13266 y0 = it.current_y;
13267 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13268 it.last_visible_y, -1,
13269 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13270 dy = it.current_y - y0;
13271 if (dy > scroll_max)
13272 return SCROLLING_FAILED;
13273
13274 /* Compute new window start. */
13275 start_display (&it, w, startp);
13276
13277 if (scroll_conservatively)
13278 amount_to_scroll
13279 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13280 else if (scroll_step || temp_scroll_step)
13281 amount_to_scroll = scroll_max;
13282 else
13283 {
13284 aggressive = current_buffer->scroll_down_aggressively;
13285 height = WINDOW_BOX_TEXT_HEIGHT (w);
13286 if (NUMBERP (aggressive))
13287 {
13288 double float_amount = XFLOATINT (aggressive) * height;
13289 amount_to_scroll = float_amount;
13290 if (amount_to_scroll == 0 && float_amount > 0)
13291 amount_to_scroll = 1;
13292 }
13293 }
13294
13295 if (amount_to_scroll <= 0)
13296 return SCROLLING_FAILED;
13297
13298 move_it_vertically_backward (&it, amount_to_scroll);
13299 startp = it.current.pos;
13300 }
13301 }
13302
13303 /* Run window scroll functions. */
13304 startp = run_window_scroll_functions (window, startp);
13305
13306 /* Display the window. Give up if new fonts are loaded, or if point
13307 doesn't appear. */
13308 if (!try_window (window, startp, 0))
13309 rc = SCROLLING_NEED_LARGER_MATRICES;
13310 else if (w->cursor.vpos < 0)
13311 {
13312 clear_glyph_matrix (w->desired_matrix);
13313 rc = SCROLLING_FAILED;
13314 }
13315 else
13316 {
13317 /* Maybe forget recorded base line for line number display. */
13318 if (!just_this_one_p
13319 || current_buffer->clip_changed
13320 || BEG_UNCHANGED < CHARPOS (startp))
13321 w->base_line_number = Qnil;
13322
13323 /* If cursor ends up on a partially visible line,
13324 treat that as being off the bottom of the screen. */
13325 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13326 {
13327 clear_glyph_matrix (w->desired_matrix);
13328 ++extra_scroll_margin_lines;
13329 goto too_near_end;
13330 }
13331 rc = SCROLLING_SUCCESS;
13332 }
13333
13334 return rc;
13335 }
13336
13337
13338 /* Compute a suitable window start for window W if display of W starts
13339 on a continuation line. Value is non-zero if a new window start
13340 was computed.
13341
13342 The new window start will be computed, based on W's width, starting
13343 from the start of the continued line. It is the start of the
13344 screen line with the minimum distance from the old start W->start. */
13345
13346 static int
13347 compute_window_start_on_continuation_line (w)
13348 struct window *w;
13349 {
13350 struct text_pos pos, start_pos;
13351 int window_start_changed_p = 0;
13352
13353 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13354
13355 /* If window start is on a continuation line... Window start may be
13356 < BEGV in case there's invisible text at the start of the
13357 buffer (M-x rmail, for example). */
13358 if (CHARPOS (start_pos) > BEGV
13359 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13360 {
13361 struct it it;
13362 struct glyph_row *row;
13363
13364 /* Handle the case that the window start is out of range. */
13365 if (CHARPOS (start_pos) < BEGV)
13366 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13367 else if (CHARPOS (start_pos) > ZV)
13368 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13369
13370 /* Find the start of the continued line. This should be fast
13371 because scan_buffer is fast (newline cache). */
13372 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13373 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13374 row, DEFAULT_FACE_ID);
13375 reseat_at_previous_visible_line_start (&it);
13376
13377 /* If the line start is "too far" away from the window start,
13378 say it takes too much time to compute a new window start. */
13379 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13380 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13381 {
13382 int min_distance, distance;
13383
13384 /* Move forward by display lines to find the new window
13385 start. If window width was enlarged, the new start can
13386 be expected to be > the old start. If window width was
13387 decreased, the new window start will be < the old start.
13388 So, we're looking for the display line start with the
13389 minimum distance from the old window start. */
13390 pos = it.current.pos;
13391 min_distance = INFINITY;
13392 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13393 distance < min_distance)
13394 {
13395 min_distance = distance;
13396 pos = it.current.pos;
13397 move_it_by_lines (&it, 1, 0);
13398 }
13399
13400 /* Set the window start there. */
13401 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13402 window_start_changed_p = 1;
13403 }
13404 }
13405
13406 return window_start_changed_p;
13407 }
13408
13409
13410 /* Try cursor movement in case text has not changed in window WINDOW,
13411 with window start STARTP. Value is
13412
13413 CURSOR_MOVEMENT_SUCCESS if successful
13414
13415 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13416
13417 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13418 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13419 we want to scroll as if scroll-step were set to 1. See the code.
13420
13421 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13422 which case we have to abort this redisplay, and adjust matrices
13423 first. */
13424
13425 enum
13426 {
13427 CURSOR_MOVEMENT_SUCCESS,
13428 CURSOR_MOVEMENT_CANNOT_BE_USED,
13429 CURSOR_MOVEMENT_MUST_SCROLL,
13430 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13431 };
13432
13433 static int
13434 try_cursor_movement (window, startp, scroll_step)
13435 Lisp_Object window;
13436 struct text_pos startp;
13437 int *scroll_step;
13438 {
13439 struct window *w = XWINDOW (window);
13440 struct frame *f = XFRAME (w->frame);
13441 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13442
13443 #if GLYPH_DEBUG
13444 if (inhibit_try_cursor_movement)
13445 return rc;
13446 #endif
13447
13448 /* Handle case where text has not changed, only point, and it has
13449 not moved off the frame. */
13450 if (/* Point may be in this window. */
13451 PT >= CHARPOS (startp)
13452 /* Selective display hasn't changed. */
13453 && !current_buffer->clip_changed
13454 /* Function force-mode-line-update is used to force a thorough
13455 redisplay. It sets either windows_or_buffers_changed or
13456 update_mode_lines. So don't take a shortcut here for these
13457 cases. */
13458 && !update_mode_lines
13459 && !windows_or_buffers_changed
13460 && !cursor_type_changed
13461 /* Can't use this case if highlighting a region. When a
13462 region exists, cursor movement has to do more than just
13463 set the cursor. */
13464 && !(!NILP (Vtransient_mark_mode)
13465 && !NILP (current_buffer->mark_active))
13466 && NILP (w->region_showing)
13467 && NILP (Vshow_trailing_whitespace)
13468 /* Right after splitting windows, last_point may be nil. */
13469 && INTEGERP (w->last_point)
13470 /* This code is not used for mini-buffer for the sake of the case
13471 of redisplaying to replace an echo area message; since in
13472 that case the mini-buffer contents per se are usually
13473 unchanged. This code is of no real use in the mini-buffer
13474 since the handling of this_line_start_pos, etc., in redisplay
13475 handles the same cases. */
13476 && !EQ (window, minibuf_window)
13477 /* When splitting windows or for new windows, it happens that
13478 redisplay is called with a nil window_end_vpos or one being
13479 larger than the window. This should really be fixed in
13480 window.c. I don't have this on my list, now, so we do
13481 approximately the same as the old redisplay code. --gerd. */
13482 && INTEGERP (w->window_end_vpos)
13483 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13484 && (FRAME_WINDOW_P (f)
13485 || !overlay_arrow_in_current_buffer_p ()))
13486 {
13487 int this_scroll_margin, top_scroll_margin;
13488 struct glyph_row *row = NULL;
13489
13490 #if GLYPH_DEBUG
13491 debug_method_add (w, "cursor movement");
13492 #endif
13493
13494 /* Scroll if point within this distance from the top or bottom
13495 of the window. This is a pixel value. */
13496 if (scroll_margin > 0)
13497 {
13498 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13499 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13500 }
13501 else
13502 this_scroll_margin = 0;
13503
13504 top_scroll_margin = this_scroll_margin;
13505 if (WINDOW_WANTS_HEADER_LINE_P (w))
13506 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13507
13508 /* Start with the row the cursor was displayed during the last
13509 not paused redisplay. Give up if that row is not valid. */
13510 if (w->last_cursor.vpos < 0
13511 || w->last_cursor.vpos >= w->current_matrix->nrows)
13512 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13513 else
13514 {
13515 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13516 if (row->mode_line_p)
13517 ++row;
13518 if (!row->enabled_p)
13519 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13520 /* If rows are bidi-reordered, back up until we find a row
13521 that does not belong to a continuation line. This is
13522 because we must consider all rows of a continued line as
13523 candidates for cursor positioning, since row start and
13524 end positions change non-linearly with vertical position
13525 in such rows. */
13526 /* FIXME: Revisit this when glyph ``spilling'' in
13527 continuation lines' rows is implemented for
13528 bidi-reordered rows. */
13529 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13530 {
13531 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13532 {
13533 xassert (row->enabled_p);
13534 --row;
13535 /* If we hit the beginning of the displayed portion
13536 without finding the first row of a continued
13537 line, give up. */
13538 if (row <= w->current_matrix->rows)
13539 {
13540 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13541 break;
13542 }
13543
13544 }
13545 }
13546 }
13547
13548 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13549 {
13550 int scroll_p = 0;
13551 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13552
13553 if (PT > XFASTINT (w->last_point))
13554 {
13555 /* Point has moved forward. */
13556 while (MATRIX_ROW_END_CHARPOS (row) < PT
13557 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13558 {
13559 xassert (row->enabled_p);
13560 ++row;
13561 }
13562
13563 /* The end position of a row equals the start position
13564 of the next row. If PT is there, we would rather
13565 display it in the next line. */
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. Note that
13572 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13573 the next line would be drawn, and that
13574 this_scroll_margin can be zero. */
13575 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13576 || PT > MATRIX_ROW_END_CHARPOS (row)
13577 /* Line is completely visible last line in window
13578 and PT is to be set in the next line. */
13579 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13580 && PT == MATRIX_ROW_END_CHARPOS (row)
13581 && !row->ends_at_zv_p
13582 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13583 scroll_p = 1;
13584 }
13585 else if (PT < XFASTINT (w->last_point))
13586 {
13587 /* Cursor has to be moved backward. Note that PT >=
13588 CHARPOS (startp) because of the outer if-statement. */
13589 while (!row->mode_line_p
13590 && (MATRIX_ROW_START_CHARPOS (row) > PT
13591 || (MATRIX_ROW_START_CHARPOS (row) == PT
13592 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13593 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13594 row > w->current_matrix->rows
13595 && (row-1)->ends_in_newline_from_string_p))))
13596 && (row->y > top_scroll_margin
13597 || CHARPOS (startp) == BEGV))
13598 {
13599 xassert (row->enabled_p);
13600 --row;
13601 }
13602
13603 /* Consider the following case: Window starts at BEGV,
13604 there is invisible, intangible text at BEGV, so that
13605 display starts at some point START > BEGV. It can
13606 happen that we are called with PT somewhere between
13607 BEGV and START. Try to handle that case. */
13608 if (row < w->current_matrix->rows
13609 || row->mode_line_p)
13610 {
13611 row = w->current_matrix->rows;
13612 if (row->mode_line_p)
13613 ++row;
13614 }
13615
13616 /* Due to newlines in overlay strings, we may have to
13617 skip forward over overlay strings. */
13618 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13619 && MATRIX_ROW_END_CHARPOS (row) == PT
13620 && !cursor_row_p (w, row))
13621 ++row;
13622
13623 /* If within the scroll margin, scroll. */
13624 if (row->y < top_scroll_margin
13625 && CHARPOS (startp) != BEGV)
13626 scroll_p = 1;
13627 }
13628 else
13629 {
13630 /* Cursor did not move. So don't scroll even if cursor line
13631 is partially visible, as it was so before. */
13632 rc = CURSOR_MOVEMENT_SUCCESS;
13633 }
13634
13635 if (PT < MATRIX_ROW_START_CHARPOS (row)
13636 || PT > MATRIX_ROW_END_CHARPOS (row))
13637 {
13638 /* if PT is not in the glyph row, give up. */
13639 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13640 }
13641 else if (rc != CURSOR_MOVEMENT_SUCCESS
13642 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13643 && make_cursor_line_fully_visible_p)
13644 {
13645 if (PT == MATRIX_ROW_END_CHARPOS (row)
13646 && !row->ends_at_zv_p
13647 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13648 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13649 else if (row->height > window_box_height (w))
13650 {
13651 /* If we end up in a partially visible line, let's
13652 make it fully visible, except when it's taller
13653 than the window, in which case we can't do much
13654 about it. */
13655 *scroll_step = 1;
13656 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13657 }
13658 else
13659 {
13660 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13661 if (!cursor_row_fully_visible_p (w, 0, 1))
13662 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13663 else
13664 rc = CURSOR_MOVEMENT_SUCCESS;
13665 }
13666 }
13667 else if (scroll_p)
13668 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13669 else if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13670 {
13671 /* With bidi-reordered rows, there could be more than
13672 one candidate row whose start and end positions
13673 occlude point. We need to let set_cursor_from_row
13674 find the best candidate. */
13675 /* FIXME: Revisit this when glyph ``spilling'' in
13676 continuation lines' rows is implemented for
13677 bidi-reordered rows. */
13678 int rv = 0;
13679
13680 do
13681 {
13682 rv |= set_cursor_from_row (w, row, w->current_matrix,
13683 0, 0, 0, 0);
13684 /* As soon as we've found the first suitable row
13685 whose ends_at_zv_p flag is set, we are done. */
13686 if (rv
13687 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13688 {
13689 rc = CURSOR_MOVEMENT_SUCCESS;
13690 break;
13691 }
13692 ++row;
13693 }
13694 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13695 && MATRIX_ROW_START_CHARPOS (row) <= PT
13696 && PT <= MATRIX_ROW_END_CHARPOS (row)
13697 && cursor_row_p (w, row));
13698 /* If we didn't find any candidate rows, or exited the
13699 loop before all the candidates were examined, signal
13700 to the caller that this method failed. */
13701 if (rc != CURSOR_MOVEMENT_SUCCESS
13702 && (!rv
13703 || (MATRIX_ROW_START_CHARPOS (row) <= PT
13704 && PT <= MATRIX_ROW_END_CHARPOS (row))))
13705 rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13706 else
13707 rc = CURSOR_MOVEMENT_SUCCESS;
13708 }
13709 else
13710 {
13711 do
13712 {
13713 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13714 {
13715 rc = CURSOR_MOVEMENT_SUCCESS;
13716 break;
13717 }
13718 ++row;
13719 }
13720 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13721 && MATRIX_ROW_START_CHARPOS (row) == PT
13722 && cursor_row_p (w, row));
13723 }
13724 }
13725 }
13726
13727 return rc;
13728 }
13729
13730 void
13731 set_vertical_scroll_bar (w)
13732 struct window *w;
13733 {
13734 int start, end, whole;
13735
13736 /* Calculate the start and end positions for the current window.
13737 At some point, it would be nice to choose between scrollbars
13738 which reflect the whole buffer size, with special markers
13739 indicating narrowing, and scrollbars which reflect only the
13740 visible region.
13741
13742 Note that mini-buffers sometimes aren't displaying any text. */
13743 if (!MINI_WINDOW_P (w)
13744 || (w == XWINDOW (minibuf_window)
13745 && NILP (echo_area_buffer[0])))
13746 {
13747 struct buffer *buf = XBUFFER (w->buffer);
13748 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13749 start = marker_position (w->start) - BUF_BEGV (buf);
13750 /* I don't think this is guaranteed to be right. For the
13751 moment, we'll pretend it is. */
13752 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13753
13754 if (end < start)
13755 end = start;
13756 if (whole < (end - start))
13757 whole = end - start;
13758 }
13759 else
13760 start = end = whole = 0;
13761
13762 /* Indicate what this scroll bar ought to be displaying now. */
13763 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13764 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13765 (w, end - start, whole, start);
13766 }
13767
13768
13769 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13770 selected_window is redisplayed.
13771
13772 We can return without actually redisplaying the window if
13773 fonts_changed_p is nonzero. In that case, redisplay_internal will
13774 retry. */
13775
13776 static void
13777 redisplay_window (window, just_this_one_p)
13778 Lisp_Object window;
13779 int just_this_one_p;
13780 {
13781 struct window *w = XWINDOW (window);
13782 struct frame *f = XFRAME (w->frame);
13783 struct buffer *buffer = XBUFFER (w->buffer);
13784 struct buffer *old = current_buffer;
13785 struct text_pos lpoint, opoint, startp;
13786 int update_mode_line;
13787 int tem;
13788 struct it it;
13789 /* Record it now because it's overwritten. */
13790 int current_matrix_up_to_date_p = 0;
13791 int used_current_matrix_p = 0;
13792 /* This is less strict than current_matrix_up_to_date_p.
13793 It indictes that the buffer contents and narrowing are unchanged. */
13794 int buffer_unchanged_p = 0;
13795 int temp_scroll_step = 0;
13796 int count = SPECPDL_INDEX ();
13797 int rc;
13798 int centering_position = -1;
13799 int last_line_misfit = 0;
13800 int beg_unchanged, end_unchanged;
13801
13802 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13803 opoint = lpoint;
13804
13805 /* W must be a leaf window here. */
13806 xassert (!NILP (w->buffer));
13807 #if GLYPH_DEBUG
13808 *w->desired_matrix->method = 0;
13809 #endif
13810
13811 restart:
13812 reconsider_clip_changes (w, buffer);
13813
13814 /* Has the mode line to be updated? */
13815 update_mode_line = (!NILP (w->update_mode_line)
13816 || update_mode_lines
13817 || buffer->clip_changed
13818 || buffer->prevent_redisplay_optimizations_p);
13819
13820 if (MINI_WINDOW_P (w))
13821 {
13822 if (w == XWINDOW (echo_area_window)
13823 && !NILP (echo_area_buffer[0]))
13824 {
13825 if (update_mode_line)
13826 /* We may have to update a tty frame's menu bar or a
13827 tool-bar. Example `M-x C-h C-h C-g'. */
13828 goto finish_menu_bars;
13829 else
13830 /* We've already displayed the echo area glyphs in this window. */
13831 goto finish_scroll_bars;
13832 }
13833 else if ((w != XWINDOW (minibuf_window)
13834 || minibuf_level == 0)
13835 /* When buffer is nonempty, redisplay window normally. */
13836 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13837 /* Quail displays non-mini buffers in minibuffer window.
13838 In that case, redisplay the window normally. */
13839 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13840 {
13841 /* W is a mini-buffer window, but it's not active, so clear
13842 it. */
13843 int yb = window_text_bottom_y (w);
13844 struct glyph_row *row;
13845 int y;
13846
13847 for (y = 0, row = w->desired_matrix->rows;
13848 y < yb;
13849 y += row->height, ++row)
13850 blank_row (w, row, y);
13851 goto finish_scroll_bars;
13852 }
13853
13854 clear_glyph_matrix (w->desired_matrix);
13855 }
13856
13857 /* Otherwise set up data on this window; select its buffer and point
13858 value. */
13859 /* Really select the buffer, for the sake of buffer-local
13860 variables. */
13861 set_buffer_internal_1 (XBUFFER (w->buffer));
13862
13863 current_matrix_up_to_date_p
13864 = (!NILP (w->window_end_valid)
13865 && !current_buffer->clip_changed
13866 && !current_buffer->prevent_redisplay_optimizations_p
13867 && XFASTINT (w->last_modified) >= MODIFF
13868 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13869
13870 /* Run the window-bottom-change-functions
13871 if it is possible that the text on the screen has changed
13872 (either due to modification of the text, or any other reason). */
13873 if (!current_matrix_up_to_date_p
13874 && !NILP (Vwindow_text_change_functions))
13875 {
13876 safe_run_hooks (Qwindow_text_change_functions);
13877 goto restart;
13878 }
13879
13880 beg_unchanged = BEG_UNCHANGED;
13881 end_unchanged = END_UNCHANGED;
13882
13883 SET_TEXT_POS (opoint, PT, PT_BYTE);
13884
13885 specbind (Qinhibit_point_motion_hooks, Qt);
13886
13887 buffer_unchanged_p
13888 = (!NILP (w->window_end_valid)
13889 && !current_buffer->clip_changed
13890 && XFASTINT (w->last_modified) >= MODIFF
13891 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13892
13893 /* When windows_or_buffers_changed is non-zero, we can't rely on
13894 the window end being valid, so set it to nil there. */
13895 if (windows_or_buffers_changed)
13896 {
13897 /* If window starts on a continuation line, maybe adjust the
13898 window start in case the window's width changed. */
13899 if (XMARKER (w->start)->buffer == current_buffer)
13900 compute_window_start_on_continuation_line (w);
13901
13902 w->window_end_valid = Qnil;
13903 }
13904
13905 /* Some sanity checks. */
13906 CHECK_WINDOW_END (w);
13907 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13908 abort ();
13909 if (BYTEPOS (opoint) < CHARPOS (opoint))
13910 abort ();
13911
13912 /* If %c is in mode line, update it if needed. */
13913 if (!NILP (w->column_number_displayed)
13914 /* This alternative quickly identifies a common case
13915 where no change is needed. */
13916 && !(PT == XFASTINT (w->last_point)
13917 && XFASTINT (w->last_modified) >= MODIFF
13918 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13919 && (XFASTINT (w->column_number_displayed)
13920 != (int) current_column ())) /* iftc */
13921 update_mode_line = 1;
13922
13923 /* Count number of windows showing the selected buffer. An indirect
13924 buffer counts as its base buffer. */
13925 if (!just_this_one_p)
13926 {
13927 struct buffer *current_base, *window_base;
13928 current_base = current_buffer;
13929 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13930 if (current_base->base_buffer)
13931 current_base = current_base->base_buffer;
13932 if (window_base->base_buffer)
13933 window_base = window_base->base_buffer;
13934 if (current_base == window_base)
13935 buffer_shared++;
13936 }
13937
13938 /* Point refers normally to the selected window. For any other
13939 window, set up appropriate value. */
13940 if (!EQ (window, selected_window))
13941 {
13942 int new_pt = XMARKER (w->pointm)->charpos;
13943 int new_pt_byte = marker_byte_position (w->pointm);
13944 if (new_pt < BEGV)
13945 {
13946 new_pt = BEGV;
13947 new_pt_byte = BEGV_BYTE;
13948 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13949 }
13950 else if (new_pt > (ZV - 1))
13951 {
13952 new_pt = ZV;
13953 new_pt_byte = ZV_BYTE;
13954 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13955 }
13956
13957 /* We don't use SET_PT so that the point-motion hooks don't run. */
13958 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13959 }
13960
13961 /* If any of the character widths specified in the display table
13962 have changed, invalidate the width run cache. It's true that
13963 this may be a bit late to catch such changes, but the rest of
13964 redisplay goes (non-fatally) haywire when the display table is
13965 changed, so why should we worry about doing any better? */
13966 if (current_buffer->width_run_cache)
13967 {
13968 struct Lisp_Char_Table *disptab = buffer_display_table ();
13969
13970 if (! disptab_matches_widthtab (disptab,
13971 XVECTOR (current_buffer->width_table)))
13972 {
13973 invalidate_region_cache (current_buffer,
13974 current_buffer->width_run_cache,
13975 BEG, Z);
13976 recompute_width_table (current_buffer, disptab);
13977 }
13978 }
13979
13980 /* If window-start is screwed up, choose a new one. */
13981 if (XMARKER (w->start)->buffer != current_buffer)
13982 goto recenter;
13983
13984 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13985
13986 /* If someone specified a new starting point but did not insist,
13987 check whether it can be used. */
13988 if (!NILP (w->optional_new_start)
13989 && CHARPOS (startp) >= BEGV
13990 && CHARPOS (startp) <= ZV)
13991 {
13992 w->optional_new_start = Qnil;
13993 start_display (&it, w, startp);
13994 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13995 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13996 if (IT_CHARPOS (it) == PT)
13997 w->force_start = Qt;
13998 /* IT may overshoot PT if text at PT is invisible. */
13999 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14000 w->force_start = Qt;
14001 }
14002
14003 force_start:
14004
14005 /* Handle case where place to start displaying has been specified,
14006 unless the specified location is outside the accessible range. */
14007 if (!NILP (w->force_start)
14008 || w->frozen_window_start_p)
14009 {
14010 /* We set this later on if we have to adjust point. */
14011 int new_vpos = -1;
14012
14013 w->force_start = Qnil;
14014 w->vscroll = 0;
14015 w->window_end_valid = Qnil;
14016
14017 /* Forget any recorded base line for line number display. */
14018 if (!buffer_unchanged_p)
14019 w->base_line_number = Qnil;
14020
14021 /* Redisplay the mode line. Select the buffer properly for that.
14022 Also, run the hook window-scroll-functions
14023 because we have scrolled. */
14024 /* Note, we do this after clearing force_start because
14025 if there's an error, it is better to forget about force_start
14026 than to get into an infinite loop calling the hook functions
14027 and having them get more errors. */
14028 if (!update_mode_line
14029 || ! NILP (Vwindow_scroll_functions))
14030 {
14031 update_mode_line = 1;
14032 w->update_mode_line = Qt;
14033 startp = run_window_scroll_functions (window, startp);
14034 }
14035
14036 w->last_modified = make_number (0);
14037 w->last_overlay_modified = make_number (0);
14038 if (CHARPOS (startp) < BEGV)
14039 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14040 else if (CHARPOS (startp) > ZV)
14041 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14042
14043 /* Redisplay, then check if cursor has been set during the
14044 redisplay. Give up if new fonts were loaded. */
14045 /* We used to issue a CHECK_MARGINS argument to try_window here,
14046 but this causes scrolling to fail when point begins inside
14047 the scroll margin (bug#148) -- cyd */
14048 if (!try_window (window, startp, 0))
14049 {
14050 w->force_start = Qt;
14051 clear_glyph_matrix (w->desired_matrix);
14052 goto need_larger_matrices;
14053 }
14054
14055 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14056 {
14057 /* If point does not appear, try to move point so it does
14058 appear. The desired matrix has been built above, so we
14059 can use it here. */
14060 new_vpos = window_box_height (w) / 2;
14061 }
14062
14063 if (!cursor_row_fully_visible_p (w, 0, 0))
14064 {
14065 /* Point does appear, but on a line partly visible at end of window.
14066 Move it back to a fully-visible line. */
14067 new_vpos = window_box_height (w);
14068 }
14069
14070 /* If we need to move point for either of the above reasons,
14071 now actually do it. */
14072 if (new_vpos >= 0)
14073 {
14074 struct glyph_row *row;
14075
14076 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14077 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14078 ++row;
14079
14080 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14081 MATRIX_ROW_START_BYTEPOS (row));
14082
14083 if (w != XWINDOW (selected_window))
14084 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14085 else if (current_buffer == old)
14086 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14087
14088 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14089
14090 /* If we are highlighting the region, then we just changed
14091 the region, so redisplay to show it. */
14092 if (!NILP (Vtransient_mark_mode)
14093 && !NILP (current_buffer->mark_active))
14094 {
14095 clear_glyph_matrix (w->desired_matrix);
14096 if (!try_window (window, startp, 0))
14097 goto need_larger_matrices;
14098 }
14099 }
14100
14101 #if GLYPH_DEBUG
14102 debug_method_add (w, "forced window start");
14103 #endif
14104 goto done;
14105 }
14106
14107 /* Handle case where text has not changed, only point, and it has
14108 not moved off the frame, and we are not retrying after hscroll.
14109 (current_matrix_up_to_date_p is nonzero when retrying.) */
14110 if (current_matrix_up_to_date_p
14111 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14112 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14113 {
14114 switch (rc)
14115 {
14116 case CURSOR_MOVEMENT_SUCCESS:
14117 used_current_matrix_p = 1;
14118 goto done;
14119
14120 case CURSOR_MOVEMENT_MUST_SCROLL:
14121 goto try_to_scroll;
14122
14123 default:
14124 abort ();
14125 }
14126 }
14127 /* If current starting point was originally the beginning of a line
14128 but no longer is, find a new starting point. */
14129 else if (!NILP (w->start_at_line_beg)
14130 && !(CHARPOS (startp) <= BEGV
14131 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14132 {
14133 #if GLYPH_DEBUG
14134 debug_method_add (w, "recenter 1");
14135 #endif
14136 goto recenter;
14137 }
14138
14139 /* Try scrolling with try_window_id. Value is > 0 if update has
14140 been done, it is -1 if we know that the same window start will
14141 not work. It is 0 if unsuccessful for some other reason. */
14142 else if ((tem = try_window_id (w)) != 0)
14143 {
14144 #if GLYPH_DEBUG
14145 debug_method_add (w, "try_window_id %d", tem);
14146 #endif
14147
14148 if (fonts_changed_p)
14149 goto need_larger_matrices;
14150 if (tem > 0)
14151 goto done;
14152
14153 /* Otherwise try_window_id has returned -1 which means that we
14154 don't want the alternative below this comment to execute. */
14155 }
14156 else if (CHARPOS (startp) >= BEGV
14157 && CHARPOS (startp) <= ZV
14158 && PT >= CHARPOS (startp)
14159 && (CHARPOS (startp) < ZV
14160 /* Avoid starting at end of buffer. */
14161 || CHARPOS (startp) == BEGV
14162 || (XFASTINT (w->last_modified) >= MODIFF
14163 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14164 {
14165
14166 /* If first window line is a continuation line, and window start
14167 is inside the modified region, but the first change is before
14168 current window start, we must select a new window start.
14169
14170 However, if this is the result of a down-mouse event (e.g. by
14171 extending the mouse-drag-overlay), we don't want to select a
14172 new window start, since that would change the position under
14173 the mouse, resulting in an unwanted mouse-movement rather
14174 than a simple mouse-click. */
14175 if (NILP (w->start_at_line_beg)
14176 && NILP (do_mouse_tracking)
14177 && CHARPOS (startp) > BEGV
14178 && CHARPOS (startp) > BEG + beg_unchanged
14179 && CHARPOS (startp) <= Z - end_unchanged
14180 /* Even if w->start_at_line_beg is nil, a new window may
14181 start at a line_beg, since that's how set_buffer_window
14182 sets it. So, we need to check the return value of
14183 compute_window_start_on_continuation_line. (See also
14184 bug#197). */
14185 && XMARKER (w->start)->buffer == current_buffer
14186 && compute_window_start_on_continuation_line (w))
14187 {
14188 w->force_start = Qt;
14189 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14190 goto force_start;
14191 }
14192
14193 #if GLYPH_DEBUG
14194 debug_method_add (w, "same window start");
14195 #endif
14196
14197 /* Try to redisplay starting at same place as before.
14198 If point has not moved off frame, accept the results. */
14199 if (!current_matrix_up_to_date_p
14200 /* Don't use try_window_reusing_current_matrix in this case
14201 because a window scroll function can have changed the
14202 buffer. */
14203 || !NILP (Vwindow_scroll_functions)
14204 || MINI_WINDOW_P (w)
14205 || !(used_current_matrix_p
14206 = try_window_reusing_current_matrix (w)))
14207 {
14208 IF_DEBUG (debug_method_add (w, "1"));
14209 if (try_window (window, startp, 1) < 0)
14210 /* -1 means we need to scroll.
14211 0 means we need new matrices, but fonts_changed_p
14212 is set in that case, so we will detect it below. */
14213 goto try_to_scroll;
14214 }
14215
14216 if (fonts_changed_p)
14217 goto need_larger_matrices;
14218
14219 if (w->cursor.vpos >= 0)
14220 {
14221 if (!just_this_one_p
14222 || current_buffer->clip_changed
14223 || BEG_UNCHANGED < CHARPOS (startp))
14224 /* Forget any recorded base line for line number display. */
14225 w->base_line_number = Qnil;
14226
14227 if (!cursor_row_fully_visible_p (w, 1, 0))
14228 {
14229 clear_glyph_matrix (w->desired_matrix);
14230 last_line_misfit = 1;
14231 }
14232 /* Drop through and scroll. */
14233 else
14234 goto done;
14235 }
14236 else
14237 clear_glyph_matrix (w->desired_matrix);
14238 }
14239
14240 try_to_scroll:
14241
14242 w->last_modified = make_number (0);
14243 w->last_overlay_modified = make_number (0);
14244
14245 /* Redisplay the mode line. Select the buffer properly for that. */
14246 if (!update_mode_line)
14247 {
14248 update_mode_line = 1;
14249 w->update_mode_line = Qt;
14250 }
14251
14252 /* Try to scroll by specified few lines. */
14253 if ((scroll_conservatively
14254 || scroll_step
14255 || temp_scroll_step
14256 || NUMBERP (current_buffer->scroll_up_aggressively)
14257 || NUMBERP (current_buffer->scroll_down_aggressively))
14258 && !current_buffer->clip_changed
14259 && CHARPOS (startp) >= BEGV
14260 && CHARPOS (startp) <= ZV)
14261 {
14262 /* The function returns -1 if new fonts were loaded, 1 if
14263 successful, 0 if not successful. */
14264 int rc = try_scrolling (window, just_this_one_p,
14265 scroll_conservatively,
14266 scroll_step,
14267 temp_scroll_step, last_line_misfit);
14268 switch (rc)
14269 {
14270 case SCROLLING_SUCCESS:
14271 goto done;
14272
14273 case SCROLLING_NEED_LARGER_MATRICES:
14274 goto need_larger_matrices;
14275
14276 case SCROLLING_FAILED:
14277 break;
14278
14279 default:
14280 abort ();
14281 }
14282 }
14283
14284 /* Finally, just choose place to start which centers point */
14285
14286 recenter:
14287 if (centering_position < 0)
14288 centering_position = window_box_height (w) / 2;
14289
14290 #if GLYPH_DEBUG
14291 debug_method_add (w, "recenter");
14292 #endif
14293
14294 /* w->vscroll = 0; */
14295
14296 /* Forget any previously recorded base line for line number display. */
14297 if (!buffer_unchanged_p)
14298 w->base_line_number = Qnil;
14299
14300 /* Move backward half the height of the window. */
14301 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14302 it.current_y = it.last_visible_y;
14303 move_it_vertically_backward (&it, centering_position);
14304 xassert (IT_CHARPOS (it) >= BEGV);
14305
14306 /* The function move_it_vertically_backward may move over more
14307 than the specified y-distance. If it->w is small, e.g. a
14308 mini-buffer window, we may end up in front of the window's
14309 display area. Start displaying at the start of the line
14310 containing PT in this case. */
14311 if (it.current_y <= 0)
14312 {
14313 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14314 move_it_vertically_backward (&it, 0);
14315 it.current_y = 0;
14316 }
14317
14318 it.current_x = it.hpos = 0;
14319
14320 /* Set startp here explicitly in case that helps avoid an infinite loop
14321 in case the window-scroll-functions functions get errors. */
14322 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14323
14324 /* Run scroll hooks. */
14325 startp = run_window_scroll_functions (window, it.current.pos);
14326
14327 /* Redisplay the window. */
14328 if (!current_matrix_up_to_date_p
14329 || windows_or_buffers_changed
14330 || cursor_type_changed
14331 /* Don't use try_window_reusing_current_matrix in this case
14332 because it can have changed the buffer. */
14333 || !NILP (Vwindow_scroll_functions)
14334 || !just_this_one_p
14335 || MINI_WINDOW_P (w)
14336 || !(used_current_matrix_p
14337 = try_window_reusing_current_matrix (w)))
14338 try_window (window, startp, 0);
14339
14340 /* If new fonts have been loaded (due to fontsets), give up. We
14341 have to start a new redisplay since we need to re-adjust glyph
14342 matrices. */
14343 if (fonts_changed_p)
14344 goto need_larger_matrices;
14345
14346 /* If cursor did not appear assume that the middle of the window is
14347 in the first line of the window. Do it again with the next line.
14348 (Imagine a window of height 100, displaying two lines of height
14349 60. Moving back 50 from it->last_visible_y will end in the first
14350 line.) */
14351 if (w->cursor.vpos < 0)
14352 {
14353 if (!NILP (w->window_end_valid)
14354 && PT >= Z - XFASTINT (w->window_end_pos))
14355 {
14356 clear_glyph_matrix (w->desired_matrix);
14357 move_it_by_lines (&it, 1, 0);
14358 try_window (window, it.current.pos, 0);
14359 }
14360 else if (PT < IT_CHARPOS (it))
14361 {
14362 clear_glyph_matrix (w->desired_matrix);
14363 move_it_by_lines (&it, -1, 0);
14364 try_window (window, it.current.pos, 0);
14365 }
14366 else
14367 {
14368 /* Not much we can do about it. */
14369 }
14370 }
14371
14372 /* Consider the following case: Window starts at BEGV, there is
14373 invisible, intangible text at BEGV, so that display starts at
14374 some point START > BEGV. It can happen that we are called with
14375 PT somewhere between BEGV and START. Try to handle that case. */
14376 if (w->cursor.vpos < 0)
14377 {
14378 struct glyph_row *row = w->current_matrix->rows;
14379 if (row->mode_line_p)
14380 ++row;
14381 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14382 }
14383
14384 if (!cursor_row_fully_visible_p (w, 0, 0))
14385 {
14386 /* If vscroll is enabled, disable it and try again. */
14387 if (w->vscroll)
14388 {
14389 w->vscroll = 0;
14390 clear_glyph_matrix (w->desired_matrix);
14391 goto recenter;
14392 }
14393
14394 /* If centering point failed to make the whole line visible,
14395 put point at the top instead. That has to make the whole line
14396 visible, if it can be done. */
14397 if (centering_position == 0)
14398 goto done;
14399
14400 clear_glyph_matrix (w->desired_matrix);
14401 centering_position = 0;
14402 goto recenter;
14403 }
14404
14405 done:
14406
14407 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14408 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14409 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14410 ? Qt : Qnil);
14411
14412 /* Display the mode line, if we must. */
14413 if ((update_mode_line
14414 /* If window not full width, must redo its mode line
14415 if (a) the window to its side is being redone and
14416 (b) we do a frame-based redisplay. This is a consequence
14417 of how inverted lines are drawn in frame-based redisplay. */
14418 || (!just_this_one_p
14419 && !FRAME_WINDOW_P (f)
14420 && !WINDOW_FULL_WIDTH_P (w))
14421 /* Line number to display. */
14422 || INTEGERP (w->base_line_pos)
14423 /* Column number is displayed and different from the one displayed. */
14424 || (!NILP (w->column_number_displayed)
14425 && (XFASTINT (w->column_number_displayed)
14426 != (int) current_column ()))) /* iftc */
14427 /* This means that the window has a mode line. */
14428 && (WINDOW_WANTS_MODELINE_P (w)
14429 || WINDOW_WANTS_HEADER_LINE_P (w)))
14430 {
14431 display_mode_lines (w);
14432
14433 /* If mode line height has changed, arrange for a thorough
14434 immediate redisplay using the correct mode line height. */
14435 if (WINDOW_WANTS_MODELINE_P (w)
14436 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14437 {
14438 fonts_changed_p = 1;
14439 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14440 = DESIRED_MODE_LINE_HEIGHT (w);
14441 }
14442
14443 /* If header line height has changed, arrange for a thorough
14444 immediate redisplay using the correct header line height. */
14445 if (WINDOW_WANTS_HEADER_LINE_P (w)
14446 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14447 {
14448 fonts_changed_p = 1;
14449 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14450 = DESIRED_HEADER_LINE_HEIGHT (w);
14451 }
14452
14453 if (fonts_changed_p)
14454 goto need_larger_matrices;
14455 }
14456
14457 if (!line_number_displayed
14458 && !BUFFERP (w->base_line_pos))
14459 {
14460 w->base_line_pos = Qnil;
14461 w->base_line_number = Qnil;
14462 }
14463
14464 finish_menu_bars:
14465
14466 /* When we reach a frame's selected window, redo the frame's menu bar. */
14467 if (update_mode_line
14468 && EQ (FRAME_SELECTED_WINDOW (f), window))
14469 {
14470 int redisplay_menu_p = 0;
14471 int redisplay_tool_bar_p = 0;
14472
14473 if (FRAME_WINDOW_P (f))
14474 {
14475 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14476 || defined (HAVE_NS) || defined (USE_GTK)
14477 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14478 #else
14479 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14480 #endif
14481 }
14482 else
14483 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14484
14485 if (redisplay_menu_p)
14486 display_menu_bar (w);
14487
14488 #ifdef HAVE_WINDOW_SYSTEM
14489 if (FRAME_WINDOW_P (f))
14490 {
14491 #if defined (USE_GTK) || defined (HAVE_NS)
14492 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14493 #else
14494 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14495 && (FRAME_TOOL_BAR_LINES (f) > 0
14496 || !NILP (Vauto_resize_tool_bars));
14497 #endif
14498
14499 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14500 {
14501 extern int ignore_mouse_drag_p;
14502 ignore_mouse_drag_p = 1;
14503 }
14504 }
14505 #endif
14506 }
14507
14508 #ifdef HAVE_WINDOW_SYSTEM
14509 if (FRAME_WINDOW_P (f)
14510 && update_window_fringes (w, (just_this_one_p
14511 || (!used_current_matrix_p && !overlay_arrow_seen)
14512 || w->pseudo_window_p)))
14513 {
14514 update_begin (f);
14515 BLOCK_INPUT;
14516 if (draw_window_fringes (w, 1))
14517 x_draw_vertical_border (w);
14518 UNBLOCK_INPUT;
14519 update_end (f);
14520 }
14521 #endif /* HAVE_WINDOW_SYSTEM */
14522
14523 /* We go to this label, with fonts_changed_p nonzero,
14524 if it is necessary to try again using larger glyph matrices.
14525 We have to redeem the scroll bar even in this case,
14526 because the loop in redisplay_internal expects that. */
14527 need_larger_matrices:
14528 ;
14529 finish_scroll_bars:
14530
14531 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14532 {
14533 /* Set the thumb's position and size. */
14534 set_vertical_scroll_bar (w);
14535
14536 /* Note that we actually used the scroll bar attached to this
14537 window, so it shouldn't be deleted at the end of redisplay. */
14538 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14539 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14540 }
14541
14542 /* Restore current_buffer and value of point in it. */
14543 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14544 set_buffer_internal_1 (old);
14545 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14546 shorter. This can be caused by log truncation in *Messages*. */
14547 if (CHARPOS (lpoint) <= ZV)
14548 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14549
14550 unbind_to (count, Qnil);
14551 }
14552
14553
14554 /* Build the complete desired matrix of WINDOW with a window start
14555 buffer position POS.
14556
14557 Value is 1 if successful. It is zero if fonts were loaded during
14558 redisplay which makes re-adjusting glyph matrices necessary, and -1
14559 if point would appear in the scroll margins.
14560 (We check that only if CHECK_MARGINS is nonzero. */
14561
14562 int
14563 try_window (window, pos, check_margins)
14564 Lisp_Object window;
14565 struct text_pos pos;
14566 int check_margins;
14567 {
14568 struct window *w = XWINDOW (window);
14569 struct it it;
14570 struct glyph_row *last_text_row = NULL;
14571 struct frame *f = XFRAME (w->frame);
14572
14573 /* Make POS the new window start. */
14574 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14575
14576 /* Mark cursor position as unknown. No overlay arrow seen. */
14577 w->cursor.vpos = -1;
14578 overlay_arrow_seen = 0;
14579
14580 /* Initialize iterator and info to start at POS. */
14581 start_display (&it, w, pos);
14582
14583 /* Display all lines of W. */
14584 while (it.current_y < it.last_visible_y)
14585 {
14586 if (display_line (&it))
14587 last_text_row = it.glyph_row - 1;
14588 if (fonts_changed_p)
14589 return 0;
14590 }
14591
14592 /* Don't let the cursor end in the scroll margins. */
14593 if (check_margins
14594 && !MINI_WINDOW_P (w))
14595 {
14596 int this_scroll_margin;
14597
14598 if (scroll_margin > 0)
14599 {
14600 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14601 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14602 }
14603 else
14604 this_scroll_margin = 0;
14605
14606 if ((w->cursor.y >= 0 /* not vscrolled */
14607 && w->cursor.y < this_scroll_margin
14608 && CHARPOS (pos) > BEGV
14609 && IT_CHARPOS (it) < ZV)
14610 /* rms: considering make_cursor_line_fully_visible_p here
14611 seems to give wrong results. We don't want to recenter
14612 when the last line is partly visible, we want to allow
14613 that case to be handled in the usual way. */
14614 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14615 {
14616 w->cursor.vpos = -1;
14617 clear_glyph_matrix (w->desired_matrix);
14618 return -1;
14619 }
14620 }
14621
14622 /* If bottom moved off end of frame, change mode line percentage. */
14623 if (XFASTINT (w->window_end_pos) <= 0
14624 && Z != IT_CHARPOS (it))
14625 w->update_mode_line = Qt;
14626
14627 /* Set window_end_pos to the offset of the last character displayed
14628 on the window from the end of current_buffer. Set
14629 window_end_vpos to its row number. */
14630 if (last_text_row)
14631 {
14632 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14633 w->window_end_bytepos
14634 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14635 w->window_end_pos
14636 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14637 w->window_end_vpos
14638 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14639 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14640 ->displays_text_p);
14641 }
14642 else
14643 {
14644 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14645 w->window_end_pos = make_number (Z - ZV);
14646 w->window_end_vpos = make_number (0);
14647 }
14648
14649 /* But that is not valid info until redisplay finishes. */
14650 w->window_end_valid = Qnil;
14651 return 1;
14652 }
14653
14654
14655 \f
14656 /************************************************************************
14657 Window redisplay reusing current matrix when buffer has not changed
14658 ************************************************************************/
14659
14660 /* Try redisplay of window W showing an unchanged buffer with a
14661 different window start than the last time it was displayed by
14662 reusing its current matrix. Value is non-zero if successful.
14663 W->start is the new window start. */
14664
14665 static int
14666 try_window_reusing_current_matrix (w)
14667 struct window *w;
14668 {
14669 struct frame *f = XFRAME (w->frame);
14670 struct glyph_row *row, *bottom_row;
14671 struct it it;
14672 struct run run;
14673 struct text_pos start, new_start;
14674 int nrows_scrolled, i;
14675 struct glyph_row *last_text_row;
14676 struct glyph_row *last_reused_text_row;
14677 struct glyph_row *start_row;
14678 int start_vpos, min_y, max_y;
14679
14680 #if GLYPH_DEBUG
14681 if (inhibit_try_window_reusing)
14682 return 0;
14683 #endif
14684
14685 if (/* This function doesn't handle terminal frames. */
14686 !FRAME_WINDOW_P (f)
14687 /* Don't try to reuse the display if windows have been split
14688 or such. */
14689 || windows_or_buffers_changed
14690 || cursor_type_changed)
14691 return 0;
14692
14693 /* Can't do this if region may have changed. */
14694 if ((!NILP (Vtransient_mark_mode)
14695 && !NILP (current_buffer->mark_active))
14696 || !NILP (w->region_showing)
14697 || !NILP (Vshow_trailing_whitespace))
14698 return 0;
14699
14700 /* If top-line visibility has changed, give up. */
14701 if (WINDOW_WANTS_HEADER_LINE_P (w)
14702 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14703 return 0;
14704
14705 /* Give up if old or new display is scrolled vertically. We could
14706 make this function handle this, but right now it doesn't. */
14707 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14708 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14709 return 0;
14710
14711 /* The variable new_start now holds the new window start. The old
14712 start `start' can be determined from the current matrix. */
14713 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14714 start = start_row->start.pos;
14715 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14716
14717 /* Clear the desired matrix for the display below. */
14718 clear_glyph_matrix (w->desired_matrix);
14719
14720 if (CHARPOS (new_start) <= CHARPOS (start))
14721 {
14722 int first_row_y;
14723
14724 /* Don't use this method if the display starts with an ellipsis
14725 displayed for invisible text. It's not easy to handle that case
14726 below, and it's certainly not worth the effort since this is
14727 not a frequent case. */
14728 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14729 return 0;
14730
14731 IF_DEBUG (debug_method_add (w, "twu1"));
14732
14733 /* Display up to a row that can be reused. The variable
14734 last_text_row is set to the last row displayed that displays
14735 text. Note that it.vpos == 0 if or if not there is a
14736 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14737 start_display (&it, w, new_start);
14738 first_row_y = it.current_y;
14739 w->cursor.vpos = -1;
14740 last_text_row = last_reused_text_row = NULL;
14741
14742 while (it.current_y < it.last_visible_y
14743 && !fonts_changed_p)
14744 {
14745 /* If we have reached into the characters in the START row,
14746 that means the line boundaries have changed. So we
14747 can't start copying with the row START. Maybe it will
14748 work to start copying with the following row. */
14749 while (IT_CHARPOS (it) > CHARPOS (start))
14750 {
14751 /* Advance to the next row as the "start". */
14752 start_row++;
14753 start = start_row->start.pos;
14754 /* If there are no more rows to try, or just one, give up. */
14755 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14756 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14757 || CHARPOS (start) == ZV)
14758 {
14759 clear_glyph_matrix (w->desired_matrix);
14760 return 0;
14761 }
14762
14763 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14764 }
14765 /* If we have reached alignment,
14766 we can copy the rest of the rows. */
14767 if (IT_CHARPOS (it) == CHARPOS (start))
14768 break;
14769
14770 if (display_line (&it))
14771 last_text_row = it.glyph_row - 1;
14772 }
14773
14774 /* A value of current_y < last_visible_y means that we stopped
14775 at the previous window start, which in turn means that we
14776 have at least one reusable row. */
14777 if (it.current_y < it.last_visible_y)
14778 {
14779 /* IT.vpos always starts from 0; it counts text lines. */
14780 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14781
14782 /* Find PT if not already found in the lines displayed. */
14783 if (w->cursor.vpos < 0)
14784 {
14785 int dy = it.current_y - start_row->y;
14786
14787 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14788 row = row_containing_pos (w, PT, row, NULL, dy);
14789 if (row)
14790 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14791 dy, nrows_scrolled);
14792 else
14793 {
14794 clear_glyph_matrix (w->desired_matrix);
14795 return 0;
14796 }
14797 }
14798
14799 /* Scroll the display. Do it before the current matrix is
14800 changed. The problem here is that update has not yet
14801 run, i.e. part of the current matrix is not up to date.
14802 scroll_run_hook will clear the cursor, and use the
14803 current matrix to get the height of the row the cursor is
14804 in. */
14805 run.current_y = start_row->y;
14806 run.desired_y = it.current_y;
14807 run.height = it.last_visible_y - it.current_y;
14808
14809 if (run.height > 0 && run.current_y != run.desired_y)
14810 {
14811 update_begin (f);
14812 FRAME_RIF (f)->update_window_begin_hook (w);
14813 FRAME_RIF (f)->clear_window_mouse_face (w);
14814 FRAME_RIF (f)->scroll_run_hook (w, &run);
14815 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14816 update_end (f);
14817 }
14818
14819 /* Shift current matrix down by nrows_scrolled lines. */
14820 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14821 rotate_matrix (w->current_matrix,
14822 start_vpos,
14823 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14824 nrows_scrolled);
14825
14826 /* Disable lines that must be updated. */
14827 for (i = 0; i < nrows_scrolled; ++i)
14828 (start_row + i)->enabled_p = 0;
14829
14830 /* Re-compute Y positions. */
14831 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14832 max_y = it.last_visible_y;
14833 for (row = start_row + nrows_scrolled;
14834 row < bottom_row;
14835 ++row)
14836 {
14837 row->y = it.current_y;
14838 row->visible_height = row->height;
14839
14840 if (row->y < min_y)
14841 row->visible_height -= min_y - row->y;
14842 if (row->y + row->height > max_y)
14843 row->visible_height -= row->y + row->height - max_y;
14844 row->redraw_fringe_bitmaps_p = 1;
14845
14846 it.current_y += row->height;
14847
14848 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14849 last_reused_text_row = row;
14850 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14851 break;
14852 }
14853
14854 /* Disable lines in the current matrix which are now
14855 below the window. */
14856 for (++row; row < bottom_row; ++row)
14857 row->enabled_p = row->mode_line_p = 0;
14858 }
14859
14860 /* Update window_end_pos etc.; last_reused_text_row is the last
14861 reused row from the current matrix containing text, if any.
14862 The value of last_text_row is the last displayed line
14863 containing text. */
14864 if (last_reused_text_row)
14865 {
14866 w->window_end_bytepos
14867 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14868 w->window_end_pos
14869 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14870 w->window_end_vpos
14871 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14872 w->current_matrix));
14873 }
14874 else if (last_text_row)
14875 {
14876 w->window_end_bytepos
14877 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14878 w->window_end_pos
14879 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14880 w->window_end_vpos
14881 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14882 }
14883 else
14884 {
14885 /* This window must be completely empty. */
14886 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14887 w->window_end_pos = make_number (Z - ZV);
14888 w->window_end_vpos = make_number (0);
14889 }
14890 w->window_end_valid = Qnil;
14891
14892 /* Update hint: don't try scrolling again in update_window. */
14893 w->desired_matrix->no_scrolling_p = 1;
14894
14895 #if GLYPH_DEBUG
14896 debug_method_add (w, "try_window_reusing_current_matrix 1");
14897 #endif
14898 return 1;
14899 }
14900 else if (CHARPOS (new_start) > CHARPOS (start))
14901 {
14902 struct glyph_row *pt_row, *row;
14903 struct glyph_row *first_reusable_row;
14904 struct glyph_row *first_row_to_display;
14905 int dy;
14906 int yb = window_text_bottom_y (w);
14907
14908 /* Find the row starting at new_start, if there is one. Don't
14909 reuse a partially visible line at the end. */
14910 first_reusable_row = start_row;
14911 while (first_reusable_row->enabled_p
14912 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14913 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14914 < CHARPOS (new_start)))
14915 ++first_reusable_row;
14916
14917 /* Give up if there is no row to reuse. */
14918 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14919 || !first_reusable_row->enabled_p
14920 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14921 != CHARPOS (new_start)))
14922 return 0;
14923
14924 /* We can reuse fully visible rows beginning with
14925 first_reusable_row to the end of the window. Set
14926 first_row_to_display to the first row that cannot be reused.
14927 Set pt_row to the row containing point, if there is any. */
14928 pt_row = NULL;
14929 for (first_row_to_display = first_reusable_row;
14930 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14931 ++first_row_to_display)
14932 {
14933 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14934 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14935 pt_row = first_row_to_display;
14936 }
14937
14938 /* Start displaying at the start of first_row_to_display. */
14939 xassert (first_row_to_display->y < yb);
14940 init_to_row_start (&it, w, first_row_to_display);
14941
14942 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14943 - start_vpos);
14944 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14945 - nrows_scrolled);
14946 it.current_y = (first_row_to_display->y - first_reusable_row->y
14947 + WINDOW_HEADER_LINE_HEIGHT (w));
14948
14949 /* Display lines beginning with first_row_to_display in the
14950 desired matrix. Set last_text_row to the last row displayed
14951 that displays text. */
14952 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14953 if (pt_row == NULL)
14954 w->cursor.vpos = -1;
14955 last_text_row = NULL;
14956 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14957 if (display_line (&it))
14958 last_text_row = it.glyph_row - 1;
14959
14960 /* If point is in a reused row, adjust y and vpos of the cursor
14961 position. */
14962 if (pt_row)
14963 {
14964 w->cursor.vpos -= nrows_scrolled;
14965 w->cursor.y -= first_reusable_row->y - start_row->y;
14966 }
14967
14968 /* Give up if point isn't in a row displayed or reused. (This
14969 also handles the case where w->cursor.vpos < nrows_scrolled
14970 after the calls to display_line, which can happen with scroll
14971 margins. See bug#1295.) */
14972 if (w->cursor.vpos < 0)
14973 {
14974 clear_glyph_matrix (w->desired_matrix);
14975 return 0;
14976 }
14977
14978 /* Scroll the display. */
14979 run.current_y = first_reusable_row->y;
14980 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14981 run.height = it.last_visible_y - run.current_y;
14982 dy = run.current_y - run.desired_y;
14983
14984 if (run.height)
14985 {
14986 update_begin (f);
14987 FRAME_RIF (f)->update_window_begin_hook (w);
14988 FRAME_RIF (f)->clear_window_mouse_face (w);
14989 FRAME_RIF (f)->scroll_run_hook (w, &run);
14990 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14991 update_end (f);
14992 }
14993
14994 /* Adjust Y positions of reused rows. */
14995 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14996 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14997 max_y = it.last_visible_y;
14998 for (row = first_reusable_row; row < first_row_to_display; ++row)
14999 {
15000 row->y -= dy;
15001 row->visible_height = row->height;
15002 if (row->y < min_y)
15003 row->visible_height -= min_y - row->y;
15004 if (row->y + row->height > max_y)
15005 row->visible_height -= row->y + row->height - max_y;
15006 row->redraw_fringe_bitmaps_p = 1;
15007 }
15008
15009 /* Scroll the current matrix. */
15010 xassert (nrows_scrolled > 0);
15011 rotate_matrix (w->current_matrix,
15012 start_vpos,
15013 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15014 -nrows_scrolled);
15015
15016 /* Disable rows not reused. */
15017 for (row -= nrows_scrolled; row < bottom_row; ++row)
15018 row->enabled_p = 0;
15019
15020 /* Point may have moved to a different line, so we cannot assume that
15021 the previous cursor position is valid; locate the correct row. */
15022 if (pt_row)
15023 {
15024 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15025 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15026 row++)
15027 {
15028 w->cursor.vpos++;
15029 w->cursor.y = row->y;
15030 }
15031 if (row < bottom_row)
15032 {
15033 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15034 struct glyph *end = glyph + row->used[TEXT_AREA];
15035 struct glyph *orig_glyph = glyph;
15036 struct cursor_pos orig_cursor = w->cursor;
15037
15038 for (; glyph < end
15039 && (!BUFFERP (glyph->object)
15040 || glyph->charpos != PT);
15041 glyph++)
15042 {
15043 w->cursor.hpos++;
15044 w->cursor.x += glyph->pixel_width;
15045 }
15046 /* With bidi reordering, charpos changes non-linearly
15047 with hpos, so the right glyph could be to the
15048 left. */
15049 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15050 && (!BUFFERP (glyph->object) || glyph->charpos != PT))
15051 {
15052 struct glyph *start_glyph = row->glyphs[TEXT_AREA];
15053
15054 glyph = orig_glyph - 1;
15055 orig_cursor.hpos--;
15056 orig_cursor.x -= glyph->pixel_width;
15057 for (; glyph >= start_glyph
15058 && (!BUFFERP (glyph->object)
15059 || glyph->charpos != PT);
15060 glyph--)
15061 {
15062 w->cursor.hpos--;
15063 w->cursor.x -= glyph->pixel_width;
15064 }
15065 if (BUFFERP (glyph->object) && glyph->charpos == PT)
15066 w->cursor = orig_cursor;
15067 }
15068 }
15069 }
15070
15071 /* Adjust window end. A null value of last_text_row means that
15072 the window end is in reused rows which in turn means that
15073 only its vpos can have changed. */
15074 if (last_text_row)
15075 {
15076 w->window_end_bytepos
15077 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15078 w->window_end_pos
15079 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15080 w->window_end_vpos
15081 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15082 }
15083 else
15084 {
15085 w->window_end_vpos
15086 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15087 }
15088
15089 w->window_end_valid = Qnil;
15090 w->desired_matrix->no_scrolling_p = 1;
15091
15092 #if GLYPH_DEBUG
15093 debug_method_add (w, "try_window_reusing_current_matrix 2");
15094 #endif
15095 return 1;
15096 }
15097
15098 return 0;
15099 }
15100
15101
15102 \f
15103 /************************************************************************
15104 Window redisplay reusing current matrix when buffer has changed
15105 ************************************************************************/
15106
15107 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
15108 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
15109 int *, int *));
15110 static struct glyph_row *
15111 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
15112 struct glyph_row *));
15113
15114
15115 /* Return the last row in MATRIX displaying text. If row START is
15116 non-null, start searching with that row. IT gives the dimensions
15117 of the display. Value is null if matrix is empty; otherwise it is
15118 a pointer to the row found. */
15119
15120 static struct glyph_row *
15121 find_last_row_displaying_text (matrix, it, start)
15122 struct glyph_matrix *matrix;
15123 struct it *it;
15124 struct glyph_row *start;
15125 {
15126 struct glyph_row *row, *row_found;
15127
15128 /* Set row_found to the last row in IT->w's current matrix
15129 displaying text. The loop looks funny but think of partially
15130 visible lines. */
15131 row_found = NULL;
15132 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15133 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15134 {
15135 xassert (row->enabled_p);
15136 row_found = row;
15137 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15138 break;
15139 ++row;
15140 }
15141
15142 return row_found;
15143 }
15144
15145
15146 /* Return the last row in the current matrix of W that is not affected
15147 by changes at the start of current_buffer that occurred since W's
15148 current matrix was built. Value is null if no such row exists.
15149
15150 BEG_UNCHANGED us the number of characters unchanged at the start of
15151 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15152 first changed character in current_buffer. Characters at positions <
15153 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15154 when the current matrix was built. */
15155
15156 static struct glyph_row *
15157 find_last_unchanged_at_beg_row (w)
15158 struct window *w;
15159 {
15160 int first_changed_pos = BEG + BEG_UNCHANGED;
15161 struct glyph_row *row;
15162 struct glyph_row *row_found = NULL;
15163 int yb = window_text_bottom_y (w);
15164
15165 /* Find the last row displaying unchanged text. */
15166 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15167 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15168 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15169 ++row)
15170 {
15171 if (/* If row ends before first_changed_pos, it is unchanged,
15172 except in some case. */
15173 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15174 /* When row ends in ZV and we write at ZV it is not
15175 unchanged. */
15176 && !row->ends_at_zv_p
15177 /* When first_changed_pos is the end of a continued line,
15178 row is not unchanged because it may be no longer
15179 continued. */
15180 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15181 && (row->continued_p
15182 || row->exact_window_width_line_p)))
15183 row_found = row;
15184
15185 /* Stop if last visible row. */
15186 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15187 break;
15188 }
15189
15190 return row_found;
15191 }
15192
15193
15194 /* Find the first glyph row in the current matrix of W that is not
15195 affected by changes at the end of current_buffer since the
15196 time W's current matrix was built.
15197
15198 Return in *DELTA the number of chars by which buffer positions in
15199 unchanged text at the end of current_buffer must be adjusted.
15200
15201 Return in *DELTA_BYTES the corresponding number of bytes.
15202
15203 Value is null if no such row exists, i.e. all rows are affected by
15204 changes. */
15205
15206 static struct glyph_row *
15207 find_first_unchanged_at_end_row (w, delta, delta_bytes)
15208 struct window *w;
15209 int *delta, *delta_bytes;
15210 {
15211 struct glyph_row *row;
15212 struct glyph_row *row_found = NULL;
15213
15214 *delta = *delta_bytes = 0;
15215
15216 /* Display must not have been paused, otherwise the current matrix
15217 is not up to date. */
15218 eassert (!NILP (w->window_end_valid));
15219
15220 /* A value of window_end_pos >= END_UNCHANGED means that the window
15221 end is in the range of changed text. If so, there is no
15222 unchanged row at the end of W's current matrix. */
15223 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15224 return NULL;
15225
15226 /* Set row to the last row in W's current matrix displaying text. */
15227 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15228
15229 /* If matrix is entirely empty, no unchanged row exists. */
15230 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15231 {
15232 /* The value of row is the last glyph row in the matrix having a
15233 meaningful buffer position in it. The end position of row
15234 corresponds to window_end_pos. This allows us to translate
15235 buffer positions in the current matrix to current buffer
15236 positions for characters not in changed text. */
15237 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15238 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15239 int last_unchanged_pos, last_unchanged_pos_old;
15240 struct glyph_row *first_text_row
15241 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15242
15243 *delta = Z - Z_old;
15244 *delta_bytes = Z_BYTE - Z_BYTE_old;
15245
15246 /* Set last_unchanged_pos to the buffer position of the last
15247 character in the buffer that has not been changed. Z is the
15248 index + 1 of the last character in current_buffer, i.e. by
15249 subtracting END_UNCHANGED we get the index of the last
15250 unchanged character, and we have to add BEG to get its buffer
15251 position. */
15252 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15253 last_unchanged_pos_old = last_unchanged_pos - *delta;
15254
15255 /* Search backward from ROW for a row displaying a line that
15256 starts at a minimum position >= last_unchanged_pos_old. */
15257 for (; row > first_text_row; --row)
15258 {
15259 /* This used to abort, but it can happen.
15260 It is ok to just stop the search instead here. KFS. */
15261 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15262 break;
15263
15264 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15265 row_found = row;
15266 }
15267 }
15268
15269 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15270
15271 return row_found;
15272 }
15273
15274
15275 /* Make sure that glyph rows in the current matrix of window W
15276 reference the same glyph memory as corresponding rows in the
15277 frame's frame matrix. This function is called after scrolling W's
15278 current matrix on a terminal frame in try_window_id and
15279 try_window_reusing_current_matrix. */
15280
15281 static void
15282 sync_frame_with_window_matrix_rows (w)
15283 struct window *w;
15284 {
15285 struct frame *f = XFRAME (w->frame);
15286 struct glyph_row *window_row, *window_row_end, *frame_row;
15287
15288 /* Preconditions: W must be a leaf window and full-width. Its frame
15289 must have a frame matrix. */
15290 xassert (NILP (w->hchild) && NILP (w->vchild));
15291 xassert (WINDOW_FULL_WIDTH_P (w));
15292 xassert (!FRAME_WINDOW_P (f));
15293
15294 /* If W is a full-width window, glyph pointers in W's current matrix
15295 have, by definition, to be the same as glyph pointers in the
15296 corresponding frame matrix. Note that frame matrices have no
15297 marginal areas (see build_frame_matrix). */
15298 window_row = w->current_matrix->rows;
15299 window_row_end = window_row + w->current_matrix->nrows;
15300 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15301 while (window_row < window_row_end)
15302 {
15303 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15304 struct glyph *end = window_row->glyphs[LAST_AREA];
15305
15306 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15307 frame_row->glyphs[TEXT_AREA] = start;
15308 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15309 frame_row->glyphs[LAST_AREA] = end;
15310
15311 /* Disable frame rows whose corresponding window rows have
15312 been disabled in try_window_id. */
15313 if (!window_row->enabled_p)
15314 frame_row->enabled_p = 0;
15315
15316 ++window_row, ++frame_row;
15317 }
15318 }
15319
15320
15321 /* Find the glyph row in window W containing CHARPOS. Consider all
15322 rows between START and END (not inclusive). END null means search
15323 all rows to the end of the display area of W. Value is the row
15324 containing CHARPOS or null. */
15325
15326 struct glyph_row *
15327 row_containing_pos (w, charpos, start, end, dy)
15328 struct window *w;
15329 int charpos;
15330 struct glyph_row *start, *end;
15331 int dy;
15332 {
15333 struct glyph_row *row = start;
15334 struct glyph_row *best_row = NULL;
15335 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15336 int last_y;
15337
15338 /* If we happen to start on a header-line, skip that. */
15339 if (row->mode_line_p)
15340 ++row;
15341
15342 if ((end && row >= end) || !row->enabled_p)
15343 return NULL;
15344
15345 last_y = window_text_bottom_y (w) - dy;
15346
15347 while (1)
15348 {
15349 /* Give up if we have gone too far. */
15350 if (end && row >= end)
15351 return NULL;
15352 /* This formerly returned if they were equal.
15353 I think that both quantities are of a "last plus one" type;
15354 if so, when they are equal, the row is within the screen. -- rms. */
15355 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15356 return NULL;
15357
15358 /* If it is in this row, return this row. */
15359 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15360 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15361 /* The end position of a row equals the start
15362 position of the next row. If CHARPOS is there, we
15363 would rather display it in the next line, except
15364 when this line ends in ZV. */
15365 && !row->ends_at_zv_p
15366 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15367 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15368 {
15369 struct glyph *g;
15370
15371 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15372 return row;
15373 /* In bidi-reordered rows, there could be several rows
15374 occluding point. We need to find the one which fits
15375 CHARPOS the best. */
15376 for (g = row->glyphs[TEXT_AREA];
15377 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15378 g++)
15379 {
15380 if (!STRINGP (g->object))
15381 {
15382 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15383 {
15384 mindif = eabs (g->charpos - charpos);
15385 best_row = row;
15386 }
15387 }
15388 }
15389 }
15390 else if (best_row)
15391 return best_row;
15392 ++row;
15393 }
15394 }
15395
15396
15397 /* Try to redisplay window W by reusing its existing display. W's
15398 current matrix must be up to date when this function is called,
15399 i.e. window_end_valid must not be nil.
15400
15401 Value is
15402
15403 1 if display has been updated
15404 0 if otherwise unsuccessful
15405 -1 if redisplay with same window start is known not to succeed
15406
15407 The following steps are performed:
15408
15409 1. Find the last row in the current matrix of W that is not
15410 affected by changes at the start of current_buffer. If no such row
15411 is found, give up.
15412
15413 2. Find the first row in W's current matrix that is not affected by
15414 changes at the end of current_buffer. Maybe there is no such row.
15415
15416 3. Display lines beginning with the row + 1 found in step 1 to the
15417 row found in step 2 or, if step 2 didn't find a row, to the end of
15418 the window.
15419
15420 4. If cursor is not known to appear on the window, give up.
15421
15422 5. If display stopped at the row found in step 2, scroll the
15423 display and current matrix as needed.
15424
15425 6. Maybe display some lines at the end of W, if we must. This can
15426 happen under various circumstances, like a partially visible line
15427 becoming fully visible, or because newly displayed lines are displayed
15428 in smaller font sizes.
15429
15430 7. Update W's window end information. */
15431
15432 static int
15433 try_window_id (w)
15434 struct window *w;
15435 {
15436 struct frame *f = XFRAME (w->frame);
15437 struct glyph_matrix *current_matrix = w->current_matrix;
15438 struct glyph_matrix *desired_matrix = w->desired_matrix;
15439 struct glyph_row *last_unchanged_at_beg_row;
15440 struct glyph_row *first_unchanged_at_end_row;
15441 struct glyph_row *row;
15442 struct glyph_row *bottom_row;
15443 int bottom_vpos;
15444 struct it it;
15445 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
15446 struct text_pos start_pos;
15447 struct run run;
15448 int first_unchanged_at_end_vpos = 0;
15449 struct glyph_row *last_text_row, *last_text_row_at_end;
15450 struct text_pos start;
15451 int first_changed_charpos, last_changed_charpos;
15452
15453 #if GLYPH_DEBUG
15454 if (inhibit_try_window_id)
15455 return 0;
15456 #endif
15457
15458 /* This is handy for debugging. */
15459 #if 0
15460 #define GIVE_UP(X) \
15461 do { \
15462 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15463 return 0; \
15464 } while (0)
15465 #else
15466 #define GIVE_UP(X) return 0
15467 #endif
15468
15469 SET_TEXT_POS_FROM_MARKER (start, w->start);
15470
15471 /* Don't use this for mini-windows because these can show
15472 messages and mini-buffers, and we don't handle that here. */
15473 if (MINI_WINDOW_P (w))
15474 GIVE_UP (1);
15475
15476 /* This flag is used to prevent redisplay optimizations. */
15477 if (windows_or_buffers_changed || cursor_type_changed)
15478 GIVE_UP (2);
15479
15480 /* Verify that narrowing has not changed.
15481 Also verify that we were not told to prevent redisplay optimizations.
15482 It would be nice to further
15483 reduce the number of cases where this prevents try_window_id. */
15484 if (current_buffer->clip_changed
15485 || current_buffer->prevent_redisplay_optimizations_p)
15486 GIVE_UP (3);
15487
15488 /* Window must either use window-based redisplay or be full width. */
15489 if (!FRAME_WINDOW_P (f)
15490 && (!FRAME_LINE_INS_DEL_OK (f)
15491 || !WINDOW_FULL_WIDTH_P (w)))
15492 GIVE_UP (4);
15493
15494 /* Give up if point is known NOT to appear in W. */
15495 if (PT < CHARPOS (start))
15496 GIVE_UP (5);
15497
15498 /* Another way to prevent redisplay optimizations. */
15499 if (XFASTINT (w->last_modified) == 0)
15500 GIVE_UP (6);
15501
15502 /* Verify that window is not hscrolled. */
15503 if (XFASTINT (w->hscroll) != 0)
15504 GIVE_UP (7);
15505
15506 /* Verify that display wasn't paused. */
15507 if (NILP (w->window_end_valid))
15508 GIVE_UP (8);
15509
15510 /* Can't use this if highlighting a region because a cursor movement
15511 will do more than just set the cursor. */
15512 if (!NILP (Vtransient_mark_mode)
15513 && !NILP (current_buffer->mark_active))
15514 GIVE_UP (9);
15515
15516 /* Likewise if highlighting trailing whitespace. */
15517 if (!NILP (Vshow_trailing_whitespace))
15518 GIVE_UP (11);
15519
15520 /* Likewise if showing a region. */
15521 if (!NILP (w->region_showing))
15522 GIVE_UP (10);
15523
15524 /* Can't use this if overlay arrow position and/or string have
15525 changed. */
15526 if (overlay_arrows_changed_p ())
15527 GIVE_UP (12);
15528
15529 /* When word-wrap is on, adding a space to the first word of a
15530 wrapped line can change the wrap position, altering the line
15531 above it. It might be worthwhile to handle this more
15532 intelligently, but for now just redisplay from scratch. */
15533 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15534 GIVE_UP (21);
15535
15536 /* Under bidi reordering, adding or deleting a character in the
15537 beginning of a paragraph, before the first strong directional
15538 character, can change the base direction of the paragraph (unless
15539 the buffer specifies a fixed paragraph direction), which will
15540 require to redisplay the whole paragraph. It might be worthwhile
15541 to find the paragraph limits and widen the range of redisplayed
15542 lines to that, but for now just give up this optimization and
15543 redisplay from scratch. */
15544 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15545 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15546 GIVE_UP (22);
15547
15548 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15549 only if buffer has really changed. The reason is that the gap is
15550 initially at Z for freshly visited files. The code below would
15551 set end_unchanged to 0 in that case. */
15552 if (MODIFF > SAVE_MODIFF
15553 /* This seems to happen sometimes after saving a buffer. */
15554 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15555 {
15556 if (GPT - BEG < BEG_UNCHANGED)
15557 BEG_UNCHANGED = GPT - BEG;
15558 if (Z - GPT < END_UNCHANGED)
15559 END_UNCHANGED = Z - GPT;
15560 }
15561
15562 /* The position of the first and last character that has been changed. */
15563 first_changed_charpos = BEG + BEG_UNCHANGED;
15564 last_changed_charpos = Z - END_UNCHANGED;
15565
15566 /* If window starts after a line end, and the last change is in
15567 front of that newline, then changes don't affect the display.
15568 This case happens with stealth-fontification. Note that although
15569 the display is unchanged, glyph positions in the matrix have to
15570 be adjusted, of course. */
15571 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15572 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15573 && ((last_changed_charpos < CHARPOS (start)
15574 && CHARPOS (start) == BEGV)
15575 || (last_changed_charpos < CHARPOS (start) - 1
15576 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15577 {
15578 int Z_old, delta, Z_BYTE_old, delta_bytes;
15579 struct glyph_row *r0;
15580
15581 /* Compute how many chars/bytes have been added to or removed
15582 from the buffer. */
15583 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15584 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15585 delta = Z - Z_old;
15586 delta_bytes = Z_BYTE - Z_BYTE_old;
15587
15588 /* Give up if PT is not in the window. Note that it already has
15589 been checked at the start of try_window_id that PT is not in
15590 front of the window start. */
15591 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15592 GIVE_UP (13);
15593
15594 /* If window start is unchanged, we can reuse the whole matrix
15595 as is, after adjusting glyph positions. No need to compute
15596 the window end again, since its offset from Z hasn't changed. */
15597 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15598 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15599 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15600 /* PT must not be in a partially visible line. */
15601 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15602 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15603 {
15604 /* Adjust positions in the glyph matrix. */
15605 if (delta || delta_bytes)
15606 {
15607 struct glyph_row *r1
15608 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15609 increment_matrix_positions (w->current_matrix,
15610 MATRIX_ROW_VPOS (r0, current_matrix),
15611 MATRIX_ROW_VPOS (r1, current_matrix),
15612 delta, delta_bytes);
15613 }
15614
15615 /* Set the cursor. */
15616 row = row_containing_pos (w, PT, r0, NULL, 0);
15617 if (row)
15618 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15619 else
15620 abort ();
15621 return 1;
15622 }
15623 }
15624
15625 /* Handle the case that changes are all below what is displayed in
15626 the window, and that PT is in the window. This shortcut cannot
15627 be taken if ZV is visible in the window, and text has been added
15628 there that is visible in the window. */
15629 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15630 /* ZV is not visible in the window, or there are no
15631 changes at ZV, actually. */
15632 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15633 || first_changed_charpos == last_changed_charpos))
15634 {
15635 struct glyph_row *r0;
15636
15637 /* Give up if PT is not in the window. Note that it already has
15638 been checked at the start of try_window_id that PT is not in
15639 front of the window start. */
15640 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15641 GIVE_UP (14);
15642
15643 /* If window start is unchanged, we can reuse the whole matrix
15644 as is, without changing glyph positions since no text has
15645 been added/removed in front of the window end. */
15646 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15647 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15648 /* PT must not be in a partially visible line. */
15649 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15650 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15651 {
15652 /* We have to compute the window end anew since text
15653 can have been added/removed after it. */
15654 w->window_end_pos
15655 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15656 w->window_end_bytepos
15657 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15658
15659 /* Set the cursor. */
15660 row = row_containing_pos (w, PT, r0, NULL, 0);
15661 if (row)
15662 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15663 else
15664 abort ();
15665 return 2;
15666 }
15667 }
15668
15669 /* Give up if window start is in the changed area.
15670
15671 The condition used to read
15672
15673 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15674
15675 but why that was tested escapes me at the moment. */
15676 if (CHARPOS (start) >= first_changed_charpos
15677 && CHARPOS (start) <= last_changed_charpos)
15678 GIVE_UP (15);
15679
15680 /* Check that window start agrees with the start of the first glyph
15681 row in its current matrix. Check this after we know the window
15682 start is not in changed text, otherwise positions would not be
15683 comparable. */
15684 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15685 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15686 GIVE_UP (16);
15687
15688 /* Give up if the window ends in strings. Overlay strings
15689 at the end are difficult to handle, so don't try. */
15690 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15691 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15692 GIVE_UP (20);
15693
15694 /* Compute the position at which we have to start displaying new
15695 lines. Some of the lines at the top of the window might be
15696 reusable because they are not displaying changed text. Find the
15697 last row in W's current matrix not affected by changes at the
15698 start of current_buffer. Value is null if changes start in the
15699 first line of window. */
15700 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15701 if (last_unchanged_at_beg_row)
15702 {
15703 /* Avoid starting to display in the moddle of a character, a TAB
15704 for instance. This is easier than to set up the iterator
15705 exactly, and it's not a frequent case, so the additional
15706 effort wouldn't really pay off. */
15707 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15708 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15709 && last_unchanged_at_beg_row > w->current_matrix->rows)
15710 --last_unchanged_at_beg_row;
15711
15712 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15713 GIVE_UP (17);
15714
15715 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15716 GIVE_UP (18);
15717 start_pos = it.current.pos;
15718
15719 /* Start displaying new lines in the desired matrix at the same
15720 vpos we would use in the current matrix, i.e. below
15721 last_unchanged_at_beg_row. */
15722 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15723 current_matrix);
15724 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15725 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15726
15727 xassert (it.hpos == 0 && it.current_x == 0);
15728 }
15729 else
15730 {
15731 /* There are no reusable lines at the start of the window.
15732 Start displaying in the first text line. */
15733 start_display (&it, w, start);
15734 it.vpos = it.first_vpos;
15735 start_pos = it.current.pos;
15736 }
15737
15738 /* Find the first row that is not affected by changes at the end of
15739 the buffer. Value will be null if there is no unchanged row, in
15740 which case we must redisplay to the end of the window. delta
15741 will be set to the value by which buffer positions beginning with
15742 first_unchanged_at_end_row have to be adjusted due to text
15743 changes. */
15744 first_unchanged_at_end_row
15745 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15746 IF_DEBUG (debug_delta = delta);
15747 IF_DEBUG (debug_delta_bytes = delta_bytes);
15748
15749 /* Set stop_pos to the buffer position up to which we will have to
15750 display new lines. If first_unchanged_at_end_row != NULL, this
15751 is the buffer position of the start of the line displayed in that
15752 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15753 that we don't stop at a buffer position. */
15754 stop_pos = 0;
15755 if (first_unchanged_at_end_row)
15756 {
15757 xassert (last_unchanged_at_beg_row == NULL
15758 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15759
15760 /* If this is a continuation line, move forward to the next one
15761 that isn't. Changes in lines above affect this line.
15762 Caution: this may move first_unchanged_at_end_row to a row
15763 not displaying text. */
15764 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15765 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15766 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15767 < it.last_visible_y))
15768 ++first_unchanged_at_end_row;
15769
15770 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15771 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15772 >= it.last_visible_y))
15773 first_unchanged_at_end_row = NULL;
15774 else
15775 {
15776 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15777 + delta);
15778 first_unchanged_at_end_vpos
15779 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15780 xassert (stop_pos >= Z - END_UNCHANGED);
15781 }
15782 }
15783 else if (last_unchanged_at_beg_row == NULL)
15784 GIVE_UP (19);
15785
15786
15787 #if GLYPH_DEBUG
15788
15789 /* Either there is no unchanged row at the end, or the one we have
15790 now displays text. This is a necessary condition for the window
15791 end pos calculation at the end of this function. */
15792 xassert (first_unchanged_at_end_row == NULL
15793 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15794
15795 debug_last_unchanged_at_beg_vpos
15796 = (last_unchanged_at_beg_row
15797 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15798 : -1);
15799 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15800
15801 #endif /* GLYPH_DEBUG != 0 */
15802
15803
15804 /* Display new lines. Set last_text_row to the last new line
15805 displayed which has text on it, i.e. might end up as being the
15806 line where the window_end_vpos is. */
15807 w->cursor.vpos = -1;
15808 last_text_row = NULL;
15809 overlay_arrow_seen = 0;
15810 while (it.current_y < it.last_visible_y
15811 && !fonts_changed_p
15812 && (first_unchanged_at_end_row == NULL
15813 || IT_CHARPOS (it) < stop_pos))
15814 {
15815 if (display_line (&it))
15816 last_text_row = it.glyph_row - 1;
15817 }
15818
15819 if (fonts_changed_p)
15820 return -1;
15821
15822
15823 /* Compute differences in buffer positions, y-positions etc. for
15824 lines reused at the bottom of the window. Compute what we can
15825 scroll. */
15826 if (first_unchanged_at_end_row
15827 /* No lines reused because we displayed everything up to the
15828 bottom of the window. */
15829 && it.current_y < it.last_visible_y)
15830 {
15831 dvpos = (it.vpos
15832 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15833 current_matrix));
15834 dy = it.current_y - first_unchanged_at_end_row->y;
15835 run.current_y = first_unchanged_at_end_row->y;
15836 run.desired_y = run.current_y + dy;
15837 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15838 }
15839 else
15840 {
15841 delta = delta_bytes = dvpos = dy
15842 = run.current_y = run.desired_y = run.height = 0;
15843 first_unchanged_at_end_row = NULL;
15844 }
15845 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15846
15847
15848 /* Find the cursor if not already found. We have to decide whether
15849 PT will appear on this window (it sometimes doesn't, but this is
15850 not a very frequent case.) This decision has to be made before
15851 the current matrix is altered. A value of cursor.vpos < 0 means
15852 that PT is either in one of the lines beginning at
15853 first_unchanged_at_end_row or below the window. Don't care for
15854 lines that might be displayed later at the window end; as
15855 mentioned, this is not a frequent case. */
15856 if (w->cursor.vpos < 0)
15857 {
15858 /* Cursor in unchanged rows at the top? */
15859 if (PT < CHARPOS (start_pos)
15860 && last_unchanged_at_beg_row)
15861 {
15862 row = row_containing_pos (w, PT,
15863 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15864 last_unchanged_at_beg_row + 1, 0);
15865 if (row)
15866 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15867 }
15868
15869 /* Start from first_unchanged_at_end_row looking for PT. */
15870 else if (first_unchanged_at_end_row)
15871 {
15872 row = row_containing_pos (w, PT - delta,
15873 first_unchanged_at_end_row, NULL, 0);
15874 if (row)
15875 set_cursor_from_row (w, row, w->current_matrix, delta,
15876 delta_bytes, dy, dvpos);
15877 }
15878
15879 /* Give up if cursor was not found. */
15880 if (w->cursor.vpos < 0)
15881 {
15882 clear_glyph_matrix (w->desired_matrix);
15883 return -1;
15884 }
15885 }
15886
15887 /* Don't let the cursor end in the scroll margins. */
15888 {
15889 int this_scroll_margin, cursor_height;
15890
15891 this_scroll_margin = max (0, scroll_margin);
15892 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15893 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15894 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15895
15896 if ((w->cursor.y < this_scroll_margin
15897 && CHARPOS (start) > BEGV)
15898 /* Old redisplay didn't take scroll margin into account at the bottom,
15899 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15900 || (w->cursor.y + (make_cursor_line_fully_visible_p
15901 ? cursor_height + this_scroll_margin
15902 : 1)) > it.last_visible_y)
15903 {
15904 w->cursor.vpos = -1;
15905 clear_glyph_matrix (w->desired_matrix);
15906 return -1;
15907 }
15908 }
15909
15910 /* Scroll the display. Do it before changing the current matrix so
15911 that xterm.c doesn't get confused about where the cursor glyph is
15912 found. */
15913 if (dy && run.height)
15914 {
15915 update_begin (f);
15916
15917 if (FRAME_WINDOW_P (f))
15918 {
15919 FRAME_RIF (f)->update_window_begin_hook (w);
15920 FRAME_RIF (f)->clear_window_mouse_face (w);
15921 FRAME_RIF (f)->scroll_run_hook (w, &run);
15922 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15923 }
15924 else
15925 {
15926 /* Terminal frame. In this case, dvpos gives the number of
15927 lines to scroll by; dvpos < 0 means scroll up. */
15928 int first_unchanged_at_end_vpos
15929 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15930 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15931 int end = (WINDOW_TOP_EDGE_LINE (w)
15932 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15933 + window_internal_height (w));
15934
15935 /* Perform the operation on the screen. */
15936 if (dvpos > 0)
15937 {
15938 /* Scroll last_unchanged_at_beg_row to the end of the
15939 window down dvpos lines. */
15940 set_terminal_window (f, end);
15941
15942 /* On dumb terminals delete dvpos lines at the end
15943 before inserting dvpos empty lines. */
15944 if (!FRAME_SCROLL_REGION_OK (f))
15945 ins_del_lines (f, end - dvpos, -dvpos);
15946
15947 /* Insert dvpos empty lines in front of
15948 last_unchanged_at_beg_row. */
15949 ins_del_lines (f, from, dvpos);
15950 }
15951 else if (dvpos < 0)
15952 {
15953 /* Scroll up last_unchanged_at_beg_vpos to the end of
15954 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15955 set_terminal_window (f, end);
15956
15957 /* Delete dvpos lines in front of
15958 last_unchanged_at_beg_vpos. ins_del_lines will set
15959 the cursor to the given vpos and emit |dvpos| delete
15960 line sequences. */
15961 ins_del_lines (f, from + dvpos, dvpos);
15962
15963 /* On a dumb terminal insert dvpos empty lines at the
15964 end. */
15965 if (!FRAME_SCROLL_REGION_OK (f))
15966 ins_del_lines (f, end + dvpos, -dvpos);
15967 }
15968
15969 set_terminal_window (f, 0);
15970 }
15971
15972 update_end (f);
15973 }
15974
15975 /* Shift reused rows of the current matrix to the right position.
15976 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15977 text. */
15978 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15979 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15980 if (dvpos < 0)
15981 {
15982 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15983 bottom_vpos, dvpos);
15984 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15985 bottom_vpos, 0);
15986 }
15987 else if (dvpos > 0)
15988 {
15989 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15990 bottom_vpos, dvpos);
15991 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15992 first_unchanged_at_end_vpos + dvpos, 0);
15993 }
15994
15995 /* For frame-based redisplay, make sure that current frame and window
15996 matrix are in sync with respect to glyph memory. */
15997 if (!FRAME_WINDOW_P (f))
15998 sync_frame_with_window_matrix_rows (w);
15999
16000 /* Adjust buffer positions in reused rows. */
16001 if (delta || delta_bytes)
16002 increment_matrix_positions (current_matrix,
16003 first_unchanged_at_end_vpos + dvpos,
16004 bottom_vpos, delta, delta_bytes);
16005
16006 /* Adjust Y positions. */
16007 if (dy)
16008 shift_glyph_matrix (w, current_matrix,
16009 first_unchanged_at_end_vpos + dvpos,
16010 bottom_vpos, dy);
16011
16012 if (first_unchanged_at_end_row)
16013 {
16014 first_unchanged_at_end_row += dvpos;
16015 if (first_unchanged_at_end_row->y >= it.last_visible_y
16016 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16017 first_unchanged_at_end_row = NULL;
16018 }
16019
16020 /* If scrolling up, there may be some lines to display at the end of
16021 the window. */
16022 last_text_row_at_end = NULL;
16023 if (dy < 0)
16024 {
16025 /* Scrolling up can leave for example a partially visible line
16026 at the end of the window to be redisplayed. */
16027 /* Set last_row to the glyph row in the current matrix where the
16028 window end line is found. It has been moved up or down in
16029 the matrix by dvpos. */
16030 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16031 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16032
16033 /* If last_row is the window end line, it should display text. */
16034 xassert (last_row->displays_text_p);
16035
16036 /* If window end line was partially visible before, begin
16037 displaying at that line. Otherwise begin displaying with the
16038 line following it. */
16039 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16040 {
16041 init_to_row_start (&it, w, last_row);
16042 it.vpos = last_vpos;
16043 it.current_y = last_row->y;
16044 }
16045 else
16046 {
16047 init_to_row_end (&it, w, last_row);
16048 it.vpos = 1 + last_vpos;
16049 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16050 ++last_row;
16051 }
16052
16053 /* We may start in a continuation line. If so, we have to
16054 get the right continuation_lines_width and current_x. */
16055 it.continuation_lines_width = last_row->continuation_lines_width;
16056 it.hpos = it.current_x = 0;
16057
16058 /* Display the rest of the lines at the window end. */
16059 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16060 while (it.current_y < it.last_visible_y
16061 && !fonts_changed_p)
16062 {
16063 /* Is it always sure that the display agrees with lines in
16064 the current matrix? I don't think so, so we mark rows
16065 displayed invalid in the current matrix by setting their
16066 enabled_p flag to zero. */
16067 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16068 if (display_line (&it))
16069 last_text_row_at_end = it.glyph_row - 1;
16070 }
16071 }
16072
16073 /* Update window_end_pos and window_end_vpos. */
16074 if (first_unchanged_at_end_row
16075 && !last_text_row_at_end)
16076 {
16077 /* Window end line if one of the preserved rows from the current
16078 matrix. Set row to the last row displaying text in current
16079 matrix starting at first_unchanged_at_end_row, after
16080 scrolling. */
16081 xassert (first_unchanged_at_end_row->displays_text_p);
16082 row = find_last_row_displaying_text (w->current_matrix, &it,
16083 first_unchanged_at_end_row);
16084 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16085
16086 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16087 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16088 w->window_end_vpos
16089 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16090 xassert (w->window_end_bytepos >= 0);
16091 IF_DEBUG (debug_method_add (w, "A"));
16092 }
16093 else if (last_text_row_at_end)
16094 {
16095 w->window_end_pos
16096 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16097 w->window_end_bytepos
16098 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16099 w->window_end_vpos
16100 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16101 xassert (w->window_end_bytepos >= 0);
16102 IF_DEBUG (debug_method_add (w, "B"));
16103 }
16104 else if (last_text_row)
16105 {
16106 /* We have displayed either to the end of the window or at the
16107 end of the window, i.e. the last row with text is to be found
16108 in the desired matrix. */
16109 w->window_end_pos
16110 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16111 w->window_end_bytepos
16112 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16113 w->window_end_vpos
16114 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16115 xassert (w->window_end_bytepos >= 0);
16116 }
16117 else if (first_unchanged_at_end_row == NULL
16118 && last_text_row == NULL
16119 && last_text_row_at_end == NULL)
16120 {
16121 /* Displayed to end of window, but no line containing text was
16122 displayed. Lines were deleted at the end of the window. */
16123 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16124 int vpos = XFASTINT (w->window_end_vpos);
16125 struct glyph_row *current_row = current_matrix->rows + vpos;
16126 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16127
16128 for (row = NULL;
16129 row == NULL && vpos >= first_vpos;
16130 --vpos, --current_row, --desired_row)
16131 {
16132 if (desired_row->enabled_p)
16133 {
16134 if (desired_row->displays_text_p)
16135 row = desired_row;
16136 }
16137 else if (current_row->displays_text_p)
16138 row = current_row;
16139 }
16140
16141 xassert (row != NULL);
16142 w->window_end_vpos = make_number (vpos + 1);
16143 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16144 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16145 xassert (w->window_end_bytepos >= 0);
16146 IF_DEBUG (debug_method_add (w, "C"));
16147 }
16148 else
16149 abort ();
16150
16151 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16152 debug_end_vpos = XFASTINT (w->window_end_vpos));
16153
16154 /* Record that display has not been completed. */
16155 w->window_end_valid = Qnil;
16156 w->desired_matrix->no_scrolling_p = 1;
16157 return 3;
16158
16159 #undef GIVE_UP
16160 }
16161
16162
16163 \f
16164 /***********************************************************************
16165 More debugging support
16166 ***********************************************************************/
16167
16168 #if GLYPH_DEBUG
16169
16170 void dump_glyph_row P_ ((struct glyph_row *, int, int));
16171 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
16172 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
16173
16174
16175 /* Dump the contents of glyph matrix MATRIX on stderr.
16176
16177 GLYPHS 0 means don't show glyph contents.
16178 GLYPHS 1 means show glyphs in short form
16179 GLYPHS > 1 means show glyphs in long form. */
16180
16181 void
16182 dump_glyph_matrix (matrix, glyphs)
16183 struct glyph_matrix *matrix;
16184 int glyphs;
16185 {
16186 int i;
16187 for (i = 0; i < matrix->nrows; ++i)
16188 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16189 }
16190
16191
16192 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16193 the glyph row and area where the glyph comes from. */
16194
16195 void
16196 dump_glyph (row, glyph, area)
16197 struct glyph_row *row;
16198 struct glyph *glyph;
16199 int area;
16200 {
16201 if (glyph->type == CHAR_GLYPH)
16202 {
16203 fprintf (stderr,
16204 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16205 glyph - row->glyphs[TEXT_AREA],
16206 'C',
16207 glyph->charpos,
16208 (BUFFERP (glyph->object)
16209 ? 'B'
16210 : (STRINGP (glyph->object)
16211 ? 'S'
16212 : '-')),
16213 glyph->pixel_width,
16214 glyph->u.ch,
16215 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16216 ? glyph->u.ch
16217 : '.'),
16218 glyph->face_id,
16219 glyph->left_box_line_p,
16220 glyph->right_box_line_p);
16221 }
16222 else if (glyph->type == STRETCH_GLYPH)
16223 {
16224 fprintf (stderr,
16225 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16226 glyph - row->glyphs[TEXT_AREA],
16227 'S',
16228 glyph->charpos,
16229 (BUFFERP (glyph->object)
16230 ? 'B'
16231 : (STRINGP (glyph->object)
16232 ? 'S'
16233 : '-')),
16234 glyph->pixel_width,
16235 0,
16236 '.',
16237 glyph->face_id,
16238 glyph->left_box_line_p,
16239 glyph->right_box_line_p);
16240 }
16241 else if (glyph->type == IMAGE_GLYPH)
16242 {
16243 fprintf (stderr,
16244 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16245 glyph - row->glyphs[TEXT_AREA],
16246 'I',
16247 glyph->charpos,
16248 (BUFFERP (glyph->object)
16249 ? 'B'
16250 : (STRINGP (glyph->object)
16251 ? 'S'
16252 : '-')),
16253 glyph->pixel_width,
16254 glyph->u.img_id,
16255 '.',
16256 glyph->face_id,
16257 glyph->left_box_line_p,
16258 glyph->right_box_line_p);
16259 }
16260 else if (glyph->type == COMPOSITE_GLYPH)
16261 {
16262 fprintf (stderr,
16263 " %5d %4c %6d %c %3d 0x%05x",
16264 glyph - row->glyphs[TEXT_AREA],
16265 '+',
16266 glyph->charpos,
16267 (BUFFERP (glyph->object)
16268 ? 'B'
16269 : (STRINGP (glyph->object)
16270 ? 'S'
16271 : '-')),
16272 glyph->pixel_width,
16273 glyph->u.cmp.id);
16274 if (glyph->u.cmp.automatic)
16275 fprintf (stderr,
16276 "[%d-%d]",
16277 glyph->u.cmp.from, glyph->u.cmp.to);
16278 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16279 glyph->face_id,
16280 glyph->left_box_line_p,
16281 glyph->right_box_line_p);
16282 }
16283 }
16284
16285
16286 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16287 GLYPHS 0 means don't show glyph contents.
16288 GLYPHS 1 means show glyphs in short form
16289 GLYPHS > 1 means show glyphs in long form. */
16290
16291 void
16292 dump_glyph_row (row, vpos, glyphs)
16293 struct glyph_row *row;
16294 int vpos, glyphs;
16295 {
16296 if (glyphs != 1)
16297 {
16298 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16299 fprintf (stderr, "======================================================================\n");
16300
16301 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16302 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16303 vpos,
16304 MATRIX_ROW_START_CHARPOS (row),
16305 MATRIX_ROW_END_CHARPOS (row),
16306 row->used[TEXT_AREA],
16307 row->contains_overlapping_glyphs_p,
16308 row->enabled_p,
16309 row->truncated_on_left_p,
16310 row->truncated_on_right_p,
16311 row->continued_p,
16312 MATRIX_ROW_CONTINUATION_LINE_P (row),
16313 row->displays_text_p,
16314 row->ends_at_zv_p,
16315 row->fill_line_p,
16316 row->ends_in_middle_of_char_p,
16317 row->starts_in_middle_of_char_p,
16318 row->mouse_face_p,
16319 row->x,
16320 row->y,
16321 row->pixel_width,
16322 row->height,
16323 row->visible_height,
16324 row->ascent,
16325 row->phys_ascent);
16326 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16327 row->end.overlay_string_index,
16328 row->continuation_lines_width);
16329 fprintf (stderr, "%9d %5d\n",
16330 CHARPOS (row->start.string_pos),
16331 CHARPOS (row->end.string_pos));
16332 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16333 row->end.dpvec_index);
16334 }
16335
16336 if (glyphs > 1)
16337 {
16338 int area;
16339
16340 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16341 {
16342 struct glyph *glyph = row->glyphs[area];
16343 struct glyph *glyph_end = glyph + row->used[area];
16344
16345 /* Glyph for a line end in text. */
16346 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16347 ++glyph_end;
16348
16349 if (glyph < glyph_end)
16350 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16351
16352 for (; glyph < glyph_end; ++glyph)
16353 dump_glyph (row, glyph, area);
16354 }
16355 }
16356 else if (glyphs == 1)
16357 {
16358 int area;
16359
16360 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16361 {
16362 char *s = (char *) alloca (row->used[area] + 1);
16363 int i;
16364
16365 for (i = 0; i < row->used[area]; ++i)
16366 {
16367 struct glyph *glyph = row->glyphs[area] + i;
16368 if (glyph->type == CHAR_GLYPH
16369 && glyph->u.ch < 0x80
16370 && glyph->u.ch >= ' ')
16371 s[i] = glyph->u.ch;
16372 else
16373 s[i] = '.';
16374 }
16375
16376 s[i] = '\0';
16377 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16378 }
16379 }
16380 }
16381
16382
16383 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16384 Sdump_glyph_matrix, 0, 1, "p",
16385 doc: /* Dump the current matrix of the selected window to stderr.
16386 Shows contents of glyph row structures. With non-nil
16387 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16388 glyphs in short form, otherwise show glyphs in long form. */)
16389 (glyphs)
16390 Lisp_Object glyphs;
16391 {
16392 struct window *w = XWINDOW (selected_window);
16393 struct buffer *buffer = XBUFFER (w->buffer);
16394
16395 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16396 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16397 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16398 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16399 fprintf (stderr, "=============================================\n");
16400 dump_glyph_matrix (w->current_matrix,
16401 NILP (glyphs) ? 0 : XINT (glyphs));
16402 return Qnil;
16403 }
16404
16405
16406 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16407 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16408 ()
16409 {
16410 struct frame *f = XFRAME (selected_frame);
16411 dump_glyph_matrix (f->current_matrix, 1);
16412 return Qnil;
16413 }
16414
16415
16416 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16417 doc: /* Dump glyph row ROW to stderr.
16418 GLYPH 0 means don't dump glyphs.
16419 GLYPH 1 means dump glyphs in short form.
16420 GLYPH > 1 or omitted means dump glyphs in long form. */)
16421 (row, glyphs)
16422 Lisp_Object row, glyphs;
16423 {
16424 struct glyph_matrix *matrix;
16425 int vpos;
16426
16427 CHECK_NUMBER (row);
16428 matrix = XWINDOW (selected_window)->current_matrix;
16429 vpos = XINT (row);
16430 if (vpos >= 0 && vpos < matrix->nrows)
16431 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16432 vpos,
16433 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16434 return Qnil;
16435 }
16436
16437
16438 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16439 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16440 GLYPH 0 means don't dump glyphs.
16441 GLYPH 1 means dump glyphs in short form.
16442 GLYPH > 1 or omitted means dump glyphs in long form. */)
16443 (row, glyphs)
16444 Lisp_Object row, glyphs;
16445 {
16446 struct frame *sf = SELECTED_FRAME ();
16447 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16448 int vpos;
16449
16450 CHECK_NUMBER (row);
16451 vpos = XINT (row);
16452 if (vpos >= 0 && vpos < m->nrows)
16453 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16454 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16455 return Qnil;
16456 }
16457
16458
16459 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16460 doc: /* Toggle tracing of redisplay.
16461 With ARG, turn tracing on if and only if ARG is positive. */)
16462 (arg)
16463 Lisp_Object arg;
16464 {
16465 if (NILP (arg))
16466 trace_redisplay_p = !trace_redisplay_p;
16467 else
16468 {
16469 arg = Fprefix_numeric_value (arg);
16470 trace_redisplay_p = XINT (arg) > 0;
16471 }
16472
16473 return Qnil;
16474 }
16475
16476
16477 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16478 doc: /* Like `format', but print result to stderr.
16479 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16480 (nargs, args)
16481 int nargs;
16482 Lisp_Object *args;
16483 {
16484 Lisp_Object s = Fformat (nargs, args);
16485 fprintf (stderr, "%s", SDATA (s));
16486 return Qnil;
16487 }
16488
16489 #endif /* GLYPH_DEBUG */
16490
16491
16492 \f
16493 /***********************************************************************
16494 Building Desired Matrix Rows
16495 ***********************************************************************/
16496
16497 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16498 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16499
16500 static struct glyph_row *
16501 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
16502 struct window *w;
16503 Lisp_Object overlay_arrow_string;
16504 {
16505 struct frame *f = XFRAME (WINDOW_FRAME (w));
16506 struct buffer *buffer = XBUFFER (w->buffer);
16507 struct buffer *old = current_buffer;
16508 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16509 int arrow_len = SCHARS (overlay_arrow_string);
16510 const unsigned char *arrow_end = arrow_string + arrow_len;
16511 const unsigned char *p;
16512 struct it it;
16513 int multibyte_p;
16514 int n_glyphs_before;
16515
16516 set_buffer_temp (buffer);
16517 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16518 it.glyph_row->used[TEXT_AREA] = 0;
16519 SET_TEXT_POS (it.position, 0, 0);
16520
16521 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16522 p = arrow_string;
16523 while (p < arrow_end)
16524 {
16525 Lisp_Object face, ilisp;
16526
16527 /* Get the next character. */
16528 if (multibyte_p)
16529 it.c = string_char_and_length (p, &it.len);
16530 else
16531 it.c = *p, it.len = 1;
16532 p += it.len;
16533
16534 /* Get its face. */
16535 ilisp = make_number (p - arrow_string);
16536 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16537 it.face_id = compute_char_face (f, it.c, face);
16538
16539 /* Compute its width, get its glyphs. */
16540 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16541 SET_TEXT_POS (it.position, -1, -1);
16542 PRODUCE_GLYPHS (&it);
16543
16544 /* If this character doesn't fit any more in the line, we have
16545 to remove some glyphs. */
16546 if (it.current_x > it.last_visible_x)
16547 {
16548 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16549 break;
16550 }
16551 }
16552
16553 set_buffer_temp (old);
16554 return it.glyph_row;
16555 }
16556
16557
16558 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16559 glyphs are only inserted for terminal frames since we can't really
16560 win with truncation glyphs when partially visible glyphs are
16561 involved. Which glyphs to insert is determined by
16562 produce_special_glyphs. */
16563
16564 static void
16565 insert_left_trunc_glyphs (it)
16566 struct it *it;
16567 {
16568 struct it truncate_it;
16569 struct glyph *from, *end, *to, *toend;
16570
16571 xassert (!FRAME_WINDOW_P (it->f));
16572
16573 /* Get the truncation glyphs. */
16574 truncate_it = *it;
16575 truncate_it.current_x = 0;
16576 truncate_it.face_id = DEFAULT_FACE_ID;
16577 truncate_it.glyph_row = &scratch_glyph_row;
16578 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16579 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16580 truncate_it.object = make_number (0);
16581 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16582
16583 /* Overwrite glyphs from IT with truncation glyphs. */
16584 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16585 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16586 to = it->glyph_row->glyphs[TEXT_AREA];
16587 toend = to + it->glyph_row->used[TEXT_AREA];
16588
16589 while (from < end)
16590 *to++ = *from++;
16591
16592 /* There may be padding glyphs left over. Overwrite them too. */
16593 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16594 {
16595 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16596 while (from < end)
16597 *to++ = *from++;
16598 }
16599
16600 if (to > toend)
16601 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16602 }
16603
16604
16605 /* Compute the pixel height and width of IT->glyph_row.
16606
16607 Most of the time, ascent and height of a display line will be equal
16608 to the max_ascent and max_height values of the display iterator
16609 structure. This is not the case if
16610
16611 1. We hit ZV without displaying anything. In this case, max_ascent
16612 and max_height will be zero.
16613
16614 2. We have some glyphs that don't contribute to the line height.
16615 (The glyph row flag contributes_to_line_height_p is for future
16616 pixmap extensions).
16617
16618 The first case is easily covered by using default values because in
16619 these cases, the line height does not really matter, except that it
16620 must not be zero. */
16621
16622 static void
16623 compute_line_metrics (it)
16624 struct it *it;
16625 {
16626 struct glyph_row *row = it->glyph_row;
16627 int area, i;
16628
16629 if (FRAME_WINDOW_P (it->f))
16630 {
16631 int i, min_y, max_y;
16632
16633 /* The line may consist of one space only, that was added to
16634 place the cursor on it. If so, the row's height hasn't been
16635 computed yet. */
16636 if (row->height == 0)
16637 {
16638 if (it->max_ascent + it->max_descent == 0)
16639 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16640 row->ascent = it->max_ascent;
16641 row->height = it->max_ascent + it->max_descent;
16642 row->phys_ascent = it->max_phys_ascent;
16643 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16644 row->extra_line_spacing = it->max_extra_line_spacing;
16645 }
16646
16647 /* Compute the width of this line. */
16648 row->pixel_width = row->x;
16649 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16650 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16651
16652 xassert (row->pixel_width >= 0);
16653 xassert (row->ascent >= 0 && row->height > 0);
16654
16655 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16656 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16657
16658 /* If first line's physical ascent is larger than its logical
16659 ascent, use the physical ascent, and make the row taller.
16660 This makes accented characters fully visible. */
16661 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16662 && row->phys_ascent > row->ascent)
16663 {
16664 row->height += row->phys_ascent - row->ascent;
16665 row->ascent = row->phys_ascent;
16666 }
16667
16668 /* Compute how much of the line is visible. */
16669 row->visible_height = row->height;
16670
16671 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16672 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16673
16674 if (row->y < min_y)
16675 row->visible_height -= min_y - row->y;
16676 if (row->y + row->height > max_y)
16677 row->visible_height -= row->y + row->height - max_y;
16678 }
16679 else
16680 {
16681 row->pixel_width = row->used[TEXT_AREA];
16682 if (row->continued_p)
16683 row->pixel_width -= it->continuation_pixel_width;
16684 else if (row->truncated_on_right_p)
16685 row->pixel_width -= it->truncation_pixel_width;
16686 row->ascent = row->phys_ascent = 0;
16687 row->height = row->phys_height = row->visible_height = 1;
16688 row->extra_line_spacing = 0;
16689 }
16690
16691 /* Compute a hash code for this row. */
16692 row->hash = 0;
16693 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16694 for (i = 0; i < row->used[area]; ++i)
16695 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16696 + row->glyphs[area][i].u.val
16697 + row->glyphs[area][i].face_id
16698 + row->glyphs[area][i].padding_p
16699 + (row->glyphs[area][i].type << 2));
16700
16701 it->max_ascent = it->max_descent = 0;
16702 it->max_phys_ascent = it->max_phys_descent = 0;
16703 }
16704
16705
16706 /* Append one space to the glyph row of iterator IT if doing a
16707 window-based redisplay. The space has the same face as
16708 IT->face_id. Value is non-zero if a space was added.
16709
16710 This function is called to make sure that there is always one glyph
16711 at the end of a glyph row that the cursor can be set on under
16712 window-systems. (If there weren't such a glyph we would not know
16713 how wide and tall a box cursor should be displayed).
16714
16715 At the same time this space let's a nicely handle clearing to the
16716 end of the line if the row ends in italic text. */
16717
16718 static int
16719 append_space_for_newline (it, default_face_p)
16720 struct it *it;
16721 int default_face_p;
16722 {
16723 if (FRAME_WINDOW_P (it->f))
16724 {
16725 int n = it->glyph_row->used[TEXT_AREA];
16726
16727 if (it->glyph_row->glyphs[TEXT_AREA] + n
16728 < it->glyph_row->glyphs[1 + TEXT_AREA])
16729 {
16730 /* Save some values that must not be changed.
16731 Must save IT->c and IT->len because otherwise
16732 ITERATOR_AT_END_P wouldn't work anymore after
16733 append_space_for_newline has been called. */
16734 enum display_element_type saved_what = it->what;
16735 int saved_c = it->c, saved_len = it->len;
16736 int saved_x = it->current_x;
16737 int saved_face_id = it->face_id;
16738 struct text_pos saved_pos;
16739 Lisp_Object saved_object;
16740 struct face *face;
16741
16742 saved_object = it->object;
16743 saved_pos = it->position;
16744
16745 it->what = IT_CHARACTER;
16746 bzero (&it->position, sizeof it->position);
16747 it->object = make_number (0);
16748 it->c = ' ';
16749 it->len = 1;
16750
16751 if (default_face_p)
16752 it->face_id = DEFAULT_FACE_ID;
16753 else if (it->face_before_selective_p)
16754 it->face_id = it->saved_face_id;
16755 face = FACE_FROM_ID (it->f, it->face_id);
16756 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16757
16758 PRODUCE_GLYPHS (it);
16759
16760 it->override_ascent = -1;
16761 it->constrain_row_ascent_descent_p = 0;
16762 it->current_x = saved_x;
16763 it->object = saved_object;
16764 it->position = saved_pos;
16765 it->what = saved_what;
16766 it->face_id = saved_face_id;
16767 it->len = saved_len;
16768 it->c = saved_c;
16769 return 1;
16770 }
16771 }
16772
16773 return 0;
16774 }
16775
16776
16777 /* Extend the face of the last glyph in the text area of IT->glyph_row
16778 to the end of the display line. Called from display_line.
16779 If the glyph row is empty, add a space glyph to it so that we
16780 know the face to draw. Set the glyph row flag fill_line_p. */
16781
16782 static void
16783 extend_face_to_end_of_line (it)
16784 struct it *it;
16785 {
16786 struct face *face;
16787 struct frame *f = it->f;
16788
16789 /* If line is already filled, do nothing. */
16790 if (it->current_x >= it->last_visible_x)
16791 return;
16792
16793 /* Face extension extends the background and box of IT->face_id
16794 to the end of the line. If the background equals the background
16795 of the frame, we don't have to do anything. */
16796 if (it->face_before_selective_p)
16797 face = FACE_FROM_ID (it->f, it->saved_face_id);
16798 else
16799 face = FACE_FROM_ID (f, it->face_id);
16800
16801 if (FRAME_WINDOW_P (f)
16802 && it->glyph_row->displays_text_p
16803 && face->box == FACE_NO_BOX
16804 && face->background == FRAME_BACKGROUND_PIXEL (f)
16805 && !face->stipple)
16806 return;
16807
16808 /* Set the glyph row flag indicating that the face of the last glyph
16809 in the text area has to be drawn to the end of the text area. */
16810 it->glyph_row->fill_line_p = 1;
16811
16812 /* If current character of IT is not ASCII, make sure we have the
16813 ASCII face. This will be automatically undone the next time
16814 get_next_display_element returns a multibyte character. Note
16815 that the character will always be single byte in unibyte
16816 text. */
16817 if (!ASCII_CHAR_P (it->c))
16818 {
16819 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16820 }
16821
16822 if (FRAME_WINDOW_P (f))
16823 {
16824 /* If the row is empty, add a space with the current face of IT,
16825 so that we know which face to draw. */
16826 if (it->glyph_row->used[TEXT_AREA] == 0)
16827 {
16828 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16829 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16830 it->glyph_row->used[TEXT_AREA] = 1;
16831 }
16832 }
16833 else
16834 {
16835 /* Save some values that must not be changed. */
16836 int saved_x = it->current_x;
16837 struct text_pos saved_pos;
16838 Lisp_Object saved_object;
16839 enum display_element_type saved_what = it->what;
16840 int saved_face_id = it->face_id;
16841
16842 saved_object = it->object;
16843 saved_pos = it->position;
16844
16845 it->what = IT_CHARACTER;
16846 bzero (&it->position, sizeof it->position);
16847 it->object = make_number (0);
16848 it->c = ' ';
16849 it->len = 1;
16850 it->face_id = face->id;
16851
16852 PRODUCE_GLYPHS (it);
16853
16854 while (it->current_x <= it->last_visible_x)
16855 PRODUCE_GLYPHS (it);
16856
16857 /* Don't count these blanks really. It would let us insert a left
16858 truncation glyph below and make us set the cursor on them, maybe. */
16859 it->current_x = saved_x;
16860 it->object = saved_object;
16861 it->position = saved_pos;
16862 it->what = saved_what;
16863 it->face_id = saved_face_id;
16864 }
16865 }
16866
16867
16868 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16869 trailing whitespace. */
16870
16871 static int
16872 trailing_whitespace_p (charpos)
16873 int charpos;
16874 {
16875 int bytepos = CHAR_TO_BYTE (charpos);
16876 int c = 0;
16877
16878 while (bytepos < ZV_BYTE
16879 && (c = FETCH_CHAR (bytepos),
16880 c == ' ' || c == '\t'))
16881 ++bytepos;
16882
16883 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16884 {
16885 if (bytepos != PT_BYTE)
16886 return 1;
16887 }
16888 return 0;
16889 }
16890
16891
16892 /* Highlight trailing whitespace, if any, in ROW. */
16893
16894 void
16895 highlight_trailing_whitespace (f, row)
16896 struct frame *f;
16897 struct glyph_row *row;
16898 {
16899 int used = row->used[TEXT_AREA];
16900
16901 if (used)
16902 {
16903 struct glyph *start = row->glyphs[TEXT_AREA];
16904 struct glyph *glyph = start + used - 1;
16905
16906 /* Skip over glyphs inserted to display the cursor at the
16907 end of a line, for extending the face of the last glyph
16908 to the end of the line on terminals, and for truncation
16909 and continuation glyphs. */
16910 while (glyph >= start
16911 && glyph->type == CHAR_GLYPH
16912 && INTEGERP (glyph->object))
16913 --glyph;
16914
16915 /* If last glyph is a space or stretch, and it's trailing
16916 whitespace, set the face of all trailing whitespace glyphs in
16917 IT->glyph_row to `trailing-whitespace'. */
16918 if (glyph >= start
16919 && BUFFERP (glyph->object)
16920 && (glyph->type == STRETCH_GLYPH
16921 || (glyph->type == CHAR_GLYPH
16922 && glyph->u.ch == ' '))
16923 && trailing_whitespace_p (glyph->charpos))
16924 {
16925 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16926 if (face_id < 0)
16927 return;
16928
16929 while (glyph >= start
16930 && BUFFERP (glyph->object)
16931 && (glyph->type == STRETCH_GLYPH
16932 || (glyph->type == CHAR_GLYPH
16933 && glyph->u.ch == ' ')))
16934 (glyph--)->face_id = face_id;
16935 }
16936 }
16937 }
16938
16939
16940 /* Value is non-zero if glyph row ROW in window W should be
16941 used to hold the cursor. */
16942
16943 static int
16944 cursor_row_p (w, row)
16945 struct window *w;
16946 struct glyph_row *row;
16947 {
16948 int cursor_row_p = 1;
16949
16950 if (PT == MATRIX_ROW_END_CHARPOS (row))
16951 {
16952 /* Suppose the row ends on a string.
16953 Unless the row is continued, that means it ends on a newline
16954 in the string. If it's anything other than a display string
16955 (e.g. a before-string from an overlay), we don't want the
16956 cursor there. (This heuristic seems to give the optimal
16957 behavior for the various types of multi-line strings.) */
16958 if (CHARPOS (row->end.string_pos) >= 0)
16959 {
16960 if (row->continued_p)
16961 cursor_row_p = 1;
16962 else
16963 {
16964 /* Check for `display' property. */
16965 struct glyph *beg = row->glyphs[TEXT_AREA];
16966 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16967 struct glyph *glyph;
16968
16969 cursor_row_p = 0;
16970 for (glyph = end; glyph >= beg; --glyph)
16971 if (STRINGP (glyph->object))
16972 {
16973 Lisp_Object prop
16974 = Fget_char_property (make_number (PT),
16975 Qdisplay, Qnil);
16976 cursor_row_p =
16977 (!NILP (prop)
16978 && display_prop_string_p (prop, glyph->object));
16979 break;
16980 }
16981 }
16982 }
16983 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16984 {
16985 /* If the row ends in middle of a real character,
16986 and the line is continued, we want the cursor here.
16987 That's because MATRIX_ROW_END_CHARPOS would equal
16988 PT if PT is before the character. */
16989 if (!row->ends_in_ellipsis_p)
16990 cursor_row_p = row->continued_p;
16991 else
16992 /* If the row ends in an ellipsis, then
16993 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16994 We want that position to be displayed after the ellipsis. */
16995 cursor_row_p = 0;
16996 }
16997 /* If the row ends at ZV, display the cursor at the end of that
16998 row instead of at the start of the row below. */
16999 else if (row->ends_at_zv_p)
17000 cursor_row_p = 1;
17001 else
17002 cursor_row_p = 0;
17003 }
17004
17005 return cursor_row_p;
17006 }
17007
17008 \f
17009
17010 /* Push the display property PROP so that it will be rendered at the
17011 current position in IT. Return 1 if PROP was successfully pushed,
17012 0 otherwise. */
17013
17014 static int
17015 push_display_prop (struct it *it, Lisp_Object prop)
17016 {
17017 push_it (it);
17018
17019 if (STRINGP (prop))
17020 {
17021 if (SCHARS (prop) == 0)
17022 {
17023 pop_it (it);
17024 return 0;
17025 }
17026
17027 it->string = prop;
17028 it->multibyte_p = STRING_MULTIBYTE (it->string);
17029 it->current.overlay_string_index = -1;
17030 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17031 it->end_charpos = it->string_nchars = SCHARS (it->string);
17032 it->method = GET_FROM_STRING;
17033 it->stop_charpos = 0;
17034 }
17035 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17036 {
17037 it->method = GET_FROM_STRETCH;
17038 it->object = prop;
17039 }
17040 #ifdef HAVE_WINDOW_SYSTEM
17041 else if (IMAGEP (prop))
17042 {
17043 it->what = IT_IMAGE;
17044 it->image_id = lookup_image (it->f, prop);
17045 it->method = GET_FROM_IMAGE;
17046 }
17047 #endif /* HAVE_WINDOW_SYSTEM */
17048 else
17049 {
17050 pop_it (it); /* bogus display property, give up */
17051 return 0;
17052 }
17053
17054 return 1;
17055 }
17056
17057 /* Return the character-property PROP at the current position in IT. */
17058
17059 static Lisp_Object
17060 get_it_property (it, prop)
17061 struct it *it;
17062 Lisp_Object prop;
17063 {
17064 Lisp_Object position;
17065
17066 if (STRINGP (it->object))
17067 position = make_number (IT_STRING_CHARPOS (*it));
17068 else if (BUFFERP (it->object))
17069 position = make_number (IT_CHARPOS (*it));
17070 else
17071 return Qnil;
17072
17073 return Fget_char_property (position, prop, it->object);
17074 }
17075
17076 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17077
17078 static void
17079 handle_line_prefix (struct it *it)
17080 {
17081 Lisp_Object prefix;
17082 if (it->continuation_lines_width > 0)
17083 {
17084 prefix = get_it_property (it, Qwrap_prefix);
17085 if (NILP (prefix))
17086 prefix = Vwrap_prefix;
17087 }
17088 else
17089 {
17090 prefix = get_it_property (it, Qline_prefix);
17091 if (NILP (prefix))
17092 prefix = Vline_prefix;
17093 }
17094 if (! NILP (prefix) && push_display_prop (it, prefix))
17095 {
17096 /* If the prefix is wider than the window, and we try to wrap
17097 it, it would acquire its own wrap prefix, and so on till the
17098 iterator stack overflows. So, don't wrap the prefix. */
17099 it->line_wrap = TRUNCATE;
17100 it->avoid_cursor_p = 1;
17101 }
17102 }
17103
17104 \f
17105
17106 /* Construct the glyph row IT->glyph_row in the desired matrix of
17107 IT->w from text at the current position of IT. See dispextern.h
17108 for an overview of struct it. Value is non-zero if
17109 IT->glyph_row displays text, as opposed to a line displaying ZV
17110 only. */
17111
17112 static int
17113 display_line (it)
17114 struct it *it;
17115 {
17116 struct glyph_row *row = it->glyph_row;
17117 Lisp_Object overlay_arrow_string;
17118 struct it wrap_it;
17119 int may_wrap = 0, wrap_x;
17120 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17121 int wrap_row_phys_ascent, wrap_row_phys_height;
17122 int wrap_row_extra_line_spacing;
17123 struct display_pos row_end;
17124 int cvpos;
17125
17126 /* We always start displaying at hpos zero even if hscrolled. */
17127 xassert (it->hpos == 0 && it->current_x == 0);
17128
17129 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17130 >= it->w->desired_matrix->nrows)
17131 {
17132 it->w->nrows_scale_factor++;
17133 fonts_changed_p = 1;
17134 return 0;
17135 }
17136
17137 /* Is IT->w showing the region? */
17138 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17139
17140 /* Clear the result glyph row and enable it. */
17141 prepare_desired_row (row);
17142
17143 row->y = it->current_y;
17144 row->start = it->start;
17145 row->continuation_lines_width = it->continuation_lines_width;
17146 row->displays_text_p = 1;
17147 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17148 it->starts_in_middle_of_char_p = 0;
17149
17150 /* Arrange the overlays nicely for our purposes. Usually, we call
17151 display_line on only one line at a time, in which case this
17152 can't really hurt too much, or we call it on lines which appear
17153 one after another in the buffer, in which case all calls to
17154 recenter_overlay_lists but the first will be pretty cheap. */
17155 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17156
17157 /* Move over display elements that are not visible because we are
17158 hscrolled. This may stop at an x-position < IT->first_visible_x
17159 if the first glyph is partially visible or if we hit a line end. */
17160 if (it->current_x < it->first_visible_x)
17161 {
17162 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17163 MOVE_TO_POS | MOVE_TO_X);
17164 }
17165 else
17166 {
17167 /* We only do this when not calling `move_it_in_display_line_to'
17168 above, because move_it_in_display_line_to calls
17169 handle_line_prefix itself. */
17170 handle_line_prefix (it);
17171 }
17172
17173 /* Get the initial row height. This is either the height of the
17174 text hscrolled, if there is any, or zero. */
17175 row->ascent = it->max_ascent;
17176 row->height = it->max_ascent + it->max_descent;
17177 row->phys_ascent = it->max_phys_ascent;
17178 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17179 row->extra_line_spacing = it->max_extra_line_spacing;
17180
17181 /* Loop generating characters. The loop is left with IT on the next
17182 character to display. */
17183 while (1)
17184 {
17185 int n_glyphs_before, hpos_before, x_before;
17186 int x, i, nglyphs;
17187 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17188
17189 /* Retrieve the next thing to display. Value is zero if end of
17190 buffer reached. */
17191 if (!get_next_display_element (it))
17192 {
17193 /* Maybe add a space at the end of this line that is used to
17194 display the cursor there under X. Set the charpos of the
17195 first glyph of blank lines not corresponding to any text
17196 to -1. */
17197 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17198 row->exact_window_width_line_p = 1;
17199 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17200 || row->used[TEXT_AREA] == 0)
17201 {
17202 row->glyphs[TEXT_AREA]->charpos = -1;
17203 row->displays_text_p = 0;
17204
17205 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17206 && (!MINI_WINDOW_P (it->w)
17207 || (minibuf_level && EQ (it->window, minibuf_window))))
17208 row->indicate_empty_line_p = 1;
17209 }
17210
17211 it->continuation_lines_width = 0;
17212 row->ends_at_zv_p = 1;
17213 /* A row that displays right-to-left text must always have
17214 its last face extended all the way to the end of line,
17215 even if this row ends in ZV. */
17216 if (row->reversed_p)
17217 extend_face_to_end_of_line (it);
17218 break;
17219 }
17220
17221 /* Now, get the metrics of what we want to display. This also
17222 generates glyphs in `row' (which is IT->glyph_row). */
17223 n_glyphs_before = row->used[TEXT_AREA];
17224 x = it->current_x;
17225
17226 /* Remember the line height so far in case the next element doesn't
17227 fit on the line. */
17228 if (it->line_wrap != TRUNCATE)
17229 {
17230 ascent = it->max_ascent;
17231 descent = it->max_descent;
17232 phys_ascent = it->max_phys_ascent;
17233 phys_descent = it->max_phys_descent;
17234
17235 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17236 {
17237 if (IT_DISPLAYING_WHITESPACE (it))
17238 may_wrap = 1;
17239 else if (may_wrap)
17240 {
17241 wrap_it = *it;
17242 wrap_x = x;
17243 wrap_row_used = row->used[TEXT_AREA];
17244 wrap_row_ascent = row->ascent;
17245 wrap_row_height = row->height;
17246 wrap_row_phys_ascent = row->phys_ascent;
17247 wrap_row_phys_height = row->phys_height;
17248 wrap_row_extra_line_spacing = row->extra_line_spacing;
17249 may_wrap = 0;
17250 }
17251 }
17252 }
17253
17254 PRODUCE_GLYPHS (it);
17255
17256 /* If this display element was in marginal areas, continue with
17257 the next one. */
17258 if (it->area != TEXT_AREA)
17259 {
17260 row->ascent = max (row->ascent, it->max_ascent);
17261 row->height = max (row->height, it->max_ascent + it->max_descent);
17262 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17263 row->phys_height = max (row->phys_height,
17264 it->max_phys_ascent + it->max_phys_descent);
17265 row->extra_line_spacing = max (row->extra_line_spacing,
17266 it->max_extra_line_spacing);
17267 set_iterator_to_next (it, 1);
17268 continue;
17269 }
17270
17271 /* Does the display element fit on the line? If we truncate
17272 lines, we should draw past the right edge of the window. If
17273 we don't truncate, we want to stop so that we can display the
17274 continuation glyph before the right margin. If lines are
17275 continued, there are two possible strategies for characters
17276 resulting in more than 1 glyph (e.g. tabs): Display as many
17277 glyphs as possible in this line and leave the rest for the
17278 continuation line, or display the whole element in the next
17279 line. Original redisplay did the former, so we do it also. */
17280 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17281 hpos_before = it->hpos;
17282 x_before = x;
17283
17284 if (/* Not a newline. */
17285 nglyphs > 0
17286 /* Glyphs produced fit entirely in the line. */
17287 && it->current_x < it->last_visible_x)
17288 {
17289 it->hpos += nglyphs;
17290 row->ascent = max (row->ascent, it->max_ascent);
17291 row->height = max (row->height, it->max_ascent + it->max_descent);
17292 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17293 row->phys_height = max (row->phys_height,
17294 it->max_phys_ascent + it->max_phys_descent);
17295 row->extra_line_spacing = max (row->extra_line_spacing,
17296 it->max_extra_line_spacing);
17297 if (it->current_x - it->pixel_width < it->first_visible_x)
17298 row->x = x - it->first_visible_x;
17299 }
17300 else
17301 {
17302 int new_x;
17303 struct glyph *glyph;
17304
17305 for (i = 0; i < nglyphs; ++i, x = new_x)
17306 {
17307 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17308 new_x = x + glyph->pixel_width;
17309
17310 if (/* Lines are continued. */
17311 it->line_wrap != TRUNCATE
17312 && (/* Glyph doesn't fit on the line. */
17313 new_x > it->last_visible_x
17314 /* Or it fits exactly on a window system frame. */
17315 || (new_x == it->last_visible_x
17316 && FRAME_WINDOW_P (it->f))))
17317 {
17318 /* End of a continued line. */
17319
17320 if (it->hpos == 0
17321 || (new_x == it->last_visible_x
17322 && FRAME_WINDOW_P (it->f)))
17323 {
17324 /* Current glyph is the only one on the line or
17325 fits exactly on the line. We must continue
17326 the line because we can't draw the cursor
17327 after the glyph. */
17328 row->continued_p = 1;
17329 it->current_x = new_x;
17330 it->continuation_lines_width += new_x;
17331 ++it->hpos;
17332 if (i == nglyphs - 1)
17333 {
17334 /* If line-wrap is on, check if a previous
17335 wrap point was found. */
17336 if (wrap_row_used > 0
17337 /* Even if there is a previous wrap
17338 point, continue the line here as
17339 usual, if (i) the previous character
17340 was a space or tab AND (ii) the
17341 current character is not. */
17342 && (!may_wrap
17343 || IT_DISPLAYING_WHITESPACE (it)))
17344 goto back_to_wrap;
17345
17346 set_iterator_to_next (it, 1);
17347 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17348 {
17349 if (!get_next_display_element (it))
17350 {
17351 row->exact_window_width_line_p = 1;
17352 it->continuation_lines_width = 0;
17353 row->continued_p = 0;
17354 row->ends_at_zv_p = 1;
17355 }
17356 else if (ITERATOR_AT_END_OF_LINE_P (it))
17357 {
17358 row->continued_p = 0;
17359 row->exact_window_width_line_p = 1;
17360 }
17361 }
17362 }
17363 }
17364 else if (CHAR_GLYPH_PADDING_P (*glyph)
17365 && !FRAME_WINDOW_P (it->f))
17366 {
17367 /* A padding glyph that doesn't fit on this line.
17368 This means the whole character doesn't fit
17369 on the line. */
17370 row->used[TEXT_AREA] = n_glyphs_before;
17371
17372 /* Fill the rest of the row with continuation
17373 glyphs like in 20.x. */
17374 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17375 < row->glyphs[1 + TEXT_AREA])
17376 produce_special_glyphs (it, IT_CONTINUATION);
17377
17378 row->continued_p = 1;
17379 it->current_x = x_before;
17380 it->continuation_lines_width += x_before;
17381
17382 /* Restore the height to what it was before the
17383 element not fitting on the line. */
17384 it->max_ascent = ascent;
17385 it->max_descent = descent;
17386 it->max_phys_ascent = phys_ascent;
17387 it->max_phys_descent = phys_descent;
17388 }
17389 else if (wrap_row_used > 0)
17390 {
17391 back_to_wrap:
17392 *it = wrap_it;
17393 it->continuation_lines_width += wrap_x;
17394 row->used[TEXT_AREA] = wrap_row_used;
17395 row->ascent = wrap_row_ascent;
17396 row->height = wrap_row_height;
17397 row->phys_ascent = wrap_row_phys_ascent;
17398 row->phys_height = wrap_row_phys_height;
17399 row->extra_line_spacing = wrap_row_extra_line_spacing;
17400 row->continued_p = 1;
17401 row->ends_at_zv_p = 0;
17402 row->exact_window_width_line_p = 0;
17403 it->continuation_lines_width += x;
17404
17405 /* Make sure that a non-default face is extended
17406 up to the right margin of the window. */
17407 extend_face_to_end_of_line (it);
17408 }
17409 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17410 {
17411 /* A TAB that extends past the right edge of the
17412 window. This produces a single glyph on
17413 window system frames. We leave the glyph in
17414 this row and let it fill the row, but don't
17415 consume the TAB. */
17416 it->continuation_lines_width += it->last_visible_x;
17417 row->ends_in_middle_of_char_p = 1;
17418 row->continued_p = 1;
17419 glyph->pixel_width = it->last_visible_x - x;
17420 it->starts_in_middle_of_char_p = 1;
17421 }
17422 else
17423 {
17424 /* Something other than a TAB that draws past
17425 the right edge of the window. Restore
17426 positions to values before the element. */
17427 row->used[TEXT_AREA] = n_glyphs_before + i;
17428
17429 /* Display continuation glyphs. */
17430 if (!FRAME_WINDOW_P (it->f))
17431 produce_special_glyphs (it, IT_CONTINUATION);
17432 row->continued_p = 1;
17433
17434 it->current_x = x_before;
17435 it->continuation_lines_width += x;
17436 extend_face_to_end_of_line (it);
17437
17438 if (nglyphs > 1 && i > 0)
17439 {
17440 row->ends_in_middle_of_char_p = 1;
17441 it->starts_in_middle_of_char_p = 1;
17442 }
17443
17444 /* Restore the height to what it was before the
17445 element not fitting on the line. */
17446 it->max_ascent = ascent;
17447 it->max_descent = descent;
17448 it->max_phys_ascent = phys_ascent;
17449 it->max_phys_descent = phys_descent;
17450 }
17451
17452 break;
17453 }
17454 else if (new_x > it->first_visible_x)
17455 {
17456 /* Increment number of glyphs actually displayed. */
17457 ++it->hpos;
17458
17459 if (x < it->first_visible_x)
17460 /* Glyph is partially visible, i.e. row starts at
17461 negative X position. */
17462 row->x = x - it->first_visible_x;
17463 }
17464 else
17465 {
17466 /* Glyph is completely off the left margin of the
17467 window. This should not happen because of the
17468 move_it_in_display_line at the start of this
17469 function, unless the text display area of the
17470 window is empty. */
17471 xassert (it->first_visible_x <= it->last_visible_x);
17472 }
17473 }
17474
17475 row->ascent = max (row->ascent, it->max_ascent);
17476 row->height = max (row->height, it->max_ascent + it->max_descent);
17477 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17478 row->phys_height = max (row->phys_height,
17479 it->max_phys_ascent + it->max_phys_descent);
17480 row->extra_line_spacing = max (row->extra_line_spacing,
17481 it->max_extra_line_spacing);
17482
17483 /* End of this display line if row is continued. */
17484 if (row->continued_p || row->ends_at_zv_p)
17485 break;
17486 }
17487
17488 at_end_of_line:
17489 /* Is this a line end? If yes, we're also done, after making
17490 sure that a non-default face is extended up to the right
17491 margin of the window. */
17492 if (ITERATOR_AT_END_OF_LINE_P (it))
17493 {
17494 int used_before = row->used[TEXT_AREA];
17495
17496 row->ends_in_newline_from_string_p = STRINGP (it->object);
17497
17498 /* Add a space at the end of the line that is used to
17499 display the cursor there. */
17500 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17501 append_space_for_newline (it, 0);
17502
17503 /* Extend the face to the end of the line. */
17504 extend_face_to_end_of_line (it);
17505
17506 /* Make sure we have the position. */
17507 if (used_before == 0)
17508 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17509
17510 /* Consume the line end. This skips over invisible lines. */
17511 set_iterator_to_next (it, 1);
17512 it->continuation_lines_width = 0;
17513 break;
17514 }
17515
17516 /* Proceed with next display element. Note that this skips
17517 over lines invisible because of selective display. */
17518 set_iterator_to_next (it, 1);
17519
17520 /* If we truncate lines, we are done when the last displayed
17521 glyphs reach past the right margin of the window. */
17522 if (it->line_wrap == TRUNCATE
17523 && (FRAME_WINDOW_P (it->f)
17524 ? (it->current_x >= it->last_visible_x)
17525 : (it->current_x > it->last_visible_x)))
17526 {
17527 /* Maybe add truncation glyphs. */
17528 if (!FRAME_WINDOW_P (it->f))
17529 {
17530 int i, n;
17531
17532 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17533 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17534 break;
17535
17536 for (n = row->used[TEXT_AREA]; i < n; ++i)
17537 {
17538 row->used[TEXT_AREA] = i;
17539 produce_special_glyphs (it, IT_TRUNCATION);
17540 }
17541 }
17542 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17543 {
17544 /* Don't truncate if we can overflow newline into fringe. */
17545 if (!get_next_display_element (it))
17546 {
17547 it->continuation_lines_width = 0;
17548 row->ends_at_zv_p = 1;
17549 row->exact_window_width_line_p = 1;
17550 break;
17551 }
17552 if (ITERATOR_AT_END_OF_LINE_P (it))
17553 {
17554 row->exact_window_width_line_p = 1;
17555 goto at_end_of_line;
17556 }
17557 }
17558
17559 row->truncated_on_right_p = 1;
17560 it->continuation_lines_width = 0;
17561 reseat_at_next_visible_line_start (it, 0);
17562 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17563 it->hpos = hpos_before;
17564 it->current_x = x_before;
17565 break;
17566 }
17567 }
17568
17569 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17570 at the left window margin. */
17571 if (it->first_visible_x
17572 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
17573 {
17574 if (!FRAME_WINDOW_P (it->f))
17575 insert_left_trunc_glyphs (it);
17576 row->truncated_on_left_p = 1;
17577 }
17578
17579 /* If the start of this line is the overlay arrow-position, then
17580 mark this glyph row as the one containing the overlay arrow.
17581 This is clearly a mess with variable size fonts. It would be
17582 better to let it be displayed like cursors under X. */
17583 if ((row->displays_text_p || !overlay_arrow_seen)
17584 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17585 !NILP (overlay_arrow_string)))
17586 {
17587 /* Overlay arrow in window redisplay is a fringe bitmap. */
17588 if (STRINGP (overlay_arrow_string))
17589 {
17590 struct glyph_row *arrow_row
17591 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17592 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17593 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17594 struct glyph *p = row->glyphs[TEXT_AREA];
17595 struct glyph *p2, *end;
17596
17597 /* Copy the arrow glyphs. */
17598 while (glyph < arrow_end)
17599 *p++ = *glyph++;
17600
17601 /* Throw away padding glyphs. */
17602 p2 = p;
17603 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17604 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17605 ++p2;
17606 if (p2 > p)
17607 {
17608 while (p2 < end)
17609 *p++ = *p2++;
17610 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17611 }
17612 }
17613 else
17614 {
17615 xassert (INTEGERP (overlay_arrow_string));
17616 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17617 }
17618 overlay_arrow_seen = 1;
17619 }
17620
17621 /* Compute pixel dimensions of this line. */
17622 compute_line_metrics (it);
17623
17624 /* Remember the position at which this line ends. */
17625 row->end = row_end = it->current;
17626 if (it->bidi_p)
17627 {
17628 /* ROW->start and ROW->end must be the smallest and largest
17629 buffer positions in ROW. But if ROW was bidi-reordered,
17630 these two positions can be anywhere in the row, so we must
17631 rescan all of the ROW's glyphs to find them. */
17632 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17633 lines' rows is implemented for bidi-reordered rows. */
17634 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17635 struct glyph *g;
17636 struct it save_it;
17637 struct text_pos tpos;
17638
17639 for (g = row->glyphs[TEXT_AREA];
17640 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17641 g++)
17642 {
17643 if (BUFFERP (g->object))
17644 {
17645 if (g->charpos > 0 && g->charpos < min_pos)
17646 min_pos = g->charpos;
17647 if (g->charpos > max_pos)
17648 max_pos = g->charpos;
17649 }
17650 }
17651 /* Empty lines have a valid buffer position at their first
17652 glyph, but that glyph's OBJECT is zero, as if it didn't come
17653 from a buffer. If we didn't find any valid buffer positions
17654 in this row, maybe we have such an empty line. */
17655 if (min_pos == ZV + 1 && row->used[TEXT_AREA])
17656 {
17657 for (g = row->glyphs[TEXT_AREA];
17658 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17659 g++)
17660 {
17661 if (INTEGERP (g->object))
17662 {
17663 if (g->charpos > 0 && g->charpos < min_pos)
17664 min_pos = g->charpos;
17665 if (g->charpos > max_pos)
17666 max_pos = g->charpos;
17667 }
17668 }
17669 }
17670 if (min_pos <= ZV)
17671 {
17672 if (min_pos != row->start.pos.charpos)
17673 {
17674 row->start.pos.charpos = min_pos;
17675 row->start.pos.bytepos = CHAR_TO_BYTE (min_pos);
17676 }
17677 if (max_pos == 0)
17678 max_pos = min_pos;
17679 }
17680 /* For ROW->end, we need the position that is _after_ max_pos,
17681 in the logical order, unless we are at ZV. */
17682 if (row->ends_at_zv_p)
17683 {
17684 row_end = row->end = it->current;
17685 if (!row->used[TEXT_AREA])
17686 {
17687 row->start.pos.charpos = row_end.pos.charpos;
17688 row->start.pos.bytepos = row_end.pos.bytepos;
17689 }
17690 }
17691 else if (row->used[TEXT_AREA] && max_pos)
17692 {
17693 SET_TEXT_POS (tpos, max_pos + 1, CHAR_TO_BYTE (max_pos + 1));
17694 row_end = it->current;
17695 row_end.pos = tpos;
17696 /* If the character at max_pos+1 is a newline, skip that as
17697 well. Note that this may skip some invisible text. */
17698 if (FETCH_CHAR (tpos.bytepos) == '\n'
17699 || (FETCH_CHAR (tpos.bytepos) == '\r' && it->selective))
17700 {
17701 save_it = *it;
17702 it->bidi_p = 0;
17703 reseat_1 (it, tpos, 0);
17704 set_iterator_to_next (it, 1);
17705 /* Record the position after the newline of a continued
17706 row. We will need that to set ROW->end of the last
17707 row produced for a continued line. */
17708 if (row->continued_p)
17709 {
17710 save_it.eol_pos.charpos = IT_CHARPOS (*it);
17711 save_it.eol_pos.bytepos = IT_BYTEPOS (*it);
17712 }
17713 else
17714 {
17715 row_end = it->current;
17716 save_it.eol_pos.charpos = save_it.eol_pos.bytepos = 0;
17717 }
17718 *it = save_it;
17719 }
17720 else if (!row->continued_p
17721 && row->continuation_lines_width
17722 && it->eol_pos.charpos > 0)
17723 {
17724 /* Last row of a continued line. Use the position
17725 recorded in ROW->eol_pos, to the effect that the
17726 newline belongs to this row, not to the row which
17727 displays the character with the largest buffer
17728 position. */
17729 row_end.pos = it->eol_pos;
17730 it->eol_pos.charpos = it->eol_pos.bytepos = 0;
17731 }
17732 row->end = row_end;
17733 }
17734 }
17735
17736 /* Record whether this row ends inside an ellipsis. */
17737 row->ends_in_ellipsis_p
17738 = (it->method == GET_FROM_DISPLAY_VECTOR
17739 && it->ellipsis_p);
17740
17741 /* Save fringe bitmaps in this row. */
17742 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17743 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17744 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17745 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17746
17747 it->left_user_fringe_bitmap = 0;
17748 it->left_user_fringe_face_id = 0;
17749 it->right_user_fringe_bitmap = 0;
17750 it->right_user_fringe_face_id = 0;
17751
17752 /* Maybe set the cursor. */
17753 cvpos = it->w->cursor.vpos;
17754 if ((cvpos < 0
17755 /* In bidi-reordered rows, keep checking for proper cursor
17756 position even if one has been found already, because buffer
17757 positions in such rows change non-linearly with ROW->VPOS,
17758 when a line is continued. One exception: when we are at ZV,
17759 display cursor on the first suitable glyph row, since all
17760 the empty rows after that also have their position set to ZV. */
17761 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17762 lines' rows is implemented for bidi-reordered rows. */
17763 || (it->bidi_p
17764 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17765 && PT >= MATRIX_ROW_START_CHARPOS (row)
17766 && PT <= MATRIX_ROW_END_CHARPOS (row)
17767 && cursor_row_p (it->w, row))
17768 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17769
17770 /* Highlight trailing whitespace. */
17771 if (!NILP (Vshow_trailing_whitespace))
17772 highlight_trailing_whitespace (it->f, it->glyph_row);
17773
17774 /* Prepare for the next line. This line starts horizontally at (X
17775 HPOS) = (0 0). Vertical positions are incremented. As a
17776 convenience for the caller, IT->glyph_row is set to the next
17777 row to be used. */
17778 it->current_x = it->hpos = 0;
17779 it->current_y += row->height;
17780 ++it->vpos;
17781 ++it->glyph_row;
17782 /* The next row should use same value of the reversed_p flag as this
17783 one. set_iterator_to_next decides when it's a new paragraph, and
17784 PRODUCE_GLYPHS recomputes the value of the flag accordingly. */
17785 it->glyph_row->reversed_p = row->reversed_p;
17786 it->start = row_end;
17787 return row->displays_text_p;
17788 }
17789
17790
17791 \f
17792 /***********************************************************************
17793 Menu Bar
17794 ***********************************************************************/
17795
17796 /* Redisplay the menu bar in the frame for window W.
17797
17798 The menu bar of X frames that don't have X toolkit support is
17799 displayed in a special window W->frame->menu_bar_window.
17800
17801 The menu bar of terminal frames is treated specially as far as
17802 glyph matrices are concerned. Menu bar lines are not part of
17803 windows, so the update is done directly on the frame matrix rows
17804 for the menu bar. */
17805
17806 static void
17807 display_menu_bar (w)
17808 struct window *w;
17809 {
17810 struct frame *f = XFRAME (WINDOW_FRAME (w));
17811 struct it it;
17812 Lisp_Object items;
17813 int i;
17814
17815 /* Don't do all this for graphical frames. */
17816 #ifdef HAVE_NTGUI
17817 if (FRAME_W32_P (f))
17818 return;
17819 #endif
17820 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17821 if (FRAME_X_P (f))
17822 return;
17823 #endif
17824
17825 #ifdef HAVE_NS
17826 if (FRAME_NS_P (f))
17827 return;
17828 #endif /* HAVE_NS */
17829
17830 #ifdef USE_X_TOOLKIT
17831 xassert (!FRAME_WINDOW_P (f));
17832 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17833 it.first_visible_x = 0;
17834 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17835 #else /* not USE_X_TOOLKIT */
17836 if (FRAME_WINDOW_P (f))
17837 {
17838 /* Menu bar lines are displayed in the desired matrix of the
17839 dummy window menu_bar_window. */
17840 struct window *menu_w;
17841 xassert (WINDOWP (f->menu_bar_window));
17842 menu_w = XWINDOW (f->menu_bar_window);
17843 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17844 MENU_FACE_ID);
17845 it.first_visible_x = 0;
17846 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17847 }
17848 else
17849 {
17850 /* This is a TTY frame, i.e. character hpos/vpos are used as
17851 pixel x/y. */
17852 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17853 MENU_FACE_ID);
17854 it.first_visible_x = 0;
17855 it.last_visible_x = FRAME_COLS (f);
17856 }
17857 #endif /* not USE_X_TOOLKIT */
17858
17859 if (! mode_line_inverse_video)
17860 /* Force the menu-bar to be displayed in the default face. */
17861 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17862
17863 /* Clear all rows of the menu bar. */
17864 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17865 {
17866 struct glyph_row *row = it.glyph_row + i;
17867 clear_glyph_row (row);
17868 row->enabled_p = 1;
17869 row->full_width_p = 1;
17870 }
17871
17872 /* Display all items of the menu bar. */
17873 items = FRAME_MENU_BAR_ITEMS (it.f);
17874 for (i = 0; i < XVECTOR (items)->size; i += 4)
17875 {
17876 Lisp_Object string;
17877
17878 /* Stop at nil string. */
17879 string = AREF (items, i + 1);
17880 if (NILP (string))
17881 break;
17882
17883 /* Remember where item was displayed. */
17884 ASET (items, i + 3, make_number (it.hpos));
17885
17886 /* Display the item, pad with one space. */
17887 if (it.current_x < it.last_visible_x)
17888 display_string (NULL, string, Qnil, 0, 0, &it,
17889 SCHARS (string) + 1, 0, 0, -1);
17890 }
17891
17892 /* Fill out the line with spaces. */
17893 if (it.current_x < it.last_visible_x)
17894 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17895
17896 /* Compute the total height of the lines. */
17897 compute_line_metrics (&it);
17898 }
17899
17900
17901 \f
17902 /***********************************************************************
17903 Mode Line
17904 ***********************************************************************/
17905
17906 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17907 FORCE is non-zero, redisplay mode lines unconditionally.
17908 Otherwise, redisplay only mode lines that are garbaged. Value is
17909 the number of windows whose mode lines were redisplayed. */
17910
17911 static int
17912 redisplay_mode_lines (window, force)
17913 Lisp_Object window;
17914 int force;
17915 {
17916 int nwindows = 0;
17917
17918 while (!NILP (window))
17919 {
17920 struct window *w = XWINDOW (window);
17921
17922 if (WINDOWP (w->hchild))
17923 nwindows += redisplay_mode_lines (w->hchild, force);
17924 else if (WINDOWP (w->vchild))
17925 nwindows += redisplay_mode_lines (w->vchild, force);
17926 else if (force
17927 || FRAME_GARBAGED_P (XFRAME (w->frame))
17928 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17929 {
17930 struct text_pos lpoint;
17931 struct buffer *old = current_buffer;
17932
17933 /* Set the window's buffer for the mode line display. */
17934 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17935 set_buffer_internal_1 (XBUFFER (w->buffer));
17936
17937 /* Point refers normally to the selected window. For any
17938 other window, set up appropriate value. */
17939 if (!EQ (window, selected_window))
17940 {
17941 struct text_pos pt;
17942
17943 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17944 if (CHARPOS (pt) < BEGV)
17945 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17946 else if (CHARPOS (pt) > (ZV - 1))
17947 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17948 else
17949 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17950 }
17951
17952 /* Display mode lines. */
17953 clear_glyph_matrix (w->desired_matrix);
17954 if (display_mode_lines (w))
17955 {
17956 ++nwindows;
17957 w->must_be_updated_p = 1;
17958 }
17959
17960 /* Restore old settings. */
17961 set_buffer_internal_1 (old);
17962 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17963 }
17964
17965 window = w->next;
17966 }
17967
17968 return nwindows;
17969 }
17970
17971
17972 /* Display the mode and/or header line of window W. Value is the
17973 sum number of mode lines and header lines displayed. */
17974
17975 static int
17976 display_mode_lines (w)
17977 struct window *w;
17978 {
17979 Lisp_Object old_selected_window, old_selected_frame;
17980 int n = 0;
17981
17982 old_selected_frame = selected_frame;
17983 selected_frame = w->frame;
17984 old_selected_window = selected_window;
17985 XSETWINDOW (selected_window, w);
17986
17987 /* These will be set while the mode line specs are processed. */
17988 line_number_displayed = 0;
17989 w->column_number_displayed = Qnil;
17990
17991 if (WINDOW_WANTS_MODELINE_P (w))
17992 {
17993 struct window *sel_w = XWINDOW (old_selected_window);
17994
17995 /* Select mode line face based on the real selected window. */
17996 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17997 current_buffer->mode_line_format);
17998 ++n;
17999 }
18000
18001 if (WINDOW_WANTS_HEADER_LINE_P (w))
18002 {
18003 display_mode_line (w, HEADER_LINE_FACE_ID,
18004 current_buffer->header_line_format);
18005 ++n;
18006 }
18007
18008 selected_frame = old_selected_frame;
18009 selected_window = old_selected_window;
18010 return n;
18011 }
18012
18013
18014 /* Display mode or header line of window W. FACE_ID specifies which
18015 line to display; it is either MODE_LINE_FACE_ID or
18016 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18017 display. Value is the pixel height of the mode/header line
18018 displayed. */
18019
18020 static int
18021 display_mode_line (w, face_id, format)
18022 struct window *w;
18023 enum face_id face_id;
18024 Lisp_Object format;
18025 {
18026 struct it it;
18027 struct face *face;
18028 int count = SPECPDL_INDEX ();
18029
18030 init_iterator (&it, w, -1, -1, NULL, face_id);
18031 /* Don't extend on a previously drawn mode-line.
18032 This may happen if called from pos_visible_p. */
18033 it.glyph_row->enabled_p = 0;
18034 prepare_desired_row (it.glyph_row);
18035
18036 it.glyph_row->mode_line_p = 1;
18037
18038 if (! mode_line_inverse_video)
18039 /* Force the mode-line to be displayed in the default face. */
18040 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18041
18042 record_unwind_protect (unwind_format_mode_line,
18043 format_mode_line_unwind_data (NULL, Qnil, 0));
18044
18045 mode_line_target = MODE_LINE_DISPLAY;
18046
18047 /* Temporarily make frame's keyboard the current kboard so that
18048 kboard-local variables in the mode_line_format will get the right
18049 values. */
18050 push_kboard (FRAME_KBOARD (it.f));
18051 record_unwind_save_match_data ();
18052 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18053 pop_kboard ();
18054
18055 unbind_to (count, Qnil);
18056
18057 /* Fill up with spaces. */
18058 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18059
18060 compute_line_metrics (&it);
18061 it.glyph_row->full_width_p = 1;
18062 it.glyph_row->continued_p = 0;
18063 it.glyph_row->truncated_on_left_p = 0;
18064 it.glyph_row->truncated_on_right_p = 0;
18065
18066 /* Make a 3D mode-line have a shadow at its right end. */
18067 face = FACE_FROM_ID (it.f, face_id);
18068 extend_face_to_end_of_line (&it);
18069 if (face->box != FACE_NO_BOX)
18070 {
18071 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18072 + it.glyph_row->used[TEXT_AREA] - 1);
18073 last->right_box_line_p = 1;
18074 }
18075
18076 return it.glyph_row->height;
18077 }
18078
18079 /* Move element ELT in LIST to the front of LIST.
18080 Return the updated list. */
18081
18082 static Lisp_Object
18083 move_elt_to_front (elt, list)
18084 Lisp_Object elt, list;
18085 {
18086 register Lisp_Object tail, prev;
18087 register Lisp_Object tem;
18088
18089 tail = list;
18090 prev = Qnil;
18091 while (CONSP (tail))
18092 {
18093 tem = XCAR (tail);
18094
18095 if (EQ (elt, tem))
18096 {
18097 /* Splice out the link TAIL. */
18098 if (NILP (prev))
18099 list = XCDR (tail);
18100 else
18101 Fsetcdr (prev, XCDR (tail));
18102
18103 /* Now make it the first. */
18104 Fsetcdr (tail, list);
18105 return tail;
18106 }
18107 else
18108 prev = tail;
18109 tail = XCDR (tail);
18110 QUIT;
18111 }
18112
18113 /* Not found--return unchanged LIST. */
18114 return list;
18115 }
18116
18117 /* Contribute ELT to the mode line for window IT->w. How it
18118 translates into text depends on its data type.
18119
18120 IT describes the display environment in which we display, as usual.
18121
18122 DEPTH is the depth in recursion. It is used to prevent
18123 infinite recursion here.
18124
18125 FIELD_WIDTH is the number of characters the display of ELT should
18126 occupy in the mode line, and PRECISION is the maximum number of
18127 characters to display from ELT's representation. See
18128 display_string for details.
18129
18130 Returns the hpos of the end of the text generated by ELT.
18131
18132 PROPS is a property list to add to any string we encounter.
18133
18134 If RISKY is nonzero, remove (disregard) any properties in any string
18135 we encounter, and ignore :eval and :propertize.
18136
18137 The global variable `mode_line_target' determines whether the
18138 output is passed to `store_mode_line_noprop',
18139 `store_mode_line_string', or `display_string'. */
18140
18141 static int
18142 display_mode_element (it, depth, field_width, precision, elt, props, risky)
18143 struct it *it;
18144 int depth;
18145 int field_width, precision;
18146 Lisp_Object elt, props;
18147 int risky;
18148 {
18149 int n = 0, field, prec;
18150 int literal = 0;
18151
18152 tail_recurse:
18153 if (depth > 100)
18154 elt = build_string ("*too-deep*");
18155
18156 depth++;
18157
18158 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18159 {
18160 case Lisp_String:
18161 {
18162 /* A string: output it and check for %-constructs within it. */
18163 unsigned char c;
18164 int offset = 0;
18165
18166 if (SCHARS (elt) > 0
18167 && (!NILP (props) || risky))
18168 {
18169 Lisp_Object oprops, aelt;
18170 oprops = Ftext_properties_at (make_number (0), elt);
18171
18172 /* If the starting string's properties are not what
18173 we want, translate the string. Also, if the string
18174 is risky, do that anyway. */
18175
18176 if (NILP (Fequal (props, oprops)) || risky)
18177 {
18178 /* If the starting string has properties,
18179 merge the specified ones onto the existing ones. */
18180 if (! NILP (oprops) && !risky)
18181 {
18182 Lisp_Object tem;
18183
18184 oprops = Fcopy_sequence (oprops);
18185 tem = props;
18186 while (CONSP (tem))
18187 {
18188 oprops = Fplist_put (oprops, XCAR (tem),
18189 XCAR (XCDR (tem)));
18190 tem = XCDR (XCDR (tem));
18191 }
18192 props = oprops;
18193 }
18194
18195 aelt = Fassoc (elt, mode_line_proptrans_alist);
18196 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18197 {
18198 /* AELT is what we want. Move it to the front
18199 without consing. */
18200 elt = XCAR (aelt);
18201 mode_line_proptrans_alist
18202 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18203 }
18204 else
18205 {
18206 Lisp_Object tem;
18207
18208 /* If AELT has the wrong props, it is useless.
18209 so get rid of it. */
18210 if (! NILP (aelt))
18211 mode_line_proptrans_alist
18212 = Fdelq (aelt, mode_line_proptrans_alist);
18213
18214 elt = Fcopy_sequence (elt);
18215 Fset_text_properties (make_number (0), Flength (elt),
18216 props, elt);
18217 /* Add this item to mode_line_proptrans_alist. */
18218 mode_line_proptrans_alist
18219 = Fcons (Fcons (elt, props),
18220 mode_line_proptrans_alist);
18221 /* Truncate mode_line_proptrans_alist
18222 to at most 50 elements. */
18223 tem = Fnthcdr (make_number (50),
18224 mode_line_proptrans_alist);
18225 if (! NILP (tem))
18226 XSETCDR (tem, Qnil);
18227 }
18228 }
18229 }
18230
18231 offset = 0;
18232
18233 if (literal)
18234 {
18235 prec = precision - n;
18236 switch (mode_line_target)
18237 {
18238 case MODE_LINE_NOPROP:
18239 case MODE_LINE_TITLE:
18240 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18241 break;
18242 case MODE_LINE_STRING:
18243 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18244 break;
18245 case MODE_LINE_DISPLAY:
18246 n += display_string (NULL, elt, Qnil, 0, 0, it,
18247 0, prec, 0, STRING_MULTIBYTE (elt));
18248 break;
18249 }
18250
18251 break;
18252 }
18253
18254 /* Handle the non-literal case. */
18255
18256 while ((precision <= 0 || n < precision)
18257 && SREF (elt, offset) != 0
18258 && (mode_line_target != MODE_LINE_DISPLAY
18259 || it->current_x < it->last_visible_x))
18260 {
18261 int last_offset = offset;
18262
18263 /* Advance to end of string or next format specifier. */
18264 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18265 ;
18266
18267 if (offset - 1 != last_offset)
18268 {
18269 int nchars, nbytes;
18270
18271 /* Output to end of string or up to '%'. Field width
18272 is length of string. Don't output more than
18273 PRECISION allows us. */
18274 offset--;
18275
18276 prec = c_string_width (SDATA (elt) + last_offset,
18277 offset - last_offset, precision - n,
18278 &nchars, &nbytes);
18279
18280 switch (mode_line_target)
18281 {
18282 case MODE_LINE_NOPROP:
18283 case MODE_LINE_TITLE:
18284 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18285 break;
18286 case MODE_LINE_STRING:
18287 {
18288 int bytepos = last_offset;
18289 int charpos = string_byte_to_char (elt, bytepos);
18290 int endpos = (precision <= 0
18291 ? string_byte_to_char (elt, offset)
18292 : charpos + nchars);
18293
18294 n += store_mode_line_string (NULL,
18295 Fsubstring (elt, make_number (charpos),
18296 make_number (endpos)),
18297 0, 0, 0, Qnil);
18298 }
18299 break;
18300 case MODE_LINE_DISPLAY:
18301 {
18302 int bytepos = last_offset;
18303 int charpos = string_byte_to_char (elt, bytepos);
18304
18305 if (precision <= 0)
18306 nchars = string_byte_to_char (elt, offset) - charpos;
18307 n += display_string (NULL, elt, Qnil, 0, charpos,
18308 it, 0, nchars, 0,
18309 STRING_MULTIBYTE (elt));
18310 }
18311 break;
18312 }
18313 }
18314 else /* c == '%' */
18315 {
18316 int percent_position = offset;
18317
18318 /* Get the specified minimum width. Zero means
18319 don't pad. */
18320 field = 0;
18321 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18322 field = field * 10 + c - '0';
18323
18324 /* Don't pad beyond the total padding allowed. */
18325 if (field_width - n > 0 && field > field_width - n)
18326 field = field_width - n;
18327
18328 /* Note that either PRECISION <= 0 or N < PRECISION. */
18329 prec = precision - n;
18330
18331 if (c == 'M')
18332 n += display_mode_element (it, depth, field, prec,
18333 Vglobal_mode_string, props,
18334 risky);
18335 else if (c != 0)
18336 {
18337 int multibyte;
18338 int bytepos, charpos;
18339 unsigned char *spec;
18340 Lisp_Object string;
18341
18342 bytepos = percent_position;
18343 charpos = (STRING_MULTIBYTE (elt)
18344 ? string_byte_to_char (elt, bytepos)
18345 : bytepos);
18346 spec = decode_mode_spec (it->w, c, field, prec, &string);
18347 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18348
18349 switch (mode_line_target)
18350 {
18351 case MODE_LINE_NOPROP:
18352 case MODE_LINE_TITLE:
18353 n += store_mode_line_noprop (spec, field, prec);
18354 break;
18355 case MODE_LINE_STRING:
18356 {
18357 int len = strlen (spec);
18358 Lisp_Object tem = make_string (spec, len);
18359 props = Ftext_properties_at (make_number (charpos), elt);
18360 /* Should only keep face property in props */
18361 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18362 }
18363 break;
18364 case MODE_LINE_DISPLAY:
18365 {
18366 int nglyphs_before, nwritten;
18367
18368 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18369 nwritten = display_string (spec, string, elt,
18370 charpos, 0, it,
18371 field, prec, 0,
18372 multibyte);
18373
18374 /* Assign to the glyphs written above the
18375 string where the `%x' came from, position
18376 of the `%'. */
18377 if (nwritten > 0)
18378 {
18379 struct glyph *glyph
18380 = (it->glyph_row->glyphs[TEXT_AREA]
18381 + nglyphs_before);
18382 int i;
18383
18384 for (i = 0; i < nwritten; ++i)
18385 {
18386 glyph[i].object = elt;
18387 glyph[i].charpos = charpos;
18388 }
18389
18390 n += nwritten;
18391 }
18392 }
18393 break;
18394 }
18395 }
18396 else /* c == 0 */
18397 break;
18398 }
18399 }
18400 }
18401 break;
18402
18403 case Lisp_Symbol:
18404 /* A symbol: process the value of the symbol recursively
18405 as if it appeared here directly. Avoid error if symbol void.
18406 Special case: if value of symbol is a string, output the string
18407 literally. */
18408 {
18409 register Lisp_Object tem;
18410
18411 /* If the variable is not marked as risky to set
18412 then its contents are risky to use. */
18413 if (NILP (Fget (elt, Qrisky_local_variable)))
18414 risky = 1;
18415
18416 tem = Fboundp (elt);
18417 if (!NILP (tem))
18418 {
18419 tem = Fsymbol_value (elt);
18420 /* If value is a string, output that string literally:
18421 don't check for % within it. */
18422 if (STRINGP (tem))
18423 literal = 1;
18424
18425 if (!EQ (tem, elt))
18426 {
18427 /* Give up right away for nil or t. */
18428 elt = tem;
18429 goto tail_recurse;
18430 }
18431 }
18432 }
18433 break;
18434
18435 case Lisp_Cons:
18436 {
18437 register Lisp_Object car, tem;
18438
18439 /* A cons cell: five distinct cases.
18440 If first element is :eval or :propertize, do something special.
18441 If first element is a string or a cons, process all the elements
18442 and effectively concatenate them.
18443 If first element is a negative number, truncate displaying cdr to
18444 at most that many characters. If positive, pad (with spaces)
18445 to at least that many characters.
18446 If first element is a symbol, process the cadr or caddr recursively
18447 according to whether the symbol's value is non-nil or nil. */
18448 car = XCAR (elt);
18449 if (EQ (car, QCeval))
18450 {
18451 /* An element of the form (:eval FORM) means evaluate FORM
18452 and use the result as mode line elements. */
18453
18454 if (risky)
18455 break;
18456
18457 if (CONSP (XCDR (elt)))
18458 {
18459 Lisp_Object spec;
18460 spec = safe_eval (XCAR (XCDR (elt)));
18461 n += display_mode_element (it, depth, field_width - n,
18462 precision - n, spec, props,
18463 risky);
18464 }
18465 }
18466 else if (EQ (car, QCpropertize))
18467 {
18468 /* An element of the form (:propertize ELT PROPS...)
18469 means display ELT but applying properties PROPS. */
18470
18471 if (risky)
18472 break;
18473
18474 if (CONSP (XCDR (elt)))
18475 n += display_mode_element (it, depth, field_width - n,
18476 precision - n, XCAR (XCDR (elt)),
18477 XCDR (XCDR (elt)), risky);
18478 }
18479 else if (SYMBOLP (car))
18480 {
18481 tem = Fboundp (car);
18482 elt = XCDR (elt);
18483 if (!CONSP (elt))
18484 goto invalid;
18485 /* elt is now the cdr, and we know it is a cons cell.
18486 Use its car if CAR has a non-nil value. */
18487 if (!NILP (tem))
18488 {
18489 tem = Fsymbol_value (car);
18490 if (!NILP (tem))
18491 {
18492 elt = XCAR (elt);
18493 goto tail_recurse;
18494 }
18495 }
18496 /* Symbol's value is nil (or symbol is unbound)
18497 Get the cddr of the original list
18498 and if possible find the caddr and use that. */
18499 elt = XCDR (elt);
18500 if (NILP (elt))
18501 break;
18502 else if (!CONSP (elt))
18503 goto invalid;
18504 elt = XCAR (elt);
18505 goto tail_recurse;
18506 }
18507 else if (INTEGERP (car))
18508 {
18509 register int lim = XINT (car);
18510 elt = XCDR (elt);
18511 if (lim < 0)
18512 {
18513 /* Negative int means reduce maximum width. */
18514 if (precision <= 0)
18515 precision = -lim;
18516 else
18517 precision = min (precision, -lim);
18518 }
18519 else if (lim > 0)
18520 {
18521 /* Padding specified. Don't let it be more than
18522 current maximum. */
18523 if (precision > 0)
18524 lim = min (precision, lim);
18525
18526 /* If that's more padding than already wanted, queue it.
18527 But don't reduce padding already specified even if
18528 that is beyond the current truncation point. */
18529 field_width = max (lim, field_width);
18530 }
18531 goto tail_recurse;
18532 }
18533 else if (STRINGP (car) || CONSP (car))
18534 {
18535 Lisp_Object halftail = elt;
18536 int len = 0;
18537
18538 while (CONSP (elt)
18539 && (precision <= 0 || n < precision))
18540 {
18541 n += display_mode_element (it, depth,
18542 /* Do padding only after the last
18543 element in the list. */
18544 (! CONSP (XCDR (elt))
18545 ? field_width - n
18546 : 0),
18547 precision - n, XCAR (elt),
18548 props, risky);
18549 elt = XCDR (elt);
18550 len++;
18551 if ((len & 1) == 0)
18552 halftail = XCDR (halftail);
18553 /* Check for cycle. */
18554 if (EQ (halftail, elt))
18555 break;
18556 }
18557 }
18558 }
18559 break;
18560
18561 default:
18562 invalid:
18563 elt = build_string ("*invalid*");
18564 goto tail_recurse;
18565 }
18566
18567 /* Pad to FIELD_WIDTH. */
18568 if (field_width > 0 && n < field_width)
18569 {
18570 switch (mode_line_target)
18571 {
18572 case MODE_LINE_NOPROP:
18573 case MODE_LINE_TITLE:
18574 n += store_mode_line_noprop ("", field_width - n, 0);
18575 break;
18576 case MODE_LINE_STRING:
18577 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18578 break;
18579 case MODE_LINE_DISPLAY:
18580 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18581 0, 0, 0);
18582 break;
18583 }
18584 }
18585
18586 return n;
18587 }
18588
18589 /* Store a mode-line string element in mode_line_string_list.
18590
18591 If STRING is non-null, display that C string. Otherwise, the Lisp
18592 string LISP_STRING is displayed.
18593
18594 FIELD_WIDTH is the minimum number of output glyphs to produce.
18595 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18596 with spaces. FIELD_WIDTH <= 0 means don't pad.
18597
18598 PRECISION is the maximum number of characters to output from
18599 STRING. PRECISION <= 0 means don't truncate the string.
18600
18601 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18602 properties to the string.
18603
18604 PROPS are the properties to add to the string.
18605 The mode_line_string_face face property is always added to the string.
18606 */
18607
18608 static int
18609 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
18610 char *string;
18611 Lisp_Object lisp_string;
18612 int copy_string;
18613 int field_width;
18614 int precision;
18615 Lisp_Object props;
18616 {
18617 int len;
18618 int n = 0;
18619
18620 if (string != NULL)
18621 {
18622 len = strlen (string);
18623 if (precision > 0 && len > precision)
18624 len = precision;
18625 lisp_string = make_string (string, len);
18626 if (NILP (props))
18627 props = mode_line_string_face_prop;
18628 else if (!NILP (mode_line_string_face))
18629 {
18630 Lisp_Object face = Fplist_get (props, Qface);
18631 props = Fcopy_sequence (props);
18632 if (NILP (face))
18633 face = mode_line_string_face;
18634 else
18635 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18636 props = Fplist_put (props, Qface, face);
18637 }
18638 Fadd_text_properties (make_number (0), make_number (len),
18639 props, lisp_string);
18640 }
18641 else
18642 {
18643 len = XFASTINT (Flength (lisp_string));
18644 if (precision > 0 && len > precision)
18645 {
18646 len = precision;
18647 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18648 precision = -1;
18649 }
18650 if (!NILP (mode_line_string_face))
18651 {
18652 Lisp_Object face;
18653 if (NILP (props))
18654 props = Ftext_properties_at (make_number (0), lisp_string);
18655 face = Fplist_get (props, Qface);
18656 if (NILP (face))
18657 face = mode_line_string_face;
18658 else
18659 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18660 props = Fcons (Qface, Fcons (face, Qnil));
18661 if (copy_string)
18662 lisp_string = Fcopy_sequence (lisp_string);
18663 }
18664 if (!NILP (props))
18665 Fadd_text_properties (make_number (0), make_number (len),
18666 props, lisp_string);
18667 }
18668
18669 if (len > 0)
18670 {
18671 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18672 n += len;
18673 }
18674
18675 if (field_width > len)
18676 {
18677 field_width -= len;
18678 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18679 if (!NILP (props))
18680 Fadd_text_properties (make_number (0), make_number (field_width),
18681 props, lisp_string);
18682 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18683 n += field_width;
18684 }
18685
18686 return n;
18687 }
18688
18689
18690 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18691 1, 4, 0,
18692 doc: /* Format a string out of a mode line format specification.
18693 First arg FORMAT specifies the mode line format (see `mode-line-format'
18694 for details) to use.
18695
18696 Optional second arg FACE specifies the face property to put
18697 on all characters for which no face is specified.
18698 The value t means whatever face the window's mode line currently uses
18699 \(either `mode-line' or `mode-line-inactive', depending).
18700 A value of nil means the default is no face property.
18701 If FACE is an integer, the value string has no text properties.
18702
18703 Optional third and fourth args WINDOW and BUFFER specify the window
18704 and buffer to use as the context for the formatting (defaults
18705 are the selected window and the window's buffer). */)
18706 (format, face, window, buffer)
18707 Lisp_Object format, face, window, buffer;
18708 {
18709 struct it it;
18710 int len;
18711 struct window *w;
18712 struct buffer *old_buffer = NULL;
18713 int face_id = -1;
18714 int no_props = INTEGERP (face);
18715 int count = SPECPDL_INDEX ();
18716 Lisp_Object str;
18717 int string_start = 0;
18718
18719 if (NILP (window))
18720 window = selected_window;
18721 CHECK_WINDOW (window);
18722 w = XWINDOW (window);
18723
18724 if (NILP (buffer))
18725 buffer = w->buffer;
18726 CHECK_BUFFER (buffer);
18727
18728 /* Make formatting the modeline a non-op when noninteractive, otherwise
18729 there will be problems later caused by a partially initialized frame. */
18730 if (NILP (format) || noninteractive)
18731 return empty_unibyte_string;
18732
18733 if (no_props)
18734 face = Qnil;
18735
18736 if (!NILP (face))
18737 {
18738 if (EQ (face, Qt))
18739 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18740 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18741 }
18742
18743 if (face_id < 0)
18744 face_id = DEFAULT_FACE_ID;
18745
18746 if (XBUFFER (buffer) != current_buffer)
18747 old_buffer = current_buffer;
18748
18749 /* Save things including mode_line_proptrans_alist,
18750 and set that to nil so that we don't alter the outer value. */
18751 record_unwind_protect (unwind_format_mode_line,
18752 format_mode_line_unwind_data
18753 (old_buffer, selected_window, 1));
18754 mode_line_proptrans_alist = Qnil;
18755
18756 Fselect_window (window, Qt);
18757 if (old_buffer)
18758 set_buffer_internal_1 (XBUFFER (buffer));
18759
18760 init_iterator (&it, w, -1, -1, NULL, face_id);
18761
18762 if (no_props)
18763 {
18764 mode_line_target = MODE_LINE_NOPROP;
18765 mode_line_string_face_prop = Qnil;
18766 mode_line_string_list = Qnil;
18767 string_start = MODE_LINE_NOPROP_LEN (0);
18768 }
18769 else
18770 {
18771 mode_line_target = MODE_LINE_STRING;
18772 mode_line_string_list = Qnil;
18773 mode_line_string_face = face;
18774 mode_line_string_face_prop
18775 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18776 }
18777
18778 push_kboard (FRAME_KBOARD (it.f));
18779 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18780 pop_kboard ();
18781
18782 if (no_props)
18783 {
18784 len = MODE_LINE_NOPROP_LEN (string_start);
18785 str = make_string (mode_line_noprop_buf + string_start, len);
18786 }
18787 else
18788 {
18789 mode_line_string_list = Fnreverse (mode_line_string_list);
18790 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18791 empty_unibyte_string);
18792 }
18793
18794 unbind_to (count, Qnil);
18795 return str;
18796 }
18797
18798 /* Write a null-terminated, right justified decimal representation of
18799 the positive integer D to BUF using a minimal field width WIDTH. */
18800
18801 static void
18802 pint2str (buf, width, d)
18803 register char *buf;
18804 register int width;
18805 register int d;
18806 {
18807 register char *p = buf;
18808
18809 if (d <= 0)
18810 *p++ = '0';
18811 else
18812 {
18813 while (d > 0)
18814 {
18815 *p++ = d % 10 + '0';
18816 d /= 10;
18817 }
18818 }
18819
18820 for (width -= (int) (p - buf); width > 0; --width)
18821 *p++ = ' ';
18822 *p-- = '\0';
18823 while (p > buf)
18824 {
18825 d = *buf;
18826 *buf++ = *p;
18827 *p-- = d;
18828 }
18829 }
18830
18831 /* Write a null-terminated, right justified decimal and "human
18832 readable" representation of the nonnegative integer D to BUF using
18833 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18834
18835 static const char power_letter[] =
18836 {
18837 0, /* not used */
18838 'k', /* kilo */
18839 'M', /* mega */
18840 'G', /* giga */
18841 'T', /* tera */
18842 'P', /* peta */
18843 'E', /* exa */
18844 'Z', /* zetta */
18845 'Y' /* yotta */
18846 };
18847
18848 static void
18849 pint2hrstr (buf, width, d)
18850 char *buf;
18851 int width;
18852 int d;
18853 {
18854 /* We aim to represent the nonnegative integer D as
18855 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18856 int quotient = d;
18857 int remainder = 0;
18858 /* -1 means: do not use TENTHS. */
18859 int tenths = -1;
18860 int exponent = 0;
18861
18862 /* Length of QUOTIENT.TENTHS as a string. */
18863 int length;
18864
18865 char * psuffix;
18866 char * p;
18867
18868 if (1000 <= quotient)
18869 {
18870 /* Scale to the appropriate EXPONENT. */
18871 do
18872 {
18873 remainder = quotient % 1000;
18874 quotient /= 1000;
18875 exponent++;
18876 }
18877 while (1000 <= quotient);
18878
18879 /* Round to nearest and decide whether to use TENTHS or not. */
18880 if (quotient <= 9)
18881 {
18882 tenths = remainder / 100;
18883 if (50 <= remainder % 100)
18884 {
18885 if (tenths < 9)
18886 tenths++;
18887 else
18888 {
18889 quotient++;
18890 if (quotient == 10)
18891 tenths = -1;
18892 else
18893 tenths = 0;
18894 }
18895 }
18896 }
18897 else
18898 if (500 <= remainder)
18899 {
18900 if (quotient < 999)
18901 quotient++;
18902 else
18903 {
18904 quotient = 1;
18905 exponent++;
18906 tenths = 0;
18907 }
18908 }
18909 }
18910
18911 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18912 if (tenths == -1 && quotient <= 99)
18913 if (quotient <= 9)
18914 length = 1;
18915 else
18916 length = 2;
18917 else
18918 length = 3;
18919 p = psuffix = buf + max (width, length);
18920
18921 /* Print EXPONENT. */
18922 if (exponent)
18923 *psuffix++ = power_letter[exponent];
18924 *psuffix = '\0';
18925
18926 /* Print TENTHS. */
18927 if (tenths >= 0)
18928 {
18929 *--p = '0' + tenths;
18930 *--p = '.';
18931 }
18932
18933 /* Print QUOTIENT. */
18934 do
18935 {
18936 int digit = quotient % 10;
18937 *--p = '0' + digit;
18938 }
18939 while ((quotient /= 10) != 0);
18940
18941 /* Print leading spaces. */
18942 while (buf < p)
18943 *--p = ' ';
18944 }
18945
18946 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18947 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18948 type of CODING_SYSTEM. Return updated pointer into BUF. */
18949
18950 static unsigned char invalid_eol_type[] = "(*invalid*)";
18951
18952 static char *
18953 decode_mode_spec_coding (coding_system, buf, eol_flag)
18954 Lisp_Object coding_system;
18955 register char *buf;
18956 int eol_flag;
18957 {
18958 Lisp_Object val;
18959 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18960 const unsigned char *eol_str;
18961 int eol_str_len;
18962 /* The EOL conversion we are using. */
18963 Lisp_Object eoltype;
18964
18965 val = CODING_SYSTEM_SPEC (coding_system);
18966 eoltype = Qnil;
18967
18968 if (!VECTORP (val)) /* Not yet decided. */
18969 {
18970 if (multibyte)
18971 *buf++ = '-';
18972 if (eol_flag)
18973 eoltype = eol_mnemonic_undecided;
18974 /* Don't mention EOL conversion if it isn't decided. */
18975 }
18976 else
18977 {
18978 Lisp_Object attrs;
18979 Lisp_Object eolvalue;
18980
18981 attrs = AREF (val, 0);
18982 eolvalue = AREF (val, 2);
18983
18984 if (multibyte)
18985 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18986
18987 if (eol_flag)
18988 {
18989 /* The EOL conversion that is normal on this system. */
18990
18991 if (NILP (eolvalue)) /* Not yet decided. */
18992 eoltype = eol_mnemonic_undecided;
18993 else if (VECTORP (eolvalue)) /* Not yet decided. */
18994 eoltype = eol_mnemonic_undecided;
18995 else /* eolvalue is Qunix, Qdos, or Qmac. */
18996 eoltype = (EQ (eolvalue, Qunix)
18997 ? eol_mnemonic_unix
18998 : (EQ (eolvalue, Qdos) == 1
18999 ? eol_mnemonic_dos : eol_mnemonic_mac));
19000 }
19001 }
19002
19003 if (eol_flag)
19004 {
19005 /* Mention the EOL conversion if it is not the usual one. */
19006 if (STRINGP (eoltype))
19007 {
19008 eol_str = SDATA (eoltype);
19009 eol_str_len = SBYTES (eoltype);
19010 }
19011 else if (CHARACTERP (eoltype))
19012 {
19013 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19014 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19015 eol_str = tmp;
19016 }
19017 else
19018 {
19019 eol_str = invalid_eol_type;
19020 eol_str_len = sizeof (invalid_eol_type) - 1;
19021 }
19022 bcopy (eol_str, buf, eol_str_len);
19023 buf += eol_str_len;
19024 }
19025
19026 return buf;
19027 }
19028
19029 /* Return a string for the output of a mode line %-spec for window W,
19030 generated by character C. PRECISION >= 0 means don't return a
19031 string longer than that value. FIELD_WIDTH > 0 means pad the
19032 string returned with spaces to that value. Return a Lisp string in
19033 *STRING if the resulting string is taken from that Lisp string.
19034
19035 Note we operate on the current buffer for most purposes,
19036 the exception being w->base_line_pos. */
19037
19038 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19039
19040 static char *
19041 decode_mode_spec (w, c, field_width, precision, string)
19042 struct window *w;
19043 register int c;
19044 int field_width, precision;
19045 Lisp_Object *string;
19046 {
19047 Lisp_Object obj;
19048 struct frame *f = XFRAME (WINDOW_FRAME (w));
19049 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19050 struct buffer *b = current_buffer;
19051
19052 obj = Qnil;
19053 *string = Qnil;
19054
19055 switch (c)
19056 {
19057 case '*':
19058 if (!NILP (b->read_only))
19059 return "%";
19060 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19061 return "*";
19062 return "-";
19063
19064 case '+':
19065 /* This differs from %* only for a modified read-only buffer. */
19066 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19067 return "*";
19068 if (!NILP (b->read_only))
19069 return "%";
19070 return "-";
19071
19072 case '&':
19073 /* This differs from %* in ignoring read-only-ness. */
19074 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19075 return "*";
19076 return "-";
19077
19078 case '%':
19079 return "%";
19080
19081 case '[':
19082 {
19083 int i;
19084 char *p;
19085
19086 if (command_loop_level > 5)
19087 return "[[[... ";
19088 p = decode_mode_spec_buf;
19089 for (i = 0; i < command_loop_level; i++)
19090 *p++ = '[';
19091 *p = 0;
19092 return decode_mode_spec_buf;
19093 }
19094
19095 case ']':
19096 {
19097 int i;
19098 char *p;
19099
19100 if (command_loop_level > 5)
19101 return " ...]]]";
19102 p = decode_mode_spec_buf;
19103 for (i = 0; i < command_loop_level; i++)
19104 *p++ = ']';
19105 *p = 0;
19106 return decode_mode_spec_buf;
19107 }
19108
19109 case '-':
19110 {
19111 register int i;
19112
19113 /* Let lots_of_dashes be a string of infinite length. */
19114 if (mode_line_target == MODE_LINE_NOPROP ||
19115 mode_line_target == MODE_LINE_STRING)
19116 return "--";
19117 if (field_width <= 0
19118 || field_width > sizeof (lots_of_dashes))
19119 {
19120 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19121 decode_mode_spec_buf[i] = '-';
19122 decode_mode_spec_buf[i] = '\0';
19123 return decode_mode_spec_buf;
19124 }
19125 else
19126 return lots_of_dashes;
19127 }
19128
19129 case 'b':
19130 obj = b->name;
19131 break;
19132
19133 case 'c':
19134 /* %c and %l are ignored in `frame-title-format'.
19135 (In redisplay_internal, the frame title is drawn _before_ the
19136 windows are updated, so the stuff which depends on actual
19137 window contents (such as %l) may fail to render properly, or
19138 even crash emacs.) */
19139 if (mode_line_target == MODE_LINE_TITLE)
19140 return "";
19141 else
19142 {
19143 int col = (int) current_column (); /* iftc */
19144 w->column_number_displayed = make_number (col);
19145 pint2str (decode_mode_spec_buf, field_width, col);
19146 return decode_mode_spec_buf;
19147 }
19148
19149 case 'e':
19150 #ifndef SYSTEM_MALLOC
19151 {
19152 if (NILP (Vmemory_full))
19153 return "";
19154 else
19155 return "!MEM FULL! ";
19156 }
19157 #else
19158 return "";
19159 #endif
19160
19161 case 'F':
19162 /* %F displays the frame name. */
19163 if (!NILP (f->title))
19164 return (char *) SDATA (f->title);
19165 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19166 return (char *) SDATA (f->name);
19167 return "Emacs";
19168
19169 case 'f':
19170 obj = b->filename;
19171 break;
19172
19173 case 'i':
19174 {
19175 int size = ZV - BEGV;
19176 pint2str (decode_mode_spec_buf, field_width, size);
19177 return decode_mode_spec_buf;
19178 }
19179
19180 case 'I':
19181 {
19182 int size = ZV - BEGV;
19183 pint2hrstr (decode_mode_spec_buf, field_width, size);
19184 return decode_mode_spec_buf;
19185 }
19186
19187 case 'l':
19188 {
19189 int startpos, startpos_byte, line, linepos, linepos_byte;
19190 int topline, nlines, junk, height;
19191
19192 /* %c and %l are ignored in `frame-title-format'. */
19193 if (mode_line_target == MODE_LINE_TITLE)
19194 return "";
19195
19196 startpos = XMARKER (w->start)->charpos;
19197 startpos_byte = marker_byte_position (w->start);
19198 height = WINDOW_TOTAL_LINES (w);
19199
19200 /* If we decided that this buffer isn't suitable for line numbers,
19201 don't forget that too fast. */
19202 if (EQ (w->base_line_pos, w->buffer))
19203 goto no_value;
19204 /* But do forget it, if the window shows a different buffer now. */
19205 else if (BUFFERP (w->base_line_pos))
19206 w->base_line_pos = Qnil;
19207
19208 /* If the buffer is very big, don't waste time. */
19209 if (INTEGERP (Vline_number_display_limit)
19210 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19211 {
19212 w->base_line_pos = Qnil;
19213 w->base_line_number = Qnil;
19214 goto no_value;
19215 }
19216
19217 if (INTEGERP (w->base_line_number)
19218 && INTEGERP (w->base_line_pos)
19219 && XFASTINT (w->base_line_pos) <= startpos)
19220 {
19221 line = XFASTINT (w->base_line_number);
19222 linepos = XFASTINT (w->base_line_pos);
19223 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19224 }
19225 else
19226 {
19227 line = 1;
19228 linepos = BUF_BEGV (b);
19229 linepos_byte = BUF_BEGV_BYTE (b);
19230 }
19231
19232 /* Count lines from base line to window start position. */
19233 nlines = display_count_lines (linepos, linepos_byte,
19234 startpos_byte,
19235 startpos, &junk);
19236
19237 topline = nlines + line;
19238
19239 /* Determine a new base line, if the old one is too close
19240 or too far away, or if we did not have one.
19241 "Too close" means it's plausible a scroll-down would
19242 go back past it. */
19243 if (startpos == BUF_BEGV (b))
19244 {
19245 w->base_line_number = make_number (topline);
19246 w->base_line_pos = make_number (BUF_BEGV (b));
19247 }
19248 else if (nlines < height + 25 || nlines > height * 3 + 50
19249 || linepos == BUF_BEGV (b))
19250 {
19251 int limit = BUF_BEGV (b);
19252 int limit_byte = BUF_BEGV_BYTE (b);
19253 int position;
19254 int distance = (height * 2 + 30) * line_number_display_limit_width;
19255
19256 if (startpos - distance > limit)
19257 {
19258 limit = startpos - distance;
19259 limit_byte = CHAR_TO_BYTE (limit);
19260 }
19261
19262 nlines = display_count_lines (startpos, startpos_byte,
19263 limit_byte,
19264 - (height * 2 + 30),
19265 &position);
19266 /* If we couldn't find the lines we wanted within
19267 line_number_display_limit_width chars per line,
19268 give up on line numbers for this window. */
19269 if (position == limit_byte && limit == startpos - distance)
19270 {
19271 w->base_line_pos = w->buffer;
19272 w->base_line_number = Qnil;
19273 goto no_value;
19274 }
19275
19276 w->base_line_number = make_number (topline - nlines);
19277 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19278 }
19279
19280 /* Now count lines from the start pos to point. */
19281 nlines = display_count_lines (startpos, startpos_byte,
19282 PT_BYTE, PT, &junk);
19283
19284 /* Record that we did display the line number. */
19285 line_number_displayed = 1;
19286
19287 /* Make the string to show. */
19288 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19289 return decode_mode_spec_buf;
19290 no_value:
19291 {
19292 char* p = decode_mode_spec_buf;
19293 int pad = field_width - 2;
19294 while (pad-- > 0)
19295 *p++ = ' ';
19296 *p++ = '?';
19297 *p++ = '?';
19298 *p = '\0';
19299 return decode_mode_spec_buf;
19300 }
19301 }
19302 break;
19303
19304 case 'm':
19305 obj = b->mode_name;
19306 break;
19307
19308 case 'n':
19309 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19310 return " Narrow";
19311 break;
19312
19313 case 'p':
19314 {
19315 int pos = marker_position (w->start);
19316 int total = BUF_ZV (b) - BUF_BEGV (b);
19317
19318 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19319 {
19320 if (pos <= BUF_BEGV (b))
19321 return "All";
19322 else
19323 return "Bottom";
19324 }
19325 else if (pos <= BUF_BEGV (b))
19326 return "Top";
19327 else
19328 {
19329 if (total > 1000000)
19330 /* Do it differently for a large value, to avoid overflow. */
19331 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19332 else
19333 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19334 /* We can't normally display a 3-digit number,
19335 so get us a 2-digit number that is close. */
19336 if (total == 100)
19337 total = 99;
19338 sprintf (decode_mode_spec_buf, "%2d%%", total);
19339 return decode_mode_spec_buf;
19340 }
19341 }
19342
19343 /* Display percentage of size above the bottom of the screen. */
19344 case 'P':
19345 {
19346 int toppos = marker_position (w->start);
19347 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19348 int total = BUF_ZV (b) - BUF_BEGV (b);
19349
19350 if (botpos >= BUF_ZV (b))
19351 {
19352 if (toppos <= BUF_BEGV (b))
19353 return "All";
19354 else
19355 return "Bottom";
19356 }
19357 else
19358 {
19359 if (total > 1000000)
19360 /* Do it differently for a large value, to avoid overflow. */
19361 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19362 else
19363 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19364 /* We can't normally display a 3-digit number,
19365 so get us a 2-digit number that is close. */
19366 if (total == 100)
19367 total = 99;
19368 if (toppos <= BUF_BEGV (b))
19369 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
19370 else
19371 sprintf (decode_mode_spec_buf, "%2d%%", total);
19372 return decode_mode_spec_buf;
19373 }
19374 }
19375
19376 case 's':
19377 /* status of process */
19378 obj = Fget_buffer_process (Fcurrent_buffer ());
19379 if (NILP (obj))
19380 return "no process";
19381 #ifdef subprocesses
19382 obj = Fsymbol_name (Fprocess_status (obj));
19383 #endif
19384 break;
19385
19386 case '@':
19387 {
19388 int count = inhibit_garbage_collection ();
19389 Lisp_Object val = call1 (intern ("file-remote-p"),
19390 current_buffer->directory);
19391 unbind_to (count, Qnil);
19392
19393 if (NILP (val))
19394 return "-";
19395 else
19396 return "@";
19397 }
19398
19399 case 't': /* indicate TEXT or BINARY */
19400 #ifdef MODE_LINE_BINARY_TEXT
19401 return MODE_LINE_BINARY_TEXT (b);
19402 #else
19403 return "T";
19404 #endif
19405
19406 case 'z':
19407 /* coding-system (not including end-of-line format) */
19408 case 'Z':
19409 /* coding-system (including end-of-line type) */
19410 {
19411 int eol_flag = (c == 'Z');
19412 char *p = decode_mode_spec_buf;
19413
19414 if (! FRAME_WINDOW_P (f))
19415 {
19416 /* No need to mention EOL here--the terminal never needs
19417 to do EOL conversion. */
19418 p = decode_mode_spec_coding (CODING_ID_NAME
19419 (FRAME_KEYBOARD_CODING (f)->id),
19420 p, 0);
19421 p = decode_mode_spec_coding (CODING_ID_NAME
19422 (FRAME_TERMINAL_CODING (f)->id),
19423 p, 0);
19424 }
19425 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19426 p, eol_flag);
19427
19428 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19429 #ifdef subprocesses
19430 obj = Fget_buffer_process (Fcurrent_buffer ());
19431 if (PROCESSP (obj))
19432 {
19433 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19434 p, eol_flag);
19435 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19436 p, eol_flag);
19437 }
19438 #endif /* subprocesses */
19439 #endif /* 0 */
19440 *p = 0;
19441 return decode_mode_spec_buf;
19442 }
19443 }
19444
19445 if (STRINGP (obj))
19446 {
19447 *string = obj;
19448 return (char *) SDATA (obj);
19449 }
19450 else
19451 return "";
19452 }
19453
19454
19455 /* Count up to COUNT lines starting from START / START_BYTE.
19456 But don't go beyond LIMIT_BYTE.
19457 Return the number of lines thus found (always nonnegative).
19458
19459 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19460
19461 static int
19462 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
19463 int start, start_byte, limit_byte, count;
19464 int *byte_pos_ptr;
19465 {
19466 register unsigned char *cursor;
19467 unsigned char *base;
19468
19469 register int ceiling;
19470 register unsigned char *ceiling_addr;
19471 int orig_count = count;
19472
19473 /* If we are not in selective display mode,
19474 check only for newlines. */
19475 int selective_display = (!NILP (current_buffer->selective_display)
19476 && !INTEGERP (current_buffer->selective_display));
19477
19478 if (count > 0)
19479 {
19480 while (start_byte < limit_byte)
19481 {
19482 ceiling = BUFFER_CEILING_OF (start_byte);
19483 ceiling = min (limit_byte - 1, ceiling);
19484 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19485 base = (cursor = BYTE_POS_ADDR (start_byte));
19486 while (1)
19487 {
19488 if (selective_display)
19489 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19490 ;
19491 else
19492 while (*cursor != '\n' && ++cursor != ceiling_addr)
19493 ;
19494
19495 if (cursor != ceiling_addr)
19496 {
19497 if (--count == 0)
19498 {
19499 start_byte += cursor - base + 1;
19500 *byte_pos_ptr = start_byte;
19501 return orig_count;
19502 }
19503 else
19504 if (++cursor == ceiling_addr)
19505 break;
19506 }
19507 else
19508 break;
19509 }
19510 start_byte += cursor - base;
19511 }
19512 }
19513 else
19514 {
19515 while (start_byte > limit_byte)
19516 {
19517 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19518 ceiling = max (limit_byte, ceiling);
19519 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19520 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19521 while (1)
19522 {
19523 if (selective_display)
19524 while (--cursor != ceiling_addr
19525 && *cursor != '\n' && *cursor != 015)
19526 ;
19527 else
19528 while (--cursor != ceiling_addr && *cursor != '\n')
19529 ;
19530
19531 if (cursor != ceiling_addr)
19532 {
19533 if (++count == 0)
19534 {
19535 start_byte += cursor - base + 1;
19536 *byte_pos_ptr = start_byte;
19537 /* When scanning backwards, we should
19538 not count the newline posterior to which we stop. */
19539 return - orig_count - 1;
19540 }
19541 }
19542 else
19543 break;
19544 }
19545 /* Here we add 1 to compensate for the last decrement
19546 of CURSOR, which took it past the valid range. */
19547 start_byte += cursor - base + 1;
19548 }
19549 }
19550
19551 *byte_pos_ptr = limit_byte;
19552
19553 if (count < 0)
19554 return - orig_count + count;
19555 return orig_count - count;
19556
19557 }
19558
19559
19560 \f
19561 /***********************************************************************
19562 Displaying strings
19563 ***********************************************************************/
19564
19565 /* Display a NUL-terminated string, starting with index START.
19566
19567 If STRING is non-null, display that C string. Otherwise, the Lisp
19568 string LISP_STRING is displayed. There's a case that STRING is
19569 non-null and LISP_STRING is not nil. It means STRING is a string
19570 data of LISP_STRING. In that case, we display LISP_STRING while
19571 ignoring its text properties.
19572
19573 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19574 FACE_STRING. Display STRING or LISP_STRING with the face at
19575 FACE_STRING_POS in FACE_STRING:
19576
19577 Display the string in the environment given by IT, but use the
19578 standard display table, temporarily.
19579
19580 FIELD_WIDTH is the minimum number of output glyphs to produce.
19581 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19582 with spaces. If STRING has more characters, more than FIELD_WIDTH
19583 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19584
19585 PRECISION is the maximum number of characters to output from
19586 STRING. PRECISION < 0 means don't truncate the string.
19587
19588 This is roughly equivalent to printf format specifiers:
19589
19590 FIELD_WIDTH PRECISION PRINTF
19591 ----------------------------------------
19592 -1 -1 %s
19593 -1 10 %.10s
19594 10 -1 %10s
19595 20 10 %20.10s
19596
19597 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19598 display them, and < 0 means obey the current buffer's value of
19599 enable_multibyte_characters.
19600
19601 Value is the number of columns displayed. */
19602
19603 static int
19604 display_string (string, lisp_string, face_string, face_string_pos,
19605 start, it, field_width, precision, max_x, multibyte)
19606 unsigned char *string;
19607 Lisp_Object lisp_string;
19608 Lisp_Object face_string;
19609 EMACS_INT face_string_pos;
19610 EMACS_INT start;
19611 struct it *it;
19612 int field_width, precision, max_x;
19613 int multibyte;
19614 {
19615 int hpos_at_start = it->hpos;
19616 int saved_face_id = it->face_id;
19617 struct glyph_row *row = it->glyph_row;
19618
19619 /* Initialize the iterator IT for iteration over STRING beginning
19620 with index START. */
19621 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19622 precision, field_width, multibyte);
19623 if (string && STRINGP (lisp_string))
19624 /* LISP_STRING is the one returned by decode_mode_spec. We should
19625 ignore its text properties. */
19626 it->stop_charpos = -1;
19627
19628 /* If displaying STRING, set up the face of the iterator
19629 from LISP_STRING, if that's given. */
19630 if (STRINGP (face_string))
19631 {
19632 EMACS_INT endptr;
19633 struct face *face;
19634
19635 it->face_id
19636 = face_at_string_position (it->w, face_string, face_string_pos,
19637 0, it->region_beg_charpos,
19638 it->region_end_charpos,
19639 &endptr, it->base_face_id, 0);
19640 face = FACE_FROM_ID (it->f, it->face_id);
19641 it->face_box_p = face->box != FACE_NO_BOX;
19642 }
19643
19644 /* Set max_x to the maximum allowed X position. Don't let it go
19645 beyond the right edge of the window. */
19646 if (max_x <= 0)
19647 max_x = it->last_visible_x;
19648 else
19649 max_x = min (max_x, it->last_visible_x);
19650
19651 /* Skip over display elements that are not visible. because IT->w is
19652 hscrolled. */
19653 if (it->current_x < it->first_visible_x)
19654 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19655 MOVE_TO_POS | MOVE_TO_X);
19656
19657 row->ascent = it->max_ascent;
19658 row->height = it->max_ascent + it->max_descent;
19659 row->phys_ascent = it->max_phys_ascent;
19660 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19661 row->extra_line_spacing = it->max_extra_line_spacing;
19662
19663 /* This condition is for the case that we are called with current_x
19664 past last_visible_x. */
19665 while (it->current_x < max_x)
19666 {
19667 int x_before, x, n_glyphs_before, i, nglyphs;
19668
19669 /* Get the next display element. */
19670 if (!get_next_display_element (it))
19671 break;
19672
19673 /* Produce glyphs. */
19674 x_before = it->current_x;
19675 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19676 PRODUCE_GLYPHS (it);
19677
19678 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19679 i = 0;
19680 x = x_before;
19681 while (i < nglyphs)
19682 {
19683 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19684
19685 if (it->line_wrap != TRUNCATE
19686 && x + glyph->pixel_width > max_x)
19687 {
19688 /* End of continued line or max_x reached. */
19689 if (CHAR_GLYPH_PADDING_P (*glyph))
19690 {
19691 /* A wide character is unbreakable. */
19692 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19693 it->current_x = x_before;
19694 }
19695 else
19696 {
19697 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19698 it->current_x = x;
19699 }
19700 break;
19701 }
19702 else if (x + glyph->pixel_width >= it->first_visible_x)
19703 {
19704 /* Glyph is at least partially visible. */
19705 ++it->hpos;
19706 if (x < it->first_visible_x)
19707 it->glyph_row->x = x - it->first_visible_x;
19708 }
19709 else
19710 {
19711 /* Glyph is off the left margin of the display area.
19712 Should not happen. */
19713 abort ();
19714 }
19715
19716 row->ascent = max (row->ascent, it->max_ascent);
19717 row->height = max (row->height, it->max_ascent + it->max_descent);
19718 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19719 row->phys_height = max (row->phys_height,
19720 it->max_phys_ascent + it->max_phys_descent);
19721 row->extra_line_spacing = max (row->extra_line_spacing,
19722 it->max_extra_line_spacing);
19723 x += glyph->pixel_width;
19724 ++i;
19725 }
19726
19727 /* Stop if max_x reached. */
19728 if (i < nglyphs)
19729 break;
19730
19731 /* Stop at line ends. */
19732 if (ITERATOR_AT_END_OF_LINE_P (it))
19733 {
19734 it->continuation_lines_width = 0;
19735 break;
19736 }
19737
19738 set_iterator_to_next (it, 1);
19739
19740 /* Stop if truncating at the right edge. */
19741 if (it->line_wrap == TRUNCATE
19742 && it->current_x >= it->last_visible_x)
19743 {
19744 /* Add truncation mark, but don't do it if the line is
19745 truncated at a padding space. */
19746 if (IT_CHARPOS (*it) < it->string_nchars)
19747 {
19748 if (!FRAME_WINDOW_P (it->f))
19749 {
19750 int i, n;
19751
19752 if (it->current_x > it->last_visible_x)
19753 {
19754 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19755 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19756 break;
19757 for (n = row->used[TEXT_AREA]; i < n; ++i)
19758 {
19759 row->used[TEXT_AREA] = i;
19760 produce_special_glyphs (it, IT_TRUNCATION);
19761 }
19762 }
19763 produce_special_glyphs (it, IT_TRUNCATION);
19764 }
19765 it->glyph_row->truncated_on_right_p = 1;
19766 }
19767 break;
19768 }
19769 }
19770
19771 /* Maybe insert a truncation at the left. */
19772 if (it->first_visible_x
19773 && IT_CHARPOS (*it) > 0)
19774 {
19775 if (!FRAME_WINDOW_P (it->f))
19776 insert_left_trunc_glyphs (it);
19777 it->glyph_row->truncated_on_left_p = 1;
19778 }
19779
19780 it->face_id = saved_face_id;
19781
19782 /* Value is number of columns displayed. */
19783 return it->hpos - hpos_at_start;
19784 }
19785
19786
19787 \f
19788 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19789 appears as an element of LIST or as the car of an element of LIST.
19790 If PROPVAL is a list, compare each element against LIST in that
19791 way, and return 1/2 if any element of PROPVAL is found in LIST.
19792 Otherwise return 0. This function cannot quit.
19793 The return value is 2 if the text is invisible but with an ellipsis
19794 and 1 if it's invisible and without an ellipsis. */
19795
19796 int
19797 invisible_p (propval, list)
19798 register Lisp_Object propval;
19799 Lisp_Object list;
19800 {
19801 register Lisp_Object tail, proptail;
19802
19803 for (tail = list; CONSP (tail); tail = XCDR (tail))
19804 {
19805 register Lisp_Object tem;
19806 tem = XCAR (tail);
19807 if (EQ (propval, tem))
19808 return 1;
19809 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19810 return NILP (XCDR (tem)) ? 1 : 2;
19811 }
19812
19813 if (CONSP (propval))
19814 {
19815 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19816 {
19817 Lisp_Object propelt;
19818 propelt = XCAR (proptail);
19819 for (tail = list; CONSP (tail); tail = XCDR (tail))
19820 {
19821 register Lisp_Object tem;
19822 tem = XCAR (tail);
19823 if (EQ (propelt, tem))
19824 return 1;
19825 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19826 return NILP (XCDR (tem)) ? 1 : 2;
19827 }
19828 }
19829 }
19830
19831 return 0;
19832 }
19833
19834 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19835 doc: /* Non-nil if the property makes the text invisible.
19836 POS-OR-PROP can be a marker or number, in which case it is taken to be
19837 a position in the current buffer and the value of the `invisible' property
19838 is checked; or it can be some other value, which is then presumed to be the
19839 value of the `invisible' property of the text of interest.
19840 The non-nil value returned can be t for truly invisible text or something
19841 else if the text is replaced by an ellipsis. */)
19842 (pos_or_prop)
19843 Lisp_Object pos_or_prop;
19844 {
19845 Lisp_Object prop
19846 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19847 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19848 : pos_or_prop);
19849 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19850 return (invis == 0 ? Qnil
19851 : invis == 1 ? Qt
19852 : make_number (invis));
19853 }
19854
19855 /* Calculate a width or height in pixels from a specification using
19856 the following elements:
19857
19858 SPEC ::=
19859 NUM - a (fractional) multiple of the default font width/height
19860 (NUM) - specifies exactly NUM pixels
19861 UNIT - a fixed number of pixels, see below.
19862 ELEMENT - size of a display element in pixels, see below.
19863 (NUM . SPEC) - equals NUM * SPEC
19864 (+ SPEC SPEC ...) - add pixel values
19865 (- SPEC SPEC ...) - subtract pixel values
19866 (- SPEC) - negate pixel value
19867
19868 NUM ::=
19869 INT or FLOAT - a number constant
19870 SYMBOL - use symbol's (buffer local) variable binding.
19871
19872 UNIT ::=
19873 in - pixels per inch *)
19874 mm - pixels per 1/1000 meter *)
19875 cm - pixels per 1/100 meter *)
19876 width - width of current font in pixels.
19877 height - height of current font in pixels.
19878
19879 *) using the ratio(s) defined in display-pixels-per-inch.
19880
19881 ELEMENT ::=
19882
19883 left-fringe - left fringe width in pixels
19884 right-fringe - right fringe width in pixels
19885
19886 left-margin - left margin width in pixels
19887 right-margin - right margin width in pixels
19888
19889 scroll-bar - scroll-bar area width in pixels
19890
19891 Examples:
19892
19893 Pixels corresponding to 5 inches:
19894 (5 . in)
19895
19896 Total width of non-text areas on left side of window (if scroll-bar is on left):
19897 '(space :width (+ left-fringe left-margin scroll-bar))
19898
19899 Align to first text column (in header line):
19900 '(space :align-to 0)
19901
19902 Align to middle of text area minus half the width of variable `my-image'
19903 containing a loaded image:
19904 '(space :align-to (0.5 . (- text my-image)))
19905
19906 Width of left margin minus width of 1 character in the default font:
19907 '(space :width (- left-margin 1))
19908
19909 Width of left margin minus width of 2 characters in the current font:
19910 '(space :width (- left-margin (2 . width)))
19911
19912 Center 1 character over left-margin (in header line):
19913 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19914
19915 Different ways to express width of left fringe plus left margin minus one pixel:
19916 '(space :width (- (+ left-fringe left-margin) (1)))
19917 '(space :width (+ left-fringe left-margin (- (1))))
19918 '(space :width (+ left-fringe left-margin (-1)))
19919
19920 */
19921
19922 #define NUMVAL(X) \
19923 ((INTEGERP (X) || FLOATP (X)) \
19924 ? XFLOATINT (X) \
19925 : - 1)
19926
19927 int
19928 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19929 double *res;
19930 struct it *it;
19931 Lisp_Object prop;
19932 struct font *font;
19933 int width_p, *align_to;
19934 {
19935 double pixels;
19936
19937 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19938 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19939
19940 if (NILP (prop))
19941 return OK_PIXELS (0);
19942
19943 xassert (FRAME_LIVE_P (it->f));
19944
19945 if (SYMBOLP (prop))
19946 {
19947 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19948 {
19949 char *unit = SDATA (SYMBOL_NAME (prop));
19950
19951 if (unit[0] == 'i' && unit[1] == 'n')
19952 pixels = 1.0;
19953 else if (unit[0] == 'm' && unit[1] == 'm')
19954 pixels = 25.4;
19955 else if (unit[0] == 'c' && unit[1] == 'm')
19956 pixels = 2.54;
19957 else
19958 pixels = 0;
19959 if (pixels > 0)
19960 {
19961 double ppi;
19962 #ifdef HAVE_WINDOW_SYSTEM
19963 if (FRAME_WINDOW_P (it->f)
19964 && (ppi = (width_p
19965 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19966 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19967 ppi > 0))
19968 return OK_PIXELS (ppi / pixels);
19969 #endif
19970
19971 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19972 || (CONSP (Vdisplay_pixels_per_inch)
19973 && (ppi = (width_p
19974 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19975 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19976 ppi > 0)))
19977 return OK_PIXELS (ppi / pixels);
19978
19979 return 0;
19980 }
19981 }
19982
19983 #ifdef HAVE_WINDOW_SYSTEM
19984 if (EQ (prop, Qheight))
19985 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19986 if (EQ (prop, Qwidth))
19987 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19988 #else
19989 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19990 return OK_PIXELS (1);
19991 #endif
19992
19993 if (EQ (prop, Qtext))
19994 return OK_PIXELS (width_p
19995 ? window_box_width (it->w, TEXT_AREA)
19996 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19997
19998 if (align_to && *align_to < 0)
19999 {
20000 *res = 0;
20001 if (EQ (prop, Qleft))
20002 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20003 if (EQ (prop, Qright))
20004 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20005 if (EQ (prop, Qcenter))
20006 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20007 + window_box_width (it->w, TEXT_AREA) / 2);
20008 if (EQ (prop, Qleft_fringe))
20009 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20010 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20011 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20012 if (EQ (prop, Qright_fringe))
20013 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20014 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20015 : window_box_right_offset (it->w, TEXT_AREA));
20016 if (EQ (prop, Qleft_margin))
20017 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20018 if (EQ (prop, Qright_margin))
20019 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20020 if (EQ (prop, Qscroll_bar))
20021 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20022 ? 0
20023 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20024 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20025 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20026 : 0)));
20027 }
20028 else
20029 {
20030 if (EQ (prop, Qleft_fringe))
20031 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20032 if (EQ (prop, Qright_fringe))
20033 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20034 if (EQ (prop, Qleft_margin))
20035 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20036 if (EQ (prop, Qright_margin))
20037 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20038 if (EQ (prop, Qscroll_bar))
20039 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20040 }
20041
20042 prop = Fbuffer_local_value (prop, it->w->buffer);
20043 }
20044
20045 if (INTEGERP (prop) || FLOATP (prop))
20046 {
20047 int base_unit = (width_p
20048 ? FRAME_COLUMN_WIDTH (it->f)
20049 : FRAME_LINE_HEIGHT (it->f));
20050 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20051 }
20052
20053 if (CONSP (prop))
20054 {
20055 Lisp_Object car = XCAR (prop);
20056 Lisp_Object cdr = XCDR (prop);
20057
20058 if (SYMBOLP (car))
20059 {
20060 #ifdef HAVE_WINDOW_SYSTEM
20061 if (FRAME_WINDOW_P (it->f)
20062 && valid_image_p (prop))
20063 {
20064 int id = lookup_image (it->f, prop);
20065 struct image *img = IMAGE_FROM_ID (it->f, id);
20066
20067 return OK_PIXELS (width_p ? img->width : img->height);
20068 }
20069 #endif
20070 if (EQ (car, Qplus) || EQ (car, Qminus))
20071 {
20072 int first = 1;
20073 double px;
20074
20075 pixels = 0;
20076 while (CONSP (cdr))
20077 {
20078 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20079 font, width_p, align_to))
20080 return 0;
20081 if (first)
20082 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20083 else
20084 pixels += px;
20085 cdr = XCDR (cdr);
20086 }
20087 if (EQ (car, Qminus))
20088 pixels = -pixels;
20089 return OK_PIXELS (pixels);
20090 }
20091
20092 car = Fbuffer_local_value (car, it->w->buffer);
20093 }
20094
20095 if (INTEGERP (car) || FLOATP (car))
20096 {
20097 double fact;
20098 pixels = XFLOATINT (car);
20099 if (NILP (cdr))
20100 return OK_PIXELS (pixels);
20101 if (calc_pixel_width_or_height (&fact, it, cdr,
20102 font, width_p, align_to))
20103 return OK_PIXELS (pixels * fact);
20104 return 0;
20105 }
20106
20107 return 0;
20108 }
20109
20110 return 0;
20111 }
20112
20113 \f
20114 /***********************************************************************
20115 Glyph Display
20116 ***********************************************************************/
20117
20118 #ifdef HAVE_WINDOW_SYSTEM
20119
20120 #if GLYPH_DEBUG
20121
20122 void
20123 dump_glyph_string (s)
20124 struct glyph_string *s;
20125 {
20126 fprintf (stderr, "glyph string\n");
20127 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20128 s->x, s->y, s->width, s->height);
20129 fprintf (stderr, " ybase = %d\n", s->ybase);
20130 fprintf (stderr, " hl = %d\n", s->hl);
20131 fprintf (stderr, " left overhang = %d, right = %d\n",
20132 s->left_overhang, s->right_overhang);
20133 fprintf (stderr, " nchars = %d\n", s->nchars);
20134 fprintf (stderr, " extends to end of line = %d\n",
20135 s->extends_to_end_of_line_p);
20136 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20137 fprintf (stderr, " bg width = %d\n", s->background_width);
20138 }
20139
20140 #endif /* GLYPH_DEBUG */
20141
20142 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20143 of XChar2b structures for S; it can't be allocated in
20144 init_glyph_string because it must be allocated via `alloca'. W
20145 is the window on which S is drawn. ROW and AREA are the glyph row
20146 and area within the row from which S is constructed. START is the
20147 index of the first glyph structure covered by S. HL is a
20148 face-override for drawing S. */
20149
20150 #ifdef HAVE_NTGUI
20151 #define OPTIONAL_HDC(hdc) hdc,
20152 #define DECLARE_HDC(hdc) HDC hdc;
20153 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20154 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20155 #endif
20156
20157 #ifndef OPTIONAL_HDC
20158 #define OPTIONAL_HDC(hdc)
20159 #define DECLARE_HDC(hdc)
20160 #define ALLOCATE_HDC(hdc, f)
20161 #define RELEASE_HDC(hdc, f)
20162 #endif
20163
20164 static void
20165 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
20166 struct glyph_string *s;
20167 DECLARE_HDC (hdc)
20168 XChar2b *char2b;
20169 struct window *w;
20170 struct glyph_row *row;
20171 enum glyph_row_area area;
20172 int start;
20173 enum draw_glyphs_face hl;
20174 {
20175 bzero (s, sizeof *s);
20176 s->w = w;
20177 s->f = XFRAME (w->frame);
20178 #ifdef HAVE_NTGUI
20179 s->hdc = hdc;
20180 #endif
20181 s->display = FRAME_X_DISPLAY (s->f);
20182 s->window = FRAME_X_WINDOW (s->f);
20183 s->char2b = char2b;
20184 s->hl = hl;
20185 s->row = row;
20186 s->area = area;
20187 s->first_glyph = row->glyphs[area] + start;
20188 s->height = row->height;
20189 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20190 s->ybase = s->y + row->ascent;
20191 }
20192
20193
20194 /* Append the list of glyph strings with head H and tail T to the list
20195 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20196
20197 static INLINE void
20198 append_glyph_string_lists (head, tail, h, t)
20199 struct glyph_string **head, **tail;
20200 struct glyph_string *h, *t;
20201 {
20202 if (h)
20203 {
20204 if (*head)
20205 (*tail)->next = h;
20206 else
20207 *head = h;
20208 h->prev = *tail;
20209 *tail = t;
20210 }
20211 }
20212
20213
20214 /* Prepend the list of glyph strings with head H and tail T to the
20215 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20216 result. */
20217
20218 static INLINE void
20219 prepend_glyph_string_lists (head, tail, h, t)
20220 struct glyph_string **head, **tail;
20221 struct glyph_string *h, *t;
20222 {
20223 if (h)
20224 {
20225 if (*head)
20226 (*head)->prev = t;
20227 else
20228 *tail = t;
20229 t->next = *head;
20230 *head = h;
20231 }
20232 }
20233
20234
20235 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20236 Set *HEAD and *TAIL to the resulting list. */
20237
20238 static INLINE void
20239 append_glyph_string (head, tail, s)
20240 struct glyph_string **head, **tail;
20241 struct glyph_string *s;
20242 {
20243 s->next = s->prev = NULL;
20244 append_glyph_string_lists (head, tail, s, s);
20245 }
20246
20247
20248 /* Get face and two-byte form of character C in face FACE_ID on frame
20249 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20250 means we want to display multibyte text. DISPLAY_P non-zero means
20251 make sure that X resources for the face returned are allocated.
20252 Value is a pointer to a realized face that is ready for display if
20253 DISPLAY_P is non-zero. */
20254
20255 static INLINE struct face *
20256 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
20257 struct frame *f;
20258 int c, face_id;
20259 XChar2b *char2b;
20260 int multibyte_p, display_p;
20261 {
20262 struct face *face = FACE_FROM_ID (f, face_id);
20263
20264 if (face->font)
20265 {
20266 unsigned code = face->font->driver->encode_char (face->font, c);
20267
20268 if (code != FONT_INVALID_CODE)
20269 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20270 else
20271 STORE_XCHAR2B (char2b, 0, 0);
20272 }
20273
20274 /* Make sure X resources of the face are allocated. */
20275 #ifdef HAVE_X_WINDOWS
20276 if (display_p)
20277 #endif
20278 {
20279 xassert (face != NULL);
20280 PREPARE_FACE_FOR_DISPLAY (f, face);
20281 }
20282
20283 return face;
20284 }
20285
20286
20287 /* Get face and two-byte form of character glyph GLYPH on frame F.
20288 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20289 a pointer to a realized face that is ready for display. */
20290
20291 static INLINE struct face *
20292 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
20293 struct frame *f;
20294 struct glyph *glyph;
20295 XChar2b *char2b;
20296 int *two_byte_p;
20297 {
20298 struct face *face;
20299
20300 xassert (glyph->type == CHAR_GLYPH);
20301 face = FACE_FROM_ID (f, glyph->face_id);
20302
20303 if (two_byte_p)
20304 *two_byte_p = 0;
20305
20306 if (face->font)
20307 {
20308 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
20309
20310 if (code != FONT_INVALID_CODE)
20311 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20312 else
20313 STORE_XCHAR2B (char2b, 0, 0);
20314 }
20315
20316 /* Make sure X resources of the face are allocated. */
20317 xassert (face != NULL);
20318 PREPARE_FACE_FOR_DISPLAY (f, face);
20319 return face;
20320 }
20321
20322
20323 /* Fill glyph string S with composition components specified by S->cmp.
20324
20325 BASE_FACE is the base face of the composition.
20326 S->cmp_from is the index of the first component for S.
20327
20328 OVERLAPS non-zero means S should draw the foreground only, and use
20329 its physical height for clipping. See also draw_glyphs.
20330
20331 Value is the index of a component not in S. */
20332
20333 static int
20334 fill_composite_glyph_string (s, base_face, overlaps)
20335 struct glyph_string *s;
20336 struct face *base_face;
20337 int overlaps;
20338 {
20339 int i;
20340 /* For all glyphs of this composition, starting at the offset
20341 S->cmp_from, until we reach the end of the definition or encounter a
20342 glyph that requires the different face, add it to S. */
20343 struct face *face;
20344
20345 xassert (s);
20346
20347 s->for_overlaps = overlaps;
20348 s->face = NULL;
20349 s->font = NULL;
20350 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20351 {
20352 int c = COMPOSITION_GLYPH (s->cmp, i);
20353
20354 if (c != '\t')
20355 {
20356 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20357 -1, Qnil);
20358
20359 face = get_char_face_and_encoding (s->f, c, face_id,
20360 s->char2b + i, 1, 1);
20361 if (face)
20362 {
20363 if (! s->face)
20364 {
20365 s->face = face;
20366 s->font = s->face->font;
20367 }
20368 else if (s->face != face)
20369 break;
20370 }
20371 }
20372 ++s->nchars;
20373 }
20374 s->cmp_to = i;
20375
20376 /* All glyph strings for the same composition has the same width,
20377 i.e. the width set for the first component of the composition. */
20378 s->width = s->first_glyph->pixel_width;
20379
20380 /* If the specified font could not be loaded, use the frame's
20381 default font, but record the fact that we couldn't load it in
20382 the glyph string so that we can draw rectangles for the
20383 characters of the glyph string. */
20384 if (s->font == NULL)
20385 {
20386 s->font_not_found_p = 1;
20387 s->font = FRAME_FONT (s->f);
20388 }
20389
20390 /* Adjust base line for subscript/superscript text. */
20391 s->ybase += s->first_glyph->voffset;
20392
20393 /* This glyph string must always be drawn with 16-bit functions. */
20394 s->two_byte_p = 1;
20395
20396 return s->cmp_to;
20397 }
20398
20399 static int
20400 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
20401 struct glyph_string *s;
20402 int face_id;
20403 int start, end, overlaps;
20404 {
20405 struct glyph *glyph, *last;
20406 Lisp_Object lgstring;
20407 int i;
20408
20409 s->for_overlaps = overlaps;
20410 glyph = s->row->glyphs[s->area] + start;
20411 last = s->row->glyphs[s->area] + end;
20412 s->cmp_id = glyph->u.cmp.id;
20413 s->cmp_from = glyph->u.cmp.from;
20414 s->cmp_to = glyph->u.cmp.to + 1;
20415 s->face = FACE_FROM_ID (s->f, face_id);
20416 lgstring = composition_gstring_from_id (s->cmp_id);
20417 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20418 glyph++;
20419 while (glyph < last
20420 && glyph->u.cmp.automatic
20421 && glyph->u.cmp.id == s->cmp_id
20422 && s->cmp_to == glyph->u.cmp.from)
20423 s->cmp_to = (glyph++)->u.cmp.to + 1;
20424
20425 for (i = s->cmp_from; i < s->cmp_to; i++)
20426 {
20427 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20428 unsigned code = LGLYPH_CODE (lglyph);
20429
20430 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20431 }
20432 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20433 return glyph - s->row->glyphs[s->area];
20434 }
20435
20436
20437 /* Fill glyph string S from a sequence of character glyphs.
20438
20439 FACE_ID is the face id of the string. START is the index of the
20440 first glyph to consider, END is the index of the last + 1.
20441 OVERLAPS non-zero means S should draw the foreground only, and use
20442 its physical height for clipping. See also draw_glyphs.
20443
20444 Value is the index of the first glyph not in S. */
20445
20446 static int
20447 fill_glyph_string (s, face_id, start, end, overlaps)
20448 struct glyph_string *s;
20449 int face_id;
20450 int start, end, overlaps;
20451 {
20452 struct glyph *glyph, *last;
20453 int voffset;
20454 int glyph_not_available_p;
20455
20456 xassert (s->f == XFRAME (s->w->frame));
20457 xassert (s->nchars == 0);
20458 xassert (start >= 0 && end > start);
20459
20460 s->for_overlaps = overlaps;
20461 glyph = s->row->glyphs[s->area] + start;
20462 last = s->row->glyphs[s->area] + end;
20463 voffset = glyph->voffset;
20464 s->padding_p = glyph->padding_p;
20465 glyph_not_available_p = glyph->glyph_not_available_p;
20466
20467 while (glyph < last
20468 && glyph->type == CHAR_GLYPH
20469 && glyph->voffset == voffset
20470 /* Same face id implies same font, nowadays. */
20471 && glyph->face_id == face_id
20472 && glyph->glyph_not_available_p == glyph_not_available_p)
20473 {
20474 int two_byte_p;
20475
20476 s->face = get_glyph_face_and_encoding (s->f, glyph,
20477 s->char2b + s->nchars,
20478 &two_byte_p);
20479 s->two_byte_p = two_byte_p;
20480 ++s->nchars;
20481 xassert (s->nchars <= end - start);
20482 s->width += glyph->pixel_width;
20483 if (glyph++->padding_p != s->padding_p)
20484 break;
20485 }
20486
20487 s->font = s->face->font;
20488
20489 /* If the specified font could not be loaded, use the frame's font,
20490 but record the fact that we couldn't load it in
20491 S->font_not_found_p so that we can draw rectangles for the
20492 characters of the glyph string. */
20493 if (s->font == NULL || glyph_not_available_p)
20494 {
20495 s->font_not_found_p = 1;
20496 s->font = FRAME_FONT (s->f);
20497 }
20498
20499 /* Adjust base line for subscript/superscript text. */
20500 s->ybase += voffset;
20501
20502 xassert (s->face && s->face->gc);
20503 return glyph - s->row->glyphs[s->area];
20504 }
20505
20506
20507 /* Fill glyph string S from image glyph S->first_glyph. */
20508
20509 static void
20510 fill_image_glyph_string (s)
20511 struct glyph_string *s;
20512 {
20513 xassert (s->first_glyph->type == IMAGE_GLYPH);
20514 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20515 xassert (s->img);
20516 s->slice = s->first_glyph->slice;
20517 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20518 s->font = s->face->font;
20519 s->width = s->first_glyph->pixel_width;
20520
20521 /* Adjust base line for subscript/superscript text. */
20522 s->ybase += s->first_glyph->voffset;
20523 }
20524
20525
20526 /* Fill glyph string S from a sequence of stretch glyphs.
20527
20528 ROW is the glyph row in which the glyphs are found, AREA is the
20529 area within the row. START is the index of the first glyph to
20530 consider, END is the index of the last + 1.
20531
20532 Value is the index of the first glyph not in S. */
20533
20534 static int
20535 fill_stretch_glyph_string (s, row, area, start, end)
20536 struct glyph_string *s;
20537 struct glyph_row *row;
20538 enum glyph_row_area area;
20539 int start, end;
20540 {
20541 struct glyph *glyph, *last;
20542 int voffset, face_id;
20543
20544 xassert (s->first_glyph->type == STRETCH_GLYPH);
20545
20546 glyph = s->row->glyphs[s->area] + start;
20547 last = s->row->glyphs[s->area] + end;
20548 face_id = glyph->face_id;
20549 s->face = FACE_FROM_ID (s->f, face_id);
20550 s->font = s->face->font;
20551 s->width = glyph->pixel_width;
20552 s->nchars = 1;
20553 voffset = glyph->voffset;
20554
20555 for (++glyph;
20556 (glyph < last
20557 && glyph->type == STRETCH_GLYPH
20558 && glyph->voffset == voffset
20559 && glyph->face_id == face_id);
20560 ++glyph)
20561 s->width += glyph->pixel_width;
20562
20563 /* Adjust base line for subscript/superscript text. */
20564 s->ybase += voffset;
20565
20566 /* The case that face->gc == 0 is handled when drawing the glyph
20567 string by calling PREPARE_FACE_FOR_DISPLAY. */
20568 xassert (s->face);
20569 return glyph - s->row->glyphs[s->area];
20570 }
20571
20572 static struct font_metrics *
20573 get_per_char_metric (f, font, char2b)
20574 struct frame *f;
20575 struct font *font;
20576 XChar2b *char2b;
20577 {
20578 static struct font_metrics metrics;
20579 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20580
20581 if (! font || code == FONT_INVALID_CODE)
20582 return NULL;
20583 font->driver->text_extents (font, &code, 1, &metrics);
20584 return &metrics;
20585 }
20586
20587 /* EXPORT for RIF:
20588 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20589 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20590 assumed to be zero. */
20591
20592 void
20593 x_get_glyph_overhangs (glyph, f, left, right)
20594 struct glyph *glyph;
20595 struct frame *f;
20596 int *left, *right;
20597 {
20598 *left = *right = 0;
20599
20600 if (glyph->type == CHAR_GLYPH)
20601 {
20602 struct face *face;
20603 XChar2b char2b;
20604 struct font_metrics *pcm;
20605
20606 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20607 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20608 {
20609 if (pcm->rbearing > pcm->width)
20610 *right = pcm->rbearing - pcm->width;
20611 if (pcm->lbearing < 0)
20612 *left = -pcm->lbearing;
20613 }
20614 }
20615 else if (glyph->type == COMPOSITE_GLYPH)
20616 {
20617 if (! glyph->u.cmp.automatic)
20618 {
20619 struct composition *cmp = composition_table[glyph->u.cmp.id];
20620
20621 if (cmp->rbearing > cmp->pixel_width)
20622 *right = cmp->rbearing - cmp->pixel_width;
20623 if (cmp->lbearing < 0)
20624 *left = - cmp->lbearing;
20625 }
20626 else
20627 {
20628 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20629 struct font_metrics metrics;
20630
20631 composition_gstring_width (gstring, glyph->u.cmp.from,
20632 glyph->u.cmp.to + 1, &metrics);
20633 if (metrics.rbearing > metrics.width)
20634 *right = metrics.rbearing - metrics.width;
20635 if (metrics.lbearing < 0)
20636 *left = - metrics.lbearing;
20637 }
20638 }
20639 }
20640
20641
20642 /* Return the index of the first glyph preceding glyph string S that
20643 is overwritten by S because of S's left overhang. Value is -1
20644 if no glyphs are overwritten. */
20645
20646 static int
20647 left_overwritten (s)
20648 struct glyph_string *s;
20649 {
20650 int k;
20651
20652 if (s->left_overhang)
20653 {
20654 int x = 0, i;
20655 struct glyph *glyphs = s->row->glyphs[s->area];
20656 int first = s->first_glyph - glyphs;
20657
20658 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20659 x -= glyphs[i].pixel_width;
20660
20661 k = i + 1;
20662 }
20663 else
20664 k = -1;
20665
20666 return k;
20667 }
20668
20669
20670 /* Return the index of the first glyph preceding glyph string S that
20671 is overwriting S because of its right overhang. Value is -1 if no
20672 glyph in front of S overwrites S. */
20673
20674 static int
20675 left_overwriting (s)
20676 struct glyph_string *s;
20677 {
20678 int i, k, x;
20679 struct glyph *glyphs = s->row->glyphs[s->area];
20680 int first = s->first_glyph - glyphs;
20681
20682 k = -1;
20683 x = 0;
20684 for (i = first - 1; i >= 0; --i)
20685 {
20686 int left, right;
20687 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20688 if (x + right > 0)
20689 k = i;
20690 x -= glyphs[i].pixel_width;
20691 }
20692
20693 return k;
20694 }
20695
20696
20697 /* Return the index of the last glyph following glyph string S that is
20698 overwritten by S because of S's right overhang. Value is -1 if
20699 no such glyph is found. */
20700
20701 static int
20702 right_overwritten (s)
20703 struct glyph_string *s;
20704 {
20705 int k = -1;
20706
20707 if (s->right_overhang)
20708 {
20709 int x = 0, i;
20710 struct glyph *glyphs = s->row->glyphs[s->area];
20711 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20712 int end = s->row->used[s->area];
20713
20714 for (i = first; i < end && s->right_overhang > x; ++i)
20715 x += glyphs[i].pixel_width;
20716
20717 k = i;
20718 }
20719
20720 return k;
20721 }
20722
20723
20724 /* Return the index of the last glyph following glyph string S that
20725 overwrites S because of its left overhang. Value is negative
20726 if no such glyph is found. */
20727
20728 static int
20729 right_overwriting (s)
20730 struct glyph_string *s;
20731 {
20732 int i, k, x;
20733 int end = s->row->used[s->area];
20734 struct glyph *glyphs = s->row->glyphs[s->area];
20735 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20736
20737 k = -1;
20738 x = 0;
20739 for (i = first; i < end; ++i)
20740 {
20741 int left, right;
20742 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20743 if (x - left < 0)
20744 k = i;
20745 x += glyphs[i].pixel_width;
20746 }
20747
20748 return k;
20749 }
20750
20751
20752 /* Set background width of glyph string S. START is the index of the
20753 first glyph following S. LAST_X is the right-most x-position + 1
20754 in the drawing area. */
20755
20756 static INLINE void
20757 set_glyph_string_background_width (s, start, last_x)
20758 struct glyph_string *s;
20759 int start;
20760 int last_x;
20761 {
20762 /* If the face of this glyph string has to be drawn to the end of
20763 the drawing area, set S->extends_to_end_of_line_p. */
20764
20765 if (start == s->row->used[s->area]
20766 && s->area == TEXT_AREA
20767 && ((s->row->fill_line_p
20768 && (s->hl == DRAW_NORMAL_TEXT
20769 || s->hl == DRAW_IMAGE_RAISED
20770 || s->hl == DRAW_IMAGE_SUNKEN))
20771 || s->hl == DRAW_MOUSE_FACE))
20772 s->extends_to_end_of_line_p = 1;
20773
20774 /* If S extends its face to the end of the line, set its
20775 background_width to the distance to the right edge of the drawing
20776 area. */
20777 if (s->extends_to_end_of_line_p)
20778 s->background_width = last_x - s->x + 1;
20779 else
20780 s->background_width = s->width;
20781 }
20782
20783
20784 /* Compute overhangs and x-positions for glyph string S and its
20785 predecessors, or successors. X is the starting x-position for S.
20786 BACKWARD_P non-zero means process predecessors. */
20787
20788 static void
20789 compute_overhangs_and_x (s, x, backward_p)
20790 struct glyph_string *s;
20791 int x;
20792 int backward_p;
20793 {
20794 if (backward_p)
20795 {
20796 while (s)
20797 {
20798 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20799 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20800 x -= s->width;
20801 s->x = x;
20802 s = s->prev;
20803 }
20804 }
20805 else
20806 {
20807 while (s)
20808 {
20809 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20810 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20811 s->x = x;
20812 x += s->width;
20813 s = s->next;
20814 }
20815 }
20816 }
20817
20818
20819
20820 /* The following macros are only called from draw_glyphs below.
20821 They reference the following parameters of that function directly:
20822 `w', `row', `area', and `overlap_p'
20823 as well as the following local variables:
20824 `s', `f', and `hdc' (in W32) */
20825
20826 #ifdef HAVE_NTGUI
20827 /* On W32, silently add local `hdc' variable to argument list of
20828 init_glyph_string. */
20829 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20830 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20831 #else
20832 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20833 init_glyph_string (s, char2b, w, row, area, start, hl)
20834 #endif
20835
20836 /* Add a glyph string for a stretch glyph to the list of strings
20837 between HEAD and TAIL. START is the index of the stretch glyph in
20838 row area AREA of glyph row ROW. END is the index of the last glyph
20839 in that glyph row area. X is the current output position assigned
20840 to the new glyph string constructed. HL overrides that face of the
20841 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20842 is the right-most x-position of the drawing area. */
20843
20844 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20845 and below -- keep them on one line. */
20846 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20847 do \
20848 { \
20849 s = (struct glyph_string *) alloca (sizeof *s); \
20850 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20851 START = fill_stretch_glyph_string (s, row, area, START, END); \
20852 append_glyph_string (&HEAD, &TAIL, s); \
20853 s->x = (X); \
20854 } \
20855 while (0)
20856
20857
20858 /* Add a glyph string for an image glyph to the list of strings
20859 between HEAD and TAIL. START is the index of the image glyph in
20860 row area AREA of glyph row ROW. END is the index of the last glyph
20861 in that glyph row area. X is the current output position assigned
20862 to the new glyph string constructed. HL overrides that face of the
20863 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20864 is the right-most x-position of the drawing area. */
20865
20866 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20867 do \
20868 { \
20869 s = (struct glyph_string *) alloca (sizeof *s); \
20870 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20871 fill_image_glyph_string (s); \
20872 append_glyph_string (&HEAD, &TAIL, s); \
20873 ++START; \
20874 s->x = (X); \
20875 } \
20876 while (0)
20877
20878
20879 /* Add a glyph string for a sequence of character glyphs to the list
20880 of strings between HEAD and TAIL. START is the index of the first
20881 glyph in row area AREA of glyph row ROW that is part of the new
20882 glyph string. END is the index of the last glyph in that glyph row
20883 area. X is the current output position assigned to the new glyph
20884 string constructed. HL overrides that face of the glyph; e.g. it
20885 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20886 right-most x-position of the drawing area. */
20887
20888 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20889 do \
20890 { \
20891 int face_id; \
20892 XChar2b *char2b; \
20893 \
20894 face_id = (row)->glyphs[area][START].face_id; \
20895 \
20896 s = (struct glyph_string *) alloca (sizeof *s); \
20897 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20898 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20899 append_glyph_string (&HEAD, &TAIL, s); \
20900 s->x = (X); \
20901 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20902 } \
20903 while (0)
20904
20905
20906 /* Add a glyph string for a composite sequence to the list of strings
20907 between HEAD and TAIL. START is the index of the first glyph in
20908 row area AREA of glyph row ROW that is part of the new glyph
20909 string. END is the index of the last glyph in that glyph row area.
20910 X is the current output position assigned to the new glyph string
20911 constructed. HL overrides that face of the glyph; e.g. it is
20912 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20913 x-position of the drawing area. */
20914
20915 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20916 do { \
20917 int face_id = (row)->glyphs[area][START].face_id; \
20918 struct face *base_face = FACE_FROM_ID (f, face_id); \
20919 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20920 struct composition *cmp = composition_table[cmp_id]; \
20921 XChar2b *char2b; \
20922 struct glyph_string *first_s; \
20923 int n; \
20924 \
20925 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20926 \
20927 /* Make glyph_strings for each glyph sequence that is drawable by \
20928 the same face, and append them to HEAD/TAIL. */ \
20929 for (n = 0; n < cmp->glyph_len;) \
20930 { \
20931 s = (struct glyph_string *) alloca (sizeof *s); \
20932 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20933 append_glyph_string (&(HEAD), &(TAIL), s); \
20934 s->cmp = cmp; \
20935 s->cmp_from = n; \
20936 s->x = (X); \
20937 if (n == 0) \
20938 first_s = s; \
20939 n = fill_composite_glyph_string (s, base_face, overlaps); \
20940 } \
20941 \
20942 ++START; \
20943 s = first_s; \
20944 } while (0)
20945
20946
20947 /* Add a glyph string for a glyph-string sequence to the list of strings
20948 between HEAD and TAIL. */
20949
20950 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20951 do { \
20952 int face_id; \
20953 XChar2b *char2b; \
20954 Lisp_Object gstring; \
20955 \
20956 face_id = (row)->glyphs[area][START].face_id; \
20957 gstring = (composition_gstring_from_id \
20958 ((row)->glyphs[area][START].u.cmp.id)); \
20959 s = (struct glyph_string *) alloca (sizeof *s); \
20960 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20961 * LGSTRING_GLYPH_LEN (gstring)); \
20962 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20963 append_glyph_string (&(HEAD), &(TAIL), s); \
20964 s->x = (X); \
20965 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20966 } while (0)
20967
20968
20969 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20970 of AREA of glyph row ROW on window W between indices START and END.
20971 HL overrides the face for drawing glyph strings, e.g. it is
20972 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20973 x-positions of the drawing area.
20974
20975 This is an ugly monster macro construct because we must use alloca
20976 to allocate glyph strings (because draw_glyphs can be called
20977 asynchronously). */
20978
20979 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20980 do \
20981 { \
20982 HEAD = TAIL = NULL; \
20983 while (START < END) \
20984 { \
20985 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20986 switch (first_glyph->type) \
20987 { \
20988 case CHAR_GLYPH: \
20989 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20990 HL, X, LAST_X); \
20991 break; \
20992 \
20993 case COMPOSITE_GLYPH: \
20994 if (first_glyph->u.cmp.automatic) \
20995 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20996 HL, X, LAST_X); \
20997 else \
20998 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20999 HL, X, LAST_X); \
21000 break; \
21001 \
21002 case STRETCH_GLYPH: \
21003 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21004 HL, X, LAST_X); \
21005 break; \
21006 \
21007 case IMAGE_GLYPH: \
21008 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21009 HL, X, LAST_X); \
21010 break; \
21011 \
21012 default: \
21013 abort (); \
21014 } \
21015 \
21016 if (s) \
21017 { \
21018 set_glyph_string_background_width (s, START, LAST_X); \
21019 (X) += s->width; \
21020 } \
21021 } \
21022 } while (0)
21023
21024
21025 /* Draw glyphs between START and END in AREA of ROW on window W,
21026 starting at x-position X. X is relative to AREA in W. HL is a
21027 face-override with the following meaning:
21028
21029 DRAW_NORMAL_TEXT draw normally
21030 DRAW_CURSOR draw in cursor face
21031 DRAW_MOUSE_FACE draw in mouse face.
21032 DRAW_INVERSE_VIDEO draw in mode line face
21033 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21034 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21035
21036 If OVERLAPS is non-zero, draw only the foreground of characters and
21037 clip to the physical height of ROW. Non-zero value also defines
21038 the overlapping part to be drawn:
21039
21040 OVERLAPS_PRED overlap with preceding rows
21041 OVERLAPS_SUCC overlap with succeeding rows
21042 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21043 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21044
21045 Value is the x-position reached, relative to AREA of W. */
21046
21047 static int
21048 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
21049 struct window *w;
21050 int x;
21051 struct glyph_row *row;
21052 enum glyph_row_area area;
21053 EMACS_INT start, end;
21054 enum draw_glyphs_face hl;
21055 int overlaps;
21056 {
21057 struct glyph_string *head, *tail;
21058 struct glyph_string *s;
21059 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21060 int i, j, x_reached, last_x, area_left = 0;
21061 struct frame *f = XFRAME (WINDOW_FRAME (w));
21062 DECLARE_HDC (hdc);
21063
21064 ALLOCATE_HDC (hdc, f);
21065
21066 /* Let's rather be paranoid than getting a SEGV. */
21067 end = min (end, row->used[area]);
21068 start = max (0, start);
21069 start = min (end, start);
21070
21071 /* Translate X to frame coordinates. Set last_x to the right
21072 end of the drawing area. */
21073 if (row->full_width_p)
21074 {
21075 /* X is relative to the left edge of W, without scroll bars
21076 or fringes. */
21077 area_left = WINDOW_LEFT_EDGE_X (w);
21078 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21079 }
21080 else
21081 {
21082 area_left = window_box_left (w, area);
21083 last_x = area_left + window_box_width (w, area);
21084 }
21085 x += area_left;
21086
21087 /* Build a doubly-linked list of glyph_string structures between
21088 head and tail from what we have to draw. Note that the macro
21089 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21090 the reason we use a separate variable `i'. */
21091 i = start;
21092 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21093 if (tail)
21094 x_reached = tail->x + tail->background_width;
21095 else
21096 x_reached = x;
21097
21098 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21099 the row, redraw some glyphs in front or following the glyph
21100 strings built above. */
21101 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21102 {
21103 struct glyph_string *h, *t;
21104 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21105 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21106 int dummy_x = 0;
21107
21108 /* If mouse highlighting is on, we may need to draw adjacent
21109 glyphs using mouse-face highlighting. */
21110 if (area == TEXT_AREA && row->mouse_face_p)
21111 {
21112 struct glyph_row *mouse_beg_row, *mouse_end_row;
21113
21114 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21115 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21116
21117 if (row >= mouse_beg_row && row <= mouse_end_row)
21118 {
21119 check_mouse_face = 1;
21120 mouse_beg_col = (row == mouse_beg_row)
21121 ? dpyinfo->mouse_face_beg_col : 0;
21122 mouse_end_col = (row == mouse_end_row)
21123 ? dpyinfo->mouse_face_end_col
21124 : row->used[TEXT_AREA];
21125 }
21126 }
21127
21128 /* Compute overhangs for all glyph strings. */
21129 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21130 for (s = head; s; s = s->next)
21131 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21132
21133 /* Prepend glyph strings for glyphs in front of the first glyph
21134 string that are overwritten because of the first glyph
21135 string's left overhang. The background of all strings
21136 prepended must be drawn because the first glyph string
21137 draws over it. */
21138 i = left_overwritten (head);
21139 if (i >= 0)
21140 {
21141 enum draw_glyphs_face overlap_hl;
21142
21143 /* If this row contains mouse highlighting, attempt to draw
21144 the overlapped glyphs with the correct highlight. This
21145 code fails if the overlap encompasses more than one glyph
21146 and mouse-highlight spans only some of these glyphs.
21147 However, making it work perfectly involves a lot more
21148 code, and I don't know if the pathological case occurs in
21149 practice, so we'll stick to this for now. --- cyd */
21150 if (check_mouse_face
21151 && mouse_beg_col < start && mouse_end_col > i)
21152 overlap_hl = DRAW_MOUSE_FACE;
21153 else
21154 overlap_hl = DRAW_NORMAL_TEXT;
21155
21156 j = i;
21157 BUILD_GLYPH_STRINGS (j, start, h, t,
21158 overlap_hl, dummy_x, last_x);
21159 start = i;
21160 compute_overhangs_and_x (t, head->x, 1);
21161 prepend_glyph_string_lists (&head, &tail, h, t);
21162 clip_head = head;
21163 }
21164
21165 /* Prepend glyph strings for glyphs in front of the first glyph
21166 string that overwrite that glyph string because of their
21167 right overhang. For these strings, only the foreground must
21168 be drawn, because it draws over the glyph string at `head'.
21169 The background must not be drawn because this would overwrite
21170 right overhangs of preceding glyphs for which no glyph
21171 strings exist. */
21172 i = left_overwriting (head);
21173 if (i >= 0)
21174 {
21175 enum draw_glyphs_face overlap_hl;
21176
21177 if (check_mouse_face
21178 && mouse_beg_col < start && mouse_end_col > i)
21179 overlap_hl = DRAW_MOUSE_FACE;
21180 else
21181 overlap_hl = DRAW_NORMAL_TEXT;
21182
21183 clip_head = head;
21184 BUILD_GLYPH_STRINGS (i, start, h, t,
21185 overlap_hl, dummy_x, last_x);
21186 for (s = h; s; s = s->next)
21187 s->background_filled_p = 1;
21188 compute_overhangs_and_x (t, head->x, 1);
21189 prepend_glyph_string_lists (&head, &tail, h, t);
21190 }
21191
21192 /* Append glyphs strings for glyphs following the last glyph
21193 string tail that are overwritten by tail. The background of
21194 these strings has to be drawn because tail's foreground draws
21195 over it. */
21196 i = right_overwritten (tail);
21197 if (i >= 0)
21198 {
21199 enum draw_glyphs_face overlap_hl;
21200
21201 if (check_mouse_face
21202 && mouse_beg_col < i && mouse_end_col > end)
21203 overlap_hl = DRAW_MOUSE_FACE;
21204 else
21205 overlap_hl = DRAW_NORMAL_TEXT;
21206
21207 BUILD_GLYPH_STRINGS (end, i, h, t,
21208 overlap_hl, x, last_x);
21209 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21210 we don't have `end = i;' here. */
21211 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21212 append_glyph_string_lists (&head, &tail, h, t);
21213 clip_tail = tail;
21214 }
21215
21216 /* Append glyph strings for glyphs following the last glyph
21217 string tail that overwrite tail. The foreground of such
21218 glyphs has to be drawn because it writes into the background
21219 of tail. The background must not be drawn because it could
21220 paint over the foreground of following glyphs. */
21221 i = right_overwriting (tail);
21222 if (i >= 0)
21223 {
21224 enum draw_glyphs_face overlap_hl;
21225 if (check_mouse_face
21226 && mouse_beg_col < i && mouse_end_col > end)
21227 overlap_hl = DRAW_MOUSE_FACE;
21228 else
21229 overlap_hl = DRAW_NORMAL_TEXT;
21230
21231 clip_tail = tail;
21232 i++; /* We must include the Ith glyph. */
21233 BUILD_GLYPH_STRINGS (end, i, h, t,
21234 overlap_hl, x, last_x);
21235 for (s = h; s; s = s->next)
21236 s->background_filled_p = 1;
21237 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21238 append_glyph_string_lists (&head, &tail, h, t);
21239 }
21240 if (clip_head || clip_tail)
21241 for (s = head; s; s = s->next)
21242 {
21243 s->clip_head = clip_head;
21244 s->clip_tail = clip_tail;
21245 }
21246 }
21247
21248 /* Draw all strings. */
21249 for (s = head; s; s = s->next)
21250 FRAME_RIF (f)->draw_glyph_string (s);
21251
21252 #ifndef HAVE_NS
21253 /* When focus a sole frame and move horizontally, this sets on_p to 0
21254 causing a failure to erase prev cursor position. */
21255 if (area == TEXT_AREA
21256 && !row->full_width_p
21257 /* When drawing overlapping rows, only the glyph strings'
21258 foreground is drawn, which doesn't erase a cursor
21259 completely. */
21260 && !overlaps)
21261 {
21262 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21263 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21264 : (tail ? tail->x + tail->background_width : x));
21265 x0 -= area_left;
21266 x1 -= area_left;
21267
21268 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21269 row->y, MATRIX_ROW_BOTTOM_Y (row));
21270 }
21271 #endif
21272
21273 /* Value is the x-position up to which drawn, relative to AREA of W.
21274 This doesn't include parts drawn because of overhangs. */
21275 if (row->full_width_p)
21276 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21277 else
21278 x_reached -= area_left;
21279
21280 RELEASE_HDC (hdc, f);
21281
21282 return x_reached;
21283 }
21284
21285 /* Expand row matrix if too narrow. Don't expand if area
21286 is not present. */
21287
21288 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21289 { \
21290 if (!fonts_changed_p \
21291 && (it->glyph_row->glyphs[area] \
21292 < it->glyph_row->glyphs[area + 1])) \
21293 { \
21294 it->w->ncols_scale_factor++; \
21295 fonts_changed_p = 1; \
21296 } \
21297 }
21298
21299 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21300 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21301
21302 static INLINE void
21303 append_glyph (it)
21304 struct it *it;
21305 {
21306 struct glyph *glyph;
21307 enum glyph_row_area area = it->area;
21308
21309 xassert (it->glyph_row);
21310 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21311
21312 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21313 if (glyph < it->glyph_row->glyphs[area + 1])
21314 {
21315 /* If the glyph row is reversed, we need to prepend the glyph
21316 rather than append it. */
21317 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21318 {
21319 struct glyph *g;
21320
21321 /* Make room for the additional glyph. */
21322 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21323 g[1] = *g;
21324 glyph = it->glyph_row->glyphs[area];
21325 }
21326 glyph->charpos = CHARPOS (it->position);
21327 glyph->object = it->object;
21328 if (it->pixel_width > 0)
21329 {
21330 glyph->pixel_width = it->pixel_width;
21331 glyph->padding_p = 0;
21332 }
21333 else
21334 {
21335 /* Assure at least 1-pixel width. Otherwise, cursor can't
21336 be displayed correctly. */
21337 glyph->pixel_width = 1;
21338 glyph->padding_p = 1;
21339 }
21340 glyph->ascent = it->ascent;
21341 glyph->descent = it->descent;
21342 glyph->voffset = it->voffset;
21343 glyph->type = CHAR_GLYPH;
21344 glyph->avoid_cursor_p = it->avoid_cursor_p;
21345 glyph->multibyte_p = it->multibyte_p;
21346 glyph->left_box_line_p = it->start_of_box_run_p;
21347 glyph->right_box_line_p = it->end_of_box_run_p;
21348 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21349 || it->phys_descent > it->descent);
21350 glyph->glyph_not_available_p = it->glyph_not_available_p;
21351 glyph->face_id = it->face_id;
21352 glyph->u.ch = it->char_to_display;
21353 glyph->slice = null_glyph_slice;
21354 glyph->font_type = FONT_TYPE_UNKNOWN;
21355 if (it->bidi_p)
21356 {
21357 glyph->resolved_level = it->bidi_it.resolved_level;
21358 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21359 abort ();
21360 glyph->bidi_type = it->bidi_it.type;
21361 }
21362 else
21363 {
21364 glyph->resolved_level = 0;
21365 glyph->bidi_type = UNKNOWN_BT;
21366 }
21367 ++it->glyph_row->used[area];
21368 }
21369 else
21370 IT_EXPAND_MATRIX_WIDTH (it, area);
21371 }
21372
21373 /* Store one glyph for the composition IT->cmp_it.id in
21374 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21375 non-null. */
21376
21377 static INLINE void
21378 append_composite_glyph (it)
21379 struct it *it;
21380 {
21381 struct glyph *glyph;
21382 enum glyph_row_area area = it->area;
21383
21384 xassert (it->glyph_row);
21385
21386 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21387 if (glyph < it->glyph_row->glyphs[area + 1])
21388 {
21389 glyph->charpos = CHARPOS (it->position);
21390 glyph->object = it->object;
21391 glyph->pixel_width = it->pixel_width;
21392 glyph->ascent = it->ascent;
21393 glyph->descent = it->descent;
21394 glyph->voffset = it->voffset;
21395 glyph->type = COMPOSITE_GLYPH;
21396 if (it->cmp_it.ch < 0)
21397 {
21398 glyph->u.cmp.automatic = 0;
21399 glyph->u.cmp.id = it->cmp_it.id;
21400 }
21401 else
21402 {
21403 glyph->u.cmp.automatic = 1;
21404 glyph->u.cmp.id = it->cmp_it.id;
21405 glyph->u.cmp.from = it->cmp_it.from;
21406 glyph->u.cmp.to = it->cmp_it.to - 1;
21407 }
21408 glyph->avoid_cursor_p = it->avoid_cursor_p;
21409 glyph->multibyte_p = it->multibyte_p;
21410 glyph->left_box_line_p = it->start_of_box_run_p;
21411 glyph->right_box_line_p = it->end_of_box_run_p;
21412 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21413 || it->phys_descent > it->descent);
21414 glyph->padding_p = 0;
21415 glyph->glyph_not_available_p = 0;
21416 glyph->face_id = it->face_id;
21417 glyph->slice = null_glyph_slice;
21418 glyph->font_type = FONT_TYPE_UNKNOWN;
21419 if (it->bidi_p)
21420 {
21421 glyph->resolved_level = it->bidi_it.resolved_level;
21422 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21423 abort ();
21424 glyph->bidi_type = it->bidi_it.type;
21425 }
21426 ++it->glyph_row->used[area];
21427 }
21428 else
21429 IT_EXPAND_MATRIX_WIDTH (it, area);
21430 }
21431
21432
21433 /* Change IT->ascent and IT->height according to the setting of
21434 IT->voffset. */
21435
21436 static INLINE void
21437 take_vertical_position_into_account (it)
21438 struct it *it;
21439 {
21440 if (it->voffset)
21441 {
21442 if (it->voffset < 0)
21443 /* Increase the ascent so that we can display the text higher
21444 in the line. */
21445 it->ascent -= it->voffset;
21446 else
21447 /* Increase the descent so that we can display the text lower
21448 in the line. */
21449 it->descent += it->voffset;
21450 }
21451 }
21452
21453
21454 /* Produce glyphs/get display metrics for the image IT is loaded with.
21455 See the description of struct display_iterator in dispextern.h for
21456 an overview of struct display_iterator. */
21457
21458 static void
21459 produce_image_glyph (it)
21460 struct it *it;
21461 {
21462 struct image *img;
21463 struct face *face;
21464 int glyph_ascent, crop;
21465 struct glyph_slice slice;
21466
21467 xassert (it->what == IT_IMAGE);
21468
21469 face = FACE_FROM_ID (it->f, it->face_id);
21470 xassert (face);
21471 /* Make sure X resources of the face is loaded. */
21472 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21473
21474 if (it->image_id < 0)
21475 {
21476 /* Fringe bitmap. */
21477 it->ascent = it->phys_ascent = 0;
21478 it->descent = it->phys_descent = 0;
21479 it->pixel_width = 0;
21480 it->nglyphs = 0;
21481 return;
21482 }
21483
21484 img = IMAGE_FROM_ID (it->f, it->image_id);
21485 xassert (img);
21486 /* Make sure X resources of the image is loaded. */
21487 prepare_image_for_display (it->f, img);
21488
21489 slice.x = slice.y = 0;
21490 slice.width = img->width;
21491 slice.height = img->height;
21492
21493 if (INTEGERP (it->slice.x))
21494 slice.x = XINT (it->slice.x);
21495 else if (FLOATP (it->slice.x))
21496 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21497
21498 if (INTEGERP (it->slice.y))
21499 slice.y = XINT (it->slice.y);
21500 else if (FLOATP (it->slice.y))
21501 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21502
21503 if (INTEGERP (it->slice.width))
21504 slice.width = XINT (it->slice.width);
21505 else if (FLOATP (it->slice.width))
21506 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21507
21508 if (INTEGERP (it->slice.height))
21509 slice.height = XINT (it->slice.height);
21510 else if (FLOATP (it->slice.height))
21511 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21512
21513 if (slice.x >= img->width)
21514 slice.x = img->width;
21515 if (slice.y >= img->height)
21516 slice.y = img->height;
21517 if (slice.x + slice.width >= img->width)
21518 slice.width = img->width - slice.x;
21519 if (slice.y + slice.height > img->height)
21520 slice.height = img->height - slice.y;
21521
21522 if (slice.width == 0 || slice.height == 0)
21523 return;
21524
21525 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21526
21527 it->descent = slice.height - glyph_ascent;
21528 if (slice.y == 0)
21529 it->descent += img->vmargin;
21530 if (slice.y + slice.height == img->height)
21531 it->descent += img->vmargin;
21532 it->phys_descent = it->descent;
21533
21534 it->pixel_width = slice.width;
21535 if (slice.x == 0)
21536 it->pixel_width += img->hmargin;
21537 if (slice.x + slice.width == img->width)
21538 it->pixel_width += img->hmargin;
21539
21540 /* It's quite possible for images to have an ascent greater than
21541 their height, so don't get confused in that case. */
21542 if (it->descent < 0)
21543 it->descent = 0;
21544
21545 it->nglyphs = 1;
21546
21547 if (face->box != FACE_NO_BOX)
21548 {
21549 if (face->box_line_width > 0)
21550 {
21551 if (slice.y == 0)
21552 it->ascent += face->box_line_width;
21553 if (slice.y + slice.height == img->height)
21554 it->descent += face->box_line_width;
21555 }
21556
21557 if (it->start_of_box_run_p && slice.x == 0)
21558 it->pixel_width += eabs (face->box_line_width);
21559 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21560 it->pixel_width += eabs (face->box_line_width);
21561 }
21562
21563 take_vertical_position_into_account (it);
21564
21565 /* Automatically crop wide image glyphs at right edge so we can
21566 draw the cursor on same display row. */
21567 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21568 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21569 {
21570 it->pixel_width -= crop;
21571 slice.width -= crop;
21572 }
21573
21574 if (it->glyph_row)
21575 {
21576 struct glyph *glyph;
21577 enum glyph_row_area area = it->area;
21578
21579 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21580 if (glyph < it->glyph_row->glyphs[area + 1])
21581 {
21582 glyph->charpos = CHARPOS (it->position);
21583 glyph->object = it->object;
21584 glyph->pixel_width = it->pixel_width;
21585 glyph->ascent = glyph_ascent;
21586 glyph->descent = it->descent;
21587 glyph->voffset = it->voffset;
21588 glyph->type = IMAGE_GLYPH;
21589 glyph->avoid_cursor_p = it->avoid_cursor_p;
21590 glyph->multibyte_p = it->multibyte_p;
21591 glyph->left_box_line_p = it->start_of_box_run_p;
21592 glyph->right_box_line_p = it->end_of_box_run_p;
21593 glyph->overlaps_vertically_p = 0;
21594 glyph->padding_p = 0;
21595 glyph->glyph_not_available_p = 0;
21596 glyph->face_id = it->face_id;
21597 glyph->u.img_id = img->id;
21598 glyph->slice = slice;
21599 glyph->font_type = FONT_TYPE_UNKNOWN;
21600 if (it->bidi_p)
21601 {
21602 glyph->resolved_level = it->bidi_it.resolved_level;
21603 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21604 abort ();
21605 glyph->bidi_type = it->bidi_it.type;
21606 }
21607 ++it->glyph_row->used[area];
21608 }
21609 else
21610 IT_EXPAND_MATRIX_WIDTH (it, area);
21611 }
21612 }
21613
21614
21615 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21616 of the glyph, WIDTH and HEIGHT are the width and height of the
21617 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21618
21619 static void
21620 append_stretch_glyph (it, object, width, height, ascent)
21621 struct it *it;
21622 Lisp_Object object;
21623 int width, height;
21624 int ascent;
21625 {
21626 struct glyph *glyph;
21627 enum glyph_row_area area = it->area;
21628
21629 xassert (ascent >= 0 && ascent <= height);
21630
21631 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21632 if (glyph < it->glyph_row->glyphs[area + 1])
21633 {
21634 glyph->charpos = CHARPOS (it->position);
21635 glyph->object = object;
21636 glyph->pixel_width = width;
21637 glyph->ascent = ascent;
21638 glyph->descent = height - ascent;
21639 glyph->voffset = it->voffset;
21640 glyph->type = STRETCH_GLYPH;
21641 glyph->avoid_cursor_p = it->avoid_cursor_p;
21642 glyph->multibyte_p = it->multibyte_p;
21643 glyph->left_box_line_p = it->start_of_box_run_p;
21644 glyph->right_box_line_p = it->end_of_box_run_p;
21645 glyph->overlaps_vertically_p = 0;
21646 glyph->padding_p = 0;
21647 glyph->glyph_not_available_p = 0;
21648 glyph->face_id = it->face_id;
21649 glyph->u.stretch.ascent = ascent;
21650 glyph->u.stretch.height = height;
21651 glyph->slice = null_glyph_slice;
21652 glyph->font_type = FONT_TYPE_UNKNOWN;
21653 if (it->bidi_p)
21654 {
21655 glyph->resolved_level = it->bidi_it.resolved_level;
21656 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21657 abort ();
21658 glyph->bidi_type = it->bidi_it.type;
21659 }
21660 ++it->glyph_row->used[area];
21661 }
21662 else
21663 IT_EXPAND_MATRIX_WIDTH (it, area);
21664 }
21665
21666
21667 /* Produce a stretch glyph for iterator IT. IT->object is the value
21668 of the glyph property displayed. The value must be a list
21669 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21670 being recognized:
21671
21672 1. `:width WIDTH' specifies that the space should be WIDTH *
21673 canonical char width wide. WIDTH may be an integer or floating
21674 point number.
21675
21676 2. `:relative-width FACTOR' specifies that the width of the stretch
21677 should be computed from the width of the first character having the
21678 `glyph' property, and should be FACTOR times that width.
21679
21680 3. `:align-to HPOS' specifies that the space should be wide enough
21681 to reach HPOS, a value in canonical character units.
21682
21683 Exactly one of the above pairs must be present.
21684
21685 4. `:height HEIGHT' specifies that the height of the stretch produced
21686 should be HEIGHT, measured in canonical character units.
21687
21688 5. `:relative-height FACTOR' specifies that the height of the
21689 stretch should be FACTOR times the height of the characters having
21690 the glyph property.
21691
21692 Either none or exactly one of 4 or 5 must be present.
21693
21694 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21695 of the stretch should be used for the ascent of the stretch.
21696 ASCENT must be in the range 0 <= ASCENT <= 100. */
21697
21698 static void
21699 produce_stretch_glyph (it)
21700 struct it *it;
21701 {
21702 /* (space :width WIDTH :height HEIGHT ...) */
21703 Lisp_Object prop, plist;
21704 int width = 0, height = 0, align_to = -1;
21705 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21706 int ascent = 0;
21707 double tem;
21708 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21709 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21710
21711 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21712
21713 /* List should start with `space'. */
21714 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21715 plist = XCDR (it->object);
21716
21717 /* Compute the width of the stretch. */
21718 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21719 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21720 {
21721 /* Absolute width `:width WIDTH' specified and valid. */
21722 zero_width_ok_p = 1;
21723 width = (int)tem;
21724 }
21725 else if (prop = Fplist_get (plist, QCrelative_width),
21726 NUMVAL (prop) > 0)
21727 {
21728 /* Relative width `:relative-width FACTOR' specified and valid.
21729 Compute the width of the characters having the `glyph'
21730 property. */
21731 struct it it2;
21732 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21733
21734 it2 = *it;
21735 if (it->multibyte_p)
21736 {
21737 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
21738 - IT_BYTEPOS (*it));
21739 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
21740 }
21741 else
21742 it2.c = *p, it2.len = 1;
21743
21744 it2.glyph_row = NULL;
21745 it2.what = IT_CHARACTER;
21746 x_produce_glyphs (&it2);
21747 width = NUMVAL (prop) * it2.pixel_width;
21748 }
21749 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21750 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21751 {
21752 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21753 align_to = (align_to < 0
21754 ? 0
21755 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21756 else if (align_to < 0)
21757 align_to = window_box_left_offset (it->w, TEXT_AREA);
21758 width = max (0, (int)tem + align_to - it->current_x);
21759 zero_width_ok_p = 1;
21760 }
21761 else
21762 /* Nothing specified -> width defaults to canonical char width. */
21763 width = FRAME_COLUMN_WIDTH (it->f);
21764
21765 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21766 width = 1;
21767
21768 /* Compute height. */
21769 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21770 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21771 {
21772 height = (int)tem;
21773 zero_height_ok_p = 1;
21774 }
21775 else if (prop = Fplist_get (plist, QCrelative_height),
21776 NUMVAL (prop) > 0)
21777 height = FONT_HEIGHT (font) * NUMVAL (prop);
21778 else
21779 height = FONT_HEIGHT (font);
21780
21781 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21782 height = 1;
21783
21784 /* Compute percentage of height used for ascent. If
21785 `:ascent ASCENT' is present and valid, use that. Otherwise,
21786 derive the ascent from the font in use. */
21787 if (prop = Fplist_get (plist, QCascent),
21788 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21789 ascent = height * NUMVAL (prop) / 100.0;
21790 else if (!NILP (prop)
21791 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21792 ascent = min (max (0, (int)tem), height);
21793 else
21794 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21795
21796 if (width > 0 && it->line_wrap != TRUNCATE
21797 && it->current_x + width > it->last_visible_x)
21798 width = it->last_visible_x - it->current_x - 1;
21799
21800 if (width > 0 && height > 0 && it->glyph_row)
21801 {
21802 Lisp_Object object = it->stack[it->sp - 1].string;
21803 if (!STRINGP (object))
21804 object = it->w->buffer;
21805 append_stretch_glyph (it, object, width, height, ascent);
21806 }
21807
21808 it->pixel_width = width;
21809 it->ascent = it->phys_ascent = ascent;
21810 it->descent = it->phys_descent = height - it->ascent;
21811 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21812
21813 take_vertical_position_into_account (it);
21814 }
21815
21816 /* Calculate line-height and line-spacing properties.
21817 An integer value specifies explicit pixel value.
21818 A float value specifies relative value to current face height.
21819 A cons (float . face-name) specifies relative value to
21820 height of specified face font.
21821
21822 Returns height in pixels, or nil. */
21823
21824
21825 static Lisp_Object
21826 calc_line_height_property (it, val, font, boff, override)
21827 struct it *it;
21828 Lisp_Object val;
21829 struct font *font;
21830 int boff, override;
21831 {
21832 Lisp_Object face_name = Qnil;
21833 int ascent, descent, height;
21834
21835 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21836 return val;
21837
21838 if (CONSP (val))
21839 {
21840 face_name = XCAR (val);
21841 val = XCDR (val);
21842 if (!NUMBERP (val))
21843 val = make_number (1);
21844 if (NILP (face_name))
21845 {
21846 height = it->ascent + it->descent;
21847 goto scale;
21848 }
21849 }
21850
21851 if (NILP (face_name))
21852 {
21853 font = FRAME_FONT (it->f);
21854 boff = FRAME_BASELINE_OFFSET (it->f);
21855 }
21856 else if (EQ (face_name, Qt))
21857 {
21858 override = 0;
21859 }
21860 else
21861 {
21862 int face_id;
21863 struct face *face;
21864
21865 face_id = lookup_named_face (it->f, face_name, 0);
21866 if (face_id < 0)
21867 return make_number (-1);
21868
21869 face = FACE_FROM_ID (it->f, face_id);
21870 font = face->font;
21871 if (font == NULL)
21872 return make_number (-1);
21873 boff = font->baseline_offset;
21874 if (font->vertical_centering)
21875 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21876 }
21877
21878 ascent = FONT_BASE (font) + boff;
21879 descent = FONT_DESCENT (font) - boff;
21880
21881 if (override)
21882 {
21883 it->override_ascent = ascent;
21884 it->override_descent = descent;
21885 it->override_boff = boff;
21886 }
21887
21888 height = ascent + descent;
21889
21890 scale:
21891 if (FLOATP (val))
21892 height = (int)(XFLOAT_DATA (val) * height);
21893 else if (INTEGERP (val))
21894 height *= XINT (val);
21895
21896 return make_number (height);
21897 }
21898
21899
21900 /* RIF:
21901 Produce glyphs/get display metrics for the display element IT is
21902 loaded with. See the description of struct it in dispextern.h
21903 for an overview of struct it. */
21904
21905 void
21906 x_produce_glyphs (it)
21907 struct it *it;
21908 {
21909 int extra_line_spacing = it->extra_line_spacing;
21910
21911 it->glyph_not_available_p = 0;
21912
21913 if (it->what == IT_CHARACTER)
21914 {
21915 XChar2b char2b;
21916 struct font *font;
21917 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21918 struct font_metrics *pcm;
21919 int font_not_found_p;
21920 int boff; /* baseline offset */
21921 /* We may change it->multibyte_p upon unibyte<->multibyte
21922 conversion. So, save the current value now and restore it
21923 later.
21924
21925 Note: It seems that we don't have to record multibyte_p in
21926 struct glyph because the character code itself tells whether
21927 or not the character is multibyte. Thus, in the future, we
21928 must consider eliminating the field `multibyte_p' in the
21929 struct glyph. */
21930 int saved_multibyte_p = it->multibyte_p;
21931
21932 /* Maybe translate single-byte characters to multibyte, or the
21933 other way. */
21934 it->char_to_display = it->c;
21935 if (!ASCII_BYTE_P (it->c)
21936 && ! it->multibyte_p)
21937 {
21938 if (SINGLE_BYTE_CHAR_P (it->c)
21939 && unibyte_display_via_language_environment)
21940 {
21941 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21942
21943 /* get_next_display_element assures that this decoding
21944 never fails. */
21945 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21946 it->multibyte_p = 1;
21947 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21948 -1, Qnil);
21949 face = FACE_FROM_ID (it->f, it->face_id);
21950 }
21951 }
21952
21953 /* Get font to use. Encode IT->char_to_display. */
21954 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21955 &char2b, it->multibyte_p, 0);
21956 font = face->font;
21957
21958 font_not_found_p = font == NULL;
21959 if (font_not_found_p)
21960 {
21961 /* When no suitable font found, display an empty box based
21962 on the metrics of the font of the default face (or what
21963 remapped). */
21964 struct face *no_font_face
21965 = FACE_FROM_ID (it->f,
21966 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21967 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21968 font = no_font_face->font;
21969 boff = font->baseline_offset;
21970 }
21971 else
21972 {
21973 boff = font->baseline_offset;
21974 if (font->vertical_centering)
21975 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21976 }
21977
21978 if (it->char_to_display >= ' '
21979 && (!it->multibyte_p || it->char_to_display < 128))
21980 {
21981 /* Either unibyte or ASCII. */
21982 int stretched_p;
21983
21984 it->nglyphs = 1;
21985
21986 pcm = get_per_char_metric (it->f, font, &char2b);
21987
21988 if (it->override_ascent >= 0)
21989 {
21990 it->ascent = it->override_ascent;
21991 it->descent = it->override_descent;
21992 boff = it->override_boff;
21993 }
21994 else
21995 {
21996 it->ascent = FONT_BASE (font) + boff;
21997 it->descent = FONT_DESCENT (font) - boff;
21998 }
21999
22000 if (pcm)
22001 {
22002 it->phys_ascent = pcm->ascent + boff;
22003 it->phys_descent = pcm->descent - boff;
22004 it->pixel_width = pcm->width;
22005 }
22006 else
22007 {
22008 it->glyph_not_available_p = 1;
22009 it->phys_ascent = it->ascent;
22010 it->phys_descent = it->descent;
22011 it->pixel_width = FONT_WIDTH (font);
22012 }
22013
22014 if (it->constrain_row_ascent_descent_p)
22015 {
22016 if (it->descent > it->max_descent)
22017 {
22018 it->ascent += it->descent - it->max_descent;
22019 it->descent = it->max_descent;
22020 }
22021 if (it->ascent > it->max_ascent)
22022 {
22023 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22024 it->ascent = it->max_ascent;
22025 }
22026 it->phys_ascent = min (it->phys_ascent, it->ascent);
22027 it->phys_descent = min (it->phys_descent, it->descent);
22028 extra_line_spacing = 0;
22029 }
22030
22031 /* If this is a space inside a region of text with
22032 `space-width' property, change its width. */
22033 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22034 if (stretched_p)
22035 it->pixel_width *= XFLOATINT (it->space_width);
22036
22037 /* If face has a box, add the box thickness to the character
22038 height. If character has a box line to the left and/or
22039 right, add the box line width to the character's width. */
22040 if (face->box != FACE_NO_BOX)
22041 {
22042 int thick = face->box_line_width;
22043
22044 if (thick > 0)
22045 {
22046 it->ascent += thick;
22047 it->descent += thick;
22048 }
22049 else
22050 thick = -thick;
22051
22052 if (it->start_of_box_run_p)
22053 it->pixel_width += thick;
22054 if (it->end_of_box_run_p)
22055 it->pixel_width += thick;
22056 }
22057
22058 /* If face has an overline, add the height of the overline
22059 (1 pixel) and a 1 pixel margin to the character height. */
22060 if (face->overline_p)
22061 it->ascent += overline_margin;
22062
22063 if (it->constrain_row_ascent_descent_p)
22064 {
22065 if (it->ascent > it->max_ascent)
22066 it->ascent = it->max_ascent;
22067 if (it->descent > it->max_descent)
22068 it->descent = it->max_descent;
22069 }
22070
22071 take_vertical_position_into_account (it);
22072
22073 /* If we have to actually produce glyphs, do it. */
22074 if (it->glyph_row)
22075 {
22076 if (stretched_p)
22077 {
22078 /* Translate a space with a `space-width' property
22079 into a stretch glyph. */
22080 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22081 / FONT_HEIGHT (font));
22082 append_stretch_glyph (it, it->object, it->pixel_width,
22083 it->ascent + it->descent, ascent);
22084 }
22085 else
22086 append_glyph (it);
22087
22088 /* If characters with lbearing or rbearing are displayed
22089 in this line, record that fact in a flag of the
22090 glyph row. This is used to optimize X output code. */
22091 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22092 it->glyph_row->contains_overlapping_glyphs_p = 1;
22093 }
22094 if (! stretched_p && it->pixel_width == 0)
22095 /* We assure that all visible glyphs have at least 1-pixel
22096 width. */
22097 it->pixel_width = 1;
22098 }
22099 else if (it->char_to_display == '\n')
22100 {
22101 /* A newline has no width, but we need the height of the
22102 line. But if previous part of the line sets a height,
22103 don't increase that height */
22104
22105 Lisp_Object height;
22106 Lisp_Object total_height = Qnil;
22107
22108 it->override_ascent = -1;
22109 it->pixel_width = 0;
22110 it->nglyphs = 0;
22111
22112 height = get_it_property(it, Qline_height);
22113 /* Split (line-height total-height) list */
22114 if (CONSP (height)
22115 && CONSP (XCDR (height))
22116 && NILP (XCDR (XCDR (height))))
22117 {
22118 total_height = XCAR (XCDR (height));
22119 height = XCAR (height);
22120 }
22121 height = calc_line_height_property(it, height, font, boff, 1);
22122
22123 if (it->override_ascent >= 0)
22124 {
22125 it->ascent = it->override_ascent;
22126 it->descent = it->override_descent;
22127 boff = it->override_boff;
22128 }
22129 else
22130 {
22131 it->ascent = FONT_BASE (font) + boff;
22132 it->descent = FONT_DESCENT (font) - boff;
22133 }
22134
22135 if (EQ (height, Qt))
22136 {
22137 if (it->descent > it->max_descent)
22138 {
22139 it->ascent += it->descent - it->max_descent;
22140 it->descent = it->max_descent;
22141 }
22142 if (it->ascent > it->max_ascent)
22143 {
22144 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22145 it->ascent = it->max_ascent;
22146 }
22147 it->phys_ascent = min (it->phys_ascent, it->ascent);
22148 it->phys_descent = min (it->phys_descent, it->descent);
22149 it->constrain_row_ascent_descent_p = 1;
22150 extra_line_spacing = 0;
22151 }
22152 else
22153 {
22154 Lisp_Object spacing;
22155
22156 it->phys_ascent = it->ascent;
22157 it->phys_descent = it->descent;
22158
22159 if ((it->max_ascent > 0 || it->max_descent > 0)
22160 && face->box != FACE_NO_BOX
22161 && face->box_line_width > 0)
22162 {
22163 it->ascent += face->box_line_width;
22164 it->descent += face->box_line_width;
22165 }
22166 if (!NILP (height)
22167 && XINT (height) > it->ascent + it->descent)
22168 it->ascent = XINT (height) - it->descent;
22169
22170 if (!NILP (total_height))
22171 spacing = calc_line_height_property(it, total_height, font, boff, 0);
22172 else
22173 {
22174 spacing = get_it_property(it, Qline_spacing);
22175 spacing = calc_line_height_property(it, spacing, font, boff, 0);
22176 }
22177 if (INTEGERP (spacing))
22178 {
22179 extra_line_spacing = XINT (spacing);
22180 if (!NILP (total_height))
22181 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22182 }
22183 }
22184 }
22185 else if (it->char_to_display == '\t')
22186 {
22187 if (font->space_width > 0)
22188 {
22189 int tab_width = it->tab_width * font->space_width;
22190 int x = it->current_x + it->continuation_lines_width;
22191 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22192
22193 /* If the distance from the current position to the next tab
22194 stop is less than a space character width, use the
22195 tab stop after that. */
22196 if (next_tab_x - x < font->space_width)
22197 next_tab_x += tab_width;
22198
22199 it->pixel_width = next_tab_x - x;
22200 it->nglyphs = 1;
22201 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22202 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22203
22204 if (it->glyph_row)
22205 {
22206 append_stretch_glyph (it, it->object, it->pixel_width,
22207 it->ascent + it->descent, it->ascent);
22208 }
22209 }
22210 else
22211 {
22212 it->pixel_width = 0;
22213 it->nglyphs = 1;
22214 }
22215 }
22216 else
22217 {
22218 /* A multi-byte character. Assume that the display width of the
22219 character is the width of the character multiplied by the
22220 width of the font. */
22221
22222 /* If we found a font, this font should give us the right
22223 metrics. If we didn't find a font, use the frame's
22224 default font and calculate the width of the character by
22225 multiplying the width of font by the width of the
22226 character. */
22227
22228 pcm = get_per_char_metric (it->f, font, &char2b);
22229
22230 if (font_not_found_p || !pcm)
22231 {
22232 int char_width = CHAR_WIDTH (it->char_to_display);
22233
22234 if (char_width == 0)
22235 /* This is a non spacing character. But, as we are
22236 going to display an empty box, the box must occupy
22237 at least one column. */
22238 char_width = 1;
22239 it->glyph_not_available_p = 1;
22240 it->pixel_width = font->space_width * char_width;
22241 it->phys_ascent = FONT_BASE (font) + boff;
22242 it->phys_descent = FONT_DESCENT (font) - boff;
22243 }
22244 else
22245 {
22246 it->pixel_width = pcm->width;
22247 it->phys_ascent = pcm->ascent + boff;
22248 it->phys_descent = pcm->descent - boff;
22249 if (it->glyph_row
22250 && (pcm->lbearing < 0
22251 || pcm->rbearing > pcm->width))
22252 it->glyph_row->contains_overlapping_glyphs_p = 1;
22253 }
22254 it->nglyphs = 1;
22255 it->ascent = FONT_BASE (font) + boff;
22256 it->descent = FONT_DESCENT (font) - boff;
22257 if (face->box != FACE_NO_BOX)
22258 {
22259 int thick = face->box_line_width;
22260
22261 if (thick > 0)
22262 {
22263 it->ascent += thick;
22264 it->descent += thick;
22265 }
22266 else
22267 thick = - thick;
22268
22269 if (it->start_of_box_run_p)
22270 it->pixel_width += thick;
22271 if (it->end_of_box_run_p)
22272 it->pixel_width += thick;
22273 }
22274
22275 /* If face has an overline, add the height of the overline
22276 (1 pixel) and a 1 pixel margin to the character height. */
22277 if (face->overline_p)
22278 it->ascent += overline_margin;
22279
22280 take_vertical_position_into_account (it);
22281
22282 if (it->ascent < 0)
22283 it->ascent = 0;
22284 if (it->descent < 0)
22285 it->descent = 0;
22286
22287 if (it->glyph_row)
22288 append_glyph (it);
22289 if (it->pixel_width == 0)
22290 /* We assure that all visible glyphs have at least 1-pixel
22291 width. */
22292 it->pixel_width = 1;
22293 }
22294 it->multibyte_p = saved_multibyte_p;
22295 }
22296 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22297 {
22298 /* A static composition.
22299
22300 Note: A composition is represented as one glyph in the
22301 glyph matrix. There are no padding glyphs.
22302
22303 Important note: pixel_width, ascent, and descent are the
22304 values of what is drawn by draw_glyphs (i.e. the values of
22305 the overall glyphs composed). */
22306 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22307 int boff; /* baseline offset */
22308 struct composition *cmp = composition_table[it->cmp_it.id];
22309 int glyph_len = cmp->glyph_len;
22310 struct font *font = face->font;
22311
22312 it->nglyphs = 1;
22313
22314 /* If we have not yet calculated pixel size data of glyphs of
22315 the composition for the current face font, calculate them
22316 now. Theoretically, we have to check all fonts for the
22317 glyphs, but that requires much time and memory space. So,
22318 here we check only the font of the first glyph. This may
22319 lead to incorrect display, but it's very rare, and C-l
22320 (recenter-top-bottom) can correct the display anyway. */
22321 if (! cmp->font || cmp->font != font)
22322 {
22323 /* Ascent and descent of the font of the first character
22324 of this composition (adjusted by baseline offset).
22325 Ascent and descent of overall glyphs should not be less
22326 than these, respectively. */
22327 int font_ascent, font_descent, font_height;
22328 /* Bounding box of the overall glyphs. */
22329 int leftmost, rightmost, lowest, highest;
22330 int lbearing, rbearing;
22331 int i, width, ascent, descent;
22332 int left_padded = 0, right_padded = 0;
22333 int c;
22334 XChar2b char2b;
22335 struct font_metrics *pcm;
22336 int font_not_found_p;
22337 int pos;
22338
22339 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22340 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22341 break;
22342 if (glyph_len < cmp->glyph_len)
22343 right_padded = 1;
22344 for (i = 0; i < glyph_len; i++)
22345 {
22346 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22347 break;
22348 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22349 }
22350 if (i > 0)
22351 left_padded = 1;
22352
22353 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22354 : IT_CHARPOS (*it));
22355 /* If no suitable font is found, use the default font. */
22356 font_not_found_p = font == NULL;
22357 if (font_not_found_p)
22358 {
22359 face = face->ascii_face;
22360 font = face->font;
22361 }
22362 boff = font->baseline_offset;
22363 if (font->vertical_centering)
22364 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22365 font_ascent = FONT_BASE (font) + boff;
22366 font_descent = FONT_DESCENT (font) - boff;
22367 font_height = FONT_HEIGHT (font);
22368
22369 cmp->font = (void *) font;
22370
22371 pcm = NULL;
22372 if (! font_not_found_p)
22373 {
22374 get_char_face_and_encoding (it->f, c, it->face_id,
22375 &char2b, it->multibyte_p, 0);
22376 pcm = get_per_char_metric (it->f, font, &char2b);
22377 }
22378
22379 /* Initialize the bounding box. */
22380 if (pcm)
22381 {
22382 width = pcm->width;
22383 ascent = pcm->ascent;
22384 descent = pcm->descent;
22385 lbearing = pcm->lbearing;
22386 rbearing = pcm->rbearing;
22387 }
22388 else
22389 {
22390 width = FONT_WIDTH (font);
22391 ascent = FONT_BASE (font);
22392 descent = FONT_DESCENT (font);
22393 lbearing = 0;
22394 rbearing = width;
22395 }
22396
22397 rightmost = width;
22398 leftmost = 0;
22399 lowest = - descent + boff;
22400 highest = ascent + boff;
22401
22402 if (! font_not_found_p
22403 && font->default_ascent
22404 && CHAR_TABLE_P (Vuse_default_ascent)
22405 && !NILP (Faref (Vuse_default_ascent,
22406 make_number (it->char_to_display))))
22407 highest = font->default_ascent + boff;
22408
22409 /* Draw the first glyph at the normal position. It may be
22410 shifted to right later if some other glyphs are drawn
22411 at the left. */
22412 cmp->offsets[i * 2] = 0;
22413 cmp->offsets[i * 2 + 1] = boff;
22414 cmp->lbearing = lbearing;
22415 cmp->rbearing = rbearing;
22416
22417 /* Set cmp->offsets for the remaining glyphs. */
22418 for (i++; i < glyph_len; i++)
22419 {
22420 int left, right, btm, top;
22421 int ch = COMPOSITION_GLYPH (cmp, i);
22422 int face_id;
22423 struct face *this_face;
22424 int this_boff;
22425
22426 if (ch == '\t')
22427 ch = ' ';
22428 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22429 this_face = FACE_FROM_ID (it->f, face_id);
22430 font = this_face->font;
22431
22432 if (font == NULL)
22433 pcm = NULL;
22434 else
22435 {
22436 this_boff = font->baseline_offset;
22437 if (font->vertical_centering)
22438 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22439 get_char_face_and_encoding (it->f, ch, face_id,
22440 &char2b, it->multibyte_p, 0);
22441 pcm = get_per_char_metric (it->f, font, &char2b);
22442 }
22443 if (! pcm)
22444 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22445 else
22446 {
22447 width = pcm->width;
22448 ascent = pcm->ascent;
22449 descent = pcm->descent;
22450 lbearing = pcm->lbearing;
22451 rbearing = pcm->rbearing;
22452 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22453 {
22454 /* Relative composition with or without
22455 alternate chars. */
22456 left = (leftmost + rightmost - width) / 2;
22457 btm = - descent + boff;
22458 if (font->relative_compose
22459 && (! CHAR_TABLE_P (Vignore_relative_composition)
22460 || NILP (Faref (Vignore_relative_composition,
22461 make_number (ch)))))
22462 {
22463
22464 if (- descent >= font->relative_compose)
22465 /* One extra pixel between two glyphs. */
22466 btm = highest + 1;
22467 else if (ascent <= 0)
22468 /* One extra pixel between two glyphs. */
22469 btm = lowest - 1 - ascent - descent;
22470 }
22471 }
22472 else
22473 {
22474 /* A composition rule is specified by an integer
22475 value that encodes global and new reference
22476 points (GREF and NREF). GREF and NREF are
22477 specified by numbers as below:
22478
22479 0---1---2 -- ascent
22480 | |
22481 | |
22482 | |
22483 9--10--11 -- center
22484 | |
22485 ---3---4---5--- baseline
22486 | |
22487 6---7---8 -- descent
22488 */
22489 int rule = COMPOSITION_RULE (cmp, i);
22490 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22491
22492 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22493 grefx = gref % 3, nrefx = nref % 3;
22494 grefy = gref / 3, nrefy = nref / 3;
22495 if (xoff)
22496 xoff = font_height * (xoff - 128) / 256;
22497 if (yoff)
22498 yoff = font_height * (yoff - 128) / 256;
22499
22500 left = (leftmost
22501 + grefx * (rightmost - leftmost) / 2
22502 - nrefx * width / 2
22503 + xoff);
22504
22505 btm = ((grefy == 0 ? highest
22506 : grefy == 1 ? 0
22507 : grefy == 2 ? lowest
22508 : (highest + lowest) / 2)
22509 - (nrefy == 0 ? ascent + descent
22510 : nrefy == 1 ? descent - boff
22511 : nrefy == 2 ? 0
22512 : (ascent + descent) / 2)
22513 + yoff);
22514 }
22515
22516 cmp->offsets[i * 2] = left;
22517 cmp->offsets[i * 2 + 1] = btm + descent;
22518
22519 /* Update the bounding box of the overall glyphs. */
22520 if (width > 0)
22521 {
22522 right = left + width;
22523 if (left < leftmost)
22524 leftmost = left;
22525 if (right > rightmost)
22526 rightmost = right;
22527 }
22528 top = btm + descent + ascent;
22529 if (top > highest)
22530 highest = top;
22531 if (btm < lowest)
22532 lowest = btm;
22533
22534 if (cmp->lbearing > left + lbearing)
22535 cmp->lbearing = left + lbearing;
22536 if (cmp->rbearing < left + rbearing)
22537 cmp->rbearing = left + rbearing;
22538 }
22539 }
22540
22541 /* If there are glyphs whose x-offsets are negative,
22542 shift all glyphs to the right and make all x-offsets
22543 non-negative. */
22544 if (leftmost < 0)
22545 {
22546 for (i = 0; i < cmp->glyph_len; i++)
22547 cmp->offsets[i * 2] -= leftmost;
22548 rightmost -= leftmost;
22549 cmp->lbearing -= leftmost;
22550 cmp->rbearing -= leftmost;
22551 }
22552
22553 if (left_padded && cmp->lbearing < 0)
22554 {
22555 for (i = 0; i < cmp->glyph_len; i++)
22556 cmp->offsets[i * 2] -= cmp->lbearing;
22557 rightmost -= cmp->lbearing;
22558 cmp->rbearing -= cmp->lbearing;
22559 cmp->lbearing = 0;
22560 }
22561 if (right_padded && rightmost < cmp->rbearing)
22562 {
22563 rightmost = cmp->rbearing;
22564 }
22565
22566 cmp->pixel_width = rightmost;
22567 cmp->ascent = highest;
22568 cmp->descent = - lowest;
22569 if (cmp->ascent < font_ascent)
22570 cmp->ascent = font_ascent;
22571 if (cmp->descent < font_descent)
22572 cmp->descent = font_descent;
22573 }
22574
22575 if (it->glyph_row
22576 && (cmp->lbearing < 0
22577 || cmp->rbearing > cmp->pixel_width))
22578 it->glyph_row->contains_overlapping_glyphs_p = 1;
22579
22580 it->pixel_width = cmp->pixel_width;
22581 it->ascent = it->phys_ascent = cmp->ascent;
22582 it->descent = it->phys_descent = cmp->descent;
22583 if (face->box != FACE_NO_BOX)
22584 {
22585 int thick = face->box_line_width;
22586
22587 if (thick > 0)
22588 {
22589 it->ascent += thick;
22590 it->descent += thick;
22591 }
22592 else
22593 thick = - thick;
22594
22595 if (it->start_of_box_run_p)
22596 it->pixel_width += thick;
22597 if (it->end_of_box_run_p)
22598 it->pixel_width += thick;
22599 }
22600
22601 /* If face has an overline, add the height of the overline
22602 (1 pixel) and a 1 pixel margin to the character height. */
22603 if (face->overline_p)
22604 it->ascent += overline_margin;
22605
22606 take_vertical_position_into_account (it);
22607 if (it->ascent < 0)
22608 it->ascent = 0;
22609 if (it->descent < 0)
22610 it->descent = 0;
22611
22612 if (it->glyph_row)
22613 append_composite_glyph (it);
22614 }
22615 else if (it->what == IT_COMPOSITION)
22616 {
22617 /* A dynamic (automatic) composition. */
22618 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22619 Lisp_Object gstring;
22620 struct font_metrics metrics;
22621
22622 gstring = composition_gstring_from_id (it->cmp_it.id);
22623 it->pixel_width
22624 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22625 &metrics);
22626 if (it->glyph_row
22627 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22628 it->glyph_row->contains_overlapping_glyphs_p = 1;
22629 it->ascent = it->phys_ascent = metrics.ascent;
22630 it->descent = it->phys_descent = metrics.descent;
22631 if (face->box != FACE_NO_BOX)
22632 {
22633 int thick = face->box_line_width;
22634
22635 if (thick > 0)
22636 {
22637 it->ascent += thick;
22638 it->descent += thick;
22639 }
22640 else
22641 thick = - thick;
22642
22643 if (it->start_of_box_run_p)
22644 it->pixel_width += thick;
22645 if (it->end_of_box_run_p)
22646 it->pixel_width += thick;
22647 }
22648 /* If face has an overline, add the height of the overline
22649 (1 pixel) and a 1 pixel margin to the character height. */
22650 if (face->overline_p)
22651 it->ascent += overline_margin;
22652 take_vertical_position_into_account (it);
22653 if (it->ascent < 0)
22654 it->ascent = 0;
22655 if (it->descent < 0)
22656 it->descent = 0;
22657
22658 if (it->glyph_row)
22659 append_composite_glyph (it);
22660 }
22661 else if (it->what == IT_IMAGE)
22662 produce_image_glyph (it);
22663 else if (it->what == IT_STRETCH)
22664 produce_stretch_glyph (it);
22665
22666 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22667 because this isn't true for images with `:ascent 100'. */
22668 xassert (it->ascent >= 0 && it->descent >= 0);
22669 if (it->area == TEXT_AREA)
22670 it->current_x += it->pixel_width;
22671
22672 if (extra_line_spacing > 0)
22673 {
22674 it->descent += extra_line_spacing;
22675 if (extra_line_spacing > it->max_extra_line_spacing)
22676 it->max_extra_line_spacing = extra_line_spacing;
22677 }
22678
22679 it->max_ascent = max (it->max_ascent, it->ascent);
22680 it->max_descent = max (it->max_descent, it->descent);
22681 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22682 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22683 }
22684
22685 /* EXPORT for RIF:
22686 Output LEN glyphs starting at START at the nominal cursor position.
22687 Advance the nominal cursor over the text. The global variable
22688 updated_window contains the window being updated, updated_row is
22689 the glyph row being updated, and updated_area is the area of that
22690 row being updated. */
22691
22692 void
22693 x_write_glyphs (start, len)
22694 struct glyph *start;
22695 int len;
22696 {
22697 int x, hpos;
22698
22699 xassert (updated_window && updated_row);
22700 BLOCK_INPUT;
22701
22702 /* Write glyphs. */
22703
22704 hpos = start - updated_row->glyphs[updated_area];
22705 x = draw_glyphs (updated_window, output_cursor.x,
22706 updated_row, updated_area,
22707 hpos, hpos + len,
22708 DRAW_NORMAL_TEXT, 0);
22709
22710 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22711 if (updated_area == TEXT_AREA
22712 && updated_window->phys_cursor_on_p
22713 && updated_window->phys_cursor.vpos == output_cursor.vpos
22714 && updated_window->phys_cursor.hpos >= hpos
22715 && updated_window->phys_cursor.hpos < hpos + len)
22716 updated_window->phys_cursor_on_p = 0;
22717
22718 UNBLOCK_INPUT;
22719
22720 /* Advance the output cursor. */
22721 output_cursor.hpos += len;
22722 output_cursor.x = x;
22723 }
22724
22725
22726 /* EXPORT for RIF:
22727 Insert LEN glyphs from START at the nominal cursor position. */
22728
22729 void
22730 x_insert_glyphs (start, len)
22731 struct glyph *start;
22732 int len;
22733 {
22734 struct frame *f;
22735 struct window *w;
22736 int line_height, shift_by_width, shifted_region_width;
22737 struct glyph_row *row;
22738 struct glyph *glyph;
22739 int frame_x, frame_y;
22740 EMACS_INT hpos;
22741
22742 xassert (updated_window && updated_row);
22743 BLOCK_INPUT;
22744 w = updated_window;
22745 f = XFRAME (WINDOW_FRAME (w));
22746
22747 /* Get the height of the line we are in. */
22748 row = updated_row;
22749 line_height = row->height;
22750
22751 /* Get the width of the glyphs to insert. */
22752 shift_by_width = 0;
22753 for (glyph = start; glyph < start + len; ++glyph)
22754 shift_by_width += glyph->pixel_width;
22755
22756 /* Get the width of the region to shift right. */
22757 shifted_region_width = (window_box_width (w, updated_area)
22758 - output_cursor.x
22759 - shift_by_width);
22760
22761 /* Shift right. */
22762 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22763 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22764
22765 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22766 line_height, shift_by_width);
22767
22768 /* Write the glyphs. */
22769 hpos = start - row->glyphs[updated_area];
22770 draw_glyphs (w, output_cursor.x, row, updated_area,
22771 hpos, hpos + len,
22772 DRAW_NORMAL_TEXT, 0);
22773
22774 /* Advance the output cursor. */
22775 output_cursor.hpos += len;
22776 output_cursor.x += shift_by_width;
22777 UNBLOCK_INPUT;
22778 }
22779
22780
22781 /* EXPORT for RIF:
22782 Erase the current text line from the nominal cursor position
22783 (inclusive) to pixel column TO_X (exclusive). The idea is that
22784 everything from TO_X onward is already erased.
22785
22786 TO_X is a pixel position relative to updated_area of
22787 updated_window. TO_X == -1 means clear to the end of this area. */
22788
22789 void
22790 x_clear_end_of_line (to_x)
22791 int to_x;
22792 {
22793 struct frame *f;
22794 struct window *w = updated_window;
22795 int max_x, min_y, max_y;
22796 int from_x, from_y, to_y;
22797
22798 xassert (updated_window && updated_row);
22799 f = XFRAME (w->frame);
22800
22801 if (updated_row->full_width_p)
22802 max_x = WINDOW_TOTAL_WIDTH (w);
22803 else
22804 max_x = window_box_width (w, updated_area);
22805 max_y = window_text_bottom_y (w);
22806
22807 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22808 of window. For TO_X > 0, truncate to end of drawing area. */
22809 if (to_x == 0)
22810 return;
22811 else if (to_x < 0)
22812 to_x = max_x;
22813 else
22814 to_x = min (to_x, max_x);
22815
22816 to_y = min (max_y, output_cursor.y + updated_row->height);
22817
22818 /* Notice if the cursor will be cleared by this operation. */
22819 if (!updated_row->full_width_p)
22820 notice_overwritten_cursor (w, updated_area,
22821 output_cursor.x, -1,
22822 updated_row->y,
22823 MATRIX_ROW_BOTTOM_Y (updated_row));
22824
22825 from_x = output_cursor.x;
22826
22827 /* Translate to frame coordinates. */
22828 if (updated_row->full_width_p)
22829 {
22830 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22831 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22832 }
22833 else
22834 {
22835 int area_left = window_box_left (w, updated_area);
22836 from_x += area_left;
22837 to_x += area_left;
22838 }
22839
22840 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22841 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22842 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22843
22844 /* Prevent inadvertently clearing to end of the X window. */
22845 if (to_x > from_x && to_y > from_y)
22846 {
22847 BLOCK_INPUT;
22848 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22849 to_x - from_x, to_y - from_y);
22850 UNBLOCK_INPUT;
22851 }
22852 }
22853
22854 #endif /* HAVE_WINDOW_SYSTEM */
22855
22856
22857 \f
22858 /***********************************************************************
22859 Cursor types
22860 ***********************************************************************/
22861
22862 /* Value is the internal representation of the specified cursor type
22863 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22864 of the bar cursor. */
22865
22866 static enum text_cursor_kinds
22867 get_specified_cursor_type (arg, width)
22868 Lisp_Object arg;
22869 int *width;
22870 {
22871 enum text_cursor_kinds type;
22872
22873 if (NILP (arg))
22874 return NO_CURSOR;
22875
22876 if (EQ (arg, Qbox))
22877 return FILLED_BOX_CURSOR;
22878
22879 if (EQ (arg, Qhollow))
22880 return HOLLOW_BOX_CURSOR;
22881
22882 if (EQ (arg, Qbar))
22883 {
22884 *width = 2;
22885 return BAR_CURSOR;
22886 }
22887
22888 if (CONSP (arg)
22889 && EQ (XCAR (arg), Qbar)
22890 && INTEGERP (XCDR (arg))
22891 && XINT (XCDR (arg)) >= 0)
22892 {
22893 *width = XINT (XCDR (arg));
22894 return BAR_CURSOR;
22895 }
22896
22897 if (EQ (arg, Qhbar))
22898 {
22899 *width = 2;
22900 return HBAR_CURSOR;
22901 }
22902
22903 if (CONSP (arg)
22904 && EQ (XCAR (arg), Qhbar)
22905 && INTEGERP (XCDR (arg))
22906 && XINT (XCDR (arg)) >= 0)
22907 {
22908 *width = XINT (XCDR (arg));
22909 return HBAR_CURSOR;
22910 }
22911
22912 /* Treat anything unknown as "hollow box cursor".
22913 It was bad to signal an error; people have trouble fixing
22914 .Xdefaults with Emacs, when it has something bad in it. */
22915 type = HOLLOW_BOX_CURSOR;
22916
22917 return type;
22918 }
22919
22920 /* Set the default cursor types for specified frame. */
22921 void
22922 set_frame_cursor_types (f, arg)
22923 struct frame *f;
22924 Lisp_Object arg;
22925 {
22926 int width;
22927 Lisp_Object tem;
22928
22929 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22930 FRAME_CURSOR_WIDTH (f) = width;
22931
22932 /* By default, set up the blink-off state depending on the on-state. */
22933
22934 tem = Fassoc (arg, Vblink_cursor_alist);
22935 if (!NILP (tem))
22936 {
22937 FRAME_BLINK_OFF_CURSOR (f)
22938 = get_specified_cursor_type (XCDR (tem), &width);
22939 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22940 }
22941 else
22942 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22943 }
22944
22945
22946 /* Return the cursor we want to be displayed in window W. Return
22947 width of bar/hbar cursor through WIDTH arg. Return with
22948 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22949 (i.e. if the `system caret' should track this cursor).
22950
22951 In a mini-buffer window, we want the cursor only to appear if we
22952 are reading input from this window. For the selected window, we
22953 want the cursor type given by the frame parameter or buffer local
22954 setting of cursor-type. If explicitly marked off, draw no cursor.
22955 In all other cases, we want a hollow box cursor. */
22956
22957 static enum text_cursor_kinds
22958 get_window_cursor_type (w, glyph, width, active_cursor)
22959 struct window *w;
22960 struct glyph *glyph;
22961 int *width;
22962 int *active_cursor;
22963 {
22964 struct frame *f = XFRAME (w->frame);
22965 struct buffer *b = XBUFFER (w->buffer);
22966 int cursor_type = DEFAULT_CURSOR;
22967 Lisp_Object alt_cursor;
22968 int non_selected = 0;
22969
22970 *active_cursor = 1;
22971
22972 /* Echo area */
22973 if (cursor_in_echo_area
22974 && FRAME_HAS_MINIBUF_P (f)
22975 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22976 {
22977 if (w == XWINDOW (echo_area_window))
22978 {
22979 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22980 {
22981 *width = FRAME_CURSOR_WIDTH (f);
22982 return FRAME_DESIRED_CURSOR (f);
22983 }
22984 else
22985 return get_specified_cursor_type (b->cursor_type, width);
22986 }
22987
22988 *active_cursor = 0;
22989 non_selected = 1;
22990 }
22991
22992 /* Detect a nonselected window or nonselected frame. */
22993 else if (w != XWINDOW (f->selected_window)
22994 #ifdef HAVE_WINDOW_SYSTEM
22995 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22996 #endif
22997 )
22998 {
22999 *active_cursor = 0;
23000
23001 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23002 return NO_CURSOR;
23003
23004 non_selected = 1;
23005 }
23006
23007 /* Never display a cursor in a window in which cursor-type is nil. */
23008 if (NILP (b->cursor_type))
23009 return NO_CURSOR;
23010
23011 /* Get the normal cursor type for this window. */
23012 if (EQ (b->cursor_type, Qt))
23013 {
23014 cursor_type = FRAME_DESIRED_CURSOR (f);
23015 *width = FRAME_CURSOR_WIDTH (f);
23016 }
23017 else
23018 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23019
23020 /* Use cursor-in-non-selected-windows instead
23021 for non-selected window or frame. */
23022 if (non_selected)
23023 {
23024 alt_cursor = b->cursor_in_non_selected_windows;
23025 if (!EQ (Qt, alt_cursor))
23026 return get_specified_cursor_type (alt_cursor, width);
23027 /* t means modify the normal cursor type. */
23028 if (cursor_type == FILLED_BOX_CURSOR)
23029 cursor_type = HOLLOW_BOX_CURSOR;
23030 else if (cursor_type == BAR_CURSOR && *width > 1)
23031 --*width;
23032 return cursor_type;
23033 }
23034
23035 /* Use normal cursor if not blinked off. */
23036 if (!w->cursor_off_p)
23037 {
23038 #ifdef HAVE_WINDOW_SYSTEM
23039 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23040 {
23041 if (cursor_type == FILLED_BOX_CURSOR)
23042 {
23043 /* Using a block cursor on large images can be very annoying.
23044 So use a hollow cursor for "large" images.
23045 If image is not transparent (no mask), also use hollow cursor. */
23046 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23047 if (img != NULL && IMAGEP (img->spec))
23048 {
23049 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23050 where N = size of default frame font size.
23051 This should cover most of the "tiny" icons people may use. */
23052 if (!img->mask
23053 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23054 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23055 cursor_type = HOLLOW_BOX_CURSOR;
23056 }
23057 }
23058 else if (cursor_type != NO_CURSOR)
23059 {
23060 /* Display current only supports BOX and HOLLOW cursors for images.
23061 So for now, unconditionally use a HOLLOW cursor when cursor is
23062 not a solid box cursor. */
23063 cursor_type = HOLLOW_BOX_CURSOR;
23064 }
23065 }
23066 #endif
23067 return cursor_type;
23068 }
23069
23070 /* Cursor is blinked off, so determine how to "toggle" it. */
23071
23072 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23073 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23074 return get_specified_cursor_type (XCDR (alt_cursor), width);
23075
23076 /* Then see if frame has specified a specific blink off cursor type. */
23077 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23078 {
23079 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23080 return FRAME_BLINK_OFF_CURSOR (f);
23081 }
23082
23083 #if 0
23084 /* Some people liked having a permanently visible blinking cursor,
23085 while others had very strong opinions against it. So it was
23086 decided to remove it. KFS 2003-09-03 */
23087
23088 /* Finally perform built-in cursor blinking:
23089 filled box <-> hollow box
23090 wide [h]bar <-> narrow [h]bar
23091 narrow [h]bar <-> no cursor
23092 other type <-> no cursor */
23093
23094 if (cursor_type == FILLED_BOX_CURSOR)
23095 return HOLLOW_BOX_CURSOR;
23096
23097 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23098 {
23099 *width = 1;
23100 return cursor_type;
23101 }
23102 #endif
23103
23104 return NO_CURSOR;
23105 }
23106
23107
23108 #ifdef HAVE_WINDOW_SYSTEM
23109
23110 /* Notice when the text cursor of window W has been completely
23111 overwritten by a drawing operation that outputs glyphs in AREA
23112 starting at X0 and ending at X1 in the line starting at Y0 and
23113 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23114 the rest of the line after X0 has been written. Y coordinates
23115 are window-relative. */
23116
23117 static void
23118 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
23119 struct window *w;
23120 enum glyph_row_area area;
23121 int x0, y0, x1, y1;
23122 {
23123 int cx0, cx1, cy0, cy1;
23124 struct glyph_row *row;
23125
23126 if (!w->phys_cursor_on_p)
23127 return;
23128 if (area != TEXT_AREA)
23129 return;
23130
23131 if (w->phys_cursor.vpos < 0
23132 || w->phys_cursor.vpos >= w->current_matrix->nrows
23133 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23134 !(row->enabled_p && row->displays_text_p)))
23135 return;
23136
23137 if (row->cursor_in_fringe_p)
23138 {
23139 row->cursor_in_fringe_p = 0;
23140 draw_fringe_bitmap (w, row, 0);
23141 w->phys_cursor_on_p = 0;
23142 return;
23143 }
23144
23145 cx0 = w->phys_cursor.x;
23146 cx1 = cx0 + w->phys_cursor_width;
23147 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23148 return;
23149
23150 /* The cursor image will be completely removed from the
23151 screen if the output area intersects the cursor area in
23152 y-direction. When we draw in [y0 y1[, and some part of
23153 the cursor is at y < y0, that part must have been drawn
23154 before. When scrolling, the cursor is erased before
23155 actually scrolling, so we don't come here. When not
23156 scrolling, the rows above the old cursor row must have
23157 changed, and in this case these rows must have written
23158 over the cursor image.
23159
23160 Likewise if part of the cursor is below y1, with the
23161 exception of the cursor being in the first blank row at
23162 the buffer and window end because update_text_area
23163 doesn't draw that row. (Except when it does, but
23164 that's handled in update_text_area.) */
23165
23166 cy0 = w->phys_cursor.y;
23167 cy1 = cy0 + w->phys_cursor_height;
23168 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23169 return;
23170
23171 w->phys_cursor_on_p = 0;
23172 }
23173
23174 #endif /* HAVE_WINDOW_SYSTEM */
23175
23176 \f
23177 /************************************************************************
23178 Mouse Face
23179 ************************************************************************/
23180
23181 #ifdef HAVE_WINDOW_SYSTEM
23182
23183 /* EXPORT for RIF:
23184 Fix the display of area AREA of overlapping row ROW in window W
23185 with respect to the overlapping part OVERLAPS. */
23186
23187 void
23188 x_fix_overlapping_area (w, row, area, overlaps)
23189 struct window *w;
23190 struct glyph_row *row;
23191 enum glyph_row_area area;
23192 int overlaps;
23193 {
23194 int i, x;
23195
23196 BLOCK_INPUT;
23197
23198 x = 0;
23199 for (i = 0; i < row->used[area];)
23200 {
23201 if (row->glyphs[area][i].overlaps_vertically_p)
23202 {
23203 int start = i, start_x = x;
23204
23205 do
23206 {
23207 x += row->glyphs[area][i].pixel_width;
23208 ++i;
23209 }
23210 while (i < row->used[area]
23211 && row->glyphs[area][i].overlaps_vertically_p);
23212
23213 draw_glyphs (w, start_x, row, area,
23214 start, i,
23215 DRAW_NORMAL_TEXT, overlaps);
23216 }
23217 else
23218 {
23219 x += row->glyphs[area][i].pixel_width;
23220 ++i;
23221 }
23222 }
23223
23224 UNBLOCK_INPUT;
23225 }
23226
23227
23228 /* EXPORT:
23229 Draw the cursor glyph of window W in glyph row ROW. See the
23230 comment of draw_glyphs for the meaning of HL. */
23231
23232 void
23233 draw_phys_cursor_glyph (w, row, hl)
23234 struct window *w;
23235 struct glyph_row *row;
23236 enum draw_glyphs_face hl;
23237 {
23238 /* If cursor hpos is out of bounds, don't draw garbage. This can
23239 happen in mini-buffer windows when switching between echo area
23240 glyphs and mini-buffer. */
23241 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
23242 {
23243 int on_p = w->phys_cursor_on_p;
23244 int x1;
23245 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23246 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23247 hl, 0);
23248 w->phys_cursor_on_p = on_p;
23249
23250 if (hl == DRAW_CURSOR)
23251 w->phys_cursor_width = x1 - w->phys_cursor.x;
23252 /* When we erase the cursor, and ROW is overlapped by other
23253 rows, make sure that these overlapping parts of other rows
23254 are redrawn. */
23255 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23256 {
23257 w->phys_cursor_width = x1 - w->phys_cursor.x;
23258
23259 if (row > w->current_matrix->rows
23260 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23261 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23262 OVERLAPS_ERASED_CURSOR);
23263
23264 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23265 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23266 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23267 OVERLAPS_ERASED_CURSOR);
23268 }
23269 }
23270 }
23271
23272
23273 /* EXPORT:
23274 Erase the image of a cursor of window W from the screen. */
23275
23276 void
23277 erase_phys_cursor (w)
23278 struct window *w;
23279 {
23280 struct frame *f = XFRAME (w->frame);
23281 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23282 int hpos = w->phys_cursor.hpos;
23283 int vpos = w->phys_cursor.vpos;
23284 int mouse_face_here_p = 0;
23285 struct glyph_matrix *active_glyphs = w->current_matrix;
23286 struct glyph_row *cursor_row;
23287 struct glyph *cursor_glyph;
23288 enum draw_glyphs_face hl;
23289
23290 /* No cursor displayed or row invalidated => nothing to do on the
23291 screen. */
23292 if (w->phys_cursor_type == NO_CURSOR)
23293 goto mark_cursor_off;
23294
23295 /* VPOS >= active_glyphs->nrows means that window has been resized.
23296 Don't bother to erase the cursor. */
23297 if (vpos >= active_glyphs->nrows)
23298 goto mark_cursor_off;
23299
23300 /* If row containing cursor is marked invalid, there is nothing we
23301 can do. */
23302 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23303 if (!cursor_row->enabled_p)
23304 goto mark_cursor_off;
23305
23306 /* If line spacing is > 0, old cursor may only be partially visible in
23307 window after split-window. So adjust visible height. */
23308 cursor_row->visible_height = min (cursor_row->visible_height,
23309 window_text_bottom_y (w) - cursor_row->y);
23310
23311 /* If row is completely invisible, don't attempt to delete a cursor which
23312 isn't there. This can happen if cursor is at top of a window, and
23313 we switch to a buffer with a header line in that window. */
23314 if (cursor_row->visible_height <= 0)
23315 goto mark_cursor_off;
23316
23317 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23318 if (cursor_row->cursor_in_fringe_p)
23319 {
23320 cursor_row->cursor_in_fringe_p = 0;
23321 draw_fringe_bitmap (w, cursor_row, 0);
23322 goto mark_cursor_off;
23323 }
23324
23325 /* This can happen when the new row is shorter than the old one.
23326 In this case, either draw_glyphs or clear_end_of_line
23327 should have cleared the cursor. Note that we wouldn't be
23328 able to erase the cursor in this case because we don't have a
23329 cursor glyph at hand. */
23330 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
23331 goto mark_cursor_off;
23332
23333 /* If the cursor is in the mouse face area, redisplay that when
23334 we clear the cursor. */
23335 if (! NILP (dpyinfo->mouse_face_window)
23336 && w == XWINDOW (dpyinfo->mouse_face_window)
23337 && (vpos > dpyinfo->mouse_face_beg_row
23338 || (vpos == dpyinfo->mouse_face_beg_row
23339 && hpos >= dpyinfo->mouse_face_beg_col))
23340 && (vpos < dpyinfo->mouse_face_end_row
23341 || (vpos == dpyinfo->mouse_face_end_row
23342 && hpos < dpyinfo->mouse_face_end_col))
23343 /* Don't redraw the cursor's spot in mouse face if it is at the
23344 end of a line (on a newline). The cursor appears there, but
23345 mouse highlighting does not. */
23346 && cursor_row->used[TEXT_AREA] > hpos)
23347 mouse_face_here_p = 1;
23348
23349 /* Maybe clear the display under the cursor. */
23350 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23351 {
23352 int x, y, left_x;
23353 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23354 int width;
23355
23356 cursor_glyph = get_phys_cursor_glyph (w);
23357 if (cursor_glyph == NULL)
23358 goto mark_cursor_off;
23359
23360 width = cursor_glyph->pixel_width;
23361 left_x = window_box_left_offset (w, TEXT_AREA);
23362 x = w->phys_cursor.x;
23363 if (x < left_x)
23364 width -= left_x - x;
23365 width = min (width, window_box_width (w, TEXT_AREA) - x);
23366 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23367 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23368
23369 if (width > 0)
23370 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23371 }
23372
23373 /* Erase the cursor by redrawing the character underneath it. */
23374 if (mouse_face_here_p)
23375 hl = DRAW_MOUSE_FACE;
23376 else
23377 hl = DRAW_NORMAL_TEXT;
23378 draw_phys_cursor_glyph (w, cursor_row, hl);
23379
23380 mark_cursor_off:
23381 w->phys_cursor_on_p = 0;
23382 w->phys_cursor_type = NO_CURSOR;
23383 }
23384
23385
23386 /* EXPORT:
23387 Display or clear cursor of window W. If ON is zero, clear the
23388 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23389 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23390
23391 void
23392 display_and_set_cursor (w, on, hpos, vpos, x, y)
23393 struct window *w;
23394 int on, hpos, vpos, x, y;
23395 {
23396 struct frame *f = XFRAME (w->frame);
23397 int new_cursor_type;
23398 int new_cursor_width;
23399 int active_cursor;
23400 struct glyph_row *glyph_row;
23401 struct glyph *glyph;
23402
23403 /* This is pointless on invisible frames, and dangerous on garbaged
23404 windows and frames; in the latter case, the frame or window may
23405 be in the midst of changing its size, and x and y may be off the
23406 window. */
23407 if (! FRAME_VISIBLE_P (f)
23408 || FRAME_GARBAGED_P (f)
23409 || vpos >= w->current_matrix->nrows
23410 || hpos >= w->current_matrix->matrix_w)
23411 return;
23412
23413 /* If cursor is off and we want it off, return quickly. */
23414 if (!on && !w->phys_cursor_on_p)
23415 return;
23416
23417 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23418 /* If cursor row is not enabled, we don't really know where to
23419 display the cursor. */
23420 if (!glyph_row->enabled_p)
23421 {
23422 w->phys_cursor_on_p = 0;
23423 return;
23424 }
23425
23426 glyph = NULL;
23427 if (!glyph_row->exact_window_width_line_p
23428 || hpos < glyph_row->used[TEXT_AREA])
23429 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23430
23431 xassert (interrupt_input_blocked);
23432
23433 /* Set new_cursor_type to the cursor we want to be displayed. */
23434 new_cursor_type = get_window_cursor_type (w, glyph,
23435 &new_cursor_width, &active_cursor);
23436
23437 /* If cursor is currently being shown and we don't want it to be or
23438 it is in the wrong place, or the cursor type is not what we want,
23439 erase it. */
23440 if (w->phys_cursor_on_p
23441 && (!on
23442 || w->phys_cursor.x != x
23443 || w->phys_cursor.y != y
23444 || new_cursor_type != w->phys_cursor_type
23445 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23446 && new_cursor_width != w->phys_cursor_width)))
23447 erase_phys_cursor (w);
23448
23449 /* Don't check phys_cursor_on_p here because that flag is only set
23450 to zero in some cases where we know that the cursor has been
23451 completely erased, to avoid the extra work of erasing the cursor
23452 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23453 still not be visible, or it has only been partly erased. */
23454 if (on)
23455 {
23456 w->phys_cursor_ascent = glyph_row->ascent;
23457 w->phys_cursor_height = glyph_row->height;
23458
23459 /* Set phys_cursor_.* before x_draw_.* is called because some
23460 of them may need the information. */
23461 w->phys_cursor.x = x;
23462 w->phys_cursor.y = glyph_row->y;
23463 w->phys_cursor.hpos = hpos;
23464 w->phys_cursor.vpos = vpos;
23465 }
23466
23467 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23468 new_cursor_type, new_cursor_width,
23469 on, active_cursor);
23470 }
23471
23472
23473 /* Switch the display of W's cursor on or off, according to the value
23474 of ON. */
23475
23476 void
23477 update_window_cursor (w, on)
23478 struct window *w;
23479 int on;
23480 {
23481 /* Don't update cursor in windows whose frame is in the process
23482 of being deleted. */
23483 if (w->current_matrix)
23484 {
23485 BLOCK_INPUT;
23486 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23487 w->phys_cursor.x, w->phys_cursor.y);
23488 UNBLOCK_INPUT;
23489 }
23490 }
23491
23492
23493 /* Call update_window_cursor with parameter ON_P on all leaf windows
23494 in the window tree rooted at W. */
23495
23496 static void
23497 update_cursor_in_window_tree (w, on_p)
23498 struct window *w;
23499 int on_p;
23500 {
23501 while (w)
23502 {
23503 if (!NILP (w->hchild))
23504 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23505 else if (!NILP (w->vchild))
23506 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23507 else
23508 update_window_cursor (w, on_p);
23509
23510 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23511 }
23512 }
23513
23514
23515 /* EXPORT:
23516 Display the cursor on window W, or clear it, according to ON_P.
23517 Don't change the cursor's position. */
23518
23519 void
23520 x_update_cursor (f, on_p)
23521 struct frame *f;
23522 int on_p;
23523 {
23524 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23525 }
23526
23527
23528 /* EXPORT:
23529 Clear the cursor of window W to background color, and mark the
23530 cursor as not shown. This is used when the text where the cursor
23531 is about to be rewritten. */
23532
23533 void
23534 x_clear_cursor (w)
23535 struct window *w;
23536 {
23537 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23538 update_window_cursor (w, 0);
23539 }
23540
23541
23542 /* EXPORT:
23543 Display the active region described by mouse_face_* according to DRAW. */
23544
23545 void
23546 show_mouse_face (dpyinfo, draw)
23547 Display_Info *dpyinfo;
23548 enum draw_glyphs_face draw;
23549 {
23550 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23551 struct frame *f = XFRAME (WINDOW_FRAME (w));
23552
23553 if (/* If window is in the process of being destroyed, don't bother
23554 to do anything. */
23555 w->current_matrix != NULL
23556 /* Don't update mouse highlight if hidden */
23557 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23558 /* Recognize when we are called to operate on rows that don't exist
23559 anymore. This can happen when a window is split. */
23560 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23561 {
23562 int phys_cursor_on_p = w->phys_cursor_on_p;
23563 struct glyph_row *row, *first, *last;
23564
23565 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23566 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23567
23568 for (row = first; row <= last && row->enabled_p; ++row)
23569 {
23570 int start_hpos, end_hpos, start_x;
23571
23572 /* For all but the first row, the highlight starts at column 0. */
23573 if (row == first)
23574 {
23575 start_hpos = dpyinfo->mouse_face_beg_col;
23576 start_x = dpyinfo->mouse_face_beg_x;
23577 }
23578 else
23579 {
23580 start_hpos = 0;
23581 start_x = 0;
23582 }
23583
23584 if (row == last)
23585 end_hpos = dpyinfo->mouse_face_end_col;
23586 else
23587 {
23588 end_hpos = row->used[TEXT_AREA];
23589 if (draw == DRAW_NORMAL_TEXT)
23590 row->fill_line_p = 1; /* Clear to end of line */
23591 }
23592
23593 if (end_hpos > start_hpos)
23594 {
23595 draw_glyphs (w, start_x, row, TEXT_AREA,
23596 start_hpos, end_hpos,
23597 draw, 0);
23598
23599 row->mouse_face_p
23600 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23601 }
23602 }
23603
23604 /* When we've written over the cursor, arrange for it to
23605 be displayed again. */
23606 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23607 {
23608 BLOCK_INPUT;
23609 display_and_set_cursor (w, 1,
23610 w->phys_cursor.hpos, w->phys_cursor.vpos,
23611 w->phys_cursor.x, w->phys_cursor.y);
23612 UNBLOCK_INPUT;
23613 }
23614 }
23615
23616 /* Change the mouse cursor. */
23617 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23618 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23619 else if (draw == DRAW_MOUSE_FACE)
23620 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23621 else
23622 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23623 }
23624
23625 /* EXPORT:
23626 Clear out the mouse-highlighted active region.
23627 Redraw it un-highlighted first. Value is non-zero if mouse
23628 face was actually drawn unhighlighted. */
23629
23630 int
23631 clear_mouse_face (dpyinfo)
23632 Display_Info *dpyinfo;
23633 {
23634 int cleared = 0;
23635
23636 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23637 {
23638 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23639 cleared = 1;
23640 }
23641
23642 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23643 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23644 dpyinfo->mouse_face_window = Qnil;
23645 dpyinfo->mouse_face_overlay = Qnil;
23646 return cleared;
23647 }
23648
23649
23650 /* EXPORT:
23651 Non-zero if physical cursor of window W is within mouse face. */
23652
23653 int
23654 cursor_in_mouse_face_p (w)
23655 struct window *w;
23656 {
23657 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23658 int in_mouse_face = 0;
23659
23660 if (WINDOWP (dpyinfo->mouse_face_window)
23661 && XWINDOW (dpyinfo->mouse_face_window) == w)
23662 {
23663 int hpos = w->phys_cursor.hpos;
23664 int vpos = w->phys_cursor.vpos;
23665
23666 if (vpos >= dpyinfo->mouse_face_beg_row
23667 && vpos <= dpyinfo->mouse_face_end_row
23668 && (vpos > dpyinfo->mouse_face_beg_row
23669 || hpos >= dpyinfo->mouse_face_beg_col)
23670 && (vpos < dpyinfo->mouse_face_end_row
23671 || hpos < dpyinfo->mouse_face_end_col
23672 || dpyinfo->mouse_face_past_end))
23673 in_mouse_face = 1;
23674 }
23675
23676 return in_mouse_face;
23677 }
23678
23679
23680
23681 \f
23682 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23683 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23684 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23685 for the overlay or run of text properties specifying the mouse
23686 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23687 before-string and after-string that must also be highlighted.
23688 DISPLAY_STRING, if non-nil, is a display string that may cover some
23689 or all of the highlighted text. */
23690
23691 static void
23692 mouse_face_from_buffer_pos (Lisp_Object window,
23693 Display_Info *dpyinfo,
23694 EMACS_INT mouse_charpos,
23695 EMACS_INT start_charpos,
23696 EMACS_INT end_charpos,
23697 Lisp_Object before_string,
23698 Lisp_Object after_string,
23699 Lisp_Object display_string)
23700 {
23701 struct window *w = XWINDOW (window);
23702 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23703 struct glyph_row *row;
23704 struct glyph *glyph, *end;
23705 EMACS_INT ignore;
23706 int x;
23707
23708 xassert (NILP (display_string) || STRINGP (display_string));
23709 xassert (NILP (before_string) || STRINGP (before_string));
23710 xassert (NILP (after_string) || STRINGP (after_string));
23711
23712 /* Find the first highlighted glyph. */
23713 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
23714 {
23715 dpyinfo->mouse_face_beg_col = 0;
23716 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23717 dpyinfo->mouse_face_beg_x = first->x;
23718 dpyinfo->mouse_face_beg_y = first->y;
23719 }
23720 else
23721 {
23722 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23723 if (row == NULL)
23724 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23725
23726 /* If the before-string or display-string contains newlines,
23727 row_containing_pos skips to its last row. Move back. */
23728 if (!NILP (before_string) || !NILP (display_string))
23729 {
23730 struct glyph_row *prev;
23731 while ((prev = row - 1, prev >= first)
23732 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23733 && prev->used[TEXT_AREA] > 0)
23734 {
23735 struct glyph *beg = prev->glyphs[TEXT_AREA];
23736 glyph = beg + prev->used[TEXT_AREA];
23737 while (--glyph >= beg && INTEGERP (glyph->object));
23738 if (glyph < beg
23739 || !(EQ (glyph->object, before_string)
23740 || EQ (glyph->object, display_string)))
23741 break;
23742 row = prev;
23743 }
23744 }
23745
23746 glyph = row->glyphs[TEXT_AREA];
23747 end = glyph + row->used[TEXT_AREA];
23748 x = row->x;
23749 dpyinfo->mouse_face_beg_y = row->y;
23750 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23751
23752 /* Skip truncation glyphs at the start of the glyph row. */
23753 if (row->displays_text_p)
23754 for (; glyph < end
23755 && INTEGERP (glyph->object)
23756 && glyph->charpos < 0;
23757 ++glyph)
23758 x += glyph->pixel_width;
23759
23760 /* Scan the glyph row, stopping before BEFORE_STRING or
23761 DISPLAY_STRING or START_CHARPOS. */
23762 for (; glyph < end
23763 && !INTEGERP (glyph->object)
23764 && !EQ (glyph->object, before_string)
23765 && !EQ (glyph->object, display_string)
23766 && !(BUFFERP (glyph->object)
23767 && glyph->charpos >= start_charpos);
23768 ++glyph)
23769 x += glyph->pixel_width;
23770
23771 dpyinfo->mouse_face_beg_x = x;
23772 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23773 }
23774
23775 /* Find the last highlighted glyph. */
23776 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23777 if (row == NULL)
23778 {
23779 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23780 dpyinfo->mouse_face_past_end = 1;
23781 }
23782 else if (!NILP (after_string))
23783 {
23784 /* If the after-string has newlines, advance to its last row. */
23785 struct glyph_row *next;
23786 struct glyph_row *last
23787 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23788
23789 for (next = row + 1;
23790 next <= last
23791 && next->used[TEXT_AREA] > 0
23792 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23793 ++next)
23794 row = next;
23795 }
23796
23797 glyph = row->glyphs[TEXT_AREA];
23798 end = glyph + row->used[TEXT_AREA];
23799 x = row->x;
23800 dpyinfo->mouse_face_end_y = row->y;
23801 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23802
23803 /* Skip truncation glyphs at the start of the row. */
23804 if (row->displays_text_p)
23805 for (; glyph < end
23806 && INTEGERP (glyph->object)
23807 && glyph->charpos < 0;
23808 ++glyph)
23809 x += glyph->pixel_width;
23810
23811 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23812 AFTER_STRING. */
23813 for (; glyph < end
23814 && !INTEGERP (glyph->object)
23815 && !EQ (glyph->object, after_string)
23816 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23817 ++glyph)
23818 x += glyph->pixel_width;
23819
23820 /* If we found AFTER_STRING, consume it and stop. */
23821 if (EQ (glyph->object, after_string))
23822 {
23823 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23824 x += glyph->pixel_width;
23825 }
23826 else
23827 {
23828 /* If there's no after-string, we must check if we overshot,
23829 which might be the case if we stopped after a string glyph.
23830 That glyph may belong to a before-string or display-string
23831 associated with the end position, which must not be
23832 highlighted. */
23833 Lisp_Object prev_object;
23834 EMACS_INT pos;
23835
23836 while (glyph > row->glyphs[TEXT_AREA])
23837 {
23838 prev_object = (glyph - 1)->object;
23839 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23840 break;
23841
23842 pos = string_buffer_position (w, prev_object, end_charpos);
23843 if (pos && pos < end_charpos)
23844 break;
23845
23846 for (; glyph > row->glyphs[TEXT_AREA]
23847 && EQ ((glyph - 1)->object, prev_object);
23848 --glyph)
23849 x -= (glyph - 1)->pixel_width;
23850 }
23851 }
23852
23853 dpyinfo->mouse_face_end_x = x;
23854 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23855 dpyinfo->mouse_face_window = window;
23856 dpyinfo->mouse_face_face_id
23857 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23858 mouse_charpos + 1,
23859 !dpyinfo->mouse_face_hidden, -1);
23860 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23861 }
23862
23863
23864 /* Find the position of the glyph for position POS in OBJECT in
23865 window W's current matrix, and return in *X, *Y the pixel
23866 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23867
23868 RIGHT_P non-zero means return the position of the right edge of the
23869 glyph, RIGHT_P zero means return the left edge position.
23870
23871 If no glyph for POS exists in the matrix, return the position of
23872 the glyph with the next smaller position that is in the matrix, if
23873 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23874 exists in the matrix, return the position of the glyph with the
23875 next larger position in OBJECT.
23876
23877 Value is non-zero if a glyph was found. */
23878
23879 static int
23880 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23881 struct window *w;
23882 EMACS_INT pos;
23883 Lisp_Object object;
23884 int *hpos, *vpos, *x, *y;
23885 int right_p;
23886 {
23887 int yb = window_text_bottom_y (w);
23888 struct glyph_row *r;
23889 struct glyph *best_glyph = NULL;
23890 struct glyph_row *best_row = NULL;
23891 int best_x = 0;
23892
23893 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23894 r->enabled_p && r->y < yb;
23895 ++r)
23896 {
23897 struct glyph *g = r->glyphs[TEXT_AREA];
23898 struct glyph *e = g + r->used[TEXT_AREA];
23899 int gx;
23900
23901 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23902 if (EQ (g->object, object))
23903 {
23904 if (g->charpos == pos)
23905 {
23906 best_glyph = g;
23907 best_x = gx;
23908 best_row = r;
23909 goto found;
23910 }
23911 else if (best_glyph == NULL
23912 || ((eabs (g->charpos - pos)
23913 < eabs (best_glyph->charpos - pos))
23914 && (right_p
23915 ? g->charpos < pos
23916 : g->charpos > pos)))
23917 {
23918 best_glyph = g;
23919 best_x = gx;
23920 best_row = r;
23921 }
23922 }
23923 }
23924
23925 found:
23926
23927 if (best_glyph)
23928 {
23929 *x = best_x;
23930 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23931
23932 if (right_p)
23933 {
23934 *x += best_glyph->pixel_width;
23935 ++*hpos;
23936 }
23937
23938 *y = best_row->y;
23939 *vpos = best_row - w->current_matrix->rows;
23940 }
23941
23942 return best_glyph != NULL;
23943 }
23944
23945
23946 /* See if position X, Y is within a hot-spot of an image. */
23947
23948 static int
23949 on_hot_spot_p (hot_spot, x, y)
23950 Lisp_Object hot_spot;
23951 int x, y;
23952 {
23953 if (!CONSP (hot_spot))
23954 return 0;
23955
23956 if (EQ (XCAR (hot_spot), Qrect))
23957 {
23958 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23959 Lisp_Object rect = XCDR (hot_spot);
23960 Lisp_Object tem;
23961 if (!CONSP (rect))
23962 return 0;
23963 if (!CONSP (XCAR (rect)))
23964 return 0;
23965 if (!CONSP (XCDR (rect)))
23966 return 0;
23967 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23968 return 0;
23969 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23970 return 0;
23971 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23972 return 0;
23973 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23974 return 0;
23975 return 1;
23976 }
23977 else if (EQ (XCAR (hot_spot), Qcircle))
23978 {
23979 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23980 Lisp_Object circ = XCDR (hot_spot);
23981 Lisp_Object lr, lx0, ly0;
23982 if (CONSP (circ)
23983 && CONSP (XCAR (circ))
23984 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23985 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23986 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23987 {
23988 double r = XFLOATINT (lr);
23989 double dx = XINT (lx0) - x;
23990 double dy = XINT (ly0) - y;
23991 return (dx * dx + dy * dy <= r * r);
23992 }
23993 }
23994 else if (EQ (XCAR (hot_spot), Qpoly))
23995 {
23996 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23997 if (VECTORP (XCDR (hot_spot)))
23998 {
23999 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24000 Lisp_Object *poly = v->contents;
24001 int n = v->size;
24002 int i;
24003 int inside = 0;
24004 Lisp_Object lx, ly;
24005 int x0, y0;
24006
24007 /* Need an even number of coordinates, and at least 3 edges. */
24008 if (n < 6 || n & 1)
24009 return 0;
24010
24011 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24012 If count is odd, we are inside polygon. Pixels on edges
24013 may or may not be included depending on actual geometry of the
24014 polygon. */
24015 if ((lx = poly[n-2], !INTEGERP (lx))
24016 || (ly = poly[n-1], !INTEGERP (lx)))
24017 return 0;
24018 x0 = XINT (lx), y0 = XINT (ly);
24019 for (i = 0; i < n; i += 2)
24020 {
24021 int x1 = x0, y1 = y0;
24022 if ((lx = poly[i], !INTEGERP (lx))
24023 || (ly = poly[i+1], !INTEGERP (ly)))
24024 return 0;
24025 x0 = XINT (lx), y0 = XINT (ly);
24026
24027 /* Does this segment cross the X line? */
24028 if (x0 >= x)
24029 {
24030 if (x1 >= x)
24031 continue;
24032 }
24033 else if (x1 < x)
24034 continue;
24035 if (y > y0 && y > y1)
24036 continue;
24037 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24038 inside = !inside;
24039 }
24040 return inside;
24041 }
24042 }
24043 return 0;
24044 }
24045
24046 Lisp_Object
24047 find_hot_spot (map, x, y)
24048 Lisp_Object map;
24049 int x, y;
24050 {
24051 while (CONSP (map))
24052 {
24053 if (CONSP (XCAR (map))
24054 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24055 return XCAR (map);
24056 map = XCDR (map);
24057 }
24058
24059 return Qnil;
24060 }
24061
24062 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24063 3, 3, 0,
24064 doc: /* Lookup in image map MAP coordinates X and Y.
24065 An image map is an alist where each element has the format (AREA ID PLIST).
24066 An AREA is specified as either a rectangle, a circle, or a polygon:
24067 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24068 pixel coordinates of the upper left and bottom right corners.
24069 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24070 and the radius of the circle; r may be a float or integer.
24071 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24072 vector describes one corner in the polygon.
24073 Returns the alist element for the first matching AREA in MAP. */)
24074 (map, x, y)
24075 Lisp_Object map;
24076 Lisp_Object x, y;
24077 {
24078 if (NILP (map))
24079 return Qnil;
24080
24081 CHECK_NUMBER (x);
24082 CHECK_NUMBER (y);
24083
24084 return find_hot_spot (map, XINT (x), XINT (y));
24085 }
24086
24087
24088 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24089 static void
24090 define_frame_cursor1 (f, cursor, pointer)
24091 struct frame *f;
24092 Cursor cursor;
24093 Lisp_Object pointer;
24094 {
24095 /* Do not change cursor shape while dragging mouse. */
24096 if (!NILP (do_mouse_tracking))
24097 return;
24098
24099 if (!NILP (pointer))
24100 {
24101 if (EQ (pointer, Qarrow))
24102 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24103 else if (EQ (pointer, Qhand))
24104 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24105 else if (EQ (pointer, Qtext))
24106 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24107 else if (EQ (pointer, intern ("hdrag")))
24108 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24109 #ifdef HAVE_X_WINDOWS
24110 else if (EQ (pointer, intern ("vdrag")))
24111 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24112 #endif
24113 else if (EQ (pointer, intern ("hourglass")))
24114 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24115 else if (EQ (pointer, Qmodeline))
24116 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24117 else
24118 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24119 }
24120
24121 if (cursor != No_Cursor)
24122 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24123 }
24124
24125 /* Take proper action when mouse has moved to the mode or header line
24126 or marginal area AREA of window W, x-position X and y-position Y.
24127 X is relative to the start of the text display area of W, so the
24128 width of bitmap areas and scroll bars must be subtracted to get a
24129 position relative to the start of the mode line. */
24130
24131 static void
24132 note_mode_line_or_margin_highlight (window, x, y, area)
24133 Lisp_Object window;
24134 int x, y;
24135 enum window_part area;
24136 {
24137 struct window *w = XWINDOW (window);
24138 struct frame *f = XFRAME (w->frame);
24139 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24140 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24141 Lisp_Object pointer = Qnil;
24142 int charpos, dx, dy, width, height;
24143 Lisp_Object string, object = Qnil;
24144 Lisp_Object pos, help;
24145
24146 Lisp_Object mouse_face;
24147 int original_x_pixel = x;
24148 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24149 struct glyph_row *row;
24150
24151 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24152 {
24153 int x0;
24154 struct glyph *end;
24155
24156 string = mode_line_string (w, area, &x, &y, &charpos,
24157 &object, &dx, &dy, &width, &height);
24158
24159 row = (area == ON_MODE_LINE
24160 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24161 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24162
24163 /* Find glyph */
24164 if (row->mode_line_p && row->enabled_p)
24165 {
24166 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24167 end = glyph + row->used[TEXT_AREA];
24168
24169 for (x0 = original_x_pixel;
24170 glyph < end && x0 >= glyph->pixel_width;
24171 ++glyph)
24172 x0 -= glyph->pixel_width;
24173
24174 if (glyph >= end)
24175 glyph = NULL;
24176 }
24177 }
24178 else
24179 {
24180 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
24181 string = marginal_area_string (w, area, &x, &y, &charpos,
24182 &object, &dx, &dy, &width, &height);
24183 }
24184
24185 help = Qnil;
24186
24187 if (IMAGEP (object))
24188 {
24189 Lisp_Object image_map, hotspot;
24190 if ((image_map = Fplist_get (XCDR (object), QCmap),
24191 !NILP (image_map))
24192 && (hotspot = find_hot_spot (image_map, dx, dy),
24193 CONSP (hotspot))
24194 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24195 {
24196 Lisp_Object area_id, plist;
24197
24198 area_id = XCAR (hotspot);
24199 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24200 If so, we could look for mouse-enter, mouse-leave
24201 properties in PLIST (and do something...). */
24202 hotspot = XCDR (hotspot);
24203 if (CONSP (hotspot)
24204 && (plist = XCAR (hotspot), CONSP (plist)))
24205 {
24206 pointer = Fplist_get (plist, Qpointer);
24207 if (NILP (pointer))
24208 pointer = Qhand;
24209 help = Fplist_get (plist, Qhelp_echo);
24210 if (!NILP (help))
24211 {
24212 help_echo_string = help;
24213 /* Is this correct? ++kfs */
24214 XSETWINDOW (help_echo_window, w);
24215 help_echo_object = w->buffer;
24216 help_echo_pos = charpos;
24217 }
24218 }
24219 }
24220 if (NILP (pointer))
24221 pointer = Fplist_get (XCDR (object), QCpointer);
24222 }
24223
24224 if (STRINGP (string))
24225 {
24226 pos = make_number (charpos);
24227 /* If we're on a string with `help-echo' text property, arrange
24228 for the help to be displayed. This is done by setting the
24229 global variable help_echo_string to the help string. */
24230 if (NILP (help))
24231 {
24232 help = Fget_text_property (pos, Qhelp_echo, string);
24233 if (!NILP (help))
24234 {
24235 help_echo_string = help;
24236 XSETWINDOW (help_echo_window, w);
24237 help_echo_object = string;
24238 help_echo_pos = charpos;
24239 }
24240 }
24241
24242 if (NILP (pointer))
24243 pointer = Fget_text_property (pos, Qpointer, string);
24244
24245 /* Change the mouse pointer according to what is under X/Y. */
24246 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24247 {
24248 Lisp_Object map;
24249 map = Fget_text_property (pos, Qlocal_map, string);
24250 if (!KEYMAPP (map))
24251 map = Fget_text_property (pos, Qkeymap, string);
24252 if (!KEYMAPP (map))
24253 cursor = dpyinfo->vertical_scroll_bar_cursor;
24254 }
24255
24256 /* Change the mouse face according to what is under X/Y. */
24257 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24258 if (!NILP (mouse_face)
24259 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24260 && glyph)
24261 {
24262 Lisp_Object b, e;
24263
24264 struct glyph * tmp_glyph;
24265
24266 int gpos;
24267 int gseq_length;
24268 int total_pixel_width;
24269 EMACS_INT ignore;
24270
24271 int vpos, hpos;
24272
24273 b = Fprevious_single_property_change (make_number (charpos + 1),
24274 Qmouse_face, string, Qnil);
24275 if (NILP (b))
24276 b = make_number (0);
24277
24278 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24279 if (NILP (e))
24280 e = make_number (SCHARS (string));
24281
24282 /* Calculate the position(glyph position: GPOS) of GLYPH in
24283 displayed string. GPOS is different from CHARPOS.
24284
24285 CHARPOS is the position of glyph in internal string
24286 object. A mode line string format has structures which
24287 is converted to a flatten by emacs lisp interpreter.
24288 The internal string is an element of the structures.
24289 The displayed string is the flatten string. */
24290 gpos = 0;
24291 if (glyph > row_start_glyph)
24292 {
24293 tmp_glyph = glyph - 1;
24294 while (tmp_glyph >= row_start_glyph
24295 && tmp_glyph->charpos >= XINT (b)
24296 && EQ (tmp_glyph->object, glyph->object))
24297 {
24298 tmp_glyph--;
24299 gpos++;
24300 }
24301 }
24302
24303 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
24304 displayed string holding GLYPH.
24305
24306 GSEQ_LENGTH is different from SCHARS (STRING).
24307 SCHARS (STRING) returns the length of the internal string. */
24308 for (tmp_glyph = glyph, gseq_length = gpos;
24309 tmp_glyph->charpos < XINT (e);
24310 tmp_glyph++, gseq_length++)
24311 {
24312 if (!EQ (tmp_glyph->object, glyph->object))
24313 break;
24314 }
24315
24316 total_pixel_width = 0;
24317 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24318 total_pixel_width += tmp_glyph->pixel_width;
24319
24320 /* Pre calculation of re-rendering position */
24321 vpos = (x - gpos);
24322 hpos = (area == ON_MODE_LINE
24323 ? (w->current_matrix)->nrows - 1
24324 : 0);
24325
24326 /* If the re-rendering position is included in the last
24327 re-rendering area, we should do nothing. */
24328 if ( EQ (window, dpyinfo->mouse_face_window)
24329 && dpyinfo->mouse_face_beg_col <= vpos
24330 && vpos < dpyinfo->mouse_face_end_col
24331 && dpyinfo->mouse_face_beg_row == hpos )
24332 return;
24333
24334 if (clear_mouse_face (dpyinfo))
24335 cursor = No_Cursor;
24336
24337 dpyinfo->mouse_face_beg_col = vpos;
24338 dpyinfo->mouse_face_beg_row = hpos;
24339
24340 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
24341 dpyinfo->mouse_face_beg_y = 0;
24342
24343 dpyinfo->mouse_face_end_col = vpos + gseq_length;
24344 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24345
24346 dpyinfo->mouse_face_end_x = 0;
24347 dpyinfo->mouse_face_end_y = 0;
24348
24349 dpyinfo->mouse_face_past_end = 0;
24350 dpyinfo->mouse_face_window = window;
24351
24352 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24353 charpos,
24354 0, 0, 0, &ignore,
24355 glyph->face_id, 1);
24356 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24357
24358 if (NILP (pointer))
24359 pointer = Qhand;
24360 }
24361 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24362 clear_mouse_face (dpyinfo);
24363 }
24364 define_frame_cursor1 (f, cursor, pointer);
24365 }
24366
24367
24368 /* EXPORT:
24369 Take proper action when the mouse has moved to position X, Y on
24370 frame F as regards highlighting characters that have mouse-face
24371 properties. Also de-highlighting chars where the mouse was before.
24372 X and Y can be negative or out of range. */
24373
24374 void
24375 note_mouse_highlight (f, x, y)
24376 struct frame *f;
24377 int x, y;
24378 {
24379 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24380 enum window_part part;
24381 Lisp_Object window;
24382 struct window *w;
24383 Cursor cursor = No_Cursor;
24384 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24385 struct buffer *b;
24386
24387 /* When a menu is active, don't highlight because this looks odd. */
24388 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24389 if (popup_activated ())
24390 return;
24391 #endif
24392
24393 if (NILP (Vmouse_highlight)
24394 || !f->glyphs_initialized_p
24395 || f->pointer_invisible)
24396 return;
24397
24398 dpyinfo->mouse_face_mouse_x = x;
24399 dpyinfo->mouse_face_mouse_y = y;
24400 dpyinfo->mouse_face_mouse_frame = f;
24401
24402 if (dpyinfo->mouse_face_defer)
24403 return;
24404
24405 if (gc_in_progress)
24406 {
24407 dpyinfo->mouse_face_deferred_gc = 1;
24408 return;
24409 }
24410
24411 /* Which window is that in? */
24412 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24413
24414 /* If we were displaying active text in another window, clear that.
24415 Also clear if we move out of text area in same window. */
24416 if (! EQ (window, dpyinfo->mouse_face_window)
24417 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24418 && !NILP (dpyinfo->mouse_face_window)))
24419 clear_mouse_face (dpyinfo);
24420
24421 /* Not on a window -> return. */
24422 if (!WINDOWP (window))
24423 return;
24424
24425 /* Reset help_echo_string. It will get recomputed below. */
24426 help_echo_string = Qnil;
24427
24428 /* Convert to window-relative pixel coordinates. */
24429 w = XWINDOW (window);
24430 frame_to_window_pixel_xy (w, &x, &y);
24431
24432 /* Handle tool-bar window differently since it doesn't display a
24433 buffer. */
24434 if (EQ (window, f->tool_bar_window))
24435 {
24436 note_tool_bar_highlight (f, x, y);
24437 return;
24438 }
24439
24440 /* Mouse is on the mode, header line or margin? */
24441 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24442 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24443 {
24444 note_mode_line_or_margin_highlight (window, x, y, part);
24445 return;
24446 }
24447
24448 if (part == ON_VERTICAL_BORDER)
24449 {
24450 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24451 help_echo_string = build_string ("drag-mouse-1: resize");
24452 }
24453 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24454 || part == ON_SCROLL_BAR)
24455 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24456 else
24457 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24458
24459 /* Are we in a window whose display is up to date?
24460 And verify the buffer's text has not changed. */
24461 b = XBUFFER (w->buffer);
24462 if (part == ON_TEXT
24463 && EQ (w->window_end_valid, w->buffer)
24464 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24465 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24466 {
24467 int hpos, vpos, i, dx, dy, area;
24468 EMACS_INT pos;
24469 struct glyph *glyph;
24470 Lisp_Object object;
24471 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24472 Lisp_Object *overlay_vec = NULL;
24473 int noverlays;
24474 struct buffer *obuf;
24475 int obegv, ozv, same_region;
24476
24477 /* Find the glyph under X/Y. */
24478 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24479
24480 /* Look for :pointer property on image. */
24481 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24482 {
24483 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24484 if (img != NULL && IMAGEP (img->spec))
24485 {
24486 Lisp_Object image_map, hotspot;
24487 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24488 !NILP (image_map))
24489 && (hotspot = find_hot_spot (image_map,
24490 glyph->slice.x + dx,
24491 glyph->slice.y + dy),
24492 CONSP (hotspot))
24493 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24494 {
24495 Lisp_Object area_id, plist;
24496
24497 area_id = XCAR (hotspot);
24498 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24499 If so, we could look for mouse-enter, mouse-leave
24500 properties in PLIST (and do something...). */
24501 hotspot = XCDR (hotspot);
24502 if (CONSP (hotspot)
24503 && (plist = XCAR (hotspot), CONSP (plist)))
24504 {
24505 pointer = Fplist_get (plist, Qpointer);
24506 if (NILP (pointer))
24507 pointer = Qhand;
24508 help_echo_string = Fplist_get (plist, Qhelp_echo);
24509 if (!NILP (help_echo_string))
24510 {
24511 help_echo_window = window;
24512 help_echo_object = glyph->object;
24513 help_echo_pos = glyph->charpos;
24514 }
24515 }
24516 }
24517 if (NILP (pointer))
24518 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24519 }
24520 }
24521
24522 /* Clear mouse face if X/Y not over text. */
24523 if (glyph == NULL
24524 || area != TEXT_AREA
24525 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24526 {
24527 if (clear_mouse_face (dpyinfo))
24528 cursor = No_Cursor;
24529 if (NILP (pointer))
24530 {
24531 if (area != TEXT_AREA)
24532 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24533 else
24534 pointer = Vvoid_text_area_pointer;
24535 }
24536 goto set_cursor;
24537 }
24538
24539 pos = glyph->charpos;
24540 object = glyph->object;
24541 if (!STRINGP (object) && !BUFFERP (object))
24542 goto set_cursor;
24543
24544 /* If we get an out-of-range value, return now; avoid an error. */
24545 if (BUFFERP (object) && pos > BUF_Z (b))
24546 goto set_cursor;
24547
24548 /* Make the window's buffer temporarily current for
24549 overlays_at and compute_char_face. */
24550 obuf = current_buffer;
24551 current_buffer = b;
24552 obegv = BEGV;
24553 ozv = ZV;
24554 BEGV = BEG;
24555 ZV = Z;
24556
24557 /* Is this char mouse-active or does it have help-echo? */
24558 position = make_number (pos);
24559
24560 if (BUFFERP (object))
24561 {
24562 /* Put all the overlays we want in a vector in overlay_vec. */
24563 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24564 /* Sort overlays into increasing priority order. */
24565 noverlays = sort_overlays (overlay_vec, noverlays, w);
24566 }
24567 else
24568 noverlays = 0;
24569
24570 same_region = (EQ (window, dpyinfo->mouse_face_window)
24571 && vpos >= dpyinfo->mouse_face_beg_row
24572 && vpos <= dpyinfo->mouse_face_end_row
24573 && (vpos > dpyinfo->mouse_face_beg_row
24574 || hpos >= dpyinfo->mouse_face_beg_col)
24575 && (vpos < dpyinfo->mouse_face_end_row
24576 || hpos < dpyinfo->mouse_face_end_col
24577 || dpyinfo->mouse_face_past_end));
24578
24579 if (same_region)
24580 cursor = No_Cursor;
24581
24582 /* Check mouse-face highlighting. */
24583 if (! same_region
24584 /* If there exists an overlay with mouse-face overlapping
24585 the one we are currently highlighting, we have to
24586 check if we enter the overlapping overlay, and then
24587 highlight only that. */
24588 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24589 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24590 {
24591 /* Find the highest priority overlay with a mouse-face. */
24592 overlay = Qnil;
24593 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24594 {
24595 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24596 if (!NILP (mouse_face))
24597 overlay = overlay_vec[i];
24598 }
24599
24600 /* If we're highlighting the same overlay as before, there's
24601 no need to do that again. */
24602 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24603 goto check_help_echo;
24604 dpyinfo->mouse_face_overlay = overlay;
24605
24606 /* Clear the display of the old active region, if any. */
24607 if (clear_mouse_face (dpyinfo))
24608 cursor = No_Cursor;
24609
24610 /* If no overlay applies, get a text property. */
24611 if (NILP (overlay))
24612 mouse_face = Fget_text_property (position, Qmouse_face, object);
24613
24614 /* Next, compute the bounds of the mouse highlighting and
24615 display it. */
24616 if (!NILP (mouse_face) && STRINGP (object))
24617 {
24618 /* The mouse-highlighting comes from a display string
24619 with a mouse-face. */
24620 Lisp_Object b, e;
24621 EMACS_INT ignore;
24622
24623 b = Fprevious_single_property_change
24624 (make_number (pos + 1), Qmouse_face, object, Qnil);
24625 e = Fnext_single_property_change
24626 (position, Qmouse_face, object, Qnil);
24627 if (NILP (b))
24628 b = make_number (0);
24629 if (NILP (e))
24630 e = make_number (SCHARS (object) - 1);
24631
24632 fast_find_string_pos (w, XINT (b), object,
24633 &dpyinfo->mouse_face_beg_col,
24634 &dpyinfo->mouse_face_beg_row,
24635 &dpyinfo->mouse_face_beg_x,
24636 &dpyinfo->mouse_face_beg_y, 0);
24637 fast_find_string_pos (w, XINT (e), object,
24638 &dpyinfo->mouse_face_end_col,
24639 &dpyinfo->mouse_face_end_row,
24640 &dpyinfo->mouse_face_end_x,
24641 &dpyinfo->mouse_face_end_y, 1);
24642 dpyinfo->mouse_face_past_end = 0;
24643 dpyinfo->mouse_face_window = window;
24644 dpyinfo->mouse_face_face_id
24645 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24646 glyph->face_id, 1);
24647 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24648 cursor = No_Cursor;
24649 }
24650 else
24651 {
24652 /* The mouse-highlighting, if any, comes from an overlay
24653 or text property in the buffer. */
24654 Lisp_Object buffer, display_string;
24655
24656 if (STRINGP (object))
24657 {
24658 /* If we are on a display string with no mouse-face,
24659 check if the text under it has one. */
24660 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24661 int start = MATRIX_ROW_START_CHARPOS (r);
24662 pos = string_buffer_position (w, object, start);
24663 if (pos > 0)
24664 {
24665 mouse_face = get_char_property_and_overlay
24666 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24667 buffer = w->buffer;
24668 display_string = object;
24669 }
24670 }
24671 else
24672 {
24673 buffer = object;
24674 display_string = Qnil;
24675 }
24676
24677 if (!NILP (mouse_face))
24678 {
24679 Lisp_Object before, after;
24680 Lisp_Object before_string, after_string;
24681
24682 if (NILP (overlay))
24683 {
24684 /* Handle the text property case. */
24685 before = Fprevious_single_property_change
24686 (make_number (pos + 1), Qmouse_face, buffer,
24687 Fmarker_position (w->start));
24688 after = Fnext_single_property_change
24689 (make_number (pos), Qmouse_face, buffer,
24690 make_number (BUF_Z (XBUFFER (buffer))
24691 - XFASTINT (w->window_end_pos)));
24692 before_string = after_string = Qnil;
24693 }
24694 else
24695 {
24696 /* Handle the overlay case. */
24697 before = Foverlay_start (overlay);
24698 after = Foverlay_end (overlay);
24699 before_string = Foverlay_get (overlay, Qbefore_string);
24700 after_string = Foverlay_get (overlay, Qafter_string);
24701
24702 if (!STRINGP (before_string)) before_string = Qnil;
24703 if (!STRINGP (after_string)) after_string = Qnil;
24704 }
24705
24706 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24707 XFASTINT (before),
24708 XFASTINT (after),
24709 before_string, after_string,
24710 display_string);
24711 cursor = No_Cursor;
24712 }
24713 }
24714 }
24715
24716 check_help_echo:
24717
24718 /* Look for a `help-echo' property. */
24719 if (NILP (help_echo_string)) {
24720 Lisp_Object help, overlay;
24721
24722 /* Check overlays first. */
24723 help = overlay = Qnil;
24724 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24725 {
24726 overlay = overlay_vec[i];
24727 help = Foverlay_get (overlay, Qhelp_echo);
24728 }
24729
24730 if (!NILP (help))
24731 {
24732 help_echo_string = help;
24733 help_echo_window = window;
24734 help_echo_object = overlay;
24735 help_echo_pos = pos;
24736 }
24737 else
24738 {
24739 Lisp_Object object = glyph->object;
24740 int charpos = glyph->charpos;
24741
24742 /* Try text properties. */
24743 if (STRINGP (object)
24744 && charpos >= 0
24745 && charpos < SCHARS (object))
24746 {
24747 help = Fget_text_property (make_number (charpos),
24748 Qhelp_echo, object);
24749 if (NILP (help))
24750 {
24751 /* If the string itself doesn't specify a help-echo,
24752 see if the buffer text ``under'' it does. */
24753 struct glyph_row *r
24754 = MATRIX_ROW (w->current_matrix, vpos);
24755 int start = MATRIX_ROW_START_CHARPOS (r);
24756 EMACS_INT pos = string_buffer_position (w, object, start);
24757 if (pos > 0)
24758 {
24759 help = Fget_char_property (make_number (pos),
24760 Qhelp_echo, w->buffer);
24761 if (!NILP (help))
24762 {
24763 charpos = pos;
24764 object = w->buffer;
24765 }
24766 }
24767 }
24768 }
24769 else if (BUFFERP (object)
24770 && charpos >= BEGV
24771 && charpos < ZV)
24772 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24773 object);
24774
24775 if (!NILP (help))
24776 {
24777 help_echo_string = help;
24778 help_echo_window = window;
24779 help_echo_object = object;
24780 help_echo_pos = charpos;
24781 }
24782 }
24783 }
24784
24785 /* Look for a `pointer' property. */
24786 if (NILP (pointer))
24787 {
24788 /* Check overlays first. */
24789 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24790 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24791
24792 if (NILP (pointer))
24793 {
24794 Lisp_Object object = glyph->object;
24795 int charpos = glyph->charpos;
24796
24797 /* Try text properties. */
24798 if (STRINGP (object)
24799 && charpos >= 0
24800 && charpos < SCHARS (object))
24801 {
24802 pointer = Fget_text_property (make_number (charpos),
24803 Qpointer, object);
24804 if (NILP (pointer))
24805 {
24806 /* If the string itself doesn't specify a pointer,
24807 see if the buffer text ``under'' it does. */
24808 struct glyph_row *r
24809 = MATRIX_ROW (w->current_matrix, vpos);
24810 int start = MATRIX_ROW_START_CHARPOS (r);
24811 EMACS_INT pos = string_buffer_position (w, object,
24812 start);
24813 if (pos > 0)
24814 pointer = Fget_char_property (make_number (pos),
24815 Qpointer, w->buffer);
24816 }
24817 }
24818 else if (BUFFERP (object)
24819 && charpos >= BEGV
24820 && charpos < ZV)
24821 pointer = Fget_text_property (make_number (charpos),
24822 Qpointer, object);
24823 }
24824 }
24825
24826 BEGV = obegv;
24827 ZV = ozv;
24828 current_buffer = obuf;
24829 }
24830
24831 set_cursor:
24832
24833 define_frame_cursor1 (f, cursor, pointer);
24834 }
24835
24836
24837 /* EXPORT for RIF:
24838 Clear any mouse-face on window W. This function is part of the
24839 redisplay interface, and is called from try_window_id and similar
24840 functions to ensure the mouse-highlight is off. */
24841
24842 void
24843 x_clear_window_mouse_face (w)
24844 struct window *w;
24845 {
24846 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24847 Lisp_Object window;
24848
24849 BLOCK_INPUT;
24850 XSETWINDOW (window, w);
24851 if (EQ (window, dpyinfo->mouse_face_window))
24852 clear_mouse_face (dpyinfo);
24853 UNBLOCK_INPUT;
24854 }
24855
24856
24857 /* EXPORT:
24858 Just discard the mouse face information for frame F, if any.
24859 This is used when the size of F is changed. */
24860
24861 void
24862 cancel_mouse_face (f)
24863 struct frame *f;
24864 {
24865 Lisp_Object window;
24866 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24867
24868 window = dpyinfo->mouse_face_window;
24869 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24870 {
24871 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24872 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24873 dpyinfo->mouse_face_window = Qnil;
24874 }
24875 }
24876
24877
24878 #endif /* HAVE_WINDOW_SYSTEM */
24879
24880 \f
24881 /***********************************************************************
24882 Exposure Events
24883 ***********************************************************************/
24884
24885 #ifdef HAVE_WINDOW_SYSTEM
24886
24887 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24888 which intersects rectangle R. R is in window-relative coordinates. */
24889
24890 static void
24891 expose_area (w, row, r, area)
24892 struct window *w;
24893 struct glyph_row *row;
24894 XRectangle *r;
24895 enum glyph_row_area area;
24896 {
24897 struct glyph *first = row->glyphs[area];
24898 struct glyph *end = row->glyphs[area] + row->used[area];
24899 struct glyph *last;
24900 int first_x, start_x, x;
24901
24902 if (area == TEXT_AREA && row->fill_line_p)
24903 /* If row extends face to end of line write the whole line. */
24904 draw_glyphs (w, 0, row, area,
24905 0, row->used[area],
24906 DRAW_NORMAL_TEXT, 0);
24907 else
24908 {
24909 /* Set START_X to the window-relative start position for drawing glyphs of
24910 AREA. The first glyph of the text area can be partially visible.
24911 The first glyphs of other areas cannot. */
24912 start_x = window_box_left_offset (w, area);
24913 x = start_x;
24914 if (area == TEXT_AREA)
24915 x += row->x;
24916
24917 /* Find the first glyph that must be redrawn. */
24918 while (first < end
24919 && x + first->pixel_width < r->x)
24920 {
24921 x += first->pixel_width;
24922 ++first;
24923 }
24924
24925 /* Find the last one. */
24926 last = first;
24927 first_x = x;
24928 while (last < end
24929 && x < r->x + r->width)
24930 {
24931 x += last->pixel_width;
24932 ++last;
24933 }
24934
24935 /* Repaint. */
24936 if (last > first)
24937 draw_glyphs (w, first_x - start_x, row, area,
24938 first - row->glyphs[area], last - row->glyphs[area],
24939 DRAW_NORMAL_TEXT, 0);
24940 }
24941 }
24942
24943
24944 /* Redraw the parts of the glyph row ROW on window W intersecting
24945 rectangle R. R is in window-relative coordinates. Value is
24946 non-zero if mouse-face was overwritten. */
24947
24948 static int
24949 expose_line (w, row, r)
24950 struct window *w;
24951 struct glyph_row *row;
24952 XRectangle *r;
24953 {
24954 xassert (row->enabled_p);
24955
24956 if (row->mode_line_p || w->pseudo_window_p)
24957 draw_glyphs (w, 0, row, TEXT_AREA,
24958 0, row->used[TEXT_AREA],
24959 DRAW_NORMAL_TEXT, 0);
24960 else
24961 {
24962 if (row->used[LEFT_MARGIN_AREA])
24963 expose_area (w, row, r, LEFT_MARGIN_AREA);
24964 if (row->used[TEXT_AREA])
24965 expose_area (w, row, r, TEXT_AREA);
24966 if (row->used[RIGHT_MARGIN_AREA])
24967 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24968 draw_row_fringe_bitmaps (w, row);
24969 }
24970
24971 return row->mouse_face_p;
24972 }
24973
24974
24975 /* Redraw those parts of glyphs rows during expose event handling that
24976 overlap other rows. Redrawing of an exposed line writes over parts
24977 of lines overlapping that exposed line; this function fixes that.
24978
24979 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24980 row in W's current matrix that is exposed and overlaps other rows.
24981 LAST_OVERLAPPING_ROW is the last such row. */
24982
24983 static void
24984 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24985 struct window *w;
24986 struct glyph_row *first_overlapping_row;
24987 struct glyph_row *last_overlapping_row;
24988 XRectangle *r;
24989 {
24990 struct glyph_row *row;
24991
24992 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24993 if (row->overlapping_p)
24994 {
24995 xassert (row->enabled_p && !row->mode_line_p);
24996
24997 row->clip = r;
24998 if (row->used[LEFT_MARGIN_AREA])
24999 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25000
25001 if (row->used[TEXT_AREA])
25002 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25003
25004 if (row->used[RIGHT_MARGIN_AREA])
25005 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25006 row->clip = NULL;
25007 }
25008 }
25009
25010
25011 /* Return non-zero if W's cursor intersects rectangle R. */
25012
25013 static int
25014 phys_cursor_in_rect_p (w, r)
25015 struct window *w;
25016 XRectangle *r;
25017 {
25018 XRectangle cr, result;
25019 struct glyph *cursor_glyph;
25020 struct glyph_row *row;
25021
25022 if (w->phys_cursor.vpos >= 0
25023 && w->phys_cursor.vpos < w->current_matrix->nrows
25024 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25025 row->enabled_p)
25026 && row->cursor_in_fringe_p)
25027 {
25028 /* Cursor is in the fringe. */
25029 cr.x = window_box_right_offset (w,
25030 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25031 ? RIGHT_MARGIN_AREA
25032 : TEXT_AREA));
25033 cr.y = row->y;
25034 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25035 cr.height = row->height;
25036 return x_intersect_rectangles (&cr, r, &result);
25037 }
25038
25039 cursor_glyph = get_phys_cursor_glyph (w);
25040 if (cursor_glyph)
25041 {
25042 /* r is relative to W's box, but w->phys_cursor.x is relative
25043 to left edge of W's TEXT area. Adjust it. */
25044 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25045 cr.y = w->phys_cursor.y;
25046 cr.width = cursor_glyph->pixel_width;
25047 cr.height = w->phys_cursor_height;
25048 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25049 I assume the effect is the same -- and this is portable. */
25050 return x_intersect_rectangles (&cr, r, &result);
25051 }
25052 /* If we don't understand the format, pretend we're not in the hot-spot. */
25053 return 0;
25054 }
25055
25056
25057 /* EXPORT:
25058 Draw a vertical window border to the right of window W if W doesn't
25059 have vertical scroll bars. */
25060
25061 void
25062 x_draw_vertical_border (w)
25063 struct window *w;
25064 {
25065 struct frame *f = XFRAME (WINDOW_FRAME (w));
25066
25067 /* We could do better, if we knew what type of scroll-bar the adjacent
25068 windows (on either side) have... But we don't :-(
25069 However, I think this works ok. ++KFS 2003-04-25 */
25070
25071 /* Redraw borders between horizontally adjacent windows. Don't
25072 do it for frames with vertical scroll bars because either the
25073 right scroll bar of a window, or the left scroll bar of its
25074 neighbor will suffice as a border. */
25075 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25076 return;
25077
25078 if (!WINDOW_RIGHTMOST_P (w)
25079 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25080 {
25081 int x0, x1, y0, y1;
25082
25083 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25084 y1 -= 1;
25085
25086 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25087 x1 -= 1;
25088
25089 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25090 }
25091 else if (!WINDOW_LEFTMOST_P (w)
25092 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25093 {
25094 int x0, x1, y0, y1;
25095
25096 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25097 y1 -= 1;
25098
25099 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25100 x0 -= 1;
25101
25102 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25103 }
25104 }
25105
25106
25107 /* Redraw the part of window W intersection rectangle FR. Pixel
25108 coordinates in FR are frame-relative. Call this function with
25109 input blocked. Value is non-zero if the exposure overwrites
25110 mouse-face. */
25111
25112 static int
25113 expose_window (w, fr)
25114 struct window *w;
25115 XRectangle *fr;
25116 {
25117 struct frame *f = XFRAME (w->frame);
25118 XRectangle wr, r;
25119 int mouse_face_overwritten_p = 0;
25120
25121 /* If window is not yet fully initialized, do nothing. This can
25122 happen when toolkit scroll bars are used and a window is split.
25123 Reconfiguring the scroll bar will generate an expose for a newly
25124 created window. */
25125 if (w->current_matrix == NULL)
25126 return 0;
25127
25128 /* When we're currently updating the window, display and current
25129 matrix usually don't agree. Arrange for a thorough display
25130 later. */
25131 if (w == updated_window)
25132 {
25133 SET_FRAME_GARBAGED (f);
25134 return 0;
25135 }
25136
25137 /* Frame-relative pixel rectangle of W. */
25138 wr.x = WINDOW_LEFT_EDGE_X (w);
25139 wr.y = WINDOW_TOP_EDGE_Y (w);
25140 wr.width = WINDOW_TOTAL_WIDTH (w);
25141 wr.height = WINDOW_TOTAL_HEIGHT (w);
25142
25143 if (x_intersect_rectangles (fr, &wr, &r))
25144 {
25145 int yb = window_text_bottom_y (w);
25146 struct glyph_row *row;
25147 int cursor_cleared_p;
25148 struct glyph_row *first_overlapping_row, *last_overlapping_row;
25149
25150 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
25151 r.x, r.y, r.width, r.height));
25152
25153 /* Convert to window coordinates. */
25154 r.x -= WINDOW_LEFT_EDGE_X (w);
25155 r.y -= WINDOW_TOP_EDGE_Y (w);
25156
25157 /* Turn off the cursor. */
25158 if (!w->pseudo_window_p
25159 && phys_cursor_in_rect_p (w, &r))
25160 {
25161 x_clear_cursor (w);
25162 cursor_cleared_p = 1;
25163 }
25164 else
25165 cursor_cleared_p = 0;
25166
25167 /* Update lines intersecting rectangle R. */
25168 first_overlapping_row = last_overlapping_row = NULL;
25169 for (row = w->current_matrix->rows;
25170 row->enabled_p;
25171 ++row)
25172 {
25173 int y0 = row->y;
25174 int y1 = MATRIX_ROW_BOTTOM_Y (row);
25175
25176 if ((y0 >= r.y && y0 < r.y + r.height)
25177 || (y1 > r.y && y1 < r.y + r.height)
25178 || (r.y >= y0 && r.y < y1)
25179 || (r.y + r.height > y0 && r.y + r.height < y1))
25180 {
25181 /* A header line may be overlapping, but there is no need
25182 to fix overlapping areas for them. KFS 2005-02-12 */
25183 if (row->overlapping_p && !row->mode_line_p)
25184 {
25185 if (first_overlapping_row == NULL)
25186 first_overlapping_row = row;
25187 last_overlapping_row = row;
25188 }
25189
25190 row->clip = fr;
25191 if (expose_line (w, row, &r))
25192 mouse_face_overwritten_p = 1;
25193 row->clip = NULL;
25194 }
25195 else if (row->overlapping_p)
25196 {
25197 /* We must redraw a row overlapping the exposed area. */
25198 if (y0 < r.y
25199 ? y0 + row->phys_height > r.y
25200 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
25201 {
25202 if (first_overlapping_row == NULL)
25203 first_overlapping_row = row;
25204 last_overlapping_row = row;
25205 }
25206 }
25207
25208 if (y1 >= yb)
25209 break;
25210 }
25211
25212 /* Display the mode line if there is one. */
25213 if (WINDOW_WANTS_MODELINE_P (w)
25214 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
25215 row->enabled_p)
25216 && row->y < r.y + r.height)
25217 {
25218 if (expose_line (w, row, &r))
25219 mouse_face_overwritten_p = 1;
25220 }
25221
25222 if (!w->pseudo_window_p)
25223 {
25224 /* Fix the display of overlapping rows. */
25225 if (first_overlapping_row)
25226 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
25227 fr);
25228
25229 /* Draw border between windows. */
25230 x_draw_vertical_border (w);
25231
25232 /* Turn the cursor on again. */
25233 if (cursor_cleared_p)
25234 update_window_cursor (w, 1);
25235 }
25236 }
25237
25238 return mouse_face_overwritten_p;
25239 }
25240
25241
25242
25243 /* Redraw (parts) of all windows in the window tree rooted at W that
25244 intersect R. R contains frame pixel coordinates. Value is
25245 non-zero if the exposure overwrites mouse-face. */
25246
25247 static int
25248 expose_window_tree (w, r)
25249 struct window *w;
25250 XRectangle *r;
25251 {
25252 struct frame *f = XFRAME (w->frame);
25253 int mouse_face_overwritten_p = 0;
25254
25255 while (w && !FRAME_GARBAGED_P (f))
25256 {
25257 if (!NILP (w->hchild))
25258 mouse_face_overwritten_p
25259 |= expose_window_tree (XWINDOW (w->hchild), r);
25260 else if (!NILP (w->vchild))
25261 mouse_face_overwritten_p
25262 |= expose_window_tree (XWINDOW (w->vchild), r);
25263 else
25264 mouse_face_overwritten_p |= expose_window (w, r);
25265
25266 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25267 }
25268
25269 return mouse_face_overwritten_p;
25270 }
25271
25272
25273 /* EXPORT:
25274 Redisplay an exposed area of frame F. X and Y are the upper-left
25275 corner of the exposed rectangle. W and H are width and height of
25276 the exposed area. All are pixel values. W or H zero means redraw
25277 the entire frame. */
25278
25279 void
25280 expose_frame (f, x, y, w, h)
25281 struct frame *f;
25282 int x, y, w, h;
25283 {
25284 XRectangle r;
25285 int mouse_face_overwritten_p = 0;
25286
25287 TRACE ((stderr, "expose_frame "));
25288
25289 /* No need to redraw if frame will be redrawn soon. */
25290 if (FRAME_GARBAGED_P (f))
25291 {
25292 TRACE ((stderr, " garbaged\n"));
25293 return;
25294 }
25295
25296 /* If basic faces haven't been realized yet, there is no point in
25297 trying to redraw anything. This can happen when we get an expose
25298 event while Emacs is starting, e.g. by moving another window. */
25299 if (FRAME_FACE_CACHE (f) == NULL
25300 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25301 {
25302 TRACE ((stderr, " no faces\n"));
25303 return;
25304 }
25305
25306 if (w == 0 || h == 0)
25307 {
25308 r.x = r.y = 0;
25309 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25310 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25311 }
25312 else
25313 {
25314 r.x = x;
25315 r.y = y;
25316 r.width = w;
25317 r.height = h;
25318 }
25319
25320 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25321 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25322
25323 if (WINDOWP (f->tool_bar_window))
25324 mouse_face_overwritten_p
25325 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25326
25327 #ifdef HAVE_X_WINDOWS
25328 #ifndef MSDOS
25329 #ifndef USE_X_TOOLKIT
25330 if (WINDOWP (f->menu_bar_window))
25331 mouse_face_overwritten_p
25332 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25333 #endif /* not USE_X_TOOLKIT */
25334 #endif
25335 #endif
25336
25337 /* Some window managers support a focus-follows-mouse style with
25338 delayed raising of frames. Imagine a partially obscured frame,
25339 and moving the mouse into partially obscured mouse-face on that
25340 frame. The visible part of the mouse-face will be highlighted,
25341 then the WM raises the obscured frame. With at least one WM, KDE
25342 2.1, Emacs is not getting any event for the raising of the frame
25343 (even tried with SubstructureRedirectMask), only Expose events.
25344 These expose events will draw text normally, i.e. not
25345 highlighted. Which means we must redo the highlight here.
25346 Subsume it under ``we love X''. --gerd 2001-08-15 */
25347 /* Included in Windows version because Windows most likely does not
25348 do the right thing if any third party tool offers
25349 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25350 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25351 {
25352 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25353 if (f == dpyinfo->mouse_face_mouse_frame)
25354 {
25355 int x = dpyinfo->mouse_face_mouse_x;
25356 int y = dpyinfo->mouse_face_mouse_y;
25357 clear_mouse_face (dpyinfo);
25358 note_mouse_highlight (f, x, y);
25359 }
25360 }
25361 }
25362
25363
25364 /* EXPORT:
25365 Determine the intersection of two rectangles R1 and R2. Return
25366 the intersection in *RESULT. Value is non-zero if RESULT is not
25367 empty. */
25368
25369 int
25370 x_intersect_rectangles (r1, r2, result)
25371 XRectangle *r1, *r2, *result;
25372 {
25373 XRectangle *left, *right;
25374 XRectangle *upper, *lower;
25375 int intersection_p = 0;
25376
25377 /* Rearrange so that R1 is the left-most rectangle. */
25378 if (r1->x < r2->x)
25379 left = r1, right = r2;
25380 else
25381 left = r2, right = r1;
25382
25383 /* X0 of the intersection is right.x0, if this is inside R1,
25384 otherwise there is no intersection. */
25385 if (right->x <= left->x + left->width)
25386 {
25387 result->x = right->x;
25388
25389 /* The right end of the intersection is the minimum of the
25390 the right ends of left and right. */
25391 result->width = (min (left->x + left->width, right->x + right->width)
25392 - result->x);
25393
25394 /* Same game for Y. */
25395 if (r1->y < r2->y)
25396 upper = r1, lower = r2;
25397 else
25398 upper = r2, lower = r1;
25399
25400 /* The upper end of the intersection is lower.y0, if this is inside
25401 of upper. Otherwise, there is no intersection. */
25402 if (lower->y <= upper->y + upper->height)
25403 {
25404 result->y = lower->y;
25405
25406 /* The lower end of the intersection is the minimum of the lower
25407 ends of upper and lower. */
25408 result->height = (min (lower->y + lower->height,
25409 upper->y + upper->height)
25410 - result->y);
25411 intersection_p = 1;
25412 }
25413 }
25414
25415 return intersection_p;
25416 }
25417
25418 #endif /* HAVE_WINDOW_SYSTEM */
25419
25420 \f
25421 /***********************************************************************
25422 Initialization
25423 ***********************************************************************/
25424
25425 void
25426 syms_of_xdisp ()
25427 {
25428 Vwith_echo_area_save_vector = Qnil;
25429 staticpro (&Vwith_echo_area_save_vector);
25430
25431 Vmessage_stack = Qnil;
25432 staticpro (&Vmessage_stack);
25433
25434 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25435 staticpro (&Qinhibit_redisplay);
25436
25437 message_dolog_marker1 = Fmake_marker ();
25438 staticpro (&message_dolog_marker1);
25439 message_dolog_marker2 = Fmake_marker ();
25440 staticpro (&message_dolog_marker2);
25441 message_dolog_marker3 = Fmake_marker ();
25442 staticpro (&message_dolog_marker3);
25443
25444 #if GLYPH_DEBUG
25445 defsubr (&Sdump_frame_glyph_matrix);
25446 defsubr (&Sdump_glyph_matrix);
25447 defsubr (&Sdump_glyph_row);
25448 defsubr (&Sdump_tool_bar_row);
25449 defsubr (&Strace_redisplay);
25450 defsubr (&Strace_to_stderr);
25451 #endif
25452 #ifdef HAVE_WINDOW_SYSTEM
25453 defsubr (&Stool_bar_lines_needed);
25454 defsubr (&Slookup_image_map);
25455 #endif
25456 defsubr (&Sformat_mode_line);
25457 defsubr (&Sinvisible_p);
25458
25459 staticpro (&Qmenu_bar_update_hook);
25460 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25461
25462 staticpro (&Qoverriding_terminal_local_map);
25463 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25464
25465 staticpro (&Qoverriding_local_map);
25466 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25467
25468 staticpro (&Qwindow_scroll_functions);
25469 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25470
25471 staticpro (&Qwindow_text_change_functions);
25472 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25473
25474 staticpro (&Qredisplay_end_trigger_functions);
25475 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25476
25477 staticpro (&Qinhibit_point_motion_hooks);
25478 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25479
25480 Qeval = intern_c_string ("eval");
25481 staticpro (&Qeval);
25482
25483 QCdata = intern_c_string (":data");
25484 staticpro (&QCdata);
25485 Qdisplay = intern_c_string ("display");
25486 staticpro (&Qdisplay);
25487 Qspace_width = intern_c_string ("space-width");
25488 staticpro (&Qspace_width);
25489 Qraise = intern_c_string ("raise");
25490 staticpro (&Qraise);
25491 Qslice = intern_c_string ("slice");
25492 staticpro (&Qslice);
25493 Qspace = intern_c_string ("space");
25494 staticpro (&Qspace);
25495 Qmargin = intern_c_string ("margin");
25496 staticpro (&Qmargin);
25497 Qpointer = intern_c_string ("pointer");
25498 staticpro (&Qpointer);
25499 Qleft_margin = intern_c_string ("left-margin");
25500 staticpro (&Qleft_margin);
25501 Qright_margin = intern_c_string ("right-margin");
25502 staticpro (&Qright_margin);
25503 Qcenter = intern_c_string ("center");
25504 staticpro (&Qcenter);
25505 Qline_height = intern_c_string ("line-height");
25506 staticpro (&Qline_height);
25507 QCalign_to = intern_c_string (":align-to");
25508 staticpro (&QCalign_to);
25509 QCrelative_width = intern_c_string (":relative-width");
25510 staticpro (&QCrelative_width);
25511 QCrelative_height = intern_c_string (":relative-height");
25512 staticpro (&QCrelative_height);
25513 QCeval = intern_c_string (":eval");
25514 staticpro (&QCeval);
25515 QCpropertize = intern_c_string (":propertize");
25516 staticpro (&QCpropertize);
25517 QCfile = intern_c_string (":file");
25518 staticpro (&QCfile);
25519 Qfontified = intern_c_string ("fontified");
25520 staticpro (&Qfontified);
25521 Qfontification_functions = intern_c_string ("fontification-functions");
25522 staticpro (&Qfontification_functions);
25523 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25524 staticpro (&Qtrailing_whitespace);
25525 Qescape_glyph = intern_c_string ("escape-glyph");
25526 staticpro (&Qescape_glyph);
25527 Qnobreak_space = intern_c_string ("nobreak-space");
25528 staticpro (&Qnobreak_space);
25529 Qimage = intern_c_string ("image");
25530 staticpro (&Qimage);
25531 QCmap = intern_c_string (":map");
25532 staticpro (&QCmap);
25533 QCpointer = intern_c_string (":pointer");
25534 staticpro (&QCpointer);
25535 Qrect = intern_c_string ("rect");
25536 staticpro (&Qrect);
25537 Qcircle = intern_c_string ("circle");
25538 staticpro (&Qcircle);
25539 Qpoly = intern_c_string ("poly");
25540 staticpro (&Qpoly);
25541 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25542 staticpro (&Qmessage_truncate_lines);
25543 Qgrow_only = intern_c_string ("grow-only");
25544 staticpro (&Qgrow_only);
25545 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25546 staticpro (&Qinhibit_menubar_update);
25547 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25548 staticpro (&Qinhibit_eval_during_redisplay);
25549 Qposition = intern_c_string ("position");
25550 staticpro (&Qposition);
25551 Qbuffer_position = intern_c_string ("buffer-position");
25552 staticpro (&Qbuffer_position);
25553 Qobject = intern_c_string ("object");
25554 staticpro (&Qobject);
25555 Qbar = intern_c_string ("bar");
25556 staticpro (&Qbar);
25557 Qhbar = intern_c_string ("hbar");
25558 staticpro (&Qhbar);
25559 Qbox = intern_c_string ("box");
25560 staticpro (&Qbox);
25561 Qhollow = intern_c_string ("hollow");
25562 staticpro (&Qhollow);
25563 Qhand = intern_c_string ("hand");
25564 staticpro (&Qhand);
25565 Qarrow = intern_c_string ("arrow");
25566 staticpro (&Qarrow);
25567 Qtext = intern_c_string ("text");
25568 staticpro (&Qtext);
25569 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25570 staticpro (&Qrisky_local_variable);
25571 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25572 staticpro (&Qinhibit_free_realized_faces);
25573
25574 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25575 Fcons (intern_c_string ("void-variable"), Qnil)),
25576 Qnil);
25577 staticpro (&list_of_error);
25578
25579 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25580 staticpro (&Qlast_arrow_position);
25581 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25582 staticpro (&Qlast_arrow_string);
25583
25584 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25585 staticpro (&Qoverlay_arrow_string);
25586 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25587 staticpro (&Qoverlay_arrow_bitmap);
25588
25589 echo_buffer[0] = echo_buffer[1] = Qnil;
25590 staticpro (&echo_buffer[0]);
25591 staticpro (&echo_buffer[1]);
25592
25593 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25594 staticpro (&echo_area_buffer[0]);
25595 staticpro (&echo_area_buffer[1]);
25596
25597 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25598 staticpro (&Vmessages_buffer_name);
25599
25600 mode_line_proptrans_alist = Qnil;
25601 staticpro (&mode_line_proptrans_alist);
25602 mode_line_string_list = Qnil;
25603 staticpro (&mode_line_string_list);
25604 mode_line_string_face = Qnil;
25605 staticpro (&mode_line_string_face);
25606 mode_line_string_face_prop = Qnil;
25607 staticpro (&mode_line_string_face_prop);
25608 Vmode_line_unwind_vector = Qnil;
25609 staticpro (&Vmode_line_unwind_vector);
25610
25611 help_echo_string = Qnil;
25612 staticpro (&help_echo_string);
25613 help_echo_object = Qnil;
25614 staticpro (&help_echo_object);
25615 help_echo_window = Qnil;
25616 staticpro (&help_echo_window);
25617 previous_help_echo_string = Qnil;
25618 staticpro (&previous_help_echo_string);
25619 help_echo_pos = -1;
25620
25621 Qright_to_left = intern_c_string ("right-to-left");
25622 staticpro (&Qright_to_left);
25623 Qleft_to_right = intern_c_string ("left-to-right");
25624 staticpro (&Qleft_to_right);
25625
25626 #ifdef HAVE_WINDOW_SYSTEM
25627 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25628 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25629 For example, if a block cursor is over a tab, it will be drawn as
25630 wide as that tab on the display. */);
25631 x_stretch_cursor_p = 0;
25632 #endif
25633
25634 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25635 doc: /* *Non-nil means highlight trailing whitespace.
25636 The face used for trailing whitespace is `trailing-whitespace'. */);
25637 Vshow_trailing_whitespace = Qnil;
25638
25639 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25640 doc: /* *Control highlighting of nobreak space and soft hyphen.
25641 A value of t means highlight the character itself (for nobreak space,
25642 use face `nobreak-space').
25643 A value of nil means no highlighting.
25644 Other values mean display the escape glyph followed by an ordinary
25645 space or ordinary hyphen. */);
25646 Vnobreak_char_display = Qt;
25647
25648 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25649 doc: /* *The pointer shape to show in void text areas.
25650 A value of nil means to show the text pointer. Other options are `arrow',
25651 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25652 Vvoid_text_area_pointer = Qarrow;
25653
25654 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25655 doc: /* Non-nil means don't actually do any redisplay.
25656 This is used for internal purposes. */);
25657 Vinhibit_redisplay = Qnil;
25658
25659 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25660 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25661 Vglobal_mode_string = Qnil;
25662
25663 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25664 doc: /* Marker for where to display an arrow on top of the buffer text.
25665 This must be the beginning of a line in order to work.
25666 See also `overlay-arrow-string'. */);
25667 Voverlay_arrow_position = Qnil;
25668
25669 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25670 doc: /* String to display as an arrow in non-window frames.
25671 See also `overlay-arrow-position'. */);
25672 Voverlay_arrow_string = make_pure_c_string ("=>");
25673
25674 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25675 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25676 The symbols on this list are examined during redisplay to determine
25677 where to display overlay arrows. */);
25678 Voverlay_arrow_variable_list
25679 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25680
25681 DEFVAR_INT ("scroll-step", &scroll_step,
25682 doc: /* *The number of lines to try scrolling a window by when point moves out.
25683 If that fails to bring point back on frame, point is centered instead.
25684 If this is zero, point is always centered after it moves off frame.
25685 If you want scrolling to always be a line at a time, you should set
25686 `scroll-conservatively' to a large value rather than set this to 1. */);
25687
25688 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25689 doc: /* *Scroll up to this many lines, to bring point back on screen.
25690 If point moves off-screen, redisplay will scroll by up to
25691 `scroll-conservatively' lines in order to bring point just barely
25692 onto the screen again. If that cannot be done, then redisplay
25693 recenters point as usual.
25694
25695 A value of zero means always recenter point if it moves off screen. */);
25696 scroll_conservatively = 0;
25697
25698 DEFVAR_INT ("scroll-margin", &scroll_margin,
25699 doc: /* *Number of lines of margin at the top and bottom of a window.
25700 Recenter the window whenever point gets within this many lines
25701 of the top or bottom of the window. */);
25702 scroll_margin = 0;
25703
25704 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25705 doc: /* Pixels per inch value for non-window system displays.
25706 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25707 Vdisplay_pixels_per_inch = make_float (72.0);
25708
25709 #if GLYPH_DEBUG
25710 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25711 #endif
25712
25713 DEFVAR_LISP ("truncate-partial-width-windows",
25714 &Vtruncate_partial_width_windows,
25715 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25716 For an integer value, truncate lines in each window narrower than the
25717 full frame width, provided the window width is less than that integer;
25718 otherwise, respect the value of `truncate-lines'.
25719
25720 For any other non-nil value, truncate lines in all windows that do
25721 not span the full frame width.
25722
25723 A value of nil means to respect the value of `truncate-lines'.
25724
25725 If `word-wrap' is enabled, you might want to reduce this. */);
25726 Vtruncate_partial_width_windows = make_number (50);
25727
25728 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25729 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25730 Any other value means to use the appropriate face, `mode-line',
25731 `header-line', or `menu' respectively. */);
25732 mode_line_inverse_video = 1;
25733
25734 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25735 doc: /* *Maximum buffer size for which line number should be displayed.
25736 If the buffer is bigger than this, the line number does not appear
25737 in the mode line. A value of nil means no limit. */);
25738 Vline_number_display_limit = Qnil;
25739
25740 DEFVAR_INT ("line-number-display-limit-width",
25741 &line_number_display_limit_width,
25742 doc: /* *Maximum line width (in characters) for line number display.
25743 If the average length of the lines near point is bigger than this, then the
25744 line number may be omitted from the mode line. */);
25745 line_number_display_limit_width = 200;
25746
25747 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25748 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25749 highlight_nonselected_windows = 0;
25750
25751 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25752 doc: /* Non-nil if more than one frame is visible on this display.
25753 Minibuffer-only frames don't count, but iconified frames do.
25754 This variable is not guaranteed to be accurate except while processing
25755 `frame-title-format' and `icon-title-format'. */);
25756
25757 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25758 doc: /* Template for displaying the title bar of visible frames.
25759 \(Assuming the window manager supports this feature.)
25760
25761 This variable has the same structure as `mode-line-format', except that
25762 the %c and %l constructs are ignored. It is used only on frames for
25763 which no explicit name has been set \(see `modify-frame-parameters'). */);
25764
25765 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25766 doc: /* Template for displaying the title bar of an iconified frame.
25767 \(Assuming the window manager supports this feature.)
25768 This variable has the same structure as `mode-line-format' (which see),
25769 and is used only on frames for which no explicit name has been set
25770 \(see `modify-frame-parameters'). */);
25771 Vicon_title_format
25772 = Vframe_title_format
25773 = pure_cons (intern_c_string ("multiple-frames"),
25774 pure_cons (make_pure_c_string ("%b"),
25775 pure_cons (pure_cons (empty_unibyte_string,
25776 pure_cons (intern_c_string ("invocation-name"),
25777 pure_cons (make_pure_c_string ("@"),
25778 pure_cons (intern_c_string ("system-name"),
25779 Qnil)))),
25780 Qnil)));
25781
25782 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25783 doc: /* Maximum number of lines to keep in the message log buffer.
25784 If nil, disable message logging. If t, log messages but don't truncate
25785 the buffer when it becomes large. */);
25786 Vmessage_log_max = make_number (100);
25787
25788 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25789 doc: /* Functions called before redisplay, if window sizes have changed.
25790 The value should be a list of functions that take one argument.
25791 Just before redisplay, for each frame, if any of its windows have changed
25792 size since the last redisplay, or have been split or deleted,
25793 all the functions in the list are called, with the frame as argument. */);
25794 Vwindow_size_change_functions = Qnil;
25795
25796 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25797 doc: /* List of functions to call before redisplaying a window with scrolling.
25798 Each function is called with two arguments, the window and its new
25799 display-start position. Note that these functions are also called by
25800 `set-window-buffer'. Also note that the value of `window-end' is not
25801 valid when these functions are called. */);
25802 Vwindow_scroll_functions = Qnil;
25803
25804 DEFVAR_LISP ("window-text-change-functions",
25805 &Vwindow_text_change_functions,
25806 doc: /* Functions to call in redisplay when text in the window might change. */);
25807 Vwindow_text_change_functions = Qnil;
25808
25809 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25810 doc: /* Functions called when redisplay of a window reaches the end trigger.
25811 Each function is called with two arguments, the window and the end trigger value.
25812 See `set-window-redisplay-end-trigger'. */);
25813 Vredisplay_end_trigger_functions = Qnil;
25814
25815 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25816 doc: /* *Non-nil means autoselect window with mouse pointer.
25817 If nil, do not autoselect windows.
25818 A positive number means delay autoselection by that many seconds: a
25819 window is autoselected only after the mouse has remained in that
25820 window for the duration of the delay.
25821 A negative number has a similar effect, but causes windows to be
25822 autoselected only after the mouse has stopped moving. \(Because of
25823 the way Emacs compares mouse events, you will occasionally wait twice
25824 that time before the window gets selected.\)
25825 Any other value means to autoselect window instantaneously when the
25826 mouse pointer enters it.
25827
25828 Autoselection selects the minibuffer only if it is active, and never
25829 unselects the minibuffer if it is active.
25830
25831 When customizing this variable make sure that the actual value of
25832 `focus-follows-mouse' matches the behavior of your window manager. */);
25833 Vmouse_autoselect_window = Qnil;
25834
25835 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25836 doc: /* *Non-nil means automatically resize tool-bars.
25837 This dynamically changes the tool-bar's height to the minimum height
25838 that is needed to make all tool-bar items visible.
25839 If value is `grow-only', the tool-bar's height is only increased
25840 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25841 Vauto_resize_tool_bars = Qt;
25842
25843 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25844 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25845 auto_raise_tool_bar_buttons_p = 1;
25846
25847 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25848 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25849 make_cursor_line_fully_visible_p = 1;
25850
25851 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25852 doc: /* *Border below tool-bar in pixels.
25853 If an integer, use it as the height of the border.
25854 If it is one of `internal-border-width' or `border-width', use the
25855 value of the corresponding frame parameter.
25856 Otherwise, no border is added below the tool-bar. */);
25857 Vtool_bar_border = Qinternal_border_width;
25858
25859 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25860 doc: /* *Margin around tool-bar buttons in pixels.
25861 If an integer, use that for both horizontal and vertical margins.
25862 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25863 HORZ specifying the horizontal margin, and VERT specifying the
25864 vertical margin. */);
25865 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25866
25867 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25868 doc: /* *Relief thickness of tool-bar buttons. */);
25869 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25870
25871 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25872 doc: /* List of functions to call to fontify regions of text.
25873 Each function is called with one argument POS. Functions must
25874 fontify a region starting at POS in the current buffer, and give
25875 fontified regions the property `fontified'. */);
25876 Vfontification_functions = Qnil;
25877 Fmake_variable_buffer_local (Qfontification_functions);
25878
25879 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25880 &unibyte_display_via_language_environment,
25881 doc: /* *Non-nil means display unibyte text according to language environment.
25882 Specifically, this means that raw bytes in the range 160-255 decimal
25883 are displayed by converting them to the equivalent multibyte characters
25884 according to the current language environment. As a result, they are
25885 displayed according to the current fontset.
25886
25887 Note that this variable affects only how these bytes are displayed,
25888 but does not change the fact they are interpreted as raw bytes. */);
25889 unibyte_display_via_language_environment = 0;
25890
25891 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25892 doc: /* *Maximum height for resizing mini-windows.
25893 If a float, it specifies a fraction of the mini-window frame's height.
25894 If an integer, it specifies a number of lines. */);
25895 Vmax_mini_window_height = make_float (0.25);
25896
25897 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25898 doc: /* *How to resize mini-windows.
25899 A value of nil means don't automatically resize mini-windows.
25900 A value of t means resize them to fit the text displayed in them.
25901 A value of `grow-only', the default, means let mini-windows grow
25902 only, until their display becomes empty, at which point the windows
25903 go back to their normal size. */);
25904 Vresize_mini_windows = Qgrow_only;
25905
25906 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25907 doc: /* Alist specifying how to blink the cursor off.
25908 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25909 `cursor-type' frame-parameter or variable equals ON-STATE,
25910 comparing using `equal', Emacs uses OFF-STATE to specify
25911 how to blink it off. ON-STATE and OFF-STATE are values for
25912 the `cursor-type' frame parameter.
25913
25914 If a frame's ON-STATE has no entry in this list,
25915 the frame's other specifications determine how to blink the cursor off. */);
25916 Vblink_cursor_alist = Qnil;
25917
25918 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25919 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25920 automatic_hscrolling_p = 1;
25921 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25922 staticpro (&Qauto_hscroll_mode);
25923
25924 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25925 doc: /* *How many columns away from the window edge point is allowed to get
25926 before automatic hscrolling will horizontally scroll the window. */);
25927 hscroll_margin = 5;
25928
25929 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25930 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25931 When point is less than `hscroll-margin' columns from the window
25932 edge, automatic hscrolling will scroll the window by the amount of columns
25933 determined by this variable. If its value is a positive integer, scroll that
25934 many columns. If it's a positive floating-point number, it specifies the
25935 fraction of the window's width to scroll. If it's nil or zero, point will be
25936 centered horizontally after the scroll. Any other value, including negative
25937 numbers, are treated as if the value were zero.
25938
25939 Automatic hscrolling always moves point outside the scroll margin, so if
25940 point was more than scroll step columns inside the margin, the window will
25941 scroll more than the value given by the scroll step.
25942
25943 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25944 and `scroll-right' overrides this variable's effect. */);
25945 Vhscroll_step = make_number (0);
25946
25947 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25948 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25949 Bind this around calls to `message' to let it take effect. */);
25950 message_truncate_lines = 0;
25951
25952 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25953 doc: /* Normal hook run to update the menu bar definitions.
25954 Redisplay runs this hook before it redisplays the menu bar.
25955 This is used to update submenus such as Buffers,
25956 whose contents depend on various data. */);
25957 Vmenu_bar_update_hook = Qnil;
25958
25959 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25960 doc: /* Frame for which we are updating a menu.
25961 The enable predicate for a menu binding should check this variable. */);
25962 Vmenu_updating_frame = Qnil;
25963
25964 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25965 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25966 inhibit_menubar_update = 0;
25967
25968 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25969 doc: /* Prefix prepended to all continuation lines at display time.
25970 The value may be a string, an image, or a stretch-glyph; it is
25971 interpreted in the same way as the value of a `display' text property.
25972
25973 This variable is overridden by any `wrap-prefix' text or overlay
25974 property.
25975
25976 To add a prefix to non-continuation lines, use `line-prefix'. */);
25977 Vwrap_prefix = Qnil;
25978 staticpro (&Qwrap_prefix);
25979 Qwrap_prefix = intern_c_string ("wrap-prefix");
25980 Fmake_variable_buffer_local (Qwrap_prefix);
25981
25982 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25983 doc: /* Prefix prepended to all non-continuation lines at display time.
25984 The value may be a string, an image, or a stretch-glyph; it is
25985 interpreted in the same way as the value of a `display' text property.
25986
25987 This variable is overridden by any `line-prefix' text or overlay
25988 property.
25989
25990 To add a prefix to continuation lines, use `wrap-prefix'. */);
25991 Vline_prefix = Qnil;
25992 staticpro (&Qline_prefix);
25993 Qline_prefix = intern_c_string ("line-prefix");
25994 Fmake_variable_buffer_local (Qline_prefix);
25995
25996 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25997 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25998 inhibit_eval_during_redisplay = 0;
25999
26000 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
26001 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26002 inhibit_free_realized_faces = 0;
26003
26004 #if GLYPH_DEBUG
26005 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
26006 doc: /* Inhibit try_window_id display optimization. */);
26007 inhibit_try_window_id = 0;
26008
26009 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
26010 doc: /* Inhibit try_window_reusing display optimization. */);
26011 inhibit_try_window_reusing = 0;
26012
26013 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
26014 doc: /* Inhibit try_cursor_movement display optimization. */);
26015 inhibit_try_cursor_movement = 0;
26016 #endif /* GLYPH_DEBUG */
26017
26018 DEFVAR_INT ("overline-margin", &overline_margin,
26019 doc: /* *Space between overline and text, in pixels.
26020 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26021 margin to the caracter height. */);
26022 overline_margin = 2;
26023
26024 DEFVAR_INT ("underline-minimum-offset",
26025 &underline_minimum_offset,
26026 doc: /* Minimum distance between baseline and underline.
26027 This can improve legibility of underlined text at small font sizes,
26028 particularly when using variable `x-use-underline-position-properties'
26029 with fonts that specify an UNDERLINE_POSITION relatively close to the
26030 baseline. The default value is 1. */);
26031 underline_minimum_offset = 1;
26032
26033 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
26034 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
26035 display_hourglass_p = 1;
26036
26037 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
26038 doc: /* *Seconds to wait before displaying an hourglass pointer.
26039 Value must be an integer or float. */);
26040 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26041
26042 hourglass_atimer = NULL;
26043 hourglass_shown_p = 0;
26044 }
26045
26046
26047 /* Initialize this module when Emacs starts. */
26048
26049 void
26050 init_xdisp ()
26051 {
26052 Lisp_Object root_window;
26053 struct window *mini_w;
26054
26055 current_header_line_height = current_mode_line_height = -1;
26056
26057 CHARPOS (this_line_start_pos) = 0;
26058
26059 mini_w = XWINDOW (minibuf_window);
26060 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26061
26062 if (!noninteractive)
26063 {
26064 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26065 int i;
26066
26067 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26068 set_window_height (root_window,
26069 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
26070 0);
26071 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
26072 set_window_height (minibuf_window, 1, 0);
26073
26074 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
26075 mini_w->total_cols = make_number (FRAME_COLS (f));
26076
26077 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
26078 scratch_glyph_row.glyphs[TEXT_AREA + 1]
26079 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
26080
26081 /* The default ellipsis glyphs `...'. */
26082 for (i = 0; i < 3; ++i)
26083 default_invis_vector[i] = make_number ('.');
26084 }
26085
26086 {
26087 /* Allocate the buffer for frame titles.
26088 Also used for `format-mode-line'. */
26089 int size = 100;
26090 mode_line_noprop_buf = (char *) xmalloc (size);
26091 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
26092 mode_line_noprop_ptr = mode_line_noprop_buf;
26093 mode_line_target = MODE_LINE_DISPLAY;
26094 }
26095
26096 help_echo_showing_p = 0;
26097 }
26098
26099 /* Since w32 does not support atimers, it defines its own implementation of
26100 the following three functions in w32fns.c. */
26101 #ifndef WINDOWSNT
26102
26103 /* Platform-independent portion of hourglass implementation. */
26104
26105 /* Return non-zero if houglass timer has been started or hourglass is shown. */
26106 int
26107 hourglass_started ()
26108 {
26109 return hourglass_shown_p || hourglass_atimer != NULL;
26110 }
26111
26112 /* Cancel a currently active hourglass timer, and start a new one. */
26113 void
26114 start_hourglass ()
26115 {
26116 #if defined (HAVE_WINDOW_SYSTEM)
26117 EMACS_TIME delay;
26118 int secs, usecs = 0;
26119
26120 cancel_hourglass ();
26121
26122 if (INTEGERP (Vhourglass_delay)
26123 && XINT (Vhourglass_delay) > 0)
26124 secs = XFASTINT (Vhourglass_delay);
26125 else if (FLOATP (Vhourglass_delay)
26126 && XFLOAT_DATA (Vhourglass_delay) > 0)
26127 {
26128 Lisp_Object tem;
26129 tem = Ftruncate (Vhourglass_delay, Qnil);
26130 secs = XFASTINT (tem);
26131 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
26132 }
26133 else
26134 secs = DEFAULT_HOURGLASS_DELAY;
26135
26136 EMACS_SET_SECS_USECS (delay, secs, usecs);
26137 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
26138 show_hourglass, NULL);
26139 #endif
26140 }
26141
26142
26143 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
26144 shown. */
26145 void
26146 cancel_hourglass ()
26147 {
26148 #if defined (HAVE_WINDOW_SYSTEM)
26149 if (hourglass_atimer)
26150 {
26151 cancel_atimer (hourglass_atimer);
26152 hourglass_atimer = NULL;
26153 }
26154
26155 if (hourglass_shown_p)
26156 hide_hourglass ();
26157 #endif
26158 }
26159 #endif /* ! WINDOWSNT */
26160
26161 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
26162 (do not change this comment) */