b3aaa52e2e5ba078d5d734a46287990696b0c14a
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 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.
36
37 The following diagram shows how redisplay code is invoked. As you
38 can see, Lisp calls redisplay and vice versa. Under window systems
39 like X, some portions of the redisplay code are also called
40 asynchronously during mouse movement or expose events. It is very
41 important that these code parts do NOT use the C library (malloc,
42 free) because many C libraries under Unix are not reentrant. They
43 may also NOT call functions of the Lisp interpreter which could
44 change the interpreter's state. If you don't follow these rules,
45 you will encounter bugs which are very hard to explain.
46
47 +--------------+ redisplay +----------------+
48 | Lisp machine |---------------->| Redisplay code |<--+
49 +--------------+ (xdisp.c) +----------------+ |
50 ^ | |
51 +----------------------------------+ |
52 Don't use this path when called |
53 asynchronously! |
54 |
55 expose_window (asynchronous) |
56 |
57 X expose events -----+
58
59 What does redisplay do? Obviously, it has to figure out somehow what
60 has been changed since the last time the display has been updated,
61 and to make these changes visible. Preferably it would do that in
62 a moderately intelligent way, i.e. fast.
63
64 Changes in buffer text can be deduced from window and buffer
65 structures, and from some global variables like `beg_unchanged' and
66 `end_unchanged'. The contents of the display are additionally
67 recorded in a `glyph matrix', a two-dimensional matrix of glyph
68 structures. Each row in such a matrix corresponds to a line on the
69 display, and each glyph in a row corresponds to a column displaying
70 a character, an image, or what else. This matrix is called the
71 `current glyph matrix' or `current matrix' in redisplay
72 terminology.
73
74 For buffer parts that have been changed since the last update, a
75 second glyph matrix is constructed, the so called `desired glyph
76 matrix' or short `desired matrix'. Current and desired matrix are
77 then compared to find a cheap way to update the display, e.g. by
78 reusing part of the display by scrolling lines.
79
80 You will find a lot of redisplay optimizations when you start
81 looking at the innards of redisplay. The overall goal of all these
82 optimizations is to make redisplay fast because it is done
83 frequently.
84
85 Desired matrices.
86
87 Desired matrices are always built per Emacs window. The function
88 `display_line' is the central function to look at if you are
89 interested. It constructs one row in a desired matrix given an
90 iterator structure containing both a buffer position and a
91 description of the environment in which the text is to be
92 displayed. But this is too early, read on.
93
94 Characters and pixmaps displayed for a range of buffer text depend
95 on various settings of buffers and windows, on overlays and text
96 properties, on display tables, on selective display. The good news
97 is that all this hairy stuff is hidden behind a small set of
98 interface functions taking an iterator structure (struct it)
99 argument.
100
101 Iteration over things to be displayed is then simple. It is
102 started by initializing an iterator with a call to init_iterator.
103 Calls to get_next_display_element fill the iterator structure with
104 relevant information about the next thing to display. Calls to
105 set_iterator_to_next move the iterator to the next thing.
106
107 Besides this, an iterator also contains information about the
108 display environment in which glyphs for display elements are to be
109 produced. It has fields for the width and height of the display,
110 the information whether long lines are truncated or continued, a
111 current X and Y position, and lots of other stuff you can better
112 see in dispextern.h.
113
114 Glyphs in a desired matrix are normally constructed in a loop
115 calling get_next_display_element and then produce_glyphs. The call
116 to produce_glyphs will fill the iterator structure with pixel
117 information about the element being displayed and at the same time
118 produce glyphs for it. If the display element fits on the line
119 being displayed, set_iterator_to_next is called next, otherwise the
120 glyphs produced are discarded.
121
122
123 Frame matrices.
124
125 That just couldn't be all, could it? What about terminal types not
126 supporting operations on sub-windows of the screen? To update the
127 display on such a terminal, window-based glyph matrices are not
128 well suited. To be able to reuse part of the display (scrolling
129 lines up and down), we must instead have a view of the whole
130 screen. This is what `frame matrices' are for. They are a trick.
131
132 Frames on terminals like above have a glyph pool. Windows on such
133 a frame sub-allocate their glyph memory from their frame's glyph
134 pool. The frame itself is given its own glyph matrices. By
135 coincidence---or maybe something else---rows in window glyph
136 matrices are slices of corresponding rows in frame matrices. Thus
137 writing to window matrices implicitly updates a frame matrix which
138 provides us with the view of the whole screen that we originally
139 wanted to have without having to move many bytes around. To be
140 honest, there is a little bit more done, but not much more. If you
141 plan to extend that code, take a look at dispnew.c. The function
142 build_frame_matrix is a good starting point. */
143
144 #include <config.h>
145 #include <stdio.h>
146 #include <limits.h>
147 #include <setjmp.h>
148
149 #include "lisp.h"
150 #include "keyboard.h"
151 #include "frame.h"
152 #include "window.h"
153 #include "termchar.h"
154 #include "dispextern.h"
155 #include "buffer.h"
156 #include "character.h"
157 #include "charset.h"
158 #include "indent.h"
159 #include "commands.h"
160 #include "keymap.h"
161 #include "macros.h"
162 #include "disptab.h"
163 #include "termhooks.h"
164 #include "intervals.h"
165 #include "coding.h"
166 #include "process.h"
167 #include "region-cache.h"
168 #include "font.h"
169 #include "fontset.h"
170 #include "blockinput.h"
171
172 #ifdef HAVE_X_WINDOWS
173 #include "xterm.h"
174 #endif
175 #ifdef WINDOWSNT
176 #include "w32term.h"
177 #endif
178 #ifdef HAVE_NS
179 #include "nsterm.h"
180 #endif
181 #ifdef USE_GTK
182 #include "gtkutil.h"
183 #endif
184
185 #include "font.h"
186
187 #ifndef FRAME_X_OUTPUT
188 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
189 #endif
190
191 #define INFINITY 10000000
192
193 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
194 || defined(HAVE_NS) || defined (USE_GTK)
195 extern void set_frame_menubar P_ ((struct frame *f, int, int));
196 extern int pending_menu_activation;
197 #endif
198
199 extern int interrupt_input;
200 extern int command_loop_level;
201
202 extern Lisp_Object do_mouse_tracking;
203
204 extern int minibuffer_auto_raise;
205 extern Lisp_Object Vminibuffer_list;
206
207 extern Lisp_Object Qface;
208 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
209
210 extern Lisp_Object Voverriding_local_map;
211 extern Lisp_Object Voverriding_local_map_menu_flag;
212 extern Lisp_Object Qmenu_item;
213 extern Lisp_Object Qwhen;
214 extern Lisp_Object Qhelp_echo;
215 extern Lisp_Object Qbefore_string, Qafter_string;
216
217 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
218 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
219 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
220 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
221 Lisp_Object Qinhibit_point_motion_hooks;
222 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
223 Lisp_Object Qfontified;
224 Lisp_Object Qgrow_only;
225 Lisp_Object Qinhibit_eval_during_redisplay;
226 Lisp_Object Qbuffer_position, Qposition, Qobject;
227 Lisp_Object Qright_to_left, Qleft_to_right;
228
229 /* Cursor shapes */
230 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
231
232 /* Pointer shapes */
233 Lisp_Object Qarrow, Qhand, Qtext;
234
235 Lisp_Object Qrisky_local_variable;
236
237 /* Holds the list (error). */
238 Lisp_Object list_of_error;
239
240 /* Functions called to fontify regions of text. */
241
242 Lisp_Object Vfontification_functions;
243 Lisp_Object Qfontification_functions;
244
245 /* Non-nil means automatically select any window when the mouse
246 cursor moves into it. */
247 Lisp_Object Vmouse_autoselect_window;
248
249 Lisp_Object Vwrap_prefix, Qwrap_prefix;
250 Lisp_Object Vline_prefix, Qline_prefix;
251
252 /* Non-zero means draw tool bar buttons raised when the mouse moves
253 over them. */
254
255 int auto_raise_tool_bar_buttons_p;
256
257 /* Non-zero means to reposition window if cursor line is only partially visible. */
258
259 int make_cursor_line_fully_visible_p;
260
261 /* Margin below tool bar in pixels. 0 or nil means no margin.
262 If value is `internal-border-width' or `border-width',
263 the corresponding frame parameter is used. */
264
265 Lisp_Object Vtool_bar_border;
266
267 /* Margin around tool bar buttons in pixels. */
268
269 Lisp_Object Vtool_bar_button_margin;
270
271 /* Thickness of shadow to draw around tool bar buttons. */
272
273 EMACS_INT tool_bar_button_relief;
274
275 /* Non-nil means automatically resize tool-bars so that all tool-bar
276 items are visible, and no blank lines remain.
277
278 If value is `grow-only', only make tool-bar bigger. */
279
280 Lisp_Object Vauto_resize_tool_bars;
281
282 /* Non-zero means draw block and hollow cursor as wide as the glyph
283 under it. For example, if a block cursor is over a tab, it will be
284 drawn as wide as that tab on the display. */
285
286 int x_stretch_cursor_p;
287
288 /* Non-nil means don't actually do any redisplay. */
289
290 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
291
292 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
293
294 int inhibit_eval_during_redisplay;
295
296 /* Names of text properties relevant for redisplay. */
297
298 Lisp_Object Qdisplay;
299 extern Lisp_Object Qface, Qinvisible, Qwidth;
300
301 /* Symbols used in text property values. */
302
303 Lisp_Object Vdisplay_pixels_per_inch;
304 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
305 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
306 Lisp_Object Qslice;
307 Lisp_Object Qcenter;
308 Lisp_Object Qmargin, Qpointer;
309 Lisp_Object Qline_height;
310 extern Lisp_Object Qheight;
311 extern Lisp_Object QCwidth, QCheight, QCascent;
312 extern Lisp_Object Qscroll_bar;
313 extern Lisp_Object Qcursor;
314
315 /* Non-nil means highlight trailing whitespace. */
316
317 Lisp_Object Vshow_trailing_whitespace;
318
319 /* Non-nil means escape non-break space and hyphens. */
320
321 Lisp_Object Vnobreak_char_display;
322
323 #ifdef HAVE_WINDOW_SYSTEM
324 extern Lisp_Object Voverflow_newline_into_fringe;
325
326 /* Test if overflow newline into fringe. Called with iterator IT
327 at or past right window margin, and with IT->current_x set. */
328
329 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
330 (!NILP (Voverflow_newline_into_fringe) \
331 && FRAME_WINDOW_P (it->f) \
332 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
333 && it->current_x == it->last_visible_x \
334 && it->line_wrap != WORD_WRAP)
335
336 #else /* !HAVE_WINDOW_SYSTEM */
337 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
338 #endif /* HAVE_WINDOW_SYSTEM */
339
340 /* Test if the display element loaded in IT is a space or tab
341 character. This is used to determine word wrapping. */
342
343 #define IT_DISPLAYING_WHITESPACE(it) \
344 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
345
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
349
350 Lisp_Object Vvoid_text_area_pointer;
351
352 /* Name of the face used to highlight trailing whitespace. */
353
354 Lisp_Object Qtrailing_whitespace;
355
356 /* Name and number of the face used to highlight escape glyphs. */
357
358 Lisp_Object Qescape_glyph;
359
360 /* Name and number of the face used to highlight non-breaking spaces. */
361
362 Lisp_Object Qnobreak_space;
363
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
366
367 Lisp_Object Qimage;
368
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
372
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
375
376 int noninteractive_need_newline;
377
378 /* Non-zero means print newline to message log before next message. */
379
380 static int message_log_need_newline;
381
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
388 \f
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
393
394 static struct text_pos this_line_start_pos;
395
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
398
399 static struct text_pos this_line_end_pos;
400
401 /* The vertical positions and the height of this line. */
402
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
406
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
409
410 static int this_line_start_x;
411
412 /* Buffer that this_line_.* variables are referring to. */
413
414 static struct buffer *this_line_buffer;
415
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
418
419 Lisp_Object Vtruncate_partial_width_windows;
420
421 /* A flag to control how to display unibyte 8-bit character. */
422
423 int unibyte_display_via_language_environment;
424
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
428
429 int multiple_frames;
430
431 Lisp_Object Vglobal_mode_string;
432
433
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
437
438 Lisp_Object Voverlay_arrow_variable_list;
439
440 /* Marker for where to display an arrow on top of the buffer text. */
441
442 Lisp_Object Voverlay_arrow_position;
443
444 /* String to display for the arrow. Only used on terminal frames. */
445
446 Lisp_Object Voverlay_arrow_string;
447
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
452
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
454
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
457
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
459
460 /* Like mode-line-format, but for the title bar on a visible frame. */
461
462 Lisp_Object Vframe_title_format;
463
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
465
466 Lisp_Object Vicon_title_format;
467
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
471
472 static Lisp_Object Vwindow_size_change_functions;
473
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
475
476 /* Nonzero if an overlay arrow has been displayed in this window. */
477
478 static int overlay_arrow_seen;
479
480 /* Nonzero means highlight the region even in nonselected windows. */
481
482 int highlight_nonselected_windows;
483
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
486
487 static EMACS_INT scroll_step;
488
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
491
492 static EMACS_INT scroll_conservatively;
493
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
498
499 EMACS_INT scroll_margin;
500
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
504
505 int buffer_shared;
506
507 /* Vector containing glyphs for an ellipsis `...'. */
508
509 static Lisp_Object default_invis_vector[3];
510
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
514
515 This variable is deprecated. */
516
517 int mode_line_inverse_video;
518
519 /* Prompt to display in front of the mini-buffer contents. */
520
521 Lisp_Object minibuf_prompt;
522
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
525
526 int minibuf_prompt_width;
527
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
531
532 Lisp_Object echo_area_window;
533
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
538
539 Lisp_Object Vmessage_stack;
540
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
543
544 int message_enable_multibyte;
545
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
547
548 int update_mode_lines;
549
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
552
553 int windows_or_buffers_changed;
554
555 /* Nonzero means a frame's cursor type has been changed. */
556
557 int cursor_type_changed;
558
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
561
562 int line_number_displayed;
563
564 /* Maximum buffer size for which to display line numbers. */
565
566 Lisp_Object Vline_number_display_limit;
567
568 /* Line width to consider when repositioning for line number display. */
569
570 static EMACS_INT line_number_display_limit_width;
571
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
574
575 Lisp_Object Vmessage_log_max;
576
577 /* The name of the *Messages* buffer, a string. */
578
579 static Lisp_Object Vmessages_buffer_name;
580
581 /* Current, index 0, and last displayed echo area message. Either
582 buffers from echo_buffers, or nil to indicate no message. */
583
584 Lisp_Object echo_area_buffer[2];
585
586 /* The buffers referenced from echo_area_buffer. */
587
588 static Lisp_Object echo_buffer[2];
589
590 /* A vector saved used in with_area_buffer to reduce consing. */
591
592 static Lisp_Object Vwith_echo_area_save_vector;
593
594 /* Non-zero means display_echo_area should display the last echo area
595 message again. Set by redisplay_preserve_echo_area. */
596
597 static int display_last_displayed_message_p;
598
599 /* Nonzero if echo area is being used by print; zero if being used by
600 message. */
601
602 int message_buf_print;
603
604 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
605
606 Lisp_Object Qinhibit_menubar_update;
607 int inhibit_menubar_update;
608
609 /* When evaluating expressions from menu bar items (enable conditions,
610 for instance), this is the frame they are being processed for. */
611
612 Lisp_Object Vmenu_updating_frame;
613
614 /* Maximum height for resizing mini-windows. Either a float
615 specifying a fraction of the available height, or an integer
616 specifying a number of lines. */
617
618 Lisp_Object Vmax_mini_window_height;
619
620 /* Non-zero means messages should be displayed with truncated
621 lines instead of being continued. */
622
623 int message_truncate_lines;
624 Lisp_Object Qmessage_truncate_lines;
625
626 /* Set to 1 in clear_message to make redisplay_internal aware
627 of an emptied echo area. */
628
629 static int message_cleared_p;
630
631 /* How to blink the default frame cursor off. */
632 Lisp_Object Vblink_cursor_alist;
633
634 /* A scratch glyph row with contents used for generating truncation
635 glyphs. Also used in direct_output_for_insert. */
636
637 #define MAX_SCRATCH_GLYPHS 100
638 struct glyph_row scratch_glyph_row;
639 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
640
641 /* Ascent and height of the last line processed by move_it_to. */
642
643 static int last_max_ascent, last_height;
644
645 /* Non-zero if there's a help-echo in the echo area. */
646
647 int help_echo_showing_p;
648
649 /* If >= 0, computed, exact values of mode-line and header-line height
650 to use in the macros CURRENT_MODE_LINE_HEIGHT and
651 CURRENT_HEADER_LINE_HEIGHT. */
652
653 int current_mode_line_height, current_header_line_height;
654
655 /* The maximum distance to look ahead for text properties. Values
656 that are too small let us call compute_char_face and similar
657 functions too often which is expensive. Values that are too large
658 let us call compute_char_face and alike too often because we
659 might not be interested in text properties that far away. */
660
661 #define TEXT_PROP_DISTANCE_LIMIT 100
662
663 #if GLYPH_DEBUG
664
665 /* Variables to turn off display optimizations from Lisp. */
666
667 int inhibit_try_window_id, inhibit_try_window_reusing;
668 int inhibit_try_cursor_movement;
669
670 /* Non-zero means print traces of redisplay if compiled with
671 GLYPH_DEBUG != 0. */
672
673 int trace_redisplay_p;
674
675 #endif /* GLYPH_DEBUG */
676
677 #ifdef DEBUG_TRACE_MOVE
678 /* Non-zero means trace with TRACE_MOVE to stderr. */
679 int trace_move;
680
681 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
682 #else
683 #define TRACE_MOVE(x) (void) 0
684 #endif
685
686 /* Non-zero means automatically scroll windows horizontally to make
687 point visible. */
688
689 int automatic_hscrolling_p;
690 Lisp_Object Qauto_hscroll_mode;
691
692 /* How close to the margin can point get before the window is scrolled
693 horizontally. */
694 EMACS_INT hscroll_margin;
695
696 /* How much to scroll horizontally when point is inside the above margin. */
697 Lisp_Object Vhscroll_step;
698
699 /* The variable `resize-mini-windows'. If nil, don't resize
700 mini-windows. If t, always resize them to fit the text they
701 display. If `grow-only', let mini-windows grow only until they
702 become empty. */
703
704 Lisp_Object Vresize_mini_windows;
705
706 /* Buffer being redisplayed -- for redisplay_window_error. */
707
708 struct buffer *displayed_buffer;
709
710 /* Space between overline and text. */
711
712 EMACS_INT overline_margin;
713
714 /* Require underline to be at least this many screen pixels below baseline
715 This to avoid underline "merging" with the base of letters at small
716 font sizes, particularly when x_use_underline_position_properties is on. */
717
718 EMACS_INT underline_minimum_offset;
719
720 /* Value returned from text property handlers (see below). */
721
722 enum prop_handled
723 {
724 HANDLED_NORMALLY,
725 HANDLED_RECOMPUTE_PROPS,
726 HANDLED_OVERLAY_STRING_CONSUMED,
727 HANDLED_RETURN
728 };
729
730 /* A description of text properties that redisplay is interested
731 in. */
732
733 struct props
734 {
735 /* The name of the property. */
736 Lisp_Object *name;
737
738 /* A unique index for the property. */
739 enum prop_idx idx;
740
741 /* A handler function called to set up iterator IT from the property
742 at IT's current position. Value is used to steer handle_stop. */
743 enum prop_handled (*handler) P_ ((struct it *it));
744 };
745
746 static enum prop_handled handle_face_prop P_ ((struct it *));
747 static enum prop_handled handle_invisible_prop P_ ((struct it *));
748 static enum prop_handled handle_display_prop P_ ((struct it *));
749 static enum prop_handled handle_composition_prop P_ ((struct it *));
750 static enum prop_handled handle_overlay_change P_ ((struct it *));
751 static enum prop_handled handle_fontified_prop P_ ((struct it *));
752
753 /* Properties handled by iterators. */
754
755 static struct props it_props[] =
756 {
757 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
758 /* Handle `face' before `display' because some sub-properties of
759 `display' need to know the face. */
760 {&Qface, FACE_PROP_IDX, handle_face_prop},
761 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
762 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
763 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
764 {NULL, 0, NULL}
765 };
766
767 /* Value is the position described by X. If X is a marker, value is
768 the marker_position of X. Otherwise, value is X. */
769
770 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
771
772 /* Enumeration returned by some move_it_.* functions internally. */
773
774 enum move_it_result
775 {
776 /* Not used. Undefined value. */
777 MOVE_UNDEFINED,
778
779 /* Move ended at the requested buffer position or ZV. */
780 MOVE_POS_MATCH_OR_ZV,
781
782 /* Move ended at the requested X pixel position. */
783 MOVE_X_REACHED,
784
785 /* Move within a line ended at the end of a line that must be
786 continued. */
787 MOVE_LINE_CONTINUED,
788
789 /* Move within a line ended at the end of a line that would
790 be displayed truncated. */
791 MOVE_LINE_TRUNCATED,
792
793 /* Move within a line ended at a line end. */
794 MOVE_NEWLINE_OR_CR
795 };
796
797 /* This counter is used to clear the face cache every once in a while
798 in redisplay_internal. It is incremented for each redisplay.
799 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
800 cleared. */
801
802 #define CLEAR_FACE_CACHE_COUNT 500
803 static int clear_face_cache_count;
804
805 /* Similarly for the image cache. */
806
807 #ifdef HAVE_WINDOW_SYSTEM
808 #define CLEAR_IMAGE_CACHE_COUNT 101
809 static int clear_image_cache_count;
810 #endif
811
812 /* Non-zero while redisplay_internal is in progress. */
813
814 int redisplaying_p;
815
816 /* Non-zero means don't free realized faces. Bound while freeing
817 realized faces is dangerous because glyph matrices might still
818 reference them. */
819
820 int inhibit_free_realized_faces;
821 Lisp_Object Qinhibit_free_realized_faces;
822
823 /* If a string, XTread_socket generates an event to display that string.
824 (The display is done in read_char.) */
825
826 Lisp_Object help_echo_string;
827 Lisp_Object help_echo_window;
828 Lisp_Object help_echo_object;
829 int help_echo_pos;
830
831 /* Temporary variable for XTread_socket. */
832
833 Lisp_Object previous_help_echo_string;
834
835 /* Null glyph slice */
836
837 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
838
839 /* Platform-independent portion of hourglass implementation. */
840
841 /* Non-zero means we're allowed to display a hourglass pointer. */
842 int display_hourglass_p;
843
844 /* Non-zero means an hourglass cursor is currently shown. */
845 int hourglass_shown_p;
846
847 /* If non-null, an asynchronous timer that, when it expires, displays
848 an hourglass cursor on all frames. */
849 struct atimer *hourglass_atimer;
850
851 /* Number of seconds to wait before displaying an hourglass cursor. */
852 Lisp_Object Vhourglass_delay;
853
854 /* Default number of seconds to wait before displaying an hourglass
855 cursor. */
856 #define DEFAULT_HOURGLASS_DELAY 1
857
858 \f
859 /* Function prototypes. */
860
861 static void setup_for_ellipsis P_ ((struct it *, int));
862 static void mark_window_display_accurate_1 P_ ((struct window *, int));
863 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
864 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
865 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
866 static int redisplay_mode_lines P_ ((Lisp_Object, int));
867 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
868
869 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
870
871 static void handle_line_prefix P_ ((struct it *));
872
873 static void pint2str P_ ((char *, int, int));
874 static void pint2hrstr P_ ((char *, int, int));
875 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
876 struct text_pos));
877 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
878 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
879 static void store_mode_line_noprop_char P_ ((char));
880 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
881 static void x_consider_frame_title P_ ((Lisp_Object));
882 static void handle_stop P_ ((struct it *));
883 static void handle_stop_backwards P_ ((struct it *, EMACS_INT));
884 static int tool_bar_lines_needed P_ ((struct frame *, int *));
885 static int single_display_spec_intangible_p P_ ((Lisp_Object));
886 static void ensure_echo_area_buffers P_ ((void));
887 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
888 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
889 static int with_echo_area_buffer P_ ((struct window *, int,
890 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
891 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
892 static void clear_garbaged_frames P_ ((void));
893 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
894 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
895 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
896 static int display_echo_area P_ ((struct window *));
897 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
898 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
899 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
900 static int string_char_and_length P_ ((const unsigned char *, int *));
901 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
902 struct text_pos));
903 static int compute_window_start_on_continuation_line P_ ((struct window *));
904 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
905 static void insert_left_trunc_glyphs P_ ((struct it *));
906 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
907 Lisp_Object));
908 static void extend_face_to_end_of_line P_ ((struct it *));
909 static int append_space_for_newline P_ ((struct it *, int));
910 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
911 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
912 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
913 static int trailing_whitespace_p P_ ((int));
914 static int message_log_check_duplicate P_ ((int, int, int, int));
915 static void push_it P_ ((struct it *));
916 static void pop_it P_ ((struct it *));
917 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
918 static void select_frame_for_redisplay P_ ((Lisp_Object));
919 static void redisplay_internal P_ ((int));
920 static int echo_area_display P_ ((int));
921 static void redisplay_windows P_ ((Lisp_Object));
922 static void redisplay_window P_ ((Lisp_Object, int));
923 static Lisp_Object redisplay_window_error ();
924 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
925 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
926 static int update_menu_bar P_ ((struct frame *, int, int));
927 static int try_window_reusing_current_matrix P_ ((struct window *));
928 static int try_window_id P_ ((struct window *));
929 static int display_line P_ ((struct it *));
930 static int display_mode_lines P_ ((struct window *));
931 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
932 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
933 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
934 static char *decode_mode_spec P_ ((struct window *, int, int, int,
935 Lisp_Object *));
936 static void display_menu_bar P_ ((struct window *));
937 static int display_count_lines P_ ((int, int, int, int, int *));
938 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
939 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
940 static void compute_line_metrics P_ ((struct it *));
941 static void run_redisplay_end_trigger_hook P_ ((struct it *));
942 static int get_overlay_strings P_ ((struct it *, int));
943 static int get_overlay_strings_1 P_ ((struct it *, int, int));
944 static void next_overlay_string P_ ((struct it *));
945 static void reseat P_ ((struct it *, struct text_pos, int));
946 static void reseat_1 P_ ((struct it *, struct text_pos, int));
947 static void back_to_previous_visible_line_start P_ ((struct it *));
948 void reseat_at_previous_visible_line_start P_ ((struct it *));
949 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
950 static int next_element_from_ellipsis P_ ((struct it *));
951 static int next_element_from_display_vector P_ ((struct it *));
952 static int next_element_from_string P_ ((struct it *));
953 static int next_element_from_c_string P_ ((struct it *));
954 static int next_element_from_buffer P_ ((struct it *));
955 static int next_element_from_composition P_ ((struct it *));
956 static int next_element_from_image P_ ((struct it *));
957 static int next_element_from_stretch P_ ((struct it *));
958 static void load_overlay_strings P_ ((struct it *, int));
959 static int init_from_display_pos P_ ((struct it *, struct window *,
960 struct display_pos *));
961 static void reseat_to_string P_ ((struct it *, unsigned char *,
962 Lisp_Object, int, int, int, int));
963 static enum move_it_result
964 move_it_in_display_line_to (struct it *, EMACS_INT, int,
965 enum move_operation_enum);
966 void move_it_vertically_backward P_ ((struct it *, int));
967 static void init_to_row_start P_ ((struct it *, struct window *,
968 struct glyph_row *));
969 static int init_to_row_end P_ ((struct it *, struct window *,
970 struct glyph_row *));
971 static void back_to_previous_line_start P_ ((struct it *));
972 static int forward_to_next_line_start P_ ((struct it *, int *));
973 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
974 Lisp_Object, int));
975 static struct text_pos string_pos P_ ((int, Lisp_Object));
976 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
977 static int number_of_chars P_ ((unsigned char *, int));
978 static void compute_stop_pos P_ ((struct it *));
979 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
980 Lisp_Object));
981 static int face_before_or_after_it_pos P_ ((struct it *, int));
982 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
983 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
984 Lisp_Object, Lisp_Object,
985 struct text_pos *, int));
986 static int underlying_face_id P_ ((struct it *));
987 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
988 struct window *));
989
990 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
991 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
992
993 #ifdef HAVE_WINDOW_SYSTEM
994
995 static void update_tool_bar P_ ((struct frame *, int));
996 static void build_desired_tool_bar_string P_ ((struct frame *f));
997 static int redisplay_tool_bar P_ ((struct frame *));
998 static void display_tool_bar_line P_ ((struct it *, int));
999 static void notice_overwritten_cursor P_ ((struct window *,
1000 enum glyph_row_area,
1001 int, int, int, int));
1002
1003
1004
1005 #endif /* HAVE_WINDOW_SYSTEM */
1006
1007 \f
1008 /***********************************************************************
1009 Window display dimensions
1010 ***********************************************************************/
1011
1012 /* Return the bottom boundary y-position for text lines in window W.
1013 This is the first y position at which a line cannot start.
1014 It is relative to the top of the window.
1015
1016 This is the height of W minus the height of a mode line, if any. */
1017
1018 INLINE int
1019 window_text_bottom_y (w)
1020 struct window *w;
1021 {
1022 int height = WINDOW_TOTAL_HEIGHT (w);
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 height -= CURRENT_MODE_LINE_HEIGHT (w);
1026 return height;
1027 }
1028
1029 /* Return the pixel width of display area AREA of window W. AREA < 0
1030 means return the total width of W, not including fringes to
1031 the left and right of the window. */
1032
1033 INLINE int
1034 window_box_width (w, area)
1035 struct window *w;
1036 int area;
1037 {
1038 int cols = XFASTINT (w->total_cols);
1039 int pixels = 0;
1040
1041 if (!w->pseudo_window_p)
1042 {
1043 cols -= WINDOW_SCROLL_BAR_COLS (w);
1044
1045 if (area == TEXT_AREA)
1046 {
1047 if (INTEGERP (w->left_margin_cols))
1048 cols -= XFASTINT (w->left_margin_cols);
1049 if (INTEGERP (w->right_margin_cols))
1050 cols -= XFASTINT (w->right_margin_cols);
1051 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1052 }
1053 else if (area == LEFT_MARGIN_AREA)
1054 {
1055 cols = (INTEGERP (w->left_margin_cols)
1056 ? XFASTINT (w->left_margin_cols) : 0);
1057 pixels = 0;
1058 }
1059 else if (area == RIGHT_MARGIN_AREA)
1060 {
1061 cols = (INTEGERP (w->right_margin_cols)
1062 ? XFASTINT (w->right_margin_cols) : 0);
1063 pixels = 0;
1064 }
1065 }
1066
1067 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1068 }
1069
1070
1071 /* Return the pixel height of the display area of window W, not
1072 including mode lines of W, if any. */
1073
1074 INLINE int
1075 window_box_height (w)
1076 struct window *w;
1077 {
1078 struct frame *f = XFRAME (w->frame);
1079 int height = WINDOW_TOTAL_HEIGHT (w);
1080
1081 xassert (height >= 0);
1082
1083 /* Note: the code below that determines the mode-line/header-line
1084 height is essentially the same as that contained in the macro
1085 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1086 the appropriate glyph row has its `mode_line_p' flag set,
1087 and if it doesn't, uses estimate_mode_line_height instead. */
1088
1089 if (WINDOW_WANTS_MODELINE_P (w))
1090 {
1091 struct glyph_row *ml_row
1092 = (w->current_matrix && w->current_matrix->rows
1093 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1094 : 0);
1095 if (ml_row && ml_row->mode_line_p)
1096 height -= ml_row->height;
1097 else
1098 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1099 }
1100
1101 if (WINDOW_WANTS_HEADER_LINE_P (w))
1102 {
1103 struct glyph_row *hl_row
1104 = (w->current_matrix && w->current_matrix->rows
1105 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1106 : 0);
1107 if (hl_row && hl_row->mode_line_p)
1108 height -= hl_row->height;
1109 else
1110 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1111 }
1112
1113 /* With a very small font and a mode-line that's taller than
1114 default, we might end up with a negative height. */
1115 return max (0, height);
1116 }
1117
1118 /* Return the window-relative coordinate of the left edge of display
1119 area AREA of window W. AREA < 0 means return the left edge of the
1120 whole window, to the right of the left fringe of W. */
1121
1122 INLINE int
1123 window_box_left_offset (w, area)
1124 struct window *w;
1125 int area;
1126 {
1127 int x;
1128
1129 if (w->pseudo_window_p)
1130 return 0;
1131
1132 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1133
1134 if (area == TEXT_AREA)
1135 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1136 + window_box_width (w, LEFT_MARGIN_AREA));
1137 else if (area == RIGHT_MARGIN_AREA)
1138 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1139 + window_box_width (w, LEFT_MARGIN_AREA)
1140 + window_box_width (w, TEXT_AREA)
1141 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1142 ? 0
1143 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1144 else if (area == LEFT_MARGIN_AREA
1145 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1146 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1147
1148 return x;
1149 }
1150
1151
1152 /* Return the window-relative coordinate of the right edge of display
1153 area AREA of window W. AREA < 0 means return the left edge of the
1154 whole window, to the left of the right fringe of W. */
1155
1156 INLINE int
1157 window_box_right_offset (w, area)
1158 struct window *w;
1159 int area;
1160 {
1161 return window_box_left_offset (w, area) + window_box_width (w, area);
1162 }
1163
1164 /* Return the frame-relative coordinate of the left edge of display
1165 area AREA of window W. AREA < 0 means return the left edge of the
1166 whole window, to the right of the left fringe of W. */
1167
1168 INLINE int
1169 window_box_left (w, area)
1170 struct window *w;
1171 int area;
1172 {
1173 struct frame *f = XFRAME (w->frame);
1174 int x;
1175
1176 if (w->pseudo_window_p)
1177 return FRAME_INTERNAL_BORDER_WIDTH (f);
1178
1179 x = (WINDOW_LEFT_EDGE_X (w)
1180 + window_box_left_offset (w, area));
1181
1182 return x;
1183 }
1184
1185
1186 /* Return the frame-relative coordinate of the right edge of display
1187 area AREA of window W. AREA < 0 means return the left edge of the
1188 whole window, to the left of the right fringe of W. */
1189
1190 INLINE int
1191 window_box_right (w, area)
1192 struct window *w;
1193 int area;
1194 {
1195 return window_box_left (w, area) + window_box_width (w, area);
1196 }
1197
1198 /* Get the bounding box of the display area AREA of window W, without
1199 mode lines, in frame-relative coordinates. AREA < 0 means the
1200 whole window, not including the left and right fringes of
1201 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1202 coordinates of the upper-left corner of the box. Return in
1203 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1204
1205 INLINE void
1206 window_box (w, area, box_x, box_y, box_width, box_height)
1207 struct window *w;
1208 int area;
1209 int *box_x, *box_y, *box_width, *box_height;
1210 {
1211 if (box_width)
1212 *box_width = window_box_width (w, area);
1213 if (box_height)
1214 *box_height = window_box_height (w);
1215 if (box_x)
1216 *box_x = window_box_left (w, area);
1217 if (box_y)
1218 {
1219 *box_y = WINDOW_TOP_EDGE_Y (w);
1220 if (WINDOW_WANTS_HEADER_LINE_P (w))
1221 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1222 }
1223 }
1224
1225
1226 /* Get the bounding box of the display area AREA of window W, without
1227 mode lines. AREA < 0 means the whole window, not including the
1228 left and right fringe of the window. Return in *TOP_LEFT_X
1229 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1230 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1231 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1232 box. */
1233
1234 INLINE void
1235 window_box_edges (w, area, top_left_x, top_left_y,
1236 bottom_right_x, bottom_right_y)
1237 struct window *w;
1238 int area;
1239 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1240 {
1241 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1242 bottom_right_y);
1243 *bottom_right_x += *top_left_x;
1244 *bottom_right_y += *top_left_y;
1245 }
1246
1247
1248 \f
1249 /***********************************************************************
1250 Utilities
1251 ***********************************************************************/
1252
1253 /* Return the bottom y-position of the line the iterator IT is in.
1254 This can modify IT's settings. */
1255
1256 int
1257 line_bottom_y (it)
1258 struct it *it;
1259 {
1260 int line_height = it->max_ascent + it->max_descent;
1261 int line_top_y = it->current_y;
1262
1263 if (line_height == 0)
1264 {
1265 if (last_height)
1266 line_height = last_height;
1267 else if (IT_CHARPOS (*it) < ZV)
1268 {
1269 move_it_by_lines (it, 1, 1);
1270 line_height = (it->max_ascent || it->max_descent
1271 ? it->max_ascent + it->max_descent
1272 : last_height);
1273 }
1274 else
1275 {
1276 struct glyph_row *row = it->glyph_row;
1277
1278 /* Use the default character height. */
1279 it->glyph_row = NULL;
1280 it->what = IT_CHARACTER;
1281 it->c = ' ';
1282 it->len = 1;
1283 PRODUCE_GLYPHS (it);
1284 line_height = it->ascent + it->descent;
1285 it->glyph_row = row;
1286 }
1287 }
1288
1289 return line_top_y + line_height;
1290 }
1291
1292
1293 /* Return 1 if position CHARPOS is visible in window W.
1294 CHARPOS < 0 means return info about WINDOW_END position.
1295 If visible, set *X and *Y to pixel coordinates of top left corner.
1296 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1297 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1298
1299 int
1300 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1301 struct window *w;
1302 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1303 {
1304 struct it it;
1305 struct text_pos top;
1306 int visible_p = 0;
1307 struct buffer *old_buffer = NULL;
1308
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1311
1312 if (XBUFFER (w->buffer) != current_buffer)
1313 {
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->buffer));
1316 }
1317
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319
1320 /* Compute exact mode line heights. */
1321 if (WINDOW_WANTS_MODELINE_P (w))
1322 current_mode_line_height
1323 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1324 current_buffer->mode_line_format);
1325
1326 if (WINDOW_WANTS_HEADER_LINE_P (w))
1327 current_header_line_height
1328 = display_mode_line (w, HEADER_LINE_FACE_ID,
1329 current_buffer->header_line_format);
1330
1331 start_display (&it, w, top);
1332 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1333 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1334
1335 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1336 {
1337 /* We have reached CHARPOS, or passed it. How the call to
1338 move_it_to can overshoot: (i) If CHARPOS is on invisible
1339 text, move_it_to stops at the end of the invisible text,
1340 after CHARPOS. (ii) If CHARPOS is in a display vector,
1341 move_it_to stops on its last glyph. */
1342 int top_x = it.current_x;
1343 int top_y = it.current_y;
1344 enum it_method it_method = it.method;
1345 /* Calling line_bottom_y may change it.method, it.position, etc. */
1346 int bottom_y = (last_height = 0, line_bottom_y (&it));
1347 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1348
1349 if (top_y < window_top_y)
1350 visible_p = bottom_y > window_top_y;
1351 else if (top_y < it.last_visible_y)
1352 visible_p = 1;
1353 if (visible_p)
1354 {
1355 if (it_method == GET_FROM_DISPLAY_VECTOR)
1356 {
1357 /* We stopped on the last glyph of a display vector.
1358 Try and recompute. Hack alert! */
1359 if (charpos < 2 || top.charpos >= charpos)
1360 top_x = it.glyph_row->x;
1361 else
1362 {
1363 struct it it2;
1364 start_display (&it2, w, top);
1365 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1366 get_next_display_element (&it2);
1367 PRODUCE_GLYPHS (&it2);
1368 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1369 || it2.current_x > it2.last_visible_x)
1370 top_x = it.glyph_row->x;
1371 else
1372 {
1373 top_x = it2.current_x;
1374 top_y = it2.current_y;
1375 }
1376 }
1377 }
1378
1379 *x = top_x;
1380 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1381 *rtop = max (0, window_top_y - top_y);
1382 *rbot = max (0, bottom_y - it.last_visible_y);
1383 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1384 - max (top_y, window_top_y)));
1385 *vpos = it.vpos;
1386 }
1387 }
1388 else
1389 {
1390 struct it it2;
1391
1392 it2 = it;
1393 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1394 move_it_by_lines (&it, 1, 0);
1395 if (charpos < IT_CHARPOS (it)
1396 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1397 {
1398 visible_p = 1;
1399 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1400 *x = it2.current_x;
1401 *y = it2.current_y + it2.max_ascent - it2.ascent;
1402 *rtop = max (0, -it2.current_y);
1403 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1404 - it.last_visible_y));
1405 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1406 it.last_visible_y)
1407 - max (it2.current_y,
1408 WINDOW_HEADER_LINE_HEIGHT (w))));
1409 *vpos = it2.vpos;
1410 }
1411 }
1412
1413 if (old_buffer)
1414 set_buffer_internal_1 (old_buffer);
1415
1416 current_header_line_height = current_mode_line_height = -1;
1417
1418 if (visible_p && XFASTINT (w->hscroll) > 0)
1419 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1420
1421 #if 0
1422 /* Debugging code. */
1423 if (visible_p)
1424 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1425 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1426 else
1427 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1428 #endif
1429
1430 return visible_p;
1431 }
1432
1433
1434 /* Return the next character from STR which is MAXLEN bytes long.
1435 Return in *LEN the length of the character. This is like
1436 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1437 we find one, we return a `?', but with the length of the invalid
1438 character. */
1439
1440 static INLINE int
1441 string_char_and_length (str, len)
1442 const unsigned char *str;
1443 int *len;
1444 {
1445 int c;
1446
1447 c = STRING_CHAR_AND_LENGTH (str, *len);
1448 if (!CHAR_VALID_P (c, 1))
1449 /* We may not change the length here because other places in Emacs
1450 don't use this function, i.e. they silently accept invalid
1451 characters. */
1452 c = '?';
1453
1454 return c;
1455 }
1456
1457
1458
1459 /* Given a position POS containing a valid character and byte position
1460 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1461
1462 static struct text_pos
1463 string_pos_nchars_ahead (pos, string, nchars)
1464 struct text_pos pos;
1465 Lisp_Object string;
1466 int nchars;
1467 {
1468 xassert (STRINGP (string) && nchars >= 0);
1469
1470 if (STRING_MULTIBYTE (string))
1471 {
1472 int rest = SBYTES (string) - BYTEPOS (pos);
1473 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1474 int len;
1475
1476 while (nchars--)
1477 {
1478 string_char_and_length (p, &len);
1479 p += len, rest -= len;
1480 xassert (rest >= 0);
1481 CHARPOS (pos) += 1;
1482 BYTEPOS (pos) += len;
1483 }
1484 }
1485 else
1486 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1487
1488 return pos;
1489 }
1490
1491
1492 /* Value is the text position, i.e. character and byte position,
1493 for character position CHARPOS in STRING. */
1494
1495 static INLINE struct text_pos
1496 string_pos (charpos, string)
1497 int charpos;
1498 Lisp_Object string;
1499 {
1500 struct text_pos pos;
1501 xassert (STRINGP (string));
1502 xassert (charpos >= 0);
1503 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1504 return pos;
1505 }
1506
1507
1508 /* Value is a text position, i.e. character and byte position, for
1509 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1510 means recognize multibyte characters. */
1511
1512 static struct text_pos
1513 c_string_pos (charpos, s, multibyte_p)
1514 int charpos;
1515 unsigned char *s;
1516 int multibyte_p;
1517 {
1518 struct text_pos pos;
1519
1520 xassert (s != NULL);
1521 xassert (charpos >= 0);
1522
1523 if (multibyte_p)
1524 {
1525 int rest = strlen (s), len;
1526
1527 SET_TEXT_POS (pos, 0, 0);
1528 while (charpos--)
1529 {
1530 string_char_and_length (s, &len);
1531 s += len, rest -= len;
1532 xassert (rest >= 0);
1533 CHARPOS (pos) += 1;
1534 BYTEPOS (pos) += len;
1535 }
1536 }
1537 else
1538 SET_TEXT_POS (pos, charpos, charpos);
1539
1540 return pos;
1541 }
1542
1543
1544 /* Value is the number of characters in C string S. MULTIBYTE_P
1545 non-zero means recognize multibyte characters. */
1546
1547 static int
1548 number_of_chars (s, multibyte_p)
1549 unsigned char *s;
1550 int multibyte_p;
1551 {
1552 int nchars;
1553
1554 if (multibyte_p)
1555 {
1556 int rest = strlen (s), len;
1557 unsigned char *p = (unsigned char *) s;
1558
1559 for (nchars = 0; rest > 0; ++nchars)
1560 {
1561 string_char_and_length (p, &len);
1562 rest -= len, p += len;
1563 }
1564 }
1565 else
1566 nchars = strlen (s);
1567
1568 return nchars;
1569 }
1570
1571
1572 /* Compute byte position NEWPOS->bytepos corresponding to
1573 NEWPOS->charpos. POS is a known position in string STRING.
1574 NEWPOS->charpos must be >= POS.charpos. */
1575
1576 static void
1577 compute_string_pos (newpos, pos, string)
1578 struct text_pos *newpos, pos;
1579 Lisp_Object string;
1580 {
1581 xassert (STRINGP (string));
1582 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1583
1584 if (STRING_MULTIBYTE (string))
1585 *newpos = string_pos_nchars_ahead (pos, string,
1586 CHARPOS (*newpos) - CHARPOS (pos));
1587 else
1588 BYTEPOS (*newpos) = CHARPOS (*newpos);
1589 }
1590
1591 /* EXPORT:
1592 Return an estimation of the pixel height of mode or header lines on
1593 frame F. FACE_ID specifies what line's height to estimate. */
1594
1595 int
1596 estimate_mode_line_height (f, face_id)
1597 struct frame *f;
1598 enum face_id face_id;
1599 {
1600 #ifdef HAVE_WINDOW_SYSTEM
1601 if (FRAME_WINDOW_P (f))
1602 {
1603 int height = FONT_HEIGHT (FRAME_FONT (f));
1604
1605 /* This function is called so early when Emacs starts that the face
1606 cache and mode line face are not yet initialized. */
1607 if (FRAME_FACE_CACHE (f))
1608 {
1609 struct face *face = FACE_FROM_ID (f, face_id);
1610 if (face)
1611 {
1612 if (face->font)
1613 height = FONT_HEIGHT (face->font);
1614 if (face->box_line_width > 0)
1615 height += 2 * face->box_line_width;
1616 }
1617 }
1618
1619 return height;
1620 }
1621 #endif
1622
1623 return 1;
1624 }
1625
1626 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1627 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1628 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1629 not force the value into range. */
1630
1631 void
1632 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1633 FRAME_PTR f;
1634 register int pix_x, pix_y;
1635 int *x, *y;
1636 NativeRectangle *bounds;
1637 int noclip;
1638 {
1639
1640 #ifdef HAVE_WINDOW_SYSTEM
1641 if (FRAME_WINDOW_P (f))
1642 {
1643 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1644 even for negative values. */
1645 if (pix_x < 0)
1646 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1647 if (pix_y < 0)
1648 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1649
1650 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1651 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1652
1653 if (bounds)
1654 STORE_NATIVE_RECT (*bounds,
1655 FRAME_COL_TO_PIXEL_X (f, pix_x),
1656 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1657 FRAME_COLUMN_WIDTH (f) - 1,
1658 FRAME_LINE_HEIGHT (f) - 1);
1659
1660 if (!noclip)
1661 {
1662 if (pix_x < 0)
1663 pix_x = 0;
1664 else if (pix_x > FRAME_TOTAL_COLS (f))
1665 pix_x = FRAME_TOTAL_COLS (f);
1666
1667 if (pix_y < 0)
1668 pix_y = 0;
1669 else if (pix_y > FRAME_LINES (f))
1670 pix_y = FRAME_LINES (f);
1671 }
1672 }
1673 #endif
1674
1675 *x = pix_x;
1676 *y = pix_y;
1677 }
1678
1679
1680 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1681 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1682 can't tell the positions because W's display is not up to date,
1683 return 0. */
1684
1685 int
1686 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1687 struct window *w;
1688 int hpos, vpos;
1689 int *frame_x, *frame_y;
1690 {
1691 #ifdef HAVE_WINDOW_SYSTEM
1692 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1693 {
1694 int success_p;
1695
1696 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1697 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1698
1699 if (display_completed)
1700 {
1701 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1702 struct glyph *glyph = row->glyphs[TEXT_AREA];
1703 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1704
1705 hpos = row->x;
1706 vpos = row->y;
1707 while (glyph < end)
1708 {
1709 hpos += glyph->pixel_width;
1710 ++glyph;
1711 }
1712
1713 /* If first glyph is partially visible, its first visible position is still 0. */
1714 if (hpos < 0)
1715 hpos = 0;
1716
1717 success_p = 1;
1718 }
1719 else
1720 {
1721 hpos = vpos = 0;
1722 success_p = 0;
1723 }
1724
1725 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1726 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1727 return success_p;
1728 }
1729 #endif
1730
1731 *frame_x = hpos;
1732 *frame_y = vpos;
1733 return 1;
1734 }
1735
1736
1737 #ifdef HAVE_WINDOW_SYSTEM
1738
1739 /* Find the glyph under window-relative coordinates X/Y in window W.
1740 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1741 strings. Return in *HPOS and *VPOS the row and column number of
1742 the glyph found. Return in *AREA the glyph area containing X.
1743 Value is a pointer to the glyph found or null if X/Y is not on
1744 text, or we can't tell because W's current matrix is not up to
1745 date. */
1746
1747 static
1748 struct glyph *
1749 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1750 struct window *w;
1751 int x, y;
1752 int *hpos, *vpos, *dx, *dy, *area;
1753 {
1754 struct glyph *glyph, *end;
1755 struct glyph_row *row = NULL;
1756 int x0, i;
1757
1758 /* Find row containing Y. Give up if some row is not enabled. */
1759 for (i = 0; i < w->current_matrix->nrows; ++i)
1760 {
1761 row = MATRIX_ROW (w->current_matrix, i);
1762 if (!row->enabled_p)
1763 return NULL;
1764 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1765 break;
1766 }
1767
1768 *vpos = i;
1769 *hpos = 0;
1770
1771 /* Give up if Y is not in the window. */
1772 if (i == w->current_matrix->nrows)
1773 return NULL;
1774
1775 /* Get the glyph area containing X. */
1776 if (w->pseudo_window_p)
1777 {
1778 *area = TEXT_AREA;
1779 x0 = 0;
1780 }
1781 else
1782 {
1783 if (x < window_box_left_offset (w, TEXT_AREA))
1784 {
1785 *area = LEFT_MARGIN_AREA;
1786 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1787 }
1788 else if (x < window_box_right_offset (w, TEXT_AREA))
1789 {
1790 *area = TEXT_AREA;
1791 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1792 }
1793 else
1794 {
1795 *area = RIGHT_MARGIN_AREA;
1796 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1797 }
1798 }
1799
1800 /* Find glyph containing X. */
1801 glyph = row->glyphs[*area];
1802 end = glyph + row->used[*area];
1803 x -= x0;
1804 while (glyph < end && x >= glyph->pixel_width)
1805 {
1806 x -= glyph->pixel_width;
1807 ++glyph;
1808 }
1809
1810 if (glyph == end)
1811 return NULL;
1812
1813 if (dx)
1814 {
1815 *dx = x;
1816 *dy = y - (row->y + row->ascent - glyph->ascent);
1817 }
1818
1819 *hpos = glyph - row->glyphs[*area];
1820 return glyph;
1821 }
1822
1823
1824 /* EXPORT:
1825 Convert frame-relative x/y to coordinates relative to window W.
1826 Takes pseudo-windows into account. */
1827
1828 void
1829 frame_to_window_pixel_xy (w, x, y)
1830 struct window *w;
1831 int *x, *y;
1832 {
1833 if (w->pseudo_window_p)
1834 {
1835 /* A pseudo-window is always full-width, and starts at the
1836 left edge of the frame, plus a frame border. */
1837 struct frame *f = XFRAME (w->frame);
1838 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1839 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1840 }
1841 else
1842 {
1843 *x -= WINDOW_LEFT_EDGE_X (w);
1844 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1845 }
1846 }
1847
1848 /* EXPORT:
1849 Return in RECTS[] at most N clipping rectangles for glyph string S.
1850 Return the number of stored rectangles. */
1851
1852 int
1853 get_glyph_string_clip_rects (s, rects, n)
1854 struct glyph_string *s;
1855 NativeRectangle *rects;
1856 int n;
1857 {
1858 XRectangle r;
1859
1860 if (n <= 0)
1861 return 0;
1862
1863 if (s->row->full_width_p)
1864 {
1865 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1866 r.x = WINDOW_LEFT_EDGE_X (s->w);
1867 r.width = WINDOW_TOTAL_WIDTH (s->w);
1868
1869 /* Unless displaying a mode or menu bar line, which are always
1870 fully visible, clip to the visible part of the row. */
1871 if (s->w->pseudo_window_p)
1872 r.height = s->row->visible_height;
1873 else
1874 r.height = s->height;
1875 }
1876 else
1877 {
1878 /* This is a text line that may be partially visible. */
1879 r.x = window_box_left (s->w, s->area);
1880 r.width = window_box_width (s->w, s->area);
1881 r.height = s->row->visible_height;
1882 }
1883
1884 if (s->clip_head)
1885 if (r.x < s->clip_head->x)
1886 {
1887 if (r.width >= s->clip_head->x - r.x)
1888 r.width -= s->clip_head->x - r.x;
1889 else
1890 r.width = 0;
1891 r.x = s->clip_head->x;
1892 }
1893 if (s->clip_tail)
1894 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1895 {
1896 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1897 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1898 else
1899 r.width = 0;
1900 }
1901
1902 /* If S draws overlapping rows, it's sufficient to use the top and
1903 bottom of the window for clipping because this glyph string
1904 intentionally draws over other lines. */
1905 if (s->for_overlaps)
1906 {
1907 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1908 r.height = window_text_bottom_y (s->w) - r.y;
1909
1910 /* Alas, the above simple strategy does not work for the
1911 environments with anti-aliased text: if the same text is
1912 drawn onto the same place multiple times, it gets thicker.
1913 If the overlap we are processing is for the erased cursor, we
1914 take the intersection with the rectagle of the cursor. */
1915 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1916 {
1917 XRectangle rc, r_save = r;
1918
1919 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1920 rc.y = s->w->phys_cursor.y;
1921 rc.width = s->w->phys_cursor_width;
1922 rc.height = s->w->phys_cursor_height;
1923
1924 x_intersect_rectangles (&r_save, &rc, &r);
1925 }
1926 }
1927 else
1928 {
1929 /* Don't use S->y for clipping because it doesn't take partially
1930 visible lines into account. For example, it can be negative for
1931 partially visible lines at the top of a window. */
1932 if (!s->row->full_width_p
1933 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1934 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1935 else
1936 r.y = max (0, s->row->y);
1937 }
1938
1939 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1940
1941 /* If drawing the cursor, don't let glyph draw outside its
1942 advertised boundaries. Cleartype does this under some circumstances. */
1943 if (s->hl == DRAW_CURSOR)
1944 {
1945 struct glyph *glyph = s->first_glyph;
1946 int height, max_y;
1947
1948 if (s->x > r.x)
1949 {
1950 r.width -= s->x - r.x;
1951 r.x = s->x;
1952 }
1953 r.width = min (r.width, glyph->pixel_width);
1954
1955 /* If r.y is below window bottom, ensure that we still see a cursor. */
1956 height = min (glyph->ascent + glyph->descent,
1957 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1958 max_y = window_text_bottom_y (s->w) - height;
1959 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1960 if (s->ybase - glyph->ascent > max_y)
1961 {
1962 r.y = max_y;
1963 r.height = height;
1964 }
1965 else
1966 {
1967 /* Don't draw cursor glyph taller than our actual glyph. */
1968 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1969 if (height < r.height)
1970 {
1971 max_y = r.y + r.height;
1972 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1973 r.height = min (max_y - r.y, height);
1974 }
1975 }
1976 }
1977
1978 if (s->row->clip)
1979 {
1980 XRectangle r_save = r;
1981
1982 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1983 r.width = 0;
1984 }
1985
1986 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1987 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1988 {
1989 #ifdef CONVERT_FROM_XRECT
1990 CONVERT_FROM_XRECT (r, *rects);
1991 #else
1992 *rects = r;
1993 #endif
1994 return 1;
1995 }
1996 else
1997 {
1998 /* If we are processing overlapping and allowed to return
1999 multiple clipping rectangles, we exclude the row of the glyph
2000 string from the clipping rectangle. This is to avoid drawing
2001 the same text on the environment with anti-aliasing. */
2002 #ifdef CONVERT_FROM_XRECT
2003 XRectangle rs[2];
2004 #else
2005 XRectangle *rs = rects;
2006 #endif
2007 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2008
2009 if (s->for_overlaps & OVERLAPS_PRED)
2010 {
2011 rs[i] = r;
2012 if (r.y + r.height > row_y)
2013 {
2014 if (r.y < row_y)
2015 rs[i].height = row_y - r.y;
2016 else
2017 rs[i].height = 0;
2018 }
2019 i++;
2020 }
2021 if (s->for_overlaps & OVERLAPS_SUCC)
2022 {
2023 rs[i] = r;
2024 if (r.y < row_y + s->row->visible_height)
2025 {
2026 if (r.y + r.height > row_y + s->row->visible_height)
2027 {
2028 rs[i].y = row_y + s->row->visible_height;
2029 rs[i].height = r.y + r.height - rs[i].y;
2030 }
2031 else
2032 rs[i].height = 0;
2033 }
2034 i++;
2035 }
2036
2037 n = i;
2038 #ifdef CONVERT_FROM_XRECT
2039 for (i = 0; i < n; i++)
2040 CONVERT_FROM_XRECT (rs[i], rects[i]);
2041 #endif
2042 return n;
2043 }
2044 }
2045
2046 /* EXPORT:
2047 Return in *NR the clipping rectangle for glyph string S. */
2048
2049 void
2050 get_glyph_string_clip_rect (s, nr)
2051 struct glyph_string *s;
2052 NativeRectangle *nr;
2053 {
2054 get_glyph_string_clip_rects (s, nr, 1);
2055 }
2056
2057
2058 /* EXPORT:
2059 Return the position and height of the phys cursor in window W.
2060 Set w->phys_cursor_width to width of phys cursor.
2061 */
2062
2063 void
2064 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2065 struct window *w;
2066 struct glyph_row *row;
2067 struct glyph *glyph;
2068 int *xp, *yp, *heightp;
2069 {
2070 struct frame *f = XFRAME (WINDOW_FRAME (w));
2071 int x, y, wd, h, h0, y0;
2072
2073 /* Compute the width of the rectangle to draw. If on a stretch
2074 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2075 rectangle as wide as the glyph, but use a canonical character
2076 width instead. */
2077 wd = glyph->pixel_width - 1;
2078 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2079 wd++; /* Why? */
2080 #endif
2081
2082 x = w->phys_cursor.x;
2083 if (x < 0)
2084 {
2085 wd += x;
2086 x = 0;
2087 }
2088
2089 if (glyph->type == STRETCH_GLYPH
2090 && !x_stretch_cursor_p)
2091 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2092 w->phys_cursor_width = wd;
2093
2094 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2095
2096 /* If y is below window bottom, ensure that we still see a cursor. */
2097 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2098
2099 h = max (h0, glyph->ascent + glyph->descent);
2100 h0 = min (h0, glyph->ascent + glyph->descent);
2101
2102 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2103 if (y < y0)
2104 {
2105 h = max (h - (y0 - y) + 1, h0);
2106 y = y0 - 1;
2107 }
2108 else
2109 {
2110 y0 = window_text_bottom_y (w) - h0;
2111 if (y > y0)
2112 {
2113 h += y - y0;
2114 y = y0;
2115 }
2116 }
2117
2118 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2119 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2120 *heightp = h;
2121 }
2122
2123 /*
2124 * Remember which glyph the mouse is over.
2125 */
2126
2127 void
2128 remember_mouse_glyph (f, gx, gy, rect)
2129 struct frame *f;
2130 int gx, gy;
2131 NativeRectangle *rect;
2132 {
2133 Lisp_Object window;
2134 struct window *w;
2135 struct glyph_row *r, *gr, *end_row;
2136 enum window_part part;
2137 enum glyph_row_area area;
2138 int x, y, width, height;
2139
2140 /* Try to determine frame pixel position and size of the glyph under
2141 frame pixel coordinates X/Y on frame F. */
2142
2143 if (!f->glyphs_initialized_p
2144 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2145 NILP (window)))
2146 {
2147 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2148 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2149 goto virtual_glyph;
2150 }
2151
2152 w = XWINDOW (window);
2153 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2154 height = WINDOW_FRAME_LINE_HEIGHT (w);
2155
2156 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2157 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2158
2159 if (w->pseudo_window_p)
2160 {
2161 area = TEXT_AREA;
2162 part = ON_MODE_LINE; /* Don't adjust margin. */
2163 goto text_glyph;
2164 }
2165
2166 switch (part)
2167 {
2168 case ON_LEFT_MARGIN:
2169 area = LEFT_MARGIN_AREA;
2170 goto text_glyph;
2171
2172 case ON_RIGHT_MARGIN:
2173 area = RIGHT_MARGIN_AREA;
2174 goto text_glyph;
2175
2176 case ON_HEADER_LINE:
2177 case ON_MODE_LINE:
2178 gr = (part == ON_HEADER_LINE
2179 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2180 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2181 gy = gr->y;
2182 area = TEXT_AREA;
2183 goto text_glyph_row_found;
2184
2185 case ON_TEXT:
2186 area = TEXT_AREA;
2187
2188 text_glyph:
2189 gr = 0; gy = 0;
2190 for (; r <= end_row && r->enabled_p; ++r)
2191 if (r->y + r->height > y)
2192 {
2193 gr = r; gy = r->y;
2194 break;
2195 }
2196
2197 text_glyph_row_found:
2198 if (gr && gy <= y)
2199 {
2200 struct glyph *g = gr->glyphs[area];
2201 struct glyph *end = g + gr->used[area];
2202
2203 height = gr->height;
2204 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2205 if (gx + g->pixel_width > x)
2206 break;
2207
2208 if (g < end)
2209 {
2210 if (g->type == IMAGE_GLYPH)
2211 {
2212 /* Don't remember when mouse is over image, as
2213 image may have hot-spots. */
2214 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2215 return;
2216 }
2217 width = g->pixel_width;
2218 }
2219 else
2220 {
2221 /* Use nominal char spacing at end of line. */
2222 x -= gx;
2223 gx += (x / width) * width;
2224 }
2225
2226 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2227 gx += window_box_left_offset (w, area);
2228 }
2229 else
2230 {
2231 /* Use nominal line height at end of window. */
2232 gx = (x / width) * width;
2233 y -= gy;
2234 gy += (y / height) * height;
2235 }
2236 break;
2237
2238 case ON_LEFT_FRINGE:
2239 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2240 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2241 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2242 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2243 goto row_glyph;
2244
2245 case ON_RIGHT_FRINGE:
2246 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2247 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2248 : window_box_right_offset (w, TEXT_AREA));
2249 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2250 goto row_glyph;
2251
2252 case ON_SCROLL_BAR:
2253 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2254 ? 0
2255 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2256 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2257 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2258 : 0)));
2259 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2260
2261 row_glyph:
2262 gr = 0, gy = 0;
2263 for (; r <= end_row && r->enabled_p; ++r)
2264 if (r->y + r->height > y)
2265 {
2266 gr = r; gy = r->y;
2267 break;
2268 }
2269
2270 if (gr && gy <= y)
2271 height = gr->height;
2272 else
2273 {
2274 /* Use nominal line height at end of window. */
2275 y -= gy;
2276 gy += (y / height) * height;
2277 }
2278 break;
2279
2280 default:
2281 ;
2282 virtual_glyph:
2283 /* If there is no glyph under the mouse, then we divide the screen
2284 into a grid of the smallest glyph in the frame, and use that
2285 as our "glyph". */
2286
2287 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2288 round down even for negative values. */
2289 if (gx < 0)
2290 gx -= width - 1;
2291 if (gy < 0)
2292 gy -= height - 1;
2293
2294 gx = (gx / width) * width;
2295 gy = (gy / height) * height;
2296
2297 goto store_rect;
2298 }
2299
2300 gx += WINDOW_LEFT_EDGE_X (w);
2301 gy += WINDOW_TOP_EDGE_Y (w);
2302
2303 store_rect:
2304 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2305
2306 /* Visible feedback for debugging. */
2307 #if 0
2308 #if HAVE_X_WINDOWS
2309 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2310 f->output_data.x->normal_gc,
2311 gx, gy, width, height);
2312 #endif
2313 #endif
2314 }
2315
2316
2317 #endif /* HAVE_WINDOW_SYSTEM */
2318
2319 \f
2320 /***********************************************************************
2321 Lisp form evaluation
2322 ***********************************************************************/
2323
2324 /* Error handler for safe_eval and safe_call. */
2325
2326 static Lisp_Object
2327 safe_eval_handler (arg)
2328 Lisp_Object arg;
2329 {
2330 add_to_log ("Error during redisplay: %s", arg, Qnil);
2331 return Qnil;
2332 }
2333
2334
2335 /* Evaluate SEXPR and return the result, or nil if something went
2336 wrong. Prevent redisplay during the evaluation. */
2337
2338 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2339 Return the result, or nil if something went wrong. Prevent
2340 redisplay during the evaluation. */
2341
2342 Lisp_Object
2343 safe_call (nargs, args)
2344 int nargs;
2345 Lisp_Object *args;
2346 {
2347 Lisp_Object val;
2348
2349 if (inhibit_eval_during_redisplay)
2350 val = Qnil;
2351 else
2352 {
2353 int count = SPECPDL_INDEX ();
2354 struct gcpro gcpro1;
2355
2356 GCPRO1 (args[0]);
2357 gcpro1.nvars = nargs;
2358 specbind (Qinhibit_redisplay, Qt);
2359 /* Use Qt to ensure debugger does not run,
2360 so there is no possibility of wanting to redisplay. */
2361 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2362 safe_eval_handler);
2363 UNGCPRO;
2364 val = unbind_to (count, val);
2365 }
2366
2367 return val;
2368 }
2369
2370
2371 /* Call function FN with one argument ARG.
2372 Return the result, or nil if something went wrong. */
2373
2374 Lisp_Object
2375 safe_call1 (fn, arg)
2376 Lisp_Object fn, arg;
2377 {
2378 Lisp_Object args[2];
2379 args[0] = fn;
2380 args[1] = arg;
2381 return safe_call (2, args);
2382 }
2383
2384 static Lisp_Object Qeval;
2385
2386 Lisp_Object
2387 safe_eval (Lisp_Object sexpr)
2388 {
2389 return safe_call1 (Qeval, sexpr);
2390 }
2391
2392 /* Call function FN with one argument ARG.
2393 Return the result, or nil if something went wrong. */
2394
2395 Lisp_Object
2396 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2397 {
2398 Lisp_Object args[3];
2399 args[0] = fn;
2400 args[1] = arg1;
2401 args[2] = arg2;
2402 return safe_call (3, args);
2403 }
2404
2405
2406 \f
2407 /***********************************************************************
2408 Debugging
2409 ***********************************************************************/
2410
2411 #if 0
2412
2413 /* Define CHECK_IT to perform sanity checks on iterators.
2414 This is for debugging. It is too slow to do unconditionally. */
2415
2416 static void
2417 check_it (it)
2418 struct it *it;
2419 {
2420 if (it->method == GET_FROM_STRING)
2421 {
2422 xassert (STRINGP (it->string));
2423 xassert (IT_STRING_CHARPOS (*it) >= 0);
2424 }
2425 else
2426 {
2427 xassert (IT_STRING_CHARPOS (*it) < 0);
2428 if (it->method == GET_FROM_BUFFER)
2429 {
2430 /* Check that character and byte positions agree. */
2431 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2432 }
2433 }
2434
2435 if (it->dpvec)
2436 xassert (it->current.dpvec_index >= 0);
2437 else
2438 xassert (it->current.dpvec_index < 0);
2439 }
2440
2441 #define CHECK_IT(IT) check_it ((IT))
2442
2443 #else /* not 0 */
2444
2445 #define CHECK_IT(IT) (void) 0
2446
2447 #endif /* not 0 */
2448
2449
2450 #if GLYPH_DEBUG
2451
2452 /* Check that the window end of window W is what we expect it
2453 to be---the last row in the current matrix displaying text. */
2454
2455 static void
2456 check_window_end (w)
2457 struct window *w;
2458 {
2459 if (!MINI_WINDOW_P (w)
2460 && !NILP (w->window_end_valid))
2461 {
2462 struct glyph_row *row;
2463 xassert ((row = MATRIX_ROW (w->current_matrix,
2464 XFASTINT (w->window_end_vpos)),
2465 !row->enabled_p
2466 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2467 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2468 }
2469 }
2470
2471 #define CHECK_WINDOW_END(W) check_window_end ((W))
2472
2473 #else /* not GLYPH_DEBUG */
2474
2475 #define CHECK_WINDOW_END(W) (void) 0
2476
2477 #endif /* not GLYPH_DEBUG */
2478
2479
2480 \f
2481 /***********************************************************************
2482 Iterator initialization
2483 ***********************************************************************/
2484
2485 /* Initialize IT for displaying current_buffer in window W, starting
2486 at character position CHARPOS. CHARPOS < 0 means that no buffer
2487 position is specified which is useful when the iterator is assigned
2488 a position later. BYTEPOS is the byte position corresponding to
2489 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2490
2491 If ROW is not null, calls to produce_glyphs with IT as parameter
2492 will produce glyphs in that row.
2493
2494 BASE_FACE_ID is the id of a base face to use. It must be one of
2495 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2496 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2497 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2498
2499 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2500 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2501 will be initialized to use the corresponding mode line glyph row of
2502 the desired matrix of W. */
2503
2504 void
2505 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2506 struct it *it;
2507 struct window *w;
2508 int charpos, bytepos;
2509 struct glyph_row *row;
2510 enum face_id base_face_id;
2511 {
2512 int highlight_region_p;
2513 enum face_id remapped_base_face_id = base_face_id;
2514
2515 /* Some precondition checks. */
2516 xassert (w != NULL && it != NULL);
2517 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2518 && charpos <= ZV));
2519
2520 /* If face attributes have been changed since the last redisplay,
2521 free realized faces now because they depend on face definitions
2522 that might have changed. Don't free faces while there might be
2523 desired matrices pending which reference these faces. */
2524 if (face_change_count && !inhibit_free_realized_faces)
2525 {
2526 face_change_count = 0;
2527 free_all_realized_faces (Qnil);
2528 }
2529
2530 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2531 if (! NILP (Vface_remapping_alist))
2532 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2533
2534 /* Use one of the mode line rows of W's desired matrix if
2535 appropriate. */
2536 if (row == NULL)
2537 {
2538 if (base_face_id == MODE_LINE_FACE_ID
2539 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2540 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2541 else if (base_face_id == HEADER_LINE_FACE_ID)
2542 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2543 }
2544
2545 /* Clear IT. */
2546 bzero (it, sizeof *it);
2547 it->current.overlay_string_index = -1;
2548 it->current.dpvec_index = -1;
2549 it->base_face_id = remapped_base_face_id;
2550 it->string = Qnil;
2551 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2552
2553 /* The window in which we iterate over current_buffer: */
2554 XSETWINDOW (it->window, w);
2555 it->w = w;
2556 it->f = XFRAME (w->frame);
2557
2558 it->cmp_it.id = -1;
2559
2560 /* Extra space between lines (on window systems only). */
2561 if (base_face_id == DEFAULT_FACE_ID
2562 && FRAME_WINDOW_P (it->f))
2563 {
2564 if (NATNUMP (current_buffer->extra_line_spacing))
2565 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2566 else if (FLOATP (current_buffer->extra_line_spacing))
2567 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2568 * FRAME_LINE_HEIGHT (it->f));
2569 else if (it->f->extra_line_spacing > 0)
2570 it->extra_line_spacing = it->f->extra_line_spacing;
2571 it->max_extra_line_spacing = 0;
2572 }
2573
2574 /* If realized faces have been removed, e.g. because of face
2575 attribute changes of named faces, recompute them. When running
2576 in batch mode, the face cache of the initial frame is null. If
2577 we happen to get called, make a dummy face cache. */
2578 if (FRAME_FACE_CACHE (it->f) == NULL)
2579 init_frame_faces (it->f);
2580 if (FRAME_FACE_CACHE (it->f)->used == 0)
2581 recompute_basic_faces (it->f);
2582
2583 /* Current value of the `slice', `space-width', and 'height' properties. */
2584 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2585 it->space_width = Qnil;
2586 it->font_height = Qnil;
2587 it->override_ascent = -1;
2588
2589 /* Are control characters displayed as `^C'? */
2590 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2591
2592 /* -1 means everything between a CR and the following line end
2593 is invisible. >0 means lines indented more than this value are
2594 invisible. */
2595 it->selective = (INTEGERP (current_buffer->selective_display)
2596 ? XFASTINT (current_buffer->selective_display)
2597 : (!NILP (current_buffer->selective_display)
2598 ? -1 : 0));
2599 it->selective_display_ellipsis_p
2600 = !NILP (current_buffer->selective_display_ellipses);
2601
2602 /* Display table to use. */
2603 it->dp = window_display_table (w);
2604
2605 /* Are multibyte characters enabled in current_buffer? */
2606 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2607
2608 /* Do we need to reorder bidirectional text? */
2609 it->bidi_p = !NILP (current_buffer->bidi_display_reordering);
2610
2611 /* Non-zero if we should highlight the region. */
2612 highlight_region_p
2613 = (!NILP (Vtransient_mark_mode)
2614 && !NILP (current_buffer->mark_active)
2615 && XMARKER (current_buffer->mark)->buffer != 0);
2616
2617 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2618 start and end of a visible region in window IT->w. Set both to
2619 -1 to indicate no region. */
2620 if (highlight_region_p
2621 /* Maybe highlight only in selected window. */
2622 && (/* Either show region everywhere. */
2623 highlight_nonselected_windows
2624 /* Or show region in the selected window. */
2625 || w == XWINDOW (selected_window)
2626 /* Or show the region if we are in the mini-buffer and W is
2627 the window the mini-buffer refers to. */
2628 || (MINI_WINDOW_P (XWINDOW (selected_window))
2629 && WINDOWP (minibuf_selected_window)
2630 && w == XWINDOW (minibuf_selected_window))))
2631 {
2632 int charpos = marker_position (current_buffer->mark);
2633 it->region_beg_charpos = min (PT, charpos);
2634 it->region_end_charpos = max (PT, charpos);
2635 }
2636 else
2637 it->region_beg_charpos = it->region_end_charpos = -1;
2638
2639 /* Get the position at which the redisplay_end_trigger hook should
2640 be run, if it is to be run at all. */
2641 if (MARKERP (w->redisplay_end_trigger)
2642 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2643 it->redisplay_end_trigger_charpos
2644 = marker_position (w->redisplay_end_trigger);
2645 else if (INTEGERP (w->redisplay_end_trigger))
2646 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2647
2648 /* Correct bogus values of tab_width. */
2649 it->tab_width = XINT (current_buffer->tab_width);
2650 if (it->tab_width <= 0 || it->tab_width > 1000)
2651 it->tab_width = 8;
2652
2653 /* Are lines in the display truncated? */
2654 if (base_face_id != DEFAULT_FACE_ID
2655 || XINT (it->w->hscroll)
2656 || (! WINDOW_FULL_WIDTH_P (it->w)
2657 && ((!NILP (Vtruncate_partial_width_windows)
2658 && !INTEGERP (Vtruncate_partial_width_windows))
2659 || (INTEGERP (Vtruncate_partial_width_windows)
2660 && (WINDOW_TOTAL_COLS (it->w)
2661 < XINT (Vtruncate_partial_width_windows))))))
2662 it->line_wrap = TRUNCATE;
2663 else if (NILP (current_buffer->truncate_lines))
2664 it->line_wrap = NILP (current_buffer->word_wrap)
2665 ? WINDOW_WRAP : WORD_WRAP;
2666 else
2667 it->line_wrap = TRUNCATE;
2668
2669 /* Get dimensions of truncation and continuation glyphs. These are
2670 displayed as fringe bitmaps under X, so we don't need them for such
2671 frames. */
2672 if (!FRAME_WINDOW_P (it->f))
2673 {
2674 if (it->line_wrap == TRUNCATE)
2675 {
2676 /* We will need the truncation glyph. */
2677 xassert (it->glyph_row == NULL);
2678 produce_special_glyphs (it, IT_TRUNCATION);
2679 it->truncation_pixel_width = it->pixel_width;
2680 }
2681 else
2682 {
2683 /* We will need the continuation glyph. */
2684 xassert (it->glyph_row == NULL);
2685 produce_special_glyphs (it, IT_CONTINUATION);
2686 it->continuation_pixel_width = it->pixel_width;
2687 }
2688
2689 /* Reset these values to zero because the produce_special_glyphs
2690 above has changed them. */
2691 it->pixel_width = it->ascent = it->descent = 0;
2692 it->phys_ascent = it->phys_descent = 0;
2693 }
2694
2695 /* Set this after getting the dimensions of truncation and
2696 continuation glyphs, so that we don't produce glyphs when calling
2697 produce_special_glyphs, above. */
2698 it->glyph_row = row;
2699 it->area = TEXT_AREA;
2700
2701 /* Forget any previous info about this row being reversed. */
2702 if (it->glyph_row)
2703 it->glyph_row->reversed_p = 0;
2704
2705 /* Get the dimensions of the display area. The display area
2706 consists of the visible window area plus a horizontally scrolled
2707 part to the left of the window. All x-values are relative to the
2708 start of this total display area. */
2709 if (base_face_id != DEFAULT_FACE_ID)
2710 {
2711 /* Mode lines, menu bar in terminal frames. */
2712 it->first_visible_x = 0;
2713 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2714 }
2715 else
2716 {
2717 it->first_visible_x
2718 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2719 it->last_visible_x = (it->first_visible_x
2720 + window_box_width (w, TEXT_AREA));
2721
2722 /* If we truncate lines, leave room for the truncator glyph(s) at
2723 the right margin. Otherwise, leave room for the continuation
2724 glyph(s). Truncation and continuation glyphs are not inserted
2725 for window-based redisplay. */
2726 if (!FRAME_WINDOW_P (it->f))
2727 {
2728 if (it->line_wrap == TRUNCATE)
2729 it->last_visible_x -= it->truncation_pixel_width;
2730 else
2731 it->last_visible_x -= it->continuation_pixel_width;
2732 }
2733
2734 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2735 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2736 }
2737
2738 /* Leave room for a border glyph. */
2739 if (!FRAME_WINDOW_P (it->f)
2740 && !WINDOW_RIGHTMOST_P (it->w))
2741 it->last_visible_x -= 1;
2742
2743 it->last_visible_y = window_text_bottom_y (w);
2744
2745 /* For mode lines and alike, arrange for the first glyph having a
2746 left box line if the face specifies a box. */
2747 if (base_face_id != DEFAULT_FACE_ID)
2748 {
2749 struct face *face;
2750
2751 it->face_id = remapped_base_face_id;
2752
2753 /* If we have a boxed mode line, make the first character appear
2754 with a left box line. */
2755 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2756 if (face->box != FACE_NO_BOX)
2757 it->start_of_box_run_p = 1;
2758 }
2759
2760 /* If we are to reorder bidirectional text, init the bidi
2761 iterator. */
2762 if (it->bidi_p)
2763 {
2764 /* Note the paragraph direction that this buffer wants to
2765 use. */
2766 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2767 it->paragraph_embedding = L2R;
2768 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2769 it->paragraph_embedding = R2L;
2770 else
2771 it->paragraph_embedding = NEUTRAL_DIR;
2772 bidi_init_it (charpos, bytepos, &it->bidi_it);
2773 }
2774
2775 /* If a buffer position was specified, set the iterator there,
2776 getting overlays and face properties from that position. */
2777 if (charpos >= BUF_BEG (current_buffer))
2778 {
2779 it->end_charpos = ZV;
2780 it->face_id = -1;
2781 IT_CHARPOS (*it) = charpos;
2782
2783 /* Compute byte position if not specified. */
2784 if (bytepos < charpos)
2785 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2786 else
2787 IT_BYTEPOS (*it) = bytepos;
2788
2789 it->start = it->current;
2790
2791 /* Compute faces etc. */
2792 reseat (it, it->current.pos, 1);
2793 }
2794
2795 CHECK_IT (it);
2796 }
2797
2798
2799 /* Initialize IT for the display of window W with window start POS. */
2800
2801 void
2802 start_display (it, w, pos)
2803 struct it *it;
2804 struct window *w;
2805 struct text_pos pos;
2806 {
2807 struct glyph_row *row;
2808 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2809
2810 row = w->desired_matrix->rows + first_vpos;
2811 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2812 it->first_vpos = first_vpos;
2813
2814 /* Don't reseat to previous visible line start if current start
2815 position is in a string or image. */
2816 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2817 {
2818 int start_at_line_beg_p;
2819 int first_y = it->current_y;
2820
2821 /* If window start is not at a line start, skip forward to POS to
2822 get the correct continuation lines width. */
2823 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2824 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2825 if (!start_at_line_beg_p)
2826 {
2827 int new_x;
2828
2829 reseat_at_previous_visible_line_start (it);
2830 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2831
2832 new_x = it->current_x + it->pixel_width;
2833
2834 /* If lines are continued, this line may end in the middle
2835 of a multi-glyph character (e.g. a control character
2836 displayed as \003, or in the middle of an overlay
2837 string). In this case move_it_to above will not have
2838 taken us to the start of the continuation line but to the
2839 end of the continued line. */
2840 if (it->current_x > 0
2841 && it->line_wrap != TRUNCATE /* Lines are continued. */
2842 && (/* And glyph doesn't fit on the line. */
2843 new_x > it->last_visible_x
2844 /* Or it fits exactly and we're on a window
2845 system frame. */
2846 || (new_x == it->last_visible_x
2847 && FRAME_WINDOW_P (it->f))))
2848 {
2849 if (it->current.dpvec_index >= 0
2850 || it->current.overlay_string_index >= 0)
2851 {
2852 set_iterator_to_next (it, 1);
2853 move_it_in_display_line_to (it, -1, -1, 0);
2854 }
2855
2856 it->continuation_lines_width += it->current_x;
2857 }
2858
2859 /* We're starting a new display line, not affected by the
2860 height of the continued line, so clear the appropriate
2861 fields in the iterator structure. */
2862 it->max_ascent = it->max_descent = 0;
2863 it->max_phys_ascent = it->max_phys_descent = 0;
2864
2865 it->current_y = first_y;
2866 it->vpos = 0;
2867 it->current_x = it->hpos = 0;
2868 }
2869 }
2870 }
2871
2872
2873 /* Return 1 if POS is a position in ellipses displayed for invisible
2874 text. W is the window we display, for text property lookup. */
2875
2876 static int
2877 in_ellipses_for_invisible_text_p (pos, w)
2878 struct display_pos *pos;
2879 struct window *w;
2880 {
2881 Lisp_Object prop, window;
2882 int ellipses_p = 0;
2883 int charpos = CHARPOS (pos->pos);
2884
2885 /* If POS specifies a position in a display vector, this might
2886 be for an ellipsis displayed for invisible text. We won't
2887 get the iterator set up for delivering that ellipsis unless
2888 we make sure that it gets aware of the invisible text. */
2889 if (pos->dpvec_index >= 0
2890 && pos->overlay_string_index < 0
2891 && CHARPOS (pos->string_pos) < 0
2892 && charpos > BEGV
2893 && (XSETWINDOW (window, w),
2894 prop = Fget_char_property (make_number (charpos),
2895 Qinvisible, window),
2896 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2897 {
2898 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2899 window);
2900 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2901 }
2902
2903 return ellipses_p;
2904 }
2905
2906
2907 /* Initialize IT for stepping through current_buffer in window W,
2908 starting at position POS that includes overlay string and display
2909 vector/ control character translation position information. Value
2910 is zero if there are overlay strings with newlines at POS. */
2911
2912 static int
2913 init_from_display_pos (it, w, pos)
2914 struct it *it;
2915 struct window *w;
2916 struct display_pos *pos;
2917 {
2918 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2919 int i, overlay_strings_with_newlines = 0;
2920
2921 /* If POS specifies a position in a display vector, this might
2922 be for an ellipsis displayed for invisible text. We won't
2923 get the iterator set up for delivering that ellipsis unless
2924 we make sure that it gets aware of the invisible text. */
2925 if (in_ellipses_for_invisible_text_p (pos, w))
2926 {
2927 --charpos;
2928 bytepos = 0;
2929 }
2930
2931 /* Keep in mind: the call to reseat in init_iterator skips invisible
2932 text, so we might end up at a position different from POS. This
2933 is only a problem when POS is a row start after a newline and an
2934 overlay starts there with an after-string, and the overlay has an
2935 invisible property. Since we don't skip invisible text in
2936 display_line and elsewhere immediately after consuming the
2937 newline before the row start, such a POS will not be in a string,
2938 but the call to init_iterator below will move us to the
2939 after-string. */
2940 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2941
2942 /* This only scans the current chunk -- it should scan all chunks.
2943 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2944 to 16 in 22.1 to make this a lesser problem. */
2945 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2946 {
2947 const char *s = SDATA (it->overlay_strings[i]);
2948 const char *e = s + SBYTES (it->overlay_strings[i]);
2949
2950 while (s < e && *s != '\n')
2951 ++s;
2952
2953 if (s < e)
2954 {
2955 overlay_strings_with_newlines = 1;
2956 break;
2957 }
2958 }
2959
2960 /* If position is within an overlay string, set up IT to the right
2961 overlay string. */
2962 if (pos->overlay_string_index >= 0)
2963 {
2964 int relative_index;
2965
2966 /* If the first overlay string happens to have a `display'
2967 property for an image, the iterator will be set up for that
2968 image, and we have to undo that setup first before we can
2969 correct the overlay string index. */
2970 if (it->method == GET_FROM_IMAGE)
2971 pop_it (it);
2972
2973 /* We already have the first chunk of overlay strings in
2974 IT->overlay_strings. Load more until the one for
2975 pos->overlay_string_index is in IT->overlay_strings. */
2976 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2977 {
2978 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2979 it->current.overlay_string_index = 0;
2980 while (n--)
2981 {
2982 load_overlay_strings (it, 0);
2983 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2984 }
2985 }
2986
2987 it->current.overlay_string_index = pos->overlay_string_index;
2988 relative_index = (it->current.overlay_string_index
2989 % OVERLAY_STRING_CHUNK_SIZE);
2990 it->string = it->overlay_strings[relative_index];
2991 xassert (STRINGP (it->string));
2992 it->current.string_pos = pos->string_pos;
2993 it->method = GET_FROM_STRING;
2994 }
2995
2996 if (CHARPOS (pos->string_pos) >= 0)
2997 {
2998 /* Recorded position is not in an overlay string, but in another
2999 string. This can only be a string from a `display' property.
3000 IT should already be filled with that string. */
3001 it->current.string_pos = pos->string_pos;
3002 xassert (STRINGP (it->string));
3003 }
3004
3005 /* Restore position in display vector translations, control
3006 character translations or ellipses. */
3007 if (pos->dpvec_index >= 0)
3008 {
3009 if (it->dpvec == NULL)
3010 get_next_display_element (it);
3011 xassert (it->dpvec && it->current.dpvec_index == 0);
3012 it->current.dpvec_index = pos->dpvec_index;
3013 }
3014
3015 CHECK_IT (it);
3016 return !overlay_strings_with_newlines;
3017 }
3018
3019
3020 /* Initialize IT for stepping through current_buffer in window W
3021 starting at ROW->start. */
3022
3023 static void
3024 init_to_row_start (it, w, row)
3025 struct it *it;
3026 struct window *w;
3027 struct glyph_row *row;
3028 {
3029 init_from_display_pos (it, w, &row->start);
3030 it->start = row->start;
3031 it->continuation_lines_width = row->continuation_lines_width;
3032 CHECK_IT (it);
3033 }
3034
3035
3036 /* Initialize IT for stepping through current_buffer in window W
3037 starting in the line following ROW, i.e. starting at ROW->end.
3038 Value is zero if there are overlay strings with newlines at ROW's
3039 end position. */
3040
3041 static int
3042 init_to_row_end (it, w, row)
3043 struct it *it;
3044 struct window *w;
3045 struct glyph_row *row;
3046 {
3047 int success = 0;
3048
3049 if (init_from_display_pos (it, w, &row->end))
3050 {
3051 if (row->continued_p)
3052 it->continuation_lines_width
3053 = row->continuation_lines_width + row->pixel_width;
3054 CHECK_IT (it);
3055 success = 1;
3056 }
3057
3058 return success;
3059 }
3060
3061
3062
3063 \f
3064 /***********************************************************************
3065 Text properties
3066 ***********************************************************************/
3067
3068 /* Called when IT reaches IT->stop_charpos. Handle text property and
3069 overlay changes. Set IT->stop_charpos to the next position where
3070 to stop. */
3071
3072 static void
3073 handle_stop (it)
3074 struct it *it;
3075 {
3076 enum prop_handled handled;
3077 int handle_overlay_change_p;
3078 struct props *p;
3079
3080 it->dpvec = NULL;
3081 it->current.dpvec_index = -1;
3082 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3083 it->ignore_overlay_strings_at_pos_p = 0;
3084 it->ellipsis_p = 0;
3085
3086 /* Use face of preceding text for ellipsis (if invisible) */
3087 if (it->selective_display_ellipsis_p)
3088 it->saved_face_id = it->face_id;
3089
3090 do
3091 {
3092 handled = HANDLED_NORMALLY;
3093
3094 /* Call text property handlers. */
3095 for (p = it_props; p->handler; ++p)
3096 {
3097 handled = p->handler (it);
3098
3099 if (handled == HANDLED_RECOMPUTE_PROPS)
3100 break;
3101 else if (handled == HANDLED_RETURN)
3102 {
3103 /* We still want to show before and after strings from
3104 overlays even if the actual buffer text is replaced. */
3105 if (!handle_overlay_change_p
3106 || it->sp > 1
3107 || !get_overlay_strings_1 (it, 0, 0))
3108 {
3109 if (it->ellipsis_p)
3110 setup_for_ellipsis (it, 0);
3111 /* When handling a display spec, we might load an
3112 empty string. In that case, discard it here. We
3113 used to discard it in handle_single_display_spec,
3114 but that causes get_overlay_strings_1, above, to
3115 ignore overlay strings that we must check. */
3116 if (STRINGP (it->string) && !SCHARS (it->string))
3117 pop_it (it);
3118 return;
3119 }
3120 else if (STRINGP (it->string) && !SCHARS (it->string))
3121 pop_it (it);
3122 else
3123 {
3124 it->ignore_overlay_strings_at_pos_p = 1;
3125 it->string_from_display_prop_p = 0;
3126 handle_overlay_change_p = 0;
3127 }
3128 handled = HANDLED_RECOMPUTE_PROPS;
3129 break;
3130 }
3131 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3132 handle_overlay_change_p = 0;
3133 }
3134
3135 if (handled != HANDLED_RECOMPUTE_PROPS)
3136 {
3137 /* Don't check for overlay strings below when set to deliver
3138 characters from a display vector. */
3139 if (it->method == GET_FROM_DISPLAY_VECTOR)
3140 handle_overlay_change_p = 0;
3141
3142 /* Handle overlay changes.
3143 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3144 if it finds overlays. */
3145 if (handle_overlay_change_p)
3146 handled = handle_overlay_change (it);
3147 }
3148
3149 if (it->ellipsis_p)
3150 {
3151 setup_for_ellipsis (it, 0);
3152 break;
3153 }
3154 }
3155 while (handled == HANDLED_RECOMPUTE_PROPS);
3156
3157 /* Determine where to stop next. */
3158 if (handled == HANDLED_NORMALLY)
3159 compute_stop_pos (it);
3160 }
3161
3162
3163 /* Compute IT->stop_charpos from text property and overlay change
3164 information for IT's current position. */
3165
3166 static void
3167 compute_stop_pos (it)
3168 struct it *it;
3169 {
3170 register INTERVAL iv, next_iv;
3171 Lisp_Object object, limit, position;
3172 EMACS_INT charpos, bytepos;
3173
3174 /* If nowhere else, stop at the end. */
3175 it->stop_charpos = it->end_charpos;
3176
3177 if (STRINGP (it->string))
3178 {
3179 /* Strings are usually short, so don't limit the search for
3180 properties. */
3181 object = it->string;
3182 limit = Qnil;
3183 charpos = IT_STRING_CHARPOS (*it);
3184 bytepos = IT_STRING_BYTEPOS (*it);
3185 }
3186 else
3187 {
3188 EMACS_INT pos;
3189
3190 /* If next overlay change is in front of the current stop pos
3191 (which is IT->end_charpos), stop there. Note: value of
3192 next_overlay_change is point-max if no overlay change
3193 follows. */
3194 charpos = IT_CHARPOS (*it);
3195 bytepos = IT_BYTEPOS (*it);
3196 pos = next_overlay_change (charpos);
3197 if (pos < it->stop_charpos)
3198 it->stop_charpos = pos;
3199
3200 /* If showing the region, we have to stop at the region
3201 start or end because the face might change there. */
3202 if (it->region_beg_charpos > 0)
3203 {
3204 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3205 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3206 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3207 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3208 }
3209
3210 /* Set up variables for computing the stop position from text
3211 property changes. */
3212 XSETBUFFER (object, current_buffer);
3213 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3214 }
3215
3216 /* Get the interval containing IT's position. Value is a null
3217 interval if there isn't such an interval. */
3218 position = make_number (charpos);
3219 iv = validate_interval_range (object, &position, &position, 0);
3220 if (!NULL_INTERVAL_P (iv))
3221 {
3222 Lisp_Object values_here[LAST_PROP_IDX];
3223 struct props *p;
3224
3225 /* Get properties here. */
3226 for (p = it_props; p->handler; ++p)
3227 values_here[p->idx] = textget (iv->plist, *p->name);
3228
3229 /* Look for an interval following iv that has different
3230 properties. */
3231 for (next_iv = next_interval (iv);
3232 (!NULL_INTERVAL_P (next_iv)
3233 && (NILP (limit)
3234 || XFASTINT (limit) > next_iv->position));
3235 next_iv = next_interval (next_iv))
3236 {
3237 for (p = it_props; p->handler; ++p)
3238 {
3239 Lisp_Object new_value;
3240
3241 new_value = textget (next_iv->plist, *p->name);
3242 if (!EQ (values_here[p->idx], new_value))
3243 break;
3244 }
3245
3246 if (p->handler)
3247 break;
3248 }
3249
3250 if (!NULL_INTERVAL_P (next_iv))
3251 {
3252 if (INTEGERP (limit)
3253 && next_iv->position >= XFASTINT (limit))
3254 /* No text property change up to limit. */
3255 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3256 else
3257 /* Text properties change in next_iv. */
3258 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3259 }
3260 }
3261
3262 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3263 it->stop_charpos, it->string);
3264
3265 xassert (STRINGP (it->string)
3266 || (it->stop_charpos >= BEGV
3267 && it->stop_charpos >= IT_CHARPOS (*it)));
3268 }
3269
3270
3271 /* Return the position of the next overlay change after POS in
3272 current_buffer. Value is point-max if no overlay change
3273 follows. This is like `next-overlay-change' but doesn't use
3274 xmalloc. */
3275
3276 static EMACS_INT
3277 next_overlay_change (pos)
3278 EMACS_INT pos;
3279 {
3280 int noverlays;
3281 EMACS_INT endpos;
3282 Lisp_Object *overlays;
3283 int i;
3284
3285 /* Get all overlays at the given position. */
3286 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3287
3288 /* If any of these overlays ends before endpos,
3289 use its ending point instead. */
3290 for (i = 0; i < noverlays; ++i)
3291 {
3292 Lisp_Object oend;
3293 EMACS_INT oendpos;
3294
3295 oend = OVERLAY_END (overlays[i]);
3296 oendpos = OVERLAY_POSITION (oend);
3297 endpos = min (endpos, oendpos);
3298 }
3299
3300 return endpos;
3301 }
3302
3303
3304 \f
3305 /***********************************************************************
3306 Fontification
3307 ***********************************************************************/
3308
3309 /* Handle changes in the `fontified' property of the current buffer by
3310 calling hook functions from Qfontification_functions to fontify
3311 regions of text. */
3312
3313 static enum prop_handled
3314 handle_fontified_prop (it)
3315 struct it *it;
3316 {
3317 Lisp_Object prop, pos;
3318 enum prop_handled handled = HANDLED_NORMALLY;
3319
3320 if (!NILP (Vmemory_full))
3321 return handled;
3322
3323 /* Get the value of the `fontified' property at IT's current buffer
3324 position. (The `fontified' property doesn't have a special
3325 meaning in strings.) If the value is nil, call functions from
3326 Qfontification_functions. */
3327 if (!STRINGP (it->string)
3328 && it->s == NULL
3329 && !NILP (Vfontification_functions)
3330 && !NILP (Vrun_hooks)
3331 && (pos = make_number (IT_CHARPOS (*it)),
3332 prop = Fget_char_property (pos, Qfontified, Qnil),
3333 /* Ignore the special cased nil value always present at EOB since
3334 no amount of fontifying will be able to change it. */
3335 NILP (prop) && IT_CHARPOS (*it) < Z))
3336 {
3337 int count = SPECPDL_INDEX ();
3338 Lisp_Object val;
3339
3340 val = Vfontification_functions;
3341 specbind (Qfontification_functions, Qnil);
3342
3343 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3344 safe_call1 (val, pos);
3345 else
3346 {
3347 Lisp_Object globals, fn;
3348 struct gcpro gcpro1, gcpro2;
3349
3350 globals = Qnil;
3351 GCPRO2 (val, globals);
3352
3353 for (; CONSP (val); val = XCDR (val))
3354 {
3355 fn = XCAR (val);
3356
3357 if (EQ (fn, Qt))
3358 {
3359 /* A value of t indicates this hook has a local
3360 binding; it means to run the global binding too.
3361 In a global value, t should not occur. If it
3362 does, we must ignore it to avoid an endless
3363 loop. */
3364 for (globals = Fdefault_value (Qfontification_functions);
3365 CONSP (globals);
3366 globals = XCDR (globals))
3367 {
3368 fn = XCAR (globals);
3369 if (!EQ (fn, Qt))
3370 safe_call1 (fn, pos);
3371 }
3372 }
3373 else
3374 safe_call1 (fn, pos);
3375 }
3376
3377 UNGCPRO;
3378 }
3379
3380 unbind_to (count, Qnil);
3381
3382 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3383 something. This avoids an endless loop if they failed to
3384 fontify the text for which reason ever. */
3385 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3386 handled = HANDLED_RECOMPUTE_PROPS;
3387 }
3388
3389 return handled;
3390 }
3391
3392
3393 \f
3394 /***********************************************************************
3395 Faces
3396 ***********************************************************************/
3397
3398 /* Set up iterator IT from face properties at its current position.
3399 Called from handle_stop. */
3400
3401 static enum prop_handled
3402 handle_face_prop (it)
3403 struct it *it;
3404 {
3405 int new_face_id;
3406 EMACS_INT next_stop;
3407
3408 if (!STRINGP (it->string))
3409 {
3410 new_face_id
3411 = face_at_buffer_position (it->w,
3412 IT_CHARPOS (*it),
3413 it->region_beg_charpos,
3414 it->region_end_charpos,
3415 &next_stop,
3416 (IT_CHARPOS (*it)
3417 + TEXT_PROP_DISTANCE_LIMIT),
3418 0, it->base_face_id);
3419
3420 /* Is this a start of a run of characters with box face?
3421 Caveat: this can be called for a freshly initialized
3422 iterator; face_id is -1 in this case. We know that the new
3423 face will not change until limit, i.e. if the new face has a
3424 box, all characters up to limit will have one. But, as
3425 usual, we don't know whether limit is really the end. */
3426 if (new_face_id != it->face_id)
3427 {
3428 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3429
3430 /* If new face has a box but old face has not, this is
3431 the start of a run of characters with box, i.e. it has
3432 a shadow on the left side. The value of face_id of the
3433 iterator will be -1 if this is the initial call that gets
3434 the face. In this case, we have to look in front of IT's
3435 position and see whether there is a face != new_face_id. */
3436 it->start_of_box_run_p
3437 = (new_face->box != FACE_NO_BOX
3438 && (it->face_id >= 0
3439 || IT_CHARPOS (*it) == BEG
3440 || new_face_id != face_before_it_pos (it)));
3441 it->face_box_p = new_face->box != FACE_NO_BOX;
3442 }
3443 }
3444 else
3445 {
3446 int base_face_id, bufpos;
3447 int i;
3448 Lisp_Object from_overlay
3449 = (it->current.overlay_string_index >= 0
3450 ? it->string_overlays[it->current.overlay_string_index]
3451 : Qnil);
3452
3453 /* See if we got to this string directly or indirectly from
3454 an overlay property. That includes the before-string or
3455 after-string of an overlay, strings in display properties
3456 provided by an overlay, their text properties, etc.
3457
3458 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3459 if (! NILP (from_overlay))
3460 for (i = it->sp - 1; i >= 0; i--)
3461 {
3462 if (it->stack[i].current.overlay_string_index >= 0)
3463 from_overlay
3464 = it->string_overlays[it->stack[i].current.overlay_string_index];
3465 else if (! NILP (it->stack[i].from_overlay))
3466 from_overlay = it->stack[i].from_overlay;
3467
3468 if (!NILP (from_overlay))
3469 break;
3470 }
3471
3472 if (! NILP (from_overlay))
3473 {
3474 bufpos = IT_CHARPOS (*it);
3475 /* For a string from an overlay, the base face depends
3476 only on text properties and ignores overlays. */
3477 base_face_id
3478 = face_for_overlay_string (it->w,
3479 IT_CHARPOS (*it),
3480 it->region_beg_charpos,
3481 it->region_end_charpos,
3482 &next_stop,
3483 (IT_CHARPOS (*it)
3484 + TEXT_PROP_DISTANCE_LIMIT),
3485 0,
3486 from_overlay);
3487 }
3488 else
3489 {
3490 bufpos = 0;
3491
3492 /* For strings from a `display' property, use the face at
3493 IT's current buffer position as the base face to merge
3494 with, so that overlay strings appear in the same face as
3495 surrounding text, unless they specify their own
3496 faces. */
3497 base_face_id = underlying_face_id (it);
3498 }
3499
3500 new_face_id = face_at_string_position (it->w,
3501 it->string,
3502 IT_STRING_CHARPOS (*it),
3503 bufpos,
3504 it->region_beg_charpos,
3505 it->region_end_charpos,
3506 &next_stop,
3507 base_face_id, 0);
3508
3509 /* Is this a start of a run of characters with box? Caveat:
3510 this can be called for a freshly allocated iterator; face_id
3511 is -1 is this case. We know that the new face will not
3512 change until the next check pos, i.e. if the new face has a
3513 box, all characters up to that position will have a
3514 box. But, as usual, we don't know whether that position
3515 is really the end. */
3516 if (new_face_id != it->face_id)
3517 {
3518 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3519 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3520
3521 /* If new face has a box but old face hasn't, this is the
3522 start of a run of characters with box, i.e. it has a
3523 shadow on the left side. */
3524 it->start_of_box_run_p
3525 = new_face->box && (old_face == NULL || !old_face->box);
3526 it->face_box_p = new_face->box != FACE_NO_BOX;
3527 }
3528 }
3529
3530 it->face_id = new_face_id;
3531 return HANDLED_NORMALLY;
3532 }
3533
3534
3535 /* Return the ID of the face ``underlying'' IT's current position,
3536 which is in a string. If the iterator is associated with a
3537 buffer, return the face at IT's current buffer position.
3538 Otherwise, use the iterator's base_face_id. */
3539
3540 static int
3541 underlying_face_id (it)
3542 struct it *it;
3543 {
3544 int face_id = it->base_face_id, i;
3545
3546 xassert (STRINGP (it->string));
3547
3548 for (i = it->sp - 1; i >= 0; --i)
3549 if (NILP (it->stack[i].string))
3550 face_id = it->stack[i].face_id;
3551
3552 return face_id;
3553 }
3554
3555
3556 /* Compute the face one character before or after the current position
3557 of IT. BEFORE_P non-zero means get the face in front of IT's
3558 position. Value is the id of the face. */
3559
3560 static int
3561 face_before_or_after_it_pos (it, before_p)
3562 struct it *it;
3563 int before_p;
3564 {
3565 int face_id, limit;
3566 EMACS_INT next_check_charpos;
3567 struct text_pos pos;
3568
3569 xassert (it->s == NULL);
3570
3571 if (STRINGP (it->string))
3572 {
3573 int bufpos, base_face_id;
3574
3575 /* No face change past the end of the string (for the case
3576 we are padding with spaces). No face change before the
3577 string start. */
3578 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3579 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3580 return it->face_id;
3581
3582 /* Set pos to the position before or after IT's current position. */
3583 if (before_p)
3584 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3585 else
3586 /* For composition, we must check the character after the
3587 composition. */
3588 pos = (it->what == IT_COMPOSITION
3589 ? string_pos (IT_STRING_CHARPOS (*it)
3590 + it->cmp_it.nchars, it->string)
3591 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3592
3593 if (it->current.overlay_string_index >= 0)
3594 bufpos = IT_CHARPOS (*it);
3595 else
3596 bufpos = 0;
3597
3598 base_face_id = underlying_face_id (it);
3599
3600 /* Get the face for ASCII, or unibyte. */
3601 face_id = face_at_string_position (it->w,
3602 it->string,
3603 CHARPOS (pos),
3604 bufpos,
3605 it->region_beg_charpos,
3606 it->region_end_charpos,
3607 &next_check_charpos,
3608 base_face_id, 0);
3609
3610 /* Correct the face for charsets different from ASCII. Do it
3611 for the multibyte case only. The face returned above is
3612 suitable for unibyte text if IT->string is unibyte. */
3613 if (STRING_MULTIBYTE (it->string))
3614 {
3615 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3616 int rest = SBYTES (it->string) - BYTEPOS (pos);
3617 int c, len;
3618 struct face *face = FACE_FROM_ID (it->f, face_id);
3619
3620 c = string_char_and_length (p, &len);
3621 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3622 }
3623 }
3624 else
3625 {
3626 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3627 || (IT_CHARPOS (*it) <= BEGV && before_p))
3628 return it->face_id;
3629
3630 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3631 pos = it->current.pos;
3632
3633 if (before_p)
3634 DEC_TEXT_POS (pos, it->multibyte_p);
3635 else
3636 {
3637 if (it->what == IT_COMPOSITION)
3638 /* For composition, we must check the position after the
3639 composition. */
3640 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3641 else
3642 INC_TEXT_POS (pos, it->multibyte_p);
3643 }
3644
3645 /* Determine face for CHARSET_ASCII, or unibyte. */
3646 face_id = face_at_buffer_position (it->w,
3647 CHARPOS (pos),
3648 it->region_beg_charpos,
3649 it->region_end_charpos,
3650 &next_check_charpos,
3651 limit, 0, -1);
3652
3653 /* Correct the face for charsets different from ASCII. Do it
3654 for the multibyte case only. The face returned above is
3655 suitable for unibyte text if current_buffer is unibyte. */
3656 if (it->multibyte_p)
3657 {
3658 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3659 struct face *face = FACE_FROM_ID (it->f, face_id);
3660 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3661 }
3662 }
3663
3664 return face_id;
3665 }
3666
3667
3668 \f
3669 /***********************************************************************
3670 Invisible text
3671 ***********************************************************************/
3672
3673 /* Set up iterator IT from invisible properties at its current
3674 position. Called from handle_stop. */
3675
3676 static enum prop_handled
3677 handle_invisible_prop (it)
3678 struct it *it;
3679 {
3680 enum prop_handled handled = HANDLED_NORMALLY;
3681
3682 if (STRINGP (it->string))
3683 {
3684 extern Lisp_Object Qinvisible;
3685 Lisp_Object prop, end_charpos, limit, charpos;
3686
3687 /* Get the value of the invisible text property at the
3688 current position. Value will be nil if there is no such
3689 property. */
3690 charpos = make_number (IT_STRING_CHARPOS (*it));
3691 prop = Fget_text_property (charpos, Qinvisible, it->string);
3692
3693 if (!NILP (prop)
3694 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3695 {
3696 handled = HANDLED_RECOMPUTE_PROPS;
3697
3698 /* Get the position at which the next change of the
3699 invisible text property can be found in IT->string.
3700 Value will be nil if the property value is the same for
3701 all the rest of IT->string. */
3702 XSETINT (limit, SCHARS (it->string));
3703 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3704 it->string, limit);
3705
3706 /* Text at current position is invisible. The next
3707 change in the property is at position end_charpos.
3708 Move IT's current position to that position. */
3709 if (INTEGERP (end_charpos)
3710 && XFASTINT (end_charpos) < XFASTINT (limit))
3711 {
3712 struct text_pos old;
3713 old = it->current.string_pos;
3714 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3715 compute_string_pos (&it->current.string_pos, old, it->string);
3716 }
3717 else
3718 {
3719 /* The rest of the string is invisible. If this is an
3720 overlay string, proceed with the next overlay string
3721 or whatever comes and return a character from there. */
3722 if (it->current.overlay_string_index >= 0)
3723 {
3724 next_overlay_string (it);
3725 /* Don't check for overlay strings when we just
3726 finished processing them. */
3727 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3728 }
3729 else
3730 {
3731 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3732 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3733 }
3734 }
3735 }
3736 }
3737 else
3738 {
3739 int invis_p;
3740 EMACS_INT newpos, next_stop, start_charpos, tem;
3741 Lisp_Object pos, prop, overlay;
3742
3743 /* First of all, is there invisible text at this position? */
3744 tem = start_charpos = IT_CHARPOS (*it);
3745 pos = make_number (tem);
3746 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3747 &overlay);
3748 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3749
3750 /* If we are on invisible text, skip over it. */
3751 if (invis_p && start_charpos < it->end_charpos)
3752 {
3753 /* Record whether we have to display an ellipsis for the
3754 invisible text. */
3755 int display_ellipsis_p = invis_p == 2;
3756
3757 handled = HANDLED_RECOMPUTE_PROPS;
3758
3759 /* Loop skipping over invisible text. The loop is left at
3760 ZV or with IT on the first char being visible again. */
3761 do
3762 {
3763 /* Try to skip some invisible text. Return value is the
3764 position reached which can be equal to where we start
3765 if there is nothing invisible there. This skips both
3766 over invisible text properties and overlays with
3767 invisible property. */
3768 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3769
3770 /* If we skipped nothing at all we weren't at invisible
3771 text in the first place. If everything to the end of
3772 the buffer was skipped, end the loop. */
3773 if (newpos == tem || newpos >= ZV)
3774 invis_p = 0;
3775 else
3776 {
3777 /* We skipped some characters but not necessarily
3778 all there are. Check if we ended up on visible
3779 text. Fget_char_property returns the property of
3780 the char before the given position, i.e. if we
3781 get invis_p = 0, this means that the char at
3782 newpos is visible. */
3783 pos = make_number (newpos);
3784 prop = Fget_char_property (pos, Qinvisible, it->window);
3785 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3786 }
3787
3788 /* If we ended up on invisible text, proceed to
3789 skip starting with next_stop. */
3790 if (invis_p)
3791 tem = next_stop;
3792
3793 /* If there are adjacent invisible texts, don't lose the
3794 second one's ellipsis. */
3795 if (invis_p == 2)
3796 display_ellipsis_p = 1;
3797 }
3798 while (invis_p);
3799
3800 /* The position newpos is now either ZV or on visible text. */
3801 if (it->bidi_p && newpos < ZV)
3802 {
3803 /* With bidi iteration, the region of invisible text
3804 could start and/or end in the middle of a non-base
3805 embedding level. Therefore, we need to skip
3806 invisible text using the bidi iterator, starting at
3807 IT's current position, until we find ourselves
3808 outside the invisible text. Skipping invisible text
3809 _after_ bidi iteration avoids affecting the visual
3810 order of the displayed text when invisible properties
3811 are added or removed. */
3812 if (it->bidi_it.first_elt)
3813 {
3814 /* If we were `reseat'ed to a new paragraph,
3815 determine the paragraph base direction. We need
3816 to do it now because next_element_from_buffer may
3817 not have a chance to do it, if we are going to
3818 skip any text at the beginning, which resets the
3819 FIRST_ELT flag. */
3820 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
3821 }
3822 do
3823 {
3824 bidi_get_next_char_visually (&it->bidi_it);
3825 }
3826 while (it->stop_charpos <= it->bidi_it.charpos
3827 && it->bidi_it.charpos < newpos);
3828 IT_CHARPOS (*it) = it->bidi_it.charpos;
3829 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3830 /* If we overstepped NEWPOS, record its position in the
3831 iterator, so that we skip invisible text if later the
3832 bidi iteration lands us in the invisible region
3833 again. */
3834 if (IT_CHARPOS (*it) >= newpos)
3835 it->prev_stop = newpos;
3836 }
3837 else
3838 {
3839 IT_CHARPOS (*it) = newpos;
3840 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3841 }
3842
3843 /* If there are before-strings at the start of invisible
3844 text, and the text is invisible because of a text
3845 property, arrange to show before-strings because 20.x did
3846 it that way. (If the text is invisible because of an
3847 overlay property instead of a text property, this is
3848 already handled in the overlay code.) */
3849 if (NILP (overlay)
3850 && get_overlay_strings (it, it->stop_charpos))
3851 {
3852 handled = HANDLED_RECOMPUTE_PROPS;
3853 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3854 }
3855 else if (display_ellipsis_p)
3856 {
3857 /* Make sure that the glyphs of the ellipsis will get
3858 correct `charpos' values. If we would not update
3859 it->position here, the glyphs would belong to the
3860 last visible character _before_ the invisible
3861 text, which confuses `set_cursor_from_row'.
3862
3863 We use the last invisible position instead of the
3864 first because this way the cursor is always drawn on
3865 the first "." of the ellipsis, whenever PT is inside
3866 the invisible text. Otherwise the cursor would be
3867 placed _after_ the ellipsis when the point is after the
3868 first invisible character. */
3869 if (!STRINGP (it->object))
3870 {
3871 it->position.charpos = newpos - 1;
3872 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3873 }
3874 it->ellipsis_p = 1;
3875 /* Let the ellipsis display before
3876 considering any properties of the following char.
3877 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3878 handled = HANDLED_RETURN;
3879 }
3880 }
3881 }
3882
3883 return handled;
3884 }
3885
3886
3887 /* Make iterator IT return `...' next.
3888 Replaces LEN characters from buffer. */
3889
3890 static void
3891 setup_for_ellipsis (it, len)
3892 struct it *it;
3893 int len;
3894 {
3895 /* Use the display table definition for `...'. Invalid glyphs
3896 will be handled by the method returning elements from dpvec. */
3897 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3898 {
3899 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3900 it->dpvec = v->contents;
3901 it->dpend = v->contents + v->size;
3902 }
3903 else
3904 {
3905 /* Default `...'. */
3906 it->dpvec = default_invis_vector;
3907 it->dpend = default_invis_vector + 3;
3908 }
3909
3910 it->dpvec_char_len = len;
3911 it->current.dpvec_index = 0;
3912 it->dpvec_face_id = -1;
3913
3914 /* Remember the current face id in case glyphs specify faces.
3915 IT's face is restored in set_iterator_to_next.
3916 saved_face_id was set to preceding char's face in handle_stop. */
3917 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3918 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3919
3920 it->method = GET_FROM_DISPLAY_VECTOR;
3921 it->ellipsis_p = 1;
3922 }
3923
3924
3925 \f
3926 /***********************************************************************
3927 'display' property
3928 ***********************************************************************/
3929
3930 /* Set up iterator IT from `display' property at its current position.
3931 Called from handle_stop.
3932 We return HANDLED_RETURN if some part of the display property
3933 overrides the display of the buffer text itself.
3934 Otherwise we return HANDLED_NORMALLY. */
3935
3936 static enum prop_handled
3937 handle_display_prop (it)
3938 struct it *it;
3939 {
3940 Lisp_Object prop, object, overlay;
3941 struct text_pos *position;
3942 /* Nonzero if some property replaces the display of the text itself. */
3943 int display_replaced_p = 0;
3944
3945 if (STRINGP (it->string))
3946 {
3947 object = it->string;
3948 position = &it->current.string_pos;
3949 }
3950 else
3951 {
3952 XSETWINDOW (object, it->w);
3953 position = &it->current.pos;
3954 }
3955
3956 /* Reset those iterator values set from display property values. */
3957 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3958 it->space_width = Qnil;
3959 it->font_height = Qnil;
3960 it->voffset = 0;
3961
3962 /* We don't support recursive `display' properties, i.e. string
3963 values that have a string `display' property, that have a string
3964 `display' property etc. */
3965 if (!it->string_from_display_prop_p)
3966 it->area = TEXT_AREA;
3967
3968 prop = get_char_property_and_overlay (make_number (position->charpos),
3969 Qdisplay, object, &overlay);
3970 if (NILP (prop))
3971 return HANDLED_NORMALLY;
3972 /* Now OVERLAY is the overlay that gave us this property, or nil
3973 if it was a text property. */
3974
3975 if (!STRINGP (it->string))
3976 object = it->w->buffer;
3977
3978 if (CONSP (prop)
3979 /* Simple properties. */
3980 && !EQ (XCAR (prop), Qimage)
3981 && !EQ (XCAR (prop), Qspace)
3982 && !EQ (XCAR (prop), Qwhen)
3983 && !EQ (XCAR (prop), Qslice)
3984 && !EQ (XCAR (prop), Qspace_width)
3985 && !EQ (XCAR (prop), Qheight)
3986 && !EQ (XCAR (prop), Qraise)
3987 /* Marginal area specifications. */
3988 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3989 && !EQ (XCAR (prop), Qleft_fringe)
3990 && !EQ (XCAR (prop), Qright_fringe)
3991 && !NILP (XCAR (prop)))
3992 {
3993 for (; CONSP (prop); prop = XCDR (prop))
3994 {
3995 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3996 position, display_replaced_p))
3997 {
3998 display_replaced_p = 1;
3999 /* If some text in a string is replaced, `position' no
4000 longer points to the position of `object'. */
4001 if (STRINGP (object))
4002 break;
4003 }
4004 }
4005 }
4006 else if (VECTORP (prop))
4007 {
4008 int i;
4009 for (i = 0; i < ASIZE (prop); ++i)
4010 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4011 position, display_replaced_p))
4012 {
4013 display_replaced_p = 1;
4014 /* If some text in a string is replaced, `position' no
4015 longer points to the position of `object'. */
4016 if (STRINGP (object))
4017 break;
4018 }
4019 }
4020 else
4021 {
4022 if (handle_single_display_spec (it, prop, object, overlay,
4023 position, 0))
4024 display_replaced_p = 1;
4025 }
4026
4027 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4028 }
4029
4030
4031 /* Value is the position of the end of the `display' property starting
4032 at START_POS in OBJECT. */
4033
4034 static struct text_pos
4035 display_prop_end (it, object, start_pos)
4036 struct it *it;
4037 Lisp_Object object;
4038 struct text_pos start_pos;
4039 {
4040 Lisp_Object end;
4041 struct text_pos end_pos;
4042
4043 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4044 Qdisplay, object, Qnil);
4045 CHARPOS (end_pos) = XFASTINT (end);
4046 if (STRINGP (object))
4047 compute_string_pos (&end_pos, start_pos, it->string);
4048 else
4049 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4050
4051 return end_pos;
4052 }
4053
4054
4055 /* Set up IT from a single `display' specification PROP. OBJECT
4056 is the object in which the `display' property was found. *POSITION
4057 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4058 means that we previously saw a display specification which already
4059 replaced text display with something else, for example an image;
4060 we ignore such properties after the first one has been processed.
4061
4062 OVERLAY is the overlay this `display' property came from,
4063 or nil if it was a text property.
4064
4065 If PROP is a `space' or `image' specification, and in some other
4066 cases too, set *POSITION to the position where the `display'
4067 property ends.
4068
4069 Value is non-zero if something was found which replaces the display
4070 of buffer or string text. */
4071
4072 static int
4073 handle_single_display_spec (it, spec, object, overlay, position,
4074 display_replaced_before_p)
4075 struct it *it;
4076 Lisp_Object spec;
4077 Lisp_Object object;
4078 Lisp_Object overlay;
4079 struct text_pos *position;
4080 int display_replaced_before_p;
4081 {
4082 Lisp_Object form;
4083 Lisp_Object location, value;
4084 struct text_pos start_pos, save_pos;
4085 int valid_p;
4086
4087 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4088 If the result is non-nil, use VALUE instead of SPEC. */
4089 form = Qt;
4090 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4091 {
4092 spec = XCDR (spec);
4093 if (!CONSP (spec))
4094 return 0;
4095 form = XCAR (spec);
4096 spec = XCDR (spec);
4097 }
4098
4099 if (!NILP (form) && !EQ (form, Qt))
4100 {
4101 int count = SPECPDL_INDEX ();
4102 struct gcpro gcpro1;
4103
4104 /* Bind `object' to the object having the `display' property, a
4105 buffer or string. Bind `position' to the position in the
4106 object where the property was found, and `buffer-position'
4107 to the current position in the buffer. */
4108 specbind (Qobject, object);
4109 specbind (Qposition, make_number (CHARPOS (*position)));
4110 specbind (Qbuffer_position,
4111 make_number (STRINGP (object)
4112 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4113 GCPRO1 (form);
4114 form = safe_eval (form);
4115 UNGCPRO;
4116 unbind_to (count, Qnil);
4117 }
4118
4119 if (NILP (form))
4120 return 0;
4121
4122 /* Handle `(height HEIGHT)' specifications. */
4123 if (CONSP (spec)
4124 && EQ (XCAR (spec), Qheight)
4125 && CONSP (XCDR (spec)))
4126 {
4127 if (!FRAME_WINDOW_P (it->f))
4128 return 0;
4129
4130 it->font_height = XCAR (XCDR (spec));
4131 if (!NILP (it->font_height))
4132 {
4133 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4134 int new_height = -1;
4135
4136 if (CONSP (it->font_height)
4137 && (EQ (XCAR (it->font_height), Qplus)
4138 || EQ (XCAR (it->font_height), Qminus))
4139 && CONSP (XCDR (it->font_height))
4140 && INTEGERP (XCAR (XCDR (it->font_height))))
4141 {
4142 /* `(+ N)' or `(- N)' where N is an integer. */
4143 int steps = XINT (XCAR (XCDR (it->font_height)));
4144 if (EQ (XCAR (it->font_height), Qplus))
4145 steps = - steps;
4146 it->face_id = smaller_face (it->f, it->face_id, steps);
4147 }
4148 else if (FUNCTIONP (it->font_height))
4149 {
4150 /* Call function with current height as argument.
4151 Value is the new height. */
4152 Lisp_Object height;
4153 height = safe_call1 (it->font_height,
4154 face->lface[LFACE_HEIGHT_INDEX]);
4155 if (NUMBERP (height))
4156 new_height = XFLOATINT (height);
4157 }
4158 else if (NUMBERP (it->font_height))
4159 {
4160 /* Value is a multiple of the canonical char height. */
4161 struct face *face;
4162
4163 face = FACE_FROM_ID (it->f,
4164 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4165 new_height = (XFLOATINT (it->font_height)
4166 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4167 }
4168 else
4169 {
4170 /* Evaluate IT->font_height with `height' bound to the
4171 current specified height to get the new height. */
4172 int count = SPECPDL_INDEX ();
4173
4174 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4175 value = safe_eval (it->font_height);
4176 unbind_to (count, Qnil);
4177
4178 if (NUMBERP (value))
4179 new_height = XFLOATINT (value);
4180 }
4181
4182 if (new_height > 0)
4183 it->face_id = face_with_height (it->f, it->face_id, new_height);
4184 }
4185
4186 return 0;
4187 }
4188
4189 /* Handle `(space-width WIDTH)'. */
4190 if (CONSP (spec)
4191 && EQ (XCAR (spec), Qspace_width)
4192 && CONSP (XCDR (spec)))
4193 {
4194 if (!FRAME_WINDOW_P (it->f))
4195 return 0;
4196
4197 value = XCAR (XCDR (spec));
4198 if (NUMBERP (value) && XFLOATINT (value) > 0)
4199 it->space_width = value;
4200
4201 return 0;
4202 }
4203
4204 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4205 if (CONSP (spec)
4206 && EQ (XCAR (spec), Qslice))
4207 {
4208 Lisp_Object tem;
4209
4210 if (!FRAME_WINDOW_P (it->f))
4211 return 0;
4212
4213 if (tem = XCDR (spec), CONSP (tem))
4214 {
4215 it->slice.x = XCAR (tem);
4216 if (tem = XCDR (tem), CONSP (tem))
4217 {
4218 it->slice.y = XCAR (tem);
4219 if (tem = XCDR (tem), CONSP (tem))
4220 {
4221 it->slice.width = XCAR (tem);
4222 if (tem = XCDR (tem), CONSP (tem))
4223 it->slice.height = XCAR (tem);
4224 }
4225 }
4226 }
4227
4228 return 0;
4229 }
4230
4231 /* Handle `(raise FACTOR)'. */
4232 if (CONSP (spec)
4233 && EQ (XCAR (spec), Qraise)
4234 && CONSP (XCDR (spec)))
4235 {
4236 if (!FRAME_WINDOW_P (it->f))
4237 return 0;
4238
4239 #ifdef HAVE_WINDOW_SYSTEM
4240 value = XCAR (XCDR (spec));
4241 if (NUMBERP (value))
4242 {
4243 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4244 it->voffset = - (XFLOATINT (value)
4245 * (FONT_HEIGHT (face->font)));
4246 }
4247 #endif /* HAVE_WINDOW_SYSTEM */
4248
4249 return 0;
4250 }
4251
4252 /* Don't handle the other kinds of display specifications
4253 inside a string that we got from a `display' property. */
4254 if (it->string_from_display_prop_p)
4255 return 0;
4256
4257 /* Characters having this form of property are not displayed, so
4258 we have to find the end of the property. */
4259 start_pos = *position;
4260 *position = display_prop_end (it, object, start_pos);
4261 value = Qnil;
4262
4263 /* Stop the scan at that end position--we assume that all
4264 text properties change there. */
4265 it->stop_charpos = position->charpos;
4266
4267 /* Handle `(left-fringe BITMAP [FACE])'
4268 and `(right-fringe BITMAP [FACE])'. */
4269 if (CONSP (spec)
4270 && (EQ (XCAR (spec), Qleft_fringe)
4271 || EQ (XCAR (spec), Qright_fringe))
4272 && CONSP (XCDR (spec)))
4273 {
4274 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4275 int fringe_bitmap;
4276
4277 if (!FRAME_WINDOW_P (it->f))
4278 /* If we return here, POSITION has been advanced
4279 across the text with this property. */
4280 return 0;
4281
4282 #ifdef HAVE_WINDOW_SYSTEM
4283 value = XCAR (XCDR (spec));
4284 if (!SYMBOLP (value)
4285 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4286 /* If we return here, POSITION has been advanced
4287 across the text with this property. */
4288 return 0;
4289
4290 if (CONSP (XCDR (XCDR (spec))))
4291 {
4292 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4293 int face_id2 = lookup_derived_face (it->f, face_name,
4294 FRINGE_FACE_ID, 0);
4295 if (face_id2 >= 0)
4296 face_id = face_id2;
4297 }
4298
4299 /* Save current settings of IT so that we can restore them
4300 when we are finished with the glyph property value. */
4301
4302 save_pos = it->position;
4303 it->position = *position;
4304 push_it (it);
4305 it->position = save_pos;
4306
4307 it->area = TEXT_AREA;
4308 it->what = IT_IMAGE;
4309 it->image_id = -1; /* no image */
4310 it->position = start_pos;
4311 it->object = NILP (object) ? it->w->buffer : object;
4312 it->method = GET_FROM_IMAGE;
4313 it->from_overlay = Qnil;
4314 it->face_id = face_id;
4315
4316 /* Say that we haven't consumed the characters with
4317 `display' property yet. The call to pop_it in
4318 set_iterator_to_next will clean this up. */
4319 *position = start_pos;
4320
4321 if (EQ (XCAR (spec), Qleft_fringe))
4322 {
4323 it->left_user_fringe_bitmap = fringe_bitmap;
4324 it->left_user_fringe_face_id = face_id;
4325 }
4326 else
4327 {
4328 it->right_user_fringe_bitmap = fringe_bitmap;
4329 it->right_user_fringe_face_id = face_id;
4330 }
4331 #endif /* HAVE_WINDOW_SYSTEM */
4332 return 1;
4333 }
4334
4335 /* Prepare to handle `((margin left-margin) ...)',
4336 `((margin right-margin) ...)' and `((margin nil) ...)'
4337 prefixes for display specifications. */
4338 location = Qunbound;
4339 if (CONSP (spec) && CONSP (XCAR (spec)))
4340 {
4341 Lisp_Object tem;
4342
4343 value = XCDR (spec);
4344 if (CONSP (value))
4345 value = XCAR (value);
4346
4347 tem = XCAR (spec);
4348 if (EQ (XCAR (tem), Qmargin)
4349 && (tem = XCDR (tem),
4350 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4351 (NILP (tem)
4352 || EQ (tem, Qleft_margin)
4353 || EQ (tem, Qright_margin))))
4354 location = tem;
4355 }
4356
4357 if (EQ (location, Qunbound))
4358 {
4359 location = Qnil;
4360 value = spec;
4361 }
4362
4363 /* After this point, VALUE is the property after any
4364 margin prefix has been stripped. It must be a string,
4365 an image specification, or `(space ...)'.
4366
4367 LOCATION specifies where to display: `left-margin',
4368 `right-margin' or nil. */
4369
4370 valid_p = (STRINGP (value)
4371 #ifdef HAVE_WINDOW_SYSTEM
4372 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4373 #endif /* not HAVE_WINDOW_SYSTEM */
4374 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4375
4376 if (valid_p && !display_replaced_before_p)
4377 {
4378 /* Save current settings of IT so that we can restore them
4379 when we are finished with the glyph property value. */
4380 save_pos = it->position;
4381 it->position = *position;
4382 push_it (it);
4383 it->position = save_pos;
4384 it->from_overlay = overlay;
4385
4386 if (NILP (location))
4387 it->area = TEXT_AREA;
4388 else if (EQ (location, Qleft_margin))
4389 it->area = LEFT_MARGIN_AREA;
4390 else
4391 it->area = RIGHT_MARGIN_AREA;
4392
4393 if (STRINGP (value))
4394 {
4395 it->string = value;
4396 it->multibyte_p = STRING_MULTIBYTE (it->string);
4397 it->current.overlay_string_index = -1;
4398 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4399 it->end_charpos = it->string_nchars = SCHARS (it->string);
4400 it->method = GET_FROM_STRING;
4401 it->stop_charpos = 0;
4402 it->string_from_display_prop_p = 1;
4403 /* Say that we haven't consumed the characters with
4404 `display' property yet. The call to pop_it in
4405 set_iterator_to_next will clean this up. */
4406 if (BUFFERP (object))
4407 *position = start_pos;
4408 }
4409 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4410 {
4411 it->method = GET_FROM_STRETCH;
4412 it->object = value;
4413 *position = it->position = start_pos;
4414 }
4415 #ifdef HAVE_WINDOW_SYSTEM
4416 else
4417 {
4418 it->what = IT_IMAGE;
4419 it->image_id = lookup_image (it->f, value);
4420 it->position = start_pos;
4421 it->object = NILP (object) ? it->w->buffer : object;
4422 it->method = GET_FROM_IMAGE;
4423
4424 /* Say that we haven't consumed the characters with
4425 `display' property yet. The call to pop_it in
4426 set_iterator_to_next will clean this up. */
4427 *position = start_pos;
4428 }
4429 #endif /* HAVE_WINDOW_SYSTEM */
4430
4431 return 1;
4432 }
4433
4434 /* Invalid property or property not supported. Restore
4435 POSITION to what it was before. */
4436 *position = start_pos;
4437 return 0;
4438 }
4439
4440
4441 /* Check if SPEC is a display sub-property value whose text should be
4442 treated as intangible. */
4443
4444 static int
4445 single_display_spec_intangible_p (prop)
4446 Lisp_Object prop;
4447 {
4448 /* Skip over `when FORM'. */
4449 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4450 {
4451 prop = XCDR (prop);
4452 if (!CONSP (prop))
4453 return 0;
4454 prop = XCDR (prop);
4455 }
4456
4457 if (STRINGP (prop))
4458 return 1;
4459
4460 if (!CONSP (prop))
4461 return 0;
4462
4463 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4464 we don't need to treat text as intangible. */
4465 if (EQ (XCAR (prop), Qmargin))
4466 {
4467 prop = XCDR (prop);
4468 if (!CONSP (prop))
4469 return 0;
4470
4471 prop = XCDR (prop);
4472 if (!CONSP (prop)
4473 || EQ (XCAR (prop), Qleft_margin)
4474 || EQ (XCAR (prop), Qright_margin))
4475 return 0;
4476 }
4477
4478 return (CONSP (prop)
4479 && (EQ (XCAR (prop), Qimage)
4480 || EQ (XCAR (prop), Qspace)));
4481 }
4482
4483
4484 /* Check if PROP is a display property value whose text should be
4485 treated as intangible. */
4486
4487 int
4488 display_prop_intangible_p (prop)
4489 Lisp_Object prop;
4490 {
4491 if (CONSP (prop)
4492 && CONSP (XCAR (prop))
4493 && !EQ (Qmargin, XCAR (XCAR (prop))))
4494 {
4495 /* A list of sub-properties. */
4496 while (CONSP (prop))
4497 {
4498 if (single_display_spec_intangible_p (XCAR (prop)))
4499 return 1;
4500 prop = XCDR (prop);
4501 }
4502 }
4503 else if (VECTORP (prop))
4504 {
4505 /* A vector of sub-properties. */
4506 int i;
4507 for (i = 0; i < ASIZE (prop); ++i)
4508 if (single_display_spec_intangible_p (AREF (prop, i)))
4509 return 1;
4510 }
4511 else
4512 return single_display_spec_intangible_p (prop);
4513
4514 return 0;
4515 }
4516
4517
4518 /* Return 1 if PROP is a display sub-property value containing STRING. */
4519
4520 static int
4521 single_display_spec_string_p (prop, string)
4522 Lisp_Object prop, string;
4523 {
4524 if (EQ (string, prop))
4525 return 1;
4526
4527 /* Skip over `when FORM'. */
4528 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4529 {
4530 prop = XCDR (prop);
4531 if (!CONSP (prop))
4532 return 0;
4533 prop = XCDR (prop);
4534 }
4535
4536 if (CONSP (prop))
4537 /* Skip over `margin LOCATION'. */
4538 if (EQ (XCAR (prop), Qmargin))
4539 {
4540 prop = XCDR (prop);
4541 if (!CONSP (prop))
4542 return 0;
4543
4544 prop = XCDR (prop);
4545 if (!CONSP (prop))
4546 return 0;
4547 }
4548
4549 return CONSP (prop) && EQ (XCAR (prop), string);
4550 }
4551
4552
4553 /* Return 1 if STRING appears in the `display' property PROP. */
4554
4555 static int
4556 display_prop_string_p (prop, string)
4557 Lisp_Object prop, string;
4558 {
4559 if (CONSP (prop)
4560 && CONSP (XCAR (prop))
4561 && !EQ (Qmargin, XCAR (XCAR (prop))))
4562 {
4563 /* A list of sub-properties. */
4564 while (CONSP (prop))
4565 {
4566 if (single_display_spec_string_p (XCAR (prop), string))
4567 return 1;
4568 prop = XCDR (prop);
4569 }
4570 }
4571 else if (VECTORP (prop))
4572 {
4573 /* A vector of sub-properties. */
4574 int i;
4575 for (i = 0; i < ASIZE (prop); ++i)
4576 if (single_display_spec_string_p (AREF (prop, i), string))
4577 return 1;
4578 }
4579 else
4580 return single_display_spec_string_p (prop, string);
4581
4582 return 0;
4583 }
4584
4585 /* Look for STRING in overlays and text properties in W's buffer,
4586 between character positions FROM and TO (excluding TO).
4587 BACK_P non-zero means look back (in this case, TO is supposed to be
4588 less than FROM).
4589 Value is the first character position where STRING was found, or
4590 zero if it wasn't found before hitting TO.
4591
4592 W's buffer must be current.
4593
4594 This function may only use code that doesn't eval because it is
4595 called asynchronously from note_mouse_highlight. */
4596
4597 static EMACS_INT
4598 string_buffer_position_lim (w, string, from, to, back_p)
4599 struct window *w;
4600 Lisp_Object string;
4601 EMACS_INT from, to;
4602 int back_p;
4603 {
4604 Lisp_Object limit, prop, pos;
4605 int found = 0;
4606
4607 pos = make_number (from);
4608
4609 if (!back_p) /* looking forward */
4610 {
4611 limit = make_number (min (to, ZV));
4612 while (!found && !EQ (pos, limit))
4613 {
4614 prop = Fget_char_property (pos, Qdisplay, Qnil);
4615 if (!NILP (prop) && display_prop_string_p (prop, string))
4616 found = 1;
4617 else
4618 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4619 limit);
4620 }
4621 }
4622 else /* looking back */
4623 {
4624 limit = make_number (max (to, BEGV));
4625 while (!found && !EQ (pos, limit))
4626 {
4627 prop = Fget_char_property (pos, Qdisplay, Qnil);
4628 if (!NILP (prop) && display_prop_string_p (prop, string))
4629 found = 1;
4630 else
4631 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4632 limit);
4633 }
4634 }
4635
4636 return found ? XINT (pos) : 0;
4637 }
4638
4639 /* Determine which buffer position in W's buffer STRING comes from.
4640 AROUND_CHARPOS is an approximate position where it could come from.
4641 Value is the buffer position or 0 if it couldn't be determined.
4642
4643 W's buffer must be current.
4644
4645 This function is necessary because we don't record buffer positions
4646 in glyphs generated from strings (to keep struct glyph small).
4647 This function may only use code that doesn't eval because it is
4648 called asynchronously from note_mouse_highlight. */
4649
4650 EMACS_INT
4651 string_buffer_position (w, string, around_charpos)
4652 struct window *w;
4653 Lisp_Object string;
4654 EMACS_INT around_charpos;
4655 {
4656 Lisp_Object limit, prop, pos;
4657 const int MAX_DISTANCE = 1000;
4658 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4659 around_charpos + MAX_DISTANCE,
4660 0);
4661
4662 if (!found)
4663 found = string_buffer_position_lim (w, string, around_charpos,
4664 around_charpos - MAX_DISTANCE, 1);
4665 return found;
4666 }
4667
4668
4669 \f
4670 /***********************************************************************
4671 `composition' property
4672 ***********************************************************************/
4673
4674 /* Set up iterator IT from `composition' property at its current
4675 position. Called from handle_stop. */
4676
4677 static enum prop_handled
4678 handle_composition_prop (it)
4679 struct it *it;
4680 {
4681 Lisp_Object prop, string;
4682 EMACS_INT pos, pos_byte, start, end;
4683
4684 if (STRINGP (it->string))
4685 {
4686 unsigned char *s;
4687
4688 pos = IT_STRING_CHARPOS (*it);
4689 pos_byte = IT_STRING_BYTEPOS (*it);
4690 string = it->string;
4691 s = SDATA (string) + pos_byte;
4692 it->c = STRING_CHAR (s);
4693 }
4694 else
4695 {
4696 pos = IT_CHARPOS (*it);
4697 pos_byte = IT_BYTEPOS (*it);
4698 string = Qnil;
4699 it->c = FETCH_CHAR (pos_byte);
4700 }
4701
4702 /* If there's a valid composition and point is not inside of the
4703 composition (in the case that the composition is from the current
4704 buffer), draw a glyph composed from the composition components. */
4705 if (find_composition (pos, -1, &start, &end, &prop, string)
4706 && COMPOSITION_VALID_P (start, end, prop)
4707 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4708 {
4709 if (start != pos)
4710 {
4711 if (STRINGP (it->string))
4712 pos_byte = string_char_to_byte (it->string, start);
4713 else
4714 pos_byte = CHAR_TO_BYTE (start);
4715 }
4716 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4717 prop, string);
4718
4719 if (it->cmp_it.id >= 0)
4720 {
4721 it->cmp_it.ch = -1;
4722 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4723 it->cmp_it.nglyphs = -1;
4724 }
4725 }
4726
4727 return HANDLED_NORMALLY;
4728 }
4729
4730
4731 \f
4732 /***********************************************************************
4733 Overlay strings
4734 ***********************************************************************/
4735
4736 /* The following structure is used to record overlay strings for
4737 later sorting in load_overlay_strings. */
4738
4739 struct overlay_entry
4740 {
4741 Lisp_Object overlay;
4742 Lisp_Object string;
4743 int priority;
4744 int after_string_p;
4745 };
4746
4747
4748 /* Set up iterator IT from overlay strings at its current position.
4749 Called from handle_stop. */
4750
4751 static enum prop_handled
4752 handle_overlay_change (it)
4753 struct it *it;
4754 {
4755 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4756 return HANDLED_RECOMPUTE_PROPS;
4757 else
4758 return HANDLED_NORMALLY;
4759 }
4760
4761
4762 /* Set up the next overlay string for delivery by IT, if there is an
4763 overlay string to deliver. Called by set_iterator_to_next when the
4764 end of the current overlay string is reached. If there are more
4765 overlay strings to display, IT->string and
4766 IT->current.overlay_string_index are set appropriately here.
4767 Otherwise IT->string is set to nil. */
4768
4769 static void
4770 next_overlay_string (it)
4771 struct it *it;
4772 {
4773 ++it->current.overlay_string_index;
4774 if (it->current.overlay_string_index == it->n_overlay_strings)
4775 {
4776 /* No more overlay strings. Restore IT's settings to what
4777 they were before overlay strings were processed, and
4778 continue to deliver from current_buffer. */
4779
4780 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4781 pop_it (it);
4782 xassert (it->sp > 0
4783 || (NILP (it->string)
4784 && it->method == GET_FROM_BUFFER
4785 && it->stop_charpos >= BEGV
4786 && it->stop_charpos <= it->end_charpos));
4787 it->current.overlay_string_index = -1;
4788 it->n_overlay_strings = 0;
4789
4790 /* If we're at the end of the buffer, record that we have
4791 processed the overlay strings there already, so that
4792 next_element_from_buffer doesn't try it again. */
4793 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4794 it->overlay_strings_at_end_processed_p = 1;
4795 }
4796 else
4797 {
4798 /* There are more overlay strings to process. If
4799 IT->current.overlay_string_index has advanced to a position
4800 where we must load IT->overlay_strings with more strings, do
4801 it. */
4802 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4803
4804 if (it->current.overlay_string_index && i == 0)
4805 load_overlay_strings (it, 0);
4806
4807 /* Initialize IT to deliver display elements from the overlay
4808 string. */
4809 it->string = it->overlay_strings[i];
4810 it->multibyte_p = STRING_MULTIBYTE (it->string);
4811 SET_TEXT_POS (it->current.string_pos, 0, 0);
4812 it->method = GET_FROM_STRING;
4813 it->stop_charpos = 0;
4814 if (it->cmp_it.stop_pos >= 0)
4815 it->cmp_it.stop_pos = 0;
4816 }
4817
4818 CHECK_IT (it);
4819 }
4820
4821
4822 /* Compare two overlay_entry structures E1 and E2. Used as a
4823 comparison function for qsort in load_overlay_strings. Overlay
4824 strings for the same position are sorted so that
4825
4826 1. All after-strings come in front of before-strings, except
4827 when they come from the same overlay.
4828
4829 2. Within after-strings, strings are sorted so that overlay strings
4830 from overlays with higher priorities come first.
4831
4832 2. Within before-strings, strings are sorted so that overlay
4833 strings from overlays with higher priorities come last.
4834
4835 Value is analogous to strcmp. */
4836
4837
4838 static int
4839 compare_overlay_entries (e1, e2)
4840 void *e1, *e2;
4841 {
4842 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4843 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4844 int result;
4845
4846 if (entry1->after_string_p != entry2->after_string_p)
4847 {
4848 /* Let after-strings appear in front of before-strings if
4849 they come from different overlays. */
4850 if (EQ (entry1->overlay, entry2->overlay))
4851 result = entry1->after_string_p ? 1 : -1;
4852 else
4853 result = entry1->after_string_p ? -1 : 1;
4854 }
4855 else if (entry1->after_string_p)
4856 /* After-strings sorted in order of decreasing priority. */
4857 result = entry2->priority - entry1->priority;
4858 else
4859 /* Before-strings sorted in order of increasing priority. */
4860 result = entry1->priority - entry2->priority;
4861
4862 return result;
4863 }
4864
4865
4866 /* Load the vector IT->overlay_strings with overlay strings from IT's
4867 current buffer position, or from CHARPOS if that is > 0. Set
4868 IT->n_overlays to the total number of overlay strings found.
4869
4870 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4871 a time. On entry into load_overlay_strings,
4872 IT->current.overlay_string_index gives the number of overlay
4873 strings that have already been loaded by previous calls to this
4874 function.
4875
4876 IT->add_overlay_start contains an additional overlay start
4877 position to consider for taking overlay strings from, if non-zero.
4878 This position comes into play when the overlay has an `invisible'
4879 property, and both before and after-strings. When we've skipped to
4880 the end of the overlay, because of its `invisible' property, we
4881 nevertheless want its before-string to appear.
4882 IT->add_overlay_start will contain the overlay start position
4883 in this case.
4884
4885 Overlay strings are sorted so that after-string strings come in
4886 front of before-string strings. Within before and after-strings,
4887 strings are sorted by overlay priority. See also function
4888 compare_overlay_entries. */
4889
4890 static void
4891 load_overlay_strings (it, charpos)
4892 struct it *it;
4893 int charpos;
4894 {
4895 extern Lisp_Object Qwindow, Qpriority;
4896 Lisp_Object overlay, window, str, invisible;
4897 struct Lisp_Overlay *ov;
4898 int start, end;
4899 int size = 20;
4900 int n = 0, i, j, invis_p;
4901 struct overlay_entry *entries
4902 = (struct overlay_entry *) alloca (size * sizeof *entries);
4903
4904 if (charpos <= 0)
4905 charpos = IT_CHARPOS (*it);
4906
4907 /* Append the overlay string STRING of overlay OVERLAY to vector
4908 `entries' which has size `size' and currently contains `n'
4909 elements. AFTER_P non-zero means STRING is an after-string of
4910 OVERLAY. */
4911 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4912 do \
4913 { \
4914 Lisp_Object priority; \
4915 \
4916 if (n == size) \
4917 { \
4918 int new_size = 2 * size; \
4919 struct overlay_entry *old = entries; \
4920 entries = \
4921 (struct overlay_entry *) alloca (new_size \
4922 * sizeof *entries); \
4923 bcopy (old, entries, size * sizeof *entries); \
4924 size = new_size; \
4925 } \
4926 \
4927 entries[n].string = (STRING); \
4928 entries[n].overlay = (OVERLAY); \
4929 priority = Foverlay_get ((OVERLAY), Qpriority); \
4930 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4931 entries[n].after_string_p = (AFTER_P); \
4932 ++n; \
4933 } \
4934 while (0)
4935
4936 /* Process overlay before the overlay center. */
4937 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4938 {
4939 XSETMISC (overlay, ov);
4940 xassert (OVERLAYP (overlay));
4941 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4942 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4943
4944 if (end < charpos)
4945 break;
4946
4947 /* Skip this overlay if it doesn't start or end at IT's current
4948 position. */
4949 if (end != charpos && start != charpos)
4950 continue;
4951
4952 /* Skip this overlay if it doesn't apply to IT->w. */
4953 window = Foverlay_get (overlay, Qwindow);
4954 if (WINDOWP (window) && XWINDOW (window) != it->w)
4955 continue;
4956
4957 /* If the text ``under'' the overlay is invisible, both before-
4958 and after-strings from this overlay are visible; start and
4959 end position are indistinguishable. */
4960 invisible = Foverlay_get (overlay, Qinvisible);
4961 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4962
4963 /* If overlay has a non-empty before-string, record it. */
4964 if ((start == charpos || (end == charpos && invis_p))
4965 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4966 && SCHARS (str))
4967 RECORD_OVERLAY_STRING (overlay, str, 0);
4968
4969 /* If overlay has a non-empty after-string, record it. */
4970 if ((end == charpos || (start == charpos && invis_p))
4971 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4972 && SCHARS (str))
4973 RECORD_OVERLAY_STRING (overlay, str, 1);
4974 }
4975
4976 /* Process overlays after the overlay center. */
4977 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4978 {
4979 XSETMISC (overlay, ov);
4980 xassert (OVERLAYP (overlay));
4981 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4982 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4983
4984 if (start > charpos)
4985 break;
4986
4987 /* Skip this overlay if it doesn't start or end at IT's current
4988 position. */
4989 if (end != charpos && start != charpos)
4990 continue;
4991
4992 /* Skip this overlay if it doesn't apply to IT->w. */
4993 window = Foverlay_get (overlay, Qwindow);
4994 if (WINDOWP (window) && XWINDOW (window) != it->w)
4995 continue;
4996
4997 /* If the text ``under'' the overlay is invisible, it has a zero
4998 dimension, and both before- and after-strings apply. */
4999 invisible = Foverlay_get (overlay, Qinvisible);
5000 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5001
5002 /* If overlay has a non-empty before-string, record it. */
5003 if ((start == charpos || (end == charpos && invis_p))
5004 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5005 && SCHARS (str))
5006 RECORD_OVERLAY_STRING (overlay, str, 0);
5007
5008 /* If overlay has a non-empty after-string, record it. */
5009 if ((end == charpos || (start == charpos && invis_p))
5010 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5011 && SCHARS (str))
5012 RECORD_OVERLAY_STRING (overlay, str, 1);
5013 }
5014
5015 #undef RECORD_OVERLAY_STRING
5016
5017 /* Sort entries. */
5018 if (n > 1)
5019 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5020
5021 /* Record the total number of strings to process. */
5022 it->n_overlay_strings = n;
5023
5024 /* IT->current.overlay_string_index is the number of overlay strings
5025 that have already been consumed by IT. Copy some of the
5026 remaining overlay strings to IT->overlay_strings. */
5027 i = 0;
5028 j = it->current.overlay_string_index;
5029 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5030 {
5031 it->overlay_strings[i] = entries[j].string;
5032 it->string_overlays[i++] = entries[j++].overlay;
5033 }
5034
5035 CHECK_IT (it);
5036 }
5037
5038
5039 /* Get the first chunk of overlay strings at IT's current buffer
5040 position, or at CHARPOS if that is > 0. Value is non-zero if at
5041 least one overlay string was found. */
5042
5043 static int
5044 get_overlay_strings_1 (it, charpos, compute_stop_p)
5045 struct it *it;
5046 int charpos;
5047 int compute_stop_p;
5048 {
5049 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5050 process. This fills IT->overlay_strings with strings, and sets
5051 IT->n_overlay_strings to the total number of strings to process.
5052 IT->pos.overlay_string_index has to be set temporarily to zero
5053 because load_overlay_strings needs this; it must be set to -1
5054 when no overlay strings are found because a zero value would
5055 indicate a position in the first overlay string. */
5056 it->current.overlay_string_index = 0;
5057 load_overlay_strings (it, charpos);
5058
5059 /* If we found overlay strings, set up IT to deliver display
5060 elements from the first one. Otherwise set up IT to deliver
5061 from current_buffer. */
5062 if (it->n_overlay_strings)
5063 {
5064 /* Make sure we know settings in current_buffer, so that we can
5065 restore meaningful values when we're done with the overlay
5066 strings. */
5067 if (compute_stop_p)
5068 compute_stop_pos (it);
5069 xassert (it->face_id >= 0);
5070
5071 /* Save IT's settings. They are restored after all overlay
5072 strings have been processed. */
5073 xassert (!compute_stop_p || it->sp == 0);
5074
5075 /* When called from handle_stop, there might be an empty display
5076 string loaded. In that case, don't bother saving it. */
5077 if (!STRINGP (it->string) || SCHARS (it->string))
5078 push_it (it);
5079
5080 /* Set up IT to deliver display elements from the first overlay
5081 string. */
5082 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5083 it->string = it->overlay_strings[0];
5084 it->from_overlay = Qnil;
5085 it->stop_charpos = 0;
5086 xassert (STRINGP (it->string));
5087 it->end_charpos = SCHARS (it->string);
5088 it->multibyte_p = STRING_MULTIBYTE (it->string);
5089 it->method = GET_FROM_STRING;
5090 return 1;
5091 }
5092
5093 it->current.overlay_string_index = -1;
5094 return 0;
5095 }
5096
5097 static int
5098 get_overlay_strings (it, charpos)
5099 struct it *it;
5100 int charpos;
5101 {
5102 it->string = Qnil;
5103 it->method = GET_FROM_BUFFER;
5104
5105 (void) get_overlay_strings_1 (it, charpos, 1);
5106
5107 CHECK_IT (it);
5108
5109 /* Value is non-zero if we found at least one overlay string. */
5110 return STRINGP (it->string);
5111 }
5112
5113
5114 \f
5115 /***********************************************************************
5116 Saving and restoring state
5117 ***********************************************************************/
5118
5119 /* Save current settings of IT on IT->stack. Called, for example,
5120 before setting up IT for an overlay string, to be able to restore
5121 IT's settings to what they were after the overlay string has been
5122 processed. */
5123
5124 static void
5125 push_it (it)
5126 struct it *it;
5127 {
5128 struct iterator_stack_entry *p;
5129
5130 xassert (it->sp < IT_STACK_SIZE);
5131 p = it->stack + it->sp;
5132
5133 p->stop_charpos = it->stop_charpos;
5134 p->prev_stop = it->prev_stop;
5135 p->base_level_stop = it->base_level_stop;
5136 p->cmp_it = it->cmp_it;
5137 xassert (it->face_id >= 0);
5138 p->face_id = it->face_id;
5139 p->string = it->string;
5140 p->method = it->method;
5141 p->from_overlay = it->from_overlay;
5142 switch (p->method)
5143 {
5144 case GET_FROM_IMAGE:
5145 p->u.image.object = it->object;
5146 p->u.image.image_id = it->image_id;
5147 p->u.image.slice = it->slice;
5148 break;
5149 case GET_FROM_STRETCH:
5150 p->u.stretch.object = it->object;
5151 break;
5152 }
5153 p->position = it->position;
5154 p->current = it->current;
5155 p->end_charpos = it->end_charpos;
5156 p->string_nchars = it->string_nchars;
5157 p->area = it->area;
5158 p->multibyte_p = it->multibyte_p;
5159 p->avoid_cursor_p = it->avoid_cursor_p;
5160 p->space_width = it->space_width;
5161 p->font_height = it->font_height;
5162 p->voffset = it->voffset;
5163 p->string_from_display_prop_p = it->string_from_display_prop_p;
5164 p->display_ellipsis_p = 0;
5165 p->line_wrap = it->line_wrap;
5166 ++it->sp;
5167 }
5168
5169
5170 /* Restore IT's settings from IT->stack. Called, for example, when no
5171 more overlay strings must be processed, and we return to delivering
5172 display elements from a buffer, or when the end of a string from a
5173 `display' property is reached and we return to delivering display
5174 elements from an overlay string, or from a buffer. */
5175
5176 static void
5177 pop_it (it)
5178 struct it *it;
5179 {
5180 struct iterator_stack_entry *p;
5181
5182 xassert (it->sp > 0);
5183 --it->sp;
5184 p = it->stack + it->sp;
5185 it->stop_charpos = p->stop_charpos;
5186 it->prev_stop = p->prev_stop;
5187 it->base_level_stop = p->base_level_stop;
5188 it->cmp_it = p->cmp_it;
5189 it->face_id = p->face_id;
5190 it->current = p->current;
5191 it->position = p->position;
5192 it->string = p->string;
5193 it->from_overlay = p->from_overlay;
5194 if (NILP (it->string))
5195 SET_TEXT_POS (it->current.string_pos, -1, -1);
5196 it->method = p->method;
5197 switch (it->method)
5198 {
5199 case GET_FROM_IMAGE:
5200 it->image_id = p->u.image.image_id;
5201 it->object = p->u.image.object;
5202 it->slice = p->u.image.slice;
5203 break;
5204 case GET_FROM_STRETCH:
5205 it->object = p->u.comp.object;
5206 break;
5207 case GET_FROM_BUFFER:
5208 it->object = it->w->buffer;
5209 break;
5210 case GET_FROM_STRING:
5211 it->object = it->string;
5212 break;
5213 case GET_FROM_DISPLAY_VECTOR:
5214 if (it->s)
5215 it->method = GET_FROM_C_STRING;
5216 else if (STRINGP (it->string))
5217 it->method = GET_FROM_STRING;
5218 else
5219 {
5220 it->method = GET_FROM_BUFFER;
5221 it->object = it->w->buffer;
5222 }
5223 }
5224 it->end_charpos = p->end_charpos;
5225 it->string_nchars = p->string_nchars;
5226 it->area = p->area;
5227 it->multibyte_p = p->multibyte_p;
5228 it->avoid_cursor_p = p->avoid_cursor_p;
5229 it->space_width = p->space_width;
5230 it->font_height = p->font_height;
5231 it->voffset = p->voffset;
5232 it->string_from_display_prop_p = p->string_from_display_prop_p;
5233 it->line_wrap = p->line_wrap;
5234 }
5235
5236
5237 \f
5238 /***********************************************************************
5239 Moving over lines
5240 ***********************************************************************/
5241
5242 /* Set IT's current position to the previous line start. */
5243
5244 static void
5245 back_to_previous_line_start (it)
5246 struct it *it;
5247 {
5248 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5249 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5250 }
5251
5252
5253 /* Move IT to the next line start.
5254
5255 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5256 we skipped over part of the text (as opposed to moving the iterator
5257 continuously over the text). Otherwise, don't change the value
5258 of *SKIPPED_P.
5259
5260 Newlines may come from buffer text, overlay strings, or strings
5261 displayed via the `display' property. That's the reason we can't
5262 simply use find_next_newline_no_quit.
5263
5264 Note that this function may not skip over invisible text that is so
5265 because of text properties and immediately follows a newline. If
5266 it would, function reseat_at_next_visible_line_start, when called
5267 from set_iterator_to_next, would effectively make invisible
5268 characters following a newline part of the wrong glyph row, which
5269 leads to wrong cursor motion. */
5270
5271 static int
5272 forward_to_next_line_start (it, skipped_p)
5273 struct it *it;
5274 int *skipped_p;
5275 {
5276 int old_selective, newline_found_p, n;
5277 const int MAX_NEWLINE_DISTANCE = 500;
5278
5279 /* If already on a newline, just consume it to avoid unintended
5280 skipping over invisible text below. */
5281 if (it->what == IT_CHARACTER
5282 && it->c == '\n'
5283 && CHARPOS (it->position) == IT_CHARPOS (*it))
5284 {
5285 set_iterator_to_next (it, 0);
5286 it->c = 0;
5287 return 1;
5288 }
5289
5290 /* Don't handle selective display in the following. It's (a)
5291 unnecessary because it's done by the caller, and (b) leads to an
5292 infinite recursion because next_element_from_ellipsis indirectly
5293 calls this function. */
5294 old_selective = it->selective;
5295 it->selective = 0;
5296
5297 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5298 from buffer text. */
5299 for (n = newline_found_p = 0;
5300 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5301 n += STRINGP (it->string) ? 0 : 1)
5302 {
5303 if (!get_next_display_element (it))
5304 return 0;
5305 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5306 set_iterator_to_next (it, 0);
5307 }
5308
5309 /* If we didn't find a newline near enough, see if we can use a
5310 short-cut. */
5311 if (!newline_found_p)
5312 {
5313 int start = IT_CHARPOS (*it);
5314 int limit = find_next_newline_no_quit (start, 1);
5315 Lisp_Object pos;
5316
5317 xassert (!STRINGP (it->string));
5318
5319 /* If there isn't any `display' property in sight, and no
5320 overlays, we can just use the position of the newline in
5321 buffer text. */
5322 if (it->stop_charpos >= limit
5323 || ((pos = Fnext_single_property_change (make_number (start),
5324 Qdisplay,
5325 Qnil, make_number (limit)),
5326 NILP (pos))
5327 && next_overlay_change (start) == ZV))
5328 {
5329 IT_CHARPOS (*it) = limit;
5330 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5331 *skipped_p = newline_found_p = 1;
5332 }
5333 else
5334 {
5335 while (get_next_display_element (it)
5336 && !newline_found_p)
5337 {
5338 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5339 set_iterator_to_next (it, 0);
5340 }
5341 }
5342 }
5343
5344 it->selective = old_selective;
5345 return newline_found_p;
5346 }
5347
5348
5349 /* Set IT's current position to the previous visible line start. Skip
5350 invisible text that is so either due to text properties or due to
5351 selective display. Caution: this does not change IT->current_x and
5352 IT->hpos. */
5353
5354 static void
5355 back_to_previous_visible_line_start (it)
5356 struct it *it;
5357 {
5358 while (IT_CHARPOS (*it) > BEGV)
5359 {
5360 back_to_previous_line_start (it);
5361
5362 if (IT_CHARPOS (*it) <= BEGV)
5363 break;
5364
5365 /* If selective > 0, then lines indented more than its value are
5366 invisible. */
5367 if (it->selective > 0
5368 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5369 (double) it->selective)) /* iftc */
5370 continue;
5371
5372 /* Check the newline before point for invisibility. */
5373 {
5374 Lisp_Object prop;
5375 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5376 Qinvisible, it->window);
5377 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5378 continue;
5379 }
5380
5381 if (IT_CHARPOS (*it) <= BEGV)
5382 break;
5383
5384 {
5385 struct it it2;
5386 int pos;
5387 EMACS_INT beg, end;
5388 Lisp_Object val, overlay;
5389
5390 /* If newline is part of a composition, continue from start of composition */
5391 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5392 && beg < IT_CHARPOS (*it))
5393 goto replaced;
5394
5395 /* If newline is replaced by a display property, find start of overlay
5396 or interval and continue search from that point. */
5397 it2 = *it;
5398 pos = --IT_CHARPOS (it2);
5399 --IT_BYTEPOS (it2);
5400 it2.sp = 0;
5401 it2.string_from_display_prop_p = 0;
5402 if (handle_display_prop (&it2) == HANDLED_RETURN
5403 && !NILP (val = get_char_property_and_overlay
5404 (make_number (pos), Qdisplay, Qnil, &overlay))
5405 && (OVERLAYP (overlay)
5406 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5407 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5408 goto replaced;
5409
5410 /* Newline is not replaced by anything -- so we are done. */
5411 break;
5412
5413 replaced:
5414 if (beg < BEGV)
5415 beg = BEGV;
5416 IT_CHARPOS (*it) = beg;
5417 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5418 }
5419 }
5420
5421 it->continuation_lines_width = 0;
5422
5423 xassert (IT_CHARPOS (*it) >= BEGV);
5424 xassert (IT_CHARPOS (*it) == BEGV
5425 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5426 CHECK_IT (it);
5427 }
5428
5429
5430 /* Reseat iterator IT at the previous visible line start. Skip
5431 invisible text that is so either due to text properties or due to
5432 selective display. At the end, update IT's overlay information,
5433 face information etc. */
5434
5435 void
5436 reseat_at_previous_visible_line_start (it)
5437 struct it *it;
5438 {
5439 back_to_previous_visible_line_start (it);
5440 reseat (it, it->current.pos, 1);
5441 CHECK_IT (it);
5442 }
5443
5444
5445 /* Reseat iterator IT on the next visible line start in the current
5446 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5447 preceding the line start. Skip over invisible text that is so
5448 because of selective display. Compute faces, overlays etc at the
5449 new position. Note that this function does not skip over text that
5450 is invisible because of text properties. */
5451
5452 static void
5453 reseat_at_next_visible_line_start (it, on_newline_p)
5454 struct it *it;
5455 int on_newline_p;
5456 {
5457 int newline_found_p, skipped_p = 0;
5458
5459 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5460
5461 /* Skip over lines that are invisible because they are indented
5462 more than the value of IT->selective. */
5463 if (it->selective > 0)
5464 while (IT_CHARPOS (*it) < ZV
5465 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5466 (double) it->selective)) /* iftc */
5467 {
5468 xassert (IT_BYTEPOS (*it) == BEGV
5469 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5470 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5471 }
5472
5473 /* Position on the newline if that's what's requested. */
5474 if (on_newline_p && newline_found_p)
5475 {
5476 if (STRINGP (it->string))
5477 {
5478 if (IT_STRING_CHARPOS (*it) > 0)
5479 {
5480 --IT_STRING_CHARPOS (*it);
5481 --IT_STRING_BYTEPOS (*it);
5482 }
5483 }
5484 else if (IT_CHARPOS (*it) > BEGV)
5485 {
5486 --IT_CHARPOS (*it);
5487 --IT_BYTEPOS (*it);
5488 reseat (it, it->current.pos, 0);
5489 }
5490 }
5491 else if (skipped_p)
5492 reseat (it, it->current.pos, 0);
5493
5494 CHECK_IT (it);
5495 }
5496
5497
5498 \f
5499 /***********************************************************************
5500 Changing an iterator's position
5501 ***********************************************************************/
5502
5503 /* Change IT's current position to POS in current_buffer. If FORCE_P
5504 is non-zero, always check for text properties at the new position.
5505 Otherwise, text properties are only looked up if POS >=
5506 IT->check_charpos of a property. */
5507
5508 static void
5509 reseat (it, pos, force_p)
5510 struct it *it;
5511 struct text_pos pos;
5512 int force_p;
5513 {
5514 int original_pos = IT_CHARPOS (*it);
5515
5516 reseat_1 (it, pos, 0);
5517
5518 /* Determine where to check text properties. Avoid doing it
5519 where possible because text property lookup is very expensive. */
5520 if (force_p
5521 || CHARPOS (pos) > it->stop_charpos
5522 || CHARPOS (pos) < original_pos)
5523 {
5524 if (it->bidi_p)
5525 {
5526 /* For bidi iteration, we need to prime prev_stop and
5527 base_level_stop with our best estimations. */
5528 if (CHARPOS (pos) < it->prev_stop)
5529 {
5530 handle_stop_backwards (it, BEGV);
5531 if (CHARPOS (pos) < it->base_level_stop)
5532 it->base_level_stop = 0;
5533 }
5534 else if (CHARPOS (pos) > it->stop_charpos
5535 && it->stop_charpos >= BEGV)
5536 handle_stop_backwards (it, it->stop_charpos);
5537 else /* force_p */
5538 handle_stop (it);
5539 }
5540 else
5541 {
5542 handle_stop (it);
5543 it->prev_stop = it->base_level_stop = 0;
5544 }
5545
5546 }
5547
5548 CHECK_IT (it);
5549 }
5550
5551
5552 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5553 IT->stop_pos to POS, also. */
5554
5555 static void
5556 reseat_1 (it, pos, set_stop_p)
5557 struct it *it;
5558 struct text_pos pos;
5559 int set_stop_p;
5560 {
5561 /* Don't call this function when scanning a C string. */
5562 xassert (it->s == NULL);
5563
5564 /* POS must be a reasonable value. */
5565 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5566
5567 it->current.pos = it->position = pos;
5568 it->end_charpos = ZV;
5569 it->dpvec = NULL;
5570 it->current.dpvec_index = -1;
5571 it->current.overlay_string_index = -1;
5572 IT_STRING_CHARPOS (*it) = -1;
5573 IT_STRING_BYTEPOS (*it) = -1;
5574 it->string = Qnil;
5575 it->string_from_display_prop_p = 0;
5576 it->method = GET_FROM_BUFFER;
5577 it->object = it->w->buffer;
5578 it->area = TEXT_AREA;
5579 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5580 it->sp = 0;
5581 it->string_from_display_prop_p = 0;
5582 it->face_before_selective_p = 0;
5583 if (it->bidi_p)
5584 it->bidi_it.first_elt = 1;
5585
5586 if (set_stop_p)
5587 {
5588 it->stop_charpos = CHARPOS (pos);
5589 it->base_level_stop = CHARPOS (pos);
5590 }
5591 }
5592
5593
5594 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5595 If S is non-null, it is a C string to iterate over. Otherwise,
5596 STRING gives a Lisp string to iterate over.
5597
5598 If PRECISION > 0, don't return more then PRECISION number of
5599 characters from the string.
5600
5601 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5602 characters have been returned. FIELD_WIDTH < 0 means an infinite
5603 field width.
5604
5605 MULTIBYTE = 0 means disable processing of multibyte characters,
5606 MULTIBYTE > 0 means enable it,
5607 MULTIBYTE < 0 means use IT->multibyte_p.
5608
5609 IT must be initialized via a prior call to init_iterator before
5610 calling this function. */
5611
5612 static void
5613 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5614 struct it *it;
5615 unsigned char *s;
5616 Lisp_Object string;
5617 int charpos;
5618 int precision, field_width, multibyte;
5619 {
5620 /* No region in strings. */
5621 it->region_beg_charpos = it->region_end_charpos = -1;
5622
5623 /* No text property checks performed by default, but see below. */
5624 it->stop_charpos = -1;
5625
5626 /* Set iterator position and end position. */
5627 bzero (&it->current, sizeof it->current);
5628 it->current.overlay_string_index = -1;
5629 it->current.dpvec_index = -1;
5630 xassert (charpos >= 0);
5631
5632 /* If STRING is specified, use its multibyteness, otherwise use the
5633 setting of MULTIBYTE, if specified. */
5634 if (multibyte >= 0)
5635 it->multibyte_p = multibyte > 0;
5636
5637 if (s == NULL)
5638 {
5639 xassert (STRINGP (string));
5640 it->string = string;
5641 it->s = NULL;
5642 it->end_charpos = it->string_nchars = SCHARS (string);
5643 it->method = GET_FROM_STRING;
5644 it->current.string_pos = string_pos (charpos, string);
5645 }
5646 else
5647 {
5648 it->s = s;
5649 it->string = Qnil;
5650
5651 /* Note that we use IT->current.pos, not it->current.string_pos,
5652 for displaying C strings. */
5653 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5654 if (it->multibyte_p)
5655 {
5656 it->current.pos = c_string_pos (charpos, s, 1);
5657 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5658 }
5659 else
5660 {
5661 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5662 it->end_charpos = it->string_nchars = strlen (s);
5663 }
5664
5665 it->method = GET_FROM_C_STRING;
5666 }
5667
5668 /* PRECISION > 0 means don't return more than PRECISION characters
5669 from the string. */
5670 if (precision > 0 && it->end_charpos - charpos > precision)
5671 it->end_charpos = it->string_nchars = charpos + precision;
5672
5673 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5674 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5675 FIELD_WIDTH < 0 means infinite field width. This is useful for
5676 padding with `-' at the end of a mode line. */
5677 if (field_width < 0)
5678 field_width = INFINITY;
5679 if (field_width > it->end_charpos - charpos)
5680 it->end_charpos = charpos + field_width;
5681
5682 /* Use the standard display table for displaying strings. */
5683 if (DISP_TABLE_P (Vstandard_display_table))
5684 it->dp = XCHAR_TABLE (Vstandard_display_table);
5685
5686 it->stop_charpos = charpos;
5687 if (s == NULL && it->multibyte_p)
5688 {
5689 EMACS_INT endpos = SCHARS (it->string);
5690 if (endpos > it->end_charpos)
5691 endpos = it->end_charpos;
5692 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5693 it->string);
5694 }
5695 CHECK_IT (it);
5696 }
5697
5698
5699 \f
5700 /***********************************************************************
5701 Iteration
5702 ***********************************************************************/
5703
5704 /* Map enum it_method value to corresponding next_element_from_* function. */
5705
5706 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5707 {
5708 next_element_from_buffer,
5709 next_element_from_display_vector,
5710 next_element_from_string,
5711 next_element_from_c_string,
5712 next_element_from_image,
5713 next_element_from_stretch
5714 };
5715
5716 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5717
5718
5719 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5720 (possibly with the following characters). */
5721
5722 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5723 ((IT)->cmp_it.id >= 0 \
5724 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5725 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5726 END_CHARPOS, (IT)->w, \
5727 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5728 (IT)->string)))
5729
5730
5731 /* Load IT's display element fields with information about the next
5732 display element from the current position of IT. Value is zero if
5733 end of buffer (or C string) is reached. */
5734
5735 static struct frame *last_escape_glyph_frame = NULL;
5736 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5737 static int last_escape_glyph_merged_face_id = 0;
5738
5739 int
5740 get_next_display_element (it)
5741 struct it *it;
5742 {
5743 /* Non-zero means that we found a display element. Zero means that
5744 we hit the end of what we iterate over. Performance note: the
5745 function pointer `method' used here turns out to be faster than
5746 using a sequence of if-statements. */
5747 int success_p;
5748
5749 get_next:
5750 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5751
5752 if (it->what == IT_CHARACTER)
5753 {
5754 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5755 and only if (a) the resolved directionality of that character
5756 is R..." */
5757 /* FIXME: Do we need an exception for characters from display
5758 tables? */
5759 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5760 it->c = bidi_mirror_char (it->c);
5761 /* Map via display table or translate control characters.
5762 IT->c, IT->len etc. have been set to the next character by
5763 the function call above. If we have a display table, and it
5764 contains an entry for IT->c, translate it. Don't do this if
5765 IT->c itself comes from a display table, otherwise we could
5766 end up in an infinite recursion. (An alternative could be to
5767 count the recursion depth of this function and signal an
5768 error when a certain maximum depth is reached.) Is it worth
5769 it? */
5770 if (success_p && it->dpvec == NULL)
5771 {
5772 Lisp_Object dv;
5773 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5774 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5775 nbsp_or_shy = char_is_other;
5776 int decoded = it->c;
5777
5778 if (it->dp
5779 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5780 VECTORP (dv)))
5781 {
5782 struct Lisp_Vector *v = XVECTOR (dv);
5783
5784 /* Return the first character from the display table
5785 entry, if not empty. If empty, don't display the
5786 current character. */
5787 if (v->size)
5788 {
5789 it->dpvec_char_len = it->len;
5790 it->dpvec = v->contents;
5791 it->dpend = v->contents + v->size;
5792 it->current.dpvec_index = 0;
5793 it->dpvec_face_id = -1;
5794 it->saved_face_id = it->face_id;
5795 it->method = GET_FROM_DISPLAY_VECTOR;
5796 it->ellipsis_p = 0;
5797 }
5798 else
5799 {
5800 set_iterator_to_next (it, 0);
5801 }
5802 goto get_next;
5803 }
5804
5805 if (unibyte_display_via_language_environment
5806 && !ASCII_CHAR_P (it->c))
5807 decoded = DECODE_CHAR (unibyte, it->c);
5808
5809 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5810 {
5811 if (it->multibyte_p)
5812 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5813 : it->c == 0xAD ? char_is_soft_hyphen
5814 : char_is_other);
5815 else if (unibyte_display_via_language_environment)
5816 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5817 : decoded == 0xAD ? char_is_soft_hyphen
5818 : char_is_other);
5819 }
5820
5821 /* Translate control characters into `\003' or `^C' form.
5822 Control characters coming from a display table entry are
5823 currently not translated because we use IT->dpvec to hold
5824 the translation. This could easily be changed but I
5825 don't believe that it is worth doing.
5826
5827 If it->multibyte_p is nonzero, non-printable non-ASCII
5828 characters are also translated to octal form.
5829
5830 If it->multibyte_p is zero, eight-bit characters that
5831 don't have corresponding multibyte char code are also
5832 translated to octal form. */
5833 if ((it->c < ' '
5834 ? (it->area != TEXT_AREA
5835 /* In mode line, treat \n, \t like other crl chars. */
5836 || (it->c != '\t'
5837 && it->glyph_row
5838 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5839 || (it->c != '\n' && it->c != '\t'))
5840 : (nbsp_or_shy
5841 || (it->multibyte_p
5842 ? ! CHAR_PRINTABLE_P (it->c)
5843 : (! unibyte_display_via_language_environment
5844 ? it->c >= 0x80
5845 : (decoded >= 0x80 && decoded < 0xA0))))))
5846 {
5847 /* IT->c is a control character which must be displayed
5848 either as '\003' or as `^C' where the '\\' and '^'
5849 can be defined in the display table. Fill
5850 IT->ctl_chars with glyphs for what we have to
5851 display. Then, set IT->dpvec to these glyphs. */
5852 Lisp_Object gc;
5853 int ctl_len;
5854 int face_id, lface_id = 0 ;
5855 int escape_glyph;
5856
5857 /* Handle control characters with ^. */
5858
5859 if (it->c < 128 && it->ctl_arrow_p)
5860 {
5861 int g;
5862
5863 g = '^'; /* default glyph for Control */
5864 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5865 if (it->dp
5866 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5867 && GLYPH_CODE_CHAR_VALID_P (gc))
5868 {
5869 g = GLYPH_CODE_CHAR (gc);
5870 lface_id = GLYPH_CODE_FACE (gc);
5871 }
5872 if (lface_id)
5873 {
5874 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5875 }
5876 else if (it->f == last_escape_glyph_frame
5877 && it->face_id == last_escape_glyph_face_id)
5878 {
5879 face_id = last_escape_glyph_merged_face_id;
5880 }
5881 else
5882 {
5883 /* Merge the escape-glyph face into the current face. */
5884 face_id = merge_faces (it->f, Qescape_glyph, 0,
5885 it->face_id);
5886 last_escape_glyph_frame = it->f;
5887 last_escape_glyph_face_id = it->face_id;
5888 last_escape_glyph_merged_face_id = face_id;
5889 }
5890
5891 XSETINT (it->ctl_chars[0], g);
5892 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5893 ctl_len = 2;
5894 goto display_control;
5895 }
5896
5897 /* Handle non-break space in the mode where it only gets
5898 highlighting. */
5899
5900 if (EQ (Vnobreak_char_display, Qt)
5901 && nbsp_or_shy == char_is_nbsp)
5902 {
5903 /* Merge the no-break-space face into the current face. */
5904 face_id = merge_faces (it->f, Qnobreak_space, 0,
5905 it->face_id);
5906
5907 it->c = ' ';
5908 XSETINT (it->ctl_chars[0], ' ');
5909 ctl_len = 1;
5910 goto display_control;
5911 }
5912
5913 /* Handle sequences that start with the "escape glyph". */
5914
5915 /* the default escape glyph is \. */
5916 escape_glyph = '\\';
5917
5918 if (it->dp
5919 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5920 && GLYPH_CODE_CHAR_VALID_P (gc))
5921 {
5922 escape_glyph = GLYPH_CODE_CHAR (gc);
5923 lface_id = GLYPH_CODE_FACE (gc);
5924 }
5925 if (lface_id)
5926 {
5927 /* The display table specified a face.
5928 Merge it into face_id and also into escape_glyph. */
5929 face_id = merge_faces (it->f, Qt, lface_id,
5930 it->face_id);
5931 }
5932 else if (it->f == last_escape_glyph_frame
5933 && it->face_id == last_escape_glyph_face_id)
5934 {
5935 face_id = last_escape_glyph_merged_face_id;
5936 }
5937 else
5938 {
5939 /* Merge the escape-glyph face into the current face. */
5940 face_id = merge_faces (it->f, Qescape_glyph, 0,
5941 it->face_id);
5942 last_escape_glyph_frame = it->f;
5943 last_escape_glyph_face_id = it->face_id;
5944 last_escape_glyph_merged_face_id = face_id;
5945 }
5946
5947 /* Handle soft hyphens in the mode where they only get
5948 highlighting. */
5949
5950 if (EQ (Vnobreak_char_display, Qt)
5951 && nbsp_or_shy == char_is_soft_hyphen)
5952 {
5953 it->c = '-';
5954 XSETINT (it->ctl_chars[0], '-');
5955 ctl_len = 1;
5956 goto display_control;
5957 }
5958
5959 /* Handle non-break space and soft hyphen
5960 with the escape glyph. */
5961
5962 if (nbsp_or_shy)
5963 {
5964 XSETINT (it->ctl_chars[0], escape_glyph);
5965 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5966 XSETINT (it->ctl_chars[1], it->c);
5967 ctl_len = 2;
5968 goto display_control;
5969 }
5970
5971 {
5972 unsigned char str[MAX_MULTIBYTE_LENGTH];
5973 int len;
5974 int i;
5975
5976 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5977 if (CHAR_BYTE8_P (it->c))
5978 {
5979 str[0] = CHAR_TO_BYTE8 (it->c);
5980 len = 1;
5981 }
5982 else if (it->c < 256)
5983 {
5984 str[0] = it->c;
5985 len = 1;
5986 }
5987 else
5988 {
5989 /* It's an invalid character, which shouldn't
5990 happen actually, but due to bugs it may
5991 happen. Let's print the char as is, there's
5992 not much meaningful we can do with it. */
5993 str[0] = it->c;
5994 str[1] = it->c >> 8;
5995 str[2] = it->c >> 16;
5996 str[3] = it->c >> 24;
5997 len = 4;
5998 }
5999
6000 for (i = 0; i < len; i++)
6001 {
6002 int g;
6003 XSETINT (it->ctl_chars[i * 4], escape_glyph);
6004 /* Insert three more glyphs into IT->ctl_chars for
6005 the octal display of the character. */
6006 g = ((str[i] >> 6) & 7) + '0';
6007 XSETINT (it->ctl_chars[i * 4 + 1], g);
6008 g = ((str[i] >> 3) & 7) + '0';
6009 XSETINT (it->ctl_chars[i * 4 + 2], g);
6010 g = (str[i] & 7) + '0';
6011 XSETINT (it->ctl_chars[i * 4 + 3], g);
6012 }
6013 ctl_len = len * 4;
6014 }
6015
6016 display_control:
6017 /* Set up IT->dpvec and return first character from it. */
6018 it->dpvec_char_len = it->len;
6019 it->dpvec = it->ctl_chars;
6020 it->dpend = it->dpvec + ctl_len;
6021 it->current.dpvec_index = 0;
6022 it->dpvec_face_id = face_id;
6023 it->saved_face_id = it->face_id;
6024 it->method = GET_FROM_DISPLAY_VECTOR;
6025 it->ellipsis_p = 0;
6026 goto get_next;
6027 }
6028 }
6029 }
6030
6031 #ifdef HAVE_WINDOW_SYSTEM
6032 /* Adjust face id for a multibyte character. There are no multibyte
6033 character in unibyte text. */
6034 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6035 && it->multibyte_p
6036 && success_p
6037 && FRAME_WINDOW_P (it->f))
6038 {
6039 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6040
6041 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6042 {
6043 /* Automatic composition with glyph-string. */
6044 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6045
6046 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6047 }
6048 else
6049 {
6050 int pos = (it->s ? -1
6051 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6052 : IT_CHARPOS (*it));
6053
6054 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6055 }
6056 }
6057 #endif
6058
6059 /* Is this character the last one of a run of characters with
6060 box? If yes, set IT->end_of_box_run_p to 1. */
6061 if (it->face_box_p
6062 && it->s == NULL)
6063 {
6064 if (it->method == GET_FROM_STRING && it->sp)
6065 {
6066 int face_id = underlying_face_id (it);
6067 struct face *face = FACE_FROM_ID (it->f, face_id);
6068
6069 if (face)
6070 {
6071 if (face->box == FACE_NO_BOX)
6072 {
6073 /* If the box comes from face properties in a
6074 display string, check faces in that string. */
6075 int string_face_id = face_after_it_pos (it);
6076 it->end_of_box_run_p
6077 = (FACE_FROM_ID (it->f, string_face_id)->box
6078 == FACE_NO_BOX);
6079 }
6080 /* Otherwise, the box comes from the underlying face.
6081 If this is the last string character displayed, check
6082 the next buffer location. */
6083 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6084 && (it->current.overlay_string_index
6085 == it->n_overlay_strings - 1))
6086 {
6087 EMACS_INT ignore;
6088 int next_face_id;
6089 struct text_pos pos = it->current.pos;
6090 INC_TEXT_POS (pos, it->multibyte_p);
6091
6092 next_face_id = face_at_buffer_position
6093 (it->w, CHARPOS (pos), it->region_beg_charpos,
6094 it->region_end_charpos, &ignore,
6095 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6096 -1);
6097 it->end_of_box_run_p
6098 = (FACE_FROM_ID (it->f, next_face_id)->box
6099 == FACE_NO_BOX);
6100 }
6101 }
6102 }
6103 else
6104 {
6105 int face_id = face_after_it_pos (it);
6106 it->end_of_box_run_p
6107 = (face_id != it->face_id
6108 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6109 }
6110 }
6111
6112 /* Value is 0 if end of buffer or string reached. */
6113 return success_p;
6114 }
6115
6116
6117 /* Move IT to the next display element.
6118
6119 RESEAT_P non-zero means if called on a newline in buffer text,
6120 skip to the next visible line start.
6121
6122 Functions get_next_display_element and set_iterator_to_next are
6123 separate because I find this arrangement easier to handle than a
6124 get_next_display_element function that also increments IT's
6125 position. The way it is we can first look at an iterator's current
6126 display element, decide whether it fits on a line, and if it does,
6127 increment the iterator position. The other way around we probably
6128 would either need a flag indicating whether the iterator has to be
6129 incremented the next time, or we would have to implement a
6130 decrement position function which would not be easy to write. */
6131
6132 void
6133 set_iterator_to_next (it, reseat_p)
6134 struct it *it;
6135 int reseat_p;
6136 {
6137 /* Reset flags indicating start and end of a sequence of characters
6138 with box. Reset them at the start of this function because
6139 moving the iterator to a new position might set them. */
6140 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6141
6142 switch (it->method)
6143 {
6144 case GET_FROM_BUFFER:
6145 /* The current display element of IT is a character from
6146 current_buffer. Advance in the buffer, and maybe skip over
6147 invisible lines that are so because of selective display. */
6148 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6149 reseat_at_next_visible_line_start (it, 0);
6150 else if (it->cmp_it.id >= 0)
6151 {
6152 IT_CHARPOS (*it) += it->cmp_it.nchars;
6153 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6154 if (it->cmp_it.to < it->cmp_it.nglyphs)
6155 it->cmp_it.from = it->cmp_it.to;
6156 else
6157 {
6158 it->cmp_it.id = -1;
6159 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6160 IT_BYTEPOS (*it), it->stop_charpos,
6161 Qnil);
6162 }
6163 }
6164 else
6165 {
6166 xassert (it->len != 0);
6167
6168 if (!it->bidi_p)
6169 {
6170 IT_BYTEPOS (*it) += it->len;
6171 IT_CHARPOS (*it) += 1;
6172 }
6173 else
6174 {
6175 /* If this is a new paragraph, determine its base
6176 direction (a.k.a. its base embedding level). */
6177 if (it->bidi_it.new_paragraph)
6178 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6179 bidi_get_next_char_visually (&it->bidi_it);
6180 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6181 IT_CHARPOS (*it) = it->bidi_it.charpos;
6182 }
6183 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6184 }
6185 break;
6186
6187 case GET_FROM_C_STRING:
6188 /* Current display element of IT is from a C string. */
6189 IT_BYTEPOS (*it) += it->len;
6190 IT_CHARPOS (*it) += 1;
6191 break;
6192
6193 case GET_FROM_DISPLAY_VECTOR:
6194 /* Current display element of IT is from a display table entry.
6195 Advance in the display table definition. Reset it to null if
6196 end reached, and continue with characters from buffers/
6197 strings. */
6198 ++it->current.dpvec_index;
6199
6200 /* Restore face of the iterator to what they were before the
6201 display vector entry (these entries may contain faces). */
6202 it->face_id = it->saved_face_id;
6203
6204 if (it->dpvec + it->current.dpvec_index == it->dpend)
6205 {
6206 int recheck_faces = it->ellipsis_p;
6207
6208 if (it->s)
6209 it->method = GET_FROM_C_STRING;
6210 else if (STRINGP (it->string))
6211 it->method = GET_FROM_STRING;
6212 else
6213 {
6214 it->method = GET_FROM_BUFFER;
6215 it->object = it->w->buffer;
6216 }
6217
6218 it->dpvec = NULL;
6219 it->current.dpvec_index = -1;
6220
6221 /* Skip over characters which were displayed via IT->dpvec. */
6222 if (it->dpvec_char_len < 0)
6223 reseat_at_next_visible_line_start (it, 1);
6224 else if (it->dpvec_char_len > 0)
6225 {
6226 if (it->method == GET_FROM_STRING
6227 && it->n_overlay_strings > 0)
6228 it->ignore_overlay_strings_at_pos_p = 1;
6229 it->len = it->dpvec_char_len;
6230 set_iterator_to_next (it, reseat_p);
6231 }
6232
6233 /* Maybe recheck faces after display vector */
6234 if (recheck_faces)
6235 it->stop_charpos = IT_CHARPOS (*it);
6236 }
6237 break;
6238
6239 case GET_FROM_STRING:
6240 /* Current display element is a character from a Lisp string. */
6241 xassert (it->s == NULL && STRINGP (it->string));
6242 if (it->cmp_it.id >= 0)
6243 {
6244 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6245 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6246 if (it->cmp_it.to < it->cmp_it.nglyphs)
6247 it->cmp_it.from = it->cmp_it.to;
6248 else
6249 {
6250 it->cmp_it.id = -1;
6251 composition_compute_stop_pos (&it->cmp_it,
6252 IT_STRING_CHARPOS (*it),
6253 IT_STRING_BYTEPOS (*it),
6254 it->stop_charpos, it->string);
6255 }
6256 }
6257 else
6258 {
6259 IT_STRING_BYTEPOS (*it) += it->len;
6260 IT_STRING_CHARPOS (*it) += 1;
6261 }
6262
6263 consider_string_end:
6264
6265 if (it->current.overlay_string_index >= 0)
6266 {
6267 /* IT->string is an overlay string. Advance to the
6268 next, if there is one. */
6269 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6270 {
6271 it->ellipsis_p = 0;
6272 next_overlay_string (it);
6273 if (it->ellipsis_p)
6274 setup_for_ellipsis (it, 0);
6275 }
6276 }
6277 else
6278 {
6279 /* IT->string is not an overlay string. If we reached
6280 its end, and there is something on IT->stack, proceed
6281 with what is on the stack. This can be either another
6282 string, this time an overlay string, or a buffer. */
6283 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6284 && it->sp > 0)
6285 {
6286 pop_it (it);
6287 if (it->method == GET_FROM_STRING)
6288 goto consider_string_end;
6289 }
6290 }
6291 break;
6292
6293 case GET_FROM_IMAGE:
6294 case GET_FROM_STRETCH:
6295 /* The position etc with which we have to proceed are on
6296 the stack. The position may be at the end of a string,
6297 if the `display' property takes up the whole string. */
6298 xassert (it->sp > 0);
6299 pop_it (it);
6300 if (it->method == GET_FROM_STRING)
6301 goto consider_string_end;
6302 break;
6303
6304 default:
6305 /* There are no other methods defined, so this should be a bug. */
6306 abort ();
6307 }
6308
6309 xassert (it->method != GET_FROM_STRING
6310 || (STRINGP (it->string)
6311 && IT_STRING_CHARPOS (*it) >= 0));
6312 }
6313
6314 /* Load IT's display element fields with information about the next
6315 display element which comes from a display table entry or from the
6316 result of translating a control character to one of the forms `^C'
6317 or `\003'.
6318
6319 IT->dpvec holds the glyphs to return as characters.
6320 IT->saved_face_id holds the face id before the display vector--it
6321 is restored into IT->face_id in set_iterator_to_next. */
6322
6323 static int
6324 next_element_from_display_vector (it)
6325 struct it *it;
6326 {
6327 Lisp_Object gc;
6328
6329 /* Precondition. */
6330 xassert (it->dpvec && it->current.dpvec_index >= 0);
6331
6332 it->face_id = it->saved_face_id;
6333
6334 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6335 That seemed totally bogus - so I changed it... */
6336 gc = it->dpvec[it->current.dpvec_index];
6337
6338 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6339 {
6340 it->c = GLYPH_CODE_CHAR (gc);
6341 it->len = CHAR_BYTES (it->c);
6342
6343 /* The entry may contain a face id to use. Such a face id is
6344 the id of a Lisp face, not a realized face. A face id of
6345 zero means no face is specified. */
6346 if (it->dpvec_face_id >= 0)
6347 it->face_id = it->dpvec_face_id;
6348 else
6349 {
6350 int lface_id = GLYPH_CODE_FACE (gc);
6351 if (lface_id > 0)
6352 it->face_id = merge_faces (it->f, Qt, lface_id,
6353 it->saved_face_id);
6354 }
6355 }
6356 else
6357 /* Display table entry is invalid. Return a space. */
6358 it->c = ' ', it->len = 1;
6359
6360 /* Don't change position and object of the iterator here. They are
6361 still the values of the character that had this display table
6362 entry or was translated, and that's what we want. */
6363 it->what = IT_CHARACTER;
6364 return 1;
6365 }
6366
6367
6368 /* Load IT with the next display element from Lisp string IT->string.
6369 IT->current.string_pos is the current position within the string.
6370 If IT->current.overlay_string_index >= 0, the Lisp string is an
6371 overlay string. */
6372
6373 static int
6374 next_element_from_string (it)
6375 struct it *it;
6376 {
6377 struct text_pos position;
6378
6379 xassert (STRINGP (it->string));
6380 xassert (IT_STRING_CHARPOS (*it) >= 0);
6381 position = it->current.string_pos;
6382
6383 /* Time to check for invisible text? */
6384 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6385 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6386 {
6387 handle_stop (it);
6388
6389 /* Since a handler may have changed IT->method, we must
6390 recurse here. */
6391 return GET_NEXT_DISPLAY_ELEMENT (it);
6392 }
6393
6394 if (it->current.overlay_string_index >= 0)
6395 {
6396 /* Get the next character from an overlay string. In overlay
6397 strings, There is no field width or padding with spaces to
6398 do. */
6399 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6400 {
6401 it->what = IT_EOB;
6402 return 0;
6403 }
6404 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6405 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6406 && next_element_from_composition (it))
6407 {
6408 return 1;
6409 }
6410 else if (STRING_MULTIBYTE (it->string))
6411 {
6412 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6413 const unsigned char *s = (SDATA (it->string)
6414 + IT_STRING_BYTEPOS (*it));
6415 it->c = string_char_and_length (s, &it->len);
6416 }
6417 else
6418 {
6419 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6420 it->len = 1;
6421 }
6422 }
6423 else
6424 {
6425 /* Get the next character from a Lisp string that is not an
6426 overlay string. Such strings come from the mode line, for
6427 example. We may have to pad with spaces, or truncate the
6428 string. See also next_element_from_c_string. */
6429 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6430 {
6431 it->what = IT_EOB;
6432 return 0;
6433 }
6434 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6435 {
6436 /* Pad with spaces. */
6437 it->c = ' ', it->len = 1;
6438 CHARPOS (position) = BYTEPOS (position) = -1;
6439 }
6440 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6441 IT_STRING_BYTEPOS (*it), it->string_nchars)
6442 && next_element_from_composition (it))
6443 {
6444 return 1;
6445 }
6446 else if (STRING_MULTIBYTE (it->string))
6447 {
6448 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6449 const unsigned char *s = (SDATA (it->string)
6450 + IT_STRING_BYTEPOS (*it));
6451 it->c = string_char_and_length (s, &it->len);
6452 }
6453 else
6454 {
6455 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6456 it->len = 1;
6457 }
6458 }
6459
6460 /* Record what we have and where it came from. */
6461 it->what = IT_CHARACTER;
6462 it->object = it->string;
6463 it->position = position;
6464 return 1;
6465 }
6466
6467
6468 /* Load IT with next display element from C string IT->s.
6469 IT->string_nchars is the maximum number of characters to return
6470 from the string. IT->end_charpos may be greater than
6471 IT->string_nchars when this function is called, in which case we
6472 may have to return padding spaces. Value is zero if end of string
6473 reached, including padding spaces. */
6474
6475 static int
6476 next_element_from_c_string (it)
6477 struct it *it;
6478 {
6479 int success_p = 1;
6480
6481 xassert (it->s);
6482 it->what = IT_CHARACTER;
6483 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6484 it->object = Qnil;
6485
6486 /* IT's position can be greater IT->string_nchars in case a field
6487 width or precision has been specified when the iterator was
6488 initialized. */
6489 if (IT_CHARPOS (*it) >= it->end_charpos)
6490 {
6491 /* End of the game. */
6492 it->what = IT_EOB;
6493 success_p = 0;
6494 }
6495 else if (IT_CHARPOS (*it) >= it->string_nchars)
6496 {
6497 /* Pad with spaces. */
6498 it->c = ' ', it->len = 1;
6499 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6500 }
6501 else if (it->multibyte_p)
6502 {
6503 /* Implementation note: The calls to strlen apparently aren't a
6504 performance problem because there is no noticeable performance
6505 difference between Emacs running in unibyte or multibyte mode. */
6506 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6507 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6508 }
6509 else
6510 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6511
6512 return success_p;
6513 }
6514
6515
6516 /* Set up IT to return characters from an ellipsis, if appropriate.
6517 The definition of the ellipsis glyphs may come from a display table
6518 entry. This function fills IT with the first glyph from the
6519 ellipsis if an ellipsis is to be displayed. */
6520
6521 static int
6522 next_element_from_ellipsis (it)
6523 struct it *it;
6524 {
6525 if (it->selective_display_ellipsis_p)
6526 setup_for_ellipsis (it, it->len);
6527 else
6528 {
6529 /* The face at the current position may be different from the
6530 face we find after the invisible text. Remember what it
6531 was in IT->saved_face_id, and signal that it's there by
6532 setting face_before_selective_p. */
6533 it->saved_face_id = it->face_id;
6534 it->method = GET_FROM_BUFFER;
6535 it->object = it->w->buffer;
6536 reseat_at_next_visible_line_start (it, 1);
6537 it->face_before_selective_p = 1;
6538 }
6539
6540 return GET_NEXT_DISPLAY_ELEMENT (it);
6541 }
6542
6543
6544 /* Deliver an image display element. The iterator IT is already
6545 filled with image information (done in handle_display_prop). Value
6546 is always 1. */
6547
6548
6549 static int
6550 next_element_from_image (it)
6551 struct it *it;
6552 {
6553 it->what = IT_IMAGE;
6554 return 1;
6555 }
6556
6557
6558 /* Fill iterator IT with next display element from a stretch glyph
6559 property. IT->object is the value of the text property. Value is
6560 always 1. */
6561
6562 static int
6563 next_element_from_stretch (it)
6564 struct it *it;
6565 {
6566 it->what = IT_STRETCH;
6567 return 1;
6568 }
6569
6570 /* Scan forward from CHARPOS in the current buffer, until we find a
6571 stop position > current IT's position. Then handle the stop
6572 position before that. This is called when we bump into a stop
6573 position while reordering bidirectional text. */
6574
6575 static void
6576 handle_stop_backwards (it, charpos)
6577 struct it *it;
6578 EMACS_INT charpos;
6579 {
6580 EMACS_INT where_we_are = IT_CHARPOS (*it);
6581 struct display_pos save_current = it->current;
6582 struct text_pos save_position = it->position;
6583 struct text_pos pos1;
6584 EMACS_INT next_stop;
6585
6586 /* Scan in strict logical order. */
6587 it->bidi_p = 0;
6588 do
6589 {
6590 it->prev_stop = charpos;
6591 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6592 reseat_1 (it, pos1, 0);
6593 compute_stop_pos (it);
6594 /* We must advance forward, right? */
6595 if (it->stop_charpos <= it->prev_stop)
6596 abort ();
6597 charpos = it->stop_charpos;
6598 }
6599 while (charpos <= where_we_are);
6600
6601 next_stop = it->stop_charpos;
6602 it->stop_charpos = it->prev_stop;
6603 it->bidi_p = 1;
6604 it->current = save_current;
6605 it->position = save_position;
6606 handle_stop (it);
6607 it->stop_charpos = next_stop;
6608 }
6609
6610 /* Load IT with the next display element from current_buffer. Value
6611 is zero if end of buffer reached. IT->stop_charpos is the next
6612 position at which to stop and check for text properties or buffer
6613 end. */
6614
6615 static int
6616 next_element_from_buffer (it)
6617 struct it *it;
6618 {
6619 int success_p = 1;
6620
6621 xassert (IT_CHARPOS (*it) >= BEGV);
6622
6623 /* With bidi reordering, the character to display might not be the
6624 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6625 we were reseat()ed to a new buffer position, which is potentially
6626 a different paragraph. */
6627 if (it->bidi_p && it->bidi_it.first_elt)
6628 {
6629 it->bidi_it.charpos = IT_CHARPOS (*it);
6630 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6631 /* If we are at the beginning of a line, we can produce the next
6632 element right away. */
6633 if (it->bidi_it.bytepos == BEGV_BYTE
6634 /* FIXME: Should support all Unicode line separators. */
6635 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6636 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6637 {
6638 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6639 bidi_get_next_char_visually (&it->bidi_it);
6640 }
6641 else
6642 {
6643 int orig_bytepos = IT_BYTEPOS (*it);
6644
6645 /* We need to prime the bidi iterator starting at the line's
6646 beginning, before we will be able to produce the next
6647 element. */
6648 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6649 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6650 it->bidi_it.charpos = IT_CHARPOS (*it);
6651 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6652 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6653 do
6654 {
6655 /* Now return to buffer position where we were asked to
6656 get the next display element, and produce that. */
6657 bidi_get_next_char_visually (&it->bidi_it);
6658 }
6659 while (it->bidi_it.bytepos != orig_bytepos
6660 && it->bidi_it.bytepos < ZV_BYTE);
6661 }
6662
6663 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6664 /* Adjust IT's position information to where we ended up. */
6665 IT_CHARPOS (*it) = it->bidi_it.charpos;
6666 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6667 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6668 }
6669
6670 if (IT_CHARPOS (*it) >= it->stop_charpos)
6671 {
6672 if (IT_CHARPOS (*it) >= it->end_charpos)
6673 {
6674 int overlay_strings_follow_p;
6675
6676 /* End of the game, except when overlay strings follow that
6677 haven't been returned yet. */
6678 if (it->overlay_strings_at_end_processed_p)
6679 overlay_strings_follow_p = 0;
6680 else
6681 {
6682 it->overlay_strings_at_end_processed_p = 1;
6683 overlay_strings_follow_p = get_overlay_strings (it, 0);
6684 }
6685
6686 if (overlay_strings_follow_p)
6687 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6688 else
6689 {
6690 it->what = IT_EOB;
6691 it->position = it->current.pos;
6692 success_p = 0;
6693 }
6694 }
6695 else if (!(!it->bidi_p
6696 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6697 || IT_CHARPOS (*it) == it->stop_charpos))
6698 {
6699 /* With bidi non-linear iteration, we could find ourselves
6700 far beyond the last computed stop_charpos, with several
6701 other stop positions in between that we missed. Scan
6702 them all now, in buffer's logical order, until we find
6703 and handle the last stop_charpos that precedes our
6704 current position. */
6705 handle_stop_backwards (it, it->stop_charpos);
6706 return GET_NEXT_DISPLAY_ELEMENT (it);
6707 }
6708 else
6709 {
6710 if (it->bidi_p)
6711 {
6712 /* Take note of the stop position we just moved across,
6713 for when we will move back across it. */
6714 it->prev_stop = it->stop_charpos;
6715 /* If we are at base paragraph embedding level, take
6716 note of the last stop position seen at this
6717 level. */
6718 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6719 it->base_level_stop = it->stop_charpos;
6720 }
6721 handle_stop (it);
6722 return GET_NEXT_DISPLAY_ELEMENT (it);
6723 }
6724 }
6725 else if (it->bidi_p
6726 /* We can sometimes back up for reasons that have nothing
6727 to do with bidi reordering. E.g., compositions. The
6728 code below is only needed when we are above the base
6729 embedding level, so test for that explicitly. */
6730 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6731 && IT_CHARPOS (*it) < it->prev_stop)
6732 {
6733 if (it->base_level_stop <= 0)
6734 it->base_level_stop = BEGV;
6735 if (IT_CHARPOS (*it) < it->base_level_stop)
6736 abort ();
6737 handle_stop_backwards (it, it->base_level_stop);
6738 return GET_NEXT_DISPLAY_ELEMENT (it);
6739 }
6740 else
6741 {
6742 /* No face changes, overlays etc. in sight, so just return a
6743 character from current_buffer. */
6744 unsigned char *p;
6745
6746 /* Maybe run the redisplay end trigger hook. Performance note:
6747 This doesn't seem to cost measurable time. */
6748 if (it->redisplay_end_trigger_charpos
6749 && it->glyph_row
6750 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6751 run_redisplay_end_trigger_hook (it);
6752
6753 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6754 it->end_charpos)
6755 && next_element_from_composition (it))
6756 {
6757 return 1;
6758 }
6759
6760 /* Get the next character, maybe multibyte. */
6761 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6762 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6763 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6764 else
6765 it->c = *p, it->len = 1;
6766
6767 /* Record what we have and where it came from. */
6768 it->what = IT_CHARACTER;
6769 it->object = it->w->buffer;
6770 it->position = it->current.pos;
6771
6772 /* Normally we return the character found above, except when we
6773 really want to return an ellipsis for selective display. */
6774 if (it->selective)
6775 {
6776 if (it->c == '\n')
6777 {
6778 /* A value of selective > 0 means hide lines indented more
6779 than that number of columns. */
6780 if (it->selective > 0
6781 && IT_CHARPOS (*it) + 1 < ZV
6782 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6783 IT_BYTEPOS (*it) + 1,
6784 (double) it->selective)) /* iftc */
6785 {
6786 success_p = next_element_from_ellipsis (it);
6787 it->dpvec_char_len = -1;
6788 }
6789 }
6790 else if (it->c == '\r' && it->selective == -1)
6791 {
6792 /* A value of selective == -1 means that everything from the
6793 CR to the end of the line is invisible, with maybe an
6794 ellipsis displayed for it. */
6795 success_p = next_element_from_ellipsis (it);
6796 it->dpvec_char_len = -1;
6797 }
6798 }
6799 }
6800
6801 /* Value is zero if end of buffer reached. */
6802 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6803 return success_p;
6804 }
6805
6806
6807 /* Run the redisplay end trigger hook for IT. */
6808
6809 static void
6810 run_redisplay_end_trigger_hook (it)
6811 struct it *it;
6812 {
6813 Lisp_Object args[3];
6814
6815 /* IT->glyph_row should be non-null, i.e. we should be actually
6816 displaying something, or otherwise we should not run the hook. */
6817 xassert (it->glyph_row);
6818
6819 /* Set up hook arguments. */
6820 args[0] = Qredisplay_end_trigger_functions;
6821 args[1] = it->window;
6822 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6823 it->redisplay_end_trigger_charpos = 0;
6824
6825 /* Since we are *trying* to run these functions, don't try to run
6826 them again, even if they get an error. */
6827 it->w->redisplay_end_trigger = Qnil;
6828 Frun_hook_with_args (3, args);
6829
6830 /* Notice if it changed the face of the character we are on. */
6831 handle_face_prop (it);
6832 }
6833
6834
6835 /* Deliver a composition display element. Unlike the other
6836 next_element_from_XXX, this function is not registered in the array
6837 get_next_element[]. It is called from next_element_from_buffer and
6838 next_element_from_string when necessary. */
6839
6840 static int
6841 next_element_from_composition (it)
6842 struct it *it;
6843 {
6844 it->what = IT_COMPOSITION;
6845 it->len = it->cmp_it.nbytes;
6846 if (STRINGP (it->string))
6847 {
6848 if (it->c < 0)
6849 {
6850 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6851 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6852 return 0;
6853 }
6854 it->position = it->current.string_pos;
6855 it->object = it->string;
6856 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6857 IT_STRING_BYTEPOS (*it), it->string);
6858 }
6859 else
6860 {
6861 if (it->c < 0)
6862 {
6863 IT_CHARPOS (*it) += it->cmp_it.nchars;
6864 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6865 return 0;
6866 }
6867 it->position = it->current.pos;
6868 it->object = it->w->buffer;
6869 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6870 IT_BYTEPOS (*it), Qnil);
6871 }
6872 return 1;
6873 }
6874
6875
6876 \f
6877 /***********************************************************************
6878 Moving an iterator without producing glyphs
6879 ***********************************************************************/
6880
6881 /* Check if iterator is at a position corresponding to a valid buffer
6882 position after some move_it_ call. */
6883
6884 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6885 ((it)->method == GET_FROM_STRING \
6886 ? IT_STRING_CHARPOS (*it) == 0 \
6887 : 1)
6888
6889
6890 /* Move iterator IT to a specified buffer or X position within one
6891 line on the display without producing glyphs.
6892
6893 OP should be a bit mask including some or all of these bits:
6894 MOVE_TO_X: Stop upon reaching x-position TO_X.
6895 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6896 Regardless of OP's value, stop upon reaching the end of the display line.
6897
6898 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6899 This means, in particular, that TO_X includes window's horizontal
6900 scroll amount.
6901
6902 The return value has several possible values that
6903 say what condition caused the scan to stop:
6904
6905 MOVE_POS_MATCH_OR_ZV
6906 - when TO_POS or ZV was reached.
6907
6908 MOVE_X_REACHED
6909 -when TO_X was reached before TO_POS or ZV were reached.
6910
6911 MOVE_LINE_CONTINUED
6912 - when we reached the end of the display area and the line must
6913 be continued.
6914
6915 MOVE_LINE_TRUNCATED
6916 - when we reached the end of the display area and the line is
6917 truncated.
6918
6919 MOVE_NEWLINE_OR_CR
6920 - when we stopped at a line end, i.e. a newline or a CR and selective
6921 display is on. */
6922
6923 static enum move_it_result
6924 move_it_in_display_line_to (struct it *it,
6925 EMACS_INT to_charpos, int to_x,
6926 enum move_operation_enum op)
6927 {
6928 enum move_it_result result = MOVE_UNDEFINED;
6929 struct glyph_row *saved_glyph_row;
6930 struct it wrap_it, atpos_it, atx_it;
6931 int may_wrap = 0;
6932 enum it_method prev_method = it->method;
6933 EMACS_INT prev_pos = IT_CHARPOS (*it);
6934
6935 /* Don't produce glyphs in produce_glyphs. */
6936 saved_glyph_row = it->glyph_row;
6937 it->glyph_row = NULL;
6938
6939 /* Use wrap_it to save a copy of IT wherever a word wrap could
6940 occur. Use atpos_it to save a copy of IT at the desired buffer
6941 position, if found, so that we can scan ahead and check if the
6942 word later overshoots the window edge. Use atx_it similarly, for
6943 pixel positions. */
6944 wrap_it.sp = -1;
6945 atpos_it.sp = -1;
6946 atx_it.sp = -1;
6947
6948 #define BUFFER_POS_REACHED_P() \
6949 ((op & MOVE_TO_POS) != 0 \
6950 && BUFFERP (it->object) \
6951 && (IT_CHARPOS (*it) == to_charpos \
6952 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
6953 && (it->method == GET_FROM_BUFFER \
6954 || (it->method == GET_FROM_DISPLAY_VECTOR \
6955 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6956
6957 /* If there's a line-/wrap-prefix, handle it. */
6958 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6959 && it->current_y < it->last_visible_y)
6960 handle_line_prefix (it);
6961
6962 while (1)
6963 {
6964 int x, i, ascent = 0, descent = 0;
6965
6966 /* Utility macro to reset an iterator with x, ascent, and descent. */
6967 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6968 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6969 (IT)->max_descent = descent)
6970
6971 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6972 glyph). */
6973 if ((op & MOVE_TO_POS) != 0
6974 && BUFFERP (it->object)
6975 && it->method == GET_FROM_BUFFER
6976 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
6977 || (it->bidi_p
6978 && (prev_method == GET_FROM_IMAGE
6979 || prev_method == GET_FROM_STRETCH)
6980 /* Passed TO_CHARPOS from left to right. */
6981 && ((prev_pos < to_charpos
6982 && IT_CHARPOS (*it) > to_charpos)
6983 /* Passed TO_CHARPOS from right to left. */
6984 || (prev_pos > to_charpos
6985 && IT_CHARPOS (*it) < to_charpos)))))
6986 {
6987 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6988 {
6989 result = MOVE_POS_MATCH_OR_ZV;
6990 break;
6991 }
6992 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6993 /* If wrap_it is valid, the current position might be in a
6994 word that is wrapped. So, save the iterator in
6995 atpos_it and continue to see if wrapping happens. */
6996 atpos_it = *it;
6997 }
6998
6999 prev_method = it->method;
7000 if (it->method == GET_FROM_BUFFER)
7001 prev_pos = IT_CHARPOS (*it);
7002 /* Stop when ZV reached.
7003 We used to stop here when TO_CHARPOS reached as well, but that is
7004 too soon if this glyph does not fit on this line. So we handle it
7005 explicitly below. */
7006 if (!get_next_display_element (it))
7007 {
7008 result = MOVE_POS_MATCH_OR_ZV;
7009 break;
7010 }
7011
7012 if (it->line_wrap == TRUNCATE)
7013 {
7014 if (BUFFER_POS_REACHED_P ())
7015 {
7016 result = MOVE_POS_MATCH_OR_ZV;
7017 break;
7018 }
7019 }
7020 else
7021 {
7022 if (it->line_wrap == WORD_WRAP)
7023 {
7024 if (IT_DISPLAYING_WHITESPACE (it))
7025 may_wrap = 1;
7026 else if (may_wrap)
7027 {
7028 /* We have reached a glyph that follows one or more
7029 whitespace characters. If the position is
7030 already found, we are done. */
7031 if (atpos_it.sp >= 0)
7032 {
7033 *it = atpos_it;
7034 result = MOVE_POS_MATCH_OR_ZV;
7035 goto done;
7036 }
7037 if (atx_it.sp >= 0)
7038 {
7039 *it = atx_it;
7040 result = MOVE_X_REACHED;
7041 goto done;
7042 }
7043 /* Otherwise, we can wrap here. */
7044 wrap_it = *it;
7045 may_wrap = 0;
7046 }
7047 }
7048 }
7049
7050 /* Remember the line height for the current line, in case
7051 the next element doesn't fit on the line. */
7052 ascent = it->max_ascent;
7053 descent = it->max_descent;
7054
7055 /* The call to produce_glyphs will get the metrics of the
7056 display element IT is loaded with. Record the x-position
7057 before this display element, in case it doesn't fit on the
7058 line. */
7059 x = it->current_x;
7060
7061 PRODUCE_GLYPHS (it);
7062
7063 if (it->area != TEXT_AREA)
7064 {
7065 set_iterator_to_next (it, 1);
7066 continue;
7067 }
7068
7069 /* The number of glyphs we get back in IT->nglyphs will normally
7070 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7071 character on a terminal frame, or (iii) a line end. For the
7072 second case, IT->nglyphs - 1 padding glyphs will be present.
7073 (On X frames, there is only one glyph produced for a
7074 composite character.)
7075
7076 The behavior implemented below means, for continuation lines,
7077 that as many spaces of a TAB as fit on the current line are
7078 displayed there. For terminal frames, as many glyphs of a
7079 multi-glyph character are displayed in the current line, too.
7080 This is what the old redisplay code did, and we keep it that
7081 way. Under X, the whole shape of a complex character must
7082 fit on the line or it will be completely displayed in the
7083 next line.
7084
7085 Note that both for tabs and padding glyphs, all glyphs have
7086 the same width. */
7087 if (it->nglyphs)
7088 {
7089 /* More than one glyph or glyph doesn't fit on line. All
7090 glyphs have the same width. */
7091 int single_glyph_width = it->pixel_width / it->nglyphs;
7092 int new_x;
7093 int x_before_this_char = x;
7094 int hpos_before_this_char = it->hpos;
7095
7096 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7097 {
7098 new_x = x + single_glyph_width;
7099
7100 /* We want to leave anything reaching TO_X to the caller. */
7101 if ((op & MOVE_TO_X) && new_x > to_x)
7102 {
7103 if (BUFFER_POS_REACHED_P ())
7104 {
7105 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7106 goto buffer_pos_reached;
7107 if (atpos_it.sp < 0)
7108 {
7109 atpos_it = *it;
7110 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7111 }
7112 }
7113 else
7114 {
7115 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7116 {
7117 it->current_x = x;
7118 result = MOVE_X_REACHED;
7119 break;
7120 }
7121 if (atx_it.sp < 0)
7122 {
7123 atx_it = *it;
7124 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7125 }
7126 }
7127 }
7128
7129 if (/* Lines are continued. */
7130 it->line_wrap != TRUNCATE
7131 && (/* And glyph doesn't fit on the line. */
7132 new_x > it->last_visible_x
7133 /* Or it fits exactly and we're on a window
7134 system frame. */
7135 || (new_x == it->last_visible_x
7136 && FRAME_WINDOW_P (it->f))))
7137 {
7138 if (/* IT->hpos == 0 means the very first glyph
7139 doesn't fit on the line, e.g. a wide image. */
7140 it->hpos == 0
7141 || (new_x == it->last_visible_x
7142 && FRAME_WINDOW_P (it->f)))
7143 {
7144 ++it->hpos;
7145 it->current_x = new_x;
7146
7147 /* The character's last glyph just barely fits
7148 in this row. */
7149 if (i == it->nglyphs - 1)
7150 {
7151 /* If this is the destination position,
7152 return a position *before* it in this row,
7153 now that we know it fits in this row. */
7154 if (BUFFER_POS_REACHED_P ())
7155 {
7156 if (it->line_wrap != WORD_WRAP
7157 || wrap_it.sp < 0)
7158 {
7159 it->hpos = hpos_before_this_char;
7160 it->current_x = x_before_this_char;
7161 result = MOVE_POS_MATCH_OR_ZV;
7162 break;
7163 }
7164 if (it->line_wrap == WORD_WRAP
7165 && atpos_it.sp < 0)
7166 {
7167 atpos_it = *it;
7168 atpos_it.current_x = x_before_this_char;
7169 atpos_it.hpos = hpos_before_this_char;
7170 }
7171 }
7172
7173 set_iterator_to_next (it, 1);
7174 /* On graphical terminals, newlines may
7175 "overflow" into the fringe if
7176 overflow-newline-into-fringe is non-nil.
7177 On text-only terminals, newlines may
7178 overflow into the last glyph on the
7179 display line.*/
7180 if (!FRAME_WINDOW_P (it->f)
7181 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7182 {
7183 if (!get_next_display_element (it))
7184 {
7185 result = MOVE_POS_MATCH_OR_ZV;
7186 break;
7187 }
7188 if (BUFFER_POS_REACHED_P ())
7189 {
7190 if (ITERATOR_AT_END_OF_LINE_P (it))
7191 result = MOVE_POS_MATCH_OR_ZV;
7192 else
7193 result = MOVE_LINE_CONTINUED;
7194 break;
7195 }
7196 if (ITERATOR_AT_END_OF_LINE_P (it))
7197 {
7198 result = MOVE_NEWLINE_OR_CR;
7199 break;
7200 }
7201 }
7202 }
7203 }
7204 else
7205 IT_RESET_X_ASCENT_DESCENT (it);
7206
7207 if (wrap_it.sp >= 0)
7208 {
7209 *it = wrap_it;
7210 atpos_it.sp = -1;
7211 atx_it.sp = -1;
7212 }
7213
7214 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7215 IT_CHARPOS (*it)));
7216 result = MOVE_LINE_CONTINUED;
7217 break;
7218 }
7219
7220 if (BUFFER_POS_REACHED_P ())
7221 {
7222 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7223 goto buffer_pos_reached;
7224 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7225 {
7226 atpos_it = *it;
7227 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7228 }
7229 }
7230
7231 if (new_x > it->first_visible_x)
7232 {
7233 /* Glyph is visible. Increment number of glyphs that
7234 would be displayed. */
7235 ++it->hpos;
7236 }
7237 }
7238
7239 if (result != MOVE_UNDEFINED)
7240 break;
7241 }
7242 else if (BUFFER_POS_REACHED_P ())
7243 {
7244 buffer_pos_reached:
7245 IT_RESET_X_ASCENT_DESCENT (it);
7246 result = MOVE_POS_MATCH_OR_ZV;
7247 break;
7248 }
7249 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7250 {
7251 /* Stop when TO_X specified and reached. This check is
7252 necessary here because of lines consisting of a line end,
7253 only. The line end will not produce any glyphs and we
7254 would never get MOVE_X_REACHED. */
7255 xassert (it->nglyphs == 0);
7256 result = MOVE_X_REACHED;
7257 break;
7258 }
7259
7260 /* Is this a line end? If yes, we're done. */
7261 if (ITERATOR_AT_END_OF_LINE_P (it))
7262 {
7263 result = MOVE_NEWLINE_OR_CR;
7264 break;
7265 }
7266
7267 if (it->method == GET_FROM_BUFFER)
7268 prev_pos = IT_CHARPOS (*it);
7269 /* The current display element has been consumed. Advance
7270 to the next. */
7271 set_iterator_to_next (it, 1);
7272
7273 /* Stop if lines are truncated and IT's current x-position is
7274 past the right edge of the window now. */
7275 if (it->line_wrap == TRUNCATE
7276 && it->current_x >= it->last_visible_x)
7277 {
7278 if (!FRAME_WINDOW_P (it->f)
7279 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7280 {
7281 if (!get_next_display_element (it)
7282 || BUFFER_POS_REACHED_P ())
7283 {
7284 result = MOVE_POS_MATCH_OR_ZV;
7285 break;
7286 }
7287 if (ITERATOR_AT_END_OF_LINE_P (it))
7288 {
7289 result = MOVE_NEWLINE_OR_CR;
7290 break;
7291 }
7292 }
7293 result = MOVE_LINE_TRUNCATED;
7294 break;
7295 }
7296 #undef IT_RESET_X_ASCENT_DESCENT
7297 }
7298
7299 #undef BUFFER_POS_REACHED_P
7300
7301 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7302 restore the saved iterator. */
7303 if (atpos_it.sp >= 0)
7304 *it = atpos_it;
7305 else if (atx_it.sp >= 0)
7306 *it = atx_it;
7307
7308 done:
7309
7310 /* Restore the iterator settings altered at the beginning of this
7311 function. */
7312 it->glyph_row = saved_glyph_row;
7313 return result;
7314 }
7315
7316 /* For external use. */
7317 void
7318 move_it_in_display_line (struct it *it,
7319 EMACS_INT to_charpos, int to_x,
7320 enum move_operation_enum op)
7321 {
7322 if (it->line_wrap == WORD_WRAP
7323 && (op & MOVE_TO_X))
7324 {
7325 struct it save_it = *it;
7326 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7327 /* When word-wrap is on, TO_X may lie past the end
7328 of a wrapped line. Then it->current is the
7329 character on the next line, so backtrack to the
7330 space before the wrap point. */
7331 if (skip == MOVE_LINE_CONTINUED)
7332 {
7333 int prev_x = max (it->current_x - 1, 0);
7334 *it = save_it;
7335 move_it_in_display_line_to
7336 (it, -1, prev_x, MOVE_TO_X);
7337 }
7338 }
7339 else
7340 move_it_in_display_line_to (it, to_charpos, to_x, op);
7341 }
7342
7343
7344 /* Move IT forward until it satisfies one or more of the criteria in
7345 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7346
7347 OP is a bit-mask that specifies where to stop, and in particular,
7348 which of those four position arguments makes a difference. See the
7349 description of enum move_operation_enum.
7350
7351 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7352 screen line, this function will set IT to the next position >
7353 TO_CHARPOS. */
7354
7355 void
7356 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7357 struct it *it;
7358 int to_charpos, to_x, to_y, to_vpos;
7359 int op;
7360 {
7361 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7362 int line_height, line_start_x = 0, reached = 0;
7363
7364 for (;;)
7365 {
7366 if (op & MOVE_TO_VPOS)
7367 {
7368 /* If no TO_CHARPOS and no TO_X specified, stop at the
7369 start of the line TO_VPOS. */
7370 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7371 {
7372 if (it->vpos == to_vpos)
7373 {
7374 reached = 1;
7375 break;
7376 }
7377 else
7378 skip = move_it_in_display_line_to (it, -1, -1, 0);
7379 }
7380 else
7381 {
7382 /* TO_VPOS >= 0 means stop at TO_X in the line at
7383 TO_VPOS, or at TO_POS, whichever comes first. */
7384 if (it->vpos == to_vpos)
7385 {
7386 reached = 2;
7387 break;
7388 }
7389
7390 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7391
7392 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7393 {
7394 reached = 3;
7395 break;
7396 }
7397 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7398 {
7399 /* We have reached TO_X but not in the line we want. */
7400 skip = move_it_in_display_line_to (it, to_charpos,
7401 -1, MOVE_TO_POS);
7402 if (skip == MOVE_POS_MATCH_OR_ZV)
7403 {
7404 reached = 4;
7405 break;
7406 }
7407 }
7408 }
7409 }
7410 else if (op & MOVE_TO_Y)
7411 {
7412 struct it it_backup;
7413
7414 if (it->line_wrap == WORD_WRAP)
7415 it_backup = *it;
7416
7417 /* TO_Y specified means stop at TO_X in the line containing
7418 TO_Y---or at TO_CHARPOS if this is reached first. The
7419 problem is that we can't really tell whether the line
7420 contains TO_Y before we have completely scanned it, and
7421 this may skip past TO_X. What we do is to first scan to
7422 TO_X.
7423
7424 If TO_X is not specified, use a TO_X of zero. The reason
7425 is to make the outcome of this function more predictable.
7426 If we didn't use TO_X == 0, we would stop at the end of
7427 the line which is probably not what a caller would expect
7428 to happen. */
7429 skip = move_it_in_display_line_to
7430 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7431 (MOVE_TO_X | (op & MOVE_TO_POS)));
7432
7433 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7434 if (skip == MOVE_POS_MATCH_OR_ZV)
7435 reached = 5;
7436 else if (skip == MOVE_X_REACHED)
7437 {
7438 /* If TO_X was reached, we want to know whether TO_Y is
7439 in the line. We know this is the case if the already
7440 scanned glyphs make the line tall enough. Otherwise,
7441 we must check by scanning the rest of the line. */
7442 line_height = it->max_ascent + it->max_descent;
7443 if (to_y >= it->current_y
7444 && to_y < it->current_y + line_height)
7445 {
7446 reached = 6;
7447 break;
7448 }
7449 it_backup = *it;
7450 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7451 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7452 op & MOVE_TO_POS);
7453 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7454 line_height = it->max_ascent + it->max_descent;
7455 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7456
7457 if (to_y >= it->current_y
7458 && to_y < it->current_y + line_height)
7459 {
7460 /* If TO_Y is in this line and TO_X was reached
7461 above, we scanned too far. We have to restore
7462 IT's settings to the ones before skipping. */
7463 *it = it_backup;
7464 reached = 6;
7465 }
7466 else
7467 {
7468 skip = skip2;
7469 if (skip == MOVE_POS_MATCH_OR_ZV)
7470 reached = 7;
7471 }
7472 }
7473 else
7474 {
7475 /* Check whether TO_Y is in this line. */
7476 line_height = it->max_ascent + it->max_descent;
7477 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7478
7479 if (to_y >= it->current_y
7480 && to_y < it->current_y + line_height)
7481 {
7482 /* When word-wrap is on, TO_X may lie past the end
7483 of a wrapped line. Then it->current is the
7484 character on the next line, so backtrack to the
7485 space before the wrap point. */
7486 if (skip == MOVE_LINE_CONTINUED
7487 && it->line_wrap == WORD_WRAP)
7488 {
7489 int prev_x = max (it->current_x - 1, 0);
7490 *it = it_backup;
7491 skip = move_it_in_display_line_to
7492 (it, -1, prev_x, MOVE_TO_X);
7493 }
7494 reached = 6;
7495 }
7496 }
7497
7498 if (reached)
7499 break;
7500 }
7501 else if (BUFFERP (it->object)
7502 && (it->method == GET_FROM_BUFFER
7503 || it->method == GET_FROM_STRETCH)
7504 && IT_CHARPOS (*it) >= to_charpos)
7505 skip = MOVE_POS_MATCH_OR_ZV;
7506 else
7507 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7508
7509 switch (skip)
7510 {
7511 case MOVE_POS_MATCH_OR_ZV:
7512 reached = 8;
7513 goto out;
7514
7515 case MOVE_NEWLINE_OR_CR:
7516 set_iterator_to_next (it, 1);
7517 it->continuation_lines_width = 0;
7518 break;
7519
7520 case MOVE_LINE_TRUNCATED:
7521 it->continuation_lines_width = 0;
7522 reseat_at_next_visible_line_start (it, 0);
7523 if ((op & MOVE_TO_POS) != 0
7524 && IT_CHARPOS (*it) > to_charpos)
7525 {
7526 reached = 9;
7527 goto out;
7528 }
7529 break;
7530
7531 case MOVE_LINE_CONTINUED:
7532 /* For continued lines ending in a tab, some of the glyphs
7533 associated with the tab are displayed on the current
7534 line. Since it->current_x does not include these glyphs,
7535 we use it->last_visible_x instead. */
7536 if (it->c == '\t')
7537 {
7538 it->continuation_lines_width += it->last_visible_x;
7539 /* When moving by vpos, ensure that the iterator really
7540 advances to the next line (bug#847, bug#969). Fixme:
7541 do we need to do this in other circumstances? */
7542 if (it->current_x != it->last_visible_x
7543 && (op & MOVE_TO_VPOS)
7544 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7545 {
7546 line_start_x = it->current_x + it->pixel_width
7547 - it->last_visible_x;
7548 set_iterator_to_next (it, 0);
7549 }
7550 }
7551 else
7552 it->continuation_lines_width += it->current_x;
7553 break;
7554
7555 default:
7556 abort ();
7557 }
7558
7559 /* Reset/increment for the next run. */
7560 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7561 it->current_x = line_start_x;
7562 line_start_x = 0;
7563 it->hpos = 0;
7564 it->current_y += it->max_ascent + it->max_descent;
7565 ++it->vpos;
7566 last_height = it->max_ascent + it->max_descent;
7567 last_max_ascent = it->max_ascent;
7568 it->max_ascent = it->max_descent = 0;
7569 }
7570
7571 out:
7572
7573 /* On text terminals, we may stop at the end of a line in the middle
7574 of a multi-character glyph. If the glyph itself is continued,
7575 i.e. it is actually displayed on the next line, don't treat this
7576 stopping point as valid; move to the next line instead (unless
7577 that brings us offscreen). */
7578 if (!FRAME_WINDOW_P (it->f)
7579 && op & MOVE_TO_POS
7580 && IT_CHARPOS (*it) == to_charpos
7581 && it->what == IT_CHARACTER
7582 && it->nglyphs > 1
7583 && it->line_wrap == WINDOW_WRAP
7584 && it->current_x == it->last_visible_x - 1
7585 && it->c != '\n'
7586 && it->c != '\t'
7587 && it->vpos < XFASTINT (it->w->window_end_vpos))
7588 {
7589 it->continuation_lines_width += it->current_x;
7590 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7591 it->current_y += it->max_ascent + it->max_descent;
7592 ++it->vpos;
7593 last_height = it->max_ascent + it->max_descent;
7594 last_max_ascent = it->max_ascent;
7595 }
7596
7597 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7598 }
7599
7600
7601 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7602
7603 If DY > 0, move IT backward at least that many pixels. DY = 0
7604 means move IT backward to the preceding line start or BEGV. This
7605 function may move over more than DY pixels if IT->current_y - DY
7606 ends up in the middle of a line; in this case IT->current_y will be
7607 set to the top of the line moved to. */
7608
7609 void
7610 move_it_vertically_backward (it, dy)
7611 struct it *it;
7612 int dy;
7613 {
7614 int nlines, h;
7615 struct it it2, it3;
7616 int start_pos;
7617
7618 move_further_back:
7619 xassert (dy >= 0);
7620
7621 start_pos = IT_CHARPOS (*it);
7622
7623 /* Estimate how many newlines we must move back. */
7624 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7625
7626 /* Set the iterator's position that many lines back. */
7627 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7628 back_to_previous_visible_line_start (it);
7629
7630 /* Reseat the iterator here. When moving backward, we don't want
7631 reseat to skip forward over invisible text, set up the iterator
7632 to deliver from overlay strings at the new position etc. So,
7633 use reseat_1 here. */
7634 reseat_1 (it, it->current.pos, 1);
7635
7636 /* We are now surely at a line start. */
7637 it->current_x = it->hpos = 0;
7638 it->continuation_lines_width = 0;
7639
7640 /* Move forward and see what y-distance we moved. First move to the
7641 start of the next line so that we get its height. We need this
7642 height to be able to tell whether we reached the specified
7643 y-distance. */
7644 it2 = *it;
7645 it2.max_ascent = it2.max_descent = 0;
7646 do
7647 {
7648 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7649 MOVE_TO_POS | MOVE_TO_VPOS);
7650 }
7651 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7652 xassert (IT_CHARPOS (*it) >= BEGV);
7653 it3 = it2;
7654
7655 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7656 xassert (IT_CHARPOS (*it) >= BEGV);
7657 /* H is the actual vertical distance from the position in *IT
7658 and the starting position. */
7659 h = it2.current_y - it->current_y;
7660 /* NLINES is the distance in number of lines. */
7661 nlines = it2.vpos - it->vpos;
7662
7663 /* Correct IT's y and vpos position
7664 so that they are relative to the starting point. */
7665 it->vpos -= nlines;
7666 it->current_y -= h;
7667
7668 if (dy == 0)
7669 {
7670 /* DY == 0 means move to the start of the screen line. The
7671 value of nlines is > 0 if continuation lines were involved. */
7672 if (nlines > 0)
7673 move_it_by_lines (it, nlines, 1);
7674 }
7675 else
7676 {
7677 /* The y-position we try to reach, relative to *IT.
7678 Note that H has been subtracted in front of the if-statement. */
7679 int target_y = it->current_y + h - dy;
7680 int y0 = it3.current_y;
7681 int y1 = line_bottom_y (&it3);
7682 int line_height = y1 - y0;
7683
7684 /* If we did not reach target_y, try to move further backward if
7685 we can. If we moved too far backward, try to move forward. */
7686 if (target_y < it->current_y
7687 /* This is heuristic. In a window that's 3 lines high, with
7688 a line height of 13 pixels each, recentering with point
7689 on the bottom line will try to move -39/2 = 19 pixels
7690 backward. Try to avoid moving into the first line. */
7691 && (it->current_y - target_y
7692 > min (window_box_height (it->w), line_height * 2 / 3))
7693 && IT_CHARPOS (*it) > BEGV)
7694 {
7695 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7696 target_y - it->current_y));
7697 dy = it->current_y - target_y;
7698 goto move_further_back;
7699 }
7700 else if (target_y >= it->current_y + line_height
7701 && IT_CHARPOS (*it) < ZV)
7702 {
7703 /* Should move forward by at least one line, maybe more.
7704
7705 Note: Calling move_it_by_lines can be expensive on
7706 terminal frames, where compute_motion is used (via
7707 vmotion) to do the job, when there are very long lines
7708 and truncate-lines is nil. That's the reason for
7709 treating terminal frames specially here. */
7710
7711 if (!FRAME_WINDOW_P (it->f))
7712 move_it_vertically (it, target_y - (it->current_y + line_height));
7713 else
7714 {
7715 do
7716 {
7717 move_it_by_lines (it, 1, 1);
7718 }
7719 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7720 }
7721 }
7722 }
7723 }
7724
7725
7726 /* Move IT by a specified amount of pixel lines DY. DY negative means
7727 move backwards. DY = 0 means move to start of screen line. At the
7728 end, IT will be on the start of a screen line. */
7729
7730 void
7731 move_it_vertically (it, dy)
7732 struct it *it;
7733 int dy;
7734 {
7735 if (dy <= 0)
7736 move_it_vertically_backward (it, -dy);
7737 else
7738 {
7739 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7740 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7741 MOVE_TO_POS | MOVE_TO_Y);
7742 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7743
7744 /* If buffer ends in ZV without a newline, move to the start of
7745 the line to satisfy the post-condition. */
7746 if (IT_CHARPOS (*it) == ZV
7747 && ZV > BEGV
7748 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7749 move_it_by_lines (it, 0, 0);
7750 }
7751 }
7752
7753
7754 /* Move iterator IT past the end of the text line it is in. */
7755
7756 void
7757 move_it_past_eol (it)
7758 struct it *it;
7759 {
7760 enum move_it_result rc;
7761
7762 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7763 if (rc == MOVE_NEWLINE_OR_CR)
7764 set_iterator_to_next (it, 0);
7765 }
7766
7767
7768 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7769 negative means move up. DVPOS == 0 means move to the start of the
7770 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7771 NEED_Y_P is zero, IT->current_y will be left unchanged.
7772
7773 Further optimization ideas: If we would know that IT->f doesn't use
7774 a face with proportional font, we could be faster for
7775 truncate-lines nil. */
7776
7777 void
7778 move_it_by_lines (it, dvpos, need_y_p)
7779 struct it *it;
7780 int dvpos, need_y_p;
7781 {
7782 struct position pos;
7783
7784 /* The commented-out optimization uses vmotion on terminals. This
7785 gives bad results, because elements like it->what, on which
7786 callers such as pos_visible_p rely, aren't updated. */
7787 /* if (!FRAME_WINDOW_P (it->f))
7788 {
7789 struct text_pos textpos;
7790
7791 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7792 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7793 reseat (it, textpos, 1);
7794 it->vpos += pos.vpos;
7795 it->current_y += pos.vpos;
7796 }
7797 else */
7798
7799 if (dvpos == 0)
7800 {
7801 /* DVPOS == 0 means move to the start of the screen line. */
7802 move_it_vertically_backward (it, 0);
7803 xassert (it->current_x == 0 && it->hpos == 0);
7804 /* Let next call to line_bottom_y calculate real line height */
7805 last_height = 0;
7806 }
7807 else if (dvpos > 0)
7808 {
7809 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7810 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7811 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7812 }
7813 else
7814 {
7815 struct it it2;
7816 int start_charpos, i;
7817
7818 /* Start at the beginning of the screen line containing IT's
7819 position. This may actually move vertically backwards,
7820 in case of overlays, so adjust dvpos accordingly. */
7821 dvpos += it->vpos;
7822 move_it_vertically_backward (it, 0);
7823 dvpos -= it->vpos;
7824
7825 /* Go back -DVPOS visible lines and reseat the iterator there. */
7826 start_charpos = IT_CHARPOS (*it);
7827 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7828 back_to_previous_visible_line_start (it);
7829 reseat (it, it->current.pos, 1);
7830
7831 /* Move further back if we end up in a string or an image. */
7832 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7833 {
7834 /* First try to move to start of display line. */
7835 dvpos += it->vpos;
7836 move_it_vertically_backward (it, 0);
7837 dvpos -= it->vpos;
7838 if (IT_POS_VALID_AFTER_MOVE_P (it))
7839 break;
7840 /* If start of line is still in string or image,
7841 move further back. */
7842 back_to_previous_visible_line_start (it);
7843 reseat (it, it->current.pos, 1);
7844 dvpos--;
7845 }
7846
7847 it->current_x = it->hpos = 0;
7848
7849 /* Above call may have moved too far if continuation lines
7850 are involved. Scan forward and see if it did. */
7851 it2 = *it;
7852 it2.vpos = it2.current_y = 0;
7853 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7854 it->vpos -= it2.vpos;
7855 it->current_y -= it2.current_y;
7856 it->current_x = it->hpos = 0;
7857
7858 /* If we moved too far back, move IT some lines forward. */
7859 if (it2.vpos > -dvpos)
7860 {
7861 int delta = it2.vpos + dvpos;
7862 it2 = *it;
7863 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7864 /* Move back again if we got too far ahead. */
7865 if (IT_CHARPOS (*it) >= start_charpos)
7866 *it = it2;
7867 }
7868 }
7869 }
7870
7871 /* Return 1 if IT points into the middle of a display vector. */
7872
7873 int
7874 in_display_vector_p (it)
7875 struct it *it;
7876 {
7877 return (it->method == GET_FROM_DISPLAY_VECTOR
7878 && it->current.dpvec_index > 0
7879 && it->dpvec + it->current.dpvec_index != it->dpend);
7880 }
7881
7882 \f
7883 /***********************************************************************
7884 Messages
7885 ***********************************************************************/
7886
7887
7888 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7889 to *Messages*. */
7890
7891 void
7892 add_to_log (format, arg1, arg2)
7893 char *format;
7894 Lisp_Object arg1, arg2;
7895 {
7896 Lisp_Object args[3];
7897 Lisp_Object msg, fmt;
7898 char *buffer;
7899 int len;
7900 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7901 USE_SAFE_ALLOCA;
7902
7903 /* Do nothing if called asynchronously. Inserting text into
7904 a buffer may call after-change-functions and alike and
7905 that would means running Lisp asynchronously. */
7906 if (handling_signal)
7907 return;
7908
7909 fmt = msg = Qnil;
7910 GCPRO4 (fmt, msg, arg1, arg2);
7911
7912 args[0] = fmt = build_string (format);
7913 args[1] = arg1;
7914 args[2] = arg2;
7915 msg = Fformat (3, args);
7916
7917 len = SBYTES (msg) + 1;
7918 SAFE_ALLOCA (buffer, char *, len);
7919 bcopy (SDATA (msg), buffer, len);
7920
7921 message_dolog (buffer, len - 1, 1, 0);
7922 SAFE_FREE ();
7923
7924 UNGCPRO;
7925 }
7926
7927
7928 /* Output a newline in the *Messages* buffer if "needs" one. */
7929
7930 void
7931 message_log_maybe_newline ()
7932 {
7933 if (message_log_need_newline)
7934 message_dolog ("", 0, 1, 0);
7935 }
7936
7937
7938 /* Add a string M of length NBYTES to the message log, optionally
7939 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7940 nonzero, means interpret the contents of M as multibyte. This
7941 function calls low-level routines in order to bypass text property
7942 hooks, etc. which might not be safe to run.
7943
7944 This may GC (insert may run before/after change hooks),
7945 so the buffer M must NOT point to a Lisp string. */
7946
7947 void
7948 message_dolog (m, nbytes, nlflag, multibyte)
7949 const char *m;
7950 int nbytes, nlflag, multibyte;
7951 {
7952 if (!NILP (Vmemory_full))
7953 return;
7954
7955 if (!NILP (Vmessage_log_max))
7956 {
7957 struct buffer *oldbuf;
7958 Lisp_Object oldpoint, oldbegv, oldzv;
7959 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7960 int point_at_end = 0;
7961 int zv_at_end = 0;
7962 Lisp_Object old_deactivate_mark, tem;
7963 struct gcpro gcpro1;
7964
7965 old_deactivate_mark = Vdeactivate_mark;
7966 oldbuf = current_buffer;
7967 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7968 current_buffer->undo_list = Qt;
7969
7970 oldpoint = message_dolog_marker1;
7971 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7972 oldbegv = message_dolog_marker2;
7973 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7974 oldzv = message_dolog_marker3;
7975 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7976 GCPRO1 (old_deactivate_mark);
7977
7978 if (PT == Z)
7979 point_at_end = 1;
7980 if (ZV == Z)
7981 zv_at_end = 1;
7982
7983 BEGV = BEG;
7984 BEGV_BYTE = BEG_BYTE;
7985 ZV = Z;
7986 ZV_BYTE = Z_BYTE;
7987 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7988
7989 /* Insert the string--maybe converting multibyte to single byte
7990 or vice versa, so that all the text fits the buffer. */
7991 if (multibyte
7992 && NILP (current_buffer->enable_multibyte_characters))
7993 {
7994 int i, c, char_bytes;
7995 unsigned char work[1];
7996
7997 /* Convert a multibyte string to single-byte
7998 for the *Message* buffer. */
7999 for (i = 0; i < nbytes; i += char_bytes)
8000 {
8001 c = string_char_and_length (m + i, &char_bytes);
8002 work[0] = (ASCII_CHAR_P (c)
8003 ? c
8004 : multibyte_char_to_unibyte (c, Qnil));
8005 insert_1_both (work, 1, 1, 1, 0, 0);
8006 }
8007 }
8008 else if (! multibyte
8009 && ! NILP (current_buffer->enable_multibyte_characters))
8010 {
8011 int i, c, char_bytes;
8012 unsigned char *msg = (unsigned char *) m;
8013 unsigned char str[MAX_MULTIBYTE_LENGTH];
8014 /* Convert a single-byte string to multibyte
8015 for the *Message* buffer. */
8016 for (i = 0; i < nbytes; i++)
8017 {
8018 c = msg[i];
8019 MAKE_CHAR_MULTIBYTE (c);
8020 char_bytes = CHAR_STRING (c, str);
8021 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8022 }
8023 }
8024 else if (nbytes)
8025 insert_1 (m, nbytes, 1, 0, 0);
8026
8027 if (nlflag)
8028 {
8029 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
8030 insert_1 ("\n", 1, 1, 0, 0);
8031
8032 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8033 this_bol = PT;
8034 this_bol_byte = PT_BYTE;
8035
8036 /* See if this line duplicates the previous one.
8037 If so, combine duplicates. */
8038 if (this_bol > BEG)
8039 {
8040 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8041 prev_bol = PT;
8042 prev_bol_byte = PT_BYTE;
8043
8044 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8045 this_bol, this_bol_byte);
8046 if (dup)
8047 {
8048 del_range_both (prev_bol, prev_bol_byte,
8049 this_bol, this_bol_byte, 0);
8050 if (dup > 1)
8051 {
8052 char dupstr[40];
8053 int duplen;
8054
8055 /* If you change this format, don't forget to also
8056 change message_log_check_duplicate. */
8057 sprintf (dupstr, " [%d times]", dup);
8058 duplen = strlen (dupstr);
8059 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8060 insert_1 (dupstr, duplen, 1, 0, 1);
8061 }
8062 }
8063 }
8064
8065 /* If we have more than the desired maximum number of lines
8066 in the *Messages* buffer now, delete the oldest ones.
8067 This is safe because we don't have undo in this buffer. */
8068
8069 if (NATNUMP (Vmessage_log_max))
8070 {
8071 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8072 -XFASTINT (Vmessage_log_max) - 1, 0);
8073 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8074 }
8075 }
8076 BEGV = XMARKER (oldbegv)->charpos;
8077 BEGV_BYTE = marker_byte_position (oldbegv);
8078
8079 if (zv_at_end)
8080 {
8081 ZV = Z;
8082 ZV_BYTE = Z_BYTE;
8083 }
8084 else
8085 {
8086 ZV = XMARKER (oldzv)->charpos;
8087 ZV_BYTE = marker_byte_position (oldzv);
8088 }
8089
8090 if (point_at_end)
8091 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8092 else
8093 /* We can't do Fgoto_char (oldpoint) because it will run some
8094 Lisp code. */
8095 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8096 XMARKER (oldpoint)->bytepos);
8097
8098 UNGCPRO;
8099 unchain_marker (XMARKER (oldpoint));
8100 unchain_marker (XMARKER (oldbegv));
8101 unchain_marker (XMARKER (oldzv));
8102
8103 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8104 set_buffer_internal (oldbuf);
8105 if (NILP (tem))
8106 windows_or_buffers_changed = old_windows_or_buffers_changed;
8107 message_log_need_newline = !nlflag;
8108 Vdeactivate_mark = old_deactivate_mark;
8109 }
8110 }
8111
8112
8113 /* We are at the end of the buffer after just having inserted a newline.
8114 (Note: We depend on the fact we won't be crossing the gap.)
8115 Check to see if the most recent message looks a lot like the previous one.
8116 Return 0 if different, 1 if the new one should just replace it, or a
8117 value N > 1 if we should also append " [N times]". */
8118
8119 static int
8120 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
8121 int prev_bol, this_bol;
8122 int prev_bol_byte, this_bol_byte;
8123 {
8124 int i;
8125 int len = Z_BYTE - 1 - this_bol_byte;
8126 int seen_dots = 0;
8127 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8128 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8129
8130 for (i = 0; i < len; i++)
8131 {
8132 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8133 seen_dots = 1;
8134 if (p1[i] != p2[i])
8135 return seen_dots;
8136 }
8137 p1 += len;
8138 if (*p1 == '\n')
8139 return 2;
8140 if (*p1++ == ' ' && *p1++ == '[')
8141 {
8142 int n = 0;
8143 while (*p1 >= '0' && *p1 <= '9')
8144 n = n * 10 + *p1++ - '0';
8145 if (strncmp (p1, " times]\n", 8) == 0)
8146 return n+1;
8147 }
8148 return 0;
8149 }
8150 \f
8151
8152 /* Display an echo area message M with a specified length of NBYTES
8153 bytes. The string may include null characters. If M is 0, clear
8154 out any existing message, and let the mini-buffer text show
8155 through.
8156
8157 This may GC, so the buffer M must NOT point to a Lisp string. */
8158
8159 void
8160 message2 (m, nbytes, multibyte)
8161 const char *m;
8162 int nbytes;
8163 int multibyte;
8164 {
8165 /* First flush out any partial line written with print. */
8166 message_log_maybe_newline ();
8167 if (m)
8168 message_dolog (m, nbytes, 1, multibyte);
8169 message2_nolog (m, nbytes, multibyte);
8170 }
8171
8172
8173 /* The non-logging counterpart of message2. */
8174
8175 void
8176 message2_nolog (m, nbytes, multibyte)
8177 const char *m;
8178 int nbytes, multibyte;
8179 {
8180 struct frame *sf = SELECTED_FRAME ();
8181 message_enable_multibyte = multibyte;
8182
8183 if (FRAME_INITIAL_P (sf))
8184 {
8185 if (noninteractive_need_newline)
8186 putc ('\n', stderr);
8187 noninteractive_need_newline = 0;
8188 if (m)
8189 fwrite (m, nbytes, 1, stderr);
8190 if (cursor_in_echo_area == 0)
8191 fprintf (stderr, "\n");
8192 fflush (stderr);
8193 }
8194 /* A null message buffer means that the frame hasn't really been
8195 initialized yet. Error messages get reported properly by
8196 cmd_error, so this must be just an informative message; toss it. */
8197 else if (INTERACTIVE
8198 && sf->glyphs_initialized_p
8199 && FRAME_MESSAGE_BUF (sf))
8200 {
8201 Lisp_Object mini_window;
8202 struct frame *f;
8203
8204 /* Get the frame containing the mini-buffer
8205 that the selected frame is using. */
8206 mini_window = FRAME_MINIBUF_WINDOW (sf);
8207 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8208
8209 FRAME_SAMPLE_VISIBILITY (f);
8210 if (FRAME_VISIBLE_P (sf)
8211 && ! FRAME_VISIBLE_P (f))
8212 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8213
8214 if (m)
8215 {
8216 set_message (m, Qnil, nbytes, multibyte);
8217 if (minibuffer_auto_raise)
8218 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8219 }
8220 else
8221 clear_message (1, 1);
8222
8223 do_pending_window_change (0);
8224 echo_area_display (1);
8225 do_pending_window_change (0);
8226 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8227 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8228 }
8229 }
8230
8231
8232 /* Display an echo area message M with a specified length of NBYTES
8233 bytes. The string may include null characters. If M is not a
8234 string, clear out any existing message, and let the mini-buffer
8235 text show through.
8236
8237 This function cancels echoing. */
8238
8239 void
8240 message3 (m, nbytes, multibyte)
8241 Lisp_Object m;
8242 int nbytes;
8243 int multibyte;
8244 {
8245 struct gcpro gcpro1;
8246
8247 GCPRO1 (m);
8248 clear_message (1,1);
8249 cancel_echoing ();
8250
8251 /* First flush out any partial line written with print. */
8252 message_log_maybe_newline ();
8253 if (STRINGP (m))
8254 {
8255 char *buffer;
8256 USE_SAFE_ALLOCA;
8257
8258 SAFE_ALLOCA (buffer, char *, nbytes);
8259 bcopy (SDATA (m), buffer, nbytes);
8260 message_dolog (buffer, nbytes, 1, multibyte);
8261 SAFE_FREE ();
8262 }
8263 message3_nolog (m, nbytes, multibyte);
8264
8265 UNGCPRO;
8266 }
8267
8268
8269 /* The non-logging version of message3.
8270 This does not cancel echoing, because it is used for echoing.
8271 Perhaps we need to make a separate function for echoing
8272 and make this cancel echoing. */
8273
8274 void
8275 message3_nolog (m, nbytes, multibyte)
8276 Lisp_Object m;
8277 int nbytes, multibyte;
8278 {
8279 struct frame *sf = SELECTED_FRAME ();
8280 message_enable_multibyte = multibyte;
8281
8282 if (FRAME_INITIAL_P (sf))
8283 {
8284 if (noninteractive_need_newline)
8285 putc ('\n', stderr);
8286 noninteractive_need_newline = 0;
8287 if (STRINGP (m))
8288 fwrite (SDATA (m), nbytes, 1, stderr);
8289 if (cursor_in_echo_area == 0)
8290 fprintf (stderr, "\n");
8291 fflush (stderr);
8292 }
8293 /* A null message buffer means that the frame hasn't really been
8294 initialized yet. Error messages get reported properly by
8295 cmd_error, so this must be just an informative message; toss it. */
8296 else if (INTERACTIVE
8297 && sf->glyphs_initialized_p
8298 && FRAME_MESSAGE_BUF (sf))
8299 {
8300 Lisp_Object mini_window;
8301 Lisp_Object frame;
8302 struct frame *f;
8303
8304 /* Get the frame containing the mini-buffer
8305 that the selected frame is using. */
8306 mini_window = FRAME_MINIBUF_WINDOW (sf);
8307 frame = XWINDOW (mini_window)->frame;
8308 f = XFRAME (frame);
8309
8310 FRAME_SAMPLE_VISIBILITY (f);
8311 if (FRAME_VISIBLE_P (sf)
8312 && !FRAME_VISIBLE_P (f))
8313 Fmake_frame_visible (frame);
8314
8315 if (STRINGP (m) && SCHARS (m) > 0)
8316 {
8317 set_message (NULL, m, nbytes, multibyte);
8318 if (minibuffer_auto_raise)
8319 Fraise_frame (frame);
8320 /* Assume we are not echoing.
8321 (If we are, echo_now will override this.) */
8322 echo_message_buffer = Qnil;
8323 }
8324 else
8325 clear_message (1, 1);
8326
8327 do_pending_window_change (0);
8328 echo_area_display (1);
8329 do_pending_window_change (0);
8330 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8331 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8332 }
8333 }
8334
8335
8336 /* Display a null-terminated echo area message M. If M is 0, clear
8337 out any existing message, and let the mini-buffer text show through.
8338
8339 The buffer M must continue to exist until after the echo area gets
8340 cleared or some other message gets displayed there. Do not pass
8341 text that is stored in a Lisp string. Do not pass text in a buffer
8342 that was alloca'd. */
8343
8344 void
8345 message1 (m)
8346 char *m;
8347 {
8348 message2 (m, (m ? strlen (m) : 0), 0);
8349 }
8350
8351
8352 /* The non-logging counterpart of message1. */
8353
8354 void
8355 message1_nolog (m)
8356 char *m;
8357 {
8358 message2_nolog (m, (m ? strlen (m) : 0), 0);
8359 }
8360
8361 /* Display a message M which contains a single %s
8362 which gets replaced with STRING. */
8363
8364 void
8365 message_with_string (m, string, log)
8366 char *m;
8367 Lisp_Object string;
8368 int log;
8369 {
8370 CHECK_STRING (string);
8371
8372 if (noninteractive)
8373 {
8374 if (m)
8375 {
8376 if (noninteractive_need_newline)
8377 putc ('\n', stderr);
8378 noninteractive_need_newline = 0;
8379 fprintf (stderr, m, SDATA (string));
8380 if (!cursor_in_echo_area)
8381 fprintf (stderr, "\n");
8382 fflush (stderr);
8383 }
8384 }
8385 else if (INTERACTIVE)
8386 {
8387 /* The frame whose minibuffer we're going to display the message on.
8388 It may be larger than the selected frame, so we need
8389 to use its buffer, not the selected frame's buffer. */
8390 Lisp_Object mini_window;
8391 struct frame *f, *sf = SELECTED_FRAME ();
8392
8393 /* Get the frame containing the minibuffer
8394 that the selected frame is using. */
8395 mini_window = FRAME_MINIBUF_WINDOW (sf);
8396 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8397
8398 /* A null message buffer means that the frame hasn't really been
8399 initialized yet. Error messages get reported properly by
8400 cmd_error, so this must be just an informative message; toss it. */
8401 if (FRAME_MESSAGE_BUF (f))
8402 {
8403 Lisp_Object args[2], message;
8404 struct gcpro gcpro1, gcpro2;
8405
8406 args[0] = build_string (m);
8407 args[1] = message = string;
8408 GCPRO2 (args[0], message);
8409 gcpro1.nvars = 2;
8410
8411 message = Fformat (2, args);
8412
8413 if (log)
8414 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8415 else
8416 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8417
8418 UNGCPRO;
8419
8420 /* Print should start at the beginning of the message
8421 buffer next time. */
8422 message_buf_print = 0;
8423 }
8424 }
8425 }
8426
8427
8428 /* Dump an informative message to the minibuf. If M is 0, clear out
8429 any existing message, and let the mini-buffer text show through. */
8430
8431 /* VARARGS 1 */
8432 void
8433 message (m, a1, a2, a3)
8434 char *m;
8435 EMACS_INT a1, a2, a3;
8436 {
8437 if (noninteractive)
8438 {
8439 if (m)
8440 {
8441 if (noninteractive_need_newline)
8442 putc ('\n', stderr);
8443 noninteractive_need_newline = 0;
8444 fprintf (stderr, m, a1, a2, a3);
8445 if (cursor_in_echo_area == 0)
8446 fprintf (stderr, "\n");
8447 fflush (stderr);
8448 }
8449 }
8450 else if (INTERACTIVE)
8451 {
8452 /* The frame whose mini-buffer we're going to display the message
8453 on. It may be larger than the selected frame, so we need to
8454 use its buffer, not the selected frame's buffer. */
8455 Lisp_Object mini_window;
8456 struct frame *f, *sf = SELECTED_FRAME ();
8457
8458 /* Get the frame containing the mini-buffer
8459 that the selected frame is using. */
8460 mini_window = FRAME_MINIBUF_WINDOW (sf);
8461 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8462
8463 /* A null message buffer means that the frame hasn't really been
8464 initialized yet. Error messages get reported properly by
8465 cmd_error, so this must be just an informative message; toss
8466 it. */
8467 if (FRAME_MESSAGE_BUF (f))
8468 {
8469 if (m)
8470 {
8471 int len;
8472 #ifdef NO_ARG_ARRAY
8473 char *a[3];
8474 a[0] = (char *) a1;
8475 a[1] = (char *) a2;
8476 a[2] = (char *) a3;
8477
8478 len = doprnt (FRAME_MESSAGE_BUF (f),
8479 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8480 #else
8481 len = doprnt (FRAME_MESSAGE_BUF (f),
8482 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8483 (char **) &a1);
8484 #endif /* NO_ARG_ARRAY */
8485
8486 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8487 }
8488 else
8489 message1 (0);
8490
8491 /* Print should start at the beginning of the message
8492 buffer next time. */
8493 message_buf_print = 0;
8494 }
8495 }
8496 }
8497
8498
8499 /* The non-logging version of message. */
8500
8501 void
8502 message_nolog (m, a1, a2, a3)
8503 char *m;
8504 EMACS_INT a1, a2, a3;
8505 {
8506 Lisp_Object old_log_max;
8507 old_log_max = Vmessage_log_max;
8508 Vmessage_log_max = Qnil;
8509 message (m, a1, a2, a3);
8510 Vmessage_log_max = old_log_max;
8511 }
8512
8513
8514 /* Display the current message in the current mini-buffer. This is
8515 only called from error handlers in process.c, and is not time
8516 critical. */
8517
8518 void
8519 update_echo_area ()
8520 {
8521 if (!NILP (echo_area_buffer[0]))
8522 {
8523 Lisp_Object string;
8524 string = Fcurrent_message ();
8525 message3 (string, SBYTES (string),
8526 !NILP (current_buffer->enable_multibyte_characters));
8527 }
8528 }
8529
8530
8531 /* Make sure echo area buffers in `echo_buffers' are live.
8532 If they aren't, make new ones. */
8533
8534 static void
8535 ensure_echo_area_buffers ()
8536 {
8537 int i;
8538
8539 for (i = 0; i < 2; ++i)
8540 if (!BUFFERP (echo_buffer[i])
8541 || NILP (XBUFFER (echo_buffer[i])->name))
8542 {
8543 char name[30];
8544 Lisp_Object old_buffer;
8545 int j;
8546
8547 old_buffer = echo_buffer[i];
8548 sprintf (name, " *Echo Area %d*", i);
8549 echo_buffer[i] = Fget_buffer_create (build_string (name));
8550 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8551 /* to force word wrap in echo area -
8552 it was decided to postpone this*/
8553 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8554
8555 for (j = 0; j < 2; ++j)
8556 if (EQ (old_buffer, echo_area_buffer[j]))
8557 echo_area_buffer[j] = echo_buffer[i];
8558 }
8559 }
8560
8561
8562 /* Call FN with args A1..A4 with either the current or last displayed
8563 echo_area_buffer as current buffer.
8564
8565 WHICH zero means use the current message buffer
8566 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8567 from echo_buffer[] and clear it.
8568
8569 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8570 suitable buffer from echo_buffer[] and clear it.
8571
8572 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8573 that the current message becomes the last displayed one, make
8574 choose a suitable buffer for echo_area_buffer[0], and clear it.
8575
8576 Value is what FN returns. */
8577
8578 static int
8579 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8580 struct window *w;
8581 int which;
8582 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8583 EMACS_INT a1;
8584 Lisp_Object a2;
8585 EMACS_INT a3, a4;
8586 {
8587 Lisp_Object buffer;
8588 int this_one, the_other, clear_buffer_p, rc;
8589 int count = SPECPDL_INDEX ();
8590
8591 /* If buffers aren't live, make new ones. */
8592 ensure_echo_area_buffers ();
8593
8594 clear_buffer_p = 0;
8595
8596 if (which == 0)
8597 this_one = 0, the_other = 1;
8598 else if (which > 0)
8599 this_one = 1, the_other = 0;
8600 else
8601 {
8602 this_one = 0, the_other = 1;
8603 clear_buffer_p = 1;
8604
8605 /* We need a fresh one in case the current echo buffer equals
8606 the one containing the last displayed echo area message. */
8607 if (!NILP (echo_area_buffer[this_one])
8608 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8609 echo_area_buffer[this_one] = Qnil;
8610 }
8611
8612 /* Choose a suitable buffer from echo_buffer[] is we don't
8613 have one. */
8614 if (NILP (echo_area_buffer[this_one]))
8615 {
8616 echo_area_buffer[this_one]
8617 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8618 ? echo_buffer[the_other]
8619 : echo_buffer[this_one]);
8620 clear_buffer_p = 1;
8621 }
8622
8623 buffer = echo_area_buffer[this_one];
8624
8625 /* Don't get confused by reusing the buffer used for echoing
8626 for a different purpose. */
8627 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8628 cancel_echoing ();
8629
8630 record_unwind_protect (unwind_with_echo_area_buffer,
8631 with_echo_area_buffer_unwind_data (w));
8632
8633 /* Make the echo area buffer current. Note that for display
8634 purposes, it is not necessary that the displayed window's buffer
8635 == current_buffer, except for text property lookup. So, let's
8636 only set that buffer temporarily here without doing a full
8637 Fset_window_buffer. We must also change w->pointm, though,
8638 because otherwise an assertions in unshow_buffer fails, and Emacs
8639 aborts. */
8640 set_buffer_internal_1 (XBUFFER (buffer));
8641 if (w)
8642 {
8643 w->buffer = buffer;
8644 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8645 }
8646
8647 current_buffer->undo_list = Qt;
8648 current_buffer->read_only = Qnil;
8649 specbind (Qinhibit_read_only, Qt);
8650 specbind (Qinhibit_modification_hooks, Qt);
8651
8652 if (clear_buffer_p && Z > BEG)
8653 del_range (BEG, Z);
8654
8655 xassert (BEGV >= BEG);
8656 xassert (ZV <= Z && ZV >= BEGV);
8657
8658 rc = fn (a1, a2, a3, a4);
8659
8660 xassert (BEGV >= BEG);
8661 xassert (ZV <= Z && ZV >= BEGV);
8662
8663 unbind_to (count, Qnil);
8664 return rc;
8665 }
8666
8667
8668 /* Save state that should be preserved around the call to the function
8669 FN called in with_echo_area_buffer. */
8670
8671 static Lisp_Object
8672 with_echo_area_buffer_unwind_data (w)
8673 struct window *w;
8674 {
8675 int i = 0;
8676 Lisp_Object vector, tmp;
8677
8678 /* Reduce consing by keeping one vector in
8679 Vwith_echo_area_save_vector. */
8680 vector = Vwith_echo_area_save_vector;
8681 Vwith_echo_area_save_vector = Qnil;
8682
8683 if (NILP (vector))
8684 vector = Fmake_vector (make_number (7), Qnil);
8685
8686 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8687 ASET (vector, i, Vdeactivate_mark); ++i;
8688 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8689
8690 if (w)
8691 {
8692 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8693 ASET (vector, i, w->buffer); ++i;
8694 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8695 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8696 }
8697 else
8698 {
8699 int end = i + 4;
8700 for (; i < end; ++i)
8701 ASET (vector, i, Qnil);
8702 }
8703
8704 xassert (i == ASIZE (vector));
8705 return vector;
8706 }
8707
8708
8709 /* Restore global state from VECTOR which was created by
8710 with_echo_area_buffer_unwind_data. */
8711
8712 static Lisp_Object
8713 unwind_with_echo_area_buffer (vector)
8714 Lisp_Object vector;
8715 {
8716 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8717 Vdeactivate_mark = AREF (vector, 1);
8718 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8719
8720 if (WINDOWP (AREF (vector, 3)))
8721 {
8722 struct window *w;
8723 Lisp_Object buffer, charpos, bytepos;
8724
8725 w = XWINDOW (AREF (vector, 3));
8726 buffer = AREF (vector, 4);
8727 charpos = AREF (vector, 5);
8728 bytepos = AREF (vector, 6);
8729
8730 w->buffer = buffer;
8731 set_marker_both (w->pointm, buffer,
8732 XFASTINT (charpos), XFASTINT (bytepos));
8733 }
8734
8735 Vwith_echo_area_save_vector = vector;
8736 return Qnil;
8737 }
8738
8739
8740 /* Set up the echo area for use by print functions. MULTIBYTE_P
8741 non-zero means we will print multibyte. */
8742
8743 void
8744 setup_echo_area_for_printing (multibyte_p)
8745 int multibyte_p;
8746 {
8747 /* If we can't find an echo area any more, exit. */
8748 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8749 Fkill_emacs (Qnil);
8750
8751 ensure_echo_area_buffers ();
8752
8753 if (!message_buf_print)
8754 {
8755 /* A message has been output since the last time we printed.
8756 Choose a fresh echo area buffer. */
8757 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8758 echo_area_buffer[0] = echo_buffer[1];
8759 else
8760 echo_area_buffer[0] = echo_buffer[0];
8761
8762 /* Switch to that buffer and clear it. */
8763 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8764 current_buffer->truncate_lines = Qnil;
8765
8766 if (Z > BEG)
8767 {
8768 int count = SPECPDL_INDEX ();
8769 specbind (Qinhibit_read_only, Qt);
8770 /* Note that undo recording is always disabled. */
8771 del_range (BEG, Z);
8772 unbind_to (count, Qnil);
8773 }
8774 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8775
8776 /* Set up the buffer for the multibyteness we need. */
8777 if (multibyte_p
8778 != !NILP (current_buffer->enable_multibyte_characters))
8779 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8780
8781 /* Raise the frame containing the echo area. */
8782 if (minibuffer_auto_raise)
8783 {
8784 struct frame *sf = SELECTED_FRAME ();
8785 Lisp_Object mini_window;
8786 mini_window = FRAME_MINIBUF_WINDOW (sf);
8787 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8788 }
8789
8790 message_log_maybe_newline ();
8791 message_buf_print = 1;
8792 }
8793 else
8794 {
8795 if (NILP (echo_area_buffer[0]))
8796 {
8797 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8798 echo_area_buffer[0] = echo_buffer[1];
8799 else
8800 echo_area_buffer[0] = echo_buffer[0];
8801 }
8802
8803 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8804 {
8805 /* Someone switched buffers between print requests. */
8806 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8807 current_buffer->truncate_lines = Qnil;
8808 }
8809 }
8810 }
8811
8812
8813 /* Display an echo area message in window W. Value is non-zero if W's
8814 height is changed. If display_last_displayed_message_p is
8815 non-zero, display the message that was last displayed, otherwise
8816 display the current message. */
8817
8818 static int
8819 display_echo_area (w)
8820 struct window *w;
8821 {
8822 int i, no_message_p, window_height_changed_p, count;
8823
8824 /* Temporarily disable garbage collections while displaying the echo
8825 area. This is done because a GC can print a message itself.
8826 That message would modify the echo area buffer's contents while a
8827 redisplay of the buffer is going on, and seriously confuse
8828 redisplay. */
8829 count = inhibit_garbage_collection ();
8830
8831 /* If there is no message, we must call display_echo_area_1
8832 nevertheless because it resizes the window. But we will have to
8833 reset the echo_area_buffer in question to nil at the end because
8834 with_echo_area_buffer will sets it to an empty buffer. */
8835 i = display_last_displayed_message_p ? 1 : 0;
8836 no_message_p = NILP (echo_area_buffer[i]);
8837
8838 window_height_changed_p
8839 = with_echo_area_buffer (w, display_last_displayed_message_p,
8840 display_echo_area_1,
8841 (EMACS_INT) w, Qnil, 0, 0);
8842
8843 if (no_message_p)
8844 echo_area_buffer[i] = Qnil;
8845
8846 unbind_to (count, Qnil);
8847 return window_height_changed_p;
8848 }
8849
8850
8851 /* Helper for display_echo_area. Display the current buffer which
8852 contains the current echo area message in window W, a mini-window,
8853 a pointer to which is passed in A1. A2..A4 are currently not used.
8854 Change the height of W so that all of the message is displayed.
8855 Value is non-zero if height of W was changed. */
8856
8857 static int
8858 display_echo_area_1 (a1, a2, a3, a4)
8859 EMACS_INT a1;
8860 Lisp_Object a2;
8861 EMACS_INT a3, a4;
8862 {
8863 struct window *w = (struct window *) a1;
8864 Lisp_Object window;
8865 struct text_pos start;
8866 int window_height_changed_p = 0;
8867
8868 /* Do this before displaying, so that we have a large enough glyph
8869 matrix for the display. If we can't get enough space for the
8870 whole text, display the last N lines. That works by setting w->start. */
8871 window_height_changed_p = resize_mini_window (w, 0);
8872
8873 /* Use the starting position chosen by resize_mini_window. */
8874 SET_TEXT_POS_FROM_MARKER (start, w->start);
8875
8876 /* Display. */
8877 clear_glyph_matrix (w->desired_matrix);
8878 XSETWINDOW (window, w);
8879 try_window (window, start, 0);
8880
8881 return window_height_changed_p;
8882 }
8883
8884
8885 /* Resize the echo area window to exactly the size needed for the
8886 currently displayed message, if there is one. If a mini-buffer
8887 is active, don't shrink it. */
8888
8889 void
8890 resize_echo_area_exactly ()
8891 {
8892 if (BUFFERP (echo_area_buffer[0])
8893 && WINDOWP (echo_area_window))
8894 {
8895 struct window *w = XWINDOW (echo_area_window);
8896 int resized_p;
8897 Lisp_Object resize_exactly;
8898
8899 if (minibuf_level == 0)
8900 resize_exactly = Qt;
8901 else
8902 resize_exactly = Qnil;
8903
8904 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8905 (EMACS_INT) w, resize_exactly, 0, 0);
8906 if (resized_p)
8907 {
8908 ++windows_or_buffers_changed;
8909 ++update_mode_lines;
8910 redisplay_internal (0);
8911 }
8912 }
8913 }
8914
8915
8916 /* Callback function for with_echo_area_buffer, when used from
8917 resize_echo_area_exactly. A1 contains a pointer to the window to
8918 resize, EXACTLY non-nil means resize the mini-window exactly to the
8919 size of the text displayed. A3 and A4 are not used. Value is what
8920 resize_mini_window returns. */
8921
8922 static int
8923 resize_mini_window_1 (a1, exactly, a3, a4)
8924 EMACS_INT a1;
8925 Lisp_Object exactly;
8926 EMACS_INT a3, a4;
8927 {
8928 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8929 }
8930
8931
8932 /* Resize mini-window W to fit the size of its contents. EXACT_P
8933 means size the window exactly to the size needed. Otherwise, it's
8934 only enlarged until W's buffer is empty.
8935
8936 Set W->start to the right place to begin display. If the whole
8937 contents fit, start at the beginning. Otherwise, start so as
8938 to make the end of the contents appear. This is particularly
8939 important for y-or-n-p, but seems desirable generally.
8940
8941 Value is non-zero if the window height has been changed. */
8942
8943 int
8944 resize_mini_window (w, exact_p)
8945 struct window *w;
8946 int exact_p;
8947 {
8948 struct frame *f = XFRAME (w->frame);
8949 int window_height_changed_p = 0;
8950
8951 xassert (MINI_WINDOW_P (w));
8952
8953 /* By default, start display at the beginning. */
8954 set_marker_both (w->start, w->buffer,
8955 BUF_BEGV (XBUFFER (w->buffer)),
8956 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8957
8958 /* Don't resize windows while redisplaying a window; it would
8959 confuse redisplay functions when the size of the window they are
8960 displaying changes from under them. Such a resizing can happen,
8961 for instance, when which-func prints a long message while
8962 we are running fontification-functions. We're running these
8963 functions with safe_call which binds inhibit-redisplay to t. */
8964 if (!NILP (Vinhibit_redisplay))
8965 return 0;
8966
8967 /* Nil means don't try to resize. */
8968 if (NILP (Vresize_mini_windows)
8969 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8970 return 0;
8971
8972 if (!FRAME_MINIBUF_ONLY_P (f))
8973 {
8974 struct it it;
8975 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8976 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8977 int height, max_height;
8978 int unit = FRAME_LINE_HEIGHT (f);
8979 struct text_pos start;
8980 struct buffer *old_current_buffer = NULL;
8981
8982 if (current_buffer != XBUFFER (w->buffer))
8983 {
8984 old_current_buffer = current_buffer;
8985 set_buffer_internal (XBUFFER (w->buffer));
8986 }
8987
8988 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8989
8990 /* Compute the max. number of lines specified by the user. */
8991 if (FLOATP (Vmax_mini_window_height))
8992 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8993 else if (INTEGERP (Vmax_mini_window_height))
8994 max_height = XINT (Vmax_mini_window_height);
8995 else
8996 max_height = total_height / 4;
8997
8998 /* Correct that max. height if it's bogus. */
8999 max_height = max (1, max_height);
9000 max_height = min (total_height, max_height);
9001
9002 /* Find out the height of the text in the window. */
9003 if (it.line_wrap == TRUNCATE)
9004 height = 1;
9005 else
9006 {
9007 last_height = 0;
9008 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9009 if (it.max_ascent == 0 && it.max_descent == 0)
9010 height = it.current_y + last_height;
9011 else
9012 height = it.current_y + it.max_ascent + it.max_descent;
9013 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9014 height = (height + unit - 1) / unit;
9015 }
9016
9017 /* Compute a suitable window start. */
9018 if (height > max_height)
9019 {
9020 height = max_height;
9021 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9022 move_it_vertically_backward (&it, (height - 1) * unit);
9023 start = it.current.pos;
9024 }
9025 else
9026 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9027 SET_MARKER_FROM_TEXT_POS (w->start, start);
9028
9029 if (EQ (Vresize_mini_windows, Qgrow_only))
9030 {
9031 /* Let it grow only, until we display an empty message, in which
9032 case the window shrinks again. */
9033 if (height > WINDOW_TOTAL_LINES (w))
9034 {
9035 int old_height = WINDOW_TOTAL_LINES (w);
9036 freeze_window_starts (f, 1);
9037 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9038 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9039 }
9040 else if (height < WINDOW_TOTAL_LINES (w)
9041 && (exact_p || BEGV == ZV))
9042 {
9043 int old_height = WINDOW_TOTAL_LINES (w);
9044 freeze_window_starts (f, 0);
9045 shrink_mini_window (w);
9046 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9047 }
9048 }
9049 else
9050 {
9051 /* Always resize to exact size needed. */
9052 if (height > WINDOW_TOTAL_LINES (w))
9053 {
9054 int old_height = WINDOW_TOTAL_LINES (w);
9055 freeze_window_starts (f, 1);
9056 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9057 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9058 }
9059 else if (height < WINDOW_TOTAL_LINES (w))
9060 {
9061 int old_height = WINDOW_TOTAL_LINES (w);
9062 freeze_window_starts (f, 0);
9063 shrink_mini_window (w);
9064
9065 if (height)
9066 {
9067 freeze_window_starts (f, 1);
9068 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9069 }
9070
9071 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9072 }
9073 }
9074
9075 if (old_current_buffer)
9076 set_buffer_internal (old_current_buffer);
9077 }
9078
9079 return window_height_changed_p;
9080 }
9081
9082
9083 /* Value is the current message, a string, or nil if there is no
9084 current message. */
9085
9086 Lisp_Object
9087 current_message ()
9088 {
9089 Lisp_Object msg;
9090
9091 if (!BUFFERP (echo_area_buffer[0]))
9092 msg = Qnil;
9093 else
9094 {
9095 with_echo_area_buffer (0, 0, current_message_1,
9096 (EMACS_INT) &msg, Qnil, 0, 0);
9097 if (NILP (msg))
9098 echo_area_buffer[0] = Qnil;
9099 }
9100
9101 return msg;
9102 }
9103
9104
9105 static int
9106 current_message_1 (a1, a2, a3, a4)
9107 EMACS_INT a1;
9108 Lisp_Object a2;
9109 EMACS_INT a3, a4;
9110 {
9111 Lisp_Object *msg = (Lisp_Object *) a1;
9112
9113 if (Z > BEG)
9114 *msg = make_buffer_string (BEG, Z, 1);
9115 else
9116 *msg = Qnil;
9117 return 0;
9118 }
9119
9120
9121 /* Push the current message on Vmessage_stack for later restauration
9122 by restore_message. Value is non-zero if the current message isn't
9123 empty. This is a relatively infrequent operation, so it's not
9124 worth optimizing. */
9125
9126 int
9127 push_message ()
9128 {
9129 Lisp_Object msg;
9130 msg = current_message ();
9131 Vmessage_stack = Fcons (msg, Vmessage_stack);
9132 return STRINGP (msg);
9133 }
9134
9135
9136 /* Restore message display from the top of Vmessage_stack. */
9137
9138 void
9139 restore_message ()
9140 {
9141 Lisp_Object msg;
9142
9143 xassert (CONSP (Vmessage_stack));
9144 msg = XCAR (Vmessage_stack);
9145 if (STRINGP (msg))
9146 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9147 else
9148 message3_nolog (msg, 0, 0);
9149 }
9150
9151
9152 /* Handler for record_unwind_protect calling pop_message. */
9153
9154 Lisp_Object
9155 pop_message_unwind (dummy)
9156 Lisp_Object dummy;
9157 {
9158 pop_message ();
9159 return Qnil;
9160 }
9161
9162 /* Pop the top-most entry off Vmessage_stack. */
9163
9164 void
9165 pop_message ()
9166 {
9167 xassert (CONSP (Vmessage_stack));
9168 Vmessage_stack = XCDR (Vmessage_stack);
9169 }
9170
9171
9172 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9173 exits. If the stack is not empty, we have a missing pop_message
9174 somewhere. */
9175
9176 void
9177 check_message_stack ()
9178 {
9179 if (!NILP (Vmessage_stack))
9180 abort ();
9181 }
9182
9183
9184 /* Truncate to NCHARS what will be displayed in the echo area the next
9185 time we display it---but don't redisplay it now. */
9186
9187 void
9188 truncate_echo_area (nchars)
9189 int nchars;
9190 {
9191 if (nchars == 0)
9192 echo_area_buffer[0] = Qnil;
9193 /* A null message buffer means that the frame hasn't really been
9194 initialized yet. Error messages get reported properly by
9195 cmd_error, so this must be just an informative message; toss it. */
9196 else if (!noninteractive
9197 && INTERACTIVE
9198 && !NILP (echo_area_buffer[0]))
9199 {
9200 struct frame *sf = SELECTED_FRAME ();
9201 if (FRAME_MESSAGE_BUF (sf))
9202 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9203 }
9204 }
9205
9206
9207 /* Helper function for truncate_echo_area. Truncate the current
9208 message to at most NCHARS characters. */
9209
9210 static int
9211 truncate_message_1 (nchars, a2, a3, a4)
9212 EMACS_INT nchars;
9213 Lisp_Object a2;
9214 EMACS_INT a3, a4;
9215 {
9216 if (BEG + nchars < Z)
9217 del_range (BEG + nchars, Z);
9218 if (Z == BEG)
9219 echo_area_buffer[0] = Qnil;
9220 return 0;
9221 }
9222
9223
9224 /* Set the current message to a substring of S or STRING.
9225
9226 If STRING is a Lisp string, set the message to the first NBYTES
9227 bytes from STRING. NBYTES zero means use the whole string. If
9228 STRING is multibyte, the message will be displayed multibyte.
9229
9230 If S is not null, set the message to the first LEN bytes of S. LEN
9231 zero means use the whole string. MULTIBYTE_P non-zero means S is
9232 multibyte. Display the message multibyte in that case.
9233
9234 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9235 to t before calling set_message_1 (which calls insert).
9236 */
9237
9238 void
9239 set_message (s, string, nbytes, multibyte_p)
9240 const char *s;
9241 Lisp_Object string;
9242 int nbytes, multibyte_p;
9243 {
9244 message_enable_multibyte
9245 = ((s && multibyte_p)
9246 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9247
9248 with_echo_area_buffer (0, -1, set_message_1,
9249 (EMACS_INT) s, string, nbytes, multibyte_p);
9250 message_buf_print = 0;
9251 help_echo_showing_p = 0;
9252 }
9253
9254
9255 /* Helper function for set_message. Arguments have the same meaning
9256 as there, with A1 corresponding to S and A2 corresponding to STRING
9257 This function is called with the echo area buffer being
9258 current. */
9259
9260 static int
9261 set_message_1 (a1, a2, nbytes, multibyte_p)
9262 EMACS_INT a1;
9263 Lisp_Object a2;
9264 EMACS_INT nbytes, multibyte_p;
9265 {
9266 const char *s = (const char *) a1;
9267 Lisp_Object string = a2;
9268
9269 /* Change multibyteness of the echo buffer appropriately. */
9270 if (message_enable_multibyte
9271 != !NILP (current_buffer->enable_multibyte_characters))
9272 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9273
9274 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9275
9276 /* Insert new message at BEG. */
9277 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9278
9279 if (STRINGP (string))
9280 {
9281 int nchars;
9282
9283 if (nbytes == 0)
9284 nbytes = SBYTES (string);
9285 nchars = string_byte_to_char (string, nbytes);
9286
9287 /* This function takes care of single/multibyte conversion. We
9288 just have to ensure that the echo area buffer has the right
9289 setting of enable_multibyte_characters. */
9290 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9291 }
9292 else if (s)
9293 {
9294 if (nbytes == 0)
9295 nbytes = strlen (s);
9296
9297 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9298 {
9299 /* Convert from multi-byte to single-byte. */
9300 int i, c, n;
9301 unsigned char work[1];
9302
9303 /* Convert a multibyte string to single-byte. */
9304 for (i = 0; i < nbytes; i += n)
9305 {
9306 c = string_char_and_length (s + i, &n);
9307 work[0] = (ASCII_CHAR_P (c)
9308 ? c
9309 : multibyte_char_to_unibyte (c, Qnil));
9310 insert_1_both (work, 1, 1, 1, 0, 0);
9311 }
9312 }
9313 else if (!multibyte_p
9314 && !NILP (current_buffer->enable_multibyte_characters))
9315 {
9316 /* Convert from single-byte to multi-byte. */
9317 int i, c, n;
9318 const unsigned char *msg = (const unsigned char *) s;
9319 unsigned char str[MAX_MULTIBYTE_LENGTH];
9320
9321 /* Convert a single-byte string to multibyte. */
9322 for (i = 0; i < nbytes; i++)
9323 {
9324 c = msg[i];
9325 MAKE_CHAR_MULTIBYTE (c);
9326 n = CHAR_STRING (c, str);
9327 insert_1_both (str, 1, n, 1, 0, 0);
9328 }
9329 }
9330 else
9331 insert_1 (s, nbytes, 1, 0, 0);
9332 }
9333
9334 return 0;
9335 }
9336
9337
9338 /* Clear messages. CURRENT_P non-zero means clear the current
9339 message. LAST_DISPLAYED_P non-zero means clear the message
9340 last displayed. */
9341
9342 void
9343 clear_message (current_p, last_displayed_p)
9344 int current_p, last_displayed_p;
9345 {
9346 if (current_p)
9347 {
9348 echo_area_buffer[0] = Qnil;
9349 message_cleared_p = 1;
9350 }
9351
9352 if (last_displayed_p)
9353 echo_area_buffer[1] = Qnil;
9354
9355 message_buf_print = 0;
9356 }
9357
9358 /* Clear garbaged frames.
9359
9360 This function is used where the old redisplay called
9361 redraw_garbaged_frames which in turn called redraw_frame which in
9362 turn called clear_frame. The call to clear_frame was a source of
9363 flickering. I believe a clear_frame is not necessary. It should
9364 suffice in the new redisplay to invalidate all current matrices,
9365 and ensure a complete redisplay of all windows. */
9366
9367 static void
9368 clear_garbaged_frames ()
9369 {
9370 if (frame_garbaged)
9371 {
9372 Lisp_Object tail, frame;
9373 int changed_count = 0;
9374
9375 FOR_EACH_FRAME (tail, frame)
9376 {
9377 struct frame *f = XFRAME (frame);
9378
9379 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9380 {
9381 if (f->resized_p)
9382 {
9383 Fredraw_frame (frame);
9384 f->force_flush_display_p = 1;
9385 }
9386 clear_current_matrices (f);
9387 changed_count++;
9388 f->garbaged = 0;
9389 f->resized_p = 0;
9390 }
9391 }
9392
9393 frame_garbaged = 0;
9394 if (changed_count)
9395 ++windows_or_buffers_changed;
9396 }
9397 }
9398
9399
9400 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9401 is non-zero update selected_frame. Value is non-zero if the
9402 mini-windows height has been changed. */
9403
9404 static int
9405 echo_area_display (update_frame_p)
9406 int update_frame_p;
9407 {
9408 Lisp_Object mini_window;
9409 struct window *w;
9410 struct frame *f;
9411 int window_height_changed_p = 0;
9412 struct frame *sf = SELECTED_FRAME ();
9413
9414 mini_window = FRAME_MINIBUF_WINDOW (sf);
9415 w = XWINDOW (mini_window);
9416 f = XFRAME (WINDOW_FRAME (w));
9417
9418 /* Don't display if frame is invisible or not yet initialized. */
9419 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9420 return 0;
9421
9422 #ifdef HAVE_WINDOW_SYSTEM
9423 /* When Emacs starts, selected_frame may be the initial terminal
9424 frame. If we let this through, a message would be displayed on
9425 the terminal. */
9426 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9427 return 0;
9428 #endif /* HAVE_WINDOW_SYSTEM */
9429
9430 /* Redraw garbaged frames. */
9431 if (frame_garbaged)
9432 clear_garbaged_frames ();
9433
9434 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9435 {
9436 echo_area_window = mini_window;
9437 window_height_changed_p = display_echo_area (w);
9438 w->must_be_updated_p = 1;
9439
9440 /* Update the display, unless called from redisplay_internal.
9441 Also don't update the screen during redisplay itself. The
9442 update will happen at the end of redisplay, and an update
9443 here could cause confusion. */
9444 if (update_frame_p && !redisplaying_p)
9445 {
9446 int n = 0;
9447
9448 /* If the display update has been interrupted by pending
9449 input, update mode lines in the frame. Due to the
9450 pending input, it might have been that redisplay hasn't
9451 been called, so that mode lines above the echo area are
9452 garbaged. This looks odd, so we prevent it here. */
9453 if (!display_completed)
9454 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9455
9456 if (window_height_changed_p
9457 /* Don't do this if Emacs is shutting down. Redisplay
9458 needs to run hooks. */
9459 && !NILP (Vrun_hooks))
9460 {
9461 /* Must update other windows. Likewise as in other
9462 cases, don't let this update be interrupted by
9463 pending input. */
9464 int count = SPECPDL_INDEX ();
9465 specbind (Qredisplay_dont_pause, Qt);
9466 windows_or_buffers_changed = 1;
9467 redisplay_internal (0);
9468 unbind_to (count, Qnil);
9469 }
9470 else if (FRAME_WINDOW_P (f) && n == 0)
9471 {
9472 /* Window configuration is the same as before.
9473 Can do with a display update of the echo area,
9474 unless we displayed some mode lines. */
9475 update_single_window (w, 1);
9476 FRAME_RIF (f)->flush_display (f);
9477 }
9478 else
9479 update_frame (f, 1, 1);
9480
9481 /* If cursor is in the echo area, make sure that the next
9482 redisplay displays the minibuffer, so that the cursor will
9483 be replaced with what the minibuffer wants. */
9484 if (cursor_in_echo_area)
9485 ++windows_or_buffers_changed;
9486 }
9487 }
9488 else if (!EQ (mini_window, selected_window))
9489 windows_or_buffers_changed++;
9490
9491 /* Last displayed message is now the current message. */
9492 echo_area_buffer[1] = echo_area_buffer[0];
9493 /* Inform read_char that we're not echoing. */
9494 echo_message_buffer = Qnil;
9495
9496 /* Prevent redisplay optimization in redisplay_internal by resetting
9497 this_line_start_pos. This is done because the mini-buffer now
9498 displays the message instead of its buffer text. */
9499 if (EQ (mini_window, selected_window))
9500 CHARPOS (this_line_start_pos) = 0;
9501
9502 return window_height_changed_p;
9503 }
9504
9505
9506 \f
9507 /***********************************************************************
9508 Mode Lines and Frame Titles
9509 ***********************************************************************/
9510
9511 /* A buffer for constructing non-propertized mode-line strings and
9512 frame titles in it; allocated from the heap in init_xdisp and
9513 resized as needed in store_mode_line_noprop_char. */
9514
9515 static char *mode_line_noprop_buf;
9516
9517 /* The buffer's end, and a current output position in it. */
9518
9519 static char *mode_line_noprop_buf_end;
9520 static char *mode_line_noprop_ptr;
9521
9522 #define MODE_LINE_NOPROP_LEN(start) \
9523 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9524
9525 static enum {
9526 MODE_LINE_DISPLAY = 0,
9527 MODE_LINE_TITLE,
9528 MODE_LINE_NOPROP,
9529 MODE_LINE_STRING
9530 } mode_line_target;
9531
9532 /* Alist that caches the results of :propertize.
9533 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9534 static Lisp_Object mode_line_proptrans_alist;
9535
9536 /* List of strings making up the mode-line. */
9537 static Lisp_Object mode_line_string_list;
9538
9539 /* Base face property when building propertized mode line string. */
9540 static Lisp_Object mode_line_string_face;
9541 static Lisp_Object mode_line_string_face_prop;
9542
9543
9544 /* Unwind data for mode line strings */
9545
9546 static Lisp_Object Vmode_line_unwind_vector;
9547
9548 static Lisp_Object
9549 format_mode_line_unwind_data (struct buffer *obuf,
9550 Lisp_Object owin,
9551 int save_proptrans)
9552 {
9553 Lisp_Object vector, tmp;
9554
9555 /* Reduce consing by keeping one vector in
9556 Vwith_echo_area_save_vector. */
9557 vector = Vmode_line_unwind_vector;
9558 Vmode_line_unwind_vector = Qnil;
9559
9560 if (NILP (vector))
9561 vector = Fmake_vector (make_number (8), Qnil);
9562
9563 ASET (vector, 0, make_number (mode_line_target));
9564 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9565 ASET (vector, 2, mode_line_string_list);
9566 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9567 ASET (vector, 4, mode_line_string_face);
9568 ASET (vector, 5, mode_line_string_face_prop);
9569
9570 if (obuf)
9571 XSETBUFFER (tmp, obuf);
9572 else
9573 tmp = Qnil;
9574 ASET (vector, 6, tmp);
9575 ASET (vector, 7, owin);
9576
9577 return vector;
9578 }
9579
9580 static Lisp_Object
9581 unwind_format_mode_line (vector)
9582 Lisp_Object vector;
9583 {
9584 mode_line_target = XINT (AREF (vector, 0));
9585 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9586 mode_line_string_list = AREF (vector, 2);
9587 if (! EQ (AREF (vector, 3), Qt))
9588 mode_line_proptrans_alist = AREF (vector, 3);
9589 mode_line_string_face = AREF (vector, 4);
9590 mode_line_string_face_prop = AREF (vector, 5);
9591
9592 if (!NILP (AREF (vector, 7)))
9593 /* Select window before buffer, since it may change the buffer. */
9594 Fselect_window (AREF (vector, 7), Qt);
9595
9596 if (!NILP (AREF (vector, 6)))
9597 {
9598 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9599 ASET (vector, 6, Qnil);
9600 }
9601
9602 Vmode_line_unwind_vector = vector;
9603 return Qnil;
9604 }
9605
9606
9607 /* Store a single character C for the frame title in mode_line_noprop_buf.
9608 Re-allocate mode_line_noprop_buf if necessary. */
9609
9610 static void
9611 #ifdef PROTOTYPES
9612 store_mode_line_noprop_char (char c)
9613 #else
9614 store_mode_line_noprop_char (c)
9615 char c;
9616 #endif
9617 {
9618 /* If output position has reached the end of the allocated buffer,
9619 double the buffer's size. */
9620 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9621 {
9622 int len = MODE_LINE_NOPROP_LEN (0);
9623 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9624 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9625 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9626 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9627 }
9628
9629 *mode_line_noprop_ptr++ = c;
9630 }
9631
9632
9633 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9634 mode_line_noprop_ptr. STR is the string to store. Do not copy
9635 characters that yield more columns than PRECISION; PRECISION <= 0
9636 means copy the whole string. Pad with spaces until FIELD_WIDTH
9637 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9638 pad. Called from display_mode_element when it is used to build a
9639 frame title. */
9640
9641 static int
9642 store_mode_line_noprop (str, field_width, precision)
9643 const unsigned char *str;
9644 int field_width, precision;
9645 {
9646 int n = 0;
9647 int dummy, nbytes;
9648
9649 /* Copy at most PRECISION chars from STR. */
9650 nbytes = strlen (str);
9651 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9652 while (nbytes--)
9653 store_mode_line_noprop_char (*str++);
9654
9655 /* Fill up with spaces until FIELD_WIDTH reached. */
9656 while (field_width > 0
9657 && n < field_width)
9658 {
9659 store_mode_line_noprop_char (' ');
9660 ++n;
9661 }
9662
9663 return n;
9664 }
9665
9666 /***********************************************************************
9667 Frame Titles
9668 ***********************************************************************/
9669
9670 #ifdef HAVE_WINDOW_SYSTEM
9671
9672 /* Set the title of FRAME, if it has changed. The title format is
9673 Vicon_title_format if FRAME is iconified, otherwise it is
9674 frame_title_format. */
9675
9676 static void
9677 x_consider_frame_title (frame)
9678 Lisp_Object frame;
9679 {
9680 struct frame *f = XFRAME (frame);
9681
9682 if (FRAME_WINDOW_P (f)
9683 || FRAME_MINIBUF_ONLY_P (f)
9684 || f->explicit_name)
9685 {
9686 /* Do we have more than one visible frame on this X display? */
9687 Lisp_Object tail;
9688 Lisp_Object fmt;
9689 int title_start;
9690 char *title;
9691 int len;
9692 struct it it;
9693 int count = SPECPDL_INDEX ();
9694
9695 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9696 {
9697 Lisp_Object other_frame = XCAR (tail);
9698 struct frame *tf = XFRAME (other_frame);
9699
9700 if (tf != f
9701 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9702 && !FRAME_MINIBUF_ONLY_P (tf)
9703 && !EQ (other_frame, tip_frame)
9704 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9705 break;
9706 }
9707
9708 /* Set global variable indicating that multiple frames exist. */
9709 multiple_frames = CONSP (tail);
9710
9711 /* Switch to the buffer of selected window of the frame. Set up
9712 mode_line_target so that display_mode_element will output into
9713 mode_line_noprop_buf; then display the title. */
9714 record_unwind_protect (unwind_format_mode_line,
9715 format_mode_line_unwind_data
9716 (current_buffer, selected_window, 0));
9717
9718 Fselect_window (f->selected_window, Qt);
9719 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9720 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9721
9722 mode_line_target = MODE_LINE_TITLE;
9723 title_start = MODE_LINE_NOPROP_LEN (0);
9724 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9725 NULL, DEFAULT_FACE_ID);
9726 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9727 len = MODE_LINE_NOPROP_LEN (title_start);
9728 title = mode_line_noprop_buf + title_start;
9729 unbind_to (count, Qnil);
9730
9731 /* Set the title only if it's changed. This avoids consing in
9732 the common case where it hasn't. (If it turns out that we've
9733 already wasted too much time by walking through the list with
9734 display_mode_element, then we might need to optimize at a
9735 higher level than this.) */
9736 if (! STRINGP (f->name)
9737 || SBYTES (f->name) != len
9738 || bcmp (title, SDATA (f->name), len) != 0)
9739 x_implicitly_set_name (f, make_string (title, len), Qnil);
9740 }
9741 }
9742
9743 #endif /* not HAVE_WINDOW_SYSTEM */
9744
9745
9746
9747 \f
9748 /***********************************************************************
9749 Menu Bars
9750 ***********************************************************************/
9751
9752
9753 /* Prepare for redisplay by updating menu-bar item lists when
9754 appropriate. This can call eval. */
9755
9756 void
9757 prepare_menu_bars ()
9758 {
9759 int all_windows;
9760 struct gcpro gcpro1, gcpro2;
9761 struct frame *f;
9762 Lisp_Object tooltip_frame;
9763
9764 #ifdef HAVE_WINDOW_SYSTEM
9765 tooltip_frame = tip_frame;
9766 #else
9767 tooltip_frame = Qnil;
9768 #endif
9769
9770 /* Update all frame titles based on their buffer names, etc. We do
9771 this before the menu bars so that the buffer-menu will show the
9772 up-to-date frame titles. */
9773 #ifdef HAVE_WINDOW_SYSTEM
9774 if (windows_or_buffers_changed || update_mode_lines)
9775 {
9776 Lisp_Object tail, frame;
9777
9778 FOR_EACH_FRAME (tail, frame)
9779 {
9780 f = XFRAME (frame);
9781 if (!EQ (frame, tooltip_frame)
9782 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9783 x_consider_frame_title (frame);
9784 }
9785 }
9786 #endif /* HAVE_WINDOW_SYSTEM */
9787
9788 /* Update the menu bar item lists, if appropriate. This has to be
9789 done before any actual redisplay or generation of display lines. */
9790 all_windows = (update_mode_lines
9791 || buffer_shared > 1
9792 || windows_or_buffers_changed);
9793 if (all_windows)
9794 {
9795 Lisp_Object tail, frame;
9796 int count = SPECPDL_INDEX ();
9797 /* 1 means that update_menu_bar has run its hooks
9798 so any further calls to update_menu_bar shouldn't do so again. */
9799 int menu_bar_hooks_run = 0;
9800
9801 record_unwind_save_match_data ();
9802
9803 FOR_EACH_FRAME (tail, frame)
9804 {
9805 f = XFRAME (frame);
9806
9807 /* Ignore tooltip frame. */
9808 if (EQ (frame, tooltip_frame))
9809 continue;
9810
9811 /* If a window on this frame changed size, report that to
9812 the user and clear the size-change flag. */
9813 if (FRAME_WINDOW_SIZES_CHANGED (f))
9814 {
9815 Lisp_Object functions;
9816
9817 /* Clear flag first in case we get an error below. */
9818 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9819 functions = Vwindow_size_change_functions;
9820 GCPRO2 (tail, functions);
9821
9822 while (CONSP (functions))
9823 {
9824 if (!EQ (XCAR (functions), Qt))
9825 call1 (XCAR (functions), frame);
9826 functions = XCDR (functions);
9827 }
9828 UNGCPRO;
9829 }
9830
9831 GCPRO1 (tail);
9832 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9833 #ifdef HAVE_WINDOW_SYSTEM
9834 update_tool_bar (f, 0);
9835 #endif
9836 #ifdef HAVE_NS
9837 if (windows_or_buffers_changed)
9838 ns_set_doc_edited (f, Fbuffer_modified_p
9839 (XWINDOW (f->selected_window)->buffer));
9840 #endif
9841 UNGCPRO;
9842 }
9843
9844 unbind_to (count, Qnil);
9845 }
9846 else
9847 {
9848 struct frame *sf = SELECTED_FRAME ();
9849 update_menu_bar (sf, 1, 0);
9850 #ifdef HAVE_WINDOW_SYSTEM
9851 update_tool_bar (sf, 1);
9852 #endif
9853 }
9854
9855 /* Motif needs this. See comment in xmenu.c. Turn it off when
9856 pending_menu_activation is not defined. */
9857 #ifdef USE_X_TOOLKIT
9858 pending_menu_activation = 0;
9859 #endif
9860 }
9861
9862
9863 /* Update the menu bar item list for frame F. This has to be done
9864 before we start to fill in any display lines, because it can call
9865 eval.
9866
9867 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9868
9869 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9870 already ran the menu bar hooks for this redisplay, so there
9871 is no need to run them again. The return value is the
9872 updated value of this flag, to pass to the next call. */
9873
9874 static int
9875 update_menu_bar (f, save_match_data, hooks_run)
9876 struct frame *f;
9877 int save_match_data;
9878 int hooks_run;
9879 {
9880 Lisp_Object window;
9881 register struct window *w;
9882
9883 /* If called recursively during a menu update, do nothing. This can
9884 happen when, for instance, an activate-menubar-hook causes a
9885 redisplay. */
9886 if (inhibit_menubar_update)
9887 return hooks_run;
9888
9889 window = FRAME_SELECTED_WINDOW (f);
9890 w = XWINDOW (window);
9891
9892 if (FRAME_WINDOW_P (f)
9893 ?
9894 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9895 || defined (HAVE_NS) || defined (USE_GTK)
9896 FRAME_EXTERNAL_MENU_BAR (f)
9897 #else
9898 FRAME_MENU_BAR_LINES (f) > 0
9899 #endif
9900 : FRAME_MENU_BAR_LINES (f) > 0)
9901 {
9902 /* If the user has switched buffers or windows, we need to
9903 recompute to reflect the new bindings. But we'll
9904 recompute when update_mode_lines is set too; that means
9905 that people can use force-mode-line-update to request
9906 that the menu bar be recomputed. The adverse effect on
9907 the rest of the redisplay algorithm is about the same as
9908 windows_or_buffers_changed anyway. */
9909 if (windows_or_buffers_changed
9910 /* This used to test w->update_mode_line, but we believe
9911 there is no need to recompute the menu in that case. */
9912 || update_mode_lines
9913 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9914 < BUF_MODIFF (XBUFFER (w->buffer)))
9915 != !NILP (w->last_had_star))
9916 || ((!NILP (Vtransient_mark_mode)
9917 && !NILP (XBUFFER (w->buffer)->mark_active))
9918 != !NILP (w->region_showing)))
9919 {
9920 struct buffer *prev = current_buffer;
9921 int count = SPECPDL_INDEX ();
9922
9923 specbind (Qinhibit_menubar_update, Qt);
9924
9925 set_buffer_internal_1 (XBUFFER (w->buffer));
9926 if (save_match_data)
9927 record_unwind_save_match_data ();
9928 if (NILP (Voverriding_local_map_menu_flag))
9929 {
9930 specbind (Qoverriding_terminal_local_map, Qnil);
9931 specbind (Qoverriding_local_map, Qnil);
9932 }
9933
9934 if (!hooks_run)
9935 {
9936 /* Run the Lucid hook. */
9937 safe_run_hooks (Qactivate_menubar_hook);
9938
9939 /* If it has changed current-menubar from previous value,
9940 really recompute the menu-bar from the value. */
9941 if (! NILP (Vlucid_menu_bar_dirty_flag))
9942 call0 (Qrecompute_lucid_menubar);
9943
9944 safe_run_hooks (Qmenu_bar_update_hook);
9945
9946 hooks_run = 1;
9947 }
9948
9949 XSETFRAME (Vmenu_updating_frame, f);
9950 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9951
9952 /* Redisplay the menu bar in case we changed it. */
9953 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9954 || defined (HAVE_NS) || defined (USE_GTK)
9955 if (FRAME_WINDOW_P (f))
9956 {
9957 #if defined (HAVE_NS)
9958 /* All frames on Mac OS share the same menubar. So only
9959 the selected frame should be allowed to set it. */
9960 if (f == SELECTED_FRAME ())
9961 #endif
9962 set_frame_menubar (f, 0, 0);
9963 }
9964 else
9965 /* On a terminal screen, the menu bar is an ordinary screen
9966 line, and this makes it get updated. */
9967 w->update_mode_line = Qt;
9968 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9969 /* In the non-toolkit version, the menu bar is an ordinary screen
9970 line, and this makes it get updated. */
9971 w->update_mode_line = Qt;
9972 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9973
9974 unbind_to (count, Qnil);
9975 set_buffer_internal_1 (prev);
9976 }
9977 }
9978
9979 return hooks_run;
9980 }
9981
9982
9983 \f
9984 /***********************************************************************
9985 Output Cursor
9986 ***********************************************************************/
9987
9988 #ifdef HAVE_WINDOW_SYSTEM
9989
9990 /* EXPORT:
9991 Nominal cursor position -- where to draw output.
9992 HPOS and VPOS are window relative glyph matrix coordinates.
9993 X and Y are window relative pixel coordinates. */
9994
9995 struct cursor_pos output_cursor;
9996
9997
9998 /* EXPORT:
9999 Set the global variable output_cursor to CURSOR. All cursor
10000 positions are relative to updated_window. */
10001
10002 void
10003 set_output_cursor (cursor)
10004 struct cursor_pos *cursor;
10005 {
10006 output_cursor.hpos = cursor->hpos;
10007 output_cursor.vpos = cursor->vpos;
10008 output_cursor.x = cursor->x;
10009 output_cursor.y = cursor->y;
10010 }
10011
10012
10013 /* EXPORT for RIF:
10014 Set a nominal cursor position.
10015
10016 HPOS and VPOS are column/row positions in a window glyph matrix. X
10017 and Y are window text area relative pixel positions.
10018
10019 If this is done during an update, updated_window will contain the
10020 window that is being updated and the position is the future output
10021 cursor position for that window. If updated_window is null, use
10022 selected_window and display the cursor at the given position. */
10023
10024 void
10025 x_cursor_to (vpos, hpos, y, x)
10026 int vpos, hpos, y, x;
10027 {
10028 struct window *w;
10029
10030 /* If updated_window is not set, work on selected_window. */
10031 if (updated_window)
10032 w = updated_window;
10033 else
10034 w = XWINDOW (selected_window);
10035
10036 /* Set the output cursor. */
10037 output_cursor.hpos = hpos;
10038 output_cursor.vpos = vpos;
10039 output_cursor.x = x;
10040 output_cursor.y = y;
10041
10042 /* If not called as part of an update, really display the cursor.
10043 This will also set the cursor position of W. */
10044 if (updated_window == NULL)
10045 {
10046 BLOCK_INPUT;
10047 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10048 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10049 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10050 UNBLOCK_INPUT;
10051 }
10052 }
10053
10054 #endif /* HAVE_WINDOW_SYSTEM */
10055
10056 \f
10057 /***********************************************************************
10058 Tool-bars
10059 ***********************************************************************/
10060
10061 #ifdef HAVE_WINDOW_SYSTEM
10062
10063 /* Where the mouse was last time we reported a mouse event. */
10064
10065 FRAME_PTR last_mouse_frame;
10066
10067 /* Tool-bar item index of the item on which a mouse button was pressed
10068 or -1. */
10069
10070 int last_tool_bar_item;
10071
10072
10073 static Lisp_Object
10074 update_tool_bar_unwind (frame)
10075 Lisp_Object frame;
10076 {
10077 selected_frame = frame;
10078 return Qnil;
10079 }
10080
10081 /* Update the tool-bar item list for frame F. This has to be done
10082 before we start to fill in any display lines. Called from
10083 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10084 and restore it here. */
10085
10086 static void
10087 update_tool_bar (f, save_match_data)
10088 struct frame *f;
10089 int save_match_data;
10090 {
10091 #if defined (USE_GTK) || defined (HAVE_NS)
10092 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10093 #else
10094 int do_update = WINDOWP (f->tool_bar_window)
10095 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10096 #endif
10097
10098 if (do_update)
10099 {
10100 Lisp_Object window;
10101 struct window *w;
10102
10103 window = FRAME_SELECTED_WINDOW (f);
10104 w = XWINDOW (window);
10105
10106 /* If the user has switched buffers or windows, we need to
10107 recompute to reflect the new bindings. But we'll
10108 recompute when update_mode_lines is set too; that means
10109 that people can use force-mode-line-update to request
10110 that the menu bar be recomputed. The adverse effect on
10111 the rest of the redisplay algorithm is about the same as
10112 windows_or_buffers_changed anyway. */
10113 if (windows_or_buffers_changed
10114 || !NILP (w->update_mode_line)
10115 || update_mode_lines
10116 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10117 < BUF_MODIFF (XBUFFER (w->buffer)))
10118 != !NILP (w->last_had_star))
10119 || ((!NILP (Vtransient_mark_mode)
10120 && !NILP (XBUFFER (w->buffer)->mark_active))
10121 != !NILP (w->region_showing)))
10122 {
10123 struct buffer *prev = current_buffer;
10124 int count = SPECPDL_INDEX ();
10125 Lisp_Object frame, new_tool_bar;
10126 int new_n_tool_bar;
10127 struct gcpro gcpro1;
10128
10129 /* Set current_buffer to the buffer of the selected
10130 window of the frame, so that we get the right local
10131 keymaps. */
10132 set_buffer_internal_1 (XBUFFER (w->buffer));
10133
10134 /* Save match data, if we must. */
10135 if (save_match_data)
10136 record_unwind_save_match_data ();
10137
10138 /* Make sure that we don't accidentally use bogus keymaps. */
10139 if (NILP (Voverriding_local_map_menu_flag))
10140 {
10141 specbind (Qoverriding_terminal_local_map, Qnil);
10142 specbind (Qoverriding_local_map, Qnil);
10143 }
10144
10145 GCPRO1 (new_tool_bar);
10146
10147 /* We must temporarily set the selected frame to this frame
10148 before calling tool_bar_items, because the calculation of
10149 the tool-bar keymap uses the selected frame (see
10150 `tool-bar-make-keymap' in tool-bar.el). */
10151 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10152 XSETFRAME (frame, f);
10153 selected_frame = frame;
10154
10155 /* Build desired tool-bar items from keymaps. */
10156 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10157 &new_n_tool_bar);
10158
10159 /* Redisplay the tool-bar if we changed it. */
10160 if (new_n_tool_bar != f->n_tool_bar_items
10161 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10162 {
10163 /* Redisplay that happens asynchronously due to an expose event
10164 may access f->tool_bar_items. Make sure we update both
10165 variables within BLOCK_INPUT so no such event interrupts. */
10166 BLOCK_INPUT;
10167 f->tool_bar_items = new_tool_bar;
10168 f->n_tool_bar_items = new_n_tool_bar;
10169 w->update_mode_line = Qt;
10170 UNBLOCK_INPUT;
10171 }
10172
10173 UNGCPRO;
10174
10175 unbind_to (count, Qnil);
10176 set_buffer_internal_1 (prev);
10177 }
10178 }
10179 }
10180
10181
10182 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10183 F's desired tool-bar contents. F->tool_bar_items must have
10184 been set up previously by calling prepare_menu_bars. */
10185
10186 static void
10187 build_desired_tool_bar_string (f)
10188 struct frame *f;
10189 {
10190 int i, size, size_needed;
10191 struct gcpro gcpro1, gcpro2, gcpro3;
10192 Lisp_Object image, plist, props;
10193
10194 image = plist = props = Qnil;
10195 GCPRO3 (image, plist, props);
10196
10197 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10198 Otherwise, make a new string. */
10199
10200 /* The size of the string we might be able to reuse. */
10201 size = (STRINGP (f->desired_tool_bar_string)
10202 ? SCHARS (f->desired_tool_bar_string)
10203 : 0);
10204
10205 /* We need one space in the string for each image. */
10206 size_needed = f->n_tool_bar_items;
10207
10208 /* Reuse f->desired_tool_bar_string, if possible. */
10209 if (size < size_needed || NILP (f->desired_tool_bar_string))
10210 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10211 make_number (' '));
10212 else
10213 {
10214 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10215 Fremove_text_properties (make_number (0), make_number (size),
10216 props, f->desired_tool_bar_string);
10217 }
10218
10219 /* Put a `display' property on the string for the images to display,
10220 put a `menu_item' property on tool-bar items with a value that
10221 is the index of the item in F's tool-bar item vector. */
10222 for (i = 0; i < f->n_tool_bar_items; ++i)
10223 {
10224 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10225
10226 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10227 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10228 int hmargin, vmargin, relief, idx, end;
10229 extern Lisp_Object QCrelief, QCmargin, QCconversion;
10230
10231 /* If image is a vector, choose the image according to the
10232 button state. */
10233 image = PROP (TOOL_BAR_ITEM_IMAGES);
10234 if (VECTORP (image))
10235 {
10236 if (enabled_p)
10237 idx = (selected_p
10238 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10239 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10240 else
10241 idx = (selected_p
10242 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10243 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10244
10245 xassert (ASIZE (image) >= idx);
10246 image = AREF (image, idx);
10247 }
10248 else
10249 idx = -1;
10250
10251 /* Ignore invalid image specifications. */
10252 if (!valid_image_p (image))
10253 continue;
10254
10255 /* Display the tool-bar button pressed, or depressed. */
10256 plist = Fcopy_sequence (XCDR (image));
10257
10258 /* Compute margin and relief to draw. */
10259 relief = (tool_bar_button_relief >= 0
10260 ? tool_bar_button_relief
10261 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10262 hmargin = vmargin = relief;
10263
10264 if (INTEGERP (Vtool_bar_button_margin)
10265 && XINT (Vtool_bar_button_margin) > 0)
10266 {
10267 hmargin += XFASTINT (Vtool_bar_button_margin);
10268 vmargin += XFASTINT (Vtool_bar_button_margin);
10269 }
10270 else if (CONSP (Vtool_bar_button_margin))
10271 {
10272 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10273 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10274 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10275
10276 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10277 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10278 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10279 }
10280
10281 if (auto_raise_tool_bar_buttons_p)
10282 {
10283 /* Add a `:relief' property to the image spec if the item is
10284 selected. */
10285 if (selected_p)
10286 {
10287 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10288 hmargin -= relief;
10289 vmargin -= relief;
10290 }
10291 }
10292 else
10293 {
10294 /* If image is selected, display it pressed, i.e. with a
10295 negative relief. If it's not selected, display it with a
10296 raised relief. */
10297 plist = Fplist_put (plist, QCrelief,
10298 (selected_p
10299 ? make_number (-relief)
10300 : make_number (relief)));
10301 hmargin -= relief;
10302 vmargin -= relief;
10303 }
10304
10305 /* Put a margin around the image. */
10306 if (hmargin || vmargin)
10307 {
10308 if (hmargin == vmargin)
10309 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10310 else
10311 plist = Fplist_put (plist, QCmargin,
10312 Fcons (make_number (hmargin),
10313 make_number (vmargin)));
10314 }
10315
10316 /* If button is not enabled, and we don't have special images
10317 for the disabled state, make the image appear disabled by
10318 applying an appropriate algorithm to it. */
10319 if (!enabled_p && idx < 0)
10320 plist = Fplist_put (plist, QCconversion, Qdisabled);
10321
10322 /* Put a `display' text property on the string for the image to
10323 display. Put a `menu-item' property on the string that gives
10324 the start of this item's properties in the tool-bar items
10325 vector. */
10326 image = Fcons (Qimage, plist);
10327 props = list4 (Qdisplay, image,
10328 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10329
10330 /* Let the last image hide all remaining spaces in the tool bar
10331 string. The string can be longer than needed when we reuse a
10332 previous string. */
10333 if (i + 1 == f->n_tool_bar_items)
10334 end = SCHARS (f->desired_tool_bar_string);
10335 else
10336 end = i + 1;
10337 Fadd_text_properties (make_number (i), make_number (end),
10338 props, f->desired_tool_bar_string);
10339 #undef PROP
10340 }
10341
10342 UNGCPRO;
10343 }
10344
10345
10346 /* Display one line of the tool-bar of frame IT->f.
10347
10348 HEIGHT specifies the desired height of the tool-bar line.
10349 If the actual height of the glyph row is less than HEIGHT, the
10350 row's height is increased to HEIGHT, and the icons are centered
10351 vertically in the new height.
10352
10353 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10354 count a final empty row in case the tool-bar width exactly matches
10355 the window width.
10356 */
10357
10358 static void
10359 display_tool_bar_line (it, height)
10360 struct it *it;
10361 int height;
10362 {
10363 struct glyph_row *row = it->glyph_row;
10364 int max_x = it->last_visible_x;
10365 struct glyph *last;
10366
10367 prepare_desired_row (row);
10368 row->y = it->current_y;
10369
10370 /* Note that this isn't made use of if the face hasn't a box,
10371 so there's no need to check the face here. */
10372 it->start_of_box_run_p = 1;
10373
10374 while (it->current_x < max_x)
10375 {
10376 int x, n_glyphs_before, i, nglyphs;
10377 struct it it_before;
10378
10379 /* Get the next display element. */
10380 if (!get_next_display_element (it))
10381 {
10382 /* Don't count empty row if we are counting needed tool-bar lines. */
10383 if (height < 0 && !it->hpos)
10384 return;
10385 break;
10386 }
10387
10388 /* Produce glyphs. */
10389 n_glyphs_before = row->used[TEXT_AREA];
10390 it_before = *it;
10391
10392 PRODUCE_GLYPHS (it);
10393
10394 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10395 i = 0;
10396 x = it_before.current_x;
10397 while (i < nglyphs)
10398 {
10399 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10400
10401 if (x + glyph->pixel_width > max_x)
10402 {
10403 /* Glyph doesn't fit on line. Backtrack. */
10404 row->used[TEXT_AREA] = n_glyphs_before;
10405 *it = it_before;
10406 /* If this is the only glyph on this line, it will never fit on the
10407 toolbar, so skip it. But ensure there is at least one glyph,
10408 so we don't accidentally disable the tool-bar. */
10409 if (n_glyphs_before == 0
10410 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10411 break;
10412 goto out;
10413 }
10414
10415 ++it->hpos;
10416 x += glyph->pixel_width;
10417 ++i;
10418 }
10419
10420 /* Stop at line ends. */
10421 if (ITERATOR_AT_END_OF_LINE_P (it))
10422 break;
10423
10424 set_iterator_to_next (it, 1);
10425 }
10426
10427 out:;
10428
10429 row->displays_text_p = row->used[TEXT_AREA] != 0;
10430
10431 /* Use default face for the border below the tool bar.
10432
10433 FIXME: When auto-resize-tool-bars is grow-only, there is
10434 no additional border below the possibly empty tool-bar lines.
10435 So to make the extra empty lines look "normal", we have to
10436 use the tool-bar face for the border too. */
10437 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10438 it->face_id = DEFAULT_FACE_ID;
10439
10440 extend_face_to_end_of_line (it);
10441 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10442 last->right_box_line_p = 1;
10443 if (last == row->glyphs[TEXT_AREA])
10444 last->left_box_line_p = 1;
10445
10446 /* Make line the desired height and center it vertically. */
10447 if ((height -= it->max_ascent + it->max_descent) > 0)
10448 {
10449 /* Don't add more than one line height. */
10450 height %= FRAME_LINE_HEIGHT (it->f);
10451 it->max_ascent += height / 2;
10452 it->max_descent += (height + 1) / 2;
10453 }
10454
10455 compute_line_metrics (it);
10456
10457 /* If line is empty, make it occupy the rest of the tool-bar. */
10458 if (!row->displays_text_p)
10459 {
10460 row->height = row->phys_height = it->last_visible_y - row->y;
10461 row->visible_height = row->height;
10462 row->ascent = row->phys_ascent = 0;
10463 row->extra_line_spacing = 0;
10464 }
10465
10466 row->full_width_p = 1;
10467 row->continued_p = 0;
10468 row->truncated_on_left_p = 0;
10469 row->truncated_on_right_p = 0;
10470
10471 it->current_x = it->hpos = 0;
10472 it->current_y += row->height;
10473 ++it->vpos;
10474 ++it->glyph_row;
10475 }
10476
10477
10478 /* Max tool-bar height. */
10479
10480 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10481 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10482
10483 /* Value is the number of screen lines needed to make all tool-bar
10484 items of frame F visible. The number of actual rows needed is
10485 returned in *N_ROWS if non-NULL. */
10486
10487 static int
10488 tool_bar_lines_needed (f, n_rows)
10489 struct frame *f;
10490 int *n_rows;
10491 {
10492 struct window *w = XWINDOW (f->tool_bar_window);
10493 struct it it;
10494 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10495 the desired matrix, so use (unused) mode-line row as temporary row to
10496 avoid destroying the first tool-bar row. */
10497 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10498
10499 /* Initialize an iterator for iteration over
10500 F->desired_tool_bar_string in the tool-bar window of frame F. */
10501 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10502 it.first_visible_x = 0;
10503 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10504 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10505
10506 while (!ITERATOR_AT_END_P (&it))
10507 {
10508 clear_glyph_row (temp_row);
10509 it.glyph_row = temp_row;
10510 display_tool_bar_line (&it, -1);
10511 }
10512 clear_glyph_row (temp_row);
10513
10514 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10515 if (n_rows)
10516 *n_rows = it.vpos > 0 ? it.vpos : -1;
10517
10518 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10519 }
10520
10521
10522 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10523 0, 1, 0,
10524 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10525 (frame)
10526 Lisp_Object frame;
10527 {
10528 struct frame *f;
10529 struct window *w;
10530 int nlines = 0;
10531
10532 if (NILP (frame))
10533 frame = selected_frame;
10534 else
10535 CHECK_FRAME (frame);
10536 f = XFRAME (frame);
10537
10538 if (WINDOWP (f->tool_bar_window)
10539 || (w = XWINDOW (f->tool_bar_window),
10540 WINDOW_TOTAL_LINES (w) > 0))
10541 {
10542 update_tool_bar (f, 1);
10543 if (f->n_tool_bar_items)
10544 {
10545 build_desired_tool_bar_string (f);
10546 nlines = tool_bar_lines_needed (f, NULL);
10547 }
10548 }
10549
10550 return make_number (nlines);
10551 }
10552
10553
10554 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10555 height should be changed. */
10556
10557 static int
10558 redisplay_tool_bar (f)
10559 struct frame *f;
10560 {
10561 struct window *w;
10562 struct it it;
10563 struct glyph_row *row;
10564
10565 #if defined (USE_GTK) || defined (HAVE_NS)
10566 if (FRAME_EXTERNAL_TOOL_BAR (f))
10567 update_frame_tool_bar (f);
10568 return 0;
10569 #endif
10570
10571 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10572 do anything. This means you must start with tool-bar-lines
10573 non-zero to get the auto-sizing effect. Or in other words, you
10574 can turn off tool-bars by specifying tool-bar-lines zero. */
10575 if (!WINDOWP (f->tool_bar_window)
10576 || (w = XWINDOW (f->tool_bar_window),
10577 WINDOW_TOTAL_LINES (w) == 0))
10578 return 0;
10579
10580 /* Set up an iterator for the tool-bar window. */
10581 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10582 it.first_visible_x = 0;
10583 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10584 row = it.glyph_row;
10585
10586 /* Build a string that represents the contents of the tool-bar. */
10587 build_desired_tool_bar_string (f);
10588 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10589
10590 if (f->n_tool_bar_rows == 0)
10591 {
10592 int nlines;
10593
10594 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10595 nlines != WINDOW_TOTAL_LINES (w)))
10596 {
10597 extern Lisp_Object Qtool_bar_lines;
10598 Lisp_Object frame;
10599 int old_height = WINDOW_TOTAL_LINES (w);
10600
10601 XSETFRAME (frame, f);
10602 Fmodify_frame_parameters (frame,
10603 Fcons (Fcons (Qtool_bar_lines,
10604 make_number (nlines)),
10605 Qnil));
10606 if (WINDOW_TOTAL_LINES (w) != old_height)
10607 {
10608 clear_glyph_matrix (w->desired_matrix);
10609 fonts_changed_p = 1;
10610 return 1;
10611 }
10612 }
10613 }
10614
10615 /* Display as many lines as needed to display all tool-bar items. */
10616
10617 if (f->n_tool_bar_rows > 0)
10618 {
10619 int border, rows, height, extra;
10620
10621 if (INTEGERP (Vtool_bar_border))
10622 border = XINT (Vtool_bar_border);
10623 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10624 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10625 else if (EQ (Vtool_bar_border, Qborder_width))
10626 border = f->border_width;
10627 else
10628 border = 0;
10629 if (border < 0)
10630 border = 0;
10631
10632 rows = f->n_tool_bar_rows;
10633 height = max (1, (it.last_visible_y - border) / rows);
10634 extra = it.last_visible_y - border - height * rows;
10635
10636 while (it.current_y < it.last_visible_y)
10637 {
10638 int h = 0;
10639 if (extra > 0 && rows-- > 0)
10640 {
10641 h = (extra + rows - 1) / rows;
10642 extra -= h;
10643 }
10644 display_tool_bar_line (&it, height + h);
10645 }
10646 }
10647 else
10648 {
10649 while (it.current_y < it.last_visible_y)
10650 display_tool_bar_line (&it, 0);
10651 }
10652
10653 /* It doesn't make much sense to try scrolling in the tool-bar
10654 window, so don't do it. */
10655 w->desired_matrix->no_scrolling_p = 1;
10656 w->must_be_updated_p = 1;
10657
10658 if (!NILP (Vauto_resize_tool_bars))
10659 {
10660 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10661 int change_height_p = 0;
10662
10663 /* If we couldn't display everything, change the tool-bar's
10664 height if there is room for more. */
10665 if (IT_STRING_CHARPOS (it) < it.end_charpos
10666 && it.current_y < max_tool_bar_height)
10667 change_height_p = 1;
10668
10669 row = it.glyph_row - 1;
10670
10671 /* If there are blank lines at the end, except for a partially
10672 visible blank line at the end that is smaller than
10673 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10674 if (!row->displays_text_p
10675 && row->height >= FRAME_LINE_HEIGHT (f))
10676 change_height_p = 1;
10677
10678 /* If row displays tool-bar items, but is partially visible,
10679 change the tool-bar's height. */
10680 if (row->displays_text_p
10681 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10682 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10683 change_height_p = 1;
10684
10685 /* Resize windows as needed by changing the `tool-bar-lines'
10686 frame parameter. */
10687 if (change_height_p)
10688 {
10689 extern Lisp_Object Qtool_bar_lines;
10690 Lisp_Object frame;
10691 int old_height = WINDOW_TOTAL_LINES (w);
10692 int nrows;
10693 int nlines = tool_bar_lines_needed (f, &nrows);
10694
10695 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10696 && !f->minimize_tool_bar_window_p)
10697 ? (nlines > old_height)
10698 : (nlines != old_height));
10699 f->minimize_tool_bar_window_p = 0;
10700
10701 if (change_height_p)
10702 {
10703 XSETFRAME (frame, f);
10704 Fmodify_frame_parameters (frame,
10705 Fcons (Fcons (Qtool_bar_lines,
10706 make_number (nlines)),
10707 Qnil));
10708 if (WINDOW_TOTAL_LINES (w) != old_height)
10709 {
10710 clear_glyph_matrix (w->desired_matrix);
10711 f->n_tool_bar_rows = nrows;
10712 fonts_changed_p = 1;
10713 return 1;
10714 }
10715 }
10716 }
10717 }
10718
10719 f->minimize_tool_bar_window_p = 0;
10720 return 0;
10721 }
10722
10723
10724 /* Get information about the tool-bar item which is displayed in GLYPH
10725 on frame F. Return in *PROP_IDX the index where tool-bar item
10726 properties start in F->tool_bar_items. Value is zero if
10727 GLYPH doesn't display a tool-bar item. */
10728
10729 static int
10730 tool_bar_item_info (f, glyph, prop_idx)
10731 struct frame *f;
10732 struct glyph *glyph;
10733 int *prop_idx;
10734 {
10735 Lisp_Object prop;
10736 int success_p;
10737 int charpos;
10738
10739 /* This function can be called asynchronously, which means we must
10740 exclude any possibility that Fget_text_property signals an
10741 error. */
10742 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10743 charpos = max (0, charpos);
10744
10745 /* Get the text property `menu-item' at pos. The value of that
10746 property is the start index of this item's properties in
10747 F->tool_bar_items. */
10748 prop = Fget_text_property (make_number (charpos),
10749 Qmenu_item, f->current_tool_bar_string);
10750 if (INTEGERP (prop))
10751 {
10752 *prop_idx = XINT (prop);
10753 success_p = 1;
10754 }
10755 else
10756 success_p = 0;
10757
10758 return success_p;
10759 }
10760
10761 \f
10762 /* Get information about the tool-bar item at position X/Y on frame F.
10763 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10764 the current matrix of the tool-bar window of F, or NULL if not
10765 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10766 item in F->tool_bar_items. Value is
10767
10768 -1 if X/Y is not on a tool-bar item
10769 0 if X/Y is on the same item that was highlighted before.
10770 1 otherwise. */
10771
10772 static int
10773 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10774 struct frame *f;
10775 int x, y;
10776 struct glyph **glyph;
10777 int *hpos, *vpos, *prop_idx;
10778 {
10779 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10780 struct window *w = XWINDOW (f->tool_bar_window);
10781 int area;
10782
10783 /* Find the glyph under X/Y. */
10784 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10785 if (*glyph == NULL)
10786 return -1;
10787
10788 /* Get the start of this tool-bar item's properties in
10789 f->tool_bar_items. */
10790 if (!tool_bar_item_info (f, *glyph, prop_idx))
10791 return -1;
10792
10793 /* Is mouse on the highlighted item? */
10794 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10795 && *vpos >= dpyinfo->mouse_face_beg_row
10796 && *vpos <= dpyinfo->mouse_face_end_row
10797 && (*vpos > dpyinfo->mouse_face_beg_row
10798 || *hpos >= dpyinfo->mouse_face_beg_col)
10799 && (*vpos < dpyinfo->mouse_face_end_row
10800 || *hpos < dpyinfo->mouse_face_end_col
10801 || dpyinfo->mouse_face_past_end))
10802 return 0;
10803
10804 return 1;
10805 }
10806
10807
10808 /* EXPORT:
10809 Handle mouse button event on the tool-bar of frame F, at
10810 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10811 0 for button release. MODIFIERS is event modifiers for button
10812 release. */
10813
10814 void
10815 handle_tool_bar_click (f, x, y, down_p, modifiers)
10816 struct frame *f;
10817 int x, y, down_p;
10818 unsigned int modifiers;
10819 {
10820 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10821 struct window *w = XWINDOW (f->tool_bar_window);
10822 int hpos, vpos, prop_idx;
10823 struct glyph *glyph;
10824 Lisp_Object enabled_p;
10825
10826 /* If not on the highlighted tool-bar item, return. */
10827 frame_to_window_pixel_xy (w, &x, &y);
10828 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10829 return;
10830
10831 /* If item is disabled, do nothing. */
10832 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10833 if (NILP (enabled_p))
10834 return;
10835
10836 if (down_p)
10837 {
10838 /* Show item in pressed state. */
10839 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10840 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10841 last_tool_bar_item = prop_idx;
10842 }
10843 else
10844 {
10845 Lisp_Object key, frame;
10846 struct input_event event;
10847 EVENT_INIT (event);
10848
10849 /* Show item in released state. */
10850 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10851 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10852
10853 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10854
10855 XSETFRAME (frame, f);
10856 event.kind = TOOL_BAR_EVENT;
10857 event.frame_or_window = frame;
10858 event.arg = frame;
10859 kbd_buffer_store_event (&event);
10860
10861 event.kind = TOOL_BAR_EVENT;
10862 event.frame_or_window = frame;
10863 event.arg = key;
10864 event.modifiers = modifiers;
10865 kbd_buffer_store_event (&event);
10866 last_tool_bar_item = -1;
10867 }
10868 }
10869
10870
10871 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10872 tool-bar window-relative coordinates X/Y. Called from
10873 note_mouse_highlight. */
10874
10875 static void
10876 note_tool_bar_highlight (f, x, y)
10877 struct frame *f;
10878 int x, y;
10879 {
10880 Lisp_Object window = f->tool_bar_window;
10881 struct window *w = XWINDOW (window);
10882 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10883 int hpos, vpos;
10884 struct glyph *glyph;
10885 struct glyph_row *row;
10886 int i;
10887 Lisp_Object enabled_p;
10888 int prop_idx;
10889 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10890 int mouse_down_p, rc;
10891
10892 /* Function note_mouse_highlight is called with negative x(y
10893 values when mouse moves outside of the frame. */
10894 if (x <= 0 || y <= 0)
10895 {
10896 clear_mouse_face (dpyinfo);
10897 return;
10898 }
10899
10900 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10901 if (rc < 0)
10902 {
10903 /* Not on tool-bar item. */
10904 clear_mouse_face (dpyinfo);
10905 return;
10906 }
10907 else if (rc == 0)
10908 /* On same tool-bar item as before. */
10909 goto set_help_echo;
10910
10911 clear_mouse_face (dpyinfo);
10912
10913 /* Mouse is down, but on different tool-bar item? */
10914 mouse_down_p = (dpyinfo->grabbed
10915 && f == last_mouse_frame
10916 && FRAME_LIVE_P (f));
10917 if (mouse_down_p
10918 && last_tool_bar_item != prop_idx)
10919 return;
10920
10921 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10922 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10923
10924 /* If tool-bar item is not enabled, don't highlight it. */
10925 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10926 if (!NILP (enabled_p))
10927 {
10928 /* Compute the x-position of the glyph. In front and past the
10929 image is a space. We include this in the highlighted area. */
10930 row = MATRIX_ROW (w->current_matrix, vpos);
10931 for (i = x = 0; i < hpos; ++i)
10932 x += row->glyphs[TEXT_AREA][i].pixel_width;
10933
10934 /* Record this as the current active region. */
10935 dpyinfo->mouse_face_beg_col = hpos;
10936 dpyinfo->mouse_face_beg_row = vpos;
10937 dpyinfo->mouse_face_beg_x = x;
10938 dpyinfo->mouse_face_beg_y = row->y;
10939 dpyinfo->mouse_face_past_end = 0;
10940
10941 dpyinfo->mouse_face_end_col = hpos + 1;
10942 dpyinfo->mouse_face_end_row = vpos;
10943 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10944 dpyinfo->mouse_face_end_y = row->y;
10945 dpyinfo->mouse_face_window = window;
10946 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10947
10948 /* Display it as active. */
10949 show_mouse_face (dpyinfo, draw);
10950 dpyinfo->mouse_face_image_state = draw;
10951 }
10952
10953 set_help_echo:
10954
10955 /* Set help_echo_string to a help string to display for this tool-bar item.
10956 XTread_socket does the rest. */
10957 help_echo_object = help_echo_window = Qnil;
10958 help_echo_pos = -1;
10959 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10960 if (NILP (help_echo_string))
10961 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10962 }
10963
10964 #endif /* HAVE_WINDOW_SYSTEM */
10965
10966
10967 \f
10968 /************************************************************************
10969 Horizontal scrolling
10970 ************************************************************************/
10971
10972 static int hscroll_window_tree P_ ((Lisp_Object));
10973 static int hscroll_windows P_ ((Lisp_Object));
10974
10975 /* For all leaf windows in the window tree rooted at WINDOW, set their
10976 hscroll value so that PT is (i) visible in the window, and (ii) so
10977 that it is not within a certain margin at the window's left and
10978 right border. Value is non-zero if any window's hscroll has been
10979 changed. */
10980
10981 static int
10982 hscroll_window_tree (window)
10983 Lisp_Object window;
10984 {
10985 int hscrolled_p = 0;
10986 int hscroll_relative_p = FLOATP (Vhscroll_step);
10987 int hscroll_step_abs = 0;
10988 double hscroll_step_rel = 0;
10989
10990 if (hscroll_relative_p)
10991 {
10992 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10993 if (hscroll_step_rel < 0)
10994 {
10995 hscroll_relative_p = 0;
10996 hscroll_step_abs = 0;
10997 }
10998 }
10999 else if (INTEGERP (Vhscroll_step))
11000 {
11001 hscroll_step_abs = XINT (Vhscroll_step);
11002 if (hscroll_step_abs < 0)
11003 hscroll_step_abs = 0;
11004 }
11005 else
11006 hscroll_step_abs = 0;
11007
11008 while (WINDOWP (window))
11009 {
11010 struct window *w = XWINDOW (window);
11011
11012 if (WINDOWP (w->hchild))
11013 hscrolled_p |= hscroll_window_tree (w->hchild);
11014 else if (WINDOWP (w->vchild))
11015 hscrolled_p |= hscroll_window_tree (w->vchild);
11016 else if (w->cursor.vpos >= 0)
11017 {
11018 int h_margin;
11019 int text_area_width;
11020 struct glyph_row *current_cursor_row
11021 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11022 struct glyph_row *desired_cursor_row
11023 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11024 struct glyph_row *cursor_row
11025 = (desired_cursor_row->enabled_p
11026 ? desired_cursor_row
11027 : current_cursor_row);
11028
11029 text_area_width = window_box_width (w, TEXT_AREA);
11030
11031 /* Scroll when cursor is inside this scroll margin. */
11032 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11033
11034 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11035 && ((XFASTINT (w->hscroll)
11036 && w->cursor.x <= h_margin)
11037 || (cursor_row->enabled_p
11038 && cursor_row->truncated_on_right_p
11039 && (w->cursor.x >= text_area_width - h_margin))))
11040 {
11041 struct it it;
11042 int hscroll;
11043 struct buffer *saved_current_buffer;
11044 int pt;
11045 int wanted_x;
11046
11047 /* Find point in a display of infinite width. */
11048 saved_current_buffer = current_buffer;
11049 current_buffer = XBUFFER (w->buffer);
11050
11051 if (w == XWINDOW (selected_window))
11052 pt = BUF_PT (current_buffer);
11053 else
11054 {
11055 pt = marker_position (w->pointm);
11056 pt = max (BEGV, pt);
11057 pt = min (ZV, pt);
11058 }
11059
11060 /* Move iterator to pt starting at cursor_row->start in
11061 a line with infinite width. */
11062 init_to_row_start (&it, w, cursor_row);
11063 it.last_visible_x = INFINITY;
11064 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11065 current_buffer = saved_current_buffer;
11066
11067 /* Position cursor in window. */
11068 if (!hscroll_relative_p && hscroll_step_abs == 0)
11069 hscroll = max (0, (it.current_x
11070 - (ITERATOR_AT_END_OF_LINE_P (&it)
11071 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11072 : (text_area_width / 2))))
11073 / FRAME_COLUMN_WIDTH (it.f);
11074 else if (w->cursor.x >= text_area_width - h_margin)
11075 {
11076 if (hscroll_relative_p)
11077 wanted_x = text_area_width * (1 - hscroll_step_rel)
11078 - h_margin;
11079 else
11080 wanted_x = text_area_width
11081 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11082 - h_margin;
11083 hscroll
11084 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11085 }
11086 else
11087 {
11088 if (hscroll_relative_p)
11089 wanted_x = text_area_width * hscroll_step_rel
11090 + h_margin;
11091 else
11092 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11093 + h_margin;
11094 hscroll
11095 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11096 }
11097 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11098
11099 /* Don't call Fset_window_hscroll if value hasn't
11100 changed because it will prevent redisplay
11101 optimizations. */
11102 if (XFASTINT (w->hscroll) != hscroll)
11103 {
11104 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11105 w->hscroll = make_number (hscroll);
11106 hscrolled_p = 1;
11107 }
11108 }
11109 }
11110
11111 window = w->next;
11112 }
11113
11114 /* Value is non-zero if hscroll of any leaf window has been changed. */
11115 return hscrolled_p;
11116 }
11117
11118
11119 /* Set hscroll so that cursor is visible and not inside horizontal
11120 scroll margins for all windows in the tree rooted at WINDOW. See
11121 also hscroll_window_tree above. Value is non-zero if any window's
11122 hscroll has been changed. If it has, desired matrices on the frame
11123 of WINDOW are cleared. */
11124
11125 static int
11126 hscroll_windows (window)
11127 Lisp_Object window;
11128 {
11129 int hscrolled_p = hscroll_window_tree (window);
11130 if (hscrolled_p)
11131 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11132 return hscrolled_p;
11133 }
11134
11135
11136 \f
11137 /************************************************************************
11138 Redisplay
11139 ************************************************************************/
11140
11141 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11142 to a non-zero value. This is sometimes handy to have in a debugger
11143 session. */
11144
11145 #if GLYPH_DEBUG
11146
11147 /* First and last unchanged row for try_window_id. */
11148
11149 int debug_first_unchanged_at_end_vpos;
11150 int debug_last_unchanged_at_beg_vpos;
11151
11152 /* Delta vpos and y. */
11153
11154 int debug_dvpos, debug_dy;
11155
11156 /* Delta in characters and bytes for try_window_id. */
11157
11158 int debug_delta, debug_delta_bytes;
11159
11160 /* Values of window_end_pos and window_end_vpos at the end of
11161 try_window_id. */
11162
11163 EMACS_INT debug_end_pos, debug_end_vpos;
11164
11165 /* Append a string to W->desired_matrix->method. FMT is a printf
11166 format string. A1...A9 are a supplement for a variable-length
11167 argument list. If trace_redisplay_p is non-zero also printf the
11168 resulting string to stderr. */
11169
11170 static void
11171 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11172 struct window *w;
11173 char *fmt;
11174 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11175 {
11176 char buffer[512];
11177 char *method = w->desired_matrix->method;
11178 int len = strlen (method);
11179 int size = sizeof w->desired_matrix->method;
11180 int remaining = size - len - 1;
11181
11182 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11183 if (len && remaining)
11184 {
11185 method[len] = '|';
11186 --remaining, ++len;
11187 }
11188
11189 strncpy (method + len, buffer, remaining);
11190
11191 if (trace_redisplay_p)
11192 fprintf (stderr, "%p (%s): %s\n",
11193 w,
11194 ((BUFFERP (w->buffer)
11195 && STRINGP (XBUFFER (w->buffer)->name))
11196 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11197 : "no buffer"),
11198 buffer);
11199 }
11200
11201 #endif /* GLYPH_DEBUG */
11202
11203
11204 /* Value is non-zero if all changes in window W, which displays
11205 current_buffer, are in the text between START and END. START is a
11206 buffer position, END is given as a distance from Z. Used in
11207 redisplay_internal for display optimization. */
11208
11209 static INLINE int
11210 text_outside_line_unchanged_p (w, start, end)
11211 struct window *w;
11212 int start, end;
11213 {
11214 int unchanged_p = 1;
11215
11216 /* If text or overlays have changed, see where. */
11217 if (XFASTINT (w->last_modified) < MODIFF
11218 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11219 {
11220 /* Gap in the line? */
11221 if (GPT < start || Z - GPT < end)
11222 unchanged_p = 0;
11223
11224 /* Changes start in front of the line, or end after it? */
11225 if (unchanged_p
11226 && (BEG_UNCHANGED < start - 1
11227 || END_UNCHANGED < end))
11228 unchanged_p = 0;
11229
11230 /* If selective display, can't optimize if changes start at the
11231 beginning of the line. */
11232 if (unchanged_p
11233 && INTEGERP (current_buffer->selective_display)
11234 && XINT (current_buffer->selective_display) > 0
11235 && (BEG_UNCHANGED < start || GPT <= start))
11236 unchanged_p = 0;
11237
11238 /* If there are overlays at the start or end of the line, these
11239 may have overlay strings with newlines in them. A change at
11240 START, for instance, may actually concern the display of such
11241 overlay strings as well, and they are displayed on different
11242 lines. So, quickly rule out this case. (For the future, it
11243 might be desirable to implement something more telling than
11244 just BEG/END_UNCHANGED.) */
11245 if (unchanged_p)
11246 {
11247 if (BEG + BEG_UNCHANGED == start
11248 && overlay_touches_p (start))
11249 unchanged_p = 0;
11250 if (END_UNCHANGED == end
11251 && overlay_touches_p (Z - end))
11252 unchanged_p = 0;
11253 }
11254
11255 /* Under bidi reordering, adding or deleting a character in the
11256 beginning of a paragraph, before the first strong directional
11257 character, can change the base direction of the paragraph (unless
11258 the buffer specifies a fixed paragraph direction), which will
11259 require to redisplay the whole paragraph. It might be worthwhile
11260 to find the paragraph limits and widen the range of redisplayed
11261 lines to that, but for now just give up this optimization. */
11262 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11263 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11264 unchanged_p = 0;
11265 }
11266
11267 return unchanged_p;
11268 }
11269
11270
11271 /* Do a frame update, taking possible shortcuts into account. This is
11272 the main external entry point for redisplay.
11273
11274 If the last redisplay displayed an echo area message and that message
11275 is no longer requested, we clear the echo area or bring back the
11276 mini-buffer if that is in use. */
11277
11278 void
11279 redisplay ()
11280 {
11281 redisplay_internal (0);
11282 }
11283
11284
11285 static Lisp_Object
11286 overlay_arrow_string_or_property (var)
11287 Lisp_Object var;
11288 {
11289 Lisp_Object val;
11290
11291 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11292 return val;
11293
11294 return Voverlay_arrow_string;
11295 }
11296
11297 /* Return 1 if there are any overlay-arrows in current_buffer. */
11298 static int
11299 overlay_arrow_in_current_buffer_p ()
11300 {
11301 Lisp_Object vlist;
11302
11303 for (vlist = Voverlay_arrow_variable_list;
11304 CONSP (vlist);
11305 vlist = XCDR (vlist))
11306 {
11307 Lisp_Object var = XCAR (vlist);
11308 Lisp_Object val;
11309
11310 if (!SYMBOLP (var))
11311 continue;
11312 val = find_symbol_value (var);
11313 if (MARKERP (val)
11314 && current_buffer == XMARKER (val)->buffer)
11315 return 1;
11316 }
11317 return 0;
11318 }
11319
11320
11321 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11322 has changed. */
11323
11324 static int
11325 overlay_arrows_changed_p ()
11326 {
11327 Lisp_Object vlist;
11328
11329 for (vlist = Voverlay_arrow_variable_list;
11330 CONSP (vlist);
11331 vlist = XCDR (vlist))
11332 {
11333 Lisp_Object var = XCAR (vlist);
11334 Lisp_Object val, pstr;
11335
11336 if (!SYMBOLP (var))
11337 continue;
11338 val = find_symbol_value (var);
11339 if (!MARKERP (val))
11340 continue;
11341 if (! EQ (COERCE_MARKER (val),
11342 Fget (var, Qlast_arrow_position))
11343 || ! (pstr = overlay_arrow_string_or_property (var),
11344 EQ (pstr, Fget (var, Qlast_arrow_string))))
11345 return 1;
11346 }
11347 return 0;
11348 }
11349
11350 /* Mark overlay arrows to be updated on next redisplay. */
11351
11352 static void
11353 update_overlay_arrows (up_to_date)
11354 int up_to_date;
11355 {
11356 Lisp_Object vlist;
11357
11358 for (vlist = Voverlay_arrow_variable_list;
11359 CONSP (vlist);
11360 vlist = XCDR (vlist))
11361 {
11362 Lisp_Object var = XCAR (vlist);
11363
11364 if (!SYMBOLP (var))
11365 continue;
11366
11367 if (up_to_date > 0)
11368 {
11369 Lisp_Object val = find_symbol_value (var);
11370 Fput (var, Qlast_arrow_position,
11371 COERCE_MARKER (val));
11372 Fput (var, Qlast_arrow_string,
11373 overlay_arrow_string_or_property (var));
11374 }
11375 else if (up_to_date < 0
11376 || !NILP (Fget (var, Qlast_arrow_position)))
11377 {
11378 Fput (var, Qlast_arrow_position, Qt);
11379 Fput (var, Qlast_arrow_string, Qt);
11380 }
11381 }
11382 }
11383
11384
11385 /* Return overlay arrow string to display at row.
11386 Return integer (bitmap number) for arrow bitmap in left fringe.
11387 Return nil if no overlay arrow. */
11388
11389 static Lisp_Object
11390 overlay_arrow_at_row (it, row)
11391 struct it *it;
11392 struct glyph_row *row;
11393 {
11394 Lisp_Object vlist;
11395
11396 for (vlist = Voverlay_arrow_variable_list;
11397 CONSP (vlist);
11398 vlist = XCDR (vlist))
11399 {
11400 Lisp_Object var = XCAR (vlist);
11401 Lisp_Object val;
11402
11403 if (!SYMBOLP (var))
11404 continue;
11405
11406 val = find_symbol_value (var);
11407
11408 if (MARKERP (val)
11409 && current_buffer == XMARKER (val)->buffer
11410 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11411 {
11412 if (FRAME_WINDOW_P (it->f)
11413 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11414 {
11415 #ifdef HAVE_WINDOW_SYSTEM
11416 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11417 {
11418 int fringe_bitmap;
11419 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11420 return make_number (fringe_bitmap);
11421 }
11422 #endif
11423 return make_number (-1); /* Use default arrow bitmap */
11424 }
11425 return overlay_arrow_string_or_property (var);
11426 }
11427 }
11428
11429 return Qnil;
11430 }
11431
11432 /* Return 1 if point moved out of or into a composition. Otherwise
11433 return 0. PREV_BUF and PREV_PT are the last point buffer and
11434 position. BUF and PT are the current point buffer and position. */
11435
11436 int
11437 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11438 struct buffer *prev_buf, *buf;
11439 int prev_pt, pt;
11440 {
11441 EMACS_INT start, end;
11442 Lisp_Object prop;
11443 Lisp_Object buffer;
11444
11445 XSETBUFFER (buffer, buf);
11446 /* Check a composition at the last point if point moved within the
11447 same buffer. */
11448 if (prev_buf == buf)
11449 {
11450 if (prev_pt == pt)
11451 /* Point didn't move. */
11452 return 0;
11453
11454 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11455 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11456 && COMPOSITION_VALID_P (start, end, prop)
11457 && start < prev_pt && end > prev_pt)
11458 /* The last point was within the composition. Return 1 iff
11459 point moved out of the composition. */
11460 return (pt <= start || pt >= end);
11461 }
11462
11463 /* Check a composition at the current point. */
11464 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11465 && find_composition (pt, -1, &start, &end, &prop, buffer)
11466 && COMPOSITION_VALID_P (start, end, prop)
11467 && start < pt && end > pt);
11468 }
11469
11470
11471 /* Reconsider the setting of B->clip_changed which is displayed
11472 in window W. */
11473
11474 static INLINE void
11475 reconsider_clip_changes (w, b)
11476 struct window *w;
11477 struct buffer *b;
11478 {
11479 if (b->clip_changed
11480 && !NILP (w->window_end_valid)
11481 && w->current_matrix->buffer == b
11482 && w->current_matrix->zv == BUF_ZV (b)
11483 && w->current_matrix->begv == BUF_BEGV (b))
11484 b->clip_changed = 0;
11485
11486 /* If display wasn't paused, and W is not a tool bar window, see if
11487 point has been moved into or out of a composition. In that case,
11488 we set b->clip_changed to 1 to force updating the screen. If
11489 b->clip_changed has already been set to 1, we can skip this
11490 check. */
11491 if (!b->clip_changed
11492 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11493 {
11494 int pt;
11495
11496 if (w == XWINDOW (selected_window))
11497 pt = BUF_PT (current_buffer);
11498 else
11499 pt = marker_position (w->pointm);
11500
11501 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11502 || pt != XINT (w->last_point))
11503 && check_point_in_composition (w->current_matrix->buffer,
11504 XINT (w->last_point),
11505 XBUFFER (w->buffer), pt))
11506 b->clip_changed = 1;
11507 }
11508 }
11509 \f
11510
11511 /* Select FRAME to forward the values of frame-local variables into C
11512 variables so that the redisplay routines can access those values
11513 directly. */
11514
11515 static void
11516 select_frame_for_redisplay (frame)
11517 Lisp_Object frame;
11518 {
11519 Lisp_Object tail, symbol, val;
11520 Lisp_Object old = selected_frame;
11521 struct Lisp_Symbol *sym;
11522
11523 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11524
11525 selected_frame = frame;
11526
11527 do
11528 {
11529 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11530 if (CONSP (XCAR (tail))
11531 && (symbol = XCAR (XCAR (tail)),
11532 SYMBOLP (symbol))
11533 && (sym = indirect_variable (XSYMBOL (symbol)),
11534 val = sym->value,
11535 (BUFFER_LOCAL_VALUEP (val)))
11536 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11537 /* Use find_symbol_value rather than Fsymbol_value
11538 to avoid an error if it is void. */
11539 find_symbol_value (symbol);
11540 } while (!EQ (frame, old) && (frame = old, 1));
11541 }
11542
11543
11544 #define STOP_POLLING \
11545 do { if (! polling_stopped_here) stop_polling (); \
11546 polling_stopped_here = 1; } while (0)
11547
11548 #define RESUME_POLLING \
11549 do { if (polling_stopped_here) start_polling (); \
11550 polling_stopped_here = 0; } while (0)
11551
11552
11553 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11554 response to any user action; therefore, we should preserve the echo
11555 area. (Actually, our caller does that job.) Perhaps in the future
11556 avoid recentering windows if it is not necessary; currently that
11557 causes some problems. */
11558
11559 static void
11560 redisplay_internal (preserve_echo_area)
11561 int preserve_echo_area;
11562 {
11563 struct window *w = XWINDOW (selected_window);
11564 struct frame *f;
11565 int pause;
11566 int must_finish = 0;
11567 struct text_pos tlbufpos, tlendpos;
11568 int number_of_visible_frames;
11569 int count, count1;
11570 struct frame *sf;
11571 int polling_stopped_here = 0;
11572 Lisp_Object old_frame = selected_frame;
11573
11574 /* Non-zero means redisplay has to consider all windows on all
11575 frames. Zero means, only selected_window is considered. */
11576 int consider_all_windows_p;
11577
11578 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11579
11580 /* No redisplay if running in batch mode or frame is not yet fully
11581 initialized, or redisplay is explicitly turned off by setting
11582 Vinhibit_redisplay. */
11583 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11584 || !NILP (Vinhibit_redisplay))
11585 return;
11586
11587 /* Don't examine these until after testing Vinhibit_redisplay.
11588 When Emacs is shutting down, perhaps because its connection to
11589 X has dropped, we should not look at them at all. */
11590 f = XFRAME (w->frame);
11591 sf = SELECTED_FRAME ();
11592
11593 if (!f->glyphs_initialized_p)
11594 return;
11595
11596 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11597 if (popup_activated ())
11598 return;
11599 #endif
11600
11601 /* I don't think this happens but let's be paranoid. */
11602 if (redisplaying_p)
11603 return;
11604
11605 /* Record a function that resets redisplaying_p to its old value
11606 when we leave this function. */
11607 count = SPECPDL_INDEX ();
11608 record_unwind_protect (unwind_redisplay,
11609 Fcons (make_number (redisplaying_p), selected_frame));
11610 ++redisplaying_p;
11611 specbind (Qinhibit_free_realized_faces, Qnil);
11612
11613 {
11614 Lisp_Object tail, frame;
11615
11616 FOR_EACH_FRAME (tail, frame)
11617 {
11618 struct frame *f = XFRAME (frame);
11619 f->already_hscrolled_p = 0;
11620 }
11621 }
11622
11623 retry:
11624 if (!EQ (old_frame, selected_frame)
11625 && FRAME_LIVE_P (XFRAME (old_frame)))
11626 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11627 selected_frame and selected_window to be temporarily out-of-sync so
11628 when we come back here via `goto retry', we need to resync because we
11629 may need to run Elisp code (via prepare_menu_bars). */
11630 select_frame_for_redisplay (old_frame);
11631
11632 pause = 0;
11633 reconsider_clip_changes (w, current_buffer);
11634 last_escape_glyph_frame = NULL;
11635 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11636
11637 /* If new fonts have been loaded that make a glyph matrix adjustment
11638 necessary, do it. */
11639 if (fonts_changed_p)
11640 {
11641 adjust_glyphs (NULL);
11642 ++windows_or_buffers_changed;
11643 fonts_changed_p = 0;
11644 }
11645
11646 /* If face_change_count is non-zero, init_iterator will free all
11647 realized faces, which includes the faces referenced from current
11648 matrices. So, we can't reuse current matrices in this case. */
11649 if (face_change_count)
11650 ++windows_or_buffers_changed;
11651
11652 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11653 && FRAME_TTY (sf)->previous_frame != sf)
11654 {
11655 /* Since frames on a single ASCII terminal share the same
11656 display area, displaying a different frame means redisplay
11657 the whole thing. */
11658 windows_or_buffers_changed++;
11659 SET_FRAME_GARBAGED (sf);
11660 #ifndef DOS_NT
11661 set_tty_color_mode (FRAME_TTY (sf), sf);
11662 #endif
11663 FRAME_TTY (sf)->previous_frame = sf;
11664 }
11665
11666 /* Set the visible flags for all frames. Do this before checking
11667 for resized or garbaged frames; they want to know if their frames
11668 are visible. See the comment in frame.h for
11669 FRAME_SAMPLE_VISIBILITY. */
11670 {
11671 Lisp_Object tail, frame;
11672
11673 number_of_visible_frames = 0;
11674
11675 FOR_EACH_FRAME (tail, frame)
11676 {
11677 struct frame *f = XFRAME (frame);
11678
11679 FRAME_SAMPLE_VISIBILITY (f);
11680 if (FRAME_VISIBLE_P (f))
11681 ++number_of_visible_frames;
11682 clear_desired_matrices (f);
11683 }
11684 }
11685
11686 /* Notice any pending interrupt request to change frame size. */
11687 do_pending_window_change (1);
11688
11689 /* Clear frames marked as garbaged. */
11690 if (frame_garbaged)
11691 clear_garbaged_frames ();
11692
11693 /* Build menubar and tool-bar items. */
11694 if (NILP (Vmemory_full))
11695 prepare_menu_bars ();
11696
11697 if (windows_or_buffers_changed)
11698 update_mode_lines++;
11699
11700 /* Detect case that we need to write or remove a star in the mode line. */
11701 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11702 {
11703 w->update_mode_line = Qt;
11704 if (buffer_shared > 1)
11705 update_mode_lines++;
11706 }
11707
11708 /* Avoid invocation of point motion hooks by `current_column' below. */
11709 count1 = SPECPDL_INDEX ();
11710 specbind (Qinhibit_point_motion_hooks, Qt);
11711
11712 /* If %c is in the mode line, update it if needed. */
11713 if (!NILP (w->column_number_displayed)
11714 /* This alternative quickly identifies a common case
11715 where no change is needed. */
11716 && !(PT == XFASTINT (w->last_point)
11717 && XFASTINT (w->last_modified) >= MODIFF
11718 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11719 && (XFASTINT (w->column_number_displayed)
11720 != (int) current_column ())) /* iftc */
11721 w->update_mode_line = Qt;
11722
11723 unbind_to (count1, Qnil);
11724
11725 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11726
11727 /* The variable buffer_shared is set in redisplay_window and
11728 indicates that we redisplay a buffer in different windows. See
11729 there. */
11730 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11731 || cursor_type_changed);
11732
11733 /* If specs for an arrow have changed, do thorough redisplay
11734 to ensure we remove any arrow that should no longer exist. */
11735 if (overlay_arrows_changed_p ())
11736 consider_all_windows_p = windows_or_buffers_changed = 1;
11737
11738 /* Normally the message* functions will have already displayed and
11739 updated the echo area, but the frame may have been trashed, or
11740 the update may have been preempted, so display the echo area
11741 again here. Checking message_cleared_p captures the case that
11742 the echo area should be cleared. */
11743 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11744 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11745 || (message_cleared_p
11746 && minibuf_level == 0
11747 /* If the mini-window is currently selected, this means the
11748 echo-area doesn't show through. */
11749 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11750 {
11751 int window_height_changed_p = echo_area_display (0);
11752 must_finish = 1;
11753
11754 /* If we don't display the current message, don't clear the
11755 message_cleared_p flag, because, if we did, we wouldn't clear
11756 the echo area in the next redisplay which doesn't preserve
11757 the echo area. */
11758 if (!display_last_displayed_message_p)
11759 message_cleared_p = 0;
11760
11761 if (fonts_changed_p)
11762 goto retry;
11763 else if (window_height_changed_p)
11764 {
11765 consider_all_windows_p = 1;
11766 ++update_mode_lines;
11767 ++windows_or_buffers_changed;
11768
11769 /* If window configuration was changed, frames may have been
11770 marked garbaged. Clear them or we will experience
11771 surprises wrt scrolling. */
11772 if (frame_garbaged)
11773 clear_garbaged_frames ();
11774 }
11775 }
11776 else if (EQ (selected_window, minibuf_window)
11777 && (current_buffer->clip_changed
11778 || XFASTINT (w->last_modified) < MODIFF
11779 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11780 && resize_mini_window (w, 0))
11781 {
11782 /* Resized active mini-window to fit the size of what it is
11783 showing if its contents might have changed. */
11784 must_finish = 1;
11785 /* FIXME: this causes all frames to be updated, which seems unnecessary
11786 since only the current frame needs to be considered. This function needs
11787 to be rewritten with two variables, consider_all_windows and
11788 consider_all_frames. */
11789 consider_all_windows_p = 1;
11790 ++windows_or_buffers_changed;
11791 ++update_mode_lines;
11792
11793 /* If window configuration was changed, frames may have been
11794 marked garbaged. Clear them or we will experience
11795 surprises wrt scrolling. */
11796 if (frame_garbaged)
11797 clear_garbaged_frames ();
11798 }
11799
11800
11801 /* If showing the region, and mark has changed, we must redisplay
11802 the whole window. The assignment to this_line_start_pos prevents
11803 the optimization directly below this if-statement. */
11804 if (((!NILP (Vtransient_mark_mode)
11805 && !NILP (XBUFFER (w->buffer)->mark_active))
11806 != !NILP (w->region_showing))
11807 || (!NILP (w->region_showing)
11808 && !EQ (w->region_showing,
11809 Fmarker_position (XBUFFER (w->buffer)->mark))))
11810 CHARPOS (this_line_start_pos) = 0;
11811
11812 /* Optimize the case that only the line containing the cursor in the
11813 selected window has changed. Variables starting with this_ are
11814 set in display_line and record information about the line
11815 containing the cursor. */
11816 tlbufpos = this_line_start_pos;
11817 tlendpos = this_line_end_pos;
11818 if (!consider_all_windows_p
11819 && CHARPOS (tlbufpos) > 0
11820 && NILP (w->update_mode_line)
11821 && !current_buffer->clip_changed
11822 && !current_buffer->prevent_redisplay_optimizations_p
11823 && FRAME_VISIBLE_P (XFRAME (w->frame))
11824 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11825 /* Make sure recorded data applies to current buffer, etc. */
11826 && this_line_buffer == current_buffer
11827 && current_buffer == XBUFFER (w->buffer)
11828 && NILP (w->force_start)
11829 && NILP (w->optional_new_start)
11830 /* Point must be on the line that we have info recorded about. */
11831 && PT >= CHARPOS (tlbufpos)
11832 && PT <= Z - CHARPOS (tlendpos)
11833 /* All text outside that line, including its final newline,
11834 must be unchanged. */
11835 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11836 CHARPOS (tlendpos)))
11837 {
11838 if (CHARPOS (tlbufpos) > BEGV
11839 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11840 && (CHARPOS (tlbufpos) == ZV
11841 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11842 /* Former continuation line has disappeared by becoming empty. */
11843 goto cancel;
11844 else if (XFASTINT (w->last_modified) < MODIFF
11845 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11846 || MINI_WINDOW_P (w))
11847 {
11848 /* We have to handle the case of continuation around a
11849 wide-column character (see the comment in indent.c around
11850 line 1340).
11851
11852 For instance, in the following case:
11853
11854 -------- Insert --------
11855 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11856 J_I_ ==> J_I_ `^^' are cursors.
11857 ^^ ^^
11858 -------- --------
11859
11860 As we have to redraw the line above, we cannot use this
11861 optimization. */
11862
11863 struct it it;
11864 int line_height_before = this_line_pixel_height;
11865
11866 /* Note that start_display will handle the case that the
11867 line starting at tlbufpos is a continuation line. */
11868 start_display (&it, w, tlbufpos);
11869
11870 /* Implementation note: It this still necessary? */
11871 if (it.current_x != this_line_start_x)
11872 goto cancel;
11873
11874 TRACE ((stderr, "trying display optimization 1\n"));
11875 w->cursor.vpos = -1;
11876 overlay_arrow_seen = 0;
11877 it.vpos = this_line_vpos;
11878 it.current_y = this_line_y;
11879 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11880 display_line (&it);
11881
11882 /* If line contains point, is not continued,
11883 and ends at same distance from eob as before, we win. */
11884 if (w->cursor.vpos >= 0
11885 /* Line is not continued, otherwise this_line_start_pos
11886 would have been set to 0 in display_line. */
11887 && CHARPOS (this_line_start_pos)
11888 /* Line ends as before. */
11889 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11890 /* Line has same height as before. Otherwise other lines
11891 would have to be shifted up or down. */
11892 && this_line_pixel_height == line_height_before)
11893 {
11894 /* If this is not the window's last line, we must adjust
11895 the charstarts of the lines below. */
11896 if (it.current_y < it.last_visible_y)
11897 {
11898 struct glyph_row *row
11899 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11900 int delta, delta_bytes;
11901
11902 /* We used to distinguish between two cases here,
11903 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11904 when the line ends in a newline or the end of the
11905 buffer's accessible portion. But both cases did
11906 the same, so they were collapsed. */
11907 delta = (Z
11908 - CHARPOS (tlendpos)
11909 - MATRIX_ROW_START_CHARPOS (row));
11910 delta_bytes = (Z_BYTE
11911 - BYTEPOS (tlendpos)
11912 - MATRIX_ROW_START_BYTEPOS (row));
11913
11914 increment_matrix_positions (w->current_matrix,
11915 this_line_vpos + 1,
11916 w->current_matrix->nrows,
11917 delta, delta_bytes);
11918 }
11919
11920 /* If this row displays text now but previously didn't,
11921 or vice versa, w->window_end_vpos may have to be
11922 adjusted. */
11923 if ((it.glyph_row - 1)->displays_text_p)
11924 {
11925 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11926 XSETINT (w->window_end_vpos, this_line_vpos);
11927 }
11928 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11929 && this_line_vpos > 0)
11930 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11931 w->window_end_valid = Qnil;
11932
11933 /* Update hint: No need to try to scroll in update_window. */
11934 w->desired_matrix->no_scrolling_p = 1;
11935
11936 #if GLYPH_DEBUG
11937 *w->desired_matrix->method = 0;
11938 debug_method_add (w, "optimization 1");
11939 #endif
11940 #ifdef HAVE_WINDOW_SYSTEM
11941 update_window_fringes (w, 0);
11942 #endif
11943 goto update;
11944 }
11945 else
11946 goto cancel;
11947 }
11948 else if (/* Cursor position hasn't changed. */
11949 PT == XFASTINT (w->last_point)
11950 /* Make sure the cursor was last displayed
11951 in this window. Otherwise we have to reposition it. */
11952 && 0 <= w->cursor.vpos
11953 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11954 {
11955 if (!must_finish)
11956 {
11957 do_pending_window_change (1);
11958
11959 /* We used to always goto end_of_redisplay here, but this
11960 isn't enough if we have a blinking cursor. */
11961 if (w->cursor_off_p == w->last_cursor_off_p)
11962 goto end_of_redisplay;
11963 }
11964 goto update;
11965 }
11966 /* If highlighting the region, or if the cursor is in the echo area,
11967 then we can't just move the cursor. */
11968 else if (! (!NILP (Vtransient_mark_mode)
11969 && !NILP (current_buffer->mark_active))
11970 && (EQ (selected_window, current_buffer->last_selected_window)
11971 || highlight_nonselected_windows)
11972 && NILP (w->region_showing)
11973 && NILP (Vshow_trailing_whitespace)
11974 && !cursor_in_echo_area)
11975 {
11976 struct it it;
11977 struct glyph_row *row;
11978
11979 /* Skip from tlbufpos to PT and see where it is. Note that
11980 PT may be in invisible text. If so, we will end at the
11981 next visible position. */
11982 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11983 NULL, DEFAULT_FACE_ID);
11984 it.current_x = this_line_start_x;
11985 it.current_y = this_line_y;
11986 it.vpos = this_line_vpos;
11987
11988 /* The call to move_it_to stops in front of PT, but
11989 moves over before-strings. */
11990 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11991
11992 if (it.vpos == this_line_vpos
11993 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11994 row->enabled_p))
11995 {
11996 xassert (this_line_vpos == it.vpos);
11997 xassert (this_line_y == it.current_y);
11998 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11999 #if GLYPH_DEBUG
12000 *w->desired_matrix->method = 0;
12001 debug_method_add (w, "optimization 3");
12002 #endif
12003 goto update;
12004 }
12005 else
12006 goto cancel;
12007 }
12008
12009 cancel:
12010 /* Text changed drastically or point moved off of line. */
12011 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12012 }
12013
12014 CHARPOS (this_line_start_pos) = 0;
12015 consider_all_windows_p |= buffer_shared > 1;
12016 ++clear_face_cache_count;
12017 #ifdef HAVE_WINDOW_SYSTEM
12018 ++clear_image_cache_count;
12019 #endif
12020
12021 /* Build desired matrices, and update the display. If
12022 consider_all_windows_p is non-zero, do it for all windows on all
12023 frames. Otherwise do it for selected_window, only. */
12024
12025 if (consider_all_windows_p)
12026 {
12027 Lisp_Object tail, frame;
12028
12029 FOR_EACH_FRAME (tail, frame)
12030 XFRAME (frame)->updated_p = 0;
12031
12032 /* Recompute # windows showing selected buffer. This will be
12033 incremented each time such a window is displayed. */
12034 buffer_shared = 0;
12035
12036 FOR_EACH_FRAME (tail, frame)
12037 {
12038 struct frame *f = XFRAME (frame);
12039
12040 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12041 {
12042 if (! EQ (frame, selected_frame))
12043 /* Select the frame, for the sake of frame-local
12044 variables. */
12045 select_frame_for_redisplay (frame);
12046
12047 /* Mark all the scroll bars to be removed; we'll redeem
12048 the ones we want when we redisplay their windows. */
12049 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12050 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12051
12052 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12053 redisplay_windows (FRAME_ROOT_WINDOW (f));
12054
12055 /* The X error handler may have deleted that frame. */
12056 if (!FRAME_LIVE_P (f))
12057 continue;
12058
12059 /* Any scroll bars which redisplay_windows should have
12060 nuked should now go away. */
12061 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12062 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12063
12064 /* If fonts changed, display again. */
12065 /* ??? rms: I suspect it is a mistake to jump all the way
12066 back to retry here. It should just retry this frame. */
12067 if (fonts_changed_p)
12068 goto retry;
12069
12070 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12071 {
12072 /* See if we have to hscroll. */
12073 if (!f->already_hscrolled_p)
12074 {
12075 f->already_hscrolled_p = 1;
12076 if (hscroll_windows (f->root_window))
12077 goto retry;
12078 }
12079
12080 /* Prevent various kinds of signals during display
12081 update. stdio is not robust about handling
12082 signals, which can cause an apparent I/O
12083 error. */
12084 if (interrupt_input)
12085 unrequest_sigio ();
12086 STOP_POLLING;
12087
12088 /* Update the display. */
12089 set_window_update_flags (XWINDOW (f->root_window), 1);
12090 pause |= update_frame (f, 0, 0);
12091 f->updated_p = 1;
12092 }
12093 }
12094 }
12095
12096 if (!EQ (old_frame, selected_frame)
12097 && FRAME_LIVE_P (XFRAME (old_frame)))
12098 /* We played a bit fast-and-loose above and allowed selected_frame
12099 and selected_window to be temporarily out-of-sync but let's make
12100 sure this stays contained. */
12101 select_frame_for_redisplay (old_frame);
12102 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12103
12104 if (!pause)
12105 {
12106 /* Do the mark_window_display_accurate after all windows have
12107 been redisplayed because this call resets flags in buffers
12108 which are needed for proper redisplay. */
12109 FOR_EACH_FRAME (tail, frame)
12110 {
12111 struct frame *f = XFRAME (frame);
12112 if (f->updated_p)
12113 {
12114 mark_window_display_accurate (f->root_window, 1);
12115 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12116 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12117 }
12118 }
12119 }
12120 }
12121 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12122 {
12123 Lisp_Object mini_window;
12124 struct frame *mini_frame;
12125
12126 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12127 /* Use list_of_error, not Qerror, so that
12128 we catch only errors and don't run the debugger. */
12129 internal_condition_case_1 (redisplay_window_1, selected_window,
12130 list_of_error,
12131 redisplay_window_error);
12132
12133 /* Compare desired and current matrices, perform output. */
12134
12135 update:
12136 /* If fonts changed, display again. */
12137 if (fonts_changed_p)
12138 goto retry;
12139
12140 /* Prevent various kinds of signals during display update.
12141 stdio is not robust about handling signals,
12142 which can cause an apparent I/O error. */
12143 if (interrupt_input)
12144 unrequest_sigio ();
12145 STOP_POLLING;
12146
12147 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12148 {
12149 if (hscroll_windows (selected_window))
12150 goto retry;
12151
12152 XWINDOW (selected_window)->must_be_updated_p = 1;
12153 pause = update_frame (sf, 0, 0);
12154 }
12155
12156 /* We may have called echo_area_display at the top of this
12157 function. If the echo area is on another frame, that may
12158 have put text on a frame other than the selected one, so the
12159 above call to update_frame would not have caught it. Catch
12160 it here. */
12161 mini_window = FRAME_MINIBUF_WINDOW (sf);
12162 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12163
12164 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12165 {
12166 XWINDOW (mini_window)->must_be_updated_p = 1;
12167 pause |= update_frame (mini_frame, 0, 0);
12168 if (!pause && hscroll_windows (mini_window))
12169 goto retry;
12170 }
12171 }
12172
12173 /* If display was paused because of pending input, make sure we do a
12174 thorough update the next time. */
12175 if (pause)
12176 {
12177 /* Prevent the optimization at the beginning of
12178 redisplay_internal that tries a single-line update of the
12179 line containing the cursor in the selected window. */
12180 CHARPOS (this_line_start_pos) = 0;
12181
12182 /* Let the overlay arrow be updated the next time. */
12183 update_overlay_arrows (0);
12184
12185 /* If we pause after scrolling, some rows in the current
12186 matrices of some windows are not valid. */
12187 if (!WINDOW_FULL_WIDTH_P (w)
12188 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12189 update_mode_lines = 1;
12190 }
12191 else
12192 {
12193 if (!consider_all_windows_p)
12194 {
12195 /* This has already been done above if
12196 consider_all_windows_p is set. */
12197 mark_window_display_accurate_1 (w, 1);
12198
12199 /* Say overlay arrows are up to date. */
12200 update_overlay_arrows (1);
12201
12202 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12203 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12204 }
12205
12206 update_mode_lines = 0;
12207 windows_or_buffers_changed = 0;
12208 cursor_type_changed = 0;
12209 }
12210
12211 /* Start SIGIO interrupts coming again. Having them off during the
12212 code above makes it less likely one will discard output, but not
12213 impossible, since there might be stuff in the system buffer here.
12214 But it is much hairier to try to do anything about that. */
12215 if (interrupt_input)
12216 request_sigio ();
12217 RESUME_POLLING;
12218
12219 /* If a frame has become visible which was not before, redisplay
12220 again, so that we display it. Expose events for such a frame
12221 (which it gets when becoming visible) don't call the parts of
12222 redisplay constructing glyphs, so simply exposing a frame won't
12223 display anything in this case. So, we have to display these
12224 frames here explicitly. */
12225 if (!pause)
12226 {
12227 Lisp_Object tail, frame;
12228 int new_count = 0;
12229
12230 FOR_EACH_FRAME (tail, frame)
12231 {
12232 int this_is_visible = 0;
12233
12234 if (XFRAME (frame)->visible)
12235 this_is_visible = 1;
12236 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12237 if (XFRAME (frame)->visible)
12238 this_is_visible = 1;
12239
12240 if (this_is_visible)
12241 new_count++;
12242 }
12243
12244 if (new_count != number_of_visible_frames)
12245 windows_or_buffers_changed++;
12246 }
12247
12248 /* Change frame size now if a change is pending. */
12249 do_pending_window_change (1);
12250
12251 /* If we just did a pending size change, or have additional
12252 visible frames, redisplay again. */
12253 if (windows_or_buffers_changed && !pause)
12254 goto retry;
12255
12256 /* Clear the face cache eventually. */
12257 if (consider_all_windows_p)
12258 {
12259 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12260 {
12261 clear_face_cache (0);
12262 clear_face_cache_count = 0;
12263 }
12264 #ifdef HAVE_WINDOW_SYSTEM
12265 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12266 {
12267 clear_image_caches (Qnil);
12268 clear_image_cache_count = 0;
12269 }
12270 #endif /* HAVE_WINDOW_SYSTEM */
12271 }
12272
12273 end_of_redisplay:
12274 unbind_to (count, Qnil);
12275 RESUME_POLLING;
12276 }
12277
12278
12279 /* Redisplay, but leave alone any recent echo area message unless
12280 another message has been requested in its place.
12281
12282 This is useful in situations where you need to redisplay but no
12283 user action has occurred, making it inappropriate for the message
12284 area to be cleared. See tracking_off and
12285 wait_reading_process_output for examples of these situations.
12286
12287 FROM_WHERE is an integer saying from where this function was
12288 called. This is useful for debugging. */
12289
12290 void
12291 redisplay_preserve_echo_area (from_where)
12292 int from_where;
12293 {
12294 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12295
12296 if (!NILP (echo_area_buffer[1]))
12297 {
12298 /* We have a previously displayed message, but no current
12299 message. Redisplay the previous message. */
12300 display_last_displayed_message_p = 1;
12301 redisplay_internal (1);
12302 display_last_displayed_message_p = 0;
12303 }
12304 else
12305 redisplay_internal (1);
12306
12307 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12308 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12309 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12310 }
12311
12312
12313 /* Function registered with record_unwind_protect in
12314 redisplay_internal. Reset redisplaying_p to the value it had
12315 before redisplay_internal was called, and clear
12316 prevent_freeing_realized_faces_p. It also selects the previously
12317 selected frame, unless it has been deleted (by an X connection
12318 failure during redisplay, for example). */
12319
12320 static Lisp_Object
12321 unwind_redisplay (val)
12322 Lisp_Object val;
12323 {
12324 Lisp_Object old_redisplaying_p, old_frame;
12325
12326 old_redisplaying_p = XCAR (val);
12327 redisplaying_p = XFASTINT (old_redisplaying_p);
12328 old_frame = XCDR (val);
12329 if (! EQ (old_frame, selected_frame)
12330 && FRAME_LIVE_P (XFRAME (old_frame)))
12331 select_frame_for_redisplay (old_frame);
12332 return Qnil;
12333 }
12334
12335
12336 /* Mark the display of window W as accurate or inaccurate. If
12337 ACCURATE_P is non-zero mark display of W as accurate. If
12338 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12339 redisplay_internal is called. */
12340
12341 static void
12342 mark_window_display_accurate_1 (w, accurate_p)
12343 struct window *w;
12344 int accurate_p;
12345 {
12346 if (BUFFERP (w->buffer))
12347 {
12348 struct buffer *b = XBUFFER (w->buffer);
12349
12350 w->last_modified
12351 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12352 w->last_overlay_modified
12353 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12354 w->last_had_star
12355 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12356
12357 if (accurate_p)
12358 {
12359 b->clip_changed = 0;
12360 b->prevent_redisplay_optimizations_p = 0;
12361
12362 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12363 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12364 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12365 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12366
12367 w->current_matrix->buffer = b;
12368 w->current_matrix->begv = BUF_BEGV (b);
12369 w->current_matrix->zv = BUF_ZV (b);
12370
12371 w->last_cursor = w->cursor;
12372 w->last_cursor_off_p = w->cursor_off_p;
12373
12374 if (w == XWINDOW (selected_window))
12375 w->last_point = make_number (BUF_PT (b));
12376 else
12377 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12378 }
12379 }
12380
12381 if (accurate_p)
12382 {
12383 w->window_end_valid = w->buffer;
12384 w->update_mode_line = Qnil;
12385 }
12386 }
12387
12388
12389 /* Mark the display of windows in the window tree rooted at WINDOW as
12390 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12391 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12392 be redisplayed the next time redisplay_internal is called. */
12393
12394 void
12395 mark_window_display_accurate (window, accurate_p)
12396 Lisp_Object window;
12397 int accurate_p;
12398 {
12399 struct window *w;
12400
12401 for (; !NILP (window); window = w->next)
12402 {
12403 w = XWINDOW (window);
12404 mark_window_display_accurate_1 (w, accurate_p);
12405
12406 if (!NILP (w->vchild))
12407 mark_window_display_accurate (w->vchild, accurate_p);
12408 if (!NILP (w->hchild))
12409 mark_window_display_accurate (w->hchild, accurate_p);
12410 }
12411
12412 if (accurate_p)
12413 {
12414 update_overlay_arrows (1);
12415 }
12416 else
12417 {
12418 /* Force a thorough redisplay the next time by setting
12419 last_arrow_position and last_arrow_string to t, which is
12420 unequal to any useful value of Voverlay_arrow_... */
12421 update_overlay_arrows (-1);
12422 }
12423 }
12424
12425
12426 /* Return value in display table DP (Lisp_Char_Table *) for character
12427 C. Since a display table doesn't have any parent, we don't have to
12428 follow parent. Do not call this function directly but use the
12429 macro DISP_CHAR_VECTOR. */
12430
12431 Lisp_Object
12432 disp_char_vector (dp, c)
12433 struct Lisp_Char_Table *dp;
12434 int c;
12435 {
12436 Lisp_Object val;
12437
12438 if (ASCII_CHAR_P (c))
12439 {
12440 val = dp->ascii;
12441 if (SUB_CHAR_TABLE_P (val))
12442 val = XSUB_CHAR_TABLE (val)->contents[c];
12443 }
12444 else
12445 {
12446 Lisp_Object table;
12447
12448 XSETCHAR_TABLE (table, dp);
12449 val = char_table_ref (table, c);
12450 }
12451 if (NILP (val))
12452 val = dp->defalt;
12453 return val;
12454 }
12455
12456
12457 \f
12458 /***********************************************************************
12459 Window Redisplay
12460 ***********************************************************************/
12461
12462 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12463
12464 static void
12465 redisplay_windows (window)
12466 Lisp_Object window;
12467 {
12468 while (!NILP (window))
12469 {
12470 struct window *w = XWINDOW (window);
12471
12472 if (!NILP (w->hchild))
12473 redisplay_windows (w->hchild);
12474 else if (!NILP (w->vchild))
12475 redisplay_windows (w->vchild);
12476 else if (!NILP (w->buffer))
12477 {
12478 displayed_buffer = XBUFFER (w->buffer);
12479 /* Use list_of_error, not Qerror, so that
12480 we catch only errors and don't run the debugger. */
12481 internal_condition_case_1 (redisplay_window_0, window,
12482 list_of_error,
12483 redisplay_window_error);
12484 }
12485
12486 window = w->next;
12487 }
12488 }
12489
12490 static Lisp_Object
12491 redisplay_window_error ()
12492 {
12493 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12494 return Qnil;
12495 }
12496
12497 static Lisp_Object
12498 redisplay_window_0 (window)
12499 Lisp_Object window;
12500 {
12501 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12502 redisplay_window (window, 0);
12503 return Qnil;
12504 }
12505
12506 static Lisp_Object
12507 redisplay_window_1 (window)
12508 Lisp_Object window;
12509 {
12510 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12511 redisplay_window (window, 1);
12512 return Qnil;
12513 }
12514 \f
12515
12516 /* Increment GLYPH until it reaches END or CONDITION fails while
12517 adding (GLYPH)->pixel_width to X. */
12518
12519 #define SKIP_GLYPHS(glyph, end, x, condition) \
12520 do \
12521 { \
12522 (x) += (glyph)->pixel_width; \
12523 ++(glyph); \
12524 } \
12525 while ((glyph) < (end) && (condition))
12526
12527
12528 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12529 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12530 which positions recorded in ROW differ from current buffer
12531 positions.
12532
12533 Return 0 if cursor is not on this row, 1 otherwise. */
12534
12535 int
12536 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12537 struct window *w;
12538 struct glyph_row *row;
12539 struct glyph_matrix *matrix;
12540 int delta, delta_bytes, dy, dvpos;
12541 {
12542 struct glyph *glyph = row->glyphs[TEXT_AREA];
12543 struct glyph *end = glyph + row->used[TEXT_AREA];
12544 struct glyph *cursor = NULL;
12545 /* The last known character position in row. */
12546 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12547 int x = row->x;
12548 int cursor_x = x;
12549 EMACS_INT pt_old = PT - delta;
12550 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12551 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12552 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12553 /* Non-zero means we've found a match for cursor position, but that
12554 glyph has the avoid_cursor_p flag set. */
12555 int match_with_avoid_cursor = 0;
12556 /* Non-zero means we've seen at least one glyph that came from a
12557 display string. */
12558 int string_seen = 0;
12559 /* Largest buffer position seen so far during scan of glyph row. */
12560 EMACS_INT bpos_max = last_pos;
12561 /* Last buffer position covered by an overlay string with an integer
12562 `cursor' property. */
12563 EMACS_INT bpos_covered = 0;
12564
12565 /* Skip over glyphs not having an object at the start and the end of
12566 the row. These are special glyphs like truncation marks on
12567 terminal frames. */
12568 if (row->displays_text_p)
12569 {
12570 if (!row->reversed_p)
12571 {
12572 while (glyph < end
12573 && INTEGERP (glyph->object)
12574 && glyph->charpos < 0)
12575 {
12576 x += glyph->pixel_width;
12577 ++glyph;
12578 }
12579 while (end > glyph
12580 && INTEGERP ((end - 1)->object)
12581 /* CHARPOS is zero for blanks inserted by
12582 extend_face_to_end_of_line. */
12583 && (end - 1)->charpos <= 0)
12584 --end;
12585 glyph_before = glyph - 1;
12586 glyph_after = end;
12587 }
12588 else
12589 {
12590 struct glyph *g;
12591
12592 /* If the glyph row is reversed, we need to process it from back
12593 to front, so swap the edge pointers. */
12594 end = glyph - 1;
12595 glyph += row->used[TEXT_AREA] - 1;
12596 /* Reverse the known positions in the row. */
12597 last_pos = pos_after = MATRIX_ROW_START_CHARPOS (row) + delta;
12598 pos_before = MATRIX_ROW_END_CHARPOS (row) + delta;
12599
12600 while (glyph > end + 1
12601 && INTEGERP (glyph->object)
12602 && glyph->charpos < 0)
12603 {
12604 --glyph;
12605 x -= glyph->pixel_width;
12606 }
12607 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12608 --glyph;
12609 /* By default, put the cursor on the rightmost glyph. */
12610 for (g = end + 1; g < glyph; g++)
12611 x += g->pixel_width;
12612 cursor_x = x;
12613 while (end < glyph
12614 && INTEGERP ((end + 1)->object)
12615 && (end + 1)->charpos <= 0)
12616 ++end;
12617 glyph_before = glyph + 1;
12618 glyph_after = end;
12619 }
12620 }
12621 else if (row->reversed_p)
12622 {
12623 /* In R2L rows that don't display text, put the cursor on the
12624 rightmost glyph. Case in point: an empty last line that is
12625 part of an R2L paragraph. */
12626 cursor = end - 1;
12627 x = -1; /* will be computed below, at lable compute_x */
12628 }
12629
12630 /* Step 1: Try to find the glyph whose character position
12631 corresponds to point. If that's not possible, find 2 glyphs
12632 whose character positions are the closest to point, one before
12633 point, the other after it. */
12634 if (!row->reversed_p)
12635 while (/* not marched to end of glyph row */
12636 glyph < end
12637 /* glyph was not inserted by redisplay for internal purposes */
12638 && !INTEGERP (glyph->object))
12639 {
12640 if (BUFFERP (glyph->object))
12641 {
12642 EMACS_INT dpos = glyph->charpos - pt_old;
12643
12644 if (glyph->charpos > bpos_max)
12645 bpos_max = glyph->charpos;
12646 if (!glyph->avoid_cursor_p)
12647 {
12648 /* If we hit point, we've found the glyph on which to
12649 display the cursor. */
12650 if (dpos == 0)
12651 {
12652 match_with_avoid_cursor = 0;
12653 break;
12654 }
12655 /* See if we've found a better approximation to
12656 POS_BEFORE or to POS_AFTER. Note that we want the
12657 first (leftmost) glyph of all those that are the
12658 closest from below, and the last (rightmost) of all
12659 those from above. */
12660 if (0 > dpos && dpos > pos_before - pt_old)
12661 {
12662 pos_before = glyph->charpos;
12663 glyph_before = glyph;
12664 }
12665 else if (0 < dpos && dpos <= pos_after - pt_old)
12666 {
12667 pos_after = glyph->charpos;
12668 glyph_after = glyph;
12669 }
12670 }
12671 else if (dpos == 0)
12672 match_with_avoid_cursor = 1;
12673 }
12674 else if (STRINGP (glyph->object))
12675 {
12676 Lisp_Object chprop;
12677 int glyph_pos = glyph->charpos;
12678
12679 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12680 glyph->object);
12681 if (INTEGERP (chprop))
12682 {
12683 bpos_covered = bpos_max + XINT (chprop);
12684 /* If the `cursor' property covers buffer positions up
12685 to and including point, we should display cursor on
12686 this glyph. */
12687 /* Implementation note: bpos_max == pt_old when, e.g.,
12688 we are in an empty line, where bpos_max is set to
12689 MATRIX_ROW_START_CHARPOS, see above. */
12690 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12691 {
12692 cursor = glyph;
12693 break;
12694 }
12695 }
12696
12697 string_seen = 1;
12698 }
12699 x += glyph->pixel_width;
12700 ++glyph;
12701 }
12702 else if (glyph > end) /* row is reversed */
12703 while (!INTEGERP (glyph->object))
12704 {
12705 if (BUFFERP (glyph->object))
12706 {
12707 EMACS_INT dpos = glyph->charpos - pt_old;
12708
12709 if (glyph->charpos > bpos_max)
12710 bpos_max = glyph->charpos;
12711 if (!glyph->avoid_cursor_p)
12712 {
12713 if (dpos == 0)
12714 {
12715 match_with_avoid_cursor = 0;
12716 break;
12717 }
12718 if (0 > dpos && dpos > pos_before - pt_old)
12719 {
12720 pos_before = glyph->charpos;
12721 glyph_before = glyph;
12722 }
12723 else if (0 < dpos && dpos <= pos_after - pt_old)
12724 {
12725 pos_after = glyph->charpos;
12726 glyph_after = glyph;
12727 }
12728 }
12729 else if (dpos == 0)
12730 match_with_avoid_cursor = 1;
12731 }
12732 else if (STRINGP (glyph->object))
12733 {
12734 Lisp_Object chprop;
12735 int glyph_pos = glyph->charpos;
12736
12737 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12738 glyph->object);
12739 if (INTEGERP (chprop))
12740 {
12741 bpos_covered = bpos_max + XINT (chprop);
12742 /* If the `cursor' property covers buffer positions up
12743 to and including point, we should display cursor on
12744 this glyph. */
12745 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12746 {
12747 cursor = glyph;
12748 break;
12749 }
12750 }
12751 string_seen = 1;
12752 }
12753 --glyph;
12754 if (glyph == end)
12755 break;
12756 x -= glyph->pixel_width;
12757 }
12758
12759 /* Step 2: If we didn't find an exact match for point, we need to
12760 look for a proper place to put the cursor among glyphs between
12761 GLYPH_BEFORE and GLYPH_AFTER. */
12762 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12763 && bpos_covered < pt_old)
12764 {
12765 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12766 {
12767 EMACS_INT ellipsis_pos;
12768
12769 /* Scan back over the ellipsis glyphs. */
12770 if (!row->reversed_p)
12771 {
12772 ellipsis_pos = (glyph - 1)->charpos;
12773 while (glyph > row->glyphs[TEXT_AREA]
12774 && (glyph - 1)->charpos == ellipsis_pos)
12775 glyph--, x -= glyph->pixel_width;
12776 /* That loop always goes one position too far, including
12777 the glyph before the ellipsis. So scan forward over
12778 that one. */
12779 x += glyph->pixel_width;
12780 glyph++;
12781 }
12782 else /* row is reversed */
12783 {
12784 ellipsis_pos = (glyph + 1)->charpos;
12785 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12786 && (glyph + 1)->charpos == ellipsis_pos)
12787 glyph++, x += glyph->pixel_width;
12788 x -= glyph->pixel_width;
12789 glyph--;
12790 }
12791 }
12792 else if (match_with_avoid_cursor
12793 /* zero-width characters produce no glyphs */
12794 || eabs (glyph_after - glyph_before) == 1)
12795 {
12796 cursor = glyph_after;
12797 x = -1;
12798 }
12799 else if (string_seen)
12800 {
12801 int incr = row->reversed_p ? -1 : +1;
12802
12803 /* Need to find the glyph that came out of a string which is
12804 present at point. That glyph is somewhere between
12805 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12806 positioned between POS_BEFORE and POS_AFTER in the
12807 buffer. */
12808 struct glyph *stop = glyph_after;
12809 EMACS_INT pos = pos_before;
12810
12811 x = -1;
12812 for (glyph = glyph_before + incr;
12813 row->reversed_p ? glyph > stop : glyph < stop; )
12814 {
12815
12816 /* Any glyphs that come from the buffer are here because
12817 of bidi reordering. Skip them, and only pay
12818 attention to glyphs that came from some string. */
12819 if (STRINGP (glyph->object))
12820 {
12821 Lisp_Object str;
12822 EMACS_INT tem;
12823
12824 str = glyph->object;
12825 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12826 if (pos <= tem)
12827 {
12828 /* If the string from which this glyph came is
12829 found in the buffer at point, then we've
12830 found the glyph we've been looking for. */
12831 if (tem == pt_old)
12832 {
12833 /* The glyphs from this string could have
12834 been reordered. Find the one with the
12835 smallest string position. Or there could
12836 be a character in the string with the
12837 `cursor' property, which means display
12838 cursor on that character's glyph. */
12839 int strpos = glyph->charpos;
12840
12841 cursor = glyph;
12842 for (glyph += incr;
12843 EQ (glyph->object, str);
12844 glyph += incr)
12845 {
12846 Lisp_Object cprop;
12847 int gpos = glyph->charpos;
12848
12849 cprop = Fget_char_property (make_number (gpos),
12850 Qcursor,
12851 glyph->object);
12852 if (!NILP (cprop))
12853 {
12854 cursor = glyph;
12855 break;
12856 }
12857 if (glyph->charpos < strpos)
12858 {
12859 strpos = glyph->charpos;
12860 cursor = glyph;
12861 }
12862 }
12863
12864 goto compute_x;
12865 }
12866 pos = tem + 1; /* don't find previous instances */
12867 }
12868 /* This string is not what we want; skip all of the
12869 glyphs that came from it. */
12870 do
12871 glyph += incr;
12872 while ((row->reversed_p ? glyph > stop : glyph < stop)
12873 && EQ (glyph->object, str));
12874 }
12875 else
12876 glyph += incr;
12877 }
12878
12879 /* If we reached the end of the line, and END was from a string,
12880 the cursor is not on this line. */
12881 if (glyph == end
12882 && STRINGP ((glyph - incr)->object)
12883 && row->continued_p)
12884 return 0;
12885 }
12886 }
12887
12888 compute_x:
12889 if (cursor != NULL)
12890 glyph = cursor;
12891 if (x < 0)
12892 {
12893 struct glyph *g;
12894
12895 /* Need to compute x that corresponds to GLYPH. */
12896 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12897 {
12898 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12899 abort ();
12900 x += g->pixel_width;
12901 }
12902 }
12903
12904 /* ROW could be part of a continued line, which might have other
12905 rows whose start and end charpos occlude point. Only set
12906 w->cursor if we found a better approximation to the cursor
12907 position than we have from previously examined rows. */
12908 if (w->cursor.vpos >= 0
12909 /* Make sure cursor.vpos specifies a row whose start and end
12910 charpos occlude point. This is because some callers of this
12911 function leave cursor.vpos at the row where the cursor was
12912 displayed during the last redisplay cycle. */
12913 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12914 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12915 {
12916 struct glyph *g1 =
12917 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12918
12919 /* Keep the candidate whose buffer position is the closest to
12920 point. */
12921 if (BUFFERP (g1->object)
12922 && (g1->charpos == pt_old /* an exact match always wins */
12923 || (BUFFERP (glyph->object)
12924 && eabs (g1->charpos - pt_old)
12925 < eabs (glyph->charpos - pt_old))))
12926 return 0;
12927 /* If this candidate gives an exact match, use that. */
12928 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12929 /* Otherwise, keep the candidate that comes from a row
12930 spanning less buffer positions. This may win when one or
12931 both candidate positions are on glyphs that came from
12932 display strings, for which we cannot compare buffer
12933 positions. */
12934 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12935 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12936 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12937 return 0;
12938 }
12939 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12940 w->cursor.x = x;
12941 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12942 w->cursor.y = row->y + dy;
12943
12944 if (w == XWINDOW (selected_window))
12945 {
12946 if (!row->continued_p
12947 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12948 && row->x == 0)
12949 {
12950 this_line_buffer = XBUFFER (w->buffer);
12951
12952 CHARPOS (this_line_start_pos)
12953 = MATRIX_ROW_START_CHARPOS (row) + delta;
12954 BYTEPOS (this_line_start_pos)
12955 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12956
12957 CHARPOS (this_line_end_pos)
12958 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12959 BYTEPOS (this_line_end_pos)
12960 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12961
12962 this_line_y = w->cursor.y;
12963 this_line_pixel_height = row->height;
12964 this_line_vpos = w->cursor.vpos;
12965 this_line_start_x = row->x;
12966 }
12967 else
12968 CHARPOS (this_line_start_pos) = 0;
12969 }
12970
12971 return 1;
12972 }
12973
12974
12975 /* Run window scroll functions, if any, for WINDOW with new window
12976 start STARTP. Sets the window start of WINDOW to that position.
12977
12978 We assume that the window's buffer is really current. */
12979
12980 static INLINE struct text_pos
12981 run_window_scroll_functions (window, startp)
12982 Lisp_Object window;
12983 struct text_pos startp;
12984 {
12985 struct window *w = XWINDOW (window);
12986 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12987
12988 if (current_buffer != XBUFFER (w->buffer))
12989 abort ();
12990
12991 if (!NILP (Vwindow_scroll_functions))
12992 {
12993 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12994 make_number (CHARPOS (startp)));
12995 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12996 /* In case the hook functions switch buffers. */
12997 if (current_buffer != XBUFFER (w->buffer))
12998 set_buffer_internal_1 (XBUFFER (w->buffer));
12999 }
13000
13001 return startp;
13002 }
13003
13004
13005 /* Make sure the line containing the cursor is fully visible.
13006 A value of 1 means there is nothing to be done.
13007 (Either the line is fully visible, or it cannot be made so,
13008 or we cannot tell.)
13009
13010 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13011 is higher than window.
13012
13013 A value of 0 means the caller should do scrolling
13014 as if point had gone off the screen. */
13015
13016 static int
13017 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
13018 struct window *w;
13019 int force_p;
13020 int current_matrix_p;
13021 {
13022 struct glyph_matrix *matrix;
13023 struct glyph_row *row;
13024 int window_height;
13025
13026 if (!make_cursor_line_fully_visible_p)
13027 return 1;
13028
13029 /* It's not always possible to find the cursor, e.g, when a window
13030 is full of overlay strings. Don't do anything in that case. */
13031 if (w->cursor.vpos < 0)
13032 return 1;
13033
13034 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13035 row = MATRIX_ROW (matrix, w->cursor.vpos);
13036
13037 /* If the cursor row is not partially visible, there's nothing to do. */
13038 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13039 return 1;
13040
13041 /* If the row the cursor is in is taller than the window's height,
13042 it's not clear what to do, so do nothing. */
13043 window_height = window_box_height (w);
13044 if (row->height >= window_height)
13045 {
13046 if (!force_p || MINI_WINDOW_P (w)
13047 || w->vscroll || w->cursor.vpos == 0)
13048 return 1;
13049 }
13050 return 0;
13051 }
13052
13053
13054 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13055 non-zero means only WINDOW is redisplayed in redisplay_internal.
13056 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13057 in redisplay_window to bring a partially visible line into view in
13058 the case that only the cursor has moved.
13059
13060 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13061 last screen line's vertical height extends past the end of the screen.
13062
13063 Value is
13064
13065 1 if scrolling succeeded
13066
13067 0 if scrolling didn't find point.
13068
13069 -1 if new fonts have been loaded so that we must interrupt
13070 redisplay, adjust glyph matrices, and try again. */
13071
13072 enum
13073 {
13074 SCROLLING_SUCCESS,
13075 SCROLLING_FAILED,
13076 SCROLLING_NEED_LARGER_MATRICES
13077 };
13078
13079 static int
13080 try_scrolling (window, just_this_one_p, scroll_conservatively,
13081 scroll_step, temp_scroll_step, last_line_misfit)
13082 Lisp_Object window;
13083 int just_this_one_p;
13084 EMACS_INT scroll_conservatively, scroll_step;
13085 int temp_scroll_step;
13086 int last_line_misfit;
13087 {
13088 struct window *w = XWINDOW (window);
13089 struct frame *f = XFRAME (w->frame);
13090 struct text_pos pos, startp;
13091 struct it it;
13092 int this_scroll_margin, scroll_max, rc, height;
13093 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13094 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13095 Lisp_Object aggressive;
13096 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13097
13098 #if GLYPH_DEBUG
13099 debug_method_add (w, "try_scrolling");
13100 #endif
13101
13102 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13103
13104 /* Compute scroll margin height in pixels. We scroll when point is
13105 within this distance from the top or bottom of the window. */
13106 if (scroll_margin > 0)
13107 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13108 * FRAME_LINE_HEIGHT (f);
13109 else
13110 this_scroll_margin = 0;
13111
13112 /* Force scroll_conservatively to have a reasonable value, to avoid
13113 overflow while computing how much to scroll. Note that the user
13114 can supply scroll-conservatively equal to `most-positive-fixnum',
13115 which can be larger than INT_MAX. */
13116 if (scroll_conservatively > scroll_limit)
13117 {
13118 scroll_conservatively = scroll_limit;
13119 scroll_max = INT_MAX;
13120 }
13121 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13122 /* Compute how much we should try to scroll maximally to bring
13123 point into view. */
13124 scroll_max = (max (scroll_step,
13125 max (scroll_conservatively, temp_scroll_step))
13126 * FRAME_LINE_HEIGHT (f));
13127 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13128 || NUMBERP (current_buffer->scroll_up_aggressively))
13129 /* We're trying to scroll because of aggressive scrolling but no
13130 scroll_step is set. Choose an arbitrary one. */
13131 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13132 else
13133 scroll_max = 0;
13134
13135 too_near_end:
13136
13137 /* Decide whether to scroll down. */
13138 if (PT > CHARPOS (startp))
13139 {
13140 int scroll_margin_y;
13141
13142 /* Compute the pixel ypos of the scroll margin, then move it to
13143 either that ypos or PT, whichever comes first. */
13144 start_display (&it, w, startp);
13145 scroll_margin_y = it.last_visible_y - this_scroll_margin
13146 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13147 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13148 (MOVE_TO_POS | MOVE_TO_Y));
13149
13150 if (PT > CHARPOS (it.current.pos))
13151 {
13152 int y0 = line_bottom_y (&it);
13153
13154 /* Compute the distance from the scroll margin to PT
13155 (including the height of the cursor line). Moving the
13156 iterator unconditionally to PT can be slow if PT is far
13157 away, so stop 10 lines past the window bottom (is there a
13158 way to do the right thing quickly?). */
13159 move_it_to (&it, PT, -1,
13160 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
13161 -1, MOVE_TO_POS | MOVE_TO_Y);
13162 dy = line_bottom_y (&it) - y0;
13163
13164 if (dy > scroll_max)
13165 return SCROLLING_FAILED;
13166
13167 scroll_down_p = 1;
13168 }
13169 }
13170
13171 if (scroll_down_p)
13172 {
13173 /* Point is in or below the bottom scroll margin, so move the
13174 window start down. If scrolling conservatively, move it just
13175 enough down to make point visible. If scroll_step is set,
13176 move it down by scroll_step. */
13177 if (scroll_conservatively)
13178 amount_to_scroll
13179 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13180 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13181 else if (scroll_step || temp_scroll_step)
13182 amount_to_scroll = scroll_max;
13183 else
13184 {
13185 aggressive = current_buffer->scroll_up_aggressively;
13186 height = WINDOW_BOX_TEXT_HEIGHT (w);
13187 if (NUMBERP (aggressive))
13188 {
13189 double float_amount = XFLOATINT (aggressive) * height;
13190 amount_to_scroll = float_amount;
13191 if (amount_to_scroll == 0 && float_amount > 0)
13192 amount_to_scroll = 1;
13193 }
13194 }
13195
13196 if (amount_to_scroll <= 0)
13197 return SCROLLING_FAILED;
13198
13199 start_display (&it, w, startp);
13200 move_it_vertically (&it, amount_to_scroll);
13201
13202 /* If STARTP is unchanged, move it down another screen line. */
13203 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13204 move_it_by_lines (&it, 1, 1);
13205 startp = it.current.pos;
13206 }
13207 else
13208 {
13209 struct text_pos scroll_margin_pos = startp;
13210
13211 /* See if point is inside the scroll margin at the top of the
13212 window. */
13213 if (this_scroll_margin)
13214 {
13215 start_display (&it, w, startp);
13216 move_it_vertically (&it, this_scroll_margin);
13217 scroll_margin_pos = it.current.pos;
13218 }
13219
13220 if (PT < CHARPOS (scroll_margin_pos))
13221 {
13222 /* Point is in the scroll margin at the top of the window or
13223 above what is displayed in the window. */
13224 int y0;
13225
13226 /* Compute the vertical distance from PT to the scroll
13227 margin position. Give up if distance is greater than
13228 scroll_max. */
13229 SET_TEXT_POS (pos, PT, PT_BYTE);
13230 start_display (&it, w, pos);
13231 y0 = it.current_y;
13232 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13233 it.last_visible_y, -1,
13234 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13235 dy = it.current_y - y0;
13236 if (dy > scroll_max)
13237 return SCROLLING_FAILED;
13238
13239 /* Compute new window start. */
13240 start_display (&it, w, startp);
13241
13242 if (scroll_conservatively)
13243 amount_to_scroll
13244 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13245 else if (scroll_step || temp_scroll_step)
13246 amount_to_scroll = scroll_max;
13247 else
13248 {
13249 aggressive = current_buffer->scroll_down_aggressively;
13250 height = WINDOW_BOX_TEXT_HEIGHT (w);
13251 if (NUMBERP (aggressive))
13252 {
13253 double float_amount = XFLOATINT (aggressive) * height;
13254 amount_to_scroll = float_amount;
13255 if (amount_to_scroll == 0 && float_amount > 0)
13256 amount_to_scroll = 1;
13257 }
13258 }
13259
13260 if (amount_to_scroll <= 0)
13261 return SCROLLING_FAILED;
13262
13263 move_it_vertically_backward (&it, amount_to_scroll);
13264 startp = it.current.pos;
13265 }
13266 }
13267
13268 /* Run window scroll functions. */
13269 startp = run_window_scroll_functions (window, startp);
13270
13271 /* Display the window. Give up if new fonts are loaded, or if point
13272 doesn't appear. */
13273 if (!try_window (window, startp, 0))
13274 rc = SCROLLING_NEED_LARGER_MATRICES;
13275 else if (w->cursor.vpos < 0)
13276 {
13277 clear_glyph_matrix (w->desired_matrix);
13278 rc = SCROLLING_FAILED;
13279 }
13280 else
13281 {
13282 /* Maybe forget recorded base line for line number display. */
13283 if (!just_this_one_p
13284 || current_buffer->clip_changed
13285 || BEG_UNCHANGED < CHARPOS (startp))
13286 w->base_line_number = Qnil;
13287
13288 /* If cursor ends up on a partially visible line,
13289 treat that as being off the bottom of the screen. */
13290 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13291 {
13292 clear_glyph_matrix (w->desired_matrix);
13293 ++extra_scroll_margin_lines;
13294 goto too_near_end;
13295 }
13296 rc = SCROLLING_SUCCESS;
13297 }
13298
13299 return rc;
13300 }
13301
13302
13303 /* Compute a suitable window start for window W if display of W starts
13304 on a continuation line. Value is non-zero if a new window start
13305 was computed.
13306
13307 The new window start will be computed, based on W's width, starting
13308 from the start of the continued line. It is the start of the
13309 screen line with the minimum distance from the old start W->start. */
13310
13311 static int
13312 compute_window_start_on_continuation_line (w)
13313 struct window *w;
13314 {
13315 struct text_pos pos, start_pos;
13316 int window_start_changed_p = 0;
13317
13318 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13319
13320 /* If window start is on a continuation line... Window start may be
13321 < BEGV in case there's invisible text at the start of the
13322 buffer (M-x rmail, for example). */
13323 if (CHARPOS (start_pos) > BEGV
13324 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13325 {
13326 struct it it;
13327 struct glyph_row *row;
13328
13329 /* Handle the case that the window start is out of range. */
13330 if (CHARPOS (start_pos) < BEGV)
13331 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13332 else if (CHARPOS (start_pos) > ZV)
13333 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13334
13335 /* Find the start of the continued line. This should be fast
13336 because scan_buffer is fast (newline cache). */
13337 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13338 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13339 row, DEFAULT_FACE_ID);
13340 reseat_at_previous_visible_line_start (&it);
13341
13342 /* If the line start is "too far" away from the window start,
13343 say it takes too much time to compute a new window start. */
13344 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13345 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13346 {
13347 int min_distance, distance;
13348
13349 /* Move forward by display lines to find the new window
13350 start. If window width was enlarged, the new start can
13351 be expected to be > the old start. If window width was
13352 decreased, the new window start will be < the old start.
13353 So, we're looking for the display line start with the
13354 minimum distance from the old window start. */
13355 pos = it.current.pos;
13356 min_distance = INFINITY;
13357 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13358 distance < min_distance)
13359 {
13360 min_distance = distance;
13361 pos = it.current.pos;
13362 move_it_by_lines (&it, 1, 0);
13363 }
13364
13365 /* Set the window start there. */
13366 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13367 window_start_changed_p = 1;
13368 }
13369 }
13370
13371 return window_start_changed_p;
13372 }
13373
13374
13375 /* Try cursor movement in case text has not changed in window WINDOW,
13376 with window start STARTP. Value is
13377
13378 CURSOR_MOVEMENT_SUCCESS if successful
13379
13380 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13381
13382 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13383 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13384 we want to scroll as if scroll-step were set to 1. See the code.
13385
13386 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13387 which case we have to abort this redisplay, and adjust matrices
13388 first. */
13389
13390 enum
13391 {
13392 CURSOR_MOVEMENT_SUCCESS,
13393 CURSOR_MOVEMENT_CANNOT_BE_USED,
13394 CURSOR_MOVEMENT_MUST_SCROLL,
13395 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13396 };
13397
13398 static int
13399 try_cursor_movement (window, startp, scroll_step)
13400 Lisp_Object window;
13401 struct text_pos startp;
13402 int *scroll_step;
13403 {
13404 struct window *w = XWINDOW (window);
13405 struct frame *f = XFRAME (w->frame);
13406 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13407
13408 #if GLYPH_DEBUG
13409 if (inhibit_try_cursor_movement)
13410 return rc;
13411 #endif
13412
13413 /* Handle case where text has not changed, only point, and it has
13414 not moved off the frame. */
13415 if (/* Point may be in this window. */
13416 PT >= CHARPOS (startp)
13417 /* Selective display hasn't changed. */
13418 && !current_buffer->clip_changed
13419 /* Function force-mode-line-update is used to force a thorough
13420 redisplay. It sets either windows_or_buffers_changed or
13421 update_mode_lines. So don't take a shortcut here for these
13422 cases. */
13423 && !update_mode_lines
13424 && !windows_or_buffers_changed
13425 && !cursor_type_changed
13426 /* Can't use this case if highlighting a region. When a
13427 region exists, cursor movement has to do more than just
13428 set the cursor. */
13429 && !(!NILP (Vtransient_mark_mode)
13430 && !NILP (current_buffer->mark_active))
13431 && NILP (w->region_showing)
13432 && NILP (Vshow_trailing_whitespace)
13433 /* Right after splitting windows, last_point may be nil. */
13434 && INTEGERP (w->last_point)
13435 /* This code is not used for mini-buffer for the sake of the case
13436 of redisplaying to replace an echo area message; since in
13437 that case the mini-buffer contents per se are usually
13438 unchanged. This code is of no real use in the mini-buffer
13439 since the handling of this_line_start_pos, etc., in redisplay
13440 handles the same cases. */
13441 && !EQ (window, minibuf_window)
13442 /* When splitting windows or for new windows, it happens that
13443 redisplay is called with a nil window_end_vpos or one being
13444 larger than the window. This should really be fixed in
13445 window.c. I don't have this on my list, now, so we do
13446 approximately the same as the old redisplay code. --gerd. */
13447 && INTEGERP (w->window_end_vpos)
13448 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13449 && (FRAME_WINDOW_P (f)
13450 || !overlay_arrow_in_current_buffer_p ()))
13451 {
13452 int this_scroll_margin, top_scroll_margin;
13453 struct glyph_row *row = NULL;
13454
13455 #if GLYPH_DEBUG
13456 debug_method_add (w, "cursor movement");
13457 #endif
13458
13459 /* Scroll if point within this distance from the top or bottom
13460 of the window. This is a pixel value. */
13461 if (scroll_margin > 0)
13462 {
13463 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13464 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13465 }
13466 else
13467 this_scroll_margin = 0;
13468
13469 top_scroll_margin = this_scroll_margin;
13470 if (WINDOW_WANTS_HEADER_LINE_P (w))
13471 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13472
13473 /* Start with the row the cursor was displayed during the last
13474 not paused redisplay. Give up if that row is not valid. */
13475 if (w->last_cursor.vpos < 0
13476 || w->last_cursor.vpos >= w->current_matrix->nrows)
13477 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13478 else
13479 {
13480 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13481 if (row->mode_line_p)
13482 ++row;
13483 if (!row->enabled_p)
13484 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13485 /* If rows are bidi-reordered, back up until we find a row
13486 that does not belong to a continuation line. This is
13487 because we must consider all rows of a continued line as
13488 candidates for cursor positioning, since row start and
13489 end positions change non-linearly with vertical position
13490 in such rows. */
13491 /* FIXME: Revisit this when glyph ``spilling'' in
13492 continuation lines' rows is implemented for
13493 bidi-reordered rows. */
13494 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13495 {
13496 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13497 {
13498 xassert (row->enabled_p);
13499 --row;
13500 /* If we hit the beginning of the displayed portion
13501 without finding the first row of a continued
13502 line, give up. */
13503 if (row <= w->current_matrix->rows)
13504 {
13505 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13506 break;
13507 }
13508
13509 }
13510 }
13511 }
13512
13513 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13514 {
13515 int scroll_p = 0;
13516 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13517
13518 if (PT > XFASTINT (w->last_point))
13519 {
13520 /* Point has moved forward. */
13521 while (MATRIX_ROW_END_CHARPOS (row) < PT
13522 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13523 {
13524 xassert (row->enabled_p);
13525 ++row;
13526 }
13527
13528 /* The end position of a row equals the start position
13529 of the next row. If PT is there, we would rather
13530 display it in the next line. */
13531 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13532 && MATRIX_ROW_END_CHARPOS (row) == PT
13533 && !cursor_row_p (w, row))
13534 ++row;
13535
13536 /* If within the scroll margin, scroll. Note that
13537 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13538 the next line would be drawn, and that
13539 this_scroll_margin can be zero. */
13540 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13541 || PT > MATRIX_ROW_END_CHARPOS (row)
13542 /* Line is completely visible last line in window
13543 and PT is to be set in the next line. */
13544 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13545 && PT == MATRIX_ROW_END_CHARPOS (row)
13546 && !row->ends_at_zv_p
13547 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13548 scroll_p = 1;
13549 }
13550 else if (PT < XFASTINT (w->last_point))
13551 {
13552 /* Cursor has to be moved backward. Note that PT >=
13553 CHARPOS (startp) because of the outer if-statement. */
13554 while (!row->mode_line_p
13555 && (MATRIX_ROW_START_CHARPOS (row) > PT
13556 || (MATRIX_ROW_START_CHARPOS (row) == PT
13557 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13558 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13559 row > w->current_matrix->rows
13560 && (row-1)->ends_in_newline_from_string_p))))
13561 && (row->y > top_scroll_margin
13562 || CHARPOS (startp) == BEGV))
13563 {
13564 xassert (row->enabled_p);
13565 --row;
13566 }
13567
13568 /* Consider the following case: Window starts at BEGV,
13569 there is invisible, intangible text at BEGV, so that
13570 display starts at some point START > BEGV. It can
13571 happen that we are called with PT somewhere between
13572 BEGV and START. Try to handle that case. */
13573 if (row < w->current_matrix->rows
13574 || row->mode_line_p)
13575 {
13576 row = w->current_matrix->rows;
13577 if (row->mode_line_p)
13578 ++row;
13579 }
13580
13581 /* Due to newlines in overlay strings, we may have to
13582 skip forward over overlay strings. */
13583 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13584 && MATRIX_ROW_END_CHARPOS (row) == PT
13585 && !cursor_row_p (w, row))
13586 ++row;
13587
13588 /* If within the scroll margin, scroll. */
13589 if (row->y < top_scroll_margin
13590 && CHARPOS (startp) != BEGV)
13591 scroll_p = 1;
13592 }
13593 else
13594 {
13595 /* Cursor did not move. So don't scroll even if cursor line
13596 is partially visible, as it was so before. */
13597 rc = CURSOR_MOVEMENT_SUCCESS;
13598 }
13599
13600 if (PT < MATRIX_ROW_START_CHARPOS (row)
13601 || PT > MATRIX_ROW_END_CHARPOS (row))
13602 {
13603 /* if PT is not in the glyph row, give up. */
13604 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13605 }
13606 else if (rc != CURSOR_MOVEMENT_SUCCESS
13607 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13608 && make_cursor_line_fully_visible_p)
13609 {
13610 if (PT == MATRIX_ROW_END_CHARPOS (row)
13611 && !row->ends_at_zv_p
13612 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13613 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13614 else if (row->height > window_box_height (w))
13615 {
13616 /* If we end up in a partially visible line, let's
13617 make it fully visible, except when it's taller
13618 than the window, in which case we can't do much
13619 about it. */
13620 *scroll_step = 1;
13621 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13622 }
13623 else
13624 {
13625 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13626 if (!cursor_row_fully_visible_p (w, 0, 1))
13627 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13628 else
13629 rc = CURSOR_MOVEMENT_SUCCESS;
13630 }
13631 }
13632 else if (scroll_p)
13633 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13634 else if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13635 {
13636 /* With bidi-reordered rows, there could be more than
13637 one candidate row whose start and end positions
13638 occlude point. We need to let set_cursor_from_row
13639 find the best candidate. */
13640 /* FIXME: Revisit this when glyph ``spilling'' in
13641 continuation lines' rows is implemented for
13642 bidi-reordered rows. */
13643 int rv = 0;
13644
13645 do
13646 {
13647 rv |= set_cursor_from_row (w, row, w->current_matrix,
13648 0, 0, 0, 0);
13649 /* As soon as we've found the first suitable row
13650 whose ends_at_zv_p flag is set, we are done. */
13651 if (rv
13652 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13653 {
13654 rc = CURSOR_MOVEMENT_SUCCESS;
13655 break;
13656 }
13657 ++row;
13658 }
13659 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13660 && MATRIX_ROW_START_CHARPOS (row) <= PT
13661 && PT <= MATRIX_ROW_END_CHARPOS (row)
13662 && cursor_row_p (w, row));
13663 /* If we didn't find any candidate rows, or exited the
13664 loop before all the candidates were examined, signal
13665 to the caller that this method failed. */
13666 if (rc != CURSOR_MOVEMENT_SUCCESS
13667 && (!rv
13668 || (MATRIX_ROW_START_CHARPOS (row) <= PT
13669 && PT <= MATRIX_ROW_END_CHARPOS (row))))
13670 rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13671 else
13672 rc = CURSOR_MOVEMENT_SUCCESS;
13673 }
13674 else
13675 {
13676 do
13677 {
13678 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13679 {
13680 rc = CURSOR_MOVEMENT_SUCCESS;
13681 break;
13682 }
13683 ++row;
13684 }
13685 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13686 && MATRIX_ROW_START_CHARPOS (row) == PT
13687 && cursor_row_p (w, row));
13688 }
13689 }
13690 }
13691
13692 return rc;
13693 }
13694
13695 void
13696 set_vertical_scroll_bar (w)
13697 struct window *w;
13698 {
13699 int start, end, whole;
13700
13701 /* Calculate the start and end positions for the current window.
13702 At some point, it would be nice to choose between scrollbars
13703 which reflect the whole buffer size, with special markers
13704 indicating narrowing, and scrollbars which reflect only the
13705 visible region.
13706
13707 Note that mini-buffers sometimes aren't displaying any text. */
13708 if (!MINI_WINDOW_P (w)
13709 || (w == XWINDOW (minibuf_window)
13710 && NILP (echo_area_buffer[0])))
13711 {
13712 struct buffer *buf = XBUFFER (w->buffer);
13713 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13714 start = marker_position (w->start) - BUF_BEGV (buf);
13715 /* I don't think this is guaranteed to be right. For the
13716 moment, we'll pretend it is. */
13717 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13718
13719 if (end < start)
13720 end = start;
13721 if (whole < (end - start))
13722 whole = end - start;
13723 }
13724 else
13725 start = end = whole = 0;
13726
13727 /* Indicate what this scroll bar ought to be displaying now. */
13728 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13729 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13730 (w, end - start, whole, start);
13731 }
13732
13733
13734 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13735 selected_window is redisplayed.
13736
13737 We can return without actually redisplaying the window if
13738 fonts_changed_p is nonzero. In that case, redisplay_internal will
13739 retry. */
13740
13741 static void
13742 redisplay_window (window, just_this_one_p)
13743 Lisp_Object window;
13744 int just_this_one_p;
13745 {
13746 struct window *w = XWINDOW (window);
13747 struct frame *f = XFRAME (w->frame);
13748 struct buffer *buffer = XBUFFER (w->buffer);
13749 struct buffer *old = current_buffer;
13750 struct text_pos lpoint, opoint, startp;
13751 int update_mode_line;
13752 int tem;
13753 struct it it;
13754 /* Record it now because it's overwritten. */
13755 int current_matrix_up_to_date_p = 0;
13756 int used_current_matrix_p = 0;
13757 /* This is less strict than current_matrix_up_to_date_p.
13758 It indictes that the buffer contents and narrowing are unchanged. */
13759 int buffer_unchanged_p = 0;
13760 int temp_scroll_step = 0;
13761 int count = SPECPDL_INDEX ();
13762 int rc;
13763 int centering_position = -1;
13764 int last_line_misfit = 0;
13765 int beg_unchanged, end_unchanged;
13766
13767 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13768 opoint = lpoint;
13769
13770 /* W must be a leaf window here. */
13771 xassert (!NILP (w->buffer));
13772 #if GLYPH_DEBUG
13773 *w->desired_matrix->method = 0;
13774 #endif
13775
13776 restart:
13777 reconsider_clip_changes (w, buffer);
13778
13779 /* Has the mode line to be updated? */
13780 update_mode_line = (!NILP (w->update_mode_line)
13781 || update_mode_lines
13782 || buffer->clip_changed
13783 || buffer->prevent_redisplay_optimizations_p);
13784
13785 if (MINI_WINDOW_P (w))
13786 {
13787 if (w == XWINDOW (echo_area_window)
13788 && !NILP (echo_area_buffer[0]))
13789 {
13790 if (update_mode_line)
13791 /* We may have to update a tty frame's menu bar or a
13792 tool-bar. Example `M-x C-h C-h C-g'. */
13793 goto finish_menu_bars;
13794 else
13795 /* We've already displayed the echo area glyphs in this window. */
13796 goto finish_scroll_bars;
13797 }
13798 else if ((w != XWINDOW (minibuf_window)
13799 || minibuf_level == 0)
13800 /* When buffer is nonempty, redisplay window normally. */
13801 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13802 /* Quail displays non-mini buffers in minibuffer window.
13803 In that case, redisplay the window normally. */
13804 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13805 {
13806 /* W is a mini-buffer window, but it's not active, so clear
13807 it. */
13808 int yb = window_text_bottom_y (w);
13809 struct glyph_row *row;
13810 int y;
13811
13812 for (y = 0, row = w->desired_matrix->rows;
13813 y < yb;
13814 y += row->height, ++row)
13815 blank_row (w, row, y);
13816 goto finish_scroll_bars;
13817 }
13818
13819 clear_glyph_matrix (w->desired_matrix);
13820 }
13821
13822 /* Otherwise set up data on this window; select its buffer and point
13823 value. */
13824 /* Really select the buffer, for the sake of buffer-local
13825 variables. */
13826 set_buffer_internal_1 (XBUFFER (w->buffer));
13827
13828 current_matrix_up_to_date_p
13829 = (!NILP (w->window_end_valid)
13830 && !current_buffer->clip_changed
13831 && !current_buffer->prevent_redisplay_optimizations_p
13832 && XFASTINT (w->last_modified) >= MODIFF
13833 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13834
13835 /* Run the window-bottom-change-functions
13836 if it is possible that the text on the screen has changed
13837 (either due to modification of the text, or any other reason). */
13838 if (!current_matrix_up_to_date_p
13839 && !NILP (Vwindow_text_change_functions))
13840 {
13841 safe_run_hooks (Qwindow_text_change_functions);
13842 goto restart;
13843 }
13844
13845 beg_unchanged = BEG_UNCHANGED;
13846 end_unchanged = END_UNCHANGED;
13847
13848 SET_TEXT_POS (opoint, PT, PT_BYTE);
13849
13850 specbind (Qinhibit_point_motion_hooks, Qt);
13851
13852 buffer_unchanged_p
13853 = (!NILP (w->window_end_valid)
13854 && !current_buffer->clip_changed
13855 && XFASTINT (w->last_modified) >= MODIFF
13856 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13857
13858 /* When windows_or_buffers_changed is non-zero, we can't rely on
13859 the window end being valid, so set it to nil there. */
13860 if (windows_or_buffers_changed)
13861 {
13862 /* If window starts on a continuation line, maybe adjust the
13863 window start in case the window's width changed. */
13864 if (XMARKER (w->start)->buffer == current_buffer)
13865 compute_window_start_on_continuation_line (w);
13866
13867 w->window_end_valid = Qnil;
13868 }
13869
13870 /* Some sanity checks. */
13871 CHECK_WINDOW_END (w);
13872 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13873 abort ();
13874 if (BYTEPOS (opoint) < CHARPOS (opoint))
13875 abort ();
13876
13877 /* If %c is in mode line, update it if needed. */
13878 if (!NILP (w->column_number_displayed)
13879 /* This alternative quickly identifies a common case
13880 where no change is needed. */
13881 && !(PT == XFASTINT (w->last_point)
13882 && XFASTINT (w->last_modified) >= MODIFF
13883 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13884 && (XFASTINT (w->column_number_displayed)
13885 != (int) current_column ())) /* iftc */
13886 update_mode_line = 1;
13887
13888 /* Count number of windows showing the selected buffer. An indirect
13889 buffer counts as its base buffer. */
13890 if (!just_this_one_p)
13891 {
13892 struct buffer *current_base, *window_base;
13893 current_base = current_buffer;
13894 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13895 if (current_base->base_buffer)
13896 current_base = current_base->base_buffer;
13897 if (window_base->base_buffer)
13898 window_base = window_base->base_buffer;
13899 if (current_base == window_base)
13900 buffer_shared++;
13901 }
13902
13903 /* Point refers normally to the selected window. For any other
13904 window, set up appropriate value. */
13905 if (!EQ (window, selected_window))
13906 {
13907 int new_pt = XMARKER (w->pointm)->charpos;
13908 int new_pt_byte = marker_byte_position (w->pointm);
13909 if (new_pt < BEGV)
13910 {
13911 new_pt = BEGV;
13912 new_pt_byte = BEGV_BYTE;
13913 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13914 }
13915 else if (new_pt > (ZV - 1))
13916 {
13917 new_pt = ZV;
13918 new_pt_byte = ZV_BYTE;
13919 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13920 }
13921
13922 /* We don't use SET_PT so that the point-motion hooks don't run. */
13923 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13924 }
13925
13926 /* If any of the character widths specified in the display table
13927 have changed, invalidate the width run cache. It's true that
13928 this may be a bit late to catch such changes, but the rest of
13929 redisplay goes (non-fatally) haywire when the display table is
13930 changed, so why should we worry about doing any better? */
13931 if (current_buffer->width_run_cache)
13932 {
13933 struct Lisp_Char_Table *disptab = buffer_display_table ();
13934
13935 if (! disptab_matches_widthtab (disptab,
13936 XVECTOR (current_buffer->width_table)))
13937 {
13938 invalidate_region_cache (current_buffer,
13939 current_buffer->width_run_cache,
13940 BEG, Z);
13941 recompute_width_table (current_buffer, disptab);
13942 }
13943 }
13944
13945 /* If window-start is screwed up, choose a new one. */
13946 if (XMARKER (w->start)->buffer != current_buffer)
13947 goto recenter;
13948
13949 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13950
13951 /* If someone specified a new starting point but did not insist,
13952 check whether it can be used. */
13953 if (!NILP (w->optional_new_start)
13954 && CHARPOS (startp) >= BEGV
13955 && CHARPOS (startp) <= ZV)
13956 {
13957 w->optional_new_start = Qnil;
13958 start_display (&it, w, startp);
13959 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13960 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13961 if (IT_CHARPOS (it) == PT)
13962 w->force_start = Qt;
13963 /* IT may overshoot PT if text at PT is invisible. */
13964 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13965 w->force_start = Qt;
13966 }
13967
13968 force_start:
13969
13970 /* Handle case where place to start displaying has been specified,
13971 unless the specified location is outside the accessible range. */
13972 if (!NILP (w->force_start)
13973 || w->frozen_window_start_p)
13974 {
13975 /* We set this later on if we have to adjust point. */
13976 int new_vpos = -1;
13977
13978 w->force_start = Qnil;
13979 w->vscroll = 0;
13980 w->window_end_valid = Qnil;
13981
13982 /* Forget any recorded base line for line number display. */
13983 if (!buffer_unchanged_p)
13984 w->base_line_number = Qnil;
13985
13986 /* Redisplay the mode line. Select the buffer properly for that.
13987 Also, run the hook window-scroll-functions
13988 because we have scrolled. */
13989 /* Note, we do this after clearing force_start because
13990 if there's an error, it is better to forget about force_start
13991 than to get into an infinite loop calling the hook functions
13992 and having them get more errors. */
13993 if (!update_mode_line
13994 || ! NILP (Vwindow_scroll_functions))
13995 {
13996 update_mode_line = 1;
13997 w->update_mode_line = Qt;
13998 startp = run_window_scroll_functions (window, startp);
13999 }
14000
14001 w->last_modified = make_number (0);
14002 w->last_overlay_modified = make_number (0);
14003 if (CHARPOS (startp) < BEGV)
14004 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14005 else if (CHARPOS (startp) > ZV)
14006 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14007
14008 /* Redisplay, then check if cursor has been set during the
14009 redisplay. Give up if new fonts were loaded. */
14010 /* We used to issue a CHECK_MARGINS argument to try_window here,
14011 but this causes scrolling to fail when point begins inside
14012 the scroll margin (bug#148) -- cyd */
14013 if (!try_window (window, startp, 0))
14014 {
14015 w->force_start = Qt;
14016 clear_glyph_matrix (w->desired_matrix);
14017 goto need_larger_matrices;
14018 }
14019
14020 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14021 {
14022 /* If point does not appear, try to move point so it does
14023 appear. The desired matrix has been built above, so we
14024 can use it here. */
14025 new_vpos = window_box_height (w) / 2;
14026 }
14027
14028 if (!cursor_row_fully_visible_p (w, 0, 0))
14029 {
14030 /* Point does appear, but on a line partly visible at end of window.
14031 Move it back to a fully-visible line. */
14032 new_vpos = window_box_height (w);
14033 }
14034
14035 /* If we need to move point for either of the above reasons,
14036 now actually do it. */
14037 if (new_vpos >= 0)
14038 {
14039 struct glyph_row *row;
14040
14041 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14042 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14043 ++row;
14044
14045 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14046 MATRIX_ROW_START_BYTEPOS (row));
14047
14048 if (w != XWINDOW (selected_window))
14049 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14050 else if (current_buffer == old)
14051 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14052
14053 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14054
14055 /* If we are highlighting the region, then we just changed
14056 the region, so redisplay to show it. */
14057 if (!NILP (Vtransient_mark_mode)
14058 && !NILP (current_buffer->mark_active))
14059 {
14060 clear_glyph_matrix (w->desired_matrix);
14061 if (!try_window (window, startp, 0))
14062 goto need_larger_matrices;
14063 }
14064 }
14065
14066 #if GLYPH_DEBUG
14067 debug_method_add (w, "forced window start");
14068 #endif
14069 goto done;
14070 }
14071
14072 /* Handle case where text has not changed, only point, and it has
14073 not moved off the frame, and we are not retrying after hscroll.
14074 (current_matrix_up_to_date_p is nonzero when retrying.) */
14075 if (current_matrix_up_to_date_p
14076 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14077 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14078 {
14079 switch (rc)
14080 {
14081 case CURSOR_MOVEMENT_SUCCESS:
14082 used_current_matrix_p = 1;
14083 goto done;
14084
14085 case CURSOR_MOVEMENT_MUST_SCROLL:
14086 goto try_to_scroll;
14087
14088 default:
14089 abort ();
14090 }
14091 }
14092 /* If current starting point was originally the beginning of a line
14093 but no longer is, find a new starting point. */
14094 else if (!NILP (w->start_at_line_beg)
14095 && !(CHARPOS (startp) <= BEGV
14096 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14097 {
14098 #if GLYPH_DEBUG
14099 debug_method_add (w, "recenter 1");
14100 #endif
14101 goto recenter;
14102 }
14103
14104 /* Try scrolling with try_window_id. Value is > 0 if update has
14105 been done, it is -1 if we know that the same window start will
14106 not work. It is 0 if unsuccessful for some other reason. */
14107 else if ((tem = try_window_id (w)) != 0)
14108 {
14109 #if GLYPH_DEBUG
14110 debug_method_add (w, "try_window_id %d", tem);
14111 #endif
14112
14113 if (fonts_changed_p)
14114 goto need_larger_matrices;
14115 if (tem > 0)
14116 goto done;
14117
14118 /* Otherwise try_window_id has returned -1 which means that we
14119 don't want the alternative below this comment to execute. */
14120 }
14121 else if (CHARPOS (startp) >= BEGV
14122 && CHARPOS (startp) <= ZV
14123 && PT >= CHARPOS (startp)
14124 && (CHARPOS (startp) < ZV
14125 /* Avoid starting at end of buffer. */
14126 || CHARPOS (startp) == BEGV
14127 || (XFASTINT (w->last_modified) >= MODIFF
14128 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14129 {
14130
14131 /* If first window line is a continuation line, and window start
14132 is inside the modified region, but the first change is before
14133 current window start, we must select a new window start.
14134
14135 However, if this is the result of a down-mouse event (e.g. by
14136 extending the mouse-drag-overlay), we don't want to select a
14137 new window start, since that would change the position under
14138 the mouse, resulting in an unwanted mouse-movement rather
14139 than a simple mouse-click. */
14140 if (NILP (w->start_at_line_beg)
14141 && NILP (do_mouse_tracking)
14142 && CHARPOS (startp) > BEGV
14143 && CHARPOS (startp) > BEG + beg_unchanged
14144 && CHARPOS (startp) <= Z - end_unchanged
14145 /* Even if w->start_at_line_beg is nil, a new window may
14146 start at a line_beg, since that's how set_buffer_window
14147 sets it. So, we need to check the return value of
14148 compute_window_start_on_continuation_line. (See also
14149 bug#197). */
14150 && XMARKER (w->start)->buffer == current_buffer
14151 && compute_window_start_on_continuation_line (w))
14152 {
14153 w->force_start = Qt;
14154 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14155 goto force_start;
14156 }
14157
14158 #if GLYPH_DEBUG
14159 debug_method_add (w, "same window start");
14160 #endif
14161
14162 /* Try to redisplay starting at same place as before.
14163 If point has not moved off frame, accept the results. */
14164 if (!current_matrix_up_to_date_p
14165 /* Don't use try_window_reusing_current_matrix in this case
14166 because a window scroll function can have changed the
14167 buffer. */
14168 || !NILP (Vwindow_scroll_functions)
14169 || MINI_WINDOW_P (w)
14170 || !(used_current_matrix_p
14171 = try_window_reusing_current_matrix (w)))
14172 {
14173 IF_DEBUG (debug_method_add (w, "1"));
14174 if (try_window (window, startp, 1) < 0)
14175 /* -1 means we need to scroll.
14176 0 means we need new matrices, but fonts_changed_p
14177 is set in that case, so we will detect it below. */
14178 goto try_to_scroll;
14179 }
14180
14181 if (fonts_changed_p)
14182 goto need_larger_matrices;
14183
14184 if (w->cursor.vpos >= 0)
14185 {
14186 if (!just_this_one_p
14187 || current_buffer->clip_changed
14188 || BEG_UNCHANGED < CHARPOS (startp))
14189 /* Forget any recorded base line for line number display. */
14190 w->base_line_number = Qnil;
14191
14192 if (!cursor_row_fully_visible_p (w, 1, 0))
14193 {
14194 clear_glyph_matrix (w->desired_matrix);
14195 last_line_misfit = 1;
14196 }
14197 /* Drop through and scroll. */
14198 else
14199 goto done;
14200 }
14201 else
14202 clear_glyph_matrix (w->desired_matrix);
14203 }
14204
14205 try_to_scroll:
14206
14207 w->last_modified = make_number (0);
14208 w->last_overlay_modified = make_number (0);
14209
14210 /* Redisplay the mode line. Select the buffer properly for that. */
14211 if (!update_mode_line)
14212 {
14213 update_mode_line = 1;
14214 w->update_mode_line = Qt;
14215 }
14216
14217 /* Try to scroll by specified few lines. */
14218 if ((scroll_conservatively
14219 || scroll_step
14220 || temp_scroll_step
14221 || NUMBERP (current_buffer->scroll_up_aggressively)
14222 || NUMBERP (current_buffer->scroll_down_aggressively))
14223 && !current_buffer->clip_changed
14224 && CHARPOS (startp) >= BEGV
14225 && CHARPOS (startp) <= ZV)
14226 {
14227 /* The function returns -1 if new fonts were loaded, 1 if
14228 successful, 0 if not successful. */
14229 int rc = try_scrolling (window, just_this_one_p,
14230 scroll_conservatively,
14231 scroll_step,
14232 temp_scroll_step, last_line_misfit);
14233 switch (rc)
14234 {
14235 case SCROLLING_SUCCESS:
14236 goto done;
14237
14238 case SCROLLING_NEED_LARGER_MATRICES:
14239 goto need_larger_matrices;
14240
14241 case SCROLLING_FAILED:
14242 break;
14243
14244 default:
14245 abort ();
14246 }
14247 }
14248
14249 /* Finally, just choose place to start which centers point */
14250
14251 recenter:
14252 if (centering_position < 0)
14253 centering_position = window_box_height (w) / 2;
14254
14255 #if GLYPH_DEBUG
14256 debug_method_add (w, "recenter");
14257 #endif
14258
14259 /* w->vscroll = 0; */
14260
14261 /* Forget any previously recorded base line for line number display. */
14262 if (!buffer_unchanged_p)
14263 w->base_line_number = Qnil;
14264
14265 /* Move backward half the height of the window. */
14266 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14267 it.current_y = it.last_visible_y;
14268 move_it_vertically_backward (&it, centering_position);
14269 xassert (IT_CHARPOS (it) >= BEGV);
14270
14271 /* The function move_it_vertically_backward may move over more
14272 than the specified y-distance. If it->w is small, e.g. a
14273 mini-buffer window, we may end up in front of the window's
14274 display area. Start displaying at the start of the line
14275 containing PT in this case. */
14276 if (it.current_y <= 0)
14277 {
14278 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14279 move_it_vertically_backward (&it, 0);
14280 it.current_y = 0;
14281 }
14282
14283 it.current_x = it.hpos = 0;
14284
14285 /* Set startp here explicitly in case that helps avoid an infinite loop
14286 in case the window-scroll-functions functions get errors. */
14287 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14288
14289 /* Run scroll hooks. */
14290 startp = run_window_scroll_functions (window, it.current.pos);
14291
14292 /* Redisplay the window. */
14293 if (!current_matrix_up_to_date_p
14294 || windows_or_buffers_changed
14295 || cursor_type_changed
14296 /* Don't use try_window_reusing_current_matrix in this case
14297 because it can have changed the buffer. */
14298 || !NILP (Vwindow_scroll_functions)
14299 || !just_this_one_p
14300 || MINI_WINDOW_P (w)
14301 || !(used_current_matrix_p
14302 = try_window_reusing_current_matrix (w)))
14303 try_window (window, startp, 0);
14304
14305 /* If new fonts have been loaded (due to fontsets), give up. We
14306 have to start a new redisplay since we need to re-adjust glyph
14307 matrices. */
14308 if (fonts_changed_p)
14309 goto need_larger_matrices;
14310
14311 /* If cursor did not appear assume that the middle of the window is
14312 in the first line of the window. Do it again with the next line.
14313 (Imagine a window of height 100, displaying two lines of height
14314 60. Moving back 50 from it->last_visible_y will end in the first
14315 line.) */
14316 if (w->cursor.vpos < 0)
14317 {
14318 if (!NILP (w->window_end_valid)
14319 && PT >= Z - XFASTINT (w->window_end_pos))
14320 {
14321 clear_glyph_matrix (w->desired_matrix);
14322 move_it_by_lines (&it, 1, 0);
14323 try_window (window, it.current.pos, 0);
14324 }
14325 else if (PT < IT_CHARPOS (it))
14326 {
14327 clear_glyph_matrix (w->desired_matrix);
14328 move_it_by_lines (&it, -1, 0);
14329 try_window (window, it.current.pos, 0);
14330 }
14331 else
14332 {
14333 /* Not much we can do about it. */
14334 }
14335 }
14336
14337 /* Consider the following case: Window starts at BEGV, there is
14338 invisible, intangible text at BEGV, so that display starts at
14339 some point START > BEGV. It can happen that we are called with
14340 PT somewhere between BEGV and START. Try to handle that case. */
14341 if (w->cursor.vpos < 0)
14342 {
14343 struct glyph_row *row = w->current_matrix->rows;
14344 if (row->mode_line_p)
14345 ++row;
14346 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14347 }
14348
14349 if (!cursor_row_fully_visible_p (w, 0, 0))
14350 {
14351 /* If vscroll is enabled, disable it and try again. */
14352 if (w->vscroll)
14353 {
14354 w->vscroll = 0;
14355 clear_glyph_matrix (w->desired_matrix);
14356 goto recenter;
14357 }
14358
14359 /* If centering point failed to make the whole line visible,
14360 put point at the top instead. That has to make the whole line
14361 visible, if it can be done. */
14362 if (centering_position == 0)
14363 goto done;
14364
14365 clear_glyph_matrix (w->desired_matrix);
14366 centering_position = 0;
14367 goto recenter;
14368 }
14369
14370 done:
14371
14372 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14373 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14374 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14375 ? Qt : Qnil);
14376
14377 /* Display the mode line, if we must. */
14378 if ((update_mode_line
14379 /* If window not full width, must redo its mode line
14380 if (a) the window to its side is being redone and
14381 (b) we do a frame-based redisplay. This is a consequence
14382 of how inverted lines are drawn in frame-based redisplay. */
14383 || (!just_this_one_p
14384 && !FRAME_WINDOW_P (f)
14385 && !WINDOW_FULL_WIDTH_P (w))
14386 /* Line number to display. */
14387 || INTEGERP (w->base_line_pos)
14388 /* Column number is displayed and different from the one displayed. */
14389 || (!NILP (w->column_number_displayed)
14390 && (XFASTINT (w->column_number_displayed)
14391 != (int) current_column ()))) /* iftc */
14392 /* This means that the window has a mode line. */
14393 && (WINDOW_WANTS_MODELINE_P (w)
14394 || WINDOW_WANTS_HEADER_LINE_P (w)))
14395 {
14396 display_mode_lines (w);
14397
14398 /* If mode line height has changed, arrange for a thorough
14399 immediate redisplay using the correct mode line height. */
14400 if (WINDOW_WANTS_MODELINE_P (w)
14401 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14402 {
14403 fonts_changed_p = 1;
14404 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14405 = DESIRED_MODE_LINE_HEIGHT (w);
14406 }
14407
14408 /* If header line height has changed, arrange for a thorough
14409 immediate redisplay using the correct header line height. */
14410 if (WINDOW_WANTS_HEADER_LINE_P (w)
14411 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14412 {
14413 fonts_changed_p = 1;
14414 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14415 = DESIRED_HEADER_LINE_HEIGHT (w);
14416 }
14417
14418 if (fonts_changed_p)
14419 goto need_larger_matrices;
14420 }
14421
14422 if (!line_number_displayed
14423 && !BUFFERP (w->base_line_pos))
14424 {
14425 w->base_line_pos = Qnil;
14426 w->base_line_number = Qnil;
14427 }
14428
14429 finish_menu_bars:
14430
14431 /* When we reach a frame's selected window, redo the frame's menu bar. */
14432 if (update_mode_line
14433 && EQ (FRAME_SELECTED_WINDOW (f), window))
14434 {
14435 int redisplay_menu_p = 0;
14436 int redisplay_tool_bar_p = 0;
14437
14438 if (FRAME_WINDOW_P (f))
14439 {
14440 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14441 || defined (HAVE_NS) || defined (USE_GTK)
14442 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14443 #else
14444 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14445 #endif
14446 }
14447 else
14448 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14449
14450 if (redisplay_menu_p)
14451 display_menu_bar (w);
14452
14453 #ifdef HAVE_WINDOW_SYSTEM
14454 if (FRAME_WINDOW_P (f))
14455 {
14456 #if defined (USE_GTK) || defined (HAVE_NS)
14457 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14458 #else
14459 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14460 && (FRAME_TOOL_BAR_LINES (f) > 0
14461 || !NILP (Vauto_resize_tool_bars));
14462 #endif
14463
14464 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14465 {
14466 extern int ignore_mouse_drag_p;
14467 ignore_mouse_drag_p = 1;
14468 }
14469 }
14470 #endif
14471 }
14472
14473 #ifdef HAVE_WINDOW_SYSTEM
14474 if (FRAME_WINDOW_P (f)
14475 && update_window_fringes (w, (just_this_one_p
14476 || (!used_current_matrix_p && !overlay_arrow_seen)
14477 || w->pseudo_window_p)))
14478 {
14479 update_begin (f);
14480 BLOCK_INPUT;
14481 if (draw_window_fringes (w, 1))
14482 x_draw_vertical_border (w);
14483 UNBLOCK_INPUT;
14484 update_end (f);
14485 }
14486 #endif /* HAVE_WINDOW_SYSTEM */
14487
14488 /* We go to this label, with fonts_changed_p nonzero,
14489 if it is necessary to try again using larger glyph matrices.
14490 We have to redeem the scroll bar even in this case,
14491 because the loop in redisplay_internal expects that. */
14492 need_larger_matrices:
14493 ;
14494 finish_scroll_bars:
14495
14496 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14497 {
14498 /* Set the thumb's position and size. */
14499 set_vertical_scroll_bar (w);
14500
14501 /* Note that we actually used the scroll bar attached to this
14502 window, so it shouldn't be deleted at the end of redisplay. */
14503 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14504 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14505 }
14506
14507 /* Restore current_buffer and value of point in it. */
14508 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14509 set_buffer_internal_1 (old);
14510 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14511 shorter. This can be caused by log truncation in *Messages*. */
14512 if (CHARPOS (lpoint) <= ZV)
14513 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14514
14515 unbind_to (count, Qnil);
14516 }
14517
14518
14519 /* Build the complete desired matrix of WINDOW with a window start
14520 buffer position POS.
14521
14522 Value is 1 if successful. It is zero if fonts were loaded during
14523 redisplay which makes re-adjusting glyph matrices necessary, and -1
14524 if point would appear in the scroll margins.
14525 (We check that only if CHECK_MARGINS is nonzero. */
14526
14527 int
14528 try_window (window, pos, check_margins)
14529 Lisp_Object window;
14530 struct text_pos pos;
14531 int check_margins;
14532 {
14533 struct window *w = XWINDOW (window);
14534 struct it it;
14535 struct glyph_row *last_text_row = NULL;
14536 struct frame *f = XFRAME (w->frame);
14537
14538 /* Make POS the new window start. */
14539 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14540
14541 /* Mark cursor position as unknown. No overlay arrow seen. */
14542 w->cursor.vpos = -1;
14543 overlay_arrow_seen = 0;
14544
14545 /* Initialize iterator and info to start at POS. */
14546 start_display (&it, w, pos);
14547
14548 /* Display all lines of W. */
14549 while (it.current_y < it.last_visible_y)
14550 {
14551 if (display_line (&it))
14552 last_text_row = it.glyph_row - 1;
14553 if (fonts_changed_p)
14554 return 0;
14555 }
14556
14557 /* Don't let the cursor end in the scroll margins. */
14558 if (check_margins
14559 && !MINI_WINDOW_P (w))
14560 {
14561 int this_scroll_margin;
14562
14563 if (scroll_margin > 0)
14564 {
14565 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14566 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14567 }
14568 else
14569 this_scroll_margin = 0;
14570
14571 if ((w->cursor.y >= 0 /* not vscrolled */
14572 && w->cursor.y < this_scroll_margin
14573 && CHARPOS (pos) > BEGV
14574 && IT_CHARPOS (it) < ZV)
14575 /* rms: considering make_cursor_line_fully_visible_p here
14576 seems to give wrong results. We don't want to recenter
14577 when the last line is partly visible, we want to allow
14578 that case to be handled in the usual way. */
14579 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14580 {
14581 w->cursor.vpos = -1;
14582 clear_glyph_matrix (w->desired_matrix);
14583 return -1;
14584 }
14585 }
14586
14587 /* If bottom moved off end of frame, change mode line percentage. */
14588 if (XFASTINT (w->window_end_pos) <= 0
14589 && Z != IT_CHARPOS (it))
14590 w->update_mode_line = Qt;
14591
14592 /* Set window_end_pos to the offset of the last character displayed
14593 on the window from the end of current_buffer. Set
14594 window_end_vpos to its row number. */
14595 if (last_text_row)
14596 {
14597 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14598 w->window_end_bytepos
14599 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14600 w->window_end_pos
14601 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14602 w->window_end_vpos
14603 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14604 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14605 ->displays_text_p);
14606 }
14607 else
14608 {
14609 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14610 w->window_end_pos = make_number (Z - ZV);
14611 w->window_end_vpos = make_number (0);
14612 }
14613
14614 /* But that is not valid info until redisplay finishes. */
14615 w->window_end_valid = Qnil;
14616 return 1;
14617 }
14618
14619
14620 \f
14621 /************************************************************************
14622 Window redisplay reusing current matrix when buffer has not changed
14623 ************************************************************************/
14624
14625 /* Try redisplay of window W showing an unchanged buffer with a
14626 different window start than the last time it was displayed by
14627 reusing its current matrix. Value is non-zero if successful.
14628 W->start is the new window start. */
14629
14630 static int
14631 try_window_reusing_current_matrix (w)
14632 struct window *w;
14633 {
14634 struct frame *f = XFRAME (w->frame);
14635 struct glyph_row *row, *bottom_row;
14636 struct it it;
14637 struct run run;
14638 struct text_pos start, new_start;
14639 int nrows_scrolled, i;
14640 struct glyph_row *last_text_row;
14641 struct glyph_row *last_reused_text_row;
14642 struct glyph_row *start_row;
14643 int start_vpos, min_y, max_y;
14644
14645 #if GLYPH_DEBUG
14646 if (inhibit_try_window_reusing)
14647 return 0;
14648 #endif
14649
14650 if (/* This function doesn't handle terminal frames. */
14651 !FRAME_WINDOW_P (f)
14652 /* Don't try to reuse the display if windows have been split
14653 or such. */
14654 || windows_or_buffers_changed
14655 || cursor_type_changed)
14656 return 0;
14657
14658 /* Can't do this if region may have changed. */
14659 if ((!NILP (Vtransient_mark_mode)
14660 && !NILP (current_buffer->mark_active))
14661 || !NILP (w->region_showing)
14662 || !NILP (Vshow_trailing_whitespace))
14663 return 0;
14664
14665 /* If top-line visibility has changed, give up. */
14666 if (WINDOW_WANTS_HEADER_LINE_P (w)
14667 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14668 return 0;
14669
14670 /* Give up if old or new display is scrolled vertically. We could
14671 make this function handle this, but right now it doesn't. */
14672 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14673 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14674 return 0;
14675
14676 /* The variable new_start now holds the new window start. The old
14677 start `start' can be determined from the current matrix. */
14678 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14679 start = start_row->start.pos;
14680 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14681
14682 /* Clear the desired matrix for the display below. */
14683 clear_glyph_matrix (w->desired_matrix);
14684
14685 if (CHARPOS (new_start) <= CHARPOS (start))
14686 {
14687 int first_row_y;
14688
14689 /* Don't use this method if the display starts with an ellipsis
14690 displayed for invisible text. It's not easy to handle that case
14691 below, and it's certainly not worth the effort since this is
14692 not a frequent case. */
14693 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14694 return 0;
14695
14696 IF_DEBUG (debug_method_add (w, "twu1"));
14697
14698 /* Display up to a row that can be reused. The variable
14699 last_text_row is set to the last row displayed that displays
14700 text. Note that it.vpos == 0 if or if not there is a
14701 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14702 start_display (&it, w, new_start);
14703 first_row_y = it.current_y;
14704 w->cursor.vpos = -1;
14705 last_text_row = last_reused_text_row = NULL;
14706
14707 while (it.current_y < it.last_visible_y
14708 && !fonts_changed_p)
14709 {
14710 /* If we have reached into the characters in the START row,
14711 that means the line boundaries have changed. So we
14712 can't start copying with the row START. Maybe it will
14713 work to start copying with the following row. */
14714 while (IT_CHARPOS (it) > CHARPOS (start))
14715 {
14716 /* Advance to the next row as the "start". */
14717 start_row++;
14718 start = start_row->start.pos;
14719 /* If there are no more rows to try, or just one, give up. */
14720 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14721 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14722 || CHARPOS (start) == ZV)
14723 {
14724 clear_glyph_matrix (w->desired_matrix);
14725 return 0;
14726 }
14727
14728 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14729 }
14730 /* If we have reached alignment,
14731 we can copy the rest of the rows. */
14732 if (IT_CHARPOS (it) == CHARPOS (start))
14733 break;
14734
14735 if (display_line (&it))
14736 last_text_row = it.glyph_row - 1;
14737 }
14738
14739 /* A value of current_y < last_visible_y means that we stopped
14740 at the previous window start, which in turn means that we
14741 have at least one reusable row. */
14742 if (it.current_y < it.last_visible_y)
14743 {
14744 /* IT.vpos always starts from 0; it counts text lines. */
14745 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14746
14747 /* Find PT if not already found in the lines displayed. */
14748 if (w->cursor.vpos < 0)
14749 {
14750 int dy = it.current_y - start_row->y;
14751
14752 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14753 row = row_containing_pos (w, PT, row, NULL, dy);
14754 if (row)
14755 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14756 dy, nrows_scrolled);
14757 else
14758 {
14759 clear_glyph_matrix (w->desired_matrix);
14760 return 0;
14761 }
14762 }
14763
14764 /* Scroll the display. Do it before the current matrix is
14765 changed. The problem here is that update has not yet
14766 run, i.e. part of the current matrix is not up to date.
14767 scroll_run_hook will clear the cursor, and use the
14768 current matrix to get the height of the row the cursor is
14769 in. */
14770 run.current_y = start_row->y;
14771 run.desired_y = it.current_y;
14772 run.height = it.last_visible_y - it.current_y;
14773
14774 if (run.height > 0 && run.current_y != run.desired_y)
14775 {
14776 update_begin (f);
14777 FRAME_RIF (f)->update_window_begin_hook (w);
14778 FRAME_RIF (f)->clear_window_mouse_face (w);
14779 FRAME_RIF (f)->scroll_run_hook (w, &run);
14780 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14781 update_end (f);
14782 }
14783
14784 /* Shift current matrix down by nrows_scrolled lines. */
14785 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14786 rotate_matrix (w->current_matrix,
14787 start_vpos,
14788 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14789 nrows_scrolled);
14790
14791 /* Disable lines that must be updated. */
14792 for (i = 0; i < nrows_scrolled; ++i)
14793 (start_row + i)->enabled_p = 0;
14794
14795 /* Re-compute Y positions. */
14796 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14797 max_y = it.last_visible_y;
14798 for (row = start_row + nrows_scrolled;
14799 row < bottom_row;
14800 ++row)
14801 {
14802 row->y = it.current_y;
14803 row->visible_height = row->height;
14804
14805 if (row->y < min_y)
14806 row->visible_height -= min_y - row->y;
14807 if (row->y + row->height > max_y)
14808 row->visible_height -= row->y + row->height - max_y;
14809 row->redraw_fringe_bitmaps_p = 1;
14810
14811 it.current_y += row->height;
14812
14813 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14814 last_reused_text_row = row;
14815 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14816 break;
14817 }
14818
14819 /* Disable lines in the current matrix which are now
14820 below the window. */
14821 for (++row; row < bottom_row; ++row)
14822 row->enabled_p = row->mode_line_p = 0;
14823 }
14824
14825 /* Update window_end_pos etc.; last_reused_text_row is the last
14826 reused row from the current matrix containing text, if any.
14827 The value of last_text_row is the last displayed line
14828 containing text. */
14829 if (last_reused_text_row)
14830 {
14831 w->window_end_bytepos
14832 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14833 w->window_end_pos
14834 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14835 w->window_end_vpos
14836 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14837 w->current_matrix));
14838 }
14839 else if (last_text_row)
14840 {
14841 w->window_end_bytepos
14842 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14843 w->window_end_pos
14844 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14845 w->window_end_vpos
14846 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14847 }
14848 else
14849 {
14850 /* This window must be completely empty. */
14851 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14852 w->window_end_pos = make_number (Z - ZV);
14853 w->window_end_vpos = make_number (0);
14854 }
14855 w->window_end_valid = Qnil;
14856
14857 /* Update hint: don't try scrolling again in update_window. */
14858 w->desired_matrix->no_scrolling_p = 1;
14859
14860 #if GLYPH_DEBUG
14861 debug_method_add (w, "try_window_reusing_current_matrix 1");
14862 #endif
14863 return 1;
14864 }
14865 else if (CHARPOS (new_start) > CHARPOS (start))
14866 {
14867 struct glyph_row *pt_row, *row;
14868 struct glyph_row *first_reusable_row;
14869 struct glyph_row *first_row_to_display;
14870 int dy;
14871 int yb = window_text_bottom_y (w);
14872
14873 /* Find the row starting at new_start, if there is one. Don't
14874 reuse a partially visible line at the end. */
14875 first_reusable_row = start_row;
14876 while (first_reusable_row->enabled_p
14877 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14878 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14879 < CHARPOS (new_start)))
14880 ++first_reusable_row;
14881
14882 /* Give up if there is no row to reuse. */
14883 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14884 || !first_reusable_row->enabled_p
14885 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14886 != CHARPOS (new_start)))
14887 return 0;
14888
14889 /* We can reuse fully visible rows beginning with
14890 first_reusable_row to the end of the window. Set
14891 first_row_to_display to the first row that cannot be reused.
14892 Set pt_row to the row containing point, if there is any. */
14893 pt_row = NULL;
14894 for (first_row_to_display = first_reusable_row;
14895 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14896 ++first_row_to_display)
14897 {
14898 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14899 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14900 pt_row = first_row_to_display;
14901 }
14902
14903 /* Start displaying at the start of first_row_to_display. */
14904 xassert (first_row_to_display->y < yb);
14905 init_to_row_start (&it, w, first_row_to_display);
14906
14907 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14908 - start_vpos);
14909 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14910 - nrows_scrolled);
14911 it.current_y = (first_row_to_display->y - first_reusable_row->y
14912 + WINDOW_HEADER_LINE_HEIGHT (w));
14913
14914 /* Display lines beginning with first_row_to_display in the
14915 desired matrix. Set last_text_row to the last row displayed
14916 that displays text. */
14917 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14918 if (pt_row == NULL)
14919 w->cursor.vpos = -1;
14920 last_text_row = NULL;
14921 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14922 if (display_line (&it))
14923 last_text_row = it.glyph_row - 1;
14924
14925 /* If point is in a reused row, adjust y and vpos of the cursor
14926 position. */
14927 if (pt_row)
14928 {
14929 w->cursor.vpos -= nrows_scrolled;
14930 w->cursor.y -= first_reusable_row->y - start_row->y;
14931 }
14932
14933 /* Give up if point isn't in a row displayed or reused. (This
14934 also handles the case where w->cursor.vpos < nrows_scrolled
14935 after the calls to display_line, which can happen with scroll
14936 margins. See bug#1295.) */
14937 if (w->cursor.vpos < 0)
14938 {
14939 clear_glyph_matrix (w->desired_matrix);
14940 return 0;
14941 }
14942
14943 /* Scroll the display. */
14944 run.current_y = first_reusable_row->y;
14945 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14946 run.height = it.last_visible_y - run.current_y;
14947 dy = run.current_y - run.desired_y;
14948
14949 if (run.height)
14950 {
14951 update_begin (f);
14952 FRAME_RIF (f)->update_window_begin_hook (w);
14953 FRAME_RIF (f)->clear_window_mouse_face (w);
14954 FRAME_RIF (f)->scroll_run_hook (w, &run);
14955 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14956 update_end (f);
14957 }
14958
14959 /* Adjust Y positions of reused rows. */
14960 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14961 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14962 max_y = it.last_visible_y;
14963 for (row = first_reusable_row; row < first_row_to_display; ++row)
14964 {
14965 row->y -= dy;
14966 row->visible_height = row->height;
14967 if (row->y < min_y)
14968 row->visible_height -= min_y - row->y;
14969 if (row->y + row->height > max_y)
14970 row->visible_height -= row->y + row->height - max_y;
14971 row->redraw_fringe_bitmaps_p = 1;
14972 }
14973
14974 /* Scroll the current matrix. */
14975 xassert (nrows_scrolled > 0);
14976 rotate_matrix (w->current_matrix,
14977 start_vpos,
14978 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14979 -nrows_scrolled);
14980
14981 /* Disable rows not reused. */
14982 for (row -= nrows_scrolled; row < bottom_row; ++row)
14983 row->enabled_p = 0;
14984
14985 /* Point may have moved to a different line, so we cannot assume that
14986 the previous cursor position is valid; locate the correct row. */
14987 if (pt_row)
14988 {
14989 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14990 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14991 row++)
14992 {
14993 w->cursor.vpos++;
14994 w->cursor.y = row->y;
14995 }
14996 if (row < bottom_row)
14997 {
14998 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14999 struct glyph *end = glyph + row->used[TEXT_AREA];
15000 struct glyph *orig_glyph = glyph;
15001 struct cursor_pos orig_cursor = w->cursor;
15002
15003 for (; glyph < end
15004 && (!BUFFERP (glyph->object)
15005 || glyph->charpos != PT);
15006 glyph++)
15007 {
15008 w->cursor.hpos++;
15009 w->cursor.x += glyph->pixel_width;
15010 }
15011 /* With bidi reordering, charpos changes non-linearly
15012 with hpos, so the right glyph could be to the
15013 left. */
15014 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15015 && (!BUFFERP (glyph->object) || glyph->charpos != PT))
15016 {
15017 struct glyph *start_glyph = row->glyphs[TEXT_AREA];
15018
15019 glyph = orig_glyph - 1;
15020 orig_cursor.hpos--;
15021 orig_cursor.x -= glyph->pixel_width;
15022 for (; glyph >= start_glyph
15023 && (!BUFFERP (glyph->object)
15024 || glyph->charpos != PT);
15025 glyph--)
15026 {
15027 w->cursor.hpos--;
15028 w->cursor.x -= glyph->pixel_width;
15029 }
15030 if (BUFFERP (glyph->object) && glyph->charpos == PT)
15031 w->cursor = orig_cursor;
15032 }
15033 }
15034 }
15035
15036 /* Adjust window end. A null value of last_text_row means that
15037 the window end is in reused rows which in turn means that
15038 only its vpos can have changed. */
15039 if (last_text_row)
15040 {
15041 w->window_end_bytepos
15042 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15043 w->window_end_pos
15044 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15045 w->window_end_vpos
15046 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15047 }
15048 else
15049 {
15050 w->window_end_vpos
15051 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15052 }
15053
15054 w->window_end_valid = Qnil;
15055 w->desired_matrix->no_scrolling_p = 1;
15056
15057 #if GLYPH_DEBUG
15058 debug_method_add (w, "try_window_reusing_current_matrix 2");
15059 #endif
15060 return 1;
15061 }
15062
15063 return 0;
15064 }
15065
15066
15067 \f
15068 /************************************************************************
15069 Window redisplay reusing current matrix when buffer has changed
15070 ************************************************************************/
15071
15072 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
15073 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
15074 int *, int *));
15075 static struct glyph_row *
15076 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
15077 struct glyph_row *));
15078
15079
15080 /* Return the last row in MATRIX displaying text. If row START is
15081 non-null, start searching with that row. IT gives the dimensions
15082 of the display. Value is null if matrix is empty; otherwise it is
15083 a pointer to the row found. */
15084
15085 static struct glyph_row *
15086 find_last_row_displaying_text (matrix, it, start)
15087 struct glyph_matrix *matrix;
15088 struct it *it;
15089 struct glyph_row *start;
15090 {
15091 struct glyph_row *row, *row_found;
15092
15093 /* Set row_found to the last row in IT->w's current matrix
15094 displaying text. The loop looks funny but think of partially
15095 visible lines. */
15096 row_found = NULL;
15097 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15098 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15099 {
15100 xassert (row->enabled_p);
15101 row_found = row;
15102 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15103 break;
15104 ++row;
15105 }
15106
15107 return row_found;
15108 }
15109
15110
15111 /* Return the last row in the current matrix of W that is not affected
15112 by changes at the start of current_buffer that occurred since W's
15113 current matrix was built. Value is null if no such row exists.
15114
15115 BEG_UNCHANGED us the number of characters unchanged at the start of
15116 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15117 first changed character in current_buffer. Characters at positions <
15118 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15119 when the current matrix was built. */
15120
15121 static struct glyph_row *
15122 find_last_unchanged_at_beg_row (w)
15123 struct window *w;
15124 {
15125 int first_changed_pos = BEG + BEG_UNCHANGED;
15126 struct glyph_row *row;
15127 struct glyph_row *row_found = NULL;
15128 int yb = window_text_bottom_y (w);
15129
15130 /* Find the last row displaying unchanged text. */
15131 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15132 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15133 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15134 ++row)
15135 {
15136 if (/* If row ends before first_changed_pos, it is unchanged,
15137 except in some case. */
15138 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15139 /* When row ends in ZV and we write at ZV it is not
15140 unchanged. */
15141 && !row->ends_at_zv_p
15142 /* When first_changed_pos is the end of a continued line,
15143 row is not unchanged because it may be no longer
15144 continued. */
15145 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15146 && (row->continued_p
15147 || row->exact_window_width_line_p)))
15148 row_found = row;
15149
15150 /* Stop if last visible row. */
15151 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15152 break;
15153 }
15154
15155 return row_found;
15156 }
15157
15158
15159 /* Find the first glyph row in the current matrix of W that is not
15160 affected by changes at the end of current_buffer since the
15161 time W's current matrix was built.
15162
15163 Return in *DELTA the number of chars by which buffer positions in
15164 unchanged text at the end of current_buffer must be adjusted.
15165
15166 Return in *DELTA_BYTES the corresponding number of bytes.
15167
15168 Value is null if no such row exists, i.e. all rows are affected by
15169 changes. */
15170
15171 static struct glyph_row *
15172 find_first_unchanged_at_end_row (w, delta, delta_bytes)
15173 struct window *w;
15174 int *delta, *delta_bytes;
15175 {
15176 struct glyph_row *row;
15177 struct glyph_row *row_found = NULL;
15178
15179 *delta = *delta_bytes = 0;
15180
15181 /* Display must not have been paused, otherwise the current matrix
15182 is not up to date. */
15183 eassert (!NILP (w->window_end_valid));
15184
15185 /* A value of window_end_pos >= END_UNCHANGED means that the window
15186 end is in the range of changed text. If so, there is no
15187 unchanged row at the end of W's current matrix. */
15188 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15189 return NULL;
15190
15191 /* Set row to the last row in W's current matrix displaying text. */
15192 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15193
15194 /* If matrix is entirely empty, no unchanged row exists. */
15195 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15196 {
15197 /* The value of row is the last glyph row in the matrix having a
15198 meaningful buffer position in it. The end position of row
15199 corresponds to window_end_pos. This allows us to translate
15200 buffer positions in the current matrix to current buffer
15201 positions for characters not in changed text. */
15202 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15203 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15204 int last_unchanged_pos, last_unchanged_pos_old;
15205 struct glyph_row *first_text_row
15206 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15207
15208 *delta = Z - Z_old;
15209 *delta_bytes = Z_BYTE - Z_BYTE_old;
15210
15211 /* Set last_unchanged_pos to the buffer position of the last
15212 character in the buffer that has not been changed. Z is the
15213 index + 1 of the last character in current_buffer, i.e. by
15214 subtracting END_UNCHANGED we get the index of the last
15215 unchanged character, and we have to add BEG to get its buffer
15216 position. */
15217 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15218 last_unchanged_pos_old = last_unchanged_pos - *delta;
15219
15220 /* Search backward from ROW for a row displaying a line that
15221 starts at a minimum position >= last_unchanged_pos_old. */
15222 for (; row > first_text_row; --row)
15223 {
15224 /* This used to abort, but it can happen.
15225 It is ok to just stop the search instead here. KFS. */
15226 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15227 break;
15228
15229 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15230 row_found = row;
15231 }
15232 }
15233
15234 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15235
15236 return row_found;
15237 }
15238
15239
15240 /* Make sure that glyph rows in the current matrix of window W
15241 reference the same glyph memory as corresponding rows in the
15242 frame's frame matrix. This function is called after scrolling W's
15243 current matrix on a terminal frame in try_window_id and
15244 try_window_reusing_current_matrix. */
15245
15246 static void
15247 sync_frame_with_window_matrix_rows (w)
15248 struct window *w;
15249 {
15250 struct frame *f = XFRAME (w->frame);
15251 struct glyph_row *window_row, *window_row_end, *frame_row;
15252
15253 /* Preconditions: W must be a leaf window and full-width. Its frame
15254 must have a frame matrix. */
15255 xassert (NILP (w->hchild) && NILP (w->vchild));
15256 xassert (WINDOW_FULL_WIDTH_P (w));
15257 xassert (!FRAME_WINDOW_P (f));
15258
15259 /* If W is a full-width window, glyph pointers in W's current matrix
15260 have, by definition, to be the same as glyph pointers in the
15261 corresponding frame matrix. Note that frame matrices have no
15262 marginal areas (see build_frame_matrix). */
15263 window_row = w->current_matrix->rows;
15264 window_row_end = window_row + w->current_matrix->nrows;
15265 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15266 while (window_row < window_row_end)
15267 {
15268 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15269 struct glyph *end = window_row->glyphs[LAST_AREA];
15270
15271 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15272 frame_row->glyphs[TEXT_AREA] = start;
15273 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15274 frame_row->glyphs[LAST_AREA] = end;
15275
15276 /* Disable frame rows whose corresponding window rows have
15277 been disabled in try_window_id. */
15278 if (!window_row->enabled_p)
15279 frame_row->enabled_p = 0;
15280
15281 ++window_row, ++frame_row;
15282 }
15283 }
15284
15285
15286 /* Find the glyph row in window W containing CHARPOS. Consider all
15287 rows between START and END (not inclusive). END null means search
15288 all rows to the end of the display area of W. Value is the row
15289 containing CHARPOS or null. */
15290
15291 struct glyph_row *
15292 row_containing_pos (w, charpos, start, end, dy)
15293 struct window *w;
15294 int charpos;
15295 struct glyph_row *start, *end;
15296 int dy;
15297 {
15298 struct glyph_row *row = start;
15299 struct glyph_row *best_row = NULL;
15300 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15301 int last_y;
15302
15303 /* If we happen to start on a header-line, skip that. */
15304 if (row->mode_line_p)
15305 ++row;
15306
15307 if ((end && row >= end) || !row->enabled_p)
15308 return NULL;
15309
15310 last_y = window_text_bottom_y (w) - dy;
15311
15312 while (1)
15313 {
15314 /* Give up if we have gone too far. */
15315 if (end && row >= end)
15316 return NULL;
15317 /* This formerly returned if they were equal.
15318 I think that both quantities are of a "last plus one" type;
15319 if so, when they are equal, the row is within the screen. -- rms. */
15320 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15321 return NULL;
15322
15323 /* If it is in this row, return this row. */
15324 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15325 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15326 /* The end position of a row equals the start
15327 position of the next row. If CHARPOS is there, we
15328 would rather display it in the next line, except
15329 when this line ends in ZV. */
15330 && !row->ends_at_zv_p
15331 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15332 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15333 {
15334 struct glyph *g;
15335
15336 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15337 return row;
15338 /* In bidi-reordered rows, there could be several rows
15339 occluding point. We need to find the one which fits
15340 CHARPOS the best. */
15341 for (g = row->glyphs[TEXT_AREA];
15342 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15343 g++)
15344 {
15345 if (!STRINGP (g->object))
15346 {
15347 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15348 {
15349 mindif = eabs (g->charpos - charpos);
15350 best_row = row;
15351 }
15352 }
15353 }
15354 }
15355 else if (best_row)
15356 return best_row;
15357 ++row;
15358 }
15359 }
15360
15361
15362 /* Try to redisplay window W by reusing its existing display. W's
15363 current matrix must be up to date when this function is called,
15364 i.e. window_end_valid must not be nil.
15365
15366 Value is
15367
15368 1 if display has been updated
15369 0 if otherwise unsuccessful
15370 -1 if redisplay with same window start is known not to succeed
15371
15372 The following steps are performed:
15373
15374 1. Find the last row in the current matrix of W that is not
15375 affected by changes at the start of current_buffer. If no such row
15376 is found, give up.
15377
15378 2. Find the first row in W's current matrix that is not affected by
15379 changes at the end of current_buffer. Maybe there is no such row.
15380
15381 3. Display lines beginning with the row + 1 found in step 1 to the
15382 row found in step 2 or, if step 2 didn't find a row, to the end of
15383 the window.
15384
15385 4. If cursor is not known to appear on the window, give up.
15386
15387 5. If display stopped at the row found in step 2, scroll the
15388 display and current matrix as needed.
15389
15390 6. Maybe display some lines at the end of W, if we must. This can
15391 happen under various circumstances, like a partially visible line
15392 becoming fully visible, or because newly displayed lines are displayed
15393 in smaller font sizes.
15394
15395 7. Update W's window end information. */
15396
15397 static int
15398 try_window_id (w)
15399 struct window *w;
15400 {
15401 struct frame *f = XFRAME (w->frame);
15402 struct glyph_matrix *current_matrix = w->current_matrix;
15403 struct glyph_matrix *desired_matrix = w->desired_matrix;
15404 struct glyph_row *last_unchanged_at_beg_row;
15405 struct glyph_row *first_unchanged_at_end_row;
15406 struct glyph_row *row;
15407 struct glyph_row *bottom_row;
15408 int bottom_vpos;
15409 struct it it;
15410 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
15411 struct text_pos start_pos;
15412 struct run run;
15413 int first_unchanged_at_end_vpos = 0;
15414 struct glyph_row *last_text_row, *last_text_row_at_end;
15415 struct text_pos start;
15416 int first_changed_charpos, last_changed_charpos;
15417
15418 #if GLYPH_DEBUG
15419 if (inhibit_try_window_id)
15420 return 0;
15421 #endif
15422
15423 /* This is handy for debugging. */
15424 #if 0
15425 #define GIVE_UP(X) \
15426 do { \
15427 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15428 return 0; \
15429 } while (0)
15430 #else
15431 #define GIVE_UP(X) return 0
15432 #endif
15433
15434 SET_TEXT_POS_FROM_MARKER (start, w->start);
15435
15436 /* Don't use this for mini-windows because these can show
15437 messages and mini-buffers, and we don't handle that here. */
15438 if (MINI_WINDOW_P (w))
15439 GIVE_UP (1);
15440
15441 /* This flag is used to prevent redisplay optimizations. */
15442 if (windows_or_buffers_changed || cursor_type_changed)
15443 GIVE_UP (2);
15444
15445 /* Verify that narrowing has not changed.
15446 Also verify that we were not told to prevent redisplay optimizations.
15447 It would be nice to further
15448 reduce the number of cases where this prevents try_window_id. */
15449 if (current_buffer->clip_changed
15450 || current_buffer->prevent_redisplay_optimizations_p)
15451 GIVE_UP (3);
15452
15453 /* Window must either use window-based redisplay or be full width. */
15454 if (!FRAME_WINDOW_P (f)
15455 && (!FRAME_LINE_INS_DEL_OK (f)
15456 || !WINDOW_FULL_WIDTH_P (w)))
15457 GIVE_UP (4);
15458
15459 /* Give up if point is known NOT to appear in W. */
15460 if (PT < CHARPOS (start))
15461 GIVE_UP (5);
15462
15463 /* Another way to prevent redisplay optimizations. */
15464 if (XFASTINT (w->last_modified) == 0)
15465 GIVE_UP (6);
15466
15467 /* Verify that window is not hscrolled. */
15468 if (XFASTINT (w->hscroll) != 0)
15469 GIVE_UP (7);
15470
15471 /* Verify that display wasn't paused. */
15472 if (NILP (w->window_end_valid))
15473 GIVE_UP (8);
15474
15475 /* Can't use this if highlighting a region because a cursor movement
15476 will do more than just set the cursor. */
15477 if (!NILP (Vtransient_mark_mode)
15478 && !NILP (current_buffer->mark_active))
15479 GIVE_UP (9);
15480
15481 /* Likewise if highlighting trailing whitespace. */
15482 if (!NILP (Vshow_trailing_whitespace))
15483 GIVE_UP (11);
15484
15485 /* Likewise if showing a region. */
15486 if (!NILP (w->region_showing))
15487 GIVE_UP (10);
15488
15489 /* Can't use this if overlay arrow position and/or string have
15490 changed. */
15491 if (overlay_arrows_changed_p ())
15492 GIVE_UP (12);
15493
15494 /* When word-wrap is on, adding a space to the first word of a
15495 wrapped line can change the wrap position, altering the line
15496 above it. It might be worthwhile to handle this more
15497 intelligently, but for now just redisplay from scratch. */
15498 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15499 GIVE_UP (21);
15500
15501 /* Under bidi reordering, adding or deleting a character in the
15502 beginning of a paragraph, before the first strong directional
15503 character, can change the base direction of the paragraph (unless
15504 the buffer specifies a fixed paragraph direction), which will
15505 require to redisplay the whole paragraph. It might be worthwhile
15506 to find the paragraph limits and widen the range of redisplayed
15507 lines to that, but for now just give up this optimization and
15508 redisplay from scratch. */
15509 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15510 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15511 GIVE_UP (22);
15512
15513 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15514 only if buffer has really changed. The reason is that the gap is
15515 initially at Z for freshly visited files. The code below would
15516 set end_unchanged to 0 in that case. */
15517 if (MODIFF > SAVE_MODIFF
15518 /* This seems to happen sometimes after saving a buffer. */
15519 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15520 {
15521 if (GPT - BEG < BEG_UNCHANGED)
15522 BEG_UNCHANGED = GPT - BEG;
15523 if (Z - GPT < END_UNCHANGED)
15524 END_UNCHANGED = Z - GPT;
15525 }
15526
15527 /* The position of the first and last character that has been changed. */
15528 first_changed_charpos = BEG + BEG_UNCHANGED;
15529 last_changed_charpos = Z - END_UNCHANGED;
15530
15531 /* If window starts after a line end, and the last change is in
15532 front of that newline, then changes don't affect the display.
15533 This case happens with stealth-fontification. Note that although
15534 the display is unchanged, glyph positions in the matrix have to
15535 be adjusted, of course. */
15536 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15537 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15538 && ((last_changed_charpos < CHARPOS (start)
15539 && CHARPOS (start) == BEGV)
15540 || (last_changed_charpos < CHARPOS (start) - 1
15541 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15542 {
15543 int Z_old, delta, Z_BYTE_old, delta_bytes;
15544 struct glyph_row *r0;
15545
15546 /* Compute how many chars/bytes have been added to or removed
15547 from the buffer. */
15548 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15549 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15550 delta = Z - Z_old;
15551 delta_bytes = Z_BYTE - Z_BYTE_old;
15552
15553 /* Give up if PT is not in the window. Note that it already has
15554 been checked at the start of try_window_id that PT is not in
15555 front of the window start. */
15556 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15557 GIVE_UP (13);
15558
15559 /* If window start is unchanged, we can reuse the whole matrix
15560 as is, after adjusting glyph positions. No need to compute
15561 the window end again, since its offset from Z hasn't changed. */
15562 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15563 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15564 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15565 /* PT must not be in a partially visible line. */
15566 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15567 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15568 {
15569 /* Adjust positions in the glyph matrix. */
15570 if (delta || delta_bytes)
15571 {
15572 struct glyph_row *r1
15573 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15574 increment_matrix_positions (w->current_matrix,
15575 MATRIX_ROW_VPOS (r0, current_matrix),
15576 MATRIX_ROW_VPOS (r1, current_matrix),
15577 delta, delta_bytes);
15578 }
15579
15580 /* Set the cursor. */
15581 row = row_containing_pos (w, PT, r0, NULL, 0);
15582 if (row)
15583 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15584 else
15585 abort ();
15586 return 1;
15587 }
15588 }
15589
15590 /* Handle the case that changes are all below what is displayed in
15591 the window, and that PT is in the window. This shortcut cannot
15592 be taken if ZV is visible in the window, and text has been added
15593 there that is visible in the window. */
15594 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15595 /* ZV is not visible in the window, or there are no
15596 changes at ZV, actually. */
15597 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15598 || first_changed_charpos == last_changed_charpos))
15599 {
15600 struct glyph_row *r0;
15601
15602 /* Give up if PT is not in the window. Note that it already has
15603 been checked at the start of try_window_id that PT is not in
15604 front of the window start. */
15605 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15606 GIVE_UP (14);
15607
15608 /* If window start is unchanged, we can reuse the whole matrix
15609 as is, without changing glyph positions since no text has
15610 been added/removed in front of the window end. */
15611 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15612 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15613 /* PT must not be in a partially visible line. */
15614 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15615 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15616 {
15617 /* We have to compute the window end anew since text
15618 can have been added/removed after it. */
15619 w->window_end_pos
15620 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15621 w->window_end_bytepos
15622 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15623
15624 /* Set the cursor. */
15625 row = row_containing_pos (w, PT, r0, NULL, 0);
15626 if (row)
15627 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15628 else
15629 abort ();
15630 return 2;
15631 }
15632 }
15633
15634 /* Give up if window start is in the changed area.
15635
15636 The condition used to read
15637
15638 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15639
15640 but why that was tested escapes me at the moment. */
15641 if (CHARPOS (start) >= first_changed_charpos
15642 && CHARPOS (start) <= last_changed_charpos)
15643 GIVE_UP (15);
15644
15645 /* Check that window start agrees with the start of the first glyph
15646 row in its current matrix. Check this after we know the window
15647 start is not in changed text, otherwise positions would not be
15648 comparable. */
15649 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15650 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15651 GIVE_UP (16);
15652
15653 /* Give up if the window ends in strings. Overlay strings
15654 at the end are difficult to handle, so don't try. */
15655 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15656 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15657 GIVE_UP (20);
15658
15659 /* Compute the position at which we have to start displaying new
15660 lines. Some of the lines at the top of the window might be
15661 reusable because they are not displaying changed text. Find the
15662 last row in W's current matrix not affected by changes at the
15663 start of current_buffer. Value is null if changes start in the
15664 first line of window. */
15665 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15666 if (last_unchanged_at_beg_row)
15667 {
15668 /* Avoid starting to display in the moddle of a character, a TAB
15669 for instance. This is easier than to set up the iterator
15670 exactly, and it's not a frequent case, so the additional
15671 effort wouldn't really pay off. */
15672 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15673 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15674 && last_unchanged_at_beg_row > w->current_matrix->rows)
15675 --last_unchanged_at_beg_row;
15676
15677 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15678 GIVE_UP (17);
15679
15680 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15681 GIVE_UP (18);
15682 start_pos = it.current.pos;
15683
15684 /* Start displaying new lines in the desired matrix at the same
15685 vpos we would use in the current matrix, i.e. below
15686 last_unchanged_at_beg_row. */
15687 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15688 current_matrix);
15689 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15690 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15691
15692 xassert (it.hpos == 0 && it.current_x == 0);
15693 }
15694 else
15695 {
15696 /* There are no reusable lines at the start of the window.
15697 Start displaying in the first text line. */
15698 start_display (&it, w, start);
15699 it.vpos = it.first_vpos;
15700 start_pos = it.current.pos;
15701 }
15702
15703 /* Find the first row that is not affected by changes at the end of
15704 the buffer. Value will be null if there is no unchanged row, in
15705 which case we must redisplay to the end of the window. delta
15706 will be set to the value by which buffer positions beginning with
15707 first_unchanged_at_end_row have to be adjusted due to text
15708 changes. */
15709 first_unchanged_at_end_row
15710 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15711 IF_DEBUG (debug_delta = delta);
15712 IF_DEBUG (debug_delta_bytes = delta_bytes);
15713
15714 /* Set stop_pos to the buffer position up to which we will have to
15715 display new lines. If first_unchanged_at_end_row != NULL, this
15716 is the buffer position of the start of the line displayed in that
15717 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15718 that we don't stop at a buffer position. */
15719 stop_pos = 0;
15720 if (first_unchanged_at_end_row)
15721 {
15722 xassert (last_unchanged_at_beg_row == NULL
15723 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15724
15725 /* If this is a continuation line, move forward to the next one
15726 that isn't. Changes in lines above affect this line.
15727 Caution: this may move first_unchanged_at_end_row to a row
15728 not displaying text. */
15729 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15730 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15731 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15732 < it.last_visible_y))
15733 ++first_unchanged_at_end_row;
15734
15735 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15736 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15737 >= it.last_visible_y))
15738 first_unchanged_at_end_row = NULL;
15739 else
15740 {
15741 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15742 + delta);
15743 first_unchanged_at_end_vpos
15744 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15745 xassert (stop_pos >= Z - END_UNCHANGED);
15746 }
15747 }
15748 else if (last_unchanged_at_beg_row == NULL)
15749 GIVE_UP (19);
15750
15751
15752 #if GLYPH_DEBUG
15753
15754 /* Either there is no unchanged row at the end, or the one we have
15755 now displays text. This is a necessary condition for the window
15756 end pos calculation at the end of this function. */
15757 xassert (first_unchanged_at_end_row == NULL
15758 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15759
15760 debug_last_unchanged_at_beg_vpos
15761 = (last_unchanged_at_beg_row
15762 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15763 : -1);
15764 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15765
15766 #endif /* GLYPH_DEBUG != 0 */
15767
15768
15769 /* Display new lines. Set last_text_row to the last new line
15770 displayed which has text on it, i.e. might end up as being the
15771 line where the window_end_vpos is. */
15772 w->cursor.vpos = -1;
15773 last_text_row = NULL;
15774 overlay_arrow_seen = 0;
15775 while (it.current_y < it.last_visible_y
15776 && !fonts_changed_p
15777 && (first_unchanged_at_end_row == NULL
15778 || IT_CHARPOS (it) < stop_pos))
15779 {
15780 if (display_line (&it))
15781 last_text_row = it.glyph_row - 1;
15782 }
15783
15784 if (fonts_changed_p)
15785 return -1;
15786
15787
15788 /* Compute differences in buffer positions, y-positions etc. for
15789 lines reused at the bottom of the window. Compute what we can
15790 scroll. */
15791 if (first_unchanged_at_end_row
15792 /* No lines reused because we displayed everything up to the
15793 bottom of the window. */
15794 && it.current_y < it.last_visible_y)
15795 {
15796 dvpos = (it.vpos
15797 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15798 current_matrix));
15799 dy = it.current_y - first_unchanged_at_end_row->y;
15800 run.current_y = first_unchanged_at_end_row->y;
15801 run.desired_y = run.current_y + dy;
15802 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15803 }
15804 else
15805 {
15806 delta = delta_bytes = dvpos = dy
15807 = run.current_y = run.desired_y = run.height = 0;
15808 first_unchanged_at_end_row = NULL;
15809 }
15810 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15811
15812
15813 /* Find the cursor if not already found. We have to decide whether
15814 PT will appear on this window (it sometimes doesn't, but this is
15815 not a very frequent case.) This decision has to be made before
15816 the current matrix is altered. A value of cursor.vpos < 0 means
15817 that PT is either in one of the lines beginning at
15818 first_unchanged_at_end_row or below the window. Don't care for
15819 lines that might be displayed later at the window end; as
15820 mentioned, this is not a frequent case. */
15821 if (w->cursor.vpos < 0)
15822 {
15823 /* Cursor in unchanged rows at the top? */
15824 if (PT < CHARPOS (start_pos)
15825 && last_unchanged_at_beg_row)
15826 {
15827 row = row_containing_pos (w, PT,
15828 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15829 last_unchanged_at_beg_row + 1, 0);
15830 if (row)
15831 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15832 }
15833
15834 /* Start from first_unchanged_at_end_row looking for PT. */
15835 else if (first_unchanged_at_end_row)
15836 {
15837 row = row_containing_pos (w, PT - delta,
15838 first_unchanged_at_end_row, NULL, 0);
15839 if (row)
15840 set_cursor_from_row (w, row, w->current_matrix, delta,
15841 delta_bytes, dy, dvpos);
15842 }
15843
15844 /* Give up if cursor was not found. */
15845 if (w->cursor.vpos < 0)
15846 {
15847 clear_glyph_matrix (w->desired_matrix);
15848 return -1;
15849 }
15850 }
15851
15852 /* Don't let the cursor end in the scroll margins. */
15853 {
15854 int this_scroll_margin, cursor_height;
15855
15856 this_scroll_margin = max (0, scroll_margin);
15857 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15858 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15859 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15860
15861 if ((w->cursor.y < this_scroll_margin
15862 && CHARPOS (start) > BEGV)
15863 /* Old redisplay didn't take scroll margin into account at the bottom,
15864 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15865 || (w->cursor.y + (make_cursor_line_fully_visible_p
15866 ? cursor_height + this_scroll_margin
15867 : 1)) > it.last_visible_y)
15868 {
15869 w->cursor.vpos = -1;
15870 clear_glyph_matrix (w->desired_matrix);
15871 return -1;
15872 }
15873 }
15874
15875 /* Scroll the display. Do it before changing the current matrix so
15876 that xterm.c doesn't get confused about where the cursor glyph is
15877 found. */
15878 if (dy && run.height)
15879 {
15880 update_begin (f);
15881
15882 if (FRAME_WINDOW_P (f))
15883 {
15884 FRAME_RIF (f)->update_window_begin_hook (w);
15885 FRAME_RIF (f)->clear_window_mouse_face (w);
15886 FRAME_RIF (f)->scroll_run_hook (w, &run);
15887 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15888 }
15889 else
15890 {
15891 /* Terminal frame. In this case, dvpos gives the number of
15892 lines to scroll by; dvpos < 0 means scroll up. */
15893 int first_unchanged_at_end_vpos
15894 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15895 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15896 int end = (WINDOW_TOP_EDGE_LINE (w)
15897 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15898 + window_internal_height (w));
15899
15900 /* Perform the operation on the screen. */
15901 if (dvpos > 0)
15902 {
15903 /* Scroll last_unchanged_at_beg_row to the end of the
15904 window down dvpos lines. */
15905 set_terminal_window (f, end);
15906
15907 /* On dumb terminals delete dvpos lines at the end
15908 before inserting dvpos empty lines. */
15909 if (!FRAME_SCROLL_REGION_OK (f))
15910 ins_del_lines (f, end - dvpos, -dvpos);
15911
15912 /* Insert dvpos empty lines in front of
15913 last_unchanged_at_beg_row. */
15914 ins_del_lines (f, from, dvpos);
15915 }
15916 else if (dvpos < 0)
15917 {
15918 /* Scroll up last_unchanged_at_beg_vpos to the end of
15919 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15920 set_terminal_window (f, end);
15921
15922 /* Delete dvpos lines in front of
15923 last_unchanged_at_beg_vpos. ins_del_lines will set
15924 the cursor to the given vpos and emit |dvpos| delete
15925 line sequences. */
15926 ins_del_lines (f, from + dvpos, dvpos);
15927
15928 /* On a dumb terminal insert dvpos empty lines at the
15929 end. */
15930 if (!FRAME_SCROLL_REGION_OK (f))
15931 ins_del_lines (f, end + dvpos, -dvpos);
15932 }
15933
15934 set_terminal_window (f, 0);
15935 }
15936
15937 update_end (f);
15938 }
15939
15940 /* Shift reused rows of the current matrix to the right position.
15941 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15942 text. */
15943 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15944 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15945 if (dvpos < 0)
15946 {
15947 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15948 bottom_vpos, dvpos);
15949 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15950 bottom_vpos, 0);
15951 }
15952 else if (dvpos > 0)
15953 {
15954 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15955 bottom_vpos, dvpos);
15956 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15957 first_unchanged_at_end_vpos + dvpos, 0);
15958 }
15959
15960 /* For frame-based redisplay, make sure that current frame and window
15961 matrix are in sync with respect to glyph memory. */
15962 if (!FRAME_WINDOW_P (f))
15963 sync_frame_with_window_matrix_rows (w);
15964
15965 /* Adjust buffer positions in reused rows. */
15966 if (delta || delta_bytes)
15967 increment_matrix_positions (current_matrix,
15968 first_unchanged_at_end_vpos + dvpos,
15969 bottom_vpos, delta, delta_bytes);
15970
15971 /* Adjust Y positions. */
15972 if (dy)
15973 shift_glyph_matrix (w, current_matrix,
15974 first_unchanged_at_end_vpos + dvpos,
15975 bottom_vpos, dy);
15976
15977 if (first_unchanged_at_end_row)
15978 {
15979 first_unchanged_at_end_row += dvpos;
15980 if (first_unchanged_at_end_row->y >= it.last_visible_y
15981 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15982 first_unchanged_at_end_row = NULL;
15983 }
15984
15985 /* If scrolling up, there may be some lines to display at the end of
15986 the window. */
15987 last_text_row_at_end = NULL;
15988 if (dy < 0)
15989 {
15990 /* Scrolling up can leave for example a partially visible line
15991 at the end of the window to be redisplayed. */
15992 /* Set last_row to the glyph row in the current matrix where the
15993 window end line is found. It has been moved up or down in
15994 the matrix by dvpos. */
15995 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15996 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15997
15998 /* If last_row is the window end line, it should display text. */
15999 xassert (last_row->displays_text_p);
16000
16001 /* If window end line was partially visible before, begin
16002 displaying at that line. Otherwise begin displaying with the
16003 line following it. */
16004 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16005 {
16006 init_to_row_start (&it, w, last_row);
16007 it.vpos = last_vpos;
16008 it.current_y = last_row->y;
16009 }
16010 else
16011 {
16012 init_to_row_end (&it, w, last_row);
16013 it.vpos = 1 + last_vpos;
16014 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16015 ++last_row;
16016 }
16017
16018 /* We may start in a continuation line. If so, we have to
16019 get the right continuation_lines_width and current_x. */
16020 it.continuation_lines_width = last_row->continuation_lines_width;
16021 it.hpos = it.current_x = 0;
16022
16023 /* Display the rest of the lines at the window end. */
16024 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16025 while (it.current_y < it.last_visible_y
16026 && !fonts_changed_p)
16027 {
16028 /* Is it always sure that the display agrees with lines in
16029 the current matrix? I don't think so, so we mark rows
16030 displayed invalid in the current matrix by setting their
16031 enabled_p flag to zero. */
16032 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16033 if (display_line (&it))
16034 last_text_row_at_end = it.glyph_row - 1;
16035 }
16036 }
16037
16038 /* Update window_end_pos and window_end_vpos. */
16039 if (first_unchanged_at_end_row
16040 && !last_text_row_at_end)
16041 {
16042 /* Window end line if one of the preserved rows from the current
16043 matrix. Set row to the last row displaying text in current
16044 matrix starting at first_unchanged_at_end_row, after
16045 scrolling. */
16046 xassert (first_unchanged_at_end_row->displays_text_p);
16047 row = find_last_row_displaying_text (w->current_matrix, &it,
16048 first_unchanged_at_end_row);
16049 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16050
16051 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16052 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16053 w->window_end_vpos
16054 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16055 xassert (w->window_end_bytepos >= 0);
16056 IF_DEBUG (debug_method_add (w, "A"));
16057 }
16058 else if (last_text_row_at_end)
16059 {
16060 w->window_end_pos
16061 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16062 w->window_end_bytepos
16063 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16064 w->window_end_vpos
16065 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16066 xassert (w->window_end_bytepos >= 0);
16067 IF_DEBUG (debug_method_add (w, "B"));
16068 }
16069 else if (last_text_row)
16070 {
16071 /* We have displayed either to the end of the window or at the
16072 end of the window, i.e. the last row with text is to be found
16073 in the desired matrix. */
16074 w->window_end_pos
16075 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16076 w->window_end_bytepos
16077 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16078 w->window_end_vpos
16079 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16080 xassert (w->window_end_bytepos >= 0);
16081 }
16082 else if (first_unchanged_at_end_row == NULL
16083 && last_text_row == NULL
16084 && last_text_row_at_end == NULL)
16085 {
16086 /* Displayed to end of window, but no line containing text was
16087 displayed. Lines were deleted at the end of the window. */
16088 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16089 int vpos = XFASTINT (w->window_end_vpos);
16090 struct glyph_row *current_row = current_matrix->rows + vpos;
16091 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16092
16093 for (row = NULL;
16094 row == NULL && vpos >= first_vpos;
16095 --vpos, --current_row, --desired_row)
16096 {
16097 if (desired_row->enabled_p)
16098 {
16099 if (desired_row->displays_text_p)
16100 row = desired_row;
16101 }
16102 else if (current_row->displays_text_p)
16103 row = current_row;
16104 }
16105
16106 xassert (row != NULL);
16107 w->window_end_vpos = make_number (vpos + 1);
16108 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16109 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16110 xassert (w->window_end_bytepos >= 0);
16111 IF_DEBUG (debug_method_add (w, "C"));
16112 }
16113 else
16114 abort ();
16115
16116 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16117 debug_end_vpos = XFASTINT (w->window_end_vpos));
16118
16119 /* Record that display has not been completed. */
16120 w->window_end_valid = Qnil;
16121 w->desired_matrix->no_scrolling_p = 1;
16122 return 3;
16123
16124 #undef GIVE_UP
16125 }
16126
16127
16128 \f
16129 /***********************************************************************
16130 More debugging support
16131 ***********************************************************************/
16132
16133 #if GLYPH_DEBUG
16134
16135 void dump_glyph_row P_ ((struct glyph_row *, int, int));
16136 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
16137 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
16138
16139
16140 /* Dump the contents of glyph matrix MATRIX on stderr.
16141
16142 GLYPHS 0 means don't show glyph contents.
16143 GLYPHS 1 means show glyphs in short form
16144 GLYPHS > 1 means show glyphs in long form. */
16145
16146 void
16147 dump_glyph_matrix (matrix, glyphs)
16148 struct glyph_matrix *matrix;
16149 int glyphs;
16150 {
16151 int i;
16152 for (i = 0; i < matrix->nrows; ++i)
16153 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16154 }
16155
16156
16157 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16158 the glyph row and area where the glyph comes from. */
16159
16160 void
16161 dump_glyph (row, glyph, area)
16162 struct glyph_row *row;
16163 struct glyph *glyph;
16164 int area;
16165 {
16166 if (glyph->type == CHAR_GLYPH)
16167 {
16168 fprintf (stderr,
16169 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16170 glyph - row->glyphs[TEXT_AREA],
16171 'C',
16172 glyph->charpos,
16173 (BUFFERP (glyph->object)
16174 ? 'B'
16175 : (STRINGP (glyph->object)
16176 ? 'S'
16177 : '-')),
16178 glyph->pixel_width,
16179 glyph->u.ch,
16180 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16181 ? glyph->u.ch
16182 : '.'),
16183 glyph->face_id,
16184 glyph->left_box_line_p,
16185 glyph->right_box_line_p);
16186 }
16187 else if (glyph->type == STRETCH_GLYPH)
16188 {
16189 fprintf (stderr,
16190 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16191 glyph - row->glyphs[TEXT_AREA],
16192 'S',
16193 glyph->charpos,
16194 (BUFFERP (glyph->object)
16195 ? 'B'
16196 : (STRINGP (glyph->object)
16197 ? 'S'
16198 : '-')),
16199 glyph->pixel_width,
16200 0,
16201 '.',
16202 glyph->face_id,
16203 glyph->left_box_line_p,
16204 glyph->right_box_line_p);
16205 }
16206 else if (glyph->type == IMAGE_GLYPH)
16207 {
16208 fprintf (stderr,
16209 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16210 glyph - row->glyphs[TEXT_AREA],
16211 'I',
16212 glyph->charpos,
16213 (BUFFERP (glyph->object)
16214 ? 'B'
16215 : (STRINGP (glyph->object)
16216 ? 'S'
16217 : '-')),
16218 glyph->pixel_width,
16219 glyph->u.img_id,
16220 '.',
16221 glyph->face_id,
16222 glyph->left_box_line_p,
16223 glyph->right_box_line_p);
16224 }
16225 else if (glyph->type == COMPOSITE_GLYPH)
16226 {
16227 fprintf (stderr,
16228 " %5d %4c %6d %c %3d 0x%05x",
16229 glyph - row->glyphs[TEXT_AREA],
16230 '+',
16231 glyph->charpos,
16232 (BUFFERP (glyph->object)
16233 ? 'B'
16234 : (STRINGP (glyph->object)
16235 ? 'S'
16236 : '-')),
16237 glyph->pixel_width,
16238 glyph->u.cmp.id);
16239 if (glyph->u.cmp.automatic)
16240 fprintf (stderr,
16241 "[%d-%d]",
16242 glyph->u.cmp.from, glyph->u.cmp.to);
16243 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16244 glyph->face_id,
16245 glyph->left_box_line_p,
16246 glyph->right_box_line_p);
16247 }
16248 }
16249
16250
16251 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16252 GLYPHS 0 means don't show glyph contents.
16253 GLYPHS 1 means show glyphs in short form
16254 GLYPHS > 1 means show glyphs in long form. */
16255
16256 void
16257 dump_glyph_row (row, vpos, glyphs)
16258 struct glyph_row *row;
16259 int vpos, glyphs;
16260 {
16261 if (glyphs != 1)
16262 {
16263 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16264 fprintf (stderr, "======================================================================\n");
16265
16266 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16267 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16268 vpos,
16269 MATRIX_ROW_START_CHARPOS (row),
16270 MATRIX_ROW_END_CHARPOS (row),
16271 row->used[TEXT_AREA],
16272 row->contains_overlapping_glyphs_p,
16273 row->enabled_p,
16274 row->truncated_on_left_p,
16275 row->truncated_on_right_p,
16276 row->continued_p,
16277 MATRIX_ROW_CONTINUATION_LINE_P (row),
16278 row->displays_text_p,
16279 row->ends_at_zv_p,
16280 row->fill_line_p,
16281 row->ends_in_middle_of_char_p,
16282 row->starts_in_middle_of_char_p,
16283 row->mouse_face_p,
16284 row->x,
16285 row->y,
16286 row->pixel_width,
16287 row->height,
16288 row->visible_height,
16289 row->ascent,
16290 row->phys_ascent);
16291 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16292 row->end.overlay_string_index,
16293 row->continuation_lines_width);
16294 fprintf (stderr, "%9d %5d\n",
16295 CHARPOS (row->start.string_pos),
16296 CHARPOS (row->end.string_pos));
16297 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16298 row->end.dpvec_index);
16299 }
16300
16301 if (glyphs > 1)
16302 {
16303 int area;
16304
16305 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16306 {
16307 struct glyph *glyph = row->glyphs[area];
16308 struct glyph *glyph_end = glyph + row->used[area];
16309
16310 /* Glyph for a line end in text. */
16311 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16312 ++glyph_end;
16313
16314 if (glyph < glyph_end)
16315 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16316
16317 for (; glyph < glyph_end; ++glyph)
16318 dump_glyph (row, glyph, area);
16319 }
16320 }
16321 else if (glyphs == 1)
16322 {
16323 int area;
16324
16325 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16326 {
16327 char *s = (char *) alloca (row->used[area] + 1);
16328 int i;
16329
16330 for (i = 0; i < row->used[area]; ++i)
16331 {
16332 struct glyph *glyph = row->glyphs[area] + i;
16333 if (glyph->type == CHAR_GLYPH
16334 && glyph->u.ch < 0x80
16335 && glyph->u.ch >= ' ')
16336 s[i] = glyph->u.ch;
16337 else
16338 s[i] = '.';
16339 }
16340
16341 s[i] = '\0';
16342 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16343 }
16344 }
16345 }
16346
16347
16348 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16349 Sdump_glyph_matrix, 0, 1, "p",
16350 doc: /* Dump the current matrix of the selected window to stderr.
16351 Shows contents of glyph row structures. With non-nil
16352 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16353 glyphs in short form, otherwise show glyphs in long form. */)
16354 (glyphs)
16355 Lisp_Object glyphs;
16356 {
16357 struct window *w = XWINDOW (selected_window);
16358 struct buffer *buffer = XBUFFER (w->buffer);
16359
16360 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16361 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16362 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16363 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16364 fprintf (stderr, "=============================================\n");
16365 dump_glyph_matrix (w->current_matrix,
16366 NILP (glyphs) ? 0 : XINT (glyphs));
16367 return Qnil;
16368 }
16369
16370
16371 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16372 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16373 ()
16374 {
16375 struct frame *f = XFRAME (selected_frame);
16376 dump_glyph_matrix (f->current_matrix, 1);
16377 return Qnil;
16378 }
16379
16380
16381 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16382 doc: /* Dump glyph row ROW to stderr.
16383 GLYPH 0 means don't dump glyphs.
16384 GLYPH 1 means dump glyphs in short form.
16385 GLYPH > 1 or omitted means dump glyphs in long form. */)
16386 (row, glyphs)
16387 Lisp_Object row, glyphs;
16388 {
16389 struct glyph_matrix *matrix;
16390 int vpos;
16391
16392 CHECK_NUMBER (row);
16393 matrix = XWINDOW (selected_window)->current_matrix;
16394 vpos = XINT (row);
16395 if (vpos >= 0 && vpos < matrix->nrows)
16396 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16397 vpos,
16398 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16399 return Qnil;
16400 }
16401
16402
16403 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16404 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16405 GLYPH 0 means don't dump glyphs.
16406 GLYPH 1 means dump glyphs in short form.
16407 GLYPH > 1 or omitted means dump glyphs in long form. */)
16408 (row, glyphs)
16409 Lisp_Object row, glyphs;
16410 {
16411 struct frame *sf = SELECTED_FRAME ();
16412 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16413 int vpos;
16414
16415 CHECK_NUMBER (row);
16416 vpos = XINT (row);
16417 if (vpos >= 0 && vpos < m->nrows)
16418 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16419 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16420 return Qnil;
16421 }
16422
16423
16424 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16425 doc: /* Toggle tracing of redisplay.
16426 With ARG, turn tracing on if and only if ARG is positive. */)
16427 (arg)
16428 Lisp_Object arg;
16429 {
16430 if (NILP (arg))
16431 trace_redisplay_p = !trace_redisplay_p;
16432 else
16433 {
16434 arg = Fprefix_numeric_value (arg);
16435 trace_redisplay_p = XINT (arg) > 0;
16436 }
16437
16438 return Qnil;
16439 }
16440
16441
16442 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16443 doc: /* Like `format', but print result to stderr.
16444 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16445 (nargs, args)
16446 int nargs;
16447 Lisp_Object *args;
16448 {
16449 Lisp_Object s = Fformat (nargs, args);
16450 fprintf (stderr, "%s", SDATA (s));
16451 return Qnil;
16452 }
16453
16454 #endif /* GLYPH_DEBUG */
16455
16456
16457 \f
16458 /***********************************************************************
16459 Building Desired Matrix Rows
16460 ***********************************************************************/
16461
16462 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16463 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16464
16465 static struct glyph_row *
16466 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
16467 struct window *w;
16468 Lisp_Object overlay_arrow_string;
16469 {
16470 struct frame *f = XFRAME (WINDOW_FRAME (w));
16471 struct buffer *buffer = XBUFFER (w->buffer);
16472 struct buffer *old = current_buffer;
16473 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16474 int arrow_len = SCHARS (overlay_arrow_string);
16475 const unsigned char *arrow_end = arrow_string + arrow_len;
16476 const unsigned char *p;
16477 struct it it;
16478 int multibyte_p;
16479 int n_glyphs_before;
16480
16481 set_buffer_temp (buffer);
16482 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16483 it.glyph_row->used[TEXT_AREA] = 0;
16484 SET_TEXT_POS (it.position, 0, 0);
16485
16486 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16487 p = arrow_string;
16488 while (p < arrow_end)
16489 {
16490 Lisp_Object face, ilisp;
16491
16492 /* Get the next character. */
16493 if (multibyte_p)
16494 it.c = string_char_and_length (p, &it.len);
16495 else
16496 it.c = *p, it.len = 1;
16497 p += it.len;
16498
16499 /* Get its face. */
16500 ilisp = make_number (p - arrow_string);
16501 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16502 it.face_id = compute_char_face (f, it.c, face);
16503
16504 /* Compute its width, get its glyphs. */
16505 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16506 SET_TEXT_POS (it.position, -1, -1);
16507 PRODUCE_GLYPHS (&it);
16508
16509 /* If this character doesn't fit any more in the line, we have
16510 to remove some glyphs. */
16511 if (it.current_x > it.last_visible_x)
16512 {
16513 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16514 break;
16515 }
16516 }
16517
16518 set_buffer_temp (old);
16519 return it.glyph_row;
16520 }
16521
16522
16523 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16524 glyphs are only inserted for terminal frames since we can't really
16525 win with truncation glyphs when partially visible glyphs are
16526 involved. Which glyphs to insert is determined by
16527 produce_special_glyphs. */
16528
16529 static void
16530 insert_left_trunc_glyphs (it)
16531 struct it *it;
16532 {
16533 struct it truncate_it;
16534 struct glyph *from, *end, *to, *toend;
16535
16536 xassert (!FRAME_WINDOW_P (it->f));
16537
16538 /* Get the truncation glyphs. */
16539 truncate_it = *it;
16540 truncate_it.current_x = 0;
16541 truncate_it.face_id = DEFAULT_FACE_ID;
16542 truncate_it.glyph_row = &scratch_glyph_row;
16543 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16544 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16545 truncate_it.object = make_number (0);
16546 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16547
16548 /* Overwrite glyphs from IT with truncation glyphs. */
16549 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16550 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16551 to = it->glyph_row->glyphs[TEXT_AREA];
16552 toend = to + it->glyph_row->used[TEXT_AREA];
16553
16554 while (from < end)
16555 *to++ = *from++;
16556
16557 /* There may be padding glyphs left over. Overwrite them too. */
16558 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16559 {
16560 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16561 while (from < end)
16562 *to++ = *from++;
16563 }
16564
16565 if (to > toend)
16566 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16567 }
16568
16569
16570 /* Compute the pixel height and width of IT->glyph_row.
16571
16572 Most of the time, ascent and height of a display line will be equal
16573 to the max_ascent and max_height values of the display iterator
16574 structure. This is not the case if
16575
16576 1. We hit ZV without displaying anything. In this case, max_ascent
16577 and max_height will be zero.
16578
16579 2. We have some glyphs that don't contribute to the line height.
16580 (The glyph row flag contributes_to_line_height_p is for future
16581 pixmap extensions).
16582
16583 The first case is easily covered by using default values because in
16584 these cases, the line height does not really matter, except that it
16585 must not be zero. */
16586
16587 static void
16588 compute_line_metrics (it)
16589 struct it *it;
16590 {
16591 struct glyph_row *row = it->glyph_row;
16592 int area, i;
16593
16594 if (FRAME_WINDOW_P (it->f))
16595 {
16596 int i, min_y, max_y;
16597
16598 /* The line may consist of one space only, that was added to
16599 place the cursor on it. If so, the row's height hasn't been
16600 computed yet. */
16601 if (row->height == 0)
16602 {
16603 if (it->max_ascent + it->max_descent == 0)
16604 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16605 row->ascent = it->max_ascent;
16606 row->height = it->max_ascent + it->max_descent;
16607 row->phys_ascent = it->max_phys_ascent;
16608 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16609 row->extra_line_spacing = it->max_extra_line_spacing;
16610 }
16611
16612 /* Compute the width of this line. */
16613 row->pixel_width = row->x;
16614 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16615 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16616
16617 xassert (row->pixel_width >= 0);
16618 xassert (row->ascent >= 0 && row->height > 0);
16619
16620 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16621 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16622
16623 /* If first line's physical ascent is larger than its logical
16624 ascent, use the physical ascent, and make the row taller.
16625 This makes accented characters fully visible. */
16626 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16627 && row->phys_ascent > row->ascent)
16628 {
16629 row->height += row->phys_ascent - row->ascent;
16630 row->ascent = row->phys_ascent;
16631 }
16632
16633 /* Compute how much of the line is visible. */
16634 row->visible_height = row->height;
16635
16636 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16637 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16638
16639 if (row->y < min_y)
16640 row->visible_height -= min_y - row->y;
16641 if (row->y + row->height > max_y)
16642 row->visible_height -= row->y + row->height - max_y;
16643 }
16644 else
16645 {
16646 row->pixel_width = row->used[TEXT_AREA];
16647 if (row->continued_p)
16648 row->pixel_width -= it->continuation_pixel_width;
16649 else if (row->truncated_on_right_p)
16650 row->pixel_width -= it->truncation_pixel_width;
16651 row->ascent = row->phys_ascent = 0;
16652 row->height = row->phys_height = row->visible_height = 1;
16653 row->extra_line_spacing = 0;
16654 }
16655
16656 /* Compute a hash code for this row. */
16657 row->hash = 0;
16658 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16659 for (i = 0; i < row->used[area]; ++i)
16660 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16661 + row->glyphs[area][i].u.val
16662 + row->glyphs[area][i].face_id
16663 + row->glyphs[area][i].padding_p
16664 + (row->glyphs[area][i].type << 2));
16665
16666 it->max_ascent = it->max_descent = 0;
16667 it->max_phys_ascent = it->max_phys_descent = 0;
16668 }
16669
16670
16671 /* Append one space to the glyph row of iterator IT if doing a
16672 window-based redisplay. The space has the same face as
16673 IT->face_id. Value is non-zero if a space was added.
16674
16675 This function is called to make sure that there is always one glyph
16676 at the end of a glyph row that the cursor can be set on under
16677 window-systems. (If there weren't such a glyph we would not know
16678 how wide and tall a box cursor should be displayed).
16679
16680 At the same time this space let's a nicely handle clearing to the
16681 end of the line if the row ends in italic text. */
16682
16683 static int
16684 append_space_for_newline (it, default_face_p)
16685 struct it *it;
16686 int default_face_p;
16687 {
16688 if (FRAME_WINDOW_P (it->f))
16689 {
16690 int n = it->glyph_row->used[TEXT_AREA];
16691
16692 if (it->glyph_row->glyphs[TEXT_AREA] + n
16693 < it->glyph_row->glyphs[1 + TEXT_AREA])
16694 {
16695 /* Save some values that must not be changed.
16696 Must save IT->c and IT->len because otherwise
16697 ITERATOR_AT_END_P wouldn't work anymore after
16698 append_space_for_newline has been called. */
16699 enum display_element_type saved_what = it->what;
16700 int saved_c = it->c, saved_len = it->len;
16701 int saved_x = it->current_x;
16702 int saved_face_id = it->face_id;
16703 struct text_pos saved_pos;
16704 Lisp_Object saved_object;
16705 struct face *face;
16706
16707 saved_object = it->object;
16708 saved_pos = it->position;
16709
16710 it->what = IT_CHARACTER;
16711 bzero (&it->position, sizeof it->position);
16712 it->object = make_number (0);
16713 it->c = ' ';
16714 it->len = 1;
16715
16716 if (default_face_p)
16717 it->face_id = DEFAULT_FACE_ID;
16718 else if (it->face_before_selective_p)
16719 it->face_id = it->saved_face_id;
16720 face = FACE_FROM_ID (it->f, it->face_id);
16721 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16722
16723 PRODUCE_GLYPHS (it);
16724
16725 it->override_ascent = -1;
16726 it->constrain_row_ascent_descent_p = 0;
16727 it->current_x = saved_x;
16728 it->object = saved_object;
16729 it->position = saved_pos;
16730 it->what = saved_what;
16731 it->face_id = saved_face_id;
16732 it->len = saved_len;
16733 it->c = saved_c;
16734 return 1;
16735 }
16736 }
16737
16738 return 0;
16739 }
16740
16741
16742 /* Extend the face of the last glyph in the text area of IT->glyph_row
16743 to the end of the display line. Called from display_line.
16744 If the glyph row is empty, add a space glyph to it so that we
16745 know the face to draw. Set the glyph row flag fill_line_p. */
16746
16747 static void
16748 extend_face_to_end_of_line (it)
16749 struct it *it;
16750 {
16751 struct face *face;
16752 struct frame *f = it->f;
16753
16754 /* If line is already filled, do nothing. */
16755 if (it->current_x >= it->last_visible_x)
16756 return;
16757
16758 /* Face extension extends the background and box of IT->face_id
16759 to the end of the line. If the background equals the background
16760 of the frame, we don't have to do anything. */
16761 if (it->face_before_selective_p)
16762 face = FACE_FROM_ID (it->f, it->saved_face_id);
16763 else
16764 face = FACE_FROM_ID (f, it->face_id);
16765
16766 if (FRAME_WINDOW_P (f)
16767 && it->glyph_row->displays_text_p
16768 && face->box == FACE_NO_BOX
16769 && face->background == FRAME_BACKGROUND_PIXEL (f)
16770 && !face->stipple)
16771 return;
16772
16773 /* Set the glyph row flag indicating that the face of the last glyph
16774 in the text area has to be drawn to the end of the text area. */
16775 it->glyph_row->fill_line_p = 1;
16776
16777 /* If current character of IT is not ASCII, make sure we have the
16778 ASCII face. This will be automatically undone the next time
16779 get_next_display_element returns a multibyte character. Note
16780 that the character will always be single byte in unibyte
16781 text. */
16782 if (!ASCII_CHAR_P (it->c))
16783 {
16784 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16785 }
16786
16787 if (FRAME_WINDOW_P (f))
16788 {
16789 /* If the row is empty, add a space with the current face of IT,
16790 so that we know which face to draw. */
16791 if (it->glyph_row->used[TEXT_AREA] == 0)
16792 {
16793 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16794 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16795 it->glyph_row->used[TEXT_AREA] = 1;
16796 }
16797 }
16798 else
16799 {
16800 /* Save some values that must not be changed. */
16801 int saved_x = it->current_x;
16802 struct text_pos saved_pos;
16803 Lisp_Object saved_object;
16804 enum display_element_type saved_what = it->what;
16805 int saved_face_id = it->face_id;
16806
16807 saved_object = it->object;
16808 saved_pos = it->position;
16809
16810 it->what = IT_CHARACTER;
16811 bzero (&it->position, sizeof it->position);
16812 it->object = make_number (0);
16813 it->c = ' ';
16814 it->len = 1;
16815 it->face_id = face->id;
16816
16817 PRODUCE_GLYPHS (it);
16818
16819 while (it->current_x <= it->last_visible_x)
16820 PRODUCE_GLYPHS (it);
16821
16822 /* Don't count these blanks really. It would let us insert a left
16823 truncation glyph below and make us set the cursor on them, maybe. */
16824 it->current_x = saved_x;
16825 it->object = saved_object;
16826 it->position = saved_pos;
16827 it->what = saved_what;
16828 it->face_id = saved_face_id;
16829 }
16830 }
16831
16832
16833 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16834 trailing whitespace. */
16835
16836 static int
16837 trailing_whitespace_p (charpos)
16838 int charpos;
16839 {
16840 int bytepos = CHAR_TO_BYTE (charpos);
16841 int c = 0;
16842
16843 while (bytepos < ZV_BYTE
16844 && (c = FETCH_CHAR (bytepos),
16845 c == ' ' || c == '\t'))
16846 ++bytepos;
16847
16848 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16849 {
16850 if (bytepos != PT_BYTE)
16851 return 1;
16852 }
16853 return 0;
16854 }
16855
16856
16857 /* Highlight trailing whitespace, if any, in ROW. */
16858
16859 void
16860 highlight_trailing_whitespace (f, row)
16861 struct frame *f;
16862 struct glyph_row *row;
16863 {
16864 int used = row->used[TEXT_AREA];
16865
16866 if (used)
16867 {
16868 struct glyph *start = row->glyphs[TEXT_AREA];
16869 struct glyph *glyph = start + used - 1;
16870
16871 /* Skip over glyphs inserted to display the cursor at the
16872 end of a line, for extending the face of the last glyph
16873 to the end of the line on terminals, and for truncation
16874 and continuation glyphs. */
16875 while (glyph >= start
16876 && glyph->type == CHAR_GLYPH
16877 && INTEGERP (glyph->object))
16878 --glyph;
16879
16880 /* If last glyph is a space or stretch, and it's trailing
16881 whitespace, set the face of all trailing whitespace glyphs in
16882 IT->glyph_row to `trailing-whitespace'. */
16883 if (glyph >= start
16884 && BUFFERP (glyph->object)
16885 && (glyph->type == STRETCH_GLYPH
16886 || (glyph->type == CHAR_GLYPH
16887 && glyph->u.ch == ' '))
16888 && trailing_whitespace_p (glyph->charpos))
16889 {
16890 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16891 if (face_id < 0)
16892 return;
16893
16894 while (glyph >= start
16895 && BUFFERP (glyph->object)
16896 && (glyph->type == STRETCH_GLYPH
16897 || (glyph->type == CHAR_GLYPH
16898 && glyph->u.ch == ' ')))
16899 (glyph--)->face_id = face_id;
16900 }
16901 }
16902 }
16903
16904
16905 /* Value is non-zero if glyph row ROW in window W should be
16906 used to hold the cursor. */
16907
16908 static int
16909 cursor_row_p (w, row)
16910 struct window *w;
16911 struct glyph_row *row;
16912 {
16913 int cursor_row_p = 1;
16914
16915 if (PT == MATRIX_ROW_END_CHARPOS (row))
16916 {
16917 /* Suppose the row ends on a string.
16918 Unless the row is continued, that means it ends on a newline
16919 in the string. If it's anything other than a display string
16920 (e.g. a before-string from an overlay), we don't want the
16921 cursor there. (This heuristic seems to give the optimal
16922 behavior for the various types of multi-line strings.) */
16923 if (CHARPOS (row->end.string_pos) >= 0)
16924 {
16925 if (row->continued_p)
16926 cursor_row_p = 1;
16927 else
16928 {
16929 /* Check for `display' property. */
16930 struct glyph *beg = row->glyphs[TEXT_AREA];
16931 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16932 struct glyph *glyph;
16933
16934 cursor_row_p = 0;
16935 for (glyph = end; glyph >= beg; --glyph)
16936 if (STRINGP (glyph->object))
16937 {
16938 Lisp_Object prop
16939 = Fget_char_property (make_number (PT),
16940 Qdisplay, Qnil);
16941 cursor_row_p =
16942 (!NILP (prop)
16943 && display_prop_string_p (prop, glyph->object));
16944 break;
16945 }
16946 }
16947 }
16948 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16949 {
16950 /* If the row ends in middle of a real character,
16951 and the line is continued, we want the cursor here.
16952 That's because MATRIX_ROW_END_CHARPOS would equal
16953 PT if PT is before the character. */
16954 if (!row->ends_in_ellipsis_p)
16955 cursor_row_p = row->continued_p;
16956 else
16957 /* If the row ends in an ellipsis, then
16958 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16959 We want that position to be displayed after the ellipsis. */
16960 cursor_row_p = 0;
16961 }
16962 /* If the row ends at ZV, display the cursor at the end of that
16963 row instead of at the start of the row below. */
16964 else if (row->ends_at_zv_p)
16965 cursor_row_p = 1;
16966 else
16967 cursor_row_p = 0;
16968 }
16969
16970 return cursor_row_p;
16971 }
16972
16973 \f
16974
16975 /* Push the display property PROP so that it will be rendered at the
16976 current position in IT. Return 1 if PROP was successfully pushed,
16977 0 otherwise. */
16978
16979 static int
16980 push_display_prop (struct it *it, Lisp_Object prop)
16981 {
16982 push_it (it);
16983
16984 if (STRINGP (prop))
16985 {
16986 if (SCHARS (prop) == 0)
16987 {
16988 pop_it (it);
16989 return 0;
16990 }
16991
16992 it->string = prop;
16993 it->multibyte_p = STRING_MULTIBYTE (it->string);
16994 it->current.overlay_string_index = -1;
16995 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16996 it->end_charpos = it->string_nchars = SCHARS (it->string);
16997 it->method = GET_FROM_STRING;
16998 it->stop_charpos = 0;
16999 }
17000 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17001 {
17002 it->method = GET_FROM_STRETCH;
17003 it->object = prop;
17004 }
17005 #ifdef HAVE_WINDOW_SYSTEM
17006 else if (IMAGEP (prop))
17007 {
17008 it->what = IT_IMAGE;
17009 it->image_id = lookup_image (it->f, prop);
17010 it->method = GET_FROM_IMAGE;
17011 }
17012 #endif /* HAVE_WINDOW_SYSTEM */
17013 else
17014 {
17015 pop_it (it); /* bogus display property, give up */
17016 return 0;
17017 }
17018
17019 return 1;
17020 }
17021
17022 /* Return the character-property PROP at the current position in IT. */
17023
17024 static Lisp_Object
17025 get_it_property (it, prop)
17026 struct it *it;
17027 Lisp_Object prop;
17028 {
17029 Lisp_Object position;
17030
17031 if (STRINGP (it->object))
17032 position = make_number (IT_STRING_CHARPOS (*it));
17033 else if (BUFFERP (it->object))
17034 position = make_number (IT_CHARPOS (*it));
17035 else
17036 return Qnil;
17037
17038 return Fget_char_property (position, prop, it->object);
17039 }
17040
17041 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17042
17043 static void
17044 handle_line_prefix (struct it *it)
17045 {
17046 Lisp_Object prefix;
17047 if (it->continuation_lines_width > 0)
17048 {
17049 prefix = get_it_property (it, Qwrap_prefix);
17050 if (NILP (prefix))
17051 prefix = Vwrap_prefix;
17052 }
17053 else
17054 {
17055 prefix = get_it_property (it, Qline_prefix);
17056 if (NILP (prefix))
17057 prefix = Vline_prefix;
17058 }
17059 if (! NILP (prefix) && push_display_prop (it, prefix))
17060 {
17061 /* If the prefix is wider than the window, and we try to wrap
17062 it, it would acquire its own wrap prefix, and so on till the
17063 iterator stack overflows. So, don't wrap the prefix. */
17064 it->line_wrap = TRUNCATE;
17065 it->avoid_cursor_p = 1;
17066 }
17067 }
17068
17069 \f
17070
17071 /* Construct the glyph row IT->glyph_row in the desired matrix of
17072 IT->w from text at the current position of IT. See dispextern.h
17073 for an overview of struct it. Value is non-zero if
17074 IT->glyph_row displays text, as opposed to a line displaying ZV
17075 only. */
17076
17077 static int
17078 display_line (it)
17079 struct it *it;
17080 {
17081 struct glyph_row *row = it->glyph_row;
17082 Lisp_Object overlay_arrow_string;
17083 struct it wrap_it;
17084 int may_wrap = 0, wrap_x;
17085 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17086 int wrap_row_phys_ascent, wrap_row_phys_height;
17087 int wrap_row_extra_line_spacing;
17088 struct display_pos row_end;
17089 int cvpos;
17090
17091 /* We always start displaying at hpos zero even if hscrolled. */
17092 xassert (it->hpos == 0 && it->current_x == 0);
17093
17094 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17095 >= it->w->desired_matrix->nrows)
17096 {
17097 it->w->nrows_scale_factor++;
17098 fonts_changed_p = 1;
17099 return 0;
17100 }
17101
17102 /* Is IT->w showing the region? */
17103 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17104
17105 /* Clear the result glyph row and enable it. */
17106 prepare_desired_row (row);
17107
17108 row->y = it->current_y;
17109 row->start = it->start;
17110 row->continuation_lines_width = it->continuation_lines_width;
17111 row->displays_text_p = 1;
17112 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17113 it->starts_in_middle_of_char_p = 0;
17114
17115 /* Arrange the overlays nicely for our purposes. Usually, we call
17116 display_line on only one line at a time, in which case this
17117 can't really hurt too much, or we call it on lines which appear
17118 one after another in the buffer, in which case all calls to
17119 recenter_overlay_lists but the first will be pretty cheap. */
17120 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17121
17122 /* Move over display elements that are not visible because we are
17123 hscrolled. This may stop at an x-position < IT->first_visible_x
17124 if the first glyph is partially visible or if we hit a line end. */
17125 if (it->current_x < it->first_visible_x)
17126 {
17127 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17128 MOVE_TO_POS | MOVE_TO_X);
17129 }
17130 else
17131 {
17132 /* We only do this when not calling `move_it_in_display_line_to'
17133 above, because move_it_in_display_line_to calls
17134 handle_line_prefix itself. */
17135 handle_line_prefix (it);
17136 }
17137
17138 /* Get the initial row height. This is either the height of the
17139 text hscrolled, if there is any, or zero. */
17140 row->ascent = it->max_ascent;
17141 row->height = it->max_ascent + it->max_descent;
17142 row->phys_ascent = it->max_phys_ascent;
17143 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17144 row->extra_line_spacing = it->max_extra_line_spacing;
17145
17146 /* Loop generating characters. The loop is left with IT on the next
17147 character to display. */
17148 while (1)
17149 {
17150 int n_glyphs_before, hpos_before, x_before;
17151 int x, i, nglyphs;
17152 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17153
17154 /* Retrieve the next thing to display. Value is zero if end of
17155 buffer reached. */
17156 if (!get_next_display_element (it))
17157 {
17158 /* Maybe add a space at the end of this line that is used to
17159 display the cursor there under X. Set the charpos of the
17160 first glyph of blank lines not corresponding to any text
17161 to -1. */
17162 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17163 row->exact_window_width_line_p = 1;
17164 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17165 || row->used[TEXT_AREA] == 0)
17166 {
17167 row->glyphs[TEXT_AREA]->charpos = -1;
17168 row->displays_text_p = 0;
17169
17170 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17171 && (!MINI_WINDOW_P (it->w)
17172 || (minibuf_level && EQ (it->window, minibuf_window))))
17173 row->indicate_empty_line_p = 1;
17174 }
17175
17176 it->continuation_lines_width = 0;
17177 row->ends_at_zv_p = 1;
17178 /* A row that displays right-to-left text must always have
17179 its last face extended all the way to the end of line,
17180 even if this row ends in ZV. */
17181 if (row->reversed_p)
17182 extend_face_to_end_of_line (it);
17183 break;
17184 }
17185
17186 /* Now, get the metrics of what we want to display. This also
17187 generates glyphs in `row' (which is IT->glyph_row). */
17188 n_glyphs_before = row->used[TEXT_AREA];
17189 x = it->current_x;
17190
17191 /* Remember the line height so far in case the next element doesn't
17192 fit on the line. */
17193 if (it->line_wrap != TRUNCATE)
17194 {
17195 ascent = it->max_ascent;
17196 descent = it->max_descent;
17197 phys_ascent = it->max_phys_ascent;
17198 phys_descent = it->max_phys_descent;
17199
17200 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17201 {
17202 if (IT_DISPLAYING_WHITESPACE (it))
17203 may_wrap = 1;
17204 else if (may_wrap)
17205 {
17206 wrap_it = *it;
17207 wrap_x = x;
17208 wrap_row_used = row->used[TEXT_AREA];
17209 wrap_row_ascent = row->ascent;
17210 wrap_row_height = row->height;
17211 wrap_row_phys_ascent = row->phys_ascent;
17212 wrap_row_phys_height = row->phys_height;
17213 wrap_row_extra_line_spacing = row->extra_line_spacing;
17214 may_wrap = 0;
17215 }
17216 }
17217 }
17218
17219 PRODUCE_GLYPHS (it);
17220
17221 /* If this display element was in marginal areas, continue with
17222 the next one. */
17223 if (it->area != TEXT_AREA)
17224 {
17225 row->ascent = max (row->ascent, it->max_ascent);
17226 row->height = max (row->height, it->max_ascent + it->max_descent);
17227 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17228 row->phys_height = max (row->phys_height,
17229 it->max_phys_ascent + it->max_phys_descent);
17230 row->extra_line_spacing = max (row->extra_line_spacing,
17231 it->max_extra_line_spacing);
17232 set_iterator_to_next (it, 1);
17233 continue;
17234 }
17235
17236 /* Does the display element fit on the line? If we truncate
17237 lines, we should draw past the right edge of the window. If
17238 we don't truncate, we want to stop so that we can display the
17239 continuation glyph before the right margin. If lines are
17240 continued, there are two possible strategies for characters
17241 resulting in more than 1 glyph (e.g. tabs): Display as many
17242 glyphs as possible in this line and leave the rest for the
17243 continuation line, or display the whole element in the next
17244 line. Original redisplay did the former, so we do it also. */
17245 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17246 hpos_before = it->hpos;
17247 x_before = x;
17248
17249 if (/* Not a newline. */
17250 nglyphs > 0
17251 /* Glyphs produced fit entirely in the line. */
17252 && it->current_x < it->last_visible_x)
17253 {
17254 it->hpos += nglyphs;
17255 row->ascent = max (row->ascent, it->max_ascent);
17256 row->height = max (row->height, it->max_ascent + it->max_descent);
17257 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17258 row->phys_height = max (row->phys_height,
17259 it->max_phys_ascent + it->max_phys_descent);
17260 row->extra_line_spacing = max (row->extra_line_spacing,
17261 it->max_extra_line_spacing);
17262 if (it->current_x - it->pixel_width < it->first_visible_x)
17263 row->x = x - it->first_visible_x;
17264 }
17265 else
17266 {
17267 int new_x;
17268 struct glyph *glyph;
17269
17270 for (i = 0; i < nglyphs; ++i, x = new_x)
17271 {
17272 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17273 new_x = x + glyph->pixel_width;
17274
17275 if (/* Lines are continued. */
17276 it->line_wrap != TRUNCATE
17277 && (/* Glyph doesn't fit on the line. */
17278 new_x > it->last_visible_x
17279 /* Or it fits exactly on a window system frame. */
17280 || (new_x == it->last_visible_x
17281 && FRAME_WINDOW_P (it->f))))
17282 {
17283 /* End of a continued line. */
17284
17285 if (it->hpos == 0
17286 || (new_x == it->last_visible_x
17287 && FRAME_WINDOW_P (it->f)))
17288 {
17289 /* Current glyph is the only one on the line or
17290 fits exactly on the line. We must continue
17291 the line because we can't draw the cursor
17292 after the glyph. */
17293 row->continued_p = 1;
17294 it->current_x = new_x;
17295 it->continuation_lines_width += new_x;
17296 ++it->hpos;
17297 if (i == nglyphs - 1)
17298 {
17299 /* If line-wrap is on, check if a previous
17300 wrap point was found. */
17301 if (wrap_row_used > 0
17302 /* Even if there is a previous wrap
17303 point, continue the line here as
17304 usual, if (i) the previous character
17305 was a space or tab AND (ii) the
17306 current character is not. */
17307 && (!may_wrap
17308 || IT_DISPLAYING_WHITESPACE (it)))
17309 goto back_to_wrap;
17310
17311 set_iterator_to_next (it, 1);
17312 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17313 {
17314 if (!get_next_display_element (it))
17315 {
17316 row->exact_window_width_line_p = 1;
17317 it->continuation_lines_width = 0;
17318 row->continued_p = 0;
17319 row->ends_at_zv_p = 1;
17320 }
17321 else if (ITERATOR_AT_END_OF_LINE_P (it))
17322 {
17323 row->continued_p = 0;
17324 row->exact_window_width_line_p = 1;
17325 }
17326 }
17327 }
17328 }
17329 else if (CHAR_GLYPH_PADDING_P (*glyph)
17330 && !FRAME_WINDOW_P (it->f))
17331 {
17332 /* A padding glyph that doesn't fit on this line.
17333 This means the whole character doesn't fit
17334 on the line. */
17335 row->used[TEXT_AREA] = n_glyphs_before;
17336
17337 /* Fill the rest of the row with continuation
17338 glyphs like in 20.x. */
17339 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17340 < row->glyphs[1 + TEXT_AREA])
17341 produce_special_glyphs (it, IT_CONTINUATION);
17342
17343 row->continued_p = 1;
17344 it->current_x = x_before;
17345 it->continuation_lines_width += x_before;
17346
17347 /* Restore the height to what it was before the
17348 element not fitting on the line. */
17349 it->max_ascent = ascent;
17350 it->max_descent = descent;
17351 it->max_phys_ascent = phys_ascent;
17352 it->max_phys_descent = phys_descent;
17353 }
17354 else if (wrap_row_used > 0)
17355 {
17356 back_to_wrap:
17357 *it = wrap_it;
17358 it->continuation_lines_width += wrap_x;
17359 row->used[TEXT_AREA] = wrap_row_used;
17360 row->ascent = wrap_row_ascent;
17361 row->height = wrap_row_height;
17362 row->phys_ascent = wrap_row_phys_ascent;
17363 row->phys_height = wrap_row_phys_height;
17364 row->extra_line_spacing = wrap_row_extra_line_spacing;
17365 row->continued_p = 1;
17366 row->ends_at_zv_p = 0;
17367 row->exact_window_width_line_p = 0;
17368 it->continuation_lines_width += x;
17369
17370 /* Make sure that a non-default face is extended
17371 up to the right margin of the window. */
17372 extend_face_to_end_of_line (it);
17373 }
17374 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17375 {
17376 /* A TAB that extends past the right edge of the
17377 window. This produces a single glyph on
17378 window system frames. We leave the glyph in
17379 this row and let it fill the row, but don't
17380 consume the TAB. */
17381 it->continuation_lines_width += it->last_visible_x;
17382 row->ends_in_middle_of_char_p = 1;
17383 row->continued_p = 1;
17384 glyph->pixel_width = it->last_visible_x - x;
17385 it->starts_in_middle_of_char_p = 1;
17386 }
17387 else
17388 {
17389 /* Something other than a TAB that draws past
17390 the right edge of the window. Restore
17391 positions to values before the element. */
17392 row->used[TEXT_AREA] = n_glyphs_before + i;
17393
17394 /* Display continuation glyphs. */
17395 if (!FRAME_WINDOW_P (it->f))
17396 produce_special_glyphs (it, IT_CONTINUATION);
17397 row->continued_p = 1;
17398
17399 it->current_x = x_before;
17400 it->continuation_lines_width += x;
17401 extend_face_to_end_of_line (it);
17402
17403 if (nglyphs > 1 && i > 0)
17404 {
17405 row->ends_in_middle_of_char_p = 1;
17406 it->starts_in_middle_of_char_p = 1;
17407 }
17408
17409 /* Restore the height to what it was before the
17410 element not fitting on the line. */
17411 it->max_ascent = ascent;
17412 it->max_descent = descent;
17413 it->max_phys_ascent = phys_ascent;
17414 it->max_phys_descent = phys_descent;
17415 }
17416
17417 break;
17418 }
17419 else if (new_x > it->first_visible_x)
17420 {
17421 /* Increment number of glyphs actually displayed. */
17422 ++it->hpos;
17423
17424 if (x < it->first_visible_x)
17425 /* Glyph is partially visible, i.e. row starts at
17426 negative X position. */
17427 row->x = x - it->first_visible_x;
17428 }
17429 else
17430 {
17431 /* Glyph is completely off the left margin of the
17432 window. This should not happen because of the
17433 move_it_in_display_line at the start of this
17434 function, unless the text display area of the
17435 window is empty. */
17436 xassert (it->first_visible_x <= it->last_visible_x);
17437 }
17438 }
17439
17440 row->ascent = max (row->ascent, it->max_ascent);
17441 row->height = max (row->height, it->max_ascent + it->max_descent);
17442 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17443 row->phys_height = max (row->phys_height,
17444 it->max_phys_ascent + it->max_phys_descent);
17445 row->extra_line_spacing = max (row->extra_line_spacing,
17446 it->max_extra_line_spacing);
17447
17448 /* End of this display line if row is continued. */
17449 if (row->continued_p || row->ends_at_zv_p)
17450 break;
17451 }
17452
17453 at_end_of_line:
17454 /* Is this a line end? If yes, we're also done, after making
17455 sure that a non-default face is extended up to the right
17456 margin of the window. */
17457 if (ITERATOR_AT_END_OF_LINE_P (it))
17458 {
17459 int used_before = row->used[TEXT_AREA];
17460
17461 row->ends_in_newline_from_string_p = STRINGP (it->object);
17462
17463 /* Add a space at the end of the line that is used to
17464 display the cursor there. */
17465 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17466 append_space_for_newline (it, 0);
17467
17468 /* Extend the face to the end of the line. */
17469 extend_face_to_end_of_line (it);
17470
17471 /* Make sure we have the position. */
17472 if (used_before == 0)
17473 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17474
17475 /* Consume the line end. This skips over invisible lines. */
17476 set_iterator_to_next (it, 1);
17477 it->continuation_lines_width = 0;
17478 break;
17479 }
17480
17481 /* Proceed with next display element. Note that this skips
17482 over lines invisible because of selective display. */
17483 set_iterator_to_next (it, 1);
17484
17485 /* If we truncate lines, we are done when the last displayed
17486 glyphs reach past the right margin of the window. */
17487 if (it->line_wrap == TRUNCATE
17488 && (FRAME_WINDOW_P (it->f)
17489 ? (it->current_x >= it->last_visible_x)
17490 : (it->current_x > it->last_visible_x)))
17491 {
17492 /* Maybe add truncation glyphs. */
17493 if (!FRAME_WINDOW_P (it->f))
17494 {
17495 int i, n;
17496
17497 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17498 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17499 break;
17500
17501 for (n = row->used[TEXT_AREA]; i < n; ++i)
17502 {
17503 row->used[TEXT_AREA] = i;
17504 produce_special_glyphs (it, IT_TRUNCATION);
17505 }
17506 }
17507 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17508 {
17509 /* Don't truncate if we can overflow newline into fringe. */
17510 if (!get_next_display_element (it))
17511 {
17512 it->continuation_lines_width = 0;
17513 row->ends_at_zv_p = 1;
17514 row->exact_window_width_line_p = 1;
17515 break;
17516 }
17517 if (ITERATOR_AT_END_OF_LINE_P (it))
17518 {
17519 row->exact_window_width_line_p = 1;
17520 goto at_end_of_line;
17521 }
17522 }
17523
17524 row->truncated_on_right_p = 1;
17525 it->continuation_lines_width = 0;
17526 reseat_at_next_visible_line_start (it, 0);
17527 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17528 it->hpos = hpos_before;
17529 it->current_x = x_before;
17530 break;
17531 }
17532 }
17533
17534 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17535 at the left window margin. */
17536 if (it->first_visible_x
17537 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
17538 {
17539 if (!FRAME_WINDOW_P (it->f))
17540 insert_left_trunc_glyphs (it);
17541 row->truncated_on_left_p = 1;
17542 }
17543
17544 /* If the start of this line is the overlay arrow-position, then
17545 mark this glyph row as the one containing the overlay arrow.
17546 This is clearly a mess with variable size fonts. It would be
17547 better to let it be displayed like cursors under X. */
17548 if ((row->displays_text_p || !overlay_arrow_seen)
17549 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17550 !NILP (overlay_arrow_string)))
17551 {
17552 /* Overlay arrow in window redisplay is a fringe bitmap. */
17553 if (STRINGP (overlay_arrow_string))
17554 {
17555 struct glyph_row *arrow_row
17556 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17557 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17558 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17559 struct glyph *p = row->glyphs[TEXT_AREA];
17560 struct glyph *p2, *end;
17561
17562 /* Copy the arrow glyphs. */
17563 while (glyph < arrow_end)
17564 *p++ = *glyph++;
17565
17566 /* Throw away padding glyphs. */
17567 p2 = p;
17568 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17569 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17570 ++p2;
17571 if (p2 > p)
17572 {
17573 while (p2 < end)
17574 *p++ = *p2++;
17575 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17576 }
17577 }
17578 else
17579 {
17580 xassert (INTEGERP (overlay_arrow_string));
17581 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17582 }
17583 overlay_arrow_seen = 1;
17584 }
17585
17586 /* Compute pixel dimensions of this line. */
17587 compute_line_metrics (it);
17588
17589 /* Remember the position at which this line ends. */
17590 row->end = row_end = it->current;
17591 if (it->bidi_p)
17592 {
17593 /* ROW->start and ROW->end must be the smallest and largest
17594 buffer positions in ROW. But if ROW was bidi-reordered,
17595 these two positions can be anywhere in the row, so we must
17596 rescan all of the ROW's glyphs to find them. */
17597 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17598 lines' rows is implemented for bidi-reordered rows. */
17599 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17600 struct glyph *g;
17601 struct it save_it;
17602 struct text_pos tpos;
17603
17604 for (g = row->glyphs[TEXT_AREA];
17605 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17606 g++)
17607 {
17608 if (BUFFERP (g->object))
17609 {
17610 if (g->charpos > 0 && g->charpos < min_pos)
17611 min_pos = g->charpos;
17612 if (g->charpos > max_pos)
17613 max_pos = g->charpos;
17614 }
17615 }
17616 /* Empty lines have a valid buffer position at their first
17617 glyph, but that glyph's OBJECT is zero, as if it didn't come
17618 from a buffer. If we didn't find any valid buffer positions
17619 in this row, maybe we have such an empty line. */
17620 if (min_pos == ZV + 1 && row->used[TEXT_AREA])
17621 {
17622 for (g = row->glyphs[TEXT_AREA];
17623 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17624 g++)
17625 {
17626 if (INTEGERP (g->object))
17627 {
17628 if (g->charpos > 0 && g->charpos < min_pos)
17629 min_pos = g->charpos;
17630 if (g->charpos > max_pos)
17631 max_pos = g->charpos;
17632 }
17633 }
17634 }
17635 if (min_pos <= ZV)
17636 {
17637 if (min_pos != row->start.pos.charpos)
17638 {
17639 row->start.pos.charpos = min_pos;
17640 row->start.pos.bytepos = CHAR_TO_BYTE (min_pos);
17641 }
17642 if (max_pos == 0)
17643 max_pos = min_pos;
17644 }
17645 /* For ROW->end, we need the position that is _after_ max_pos,
17646 in the logical order, unless we are at ZV. */
17647 if (row->ends_at_zv_p)
17648 {
17649 row_end = row->end = it->current;
17650 if (!row->used[TEXT_AREA])
17651 {
17652 row->start.pos.charpos = row_end.pos.charpos;
17653 row->start.pos.bytepos = row_end.pos.bytepos;
17654 }
17655 }
17656 else if (row->used[TEXT_AREA] && max_pos)
17657 {
17658 SET_TEXT_POS (tpos, max_pos + 1, CHAR_TO_BYTE (max_pos + 1));
17659 row_end = it->current;
17660 row_end.pos = tpos;
17661 /* If the character at max_pos+1 is a newline, skip that as
17662 well. Note that this may skip some invisible text. */
17663 if (FETCH_CHAR (tpos.bytepos) == '\n'
17664 || (FETCH_CHAR (tpos.bytepos) == '\r' && it->selective))
17665 {
17666 save_it = *it;
17667 it->bidi_p = 0;
17668 reseat_1 (it, tpos, 0);
17669 set_iterator_to_next (it, 1);
17670 /* Record the position after the newline of a continued
17671 row. We will need that to set ROW->end of the last
17672 row produced for a continued line. */
17673 if (row->continued_p)
17674 {
17675 save_it.eol_pos.charpos = IT_CHARPOS (*it);
17676 save_it.eol_pos.bytepos = IT_BYTEPOS (*it);
17677 }
17678 else
17679 {
17680 row_end = it->current;
17681 save_it.eol_pos.charpos = save_it.eol_pos.bytepos = 0;
17682 }
17683 *it = save_it;
17684 }
17685 else if (!row->continued_p
17686 && row->continuation_lines_width
17687 && it->eol_pos.charpos > 0)
17688 {
17689 /* Last row of a continued line. Use the position
17690 recorded in ROW->eol_pos, to the effect that the
17691 newline belongs to this row, not to the row which
17692 displays the character with the largest buffer
17693 position. */
17694 row_end.pos = it->eol_pos;
17695 it->eol_pos.charpos = it->eol_pos.bytepos = 0;
17696 }
17697 row->end = row_end;
17698 }
17699 }
17700
17701 /* Record whether this row ends inside an ellipsis. */
17702 row->ends_in_ellipsis_p
17703 = (it->method == GET_FROM_DISPLAY_VECTOR
17704 && it->ellipsis_p);
17705
17706 /* Save fringe bitmaps in this row. */
17707 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17708 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17709 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17710 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17711
17712 it->left_user_fringe_bitmap = 0;
17713 it->left_user_fringe_face_id = 0;
17714 it->right_user_fringe_bitmap = 0;
17715 it->right_user_fringe_face_id = 0;
17716
17717 /* Maybe set the cursor. */
17718 cvpos = it->w->cursor.vpos;
17719 if ((cvpos < 0
17720 /* In bidi-reordered rows, keep checking for proper cursor
17721 position even if one has been found already, because buffer
17722 positions in such rows change non-linearly with ROW->VPOS,
17723 when a line is continued. One exception: when we are at ZV,
17724 display cursor on the first suitable glyph row, since all
17725 the empty rows after that also have their position set to ZV. */
17726 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17727 lines' rows is implemented for bidi-reordered rows. */
17728 || (it->bidi_p
17729 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17730 && PT >= MATRIX_ROW_START_CHARPOS (row)
17731 && PT <= MATRIX_ROW_END_CHARPOS (row)
17732 && cursor_row_p (it->w, row))
17733 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17734
17735 /* Highlight trailing whitespace. */
17736 if (!NILP (Vshow_trailing_whitespace))
17737 highlight_trailing_whitespace (it->f, it->glyph_row);
17738
17739 /* Prepare for the next line. This line starts horizontally at (X
17740 HPOS) = (0 0). Vertical positions are incremented. As a
17741 convenience for the caller, IT->glyph_row is set to the next
17742 row to be used. */
17743 it->current_x = it->hpos = 0;
17744 it->current_y += row->height;
17745 ++it->vpos;
17746 ++it->glyph_row;
17747 /* The next row should use same value of the reversed_p flag as this
17748 one. set_iterator_to_next decides when it's a new paragraph, and
17749 PRODUCE_GLYPHS recomputes the value of the flag accordingly. */
17750 it->glyph_row->reversed_p = row->reversed_p;
17751 it->start = row_end;
17752 return row->displays_text_p;
17753 }
17754
17755
17756 \f
17757 /***********************************************************************
17758 Menu Bar
17759 ***********************************************************************/
17760
17761 /* Redisplay the menu bar in the frame for window W.
17762
17763 The menu bar of X frames that don't have X toolkit support is
17764 displayed in a special window W->frame->menu_bar_window.
17765
17766 The menu bar of terminal frames is treated specially as far as
17767 glyph matrices are concerned. Menu bar lines are not part of
17768 windows, so the update is done directly on the frame matrix rows
17769 for the menu bar. */
17770
17771 static void
17772 display_menu_bar (w)
17773 struct window *w;
17774 {
17775 struct frame *f = XFRAME (WINDOW_FRAME (w));
17776 struct it it;
17777 Lisp_Object items;
17778 int i;
17779
17780 /* Don't do all this for graphical frames. */
17781 #ifdef HAVE_NTGUI
17782 if (FRAME_W32_P (f))
17783 return;
17784 #endif
17785 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17786 if (FRAME_X_P (f))
17787 return;
17788 #endif
17789
17790 #ifdef HAVE_NS
17791 if (FRAME_NS_P (f))
17792 return;
17793 #endif /* HAVE_NS */
17794
17795 #ifdef USE_X_TOOLKIT
17796 xassert (!FRAME_WINDOW_P (f));
17797 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17798 it.first_visible_x = 0;
17799 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17800 #else /* not USE_X_TOOLKIT */
17801 if (FRAME_WINDOW_P (f))
17802 {
17803 /* Menu bar lines are displayed in the desired matrix of the
17804 dummy window menu_bar_window. */
17805 struct window *menu_w;
17806 xassert (WINDOWP (f->menu_bar_window));
17807 menu_w = XWINDOW (f->menu_bar_window);
17808 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17809 MENU_FACE_ID);
17810 it.first_visible_x = 0;
17811 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17812 }
17813 else
17814 {
17815 /* This is a TTY frame, i.e. character hpos/vpos are used as
17816 pixel x/y. */
17817 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17818 MENU_FACE_ID);
17819 it.first_visible_x = 0;
17820 it.last_visible_x = FRAME_COLS (f);
17821 }
17822 #endif /* not USE_X_TOOLKIT */
17823
17824 if (! mode_line_inverse_video)
17825 /* Force the menu-bar to be displayed in the default face. */
17826 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17827
17828 /* Clear all rows of the menu bar. */
17829 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17830 {
17831 struct glyph_row *row = it.glyph_row + i;
17832 clear_glyph_row (row);
17833 row->enabled_p = 1;
17834 row->full_width_p = 1;
17835 }
17836
17837 /* Display all items of the menu bar. */
17838 items = FRAME_MENU_BAR_ITEMS (it.f);
17839 for (i = 0; i < XVECTOR (items)->size; i += 4)
17840 {
17841 Lisp_Object string;
17842
17843 /* Stop at nil string. */
17844 string = AREF (items, i + 1);
17845 if (NILP (string))
17846 break;
17847
17848 /* Remember where item was displayed. */
17849 ASET (items, i + 3, make_number (it.hpos));
17850
17851 /* Display the item, pad with one space. */
17852 if (it.current_x < it.last_visible_x)
17853 display_string (NULL, string, Qnil, 0, 0, &it,
17854 SCHARS (string) + 1, 0, 0, -1);
17855 }
17856
17857 /* Fill out the line with spaces. */
17858 if (it.current_x < it.last_visible_x)
17859 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17860
17861 /* Compute the total height of the lines. */
17862 compute_line_metrics (&it);
17863 }
17864
17865
17866 \f
17867 /***********************************************************************
17868 Mode Line
17869 ***********************************************************************/
17870
17871 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17872 FORCE is non-zero, redisplay mode lines unconditionally.
17873 Otherwise, redisplay only mode lines that are garbaged. Value is
17874 the number of windows whose mode lines were redisplayed. */
17875
17876 static int
17877 redisplay_mode_lines (window, force)
17878 Lisp_Object window;
17879 int force;
17880 {
17881 int nwindows = 0;
17882
17883 while (!NILP (window))
17884 {
17885 struct window *w = XWINDOW (window);
17886
17887 if (WINDOWP (w->hchild))
17888 nwindows += redisplay_mode_lines (w->hchild, force);
17889 else if (WINDOWP (w->vchild))
17890 nwindows += redisplay_mode_lines (w->vchild, force);
17891 else if (force
17892 || FRAME_GARBAGED_P (XFRAME (w->frame))
17893 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17894 {
17895 struct text_pos lpoint;
17896 struct buffer *old = current_buffer;
17897
17898 /* Set the window's buffer for the mode line display. */
17899 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17900 set_buffer_internal_1 (XBUFFER (w->buffer));
17901
17902 /* Point refers normally to the selected window. For any
17903 other window, set up appropriate value. */
17904 if (!EQ (window, selected_window))
17905 {
17906 struct text_pos pt;
17907
17908 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17909 if (CHARPOS (pt) < BEGV)
17910 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17911 else if (CHARPOS (pt) > (ZV - 1))
17912 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17913 else
17914 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17915 }
17916
17917 /* Display mode lines. */
17918 clear_glyph_matrix (w->desired_matrix);
17919 if (display_mode_lines (w))
17920 {
17921 ++nwindows;
17922 w->must_be_updated_p = 1;
17923 }
17924
17925 /* Restore old settings. */
17926 set_buffer_internal_1 (old);
17927 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17928 }
17929
17930 window = w->next;
17931 }
17932
17933 return nwindows;
17934 }
17935
17936
17937 /* Display the mode and/or header line of window W. Value is the
17938 sum number of mode lines and header lines displayed. */
17939
17940 static int
17941 display_mode_lines (w)
17942 struct window *w;
17943 {
17944 Lisp_Object old_selected_window, old_selected_frame;
17945 int n = 0;
17946
17947 old_selected_frame = selected_frame;
17948 selected_frame = w->frame;
17949 old_selected_window = selected_window;
17950 XSETWINDOW (selected_window, w);
17951
17952 /* These will be set while the mode line specs are processed. */
17953 line_number_displayed = 0;
17954 w->column_number_displayed = Qnil;
17955
17956 if (WINDOW_WANTS_MODELINE_P (w))
17957 {
17958 struct window *sel_w = XWINDOW (old_selected_window);
17959
17960 /* Select mode line face based on the real selected window. */
17961 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17962 current_buffer->mode_line_format);
17963 ++n;
17964 }
17965
17966 if (WINDOW_WANTS_HEADER_LINE_P (w))
17967 {
17968 display_mode_line (w, HEADER_LINE_FACE_ID,
17969 current_buffer->header_line_format);
17970 ++n;
17971 }
17972
17973 selected_frame = old_selected_frame;
17974 selected_window = old_selected_window;
17975 return n;
17976 }
17977
17978
17979 /* Display mode or header line of window W. FACE_ID specifies which
17980 line to display; it is either MODE_LINE_FACE_ID or
17981 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17982 display. Value is the pixel height of the mode/header line
17983 displayed. */
17984
17985 static int
17986 display_mode_line (w, face_id, format)
17987 struct window *w;
17988 enum face_id face_id;
17989 Lisp_Object format;
17990 {
17991 struct it it;
17992 struct face *face;
17993 int count = SPECPDL_INDEX ();
17994
17995 init_iterator (&it, w, -1, -1, NULL, face_id);
17996 /* Don't extend on a previously drawn mode-line.
17997 This may happen if called from pos_visible_p. */
17998 it.glyph_row->enabled_p = 0;
17999 prepare_desired_row (it.glyph_row);
18000
18001 it.glyph_row->mode_line_p = 1;
18002
18003 if (! mode_line_inverse_video)
18004 /* Force the mode-line to be displayed in the default face. */
18005 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18006
18007 record_unwind_protect (unwind_format_mode_line,
18008 format_mode_line_unwind_data (NULL, Qnil, 0));
18009
18010 mode_line_target = MODE_LINE_DISPLAY;
18011
18012 /* Temporarily make frame's keyboard the current kboard so that
18013 kboard-local variables in the mode_line_format will get the right
18014 values. */
18015 push_kboard (FRAME_KBOARD (it.f));
18016 record_unwind_save_match_data ();
18017 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18018 pop_kboard ();
18019
18020 unbind_to (count, Qnil);
18021
18022 /* Fill up with spaces. */
18023 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18024
18025 compute_line_metrics (&it);
18026 it.glyph_row->full_width_p = 1;
18027 it.glyph_row->continued_p = 0;
18028 it.glyph_row->truncated_on_left_p = 0;
18029 it.glyph_row->truncated_on_right_p = 0;
18030
18031 /* Make a 3D mode-line have a shadow at its right end. */
18032 face = FACE_FROM_ID (it.f, face_id);
18033 extend_face_to_end_of_line (&it);
18034 if (face->box != FACE_NO_BOX)
18035 {
18036 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18037 + it.glyph_row->used[TEXT_AREA] - 1);
18038 last->right_box_line_p = 1;
18039 }
18040
18041 return it.glyph_row->height;
18042 }
18043
18044 /* Move element ELT in LIST to the front of LIST.
18045 Return the updated list. */
18046
18047 static Lisp_Object
18048 move_elt_to_front (elt, list)
18049 Lisp_Object elt, list;
18050 {
18051 register Lisp_Object tail, prev;
18052 register Lisp_Object tem;
18053
18054 tail = list;
18055 prev = Qnil;
18056 while (CONSP (tail))
18057 {
18058 tem = XCAR (tail);
18059
18060 if (EQ (elt, tem))
18061 {
18062 /* Splice out the link TAIL. */
18063 if (NILP (prev))
18064 list = XCDR (tail);
18065 else
18066 Fsetcdr (prev, XCDR (tail));
18067
18068 /* Now make it the first. */
18069 Fsetcdr (tail, list);
18070 return tail;
18071 }
18072 else
18073 prev = tail;
18074 tail = XCDR (tail);
18075 QUIT;
18076 }
18077
18078 /* Not found--return unchanged LIST. */
18079 return list;
18080 }
18081
18082 /* Contribute ELT to the mode line for window IT->w. How it
18083 translates into text depends on its data type.
18084
18085 IT describes the display environment in which we display, as usual.
18086
18087 DEPTH is the depth in recursion. It is used to prevent
18088 infinite recursion here.
18089
18090 FIELD_WIDTH is the number of characters the display of ELT should
18091 occupy in the mode line, and PRECISION is the maximum number of
18092 characters to display from ELT's representation. See
18093 display_string for details.
18094
18095 Returns the hpos of the end of the text generated by ELT.
18096
18097 PROPS is a property list to add to any string we encounter.
18098
18099 If RISKY is nonzero, remove (disregard) any properties in any string
18100 we encounter, and ignore :eval and :propertize.
18101
18102 The global variable `mode_line_target' determines whether the
18103 output is passed to `store_mode_line_noprop',
18104 `store_mode_line_string', or `display_string'. */
18105
18106 static int
18107 display_mode_element (it, depth, field_width, precision, elt, props, risky)
18108 struct it *it;
18109 int depth;
18110 int field_width, precision;
18111 Lisp_Object elt, props;
18112 int risky;
18113 {
18114 int n = 0, field, prec;
18115 int literal = 0;
18116
18117 tail_recurse:
18118 if (depth > 100)
18119 elt = build_string ("*too-deep*");
18120
18121 depth++;
18122
18123 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18124 {
18125 case Lisp_String:
18126 {
18127 /* A string: output it and check for %-constructs within it. */
18128 unsigned char c;
18129 int offset = 0;
18130
18131 if (SCHARS (elt) > 0
18132 && (!NILP (props) || risky))
18133 {
18134 Lisp_Object oprops, aelt;
18135 oprops = Ftext_properties_at (make_number (0), elt);
18136
18137 /* If the starting string's properties are not what
18138 we want, translate the string. Also, if the string
18139 is risky, do that anyway. */
18140
18141 if (NILP (Fequal (props, oprops)) || risky)
18142 {
18143 /* If the starting string has properties,
18144 merge the specified ones onto the existing ones. */
18145 if (! NILP (oprops) && !risky)
18146 {
18147 Lisp_Object tem;
18148
18149 oprops = Fcopy_sequence (oprops);
18150 tem = props;
18151 while (CONSP (tem))
18152 {
18153 oprops = Fplist_put (oprops, XCAR (tem),
18154 XCAR (XCDR (tem)));
18155 tem = XCDR (XCDR (tem));
18156 }
18157 props = oprops;
18158 }
18159
18160 aelt = Fassoc (elt, mode_line_proptrans_alist);
18161 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18162 {
18163 /* AELT is what we want. Move it to the front
18164 without consing. */
18165 elt = XCAR (aelt);
18166 mode_line_proptrans_alist
18167 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18168 }
18169 else
18170 {
18171 Lisp_Object tem;
18172
18173 /* If AELT has the wrong props, it is useless.
18174 so get rid of it. */
18175 if (! NILP (aelt))
18176 mode_line_proptrans_alist
18177 = Fdelq (aelt, mode_line_proptrans_alist);
18178
18179 elt = Fcopy_sequence (elt);
18180 Fset_text_properties (make_number (0), Flength (elt),
18181 props, elt);
18182 /* Add this item to mode_line_proptrans_alist. */
18183 mode_line_proptrans_alist
18184 = Fcons (Fcons (elt, props),
18185 mode_line_proptrans_alist);
18186 /* Truncate mode_line_proptrans_alist
18187 to at most 50 elements. */
18188 tem = Fnthcdr (make_number (50),
18189 mode_line_proptrans_alist);
18190 if (! NILP (tem))
18191 XSETCDR (tem, Qnil);
18192 }
18193 }
18194 }
18195
18196 offset = 0;
18197
18198 if (literal)
18199 {
18200 prec = precision - n;
18201 switch (mode_line_target)
18202 {
18203 case MODE_LINE_NOPROP:
18204 case MODE_LINE_TITLE:
18205 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18206 break;
18207 case MODE_LINE_STRING:
18208 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18209 break;
18210 case MODE_LINE_DISPLAY:
18211 n += display_string (NULL, elt, Qnil, 0, 0, it,
18212 0, prec, 0, STRING_MULTIBYTE (elt));
18213 break;
18214 }
18215
18216 break;
18217 }
18218
18219 /* Handle the non-literal case. */
18220
18221 while ((precision <= 0 || n < precision)
18222 && SREF (elt, offset) != 0
18223 && (mode_line_target != MODE_LINE_DISPLAY
18224 || it->current_x < it->last_visible_x))
18225 {
18226 int last_offset = offset;
18227
18228 /* Advance to end of string or next format specifier. */
18229 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18230 ;
18231
18232 if (offset - 1 != last_offset)
18233 {
18234 int nchars, nbytes;
18235
18236 /* Output to end of string or up to '%'. Field width
18237 is length of string. Don't output more than
18238 PRECISION allows us. */
18239 offset--;
18240
18241 prec = c_string_width (SDATA (elt) + last_offset,
18242 offset - last_offset, precision - n,
18243 &nchars, &nbytes);
18244
18245 switch (mode_line_target)
18246 {
18247 case MODE_LINE_NOPROP:
18248 case MODE_LINE_TITLE:
18249 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18250 break;
18251 case MODE_LINE_STRING:
18252 {
18253 int bytepos = last_offset;
18254 int charpos = string_byte_to_char (elt, bytepos);
18255 int endpos = (precision <= 0
18256 ? string_byte_to_char (elt, offset)
18257 : charpos + nchars);
18258
18259 n += store_mode_line_string (NULL,
18260 Fsubstring (elt, make_number (charpos),
18261 make_number (endpos)),
18262 0, 0, 0, Qnil);
18263 }
18264 break;
18265 case MODE_LINE_DISPLAY:
18266 {
18267 int bytepos = last_offset;
18268 int charpos = string_byte_to_char (elt, bytepos);
18269
18270 if (precision <= 0)
18271 nchars = string_byte_to_char (elt, offset) - charpos;
18272 n += display_string (NULL, elt, Qnil, 0, charpos,
18273 it, 0, nchars, 0,
18274 STRING_MULTIBYTE (elt));
18275 }
18276 break;
18277 }
18278 }
18279 else /* c == '%' */
18280 {
18281 int percent_position = offset;
18282
18283 /* Get the specified minimum width. Zero means
18284 don't pad. */
18285 field = 0;
18286 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18287 field = field * 10 + c - '0';
18288
18289 /* Don't pad beyond the total padding allowed. */
18290 if (field_width - n > 0 && field > field_width - n)
18291 field = field_width - n;
18292
18293 /* Note that either PRECISION <= 0 or N < PRECISION. */
18294 prec = precision - n;
18295
18296 if (c == 'M')
18297 n += display_mode_element (it, depth, field, prec,
18298 Vglobal_mode_string, props,
18299 risky);
18300 else if (c != 0)
18301 {
18302 int multibyte;
18303 int bytepos, charpos;
18304 unsigned char *spec;
18305 Lisp_Object string;
18306
18307 bytepos = percent_position;
18308 charpos = (STRING_MULTIBYTE (elt)
18309 ? string_byte_to_char (elt, bytepos)
18310 : bytepos);
18311 spec = decode_mode_spec (it->w, c, field, prec, &string);
18312 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18313
18314 switch (mode_line_target)
18315 {
18316 case MODE_LINE_NOPROP:
18317 case MODE_LINE_TITLE:
18318 n += store_mode_line_noprop (spec, field, prec);
18319 break;
18320 case MODE_LINE_STRING:
18321 {
18322 int len = strlen (spec);
18323 Lisp_Object tem = make_string (spec, len);
18324 props = Ftext_properties_at (make_number (charpos), elt);
18325 /* Should only keep face property in props */
18326 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18327 }
18328 break;
18329 case MODE_LINE_DISPLAY:
18330 {
18331 int nglyphs_before, nwritten;
18332
18333 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18334 nwritten = display_string (spec, string, elt,
18335 charpos, 0, it,
18336 field, prec, 0,
18337 multibyte);
18338
18339 /* Assign to the glyphs written above the
18340 string where the `%x' came from, position
18341 of the `%'. */
18342 if (nwritten > 0)
18343 {
18344 struct glyph *glyph
18345 = (it->glyph_row->glyphs[TEXT_AREA]
18346 + nglyphs_before);
18347 int i;
18348
18349 for (i = 0; i < nwritten; ++i)
18350 {
18351 glyph[i].object = elt;
18352 glyph[i].charpos = charpos;
18353 }
18354
18355 n += nwritten;
18356 }
18357 }
18358 break;
18359 }
18360 }
18361 else /* c == 0 */
18362 break;
18363 }
18364 }
18365 }
18366 break;
18367
18368 case Lisp_Symbol:
18369 /* A symbol: process the value of the symbol recursively
18370 as if it appeared here directly. Avoid error if symbol void.
18371 Special case: if value of symbol is a string, output the string
18372 literally. */
18373 {
18374 register Lisp_Object tem;
18375
18376 /* If the variable is not marked as risky to set
18377 then its contents are risky to use. */
18378 if (NILP (Fget (elt, Qrisky_local_variable)))
18379 risky = 1;
18380
18381 tem = Fboundp (elt);
18382 if (!NILP (tem))
18383 {
18384 tem = Fsymbol_value (elt);
18385 /* If value is a string, output that string literally:
18386 don't check for % within it. */
18387 if (STRINGP (tem))
18388 literal = 1;
18389
18390 if (!EQ (tem, elt))
18391 {
18392 /* Give up right away for nil or t. */
18393 elt = tem;
18394 goto tail_recurse;
18395 }
18396 }
18397 }
18398 break;
18399
18400 case Lisp_Cons:
18401 {
18402 register Lisp_Object car, tem;
18403
18404 /* A cons cell: five distinct cases.
18405 If first element is :eval or :propertize, do something special.
18406 If first element is a string or a cons, process all the elements
18407 and effectively concatenate them.
18408 If first element is a negative number, truncate displaying cdr to
18409 at most that many characters. If positive, pad (with spaces)
18410 to at least that many characters.
18411 If first element is a symbol, process the cadr or caddr recursively
18412 according to whether the symbol's value is non-nil or nil. */
18413 car = XCAR (elt);
18414 if (EQ (car, QCeval))
18415 {
18416 /* An element of the form (:eval FORM) means evaluate FORM
18417 and use the result as mode line elements. */
18418
18419 if (risky)
18420 break;
18421
18422 if (CONSP (XCDR (elt)))
18423 {
18424 Lisp_Object spec;
18425 spec = safe_eval (XCAR (XCDR (elt)));
18426 n += display_mode_element (it, depth, field_width - n,
18427 precision - n, spec, props,
18428 risky);
18429 }
18430 }
18431 else if (EQ (car, QCpropertize))
18432 {
18433 /* An element of the form (:propertize ELT PROPS...)
18434 means display ELT but applying properties PROPS. */
18435
18436 if (risky)
18437 break;
18438
18439 if (CONSP (XCDR (elt)))
18440 n += display_mode_element (it, depth, field_width - n,
18441 precision - n, XCAR (XCDR (elt)),
18442 XCDR (XCDR (elt)), risky);
18443 }
18444 else if (SYMBOLP (car))
18445 {
18446 tem = Fboundp (car);
18447 elt = XCDR (elt);
18448 if (!CONSP (elt))
18449 goto invalid;
18450 /* elt is now the cdr, and we know it is a cons cell.
18451 Use its car if CAR has a non-nil value. */
18452 if (!NILP (tem))
18453 {
18454 tem = Fsymbol_value (car);
18455 if (!NILP (tem))
18456 {
18457 elt = XCAR (elt);
18458 goto tail_recurse;
18459 }
18460 }
18461 /* Symbol's value is nil (or symbol is unbound)
18462 Get the cddr of the original list
18463 and if possible find the caddr and use that. */
18464 elt = XCDR (elt);
18465 if (NILP (elt))
18466 break;
18467 else if (!CONSP (elt))
18468 goto invalid;
18469 elt = XCAR (elt);
18470 goto tail_recurse;
18471 }
18472 else if (INTEGERP (car))
18473 {
18474 register int lim = XINT (car);
18475 elt = XCDR (elt);
18476 if (lim < 0)
18477 {
18478 /* Negative int means reduce maximum width. */
18479 if (precision <= 0)
18480 precision = -lim;
18481 else
18482 precision = min (precision, -lim);
18483 }
18484 else if (lim > 0)
18485 {
18486 /* Padding specified. Don't let it be more than
18487 current maximum. */
18488 if (precision > 0)
18489 lim = min (precision, lim);
18490
18491 /* If that's more padding than already wanted, queue it.
18492 But don't reduce padding already specified even if
18493 that is beyond the current truncation point. */
18494 field_width = max (lim, field_width);
18495 }
18496 goto tail_recurse;
18497 }
18498 else if (STRINGP (car) || CONSP (car))
18499 {
18500 Lisp_Object halftail = elt;
18501 int len = 0;
18502
18503 while (CONSP (elt)
18504 && (precision <= 0 || n < precision))
18505 {
18506 n += display_mode_element (it, depth,
18507 /* Do padding only after the last
18508 element in the list. */
18509 (! CONSP (XCDR (elt))
18510 ? field_width - n
18511 : 0),
18512 precision - n, XCAR (elt),
18513 props, risky);
18514 elt = XCDR (elt);
18515 len++;
18516 if ((len & 1) == 0)
18517 halftail = XCDR (halftail);
18518 /* Check for cycle. */
18519 if (EQ (halftail, elt))
18520 break;
18521 }
18522 }
18523 }
18524 break;
18525
18526 default:
18527 invalid:
18528 elt = build_string ("*invalid*");
18529 goto tail_recurse;
18530 }
18531
18532 /* Pad to FIELD_WIDTH. */
18533 if (field_width > 0 && n < field_width)
18534 {
18535 switch (mode_line_target)
18536 {
18537 case MODE_LINE_NOPROP:
18538 case MODE_LINE_TITLE:
18539 n += store_mode_line_noprop ("", field_width - n, 0);
18540 break;
18541 case MODE_LINE_STRING:
18542 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18543 break;
18544 case MODE_LINE_DISPLAY:
18545 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18546 0, 0, 0);
18547 break;
18548 }
18549 }
18550
18551 return n;
18552 }
18553
18554 /* Store a mode-line string element in mode_line_string_list.
18555
18556 If STRING is non-null, display that C string. Otherwise, the Lisp
18557 string LISP_STRING is displayed.
18558
18559 FIELD_WIDTH is the minimum number of output glyphs to produce.
18560 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18561 with spaces. FIELD_WIDTH <= 0 means don't pad.
18562
18563 PRECISION is the maximum number of characters to output from
18564 STRING. PRECISION <= 0 means don't truncate the string.
18565
18566 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18567 properties to the string.
18568
18569 PROPS are the properties to add to the string.
18570 The mode_line_string_face face property is always added to the string.
18571 */
18572
18573 static int
18574 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
18575 char *string;
18576 Lisp_Object lisp_string;
18577 int copy_string;
18578 int field_width;
18579 int precision;
18580 Lisp_Object props;
18581 {
18582 int len;
18583 int n = 0;
18584
18585 if (string != NULL)
18586 {
18587 len = strlen (string);
18588 if (precision > 0 && len > precision)
18589 len = precision;
18590 lisp_string = make_string (string, len);
18591 if (NILP (props))
18592 props = mode_line_string_face_prop;
18593 else if (!NILP (mode_line_string_face))
18594 {
18595 Lisp_Object face = Fplist_get (props, Qface);
18596 props = Fcopy_sequence (props);
18597 if (NILP (face))
18598 face = mode_line_string_face;
18599 else
18600 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18601 props = Fplist_put (props, Qface, face);
18602 }
18603 Fadd_text_properties (make_number (0), make_number (len),
18604 props, lisp_string);
18605 }
18606 else
18607 {
18608 len = XFASTINT (Flength (lisp_string));
18609 if (precision > 0 && len > precision)
18610 {
18611 len = precision;
18612 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18613 precision = -1;
18614 }
18615 if (!NILP (mode_line_string_face))
18616 {
18617 Lisp_Object face;
18618 if (NILP (props))
18619 props = Ftext_properties_at (make_number (0), lisp_string);
18620 face = Fplist_get (props, Qface);
18621 if (NILP (face))
18622 face = mode_line_string_face;
18623 else
18624 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18625 props = Fcons (Qface, Fcons (face, Qnil));
18626 if (copy_string)
18627 lisp_string = Fcopy_sequence (lisp_string);
18628 }
18629 if (!NILP (props))
18630 Fadd_text_properties (make_number (0), make_number (len),
18631 props, lisp_string);
18632 }
18633
18634 if (len > 0)
18635 {
18636 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18637 n += len;
18638 }
18639
18640 if (field_width > len)
18641 {
18642 field_width -= len;
18643 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18644 if (!NILP (props))
18645 Fadd_text_properties (make_number (0), make_number (field_width),
18646 props, lisp_string);
18647 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18648 n += field_width;
18649 }
18650
18651 return n;
18652 }
18653
18654
18655 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18656 1, 4, 0,
18657 doc: /* Format a string out of a mode line format specification.
18658 First arg FORMAT specifies the mode line format (see `mode-line-format'
18659 for details) to use.
18660
18661 Optional second arg FACE specifies the face property to put
18662 on all characters for which no face is specified.
18663 The value t means whatever face the window's mode line currently uses
18664 \(either `mode-line' or `mode-line-inactive', depending).
18665 A value of nil means the default is no face property.
18666 If FACE is an integer, the value string has no text properties.
18667
18668 Optional third and fourth args WINDOW and BUFFER specify the window
18669 and buffer to use as the context for the formatting (defaults
18670 are the selected window and the window's buffer). */)
18671 (format, face, window, buffer)
18672 Lisp_Object format, face, window, buffer;
18673 {
18674 struct it it;
18675 int len;
18676 struct window *w;
18677 struct buffer *old_buffer = NULL;
18678 int face_id = -1;
18679 int no_props = INTEGERP (face);
18680 int count = SPECPDL_INDEX ();
18681 Lisp_Object str;
18682 int string_start = 0;
18683
18684 if (NILP (window))
18685 window = selected_window;
18686 CHECK_WINDOW (window);
18687 w = XWINDOW (window);
18688
18689 if (NILP (buffer))
18690 buffer = w->buffer;
18691 CHECK_BUFFER (buffer);
18692
18693 /* Make formatting the modeline a non-op when noninteractive, otherwise
18694 there will be problems later caused by a partially initialized frame. */
18695 if (NILP (format) || noninteractive)
18696 return empty_unibyte_string;
18697
18698 if (no_props)
18699 face = Qnil;
18700
18701 if (!NILP (face))
18702 {
18703 if (EQ (face, Qt))
18704 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18705 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18706 }
18707
18708 if (face_id < 0)
18709 face_id = DEFAULT_FACE_ID;
18710
18711 if (XBUFFER (buffer) != current_buffer)
18712 old_buffer = current_buffer;
18713
18714 /* Save things including mode_line_proptrans_alist,
18715 and set that to nil so that we don't alter the outer value. */
18716 record_unwind_protect (unwind_format_mode_line,
18717 format_mode_line_unwind_data
18718 (old_buffer, selected_window, 1));
18719 mode_line_proptrans_alist = Qnil;
18720
18721 Fselect_window (window, Qt);
18722 if (old_buffer)
18723 set_buffer_internal_1 (XBUFFER (buffer));
18724
18725 init_iterator (&it, w, -1, -1, NULL, face_id);
18726
18727 if (no_props)
18728 {
18729 mode_line_target = MODE_LINE_NOPROP;
18730 mode_line_string_face_prop = Qnil;
18731 mode_line_string_list = Qnil;
18732 string_start = MODE_LINE_NOPROP_LEN (0);
18733 }
18734 else
18735 {
18736 mode_line_target = MODE_LINE_STRING;
18737 mode_line_string_list = Qnil;
18738 mode_line_string_face = face;
18739 mode_line_string_face_prop
18740 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18741 }
18742
18743 push_kboard (FRAME_KBOARD (it.f));
18744 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18745 pop_kboard ();
18746
18747 if (no_props)
18748 {
18749 len = MODE_LINE_NOPROP_LEN (string_start);
18750 str = make_string (mode_line_noprop_buf + string_start, len);
18751 }
18752 else
18753 {
18754 mode_line_string_list = Fnreverse (mode_line_string_list);
18755 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18756 empty_unibyte_string);
18757 }
18758
18759 unbind_to (count, Qnil);
18760 return str;
18761 }
18762
18763 /* Write a null-terminated, right justified decimal representation of
18764 the positive integer D to BUF using a minimal field width WIDTH. */
18765
18766 static void
18767 pint2str (buf, width, d)
18768 register char *buf;
18769 register int width;
18770 register int d;
18771 {
18772 register char *p = buf;
18773
18774 if (d <= 0)
18775 *p++ = '0';
18776 else
18777 {
18778 while (d > 0)
18779 {
18780 *p++ = d % 10 + '0';
18781 d /= 10;
18782 }
18783 }
18784
18785 for (width -= (int) (p - buf); width > 0; --width)
18786 *p++ = ' ';
18787 *p-- = '\0';
18788 while (p > buf)
18789 {
18790 d = *buf;
18791 *buf++ = *p;
18792 *p-- = d;
18793 }
18794 }
18795
18796 /* Write a null-terminated, right justified decimal and "human
18797 readable" representation of the nonnegative integer D to BUF using
18798 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18799
18800 static const char power_letter[] =
18801 {
18802 0, /* not used */
18803 'k', /* kilo */
18804 'M', /* mega */
18805 'G', /* giga */
18806 'T', /* tera */
18807 'P', /* peta */
18808 'E', /* exa */
18809 'Z', /* zetta */
18810 'Y' /* yotta */
18811 };
18812
18813 static void
18814 pint2hrstr (buf, width, d)
18815 char *buf;
18816 int width;
18817 int d;
18818 {
18819 /* We aim to represent the nonnegative integer D as
18820 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18821 int quotient = d;
18822 int remainder = 0;
18823 /* -1 means: do not use TENTHS. */
18824 int tenths = -1;
18825 int exponent = 0;
18826
18827 /* Length of QUOTIENT.TENTHS as a string. */
18828 int length;
18829
18830 char * psuffix;
18831 char * p;
18832
18833 if (1000 <= quotient)
18834 {
18835 /* Scale to the appropriate EXPONENT. */
18836 do
18837 {
18838 remainder = quotient % 1000;
18839 quotient /= 1000;
18840 exponent++;
18841 }
18842 while (1000 <= quotient);
18843
18844 /* Round to nearest and decide whether to use TENTHS or not. */
18845 if (quotient <= 9)
18846 {
18847 tenths = remainder / 100;
18848 if (50 <= remainder % 100)
18849 {
18850 if (tenths < 9)
18851 tenths++;
18852 else
18853 {
18854 quotient++;
18855 if (quotient == 10)
18856 tenths = -1;
18857 else
18858 tenths = 0;
18859 }
18860 }
18861 }
18862 else
18863 if (500 <= remainder)
18864 {
18865 if (quotient < 999)
18866 quotient++;
18867 else
18868 {
18869 quotient = 1;
18870 exponent++;
18871 tenths = 0;
18872 }
18873 }
18874 }
18875
18876 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18877 if (tenths == -1 && quotient <= 99)
18878 if (quotient <= 9)
18879 length = 1;
18880 else
18881 length = 2;
18882 else
18883 length = 3;
18884 p = psuffix = buf + max (width, length);
18885
18886 /* Print EXPONENT. */
18887 if (exponent)
18888 *psuffix++ = power_letter[exponent];
18889 *psuffix = '\0';
18890
18891 /* Print TENTHS. */
18892 if (tenths >= 0)
18893 {
18894 *--p = '0' + tenths;
18895 *--p = '.';
18896 }
18897
18898 /* Print QUOTIENT. */
18899 do
18900 {
18901 int digit = quotient % 10;
18902 *--p = '0' + digit;
18903 }
18904 while ((quotient /= 10) != 0);
18905
18906 /* Print leading spaces. */
18907 while (buf < p)
18908 *--p = ' ';
18909 }
18910
18911 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18912 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18913 type of CODING_SYSTEM. Return updated pointer into BUF. */
18914
18915 static unsigned char invalid_eol_type[] = "(*invalid*)";
18916
18917 static char *
18918 decode_mode_spec_coding (coding_system, buf, eol_flag)
18919 Lisp_Object coding_system;
18920 register char *buf;
18921 int eol_flag;
18922 {
18923 Lisp_Object val;
18924 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18925 const unsigned char *eol_str;
18926 int eol_str_len;
18927 /* The EOL conversion we are using. */
18928 Lisp_Object eoltype;
18929
18930 val = CODING_SYSTEM_SPEC (coding_system);
18931 eoltype = Qnil;
18932
18933 if (!VECTORP (val)) /* Not yet decided. */
18934 {
18935 if (multibyte)
18936 *buf++ = '-';
18937 if (eol_flag)
18938 eoltype = eol_mnemonic_undecided;
18939 /* Don't mention EOL conversion if it isn't decided. */
18940 }
18941 else
18942 {
18943 Lisp_Object attrs;
18944 Lisp_Object eolvalue;
18945
18946 attrs = AREF (val, 0);
18947 eolvalue = AREF (val, 2);
18948
18949 if (multibyte)
18950 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18951
18952 if (eol_flag)
18953 {
18954 /* The EOL conversion that is normal on this system. */
18955
18956 if (NILP (eolvalue)) /* Not yet decided. */
18957 eoltype = eol_mnemonic_undecided;
18958 else if (VECTORP (eolvalue)) /* Not yet decided. */
18959 eoltype = eol_mnemonic_undecided;
18960 else /* eolvalue is Qunix, Qdos, or Qmac. */
18961 eoltype = (EQ (eolvalue, Qunix)
18962 ? eol_mnemonic_unix
18963 : (EQ (eolvalue, Qdos) == 1
18964 ? eol_mnemonic_dos : eol_mnemonic_mac));
18965 }
18966 }
18967
18968 if (eol_flag)
18969 {
18970 /* Mention the EOL conversion if it is not the usual one. */
18971 if (STRINGP (eoltype))
18972 {
18973 eol_str = SDATA (eoltype);
18974 eol_str_len = SBYTES (eoltype);
18975 }
18976 else if (CHARACTERP (eoltype))
18977 {
18978 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18979 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18980 eol_str = tmp;
18981 }
18982 else
18983 {
18984 eol_str = invalid_eol_type;
18985 eol_str_len = sizeof (invalid_eol_type) - 1;
18986 }
18987 bcopy (eol_str, buf, eol_str_len);
18988 buf += eol_str_len;
18989 }
18990
18991 return buf;
18992 }
18993
18994 /* Return a string for the output of a mode line %-spec for window W,
18995 generated by character C. PRECISION >= 0 means don't return a
18996 string longer than that value. FIELD_WIDTH > 0 means pad the
18997 string returned with spaces to that value. Return a Lisp string in
18998 *STRING if the resulting string is taken from that Lisp string.
18999
19000 Note we operate on the current buffer for most purposes,
19001 the exception being w->base_line_pos. */
19002
19003 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19004
19005 static char *
19006 decode_mode_spec (w, c, field_width, precision, string)
19007 struct window *w;
19008 register int c;
19009 int field_width, precision;
19010 Lisp_Object *string;
19011 {
19012 Lisp_Object obj;
19013 struct frame *f = XFRAME (WINDOW_FRAME (w));
19014 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19015 struct buffer *b = current_buffer;
19016
19017 obj = Qnil;
19018 *string = Qnil;
19019
19020 switch (c)
19021 {
19022 case '*':
19023 if (!NILP (b->read_only))
19024 return "%";
19025 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19026 return "*";
19027 return "-";
19028
19029 case '+':
19030 /* This differs from %* only for a modified read-only buffer. */
19031 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19032 return "*";
19033 if (!NILP (b->read_only))
19034 return "%";
19035 return "-";
19036
19037 case '&':
19038 /* This differs from %* in ignoring read-only-ness. */
19039 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19040 return "*";
19041 return "-";
19042
19043 case '%':
19044 return "%";
19045
19046 case '[':
19047 {
19048 int i;
19049 char *p;
19050
19051 if (command_loop_level > 5)
19052 return "[[[... ";
19053 p = decode_mode_spec_buf;
19054 for (i = 0; i < command_loop_level; i++)
19055 *p++ = '[';
19056 *p = 0;
19057 return decode_mode_spec_buf;
19058 }
19059
19060 case ']':
19061 {
19062 int i;
19063 char *p;
19064
19065 if (command_loop_level > 5)
19066 return " ...]]]";
19067 p = decode_mode_spec_buf;
19068 for (i = 0; i < command_loop_level; i++)
19069 *p++ = ']';
19070 *p = 0;
19071 return decode_mode_spec_buf;
19072 }
19073
19074 case '-':
19075 {
19076 register int i;
19077
19078 /* Let lots_of_dashes be a string of infinite length. */
19079 if (mode_line_target == MODE_LINE_NOPROP ||
19080 mode_line_target == MODE_LINE_STRING)
19081 return "--";
19082 if (field_width <= 0
19083 || field_width > sizeof (lots_of_dashes))
19084 {
19085 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19086 decode_mode_spec_buf[i] = '-';
19087 decode_mode_spec_buf[i] = '\0';
19088 return decode_mode_spec_buf;
19089 }
19090 else
19091 return lots_of_dashes;
19092 }
19093
19094 case 'b':
19095 obj = b->name;
19096 break;
19097
19098 case 'c':
19099 /* %c and %l are ignored in `frame-title-format'.
19100 (In redisplay_internal, the frame title is drawn _before_ the
19101 windows are updated, so the stuff which depends on actual
19102 window contents (such as %l) may fail to render properly, or
19103 even crash emacs.) */
19104 if (mode_line_target == MODE_LINE_TITLE)
19105 return "";
19106 else
19107 {
19108 int col = (int) current_column (); /* iftc */
19109 w->column_number_displayed = make_number (col);
19110 pint2str (decode_mode_spec_buf, field_width, col);
19111 return decode_mode_spec_buf;
19112 }
19113
19114 case 'e':
19115 #ifndef SYSTEM_MALLOC
19116 {
19117 if (NILP (Vmemory_full))
19118 return "";
19119 else
19120 return "!MEM FULL! ";
19121 }
19122 #else
19123 return "";
19124 #endif
19125
19126 case 'F':
19127 /* %F displays the frame name. */
19128 if (!NILP (f->title))
19129 return (char *) SDATA (f->title);
19130 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19131 return (char *) SDATA (f->name);
19132 return "Emacs";
19133
19134 case 'f':
19135 obj = b->filename;
19136 break;
19137
19138 case 'i':
19139 {
19140 int size = ZV - BEGV;
19141 pint2str (decode_mode_spec_buf, field_width, size);
19142 return decode_mode_spec_buf;
19143 }
19144
19145 case 'I':
19146 {
19147 int size = ZV - BEGV;
19148 pint2hrstr (decode_mode_spec_buf, field_width, size);
19149 return decode_mode_spec_buf;
19150 }
19151
19152 case 'l':
19153 {
19154 int startpos, startpos_byte, line, linepos, linepos_byte;
19155 int topline, nlines, junk, height;
19156
19157 /* %c and %l are ignored in `frame-title-format'. */
19158 if (mode_line_target == MODE_LINE_TITLE)
19159 return "";
19160
19161 startpos = XMARKER (w->start)->charpos;
19162 startpos_byte = marker_byte_position (w->start);
19163 height = WINDOW_TOTAL_LINES (w);
19164
19165 /* If we decided that this buffer isn't suitable for line numbers,
19166 don't forget that too fast. */
19167 if (EQ (w->base_line_pos, w->buffer))
19168 goto no_value;
19169 /* But do forget it, if the window shows a different buffer now. */
19170 else if (BUFFERP (w->base_line_pos))
19171 w->base_line_pos = Qnil;
19172
19173 /* If the buffer is very big, don't waste time. */
19174 if (INTEGERP (Vline_number_display_limit)
19175 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19176 {
19177 w->base_line_pos = Qnil;
19178 w->base_line_number = Qnil;
19179 goto no_value;
19180 }
19181
19182 if (INTEGERP (w->base_line_number)
19183 && INTEGERP (w->base_line_pos)
19184 && XFASTINT (w->base_line_pos) <= startpos)
19185 {
19186 line = XFASTINT (w->base_line_number);
19187 linepos = XFASTINT (w->base_line_pos);
19188 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19189 }
19190 else
19191 {
19192 line = 1;
19193 linepos = BUF_BEGV (b);
19194 linepos_byte = BUF_BEGV_BYTE (b);
19195 }
19196
19197 /* Count lines from base line to window start position. */
19198 nlines = display_count_lines (linepos, linepos_byte,
19199 startpos_byte,
19200 startpos, &junk);
19201
19202 topline = nlines + line;
19203
19204 /* Determine a new base line, if the old one is too close
19205 or too far away, or if we did not have one.
19206 "Too close" means it's plausible a scroll-down would
19207 go back past it. */
19208 if (startpos == BUF_BEGV (b))
19209 {
19210 w->base_line_number = make_number (topline);
19211 w->base_line_pos = make_number (BUF_BEGV (b));
19212 }
19213 else if (nlines < height + 25 || nlines > height * 3 + 50
19214 || linepos == BUF_BEGV (b))
19215 {
19216 int limit = BUF_BEGV (b);
19217 int limit_byte = BUF_BEGV_BYTE (b);
19218 int position;
19219 int distance = (height * 2 + 30) * line_number_display_limit_width;
19220
19221 if (startpos - distance > limit)
19222 {
19223 limit = startpos - distance;
19224 limit_byte = CHAR_TO_BYTE (limit);
19225 }
19226
19227 nlines = display_count_lines (startpos, startpos_byte,
19228 limit_byte,
19229 - (height * 2 + 30),
19230 &position);
19231 /* If we couldn't find the lines we wanted within
19232 line_number_display_limit_width chars per line,
19233 give up on line numbers for this window. */
19234 if (position == limit_byte && limit == startpos - distance)
19235 {
19236 w->base_line_pos = w->buffer;
19237 w->base_line_number = Qnil;
19238 goto no_value;
19239 }
19240
19241 w->base_line_number = make_number (topline - nlines);
19242 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19243 }
19244
19245 /* Now count lines from the start pos to point. */
19246 nlines = display_count_lines (startpos, startpos_byte,
19247 PT_BYTE, PT, &junk);
19248
19249 /* Record that we did display the line number. */
19250 line_number_displayed = 1;
19251
19252 /* Make the string to show. */
19253 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19254 return decode_mode_spec_buf;
19255 no_value:
19256 {
19257 char* p = decode_mode_spec_buf;
19258 int pad = field_width - 2;
19259 while (pad-- > 0)
19260 *p++ = ' ';
19261 *p++ = '?';
19262 *p++ = '?';
19263 *p = '\0';
19264 return decode_mode_spec_buf;
19265 }
19266 }
19267 break;
19268
19269 case 'm':
19270 obj = b->mode_name;
19271 break;
19272
19273 case 'n':
19274 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19275 return " Narrow";
19276 break;
19277
19278 case 'p':
19279 {
19280 int pos = marker_position (w->start);
19281 int total = BUF_ZV (b) - BUF_BEGV (b);
19282
19283 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19284 {
19285 if (pos <= BUF_BEGV (b))
19286 return "All";
19287 else
19288 return "Bottom";
19289 }
19290 else if (pos <= BUF_BEGV (b))
19291 return "Top";
19292 else
19293 {
19294 if (total > 1000000)
19295 /* Do it differently for a large value, to avoid overflow. */
19296 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19297 else
19298 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19299 /* We can't normally display a 3-digit number,
19300 so get us a 2-digit number that is close. */
19301 if (total == 100)
19302 total = 99;
19303 sprintf (decode_mode_spec_buf, "%2d%%", total);
19304 return decode_mode_spec_buf;
19305 }
19306 }
19307
19308 /* Display percentage of size above the bottom of the screen. */
19309 case 'P':
19310 {
19311 int toppos = marker_position (w->start);
19312 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19313 int total = BUF_ZV (b) - BUF_BEGV (b);
19314
19315 if (botpos >= BUF_ZV (b))
19316 {
19317 if (toppos <= BUF_BEGV (b))
19318 return "All";
19319 else
19320 return "Bottom";
19321 }
19322 else
19323 {
19324 if (total > 1000000)
19325 /* Do it differently for a large value, to avoid overflow. */
19326 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19327 else
19328 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19329 /* We can't normally display a 3-digit number,
19330 so get us a 2-digit number that is close. */
19331 if (total == 100)
19332 total = 99;
19333 if (toppos <= BUF_BEGV (b))
19334 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
19335 else
19336 sprintf (decode_mode_spec_buf, "%2d%%", total);
19337 return decode_mode_spec_buf;
19338 }
19339 }
19340
19341 case 's':
19342 /* status of process */
19343 obj = Fget_buffer_process (Fcurrent_buffer ());
19344 if (NILP (obj))
19345 return "no process";
19346 #ifdef subprocesses
19347 obj = Fsymbol_name (Fprocess_status (obj));
19348 #endif
19349 break;
19350
19351 case '@':
19352 {
19353 int count = inhibit_garbage_collection ();
19354 Lisp_Object val = call1 (intern ("file-remote-p"),
19355 current_buffer->directory);
19356 unbind_to (count, Qnil);
19357
19358 if (NILP (val))
19359 return "-";
19360 else
19361 return "@";
19362 }
19363
19364 case 't': /* indicate TEXT or BINARY */
19365 #ifdef MODE_LINE_BINARY_TEXT
19366 return MODE_LINE_BINARY_TEXT (b);
19367 #else
19368 return "T";
19369 #endif
19370
19371 case 'z':
19372 /* coding-system (not including end-of-line format) */
19373 case 'Z':
19374 /* coding-system (including end-of-line type) */
19375 {
19376 int eol_flag = (c == 'Z');
19377 char *p = decode_mode_spec_buf;
19378
19379 if (! FRAME_WINDOW_P (f))
19380 {
19381 /* No need to mention EOL here--the terminal never needs
19382 to do EOL conversion. */
19383 p = decode_mode_spec_coding (CODING_ID_NAME
19384 (FRAME_KEYBOARD_CODING (f)->id),
19385 p, 0);
19386 p = decode_mode_spec_coding (CODING_ID_NAME
19387 (FRAME_TERMINAL_CODING (f)->id),
19388 p, 0);
19389 }
19390 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19391 p, eol_flag);
19392
19393 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19394 #ifdef subprocesses
19395 obj = Fget_buffer_process (Fcurrent_buffer ());
19396 if (PROCESSP (obj))
19397 {
19398 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19399 p, eol_flag);
19400 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19401 p, eol_flag);
19402 }
19403 #endif /* subprocesses */
19404 #endif /* 0 */
19405 *p = 0;
19406 return decode_mode_spec_buf;
19407 }
19408 }
19409
19410 if (STRINGP (obj))
19411 {
19412 *string = obj;
19413 return (char *) SDATA (obj);
19414 }
19415 else
19416 return "";
19417 }
19418
19419
19420 /* Count up to COUNT lines starting from START / START_BYTE.
19421 But don't go beyond LIMIT_BYTE.
19422 Return the number of lines thus found (always nonnegative).
19423
19424 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19425
19426 static int
19427 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
19428 int start, start_byte, limit_byte, count;
19429 int *byte_pos_ptr;
19430 {
19431 register unsigned char *cursor;
19432 unsigned char *base;
19433
19434 register int ceiling;
19435 register unsigned char *ceiling_addr;
19436 int orig_count = count;
19437
19438 /* If we are not in selective display mode,
19439 check only for newlines. */
19440 int selective_display = (!NILP (current_buffer->selective_display)
19441 && !INTEGERP (current_buffer->selective_display));
19442
19443 if (count > 0)
19444 {
19445 while (start_byte < limit_byte)
19446 {
19447 ceiling = BUFFER_CEILING_OF (start_byte);
19448 ceiling = min (limit_byte - 1, ceiling);
19449 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19450 base = (cursor = BYTE_POS_ADDR (start_byte));
19451 while (1)
19452 {
19453 if (selective_display)
19454 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19455 ;
19456 else
19457 while (*cursor != '\n' && ++cursor != ceiling_addr)
19458 ;
19459
19460 if (cursor != ceiling_addr)
19461 {
19462 if (--count == 0)
19463 {
19464 start_byte += cursor - base + 1;
19465 *byte_pos_ptr = start_byte;
19466 return orig_count;
19467 }
19468 else
19469 if (++cursor == ceiling_addr)
19470 break;
19471 }
19472 else
19473 break;
19474 }
19475 start_byte += cursor - base;
19476 }
19477 }
19478 else
19479 {
19480 while (start_byte > limit_byte)
19481 {
19482 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19483 ceiling = max (limit_byte, ceiling);
19484 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19485 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19486 while (1)
19487 {
19488 if (selective_display)
19489 while (--cursor != ceiling_addr
19490 && *cursor != '\n' && *cursor != 015)
19491 ;
19492 else
19493 while (--cursor != ceiling_addr && *cursor != '\n')
19494 ;
19495
19496 if (cursor != ceiling_addr)
19497 {
19498 if (++count == 0)
19499 {
19500 start_byte += cursor - base + 1;
19501 *byte_pos_ptr = start_byte;
19502 /* When scanning backwards, we should
19503 not count the newline posterior to which we stop. */
19504 return - orig_count - 1;
19505 }
19506 }
19507 else
19508 break;
19509 }
19510 /* Here we add 1 to compensate for the last decrement
19511 of CURSOR, which took it past the valid range. */
19512 start_byte += cursor - base + 1;
19513 }
19514 }
19515
19516 *byte_pos_ptr = limit_byte;
19517
19518 if (count < 0)
19519 return - orig_count + count;
19520 return orig_count - count;
19521
19522 }
19523
19524
19525 \f
19526 /***********************************************************************
19527 Displaying strings
19528 ***********************************************************************/
19529
19530 /* Display a NUL-terminated string, starting with index START.
19531
19532 If STRING is non-null, display that C string. Otherwise, the Lisp
19533 string LISP_STRING is displayed. There's a case that STRING is
19534 non-null and LISP_STRING is not nil. It means STRING is a string
19535 data of LISP_STRING. In that case, we display LISP_STRING while
19536 ignoring its text properties.
19537
19538 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19539 FACE_STRING. Display STRING or LISP_STRING with the face at
19540 FACE_STRING_POS in FACE_STRING:
19541
19542 Display the string in the environment given by IT, but use the
19543 standard display table, temporarily.
19544
19545 FIELD_WIDTH is the minimum number of output glyphs to produce.
19546 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19547 with spaces. If STRING has more characters, more than FIELD_WIDTH
19548 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19549
19550 PRECISION is the maximum number of characters to output from
19551 STRING. PRECISION < 0 means don't truncate the string.
19552
19553 This is roughly equivalent to printf format specifiers:
19554
19555 FIELD_WIDTH PRECISION PRINTF
19556 ----------------------------------------
19557 -1 -1 %s
19558 -1 10 %.10s
19559 10 -1 %10s
19560 20 10 %20.10s
19561
19562 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19563 display them, and < 0 means obey the current buffer's value of
19564 enable_multibyte_characters.
19565
19566 Value is the number of columns displayed. */
19567
19568 static int
19569 display_string (string, lisp_string, face_string, face_string_pos,
19570 start, it, field_width, precision, max_x, multibyte)
19571 unsigned char *string;
19572 Lisp_Object lisp_string;
19573 Lisp_Object face_string;
19574 EMACS_INT face_string_pos;
19575 EMACS_INT start;
19576 struct it *it;
19577 int field_width, precision, max_x;
19578 int multibyte;
19579 {
19580 int hpos_at_start = it->hpos;
19581 int saved_face_id = it->face_id;
19582 struct glyph_row *row = it->glyph_row;
19583
19584 /* Initialize the iterator IT for iteration over STRING beginning
19585 with index START. */
19586 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19587 precision, field_width, multibyte);
19588 if (string && STRINGP (lisp_string))
19589 /* LISP_STRING is the one returned by decode_mode_spec. We should
19590 ignore its text properties. */
19591 it->stop_charpos = -1;
19592
19593 /* If displaying STRING, set up the face of the iterator
19594 from LISP_STRING, if that's given. */
19595 if (STRINGP (face_string))
19596 {
19597 EMACS_INT endptr;
19598 struct face *face;
19599
19600 it->face_id
19601 = face_at_string_position (it->w, face_string, face_string_pos,
19602 0, it->region_beg_charpos,
19603 it->region_end_charpos,
19604 &endptr, it->base_face_id, 0);
19605 face = FACE_FROM_ID (it->f, it->face_id);
19606 it->face_box_p = face->box != FACE_NO_BOX;
19607 }
19608
19609 /* Set max_x to the maximum allowed X position. Don't let it go
19610 beyond the right edge of the window. */
19611 if (max_x <= 0)
19612 max_x = it->last_visible_x;
19613 else
19614 max_x = min (max_x, it->last_visible_x);
19615
19616 /* Skip over display elements that are not visible. because IT->w is
19617 hscrolled. */
19618 if (it->current_x < it->first_visible_x)
19619 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19620 MOVE_TO_POS | MOVE_TO_X);
19621
19622 row->ascent = it->max_ascent;
19623 row->height = it->max_ascent + it->max_descent;
19624 row->phys_ascent = it->max_phys_ascent;
19625 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19626 row->extra_line_spacing = it->max_extra_line_spacing;
19627
19628 /* This condition is for the case that we are called with current_x
19629 past last_visible_x. */
19630 while (it->current_x < max_x)
19631 {
19632 int x_before, x, n_glyphs_before, i, nglyphs;
19633
19634 /* Get the next display element. */
19635 if (!get_next_display_element (it))
19636 break;
19637
19638 /* Produce glyphs. */
19639 x_before = it->current_x;
19640 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19641 PRODUCE_GLYPHS (it);
19642
19643 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19644 i = 0;
19645 x = x_before;
19646 while (i < nglyphs)
19647 {
19648 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19649
19650 if (it->line_wrap != TRUNCATE
19651 && x + glyph->pixel_width > max_x)
19652 {
19653 /* End of continued line or max_x reached. */
19654 if (CHAR_GLYPH_PADDING_P (*glyph))
19655 {
19656 /* A wide character is unbreakable. */
19657 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19658 it->current_x = x_before;
19659 }
19660 else
19661 {
19662 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19663 it->current_x = x;
19664 }
19665 break;
19666 }
19667 else if (x + glyph->pixel_width >= it->first_visible_x)
19668 {
19669 /* Glyph is at least partially visible. */
19670 ++it->hpos;
19671 if (x < it->first_visible_x)
19672 it->glyph_row->x = x - it->first_visible_x;
19673 }
19674 else
19675 {
19676 /* Glyph is off the left margin of the display area.
19677 Should not happen. */
19678 abort ();
19679 }
19680
19681 row->ascent = max (row->ascent, it->max_ascent);
19682 row->height = max (row->height, it->max_ascent + it->max_descent);
19683 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19684 row->phys_height = max (row->phys_height,
19685 it->max_phys_ascent + it->max_phys_descent);
19686 row->extra_line_spacing = max (row->extra_line_spacing,
19687 it->max_extra_line_spacing);
19688 x += glyph->pixel_width;
19689 ++i;
19690 }
19691
19692 /* Stop if max_x reached. */
19693 if (i < nglyphs)
19694 break;
19695
19696 /* Stop at line ends. */
19697 if (ITERATOR_AT_END_OF_LINE_P (it))
19698 {
19699 it->continuation_lines_width = 0;
19700 break;
19701 }
19702
19703 set_iterator_to_next (it, 1);
19704
19705 /* Stop if truncating at the right edge. */
19706 if (it->line_wrap == TRUNCATE
19707 && it->current_x >= it->last_visible_x)
19708 {
19709 /* Add truncation mark, but don't do it if the line is
19710 truncated at a padding space. */
19711 if (IT_CHARPOS (*it) < it->string_nchars)
19712 {
19713 if (!FRAME_WINDOW_P (it->f))
19714 {
19715 int i, n;
19716
19717 if (it->current_x > it->last_visible_x)
19718 {
19719 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19720 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19721 break;
19722 for (n = row->used[TEXT_AREA]; i < n; ++i)
19723 {
19724 row->used[TEXT_AREA] = i;
19725 produce_special_glyphs (it, IT_TRUNCATION);
19726 }
19727 }
19728 produce_special_glyphs (it, IT_TRUNCATION);
19729 }
19730 it->glyph_row->truncated_on_right_p = 1;
19731 }
19732 break;
19733 }
19734 }
19735
19736 /* Maybe insert a truncation at the left. */
19737 if (it->first_visible_x
19738 && IT_CHARPOS (*it) > 0)
19739 {
19740 if (!FRAME_WINDOW_P (it->f))
19741 insert_left_trunc_glyphs (it);
19742 it->glyph_row->truncated_on_left_p = 1;
19743 }
19744
19745 it->face_id = saved_face_id;
19746
19747 /* Value is number of columns displayed. */
19748 return it->hpos - hpos_at_start;
19749 }
19750
19751
19752 \f
19753 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19754 appears as an element of LIST or as the car of an element of LIST.
19755 If PROPVAL is a list, compare each element against LIST in that
19756 way, and return 1/2 if any element of PROPVAL is found in LIST.
19757 Otherwise return 0. This function cannot quit.
19758 The return value is 2 if the text is invisible but with an ellipsis
19759 and 1 if it's invisible and without an ellipsis. */
19760
19761 int
19762 invisible_p (propval, list)
19763 register Lisp_Object propval;
19764 Lisp_Object list;
19765 {
19766 register Lisp_Object tail, proptail;
19767
19768 for (tail = list; CONSP (tail); tail = XCDR (tail))
19769 {
19770 register Lisp_Object tem;
19771 tem = XCAR (tail);
19772 if (EQ (propval, tem))
19773 return 1;
19774 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19775 return NILP (XCDR (tem)) ? 1 : 2;
19776 }
19777
19778 if (CONSP (propval))
19779 {
19780 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19781 {
19782 Lisp_Object propelt;
19783 propelt = XCAR (proptail);
19784 for (tail = list; CONSP (tail); tail = XCDR (tail))
19785 {
19786 register Lisp_Object tem;
19787 tem = XCAR (tail);
19788 if (EQ (propelt, tem))
19789 return 1;
19790 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19791 return NILP (XCDR (tem)) ? 1 : 2;
19792 }
19793 }
19794 }
19795
19796 return 0;
19797 }
19798
19799 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19800 doc: /* Non-nil if the property makes the text invisible.
19801 POS-OR-PROP can be a marker or number, in which case it is taken to be
19802 a position in the current buffer and the value of the `invisible' property
19803 is checked; or it can be some other value, which is then presumed to be the
19804 value of the `invisible' property of the text of interest.
19805 The non-nil value returned can be t for truly invisible text or something
19806 else if the text is replaced by an ellipsis. */)
19807 (pos_or_prop)
19808 Lisp_Object pos_or_prop;
19809 {
19810 Lisp_Object prop
19811 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19812 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19813 : pos_or_prop);
19814 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19815 return (invis == 0 ? Qnil
19816 : invis == 1 ? Qt
19817 : make_number (invis));
19818 }
19819
19820 /* Calculate a width or height in pixels from a specification using
19821 the following elements:
19822
19823 SPEC ::=
19824 NUM - a (fractional) multiple of the default font width/height
19825 (NUM) - specifies exactly NUM pixels
19826 UNIT - a fixed number of pixels, see below.
19827 ELEMENT - size of a display element in pixels, see below.
19828 (NUM . SPEC) - equals NUM * SPEC
19829 (+ SPEC SPEC ...) - add pixel values
19830 (- SPEC SPEC ...) - subtract pixel values
19831 (- SPEC) - negate pixel value
19832
19833 NUM ::=
19834 INT or FLOAT - a number constant
19835 SYMBOL - use symbol's (buffer local) variable binding.
19836
19837 UNIT ::=
19838 in - pixels per inch *)
19839 mm - pixels per 1/1000 meter *)
19840 cm - pixels per 1/100 meter *)
19841 width - width of current font in pixels.
19842 height - height of current font in pixels.
19843
19844 *) using the ratio(s) defined in display-pixels-per-inch.
19845
19846 ELEMENT ::=
19847
19848 left-fringe - left fringe width in pixels
19849 right-fringe - right fringe width in pixels
19850
19851 left-margin - left margin width in pixels
19852 right-margin - right margin width in pixels
19853
19854 scroll-bar - scroll-bar area width in pixels
19855
19856 Examples:
19857
19858 Pixels corresponding to 5 inches:
19859 (5 . in)
19860
19861 Total width of non-text areas on left side of window (if scroll-bar is on left):
19862 '(space :width (+ left-fringe left-margin scroll-bar))
19863
19864 Align to first text column (in header line):
19865 '(space :align-to 0)
19866
19867 Align to middle of text area minus half the width of variable `my-image'
19868 containing a loaded image:
19869 '(space :align-to (0.5 . (- text my-image)))
19870
19871 Width of left margin minus width of 1 character in the default font:
19872 '(space :width (- left-margin 1))
19873
19874 Width of left margin minus width of 2 characters in the current font:
19875 '(space :width (- left-margin (2 . width)))
19876
19877 Center 1 character over left-margin (in header line):
19878 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19879
19880 Different ways to express width of left fringe plus left margin minus one pixel:
19881 '(space :width (- (+ left-fringe left-margin) (1)))
19882 '(space :width (+ left-fringe left-margin (- (1))))
19883 '(space :width (+ left-fringe left-margin (-1)))
19884
19885 */
19886
19887 #define NUMVAL(X) \
19888 ((INTEGERP (X) || FLOATP (X)) \
19889 ? XFLOATINT (X) \
19890 : - 1)
19891
19892 int
19893 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19894 double *res;
19895 struct it *it;
19896 Lisp_Object prop;
19897 struct font *font;
19898 int width_p, *align_to;
19899 {
19900 double pixels;
19901
19902 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19903 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19904
19905 if (NILP (prop))
19906 return OK_PIXELS (0);
19907
19908 xassert (FRAME_LIVE_P (it->f));
19909
19910 if (SYMBOLP (prop))
19911 {
19912 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19913 {
19914 char *unit = SDATA (SYMBOL_NAME (prop));
19915
19916 if (unit[0] == 'i' && unit[1] == 'n')
19917 pixels = 1.0;
19918 else if (unit[0] == 'm' && unit[1] == 'm')
19919 pixels = 25.4;
19920 else if (unit[0] == 'c' && unit[1] == 'm')
19921 pixels = 2.54;
19922 else
19923 pixels = 0;
19924 if (pixels > 0)
19925 {
19926 double ppi;
19927 #ifdef HAVE_WINDOW_SYSTEM
19928 if (FRAME_WINDOW_P (it->f)
19929 && (ppi = (width_p
19930 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19931 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19932 ppi > 0))
19933 return OK_PIXELS (ppi / pixels);
19934 #endif
19935
19936 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19937 || (CONSP (Vdisplay_pixels_per_inch)
19938 && (ppi = (width_p
19939 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19940 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19941 ppi > 0)))
19942 return OK_PIXELS (ppi / pixels);
19943
19944 return 0;
19945 }
19946 }
19947
19948 #ifdef HAVE_WINDOW_SYSTEM
19949 if (EQ (prop, Qheight))
19950 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19951 if (EQ (prop, Qwidth))
19952 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19953 #else
19954 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19955 return OK_PIXELS (1);
19956 #endif
19957
19958 if (EQ (prop, Qtext))
19959 return OK_PIXELS (width_p
19960 ? window_box_width (it->w, TEXT_AREA)
19961 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19962
19963 if (align_to && *align_to < 0)
19964 {
19965 *res = 0;
19966 if (EQ (prop, Qleft))
19967 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19968 if (EQ (prop, Qright))
19969 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19970 if (EQ (prop, Qcenter))
19971 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19972 + window_box_width (it->w, TEXT_AREA) / 2);
19973 if (EQ (prop, Qleft_fringe))
19974 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19975 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19976 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19977 if (EQ (prop, Qright_fringe))
19978 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19979 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19980 : window_box_right_offset (it->w, TEXT_AREA));
19981 if (EQ (prop, Qleft_margin))
19982 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19983 if (EQ (prop, Qright_margin))
19984 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19985 if (EQ (prop, Qscroll_bar))
19986 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19987 ? 0
19988 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19989 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19990 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19991 : 0)));
19992 }
19993 else
19994 {
19995 if (EQ (prop, Qleft_fringe))
19996 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19997 if (EQ (prop, Qright_fringe))
19998 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19999 if (EQ (prop, Qleft_margin))
20000 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20001 if (EQ (prop, Qright_margin))
20002 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20003 if (EQ (prop, Qscroll_bar))
20004 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20005 }
20006
20007 prop = Fbuffer_local_value (prop, it->w->buffer);
20008 }
20009
20010 if (INTEGERP (prop) || FLOATP (prop))
20011 {
20012 int base_unit = (width_p
20013 ? FRAME_COLUMN_WIDTH (it->f)
20014 : FRAME_LINE_HEIGHT (it->f));
20015 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20016 }
20017
20018 if (CONSP (prop))
20019 {
20020 Lisp_Object car = XCAR (prop);
20021 Lisp_Object cdr = XCDR (prop);
20022
20023 if (SYMBOLP (car))
20024 {
20025 #ifdef HAVE_WINDOW_SYSTEM
20026 if (FRAME_WINDOW_P (it->f)
20027 && valid_image_p (prop))
20028 {
20029 int id = lookup_image (it->f, prop);
20030 struct image *img = IMAGE_FROM_ID (it->f, id);
20031
20032 return OK_PIXELS (width_p ? img->width : img->height);
20033 }
20034 #endif
20035 if (EQ (car, Qplus) || EQ (car, Qminus))
20036 {
20037 int first = 1;
20038 double px;
20039
20040 pixels = 0;
20041 while (CONSP (cdr))
20042 {
20043 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20044 font, width_p, align_to))
20045 return 0;
20046 if (first)
20047 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20048 else
20049 pixels += px;
20050 cdr = XCDR (cdr);
20051 }
20052 if (EQ (car, Qminus))
20053 pixels = -pixels;
20054 return OK_PIXELS (pixels);
20055 }
20056
20057 car = Fbuffer_local_value (car, it->w->buffer);
20058 }
20059
20060 if (INTEGERP (car) || FLOATP (car))
20061 {
20062 double fact;
20063 pixels = XFLOATINT (car);
20064 if (NILP (cdr))
20065 return OK_PIXELS (pixels);
20066 if (calc_pixel_width_or_height (&fact, it, cdr,
20067 font, width_p, align_to))
20068 return OK_PIXELS (pixels * fact);
20069 return 0;
20070 }
20071
20072 return 0;
20073 }
20074
20075 return 0;
20076 }
20077
20078 \f
20079 /***********************************************************************
20080 Glyph Display
20081 ***********************************************************************/
20082
20083 #ifdef HAVE_WINDOW_SYSTEM
20084
20085 #if GLYPH_DEBUG
20086
20087 void
20088 dump_glyph_string (s)
20089 struct glyph_string *s;
20090 {
20091 fprintf (stderr, "glyph string\n");
20092 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20093 s->x, s->y, s->width, s->height);
20094 fprintf (stderr, " ybase = %d\n", s->ybase);
20095 fprintf (stderr, " hl = %d\n", s->hl);
20096 fprintf (stderr, " left overhang = %d, right = %d\n",
20097 s->left_overhang, s->right_overhang);
20098 fprintf (stderr, " nchars = %d\n", s->nchars);
20099 fprintf (stderr, " extends to end of line = %d\n",
20100 s->extends_to_end_of_line_p);
20101 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20102 fprintf (stderr, " bg width = %d\n", s->background_width);
20103 }
20104
20105 #endif /* GLYPH_DEBUG */
20106
20107 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20108 of XChar2b structures for S; it can't be allocated in
20109 init_glyph_string because it must be allocated via `alloca'. W
20110 is the window on which S is drawn. ROW and AREA are the glyph row
20111 and area within the row from which S is constructed. START is the
20112 index of the first glyph structure covered by S. HL is a
20113 face-override for drawing S. */
20114
20115 #ifdef HAVE_NTGUI
20116 #define OPTIONAL_HDC(hdc) hdc,
20117 #define DECLARE_HDC(hdc) HDC hdc;
20118 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20119 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20120 #endif
20121
20122 #ifndef OPTIONAL_HDC
20123 #define OPTIONAL_HDC(hdc)
20124 #define DECLARE_HDC(hdc)
20125 #define ALLOCATE_HDC(hdc, f)
20126 #define RELEASE_HDC(hdc, f)
20127 #endif
20128
20129 static void
20130 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
20131 struct glyph_string *s;
20132 DECLARE_HDC (hdc)
20133 XChar2b *char2b;
20134 struct window *w;
20135 struct glyph_row *row;
20136 enum glyph_row_area area;
20137 int start;
20138 enum draw_glyphs_face hl;
20139 {
20140 bzero (s, sizeof *s);
20141 s->w = w;
20142 s->f = XFRAME (w->frame);
20143 #ifdef HAVE_NTGUI
20144 s->hdc = hdc;
20145 #endif
20146 s->display = FRAME_X_DISPLAY (s->f);
20147 s->window = FRAME_X_WINDOW (s->f);
20148 s->char2b = char2b;
20149 s->hl = hl;
20150 s->row = row;
20151 s->area = area;
20152 s->first_glyph = row->glyphs[area] + start;
20153 s->height = row->height;
20154 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20155 s->ybase = s->y + row->ascent;
20156 }
20157
20158
20159 /* Append the list of glyph strings with head H and tail T to the list
20160 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20161
20162 static INLINE void
20163 append_glyph_string_lists (head, tail, h, t)
20164 struct glyph_string **head, **tail;
20165 struct glyph_string *h, *t;
20166 {
20167 if (h)
20168 {
20169 if (*head)
20170 (*tail)->next = h;
20171 else
20172 *head = h;
20173 h->prev = *tail;
20174 *tail = t;
20175 }
20176 }
20177
20178
20179 /* Prepend the list of glyph strings with head H and tail T to the
20180 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20181 result. */
20182
20183 static INLINE void
20184 prepend_glyph_string_lists (head, tail, h, t)
20185 struct glyph_string **head, **tail;
20186 struct glyph_string *h, *t;
20187 {
20188 if (h)
20189 {
20190 if (*head)
20191 (*head)->prev = t;
20192 else
20193 *tail = t;
20194 t->next = *head;
20195 *head = h;
20196 }
20197 }
20198
20199
20200 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20201 Set *HEAD and *TAIL to the resulting list. */
20202
20203 static INLINE void
20204 append_glyph_string (head, tail, s)
20205 struct glyph_string **head, **tail;
20206 struct glyph_string *s;
20207 {
20208 s->next = s->prev = NULL;
20209 append_glyph_string_lists (head, tail, s, s);
20210 }
20211
20212
20213 /* Get face and two-byte form of character C in face FACE_ID on frame
20214 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20215 means we want to display multibyte text. DISPLAY_P non-zero means
20216 make sure that X resources for the face returned are allocated.
20217 Value is a pointer to a realized face that is ready for display if
20218 DISPLAY_P is non-zero. */
20219
20220 static INLINE struct face *
20221 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
20222 struct frame *f;
20223 int c, face_id;
20224 XChar2b *char2b;
20225 int multibyte_p, display_p;
20226 {
20227 struct face *face = FACE_FROM_ID (f, face_id);
20228
20229 if (face->font)
20230 {
20231 unsigned code = face->font->driver->encode_char (face->font, c);
20232
20233 if (code != FONT_INVALID_CODE)
20234 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20235 else
20236 STORE_XCHAR2B (char2b, 0, 0);
20237 }
20238
20239 /* Make sure X resources of the face are allocated. */
20240 #ifdef HAVE_X_WINDOWS
20241 if (display_p)
20242 #endif
20243 {
20244 xassert (face != NULL);
20245 PREPARE_FACE_FOR_DISPLAY (f, face);
20246 }
20247
20248 return face;
20249 }
20250
20251
20252 /* Get face and two-byte form of character glyph GLYPH on frame F.
20253 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20254 a pointer to a realized face that is ready for display. */
20255
20256 static INLINE struct face *
20257 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
20258 struct frame *f;
20259 struct glyph *glyph;
20260 XChar2b *char2b;
20261 int *two_byte_p;
20262 {
20263 struct face *face;
20264
20265 xassert (glyph->type == CHAR_GLYPH);
20266 face = FACE_FROM_ID (f, glyph->face_id);
20267
20268 if (two_byte_p)
20269 *two_byte_p = 0;
20270
20271 if (face->font)
20272 {
20273 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
20274
20275 if (code != FONT_INVALID_CODE)
20276 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20277 else
20278 STORE_XCHAR2B (char2b, 0, 0);
20279 }
20280
20281 /* Make sure X resources of the face are allocated. */
20282 xassert (face != NULL);
20283 PREPARE_FACE_FOR_DISPLAY (f, face);
20284 return face;
20285 }
20286
20287
20288 /* Fill glyph string S with composition components specified by S->cmp.
20289
20290 BASE_FACE is the base face of the composition.
20291 S->cmp_from is the index of the first component for S.
20292
20293 OVERLAPS non-zero means S should draw the foreground only, and use
20294 its physical height for clipping. See also draw_glyphs.
20295
20296 Value is the index of a component not in S. */
20297
20298 static int
20299 fill_composite_glyph_string (s, base_face, overlaps)
20300 struct glyph_string *s;
20301 struct face *base_face;
20302 int overlaps;
20303 {
20304 int i;
20305 /* For all glyphs of this composition, starting at the offset
20306 S->cmp_from, until we reach the end of the definition or encounter a
20307 glyph that requires the different face, add it to S. */
20308 struct face *face;
20309
20310 xassert (s);
20311
20312 s->for_overlaps = overlaps;
20313 s->face = NULL;
20314 s->font = NULL;
20315 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20316 {
20317 int c = COMPOSITION_GLYPH (s->cmp, i);
20318
20319 if (c != '\t')
20320 {
20321 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20322 -1, Qnil);
20323
20324 face = get_char_face_and_encoding (s->f, c, face_id,
20325 s->char2b + i, 1, 1);
20326 if (face)
20327 {
20328 if (! s->face)
20329 {
20330 s->face = face;
20331 s->font = s->face->font;
20332 }
20333 else if (s->face != face)
20334 break;
20335 }
20336 }
20337 ++s->nchars;
20338 }
20339 s->cmp_to = i;
20340
20341 /* All glyph strings for the same composition has the same width,
20342 i.e. the width set for the first component of the composition. */
20343 s->width = s->first_glyph->pixel_width;
20344
20345 /* If the specified font could not be loaded, use the frame's
20346 default font, but record the fact that we couldn't load it in
20347 the glyph string so that we can draw rectangles for the
20348 characters of the glyph string. */
20349 if (s->font == NULL)
20350 {
20351 s->font_not_found_p = 1;
20352 s->font = FRAME_FONT (s->f);
20353 }
20354
20355 /* Adjust base line for subscript/superscript text. */
20356 s->ybase += s->first_glyph->voffset;
20357
20358 /* This glyph string must always be drawn with 16-bit functions. */
20359 s->two_byte_p = 1;
20360
20361 return s->cmp_to;
20362 }
20363
20364 static int
20365 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
20366 struct glyph_string *s;
20367 int face_id;
20368 int start, end, overlaps;
20369 {
20370 struct glyph *glyph, *last;
20371 Lisp_Object lgstring;
20372 int i;
20373
20374 s->for_overlaps = overlaps;
20375 glyph = s->row->glyphs[s->area] + start;
20376 last = s->row->glyphs[s->area] + end;
20377 s->cmp_id = glyph->u.cmp.id;
20378 s->cmp_from = glyph->u.cmp.from;
20379 s->cmp_to = glyph->u.cmp.to + 1;
20380 s->face = FACE_FROM_ID (s->f, face_id);
20381 lgstring = composition_gstring_from_id (s->cmp_id);
20382 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20383 glyph++;
20384 while (glyph < last
20385 && glyph->u.cmp.automatic
20386 && glyph->u.cmp.id == s->cmp_id
20387 && s->cmp_to == glyph->u.cmp.from)
20388 s->cmp_to = (glyph++)->u.cmp.to + 1;
20389
20390 for (i = s->cmp_from; i < s->cmp_to; i++)
20391 {
20392 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20393 unsigned code = LGLYPH_CODE (lglyph);
20394
20395 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20396 }
20397 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20398 return glyph - s->row->glyphs[s->area];
20399 }
20400
20401
20402 /* Fill glyph string S from a sequence of character glyphs.
20403
20404 FACE_ID is the face id of the string. START is the index of the
20405 first glyph to consider, END is the index of the last + 1.
20406 OVERLAPS non-zero means S should draw the foreground only, and use
20407 its physical height for clipping. See also draw_glyphs.
20408
20409 Value is the index of the first glyph not in S. */
20410
20411 static int
20412 fill_glyph_string (s, face_id, start, end, overlaps)
20413 struct glyph_string *s;
20414 int face_id;
20415 int start, end, overlaps;
20416 {
20417 struct glyph *glyph, *last;
20418 int voffset;
20419 int glyph_not_available_p;
20420
20421 xassert (s->f == XFRAME (s->w->frame));
20422 xassert (s->nchars == 0);
20423 xassert (start >= 0 && end > start);
20424
20425 s->for_overlaps = overlaps;
20426 glyph = s->row->glyphs[s->area] + start;
20427 last = s->row->glyphs[s->area] + end;
20428 voffset = glyph->voffset;
20429 s->padding_p = glyph->padding_p;
20430 glyph_not_available_p = glyph->glyph_not_available_p;
20431
20432 while (glyph < last
20433 && glyph->type == CHAR_GLYPH
20434 && glyph->voffset == voffset
20435 /* Same face id implies same font, nowadays. */
20436 && glyph->face_id == face_id
20437 && glyph->glyph_not_available_p == glyph_not_available_p)
20438 {
20439 int two_byte_p;
20440
20441 s->face = get_glyph_face_and_encoding (s->f, glyph,
20442 s->char2b + s->nchars,
20443 &two_byte_p);
20444 s->two_byte_p = two_byte_p;
20445 ++s->nchars;
20446 xassert (s->nchars <= end - start);
20447 s->width += glyph->pixel_width;
20448 if (glyph++->padding_p != s->padding_p)
20449 break;
20450 }
20451
20452 s->font = s->face->font;
20453
20454 /* If the specified font could not be loaded, use the frame's font,
20455 but record the fact that we couldn't load it in
20456 S->font_not_found_p so that we can draw rectangles for the
20457 characters of the glyph string. */
20458 if (s->font == NULL || glyph_not_available_p)
20459 {
20460 s->font_not_found_p = 1;
20461 s->font = FRAME_FONT (s->f);
20462 }
20463
20464 /* Adjust base line for subscript/superscript text. */
20465 s->ybase += voffset;
20466
20467 xassert (s->face && s->face->gc);
20468 return glyph - s->row->glyphs[s->area];
20469 }
20470
20471
20472 /* Fill glyph string S from image glyph S->first_glyph. */
20473
20474 static void
20475 fill_image_glyph_string (s)
20476 struct glyph_string *s;
20477 {
20478 xassert (s->first_glyph->type == IMAGE_GLYPH);
20479 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20480 xassert (s->img);
20481 s->slice = s->first_glyph->slice;
20482 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20483 s->font = s->face->font;
20484 s->width = s->first_glyph->pixel_width;
20485
20486 /* Adjust base line for subscript/superscript text. */
20487 s->ybase += s->first_glyph->voffset;
20488 }
20489
20490
20491 /* Fill glyph string S from a sequence of stretch glyphs.
20492
20493 ROW is the glyph row in which the glyphs are found, AREA is the
20494 area within the row. START is the index of the first glyph to
20495 consider, END is the index of the last + 1.
20496
20497 Value is the index of the first glyph not in S. */
20498
20499 static int
20500 fill_stretch_glyph_string (s, row, area, start, end)
20501 struct glyph_string *s;
20502 struct glyph_row *row;
20503 enum glyph_row_area area;
20504 int start, end;
20505 {
20506 struct glyph *glyph, *last;
20507 int voffset, face_id;
20508
20509 xassert (s->first_glyph->type == STRETCH_GLYPH);
20510
20511 glyph = s->row->glyphs[s->area] + start;
20512 last = s->row->glyphs[s->area] + end;
20513 face_id = glyph->face_id;
20514 s->face = FACE_FROM_ID (s->f, face_id);
20515 s->font = s->face->font;
20516 s->width = glyph->pixel_width;
20517 s->nchars = 1;
20518 voffset = glyph->voffset;
20519
20520 for (++glyph;
20521 (glyph < last
20522 && glyph->type == STRETCH_GLYPH
20523 && glyph->voffset == voffset
20524 && glyph->face_id == face_id);
20525 ++glyph)
20526 s->width += glyph->pixel_width;
20527
20528 /* Adjust base line for subscript/superscript text. */
20529 s->ybase += voffset;
20530
20531 /* The case that face->gc == 0 is handled when drawing the glyph
20532 string by calling PREPARE_FACE_FOR_DISPLAY. */
20533 xassert (s->face);
20534 return glyph - s->row->glyphs[s->area];
20535 }
20536
20537 static struct font_metrics *
20538 get_per_char_metric (f, font, char2b)
20539 struct frame *f;
20540 struct font *font;
20541 XChar2b *char2b;
20542 {
20543 static struct font_metrics metrics;
20544 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20545
20546 if (! font || code == FONT_INVALID_CODE)
20547 return NULL;
20548 font->driver->text_extents (font, &code, 1, &metrics);
20549 return &metrics;
20550 }
20551
20552 /* EXPORT for RIF:
20553 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20554 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20555 assumed to be zero. */
20556
20557 void
20558 x_get_glyph_overhangs (glyph, f, left, right)
20559 struct glyph *glyph;
20560 struct frame *f;
20561 int *left, *right;
20562 {
20563 *left = *right = 0;
20564
20565 if (glyph->type == CHAR_GLYPH)
20566 {
20567 struct face *face;
20568 XChar2b char2b;
20569 struct font_metrics *pcm;
20570
20571 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20572 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20573 {
20574 if (pcm->rbearing > pcm->width)
20575 *right = pcm->rbearing - pcm->width;
20576 if (pcm->lbearing < 0)
20577 *left = -pcm->lbearing;
20578 }
20579 }
20580 else if (glyph->type == COMPOSITE_GLYPH)
20581 {
20582 if (! glyph->u.cmp.automatic)
20583 {
20584 struct composition *cmp = composition_table[glyph->u.cmp.id];
20585
20586 if (cmp->rbearing > cmp->pixel_width)
20587 *right = cmp->rbearing - cmp->pixel_width;
20588 if (cmp->lbearing < 0)
20589 *left = - cmp->lbearing;
20590 }
20591 else
20592 {
20593 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20594 struct font_metrics metrics;
20595
20596 composition_gstring_width (gstring, glyph->u.cmp.from,
20597 glyph->u.cmp.to + 1, &metrics);
20598 if (metrics.rbearing > metrics.width)
20599 *right = metrics.rbearing - metrics.width;
20600 if (metrics.lbearing < 0)
20601 *left = - metrics.lbearing;
20602 }
20603 }
20604 }
20605
20606
20607 /* Return the index of the first glyph preceding glyph string S that
20608 is overwritten by S because of S's left overhang. Value is -1
20609 if no glyphs are overwritten. */
20610
20611 static int
20612 left_overwritten (s)
20613 struct glyph_string *s;
20614 {
20615 int k;
20616
20617 if (s->left_overhang)
20618 {
20619 int x = 0, i;
20620 struct glyph *glyphs = s->row->glyphs[s->area];
20621 int first = s->first_glyph - glyphs;
20622
20623 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20624 x -= glyphs[i].pixel_width;
20625
20626 k = i + 1;
20627 }
20628 else
20629 k = -1;
20630
20631 return k;
20632 }
20633
20634
20635 /* Return the index of the first glyph preceding glyph string S that
20636 is overwriting S because of its right overhang. Value is -1 if no
20637 glyph in front of S overwrites S. */
20638
20639 static int
20640 left_overwriting (s)
20641 struct glyph_string *s;
20642 {
20643 int i, k, x;
20644 struct glyph *glyphs = s->row->glyphs[s->area];
20645 int first = s->first_glyph - glyphs;
20646
20647 k = -1;
20648 x = 0;
20649 for (i = first - 1; i >= 0; --i)
20650 {
20651 int left, right;
20652 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20653 if (x + right > 0)
20654 k = i;
20655 x -= glyphs[i].pixel_width;
20656 }
20657
20658 return k;
20659 }
20660
20661
20662 /* Return the index of the last glyph following glyph string S that is
20663 overwritten by S because of S's right overhang. Value is -1 if
20664 no such glyph is found. */
20665
20666 static int
20667 right_overwritten (s)
20668 struct glyph_string *s;
20669 {
20670 int k = -1;
20671
20672 if (s->right_overhang)
20673 {
20674 int x = 0, i;
20675 struct glyph *glyphs = s->row->glyphs[s->area];
20676 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20677 int end = s->row->used[s->area];
20678
20679 for (i = first; i < end && s->right_overhang > x; ++i)
20680 x += glyphs[i].pixel_width;
20681
20682 k = i;
20683 }
20684
20685 return k;
20686 }
20687
20688
20689 /* Return the index of the last glyph following glyph string S that
20690 overwrites S because of its left overhang. Value is negative
20691 if no such glyph is found. */
20692
20693 static int
20694 right_overwriting (s)
20695 struct glyph_string *s;
20696 {
20697 int i, k, x;
20698 int end = s->row->used[s->area];
20699 struct glyph *glyphs = s->row->glyphs[s->area];
20700 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20701
20702 k = -1;
20703 x = 0;
20704 for (i = first; i < end; ++i)
20705 {
20706 int left, right;
20707 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20708 if (x - left < 0)
20709 k = i;
20710 x += glyphs[i].pixel_width;
20711 }
20712
20713 return k;
20714 }
20715
20716
20717 /* Set background width of glyph string S. START is the index of the
20718 first glyph following S. LAST_X is the right-most x-position + 1
20719 in the drawing area. */
20720
20721 static INLINE void
20722 set_glyph_string_background_width (s, start, last_x)
20723 struct glyph_string *s;
20724 int start;
20725 int last_x;
20726 {
20727 /* If the face of this glyph string has to be drawn to the end of
20728 the drawing area, set S->extends_to_end_of_line_p. */
20729
20730 if (start == s->row->used[s->area]
20731 && s->area == TEXT_AREA
20732 && ((s->row->fill_line_p
20733 && (s->hl == DRAW_NORMAL_TEXT
20734 || s->hl == DRAW_IMAGE_RAISED
20735 || s->hl == DRAW_IMAGE_SUNKEN))
20736 || s->hl == DRAW_MOUSE_FACE))
20737 s->extends_to_end_of_line_p = 1;
20738
20739 /* If S extends its face to the end of the line, set its
20740 background_width to the distance to the right edge of the drawing
20741 area. */
20742 if (s->extends_to_end_of_line_p)
20743 s->background_width = last_x - s->x + 1;
20744 else
20745 s->background_width = s->width;
20746 }
20747
20748
20749 /* Compute overhangs and x-positions for glyph string S and its
20750 predecessors, or successors. X is the starting x-position for S.
20751 BACKWARD_P non-zero means process predecessors. */
20752
20753 static void
20754 compute_overhangs_and_x (s, x, backward_p)
20755 struct glyph_string *s;
20756 int x;
20757 int backward_p;
20758 {
20759 if (backward_p)
20760 {
20761 while (s)
20762 {
20763 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20764 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20765 x -= s->width;
20766 s->x = x;
20767 s = s->prev;
20768 }
20769 }
20770 else
20771 {
20772 while (s)
20773 {
20774 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20775 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20776 s->x = x;
20777 x += s->width;
20778 s = s->next;
20779 }
20780 }
20781 }
20782
20783
20784
20785 /* The following macros are only called from draw_glyphs below.
20786 They reference the following parameters of that function directly:
20787 `w', `row', `area', and `overlap_p'
20788 as well as the following local variables:
20789 `s', `f', and `hdc' (in W32) */
20790
20791 #ifdef HAVE_NTGUI
20792 /* On W32, silently add local `hdc' variable to argument list of
20793 init_glyph_string. */
20794 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20795 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20796 #else
20797 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20798 init_glyph_string (s, char2b, w, row, area, start, hl)
20799 #endif
20800
20801 /* Add a glyph string for a stretch glyph to the list of strings
20802 between HEAD and TAIL. START is the index of the stretch glyph in
20803 row area AREA of glyph row ROW. END is the index of the last glyph
20804 in that glyph row area. X is the current output position assigned
20805 to the new glyph string constructed. HL overrides that face of the
20806 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20807 is the right-most x-position of the drawing area. */
20808
20809 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20810 and below -- keep them on one line. */
20811 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20812 do \
20813 { \
20814 s = (struct glyph_string *) alloca (sizeof *s); \
20815 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20816 START = fill_stretch_glyph_string (s, row, area, START, END); \
20817 append_glyph_string (&HEAD, &TAIL, s); \
20818 s->x = (X); \
20819 } \
20820 while (0)
20821
20822
20823 /* Add a glyph string for an image glyph to the list of strings
20824 between HEAD and TAIL. START is the index of the image glyph in
20825 row area AREA of glyph row ROW. END is the index of the last glyph
20826 in that glyph row area. X is the current output position assigned
20827 to the new glyph string constructed. HL overrides that face of the
20828 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20829 is the right-most x-position of the drawing area. */
20830
20831 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20832 do \
20833 { \
20834 s = (struct glyph_string *) alloca (sizeof *s); \
20835 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20836 fill_image_glyph_string (s); \
20837 append_glyph_string (&HEAD, &TAIL, s); \
20838 ++START; \
20839 s->x = (X); \
20840 } \
20841 while (0)
20842
20843
20844 /* Add a glyph string for a sequence of character glyphs to the list
20845 of strings between HEAD and TAIL. START is the index of the first
20846 glyph in row area AREA of glyph row ROW that is part of the new
20847 glyph string. END is the index of the last glyph in that glyph row
20848 area. X is the current output position assigned to the new glyph
20849 string constructed. HL overrides that face of the glyph; e.g. it
20850 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20851 right-most x-position of the drawing area. */
20852
20853 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20854 do \
20855 { \
20856 int face_id; \
20857 XChar2b *char2b; \
20858 \
20859 face_id = (row)->glyphs[area][START].face_id; \
20860 \
20861 s = (struct glyph_string *) alloca (sizeof *s); \
20862 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20863 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20864 append_glyph_string (&HEAD, &TAIL, s); \
20865 s->x = (X); \
20866 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20867 } \
20868 while (0)
20869
20870
20871 /* Add a glyph string for a composite sequence to the list of strings
20872 between HEAD and TAIL. START is the index of the first glyph in
20873 row area AREA of glyph row ROW that is part of the new glyph
20874 string. END is the index of the last glyph in that glyph row area.
20875 X is the current output position assigned to the new glyph string
20876 constructed. HL overrides that face of the glyph; e.g. it is
20877 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20878 x-position of the drawing area. */
20879
20880 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20881 do { \
20882 int face_id = (row)->glyphs[area][START].face_id; \
20883 struct face *base_face = FACE_FROM_ID (f, face_id); \
20884 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20885 struct composition *cmp = composition_table[cmp_id]; \
20886 XChar2b *char2b; \
20887 struct glyph_string *first_s; \
20888 int n; \
20889 \
20890 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20891 \
20892 /* Make glyph_strings for each glyph sequence that is drawable by \
20893 the same face, and append them to HEAD/TAIL. */ \
20894 for (n = 0; n < cmp->glyph_len;) \
20895 { \
20896 s = (struct glyph_string *) alloca (sizeof *s); \
20897 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20898 append_glyph_string (&(HEAD), &(TAIL), s); \
20899 s->cmp = cmp; \
20900 s->cmp_from = n; \
20901 s->x = (X); \
20902 if (n == 0) \
20903 first_s = s; \
20904 n = fill_composite_glyph_string (s, base_face, overlaps); \
20905 } \
20906 \
20907 ++START; \
20908 s = first_s; \
20909 } while (0)
20910
20911
20912 /* Add a glyph string for a glyph-string sequence to the list of strings
20913 between HEAD and TAIL. */
20914
20915 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20916 do { \
20917 int face_id; \
20918 XChar2b *char2b; \
20919 Lisp_Object gstring; \
20920 \
20921 face_id = (row)->glyphs[area][START].face_id; \
20922 gstring = (composition_gstring_from_id \
20923 ((row)->glyphs[area][START].u.cmp.id)); \
20924 s = (struct glyph_string *) alloca (sizeof *s); \
20925 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20926 * LGSTRING_GLYPH_LEN (gstring)); \
20927 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20928 append_glyph_string (&(HEAD), &(TAIL), s); \
20929 s->x = (X); \
20930 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20931 } while (0)
20932
20933
20934 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20935 of AREA of glyph row ROW on window W between indices START and END.
20936 HL overrides the face for drawing glyph strings, e.g. it is
20937 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20938 x-positions of the drawing area.
20939
20940 This is an ugly monster macro construct because we must use alloca
20941 to allocate glyph strings (because draw_glyphs can be called
20942 asynchronously). */
20943
20944 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20945 do \
20946 { \
20947 HEAD = TAIL = NULL; \
20948 while (START < END) \
20949 { \
20950 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20951 switch (first_glyph->type) \
20952 { \
20953 case CHAR_GLYPH: \
20954 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20955 HL, X, LAST_X); \
20956 break; \
20957 \
20958 case COMPOSITE_GLYPH: \
20959 if (first_glyph->u.cmp.automatic) \
20960 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20961 HL, X, LAST_X); \
20962 else \
20963 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20964 HL, X, LAST_X); \
20965 break; \
20966 \
20967 case STRETCH_GLYPH: \
20968 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20969 HL, X, LAST_X); \
20970 break; \
20971 \
20972 case IMAGE_GLYPH: \
20973 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20974 HL, X, LAST_X); \
20975 break; \
20976 \
20977 default: \
20978 abort (); \
20979 } \
20980 \
20981 if (s) \
20982 { \
20983 set_glyph_string_background_width (s, START, LAST_X); \
20984 (X) += s->width; \
20985 } \
20986 } \
20987 } while (0)
20988
20989
20990 /* Draw glyphs between START and END in AREA of ROW on window W,
20991 starting at x-position X. X is relative to AREA in W. HL is a
20992 face-override with the following meaning:
20993
20994 DRAW_NORMAL_TEXT draw normally
20995 DRAW_CURSOR draw in cursor face
20996 DRAW_MOUSE_FACE draw in mouse face.
20997 DRAW_INVERSE_VIDEO draw in mode line face
20998 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20999 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21000
21001 If OVERLAPS is non-zero, draw only the foreground of characters and
21002 clip to the physical height of ROW. Non-zero value also defines
21003 the overlapping part to be drawn:
21004
21005 OVERLAPS_PRED overlap with preceding rows
21006 OVERLAPS_SUCC overlap with succeeding rows
21007 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21008 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21009
21010 Value is the x-position reached, relative to AREA of W. */
21011
21012 static int
21013 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
21014 struct window *w;
21015 int x;
21016 struct glyph_row *row;
21017 enum glyph_row_area area;
21018 EMACS_INT start, end;
21019 enum draw_glyphs_face hl;
21020 int overlaps;
21021 {
21022 struct glyph_string *head, *tail;
21023 struct glyph_string *s;
21024 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21025 int i, j, x_reached, last_x, area_left = 0;
21026 struct frame *f = XFRAME (WINDOW_FRAME (w));
21027 DECLARE_HDC (hdc);
21028
21029 ALLOCATE_HDC (hdc, f);
21030
21031 /* Let's rather be paranoid than getting a SEGV. */
21032 end = min (end, row->used[area]);
21033 start = max (0, start);
21034 start = min (end, start);
21035
21036 /* Translate X to frame coordinates. Set last_x to the right
21037 end of the drawing area. */
21038 if (row->full_width_p)
21039 {
21040 /* X is relative to the left edge of W, without scroll bars
21041 or fringes. */
21042 area_left = WINDOW_LEFT_EDGE_X (w);
21043 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21044 }
21045 else
21046 {
21047 area_left = window_box_left (w, area);
21048 last_x = area_left + window_box_width (w, area);
21049 }
21050 x += area_left;
21051
21052 /* Build a doubly-linked list of glyph_string structures between
21053 head and tail from what we have to draw. Note that the macro
21054 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21055 the reason we use a separate variable `i'. */
21056 i = start;
21057 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21058 if (tail)
21059 x_reached = tail->x + tail->background_width;
21060 else
21061 x_reached = x;
21062
21063 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21064 the row, redraw some glyphs in front or following the glyph
21065 strings built above. */
21066 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21067 {
21068 struct glyph_string *h, *t;
21069 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21070 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21071 int dummy_x = 0;
21072
21073 /* If mouse highlighting is on, we may need to draw adjacent
21074 glyphs using mouse-face highlighting. */
21075 if (area == TEXT_AREA && row->mouse_face_p)
21076 {
21077 struct glyph_row *mouse_beg_row, *mouse_end_row;
21078
21079 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21080 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21081
21082 if (row >= mouse_beg_row && row <= mouse_end_row)
21083 {
21084 check_mouse_face = 1;
21085 mouse_beg_col = (row == mouse_beg_row)
21086 ? dpyinfo->mouse_face_beg_col : 0;
21087 mouse_end_col = (row == mouse_end_row)
21088 ? dpyinfo->mouse_face_end_col
21089 : row->used[TEXT_AREA];
21090 }
21091 }
21092
21093 /* Compute overhangs for all glyph strings. */
21094 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21095 for (s = head; s; s = s->next)
21096 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21097
21098 /* Prepend glyph strings for glyphs in front of the first glyph
21099 string that are overwritten because of the first glyph
21100 string's left overhang. The background of all strings
21101 prepended must be drawn because the first glyph string
21102 draws over it. */
21103 i = left_overwritten (head);
21104 if (i >= 0)
21105 {
21106 enum draw_glyphs_face overlap_hl;
21107
21108 /* If this row contains mouse highlighting, attempt to draw
21109 the overlapped glyphs with the correct highlight. This
21110 code fails if the overlap encompasses more than one glyph
21111 and mouse-highlight spans only some of these glyphs.
21112 However, making it work perfectly involves a lot more
21113 code, and I don't know if the pathological case occurs in
21114 practice, so we'll stick to this for now. --- cyd */
21115 if (check_mouse_face
21116 && mouse_beg_col < start && mouse_end_col > i)
21117 overlap_hl = DRAW_MOUSE_FACE;
21118 else
21119 overlap_hl = DRAW_NORMAL_TEXT;
21120
21121 j = i;
21122 BUILD_GLYPH_STRINGS (j, start, h, t,
21123 overlap_hl, dummy_x, last_x);
21124 start = i;
21125 compute_overhangs_and_x (t, head->x, 1);
21126 prepend_glyph_string_lists (&head, &tail, h, t);
21127 clip_head = head;
21128 }
21129
21130 /* Prepend glyph strings for glyphs in front of the first glyph
21131 string that overwrite that glyph string because of their
21132 right overhang. For these strings, only the foreground must
21133 be drawn, because it draws over the glyph string at `head'.
21134 The background must not be drawn because this would overwrite
21135 right overhangs of preceding glyphs for which no glyph
21136 strings exist. */
21137 i = left_overwriting (head);
21138 if (i >= 0)
21139 {
21140 enum draw_glyphs_face overlap_hl;
21141
21142 if (check_mouse_face
21143 && mouse_beg_col < start && mouse_end_col > i)
21144 overlap_hl = DRAW_MOUSE_FACE;
21145 else
21146 overlap_hl = DRAW_NORMAL_TEXT;
21147
21148 clip_head = head;
21149 BUILD_GLYPH_STRINGS (i, start, h, t,
21150 overlap_hl, dummy_x, last_x);
21151 for (s = h; s; s = s->next)
21152 s->background_filled_p = 1;
21153 compute_overhangs_and_x (t, head->x, 1);
21154 prepend_glyph_string_lists (&head, &tail, h, t);
21155 }
21156
21157 /* Append glyphs strings for glyphs following the last glyph
21158 string tail that are overwritten by tail. The background of
21159 these strings has to be drawn because tail's foreground draws
21160 over it. */
21161 i = right_overwritten (tail);
21162 if (i >= 0)
21163 {
21164 enum draw_glyphs_face overlap_hl;
21165
21166 if (check_mouse_face
21167 && mouse_beg_col < i && mouse_end_col > end)
21168 overlap_hl = DRAW_MOUSE_FACE;
21169 else
21170 overlap_hl = DRAW_NORMAL_TEXT;
21171
21172 BUILD_GLYPH_STRINGS (end, i, h, t,
21173 overlap_hl, x, last_x);
21174 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21175 we don't have `end = i;' here. */
21176 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21177 append_glyph_string_lists (&head, &tail, h, t);
21178 clip_tail = tail;
21179 }
21180
21181 /* Append glyph strings for glyphs following the last glyph
21182 string tail that overwrite tail. The foreground of such
21183 glyphs has to be drawn because it writes into the background
21184 of tail. The background must not be drawn because it could
21185 paint over the foreground of following glyphs. */
21186 i = right_overwriting (tail);
21187 if (i >= 0)
21188 {
21189 enum draw_glyphs_face overlap_hl;
21190 if (check_mouse_face
21191 && mouse_beg_col < i && mouse_end_col > end)
21192 overlap_hl = DRAW_MOUSE_FACE;
21193 else
21194 overlap_hl = DRAW_NORMAL_TEXT;
21195
21196 clip_tail = tail;
21197 i++; /* We must include the Ith glyph. */
21198 BUILD_GLYPH_STRINGS (end, i, h, t,
21199 overlap_hl, x, last_x);
21200 for (s = h; s; s = s->next)
21201 s->background_filled_p = 1;
21202 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21203 append_glyph_string_lists (&head, &tail, h, t);
21204 }
21205 if (clip_head || clip_tail)
21206 for (s = head; s; s = s->next)
21207 {
21208 s->clip_head = clip_head;
21209 s->clip_tail = clip_tail;
21210 }
21211 }
21212
21213 /* Draw all strings. */
21214 for (s = head; s; s = s->next)
21215 FRAME_RIF (f)->draw_glyph_string (s);
21216
21217 #ifndef HAVE_NS
21218 /* When focus a sole frame and move horizontally, this sets on_p to 0
21219 causing a failure to erase prev cursor position. */
21220 if (area == TEXT_AREA
21221 && !row->full_width_p
21222 /* When drawing overlapping rows, only the glyph strings'
21223 foreground is drawn, which doesn't erase a cursor
21224 completely. */
21225 && !overlaps)
21226 {
21227 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21228 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21229 : (tail ? tail->x + tail->background_width : x));
21230 x0 -= area_left;
21231 x1 -= area_left;
21232
21233 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21234 row->y, MATRIX_ROW_BOTTOM_Y (row));
21235 }
21236 #endif
21237
21238 /* Value is the x-position up to which drawn, relative to AREA of W.
21239 This doesn't include parts drawn because of overhangs. */
21240 if (row->full_width_p)
21241 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21242 else
21243 x_reached -= area_left;
21244
21245 RELEASE_HDC (hdc, f);
21246
21247 return x_reached;
21248 }
21249
21250 /* Expand row matrix if too narrow. Don't expand if area
21251 is not present. */
21252
21253 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21254 { \
21255 if (!fonts_changed_p \
21256 && (it->glyph_row->glyphs[area] \
21257 < it->glyph_row->glyphs[area + 1])) \
21258 { \
21259 it->w->ncols_scale_factor++; \
21260 fonts_changed_p = 1; \
21261 } \
21262 }
21263
21264 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21265 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21266
21267 static INLINE void
21268 append_glyph (it)
21269 struct it *it;
21270 {
21271 struct glyph *glyph;
21272 enum glyph_row_area area = it->area;
21273
21274 xassert (it->glyph_row);
21275 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21276
21277 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21278 if (glyph < it->glyph_row->glyphs[area + 1])
21279 {
21280 /* If the glyph row is reversed, we need to prepend the glyph
21281 rather than append it. */
21282 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21283 {
21284 struct glyph *g;
21285
21286 /* Make room for the additional glyph. */
21287 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21288 g[1] = *g;
21289 glyph = it->glyph_row->glyphs[area];
21290 }
21291 glyph->charpos = CHARPOS (it->position);
21292 glyph->object = it->object;
21293 if (it->pixel_width > 0)
21294 {
21295 glyph->pixel_width = it->pixel_width;
21296 glyph->padding_p = 0;
21297 }
21298 else
21299 {
21300 /* Assure at least 1-pixel width. Otherwise, cursor can't
21301 be displayed correctly. */
21302 glyph->pixel_width = 1;
21303 glyph->padding_p = 1;
21304 }
21305 glyph->ascent = it->ascent;
21306 glyph->descent = it->descent;
21307 glyph->voffset = it->voffset;
21308 glyph->type = CHAR_GLYPH;
21309 glyph->avoid_cursor_p = it->avoid_cursor_p;
21310 glyph->multibyte_p = it->multibyte_p;
21311 glyph->left_box_line_p = it->start_of_box_run_p;
21312 glyph->right_box_line_p = it->end_of_box_run_p;
21313 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21314 || it->phys_descent > it->descent);
21315 glyph->glyph_not_available_p = it->glyph_not_available_p;
21316 glyph->face_id = it->face_id;
21317 glyph->u.ch = it->char_to_display;
21318 glyph->slice = null_glyph_slice;
21319 glyph->font_type = FONT_TYPE_UNKNOWN;
21320 if (it->bidi_p)
21321 {
21322 glyph->resolved_level = it->bidi_it.resolved_level;
21323 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21324 abort ();
21325 glyph->bidi_type = it->bidi_it.type;
21326 }
21327 else
21328 {
21329 glyph->resolved_level = 0;
21330 glyph->bidi_type = UNKNOWN_BT;
21331 }
21332 ++it->glyph_row->used[area];
21333 }
21334 else
21335 IT_EXPAND_MATRIX_WIDTH (it, area);
21336 }
21337
21338 /* Store one glyph for the composition IT->cmp_it.id in
21339 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21340 non-null. */
21341
21342 static INLINE void
21343 append_composite_glyph (it)
21344 struct it *it;
21345 {
21346 struct glyph *glyph;
21347 enum glyph_row_area area = it->area;
21348
21349 xassert (it->glyph_row);
21350
21351 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21352 if (glyph < it->glyph_row->glyphs[area + 1])
21353 {
21354 glyph->charpos = CHARPOS (it->position);
21355 glyph->object = it->object;
21356 glyph->pixel_width = it->pixel_width;
21357 glyph->ascent = it->ascent;
21358 glyph->descent = it->descent;
21359 glyph->voffset = it->voffset;
21360 glyph->type = COMPOSITE_GLYPH;
21361 if (it->cmp_it.ch < 0)
21362 {
21363 glyph->u.cmp.automatic = 0;
21364 glyph->u.cmp.id = it->cmp_it.id;
21365 }
21366 else
21367 {
21368 glyph->u.cmp.automatic = 1;
21369 glyph->u.cmp.id = it->cmp_it.id;
21370 glyph->u.cmp.from = it->cmp_it.from;
21371 glyph->u.cmp.to = it->cmp_it.to - 1;
21372 }
21373 glyph->avoid_cursor_p = it->avoid_cursor_p;
21374 glyph->multibyte_p = it->multibyte_p;
21375 glyph->left_box_line_p = it->start_of_box_run_p;
21376 glyph->right_box_line_p = it->end_of_box_run_p;
21377 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21378 || it->phys_descent > it->descent);
21379 glyph->padding_p = 0;
21380 glyph->glyph_not_available_p = 0;
21381 glyph->face_id = it->face_id;
21382 glyph->slice = null_glyph_slice;
21383 glyph->font_type = FONT_TYPE_UNKNOWN;
21384 if (it->bidi_p)
21385 {
21386 glyph->resolved_level = it->bidi_it.resolved_level;
21387 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21388 abort ();
21389 glyph->bidi_type = it->bidi_it.type;
21390 }
21391 ++it->glyph_row->used[area];
21392 }
21393 else
21394 IT_EXPAND_MATRIX_WIDTH (it, area);
21395 }
21396
21397
21398 /* Change IT->ascent and IT->height according to the setting of
21399 IT->voffset. */
21400
21401 static INLINE void
21402 take_vertical_position_into_account (it)
21403 struct it *it;
21404 {
21405 if (it->voffset)
21406 {
21407 if (it->voffset < 0)
21408 /* Increase the ascent so that we can display the text higher
21409 in the line. */
21410 it->ascent -= it->voffset;
21411 else
21412 /* Increase the descent so that we can display the text lower
21413 in the line. */
21414 it->descent += it->voffset;
21415 }
21416 }
21417
21418
21419 /* Produce glyphs/get display metrics for the image IT is loaded with.
21420 See the description of struct display_iterator in dispextern.h for
21421 an overview of struct display_iterator. */
21422
21423 static void
21424 produce_image_glyph (it)
21425 struct it *it;
21426 {
21427 struct image *img;
21428 struct face *face;
21429 int glyph_ascent, crop;
21430 struct glyph_slice slice;
21431
21432 xassert (it->what == IT_IMAGE);
21433
21434 face = FACE_FROM_ID (it->f, it->face_id);
21435 xassert (face);
21436 /* Make sure X resources of the face is loaded. */
21437 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21438
21439 if (it->image_id < 0)
21440 {
21441 /* Fringe bitmap. */
21442 it->ascent = it->phys_ascent = 0;
21443 it->descent = it->phys_descent = 0;
21444 it->pixel_width = 0;
21445 it->nglyphs = 0;
21446 return;
21447 }
21448
21449 img = IMAGE_FROM_ID (it->f, it->image_id);
21450 xassert (img);
21451 /* Make sure X resources of the image is loaded. */
21452 prepare_image_for_display (it->f, img);
21453
21454 slice.x = slice.y = 0;
21455 slice.width = img->width;
21456 slice.height = img->height;
21457
21458 if (INTEGERP (it->slice.x))
21459 slice.x = XINT (it->slice.x);
21460 else if (FLOATP (it->slice.x))
21461 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21462
21463 if (INTEGERP (it->slice.y))
21464 slice.y = XINT (it->slice.y);
21465 else if (FLOATP (it->slice.y))
21466 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21467
21468 if (INTEGERP (it->slice.width))
21469 slice.width = XINT (it->slice.width);
21470 else if (FLOATP (it->slice.width))
21471 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21472
21473 if (INTEGERP (it->slice.height))
21474 slice.height = XINT (it->slice.height);
21475 else if (FLOATP (it->slice.height))
21476 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21477
21478 if (slice.x >= img->width)
21479 slice.x = img->width;
21480 if (slice.y >= img->height)
21481 slice.y = img->height;
21482 if (slice.x + slice.width >= img->width)
21483 slice.width = img->width - slice.x;
21484 if (slice.y + slice.height > img->height)
21485 slice.height = img->height - slice.y;
21486
21487 if (slice.width == 0 || slice.height == 0)
21488 return;
21489
21490 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21491
21492 it->descent = slice.height - glyph_ascent;
21493 if (slice.y == 0)
21494 it->descent += img->vmargin;
21495 if (slice.y + slice.height == img->height)
21496 it->descent += img->vmargin;
21497 it->phys_descent = it->descent;
21498
21499 it->pixel_width = slice.width;
21500 if (slice.x == 0)
21501 it->pixel_width += img->hmargin;
21502 if (slice.x + slice.width == img->width)
21503 it->pixel_width += img->hmargin;
21504
21505 /* It's quite possible for images to have an ascent greater than
21506 their height, so don't get confused in that case. */
21507 if (it->descent < 0)
21508 it->descent = 0;
21509
21510 it->nglyphs = 1;
21511
21512 if (face->box != FACE_NO_BOX)
21513 {
21514 if (face->box_line_width > 0)
21515 {
21516 if (slice.y == 0)
21517 it->ascent += face->box_line_width;
21518 if (slice.y + slice.height == img->height)
21519 it->descent += face->box_line_width;
21520 }
21521
21522 if (it->start_of_box_run_p && slice.x == 0)
21523 it->pixel_width += eabs (face->box_line_width);
21524 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21525 it->pixel_width += eabs (face->box_line_width);
21526 }
21527
21528 take_vertical_position_into_account (it);
21529
21530 /* Automatically crop wide image glyphs at right edge so we can
21531 draw the cursor on same display row. */
21532 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21533 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21534 {
21535 it->pixel_width -= crop;
21536 slice.width -= crop;
21537 }
21538
21539 if (it->glyph_row)
21540 {
21541 struct glyph *glyph;
21542 enum glyph_row_area area = it->area;
21543
21544 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21545 if (glyph < it->glyph_row->glyphs[area + 1])
21546 {
21547 glyph->charpos = CHARPOS (it->position);
21548 glyph->object = it->object;
21549 glyph->pixel_width = it->pixel_width;
21550 glyph->ascent = glyph_ascent;
21551 glyph->descent = it->descent;
21552 glyph->voffset = it->voffset;
21553 glyph->type = IMAGE_GLYPH;
21554 glyph->avoid_cursor_p = it->avoid_cursor_p;
21555 glyph->multibyte_p = it->multibyte_p;
21556 glyph->left_box_line_p = it->start_of_box_run_p;
21557 glyph->right_box_line_p = it->end_of_box_run_p;
21558 glyph->overlaps_vertically_p = 0;
21559 glyph->padding_p = 0;
21560 glyph->glyph_not_available_p = 0;
21561 glyph->face_id = it->face_id;
21562 glyph->u.img_id = img->id;
21563 glyph->slice = slice;
21564 glyph->font_type = FONT_TYPE_UNKNOWN;
21565 if (it->bidi_p)
21566 {
21567 glyph->resolved_level = it->bidi_it.resolved_level;
21568 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21569 abort ();
21570 glyph->bidi_type = it->bidi_it.type;
21571 }
21572 ++it->glyph_row->used[area];
21573 }
21574 else
21575 IT_EXPAND_MATRIX_WIDTH (it, area);
21576 }
21577 }
21578
21579
21580 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21581 of the glyph, WIDTH and HEIGHT are the width and height of the
21582 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21583
21584 static void
21585 append_stretch_glyph (it, object, width, height, ascent)
21586 struct it *it;
21587 Lisp_Object object;
21588 int width, height;
21589 int ascent;
21590 {
21591 struct glyph *glyph;
21592 enum glyph_row_area area = it->area;
21593
21594 xassert (ascent >= 0 && ascent <= height);
21595
21596 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21597 if (glyph < it->glyph_row->glyphs[area + 1])
21598 {
21599 glyph->charpos = CHARPOS (it->position);
21600 glyph->object = object;
21601 glyph->pixel_width = width;
21602 glyph->ascent = ascent;
21603 glyph->descent = height - ascent;
21604 glyph->voffset = it->voffset;
21605 glyph->type = STRETCH_GLYPH;
21606 glyph->avoid_cursor_p = it->avoid_cursor_p;
21607 glyph->multibyte_p = it->multibyte_p;
21608 glyph->left_box_line_p = it->start_of_box_run_p;
21609 glyph->right_box_line_p = it->end_of_box_run_p;
21610 glyph->overlaps_vertically_p = 0;
21611 glyph->padding_p = 0;
21612 glyph->glyph_not_available_p = 0;
21613 glyph->face_id = it->face_id;
21614 glyph->u.stretch.ascent = ascent;
21615 glyph->u.stretch.height = height;
21616 glyph->slice = null_glyph_slice;
21617 glyph->font_type = FONT_TYPE_UNKNOWN;
21618 if (it->bidi_p)
21619 {
21620 glyph->resolved_level = it->bidi_it.resolved_level;
21621 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21622 abort ();
21623 glyph->bidi_type = it->bidi_it.type;
21624 }
21625 ++it->glyph_row->used[area];
21626 }
21627 else
21628 IT_EXPAND_MATRIX_WIDTH (it, area);
21629 }
21630
21631
21632 /* Produce a stretch glyph for iterator IT. IT->object is the value
21633 of the glyph property displayed. The value must be a list
21634 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21635 being recognized:
21636
21637 1. `:width WIDTH' specifies that the space should be WIDTH *
21638 canonical char width wide. WIDTH may be an integer or floating
21639 point number.
21640
21641 2. `:relative-width FACTOR' specifies that the width of the stretch
21642 should be computed from the width of the first character having the
21643 `glyph' property, and should be FACTOR times that width.
21644
21645 3. `:align-to HPOS' specifies that the space should be wide enough
21646 to reach HPOS, a value in canonical character units.
21647
21648 Exactly one of the above pairs must be present.
21649
21650 4. `:height HEIGHT' specifies that the height of the stretch produced
21651 should be HEIGHT, measured in canonical character units.
21652
21653 5. `:relative-height FACTOR' specifies that the height of the
21654 stretch should be FACTOR times the height of the characters having
21655 the glyph property.
21656
21657 Either none or exactly one of 4 or 5 must be present.
21658
21659 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21660 of the stretch should be used for the ascent of the stretch.
21661 ASCENT must be in the range 0 <= ASCENT <= 100. */
21662
21663 static void
21664 produce_stretch_glyph (it)
21665 struct it *it;
21666 {
21667 /* (space :width WIDTH :height HEIGHT ...) */
21668 Lisp_Object prop, plist;
21669 int width = 0, height = 0, align_to = -1;
21670 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21671 int ascent = 0;
21672 double tem;
21673 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21674 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21675
21676 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21677
21678 /* List should start with `space'. */
21679 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21680 plist = XCDR (it->object);
21681
21682 /* Compute the width of the stretch. */
21683 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21684 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21685 {
21686 /* Absolute width `:width WIDTH' specified and valid. */
21687 zero_width_ok_p = 1;
21688 width = (int)tem;
21689 }
21690 else if (prop = Fplist_get (plist, QCrelative_width),
21691 NUMVAL (prop) > 0)
21692 {
21693 /* Relative width `:relative-width FACTOR' specified and valid.
21694 Compute the width of the characters having the `glyph'
21695 property. */
21696 struct it it2;
21697 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21698
21699 it2 = *it;
21700 if (it->multibyte_p)
21701 {
21702 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
21703 - IT_BYTEPOS (*it));
21704 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
21705 }
21706 else
21707 it2.c = *p, it2.len = 1;
21708
21709 it2.glyph_row = NULL;
21710 it2.what = IT_CHARACTER;
21711 x_produce_glyphs (&it2);
21712 width = NUMVAL (prop) * it2.pixel_width;
21713 }
21714 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21715 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21716 {
21717 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21718 align_to = (align_to < 0
21719 ? 0
21720 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21721 else if (align_to < 0)
21722 align_to = window_box_left_offset (it->w, TEXT_AREA);
21723 width = max (0, (int)tem + align_to - it->current_x);
21724 zero_width_ok_p = 1;
21725 }
21726 else
21727 /* Nothing specified -> width defaults to canonical char width. */
21728 width = FRAME_COLUMN_WIDTH (it->f);
21729
21730 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21731 width = 1;
21732
21733 /* Compute height. */
21734 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21735 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21736 {
21737 height = (int)tem;
21738 zero_height_ok_p = 1;
21739 }
21740 else if (prop = Fplist_get (plist, QCrelative_height),
21741 NUMVAL (prop) > 0)
21742 height = FONT_HEIGHT (font) * NUMVAL (prop);
21743 else
21744 height = FONT_HEIGHT (font);
21745
21746 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21747 height = 1;
21748
21749 /* Compute percentage of height used for ascent. If
21750 `:ascent ASCENT' is present and valid, use that. Otherwise,
21751 derive the ascent from the font in use. */
21752 if (prop = Fplist_get (plist, QCascent),
21753 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21754 ascent = height * NUMVAL (prop) / 100.0;
21755 else if (!NILP (prop)
21756 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21757 ascent = min (max (0, (int)tem), height);
21758 else
21759 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21760
21761 if (width > 0 && it->line_wrap != TRUNCATE
21762 && it->current_x + width > it->last_visible_x)
21763 width = it->last_visible_x - it->current_x - 1;
21764
21765 if (width > 0 && height > 0 && it->glyph_row)
21766 {
21767 Lisp_Object object = it->stack[it->sp - 1].string;
21768 if (!STRINGP (object))
21769 object = it->w->buffer;
21770 append_stretch_glyph (it, object, width, height, ascent);
21771 }
21772
21773 it->pixel_width = width;
21774 it->ascent = it->phys_ascent = ascent;
21775 it->descent = it->phys_descent = height - it->ascent;
21776 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21777
21778 take_vertical_position_into_account (it);
21779 }
21780
21781 /* Calculate line-height and line-spacing properties.
21782 An integer value specifies explicit pixel value.
21783 A float value specifies relative value to current face height.
21784 A cons (float . face-name) specifies relative value to
21785 height of specified face font.
21786
21787 Returns height in pixels, or nil. */
21788
21789
21790 static Lisp_Object
21791 calc_line_height_property (it, val, font, boff, override)
21792 struct it *it;
21793 Lisp_Object val;
21794 struct font *font;
21795 int boff, override;
21796 {
21797 Lisp_Object face_name = Qnil;
21798 int ascent, descent, height;
21799
21800 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21801 return val;
21802
21803 if (CONSP (val))
21804 {
21805 face_name = XCAR (val);
21806 val = XCDR (val);
21807 if (!NUMBERP (val))
21808 val = make_number (1);
21809 if (NILP (face_name))
21810 {
21811 height = it->ascent + it->descent;
21812 goto scale;
21813 }
21814 }
21815
21816 if (NILP (face_name))
21817 {
21818 font = FRAME_FONT (it->f);
21819 boff = FRAME_BASELINE_OFFSET (it->f);
21820 }
21821 else if (EQ (face_name, Qt))
21822 {
21823 override = 0;
21824 }
21825 else
21826 {
21827 int face_id;
21828 struct face *face;
21829
21830 face_id = lookup_named_face (it->f, face_name, 0);
21831 if (face_id < 0)
21832 return make_number (-1);
21833
21834 face = FACE_FROM_ID (it->f, face_id);
21835 font = face->font;
21836 if (font == NULL)
21837 return make_number (-1);
21838 boff = font->baseline_offset;
21839 if (font->vertical_centering)
21840 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21841 }
21842
21843 ascent = FONT_BASE (font) + boff;
21844 descent = FONT_DESCENT (font) - boff;
21845
21846 if (override)
21847 {
21848 it->override_ascent = ascent;
21849 it->override_descent = descent;
21850 it->override_boff = boff;
21851 }
21852
21853 height = ascent + descent;
21854
21855 scale:
21856 if (FLOATP (val))
21857 height = (int)(XFLOAT_DATA (val) * height);
21858 else if (INTEGERP (val))
21859 height *= XINT (val);
21860
21861 return make_number (height);
21862 }
21863
21864
21865 /* RIF:
21866 Produce glyphs/get display metrics for the display element IT is
21867 loaded with. See the description of struct it in dispextern.h
21868 for an overview of struct it. */
21869
21870 void
21871 x_produce_glyphs (it)
21872 struct it *it;
21873 {
21874 int extra_line_spacing = it->extra_line_spacing;
21875
21876 it->glyph_not_available_p = 0;
21877
21878 if (it->what == IT_CHARACTER)
21879 {
21880 XChar2b char2b;
21881 struct font *font;
21882 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21883 struct font_metrics *pcm;
21884 int font_not_found_p;
21885 int boff; /* baseline offset */
21886 /* We may change it->multibyte_p upon unibyte<->multibyte
21887 conversion. So, save the current value now and restore it
21888 later.
21889
21890 Note: It seems that we don't have to record multibyte_p in
21891 struct glyph because the character code itself tells whether
21892 or not the character is multibyte. Thus, in the future, we
21893 must consider eliminating the field `multibyte_p' in the
21894 struct glyph. */
21895 int saved_multibyte_p = it->multibyte_p;
21896
21897 /* Maybe translate single-byte characters to multibyte, or the
21898 other way. */
21899 it->char_to_display = it->c;
21900 if (!ASCII_BYTE_P (it->c)
21901 && ! it->multibyte_p)
21902 {
21903 if (SINGLE_BYTE_CHAR_P (it->c)
21904 && unibyte_display_via_language_environment)
21905 {
21906 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21907
21908 /* get_next_display_element assures that this decoding
21909 never fails. */
21910 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21911 it->multibyte_p = 1;
21912 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21913 -1, Qnil);
21914 face = FACE_FROM_ID (it->f, it->face_id);
21915 }
21916 }
21917
21918 /* Get font to use. Encode IT->char_to_display. */
21919 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21920 &char2b, it->multibyte_p, 0);
21921 font = face->font;
21922
21923 font_not_found_p = font == NULL;
21924 if (font_not_found_p)
21925 {
21926 /* When no suitable font found, display an empty box based
21927 on the metrics of the font of the default face (or what
21928 remapped). */
21929 struct face *no_font_face
21930 = FACE_FROM_ID (it->f,
21931 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21932 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21933 font = no_font_face->font;
21934 boff = font->baseline_offset;
21935 }
21936 else
21937 {
21938 boff = font->baseline_offset;
21939 if (font->vertical_centering)
21940 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21941 }
21942
21943 if (it->char_to_display >= ' '
21944 && (!it->multibyte_p || it->char_to_display < 128))
21945 {
21946 /* Either unibyte or ASCII. */
21947 int stretched_p;
21948
21949 it->nglyphs = 1;
21950
21951 pcm = get_per_char_metric (it->f, font, &char2b);
21952
21953 if (it->override_ascent >= 0)
21954 {
21955 it->ascent = it->override_ascent;
21956 it->descent = it->override_descent;
21957 boff = it->override_boff;
21958 }
21959 else
21960 {
21961 it->ascent = FONT_BASE (font) + boff;
21962 it->descent = FONT_DESCENT (font) - boff;
21963 }
21964
21965 if (pcm)
21966 {
21967 it->phys_ascent = pcm->ascent + boff;
21968 it->phys_descent = pcm->descent - boff;
21969 it->pixel_width = pcm->width;
21970 }
21971 else
21972 {
21973 it->glyph_not_available_p = 1;
21974 it->phys_ascent = it->ascent;
21975 it->phys_descent = it->descent;
21976 it->pixel_width = FONT_WIDTH (font);
21977 }
21978
21979 if (it->constrain_row_ascent_descent_p)
21980 {
21981 if (it->descent > it->max_descent)
21982 {
21983 it->ascent += it->descent - it->max_descent;
21984 it->descent = it->max_descent;
21985 }
21986 if (it->ascent > it->max_ascent)
21987 {
21988 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21989 it->ascent = it->max_ascent;
21990 }
21991 it->phys_ascent = min (it->phys_ascent, it->ascent);
21992 it->phys_descent = min (it->phys_descent, it->descent);
21993 extra_line_spacing = 0;
21994 }
21995
21996 /* If this is a space inside a region of text with
21997 `space-width' property, change its width. */
21998 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21999 if (stretched_p)
22000 it->pixel_width *= XFLOATINT (it->space_width);
22001
22002 /* If face has a box, add the box thickness to the character
22003 height. If character has a box line to the left and/or
22004 right, add the box line width to the character's width. */
22005 if (face->box != FACE_NO_BOX)
22006 {
22007 int thick = face->box_line_width;
22008
22009 if (thick > 0)
22010 {
22011 it->ascent += thick;
22012 it->descent += thick;
22013 }
22014 else
22015 thick = -thick;
22016
22017 if (it->start_of_box_run_p)
22018 it->pixel_width += thick;
22019 if (it->end_of_box_run_p)
22020 it->pixel_width += thick;
22021 }
22022
22023 /* If face has an overline, add the height of the overline
22024 (1 pixel) and a 1 pixel margin to the character height. */
22025 if (face->overline_p)
22026 it->ascent += overline_margin;
22027
22028 if (it->constrain_row_ascent_descent_p)
22029 {
22030 if (it->ascent > it->max_ascent)
22031 it->ascent = it->max_ascent;
22032 if (it->descent > it->max_descent)
22033 it->descent = it->max_descent;
22034 }
22035
22036 take_vertical_position_into_account (it);
22037
22038 /* If we have to actually produce glyphs, do it. */
22039 if (it->glyph_row)
22040 {
22041 if (stretched_p)
22042 {
22043 /* Translate a space with a `space-width' property
22044 into a stretch glyph. */
22045 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22046 / FONT_HEIGHT (font));
22047 append_stretch_glyph (it, it->object, it->pixel_width,
22048 it->ascent + it->descent, ascent);
22049 }
22050 else
22051 append_glyph (it);
22052
22053 /* If characters with lbearing or rbearing are displayed
22054 in this line, record that fact in a flag of the
22055 glyph row. This is used to optimize X output code. */
22056 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22057 it->glyph_row->contains_overlapping_glyphs_p = 1;
22058 }
22059 if (! stretched_p && it->pixel_width == 0)
22060 /* We assure that all visible glyphs have at least 1-pixel
22061 width. */
22062 it->pixel_width = 1;
22063 }
22064 else if (it->char_to_display == '\n')
22065 {
22066 /* A newline has no width, but we need the height of the
22067 line. But if previous part of the line sets a height,
22068 don't increase that height */
22069
22070 Lisp_Object height;
22071 Lisp_Object total_height = Qnil;
22072
22073 it->override_ascent = -1;
22074 it->pixel_width = 0;
22075 it->nglyphs = 0;
22076
22077 height = get_it_property(it, Qline_height);
22078 /* Split (line-height total-height) list */
22079 if (CONSP (height)
22080 && CONSP (XCDR (height))
22081 && NILP (XCDR (XCDR (height))))
22082 {
22083 total_height = XCAR (XCDR (height));
22084 height = XCAR (height);
22085 }
22086 height = calc_line_height_property(it, height, font, boff, 1);
22087
22088 if (it->override_ascent >= 0)
22089 {
22090 it->ascent = it->override_ascent;
22091 it->descent = it->override_descent;
22092 boff = it->override_boff;
22093 }
22094 else
22095 {
22096 it->ascent = FONT_BASE (font) + boff;
22097 it->descent = FONT_DESCENT (font) - boff;
22098 }
22099
22100 if (EQ (height, Qt))
22101 {
22102 if (it->descent > it->max_descent)
22103 {
22104 it->ascent += it->descent - it->max_descent;
22105 it->descent = it->max_descent;
22106 }
22107 if (it->ascent > it->max_ascent)
22108 {
22109 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22110 it->ascent = it->max_ascent;
22111 }
22112 it->phys_ascent = min (it->phys_ascent, it->ascent);
22113 it->phys_descent = min (it->phys_descent, it->descent);
22114 it->constrain_row_ascent_descent_p = 1;
22115 extra_line_spacing = 0;
22116 }
22117 else
22118 {
22119 Lisp_Object spacing;
22120
22121 it->phys_ascent = it->ascent;
22122 it->phys_descent = it->descent;
22123
22124 if ((it->max_ascent > 0 || it->max_descent > 0)
22125 && face->box != FACE_NO_BOX
22126 && face->box_line_width > 0)
22127 {
22128 it->ascent += face->box_line_width;
22129 it->descent += face->box_line_width;
22130 }
22131 if (!NILP (height)
22132 && XINT (height) > it->ascent + it->descent)
22133 it->ascent = XINT (height) - it->descent;
22134
22135 if (!NILP (total_height))
22136 spacing = calc_line_height_property(it, total_height, font, boff, 0);
22137 else
22138 {
22139 spacing = get_it_property(it, Qline_spacing);
22140 spacing = calc_line_height_property(it, spacing, font, boff, 0);
22141 }
22142 if (INTEGERP (spacing))
22143 {
22144 extra_line_spacing = XINT (spacing);
22145 if (!NILP (total_height))
22146 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22147 }
22148 }
22149 }
22150 else if (it->char_to_display == '\t')
22151 {
22152 if (font->space_width > 0)
22153 {
22154 int tab_width = it->tab_width * font->space_width;
22155 int x = it->current_x + it->continuation_lines_width;
22156 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22157
22158 /* If the distance from the current position to the next tab
22159 stop is less than a space character width, use the
22160 tab stop after that. */
22161 if (next_tab_x - x < font->space_width)
22162 next_tab_x += tab_width;
22163
22164 it->pixel_width = next_tab_x - x;
22165 it->nglyphs = 1;
22166 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22167 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22168
22169 if (it->glyph_row)
22170 {
22171 append_stretch_glyph (it, it->object, it->pixel_width,
22172 it->ascent + it->descent, it->ascent);
22173 }
22174 }
22175 else
22176 {
22177 it->pixel_width = 0;
22178 it->nglyphs = 1;
22179 }
22180 }
22181 else
22182 {
22183 /* A multi-byte character. Assume that the display width of the
22184 character is the width of the character multiplied by the
22185 width of the font. */
22186
22187 /* If we found a font, this font should give us the right
22188 metrics. If we didn't find a font, use the frame's
22189 default font and calculate the width of the character by
22190 multiplying the width of font by the width of the
22191 character. */
22192
22193 pcm = get_per_char_metric (it->f, font, &char2b);
22194
22195 if (font_not_found_p || !pcm)
22196 {
22197 int char_width = CHAR_WIDTH (it->char_to_display);
22198
22199 if (char_width == 0)
22200 /* This is a non spacing character. But, as we are
22201 going to display an empty box, the box must occupy
22202 at least one column. */
22203 char_width = 1;
22204 it->glyph_not_available_p = 1;
22205 it->pixel_width = font->space_width * char_width;
22206 it->phys_ascent = FONT_BASE (font) + boff;
22207 it->phys_descent = FONT_DESCENT (font) - boff;
22208 }
22209 else
22210 {
22211 it->pixel_width = pcm->width;
22212 it->phys_ascent = pcm->ascent + boff;
22213 it->phys_descent = pcm->descent - boff;
22214 if (it->glyph_row
22215 && (pcm->lbearing < 0
22216 || pcm->rbearing > pcm->width))
22217 it->glyph_row->contains_overlapping_glyphs_p = 1;
22218 }
22219 it->nglyphs = 1;
22220 it->ascent = FONT_BASE (font) + boff;
22221 it->descent = FONT_DESCENT (font) - boff;
22222 if (face->box != FACE_NO_BOX)
22223 {
22224 int thick = face->box_line_width;
22225
22226 if (thick > 0)
22227 {
22228 it->ascent += thick;
22229 it->descent += thick;
22230 }
22231 else
22232 thick = - thick;
22233
22234 if (it->start_of_box_run_p)
22235 it->pixel_width += thick;
22236 if (it->end_of_box_run_p)
22237 it->pixel_width += thick;
22238 }
22239
22240 /* If face has an overline, add the height of the overline
22241 (1 pixel) and a 1 pixel margin to the character height. */
22242 if (face->overline_p)
22243 it->ascent += overline_margin;
22244
22245 take_vertical_position_into_account (it);
22246
22247 if (it->ascent < 0)
22248 it->ascent = 0;
22249 if (it->descent < 0)
22250 it->descent = 0;
22251
22252 if (it->glyph_row)
22253 append_glyph (it);
22254 if (it->pixel_width == 0)
22255 /* We assure that all visible glyphs have at least 1-pixel
22256 width. */
22257 it->pixel_width = 1;
22258 }
22259 it->multibyte_p = saved_multibyte_p;
22260 }
22261 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22262 {
22263 /* A static composition.
22264
22265 Note: A composition is represented as one glyph in the
22266 glyph matrix. There are no padding glyphs.
22267
22268 Important note: pixel_width, ascent, and descent are the
22269 values of what is drawn by draw_glyphs (i.e. the values of
22270 the overall glyphs composed). */
22271 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22272 int boff; /* baseline offset */
22273 struct composition *cmp = composition_table[it->cmp_it.id];
22274 int glyph_len = cmp->glyph_len;
22275 struct font *font = face->font;
22276
22277 it->nglyphs = 1;
22278
22279 /* If we have not yet calculated pixel size data of glyphs of
22280 the composition for the current face font, calculate them
22281 now. Theoretically, we have to check all fonts for the
22282 glyphs, but that requires much time and memory space. So,
22283 here we check only the font of the first glyph. This may
22284 lead to incorrect display, but it's very rare, and C-l
22285 (recenter-top-bottom) can correct the display anyway. */
22286 if (! cmp->font || cmp->font != font)
22287 {
22288 /* Ascent and descent of the font of the first character
22289 of this composition (adjusted by baseline offset).
22290 Ascent and descent of overall glyphs should not be less
22291 than these, respectively. */
22292 int font_ascent, font_descent, font_height;
22293 /* Bounding box of the overall glyphs. */
22294 int leftmost, rightmost, lowest, highest;
22295 int lbearing, rbearing;
22296 int i, width, ascent, descent;
22297 int left_padded = 0, right_padded = 0;
22298 int c;
22299 XChar2b char2b;
22300 struct font_metrics *pcm;
22301 int font_not_found_p;
22302 int pos;
22303
22304 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22305 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22306 break;
22307 if (glyph_len < cmp->glyph_len)
22308 right_padded = 1;
22309 for (i = 0; i < glyph_len; i++)
22310 {
22311 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22312 break;
22313 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22314 }
22315 if (i > 0)
22316 left_padded = 1;
22317
22318 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22319 : IT_CHARPOS (*it));
22320 /* If no suitable font is found, use the default font. */
22321 font_not_found_p = font == NULL;
22322 if (font_not_found_p)
22323 {
22324 face = face->ascii_face;
22325 font = face->font;
22326 }
22327 boff = font->baseline_offset;
22328 if (font->vertical_centering)
22329 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22330 font_ascent = FONT_BASE (font) + boff;
22331 font_descent = FONT_DESCENT (font) - boff;
22332 font_height = FONT_HEIGHT (font);
22333
22334 cmp->font = (void *) font;
22335
22336 pcm = NULL;
22337 if (! font_not_found_p)
22338 {
22339 get_char_face_and_encoding (it->f, c, it->face_id,
22340 &char2b, it->multibyte_p, 0);
22341 pcm = get_per_char_metric (it->f, font, &char2b);
22342 }
22343
22344 /* Initialize the bounding box. */
22345 if (pcm)
22346 {
22347 width = pcm->width;
22348 ascent = pcm->ascent;
22349 descent = pcm->descent;
22350 lbearing = pcm->lbearing;
22351 rbearing = pcm->rbearing;
22352 }
22353 else
22354 {
22355 width = FONT_WIDTH (font);
22356 ascent = FONT_BASE (font);
22357 descent = FONT_DESCENT (font);
22358 lbearing = 0;
22359 rbearing = width;
22360 }
22361
22362 rightmost = width;
22363 leftmost = 0;
22364 lowest = - descent + boff;
22365 highest = ascent + boff;
22366
22367 if (! font_not_found_p
22368 && font->default_ascent
22369 && CHAR_TABLE_P (Vuse_default_ascent)
22370 && !NILP (Faref (Vuse_default_ascent,
22371 make_number (it->char_to_display))))
22372 highest = font->default_ascent + boff;
22373
22374 /* Draw the first glyph at the normal position. It may be
22375 shifted to right later if some other glyphs are drawn
22376 at the left. */
22377 cmp->offsets[i * 2] = 0;
22378 cmp->offsets[i * 2 + 1] = boff;
22379 cmp->lbearing = lbearing;
22380 cmp->rbearing = rbearing;
22381
22382 /* Set cmp->offsets for the remaining glyphs. */
22383 for (i++; i < glyph_len; i++)
22384 {
22385 int left, right, btm, top;
22386 int ch = COMPOSITION_GLYPH (cmp, i);
22387 int face_id;
22388 struct face *this_face;
22389 int this_boff;
22390
22391 if (ch == '\t')
22392 ch = ' ';
22393 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22394 this_face = FACE_FROM_ID (it->f, face_id);
22395 font = this_face->font;
22396
22397 if (font == NULL)
22398 pcm = NULL;
22399 else
22400 {
22401 this_boff = font->baseline_offset;
22402 if (font->vertical_centering)
22403 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22404 get_char_face_and_encoding (it->f, ch, face_id,
22405 &char2b, it->multibyte_p, 0);
22406 pcm = get_per_char_metric (it->f, font, &char2b);
22407 }
22408 if (! pcm)
22409 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22410 else
22411 {
22412 width = pcm->width;
22413 ascent = pcm->ascent;
22414 descent = pcm->descent;
22415 lbearing = pcm->lbearing;
22416 rbearing = pcm->rbearing;
22417 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22418 {
22419 /* Relative composition with or without
22420 alternate chars. */
22421 left = (leftmost + rightmost - width) / 2;
22422 btm = - descent + boff;
22423 if (font->relative_compose
22424 && (! CHAR_TABLE_P (Vignore_relative_composition)
22425 || NILP (Faref (Vignore_relative_composition,
22426 make_number (ch)))))
22427 {
22428
22429 if (- descent >= font->relative_compose)
22430 /* One extra pixel between two glyphs. */
22431 btm = highest + 1;
22432 else if (ascent <= 0)
22433 /* One extra pixel between two glyphs. */
22434 btm = lowest - 1 - ascent - descent;
22435 }
22436 }
22437 else
22438 {
22439 /* A composition rule is specified by an integer
22440 value that encodes global and new reference
22441 points (GREF and NREF). GREF and NREF are
22442 specified by numbers as below:
22443
22444 0---1---2 -- ascent
22445 | |
22446 | |
22447 | |
22448 9--10--11 -- center
22449 | |
22450 ---3---4---5--- baseline
22451 | |
22452 6---7---8 -- descent
22453 */
22454 int rule = COMPOSITION_RULE (cmp, i);
22455 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22456
22457 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22458 grefx = gref % 3, nrefx = nref % 3;
22459 grefy = gref / 3, nrefy = nref / 3;
22460 if (xoff)
22461 xoff = font_height * (xoff - 128) / 256;
22462 if (yoff)
22463 yoff = font_height * (yoff - 128) / 256;
22464
22465 left = (leftmost
22466 + grefx * (rightmost - leftmost) / 2
22467 - nrefx * width / 2
22468 + xoff);
22469
22470 btm = ((grefy == 0 ? highest
22471 : grefy == 1 ? 0
22472 : grefy == 2 ? lowest
22473 : (highest + lowest) / 2)
22474 - (nrefy == 0 ? ascent + descent
22475 : nrefy == 1 ? descent - boff
22476 : nrefy == 2 ? 0
22477 : (ascent + descent) / 2)
22478 + yoff);
22479 }
22480
22481 cmp->offsets[i * 2] = left;
22482 cmp->offsets[i * 2 + 1] = btm + descent;
22483
22484 /* Update the bounding box of the overall glyphs. */
22485 if (width > 0)
22486 {
22487 right = left + width;
22488 if (left < leftmost)
22489 leftmost = left;
22490 if (right > rightmost)
22491 rightmost = right;
22492 }
22493 top = btm + descent + ascent;
22494 if (top > highest)
22495 highest = top;
22496 if (btm < lowest)
22497 lowest = btm;
22498
22499 if (cmp->lbearing > left + lbearing)
22500 cmp->lbearing = left + lbearing;
22501 if (cmp->rbearing < left + rbearing)
22502 cmp->rbearing = left + rbearing;
22503 }
22504 }
22505
22506 /* If there are glyphs whose x-offsets are negative,
22507 shift all glyphs to the right and make all x-offsets
22508 non-negative. */
22509 if (leftmost < 0)
22510 {
22511 for (i = 0; i < cmp->glyph_len; i++)
22512 cmp->offsets[i * 2] -= leftmost;
22513 rightmost -= leftmost;
22514 cmp->lbearing -= leftmost;
22515 cmp->rbearing -= leftmost;
22516 }
22517
22518 if (left_padded && cmp->lbearing < 0)
22519 {
22520 for (i = 0; i < cmp->glyph_len; i++)
22521 cmp->offsets[i * 2] -= cmp->lbearing;
22522 rightmost -= cmp->lbearing;
22523 cmp->rbearing -= cmp->lbearing;
22524 cmp->lbearing = 0;
22525 }
22526 if (right_padded && rightmost < cmp->rbearing)
22527 {
22528 rightmost = cmp->rbearing;
22529 }
22530
22531 cmp->pixel_width = rightmost;
22532 cmp->ascent = highest;
22533 cmp->descent = - lowest;
22534 if (cmp->ascent < font_ascent)
22535 cmp->ascent = font_ascent;
22536 if (cmp->descent < font_descent)
22537 cmp->descent = font_descent;
22538 }
22539
22540 if (it->glyph_row
22541 && (cmp->lbearing < 0
22542 || cmp->rbearing > cmp->pixel_width))
22543 it->glyph_row->contains_overlapping_glyphs_p = 1;
22544
22545 it->pixel_width = cmp->pixel_width;
22546 it->ascent = it->phys_ascent = cmp->ascent;
22547 it->descent = it->phys_descent = cmp->descent;
22548 if (face->box != FACE_NO_BOX)
22549 {
22550 int thick = face->box_line_width;
22551
22552 if (thick > 0)
22553 {
22554 it->ascent += thick;
22555 it->descent += thick;
22556 }
22557 else
22558 thick = - thick;
22559
22560 if (it->start_of_box_run_p)
22561 it->pixel_width += thick;
22562 if (it->end_of_box_run_p)
22563 it->pixel_width += thick;
22564 }
22565
22566 /* If face has an overline, add the height of the overline
22567 (1 pixel) and a 1 pixel margin to the character height. */
22568 if (face->overline_p)
22569 it->ascent += overline_margin;
22570
22571 take_vertical_position_into_account (it);
22572 if (it->ascent < 0)
22573 it->ascent = 0;
22574 if (it->descent < 0)
22575 it->descent = 0;
22576
22577 if (it->glyph_row)
22578 append_composite_glyph (it);
22579 }
22580 else if (it->what == IT_COMPOSITION)
22581 {
22582 /* A dynamic (automatic) composition. */
22583 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22584 Lisp_Object gstring;
22585 struct font_metrics metrics;
22586
22587 gstring = composition_gstring_from_id (it->cmp_it.id);
22588 it->pixel_width
22589 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22590 &metrics);
22591 if (it->glyph_row
22592 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22593 it->glyph_row->contains_overlapping_glyphs_p = 1;
22594 it->ascent = it->phys_ascent = metrics.ascent;
22595 it->descent = it->phys_descent = metrics.descent;
22596 if (face->box != FACE_NO_BOX)
22597 {
22598 int thick = face->box_line_width;
22599
22600 if (thick > 0)
22601 {
22602 it->ascent += thick;
22603 it->descent += thick;
22604 }
22605 else
22606 thick = - thick;
22607
22608 if (it->start_of_box_run_p)
22609 it->pixel_width += thick;
22610 if (it->end_of_box_run_p)
22611 it->pixel_width += thick;
22612 }
22613 /* If face has an overline, add the height of the overline
22614 (1 pixel) and a 1 pixel margin to the character height. */
22615 if (face->overline_p)
22616 it->ascent += overline_margin;
22617 take_vertical_position_into_account (it);
22618 if (it->ascent < 0)
22619 it->ascent = 0;
22620 if (it->descent < 0)
22621 it->descent = 0;
22622
22623 if (it->glyph_row)
22624 append_composite_glyph (it);
22625 }
22626 else if (it->what == IT_IMAGE)
22627 produce_image_glyph (it);
22628 else if (it->what == IT_STRETCH)
22629 produce_stretch_glyph (it);
22630
22631 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22632 because this isn't true for images with `:ascent 100'. */
22633 xassert (it->ascent >= 0 && it->descent >= 0);
22634 if (it->area == TEXT_AREA)
22635 it->current_x += it->pixel_width;
22636
22637 if (extra_line_spacing > 0)
22638 {
22639 it->descent += extra_line_spacing;
22640 if (extra_line_spacing > it->max_extra_line_spacing)
22641 it->max_extra_line_spacing = extra_line_spacing;
22642 }
22643
22644 it->max_ascent = max (it->max_ascent, it->ascent);
22645 it->max_descent = max (it->max_descent, it->descent);
22646 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22647 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22648 }
22649
22650 /* EXPORT for RIF:
22651 Output LEN glyphs starting at START at the nominal cursor position.
22652 Advance the nominal cursor over the text. The global variable
22653 updated_window contains the window being updated, updated_row is
22654 the glyph row being updated, and updated_area is the area of that
22655 row being updated. */
22656
22657 void
22658 x_write_glyphs (start, len)
22659 struct glyph *start;
22660 int len;
22661 {
22662 int x, hpos;
22663
22664 xassert (updated_window && updated_row);
22665 BLOCK_INPUT;
22666
22667 /* Write glyphs. */
22668
22669 hpos = start - updated_row->glyphs[updated_area];
22670 x = draw_glyphs (updated_window, output_cursor.x,
22671 updated_row, updated_area,
22672 hpos, hpos + len,
22673 DRAW_NORMAL_TEXT, 0);
22674
22675 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22676 if (updated_area == TEXT_AREA
22677 && updated_window->phys_cursor_on_p
22678 && updated_window->phys_cursor.vpos == output_cursor.vpos
22679 && updated_window->phys_cursor.hpos >= hpos
22680 && updated_window->phys_cursor.hpos < hpos + len)
22681 updated_window->phys_cursor_on_p = 0;
22682
22683 UNBLOCK_INPUT;
22684
22685 /* Advance the output cursor. */
22686 output_cursor.hpos += len;
22687 output_cursor.x = x;
22688 }
22689
22690
22691 /* EXPORT for RIF:
22692 Insert LEN glyphs from START at the nominal cursor position. */
22693
22694 void
22695 x_insert_glyphs (start, len)
22696 struct glyph *start;
22697 int len;
22698 {
22699 struct frame *f;
22700 struct window *w;
22701 int line_height, shift_by_width, shifted_region_width;
22702 struct glyph_row *row;
22703 struct glyph *glyph;
22704 int frame_x, frame_y;
22705 EMACS_INT hpos;
22706
22707 xassert (updated_window && updated_row);
22708 BLOCK_INPUT;
22709 w = updated_window;
22710 f = XFRAME (WINDOW_FRAME (w));
22711
22712 /* Get the height of the line we are in. */
22713 row = updated_row;
22714 line_height = row->height;
22715
22716 /* Get the width of the glyphs to insert. */
22717 shift_by_width = 0;
22718 for (glyph = start; glyph < start + len; ++glyph)
22719 shift_by_width += glyph->pixel_width;
22720
22721 /* Get the width of the region to shift right. */
22722 shifted_region_width = (window_box_width (w, updated_area)
22723 - output_cursor.x
22724 - shift_by_width);
22725
22726 /* Shift right. */
22727 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22728 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22729
22730 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22731 line_height, shift_by_width);
22732
22733 /* Write the glyphs. */
22734 hpos = start - row->glyphs[updated_area];
22735 draw_glyphs (w, output_cursor.x, row, updated_area,
22736 hpos, hpos + len,
22737 DRAW_NORMAL_TEXT, 0);
22738
22739 /* Advance the output cursor. */
22740 output_cursor.hpos += len;
22741 output_cursor.x += shift_by_width;
22742 UNBLOCK_INPUT;
22743 }
22744
22745
22746 /* EXPORT for RIF:
22747 Erase the current text line from the nominal cursor position
22748 (inclusive) to pixel column TO_X (exclusive). The idea is that
22749 everything from TO_X onward is already erased.
22750
22751 TO_X is a pixel position relative to updated_area of
22752 updated_window. TO_X == -1 means clear to the end of this area. */
22753
22754 void
22755 x_clear_end_of_line (to_x)
22756 int to_x;
22757 {
22758 struct frame *f;
22759 struct window *w = updated_window;
22760 int max_x, min_y, max_y;
22761 int from_x, from_y, to_y;
22762
22763 xassert (updated_window && updated_row);
22764 f = XFRAME (w->frame);
22765
22766 if (updated_row->full_width_p)
22767 max_x = WINDOW_TOTAL_WIDTH (w);
22768 else
22769 max_x = window_box_width (w, updated_area);
22770 max_y = window_text_bottom_y (w);
22771
22772 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22773 of window. For TO_X > 0, truncate to end of drawing area. */
22774 if (to_x == 0)
22775 return;
22776 else if (to_x < 0)
22777 to_x = max_x;
22778 else
22779 to_x = min (to_x, max_x);
22780
22781 to_y = min (max_y, output_cursor.y + updated_row->height);
22782
22783 /* Notice if the cursor will be cleared by this operation. */
22784 if (!updated_row->full_width_p)
22785 notice_overwritten_cursor (w, updated_area,
22786 output_cursor.x, -1,
22787 updated_row->y,
22788 MATRIX_ROW_BOTTOM_Y (updated_row));
22789
22790 from_x = output_cursor.x;
22791
22792 /* Translate to frame coordinates. */
22793 if (updated_row->full_width_p)
22794 {
22795 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22796 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22797 }
22798 else
22799 {
22800 int area_left = window_box_left (w, updated_area);
22801 from_x += area_left;
22802 to_x += area_left;
22803 }
22804
22805 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22806 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22807 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22808
22809 /* Prevent inadvertently clearing to end of the X window. */
22810 if (to_x > from_x && to_y > from_y)
22811 {
22812 BLOCK_INPUT;
22813 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22814 to_x - from_x, to_y - from_y);
22815 UNBLOCK_INPUT;
22816 }
22817 }
22818
22819 #endif /* HAVE_WINDOW_SYSTEM */
22820
22821
22822 \f
22823 /***********************************************************************
22824 Cursor types
22825 ***********************************************************************/
22826
22827 /* Value is the internal representation of the specified cursor type
22828 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22829 of the bar cursor. */
22830
22831 static enum text_cursor_kinds
22832 get_specified_cursor_type (arg, width)
22833 Lisp_Object arg;
22834 int *width;
22835 {
22836 enum text_cursor_kinds type;
22837
22838 if (NILP (arg))
22839 return NO_CURSOR;
22840
22841 if (EQ (arg, Qbox))
22842 return FILLED_BOX_CURSOR;
22843
22844 if (EQ (arg, Qhollow))
22845 return HOLLOW_BOX_CURSOR;
22846
22847 if (EQ (arg, Qbar))
22848 {
22849 *width = 2;
22850 return BAR_CURSOR;
22851 }
22852
22853 if (CONSP (arg)
22854 && EQ (XCAR (arg), Qbar)
22855 && INTEGERP (XCDR (arg))
22856 && XINT (XCDR (arg)) >= 0)
22857 {
22858 *width = XINT (XCDR (arg));
22859 return BAR_CURSOR;
22860 }
22861
22862 if (EQ (arg, Qhbar))
22863 {
22864 *width = 2;
22865 return HBAR_CURSOR;
22866 }
22867
22868 if (CONSP (arg)
22869 && EQ (XCAR (arg), Qhbar)
22870 && INTEGERP (XCDR (arg))
22871 && XINT (XCDR (arg)) >= 0)
22872 {
22873 *width = XINT (XCDR (arg));
22874 return HBAR_CURSOR;
22875 }
22876
22877 /* Treat anything unknown as "hollow box cursor".
22878 It was bad to signal an error; people have trouble fixing
22879 .Xdefaults with Emacs, when it has something bad in it. */
22880 type = HOLLOW_BOX_CURSOR;
22881
22882 return type;
22883 }
22884
22885 /* Set the default cursor types for specified frame. */
22886 void
22887 set_frame_cursor_types (f, arg)
22888 struct frame *f;
22889 Lisp_Object arg;
22890 {
22891 int width;
22892 Lisp_Object tem;
22893
22894 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22895 FRAME_CURSOR_WIDTH (f) = width;
22896
22897 /* By default, set up the blink-off state depending on the on-state. */
22898
22899 tem = Fassoc (arg, Vblink_cursor_alist);
22900 if (!NILP (tem))
22901 {
22902 FRAME_BLINK_OFF_CURSOR (f)
22903 = get_specified_cursor_type (XCDR (tem), &width);
22904 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22905 }
22906 else
22907 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22908 }
22909
22910
22911 /* Return the cursor we want to be displayed in window W. Return
22912 width of bar/hbar cursor through WIDTH arg. Return with
22913 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22914 (i.e. if the `system caret' should track this cursor).
22915
22916 In a mini-buffer window, we want the cursor only to appear if we
22917 are reading input from this window. For the selected window, we
22918 want the cursor type given by the frame parameter or buffer local
22919 setting of cursor-type. If explicitly marked off, draw no cursor.
22920 In all other cases, we want a hollow box cursor. */
22921
22922 static enum text_cursor_kinds
22923 get_window_cursor_type (w, glyph, width, active_cursor)
22924 struct window *w;
22925 struct glyph *glyph;
22926 int *width;
22927 int *active_cursor;
22928 {
22929 struct frame *f = XFRAME (w->frame);
22930 struct buffer *b = XBUFFER (w->buffer);
22931 int cursor_type = DEFAULT_CURSOR;
22932 Lisp_Object alt_cursor;
22933 int non_selected = 0;
22934
22935 *active_cursor = 1;
22936
22937 /* Echo area */
22938 if (cursor_in_echo_area
22939 && FRAME_HAS_MINIBUF_P (f)
22940 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22941 {
22942 if (w == XWINDOW (echo_area_window))
22943 {
22944 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22945 {
22946 *width = FRAME_CURSOR_WIDTH (f);
22947 return FRAME_DESIRED_CURSOR (f);
22948 }
22949 else
22950 return get_specified_cursor_type (b->cursor_type, width);
22951 }
22952
22953 *active_cursor = 0;
22954 non_selected = 1;
22955 }
22956
22957 /* Detect a nonselected window or nonselected frame. */
22958 else if (w != XWINDOW (f->selected_window)
22959 #ifdef HAVE_WINDOW_SYSTEM
22960 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22961 #endif
22962 )
22963 {
22964 *active_cursor = 0;
22965
22966 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22967 return NO_CURSOR;
22968
22969 non_selected = 1;
22970 }
22971
22972 /* Never display a cursor in a window in which cursor-type is nil. */
22973 if (NILP (b->cursor_type))
22974 return NO_CURSOR;
22975
22976 /* Get the normal cursor type for this window. */
22977 if (EQ (b->cursor_type, Qt))
22978 {
22979 cursor_type = FRAME_DESIRED_CURSOR (f);
22980 *width = FRAME_CURSOR_WIDTH (f);
22981 }
22982 else
22983 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22984
22985 /* Use cursor-in-non-selected-windows instead
22986 for non-selected window or frame. */
22987 if (non_selected)
22988 {
22989 alt_cursor = b->cursor_in_non_selected_windows;
22990 if (!EQ (Qt, alt_cursor))
22991 return get_specified_cursor_type (alt_cursor, width);
22992 /* t means modify the normal cursor type. */
22993 if (cursor_type == FILLED_BOX_CURSOR)
22994 cursor_type = HOLLOW_BOX_CURSOR;
22995 else if (cursor_type == BAR_CURSOR && *width > 1)
22996 --*width;
22997 return cursor_type;
22998 }
22999
23000 /* Use normal cursor if not blinked off. */
23001 if (!w->cursor_off_p)
23002 {
23003 #ifdef HAVE_WINDOW_SYSTEM
23004 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23005 {
23006 if (cursor_type == FILLED_BOX_CURSOR)
23007 {
23008 /* Using a block cursor on large images can be very annoying.
23009 So use a hollow cursor for "large" images.
23010 If image is not transparent (no mask), also use hollow cursor. */
23011 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23012 if (img != NULL && IMAGEP (img->spec))
23013 {
23014 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23015 where N = size of default frame font size.
23016 This should cover most of the "tiny" icons people may use. */
23017 if (!img->mask
23018 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23019 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23020 cursor_type = HOLLOW_BOX_CURSOR;
23021 }
23022 }
23023 else if (cursor_type != NO_CURSOR)
23024 {
23025 /* Display current only supports BOX and HOLLOW cursors for images.
23026 So for now, unconditionally use a HOLLOW cursor when cursor is
23027 not a solid box cursor. */
23028 cursor_type = HOLLOW_BOX_CURSOR;
23029 }
23030 }
23031 #endif
23032 return cursor_type;
23033 }
23034
23035 /* Cursor is blinked off, so determine how to "toggle" it. */
23036
23037 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23038 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23039 return get_specified_cursor_type (XCDR (alt_cursor), width);
23040
23041 /* Then see if frame has specified a specific blink off cursor type. */
23042 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23043 {
23044 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23045 return FRAME_BLINK_OFF_CURSOR (f);
23046 }
23047
23048 #if 0
23049 /* Some people liked having a permanently visible blinking cursor,
23050 while others had very strong opinions against it. So it was
23051 decided to remove it. KFS 2003-09-03 */
23052
23053 /* Finally perform built-in cursor blinking:
23054 filled box <-> hollow box
23055 wide [h]bar <-> narrow [h]bar
23056 narrow [h]bar <-> no cursor
23057 other type <-> no cursor */
23058
23059 if (cursor_type == FILLED_BOX_CURSOR)
23060 return HOLLOW_BOX_CURSOR;
23061
23062 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23063 {
23064 *width = 1;
23065 return cursor_type;
23066 }
23067 #endif
23068
23069 return NO_CURSOR;
23070 }
23071
23072
23073 #ifdef HAVE_WINDOW_SYSTEM
23074
23075 /* Notice when the text cursor of window W has been completely
23076 overwritten by a drawing operation that outputs glyphs in AREA
23077 starting at X0 and ending at X1 in the line starting at Y0 and
23078 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23079 the rest of the line after X0 has been written. Y coordinates
23080 are window-relative. */
23081
23082 static void
23083 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
23084 struct window *w;
23085 enum glyph_row_area area;
23086 int x0, y0, x1, y1;
23087 {
23088 int cx0, cx1, cy0, cy1;
23089 struct glyph_row *row;
23090
23091 if (!w->phys_cursor_on_p)
23092 return;
23093 if (area != TEXT_AREA)
23094 return;
23095
23096 if (w->phys_cursor.vpos < 0
23097 || w->phys_cursor.vpos >= w->current_matrix->nrows
23098 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23099 !(row->enabled_p && row->displays_text_p)))
23100 return;
23101
23102 if (row->cursor_in_fringe_p)
23103 {
23104 row->cursor_in_fringe_p = 0;
23105 draw_fringe_bitmap (w, row, 0);
23106 w->phys_cursor_on_p = 0;
23107 return;
23108 }
23109
23110 cx0 = w->phys_cursor.x;
23111 cx1 = cx0 + w->phys_cursor_width;
23112 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23113 return;
23114
23115 /* The cursor image will be completely removed from the
23116 screen if the output area intersects the cursor area in
23117 y-direction. When we draw in [y0 y1[, and some part of
23118 the cursor is at y < y0, that part must have been drawn
23119 before. When scrolling, the cursor is erased before
23120 actually scrolling, so we don't come here. When not
23121 scrolling, the rows above the old cursor row must have
23122 changed, and in this case these rows must have written
23123 over the cursor image.
23124
23125 Likewise if part of the cursor is below y1, with the
23126 exception of the cursor being in the first blank row at
23127 the buffer and window end because update_text_area
23128 doesn't draw that row. (Except when it does, but
23129 that's handled in update_text_area.) */
23130
23131 cy0 = w->phys_cursor.y;
23132 cy1 = cy0 + w->phys_cursor_height;
23133 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23134 return;
23135
23136 w->phys_cursor_on_p = 0;
23137 }
23138
23139 #endif /* HAVE_WINDOW_SYSTEM */
23140
23141 \f
23142 /************************************************************************
23143 Mouse Face
23144 ************************************************************************/
23145
23146 #ifdef HAVE_WINDOW_SYSTEM
23147
23148 /* EXPORT for RIF:
23149 Fix the display of area AREA of overlapping row ROW in window W
23150 with respect to the overlapping part OVERLAPS. */
23151
23152 void
23153 x_fix_overlapping_area (w, row, area, overlaps)
23154 struct window *w;
23155 struct glyph_row *row;
23156 enum glyph_row_area area;
23157 int overlaps;
23158 {
23159 int i, x;
23160
23161 BLOCK_INPUT;
23162
23163 x = 0;
23164 for (i = 0; i < row->used[area];)
23165 {
23166 if (row->glyphs[area][i].overlaps_vertically_p)
23167 {
23168 int start = i, start_x = x;
23169
23170 do
23171 {
23172 x += row->glyphs[area][i].pixel_width;
23173 ++i;
23174 }
23175 while (i < row->used[area]
23176 && row->glyphs[area][i].overlaps_vertically_p);
23177
23178 draw_glyphs (w, start_x, row, area,
23179 start, i,
23180 DRAW_NORMAL_TEXT, overlaps);
23181 }
23182 else
23183 {
23184 x += row->glyphs[area][i].pixel_width;
23185 ++i;
23186 }
23187 }
23188
23189 UNBLOCK_INPUT;
23190 }
23191
23192
23193 /* EXPORT:
23194 Draw the cursor glyph of window W in glyph row ROW. See the
23195 comment of draw_glyphs for the meaning of HL. */
23196
23197 void
23198 draw_phys_cursor_glyph (w, row, hl)
23199 struct window *w;
23200 struct glyph_row *row;
23201 enum draw_glyphs_face hl;
23202 {
23203 /* If cursor hpos is out of bounds, don't draw garbage. This can
23204 happen in mini-buffer windows when switching between echo area
23205 glyphs and mini-buffer. */
23206 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
23207 {
23208 int on_p = w->phys_cursor_on_p;
23209 int x1;
23210 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23211 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23212 hl, 0);
23213 w->phys_cursor_on_p = on_p;
23214
23215 if (hl == DRAW_CURSOR)
23216 w->phys_cursor_width = x1 - w->phys_cursor.x;
23217 /* When we erase the cursor, and ROW is overlapped by other
23218 rows, make sure that these overlapping parts of other rows
23219 are redrawn. */
23220 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23221 {
23222 w->phys_cursor_width = x1 - w->phys_cursor.x;
23223
23224 if (row > w->current_matrix->rows
23225 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23226 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23227 OVERLAPS_ERASED_CURSOR);
23228
23229 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23230 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23231 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23232 OVERLAPS_ERASED_CURSOR);
23233 }
23234 }
23235 }
23236
23237
23238 /* EXPORT:
23239 Erase the image of a cursor of window W from the screen. */
23240
23241 void
23242 erase_phys_cursor (w)
23243 struct window *w;
23244 {
23245 struct frame *f = XFRAME (w->frame);
23246 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23247 int hpos = w->phys_cursor.hpos;
23248 int vpos = w->phys_cursor.vpos;
23249 int mouse_face_here_p = 0;
23250 struct glyph_matrix *active_glyphs = w->current_matrix;
23251 struct glyph_row *cursor_row;
23252 struct glyph *cursor_glyph;
23253 enum draw_glyphs_face hl;
23254
23255 /* No cursor displayed or row invalidated => nothing to do on the
23256 screen. */
23257 if (w->phys_cursor_type == NO_CURSOR)
23258 goto mark_cursor_off;
23259
23260 /* VPOS >= active_glyphs->nrows means that window has been resized.
23261 Don't bother to erase the cursor. */
23262 if (vpos >= active_glyphs->nrows)
23263 goto mark_cursor_off;
23264
23265 /* If row containing cursor is marked invalid, there is nothing we
23266 can do. */
23267 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23268 if (!cursor_row->enabled_p)
23269 goto mark_cursor_off;
23270
23271 /* If line spacing is > 0, old cursor may only be partially visible in
23272 window after split-window. So adjust visible height. */
23273 cursor_row->visible_height = min (cursor_row->visible_height,
23274 window_text_bottom_y (w) - cursor_row->y);
23275
23276 /* If row is completely invisible, don't attempt to delete a cursor which
23277 isn't there. This can happen if cursor is at top of a window, and
23278 we switch to a buffer with a header line in that window. */
23279 if (cursor_row->visible_height <= 0)
23280 goto mark_cursor_off;
23281
23282 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23283 if (cursor_row->cursor_in_fringe_p)
23284 {
23285 cursor_row->cursor_in_fringe_p = 0;
23286 draw_fringe_bitmap (w, cursor_row, 0);
23287 goto mark_cursor_off;
23288 }
23289
23290 /* This can happen when the new row is shorter than the old one.
23291 In this case, either draw_glyphs or clear_end_of_line
23292 should have cleared the cursor. Note that we wouldn't be
23293 able to erase the cursor in this case because we don't have a
23294 cursor glyph at hand. */
23295 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
23296 goto mark_cursor_off;
23297
23298 /* If the cursor is in the mouse face area, redisplay that when
23299 we clear the cursor. */
23300 if (! NILP (dpyinfo->mouse_face_window)
23301 && w == XWINDOW (dpyinfo->mouse_face_window)
23302 && (vpos > dpyinfo->mouse_face_beg_row
23303 || (vpos == dpyinfo->mouse_face_beg_row
23304 && hpos >= dpyinfo->mouse_face_beg_col))
23305 && (vpos < dpyinfo->mouse_face_end_row
23306 || (vpos == dpyinfo->mouse_face_end_row
23307 && hpos < dpyinfo->mouse_face_end_col))
23308 /* Don't redraw the cursor's spot in mouse face if it is at the
23309 end of a line (on a newline). The cursor appears there, but
23310 mouse highlighting does not. */
23311 && cursor_row->used[TEXT_AREA] > hpos)
23312 mouse_face_here_p = 1;
23313
23314 /* Maybe clear the display under the cursor. */
23315 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23316 {
23317 int x, y, left_x;
23318 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23319 int width;
23320
23321 cursor_glyph = get_phys_cursor_glyph (w);
23322 if (cursor_glyph == NULL)
23323 goto mark_cursor_off;
23324
23325 width = cursor_glyph->pixel_width;
23326 left_x = window_box_left_offset (w, TEXT_AREA);
23327 x = w->phys_cursor.x;
23328 if (x < left_x)
23329 width -= left_x - x;
23330 width = min (width, window_box_width (w, TEXT_AREA) - x);
23331 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23332 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23333
23334 if (width > 0)
23335 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23336 }
23337
23338 /* Erase the cursor by redrawing the character underneath it. */
23339 if (mouse_face_here_p)
23340 hl = DRAW_MOUSE_FACE;
23341 else
23342 hl = DRAW_NORMAL_TEXT;
23343 draw_phys_cursor_glyph (w, cursor_row, hl);
23344
23345 mark_cursor_off:
23346 w->phys_cursor_on_p = 0;
23347 w->phys_cursor_type = NO_CURSOR;
23348 }
23349
23350
23351 /* EXPORT:
23352 Display or clear cursor of window W. If ON is zero, clear the
23353 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23354 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23355
23356 void
23357 display_and_set_cursor (w, on, hpos, vpos, x, y)
23358 struct window *w;
23359 int on, hpos, vpos, x, y;
23360 {
23361 struct frame *f = XFRAME (w->frame);
23362 int new_cursor_type;
23363 int new_cursor_width;
23364 int active_cursor;
23365 struct glyph_row *glyph_row;
23366 struct glyph *glyph;
23367
23368 /* This is pointless on invisible frames, and dangerous on garbaged
23369 windows and frames; in the latter case, the frame or window may
23370 be in the midst of changing its size, and x and y may be off the
23371 window. */
23372 if (! FRAME_VISIBLE_P (f)
23373 || FRAME_GARBAGED_P (f)
23374 || vpos >= w->current_matrix->nrows
23375 || hpos >= w->current_matrix->matrix_w)
23376 return;
23377
23378 /* If cursor is off and we want it off, return quickly. */
23379 if (!on && !w->phys_cursor_on_p)
23380 return;
23381
23382 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23383 /* If cursor row is not enabled, we don't really know where to
23384 display the cursor. */
23385 if (!glyph_row->enabled_p)
23386 {
23387 w->phys_cursor_on_p = 0;
23388 return;
23389 }
23390
23391 glyph = NULL;
23392 if (!glyph_row->exact_window_width_line_p
23393 || hpos < glyph_row->used[TEXT_AREA])
23394 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23395
23396 xassert (interrupt_input_blocked);
23397
23398 /* Set new_cursor_type to the cursor we want to be displayed. */
23399 new_cursor_type = get_window_cursor_type (w, glyph,
23400 &new_cursor_width, &active_cursor);
23401
23402 /* If cursor is currently being shown and we don't want it to be or
23403 it is in the wrong place, or the cursor type is not what we want,
23404 erase it. */
23405 if (w->phys_cursor_on_p
23406 && (!on
23407 || w->phys_cursor.x != x
23408 || w->phys_cursor.y != y
23409 || new_cursor_type != w->phys_cursor_type
23410 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23411 && new_cursor_width != w->phys_cursor_width)))
23412 erase_phys_cursor (w);
23413
23414 /* Don't check phys_cursor_on_p here because that flag is only set
23415 to zero in some cases where we know that the cursor has been
23416 completely erased, to avoid the extra work of erasing the cursor
23417 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23418 still not be visible, or it has only been partly erased. */
23419 if (on)
23420 {
23421 w->phys_cursor_ascent = glyph_row->ascent;
23422 w->phys_cursor_height = glyph_row->height;
23423
23424 /* Set phys_cursor_.* before x_draw_.* is called because some
23425 of them may need the information. */
23426 w->phys_cursor.x = x;
23427 w->phys_cursor.y = glyph_row->y;
23428 w->phys_cursor.hpos = hpos;
23429 w->phys_cursor.vpos = vpos;
23430 }
23431
23432 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23433 new_cursor_type, new_cursor_width,
23434 on, active_cursor);
23435 }
23436
23437
23438 /* Switch the display of W's cursor on or off, according to the value
23439 of ON. */
23440
23441 void
23442 update_window_cursor (w, on)
23443 struct window *w;
23444 int on;
23445 {
23446 /* Don't update cursor in windows whose frame is in the process
23447 of being deleted. */
23448 if (w->current_matrix)
23449 {
23450 BLOCK_INPUT;
23451 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23452 w->phys_cursor.x, w->phys_cursor.y);
23453 UNBLOCK_INPUT;
23454 }
23455 }
23456
23457
23458 /* Call update_window_cursor with parameter ON_P on all leaf windows
23459 in the window tree rooted at W. */
23460
23461 static void
23462 update_cursor_in_window_tree (w, on_p)
23463 struct window *w;
23464 int on_p;
23465 {
23466 while (w)
23467 {
23468 if (!NILP (w->hchild))
23469 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23470 else if (!NILP (w->vchild))
23471 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23472 else
23473 update_window_cursor (w, on_p);
23474
23475 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23476 }
23477 }
23478
23479
23480 /* EXPORT:
23481 Display the cursor on window W, or clear it, according to ON_P.
23482 Don't change the cursor's position. */
23483
23484 void
23485 x_update_cursor (f, on_p)
23486 struct frame *f;
23487 int on_p;
23488 {
23489 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23490 }
23491
23492
23493 /* EXPORT:
23494 Clear the cursor of window W to background color, and mark the
23495 cursor as not shown. This is used when the text where the cursor
23496 is about to be rewritten. */
23497
23498 void
23499 x_clear_cursor (w)
23500 struct window *w;
23501 {
23502 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23503 update_window_cursor (w, 0);
23504 }
23505
23506
23507 /* EXPORT:
23508 Display the active region described by mouse_face_* according to DRAW. */
23509
23510 void
23511 show_mouse_face (dpyinfo, draw)
23512 Display_Info *dpyinfo;
23513 enum draw_glyphs_face draw;
23514 {
23515 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23516 struct frame *f = XFRAME (WINDOW_FRAME (w));
23517
23518 if (/* If window is in the process of being destroyed, don't bother
23519 to do anything. */
23520 w->current_matrix != NULL
23521 /* Don't update mouse highlight if hidden */
23522 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23523 /* Recognize when we are called to operate on rows that don't exist
23524 anymore. This can happen when a window is split. */
23525 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23526 {
23527 int phys_cursor_on_p = w->phys_cursor_on_p;
23528 struct glyph_row *row, *first, *last;
23529
23530 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23531 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23532
23533 for (row = first; row <= last && row->enabled_p; ++row)
23534 {
23535 int start_hpos, end_hpos, start_x;
23536
23537 /* For all but the first row, the highlight starts at column 0. */
23538 if (row == first)
23539 {
23540 start_hpos = dpyinfo->mouse_face_beg_col;
23541 start_x = dpyinfo->mouse_face_beg_x;
23542 }
23543 else
23544 {
23545 start_hpos = 0;
23546 start_x = 0;
23547 }
23548
23549 if (row == last)
23550 end_hpos = dpyinfo->mouse_face_end_col;
23551 else
23552 {
23553 end_hpos = row->used[TEXT_AREA];
23554 if (draw == DRAW_NORMAL_TEXT)
23555 row->fill_line_p = 1; /* Clear to end of line */
23556 }
23557
23558 if (end_hpos > start_hpos)
23559 {
23560 draw_glyphs (w, start_x, row, TEXT_AREA,
23561 start_hpos, end_hpos,
23562 draw, 0);
23563
23564 row->mouse_face_p
23565 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23566 }
23567 }
23568
23569 /* When we've written over the cursor, arrange for it to
23570 be displayed again. */
23571 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23572 {
23573 BLOCK_INPUT;
23574 display_and_set_cursor (w, 1,
23575 w->phys_cursor.hpos, w->phys_cursor.vpos,
23576 w->phys_cursor.x, w->phys_cursor.y);
23577 UNBLOCK_INPUT;
23578 }
23579 }
23580
23581 /* Change the mouse cursor. */
23582 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23583 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23584 else if (draw == DRAW_MOUSE_FACE)
23585 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23586 else
23587 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23588 }
23589
23590 /* EXPORT:
23591 Clear out the mouse-highlighted active region.
23592 Redraw it un-highlighted first. Value is non-zero if mouse
23593 face was actually drawn unhighlighted. */
23594
23595 int
23596 clear_mouse_face (dpyinfo)
23597 Display_Info *dpyinfo;
23598 {
23599 int cleared = 0;
23600
23601 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23602 {
23603 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23604 cleared = 1;
23605 }
23606
23607 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23608 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23609 dpyinfo->mouse_face_window = Qnil;
23610 dpyinfo->mouse_face_overlay = Qnil;
23611 return cleared;
23612 }
23613
23614
23615 /* EXPORT:
23616 Non-zero if physical cursor of window W is within mouse face. */
23617
23618 int
23619 cursor_in_mouse_face_p (w)
23620 struct window *w;
23621 {
23622 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23623 int in_mouse_face = 0;
23624
23625 if (WINDOWP (dpyinfo->mouse_face_window)
23626 && XWINDOW (dpyinfo->mouse_face_window) == w)
23627 {
23628 int hpos = w->phys_cursor.hpos;
23629 int vpos = w->phys_cursor.vpos;
23630
23631 if (vpos >= dpyinfo->mouse_face_beg_row
23632 && vpos <= dpyinfo->mouse_face_end_row
23633 && (vpos > dpyinfo->mouse_face_beg_row
23634 || hpos >= dpyinfo->mouse_face_beg_col)
23635 && (vpos < dpyinfo->mouse_face_end_row
23636 || hpos < dpyinfo->mouse_face_end_col
23637 || dpyinfo->mouse_face_past_end))
23638 in_mouse_face = 1;
23639 }
23640
23641 return in_mouse_face;
23642 }
23643
23644
23645
23646 \f
23647 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23648 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23649 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23650 for the overlay or run of text properties specifying the mouse
23651 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23652 before-string and after-string that must also be highlighted.
23653 DISPLAY_STRING, if non-nil, is a display string that may cover some
23654 or all of the highlighted text. */
23655
23656 static void
23657 mouse_face_from_buffer_pos (Lisp_Object window,
23658 Display_Info *dpyinfo,
23659 EMACS_INT mouse_charpos,
23660 EMACS_INT start_charpos,
23661 EMACS_INT end_charpos,
23662 Lisp_Object before_string,
23663 Lisp_Object after_string,
23664 Lisp_Object display_string)
23665 {
23666 struct window *w = XWINDOW (window);
23667 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23668 struct glyph_row *row;
23669 struct glyph *glyph, *end;
23670 EMACS_INT ignore;
23671 int x;
23672
23673 xassert (NILP (display_string) || STRINGP (display_string));
23674 xassert (NILP (before_string) || STRINGP (before_string));
23675 xassert (NILP (after_string) || STRINGP (after_string));
23676
23677 /* Find the first highlighted glyph. */
23678 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
23679 {
23680 dpyinfo->mouse_face_beg_col = 0;
23681 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23682 dpyinfo->mouse_face_beg_x = first->x;
23683 dpyinfo->mouse_face_beg_y = first->y;
23684 }
23685 else
23686 {
23687 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23688 if (row == NULL)
23689 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23690
23691 /* If the before-string or display-string contains newlines,
23692 row_containing_pos skips to its last row. Move back. */
23693 if (!NILP (before_string) || !NILP (display_string))
23694 {
23695 struct glyph_row *prev;
23696 while ((prev = row - 1, prev >= first)
23697 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23698 && prev->used[TEXT_AREA] > 0)
23699 {
23700 struct glyph *beg = prev->glyphs[TEXT_AREA];
23701 glyph = beg + prev->used[TEXT_AREA];
23702 while (--glyph >= beg && INTEGERP (glyph->object));
23703 if (glyph < beg
23704 || !(EQ (glyph->object, before_string)
23705 || EQ (glyph->object, display_string)))
23706 break;
23707 row = prev;
23708 }
23709 }
23710
23711 glyph = row->glyphs[TEXT_AREA];
23712 end = glyph + row->used[TEXT_AREA];
23713 x = row->x;
23714 dpyinfo->mouse_face_beg_y = row->y;
23715 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23716
23717 /* Skip truncation glyphs at the start of the glyph row. */
23718 if (row->displays_text_p)
23719 for (; glyph < end
23720 && INTEGERP (glyph->object)
23721 && glyph->charpos < 0;
23722 ++glyph)
23723 x += glyph->pixel_width;
23724
23725 /* Scan the glyph row, stopping before BEFORE_STRING or
23726 DISPLAY_STRING or START_CHARPOS. */
23727 for (; glyph < end
23728 && !INTEGERP (glyph->object)
23729 && !EQ (glyph->object, before_string)
23730 && !EQ (glyph->object, display_string)
23731 && !(BUFFERP (glyph->object)
23732 && glyph->charpos >= start_charpos);
23733 ++glyph)
23734 x += glyph->pixel_width;
23735
23736 dpyinfo->mouse_face_beg_x = x;
23737 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23738 }
23739
23740 /* Find the last highlighted glyph. */
23741 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23742 if (row == NULL)
23743 {
23744 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23745 dpyinfo->mouse_face_past_end = 1;
23746 }
23747 else if (!NILP (after_string))
23748 {
23749 /* If the after-string has newlines, advance to its last row. */
23750 struct glyph_row *next;
23751 struct glyph_row *last
23752 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23753
23754 for (next = row + 1;
23755 next <= last
23756 && next->used[TEXT_AREA] > 0
23757 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23758 ++next)
23759 row = next;
23760 }
23761
23762 glyph = row->glyphs[TEXT_AREA];
23763 end = glyph + row->used[TEXT_AREA];
23764 x = row->x;
23765 dpyinfo->mouse_face_end_y = row->y;
23766 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23767
23768 /* Skip truncation glyphs at the start of the row. */
23769 if (row->displays_text_p)
23770 for (; glyph < end
23771 && INTEGERP (glyph->object)
23772 && glyph->charpos < 0;
23773 ++glyph)
23774 x += glyph->pixel_width;
23775
23776 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23777 AFTER_STRING. */
23778 for (; glyph < end
23779 && !INTEGERP (glyph->object)
23780 && !EQ (glyph->object, after_string)
23781 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23782 ++glyph)
23783 x += glyph->pixel_width;
23784
23785 /* If we found AFTER_STRING, consume it and stop. */
23786 if (EQ (glyph->object, after_string))
23787 {
23788 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23789 x += glyph->pixel_width;
23790 }
23791 else
23792 {
23793 /* If there's no after-string, we must check if we overshot,
23794 which might be the case if we stopped after a string glyph.
23795 That glyph may belong to a before-string or display-string
23796 associated with the end position, which must not be
23797 highlighted. */
23798 Lisp_Object prev_object;
23799 EMACS_INT pos;
23800
23801 while (glyph > row->glyphs[TEXT_AREA])
23802 {
23803 prev_object = (glyph - 1)->object;
23804 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23805 break;
23806
23807 pos = string_buffer_position (w, prev_object, end_charpos);
23808 if (pos && pos < end_charpos)
23809 break;
23810
23811 for (; glyph > row->glyphs[TEXT_AREA]
23812 && EQ ((glyph - 1)->object, prev_object);
23813 --glyph)
23814 x -= (glyph - 1)->pixel_width;
23815 }
23816 }
23817
23818 dpyinfo->mouse_face_end_x = x;
23819 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23820 dpyinfo->mouse_face_window = window;
23821 dpyinfo->mouse_face_face_id
23822 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23823 mouse_charpos + 1,
23824 !dpyinfo->mouse_face_hidden, -1);
23825 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23826 }
23827
23828
23829 /* Find the position of the glyph for position POS in OBJECT in
23830 window W's current matrix, and return in *X, *Y the pixel
23831 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23832
23833 RIGHT_P non-zero means return the position of the right edge of the
23834 glyph, RIGHT_P zero means return the left edge position.
23835
23836 If no glyph for POS exists in the matrix, return the position of
23837 the glyph with the next smaller position that is in the matrix, if
23838 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23839 exists in the matrix, return the position of the glyph with the
23840 next larger position in OBJECT.
23841
23842 Value is non-zero if a glyph was found. */
23843
23844 static int
23845 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23846 struct window *w;
23847 EMACS_INT pos;
23848 Lisp_Object object;
23849 int *hpos, *vpos, *x, *y;
23850 int right_p;
23851 {
23852 int yb = window_text_bottom_y (w);
23853 struct glyph_row *r;
23854 struct glyph *best_glyph = NULL;
23855 struct glyph_row *best_row = NULL;
23856 int best_x = 0;
23857
23858 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23859 r->enabled_p && r->y < yb;
23860 ++r)
23861 {
23862 struct glyph *g = r->glyphs[TEXT_AREA];
23863 struct glyph *e = g + r->used[TEXT_AREA];
23864 int gx;
23865
23866 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23867 if (EQ (g->object, object))
23868 {
23869 if (g->charpos == pos)
23870 {
23871 best_glyph = g;
23872 best_x = gx;
23873 best_row = r;
23874 goto found;
23875 }
23876 else if (best_glyph == NULL
23877 || ((eabs (g->charpos - pos)
23878 < eabs (best_glyph->charpos - pos))
23879 && (right_p
23880 ? g->charpos < pos
23881 : g->charpos > pos)))
23882 {
23883 best_glyph = g;
23884 best_x = gx;
23885 best_row = r;
23886 }
23887 }
23888 }
23889
23890 found:
23891
23892 if (best_glyph)
23893 {
23894 *x = best_x;
23895 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23896
23897 if (right_p)
23898 {
23899 *x += best_glyph->pixel_width;
23900 ++*hpos;
23901 }
23902
23903 *y = best_row->y;
23904 *vpos = best_row - w->current_matrix->rows;
23905 }
23906
23907 return best_glyph != NULL;
23908 }
23909
23910
23911 /* See if position X, Y is within a hot-spot of an image. */
23912
23913 static int
23914 on_hot_spot_p (hot_spot, x, y)
23915 Lisp_Object hot_spot;
23916 int x, y;
23917 {
23918 if (!CONSP (hot_spot))
23919 return 0;
23920
23921 if (EQ (XCAR (hot_spot), Qrect))
23922 {
23923 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23924 Lisp_Object rect = XCDR (hot_spot);
23925 Lisp_Object tem;
23926 if (!CONSP (rect))
23927 return 0;
23928 if (!CONSP (XCAR (rect)))
23929 return 0;
23930 if (!CONSP (XCDR (rect)))
23931 return 0;
23932 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23933 return 0;
23934 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23935 return 0;
23936 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23937 return 0;
23938 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23939 return 0;
23940 return 1;
23941 }
23942 else if (EQ (XCAR (hot_spot), Qcircle))
23943 {
23944 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23945 Lisp_Object circ = XCDR (hot_spot);
23946 Lisp_Object lr, lx0, ly0;
23947 if (CONSP (circ)
23948 && CONSP (XCAR (circ))
23949 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23950 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23951 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23952 {
23953 double r = XFLOATINT (lr);
23954 double dx = XINT (lx0) - x;
23955 double dy = XINT (ly0) - y;
23956 return (dx * dx + dy * dy <= r * r);
23957 }
23958 }
23959 else if (EQ (XCAR (hot_spot), Qpoly))
23960 {
23961 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23962 if (VECTORP (XCDR (hot_spot)))
23963 {
23964 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23965 Lisp_Object *poly = v->contents;
23966 int n = v->size;
23967 int i;
23968 int inside = 0;
23969 Lisp_Object lx, ly;
23970 int x0, y0;
23971
23972 /* Need an even number of coordinates, and at least 3 edges. */
23973 if (n < 6 || n & 1)
23974 return 0;
23975
23976 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23977 If count is odd, we are inside polygon. Pixels on edges
23978 may or may not be included depending on actual geometry of the
23979 polygon. */
23980 if ((lx = poly[n-2], !INTEGERP (lx))
23981 || (ly = poly[n-1], !INTEGERP (lx)))
23982 return 0;
23983 x0 = XINT (lx), y0 = XINT (ly);
23984 for (i = 0; i < n; i += 2)
23985 {
23986 int x1 = x0, y1 = y0;
23987 if ((lx = poly[i], !INTEGERP (lx))
23988 || (ly = poly[i+1], !INTEGERP (ly)))
23989 return 0;
23990 x0 = XINT (lx), y0 = XINT (ly);
23991
23992 /* Does this segment cross the X line? */
23993 if (x0 >= x)
23994 {
23995 if (x1 >= x)
23996 continue;
23997 }
23998 else if (x1 < x)
23999 continue;
24000 if (y > y0 && y > y1)
24001 continue;
24002 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24003 inside = !inside;
24004 }
24005 return inside;
24006 }
24007 }
24008 return 0;
24009 }
24010
24011 Lisp_Object
24012 find_hot_spot (map, x, y)
24013 Lisp_Object map;
24014 int x, y;
24015 {
24016 while (CONSP (map))
24017 {
24018 if (CONSP (XCAR (map))
24019 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24020 return XCAR (map);
24021 map = XCDR (map);
24022 }
24023
24024 return Qnil;
24025 }
24026
24027 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24028 3, 3, 0,
24029 doc: /* Lookup in image map MAP coordinates X and Y.
24030 An image map is an alist where each element has the format (AREA ID PLIST).
24031 An AREA is specified as either a rectangle, a circle, or a polygon:
24032 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24033 pixel coordinates of the upper left and bottom right corners.
24034 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24035 and the radius of the circle; r may be a float or integer.
24036 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24037 vector describes one corner in the polygon.
24038 Returns the alist element for the first matching AREA in MAP. */)
24039 (map, x, y)
24040 Lisp_Object map;
24041 Lisp_Object x, y;
24042 {
24043 if (NILP (map))
24044 return Qnil;
24045
24046 CHECK_NUMBER (x);
24047 CHECK_NUMBER (y);
24048
24049 return find_hot_spot (map, XINT (x), XINT (y));
24050 }
24051
24052
24053 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24054 static void
24055 define_frame_cursor1 (f, cursor, pointer)
24056 struct frame *f;
24057 Cursor cursor;
24058 Lisp_Object pointer;
24059 {
24060 /* Do not change cursor shape while dragging mouse. */
24061 if (!NILP (do_mouse_tracking))
24062 return;
24063
24064 if (!NILP (pointer))
24065 {
24066 if (EQ (pointer, Qarrow))
24067 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24068 else if (EQ (pointer, Qhand))
24069 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24070 else if (EQ (pointer, Qtext))
24071 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24072 else if (EQ (pointer, intern ("hdrag")))
24073 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24074 #ifdef HAVE_X_WINDOWS
24075 else if (EQ (pointer, intern ("vdrag")))
24076 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24077 #endif
24078 else if (EQ (pointer, intern ("hourglass")))
24079 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24080 else if (EQ (pointer, Qmodeline))
24081 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24082 else
24083 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24084 }
24085
24086 if (cursor != No_Cursor)
24087 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24088 }
24089
24090 /* Take proper action when mouse has moved to the mode or header line
24091 or marginal area AREA of window W, x-position X and y-position Y.
24092 X is relative to the start of the text display area of W, so the
24093 width of bitmap areas and scroll bars must be subtracted to get a
24094 position relative to the start of the mode line. */
24095
24096 static void
24097 note_mode_line_or_margin_highlight (window, x, y, area)
24098 Lisp_Object window;
24099 int x, y;
24100 enum window_part area;
24101 {
24102 struct window *w = XWINDOW (window);
24103 struct frame *f = XFRAME (w->frame);
24104 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24105 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24106 Lisp_Object pointer = Qnil;
24107 int charpos, dx, dy, width, height;
24108 Lisp_Object string, object = Qnil;
24109 Lisp_Object pos, help;
24110
24111 Lisp_Object mouse_face;
24112 int original_x_pixel = x;
24113 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24114 struct glyph_row *row;
24115
24116 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24117 {
24118 int x0;
24119 struct glyph *end;
24120
24121 string = mode_line_string (w, area, &x, &y, &charpos,
24122 &object, &dx, &dy, &width, &height);
24123
24124 row = (area == ON_MODE_LINE
24125 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24126 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24127
24128 /* Find glyph */
24129 if (row->mode_line_p && row->enabled_p)
24130 {
24131 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24132 end = glyph + row->used[TEXT_AREA];
24133
24134 for (x0 = original_x_pixel;
24135 glyph < end && x0 >= glyph->pixel_width;
24136 ++glyph)
24137 x0 -= glyph->pixel_width;
24138
24139 if (glyph >= end)
24140 glyph = NULL;
24141 }
24142 }
24143 else
24144 {
24145 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
24146 string = marginal_area_string (w, area, &x, &y, &charpos,
24147 &object, &dx, &dy, &width, &height);
24148 }
24149
24150 help = Qnil;
24151
24152 if (IMAGEP (object))
24153 {
24154 Lisp_Object image_map, hotspot;
24155 if ((image_map = Fplist_get (XCDR (object), QCmap),
24156 !NILP (image_map))
24157 && (hotspot = find_hot_spot (image_map, dx, dy),
24158 CONSP (hotspot))
24159 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24160 {
24161 Lisp_Object area_id, plist;
24162
24163 area_id = XCAR (hotspot);
24164 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24165 If so, we could look for mouse-enter, mouse-leave
24166 properties in PLIST (and do something...). */
24167 hotspot = XCDR (hotspot);
24168 if (CONSP (hotspot)
24169 && (plist = XCAR (hotspot), CONSP (plist)))
24170 {
24171 pointer = Fplist_get (plist, Qpointer);
24172 if (NILP (pointer))
24173 pointer = Qhand;
24174 help = Fplist_get (plist, Qhelp_echo);
24175 if (!NILP (help))
24176 {
24177 help_echo_string = help;
24178 /* Is this correct? ++kfs */
24179 XSETWINDOW (help_echo_window, w);
24180 help_echo_object = w->buffer;
24181 help_echo_pos = charpos;
24182 }
24183 }
24184 }
24185 if (NILP (pointer))
24186 pointer = Fplist_get (XCDR (object), QCpointer);
24187 }
24188
24189 if (STRINGP (string))
24190 {
24191 pos = make_number (charpos);
24192 /* If we're on a string with `help-echo' text property, arrange
24193 for the help to be displayed. This is done by setting the
24194 global variable help_echo_string to the help string. */
24195 if (NILP (help))
24196 {
24197 help = Fget_text_property (pos, Qhelp_echo, string);
24198 if (!NILP (help))
24199 {
24200 help_echo_string = help;
24201 XSETWINDOW (help_echo_window, w);
24202 help_echo_object = string;
24203 help_echo_pos = charpos;
24204 }
24205 }
24206
24207 if (NILP (pointer))
24208 pointer = Fget_text_property (pos, Qpointer, string);
24209
24210 /* Change the mouse pointer according to what is under X/Y. */
24211 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24212 {
24213 Lisp_Object map;
24214 map = Fget_text_property (pos, Qlocal_map, string);
24215 if (!KEYMAPP (map))
24216 map = Fget_text_property (pos, Qkeymap, string);
24217 if (!KEYMAPP (map))
24218 cursor = dpyinfo->vertical_scroll_bar_cursor;
24219 }
24220
24221 /* Change the mouse face according to what is under X/Y. */
24222 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24223 if (!NILP (mouse_face)
24224 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24225 && glyph)
24226 {
24227 Lisp_Object b, e;
24228
24229 struct glyph * tmp_glyph;
24230
24231 int gpos;
24232 int gseq_length;
24233 int total_pixel_width;
24234 EMACS_INT ignore;
24235
24236 int vpos, hpos;
24237
24238 b = Fprevious_single_property_change (make_number (charpos + 1),
24239 Qmouse_face, string, Qnil);
24240 if (NILP (b))
24241 b = make_number (0);
24242
24243 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24244 if (NILP (e))
24245 e = make_number (SCHARS (string));
24246
24247 /* Calculate the position(glyph position: GPOS) of GLYPH in
24248 displayed string. GPOS is different from CHARPOS.
24249
24250 CHARPOS is the position of glyph in internal string
24251 object. A mode line string format has structures which
24252 is converted to a flatten by emacs lisp interpreter.
24253 The internal string is an element of the structures.
24254 The displayed string is the flatten string. */
24255 gpos = 0;
24256 if (glyph > row_start_glyph)
24257 {
24258 tmp_glyph = glyph - 1;
24259 while (tmp_glyph >= row_start_glyph
24260 && tmp_glyph->charpos >= XINT (b)
24261 && EQ (tmp_glyph->object, glyph->object))
24262 {
24263 tmp_glyph--;
24264 gpos++;
24265 }
24266 }
24267
24268 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
24269 displayed string holding GLYPH.
24270
24271 GSEQ_LENGTH is different from SCHARS (STRING).
24272 SCHARS (STRING) returns the length of the internal string. */
24273 for (tmp_glyph = glyph, gseq_length = gpos;
24274 tmp_glyph->charpos < XINT (e);
24275 tmp_glyph++, gseq_length++)
24276 {
24277 if (!EQ (tmp_glyph->object, glyph->object))
24278 break;
24279 }
24280
24281 total_pixel_width = 0;
24282 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24283 total_pixel_width += tmp_glyph->pixel_width;
24284
24285 /* Pre calculation of re-rendering position */
24286 vpos = (x - gpos);
24287 hpos = (area == ON_MODE_LINE
24288 ? (w->current_matrix)->nrows - 1
24289 : 0);
24290
24291 /* If the re-rendering position is included in the last
24292 re-rendering area, we should do nothing. */
24293 if ( EQ (window, dpyinfo->mouse_face_window)
24294 && dpyinfo->mouse_face_beg_col <= vpos
24295 && vpos < dpyinfo->mouse_face_end_col
24296 && dpyinfo->mouse_face_beg_row == hpos )
24297 return;
24298
24299 if (clear_mouse_face (dpyinfo))
24300 cursor = No_Cursor;
24301
24302 dpyinfo->mouse_face_beg_col = vpos;
24303 dpyinfo->mouse_face_beg_row = hpos;
24304
24305 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
24306 dpyinfo->mouse_face_beg_y = 0;
24307
24308 dpyinfo->mouse_face_end_col = vpos + gseq_length;
24309 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24310
24311 dpyinfo->mouse_face_end_x = 0;
24312 dpyinfo->mouse_face_end_y = 0;
24313
24314 dpyinfo->mouse_face_past_end = 0;
24315 dpyinfo->mouse_face_window = window;
24316
24317 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24318 charpos,
24319 0, 0, 0, &ignore,
24320 glyph->face_id, 1);
24321 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24322
24323 if (NILP (pointer))
24324 pointer = Qhand;
24325 }
24326 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24327 clear_mouse_face (dpyinfo);
24328 }
24329 define_frame_cursor1 (f, cursor, pointer);
24330 }
24331
24332
24333 /* EXPORT:
24334 Take proper action when the mouse has moved to position X, Y on
24335 frame F as regards highlighting characters that have mouse-face
24336 properties. Also de-highlighting chars where the mouse was before.
24337 X and Y can be negative or out of range. */
24338
24339 void
24340 note_mouse_highlight (f, x, y)
24341 struct frame *f;
24342 int x, y;
24343 {
24344 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24345 enum window_part part;
24346 Lisp_Object window;
24347 struct window *w;
24348 Cursor cursor = No_Cursor;
24349 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24350 struct buffer *b;
24351
24352 /* When a menu is active, don't highlight because this looks odd. */
24353 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24354 if (popup_activated ())
24355 return;
24356 #endif
24357
24358 if (NILP (Vmouse_highlight)
24359 || !f->glyphs_initialized_p
24360 || f->pointer_invisible)
24361 return;
24362
24363 dpyinfo->mouse_face_mouse_x = x;
24364 dpyinfo->mouse_face_mouse_y = y;
24365 dpyinfo->mouse_face_mouse_frame = f;
24366
24367 if (dpyinfo->mouse_face_defer)
24368 return;
24369
24370 if (gc_in_progress)
24371 {
24372 dpyinfo->mouse_face_deferred_gc = 1;
24373 return;
24374 }
24375
24376 /* Which window is that in? */
24377 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24378
24379 /* If we were displaying active text in another window, clear that.
24380 Also clear if we move out of text area in same window. */
24381 if (! EQ (window, dpyinfo->mouse_face_window)
24382 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24383 && !NILP (dpyinfo->mouse_face_window)))
24384 clear_mouse_face (dpyinfo);
24385
24386 /* Not on a window -> return. */
24387 if (!WINDOWP (window))
24388 return;
24389
24390 /* Reset help_echo_string. It will get recomputed below. */
24391 help_echo_string = Qnil;
24392
24393 /* Convert to window-relative pixel coordinates. */
24394 w = XWINDOW (window);
24395 frame_to_window_pixel_xy (w, &x, &y);
24396
24397 /* Handle tool-bar window differently since it doesn't display a
24398 buffer. */
24399 if (EQ (window, f->tool_bar_window))
24400 {
24401 note_tool_bar_highlight (f, x, y);
24402 return;
24403 }
24404
24405 /* Mouse is on the mode, header line or margin? */
24406 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24407 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24408 {
24409 note_mode_line_or_margin_highlight (window, x, y, part);
24410 return;
24411 }
24412
24413 if (part == ON_VERTICAL_BORDER)
24414 {
24415 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24416 help_echo_string = build_string ("drag-mouse-1: resize");
24417 }
24418 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24419 || part == ON_SCROLL_BAR)
24420 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24421 else
24422 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24423
24424 /* Are we in a window whose display is up to date?
24425 And verify the buffer's text has not changed. */
24426 b = XBUFFER (w->buffer);
24427 if (part == ON_TEXT
24428 && EQ (w->window_end_valid, w->buffer)
24429 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24430 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24431 {
24432 int hpos, vpos, i, dx, dy, area;
24433 EMACS_INT pos;
24434 struct glyph *glyph;
24435 Lisp_Object object;
24436 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24437 Lisp_Object *overlay_vec = NULL;
24438 int noverlays;
24439 struct buffer *obuf;
24440 int obegv, ozv, same_region;
24441
24442 /* Find the glyph under X/Y. */
24443 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24444
24445 /* Look for :pointer property on image. */
24446 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24447 {
24448 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24449 if (img != NULL && IMAGEP (img->spec))
24450 {
24451 Lisp_Object image_map, hotspot;
24452 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24453 !NILP (image_map))
24454 && (hotspot = find_hot_spot (image_map,
24455 glyph->slice.x + dx,
24456 glyph->slice.y + dy),
24457 CONSP (hotspot))
24458 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24459 {
24460 Lisp_Object area_id, plist;
24461
24462 area_id = XCAR (hotspot);
24463 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24464 If so, we could look for mouse-enter, mouse-leave
24465 properties in PLIST (and do something...). */
24466 hotspot = XCDR (hotspot);
24467 if (CONSP (hotspot)
24468 && (plist = XCAR (hotspot), CONSP (plist)))
24469 {
24470 pointer = Fplist_get (plist, Qpointer);
24471 if (NILP (pointer))
24472 pointer = Qhand;
24473 help_echo_string = Fplist_get (plist, Qhelp_echo);
24474 if (!NILP (help_echo_string))
24475 {
24476 help_echo_window = window;
24477 help_echo_object = glyph->object;
24478 help_echo_pos = glyph->charpos;
24479 }
24480 }
24481 }
24482 if (NILP (pointer))
24483 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24484 }
24485 }
24486
24487 /* Clear mouse face if X/Y not over text. */
24488 if (glyph == NULL
24489 || area != TEXT_AREA
24490 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24491 {
24492 if (clear_mouse_face (dpyinfo))
24493 cursor = No_Cursor;
24494 if (NILP (pointer))
24495 {
24496 if (area != TEXT_AREA)
24497 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24498 else
24499 pointer = Vvoid_text_area_pointer;
24500 }
24501 goto set_cursor;
24502 }
24503
24504 pos = glyph->charpos;
24505 object = glyph->object;
24506 if (!STRINGP (object) && !BUFFERP (object))
24507 goto set_cursor;
24508
24509 /* If we get an out-of-range value, return now; avoid an error. */
24510 if (BUFFERP (object) && pos > BUF_Z (b))
24511 goto set_cursor;
24512
24513 /* Make the window's buffer temporarily current for
24514 overlays_at and compute_char_face. */
24515 obuf = current_buffer;
24516 current_buffer = b;
24517 obegv = BEGV;
24518 ozv = ZV;
24519 BEGV = BEG;
24520 ZV = Z;
24521
24522 /* Is this char mouse-active or does it have help-echo? */
24523 position = make_number (pos);
24524
24525 if (BUFFERP (object))
24526 {
24527 /* Put all the overlays we want in a vector in overlay_vec. */
24528 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24529 /* Sort overlays into increasing priority order. */
24530 noverlays = sort_overlays (overlay_vec, noverlays, w);
24531 }
24532 else
24533 noverlays = 0;
24534
24535 same_region = (EQ (window, dpyinfo->mouse_face_window)
24536 && vpos >= dpyinfo->mouse_face_beg_row
24537 && vpos <= dpyinfo->mouse_face_end_row
24538 && (vpos > dpyinfo->mouse_face_beg_row
24539 || hpos >= dpyinfo->mouse_face_beg_col)
24540 && (vpos < dpyinfo->mouse_face_end_row
24541 || hpos < dpyinfo->mouse_face_end_col
24542 || dpyinfo->mouse_face_past_end));
24543
24544 if (same_region)
24545 cursor = No_Cursor;
24546
24547 /* Check mouse-face highlighting. */
24548 if (! same_region
24549 /* If there exists an overlay with mouse-face overlapping
24550 the one we are currently highlighting, we have to
24551 check if we enter the overlapping overlay, and then
24552 highlight only that. */
24553 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24554 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24555 {
24556 /* Find the highest priority overlay with a mouse-face. */
24557 overlay = Qnil;
24558 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24559 {
24560 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24561 if (!NILP (mouse_face))
24562 overlay = overlay_vec[i];
24563 }
24564
24565 /* If we're highlighting the same overlay as before, there's
24566 no need to do that again. */
24567 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24568 goto check_help_echo;
24569 dpyinfo->mouse_face_overlay = overlay;
24570
24571 /* Clear the display of the old active region, if any. */
24572 if (clear_mouse_face (dpyinfo))
24573 cursor = No_Cursor;
24574
24575 /* If no overlay applies, get a text property. */
24576 if (NILP (overlay))
24577 mouse_face = Fget_text_property (position, Qmouse_face, object);
24578
24579 /* Next, compute the bounds of the mouse highlighting and
24580 display it. */
24581 if (!NILP (mouse_face) && STRINGP (object))
24582 {
24583 /* The mouse-highlighting comes from a display string
24584 with a mouse-face. */
24585 Lisp_Object b, e;
24586 EMACS_INT ignore;
24587
24588 b = Fprevious_single_property_change
24589 (make_number (pos + 1), Qmouse_face, object, Qnil);
24590 e = Fnext_single_property_change
24591 (position, Qmouse_face, object, Qnil);
24592 if (NILP (b))
24593 b = make_number (0);
24594 if (NILP (e))
24595 e = make_number (SCHARS (object) - 1);
24596
24597 fast_find_string_pos (w, XINT (b), object,
24598 &dpyinfo->mouse_face_beg_col,
24599 &dpyinfo->mouse_face_beg_row,
24600 &dpyinfo->mouse_face_beg_x,
24601 &dpyinfo->mouse_face_beg_y, 0);
24602 fast_find_string_pos (w, XINT (e), object,
24603 &dpyinfo->mouse_face_end_col,
24604 &dpyinfo->mouse_face_end_row,
24605 &dpyinfo->mouse_face_end_x,
24606 &dpyinfo->mouse_face_end_y, 1);
24607 dpyinfo->mouse_face_past_end = 0;
24608 dpyinfo->mouse_face_window = window;
24609 dpyinfo->mouse_face_face_id
24610 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24611 glyph->face_id, 1);
24612 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24613 cursor = No_Cursor;
24614 }
24615 else
24616 {
24617 /* The mouse-highlighting, if any, comes from an overlay
24618 or text property in the buffer. */
24619 Lisp_Object buffer, display_string;
24620
24621 if (STRINGP (object))
24622 {
24623 /* If we are on a display string with no mouse-face,
24624 check if the text under it has one. */
24625 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24626 int start = MATRIX_ROW_START_CHARPOS (r);
24627 pos = string_buffer_position (w, object, start);
24628 if (pos > 0)
24629 {
24630 mouse_face = get_char_property_and_overlay
24631 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24632 buffer = w->buffer;
24633 display_string = object;
24634 }
24635 }
24636 else
24637 {
24638 buffer = object;
24639 display_string = Qnil;
24640 }
24641
24642 if (!NILP (mouse_face))
24643 {
24644 Lisp_Object before, after;
24645 Lisp_Object before_string, after_string;
24646
24647 if (NILP (overlay))
24648 {
24649 /* Handle the text property case. */
24650 before = Fprevious_single_property_change
24651 (make_number (pos + 1), Qmouse_face, buffer,
24652 Fmarker_position (w->start));
24653 after = Fnext_single_property_change
24654 (make_number (pos), Qmouse_face, buffer,
24655 make_number (BUF_Z (XBUFFER (buffer))
24656 - XFASTINT (w->window_end_pos)));
24657 before_string = after_string = Qnil;
24658 }
24659 else
24660 {
24661 /* Handle the overlay case. */
24662 before = Foverlay_start (overlay);
24663 after = Foverlay_end (overlay);
24664 before_string = Foverlay_get (overlay, Qbefore_string);
24665 after_string = Foverlay_get (overlay, Qafter_string);
24666
24667 if (!STRINGP (before_string)) before_string = Qnil;
24668 if (!STRINGP (after_string)) after_string = Qnil;
24669 }
24670
24671 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24672 XFASTINT (before),
24673 XFASTINT (after),
24674 before_string, after_string,
24675 display_string);
24676 cursor = No_Cursor;
24677 }
24678 }
24679 }
24680
24681 check_help_echo:
24682
24683 /* Look for a `help-echo' property. */
24684 if (NILP (help_echo_string)) {
24685 Lisp_Object help, overlay;
24686
24687 /* Check overlays first. */
24688 help = overlay = Qnil;
24689 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24690 {
24691 overlay = overlay_vec[i];
24692 help = Foverlay_get (overlay, Qhelp_echo);
24693 }
24694
24695 if (!NILP (help))
24696 {
24697 help_echo_string = help;
24698 help_echo_window = window;
24699 help_echo_object = overlay;
24700 help_echo_pos = pos;
24701 }
24702 else
24703 {
24704 Lisp_Object object = glyph->object;
24705 int charpos = glyph->charpos;
24706
24707 /* Try text properties. */
24708 if (STRINGP (object)
24709 && charpos >= 0
24710 && charpos < SCHARS (object))
24711 {
24712 help = Fget_text_property (make_number (charpos),
24713 Qhelp_echo, object);
24714 if (NILP (help))
24715 {
24716 /* If the string itself doesn't specify a help-echo,
24717 see if the buffer text ``under'' it does. */
24718 struct glyph_row *r
24719 = MATRIX_ROW (w->current_matrix, vpos);
24720 int start = MATRIX_ROW_START_CHARPOS (r);
24721 EMACS_INT pos = string_buffer_position (w, object, start);
24722 if (pos > 0)
24723 {
24724 help = Fget_char_property (make_number (pos),
24725 Qhelp_echo, w->buffer);
24726 if (!NILP (help))
24727 {
24728 charpos = pos;
24729 object = w->buffer;
24730 }
24731 }
24732 }
24733 }
24734 else if (BUFFERP (object)
24735 && charpos >= BEGV
24736 && charpos < ZV)
24737 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24738 object);
24739
24740 if (!NILP (help))
24741 {
24742 help_echo_string = help;
24743 help_echo_window = window;
24744 help_echo_object = object;
24745 help_echo_pos = charpos;
24746 }
24747 }
24748 }
24749
24750 /* Look for a `pointer' property. */
24751 if (NILP (pointer))
24752 {
24753 /* Check overlays first. */
24754 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24755 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24756
24757 if (NILP (pointer))
24758 {
24759 Lisp_Object object = glyph->object;
24760 int charpos = glyph->charpos;
24761
24762 /* Try text properties. */
24763 if (STRINGP (object)
24764 && charpos >= 0
24765 && charpos < SCHARS (object))
24766 {
24767 pointer = Fget_text_property (make_number (charpos),
24768 Qpointer, object);
24769 if (NILP (pointer))
24770 {
24771 /* If the string itself doesn't specify a pointer,
24772 see if the buffer text ``under'' it does. */
24773 struct glyph_row *r
24774 = MATRIX_ROW (w->current_matrix, vpos);
24775 int start = MATRIX_ROW_START_CHARPOS (r);
24776 EMACS_INT pos = string_buffer_position (w, object,
24777 start);
24778 if (pos > 0)
24779 pointer = Fget_char_property (make_number (pos),
24780 Qpointer, w->buffer);
24781 }
24782 }
24783 else if (BUFFERP (object)
24784 && charpos >= BEGV
24785 && charpos < ZV)
24786 pointer = Fget_text_property (make_number (charpos),
24787 Qpointer, object);
24788 }
24789 }
24790
24791 BEGV = obegv;
24792 ZV = ozv;
24793 current_buffer = obuf;
24794 }
24795
24796 set_cursor:
24797
24798 define_frame_cursor1 (f, cursor, pointer);
24799 }
24800
24801
24802 /* EXPORT for RIF:
24803 Clear any mouse-face on window W. This function is part of the
24804 redisplay interface, and is called from try_window_id and similar
24805 functions to ensure the mouse-highlight is off. */
24806
24807 void
24808 x_clear_window_mouse_face (w)
24809 struct window *w;
24810 {
24811 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24812 Lisp_Object window;
24813
24814 BLOCK_INPUT;
24815 XSETWINDOW (window, w);
24816 if (EQ (window, dpyinfo->mouse_face_window))
24817 clear_mouse_face (dpyinfo);
24818 UNBLOCK_INPUT;
24819 }
24820
24821
24822 /* EXPORT:
24823 Just discard the mouse face information for frame F, if any.
24824 This is used when the size of F is changed. */
24825
24826 void
24827 cancel_mouse_face (f)
24828 struct frame *f;
24829 {
24830 Lisp_Object window;
24831 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24832
24833 window = dpyinfo->mouse_face_window;
24834 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24835 {
24836 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24837 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24838 dpyinfo->mouse_face_window = Qnil;
24839 }
24840 }
24841
24842
24843 #endif /* HAVE_WINDOW_SYSTEM */
24844
24845 \f
24846 /***********************************************************************
24847 Exposure Events
24848 ***********************************************************************/
24849
24850 #ifdef HAVE_WINDOW_SYSTEM
24851
24852 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24853 which intersects rectangle R. R is in window-relative coordinates. */
24854
24855 static void
24856 expose_area (w, row, r, area)
24857 struct window *w;
24858 struct glyph_row *row;
24859 XRectangle *r;
24860 enum glyph_row_area area;
24861 {
24862 struct glyph *first = row->glyphs[area];
24863 struct glyph *end = row->glyphs[area] + row->used[area];
24864 struct glyph *last;
24865 int first_x, start_x, x;
24866
24867 if (area == TEXT_AREA && row->fill_line_p)
24868 /* If row extends face to end of line write the whole line. */
24869 draw_glyphs (w, 0, row, area,
24870 0, row->used[area],
24871 DRAW_NORMAL_TEXT, 0);
24872 else
24873 {
24874 /* Set START_X to the window-relative start position for drawing glyphs of
24875 AREA. The first glyph of the text area can be partially visible.
24876 The first glyphs of other areas cannot. */
24877 start_x = window_box_left_offset (w, area);
24878 x = start_x;
24879 if (area == TEXT_AREA)
24880 x += row->x;
24881
24882 /* Find the first glyph that must be redrawn. */
24883 while (first < end
24884 && x + first->pixel_width < r->x)
24885 {
24886 x += first->pixel_width;
24887 ++first;
24888 }
24889
24890 /* Find the last one. */
24891 last = first;
24892 first_x = x;
24893 while (last < end
24894 && x < r->x + r->width)
24895 {
24896 x += last->pixel_width;
24897 ++last;
24898 }
24899
24900 /* Repaint. */
24901 if (last > first)
24902 draw_glyphs (w, first_x - start_x, row, area,
24903 first - row->glyphs[area], last - row->glyphs[area],
24904 DRAW_NORMAL_TEXT, 0);
24905 }
24906 }
24907
24908
24909 /* Redraw the parts of the glyph row ROW on window W intersecting
24910 rectangle R. R is in window-relative coordinates. Value is
24911 non-zero if mouse-face was overwritten. */
24912
24913 static int
24914 expose_line (w, row, r)
24915 struct window *w;
24916 struct glyph_row *row;
24917 XRectangle *r;
24918 {
24919 xassert (row->enabled_p);
24920
24921 if (row->mode_line_p || w->pseudo_window_p)
24922 draw_glyphs (w, 0, row, TEXT_AREA,
24923 0, row->used[TEXT_AREA],
24924 DRAW_NORMAL_TEXT, 0);
24925 else
24926 {
24927 if (row->used[LEFT_MARGIN_AREA])
24928 expose_area (w, row, r, LEFT_MARGIN_AREA);
24929 if (row->used[TEXT_AREA])
24930 expose_area (w, row, r, TEXT_AREA);
24931 if (row->used[RIGHT_MARGIN_AREA])
24932 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24933 draw_row_fringe_bitmaps (w, row);
24934 }
24935
24936 return row->mouse_face_p;
24937 }
24938
24939
24940 /* Redraw those parts of glyphs rows during expose event handling that
24941 overlap other rows. Redrawing of an exposed line writes over parts
24942 of lines overlapping that exposed line; this function fixes that.
24943
24944 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24945 row in W's current matrix that is exposed and overlaps other rows.
24946 LAST_OVERLAPPING_ROW is the last such row. */
24947
24948 static void
24949 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24950 struct window *w;
24951 struct glyph_row *first_overlapping_row;
24952 struct glyph_row *last_overlapping_row;
24953 XRectangle *r;
24954 {
24955 struct glyph_row *row;
24956
24957 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24958 if (row->overlapping_p)
24959 {
24960 xassert (row->enabled_p && !row->mode_line_p);
24961
24962 row->clip = r;
24963 if (row->used[LEFT_MARGIN_AREA])
24964 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24965
24966 if (row->used[TEXT_AREA])
24967 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24968
24969 if (row->used[RIGHT_MARGIN_AREA])
24970 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24971 row->clip = NULL;
24972 }
24973 }
24974
24975
24976 /* Return non-zero if W's cursor intersects rectangle R. */
24977
24978 static int
24979 phys_cursor_in_rect_p (w, r)
24980 struct window *w;
24981 XRectangle *r;
24982 {
24983 XRectangle cr, result;
24984 struct glyph *cursor_glyph;
24985 struct glyph_row *row;
24986
24987 if (w->phys_cursor.vpos >= 0
24988 && w->phys_cursor.vpos < w->current_matrix->nrows
24989 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24990 row->enabled_p)
24991 && row->cursor_in_fringe_p)
24992 {
24993 /* Cursor is in the fringe. */
24994 cr.x = window_box_right_offset (w,
24995 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24996 ? RIGHT_MARGIN_AREA
24997 : TEXT_AREA));
24998 cr.y = row->y;
24999 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25000 cr.height = row->height;
25001 return x_intersect_rectangles (&cr, r, &result);
25002 }
25003
25004 cursor_glyph = get_phys_cursor_glyph (w);
25005 if (cursor_glyph)
25006 {
25007 /* r is relative to W's box, but w->phys_cursor.x is relative
25008 to left edge of W's TEXT area. Adjust it. */
25009 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25010 cr.y = w->phys_cursor.y;
25011 cr.width = cursor_glyph->pixel_width;
25012 cr.height = w->phys_cursor_height;
25013 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25014 I assume the effect is the same -- and this is portable. */
25015 return x_intersect_rectangles (&cr, r, &result);
25016 }
25017 /* If we don't understand the format, pretend we're not in the hot-spot. */
25018 return 0;
25019 }
25020
25021
25022 /* EXPORT:
25023 Draw a vertical window border to the right of window W if W doesn't
25024 have vertical scroll bars. */
25025
25026 void
25027 x_draw_vertical_border (w)
25028 struct window *w;
25029 {
25030 struct frame *f = XFRAME (WINDOW_FRAME (w));
25031
25032 /* We could do better, if we knew what type of scroll-bar the adjacent
25033 windows (on either side) have... But we don't :-(
25034 However, I think this works ok. ++KFS 2003-04-25 */
25035
25036 /* Redraw borders between horizontally adjacent windows. Don't
25037 do it for frames with vertical scroll bars because either the
25038 right scroll bar of a window, or the left scroll bar of its
25039 neighbor will suffice as a border. */
25040 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25041 return;
25042
25043 if (!WINDOW_RIGHTMOST_P (w)
25044 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25045 {
25046 int x0, x1, y0, y1;
25047
25048 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25049 y1 -= 1;
25050
25051 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25052 x1 -= 1;
25053
25054 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25055 }
25056 else if (!WINDOW_LEFTMOST_P (w)
25057 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25058 {
25059 int x0, x1, y0, y1;
25060
25061 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25062 y1 -= 1;
25063
25064 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25065 x0 -= 1;
25066
25067 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25068 }
25069 }
25070
25071
25072 /* Redraw the part of window W intersection rectangle FR. Pixel
25073 coordinates in FR are frame-relative. Call this function with
25074 input blocked. Value is non-zero if the exposure overwrites
25075 mouse-face. */
25076
25077 static int
25078 expose_window (w, fr)
25079 struct window *w;
25080 XRectangle *fr;
25081 {
25082 struct frame *f = XFRAME (w->frame);
25083 XRectangle wr, r;
25084 int mouse_face_overwritten_p = 0;
25085
25086 /* If window is not yet fully initialized, do nothing. This can
25087 happen when toolkit scroll bars are used and a window is split.
25088 Reconfiguring the scroll bar will generate an expose for a newly
25089 created window. */
25090 if (w->current_matrix == NULL)
25091 return 0;
25092
25093 /* When we're currently updating the window, display and current
25094 matrix usually don't agree. Arrange for a thorough display
25095 later. */
25096 if (w == updated_window)
25097 {
25098 SET_FRAME_GARBAGED (f);
25099 return 0;
25100 }
25101
25102 /* Frame-relative pixel rectangle of W. */
25103 wr.x = WINDOW_LEFT_EDGE_X (w);
25104 wr.y = WINDOW_TOP_EDGE_Y (w);
25105 wr.width = WINDOW_TOTAL_WIDTH (w);
25106 wr.height = WINDOW_TOTAL_HEIGHT (w);
25107
25108 if (x_intersect_rectangles (fr, &wr, &r))
25109 {
25110 int yb = window_text_bottom_y (w);
25111 struct glyph_row *row;
25112 int cursor_cleared_p;
25113 struct glyph_row *first_overlapping_row, *last_overlapping_row;
25114
25115 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
25116 r.x, r.y, r.width, r.height));
25117
25118 /* Convert to window coordinates. */
25119 r.x -= WINDOW_LEFT_EDGE_X (w);
25120 r.y -= WINDOW_TOP_EDGE_Y (w);
25121
25122 /* Turn off the cursor. */
25123 if (!w->pseudo_window_p
25124 && phys_cursor_in_rect_p (w, &r))
25125 {
25126 x_clear_cursor (w);
25127 cursor_cleared_p = 1;
25128 }
25129 else
25130 cursor_cleared_p = 0;
25131
25132 /* Update lines intersecting rectangle R. */
25133 first_overlapping_row = last_overlapping_row = NULL;
25134 for (row = w->current_matrix->rows;
25135 row->enabled_p;
25136 ++row)
25137 {
25138 int y0 = row->y;
25139 int y1 = MATRIX_ROW_BOTTOM_Y (row);
25140
25141 if ((y0 >= r.y && y0 < r.y + r.height)
25142 || (y1 > r.y && y1 < r.y + r.height)
25143 || (r.y >= y0 && r.y < y1)
25144 || (r.y + r.height > y0 && r.y + r.height < y1))
25145 {
25146 /* A header line may be overlapping, but there is no need
25147 to fix overlapping areas for them. KFS 2005-02-12 */
25148 if (row->overlapping_p && !row->mode_line_p)
25149 {
25150 if (first_overlapping_row == NULL)
25151 first_overlapping_row = row;
25152 last_overlapping_row = row;
25153 }
25154
25155 row->clip = fr;
25156 if (expose_line (w, row, &r))
25157 mouse_face_overwritten_p = 1;
25158 row->clip = NULL;
25159 }
25160 else if (row->overlapping_p)
25161 {
25162 /* We must redraw a row overlapping the exposed area. */
25163 if (y0 < r.y
25164 ? y0 + row->phys_height > r.y
25165 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
25166 {
25167 if (first_overlapping_row == NULL)
25168 first_overlapping_row = row;
25169 last_overlapping_row = row;
25170 }
25171 }
25172
25173 if (y1 >= yb)
25174 break;
25175 }
25176
25177 /* Display the mode line if there is one. */
25178 if (WINDOW_WANTS_MODELINE_P (w)
25179 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
25180 row->enabled_p)
25181 && row->y < r.y + r.height)
25182 {
25183 if (expose_line (w, row, &r))
25184 mouse_face_overwritten_p = 1;
25185 }
25186
25187 if (!w->pseudo_window_p)
25188 {
25189 /* Fix the display of overlapping rows. */
25190 if (first_overlapping_row)
25191 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
25192 fr);
25193
25194 /* Draw border between windows. */
25195 x_draw_vertical_border (w);
25196
25197 /* Turn the cursor on again. */
25198 if (cursor_cleared_p)
25199 update_window_cursor (w, 1);
25200 }
25201 }
25202
25203 return mouse_face_overwritten_p;
25204 }
25205
25206
25207
25208 /* Redraw (parts) of all windows in the window tree rooted at W that
25209 intersect R. R contains frame pixel coordinates. Value is
25210 non-zero if the exposure overwrites mouse-face. */
25211
25212 static int
25213 expose_window_tree (w, r)
25214 struct window *w;
25215 XRectangle *r;
25216 {
25217 struct frame *f = XFRAME (w->frame);
25218 int mouse_face_overwritten_p = 0;
25219
25220 while (w && !FRAME_GARBAGED_P (f))
25221 {
25222 if (!NILP (w->hchild))
25223 mouse_face_overwritten_p
25224 |= expose_window_tree (XWINDOW (w->hchild), r);
25225 else if (!NILP (w->vchild))
25226 mouse_face_overwritten_p
25227 |= expose_window_tree (XWINDOW (w->vchild), r);
25228 else
25229 mouse_face_overwritten_p |= expose_window (w, r);
25230
25231 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25232 }
25233
25234 return mouse_face_overwritten_p;
25235 }
25236
25237
25238 /* EXPORT:
25239 Redisplay an exposed area of frame F. X and Y are the upper-left
25240 corner of the exposed rectangle. W and H are width and height of
25241 the exposed area. All are pixel values. W or H zero means redraw
25242 the entire frame. */
25243
25244 void
25245 expose_frame (f, x, y, w, h)
25246 struct frame *f;
25247 int x, y, w, h;
25248 {
25249 XRectangle r;
25250 int mouse_face_overwritten_p = 0;
25251
25252 TRACE ((stderr, "expose_frame "));
25253
25254 /* No need to redraw if frame will be redrawn soon. */
25255 if (FRAME_GARBAGED_P (f))
25256 {
25257 TRACE ((stderr, " garbaged\n"));
25258 return;
25259 }
25260
25261 /* If basic faces haven't been realized yet, there is no point in
25262 trying to redraw anything. This can happen when we get an expose
25263 event while Emacs is starting, e.g. by moving another window. */
25264 if (FRAME_FACE_CACHE (f) == NULL
25265 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25266 {
25267 TRACE ((stderr, " no faces\n"));
25268 return;
25269 }
25270
25271 if (w == 0 || h == 0)
25272 {
25273 r.x = r.y = 0;
25274 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25275 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25276 }
25277 else
25278 {
25279 r.x = x;
25280 r.y = y;
25281 r.width = w;
25282 r.height = h;
25283 }
25284
25285 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25286 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25287
25288 if (WINDOWP (f->tool_bar_window))
25289 mouse_face_overwritten_p
25290 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25291
25292 #ifdef HAVE_X_WINDOWS
25293 #ifndef MSDOS
25294 #ifndef USE_X_TOOLKIT
25295 if (WINDOWP (f->menu_bar_window))
25296 mouse_face_overwritten_p
25297 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25298 #endif /* not USE_X_TOOLKIT */
25299 #endif
25300 #endif
25301
25302 /* Some window managers support a focus-follows-mouse style with
25303 delayed raising of frames. Imagine a partially obscured frame,
25304 and moving the mouse into partially obscured mouse-face on that
25305 frame. The visible part of the mouse-face will be highlighted,
25306 then the WM raises the obscured frame. With at least one WM, KDE
25307 2.1, Emacs is not getting any event for the raising of the frame
25308 (even tried with SubstructureRedirectMask), only Expose events.
25309 These expose events will draw text normally, i.e. not
25310 highlighted. Which means we must redo the highlight here.
25311 Subsume it under ``we love X''. --gerd 2001-08-15 */
25312 /* Included in Windows version because Windows most likely does not
25313 do the right thing if any third party tool offers
25314 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25315 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25316 {
25317 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25318 if (f == dpyinfo->mouse_face_mouse_frame)
25319 {
25320 int x = dpyinfo->mouse_face_mouse_x;
25321 int y = dpyinfo->mouse_face_mouse_y;
25322 clear_mouse_face (dpyinfo);
25323 note_mouse_highlight (f, x, y);
25324 }
25325 }
25326 }
25327
25328
25329 /* EXPORT:
25330 Determine the intersection of two rectangles R1 and R2. Return
25331 the intersection in *RESULT. Value is non-zero if RESULT is not
25332 empty. */
25333
25334 int
25335 x_intersect_rectangles (r1, r2, result)
25336 XRectangle *r1, *r2, *result;
25337 {
25338 XRectangle *left, *right;
25339 XRectangle *upper, *lower;
25340 int intersection_p = 0;
25341
25342 /* Rearrange so that R1 is the left-most rectangle. */
25343 if (r1->x < r2->x)
25344 left = r1, right = r2;
25345 else
25346 left = r2, right = r1;
25347
25348 /* X0 of the intersection is right.x0, if this is inside R1,
25349 otherwise there is no intersection. */
25350 if (right->x <= left->x + left->width)
25351 {
25352 result->x = right->x;
25353
25354 /* The right end of the intersection is the minimum of the
25355 the right ends of left and right. */
25356 result->width = (min (left->x + left->width, right->x + right->width)
25357 - result->x);
25358
25359 /* Same game for Y. */
25360 if (r1->y < r2->y)
25361 upper = r1, lower = r2;
25362 else
25363 upper = r2, lower = r1;
25364
25365 /* The upper end of the intersection is lower.y0, if this is inside
25366 of upper. Otherwise, there is no intersection. */
25367 if (lower->y <= upper->y + upper->height)
25368 {
25369 result->y = lower->y;
25370
25371 /* The lower end of the intersection is the minimum of the lower
25372 ends of upper and lower. */
25373 result->height = (min (lower->y + lower->height,
25374 upper->y + upper->height)
25375 - result->y);
25376 intersection_p = 1;
25377 }
25378 }
25379
25380 return intersection_p;
25381 }
25382
25383 #endif /* HAVE_WINDOW_SYSTEM */
25384
25385 \f
25386 /***********************************************************************
25387 Initialization
25388 ***********************************************************************/
25389
25390 void
25391 syms_of_xdisp ()
25392 {
25393 Vwith_echo_area_save_vector = Qnil;
25394 staticpro (&Vwith_echo_area_save_vector);
25395
25396 Vmessage_stack = Qnil;
25397 staticpro (&Vmessage_stack);
25398
25399 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25400 staticpro (&Qinhibit_redisplay);
25401
25402 message_dolog_marker1 = Fmake_marker ();
25403 staticpro (&message_dolog_marker1);
25404 message_dolog_marker2 = Fmake_marker ();
25405 staticpro (&message_dolog_marker2);
25406 message_dolog_marker3 = Fmake_marker ();
25407 staticpro (&message_dolog_marker3);
25408
25409 #if GLYPH_DEBUG
25410 defsubr (&Sdump_frame_glyph_matrix);
25411 defsubr (&Sdump_glyph_matrix);
25412 defsubr (&Sdump_glyph_row);
25413 defsubr (&Sdump_tool_bar_row);
25414 defsubr (&Strace_redisplay);
25415 defsubr (&Strace_to_stderr);
25416 #endif
25417 #ifdef HAVE_WINDOW_SYSTEM
25418 defsubr (&Stool_bar_lines_needed);
25419 defsubr (&Slookup_image_map);
25420 #endif
25421 defsubr (&Sformat_mode_line);
25422 defsubr (&Sinvisible_p);
25423
25424 staticpro (&Qmenu_bar_update_hook);
25425 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25426
25427 staticpro (&Qoverriding_terminal_local_map);
25428 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25429
25430 staticpro (&Qoverriding_local_map);
25431 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25432
25433 staticpro (&Qwindow_scroll_functions);
25434 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25435
25436 staticpro (&Qwindow_text_change_functions);
25437 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25438
25439 staticpro (&Qredisplay_end_trigger_functions);
25440 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25441
25442 staticpro (&Qinhibit_point_motion_hooks);
25443 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25444
25445 Qeval = intern_c_string ("eval");
25446 staticpro (&Qeval);
25447
25448 QCdata = intern_c_string (":data");
25449 staticpro (&QCdata);
25450 Qdisplay = intern_c_string ("display");
25451 staticpro (&Qdisplay);
25452 Qspace_width = intern_c_string ("space-width");
25453 staticpro (&Qspace_width);
25454 Qraise = intern_c_string ("raise");
25455 staticpro (&Qraise);
25456 Qslice = intern_c_string ("slice");
25457 staticpro (&Qslice);
25458 Qspace = intern_c_string ("space");
25459 staticpro (&Qspace);
25460 Qmargin = intern_c_string ("margin");
25461 staticpro (&Qmargin);
25462 Qpointer = intern_c_string ("pointer");
25463 staticpro (&Qpointer);
25464 Qleft_margin = intern_c_string ("left-margin");
25465 staticpro (&Qleft_margin);
25466 Qright_margin = intern_c_string ("right-margin");
25467 staticpro (&Qright_margin);
25468 Qcenter = intern_c_string ("center");
25469 staticpro (&Qcenter);
25470 Qline_height = intern_c_string ("line-height");
25471 staticpro (&Qline_height);
25472 QCalign_to = intern_c_string (":align-to");
25473 staticpro (&QCalign_to);
25474 QCrelative_width = intern_c_string (":relative-width");
25475 staticpro (&QCrelative_width);
25476 QCrelative_height = intern_c_string (":relative-height");
25477 staticpro (&QCrelative_height);
25478 QCeval = intern_c_string (":eval");
25479 staticpro (&QCeval);
25480 QCpropertize = intern_c_string (":propertize");
25481 staticpro (&QCpropertize);
25482 QCfile = intern_c_string (":file");
25483 staticpro (&QCfile);
25484 Qfontified = intern_c_string ("fontified");
25485 staticpro (&Qfontified);
25486 Qfontification_functions = intern_c_string ("fontification-functions");
25487 staticpro (&Qfontification_functions);
25488 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25489 staticpro (&Qtrailing_whitespace);
25490 Qescape_glyph = intern_c_string ("escape-glyph");
25491 staticpro (&Qescape_glyph);
25492 Qnobreak_space = intern_c_string ("nobreak-space");
25493 staticpro (&Qnobreak_space);
25494 Qimage = intern_c_string ("image");
25495 staticpro (&Qimage);
25496 QCmap = intern_c_string (":map");
25497 staticpro (&QCmap);
25498 QCpointer = intern_c_string (":pointer");
25499 staticpro (&QCpointer);
25500 Qrect = intern_c_string ("rect");
25501 staticpro (&Qrect);
25502 Qcircle = intern_c_string ("circle");
25503 staticpro (&Qcircle);
25504 Qpoly = intern_c_string ("poly");
25505 staticpro (&Qpoly);
25506 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25507 staticpro (&Qmessage_truncate_lines);
25508 Qgrow_only = intern_c_string ("grow-only");
25509 staticpro (&Qgrow_only);
25510 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25511 staticpro (&Qinhibit_menubar_update);
25512 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25513 staticpro (&Qinhibit_eval_during_redisplay);
25514 Qposition = intern_c_string ("position");
25515 staticpro (&Qposition);
25516 Qbuffer_position = intern_c_string ("buffer-position");
25517 staticpro (&Qbuffer_position);
25518 Qobject = intern_c_string ("object");
25519 staticpro (&Qobject);
25520 Qbar = intern_c_string ("bar");
25521 staticpro (&Qbar);
25522 Qhbar = intern_c_string ("hbar");
25523 staticpro (&Qhbar);
25524 Qbox = intern_c_string ("box");
25525 staticpro (&Qbox);
25526 Qhollow = intern_c_string ("hollow");
25527 staticpro (&Qhollow);
25528 Qhand = intern_c_string ("hand");
25529 staticpro (&Qhand);
25530 Qarrow = intern_c_string ("arrow");
25531 staticpro (&Qarrow);
25532 Qtext = intern_c_string ("text");
25533 staticpro (&Qtext);
25534 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25535 staticpro (&Qrisky_local_variable);
25536 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25537 staticpro (&Qinhibit_free_realized_faces);
25538
25539 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25540 Fcons (intern_c_string ("void-variable"), Qnil)),
25541 Qnil);
25542 staticpro (&list_of_error);
25543
25544 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25545 staticpro (&Qlast_arrow_position);
25546 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25547 staticpro (&Qlast_arrow_string);
25548
25549 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25550 staticpro (&Qoverlay_arrow_string);
25551 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25552 staticpro (&Qoverlay_arrow_bitmap);
25553
25554 echo_buffer[0] = echo_buffer[1] = Qnil;
25555 staticpro (&echo_buffer[0]);
25556 staticpro (&echo_buffer[1]);
25557
25558 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25559 staticpro (&echo_area_buffer[0]);
25560 staticpro (&echo_area_buffer[1]);
25561
25562 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25563 staticpro (&Vmessages_buffer_name);
25564
25565 mode_line_proptrans_alist = Qnil;
25566 staticpro (&mode_line_proptrans_alist);
25567 mode_line_string_list = Qnil;
25568 staticpro (&mode_line_string_list);
25569 mode_line_string_face = Qnil;
25570 staticpro (&mode_line_string_face);
25571 mode_line_string_face_prop = Qnil;
25572 staticpro (&mode_line_string_face_prop);
25573 Vmode_line_unwind_vector = Qnil;
25574 staticpro (&Vmode_line_unwind_vector);
25575
25576 help_echo_string = Qnil;
25577 staticpro (&help_echo_string);
25578 help_echo_object = Qnil;
25579 staticpro (&help_echo_object);
25580 help_echo_window = Qnil;
25581 staticpro (&help_echo_window);
25582 previous_help_echo_string = Qnil;
25583 staticpro (&previous_help_echo_string);
25584 help_echo_pos = -1;
25585
25586 Qright_to_left = intern_c_string ("right-to-left");
25587 staticpro (&Qright_to_left);
25588 Qleft_to_right = intern_c_string ("left-to-right");
25589 staticpro (&Qleft_to_right);
25590
25591 #ifdef HAVE_WINDOW_SYSTEM
25592 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25593 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25594 For example, if a block cursor is over a tab, it will be drawn as
25595 wide as that tab on the display. */);
25596 x_stretch_cursor_p = 0;
25597 #endif
25598
25599 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25600 doc: /* *Non-nil means highlight trailing whitespace.
25601 The face used for trailing whitespace is `trailing-whitespace'. */);
25602 Vshow_trailing_whitespace = Qnil;
25603
25604 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25605 doc: /* *Control highlighting of nobreak space and soft hyphen.
25606 A value of t means highlight the character itself (for nobreak space,
25607 use face `nobreak-space').
25608 A value of nil means no highlighting.
25609 Other values mean display the escape glyph followed by an ordinary
25610 space or ordinary hyphen. */);
25611 Vnobreak_char_display = Qt;
25612
25613 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25614 doc: /* *The pointer shape to show in void text areas.
25615 A value of nil means to show the text pointer. Other options are `arrow',
25616 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25617 Vvoid_text_area_pointer = Qarrow;
25618
25619 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25620 doc: /* Non-nil means don't actually do any redisplay.
25621 This is used for internal purposes. */);
25622 Vinhibit_redisplay = Qnil;
25623
25624 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25625 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25626 Vglobal_mode_string = Qnil;
25627
25628 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25629 doc: /* Marker for where to display an arrow on top of the buffer text.
25630 This must be the beginning of a line in order to work.
25631 See also `overlay-arrow-string'. */);
25632 Voverlay_arrow_position = Qnil;
25633
25634 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25635 doc: /* String to display as an arrow in non-window frames.
25636 See also `overlay-arrow-position'. */);
25637 Voverlay_arrow_string = make_pure_c_string ("=>");
25638
25639 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25640 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25641 The symbols on this list are examined during redisplay to determine
25642 where to display overlay arrows. */);
25643 Voverlay_arrow_variable_list
25644 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25645
25646 DEFVAR_INT ("scroll-step", &scroll_step,
25647 doc: /* *The number of lines to try scrolling a window by when point moves out.
25648 If that fails to bring point back on frame, point is centered instead.
25649 If this is zero, point is always centered after it moves off frame.
25650 If you want scrolling to always be a line at a time, you should set
25651 `scroll-conservatively' to a large value rather than set this to 1. */);
25652
25653 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25654 doc: /* *Scroll up to this many lines, to bring point back on screen.
25655 If point moves off-screen, redisplay will scroll by up to
25656 `scroll-conservatively' lines in order to bring point just barely
25657 onto the screen again. If that cannot be done, then redisplay
25658 recenters point as usual.
25659
25660 A value of zero means always recenter point if it moves off screen. */);
25661 scroll_conservatively = 0;
25662
25663 DEFVAR_INT ("scroll-margin", &scroll_margin,
25664 doc: /* *Number of lines of margin at the top and bottom of a window.
25665 Recenter the window whenever point gets within this many lines
25666 of the top or bottom of the window. */);
25667 scroll_margin = 0;
25668
25669 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25670 doc: /* Pixels per inch value for non-window system displays.
25671 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25672 Vdisplay_pixels_per_inch = make_float (72.0);
25673
25674 #if GLYPH_DEBUG
25675 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25676 #endif
25677
25678 DEFVAR_LISP ("truncate-partial-width-windows",
25679 &Vtruncate_partial_width_windows,
25680 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25681 For an integer value, truncate lines in each window narrower than the
25682 full frame width, provided the window width is less than that integer;
25683 otherwise, respect the value of `truncate-lines'.
25684
25685 For any other non-nil value, truncate lines in all windows that do
25686 not span the full frame width.
25687
25688 A value of nil means to respect the value of `truncate-lines'.
25689
25690 If `word-wrap' is enabled, you might want to reduce this. */);
25691 Vtruncate_partial_width_windows = make_number (50);
25692
25693 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25694 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25695 Any other value means to use the appropriate face, `mode-line',
25696 `header-line', or `menu' respectively. */);
25697 mode_line_inverse_video = 1;
25698
25699 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25700 doc: /* *Maximum buffer size for which line number should be displayed.
25701 If the buffer is bigger than this, the line number does not appear
25702 in the mode line. A value of nil means no limit. */);
25703 Vline_number_display_limit = Qnil;
25704
25705 DEFVAR_INT ("line-number-display-limit-width",
25706 &line_number_display_limit_width,
25707 doc: /* *Maximum line width (in characters) for line number display.
25708 If the average length of the lines near point is bigger than this, then the
25709 line number may be omitted from the mode line. */);
25710 line_number_display_limit_width = 200;
25711
25712 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25713 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25714 highlight_nonselected_windows = 0;
25715
25716 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25717 doc: /* Non-nil if more than one frame is visible on this display.
25718 Minibuffer-only frames don't count, but iconified frames do.
25719 This variable is not guaranteed to be accurate except while processing
25720 `frame-title-format' and `icon-title-format'. */);
25721
25722 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25723 doc: /* Template for displaying the title bar of visible frames.
25724 \(Assuming the window manager supports this feature.)
25725
25726 This variable has the same structure as `mode-line-format', except that
25727 the %c and %l constructs are ignored. It is used only on frames for
25728 which no explicit name has been set \(see `modify-frame-parameters'). */);
25729
25730 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25731 doc: /* Template for displaying the title bar of an iconified frame.
25732 \(Assuming the window manager supports this feature.)
25733 This variable has the same structure as `mode-line-format' (which see),
25734 and is used only on frames for which no explicit name has been set
25735 \(see `modify-frame-parameters'). */);
25736 Vicon_title_format
25737 = Vframe_title_format
25738 = pure_cons (intern_c_string ("multiple-frames"),
25739 pure_cons (make_pure_c_string ("%b"),
25740 pure_cons (pure_cons (empty_unibyte_string,
25741 pure_cons (intern_c_string ("invocation-name"),
25742 pure_cons (make_pure_c_string ("@"),
25743 pure_cons (intern_c_string ("system-name"),
25744 Qnil)))),
25745 Qnil)));
25746
25747 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25748 doc: /* Maximum number of lines to keep in the message log buffer.
25749 If nil, disable message logging. If t, log messages but don't truncate
25750 the buffer when it becomes large. */);
25751 Vmessage_log_max = make_number (100);
25752
25753 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25754 doc: /* Functions called before redisplay, if window sizes have changed.
25755 The value should be a list of functions that take one argument.
25756 Just before redisplay, for each frame, if any of its windows have changed
25757 size since the last redisplay, or have been split or deleted,
25758 all the functions in the list are called, with the frame as argument. */);
25759 Vwindow_size_change_functions = Qnil;
25760
25761 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25762 doc: /* List of functions to call before redisplaying a window with scrolling.
25763 Each function is called with two arguments, the window and its new
25764 display-start position. Note that these functions are also called by
25765 `set-window-buffer'. Also note that the value of `window-end' is not
25766 valid when these functions are called. */);
25767 Vwindow_scroll_functions = Qnil;
25768
25769 DEFVAR_LISP ("window-text-change-functions",
25770 &Vwindow_text_change_functions,
25771 doc: /* Functions to call in redisplay when text in the window might change. */);
25772 Vwindow_text_change_functions = Qnil;
25773
25774 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25775 doc: /* Functions called when redisplay of a window reaches the end trigger.
25776 Each function is called with two arguments, the window and the end trigger value.
25777 See `set-window-redisplay-end-trigger'. */);
25778 Vredisplay_end_trigger_functions = Qnil;
25779
25780 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25781 doc: /* *Non-nil means autoselect window with mouse pointer.
25782 If nil, do not autoselect windows.
25783 A positive number means delay autoselection by that many seconds: a
25784 window is autoselected only after the mouse has remained in that
25785 window for the duration of the delay.
25786 A negative number has a similar effect, but causes windows to be
25787 autoselected only after the mouse has stopped moving. \(Because of
25788 the way Emacs compares mouse events, you will occasionally wait twice
25789 that time before the window gets selected.\)
25790 Any other value means to autoselect window instantaneously when the
25791 mouse pointer enters it.
25792
25793 Autoselection selects the minibuffer only if it is active, and never
25794 unselects the minibuffer if it is active.
25795
25796 When customizing this variable make sure that the actual value of
25797 `focus-follows-mouse' matches the behavior of your window manager. */);
25798 Vmouse_autoselect_window = Qnil;
25799
25800 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25801 doc: /* *Non-nil means automatically resize tool-bars.
25802 This dynamically changes the tool-bar's height to the minimum height
25803 that is needed to make all tool-bar items visible.
25804 If value is `grow-only', the tool-bar's height is only increased
25805 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25806 Vauto_resize_tool_bars = Qt;
25807
25808 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25809 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25810 auto_raise_tool_bar_buttons_p = 1;
25811
25812 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25813 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25814 make_cursor_line_fully_visible_p = 1;
25815
25816 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25817 doc: /* *Border below tool-bar in pixels.
25818 If an integer, use it as the height of the border.
25819 If it is one of `internal-border-width' or `border-width', use the
25820 value of the corresponding frame parameter.
25821 Otherwise, no border is added below the tool-bar. */);
25822 Vtool_bar_border = Qinternal_border_width;
25823
25824 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25825 doc: /* *Margin around tool-bar buttons in pixels.
25826 If an integer, use that for both horizontal and vertical margins.
25827 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25828 HORZ specifying the horizontal margin, and VERT specifying the
25829 vertical margin. */);
25830 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25831
25832 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25833 doc: /* *Relief thickness of tool-bar buttons. */);
25834 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25835
25836 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25837 doc: /* List of functions to call to fontify regions of text.
25838 Each function is called with one argument POS. Functions must
25839 fontify a region starting at POS in the current buffer, and give
25840 fontified regions the property `fontified'. */);
25841 Vfontification_functions = Qnil;
25842 Fmake_variable_buffer_local (Qfontification_functions);
25843
25844 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25845 &unibyte_display_via_language_environment,
25846 doc: /* *Non-nil means display unibyte text according to language environment.
25847 Specifically, this means that raw bytes in the range 160-255 decimal
25848 are displayed by converting them to the equivalent multibyte characters
25849 according to the current language environment. As a result, they are
25850 displayed according to the current fontset.
25851
25852 Note that this variable affects only how these bytes are displayed,
25853 but does not change the fact they are interpreted as raw bytes. */);
25854 unibyte_display_via_language_environment = 0;
25855
25856 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25857 doc: /* *Maximum height for resizing mini-windows.
25858 If a float, it specifies a fraction of the mini-window frame's height.
25859 If an integer, it specifies a number of lines. */);
25860 Vmax_mini_window_height = make_float (0.25);
25861
25862 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25863 doc: /* *How to resize mini-windows.
25864 A value of nil means don't automatically resize mini-windows.
25865 A value of t means resize them to fit the text displayed in them.
25866 A value of `grow-only', the default, means let mini-windows grow
25867 only, until their display becomes empty, at which point the windows
25868 go back to their normal size. */);
25869 Vresize_mini_windows = Qgrow_only;
25870
25871 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25872 doc: /* Alist specifying how to blink the cursor off.
25873 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25874 `cursor-type' frame-parameter or variable equals ON-STATE,
25875 comparing using `equal', Emacs uses OFF-STATE to specify
25876 how to blink it off. ON-STATE and OFF-STATE are values for
25877 the `cursor-type' frame parameter.
25878
25879 If a frame's ON-STATE has no entry in this list,
25880 the frame's other specifications determine how to blink the cursor off. */);
25881 Vblink_cursor_alist = Qnil;
25882
25883 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25884 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25885 automatic_hscrolling_p = 1;
25886 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25887 staticpro (&Qauto_hscroll_mode);
25888
25889 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25890 doc: /* *How many columns away from the window edge point is allowed to get
25891 before automatic hscrolling will horizontally scroll the window. */);
25892 hscroll_margin = 5;
25893
25894 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25895 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25896 When point is less than `hscroll-margin' columns from the window
25897 edge, automatic hscrolling will scroll the window by the amount of columns
25898 determined by this variable. If its value is a positive integer, scroll that
25899 many columns. If it's a positive floating-point number, it specifies the
25900 fraction of the window's width to scroll. If it's nil or zero, point will be
25901 centered horizontally after the scroll. Any other value, including negative
25902 numbers, are treated as if the value were zero.
25903
25904 Automatic hscrolling always moves point outside the scroll margin, so if
25905 point was more than scroll step columns inside the margin, the window will
25906 scroll more than the value given by the scroll step.
25907
25908 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25909 and `scroll-right' overrides this variable's effect. */);
25910 Vhscroll_step = make_number (0);
25911
25912 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25913 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25914 Bind this around calls to `message' to let it take effect. */);
25915 message_truncate_lines = 0;
25916
25917 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25918 doc: /* Normal hook run to update the menu bar definitions.
25919 Redisplay runs this hook before it redisplays the menu bar.
25920 This is used to update submenus such as Buffers,
25921 whose contents depend on various data. */);
25922 Vmenu_bar_update_hook = Qnil;
25923
25924 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25925 doc: /* Frame for which we are updating a menu.
25926 The enable predicate for a menu binding should check this variable. */);
25927 Vmenu_updating_frame = Qnil;
25928
25929 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25930 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25931 inhibit_menubar_update = 0;
25932
25933 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25934 doc: /* Prefix prepended to all continuation lines at display time.
25935 The value may be a string, an image, or a stretch-glyph; it is
25936 interpreted in the same way as the value of a `display' text property.
25937
25938 This variable is overridden by any `wrap-prefix' text or overlay
25939 property.
25940
25941 To add a prefix to non-continuation lines, use `line-prefix'. */);
25942 Vwrap_prefix = Qnil;
25943 staticpro (&Qwrap_prefix);
25944 Qwrap_prefix = intern_c_string ("wrap-prefix");
25945 Fmake_variable_buffer_local (Qwrap_prefix);
25946
25947 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25948 doc: /* Prefix prepended to all non-continuation lines at display time.
25949 The value may be a string, an image, or a stretch-glyph; it is
25950 interpreted in the same way as the value of a `display' text property.
25951
25952 This variable is overridden by any `line-prefix' text or overlay
25953 property.
25954
25955 To add a prefix to continuation lines, use `wrap-prefix'. */);
25956 Vline_prefix = Qnil;
25957 staticpro (&Qline_prefix);
25958 Qline_prefix = intern_c_string ("line-prefix");
25959 Fmake_variable_buffer_local (Qline_prefix);
25960
25961 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25962 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25963 inhibit_eval_during_redisplay = 0;
25964
25965 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25966 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25967 inhibit_free_realized_faces = 0;
25968
25969 #if GLYPH_DEBUG
25970 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25971 doc: /* Inhibit try_window_id display optimization. */);
25972 inhibit_try_window_id = 0;
25973
25974 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25975 doc: /* Inhibit try_window_reusing display optimization. */);
25976 inhibit_try_window_reusing = 0;
25977
25978 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25979 doc: /* Inhibit try_cursor_movement display optimization. */);
25980 inhibit_try_cursor_movement = 0;
25981 #endif /* GLYPH_DEBUG */
25982
25983 DEFVAR_INT ("overline-margin", &overline_margin,
25984 doc: /* *Space between overline and text, in pixels.
25985 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25986 margin to the caracter height. */);
25987 overline_margin = 2;
25988
25989 DEFVAR_INT ("underline-minimum-offset",
25990 &underline_minimum_offset,
25991 doc: /* Minimum distance between baseline and underline.
25992 This can improve legibility of underlined text at small font sizes,
25993 particularly when using variable `x-use-underline-position-properties'
25994 with fonts that specify an UNDERLINE_POSITION relatively close to the
25995 baseline. The default value is 1. */);
25996 underline_minimum_offset = 1;
25997
25998 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25999 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
26000 display_hourglass_p = 1;
26001
26002 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
26003 doc: /* *Seconds to wait before displaying an hourglass pointer.
26004 Value must be an integer or float. */);
26005 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26006
26007 hourglass_atimer = NULL;
26008 hourglass_shown_p = 0;
26009 }
26010
26011
26012 /* Initialize this module when Emacs starts. */
26013
26014 void
26015 init_xdisp ()
26016 {
26017 Lisp_Object root_window;
26018 struct window *mini_w;
26019
26020 current_header_line_height = current_mode_line_height = -1;
26021
26022 CHARPOS (this_line_start_pos) = 0;
26023
26024 mini_w = XWINDOW (minibuf_window);
26025 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26026
26027 if (!noninteractive)
26028 {
26029 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26030 int i;
26031
26032 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26033 set_window_height (root_window,
26034 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
26035 0);
26036 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
26037 set_window_height (minibuf_window, 1, 0);
26038
26039 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
26040 mini_w->total_cols = make_number (FRAME_COLS (f));
26041
26042 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
26043 scratch_glyph_row.glyphs[TEXT_AREA + 1]
26044 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
26045
26046 /* The default ellipsis glyphs `...'. */
26047 for (i = 0; i < 3; ++i)
26048 default_invis_vector[i] = make_number ('.');
26049 }
26050
26051 {
26052 /* Allocate the buffer for frame titles.
26053 Also used for `format-mode-line'. */
26054 int size = 100;
26055 mode_line_noprop_buf = (char *) xmalloc (size);
26056 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
26057 mode_line_noprop_ptr = mode_line_noprop_buf;
26058 mode_line_target = MODE_LINE_DISPLAY;
26059 }
26060
26061 help_echo_showing_p = 0;
26062 }
26063
26064 /* Since w32 does not support atimers, it defines its own implementation of
26065 the following three functions in w32fns.c. */
26066 #ifndef WINDOWSNT
26067
26068 /* Platform-independent portion of hourglass implementation. */
26069
26070 /* Return non-zero if houglass timer has been started or hourglass is shown. */
26071 int
26072 hourglass_started ()
26073 {
26074 return hourglass_shown_p || hourglass_atimer != NULL;
26075 }
26076
26077 /* Cancel a currently active hourglass timer, and start a new one. */
26078 void
26079 start_hourglass ()
26080 {
26081 #if defined (HAVE_WINDOW_SYSTEM)
26082 EMACS_TIME delay;
26083 int secs, usecs = 0;
26084
26085 cancel_hourglass ();
26086
26087 if (INTEGERP (Vhourglass_delay)
26088 && XINT (Vhourglass_delay) > 0)
26089 secs = XFASTINT (Vhourglass_delay);
26090 else if (FLOATP (Vhourglass_delay)
26091 && XFLOAT_DATA (Vhourglass_delay) > 0)
26092 {
26093 Lisp_Object tem;
26094 tem = Ftruncate (Vhourglass_delay, Qnil);
26095 secs = XFASTINT (tem);
26096 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
26097 }
26098 else
26099 secs = DEFAULT_HOURGLASS_DELAY;
26100
26101 EMACS_SET_SECS_USECS (delay, secs, usecs);
26102 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
26103 show_hourglass, NULL);
26104 #endif
26105 }
26106
26107
26108 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
26109 shown. */
26110 void
26111 cancel_hourglass ()
26112 {
26113 #if defined (HAVE_WINDOW_SYSTEM)
26114 if (hourglass_atimer)
26115 {
26116 cancel_atimer (hourglass_atimer);
26117 hourglass_atimer = NULL;
26118 }
26119
26120 if (hourglass_shown_p)
26121 hide_hourglass ();
26122 #endif
26123 }
26124 #endif /* ! WINDOWSNT */
26125
26126 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
26127 (do not change this comment) */