Continue work on R2L paragraphs in GUI sessions.
[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 && ((IT)->bidi_it.paragraph_dir == R2L \
333 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
334 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
335 && (IT)->current_x == (IT)->last_visible_x \
336 && (IT)->line_wrap != WORD_WRAP)
337
338 #else /* !HAVE_WINDOW_SYSTEM */
339 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
340 #endif /* HAVE_WINDOW_SYSTEM */
341
342 /* Test if the display element loaded in IT is a space or tab
343 character. This is used to determine word wrapping. */
344
345 #define IT_DISPLAYING_WHITESPACE(it) \
346 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
347
348 /* Non-nil means show the text cursor in void text areas
349 i.e. in blank areas after eol and eob. This used to be
350 the default in 21.3. */
351
352 Lisp_Object Vvoid_text_area_pointer;
353
354 /* Name of the face used to highlight trailing whitespace. */
355
356 Lisp_Object Qtrailing_whitespace;
357
358 /* Name and number of the face used to highlight escape glyphs. */
359
360 Lisp_Object Qescape_glyph;
361
362 /* Name and number of the face used to highlight non-breaking spaces. */
363
364 Lisp_Object Qnobreak_space;
365
366 /* The symbol `image' which is the car of the lists used to represent
367 images in Lisp. */
368
369 Lisp_Object Qimage;
370
371 /* The image map types. */
372 Lisp_Object QCmap, QCpointer;
373 Lisp_Object Qrect, Qcircle, Qpoly;
374
375 /* Non-zero means print newline to stdout before next mini-buffer
376 message. */
377
378 int noninteractive_need_newline;
379
380 /* Non-zero means print newline to message log before next message. */
381
382 static int message_log_need_newline;
383
384 /* Three markers that message_dolog uses.
385 It could allocate them itself, but that causes trouble
386 in handling memory-full errors. */
387 static Lisp_Object message_dolog_marker1;
388 static Lisp_Object message_dolog_marker2;
389 static Lisp_Object message_dolog_marker3;
390 \f
391 /* The buffer position of the first character appearing entirely or
392 partially on the line of the selected window which contains the
393 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
394 redisplay optimization in redisplay_internal. */
395
396 static struct text_pos this_line_start_pos;
397
398 /* Number of characters past the end of the line above, including the
399 terminating newline. */
400
401 static struct text_pos this_line_end_pos;
402
403 /* The vertical positions and the height of this line. */
404
405 static int this_line_vpos;
406 static int this_line_y;
407 static int this_line_pixel_height;
408
409 /* X position at which this display line starts. Usually zero;
410 negative if first character is partially visible. */
411
412 static int this_line_start_x;
413
414 /* Buffer that this_line_.* variables are referring to. */
415
416 static struct buffer *this_line_buffer;
417
418 /* Nonzero means truncate lines in all windows less wide than the
419 frame. */
420
421 Lisp_Object Vtruncate_partial_width_windows;
422
423 /* A flag to control how to display unibyte 8-bit character. */
424
425 int unibyte_display_via_language_environment;
426
427 /* Nonzero means we have more than one non-mini-buffer-only frame.
428 Not guaranteed to be accurate except while parsing
429 frame-title-format. */
430
431 int multiple_frames;
432
433 Lisp_Object Vglobal_mode_string;
434
435
436 /* List of variables (symbols) which hold markers for overlay arrows.
437 The symbols on this list are examined during redisplay to determine
438 where to display overlay arrows. */
439
440 Lisp_Object Voverlay_arrow_variable_list;
441
442 /* Marker for where to display an arrow on top of the buffer text. */
443
444 Lisp_Object Voverlay_arrow_position;
445
446 /* String to display for the arrow. Only used on terminal frames. */
447
448 Lisp_Object Voverlay_arrow_string;
449
450 /* Values of those variables at last redisplay are stored as
451 properties on `overlay-arrow-position' symbol. However, if
452 Voverlay_arrow_position is a marker, last-arrow-position is its
453 numerical position. */
454
455 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
456
457 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
458 properties on a symbol in overlay-arrow-variable-list. */
459
460 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
461
462 /* Like mode-line-format, but for the title bar on a visible frame. */
463
464 Lisp_Object Vframe_title_format;
465
466 /* Like mode-line-format, but for the title bar on an iconified frame. */
467
468 Lisp_Object Vicon_title_format;
469
470 /* List of functions to call when a window's size changes. These
471 functions get one arg, a frame on which one or more windows' sizes
472 have changed. */
473
474 static Lisp_Object Vwindow_size_change_functions;
475
476 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
477
478 /* Nonzero if an overlay arrow has been displayed in this window. */
479
480 static int overlay_arrow_seen;
481
482 /* Nonzero means highlight the region even in nonselected windows. */
483
484 int highlight_nonselected_windows;
485
486 /* If cursor motion alone moves point off frame, try scrolling this
487 many lines up or down if that will bring it back. */
488
489 static EMACS_INT scroll_step;
490
491 /* Nonzero means scroll just far enough to bring point back on the
492 screen, when appropriate. */
493
494 static EMACS_INT scroll_conservatively;
495
496 /* Recenter the window whenever point gets within this many lines of
497 the top or bottom of the window. This value is translated into a
498 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
499 that there is really a fixed pixel height scroll margin. */
500
501 EMACS_INT scroll_margin;
502
503 /* Number of windows showing the buffer of the selected window (or
504 another buffer with the same base buffer). keyboard.c refers to
505 this. */
506
507 int buffer_shared;
508
509 /* Vector containing glyphs for an ellipsis `...'. */
510
511 static Lisp_Object default_invis_vector[3];
512
513 /* Zero means display the mode-line/header-line/menu-bar in the default face
514 (this slightly odd definition is for compatibility with previous versions
515 of emacs), non-zero means display them using their respective faces.
516
517 This variable is deprecated. */
518
519 int mode_line_inverse_video;
520
521 /* Prompt to display in front of the mini-buffer contents. */
522
523 Lisp_Object minibuf_prompt;
524
525 /* Width of current mini-buffer prompt. Only set after display_line
526 of the line that contains the prompt. */
527
528 int minibuf_prompt_width;
529
530 /* This is the window where the echo area message was displayed. It
531 is always a mini-buffer window, but it may not be the same window
532 currently active as a mini-buffer. */
533
534 Lisp_Object echo_area_window;
535
536 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
537 pushes the current message and the value of
538 message_enable_multibyte on the stack, the function restore_message
539 pops the stack and displays MESSAGE again. */
540
541 Lisp_Object Vmessage_stack;
542
543 /* Nonzero means multibyte characters were enabled when the echo area
544 message was specified. */
545
546 int message_enable_multibyte;
547
548 /* Nonzero if we should redraw the mode lines on the next redisplay. */
549
550 int update_mode_lines;
551
552 /* Nonzero if window sizes or contents have changed since last
553 redisplay that finished. */
554
555 int windows_or_buffers_changed;
556
557 /* Nonzero means a frame's cursor type has been changed. */
558
559 int cursor_type_changed;
560
561 /* Nonzero after display_mode_line if %l was used and it displayed a
562 line number. */
563
564 int line_number_displayed;
565
566 /* Maximum buffer size for which to display line numbers. */
567
568 Lisp_Object Vline_number_display_limit;
569
570 /* Line width to consider when repositioning for line number display. */
571
572 static EMACS_INT line_number_display_limit_width;
573
574 /* Number of lines to keep in the message log buffer. t means
575 infinite. nil means don't log at all. */
576
577 Lisp_Object Vmessage_log_max;
578
579 /* The name of the *Messages* buffer, a string. */
580
581 static Lisp_Object Vmessages_buffer_name;
582
583 /* Current, index 0, and last displayed echo area message. Either
584 buffers from echo_buffers, or nil to indicate no message. */
585
586 Lisp_Object echo_area_buffer[2];
587
588 /* The buffers referenced from echo_area_buffer. */
589
590 static Lisp_Object echo_buffer[2];
591
592 /* A vector saved used in with_area_buffer to reduce consing. */
593
594 static Lisp_Object Vwith_echo_area_save_vector;
595
596 /* Non-zero means display_echo_area should display the last echo area
597 message again. Set by redisplay_preserve_echo_area. */
598
599 static int display_last_displayed_message_p;
600
601 /* Nonzero if echo area is being used by print; zero if being used by
602 message. */
603
604 int message_buf_print;
605
606 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
607
608 Lisp_Object Qinhibit_menubar_update;
609 int inhibit_menubar_update;
610
611 /* When evaluating expressions from menu bar items (enable conditions,
612 for instance), this is the frame they are being processed for. */
613
614 Lisp_Object Vmenu_updating_frame;
615
616 /* Maximum height for resizing mini-windows. Either a float
617 specifying a fraction of the available height, or an integer
618 specifying a number of lines. */
619
620 Lisp_Object Vmax_mini_window_height;
621
622 /* Non-zero means messages should be displayed with truncated
623 lines instead of being continued. */
624
625 int message_truncate_lines;
626 Lisp_Object Qmessage_truncate_lines;
627
628 /* Set to 1 in clear_message to make redisplay_internal aware
629 of an emptied echo area. */
630
631 static int message_cleared_p;
632
633 /* How to blink the default frame cursor off. */
634 Lisp_Object Vblink_cursor_alist;
635
636 /* A scratch glyph row with contents used for generating truncation
637 glyphs. Also used in direct_output_for_insert. */
638
639 #define MAX_SCRATCH_GLYPHS 100
640 struct glyph_row scratch_glyph_row;
641 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
642
643 /* Ascent and height of the last line processed by move_it_to. */
644
645 static int last_max_ascent, last_height;
646
647 /* Non-zero if there's a help-echo in the echo area. */
648
649 int help_echo_showing_p;
650
651 /* If >= 0, computed, exact values of mode-line and header-line height
652 to use in the macros CURRENT_MODE_LINE_HEIGHT and
653 CURRENT_HEADER_LINE_HEIGHT. */
654
655 int current_mode_line_height, current_header_line_height;
656
657 /* The maximum distance to look ahead for text properties. Values
658 that are too small let us call compute_char_face and similar
659 functions too often which is expensive. Values that are too large
660 let us call compute_char_face and alike too often because we
661 might not be interested in text properties that far away. */
662
663 #define TEXT_PROP_DISTANCE_LIMIT 100
664
665 #if GLYPH_DEBUG
666
667 /* Variables to turn off display optimizations from Lisp. */
668
669 int inhibit_try_window_id, inhibit_try_window_reusing;
670 int inhibit_try_cursor_movement;
671
672 /* Non-zero means print traces of redisplay if compiled with
673 GLYPH_DEBUG != 0. */
674
675 int trace_redisplay_p;
676
677 #endif /* GLYPH_DEBUG */
678
679 #ifdef DEBUG_TRACE_MOVE
680 /* Non-zero means trace with TRACE_MOVE to stderr. */
681 int trace_move;
682
683 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
684 #else
685 #define TRACE_MOVE(x) (void) 0
686 #endif
687
688 /* Non-zero means automatically scroll windows horizontally to make
689 point visible. */
690
691 int automatic_hscrolling_p;
692 Lisp_Object Qauto_hscroll_mode;
693
694 /* How close to the margin can point get before the window is scrolled
695 horizontally. */
696 EMACS_INT hscroll_margin;
697
698 /* How much to scroll horizontally when point is inside the above margin. */
699 Lisp_Object Vhscroll_step;
700
701 /* The variable `resize-mini-windows'. If nil, don't resize
702 mini-windows. If t, always resize them to fit the text they
703 display. If `grow-only', let mini-windows grow only until they
704 become empty. */
705
706 Lisp_Object Vresize_mini_windows;
707
708 /* Buffer being redisplayed -- for redisplay_window_error. */
709
710 struct buffer *displayed_buffer;
711
712 /* Space between overline and text. */
713
714 EMACS_INT overline_margin;
715
716 /* Require underline to be at least this many screen pixels below baseline
717 This to avoid underline "merging" with the base of letters at small
718 font sizes, particularly when x_use_underline_position_properties is on. */
719
720 EMACS_INT underline_minimum_offset;
721
722 /* Value returned from text property handlers (see below). */
723
724 enum prop_handled
725 {
726 HANDLED_NORMALLY,
727 HANDLED_RECOMPUTE_PROPS,
728 HANDLED_OVERLAY_STRING_CONSUMED,
729 HANDLED_RETURN
730 };
731
732 /* A description of text properties that redisplay is interested
733 in. */
734
735 struct props
736 {
737 /* The name of the property. */
738 Lisp_Object *name;
739
740 /* A unique index for the property. */
741 enum prop_idx idx;
742
743 /* A handler function called to set up iterator IT from the property
744 at IT's current position. Value is used to steer handle_stop. */
745 enum prop_handled (*handler) P_ ((struct it *it));
746 };
747
748 static enum prop_handled handle_face_prop P_ ((struct it *));
749 static enum prop_handled handle_invisible_prop P_ ((struct it *));
750 static enum prop_handled handle_display_prop P_ ((struct it *));
751 static enum prop_handled handle_composition_prop P_ ((struct it *));
752 static enum prop_handled handle_overlay_change P_ ((struct it *));
753 static enum prop_handled handle_fontified_prop P_ ((struct it *));
754
755 /* Properties handled by iterators. */
756
757 static struct props it_props[] =
758 {
759 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
760 /* Handle `face' before `display' because some sub-properties of
761 `display' need to know the face. */
762 {&Qface, FACE_PROP_IDX, handle_face_prop},
763 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
764 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
765 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
766 {NULL, 0, NULL}
767 };
768
769 /* Value is the position described by X. If X is a marker, value is
770 the marker_position of X. Otherwise, value is X. */
771
772 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
773
774 /* Enumeration returned by some move_it_.* functions internally. */
775
776 enum move_it_result
777 {
778 /* Not used. Undefined value. */
779 MOVE_UNDEFINED,
780
781 /* Move ended at the requested buffer position or ZV. */
782 MOVE_POS_MATCH_OR_ZV,
783
784 /* Move ended at the requested X pixel position. */
785 MOVE_X_REACHED,
786
787 /* Move within a line ended at the end of a line that must be
788 continued. */
789 MOVE_LINE_CONTINUED,
790
791 /* Move within a line ended at the end of a line that would
792 be displayed truncated. */
793 MOVE_LINE_TRUNCATED,
794
795 /* Move within a line ended at a line end. */
796 MOVE_NEWLINE_OR_CR
797 };
798
799 /* This counter is used to clear the face cache every once in a while
800 in redisplay_internal. It is incremented for each redisplay.
801 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
802 cleared. */
803
804 #define CLEAR_FACE_CACHE_COUNT 500
805 static int clear_face_cache_count;
806
807 /* Similarly for the image cache. */
808
809 #ifdef HAVE_WINDOW_SYSTEM
810 #define CLEAR_IMAGE_CACHE_COUNT 101
811 static int clear_image_cache_count;
812 #endif
813
814 /* Non-zero while redisplay_internal is in progress. */
815
816 int redisplaying_p;
817
818 /* Non-zero means don't free realized faces. Bound while freeing
819 realized faces is dangerous because glyph matrices might still
820 reference them. */
821
822 int inhibit_free_realized_faces;
823 Lisp_Object Qinhibit_free_realized_faces;
824
825 /* If a string, XTread_socket generates an event to display that string.
826 (The display is done in read_char.) */
827
828 Lisp_Object help_echo_string;
829 Lisp_Object help_echo_window;
830 Lisp_Object help_echo_object;
831 int help_echo_pos;
832
833 /* Temporary variable for XTread_socket. */
834
835 Lisp_Object previous_help_echo_string;
836
837 /* Null glyph slice */
838
839 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
840
841 /* Platform-independent portion of hourglass implementation. */
842
843 /* Non-zero means we're allowed to display a hourglass pointer. */
844 int display_hourglass_p;
845
846 /* Non-zero means an hourglass cursor is currently shown. */
847 int hourglass_shown_p;
848
849 /* If non-null, an asynchronous timer that, when it expires, displays
850 an hourglass cursor on all frames. */
851 struct atimer *hourglass_atimer;
852
853 /* Number of seconds to wait before displaying an hourglass cursor. */
854 Lisp_Object Vhourglass_delay;
855
856 /* Default number of seconds to wait before displaying an hourglass
857 cursor. */
858 #define DEFAULT_HOURGLASS_DELAY 1
859
860 \f
861 /* Function prototypes. */
862
863 static void setup_for_ellipsis P_ ((struct it *, int));
864 static void mark_window_display_accurate_1 P_ ((struct window *, int));
865 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
866 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
867 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
868 static int redisplay_mode_lines P_ ((Lisp_Object, int));
869 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
870
871 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
872
873 static void handle_line_prefix P_ ((struct it *));
874
875 static void pint2str P_ ((char *, int, int));
876 static void pint2hrstr P_ ((char *, int, int));
877 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
878 struct text_pos));
879 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
880 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
881 static void store_mode_line_noprop_char P_ ((char));
882 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
883 static void x_consider_frame_title P_ ((Lisp_Object));
884 static void handle_stop P_ ((struct it *));
885 static void handle_stop_backwards P_ ((struct it *, EMACS_INT));
886 static int tool_bar_lines_needed P_ ((struct frame *, int *));
887 static int single_display_spec_intangible_p P_ ((Lisp_Object));
888 static void ensure_echo_area_buffers P_ ((void));
889 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
890 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
891 static int with_echo_area_buffer P_ ((struct window *, int,
892 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
893 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
894 static void clear_garbaged_frames P_ ((void));
895 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
896 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
897 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
898 static int display_echo_area P_ ((struct window *));
899 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
900 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
901 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
902 static int string_char_and_length P_ ((const unsigned char *, int *));
903 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
904 struct text_pos));
905 static int compute_window_start_on_continuation_line P_ ((struct window *));
906 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
907 static void insert_left_trunc_glyphs P_ ((struct it *));
908 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
909 Lisp_Object));
910 static void extend_face_to_end_of_line P_ ((struct it *));
911 static int append_space_for_newline P_ ((struct it *, int));
912 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
913 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
914 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
915 static int trailing_whitespace_p P_ ((int));
916 static int message_log_check_duplicate P_ ((int, int, int, int));
917 static void push_it P_ ((struct it *));
918 static void pop_it P_ ((struct it *));
919 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
920 static void select_frame_for_redisplay P_ ((Lisp_Object));
921 static void redisplay_internal P_ ((int));
922 static int echo_area_display P_ ((int));
923 static void redisplay_windows P_ ((Lisp_Object));
924 static void redisplay_window P_ ((Lisp_Object, int));
925 static Lisp_Object redisplay_window_error ();
926 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
927 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
928 static int update_menu_bar P_ ((struct frame *, int, int));
929 static int try_window_reusing_current_matrix P_ ((struct window *));
930 static int try_window_id P_ ((struct window *));
931 static int display_line P_ ((struct it *));
932 static int display_mode_lines P_ ((struct window *));
933 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
934 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
935 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
936 static char *decode_mode_spec P_ ((struct window *, int, int, int,
937 Lisp_Object *));
938 static void display_menu_bar P_ ((struct window *));
939 static int display_count_lines P_ ((int, int, int, int, int *));
940 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
941 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
942 static void compute_line_metrics P_ ((struct it *));
943 static void run_redisplay_end_trigger_hook P_ ((struct it *));
944 static int get_overlay_strings P_ ((struct it *, int));
945 static int get_overlay_strings_1 P_ ((struct it *, int, int));
946 static void next_overlay_string P_ ((struct it *));
947 static void reseat P_ ((struct it *, struct text_pos, int));
948 static void reseat_1 P_ ((struct it *, struct text_pos, int));
949 static void back_to_previous_visible_line_start P_ ((struct it *));
950 void reseat_at_previous_visible_line_start P_ ((struct it *));
951 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
952 static int next_element_from_ellipsis P_ ((struct it *));
953 static int next_element_from_display_vector P_ ((struct it *));
954 static int next_element_from_string P_ ((struct it *));
955 static int next_element_from_c_string P_ ((struct it *));
956 static int next_element_from_buffer P_ ((struct it *));
957 static int next_element_from_composition P_ ((struct it *));
958 static int next_element_from_image P_ ((struct it *));
959 static int next_element_from_stretch P_ ((struct it *));
960 static void load_overlay_strings P_ ((struct it *, int));
961 static int init_from_display_pos P_ ((struct it *, struct window *,
962 struct display_pos *));
963 static void reseat_to_string P_ ((struct it *, unsigned char *,
964 Lisp_Object, int, int, int, int));
965 static enum move_it_result
966 move_it_in_display_line_to (struct it *, EMACS_INT, int,
967 enum move_operation_enum);
968 void move_it_vertically_backward P_ ((struct it *, int));
969 static void init_to_row_start P_ ((struct it *, struct window *,
970 struct glyph_row *));
971 static int init_to_row_end P_ ((struct it *, struct window *,
972 struct glyph_row *));
973 static void back_to_previous_line_start P_ ((struct it *));
974 static int forward_to_next_line_start P_ ((struct it *, int *));
975 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
976 Lisp_Object, int));
977 static struct text_pos string_pos P_ ((int, Lisp_Object));
978 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
979 static int number_of_chars P_ ((unsigned char *, int));
980 static void compute_stop_pos P_ ((struct it *));
981 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
982 Lisp_Object));
983 static int face_before_or_after_it_pos P_ ((struct it *, int));
984 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
985 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
986 Lisp_Object, Lisp_Object,
987 struct text_pos *, int));
988 static int underlying_face_id P_ ((struct it *));
989 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
990 struct window *));
991
992 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
993 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
994
995 #ifdef HAVE_WINDOW_SYSTEM
996
997 static void update_tool_bar P_ ((struct frame *, int));
998 static void build_desired_tool_bar_string P_ ((struct frame *f));
999 static int redisplay_tool_bar P_ ((struct frame *));
1000 static void display_tool_bar_line P_ ((struct it *, int));
1001 static void notice_overwritten_cursor P_ ((struct window *,
1002 enum glyph_row_area,
1003 int, int, int, int));
1004 static void append_stretch_glyph P_ ((struct it *, Lisp_Object,
1005 int, int, int));
1006
1007
1008
1009 #endif /* HAVE_WINDOW_SYSTEM */
1010
1011 \f
1012 /***********************************************************************
1013 Window display dimensions
1014 ***********************************************************************/
1015
1016 /* Return the bottom boundary y-position for text lines in window W.
1017 This is the first y position at which a line cannot start.
1018 It is relative to the top of the window.
1019
1020 This is the height of W minus the height of a mode line, if any. */
1021
1022 INLINE int
1023 window_text_bottom_y (w)
1024 struct window *w;
1025 {
1026 int height = WINDOW_TOTAL_HEIGHT (w);
1027
1028 if (WINDOW_WANTS_MODELINE_P (w))
1029 height -= CURRENT_MODE_LINE_HEIGHT (w);
1030 return height;
1031 }
1032
1033 /* Return the pixel width of display area AREA of window W. AREA < 0
1034 means return the total width of W, not including fringes to
1035 the left and right of the window. */
1036
1037 INLINE int
1038 window_box_width (w, area)
1039 struct window *w;
1040 int area;
1041 {
1042 int cols = XFASTINT (w->total_cols);
1043 int pixels = 0;
1044
1045 if (!w->pseudo_window_p)
1046 {
1047 cols -= WINDOW_SCROLL_BAR_COLS (w);
1048
1049 if (area == TEXT_AREA)
1050 {
1051 if (INTEGERP (w->left_margin_cols))
1052 cols -= XFASTINT (w->left_margin_cols);
1053 if (INTEGERP (w->right_margin_cols))
1054 cols -= XFASTINT (w->right_margin_cols);
1055 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1056 }
1057 else if (area == LEFT_MARGIN_AREA)
1058 {
1059 cols = (INTEGERP (w->left_margin_cols)
1060 ? XFASTINT (w->left_margin_cols) : 0);
1061 pixels = 0;
1062 }
1063 else if (area == RIGHT_MARGIN_AREA)
1064 {
1065 cols = (INTEGERP (w->right_margin_cols)
1066 ? XFASTINT (w->right_margin_cols) : 0);
1067 pixels = 0;
1068 }
1069 }
1070
1071 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1072 }
1073
1074
1075 /* Return the pixel height of the display area of window W, not
1076 including mode lines of W, if any. */
1077
1078 INLINE int
1079 window_box_height (w)
1080 struct window *w;
1081 {
1082 struct frame *f = XFRAME (w->frame);
1083 int height = WINDOW_TOTAL_HEIGHT (w);
1084
1085 xassert (height >= 0);
1086
1087 /* Note: the code below that determines the mode-line/header-line
1088 height is essentially the same as that contained in the macro
1089 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1090 the appropriate glyph row has its `mode_line_p' flag set,
1091 and if it doesn't, uses estimate_mode_line_height instead. */
1092
1093 if (WINDOW_WANTS_MODELINE_P (w))
1094 {
1095 struct glyph_row *ml_row
1096 = (w->current_matrix && w->current_matrix->rows
1097 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1098 : 0);
1099 if (ml_row && ml_row->mode_line_p)
1100 height -= ml_row->height;
1101 else
1102 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1103 }
1104
1105 if (WINDOW_WANTS_HEADER_LINE_P (w))
1106 {
1107 struct glyph_row *hl_row
1108 = (w->current_matrix && w->current_matrix->rows
1109 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1110 : 0);
1111 if (hl_row && hl_row->mode_line_p)
1112 height -= hl_row->height;
1113 else
1114 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1115 }
1116
1117 /* With a very small font and a mode-line that's taller than
1118 default, we might end up with a negative height. */
1119 return max (0, height);
1120 }
1121
1122 /* Return the window-relative coordinate of the left edge of display
1123 area AREA of window W. AREA < 0 means return the left edge of the
1124 whole window, to the right of the left fringe of W. */
1125
1126 INLINE int
1127 window_box_left_offset (w, area)
1128 struct window *w;
1129 int area;
1130 {
1131 int x;
1132
1133 if (w->pseudo_window_p)
1134 return 0;
1135
1136 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1137
1138 if (area == TEXT_AREA)
1139 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1140 + window_box_width (w, LEFT_MARGIN_AREA));
1141 else if (area == RIGHT_MARGIN_AREA)
1142 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1143 + window_box_width (w, LEFT_MARGIN_AREA)
1144 + window_box_width (w, TEXT_AREA)
1145 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1146 ? 0
1147 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1148 else if (area == LEFT_MARGIN_AREA
1149 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1150 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1151
1152 return x;
1153 }
1154
1155
1156 /* Return the window-relative coordinate of the right edge of display
1157 area AREA of window W. AREA < 0 means return the left edge of the
1158 whole window, to the left of the right fringe of W. */
1159
1160 INLINE int
1161 window_box_right_offset (w, area)
1162 struct window *w;
1163 int area;
1164 {
1165 return window_box_left_offset (w, area) + window_box_width (w, area);
1166 }
1167
1168 /* Return the frame-relative coordinate of the left edge of display
1169 area AREA of window W. AREA < 0 means return the left edge of the
1170 whole window, to the right of the left fringe of W. */
1171
1172 INLINE int
1173 window_box_left (w, area)
1174 struct window *w;
1175 int area;
1176 {
1177 struct frame *f = XFRAME (w->frame);
1178 int x;
1179
1180 if (w->pseudo_window_p)
1181 return FRAME_INTERNAL_BORDER_WIDTH (f);
1182
1183 x = (WINDOW_LEFT_EDGE_X (w)
1184 + window_box_left_offset (w, area));
1185
1186 return x;
1187 }
1188
1189
1190 /* Return the frame-relative coordinate of the right edge of display
1191 area AREA of window W. AREA < 0 means return the left edge of the
1192 whole window, to the left of the right fringe of W. */
1193
1194 INLINE int
1195 window_box_right (w, area)
1196 struct window *w;
1197 int area;
1198 {
1199 return window_box_left (w, area) + window_box_width (w, area);
1200 }
1201
1202 /* Get the bounding box of the display area AREA of window W, without
1203 mode lines, in frame-relative coordinates. AREA < 0 means the
1204 whole window, not including the left and right fringes of
1205 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1206 coordinates of the upper-left corner of the box. Return in
1207 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1208
1209 INLINE void
1210 window_box (w, area, box_x, box_y, box_width, box_height)
1211 struct window *w;
1212 int area;
1213 int *box_x, *box_y, *box_width, *box_height;
1214 {
1215 if (box_width)
1216 *box_width = window_box_width (w, area);
1217 if (box_height)
1218 *box_height = window_box_height (w);
1219 if (box_x)
1220 *box_x = window_box_left (w, area);
1221 if (box_y)
1222 {
1223 *box_y = WINDOW_TOP_EDGE_Y (w);
1224 if (WINDOW_WANTS_HEADER_LINE_P (w))
1225 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1226 }
1227 }
1228
1229
1230 /* Get the bounding box of the display area AREA of window W, without
1231 mode lines. AREA < 0 means the whole window, not including the
1232 left and right fringe of the window. Return in *TOP_LEFT_X
1233 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1234 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1235 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1236 box. */
1237
1238 INLINE void
1239 window_box_edges (w, area, top_left_x, top_left_y,
1240 bottom_right_x, bottom_right_y)
1241 struct window *w;
1242 int area;
1243 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1244 {
1245 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1246 bottom_right_y);
1247 *bottom_right_x += *top_left_x;
1248 *bottom_right_y += *top_left_y;
1249 }
1250
1251
1252 \f
1253 /***********************************************************************
1254 Utilities
1255 ***********************************************************************/
1256
1257 /* Return the bottom y-position of the line the iterator IT is in.
1258 This can modify IT's settings. */
1259
1260 int
1261 line_bottom_y (it)
1262 struct it *it;
1263 {
1264 int line_height = it->max_ascent + it->max_descent;
1265 int line_top_y = it->current_y;
1266
1267 if (line_height == 0)
1268 {
1269 if (last_height)
1270 line_height = last_height;
1271 else if (IT_CHARPOS (*it) < ZV)
1272 {
1273 move_it_by_lines (it, 1, 1);
1274 line_height = (it->max_ascent || it->max_descent
1275 ? it->max_ascent + it->max_descent
1276 : last_height);
1277 }
1278 else
1279 {
1280 struct glyph_row *row = it->glyph_row;
1281
1282 /* Use the default character height. */
1283 it->glyph_row = NULL;
1284 it->what = IT_CHARACTER;
1285 it->c = ' ';
1286 it->len = 1;
1287 PRODUCE_GLYPHS (it);
1288 line_height = it->ascent + it->descent;
1289 it->glyph_row = row;
1290 }
1291 }
1292
1293 return line_top_y + line_height;
1294 }
1295
1296
1297 /* Return 1 if position CHARPOS is visible in window W.
1298 CHARPOS < 0 means return info about WINDOW_END position.
1299 If visible, set *X and *Y to pixel coordinates of top left corner.
1300 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1301 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1302
1303 int
1304 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1305 struct window *w;
1306 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1307 {
1308 struct it it;
1309 struct text_pos top;
1310 int visible_p = 0;
1311 struct buffer *old_buffer = NULL;
1312
1313 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1314 return visible_p;
1315
1316 if (XBUFFER (w->buffer) != current_buffer)
1317 {
1318 old_buffer = current_buffer;
1319 set_buffer_internal_1 (XBUFFER (w->buffer));
1320 }
1321
1322 SET_TEXT_POS_FROM_MARKER (top, w->start);
1323
1324 /* Compute exact mode line heights. */
1325 if (WINDOW_WANTS_MODELINE_P (w))
1326 current_mode_line_height
1327 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1328 current_buffer->mode_line_format);
1329
1330 if (WINDOW_WANTS_HEADER_LINE_P (w))
1331 current_header_line_height
1332 = display_mode_line (w, HEADER_LINE_FACE_ID,
1333 current_buffer->header_line_format);
1334
1335 start_display (&it, w, top);
1336 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1337 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1338
1339 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1340 {
1341 /* We have reached CHARPOS, or passed it. How the call to
1342 move_it_to can overshoot: (i) If CHARPOS is on invisible
1343 text, move_it_to stops at the end of the invisible text,
1344 after CHARPOS. (ii) If CHARPOS is in a display vector,
1345 move_it_to stops on its last glyph. */
1346 int top_x = it.current_x;
1347 int top_y = it.current_y;
1348 enum it_method it_method = it.method;
1349 /* Calling line_bottom_y may change it.method, it.position, etc. */
1350 int bottom_y = (last_height = 0, line_bottom_y (&it));
1351 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1352
1353 if (top_y < window_top_y)
1354 visible_p = bottom_y > window_top_y;
1355 else if (top_y < it.last_visible_y)
1356 visible_p = 1;
1357 if (visible_p)
1358 {
1359 if (it_method == GET_FROM_DISPLAY_VECTOR)
1360 {
1361 /* We stopped on the last glyph of a display vector.
1362 Try and recompute. Hack alert! */
1363 if (charpos < 2 || top.charpos >= charpos)
1364 top_x = it.glyph_row->x;
1365 else
1366 {
1367 struct it it2;
1368 start_display (&it2, w, top);
1369 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1370 get_next_display_element (&it2);
1371 PRODUCE_GLYPHS (&it2);
1372 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1373 || it2.current_x > it2.last_visible_x)
1374 top_x = it.glyph_row->x;
1375 else
1376 {
1377 top_x = it2.current_x;
1378 top_y = it2.current_y;
1379 }
1380 }
1381 }
1382
1383 *x = top_x;
1384 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1385 *rtop = max (0, window_top_y - top_y);
1386 *rbot = max (0, bottom_y - it.last_visible_y);
1387 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1388 - max (top_y, window_top_y)));
1389 *vpos = it.vpos;
1390 }
1391 }
1392 else
1393 {
1394 struct it it2;
1395
1396 it2 = it;
1397 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1398 move_it_by_lines (&it, 1, 0);
1399 if (charpos < IT_CHARPOS (it)
1400 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1401 {
1402 visible_p = 1;
1403 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1404 *x = it2.current_x;
1405 *y = it2.current_y + it2.max_ascent - it2.ascent;
1406 *rtop = max (0, -it2.current_y);
1407 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1408 - it.last_visible_y));
1409 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1410 it.last_visible_y)
1411 - max (it2.current_y,
1412 WINDOW_HEADER_LINE_HEIGHT (w))));
1413 *vpos = it2.vpos;
1414 }
1415 }
1416
1417 if (old_buffer)
1418 set_buffer_internal_1 (old_buffer);
1419
1420 current_header_line_height = current_mode_line_height = -1;
1421
1422 if (visible_p && XFASTINT (w->hscroll) > 0)
1423 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1424
1425 #if 0
1426 /* Debugging code. */
1427 if (visible_p)
1428 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1429 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1430 else
1431 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1432 #endif
1433
1434 return visible_p;
1435 }
1436
1437
1438 /* Return the next character from STR which is MAXLEN bytes long.
1439 Return in *LEN the length of the character. This is like
1440 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1441 we find one, we return a `?', but with the length of the invalid
1442 character. */
1443
1444 static INLINE int
1445 string_char_and_length (str, len)
1446 const unsigned char *str;
1447 int *len;
1448 {
1449 int c;
1450
1451 c = STRING_CHAR_AND_LENGTH (str, *len);
1452 if (!CHAR_VALID_P (c, 1))
1453 /* We may not change the length here because other places in Emacs
1454 don't use this function, i.e. they silently accept invalid
1455 characters. */
1456 c = '?';
1457
1458 return c;
1459 }
1460
1461
1462
1463 /* Given a position POS containing a valid character and byte position
1464 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1465
1466 static struct text_pos
1467 string_pos_nchars_ahead (pos, string, nchars)
1468 struct text_pos pos;
1469 Lisp_Object string;
1470 int nchars;
1471 {
1472 xassert (STRINGP (string) && nchars >= 0);
1473
1474 if (STRING_MULTIBYTE (string))
1475 {
1476 int rest = SBYTES (string) - BYTEPOS (pos);
1477 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1478 int len;
1479
1480 while (nchars--)
1481 {
1482 string_char_and_length (p, &len);
1483 p += len, rest -= len;
1484 xassert (rest >= 0);
1485 CHARPOS (pos) += 1;
1486 BYTEPOS (pos) += len;
1487 }
1488 }
1489 else
1490 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1491
1492 return pos;
1493 }
1494
1495
1496 /* Value is the text position, i.e. character and byte position,
1497 for character position CHARPOS in STRING. */
1498
1499 static INLINE struct text_pos
1500 string_pos (charpos, string)
1501 int charpos;
1502 Lisp_Object string;
1503 {
1504 struct text_pos pos;
1505 xassert (STRINGP (string));
1506 xassert (charpos >= 0);
1507 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1508 return pos;
1509 }
1510
1511
1512 /* Value is a text position, i.e. character and byte position, for
1513 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1514 means recognize multibyte characters. */
1515
1516 static struct text_pos
1517 c_string_pos (charpos, s, multibyte_p)
1518 int charpos;
1519 unsigned char *s;
1520 int multibyte_p;
1521 {
1522 struct text_pos pos;
1523
1524 xassert (s != NULL);
1525 xassert (charpos >= 0);
1526
1527 if (multibyte_p)
1528 {
1529 int rest = strlen (s), len;
1530
1531 SET_TEXT_POS (pos, 0, 0);
1532 while (charpos--)
1533 {
1534 string_char_and_length (s, &len);
1535 s += len, rest -= len;
1536 xassert (rest >= 0);
1537 CHARPOS (pos) += 1;
1538 BYTEPOS (pos) += len;
1539 }
1540 }
1541 else
1542 SET_TEXT_POS (pos, charpos, charpos);
1543
1544 return pos;
1545 }
1546
1547
1548 /* Value is the number of characters in C string S. MULTIBYTE_P
1549 non-zero means recognize multibyte characters. */
1550
1551 static int
1552 number_of_chars (s, multibyte_p)
1553 unsigned char *s;
1554 int multibyte_p;
1555 {
1556 int nchars;
1557
1558 if (multibyte_p)
1559 {
1560 int rest = strlen (s), len;
1561 unsigned char *p = (unsigned char *) s;
1562
1563 for (nchars = 0; rest > 0; ++nchars)
1564 {
1565 string_char_and_length (p, &len);
1566 rest -= len, p += len;
1567 }
1568 }
1569 else
1570 nchars = strlen (s);
1571
1572 return nchars;
1573 }
1574
1575
1576 /* Compute byte position NEWPOS->bytepos corresponding to
1577 NEWPOS->charpos. POS is a known position in string STRING.
1578 NEWPOS->charpos must be >= POS.charpos. */
1579
1580 static void
1581 compute_string_pos (newpos, pos, string)
1582 struct text_pos *newpos, pos;
1583 Lisp_Object string;
1584 {
1585 xassert (STRINGP (string));
1586 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1587
1588 if (STRING_MULTIBYTE (string))
1589 *newpos = string_pos_nchars_ahead (pos, string,
1590 CHARPOS (*newpos) - CHARPOS (pos));
1591 else
1592 BYTEPOS (*newpos) = CHARPOS (*newpos);
1593 }
1594
1595 /* EXPORT:
1596 Return an estimation of the pixel height of mode or header lines on
1597 frame F. FACE_ID specifies what line's height to estimate. */
1598
1599 int
1600 estimate_mode_line_height (f, face_id)
1601 struct frame *f;
1602 enum face_id face_id;
1603 {
1604 #ifdef HAVE_WINDOW_SYSTEM
1605 if (FRAME_WINDOW_P (f))
1606 {
1607 int height = FONT_HEIGHT (FRAME_FONT (f));
1608
1609 /* This function is called so early when Emacs starts that the face
1610 cache and mode line face are not yet initialized. */
1611 if (FRAME_FACE_CACHE (f))
1612 {
1613 struct face *face = FACE_FROM_ID (f, face_id);
1614 if (face)
1615 {
1616 if (face->font)
1617 height = FONT_HEIGHT (face->font);
1618 if (face->box_line_width > 0)
1619 height += 2 * face->box_line_width;
1620 }
1621 }
1622
1623 return height;
1624 }
1625 #endif
1626
1627 return 1;
1628 }
1629
1630 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1631 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1632 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1633 not force the value into range. */
1634
1635 void
1636 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1637 FRAME_PTR f;
1638 register int pix_x, pix_y;
1639 int *x, *y;
1640 NativeRectangle *bounds;
1641 int noclip;
1642 {
1643
1644 #ifdef HAVE_WINDOW_SYSTEM
1645 if (FRAME_WINDOW_P (f))
1646 {
1647 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1648 even for negative values. */
1649 if (pix_x < 0)
1650 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1651 if (pix_y < 0)
1652 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1653
1654 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1655 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1656
1657 if (bounds)
1658 STORE_NATIVE_RECT (*bounds,
1659 FRAME_COL_TO_PIXEL_X (f, pix_x),
1660 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1661 FRAME_COLUMN_WIDTH (f) - 1,
1662 FRAME_LINE_HEIGHT (f) - 1);
1663
1664 if (!noclip)
1665 {
1666 if (pix_x < 0)
1667 pix_x = 0;
1668 else if (pix_x > FRAME_TOTAL_COLS (f))
1669 pix_x = FRAME_TOTAL_COLS (f);
1670
1671 if (pix_y < 0)
1672 pix_y = 0;
1673 else if (pix_y > FRAME_LINES (f))
1674 pix_y = FRAME_LINES (f);
1675 }
1676 }
1677 #endif
1678
1679 *x = pix_x;
1680 *y = pix_y;
1681 }
1682
1683
1684 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1685 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1686 can't tell the positions because W's display is not up to date,
1687 return 0. */
1688
1689 int
1690 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1691 struct window *w;
1692 int hpos, vpos;
1693 int *frame_x, *frame_y;
1694 {
1695 #ifdef HAVE_WINDOW_SYSTEM
1696 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1697 {
1698 int success_p;
1699
1700 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1701 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1702
1703 if (display_completed)
1704 {
1705 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1706 struct glyph *glyph = row->glyphs[TEXT_AREA];
1707 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1708
1709 hpos = row->x;
1710 vpos = row->y;
1711 while (glyph < end)
1712 {
1713 hpos += glyph->pixel_width;
1714 ++glyph;
1715 }
1716
1717 /* If first glyph is partially visible, its first visible position is still 0. */
1718 if (hpos < 0)
1719 hpos = 0;
1720
1721 success_p = 1;
1722 }
1723 else
1724 {
1725 hpos = vpos = 0;
1726 success_p = 0;
1727 }
1728
1729 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1730 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1731 return success_p;
1732 }
1733 #endif
1734
1735 *frame_x = hpos;
1736 *frame_y = vpos;
1737 return 1;
1738 }
1739
1740
1741 #ifdef HAVE_WINDOW_SYSTEM
1742
1743 /* Find the glyph under window-relative coordinates X/Y in window W.
1744 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1745 strings. Return in *HPOS and *VPOS the row and column number of
1746 the glyph found. Return in *AREA the glyph area containing X.
1747 Value is a pointer to the glyph found or null if X/Y is not on
1748 text, or we can't tell because W's current matrix is not up to
1749 date. */
1750
1751 static
1752 struct glyph *
1753 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1754 struct window *w;
1755 int x, y;
1756 int *hpos, *vpos, *dx, *dy, *area;
1757 {
1758 struct glyph *glyph, *end;
1759 struct glyph_row *row = NULL;
1760 int x0, i;
1761
1762 /* Find row containing Y. Give up if some row is not enabled. */
1763 for (i = 0; i < w->current_matrix->nrows; ++i)
1764 {
1765 row = MATRIX_ROW (w->current_matrix, i);
1766 if (!row->enabled_p)
1767 return NULL;
1768 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1769 break;
1770 }
1771
1772 *vpos = i;
1773 *hpos = 0;
1774
1775 /* Give up if Y is not in the window. */
1776 if (i == w->current_matrix->nrows)
1777 return NULL;
1778
1779 /* Get the glyph area containing X. */
1780 if (w->pseudo_window_p)
1781 {
1782 *area = TEXT_AREA;
1783 x0 = 0;
1784 }
1785 else
1786 {
1787 if (x < window_box_left_offset (w, TEXT_AREA))
1788 {
1789 *area = LEFT_MARGIN_AREA;
1790 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1791 }
1792 else if (x < window_box_right_offset (w, TEXT_AREA))
1793 {
1794 *area = TEXT_AREA;
1795 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1796 }
1797 else
1798 {
1799 *area = RIGHT_MARGIN_AREA;
1800 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1801 }
1802 }
1803
1804 /* Find glyph containing X. */
1805 glyph = row->glyphs[*area];
1806 end = glyph + row->used[*area];
1807 x -= x0;
1808 while (glyph < end && x >= glyph->pixel_width)
1809 {
1810 x -= glyph->pixel_width;
1811 ++glyph;
1812 }
1813
1814 if (glyph == end)
1815 return NULL;
1816
1817 if (dx)
1818 {
1819 *dx = x;
1820 *dy = y - (row->y + row->ascent - glyph->ascent);
1821 }
1822
1823 *hpos = glyph - row->glyphs[*area];
1824 return glyph;
1825 }
1826
1827
1828 /* EXPORT:
1829 Convert frame-relative x/y to coordinates relative to window W.
1830 Takes pseudo-windows into account. */
1831
1832 void
1833 frame_to_window_pixel_xy (w, x, y)
1834 struct window *w;
1835 int *x, *y;
1836 {
1837 if (w->pseudo_window_p)
1838 {
1839 /* A pseudo-window is always full-width, and starts at the
1840 left edge of the frame, plus a frame border. */
1841 struct frame *f = XFRAME (w->frame);
1842 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1843 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1844 }
1845 else
1846 {
1847 *x -= WINDOW_LEFT_EDGE_X (w);
1848 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1849 }
1850 }
1851
1852 /* EXPORT:
1853 Return in RECTS[] at most N clipping rectangles for glyph string S.
1854 Return the number of stored rectangles. */
1855
1856 int
1857 get_glyph_string_clip_rects (s, rects, n)
1858 struct glyph_string *s;
1859 NativeRectangle *rects;
1860 int n;
1861 {
1862 XRectangle r;
1863
1864 if (n <= 0)
1865 return 0;
1866
1867 if (s->row->full_width_p)
1868 {
1869 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1870 r.x = WINDOW_LEFT_EDGE_X (s->w);
1871 r.width = WINDOW_TOTAL_WIDTH (s->w);
1872
1873 /* Unless displaying a mode or menu bar line, which are always
1874 fully visible, clip to the visible part of the row. */
1875 if (s->w->pseudo_window_p)
1876 r.height = s->row->visible_height;
1877 else
1878 r.height = s->height;
1879 }
1880 else
1881 {
1882 /* This is a text line that may be partially visible. */
1883 r.x = window_box_left (s->w, s->area);
1884 r.width = window_box_width (s->w, s->area);
1885 r.height = s->row->visible_height;
1886 }
1887
1888 if (s->clip_head)
1889 if (r.x < s->clip_head->x)
1890 {
1891 if (r.width >= s->clip_head->x - r.x)
1892 r.width -= s->clip_head->x - r.x;
1893 else
1894 r.width = 0;
1895 r.x = s->clip_head->x;
1896 }
1897 if (s->clip_tail)
1898 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1899 {
1900 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1901 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1902 else
1903 r.width = 0;
1904 }
1905
1906 /* If S draws overlapping rows, it's sufficient to use the top and
1907 bottom of the window for clipping because this glyph string
1908 intentionally draws over other lines. */
1909 if (s->for_overlaps)
1910 {
1911 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1912 r.height = window_text_bottom_y (s->w) - r.y;
1913
1914 /* Alas, the above simple strategy does not work for the
1915 environments with anti-aliased text: if the same text is
1916 drawn onto the same place multiple times, it gets thicker.
1917 If the overlap we are processing is for the erased cursor, we
1918 take the intersection with the rectagle of the cursor. */
1919 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1920 {
1921 XRectangle rc, r_save = r;
1922
1923 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1924 rc.y = s->w->phys_cursor.y;
1925 rc.width = s->w->phys_cursor_width;
1926 rc.height = s->w->phys_cursor_height;
1927
1928 x_intersect_rectangles (&r_save, &rc, &r);
1929 }
1930 }
1931 else
1932 {
1933 /* Don't use S->y for clipping because it doesn't take partially
1934 visible lines into account. For example, it can be negative for
1935 partially visible lines at the top of a window. */
1936 if (!s->row->full_width_p
1937 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1938 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1939 else
1940 r.y = max (0, s->row->y);
1941 }
1942
1943 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1944
1945 /* If drawing the cursor, don't let glyph draw outside its
1946 advertised boundaries. Cleartype does this under some circumstances. */
1947 if (s->hl == DRAW_CURSOR)
1948 {
1949 struct glyph *glyph = s->first_glyph;
1950 int height, max_y;
1951
1952 if (s->x > r.x)
1953 {
1954 r.width -= s->x - r.x;
1955 r.x = s->x;
1956 }
1957 r.width = min (r.width, glyph->pixel_width);
1958
1959 /* If r.y is below window bottom, ensure that we still see a cursor. */
1960 height = min (glyph->ascent + glyph->descent,
1961 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1962 max_y = window_text_bottom_y (s->w) - height;
1963 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1964 if (s->ybase - glyph->ascent > max_y)
1965 {
1966 r.y = max_y;
1967 r.height = height;
1968 }
1969 else
1970 {
1971 /* Don't draw cursor glyph taller than our actual glyph. */
1972 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1973 if (height < r.height)
1974 {
1975 max_y = r.y + r.height;
1976 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1977 r.height = min (max_y - r.y, height);
1978 }
1979 }
1980 }
1981
1982 if (s->row->clip)
1983 {
1984 XRectangle r_save = r;
1985
1986 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1987 r.width = 0;
1988 }
1989
1990 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1991 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1992 {
1993 #ifdef CONVERT_FROM_XRECT
1994 CONVERT_FROM_XRECT (r, *rects);
1995 #else
1996 *rects = r;
1997 #endif
1998 return 1;
1999 }
2000 else
2001 {
2002 /* If we are processing overlapping and allowed to return
2003 multiple clipping rectangles, we exclude the row of the glyph
2004 string from the clipping rectangle. This is to avoid drawing
2005 the same text on the environment with anti-aliasing. */
2006 #ifdef CONVERT_FROM_XRECT
2007 XRectangle rs[2];
2008 #else
2009 XRectangle *rs = rects;
2010 #endif
2011 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2012
2013 if (s->for_overlaps & OVERLAPS_PRED)
2014 {
2015 rs[i] = r;
2016 if (r.y + r.height > row_y)
2017 {
2018 if (r.y < row_y)
2019 rs[i].height = row_y - r.y;
2020 else
2021 rs[i].height = 0;
2022 }
2023 i++;
2024 }
2025 if (s->for_overlaps & OVERLAPS_SUCC)
2026 {
2027 rs[i] = r;
2028 if (r.y < row_y + s->row->visible_height)
2029 {
2030 if (r.y + r.height > row_y + s->row->visible_height)
2031 {
2032 rs[i].y = row_y + s->row->visible_height;
2033 rs[i].height = r.y + r.height - rs[i].y;
2034 }
2035 else
2036 rs[i].height = 0;
2037 }
2038 i++;
2039 }
2040
2041 n = i;
2042 #ifdef CONVERT_FROM_XRECT
2043 for (i = 0; i < n; i++)
2044 CONVERT_FROM_XRECT (rs[i], rects[i]);
2045 #endif
2046 return n;
2047 }
2048 }
2049
2050 /* EXPORT:
2051 Return in *NR the clipping rectangle for glyph string S. */
2052
2053 void
2054 get_glyph_string_clip_rect (s, nr)
2055 struct glyph_string *s;
2056 NativeRectangle *nr;
2057 {
2058 get_glyph_string_clip_rects (s, nr, 1);
2059 }
2060
2061
2062 /* EXPORT:
2063 Return the position and height of the phys cursor in window W.
2064 Set w->phys_cursor_width to width of phys cursor.
2065 */
2066
2067 void
2068 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2069 struct window *w;
2070 struct glyph_row *row;
2071 struct glyph *glyph;
2072 int *xp, *yp, *heightp;
2073 {
2074 struct frame *f = XFRAME (WINDOW_FRAME (w));
2075 int x, y, wd, h, h0, y0;
2076
2077 /* Compute the width of the rectangle to draw. If on a stretch
2078 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2079 rectangle as wide as the glyph, but use a canonical character
2080 width instead. */
2081 wd = glyph->pixel_width - 1;
2082 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2083 wd++; /* Why? */
2084 #endif
2085
2086 x = w->phys_cursor.x;
2087 if (x < 0)
2088 {
2089 wd += x;
2090 x = 0;
2091 }
2092
2093 if (glyph->type == STRETCH_GLYPH
2094 && !x_stretch_cursor_p)
2095 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2096 w->phys_cursor_width = wd;
2097
2098 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2099
2100 /* If y is below window bottom, ensure that we still see a cursor. */
2101 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2102
2103 h = max (h0, glyph->ascent + glyph->descent);
2104 h0 = min (h0, glyph->ascent + glyph->descent);
2105
2106 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2107 if (y < y0)
2108 {
2109 h = max (h - (y0 - y) + 1, h0);
2110 y = y0 - 1;
2111 }
2112 else
2113 {
2114 y0 = window_text_bottom_y (w) - h0;
2115 if (y > y0)
2116 {
2117 h += y - y0;
2118 y = y0;
2119 }
2120 }
2121
2122 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2123 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2124 *heightp = h;
2125 }
2126
2127 /*
2128 * Remember which glyph the mouse is over.
2129 */
2130
2131 void
2132 remember_mouse_glyph (f, gx, gy, rect)
2133 struct frame *f;
2134 int gx, gy;
2135 NativeRectangle *rect;
2136 {
2137 Lisp_Object window;
2138 struct window *w;
2139 struct glyph_row *r, *gr, *end_row;
2140 enum window_part part;
2141 enum glyph_row_area area;
2142 int x, y, width, height;
2143
2144 /* Try to determine frame pixel position and size of the glyph under
2145 frame pixel coordinates X/Y on frame F. */
2146
2147 if (!f->glyphs_initialized_p
2148 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2149 NILP (window)))
2150 {
2151 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2152 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2153 goto virtual_glyph;
2154 }
2155
2156 w = XWINDOW (window);
2157 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2158 height = WINDOW_FRAME_LINE_HEIGHT (w);
2159
2160 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2161 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2162
2163 if (w->pseudo_window_p)
2164 {
2165 area = TEXT_AREA;
2166 part = ON_MODE_LINE; /* Don't adjust margin. */
2167 goto text_glyph;
2168 }
2169
2170 switch (part)
2171 {
2172 case ON_LEFT_MARGIN:
2173 area = LEFT_MARGIN_AREA;
2174 goto text_glyph;
2175
2176 case ON_RIGHT_MARGIN:
2177 area = RIGHT_MARGIN_AREA;
2178 goto text_glyph;
2179
2180 case ON_HEADER_LINE:
2181 case ON_MODE_LINE:
2182 gr = (part == ON_HEADER_LINE
2183 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2184 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2185 gy = gr->y;
2186 area = TEXT_AREA;
2187 goto text_glyph_row_found;
2188
2189 case ON_TEXT:
2190 area = TEXT_AREA;
2191
2192 text_glyph:
2193 gr = 0; gy = 0;
2194 for (; r <= end_row && r->enabled_p; ++r)
2195 if (r->y + r->height > y)
2196 {
2197 gr = r; gy = r->y;
2198 break;
2199 }
2200
2201 text_glyph_row_found:
2202 if (gr && gy <= y)
2203 {
2204 struct glyph *g = gr->glyphs[area];
2205 struct glyph *end = g + gr->used[area];
2206
2207 height = gr->height;
2208 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2209 if (gx + g->pixel_width > x)
2210 break;
2211
2212 if (g < end)
2213 {
2214 if (g->type == IMAGE_GLYPH)
2215 {
2216 /* Don't remember when mouse is over image, as
2217 image may have hot-spots. */
2218 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2219 return;
2220 }
2221 width = g->pixel_width;
2222 }
2223 else
2224 {
2225 /* Use nominal char spacing at end of line. */
2226 x -= gx;
2227 gx += (x / width) * width;
2228 }
2229
2230 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2231 gx += window_box_left_offset (w, area);
2232 }
2233 else
2234 {
2235 /* Use nominal line height at end of window. */
2236 gx = (x / width) * width;
2237 y -= gy;
2238 gy += (y / height) * height;
2239 }
2240 break;
2241
2242 case ON_LEFT_FRINGE:
2243 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2244 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2245 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2246 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2247 goto row_glyph;
2248
2249 case ON_RIGHT_FRINGE:
2250 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2251 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2252 : window_box_right_offset (w, TEXT_AREA));
2253 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2254 goto row_glyph;
2255
2256 case ON_SCROLL_BAR:
2257 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2258 ? 0
2259 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2260 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2261 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2262 : 0)));
2263 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2264
2265 row_glyph:
2266 gr = 0, gy = 0;
2267 for (; r <= end_row && r->enabled_p; ++r)
2268 if (r->y + r->height > y)
2269 {
2270 gr = r; gy = r->y;
2271 break;
2272 }
2273
2274 if (gr && gy <= y)
2275 height = gr->height;
2276 else
2277 {
2278 /* Use nominal line height at end of window. */
2279 y -= gy;
2280 gy += (y / height) * height;
2281 }
2282 break;
2283
2284 default:
2285 ;
2286 virtual_glyph:
2287 /* If there is no glyph under the mouse, then we divide the screen
2288 into a grid of the smallest glyph in the frame, and use that
2289 as our "glyph". */
2290
2291 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2292 round down even for negative values. */
2293 if (gx < 0)
2294 gx -= width - 1;
2295 if (gy < 0)
2296 gy -= height - 1;
2297
2298 gx = (gx / width) * width;
2299 gy = (gy / height) * height;
2300
2301 goto store_rect;
2302 }
2303
2304 gx += WINDOW_LEFT_EDGE_X (w);
2305 gy += WINDOW_TOP_EDGE_Y (w);
2306
2307 store_rect:
2308 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2309
2310 /* Visible feedback for debugging. */
2311 #if 0
2312 #if HAVE_X_WINDOWS
2313 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2314 f->output_data.x->normal_gc,
2315 gx, gy, width, height);
2316 #endif
2317 #endif
2318 }
2319
2320
2321 #endif /* HAVE_WINDOW_SYSTEM */
2322
2323 \f
2324 /***********************************************************************
2325 Lisp form evaluation
2326 ***********************************************************************/
2327
2328 /* Error handler for safe_eval and safe_call. */
2329
2330 static Lisp_Object
2331 safe_eval_handler (arg)
2332 Lisp_Object arg;
2333 {
2334 add_to_log ("Error during redisplay: %s", arg, Qnil);
2335 return Qnil;
2336 }
2337
2338
2339 /* Evaluate SEXPR and return the result, or nil if something went
2340 wrong. Prevent redisplay during the evaluation. */
2341
2342 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2343 Return the result, or nil if something went wrong. Prevent
2344 redisplay during the evaluation. */
2345
2346 Lisp_Object
2347 safe_call (nargs, args)
2348 int nargs;
2349 Lisp_Object *args;
2350 {
2351 Lisp_Object val;
2352
2353 if (inhibit_eval_during_redisplay)
2354 val = Qnil;
2355 else
2356 {
2357 int count = SPECPDL_INDEX ();
2358 struct gcpro gcpro1;
2359
2360 GCPRO1 (args[0]);
2361 gcpro1.nvars = nargs;
2362 specbind (Qinhibit_redisplay, Qt);
2363 /* Use Qt to ensure debugger does not run,
2364 so there is no possibility of wanting to redisplay. */
2365 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2366 safe_eval_handler);
2367 UNGCPRO;
2368 val = unbind_to (count, val);
2369 }
2370
2371 return val;
2372 }
2373
2374
2375 /* Call function FN with one argument ARG.
2376 Return the result, or nil if something went wrong. */
2377
2378 Lisp_Object
2379 safe_call1 (fn, arg)
2380 Lisp_Object fn, arg;
2381 {
2382 Lisp_Object args[2];
2383 args[0] = fn;
2384 args[1] = arg;
2385 return safe_call (2, args);
2386 }
2387
2388 static Lisp_Object Qeval;
2389
2390 Lisp_Object
2391 safe_eval (Lisp_Object sexpr)
2392 {
2393 return safe_call1 (Qeval, sexpr);
2394 }
2395
2396 /* Call function FN with one argument ARG.
2397 Return the result, or nil if something went wrong. */
2398
2399 Lisp_Object
2400 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2401 {
2402 Lisp_Object args[3];
2403 args[0] = fn;
2404 args[1] = arg1;
2405 args[2] = arg2;
2406 return safe_call (3, args);
2407 }
2408
2409
2410 \f
2411 /***********************************************************************
2412 Debugging
2413 ***********************************************************************/
2414
2415 #if 0
2416
2417 /* Define CHECK_IT to perform sanity checks on iterators.
2418 This is for debugging. It is too slow to do unconditionally. */
2419
2420 static void
2421 check_it (it)
2422 struct it *it;
2423 {
2424 if (it->method == GET_FROM_STRING)
2425 {
2426 xassert (STRINGP (it->string));
2427 xassert (IT_STRING_CHARPOS (*it) >= 0);
2428 }
2429 else
2430 {
2431 xassert (IT_STRING_CHARPOS (*it) < 0);
2432 if (it->method == GET_FROM_BUFFER)
2433 {
2434 /* Check that character and byte positions agree. */
2435 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2436 }
2437 }
2438
2439 if (it->dpvec)
2440 xassert (it->current.dpvec_index >= 0);
2441 else
2442 xassert (it->current.dpvec_index < 0);
2443 }
2444
2445 #define CHECK_IT(IT) check_it ((IT))
2446
2447 #else /* not 0 */
2448
2449 #define CHECK_IT(IT) (void) 0
2450
2451 #endif /* not 0 */
2452
2453
2454 #if GLYPH_DEBUG
2455
2456 /* Check that the window end of window W is what we expect it
2457 to be---the last row in the current matrix displaying text. */
2458
2459 static void
2460 check_window_end (w)
2461 struct window *w;
2462 {
2463 if (!MINI_WINDOW_P (w)
2464 && !NILP (w->window_end_valid))
2465 {
2466 struct glyph_row *row;
2467 xassert ((row = MATRIX_ROW (w->current_matrix,
2468 XFASTINT (w->window_end_vpos)),
2469 !row->enabled_p
2470 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2471 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2472 }
2473 }
2474
2475 #define CHECK_WINDOW_END(W) check_window_end ((W))
2476
2477 #else /* not GLYPH_DEBUG */
2478
2479 #define CHECK_WINDOW_END(W) (void) 0
2480
2481 #endif /* not GLYPH_DEBUG */
2482
2483
2484 \f
2485 /***********************************************************************
2486 Iterator initialization
2487 ***********************************************************************/
2488
2489 /* Initialize IT for displaying current_buffer in window W, starting
2490 at character position CHARPOS. CHARPOS < 0 means that no buffer
2491 position is specified which is useful when the iterator is assigned
2492 a position later. BYTEPOS is the byte position corresponding to
2493 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2494
2495 If ROW is not null, calls to produce_glyphs with IT as parameter
2496 will produce glyphs in that row.
2497
2498 BASE_FACE_ID is the id of a base face to use. It must be one of
2499 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2500 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2501 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2502
2503 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2504 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2505 will be initialized to use the corresponding mode line glyph row of
2506 the desired matrix of W. */
2507
2508 void
2509 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2510 struct it *it;
2511 struct window *w;
2512 int charpos, bytepos;
2513 struct glyph_row *row;
2514 enum face_id base_face_id;
2515 {
2516 int highlight_region_p;
2517 enum face_id remapped_base_face_id = base_face_id;
2518
2519 /* Some precondition checks. */
2520 xassert (w != NULL && it != NULL);
2521 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2522 && charpos <= ZV));
2523
2524 /* If face attributes have been changed since the last redisplay,
2525 free realized faces now because they depend on face definitions
2526 that might have changed. Don't free faces while there might be
2527 desired matrices pending which reference these faces. */
2528 if (face_change_count && !inhibit_free_realized_faces)
2529 {
2530 face_change_count = 0;
2531 free_all_realized_faces (Qnil);
2532 }
2533
2534 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2535 if (! NILP (Vface_remapping_alist))
2536 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2537
2538 /* Use one of the mode line rows of W's desired matrix if
2539 appropriate. */
2540 if (row == NULL)
2541 {
2542 if (base_face_id == MODE_LINE_FACE_ID
2543 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2544 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2545 else if (base_face_id == HEADER_LINE_FACE_ID)
2546 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2547 }
2548
2549 /* Clear IT. */
2550 bzero (it, sizeof *it);
2551 it->current.overlay_string_index = -1;
2552 it->current.dpvec_index = -1;
2553 it->base_face_id = remapped_base_face_id;
2554 it->string = Qnil;
2555 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2556
2557 /* The window in which we iterate over current_buffer: */
2558 XSETWINDOW (it->window, w);
2559 it->w = w;
2560 it->f = XFRAME (w->frame);
2561
2562 it->cmp_it.id = -1;
2563
2564 /* Extra space between lines (on window systems only). */
2565 if (base_face_id == DEFAULT_FACE_ID
2566 && FRAME_WINDOW_P (it->f))
2567 {
2568 if (NATNUMP (current_buffer->extra_line_spacing))
2569 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2570 else if (FLOATP (current_buffer->extra_line_spacing))
2571 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2572 * FRAME_LINE_HEIGHT (it->f));
2573 else if (it->f->extra_line_spacing > 0)
2574 it->extra_line_spacing = it->f->extra_line_spacing;
2575 it->max_extra_line_spacing = 0;
2576 }
2577
2578 /* If realized faces have been removed, e.g. because of face
2579 attribute changes of named faces, recompute them. When running
2580 in batch mode, the face cache of the initial frame is null. If
2581 we happen to get called, make a dummy face cache. */
2582 if (FRAME_FACE_CACHE (it->f) == NULL)
2583 init_frame_faces (it->f);
2584 if (FRAME_FACE_CACHE (it->f)->used == 0)
2585 recompute_basic_faces (it->f);
2586
2587 /* Current value of the `slice', `space-width', and 'height' properties. */
2588 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2589 it->space_width = Qnil;
2590 it->font_height = Qnil;
2591 it->override_ascent = -1;
2592
2593 /* Are control characters displayed as `^C'? */
2594 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2595
2596 /* -1 means everything between a CR and the following line end
2597 is invisible. >0 means lines indented more than this value are
2598 invisible. */
2599 it->selective = (INTEGERP (current_buffer->selective_display)
2600 ? XFASTINT (current_buffer->selective_display)
2601 : (!NILP (current_buffer->selective_display)
2602 ? -1 : 0));
2603 it->selective_display_ellipsis_p
2604 = !NILP (current_buffer->selective_display_ellipses);
2605
2606 /* Display table to use. */
2607 it->dp = window_display_table (w);
2608
2609 /* Are multibyte characters enabled in current_buffer? */
2610 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2611
2612 /* Do we need to reorder bidirectional text? */
2613 it->bidi_p = !NILP (current_buffer->bidi_display_reordering);
2614
2615 /* Non-zero if we should highlight the region. */
2616 highlight_region_p
2617 = (!NILP (Vtransient_mark_mode)
2618 && !NILP (current_buffer->mark_active)
2619 && XMARKER (current_buffer->mark)->buffer != 0);
2620
2621 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2622 start and end of a visible region in window IT->w. Set both to
2623 -1 to indicate no region. */
2624 if (highlight_region_p
2625 /* Maybe highlight only in selected window. */
2626 && (/* Either show region everywhere. */
2627 highlight_nonselected_windows
2628 /* Or show region in the selected window. */
2629 || w == XWINDOW (selected_window)
2630 /* Or show the region if we are in the mini-buffer and W is
2631 the window the mini-buffer refers to. */
2632 || (MINI_WINDOW_P (XWINDOW (selected_window))
2633 && WINDOWP (minibuf_selected_window)
2634 && w == XWINDOW (minibuf_selected_window))))
2635 {
2636 int charpos = marker_position (current_buffer->mark);
2637 it->region_beg_charpos = min (PT, charpos);
2638 it->region_end_charpos = max (PT, charpos);
2639 }
2640 else
2641 it->region_beg_charpos = it->region_end_charpos = -1;
2642
2643 /* Get the position at which the redisplay_end_trigger hook should
2644 be run, if it is to be run at all. */
2645 if (MARKERP (w->redisplay_end_trigger)
2646 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2647 it->redisplay_end_trigger_charpos
2648 = marker_position (w->redisplay_end_trigger);
2649 else if (INTEGERP (w->redisplay_end_trigger))
2650 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2651
2652 /* Correct bogus values of tab_width. */
2653 it->tab_width = XINT (current_buffer->tab_width);
2654 if (it->tab_width <= 0 || it->tab_width > 1000)
2655 it->tab_width = 8;
2656
2657 /* Are lines in the display truncated? */
2658 if (base_face_id != DEFAULT_FACE_ID
2659 || XINT (it->w->hscroll)
2660 || (! WINDOW_FULL_WIDTH_P (it->w)
2661 && ((!NILP (Vtruncate_partial_width_windows)
2662 && !INTEGERP (Vtruncate_partial_width_windows))
2663 || (INTEGERP (Vtruncate_partial_width_windows)
2664 && (WINDOW_TOTAL_COLS (it->w)
2665 < XINT (Vtruncate_partial_width_windows))))))
2666 it->line_wrap = TRUNCATE;
2667 else if (NILP (current_buffer->truncate_lines))
2668 it->line_wrap = NILP (current_buffer->word_wrap)
2669 ? WINDOW_WRAP : WORD_WRAP;
2670 else
2671 it->line_wrap = TRUNCATE;
2672
2673 /* Get dimensions of truncation and continuation glyphs. These are
2674 displayed as fringe bitmaps under X, so we don't need them for such
2675 frames. */
2676 if (!FRAME_WINDOW_P (it->f))
2677 {
2678 if (it->line_wrap == TRUNCATE)
2679 {
2680 /* We will need the truncation glyph. */
2681 xassert (it->glyph_row == NULL);
2682 produce_special_glyphs (it, IT_TRUNCATION);
2683 it->truncation_pixel_width = it->pixel_width;
2684 }
2685 else
2686 {
2687 /* We will need the continuation glyph. */
2688 xassert (it->glyph_row == NULL);
2689 produce_special_glyphs (it, IT_CONTINUATION);
2690 it->continuation_pixel_width = it->pixel_width;
2691 }
2692
2693 /* Reset these values to zero because the produce_special_glyphs
2694 above has changed them. */
2695 it->pixel_width = it->ascent = it->descent = 0;
2696 it->phys_ascent = it->phys_descent = 0;
2697 }
2698
2699 /* Set this after getting the dimensions of truncation and
2700 continuation glyphs, so that we don't produce glyphs when calling
2701 produce_special_glyphs, above. */
2702 it->glyph_row = row;
2703 it->area = TEXT_AREA;
2704
2705 /* Forget any previous info about this row being reversed. */
2706 if (it->glyph_row)
2707 it->glyph_row->reversed_p = 0;
2708
2709 /* Get the dimensions of the display area. The display area
2710 consists of the visible window area plus a horizontally scrolled
2711 part to the left of the window. All x-values are relative to the
2712 start of this total display area. */
2713 if (base_face_id != DEFAULT_FACE_ID)
2714 {
2715 /* Mode lines, menu bar in terminal frames. */
2716 it->first_visible_x = 0;
2717 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2718 }
2719 else
2720 {
2721 it->first_visible_x
2722 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2723 it->last_visible_x = (it->first_visible_x
2724 + window_box_width (w, TEXT_AREA));
2725
2726 /* If we truncate lines, leave room for the truncator glyph(s) at
2727 the right margin. Otherwise, leave room for the continuation
2728 glyph(s). Truncation and continuation glyphs are not inserted
2729 for window-based redisplay. */
2730 if (!FRAME_WINDOW_P (it->f))
2731 {
2732 if (it->line_wrap == TRUNCATE)
2733 it->last_visible_x -= it->truncation_pixel_width;
2734 else
2735 it->last_visible_x -= it->continuation_pixel_width;
2736 }
2737
2738 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2739 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2740 }
2741
2742 /* Leave room for a border glyph. */
2743 if (!FRAME_WINDOW_P (it->f)
2744 && !WINDOW_RIGHTMOST_P (it->w))
2745 it->last_visible_x -= 1;
2746
2747 it->last_visible_y = window_text_bottom_y (w);
2748
2749 /* For mode lines and alike, arrange for the first glyph having a
2750 left box line if the face specifies a box. */
2751 if (base_face_id != DEFAULT_FACE_ID)
2752 {
2753 struct face *face;
2754
2755 it->face_id = remapped_base_face_id;
2756
2757 /* If we have a boxed mode line, make the first character appear
2758 with a left box line. */
2759 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2760 if (face->box != FACE_NO_BOX)
2761 it->start_of_box_run_p = 1;
2762 }
2763
2764 /* If we are to reorder bidirectional text, init the bidi
2765 iterator. */
2766 if (it->bidi_p)
2767 {
2768 /* Note the paragraph direction that this buffer wants to
2769 use. */
2770 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2771 it->paragraph_embedding = L2R;
2772 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2773 it->paragraph_embedding = R2L;
2774 else
2775 it->paragraph_embedding = NEUTRAL_DIR;
2776 bidi_init_it (charpos, bytepos, &it->bidi_it);
2777 }
2778
2779 /* If a buffer position was specified, set the iterator there,
2780 getting overlays and face properties from that position. */
2781 if (charpos >= BUF_BEG (current_buffer))
2782 {
2783 it->end_charpos = ZV;
2784 it->face_id = -1;
2785 IT_CHARPOS (*it) = charpos;
2786
2787 /* Compute byte position if not specified. */
2788 if (bytepos < charpos)
2789 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2790 else
2791 IT_BYTEPOS (*it) = bytepos;
2792
2793 it->start = it->current;
2794
2795 /* Compute faces etc. */
2796 reseat (it, it->current.pos, 1);
2797 }
2798
2799 CHECK_IT (it);
2800 }
2801
2802
2803 /* Initialize IT for the display of window W with window start POS. */
2804
2805 void
2806 start_display (it, w, pos)
2807 struct it *it;
2808 struct window *w;
2809 struct text_pos pos;
2810 {
2811 struct glyph_row *row;
2812 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2813
2814 row = w->desired_matrix->rows + first_vpos;
2815 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2816 it->first_vpos = first_vpos;
2817
2818 /* Don't reseat to previous visible line start if current start
2819 position is in a string or image. */
2820 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2821 {
2822 int start_at_line_beg_p;
2823 int first_y = it->current_y;
2824
2825 /* If window start is not at a line start, skip forward to POS to
2826 get the correct continuation lines width. */
2827 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2828 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2829 if (!start_at_line_beg_p)
2830 {
2831 int new_x;
2832
2833 reseat_at_previous_visible_line_start (it);
2834 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2835
2836 new_x = it->current_x + it->pixel_width;
2837
2838 /* If lines are continued, this line may end in the middle
2839 of a multi-glyph character (e.g. a control character
2840 displayed as \003, or in the middle of an overlay
2841 string). In this case move_it_to above will not have
2842 taken us to the start of the continuation line but to the
2843 end of the continued line. */
2844 if (it->current_x > 0
2845 && it->line_wrap != TRUNCATE /* Lines are continued. */
2846 && (/* And glyph doesn't fit on the line. */
2847 new_x > it->last_visible_x
2848 /* Or it fits exactly and we're on a window
2849 system frame. */
2850 || (new_x == it->last_visible_x
2851 && FRAME_WINDOW_P (it->f))))
2852 {
2853 if (it->current.dpvec_index >= 0
2854 || it->current.overlay_string_index >= 0)
2855 {
2856 set_iterator_to_next (it, 1);
2857 move_it_in_display_line_to (it, -1, -1, 0);
2858 }
2859
2860 it->continuation_lines_width += it->current_x;
2861 }
2862
2863 /* We're starting a new display line, not affected by the
2864 height of the continued line, so clear the appropriate
2865 fields in the iterator structure. */
2866 it->max_ascent = it->max_descent = 0;
2867 it->max_phys_ascent = it->max_phys_descent = 0;
2868
2869 it->current_y = first_y;
2870 it->vpos = 0;
2871 it->current_x = it->hpos = 0;
2872 }
2873 }
2874 }
2875
2876
2877 /* Return 1 if POS is a position in ellipses displayed for invisible
2878 text. W is the window we display, for text property lookup. */
2879
2880 static int
2881 in_ellipses_for_invisible_text_p (pos, w)
2882 struct display_pos *pos;
2883 struct window *w;
2884 {
2885 Lisp_Object prop, window;
2886 int ellipses_p = 0;
2887 int charpos = CHARPOS (pos->pos);
2888
2889 /* If POS specifies a position in a display vector, this might
2890 be for an ellipsis displayed for invisible text. We won't
2891 get the iterator set up for delivering that ellipsis unless
2892 we make sure that it gets aware of the invisible text. */
2893 if (pos->dpvec_index >= 0
2894 && pos->overlay_string_index < 0
2895 && CHARPOS (pos->string_pos) < 0
2896 && charpos > BEGV
2897 && (XSETWINDOW (window, w),
2898 prop = Fget_char_property (make_number (charpos),
2899 Qinvisible, window),
2900 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2901 {
2902 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2903 window);
2904 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2905 }
2906
2907 return ellipses_p;
2908 }
2909
2910
2911 /* Initialize IT for stepping through current_buffer in window W,
2912 starting at position POS that includes overlay string and display
2913 vector/ control character translation position information. Value
2914 is zero if there are overlay strings with newlines at POS. */
2915
2916 static int
2917 init_from_display_pos (it, w, pos)
2918 struct it *it;
2919 struct window *w;
2920 struct display_pos *pos;
2921 {
2922 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2923 int i, overlay_strings_with_newlines = 0;
2924
2925 /* If POS specifies a position in a display vector, this might
2926 be for an ellipsis displayed for invisible text. We won't
2927 get the iterator set up for delivering that ellipsis unless
2928 we make sure that it gets aware of the invisible text. */
2929 if (in_ellipses_for_invisible_text_p (pos, w))
2930 {
2931 --charpos;
2932 bytepos = 0;
2933 }
2934
2935 /* Keep in mind: the call to reseat in init_iterator skips invisible
2936 text, so we might end up at a position different from POS. This
2937 is only a problem when POS is a row start after a newline and an
2938 overlay starts there with an after-string, and the overlay has an
2939 invisible property. Since we don't skip invisible text in
2940 display_line and elsewhere immediately after consuming the
2941 newline before the row start, such a POS will not be in a string,
2942 but the call to init_iterator below will move us to the
2943 after-string. */
2944 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2945
2946 /* This only scans the current chunk -- it should scan all chunks.
2947 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2948 to 16 in 22.1 to make this a lesser problem. */
2949 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2950 {
2951 const char *s = SDATA (it->overlay_strings[i]);
2952 const char *e = s + SBYTES (it->overlay_strings[i]);
2953
2954 while (s < e && *s != '\n')
2955 ++s;
2956
2957 if (s < e)
2958 {
2959 overlay_strings_with_newlines = 1;
2960 break;
2961 }
2962 }
2963
2964 /* If position is within an overlay string, set up IT to the right
2965 overlay string. */
2966 if (pos->overlay_string_index >= 0)
2967 {
2968 int relative_index;
2969
2970 /* If the first overlay string happens to have a `display'
2971 property for an image, the iterator will be set up for that
2972 image, and we have to undo that setup first before we can
2973 correct the overlay string index. */
2974 if (it->method == GET_FROM_IMAGE)
2975 pop_it (it);
2976
2977 /* We already have the first chunk of overlay strings in
2978 IT->overlay_strings. Load more until the one for
2979 pos->overlay_string_index is in IT->overlay_strings. */
2980 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2981 {
2982 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2983 it->current.overlay_string_index = 0;
2984 while (n--)
2985 {
2986 load_overlay_strings (it, 0);
2987 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2988 }
2989 }
2990
2991 it->current.overlay_string_index = pos->overlay_string_index;
2992 relative_index = (it->current.overlay_string_index
2993 % OVERLAY_STRING_CHUNK_SIZE);
2994 it->string = it->overlay_strings[relative_index];
2995 xassert (STRINGP (it->string));
2996 it->current.string_pos = pos->string_pos;
2997 it->method = GET_FROM_STRING;
2998 }
2999
3000 if (CHARPOS (pos->string_pos) >= 0)
3001 {
3002 /* Recorded position is not in an overlay string, but in another
3003 string. This can only be a string from a `display' property.
3004 IT should already be filled with that string. */
3005 it->current.string_pos = pos->string_pos;
3006 xassert (STRINGP (it->string));
3007 }
3008
3009 /* Restore position in display vector translations, control
3010 character translations or ellipses. */
3011 if (pos->dpvec_index >= 0)
3012 {
3013 if (it->dpvec == NULL)
3014 get_next_display_element (it);
3015 xassert (it->dpvec && it->current.dpvec_index == 0);
3016 it->current.dpvec_index = pos->dpvec_index;
3017 }
3018
3019 CHECK_IT (it);
3020 return !overlay_strings_with_newlines;
3021 }
3022
3023
3024 /* Initialize IT for stepping through current_buffer in window W
3025 starting at ROW->start. */
3026
3027 static void
3028 init_to_row_start (it, w, row)
3029 struct it *it;
3030 struct window *w;
3031 struct glyph_row *row;
3032 {
3033 init_from_display_pos (it, w, &row->start);
3034 it->start = row->start;
3035 it->continuation_lines_width = row->continuation_lines_width;
3036 CHECK_IT (it);
3037 }
3038
3039
3040 /* Initialize IT for stepping through current_buffer in window W
3041 starting in the line following ROW, i.e. starting at ROW->end.
3042 Value is zero if there are overlay strings with newlines at ROW's
3043 end position. */
3044
3045 static int
3046 init_to_row_end (it, w, row)
3047 struct it *it;
3048 struct window *w;
3049 struct glyph_row *row;
3050 {
3051 int success = 0;
3052
3053 if (init_from_display_pos (it, w, &row->end))
3054 {
3055 if (row->continued_p)
3056 it->continuation_lines_width
3057 = row->continuation_lines_width + row->pixel_width;
3058 CHECK_IT (it);
3059 success = 1;
3060 }
3061
3062 return success;
3063 }
3064
3065
3066
3067 \f
3068 /***********************************************************************
3069 Text properties
3070 ***********************************************************************/
3071
3072 /* Called when IT reaches IT->stop_charpos. Handle text property and
3073 overlay changes. Set IT->stop_charpos to the next position where
3074 to stop. */
3075
3076 static void
3077 handle_stop (it)
3078 struct it *it;
3079 {
3080 enum prop_handled handled;
3081 int handle_overlay_change_p;
3082 struct props *p;
3083
3084 it->dpvec = NULL;
3085 it->current.dpvec_index = -1;
3086 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3087 it->ignore_overlay_strings_at_pos_p = 0;
3088 it->ellipsis_p = 0;
3089
3090 /* Use face of preceding text for ellipsis (if invisible) */
3091 if (it->selective_display_ellipsis_p)
3092 it->saved_face_id = it->face_id;
3093
3094 do
3095 {
3096 handled = HANDLED_NORMALLY;
3097
3098 /* Call text property handlers. */
3099 for (p = it_props; p->handler; ++p)
3100 {
3101 handled = p->handler (it);
3102
3103 if (handled == HANDLED_RECOMPUTE_PROPS)
3104 break;
3105 else if (handled == HANDLED_RETURN)
3106 {
3107 /* We still want to show before and after strings from
3108 overlays even if the actual buffer text is replaced. */
3109 if (!handle_overlay_change_p
3110 || it->sp > 1
3111 || !get_overlay_strings_1 (it, 0, 0))
3112 {
3113 if (it->ellipsis_p)
3114 setup_for_ellipsis (it, 0);
3115 /* When handling a display spec, we might load an
3116 empty string. In that case, discard it here. We
3117 used to discard it in handle_single_display_spec,
3118 but that causes get_overlay_strings_1, above, to
3119 ignore overlay strings that we must check. */
3120 if (STRINGP (it->string) && !SCHARS (it->string))
3121 pop_it (it);
3122 return;
3123 }
3124 else if (STRINGP (it->string) && !SCHARS (it->string))
3125 pop_it (it);
3126 else
3127 {
3128 it->ignore_overlay_strings_at_pos_p = 1;
3129 it->string_from_display_prop_p = 0;
3130 handle_overlay_change_p = 0;
3131 }
3132 handled = HANDLED_RECOMPUTE_PROPS;
3133 break;
3134 }
3135 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3136 handle_overlay_change_p = 0;
3137 }
3138
3139 if (handled != HANDLED_RECOMPUTE_PROPS)
3140 {
3141 /* Don't check for overlay strings below when set to deliver
3142 characters from a display vector. */
3143 if (it->method == GET_FROM_DISPLAY_VECTOR)
3144 handle_overlay_change_p = 0;
3145
3146 /* Handle overlay changes.
3147 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3148 if it finds overlays. */
3149 if (handle_overlay_change_p)
3150 handled = handle_overlay_change (it);
3151 }
3152
3153 if (it->ellipsis_p)
3154 {
3155 setup_for_ellipsis (it, 0);
3156 break;
3157 }
3158 }
3159 while (handled == HANDLED_RECOMPUTE_PROPS);
3160
3161 /* Determine where to stop next. */
3162 if (handled == HANDLED_NORMALLY)
3163 compute_stop_pos (it);
3164 }
3165
3166
3167 /* Compute IT->stop_charpos from text property and overlay change
3168 information for IT's current position. */
3169
3170 static void
3171 compute_stop_pos (it)
3172 struct it *it;
3173 {
3174 register INTERVAL iv, next_iv;
3175 Lisp_Object object, limit, position;
3176 EMACS_INT charpos, bytepos;
3177
3178 /* If nowhere else, stop at the end. */
3179 it->stop_charpos = it->end_charpos;
3180
3181 if (STRINGP (it->string))
3182 {
3183 /* Strings are usually short, so don't limit the search for
3184 properties. */
3185 object = it->string;
3186 limit = Qnil;
3187 charpos = IT_STRING_CHARPOS (*it);
3188 bytepos = IT_STRING_BYTEPOS (*it);
3189 }
3190 else
3191 {
3192 EMACS_INT pos;
3193
3194 /* If next overlay change is in front of the current stop pos
3195 (which is IT->end_charpos), stop there. Note: value of
3196 next_overlay_change is point-max if no overlay change
3197 follows. */
3198 charpos = IT_CHARPOS (*it);
3199 bytepos = IT_BYTEPOS (*it);
3200 pos = next_overlay_change (charpos);
3201 if (pos < it->stop_charpos)
3202 it->stop_charpos = pos;
3203
3204 /* If showing the region, we have to stop at the region
3205 start or end because the face might change there. */
3206 if (it->region_beg_charpos > 0)
3207 {
3208 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3209 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3210 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3211 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3212 }
3213
3214 /* Set up variables for computing the stop position from text
3215 property changes. */
3216 XSETBUFFER (object, current_buffer);
3217 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3218 }
3219
3220 /* Get the interval containing IT's position. Value is a null
3221 interval if there isn't such an interval. */
3222 position = make_number (charpos);
3223 iv = validate_interval_range (object, &position, &position, 0);
3224 if (!NULL_INTERVAL_P (iv))
3225 {
3226 Lisp_Object values_here[LAST_PROP_IDX];
3227 struct props *p;
3228
3229 /* Get properties here. */
3230 for (p = it_props; p->handler; ++p)
3231 values_here[p->idx] = textget (iv->plist, *p->name);
3232
3233 /* Look for an interval following iv that has different
3234 properties. */
3235 for (next_iv = next_interval (iv);
3236 (!NULL_INTERVAL_P (next_iv)
3237 && (NILP (limit)
3238 || XFASTINT (limit) > next_iv->position));
3239 next_iv = next_interval (next_iv))
3240 {
3241 for (p = it_props; p->handler; ++p)
3242 {
3243 Lisp_Object new_value;
3244
3245 new_value = textget (next_iv->plist, *p->name);
3246 if (!EQ (values_here[p->idx], new_value))
3247 break;
3248 }
3249
3250 if (p->handler)
3251 break;
3252 }
3253
3254 if (!NULL_INTERVAL_P (next_iv))
3255 {
3256 if (INTEGERP (limit)
3257 && next_iv->position >= XFASTINT (limit))
3258 /* No text property change up to limit. */
3259 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3260 else
3261 /* Text properties change in next_iv. */
3262 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3263 }
3264 }
3265
3266 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3267 it->stop_charpos, it->string);
3268
3269 xassert (STRINGP (it->string)
3270 || (it->stop_charpos >= BEGV
3271 && it->stop_charpos >= IT_CHARPOS (*it)));
3272 }
3273
3274
3275 /* Return the position of the next overlay change after POS in
3276 current_buffer. Value is point-max if no overlay change
3277 follows. This is like `next-overlay-change' but doesn't use
3278 xmalloc. */
3279
3280 static EMACS_INT
3281 next_overlay_change (pos)
3282 EMACS_INT pos;
3283 {
3284 int noverlays;
3285 EMACS_INT endpos;
3286 Lisp_Object *overlays;
3287 int i;
3288
3289 /* Get all overlays at the given position. */
3290 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3291
3292 /* If any of these overlays ends before endpos,
3293 use its ending point instead. */
3294 for (i = 0; i < noverlays; ++i)
3295 {
3296 Lisp_Object oend;
3297 EMACS_INT oendpos;
3298
3299 oend = OVERLAY_END (overlays[i]);
3300 oendpos = OVERLAY_POSITION (oend);
3301 endpos = min (endpos, oendpos);
3302 }
3303
3304 return endpos;
3305 }
3306
3307
3308 \f
3309 /***********************************************************************
3310 Fontification
3311 ***********************************************************************/
3312
3313 /* Handle changes in the `fontified' property of the current buffer by
3314 calling hook functions from Qfontification_functions to fontify
3315 regions of text. */
3316
3317 static enum prop_handled
3318 handle_fontified_prop (it)
3319 struct it *it;
3320 {
3321 Lisp_Object prop, pos;
3322 enum prop_handled handled = HANDLED_NORMALLY;
3323
3324 if (!NILP (Vmemory_full))
3325 return handled;
3326
3327 /* Get the value of the `fontified' property at IT's current buffer
3328 position. (The `fontified' property doesn't have a special
3329 meaning in strings.) If the value is nil, call functions from
3330 Qfontification_functions. */
3331 if (!STRINGP (it->string)
3332 && it->s == NULL
3333 && !NILP (Vfontification_functions)
3334 && !NILP (Vrun_hooks)
3335 && (pos = make_number (IT_CHARPOS (*it)),
3336 prop = Fget_char_property (pos, Qfontified, Qnil),
3337 /* Ignore the special cased nil value always present at EOB since
3338 no amount of fontifying will be able to change it. */
3339 NILP (prop) && IT_CHARPOS (*it) < Z))
3340 {
3341 int count = SPECPDL_INDEX ();
3342 Lisp_Object val;
3343
3344 val = Vfontification_functions;
3345 specbind (Qfontification_functions, Qnil);
3346
3347 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3348 safe_call1 (val, pos);
3349 else
3350 {
3351 Lisp_Object globals, fn;
3352 struct gcpro gcpro1, gcpro2;
3353
3354 globals = Qnil;
3355 GCPRO2 (val, globals);
3356
3357 for (; CONSP (val); val = XCDR (val))
3358 {
3359 fn = XCAR (val);
3360
3361 if (EQ (fn, Qt))
3362 {
3363 /* A value of t indicates this hook has a local
3364 binding; it means to run the global binding too.
3365 In a global value, t should not occur. If it
3366 does, we must ignore it to avoid an endless
3367 loop. */
3368 for (globals = Fdefault_value (Qfontification_functions);
3369 CONSP (globals);
3370 globals = XCDR (globals))
3371 {
3372 fn = XCAR (globals);
3373 if (!EQ (fn, Qt))
3374 safe_call1 (fn, pos);
3375 }
3376 }
3377 else
3378 safe_call1 (fn, pos);
3379 }
3380
3381 UNGCPRO;
3382 }
3383
3384 unbind_to (count, Qnil);
3385
3386 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3387 something. This avoids an endless loop if they failed to
3388 fontify the text for which reason ever. */
3389 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3390 handled = HANDLED_RECOMPUTE_PROPS;
3391 }
3392
3393 return handled;
3394 }
3395
3396
3397 \f
3398 /***********************************************************************
3399 Faces
3400 ***********************************************************************/
3401
3402 /* Set up iterator IT from face properties at its current position.
3403 Called from handle_stop. */
3404
3405 static enum prop_handled
3406 handle_face_prop (it)
3407 struct it *it;
3408 {
3409 int new_face_id;
3410 EMACS_INT next_stop;
3411
3412 if (!STRINGP (it->string))
3413 {
3414 new_face_id
3415 = face_at_buffer_position (it->w,
3416 IT_CHARPOS (*it),
3417 it->region_beg_charpos,
3418 it->region_end_charpos,
3419 &next_stop,
3420 (IT_CHARPOS (*it)
3421 + TEXT_PROP_DISTANCE_LIMIT),
3422 0, it->base_face_id);
3423
3424 /* Is this a start of a run of characters with box face?
3425 Caveat: this can be called for a freshly initialized
3426 iterator; face_id is -1 in this case. We know that the new
3427 face will not change until limit, i.e. if the new face has a
3428 box, all characters up to limit will have one. But, as
3429 usual, we don't know whether limit is really the end. */
3430 if (new_face_id != it->face_id)
3431 {
3432 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3433
3434 /* If new face has a box but old face has not, this is
3435 the start of a run of characters with box, i.e. it has
3436 a shadow on the left side. The value of face_id of the
3437 iterator will be -1 if this is the initial call that gets
3438 the face. In this case, we have to look in front of IT's
3439 position and see whether there is a face != new_face_id. */
3440 it->start_of_box_run_p
3441 = (new_face->box != FACE_NO_BOX
3442 && (it->face_id >= 0
3443 || IT_CHARPOS (*it) == BEG
3444 || new_face_id != face_before_it_pos (it)));
3445 it->face_box_p = new_face->box != FACE_NO_BOX;
3446 }
3447 }
3448 else
3449 {
3450 int base_face_id, bufpos;
3451 int i;
3452 Lisp_Object from_overlay
3453 = (it->current.overlay_string_index >= 0
3454 ? it->string_overlays[it->current.overlay_string_index]
3455 : Qnil);
3456
3457 /* See if we got to this string directly or indirectly from
3458 an overlay property. That includes the before-string or
3459 after-string of an overlay, strings in display properties
3460 provided by an overlay, their text properties, etc.
3461
3462 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3463 if (! NILP (from_overlay))
3464 for (i = it->sp - 1; i >= 0; i--)
3465 {
3466 if (it->stack[i].current.overlay_string_index >= 0)
3467 from_overlay
3468 = it->string_overlays[it->stack[i].current.overlay_string_index];
3469 else if (! NILP (it->stack[i].from_overlay))
3470 from_overlay = it->stack[i].from_overlay;
3471
3472 if (!NILP (from_overlay))
3473 break;
3474 }
3475
3476 if (! NILP (from_overlay))
3477 {
3478 bufpos = IT_CHARPOS (*it);
3479 /* For a string from an overlay, the base face depends
3480 only on text properties and ignores overlays. */
3481 base_face_id
3482 = face_for_overlay_string (it->w,
3483 IT_CHARPOS (*it),
3484 it->region_beg_charpos,
3485 it->region_end_charpos,
3486 &next_stop,
3487 (IT_CHARPOS (*it)
3488 + TEXT_PROP_DISTANCE_LIMIT),
3489 0,
3490 from_overlay);
3491 }
3492 else
3493 {
3494 bufpos = 0;
3495
3496 /* For strings from a `display' property, use the face at
3497 IT's current buffer position as the base face to merge
3498 with, so that overlay strings appear in the same face as
3499 surrounding text, unless they specify their own
3500 faces. */
3501 base_face_id = underlying_face_id (it);
3502 }
3503
3504 new_face_id = face_at_string_position (it->w,
3505 it->string,
3506 IT_STRING_CHARPOS (*it),
3507 bufpos,
3508 it->region_beg_charpos,
3509 it->region_end_charpos,
3510 &next_stop,
3511 base_face_id, 0);
3512
3513 /* Is this a start of a run of characters with box? Caveat:
3514 this can be called for a freshly allocated iterator; face_id
3515 is -1 is this case. We know that the new face will not
3516 change until the next check pos, i.e. if the new face has a
3517 box, all characters up to that position will have a
3518 box. But, as usual, we don't know whether that position
3519 is really the end. */
3520 if (new_face_id != it->face_id)
3521 {
3522 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3523 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3524
3525 /* If new face has a box but old face hasn't, this is the
3526 start of a run of characters with box, i.e. it has a
3527 shadow on the left side. */
3528 it->start_of_box_run_p
3529 = new_face->box && (old_face == NULL || !old_face->box);
3530 it->face_box_p = new_face->box != FACE_NO_BOX;
3531 }
3532 }
3533
3534 it->face_id = new_face_id;
3535 return HANDLED_NORMALLY;
3536 }
3537
3538
3539 /* Return the ID of the face ``underlying'' IT's current position,
3540 which is in a string. If the iterator is associated with a
3541 buffer, return the face at IT's current buffer position.
3542 Otherwise, use the iterator's base_face_id. */
3543
3544 static int
3545 underlying_face_id (it)
3546 struct it *it;
3547 {
3548 int face_id = it->base_face_id, i;
3549
3550 xassert (STRINGP (it->string));
3551
3552 for (i = it->sp - 1; i >= 0; --i)
3553 if (NILP (it->stack[i].string))
3554 face_id = it->stack[i].face_id;
3555
3556 return face_id;
3557 }
3558
3559
3560 /* Compute the face one character before or after the current position
3561 of IT. BEFORE_P non-zero means get the face in front of IT's
3562 position. Value is the id of the face. */
3563
3564 static int
3565 face_before_or_after_it_pos (it, before_p)
3566 struct it *it;
3567 int before_p;
3568 {
3569 int face_id, limit;
3570 EMACS_INT next_check_charpos;
3571 struct text_pos pos;
3572
3573 xassert (it->s == NULL);
3574
3575 if (STRINGP (it->string))
3576 {
3577 int bufpos, base_face_id;
3578
3579 /* No face change past the end of the string (for the case
3580 we are padding with spaces). No face change before the
3581 string start. */
3582 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3583 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3584 return it->face_id;
3585
3586 /* Set pos to the position before or after IT's current position. */
3587 if (before_p)
3588 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3589 else
3590 /* For composition, we must check the character after the
3591 composition. */
3592 pos = (it->what == IT_COMPOSITION
3593 ? string_pos (IT_STRING_CHARPOS (*it)
3594 + it->cmp_it.nchars, it->string)
3595 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3596
3597 if (it->current.overlay_string_index >= 0)
3598 bufpos = IT_CHARPOS (*it);
3599 else
3600 bufpos = 0;
3601
3602 base_face_id = underlying_face_id (it);
3603
3604 /* Get the face for ASCII, or unibyte. */
3605 face_id = face_at_string_position (it->w,
3606 it->string,
3607 CHARPOS (pos),
3608 bufpos,
3609 it->region_beg_charpos,
3610 it->region_end_charpos,
3611 &next_check_charpos,
3612 base_face_id, 0);
3613
3614 /* Correct the face for charsets different from ASCII. Do it
3615 for the multibyte case only. The face returned above is
3616 suitable for unibyte text if IT->string is unibyte. */
3617 if (STRING_MULTIBYTE (it->string))
3618 {
3619 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3620 int rest = SBYTES (it->string) - BYTEPOS (pos);
3621 int c, len;
3622 struct face *face = FACE_FROM_ID (it->f, face_id);
3623
3624 c = string_char_and_length (p, &len);
3625 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3626 }
3627 }
3628 else
3629 {
3630 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3631 || (IT_CHARPOS (*it) <= BEGV && before_p))
3632 return it->face_id;
3633
3634 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3635 pos = it->current.pos;
3636
3637 if (before_p)
3638 DEC_TEXT_POS (pos, it->multibyte_p);
3639 else
3640 {
3641 if (it->what == IT_COMPOSITION)
3642 /* For composition, we must check the position after the
3643 composition. */
3644 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3645 else
3646 INC_TEXT_POS (pos, it->multibyte_p);
3647 }
3648
3649 /* Determine face for CHARSET_ASCII, or unibyte. */
3650 face_id = face_at_buffer_position (it->w,
3651 CHARPOS (pos),
3652 it->region_beg_charpos,
3653 it->region_end_charpos,
3654 &next_check_charpos,
3655 limit, 0, -1);
3656
3657 /* Correct the face for charsets different from ASCII. Do it
3658 for the multibyte case only. The face returned above is
3659 suitable for unibyte text if current_buffer is unibyte. */
3660 if (it->multibyte_p)
3661 {
3662 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3663 struct face *face = FACE_FROM_ID (it->f, face_id);
3664 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3665 }
3666 }
3667
3668 return face_id;
3669 }
3670
3671
3672 \f
3673 /***********************************************************************
3674 Invisible text
3675 ***********************************************************************/
3676
3677 /* Set up iterator IT from invisible properties at its current
3678 position. Called from handle_stop. */
3679
3680 static enum prop_handled
3681 handle_invisible_prop (it)
3682 struct it *it;
3683 {
3684 enum prop_handled handled = HANDLED_NORMALLY;
3685
3686 if (STRINGP (it->string))
3687 {
3688 extern Lisp_Object Qinvisible;
3689 Lisp_Object prop, end_charpos, limit, charpos;
3690
3691 /* Get the value of the invisible text property at the
3692 current position. Value will be nil if there is no such
3693 property. */
3694 charpos = make_number (IT_STRING_CHARPOS (*it));
3695 prop = Fget_text_property (charpos, Qinvisible, it->string);
3696
3697 if (!NILP (prop)
3698 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3699 {
3700 handled = HANDLED_RECOMPUTE_PROPS;
3701
3702 /* Get the position at which the next change of the
3703 invisible text property can be found in IT->string.
3704 Value will be nil if the property value is the same for
3705 all the rest of IT->string. */
3706 XSETINT (limit, SCHARS (it->string));
3707 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3708 it->string, limit);
3709
3710 /* Text at current position is invisible. The next
3711 change in the property is at position end_charpos.
3712 Move IT's current position to that position. */
3713 if (INTEGERP (end_charpos)
3714 && XFASTINT (end_charpos) < XFASTINT (limit))
3715 {
3716 struct text_pos old;
3717 old = it->current.string_pos;
3718 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3719 compute_string_pos (&it->current.string_pos, old, it->string);
3720 }
3721 else
3722 {
3723 /* The rest of the string is invisible. If this is an
3724 overlay string, proceed with the next overlay string
3725 or whatever comes and return a character from there. */
3726 if (it->current.overlay_string_index >= 0)
3727 {
3728 next_overlay_string (it);
3729 /* Don't check for overlay strings when we just
3730 finished processing them. */
3731 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3732 }
3733 else
3734 {
3735 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3736 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3737 }
3738 }
3739 }
3740 }
3741 else
3742 {
3743 int invis_p;
3744 EMACS_INT newpos, next_stop, start_charpos, tem;
3745 Lisp_Object pos, prop, overlay;
3746
3747 /* First of all, is there invisible text at this position? */
3748 tem = start_charpos = IT_CHARPOS (*it);
3749 pos = make_number (tem);
3750 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3751 &overlay);
3752 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3753
3754 /* If we are on invisible text, skip over it. */
3755 if (invis_p && start_charpos < it->end_charpos)
3756 {
3757 /* Record whether we have to display an ellipsis for the
3758 invisible text. */
3759 int display_ellipsis_p = invis_p == 2;
3760
3761 handled = HANDLED_RECOMPUTE_PROPS;
3762
3763 /* Loop skipping over invisible text. The loop is left at
3764 ZV or with IT on the first char being visible again. */
3765 do
3766 {
3767 /* Try to skip some invisible text. Return value is the
3768 position reached which can be equal to where we start
3769 if there is nothing invisible there. This skips both
3770 over invisible text properties and overlays with
3771 invisible property. */
3772 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3773
3774 /* If we skipped nothing at all we weren't at invisible
3775 text in the first place. If everything to the end of
3776 the buffer was skipped, end the loop. */
3777 if (newpos == tem || newpos >= ZV)
3778 invis_p = 0;
3779 else
3780 {
3781 /* We skipped some characters but not necessarily
3782 all there are. Check if we ended up on visible
3783 text. Fget_char_property returns the property of
3784 the char before the given position, i.e. if we
3785 get invis_p = 0, this means that the char at
3786 newpos is visible. */
3787 pos = make_number (newpos);
3788 prop = Fget_char_property (pos, Qinvisible, it->window);
3789 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3790 }
3791
3792 /* If we ended up on invisible text, proceed to
3793 skip starting with next_stop. */
3794 if (invis_p)
3795 tem = next_stop;
3796
3797 /* If there are adjacent invisible texts, don't lose the
3798 second one's ellipsis. */
3799 if (invis_p == 2)
3800 display_ellipsis_p = 1;
3801 }
3802 while (invis_p);
3803
3804 /* The position newpos is now either ZV or on visible text. */
3805 if (it->bidi_p && newpos < ZV)
3806 {
3807 /* With bidi iteration, the region of invisible text
3808 could start and/or end in the middle of a non-base
3809 embedding level. Therefore, we need to skip
3810 invisible text using the bidi iterator, starting at
3811 IT's current position, until we find ourselves
3812 outside the invisible text. Skipping invisible text
3813 _after_ bidi iteration avoids affecting the visual
3814 order of the displayed text when invisible properties
3815 are added or removed. */
3816 if (it->bidi_it.first_elt)
3817 {
3818 /* If we were `reseat'ed to a new paragraph,
3819 determine the paragraph base direction. We need
3820 to do it now because next_element_from_buffer may
3821 not have a chance to do it, if we are going to
3822 skip any text at the beginning, which resets the
3823 FIRST_ELT flag. */
3824 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
3825 }
3826 do
3827 {
3828 bidi_get_next_char_visually (&it->bidi_it);
3829 }
3830 while (it->stop_charpos <= it->bidi_it.charpos
3831 && it->bidi_it.charpos < newpos);
3832 IT_CHARPOS (*it) = it->bidi_it.charpos;
3833 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3834 /* If we overstepped NEWPOS, record its position in the
3835 iterator, so that we skip invisible text if later the
3836 bidi iteration lands us in the invisible region
3837 again. */
3838 if (IT_CHARPOS (*it) >= newpos)
3839 it->prev_stop = newpos;
3840 }
3841 else
3842 {
3843 IT_CHARPOS (*it) = newpos;
3844 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3845 }
3846
3847 /* If there are before-strings at the start of invisible
3848 text, and the text is invisible because of a text
3849 property, arrange to show before-strings because 20.x did
3850 it that way. (If the text is invisible because of an
3851 overlay property instead of a text property, this is
3852 already handled in the overlay code.) */
3853 if (NILP (overlay)
3854 && get_overlay_strings (it, it->stop_charpos))
3855 {
3856 handled = HANDLED_RECOMPUTE_PROPS;
3857 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3858 }
3859 else if (display_ellipsis_p)
3860 {
3861 /* Make sure that the glyphs of the ellipsis will get
3862 correct `charpos' values. If we would not update
3863 it->position here, the glyphs would belong to the
3864 last visible character _before_ the invisible
3865 text, which confuses `set_cursor_from_row'.
3866
3867 We use the last invisible position instead of the
3868 first because this way the cursor is always drawn on
3869 the first "." of the ellipsis, whenever PT is inside
3870 the invisible text. Otherwise the cursor would be
3871 placed _after_ the ellipsis when the point is after the
3872 first invisible character. */
3873 if (!STRINGP (it->object))
3874 {
3875 it->position.charpos = newpos - 1;
3876 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3877 }
3878 it->ellipsis_p = 1;
3879 /* Let the ellipsis display before
3880 considering any properties of the following char.
3881 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3882 handled = HANDLED_RETURN;
3883 }
3884 }
3885 }
3886
3887 return handled;
3888 }
3889
3890
3891 /* Make iterator IT return `...' next.
3892 Replaces LEN characters from buffer. */
3893
3894 static void
3895 setup_for_ellipsis (it, len)
3896 struct it *it;
3897 int len;
3898 {
3899 /* Use the display table definition for `...'. Invalid glyphs
3900 will be handled by the method returning elements from dpvec. */
3901 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3902 {
3903 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3904 it->dpvec = v->contents;
3905 it->dpend = v->contents + v->size;
3906 }
3907 else
3908 {
3909 /* Default `...'. */
3910 it->dpvec = default_invis_vector;
3911 it->dpend = default_invis_vector + 3;
3912 }
3913
3914 it->dpvec_char_len = len;
3915 it->current.dpvec_index = 0;
3916 it->dpvec_face_id = -1;
3917
3918 /* Remember the current face id in case glyphs specify faces.
3919 IT's face is restored in set_iterator_to_next.
3920 saved_face_id was set to preceding char's face in handle_stop. */
3921 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3922 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3923
3924 it->method = GET_FROM_DISPLAY_VECTOR;
3925 it->ellipsis_p = 1;
3926 }
3927
3928
3929 \f
3930 /***********************************************************************
3931 'display' property
3932 ***********************************************************************/
3933
3934 /* Set up iterator IT from `display' property at its current position.
3935 Called from handle_stop.
3936 We return HANDLED_RETURN if some part of the display property
3937 overrides the display of the buffer text itself.
3938 Otherwise we return HANDLED_NORMALLY. */
3939
3940 static enum prop_handled
3941 handle_display_prop (it)
3942 struct it *it;
3943 {
3944 Lisp_Object prop, object, overlay;
3945 struct text_pos *position;
3946 /* Nonzero if some property replaces the display of the text itself. */
3947 int display_replaced_p = 0;
3948
3949 if (STRINGP (it->string))
3950 {
3951 object = it->string;
3952 position = &it->current.string_pos;
3953 }
3954 else
3955 {
3956 XSETWINDOW (object, it->w);
3957 position = &it->current.pos;
3958 }
3959
3960 /* Reset those iterator values set from display property values. */
3961 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3962 it->space_width = Qnil;
3963 it->font_height = Qnil;
3964 it->voffset = 0;
3965
3966 /* We don't support recursive `display' properties, i.e. string
3967 values that have a string `display' property, that have a string
3968 `display' property etc. */
3969 if (!it->string_from_display_prop_p)
3970 it->area = TEXT_AREA;
3971
3972 prop = get_char_property_and_overlay (make_number (position->charpos),
3973 Qdisplay, object, &overlay);
3974 if (NILP (prop))
3975 return HANDLED_NORMALLY;
3976 /* Now OVERLAY is the overlay that gave us this property, or nil
3977 if it was a text property. */
3978
3979 if (!STRINGP (it->string))
3980 object = it->w->buffer;
3981
3982 if (CONSP (prop)
3983 /* Simple properties. */
3984 && !EQ (XCAR (prop), Qimage)
3985 && !EQ (XCAR (prop), Qspace)
3986 && !EQ (XCAR (prop), Qwhen)
3987 && !EQ (XCAR (prop), Qslice)
3988 && !EQ (XCAR (prop), Qspace_width)
3989 && !EQ (XCAR (prop), Qheight)
3990 && !EQ (XCAR (prop), Qraise)
3991 /* Marginal area specifications. */
3992 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3993 && !EQ (XCAR (prop), Qleft_fringe)
3994 && !EQ (XCAR (prop), Qright_fringe)
3995 && !NILP (XCAR (prop)))
3996 {
3997 for (; CONSP (prop); prop = XCDR (prop))
3998 {
3999 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4000 position, display_replaced_p))
4001 {
4002 display_replaced_p = 1;
4003 /* If some text in a string is replaced, `position' no
4004 longer points to the position of `object'. */
4005 if (STRINGP (object))
4006 break;
4007 }
4008 }
4009 }
4010 else if (VECTORP (prop))
4011 {
4012 int i;
4013 for (i = 0; i < ASIZE (prop); ++i)
4014 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4015 position, display_replaced_p))
4016 {
4017 display_replaced_p = 1;
4018 /* If some text in a string is replaced, `position' no
4019 longer points to the position of `object'. */
4020 if (STRINGP (object))
4021 break;
4022 }
4023 }
4024 else
4025 {
4026 if (handle_single_display_spec (it, prop, object, overlay,
4027 position, 0))
4028 display_replaced_p = 1;
4029 }
4030
4031 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4032 }
4033
4034
4035 /* Value is the position of the end of the `display' property starting
4036 at START_POS in OBJECT. */
4037
4038 static struct text_pos
4039 display_prop_end (it, object, start_pos)
4040 struct it *it;
4041 Lisp_Object object;
4042 struct text_pos start_pos;
4043 {
4044 Lisp_Object end;
4045 struct text_pos end_pos;
4046
4047 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4048 Qdisplay, object, Qnil);
4049 CHARPOS (end_pos) = XFASTINT (end);
4050 if (STRINGP (object))
4051 compute_string_pos (&end_pos, start_pos, it->string);
4052 else
4053 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4054
4055 return end_pos;
4056 }
4057
4058
4059 /* Set up IT from a single `display' specification PROP. OBJECT
4060 is the object in which the `display' property was found. *POSITION
4061 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4062 means that we previously saw a display specification which already
4063 replaced text display with something else, for example an image;
4064 we ignore such properties after the first one has been processed.
4065
4066 OVERLAY is the overlay this `display' property came from,
4067 or nil if it was a text property.
4068
4069 If PROP is a `space' or `image' specification, and in some other
4070 cases too, set *POSITION to the position where the `display'
4071 property ends.
4072
4073 Value is non-zero if something was found which replaces the display
4074 of buffer or string text. */
4075
4076 static int
4077 handle_single_display_spec (it, spec, object, overlay, position,
4078 display_replaced_before_p)
4079 struct it *it;
4080 Lisp_Object spec;
4081 Lisp_Object object;
4082 Lisp_Object overlay;
4083 struct text_pos *position;
4084 int display_replaced_before_p;
4085 {
4086 Lisp_Object form;
4087 Lisp_Object location, value;
4088 struct text_pos start_pos, save_pos;
4089 int valid_p;
4090
4091 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4092 If the result is non-nil, use VALUE instead of SPEC. */
4093 form = Qt;
4094 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4095 {
4096 spec = XCDR (spec);
4097 if (!CONSP (spec))
4098 return 0;
4099 form = XCAR (spec);
4100 spec = XCDR (spec);
4101 }
4102
4103 if (!NILP (form) && !EQ (form, Qt))
4104 {
4105 int count = SPECPDL_INDEX ();
4106 struct gcpro gcpro1;
4107
4108 /* Bind `object' to the object having the `display' property, a
4109 buffer or string. Bind `position' to the position in the
4110 object where the property was found, and `buffer-position'
4111 to the current position in the buffer. */
4112 specbind (Qobject, object);
4113 specbind (Qposition, make_number (CHARPOS (*position)));
4114 specbind (Qbuffer_position,
4115 make_number (STRINGP (object)
4116 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4117 GCPRO1 (form);
4118 form = safe_eval (form);
4119 UNGCPRO;
4120 unbind_to (count, Qnil);
4121 }
4122
4123 if (NILP (form))
4124 return 0;
4125
4126 /* Handle `(height HEIGHT)' specifications. */
4127 if (CONSP (spec)
4128 && EQ (XCAR (spec), Qheight)
4129 && CONSP (XCDR (spec)))
4130 {
4131 if (!FRAME_WINDOW_P (it->f))
4132 return 0;
4133
4134 it->font_height = XCAR (XCDR (spec));
4135 if (!NILP (it->font_height))
4136 {
4137 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4138 int new_height = -1;
4139
4140 if (CONSP (it->font_height)
4141 && (EQ (XCAR (it->font_height), Qplus)
4142 || EQ (XCAR (it->font_height), Qminus))
4143 && CONSP (XCDR (it->font_height))
4144 && INTEGERP (XCAR (XCDR (it->font_height))))
4145 {
4146 /* `(+ N)' or `(- N)' where N is an integer. */
4147 int steps = XINT (XCAR (XCDR (it->font_height)));
4148 if (EQ (XCAR (it->font_height), Qplus))
4149 steps = - steps;
4150 it->face_id = smaller_face (it->f, it->face_id, steps);
4151 }
4152 else if (FUNCTIONP (it->font_height))
4153 {
4154 /* Call function with current height as argument.
4155 Value is the new height. */
4156 Lisp_Object height;
4157 height = safe_call1 (it->font_height,
4158 face->lface[LFACE_HEIGHT_INDEX]);
4159 if (NUMBERP (height))
4160 new_height = XFLOATINT (height);
4161 }
4162 else if (NUMBERP (it->font_height))
4163 {
4164 /* Value is a multiple of the canonical char height. */
4165 struct face *face;
4166
4167 face = FACE_FROM_ID (it->f,
4168 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4169 new_height = (XFLOATINT (it->font_height)
4170 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4171 }
4172 else
4173 {
4174 /* Evaluate IT->font_height with `height' bound to the
4175 current specified height to get the new height. */
4176 int count = SPECPDL_INDEX ();
4177
4178 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4179 value = safe_eval (it->font_height);
4180 unbind_to (count, Qnil);
4181
4182 if (NUMBERP (value))
4183 new_height = XFLOATINT (value);
4184 }
4185
4186 if (new_height > 0)
4187 it->face_id = face_with_height (it->f, it->face_id, new_height);
4188 }
4189
4190 return 0;
4191 }
4192
4193 /* Handle `(space-width WIDTH)'. */
4194 if (CONSP (spec)
4195 && EQ (XCAR (spec), Qspace_width)
4196 && CONSP (XCDR (spec)))
4197 {
4198 if (!FRAME_WINDOW_P (it->f))
4199 return 0;
4200
4201 value = XCAR (XCDR (spec));
4202 if (NUMBERP (value) && XFLOATINT (value) > 0)
4203 it->space_width = value;
4204
4205 return 0;
4206 }
4207
4208 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4209 if (CONSP (spec)
4210 && EQ (XCAR (spec), Qslice))
4211 {
4212 Lisp_Object tem;
4213
4214 if (!FRAME_WINDOW_P (it->f))
4215 return 0;
4216
4217 if (tem = XCDR (spec), CONSP (tem))
4218 {
4219 it->slice.x = XCAR (tem);
4220 if (tem = XCDR (tem), CONSP (tem))
4221 {
4222 it->slice.y = XCAR (tem);
4223 if (tem = XCDR (tem), CONSP (tem))
4224 {
4225 it->slice.width = XCAR (tem);
4226 if (tem = XCDR (tem), CONSP (tem))
4227 it->slice.height = XCAR (tem);
4228 }
4229 }
4230 }
4231
4232 return 0;
4233 }
4234
4235 /* Handle `(raise FACTOR)'. */
4236 if (CONSP (spec)
4237 && EQ (XCAR (spec), Qraise)
4238 && CONSP (XCDR (spec)))
4239 {
4240 if (!FRAME_WINDOW_P (it->f))
4241 return 0;
4242
4243 #ifdef HAVE_WINDOW_SYSTEM
4244 value = XCAR (XCDR (spec));
4245 if (NUMBERP (value))
4246 {
4247 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4248 it->voffset = - (XFLOATINT (value)
4249 * (FONT_HEIGHT (face->font)));
4250 }
4251 #endif /* HAVE_WINDOW_SYSTEM */
4252
4253 return 0;
4254 }
4255
4256 /* Don't handle the other kinds of display specifications
4257 inside a string that we got from a `display' property. */
4258 if (it->string_from_display_prop_p)
4259 return 0;
4260
4261 /* Characters having this form of property are not displayed, so
4262 we have to find the end of the property. */
4263 start_pos = *position;
4264 *position = display_prop_end (it, object, start_pos);
4265 value = Qnil;
4266
4267 /* Stop the scan at that end position--we assume that all
4268 text properties change there. */
4269 it->stop_charpos = position->charpos;
4270
4271 /* Handle `(left-fringe BITMAP [FACE])'
4272 and `(right-fringe BITMAP [FACE])'. */
4273 if (CONSP (spec)
4274 && (EQ (XCAR (spec), Qleft_fringe)
4275 || EQ (XCAR (spec), Qright_fringe))
4276 && CONSP (XCDR (spec)))
4277 {
4278 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4279 int fringe_bitmap;
4280
4281 if (!FRAME_WINDOW_P (it->f))
4282 /* If we return here, POSITION has been advanced
4283 across the text with this property. */
4284 return 0;
4285
4286 #ifdef HAVE_WINDOW_SYSTEM
4287 value = XCAR (XCDR (spec));
4288 if (!SYMBOLP (value)
4289 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4290 /* If we return here, POSITION has been advanced
4291 across the text with this property. */
4292 return 0;
4293
4294 if (CONSP (XCDR (XCDR (spec))))
4295 {
4296 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4297 int face_id2 = lookup_derived_face (it->f, face_name,
4298 FRINGE_FACE_ID, 0);
4299 if (face_id2 >= 0)
4300 face_id = face_id2;
4301 }
4302
4303 /* Save current settings of IT so that we can restore them
4304 when we are finished with the glyph property value. */
4305
4306 save_pos = it->position;
4307 it->position = *position;
4308 push_it (it);
4309 it->position = save_pos;
4310
4311 it->area = TEXT_AREA;
4312 it->what = IT_IMAGE;
4313 it->image_id = -1; /* no image */
4314 it->position = start_pos;
4315 it->object = NILP (object) ? it->w->buffer : object;
4316 it->method = GET_FROM_IMAGE;
4317 it->from_overlay = Qnil;
4318 it->face_id = face_id;
4319
4320 /* Say that we haven't consumed the characters with
4321 `display' property yet. The call to pop_it in
4322 set_iterator_to_next will clean this up. */
4323 *position = start_pos;
4324
4325 if (EQ (XCAR (spec), Qleft_fringe))
4326 {
4327 it->left_user_fringe_bitmap = fringe_bitmap;
4328 it->left_user_fringe_face_id = face_id;
4329 }
4330 else
4331 {
4332 it->right_user_fringe_bitmap = fringe_bitmap;
4333 it->right_user_fringe_face_id = face_id;
4334 }
4335 #endif /* HAVE_WINDOW_SYSTEM */
4336 return 1;
4337 }
4338
4339 /* Prepare to handle `((margin left-margin) ...)',
4340 `((margin right-margin) ...)' and `((margin nil) ...)'
4341 prefixes for display specifications. */
4342 location = Qunbound;
4343 if (CONSP (spec) && CONSP (XCAR (spec)))
4344 {
4345 Lisp_Object tem;
4346
4347 value = XCDR (spec);
4348 if (CONSP (value))
4349 value = XCAR (value);
4350
4351 tem = XCAR (spec);
4352 if (EQ (XCAR (tem), Qmargin)
4353 && (tem = XCDR (tem),
4354 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4355 (NILP (tem)
4356 || EQ (tem, Qleft_margin)
4357 || EQ (tem, Qright_margin))))
4358 location = tem;
4359 }
4360
4361 if (EQ (location, Qunbound))
4362 {
4363 location = Qnil;
4364 value = spec;
4365 }
4366
4367 /* After this point, VALUE is the property after any
4368 margin prefix has been stripped. It must be a string,
4369 an image specification, or `(space ...)'.
4370
4371 LOCATION specifies where to display: `left-margin',
4372 `right-margin' or nil. */
4373
4374 valid_p = (STRINGP (value)
4375 #ifdef HAVE_WINDOW_SYSTEM
4376 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4377 #endif /* not HAVE_WINDOW_SYSTEM */
4378 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4379
4380 if (valid_p && !display_replaced_before_p)
4381 {
4382 /* Save current settings of IT so that we can restore them
4383 when we are finished with the glyph property value. */
4384 save_pos = it->position;
4385 it->position = *position;
4386 push_it (it);
4387 it->position = save_pos;
4388 it->from_overlay = overlay;
4389
4390 if (NILP (location))
4391 it->area = TEXT_AREA;
4392 else if (EQ (location, Qleft_margin))
4393 it->area = LEFT_MARGIN_AREA;
4394 else
4395 it->area = RIGHT_MARGIN_AREA;
4396
4397 if (STRINGP (value))
4398 {
4399 it->string = value;
4400 it->multibyte_p = STRING_MULTIBYTE (it->string);
4401 it->current.overlay_string_index = -1;
4402 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4403 it->end_charpos = it->string_nchars = SCHARS (it->string);
4404 it->method = GET_FROM_STRING;
4405 it->stop_charpos = 0;
4406 it->string_from_display_prop_p = 1;
4407 /* Say that we haven't consumed the characters with
4408 `display' property yet. The call to pop_it in
4409 set_iterator_to_next will clean this up. */
4410 if (BUFFERP (object))
4411 *position = start_pos;
4412 }
4413 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4414 {
4415 it->method = GET_FROM_STRETCH;
4416 it->object = value;
4417 *position = it->position = start_pos;
4418 }
4419 #ifdef HAVE_WINDOW_SYSTEM
4420 else
4421 {
4422 it->what = IT_IMAGE;
4423 it->image_id = lookup_image (it->f, value);
4424 it->position = start_pos;
4425 it->object = NILP (object) ? it->w->buffer : object;
4426 it->method = GET_FROM_IMAGE;
4427
4428 /* Say that we haven't consumed the characters with
4429 `display' property yet. The call to pop_it in
4430 set_iterator_to_next will clean this up. */
4431 *position = start_pos;
4432 }
4433 #endif /* HAVE_WINDOW_SYSTEM */
4434
4435 return 1;
4436 }
4437
4438 /* Invalid property or property not supported. Restore
4439 POSITION to what it was before. */
4440 *position = start_pos;
4441 return 0;
4442 }
4443
4444
4445 /* Check if SPEC is a display sub-property value whose text should be
4446 treated as intangible. */
4447
4448 static int
4449 single_display_spec_intangible_p (prop)
4450 Lisp_Object prop;
4451 {
4452 /* Skip over `when FORM'. */
4453 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4454 {
4455 prop = XCDR (prop);
4456 if (!CONSP (prop))
4457 return 0;
4458 prop = XCDR (prop);
4459 }
4460
4461 if (STRINGP (prop))
4462 return 1;
4463
4464 if (!CONSP (prop))
4465 return 0;
4466
4467 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4468 we don't need to treat text as intangible. */
4469 if (EQ (XCAR (prop), Qmargin))
4470 {
4471 prop = XCDR (prop);
4472 if (!CONSP (prop))
4473 return 0;
4474
4475 prop = XCDR (prop);
4476 if (!CONSP (prop)
4477 || EQ (XCAR (prop), Qleft_margin)
4478 || EQ (XCAR (prop), Qright_margin))
4479 return 0;
4480 }
4481
4482 return (CONSP (prop)
4483 && (EQ (XCAR (prop), Qimage)
4484 || EQ (XCAR (prop), Qspace)));
4485 }
4486
4487
4488 /* Check if PROP is a display property value whose text should be
4489 treated as intangible. */
4490
4491 int
4492 display_prop_intangible_p (prop)
4493 Lisp_Object prop;
4494 {
4495 if (CONSP (prop)
4496 && CONSP (XCAR (prop))
4497 && !EQ (Qmargin, XCAR (XCAR (prop))))
4498 {
4499 /* A list of sub-properties. */
4500 while (CONSP (prop))
4501 {
4502 if (single_display_spec_intangible_p (XCAR (prop)))
4503 return 1;
4504 prop = XCDR (prop);
4505 }
4506 }
4507 else if (VECTORP (prop))
4508 {
4509 /* A vector of sub-properties. */
4510 int i;
4511 for (i = 0; i < ASIZE (prop); ++i)
4512 if (single_display_spec_intangible_p (AREF (prop, i)))
4513 return 1;
4514 }
4515 else
4516 return single_display_spec_intangible_p (prop);
4517
4518 return 0;
4519 }
4520
4521
4522 /* Return 1 if PROP is a display sub-property value containing STRING. */
4523
4524 static int
4525 single_display_spec_string_p (prop, string)
4526 Lisp_Object prop, string;
4527 {
4528 if (EQ (string, prop))
4529 return 1;
4530
4531 /* Skip over `when FORM'. */
4532 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4533 {
4534 prop = XCDR (prop);
4535 if (!CONSP (prop))
4536 return 0;
4537 prop = XCDR (prop);
4538 }
4539
4540 if (CONSP (prop))
4541 /* Skip over `margin LOCATION'. */
4542 if (EQ (XCAR (prop), Qmargin))
4543 {
4544 prop = XCDR (prop);
4545 if (!CONSP (prop))
4546 return 0;
4547
4548 prop = XCDR (prop);
4549 if (!CONSP (prop))
4550 return 0;
4551 }
4552
4553 return CONSP (prop) && EQ (XCAR (prop), string);
4554 }
4555
4556
4557 /* Return 1 if STRING appears in the `display' property PROP. */
4558
4559 static int
4560 display_prop_string_p (prop, string)
4561 Lisp_Object prop, string;
4562 {
4563 if (CONSP (prop)
4564 && CONSP (XCAR (prop))
4565 && !EQ (Qmargin, XCAR (XCAR (prop))))
4566 {
4567 /* A list of sub-properties. */
4568 while (CONSP (prop))
4569 {
4570 if (single_display_spec_string_p (XCAR (prop), string))
4571 return 1;
4572 prop = XCDR (prop);
4573 }
4574 }
4575 else if (VECTORP (prop))
4576 {
4577 /* A vector of sub-properties. */
4578 int i;
4579 for (i = 0; i < ASIZE (prop); ++i)
4580 if (single_display_spec_string_p (AREF (prop, i), string))
4581 return 1;
4582 }
4583 else
4584 return single_display_spec_string_p (prop, string);
4585
4586 return 0;
4587 }
4588
4589 /* Look for STRING in overlays and text properties in W's buffer,
4590 between character positions FROM and TO (excluding TO).
4591 BACK_P non-zero means look back (in this case, TO is supposed to be
4592 less than FROM).
4593 Value is the first character position where STRING was found, or
4594 zero if it wasn't found before hitting TO.
4595
4596 W's buffer must be current.
4597
4598 This function may only use code that doesn't eval because it is
4599 called asynchronously from note_mouse_highlight. */
4600
4601 static EMACS_INT
4602 string_buffer_position_lim (w, string, from, to, back_p)
4603 struct window *w;
4604 Lisp_Object string;
4605 EMACS_INT from, to;
4606 int back_p;
4607 {
4608 Lisp_Object limit, prop, pos;
4609 int found = 0;
4610
4611 pos = make_number (from);
4612
4613 if (!back_p) /* looking forward */
4614 {
4615 limit = make_number (min (to, ZV));
4616 while (!found && !EQ (pos, limit))
4617 {
4618 prop = Fget_char_property (pos, Qdisplay, Qnil);
4619 if (!NILP (prop) && display_prop_string_p (prop, string))
4620 found = 1;
4621 else
4622 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4623 limit);
4624 }
4625 }
4626 else /* looking back */
4627 {
4628 limit = make_number (max (to, BEGV));
4629 while (!found && !EQ (pos, limit))
4630 {
4631 prop = Fget_char_property (pos, Qdisplay, Qnil);
4632 if (!NILP (prop) && display_prop_string_p (prop, string))
4633 found = 1;
4634 else
4635 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4636 limit);
4637 }
4638 }
4639
4640 return found ? XINT (pos) : 0;
4641 }
4642
4643 /* Determine which buffer position in W's buffer STRING comes from.
4644 AROUND_CHARPOS is an approximate position where it could come from.
4645 Value is the buffer position or 0 if it couldn't be determined.
4646
4647 W's buffer must be current.
4648
4649 This function is necessary because we don't record buffer positions
4650 in glyphs generated from strings (to keep struct glyph small).
4651 This function may only use code that doesn't eval because it is
4652 called asynchronously from note_mouse_highlight. */
4653
4654 EMACS_INT
4655 string_buffer_position (w, string, around_charpos)
4656 struct window *w;
4657 Lisp_Object string;
4658 EMACS_INT around_charpos;
4659 {
4660 Lisp_Object limit, prop, pos;
4661 const int MAX_DISTANCE = 1000;
4662 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4663 around_charpos + MAX_DISTANCE,
4664 0);
4665
4666 if (!found)
4667 found = string_buffer_position_lim (w, string, around_charpos,
4668 around_charpos - MAX_DISTANCE, 1);
4669 return found;
4670 }
4671
4672
4673 \f
4674 /***********************************************************************
4675 `composition' property
4676 ***********************************************************************/
4677
4678 /* Set up iterator IT from `composition' property at its current
4679 position. Called from handle_stop. */
4680
4681 static enum prop_handled
4682 handle_composition_prop (it)
4683 struct it *it;
4684 {
4685 Lisp_Object prop, string;
4686 EMACS_INT pos, pos_byte, start, end;
4687
4688 if (STRINGP (it->string))
4689 {
4690 unsigned char *s;
4691
4692 pos = IT_STRING_CHARPOS (*it);
4693 pos_byte = IT_STRING_BYTEPOS (*it);
4694 string = it->string;
4695 s = SDATA (string) + pos_byte;
4696 it->c = STRING_CHAR (s);
4697 }
4698 else
4699 {
4700 pos = IT_CHARPOS (*it);
4701 pos_byte = IT_BYTEPOS (*it);
4702 string = Qnil;
4703 it->c = FETCH_CHAR (pos_byte);
4704 }
4705
4706 /* If there's a valid composition and point is not inside of the
4707 composition (in the case that the composition is from the current
4708 buffer), draw a glyph composed from the composition components. */
4709 if (find_composition (pos, -1, &start, &end, &prop, string)
4710 && COMPOSITION_VALID_P (start, end, prop)
4711 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4712 {
4713 if (start != pos)
4714 {
4715 if (STRINGP (it->string))
4716 pos_byte = string_char_to_byte (it->string, start);
4717 else
4718 pos_byte = CHAR_TO_BYTE (start);
4719 }
4720 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4721 prop, string);
4722
4723 if (it->cmp_it.id >= 0)
4724 {
4725 it->cmp_it.ch = -1;
4726 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4727 it->cmp_it.nglyphs = -1;
4728 }
4729 }
4730
4731 return HANDLED_NORMALLY;
4732 }
4733
4734
4735 \f
4736 /***********************************************************************
4737 Overlay strings
4738 ***********************************************************************/
4739
4740 /* The following structure is used to record overlay strings for
4741 later sorting in load_overlay_strings. */
4742
4743 struct overlay_entry
4744 {
4745 Lisp_Object overlay;
4746 Lisp_Object string;
4747 int priority;
4748 int after_string_p;
4749 };
4750
4751
4752 /* Set up iterator IT from overlay strings at its current position.
4753 Called from handle_stop. */
4754
4755 static enum prop_handled
4756 handle_overlay_change (it)
4757 struct it *it;
4758 {
4759 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4760 return HANDLED_RECOMPUTE_PROPS;
4761 else
4762 return HANDLED_NORMALLY;
4763 }
4764
4765
4766 /* Set up the next overlay string for delivery by IT, if there is an
4767 overlay string to deliver. Called by set_iterator_to_next when the
4768 end of the current overlay string is reached. If there are more
4769 overlay strings to display, IT->string and
4770 IT->current.overlay_string_index are set appropriately here.
4771 Otherwise IT->string is set to nil. */
4772
4773 static void
4774 next_overlay_string (it)
4775 struct it *it;
4776 {
4777 ++it->current.overlay_string_index;
4778 if (it->current.overlay_string_index == it->n_overlay_strings)
4779 {
4780 /* No more overlay strings. Restore IT's settings to what
4781 they were before overlay strings were processed, and
4782 continue to deliver from current_buffer. */
4783
4784 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4785 pop_it (it);
4786 xassert (it->sp > 0
4787 || (NILP (it->string)
4788 && it->method == GET_FROM_BUFFER
4789 && it->stop_charpos >= BEGV
4790 && it->stop_charpos <= it->end_charpos));
4791 it->current.overlay_string_index = -1;
4792 it->n_overlay_strings = 0;
4793
4794 /* If we're at the end of the buffer, record that we have
4795 processed the overlay strings there already, so that
4796 next_element_from_buffer doesn't try it again. */
4797 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4798 it->overlay_strings_at_end_processed_p = 1;
4799 }
4800 else
4801 {
4802 /* There are more overlay strings to process. If
4803 IT->current.overlay_string_index has advanced to a position
4804 where we must load IT->overlay_strings with more strings, do
4805 it. */
4806 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4807
4808 if (it->current.overlay_string_index && i == 0)
4809 load_overlay_strings (it, 0);
4810
4811 /* Initialize IT to deliver display elements from the overlay
4812 string. */
4813 it->string = it->overlay_strings[i];
4814 it->multibyte_p = STRING_MULTIBYTE (it->string);
4815 SET_TEXT_POS (it->current.string_pos, 0, 0);
4816 it->method = GET_FROM_STRING;
4817 it->stop_charpos = 0;
4818 if (it->cmp_it.stop_pos >= 0)
4819 it->cmp_it.stop_pos = 0;
4820 }
4821
4822 CHECK_IT (it);
4823 }
4824
4825
4826 /* Compare two overlay_entry structures E1 and E2. Used as a
4827 comparison function for qsort in load_overlay_strings. Overlay
4828 strings for the same position are sorted so that
4829
4830 1. All after-strings come in front of before-strings, except
4831 when they come from the same overlay.
4832
4833 2. Within after-strings, strings are sorted so that overlay strings
4834 from overlays with higher priorities come first.
4835
4836 2. Within before-strings, strings are sorted so that overlay
4837 strings from overlays with higher priorities come last.
4838
4839 Value is analogous to strcmp. */
4840
4841
4842 static int
4843 compare_overlay_entries (e1, e2)
4844 void *e1, *e2;
4845 {
4846 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4847 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4848 int result;
4849
4850 if (entry1->after_string_p != entry2->after_string_p)
4851 {
4852 /* Let after-strings appear in front of before-strings if
4853 they come from different overlays. */
4854 if (EQ (entry1->overlay, entry2->overlay))
4855 result = entry1->after_string_p ? 1 : -1;
4856 else
4857 result = entry1->after_string_p ? -1 : 1;
4858 }
4859 else if (entry1->after_string_p)
4860 /* After-strings sorted in order of decreasing priority. */
4861 result = entry2->priority - entry1->priority;
4862 else
4863 /* Before-strings sorted in order of increasing priority. */
4864 result = entry1->priority - entry2->priority;
4865
4866 return result;
4867 }
4868
4869
4870 /* Load the vector IT->overlay_strings with overlay strings from IT's
4871 current buffer position, or from CHARPOS if that is > 0. Set
4872 IT->n_overlays to the total number of overlay strings found.
4873
4874 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4875 a time. On entry into load_overlay_strings,
4876 IT->current.overlay_string_index gives the number of overlay
4877 strings that have already been loaded by previous calls to this
4878 function.
4879
4880 IT->add_overlay_start contains an additional overlay start
4881 position to consider for taking overlay strings from, if non-zero.
4882 This position comes into play when the overlay has an `invisible'
4883 property, and both before and after-strings. When we've skipped to
4884 the end of the overlay, because of its `invisible' property, we
4885 nevertheless want its before-string to appear.
4886 IT->add_overlay_start will contain the overlay start position
4887 in this case.
4888
4889 Overlay strings are sorted so that after-string strings come in
4890 front of before-string strings. Within before and after-strings,
4891 strings are sorted by overlay priority. See also function
4892 compare_overlay_entries. */
4893
4894 static void
4895 load_overlay_strings (it, charpos)
4896 struct it *it;
4897 int charpos;
4898 {
4899 extern Lisp_Object Qwindow, Qpriority;
4900 Lisp_Object overlay, window, str, invisible;
4901 struct Lisp_Overlay *ov;
4902 int start, end;
4903 int size = 20;
4904 int n = 0, i, j, invis_p;
4905 struct overlay_entry *entries
4906 = (struct overlay_entry *) alloca (size * sizeof *entries);
4907
4908 if (charpos <= 0)
4909 charpos = IT_CHARPOS (*it);
4910
4911 /* Append the overlay string STRING of overlay OVERLAY to vector
4912 `entries' which has size `size' and currently contains `n'
4913 elements. AFTER_P non-zero means STRING is an after-string of
4914 OVERLAY. */
4915 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4916 do \
4917 { \
4918 Lisp_Object priority; \
4919 \
4920 if (n == size) \
4921 { \
4922 int new_size = 2 * size; \
4923 struct overlay_entry *old = entries; \
4924 entries = \
4925 (struct overlay_entry *) alloca (new_size \
4926 * sizeof *entries); \
4927 bcopy (old, entries, size * sizeof *entries); \
4928 size = new_size; \
4929 } \
4930 \
4931 entries[n].string = (STRING); \
4932 entries[n].overlay = (OVERLAY); \
4933 priority = Foverlay_get ((OVERLAY), Qpriority); \
4934 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4935 entries[n].after_string_p = (AFTER_P); \
4936 ++n; \
4937 } \
4938 while (0)
4939
4940 /* Process overlay before the overlay center. */
4941 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4942 {
4943 XSETMISC (overlay, ov);
4944 xassert (OVERLAYP (overlay));
4945 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4946 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4947
4948 if (end < charpos)
4949 break;
4950
4951 /* Skip this overlay if it doesn't start or end at IT's current
4952 position. */
4953 if (end != charpos && start != charpos)
4954 continue;
4955
4956 /* Skip this overlay if it doesn't apply to IT->w. */
4957 window = Foverlay_get (overlay, Qwindow);
4958 if (WINDOWP (window) && XWINDOW (window) != it->w)
4959 continue;
4960
4961 /* If the text ``under'' the overlay is invisible, both before-
4962 and after-strings from this overlay are visible; start and
4963 end position are indistinguishable. */
4964 invisible = Foverlay_get (overlay, Qinvisible);
4965 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4966
4967 /* If overlay has a non-empty before-string, record it. */
4968 if ((start == charpos || (end == charpos && invis_p))
4969 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4970 && SCHARS (str))
4971 RECORD_OVERLAY_STRING (overlay, str, 0);
4972
4973 /* If overlay has a non-empty after-string, record it. */
4974 if ((end == charpos || (start == charpos && invis_p))
4975 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4976 && SCHARS (str))
4977 RECORD_OVERLAY_STRING (overlay, str, 1);
4978 }
4979
4980 /* Process overlays after the overlay center. */
4981 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4982 {
4983 XSETMISC (overlay, ov);
4984 xassert (OVERLAYP (overlay));
4985 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4986 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4987
4988 if (start > charpos)
4989 break;
4990
4991 /* Skip this overlay if it doesn't start or end at IT's current
4992 position. */
4993 if (end != charpos && start != charpos)
4994 continue;
4995
4996 /* Skip this overlay if it doesn't apply to IT->w. */
4997 window = Foverlay_get (overlay, Qwindow);
4998 if (WINDOWP (window) && XWINDOW (window) != it->w)
4999 continue;
5000
5001 /* If the text ``under'' the overlay is invisible, it has a zero
5002 dimension, and both before- and after-strings apply. */
5003 invisible = Foverlay_get (overlay, Qinvisible);
5004 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5005
5006 /* If overlay has a non-empty before-string, record it. */
5007 if ((start == charpos || (end == charpos && invis_p))
5008 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5009 && SCHARS (str))
5010 RECORD_OVERLAY_STRING (overlay, str, 0);
5011
5012 /* If overlay has a non-empty after-string, record it. */
5013 if ((end == charpos || (start == charpos && invis_p))
5014 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5015 && SCHARS (str))
5016 RECORD_OVERLAY_STRING (overlay, str, 1);
5017 }
5018
5019 #undef RECORD_OVERLAY_STRING
5020
5021 /* Sort entries. */
5022 if (n > 1)
5023 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5024
5025 /* Record the total number of strings to process. */
5026 it->n_overlay_strings = n;
5027
5028 /* IT->current.overlay_string_index is the number of overlay strings
5029 that have already been consumed by IT. Copy some of the
5030 remaining overlay strings to IT->overlay_strings. */
5031 i = 0;
5032 j = it->current.overlay_string_index;
5033 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5034 {
5035 it->overlay_strings[i] = entries[j].string;
5036 it->string_overlays[i++] = entries[j++].overlay;
5037 }
5038
5039 CHECK_IT (it);
5040 }
5041
5042
5043 /* Get the first chunk of overlay strings at IT's current buffer
5044 position, or at CHARPOS if that is > 0. Value is non-zero if at
5045 least one overlay string was found. */
5046
5047 static int
5048 get_overlay_strings_1 (it, charpos, compute_stop_p)
5049 struct it *it;
5050 int charpos;
5051 int compute_stop_p;
5052 {
5053 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5054 process. This fills IT->overlay_strings with strings, and sets
5055 IT->n_overlay_strings to the total number of strings to process.
5056 IT->pos.overlay_string_index has to be set temporarily to zero
5057 because load_overlay_strings needs this; it must be set to -1
5058 when no overlay strings are found because a zero value would
5059 indicate a position in the first overlay string. */
5060 it->current.overlay_string_index = 0;
5061 load_overlay_strings (it, charpos);
5062
5063 /* If we found overlay strings, set up IT to deliver display
5064 elements from the first one. Otherwise set up IT to deliver
5065 from current_buffer. */
5066 if (it->n_overlay_strings)
5067 {
5068 /* Make sure we know settings in current_buffer, so that we can
5069 restore meaningful values when we're done with the overlay
5070 strings. */
5071 if (compute_stop_p)
5072 compute_stop_pos (it);
5073 xassert (it->face_id >= 0);
5074
5075 /* Save IT's settings. They are restored after all overlay
5076 strings have been processed. */
5077 xassert (!compute_stop_p || it->sp == 0);
5078
5079 /* When called from handle_stop, there might be an empty display
5080 string loaded. In that case, don't bother saving it. */
5081 if (!STRINGP (it->string) || SCHARS (it->string))
5082 push_it (it);
5083
5084 /* Set up IT to deliver display elements from the first overlay
5085 string. */
5086 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5087 it->string = it->overlay_strings[0];
5088 it->from_overlay = Qnil;
5089 it->stop_charpos = 0;
5090 xassert (STRINGP (it->string));
5091 it->end_charpos = SCHARS (it->string);
5092 it->multibyte_p = STRING_MULTIBYTE (it->string);
5093 it->method = GET_FROM_STRING;
5094 return 1;
5095 }
5096
5097 it->current.overlay_string_index = -1;
5098 return 0;
5099 }
5100
5101 static int
5102 get_overlay_strings (it, charpos)
5103 struct it *it;
5104 int charpos;
5105 {
5106 it->string = Qnil;
5107 it->method = GET_FROM_BUFFER;
5108
5109 (void) get_overlay_strings_1 (it, charpos, 1);
5110
5111 CHECK_IT (it);
5112
5113 /* Value is non-zero if we found at least one overlay string. */
5114 return STRINGP (it->string);
5115 }
5116
5117
5118 \f
5119 /***********************************************************************
5120 Saving and restoring state
5121 ***********************************************************************/
5122
5123 /* Save current settings of IT on IT->stack. Called, for example,
5124 before setting up IT for an overlay string, to be able to restore
5125 IT's settings to what they were after the overlay string has been
5126 processed. */
5127
5128 static void
5129 push_it (it)
5130 struct it *it;
5131 {
5132 struct iterator_stack_entry *p;
5133
5134 xassert (it->sp < IT_STACK_SIZE);
5135 p = it->stack + it->sp;
5136
5137 p->stop_charpos = it->stop_charpos;
5138 p->prev_stop = it->prev_stop;
5139 p->base_level_stop = it->base_level_stop;
5140 p->cmp_it = it->cmp_it;
5141 xassert (it->face_id >= 0);
5142 p->face_id = it->face_id;
5143 p->string = it->string;
5144 p->method = it->method;
5145 p->from_overlay = it->from_overlay;
5146 switch (p->method)
5147 {
5148 case GET_FROM_IMAGE:
5149 p->u.image.object = it->object;
5150 p->u.image.image_id = it->image_id;
5151 p->u.image.slice = it->slice;
5152 break;
5153 case GET_FROM_STRETCH:
5154 p->u.stretch.object = it->object;
5155 break;
5156 }
5157 p->position = it->position;
5158 p->current = it->current;
5159 p->end_charpos = it->end_charpos;
5160 p->string_nchars = it->string_nchars;
5161 p->area = it->area;
5162 p->multibyte_p = it->multibyte_p;
5163 p->avoid_cursor_p = it->avoid_cursor_p;
5164 p->space_width = it->space_width;
5165 p->font_height = it->font_height;
5166 p->voffset = it->voffset;
5167 p->string_from_display_prop_p = it->string_from_display_prop_p;
5168 p->display_ellipsis_p = 0;
5169 p->line_wrap = it->line_wrap;
5170 ++it->sp;
5171 }
5172
5173
5174 /* Restore IT's settings from IT->stack. Called, for example, when no
5175 more overlay strings must be processed, and we return to delivering
5176 display elements from a buffer, or when the end of a string from a
5177 `display' property is reached and we return to delivering display
5178 elements from an overlay string, or from a buffer. */
5179
5180 static void
5181 pop_it (it)
5182 struct it *it;
5183 {
5184 struct iterator_stack_entry *p;
5185
5186 xassert (it->sp > 0);
5187 --it->sp;
5188 p = it->stack + it->sp;
5189 it->stop_charpos = p->stop_charpos;
5190 it->prev_stop = p->prev_stop;
5191 it->base_level_stop = p->base_level_stop;
5192 it->cmp_it = p->cmp_it;
5193 it->face_id = p->face_id;
5194 it->current = p->current;
5195 it->position = p->position;
5196 it->string = p->string;
5197 it->from_overlay = p->from_overlay;
5198 if (NILP (it->string))
5199 SET_TEXT_POS (it->current.string_pos, -1, -1);
5200 it->method = p->method;
5201 switch (it->method)
5202 {
5203 case GET_FROM_IMAGE:
5204 it->image_id = p->u.image.image_id;
5205 it->object = p->u.image.object;
5206 it->slice = p->u.image.slice;
5207 break;
5208 case GET_FROM_STRETCH:
5209 it->object = p->u.comp.object;
5210 break;
5211 case GET_FROM_BUFFER:
5212 it->object = it->w->buffer;
5213 break;
5214 case GET_FROM_STRING:
5215 it->object = it->string;
5216 break;
5217 case GET_FROM_DISPLAY_VECTOR:
5218 if (it->s)
5219 it->method = GET_FROM_C_STRING;
5220 else if (STRINGP (it->string))
5221 it->method = GET_FROM_STRING;
5222 else
5223 {
5224 it->method = GET_FROM_BUFFER;
5225 it->object = it->w->buffer;
5226 }
5227 }
5228 it->end_charpos = p->end_charpos;
5229 it->string_nchars = p->string_nchars;
5230 it->area = p->area;
5231 it->multibyte_p = p->multibyte_p;
5232 it->avoid_cursor_p = p->avoid_cursor_p;
5233 it->space_width = p->space_width;
5234 it->font_height = p->font_height;
5235 it->voffset = p->voffset;
5236 it->string_from_display_prop_p = p->string_from_display_prop_p;
5237 it->line_wrap = p->line_wrap;
5238 }
5239
5240
5241 \f
5242 /***********************************************************************
5243 Moving over lines
5244 ***********************************************************************/
5245
5246 /* Set IT's current position to the previous line start. */
5247
5248 static void
5249 back_to_previous_line_start (it)
5250 struct it *it;
5251 {
5252 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5253 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5254 }
5255
5256
5257 /* Move IT to the next line start.
5258
5259 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5260 we skipped over part of the text (as opposed to moving the iterator
5261 continuously over the text). Otherwise, don't change the value
5262 of *SKIPPED_P.
5263
5264 Newlines may come from buffer text, overlay strings, or strings
5265 displayed via the `display' property. That's the reason we can't
5266 simply use find_next_newline_no_quit.
5267
5268 Note that this function may not skip over invisible text that is so
5269 because of text properties and immediately follows a newline. If
5270 it would, function reseat_at_next_visible_line_start, when called
5271 from set_iterator_to_next, would effectively make invisible
5272 characters following a newline part of the wrong glyph row, which
5273 leads to wrong cursor motion. */
5274
5275 static int
5276 forward_to_next_line_start (it, skipped_p)
5277 struct it *it;
5278 int *skipped_p;
5279 {
5280 int old_selective, newline_found_p, n;
5281 const int MAX_NEWLINE_DISTANCE = 500;
5282
5283 /* If already on a newline, just consume it to avoid unintended
5284 skipping over invisible text below. */
5285 if (it->what == IT_CHARACTER
5286 && it->c == '\n'
5287 && CHARPOS (it->position) == IT_CHARPOS (*it))
5288 {
5289 set_iterator_to_next (it, 0);
5290 it->c = 0;
5291 return 1;
5292 }
5293
5294 /* Don't handle selective display in the following. It's (a)
5295 unnecessary because it's done by the caller, and (b) leads to an
5296 infinite recursion because next_element_from_ellipsis indirectly
5297 calls this function. */
5298 old_selective = it->selective;
5299 it->selective = 0;
5300
5301 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5302 from buffer text. */
5303 for (n = newline_found_p = 0;
5304 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5305 n += STRINGP (it->string) ? 0 : 1)
5306 {
5307 if (!get_next_display_element (it))
5308 return 0;
5309 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5310 set_iterator_to_next (it, 0);
5311 }
5312
5313 /* If we didn't find a newline near enough, see if we can use a
5314 short-cut. */
5315 if (!newline_found_p)
5316 {
5317 int start = IT_CHARPOS (*it);
5318 int limit = find_next_newline_no_quit (start, 1);
5319 Lisp_Object pos;
5320
5321 xassert (!STRINGP (it->string));
5322
5323 /* If there isn't any `display' property in sight, and no
5324 overlays, we can just use the position of the newline in
5325 buffer text. */
5326 if (it->stop_charpos >= limit
5327 || ((pos = Fnext_single_property_change (make_number (start),
5328 Qdisplay,
5329 Qnil, make_number (limit)),
5330 NILP (pos))
5331 && next_overlay_change (start) == ZV))
5332 {
5333 IT_CHARPOS (*it) = limit;
5334 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5335 *skipped_p = newline_found_p = 1;
5336 }
5337 else
5338 {
5339 while (get_next_display_element (it)
5340 && !newline_found_p)
5341 {
5342 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5343 set_iterator_to_next (it, 0);
5344 }
5345 }
5346 }
5347
5348 it->selective = old_selective;
5349 return newline_found_p;
5350 }
5351
5352
5353 /* Set IT's current position to the previous visible line start. Skip
5354 invisible text that is so either due to text properties or due to
5355 selective display. Caution: this does not change IT->current_x and
5356 IT->hpos. */
5357
5358 static void
5359 back_to_previous_visible_line_start (it)
5360 struct it *it;
5361 {
5362 while (IT_CHARPOS (*it) > BEGV)
5363 {
5364 back_to_previous_line_start (it);
5365
5366 if (IT_CHARPOS (*it) <= BEGV)
5367 break;
5368
5369 /* If selective > 0, then lines indented more than its value are
5370 invisible. */
5371 if (it->selective > 0
5372 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5373 (double) it->selective)) /* iftc */
5374 continue;
5375
5376 /* Check the newline before point for invisibility. */
5377 {
5378 Lisp_Object prop;
5379 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5380 Qinvisible, it->window);
5381 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5382 continue;
5383 }
5384
5385 if (IT_CHARPOS (*it) <= BEGV)
5386 break;
5387
5388 {
5389 struct it it2;
5390 int pos;
5391 EMACS_INT beg, end;
5392 Lisp_Object val, overlay;
5393
5394 /* If newline is part of a composition, continue from start of composition */
5395 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5396 && beg < IT_CHARPOS (*it))
5397 goto replaced;
5398
5399 /* If newline is replaced by a display property, find start of overlay
5400 or interval and continue search from that point. */
5401 it2 = *it;
5402 pos = --IT_CHARPOS (it2);
5403 --IT_BYTEPOS (it2);
5404 it2.sp = 0;
5405 it2.string_from_display_prop_p = 0;
5406 if (handle_display_prop (&it2) == HANDLED_RETURN
5407 && !NILP (val = get_char_property_and_overlay
5408 (make_number (pos), Qdisplay, Qnil, &overlay))
5409 && (OVERLAYP (overlay)
5410 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5411 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5412 goto replaced;
5413
5414 /* Newline is not replaced by anything -- so we are done. */
5415 break;
5416
5417 replaced:
5418 if (beg < BEGV)
5419 beg = BEGV;
5420 IT_CHARPOS (*it) = beg;
5421 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5422 }
5423 }
5424
5425 it->continuation_lines_width = 0;
5426
5427 xassert (IT_CHARPOS (*it) >= BEGV);
5428 xassert (IT_CHARPOS (*it) == BEGV
5429 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5430 CHECK_IT (it);
5431 }
5432
5433
5434 /* Reseat iterator IT at the previous visible line start. Skip
5435 invisible text that is so either due to text properties or due to
5436 selective display. At the end, update IT's overlay information,
5437 face information etc. */
5438
5439 void
5440 reseat_at_previous_visible_line_start (it)
5441 struct it *it;
5442 {
5443 back_to_previous_visible_line_start (it);
5444 reseat (it, it->current.pos, 1);
5445 CHECK_IT (it);
5446 }
5447
5448
5449 /* Reseat iterator IT on the next visible line start in the current
5450 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5451 preceding the line start. Skip over invisible text that is so
5452 because of selective display. Compute faces, overlays etc at the
5453 new position. Note that this function does not skip over text that
5454 is invisible because of text properties. */
5455
5456 static void
5457 reseat_at_next_visible_line_start (it, on_newline_p)
5458 struct it *it;
5459 int on_newline_p;
5460 {
5461 int newline_found_p, skipped_p = 0;
5462
5463 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5464
5465 /* Skip over lines that are invisible because they are indented
5466 more than the value of IT->selective. */
5467 if (it->selective > 0)
5468 while (IT_CHARPOS (*it) < ZV
5469 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5470 (double) it->selective)) /* iftc */
5471 {
5472 xassert (IT_BYTEPOS (*it) == BEGV
5473 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5474 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5475 }
5476
5477 /* Position on the newline if that's what's requested. */
5478 if (on_newline_p && newline_found_p)
5479 {
5480 if (STRINGP (it->string))
5481 {
5482 if (IT_STRING_CHARPOS (*it) > 0)
5483 {
5484 --IT_STRING_CHARPOS (*it);
5485 --IT_STRING_BYTEPOS (*it);
5486 }
5487 }
5488 else if (IT_CHARPOS (*it) > BEGV)
5489 {
5490 --IT_CHARPOS (*it);
5491 --IT_BYTEPOS (*it);
5492 reseat (it, it->current.pos, 0);
5493 }
5494 }
5495 else if (skipped_p)
5496 reseat (it, it->current.pos, 0);
5497
5498 CHECK_IT (it);
5499 }
5500
5501
5502 \f
5503 /***********************************************************************
5504 Changing an iterator's position
5505 ***********************************************************************/
5506
5507 /* Change IT's current position to POS in current_buffer. If FORCE_P
5508 is non-zero, always check for text properties at the new position.
5509 Otherwise, text properties are only looked up if POS >=
5510 IT->check_charpos of a property. */
5511
5512 static void
5513 reseat (it, pos, force_p)
5514 struct it *it;
5515 struct text_pos pos;
5516 int force_p;
5517 {
5518 int original_pos = IT_CHARPOS (*it);
5519
5520 reseat_1 (it, pos, 0);
5521
5522 /* Determine where to check text properties. Avoid doing it
5523 where possible because text property lookup is very expensive. */
5524 if (force_p
5525 || CHARPOS (pos) > it->stop_charpos
5526 || CHARPOS (pos) < original_pos)
5527 {
5528 if (it->bidi_p)
5529 {
5530 /* For bidi iteration, we need to prime prev_stop and
5531 base_level_stop with our best estimations. */
5532 if (CHARPOS (pos) < it->prev_stop)
5533 {
5534 handle_stop_backwards (it, BEGV);
5535 if (CHARPOS (pos) < it->base_level_stop)
5536 it->base_level_stop = 0;
5537 }
5538 else if (CHARPOS (pos) > it->stop_charpos
5539 && it->stop_charpos >= BEGV)
5540 handle_stop_backwards (it, it->stop_charpos);
5541 else /* force_p */
5542 handle_stop (it);
5543 }
5544 else
5545 {
5546 handle_stop (it);
5547 it->prev_stop = it->base_level_stop = 0;
5548 }
5549
5550 }
5551
5552 CHECK_IT (it);
5553 }
5554
5555
5556 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5557 IT->stop_pos to POS, also. */
5558
5559 static void
5560 reseat_1 (it, pos, set_stop_p)
5561 struct it *it;
5562 struct text_pos pos;
5563 int set_stop_p;
5564 {
5565 /* Don't call this function when scanning a C string. */
5566 xassert (it->s == NULL);
5567
5568 /* POS must be a reasonable value. */
5569 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5570
5571 it->current.pos = it->position = pos;
5572 it->end_charpos = ZV;
5573 it->dpvec = NULL;
5574 it->current.dpvec_index = -1;
5575 it->current.overlay_string_index = -1;
5576 IT_STRING_CHARPOS (*it) = -1;
5577 IT_STRING_BYTEPOS (*it) = -1;
5578 it->string = Qnil;
5579 it->string_from_display_prop_p = 0;
5580 it->method = GET_FROM_BUFFER;
5581 it->object = it->w->buffer;
5582 it->area = TEXT_AREA;
5583 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5584 it->sp = 0;
5585 it->string_from_display_prop_p = 0;
5586 it->face_before_selective_p = 0;
5587 if (it->bidi_p)
5588 it->bidi_it.first_elt = 1;
5589
5590 if (set_stop_p)
5591 {
5592 it->stop_charpos = CHARPOS (pos);
5593 it->base_level_stop = CHARPOS (pos);
5594 }
5595 }
5596
5597
5598 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5599 If S is non-null, it is a C string to iterate over. Otherwise,
5600 STRING gives a Lisp string to iterate over.
5601
5602 If PRECISION > 0, don't return more then PRECISION number of
5603 characters from the string.
5604
5605 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5606 characters have been returned. FIELD_WIDTH < 0 means an infinite
5607 field width.
5608
5609 MULTIBYTE = 0 means disable processing of multibyte characters,
5610 MULTIBYTE > 0 means enable it,
5611 MULTIBYTE < 0 means use IT->multibyte_p.
5612
5613 IT must be initialized via a prior call to init_iterator before
5614 calling this function. */
5615
5616 static void
5617 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5618 struct it *it;
5619 unsigned char *s;
5620 Lisp_Object string;
5621 int charpos;
5622 int precision, field_width, multibyte;
5623 {
5624 /* No region in strings. */
5625 it->region_beg_charpos = it->region_end_charpos = -1;
5626
5627 /* No text property checks performed by default, but see below. */
5628 it->stop_charpos = -1;
5629
5630 /* Set iterator position and end position. */
5631 bzero (&it->current, sizeof it->current);
5632 it->current.overlay_string_index = -1;
5633 it->current.dpvec_index = -1;
5634 xassert (charpos >= 0);
5635
5636 /* If STRING is specified, use its multibyteness, otherwise use the
5637 setting of MULTIBYTE, if specified. */
5638 if (multibyte >= 0)
5639 it->multibyte_p = multibyte > 0;
5640
5641 if (s == NULL)
5642 {
5643 xassert (STRINGP (string));
5644 it->string = string;
5645 it->s = NULL;
5646 it->end_charpos = it->string_nchars = SCHARS (string);
5647 it->method = GET_FROM_STRING;
5648 it->current.string_pos = string_pos (charpos, string);
5649 }
5650 else
5651 {
5652 it->s = s;
5653 it->string = Qnil;
5654
5655 /* Note that we use IT->current.pos, not it->current.string_pos,
5656 for displaying C strings. */
5657 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5658 if (it->multibyte_p)
5659 {
5660 it->current.pos = c_string_pos (charpos, s, 1);
5661 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5662 }
5663 else
5664 {
5665 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5666 it->end_charpos = it->string_nchars = strlen (s);
5667 }
5668
5669 it->method = GET_FROM_C_STRING;
5670 }
5671
5672 /* PRECISION > 0 means don't return more than PRECISION characters
5673 from the string. */
5674 if (precision > 0 && it->end_charpos - charpos > precision)
5675 it->end_charpos = it->string_nchars = charpos + precision;
5676
5677 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5678 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5679 FIELD_WIDTH < 0 means infinite field width. This is useful for
5680 padding with `-' at the end of a mode line. */
5681 if (field_width < 0)
5682 field_width = INFINITY;
5683 if (field_width > it->end_charpos - charpos)
5684 it->end_charpos = charpos + field_width;
5685
5686 /* Use the standard display table for displaying strings. */
5687 if (DISP_TABLE_P (Vstandard_display_table))
5688 it->dp = XCHAR_TABLE (Vstandard_display_table);
5689
5690 it->stop_charpos = charpos;
5691 if (s == NULL && it->multibyte_p)
5692 {
5693 EMACS_INT endpos = SCHARS (it->string);
5694 if (endpos > it->end_charpos)
5695 endpos = it->end_charpos;
5696 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5697 it->string);
5698 }
5699 CHECK_IT (it);
5700 }
5701
5702
5703 \f
5704 /***********************************************************************
5705 Iteration
5706 ***********************************************************************/
5707
5708 /* Map enum it_method value to corresponding next_element_from_* function. */
5709
5710 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5711 {
5712 next_element_from_buffer,
5713 next_element_from_display_vector,
5714 next_element_from_string,
5715 next_element_from_c_string,
5716 next_element_from_image,
5717 next_element_from_stretch
5718 };
5719
5720 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5721
5722
5723 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5724 (possibly with the following characters). */
5725
5726 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5727 ((IT)->cmp_it.id >= 0 \
5728 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5729 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5730 END_CHARPOS, (IT)->w, \
5731 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5732 (IT)->string)))
5733
5734
5735 /* Load IT's display element fields with information about the next
5736 display element from the current position of IT. Value is zero if
5737 end of buffer (or C string) is reached. */
5738
5739 static struct frame *last_escape_glyph_frame = NULL;
5740 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5741 static int last_escape_glyph_merged_face_id = 0;
5742
5743 int
5744 get_next_display_element (it)
5745 struct it *it;
5746 {
5747 /* Non-zero means that we found a display element. Zero means that
5748 we hit the end of what we iterate over. Performance note: the
5749 function pointer `method' used here turns out to be faster than
5750 using a sequence of if-statements. */
5751 int success_p;
5752
5753 get_next:
5754 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5755
5756 if (it->what == IT_CHARACTER)
5757 {
5758 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5759 and only if (a) the resolved directionality of that character
5760 is R..." */
5761 /* FIXME: Do we need an exception for characters from display
5762 tables? */
5763 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5764 it->c = bidi_mirror_char (it->c);
5765 /* Map via display table or translate control characters.
5766 IT->c, IT->len etc. have been set to the next character by
5767 the function call above. If we have a display table, and it
5768 contains an entry for IT->c, translate it. Don't do this if
5769 IT->c itself comes from a display table, otherwise we could
5770 end up in an infinite recursion. (An alternative could be to
5771 count the recursion depth of this function and signal an
5772 error when a certain maximum depth is reached.) Is it worth
5773 it? */
5774 if (success_p && it->dpvec == NULL)
5775 {
5776 Lisp_Object dv;
5777 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5778 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5779 nbsp_or_shy = char_is_other;
5780 int decoded = it->c;
5781
5782 if (it->dp
5783 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5784 VECTORP (dv)))
5785 {
5786 struct Lisp_Vector *v = XVECTOR (dv);
5787
5788 /* Return the first character from the display table
5789 entry, if not empty. If empty, don't display the
5790 current character. */
5791 if (v->size)
5792 {
5793 it->dpvec_char_len = it->len;
5794 it->dpvec = v->contents;
5795 it->dpend = v->contents + v->size;
5796 it->current.dpvec_index = 0;
5797 it->dpvec_face_id = -1;
5798 it->saved_face_id = it->face_id;
5799 it->method = GET_FROM_DISPLAY_VECTOR;
5800 it->ellipsis_p = 0;
5801 }
5802 else
5803 {
5804 set_iterator_to_next (it, 0);
5805 }
5806 goto get_next;
5807 }
5808
5809 if (unibyte_display_via_language_environment
5810 && !ASCII_CHAR_P (it->c))
5811 decoded = DECODE_CHAR (unibyte, it->c);
5812
5813 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5814 {
5815 if (it->multibyte_p)
5816 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5817 : it->c == 0xAD ? char_is_soft_hyphen
5818 : char_is_other);
5819 else if (unibyte_display_via_language_environment)
5820 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5821 : decoded == 0xAD ? char_is_soft_hyphen
5822 : char_is_other);
5823 }
5824
5825 /* Translate control characters into `\003' or `^C' form.
5826 Control characters coming from a display table entry are
5827 currently not translated because we use IT->dpvec to hold
5828 the translation. This could easily be changed but I
5829 don't believe that it is worth doing.
5830
5831 If it->multibyte_p is nonzero, non-printable non-ASCII
5832 characters are also translated to octal form.
5833
5834 If it->multibyte_p is zero, eight-bit characters that
5835 don't have corresponding multibyte char code are also
5836 translated to octal form. */
5837 if ((it->c < ' '
5838 ? (it->area != TEXT_AREA
5839 /* In mode line, treat \n, \t like other crl chars. */
5840 || (it->c != '\t'
5841 && it->glyph_row
5842 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5843 || (it->c != '\n' && it->c != '\t'))
5844 : (nbsp_or_shy
5845 || (it->multibyte_p
5846 ? ! CHAR_PRINTABLE_P (it->c)
5847 : (! unibyte_display_via_language_environment
5848 ? it->c >= 0x80
5849 : (decoded >= 0x80 && decoded < 0xA0))))))
5850 {
5851 /* IT->c is a control character which must be displayed
5852 either as '\003' or as `^C' where the '\\' and '^'
5853 can be defined in the display table. Fill
5854 IT->ctl_chars with glyphs for what we have to
5855 display. Then, set IT->dpvec to these glyphs. */
5856 Lisp_Object gc;
5857 int ctl_len;
5858 int face_id, lface_id = 0 ;
5859 int escape_glyph;
5860
5861 /* Handle control characters with ^. */
5862
5863 if (it->c < 128 && it->ctl_arrow_p)
5864 {
5865 int g;
5866
5867 g = '^'; /* default glyph for Control */
5868 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5869 if (it->dp
5870 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5871 && GLYPH_CODE_CHAR_VALID_P (gc))
5872 {
5873 g = GLYPH_CODE_CHAR (gc);
5874 lface_id = GLYPH_CODE_FACE (gc);
5875 }
5876 if (lface_id)
5877 {
5878 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5879 }
5880 else if (it->f == last_escape_glyph_frame
5881 && it->face_id == last_escape_glyph_face_id)
5882 {
5883 face_id = last_escape_glyph_merged_face_id;
5884 }
5885 else
5886 {
5887 /* Merge the escape-glyph face into the current face. */
5888 face_id = merge_faces (it->f, Qescape_glyph, 0,
5889 it->face_id);
5890 last_escape_glyph_frame = it->f;
5891 last_escape_glyph_face_id = it->face_id;
5892 last_escape_glyph_merged_face_id = face_id;
5893 }
5894
5895 XSETINT (it->ctl_chars[0], g);
5896 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5897 ctl_len = 2;
5898 goto display_control;
5899 }
5900
5901 /* Handle non-break space in the mode where it only gets
5902 highlighting. */
5903
5904 if (EQ (Vnobreak_char_display, Qt)
5905 && nbsp_or_shy == char_is_nbsp)
5906 {
5907 /* Merge the no-break-space face into the current face. */
5908 face_id = merge_faces (it->f, Qnobreak_space, 0,
5909 it->face_id);
5910
5911 it->c = ' ';
5912 XSETINT (it->ctl_chars[0], ' ');
5913 ctl_len = 1;
5914 goto display_control;
5915 }
5916
5917 /* Handle sequences that start with the "escape glyph". */
5918
5919 /* the default escape glyph is \. */
5920 escape_glyph = '\\';
5921
5922 if (it->dp
5923 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5924 && GLYPH_CODE_CHAR_VALID_P (gc))
5925 {
5926 escape_glyph = GLYPH_CODE_CHAR (gc);
5927 lface_id = GLYPH_CODE_FACE (gc);
5928 }
5929 if (lface_id)
5930 {
5931 /* The display table specified a face.
5932 Merge it into face_id and also into escape_glyph. */
5933 face_id = merge_faces (it->f, Qt, lface_id,
5934 it->face_id);
5935 }
5936 else if (it->f == last_escape_glyph_frame
5937 && it->face_id == last_escape_glyph_face_id)
5938 {
5939 face_id = last_escape_glyph_merged_face_id;
5940 }
5941 else
5942 {
5943 /* Merge the escape-glyph face into the current face. */
5944 face_id = merge_faces (it->f, Qescape_glyph, 0,
5945 it->face_id);
5946 last_escape_glyph_frame = it->f;
5947 last_escape_glyph_face_id = it->face_id;
5948 last_escape_glyph_merged_face_id = face_id;
5949 }
5950
5951 /* Handle soft hyphens in the mode where they only get
5952 highlighting. */
5953
5954 if (EQ (Vnobreak_char_display, Qt)
5955 && nbsp_or_shy == char_is_soft_hyphen)
5956 {
5957 it->c = '-';
5958 XSETINT (it->ctl_chars[0], '-');
5959 ctl_len = 1;
5960 goto display_control;
5961 }
5962
5963 /* Handle non-break space and soft hyphen
5964 with the escape glyph. */
5965
5966 if (nbsp_or_shy)
5967 {
5968 XSETINT (it->ctl_chars[0], escape_glyph);
5969 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5970 XSETINT (it->ctl_chars[1], it->c);
5971 ctl_len = 2;
5972 goto display_control;
5973 }
5974
5975 {
5976 unsigned char str[MAX_MULTIBYTE_LENGTH];
5977 int len;
5978 int i;
5979
5980 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5981 if (CHAR_BYTE8_P (it->c))
5982 {
5983 str[0] = CHAR_TO_BYTE8 (it->c);
5984 len = 1;
5985 }
5986 else if (it->c < 256)
5987 {
5988 str[0] = it->c;
5989 len = 1;
5990 }
5991 else
5992 {
5993 /* It's an invalid character, which shouldn't
5994 happen actually, but due to bugs it may
5995 happen. Let's print the char as is, there's
5996 not much meaningful we can do with it. */
5997 str[0] = it->c;
5998 str[1] = it->c >> 8;
5999 str[2] = it->c >> 16;
6000 str[3] = it->c >> 24;
6001 len = 4;
6002 }
6003
6004 for (i = 0; i < len; i++)
6005 {
6006 int g;
6007 XSETINT (it->ctl_chars[i * 4], escape_glyph);
6008 /* Insert three more glyphs into IT->ctl_chars for
6009 the octal display of the character. */
6010 g = ((str[i] >> 6) & 7) + '0';
6011 XSETINT (it->ctl_chars[i * 4 + 1], g);
6012 g = ((str[i] >> 3) & 7) + '0';
6013 XSETINT (it->ctl_chars[i * 4 + 2], g);
6014 g = (str[i] & 7) + '0';
6015 XSETINT (it->ctl_chars[i * 4 + 3], g);
6016 }
6017 ctl_len = len * 4;
6018 }
6019
6020 display_control:
6021 /* Set up IT->dpvec and return first character from it. */
6022 it->dpvec_char_len = it->len;
6023 it->dpvec = it->ctl_chars;
6024 it->dpend = it->dpvec + ctl_len;
6025 it->current.dpvec_index = 0;
6026 it->dpvec_face_id = face_id;
6027 it->saved_face_id = it->face_id;
6028 it->method = GET_FROM_DISPLAY_VECTOR;
6029 it->ellipsis_p = 0;
6030 goto get_next;
6031 }
6032 }
6033 }
6034
6035 #ifdef HAVE_WINDOW_SYSTEM
6036 /* Adjust face id for a multibyte character. There are no multibyte
6037 character in unibyte text. */
6038 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6039 && it->multibyte_p
6040 && success_p
6041 && FRAME_WINDOW_P (it->f))
6042 {
6043 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6044
6045 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6046 {
6047 /* Automatic composition with glyph-string. */
6048 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6049
6050 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6051 }
6052 else
6053 {
6054 int pos = (it->s ? -1
6055 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6056 : IT_CHARPOS (*it));
6057
6058 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6059 }
6060 }
6061 #endif
6062
6063 /* Is this character the last one of a run of characters with
6064 box? If yes, set IT->end_of_box_run_p to 1. */
6065 if (it->face_box_p
6066 && it->s == NULL)
6067 {
6068 if (it->method == GET_FROM_STRING && it->sp)
6069 {
6070 int face_id = underlying_face_id (it);
6071 struct face *face = FACE_FROM_ID (it->f, face_id);
6072
6073 if (face)
6074 {
6075 if (face->box == FACE_NO_BOX)
6076 {
6077 /* If the box comes from face properties in a
6078 display string, check faces in that string. */
6079 int string_face_id = face_after_it_pos (it);
6080 it->end_of_box_run_p
6081 = (FACE_FROM_ID (it->f, string_face_id)->box
6082 == FACE_NO_BOX);
6083 }
6084 /* Otherwise, the box comes from the underlying face.
6085 If this is the last string character displayed, check
6086 the next buffer location. */
6087 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6088 && (it->current.overlay_string_index
6089 == it->n_overlay_strings - 1))
6090 {
6091 EMACS_INT ignore;
6092 int next_face_id;
6093 struct text_pos pos = it->current.pos;
6094 INC_TEXT_POS (pos, it->multibyte_p);
6095
6096 next_face_id = face_at_buffer_position
6097 (it->w, CHARPOS (pos), it->region_beg_charpos,
6098 it->region_end_charpos, &ignore,
6099 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6100 -1);
6101 it->end_of_box_run_p
6102 = (FACE_FROM_ID (it->f, next_face_id)->box
6103 == FACE_NO_BOX);
6104 }
6105 }
6106 }
6107 else
6108 {
6109 int face_id = face_after_it_pos (it);
6110 it->end_of_box_run_p
6111 = (face_id != it->face_id
6112 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6113 }
6114 }
6115
6116 /* Value is 0 if end of buffer or string reached. */
6117 return success_p;
6118 }
6119
6120
6121 /* Move IT to the next display element.
6122
6123 RESEAT_P non-zero means if called on a newline in buffer text,
6124 skip to the next visible line start.
6125
6126 Functions get_next_display_element and set_iterator_to_next are
6127 separate because I find this arrangement easier to handle than a
6128 get_next_display_element function that also increments IT's
6129 position. The way it is we can first look at an iterator's current
6130 display element, decide whether it fits on a line, and if it does,
6131 increment the iterator position. The other way around we probably
6132 would either need a flag indicating whether the iterator has to be
6133 incremented the next time, or we would have to implement a
6134 decrement position function which would not be easy to write. */
6135
6136 void
6137 set_iterator_to_next (it, reseat_p)
6138 struct it *it;
6139 int reseat_p;
6140 {
6141 /* Reset flags indicating start and end of a sequence of characters
6142 with box. Reset them at the start of this function because
6143 moving the iterator to a new position might set them. */
6144 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6145
6146 switch (it->method)
6147 {
6148 case GET_FROM_BUFFER:
6149 /* The current display element of IT is a character from
6150 current_buffer. Advance in the buffer, and maybe skip over
6151 invisible lines that are so because of selective display. */
6152 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6153 reseat_at_next_visible_line_start (it, 0);
6154 else if (it->cmp_it.id >= 0)
6155 {
6156 IT_CHARPOS (*it) += it->cmp_it.nchars;
6157 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6158 if (it->cmp_it.to < it->cmp_it.nglyphs)
6159 it->cmp_it.from = it->cmp_it.to;
6160 else
6161 {
6162 it->cmp_it.id = -1;
6163 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6164 IT_BYTEPOS (*it), it->stop_charpos,
6165 Qnil);
6166 }
6167 }
6168 else
6169 {
6170 xassert (it->len != 0);
6171
6172 if (!it->bidi_p)
6173 {
6174 IT_BYTEPOS (*it) += it->len;
6175 IT_CHARPOS (*it) += 1;
6176 }
6177 else
6178 {
6179 /* If this is a new paragraph, determine its base
6180 direction (a.k.a. its base embedding level). */
6181 if (it->bidi_it.new_paragraph)
6182 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6183 bidi_get_next_char_visually (&it->bidi_it);
6184 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6185 IT_CHARPOS (*it) = it->bidi_it.charpos;
6186 }
6187 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6188 }
6189 break;
6190
6191 case GET_FROM_C_STRING:
6192 /* Current display element of IT is from a C string. */
6193 IT_BYTEPOS (*it) += it->len;
6194 IT_CHARPOS (*it) += 1;
6195 break;
6196
6197 case GET_FROM_DISPLAY_VECTOR:
6198 /* Current display element of IT is from a display table entry.
6199 Advance in the display table definition. Reset it to null if
6200 end reached, and continue with characters from buffers/
6201 strings. */
6202 ++it->current.dpvec_index;
6203
6204 /* Restore face of the iterator to what they were before the
6205 display vector entry (these entries may contain faces). */
6206 it->face_id = it->saved_face_id;
6207
6208 if (it->dpvec + it->current.dpvec_index == it->dpend)
6209 {
6210 int recheck_faces = it->ellipsis_p;
6211
6212 if (it->s)
6213 it->method = GET_FROM_C_STRING;
6214 else if (STRINGP (it->string))
6215 it->method = GET_FROM_STRING;
6216 else
6217 {
6218 it->method = GET_FROM_BUFFER;
6219 it->object = it->w->buffer;
6220 }
6221
6222 it->dpvec = NULL;
6223 it->current.dpvec_index = -1;
6224
6225 /* Skip over characters which were displayed via IT->dpvec. */
6226 if (it->dpvec_char_len < 0)
6227 reseat_at_next_visible_line_start (it, 1);
6228 else if (it->dpvec_char_len > 0)
6229 {
6230 if (it->method == GET_FROM_STRING
6231 && it->n_overlay_strings > 0)
6232 it->ignore_overlay_strings_at_pos_p = 1;
6233 it->len = it->dpvec_char_len;
6234 set_iterator_to_next (it, reseat_p);
6235 }
6236
6237 /* Maybe recheck faces after display vector */
6238 if (recheck_faces)
6239 it->stop_charpos = IT_CHARPOS (*it);
6240 }
6241 break;
6242
6243 case GET_FROM_STRING:
6244 /* Current display element is a character from a Lisp string. */
6245 xassert (it->s == NULL && STRINGP (it->string));
6246 if (it->cmp_it.id >= 0)
6247 {
6248 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6249 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6250 if (it->cmp_it.to < it->cmp_it.nglyphs)
6251 it->cmp_it.from = it->cmp_it.to;
6252 else
6253 {
6254 it->cmp_it.id = -1;
6255 composition_compute_stop_pos (&it->cmp_it,
6256 IT_STRING_CHARPOS (*it),
6257 IT_STRING_BYTEPOS (*it),
6258 it->stop_charpos, it->string);
6259 }
6260 }
6261 else
6262 {
6263 IT_STRING_BYTEPOS (*it) += it->len;
6264 IT_STRING_CHARPOS (*it) += 1;
6265 }
6266
6267 consider_string_end:
6268
6269 if (it->current.overlay_string_index >= 0)
6270 {
6271 /* IT->string is an overlay string. Advance to the
6272 next, if there is one. */
6273 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6274 {
6275 it->ellipsis_p = 0;
6276 next_overlay_string (it);
6277 if (it->ellipsis_p)
6278 setup_for_ellipsis (it, 0);
6279 }
6280 }
6281 else
6282 {
6283 /* IT->string is not an overlay string. If we reached
6284 its end, and there is something on IT->stack, proceed
6285 with what is on the stack. This can be either another
6286 string, this time an overlay string, or a buffer. */
6287 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6288 && it->sp > 0)
6289 {
6290 pop_it (it);
6291 if (it->method == GET_FROM_STRING)
6292 goto consider_string_end;
6293 }
6294 }
6295 break;
6296
6297 case GET_FROM_IMAGE:
6298 case GET_FROM_STRETCH:
6299 /* The position etc with which we have to proceed are on
6300 the stack. The position may be at the end of a string,
6301 if the `display' property takes up the whole string. */
6302 xassert (it->sp > 0);
6303 pop_it (it);
6304 if (it->method == GET_FROM_STRING)
6305 goto consider_string_end;
6306 break;
6307
6308 default:
6309 /* There are no other methods defined, so this should be a bug. */
6310 abort ();
6311 }
6312
6313 xassert (it->method != GET_FROM_STRING
6314 || (STRINGP (it->string)
6315 && IT_STRING_CHARPOS (*it) >= 0));
6316 }
6317
6318 /* Load IT's display element fields with information about the next
6319 display element which comes from a display table entry or from the
6320 result of translating a control character to one of the forms `^C'
6321 or `\003'.
6322
6323 IT->dpvec holds the glyphs to return as characters.
6324 IT->saved_face_id holds the face id before the display vector--it
6325 is restored into IT->face_id in set_iterator_to_next. */
6326
6327 static int
6328 next_element_from_display_vector (it)
6329 struct it *it;
6330 {
6331 Lisp_Object gc;
6332
6333 /* Precondition. */
6334 xassert (it->dpvec && it->current.dpvec_index >= 0);
6335
6336 it->face_id = it->saved_face_id;
6337
6338 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6339 That seemed totally bogus - so I changed it... */
6340 gc = it->dpvec[it->current.dpvec_index];
6341
6342 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6343 {
6344 it->c = GLYPH_CODE_CHAR (gc);
6345 it->len = CHAR_BYTES (it->c);
6346
6347 /* The entry may contain a face id to use. Such a face id is
6348 the id of a Lisp face, not a realized face. A face id of
6349 zero means no face is specified. */
6350 if (it->dpvec_face_id >= 0)
6351 it->face_id = it->dpvec_face_id;
6352 else
6353 {
6354 int lface_id = GLYPH_CODE_FACE (gc);
6355 if (lface_id > 0)
6356 it->face_id = merge_faces (it->f, Qt, lface_id,
6357 it->saved_face_id);
6358 }
6359 }
6360 else
6361 /* Display table entry is invalid. Return a space. */
6362 it->c = ' ', it->len = 1;
6363
6364 /* Don't change position and object of the iterator here. They are
6365 still the values of the character that had this display table
6366 entry or was translated, and that's what we want. */
6367 it->what = IT_CHARACTER;
6368 return 1;
6369 }
6370
6371
6372 /* Load IT with the next display element from Lisp string IT->string.
6373 IT->current.string_pos is the current position within the string.
6374 If IT->current.overlay_string_index >= 0, the Lisp string is an
6375 overlay string. */
6376
6377 static int
6378 next_element_from_string (it)
6379 struct it *it;
6380 {
6381 struct text_pos position;
6382
6383 xassert (STRINGP (it->string));
6384 xassert (IT_STRING_CHARPOS (*it) >= 0);
6385 position = it->current.string_pos;
6386
6387 /* Time to check for invisible text? */
6388 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6389 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6390 {
6391 handle_stop (it);
6392
6393 /* Since a handler may have changed IT->method, we must
6394 recurse here. */
6395 return GET_NEXT_DISPLAY_ELEMENT (it);
6396 }
6397
6398 if (it->current.overlay_string_index >= 0)
6399 {
6400 /* Get the next character from an overlay string. In overlay
6401 strings, There is no field width or padding with spaces to
6402 do. */
6403 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6404 {
6405 it->what = IT_EOB;
6406 return 0;
6407 }
6408 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6409 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6410 && next_element_from_composition (it))
6411 {
6412 return 1;
6413 }
6414 else if (STRING_MULTIBYTE (it->string))
6415 {
6416 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6417 const unsigned char *s = (SDATA (it->string)
6418 + IT_STRING_BYTEPOS (*it));
6419 it->c = string_char_and_length (s, &it->len);
6420 }
6421 else
6422 {
6423 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6424 it->len = 1;
6425 }
6426 }
6427 else
6428 {
6429 /* Get the next character from a Lisp string that is not an
6430 overlay string. Such strings come from the mode line, for
6431 example. We may have to pad with spaces, or truncate the
6432 string. See also next_element_from_c_string. */
6433 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6434 {
6435 it->what = IT_EOB;
6436 return 0;
6437 }
6438 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6439 {
6440 /* Pad with spaces. */
6441 it->c = ' ', it->len = 1;
6442 CHARPOS (position) = BYTEPOS (position) = -1;
6443 }
6444 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6445 IT_STRING_BYTEPOS (*it), it->string_nchars)
6446 && next_element_from_composition (it))
6447 {
6448 return 1;
6449 }
6450 else if (STRING_MULTIBYTE (it->string))
6451 {
6452 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6453 const unsigned char *s = (SDATA (it->string)
6454 + IT_STRING_BYTEPOS (*it));
6455 it->c = string_char_and_length (s, &it->len);
6456 }
6457 else
6458 {
6459 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6460 it->len = 1;
6461 }
6462 }
6463
6464 /* Record what we have and where it came from. */
6465 it->what = IT_CHARACTER;
6466 it->object = it->string;
6467 it->position = position;
6468 return 1;
6469 }
6470
6471
6472 /* Load IT with next display element from C string IT->s.
6473 IT->string_nchars is the maximum number of characters to return
6474 from the string. IT->end_charpos may be greater than
6475 IT->string_nchars when this function is called, in which case we
6476 may have to return padding spaces. Value is zero if end of string
6477 reached, including padding spaces. */
6478
6479 static int
6480 next_element_from_c_string (it)
6481 struct it *it;
6482 {
6483 int success_p = 1;
6484
6485 xassert (it->s);
6486 it->what = IT_CHARACTER;
6487 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6488 it->object = Qnil;
6489
6490 /* IT's position can be greater IT->string_nchars in case a field
6491 width or precision has been specified when the iterator was
6492 initialized. */
6493 if (IT_CHARPOS (*it) >= it->end_charpos)
6494 {
6495 /* End of the game. */
6496 it->what = IT_EOB;
6497 success_p = 0;
6498 }
6499 else if (IT_CHARPOS (*it) >= it->string_nchars)
6500 {
6501 /* Pad with spaces. */
6502 it->c = ' ', it->len = 1;
6503 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6504 }
6505 else if (it->multibyte_p)
6506 {
6507 /* Implementation note: The calls to strlen apparently aren't a
6508 performance problem because there is no noticeable performance
6509 difference between Emacs running in unibyte or multibyte mode. */
6510 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6511 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6512 }
6513 else
6514 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6515
6516 return success_p;
6517 }
6518
6519
6520 /* Set up IT to return characters from an ellipsis, if appropriate.
6521 The definition of the ellipsis glyphs may come from a display table
6522 entry. This function fills IT with the first glyph from the
6523 ellipsis if an ellipsis is to be displayed. */
6524
6525 static int
6526 next_element_from_ellipsis (it)
6527 struct it *it;
6528 {
6529 if (it->selective_display_ellipsis_p)
6530 setup_for_ellipsis (it, it->len);
6531 else
6532 {
6533 /* The face at the current position may be different from the
6534 face we find after the invisible text. Remember what it
6535 was in IT->saved_face_id, and signal that it's there by
6536 setting face_before_selective_p. */
6537 it->saved_face_id = it->face_id;
6538 it->method = GET_FROM_BUFFER;
6539 it->object = it->w->buffer;
6540 reseat_at_next_visible_line_start (it, 1);
6541 it->face_before_selective_p = 1;
6542 }
6543
6544 return GET_NEXT_DISPLAY_ELEMENT (it);
6545 }
6546
6547
6548 /* Deliver an image display element. The iterator IT is already
6549 filled with image information (done in handle_display_prop). Value
6550 is always 1. */
6551
6552
6553 static int
6554 next_element_from_image (it)
6555 struct it *it;
6556 {
6557 it->what = IT_IMAGE;
6558 return 1;
6559 }
6560
6561
6562 /* Fill iterator IT with next display element from a stretch glyph
6563 property. IT->object is the value of the text property. Value is
6564 always 1. */
6565
6566 static int
6567 next_element_from_stretch (it)
6568 struct it *it;
6569 {
6570 it->what = IT_STRETCH;
6571 return 1;
6572 }
6573
6574 /* Scan forward from CHARPOS in the current buffer, until we find a
6575 stop position > current IT's position. Then handle the stop
6576 position before that. This is called when we bump into a stop
6577 position while reordering bidirectional text. CHARPOS should be
6578 the last previously processed stop_pos (or BEGV, if none were
6579 processed yet) whose position is less that IT's current
6580 position. */
6581
6582 static void
6583 handle_stop_backwards (it, charpos)
6584 struct it *it;
6585 EMACS_INT charpos;
6586 {
6587 EMACS_INT where_we_are = IT_CHARPOS (*it);
6588 struct display_pos save_current = it->current;
6589 struct text_pos save_position = it->position;
6590 struct text_pos pos1;
6591 EMACS_INT next_stop;
6592
6593 /* Scan in strict logical order. */
6594 it->bidi_p = 0;
6595 do
6596 {
6597 it->prev_stop = charpos;
6598 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6599 reseat_1 (it, pos1, 0);
6600 compute_stop_pos (it);
6601 /* We must advance forward, right? */
6602 if (it->stop_charpos <= it->prev_stop)
6603 abort ();
6604 charpos = it->stop_charpos;
6605 }
6606 while (charpos <= where_we_are);
6607
6608 next_stop = it->stop_charpos;
6609 it->stop_charpos = it->prev_stop;
6610 it->bidi_p = 1;
6611 it->current = save_current;
6612 it->position = save_position;
6613 handle_stop (it);
6614 it->stop_charpos = next_stop;
6615 }
6616
6617 /* Load IT with the next display element from current_buffer. Value
6618 is zero if end of buffer reached. IT->stop_charpos is the next
6619 position at which to stop and check for text properties or buffer
6620 end. */
6621
6622 static int
6623 next_element_from_buffer (it)
6624 struct it *it;
6625 {
6626 int success_p = 1;
6627
6628 xassert (IT_CHARPOS (*it) >= BEGV);
6629
6630 /* With bidi reordering, the character to display might not be the
6631 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6632 we were reseat()ed to a new buffer position, which is potentially
6633 a different paragraph. */
6634 if (it->bidi_p && it->bidi_it.first_elt)
6635 {
6636 it->bidi_it.charpos = IT_CHARPOS (*it);
6637 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6638 /* If we are at the beginning of a line, we can produce the next
6639 element right away. */
6640 if (it->bidi_it.bytepos == BEGV_BYTE
6641 /* FIXME: Should support all Unicode line separators. */
6642 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6643 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6644 {
6645 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6646 bidi_get_next_char_visually (&it->bidi_it);
6647 }
6648 else
6649 {
6650 int orig_bytepos = IT_BYTEPOS (*it);
6651
6652 /* We need to prime the bidi iterator starting at the line's
6653 beginning, before we will be able to produce the next
6654 element. */
6655 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6656 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6657 it->bidi_it.charpos = IT_CHARPOS (*it);
6658 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6659 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6660 do
6661 {
6662 /* Now return to buffer position where we were asked to
6663 get the next display element, and produce that. */
6664 bidi_get_next_char_visually (&it->bidi_it);
6665 }
6666 while (it->bidi_it.bytepos != orig_bytepos
6667 && it->bidi_it.bytepos < ZV_BYTE);
6668 }
6669
6670 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6671 /* Adjust IT's position information to where we ended up. */
6672 IT_CHARPOS (*it) = it->bidi_it.charpos;
6673 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6674 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6675 }
6676
6677 if (IT_CHARPOS (*it) >= it->stop_charpos)
6678 {
6679 if (IT_CHARPOS (*it) >= it->end_charpos)
6680 {
6681 int overlay_strings_follow_p;
6682
6683 /* End of the game, except when overlay strings follow that
6684 haven't been returned yet. */
6685 if (it->overlay_strings_at_end_processed_p)
6686 overlay_strings_follow_p = 0;
6687 else
6688 {
6689 it->overlay_strings_at_end_processed_p = 1;
6690 overlay_strings_follow_p = get_overlay_strings (it, 0);
6691 }
6692
6693 if (overlay_strings_follow_p)
6694 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6695 else
6696 {
6697 it->what = IT_EOB;
6698 it->position = it->current.pos;
6699 success_p = 0;
6700 }
6701 }
6702 else if (!(!it->bidi_p
6703 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6704 || IT_CHARPOS (*it) == it->stop_charpos))
6705 {
6706 /* With bidi non-linear iteration, we could find ourselves
6707 far beyond the last computed stop_charpos, with several
6708 other stop positions in between that we missed. Scan
6709 them all now, in buffer's logical order, until we find
6710 and handle the last stop_charpos that precedes our
6711 current position. */
6712 handle_stop_backwards (it, it->stop_charpos);
6713 return GET_NEXT_DISPLAY_ELEMENT (it);
6714 }
6715 else
6716 {
6717 if (it->bidi_p)
6718 {
6719 /* Take note of the stop position we just moved across,
6720 for when we will move back across it. */
6721 it->prev_stop = it->stop_charpos;
6722 /* If we are at base paragraph embedding level, take
6723 note of the last stop position seen at this
6724 level. */
6725 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6726 it->base_level_stop = it->stop_charpos;
6727 }
6728 handle_stop (it);
6729 return GET_NEXT_DISPLAY_ELEMENT (it);
6730 }
6731 }
6732 else if (it->bidi_p
6733 /* We can sometimes back up for reasons that have nothing
6734 to do with bidi reordering. E.g., compositions. The
6735 code below is only needed when we are above the base
6736 embedding level, so test for that explicitly. */
6737 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6738 && IT_CHARPOS (*it) < it->prev_stop)
6739 {
6740 if (it->base_level_stop <= 0)
6741 it->base_level_stop = BEGV;
6742 if (IT_CHARPOS (*it) < it->base_level_stop)
6743 abort ();
6744 handle_stop_backwards (it, it->base_level_stop);
6745 return GET_NEXT_DISPLAY_ELEMENT (it);
6746 }
6747 else
6748 {
6749 /* No face changes, overlays etc. in sight, so just return a
6750 character from current_buffer. */
6751 unsigned char *p;
6752
6753 /* Maybe run the redisplay end trigger hook. Performance note:
6754 This doesn't seem to cost measurable time. */
6755 if (it->redisplay_end_trigger_charpos
6756 && it->glyph_row
6757 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6758 run_redisplay_end_trigger_hook (it);
6759
6760 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6761 it->end_charpos)
6762 && next_element_from_composition (it))
6763 {
6764 return 1;
6765 }
6766
6767 /* Get the next character, maybe multibyte. */
6768 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6769 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6770 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6771 else
6772 it->c = *p, it->len = 1;
6773
6774 /* Record what we have and where it came from. */
6775 it->what = IT_CHARACTER;
6776 it->object = it->w->buffer;
6777 it->position = it->current.pos;
6778
6779 /* Normally we return the character found above, except when we
6780 really want to return an ellipsis for selective display. */
6781 if (it->selective)
6782 {
6783 if (it->c == '\n')
6784 {
6785 /* A value of selective > 0 means hide lines indented more
6786 than that number of columns. */
6787 if (it->selective > 0
6788 && IT_CHARPOS (*it) + 1 < ZV
6789 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6790 IT_BYTEPOS (*it) + 1,
6791 (double) it->selective)) /* iftc */
6792 {
6793 success_p = next_element_from_ellipsis (it);
6794 it->dpvec_char_len = -1;
6795 }
6796 }
6797 else if (it->c == '\r' && it->selective == -1)
6798 {
6799 /* A value of selective == -1 means that everything from the
6800 CR to the end of the line is invisible, with maybe an
6801 ellipsis displayed for it. */
6802 success_p = next_element_from_ellipsis (it);
6803 it->dpvec_char_len = -1;
6804 }
6805 }
6806 }
6807
6808 /* Value is zero if end of buffer reached. */
6809 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6810 return success_p;
6811 }
6812
6813
6814 /* Run the redisplay end trigger hook for IT. */
6815
6816 static void
6817 run_redisplay_end_trigger_hook (it)
6818 struct it *it;
6819 {
6820 Lisp_Object args[3];
6821
6822 /* IT->glyph_row should be non-null, i.e. we should be actually
6823 displaying something, or otherwise we should not run the hook. */
6824 xassert (it->glyph_row);
6825
6826 /* Set up hook arguments. */
6827 args[0] = Qredisplay_end_trigger_functions;
6828 args[1] = it->window;
6829 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6830 it->redisplay_end_trigger_charpos = 0;
6831
6832 /* Since we are *trying* to run these functions, don't try to run
6833 them again, even if they get an error. */
6834 it->w->redisplay_end_trigger = Qnil;
6835 Frun_hook_with_args (3, args);
6836
6837 /* Notice if it changed the face of the character we are on. */
6838 handle_face_prop (it);
6839 }
6840
6841
6842 /* Deliver a composition display element. Unlike the other
6843 next_element_from_XXX, this function is not registered in the array
6844 get_next_element[]. It is called from next_element_from_buffer and
6845 next_element_from_string when necessary. */
6846
6847 static int
6848 next_element_from_composition (it)
6849 struct it *it;
6850 {
6851 it->what = IT_COMPOSITION;
6852 it->len = it->cmp_it.nbytes;
6853 if (STRINGP (it->string))
6854 {
6855 if (it->c < 0)
6856 {
6857 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6858 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6859 return 0;
6860 }
6861 it->position = it->current.string_pos;
6862 it->object = it->string;
6863 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6864 IT_STRING_BYTEPOS (*it), it->string);
6865 }
6866 else
6867 {
6868 if (it->c < 0)
6869 {
6870 IT_CHARPOS (*it) += it->cmp_it.nchars;
6871 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6872 return 0;
6873 }
6874 it->position = it->current.pos;
6875 it->object = it->w->buffer;
6876 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6877 IT_BYTEPOS (*it), Qnil);
6878 }
6879 return 1;
6880 }
6881
6882
6883 \f
6884 /***********************************************************************
6885 Moving an iterator without producing glyphs
6886 ***********************************************************************/
6887
6888 /* Check if iterator is at a position corresponding to a valid buffer
6889 position after some move_it_ call. */
6890
6891 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6892 ((it)->method == GET_FROM_STRING \
6893 ? IT_STRING_CHARPOS (*it) == 0 \
6894 : 1)
6895
6896
6897 /* Move iterator IT to a specified buffer or X position within one
6898 line on the display without producing glyphs.
6899
6900 OP should be a bit mask including some or all of these bits:
6901 MOVE_TO_X: Stop upon reaching x-position TO_X.
6902 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6903 Regardless of OP's value, stop upon reaching the end of the display line.
6904
6905 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6906 This means, in particular, that TO_X includes window's horizontal
6907 scroll amount.
6908
6909 The return value has several possible values that
6910 say what condition caused the scan to stop:
6911
6912 MOVE_POS_MATCH_OR_ZV
6913 - when TO_POS or ZV was reached.
6914
6915 MOVE_X_REACHED
6916 -when TO_X was reached before TO_POS or ZV were reached.
6917
6918 MOVE_LINE_CONTINUED
6919 - when we reached the end of the display area and the line must
6920 be continued.
6921
6922 MOVE_LINE_TRUNCATED
6923 - when we reached the end of the display area and the line is
6924 truncated.
6925
6926 MOVE_NEWLINE_OR_CR
6927 - when we stopped at a line end, i.e. a newline or a CR and selective
6928 display is on. */
6929
6930 static enum move_it_result
6931 move_it_in_display_line_to (struct it *it,
6932 EMACS_INT to_charpos, int to_x,
6933 enum move_operation_enum op)
6934 {
6935 enum move_it_result result = MOVE_UNDEFINED;
6936 struct glyph_row *saved_glyph_row;
6937 struct it wrap_it, atpos_it, atx_it;
6938 int may_wrap = 0;
6939 enum it_method prev_method = it->method;
6940 EMACS_INT prev_pos = IT_CHARPOS (*it);
6941
6942 /* Don't produce glyphs in produce_glyphs. */
6943 saved_glyph_row = it->glyph_row;
6944 it->glyph_row = NULL;
6945
6946 /* Use wrap_it to save a copy of IT wherever a word wrap could
6947 occur. Use atpos_it to save a copy of IT at the desired buffer
6948 position, if found, so that we can scan ahead and check if the
6949 word later overshoots the window edge. Use atx_it similarly, for
6950 pixel positions. */
6951 wrap_it.sp = -1;
6952 atpos_it.sp = -1;
6953 atx_it.sp = -1;
6954
6955 #define BUFFER_POS_REACHED_P() \
6956 ((op & MOVE_TO_POS) != 0 \
6957 && BUFFERP (it->object) \
6958 && (IT_CHARPOS (*it) == to_charpos \
6959 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
6960 && (it->method == GET_FROM_BUFFER \
6961 || (it->method == GET_FROM_DISPLAY_VECTOR \
6962 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6963
6964 /* If there's a line-/wrap-prefix, handle it. */
6965 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6966 && it->current_y < it->last_visible_y)
6967 handle_line_prefix (it);
6968
6969 while (1)
6970 {
6971 int x, i, ascent = 0, descent = 0;
6972
6973 /* Utility macro to reset an iterator with x, ascent, and descent. */
6974 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6975 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6976 (IT)->max_descent = descent)
6977
6978 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6979 glyph). */
6980 if ((op & MOVE_TO_POS) != 0
6981 && BUFFERP (it->object)
6982 && it->method == GET_FROM_BUFFER
6983 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
6984 || (it->bidi_p
6985 && (prev_method == GET_FROM_IMAGE
6986 || prev_method == GET_FROM_STRETCH)
6987 /* Passed TO_CHARPOS from left to right. */
6988 && ((prev_pos < to_charpos
6989 && IT_CHARPOS (*it) > to_charpos)
6990 /* Passed TO_CHARPOS from right to left. */
6991 || (prev_pos > to_charpos
6992 && IT_CHARPOS (*it) < to_charpos)))))
6993 {
6994 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6995 {
6996 result = MOVE_POS_MATCH_OR_ZV;
6997 break;
6998 }
6999 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7000 /* If wrap_it is valid, the current position might be in a
7001 word that is wrapped. So, save the iterator in
7002 atpos_it and continue to see if wrapping happens. */
7003 atpos_it = *it;
7004 }
7005
7006 prev_method = it->method;
7007 if (it->method == GET_FROM_BUFFER)
7008 prev_pos = IT_CHARPOS (*it);
7009 /* Stop when ZV reached.
7010 We used to stop here when TO_CHARPOS reached as well, but that is
7011 too soon if this glyph does not fit on this line. So we handle it
7012 explicitly below. */
7013 if (!get_next_display_element (it))
7014 {
7015 result = MOVE_POS_MATCH_OR_ZV;
7016 break;
7017 }
7018
7019 if (it->line_wrap == TRUNCATE)
7020 {
7021 if (BUFFER_POS_REACHED_P ())
7022 {
7023 result = MOVE_POS_MATCH_OR_ZV;
7024 break;
7025 }
7026 }
7027 else
7028 {
7029 if (it->line_wrap == WORD_WRAP)
7030 {
7031 if (IT_DISPLAYING_WHITESPACE (it))
7032 may_wrap = 1;
7033 else if (may_wrap)
7034 {
7035 /* We have reached a glyph that follows one or more
7036 whitespace characters. If the position is
7037 already found, we are done. */
7038 if (atpos_it.sp >= 0)
7039 {
7040 *it = atpos_it;
7041 result = MOVE_POS_MATCH_OR_ZV;
7042 goto done;
7043 }
7044 if (atx_it.sp >= 0)
7045 {
7046 *it = atx_it;
7047 result = MOVE_X_REACHED;
7048 goto done;
7049 }
7050 /* Otherwise, we can wrap here. */
7051 wrap_it = *it;
7052 may_wrap = 0;
7053 }
7054 }
7055 }
7056
7057 /* Remember the line height for the current line, in case
7058 the next element doesn't fit on the line. */
7059 ascent = it->max_ascent;
7060 descent = it->max_descent;
7061
7062 /* The call to produce_glyphs will get the metrics of the
7063 display element IT is loaded with. Record the x-position
7064 before this display element, in case it doesn't fit on the
7065 line. */
7066 x = it->current_x;
7067
7068 PRODUCE_GLYPHS (it);
7069
7070 if (it->area != TEXT_AREA)
7071 {
7072 set_iterator_to_next (it, 1);
7073 continue;
7074 }
7075
7076 /* The number of glyphs we get back in IT->nglyphs will normally
7077 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7078 character on a terminal frame, or (iii) a line end. For the
7079 second case, IT->nglyphs - 1 padding glyphs will be present.
7080 (On X frames, there is only one glyph produced for a
7081 composite character.)
7082
7083 The behavior implemented below means, for continuation lines,
7084 that as many spaces of a TAB as fit on the current line are
7085 displayed there. For terminal frames, as many glyphs of a
7086 multi-glyph character are displayed in the current line, too.
7087 This is what the old redisplay code did, and we keep it that
7088 way. Under X, the whole shape of a complex character must
7089 fit on the line or it will be completely displayed in the
7090 next line.
7091
7092 Note that both for tabs and padding glyphs, all glyphs have
7093 the same width. */
7094 if (it->nglyphs)
7095 {
7096 /* More than one glyph or glyph doesn't fit on line. All
7097 glyphs have the same width. */
7098 int single_glyph_width = it->pixel_width / it->nglyphs;
7099 int new_x;
7100 int x_before_this_char = x;
7101 int hpos_before_this_char = it->hpos;
7102
7103 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7104 {
7105 new_x = x + single_glyph_width;
7106
7107 /* We want to leave anything reaching TO_X to the caller. */
7108 if ((op & MOVE_TO_X) && new_x > to_x)
7109 {
7110 if (BUFFER_POS_REACHED_P ())
7111 {
7112 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7113 goto buffer_pos_reached;
7114 if (atpos_it.sp < 0)
7115 {
7116 atpos_it = *it;
7117 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7118 }
7119 }
7120 else
7121 {
7122 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7123 {
7124 it->current_x = x;
7125 result = MOVE_X_REACHED;
7126 break;
7127 }
7128 if (atx_it.sp < 0)
7129 {
7130 atx_it = *it;
7131 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7132 }
7133 }
7134 }
7135
7136 if (/* Lines are continued. */
7137 it->line_wrap != TRUNCATE
7138 && (/* And glyph doesn't fit on the line. */
7139 new_x > it->last_visible_x
7140 /* Or it fits exactly and we're on a window
7141 system frame. */
7142 || (new_x == it->last_visible_x
7143 && FRAME_WINDOW_P (it->f))))
7144 {
7145 if (/* IT->hpos == 0 means the very first glyph
7146 doesn't fit on the line, e.g. a wide image. */
7147 it->hpos == 0
7148 || (new_x == it->last_visible_x
7149 && FRAME_WINDOW_P (it->f)))
7150 {
7151 ++it->hpos;
7152 it->current_x = new_x;
7153
7154 /* The character's last glyph just barely fits
7155 in this row. */
7156 if (i == it->nglyphs - 1)
7157 {
7158 /* If this is the destination position,
7159 return a position *before* it in this row,
7160 now that we know it fits in this row. */
7161 if (BUFFER_POS_REACHED_P ())
7162 {
7163 if (it->line_wrap != WORD_WRAP
7164 || wrap_it.sp < 0)
7165 {
7166 it->hpos = hpos_before_this_char;
7167 it->current_x = x_before_this_char;
7168 result = MOVE_POS_MATCH_OR_ZV;
7169 break;
7170 }
7171 if (it->line_wrap == WORD_WRAP
7172 && atpos_it.sp < 0)
7173 {
7174 atpos_it = *it;
7175 atpos_it.current_x = x_before_this_char;
7176 atpos_it.hpos = hpos_before_this_char;
7177 }
7178 }
7179
7180 set_iterator_to_next (it, 1);
7181 /* On graphical terminals, newlines may
7182 "overflow" into the fringe if
7183 overflow-newline-into-fringe is non-nil.
7184 On text-only terminals, newlines may
7185 overflow into the last glyph on the
7186 display line.*/
7187 if (!FRAME_WINDOW_P (it->f)
7188 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7189 {
7190 if (!get_next_display_element (it))
7191 {
7192 result = MOVE_POS_MATCH_OR_ZV;
7193 break;
7194 }
7195 if (BUFFER_POS_REACHED_P ())
7196 {
7197 if (ITERATOR_AT_END_OF_LINE_P (it))
7198 result = MOVE_POS_MATCH_OR_ZV;
7199 else
7200 result = MOVE_LINE_CONTINUED;
7201 break;
7202 }
7203 if (ITERATOR_AT_END_OF_LINE_P (it))
7204 {
7205 result = MOVE_NEWLINE_OR_CR;
7206 break;
7207 }
7208 }
7209 }
7210 }
7211 else
7212 IT_RESET_X_ASCENT_DESCENT (it);
7213
7214 if (wrap_it.sp >= 0)
7215 {
7216 *it = wrap_it;
7217 atpos_it.sp = -1;
7218 atx_it.sp = -1;
7219 }
7220
7221 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7222 IT_CHARPOS (*it)));
7223 result = MOVE_LINE_CONTINUED;
7224 break;
7225 }
7226
7227 if (BUFFER_POS_REACHED_P ())
7228 {
7229 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7230 goto buffer_pos_reached;
7231 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7232 {
7233 atpos_it = *it;
7234 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7235 }
7236 }
7237
7238 if (new_x > it->first_visible_x)
7239 {
7240 /* Glyph is visible. Increment number of glyphs that
7241 would be displayed. */
7242 ++it->hpos;
7243 }
7244 }
7245
7246 if (result != MOVE_UNDEFINED)
7247 break;
7248 }
7249 else if (BUFFER_POS_REACHED_P ())
7250 {
7251 buffer_pos_reached:
7252 IT_RESET_X_ASCENT_DESCENT (it);
7253 result = MOVE_POS_MATCH_OR_ZV;
7254 break;
7255 }
7256 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7257 {
7258 /* Stop when TO_X specified and reached. This check is
7259 necessary here because of lines consisting of a line end,
7260 only. The line end will not produce any glyphs and we
7261 would never get MOVE_X_REACHED. */
7262 xassert (it->nglyphs == 0);
7263 result = MOVE_X_REACHED;
7264 break;
7265 }
7266
7267 /* Is this a line end? If yes, we're done. */
7268 if (ITERATOR_AT_END_OF_LINE_P (it))
7269 {
7270 result = MOVE_NEWLINE_OR_CR;
7271 break;
7272 }
7273
7274 if (it->method == GET_FROM_BUFFER)
7275 prev_pos = IT_CHARPOS (*it);
7276 /* The current display element has been consumed. Advance
7277 to the next. */
7278 set_iterator_to_next (it, 1);
7279
7280 /* Stop if lines are truncated and IT's current x-position is
7281 past the right edge of the window now. */
7282 if (it->line_wrap == TRUNCATE
7283 && it->current_x >= it->last_visible_x)
7284 {
7285 if (!FRAME_WINDOW_P (it->f)
7286 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7287 {
7288 if (!get_next_display_element (it)
7289 || BUFFER_POS_REACHED_P ())
7290 {
7291 result = MOVE_POS_MATCH_OR_ZV;
7292 break;
7293 }
7294 if (ITERATOR_AT_END_OF_LINE_P (it))
7295 {
7296 result = MOVE_NEWLINE_OR_CR;
7297 break;
7298 }
7299 }
7300 result = MOVE_LINE_TRUNCATED;
7301 break;
7302 }
7303 #undef IT_RESET_X_ASCENT_DESCENT
7304 }
7305
7306 #undef BUFFER_POS_REACHED_P
7307
7308 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7309 restore the saved iterator. */
7310 if (atpos_it.sp >= 0)
7311 *it = atpos_it;
7312 else if (atx_it.sp >= 0)
7313 *it = atx_it;
7314
7315 done:
7316
7317 /* Restore the iterator settings altered at the beginning of this
7318 function. */
7319 it->glyph_row = saved_glyph_row;
7320 return result;
7321 }
7322
7323 /* For external use. */
7324 void
7325 move_it_in_display_line (struct it *it,
7326 EMACS_INT to_charpos, int to_x,
7327 enum move_operation_enum op)
7328 {
7329 if (it->line_wrap == WORD_WRAP
7330 && (op & MOVE_TO_X))
7331 {
7332 struct it save_it = *it;
7333 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7334 /* When word-wrap is on, TO_X may lie past the end
7335 of a wrapped line. Then it->current is the
7336 character on the next line, so backtrack to the
7337 space before the wrap point. */
7338 if (skip == MOVE_LINE_CONTINUED)
7339 {
7340 int prev_x = max (it->current_x - 1, 0);
7341 *it = save_it;
7342 move_it_in_display_line_to
7343 (it, -1, prev_x, MOVE_TO_X);
7344 }
7345 }
7346 else
7347 move_it_in_display_line_to (it, to_charpos, to_x, op);
7348 }
7349
7350
7351 /* Move IT forward until it satisfies one or more of the criteria in
7352 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7353
7354 OP is a bit-mask that specifies where to stop, and in particular,
7355 which of those four position arguments makes a difference. See the
7356 description of enum move_operation_enum.
7357
7358 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7359 screen line, this function will set IT to the next position >
7360 TO_CHARPOS. */
7361
7362 void
7363 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7364 struct it *it;
7365 int to_charpos, to_x, to_y, to_vpos;
7366 int op;
7367 {
7368 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7369 int line_height, line_start_x = 0, reached = 0;
7370
7371 for (;;)
7372 {
7373 if (op & MOVE_TO_VPOS)
7374 {
7375 /* If no TO_CHARPOS and no TO_X specified, stop at the
7376 start of the line TO_VPOS. */
7377 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7378 {
7379 if (it->vpos == to_vpos)
7380 {
7381 reached = 1;
7382 break;
7383 }
7384 else
7385 skip = move_it_in_display_line_to (it, -1, -1, 0);
7386 }
7387 else
7388 {
7389 /* TO_VPOS >= 0 means stop at TO_X in the line at
7390 TO_VPOS, or at TO_POS, whichever comes first. */
7391 if (it->vpos == to_vpos)
7392 {
7393 reached = 2;
7394 break;
7395 }
7396
7397 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7398
7399 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7400 {
7401 reached = 3;
7402 break;
7403 }
7404 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7405 {
7406 /* We have reached TO_X but not in the line we want. */
7407 skip = move_it_in_display_line_to (it, to_charpos,
7408 -1, MOVE_TO_POS);
7409 if (skip == MOVE_POS_MATCH_OR_ZV)
7410 {
7411 reached = 4;
7412 break;
7413 }
7414 }
7415 }
7416 }
7417 else if (op & MOVE_TO_Y)
7418 {
7419 struct it it_backup;
7420
7421 if (it->line_wrap == WORD_WRAP)
7422 it_backup = *it;
7423
7424 /* TO_Y specified means stop at TO_X in the line containing
7425 TO_Y---or at TO_CHARPOS if this is reached first. The
7426 problem is that we can't really tell whether the line
7427 contains TO_Y before we have completely scanned it, and
7428 this may skip past TO_X. What we do is to first scan to
7429 TO_X.
7430
7431 If TO_X is not specified, use a TO_X of zero. The reason
7432 is to make the outcome of this function more predictable.
7433 If we didn't use TO_X == 0, we would stop at the end of
7434 the line which is probably not what a caller would expect
7435 to happen. */
7436 skip = move_it_in_display_line_to
7437 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7438 (MOVE_TO_X | (op & MOVE_TO_POS)));
7439
7440 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7441 if (skip == MOVE_POS_MATCH_OR_ZV)
7442 reached = 5;
7443 else if (skip == MOVE_X_REACHED)
7444 {
7445 /* If TO_X was reached, we want to know whether TO_Y is
7446 in the line. We know this is the case if the already
7447 scanned glyphs make the line tall enough. Otherwise,
7448 we must check by scanning the rest of the line. */
7449 line_height = it->max_ascent + it->max_descent;
7450 if (to_y >= it->current_y
7451 && to_y < it->current_y + line_height)
7452 {
7453 reached = 6;
7454 break;
7455 }
7456 it_backup = *it;
7457 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7458 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7459 op & MOVE_TO_POS);
7460 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7461 line_height = it->max_ascent + it->max_descent;
7462 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7463
7464 if (to_y >= it->current_y
7465 && to_y < it->current_y + line_height)
7466 {
7467 /* If TO_Y is in this line and TO_X was reached
7468 above, we scanned too far. We have to restore
7469 IT's settings to the ones before skipping. */
7470 *it = it_backup;
7471 reached = 6;
7472 }
7473 else
7474 {
7475 skip = skip2;
7476 if (skip == MOVE_POS_MATCH_OR_ZV)
7477 reached = 7;
7478 }
7479 }
7480 else
7481 {
7482 /* Check whether TO_Y is in this line. */
7483 line_height = it->max_ascent + it->max_descent;
7484 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7485
7486 if (to_y >= it->current_y
7487 && to_y < it->current_y + line_height)
7488 {
7489 /* When word-wrap is on, TO_X may lie past the end
7490 of a wrapped line. Then it->current is the
7491 character on the next line, so backtrack to the
7492 space before the wrap point. */
7493 if (skip == MOVE_LINE_CONTINUED
7494 && it->line_wrap == WORD_WRAP)
7495 {
7496 int prev_x = max (it->current_x - 1, 0);
7497 *it = it_backup;
7498 skip = move_it_in_display_line_to
7499 (it, -1, prev_x, MOVE_TO_X);
7500 }
7501 reached = 6;
7502 }
7503 }
7504
7505 if (reached)
7506 break;
7507 }
7508 else if (BUFFERP (it->object)
7509 && (it->method == GET_FROM_BUFFER
7510 || it->method == GET_FROM_STRETCH)
7511 && IT_CHARPOS (*it) >= to_charpos)
7512 skip = MOVE_POS_MATCH_OR_ZV;
7513 else
7514 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7515
7516 switch (skip)
7517 {
7518 case MOVE_POS_MATCH_OR_ZV:
7519 reached = 8;
7520 goto out;
7521
7522 case MOVE_NEWLINE_OR_CR:
7523 set_iterator_to_next (it, 1);
7524 it->continuation_lines_width = 0;
7525 break;
7526
7527 case MOVE_LINE_TRUNCATED:
7528 it->continuation_lines_width = 0;
7529 reseat_at_next_visible_line_start (it, 0);
7530 if ((op & MOVE_TO_POS) != 0
7531 && IT_CHARPOS (*it) > to_charpos)
7532 {
7533 reached = 9;
7534 goto out;
7535 }
7536 break;
7537
7538 case MOVE_LINE_CONTINUED:
7539 /* For continued lines ending in a tab, some of the glyphs
7540 associated with the tab are displayed on the current
7541 line. Since it->current_x does not include these glyphs,
7542 we use it->last_visible_x instead. */
7543 if (it->c == '\t')
7544 {
7545 it->continuation_lines_width += it->last_visible_x;
7546 /* When moving by vpos, ensure that the iterator really
7547 advances to the next line (bug#847, bug#969). Fixme:
7548 do we need to do this in other circumstances? */
7549 if (it->current_x != it->last_visible_x
7550 && (op & MOVE_TO_VPOS)
7551 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7552 {
7553 line_start_x = it->current_x + it->pixel_width
7554 - it->last_visible_x;
7555 set_iterator_to_next (it, 0);
7556 }
7557 }
7558 else
7559 it->continuation_lines_width += it->current_x;
7560 break;
7561
7562 default:
7563 abort ();
7564 }
7565
7566 /* Reset/increment for the next run. */
7567 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7568 it->current_x = line_start_x;
7569 line_start_x = 0;
7570 it->hpos = 0;
7571 it->current_y += it->max_ascent + it->max_descent;
7572 ++it->vpos;
7573 last_height = it->max_ascent + it->max_descent;
7574 last_max_ascent = it->max_ascent;
7575 it->max_ascent = it->max_descent = 0;
7576 }
7577
7578 out:
7579
7580 /* On text terminals, we may stop at the end of a line in the middle
7581 of a multi-character glyph. If the glyph itself is continued,
7582 i.e. it is actually displayed on the next line, don't treat this
7583 stopping point as valid; move to the next line instead (unless
7584 that brings us offscreen). */
7585 if (!FRAME_WINDOW_P (it->f)
7586 && op & MOVE_TO_POS
7587 && IT_CHARPOS (*it) == to_charpos
7588 && it->what == IT_CHARACTER
7589 && it->nglyphs > 1
7590 && it->line_wrap == WINDOW_WRAP
7591 && it->current_x == it->last_visible_x - 1
7592 && it->c != '\n'
7593 && it->c != '\t'
7594 && it->vpos < XFASTINT (it->w->window_end_vpos))
7595 {
7596 it->continuation_lines_width += it->current_x;
7597 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7598 it->current_y += it->max_ascent + it->max_descent;
7599 ++it->vpos;
7600 last_height = it->max_ascent + it->max_descent;
7601 last_max_ascent = it->max_ascent;
7602 }
7603
7604 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7605 }
7606
7607
7608 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7609
7610 If DY > 0, move IT backward at least that many pixels. DY = 0
7611 means move IT backward to the preceding line start or BEGV. This
7612 function may move over more than DY pixels if IT->current_y - DY
7613 ends up in the middle of a line; in this case IT->current_y will be
7614 set to the top of the line moved to. */
7615
7616 void
7617 move_it_vertically_backward (it, dy)
7618 struct it *it;
7619 int dy;
7620 {
7621 int nlines, h;
7622 struct it it2, it3;
7623 int start_pos;
7624
7625 move_further_back:
7626 xassert (dy >= 0);
7627
7628 start_pos = IT_CHARPOS (*it);
7629
7630 /* Estimate how many newlines we must move back. */
7631 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7632
7633 /* Set the iterator's position that many lines back. */
7634 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7635 back_to_previous_visible_line_start (it);
7636
7637 /* Reseat the iterator here. When moving backward, we don't want
7638 reseat to skip forward over invisible text, set up the iterator
7639 to deliver from overlay strings at the new position etc. So,
7640 use reseat_1 here. */
7641 reseat_1 (it, it->current.pos, 1);
7642
7643 /* We are now surely at a line start. */
7644 it->current_x = it->hpos = 0;
7645 it->continuation_lines_width = 0;
7646
7647 /* Move forward and see what y-distance we moved. First move to the
7648 start of the next line so that we get its height. We need this
7649 height to be able to tell whether we reached the specified
7650 y-distance. */
7651 it2 = *it;
7652 it2.max_ascent = it2.max_descent = 0;
7653 do
7654 {
7655 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7656 MOVE_TO_POS | MOVE_TO_VPOS);
7657 }
7658 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7659 xassert (IT_CHARPOS (*it) >= BEGV);
7660 it3 = it2;
7661
7662 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7663 xassert (IT_CHARPOS (*it) >= BEGV);
7664 /* H is the actual vertical distance from the position in *IT
7665 and the starting position. */
7666 h = it2.current_y - it->current_y;
7667 /* NLINES is the distance in number of lines. */
7668 nlines = it2.vpos - it->vpos;
7669
7670 /* Correct IT's y and vpos position
7671 so that they are relative to the starting point. */
7672 it->vpos -= nlines;
7673 it->current_y -= h;
7674
7675 if (dy == 0)
7676 {
7677 /* DY == 0 means move to the start of the screen line. The
7678 value of nlines is > 0 if continuation lines were involved. */
7679 if (nlines > 0)
7680 move_it_by_lines (it, nlines, 1);
7681 }
7682 else
7683 {
7684 /* The y-position we try to reach, relative to *IT.
7685 Note that H has been subtracted in front of the if-statement. */
7686 int target_y = it->current_y + h - dy;
7687 int y0 = it3.current_y;
7688 int y1 = line_bottom_y (&it3);
7689 int line_height = y1 - y0;
7690
7691 /* If we did not reach target_y, try to move further backward if
7692 we can. If we moved too far backward, try to move forward. */
7693 if (target_y < it->current_y
7694 /* This is heuristic. In a window that's 3 lines high, with
7695 a line height of 13 pixels each, recentering with point
7696 on the bottom line will try to move -39/2 = 19 pixels
7697 backward. Try to avoid moving into the first line. */
7698 && (it->current_y - target_y
7699 > min (window_box_height (it->w), line_height * 2 / 3))
7700 && IT_CHARPOS (*it) > BEGV)
7701 {
7702 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7703 target_y - it->current_y));
7704 dy = it->current_y - target_y;
7705 goto move_further_back;
7706 }
7707 else if (target_y >= it->current_y + line_height
7708 && IT_CHARPOS (*it) < ZV)
7709 {
7710 /* Should move forward by at least one line, maybe more.
7711
7712 Note: Calling move_it_by_lines can be expensive on
7713 terminal frames, where compute_motion is used (via
7714 vmotion) to do the job, when there are very long lines
7715 and truncate-lines is nil. That's the reason for
7716 treating terminal frames specially here. */
7717
7718 if (!FRAME_WINDOW_P (it->f))
7719 move_it_vertically (it, target_y - (it->current_y + line_height));
7720 else
7721 {
7722 do
7723 {
7724 move_it_by_lines (it, 1, 1);
7725 }
7726 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7727 }
7728 }
7729 }
7730 }
7731
7732
7733 /* Move IT by a specified amount of pixel lines DY. DY negative means
7734 move backwards. DY = 0 means move to start of screen line. At the
7735 end, IT will be on the start of a screen line. */
7736
7737 void
7738 move_it_vertically (it, dy)
7739 struct it *it;
7740 int dy;
7741 {
7742 if (dy <= 0)
7743 move_it_vertically_backward (it, -dy);
7744 else
7745 {
7746 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7747 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7748 MOVE_TO_POS | MOVE_TO_Y);
7749 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7750
7751 /* If buffer ends in ZV without a newline, move to the start of
7752 the line to satisfy the post-condition. */
7753 if (IT_CHARPOS (*it) == ZV
7754 && ZV > BEGV
7755 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7756 move_it_by_lines (it, 0, 0);
7757 }
7758 }
7759
7760
7761 /* Move iterator IT past the end of the text line it is in. */
7762
7763 void
7764 move_it_past_eol (it)
7765 struct it *it;
7766 {
7767 enum move_it_result rc;
7768
7769 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7770 if (rc == MOVE_NEWLINE_OR_CR)
7771 set_iterator_to_next (it, 0);
7772 }
7773
7774
7775 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7776 negative means move up. DVPOS == 0 means move to the start of the
7777 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7778 NEED_Y_P is zero, IT->current_y will be left unchanged.
7779
7780 Further optimization ideas: If we would know that IT->f doesn't use
7781 a face with proportional font, we could be faster for
7782 truncate-lines nil. */
7783
7784 void
7785 move_it_by_lines (it, dvpos, need_y_p)
7786 struct it *it;
7787 int dvpos, need_y_p;
7788 {
7789 struct position pos;
7790
7791 /* The commented-out optimization uses vmotion on terminals. This
7792 gives bad results, because elements like it->what, on which
7793 callers such as pos_visible_p rely, aren't updated. */
7794 /* if (!FRAME_WINDOW_P (it->f))
7795 {
7796 struct text_pos textpos;
7797
7798 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7799 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7800 reseat (it, textpos, 1);
7801 it->vpos += pos.vpos;
7802 it->current_y += pos.vpos;
7803 }
7804 else */
7805
7806 if (dvpos == 0)
7807 {
7808 /* DVPOS == 0 means move to the start of the screen line. */
7809 move_it_vertically_backward (it, 0);
7810 xassert (it->current_x == 0 && it->hpos == 0);
7811 /* Let next call to line_bottom_y calculate real line height */
7812 last_height = 0;
7813 }
7814 else if (dvpos > 0)
7815 {
7816 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7817 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7818 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7819 }
7820 else
7821 {
7822 struct it it2;
7823 int start_charpos, i;
7824
7825 /* Start at the beginning of the screen line containing IT's
7826 position. This may actually move vertically backwards,
7827 in case of overlays, so adjust dvpos accordingly. */
7828 dvpos += it->vpos;
7829 move_it_vertically_backward (it, 0);
7830 dvpos -= it->vpos;
7831
7832 /* Go back -DVPOS visible lines and reseat the iterator there. */
7833 start_charpos = IT_CHARPOS (*it);
7834 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7835 back_to_previous_visible_line_start (it);
7836 reseat (it, it->current.pos, 1);
7837
7838 /* Move further back if we end up in a string or an image. */
7839 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7840 {
7841 /* First try to move to start of display line. */
7842 dvpos += it->vpos;
7843 move_it_vertically_backward (it, 0);
7844 dvpos -= it->vpos;
7845 if (IT_POS_VALID_AFTER_MOVE_P (it))
7846 break;
7847 /* If start of line is still in string or image,
7848 move further back. */
7849 back_to_previous_visible_line_start (it);
7850 reseat (it, it->current.pos, 1);
7851 dvpos--;
7852 }
7853
7854 it->current_x = it->hpos = 0;
7855
7856 /* Above call may have moved too far if continuation lines
7857 are involved. Scan forward and see if it did. */
7858 it2 = *it;
7859 it2.vpos = it2.current_y = 0;
7860 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7861 it->vpos -= it2.vpos;
7862 it->current_y -= it2.current_y;
7863 it->current_x = it->hpos = 0;
7864
7865 /* If we moved too far back, move IT some lines forward. */
7866 if (it2.vpos > -dvpos)
7867 {
7868 int delta = it2.vpos + dvpos;
7869 it2 = *it;
7870 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7871 /* Move back again if we got too far ahead. */
7872 if (IT_CHARPOS (*it) >= start_charpos)
7873 *it = it2;
7874 }
7875 }
7876 }
7877
7878 /* Return 1 if IT points into the middle of a display vector. */
7879
7880 int
7881 in_display_vector_p (it)
7882 struct it *it;
7883 {
7884 return (it->method == GET_FROM_DISPLAY_VECTOR
7885 && it->current.dpvec_index > 0
7886 && it->dpvec + it->current.dpvec_index != it->dpend);
7887 }
7888
7889 \f
7890 /***********************************************************************
7891 Messages
7892 ***********************************************************************/
7893
7894
7895 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7896 to *Messages*. */
7897
7898 void
7899 add_to_log (format, arg1, arg2)
7900 char *format;
7901 Lisp_Object arg1, arg2;
7902 {
7903 Lisp_Object args[3];
7904 Lisp_Object msg, fmt;
7905 char *buffer;
7906 int len;
7907 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7908 USE_SAFE_ALLOCA;
7909
7910 /* Do nothing if called asynchronously. Inserting text into
7911 a buffer may call after-change-functions and alike and
7912 that would means running Lisp asynchronously. */
7913 if (handling_signal)
7914 return;
7915
7916 fmt = msg = Qnil;
7917 GCPRO4 (fmt, msg, arg1, arg2);
7918
7919 args[0] = fmt = build_string (format);
7920 args[1] = arg1;
7921 args[2] = arg2;
7922 msg = Fformat (3, args);
7923
7924 len = SBYTES (msg) + 1;
7925 SAFE_ALLOCA (buffer, char *, len);
7926 bcopy (SDATA (msg), buffer, len);
7927
7928 message_dolog (buffer, len - 1, 1, 0);
7929 SAFE_FREE ();
7930
7931 UNGCPRO;
7932 }
7933
7934
7935 /* Output a newline in the *Messages* buffer if "needs" one. */
7936
7937 void
7938 message_log_maybe_newline ()
7939 {
7940 if (message_log_need_newline)
7941 message_dolog ("", 0, 1, 0);
7942 }
7943
7944
7945 /* Add a string M of length NBYTES to the message log, optionally
7946 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7947 nonzero, means interpret the contents of M as multibyte. This
7948 function calls low-level routines in order to bypass text property
7949 hooks, etc. which might not be safe to run.
7950
7951 This may GC (insert may run before/after change hooks),
7952 so the buffer M must NOT point to a Lisp string. */
7953
7954 void
7955 message_dolog (m, nbytes, nlflag, multibyte)
7956 const char *m;
7957 int nbytes, nlflag, multibyte;
7958 {
7959 if (!NILP (Vmemory_full))
7960 return;
7961
7962 if (!NILP (Vmessage_log_max))
7963 {
7964 struct buffer *oldbuf;
7965 Lisp_Object oldpoint, oldbegv, oldzv;
7966 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7967 int point_at_end = 0;
7968 int zv_at_end = 0;
7969 Lisp_Object old_deactivate_mark, tem;
7970 struct gcpro gcpro1;
7971
7972 old_deactivate_mark = Vdeactivate_mark;
7973 oldbuf = current_buffer;
7974 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7975 current_buffer->undo_list = Qt;
7976
7977 oldpoint = message_dolog_marker1;
7978 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7979 oldbegv = message_dolog_marker2;
7980 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7981 oldzv = message_dolog_marker3;
7982 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7983 GCPRO1 (old_deactivate_mark);
7984
7985 if (PT == Z)
7986 point_at_end = 1;
7987 if (ZV == Z)
7988 zv_at_end = 1;
7989
7990 BEGV = BEG;
7991 BEGV_BYTE = BEG_BYTE;
7992 ZV = Z;
7993 ZV_BYTE = Z_BYTE;
7994 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7995
7996 /* Insert the string--maybe converting multibyte to single byte
7997 or vice versa, so that all the text fits the buffer. */
7998 if (multibyte
7999 && NILP (current_buffer->enable_multibyte_characters))
8000 {
8001 int i, c, char_bytes;
8002 unsigned char work[1];
8003
8004 /* Convert a multibyte string to single-byte
8005 for the *Message* buffer. */
8006 for (i = 0; i < nbytes; i += char_bytes)
8007 {
8008 c = string_char_and_length (m + i, &char_bytes);
8009 work[0] = (ASCII_CHAR_P (c)
8010 ? c
8011 : multibyte_char_to_unibyte (c, Qnil));
8012 insert_1_both (work, 1, 1, 1, 0, 0);
8013 }
8014 }
8015 else if (! multibyte
8016 && ! NILP (current_buffer->enable_multibyte_characters))
8017 {
8018 int i, c, char_bytes;
8019 unsigned char *msg = (unsigned char *) m;
8020 unsigned char str[MAX_MULTIBYTE_LENGTH];
8021 /* Convert a single-byte string to multibyte
8022 for the *Message* buffer. */
8023 for (i = 0; i < nbytes; i++)
8024 {
8025 c = msg[i];
8026 MAKE_CHAR_MULTIBYTE (c);
8027 char_bytes = CHAR_STRING (c, str);
8028 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8029 }
8030 }
8031 else if (nbytes)
8032 insert_1 (m, nbytes, 1, 0, 0);
8033
8034 if (nlflag)
8035 {
8036 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
8037 insert_1 ("\n", 1, 1, 0, 0);
8038
8039 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8040 this_bol = PT;
8041 this_bol_byte = PT_BYTE;
8042
8043 /* See if this line duplicates the previous one.
8044 If so, combine duplicates. */
8045 if (this_bol > BEG)
8046 {
8047 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8048 prev_bol = PT;
8049 prev_bol_byte = PT_BYTE;
8050
8051 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8052 this_bol, this_bol_byte);
8053 if (dup)
8054 {
8055 del_range_both (prev_bol, prev_bol_byte,
8056 this_bol, this_bol_byte, 0);
8057 if (dup > 1)
8058 {
8059 char dupstr[40];
8060 int duplen;
8061
8062 /* If you change this format, don't forget to also
8063 change message_log_check_duplicate. */
8064 sprintf (dupstr, " [%d times]", dup);
8065 duplen = strlen (dupstr);
8066 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8067 insert_1 (dupstr, duplen, 1, 0, 1);
8068 }
8069 }
8070 }
8071
8072 /* If we have more than the desired maximum number of lines
8073 in the *Messages* buffer now, delete the oldest ones.
8074 This is safe because we don't have undo in this buffer. */
8075
8076 if (NATNUMP (Vmessage_log_max))
8077 {
8078 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8079 -XFASTINT (Vmessage_log_max) - 1, 0);
8080 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8081 }
8082 }
8083 BEGV = XMARKER (oldbegv)->charpos;
8084 BEGV_BYTE = marker_byte_position (oldbegv);
8085
8086 if (zv_at_end)
8087 {
8088 ZV = Z;
8089 ZV_BYTE = Z_BYTE;
8090 }
8091 else
8092 {
8093 ZV = XMARKER (oldzv)->charpos;
8094 ZV_BYTE = marker_byte_position (oldzv);
8095 }
8096
8097 if (point_at_end)
8098 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8099 else
8100 /* We can't do Fgoto_char (oldpoint) because it will run some
8101 Lisp code. */
8102 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8103 XMARKER (oldpoint)->bytepos);
8104
8105 UNGCPRO;
8106 unchain_marker (XMARKER (oldpoint));
8107 unchain_marker (XMARKER (oldbegv));
8108 unchain_marker (XMARKER (oldzv));
8109
8110 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8111 set_buffer_internal (oldbuf);
8112 if (NILP (tem))
8113 windows_or_buffers_changed = old_windows_or_buffers_changed;
8114 message_log_need_newline = !nlflag;
8115 Vdeactivate_mark = old_deactivate_mark;
8116 }
8117 }
8118
8119
8120 /* We are at the end of the buffer after just having inserted a newline.
8121 (Note: We depend on the fact we won't be crossing the gap.)
8122 Check to see if the most recent message looks a lot like the previous one.
8123 Return 0 if different, 1 if the new one should just replace it, or a
8124 value N > 1 if we should also append " [N times]". */
8125
8126 static int
8127 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
8128 int prev_bol, this_bol;
8129 int prev_bol_byte, this_bol_byte;
8130 {
8131 int i;
8132 int len = Z_BYTE - 1 - this_bol_byte;
8133 int seen_dots = 0;
8134 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8135 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8136
8137 for (i = 0; i < len; i++)
8138 {
8139 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8140 seen_dots = 1;
8141 if (p1[i] != p2[i])
8142 return seen_dots;
8143 }
8144 p1 += len;
8145 if (*p1 == '\n')
8146 return 2;
8147 if (*p1++ == ' ' && *p1++ == '[')
8148 {
8149 int n = 0;
8150 while (*p1 >= '0' && *p1 <= '9')
8151 n = n * 10 + *p1++ - '0';
8152 if (strncmp (p1, " times]\n", 8) == 0)
8153 return n+1;
8154 }
8155 return 0;
8156 }
8157 \f
8158
8159 /* Display an echo area message M with a specified length of NBYTES
8160 bytes. The string may include null characters. If M is 0, clear
8161 out any existing message, and let the mini-buffer text show
8162 through.
8163
8164 This may GC, so the buffer M must NOT point to a Lisp string. */
8165
8166 void
8167 message2 (m, nbytes, multibyte)
8168 const char *m;
8169 int nbytes;
8170 int multibyte;
8171 {
8172 /* First flush out any partial line written with print. */
8173 message_log_maybe_newline ();
8174 if (m)
8175 message_dolog (m, nbytes, 1, multibyte);
8176 message2_nolog (m, nbytes, multibyte);
8177 }
8178
8179
8180 /* The non-logging counterpart of message2. */
8181
8182 void
8183 message2_nolog (m, nbytes, multibyte)
8184 const char *m;
8185 int nbytes, multibyte;
8186 {
8187 struct frame *sf = SELECTED_FRAME ();
8188 message_enable_multibyte = multibyte;
8189
8190 if (FRAME_INITIAL_P (sf))
8191 {
8192 if (noninteractive_need_newline)
8193 putc ('\n', stderr);
8194 noninteractive_need_newline = 0;
8195 if (m)
8196 fwrite (m, nbytes, 1, stderr);
8197 if (cursor_in_echo_area == 0)
8198 fprintf (stderr, "\n");
8199 fflush (stderr);
8200 }
8201 /* A null message buffer means that the frame hasn't really been
8202 initialized yet. Error messages get reported properly by
8203 cmd_error, so this must be just an informative message; toss it. */
8204 else if (INTERACTIVE
8205 && sf->glyphs_initialized_p
8206 && FRAME_MESSAGE_BUF (sf))
8207 {
8208 Lisp_Object mini_window;
8209 struct frame *f;
8210
8211 /* Get the frame containing the mini-buffer
8212 that the selected frame is using. */
8213 mini_window = FRAME_MINIBUF_WINDOW (sf);
8214 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8215
8216 FRAME_SAMPLE_VISIBILITY (f);
8217 if (FRAME_VISIBLE_P (sf)
8218 && ! FRAME_VISIBLE_P (f))
8219 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8220
8221 if (m)
8222 {
8223 set_message (m, Qnil, nbytes, multibyte);
8224 if (minibuffer_auto_raise)
8225 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8226 }
8227 else
8228 clear_message (1, 1);
8229
8230 do_pending_window_change (0);
8231 echo_area_display (1);
8232 do_pending_window_change (0);
8233 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8234 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8235 }
8236 }
8237
8238
8239 /* Display an echo area message M with a specified length of NBYTES
8240 bytes. The string may include null characters. If M is not a
8241 string, clear out any existing message, and let the mini-buffer
8242 text show through.
8243
8244 This function cancels echoing. */
8245
8246 void
8247 message3 (m, nbytes, multibyte)
8248 Lisp_Object m;
8249 int nbytes;
8250 int multibyte;
8251 {
8252 struct gcpro gcpro1;
8253
8254 GCPRO1 (m);
8255 clear_message (1,1);
8256 cancel_echoing ();
8257
8258 /* First flush out any partial line written with print. */
8259 message_log_maybe_newline ();
8260 if (STRINGP (m))
8261 {
8262 char *buffer;
8263 USE_SAFE_ALLOCA;
8264
8265 SAFE_ALLOCA (buffer, char *, nbytes);
8266 bcopy (SDATA (m), buffer, nbytes);
8267 message_dolog (buffer, nbytes, 1, multibyte);
8268 SAFE_FREE ();
8269 }
8270 message3_nolog (m, nbytes, multibyte);
8271
8272 UNGCPRO;
8273 }
8274
8275
8276 /* The non-logging version of message3.
8277 This does not cancel echoing, because it is used for echoing.
8278 Perhaps we need to make a separate function for echoing
8279 and make this cancel echoing. */
8280
8281 void
8282 message3_nolog (m, nbytes, multibyte)
8283 Lisp_Object m;
8284 int nbytes, multibyte;
8285 {
8286 struct frame *sf = SELECTED_FRAME ();
8287 message_enable_multibyte = multibyte;
8288
8289 if (FRAME_INITIAL_P (sf))
8290 {
8291 if (noninteractive_need_newline)
8292 putc ('\n', stderr);
8293 noninteractive_need_newline = 0;
8294 if (STRINGP (m))
8295 fwrite (SDATA (m), nbytes, 1, stderr);
8296 if (cursor_in_echo_area == 0)
8297 fprintf (stderr, "\n");
8298 fflush (stderr);
8299 }
8300 /* A null message buffer means that the frame hasn't really been
8301 initialized yet. Error messages get reported properly by
8302 cmd_error, so this must be just an informative message; toss it. */
8303 else if (INTERACTIVE
8304 && sf->glyphs_initialized_p
8305 && FRAME_MESSAGE_BUF (sf))
8306 {
8307 Lisp_Object mini_window;
8308 Lisp_Object frame;
8309 struct frame *f;
8310
8311 /* Get the frame containing the mini-buffer
8312 that the selected frame is using. */
8313 mini_window = FRAME_MINIBUF_WINDOW (sf);
8314 frame = XWINDOW (mini_window)->frame;
8315 f = XFRAME (frame);
8316
8317 FRAME_SAMPLE_VISIBILITY (f);
8318 if (FRAME_VISIBLE_P (sf)
8319 && !FRAME_VISIBLE_P (f))
8320 Fmake_frame_visible (frame);
8321
8322 if (STRINGP (m) && SCHARS (m) > 0)
8323 {
8324 set_message (NULL, m, nbytes, multibyte);
8325 if (minibuffer_auto_raise)
8326 Fraise_frame (frame);
8327 /* Assume we are not echoing.
8328 (If we are, echo_now will override this.) */
8329 echo_message_buffer = Qnil;
8330 }
8331 else
8332 clear_message (1, 1);
8333
8334 do_pending_window_change (0);
8335 echo_area_display (1);
8336 do_pending_window_change (0);
8337 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8338 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8339 }
8340 }
8341
8342
8343 /* Display a null-terminated echo area message M. If M is 0, clear
8344 out any existing message, and let the mini-buffer text show through.
8345
8346 The buffer M must continue to exist until after the echo area gets
8347 cleared or some other message gets displayed there. Do not pass
8348 text that is stored in a Lisp string. Do not pass text in a buffer
8349 that was alloca'd. */
8350
8351 void
8352 message1 (m)
8353 char *m;
8354 {
8355 message2 (m, (m ? strlen (m) : 0), 0);
8356 }
8357
8358
8359 /* The non-logging counterpart of message1. */
8360
8361 void
8362 message1_nolog (m)
8363 char *m;
8364 {
8365 message2_nolog (m, (m ? strlen (m) : 0), 0);
8366 }
8367
8368 /* Display a message M which contains a single %s
8369 which gets replaced with STRING. */
8370
8371 void
8372 message_with_string (m, string, log)
8373 char *m;
8374 Lisp_Object string;
8375 int log;
8376 {
8377 CHECK_STRING (string);
8378
8379 if (noninteractive)
8380 {
8381 if (m)
8382 {
8383 if (noninteractive_need_newline)
8384 putc ('\n', stderr);
8385 noninteractive_need_newline = 0;
8386 fprintf (stderr, m, SDATA (string));
8387 if (!cursor_in_echo_area)
8388 fprintf (stderr, "\n");
8389 fflush (stderr);
8390 }
8391 }
8392 else if (INTERACTIVE)
8393 {
8394 /* The frame whose minibuffer we're going to display the message on.
8395 It may be larger than the selected frame, so we need
8396 to use its buffer, not the selected frame's buffer. */
8397 Lisp_Object mini_window;
8398 struct frame *f, *sf = SELECTED_FRAME ();
8399
8400 /* Get the frame containing the minibuffer
8401 that the selected frame is using. */
8402 mini_window = FRAME_MINIBUF_WINDOW (sf);
8403 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8404
8405 /* A null message buffer means that the frame hasn't really been
8406 initialized yet. Error messages get reported properly by
8407 cmd_error, so this must be just an informative message; toss it. */
8408 if (FRAME_MESSAGE_BUF (f))
8409 {
8410 Lisp_Object args[2], message;
8411 struct gcpro gcpro1, gcpro2;
8412
8413 args[0] = build_string (m);
8414 args[1] = message = string;
8415 GCPRO2 (args[0], message);
8416 gcpro1.nvars = 2;
8417
8418 message = Fformat (2, args);
8419
8420 if (log)
8421 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8422 else
8423 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8424
8425 UNGCPRO;
8426
8427 /* Print should start at the beginning of the message
8428 buffer next time. */
8429 message_buf_print = 0;
8430 }
8431 }
8432 }
8433
8434
8435 /* Dump an informative message to the minibuf. If M is 0, clear out
8436 any existing message, and let the mini-buffer text show through. */
8437
8438 /* VARARGS 1 */
8439 void
8440 message (m, a1, a2, a3)
8441 char *m;
8442 EMACS_INT a1, a2, a3;
8443 {
8444 if (noninteractive)
8445 {
8446 if (m)
8447 {
8448 if (noninteractive_need_newline)
8449 putc ('\n', stderr);
8450 noninteractive_need_newline = 0;
8451 fprintf (stderr, m, a1, a2, a3);
8452 if (cursor_in_echo_area == 0)
8453 fprintf (stderr, "\n");
8454 fflush (stderr);
8455 }
8456 }
8457 else if (INTERACTIVE)
8458 {
8459 /* The frame whose mini-buffer we're going to display the message
8460 on. It may be larger than the selected frame, so we need to
8461 use its buffer, not the selected frame's buffer. */
8462 Lisp_Object mini_window;
8463 struct frame *f, *sf = SELECTED_FRAME ();
8464
8465 /* Get the frame containing the mini-buffer
8466 that the selected frame is using. */
8467 mini_window = FRAME_MINIBUF_WINDOW (sf);
8468 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8469
8470 /* A null message buffer means that the frame hasn't really been
8471 initialized yet. Error messages get reported properly by
8472 cmd_error, so this must be just an informative message; toss
8473 it. */
8474 if (FRAME_MESSAGE_BUF (f))
8475 {
8476 if (m)
8477 {
8478 int len;
8479 #ifdef NO_ARG_ARRAY
8480 char *a[3];
8481 a[0] = (char *) a1;
8482 a[1] = (char *) a2;
8483 a[2] = (char *) a3;
8484
8485 len = doprnt (FRAME_MESSAGE_BUF (f),
8486 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8487 #else
8488 len = doprnt (FRAME_MESSAGE_BUF (f),
8489 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8490 (char **) &a1);
8491 #endif /* NO_ARG_ARRAY */
8492
8493 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8494 }
8495 else
8496 message1 (0);
8497
8498 /* Print should start at the beginning of the message
8499 buffer next time. */
8500 message_buf_print = 0;
8501 }
8502 }
8503 }
8504
8505
8506 /* The non-logging version of message. */
8507
8508 void
8509 message_nolog (m, a1, a2, a3)
8510 char *m;
8511 EMACS_INT a1, a2, a3;
8512 {
8513 Lisp_Object old_log_max;
8514 old_log_max = Vmessage_log_max;
8515 Vmessage_log_max = Qnil;
8516 message (m, a1, a2, a3);
8517 Vmessage_log_max = old_log_max;
8518 }
8519
8520
8521 /* Display the current message in the current mini-buffer. This is
8522 only called from error handlers in process.c, and is not time
8523 critical. */
8524
8525 void
8526 update_echo_area ()
8527 {
8528 if (!NILP (echo_area_buffer[0]))
8529 {
8530 Lisp_Object string;
8531 string = Fcurrent_message ();
8532 message3 (string, SBYTES (string),
8533 !NILP (current_buffer->enable_multibyte_characters));
8534 }
8535 }
8536
8537
8538 /* Make sure echo area buffers in `echo_buffers' are live.
8539 If they aren't, make new ones. */
8540
8541 static void
8542 ensure_echo_area_buffers ()
8543 {
8544 int i;
8545
8546 for (i = 0; i < 2; ++i)
8547 if (!BUFFERP (echo_buffer[i])
8548 || NILP (XBUFFER (echo_buffer[i])->name))
8549 {
8550 char name[30];
8551 Lisp_Object old_buffer;
8552 int j;
8553
8554 old_buffer = echo_buffer[i];
8555 sprintf (name, " *Echo Area %d*", i);
8556 echo_buffer[i] = Fget_buffer_create (build_string (name));
8557 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8558 /* to force word wrap in echo area -
8559 it was decided to postpone this*/
8560 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8561
8562 for (j = 0; j < 2; ++j)
8563 if (EQ (old_buffer, echo_area_buffer[j]))
8564 echo_area_buffer[j] = echo_buffer[i];
8565 }
8566 }
8567
8568
8569 /* Call FN with args A1..A4 with either the current or last displayed
8570 echo_area_buffer as current buffer.
8571
8572 WHICH zero means use the current message buffer
8573 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8574 from echo_buffer[] and clear it.
8575
8576 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8577 suitable buffer from echo_buffer[] and clear it.
8578
8579 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8580 that the current message becomes the last displayed one, make
8581 choose a suitable buffer for echo_area_buffer[0], and clear it.
8582
8583 Value is what FN returns. */
8584
8585 static int
8586 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8587 struct window *w;
8588 int which;
8589 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8590 EMACS_INT a1;
8591 Lisp_Object a2;
8592 EMACS_INT a3, a4;
8593 {
8594 Lisp_Object buffer;
8595 int this_one, the_other, clear_buffer_p, rc;
8596 int count = SPECPDL_INDEX ();
8597
8598 /* If buffers aren't live, make new ones. */
8599 ensure_echo_area_buffers ();
8600
8601 clear_buffer_p = 0;
8602
8603 if (which == 0)
8604 this_one = 0, the_other = 1;
8605 else if (which > 0)
8606 this_one = 1, the_other = 0;
8607 else
8608 {
8609 this_one = 0, the_other = 1;
8610 clear_buffer_p = 1;
8611
8612 /* We need a fresh one in case the current echo buffer equals
8613 the one containing the last displayed echo area message. */
8614 if (!NILP (echo_area_buffer[this_one])
8615 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8616 echo_area_buffer[this_one] = Qnil;
8617 }
8618
8619 /* Choose a suitable buffer from echo_buffer[] is we don't
8620 have one. */
8621 if (NILP (echo_area_buffer[this_one]))
8622 {
8623 echo_area_buffer[this_one]
8624 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8625 ? echo_buffer[the_other]
8626 : echo_buffer[this_one]);
8627 clear_buffer_p = 1;
8628 }
8629
8630 buffer = echo_area_buffer[this_one];
8631
8632 /* Don't get confused by reusing the buffer used for echoing
8633 for a different purpose. */
8634 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8635 cancel_echoing ();
8636
8637 record_unwind_protect (unwind_with_echo_area_buffer,
8638 with_echo_area_buffer_unwind_data (w));
8639
8640 /* Make the echo area buffer current. Note that for display
8641 purposes, it is not necessary that the displayed window's buffer
8642 == current_buffer, except for text property lookup. So, let's
8643 only set that buffer temporarily here without doing a full
8644 Fset_window_buffer. We must also change w->pointm, though,
8645 because otherwise an assertions in unshow_buffer fails, and Emacs
8646 aborts. */
8647 set_buffer_internal_1 (XBUFFER (buffer));
8648 if (w)
8649 {
8650 w->buffer = buffer;
8651 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8652 }
8653
8654 current_buffer->undo_list = Qt;
8655 current_buffer->read_only = Qnil;
8656 specbind (Qinhibit_read_only, Qt);
8657 specbind (Qinhibit_modification_hooks, Qt);
8658
8659 if (clear_buffer_p && Z > BEG)
8660 del_range (BEG, Z);
8661
8662 xassert (BEGV >= BEG);
8663 xassert (ZV <= Z && ZV >= BEGV);
8664
8665 rc = fn (a1, a2, a3, a4);
8666
8667 xassert (BEGV >= BEG);
8668 xassert (ZV <= Z && ZV >= BEGV);
8669
8670 unbind_to (count, Qnil);
8671 return rc;
8672 }
8673
8674
8675 /* Save state that should be preserved around the call to the function
8676 FN called in with_echo_area_buffer. */
8677
8678 static Lisp_Object
8679 with_echo_area_buffer_unwind_data (w)
8680 struct window *w;
8681 {
8682 int i = 0;
8683 Lisp_Object vector, tmp;
8684
8685 /* Reduce consing by keeping one vector in
8686 Vwith_echo_area_save_vector. */
8687 vector = Vwith_echo_area_save_vector;
8688 Vwith_echo_area_save_vector = Qnil;
8689
8690 if (NILP (vector))
8691 vector = Fmake_vector (make_number (7), Qnil);
8692
8693 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8694 ASET (vector, i, Vdeactivate_mark); ++i;
8695 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8696
8697 if (w)
8698 {
8699 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8700 ASET (vector, i, w->buffer); ++i;
8701 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8702 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8703 }
8704 else
8705 {
8706 int end = i + 4;
8707 for (; i < end; ++i)
8708 ASET (vector, i, Qnil);
8709 }
8710
8711 xassert (i == ASIZE (vector));
8712 return vector;
8713 }
8714
8715
8716 /* Restore global state from VECTOR which was created by
8717 with_echo_area_buffer_unwind_data. */
8718
8719 static Lisp_Object
8720 unwind_with_echo_area_buffer (vector)
8721 Lisp_Object vector;
8722 {
8723 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8724 Vdeactivate_mark = AREF (vector, 1);
8725 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8726
8727 if (WINDOWP (AREF (vector, 3)))
8728 {
8729 struct window *w;
8730 Lisp_Object buffer, charpos, bytepos;
8731
8732 w = XWINDOW (AREF (vector, 3));
8733 buffer = AREF (vector, 4);
8734 charpos = AREF (vector, 5);
8735 bytepos = AREF (vector, 6);
8736
8737 w->buffer = buffer;
8738 set_marker_both (w->pointm, buffer,
8739 XFASTINT (charpos), XFASTINT (bytepos));
8740 }
8741
8742 Vwith_echo_area_save_vector = vector;
8743 return Qnil;
8744 }
8745
8746
8747 /* Set up the echo area for use by print functions. MULTIBYTE_P
8748 non-zero means we will print multibyte. */
8749
8750 void
8751 setup_echo_area_for_printing (multibyte_p)
8752 int multibyte_p;
8753 {
8754 /* If we can't find an echo area any more, exit. */
8755 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8756 Fkill_emacs (Qnil);
8757
8758 ensure_echo_area_buffers ();
8759
8760 if (!message_buf_print)
8761 {
8762 /* A message has been output since the last time we printed.
8763 Choose a fresh echo area buffer. */
8764 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8765 echo_area_buffer[0] = echo_buffer[1];
8766 else
8767 echo_area_buffer[0] = echo_buffer[0];
8768
8769 /* Switch to that buffer and clear it. */
8770 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8771 current_buffer->truncate_lines = Qnil;
8772
8773 if (Z > BEG)
8774 {
8775 int count = SPECPDL_INDEX ();
8776 specbind (Qinhibit_read_only, Qt);
8777 /* Note that undo recording is always disabled. */
8778 del_range (BEG, Z);
8779 unbind_to (count, Qnil);
8780 }
8781 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8782
8783 /* Set up the buffer for the multibyteness we need. */
8784 if (multibyte_p
8785 != !NILP (current_buffer->enable_multibyte_characters))
8786 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8787
8788 /* Raise the frame containing the echo area. */
8789 if (minibuffer_auto_raise)
8790 {
8791 struct frame *sf = SELECTED_FRAME ();
8792 Lisp_Object mini_window;
8793 mini_window = FRAME_MINIBUF_WINDOW (sf);
8794 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8795 }
8796
8797 message_log_maybe_newline ();
8798 message_buf_print = 1;
8799 }
8800 else
8801 {
8802 if (NILP (echo_area_buffer[0]))
8803 {
8804 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8805 echo_area_buffer[0] = echo_buffer[1];
8806 else
8807 echo_area_buffer[0] = echo_buffer[0];
8808 }
8809
8810 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8811 {
8812 /* Someone switched buffers between print requests. */
8813 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8814 current_buffer->truncate_lines = Qnil;
8815 }
8816 }
8817 }
8818
8819
8820 /* Display an echo area message in window W. Value is non-zero if W's
8821 height is changed. If display_last_displayed_message_p is
8822 non-zero, display the message that was last displayed, otherwise
8823 display the current message. */
8824
8825 static int
8826 display_echo_area (w)
8827 struct window *w;
8828 {
8829 int i, no_message_p, window_height_changed_p, count;
8830
8831 /* Temporarily disable garbage collections while displaying the echo
8832 area. This is done because a GC can print a message itself.
8833 That message would modify the echo area buffer's contents while a
8834 redisplay of the buffer is going on, and seriously confuse
8835 redisplay. */
8836 count = inhibit_garbage_collection ();
8837
8838 /* If there is no message, we must call display_echo_area_1
8839 nevertheless because it resizes the window. But we will have to
8840 reset the echo_area_buffer in question to nil at the end because
8841 with_echo_area_buffer will sets it to an empty buffer. */
8842 i = display_last_displayed_message_p ? 1 : 0;
8843 no_message_p = NILP (echo_area_buffer[i]);
8844
8845 window_height_changed_p
8846 = with_echo_area_buffer (w, display_last_displayed_message_p,
8847 display_echo_area_1,
8848 (EMACS_INT) w, Qnil, 0, 0);
8849
8850 if (no_message_p)
8851 echo_area_buffer[i] = Qnil;
8852
8853 unbind_to (count, Qnil);
8854 return window_height_changed_p;
8855 }
8856
8857
8858 /* Helper for display_echo_area. Display the current buffer which
8859 contains the current echo area message in window W, a mini-window,
8860 a pointer to which is passed in A1. A2..A4 are currently not used.
8861 Change the height of W so that all of the message is displayed.
8862 Value is non-zero if height of W was changed. */
8863
8864 static int
8865 display_echo_area_1 (a1, a2, a3, a4)
8866 EMACS_INT a1;
8867 Lisp_Object a2;
8868 EMACS_INT a3, a4;
8869 {
8870 struct window *w = (struct window *) a1;
8871 Lisp_Object window;
8872 struct text_pos start;
8873 int window_height_changed_p = 0;
8874
8875 /* Do this before displaying, so that we have a large enough glyph
8876 matrix for the display. If we can't get enough space for the
8877 whole text, display the last N lines. That works by setting w->start. */
8878 window_height_changed_p = resize_mini_window (w, 0);
8879
8880 /* Use the starting position chosen by resize_mini_window. */
8881 SET_TEXT_POS_FROM_MARKER (start, w->start);
8882
8883 /* Display. */
8884 clear_glyph_matrix (w->desired_matrix);
8885 XSETWINDOW (window, w);
8886 try_window (window, start, 0);
8887
8888 return window_height_changed_p;
8889 }
8890
8891
8892 /* Resize the echo area window to exactly the size needed for the
8893 currently displayed message, if there is one. If a mini-buffer
8894 is active, don't shrink it. */
8895
8896 void
8897 resize_echo_area_exactly ()
8898 {
8899 if (BUFFERP (echo_area_buffer[0])
8900 && WINDOWP (echo_area_window))
8901 {
8902 struct window *w = XWINDOW (echo_area_window);
8903 int resized_p;
8904 Lisp_Object resize_exactly;
8905
8906 if (minibuf_level == 0)
8907 resize_exactly = Qt;
8908 else
8909 resize_exactly = Qnil;
8910
8911 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8912 (EMACS_INT) w, resize_exactly, 0, 0);
8913 if (resized_p)
8914 {
8915 ++windows_or_buffers_changed;
8916 ++update_mode_lines;
8917 redisplay_internal (0);
8918 }
8919 }
8920 }
8921
8922
8923 /* Callback function for with_echo_area_buffer, when used from
8924 resize_echo_area_exactly. A1 contains a pointer to the window to
8925 resize, EXACTLY non-nil means resize the mini-window exactly to the
8926 size of the text displayed. A3 and A4 are not used. Value is what
8927 resize_mini_window returns. */
8928
8929 static int
8930 resize_mini_window_1 (a1, exactly, a3, a4)
8931 EMACS_INT a1;
8932 Lisp_Object exactly;
8933 EMACS_INT a3, a4;
8934 {
8935 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8936 }
8937
8938
8939 /* Resize mini-window W to fit the size of its contents. EXACT_P
8940 means size the window exactly to the size needed. Otherwise, it's
8941 only enlarged until W's buffer is empty.
8942
8943 Set W->start to the right place to begin display. If the whole
8944 contents fit, start at the beginning. Otherwise, start so as
8945 to make the end of the contents appear. This is particularly
8946 important for y-or-n-p, but seems desirable generally.
8947
8948 Value is non-zero if the window height has been changed. */
8949
8950 int
8951 resize_mini_window (w, exact_p)
8952 struct window *w;
8953 int exact_p;
8954 {
8955 struct frame *f = XFRAME (w->frame);
8956 int window_height_changed_p = 0;
8957
8958 xassert (MINI_WINDOW_P (w));
8959
8960 /* By default, start display at the beginning. */
8961 set_marker_both (w->start, w->buffer,
8962 BUF_BEGV (XBUFFER (w->buffer)),
8963 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8964
8965 /* Don't resize windows while redisplaying a window; it would
8966 confuse redisplay functions when the size of the window they are
8967 displaying changes from under them. Such a resizing can happen,
8968 for instance, when which-func prints a long message while
8969 we are running fontification-functions. We're running these
8970 functions with safe_call which binds inhibit-redisplay to t. */
8971 if (!NILP (Vinhibit_redisplay))
8972 return 0;
8973
8974 /* Nil means don't try to resize. */
8975 if (NILP (Vresize_mini_windows)
8976 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8977 return 0;
8978
8979 if (!FRAME_MINIBUF_ONLY_P (f))
8980 {
8981 struct it it;
8982 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8983 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8984 int height, max_height;
8985 int unit = FRAME_LINE_HEIGHT (f);
8986 struct text_pos start;
8987 struct buffer *old_current_buffer = NULL;
8988
8989 if (current_buffer != XBUFFER (w->buffer))
8990 {
8991 old_current_buffer = current_buffer;
8992 set_buffer_internal (XBUFFER (w->buffer));
8993 }
8994
8995 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8996
8997 /* Compute the max. number of lines specified by the user. */
8998 if (FLOATP (Vmax_mini_window_height))
8999 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9000 else if (INTEGERP (Vmax_mini_window_height))
9001 max_height = XINT (Vmax_mini_window_height);
9002 else
9003 max_height = total_height / 4;
9004
9005 /* Correct that max. height if it's bogus. */
9006 max_height = max (1, max_height);
9007 max_height = min (total_height, max_height);
9008
9009 /* Find out the height of the text in the window. */
9010 if (it.line_wrap == TRUNCATE)
9011 height = 1;
9012 else
9013 {
9014 last_height = 0;
9015 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9016 if (it.max_ascent == 0 && it.max_descent == 0)
9017 height = it.current_y + last_height;
9018 else
9019 height = it.current_y + it.max_ascent + it.max_descent;
9020 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9021 height = (height + unit - 1) / unit;
9022 }
9023
9024 /* Compute a suitable window start. */
9025 if (height > max_height)
9026 {
9027 height = max_height;
9028 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9029 move_it_vertically_backward (&it, (height - 1) * unit);
9030 start = it.current.pos;
9031 }
9032 else
9033 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9034 SET_MARKER_FROM_TEXT_POS (w->start, start);
9035
9036 if (EQ (Vresize_mini_windows, Qgrow_only))
9037 {
9038 /* Let it grow only, until we display an empty message, in which
9039 case the window shrinks again. */
9040 if (height > WINDOW_TOTAL_LINES (w))
9041 {
9042 int old_height = WINDOW_TOTAL_LINES (w);
9043 freeze_window_starts (f, 1);
9044 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9045 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9046 }
9047 else if (height < WINDOW_TOTAL_LINES (w)
9048 && (exact_p || BEGV == ZV))
9049 {
9050 int old_height = WINDOW_TOTAL_LINES (w);
9051 freeze_window_starts (f, 0);
9052 shrink_mini_window (w);
9053 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9054 }
9055 }
9056 else
9057 {
9058 /* Always resize to exact size needed. */
9059 if (height > WINDOW_TOTAL_LINES (w))
9060 {
9061 int old_height = WINDOW_TOTAL_LINES (w);
9062 freeze_window_starts (f, 1);
9063 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9064 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9065 }
9066 else if (height < WINDOW_TOTAL_LINES (w))
9067 {
9068 int old_height = WINDOW_TOTAL_LINES (w);
9069 freeze_window_starts (f, 0);
9070 shrink_mini_window (w);
9071
9072 if (height)
9073 {
9074 freeze_window_starts (f, 1);
9075 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9076 }
9077
9078 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9079 }
9080 }
9081
9082 if (old_current_buffer)
9083 set_buffer_internal (old_current_buffer);
9084 }
9085
9086 return window_height_changed_p;
9087 }
9088
9089
9090 /* Value is the current message, a string, or nil if there is no
9091 current message. */
9092
9093 Lisp_Object
9094 current_message ()
9095 {
9096 Lisp_Object msg;
9097
9098 if (!BUFFERP (echo_area_buffer[0]))
9099 msg = Qnil;
9100 else
9101 {
9102 with_echo_area_buffer (0, 0, current_message_1,
9103 (EMACS_INT) &msg, Qnil, 0, 0);
9104 if (NILP (msg))
9105 echo_area_buffer[0] = Qnil;
9106 }
9107
9108 return msg;
9109 }
9110
9111
9112 static int
9113 current_message_1 (a1, a2, a3, a4)
9114 EMACS_INT a1;
9115 Lisp_Object a2;
9116 EMACS_INT a3, a4;
9117 {
9118 Lisp_Object *msg = (Lisp_Object *) a1;
9119
9120 if (Z > BEG)
9121 *msg = make_buffer_string (BEG, Z, 1);
9122 else
9123 *msg = Qnil;
9124 return 0;
9125 }
9126
9127
9128 /* Push the current message on Vmessage_stack for later restauration
9129 by restore_message. Value is non-zero if the current message isn't
9130 empty. This is a relatively infrequent operation, so it's not
9131 worth optimizing. */
9132
9133 int
9134 push_message ()
9135 {
9136 Lisp_Object msg;
9137 msg = current_message ();
9138 Vmessage_stack = Fcons (msg, Vmessage_stack);
9139 return STRINGP (msg);
9140 }
9141
9142
9143 /* Restore message display from the top of Vmessage_stack. */
9144
9145 void
9146 restore_message ()
9147 {
9148 Lisp_Object msg;
9149
9150 xassert (CONSP (Vmessage_stack));
9151 msg = XCAR (Vmessage_stack);
9152 if (STRINGP (msg))
9153 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9154 else
9155 message3_nolog (msg, 0, 0);
9156 }
9157
9158
9159 /* Handler for record_unwind_protect calling pop_message. */
9160
9161 Lisp_Object
9162 pop_message_unwind (dummy)
9163 Lisp_Object dummy;
9164 {
9165 pop_message ();
9166 return Qnil;
9167 }
9168
9169 /* Pop the top-most entry off Vmessage_stack. */
9170
9171 void
9172 pop_message ()
9173 {
9174 xassert (CONSP (Vmessage_stack));
9175 Vmessage_stack = XCDR (Vmessage_stack);
9176 }
9177
9178
9179 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9180 exits. If the stack is not empty, we have a missing pop_message
9181 somewhere. */
9182
9183 void
9184 check_message_stack ()
9185 {
9186 if (!NILP (Vmessage_stack))
9187 abort ();
9188 }
9189
9190
9191 /* Truncate to NCHARS what will be displayed in the echo area the next
9192 time we display it---but don't redisplay it now. */
9193
9194 void
9195 truncate_echo_area (nchars)
9196 int nchars;
9197 {
9198 if (nchars == 0)
9199 echo_area_buffer[0] = Qnil;
9200 /* A null message buffer means that the frame hasn't really been
9201 initialized yet. Error messages get reported properly by
9202 cmd_error, so this must be just an informative message; toss it. */
9203 else if (!noninteractive
9204 && INTERACTIVE
9205 && !NILP (echo_area_buffer[0]))
9206 {
9207 struct frame *sf = SELECTED_FRAME ();
9208 if (FRAME_MESSAGE_BUF (sf))
9209 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9210 }
9211 }
9212
9213
9214 /* Helper function for truncate_echo_area. Truncate the current
9215 message to at most NCHARS characters. */
9216
9217 static int
9218 truncate_message_1 (nchars, a2, a3, a4)
9219 EMACS_INT nchars;
9220 Lisp_Object a2;
9221 EMACS_INT a3, a4;
9222 {
9223 if (BEG + nchars < Z)
9224 del_range (BEG + nchars, Z);
9225 if (Z == BEG)
9226 echo_area_buffer[0] = Qnil;
9227 return 0;
9228 }
9229
9230
9231 /* Set the current message to a substring of S or STRING.
9232
9233 If STRING is a Lisp string, set the message to the first NBYTES
9234 bytes from STRING. NBYTES zero means use the whole string. If
9235 STRING is multibyte, the message will be displayed multibyte.
9236
9237 If S is not null, set the message to the first LEN bytes of S. LEN
9238 zero means use the whole string. MULTIBYTE_P non-zero means S is
9239 multibyte. Display the message multibyte in that case.
9240
9241 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9242 to t before calling set_message_1 (which calls insert).
9243 */
9244
9245 void
9246 set_message (s, string, nbytes, multibyte_p)
9247 const char *s;
9248 Lisp_Object string;
9249 int nbytes, multibyte_p;
9250 {
9251 message_enable_multibyte
9252 = ((s && multibyte_p)
9253 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9254
9255 with_echo_area_buffer (0, -1, set_message_1,
9256 (EMACS_INT) s, string, nbytes, multibyte_p);
9257 message_buf_print = 0;
9258 help_echo_showing_p = 0;
9259 }
9260
9261
9262 /* Helper function for set_message. Arguments have the same meaning
9263 as there, with A1 corresponding to S and A2 corresponding to STRING
9264 This function is called with the echo area buffer being
9265 current. */
9266
9267 static int
9268 set_message_1 (a1, a2, nbytes, multibyte_p)
9269 EMACS_INT a1;
9270 Lisp_Object a2;
9271 EMACS_INT nbytes, multibyte_p;
9272 {
9273 const char *s = (const char *) a1;
9274 Lisp_Object string = a2;
9275
9276 /* Change multibyteness of the echo buffer appropriately. */
9277 if (message_enable_multibyte
9278 != !NILP (current_buffer->enable_multibyte_characters))
9279 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9280
9281 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9282
9283 /* Insert new message at BEG. */
9284 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9285
9286 if (STRINGP (string))
9287 {
9288 int nchars;
9289
9290 if (nbytes == 0)
9291 nbytes = SBYTES (string);
9292 nchars = string_byte_to_char (string, nbytes);
9293
9294 /* This function takes care of single/multibyte conversion. We
9295 just have to ensure that the echo area buffer has the right
9296 setting of enable_multibyte_characters. */
9297 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9298 }
9299 else if (s)
9300 {
9301 if (nbytes == 0)
9302 nbytes = strlen (s);
9303
9304 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9305 {
9306 /* Convert from multi-byte to single-byte. */
9307 int i, c, n;
9308 unsigned char work[1];
9309
9310 /* Convert a multibyte string to single-byte. */
9311 for (i = 0; i < nbytes; i += n)
9312 {
9313 c = string_char_and_length (s + i, &n);
9314 work[0] = (ASCII_CHAR_P (c)
9315 ? c
9316 : multibyte_char_to_unibyte (c, Qnil));
9317 insert_1_both (work, 1, 1, 1, 0, 0);
9318 }
9319 }
9320 else if (!multibyte_p
9321 && !NILP (current_buffer->enable_multibyte_characters))
9322 {
9323 /* Convert from single-byte to multi-byte. */
9324 int i, c, n;
9325 const unsigned char *msg = (const unsigned char *) s;
9326 unsigned char str[MAX_MULTIBYTE_LENGTH];
9327
9328 /* Convert a single-byte string to multibyte. */
9329 for (i = 0; i < nbytes; i++)
9330 {
9331 c = msg[i];
9332 MAKE_CHAR_MULTIBYTE (c);
9333 n = CHAR_STRING (c, str);
9334 insert_1_both (str, 1, n, 1, 0, 0);
9335 }
9336 }
9337 else
9338 insert_1 (s, nbytes, 1, 0, 0);
9339 }
9340
9341 return 0;
9342 }
9343
9344
9345 /* Clear messages. CURRENT_P non-zero means clear the current
9346 message. LAST_DISPLAYED_P non-zero means clear the message
9347 last displayed. */
9348
9349 void
9350 clear_message (current_p, last_displayed_p)
9351 int current_p, last_displayed_p;
9352 {
9353 if (current_p)
9354 {
9355 echo_area_buffer[0] = Qnil;
9356 message_cleared_p = 1;
9357 }
9358
9359 if (last_displayed_p)
9360 echo_area_buffer[1] = Qnil;
9361
9362 message_buf_print = 0;
9363 }
9364
9365 /* Clear garbaged frames.
9366
9367 This function is used where the old redisplay called
9368 redraw_garbaged_frames which in turn called redraw_frame which in
9369 turn called clear_frame. The call to clear_frame was a source of
9370 flickering. I believe a clear_frame is not necessary. It should
9371 suffice in the new redisplay to invalidate all current matrices,
9372 and ensure a complete redisplay of all windows. */
9373
9374 static void
9375 clear_garbaged_frames ()
9376 {
9377 if (frame_garbaged)
9378 {
9379 Lisp_Object tail, frame;
9380 int changed_count = 0;
9381
9382 FOR_EACH_FRAME (tail, frame)
9383 {
9384 struct frame *f = XFRAME (frame);
9385
9386 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9387 {
9388 if (f->resized_p)
9389 {
9390 Fredraw_frame (frame);
9391 f->force_flush_display_p = 1;
9392 }
9393 clear_current_matrices (f);
9394 changed_count++;
9395 f->garbaged = 0;
9396 f->resized_p = 0;
9397 }
9398 }
9399
9400 frame_garbaged = 0;
9401 if (changed_count)
9402 ++windows_or_buffers_changed;
9403 }
9404 }
9405
9406
9407 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9408 is non-zero update selected_frame. Value is non-zero if the
9409 mini-windows height has been changed. */
9410
9411 static int
9412 echo_area_display (update_frame_p)
9413 int update_frame_p;
9414 {
9415 Lisp_Object mini_window;
9416 struct window *w;
9417 struct frame *f;
9418 int window_height_changed_p = 0;
9419 struct frame *sf = SELECTED_FRAME ();
9420
9421 mini_window = FRAME_MINIBUF_WINDOW (sf);
9422 w = XWINDOW (mini_window);
9423 f = XFRAME (WINDOW_FRAME (w));
9424
9425 /* Don't display if frame is invisible or not yet initialized. */
9426 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9427 return 0;
9428
9429 #ifdef HAVE_WINDOW_SYSTEM
9430 /* When Emacs starts, selected_frame may be the initial terminal
9431 frame. If we let this through, a message would be displayed on
9432 the terminal. */
9433 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9434 return 0;
9435 #endif /* HAVE_WINDOW_SYSTEM */
9436
9437 /* Redraw garbaged frames. */
9438 if (frame_garbaged)
9439 clear_garbaged_frames ();
9440
9441 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9442 {
9443 echo_area_window = mini_window;
9444 window_height_changed_p = display_echo_area (w);
9445 w->must_be_updated_p = 1;
9446
9447 /* Update the display, unless called from redisplay_internal.
9448 Also don't update the screen during redisplay itself. The
9449 update will happen at the end of redisplay, and an update
9450 here could cause confusion. */
9451 if (update_frame_p && !redisplaying_p)
9452 {
9453 int n = 0;
9454
9455 /* If the display update has been interrupted by pending
9456 input, update mode lines in the frame. Due to the
9457 pending input, it might have been that redisplay hasn't
9458 been called, so that mode lines above the echo area are
9459 garbaged. This looks odd, so we prevent it here. */
9460 if (!display_completed)
9461 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9462
9463 if (window_height_changed_p
9464 /* Don't do this if Emacs is shutting down. Redisplay
9465 needs to run hooks. */
9466 && !NILP (Vrun_hooks))
9467 {
9468 /* Must update other windows. Likewise as in other
9469 cases, don't let this update be interrupted by
9470 pending input. */
9471 int count = SPECPDL_INDEX ();
9472 specbind (Qredisplay_dont_pause, Qt);
9473 windows_or_buffers_changed = 1;
9474 redisplay_internal (0);
9475 unbind_to (count, Qnil);
9476 }
9477 else if (FRAME_WINDOW_P (f) && n == 0)
9478 {
9479 /* Window configuration is the same as before.
9480 Can do with a display update of the echo area,
9481 unless we displayed some mode lines. */
9482 update_single_window (w, 1);
9483 FRAME_RIF (f)->flush_display (f);
9484 }
9485 else
9486 update_frame (f, 1, 1);
9487
9488 /* If cursor is in the echo area, make sure that the next
9489 redisplay displays the minibuffer, so that the cursor will
9490 be replaced with what the minibuffer wants. */
9491 if (cursor_in_echo_area)
9492 ++windows_or_buffers_changed;
9493 }
9494 }
9495 else if (!EQ (mini_window, selected_window))
9496 windows_or_buffers_changed++;
9497
9498 /* Last displayed message is now the current message. */
9499 echo_area_buffer[1] = echo_area_buffer[0];
9500 /* Inform read_char that we're not echoing. */
9501 echo_message_buffer = Qnil;
9502
9503 /* Prevent redisplay optimization in redisplay_internal by resetting
9504 this_line_start_pos. This is done because the mini-buffer now
9505 displays the message instead of its buffer text. */
9506 if (EQ (mini_window, selected_window))
9507 CHARPOS (this_line_start_pos) = 0;
9508
9509 return window_height_changed_p;
9510 }
9511
9512
9513 \f
9514 /***********************************************************************
9515 Mode Lines and Frame Titles
9516 ***********************************************************************/
9517
9518 /* A buffer for constructing non-propertized mode-line strings and
9519 frame titles in it; allocated from the heap in init_xdisp and
9520 resized as needed in store_mode_line_noprop_char. */
9521
9522 static char *mode_line_noprop_buf;
9523
9524 /* The buffer's end, and a current output position in it. */
9525
9526 static char *mode_line_noprop_buf_end;
9527 static char *mode_line_noprop_ptr;
9528
9529 #define MODE_LINE_NOPROP_LEN(start) \
9530 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9531
9532 static enum {
9533 MODE_LINE_DISPLAY = 0,
9534 MODE_LINE_TITLE,
9535 MODE_LINE_NOPROP,
9536 MODE_LINE_STRING
9537 } mode_line_target;
9538
9539 /* Alist that caches the results of :propertize.
9540 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9541 static Lisp_Object mode_line_proptrans_alist;
9542
9543 /* List of strings making up the mode-line. */
9544 static Lisp_Object mode_line_string_list;
9545
9546 /* Base face property when building propertized mode line string. */
9547 static Lisp_Object mode_line_string_face;
9548 static Lisp_Object mode_line_string_face_prop;
9549
9550
9551 /* Unwind data for mode line strings */
9552
9553 static Lisp_Object Vmode_line_unwind_vector;
9554
9555 static Lisp_Object
9556 format_mode_line_unwind_data (struct buffer *obuf,
9557 Lisp_Object owin,
9558 int save_proptrans)
9559 {
9560 Lisp_Object vector, tmp;
9561
9562 /* Reduce consing by keeping one vector in
9563 Vwith_echo_area_save_vector. */
9564 vector = Vmode_line_unwind_vector;
9565 Vmode_line_unwind_vector = Qnil;
9566
9567 if (NILP (vector))
9568 vector = Fmake_vector (make_number (8), Qnil);
9569
9570 ASET (vector, 0, make_number (mode_line_target));
9571 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9572 ASET (vector, 2, mode_line_string_list);
9573 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9574 ASET (vector, 4, mode_line_string_face);
9575 ASET (vector, 5, mode_line_string_face_prop);
9576
9577 if (obuf)
9578 XSETBUFFER (tmp, obuf);
9579 else
9580 tmp = Qnil;
9581 ASET (vector, 6, tmp);
9582 ASET (vector, 7, owin);
9583
9584 return vector;
9585 }
9586
9587 static Lisp_Object
9588 unwind_format_mode_line (vector)
9589 Lisp_Object vector;
9590 {
9591 mode_line_target = XINT (AREF (vector, 0));
9592 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9593 mode_line_string_list = AREF (vector, 2);
9594 if (! EQ (AREF (vector, 3), Qt))
9595 mode_line_proptrans_alist = AREF (vector, 3);
9596 mode_line_string_face = AREF (vector, 4);
9597 mode_line_string_face_prop = AREF (vector, 5);
9598
9599 if (!NILP (AREF (vector, 7)))
9600 /* Select window before buffer, since it may change the buffer. */
9601 Fselect_window (AREF (vector, 7), Qt);
9602
9603 if (!NILP (AREF (vector, 6)))
9604 {
9605 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9606 ASET (vector, 6, Qnil);
9607 }
9608
9609 Vmode_line_unwind_vector = vector;
9610 return Qnil;
9611 }
9612
9613
9614 /* Store a single character C for the frame title in mode_line_noprop_buf.
9615 Re-allocate mode_line_noprop_buf if necessary. */
9616
9617 static void
9618 #ifdef PROTOTYPES
9619 store_mode_line_noprop_char (char c)
9620 #else
9621 store_mode_line_noprop_char (c)
9622 char c;
9623 #endif
9624 {
9625 /* If output position has reached the end of the allocated buffer,
9626 double the buffer's size. */
9627 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9628 {
9629 int len = MODE_LINE_NOPROP_LEN (0);
9630 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9631 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9632 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9633 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9634 }
9635
9636 *mode_line_noprop_ptr++ = c;
9637 }
9638
9639
9640 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9641 mode_line_noprop_ptr. STR is the string to store. Do not copy
9642 characters that yield more columns than PRECISION; PRECISION <= 0
9643 means copy the whole string. Pad with spaces until FIELD_WIDTH
9644 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9645 pad. Called from display_mode_element when it is used to build a
9646 frame title. */
9647
9648 static int
9649 store_mode_line_noprop (str, field_width, precision)
9650 const unsigned char *str;
9651 int field_width, precision;
9652 {
9653 int n = 0;
9654 int dummy, nbytes;
9655
9656 /* Copy at most PRECISION chars from STR. */
9657 nbytes = strlen (str);
9658 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9659 while (nbytes--)
9660 store_mode_line_noprop_char (*str++);
9661
9662 /* Fill up with spaces until FIELD_WIDTH reached. */
9663 while (field_width > 0
9664 && n < field_width)
9665 {
9666 store_mode_line_noprop_char (' ');
9667 ++n;
9668 }
9669
9670 return n;
9671 }
9672
9673 /***********************************************************************
9674 Frame Titles
9675 ***********************************************************************/
9676
9677 #ifdef HAVE_WINDOW_SYSTEM
9678
9679 /* Set the title of FRAME, if it has changed. The title format is
9680 Vicon_title_format if FRAME is iconified, otherwise it is
9681 frame_title_format. */
9682
9683 static void
9684 x_consider_frame_title (frame)
9685 Lisp_Object frame;
9686 {
9687 struct frame *f = XFRAME (frame);
9688
9689 if (FRAME_WINDOW_P (f)
9690 || FRAME_MINIBUF_ONLY_P (f)
9691 || f->explicit_name)
9692 {
9693 /* Do we have more than one visible frame on this X display? */
9694 Lisp_Object tail;
9695 Lisp_Object fmt;
9696 int title_start;
9697 char *title;
9698 int len;
9699 struct it it;
9700 int count = SPECPDL_INDEX ();
9701
9702 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9703 {
9704 Lisp_Object other_frame = XCAR (tail);
9705 struct frame *tf = XFRAME (other_frame);
9706
9707 if (tf != f
9708 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9709 && !FRAME_MINIBUF_ONLY_P (tf)
9710 && !EQ (other_frame, tip_frame)
9711 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9712 break;
9713 }
9714
9715 /* Set global variable indicating that multiple frames exist. */
9716 multiple_frames = CONSP (tail);
9717
9718 /* Switch to the buffer of selected window of the frame. Set up
9719 mode_line_target so that display_mode_element will output into
9720 mode_line_noprop_buf; then display the title. */
9721 record_unwind_protect (unwind_format_mode_line,
9722 format_mode_line_unwind_data
9723 (current_buffer, selected_window, 0));
9724
9725 Fselect_window (f->selected_window, Qt);
9726 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9727 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9728
9729 mode_line_target = MODE_LINE_TITLE;
9730 title_start = MODE_LINE_NOPROP_LEN (0);
9731 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9732 NULL, DEFAULT_FACE_ID);
9733 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9734 len = MODE_LINE_NOPROP_LEN (title_start);
9735 title = mode_line_noprop_buf + title_start;
9736 unbind_to (count, Qnil);
9737
9738 /* Set the title only if it's changed. This avoids consing in
9739 the common case where it hasn't. (If it turns out that we've
9740 already wasted too much time by walking through the list with
9741 display_mode_element, then we might need to optimize at a
9742 higher level than this.) */
9743 if (! STRINGP (f->name)
9744 || SBYTES (f->name) != len
9745 || bcmp (title, SDATA (f->name), len) != 0)
9746 x_implicitly_set_name (f, make_string (title, len), Qnil);
9747 }
9748 }
9749
9750 #endif /* not HAVE_WINDOW_SYSTEM */
9751
9752
9753
9754 \f
9755 /***********************************************************************
9756 Menu Bars
9757 ***********************************************************************/
9758
9759
9760 /* Prepare for redisplay by updating menu-bar item lists when
9761 appropriate. This can call eval. */
9762
9763 void
9764 prepare_menu_bars ()
9765 {
9766 int all_windows;
9767 struct gcpro gcpro1, gcpro2;
9768 struct frame *f;
9769 Lisp_Object tooltip_frame;
9770
9771 #ifdef HAVE_WINDOW_SYSTEM
9772 tooltip_frame = tip_frame;
9773 #else
9774 tooltip_frame = Qnil;
9775 #endif
9776
9777 /* Update all frame titles based on their buffer names, etc. We do
9778 this before the menu bars so that the buffer-menu will show the
9779 up-to-date frame titles. */
9780 #ifdef HAVE_WINDOW_SYSTEM
9781 if (windows_or_buffers_changed || update_mode_lines)
9782 {
9783 Lisp_Object tail, frame;
9784
9785 FOR_EACH_FRAME (tail, frame)
9786 {
9787 f = XFRAME (frame);
9788 if (!EQ (frame, tooltip_frame)
9789 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9790 x_consider_frame_title (frame);
9791 }
9792 }
9793 #endif /* HAVE_WINDOW_SYSTEM */
9794
9795 /* Update the menu bar item lists, if appropriate. This has to be
9796 done before any actual redisplay or generation of display lines. */
9797 all_windows = (update_mode_lines
9798 || buffer_shared > 1
9799 || windows_or_buffers_changed);
9800 if (all_windows)
9801 {
9802 Lisp_Object tail, frame;
9803 int count = SPECPDL_INDEX ();
9804 /* 1 means that update_menu_bar has run its hooks
9805 so any further calls to update_menu_bar shouldn't do so again. */
9806 int menu_bar_hooks_run = 0;
9807
9808 record_unwind_save_match_data ();
9809
9810 FOR_EACH_FRAME (tail, frame)
9811 {
9812 f = XFRAME (frame);
9813
9814 /* Ignore tooltip frame. */
9815 if (EQ (frame, tooltip_frame))
9816 continue;
9817
9818 /* If a window on this frame changed size, report that to
9819 the user and clear the size-change flag. */
9820 if (FRAME_WINDOW_SIZES_CHANGED (f))
9821 {
9822 Lisp_Object functions;
9823
9824 /* Clear flag first in case we get an error below. */
9825 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9826 functions = Vwindow_size_change_functions;
9827 GCPRO2 (tail, functions);
9828
9829 while (CONSP (functions))
9830 {
9831 if (!EQ (XCAR (functions), Qt))
9832 call1 (XCAR (functions), frame);
9833 functions = XCDR (functions);
9834 }
9835 UNGCPRO;
9836 }
9837
9838 GCPRO1 (tail);
9839 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9840 #ifdef HAVE_WINDOW_SYSTEM
9841 update_tool_bar (f, 0);
9842 #endif
9843 #ifdef HAVE_NS
9844 if (windows_or_buffers_changed)
9845 ns_set_doc_edited (f, Fbuffer_modified_p
9846 (XWINDOW (f->selected_window)->buffer));
9847 #endif
9848 UNGCPRO;
9849 }
9850
9851 unbind_to (count, Qnil);
9852 }
9853 else
9854 {
9855 struct frame *sf = SELECTED_FRAME ();
9856 update_menu_bar (sf, 1, 0);
9857 #ifdef HAVE_WINDOW_SYSTEM
9858 update_tool_bar (sf, 1);
9859 #endif
9860 }
9861
9862 /* Motif needs this. See comment in xmenu.c. Turn it off when
9863 pending_menu_activation is not defined. */
9864 #ifdef USE_X_TOOLKIT
9865 pending_menu_activation = 0;
9866 #endif
9867 }
9868
9869
9870 /* Update the menu bar item list for frame F. This has to be done
9871 before we start to fill in any display lines, because it can call
9872 eval.
9873
9874 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9875
9876 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9877 already ran the menu bar hooks for this redisplay, so there
9878 is no need to run them again. The return value is the
9879 updated value of this flag, to pass to the next call. */
9880
9881 static int
9882 update_menu_bar (f, save_match_data, hooks_run)
9883 struct frame *f;
9884 int save_match_data;
9885 int hooks_run;
9886 {
9887 Lisp_Object window;
9888 register struct window *w;
9889
9890 /* If called recursively during a menu update, do nothing. This can
9891 happen when, for instance, an activate-menubar-hook causes a
9892 redisplay. */
9893 if (inhibit_menubar_update)
9894 return hooks_run;
9895
9896 window = FRAME_SELECTED_WINDOW (f);
9897 w = XWINDOW (window);
9898
9899 if (FRAME_WINDOW_P (f)
9900 ?
9901 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9902 || defined (HAVE_NS) || defined (USE_GTK)
9903 FRAME_EXTERNAL_MENU_BAR (f)
9904 #else
9905 FRAME_MENU_BAR_LINES (f) > 0
9906 #endif
9907 : FRAME_MENU_BAR_LINES (f) > 0)
9908 {
9909 /* If the user has switched buffers or windows, we need to
9910 recompute to reflect the new bindings. But we'll
9911 recompute when update_mode_lines is set too; that means
9912 that people can use force-mode-line-update to request
9913 that the menu bar be recomputed. The adverse effect on
9914 the rest of the redisplay algorithm is about the same as
9915 windows_or_buffers_changed anyway. */
9916 if (windows_or_buffers_changed
9917 /* This used to test w->update_mode_line, but we believe
9918 there is no need to recompute the menu in that case. */
9919 || update_mode_lines
9920 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9921 < BUF_MODIFF (XBUFFER (w->buffer)))
9922 != !NILP (w->last_had_star))
9923 || ((!NILP (Vtransient_mark_mode)
9924 && !NILP (XBUFFER (w->buffer)->mark_active))
9925 != !NILP (w->region_showing)))
9926 {
9927 struct buffer *prev = current_buffer;
9928 int count = SPECPDL_INDEX ();
9929
9930 specbind (Qinhibit_menubar_update, Qt);
9931
9932 set_buffer_internal_1 (XBUFFER (w->buffer));
9933 if (save_match_data)
9934 record_unwind_save_match_data ();
9935 if (NILP (Voverriding_local_map_menu_flag))
9936 {
9937 specbind (Qoverriding_terminal_local_map, Qnil);
9938 specbind (Qoverriding_local_map, Qnil);
9939 }
9940
9941 if (!hooks_run)
9942 {
9943 /* Run the Lucid hook. */
9944 safe_run_hooks (Qactivate_menubar_hook);
9945
9946 /* If it has changed current-menubar from previous value,
9947 really recompute the menu-bar from the value. */
9948 if (! NILP (Vlucid_menu_bar_dirty_flag))
9949 call0 (Qrecompute_lucid_menubar);
9950
9951 safe_run_hooks (Qmenu_bar_update_hook);
9952
9953 hooks_run = 1;
9954 }
9955
9956 XSETFRAME (Vmenu_updating_frame, f);
9957 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9958
9959 /* Redisplay the menu bar in case we changed it. */
9960 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9961 || defined (HAVE_NS) || defined (USE_GTK)
9962 if (FRAME_WINDOW_P (f))
9963 {
9964 #if defined (HAVE_NS)
9965 /* All frames on Mac OS share the same menubar. So only
9966 the selected frame should be allowed to set it. */
9967 if (f == SELECTED_FRAME ())
9968 #endif
9969 set_frame_menubar (f, 0, 0);
9970 }
9971 else
9972 /* On a terminal screen, the menu bar is an ordinary screen
9973 line, and this makes it get updated. */
9974 w->update_mode_line = Qt;
9975 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9976 /* In the non-toolkit version, the menu bar is an ordinary screen
9977 line, and this makes it get updated. */
9978 w->update_mode_line = Qt;
9979 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9980
9981 unbind_to (count, Qnil);
9982 set_buffer_internal_1 (prev);
9983 }
9984 }
9985
9986 return hooks_run;
9987 }
9988
9989
9990 \f
9991 /***********************************************************************
9992 Output Cursor
9993 ***********************************************************************/
9994
9995 #ifdef HAVE_WINDOW_SYSTEM
9996
9997 /* EXPORT:
9998 Nominal cursor position -- where to draw output.
9999 HPOS and VPOS are window relative glyph matrix coordinates.
10000 X and Y are window relative pixel coordinates. */
10001
10002 struct cursor_pos output_cursor;
10003
10004
10005 /* EXPORT:
10006 Set the global variable output_cursor to CURSOR. All cursor
10007 positions are relative to updated_window. */
10008
10009 void
10010 set_output_cursor (cursor)
10011 struct cursor_pos *cursor;
10012 {
10013 output_cursor.hpos = cursor->hpos;
10014 output_cursor.vpos = cursor->vpos;
10015 output_cursor.x = cursor->x;
10016 output_cursor.y = cursor->y;
10017 }
10018
10019
10020 /* EXPORT for RIF:
10021 Set a nominal cursor position.
10022
10023 HPOS and VPOS are column/row positions in a window glyph matrix. X
10024 and Y are window text area relative pixel positions.
10025
10026 If this is done during an update, updated_window will contain the
10027 window that is being updated and the position is the future output
10028 cursor position for that window. If updated_window is null, use
10029 selected_window and display the cursor at the given position. */
10030
10031 void
10032 x_cursor_to (vpos, hpos, y, x)
10033 int vpos, hpos, y, x;
10034 {
10035 struct window *w;
10036
10037 /* If updated_window is not set, work on selected_window. */
10038 if (updated_window)
10039 w = updated_window;
10040 else
10041 w = XWINDOW (selected_window);
10042
10043 /* Set the output cursor. */
10044 output_cursor.hpos = hpos;
10045 output_cursor.vpos = vpos;
10046 output_cursor.x = x;
10047 output_cursor.y = y;
10048
10049 /* If not called as part of an update, really display the cursor.
10050 This will also set the cursor position of W. */
10051 if (updated_window == NULL)
10052 {
10053 BLOCK_INPUT;
10054 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10055 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10056 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10057 UNBLOCK_INPUT;
10058 }
10059 }
10060
10061 #endif /* HAVE_WINDOW_SYSTEM */
10062
10063 \f
10064 /***********************************************************************
10065 Tool-bars
10066 ***********************************************************************/
10067
10068 #ifdef HAVE_WINDOW_SYSTEM
10069
10070 /* Where the mouse was last time we reported a mouse event. */
10071
10072 FRAME_PTR last_mouse_frame;
10073
10074 /* Tool-bar item index of the item on which a mouse button was pressed
10075 or -1. */
10076
10077 int last_tool_bar_item;
10078
10079
10080 static Lisp_Object
10081 update_tool_bar_unwind (frame)
10082 Lisp_Object frame;
10083 {
10084 selected_frame = frame;
10085 return Qnil;
10086 }
10087
10088 /* Update the tool-bar item list for frame F. This has to be done
10089 before we start to fill in any display lines. Called from
10090 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10091 and restore it here. */
10092
10093 static void
10094 update_tool_bar (f, save_match_data)
10095 struct frame *f;
10096 int save_match_data;
10097 {
10098 #if defined (USE_GTK) || defined (HAVE_NS)
10099 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10100 #else
10101 int do_update = WINDOWP (f->tool_bar_window)
10102 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10103 #endif
10104
10105 if (do_update)
10106 {
10107 Lisp_Object window;
10108 struct window *w;
10109
10110 window = FRAME_SELECTED_WINDOW (f);
10111 w = XWINDOW (window);
10112
10113 /* If the user has switched buffers or windows, we need to
10114 recompute to reflect the new bindings. But we'll
10115 recompute when update_mode_lines is set too; that means
10116 that people can use force-mode-line-update to request
10117 that the menu bar be recomputed. The adverse effect on
10118 the rest of the redisplay algorithm is about the same as
10119 windows_or_buffers_changed anyway. */
10120 if (windows_or_buffers_changed
10121 || !NILP (w->update_mode_line)
10122 || update_mode_lines
10123 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10124 < BUF_MODIFF (XBUFFER (w->buffer)))
10125 != !NILP (w->last_had_star))
10126 || ((!NILP (Vtransient_mark_mode)
10127 && !NILP (XBUFFER (w->buffer)->mark_active))
10128 != !NILP (w->region_showing)))
10129 {
10130 struct buffer *prev = current_buffer;
10131 int count = SPECPDL_INDEX ();
10132 Lisp_Object frame, new_tool_bar;
10133 int new_n_tool_bar;
10134 struct gcpro gcpro1;
10135
10136 /* Set current_buffer to the buffer of the selected
10137 window of the frame, so that we get the right local
10138 keymaps. */
10139 set_buffer_internal_1 (XBUFFER (w->buffer));
10140
10141 /* Save match data, if we must. */
10142 if (save_match_data)
10143 record_unwind_save_match_data ();
10144
10145 /* Make sure that we don't accidentally use bogus keymaps. */
10146 if (NILP (Voverriding_local_map_menu_flag))
10147 {
10148 specbind (Qoverriding_terminal_local_map, Qnil);
10149 specbind (Qoverriding_local_map, Qnil);
10150 }
10151
10152 GCPRO1 (new_tool_bar);
10153
10154 /* We must temporarily set the selected frame to this frame
10155 before calling tool_bar_items, because the calculation of
10156 the tool-bar keymap uses the selected frame (see
10157 `tool-bar-make-keymap' in tool-bar.el). */
10158 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10159 XSETFRAME (frame, f);
10160 selected_frame = frame;
10161
10162 /* Build desired tool-bar items from keymaps. */
10163 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10164 &new_n_tool_bar);
10165
10166 /* Redisplay the tool-bar if we changed it. */
10167 if (new_n_tool_bar != f->n_tool_bar_items
10168 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10169 {
10170 /* Redisplay that happens asynchronously due to an expose event
10171 may access f->tool_bar_items. Make sure we update both
10172 variables within BLOCK_INPUT so no such event interrupts. */
10173 BLOCK_INPUT;
10174 f->tool_bar_items = new_tool_bar;
10175 f->n_tool_bar_items = new_n_tool_bar;
10176 w->update_mode_line = Qt;
10177 UNBLOCK_INPUT;
10178 }
10179
10180 UNGCPRO;
10181
10182 unbind_to (count, Qnil);
10183 set_buffer_internal_1 (prev);
10184 }
10185 }
10186 }
10187
10188
10189 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10190 F's desired tool-bar contents. F->tool_bar_items must have
10191 been set up previously by calling prepare_menu_bars. */
10192
10193 static void
10194 build_desired_tool_bar_string (f)
10195 struct frame *f;
10196 {
10197 int i, size, size_needed;
10198 struct gcpro gcpro1, gcpro2, gcpro3;
10199 Lisp_Object image, plist, props;
10200
10201 image = plist = props = Qnil;
10202 GCPRO3 (image, plist, props);
10203
10204 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10205 Otherwise, make a new string. */
10206
10207 /* The size of the string we might be able to reuse. */
10208 size = (STRINGP (f->desired_tool_bar_string)
10209 ? SCHARS (f->desired_tool_bar_string)
10210 : 0);
10211
10212 /* We need one space in the string for each image. */
10213 size_needed = f->n_tool_bar_items;
10214
10215 /* Reuse f->desired_tool_bar_string, if possible. */
10216 if (size < size_needed || NILP (f->desired_tool_bar_string))
10217 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10218 make_number (' '));
10219 else
10220 {
10221 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10222 Fremove_text_properties (make_number (0), make_number (size),
10223 props, f->desired_tool_bar_string);
10224 }
10225
10226 /* Put a `display' property on the string for the images to display,
10227 put a `menu_item' property on tool-bar items with a value that
10228 is the index of the item in F's tool-bar item vector. */
10229 for (i = 0; i < f->n_tool_bar_items; ++i)
10230 {
10231 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10232
10233 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10234 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10235 int hmargin, vmargin, relief, idx, end;
10236 extern Lisp_Object QCrelief, QCmargin, QCconversion;
10237
10238 /* If image is a vector, choose the image according to the
10239 button state. */
10240 image = PROP (TOOL_BAR_ITEM_IMAGES);
10241 if (VECTORP (image))
10242 {
10243 if (enabled_p)
10244 idx = (selected_p
10245 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10246 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10247 else
10248 idx = (selected_p
10249 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10250 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10251
10252 xassert (ASIZE (image) >= idx);
10253 image = AREF (image, idx);
10254 }
10255 else
10256 idx = -1;
10257
10258 /* Ignore invalid image specifications. */
10259 if (!valid_image_p (image))
10260 continue;
10261
10262 /* Display the tool-bar button pressed, or depressed. */
10263 plist = Fcopy_sequence (XCDR (image));
10264
10265 /* Compute margin and relief to draw. */
10266 relief = (tool_bar_button_relief >= 0
10267 ? tool_bar_button_relief
10268 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10269 hmargin = vmargin = relief;
10270
10271 if (INTEGERP (Vtool_bar_button_margin)
10272 && XINT (Vtool_bar_button_margin) > 0)
10273 {
10274 hmargin += XFASTINT (Vtool_bar_button_margin);
10275 vmargin += XFASTINT (Vtool_bar_button_margin);
10276 }
10277 else if (CONSP (Vtool_bar_button_margin))
10278 {
10279 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10280 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10281 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10282
10283 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10284 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10285 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10286 }
10287
10288 if (auto_raise_tool_bar_buttons_p)
10289 {
10290 /* Add a `:relief' property to the image spec if the item is
10291 selected. */
10292 if (selected_p)
10293 {
10294 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10295 hmargin -= relief;
10296 vmargin -= relief;
10297 }
10298 }
10299 else
10300 {
10301 /* If image is selected, display it pressed, i.e. with a
10302 negative relief. If it's not selected, display it with a
10303 raised relief. */
10304 plist = Fplist_put (plist, QCrelief,
10305 (selected_p
10306 ? make_number (-relief)
10307 : make_number (relief)));
10308 hmargin -= relief;
10309 vmargin -= relief;
10310 }
10311
10312 /* Put a margin around the image. */
10313 if (hmargin || vmargin)
10314 {
10315 if (hmargin == vmargin)
10316 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10317 else
10318 plist = Fplist_put (plist, QCmargin,
10319 Fcons (make_number (hmargin),
10320 make_number (vmargin)));
10321 }
10322
10323 /* If button is not enabled, and we don't have special images
10324 for the disabled state, make the image appear disabled by
10325 applying an appropriate algorithm to it. */
10326 if (!enabled_p && idx < 0)
10327 plist = Fplist_put (plist, QCconversion, Qdisabled);
10328
10329 /* Put a `display' text property on the string for the image to
10330 display. Put a `menu-item' property on the string that gives
10331 the start of this item's properties in the tool-bar items
10332 vector. */
10333 image = Fcons (Qimage, plist);
10334 props = list4 (Qdisplay, image,
10335 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10336
10337 /* Let the last image hide all remaining spaces in the tool bar
10338 string. The string can be longer than needed when we reuse a
10339 previous string. */
10340 if (i + 1 == f->n_tool_bar_items)
10341 end = SCHARS (f->desired_tool_bar_string);
10342 else
10343 end = i + 1;
10344 Fadd_text_properties (make_number (i), make_number (end),
10345 props, f->desired_tool_bar_string);
10346 #undef PROP
10347 }
10348
10349 UNGCPRO;
10350 }
10351
10352
10353 /* Display one line of the tool-bar of frame IT->f.
10354
10355 HEIGHT specifies the desired height of the tool-bar line.
10356 If the actual height of the glyph row is less than HEIGHT, the
10357 row's height is increased to HEIGHT, and the icons are centered
10358 vertically in the new height.
10359
10360 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10361 count a final empty row in case the tool-bar width exactly matches
10362 the window width.
10363 */
10364
10365 static void
10366 display_tool_bar_line (it, height)
10367 struct it *it;
10368 int height;
10369 {
10370 struct glyph_row *row = it->glyph_row;
10371 int max_x = it->last_visible_x;
10372 struct glyph *last;
10373
10374 prepare_desired_row (row);
10375 row->y = it->current_y;
10376
10377 /* Note that this isn't made use of if the face hasn't a box,
10378 so there's no need to check the face here. */
10379 it->start_of_box_run_p = 1;
10380
10381 while (it->current_x < max_x)
10382 {
10383 int x, n_glyphs_before, i, nglyphs;
10384 struct it it_before;
10385
10386 /* Get the next display element. */
10387 if (!get_next_display_element (it))
10388 {
10389 /* Don't count empty row if we are counting needed tool-bar lines. */
10390 if (height < 0 && !it->hpos)
10391 return;
10392 break;
10393 }
10394
10395 /* Produce glyphs. */
10396 n_glyphs_before = row->used[TEXT_AREA];
10397 it_before = *it;
10398
10399 PRODUCE_GLYPHS (it);
10400
10401 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10402 i = 0;
10403 x = it_before.current_x;
10404 while (i < nglyphs)
10405 {
10406 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10407
10408 if (x + glyph->pixel_width > max_x)
10409 {
10410 /* Glyph doesn't fit on line. Backtrack. */
10411 row->used[TEXT_AREA] = n_glyphs_before;
10412 *it = it_before;
10413 /* If this is the only glyph on this line, it will never fit on the
10414 toolbar, so skip it. But ensure there is at least one glyph,
10415 so we don't accidentally disable the tool-bar. */
10416 if (n_glyphs_before == 0
10417 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10418 break;
10419 goto out;
10420 }
10421
10422 ++it->hpos;
10423 x += glyph->pixel_width;
10424 ++i;
10425 }
10426
10427 /* Stop at line ends. */
10428 if (ITERATOR_AT_END_OF_LINE_P (it))
10429 break;
10430
10431 set_iterator_to_next (it, 1);
10432 }
10433
10434 out:;
10435
10436 row->displays_text_p = row->used[TEXT_AREA] != 0;
10437
10438 /* Use default face for the border below the tool bar.
10439
10440 FIXME: When auto-resize-tool-bars is grow-only, there is
10441 no additional border below the possibly empty tool-bar lines.
10442 So to make the extra empty lines look "normal", we have to
10443 use the tool-bar face for the border too. */
10444 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10445 it->face_id = DEFAULT_FACE_ID;
10446
10447 extend_face_to_end_of_line (it);
10448 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10449 last->right_box_line_p = 1;
10450 if (last == row->glyphs[TEXT_AREA])
10451 last->left_box_line_p = 1;
10452
10453 /* Make line the desired height and center it vertically. */
10454 if ((height -= it->max_ascent + it->max_descent) > 0)
10455 {
10456 /* Don't add more than one line height. */
10457 height %= FRAME_LINE_HEIGHT (it->f);
10458 it->max_ascent += height / 2;
10459 it->max_descent += (height + 1) / 2;
10460 }
10461
10462 compute_line_metrics (it);
10463
10464 /* If line is empty, make it occupy the rest of the tool-bar. */
10465 if (!row->displays_text_p)
10466 {
10467 row->height = row->phys_height = it->last_visible_y - row->y;
10468 row->visible_height = row->height;
10469 row->ascent = row->phys_ascent = 0;
10470 row->extra_line_spacing = 0;
10471 }
10472
10473 row->full_width_p = 1;
10474 row->continued_p = 0;
10475 row->truncated_on_left_p = 0;
10476 row->truncated_on_right_p = 0;
10477
10478 it->current_x = it->hpos = 0;
10479 it->current_y += row->height;
10480 ++it->vpos;
10481 ++it->glyph_row;
10482 }
10483
10484
10485 /* Max tool-bar height. */
10486
10487 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10488 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10489
10490 /* Value is the number of screen lines needed to make all tool-bar
10491 items of frame F visible. The number of actual rows needed is
10492 returned in *N_ROWS if non-NULL. */
10493
10494 static int
10495 tool_bar_lines_needed (f, n_rows)
10496 struct frame *f;
10497 int *n_rows;
10498 {
10499 struct window *w = XWINDOW (f->tool_bar_window);
10500 struct it it;
10501 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10502 the desired matrix, so use (unused) mode-line row as temporary row to
10503 avoid destroying the first tool-bar row. */
10504 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10505
10506 /* Initialize an iterator for iteration over
10507 F->desired_tool_bar_string in the tool-bar window of frame F. */
10508 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10509 it.first_visible_x = 0;
10510 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10511 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10512
10513 while (!ITERATOR_AT_END_P (&it))
10514 {
10515 clear_glyph_row (temp_row);
10516 it.glyph_row = temp_row;
10517 display_tool_bar_line (&it, -1);
10518 }
10519 clear_glyph_row (temp_row);
10520
10521 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10522 if (n_rows)
10523 *n_rows = it.vpos > 0 ? it.vpos : -1;
10524
10525 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10526 }
10527
10528
10529 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10530 0, 1, 0,
10531 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10532 (frame)
10533 Lisp_Object frame;
10534 {
10535 struct frame *f;
10536 struct window *w;
10537 int nlines = 0;
10538
10539 if (NILP (frame))
10540 frame = selected_frame;
10541 else
10542 CHECK_FRAME (frame);
10543 f = XFRAME (frame);
10544
10545 if (WINDOWP (f->tool_bar_window)
10546 || (w = XWINDOW (f->tool_bar_window),
10547 WINDOW_TOTAL_LINES (w) > 0))
10548 {
10549 update_tool_bar (f, 1);
10550 if (f->n_tool_bar_items)
10551 {
10552 build_desired_tool_bar_string (f);
10553 nlines = tool_bar_lines_needed (f, NULL);
10554 }
10555 }
10556
10557 return make_number (nlines);
10558 }
10559
10560
10561 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10562 height should be changed. */
10563
10564 static int
10565 redisplay_tool_bar (f)
10566 struct frame *f;
10567 {
10568 struct window *w;
10569 struct it it;
10570 struct glyph_row *row;
10571
10572 #if defined (USE_GTK) || defined (HAVE_NS)
10573 if (FRAME_EXTERNAL_TOOL_BAR (f))
10574 update_frame_tool_bar (f);
10575 return 0;
10576 #endif
10577
10578 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10579 do anything. This means you must start with tool-bar-lines
10580 non-zero to get the auto-sizing effect. Or in other words, you
10581 can turn off tool-bars by specifying tool-bar-lines zero. */
10582 if (!WINDOWP (f->tool_bar_window)
10583 || (w = XWINDOW (f->tool_bar_window),
10584 WINDOW_TOTAL_LINES (w) == 0))
10585 return 0;
10586
10587 /* Set up an iterator for the tool-bar window. */
10588 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10589 it.first_visible_x = 0;
10590 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10591 row = it.glyph_row;
10592
10593 /* Build a string that represents the contents of the tool-bar. */
10594 build_desired_tool_bar_string (f);
10595 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10596
10597 if (f->n_tool_bar_rows == 0)
10598 {
10599 int nlines;
10600
10601 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10602 nlines != WINDOW_TOTAL_LINES (w)))
10603 {
10604 extern Lisp_Object Qtool_bar_lines;
10605 Lisp_Object frame;
10606 int old_height = WINDOW_TOTAL_LINES (w);
10607
10608 XSETFRAME (frame, f);
10609 Fmodify_frame_parameters (frame,
10610 Fcons (Fcons (Qtool_bar_lines,
10611 make_number (nlines)),
10612 Qnil));
10613 if (WINDOW_TOTAL_LINES (w) != old_height)
10614 {
10615 clear_glyph_matrix (w->desired_matrix);
10616 fonts_changed_p = 1;
10617 return 1;
10618 }
10619 }
10620 }
10621
10622 /* Display as many lines as needed to display all tool-bar items. */
10623
10624 if (f->n_tool_bar_rows > 0)
10625 {
10626 int border, rows, height, extra;
10627
10628 if (INTEGERP (Vtool_bar_border))
10629 border = XINT (Vtool_bar_border);
10630 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10631 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10632 else if (EQ (Vtool_bar_border, Qborder_width))
10633 border = f->border_width;
10634 else
10635 border = 0;
10636 if (border < 0)
10637 border = 0;
10638
10639 rows = f->n_tool_bar_rows;
10640 height = max (1, (it.last_visible_y - border) / rows);
10641 extra = it.last_visible_y - border - height * rows;
10642
10643 while (it.current_y < it.last_visible_y)
10644 {
10645 int h = 0;
10646 if (extra > 0 && rows-- > 0)
10647 {
10648 h = (extra + rows - 1) / rows;
10649 extra -= h;
10650 }
10651 display_tool_bar_line (&it, height + h);
10652 }
10653 }
10654 else
10655 {
10656 while (it.current_y < it.last_visible_y)
10657 display_tool_bar_line (&it, 0);
10658 }
10659
10660 /* It doesn't make much sense to try scrolling in the tool-bar
10661 window, so don't do it. */
10662 w->desired_matrix->no_scrolling_p = 1;
10663 w->must_be_updated_p = 1;
10664
10665 if (!NILP (Vauto_resize_tool_bars))
10666 {
10667 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10668 int change_height_p = 0;
10669
10670 /* If we couldn't display everything, change the tool-bar's
10671 height if there is room for more. */
10672 if (IT_STRING_CHARPOS (it) < it.end_charpos
10673 && it.current_y < max_tool_bar_height)
10674 change_height_p = 1;
10675
10676 row = it.glyph_row - 1;
10677
10678 /* If there are blank lines at the end, except for a partially
10679 visible blank line at the end that is smaller than
10680 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10681 if (!row->displays_text_p
10682 && row->height >= FRAME_LINE_HEIGHT (f))
10683 change_height_p = 1;
10684
10685 /* If row displays tool-bar items, but is partially visible,
10686 change the tool-bar's height. */
10687 if (row->displays_text_p
10688 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10689 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10690 change_height_p = 1;
10691
10692 /* Resize windows as needed by changing the `tool-bar-lines'
10693 frame parameter. */
10694 if (change_height_p)
10695 {
10696 extern Lisp_Object Qtool_bar_lines;
10697 Lisp_Object frame;
10698 int old_height = WINDOW_TOTAL_LINES (w);
10699 int nrows;
10700 int nlines = tool_bar_lines_needed (f, &nrows);
10701
10702 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10703 && !f->minimize_tool_bar_window_p)
10704 ? (nlines > old_height)
10705 : (nlines != old_height));
10706 f->minimize_tool_bar_window_p = 0;
10707
10708 if (change_height_p)
10709 {
10710 XSETFRAME (frame, f);
10711 Fmodify_frame_parameters (frame,
10712 Fcons (Fcons (Qtool_bar_lines,
10713 make_number (nlines)),
10714 Qnil));
10715 if (WINDOW_TOTAL_LINES (w) != old_height)
10716 {
10717 clear_glyph_matrix (w->desired_matrix);
10718 f->n_tool_bar_rows = nrows;
10719 fonts_changed_p = 1;
10720 return 1;
10721 }
10722 }
10723 }
10724 }
10725
10726 f->minimize_tool_bar_window_p = 0;
10727 return 0;
10728 }
10729
10730
10731 /* Get information about the tool-bar item which is displayed in GLYPH
10732 on frame F. Return in *PROP_IDX the index where tool-bar item
10733 properties start in F->tool_bar_items. Value is zero if
10734 GLYPH doesn't display a tool-bar item. */
10735
10736 static int
10737 tool_bar_item_info (f, glyph, prop_idx)
10738 struct frame *f;
10739 struct glyph *glyph;
10740 int *prop_idx;
10741 {
10742 Lisp_Object prop;
10743 int success_p;
10744 int charpos;
10745
10746 /* This function can be called asynchronously, which means we must
10747 exclude any possibility that Fget_text_property signals an
10748 error. */
10749 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10750 charpos = max (0, charpos);
10751
10752 /* Get the text property `menu-item' at pos. The value of that
10753 property is the start index of this item's properties in
10754 F->tool_bar_items. */
10755 prop = Fget_text_property (make_number (charpos),
10756 Qmenu_item, f->current_tool_bar_string);
10757 if (INTEGERP (prop))
10758 {
10759 *prop_idx = XINT (prop);
10760 success_p = 1;
10761 }
10762 else
10763 success_p = 0;
10764
10765 return success_p;
10766 }
10767
10768 \f
10769 /* Get information about the tool-bar item at position X/Y on frame F.
10770 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10771 the current matrix of the tool-bar window of F, or NULL if not
10772 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10773 item in F->tool_bar_items. Value is
10774
10775 -1 if X/Y is not on a tool-bar item
10776 0 if X/Y is on the same item that was highlighted before.
10777 1 otherwise. */
10778
10779 static int
10780 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10781 struct frame *f;
10782 int x, y;
10783 struct glyph **glyph;
10784 int *hpos, *vpos, *prop_idx;
10785 {
10786 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10787 struct window *w = XWINDOW (f->tool_bar_window);
10788 int area;
10789
10790 /* Find the glyph under X/Y. */
10791 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10792 if (*glyph == NULL)
10793 return -1;
10794
10795 /* Get the start of this tool-bar item's properties in
10796 f->tool_bar_items. */
10797 if (!tool_bar_item_info (f, *glyph, prop_idx))
10798 return -1;
10799
10800 /* Is mouse on the highlighted item? */
10801 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10802 && *vpos >= dpyinfo->mouse_face_beg_row
10803 && *vpos <= dpyinfo->mouse_face_end_row
10804 && (*vpos > dpyinfo->mouse_face_beg_row
10805 || *hpos >= dpyinfo->mouse_face_beg_col)
10806 && (*vpos < dpyinfo->mouse_face_end_row
10807 || *hpos < dpyinfo->mouse_face_end_col
10808 || dpyinfo->mouse_face_past_end))
10809 return 0;
10810
10811 return 1;
10812 }
10813
10814
10815 /* EXPORT:
10816 Handle mouse button event on the tool-bar of frame F, at
10817 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10818 0 for button release. MODIFIERS is event modifiers for button
10819 release. */
10820
10821 void
10822 handle_tool_bar_click (f, x, y, down_p, modifiers)
10823 struct frame *f;
10824 int x, y, down_p;
10825 unsigned int modifiers;
10826 {
10827 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10828 struct window *w = XWINDOW (f->tool_bar_window);
10829 int hpos, vpos, prop_idx;
10830 struct glyph *glyph;
10831 Lisp_Object enabled_p;
10832
10833 /* If not on the highlighted tool-bar item, return. */
10834 frame_to_window_pixel_xy (w, &x, &y);
10835 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10836 return;
10837
10838 /* If item is disabled, do nothing. */
10839 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10840 if (NILP (enabled_p))
10841 return;
10842
10843 if (down_p)
10844 {
10845 /* Show item in pressed state. */
10846 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10847 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10848 last_tool_bar_item = prop_idx;
10849 }
10850 else
10851 {
10852 Lisp_Object key, frame;
10853 struct input_event event;
10854 EVENT_INIT (event);
10855
10856 /* Show item in released state. */
10857 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10858 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10859
10860 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10861
10862 XSETFRAME (frame, f);
10863 event.kind = TOOL_BAR_EVENT;
10864 event.frame_or_window = frame;
10865 event.arg = frame;
10866 kbd_buffer_store_event (&event);
10867
10868 event.kind = TOOL_BAR_EVENT;
10869 event.frame_or_window = frame;
10870 event.arg = key;
10871 event.modifiers = modifiers;
10872 kbd_buffer_store_event (&event);
10873 last_tool_bar_item = -1;
10874 }
10875 }
10876
10877
10878 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10879 tool-bar window-relative coordinates X/Y. Called from
10880 note_mouse_highlight. */
10881
10882 static void
10883 note_tool_bar_highlight (f, x, y)
10884 struct frame *f;
10885 int x, y;
10886 {
10887 Lisp_Object window = f->tool_bar_window;
10888 struct window *w = XWINDOW (window);
10889 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10890 int hpos, vpos;
10891 struct glyph *glyph;
10892 struct glyph_row *row;
10893 int i;
10894 Lisp_Object enabled_p;
10895 int prop_idx;
10896 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10897 int mouse_down_p, rc;
10898
10899 /* Function note_mouse_highlight is called with negative x(y
10900 values when mouse moves outside of the frame. */
10901 if (x <= 0 || y <= 0)
10902 {
10903 clear_mouse_face (dpyinfo);
10904 return;
10905 }
10906
10907 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10908 if (rc < 0)
10909 {
10910 /* Not on tool-bar item. */
10911 clear_mouse_face (dpyinfo);
10912 return;
10913 }
10914 else if (rc == 0)
10915 /* On same tool-bar item as before. */
10916 goto set_help_echo;
10917
10918 clear_mouse_face (dpyinfo);
10919
10920 /* Mouse is down, but on different tool-bar item? */
10921 mouse_down_p = (dpyinfo->grabbed
10922 && f == last_mouse_frame
10923 && FRAME_LIVE_P (f));
10924 if (mouse_down_p
10925 && last_tool_bar_item != prop_idx)
10926 return;
10927
10928 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10929 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10930
10931 /* If tool-bar item is not enabled, don't highlight it. */
10932 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10933 if (!NILP (enabled_p))
10934 {
10935 /* Compute the x-position of the glyph. In front and past the
10936 image is a space. We include this in the highlighted area. */
10937 row = MATRIX_ROW (w->current_matrix, vpos);
10938 for (i = x = 0; i < hpos; ++i)
10939 x += row->glyphs[TEXT_AREA][i].pixel_width;
10940
10941 /* Record this as the current active region. */
10942 dpyinfo->mouse_face_beg_col = hpos;
10943 dpyinfo->mouse_face_beg_row = vpos;
10944 dpyinfo->mouse_face_beg_x = x;
10945 dpyinfo->mouse_face_beg_y = row->y;
10946 dpyinfo->mouse_face_past_end = 0;
10947
10948 dpyinfo->mouse_face_end_col = hpos + 1;
10949 dpyinfo->mouse_face_end_row = vpos;
10950 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10951 dpyinfo->mouse_face_end_y = row->y;
10952 dpyinfo->mouse_face_window = window;
10953 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10954
10955 /* Display it as active. */
10956 show_mouse_face (dpyinfo, draw);
10957 dpyinfo->mouse_face_image_state = draw;
10958 }
10959
10960 set_help_echo:
10961
10962 /* Set help_echo_string to a help string to display for this tool-bar item.
10963 XTread_socket does the rest. */
10964 help_echo_object = help_echo_window = Qnil;
10965 help_echo_pos = -1;
10966 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10967 if (NILP (help_echo_string))
10968 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10969 }
10970
10971 #endif /* HAVE_WINDOW_SYSTEM */
10972
10973
10974 \f
10975 /************************************************************************
10976 Horizontal scrolling
10977 ************************************************************************/
10978
10979 static int hscroll_window_tree P_ ((Lisp_Object));
10980 static int hscroll_windows P_ ((Lisp_Object));
10981
10982 /* For all leaf windows in the window tree rooted at WINDOW, set their
10983 hscroll value so that PT is (i) visible in the window, and (ii) so
10984 that it is not within a certain margin at the window's left and
10985 right border. Value is non-zero if any window's hscroll has been
10986 changed. */
10987
10988 static int
10989 hscroll_window_tree (window)
10990 Lisp_Object window;
10991 {
10992 int hscrolled_p = 0;
10993 int hscroll_relative_p = FLOATP (Vhscroll_step);
10994 int hscroll_step_abs = 0;
10995 double hscroll_step_rel = 0;
10996
10997 if (hscroll_relative_p)
10998 {
10999 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11000 if (hscroll_step_rel < 0)
11001 {
11002 hscroll_relative_p = 0;
11003 hscroll_step_abs = 0;
11004 }
11005 }
11006 else if (INTEGERP (Vhscroll_step))
11007 {
11008 hscroll_step_abs = XINT (Vhscroll_step);
11009 if (hscroll_step_abs < 0)
11010 hscroll_step_abs = 0;
11011 }
11012 else
11013 hscroll_step_abs = 0;
11014
11015 while (WINDOWP (window))
11016 {
11017 struct window *w = XWINDOW (window);
11018
11019 if (WINDOWP (w->hchild))
11020 hscrolled_p |= hscroll_window_tree (w->hchild);
11021 else if (WINDOWP (w->vchild))
11022 hscrolled_p |= hscroll_window_tree (w->vchild);
11023 else if (w->cursor.vpos >= 0)
11024 {
11025 int h_margin;
11026 int text_area_width;
11027 struct glyph_row *current_cursor_row
11028 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11029 struct glyph_row *desired_cursor_row
11030 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11031 struct glyph_row *cursor_row
11032 = (desired_cursor_row->enabled_p
11033 ? desired_cursor_row
11034 : current_cursor_row);
11035
11036 text_area_width = window_box_width (w, TEXT_AREA);
11037
11038 /* Scroll when cursor is inside this scroll margin. */
11039 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11040
11041 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11042 && ((XFASTINT (w->hscroll)
11043 && w->cursor.x <= h_margin)
11044 || (cursor_row->enabled_p
11045 && cursor_row->truncated_on_right_p
11046 && (w->cursor.x >= text_area_width - h_margin))))
11047 {
11048 struct it it;
11049 int hscroll;
11050 struct buffer *saved_current_buffer;
11051 int pt;
11052 int wanted_x;
11053
11054 /* Find point in a display of infinite width. */
11055 saved_current_buffer = current_buffer;
11056 current_buffer = XBUFFER (w->buffer);
11057
11058 if (w == XWINDOW (selected_window))
11059 pt = BUF_PT (current_buffer);
11060 else
11061 {
11062 pt = marker_position (w->pointm);
11063 pt = max (BEGV, pt);
11064 pt = min (ZV, pt);
11065 }
11066
11067 /* Move iterator to pt starting at cursor_row->start in
11068 a line with infinite width. */
11069 init_to_row_start (&it, w, cursor_row);
11070 it.last_visible_x = INFINITY;
11071 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11072 current_buffer = saved_current_buffer;
11073
11074 /* Position cursor in window. */
11075 if (!hscroll_relative_p && hscroll_step_abs == 0)
11076 hscroll = max (0, (it.current_x
11077 - (ITERATOR_AT_END_OF_LINE_P (&it)
11078 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11079 : (text_area_width / 2))))
11080 / FRAME_COLUMN_WIDTH (it.f);
11081 else if (w->cursor.x >= text_area_width - h_margin)
11082 {
11083 if (hscroll_relative_p)
11084 wanted_x = text_area_width * (1 - hscroll_step_rel)
11085 - h_margin;
11086 else
11087 wanted_x = text_area_width
11088 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11089 - h_margin;
11090 hscroll
11091 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11092 }
11093 else
11094 {
11095 if (hscroll_relative_p)
11096 wanted_x = text_area_width * hscroll_step_rel
11097 + h_margin;
11098 else
11099 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11100 + h_margin;
11101 hscroll
11102 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11103 }
11104 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11105
11106 /* Don't call Fset_window_hscroll if value hasn't
11107 changed because it will prevent redisplay
11108 optimizations. */
11109 if (XFASTINT (w->hscroll) != hscroll)
11110 {
11111 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11112 w->hscroll = make_number (hscroll);
11113 hscrolled_p = 1;
11114 }
11115 }
11116 }
11117
11118 window = w->next;
11119 }
11120
11121 /* Value is non-zero if hscroll of any leaf window has been changed. */
11122 return hscrolled_p;
11123 }
11124
11125
11126 /* Set hscroll so that cursor is visible and not inside horizontal
11127 scroll margins for all windows in the tree rooted at WINDOW. See
11128 also hscroll_window_tree above. Value is non-zero if any window's
11129 hscroll has been changed. If it has, desired matrices on the frame
11130 of WINDOW are cleared. */
11131
11132 static int
11133 hscroll_windows (window)
11134 Lisp_Object window;
11135 {
11136 int hscrolled_p = hscroll_window_tree (window);
11137 if (hscrolled_p)
11138 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11139 return hscrolled_p;
11140 }
11141
11142
11143 \f
11144 /************************************************************************
11145 Redisplay
11146 ************************************************************************/
11147
11148 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11149 to a non-zero value. This is sometimes handy to have in a debugger
11150 session. */
11151
11152 #if GLYPH_DEBUG
11153
11154 /* First and last unchanged row for try_window_id. */
11155
11156 int debug_first_unchanged_at_end_vpos;
11157 int debug_last_unchanged_at_beg_vpos;
11158
11159 /* Delta vpos and y. */
11160
11161 int debug_dvpos, debug_dy;
11162
11163 /* Delta in characters and bytes for try_window_id. */
11164
11165 int debug_delta, debug_delta_bytes;
11166
11167 /* Values of window_end_pos and window_end_vpos at the end of
11168 try_window_id. */
11169
11170 EMACS_INT debug_end_pos, debug_end_vpos;
11171
11172 /* Append a string to W->desired_matrix->method. FMT is a printf
11173 format string. A1...A9 are a supplement for a variable-length
11174 argument list. If trace_redisplay_p is non-zero also printf the
11175 resulting string to stderr. */
11176
11177 static void
11178 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11179 struct window *w;
11180 char *fmt;
11181 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11182 {
11183 char buffer[512];
11184 char *method = w->desired_matrix->method;
11185 int len = strlen (method);
11186 int size = sizeof w->desired_matrix->method;
11187 int remaining = size - len - 1;
11188
11189 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11190 if (len && remaining)
11191 {
11192 method[len] = '|';
11193 --remaining, ++len;
11194 }
11195
11196 strncpy (method + len, buffer, remaining);
11197
11198 if (trace_redisplay_p)
11199 fprintf (stderr, "%p (%s): %s\n",
11200 w,
11201 ((BUFFERP (w->buffer)
11202 && STRINGP (XBUFFER (w->buffer)->name))
11203 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11204 : "no buffer"),
11205 buffer);
11206 }
11207
11208 #endif /* GLYPH_DEBUG */
11209
11210
11211 /* Value is non-zero if all changes in window W, which displays
11212 current_buffer, are in the text between START and END. START is a
11213 buffer position, END is given as a distance from Z. Used in
11214 redisplay_internal for display optimization. */
11215
11216 static INLINE int
11217 text_outside_line_unchanged_p (w, start, end)
11218 struct window *w;
11219 int start, end;
11220 {
11221 int unchanged_p = 1;
11222
11223 /* If text or overlays have changed, see where. */
11224 if (XFASTINT (w->last_modified) < MODIFF
11225 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11226 {
11227 /* Gap in the line? */
11228 if (GPT < start || Z - GPT < end)
11229 unchanged_p = 0;
11230
11231 /* Changes start in front of the line, or end after it? */
11232 if (unchanged_p
11233 && (BEG_UNCHANGED < start - 1
11234 || END_UNCHANGED < end))
11235 unchanged_p = 0;
11236
11237 /* If selective display, can't optimize if changes start at the
11238 beginning of the line. */
11239 if (unchanged_p
11240 && INTEGERP (current_buffer->selective_display)
11241 && XINT (current_buffer->selective_display) > 0
11242 && (BEG_UNCHANGED < start || GPT <= start))
11243 unchanged_p = 0;
11244
11245 /* If there are overlays at the start or end of the line, these
11246 may have overlay strings with newlines in them. A change at
11247 START, for instance, may actually concern the display of such
11248 overlay strings as well, and they are displayed on different
11249 lines. So, quickly rule out this case. (For the future, it
11250 might be desirable to implement something more telling than
11251 just BEG/END_UNCHANGED.) */
11252 if (unchanged_p)
11253 {
11254 if (BEG + BEG_UNCHANGED == start
11255 && overlay_touches_p (start))
11256 unchanged_p = 0;
11257 if (END_UNCHANGED == end
11258 && overlay_touches_p (Z - end))
11259 unchanged_p = 0;
11260 }
11261
11262 /* Under bidi reordering, adding or deleting a character in the
11263 beginning of a paragraph, before the first strong directional
11264 character, can change the base direction of the paragraph (unless
11265 the buffer specifies a fixed paragraph direction), which will
11266 require to redisplay the whole paragraph. It might be worthwhile
11267 to find the paragraph limits and widen the range of redisplayed
11268 lines to that, but for now just give up this optimization. */
11269 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11270 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11271 unchanged_p = 0;
11272 }
11273
11274 return unchanged_p;
11275 }
11276
11277
11278 /* Do a frame update, taking possible shortcuts into account. This is
11279 the main external entry point for redisplay.
11280
11281 If the last redisplay displayed an echo area message and that message
11282 is no longer requested, we clear the echo area or bring back the
11283 mini-buffer if that is in use. */
11284
11285 void
11286 redisplay ()
11287 {
11288 redisplay_internal (0);
11289 }
11290
11291
11292 static Lisp_Object
11293 overlay_arrow_string_or_property (var)
11294 Lisp_Object var;
11295 {
11296 Lisp_Object val;
11297
11298 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11299 return val;
11300
11301 return Voverlay_arrow_string;
11302 }
11303
11304 /* Return 1 if there are any overlay-arrows in current_buffer. */
11305 static int
11306 overlay_arrow_in_current_buffer_p ()
11307 {
11308 Lisp_Object vlist;
11309
11310 for (vlist = Voverlay_arrow_variable_list;
11311 CONSP (vlist);
11312 vlist = XCDR (vlist))
11313 {
11314 Lisp_Object var = XCAR (vlist);
11315 Lisp_Object val;
11316
11317 if (!SYMBOLP (var))
11318 continue;
11319 val = find_symbol_value (var);
11320 if (MARKERP (val)
11321 && current_buffer == XMARKER (val)->buffer)
11322 return 1;
11323 }
11324 return 0;
11325 }
11326
11327
11328 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11329 has changed. */
11330
11331 static int
11332 overlay_arrows_changed_p ()
11333 {
11334 Lisp_Object vlist;
11335
11336 for (vlist = Voverlay_arrow_variable_list;
11337 CONSP (vlist);
11338 vlist = XCDR (vlist))
11339 {
11340 Lisp_Object var = XCAR (vlist);
11341 Lisp_Object val, pstr;
11342
11343 if (!SYMBOLP (var))
11344 continue;
11345 val = find_symbol_value (var);
11346 if (!MARKERP (val))
11347 continue;
11348 if (! EQ (COERCE_MARKER (val),
11349 Fget (var, Qlast_arrow_position))
11350 || ! (pstr = overlay_arrow_string_or_property (var),
11351 EQ (pstr, Fget (var, Qlast_arrow_string))))
11352 return 1;
11353 }
11354 return 0;
11355 }
11356
11357 /* Mark overlay arrows to be updated on next redisplay. */
11358
11359 static void
11360 update_overlay_arrows (up_to_date)
11361 int up_to_date;
11362 {
11363 Lisp_Object vlist;
11364
11365 for (vlist = Voverlay_arrow_variable_list;
11366 CONSP (vlist);
11367 vlist = XCDR (vlist))
11368 {
11369 Lisp_Object var = XCAR (vlist);
11370
11371 if (!SYMBOLP (var))
11372 continue;
11373
11374 if (up_to_date > 0)
11375 {
11376 Lisp_Object val = find_symbol_value (var);
11377 Fput (var, Qlast_arrow_position,
11378 COERCE_MARKER (val));
11379 Fput (var, Qlast_arrow_string,
11380 overlay_arrow_string_or_property (var));
11381 }
11382 else if (up_to_date < 0
11383 || !NILP (Fget (var, Qlast_arrow_position)))
11384 {
11385 Fput (var, Qlast_arrow_position, Qt);
11386 Fput (var, Qlast_arrow_string, Qt);
11387 }
11388 }
11389 }
11390
11391
11392 /* Return overlay arrow string to display at row.
11393 Return integer (bitmap number) for arrow bitmap in left fringe.
11394 Return nil if no overlay arrow. */
11395
11396 static Lisp_Object
11397 overlay_arrow_at_row (it, row)
11398 struct it *it;
11399 struct glyph_row *row;
11400 {
11401 Lisp_Object vlist;
11402
11403 for (vlist = Voverlay_arrow_variable_list;
11404 CONSP (vlist);
11405 vlist = XCDR (vlist))
11406 {
11407 Lisp_Object var = XCAR (vlist);
11408 Lisp_Object val;
11409
11410 if (!SYMBOLP (var))
11411 continue;
11412
11413 val = find_symbol_value (var);
11414
11415 if (MARKERP (val)
11416 && current_buffer == XMARKER (val)->buffer
11417 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11418 {
11419 if (FRAME_WINDOW_P (it->f)
11420 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11421 {
11422 #ifdef HAVE_WINDOW_SYSTEM
11423 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11424 {
11425 int fringe_bitmap;
11426 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11427 return make_number (fringe_bitmap);
11428 }
11429 #endif
11430 return make_number (-1); /* Use default arrow bitmap */
11431 }
11432 return overlay_arrow_string_or_property (var);
11433 }
11434 }
11435
11436 return Qnil;
11437 }
11438
11439 /* Return 1 if point moved out of or into a composition. Otherwise
11440 return 0. PREV_BUF and PREV_PT are the last point buffer and
11441 position. BUF and PT are the current point buffer and position. */
11442
11443 int
11444 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11445 struct buffer *prev_buf, *buf;
11446 int prev_pt, pt;
11447 {
11448 EMACS_INT start, end;
11449 Lisp_Object prop;
11450 Lisp_Object buffer;
11451
11452 XSETBUFFER (buffer, buf);
11453 /* Check a composition at the last point if point moved within the
11454 same buffer. */
11455 if (prev_buf == buf)
11456 {
11457 if (prev_pt == pt)
11458 /* Point didn't move. */
11459 return 0;
11460
11461 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11462 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11463 && COMPOSITION_VALID_P (start, end, prop)
11464 && start < prev_pt && end > prev_pt)
11465 /* The last point was within the composition. Return 1 iff
11466 point moved out of the composition. */
11467 return (pt <= start || pt >= end);
11468 }
11469
11470 /* Check a composition at the current point. */
11471 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11472 && find_composition (pt, -1, &start, &end, &prop, buffer)
11473 && COMPOSITION_VALID_P (start, end, prop)
11474 && start < pt && end > pt);
11475 }
11476
11477
11478 /* Reconsider the setting of B->clip_changed which is displayed
11479 in window W. */
11480
11481 static INLINE void
11482 reconsider_clip_changes (w, b)
11483 struct window *w;
11484 struct buffer *b;
11485 {
11486 if (b->clip_changed
11487 && !NILP (w->window_end_valid)
11488 && w->current_matrix->buffer == b
11489 && w->current_matrix->zv == BUF_ZV (b)
11490 && w->current_matrix->begv == BUF_BEGV (b))
11491 b->clip_changed = 0;
11492
11493 /* If display wasn't paused, and W is not a tool bar window, see if
11494 point has been moved into or out of a composition. In that case,
11495 we set b->clip_changed to 1 to force updating the screen. If
11496 b->clip_changed has already been set to 1, we can skip this
11497 check. */
11498 if (!b->clip_changed
11499 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11500 {
11501 int pt;
11502
11503 if (w == XWINDOW (selected_window))
11504 pt = BUF_PT (current_buffer);
11505 else
11506 pt = marker_position (w->pointm);
11507
11508 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11509 || pt != XINT (w->last_point))
11510 && check_point_in_composition (w->current_matrix->buffer,
11511 XINT (w->last_point),
11512 XBUFFER (w->buffer), pt))
11513 b->clip_changed = 1;
11514 }
11515 }
11516 \f
11517
11518 /* Select FRAME to forward the values of frame-local variables into C
11519 variables so that the redisplay routines can access those values
11520 directly. */
11521
11522 static void
11523 select_frame_for_redisplay (frame)
11524 Lisp_Object frame;
11525 {
11526 Lisp_Object tail, symbol, val;
11527 Lisp_Object old = selected_frame;
11528 struct Lisp_Symbol *sym;
11529
11530 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11531
11532 selected_frame = frame;
11533
11534 do
11535 {
11536 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11537 if (CONSP (XCAR (tail))
11538 && (symbol = XCAR (XCAR (tail)),
11539 SYMBOLP (symbol))
11540 && (sym = indirect_variable (XSYMBOL (symbol)),
11541 val = sym->value,
11542 (BUFFER_LOCAL_VALUEP (val)))
11543 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11544 /* Use find_symbol_value rather than Fsymbol_value
11545 to avoid an error if it is void. */
11546 find_symbol_value (symbol);
11547 } while (!EQ (frame, old) && (frame = old, 1));
11548 }
11549
11550
11551 #define STOP_POLLING \
11552 do { if (! polling_stopped_here) stop_polling (); \
11553 polling_stopped_here = 1; } while (0)
11554
11555 #define RESUME_POLLING \
11556 do { if (polling_stopped_here) start_polling (); \
11557 polling_stopped_here = 0; } while (0)
11558
11559
11560 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11561 response to any user action; therefore, we should preserve the echo
11562 area. (Actually, our caller does that job.) Perhaps in the future
11563 avoid recentering windows if it is not necessary; currently that
11564 causes some problems. */
11565
11566 static void
11567 redisplay_internal (preserve_echo_area)
11568 int preserve_echo_area;
11569 {
11570 struct window *w = XWINDOW (selected_window);
11571 struct frame *f;
11572 int pause;
11573 int must_finish = 0;
11574 struct text_pos tlbufpos, tlendpos;
11575 int number_of_visible_frames;
11576 int count, count1;
11577 struct frame *sf;
11578 int polling_stopped_here = 0;
11579 Lisp_Object old_frame = selected_frame;
11580
11581 /* Non-zero means redisplay has to consider all windows on all
11582 frames. Zero means, only selected_window is considered. */
11583 int consider_all_windows_p;
11584
11585 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11586
11587 /* No redisplay if running in batch mode or frame is not yet fully
11588 initialized, or redisplay is explicitly turned off by setting
11589 Vinhibit_redisplay. */
11590 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11591 || !NILP (Vinhibit_redisplay))
11592 return;
11593
11594 /* Don't examine these until after testing Vinhibit_redisplay.
11595 When Emacs is shutting down, perhaps because its connection to
11596 X has dropped, we should not look at them at all. */
11597 f = XFRAME (w->frame);
11598 sf = SELECTED_FRAME ();
11599
11600 if (!f->glyphs_initialized_p)
11601 return;
11602
11603 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11604 if (popup_activated ())
11605 return;
11606 #endif
11607
11608 /* I don't think this happens but let's be paranoid. */
11609 if (redisplaying_p)
11610 return;
11611
11612 /* Record a function that resets redisplaying_p to its old value
11613 when we leave this function. */
11614 count = SPECPDL_INDEX ();
11615 record_unwind_protect (unwind_redisplay,
11616 Fcons (make_number (redisplaying_p), selected_frame));
11617 ++redisplaying_p;
11618 specbind (Qinhibit_free_realized_faces, Qnil);
11619
11620 {
11621 Lisp_Object tail, frame;
11622
11623 FOR_EACH_FRAME (tail, frame)
11624 {
11625 struct frame *f = XFRAME (frame);
11626 f->already_hscrolled_p = 0;
11627 }
11628 }
11629
11630 retry:
11631 if (!EQ (old_frame, selected_frame)
11632 && FRAME_LIVE_P (XFRAME (old_frame)))
11633 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11634 selected_frame and selected_window to be temporarily out-of-sync so
11635 when we come back here via `goto retry', we need to resync because we
11636 may need to run Elisp code (via prepare_menu_bars). */
11637 select_frame_for_redisplay (old_frame);
11638
11639 pause = 0;
11640 reconsider_clip_changes (w, current_buffer);
11641 last_escape_glyph_frame = NULL;
11642 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11643
11644 /* If new fonts have been loaded that make a glyph matrix adjustment
11645 necessary, do it. */
11646 if (fonts_changed_p)
11647 {
11648 adjust_glyphs (NULL);
11649 ++windows_or_buffers_changed;
11650 fonts_changed_p = 0;
11651 }
11652
11653 /* If face_change_count is non-zero, init_iterator will free all
11654 realized faces, which includes the faces referenced from current
11655 matrices. So, we can't reuse current matrices in this case. */
11656 if (face_change_count)
11657 ++windows_or_buffers_changed;
11658
11659 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11660 && FRAME_TTY (sf)->previous_frame != sf)
11661 {
11662 /* Since frames on a single ASCII terminal share the same
11663 display area, displaying a different frame means redisplay
11664 the whole thing. */
11665 windows_or_buffers_changed++;
11666 SET_FRAME_GARBAGED (sf);
11667 #ifndef DOS_NT
11668 set_tty_color_mode (FRAME_TTY (sf), sf);
11669 #endif
11670 FRAME_TTY (sf)->previous_frame = sf;
11671 }
11672
11673 /* Set the visible flags for all frames. Do this before checking
11674 for resized or garbaged frames; they want to know if their frames
11675 are visible. See the comment in frame.h for
11676 FRAME_SAMPLE_VISIBILITY. */
11677 {
11678 Lisp_Object tail, frame;
11679
11680 number_of_visible_frames = 0;
11681
11682 FOR_EACH_FRAME (tail, frame)
11683 {
11684 struct frame *f = XFRAME (frame);
11685
11686 FRAME_SAMPLE_VISIBILITY (f);
11687 if (FRAME_VISIBLE_P (f))
11688 ++number_of_visible_frames;
11689 clear_desired_matrices (f);
11690 }
11691 }
11692
11693 /* Notice any pending interrupt request to change frame size. */
11694 do_pending_window_change (1);
11695
11696 /* Clear frames marked as garbaged. */
11697 if (frame_garbaged)
11698 clear_garbaged_frames ();
11699
11700 /* Build menubar and tool-bar items. */
11701 if (NILP (Vmemory_full))
11702 prepare_menu_bars ();
11703
11704 if (windows_or_buffers_changed)
11705 update_mode_lines++;
11706
11707 /* Detect case that we need to write or remove a star in the mode line. */
11708 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11709 {
11710 w->update_mode_line = Qt;
11711 if (buffer_shared > 1)
11712 update_mode_lines++;
11713 }
11714
11715 /* Avoid invocation of point motion hooks by `current_column' below. */
11716 count1 = SPECPDL_INDEX ();
11717 specbind (Qinhibit_point_motion_hooks, Qt);
11718
11719 /* If %c is in the mode line, update it if needed. */
11720 if (!NILP (w->column_number_displayed)
11721 /* This alternative quickly identifies a common case
11722 where no change is needed. */
11723 && !(PT == XFASTINT (w->last_point)
11724 && XFASTINT (w->last_modified) >= MODIFF
11725 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11726 && (XFASTINT (w->column_number_displayed)
11727 != (int) current_column ())) /* iftc */
11728 w->update_mode_line = Qt;
11729
11730 unbind_to (count1, Qnil);
11731
11732 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11733
11734 /* The variable buffer_shared is set in redisplay_window and
11735 indicates that we redisplay a buffer in different windows. See
11736 there. */
11737 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11738 || cursor_type_changed);
11739
11740 /* If specs for an arrow have changed, do thorough redisplay
11741 to ensure we remove any arrow that should no longer exist. */
11742 if (overlay_arrows_changed_p ())
11743 consider_all_windows_p = windows_or_buffers_changed = 1;
11744
11745 /* Normally the message* functions will have already displayed and
11746 updated the echo area, but the frame may have been trashed, or
11747 the update may have been preempted, so display the echo area
11748 again here. Checking message_cleared_p captures the case that
11749 the echo area should be cleared. */
11750 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11751 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11752 || (message_cleared_p
11753 && minibuf_level == 0
11754 /* If the mini-window is currently selected, this means the
11755 echo-area doesn't show through. */
11756 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11757 {
11758 int window_height_changed_p = echo_area_display (0);
11759 must_finish = 1;
11760
11761 /* If we don't display the current message, don't clear the
11762 message_cleared_p flag, because, if we did, we wouldn't clear
11763 the echo area in the next redisplay which doesn't preserve
11764 the echo area. */
11765 if (!display_last_displayed_message_p)
11766 message_cleared_p = 0;
11767
11768 if (fonts_changed_p)
11769 goto retry;
11770 else if (window_height_changed_p)
11771 {
11772 consider_all_windows_p = 1;
11773 ++update_mode_lines;
11774 ++windows_or_buffers_changed;
11775
11776 /* If window configuration was changed, frames may have been
11777 marked garbaged. Clear them or we will experience
11778 surprises wrt scrolling. */
11779 if (frame_garbaged)
11780 clear_garbaged_frames ();
11781 }
11782 }
11783 else if (EQ (selected_window, minibuf_window)
11784 && (current_buffer->clip_changed
11785 || XFASTINT (w->last_modified) < MODIFF
11786 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11787 && resize_mini_window (w, 0))
11788 {
11789 /* Resized active mini-window to fit the size of what it is
11790 showing if its contents might have changed. */
11791 must_finish = 1;
11792 /* FIXME: this causes all frames to be updated, which seems unnecessary
11793 since only the current frame needs to be considered. This function needs
11794 to be rewritten with two variables, consider_all_windows and
11795 consider_all_frames. */
11796 consider_all_windows_p = 1;
11797 ++windows_or_buffers_changed;
11798 ++update_mode_lines;
11799
11800 /* If window configuration was changed, frames may have been
11801 marked garbaged. Clear them or we will experience
11802 surprises wrt scrolling. */
11803 if (frame_garbaged)
11804 clear_garbaged_frames ();
11805 }
11806
11807
11808 /* If showing the region, and mark has changed, we must redisplay
11809 the whole window. The assignment to this_line_start_pos prevents
11810 the optimization directly below this if-statement. */
11811 if (((!NILP (Vtransient_mark_mode)
11812 && !NILP (XBUFFER (w->buffer)->mark_active))
11813 != !NILP (w->region_showing))
11814 || (!NILP (w->region_showing)
11815 && !EQ (w->region_showing,
11816 Fmarker_position (XBUFFER (w->buffer)->mark))))
11817 CHARPOS (this_line_start_pos) = 0;
11818
11819 /* Optimize the case that only the line containing the cursor in the
11820 selected window has changed. Variables starting with this_ are
11821 set in display_line and record information about the line
11822 containing the cursor. */
11823 tlbufpos = this_line_start_pos;
11824 tlendpos = this_line_end_pos;
11825 if (!consider_all_windows_p
11826 && CHARPOS (tlbufpos) > 0
11827 && NILP (w->update_mode_line)
11828 && !current_buffer->clip_changed
11829 && !current_buffer->prevent_redisplay_optimizations_p
11830 && FRAME_VISIBLE_P (XFRAME (w->frame))
11831 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11832 /* Make sure recorded data applies to current buffer, etc. */
11833 && this_line_buffer == current_buffer
11834 && current_buffer == XBUFFER (w->buffer)
11835 && NILP (w->force_start)
11836 && NILP (w->optional_new_start)
11837 /* Point must be on the line that we have info recorded about. */
11838 && PT >= CHARPOS (tlbufpos)
11839 && PT <= Z - CHARPOS (tlendpos)
11840 /* All text outside that line, including its final newline,
11841 must be unchanged. */
11842 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11843 CHARPOS (tlendpos)))
11844 {
11845 if (CHARPOS (tlbufpos) > BEGV
11846 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11847 && (CHARPOS (tlbufpos) == ZV
11848 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11849 /* Former continuation line has disappeared by becoming empty. */
11850 goto cancel;
11851 else if (XFASTINT (w->last_modified) < MODIFF
11852 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11853 || MINI_WINDOW_P (w))
11854 {
11855 /* We have to handle the case of continuation around a
11856 wide-column character (see the comment in indent.c around
11857 line 1340).
11858
11859 For instance, in the following case:
11860
11861 -------- Insert --------
11862 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11863 J_I_ ==> J_I_ `^^' are cursors.
11864 ^^ ^^
11865 -------- --------
11866
11867 As we have to redraw the line above, we cannot use this
11868 optimization. */
11869
11870 struct it it;
11871 int line_height_before = this_line_pixel_height;
11872
11873 /* Note that start_display will handle the case that the
11874 line starting at tlbufpos is a continuation line. */
11875 start_display (&it, w, tlbufpos);
11876
11877 /* Implementation note: It this still necessary? */
11878 if (it.current_x != this_line_start_x)
11879 goto cancel;
11880
11881 TRACE ((stderr, "trying display optimization 1\n"));
11882 w->cursor.vpos = -1;
11883 overlay_arrow_seen = 0;
11884 it.vpos = this_line_vpos;
11885 it.current_y = this_line_y;
11886 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11887 display_line (&it);
11888
11889 /* If line contains point, is not continued,
11890 and ends at same distance from eob as before, we win. */
11891 if (w->cursor.vpos >= 0
11892 /* Line is not continued, otherwise this_line_start_pos
11893 would have been set to 0 in display_line. */
11894 && CHARPOS (this_line_start_pos)
11895 /* Line ends as before. */
11896 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11897 /* Line has same height as before. Otherwise other lines
11898 would have to be shifted up or down. */
11899 && this_line_pixel_height == line_height_before)
11900 {
11901 /* If this is not the window's last line, we must adjust
11902 the charstarts of the lines below. */
11903 if (it.current_y < it.last_visible_y)
11904 {
11905 struct glyph_row *row
11906 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11907 int delta, delta_bytes;
11908
11909 /* We used to distinguish between two cases here,
11910 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11911 when the line ends in a newline or the end of the
11912 buffer's accessible portion. But both cases did
11913 the same, so they were collapsed. */
11914 delta = (Z
11915 - CHARPOS (tlendpos)
11916 - MATRIX_ROW_START_CHARPOS (row));
11917 delta_bytes = (Z_BYTE
11918 - BYTEPOS (tlendpos)
11919 - MATRIX_ROW_START_BYTEPOS (row));
11920
11921 increment_matrix_positions (w->current_matrix,
11922 this_line_vpos + 1,
11923 w->current_matrix->nrows,
11924 delta, delta_bytes);
11925 }
11926
11927 /* If this row displays text now but previously didn't,
11928 or vice versa, w->window_end_vpos may have to be
11929 adjusted. */
11930 if ((it.glyph_row - 1)->displays_text_p)
11931 {
11932 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11933 XSETINT (w->window_end_vpos, this_line_vpos);
11934 }
11935 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11936 && this_line_vpos > 0)
11937 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11938 w->window_end_valid = Qnil;
11939
11940 /* Update hint: No need to try to scroll in update_window. */
11941 w->desired_matrix->no_scrolling_p = 1;
11942
11943 #if GLYPH_DEBUG
11944 *w->desired_matrix->method = 0;
11945 debug_method_add (w, "optimization 1");
11946 #endif
11947 #ifdef HAVE_WINDOW_SYSTEM
11948 update_window_fringes (w, 0);
11949 #endif
11950 goto update;
11951 }
11952 else
11953 goto cancel;
11954 }
11955 else if (/* Cursor position hasn't changed. */
11956 PT == XFASTINT (w->last_point)
11957 /* Make sure the cursor was last displayed
11958 in this window. Otherwise we have to reposition it. */
11959 && 0 <= w->cursor.vpos
11960 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11961 {
11962 if (!must_finish)
11963 {
11964 do_pending_window_change (1);
11965
11966 /* We used to always goto end_of_redisplay here, but this
11967 isn't enough if we have a blinking cursor. */
11968 if (w->cursor_off_p == w->last_cursor_off_p)
11969 goto end_of_redisplay;
11970 }
11971 goto update;
11972 }
11973 /* If highlighting the region, or if the cursor is in the echo area,
11974 then we can't just move the cursor. */
11975 else if (! (!NILP (Vtransient_mark_mode)
11976 && !NILP (current_buffer->mark_active))
11977 && (EQ (selected_window, current_buffer->last_selected_window)
11978 || highlight_nonselected_windows)
11979 && NILP (w->region_showing)
11980 && NILP (Vshow_trailing_whitespace)
11981 && !cursor_in_echo_area)
11982 {
11983 struct it it;
11984 struct glyph_row *row;
11985
11986 /* Skip from tlbufpos to PT and see where it is. Note that
11987 PT may be in invisible text. If so, we will end at the
11988 next visible position. */
11989 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11990 NULL, DEFAULT_FACE_ID);
11991 it.current_x = this_line_start_x;
11992 it.current_y = this_line_y;
11993 it.vpos = this_line_vpos;
11994
11995 /* The call to move_it_to stops in front of PT, but
11996 moves over before-strings. */
11997 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11998
11999 if (it.vpos == this_line_vpos
12000 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12001 row->enabled_p))
12002 {
12003 xassert (this_line_vpos == it.vpos);
12004 xassert (this_line_y == it.current_y);
12005 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12006 #if GLYPH_DEBUG
12007 *w->desired_matrix->method = 0;
12008 debug_method_add (w, "optimization 3");
12009 #endif
12010 goto update;
12011 }
12012 else
12013 goto cancel;
12014 }
12015
12016 cancel:
12017 /* Text changed drastically or point moved off of line. */
12018 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12019 }
12020
12021 CHARPOS (this_line_start_pos) = 0;
12022 consider_all_windows_p |= buffer_shared > 1;
12023 ++clear_face_cache_count;
12024 #ifdef HAVE_WINDOW_SYSTEM
12025 ++clear_image_cache_count;
12026 #endif
12027
12028 /* Build desired matrices, and update the display. If
12029 consider_all_windows_p is non-zero, do it for all windows on all
12030 frames. Otherwise do it for selected_window, only. */
12031
12032 if (consider_all_windows_p)
12033 {
12034 Lisp_Object tail, frame;
12035
12036 FOR_EACH_FRAME (tail, frame)
12037 XFRAME (frame)->updated_p = 0;
12038
12039 /* Recompute # windows showing selected buffer. This will be
12040 incremented each time such a window is displayed. */
12041 buffer_shared = 0;
12042
12043 FOR_EACH_FRAME (tail, frame)
12044 {
12045 struct frame *f = XFRAME (frame);
12046
12047 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12048 {
12049 if (! EQ (frame, selected_frame))
12050 /* Select the frame, for the sake of frame-local
12051 variables. */
12052 select_frame_for_redisplay (frame);
12053
12054 /* Mark all the scroll bars to be removed; we'll redeem
12055 the ones we want when we redisplay their windows. */
12056 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12057 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12058
12059 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12060 redisplay_windows (FRAME_ROOT_WINDOW (f));
12061
12062 /* The X error handler may have deleted that frame. */
12063 if (!FRAME_LIVE_P (f))
12064 continue;
12065
12066 /* Any scroll bars which redisplay_windows should have
12067 nuked should now go away. */
12068 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12069 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12070
12071 /* If fonts changed, display again. */
12072 /* ??? rms: I suspect it is a mistake to jump all the way
12073 back to retry here. It should just retry this frame. */
12074 if (fonts_changed_p)
12075 goto retry;
12076
12077 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12078 {
12079 /* See if we have to hscroll. */
12080 if (!f->already_hscrolled_p)
12081 {
12082 f->already_hscrolled_p = 1;
12083 if (hscroll_windows (f->root_window))
12084 goto retry;
12085 }
12086
12087 /* Prevent various kinds of signals during display
12088 update. stdio is not robust about handling
12089 signals, which can cause an apparent I/O
12090 error. */
12091 if (interrupt_input)
12092 unrequest_sigio ();
12093 STOP_POLLING;
12094
12095 /* Update the display. */
12096 set_window_update_flags (XWINDOW (f->root_window), 1);
12097 pause |= update_frame (f, 0, 0);
12098 f->updated_p = 1;
12099 }
12100 }
12101 }
12102
12103 if (!EQ (old_frame, selected_frame)
12104 && FRAME_LIVE_P (XFRAME (old_frame)))
12105 /* We played a bit fast-and-loose above and allowed selected_frame
12106 and selected_window to be temporarily out-of-sync but let's make
12107 sure this stays contained. */
12108 select_frame_for_redisplay (old_frame);
12109 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12110
12111 if (!pause)
12112 {
12113 /* Do the mark_window_display_accurate after all windows have
12114 been redisplayed because this call resets flags in buffers
12115 which are needed for proper redisplay. */
12116 FOR_EACH_FRAME (tail, frame)
12117 {
12118 struct frame *f = XFRAME (frame);
12119 if (f->updated_p)
12120 {
12121 mark_window_display_accurate (f->root_window, 1);
12122 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12123 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12124 }
12125 }
12126 }
12127 }
12128 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12129 {
12130 Lisp_Object mini_window;
12131 struct frame *mini_frame;
12132
12133 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12134 /* Use list_of_error, not Qerror, so that
12135 we catch only errors and don't run the debugger. */
12136 internal_condition_case_1 (redisplay_window_1, selected_window,
12137 list_of_error,
12138 redisplay_window_error);
12139
12140 /* Compare desired and current matrices, perform output. */
12141
12142 update:
12143 /* If fonts changed, display again. */
12144 if (fonts_changed_p)
12145 goto retry;
12146
12147 /* Prevent various kinds of signals during display update.
12148 stdio is not robust about handling signals,
12149 which can cause an apparent I/O error. */
12150 if (interrupt_input)
12151 unrequest_sigio ();
12152 STOP_POLLING;
12153
12154 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12155 {
12156 if (hscroll_windows (selected_window))
12157 goto retry;
12158
12159 XWINDOW (selected_window)->must_be_updated_p = 1;
12160 pause = update_frame (sf, 0, 0);
12161 }
12162
12163 /* We may have called echo_area_display at the top of this
12164 function. If the echo area is on another frame, that may
12165 have put text on a frame other than the selected one, so the
12166 above call to update_frame would not have caught it. Catch
12167 it here. */
12168 mini_window = FRAME_MINIBUF_WINDOW (sf);
12169 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12170
12171 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12172 {
12173 XWINDOW (mini_window)->must_be_updated_p = 1;
12174 pause |= update_frame (mini_frame, 0, 0);
12175 if (!pause && hscroll_windows (mini_window))
12176 goto retry;
12177 }
12178 }
12179
12180 /* If display was paused because of pending input, make sure we do a
12181 thorough update the next time. */
12182 if (pause)
12183 {
12184 /* Prevent the optimization at the beginning of
12185 redisplay_internal that tries a single-line update of the
12186 line containing the cursor in the selected window. */
12187 CHARPOS (this_line_start_pos) = 0;
12188
12189 /* Let the overlay arrow be updated the next time. */
12190 update_overlay_arrows (0);
12191
12192 /* If we pause after scrolling, some rows in the current
12193 matrices of some windows are not valid. */
12194 if (!WINDOW_FULL_WIDTH_P (w)
12195 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12196 update_mode_lines = 1;
12197 }
12198 else
12199 {
12200 if (!consider_all_windows_p)
12201 {
12202 /* This has already been done above if
12203 consider_all_windows_p is set. */
12204 mark_window_display_accurate_1 (w, 1);
12205
12206 /* Say overlay arrows are up to date. */
12207 update_overlay_arrows (1);
12208
12209 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12210 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12211 }
12212
12213 update_mode_lines = 0;
12214 windows_or_buffers_changed = 0;
12215 cursor_type_changed = 0;
12216 }
12217
12218 /* Start SIGIO interrupts coming again. Having them off during the
12219 code above makes it less likely one will discard output, but not
12220 impossible, since there might be stuff in the system buffer here.
12221 But it is much hairier to try to do anything about that. */
12222 if (interrupt_input)
12223 request_sigio ();
12224 RESUME_POLLING;
12225
12226 /* If a frame has become visible which was not before, redisplay
12227 again, so that we display it. Expose events for such a frame
12228 (which it gets when becoming visible) don't call the parts of
12229 redisplay constructing glyphs, so simply exposing a frame won't
12230 display anything in this case. So, we have to display these
12231 frames here explicitly. */
12232 if (!pause)
12233 {
12234 Lisp_Object tail, frame;
12235 int new_count = 0;
12236
12237 FOR_EACH_FRAME (tail, frame)
12238 {
12239 int this_is_visible = 0;
12240
12241 if (XFRAME (frame)->visible)
12242 this_is_visible = 1;
12243 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12244 if (XFRAME (frame)->visible)
12245 this_is_visible = 1;
12246
12247 if (this_is_visible)
12248 new_count++;
12249 }
12250
12251 if (new_count != number_of_visible_frames)
12252 windows_or_buffers_changed++;
12253 }
12254
12255 /* Change frame size now if a change is pending. */
12256 do_pending_window_change (1);
12257
12258 /* If we just did a pending size change, or have additional
12259 visible frames, redisplay again. */
12260 if (windows_or_buffers_changed && !pause)
12261 goto retry;
12262
12263 /* Clear the face cache eventually. */
12264 if (consider_all_windows_p)
12265 {
12266 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12267 {
12268 clear_face_cache (0);
12269 clear_face_cache_count = 0;
12270 }
12271 #ifdef HAVE_WINDOW_SYSTEM
12272 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12273 {
12274 clear_image_caches (Qnil);
12275 clear_image_cache_count = 0;
12276 }
12277 #endif /* HAVE_WINDOW_SYSTEM */
12278 }
12279
12280 end_of_redisplay:
12281 unbind_to (count, Qnil);
12282 RESUME_POLLING;
12283 }
12284
12285
12286 /* Redisplay, but leave alone any recent echo area message unless
12287 another message has been requested in its place.
12288
12289 This is useful in situations where you need to redisplay but no
12290 user action has occurred, making it inappropriate for the message
12291 area to be cleared. See tracking_off and
12292 wait_reading_process_output for examples of these situations.
12293
12294 FROM_WHERE is an integer saying from where this function was
12295 called. This is useful for debugging. */
12296
12297 void
12298 redisplay_preserve_echo_area (from_where)
12299 int from_where;
12300 {
12301 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12302
12303 if (!NILP (echo_area_buffer[1]))
12304 {
12305 /* We have a previously displayed message, but no current
12306 message. Redisplay the previous message. */
12307 display_last_displayed_message_p = 1;
12308 redisplay_internal (1);
12309 display_last_displayed_message_p = 0;
12310 }
12311 else
12312 redisplay_internal (1);
12313
12314 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12315 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12316 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12317 }
12318
12319
12320 /* Function registered with record_unwind_protect in
12321 redisplay_internal. Reset redisplaying_p to the value it had
12322 before redisplay_internal was called, and clear
12323 prevent_freeing_realized_faces_p. It also selects the previously
12324 selected frame, unless it has been deleted (by an X connection
12325 failure during redisplay, for example). */
12326
12327 static Lisp_Object
12328 unwind_redisplay (val)
12329 Lisp_Object val;
12330 {
12331 Lisp_Object old_redisplaying_p, old_frame;
12332
12333 old_redisplaying_p = XCAR (val);
12334 redisplaying_p = XFASTINT (old_redisplaying_p);
12335 old_frame = XCDR (val);
12336 if (! EQ (old_frame, selected_frame)
12337 && FRAME_LIVE_P (XFRAME (old_frame)))
12338 select_frame_for_redisplay (old_frame);
12339 return Qnil;
12340 }
12341
12342
12343 /* Mark the display of window W as accurate or inaccurate. If
12344 ACCURATE_P is non-zero mark display of W as accurate. If
12345 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12346 redisplay_internal is called. */
12347
12348 static void
12349 mark_window_display_accurate_1 (w, accurate_p)
12350 struct window *w;
12351 int accurate_p;
12352 {
12353 if (BUFFERP (w->buffer))
12354 {
12355 struct buffer *b = XBUFFER (w->buffer);
12356
12357 w->last_modified
12358 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12359 w->last_overlay_modified
12360 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12361 w->last_had_star
12362 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12363
12364 if (accurate_p)
12365 {
12366 b->clip_changed = 0;
12367 b->prevent_redisplay_optimizations_p = 0;
12368
12369 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12370 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12371 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12372 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12373
12374 w->current_matrix->buffer = b;
12375 w->current_matrix->begv = BUF_BEGV (b);
12376 w->current_matrix->zv = BUF_ZV (b);
12377
12378 w->last_cursor = w->cursor;
12379 w->last_cursor_off_p = w->cursor_off_p;
12380
12381 if (w == XWINDOW (selected_window))
12382 w->last_point = make_number (BUF_PT (b));
12383 else
12384 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12385 }
12386 }
12387
12388 if (accurate_p)
12389 {
12390 w->window_end_valid = w->buffer;
12391 w->update_mode_line = Qnil;
12392 }
12393 }
12394
12395
12396 /* Mark the display of windows in the window tree rooted at WINDOW as
12397 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12398 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12399 be redisplayed the next time redisplay_internal is called. */
12400
12401 void
12402 mark_window_display_accurate (window, accurate_p)
12403 Lisp_Object window;
12404 int accurate_p;
12405 {
12406 struct window *w;
12407
12408 for (; !NILP (window); window = w->next)
12409 {
12410 w = XWINDOW (window);
12411 mark_window_display_accurate_1 (w, accurate_p);
12412
12413 if (!NILP (w->vchild))
12414 mark_window_display_accurate (w->vchild, accurate_p);
12415 if (!NILP (w->hchild))
12416 mark_window_display_accurate (w->hchild, accurate_p);
12417 }
12418
12419 if (accurate_p)
12420 {
12421 update_overlay_arrows (1);
12422 }
12423 else
12424 {
12425 /* Force a thorough redisplay the next time by setting
12426 last_arrow_position and last_arrow_string to t, which is
12427 unequal to any useful value of Voverlay_arrow_... */
12428 update_overlay_arrows (-1);
12429 }
12430 }
12431
12432
12433 /* Return value in display table DP (Lisp_Char_Table *) for character
12434 C. Since a display table doesn't have any parent, we don't have to
12435 follow parent. Do not call this function directly but use the
12436 macro DISP_CHAR_VECTOR. */
12437
12438 Lisp_Object
12439 disp_char_vector (dp, c)
12440 struct Lisp_Char_Table *dp;
12441 int c;
12442 {
12443 Lisp_Object val;
12444
12445 if (ASCII_CHAR_P (c))
12446 {
12447 val = dp->ascii;
12448 if (SUB_CHAR_TABLE_P (val))
12449 val = XSUB_CHAR_TABLE (val)->contents[c];
12450 }
12451 else
12452 {
12453 Lisp_Object table;
12454
12455 XSETCHAR_TABLE (table, dp);
12456 val = char_table_ref (table, c);
12457 }
12458 if (NILP (val))
12459 val = dp->defalt;
12460 return val;
12461 }
12462
12463
12464 \f
12465 /***********************************************************************
12466 Window Redisplay
12467 ***********************************************************************/
12468
12469 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12470
12471 static void
12472 redisplay_windows (window)
12473 Lisp_Object window;
12474 {
12475 while (!NILP (window))
12476 {
12477 struct window *w = XWINDOW (window);
12478
12479 if (!NILP (w->hchild))
12480 redisplay_windows (w->hchild);
12481 else if (!NILP (w->vchild))
12482 redisplay_windows (w->vchild);
12483 else if (!NILP (w->buffer))
12484 {
12485 displayed_buffer = XBUFFER (w->buffer);
12486 /* Use list_of_error, not Qerror, so that
12487 we catch only errors and don't run the debugger. */
12488 internal_condition_case_1 (redisplay_window_0, window,
12489 list_of_error,
12490 redisplay_window_error);
12491 }
12492
12493 window = w->next;
12494 }
12495 }
12496
12497 static Lisp_Object
12498 redisplay_window_error ()
12499 {
12500 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12501 return Qnil;
12502 }
12503
12504 static Lisp_Object
12505 redisplay_window_0 (window)
12506 Lisp_Object window;
12507 {
12508 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12509 redisplay_window (window, 0);
12510 return Qnil;
12511 }
12512
12513 static Lisp_Object
12514 redisplay_window_1 (window)
12515 Lisp_Object window;
12516 {
12517 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12518 redisplay_window (window, 1);
12519 return Qnil;
12520 }
12521 \f
12522
12523 /* Increment GLYPH until it reaches END or CONDITION fails while
12524 adding (GLYPH)->pixel_width to X. */
12525
12526 #define SKIP_GLYPHS(glyph, end, x, condition) \
12527 do \
12528 { \
12529 (x) += (glyph)->pixel_width; \
12530 ++(glyph); \
12531 } \
12532 while ((glyph) < (end) && (condition))
12533
12534
12535 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12536 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12537 which positions recorded in ROW differ from current buffer
12538 positions.
12539
12540 Return 0 if cursor is not on this row, 1 otherwise. */
12541
12542 int
12543 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12544 struct window *w;
12545 struct glyph_row *row;
12546 struct glyph_matrix *matrix;
12547 int delta, delta_bytes, dy, dvpos;
12548 {
12549 struct glyph *glyph = row->glyphs[TEXT_AREA];
12550 struct glyph *end = glyph + row->used[TEXT_AREA];
12551 struct glyph *cursor = NULL;
12552 /* The last known character position in row. */
12553 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12554 int x = row->x;
12555 EMACS_INT pt_old = PT - delta;
12556 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12557 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12558 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12559 /* A glyph beyond the edge of TEXT_AREA which we should never
12560 touch. */
12561 struct glyph *glyphs_end = end;
12562 /* Non-zero means we've found a match for cursor position, but that
12563 glyph has the avoid_cursor_p flag set. */
12564 int match_with_avoid_cursor = 0;
12565 /* Non-zero means we've seen at least one glyph that came from a
12566 display string. */
12567 int string_seen = 0;
12568 /* Largest buffer position seen so far during scan of glyph row. */
12569 EMACS_INT bpos_max = last_pos;
12570 /* Last buffer position covered by an overlay string with an integer
12571 `cursor' property. */
12572 EMACS_INT bpos_covered = 0;
12573
12574 /* Skip over glyphs not having an object at the start and the end of
12575 the row. These are special glyphs like truncation marks on
12576 terminal frames. */
12577 if (row->displays_text_p)
12578 {
12579 if (!row->reversed_p)
12580 {
12581 while (glyph < end
12582 && INTEGERP (glyph->object)
12583 && glyph->charpos < 0)
12584 {
12585 x += glyph->pixel_width;
12586 ++glyph;
12587 }
12588 while (end > glyph
12589 && INTEGERP ((end - 1)->object)
12590 /* CHARPOS is zero for blanks and stretch glyphs
12591 inserted by extend_face_to_end_of_line. */
12592 && (end - 1)->charpos <= 0)
12593 --end;
12594 glyph_before = glyph - 1;
12595 glyph_after = end;
12596 }
12597 else
12598 {
12599 struct glyph *g;
12600
12601 /* If the glyph row is reversed, we need to process it from back
12602 to front, so swap the edge pointers. */
12603 glyphs_end = end = glyph - 1;
12604 glyph += row->used[TEXT_AREA] - 1;
12605
12606 while (glyph > end + 1
12607 && INTEGERP (glyph->object)
12608 && glyph->charpos < 0)
12609 {
12610 --glyph;
12611 x -= glyph->pixel_width;
12612 }
12613 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12614 --glyph;
12615 /* By default, in reversed rows we put the cursor on the
12616 rightmost (first in the reading order) glyph. */
12617 for (g = end + 1; g < glyph; g++)
12618 x += g->pixel_width;
12619 while (end < glyph
12620 && INTEGERP ((end + 1)->object)
12621 && (end + 1)->charpos <= 0)
12622 ++end;
12623 glyph_before = glyph + 1;
12624 glyph_after = end;
12625 }
12626 }
12627 else if (row->reversed_p)
12628 {
12629 /* In R2L rows that don't display text, put the cursor on the
12630 rightmost glyph. Case in point: an empty last line that is
12631 part of an R2L paragraph. */
12632 cursor = end - 1;
12633 x = -1; /* will be computed below, at label compute_x */
12634 }
12635
12636 /* Step 1: Try to find the glyph whose character position
12637 corresponds to point. If that's not possible, find 2 glyphs
12638 whose character positions are the closest to point, one before
12639 point, the other after it. */
12640 if (!row->reversed_p)
12641 while (/* not marched to end of glyph row */
12642 glyph < end
12643 /* glyph was not inserted by redisplay for internal purposes */
12644 && !INTEGERP (glyph->object))
12645 {
12646 if (BUFFERP (glyph->object))
12647 {
12648 EMACS_INT dpos = glyph->charpos - pt_old;
12649
12650 if (glyph->charpos > bpos_max)
12651 bpos_max = glyph->charpos;
12652 if (!glyph->avoid_cursor_p)
12653 {
12654 /* If we hit point, we've found the glyph on which to
12655 display the cursor. */
12656 if (dpos == 0)
12657 {
12658 match_with_avoid_cursor = 0;
12659 break;
12660 }
12661 /* See if we've found a better approximation to
12662 POS_BEFORE or to POS_AFTER. Note that we want the
12663 first (leftmost) glyph of all those that are the
12664 closest from below, and the last (rightmost) of all
12665 those from above. */
12666 if (0 > dpos && dpos > pos_before - pt_old)
12667 {
12668 pos_before = glyph->charpos;
12669 glyph_before = glyph;
12670 }
12671 else if (0 < dpos && dpos <= pos_after - pt_old)
12672 {
12673 pos_after = glyph->charpos;
12674 glyph_after = glyph;
12675 }
12676 }
12677 else if (dpos == 0)
12678 match_with_avoid_cursor = 1;
12679 }
12680 else if (STRINGP (glyph->object))
12681 {
12682 Lisp_Object chprop;
12683 int glyph_pos = glyph->charpos;
12684
12685 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12686 glyph->object);
12687 if (INTEGERP (chprop))
12688 {
12689 bpos_covered = bpos_max + XINT (chprop);
12690 /* If the `cursor' property covers buffer positions up
12691 to and including point, we should display cursor on
12692 this glyph. Note that overlays and text properties
12693 with string values stop bidi reordering, so every
12694 buffer position to the left of the string is always
12695 smaller than any position to the right of the
12696 string. Therefore, if a `cursor' property on one
12697 of the string's characters has an integer value, we
12698 will break out of the loop below _before_ we get to
12699 the position match above. IOW, integer values of
12700 the `cursor' property override the "exact match for
12701 point" strategy of positioning the cursor. */
12702 /* Implementation note: bpos_max == pt_old when, e.g.,
12703 we are in an empty line, where bpos_max is set to
12704 MATRIX_ROW_START_CHARPOS, see above. */
12705 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12706 {
12707 cursor = glyph;
12708 break;
12709 }
12710 }
12711
12712 string_seen = 1;
12713 }
12714 x += glyph->pixel_width;
12715 ++glyph;
12716 }
12717 else if (glyph > end) /* row is reversed */
12718 while (!INTEGERP (glyph->object))
12719 {
12720 if (BUFFERP (glyph->object))
12721 {
12722 EMACS_INT dpos = glyph->charpos - pt_old;
12723
12724 if (glyph->charpos > bpos_max)
12725 bpos_max = glyph->charpos;
12726 if (!glyph->avoid_cursor_p)
12727 {
12728 if (dpos == 0)
12729 {
12730 match_with_avoid_cursor = 0;
12731 break;
12732 }
12733 if (0 > dpos && dpos > pos_before - pt_old)
12734 {
12735 pos_before = glyph->charpos;
12736 glyph_before = glyph;
12737 }
12738 else if (0 < dpos && dpos <= pos_after - pt_old)
12739 {
12740 pos_after = glyph->charpos;
12741 glyph_after = glyph;
12742 }
12743 }
12744 else if (dpos == 0)
12745 match_with_avoid_cursor = 1;
12746 }
12747 else if (STRINGP (glyph->object))
12748 {
12749 Lisp_Object chprop;
12750 int glyph_pos = glyph->charpos;
12751
12752 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12753 glyph->object);
12754 if (INTEGERP (chprop))
12755 {
12756 bpos_covered = bpos_max + XINT (chprop);
12757 /* If the `cursor' property covers buffer positions up
12758 to and including point, we should display cursor on
12759 this glyph. */
12760 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12761 {
12762 cursor = glyph;
12763 break;
12764 }
12765 }
12766 string_seen = 1;
12767 }
12768 --glyph;
12769 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12770 {
12771 x--; /* can't use any pixel_width */
12772 break;
12773 }
12774 x -= glyph->pixel_width;
12775 }
12776
12777 /* Step 2: If we didn't find an exact match for point, we need to
12778 look for a proper place to put the cursor among glyphs between
12779 GLYPH_BEFORE and GLYPH_AFTER. */
12780 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12781 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12782 && bpos_covered < pt_old)
12783 {
12784 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12785 {
12786 EMACS_INT ellipsis_pos;
12787
12788 /* Scan back over the ellipsis glyphs. */
12789 if (!row->reversed_p)
12790 {
12791 ellipsis_pos = (glyph - 1)->charpos;
12792 while (glyph > row->glyphs[TEXT_AREA]
12793 && (glyph - 1)->charpos == ellipsis_pos)
12794 glyph--, x -= glyph->pixel_width;
12795 /* That loop always goes one position too far, including
12796 the glyph before the ellipsis. So scan forward over
12797 that one. */
12798 x += glyph->pixel_width;
12799 glyph++;
12800 }
12801 else /* row is reversed */
12802 {
12803 ellipsis_pos = (glyph + 1)->charpos;
12804 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12805 && (glyph + 1)->charpos == ellipsis_pos)
12806 glyph++, x += glyph->pixel_width;
12807 x -= glyph->pixel_width;
12808 glyph--;
12809 }
12810 }
12811 else if (match_with_avoid_cursor
12812 /* zero-width characters produce no glyphs */
12813 || ((row->reversed_p
12814 ? glyph_after > glyphs_end
12815 : glyph_after < glyphs_end)
12816 && eabs (glyph_after - glyph_before) == 1))
12817 {
12818 cursor = glyph_after;
12819 x = -1;
12820 }
12821 else if (string_seen)
12822 {
12823 int incr = row->reversed_p ? -1 : +1;
12824
12825 /* Need to find the glyph that came out of a string which is
12826 present at point. That glyph is somewhere between
12827 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12828 positioned between POS_BEFORE and POS_AFTER in the
12829 buffer. */
12830 struct glyph *stop = glyph_after;
12831 EMACS_INT pos = pos_before;
12832
12833 x = -1;
12834 for (glyph = glyph_before + incr;
12835 row->reversed_p ? glyph > stop : glyph < stop; )
12836 {
12837
12838 /* Any glyphs that come from the buffer are here because
12839 of bidi reordering. Skip them, and only pay
12840 attention to glyphs that came from some string. */
12841 if (STRINGP (glyph->object))
12842 {
12843 Lisp_Object str;
12844 EMACS_INT tem;
12845
12846 str = glyph->object;
12847 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12848 if (tem == 0 /* from overlay */
12849 || pos <= tem)
12850 {
12851 /* If the string from which this glyph came is
12852 found in the buffer at point, then we've
12853 found the glyph we've been looking for. If
12854 it comes from an overlay (tem == 0), and it
12855 has the `cursor' property on one of its
12856 glyphs, record that glyph as a candidate for
12857 displaying the cursor. (As in the
12858 unidirectional version, we will display the
12859 cursor on the last candidate we find.) */
12860 if (tem == 0 || tem == pt_old)
12861 {
12862 /* The glyphs from this string could have
12863 been reordered. Find the one with the
12864 smallest string position. Or there could
12865 be a character in the string with the
12866 `cursor' property, which means display
12867 cursor on that character's glyph. */
12868 int strpos = glyph->charpos;
12869
12870 cursor = glyph;
12871 for (glyph += incr;
12872 EQ (glyph->object, str);
12873 glyph += incr)
12874 {
12875 Lisp_Object cprop;
12876 int gpos = glyph->charpos;
12877
12878 cprop = Fget_char_property (make_number (gpos),
12879 Qcursor,
12880 glyph->object);
12881 if (!NILP (cprop))
12882 {
12883 cursor = glyph;
12884 break;
12885 }
12886 if (glyph->charpos < strpos)
12887 {
12888 strpos = glyph->charpos;
12889 cursor = glyph;
12890 }
12891 }
12892
12893 if (tem == pt_old)
12894 goto compute_x;
12895 }
12896 if (tem)
12897 pos = tem + 1; /* don't find previous instances */
12898 }
12899 /* This string is not what we want; skip all of the
12900 glyphs that came from it. */
12901 do
12902 glyph += incr;
12903 while ((row->reversed_p ? glyph > stop : glyph < stop)
12904 && EQ (glyph->object, str));
12905 }
12906 else
12907 glyph += incr;
12908 }
12909
12910 /* If we reached the end of the line, and END was from a string,
12911 the cursor is not on this line. */
12912 if (glyph == end
12913 && STRINGP ((glyph - incr)->object)
12914 && row->continued_p)
12915 return 0;
12916 }
12917 }
12918
12919 compute_x:
12920 if (cursor != NULL)
12921 glyph = cursor;
12922 if (x < 0)
12923 {
12924 struct glyph *g;
12925
12926 /* Need to compute x that corresponds to GLYPH. */
12927 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12928 {
12929 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12930 abort ();
12931 x += g->pixel_width;
12932 }
12933 }
12934
12935 /* ROW could be part of a continued line, which, under bidi
12936 reordering, might have other rows whose start and end charpos
12937 occlude point. Only set w->cursor if we found a better
12938 approximation to the cursor position than we have from previously
12939 examined candidate rows belonging to the same continued line. */
12940 if (/* we already have a candidate row */
12941 w->cursor.vpos >= 0
12942 /* that candidate is not the row we are processing */
12943 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12944 /* the row we are processing is part of a continued line */
12945 && (row->continued_p || row->continuation_lines_width)
12946 /* Make sure cursor.vpos specifies a row whose start and end
12947 charpos occlude point. This is because some callers of this
12948 function leave cursor.vpos at the row where the cursor was
12949 displayed during the last redisplay cycle. */
12950 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12951 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12952 {
12953 struct glyph *g1 =
12954 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12955
12956 /* Don't consider glyphs that are outside TEXT_AREA. */
12957 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12958 return 0;
12959 /* Keep the candidate whose buffer position is the closest to
12960 point. */
12961 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12962 w->cursor.hpos >= 0
12963 && w->cursor.hpos < MATRIX_ROW_USED(matrix, w->cursor.vpos)
12964 && BUFFERP (g1->object)
12965 && (g1->charpos == pt_old /* an exact match always wins */
12966 || (BUFFERP (glyph->object)
12967 && eabs (g1->charpos - pt_old)
12968 < eabs (glyph->charpos - pt_old))))
12969 return 0;
12970 /* If this candidate gives an exact match, use that. */
12971 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12972 /* Otherwise, keep the candidate that comes from a row
12973 spanning less buffer positions. This may win when one or
12974 both candidate positions are on glyphs that came from
12975 display strings, for which we cannot compare buffer
12976 positions. */
12977 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12978 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12979 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12980 return 0;
12981 }
12982 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12983 w->cursor.x = x;
12984 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12985 w->cursor.y = row->y + dy;
12986
12987 if (w == XWINDOW (selected_window))
12988 {
12989 if (!row->continued_p
12990 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12991 && row->x == 0)
12992 {
12993 this_line_buffer = XBUFFER (w->buffer);
12994
12995 CHARPOS (this_line_start_pos)
12996 = MATRIX_ROW_START_CHARPOS (row) + delta;
12997 BYTEPOS (this_line_start_pos)
12998 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12999
13000 CHARPOS (this_line_end_pos)
13001 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13002 BYTEPOS (this_line_end_pos)
13003 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13004
13005 this_line_y = w->cursor.y;
13006 this_line_pixel_height = row->height;
13007 this_line_vpos = w->cursor.vpos;
13008 this_line_start_x = row->x;
13009 }
13010 else
13011 CHARPOS (this_line_start_pos) = 0;
13012 }
13013
13014 return 1;
13015 }
13016
13017
13018 /* Run window scroll functions, if any, for WINDOW with new window
13019 start STARTP. Sets the window start of WINDOW to that position.
13020
13021 We assume that the window's buffer is really current. */
13022
13023 static INLINE struct text_pos
13024 run_window_scroll_functions (window, startp)
13025 Lisp_Object window;
13026 struct text_pos startp;
13027 {
13028 struct window *w = XWINDOW (window);
13029 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13030
13031 if (current_buffer != XBUFFER (w->buffer))
13032 abort ();
13033
13034 if (!NILP (Vwindow_scroll_functions))
13035 {
13036 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13037 make_number (CHARPOS (startp)));
13038 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13039 /* In case the hook functions switch buffers. */
13040 if (current_buffer != XBUFFER (w->buffer))
13041 set_buffer_internal_1 (XBUFFER (w->buffer));
13042 }
13043
13044 return startp;
13045 }
13046
13047
13048 /* Make sure the line containing the cursor is fully visible.
13049 A value of 1 means there is nothing to be done.
13050 (Either the line is fully visible, or it cannot be made so,
13051 or we cannot tell.)
13052
13053 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13054 is higher than window.
13055
13056 A value of 0 means the caller should do scrolling
13057 as if point had gone off the screen. */
13058
13059 static int
13060 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
13061 struct window *w;
13062 int force_p;
13063 int current_matrix_p;
13064 {
13065 struct glyph_matrix *matrix;
13066 struct glyph_row *row;
13067 int window_height;
13068
13069 if (!make_cursor_line_fully_visible_p)
13070 return 1;
13071
13072 /* It's not always possible to find the cursor, e.g, when a window
13073 is full of overlay strings. Don't do anything in that case. */
13074 if (w->cursor.vpos < 0)
13075 return 1;
13076
13077 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13078 row = MATRIX_ROW (matrix, w->cursor.vpos);
13079
13080 /* If the cursor row is not partially visible, there's nothing to do. */
13081 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13082 return 1;
13083
13084 /* If the row the cursor is in is taller than the window's height,
13085 it's not clear what to do, so do nothing. */
13086 window_height = window_box_height (w);
13087 if (row->height >= window_height)
13088 {
13089 if (!force_p || MINI_WINDOW_P (w)
13090 || w->vscroll || w->cursor.vpos == 0)
13091 return 1;
13092 }
13093 return 0;
13094 }
13095
13096
13097 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13098 non-zero means only WINDOW is redisplayed in redisplay_internal.
13099 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13100 in redisplay_window to bring a partially visible line into view in
13101 the case that only the cursor has moved.
13102
13103 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13104 last screen line's vertical height extends past the end of the screen.
13105
13106 Value is
13107
13108 1 if scrolling succeeded
13109
13110 0 if scrolling didn't find point.
13111
13112 -1 if new fonts have been loaded so that we must interrupt
13113 redisplay, adjust glyph matrices, and try again. */
13114
13115 enum
13116 {
13117 SCROLLING_SUCCESS,
13118 SCROLLING_FAILED,
13119 SCROLLING_NEED_LARGER_MATRICES
13120 };
13121
13122 static int
13123 try_scrolling (window, just_this_one_p, scroll_conservatively,
13124 scroll_step, temp_scroll_step, last_line_misfit)
13125 Lisp_Object window;
13126 int just_this_one_p;
13127 EMACS_INT scroll_conservatively, scroll_step;
13128 int temp_scroll_step;
13129 int last_line_misfit;
13130 {
13131 struct window *w = XWINDOW (window);
13132 struct frame *f = XFRAME (w->frame);
13133 struct text_pos pos, startp;
13134 struct it it;
13135 int this_scroll_margin, scroll_max, rc, height;
13136 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13137 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13138 Lisp_Object aggressive;
13139 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13140
13141 #if GLYPH_DEBUG
13142 debug_method_add (w, "try_scrolling");
13143 #endif
13144
13145 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13146
13147 /* Compute scroll margin height in pixels. We scroll when point is
13148 within this distance from the top or bottom of the window. */
13149 if (scroll_margin > 0)
13150 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13151 * FRAME_LINE_HEIGHT (f);
13152 else
13153 this_scroll_margin = 0;
13154
13155 /* Force scroll_conservatively to have a reasonable value, to avoid
13156 overflow while computing how much to scroll. Note that the user
13157 can supply scroll-conservatively equal to `most-positive-fixnum',
13158 which can be larger than INT_MAX. */
13159 if (scroll_conservatively > scroll_limit)
13160 {
13161 scroll_conservatively = scroll_limit;
13162 scroll_max = INT_MAX;
13163 }
13164 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13165 /* Compute how much we should try to scroll maximally to bring
13166 point into view. */
13167 scroll_max = (max (scroll_step,
13168 max (scroll_conservatively, temp_scroll_step))
13169 * FRAME_LINE_HEIGHT (f));
13170 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13171 || NUMBERP (current_buffer->scroll_up_aggressively))
13172 /* We're trying to scroll because of aggressive scrolling but no
13173 scroll_step is set. Choose an arbitrary one. */
13174 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13175 else
13176 scroll_max = 0;
13177
13178 too_near_end:
13179
13180 /* Decide whether to scroll down. */
13181 if (PT > CHARPOS (startp))
13182 {
13183 int scroll_margin_y;
13184
13185 /* Compute the pixel ypos of the scroll margin, then move it to
13186 either that ypos or PT, whichever comes first. */
13187 start_display (&it, w, startp);
13188 scroll_margin_y = it.last_visible_y - this_scroll_margin
13189 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13190 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13191 (MOVE_TO_POS | MOVE_TO_Y));
13192
13193 if (PT > CHARPOS (it.current.pos))
13194 {
13195 int y0 = line_bottom_y (&it);
13196
13197 /* Compute the distance from the scroll margin to PT
13198 (including the height of the cursor line). Moving the
13199 iterator unconditionally to PT can be slow if PT is far
13200 away, so stop 10 lines past the window bottom (is there a
13201 way to do the right thing quickly?). */
13202 move_it_to (&it, PT, -1,
13203 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
13204 -1, MOVE_TO_POS | MOVE_TO_Y);
13205 dy = line_bottom_y (&it) - y0;
13206
13207 if (dy > scroll_max)
13208 return SCROLLING_FAILED;
13209
13210 scroll_down_p = 1;
13211 }
13212 }
13213
13214 if (scroll_down_p)
13215 {
13216 /* Point is in or below the bottom scroll margin, so move the
13217 window start down. If scrolling conservatively, move it just
13218 enough down to make point visible. If scroll_step is set,
13219 move it down by scroll_step. */
13220 if (scroll_conservatively)
13221 amount_to_scroll
13222 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13223 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13224 else if (scroll_step || temp_scroll_step)
13225 amount_to_scroll = scroll_max;
13226 else
13227 {
13228 aggressive = current_buffer->scroll_up_aggressively;
13229 height = WINDOW_BOX_TEXT_HEIGHT (w);
13230 if (NUMBERP (aggressive))
13231 {
13232 double float_amount = XFLOATINT (aggressive) * height;
13233 amount_to_scroll = float_amount;
13234 if (amount_to_scroll == 0 && float_amount > 0)
13235 amount_to_scroll = 1;
13236 }
13237 }
13238
13239 if (amount_to_scroll <= 0)
13240 return SCROLLING_FAILED;
13241
13242 start_display (&it, w, startp);
13243 move_it_vertically (&it, amount_to_scroll);
13244
13245 /* If STARTP is unchanged, move it down another screen line. */
13246 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13247 move_it_by_lines (&it, 1, 1);
13248 startp = it.current.pos;
13249 }
13250 else
13251 {
13252 struct text_pos scroll_margin_pos = startp;
13253
13254 /* See if point is inside the scroll margin at the top of the
13255 window. */
13256 if (this_scroll_margin)
13257 {
13258 start_display (&it, w, startp);
13259 move_it_vertically (&it, this_scroll_margin);
13260 scroll_margin_pos = it.current.pos;
13261 }
13262
13263 if (PT < CHARPOS (scroll_margin_pos))
13264 {
13265 /* Point is in the scroll margin at the top of the window or
13266 above what is displayed in the window. */
13267 int y0;
13268
13269 /* Compute the vertical distance from PT to the scroll
13270 margin position. Give up if distance is greater than
13271 scroll_max. */
13272 SET_TEXT_POS (pos, PT, PT_BYTE);
13273 start_display (&it, w, pos);
13274 y0 = it.current_y;
13275 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13276 it.last_visible_y, -1,
13277 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13278 dy = it.current_y - y0;
13279 if (dy > scroll_max)
13280 return SCROLLING_FAILED;
13281
13282 /* Compute new window start. */
13283 start_display (&it, w, startp);
13284
13285 if (scroll_conservatively)
13286 amount_to_scroll
13287 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13288 else if (scroll_step || temp_scroll_step)
13289 amount_to_scroll = scroll_max;
13290 else
13291 {
13292 aggressive = current_buffer->scroll_down_aggressively;
13293 height = WINDOW_BOX_TEXT_HEIGHT (w);
13294 if (NUMBERP (aggressive))
13295 {
13296 double float_amount = XFLOATINT (aggressive) * height;
13297 amount_to_scroll = float_amount;
13298 if (amount_to_scroll == 0 && float_amount > 0)
13299 amount_to_scroll = 1;
13300 }
13301 }
13302
13303 if (amount_to_scroll <= 0)
13304 return SCROLLING_FAILED;
13305
13306 move_it_vertically_backward (&it, amount_to_scroll);
13307 startp = it.current.pos;
13308 }
13309 }
13310
13311 /* Run window scroll functions. */
13312 startp = run_window_scroll_functions (window, startp);
13313
13314 /* Display the window. Give up if new fonts are loaded, or if point
13315 doesn't appear. */
13316 if (!try_window (window, startp, 0))
13317 rc = SCROLLING_NEED_LARGER_MATRICES;
13318 else if (w->cursor.vpos < 0)
13319 {
13320 clear_glyph_matrix (w->desired_matrix);
13321 rc = SCROLLING_FAILED;
13322 }
13323 else
13324 {
13325 /* Maybe forget recorded base line for line number display. */
13326 if (!just_this_one_p
13327 || current_buffer->clip_changed
13328 || BEG_UNCHANGED < CHARPOS (startp))
13329 w->base_line_number = Qnil;
13330
13331 /* If cursor ends up on a partially visible line,
13332 treat that as being off the bottom of the screen. */
13333 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13334 {
13335 clear_glyph_matrix (w->desired_matrix);
13336 ++extra_scroll_margin_lines;
13337 goto too_near_end;
13338 }
13339 rc = SCROLLING_SUCCESS;
13340 }
13341
13342 return rc;
13343 }
13344
13345
13346 /* Compute a suitable window start for window W if display of W starts
13347 on a continuation line. Value is non-zero if a new window start
13348 was computed.
13349
13350 The new window start will be computed, based on W's width, starting
13351 from the start of the continued line. It is the start of the
13352 screen line with the minimum distance from the old start W->start. */
13353
13354 static int
13355 compute_window_start_on_continuation_line (w)
13356 struct window *w;
13357 {
13358 struct text_pos pos, start_pos;
13359 int window_start_changed_p = 0;
13360
13361 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13362
13363 /* If window start is on a continuation line... Window start may be
13364 < BEGV in case there's invisible text at the start of the
13365 buffer (M-x rmail, for example). */
13366 if (CHARPOS (start_pos) > BEGV
13367 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13368 {
13369 struct it it;
13370 struct glyph_row *row;
13371
13372 /* Handle the case that the window start is out of range. */
13373 if (CHARPOS (start_pos) < BEGV)
13374 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13375 else if (CHARPOS (start_pos) > ZV)
13376 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13377
13378 /* Find the start of the continued line. This should be fast
13379 because scan_buffer is fast (newline cache). */
13380 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13381 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13382 row, DEFAULT_FACE_ID);
13383 reseat_at_previous_visible_line_start (&it);
13384
13385 /* If the line start is "too far" away from the window start,
13386 say it takes too much time to compute a new window start. */
13387 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13388 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13389 {
13390 int min_distance, distance;
13391
13392 /* Move forward by display lines to find the new window
13393 start. If window width was enlarged, the new start can
13394 be expected to be > the old start. If window width was
13395 decreased, the new window start will be < the old start.
13396 So, we're looking for the display line start with the
13397 minimum distance from the old window start. */
13398 pos = it.current.pos;
13399 min_distance = INFINITY;
13400 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13401 distance < min_distance)
13402 {
13403 min_distance = distance;
13404 pos = it.current.pos;
13405 move_it_by_lines (&it, 1, 0);
13406 }
13407
13408 /* Set the window start there. */
13409 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13410 window_start_changed_p = 1;
13411 }
13412 }
13413
13414 return window_start_changed_p;
13415 }
13416
13417
13418 /* Try cursor movement in case text has not changed in window WINDOW,
13419 with window start STARTP. Value is
13420
13421 CURSOR_MOVEMENT_SUCCESS if successful
13422
13423 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13424
13425 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13426 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13427 we want to scroll as if scroll-step were set to 1. See the code.
13428
13429 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13430 which case we have to abort this redisplay, and adjust matrices
13431 first. */
13432
13433 enum
13434 {
13435 CURSOR_MOVEMENT_SUCCESS,
13436 CURSOR_MOVEMENT_CANNOT_BE_USED,
13437 CURSOR_MOVEMENT_MUST_SCROLL,
13438 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13439 };
13440
13441 static int
13442 try_cursor_movement (window, startp, scroll_step)
13443 Lisp_Object window;
13444 struct text_pos startp;
13445 int *scroll_step;
13446 {
13447 struct window *w = XWINDOW (window);
13448 struct frame *f = XFRAME (w->frame);
13449 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13450
13451 #if GLYPH_DEBUG
13452 if (inhibit_try_cursor_movement)
13453 return rc;
13454 #endif
13455
13456 /* Handle case where text has not changed, only point, and it has
13457 not moved off the frame. */
13458 if (/* Point may be in this window. */
13459 PT >= CHARPOS (startp)
13460 /* Selective display hasn't changed. */
13461 && !current_buffer->clip_changed
13462 /* Function force-mode-line-update is used to force a thorough
13463 redisplay. It sets either windows_or_buffers_changed or
13464 update_mode_lines. So don't take a shortcut here for these
13465 cases. */
13466 && !update_mode_lines
13467 && !windows_or_buffers_changed
13468 && !cursor_type_changed
13469 /* Can't use this case if highlighting a region. When a
13470 region exists, cursor movement has to do more than just
13471 set the cursor. */
13472 && !(!NILP (Vtransient_mark_mode)
13473 && !NILP (current_buffer->mark_active))
13474 && NILP (w->region_showing)
13475 && NILP (Vshow_trailing_whitespace)
13476 /* Right after splitting windows, last_point may be nil. */
13477 && INTEGERP (w->last_point)
13478 /* This code is not used for mini-buffer for the sake of the case
13479 of redisplaying to replace an echo area message; since in
13480 that case the mini-buffer contents per se are usually
13481 unchanged. This code is of no real use in the mini-buffer
13482 since the handling of this_line_start_pos, etc., in redisplay
13483 handles the same cases. */
13484 && !EQ (window, minibuf_window)
13485 /* When splitting windows or for new windows, it happens that
13486 redisplay is called with a nil window_end_vpos or one being
13487 larger than the window. This should really be fixed in
13488 window.c. I don't have this on my list, now, so we do
13489 approximately the same as the old redisplay code. --gerd. */
13490 && INTEGERP (w->window_end_vpos)
13491 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13492 && (FRAME_WINDOW_P (f)
13493 || !overlay_arrow_in_current_buffer_p ()))
13494 {
13495 int this_scroll_margin, top_scroll_margin;
13496 struct glyph_row *row = NULL;
13497
13498 #if GLYPH_DEBUG
13499 debug_method_add (w, "cursor movement");
13500 #endif
13501
13502 /* Scroll if point within this distance from the top or bottom
13503 of the window. This is a pixel value. */
13504 if (scroll_margin > 0)
13505 {
13506 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13507 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13508 }
13509 else
13510 this_scroll_margin = 0;
13511
13512 top_scroll_margin = this_scroll_margin;
13513 if (WINDOW_WANTS_HEADER_LINE_P (w))
13514 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13515
13516 /* Start with the row the cursor was displayed during the last
13517 not paused redisplay. Give up if that row is not valid. */
13518 if (w->last_cursor.vpos < 0
13519 || w->last_cursor.vpos >= w->current_matrix->nrows)
13520 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13521 else
13522 {
13523 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13524 if (row->mode_line_p)
13525 ++row;
13526 if (!row->enabled_p)
13527 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13528 /* If rows are bidi-reordered, back up until we find a row
13529 that does not belong to a continuation line. This is
13530 because we must consider all rows of a continued line as
13531 candidates for cursor positioning, since row start and
13532 end positions change non-linearly with vertical position
13533 in such rows. */
13534 /* FIXME: Revisit this when glyph ``spilling'' in
13535 continuation lines' rows is implemented for
13536 bidi-reordered rows. */
13537 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13538 {
13539 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13540 {
13541 xassert (row->enabled_p);
13542 --row;
13543 /* If we hit the beginning of the displayed portion
13544 without finding the first row of a continued
13545 line, give up. */
13546 if (row <= w->current_matrix->rows)
13547 {
13548 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13549 break;
13550 }
13551
13552 }
13553 }
13554 }
13555
13556 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13557 {
13558 int scroll_p = 0;
13559 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13560
13561 if (PT > XFASTINT (w->last_point))
13562 {
13563 /* Point has moved forward. */
13564 while (MATRIX_ROW_END_CHARPOS (row) < PT
13565 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13566 {
13567 xassert (row->enabled_p);
13568 ++row;
13569 }
13570
13571 /* The end position of a row equals the start position
13572 of the next row. If PT is there, we would rather
13573 display it in the next line. */
13574 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13575 && MATRIX_ROW_END_CHARPOS (row) == PT
13576 && !cursor_row_p (w, row))
13577 ++row;
13578
13579 /* If within the scroll margin, scroll. Note that
13580 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13581 the next line would be drawn, and that
13582 this_scroll_margin can be zero. */
13583 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13584 || PT > MATRIX_ROW_END_CHARPOS (row)
13585 /* Line is completely visible last line in window
13586 and PT is to be set in the next line. */
13587 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13588 && PT == MATRIX_ROW_END_CHARPOS (row)
13589 && !row->ends_at_zv_p
13590 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13591 scroll_p = 1;
13592 }
13593 else if (PT < XFASTINT (w->last_point))
13594 {
13595 /* Cursor has to be moved backward. Note that PT >=
13596 CHARPOS (startp) because of the outer if-statement. */
13597 while (!row->mode_line_p
13598 && (MATRIX_ROW_START_CHARPOS (row) > PT
13599 || (MATRIX_ROW_START_CHARPOS (row) == PT
13600 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13601 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13602 row > w->current_matrix->rows
13603 && (row-1)->ends_in_newline_from_string_p))))
13604 && (row->y > top_scroll_margin
13605 || CHARPOS (startp) == BEGV))
13606 {
13607 xassert (row->enabled_p);
13608 --row;
13609 }
13610
13611 /* Consider the following case: Window starts at BEGV,
13612 there is invisible, intangible text at BEGV, so that
13613 display starts at some point START > BEGV. It can
13614 happen that we are called with PT somewhere between
13615 BEGV and START. Try to handle that case. */
13616 if (row < w->current_matrix->rows
13617 || row->mode_line_p)
13618 {
13619 row = w->current_matrix->rows;
13620 if (row->mode_line_p)
13621 ++row;
13622 }
13623
13624 /* Due to newlines in overlay strings, we may have to
13625 skip forward over overlay strings. */
13626 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13627 && MATRIX_ROW_END_CHARPOS (row) == PT
13628 && !cursor_row_p (w, row))
13629 ++row;
13630
13631 /* If within the scroll margin, scroll. */
13632 if (row->y < top_scroll_margin
13633 && CHARPOS (startp) != BEGV)
13634 scroll_p = 1;
13635 }
13636 else
13637 {
13638 /* Cursor did not move. So don't scroll even if cursor line
13639 is partially visible, as it was so before. */
13640 rc = CURSOR_MOVEMENT_SUCCESS;
13641 }
13642
13643 if (PT < MATRIX_ROW_START_CHARPOS (row)
13644 || PT > MATRIX_ROW_END_CHARPOS (row))
13645 {
13646 /* if PT is not in the glyph row, give up. */
13647 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13648 }
13649 else if (rc != CURSOR_MOVEMENT_SUCCESS
13650 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13651 && make_cursor_line_fully_visible_p)
13652 {
13653 if (PT == MATRIX_ROW_END_CHARPOS (row)
13654 && !row->ends_at_zv_p
13655 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13656 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13657 else if (row->height > window_box_height (w))
13658 {
13659 /* If we end up in a partially visible line, let's
13660 make it fully visible, except when it's taller
13661 than the window, in which case we can't do much
13662 about it. */
13663 *scroll_step = 1;
13664 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13665 }
13666 else
13667 {
13668 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13669 if (!cursor_row_fully_visible_p (w, 0, 1))
13670 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13671 else
13672 rc = CURSOR_MOVEMENT_SUCCESS;
13673 }
13674 }
13675 else if (scroll_p)
13676 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13677 else if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13678 {
13679 /* With bidi-reordered rows, there could be more than
13680 one candidate row whose start and end positions
13681 occlude point. We need to let set_cursor_from_row
13682 find the best candidate. */
13683 /* FIXME: Revisit this when glyph ``spilling'' in
13684 continuation lines' rows is implemented for
13685 bidi-reordered rows. */
13686 int rv = 0;
13687
13688 do
13689 {
13690 rv |= set_cursor_from_row (w, row, w->current_matrix,
13691 0, 0, 0, 0);
13692 /* As soon as we've found the first suitable row
13693 whose ends_at_zv_p flag is set, we are done. */
13694 if (rv
13695 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13696 {
13697 rc = CURSOR_MOVEMENT_SUCCESS;
13698 break;
13699 }
13700 ++row;
13701 }
13702 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13703 && MATRIX_ROW_START_CHARPOS (row) <= PT
13704 && PT <= MATRIX_ROW_END_CHARPOS (row)
13705 && cursor_row_p (w, row));
13706 /* If we didn't find any candidate rows, or exited the
13707 loop before all the candidates were examined, signal
13708 to the caller that this method failed. */
13709 if (rc != CURSOR_MOVEMENT_SUCCESS
13710 && (!rv
13711 || (MATRIX_ROW_START_CHARPOS (row) <= PT
13712 && PT <= MATRIX_ROW_END_CHARPOS (row))))
13713 rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13714 else
13715 rc = CURSOR_MOVEMENT_SUCCESS;
13716 }
13717 else
13718 {
13719 do
13720 {
13721 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13722 {
13723 rc = CURSOR_MOVEMENT_SUCCESS;
13724 break;
13725 }
13726 ++row;
13727 }
13728 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13729 && MATRIX_ROW_START_CHARPOS (row) == PT
13730 && cursor_row_p (w, row));
13731 }
13732 }
13733 }
13734
13735 return rc;
13736 }
13737
13738 void
13739 set_vertical_scroll_bar (w)
13740 struct window *w;
13741 {
13742 int start, end, whole;
13743
13744 /* Calculate the start and end positions for the current window.
13745 At some point, it would be nice to choose between scrollbars
13746 which reflect the whole buffer size, with special markers
13747 indicating narrowing, and scrollbars which reflect only the
13748 visible region.
13749
13750 Note that mini-buffers sometimes aren't displaying any text. */
13751 if (!MINI_WINDOW_P (w)
13752 || (w == XWINDOW (minibuf_window)
13753 && NILP (echo_area_buffer[0])))
13754 {
13755 struct buffer *buf = XBUFFER (w->buffer);
13756 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13757 start = marker_position (w->start) - BUF_BEGV (buf);
13758 /* I don't think this is guaranteed to be right. For the
13759 moment, we'll pretend it is. */
13760 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13761
13762 if (end < start)
13763 end = start;
13764 if (whole < (end - start))
13765 whole = end - start;
13766 }
13767 else
13768 start = end = whole = 0;
13769
13770 /* Indicate what this scroll bar ought to be displaying now. */
13771 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13772 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13773 (w, end - start, whole, start);
13774 }
13775
13776
13777 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13778 selected_window is redisplayed.
13779
13780 We can return without actually redisplaying the window if
13781 fonts_changed_p is nonzero. In that case, redisplay_internal will
13782 retry. */
13783
13784 static void
13785 redisplay_window (window, just_this_one_p)
13786 Lisp_Object window;
13787 int just_this_one_p;
13788 {
13789 struct window *w = XWINDOW (window);
13790 struct frame *f = XFRAME (w->frame);
13791 struct buffer *buffer = XBUFFER (w->buffer);
13792 struct buffer *old = current_buffer;
13793 struct text_pos lpoint, opoint, startp;
13794 int update_mode_line;
13795 int tem;
13796 struct it it;
13797 /* Record it now because it's overwritten. */
13798 int current_matrix_up_to_date_p = 0;
13799 int used_current_matrix_p = 0;
13800 /* This is less strict than current_matrix_up_to_date_p.
13801 It indictes that the buffer contents and narrowing are unchanged. */
13802 int buffer_unchanged_p = 0;
13803 int temp_scroll_step = 0;
13804 int count = SPECPDL_INDEX ();
13805 int rc;
13806 int centering_position = -1;
13807 int last_line_misfit = 0;
13808 int beg_unchanged, end_unchanged;
13809
13810 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13811 opoint = lpoint;
13812
13813 /* W must be a leaf window here. */
13814 xassert (!NILP (w->buffer));
13815 #if GLYPH_DEBUG
13816 *w->desired_matrix->method = 0;
13817 #endif
13818
13819 restart:
13820 reconsider_clip_changes (w, buffer);
13821
13822 /* Has the mode line to be updated? */
13823 update_mode_line = (!NILP (w->update_mode_line)
13824 || update_mode_lines
13825 || buffer->clip_changed
13826 || buffer->prevent_redisplay_optimizations_p);
13827
13828 if (MINI_WINDOW_P (w))
13829 {
13830 if (w == XWINDOW (echo_area_window)
13831 && !NILP (echo_area_buffer[0]))
13832 {
13833 if (update_mode_line)
13834 /* We may have to update a tty frame's menu bar or a
13835 tool-bar. Example `M-x C-h C-h C-g'. */
13836 goto finish_menu_bars;
13837 else
13838 /* We've already displayed the echo area glyphs in this window. */
13839 goto finish_scroll_bars;
13840 }
13841 else if ((w != XWINDOW (minibuf_window)
13842 || minibuf_level == 0)
13843 /* When buffer is nonempty, redisplay window normally. */
13844 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13845 /* Quail displays non-mini buffers in minibuffer window.
13846 In that case, redisplay the window normally. */
13847 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13848 {
13849 /* W is a mini-buffer window, but it's not active, so clear
13850 it. */
13851 int yb = window_text_bottom_y (w);
13852 struct glyph_row *row;
13853 int y;
13854
13855 for (y = 0, row = w->desired_matrix->rows;
13856 y < yb;
13857 y += row->height, ++row)
13858 blank_row (w, row, y);
13859 goto finish_scroll_bars;
13860 }
13861
13862 clear_glyph_matrix (w->desired_matrix);
13863 }
13864
13865 /* Otherwise set up data on this window; select its buffer and point
13866 value. */
13867 /* Really select the buffer, for the sake of buffer-local
13868 variables. */
13869 set_buffer_internal_1 (XBUFFER (w->buffer));
13870
13871 current_matrix_up_to_date_p
13872 = (!NILP (w->window_end_valid)
13873 && !current_buffer->clip_changed
13874 && !current_buffer->prevent_redisplay_optimizations_p
13875 && XFASTINT (w->last_modified) >= MODIFF
13876 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13877
13878 /* Run the window-bottom-change-functions
13879 if it is possible that the text on the screen has changed
13880 (either due to modification of the text, or any other reason). */
13881 if (!current_matrix_up_to_date_p
13882 && !NILP (Vwindow_text_change_functions))
13883 {
13884 safe_run_hooks (Qwindow_text_change_functions);
13885 goto restart;
13886 }
13887
13888 beg_unchanged = BEG_UNCHANGED;
13889 end_unchanged = END_UNCHANGED;
13890
13891 SET_TEXT_POS (opoint, PT, PT_BYTE);
13892
13893 specbind (Qinhibit_point_motion_hooks, Qt);
13894
13895 buffer_unchanged_p
13896 = (!NILP (w->window_end_valid)
13897 && !current_buffer->clip_changed
13898 && XFASTINT (w->last_modified) >= MODIFF
13899 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13900
13901 /* When windows_or_buffers_changed is non-zero, we can't rely on
13902 the window end being valid, so set it to nil there. */
13903 if (windows_or_buffers_changed)
13904 {
13905 /* If window starts on a continuation line, maybe adjust the
13906 window start in case the window's width changed. */
13907 if (XMARKER (w->start)->buffer == current_buffer)
13908 compute_window_start_on_continuation_line (w);
13909
13910 w->window_end_valid = Qnil;
13911 }
13912
13913 /* Some sanity checks. */
13914 CHECK_WINDOW_END (w);
13915 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13916 abort ();
13917 if (BYTEPOS (opoint) < CHARPOS (opoint))
13918 abort ();
13919
13920 /* If %c is in mode line, update it if needed. */
13921 if (!NILP (w->column_number_displayed)
13922 /* This alternative quickly identifies a common case
13923 where no change is needed. */
13924 && !(PT == XFASTINT (w->last_point)
13925 && XFASTINT (w->last_modified) >= MODIFF
13926 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13927 && (XFASTINT (w->column_number_displayed)
13928 != (int) current_column ())) /* iftc */
13929 update_mode_line = 1;
13930
13931 /* Count number of windows showing the selected buffer. An indirect
13932 buffer counts as its base buffer. */
13933 if (!just_this_one_p)
13934 {
13935 struct buffer *current_base, *window_base;
13936 current_base = current_buffer;
13937 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13938 if (current_base->base_buffer)
13939 current_base = current_base->base_buffer;
13940 if (window_base->base_buffer)
13941 window_base = window_base->base_buffer;
13942 if (current_base == window_base)
13943 buffer_shared++;
13944 }
13945
13946 /* Point refers normally to the selected window. For any other
13947 window, set up appropriate value. */
13948 if (!EQ (window, selected_window))
13949 {
13950 int new_pt = XMARKER (w->pointm)->charpos;
13951 int new_pt_byte = marker_byte_position (w->pointm);
13952 if (new_pt < BEGV)
13953 {
13954 new_pt = BEGV;
13955 new_pt_byte = BEGV_BYTE;
13956 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13957 }
13958 else if (new_pt > (ZV - 1))
13959 {
13960 new_pt = ZV;
13961 new_pt_byte = ZV_BYTE;
13962 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13963 }
13964
13965 /* We don't use SET_PT so that the point-motion hooks don't run. */
13966 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13967 }
13968
13969 /* If any of the character widths specified in the display table
13970 have changed, invalidate the width run cache. It's true that
13971 this may be a bit late to catch such changes, but the rest of
13972 redisplay goes (non-fatally) haywire when the display table is
13973 changed, so why should we worry about doing any better? */
13974 if (current_buffer->width_run_cache)
13975 {
13976 struct Lisp_Char_Table *disptab = buffer_display_table ();
13977
13978 if (! disptab_matches_widthtab (disptab,
13979 XVECTOR (current_buffer->width_table)))
13980 {
13981 invalidate_region_cache (current_buffer,
13982 current_buffer->width_run_cache,
13983 BEG, Z);
13984 recompute_width_table (current_buffer, disptab);
13985 }
13986 }
13987
13988 /* If window-start is screwed up, choose a new one. */
13989 if (XMARKER (w->start)->buffer != current_buffer)
13990 goto recenter;
13991
13992 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13993
13994 /* If someone specified a new starting point but did not insist,
13995 check whether it can be used. */
13996 if (!NILP (w->optional_new_start)
13997 && CHARPOS (startp) >= BEGV
13998 && CHARPOS (startp) <= ZV)
13999 {
14000 w->optional_new_start = Qnil;
14001 start_display (&it, w, startp);
14002 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14003 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14004 if (IT_CHARPOS (it) == PT)
14005 w->force_start = Qt;
14006 /* IT may overshoot PT if text at PT is invisible. */
14007 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14008 w->force_start = Qt;
14009 }
14010
14011 force_start:
14012
14013 /* Handle case where place to start displaying has been specified,
14014 unless the specified location is outside the accessible range. */
14015 if (!NILP (w->force_start)
14016 || w->frozen_window_start_p)
14017 {
14018 /* We set this later on if we have to adjust point. */
14019 int new_vpos = -1;
14020
14021 w->force_start = Qnil;
14022 w->vscroll = 0;
14023 w->window_end_valid = Qnil;
14024
14025 /* Forget any recorded base line for line number display. */
14026 if (!buffer_unchanged_p)
14027 w->base_line_number = Qnil;
14028
14029 /* Redisplay the mode line. Select the buffer properly for that.
14030 Also, run the hook window-scroll-functions
14031 because we have scrolled. */
14032 /* Note, we do this after clearing force_start because
14033 if there's an error, it is better to forget about force_start
14034 than to get into an infinite loop calling the hook functions
14035 and having them get more errors. */
14036 if (!update_mode_line
14037 || ! NILP (Vwindow_scroll_functions))
14038 {
14039 update_mode_line = 1;
14040 w->update_mode_line = Qt;
14041 startp = run_window_scroll_functions (window, startp);
14042 }
14043
14044 w->last_modified = make_number (0);
14045 w->last_overlay_modified = make_number (0);
14046 if (CHARPOS (startp) < BEGV)
14047 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14048 else if (CHARPOS (startp) > ZV)
14049 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14050
14051 /* Redisplay, then check if cursor has been set during the
14052 redisplay. Give up if new fonts were loaded. */
14053 /* We used to issue a CHECK_MARGINS argument to try_window here,
14054 but this causes scrolling to fail when point begins inside
14055 the scroll margin (bug#148) -- cyd */
14056 if (!try_window (window, startp, 0))
14057 {
14058 w->force_start = Qt;
14059 clear_glyph_matrix (w->desired_matrix);
14060 goto need_larger_matrices;
14061 }
14062
14063 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14064 {
14065 /* If point does not appear, try to move point so it does
14066 appear. The desired matrix has been built above, so we
14067 can use it here. */
14068 new_vpos = window_box_height (w) / 2;
14069 }
14070
14071 if (!cursor_row_fully_visible_p (w, 0, 0))
14072 {
14073 /* Point does appear, but on a line partly visible at end of window.
14074 Move it back to a fully-visible line. */
14075 new_vpos = window_box_height (w);
14076 }
14077
14078 /* If we need to move point for either of the above reasons,
14079 now actually do it. */
14080 if (new_vpos >= 0)
14081 {
14082 struct glyph_row *row;
14083
14084 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14085 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14086 ++row;
14087
14088 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14089 MATRIX_ROW_START_BYTEPOS (row));
14090
14091 if (w != XWINDOW (selected_window))
14092 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14093 else if (current_buffer == old)
14094 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14095
14096 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14097
14098 /* If we are highlighting the region, then we just changed
14099 the region, so redisplay to show it. */
14100 if (!NILP (Vtransient_mark_mode)
14101 && !NILP (current_buffer->mark_active))
14102 {
14103 clear_glyph_matrix (w->desired_matrix);
14104 if (!try_window (window, startp, 0))
14105 goto need_larger_matrices;
14106 }
14107 }
14108
14109 #if GLYPH_DEBUG
14110 debug_method_add (w, "forced window start");
14111 #endif
14112 goto done;
14113 }
14114
14115 /* Handle case where text has not changed, only point, and it has
14116 not moved off the frame, and we are not retrying after hscroll.
14117 (current_matrix_up_to_date_p is nonzero when retrying.) */
14118 if (current_matrix_up_to_date_p
14119 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14120 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14121 {
14122 switch (rc)
14123 {
14124 case CURSOR_MOVEMENT_SUCCESS:
14125 used_current_matrix_p = 1;
14126 goto done;
14127
14128 case CURSOR_MOVEMENT_MUST_SCROLL:
14129 goto try_to_scroll;
14130
14131 default:
14132 abort ();
14133 }
14134 }
14135 /* If current starting point was originally the beginning of a line
14136 but no longer is, find a new starting point. */
14137 else if (!NILP (w->start_at_line_beg)
14138 && !(CHARPOS (startp) <= BEGV
14139 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14140 {
14141 #if GLYPH_DEBUG
14142 debug_method_add (w, "recenter 1");
14143 #endif
14144 goto recenter;
14145 }
14146
14147 /* Try scrolling with try_window_id. Value is > 0 if update has
14148 been done, it is -1 if we know that the same window start will
14149 not work. It is 0 if unsuccessful for some other reason. */
14150 else if ((tem = try_window_id (w)) != 0)
14151 {
14152 #if GLYPH_DEBUG
14153 debug_method_add (w, "try_window_id %d", tem);
14154 #endif
14155
14156 if (fonts_changed_p)
14157 goto need_larger_matrices;
14158 if (tem > 0)
14159 goto done;
14160
14161 /* Otherwise try_window_id has returned -1 which means that we
14162 don't want the alternative below this comment to execute. */
14163 }
14164 else if (CHARPOS (startp) >= BEGV
14165 && CHARPOS (startp) <= ZV
14166 && PT >= CHARPOS (startp)
14167 && (CHARPOS (startp) < ZV
14168 /* Avoid starting at end of buffer. */
14169 || CHARPOS (startp) == BEGV
14170 || (XFASTINT (w->last_modified) >= MODIFF
14171 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14172 {
14173
14174 /* If first window line is a continuation line, and window start
14175 is inside the modified region, but the first change is before
14176 current window start, we must select a new window start.
14177
14178 However, if this is the result of a down-mouse event (e.g. by
14179 extending the mouse-drag-overlay), we don't want to select a
14180 new window start, since that would change the position under
14181 the mouse, resulting in an unwanted mouse-movement rather
14182 than a simple mouse-click. */
14183 if (NILP (w->start_at_line_beg)
14184 && NILP (do_mouse_tracking)
14185 && CHARPOS (startp) > BEGV
14186 && CHARPOS (startp) > BEG + beg_unchanged
14187 && CHARPOS (startp) <= Z - end_unchanged
14188 /* Even if w->start_at_line_beg is nil, a new window may
14189 start at a line_beg, since that's how set_buffer_window
14190 sets it. So, we need to check the return value of
14191 compute_window_start_on_continuation_line. (See also
14192 bug#197). */
14193 && XMARKER (w->start)->buffer == current_buffer
14194 && compute_window_start_on_continuation_line (w))
14195 {
14196 w->force_start = Qt;
14197 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14198 goto force_start;
14199 }
14200
14201 #if GLYPH_DEBUG
14202 debug_method_add (w, "same window start");
14203 #endif
14204
14205 /* Try to redisplay starting at same place as before.
14206 If point has not moved off frame, accept the results. */
14207 if (!current_matrix_up_to_date_p
14208 /* Don't use try_window_reusing_current_matrix in this case
14209 because a window scroll function can have changed the
14210 buffer. */
14211 || !NILP (Vwindow_scroll_functions)
14212 || MINI_WINDOW_P (w)
14213 || !(used_current_matrix_p
14214 = try_window_reusing_current_matrix (w)))
14215 {
14216 IF_DEBUG (debug_method_add (w, "1"));
14217 if (try_window (window, startp, 1) < 0)
14218 /* -1 means we need to scroll.
14219 0 means we need new matrices, but fonts_changed_p
14220 is set in that case, so we will detect it below. */
14221 goto try_to_scroll;
14222 }
14223
14224 if (fonts_changed_p)
14225 goto need_larger_matrices;
14226
14227 if (w->cursor.vpos >= 0)
14228 {
14229 if (!just_this_one_p
14230 || current_buffer->clip_changed
14231 || BEG_UNCHANGED < CHARPOS (startp))
14232 /* Forget any recorded base line for line number display. */
14233 w->base_line_number = Qnil;
14234
14235 if (!cursor_row_fully_visible_p (w, 1, 0))
14236 {
14237 clear_glyph_matrix (w->desired_matrix);
14238 last_line_misfit = 1;
14239 }
14240 /* Drop through and scroll. */
14241 else
14242 goto done;
14243 }
14244 else
14245 clear_glyph_matrix (w->desired_matrix);
14246 }
14247
14248 try_to_scroll:
14249
14250 w->last_modified = make_number (0);
14251 w->last_overlay_modified = make_number (0);
14252
14253 /* Redisplay the mode line. Select the buffer properly for that. */
14254 if (!update_mode_line)
14255 {
14256 update_mode_line = 1;
14257 w->update_mode_line = Qt;
14258 }
14259
14260 /* Try to scroll by specified few lines. */
14261 if ((scroll_conservatively
14262 || scroll_step
14263 || temp_scroll_step
14264 || NUMBERP (current_buffer->scroll_up_aggressively)
14265 || NUMBERP (current_buffer->scroll_down_aggressively))
14266 && !current_buffer->clip_changed
14267 && CHARPOS (startp) >= BEGV
14268 && CHARPOS (startp) <= ZV)
14269 {
14270 /* The function returns -1 if new fonts were loaded, 1 if
14271 successful, 0 if not successful. */
14272 int rc = try_scrolling (window, just_this_one_p,
14273 scroll_conservatively,
14274 scroll_step,
14275 temp_scroll_step, last_line_misfit);
14276 switch (rc)
14277 {
14278 case SCROLLING_SUCCESS:
14279 goto done;
14280
14281 case SCROLLING_NEED_LARGER_MATRICES:
14282 goto need_larger_matrices;
14283
14284 case SCROLLING_FAILED:
14285 break;
14286
14287 default:
14288 abort ();
14289 }
14290 }
14291
14292 /* Finally, just choose place to start which centers point */
14293
14294 recenter:
14295 if (centering_position < 0)
14296 centering_position = window_box_height (w) / 2;
14297
14298 #if GLYPH_DEBUG
14299 debug_method_add (w, "recenter");
14300 #endif
14301
14302 /* w->vscroll = 0; */
14303
14304 /* Forget any previously recorded base line for line number display. */
14305 if (!buffer_unchanged_p)
14306 w->base_line_number = Qnil;
14307
14308 /* Move backward half the height of the window. */
14309 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14310 it.current_y = it.last_visible_y;
14311 move_it_vertically_backward (&it, centering_position);
14312 xassert (IT_CHARPOS (it) >= BEGV);
14313
14314 /* The function move_it_vertically_backward may move over more
14315 than the specified y-distance. If it->w is small, e.g. a
14316 mini-buffer window, we may end up in front of the window's
14317 display area. Start displaying at the start of the line
14318 containing PT in this case. */
14319 if (it.current_y <= 0)
14320 {
14321 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14322 move_it_vertically_backward (&it, 0);
14323 it.current_y = 0;
14324 }
14325
14326 it.current_x = it.hpos = 0;
14327
14328 /* Set startp here explicitly in case that helps avoid an infinite loop
14329 in case the window-scroll-functions functions get errors. */
14330 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14331
14332 /* Run scroll hooks. */
14333 startp = run_window_scroll_functions (window, it.current.pos);
14334
14335 /* Redisplay the window. */
14336 if (!current_matrix_up_to_date_p
14337 || windows_or_buffers_changed
14338 || cursor_type_changed
14339 /* Don't use try_window_reusing_current_matrix in this case
14340 because it can have changed the buffer. */
14341 || !NILP (Vwindow_scroll_functions)
14342 || !just_this_one_p
14343 || MINI_WINDOW_P (w)
14344 || !(used_current_matrix_p
14345 = try_window_reusing_current_matrix (w)))
14346 try_window (window, startp, 0);
14347
14348 /* If new fonts have been loaded (due to fontsets), give up. We
14349 have to start a new redisplay since we need to re-adjust glyph
14350 matrices. */
14351 if (fonts_changed_p)
14352 goto need_larger_matrices;
14353
14354 /* If cursor did not appear assume that the middle of the window is
14355 in the first line of the window. Do it again with the next line.
14356 (Imagine a window of height 100, displaying two lines of height
14357 60. Moving back 50 from it->last_visible_y will end in the first
14358 line.) */
14359 if (w->cursor.vpos < 0)
14360 {
14361 if (!NILP (w->window_end_valid)
14362 && PT >= Z - XFASTINT (w->window_end_pos))
14363 {
14364 clear_glyph_matrix (w->desired_matrix);
14365 move_it_by_lines (&it, 1, 0);
14366 try_window (window, it.current.pos, 0);
14367 }
14368 else if (PT < IT_CHARPOS (it))
14369 {
14370 clear_glyph_matrix (w->desired_matrix);
14371 move_it_by_lines (&it, -1, 0);
14372 try_window (window, it.current.pos, 0);
14373 }
14374 else
14375 {
14376 /* Not much we can do about it. */
14377 }
14378 }
14379
14380 /* Consider the following case: Window starts at BEGV, there is
14381 invisible, intangible text at BEGV, so that display starts at
14382 some point START > BEGV. It can happen that we are called with
14383 PT somewhere between BEGV and START. Try to handle that case. */
14384 if (w->cursor.vpos < 0)
14385 {
14386 struct glyph_row *row = w->current_matrix->rows;
14387 if (row->mode_line_p)
14388 ++row;
14389 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14390 }
14391
14392 if (!cursor_row_fully_visible_p (w, 0, 0))
14393 {
14394 /* If vscroll is enabled, disable it and try again. */
14395 if (w->vscroll)
14396 {
14397 w->vscroll = 0;
14398 clear_glyph_matrix (w->desired_matrix);
14399 goto recenter;
14400 }
14401
14402 /* If centering point failed to make the whole line visible,
14403 put point at the top instead. That has to make the whole line
14404 visible, if it can be done. */
14405 if (centering_position == 0)
14406 goto done;
14407
14408 clear_glyph_matrix (w->desired_matrix);
14409 centering_position = 0;
14410 goto recenter;
14411 }
14412
14413 done:
14414
14415 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14416 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14417 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14418 ? Qt : Qnil);
14419
14420 /* Display the mode line, if we must. */
14421 if ((update_mode_line
14422 /* If window not full width, must redo its mode line
14423 if (a) the window to its side is being redone and
14424 (b) we do a frame-based redisplay. This is a consequence
14425 of how inverted lines are drawn in frame-based redisplay. */
14426 || (!just_this_one_p
14427 && !FRAME_WINDOW_P (f)
14428 && !WINDOW_FULL_WIDTH_P (w))
14429 /* Line number to display. */
14430 || INTEGERP (w->base_line_pos)
14431 /* Column number is displayed and different from the one displayed. */
14432 || (!NILP (w->column_number_displayed)
14433 && (XFASTINT (w->column_number_displayed)
14434 != (int) current_column ()))) /* iftc */
14435 /* This means that the window has a mode line. */
14436 && (WINDOW_WANTS_MODELINE_P (w)
14437 || WINDOW_WANTS_HEADER_LINE_P (w)))
14438 {
14439 display_mode_lines (w);
14440
14441 /* If mode line height has changed, arrange for a thorough
14442 immediate redisplay using the correct mode line height. */
14443 if (WINDOW_WANTS_MODELINE_P (w)
14444 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14445 {
14446 fonts_changed_p = 1;
14447 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14448 = DESIRED_MODE_LINE_HEIGHT (w);
14449 }
14450
14451 /* If header line height has changed, arrange for a thorough
14452 immediate redisplay using the correct header line height. */
14453 if (WINDOW_WANTS_HEADER_LINE_P (w)
14454 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14455 {
14456 fonts_changed_p = 1;
14457 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14458 = DESIRED_HEADER_LINE_HEIGHT (w);
14459 }
14460
14461 if (fonts_changed_p)
14462 goto need_larger_matrices;
14463 }
14464
14465 if (!line_number_displayed
14466 && !BUFFERP (w->base_line_pos))
14467 {
14468 w->base_line_pos = Qnil;
14469 w->base_line_number = Qnil;
14470 }
14471
14472 finish_menu_bars:
14473
14474 /* When we reach a frame's selected window, redo the frame's menu bar. */
14475 if (update_mode_line
14476 && EQ (FRAME_SELECTED_WINDOW (f), window))
14477 {
14478 int redisplay_menu_p = 0;
14479 int redisplay_tool_bar_p = 0;
14480
14481 if (FRAME_WINDOW_P (f))
14482 {
14483 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14484 || defined (HAVE_NS) || defined (USE_GTK)
14485 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14486 #else
14487 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14488 #endif
14489 }
14490 else
14491 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14492
14493 if (redisplay_menu_p)
14494 display_menu_bar (w);
14495
14496 #ifdef HAVE_WINDOW_SYSTEM
14497 if (FRAME_WINDOW_P (f))
14498 {
14499 #if defined (USE_GTK) || defined (HAVE_NS)
14500 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14501 #else
14502 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14503 && (FRAME_TOOL_BAR_LINES (f) > 0
14504 || !NILP (Vauto_resize_tool_bars));
14505 #endif
14506
14507 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14508 {
14509 extern int ignore_mouse_drag_p;
14510 ignore_mouse_drag_p = 1;
14511 }
14512 }
14513 #endif
14514 }
14515
14516 #ifdef HAVE_WINDOW_SYSTEM
14517 if (FRAME_WINDOW_P (f)
14518 && update_window_fringes (w, (just_this_one_p
14519 || (!used_current_matrix_p && !overlay_arrow_seen)
14520 || w->pseudo_window_p)))
14521 {
14522 update_begin (f);
14523 BLOCK_INPUT;
14524 if (draw_window_fringes (w, 1))
14525 x_draw_vertical_border (w);
14526 UNBLOCK_INPUT;
14527 update_end (f);
14528 }
14529 #endif /* HAVE_WINDOW_SYSTEM */
14530
14531 /* We go to this label, with fonts_changed_p nonzero,
14532 if it is necessary to try again using larger glyph matrices.
14533 We have to redeem the scroll bar even in this case,
14534 because the loop in redisplay_internal expects that. */
14535 need_larger_matrices:
14536 ;
14537 finish_scroll_bars:
14538
14539 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14540 {
14541 /* Set the thumb's position and size. */
14542 set_vertical_scroll_bar (w);
14543
14544 /* Note that we actually used the scroll bar attached to this
14545 window, so it shouldn't be deleted at the end of redisplay. */
14546 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14547 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14548 }
14549
14550 /* Restore current_buffer and value of point in it. */
14551 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14552 set_buffer_internal_1 (old);
14553 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14554 shorter. This can be caused by log truncation in *Messages*. */
14555 if (CHARPOS (lpoint) <= ZV)
14556 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14557
14558 unbind_to (count, Qnil);
14559 }
14560
14561
14562 /* Build the complete desired matrix of WINDOW with a window start
14563 buffer position POS.
14564
14565 Value is 1 if successful. It is zero if fonts were loaded during
14566 redisplay which makes re-adjusting glyph matrices necessary, and -1
14567 if point would appear in the scroll margins.
14568 (We check that only if CHECK_MARGINS is nonzero. */
14569
14570 int
14571 try_window (window, pos, check_margins)
14572 Lisp_Object window;
14573 struct text_pos pos;
14574 int check_margins;
14575 {
14576 struct window *w = XWINDOW (window);
14577 struct it it;
14578 struct glyph_row *last_text_row = NULL;
14579 struct frame *f = XFRAME (w->frame);
14580
14581 /* Make POS the new window start. */
14582 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14583
14584 /* Mark cursor position as unknown. No overlay arrow seen. */
14585 w->cursor.vpos = -1;
14586 overlay_arrow_seen = 0;
14587
14588 /* Initialize iterator and info to start at POS. */
14589 start_display (&it, w, pos);
14590
14591 /* Display all lines of W. */
14592 while (it.current_y < it.last_visible_y)
14593 {
14594 if (display_line (&it))
14595 last_text_row = it.glyph_row - 1;
14596 if (fonts_changed_p)
14597 return 0;
14598 }
14599
14600 /* Don't let the cursor end in the scroll margins. */
14601 if (check_margins
14602 && !MINI_WINDOW_P (w))
14603 {
14604 int this_scroll_margin;
14605
14606 if (scroll_margin > 0)
14607 {
14608 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14609 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14610 }
14611 else
14612 this_scroll_margin = 0;
14613
14614 if ((w->cursor.y >= 0 /* not vscrolled */
14615 && w->cursor.y < this_scroll_margin
14616 && CHARPOS (pos) > BEGV
14617 && IT_CHARPOS (it) < ZV)
14618 /* rms: considering make_cursor_line_fully_visible_p here
14619 seems to give wrong results. We don't want to recenter
14620 when the last line is partly visible, we want to allow
14621 that case to be handled in the usual way. */
14622 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14623 {
14624 w->cursor.vpos = -1;
14625 clear_glyph_matrix (w->desired_matrix);
14626 return -1;
14627 }
14628 }
14629
14630 /* If bottom moved off end of frame, change mode line percentage. */
14631 if (XFASTINT (w->window_end_pos) <= 0
14632 && Z != IT_CHARPOS (it))
14633 w->update_mode_line = Qt;
14634
14635 /* Set window_end_pos to the offset of the last character displayed
14636 on the window from the end of current_buffer. Set
14637 window_end_vpos to its row number. */
14638 if (last_text_row)
14639 {
14640 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14641 w->window_end_bytepos
14642 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14643 w->window_end_pos
14644 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14645 w->window_end_vpos
14646 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14647 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14648 ->displays_text_p);
14649 }
14650 else
14651 {
14652 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14653 w->window_end_pos = make_number (Z - ZV);
14654 w->window_end_vpos = make_number (0);
14655 }
14656
14657 /* But that is not valid info until redisplay finishes. */
14658 w->window_end_valid = Qnil;
14659 return 1;
14660 }
14661
14662
14663 \f
14664 /************************************************************************
14665 Window redisplay reusing current matrix when buffer has not changed
14666 ************************************************************************/
14667
14668 /* Try redisplay of window W showing an unchanged buffer with a
14669 different window start than the last time it was displayed by
14670 reusing its current matrix. Value is non-zero if successful.
14671 W->start is the new window start. */
14672
14673 static int
14674 try_window_reusing_current_matrix (w)
14675 struct window *w;
14676 {
14677 struct frame *f = XFRAME (w->frame);
14678 struct glyph_row *row, *bottom_row;
14679 struct it it;
14680 struct run run;
14681 struct text_pos start, new_start;
14682 int nrows_scrolled, i;
14683 struct glyph_row *last_text_row;
14684 struct glyph_row *last_reused_text_row;
14685 struct glyph_row *start_row;
14686 int start_vpos, min_y, max_y;
14687
14688 #if GLYPH_DEBUG
14689 if (inhibit_try_window_reusing)
14690 return 0;
14691 #endif
14692
14693 if (/* This function doesn't handle terminal frames. */
14694 !FRAME_WINDOW_P (f)
14695 /* Don't try to reuse the display if windows have been split
14696 or such. */
14697 || windows_or_buffers_changed
14698 || cursor_type_changed)
14699 return 0;
14700
14701 /* Can't do this if region may have changed. */
14702 if ((!NILP (Vtransient_mark_mode)
14703 && !NILP (current_buffer->mark_active))
14704 || !NILP (w->region_showing)
14705 || !NILP (Vshow_trailing_whitespace))
14706 return 0;
14707
14708 /* If top-line visibility has changed, give up. */
14709 if (WINDOW_WANTS_HEADER_LINE_P (w)
14710 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14711 return 0;
14712
14713 /* Give up if old or new display is scrolled vertically. We could
14714 make this function handle this, but right now it doesn't. */
14715 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14716 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14717 return 0;
14718
14719 /* The variable new_start now holds the new window start. The old
14720 start `start' can be determined from the current matrix. */
14721 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14722 start = start_row->start.pos;
14723 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14724
14725 /* Clear the desired matrix for the display below. */
14726 clear_glyph_matrix (w->desired_matrix);
14727
14728 if (CHARPOS (new_start) <= CHARPOS (start))
14729 {
14730 int first_row_y;
14731
14732 /* Don't use this method if the display starts with an ellipsis
14733 displayed for invisible text. It's not easy to handle that case
14734 below, and it's certainly not worth the effort since this is
14735 not a frequent case. */
14736 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14737 return 0;
14738
14739 IF_DEBUG (debug_method_add (w, "twu1"));
14740
14741 /* Display up to a row that can be reused. The variable
14742 last_text_row is set to the last row displayed that displays
14743 text. Note that it.vpos == 0 if or if not there is a
14744 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14745 start_display (&it, w, new_start);
14746 first_row_y = it.current_y;
14747 w->cursor.vpos = -1;
14748 last_text_row = last_reused_text_row = NULL;
14749
14750 while (it.current_y < it.last_visible_y
14751 && !fonts_changed_p)
14752 {
14753 /* If we have reached into the characters in the START row,
14754 that means the line boundaries have changed. So we
14755 can't start copying with the row START. Maybe it will
14756 work to start copying with the following row. */
14757 while (IT_CHARPOS (it) > CHARPOS (start))
14758 {
14759 /* Advance to the next row as the "start". */
14760 start_row++;
14761 start = start_row->start.pos;
14762 /* If there are no more rows to try, or just one, give up. */
14763 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14764 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14765 || CHARPOS (start) == ZV)
14766 {
14767 clear_glyph_matrix (w->desired_matrix);
14768 return 0;
14769 }
14770
14771 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14772 }
14773 /* If we have reached alignment,
14774 we can copy the rest of the rows. */
14775 if (IT_CHARPOS (it) == CHARPOS (start))
14776 break;
14777
14778 if (display_line (&it))
14779 last_text_row = it.glyph_row - 1;
14780 }
14781
14782 /* A value of current_y < last_visible_y means that we stopped
14783 at the previous window start, which in turn means that we
14784 have at least one reusable row. */
14785 if (it.current_y < it.last_visible_y)
14786 {
14787 /* IT.vpos always starts from 0; it counts text lines. */
14788 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14789
14790 /* Find PT if not already found in the lines displayed. */
14791 if (w->cursor.vpos < 0)
14792 {
14793 int dy = it.current_y - start_row->y;
14794
14795 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14796 row = row_containing_pos (w, PT, row, NULL, dy);
14797 if (row)
14798 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14799 dy, nrows_scrolled);
14800 else
14801 {
14802 clear_glyph_matrix (w->desired_matrix);
14803 return 0;
14804 }
14805 }
14806
14807 /* Scroll the display. Do it before the current matrix is
14808 changed. The problem here is that update has not yet
14809 run, i.e. part of the current matrix is not up to date.
14810 scroll_run_hook will clear the cursor, and use the
14811 current matrix to get the height of the row the cursor is
14812 in. */
14813 run.current_y = start_row->y;
14814 run.desired_y = it.current_y;
14815 run.height = it.last_visible_y - it.current_y;
14816
14817 if (run.height > 0 && run.current_y != run.desired_y)
14818 {
14819 update_begin (f);
14820 FRAME_RIF (f)->update_window_begin_hook (w);
14821 FRAME_RIF (f)->clear_window_mouse_face (w);
14822 FRAME_RIF (f)->scroll_run_hook (w, &run);
14823 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14824 update_end (f);
14825 }
14826
14827 /* Shift current matrix down by nrows_scrolled lines. */
14828 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14829 rotate_matrix (w->current_matrix,
14830 start_vpos,
14831 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14832 nrows_scrolled);
14833
14834 /* Disable lines that must be updated. */
14835 for (i = 0; i < nrows_scrolled; ++i)
14836 (start_row + i)->enabled_p = 0;
14837
14838 /* Re-compute Y positions. */
14839 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14840 max_y = it.last_visible_y;
14841 for (row = start_row + nrows_scrolled;
14842 row < bottom_row;
14843 ++row)
14844 {
14845 row->y = it.current_y;
14846 row->visible_height = row->height;
14847
14848 if (row->y < min_y)
14849 row->visible_height -= min_y - row->y;
14850 if (row->y + row->height > max_y)
14851 row->visible_height -= row->y + row->height - max_y;
14852 row->redraw_fringe_bitmaps_p = 1;
14853
14854 it.current_y += row->height;
14855
14856 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14857 last_reused_text_row = row;
14858 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14859 break;
14860 }
14861
14862 /* Disable lines in the current matrix which are now
14863 below the window. */
14864 for (++row; row < bottom_row; ++row)
14865 row->enabled_p = row->mode_line_p = 0;
14866 }
14867
14868 /* Update window_end_pos etc.; last_reused_text_row is the last
14869 reused row from the current matrix containing text, if any.
14870 The value of last_text_row is the last displayed line
14871 containing text. */
14872 if (last_reused_text_row)
14873 {
14874 w->window_end_bytepos
14875 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14876 w->window_end_pos
14877 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14878 w->window_end_vpos
14879 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14880 w->current_matrix));
14881 }
14882 else if (last_text_row)
14883 {
14884 w->window_end_bytepos
14885 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14886 w->window_end_pos
14887 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14888 w->window_end_vpos
14889 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14890 }
14891 else
14892 {
14893 /* This window must be completely empty. */
14894 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14895 w->window_end_pos = make_number (Z - ZV);
14896 w->window_end_vpos = make_number (0);
14897 }
14898 w->window_end_valid = Qnil;
14899
14900 /* Update hint: don't try scrolling again in update_window. */
14901 w->desired_matrix->no_scrolling_p = 1;
14902
14903 #if GLYPH_DEBUG
14904 debug_method_add (w, "try_window_reusing_current_matrix 1");
14905 #endif
14906 return 1;
14907 }
14908 else if (CHARPOS (new_start) > CHARPOS (start))
14909 {
14910 struct glyph_row *pt_row, *row;
14911 struct glyph_row *first_reusable_row;
14912 struct glyph_row *first_row_to_display;
14913 int dy;
14914 int yb = window_text_bottom_y (w);
14915
14916 /* Find the row starting at new_start, if there is one. Don't
14917 reuse a partially visible line at the end. */
14918 first_reusable_row = start_row;
14919 while (first_reusable_row->enabled_p
14920 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14921 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14922 < CHARPOS (new_start)))
14923 ++first_reusable_row;
14924
14925 /* Give up if there is no row to reuse. */
14926 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14927 || !first_reusable_row->enabled_p
14928 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14929 != CHARPOS (new_start)))
14930 return 0;
14931
14932 /* We can reuse fully visible rows beginning with
14933 first_reusable_row to the end of the window. Set
14934 first_row_to_display to the first row that cannot be reused.
14935 Set pt_row to the row containing point, if there is any. */
14936 pt_row = NULL;
14937 for (first_row_to_display = first_reusable_row;
14938 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14939 ++first_row_to_display)
14940 {
14941 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14942 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14943 pt_row = first_row_to_display;
14944 }
14945
14946 /* Start displaying at the start of first_row_to_display. */
14947 xassert (first_row_to_display->y < yb);
14948 init_to_row_start (&it, w, first_row_to_display);
14949
14950 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14951 - start_vpos);
14952 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14953 - nrows_scrolled);
14954 it.current_y = (first_row_to_display->y - first_reusable_row->y
14955 + WINDOW_HEADER_LINE_HEIGHT (w));
14956
14957 /* Display lines beginning with first_row_to_display in the
14958 desired matrix. Set last_text_row to the last row displayed
14959 that displays text. */
14960 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14961 if (pt_row == NULL)
14962 w->cursor.vpos = -1;
14963 last_text_row = NULL;
14964 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14965 if (display_line (&it))
14966 last_text_row = it.glyph_row - 1;
14967
14968 /* If point is in a reused row, adjust y and vpos of the cursor
14969 position. */
14970 if (pt_row)
14971 {
14972 w->cursor.vpos -= nrows_scrolled;
14973 w->cursor.y -= first_reusable_row->y - start_row->y;
14974 }
14975
14976 /* Give up if point isn't in a row displayed or reused. (This
14977 also handles the case where w->cursor.vpos < nrows_scrolled
14978 after the calls to display_line, which can happen with scroll
14979 margins. See bug#1295.) */
14980 if (w->cursor.vpos < 0)
14981 {
14982 clear_glyph_matrix (w->desired_matrix);
14983 return 0;
14984 }
14985
14986 /* Scroll the display. */
14987 run.current_y = first_reusable_row->y;
14988 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14989 run.height = it.last_visible_y - run.current_y;
14990 dy = run.current_y - run.desired_y;
14991
14992 if (run.height)
14993 {
14994 update_begin (f);
14995 FRAME_RIF (f)->update_window_begin_hook (w);
14996 FRAME_RIF (f)->clear_window_mouse_face (w);
14997 FRAME_RIF (f)->scroll_run_hook (w, &run);
14998 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14999 update_end (f);
15000 }
15001
15002 /* Adjust Y positions of reused rows. */
15003 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15004 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15005 max_y = it.last_visible_y;
15006 for (row = first_reusable_row; row < first_row_to_display; ++row)
15007 {
15008 row->y -= dy;
15009 row->visible_height = row->height;
15010 if (row->y < min_y)
15011 row->visible_height -= min_y - row->y;
15012 if (row->y + row->height > max_y)
15013 row->visible_height -= row->y + row->height - max_y;
15014 row->redraw_fringe_bitmaps_p = 1;
15015 }
15016
15017 /* Scroll the current matrix. */
15018 xassert (nrows_scrolled > 0);
15019 rotate_matrix (w->current_matrix,
15020 start_vpos,
15021 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15022 -nrows_scrolled);
15023
15024 /* Disable rows not reused. */
15025 for (row -= nrows_scrolled; row < bottom_row; ++row)
15026 row->enabled_p = 0;
15027
15028 /* Point may have moved to a different line, so we cannot assume that
15029 the previous cursor position is valid; locate the correct row. */
15030 if (pt_row)
15031 {
15032 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15033 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15034 row++)
15035 {
15036 w->cursor.vpos++;
15037 w->cursor.y = row->y;
15038 }
15039 if (row < bottom_row)
15040 {
15041 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15042 struct glyph *end = glyph + row->used[TEXT_AREA];
15043 struct glyph *orig_glyph = glyph;
15044 struct cursor_pos orig_cursor = w->cursor;
15045
15046 for (; glyph < end
15047 && (!BUFFERP (glyph->object)
15048 || glyph->charpos != PT);
15049 glyph++)
15050 {
15051 w->cursor.hpos++;
15052 w->cursor.x += glyph->pixel_width;
15053 }
15054 /* With bidi reordering, charpos changes non-linearly
15055 with hpos, so the right glyph could be to the
15056 left. */
15057 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15058 && (!BUFFERP (glyph->object) || glyph->charpos != PT))
15059 {
15060 struct glyph *start_glyph = row->glyphs[TEXT_AREA];
15061
15062 glyph = orig_glyph - 1;
15063 orig_cursor.hpos--;
15064 orig_cursor.x -= glyph->pixel_width;
15065 for (; glyph >= start_glyph
15066 && (!BUFFERP (glyph->object)
15067 || glyph->charpos != PT);
15068 glyph--)
15069 {
15070 w->cursor.hpos--;
15071 w->cursor.x -= glyph->pixel_width;
15072 }
15073 if (BUFFERP (glyph->object) && glyph->charpos == PT)
15074 w->cursor = orig_cursor;
15075 }
15076 }
15077 }
15078
15079 /* Adjust window end. A null value of last_text_row means that
15080 the window end is in reused rows which in turn means that
15081 only its vpos can have changed. */
15082 if (last_text_row)
15083 {
15084 w->window_end_bytepos
15085 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15086 w->window_end_pos
15087 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15088 w->window_end_vpos
15089 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15090 }
15091 else
15092 {
15093 w->window_end_vpos
15094 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15095 }
15096
15097 w->window_end_valid = Qnil;
15098 w->desired_matrix->no_scrolling_p = 1;
15099
15100 #if GLYPH_DEBUG
15101 debug_method_add (w, "try_window_reusing_current_matrix 2");
15102 #endif
15103 return 1;
15104 }
15105
15106 return 0;
15107 }
15108
15109
15110 \f
15111 /************************************************************************
15112 Window redisplay reusing current matrix when buffer has changed
15113 ************************************************************************/
15114
15115 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
15116 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
15117 int *, int *));
15118 static struct glyph_row *
15119 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
15120 struct glyph_row *));
15121
15122
15123 /* Return the last row in MATRIX displaying text. If row START is
15124 non-null, start searching with that row. IT gives the dimensions
15125 of the display. Value is null if matrix is empty; otherwise it is
15126 a pointer to the row found. */
15127
15128 static struct glyph_row *
15129 find_last_row_displaying_text (matrix, it, start)
15130 struct glyph_matrix *matrix;
15131 struct it *it;
15132 struct glyph_row *start;
15133 {
15134 struct glyph_row *row, *row_found;
15135
15136 /* Set row_found to the last row in IT->w's current matrix
15137 displaying text. The loop looks funny but think of partially
15138 visible lines. */
15139 row_found = NULL;
15140 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15141 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15142 {
15143 xassert (row->enabled_p);
15144 row_found = row;
15145 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15146 break;
15147 ++row;
15148 }
15149
15150 return row_found;
15151 }
15152
15153
15154 /* Return the last row in the current matrix of W that is not affected
15155 by changes at the start of current_buffer that occurred since W's
15156 current matrix was built. Value is null if no such row exists.
15157
15158 BEG_UNCHANGED us the number of characters unchanged at the start of
15159 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15160 first changed character in current_buffer. Characters at positions <
15161 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15162 when the current matrix was built. */
15163
15164 static struct glyph_row *
15165 find_last_unchanged_at_beg_row (w)
15166 struct window *w;
15167 {
15168 int first_changed_pos = BEG + BEG_UNCHANGED;
15169 struct glyph_row *row;
15170 struct glyph_row *row_found = NULL;
15171 int yb = window_text_bottom_y (w);
15172
15173 /* Find the last row displaying unchanged text. */
15174 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15175 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15176 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15177 ++row)
15178 {
15179 if (/* If row ends before first_changed_pos, it is unchanged,
15180 except in some case. */
15181 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15182 /* When row ends in ZV and we write at ZV it is not
15183 unchanged. */
15184 && !row->ends_at_zv_p
15185 /* When first_changed_pos is the end of a continued line,
15186 row is not unchanged because it may be no longer
15187 continued. */
15188 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15189 && (row->continued_p
15190 || row->exact_window_width_line_p)))
15191 row_found = row;
15192
15193 /* Stop if last visible row. */
15194 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15195 break;
15196 }
15197
15198 return row_found;
15199 }
15200
15201
15202 /* Find the first glyph row in the current matrix of W that is not
15203 affected by changes at the end of current_buffer since the
15204 time W's current matrix was built.
15205
15206 Return in *DELTA the number of chars by which buffer positions in
15207 unchanged text at the end of current_buffer must be adjusted.
15208
15209 Return in *DELTA_BYTES the corresponding number of bytes.
15210
15211 Value is null if no such row exists, i.e. all rows are affected by
15212 changes. */
15213
15214 static struct glyph_row *
15215 find_first_unchanged_at_end_row (w, delta, delta_bytes)
15216 struct window *w;
15217 int *delta, *delta_bytes;
15218 {
15219 struct glyph_row *row;
15220 struct glyph_row *row_found = NULL;
15221
15222 *delta = *delta_bytes = 0;
15223
15224 /* Display must not have been paused, otherwise the current matrix
15225 is not up to date. */
15226 eassert (!NILP (w->window_end_valid));
15227
15228 /* A value of window_end_pos >= END_UNCHANGED means that the window
15229 end is in the range of changed text. If so, there is no
15230 unchanged row at the end of W's current matrix. */
15231 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15232 return NULL;
15233
15234 /* Set row to the last row in W's current matrix displaying text. */
15235 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15236
15237 /* If matrix is entirely empty, no unchanged row exists. */
15238 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15239 {
15240 /* The value of row is the last glyph row in the matrix having a
15241 meaningful buffer position in it. The end position of row
15242 corresponds to window_end_pos. This allows us to translate
15243 buffer positions in the current matrix to current buffer
15244 positions for characters not in changed text. */
15245 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15246 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15247 int last_unchanged_pos, last_unchanged_pos_old;
15248 struct glyph_row *first_text_row
15249 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15250
15251 *delta = Z - Z_old;
15252 *delta_bytes = Z_BYTE - Z_BYTE_old;
15253
15254 /* Set last_unchanged_pos to the buffer position of the last
15255 character in the buffer that has not been changed. Z is the
15256 index + 1 of the last character in current_buffer, i.e. by
15257 subtracting END_UNCHANGED we get the index of the last
15258 unchanged character, and we have to add BEG to get its buffer
15259 position. */
15260 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15261 last_unchanged_pos_old = last_unchanged_pos - *delta;
15262
15263 /* Search backward from ROW for a row displaying a line that
15264 starts at a minimum position >= last_unchanged_pos_old. */
15265 for (; row > first_text_row; --row)
15266 {
15267 /* This used to abort, but it can happen.
15268 It is ok to just stop the search instead here. KFS. */
15269 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15270 break;
15271
15272 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15273 row_found = row;
15274 }
15275 }
15276
15277 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15278
15279 return row_found;
15280 }
15281
15282
15283 /* Make sure that glyph rows in the current matrix of window W
15284 reference the same glyph memory as corresponding rows in the
15285 frame's frame matrix. This function is called after scrolling W's
15286 current matrix on a terminal frame in try_window_id and
15287 try_window_reusing_current_matrix. */
15288
15289 static void
15290 sync_frame_with_window_matrix_rows (w)
15291 struct window *w;
15292 {
15293 struct frame *f = XFRAME (w->frame);
15294 struct glyph_row *window_row, *window_row_end, *frame_row;
15295
15296 /* Preconditions: W must be a leaf window and full-width. Its frame
15297 must have a frame matrix. */
15298 xassert (NILP (w->hchild) && NILP (w->vchild));
15299 xassert (WINDOW_FULL_WIDTH_P (w));
15300 xassert (!FRAME_WINDOW_P (f));
15301
15302 /* If W is a full-width window, glyph pointers in W's current matrix
15303 have, by definition, to be the same as glyph pointers in the
15304 corresponding frame matrix. Note that frame matrices have no
15305 marginal areas (see build_frame_matrix). */
15306 window_row = w->current_matrix->rows;
15307 window_row_end = window_row + w->current_matrix->nrows;
15308 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15309 while (window_row < window_row_end)
15310 {
15311 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15312 struct glyph *end = window_row->glyphs[LAST_AREA];
15313
15314 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15315 frame_row->glyphs[TEXT_AREA] = start;
15316 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15317 frame_row->glyphs[LAST_AREA] = end;
15318
15319 /* Disable frame rows whose corresponding window rows have
15320 been disabled in try_window_id. */
15321 if (!window_row->enabled_p)
15322 frame_row->enabled_p = 0;
15323
15324 ++window_row, ++frame_row;
15325 }
15326 }
15327
15328
15329 /* Find the glyph row in window W containing CHARPOS. Consider all
15330 rows between START and END (not inclusive). END null means search
15331 all rows to the end of the display area of W. Value is the row
15332 containing CHARPOS or null. */
15333
15334 struct glyph_row *
15335 row_containing_pos (w, charpos, start, end, dy)
15336 struct window *w;
15337 int charpos;
15338 struct glyph_row *start, *end;
15339 int dy;
15340 {
15341 struct glyph_row *row = start;
15342 struct glyph_row *best_row = NULL;
15343 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15344 int last_y;
15345
15346 /* If we happen to start on a header-line, skip that. */
15347 if (row->mode_line_p)
15348 ++row;
15349
15350 if ((end && row >= end) || !row->enabled_p)
15351 return NULL;
15352
15353 last_y = window_text_bottom_y (w) - dy;
15354
15355 while (1)
15356 {
15357 /* Give up if we have gone too far. */
15358 if (end && row >= end)
15359 return NULL;
15360 /* This formerly returned if they were equal.
15361 I think that both quantities are of a "last plus one" type;
15362 if so, when they are equal, the row is within the screen. -- rms. */
15363 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15364 return NULL;
15365
15366 /* If it is in this row, return this row. */
15367 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15368 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15369 /* The end position of a row equals the start
15370 position of the next row. If CHARPOS is there, we
15371 would rather display it in the next line, except
15372 when this line ends in ZV. */
15373 && !row->ends_at_zv_p
15374 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15375 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15376 {
15377 struct glyph *g;
15378
15379 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15380 return row;
15381 /* In bidi-reordered rows, there could be several rows
15382 occluding point. We need to find the one which fits
15383 CHARPOS the best. */
15384 for (g = row->glyphs[TEXT_AREA];
15385 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15386 g++)
15387 {
15388 if (!STRINGP (g->object))
15389 {
15390 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15391 {
15392 mindif = eabs (g->charpos - charpos);
15393 best_row = row;
15394 }
15395 }
15396 }
15397 }
15398 else if (best_row)
15399 return best_row;
15400 ++row;
15401 }
15402 }
15403
15404
15405 /* Try to redisplay window W by reusing its existing display. W's
15406 current matrix must be up to date when this function is called,
15407 i.e. window_end_valid must not be nil.
15408
15409 Value is
15410
15411 1 if display has been updated
15412 0 if otherwise unsuccessful
15413 -1 if redisplay with same window start is known not to succeed
15414
15415 The following steps are performed:
15416
15417 1. Find the last row in the current matrix of W that is not
15418 affected by changes at the start of current_buffer. If no such row
15419 is found, give up.
15420
15421 2. Find the first row in W's current matrix that is not affected by
15422 changes at the end of current_buffer. Maybe there is no such row.
15423
15424 3. Display lines beginning with the row + 1 found in step 1 to the
15425 row found in step 2 or, if step 2 didn't find a row, to the end of
15426 the window.
15427
15428 4. If cursor is not known to appear on the window, give up.
15429
15430 5. If display stopped at the row found in step 2, scroll the
15431 display and current matrix as needed.
15432
15433 6. Maybe display some lines at the end of W, if we must. This can
15434 happen under various circumstances, like a partially visible line
15435 becoming fully visible, or because newly displayed lines are displayed
15436 in smaller font sizes.
15437
15438 7. Update W's window end information. */
15439
15440 static int
15441 try_window_id (w)
15442 struct window *w;
15443 {
15444 struct frame *f = XFRAME (w->frame);
15445 struct glyph_matrix *current_matrix = w->current_matrix;
15446 struct glyph_matrix *desired_matrix = w->desired_matrix;
15447 struct glyph_row *last_unchanged_at_beg_row;
15448 struct glyph_row *first_unchanged_at_end_row;
15449 struct glyph_row *row;
15450 struct glyph_row *bottom_row;
15451 int bottom_vpos;
15452 struct it it;
15453 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
15454 struct text_pos start_pos;
15455 struct run run;
15456 int first_unchanged_at_end_vpos = 0;
15457 struct glyph_row *last_text_row, *last_text_row_at_end;
15458 struct text_pos start;
15459 int first_changed_charpos, last_changed_charpos;
15460
15461 #if GLYPH_DEBUG
15462 if (inhibit_try_window_id)
15463 return 0;
15464 #endif
15465
15466 /* This is handy for debugging. */
15467 #if 0
15468 #define GIVE_UP(X) \
15469 do { \
15470 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15471 return 0; \
15472 } while (0)
15473 #else
15474 #define GIVE_UP(X) return 0
15475 #endif
15476
15477 SET_TEXT_POS_FROM_MARKER (start, w->start);
15478
15479 /* Don't use this for mini-windows because these can show
15480 messages and mini-buffers, and we don't handle that here. */
15481 if (MINI_WINDOW_P (w))
15482 GIVE_UP (1);
15483
15484 /* This flag is used to prevent redisplay optimizations. */
15485 if (windows_or_buffers_changed || cursor_type_changed)
15486 GIVE_UP (2);
15487
15488 /* Verify that narrowing has not changed.
15489 Also verify that we were not told to prevent redisplay optimizations.
15490 It would be nice to further
15491 reduce the number of cases where this prevents try_window_id. */
15492 if (current_buffer->clip_changed
15493 || current_buffer->prevent_redisplay_optimizations_p)
15494 GIVE_UP (3);
15495
15496 /* Window must either use window-based redisplay or be full width. */
15497 if (!FRAME_WINDOW_P (f)
15498 && (!FRAME_LINE_INS_DEL_OK (f)
15499 || !WINDOW_FULL_WIDTH_P (w)))
15500 GIVE_UP (4);
15501
15502 /* Give up if point is known NOT to appear in W. */
15503 if (PT < CHARPOS (start))
15504 GIVE_UP (5);
15505
15506 /* Another way to prevent redisplay optimizations. */
15507 if (XFASTINT (w->last_modified) == 0)
15508 GIVE_UP (6);
15509
15510 /* Verify that window is not hscrolled. */
15511 if (XFASTINT (w->hscroll) != 0)
15512 GIVE_UP (7);
15513
15514 /* Verify that display wasn't paused. */
15515 if (NILP (w->window_end_valid))
15516 GIVE_UP (8);
15517
15518 /* Can't use this if highlighting a region because a cursor movement
15519 will do more than just set the cursor. */
15520 if (!NILP (Vtransient_mark_mode)
15521 && !NILP (current_buffer->mark_active))
15522 GIVE_UP (9);
15523
15524 /* Likewise if highlighting trailing whitespace. */
15525 if (!NILP (Vshow_trailing_whitespace))
15526 GIVE_UP (11);
15527
15528 /* Likewise if showing a region. */
15529 if (!NILP (w->region_showing))
15530 GIVE_UP (10);
15531
15532 /* Can't use this if overlay arrow position and/or string have
15533 changed. */
15534 if (overlay_arrows_changed_p ())
15535 GIVE_UP (12);
15536
15537 /* When word-wrap is on, adding a space to the first word of a
15538 wrapped line can change the wrap position, altering the line
15539 above it. It might be worthwhile to handle this more
15540 intelligently, but for now just redisplay from scratch. */
15541 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15542 GIVE_UP (21);
15543
15544 /* Under bidi reordering, adding or deleting a character in the
15545 beginning of a paragraph, before the first strong directional
15546 character, can change the base direction of the paragraph (unless
15547 the buffer specifies a fixed paragraph direction), which will
15548 require to redisplay the whole paragraph. It might be worthwhile
15549 to find the paragraph limits and widen the range of redisplayed
15550 lines to that, but for now just give up this optimization and
15551 redisplay from scratch. */
15552 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15553 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15554 GIVE_UP (22);
15555
15556 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15557 only if buffer has really changed. The reason is that the gap is
15558 initially at Z for freshly visited files. The code below would
15559 set end_unchanged to 0 in that case. */
15560 if (MODIFF > SAVE_MODIFF
15561 /* This seems to happen sometimes after saving a buffer. */
15562 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15563 {
15564 if (GPT - BEG < BEG_UNCHANGED)
15565 BEG_UNCHANGED = GPT - BEG;
15566 if (Z - GPT < END_UNCHANGED)
15567 END_UNCHANGED = Z - GPT;
15568 }
15569
15570 /* The position of the first and last character that has been changed. */
15571 first_changed_charpos = BEG + BEG_UNCHANGED;
15572 last_changed_charpos = Z - END_UNCHANGED;
15573
15574 /* If window starts after a line end, and the last change is in
15575 front of that newline, then changes don't affect the display.
15576 This case happens with stealth-fontification. Note that although
15577 the display is unchanged, glyph positions in the matrix have to
15578 be adjusted, of course. */
15579 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15580 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15581 && ((last_changed_charpos < CHARPOS (start)
15582 && CHARPOS (start) == BEGV)
15583 || (last_changed_charpos < CHARPOS (start) - 1
15584 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15585 {
15586 int Z_old, delta, Z_BYTE_old, delta_bytes;
15587 struct glyph_row *r0;
15588
15589 /* Compute how many chars/bytes have been added to or removed
15590 from the buffer. */
15591 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15592 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15593 delta = Z - Z_old;
15594 delta_bytes = Z_BYTE - Z_BYTE_old;
15595
15596 /* Give up if PT is not in the window. Note that it already has
15597 been checked at the start of try_window_id that PT is not in
15598 front of the window start. */
15599 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15600 GIVE_UP (13);
15601
15602 /* If window start is unchanged, we can reuse the whole matrix
15603 as is, after adjusting glyph positions. No need to compute
15604 the window end again, since its offset from Z hasn't changed. */
15605 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15606 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15607 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15608 /* PT must not be in a partially visible line. */
15609 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15610 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15611 {
15612 /* Adjust positions in the glyph matrix. */
15613 if (delta || delta_bytes)
15614 {
15615 struct glyph_row *r1
15616 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15617 increment_matrix_positions (w->current_matrix,
15618 MATRIX_ROW_VPOS (r0, current_matrix),
15619 MATRIX_ROW_VPOS (r1, current_matrix),
15620 delta, delta_bytes);
15621 }
15622
15623 /* Set the cursor. */
15624 row = row_containing_pos (w, PT, r0, NULL, 0);
15625 if (row)
15626 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15627 else
15628 abort ();
15629 return 1;
15630 }
15631 }
15632
15633 /* Handle the case that changes are all below what is displayed in
15634 the window, and that PT is in the window. This shortcut cannot
15635 be taken if ZV is visible in the window, and text has been added
15636 there that is visible in the window. */
15637 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15638 /* ZV is not visible in the window, or there are no
15639 changes at ZV, actually. */
15640 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15641 || first_changed_charpos == last_changed_charpos))
15642 {
15643 struct glyph_row *r0;
15644
15645 /* Give up if PT is not in the window. Note that it already has
15646 been checked at the start of try_window_id that PT is not in
15647 front of the window start. */
15648 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15649 GIVE_UP (14);
15650
15651 /* If window start is unchanged, we can reuse the whole matrix
15652 as is, without changing glyph positions since no text has
15653 been added/removed in front of the window end. */
15654 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15655 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15656 /* PT must not be in a partially visible line. */
15657 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15658 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15659 {
15660 /* We have to compute the window end anew since text
15661 can have been added/removed after it. */
15662 w->window_end_pos
15663 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15664 w->window_end_bytepos
15665 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15666
15667 /* Set the cursor. */
15668 row = row_containing_pos (w, PT, r0, NULL, 0);
15669 if (row)
15670 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15671 else
15672 abort ();
15673 return 2;
15674 }
15675 }
15676
15677 /* Give up if window start is in the changed area.
15678
15679 The condition used to read
15680
15681 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15682
15683 but why that was tested escapes me at the moment. */
15684 if (CHARPOS (start) >= first_changed_charpos
15685 && CHARPOS (start) <= last_changed_charpos)
15686 GIVE_UP (15);
15687
15688 /* Check that window start agrees with the start of the first glyph
15689 row in its current matrix. Check this after we know the window
15690 start is not in changed text, otherwise positions would not be
15691 comparable. */
15692 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15693 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15694 GIVE_UP (16);
15695
15696 /* Give up if the window ends in strings. Overlay strings
15697 at the end are difficult to handle, so don't try. */
15698 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15699 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15700 GIVE_UP (20);
15701
15702 /* Compute the position at which we have to start displaying new
15703 lines. Some of the lines at the top of the window might be
15704 reusable because they are not displaying changed text. Find the
15705 last row in W's current matrix not affected by changes at the
15706 start of current_buffer. Value is null if changes start in the
15707 first line of window. */
15708 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15709 if (last_unchanged_at_beg_row)
15710 {
15711 /* Avoid starting to display in the moddle of a character, a TAB
15712 for instance. This is easier than to set up the iterator
15713 exactly, and it's not a frequent case, so the additional
15714 effort wouldn't really pay off. */
15715 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15716 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15717 && last_unchanged_at_beg_row > w->current_matrix->rows)
15718 --last_unchanged_at_beg_row;
15719
15720 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15721 GIVE_UP (17);
15722
15723 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15724 GIVE_UP (18);
15725 start_pos = it.current.pos;
15726
15727 /* Start displaying new lines in the desired matrix at the same
15728 vpos we would use in the current matrix, i.e. below
15729 last_unchanged_at_beg_row. */
15730 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15731 current_matrix);
15732 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15733 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15734
15735 xassert (it.hpos == 0 && it.current_x == 0);
15736 }
15737 else
15738 {
15739 /* There are no reusable lines at the start of the window.
15740 Start displaying in the first text line. */
15741 start_display (&it, w, start);
15742 it.vpos = it.first_vpos;
15743 start_pos = it.current.pos;
15744 }
15745
15746 /* Find the first row that is not affected by changes at the end of
15747 the buffer. Value will be null if there is no unchanged row, in
15748 which case we must redisplay to the end of the window. delta
15749 will be set to the value by which buffer positions beginning with
15750 first_unchanged_at_end_row have to be adjusted due to text
15751 changes. */
15752 first_unchanged_at_end_row
15753 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15754 IF_DEBUG (debug_delta = delta);
15755 IF_DEBUG (debug_delta_bytes = delta_bytes);
15756
15757 /* Set stop_pos to the buffer position up to which we will have to
15758 display new lines. If first_unchanged_at_end_row != NULL, this
15759 is the buffer position of the start of the line displayed in that
15760 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15761 that we don't stop at a buffer position. */
15762 stop_pos = 0;
15763 if (first_unchanged_at_end_row)
15764 {
15765 xassert (last_unchanged_at_beg_row == NULL
15766 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15767
15768 /* If this is a continuation line, move forward to the next one
15769 that isn't. Changes in lines above affect this line.
15770 Caution: this may move first_unchanged_at_end_row to a row
15771 not displaying text. */
15772 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15773 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15774 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15775 < it.last_visible_y))
15776 ++first_unchanged_at_end_row;
15777
15778 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15779 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15780 >= it.last_visible_y))
15781 first_unchanged_at_end_row = NULL;
15782 else
15783 {
15784 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15785 + delta);
15786 first_unchanged_at_end_vpos
15787 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15788 xassert (stop_pos >= Z - END_UNCHANGED);
15789 }
15790 }
15791 else if (last_unchanged_at_beg_row == NULL)
15792 GIVE_UP (19);
15793
15794
15795 #if GLYPH_DEBUG
15796
15797 /* Either there is no unchanged row at the end, or the one we have
15798 now displays text. This is a necessary condition for the window
15799 end pos calculation at the end of this function. */
15800 xassert (first_unchanged_at_end_row == NULL
15801 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15802
15803 debug_last_unchanged_at_beg_vpos
15804 = (last_unchanged_at_beg_row
15805 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15806 : -1);
15807 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15808
15809 #endif /* GLYPH_DEBUG != 0 */
15810
15811
15812 /* Display new lines. Set last_text_row to the last new line
15813 displayed which has text on it, i.e. might end up as being the
15814 line where the window_end_vpos is. */
15815 w->cursor.vpos = -1;
15816 last_text_row = NULL;
15817 overlay_arrow_seen = 0;
15818 while (it.current_y < it.last_visible_y
15819 && !fonts_changed_p
15820 && (first_unchanged_at_end_row == NULL
15821 || IT_CHARPOS (it) < stop_pos))
15822 {
15823 if (display_line (&it))
15824 last_text_row = it.glyph_row - 1;
15825 }
15826
15827 if (fonts_changed_p)
15828 return -1;
15829
15830
15831 /* Compute differences in buffer positions, y-positions etc. for
15832 lines reused at the bottom of the window. Compute what we can
15833 scroll. */
15834 if (first_unchanged_at_end_row
15835 /* No lines reused because we displayed everything up to the
15836 bottom of the window. */
15837 && it.current_y < it.last_visible_y)
15838 {
15839 dvpos = (it.vpos
15840 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15841 current_matrix));
15842 dy = it.current_y - first_unchanged_at_end_row->y;
15843 run.current_y = first_unchanged_at_end_row->y;
15844 run.desired_y = run.current_y + dy;
15845 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15846 }
15847 else
15848 {
15849 delta = delta_bytes = dvpos = dy
15850 = run.current_y = run.desired_y = run.height = 0;
15851 first_unchanged_at_end_row = NULL;
15852 }
15853 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15854
15855
15856 /* Find the cursor if not already found. We have to decide whether
15857 PT will appear on this window (it sometimes doesn't, but this is
15858 not a very frequent case.) This decision has to be made before
15859 the current matrix is altered. A value of cursor.vpos < 0 means
15860 that PT is either in one of the lines beginning at
15861 first_unchanged_at_end_row or below the window. Don't care for
15862 lines that might be displayed later at the window end; as
15863 mentioned, this is not a frequent case. */
15864 if (w->cursor.vpos < 0)
15865 {
15866 /* Cursor in unchanged rows at the top? */
15867 if (PT < CHARPOS (start_pos)
15868 && last_unchanged_at_beg_row)
15869 {
15870 row = row_containing_pos (w, PT,
15871 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15872 last_unchanged_at_beg_row + 1, 0);
15873 if (row)
15874 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15875 }
15876
15877 /* Start from first_unchanged_at_end_row looking for PT. */
15878 else if (first_unchanged_at_end_row)
15879 {
15880 row = row_containing_pos (w, PT - delta,
15881 first_unchanged_at_end_row, NULL, 0);
15882 if (row)
15883 set_cursor_from_row (w, row, w->current_matrix, delta,
15884 delta_bytes, dy, dvpos);
15885 }
15886
15887 /* Give up if cursor was not found. */
15888 if (w->cursor.vpos < 0)
15889 {
15890 clear_glyph_matrix (w->desired_matrix);
15891 return -1;
15892 }
15893 }
15894
15895 /* Don't let the cursor end in the scroll margins. */
15896 {
15897 int this_scroll_margin, cursor_height;
15898
15899 this_scroll_margin = max (0, scroll_margin);
15900 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15901 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15902 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15903
15904 if ((w->cursor.y < this_scroll_margin
15905 && CHARPOS (start) > BEGV)
15906 /* Old redisplay didn't take scroll margin into account at the bottom,
15907 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15908 || (w->cursor.y + (make_cursor_line_fully_visible_p
15909 ? cursor_height + this_scroll_margin
15910 : 1)) > it.last_visible_y)
15911 {
15912 w->cursor.vpos = -1;
15913 clear_glyph_matrix (w->desired_matrix);
15914 return -1;
15915 }
15916 }
15917
15918 /* Scroll the display. Do it before changing the current matrix so
15919 that xterm.c doesn't get confused about where the cursor glyph is
15920 found. */
15921 if (dy && run.height)
15922 {
15923 update_begin (f);
15924
15925 if (FRAME_WINDOW_P (f))
15926 {
15927 FRAME_RIF (f)->update_window_begin_hook (w);
15928 FRAME_RIF (f)->clear_window_mouse_face (w);
15929 FRAME_RIF (f)->scroll_run_hook (w, &run);
15930 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15931 }
15932 else
15933 {
15934 /* Terminal frame. In this case, dvpos gives the number of
15935 lines to scroll by; dvpos < 0 means scroll up. */
15936 int first_unchanged_at_end_vpos
15937 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15938 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15939 int end = (WINDOW_TOP_EDGE_LINE (w)
15940 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15941 + window_internal_height (w));
15942
15943 /* Perform the operation on the screen. */
15944 if (dvpos > 0)
15945 {
15946 /* Scroll last_unchanged_at_beg_row to the end of the
15947 window down dvpos lines. */
15948 set_terminal_window (f, end);
15949
15950 /* On dumb terminals delete dvpos lines at the end
15951 before inserting dvpos empty lines. */
15952 if (!FRAME_SCROLL_REGION_OK (f))
15953 ins_del_lines (f, end - dvpos, -dvpos);
15954
15955 /* Insert dvpos empty lines in front of
15956 last_unchanged_at_beg_row. */
15957 ins_del_lines (f, from, dvpos);
15958 }
15959 else if (dvpos < 0)
15960 {
15961 /* Scroll up last_unchanged_at_beg_vpos to the end of
15962 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15963 set_terminal_window (f, end);
15964
15965 /* Delete dvpos lines in front of
15966 last_unchanged_at_beg_vpos. ins_del_lines will set
15967 the cursor to the given vpos and emit |dvpos| delete
15968 line sequences. */
15969 ins_del_lines (f, from + dvpos, dvpos);
15970
15971 /* On a dumb terminal insert dvpos empty lines at the
15972 end. */
15973 if (!FRAME_SCROLL_REGION_OK (f))
15974 ins_del_lines (f, end + dvpos, -dvpos);
15975 }
15976
15977 set_terminal_window (f, 0);
15978 }
15979
15980 update_end (f);
15981 }
15982
15983 /* Shift reused rows of the current matrix to the right position.
15984 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15985 text. */
15986 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15987 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15988 if (dvpos < 0)
15989 {
15990 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15991 bottom_vpos, dvpos);
15992 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15993 bottom_vpos, 0);
15994 }
15995 else if (dvpos > 0)
15996 {
15997 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15998 bottom_vpos, dvpos);
15999 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16000 first_unchanged_at_end_vpos + dvpos, 0);
16001 }
16002
16003 /* For frame-based redisplay, make sure that current frame and window
16004 matrix are in sync with respect to glyph memory. */
16005 if (!FRAME_WINDOW_P (f))
16006 sync_frame_with_window_matrix_rows (w);
16007
16008 /* Adjust buffer positions in reused rows. */
16009 if (delta || delta_bytes)
16010 increment_matrix_positions (current_matrix,
16011 first_unchanged_at_end_vpos + dvpos,
16012 bottom_vpos, delta, delta_bytes);
16013
16014 /* Adjust Y positions. */
16015 if (dy)
16016 shift_glyph_matrix (w, current_matrix,
16017 first_unchanged_at_end_vpos + dvpos,
16018 bottom_vpos, dy);
16019
16020 if (first_unchanged_at_end_row)
16021 {
16022 first_unchanged_at_end_row += dvpos;
16023 if (first_unchanged_at_end_row->y >= it.last_visible_y
16024 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16025 first_unchanged_at_end_row = NULL;
16026 }
16027
16028 /* If scrolling up, there may be some lines to display at the end of
16029 the window. */
16030 last_text_row_at_end = NULL;
16031 if (dy < 0)
16032 {
16033 /* Scrolling up can leave for example a partially visible line
16034 at the end of the window to be redisplayed. */
16035 /* Set last_row to the glyph row in the current matrix where the
16036 window end line is found. It has been moved up or down in
16037 the matrix by dvpos. */
16038 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16039 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16040
16041 /* If last_row is the window end line, it should display text. */
16042 xassert (last_row->displays_text_p);
16043
16044 /* If window end line was partially visible before, begin
16045 displaying at that line. Otherwise begin displaying with the
16046 line following it. */
16047 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16048 {
16049 init_to_row_start (&it, w, last_row);
16050 it.vpos = last_vpos;
16051 it.current_y = last_row->y;
16052 }
16053 else
16054 {
16055 init_to_row_end (&it, w, last_row);
16056 it.vpos = 1 + last_vpos;
16057 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16058 ++last_row;
16059 }
16060
16061 /* We may start in a continuation line. If so, we have to
16062 get the right continuation_lines_width and current_x. */
16063 it.continuation_lines_width = last_row->continuation_lines_width;
16064 it.hpos = it.current_x = 0;
16065
16066 /* Display the rest of the lines at the window end. */
16067 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16068 while (it.current_y < it.last_visible_y
16069 && !fonts_changed_p)
16070 {
16071 /* Is it always sure that the display agrees with lines in
16072 the current matrix? I don't think so, so we mark rows
16073 displayed invalid in the current matrix by setting their
16074 enabled_p flag to zero. */
16075 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16076 if (display_line (&it))
16077 last_text_row_at_end = it.glyph_row - 1;
16078 }
16079 }
16080
16081 /* Update window_end_pos and window_end_vpos. */
16082 if (first_unchanged_at_end_row
16083 && !last_text_row_at_end)
16084 {
16085 /* Window end line if one of the preserved rows from the current
16086 matrix. Set row to the last row displaying text in current
16087 matrix starting at first_unchanged_at_end_row, after
16088 scrolling. */
16089 xassert (first_unchanged_at_end_row->displays_text_p);
16090 row = find_last_row_displaying_text (w->current_matrix, &it,
16091 first_unchanged_at_end_row);
16092 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16093
16094 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16095 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16096 w->window_end_vpos
16097 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16098 xassert (w->window_end_bytepos >= 0);
16099 IF_DEBUG (debug_method_add (w, "A"));
16100 }
16101 else if (last_text_row_at_end)
16102 {
16103 w->window_end_pos
16104 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16105 w->window_end_bytepos
16106 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16107 w->window_end_vpos
16108 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16109 xassert (w->window_end_bytepos >= 0);
16110 IF_DEBUG (debug_method_add (w, "B"));
16111 }
16112 else if (last_text_row)
16113 {
16114 /* We have displayed either to the end of the window or at the
16115 end of the window, i.e. the last row with text is to be found
16116 in the desired matrix. */
16117 w->window_end_pos
16118 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16119 w->window_end_bytepos
16120 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16121 w->window_end_vpos
16122 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16123 xassert (w->window_end_bytepos >= 0);
16124 }
16125 else if (first_unchanged_at_end_row == NULL
16126 && last_text_row == NULL
16127 && last_text_row_at_end == NULL)
16128 {
16129 /* Displayed to end of window, but no line containing text was
16130 displayed. Lines were deleted at the end of the window. */
16131 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16132 int vpos = XFASTINT (w->window_end_vpos);
16133 struct glyph_row *current_row = current_matrix->rows + vpos;
16134 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16135
16136 for (row = NULL;
16137 row == NULL && vpos >= first_vpos;
16138 --vpos, --current_row, --desired_row)
16139 {
16140 if (desired_row->enabled_p)
16141 {
16142 if (desired_row->displays_text_p)
16143 row = desired_row;
16144 }
16145 else if (current_row->displays_text_p)
16146 row = current_row;
16147 }
16148
16149 xassert (row != NULL);
16150 w->window_end_vpos = make_number (vpos + 1);
16151 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16152 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16153 xassert (w->window_end_bytepos >= 0);
16154 IF_DEBUG (debug_method_add (w, "C"));
16155 }
16156 else
16157 abort ();
16158
16159 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16160 debug_end_vpos = XFASTINT (w->window_end_vpos));
16161
16162 /* Record that display has not been completed. */
16163 w->window_end_valid = Qnil;
16164 w->desired_matrix->no_scrolling_p = 1;
16165 return 3;
16166
16167 #undef GIVE_UP
16168 }
16169
16170
16171 \f
16172 /***********************************************************************
16173 More debugging support
16174 ***********************************************************************/
16175
16176 #if GLYPH_DEBUG
16177
16178 void dump_glyph_row P_ ((struct glyph_row *, int, int));
16179 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
16180 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
16181
16182
16183 /* Dump the contents of glyph matrix MATRIX on stderr.
16184
16185 GLYPHS 0 means don't show glyph contents.
16186 GLYPHS 1 means show glyphs in short form
16187 GLYPHS > 1 means show glyphs in long form. */
16188
16189 void
16190 dump_glyph_matrix (matrix, glyphs)
16191 struct glyph_matrix *matrix;
16192 int glyphs;
16193 {
16194 int i;
16195 for (i = 0; i < matrix->nrows; ++i)
16196 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16197 }
16198
16199
16200 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16201 the glyph row and area where the glyph comes from. */
16202
16203 void
16204 dump_glyph (row, glyph, area)
16205 struct glyph_row *row;
16206 struct glyph *glyph;
16207 int area;
16208 {
16209 if (glyph->type == CHAR_GLYPH)
16210 {
16211 fprintf (stderr,
16212 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16213 glyph - row->glyphs[TEXT_AREA],
16214 'C',
16215 glyph->charpos,
16216 (BUFFERP (glyph->object)
16217 ? 'B'
16218 : (STRINGP (glyph->object)
16219 ? 'S'
16220 : '-')),
16221 glyph->pixel_width,
16222 glyph->u.ch,
16223 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16224 ? glyph->u.ch
16225 : '.'),
16226 glyph->face_id,
16227 glyph->left_box_line_p,
16228 glyph->right_box_line_p);
16229 }
16230 else if (glyph->type == STRETCH_GLYPH)
16231 {
16232 fprintf (stderr,
16233 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16234 glyph - row->glyphs[TEXT_AREA],
16235 'S',
16236 glyph->charpos,
16237 (BUFFERP (glyph->object)
16238 ? 'B'
16239 : (STRINGP (glyph->object)
16240 ? 'S'
16241 : '-')),
16242 glyph->pixel_width,
16243 0,
16244 '.',
16245 glyph->face_id,
16246 glyph->left_box_line_p,
16247 glyph->right_box_line_p);
16248 }
16249 else if (glyph->type == IMAGE_GLYPH)
16250 {
16251 fprintf (stderr,
16252 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16253 glyph - row->glyphs[TEXT_AREA],
16254 'I',
16255 glyph->charpos,
16256 (BUFFERP (glyph->object)
16257 ? 'B'
16258 : (STRINGP (glyph->object)
16259 ? 'S'
16260 : '-')),
16261 glyph->pixel_width,
16262 glyph->u.img_id,
16263 '.',
16264 glyph->face_id,
16265 glyph->left_box_line_p,
16266 glyph->right_box_line_p);
16267 }
16268 else if (glyph->type == COMPOSITE_GLYPH)
16269 {
16270 fprintf (stderr,
16271 " %5d %4c %6d %c %3d 0x%05x",
16272 glyph - row->glyphs[TEXT_AREA],
16273 '+',
16274 glyph->charpos,
16275 (BUFFERP (glyph->object)
16276 ? 'B'
16277 : (STRINGP (glyph->object)
16278 ? 'S'
16279 : '-')),
16280 glyph->pixel_width,
16281 glyph->u.cmp.id);
16282 if (glyph->u.cmp.automatic)
16283 fprintf (stderr,
16284 "[%d-%d]",
16285 glyph->u.cmp.from, glyph->u.cmp.to);
16286 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16287 glyph->face_id,
16288 glyph->left_box_line_p,
16289 glyph->right_box_line_p);
16290 }
16291 }
16292
16293
16294 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16295 GLYPHS 0 means don't show glyph contents.
16296 GLYPHS 1 means show glyphs in short form
16297 GLYPHS > 1 means show glyphs in long form. */
16298
16299 void
16300 dump_glyph_row (row, vpos, glyphs)
16301 struct glyph_row *row;
16302 int vpos, glyphs;
16303 {
16304 if (glyphs != 1)
16305 {
16306 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16307 fprintf (stderr, "======================================================================\n");
16308
16309 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16310 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16311 vpos,
16312 MATRIX_ROW_START_CHARPOS (row),
16313 MATRIX_ROW_END_CHARPOS (row),
16314 row->used[TEXT_AREA],
16315 row->contains_overlapping_glyphs_p,
16316 row->enabled_p,
16317 row->truncated_on_left_p,
16318 row->truncated_on_right_p,
16319 row->continued_p,
16320 MATRIX_ROW_CONTINUATION_LINE_P (row),
16321 row->displays_text_p,
16322 row->ends_at_zv_p,
16323 row->fill_line_p,
16324 row->ends_in_middle_of_char_p,
16325 row->starts_in_middle_of_char_p,
16326 row->mouse_face_p,
16327 row->x,
16328 row->y,
16329 row->pixel_width,
16330 row->height,
16331 row->visible_height,
16332 row->ascent,
16333 row->phys_ascent);
16334 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16335 row->end.overlay_string_index,
16336 row->continuation_lines_width);
16337 fprintf (stderr, "%9d %5d\n",
16338 CHARPOS (row->start.string_pos),
16339 CHARPOS (row->end.string_pos));
16340 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16341 row->end.dpvec_index);
16342 }
16343
16344 if (glyphs > 1)
16345 {
16346 int area;
16347
16348 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16349 {
16350 struct glyph *glyph = row->glyphs[area];
16351 struct glyph *glyph_end = glyph + row->used[area];
16352
16353 /* Glyph for a line end in text. */
16354 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16355 ++glyph_end;
16356
16357 if (glyph < glyph_end)
16358 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16359
16360 for (; glyph < glyph_end; ++glyph)
16361 dump_glyph (row, glyph, area);
16362 }
16363 }
16364 else if (glyphs == 1)
16365 {
16366 int area;
16367
16368 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16369 {
16370 char *s = (char *) alloca (row->used[area] + 1);
16371 int i;
16372
16373 for (i = 0; i < row->used[area]; ++i)
16374 {
16375 struct glyph *glyph = row->glyphs[area] + i;
16376 if (glyph->type == CHAR_GLYPH
16377 && glyph->u.ch < 0x80
16378 && glyph->u.ch >= ' ')
16379 s[i] = glyph->u.ch;
16380 else
16381 s[i] = '.';
16382 }
16383
16384 s[i] = '\0';
16385 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16386 }
16387 }
16388 }
16389
16390
16391 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16392 Sdump_glyph_matrix, 0, 1, "p",
16393 doc: /* Dump the current matrix of the selected window to stderr.
16394 Shows contents of glyph row structures. With non-nil
16395 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16396 glyphs in short form, otherwise show glyphs in long form. */)
16397 (glyphs)
16398 Lisp_Object glyphs;
16399 {
16400 struct window *w = XWINDOW (selected_window);
16401 struct buffer *buffer = XBUFFER (w->buffer);
16402
16403 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16404 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16405 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16406 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16407 fprintf (stderr, "=============================================\n");
16408 dump_glyph_matrix (w->current_matrix,
16409 NILP (glyphs) ? 0 : XINT (glyphs));
16410 return Qnil;
16411 }
16412
16413
16414 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16415 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16416 ()
16417 {
16418 struct frame *f = XFRAME (selected_frame);
16419 dump_glyph_matrix (f->current_matrix, 1);
16420 return Qnil;
16421 }
16422
16423
16424 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16425 doc: /* Dump glyph row ROW to stderr.
16426 GLYPH 0 means don't dump glyphs.
16427 GLYPH 1 means dump glyphs in short form.
16428 GLYPH > 1 or omitted means dump glyphs in long form. */)
16429 (row, glyphs)
16430 Lisp_Object row, glyphs;
16431 {
16432 struct glyph_matrix *matrix;
16433 int vpos;
16434
16435 CHECK_NUMBER (row);
16436 matrix = XWINDOW (selected_window)->current_matrix;
16437 vpos = XINT (row);
16438 if (vpos >= 0 && vpos < matrix->nrows)
16439 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16440 vpos,
16441 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16442 return Qnil;
16443 }
16444
16445
16446 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16447 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16448 GLYPH 0 means don't dump glyphs.
16449 GLYPH 1 means dump glyphs in short form.
16450 GLYPH > 1 or omitted means dump glyphs in long form. */)
16451 (row, glyphs)
16452 Lisp_Object row, glyphs;
16453 {
16454 struct frame *sf = SELECTED_FRAME ();
16455 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16456 int vpos;
16457
16458 CHECK_NUMBER (row);
16459 vpos = XINT (row);
16460 if (vpos >= 0 && vpos < m->nrows)
16461 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16462 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16463 return Qnil;
16464 }
16465
16466
16467 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16468 doc: /* Toggle tracing of redisplay.
16469 With ARG, turn tracing on if and only if ARG is positive. */)
16470 (arg)
16471 Lisp_Object arg;
16472 {
16473 if (NILP (arg))
16474 trace_redisplay_p = !trace_redisplay_p;
16475 else
16476 {
16477 arg = Fprefix_numeric_value (arg);
16478 trace_redisplay_p = XINT (arg) > 0;
16479 }
16480
16481 return Qnil;
16482 }
16483
16484
16485 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16486 doc: /* Like `format', but print result to stderr.
16487 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16488 (nargs, args)
16489 int nargs;
16490 Lisp_Object *args;
16491 {
16492 Lisp_Object s = Fformat (nargs, args);
16493 fprintf (stderr, "%s", SDATA (s));
16494 return Qnil;
16495 }
16496
16497 #endif /* GLYPH_DEBUG */
16498
16499
16500 \f
16501 /***********************************************************************
16502 Building Desired Matrix Rows
16503 ***********************************************************************/
16504
16505 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16506 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16507
16508 static struct glyph_row *
16509 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
16510 struct window *w;
16511 Lisp_Object overlay_arrow_string;
16512 {
16513 struct frame *f = XFRAME (WINDOW_FRAME (w));
16514 struct buffer *buffer = XBUFFER (w->buffer);
16515 struct buffer *old = current_buffer;
16516 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16517 int arrow_len = SCHARS (overlay_arrow_string);
16518 const unsigned char *arrow_end = arrow_string + arrow_len;
16519 const unsigned char *p;
16520 struct it it;
16521 int multibyte_p;
16522 int n_glyphs_before;
16523
16524 set_buffer_temp (buffer);
16525 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16526 it.glyph_row->used[TEXT_AREA] = 0;
16527 SET_TEXT_POS (it.position, 0, 0);
16528
16529 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16530 p = arrow_string;
16531 while (p < arrow_end)
16532 {
16533 Lisp_Object face, ilisp;
16534
16535 /* Get the next character. */
16536 if (multibyte_p)
16537 it.c = string_char_and_length (p, &it.len);
16538 else
16539 it.c = *p, it.len = 1;
16540 p += it.len;
16541
16542 /* Get its face. */
16543 ilisp = make_number (p - arrow_string);
16544 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16545 it.face_id = compute_char_face (f, it.c, face);
16546
16547 /* Compute its width, get its glyphs. */
16548 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16549 SET_TEXT_POS (it.position, -1, -1);
16550 PRODUCE_GLYPHS (&it);
16551
16552 /* If this character doesn't fit any more in the line, we have
16553 to remove some glyphs. */
16554 if (it.current_x > it.last_visible_x)
16555 {
16556 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16557 break;
16558 }
16559 }
16560
16561 set_buffer_temp (old);
16562 return it.glyph_row;
16563 }
16564
16565
16566 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16567 glyphs are only inserted for terminal frames since we can't really
16568 win with truncation glyphs when partially visible glyphs are
16569 involved. Which glyphs to insert is determined by
16570 produce_special_glyphs. */
16571
16572 static void
16573 insert_left_trunc_glyphs (it)
16574 struct it *it;
16575 {
16576 struct it truncate_it;
16577 struct glyph *from, *end, *to, *toend;
16578
16579 xassert (!FRAME_WINDOW_P (it->f));
16580
16581 /* Get the truncation glyphs. */
16582 truncate_it = *it;
16583 truncate_it.current_x = 0;
16584 truncate_it.face_id = DEFAULT_FACE_ID;
16585 truncate_it.glyph_row = &scratch_glyph_row;
16586 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16587 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16588 truncate_it.object = make_number (0);
16589 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16590
16591 /* Overwrite glyphs from IT with truncation glyphs. */
16592 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16593 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16594 to = it->glyph_row->glyphs[TEXT_AREA];
16595 toend = to + it->glyph_row->used[TEXT_AREA];
16596
16597 while (from < end)
16598 *to++ = *from++;
16599
16600 /* There may be padding glyphs left over. Overwrite them too. */
16601 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16602 {
16603 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16604 while (from < end)
16605 *to++ = *from++;
16606 }
16607
16608 if (to > toend)
16609 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16610 }
16611
16612
16613 /* Compute the pixel height and width of IT->glyph_row.
16614
16615 Most of the time, ascent and height of a display line will be equal
16616 to the max_ascent and max_height values of the display iterator
16617 structure. This is not the case if
16618
16619 1. We hit ZV without displaying anything. In this case, max_ascent
16620 and max_height will be zero.
16621
16622 2. We have some glyphs that don't contribute to the line height.
16623 (The glyph row flag contributes_to_line_height_p is for future
16624 pixmap extensions).
16625
16626 The first case is easily covered by using default values because in
16627 these cases, the line height does not really matter, except that it
16628 must not be zero. */
16629
16630 static void
16631 compute_line_metrics (it)
16632 struct it *it;
16633 {
16634 struct glyph_row *row = it->glyph_row;
16635 int area, i;
16636
16637 if (FRAME_WINDOW_P (it->f))
16638 {
16639 int i, min_y, max_y;
16640
16641 /* The line may consist of one space only, that was added to
16642 place the cursor on it. If so, the row's height hasn't been
16643 computed yet. */
16644 if (row->height == 0)
16645 {
16646 if (it->max_ascent + it->max_descent == 0)
16647 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16648 row->ascent = it->max_ascent;
16649 row->height = it->max_ascent + it->max_descent;
16650 row->phys_ascent = it->max_phys_ascent;
16651 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16652 row->extra_line_spacing = it->max_extra_line_spacing;
16653 }
16654
16655 /* Compute the width of this line. */
16656 row->pixel_width = row->x;
16657 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16658 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16659
16660 xassert (row->pixel_width >= 0);
16661 xassert (row->ascent >= 0 && row->height > 0);
16662
16663 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16664 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16665
16666 /* If first line's physical ascent is larger than its logical
16667 ascent, use the physical ascent, and make the row taller.
16668 This makes accented characters fully visible. */
16669 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16670 && row->phys_ascent > row->ascent)
16671 {
16672 row->height += row->phys_ascent - row->ascent;
16673 row->ascent = row->phys_ascent;
16674 }
16675
16676 /* Compute how much of the line is visible. */
16677 row->visible_height = row->height;
16678
16679 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16680 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16681
16682 if (row->y < min_y)
16683 row->visible_height -= min_y - row->y;
16684 if (row->y + row->height > max_y)
16685 row->visible_height -= row->y + row->height - max_y;
16686 }
16687 else
16688 {
16689 row->pixel_width = row->used[TEXT_AREA];
16690 if (row->continued_p)
16691 row->pixel_width -= it->continuation_pixel_width;
16692 else if (row->truncated_on_right_p)
16693 row->pixel_width -= it->truncation_pixel_width;
16694 row->ascent = row->phys_ascent = 0;
16695 row->height = row->phys_height = row->visible_height = 1;
16696 row->extra_line_spacing = 0;
16697 }
16698
16699 /* Compute a hash code for this row. */
16700 row->hash = 0;
16701 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16702 for (i = 0; i < row->used[area]; ++i)
16703 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16704 + row->glyphs[area][i].u.val
16705 + row->glyphs[area][i].face_id
16706 + row->glyphs[area][i].padding_p
16707 + (row->glyphs[area][i].type << 2));
16708
16709 it->max_ascent = it->max_descent = 0;
16710 it->max_phys_ascent = it->max_phys_descent = 0;
16711 }
16712
16713
16714 /* Append one space to the glyph row of iterator IT if doing a
16715 window-based redisplay. The space has the same face as
16716 IT->face_id. Value is non-zero if a space was added.
16717
16718 This function is called to make sure that there is always one glyph
16719 at the end of a glyph row that the cursor can be set on under
16720 window-systems. (If there weren't such a glyph we would not know
16721 how wide and tall a box cursor should be displayed).
16722
16723 At the same time this space let's a nicely handle clearing to the
16724 end of the line if the row ends in italic text. */
16725
16726 static int
16727 append_space_for_newline (it, default_face_p)
16728 struct it *it;
16729 int default_face_p;
16730 {
16731 if (FRAME_WINDOW_P (it->f))
16732 {
16733 int n = it->glyph_row->used[TEXT_AREA];
16734
16735 if (it->glyph_row->glyphs[TEXT_AREA] + n
16736 < it->glyph_row->glyphs[1 + TEXT_AREA])
16737 {
16738 /* Save some values that must not be changed.
16739 Must save IT->c and IT->len because otherwise
16740 ITERATOR_AT_END_P wouldn't work anymore after
16741 append_space_for_newline has been called. */
16742 enum display_element_type saved_what = it->what;
16743 int saved_c = it->c, saved_len = it->len;
16744 int saved_x = it->current_x;
16745 int saved_face_id = it->face_id;
16746 struct text_pos saved_pos;
16747 Lisp_Object saved_object;
16748 struct face *face;
16749
16750 saved_object = it->object;
16751 saved_pos = it->position;
16752
16753 it->what = IT_CHARACTER;
16754 bzero (&it->position, sizeof it->position);
16755 it->object = make_number (0);
16756 it->c = ' ';
16757 it->len = 1;
16758
16759 if (default_face_p)
16760 it->face_id = DEFAULT_FACE_ID;
16761 else if (it->face_before_selective_p)
16762 it->face_id = it->saved_face_id;
16763 face = FACE_FROM_ID (it->f, it->face_id);
16764 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16765
16766 PRODUCE_GLYPHS (it);
16767
16768 it->override_ascent = -1;
16769 it->constrain_row_ascent_descent_p = 0;
16770 it->current_x = saved_x;
16771 it->object = saved_object;
16772 it->position = saved_pos;
16773 it->what = saved_what;
16774 it->face_id = saved_face_id;
16775 it->len = saved_len;
16776 it->c = saved_c;
16777 return 1;
16778 }
16779 }
16780
16781 return 0;
16782 }
16783
16784
16785 /* Extend the face of the last glyph in the text area of IT->glyph_row
16786 to the end of the display line. Called from display_line. If the
16787 glyph row is empty, add a space glyph to it so that we know the
16788 face to draw. Set the glyph row flag fill_line_p. If the glyph
16789 row is R2L, prepend a stretch glyph to cover the empty space to the
16790 left of the leftmost glyph. */
16791
16792 static void
16793 extend_face_to_end_of_line (it)
16794 struct it *it;
16795 {
16796 struct face *face;
16797 struct frame *f = it->f;
16798
16799 /* If line is already filled, do nothing. */
16800 if (it->current_x >= it->last_visible_x)
16801 return;
16802
16803 /* Face extension extends the background and box of IT->face_id
16804 to the end of the line. If the background equals the background
16805 of the frame, we don't have to do anything. */
16806 if (it->face_before_selective_p)
16807 face = FACE_FROM_ID (f, it->saved_face_id);
16808 else
16809 face = FACE_FROM_ID (f, it->face_id);
16810
16811 if (FRAME_WINDOW_P (f)
16812 && it->glyph_row->displays_text_p
16813 && face->box == FACE_NO_BOX
16814 && face->background == FRAME_BACKGROUND_PIXEL (f)
16815 && !face->stipple
16816 && !it->glyph_row->reversed_p)
16817 return;
16818
16819 /* Set the glyph row flag indicating that the face of the last glyph
16820 in the text area has to be drawn to the end of the text area. */
16821 it->glyph_row->fill_line_p = 1;
16822
16823 /* If current character of IT is not ASCII, make sure we have the
16824 ASCII face. This will be automatically undone the next time
16825 get_next_display_element returns a multibyte character. Note
16826 that the character will always be single byte in unibyte
16827 text. */
16828 if (!ASCII_CHAR_P (it->c))
16829 {
16830 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16831 }
16832
16833 if (FRAME_WINDOW_P (f))
16834 {
16835 /* If the row is empty, add a space with the current face of IT,
16836 so that we know which face to draw. */
16837 if (it->glyph_row->used[TEXT_AREA] == 0)
16838 {
16839 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16840 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16841 it->glyph_row->used[TEXT_AREA] = 1;
16842 }
16843 #ifdef HAVE_WINDOW_SYSTEM
16844 if (it->glyph_row->reversed_p)
16845 {
16846 /* Prepend a stretch glyph to the row, such that the
16847 rightmost glyph will be drawn flushed all the way to the
16848 right margin of the window. The stretch glyph that will
16849 occupy the empty space, if any, to the left of the
16850 glyphs. */
16851 struct font *font = face->font ? face->font : FRAME_FONT (f);
16852 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16853 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16854 struct glyph *g;
16855 int row_width, stretch_ascent, stretch_width;
16856 struct text_pos saved_pos;
16857 int saved_face_id, saved_avoid_cursor;
16858
16859 for (row_width = 0, g = row_start; g < row_end; g++)
16860 row_width += g->pixel_width;
16861 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16862 if (stretch_width > 0)
16863 {
16864 stretch_ascent =
16865 (((it->ascent + it->descent)
16866 * FONT_BASE (font)) / FONT_HEIGHT (font));
16867 saved_pos = it->position;
16868 bzero (&it->position, sizeof it->position);
16869 saved_avoid_cursor = it->avoid_cursor_p;
16870 it->avoid_cursor_p = 1;
16871 saved_face_id = it->face_id;
16872 /* The last row should get the default face, to avoid
16873 painting the rest of the window with the region face,
16874 if the region ends at ZV. */
16875 if (it->glyph_row->ends_at_zv_p)
16876 it->face_id = DEFAULT_FACE_ID;
16877 else
16878 it->face_id = face->id;
16879 append_stretch_glyph (it, make_number (0), stretch_width,
16880 it->ascent + it->descent, stretch_ascent);
16881 it->position = saved_pos;
16882 it->avoid_cursor_p = saved_avoid_cursor;
16883 it->face_id = saved_face_id;
16884 }
16885 }
16886 #endif /* HAVE_WINDOW_SYSTEM */
16887 }
16888 else
16889 {
16890 /* Save some values that must not be changed. */
16891 int saved_x = it->current_x;
16892 struct text_pos saved_pos;
16893 Lisp_Object saved_object;
16894 enum display_element_type saved_what = it->what;
16895 int saved_face_id = it->face_id;
16896
16897 saved_object = it->object;
16898 saved_pos = it->position;
16899
16900 it->what = IT_CHARACTER;
16901 bzero (&it->position, sizeof it->position);
16902 it->object = make_number (0);
16903 it->c = ' ';
16904 it->len = 1;
16905 it->face_id = face->id;
16906
16907 PRODUCE_GLYPHS (it);
16908
16909 while (it->current_x <= it->last_visible_x)
16910 PRODUCE_GLYPHS (it);
16911
16912 /* Don't count these blanks really. It would let us insert a left
16913 truncation glyph below and make us set the cursor on them, maybe. */
16914 it->current_x = saved_x;
16915 it->object = saved_object;
16916 it->position = saved_pos;
16917 it->what = saved_what;
16918 it->face_id = saved_face_id;
16919 }
16920 }
16921
16922
16923 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16924 trailing whitespace. */
16925
16926 static int
16927 trailing_whitespace_p (charpos)
16928 int charpos;
16929 {
16930 int bytepos = CHAR_TO_BYTE (charpos);
16931 int c = 0;
16932
16933 while (bytepos < ZV_BYTE
16934 && (c = FETCH_CHAR (bytepos),
16935 c == ' ' || c == '\t'))
16936 ++bytepos;
16937
16938 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16939 {
16940 if (bytepos != PT_BYTE)
16941 return 1;
16942 }
16943 return 0;
16944 }
16945
16946
16947 /* Highlight trailing whitespace, if any, in ROW. */
16948
16949 void
16950 highlight_trailing_whitespace (f, row)
16951 struct frame *f;
16952 struct glyph_row *row;
16953 {
16954 int used = row->used[TEXT_AREA];
16955
16956 if (used)
16957 {
16958 struct glyph *start = row->glyphs[TEXT_AREA];
16959 struct glyph *glyph = start + used - 1;
16960
16961 if (row->reversed_p)
16962 {
16963 /* Right-to-left rows need to be processed in the opposite
16964 direction, so swap the edge pointers. */
16965 glyph = start;
16966 start = row->glyphs[TEXT_AREA] + used - 1;
16967 }
16968
16969 /* Skip over glyphs inserted to display the cursor at the
16970 end of a line, for extending the face of the last glyph
16971 to the end of the line on terminals, and for truncation
16972 and continuation glyphs. */
16973 if (!row->reversed_p)
16974 {
16975 while (glyph >= start
16976 && glyph->type == CHAR_GLYPH
16977 && INTEGERP (glyph->object))
16978 --glyph;
16979 }
16980 else
16981 {
16982 while (glyph <= start
16983 && glyph->type == CHAR_GLYPH
16984 && INTEGERP (glyph->object))
16985 ++glyph;
16986 }
16987
16988 /* If last glyph is a space or stretch, and it's trailing
16989 whitespace, set the face of all trailing whitespace glyphs in
16990 IT->glyph_row to `trailing-whitespace'. */
16991 if ((row->reversed_p ? glyph <= start : glyph >= start)
16992 && BUFFERP (glyph->object)
16993 && (glyph->type == STRETCH_GLYPH
16994 || (glyph->type == CHAR_GLYPH
16995 && glyph->u.ch == ' '))
16996 && trailing_whitespace_p (glyph->charpos))
16997 {
16998 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16999 if (face_id < 0)
17000 return;
17001
17002 if (!row->reversed_p)
17003 {
17004 while (glyph >= start
17005 && BUFFERP (glyph->object)
17006 && (glyph->type == STRETCH_GLYPH
17007 || (glyph->type == CHAR_GLYPH
17008 && glyph->u.ch == ' ')))
17009 (glyph--)->face_id = face_id;
17010 }
17011 else
17012 {
17013 while (glyph <= start
17014 && BUFFERP (glyph->object)
17015 && (glyph->type == STRETCH_GLYPH
17016 || (glyph->type == CHAR_GLYPH
17017 && glyph->u.ch == ' ')))
17018 (glyph++)->face_id = face_id;
17019 }
17020 }
17021 }
17022 }
17023
17024
17025 /* Value is non-zero if glyph row ROW in window W should be
17026 used to hold the cursor. */
17027
17028 static int
17029 cursor_row_p (w, row)
17030 struct window *w;
17031 struct glyph_row *row;
17032 {
17033 int cursor_row_p = 1;
17034
17035 if (PT == MATRIX_ROW_END_CHARPOS (row))
17036 {
17037 /* Suppose the row ends on a string.
17038 Unless the row is continued, that means it ends on a newline
17039 in the string. If it's anything other than a display string
17040 (e.g. a before-string from an overlay), we don't want the
17041 cursor there. (This heuristic seems to give the optimal
17042 behavior for the various types of multi-line strings.) */
17043 if (CHARPOS (row->end.string_pos) >= 0)
17044 {
17045 if (row->continued_p)
17046 cursor_row_p = 1;
17047 else
17048 {
17049 /* Check for `display' property. */
17050 struct glyph *beg = row->glyphs[TEXT_AREA];
17051 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17052 struct glyph *glyph;
17053
17054 cursor_row_p = 0;
17055 for (glyph = end; glyph >= beg; --glyph)
17056 if (STRINGP (glyph->object))
17057 {
17058 Lisp_Object prop
17059 = Fget_char_property (make_number (PT),
17060 Qdisplay, Qnil);
17061 cursor_row_p =
17062 (!NILP (prop)
17063 && display_prop_string_p (prop, glyph->object));
17064 break;
17065 }
17066 }
17067 }
17068 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17069 {
17070 /* If the row ends in middle of a real character,
17071 and the line is continued, we want the cursor here.
17072 That's because MATRIX_ROW_END_CHARPOS would equal
17073 PT if PT is before the character. */
17074 if (!row->ends_in_ellipsis_p)
17075 cursor_row_p = row->continued_p;
17076 else
17077 /* If the row ends in an ellipsis, then
17078 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
17079 We want that position to be displayed after the ellipsis. */
17080 cursor_row_p = 0;
17081 }
17082 /* If the row ends at ZV, display the cursor at the end of that
17083 row instead of at the start of the row below. */
17084 else if (row->ends_at_zv_p)
17085 cursor_row_p = 1;
17086 else
17087 cursor_row_p = 0;
17088 }
17089
17090 return cursor_row_p;
17091 }
17092
17093 \f
17094
17095 /* Push the display property PROP so that it will be rendered at the
17096 current position in IT. Return 1 if PROP was successfully pushed,
17097 0 otherwise. */
17098
17099 static int
17100 push_display_prop (struct it *it, Lisp_Object prop)
17101 {
17102 push_it (it);
17103
17104 if (STRINGP (prop))
17105 {
17106 if (SCHARS (prop) == 0)
17107 {
17108 pop_it (it);
17109 return 0;
17110 }
17111
17112 it->string = prop;
17113 it->multibyte_p = STRING_MULTIBYTE (it->string);
17114 it->current.overlay_string_index = -1;
17115 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17116 it->end_charpos = it->string_nchars = SCHARS (it->string);
17117 it->method = GET_FROM_STRING;
17118 it->stop_charpos = 0;
17119 }
17120 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17121 {
17122 it->method = GET_FROM_STRETCH;
17123 it->object = prop;
17124 }
17125 #ifdef HAVE_WINDOW_SYSTEM
17126 else if (IMAGEP (prop))
17127 {
17128 it->what = IT_IMAGE;
17129 it->image_id = lookup_image (it->f, prop);
17130 it->method = GET_FROM_IMAGE;
17131 }
17132 #endif /* HAVE_WINDOW_SYSTEM */
17133 else
17134 {
17135 pop_it (it); /* bogus display property, give up */
17136 return 0;
17137 }
17138
17139 return 1;
17140 }
17141
17142 /* Return the character-property PROP at the current position in IT. */
17143
17144 static Lisp_Object
17145 get_it_property (it, prop)
17146 struct it *it;
17147 Lisp_Object prop;
17148 {
17149 Lisp_Object position;
17150
17151 if (STRINGP (it->object))
17152 position = make_number (IT_STRING_CHARPOS (*it));
17153 else if (BUFFERP (it->object))
17154 position = make_number (IT_CHARPOS (*it));
17155 else
17156 return Qnil;
17157
17158 return Fget_char_property (position, prop, it->object);
17159 }
17160
17161 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17162
17163 static void
17164 handle_line_prefix (struct it *it)
17165 {
17166 Lisp_Object prefix;
17167 if (it->continuation_lines_width > 0)
17168 {
17169 prefix = get_it_property (it, Qwrap_prefix);
17170 if (NILP (prefix))
17171 prefix = Vwrap_prefix;
17172 }
17173 else
17174 {
17175 prefix = get_it_property (it, Qline_prefix);
17176 if (NILP (prefix))
17177 prefix = Vline_prefix;
17178 }
17179 if (! NILP (prefix) && push_display_prop (it, prefix))
17180 {
17181 /* If the prefix is wider than the window, and we try to wrap
17182 it, it would acquire its own wrap prefix, and so on till the
17183 iterator stack overflows. So, don't wrap the prefix. */
17184 it->line_wrap = TRUNCATE;
17185 it->avoid_cursor_p = 1;
17186 }
17187 }
17188
17189 \f
17190
17191 /* Construct the glyph row IT->glyph_row in the desired matrix of
17192 IT->w from text at the current position of IT. See dispextern.h
17193 for an overview of struct it. Value is non-zero if
17194 IT->glyph_row displays text, as opposed to a line displaying ZV
17195 only. */
17196
17197 static int
17198 display_line (it)
17199 struct it *it;
17200 {
17201 struct glyph_row *row = it->glyph_row;
17202 Lisp_Object overlay_arrow_string;
17203 struct it wrap_it;
17204 int may_wrap = 0, wrap_x;
17205 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17206 int wrap_row_phys_ascent, wrap_row_phys_height;
17207 int wrap_row_extra_line_spacing;
17208 struct display_pos row_end;
17209 int cvpos;
17210
17211 /* We always start displaying at hpos zero even if hscrolled. */
17212 xassert (it->hpos == 0 && it->current_x == 0);
17213
17214 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17215 >= it->w->desired_matrix->nrows)
17216 {
17217 it->w->nrows_scale_factor++;
17218 fonts_changed_p = 1;
17219 return 0;
17220 }
17221
17222 /* Is IT->w showing the region? */
17223 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17224
17225 /* Clear the result glyph row and enable it. */
17226 prepare_desired_row (row);
17227
17228 row->y = it->current_y;
17229 row->start = it->start;
17230 row->continuation_lines_width = it->continuation_lines_width;
17231 row->displays_text_p = 1;
17232 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17233 it->starts_in_middle_of_char_p = 0;
17234
17235 /* Arrange the overlays nicely for our purposes. Usually, we call
17236 display_line on only one line at a time, in which case this
17237 can't really hurt too much, or we call it on lines which appear
17238 one after another in the buffer, in which case all calls to
17239 recenter_overlay_lists but the first will be pretty cheap. */
17240 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17241
17242 /* Move over display elements that are not visible because we are
17243 hscrolled. This may stop at an x-position < IT->first_visible_x
17244 if the first glyph is partially visible or if we hit a line end. */
17245 if (it->current_x < it->first_visible_x)
17246 {
17247 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17248 MOVE_TO_POS | MOVE_TO_X);
17249 }
17250 else
17251 {
17252 /* We only do this when not calling `move_it_in_display_line_to'
17253 above, because move_it_in_display_line_to calls
17254 handle_line_prefix itself. */
17255 handle_line_prefix (it);
17256 }
17257
17258 /* Get the initial row height. This is either the height of the
17259 text hscrolled, if there is any, or zero. */
17260 row->ascent = it->max_ascent;
17261 row->height = it->max_ascent + it->max_descent;
17262 row->phys_ascent = it->max_phys_ascent;
17263 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17264 row->extra_line_spacing = it->max_extra_line_spacing;
17265
17266 /* Loop generating characters. The loop is left with IT on the next
17267 character to display. */
17268 while (1)
17269 {
17270 int n_glyphs_before, hpos_before, x_before;
17271 int x, i, nglyphs;
17272 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17273
17274 /* Retrieve the next thing to display. Value is zero if end of
17275 buffer reached. */
17276 if (!get_next_display_element (it))
17277 {
17278 /* Maybe add a space at the end of this line that is used to
17279 display the cursor there under X. Set the charpos of the
17280 first glyph of blank lines not corresponding to any text
17281 to -1. */
17282 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17283 row->exact_window_width_line_p = 1;
17284 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17285 || row->used[TEXT_AREA] == 0)
17286 {
17287 row->glyphs[TEXT_AREA]->charpos = -1;
17288 row->displays_text_p = 0;
17289
17290 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17291 && (!MINI_WINDOW_P (it->w)
17292 || (minibuf_level && EQ (it->window, minibuf_window))))
17293 row->indicate_empty_line_p = 1;
17294 }
17295
17296 it->continuation_lines_width = 0;
17297 row->ends_at_zv_p = 1;
17298 /* A row that displays right-to-left text must always have
17299 its last face extended all the way to the end of line,
17300 even if this row ends in ZV. */
17301 if (row->reversed_p)
17302 extend_face_to_end_of_line (it);
17303 break;
17304 }
17305
17306 /* Now, get the metrics of what we want to display. This also
17307 generates glyphs in `row' (which is IT->glyph_row). */
17308 n_glyphs_before = row->used[TEXT_AREA];
17309 x = it->current_x;
17310
17311 /* Remember the line height so far in case the next element doesn't
17312 fit on the line. */
17313 if (it->line_wrap != TRUNCATE)
17314 {
17315 ascent = it->max_ascent;
17316 descent = it->max_descent;
17317 phys_ascent = it->max_phys_ascent;
17318 phys_descent = it->max_phys_descent;
17319
17320 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17321 {
17322 if (IT_DISPLAYING_WHITESPACE (it))
17323 may_wrap = 1;
17324 else if (may_wrap)
17325 {
17326 wrap_it = *it;
17327 wrap_x = x;
17328 wrap_row_used = row->used[TEXT_AREA];
17329 wrap_row_ascent = row->ascent;
17330 wrap_row_height = row->height;
17331 wrap_row_phys_ascent = row->phys_ascent;
17332 wrap_row_phys_height = row->phys_height;
17333 wrap_row_extra_line_spacing = row->extra_line_spacing;
17334 may_wrap = 0;
17335 }
17336 }
17337 }
17338
17339 PRODUCE_GLYPHS (it);
17340
17341 /* If this display element was in marginal areas, continue with
17342 the next one. */
17343 if (it->area != TEXT_AREA)
17344 {
17345 row->ascent = max (row->ascent, it->max_ascent);
17346 row->height = max (row->height, it->max_ascent + it->max_descent);
17347 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17348 row->phys_height = max (row->phys_height,
17349 it->max_phys_ascent + it->max_phys_descent);
17350 row->extra_line_spacing = max (row->extra_line_spacing,
17351 it->max_extra_line_spacing);
17352 set_iterator_to_next (it, 1);
17353 continue;
17354 }
17355
17356 /* Does the display element fit on the line? If we truncate
17357 lines, we should draw past the right edge of the window. If
17358 we don't truncate, we want to stop so that we can display the
17359 continuation glyph before the right margin. If lines are
17360 continued, there are two possible strategies for characters
17361 resulting in more than 1 glyph (e.g. tabs): Display as many
17362 glyphs as possible in this line and leave the rest for the
17363 continuation line, or display the whole element in the next
17364 line. Original redisplay did the former, so we do it also. */
17365 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17366 hpos_before = it->hpos;
17367 x_before = x;
17368
17369 if (/* Not a newline. */
17370 nglyphs > 0
17371 /* Glyphs produced fit entirely in the line. */
17372 && it->current_x < it->last_visible_x)
17373 {
17374 it->hpos += nglyphs;
17375 row->ascent = max (row->ascent, it->max_ascent);
17376 row->height = max (row->height, it->max_ascent + it->max_descent);
17377 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17378 row->phys_height = max (row->phys_height,
17379 it->max_phys_ascent + it->max_phys_descent);
17380 row->extra_line_spacing = max (row->extra_line_spacing,
17381 it->max_extra_line_spacing);
17382 if (it->current_x - it->pixel_width < it->first_visible_x)
17383 row->x = x - it->first_visible_x;
17384 }
17385 else
17386 {
17387 int new_x;
17388 struct glyph *glyph;
17389
17390 for (i = 0; i < nglyphs; ++i, x = new_x)
17391 {
17392 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17393 new_x = x + glyph->pixel_width;
17394
17395 if (/* Lines are continued. */
17396 it->line_wrap != TRUNCATE
17397 && (/* Glyph doesn't fit on the line. */
17398 new_x > it->last_visible_x
17399 /* Or it fits exactly on a window system frame. */
17400 || (new_x == it->last_visible_x
17401 && FRAME_WINDOW_P (it->f))))
17402 {
17403 /* End of a continued line. */
17404
17405 if (it->hpos == 0
17406 || (new_x == it->last_visible_x
17407 && FRAME_WINDOW_P (it->f)))
17408 {
17409 /* Current glyph is the only one on the line or
17410 fits exactly on the line. We must continue
17411 the line because we can't draw the cursor
17412 after the glyph. */
17413 row->continued_p = 1;
17414 it->current_x = new_x;
17415 it->continuation_lines_width += new_x;
17416 ++it->hpos;
17417 if (i == nglyphs - 1)
17418 {
17419 /* If line-wrap is on, check if a previous
17420 wrap point was found. */
17421 if (wrap_row_used > 0
17422 /* Even if there is a previous wrap
17423 point, continue the line here as
17424 usual, if (i) the previous character
17425 was a space or tab AND (ii) the
17426 current character is not. */
17427 && (!may_wrap
17428 || IT_DISPLAYING_WHITESPACE (it)))
17429 goto back_to_wrap;
17430
17431 set_iterator_to_next (it, 1);
17432 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17433 {
17434 if (!get_next_display_element (it))
17435 {
17436 row->exact_window_width_line_p = 1;
17437 it->continuation_lines_width = 0;
17438 row->continued_p = 0;
17439 row->ends_at_zv_p = 1;
17440 }
17441 else if (ITERATOR_AT_END_OF_LINE_P (it))
17442 {
17443 row->continued_p = 0;
17444 row->exact_window_width_line_p = 1;
17445 }
17446 }
17447 }
17448 }
17449 else if (CHAR_GLYPH_PADDING_P (*glyph)
17450 && !FRAME_WINDOW_P (it->f))
17451 {
17452 /* A padding glyph that doesn't fit on this line.
17453 This means the whole character doesn't fit
17454 on the line. */
17455 row->used[TEXT_AREA] = n_glyphs_before;
17456
17457 /* Fill the rest of the row with continuation
17458 glyphs like in 20.x. */
17459 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17460 < row->glyphs[1 + TEXT_AREA])
17461 produce_special_glyphs (it, IT_CONTINUATION);
17462
17463 row->continued_p = 1;
17464 it->current_x = x_before;
17465 it->continuation_lines_width += x_before;
17466
17467 /* Restore the height to what it was before the
17468 element not fitting on the line. */
17469 it->max_ascent = ascent;
17470 it->max_descent = descent;
17471 it->max_phys_ascent = phys_ascent;
17472 it->max_phys_descent = phys_descent;
17473 }
17474 else if (wrap_row_used > 0)
17475 {
17476 back_to_wrap:
17477 *it = wrap_it;
17478 it->continuation_lines_width += wrap_x;
17479 row->used[TEXT_AREA] = wrap_row_used;
17480 row->ascent = wrap_row_ascent;
17481 row->height = wrap_row_height;
17482 row->phys_ascent = wrap_row_phys_ascent;
17483 row->phys_height = wrap_row_phys_height;
17484 row->extra_line_spacing = wrap_row_extra_line_spacing;
17485 row->continued_p = 1;
17486 row->ends_at_zv_p = 0;
17487 row->exact_window_width_line_p = 0;
17488 it->continuation_lines_width += x;
17489
17490 /* Make sure that a non-default face is extended
17491 up to the right margin of the window. */
17492 extend_face_to_end_of_line (it);
17493 }
17494 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17495 {
17496 /* A TAB that extends past the right edge of the
17497 window. This produces a single glyph on
17498 window system frames. We leave the glyph in
17499 this row and let it fill the row, but don't
17500 consume the TAB. */
17501 it->continuation_lines_width += it->last_visible_x;
17502 row->ends_in_middle_of_char_p = 1;
17503 row->continued_p = 1;
17504 glyph->pixel_width = it->last_visible_x - x;
17505 it->starts_in_middle_of_char_p = 1;
17506 }
17507 else
17508 {
17509 /* Something other than a TAB that draws past
17510 the right edge of the window. Restore
17511 positions to values before the element. */
17512 row->used[TEXT_AREA] = n_glyphs_before + i;
17513
17514 /* Display continuation glyphs. */
17515 if (!FRAME_WINDOW_P (it->f))
17516 produce_special_glyphs (it, IT_CONTINUATION);
17517 row->continued_p = 1;
17518
17519 it->current_x = x_before;
17520 it->continuation_lines_width += x;
17521 extend_face_to_end_of_line (it);
17522
17523 if (nglyphs > 1 && i > 0)
17524 {
17525 row->ends_in_middle_of_char_p = 1;
17526 it->starts_in_middle_of_char_p = 1;
17527 }
17528
17529 /* Restore the height to what it was before the
17530 element not fitting on the line. */
17531 it->max_ascent = ascent;
17532 it->max_descent = descent;
17533 it->max_phys_ascent = phys_ascent;
17534 it->max_phys_descent = phys_descent;
17535 }
17536
17537 break;
17538 }
17539 else if (new_x > it->first_visible_x)
17540 {
17541 /* Increment number of glyphs actually displayed. */
17542 ++it->hpos;
17543
17544 if (x < it->first_visible_x)
17545 /* Glyph is partially visible, i.e. row starts at
17546 negative X position. */
17547 row->x = x - it->first_visible_x;
17548 }
17549 else
17550 {
17551 /* Glyph is completely off the left margin of the
17552 window. This should not happen because of the
17553 move_it_in_display_line at the start of this
17554 function, unless the text display area of the
17555 window is empty. */
17556 xassert (it->first_visible_x <= it->last_visible_x);
17557 }
17558 }
17559
17560 row->ascent = max (row->ascent, it->max_ascent);
17561 row->height = max (row->height, it->max_ascent + it->max_descent);
17562 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17563 row->phys_height = max (row->phys_height,
17564 it->max_phys_ascent + it->max_phys_descent);
17565 row->extra_line_spacing = max (row->extra_line_spacing,
17566 it->max_extra_line_spacing);
17567
17568 /* End of this display line if row is continued. */
17569 if (row->continued_p || row->ends_at_zv_p)
17570 break;
17571 }
17572
17573 at_end_of_line:
17574 /* Is this a line end? If yes, we're also done, after making
17575 sure that a non-default face is extended up to the right
17576 margin of the window. */
17577 if (ITERATOR_AT_END_OF_LINE_P (it))
17578 {
17579 int used_before = row->used[TEXT_AREA];
17580
17581 row->ends_in_newline_from_string_p = STRINGP (it->object);
17582
17583 /* Add a space at the end of the line that is used to
17584 display the cursor there. */
17585 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17586 append_space_for_newline (it, 0);
17587
17588 /* Extend the face to the end of the line. */
17589 extend_face_to_end_of_line (it);
17590
17591 /* Make sure we have the position. */
17592 if (used_before == 0)
17593 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17594
17595 /* Consume the line end. This skips over invisible lines. */
17596 set_iterator_to_next (it, 1);
17597 it->continuation_lines_width = 0;
17598 break;
17599 }
17600
17601 /* Proceed with next display element. Note that this skips
17602 over lines invisible because of selective display. */
17603 set_iterator_to_next (it, 1);
17604
17605 /* If we truncate lines, we are done when the last displayed
17606 glyphs reach past the right margin of the window. */
17607 if (it->line_wrap == TRUNCATE
17608 && (FRAME_WINDOW_P (it->f)
17609 ? (it->current_x >= it->last_visible_x)
17610 : (it->current_x > it->last_visible_x)))
17611 {
17612 /* Maybe add truncation glyphs. */
17613 if (!FRAME_WINDOW_P (it->f))
17614 {
17615 int i, n;
17616
17617 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17618 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17619 break;
17620
17621 for (n = row->used[TEXT_AREA]; i < n; ++i)
17622 {
17623 row->used[TEXT_AREA] = i;
17624 produce_special_glyphs (it, IT_TRUNCATION);
17625 }
17626 }
17627 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17628 {
17629 /* Don't truncate if we can overflow newline into fringe. */
17630 if (!get_next_display_element (it))
17631 {
17632 it->continuation_lines_width = 0;
17633 row->ends_at_zv_p = 1;
17634 row->exact_window_width_line_p = 1;
17635 break;
17636 }
17637 if (ITERATOR_AT_END_OF_LINE_P (it))
17638 {
17639 row->exact_window_width_line_p = 1;
17640 goto at_end_of_line;
17641 }
17642 }
17643
17644 row->truncated_on_right_p = 1;
17645 it->continuation_lines_width = 0;
17646 reseat_at_next_visible_line_start (it, 0);
17647 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17648 it->hpos = hpos_before;
17649 it->current_x = x_before;
17650 break;
17651 }
17652 }
17653
17654 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17655 at the left window margin. */
17656 if (it->first_visible_x
17657 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
17658 {
17659 if (!FRAME_WINDOW_P (it->f))
17660 insert_left_trunc_glyphs (it);
17661 row->truncated_on_left_p = 1;
17662 }
17663
17664 /* If the start of this line is the overlay arrow-position, then
17665 mark this glyph row as the one containing the overlay arrow.
17666 This is clearly a mess with variable size fonts. It would be
17667 better to let it be displayed like cursors under X. */
17668 if ((row->displays_text_p || !overlay_arrow_seen)
17669 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17670 !NILP (overlay_arrow_string)))
17671 {
17672 /* Overlay arrow in window redisplay is a fringe bitmap. */
17673 if (STRINGP (overlay_arrow_string))
17674 {
17675 struct glyph_row *arrow_row
17676 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17677 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17678 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17679 struct glyph *p = row->glyphs[TEXT_AREA];
17680 struct glyph *p2, *end;
17681
17682 /* Copy the arrow glyphs. */
17683 while (glyph < arrow_end)
17684 *p++ = *glyph++;
17685
17686 /* Throw away padding glyphs. */
17687 p2 = p;
17688 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17689 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17690 ++p2;
17691 if (p2 > p)
17692 {
17693 while (p2 < end)
17694 *p++ = *p2++;
17695 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17696 }
17697 }
17698 else
17699 {
17700 xassert (INTEGERP (overlay_arrow_string));
17701 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17702 }
17703 overlay_arrow_seen = 1;
17704 }
17705
17706 /* Compute pixel dimensions of this line. */
17707 compute_line_metrics (it);
17708
17709 /* Remember the position at which this line ends. */
17710 row->end = row_end = it->current;
17711 if (it->bidi_p)
17712 {
17713 /* ROW->start and ROW->end must be the smallest and largest
17714 buffer positions in ROW. But if ROW was bidi-reordered,
17715 these two positions can be anywhere in the row, so we must
17716 rescan all of the ROW's glyphs to find them. */
17717 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17718 lines' rows is implemented for bidi-reordered rows. */
17719 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17720 struct glyph *g;
17721 struct it save_it;
17722 struct text_pos tpos;
17723
17724 for (g = row->glyphs[TEXT_AREA];
17725 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17726 g++)
17727 {
17728 if (BUFFERP (g->object))
17729 {
17730 if (g->charpos > 0 && g->charpos < min_pos)
17731 min_pos = g->charpos;
17732 if (g->charpos > max_pos)
17733 max_pos = g->charpos;
17734 }
17735 }
17736 /* Empty lines have a valid buffer position at their first
17737 glyph, but that glyph's OBJECT is zero, as if it didn't come
17738 from a buffer. If we didn't find any valid buffer positions
17739 in this row, maybe we have such an empty line. */
17740 if (min_pos == ZV + 1 && row->used[TEXT_AREA])
17741 {
17742 for (g = row->glyphs[TEXT_AREA];
17743 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17744 g++)
17745 {
17746 if (INTEGERP (g->object))
17747 {
17748 if (g->charpos > 0 && g->charpos < min_pos)
17749 min_pos = g->charpos;
17750 if (g->charpos > max_pos)
17751 max_pos = g->charpos;
17752 }
17753 }
17754 }
17755 if (min_pos <= ZV)
17756 {
17757 if (min_pos != row->start.pos.charpos)
17758 {
17759 row->start.pos.charpos = min_pos;
17760 row->start.pos.bytepos = CHAR_TO_BYTE (min_pos);
17761 }
17762 if (max_pos == 0)
17763 max_pos = min_pos;
17764 }
17765 /* For ROW->end, we need the position that is _after_ max_pos,
17766 in the logical order, unless we are at ZV. */
17767 if (row->ends_at_zv_p)
17768 {
17769 row_end = row->end = it->current;
17770 if (!row->used[TEXT_AREA])
17771 {
17772 row->start.pos.charpos = row_end.pos.charpos;
17773 row->start.pos.bytepos = row_end.pos.bytepos;
17774 }
17775 }
17776 else if (row->used[TEXT_AREA] && max_pos)
17777 {
17778 SET_TEXT_POS (tpos, max_pos + 1, CHAR_TO_BYTE (max_pos + 1));
17779 row_end = it->current;
17780 row_end.pos = tpos;
17781 /* If the character at max_pos+1 is a newline, skip that as
17782 well. Note that this may skip some invisible text. */
17783 if (FETCH_CHAR (tpos.bytepos) == '\n'
17784 || (FETCH_CHAR (tpos.bytepos) == '\r' && it->selective))
17785 {
17786 save_it = *it;
17787 it->bidi_p = 0;
17788 reseat_1 (it, tpos, 0);
17789 set_iterator_to_next (it, 1);
17790 /* Record the position after the newline of a continued
17791 row. We will need that to set ROW->end of the last
17792 row produced for a continued line. */
17793 if (row->continued_p)
17794 {
17795 save_it.eol_pos.charpos = IT_CHARPOS (*it);
17796 save_it.eol_pos.bytepos = IT_BYTEPOS (*it);
17797 }
17798 else
17799 {
17800 row_end = it->current;
17801 save_it.eol_pos.charpos = save_it.eol_pos.bytepos = 0;
17802 }
17803 *it = save_it;
17804 }
17805 else if (!row->continued_p
17806 && row->continuation_lines_width
17807 && it->eol_pos.charpos > 0)
17808 {
17809 /* Last row of a continued line. Use the position
17810 recorded in ROW->eol_pos, to the effect that the
17811 newline belongs to this row, not to the row which
17812 displays the character with the largest buffer
17813 position. */
17814 row_end.pos = it->eol_pos;
17815 it->eol_pos.charpos = it->eol_pos.bytepos = 0;
17816 }
17817 row->end = row_end;
17818 }
17819 }
17820
17821 /* Record whether this row ends inside an ellipsis. */
17822 row->ends_in_ellipsis_p
17823 = (it->method == GET_FROM_DISPLAY_VECTOR
17824 && it->ellipsis_p);
17825
17826 /* Save fringe bitmaps in this row. */
17827 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17828 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17829 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17830 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17831
17832 it->left_user_fringe_bitmap = 0;
17833 it->left_user_fringe_face_id = 0;
17834 it->right_user_fringe_bitmap = 0;
17835 it->right_user_fringe_face_id = 0;
17836
17837 /* Maybe set the cursor. */
17838 cvpos = it->w->cursor.vpos;
17839 if ((cvpos < 0
17840 /* In bidi-reordered rows, keep checking for proper cursor
17841 position even if one has been found already, because buffer
17842 positions in such rows change non-linearly with ROW->VPOS,
17843 when a line is continued. One exception: when we are at ZV,
17844 display cursor on the first suitable glyph row, since all
17845 the empty rows after that also have their position set to ZV. */
17846 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17847 lines' rows is implemented for bidi-reordered rows. */
17848 || (it->bidi_p
17849 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17850 && PT >= MATRIX_ROW_START_CHARPOS (row)
17851 && PT <= MATRIX_ROW_END_CHARPOS (row)
17852 && cursor_row_p (it->w, row))
17853 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17854
17855 /* Highlight trailing whitespace. */
17856 if (!NILP (Vshow_trailing_whitespace))
17857 highlight_trailing_whitespace (it->f, it->glyph_row);
17858
17859 /* Prepare for the next line. This line starts horizontally at (X
17860 HPOS) = (0 0). Vertical positions are incremented. As a
17861 convenience for the caller, IT->glyph_row is set to the next
17862 row to be used. */
17863 it->current_x = it->hpos = 0;
17864 it->current_y += row->height;
17865 ++it->vpos;
17866 ++it->glyph_row;
17867 /* The next row should use same value of the reversed_p flag as this
17868 one. set_iterator_to_next decides when it's a new paragraph, and
17869 PRODUCE_GLYPHS recomputes the value of the flag accordingly. */
17870 it->glyph_row->reversed_p = row->reversed_p;
17871 it->start = row_end;
17872 return row->displays_text_p;
17873 }
17874
17875
17876 \f
17877 /***********************************************************************
17878 Menu Bar
17879 ***********************************************************************/
17880
17881 /* Redisplay the menu bar in the frame for window W.
17882
17883 The menu bar of X frames that don't have X toolkit support is
17884 displayed in a special window W->frame->menu_bar_window.
17885
17886 The menu bar of terminal frames is treated specially as far as
17887 glyph matrices are concerned. Menu bar lines are not part of
17888 windows, so the update is done directly on the frame matrix rows
17889 for the menu bar. */
17890
17891 static void
17892 display_menu_bar (w)
17893 struct window *w;
17894 {
17895 struct frame *f = XFRAME (WINDOW_FRAME (w));
17896 struct it it;
17897 Lisp_Object items;
17898 int i;
17899
17900 /* Don't do all this for graphical frames. */
17901 #ifdef HAVE_NTGUI
17902 if (FRAME_W32_P (f))
17903 return;
17904 #endif
17905 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17906 if (FRAME_X_P (f))
17907 return;
17908 #endif
17909
17910 #ifdef HAVE_NS
17911 if (FRAME_NS_P (f))
17912 return;
17913 #endif /* HAVE_NS */
17914
17915 #ifdef USE_X_TOOLKIT
17916 xassert (!FRAME_WINDOW_P (f));
17917 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17918 it.first_visible_x = 0;
17919 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17920 #else /* not USE_X_TOOLKIT */
17921 if (FRAME_WINDOW_P (f))
17922 {
17923 /* Menu bar lines are displayed in the desired matrix of the
17924 dummy window menu_bar_window. */
17925 struct window *menu_w;
17926 xassert (WINDOWP (f->menu_bar_window));
17927 menu_w = XWINDOW (f->menu_bar_window);
17928 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17929 MENU_FACE_ID);
17930 it.first_visible_x = 0;
17931 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17932 }
17933 else
17934 {
17935 /* This is a TTY frame, i.e. character hpos/vpos are used as
17936 pixel x/y. */
17937 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17938 MENU_FACE_ID);
17939 it.first_visible_x = 0;
17940 it.last_visible_x = FRAME_COLS (f);
17941 }
17942 #endif /* not USE_X_TOOLKIT */
17943
17944 if (! mode_line_inverse_video)
17945 /* Force the menu-bar to be displayed in the default face. */
17946 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17947
17948 /* Clear all rows of the menu bar. */
17949 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17950 {
17951 struct glyph_row *row = it.glyph_row + i;
17952 clear_glyph_row (row);
17953 row->enabled_p = 1;
17954 row->full_width_p = 1;
17955 }
17956
17957 /* Display all items of the menu bar. */
17958 items = FRAME_MENU_BAR_ITEMS (it.f);
17959 for (i = 0; i < XVECTOR (items)->size; i += 4)
17960 {
17961 Lisp_Object string;
17962
17963 /* Stop at nil string. */
17964 string = AREF (items, i + 1);
17965 if (NILP (string))
17966 break;
17967
17968 /* Remember where item was displayed. */
17969 ASET (items, i + 3, make_number (it.hpos));
17970
17971 /* Display the item, pad with one space. */
17972 if (it.current_x < it.last_visible_x)
17973 display_string (NULL, string, Qnil, 0, 0, &it,
17974 SCHARS (string) + 1, 0, 0, -1);
17975 }
17976
17977 /* Fill out the line with spaces. */
17978 if (it.current_x < it.last_visible_x)
17979 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17980
17981 /* Compute the total height of the lines. */
17982 compute_line_metrics (&it);
17983 }
17984
17985
17986 \f
17987 /***********************************************************************
17988 Mode Line
17989 ***********************************************************************/
17990
17991 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17992 FORCE is non-zero, redisplay mode lines unconditionally.
17993 Otherwise, redisplay only mode lines that are garbaged. Value is
17994 the number of windows whose mode lines were redisplayed. */
17995
17996 static int
17997 redisplay_mode_lines (window, force)
17998 Lisp_Object window;
17999 int force;
18000 {
18001 int nwindows = 0;
18002
18003 while (!NILP (window))
18004 {
18005 struct window *w = XWINDOW (window);
18006
18007 if (WINDOWP (w->hchild))
18008 nwindows += redisplay_mode_lines (w->hchild, force);
18009 else if (WINDOWP (w->vchild))
18010 nwindows += redisplay_mode_lines (w->vchild, force);
18011 else if (force
18012 || FRAME_GARBAGED_P (XFRAME (w->frame))
18013 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18014 {
18015 struct text_pos lpoint;
18016 struct buffer *old = current_buffer;
18017
18018 /* Set the window's buffer for the mode line display. */
18019 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18020 set_buffer_internal_1 (XBUFFER (w->buffer));
18021
18022 /* Point refers normally to the selected window. For any
18023 other window, set up appropriate value. */
18024 if (!EQ (window, selected_window))
18025 {
18026 struct text_pos pt;
18027
18028 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18029 if (CHARPOS (pt) < BEGV)
18030 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18031 else if (CHARPOS (pt) > (ZV - 1))
18032 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18033 else
18034 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18035 }
18036
18037 /* Display mode lines. */
18038 clear_glyph_matrix (w->desired_matrix);
18039 if (display_mode_lines (w))
18040 {
18041 ++nwindows;
18042 w->must_be_updated_p = 1;
18043 }
18044
18045 /* Restore old settings. */
18046 set_buffer_internal_1 (old);
18047 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18048 }
18049
18050 window = w->next;
18051 }
18052
18053 return nwindows;
18054 }
18055
18056
18057 /* Display the mode and/or header line of window W. Value is the
18058 sum number of mode lines and header lines displayed. */
18059
18060 static int
18061 display_mode_lines (w)
18062 struct window *w;
18063 {
18064 Lisp_Object old_selected_window, old_selected_frame;
18065 int n = 0;
18066
18067 old_selected_frame = selected_frame;
18068 selected_frame = w->frame;
18069 old_selected_window = selected_window;
18070 XSETWINDOW (selected_window, w);
18071
18072 /* These will be set while the mode line specs are processed. */
18073 line_number_displayed = 0;
18074 w->column_number_displayed = Qnil;
18075
18076 if (WINDOW_WANTS_MODELINE_P (w))
18077 {
18078 struct window *sel_w = XWINDOW (old_selected_window);
18079
18080 /* Select mode line face based on the real selected window. */
18081 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18082 current_buffer->mode_line_format);
18083 ++n;
18084 }
18085
18086 if (WINDOW_WANTS_HEADER_LINE_P (w))
18087 {
18088 display_mode_line (w, HEADER_LINE_FACE_ID,
18089 current_buffer->header_line_format);
18090 ++n;
18091 }
18092
18093 selected_frame = old_selected_frame;
18094 selected_window = old_selected_window;
18095 return n;
18096 }
18097
18098
18099 /* Display mode or header line of window W. FACE_ID specifies which
18100 line to display; it is either MODE_LINE_FACE_ID or
18101 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18102 display. Value is the pixel height of the mode/header line
18103 displayed. */
18104
18105 static int
18106 display_mode_line (w, face_id, format)
18107 struct window *w;
18108 enum face_id face_id;
18109 Lisp_Object format;
18110 {
18111 struct it it;
18112 struct face *face;
18113 int count = SPECPDL_INDEX ();
18114
18115 init_iterator (&it, w, -1, -1, NULL, face_id);
18116 /* Don't extend on a previously drawn mode-line.
18117 This may happen if called from pos_visible_p. */
18118 it.glyph_row->enabled_p = 0;
18119 prepare_desired_row (it.glyph_row);
18120
18121 it.glyph_row->mode_line_p = 1;
18122
18123 if (! mode_line_inverse_video)
18124 /* Force the mode-line to be displayed in the default face. */
18125 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18126
18127 record_unwind_protect (unwind_format_mode_line,
18128 format_mode_line_unwind_data (NULL, Qnil, 0));
18129
18130 mode_line_target = MODE_LINE_DISPLAY;
18131
18132 /* Temporarily make frame's keyboard the current kboard so that
18133 kboard-local variables in the mode_line_format will get the right
18134 values. */
18135 push_kboard (FRAME_KBOARD (it.f));
18136 record_unwind_save_match_data ();
18137 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18138 pop_kboard ();
18139
18140 unbind_to (count, Qnil);
18141
18142 /* Fill up with spaces. */
18143 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18144
18145 compute_line_metrics (&it);
18146 it.glyph_row->full_width_p = 1;
18147 it.glyph_row->continued_p = 0;
18148 it.glyph_row->truncated_on_left_p = 0;
18149 it.glyph_row->truncated_on_right_p = 0;
18150
18151 /* Make a 3D mode-line have a shadow at its right end. */
18152 face = FACE_FROM_ID (it.f, face_id);
18153 extend_face_to_end_of_line (&it);
18154 if (face->box != FACE_NO_BOX)
18155 {
18156 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18157 + it.glyph_row->used[TEXT_AREA] - 1);
18158 last->right_box_line_p = 1;
18159 }
18160
18161 return it.glyph_row->height;
18162 }
18163
18164 /* Move element ELT in LIST to the front of LIST.
18165 Return the updated list. */
18166
18167 static Lisp_Object
18168 move_elt_to_front (elt, list)
18169 Lisp_Object elt, list;
18170 {
18171 register Lisp_Object tail, prev;
18172 register Lisp_Object tem;
18173
18174 tail = list;
18175 prev = Qnil;
18176 while (CONSP (tail))
18177 {
18178 tem = XCAR (tail);
18179
18180 if (EQ (elt, tem))
18181 {
18182 /* Splice out the link TAIL. */
18183 if (NILP (prev))
18184 list = XCDR (tail);
18185 else
18186 Fsetcdr (prev, XCDR (tail));
18187
18188 /* Now make it the first. */
18189 Fsetcdr (tail, list);
18190 return tail;
18191 }
18192 else
18193 prev = tail;
18194 tail = XCDR (tail);
18195 QUIT;
18196 }
18197
18198 /* Not found--return unchanged LIST. */
18199 return list;
18200 }
18201
18202 /* Contribute ELT to the mode line for window IT->w. How it
18203 translates into text depends on its data type.
18204
18205 IT describes the display environment in which we display, as usual.
18206
18207 DEPTH is the depth in recursion. It is used to prevent
18208 infinite recursion here.
18209
18210 FIELD_WIDTH is the number of characters the display of ELT should
18211 occupy in the mode line, and PRECISION is the maximum number of
18212 characters to display from ELT's representation. See
18213 display_string for details.
18214
18215 Returns the hpos of the end of the text generated by ELT.
18216
18217 PROPS is a property list to add to any string we encounter.
18218
18219 If RISKY is nonzero, remove (disregard) any properties in any string
18220 we encounter, and ignore :eval and :propertize.
18221
18222 The global variable `mode_line_target' determines whether the
18223 output is passed to `store_mode_line_noprop',
18224 `store_mode_line_string', or `display_string'. */
18225
18226 static int
18227 display_mode_element (it, depth, field_width, precision, elt, props, risky)
18228 struct it *it;
18229 int depth;
18230 int field_width, precision;
18231 Lisp_Object elt, props;
18232 int risky;
18233 {
18234 int n = 0, field, prec;
18235 int literal = 0;
18236
18237 tail_recurse:
18238 if (depth > 100)
18239 elt = build_string ("*too-deep*");
18240
18241 depth++;
18242
18243 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18244 {
18245 case Lisp_String:
18246 {
18247 /* A string: output it and check for %-constructs within it. */
18248 unsigned char c;
18249 int offset = 0;
18250
18251 if (SCHARS (elt) > 0
18252 && (!NILP (props) || risky))
18253 {
18254 Lisp_Object oprops, aelt;
18255 oprops = Ftext_properties_at (make_number (0), elt);
18256
18257 /* If the starting string's properties are not what
18258 we want, translate the string. Also, if the string
18259 is risky, do that anyway. */
18260
18261 if (NILP (Fequal (props, oprops)) || risky)
18262 {
18263 /* If the starting string has properties,
18264 merge the specified ones onto the existing ones. */
18265 if (! NILP (oprops) && !risky)
18266 {
18267 Lisp_Object tem;
18268
18269 oprops = Fcopy_sequence (oprops);
18270 tem = props;
18271 while (CONSP (tem))
18272 {
18273 oprops = Fplist_put (oprops, XCAR (tem),
18274 XCAR (XCDR (tem)));
18275 tem = XCDR (XCDR (tem));
18276 }
18277 props = oprops;
18278 }
18279
18280 aelt = Fassoc (elt, mode_line_proptrans_alist);
18281 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18282 {
18283 /* AELT is what we want. Move it to the front
18284 without consing. */
18285 elt = XCAR (aelt);
18286 mode_line_proptrans_alist
18287 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18288 }
18289 else
18290 {
18291 Lisp_Object tem;
18292
18293 /* If AELT has the wrong props, it is useless.
18294 so get rid of it. */
18295 if (! NILP (aelt))
18296 mode_line_proptrans_alist
18297 = Fdelq (aelt, mode_line_proptrans_alist);
18298
18299 elt = Fcopy_sequence (elt);
18300 Fset_text_properties (make_number (0), Flength (elt),
18301 props, elt);
18302 /* Add this item to mode_line_proptrans_alist. */
18303 mode_line_proptrans_alist
18304 = Fcons (Fcons (elt, props),
18305 mode_line_proptrans_alist);
18306 /* Truncate mode_line_proptrans_alist
18307 to at most 50 elements. */
18308 tem = Fnthcdr (make_number (50),
18309 mode_line_proptrans_alist);
18310 if (! NILP (tem))
18311 XSETCDR (tem, Qnil);
18312 }
18313 }
18314 }
18315
18316 offset = 0;
18317
18318 if (literal)
18319 {
18320 prec = precision - n;
18321 switch (mode_line_target)
18322 {
18323 case MODE_LINE_NOPROP:
18324 case MODE_LINE_TITLE:
18325 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18326 break;
18327 case MODE_LINE_STRING:
18328 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18329 break;
18330 case MODE_LINE_DISPLAY:
18331 n += display_string (NULL, elt, Qnil, 0, 0, it,
18332 0, prec, 0, STRING_MULTIBYTE (elt));
18333 break;
18334 }
18335
18336 break;
18337 }
18338
18339 /* Handle the non-literal case. */
18340
18341 while ((precision <= 0 || n < precision)
18342 && SREF (elt, offset) != 0
18343 && (mode_line_target != MODE_LINE_DISPLAY
18344 || it->current_x < it->last_visible_x))
18345 {
18346 int last_offset = offset;
18347
18348 /* Advance to end of string or next format specifier. */
18349 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18350 ;
18351
18352 if (offset - 1 != last_offset)
18353 {
18354 int nchars, nbytes;
18355
18356 /* Output to end of string or up to '%'. Field width
18357 is length of string. Don't output more than
18358 PRECISION allows us. */
18359 offset--;
18360
18361 prec = c_string_width (SDATA (elt) + last_offset,
18362 offset - last_offset, precision - n,
18363 &nchars, &nbytes);
18364
18365 switch (mode_line_target)
18366 {
18367 case MODE_LINE_NOPROP:
18368 case MODE_LINE_TITLE:
18369 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18370 break;
18371 case MODE_LINE_STRING:
18372 {
18373 int bytepos = last_offset;
18374 int charpos = string_byte_to_char (elt, bytepos);
18375 int endpos = (precision <= 0
18376 ? string_byte_to_char (elt, offset)
18377 : charpos + nchars);
18378
18379 n += store_mode_line_string (NULL,
18380 Fsubstring (elt, make_number (charpos),
18381 make_number (endpos)),
18382 0, 0, 0, Qnil);
18383 }
18384 break;
18385 case MODE_LINE_DISPLAY:
18386 {
18387 int bytepos = last_offset;
18388 int charpos = string_byte_to_char (elt, bytepos);
18389
18390 if (precision <= 0)
18391 nchars = string_byte_to_char (elt, offset) - charpos;
18392 n += display_string (NULL, elt, Qnil, 0, charpos,
18393 it, 0, nchars, 0,
18394 STRING_MULTIBYTE (elt));
18395 }
18396 break;
18397 }
18398 }
18399 else /* c == '%' */
18400 {
18401 int percent_position = offset;
18402
18403 /* Get the specified minimum width. Zero means
18404 don't pad. */
18405 field = 0;
18406 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18407 field = field * 10 + c - '0';
18408
18409 /* Don't pad beyond the total padding allowed. */
18410 if (field_width - n > 0 && field > field_width - n)
18411 field = field_width - n;
18412
18413 /* Note that either PRECISION <= 0 or N < PRECISION. */
18414 prec = precision - n;
18415
18416 if (c == 'M')
18417 n += display_mode_element (it, depth, field, prec,
18418 Vglobal_mode_string, props,
18419 risky);
18420 else if (c != 0)
18421 {
18422 int multibyte;
18423 int bytepos, charpos;
18424 unsigned char *spec;
18425 Lisp_Object string;
18426
18427 bytepos = percent_position;
18428 charpos = (STRING_MULTIBYTE (elt)
18429 ? string_byte_to_char (elt, bytepos)
18430 : bytepos);
18431 spec = decode_mode_spec (it->w, c, field, prec, &string);
18432 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18433
18434 switch (mode_line_target)
18435 {
18436 case MODE_LINE_NOPROP:
18437 case MODE_LINE_TITLE:
18438 n += store_mode_line_noprop (spec, field, prec);
18439 break;
18440 case MODE_LINE_STRING:
18441 {
18442 int len = strlen (spec);
18443 Lisp_Object tem = make_string (spec, len);
18444 props = Ftext_properties_at (make_number (charpos), elt);
18445 /* Should only keep face property in props */
18446 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18447 }
18448 break;
18449 case MODE_LINE_DISPLAY:
18450 {
18451 int nglyphs_before, nwritten;
18452
18453 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18454 nwritten = display_string (spec, string, elt,
18455 charpos, 0, it,
18456 field, prec, 0,
18457 multibyte);
18458
18459 /* Assign to the glyphs written above the
18460 string where the `%x' came from, position
18461 of the `%'. */
18462 if (nwritten > 0)
18463 {
18464 struct glyph *glyph
18465 = (it->glyph_row->glyphs[TEXT_AREA]
18466 + nglyphs_before);
18467 int i;
18468
18469 for (i = 0; i < nwritten; ++i)
18470 {
18471 glyph[i].object = elt;
18472 glyph[i].charpos = charpos;
18473 }
18474
18475 n += nwritten;
18476 }
18477 }
18478 break;
18479 }
18480 }
18481 else /* c == 0 */
18482 break;
18483 }
18484 }
18485 }
18486 break;
18487
18488 case Lisp_Symbol:
18489 /* A symbol: process the value of the symbol recursively
18490 as if it appeared here directly. Avoid error if symbol void.
18491 Special case: if value of symbol is a string, output the string
18492 literally. */
18493 {
18494 register Lisp_Object tem;
18495
18496 /* If the variable is not marked as risky to set
18497 then its contents are risky to use. */
18498 if (NILP (Fget (elt, Qrisky_local_variable)))
18499 risky = 1;
18500
18501 tem = Fboundp (elt);
18502 if (!NILP (tem))
18503 {
18504 tem = Fsymbol_value (elt);
18505 /* If value is a string, output that string literally:
18506 don't check for % within it. */
18507 if (STRINGP (tem))
18508 literal = 1;
18509
18510 if (!EQ (tem, elt))
18511 {
18512 /* Give up right away for nil or t. */
18513 elt = tem;
18514 goto tail_recurse;
18515 }
18516 }
18517 }
18518 break;
18519
18520 case Lisp_Cons:
18521 {
18522 register Lisp_Object car, tem;
18523
18524 /* A cons cell: five distinct cases.
18525 If first element is :eval or :propertize, do something special.
18526 If first element is a string or a cons, process all the elements
18527 and effectively concatenate them.
18528 If first element is a negative number, truncate displaying cdr to
18529 at most that many characters. If positive, pad (with spaces)
18530 to at least that many characters.
18531 If first element is a symbol, process the cadr or caddr recursively
18532 according to whether the symbol's value is non-nil or nil. */
18533 car = XCAR (elt);
18534 if (EQ (car, QCeval))
18535 {
18536 /* An element of the form (:eval FORM) means evaluate FORM
18537 and use the result as mode line elements. */
18538
18539 if (risky)
18540 break;
18541
18542 if (CONSP (XCDR (elt)))
18543 {
18544 Lisp_Object spec;
18545 spec = safe_eval (XCAR (XCDR (elt)));
18546 n += display_mode_element (it, depth, field_width - n,
18547 precision - n, spec, props,
18548 risky);
18549 }
18550 }
18551 else if (EQ (car, QCpropertize))
18552 {
18553 /* An element of the form (:propertize ELT PROPS...)
18554 means display ELT but applying properties PROPS. */
18555
18556 if (risky)
18557 break;
18558
18559 if (CONSP (XCDR (elt)))
18560 n += display_mode_element (it, depth, field_width - n,
18561 precision - n, XCAR (XCDR (elt)),
18562 XCDR (XCDR (elt)), risky);
18563 }
18564 else if (SYMBOLP (car))
18565 {
18566 tem = Fboundp (car);
18567 elt = XCDR (elt);
18568 if (!CONSP (elt))
18569 goto invalid;
18570 /* elt is now the cdr, and we know it is a cons cell.
18571 Use its car if CAR has a non-nil value. */
18572 if (!NILP (tem))
18573 {
18574 tem = Fsymbol_value (car);
18575 if (!NILP (tem))
18576 {
18577 elt = XCAR (elt);
18578 goto tail_recurse;
18579 }
18580 }
18581 /* Symbol's value is nil (or symbol is unbound)
18582 Get the cddr of the original list
18583 and if possible find the caddr and use that. */
18584 elt = XCDR (elt);
18585 if (NILP (elt))
18586 break;
18587 else if (!CONSP (elt))
18588 goto invalid;
18589 elt = XCAR (elt);
18590 goto tail_recurse;
18591 }
18592 else if (INTEGERP (car))
18593 {
18594 register int lim = XINT (car);
18595 elt = XCDR (elt);
18596 if (lim < 0)
18597 {
18598 /* Negative int means reduce maximum width. */
18599 if (precision <= 0)
18600 precision = -lim;
18601 else
18602 precision = min (precision, -lim);
18603 }
18604 else if (lim > 0)
18605 {
18606 /* Padding specified. Don't let it be more than
18607 current maximum. */
18608 if (precision > 0)
18609 lim = min (precision, lim);
18610
18611 /* If that's more padding than already wanted, queue it.
18612 But don't reduce padding already specified even if
18613 that is beyond the current truncation point. */
18614 field_width = max (lim, field_width);
18615 }
18616 goto tail_recurse;
18617 }
18618 else if (STRINGP (car) || CONSP (car))
18619 {
18620 Lisp_Object halftail = elt;
18621 int len = 0;
18622
18623 while (CONSP (elt)
18624 && (precision <= 0 || n < precision))
18625 {
18626 n += display_mode_element (it, depth,
18627 /* Do padding only after the last
18628 element in the list. */
18629 (! CONSP (XCDR (elt))
18630 ? field_width - n
18631 : 0),
18632 precision - n, XCAR (elt),
18633 props, risky);
18634 elt = XCDR (elt);
18635 len++;
18636 if ((len & 1) == 0)
18637 halftail = XCDR (halftail);
18638 /* Check for cycle. */
18639 if (EQ (halftail, elt))
18640 break;
18641 }
18642 }
18643 }
18644 break;
18645
18646 default:
18647 invalid:
18648 elt = build_string ("*invalid*");
18649 goto tail_recurse;
18650 }
18651
18652 /* Pad to FIELD_WIDTH. */
18653 if (field_width > 0 && n < field_width)
18654 {
18655 switch (mode_line_target)
18656 {
18657 case MODE_LINE_NOPROP:
18658 case MODE_LINE_TITLE:
18659 n += store_mode_line_noprop ("", field_width - n, 0);
18660 break;
18661 case MODE_LINE_STRING:
18662 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18663 break;
18664 case MODE_LINE_DISPLAY:
18665 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18666 0, 0, 0);
18667 break;
18668 }
18669 }
18670
18671 return n;
18672 }
18673
18674 /* Store a mode-line string element in mode_line_string_list.
18675
18676 If STRING is non-null, display that C string. Otherwise, the Lisp
18677 string LISP_STRING is displayed.
18678
18679 FIELD_WIDTH is the minimum number of output glyphs to produce.
18680 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18681 with spaces. FIELD_WIDTH <= 0 means don't pad.
18682
18683 PRECISION is the maximum number of characters to output from
18684 STRING. PRECISION <= 0 means don't truncate the string.
18685
18686 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18687 properties to the string.
18688
18689 PROPS are the properties to add to the string.
18690 The mode_line_string_face face property is always added to the string.
18691 */
18692
18693 static int
18694 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
18695 char *string;
18696 Lisp_Object lisp_string;
18697 int copy_string;
18698 int field_width;
18699 int precision;
18700 Lisp_Object props;
18701 {
18702 int len;
18703 int n = 0;
18704
18705 if (string != NULL)
18706 {
18707 len = strlen (string);
18708 if (precision > 0 && len > precision)
18709 len = precision;
18710 lisp_string = make_string (string, len);
18711 if (NILP (props))
18712 props = mode_line_string_face_prop;
18713 else if (!NILP (mode_line_string_face))
18714 {
18715 Lisp_Object face = Fplist_get (props, Qface);
18716 props = Fcopy_sequence (props);
18717 if (NILP (face))
18718 face = mode_line_string_face;
18719 else
18720 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18721 props = Fplist_put (props, Qface, face);
18722 }
18723 Fadd_text_properties (make_number (0), make_number (len),
18724 props, lisp_string);
18725 }
18726 else
18727 {
18728 len = XFASTINT (Flength (lisp_string));
18729 if (precision > 0 && len > precision)
18730 {
18731 len = precision;
18732 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18733 precision = -1;
18734 }
18735 if (!NILP (mode_line_string_face))
18736 {
18737 Lisp_Object face;
18738 if (NILP (props))
18739 props = Ftext_properties_at (make_number (0), lisp_string);
18740 face = Fplist_get (props, Qface);
18741 if (NILP (face))
18742 face = mode_line_string_face;
18743 else
18744 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18745 props = Fcons (Qface, Fcons (face, Qnil));
18746 if (copy_string)
18747 lisp_string = Fcopy_sequence (lisp_string);
18748 }
18749 if (!NILP (props))
18750 Fadd_text_properties (make_number (0), make_number (len),
18751 props, lisp_string);
18752 }
18753
18754 if (len > 0)
18755 {
18756 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18757 n += len;
18758 }
18759
18760 if (field_width > len)
18761 {
18762 field_width -= len;
18763 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18764 if (!NILP (props))
18765 Fadd_text_properties (make_number (0), make_number (field_width),
18766 props, lisp_string);
18767 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18768 n += field_width;
18769 }
18770
18771 return n;
18772 }
18773
18774
18775 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18776 1, 4, 0,
18777 doc: /* Format a string out of a mode line format specification.
18778 First arg FORMAT specifies the mode line format (see `mode-line-format'
18779 for details) to use.
18780
18781 Optional second arg FACE specifies the face property to put
18782 on all characters for which no face is specified.
18783 The value t means whatever face the window's mode line currently uses
18784 \(either `mode-line' or `mode-line-inactive', depending).
18785 A value of nil means the default is no face property.
18786 If FACE is an integer, the value string has no text properties.
18787
18788 Optional third and fourth args WINDOW and BUFFER specify the window
18789 and buffer to use as the context for the formatting (defaults
18790 are the selected window and the window's buffer). */)
18791 (format, face, window, buffer)
18792 Lisp_Object format, face, window, buffer;
18793 {
18794 struct it it;
18795 int len;
18796 struct window *w;
18797 struct buffer *old_buffer = NULL;
18798 int face_id = -1;
18799 int no_props = INTEGERP (face);
18800 int count = SPECPDL_INDEX ();
18801 Lisp_Object str;
18802 int string_start = 0;
18803
18804 if (NILP (window))
18805 window = selected_window;
18806 CHECK_WINDOW (window);
18807 w = XWINDOW (window);
18808
18809 if (NILP (buffer))
18810 buffer = w->buffer;
18811 CHECK_BUFFER (buffer);
18812
18813 /* Make formatting the modeline a non-op when noninteractive, otherwise
18814 there will be problems later caused by a partially initialized frame. */
18815 if (NILP (format) || noninteractive)
18816 return empty_unibyte_string;
18817
18818 if (no_props)
18819 face = Qnil;
18820
18821 if (!NILP (face))
18822 {
18823 if (EQ (face, Qt))
18824 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18825 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18826 }
18827
18828 if (face_id < 0)
18829 face_id = DEFAULT_FACE_ID;
18830
18831 if (XBUFFER (buffer) != current_buffer)
18832 old_buffer = current_buffer;
18833
18834 /* Save things including mode_line_proptrans_alist,
18835 and set that to nil so that we don't alter the outer value. */
18836 record_unwind_protect (unwind_format_mode_line,
18837 format_mode_line_unwind_data
18838 (old_buffer, selected_window, 1));
18839 mode_line_proptrans_alist = Qnil;
18840
18841 Fselect_window (window, Qt);
18842 if (old_buffer)
18843 set_buffer_internal_1 (XBUFFER (buffer));
18844
18845 init_iterator (&it, w, -1, -1, NULL, face_id);
18846
18847 if (no_props)
18848 {
18849 mode_line_target = MODE_LINE_NOPROP;
18850 mode_line_string_face_prop = Qnil;
18851 mode_line_string_list = Qnil;
18852 string_start = MODE_LINE_NOPROP_LEN (0);
18853 }
18854 else
18855 {
18856 mode_line_target = MODE_LINE_STRING;
18857 mode_line_string_list = Qnil;
18858 mode_line_string_face = face;
18859 mode_line_string_face_prop
18860 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18861 }
18862
18863 push_kboard (FRAME_KBOARD (it.f));
18864 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18865 pop_kboard ();
18866
18867 if (no_props)
18868 {
18869 len = MODE_LINE_NOPROP_LEN (string_start);
18870 str = make_string (mode_line_noprop_buf + string_start, len);
18871 }
18872 else
18873 {
18874 mode_line_string_list = Fnreverse (mode_line_string_list);
18875 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18876 empty_unibyte_string);
18877 }
18878
18879 unbind_to (count, Qnil);
18880 return str;
18881 }
18882
18883 /* Write a null-terminated, right justified decimal representation of
18884 the positive integer D to BUF using a minimal field width WIDTH. */
18885
18886 static void
18887 pint2str (buf, width, d)
18888 register char *buf;
18889 register int width;
18890 register int d;
18891 {
18892 register char *p = buf;
18893
18894 if (d <= 0)
18895 *p++ = '0';
18896 else
18897 {
18898 while (d > 0)
18899 {
18900 *p++ = d % 10 + '0';
18901 d /= 10;
18902 }
18903 }
18904
18905 for (width -= (int) (p - buf); width > 0; --width)
18906 *p++ = ' ';
18907 *p-- = '\0';
18908 while (p > buf)
18909 {
18910 d = *buf;
18911 *buf++ = *p;
18912 *p-- = d;
18913 }
18914 }
18915
18916 /* Write a null-terminated, right justified decimal and "human
18917 readable" representation of the nonnegative integer D to BUF using
18918 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18919
18920 static const char power_letter[] =
18921 {
18922 0, /* not used */
18923 'k', /* kilo */
18924 'M', /* mega */
18925 'G', /* giga */
18926 'T', /* tera */
18927 'P', /* peta */
18928 'E', /* exa */
18929 'Z', /* zetta */
18930 'Y' /* yotta */
18931 };
18932
18933 static void
18934 pint2hrstr (buf, width, d)
18935 char *buf;
18936 int width;
18937 int d;
18938 {
18939 /* We aim to represent the nonnegative integer D as
18940 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18941 int quotient = d;
18942 int remainder = 0;
18943 /* -1 means: do not use TENTHS. */
18944 int tenths = -1;
18945 int exponent = 0;
18946
18947 /* Length of QUOTIENT.TENTHS as a string. */
18948 int length;
18949
18950 char * psuffix;
18951 char * p;
18952
18953 if (1000 <= quotient)
18954 {
18955 /* Scale to the appropriate EXPONENT. */
18956 do
18957 {
18958 remainder = quotient % 1000;
18959 quotient /= 1000;
18960 exponent++;
18961 }
18962 while (1000 <= quotient);
18963
18964 /* Round to nearest and decide whether to use TENTHS or not. */
18965 if (quotient <= 9)
18966 {
18967 tenths = remainder / 100;
18968 if (50 <= remainder % 100)
18969 {
18970 if (tenths < 9)
18971 tenths++;
18972 else
18973 {
18974 quotient++;
18975 if (quotient == 10)
18976 tenths = -1;
18977 else
18978 tenths = 0;
18979 }
18980 }
18981 }
18982 else
18983 if (500 <= remainder)
18984 {
18985 if (quotient < 999)
18986 quotient++;
18987 else
18988 {
18989 quotient = 1;
18990 exponent++;
18991 tenths = 0;
18992 }
18993 }
18994 }
18995
18996 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18997 if (tenths == -1 && quotient <= 99)
18998 if (quotient <= 9)
18999 length = 1;
19000 else
19001 length = 2;
19002 else
19003 length = 3;
19004 p = psuffix = buf + max (width, length);
19005
19006 /* Print EXPONENT. */
19007 if (exponent)
19008 *psuffix++ = power_letter[exponent];
19009 *psuffix = '\0';
19010
19011 /* Print TENTHS. */
19012 if (tenths >= 0)
19013 {
19014 *--p = '0' + tenths;
19015 *--p = '.';
19016 }
19017
19018 /* Print QUOTIENT. */
19019 do
19020 {
19021 int digit = quotient % 10;
19022 *--p = '0' + digit;
19023 }
19024 while ((quotient /= 10) != 0);
19025
19026 /* Print leading spaces. */
19027 while (buf < p)
19028 *--p = ' ';
19029 }
19030
19031 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19032 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19033 type of CODING_SYSTEM. Return updated pointer into BUF. */
19034
19035 static unsigned char invalid_eol_type[] = "(*invalid*)";
19036
19037 static char *
19038 decode_mode_spec_coding (coding_system, buf, eol_flag)
19039 Lisp_Object coding_system;
19040 register char *buf;
19041 int eol_flag;
19042 {
19043 Lisp_Object val;
19044 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19045 const unsigned char *eol_str;
19046 int eol_str_len;
19047 /* The EOL conversion we are using. */
19048 Lisp_Object eoltype;
19049
19050 val = CODING_SYSTEM_SPEC (coding_system);
19051 eoltype = Qnil;
19052
19053 if (!VECTORP (val)) /* Not yet decided. */
19054 {
19055 if (multibyte)
19056 *buf++ = '-';
19057 if (eol_flag)
19058 eoltype = eol_mnemonic_undecided;
19059 /* Don't mention EOL conversion if it isn't decided. */
19060 }
19061 else
19062 {
19063 Lisp_Object attrs;
19064 Lisp_Object eolvalue;
19065
19066 attrs = AREF (val, 0);
19067 eolvalue = AREF (val, 2);
19068
19069 if (multibyte)
19070 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19071
19072 if (eol_flag)
19073 {
19074 /* The EOL conversion that is normal on this system. */
19075
19076 if (NILP (eolvalue)) /* Not yet decided. */
19077 eoltype = eol_mnemonic_undecided;
19078 else if (VECTORP (eolvalue)) /* Not yet decided. */
19079 eoltype = eol_mnemonic_undecided;
19080 else /* eolvalue is Qunix, Qdos, or Qmac. */
19081 eoltype = (EQ (eolvalue, Qunix)
19082 ? eol_mnemonic_unix
19083 : (EQ (eolvalue, Qdos) == 1
19084 ? eol_mnemonic_dos : eol_mnemonic_mac));
19085 }
19086 }
19087
19088 if (eol_flag)
19089 {
19090 /* Mention the EOL conversion if it is not the usual one. */
19091 if (STRINGP (eoltype))
19092 {
19093 eol_str = SDATA (eoltype);
19094 eol_str_len = SBYTES (eoltype);
19095 }
19096 else if (CHARACTERP (eoltype))
19097 {
19098 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19099 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19100 eol_str = tmp;
19101 }
19102 else
19103 {
19104 eol_str = invalid_eol_type;
19105 eol_str_len = sizeof (invalid_eol_type) - 1;
19106 }
19107 bcopy (eol_str, buf, eol_str_len);
19108 buf += eol_str_len;
19109 }
19110
19111 return buf;
19112 }
19113
19114 /* Return a string for the output of a mode line %-spec for window W,
19115 generated by character C. PRECISION >= 0 means don't return a
19116 string longer than that value. FIELD_WIDTH > 0 means pad the
19117 string returned with spaces to that value. Return a Lisp string in
19118 *STRING if the resulting string is taken from that Lisp string.
19119
19120 Note we operate on the current buffer for most purposes,
19121 the exception being w->base_line_pos. */
19122
19123 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19124
19125 static char *
19126 decode_mode_spec (w, c, field_width, precision, string)
19127 struct window *w;
19128 register int c;
19129 int field_width, precision;
19130 Lisp_Object *string;
19131 {
19132 Lisp_Object obj;
19133 struct frame *f = XFRAME (WINDOW_FRAME (w));
19134 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19135 struct buffer *b = current_buffer;
19136
19137 obj = Qnil;
19138 *string = Qnil;
19139
19140 switch (c)
19141 {
19142 case '*':
19143 if (!NILP (b->read_only))
19144 return "%";
19145 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19146 return "*";
19147 return "-";
19148
19149 case '+':
19150 /* This differs from %* only for a modified read-only buffer. */
19151 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19152 return "*";
19153 if (!NILP (b->read_only))
19154 return "%";
19155 return "-";
19156
19157 case '&':
19158 /* This differs from %* in ignoring read-only-ness. */
19159 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19160 return "*";
19161 return "-";
19162
19163 case '%':
19164 return "%";
19165
19166 case '[':
19167 {
19168 int i;
19169 char *p;
19170
19171 if (command_loop_level > 5)
19172 return "[[[... ";
19173 p = decode_mode_spec_buf;
19174 for (i = 0; i < command_loop_level; i++)
19175 *p++ = '[';
19176 *p = 0;
19177 return decode_mode_spec_buf;
19178 }
19179
19180 case ']':
19181 {
19182 int i;
19183 char *p;
19184
19185 if (command_loop_level > 5)
19186 return " ...]]]";
19187 p = decode_mode_spec_buf;
19188 for (i = 0; i < command_loop_level; i++)
19189 *p++ = ']';
19190 *p = 0;
19191 return decode_mode_spec_buf;
19192 }
19193
19194 case '-':
19195 {
19196 register int i;
19197
19198 /* Let lots_of_dashes be a string of infinite length. */
19199 if (mode_line_target == MODE_LINE_NOPROP ||
19200 mode_line_target == MODE_LINE_STRING)
19201 return "--";
19202 if (field_width <= 0
19203 || field_width > sizeof (lots_of_dashes))
19204 {
19205 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19206 decode_mode_spec_buf[i] = '-';
19207 decode_mode_spec_buf[i] = '\0';
19208 return decode_mode_spec_buf;
19209 }
19210 else
19211 return lots_of_dashes;
19212 }
19213
19214 case 'b':
19215 obj = b->name;
19216 break;
19217
19218 case 'c':
19219 /* %c and %l are ignored in `frame-title-format'.
19220 (In redisplay_internal, the frame title is drawn _before_ the
19221 windows are updated, so the stuff which depends on actual
19222 window contents (such as %l) may fail to render properly, or
19223 even crash emacs.) */
19224 if (mode_line_target == MODE_LINE_TITLE)
19225 return "";
19226 else
19227 {
19228 int col = (int) current_column (); /* iftc */
19229 w->column_number_displayed = make_number (col);
19230 pint2str (decode_mode_spec_buf, field_width, col);
19231 return decode_mode_spec_buf;
19232 }
19233
19234 case 'e':
19235 #ifndef SYSTEM_MALLOC
19236 {
19237 if (NILP (Vmemory_full))
19238 return "";
19239 else
19240 return "!MEM FULL! ";
19241 }
19242 #else
19243 return "";
19244 #endif
19245
19246 case 'F':
19247 /* %F displays the frame name. */
19248 if (!NILP (f->title))
19249 return (char *) SDATA (f->title);
19250 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19251 return (char *) SDATA (f->name);
19252 return "Emacs";
19253
19254 case 'f':
19255 obj = b->filename;
19256 break;
19257
19258 case 'i':
19259 {
19260 int size = ZV - BEGV;
19261 pint2str (decode_mode_spec_buf, field_width, size);
19262 return decode_mode_spec_buf;
19263 }
19264
19265 case 'I':
19266 {
19267 int size = ZV - BEGV;
19268 pint2hrstr (decode_mode_spec_buf, field_width, size);
19269 return decode_mode_spec_buf;
19270 }
19271
19272 case 'l':
19273 {
19274 int startpos, startpos_byte, line, linepos, linepos_byte;
19275 int topline, nlines, junk, height;
19276
19277 /* %c and %l are ignored in `frame-title-format'. */
19278 if (mode_line_target == MODE_LINE_TITLE)
19279 return "";
19280
19281 startpos = XMARKER (w->start)->charpos;
19282 startpos_byte = marker_byte_position (w->start);
19283 height = WINDOW_TOTAL_LINES (w);
19284
19285 /* If we decided that this buffer isn't suitable for line numbers,
19286 don't forget that too fast. */
19287 if (EQ (w->base_line_pos, w->buffer))
19288 goto no_value;
19289 /* But do forget it, if the window shows a different buffer now. */
19290 else if (BUFFERP (w->base_line_pos))
19291 w->base_line_pos = Qnil;
19292
19293 /* If the buffer is very big, don't waste time. */
19294 if (INTEGERP (Vline_number_display_limit)
19295 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19296 {
19297 w->base_line_pos = Qnil;
19298 w->base_line_number = Qnil;
19299 goto no_value;
19300 }
19301
19302 if (INTEGERP (w->base_line_number)
19303 && INTEGERP (w->base_line_pos)
19304 && XFASTINT (w->base_line_pos) <= startpos)
19305 {
19306 line = XFASTINT (w->base_line_number);
19307 linepos = XFASTINT (w->base_line_pos);
19308 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19309 }
19310 else
19311 {
19312 line = 1;
19313 linepos = BUF_BEGV (b);
19314 linepos_byte = BUF_BEGV_BYTE (b);
19315 }
19316
19317 /* Count lines from base line to window start position. */
19318 nlines = display_count_lines (linepos, linepos_byte,
19319 startpos_byte,
19320 startpos, &junk);
19321
19322 topline = nlines + line;
19323
19324 /* Determine a new base line, if the old one is too close
19325 or too far away, or if we did not have one.
19326 "Too close" means it's plausible a scroll-down would
19327 go back past it. */
19328 if (startpos == BUF_BEGV (b))
19329 {
19330 w->base_line_number = make_number (topline);
19331 w->base_line_pos = make_number (BUF_BEGV (b));
19332 }
19333 else if (nlines < height + 25 || nlines > height * 3 + 50
19334 || linepos == BUF_BEGV (b))
19335 {
19336 int limit = BUF_BEGV (b);
19337 int limit_byte = BUF_BEGV_BYTE (b);
19338 int position;
19339 int distance = (height * 2 + 30) * line_number_display_limit_width;
19340
19341 if (startpos - distance > limit)
19342 {
19343 limit = startpos - distance;
19344 limit_byte = CHAR_TO_BYTE (limit);
19345 }
19346
19347 nlines = display_count_lines (startpos, startpos_byte,
19348 limit_byte,
19349 - (height * 2 + 30),
19350 &position);
19351 /* If we couldn't find the lines we wanted within
19352 line_number_display_limit_width chars per line,
19353 give up on line numbers for this window. */
19354 if (position == limit_byte && limit == startpos - distance)
19355 {
19356 w->base_line_pos = w->buffer;
19357 w->base_line_number = Qnil;
19358 goto no_value;
19359 }
19360
19361 w->base_line_number = make_number (topline - nlines);
19362 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19363 }
19364
19365 /* Now count lines from the start pos to point. */
19366 nlines = display_count_lines (startpos, startpos_byte,
19367 PT_BYTE, PT, &junk);
19368
19369 /* Record that we did display the line number. */
19370 line_number_displayed = 1;
19371
19372 /* Make the string to show. */
19373 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19374 return decode_mode_spec_buf;
19375 no_value:
19376 {
19377 char* p = decode_mode_spec_buf;
19378 int pad = field_width - 2;
19379 while (pad-- > 0)
19380 *p++ = ' ';
19381 *p++ = '?';
19382 *p++ = '?';
19383 *p = '\0';
19384 return decode_mode_spec_buf;
19385 }
19386 }
19387 break;
19388
19389 case 'm':
19390 obj = b->mode_name;
19391 break;
19392
19393 case 'n':
19394 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19395 return " Narrow";
19396 break;
19397
19398 case 'p':
19399 {
19400 int pos = marker_position (w->start);
19401 int total = BUF_ZV (b) - BUF_BEGV (b);
19402
19403 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19404 {
19405 if (pos <= BUF_BEGV (b))
19406 return "All";
19407 else
19408 return "Bottom";
19409 }
19410 else if (pos <= BUF_BEGV (b))
19411 return "Top";
19412 else
19413 {
19414 if (total > 1000000)
19415 /* Do it differently for a large value, to avoid overflow. */
19416 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19417 else
19418 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19419 /* We can't normally display a 3-digit number,
19420 so get us a 2-digit number that is close. */
19421 if (total == 100)
19422 total = 99;
19423 sprintf (decode_mode_spec_buf, "%2d%%", total);
19424 return decode_mode_spec_buf;
19425 }
19426 }
19427
19428 /* Display percentage of size above the bottom of the screen. */
19429 case 'P':
19430 {
19431 int toppos = marker_position (w->start);
19432 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19433 int total = BUF_ZV (b) - BUF_BEGV (b);
19434
19435 if (botpos >= BUF_ZV (b))
19436 {
19437 if (toppos <= BUF_BEGV (b))
19438 return "All";
19439 else
19440 return "Bottom";
19441 }
19442 else
19443 {
19444 if (total > 1000000)
19445 /* Do it differently for a large value, to avoid overflow. */
19446 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19447 else
19448 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19449 /* We can't normally display a 3-digit number,
19450 so get us a 2-digit number that is close. */
19451 if (total == 100)
19452 total = 99;
19453 if (toppos <= BUF_BEGV (b))
19454 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
19455 else
19456 sprintf (decode_mode_spec_buf, "%2d%%", total);
19457 return decode_mode_spec_buf;
19458 }
19459 }
19460
19461 case 's':
19462 /* status of process */
19463 obj = Fget_buffer_process (Fcurrent_buffer ());
19464 if (NILP (obj))
19465 return "no process";
19466 #ifdef subprocesses
19467 obj = Fsymbol_name (Fprocess_status (obj));
19468 #endif
19469 break;
19470
19471 case '@':
19472 {
19473 int count = inhibit_garbage_collection ();
19474 Lisp_Object val = call1 (intern ("file-remote-p"),
19475 current_buffer->directory);
19476 unbind_to (count, Qnil);
19477
19478 if (NILP (val))
19479 return "-";
19480 else
19481 return "@";
19482 }
19483
19484 case 't': /* indicate TEXT or BINARY */
19485 #ifdef MODE_LINE_BINARY_TEXT
19486 return MODE_LINE_BINARY_TEXT (b);
19487 #else
19488 return "T";
19489 #endif
19490
19491 case 'z':
19492 /* coding-system (not including end-of-line format) */
19493 case 'Z':
19494 /* coding-system (including end-of-line type) */
19495 {
19496 int eol_flag = (c == 'Z');
19497 char *p = decode_mode_spec_buf;
19498
19499 if (! FRAME_WINDOW_P (f))
19500 {
19501 /* No need to mention EOL here--the terminal never needs
19502 to do EOL conversion. */
19503 p = decode_mode_spec_coding (CODING_ID_NAME
19504 (FRAME_KEYBOARD_CODING (f)->id),
19505 p, 0);
19506 p = decode_mode_spec_coding (CODING_ID_NAME
19507 (FRAME_TERMINAL_CODING (f)->id),
19508 p, 0);
19509 }
19510 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19511 p, eol_flag);
19512
19513 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19514 #ifdef subprocesses
19515 obj = Fget_buffer_process (Fcurrent_buffer ());
19516 if (PROCESSP (obj))
19517 {
19518 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19519 p, eol_flag);
19520 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19521 p, eol_flag);
19522 }
19523 #endif /* subprocesses */
19524 #endif /* 0 */
19525 *p = 0;
19526 return decode_mode_spec_buf;
19527 }
19528 }
19529
19530 if (STRINGP (obj))
19531 {
19532 *string = obj;
19533 return (char *) SDATA (obj);
19534 }
19535 else
19536 return "";
19537 }
19538
19539
19540 /* Count up to COUNT lines starting from START / START_BYTE.
19541 But don't go beyond LIMIT_BYTE.
19542 Return the number of lines thus found (always nonnegative).
19543
19544 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19545
19546 static int
19547 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
19548 int start, start_byte, limit_byte, count;
19549 int *byte_pos_ptr;
19550 {
19551 register unsigned char *cursor;
19552 unsigned char *base;
19553
19554 register int ceiling;
19555 register unsigned char *ceiling_addr;
19556 int orig_count = count;
19557
19558 /* If we are not in selective display mode,
19559 check only for newlines. */
19560 int selective_display = (!NILP (current_buffer->selective_display)
19561 && !INTEGERP (current_buffer->selective_display));
19562
19563 if (count > 0)
19564 {
19565 while (start_byte < limit_byte)
19566 {
19567 ceiling = BUFFER_CEILING_OF (start_byte);
19568 ceiling = min (limit_byte - 1, ceiling);
19569 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19570 base = (cursor = BYTE_POS_ADDR (start_byte));
19571 while (1)
19572 {
19573 if (selective_display)
19574 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19575 ;
19576 else
19577 while (*cursor != '\n' && ++cursor != ceiling_addr)
19578 ;
19579
19580 if (cursor != ceiling_addr)
19581 {
19582 if (--count == 0)
19583 {
19584 start_byte += cursor - base + 1;
19585 *byte_pos_ptr = start_byte;
19586 return orig_count;
19587 }
19588 else
19589 if (++cursor == ceiling_addr)
19590 break;
19591 }
19592 else
19593 break;
19594 }
19595 start_byte += cursor - base;
19596 }
19597 }
19598 else
19599 {
19600 while (start_byte > limit_byte)
19601 {
19602 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19603 ceiling = max (limit_byte, ceiling);
19604 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19605 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19606 while (1)
19607 {
19608 if (selective_display)
19609 while (--cursor != ceiling_addr
19610 && *cursor != '\n' && *cursor != 015)
19611 ;
19612 else
19613 while (--cursor != ceiling_addr && *cursor != '\n')
19614 ;
19615
19616 if (cursor != ceiling_addr)
19617 {
19618 if (++count == 0)
19619 {
19620 start_byte += cursor - base + 1;
19621 *byte_pos_ptr = start_byte;
19622 /* When scanning backwards, we should
19623 not count the newline posterior to which we stop. */
19624 return - orig_count - 1;
19625 }
19626 }
19627 else
19628 break;
19629 }
19630 /* Here we add 1 to compensate for the last decrement
19631 of CURSOR, which took it past the valid range. */
19632 start_byte += cursor - base + 1;
19633 }
19634 }
19635
19636 *byte_pos_ptr = limit_byte;
19637
19638 if (count < 0)
19639 return - orig_count + count;
19640 return orig_count - count;
19641
19642 }
19643
19644
19645 \f
19646 /***********************************************************************
19647 Displaying strings
19648 ***********************************************************************/
19649
19650 /* Display a NUL-terminated string, starting with index START.
19651
19652 If STRING is non-null, display that C string. Otherwise, the Lisp
19653 string LISP_STRING is displayed. There's a case that STRING is
19654 non-null and LISP_STRING is not nil. It means STRING is a string
19655 data of LISP_STRING. In that case, we display LISP_STRING while
19656 ignoring its text properties.
19657
19658 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19659 FACE_STRING. Display STRING or LISP_STRING with the face at
19660 FACE_STRING_POS in FACE_STRING:
19661
19662 Display the string in the environment given by IT, but use the
19663 standard display table, temporarily.
19664
19665 FIELD_WIDTH is the minimum number of output glyphs to produce.
19666 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19667 with spaces. If STRING has more characters, more than FIELD_WIDTH
19668 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19669
19670 PRECISION is the maximum number of characters to output from
19671 STRING. PRECISION < 0 means don't truncate the string.
19672
19673 This is roughly equivalent to printf format specifiers:
19674
19675 FIELD_WIDTH PRECISION PRINTF
19676 ----------------------------------------
19677 -1 -1 %s
19678 -1 10 %.10s
19679 10 -1 %10s
19680 20 10 %20.10s
19681
19682 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19683 display them, and < 0 means obey the current buffer's value of
19684 enable_multibyte_characters.
19685
19686 Value is the number of columns displayed. */
19687
19688 static int
19689 display_string (string, lisp_string, face_string, face_string_pos,
19690 start, it, field_width, precision, max_x, multibyte)
19691 unsigned char *string;
19692 Lisp_Object lisp_string;
19693 Lisp_Object face_string;
19694 EMACS_INT face_string_pos;
19695 EMACS_INT start;
19696 struct it *it;
19697 int field_width, precision, max_x;
19698 int multibyte;
19699 {
19700 int hpos_at_start = it->hpos;
19701 int saved_face_id = it->face_id;
19702 struct glyph_row *row = it->glyph_row;
19703
19704 /* Initialize the iterator IT for iteration over STRING beginning
19705 with index START. */
19706 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19707 precision, field_width, multibyte);
19708 if (string && STRINGP (lisp_string))
19709 /* LISP_STRING is the one returned by decode_mode_spec. We should
19710 ignore its text properties. */
19711 it->stop_charpos = -1;
19712
19713 /* If displaying STRING, set up the face of the iterator
19714 from LISP_STRING, if that's given. */
19715 if (STRINGP (face_string))
19716 {
19717 EMACS_INT endptr;
19718 struct face *face;
19719
19720 it->face_id
19721 = face_at_string_position (it->w, face_string, face_string_pos,
19722 0, it->region_beg_charpos,
19723 it->region_end_charpos,
19724 &endptr, it->base_face_id, 0);
19725 face = FACE_FROM_ID (it->f, it->face_id);
19726 it->face_box_p = face->box != FACE_NO_BOX;
19727 }
19728
19729 /* Set max_x to the maximum allowed X position. Don't let it go
19730 beyond the right edge of the window. */
19731 if (max_x <= 0)
19732 max_x = it->last_visible_x;
19733 else
19734 max_x = min (max_x, it->last_visible_x);
19735
19736 /* Skip over display elements that are not visible. because IT->w is
19737 hscrolled. */
19738 if (it->current_x < it->first_visible_x)
19739 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19740 MOVE_TO_POS | MOVE_TO_X);
19741
19742 row->ascent = it->max_ascent;
19743 row->height = it->max_ascent + it->max_descent;
19744 row->phys_ascent = it->max_phys_ascent;
19745 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19746 row->extra_line_spacing = it->max_extra_line_spacing;
19747
19748 /* This condition is for the case that we are called with current_x
19749 past last_visible_x. */
19750 while (it->current_x < max_x)
19751 {
19752 int x_before, x, n_glyphs_before, i, nglyphs;
19753
19754 /* Get the next display element. */
19755 if (!get_next_display_element (it))
19756 break;
19757
19758 /* Produce glyphs. */
19759 x_before = it->current_x;
19760 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19761 PRODUCE_GLYPHS (it);
19762
19763 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19764 i = 0;
19765 x = x_before;
19766 while (i < nglyphs)
19767 {
19768 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19769
19770 if (it->line_wrap != TRUNCATE
19771 && x + glyph->pixel_width > max_x)
19772 {
19773 /* End of continued line or max_x reached. */
19774 if (CHAR_GLYPH_PADDING_P (*glyph))
19775 {
19776 /* A wide character is unbreakable. */
19777 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19778 it->current_x = x_before;
19779 }
19780 else
19781 {
19782 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19783 it->current_x = x;
19784 }
19785 break;
19786 }
19787 else if (x + glyph->pixel_width >= it->first_visible_x)
19788 {
19789 /* Glyph is at least partially visible. */
19790 ++it->hpos;
19791 if (x < it->first_visible_x)
19792 it->glyph_row->x = x - it->first_visible_x;
19793 }
19794 else
19795 {
19796 /* Glyph is off the left margin of the display area.
19797 Should not happen. */
19798 abort ();
19799 }
19800
19801 row->ascent = max (row->ascent, it->max_ascent);
19802 row->height = max (row->height, it->max_ascent + it->max_descent);
19803 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19804 row->phys_height = max (row->phys_height,
19805 it->max_phys_ascent + it->max_phys_descent);
19806 row->extra_line_spacing = max (row->extra_line_spacing,
19807 it->max_extra_line_spacing);
19808 x += glyph->pixel_width;
19809 ++i;
19810 }
19811
19812 /* Stop if max_x reached. */
19813 if (i < nglyphs)
19814 break;
19815
19816 /* Stop at line ends. */
19817 if (ITERATOR_AT_END_OF_LINE_P (it))
19818 {
19819 it->continuation_lines_width = 0;
19820 break;
19821 }
19822
19823 set_iterator_to_next (it, 1);
19824
19825 /* Stop if truncating at the right edge. */
19826 if (it->line_wrap == TRUNCATE
19827 && it->current_x >= it->last_visible_x)
19828 {
19829 /* Add truncation mark, but don't do it if the line is
19830 truncated at a padding space. */
19831 if (IT_CHARPOS (*it) < it->string_nchars)
19832 {
19833 if (!FRAME_WINDOW_P (it->f))
19834 {
19835 int i, n;
19836
19837 if (it->current_x > it->last_visible_x)
19838 {
19839 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19840 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19841 break;
19842 for (n = row->used[TEXT_AREA]; i < n; ++i)
19843 {
19844 row->used[TEXT_AREA] = i;
19845 produce_special_glyphs (it, IT_TRUNCATION);
19846 }
19847 }
19848 produce_special_glyphs (it, IT_TRUNCATION);
19849 }
19850 it->glyph_row->truncated_on_right_p = 1;
19851 }
19852 break;
19853 }
19854 }
19855
19856 /* Maybe insert a truncation at the left. */
19857 if (it->first_visible_x
19858 && IT_CHARPOS (*it) > 0)
19859 {
19860 if (!FRAME_WINDOW_P (it->f))
19861 insert_left_trunc_glyphs (it);
19862 it->glyph_row->truncated_on_left_p = 1;
19863 }
19864
19865 it->face_id = saved_face_id;
19866
19867 /* Value is number of columns displayed. */
19868 return it->hpos - hpos_at_start;
19869 }
19870
19871
19872 \f
19873 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19874 appears as an element of LIST or as the car of an element of LIST.
19875 If PROPVAL is a list, compare each element against LIST in that
19876 way, and return 1/2 if any element of PROPVAL is found in LIST.
19877 Otherwise return 0. This function cannot quit.
19878 The return value is 2 if the text is invisible but with an ellipsis
19879 and 1 if it's invisible and without an ellipsis. */
19880
19881 int
19882 invisible_p (propval, list)
19883 register Lisp_Object propval;
19884 Lisp_Object list;
19885 {
19886 register Lisp_Object tail, proptail;
19887
19888 for (tail = list; CONSP (tail); tail = XCDR (tail))
19889 {
19890 register Lisp_Object tem;
19891 tem = XCAR (tail);
19892 if (EQ (propval, tem))
19893 return 1;
19894 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19895 return NILP (XCDR (tem)) ? 1 : 2;
19896 }
19897
19898 if (CONSP (propval))
19899 {
19900 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19901 {
19902 Lisp_Object propelt;
19903 propelt = XCAR (proptail);
19904 for (tail = list; CONSP (tail); tail = XCDR (tail))
19905 {
19906 register Lisp_Object tem;
19907 tem = XCAR (tail);
19908 if (EQ (propelt, tem))
19909 return 1;
19910 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19911 return NILP (XCDR (tem)) ? 1 : 2;
19912 }
19913 }
19914 }
19915
19916 return 0;
19917 }
19918
19919 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19920 doc: /* Non-nil if the property makes the text invisible.
19921 POS-OR-PROP can be a marker or number, in which case it is taken to be
19922 a position in the current buffer and the value of the `invisible' property
19923 is checked; or it can be some other value, which is then presumed to be the
19924 value of the `invisible' property of the text of interest.
19925 The non-nil value returned can be t for truly invisible text or something
19926 else if the text is replaced by an ellipsis. */)
19927 (pos_or_prop)
19928 Lisp_Object pos_or_prop;
19929 {
19930 Lisp_Object prop
19931 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19932 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19933 : pos_or_prop);
19934 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19935 return (invis == 0 ? Qnil
19936 : invis == 1 ? Qt
19937 : make_number (invis));
19938 }
19939
19940 /* Calculate a width or height in pixels from a specification using
19941 the following elements:
19942
19943 SPEC ::=
19944 NUM - a (fractional) multiple of the default font width/height
19945 (NUM) - specifies exactly NUM pixels
19946 UNIT - a fixed number of pixels, see below.
19947 ELEMENT - size of a display element in pixels, see below.
19948 (NUM . SPEC) - equals NUM * SPEC
19949 (+ SPEC SPEC ...) - add pixel values
19950 (- SPEC SPEC ...) - subtract pixel values
19951 (- SPEC) - negate pixel value
19952
19953 NUM ::=
19954 INT or FLOAT - a number constant
19955 SYMBOL - use symbol's (buffer local) variable binding.
19956
19957 UNIT ::=
19958 in - pixels per inch *)
19959 mm - pixels per 1/1000 meter *)
19960 cm - pixels per 1/100 meter *)
19961 width - width of current font in pixels.
19962 height - height of current font in pixels.
19963
19964 *) using the ratio(s) defined in display-pixels-per-inch.
19965
19966 ELEMENT ::=
19967
19968 left-fringe - left fringe width in pixels
19969 right-fringe - right fringe width in pixels
19970
19971 left-margin - left margin width in pixels
19972 right-margin - right margin width in pixels
19973
19974 scroll-bar - scroll-bar area width in pixels
19975
19976 Examples:
19977
19978 Pixels corresponding to 5 inches:
19979 (5 . in)
19980
19981 Total width of non-text areas on left side of window (if scroll-bar is on left):
19982 '(space :width (+ left-fringe left-margin scroll-bar))
19983
19984 Align to first text column (in header line):
19985 '(space :align-to 0)
19986
19987 Align to middle of text area minus half the width of variable `my-image'
19988 containing a loaded image:
19989 '(space :align-to (0.5 . (- text my-image)))
19990
19991 Width of left margin minus width of 1 character in the default font:
19992 '(space :width (- left-margin 1))
19993
19994 Width of left margin minus width of 2 characters in the current font:
19995 '(space :width (- left-margin (2 . width)))
19996
19997 Center 1 character over left-margin (in header line):
19998 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19999
20000 Different ways to express width of left fringe plus left margin minus one pixel:
20001 '(space :width (- (+ left-fringe left-margin) (1)))
20002 '(space :width (+ left-fringe left-margin (- (1))))
20003 '(space :width (+ left-fringe left-margin (-1)))
20004
20005 */
20006
20007 #define NUMVAL(X) \
20008 ((INTEGERP (X) || FLOATP (X)) \
20009 ? XFLOATINT (X) \
20010 : - 1)
20011
20012 int
20013 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
20014 double *res;
20015 struct it *it;
20016 Lisp_Object prop;
20017 struct font *font;
20018 int width_p, *align_to;
20019 {
20020 double pixels;
20021
20022 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20023 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20024
20025 if (NILP (prop))
20026 return OK_PIXELS (0);
20027
20028 xassert (FRAME_LIVE_P (it->f));
20029
20030 if (SYMBOLP (prop))
20031 {
20032 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20033 {
20034 char *unit = SDATA (SYMBOL_NAME (prop));
20035
20036 if (unit[0] == 'i' && unit[1] == 'n')
20037 pixels = 1.0;
20038 else if (unit[0] == 'm' && unit[1] == 'm')
20039 pixels = 25.4;
20040 else if (unit[0] == 'c' && unit[1] == 'm')
20041 pixels = 2.54;
20042 else
20043 pixels = 0;
20044 if (pixels > 0)
20045 {
20046 double ppi;
20047 #ifdef HAVE_WINDOW_SYSTEM
20048 if (FRAME_WINDOW_P (it->f)
20049 && (ppi = (width_p
20050 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20051 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20052 ppi > 0))
20053 return OK_PIXELS (ppi / pixels);
20054 #endif
20055
20056 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20057 || (CONSP (Vdisplay_pixels_per_inch)
20058 && (ppi = (width_p
20059 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20060 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20061 ppi > 0)))
20062 return OK_PIXELS (ppi / pixels);
20063
20064 return 0;
20065 }
20066 }
20067
20068 #ifdef HAVE_WINDOW_SYSTEM
20069 if (EQ (prop, Qheight))
20070 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20071 if (EQ (prop, Qwidth))
20072 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20073 #else
20074 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20075 return OK_PIXELS (1);
20076 #endif
20077
20078 if (EQ (prop, Qtext))
20079 return OK_PIXELS (width_p
20080 ? window_box_width (it->w, TEXT_AREA)
20081 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20082
20083 if (align_to && *align_to < 0)
20084 {
20085 *res = 0;
20086 if (EQ (prop, Qleft))
20087 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20088 if (EQ (prop, Qright))
20089 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20090 if (EQ (prop, Qcenter))
20091 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20092 + window_box_width (it->w, TEXT_AREA) / 2);
20093 if (EQ (prop, Qleft_fringe))
20094 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20095 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20096 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20097 if (EQ (prop, Qright_fringe))
20098 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20099 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20100 : window_box_right_offset (it->w, TEXT_AREA));
20101 if (EQ (prop, Qleft_margin))
20102 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20103 if (EQ (prop, Qright_margin))
20104 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20105 if (EQ (prop, Qscroll_bar))
20106 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20107 ? 0
20108 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20109 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20110 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20111 : 0)));
20112 }
20113 else
20114 {
20115 if (EQ (prop, Qleft_fringe))
20116 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20117 if (EQ (prop, Qright_fringe))
20118 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20119 if (EQ (prop, Qleft_margin))
20120 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20121 if (EQ (prop, Qright_margin))
20122 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20123 if (EQ (prop, Qscroll_bar))
20124 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20125 }
20126
20127 prop = Fbuffer_local_value (prop, it->w->buffer);
20128 }
20129
20130 if (INTEGERP (prop) || FLOATP (prop))
20131 {
20132 int base_unit = (width_p
20133 ? FRAME_COLUMN_WIDTH (it->f)
20134 : FRAME_LINE_HEIGHT (it->f));
20135 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20136 }
20137
20138 if (CONSP (prop))
20139 {
20140 Lisp_Object car = XCAR (prop);
20141 Lisp_Object cdr = XCDR (prop);
20142
20143 if (SYMBOLP (car))
20144 {
20145 #ifdef HAVE_WINDOW_SYSTEM
20146 if (FRAME_WINDOW_P (it->f)
20147 && valid_image_p (prop))
20148 {
20149 int id = lookup_image (it->f, prop);
20150 struct image *img = IMAGE_FROM_ID (it->f, id);
20151
20152 return OK_PIXELS (width_p ? img->width : img->height);
20153 }
20154 #endif
20155 if (EQ (car, Qplus) || EQ (car, Qminus))
20156 {
20157 int first = 1;
20158 double px;
20159
20160 pixels = 0;
20161 while (CONSP (cdr))
20162 {
20163 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20164 font, width_p, align_to))
20165 return 0;
20166 if (first)
20167 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20168 else
20169 pixels += px;
20170 cdr = XCDR (cdr);
20171 }
20172 if (EQ (car, Qminus))
20173 pixels = -pixels;
20174 return OK_PIXELS (pixels);
20175 }
20176
20177 car = Fbuffer_local_value (car, it->w->buffer);
20178 }
20179
20180 if (INTEGERP (car) || FLOATP (car))
20181 {
20182 double fact;
20183 pixels = XFLOATINT (car);
20184 if (NILP (cdr))
20185 return OK_PIXELS (pixels);
20186 if (calc_pixel_width_or_height (&fact, it, cdr,
20187 font, width_p, align_to))
20188 return OK_PIXELS (pixels * fact);
20189 return 0;
20190 }
20191
20192 return 0;
20193 }
20194
20195 return 0;
20196 }
20197
20198 \f
20199 /***********************************************************************
20200 Glyph Display
20201 ***********************************************************************/
20202
20203 #ifdef HAVE_WINDOW_SYSTEM
20204
20205 #if GLYPH_DEBUG
20206
20207 void
20208 dump_glyph_string (s)
20209 struct glyph_string *s;
20210 {
20211 fprintf (stderr, "glyph string\n");
20212 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20213 s->x, s->y, s->width, s->height);
20214 fprintf (stderr, " ybase = %d\n", s->ybase);
20215 fprintf (stderr, " hl = %d\n", s->hl);
20216 fprintf (stderr, " left overhang = %d, right = %d\n",
20217 s->left_overhang, s->right_overhang);
20218 fprintf (stderr, " nchars = %d\n", s->nchars);
20219 fprintf (stderr, " extends to end of line = %d\n",
20220 s->extends_to_end_of_line_p);
20221 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20222 fprintf (stderr, " bg width = %d\n", s->background_width);
20223 }
20224
20225 #endif /* GLYPH_DEBUG */
20226
20227 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20228 of XChar2b structures for S; it can't be allocated in
20229 init_glyph_string because it must be allocated via `alloca'. W
20230 is the window on which S is drawn. ROW and AREA are the glyph row
20231 and area within the row from which S is constructed. START is the
20232 index of the first glyph structure covered by S. HL is a
20233 face-override for drawing S. */
20234
20235 #ifdef HAVE_NTGUI
20236 #define OPTIONAL_HDC(hdc) hdc,
20237 #define DECLARE_HDC(hdc) HDC hdc;
20238 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20239 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20240 #endif
20241
20242 #ifndef OPTIONAL_HDC
20243 #define OPTIONAL_HDC(hdc)
20244 #define DECLARE_HDC(hdc)
20245 #define ALLOCATE_HDC(hdc, f)
20246 #define RELEASE_HDC(hdc, f)
20247 #endif
20248
20249 static void
20250 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
20251 struct glyph_string *s;
20252 DECLARE_HDC (hdc)
20253 XChar2b *char2b;
20254 struct window *w;
20255 struct glyph_row *row;
20256 enum glyph_row_area area;
20257 int start;
20258 enum draw_glyphs_face hl;
20259 {
20260 bzero (s, sizeof *s);
20261 s->w = w;
20262 s->f = XFRAME (w->frame);
20263 #ifdef HAVE_NTGUI
20264 s->hdc = hdc;
20265 #endif
20266 s->display = FRAME_X_DISPLAY (s->f);
20267 s->window = FRAME_X_WINDOW (s->f);
20268 s->char2b = char2b;
20269 s->hl = hl;
20270 s->row = row;
20271 s->area = area;
20272 s->first_glyph = row->glyphs[area] + start;
20273 s->height = row->height;
20274 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20275 s->ybase = s->y + row->ascent;
20276 }
20277
20278
20279 /* Append the list of glyph strings with head H and tail T to the list
20280 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20281
20282 static INLINE void
20283 append_glyph_string_lists (head, tail, h, t)
20284 struct glyph_string **head, **tail;
20285 struct glyph_string *h, *t;
20286 {
20287 if (h)
20288 {
20289 if (*head)
20290 (*tail)->next = h;
20291 else
20292 *head = h;
20293 h->prev = *tail;
20294 *tail = t;
20295 }
20296 }
20297
20298
20299 /* Prepend the list of glyph strings with head H and tail T to the
20300 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20301 result. */
20302
20303 static INLINE void
20304 prepend_glyph_string_lists (head, tail, h, t)
20305 struct glyph_string **head, **tail;
20306 struct glyph_string *h, *t;
20307 {
20308 if (h)
20309 {
20310 if (*head)
20311 (*head)->prev = t;
20312 else
20313 *tail = t;
20314 t->next = *head;
20315 *head = h;
20316 }
20317 }
20318
20319
20320 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20321 Set *HEAD and *TAIL to the resulting list. */
20322
20323 static INLINE void
20324 append_glyph_string (head, tail, s)
20325 struct glyph_string **head, **tail;
20326 struct glyph_string *s;
20327 {
20328 s->next = s->prev = NULL;
20329 append_glyph_string_lists (head, tail, s, s);
20330 }
20331
20332
20333 /* Get face and two-byte form of character C in face FACE_ID on frame
20334 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20335 means we want to display multibyte text. DISPLAY_P non-zero means
20336 make sure that X resources for the face returned are allocated.
20337 Value is a pointer to a realized face that is ready for display if
20338 DISPLAY_P is non-zero. */
20339
20340 static INLINE struct face *
20341 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
20342 struct frame *f;
20343 int c, face_id;
20344 XChar2b *char2b;
20345 int multibyte_p, display_p;
20346 {
20347 struct face *face = FACE_FROM_ID (f, face_id);
20348
20349 if (face->font)
20350 {
20351 unsigned code = face->font->driver->encode_char (face->font, c);
20352
20353 if (code != FONT_INVALID_CODE)
20354 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20355 else
20356 STORE_XCHAR2B (char2b, 0, 0);
20357 }
20358
20359 /* Make sure X resources of the face are allocated. */
20360 #ifdef HAVE_X_WINDOWS
20361 if (display_p)
20362 #endif
20363 {
20364 xassert (face != NULL);
20365 PREPARE_FACE_FOR_DISPLAY (f, face);
20366 }
20367
20368 return face;
20369 }
20370
20371
20372 /* Get face and two-byte form of character glyph GLYPH on frame F.
20373 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20374 a pointer to a realized face that is ready for display. */
20375
20376 static INLINE struct face *
20377 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
20378 struct frame *f;
20379 struct glyph *glyph;
20380 XChar2b *char2b;
20381 int *two_byte_p;
20382 {
20383 struct face *face;
20384
20385 xassert (glyph->type == CHAR_GLYPH);
20386 face = FACE_FROM_ID (f, glyph->face_id);
20387
20388 if (two_byte_p)
20389 *two_byte_p = 0;
20390
20391 if (face->font)
20392 {
20393 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
20394
20395 if (code != FONT_INVALID_CODE)
20396 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20397 else
20398 STORE_XCHAR2B (char2b, 0, 0);
20399 }
20400
20401 /* Make sure X resources of the face are allocated. */
20402 xassert (face != NULL);
20403 PREPARE_FACE_FOR_DISPLAY (f, face);
20404 return face;
20405 }
20406
20407
20408 /* Fill glyph string S with composition components specified by S->cmp.
20409
20410 BASE_FACE is the base face of the composition.
20411 S->cmp_from is the index of the first component for S.
20412
20413 OVERLAPS non-zero means S should draw the foreground only, and use
20414 its physical height for clipping. See also draw_glyphs.
20415
20416 Value is the index of a component not in S. */
20417
20418 static int
20419 fill_composite_glyph_string (s, base_face, overlaps)
20420 struct glyph_string *s;
20421 struct face *base_face;
20422 int overlaps;
20423 {
20424 int i;
20425 /* For all glyphs of this composition, starting at the offset
20426 S->cmp_from, until we reach the end of the definition or encounter a
20427 glyph that requires the different face, add it to S. */
20428 struct face *face;
20429
20430 xassert (s);
20431
20432 s->for_overlaps = overlaps;
20433 s->face = NULL;
20434 s->font = NULL;
20435 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20436 {
20437 int c = COMPOSITION_GLYPH (s->cmp, i);
20438
20439 if (c != '\t')
20440 {
20441 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20442 -1, Qnil);
20443
20444 face = get_char_face_and_encoding (s->f, c, face_id,
20445 s->char2b + i, 1, 1);
20446 if (face)
20447 {
20448 if (! s->face)
20449 {
20450 s->face = face;
20451 s->font = s->face->font;
20452 }
20453 else if (s->face != face)
20454 break;
20455 }
20456 }
20457 ++s->nchars;
20458 }
20459 s->cmp_to = i;
20460
20461 /* All glyph strings for the same composition has the same width,
20462 i.e. the width set for the first component of the composition. */
20463 s->width = s->first_glyph->pixel_width;
20464
20465 /* If the specified font could not be loaded, use the frame's
20466 default font, but record the fact that we couldn't load it in
20467 the glyph string so that we can draw rectangles for the
20468 characters of the glyph string. */
20469 if (s->font == NULL)
20470 {
20471 s->font_not_found_p = 1;
20472 s->font = FRAME_FONT (s->f);
20473 }
20474
20475 /* Adjust base line for subscript/superscript text. */
20476 s->ybase += s->first_glyph->voffset;
20477
20478 /* This glyph string must always be drawn with 16-bit functions. */
20479 s->two_byte_p = 1;
20480
20481 return s->cmp_to;
20482 }
20483
20484 static int
20485 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
20486 struct glyph_string *s;
20487 int face_id;
20488 int start, end, overlaps;
20489 {
20490 struct glyph *glyph, *last;
20491 Lisp_Object lgstring;
20492 int i;
20493
20494 s->for_overlaps = overlaps;
20495 glyph = s->row->glyphs[s->area] + start;
20496 last = s->row->glyphs[s->area] + end;
20497 s->cmp_id = glyph->u.cmp.id;
20498 s->cmp_from = glyph->u.cmp.from;
20499 s->cmp_to = glyph->u.cmp.to + 1;
20500 s->face = FACE_FROM_ID (s->f, face_id);
20501 lgstring = composition_gstring_from_id (s->cmp_id);
20502 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20503 glyph++;
20504 while (glyph < last
20505 && glyph->u.cmp.automatic
20506 && glyph->u.cmp.id == s->cmp_id
20507 && s->cmp_to == glyph->u.cmp.from)
20508 s->cmp_to = (glyph++)->u.cmp.to + 1;
20509
20510 for (i = s->cmp_from; i < s->cmp_to; i++)
20511 {
20512 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20513 unsigned code = LGLYPH_CODE (lglyph);
20514
20515 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20516 }
20517 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20518 return glyph - s->row->glyphs[s->area];
20519 }
20520
20521
20522 /* Fill glyph string S from a sequence of character glyphs.
20523
20524 FACE_ID is the face id of the string. START is the index of the
20525 first glyph to consider, END is the index of the last + 1.
20526 OVERLAPS non-zero means S should draw the foreground only, and use
20527 its physical height for clipping. See also draw_glyphs.
20528
20529 Value is the index of the first glyph not in S. */
20530
20531 static int
20532 fill_glyph_string (s, face_id, start, end, overlaps)
20533 struct glyph_string *s;
20534 int face_id;
20535 int start, end, overlaps;
20536 {
20537 struct glyph *glyph, *last;
20538 int voffset;
20539 int glyph_not_available_p;
20540
20541 xassert (s->f == XFRAME (s->w->frame));
20542 xassert (s->nchars == 0);
20543 xassert (start >= 0 && end > start);
20544
20545 s->for_overlaps = overlaps;
20546 glyph = s->row->glyphs[s->area] + start;
20547 last = s->row->glyphs[s->area] + end;
20548 voffset = glyph->voffset;
20549 s->padding_p = glyph->padding_p;
20550 glyph_not_available_p = glyph->glyph_not_available_p;
20551
20552 while (glyph < last
20553 && glyph->type == CHAR_GLYPH
20554 && glyph->voffset == voffset
20555 /* Same face id implies same font, nowadays. */
20556 && glyph->face_id == face_id
20557 && glyph->glyph_not_available_p == glyph_not_available_p)
20558 {
20559 int two_byte_p;
20560
20561 s->face = get_glyph_face_and_encoding (s->f, glyph,
20562 s->char2b + s->nchars,
20563 &two_byte_p);
20564 s->two_byte_p = two_byte_p;
20565 ++s->nchars;
20566 xassert (s->nchars <= end - start);
20567 s->width += glyph->pixel_width;
20568 if (glyph++->padding_p != s->padding_p)
20569 break;
20570 }
20571
20572 s->font = s->face->font;
20573
20574 /* If the specified font could not be loaded, use the frame's font,
20575 but record the fact that we couldn't load it in
20576 S->font_not_found_p so that we can draw rectangles for the
20577 characters of the glyph string. */
20578 if (s->font == NULL || glyph_not_available_p)
20579 {
20580 s->font_not_found_p = 1;
20581 s->font = FRAME_FONT (s->f);
20582 }
20583
20584 /* Adjust base line for subscript/superscript text. */
20585 s->ybase += voffset;
20586
20587 xassert (s->face && s->face->gc);
20588 return glyph - s->row->glyphs[s->area];
20589 }
20590
20591
20592 /* Fill glyph string S from image glyph S->first_glyph. */
20593
20594 static void
20595 fill_image_glyph_string (s)
20596 struct glyph_string *s;
20597 {
20598 xassert (s->first_glyph->type == IMAGE_GLYPH);
20599 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20600 xassert (s->img);
20601 s->slice = s->first_glyph->slice;
20602 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20603 s->font = s->face->font;
20604 s->width = s->first_glyph->pixel_width;
20605
20606 /* Adjust base line for subscript/superscript text. */
20607 s->ybase += s->first_glyph->voffset;
20608 }
20609
20610
20611 /* Fill glyph string S from a sequence of stretch glyphs.
20612
20613 ROW is the glyph row in which the glyphs are found, AREA is the
20614 area within the row. START is the index of the first glyph to
20615 consider, END is the index of the last + 1.
20616
20617 Value is the index of the first glyph not in S. */
20618
20619 static int
20620 fill_stretch_glyph_string (s, row, area, start, end)
20621 struct glyph_string *s;
20622 struct glyph_row *row;
20623 enum glyph_row_area area;
20624 int start, end;
20625 {
20626 struct glyph *glyph, *last;
20627 int voffset, face_id;
20628
20629 xassert (s->first_glyph->type == STRETCH_GLYPH);
20630
20631 glyph = s->row->glyphs[s->area] + start;
20632 last = s->row->glyphs[s->area] + end;
20633 face_id = glyph->face_id;
20634 s->face = FACE_FROM_ID (s->f, face_id);
20635 s->font = s->face->font;
20636 s->width = glyph->pixel_width;
20637 s->nchars = 1;
20638 voffset = glyph->voffset;
20639
20640 for (++glyph;
20641 (glyph < last
20642 && glyph->type == STRETCH_GLYPH
20643 && glyph->voffset == voffset
20644 && glyph->face_id == face_id);
20645 ++glyph)
20646 s->width += glyph->pixel_width;
20647
20648 /* Adjust base line for subscript/superscript text. */
20649 s->ybase += voffset;
20650
20651 /* The case that face->gc == 0 is handled when drawing the glyph
20652 string by calling PREPARE_FACE_FOR_DISPLAY. */
20653 xassert (s->face);
20654 return glyph - s->row->glyphs[s->area];
20655 }
20656
20657 static struct font_metrics *
20658 get_per_char_metric (f, font, char2b)
20659 struct frame *f;
20660 struct font *font;
20661 XChar2b *char2b;
20662 {
20663 static struct font_metrics metrics;
20664 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20665
20666 if (! font || code == FONT_INVALID_CODE)
20667 return NULL;
20668 font->driver->text_extents (font, &code, 1, &metrics);
20669 return &metrics;
20670 }
20671
20672 /* EXPORT for RIF:
20673 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20674 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20675 assumed to be zero. */
20676
20677 void
20678 x_get_glyph_overhangs (glyph, f, left, right)
20679 struct glyph *glyph;
20680 struct frame *f;
20681 int *left, *right;
20682 {
20683 *left = *right = 0;
20684
20685 if (glyph->type == CHAR_GLYPH)
20686 {
20687 struct face *face;
20688 XChar2b char2b;
20689 struct font_metrics *pcm;
20690
20691 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20692 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20693 {
20694 if (pcm->rbearing > pcm->width)
20695 *right = pcm->rbearing - pcm->width;
20696 if (pcm->lbearing < 0)
20697 *left = -pcm->lbearing;
20698 }
20699 }
20700 else if (glyph->type == COMPOSITE_GLYPH)
20701 {
20702 if (! glyph->u.cmp.automatic)
20703 {
20704 struct composition *cmp = composition_table[glyph->u.cmp.id];
20705
20706 if (cmp->rbearing > cmp->pixel_width)
20707 *right = cmp->rbearing - cmp->pixel_width;
20708 if (cmp->lbearing < 0)
20709 *left = - cmp->lbearing;
20710 }
20711 else
20712 {
20713 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20714 struct font_metrics metrics;
20715
20716 composition_gstring_width (gstring, glyph->u.cmp.from,
20717 glyph->u.cmp.to + 1, &metrics);
20718 if (metrics.rbearing > metrics.width)
20719 *right = metrics.rbearing - metrics.width;
20720 if (metrics.lbearing < 0)
20721 *left = - metrics.lbearing;
20722 }
20723 }
20724 }
20725
20726
20727 /* Return the index of the first glyph preceding glyph string S that
20728 is overwritten by S because of S's left overhang. Value is -1
20729 if no glyphs are overwritten. */
20730
20731 static int
20732 left_overwritten (s)
20733 struct glyph_string *s;
20734 {
20735 int k;
20736
20737 if (s->left_overhang)
20738 {
20739 int x = 0, i;
20740 struct glyph *glyphs = s->row->glyphs[s->area];
20741 int first = s->first_glyph - glyphs;
20742
20743 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20744 x -= glyphs[i].pixel_width;
20745
20746 k = i + 1;
20747 }
20748 else
20749 k = -1;
20750
20751 return k;
20752 }
20753
20754
20755 /* Return the index of the first glyph preceding glyph string S that
20756 is overwriting S because of its right overhang. Value is -1 if no
20757 glyph in front of S overwrites S. */
20758
20759 static int
20760 left_overwriting (s)
20761 struct glyph_string *s;
20762 {
20763 int i, k, x;
20764 struct glyph *glyphs = s->row->glyphs[s->area];
20765 int first = s->first_glyph - glyphs;
20766
20767 k = -1;
20768 x = 0;
20769 for (i = first - 1; i >= 0; --i)
20770 {
20771 int left, right;
20772 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20773 if (x + right > 0)
20774 k = i;
20775 x -= glyphs[i].pixel_width;
20776 }
20777
20778 return k;
20779 }
20780
20781
20782 /* Return the index of the last glyph following glyph string S that is
20783 overwritten by S because of S's right overhang. Value is -1 if
20784 no such glyph is found. */
20785
20786 static int
20787 right_overwritten (s)
20788 struct glyph_string *s;
20789 {
20790 int k = -1;
20791
20792 if (s->right_overhang)
20793 {
20794 int x = 0, i;
20795 struct glyph *glyphs = s->row->glyphs[s->area];
20796 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20797 int end = s->row->used[s->area];
20798
20799 for (i = first; i < end && s->right_overhang > x; ++i)
20800 x += glyphs[i].pixel_width;
20801
20802 k = i;
20803 }
20804
20805 return k;
20806 }
20807
20808
20809 /* Return the index of the last glyph following glyph string S that
20810 overwrites S because of its left overhang. Value is negative
20811 if no such glyph is found. */
20812
20813 static int
20814 right_overwriting (s)
20815 struct glyph_string *s;
20816 {
20817 int i, k, x;
20818 int end = s->row->used[s->area];
20819 struct glyph *glyphs = s->row->glyphs[s->area];
20820 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20821
20822 k = -1;
20823 x = 0;
20824 for (i = first; i < end; ++i)
20825 {
20826 int left, right;
20827 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20828 if (x - left < 0)
20829 k = i;
20830 x += glyphs[i].pixel_width;
20831 }
20832
20833 return k;
20834 }
20835
20836
20837 /* Set background width of glyph string S. START is the index of the
20838 first glyph following S. LAST_X is the right-most x-position + 1
20839 in the drawing area. */
20840
20841 static INLINE void
20842 set_glyph_string_background_width (s, start, last_x)
20843 struct glyph_string *s;
20844 int start;
20845 int last_x;
20846 {
20847 /* If the face of this glyph string has to be drawn to the end of
20848 the drawing area, set S->extends_to_end_of_line_p. */
20849
20850 if (start == s->row->used[s->area]
20851 && s->area == TEXT_AREA
20852 && ((s->row->fill_line_p
20853 && (s->hl == DRAW_NORMAL_TEXT
20854 || s->hl == DRAW_IMAGE_RAISED
20855 || s->hl == DRAW_IMAGE_SUNKEN))
20856 || s->hl == DRAW_MOUSE_FACE))
20857 s->extends_to_end_of_line_p = 1;
20858
20859 /* If S extends its face to the end of the line, set its
20860 background_width to the distance to the right edge of the drawing
20861 area. */
20862 if (s->extends_to_end_of_line_p)
20863 s->background_width = last_x - s->x + 1;
20864 else
20865 s->background_width = s->width;
20866 }
20867
20868
20869 /* Compute overhangs and x-positions for glyph string S and its
20870 predecessors, or successors. X is the starting x-position for S.
20871 BACKWARD_P non-zero means process predecessors. */
20872
20873 static void
20874 compute_overhangs_and_x (s, x, backward_p)
20875 struct glyph_string *s;
20876 int x;
20877 int backward_p;
20878 {
20879 if (backward_p)
20880 {
20881 while (s)
20882 {
20883 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20884 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20885 x -= s->width;
20886 s->x = x;
20887 s = s->prev;
20888 }
20889 }
20890 else
20891 {
20892 while (s)
20893 {
20894 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20895 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20896 s->x = x;
20897 x += s->width;
20898 s = s->next;
20899 }
20900 }
20901 }
20902
20903
20904
20905 /* The following macros are only called from draw_glyphs below.
20906 They reference the following parameters of that function directly:
20907 `w', `row', `area', and `overlap_p'
20908 as well as the following local variables:
20909 `s', `f', and `hdc' (in W32) */
20910
20911 #ifdef HAVE_NTGUI
20912 /* On W32, silently add local `hdc' variable to argument list of
20913 init_glyph_string. */
20914 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20915 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20916 #else
20917 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20918 init_glyph_string (s, char2b, w, row, area, start, hl)
20919 #endif
20920
20921 /* Add a glyph string for a stretch glyph to the list of strings
20922 between HEAD and TAIL. START is the index of the stretch glyph in
20923 row area AREA of glyph row ROW. END is the index of the last glyph
20924 in that glyph row area. X is the current output position assigned
20925 to the new glyph string constructed. HL overrides that face of the
20926 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20927 is the right-most x-position of the drawing area. */
20928
20929 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20930 and below -- keep them on one line. */
20931 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20932 do \
20933 { \
20934 s = (struct glyph_string *) alloca (sizeof *s); \
20935 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20936 START = fill_stretch_glyph_string (s, row, area, START, END); \
20937 append_glyph_string (&HEAD, &TAIL, s); \
20938 s->x = (X); \
20939 } \
20940 while (0)
20941
20942
20943 /* Add a glyph string for an image glyph to the list of strings
20944 between HEAD and TAIL. START is the index of the image glyph in
20945 row area AREA of glyph row ROW. END is the index of the last glyph
20946 in that glyph row area. X is the current output position assigned
20947 to the new glyph string constructed. HL overrides that face of the
20948 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20949 is the right-most x-position of the drawing area. */
20950
20951 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20952 do \
20953 { \
20954 s = (struct glyph_string *) alloca (sizeof *s); \
20955 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20956 fill_image_glyph_string (s); \
20957 append_glyph_string (&HEAD, &TAIL, s); \
20958 ++START; \
20959 s->x = (X); \
20960 } \
20961 while (0)
20962
20963
20964 /* Add a glyph string for a sequence of character glyphs to the list
20965 of strings between HEAD and TAIL. START is the index of the first
20966 glyph in row area AREA of glyph row ROW that is part of the new
20967 glyph string. END is the index of the last glyph in that glyph row
20968 area. X is the current output position assigned to the new glyph
20969 string constructed. HL overrides that face of the glyph; e.g. it
20970 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20971 right-most x-position of the drawing area. */
20972
20973 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20974 do \
20975 { \
20976 int face_id; \
20977 XChar2b *char2b; \
20978 \
20979 face_id = (row)->glyphs[area][START].face_id; \
20980 \
20981 s = (struct glyph_string *) alloca (sizeof *s); \
20982 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20983 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20984 append_glyph_string (&HEAD, &TAIL, s); \
20985 s->x = (X); \
20986 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20987 } \
20988 while (0)
20989
20990
20991 /* Add a glyph string for a composite sequence to the list of strings
20992 between HEAD and TAIL. START is the index of the first glyph in
20993 row area AREA of glyph row ROW that is part of the new glyph
20994 string. END is the index of the last glyph in that glyph row area.
20995 X is the current output position assigned to the new glyph string
20996 constructed. HL overrides that face of the glyph; e.g. it is
20997 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20998 x-position of the drawing area. */
20999
21000 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21001 do { \
21002 int face_id = (row)->glyphs[area][START].face_id; \
21003 struct face *base_face = FACE_FROM_ID (f, face_id); \
21004 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21005 struct composition *cmp = composition_table[cmp_id]; \
21006 XChar2b *char2b; \
21007 struct glyph_string *first_s; \
21008 int n; \
21009 \
21010 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21011 \
21012 /* Make glyph_strings for each glyph sequence that is drawable by \
21013 the same face, and append them to HEAD/TAIL. */ \
21014 for (n = 0; n < cmp->glyph_len;) \
21015 { \
21016 s = (struct glyph_string *) alloca (sizeof *s); \
21017 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21018 append_glyph_string (&(HEAD), &(TAIL), s); \
21019 s->cmp = cmp; \
21020 s->cmp_from = n; \
21021 s->x = (X); \
21022 if (n == 0) \
21023 first_s = s; \
21024 n = fill_composite_glyph_string (s, base_face, overlaps); \
21025 } \
21026 \
21027 ++START; \
21028 s = first_s; \
21029 } while (0)
21030
21031
21032 /* Add a glyph string for a glyph-string sequence to the list of strings
21033 between HEAD and TAIL. */
21034
21035 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21036 do { \
21037 int face_id; \
21038 XChar2b *char2b; \
21039 Lisp_Object gstring; \
21040 \
21041 face_id = (row)->glyphs[area][START].face_id; \
21042 gstring = (composition_gstring_from_id \
21043 ((row)->glyphs[area][START].u.cmp.id)); \
21044 s = (struct glyph_string *) alloca (sizeof *s); \
21045 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21046 * LGSTRING_GLYPH_LEN (gstring)); \
21047 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21048 append_glyph_string (&(HEAD), &(TAIL), s); \
21049 s->x = (X); \
21050 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21051 } while (0)
21052
21053
21054 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21055 of AREA of glyph row ROW on window W between indices START and END.
21056 HL overrides the face for drawing glyph strings, e.g. it is
21057 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21058 x-positions of the drawing area.
21059
21060 This is an ugly monster macro construct because we must use alloca
21061 to allocate glyph strings (because draw_glyphs can be called
21062 asynchronously). */
21063
21064 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21065 do \
21066 { \
21067 HEAD = TAIL = NULL; \
21068 while (START < END) \
21069 { \
21070 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21071 switch (first_glyph->type) \
21072 { \
21073 case CHAR_GLYPH: \
21074 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21075 HL, X, LAST_X); \
21076 break; \
21077 \
21078 case COMPOSITE_GLYPH: \
21079 if (first_glyph->u.cmp.automatic) \
21080 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21081 HL, X, LAST_X); \
21082 else \
21083 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21084 HL, X, LAST_X); \
21085 break; \
21086 \
21087 case STRETCH_GLYPH: \
21088 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21089 HL, X, LAST_X); \
21090 break; \
21091 \
21092 case IMAGE_GLYPH: \
21093 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21094 HL, X, LAST_X); \
21095 break; \
21096 \
21097 default: \
21098 abort (); \
21099 } \
21100 \
21101 if (s) \
21102 { \
21103 set_glyph_string_background_width (s, START, LAST_X); \
21104 (X) += s->width; \
21105 } \
21106 } \
21107 } while (0)
21108
21109
21110 /* Draw glyphs between START and END in AREA of ROW on window W,
21111 starting at x-position X. X is relative to AREA in W. HL is a
21112 face-override with the following meaning:
21113
21114 DRAW_NORMAL_TEXT draw normally
21115 DRAW_CURSOR draw in cursor face
21116 DRAW_MOUSE_FACE draw in mouse face.
21117 DRAW_INVERSE_VIDEO draw in mode line face
21118 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21119 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21120
21121 If OVERLAPS is non-zero, draw only the foreground of characters and
21122 clip to the physical height of ROW. Non-zero value also defines
21123 the overlapping part to be drawn:
21124
21125 OVERLAPS_PRED overlap with preceding rows
21126 OVERLAPS_SUCC overlap with succeeding rows
21127 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21128 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21129
21130 Value is the x-position reached, relative to AREA of W. */
21131
21132 static int
21133 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
21134 struct window *w;
21135 int x;
21136 struct glyph_row *row;
21137 enum glyph_row_area area;
21138 EMACS_INT start, end;
21139 enum draw_glyphs_face hl;
21140 int overlaps;
21141 {
21142 struct glyph_string *head, *tail;
21143 struct glyph_string *s;
21144 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21145 int i, j, x_reached, last_x, area_left = 0;
21146 struct frame *f = XFRAME (WINDOW_FRAME (w));
21147 DECLARE_HDC (hdc);
21148
21149 ALLOCATE_HDC (hdc, f);
21150
21151 /* Let's rather be paranoid than getting a SEGV. */
21152 end = min (end, row->used[area]);
21153 start = max (0, start);
21154 start = min (end, start);
21155
21156 /* Translate X to frame coordinates. Set last_x to the right
21157 end of the drawing area. */
21158 if (row->full_width_p)
21159 {
21160 /* X is relative to the left edge of W, without scroll bars
21161 or fringes. */
21162 area_left = WINDOW_LEFT_EDGE_X (w);
21163 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21164 }
21165 else
21166 {
21167 area_left = window_box_left (w, area);
21168 last_x = area_left + window_box_width (w, area);
21169 }
21170 x += area_left;
21171
21172 /* Build a doubly-linked list of glyph_string structures between
21173 head and tail from what we have to draw. Note that the macro
21174 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21175 the reason we use a separate variable `i'. */
21176 i = start;
21177 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21178 if (tail)
21179 x_reached = tail->x + tail->background_width;
21180 else
21181 x_reached = x;
21182
21183 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21184 the row, redraw some glyphs in front or following the glyph
21185 strings built above. */
21186 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21187 {
21188 struct glyph_string *h, *t;
21189 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21190 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21191 int dummy_x = 0;
21192
21193 /* If mouse highlighting is on, we may need to draw adjacent
21194 glyphs using mouse-face highlighting. */
21195 if (area == TEXT_AREA && row->mouse_face_p)
21196 {
21197 struct glyph_row *mouse_beg_row, *mouse_end_row;
21198
21199 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21200 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21201
21202 if (row >= mouse_beg_row && row <= mouse_end_row)
21203 {
21204 check_mouse_face = 1;
21205 mouse_beg_col = (row == mouse_beg_row)
21206 ? dpyinfo->mouse_face_beg_col : 0;
21207 mouse_end_col = (row == mouse_end_row)
21208 ? dpyinfo->mouse_face_end_col
21209 : row->used[TEXT_AREA];
21210 }
21211 }
21212
21213 /* Compute overhangs for all glyph strings. */
21214 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21215 for (s = head; s; s = s->next)
21216 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21217
21218 /* Prepend glyph strings for glyphs in front of the first glyph
21219 string that are overwritten because of the first glyph
21220 string's left overhang. The background of all strings
21221 prepended must be drawn because the first glyph string
21222 draws over it. */
21223 i = left_overwritten (head);
21224 if (i >= 0)
21225 {
21226 enum draw_glyphs_face overlap_hl;
21227
21228 /* If this row contains mouse highlighting, attempt to draw
21229 the overlapped glyphs with the correct highlight. This
21230 code fails if the overlap encompasses more than one glyph
21231 and mouse-highlight spans only some of these glyphs.
21232 However, making it work perfectly involves a lot more
21233 code, and I don't know if the pathological case occurs in
21234 practice, so we'll stick to this for now. --- cyd */
21235 if (check_mouse_face
21236 && mouse_beg_col < start && mouse_end_col > i)
21237 overlap_hl = DRAW_MOUSE_FACE;
21238 else
21239 overlap_hl = DRAW_NORMAL_TEXT;
21240
21241 j = i;
21242 BUILD_GLYPH_STRINGS (j, start, h, t,
21243 overlap_hl, dummy_x, last_x);
21244 start = i;
21245 compute_overhangs_and_x (t, head->x, 1);
21246 prepend_glyph_string_lists (&head, &tail, h, t);
21247 clip_head = head;
21248 }
21249
21250 /* Prepend glyph strings for glyphs in front of the first glyph
21251 string that overwrite that glyph string because of their
21252 right overhang. For these strings, only the foreground must
21253 be drawn, because it draws over the glyph string at `head'.
21254 The background must not be drawn because this would overwrite
21255 right overhangs of preceding glyphs for which no glyph
21256 strings exist. */
21257 i = left_overwriting (head);
21258 if (i >= 0)
21259 {
21260 enum draw_glyphs_face overlap_hl;
21261
21262 if (check_mouse_face
21263 && mouse_beg_col < start && mouse_end_col > i)
21264 overlap_hl = DRAW_MOUSE_FACE;
21265 else
21266 overlap_hl = DRAW_NORMAL_TEXT;
21267
21268 clip_head = head;
21269 BUILD_GLYPH_STRINGS (i, start, h, t,
21270 overlap_hl, dummy_x, last_x);
21271 for (s = h; s; s = s->next)
21272 s->background_filled_p = 1;
21273 compute_overhangs_and_x (t, head->x, 1);
21274 prepend_glyph_string_lists (&head, &tail, h, t);
21275 }
21276
21277 /* Append glyphs strings for glyphs following the last glyph
21278 string tail that are overwritten by tail. The background of
21279 these strings has to be drawn because tail's foreground draws
21280 over it. */
21281 i = right_overwritten (tail);
21282 if (i >= 0)
21283 {
21284 enum draw_glyphs_face overlap_hl;
21285
21286 if (check_mouse_face
21287 && mouse_beg_col < i && mouse_end_col > end)
21288 overlap_hl = DRAW_MOUSE_FACE;
21289 else
21290 overlap_hl = DRAW_NORMAL_TEXT;
21291
21292 BUILD_GLYPH_STRINGS (end, i, h, t,
21293 overlap_hl, x, last_x);
21294 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21295 we don't have `end = i;' here. */
21296 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21297 append_glyph_string_lists (&head, &tail, h, t);
21298 clip_tail = tail;
21299 }
21300
21301 /* Append glyph strings for glyphs following the last glyph
21302 string tail that overwrite tail. The foreground of such
21303 glyphs has to be drawn because it writes into the background
21304 of tail. The background must not be drawn because it could
21305 paint over the foreground of following glyphs. */
21306 i = right_overwriting (tail);
21307 if (i >= 0)
21308 {
21309 enum draw_glyphs_face overlap_hl;
21310 if (check_mouse_face
21311 && mouse_beg_col < i && mouse_end_col > end)
21312 overlap_hl = DRAW_MOUSE_FACE;
21313 else
21314 overlap_hl = DRAW_NORMAL_TEXT;
21315
21316 clip_tail = tail;
21317 i++; /* We must include the Ith glyph. */
21318 BUILD_GLYPH_STRINGS (end, i, h, t,
21319 overlap_hl, x, last_x);
21320 for (s = h; s; s = s->next)
21321 s->background_filled_p = 1;
21322 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21323 append_glyph_string_lists (&head, &tail, h, t);
21324 }
21325 if (clip_head || clip_tail)
21326 for (s = head; s; s = s->next)
21327 {
21328 s->clip_head = clip_head;
21329 s->clip_tail = clip_tail;
21330 }
21331 }
21332
21333 /* Draw all strings. */
21334 for (s = head; s; s = s->next)
21335 FRAME_RIF (f)->draw_glyph_string (s);
21336
21337 #ifndef HAVE_NS
21338 /* When focus a sole frame and move horizontally, this sets on_p to 0
21339 causing a failure to erase prev cursor position. */
21340 if (area == TEXT_AREA
21341 && !row->full_width_p
21342 /* When drawing overlapping rows, only the glyph strings'
21343 foreground is drawn, which doesn't erase a cursor
21344 completely. */
21345 && !overlaps)
21346 {
21347 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21348 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21349 : (tail ? tail->x + tail->background_width : x));
21350 x0 -= area_left;
21351 x1 -= area_left;
21352
21353 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21354 row->y, MATRIX_ROW_BOTTOM_Y (row));
21355 }
21356 #endif
21357
21358 /* Value is the x-position up to which drawn, relative to AREA of W.
21359 This doesn't include parts drawn because of overhangs. */
21360 if (row->full_width_p)
21361 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21362 else
21363 x_reached -= area_left;
21364
21365 RELEASE_HDC (hdc, f);
21366
21367 return x_reached;
21368 }
21369
21370 /* Expand row matrix if too narrow. Don't expand if area
21371 is not present. */
21372
21373 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21374 { \
21375 if (!fonts_changed_p \
21376 && (it->glyph_row->glyphs[area] \
21377 < it->glyph_row->glyphs[area + 1])) \
21378 { \
21379 it->w->ncols_scale_factor++; \
21380 fonts_changed_p = 1; \
21381 } \
21382 }
21383
21384 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21385 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21386
21387 static INLINE void
21388 append_glyph (it)
21389 struct it *it;
21390 {
21391 struct glyph *glyph;
21392 enum glyph_row_area area = it->area;
21393
21394 xassert (it->glyph_row);
21395 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21396
21397 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21398 if (glyph < it->glyph_row->glyphs[area + 1])
21399 {
21400 /* If the glyph row is reversed, we need to prepend the glyph
21401 rather than append it. */
21402 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21403 {
21404 struct glyph *g;
21405
21406 /* Make room for the additional glyph. */
21407 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21408 g[1] = *g;
21409 glyph = it->glyph_row->glyphs[area];
21410 }
21411 glyph->charpos = CHARPOS (it->position);
21412 glyph->object = it->object;
21413 if (it->pixel_width > 0)
21414 {
21415 glyph->pixel_width = it->pixel_width;
21416 glyph->padding_p = 0;
21417 }
21418 else
21419 {
21420 /* Assure at least 1-pixel width. Otherwise, cursor can't
21421 be displayed correctly. */
21422 glyph->pixel_width = 1;
21423 glyph->padding_p = 1;
21424 }
21425 glyph->ascent = it->ascent;
21426 glyph->descent = it->descent;
21427 glyph->voffset = it->voffset;
21428 glyph->type = CHAR_GLYPH;
21429 glyph->avoid_cursor_p = it->avoid_cursor_p;
21430 glyph->multibyte_p = it->multibyte_p;
21431 glyph->left_box_line_p = it->start_of_box_run_p;
21432 glyph->right_box_line_p = it->end_of_box_run_p;
21433 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21434 || it->phys_descent > it->descent);
21435 glyph->glyph_not_available_p = it->glyph_not_available_p;
21436 glyph->face_id = it->face_id;
21437 glyph->u.ch = it->char_to_display;
21438 glyph->slice = null_glyph_slice;
21439 glyph->font_type = FONT_TYPE_UNKNOWN;
21440 if (it->bidi_p)
21441 {
21442 glyph->resolved_level = it->bidi_it.resolved_level;
21443 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21444 abort ();
21445 glyph->bidi_type = it->bidi_it.type;
21446 }
21447 else
21448 {
21449 glyph->resolved_level = 0;
21450 glyph->bidi_type = UNKNOWN_BT;
21451 }
21452 ++it->glyph_row->used[area];
21453 }
21454 else
21455 IT_EXPAND_MATRIX_WIDTH (it, area);
21456 }
21457
21458 /* Store one glyph for the composition IT->cmp_it.id in
21459 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21460 non-null. */
21461
21462 static INLINE void
21463 append_composite_glyph (it)
21464 struct it *it;
21465 {
21466 struct glyph *glyph;
21467 enum glyph_row_area area = it->area;
21468
21469 xassert (it->glyph_row);
21470
21471 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21472 if (glyph < it->glyph_row->glyphs[area + 1])
21473 {
21474 glyph->charpos = CHARPOS (it->position);
21475 glyph->object = it->object;
21476 glyph->pixel_width = it->pixel_width;
21477 glyph->ascent = it->ascent;
21478 glyph->descent = it->descent;
21479 glyph->voffset = it->voffset;
21480 glyph->type = COMPOSITE_GLYPH;
21481 if (it->cmp_it.ch < 0)
21482 {
21483 glyph->u.cmp.automatic = 0;
21484 glyph->u.cmp.id = it->cmp_it.id;
21485 }
21486 else
21487 {
21488 glyph->u.cmp.automatic = 1;
21489 glyph->u.cmp.id = it->cmp_it.id;
21490 glyph->u.cmp.from = it->cmp_it.from;
21491 glyph->u.cmp.to = it->cmp_it.to - 1;
21492 }
21493 glyph->avoid_cursor_p = it->avoid_cursor_p;
21494 glyph->multibyte_p = it->multibyte_p;
21495 glyph->left_box_line_p = it->start_of_box_run_p;
21496 glyph->right_box_line_p = it->end_of_box_run_p;
21497 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21498 || it->phys_descent > it->descent);
21499 glyph->padding_p = 0;
21500 glyph->glyph_not_available_p = 0;
21501 glyph->face_id = it->face_id;
21502 glyph->slice = null_glyph_slice;
21503 glyph->font_type = FONT_TYPE_UNKNOWN;
21504 if (it->bidi_p)
21505 {
21506 glyph->resolved_level = it->bidi_it.resolved_level;
21507 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21508 abort ();
21509 glyph->bidi_type = it->bidi_it.type;
21510 }
21511 ++it->glyph_row->used[area];
21512 }
21513 else
21514 IT_EXPAND_MATRIX_WIDTH (it, area);
21515 }
21516
21517
21518 /* Change IT->ascent and IT->height according to the setting of
21519 IT->voffset. */
21520
21521 static INLINE void
21522 take_vertical_position_into_account (it)
21523 struct it *it;
21524 {
21525 if (it->voffset)
21526 {
21527 if (it->voffset < 0)
21528 /* Increase the ascent so that we can display the text higher
21529 in the line. */
21530 it->ascent -= it->voffset;
21531 else
21532 /* Increase the descent so that we can display the text lower
21533 in the line. */
21534 it->descent += it->voffset;
21535 }
21536 }
21537
21538
21539 /* Produce glyphs/get display metrics for the image IT is loaded with.
21540 See the description of struct display_iterator in dispextern.h for
21541 an overview of struct display_iterator. */
21542
21543 static void
21544 produce_image_glyph (it)
21545 struct it *it;
21546 {
21547 struct image *img;
21548 struct face *face;
21549 int glyph_ascent, crop;
21550 struct glyph_slice slice;
21551
21552 xassert (it->what == IT_IMAGE);
21553
21554 face = FACE_FROM_ID (it->f, it->face_id);
21555 xassert (face);
21556 /* Make sure X resources of the face is loaded. */
21557 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21558
21559 if (it->image_id < 0)
21560 {
21561 /* Fringe bitmap. */
21562 it->ascent = it->phys_ascent = 0;
21563 it->descent = it->phys_descent = 0;
21564 it->pixel_width = 0;
21565 it->nglyphs = 0;
21566 return;
21567 }
21568
21569 img = IMAGE_FROM_ID (it->f, it->image_id);
21570 xassert (img);
21571 /* Make sure X resources of the image is loaded. */
21572 prepare_image_for_display (it->f, img);
21573
21574 slice.x = slice.y = 0;
21575 slice.width = img->width;
21576 slice.height = img->height;
21577
21578 if (INTEGERP (it->slice.x))
21579 slice.x = XINT (it->slice.x);
21580 else if (FLOATP (it->slice.x))
21581 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21582
21583 if (INTEGERP (it->slice.y))
21584 slice.y = XINT (it->slice.y);
21585 else if (FLOATP (it->slice.y))
21586 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21587
21588 if (INTEGERP (it->slice.width))
21589 slice.width = XINT (it->slice.width);
21590 else if (FLOATP (it->slice.width))
21591 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21592
21593 if (INTEGERP (it->slice.height))
21594 slice.height = XINT (it->slice.height);
21595 else if (FLOATP (it->slice.height))
21596 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21597
21598 if (slice.x >= img->width)
21599 slice.x = img->width;
21600 if (slice.y >= img->height)
21601 slice.y = img->height;
21602 if (slice.x + slice.width >= img->width)
21603 slice.width = img->width - slice.x;
21604 if (slice.y + slice.height > img->height)
21605 slice.height = img->height - slice.y;
21606
21607 if (slice.width == 0 || slice.height == 0)
21608 return;
21609
21610 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21611
21612 it->descent = slice.height - glyph_ascent;
21613 if (slice.y == 0)
21614 it->descent += img->vmargin;
21615 if (slice.y + slice.height == img->height)
21616 it->descent += img->vmargin;
21617 it->phys_descent = it->descent;
21618
21619 it->pixel_width = slice.width;
21620 if (slice.x == 0)
21621 it->pixel_width += img->hmargin;
21622 if (slice.x + slice.width == img->width)
21623 it->pixel_width += img->hmargin;
21624
21625 /* It's quite possible for images to have an ascent greater than
21626 their height, so don't get confused in that case. */
21627 if (it->descent < 0)
21628 it->descent = 0;
21629
21630 it->nglyphs = 1;
21631
21632 if (face->box != FACE_NO_BOX)
21633 {
21634 if (face->box_line_width > 0)
21635 {
21636 if (slice.y == 0)
21637 it->ascent += face->box_line_width;
21638 if (slice.y + slice.height == img->height)
21639 it->descent += face->box_line_width;
21640 }
21641
21642 if (it->start_of_box_run_p && slice.x == 0)
21643 it->pixel_width += eabs (face->box_line_width);
21644 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21645 it->pixel_width += eabs (face->box_line_width);
21646 }
21647
21648 take_vertical_position_into_account (it);
21649
21650 /* Automatically crop wide image glyphs at right edge so we can
21651 draw the cursor on same display row. */
21652 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21653 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21654 {
21655 it->pixel_width -= crop;
21656 slice.width -= crop;
21657 }
21658
21659 if (it->glyph_row)
21660 {
21661 struct glyph *glyph;
21662 enum glyph_row_area area = it->area;
21663
21664 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21665 if (glyph < it->glyph_row->glyphs[area + 1])
21666 {
21667 glyph->charpos = CHARPOS (it->position);
21668 glyph->object = it->object;
21669 glyph->pixel_width = it->pixel_width;
21670 glyph->ascent = glyph_ascent;
21671 glyph->descent = it->descent;
21672 glyph->voffset = it->voffset;
21673 glyph->type = IMAGE_GLYPH;
21674 glyph->avoid_cursor_p = it->avoid_cursor_p;
21675 glyph->multibyte_p = it->multibyte_p;
21676 glyph->left_box_line_p = it->start_of_box_run_p;
21677 glyph->right_box_line_p = it->end_of_box_run_p;
21678 glyph->overlaps_vertically_p = 0;
21679 glyph->padding_p = 0;
21680 glyph->glyph_not_available_p = 0;
21681 glyph->face_id = it->face_id;
21682 glyph->u.img_id = img->id;
21683 glyph->slice = slice;
21684 glyph->font_type = FONT_TYPE_UNKNOWN;
21685 if (it->bidi_p)
21686 {
21687 glyph->resolved_level = it->bidi_it.resolved_level;
21688 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21689 abort ();
21690 glyph->bidi_type = it->bidi_it.type;
21691 }
21692 ++it->glyph_row->used[area];
21693 }
21694 else
21695 IT_EXPAND_MATRIX_WIDTH (it, area);
21696 }
21697 }
21698
21699
21700 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21701 of the glyph, WIDTH and HEIGHT are the width and height of the
21702 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21703
21704 static void
21705 append_stretch_glyph (it, object, width, height, ascent)
21706 struct it *it;
21707 Lisp_Object object;
21708 int width, height;
21709 int ascent;
21710 {
21711 struct glyph *glyph;
21712 enum glyph_row_area area = it->area;
21713
21714 xassert (ascent >= 0 && ascent <= height);
21715
21716 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21717 if (glyph < it->glyph_row->glyphs[area + 1])
21718 {
21719 /* If the glyph row is reversed, we need to prepend the glyph
21720 rather than append it. */
21721 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21722 {
21723 struct glyph *g;
21724
21725 /* Make room for the additional glyph. */
21726 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21727 g[1] = *g;
21728 glyph = it->glyph_row->glyphs[area];
21729 }
21730 glyph->charpos = CHARPOS (it->position);
21731 glyph->object = object;
21732 glyph->pixel_width = width;
21733 glyph->ascent = ascent;
21734 glyph->descent = height - ascent;
21735 glyph->voffset = it->voffset;
21736 glyph->type = STRETCH_GLYPH;
21737 glyph->avoid_cursor_p = it->avoid_cursor_p;
21738 glyph->multibyte_p = it->multibyte_p;
21739 glyph->left_box_line_p = it->start_of_box_run_p;
21740 glyph->right_box_line_p = it->end_of_box_run_p;
21741 glyph->overlaps_vertically_p = 0;
21742 glyph->padding_p = 0;
21743 glyph->glyph_not_available_p = 0;
21744 glyph->face_id = it->face_id;
21745 glyph->u.stretch.ascent = ascent;
21746 glyph->u.stretch.height = height;
21747 glyph->slice = null_glyph_slice;
21748 glyph->font_type = FONT_TYPE_UNKNOWN;
21749 if (it->bidi_p)
21750 {
21751 glyph->resolved_level = it->bidi_it.resolved_level;
21752 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21753 abort ();
21754 glyph->bidi_type = it->bidi_it.type;
21755 }
21756 else
21757 {
21758 glyph->resolved_level = 0;
21759 glyph->bidi_type = UNKNOWN_BT;
21760 }
21761 ++it->glyph_row->used[area];
21762 }
21763 else
21764 IT_EXPAND_MATRIX_WIDTH (it, area);
21765 }
21766
21767
21768 /* Produce a stretch glyph for iterator IT. IT->object is the value
21769 of the glyph property displayed. The value must be a list
21770 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21771 being recognized:
21772
21773 1. `:width WIDTH' specifies that the space should be WIDTH *
21774 canonical char width wide. WIDTH may be an integer or floating
21775 point number.
21776
21777 2. `:relative-width FACTOR' specifies that the width of the stretch
21778 should be computed from the width of the first character having the
21779 `glyph' property, and should be FACTOR times that width.
21780
21781 3. `:align-to HPOS' specifies that the space should be wide enough
21782 to reach HPOS, a value in canonical character units.
21783
21784 Exactly one of the above pairs must be present.
21785
21786 4. `:height HEIGHT' specifies that the height of the stretch produced
21787 should be HEIGHT, measured in canonical character units.
21788
21789 5. `:relative-height FACTOR' specifies that the height of the
21790 stretch should be FACTOR times the height of the characters having
21791 the glyph property.
21792
21793 Either none or exactly one of 4 or 5 must be present.
21794
21795 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21796 of the stretch should be used for the ascent of the stretch.
21797 ASCENT must be in the range 0 <= ASCENT <= 100. */
21798
21799 static void
21800 produce_stretch_glyph (it)
21801 struct it *it;
21802 {
21803 /* (space :width WIDTH :height HEIGHT ...) */
21804 Lisp_Object prop, plist;
21805 int width = 0, height = 0, align_to = -1;
21806 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21807 int ascent = 0;
21808 double tem;
21809 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21810 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21811
21812 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21813
21814 /* List should start with `space'. */
21815 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21816 plist = XCDR (it->object);
21817
21818 /* Compute the width of the stretch. */
21819 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21820 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21821 {
21822 /* Absolute width `:width WIDTH' specified and valid. */
21823 zero_width_ok_p = 1;
21824 width = (int)tem;
21825 }
21826 else if (prop = Fplist_get (plist, QCrelative_width),
21827 NUMVAL (prop) > 0)
21828 {
21829 /* Relative width `:relative-width FACTOR' specified and valid.
21830 Compute the width of the characters having the `glyph'
21831 property. */
21832 struct it it2;
21833 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21834
21835 it2 = *it;
21836 if (it->multibyte_p)
21837 {
21838 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
21839 - IT_BYTEPOS (*it));
21840 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
21841 }
21842 else
21843 it2.c = *p, it2.len = 1;
21844
21845 it2.glyph_row = NULL;
21846 it2.what = IT_CHARACTER;
21847 x_produce_glyphs (&it2);
21848 width = NUMVAL (prop) * it2.pixel_width;
21849 }
21850 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21851 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21852 {
21853 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21854 align_to = (align_to < 0
21855 ? 0
21856 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21857 else if (align_to < 0)
21858 align_to = window_box_left_offset (it->w, TEXT_AREA);
21859 width = max (0, (int)tem + align_to - it->current_x);
21860 zero_width_ok_p = 1;
21861 }
21862 else
21863 /* Nothing specified -> width defaults to canonical char width. */
21864 width = FRAME_COLUMN_WIDTH (it->f);
21865
21866 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21867 width = 1;
21868
21869 /* Compute height. */
21870 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21871 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21872 {
21873 height = (int)tem;
21874 zero_height_ok_p = 1;
21875 }
21876 else if (prop = Fplist_get (plist, QCrelative_height),
21877 NUMVAL (prop) > 0)
21878 height = FONT_HEIGHT (font) * NUMVAL (prop);
21879 else
21880 height = FONT_HEIGHT (font);
21881
21882 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21883 height = 1;
21884
21885 /* Compute percentage of height used for ascent. If
21886 `:ascent ASCENT' is present and valid, use that. Otherwise,
21887 derive the ascent from the font in use. */
21888 if (prop = Fplist_get (plist, QCascent),
21889 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21890 ascent = height * NUMVAL (prop) / 100.0;
21891 else if (!NILP (prop)
21892 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21893 ascent = min (max (0, (int)tem), height);
21894 else
21895 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21896
21897 if (width > 0 && it->line_wrap != TRUNCATE
21898 && it->current_x + width > it->last_visible_x)
21899 width = it->last_visible_x - it->current_x - 1;
21900
21901 if (width > 0 && height > 0 && it->glyph_row)
21902 {
21903 Lisp_Object object = it->stack[it->sp - 1].string;
21904 if (!STRINGP (object))
21905 object = it->w->buffer;
21906 append_stretch_glyph (it, object, width, height, ascent);
21907 }
21908
21909 it->pixel_width = width;
21910 it->ascent = it->phys_ascent = ascent;
21911 it->descent = it->phys_descent = height - it->ascent;
21912 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21913
21914 take_vertical_position_into_account (it);
21915 }
21916
21917 /* Calculate line-height and line-spacing properties.
21918 An integer value specifies explicit pixel value.
21919 A float value specifies relative value to current face height.
21920 A cons (float . face-name) specifies relative value to
21921 height of specified face font.
21922
21923 Returns height in pixels, or nil. */
21924
21925
21926 static Lisp_Object
21927 calc_line_height_property (it, val, font, boff, override)
21928 struct it *it;
21929 Lisp_Object val;
21930 struct font *font;
21931 int boff, override;
21932 {
21933 Lisp_Object face_name = Qnil;
21934 int ascent, descent, height;
21935
21936 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21937 return val;
21938
21939 if (CONSP (val))
21940 {
21941 face_name = XCAR (val);
21942 val = XCDR (val);
21943 if (!NUMBERP (val))
21944 val = make_number (1);
21945 if (NILP (face_name))
21946 {
21947 height = it->ascent + it->descent;
21948 goto scale;
21949 }
21950 }
21951
21952 if (NILP (face_name))
21953 {
21954 font = FRAME_FONT (it->f);
21955 boff = FRAME_BASELINE_OFFSET (it->f);
21956 }
21957 else if (EQ (face_name, Qt))
21958 {
21959 override = 0;
21960 }
21961 else
21962 {
21963 int face_id;
21964 struct face *face;
21965
21966 face_id = lookup_named_face (it->f, face_name, 0);
21967 if (face_id < 0)
21968 return make_number (-1);
21969
21970 face = FACE_FROM_ID (it->f, face_id);
21971 font = face->font;
21972 if (font == NULL)
21973 return make_number (-1);
21974 boff = font->baseline_offset;
21975 if (font->vertical_centering)
21976 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21977 }
21978
21979 ascent = FONT_BASE (font) + boff;
21980 descent = FONT_DESCENT (font) - boff;
21981
21982 if (override)
21983 {
21984 it->override_ascent = ascent;
21985 it->override_descent = descent;
21986 it->override_boff = boff;
21987 }
21988
21989 height = ascent + descent;
21990
21991 scale:
21992 if (FLOATP (val))
21993 height = (int)(XFLOAT_DATA (val) * height);
21994 else if (INTEGERP (val))
21995 height *= XINT (val);
21996
21997 return make_number (height);
21998 }
21999
22000
22001 /* RIF:
22002 Produce glyphs/get display metrics for the display element IT is
22003 loaded with. See the description of struct it in dispextern.h
22004 for an overview of struct it. */
22005
22006 void
22007 x_produce_glyphs (it)
22008 struct it *it;
22009 {
22010 int extra_line_spacing = it->extra_line_spacing;
22011
22012 it->glyph_not_available_p = 0;
22013
22014 if (it->what == IT_CHARACTER)
22015 {
22016 XChar2b char2b;
22017 struct font *font;
22018 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22019 struct font_metrics *pcm;
22020 int font_not_found_p;
22021 int boff; /* baseline offset */
22022 /* We may change it->multibyte_p upon unibyte<->multibyte
22023 conversion. So, save the current value now and restore it
22024 later.
22025
22026 Note: It seems that we don't have to record multibyte_p in
22027 struct glyph because the character code itself tells whether
22028 or not the character is multibyte. Thus, in the future, we
22029 must consider eliminating the field `multibyte_p' in the
22030 struct glyph. */
22031 int saved_multibyte_p = it->multibyte_p;
22032
22033 /* Maybe translate single-byte characters to multibyte, or the
22034 other way. */
22035 it->char_to_display = it->c;
22036 if (!ASCII_BYTE_P (it->c)
22037 && ! it->multibyte_p)
22038 {
22039 if (SINGLE_BYTE_CHAR_P (it->c)
22040 && unibyte_display_via_language_environment)
22041 {
22042 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
22043
22044 /* get_next_display_element assures that this decoding
22045 never fails. */
22046 it->char_to_display = DECODE_CHAR (unibyte, it->c);
22047 it->multibyte_p = 1;
22048 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
22049 -1, Qnil);
22050 face = FACE_FROM_ID (it->f, it->face_id);
22051 }
22052 }
22053
22054 /* Get font to use. Encode IT->char_to_display. */
22055 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
22056 &char2b, it->multibyte_p, 0);
22057 font = face->font;
22058
22059 font_not_found_p = font == NULL;
22060 if (font_not_found_p)
22061 {
22062 /* When no suitable font found, display an empty box based
22063 on the metrics of the font of the default face (or what
22064 remapped). */
22065 struct face *no_font_face
22066 = FACE_FROM_ID (it->f,
22067 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
22068 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
22069 font = no_font_face->font;
22070 boff = font->baseline_offset;
22071 }
22072 else
22073 {
22074 boff = font->baseline_offset;
22075 if (font->vertical_centering)
22076 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22077 }
22078
22079 if (it->char_to_display >= ' '
22080 && (!it->multibyte_p || it->char_to_display < 128))
22081 {
22082 /* Either unibyte or ASCII. */
22083 int stretched_p;
22084
22085 it->nglyphs = 1;
22086
22087 pcm = get_per_char_metric (it->f, font, &char2b);
22088
22089 if (it->override_ascent >= 0)
22090 {
22091 it->ascent = it->override_ascent;
22092 it->descent = it->override_descent;
22093 boff = it->override_boff;
22094 }
22095 else
22096 {
22097 it->ascent = FONT_BASE (font) + boff;
22098 it->descent = FONT_DESCENT (font) - boff;
22099 }
22100
22101 if (pcm)
22102 {
22103 it->phys_ascent = pcm->ascent + boff;
22104 it->phys_descent = pcm->descent - boff;
22105 it->pixel_width = pcm->width;
22106 }
22107 else
22108 {
22109 it->glyph_not_available_p = 1;
22110 it->phys_ascent = it->ascent;
22111 it->phys_descent = it->descent;
22112 it->pixel_width = FONT_WIDTH (font);
22113 }
22114
22115 if (it->constrain_row_ascent_descent_p)
22116 {
22117 if (it->descent > it->max_descent)
22118 {
22119 it->ascent += it->descent - it->max_descent;
22120 it->descent = it->max_descent;
22121 }
22122 if (it->ascent > it->max_ascent)
22123 {
22124 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22125 it->ascent = it->max_ascent;
22126 }
22127 it->phys_ascent = min (it->phys_ascent, it->ascent);
22128 it->phys_descent = min (it->phys_descent, it->descent);
22129 extra_line_spacing = 0;
22130 }
22131
22132 /* If this is a space inside a region of text with
22133 `space-width' property, change its width. */
22134 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22135 if (stretched_p)
22136 it->pixel_width *= XFLOATINT (it->space_width);
22137
22138 /* If face has a box, add the box thickness to the character
22139 height. If character has a box line to the left and/or
22140 right, add the box line width to the character's width. */
22141 if (face->box != FACE_NO_BOX)
22142 {
22143 int thick = face->box_line_width;
22144
22145 if (thick > 0)
22146 {
22147 it->ascent += thick;
22148 it->descent += thick;
22149 }
22150 else
22151 thick = -thick;
22152
22153 if (it->start_of_box_run_p)
22154 it->pixel_width += thick;
22155 if (it->end_of_box_run_p)
22156 it->pixel_width += thick;
22157 }
22158
22159 /* If face has an overline, add the height of the overline
22160 (1 pixel) and a 1 pixel margin to the character height. */
22161 if (face->overline_p)
22162 it->ascent += overline_margin;
22163
22164 if (it->constrain_row_ascent_descent_p)
22165 {
22166 if (it->ascent > it->max_ascent)
22167 it->ascent = it->max_ascent;
22168 if (it->descent > it->max_descent)
22169 it->descent = it->max_descent;
22170 }
22171
22172 take_vertical_position_into_account (it);
22173
22174 /* If we have to actually produce glyphs, do it. */
22175 if (it->glyph_row)
22176 {
22177 if (stretched_p)
22178 {
22179 /* Translate a space with a `space-width' property
22180 into a stretch glyph. */
22181 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22182 / FONT_HEIGHT (font));
22183 append_stretch_glyph (it, it->object, it->pixel_width,
22184 it->ascent + it->descent, ascent);
22185 }
22186 else
22187 append_glyph (it);
22188
22189 /* If characters with lbearing or rbearing are displayed
22190 in this line, record that fact in a flag of the
22191 glyph row. This is used to optimize X output code. */
22192 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22193 it->glyph_row->contains_overlapping_glyphs_p = 1;
22194 }
22195 if (! stretched_p && it->pixel_width == 0)
22196 /* We assure that all visible glyphs have at least 1-pixel
22197 width. */
22198 it->pixel_width = 1;
22199 }
22200 else if (it->char_to_display == '\n')
22201 {
22202 /* A newline has no width, but we need the height of the
22203 line. But if previous part of the line sets a height,
22204 don't increase that height */
22205
22206 Lisp_Object height;
22207 Lisp_Object total_height = Qnil;
22208
22209 it->override_ascent = -1;
22210 it->pixel_width = 0;
22211 it->nglyphs = 0;
22212
22213 height = get_it_property(it, Qline_height);
22214 /* Split (line-height total-height) list */
22215 if (CONSP (height)
22216 && CONSP (XCDR (height))
22217 && NILP (XCDR (XCDR (height))))
22218 {
22219 total_height = XCAR (XCDR (height));
22220 height = XCAR (height);
22221 }
22222 height = calc_line_height_property(it, height, font, boff, 1);
22223
22224 if (it->override_ascent >= 0)
22225 {
22226 it->ascent = it->override_ascent;
22227 it->descent = it->override_descent;
22228 boff = it->override_boff;
22229 }
22230 else
22231 {
22232 it->ascent = FONT_BASE (font) + boff;
22233 it->descent = FONT_DESCENT (font) - boff;
22234 }
22235
22236 if (EQ (height, Qt))
22237 {
22238 if (it->descent > it->max_descent)
22239 {
22240 it->ascent += it->descent - it->max_descent;
22241 it->descent = it->max_descent;
22242 }
22243 if (it->ascent > it->max_ascent)
22244 {
22245 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22246 it->ascent = it->max_ascent;
22247 }
22248 it->phys_ascent = min (it->phys_ascent, it->ascent);
22249 it->phys_descent = min (it->phys_descent, it->descent);
22250 it->constrain_row_ascent_descent_p = 1;
22251 extra_line_spacing = 0;
22252 }
22253 else
22254 {
22255 Lisp_Object spacing;
22256
22257 it->phys_ascent = it->ascent;
22258 it->phys_descent = it->descent;
22259
22260 if ((it->max_ascent > 0 || it->max_descent > 0)
22261 && face->box != FACE_NO_BOX
22262 && face->box_line_width > 0)
22263 {
22264 it->ascent += face->box_line_width;
22265 it->descent += face->box_line_width;
22266 }
22267 if (!NILP (height)
22268 && XINT (height) > it->ascent + it->descent)
22269 it->ascent = XINT (height) - it->descent;
22270
22271 if (!NILP (total_height))
22272 spacing = calc_line_height_property(it, total_height, font, boff, 0);
22273 else
22274 {
22275 spacing = get_it_property(it, Qline_spacing);
22276 spacing = calc_line_height_property(it, spacing, font, boff, 0);
22277 }
22278 if (INTEGERP (spacing))
22279 {
22280 extra_line_spacing = XINT (spacing);
22281 if (!NILP (total_height))
22282 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22283 }
22284 }
22285 }
22286 else if (it->char_to_display == '\t')
22287 {
22288 if (font->space_width > 0)
22289 {
22290 int tab_width = it->tab_width * font->space_width;
22291 int x = it->current_x + it->continuation_lines_width;
22292 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22293
22294 /* If the distance from the current position to the next tab
22295 stop is less than a space character width, use the
22296 tab stop after that. */
22297 if (next_tab_x - x < font->space_width)
22298 next_tab_x += tab_width;
22299
22300 it->pixel_width = next_tab_x - x;
22301 it->nglyphs = 1;
22302 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22303 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22304
22305 if (it->glyph_row)
22306 {
22307 append_stretch_glyph (it, it->object, it->pixel_width,
22308 it->ascent + it->descent, it->ascent);
22309 }
22310 }
22311 else
22312 {
22313 it->pixel_width = 0;
22314 it->nglyphs = 1;
22315 }
22316 }
22317 else
22318 {
22319 /* A multi-byte character. Assume that the display width of the
22320 character is the width of the character multiplied by the
22321 width of the font. */
22322
22323 /* If we found a font, this font should give us the right
22324 metrics. If we didn't find a font, use the frame's
22325 default font and calculate the width of the character by
22326 multiplying the width of font by the width of the
22327 character. */
22328
22329 pcm = get_per_char_metric (it->f, font, &char2b);
22330
22331 if (font_not_found_p || !pcm)
22332 {
22333 int char_width = CHAR_WIDTH (it->char_to_display);
22334
22335 if (char_width == 0)
22336 /* This is a non spacing character. But, as we are
22337 going to display an empty box, the box must occupy
22338 at least one column. */
22339 char_width = 1;
22340 it->glyph_not_available_p = 1;
22341 it->pixel_width = font->space_width * char_width;
22342 it->phys_ascent = FONT_BASE (font) + boff;
22343 it->phys_descent = FONT_DESCENT (font) - boff;
22344 }
22345 else
22346 {
22347 it->pixel_width = pcm->width;
22348 it->phys_ascent = pcm->ascent + boff;
22349 it->phys_descent = pcm->descent - boff;
22350 if (it->glyph_row
22351 && (pcm->lbearing < 0
22352 || pcm->rbearing > pcm->width))
22353 it->glyph_row->contains_overlapping_glyphs_p = 1;
22354 }
22355 it->nglyphs = 1;
22356 it->ascent = FONT_BASE (font) + boff;
22357 it->descent = FONT_DESCENT (font) - boff;
22358 if (face->box != FACE_NO_BOX)
22359 {
22360 int thick = face->box_line_width;
22361
22362 if (thick > 0)
22363 {
22364 it->ascent += thick;
22365 it->descent += thick;
22366 }
22367 else
22368 thick = - thick;
22369
22370 if (it->start_of_box_run_p)
22371 it->pixel_width += thick;
22372 if (it->end_of_box_run_p)
22373 it->pixel_width += thick;
22374 }
22375
22376 /* If face has an overline, add the height of the overline
22377 (1 pixel) and a 1 pixel margin to the character height. */
22378 if (face->overline_p)
22379 it->ascent += overline_margin;
22380
22381 take_vertical_position_into_account (it);
22382
22383 if (it->ascent < 0)
22384 it->ascent = 0;
22385 if (it->descent < 0)
22386 it->descent = 0;
22387
22388 if (it->glyph_row)
22389 append_glyph (it);
22390 if (it->pixel_width == 0)
22391 /* We assure that all visible glyphs have at least 1-pixel
22392 width. */
22393 it->pixel_width = 1;
22394 }
22395 it->multibyte_p = saved_multibyte_p;
22396 }
22397 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22398 {
22399 /* A static composition.
22400
22401 Note: A composition is represented as one glyph in the
22402 glyph matrix. There are no padding glyphs.
22403
22404 Important note: pixel_width, ascent, and descent are the
22405 values of what is drawn by draw_glyphs (i.e. the values of
22406 the overall glyphs composed). */
22407 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22408 int boff; /* baseline offset */
22409 struct composition *cmp = composition_table[it->cmp_it.id];
22410 int glyph_len = cmp->glyph_len;
22411 struct font *font = face->font;
22412
22413 it->nglyphs = 1;
22414
22415 /* If we have not yet calculated pixel size data of glyphs of
22416 the composition for the current face font, calculate them
22417 now. Theoretically, we have to check all fonts for the
22418 glyphs, but that requires much time and memory space. So,
22419 here we check only the font of the first glyph. This may
22420 lead to incorrect display, but it's very rare, and C-l
22421 (recenter-top-bottom) can correct the display anyway. */
22422 if (! cmp->font || cmp->font != font)
22423 {
22424 /* Ascent and descent of the font of the first character
22425 of this composition (adjusted by baseline offset).
22426 Ascent and descent of overall glyphs should not be less
22427 than these, respectively. */
22428 int font_ascent, font_descent, font_height;
22429 /* Bounding box of the overall glyphs. */
22430 int leftmost, rightmost, lowest, highest;
22431 int lbearing, rbearing;
22432 int i, width, ascent, descent;
22433 int left_padded = 0, right_padded = 0;
22434 int c;
22435 XChar2b char2b;
22436 struct font_metrics *pcm;
22437 int font_not_found_p;
22438 int pos;
22439
22440 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22441 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22442 break;
22443 if (glyph_len < cmp->glyph_len)
22444 right_padded = 1;
22445 for (i = 0; i < glyph_len; i++)
22446 {
22447 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22448 break;
22449 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22450 }
22451 if (i > 0)
22452 left_padded = 1;
22453
22454 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22455 : IT_CHARPOS (*it));
22456 /* If no suitable font is found, use the default font. */
22457 font_not_found_p = font == NULL;
22458 if (font_not_found_p)
22459 {
22460 face = face->ascii_face;
22461 font = face->font;
22462 }
22463 boff = font->baseline_offset;
22464 if (font->vertical_centering)
22465 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22466 font_ascent = FONT_BASE (font) + boff;
22467 font_descent = FONT_DESCENT (font) - boff;
22468 font_height = FONT_HEIGHT (font);
22469
22470 cmp->font = (void *) font;
22471
22472 pcm = NULL;
22473 if (! font_not_found_p)
22474 {
22475 get_char_face_and_encoding (it->f, c, it->face_id,
22476 &char2b, it->multibyte_p, 0);
22477 pcm = get_per_char_metric (it->f, font, &char2b);
22478 }
22479
22480 /* Initialize the bounding box. */
22481 if (pcm)
22482 {
22483 width = pcm->width;
22484 ascent = pcm->ascent;
22485 descent = pcm->descent;
22486 lbearing = pcm->lbearing;
22487 rbearing = pcm->rbearing;
22488 }
22489 else
22490 {
22491 width = FONT_WIDTH (font);
22492 ascent = FONT_BASE (font);
22493 descent = FONT_DESCENT (font);
22494 lbearing = 0;
22495 rbearing = width;
22496 }
22497
22498 rightmost = width;
22499 leftmost = 0;
22500 lowest = - descent + boff;
22501 highest = ascent + boff;
22502
22503 if (! font_not_found_p
22504 && font->default_ascent
22505 && CHAR_TABLE_P (Vuse_default_ascent)
22506 && !NILP (Faref (Vuse_default_ascent,
22507 make_number (it->char_to_display))))
22508 highest = font->default_ascent + boff;
22509
22510 /* Draw the first glyph at the normal position. It may be
22511 shifted to right later if some other glyphs are drawn
22512 at the left. */
22513 cmp->offsets[i * 2] = 0;
22514 cmp->offsets[i * 2 + 1] = boff;
22515 cmp->lbearing = lbearing;
22516 cmp->rbearing = rbearing;
22517
22518 /* Set cmp->offsets for the remaining glyphs. */
22519 for (i++; i < glyph_len; i++)
22520 {
22521 int left, right, btm, top;
22522 int ch = COMPOSITION_GLYPH (cmp, i);
22523 int face_id;
22524 struct face *this_face;
22525 int this_boff;
22526
22527 if (ch == '\t')
22528 ch = ' ';
22529 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22530 this_face = FACE_FROM_ID (it->f, face_id);
22531 font = this_face->font;
22532
22533 if (font == NULL)
22534 pcm = NULL;
22535 else
22536 {
22537 this_boff = font->baseline_offset;
22538 if (font->vertical_centering)
22539 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22540 get_char_face_and_encoding (it->f, ch, face_id,
22541 &char2b, it->multibyte_p, 0);
22542 pcm = get_per_char_metric (it->f, font, &char2b);
22543 }
22544 if (! pcm)
22545 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22546 else
22547 {
22548 width = pcm->width;
22549 ascent = pcm->ascent;
22550 descent = pcm->descent;
22551 lbearing = pcm->lbearing;
22552 rbearing = pcm->rbearing;
22553 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22554 {
22555 /* Relative composition with or without
22556 alternate chars. */
22557 left = (leftmost + rightmost - width) / 2;
22558 btm = - descent + boff;
22559 if (font->relative_compose
22560 && (! CHAR_TABLE_P (Vignore_relative_composition)
22561 || NILP (Faref (Vignore_relative_composition,
22562 make_number (ch)))))
22563 {
22564
22565 if (- descent >= font->relative_compose)
22566 /* One extra pixel between two glyphs. */
22567 btm = highest + 1;
22568 else if (ascent <= 0)
22569 /* One extra pixel between two glyphs. */
22570 btm = lowest - 1 - ascent - descent;
22571 }
22572 }
22573 else
22574 {
22575 /* A composition rule is specified by an integer
22576 value that encodes global and new reference
22577 points (GREF and NREF). GREF and NREF are
22578 specified by numbers as below:
22579
22580 0---1---2 -- ascent
22581 | |
22582 | |
22583 | |
22584 9--10--11 -- center
22585 | |
22586 ---3---4---5--- baseline
22587 | |
22588 6---7---8 -- descent
22589 */
22590 int rule = COMPOSITION_RULE (cmp, i);
22591 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22592
22593 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22594 grefx = gref % 3, nrefx = nref % 3;
22595 grefy = gref / 3, nrefy = nref / 3;
22596 if (xoff)
22597 xoff = font_height * (xoff - 128) / 256;
22598 if (yoff)
22599 yoff = font_height * (yoff - 128) / 256;
22600
22601 left = (leftmost
22602 + grefx * (rightmost - leftmost) / 2
22603 - nrefx * width / 2
22604 + xoff);
22605
22606 btm = ((grefy == 0 ? highest
22607 : grefy == 1 ? 0
22608 : grefy == 2 ? lowest
22609 : (highest + lowest) / 2)
22610 - (nrefy == 0 ? ascent + descent
22611 : nrefy == 1 ? descent - boff
22612 : nrefy == 2 ? 0
22613 : (ascent + descent) / 2)
22614 + yoff);
22615 }
22616
22617 cmp->offsets[i * 2] = left;
22618 cmp->offsets[i * 2 + 1] = btm + descent;
22619
22620 /* Update the bounding box of the overall glyphs. */
22621 if (width > 0)
22622 {
22623 right = left + width;
22624 if (left < leftmost)
22625 leftmost = left;
22626 if (right > rightmost)
22627 rightmost = right;
22628 }
22629 top = btm + descent + ascent;
22630 if (top > highest)
22631 highest = top;
22632 if (btm < lowest)
22633 lowest = btm;
22634
22635 if (cmp->lbearing > left + lbearing)
22636 cmp->lbearing = left + lbearing;
22637 if (cmp->rbearing < left + rbearing)
22638 cmp->rbearing = left + rbearing;
22639 }
22640 }
22641
22642 /* If there are glyphs whose x-offsets are negative,
22643 shift all glyphs to the right and make all x-offsets
22644 non-negative. */
22645 if (leftmost < 0)
22646 {
22647 for (i = 0; i < cmp->glyph_len; i++)
22648 cmp->offsets[i * 2] -= leftmost;
22649 rightmost -= leftmost;
22650 cmp->lbearing -= leftmost;
22651 cmp->rbearing -= leftmost;
22652 }
22653
22654 if (left_padded && cmp->lbearing < 0)
22655 {
22656 for (i = 0; i < cmp->glyph_len; i++)
22657 cmp->offsets[i * 2] -= cmp->lbearing;
22658 rightmost -= cmp->lbearing;
22659 cmp->rbearing -= cmp->lbearing;
22660 cmp->lbearing = 0;
22661 }
22662 if (right_padded && rightmost < cmp->rbearing)
22663 {
22664 rightmost = cmp->rbearing;
22665 }
22666
22667 cmp->pixel_width = rightmost;
22668 cmp->ascent = highest;
22669 cmp->descent = - lowest;
22670 if (cmp->ascent < font_ascent)
22671 cmp->ascent = font_ascent;
22672 if (cmp->descent < font_descent)
22673 cmp->descent = font_descent;
22674 }
22675
22676 if (it->glyph_row
22677 && (cmp->lbearing < 0
22678 || cmp->rbearing > cmp->pixel_width))
22679 it->glyph_row->contains_overlapping_glyphs_p = 1;
22680
22681 it->pixel_width = cmp->pixel_width;
22682 it->ascent = it->phys_ascent = cmp->ascent;
22683 it->descent = it->phys_descent = cmp->descent;
22684 if (face->box != FACE_NO_BOX)
22685 {
22686 int thick = face->box_line_width;
22687
22688 if (thick > 0)
22689 {
22690 it->ascent += thick;
22691 it->descent += thick;
22692 }
22693 else
22694 thick = - thick;
22695
22696 if (it->start_of_box_run_p)
22697 it->pixel_width += thick;
22698 if (it->end_of_box_run_p)
22699 it->pixel_width += thick;
22700 }
22701
22702 /* If face has an overline, add the height of the overline
22703 (1 pixel) and a 1 pixel margin to the character height. */
22704 if (face->overline_p)
22705 it->ascent += overline_margin;
22706
22707 take_vertical_position_into_account (it);
22708 if (it->ascent < 0)
22709 it->ascent = 0;
22710 if (it->descent < 0)
22711 it->descent = 0;
22712
22713 if (it->glyph_row)
22714 append_composite_glyph (it);
22715 }
22716 else if (it->what == IT_COMPOSITION)
22717 {
22718 /* A dynamic (automatic) composition. */
22719 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22720 Lisp_Object gstring;
22721 struct font_metrics metrics;
22722
22723 gstring = composition_gstring_from_id (it->cmp_it.id);
22724 it->pixel_width
22725 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22726 &metrics);
22727 if (it->glyph_row
22728 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22729 it->glyph_row->contains_overlapping_glyphs_p = 1;
22730 it->ascent = it->phys_ascent = metrics.ascent;
22731 it->descent = it->phys_descent = metrics.descent;
22732 if (face->box != FACE_NO_BOX)
22733 {
22734 int thick = face->box_line_width;
22735
22736 if (thick > 0)
22737 {
22738 it->ascent += thick;
22739 it->descent += thick;
22740 }
22741 else
22742 thick = - thick;
22743
22744 if (it->start_of_box_run_p)
22745 it->pixel_width += thick;
22746 if (it->end_of_box_run_p)
22747 it->pixel_width += thick;
22748 }
22749 /* If face has an overline, add the height of the overline
22750 (1 pixel) and a 1 pixel margin to the character height. */
22751 if (face->overline_p)
22752 it->ascent += overline_margin;
22753 take_vertical_position_into_account (it);
22754 if (it->ascent < 0)
22755 it->ascent = 0;
22756 if (it->descent < 0)
22757 it->descent = 0;
22758
22759 if (it->glyph_row)
22760 append_composite_glyph (it);
22761 }
22762 else if (it->what == IT_IMAGE)
22763 produce_image_glyph (it);
22764 else if (it->what == IT_STRETCH)
22765 produce_stretch_glyph (it);
22766
22767 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22768 because this isn't true for images with `:ascent 100'. */
22769 xassert (it->ascent >= 0 && it->descent >= 0);
22770 if (it->area == TEXT_AREA)
22771 it->current_x += it->pixel_width;
22772
22773 if (extra_line_spacing > 0)
22774 {
22775 it->descent += extra_line_spacing;
22776 if (extra_line_spacing > it->max_extra_line_spacing)
22777 it->max_extra_line_spacing = extra_line_spacing;
22778 }
22779
22780 it->max_ascent = max (it->max_ascent, it->ascent);
22781 it->max_descent = max (it->max_descent, it->descent);
22782 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22783 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22784 }
22785
22786 /* EXPORT for RIF:
22787 Output LEN glyphs starting at START at the nominal cursor position.
22788 Advance the nominal cursor over the text. The global variable
22789 updated_window contains the window being updated, updated_row is
22790 the glyph row being updated, and updated_area is the area of that
22791 row being updated. */
22792
22793 void
22794 x_write_glyphs (start, len)
22795 struct glyph *start;
22796 int len;
22797 {
22798 int x, hpos;
22799
22800 xassert (updated_window && updated_row);
22801 BLOCK_INPUT;
22802
22803 /* Write glyphs. */
22804
22805 hpos = start - updated_row->glyphs[updated_area];
22806 x = draw_glyphs (updated_window, output_cursor.x,
22807 updated_row, updated_area,
22808 hpos, hpos + len,
22809 DRAW_NORMAL_TEXT, 0);
22810
22811 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22812 if (updated_area == TEXT_AREA
22813 && updated_window->phys_cursor_on_p
22814 && updated_window->phys_cursor.vpos == output_cursor.vpos
22815 && updated_window->phys_cursor.hpos >= hpos
22816 && updated_window->phys_cursor.hpos < hpos + len)
22817 updated_window->phys_cursor_on_p = 0;
22818
22819 UNBLOCK_INPUT;
22820
22821 /* Advance the output cursor. */
22822 output_cursor.hpos += len;
22823 output_cursor.x = x;
22824 }
22825
22826
22827 /* EXPORT for RIF:
22828 Insert LEN glyphs from START at the nominal cursor position. */
22829
22830 void
22831 x_insert_glyphs (start, len)
22832 struct glyph *start;
22833 int len;
22834 {
22835 struct frame *f;
22836 struct window *w;
22837 int line_height, shift_by_width, shifted_region_width;
22838 struct glyph_row *row;
22839 struct glyph *glyph;
22840 int frame_x, frame_y;
22841 EMACS_INT hpos;
22842
22843 xassert (updated_window && updated_row);
22844 BLOCK_INPUT;
22845 w = updated_window;
22846 f = XFRAME (WINDOW_FRAME (w));
22847
22848 /* Get the height of the line we are in. */
22849 row = updated_row;
22850 line_height = row->height;
22851
22852 /* Get the width of the glyphs to insert. */
22853 shift_by_width = 0;
22854 for (glyph = start; glyph < start + len; ++glyph)
22855 shift_by_width += glyph->pixel_width;
22856
22857 /* Get the width of the region to shift right. */
22858 shifted_region_width = (window_box_width (w, updated_area)
22859 - output_cursor.x
22860 - shift_by_width);
22861
22862 /* Shift right. */
22863 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22864 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22865
22866 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22867 line_height, shift_by_width);
22868
22869 /* Write the glyphs. */
22870 hpos = start - row->glyphs[updated_area];
22871 draw_glyphs (w, output_cursor.x, row, updated_area,
22872 hpos, hpos + len,
22873 DRAW_NORMAL_TEXT, 0);
22874
22875 /* Advance the output cursor. */
22876 output_cursor.hpos += len;
22877 output_cursor.x += shift_by_width;
22878 UNBLOCK_INPUT;
22879 }
22880
22881
22882 /* EXPORT for RIF:
22883 Erase the current text line from the nominal cursor position
22884 (inclusive) to pixel column TO_X (exclusive). The idea is that
22885 everything from TO_X onward is already erased.
22886
22887 TO_X is a pixel position relative to updated_area of
22888 updated_window. TO_X == -1 means clear to the end of this area. */
22889
22890 void
22891 x_clear_end_of_line (to_x)
22892 int to_x;
22893 {
22894 struct frame *f;
22895 struct window *w = updated_window;
22896 int max_x, min_y, max_y;
22897 int from_x, from_y, to_y;
22898
22899 xassert (updated_window && updated_row);
22900 f = XFRAME (w->frame);
22901
22902 if (updated_row->full_width_p)
22903 max_x = WINDOW_TOTAL_WIDTH (w);
22904 else
22905 max_x = window_box_width (w, updated_area);
22906 max_y = window_text_bottom_y (w);
22907
22908 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22909 of window. For TO_X > 0, truncate to end of drawing area. */
22910 if (to_x == 0)
22911 return;
22912 else if (to_x < 0)
22913 to_x = max_x;
22914 else
22915 to_x = min (to_x, max_x);
22916
22917 to_y = min (max_y, output_cursor.y + updated_row->height);
22918
22919 /* Notice if the cursor will be cleared by this operation. */
22920 if (!updated_row->full_width_p)
22921 notice_overwritten_cursor (w, updated_area,
22922 output_cursor.x, -1,
22923 updated_row->y,
22924 MATRIX_ROW_BOTTOM_Y (updated_row));
22925
22926 from_x = output_cursor.x;
22927
22928 /* Translate to frame coordinates. */
22929 if (updated_row->full_width_p)
22930 {
22931 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22932 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22933 }
22934 else
22935 {
22936 int area_left = window_box_left (w, updated_area);
22937 from_x += area_left;
22938 to_x += area_left;
22939 }
22940
22941 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22942 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22943 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22944
22945 /* Prevent inadvertently clearing to end of the X window. */
22946 if (to_x > from_x && to_y > from_y)
22947 {
22948 BLOCK_INPUT;
22949 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22950 to_x - from_x, to_y - from_y);
22951 UNBLOCK_INPUT;
22952 }
22953 }
22954
22955 #endif /* HAVE_WINDOW_SYSTEM */
22956
22957
22958 \f
22959 /***********************************************************************
22960 Cursor types
22961 ***********************************************************************/
22962
22963 /* Value is the internal representation of the specified cursor type
22964 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22965 of the bar cursor. */
22966
22967 static enum text_cursor_kinds
22968 get_specified_cursor_type (arg, width)
22969 Lisp_Object arg;
22970 int *width;
22971 {
22972 enum text_cursor_kinds type;
22973
22974 if (NILP (arg))
22975 return NO_CURSOR;
22976
22977 if (EQ (arg, Qbox))
22978 return FILLED_BOX_CURSOR;
22979
22980 if (EQ (arg, Qhollow))
22981 return HOLLOW_BOX_CURSOR;
22982
22983 if (EQ (arg, Qbar))
22984 {
22985 *width = 2;
22986 return BAR_CURSOR;
22987 }
22988
22989 if (CONSP (arg)
22990 && EQ (XCAR (arg), Qbar)
22991 && INTEGERP (XCDR (arg))
22992 && XINT (XCDR (arg)) >= 0)
22993 {
22994 *width = XINT (XCDR (arg));
22995 return BAR_CURSOR;
22996 }
22997
22998 if (EQ (arg, Qhbar))
22999 {
23000 *width = 2;
23001 return HBAR_CURSOR;
23002 }
23003
23004 if (CONSP (arg)
23005 && EQ (XCAR (arg), Qhbar)
23006 && INTEGERP (XCDR (arg))
23007 && XINT (XCDR (arg)) >= 0)
23008 {
23009 *width = XINT (XCDR (arg));
23010 return HBAR_CURSOR;
23011 }
23012
23013 /* Treat anything unknown as "hollow box cursor".
23014 It was bad to signal an error; people have trouble fixing
23015 .Xdefaults with Emacs, when it has something bad in it. */
23016 type = HOLLOW_BOX_CURSOR;
23017
23018 return type;
23019 }
23020
23021 /* Set the default cursor types for specified frame. */
23022 void
23023 set_frame_cursor_types (f, arg)
23024 struct frame *f;
23025 Lisp_Object arg;
23026 {
23027 int width;
23028 Lisp_Object tem;
23029
23030 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23031 FRAME_CURSOR_WIDTH (f) = width;
23032
23033 /* By default, set up the blink-off state depending on the on-state. */
23034
23035 tem = Fassoc (arg, Vblink_cursor_alist);
23036 if (!NILP (tem))
23037 {
23038 FRAME_BLINK_OFF_CURSOR (f)
23039 = get_specified_cursor_type (XCDR (tem), &width);
23040 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23041 }
23042 else
23043 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23044 }
23045
23046
23047 /* Return the cursor we want to be displayed in window W. Return
23048 width of bar/hbar cursor through WIDTH arg. Return with
23049 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23050 (i.e. if the `system caret' should track this cursor).
23051
23052 In a mini-buffer window, we want the cursor only to appear if we
23053 are reading input from this window. For the selected window, we
23054 want the cursor type given by the frame parameter or buffer local
23055 setting of cursor-type. If explicitly marked off, draw no cursor.
23056 In all other cases, we want a hollow box cursor. */
23057
23058 static enum text_cursor_kinds
23059 get_window_cursor_type (w, glyph, width, active_cursor)
23060 struct window *w;
23061 struct glyph *glyph;
23062 int *width;
23063 int *active_cursor;
23064 {
23065 struct frame *f = XFRAME (w->frame);
23066 struct buffer *b = XBUFFER (w->buffer);
23067 int cursor_type = DEFAULT_CURSOR;
23068 Lisp_Object alt_cursor;
23069 int non_selected = 0;
23070
23071 *active_cursor = 1;
23072
23073 /* Echo area */
23074 if (cursor_in_echo_area
23075 && FRAME_HAS_MINIBUF_P (f)
23076 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23077 {
23078 if (w == XWINDOW (echo_area_window))
23079 {
23080 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23081 {
23082 *width = FRAME_CURSOR_WIDTH (f);
23083 return FRAME_DESIRED_CURSOR (f);
23084 }
23085 else
23086 return get_specified_cursor_type (b->cursor_type, width);
23087 }
23088
23089 *active_cursor = 0;
23090 non_selected = 1;
23091 }
23092
23093 /* Detect a nonselected window or nonselected frame. */
23094 else if (w != XWINDOW (f->selected_window)
23095 #ifdef HAVE_WINDOW_SYSTEM
23096 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
23097 #endif
23098 )
23099 {
23100 *active_cursor = 0;
23101
23102 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23103 return NO_CURSOR;
23104
23105 non_selected = 1;
23106 }
23107
23108 /* Never display a cursor in a window in which cursor-type is nil. */
23109 if (NILP (b->cursor_type))
23110 return NO_CURSOR;
23111
23112 /* Get the normal cursor type for this window. */
23113 if (EQ (b->cursor_type, Qt))
23114 {
23115 cursor_type = FRAME_DESIRED_CURSOR (f);
23116 *width = FRAME_CURSOR_WIDTH (f);
23117 }
23118 else
23119 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23120
23121 /* Use cursor-in-non-selected-windows instead
23122 for non-selected window or frame. */
23123 if (non_selected)
23124 {
23125 alt_cursor = b->cursor_in_non_selected_windows;
23126 if (!EQ (Qt, alt_cursor))
23127 return get_specified_cursor_type (alt_cursor, width);
23128 /* t means modify the normal cursor type. */
23129 if (cursor_type == FILLED_BOX_CURSOR)
23130 cursor_type = HOLLOW_BOX_CURSOR;
23131 else if (cursor_type == BAR_CURSOR && *width > 1)
23132 --*width;
23133 return cursor_type;
23134 }
23135
23136 /* Use normal cursor if not blinked off. */
23137 if (!w->cursor_off_p)
23138 {
23139 #ifdef HAVE_WINDOW_SYSTEM
23140 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23141 {
23142 if (cursor_type == FILLED_BOX_CURSOR)
23143 {
23144 /* Using a block cursor on large images can be very annoying.
23145 So use a hollow cursor for "large" images.
23146 If image is not transparent (no mask), also use hollow cursor. */
23147 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23148 if (img != NULL && IMAGEP (img->spec))
23149 {
23150 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23151 where N = size of default frame font size.
23152 This should cover most of the "tiny" icons people may use. */
23153 if (!img->mask
23154 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23155 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23156 cursor_type = HOLLOW_BOX_CURSOR;
23157 }
23158 }
23159 else if (cursor_type != NO_CURSOR)
23160 {
23161 /* Display current only supports BOX and HOLLOW cursors for images.
23162 So for now, unconditionally use a HOLLOW cursor when cursor is
23163 not a solid box cursor. */
23164 cursor_type = HOLLOW_BOX_CURSOR;
23165 }
23166 }
23167 #endif
23168 return cursor_type;
23169 }
23170
23171 /* Cursor is blinked off, so determine how to "toggle" it. */
23172
23173 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23174 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23175 return get_specified_cursor_type (XCDR (alt_cursor), width);
23176
23177 /* Then see if frame has specified a specific blink off cursor type. */
23178 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23179 {
23180 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23181 return FRAME_BLINK_OFF_CURSOR (f);
23182 }
23183
23184 #if 0
23185 /* Some people liked having a permanently visible blinking cursor,
23186 while others had very strong opinions against it. So it was
23187 decided to remove it. KFS 2003-09-03 */
23188
23189 /* Finally perform built-in cursor blinking:
23190 filled box <-> hollow box
23191 wide [h]bar <-> narrow [h]bar
23192 narrow [h]bar <-> no cursor
23193 other type <-> no cursor */
23194
23195 if (cursor_type == FILLED_BOX_CURSOR)
23196 return HOLLOW_BOX_CURSOR;
23197
23198 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23199 {
23200 *width = 1;
23201 return cursor_type;
23202 }
23203 #endif
23204
23205 return NO_CURSOR;
23206 }
23207
23208
23209 #ifdef HAVE_WINDOW_SYSTEM
23210
23211 /* Notice when the text cursor of window W has been completely
23212 overwritten by a drawing operation that outputs glyphs in AREA
23213 starting at X0 and ending at X1 in the line starting at Y0 and
23214 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23215 the rest of the line after X0 has been written. Y coordinates
23216 are window-relative. */
23217
23218 static void
23219 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
23220 struct window *w;
23221 enum glyph_row_area area;
23222 int x0, y0, x1, y1;
23223 {
23224 int cx0, cx1, cy0, cy1;
23225 struct glyph_row *row;
23226
23227 if (!w->phys_cursor_on_p)
23228 return;
23229 if (area != TEXT_AREA)
23230 return;
23231
23232 if (w->phys_cursor.vpos < 0
23233 || w->phys_cursor.vpos >= w->current_matrix->nrows
23234 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23235 !(row->enabled_p && row->displays_text_p)))
23236 return;
23237
23238 if (row->cursor_in_fringe_p)
23239 {
23240 row->cursor_in_fringe_p = 0;
23241 draw_fringe_bitmap (w, row, row->reversed_p);
23242 w->phys_cursor_on_p = 0;
23243 return;
23244 }
23245
23246 cx0 = w->phys_cursor.x;
23247 cx1 = cx0 + w->phys_cursor_width;
23248 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23249 return;
23250
23251 /* The cursor image will be completely removed from the
23252 screen if the output area intersects the cursor area in
23253 y-direction. When we draw in [y0 y1[, and some part of
23254 the cursor is at y < y0, that part must have been drawn
23255 before. When scrolling, the cursor is erased before
23256 actually scrolling, so we don't come here. When not
23257 scrolling, the rows above the old cursor row must have
23258 changed, and in this case these rows must have written
23259 over the cursor image.
23260
23261 Likewise if part of the cursor is below y1, with the
23262 exception of the cursor being in the first blank row at
23263 the buffer and window end because update_text_area
23264 doesn't draw that row. (Except when it does, but
23265 that's handled in update_text_area.) */
23266
23267 cy0 = w->phys_cursor.y;
23268 cy1 = cy0 + w->phys_cursor_height;
23269 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23270 return;
23271
23272 w->phys_cursor_on_p = 0;
23273 }
23274
23275 #endif /* HAVE_WINDOW_SYSTEM */
23276
23277 \f
23278 /************************************************************************
23279 Mouse Face
23280 ************************************************************************/
23281
23282 #ifdef HAVE_WINDOW_SYSTEM
23283
23284 /* EXPORT for RIF:
23285 Fix the display of area AREA of overlapping row ROW in window W
23286 with respect to the overlapping part OVERLAPS. */
23287
23288 void
23289 x_fix_overlapping_area (w, row, area, overlaps)
23290 struct window *w;
23291 struct glyph_row *row;
23292 enum glyph_row_area area;
23293 int overlaps;
23294 {
23295 int i, x;
23296
23297 BLOCK_INPUT;
23298
23299 x = 0;
23300 for (i = 0; i < row->used[area];)
23301 {
23302 if (row->glyphs[area][i].overlaps_vertically_p)
23303 {
23304 int start = i, start_x = x;
23305
23306 do
23307 {
23308 x += row->glyphs[area][i].pixel_width;
23309 ++i;
23310 }
23311 while (i < row->used[area]
23312 && row->glyphs[area][i].overlaps_vertically_p);
23313
23314 draw_glyphs (w, start_x, row, area,
23315 start, i,
23316 DRAW_NORMAL_TEXT, overlaps);
23317 }
23318 else
23319 {
23320 x += row->glyphs[area][i].pixel_width;
23321 ++i;
23322 }
23323 }
23324
23325 UNBLOCK_INPUT;
23326 }
23327
23328
23329 /* EXPORT:
23330 Draw the cursor glyph of window W in glyph row ROW. See the
23331 comment of draw_glyphs for the meaning of HL. */
23332
23333 void
23334 draw_phys_cursor_glyph (w, row, hl)
23335 struct window *w;
23336 struct glyph_row *row;
23337 enum draw_glyphs_face hl;
23338 {
23339 /* If cursor hpos is out of bounds, don't draw garbage. This can
23340 happen in mini-buffer windows when switching between echo area
23341 glyphs and mini-buffer. */
23342 if ((row->reversed_p
23343 ? (w->phys_cursor.hpos >= 0)
23344 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23345 {
23346 int on_p = w->phys_cursor_on_p;
23347 int x1;
23348 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23349 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23350 hl, 0);
23351 w->phys_cursor_on_p = on_p;
23352
23353 if (hl == DRAW_CURSOR)
23354 w->phys_cursor_width = x1 - w->phys_cursor.x;
23355 /* When we erase the cursor, and ROW is overlapped by other
23356 rows, make sure that these overlapping parts of other rows
23357 are redrawn. */
23358 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23359 {
23360 w->phys_cursor_width = x1 - w->phys_cursor.x;
23361
23362 if (row > w->current_matrix->rows
23363 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23364 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23365 OVERLAPS_ERASED_CURSOR);
23366
23367 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23368 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23369 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23370 OVERLAPS_ERASED_CURSOR);
23371 }
23372 }
23373 }
23374
23375
23376 /* EXPORT:
23377 Erase the image of a cursor of window W from the screen. */
23378
23379 void
23380 erase_phys_cursor (w)
23381 struct window *w;
23382 {
23383 struct frame *f = XFRAME (w->frame);
23384 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23385 int hpos = w->phys_cursor.hpos;
23386 int vpos = w->phys_cursor.vpos;
23387 int mouse_face_here_p = 0;
23388 struct glyph_matrix *active_glyphs = w->current_matrix;
23389 struct glyph_row *cursor_row;
23390 struct glyph *cursor_glyph;
23391 enum draw_glyphs_face hl;
23392
23393 /* No cursor displayed or row invalidated => nothing to do on the
23394 screen. */
23395 if (w->phys_cursor_type == NO_CURSOR)
23396 goto mark_cursor_off;
23397
23398 /* VPOS >= active_glyphs->nrows means that window has been resized.
23399 Don't bother to erase the cursor. */
23400 if (vpos >= active_glyphs->nrows)
23401 goto mark_cursor_off;
23402
23403 /* If row containing cursor is marked invalid, there is nothing we
23404 can do. */
23405 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23406 if (!cursor_row->enabled_p)
23407 goto mark_cursor_off;
23408
23409 /* If line spacing is > 0, old cursor may only be partially visible in
23410 window after split-window. So adjust visible height. */
23411 cursor_row->visible_height = min (cursor_row->visible_height,
23412 window_text_bottom_y (w) - cursor_row->y);
23413
23414 /* If row is completely invisible, don't attempt to delete a cursor which
23415 isn't there. This can happen if cursor is at top of a window, and
23416 we switch to a buffer with a header line in that window. */
23417 if (cursor_row->visible_height <= 0)
23418 goto mark_cursor_off;
23419
23420 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23421 if (cursor_row->cursor_in_fringe_p)
23422 {
23423 cursor_row->cursor_in_fringe_p = 0;
23424 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23425 goto mark_cursor_off;
23426 }
23427
23428 /* This can happen when the new row is shorter than the old one.
23429 In this case, either draw_glyphs or clear_end_of_line
23430 should have cleared the cursor. Note that we wouldn't be
23431 able to erase the cursor in this case because we don't have a
23432 cursor glyph at hand. */
23433 if ((cursor_row->reversed_p
23434 ? (w->phys_cursor.hpos < 0)
23435 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23436 goto mark_cursor_off;
23437
23438 /* If the cursor is in the mouse face area, redisplay that when
23439 we clear the cursor. */
23440 if (! NILP (dpyinfo->mouse_face_window)
23441 && w == XWINDOW (dpyinfo->mouse_face_window)
23442 && (vpos > dpyinfo->mouse_face_beg_row
23443 || (vpos == dpyinfo->mouse_face_beg_row
23444 && hpos >= dpyinfo->mouse_face_beg_col))
23445 && (vpos < dpyinfo->mouse_face_end_row
23446 || (vpos == dpyinfo->mouse_face_end_row
23447 && hpos < dpyinfo->mouse_face_end_col))
23448 /* Don't redraw the cursor's spot in mouse face if it is at the
23449 end of a line (on a newline). The cursor appears there, but
23450 mouse highlighting does not. */
23451 && cursor_row->used[TEXT_AREA] > hpos)
23452 mouse_face_here_p = 1;
23453
23454 /* Maybe clear the display under the cursor. */
23455 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23456 {
23457 int x, y, left_x;
23458 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23459 int width;
23460
23461 cursor_glyph = get_phys_cursor_glyph (w);
23462 if (cursor_glyph == NULL)
23463 goto mark_cursor_off;
23464
23465 width = cursor_glyph->pixel_width;
23466 left_x = window_box_left_offset (w, TEXT_AREA);
23467 x = w->phys_cursor.x;
23468 if (x < left_x)
23469 width -= left_x - x;
23470 width = min (width, window_box_width (w, TEXT_AREA) - x);
23471 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23472 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23473
23474 if (width > 0)
23475 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23476 }
23477
23478 /* Erase the cursor by redrawing the character underneath it. */
23479 if (mouse_face_here_p)
23480 hl = DRAW_MOUSE_FACE;
23481 else
23482 hl = DRAW_NORMAL_TEXT;
23483 draw_phys_cursor_glyph (w, cursor_row, hl);
23484
23485 mark_cursor_off:
23486 w->phys_cursor_on_p = 0;
23487 w->phys_cursor_type = NO_CURSOR;
23488 }
23489
23490
23491 /* EXPORT:
23492 Display or clear cursor of window W. If ON is zero, clear the
23493 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23494 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23495
23496 void
23497 display_and_set_cursor (w, on, hpos, vpos, x, y)
23498 struct window *w;
23499 int on, hpos, vpos, x, y;
23500 {
23501 struct frame *f = XFRAME (w->frame);
23502 int new_cursor_type;
23503 int new_cursor_width;
23504 int active_cursor;
23505 struct glyph_row *glyph_row;
23506 struct glyph *glyph;
23507
23508 /* This is pointless on invisible frames, and dangerous on garbaged
23509 windows and frames; in the latter case, the frame or window may
23510 be in the midst of changing its size, and x and y may be off the
23511 window. */
23512 if (! FRAME_VISIBLE_P (f)
23513 || FRAME_GARBAGED_P (f)
23514 || vpos >= w->current_matrix->nrows
23515 || hpos >= w->current_matrix->matrix_w)
23516 return;
23517
23518 /* If cursor is off and we want it off, return quickly. */
23519 if (!on && !w->phys_cursor_on_p)
23520 return;
23521
23522 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23523 /* If cursor row is not enabled, we don't really know where to
23524 display the cursor. */
23525 if (!glyph_row->enabled_p)
23526 {
23527 w->phys_cursor_on_p = 0;
23528 return;
23529 }
23530
23531 glyph = NULL;
23532 if (!glyph_row->exact_window_width_line_p
23533 || hpos < glyph_row->used[TEXT_AREA])
23534 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23535
23536 xassert (interrupt_input_blocked);
23537
23538 /* Set new_cursor_type to the cursor we want to be displayed. */
23539 new_cursor_type = get_window_cursor_type (w, glyph,
23540 &new_cursor_width, &active_cursor);
23541
23542 /* If cursor is currently being shown and we don't want it to be or
23543 it is in the wrong place, or the cursor type is not what we want,
23544 erase it. */
23545 if (w->phys_cursor_on_p
23546 && (!on
23547 || w->phys_cursor.x != x
23548 || w->phys_cursor.y != y
23549 || new_cursor_type != w->phys_cursor_type
23550 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23551 && new_cursor_width != w->phys_cursor_width)))
23552 erase_phys_cursor (w);
23553
23554 /* Don't check phys_cursor_on_p here because that flag is only set
23555 to zero in some cases where we know that the cursor has been
23556 completely erased, to avoid the extra work of erasing the cursor
23557 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23558 still not be visible, or it has only been partly erased. */
23559 if (on)
23560 {
23561 w->phys_cursor_ascent = glyph_row->ascent;
23562 w->phys_cursor_height = glyph_row->height;
23563
23564 /* Set phys_cursor_.* before x_draw_.* is called because some
23565 of them may need the information. */
23566 w->phys_cursor.x = x;
23567 w->phys_cursor.y = glyph_row->y;
23568 w->phys_cursor.hpos = hpos;
23569 w->phys_cursor.vpos = vpos;
23570 }
23571
23572 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23573 new_cursor_type, new_cursor_width,
23574 on, active_cursor);
23575 }
23576
23577
23578 /* Switch the display of W's cursor on or off, according to the value
23579 of ON. */
23580
23581 void
23582 update_window_cursor (w, on)
23583 struct window *w;
23584 int on;
23585 {
23586 /* Don't update cursor in windows whose frame is in the process
23587 of being deleted. */
23588 if (w->current_matrix)
23589 {
23590 BLOCK_INPUT;
23591 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23592 w->phys_cursor.x, w->phys_cursor.y);
23593 UNBLOCK_INPUT;
23594 }
23595 }
23596
23597
23598 /* Call update_window_cursor with parameter ON_P on all leaf windows
23599 in the window tree rooted at W. */
23600
23601 static void
23602 update_cursor_in_window_tree (w, on_p)
23603 struct window *w;
23604 int on_p;
23605 {
23606 while (w)
23607 {
23608 if (!NILP (w->hchild))
23609 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23610 else if (!NILP (w->vchild))
23611 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23612 else
23613 update_window_cursor (w, on_p);
23614
23615 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23616 }
23617 }
23618
23619
23620 /* EXPORT:
23621 Display the cursor on window W, or clear it, according to ON_P.
23622 Don't change the cursor's position. */
23623
23624 void
23625 x_update_cursor (f, on_p)
23626 struct frame *f;
23627 int on_p;
23628 {
23629 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23630 }
23631
23632
23633 /* EXPORT:
23634 Clear the cursor of window W to background color, and mark the
23635 cursor as not shown. This is used when the text where the cursor
23636 is about to be rewritten. */
23637
23638 void
23639 x_clear_cursor (w)
23640 struct window *w;
23641 {
23642 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23643 update_window_cursor (w, 0);
23644 }
23645
23646
23647 /* EXPORT:
23648 Display the active region described by mouse_face_* according to DRAW. */
23649
23650 void
23651 show_mouse_face (dpyinfo, draw)
23652 Display_Info *dpyinfo;
23653 enum draw_glyphs_face draw;
23654 {
23655 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23656 struct frame *f = XFRAME (WINDOW_FRAME (w));
23657
23658 if (/* If window is in the process of being destroyed, don't bother
23659 to do anything. */
23660 w->current_matrix != NULL
23661 /* Don't update mouse highlight if hidden */
23662 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23663 /* Recognize when we are called to operate on rows that don't exist
23664 anymore. This can happen when a window is split. */
23665 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23666 {
23667 int phys_cursor_on_p = w->phys_cursor_on_p;
23668 struct glyph_row *row, *first, *last;
23669
23670 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23671 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23672
23673 for (row = first; row <= last && row->enabled_p; ++row)
23674 {
23675 int start_hpos, end_hpos, start_x;
23676
23677 /* For all but the first row, the highlight starts at column 0. */
23678 if (row == first)
23679 {
23680 start_hpos = dpyinfo->mouse_face_beg_col;
23681 start_x = dpyinfo->mouse_face_beg_x;
23682 }
23683 else
23684 {
23685 start_hpos = 0;
23686 start_x = 0;
23687 }
23688
23689 if (row == last)
23690 end_hpos = dpyinfo->mouse_face_end_col;
23691 else
23692 {
23693 end_hpos = row->used[TEXT_AREA];
23694 if (draw == DRAW_NORMAL_TEXT)
23695 row->fill_line_p = 1; /* Clear to end of line */
23696 }
23697
23698 if (end_hpos > start_hpos)
23699 {
23700 draw_glyphs (w, start_x, row, TEXT_AREA,
23701 start_hpos, end_hpos,
23702 draw, 0);
23703
23704 row->mouse_face_p
23705 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23706 }
23707 }
23708
23709 /* When we've written over the cursor, arrange for it to
23710 be displayed again. */
23711 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23712 {
23713 BLOCK_INPUT;
23714 display_and_set_cursor (w, 1,
23715 w->phys_cursor.hpos, w->phys_cursor.vpos,
23716 w->phys_cursor.x, w->phys_cursor.y);
23717 UNBLOCK_INPUT;
23718 }
23719 }
23720
23721 /* Change the mouse cursor. */
23722 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23723 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23724 else if (draw == DRAW_MOUSE_FACE)
23725 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23726 else
23727 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23728 }
23729
23730 /* EXPORT:
23731 Clear out the mouse-highlighted active region.
23732 Redraw it un-highlighted first. Value is non-zero if mouse
23733 face was actually drawn unhighlighted. */
23734
23735 int
23736 clear_mouse_face (dpyinfo)
23737 Display_Info *dpyinfo;
23738 {
23739 int cleared = 0;
23740
23741 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23742 {
23743 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23744 cleared = 1;
23745 }
23746
23747 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23748 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23749 dpyinfo->mouse_face_window = Qnil;
23750 dpyinfo->mouse_face_overlay = Qnil;
23751 return cleared;
23752 }
23753
23754
23755 /* EXPORT:
23756 Non-zero if physical cursor of window W is within mouse face. */
23757
23758 int
23759 cursor_in_mouse_face_p (w)
23760 struct window *w;
23761 {
23762 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23763 int in_mouse_face = 0;
23764
23765 if (WINDOWP (dpyinfo->mouse_face_window)
23766 && XWINDOW (dpyinfo->mouse_face_window) == w)
23767 {
23768 int hpos = w->phys_cursor.hpos;
23769 int vpos = w->phys_cursor.vpos;
23770
23771 if (vpos >= dpyinfo->mouse_face_beg_row
23772 && vpos <= dpyinfo->mouse_face_end_row
23773 && (vpos > dpyinfo->mouse_face_beg_row
23774 || hpos >= dpyinfo->mouse_face_beg_col)
23775 && (vpos < dpyinfo->mouse_face_end_row
23776 || hpos < dpyinfo->mouse_face_end_col
23777 || dpyinfo->mouse_face_past_end))
23778 in_mouse_face = 1;
23779 }
23780
23781 return in_mouse_face;
23782 }
23783
23784
23785
23786 \f
23787 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23788 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23789 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23790 for the overlay or run of text properties specifying the mouse
23791 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23792 before-string and after-string that must also be highlighted.
23793 DISPLAY_STRING, if non-nil, is a display string that may cover some
23794 or all of the highlighted text. */
23795
23796 static void
23797 mouse_face_from_buffer_pos (Lisp_Object window,
23798 Display_Info *dpyinfo,
23799 EMACS_INT mouse_charpos,
23800 EMACS_INT start_charpos,
23801 EMACS_INT end_charpos,
23802 Lisp_Object before_string,
23803 Lisp_Object after_string,
23804 Lisp_Object display_string)
23805 {
23806 struct window *w = XWINDOW (window);
23807 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23808 struct glyph_row *row;
23809 struct glyph *glyph, *end;
23810 EMACS_INT ignore;
23811 int x;
23812
23813 xassert (NILP (display_string) || STRINGP (display_string));
23814 xassert (NILP (before_string) || STRINGP (before_string));
23815 xassert (NILP (after_string) || STRINGP (after_string));
23816
23817 /* Find the first highlighted glyph. */
23818 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
23819 {
23820 dpyinfo->mouse_face_beg_col = 0;
23821 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23822 dpyinfo->mouse_face_beg_x = first->x;
23823 dpyinfo->mouse_face_beg_y = first->y;
23824 }
23825 else
23826 {
23827 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23828 if (row == NULL)
23829 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23830
23831 /* If the before-string or display-string contains newlines,
23832 row_containing_pos skips to its last row. Move back. */
23833 if (!NILP (before_string) || !NILP (display_string))
23834 {
23835 struct glyph_row *prev;
23836 while ((prev = row - 1, prev >= first)
23837 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23838 && prev->used[TEXT_AREA] > 0)
23839 {
23840 struct glyph *beg = prev->glyphs[TEXT_AREA];
23841 glyph = beg + prev->used[TEXT_AREA];
23842 while (--glyph >= beg && INTEGERP (glyph->object));
23843 if (glyph < beg
23844 || !(EQ (glyph->object, before_string)
23845 || EQ (glyph->object, display_string)))
23846 break;
23847 row = prev;
23848 }
23849 }
23850
23851 glyph = row->glyphs[TEXT_AREA];
23852 end = glyph + row->used[TEXT_AREA];
23853 x = row->x;
23854 dpyinfo->mouse_face_beg_y = row->y;
23855 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23856
23857 /* Skip truncation glyphs at the start of the glyph row. */
23858 if (row->displays_text_p)
23859 for (; glyph < end
23860 && INTEGERP (glyph->object)
23861 && glyph->charpos < 0;
23862 ++glyph)
23863 x += glyph->pixel_width;
23864
23865 /* Scan the glyph row, stopping before BEFORE_STRING or
23866 DISPLAY_STRING or START_CHARPOS. */
23867 for (; glyph < end
23868 && !INTEGERP (glyph->object)
23869 && !EQ (glyph->object, before_string)
23870 && !EQ (glyph->object, display_string)
23871 && !(BUFFERP (glyph->object)
23872 && glyph->charpos >= start_charpos);
23873 ++glyph)
23874 x += glyph->pixel_width;
23875
23876 dpyinfo->mouse_face_beg_x = x;
23877 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23878 }
23879
23880 /* Find the last highlighted glyph. */
23881 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23882 if (row == NULL)
23883 {
23884 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23885 dpyinfo->mouse_face_past_end = 1;
23886 }
23887 else if (!NILP (after_string))
23888 {
23889 /* If the after-string has newlines, advance to its last row. */
23890 struct glyph_row *next;
23891 struct glyph_row *last
23892 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23893
23894 for (next = row + 1;
23895 next <= last
23896 && next->used[TEXT_AREA] > 0
23897 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23898 ++next)
23899 row = next;
23900 }
23901
23902 glyph = row->glyphs[TEXT_AREA];
23903 end = glyph + row->used[TEXT_AREA];
23904 x = row->x;
23905 dpyinfo->mouse_face_end_y = row->y;
23906 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23907
23908 /* Skip truncation glyphs at the start of the row. */
23909 if (row->displays_text_p)
23910 for (; glyph < end
23911 && INTEGERP (glyph->object)
23912 && glyph->charpos < 0;
23913 ++glyph)
23914 x += glyph->pixel_width;
23915
23916 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23917 AFTER_STRING. */
23918 for (; glyph < end
23919 && !INTEGERP (glyph->object)
23920 && !EQ (glyph->object, after_string)
23921 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23922 ++glyph)
23923 x += glyph->pixel_width;
23924
23925 /* If we found AFTER_STRING, consume it and stop. */
23926 if (EQ (glyph->object, after_string))
23927 {
23928 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23929 x += glyph->pixel_width;
23930 }
23931 else
23932 {
23933 /* If there's no after-string, we must check if we overshot,
23934 which might be the case if we stopped after a string glyph.
23935 That glyph may belong to a before-string or display-string
23936 associated with the end position, which must not be
23937 highlighted. */
23938 Lisp_Object prev_object;
23939 EMACS_INT pos;
23940
23941 while (glyph > row->glyphs[TEXT_AREA])
23942 {
23943 prev_object = (glyph - 1)->object;
23944 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23945 break;
23946
23947 pos = string_buffer_position (w, prev_object, end_charpos);
23948 if (pos && pos < end_charpos)
23949 break;
23950
23951 for (; glyph > row->glyphs[TEXT_AREA]
23952 && EQ ((glyph - 1)->object, prev_object);
23953 --glyph)
23954 x -= (glyph - 1)->pixel_width;
23955 }
23956 }
23957
23958 dpyinfo->mouse_face_end_x = x;
23959 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23960 dpyinfo->mouse_face_window = window;
23961 dpyinfo->mouse_face_face_id
23962 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23963 mouse_charpos + 1,
23964 !dpyinfo->mouse_face_hidden, -1);
23965 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23966 }
23967
23968
23969 /* Find the position of the glyph for position POS in OBJECT in
23970 window W's current matrix, and return in *X, *Y the pixel
23971 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23972
23973 RIGHT_P non-zero means return the position of the right edge of the
23974 glyph, RIGHT_P zero means return the left edge position.
23975
23976 If no glyph for POS exists in the matrix, return the position of
23977 the glyph with the next smaller position that is in the matrix, if
23978 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23979 exists in the matrix, return the position of the glyph with the
23980 next larger position in OBJECT.
23981
23982 Value is non-zero if a glyph was found. */
23983
23984 static int
23985 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23986 struct window *w;
23987 EMACS_INT pos;
23988 Lisp_Object object;
23989 int *hpos, *vpos, *x, *y;
23990 int right_p;
23991 {
23992 int yb = window_text_bottom_y (w);
23993 struct glyph_row *r;
23994 struct glyph *best_glyph = NULL;
23995 struct glyph_row *best_row = NULL;
23996 int best_x = 0;
23997
23998 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23999 r->enabled_p && r->y < yb;
24000 ++r)
24001 {
24002 struct glyph *g = r->glyphs[TEXT_AREA];
24003 struct glyph *e = g + r->used[TEXT_AREA];
24004 int gx;
24005
24006 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24007 if (EQ (g->object, object))
24008 {
24009 if (g->charpos == pos)
24010 {
24011 best_glyph = g;
24012 best_x = gx;
24013 best_row = r;
24014 goto found;
24015 }
24016 else if (best_glyph == NULL
24017 || ((eabs (g->charpos - pos)
24018 < eabs (best_glyph->charpos - pos))
24019 && (right_p
24020 ? g->charpos < pos
24021 : g->charpos > pos)))
24022 {
24023 best_glyph = g;
24024 best_x = gx;
24025 best_row = r;
24026 }
24027 }
24028 }
24029
24030 found:
24031
24032 if (best_glyph)
24033 {
24034 *x = best_x;
24035 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24036
24037 if (right_p)
24038 {
24039 *x += best_glyph->pixel_width;
24040 ++*hpos;
24041 }
24042
24043 *y = best_row->y;
24044 *vpos = best_row - w->current_matrix->rows;
24045 }
24046
24047 return best_glyph != NULL;
24048 }
24049
24050
24051 /* See if position X, Y is within a hot-spot of an image. */
24052
24053 static int
24054 on_hot_spot_p (hot_spot, x, y)
24055 Lisp_Object hot_spot;
24056 int x, y;
24057 {
24058 if (!CONSP (hot_spot))
24059 return 0;
24060
24061 if (EQ (XCAR (hot_spot), Qrect))
24062 {
24063 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24064 Lisp_Object rect = XCDR (hot_spot);
24065 Lisp_Object tem;
24066 if (!CONSP (rect))
24067 return 0;
24068 if (!CONSP (XCAR (rect)))
24069 return 0;
24070 if (!CONSP (XCDR (rect)))
24071 return 0;
24072 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24073 return 0;
24074 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24075 return 0;
24076 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24077 return 0;
24078 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24079 return 0;
24080 return 1;
24081 }
24082 else if (EQ (XCAR (hot_spot), Qcircle))
24083 {
24084 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24085 Lisp_Object circ = XCDR (hot_spot);
24086 Lisp_Object lr, lx0, ly0;
24087 if (CONSP (circ)
24088 && CONSP (XCAR (circ))
24089 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24090 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24091 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24092 {
24093 double r = XFLOATINT (lr);
24094 double dx = XINT (lx0) - x;
24095 double dy = XINT (ly0) - y;
24096 return (dx * dx + dy * dy <= r * r);
24097 }
24098 }
24099 else if (EQ (XCAR (hot_spot), Qpoly))
24100 {
24101 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24102 if (VECTORP (XCDR (hot_spot)))
24103 {
24104 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24105 Lisp_Object *poly = v->contents;
24106 int n = v->size;
24107 int i;
24108 int inside = 0;
24109 Lisp_Object lx, ly;
24110 int x0, y0;
24111
24112 /* Need an even number of coordinates, and at least 3 edges. */
24113 if (n < 6 || n & 1)
24114 return 0;
24115
24116 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24117 If count is odd, we are inside polygon. Pixels on edges
24118 may or may not be included depending on actual geometry of the
24119 polygon. */
24120 if ((lx = poly[n-2], !INTEGERP (lx))
24121 || (ly = poly[n-1], !INTEGERP (lx)))
24122 return 0;
24123 x0 = XINT (lx), y0 = XINT (ly);
24124 for (i = 0; i < n; i += 2)
24125 {
24126 int x1 = x0, y1 = y0;
24127 if ((lx = poly[i], !INTEGERP (lx))
24128 || (ly = poly[i+1], !INTEGERP (ly)))
24129 return 0;
24130 x0 = XINT (lx), y0 = XINT (ly);
24131
24132 /* Does this segment cross the X line? */
24133 if (x0 >= x)
24134 {
24135 if (x1 >= x)
24136 continue;
24137 }
24138 else if (x1 < x)
24139 continue;
24140 if (y > y0 && y > y1)
24141 continue;
24142 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24143 inside = !inside;
24144 }
24145 return inside;
24146 }
24147 }
24148 return 0;
24149 }
24150
24151 Lisp_Object
24152 find_hot_spot (map, x, y)
24153 Lisp_Object map;
24154 int x, y;
24155 {
24156 while (CONSP (map))
24157 {
24158 if (CONSP (XCAR (map))
24159 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24160 return XCAR (map);
24161 map = XCDR (map);
24162 }
24163
24164 return Qnil;
24165 }
24166
24167 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24168 3, 3, 0,
24169 doc: /* Lookup in image map MAP coordinates X and Y.
24170 An image map is an alist where each element has the format (AREA ID PLIST).
24171 An AREA is specified as either a rectangle, a circle, or a polygon:
24172 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24173 pixel coordinates of the upper left and bottom right corners.
24174 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24175 and the radius of the circle; r may be a float or integer.
24176 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24177 vector describes one corner in the polygon.
24178 Returns the alist element for the first matching AREA in MAP. */)
24179 (map, x, y)
24180 Lisp_Object map;
24181 Lisp_Object x, y;
24182 {
24183 if (NILP (map))
24184 return Qnil;
24185
24186 CHECK_NUMBER (x);
24187 CHECK_NUMBER (y);
24188
24189 return find_hot_spot (map, XINT (x), XINT (y));
24190 }
24191
24192
24193 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24194 static void
24195 define_frame_cursor1 (f, cursor, pointer)
24196 struct frame *f;
24197 Cursor cursor;
24198 Lisp_Object pointer;
24199 {
24200 /* Do not change cursor shape while dragging mouse. */
24201 if (!NILP (do_mouse_tracking))
24202 return;
24203
24204 if (!NILP (pointer))
24205 {
24206 if (EQ (pointer, Qarrow))
24207 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24208 else if (EQ (pointer, Qhand))
24209 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24210 else if (EQ (pointer, Qtext))
24211 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24212 else if (EQ (pointer, intern ("hdrag")))
24213 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24214 #ifdef HAVE_X_WINDOWS
24215 else if (EQ (pointer, intern ("vdrag")))
24216 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24217 #endif
24218 else if (EQ (pointer, intern ("hourglass")))
24219 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24220 else if (EQ (pointer, Qmodeline))
24221 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24222 else
24223 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24224 }
24225
24226 if (cursor != No_Cursor)
24227 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24228 }
24229
24230 /* Take proper action when mouse has moved to the mode or header line
24231 or marginal area AREA of window W, x-position X and y-position Y.
24232 X is relative to the start of the text display area of W, so the
24233 width of bitmap areas and scroll bars must be subtracted to get a
24234 position relative to the start of the mode line. */
24235
24236 static void
24237 note_mode_line_or_margin_highlight (window, x, y, area)
24238 Lisp_Object window;
24239 int x, y;
24240 enum window_part area;
24241 {
24242 struct window *w = XWINDOW (window);
24243 struct frame *f = XFRAME (w->frame);
24244 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24245 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24246 Lisp_Object pointer = Qnil;
24247 int charpos, dx, dy, width, height;
24248 Lisp_Object string, object = Qnil;
24249 Lisp_Object pos, help;
24250
24251 Lisp_Object mouse_face;
24252 int original_x_pixel = x;
24253 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24254 struct glyph_row *row;
24255
24256 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24257 {
24258 int x0;
24259 struct glyph *end;
24260
24261 string = mode_line_string (w, area, &x, &y, &charpos,
24262 &object, &dx, &dy, &width, &height);
24263
24264 row = (area == ON_MODE_LINE
24265 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24266 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24267
24268 /* Find glyph */
24269 if (row->mode_line_p && row->enabled_p)
24270 {
24271 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24272 end = glyph + row->used[TEXT_AREA];
24273
24274 for (x0 = original_x_pixel;
24275 glyph < end && x0 >= glyph->pixel_width;
24276 ++glyph)
24277 x0 -= glyph->pixel_width;
24278
24279 if (glyph >= end)
24280 glyph = NULL;
24281 }
24282 }
24283 else
24284 {
24285 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
24286 string = marginal_area_string (w, area, &x, &y, &charpos,
24287 &object, &dx, &dy, &width, &height);
24288 }
24289
24290 help = Qnil;
24291
24292 if (IMAGEP (object))
24293 {
24294 Lisp_Object image_map, hotspot;
24295 if ((image_map = Fplist_get (XCDR (object), QCmap),
24296 !NILP (image_map))
24297 && (hotspot = find_hot_spot (image_map, dx, dy),
24298 CONSP (hotspot))
24299 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24300 {
24301 Lisp_Object area_id, plist;
24302
24303 area_id = XCAR (hotspot);
24304 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24305 If so, we could look for mouse-enter, mouse-leave
24306 properties in PLIST (and do something...). */
24307 hotspot = XCDR (hotspot);
24308 if (CONSP (hotspot)
24309 && (plist = XCAR (hotspot), CONSP (plist)))
24310 {
24311 pointer = Fplist_get (plist, Qpointer);
24312 if (NILP (pointer))
24313 pointer = Qhand;
24314 help = Fplist_get (plist, Qhelp_echo);
24315 if (!NILP (help))
24316 {
24317 help_echo_string = help;
24318 /* Is this correct? ++kfs */
24319 XSETWINDOW (help_echo_window, w);
24320 help_echo_object = w->buffer;
24321 help_echo_pos = charpos;
24322 }
24323 }
24324 }
24325 if (NILP (pointer))
24326 pointer = Fplist_get (XCDR (object), QCpointer);
24327 }
24328
24329 if (STRINGP (string))
24330 {
24331 pos = make_number (charpos);
24332 /* If we're on a string with `help-echo' text property, arrange
24333 for the help to be displayed. This is done by setting the
24334 global variable help_echo_string to the help string. */
24335 if (NILP (help))
24336 {
24337 help = Fget_text_property (pos, Qhelp_echo, string);
24338 if (!NILP (help))
24339 {
24340 help_echo_string = help;
24341 XSETWINDOW (help_echo_window, w);
24342 help_echo_object = string;
24343 help_echo_pos = charpos;
24344 }
24345 }
24346
24347 if (NILP (pointer))
24348 pointer = Fget_text_property (pos, Qpointer, string);
24349
24350 /* Change the mouse pointer according to what is under X/Y. */
24351 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24352 {
24353 Lisp_Object map;
24354 map = Fget_text_property (pos, Qlocal_map, string);
24355 if (!KEYMAPP (map))
24356 map = Fget_text_property (pos, Qkeymap, string);
24357 if (!KEYMAPP (map))
24358 cursor = dpyinfo->vertical_scroll_bar_cursor;
24359 }
24360
24361 /* Change the mouse face according to what is under X/Y. */
24362 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24363 if (!NILP (mouse_face)
24364 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24365 && glyph)
24366 {
24367 Lisp_Object b, e;
24368
24369 struct glyph * tmp_glyph;
24370
24371 int gpos;
24372 int gseq_length;
24373 int total_pixel_width;
24374 EMACS_INT ignore;
24375
24376 int vpos, hpos;
24377
24378 b = Fprevious_single_property_change (make_number (charpos + 1),
24379 Qmouse_face, string, Qnil);
24380 if (NILP (b))
24381 b = make_number (0);
24382
24383 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24384 if (NILP (e))
24385 e = make_number (SCHARS (string));
24386
24387 /* Calculate the position(glyph position: GPOS) of GLYPH in
24388 displayed string. GPOS is different from CHARPOS.
24389
24390 CHARPOS is the position of glyph in internal string
24391 object. A mode line string format has structures which
24392 is converted to a flatten by emacs lisp interpreter.
24393 The internal string is an element of the structures.
24394 The displayed string is the flatten string. */
24395 gpos = 0;
24396 if (glyph > row_start_glyph)
24397 {
24398 tmp_glyph = glyph - 1;
24399 while (tmp_glyph >= row_start_glyph
24400 && tmp_glyph->charpos >= XINT (b)
24401 && EQ (tmp_glyph->object, glyph->object))
24402 {
24403 tmp_glyph--;
24404 gpos++;
24405 }
24406 }
24407
24408 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
24409 displayed string holding GLYPH.
24410
24411 GSEQ_LENGTH is different from SCHARS (STRING).
24412 SCHARS (STRING) returns the length of the internal string. */
24413 for (tmp_glyph = glyph, gseq_length = gpos;
24414 tmp_glyph->charpos < XINT (e);
24415 tmp_glyph++, gseq_length++)
24416 {
24417 if (!EQ (tmp_glyph->object, glyph->object))
24418 break;
24419 }
24420
24421 total_pixel_width = 0;
24422 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24423 total_pixel_width += tmp_glyph->pixel_width;
24424
24425 /* Pre calculation of re-rendering position */
24426 vpos = (x - gpos);
24427 hpos = (area == ON_MODE_LINE
24428 ? (w->current_matrix)->nrows - 1
24429 : 0);
24430
24431 /* If the re-rendering position is included in the last
24432 re-rendering area, we should do nothing. */
24433 if ( EQ (window, dpyinfo->mouse_face_window)
24434 && dpyinfo->mouse_face_beg_col <= vpos
24435 && vpos < dpyinfo->mouse_face_end_col
24436 && dpyinfo->mouse_face_beg_row == hpos )
24437 return;
24438
24439 if (clear_mouse_face (dpyinfo))
24440 cursor = No_Cursor;
24441
24442 dpyinfo->mouse_face_beg_col = vpos;
24443 dpyinfo->mouse_face_beg_row = hpos;
24444
24445 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
24446 dpyinfo->mouse_face_beg_y = 0;
24447
24448 dpyinfo->mouse_face_end_col = vpos + gseq_length;
24449 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24450
24451 dpyinfo->mouse_face_end_x = 0;
24452 dpyinfo->mouse_face_end_y = 0;
24453
24454 dpyinfo->mouse_face_past_end = 0;
24455 dpyinfo->mouse_face_window = window;
24456
24457 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24458 charpos,
24459 0, 0, 0, &ignore,
24460 glyph->face_id, 1);
24461 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24462
24463 if (NILP (pointer))
24464 pointer = Qhand;
24465 }
24466 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24467 clear_mouse_face (dpyinfo);
24468 }
24469 define_frame_cursor1 (f, cursor, pointer);
24470 }
24471
24472
24473 /* EXPORT:
24474 Take proper action when the mouse has moved to position X, Y on
24475 frame F as regards highlighting characters that have mouse-face
24476 properties. Also de-highlighting chars where the mouse was before.
24477 X and Y can be negative or out of range. */
24478
24479 void
24480 note_mouse_highlight (f, x, y)
24481 struct frame *f;
24482 int x, y;
24483 {
24484 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24485 enum window_part part;
24486 Lisp_Object window;
24487 struct window *w;
24488 Cursor cursor = No_Cursor;
24489 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24490 struct buffer *b;
24491
24492 /* When a menu is active, don't highlight because this looks odd. */
24493 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24494 if (popup_activated ())
24495 return;
24496 #endif
24497
24498 if (NILP (Vmouse_highlight)
24499 || !f->glyphs_initialized_p
24500 || f->pointer_invisible)
24501 return;
24502
24503 dpyinfo->mouse_face_mouse_x = x;
24504 dpyinfo->mouse_face_mouse_y = y;
24505 dpyinfo->mouse_face_mouse_frame = f;
24506
24507 if (dpyinfo->mouse_face_defer)
24508 return;
24509
24510 if (gc_in_progress)
24511 {
24512 dpyinfo->mouse_face_deferred_gc = 1;
24513 return;
24514 }
24515
24516 /* Which window is that in? */
24517 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24518
24519 /* If we were displaying active text in another window, clear that.
24520 Also clear if we move out of text area in same window. */
24521 if (! EQ (window, dpyinfo->mouse_face_window)
24522 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24523 && !NILP (dpyinfo->mouse_face_window)))
24524 clear_mouse_face (dpyinfo);
24525
24526 /* Not on a window -> return. */
24527 if (!WINDOWP (window))
24528 return;
24529
24530 /* Reset help_echo_string. It will get recomputed below. */
24531 help_echo_string = Qnil;
24532
24533 /* Convert to window-relative pixel coordinates. */
24534 w = XWINDOW (window);
24535 frame_to_window_pixel_xy (w, &x, &y);
24536
24537 /* Handle tool-bar window differently since it doesn't display a
24538 buffer. */
24539 if (EQ (window, f->tool_bar_window))
24540 {
24541 note_tool_bar_highlight (f, x, y);
24542 return;
24543 }
24544
24545 /* Mouse is on the mode, header line or margin? */
24546 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24547 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24548 {
24549 note_mode_line_or_margin_highlight (window, x, y, part);
24550 return;
24551 }
24552
24553 if (part == ON_VERTICAL_BORDER)
24554 {
24555 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24556 help_echo_string = build_string ("drag-mouse-1: resize");
24557 }
24558 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24559 || part == ON_SCROLL_BAR)
24560 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24561 else
24562 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24563
24564 /* Are we in a window whose display is up to date?
24565 And verify the buffer's text has not changed. */
24566 b = XBUFFER (w->buffer);
24567 if (part == ON_TEXT
24568 && EQ (w->window_end_valid, w->buffer)
24569 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24570 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24571 {
24572 int hpos, vpos, i, dx, dy, area;
24573 EMACS_INT pos;
24574 struct glyph *glyph;
24575 Lisp_Object object;
24576 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24577 Lisp_Object *overlay_vec = NULL;
24578 int noverlays;
24579 struct buffer *obuf;
24580 int obegv, ozv, same_region;
24581
24582 /* Find the glyph under X/Y. */
24583 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24584
24585 /* Look for :pointer property on image. */
24586 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24587 {
24588 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24589 if (img != NULL && IMAGEP (img->spec))
24590 {
24591 Lisp_Object image_map, hotspot;
24592 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24593 !NILP (image_map))
24594 && (hotspot = find_hot_spot (image_map,
24595 glyph->slice.x + dx,
24596 glyph->slice.y + dy),
24597 CONSP (hotspot))
24598 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24599 {
24600 Lisp_Object area_id, plist;
24601
24602 area_id = XCAR (hotspot);
24603 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24604 If so, we could look for mouse-enter, mouse-leave
24605 properties in PLIST (and do something...). */
24606 hotspot = XCDR (hotspot);
24607 if (CONSP (hotspot)
24608 && (plist = XCAR (hotspot), CONSP (plist)))
24609 {
24610 pointer = Fplist_get (plist, Qpointer);
24611 if (NILP (pointer))
24612 pointer = Qhand;
24613 help_echo_string = Fplist_get (plist, Qhelp_echo);
24614 if (!NILP (help_echo_string))
24615 {
24616 help_echo_window = window;
24617 help_echo_object = glyph->object;
24618 help_echo_pos = glyph->charpos;
24619 }
24620 }
24621 }
24622 if (NILP (pointer))
24623 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24624 }
24625 }
24626
24627 /* Clear mouse face if X/Y not over text. */
24628 if (glyph == NULL
24629 || area != TEXT_AREA
24630 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24631 {
24632 if (clear_mouse_face (dpyinfo))
24633 cursor = No_Cursor;
24634 if (NILP (pointer))
24635 {
24636 if (area != TEXT_AREA)
24637 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24638 else
24639 pointer = Vvoid_text_area_pointer;
24640 }
24641 goto set_cursor;
24642 }
24643
24644 pos = glyph->charpos;
24645 object = glyph->object;
24646 if (!STRINGP (object) && !BUFFERP (object))
24647 goto set_cursor;
24648
24649 /* If we get an out-of-range value, return now; avoid an error. */
24650 if (BUFFERP (object) && pos > BUF_Z (b))
24651 goto set_cursor;
24652
24653 /* Make the window's buffer temporarily current for
24654 overlays_at and compute_char_face. */
24655 obuf = current_buffer;
24656 current_buffer = b;
24657 obegv = BEGV;
24658 ozv = ZV;
24659 BEGV = BEG;
24660 ZV = Z;
24661
24662 /* Is this char mouse-active or does it have help-echo? */
24663 position = make_number (pos);
24664
24665 if (BUFFERP (object))
24666 {
24667 /* Put all the overlays we want in a vector in overlay_vec. */
24668 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24669 /* Sort overlays into increasing priority order. */
24670 noverlays = sort_overlays (overlay_vec, noverlays, w);
24671 }
24672 else
24673 noverlays = 0;
24674
24675 same_region = (EQ (window, dpyinfo->mouse_face_window)
24676 && vpos >= dpyinfo->mouse_face_beg_row
24677 && vpos <= dpyinfo->mouse_face_end_row
24678 && (vpos > dpyinfo->mouse_face_beg_row
24679 || hpos >= dpyinfo->mouse_face_beg_col)
24680 && (vpos < dpyinfo->mouse_face_end_row
24681 || hpos < dpyinfo->mouse_face_end_col
24682 || dpyinfo->mouse_face_past_end));
24683
24684 if (same_region)
24685 cursor = No_Cursor;
24686
24687 /* Check mouse-face highlighting. */
24688 if (! same_region
24689 /* If there exists an overlay with mouse-face overlapping
24690 the one we are currently highlighting, we have to
24691 check if we enter the overlapping overlay, and then
24692 highlight only that. */
24693 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24694 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24695 {
24696 /* Find the highest priority overlay with a mouse-face. */
24697 overlay = Qnil;
24698 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24699 {
24700 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24701 if (!NILP (mouse_face))
24702 overlay = overlay_vec[i];
24703 }
24704
24705 /* If we're highlighting the same overlay as before, there's
24706 no need to do that again. */
24707 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24708 goto check_help_echo;
24709 dpyinfo->mouse_face_overlay = overlay;
24710
24711 /* Clear the display of the old active region, if any. */
24712 if (clear_mouse_face (dpyinfo))
24713 cursor = No_Cursor;
24714
24715 /* If no overlay applies, get a text property. */
24716 if (NILP (overlay))
24717 mouse_face = Fget_text_property (position, Qmouse_face, object);
24718
24719 /* Next, compute the bounds of the mouse highlighting and
24720 display it. */
24721 if (!NILP (mouse_face) && STRINGP (object))
24722 {
24723 /* The mouse-highlighting comes from a display string
24724 with a mouse-face. */
24725 Lisp_Object b, e;
24726 EMACS_INT ignore;
24727
24728 b = Fprevious_single_property_change
24729 (make_number (pos + 1), Qmouse_face, object, Qnil);
24730 e = Fnext_single_property_change
24731 (position, Qmouse_face, object, Qnil);
24732 if (NILP (b))
24733 b = make_number (0);
24734 if (NILP (e))
24735 e = make_number (SCHARS (object) - 1);
24736
24737 fast_find_string_pos (w, XINT (b), object,
24738 &dpyinfo->mouse_face_beg_col,
24739 &dpyinfo->mouse_face_beg_row,
24740 &dpyinfo->mouse_face_beg_x,
24741 &dpyinfo->mouse_face_beg_y, 0);
24742 fast_find_string_pos (w, XINT (e), object,
24743 &dpyinfo->mouse_face_end_col,
24744 &dpyinfo->mouse_face_end_row,
24745 &dpyinfo->mouse_face_end_x,
24746 &dpyinfo->mouse_face_end_y, 1);
24747 dpyinfo->mouse_face_past_end = 0;
24748 dpyinfo->mouse_face_window = window;
24749 dpyinfo->mouse_face_face_id
24750 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24751 glyph->face_id, 1);
24752 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24753 cursor = No_Cursor;
24754 }
24755 else
24756 {
24757 /* The mouse-highlighting, if any, comes from an overlay
24758 or text property in the buffer. */
24759 Lisp_Object buffer, display_string;
24760
24761 if (STRINGP (object))
24762 {
24763 /* If we are on a display string with no mouse-face,
24764 check if the text under it has one. */
24765 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24766 int start = MATRIX_ROW_START_CHARPOS (r);
24767 pos = string_buffer_position (w, object, start);
24768 if (pos > 0)
24769 {
24770 mouse_face = get_char_property_and_overlay
24771 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24772 buffer = w->buffer;
24773 display_string = object;
24774 }
24775 }
24776 else
24777 {
24778 buffer = object;
24779 display_string = Qnil;
24780 }
24781
24782 if (!NILP (mouse_face))
24783 {
24784 Lisp_Object before, after;
24785 Lisp_Object before_string, after_string;
24786
24787 if (NILP (overlay))
24788 {
24789 /* Handle the text property case. */
24790 before = Fprevious_single_property_change
24791 (make_number (pos + 1), Qmouse_face, buffer,
24792 Fmarker_position (w->start));
24793 after = Fnext_single_property_change
24794 (make_number (pos), Qmouse_face, buffer,
24795 make_number (BUF_Z (XBUFFER (buffer))
24796 - XFASTINT (w->window_end_pos)));
24797 before_string = after_string = Qnil;
24798 }
24799 else
24800 {
24801 /* Handle the overlay case. */
24802 before = Foverlay_start (overlay);
24803 after = Foverlay_end (overlay);
24804 before_string = Foverlay_get (overlay, Qbefore_string);
24805 after_string = Foverlay_get (overlay, Qafter_string);
24806
24807 if (!STRINGP (before_string)) before_string = Qnil;
24808 if (!STRINGP (after_string)) after_string = Qnil;
24809 }
24810
24811 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24812 XFASTINT (before),
24813 XFASTINT (after),
24814 before_string, after_string,
24815 display_string);
24816 cursor = No_Cursor;
24817 }
24818 }
24819 }
24820
24821 check_help_echo:
24822
24823 /* Look for a `help-echo' property. */
24824 if (NILP (help_echo_string)) {
24825 Lisp_Object help, overlay;
24826
24827 /* Check overlays first. */
24828 help = overlay = Qnil;
24829 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24830 {
24831 overlay = overlay_vec[i];
24832 help = Foverlay_get (overlay, Qhelp_echo);
24833 }
24834
24835 if (!NILP (help))
24836 {
24837 help_echo_string = help;
24838 help_echo_window = window;
24839 help_echo_object = overlay;
24840 help_echo_pos = pos;
24841 }
24842 else
24843 {
24844 Lisp_Object object = glyph->object;
24845 int charpos = glyph->charpos;
24846
24847 /* Try text properties. */
24848 if (STRINGP (object)
24849 && charpos >= 0
24850 && charpos < SCHARS (object))
24851 {
24852 help = Fget_text_property (make_number (charpos),
24853 Qhelp_echo, object);
24854 if (NILP (help))
24855 {
24856 /* If the string itself doesn't specify a help-echo,
24857 see if the buffer text ``under'' it does. */
24858 struct glyph_row *r
24859 = MATRIX_ROW (w->current_matrix, vpos);
24860 int start = MATRIX_ROW_START_CHARPOS (r);
24861 EMACS_INT pos = string_buffer_position (w, object, start);
24862 if (pos > 0)
24863 {
24864 help = Fget_char_property (make_number (pos),
24865 Qhelp_echo, w->buffer);
24866 if (!NILP (help))
24867 {
24868 charpos = pos;
24869 object = w->buffer;
24870 }
24871 }
24872 }
24873 }
24874 else if (BUFFERP (object)
24875 && charpos >= BEGV
24876 && charpos < ZV)
24877 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24878 object);
24879
24880 if (!NILP (help))
24881 {
24882 help_echo_string = help;
24883 help_echo_window = window;
24884 help_echo_object = object;
24885 help_echo_pos = charpos;
24886 }
24887 }
24888 }
24889
24890 /* Look for a `pointer' property. */
24891 if (NILP (pointer))
24892 {
24893 /* Check overlays first. */
24894 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24895 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24896
24897 if (NILP (pointer))
24898 {
24899 Lisp_Object object = glyph->object;
24900 int charpos = glyph->charpos;
24901
24902 /* Try text properties. */
24903 if (STRINGP (object)
24904 && charpos >= 0
24905 && charpos < SCHARS (object))
24906 {
24907 pointer = Fget_text_property (make_number (charpos),
24908 Qpointer, object);
24909 if (NILP (pointer))
24910 {
24911 /* If the string itself doesn't specify a pointer,
24912 see if the buffer text ``under'' it does. */
24913 struct glyph_row *r
24914 = MATRIX_ROW (w->current_matrix, vpos);
24915 int start = MATRIX_ROW_START_CHARPOS (r);
24916 EMACS_INT pos = string_buffer_position (w, object,
24917 start);
24918 if (pos > 0)
24919 pointer = Fget_char_property (make_number (pos),
24920 Qpointer, w->buffer);
24921 }
24922 }
24923 else if (BUFFERP (object)
24924 && charpos >= BEGV
24925 && charpos < ZV)
24926 pointer = Fget_text_property (make_number (charpos),
24927 Qpointer, object);
24928 }
24929 }
24930
24931 BEGV = obegv;
24932 ZV = ozv;
24933 current_buffer = obuf;
24934 }
24935
24936 set_cursor:
24937
24938 define_frame_cursor1 (f, cursor, pointer);
24939 }
24940
24941
24942 /* EXPORT for RIF:
24943 Clear any mouse-face on window W. This function is part of the
24944 redisplay interface, and is called from try_window_id and similar
24945 functions to ensure the mouse-highlight is off. */
24946
24947 void
24948 x_clear_window_mouse_face (w)
24949 struct window *w;
24950 {
24951 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24952 Lisp_Object window;
24953
24954 BLOCK_INPUT;
24955 XSETWINDOW (window, w);
24956 if (EQ (window, dpyinfo->mouse_face_window))
24957 clear_mouse_face (dpyinfo);
24958 UNBLOCK_INPUT;
24959 }
24960
24961
24962 /* EXPORT:
24963 Just discard the mouse face information for frame F, if any.
24964 This is used when the size of F is changed. */
24965
24966 void
24967 cancel_mouse_face (f)
24968 struct frame *f;
24969 {
24970 Lisp_Object window;
24971 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24972
24973 window = dpyinfo->mouse_face_window;
24974 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24975 {
24976 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24977 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24978 dpyinfo->mouse_face_window = Qnil;
24979 }
24980 }
24981
24982
24983 #endif /* HAVE_WINDOW_SYSTEM */
24984
24985 \f
24986 /***********************************************************************
24987 Exposure Events
24988 ***********************************************************************/
24989
24990 #ifdef HAVE_WINDOW_SYSTEM
24991
24992 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24993 which intersects rectangle R. R is in window-relative coordinates. */
24994
24995 static void
24996 expose_area (w, row, r, area)
24997 struct window *w;
24998 struct glyph_row *row;
24999 XRectangle *r;
25000 enum glyph_row_area area;
25001 {
25002 struct glyph *first = row->glyphs[area];
25003 struct glyph *end = row->glyphs[area] + row->used[area];
25004 struct glyph *last;
25005 int first_x, start_x, x;
25006
25007 if (area == TEXT_AREA && row->fill_line_p)
25008 /* If row extends face to end of line write the whole line. */
25009 draw_glyphs (w, 0, row, area,
25010 0, row->used[area],
25011 DRAW_NORMAL_TEXT, 0);
25012 else
25013 {
25014 /* Set START_X to the window-relative start position for drawing glyphs of
25015 AREA. The first glyph of the text area can be partially visible.
25016 The first glyphs of other areas cannot. */
25017 start_x = window_box_left_offset (w, area);
25018 x = start_x;
25019 if (area == TEXT_AREA)
25020 x += row->x;
25021
25022 /* Find the first glyph that must be redrawn. */
25023 while (first < end
25024 && x + first->pixel_width < r->x)
25025 {
25026 x += first->pixel_width;
25027 ++first;
25028 }
25029
25030 /* Find the last one. */
25031 last = first;
25032 first_x = x;
25033 while (last < end
25034 && x < r->x + r->width)
25035 {
25036 x += last->pixel_width;
25037 ++last;
25038 }
25039
25040 /* Repaint. */
25041 if (last > first)
25042 draw_glyphs (w, first_x - start_x, row, area,
25043 first - row->glyphs[area], last - row->glyphs[area],
25044 DRAW_NORMAL_TEXT, 0);
25045 }
25046 }
25047
25048
25049 /* Redraw the parts of the glyph row ROW on window W intersecting
25050 rectangle R. R is in window-relative coordinates. Value is
25051 non-zero if mouse-face was overwritten. */
25052
25053 static int
25054 expose_line (w, row, r)
25055 struct window *w;
25056 struct glyph_row *row;
25057 XRectangle *r;
25058 {
25059 xassert (row->enabled_p);
25060
25061 if (row->mode_line_p || w->pseudo_window_p)
25062 draw_glyphs (w, 0, row, TEXT_AREA,
25063 0, row->used[TEXT_AREA],
25064 DRAW_NORMAL_TEXT, 0);
25065 else
25066 {
25067 if (row->used[LEFT_MARGIN_AREA])
25068 expose_area (w, row, r, LEFT_MARGIN_AREA);
25069 if (row->used[TEXT_AREA])
25070 expose_area (w, row, r, TEXT_AREA);
25071 if (row->used[RIGHT_MARGIN_AREA])
25072 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25073 draw_row_fringe_bitmaps (w, row);
25074 }
25075
25076 return row->mouse_face_p;
25077 }
25078
25079
25080 /* Redraw those parts of glyphs rows during expose event handling that
25081 overlap other rows. Redrawing of an exposed line writes over parts
25082 of lines overlapping that exposed line; this function fixes that.
25083
25084 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25085 row in W's current matrix that is exposed and overlaps other rows.
25086 LAST_OVERLAPPING_ROW is the last such row. */
25087
25088 static void
25089 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
25090 struct window *w;
25091 struct glyph_row *first_overlapping_row;
25092 struct glyph_row *last_overlapping_row;
25093 XRectangle *r;
25094 {
25095 struct glyph_row *row;
25096
25097 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25098 if (row->overlapping_p)
25099 {
25100 xassert (row->enabled_p && !row->mode_line_p);
25101
25102 row->clip = r;
25103 if (row->used[LEFT_MARGIN_AREA])
25104 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25105
25106 if (row->used[TEXT_AREA])
25107 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25108
25109 if (row->used[RIGHT_MARGIN_AREA])
25110 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25111 row->clip = NULL;
25112 }
25113 }
25114
25115
25116 /* Return non-zero if W's cursor intersects rectangle R. */
25117
25118 static int
25119 phys_cursor_in_rect_p (w, r)
25120 struct window *w;
25121 XRectangle *r;
25122 {
25123 XRectangle cr, result;
25124 struct glyph *cursor_glyph;
25125 struct glyph_row *row;
25126
25127 if (w->phys_cursor.vpos >= 0
25128 && w->phys_cursor.vpos < w->current_matrix->nrows
25129 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25130 row->enabled_p)
25131 && row->cursor_in_fringe_p)
25132 {
25133 /* Cursor is in the fringe. */
25134 cr.x = window_box_right_offset (w,
25135 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25136 ? RIGHT_MARGIN_AREA
25137 : TEXT_AREA));
25138 cr.y = row->y;
25139 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25140 cr.height = row->height;
25141 return x_intersect_rectangles (&cr, r, &result);
25142 }
25143
25144 cursor_glyph = get_phys_cursor_glyph (w);
25145 if (cursor_glyph)
25146 {
25147 /* r is relative to W's box, but w->phys_cursor.x is relative
25148 to left edge of W's TEXT area. Adjust it. */
25149 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25150 cr.y = w->phys_cursor.y;
25151 cr.width = cursor_glyph->pixel_width;
25152 cr.height = w->phys_cursor_height;
25153 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25154 I assume the effect is the same -- and this is portable. */
25155 return x_intersect_rectangles (&cr, r, &result);
25156 }
25157 /* If we don't understand the format, pretend we're not in the hot-spot. */
25158 return 0;
25159 }
25160
25161
25162 /* EXPORT:
25163 Draw a vertical window border to the right of window W if W doesn't
25164 have vertical scroll bars. */
25165
25166 void
25167 x_draw_vertical_border (w)
25168 struct window *w;
25169 {
25170 struct frame *f = XFRAME (WINDOW_FRAME (w));
25171
25172 /* We could do better, if we knew what type of scroll-bar the adjacent
25173 windows (on either side) have... But we don't :-(
25174 However, I think this works ok. ++KFS 2003-04-25 */
25175
25176 /* Redraw borders between horizontally adjacent windows. Don't
25177 do it for frames with vertical scroll bars because either the
25178 right scroll bar of a window, or the left scroll bar of its
25179 neighbor will suffice as a border. */
25180 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25181 return;
25182
25183 if (!WINDOW_RIGHTMOST_P (w)
25184 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25185 {
25186 int x0, x1, y0, y1;
25187
25188 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25189 y1 -= 1;
25190
25191 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25192 x1 -= 1;
25193
25194 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25195 }
25196 else if (!WINDOW_LEFTMOST_P (w)
25197 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25198 {
25199 int x0, x1, y0, y1;
25200
25201 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25202 y1 -= 1;
25203
25204 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25205 x0 -= 1;
25206
25207 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25208 }
25209 }
25210
25211
25212 /* Redraw the part of window W intersection rectangle FR. Pixel
25213 coordinates in FR are frame-relative. Call this function with
25214 input blocked. Value is non-zero if the exposure overwrites
25215 mouse-face. */
25216
25217 static int
25218 expose_window (w, fr)
25219 struct window *w;
25220 XRectangle *fr;
25221 {
25222 struct frame *f = XFRAME (w->frame);
25223 XRectangle wr, r;
25224 int mouse_face_overwritten_p = 0;
25225
25226 /* If window is not yet fully initialized, do nothing. This can
25227 happen when toolkit scroll bars are used and a window is split.
25228 Reconfiguring the scroll bar will generate an expose for a newly
25229 created window. */
25230 if (w->current_matrix == NULL)
25231 return 0;
25232
25233 /* When we're currently updating the window, display and current
25234 matrix usually don't agree. Arrange for a thorough display
25235 later. */
25236 if (w == updated_window)
25237 {
25238 SET_FRAME_GARBAGED (f);
25239 return 0;
25240 }
25241
25242 /* Frame-relative pixel rectangle of W. */
25243 wr.x = WINDOW_LEFT_EDGE_X (w);
25244 wr.y = WINDOW_TOP_EDGE_Y (w);
25245 wr.width = WINDOW_TOTAL_WIDTH (w);
25246 wr.height = WINDOW_TOTAL_HEIGHT (w);
25247
25248 if (x_intersect_rectangles (fr, &wr, &r))
25249 {
25250 int yb = window_text_bottom_y (w);
25251 struct glyph_row *row;
25252 int cursor_cleared_p;
25253 struct glyph_row *first_overlapping_row, *last_overlapping_row;
25254
25255 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
25256 r.x, r.y, r.width, r.height));
25257
25258 /* Convert to window coordinates. */
25259 r.x -= WINDOW_LEFT_EDGE_X (w);
25260 r.y -= WINDOW_TOP_EDGE_Y (w);
25261
25262 /* Turn off the cursor. */
25263 if (!w->pseudo_window_p
25264 && phys_cursor_in_rect_p (w, &r))
25265 {
25266 x_clear_cursor (w);
25267 cursor_cleared_p = 1;
25268 }
25269 else
25270 cursor_cleared_p = 0;
25271
25272 /* Update lines intersecting rectangle R. */
25273 first_overlapping_row = last_overlapping_row = NULL;
25274 for (row = w->current_matrix->rows;
25275 row->enabled_p;
25276 ++row)
25277 {
25278 int y0 = row->y;
25279 int y1 = MATRIX_ROW_BOTTOM_Y (row);
25280
25281 if ((y0 >= r.y && y0 < r.y + r.height)
25282 || (y1 > r.y && y1 < r.y + r.height)
25283 || (r.y >= y0 && r.y < y1)
25284 || (r.y + r.height > y0 && r.y + r.height < y1))
25285 {
25286 /* A header line may be overlapping, but there is no need
25287 to fix overlapping areas for them. KFS 2005-02-12 */
25288 if (row->overlapping_p && !row->mode_line_p)
25289 {
25290 if (first_overlapping_row == NULL)
25291 first_overlapping_row = row;
25292 last_overlapping_row = row;
25293 }
25294
25295 row->clip = fr;
25296 if (expose_line (w, row, &r))
25297 mouse_face_overwritten_p = 1;
25298 row->clip = NULL;
25299 }
25300 else if (row->overlapping_p)
25301 {
25302 /* We must redraw a row overlapping the exposed area. */
25303 if (y0 < r.y
25304 ? y0 + row->phys_height > r.y
25305 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
25306 {
25307 if (first_overlapping_row == NULL)
25308 first_overlapping_row = row;
25309 last_overlapping_row = row;
25310 }
25311 }
25312
25313 if (y1 >= yb)
25314 break;
25315 }
25316
25317 /* Display the mode line if there is one. */
25318 if (WINDOW_WANTS_MODELINE_P (w)
25319 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
25320 row->enabled_p)
25321 && row->y < r.y + r.height)
25322 {
25323 if (expose_line (w, row, &r))
25324 mouse_face_overwritten_p = 1;
25325 }
25326
25327 if (!w->pseudo_window_p)
25328 {
25329 /* Fix the display of overlapping rows. */
25330 if (first_overlapping_row)
25331 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
25332 fr);
25333
25334 /* Draw border between windows. */
25335 x_draw_vertical_border (w);
25336
25337 /* Turn the cursor on again. */
25338 if (cursor_cleared_p)
25339 update_window_cursor (w, 1);
25340 }
25341 }
25342
25343 return mouse_face_overwritten_p;
25344 }
25345
25346
25347
25348 /* Redraw (parts) of all windows in the window tree rooted at W that
25349 intersect R. R contains frame pixel coordinates. Value is
25350 non-zero if the exposure overwrites mouse-face. */
25351
25352 static int
25353 expose_window_tree (w, r)
25354 struct window *w;
25355 XRectangle *r;
25356 {
25357 struct frame *f = XFRAME (w->frame);
25358 int mouse_face_overwritten_p = 0;
25359
25360 while (w && !FRAME_GARBAGED_P (f))
25361 {
25362 if (!NILP (w->hchild))
25363 mouse_face_overwritten_p
25364 |= expose_window_tree (XWINDOW (w->hchild), r);
25365 else if (!NILP (w->vchild))
25366 mouse_face_overwritten_p
25367 |= expose_window_tree (XWINDOW (w->vchild), r);
25368 else
25369 mouse_face_overwritten_p |= expose_window (w, r);
25370
25371 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25372 }
25373
25374 return mouse_face_overwritten_p;
25375 }
25376
25377
25378 /* EXPORT:
25379 Redisplay an exposed area of frame F. X and Y are the upper-left
25380 corner of the exposed rectangle. W and H are width and height of
25381 the exposed area. All are pixel values. W or H zero means redraw
25382 the entire frame. */
25383
25384 void
25385 expose_frame (f, x, y, w, h)
25386 struct frame *f;
25387 int x, y, w, h;
25388 {
25389 XRectangle r;
25390 int mouse_face_overwritten_p = 0;
25391
25392 TRACE ((stderr, "expose_frame "));
25393
25394 /* No need to redraw if frame will be redrawn soon. */
25395 if (FRAME_GARBAGED_P (f))
25396 {
25397 TRACE ((stderr, " garbaged\n"));
25398 return;
25399 }
25400
25401 /* If basic faces haven't been realized yet, there is no point in
25402 trying to redraw anything. This can happen when we get an expose
25403 event while Emacs is starting, e.g. by moving another window. */
25404 if (FRAME_FACE_CACHE (f) == NULL
25405 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25406 {
25407 TRACE ((stderr, " no faces\n"));
25408 return;
25409 }
25410
25411 if (w == 0 || h == 0)
25412 {
25413 r.x = r.y = 0;
25414 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25415 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25416 }
25417 else
25418 {
25419 r.x = x;
25420 r.y = y;
25421 r.width = w;
25422 r.height = h;
25423 }
25424
25425 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25426 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25427
25428 if (WINDOWP (f->tool_bar_window))
25429 mouse_face_overwritten_p
25430 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25431
25432 #ifdef HAVE_X_WINDOWS
25433 #ifndef MSDOS
25434 #ifndef USE_X_TOOLKIT
25435 if (WINDOWP (f->menu_bar_window))
25436 mouse_face_overwritten_p
25437 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25438 #endif /* not USE_X_TOOLKIT */
25439 #endif
25440 #endif
25441
25442 /* Some window managers support a focus-follows-mouse style with
25443 delayed raising of frames. Imagine a partially obscured frame,
25444 and moving the mouse into partially obscured mouse-face on that
25445 frame. The visible part of the mouse-face will be highlighted,
25446 then the WM raises the obscured frame. With at least one WM, KDE
25447 2.1, Emacs is not getting any event for the raising of the frame
25448 (even tried with SubstructureRedirectMask), only Expose events.
25449 These expose events will draw text normally, i.e. not
25450 highlighted. Which means we must redo the highlight here.
25451 Subsume it under ``we love X''. --gerd 2001-08-15 */
25452 /* Included in Windows version because Windows most likely does not
25453 do the right thing if any third party tool offers
25454 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25455 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25456 {
25457 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25458 if (f == dpyinfo->mouse_face_mouse_frame)
25459 {
25460 int x = dpyinfo->mouse_face_mouse_x;
25461 int y = dpyinfo->mouse_face_mouse_y;
25462 clear_mouse_face (dpyinfo);
25463 note_mouse_highlight (f, x, y);
25464 }
25465 }
25466 }
25467
25468
25469 /* EXPORT:
25470 Determine the intersection of two rectangles R1 and R2. Return
25471 the intersection in *RESULT. Value is non-zero if RESULT is not
25472 empty. */
25473
25474 int
25475 x_intersect_rectangles (r1, r2, result)
25476 XRectangle *r1, *r2, *result;
25477 {
25478 XRectangle *left, *right;
25479 XRectangle *upper, *lower;
25480 int intersection_p = 0;
25481
25482 /* Rearrange so that R1 is the left-most rectangle. */
25483 if (r1->x < r2->x)
25484 left = r1, right = r2;
25485 else
25486 left = r2, right = r1;
25487
25488 /* X0 of the intersection is right.x0, if this is inside R1,
25489 otherwise there is no intersection. */
25490 if (right->x <= left->x + left->width)
25491 {
25492 result->x = right->x;
25493
25494 /* The right end of the intersection is the minimum of the
25495 the right ends of left and right. */
25496 result->width = (min (left->x + left->width, right->x + right->width)
25497 - result->x);
25498
25499 /* Same game for Y. */
25500 if (r1->y < r2->y)
25501 upper = r1, lower = r2;
25502 else
25503 upper = r2, lower = r1;
25504
25505 /* The upper end of the intersection is lower.y0, if this is inside
25506 of upper. Otherwise, there is no intersection. */
25507 if (lower->y <= upper->y + upper->height)
25508 {
25509 result->y = lower->y;
25510
25511 /* The lower end of the intersection is the minimum of the lower
25512 ends of upper and lower. */
25513 result->height = (min (lower->y + lower->height,
25514 upper->y + upper->height)
25515 - result->y);
25516 intersection_p = 1;
25517 }
25518 }
25519
25520 return intersection_p;
25521 }
25522
25523 #endif /* HAVE_WINDOW_SYSTEM */
25524
25525 \f
25526 /***********************************************************************
25527 Initialization
25528 ***********************************************************************/
25529
25530 void
25531 syms_of_xdisp ()
25532 {
25533 Vwith_echo_area_save_vector = Qnil;
25534 staticpro (&Vwith_echo_area_save_vector);
25535
25536 Vmessage_stack = Qnil;
25537 staticpro (&Vmessage_stack);
25538
25539 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25540 staticpro (&Qinhibit_redisplay);
25541
25542 message_dolog_marker1 = Fmake_marker ();
25543 staticpro (&message_dolog_marker1);
25544 message_dolog_marker2 = Fmake_marker ();
25545 staticpro (&message_dolog_marker2);
25546 message_dolog_marker3 = Fmake_marker ();
25547 staticpro (&message_dolog_marker3);
25548
25549 #if GLYPH_DEBUG
25550 defsubr (&Sdump_frame_glyph_matrix);
25551 defsubr (&Sdump_glyph_matrix);
25552 defsubr (&Sdump_glyph_row);
25553 defsubr (&Sdump_tool_bar_row);
25554 defsubr (&Strace_redisplay);
25555 defsubr (&Strace_to_stderr);
25556 #endif
25557 #ifdef HAVE_WINDOW_SYSTEM
25558 defsubr (&Stool_bar_lines_needed);
25559 defsubr (&Slookup_image_map);
25560 #endif
25561 defsubr (&Sformat_mode_line);
25562 defsubr (&Sinvisible_p);
25563
25564 staticpro (&Qmenu_bar_update_hook);
25565 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25566
25567 staticpro (&Qoverriding_terminal_local_map);
25568 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25569
25570 staticpro (&Qoverriding_local_map);
25571 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25572
25573 staticpro (&Qwindow_scroll_functions);
25574 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25575
25576 staticpro (&Qwindow_text_change_functions);
25577 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25578
25579 staticpro (&Qredisplay_end_trigger_functions);
25580 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25581
25582 staticpro (&Qinhibit_point_motion_hooks);
25583 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25584
25585 Qeval = intern_c_string ("eval");
25586 staticpro (&Qeval);
25587
25588 QCdata = intern_c_string (":data");
25589 staticpro (&QCdata);
25590 Qdisplay = intern_c_string ("display");
25591 staticpro (&Qdisplay);
25592 Qspace_width = intern_c_string ("space-width");
25593 staticpro (&Qspace_width);
25594 Qraise = intern_c_string ("raise");
25595 staticpro (&Qraise);
25596 Qslice = intern_c_string ("slice");
25597 staticpro (&Qslice);
25598 Qspace = intern_c_string ("space");
25599 staticpro (&Qspace);
25600 Qmargin = intern_c_string ("margin");
25601 staticpro (&Qmargin);
25602 Qpointer = intern_c_string ("pointer");
25603 staticpro (&Qpointer);
25604 Qleft_margin = intern_c_string ("left-margin");
25605 staticpro (&Qleft_margin);
25606 Qright_margin = intern_c_string ("right-margin");
25607 staticpro (&Qright_margin);
25608 Qcenter = intern_c_string ("center");
25609 staticpro (&Qcenter);
25610 Qline_height = intern_c_string ("line-height");
25611 staticpro (&Qline_height);
25612 QCalign_to = intern_c_string (":align-to");
25613 staticpro (&QCalign_to);
25614 QCrelative_width = intern_c_string (":relative-width");
25615 staticpro (&QCrelative_width);
25616 QCrelative_height = intern_c_string (":relative-height");
25617 staticpro (&QCrelative_height);
25618 QCeval = intern_c_string (":eval");
25619 staticpro (&QCeval);
25620 QCpropertize = intern_c_string (":propertize");
25621 staticpro (&QCpropertize);
25622 QCfile = intern_c_string (":file");
25623 staticpro (&QCfile);
25624 Qfontified = intern_c_string ("fontified");
25625 staticpro (&Qfontified);
25626 Qfontification_functions = intern_c_string ("fontification-functions");
25627 staticpro (&Qfontification_functions);
25628 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25629 staticpro (&Qtrailing_whitespace);
25630 Qescape_glyph = intern_c_string ("escape-glyph");
25631 staticpro (&Qescape_glyph);
25632 Qnobreak_space = intern_c_string ("nobreak-space");
25633 staticpro (&Qnobreak_space);
25634 Qimage = intern_c_string ("image");
25635 staticpro (&Qimage);
25636 QCmap = intern_c_string (":map");
25637 staticpro (&QCmap);
25638 QCpointer = intern_c_string (":pointer");
25639 staticpro (&QCpointer);
25640 Qrect = intern_c_string ("rect");
25641 staticpro (&Qrect);
25642 Qcircle = intern_c_string ("circle");
25643 staticpro (&Qcircle);
25644 Qpoly = intern_c_string ("poly");
25645 staticpro (&Qpoly);
25646 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25647 staticpro (&Qmessage_truncate_lines);
25648 Qgrow_only = intern_c_string ("grow-only");
25649 staticpro (&Qgrow_only);
25650 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25651 staticpro (&Qinhibit_menubar_update);
25652 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25653 staticpro (&Qinhibit_eval_during_redisplay);
25654 Qposition = intern_c_string ("position");
25655 staticpro (&Qposition);
25656 Qbuffer_position = intern_c_string ("buffer-position");
25657 staticpro (&Qbuffer_position);
25658 Qobject = intern_c_string ("object");
25659 staticpro (&Qobject);
25660 Qbar = intern_c_string ("bar");
25661 staticpro (&Qbar);
25662 Qhbar = intern_c_string ("hbar");
25663 staticpro (&Qhbar);
25664 Qbox = intern_c_string ("box");
25665 staticpro (&Qbox);
25666 Qhollow = intern_c_string ("hollow");
25667 staticpro (&Qhollow);
25668 Qhand = intern_c_string ("hand");
25669 staticpro (&Qhand);
25670 Qarrow = intern_c_string ("arrow");
25671 staticpro (&Qarrow);
25672 Qtext = intern_c_string ("text");
25673 staticpro (&Qtext);
25674 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25675 staticpro (&Qrisky_local_variable);
25676 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25677 staticpro (&Qinhibit_free_realized_faces);
25678
25679 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25680 Fcons (intern_c_string ("void-variable"), Qnil)),
25681 Qnil);
25682 staticpro (&list_of_error);
25683
25684 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25685 staticpro (&Qlast_arrow_position);
25686 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25687 staticpro (&Qlast_arrow_string);
25688
25689 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25690 staticpro (&Qoverlay_arrow_string);
25691 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25692 staticpro (&Qoverlay_arrow_bitmap);
25693
25694 echo_buffer[0] = echo_buffer[1] = Qnil;
25695 staticpro (&echo_buffer[0]);
25696 staticpro (&echo_buffer[1]);
25697
25698 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25699 staticpro (&echo_area_buffer[0]);
25700 staticpro (&echo_area_buffer[1]);
25701
25702 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25703 staticpro (&Vmessages_buffer_name);
25704
25705 mode_line_proptrans_alist = Qnil;
25706 staticpro (&mode_line_proptrans_alist);
25707 mode_line_string_list = Qnil;
25708 staticpro (&mode_line_string_list);
25709 mode_line_string_face = Qnil;
25710 staticpro (&mode_line_string_face);
25711 mode_line_string_face_prop = Qnil;
25712 staticpro (&mode_line_string_face_prop);
25713 Vmode_line_unwind_vector = Qnil;
25714 staticpro (&Vmode_line_unwind_vector);
25715
25716 help_echo_string = Qnil;
25717 staticpro (&help_echo_string);
25718 help_echo_object = Qnil;
25719 staticpro (&help_echo_object);
25720 help_echo_window = Qnil;
25721 staticpro (&help_echo_window);
25722 previous_help_echo_string = Qnil;
25723 staticpro (&previous_help_echo_string);
25724 help_echo_pos = -1;
25725
25726 Qright_to_left = intern_c_string ("right-to-left");
25727 staticpro (&Qright_to_left);
25728 Qleft_to_right = intern_c_string ("left-to-right");
25729 staticpro (&Qleft_to_right);
25730
25731 #ifdef HAVE_WINDOW_SYSTEM
25732 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25733 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25734 For example, if a block cursor is over a tab, it will be drawn as
25735 wide as that tab on the display. */);
25736 x_stretch_cursor_p = 0;
25737 #endif
25738
25739 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25740 doc: /* *Non-nil means highlight trailing whitespace.
25741 The face used for trailing whitespace is `trailing-whitespace'. */);
25742 Vshow_trailing_whitespace = Qnil;
25743
25744 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25745 doc: /* *Control highlighting of nobreak space and soft hyphen.
25746 A value of t means highlight the character itself (for nobreak space,
25747 use face `nobreak-space').
25748 A value of nil means no highlighting.
25749 Other values mean display the escape glyph followed by an ordinary
25750 space or ordinary hyphen. */);
25751 Vnobreak_char_display = Qt;
25752
25753 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25754 doc: /* *The pointer shape to show in void text areas.
25755 A value of nil means to show the text pointer. Other options are `arrow',
25756 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25757 Vvoid_text_area_pointer = Qarrow;
25758
25759 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25760 doc: /* Non-nil means don't actually do any redisplay.
25761 This is used for internal purposes. */);
25762 Vinhibit_redisplay = Qnil;
25763
25764 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25765 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25766 Vglobal_mode_string = Qnil;
25767
25768 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25769 doc: /* Marker for where to display an arrow on top of the buffer text.
25770 This must be the beginning of a line in order to work.
25771 See also `overlay-arrow-string'. */);
25772 Voverlay_arrow_position = Qnil;
25773
25774 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25775 doc: /* String to display as an arrow in non-window frames.
25776 See also `overlay-arrow-position'. */);
25777 Voverlay_arrow_string = make_pure_c_string ("=>");
25778
25779 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25780 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25781 The symbols on this list are examined during redisplay to determine
25782 where to display overlay arrows. */);
25783 Voverlay_arrow_variable_list
25784 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25785
25786 DEFVAR_INT ("scroll-step", &scroll_step,
25787 doc: /* *The number of lines to try scrolling a window by when point moves out.
25788 If that fails to bring point back on frame, point is centered instead.
25789 If this is zero, point is always centered after it moves off frame.
25790 If you want scrolling to always be a line at a time, you should set
25791 `scroll-conservatively' to a large value rather than set this to 1. */);
25792
25793 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25794 doc: /* *Scroll up to this many lines, to bring point back on screen.
25795 If point moves off-screen, redisplay will scroll by up to
25796 `scroll-conservatively' lines in order to bring point just barely
25797 onto the screen again. If that cannot be done, then redisplay
25798 recenters point as usual.
25799
25800 A value of zero means always recenter point if it moves off screen. */);
25801 scroll_conservatively = 0;
25802
25803 DEFVAR_INT ("scroll-margin", &scroll_margin,
25804 doc: /* *Number of lines of margin at the top and bottom of a window.
25805 Recenter the window whenever point gets within this many lines
25806 of the top or bottom of the window. */);
25807 scroll_margin = 0;
25808
25809 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25810 doc: /* Pixels per inch value for non-window system displays.
25811 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25812 Vdisplay_pixels_per_inch = make_float (72.0);
25813
25814 #if GLYPH_DEBUG
25815 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25816 #endif
25817
25818 DEFVAR_LISP ("truncate-partial-width-windows",
25819 &Vtruncate_partial_width_windows,
25820 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25821 For an integer value, truncate lines in each window narrower than the
25822 full frame width, provided the window width is less than that integer;
25823 otherwise, respect the value of `truncate-lines'.
25824
25825 For any other non-nil value, truncate lines in all windows that do
25826 not span the full frame width.
25827
25828 A value of nil means to respect the value of `truncate-lines'.
25829
25830 If `word-wrap' is enabled, you might want to reduce this. */);
25831 Vtruncate_partial_width_windows = make_number (50);
25832
25833 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25834 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25835 Any other value means to use the appropriate face, `mode-line',
25836 `header-line', or `menu' respectively. */);
25837 mode_line_inverse_video = 1;
25838
25839 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25840 doc: /* *Maximum buffer size for which line number should be displayed.
25841 If the buffer is bigger than this, the line number does not appear
25842 in the mode line. A value of nil means no limit. */);
25843 Vline_number_display_limit = Qnil;
25844
25845 DEFVAR_INT ("line-number-display-limit-width",
25846 &line_number_display_limit_width,
25847 doc: /* *Maximum line width (in characters) for line number display.
25848 If the average length of the lines near point is bigger than this, then the
25849 line number may be omitted from the mode line. */);
25850 line_number_display_limit_width = 200;
25851
25852 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25853 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25854 highlight_nonselected_windows = 0;
25855
25856 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25857 doc: /* Non-nil if more than one frame is visible on this display.
25858 Minibuffer-only frames don't count, but iconified frames do.
25859 This variable is not guaranteed to be accurate except while processing
25860 `frame-title-format' and `icon-title-format'. */);
25861
25862 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25863 doc: /* Template for displaying the title bar of visible frames.
25864 \(Assuming the window manager supports this feature.)
25865
25866 This variable has the same structure as `mode-line-format', except that
25867 the %c and %l constructs are ignored. It is used only on frames for
25868 which no explicit name has been set \(see `modify-frame-parameters'). */);
25869
25870 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25871 doc: /* Template for displaying the title bar of an iconified frame.
25872 \(Assuming the window manager supports this feature.)
25873 This variable has the same structure as `mode-line-format' (which see),
25874 and is used only on frames for which no explicit name has been set
25875 \(see `modify-frame-parameters'). */);
25876 Vicon_title_format
25877 = Vframe_title_format
25878 = pure_cons (intern_c_string ("multiple-frames"),
25879 pure_cons (make_pure_c_string ("%b"),
25880 pure_cons (pure_cons (empty_unibyte_string,
25881 pure_cons (intern_c_string ("invocation-name"),
25882 pure_cons (make_pure_c_string ("@"),
25883 pure_cons (intern_c_string ("system-name"),
25884 Qnil)))),
25885 Qnil)));
25886
25887 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25888 doc: /* Maximum number of lines to keep in the message log buffer.
25889 If nil, disable message logging. If t, log messages but don't truncate
25890 the buffer when it becomes large. */);
25891 Vmessage_log_max = make_number (100);
25892
25893 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25894 doc: /* Functions called before redisplay, if window sizes have changed.
25895 The value should be a list of functions that take one argument.
25896 Just before redisplay, for each frame, if any of its windows have changed
25897 size since the last redisplay, or have been split or deleted,
25898 all the functions in the list are called, with the frame as argument. */);
25899 Vwindow_size_change_functions = Qnil;
25900
25901 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25902 doc: /* List of functions to call before redisplaying a window with scrolling.
25903 Each function is called with two arguments, the window and its new
25904 display-start position. Note that these functions are also called by
25905 `set-window-buffer'. Also note that the value of `window-end' is not
25906 valid when these functions are called. */);
25907 Vwindow_scroll_functions = Qnil;
25908
25909 DEFVAR_LISP ("window-text-change-functions",
25910 &Vwindow_text_change_functions,
25911 doc: /* Functions to call in redisplay when text in the window might change. */);
25912 Vwindow_text_change_functions = Qnil;
25913
25914 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25915 doc: /* Functions called when redisplay of a window reaches the end trigger.
25916 Each function is called with two arguments, the window and the end trigger value.
25917 See `set-window-redisplay-end-trigger'. */);
25918 Vredisplay_end_trigger_functions = Qnil;
25919
25920 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25921 doc: /* *Non-nil means autoselect window with mouse pointer.
25922 If nil, do not autoselect windows.
25923 A positive number means delay autoselection by that many seconds: a
25924 window is autoselected only after the mouse has remained in that
25925 window for the duration of the delay.
25926 A negative number has a similar effect, but causes windows to be
25927 autoselected only after the mouse has stopped moving. \(Because of
25928 the way Emacs compares mouse events, you will occasionally wait twice
25929 that time before the window gets selected.\)
25930 Any other value means to autoselect window instantaneously when the
25931 mouse pointer enters it.
25932
25933 Autoselection selects the minibuffer only if it is active, and never
25934 unselects the minibuffer if it is active.
25935
25936 When customizing this variable make sure that the actual value of
25937 `focus-follows-mouse' matches the behavior of your window manager. */);
25938 Vmouse_autoselect_window = Qnil;
25939
25940 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25941 doc: /* *Non-nil means automatically resize tool-bars.
25942 This dynamically changes the tool-bar's height to the minimum height
25943 that is needed to make all tool-bar items visible.
25944 If value is `grow-only', the tool-bar's height is only increased
25945 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25946 Vauto_resize_tool_bars = Qt;
25947
25948 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25949 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25950 auto_raise_tool_bar_buttons_p = 1;
25951
25952 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25953 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25954 make_cursor_line_fully_visible_p = 1;
25955
25956 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25957 doc: /* *Border below tool-bar in pixels.
25958 If an integer, use it as the height of the border.
25959 If it is one of `internal-border-width' or `border-width', use the
25960 value of the corresponding frame parameter.
25961 Otherwise, no border is added below the tool-bar. */);
25962 Vtool_bar_border = Qinternal_border_width;
25963
25964 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25965 doc: /* *Margin around tool-bar buttons in pixels.
25966 If an integer, use that for both horizontal and vertical margins.
25967 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25968 HORZ specifying the horizontal margin, and VERT specifying the
25969 vertical margin. */);
25970 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25971
25972 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25973 doc: /* *Relief thickness of tool-bar buttons. */);
25974 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25975
25976 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25977 doc: /* List of functions to call to fontify regions of text.
25978 Each function is called with one argument POS. Functions must
25979 fontify a region starting at POS in the current buffer, and give
25980 fontified regions the property `fontified'. */);
25981 Vfontification_functions = Qnil;
25982 Fmake_variable_buffer_local (Qfontification_functions);
25983
25984 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25985 &unibyte_display_via_language_environment,
25986 doc: /* *Non-nil means display unibyte text according to language environment.
25987 Specifically, this means that raw bytes in the range 160-255 decimal
25988 are displayed by converting them to the equivalent multibyte characters
25989 according to the current language environment. As a result, they are
25990 displayed according to the current fontset.
25991
25992 Note that this variable affects only how these bytes are displayed,
25993 but does not change the fact they are interpreted as raw bytes. */);
25994 unibyte_display_via_language_environment = 0;
25995
25996 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25997 doc: /* *Maximum height for resizing mini-windows.
25998 If a float, it specifies a fraction of the mini-window frame's height.
25999 If an integer, it specifies a number of lines. */);
26000 Vmax_mini_window_height = make_float (0.25);
26001
26002 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26003 doc: /* *How to resize mini-windows.
26004 A value of nil means don't automatically resize mini-windows.
26005 A value of t means resize them to fit the text displayed in them.
26006 A value of `grow-only', the default, means let mini-windows grow
26007 only, until their display becomes empty, at which point the windows
26008 go back to their normal size. */);
26009 Vresize_mini_windows = Qgrow_only;
26010
26011 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26012 doc: /* Alist specifying how to blink the cursor off.
26013 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26014 `cursor-type' frame-parameter or variable equals ON-STATE,
26015 comparing using `equal', Emacs uses OFF-STATE to specify
26016 how to blink it off. ON-STATE and OFF-STATE are values for
26017 the `cursor-type' frame parameter.
26018
26019 If a frame's ON-STATE has no entry in this list,
26020 the frame's other specifications determine how to blink the cursor off. */);
26021 Vblink_cursor_alist = Qnil;
26022
26023 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26024 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
26025 automatic_hscrolling_p = 1;
26026 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26027 staticpro (&Qauto_hscroll_mode);
26028
26029 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26030 doc: /* *How many columns away from the window edge point is allowed to get
26031 before automatic hscrolling will horizontally scroll the window. */);
26032 hscroll_margin = 5;
26033
26034 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26035 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26036 When point is less than `hscroll-margin' columns from the window
26037 edge, automatic hscrolling will scroll the window by the amount of columns
26038 determined by this variable. If its value is a positive integer, scroll that
26039 many columns. If it's a positive floating-point number, it specifies the
26040 fraction of the window's width to scroll. If it's nil or zero, point will be
26041 centered horizontally after the scroll. Any other value, including negative
26042 numbers, are treated as if the value were zero.
26043
26044 Automatic hscrolling always moves point outside the scroll margin, so if
26045 point was more than scroll step columns inside the margin, the window will
26046 scroll more than the value given by the scroll step.
26047
26048 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26049 and `scroll-right' overrides this variable's effect. */);
26050 Vhscroll_step = make_number (0);
26051
26052 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26053 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26054 Bind this around calls to `message' to let it take effect. */);
26055 message_truncate_lines = 0;
26056
26057 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26058 doc: /* Normal hook run to update the menu bar definitions.
26059 Redisplay runs this hook before it redisplays the menu bar.
26060 This is used to update submenus such as Buffers,
26061 whose contents depend on various data. */);
26062 Vmenu_bar_update_hook = Qnil;
26063
26064 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26065 doc: /* Frame for which we are updating a menu.
26066 The enable predicate for a menu binding should check this variable. */);
26067 Vmenu_updating_frame = Qnil;
26068
26069 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26070 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26071 inhibit_menubar_update = 0;
26072
26073 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26074 doc: /* Prefix prepended to all continuation lines at display time.
26075 The value may be a string, an image, or a stretch-glyph; it is
26076 interpreted in the same way as the value of a `display' text property.
26077
26078 This variable is overridden by any `wrap-prefix' text or overlay
26079 property.
26080
26081 To add a prefix to non-continuation lines, use `line-prefix'. */);
26082 Vwrap_prefix = Qnil;
26083 staticpro (&Qwrap_prefix);
26084 Qwrap_prefix = intern_c_string ("wrap-prefix");
26085 Fmake_variable_buffer_local (Qwrap_prefix);
26086
26087 DEFVAR_LISP ("line-prefix", &Vline_prefix,
26088 doc: /* Prefix prepended to all non-continuation lines at display time.
26089 The value may be a string, an image, or a stretch-glyph; it is
26090 interpreted in the same way as the value of a `display' text property.
26091
26092 This variable is overridden by any `line-prefix' text or overlay
26093 property.
26094
26095 To add a prefix to continuation lines, use `wrap-prefix'. */);
26096 Vline_prefix = Qnil;
26097 staticpro (&Qline_prefix);
26098 Qline_prefix = intern_c_string ("line-prefix");
26099 Fmake_variable_buffer_local (Qline_prefix);
26100
26101 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
26102 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26103 inhibit_eval_during_redisplay = 0;
26104
26105 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
26106 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26107 inhibit_free_realized_faces = 0;
26108
26109 #if GLYPH_DEBUG
26110 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
26111 doc: /* Inhibit try_window_id display optimization. */);
26112 inhibit_try_window_id = 0;
26113
26114 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
26115 doc: /* Inhibit try_window_reusing display optimization. */);
26116 inhibit_try_window_reusing = 0;
26117
26118 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
26119 doc: /* Inhibit try_cursor_movement display optimization. */);
26120 inhibit_try_cursor_movement = 0;
26121 #endif /* GLYPH_DEBUG */
26122
26123 DEFVAR_INT ("overline-margin", &overline_margin,
26124 doc: /* *Space between overline and text, in pixels.
26125 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26126 margin to the caracter height. */);
26127 overline_margin = 2;
26128
26129 DEFVAR_INT ("underline-minimum-offset",
26130 &underline_minimum_offset,
26131 doc: /* Minimum distance between baseline and underline.
26132 This can improve legibility of underlined text at small font sizes,
26133 particularly when using variable `x-use-underline-position-properties'
26134 with fonts that specify an UNDERLINE_POSITION relatively close to the
26135 baseline. The default value is 1. */);
26136 underline_minimum_offset = 1;
26137
26138 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
26139 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
26140 display_hourglass_p = 1;
26141
26142 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
26143 doc: /* *Seconds to wait before displaying an hourglass pointer.
26144 Value must be an integer or float. */);
26145 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26146
26147 hourglass_atimer = NULL;
26148 hourglass_shown_p = 0;
26149 }
26150
26151
26152 /* Initialize this module when Emacs starts. */
26153
26154 void
26155 init_xdisp ()
26156 {
26157 Lisp_Object root_window;
26158 struct window *mini_w;
26159
26160 current_header_line_height = current_mode_line_height = -1;
26161
26162 CHARPOS (this_line_start_pos) = 0;
26163
26164 mini_w = XWINDOW (minibuf_window);
26165 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26166
26167 if (!noninteractive)
26168 {
26169 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26170 int i;
26171
26172 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26173 set_window_height (root_window,
26174 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
26175 0);
26176 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
26177 set_window_height (minibuf_window, 1, 0);
26178
26179 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
26180 mini_w->total_cols = make_number (FRAME_COLS (f));
26181
26182 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
26183 scratch_glyph_row.glyphs[TEXT_AREA + 1]
26184 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
26185
26186 /* The default ellipsis glyphs `...'. */
26187 for (i = 0; i < 3; ++i)
26188 default_invis_vector[i] = make_number ('.');
26189 }
26190
26191 {
26192 /* Allocate the buffer for frame titles.
26193 Also used for `format-mode-line'. */
26194 int size = 100;
26195 mode_line_noprop_buf = (char *) xmalloc (size);
26196 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
26197 mode_line_noprop_ptr = mode_line_noprop_buf;
26198 mode_line_target = MODE_LINE_DISPLAY;
26199 }
26200
26201 help_echo_showing_p = 0;
26202 }
26203
26204 /* Since w32 does not support atimers, it defines its own implementation of
26205 the following three functions in w32fns.c. */
26206 #ifndef WINDOWSNT
26207
26208 /* Platform-independent portion of hourglass implementation. */
26209
26210 /* Return non-zero if houglass timer has been started or hourglass is shown. */
26211 int
26212 hourglass_started ()
26213 {
26214 return hourglass_shown_p || hourglass_atimer != NULL;
26215 }
26216
26217 /* Cancel a currently active hourglass timer, and start a new one. */
26218 void
26219 start_hourglass ()
26220 {
26221 #if defined (HAVE_WINDOW_SYSTEM)
26222 EMACS_TIME delay;
26223 int secs, usecs = 0;
26224
26225 cancel_hourglass ();
26226
26227 if (INTEGERP (Vhourglass_delay)
26228 && XINT (Vhourglass_delay) > 0)
26229 secs = XFASTINT (Vhourglass_delay);
26230 else if (FLOATP (Vhourglass_delay)
26231 && XFLOAT_DATA (Vhourglass_delay) > 0)
26232 {
26233 Lisp_Object tem;
26234 tem = Ftruncate (Vhourglass_delay, Qnil);
26235 secs = XFASTINT (tem);
26236 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
26237 }
26238 else
26239 secs = DEFAULT_HOURGLASS_DELAY;
26240
26241 EMACS_SET_SECS_USECS (delay, secs, usecs);
26242 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
26243 show_hourglass, NULL);
26244 #endif
26245 }
26246
26247
26248 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
26249 shown. */
26250 void
26251 cancel_hourglass ()
26252 {
26253 #if defined (HAVE_WINDOW_SYSTEM)
26254 if (hourglass_atimer)
26255 {
26256 cancel_atimer (hourglass_atimer);
26257 hourglass_atimer = NULL;
26258 }
26259
26260 if (hourglass_shown_p)
26261 hide_hourglass ();
26262 #endif
26263 }
26264 #endif /* ! WINDOWSNT */
26265
26266 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
26267 (do not change this comment) */