]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Slightly fix the docstring of `select-frame'.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
37
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
47
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
61 |
62 expose_window (asynchronous) |
63 |
64 X expose events -----+
65
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
70
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
80
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
86
87
88 Direct operations.
89
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
94
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
101
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
107
108
109 Desired matrices.
110
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
117
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
124
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
130
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
137
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
145
146
147 Frame matrices.
148
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
155
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
167
168 #include <config.h>
169 #include <stdio.h>
170 #include <limits.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "font.h"
192 #include "fontset.h"
193 #include "blockinput.h"
194
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef HAVE_NS
202 #include "nsterm.h"
203 #endif
204 #ifdef USE_GTK
205 #include "gtkutil.h"
206 #endif
207
208 #include "font.h"
209
210 #ifndef FRAME_X_OUTPUT
211 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
212 #endif
213
214 #define INFINITY 10000000
215
216 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
217 || defined(HAVE_NS) || defined (USE_GTK)
218 extern void set_frame_menubar P_ ((struct frame *f, int, int));
219 extern int pending_menu_activation;
220 #endif
221
222 extern int interrupt_input;
223 extern int command_loop_level;
224
225 extern Lisp_Object do_mouse_tracking;
226
227 extern int minibuffer_auto_raise;
228 extern Lisp_Object Vminibuffer_list;
229
230 extern Lisp_Object Qface;
231 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
232
233 extern Lisp_Object Voverriding_local_map;
234 extern Lisp_Object Voverriding_local_map_menu_flag;
235 extern Lisp_Object Qmenu_item;
236 extern Lisp_Object Qwhen;
237 extern Lisp_Object Qhelp_echo;
238 extern Lisp_Object Qbefore_string, Qafter_string;
239
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
243 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
244 Lisp_Object Qinhibit_point_motion_hooks;
245 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
246 Lisp_Object Qfontified;
247 Lisp_Object Qgrow_only;
248 Lisp_Object Qinhibit_eval_during_redisplay;
249 Lisp_Object Qbuffer_position, Qposition, Qobject;
250
251 /* Cursor shapes */
252 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
253
254 /* Pointer shapes */
255 Lisp_Object Qarrow, Qhand, Qtext;
256
257 Lisp_Object Qrisky_local_variable;
258
259 /* Holds the list (error). */
260 Lisp_Object list_of_error;
261
262 /* Functions called to fontify regions of text. */
263
264 Lisp_Object Vfontification_functions;
265 Lisp_Object Qfontification_functions;
266
267 /* Non-nil means automatically select any window when the mouse
268 cursor moves into it. */
269 Lisp_Object Vmouse_autoselect_window;
270
271 Lisp_Object Vwrap_prefix, Qwrap_prefix;
272 Lisp_Object Vline_prefix, Qline_prefix;
273
274 /* Non-zero means draw tool bar buttons raised when the mouse moves
275 over them. */
276
277 int auto_raise_tool_bar_buttons_p;
278
279 /* Non-zero means to reposition window if cursor line is only partially visible. */
280
281 int make_cursor_line_fully_visible_p;
282
283 /* Margin below tool bar in pixels. 0 or nil means no margin.
284 If value is `internal-border-width' or `border-width',
285 the corresponding frame parameter is used. */
286
287 Lisp_Object Vtool_bar_border;
288
289 /* Margin around tool bar buttons in pixels. */
290
291 Lisp_Object Vtool_bar_button_margin;
292
293 /* Thickness of shadow to draw around tool bar buttons. */
294
295 EMACS_INT tool_bar_button_relief;
296
297 /* Non-nil means automatically resize tool-bars so that all tool-bar
298 items are visible, and no blank lines remain.
299
300 If value is `grow-only', only make tool-bar bigger. */
301
302 Lisp_Object Vauto_resize_tool_bars;
303
304 /* Non-zero means draw block and hollow cursor as wide as the glyph
305 under it. For example, if a block cursor is over a tab, it will be
306 drawn as wide as that tab on the display. */
307
308 int x_stretch_cursor_p;
309
310 /* Non-nil means don't actually do any redisplay. */
311
312 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
313
314 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
315
316 int inhibit_eval_during_redisplay;
317
318 /* Names of text properties relevant for redisplay. */
319
320 Lisp_Object Qdisplay;
321 extern Lisp_Object Qface, Qinvisible, Qwidth;
322
323 /* Symbols used in text property values. */
324
325 Lisp_Object Vdisplay_pixels_per_inch;
326 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
327 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
328 Lisp_Object Qslice;
329 Lisp_Object Qcenter;
330 Lisp_Object Qmargin, Qpointer;
331 Lisp_Object Qline_height;
332 extern Lisp_Object Qheight;
333 extern Lisp_Object QCwidth, QCheight, QCascent;
334 extern Lisp_Object Qscroll_bar;
335 extern Lisp_Object Qcursor;
336
337 /* Non-nil means highlight trailing whitespace. */
338
339 Lisp_Object Vshow_trailing_whitespace;
340
341 /* Non-nil means escape non-break space and hyphens. */
342
343 Lisp_Object Vnobreak_char_display;
344
345 #ifdef HAVE_WINDOW_SYSTEM
346 extern Lisp_Object Voverflow_newline_into_fringe;
347
348 /* Test if overflow newline into fringe. Called with iterator IT
349 at or past right window margin, and with IT->current_x set. */
350
351 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
352 (!NILP (Voverflow_newline_into_fringe) \
353 && FRAME_WINDOW_P (it->f) \
354 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
355 && it->current_x == it->last_visible_x \
356 && it->line_wrap != WORD_WRAP)
357
358 #else /* !HAVE_WINDOW_SYSTEM */
359 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
360 #endif /* HAVE_WINDOW_SYSTEM */
361
362 /* Test if the display element loaded in IT is a space or tab
363 character. This is used to determine word wrapping. */
364
365 #define IT_DISPLAYING_WHITESPACE(it) \
366 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
367
368 /* Non-nil means show the text cursor in void text areas
369 i.e. in blank areas after eol and eob. This used to be
370 the default in 21.3. */
371
372 Lisp_Object Vvoid_text_area_pointer;
373
374 /* Name of the face used to highlight trailing whitespace. */
375
376 Lisp_Object Qtrailing_whitespace;
377
378 /* Name and number of the face used to highlight escape glyphs. */
379
380 Lisp_Object Qescape_glyph;
381
382 /* Name and number of the face used to highlight non-breaking spaces. */
383
384 Lisp_Object Qnobreak_space;
385
386 /* The symbol `image' which is the car of the lists used to represent
387 images in Lisp. */
388
389 Lisp_Object Qimage;
390
391 /* The image map types. */
392 Lisp_Object QCmap, QCpointer;
393 Lisp_Object Qrect, Qcircle, Qpoly;
394
395 /* Non-zero means print newline to stdout before next mini-buffer
396 message. */
397
398 int noninteractive_need_newline;
399
400 /* Non-zero means print newline to message log before next message. */
401
402 static int message_log_need_newline;
403
404 /* Three markers that message_dolog uses.
405 It could allocate them itself, but that causes trouble
406 in handling memory-full errors. */
407 static Lisp_Object message_dolog_marker1;
408 static Lisp_Object message_dolog_marker2;
409 static Lisp_Object message_dolog_marker3;
410 \f
411 /* The buffer position of the first character appearing entirely or
412 partially on the line of the selected window which contains the
413 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
414 redisplay optimization in redisplay_internal. */
415
416 static struct text_pos this_line_start_pos;
417
418 /* Number of characters past the end of the line above, including the
419 terminating newline. */
420
421 static struct text_pos this_line_end_pos;
422
423 /* The vertical positions and the height of this line. */
424
425 static int this_line_vpos;
426 static int this_line_y;
427 static int this_line_pixel_height;
428
429 /* X position at which this display line starts. Usually zero;
430 negative if first character is partially visible. */
431
432 static int this_line_start_x;
433
434 /* Buffer that this_line_.* variables are referring to. */
435
436 static struct buffer *this_line_buffer;
437
438 /* Nonzero means truncate lines in all windows less wide than the
439 frame. */
440
441 Lisp_Object Vtruncate_partial_width_windows;
442
443 /* A flag to control how to display unibyte 8-bit character. */
444
445 int unibyte_display_via_language_environment;
446
447 /* Nonzero means we have more than one non-mini-buffer-only frame.
448 Not guaranteed to be accurate except while parsing
449 frame-title-format. */
450
451 int multiple_frames;
452
453 Lisp_Object Vglobal_mode_string;
454
455
456 /* List of variables (symbols) which hold markers for overlay arrows.
457 The symbols on this list are examined during redisplay to determine
458 where to display overlay arrows. */
459
460 Lisp_Object Voverlay_arrow_variable_list;
461
462 /* Marker for where to display an arrow on top of the buffer text. */
463
464 Lisp_Object Voverlay_arrow_position;
465
466 /* String to display for the arrow. Only used on terminal frames. */
467
468 Lisp_Object Voverlay_arrow_string;
469
470 /* Values of those variables at last redisplay are stored as
471 properties on `overlay-arrow-position' symbol. However, if
472 Voverlay_arrow_position is a marker, last-arrow-position is its
473 numerical position. */
474
475 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
476
477 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
478 properties on a symbol in overlay-arrow-variable-list. */
479
480 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
481
482 /* Like mode-line-format, but for the title bar on a visible frame. */
483
484 Lisp_Object Vframe_title_format;
485
486 /* Like mode-line-format, but for the title bar on an iconified frame. */
487
488 Lisp_Object Vicon_title_format;
489
490 /* List of functions to call when a window's size changes. These
491 functions get one arg, a frame on which one or more windows' sizes
492 have changed. */
493
494 static Lisp_Object Vwindow_size_change_functions;
495
496 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
497
498 /* Nonzero if an overlay arrow has been displayed in this window. */
499
500 static int overlay_arrow_seen;
501
502 /* Nonzero means highlight the region even in nonselected windows. */
503
504 int highlight_nonselected_windows;
505
506 /* If cursor motion alone moves point off frame, try scrolling this
507 many lines up or down if that will bring it back. */
508
509 static EMACS_INT scroll_step;
510
511 /* Nonzero means scroll just far enough to bring point back on the
512 screen, when appropriate. */
513
514 static EMACS_INT scroll_conservatively;
515
516 /* Recenter the window whenever point gets within this many lines of
517 the top or bottom of the window. This value is translated into a
518 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
519 that there is really a fixed pixel height scroll margin. */
520
521 EMACS_INT scroll_margin;
522
523 /* Number of windows showing the buffer of the selected window (or
524 another buffer with the same base buffer). keyboard.c refers to
525 this. */
526
527 int buffer_shared;
528
529 /* Vector containing glyphs for an ellipsis `...'. */
530
531 static Lisp_Object default_invis_vector[3];
532
533 /* Zero means display the mode-line/header-line/menu-bar in the default face
534 (this slightly odd definition is for compatibility with previous versions
535 of emacs), non-zero means display them using their respective faces.
536
537 This variable is deprecated. */
538
539 int mode_line_inverse_video;
540
541 /* Prompt to display in front of the mini-buffer contents. */
542
543 Lisp_Object minibuf_prompt;
544
545 /* Width of current mini-buffer prompt. Only set after display_line
546 of the line that contains the prompt. */
547
548 int minibuf_prompt_width;
549
550 /* This is the window where the echo area message was displayed. It
551 is always a mini-buffer window, but it may not be the same window
552 currently active as a mini-buffer. */
553
554 Lisp_Object echo_area_window;
555
556 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
557 pushes the current message and the value of
558 message_enable_multibyte on the stack, the function restore_message
559 pops the stack and displays MESSAGE again. */
560
561 Lisp_Object Vmessage_stack;
562
563 /* Nonzero means multibyte characters were enabled when the echo area
564 message was specified. */
565
566 int message_enable_multibyte;
567
568 /* Nonzero if we should redraw the mode lines on the next redisplay. */
569
570 int update_mode_lines;
571
572 /* Nonzero if window sizes or contents have changed since last
573 redisplay that finished. */
574
575 int windows_or_buffers_changed;
576
577 /* Nonzero means a frame's cursor type has been changed. */
578
579 int cursor_type_changed;
580
581 /* Nonzero after display_mode_line if %l was used and it displayed a
582 line number. */
583
584 int line_number_displayed;
585
586 /* Maximum buffer size for which to display line numbers. */
587
588 Lisp_Object Vline_number_display_limit;
589
590 /* Line width to consider when repositioning for line number display. */
591
592 static EMACS_INT line_number_display_limit_width;
593
594 /* Number of lines to keep in the message log buffer. t means
595 infinite. nil means don't log at all. */
596
597 Lisp_Object Vmessage_log_max;
598
599 /* The name of the *Messages* buffer, a string. */
600
601 static Lisp_Object Vmessages_buffer_name;
602
603 /* Current, index 0, and last displayed echo area message. Either
604 buffers from echo_buffers, or nil to indicate no message. */
605
606 Lisp_Object echo_area_buffer[2];
607
608 /* The buffers referenced from echo_area_buffer. */
609
610 static Lisp_Object echo_buffer[2];
611
612 /* A vector saved used in with_area_buffer to reduce consing. */
613
614 static Lisp_Object Vwith_echo_area_save_vector;
615
616 /* Non-zero means display_echo_area should display the last echo area
617 message again. Set by redisplay_preserve_echo_area. */
618
619 static int display_last_displayed_message_p;
620
621 /* Nonzero if echo area is being used by print; zero if being used by
622 message. */
623
624 int message_buf_print;
625
626 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
627
628 Lisp_Object Qinhibit_menubar_update;
629 int inhibit_menubar_update;
630
631 /* When evaluating expressions from menu bar items (enable conditions,
632 for instance), this is the frame they are being processed for. */
633
634 Lisp_Object Vmenu_updating_frame;
635
636 /* Maximum height for resizing mini-windows. Either a float
637 specifying a fraction of the available height, or an integer
638 specifying a number of lines. */
639
640 Lisp_Object Vmax_mini_window_height;
641
642 /* Non-zero means messages should be displayed with truncated
643 lines instead of being continued. */
644
645 int message_truncate_lines;
646 Lisp_Object Qmessage_truncate_lines;
647
648 /* Set to 1 in clear_message to make redisplay_internal aware
649 of an emptied echo area. */
650
651 static int message_cleared_p;
652
653 /* How to blink the default frame cursor off. */
654 Lisp_Object Vblink_cursor_alist;
655
656 /* A scratch glyph row with contents used for generating truncation
657 glyphs. Also used in direct_output_for_insert. */
658
659 #define MAX_SCRATCH_GLYPHS 100
660 struct glyph_row scratch_glyph_row;
661 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
662
663 /* Ascent and height of the last line processed by move_it_to. */
664
665 static int last_max_ascent, last_height;
666
667 /* Non-zero if there's a help-echo in the echo area. */
668
669 int help_echo_showing_p;
670
671 /* If >= 0, computed, exact values of mode-line and header-line height
672 to use in the macros CURRENT_MODE_LINE_HEIGHT and
673 CURRENT_HEADER_LINE_HEIGHT. */
674
675 int current_mode_line_height, current_header_line_height;
676
677 /* The maximum distance to look ahead for text properties. Values
678 that are too small let us call compute_char_face and similar
679 functions too often which is expensive. Values that are too large
680 let us call compute_char_face and alike too often because we
681 might not be interested in text properties that far away. */
682
683 #define TEXT_PROP_DISTANCE_LIMIT 100
684
685 #if GLYPH_DEBUG
686
687 /* Variables to turn off display optimizations from Lisp. */
688
689 int inhibit_try_window_id, inhibit_try_window_reusing;
690 int inhibit_try_cursor_movement;
691
692 /* Non-zero means print traces of redisplay if compiled with
693 GLYPH_DEBUG != 0. */
694
695 int trace_redisplay_p;
696
697 #endif /* GLYPH_DEBUG */
698
699 #ifdef DEBUG_TRACE_MOVE
700 /* Non-zero means trace with TRACE_MOVE to stderr. */
701 int trace_move;
702
703 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
704 #else
705 #define TRACE_MOVE(x) (void) 0
706 #endif
707
708 /* Non-zero means automatically scroll windows horizontally to make
709 point visible. */
710
711 int automatic_hscrolling_p;
712 Lisp_Object Qauto_hscroll_mode;
713
714 /* How close to the margin can point get before the window is scrolled
715 horizontally. */
716 EMACS_INT hscroll_margin;
717
718 /* How much to scroll horizontally when point is inside the above margin. */
719 Lisp_Object Vhscroll_step;
720
721 /* The variable `resize-mini-windows'. If nil, don't resize
722 mini-windows. If t, always resize them to fit the text they
723 display. If `grow-only', let mini-windows grow only until they
724 become empty. */
725
726 Lisp_Object Vresize_mini_windows;
727
728 /* Buffer being redisplayed -- for redisplay_window_error. */
729
730 struct buffer *displayed_buffer;
731
732 /* Space between overline and text. */
733
734 EMACS_INT overline_margin;
735
736 /* Require underline to be at least this many screen pixels below baseline
737 This to avoid underline "merging" with the base of letters at small
738 font sizes, particularly when x_use_underline_position_properties is on. */
739
740 EMACS_INT underline_minimum_offset;
741
742 /* Value returned from text property handlers (see below). */
743
744 enum prop_handled
745 {
746 HANDLED_NORMALLY,
747 HANDLED_RECOMPUTE_PROPS,
748 HANDLED_OVERLAY_STRING_CONSUMED,
749 HANDLED_RETURN
750 };
751
752 /* A description of text properties that redisplay is interested
753 in. */
754
755 struct props
756 {
757 /* The name of the property. */
758 Lisp_Object *name;
759
760 /* A unique index for the property. */
761 enum prop_idx idx;
762
763 /* A handler function called to set up iterator IT from the property
764 at IT's current position. Value is used to steer handle_stop. */
765 enum prop_handled (*handler) P_ ((struct it *it));
766 };
767
768 static enum prop_handled handle_face_prop P_ ((struct it *));
769 static enum prop_handled handle_invisible_prop P_ ((struct it *));
770 static enum prop_handled handle_display_prop P_ ((struct it *));
771 static enum prop_handled handle_composition_prop P_ ((struct it *));
772 static enum prop_handled handle_overlay_change P_ ((struct it *));
773 static enum prop_handled handle_fontified_prop P_ ((struct it *));
774
775 /* Properties handled by iterators. */
776
777 static struct props it_props[] =
778 {
779 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
780 /* Handle `face' before `display' because some sub-properties of
781 `display' need to know the face. */
782 {&Qface, FACE_PROP_IDX, handle_face_prop},
783 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
784 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
785 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
786 {NULL, 0, NULL}
787 };
788
789 /* Value is the position described by X. If X is a marker, value is
790 the marker_position of X. Otherwise, value is X. */
791
792 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
793
794 /* Enumeration returned by some move_it_.* functions internally. */
795
796 enum move_it_result
797 {
798 /* Not used. Undefined value. */
799 MOVE_UNDEFINED,
800
801 /* Move ended at the requested buffer position or ZV. */
802 MOVE_POS_MATCH_OR_ZV,
803
804 /* Move ended at the requested X pixel position. */
805 MOVE_X_REACHED,
806
807 /* Move within a line ended at the end of a line that must be
808 continued. */
809 MOVE_LINE_CONTINUED,
810
811 /* Move within a line ended at the end of a line that would
812 be displayed truncated. */
813 MOVE_LINE_TRUNCATED,
814
815 /* Move within a line ended at a line end. */
816 MOVE_NEWLINE_OR_CR
817 };
818
819 /* This counter is used to clear the face cache every once in a while
820 in redisplay_internal. It is incremented for each redisplay.
821 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
822 cleared. */
823
824 #define CLEAR_FACE_CACHE_COUNT 500
825 static int clear_face_cache_count;
826
827 /* Similarly for the image cache. */
828
829 #ifdef HAVE_WINDOW_SYSTEM
830 #define CLEAR_IMAGE_CACHE_COUNT 101
831 static int clear_image_cache_count;
832 #endif
833
834 /* Non-zero while redisplay_internal is in progress. */
835
836 int redisplaying_p;
837
838 /* Non-zero means don't free realized faces. Bound while freeing
839 realized faces is dangerous because glyph matrices might still
840 reference them. */
841
842 int inhibit_free_realized_faces;
843 Lisp_Object Qinhibit_free_realized_faces;
844
845 /* If a string, XTread_socket generates an event to display that string.
846 (The display is done in read_char.) */
847
848 Lisp_Object help_echo_string;
849 Lisp_Object help_echo_window;
850 Lisp_Object help_echo_object;
851 int help_echo_pos;
852
853 /* Temporary variable for XTread_socket. */
854
855 Lisp_Object previous_help_echo_string;
856
857 /* Null glyph slice */
858
859 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
860
861 /* Platform-independent portion of hourglass implementation. */
862
863 /* Non-zero means we're allowed to display a hourglass pointer. */
864 int display_hourglass_p;
865
866 /* Non-zero means an hourglass cursor is currently shown. */
867 int hourglass_shown_p;
868
869 /* If non-null, an asynchronous timer that, when it expires, displays
870 an hourglass cursor on all frames. */
871 struct atimer *hourglass_atimer;
872
873 /* Number of seconds to wait before displaying an hourglass cursor. */
874 Lisp_Object Vhourglass_delay;
875
876 /* Default number of seconds to wait before displaying an hourglass
877 cursor. */
878 #define DEFAULT_HOURGLASS_DELAY 1
879
880 \f
881 /* Function prototypes. */
882
883 static void setup_for_ellipsis P_ ((struct it *, int));
884 static void mark_window_display_accurate_1 P_ ((struct window *, int));
885 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
886 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
887 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
888 static int redisplay_mode_lines P_ ((Lisp_Object, int));
889 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
890
891 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
892
893 static void handle_line_prefix P_ ((struct it *));
894
895 static void pint2str P_ ((char *, int, int));
896 static void pint2hrstr P_ ((char *, int, int));
897 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
898 struct text_pos));
899 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
900 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
901 static void store_mode_line_noprop_char P_ ((char));
902 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
903 static void x_consider_frame_title P_ ((Lisp_Object));
904 static void handle_stop P_ ((struct it *));
905 static int tool_bar_lines_needed P_ ((struct frame *, int *));
906 static int single_display_spec_intangible_p P_ ((Lisp_Object));
907 static void ensure_echo_area_buffers P_ ((void));
908 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
909 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
910 static int with_echo_area_buffer P_ ((struct window *, int,
911 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
912 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
913 static void clear_garbaged_frames P_ ((void));
914 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
916 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int display_echo_area P_ ((struct window *));
918 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
920 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
921 static int string_char_and_length P_ ((const unsigned char *, int, int *));
922 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
923 struct text_pos));
924 static int compute_window_start_on_continuation_line P_ ((struct window *));
925 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
926 static void insert_left_trunc_glyphs P_ ((struct it *));
927 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
928 Lisp_Object));
929 static void extend_face_to_end_of_line P_ ((struct it *));
930 static int append_space_for_newline P_ ((struct it *, int));
931 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
932 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
933 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
934 static int trailing_whitespace_p P_ ((int));
935 static int message_log_check_duplicate P_ ((int, int, int, int));
936 static void push_it P_ ((struct it *));
937 static void pop_it P_ ((struct it *));
938 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
939 static void select_frame_for_redisplay P_ ((Lisp_Object));
940 static void redisplay_internal P_ ((int));
941 static int echo_area_display P_ ((int));
942 static void redisplay_windows P_ ((Lisp_Object));
943 static void redisplay_window P_ ((Lisp_Object, int));
944 static Lisp_Object redisplay_window_error ();
945 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
946 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
947 static int update_menu_bar P_ ((struct frame *, int, int));
948 static int try_window_reusing_current_matrix P_ ((struct window *));
949 static int try_window_id P_ ((struct window *));
950 static int display_line P_ ((struct it *));
951 static int display_mode_lines P_ ((struct window *));
952 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
953 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
954 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
955 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
956 static void display_menu_bar P_ ((struct window *));
957 static int display_count_lines P_ ((int, int, int, int, int *));
958 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
959 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
960 static void compute_line_metrics P_ ((struct it *));
961 static void run_redisplay_end_trigger_hook P_ ((struct it *));
962 static int get_overlay_strings P_ ((struct it *, int));
963 static int get_overlay_strings_1 P_ ((struct it *, int, int));
964 static void next_overlay_string P_ ((struct it *));
965 static void reseat P_ ((struct it *, struct text_pos, int));
966 static void reseat_1 P_ ((struct it *, struct text_pos, int));
967 static void back_to_previous_visible_line_start P_ ((struct it *));
968 void reseat_at_previous_visible_line_start P_ ((struct it *));
969 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
970 static int next_element_from_ellipsis P_ ((struct it *));
971 static int next_element_from_display_vector P_ ((struct it *));
972 static int next_element_from_string P_ ((struct it *));
973 static int next_element_from_c_string P_ ((struct it *));
974 static int next_element_from_buffer P_ ((struct it *));
975 static int next_element_from_composition P_ ((struct it *));
976 static int next_element_from_image P_ ((struct it *));
977 static int next_element_from_stretch P_ ((struct it *));
978 static void load_overlay_strings P_ ((struct it *, int));
979 static int init_from_display_pos P_ ((struct it *, struct window *,
980 struct display_pos *));
981 static void reseat_to_string P_ ((struct it *, unsigned char *,
982 Lisp_Object, int, int, int, int));
983 static enum move_it_result
984 move_it_in_display_line_to (struct it *, EMACS_INT, int,
985 enum move_operation_enum);
986 void move_it_vertically_backward P_ ((struct it *, int));
987 static void init_to_row_start P_ ((struct it *, struct window *,
988 struct glyph_row *));
989 static int init_to_row_end P_ ((struct it *, struct window *,
990 struct glyph_row *));
991 static void back_to_previous_line_start P_ ((struct it *));
992 static int forward_to_next_line_start P_ ((struct it *, int *));
993 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
994 Lisp_Object, int));
995 static struct text_pos string_pos P_ ((int, Lisp_Object));
996 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
997 static int number_of_chars P_ ((unsigned char *, int));
998 static void compute_stop_pos P_ ((struct it *));
999 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1000 Lisp_Object));
1001 static int face_before_or_after_it_pos P_ ((struct it *, int));
1002 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1003 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1004 Lisp_Object, Lisp_Object,
1005 struct text_pos *, int));
1006 static int underlying_face_id P_ ((struct it *));
1007 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1008 struct window *));
1009
1010 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1011 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1012
1013 #ifdef HAVE_WINDOW_SYSTEM
1014
1015 static void update_tool_bar P_ ((struct frame *, int));
1016 static void build_desired_tool_bar_string P_ ((struct frame *f));
1017 static int redisplay_tool_bar P_ ((struct frame *));
1018 static void display_tool_bar_line P_ ((struct it *, int));
1019 static void notice_overwritten_cursor P_ ((struct window *,
1020 enum glyph_row_area,
1021 int, int, int, int));
1022
1023
1024
1025 #endif /* HAVE_WINDOW_SYSTEM */
1026
1027 \f
1028 /***********************************************************************
1029 Window display dimensions
1030 ***********************************************************************/
1031
1032 /* Return the bottom boundary y-position for text lines in window W.
1033 This is the first y position at which a line cannot start.
1034 It is relative to the top of the window.
1035
1036 This is the height of W minus the height of a mode line, if any. */
1037
1038 INLINE int
1039 window_text_bottom_y (w)
1040 struct window *w;
1041 {
1042 int height = WINDOW_TOTAL_HEIGHT (w);
1043
1044 if (WINDOW_WANTS_MODELINE_P (w))
1045 height -= CURRENT_MODE_LINE_HEIGHT (w);
1046 return height;
1047 }
1048
1049 /* Return the pixel width of display area AREA of window W. AREA < 0
1050 means return the total width of W, not including fringes to
1051 the left and right of the window. */
1052
1053 INLINE int
1054 window_box_width (w, area)
1055 struct window *w;
1056 int area;
1057 {
1058 int cols = XFASTINT (w->total_cols);
1059 int pixels = 0;
1060
1061 if (!w->pseudo_window_p)
1062 {
1063 cols -= WINDOW_SCROLL_BAR_COLS (w);
1064
1065 if (area == TEXT_AREA)
1066 {
1067 if (INTEGERP (w->left_margin_cols))
1068 cols -= XFASTINT (w->left_margin_cols);
1069 if (INTEGERP (w->right_margin_cols))
1070 cols -= XFASTINT (w->right_margin_cols);
1071 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1072 }
1073 else if (area == LEFT_MARGIN_AREA)
1074 {
1075 cols = (INTEGERP (w->left_margin_cols)
1076 ? XFASTINT (w->left_margin_cols) : 0);
1077 pixels = 0;
1078 }
1079 else if (area == RIGHT_MARGIN_AREA)
1080 {
1081 cols = (INTEGERP (w->right_margin_cols)
1082 ? XFASTINT (w->right_margin_cols) : 0);
1083 pixels = 0;
1084 }
1085 }
1086
1087 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1088 }
1089
1090
1091 /* Return the pixel height of the display area of window W, not
1092 including mode lines of W, if any. */
1093
1094 INLINE int
1095 window_box_height (w)
1096 struct window *w;
1097 {
1098 struct frame *f = XFRAME (w->frame);
1099 int height = WINDOW_TOTAL_HEIGHT (w);
1100
1101 xassert (height >= 0);
1102
1103 /* Note: the code below that determines the mode-line/header-line
1104 height is essentially the same as that contained in the macro
1105 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1106 the appropriate glyph row has its `mode_line_p' flag set,
1107 and if it doesn't, uses estimate_mode_line_height instead. */
1108
1109 if (WINDOW_WANTS_MODELINE_P (w))
1110 {
1111 struct glyph_row *ml_row
1112 = (w->current_matrix && w->current_matrix->rows
1113 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1114 : 0);
1115 if (ml_row && ml_row->mode_line_p)
1116 height -= ml_row->height;
1117 else
1118 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1119 }
1120
1121 if (WINDOW_WANTS_HEADER_LINE_P (w))
1122 {
1123 struct glyph_row *hl_row
1124 = (w->current_matrix && w->current_matrix->rows
1125 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1126 : 0);
1127 if (hl_row && hl_row->mode_line_p)
1128 height -= hl_row->height;
1129 else
1130 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1131 }
1132
1133 /* With a very small font and a mode-line that's taller than
1134 default, we might end up with a negative height. */
1135 return max (0, height);
1136 }
1137
1138 /* Return the window-relative coordinate of the left edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the right of the left fringe of W. */
1141
1142 INLINE int
1143 window_box_left_offset (w, area)
1144 struct window *w;
1145 int area;
1146 {
1147 int x;
1148
1149 if (w->pseudo_window_p)
1150 return 0;
1151
1152 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1153
1154 if (area == TEXT_AREA)
1155 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1156 + window_box_width (w, LEFT_MARGIN_AREA));
1157 else if (area == RIGHT_MARGIN_AREA)
1158 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1159 + window_box_width (w, LEFT_MARGIN_AREA)
1160 + window_box_width (w, TEXT_AREA)
1161 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1162 ? 0
1163 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1164 else if (area == LEFT_MARGIN_AREA
1165 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1166 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1167
1168 return x;
1169 }
1170
1171
1172 /* Return the window-relative coordinate of the right edge of display
1173 area AREA of window W. AREA < 0 means return the left edge of the
1174 whole window, to the left of the right fringe of W. */
1175
1176 INLINE int
1177 window_box_right_offset (w, area)
1178 struct window *w;
1179 int area;
1180 {
1181 return window_box_left_offset (w, area) + window_box_width (w, area);
1182 }
1183
1184 /* Return the frame-relative coordinate of the left edge of display
1185 area AREA of window W. AREA < 0 means return the left edge of the
1186 whole window, to the right of the left fringe of W. */
1187
1188 INLINE int
1189 window_box_left (w, area)
1190 struct window *w;
1191 int area;
1192 {
1193 struct frame *f = XFRAME (w->frame);
1194 int x;
1195
1196 if (w->pseudo_window_p)
1197 return FRAME_INTERNAL_BORDER_WIDTH (f);
1198
1199 x = (WINDOW_LEFT_EDGE_X (w)
1200 + window_box_left_offset (w, area));
1201
1202 return x;
1203 }
1204
1205
1206 /* Return the frame-relative coordinate of the right edge of display
1207 area AREA of window W. AREA < 0 means return the left edge of the
1208 whole window, to the left of the right fringe of W. */
1209
1210 INLINE int
1211 window_box_right (w, area)
1212 struct window *w;
1213 int area;
1214 {
1215 return window_box_left (w, area) + window_box_width (w, area);
1216 }
1217
1218 /* Get the bounding box of the display area AREA of window W, without
1219 mode lines, in frame-relative coordinates. AREA < 0 means the
1220 whole window, not including the left and right fringes of
1221 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1222 coordinates of the upper-left corner of the box. Return in
1223 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1224
1225 INLINE void
1226 window_box (w, area, box_x, box_y, box_width, box_height)
1227 struct window *w;
1228 int area;
1229 int *box_x, *box_y, *box_width, *box_height;
1230 {
1231 if (box_width)
1232 *box_width = window_box_width (w, area);
1233 if (box_height)
1234 *box_height = window_box_height (w);
1235 if (box_x)
1236 *box_x = window_box_left (w, area);
1237 if (box_y)
1238 {
1239 *box_y = WINDOW_TOP_EDGE_Y (w);
1240 if (WINDOW_WANTS_HEADER_LINE_P (w))
1241 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1242 }
1243 }
1244
1245
1246 /* Get the bounding box of the display area AREA of window W, without
1247 mode lines. AREA < 0 means the whole window, not including the
1248 left and right fringe of the window. Return in *TOP_LEFT_X
1249 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1250 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1251 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1252 box. */
1253
1254 INLINE void
1255 window_box_edges (w, area, top_left_x, top_left_y,
1256 bottom_right_x, bottom_right_y)
1257 struct window *w;
1258 int area;
1259 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1260 {
1261 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1262 bottom_right_y);
1263 *bottom_right_x += *top_left_x;
1264 *bottom_right_y += *top_left_y;
1265 }
1266
1267
1268 \f
1269 /***********************************************************************
1270 Utilities
1271 ***********************************************************************/
1272
1273 /* Return the bottom y-position of the line the iterator IT is in.
1274 This can modify IT's settings. */
1275
1276 int
1277 line_bottom_y (it)
1278 struct it *it;
1279 {
1280 int line_height = it->max_ascent + it->max_descent;
1281 int line_top_y = it->current_y;
1282
1283 if (line_height == 0)
1284 {
1285 if (last_height)
1286 line_height = last_height;
1287 else if (IT_CHARPOS (*it) < ZV)
1288 {
1289 move_it_by_lines (it, 1, 1);
1290 line_height = (it->max_ascent || it->max_descent
1291 ? it->max_ascent + it->max_descent
1292 : last_height);
1293 }
1294 else
1295 {
1296 struct glyph_row *row = it->glyph_row;
1297
1298 /* Use the default character height. */
1299 it->glyph_row = NULL;
1300 it->what = IT_CHARACTER;
1301 it->c = ' ';
1302 it->len = 1;
1303 PRODUCE_GLYPHS (it);
1304 line_height = it->ascent + it->descent;
1305 it->glyph_row = row;
1306 }
1307 }
1308
1309 return line_top_y + line_height;
1310 }
1311
1312
1313 /* Return 1 if position CHARPOS is visible in window W.
1314 CHARPOS < 0 means return info about WINDOW_END position.
1315 If visible, set *X and *Y to pixel coordinates of top left corner.
1316 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1317 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1318
1319 int
1320 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1321 struct window *w;
1322 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1323 {
1324 struct it it;
1325 struct text_pos top;
1326 int visible_p = 0;
1327 struct buffer *old_buffer = NULL;
1328
1329 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1330 return visible_p;
1331
1332 if (XBUFFER (w->buffer) != current_buffer)
1333 {
1334 old_buffer = current_buffer;
1335 set_buffer_internal_1 (XBUFFER (w->buffer));
1336 }
1337
1338 SET_TEXT_POS_FROM_MARKER (top, w->start);
1339
1340 /* Compute exact mode line heights. */
1341 if (WINDOW_WANTS_MODELINE_P (w))
1342 current_mode_line_height
1343 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1344 current_buffer->mode_line_format);
1345
1346 if (WINDOW_WANTS_HEADER_LINE_P (w))
1347 current_header_line_height
1348 = display_mode_line (w, HEADER_LINE_FACE_ID,
1349 current_buffer->header_line_format);
1350
1351 start_display (&it, w, top);
1352 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1353 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1354
1355 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1356 {
1357 /* We have reached CHARPOS, or passed it. How the call to
1358 move_it_to can overshoot: (i) If CHARPOS is on invisible
1359 text, move_it_to stops at the end of the invisible text,
1360 after CHARPOS. (ii) If CHARPOS is in a display vector,
1361 move_it_to stops on its last glyph. */
1362 int top_x = it.current_x;
1363 int top_y = it.current_y;
1364 enum it_method it_method = it.method;
1365 /* Calling line_bottom_y may change it.method. */
1366 int bottom_y = (last_height = 0, line_bottom_y (&it));
1367 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1368
1369 if (top_y < window_top_y)
1370 visible_p = bottom_y > window_top_y;
1371 else if (top_y < it.last_visible_y)
1372 visible_p = 1;
1373 if (visible_p)
1374 {
1375 if (it_method == GET_FROM_BUFFER)
1376 {
1377 Lisp_Object window, prop;
1378
1379 XSETWINDOW (window, w);
1380 prop = Fget_char_property (make_number (it.position.charpos),
1381 Qinvisible, window);
1382
1383 /* If charpos coincides with invisible text covered with an
1384 ellipsis, use the first glyph of the ellipsis to compute
1385 the pixel positions. */
1386 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1387 {
1388 struct glyph_row *row = it.glyph_row;
1389 struct glyph *glyph = row->glyphs[TEXT_AREA];
1390 struct glyph *end = glyph + row->used[TEXT_AREA];
1391 int x = row->x;
1392
1393 for (; glyph < end
1394 && (!BUFFERP (glyph->object)
1395 || glyph->charpos < charpos);
1396 glyph++)
1397 x += glyph->pixel_width;
1398 top_x = x;
1399 }
1400 }
1401 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1402 {
1403 /* We stopped on the last glyph of a display vector.
1404 Try and recompute. Hack alert! */
1405 if (charpos < 2 || top.charpos >= charpos)
1406 top_x = it.glyph_row->x;
1407 else
1408 {
1409 struct it it2;
1410 start_display (&it2, w, top);
1411 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1412 get_next_display_element (&it2);
1413 PRODUCE_GLYPHS (&it2);
1414 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1415 || it2.current_x > it2.last_visible_x)
1416 top_x = it.glyph_row->x;
1417 else
1418 {
1419 top_x = it2.current_x;
1420 top_y = it2.current_y;
1421 }
1422 }
1423 }
1424
1425 *x = top_x;
1426 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1427 *rtop = max (0, window_top_y - top_y);
1428 *rbot = max (0, bottom_y - it.last_visible_y);
1429 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1430 - max (top_y, window_top_y)));
1431 *vpos = it.vpos;
1432 }
1433 }
1434 else
1435 {
1436 struct it it2;
1437
1438 it2 = it;
1439 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1440 move_it_by_lines (&it, 1, 0);
1441 if (charpos < IT_CHARPOS (it)
1442 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1443 {
1444 visible_p = 1;
1445 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1446 *x = it2.current_x;
1447 *y = it2.current_y + it2.max_ascent - it2.ascent;
1448 *rtop = max (0, -it2.current_y);
1449 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1450 - it.last_visible_y));
1451 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1452 it.last_visible_y)
1453 - max (it2.current_y,
1454 WINDOW_HEADER_LINE_HEIGHT (w))));
1455 *vpos = it2.vpos;
1456 }
1457 }
1458
1459 if (old_buffer)
1460 set_buffer_internal_1 (old_buffer);
1461
1462 current_header_line_height = current_mode_line_height = -1;
1463
1464 if (visible_p && XFASTINT (w->hscroll) > 0)
1465 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1466
1467 #if 0
1468 /* Debugging code. */
1469 if (visible_p)
1470 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1471 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1472 else
1473 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1474 #endif
1475
1476 return visible_p;
1477 }
1478
1479
1480 /* Return the next character from STR which is MAXLEN bytes long.
1481 Return in *LEN the length of the character. This is like
1482 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1483 we find one, we return a `?', but with the length of the invalid
1484 character. */
1485
1486 static INLINE int
1487 string_char_and_length (str, maxlen, len)
1488 const unsigned char *str;
1489 int maxlen, *len;
1490 {
1491 int c;
1492
1493 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1494 if (!CHAR_VALID_P (c, 1))
1495 /* We may not change the length here because other places in Emacs
1496 don't use this function, i.e. they silently accept invalid
1497 characters. */
1498 c = '?';
1499
1500 return c;
1501 }
1502
1503
1504
1505 /* Given a position POS containing a valid character and byte position
1506 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1507
1508 static struct text_pos
1509 string_pos_nchars_ahead (pos, string, nchars)
1510 struct text_pos pos;
1511 Lisp_Object string;
1512 int nchars;
1513 {
1514 xassert (STRINGP (string) && nchars >= 0);
1515
1516 if (STRING_MULTIBYTE (string))
1517 {
1518 int rest = SBYTES (string) - BYTEPOS (pos);
1519 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1520 int len;
1521
1522 while (nchars--)
1523 {
1524 string_char_and_length (p, rest, &len);
1525 p += len, rest -= len;
1526 xassert (rest >= 0);
1527 CHARPOS (pos) += 1;
1528 BYTEPOS (pos) += len;
1529 }
1530 }
1531 else
1532 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1533
1534 return pos;
1535 }
1536
1537
1538 /* Value is the text position, i.e. character and byte position,
1539 for character position CHARPOS in STRING. */
1540
1541 static INLINE struct text_pos
1542 string_pos (charpos, string)
1543 int charpos;
1544 Lisp_Object string;
1545 {
1546 struct text_pos pos;
1547 xassert (STRINGP (string));
1548 xassert (charpos >= 0);
1549 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1550 return pos;
1551 }
1552
1553
1554 /* Value is a text position, i.e. character and byte position, for
1555 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1556 means recognize multibyte characters. */
1557
1558 static struct text_pos
1559 c_string_pos (charpos, s, multibyte_p)
1560 int charpos;
1561 unsigned char *s;
1562 int multibyte_p;
1563 {
1564 struct text_pos pos;
1565
1566 xassert (s != NULL);
1567 xassert (charpos >= 0);
1568
1569 if (multibyte_p)
1570 {
1571 int rest = strlen (s), len;
1572
1573 SET_TEXT_POS (pos, 0, 0);
1574 while (charpos--)
1575 {
1576 string_char_and_length (s, rest, &len);
1577 s += len, rest -= len;
1578 xassert (rest >= 0);
1579 CHARPOS (pos) += 1;
1580 BYTEPOS (pos) += len;
1581 }
1582 }
1583 else
1584 SET_TEXT_POS (pos, charpos, charpos);
1585
1586 return pos;
1587 }
1588
1589
1590 /* Value is the number of characters in C string S. MULTIBYTE_P
1591 non-zero means recognize multibyte characters. */
1592
1593 static int
1594 number_of_chars (s, multibyte_p)
1595 unsigned char *s;
1596 int multibyte_p;
1597 {
1598 int nchars;
1599
1600 if (multibyte_p)
1601 {
1602 int rest = strlen (s), len;
1603 unsigned char *p = (unsigned char *) s;
1604
1605 for (nchars = 0; rest > 0; ++nchars)
1606 {
1607 string_char_and_length (p, rest, &len);
1608 rest -= len, p += len;
1609 }
1610 }
1611 else
1612 nchars = strlen (s);
1613
1614 return nchars;
1615 }
1616
1617
1618 /* Compute byte position NEWPOS->bytepos corresponding to
1619 NEWPOS->charpos. POS is a known position in string STRING.
1620 NEWPOS->charpos must be >= POS.charpos. */
1621
1622 static void
1623 compute_string_pos (newpos, pos, string)
1624 struct text_pos *newpos, pos;
1625 Lisp_Object string;
1626 {
1627 xassert (STRINGP (string));
1628 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1629
1630 if (STRING_MULTIBYTE (string))
1631 *newpos = string_pos_nchars_ahead (pos, string,
1632 CHARPOS (*newpos) - CHARPOS (pos));
1633 else
1634 BYTEPOS (*newpos) = CHARPOS (*newpos);
1635 }
1636
1637 /* EXPORT:
1638 Return an estimation of the pixel height of mode or top lines on
1639 frame F. FACE_ID specifies what line's height to estimate. */
1640
1641 int
1642 estimate_mode_line_height (f, face_id)
1643 struct frame *f;
1644 enum face_id face_id;
1645 {
1646 #ifdef HAVE_WINDOW_SYSTEM
1647 if (FRAME_WINDOW_P (f))
1648 {
1649 int height = FONT_HEIGHT (FRAME_FONT (f));
1650
1651 /* This function is called so early when Emacs starts that the face
1652 cache and mode line face are not yet initialized. */
1653 if (FRAME_FACE_CACHE (f))
1654 {
1655 struct face *face = FACE_FROM_ID (f, face_id);
1656 if (face)
1657 {
1658 if (face->font)
1659 height = FONT_HEIGHT (face->font);
1660 if (face->box_line_width > 0)
1661 height += 2 * face->box_line_width;
1662 }
1663 }
1664
1665 return height;
1666 }
1667 #endif
1668
1669 return 1;
1670 }
1671
1672 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1673 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1674 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1675 not force the value into range. */
1676
1677 void
1678 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1679 FRAME_PTR f;
1680 register int pix_x, pix_y;
1681 int *x, *y;
1682 NativeRectangle *bounds;
1683 int noclip;
1684 {
1685
1686 #ifdef HAVE_WINDOW_SYSTEM
1687 if (FRAME_WINDOW_P (f))
1688 {
1689 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1690 even for negative values. */
1691 if (pix_x < 0)
1692 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1693 if (pix_y < 0)
1694 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1695
1696 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1697 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1698
1699 if (bounds)
1700 STORE_NATIVE_RECT (*bounds,
1701 FRAME_COL_TO_PIXEL_X (f, pix_x),
1702 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1703 FRAME_COLUMN_WIDTH (f) - 1,
1704 FRAME_LINE_HEIGHT (f) - 1);
1705
1706 if (!noclip)
1707 {
1708 if (pix_x < 0)
1709 pix_x = 0;
1710 else if (pix_x > FRAME_TOTAL_COLS (f))
1711 pix_x = FRAME_TOTAL_COLS (f);
1712
1713 if (pix_y < 0)
1714 pix_y = 0;
1715 else if (pix_y > FRAME_LINES (f))
1716 pix_y = FRAME_LINES (f);
1717 }
1718 }
1719 #endif
1720
1721 *x = pix_x;
1722 *y = pix_y;
1723 }
1724
1725
1726 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1727 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1728 can't tell the positions because W's display is not up to date,
1729 return 0. */
1730
1731 int
1732 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1733 struct window *w;
1734 int hpos, vpos;
1735 int *frame_x, *frame_y;
1736 {
1737 #ifdef HAVE_WINDOW_SYSTEM
1738 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1739 {
1740 int success_p;
1741
1742 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1743 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1744
1745 if (display_completed)
1746 {
1747 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1748 struct glyph *glyph = row->glyphs[TEXT_AREA];
1749 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1750
1751 hpos = row->x;
1752 vpos = row->y;
1753 while (glyph < end)
1754 {
1755 hpos += glyph->pixel_width;
1756 ++glyph;
1757 }
1758
1759 /* If first glyph is partially visible, its first visible position is still 0. */
1760 if (hpos < 0)
1761 hpos = 0;
1762
1763 success_p = 1;
1764 }
1765 else
1766 {
1767 hpos = vpos = 0;
1768 success_p = 0;
1769 }
1770
1771 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1772 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1773 return success_p;
1774 }
1775 #endif
1776
1777 *frame_x = hpos;
1778 *frame_y = vpos;
1779 return 1;
1780 }
1781
1782
1783 #ifdef HAVE_WINDOW_SYSTEM
1784
1785 /* Find the glyph under window-relative coordinates X/Y in window W.
1786 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1787 strings. Return in *HPOS and *VPOS the row and column number of
1788 the glyph found. Return in *AREA the glyph area containing X.
1789 Value is a pointer to the glyph found or null if X/Y is not on
1790 text, or we can't tell because W's current matrix is not up to
1791 date. */
1792
1793 static
1794 struct glyph *
1795 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1796 struct window *w;
1797 int x, y;
1798 int *hpos, *vpos, *dx, *dy, *area;
1799 {
1800 struct glyph *glyph, *end;
1801 struct glyph_row *row = NULL;
1802 int x0, i;
1803
1804 /* Find row containing Y. Give up if some row is not enabled. */
1805 for (i = 0; i < w->current_matrix->nrows; ++i)
1806 {
1807 row = MATRIX_ROW (w->current_matrix, i);
1808 if (!row->enabled_p)
1809 return NULL;
1810 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1811 break;
1812 }
1813
1814 *vpos = i;
1815 *hpos = 0;
1816
1817 /* Give up if Y is not in the window. */
1818 if (i == w->current_matrix->nrows)
1819 return NULL;
1820
1821 /* Get the glyph area containing X. */
1822 if (w->pseudo_window_p)
1823 {
1824 *area = TEXT_AREA;
1825 x0 = 0;
1826 }
1827 else
1828 {
1829 if (x < window_box_left_offset (w, TEXT_AREA))
1830 {
1831 *area = LEFT_MARGIN_AREA;
1832 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1833 }
1834 else if (x < window_box_right_offset (w, TEXT_AREA))
1835 {
1836 *area = TEXT_AREA;
1837 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1838 }
1839 else
1840 {
1841 *area = RIGHT_MARGIN_AREA;
1842 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1843 }
1844 }
1845
1846 /* Find glyph containing X. */
1847 glyph = row->glyphs[*area];
1848 end = glyph + row->used[*area];
1849 x -= x0;
1850 while (glyph < end && x >= glyph->pixel_width)
1851 {
1852 x -= glyph->pixel_width;
1853 ++glyph;
1854 }
1855
1856 if (glyph == end)
1857 return NULL;
1858
1859 if (dx)
1860 {
1861 *dx = x;
1862 *dy = y - (row->y + row->ascent - glyph->ascent);
1863 }
1864
1865 *hpos = glyph - row->glyphs[*area];
1866 return glyph;
1867 }
1868
1869
1870 /* EXPORT:
1871 Convert frame-relative x/y to coordinates relative to window W.
1872 Takes pseudo-windows into account. */
1873
1874 void
1875 frame_to_window_pixel_xy (w, x, y)
1876 struct window *w;
1877 int *x, *y;
1878 {
1879 if (w->pseudo_window_p)
1880 {
1881 /* A pseudo-window is always full-width, and starts at the
1882 left edge of the frame, plus a frame border. */
1883 struct frame *f = XFRAME (w->frame);
1884 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1885 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1886 }
1887 else
1888 {
1889 *x -= WINDOW_LEFT_EDGE_X (w);
1890 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1891 }
1892 }
1893
1894 /* EXPORT:
1895 Return in RECTS[] at most N clipping rectangles for glyph string S.
1896 Return the number of stored rectangles. */
1897
1898 int
1899 get_glyph_string_clip_rects (s, rects, n)
1900 struct glyph_string *s;
1901 NativeRectangle *rects;
1902 int n;
1903 {
1904 XRectangle r;
1905
1906 if (n <= 0)
1907 return 0;
1908
1909 if (s->row->full_width_p)
1910 {
1911 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1912 r.x = WINDOW_LEFT_EDGE_X (s->w);
1913 r.width = WINDOW_TOTAL_WIDTH (s->w);
1914
1915 /* Unless displaying a mode or menu bar line, which are always
1916 fully visible, clip to the visible part of the row. */
1917 if (s->w->pseudo_window_p)
1918 r.height = s->row->visible_height;
1919 else
1920 r.height = s->height;
1921 }
1922 else
1923 {
1924 /* This is a text line that may be partially visible. */
1925 r.x = window_box_left (s->w, s->area);
1926 r.width = window_box_width (s->w, s->area);
1927 r.height = s->row->visible_height;
1928 }
1929
1930 if (s->clip_head)
1931 if (r.x < s->clip_head->x)
1932 {
1933 if (r.width >= s->clip_head->x - r.x)
1934 r.width -= s->clip_head->x - r.x;
1935 else
1936 r.width = 0;
1937 r.x = s->clip_head->x;
1938 }
1939 if (s->clip_tail)
1940 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1941 {
1942 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1943 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1944 else
1945 r.width = 0;
1946 }
1947
1948 /* If S draws overlapping rows, it's sufficient to use the top and
1949 bottom of the window for clipping because this glyph string
1950 intentionally draws over other lines. */
1951 if (s->for_overlaps)
1952 {
1953 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1954 r.height = window_text_bottom_y (s->w) - r.y;
1955
1956 /* Alas, the above simple strategy does not work for the
1957 environments with anti-aliased text: if the same text is
1958 drawn onto the same place multiple times, it gets thicker.
1959 If the overlap we are processing is for the erased cursor, we
1960 take the intersection with the rectagle of the cursor. */
1961 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1962 {
1963 XRectangle rc, r_save = r;
1964
1965 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1966 rc.y = s->w->phys_cursor.y;
1967 rc.width = s->w->phys_cursor_width;
1968 rc.height = s->w->phys_cursor_height;
1969
1970 x_intersect_rectangles (&r_save, &rc, &r);
1971 }
1972 }
1973 else
1974 {
1975 /* Don't use S->y for clipping because it doesn't take partially
1976 visible lines into account. For example, it can be negative for
1977 partially visible lines at the top of a window. */
1978 if (!s->row->full_width_p
1979 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1980 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1981 else
1982 r.y = max (0, s->row->y);
1983
1984 /* If drawing a tool-bar window, draw it over the internal border
1985 at the top of the window. */
1986 if (WINDOWP (s->f->tool_bar_window)
1987 && s->w == XWINDOW (s->f->tool_bar_window))
1988 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1989 }
1990
1991 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1992
1993 /* If drawing the cursor, don't let glyph draw outside its
1994 advertised boundaries. Cleartype does this under some circumstances. */
1995 if (s->hl == DRAW_CURSOR)
1996 {
1997 struct glyph *glyph = s->first_glyph;
1998 int height, max_y;
1999
2000 if (s->x > r.x)
2001 {
2002 r.width -= s->x - r.x;
2003 r.x = s->x;
2004 }
2005 r.width = min (r.width, glyph->pixel_width);
2006
2007 /* If r.y is below window bottom, ensure that we still see a cursor. */
2008 height = min (glyph->ascent + glyph->descent,
2009 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2010 max_y = window_text_bottom_y (s->w) - height;
2011 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2012 if (s->ybase - glyph->ascent > max_y)
2013 {
2014 r.y = max_y;
2015 r.height = height;
2016 }
2017 else
2018 {
2019 /* Don't draw cursor glyph taller than our actual glyph. */
2020 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2021 if (height < r.height)
2022 {
2023 max_y = r.y + r.height;
2024 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2025 r.height = min (max_y - r.y, height);
2026 }
2027 }
2028 }
2029
2030 if (s->row->clip)
2031 {
2032 XRectangle r_save = r;
2033
2034 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2035 r.width = 0;
2036 }
2037
2038 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2039 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2040 {
2041 #ifdef CONVERT_FROM_XRECT
2042 CONVERT_FROM_XRECT (r, *rects);
2043 #else
2044 *rects = r;
2045 #endif
2046 return 1;
2047 }
2048 else
2049 {
2050 /* If we are processing overlapping and allowed to return
2051 multiple clipping rectangles, we exclude the row of the glyph
2052 string from the clipping rectangle. This is to avoid drawing
2053 the same text on the environment with anti-aliasing. */
2054 #ifdef CONVERT_FROM_XRECT
2055 XRectangle rs[2];
2056 #else
2057 XRectangle *rs = rects;
2058 #endif
2059 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2060
2061 if (s->for_overlaps & OVERLAPS_PRED)
2062 {
2063 rs[i] = r;
2064 if (r.y + r.height > row_y)
2065 {
2066 if (r.y < row_y)
2067 rs[i].height = row_y - r.y;
2068 else
2069 rs[i].height = 0;
2070 }
2071 i++;
2072 }
2073 if (s->for_overlaps & OVERLAPS_SUCC)
2074 {
2075 rs[i] = r;
2076 if (r.y < row_y + s->row->visible_height)
2077 {
2078 if (r.y + r.height > row_y + s->row->visible_height)
2079 {
2080 rs[i].y = row_y + s->row->visible_height;
2081 rs[i].height = r.y + r.height - rs[i].y;
2082 }
2083 else
2084 rs[i].height = 0;
2085 }
2086 i++;
2087 }
2088
2089 n = i;
2090 #ifdef CONVERT_FROM_XRECT
2091 for (i = 0; i < n; i++)
2092 CONVERT_FROM_XRECT (rs[i], rects[i]);
2093 #endif
2094 return n;
2095 }
2096 }
2097
2098 /* EXPORT:
2099 Return in *NR the clipping rectangle for glyph string S. */
2100
2101 void
2102 get_glyph_string_clip_rect (s, nr)
2103 struct glyph_string *s;
2104 NativeRectangle *nr;
2105 {
2106 get_glyph_string_clip_rects (s, nr, 1);
2107 }
2108
2109
2110 /* EXPORT:
2111 Return the position and height of the phys cursor in window W.
2112 Set w->phys_cursor_width to width of phys cursor.
2113 */
2114
2115 void
2116 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2117 struct window *w;
2118 struct glyph_row *row;
2119 struct glyph *glyph;
2120 int *xp, *yp, *heightp;
2121 {
2122 struct frame *f = XFRAME (WINDOW_FRAME (w));
2123 int x, y, wd, h, h0, y0;
2124
2125 /* Compute the width of the rectangle to draw. If on a stretch
2126 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2127 rectangle as wide as the glyph, but use a canonical character
2128 width instead. */
2129 wd = glyph->pixel_width - 1;
2130 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2131 wd++; /* Why? */
2132 #endif
2133
2134 x = w->phys_cursor.x;
2135 if (x < 0)
2136 {
2137 wd += x;
2138 x = 0;
2139 }
2140
2141 if (glyph->type == STRETCH_GLYPH
2142 && !x_stretch_cursor_p)
2143 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2144 w->phys_cursor_width = wd;
2145
2146 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2147
2148 /* If y is below window bottom, ensure that we still see a cursor. */
2149 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2150
2151 h = max (h0, glyph->ascent + glyph->descent);
2152 h0 = min (h0, glyph->ascent + glyph->descent);
2153
2154 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2155 if (y < y0)
2156 {
2157 h = max (h - (y0 - y) + 1, h0);
2158 y = y0 - 1;
2159 }
2160 else
2161 {
2162 y0 = window_text_bottom_y (w) - h0;
2163 if (y > y0)
2164 {
2165 h += y - y0;
2166 y = y0;
2167 }
2168 }
2169
2170 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2171 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2172 *heightp = h;
2173 }
2174
2175 /*
2176 * Remember which glyph the mouse is over.
2177 */
2178
2179 void
2180 remember_mouse_glyph (f, gx, gy, rect)
2181 struct frame *f;
2182 int gx, gy;
2183 NativeRectangle *rect;
2184 {
2185 Lisp_Object window;
2186 struct window *w;
2187 struct glyph_row *r, *gr, *end_row;
2188 enum window_part part;
2189 enum glyph_row_area area;
2190 int x, y, width, height;
2191
2192 /* Try to determine frame pixel position and size of the glyph under
2193 frame pixel coordinates X/Y on frame F. */
2194
2195 if (!f->glyphs_initialized_p
2196 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2197 NILP (window)))
2198 {
2199 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2200 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2201 goto virtual_glyph;
2202 }
2203
2204 w = XWINDOW (window);
2205 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2206 height = WINDOW_FRAME_LINE_HEIGHT (w);
2207
2208 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2209 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2210
2211 if (w->pseudo_window_p)
2212 {
2213 area = TEXT_AREA;
2214 part = ON_MODE_LINE; /* Don't adjust margin. */
2215 goto text_glyph;
2216 }
2217
2218 switch (part)
2219 {
2220 case ON_LEFT_MARGIN:
2221 area = LEFT_MARGIN_AREA;
2222 goto text_glyph;
2223
2224 case ON_RIGHT_MARGIN:
2225 area = RIGHT_MARGIN_AREA;
2226 goto text_glyph;
2227
2228 case ON_HEADER_LINE:
2229 case ON_MODE_LINE:
2230 gr = (part == ON_HEADER_LINE
2231 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2232 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2233 gy = gr->y;
2234 area = TEXT_AREA;
2235 goto text_glyph_row_found;
2236
2237 case ON_TEXT:
2238 area = TEXT_AREA;
2239
2240 text_glyph:
2241 gr = 0; gy = 0;
2242 for (; r <= end_row && r->enabled_p; ++r)
2243 if (r->y + r->height > y)
2244 {
2245 gr = r; gy = r->y;
2246 break;
2247 }
2248
2249 text_glyph_row_found:
2250 if (gr && gy <= y)
2251 {
2252 struct glyph *g = gr->glyphs[area];
2253 struct glyph *end = g + gr->used[area];
2254
2255 height = gr->height;
2256 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2257 if (gx + g->pixel_width > x)
2258 break;
2259
2260 if (g < end)
2261 {
2262 if (g->type == IMAGE_GLYPH)
2263 {
2264 /* Don't remember when mouse is over image, as
2265 image may have hot-spots. */
2266 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2267 return;
2268 }
2269 width = g->pixel_width;
2270 }
2271 else
2272 {
2273 /* Use nominal char spacing at end of line. */
2274 x -= gx;
2275 gx += (x / width) * width;
2276 }
2277
2278 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2279 gx += window_box_left_offset (w, area);
2280 }
2281 else
2282 {
2283 /* Use nominal line height at end of window. */
2284 gx = (x / width) * width;
2285 y -= gy;
2286 gy += (y / height) * height;
2287 }
2288 break;
2289
2290 case ON_LEFT_FRINGE:
2291 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2292 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2293 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2294 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2295 goto row_glyph;
2296
2297 case ON_RIGHT_FRINGE:
2298 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2299 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2300 : window_box_right_offset (w, TEXT_AREA));
2301 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2302 goto row_glyph;
2303
2304 case ON_SCROLL_BAR:
2305 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2306 ? 0
2307 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2308 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2309 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2310 : 0)));
2311 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2312
2313 row_glyph:
2314 gr = 0, gy = 0;
2315 for (; r <= end_row && r->enabled_p; ++r)
2316 if (r->y + r->height > y)
2317 {
2318 gr = r; gy = r->y;
2319 break;
2320 }
2321
2322 if (gr && gy <= y)
2323 height = gr->height;
2324 else
2325 {
2326 /* Use nominal line height at end of window. */
2327 y -= gy;
2328 gy += (y / height) * height;
2329 }
2330 break;
2331
2332 default:
2333 ;
2334 virtual_glyph:
2335 /* If there is no glyph under the mouse, then we divide the screen
2336 into a grid of the smallest glyph in the frame, and use that
2337 as our "glyph". */
2338
2339 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2340 round down even for negative values. */
2341 if (gx < 0)
2342 gx -= width - 1;
2343 if (gy < 0)
2344 gy -= height - 1;
2345
2346 gx = (gx / width) * width;
2347 gy = (gy / height) * height;
2348
2349 goto store_rect;
2350 }
2351
2352 gx += WINDOW_LEFT_EDGE_X (w);
2353 gy += WINDOW_TOP_EDGE_Y (w);
2354
2355 store_rect:
2356 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2357
2358 /* Visible feedback for debugging. */
2359 #if 0
2360 #if HAVE_X_WINDOWS
2361 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2362 f->output_data.x->normal_gc,
2363 gx, gy, width, height);
2364 #endif
2365 #endif
2366 }
2367
2368
2369 #endif /* HAVE_WINDOW_SYSTEM */
2370
2371 \f
2372 /***********************************************************************
2373 Lisp form evaluation
2374 ***********************************************************************/
2375
2376 /* Error handler for safe_eval and safe_call. */
2377
2378 static Lisp_Object
2379 safe_eval_handler (arg)
2380 Lisp_Object arg;
2381 {
2382 add_to_log ("Error during redisplay: %s", arg, Qnil);
2383 return Qnil;
2384 }
2385
2386
2387 /* Evaluate SEXPR and return the result, or nil if something went
2388 wrong. Prevent redisplay during the evaluation. */
2389
2390 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2391 Return the result, or nil if something went wrong. Prevent
2392 redisplay during the evaluation. */
2393
2394 Lisp_Object
2395 safe_call (nargs, args)
2396 int nargs;
2397 Lisp_Object *args;
2398 {
2399 Lisp_Object val;
2400
2401 if (inhibit_eval_during_redisplay)
2402 val = Qnil;
2403 else
2404 {
2405 int count = SPECPDL_INDEX ();
2406 struct gcpro gcpro1;
2407
2408 GCPRO1 (args[0]);
2409 gcpro1.nvars = nargs;
2410 specbind (Qinhibit_redisplay, Qt);
2411 /* Use Qt to ensure debugger does not run,
2412 so there is no possibility of wanting to redisplay. */
2413 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2414 safe_eval_handler);
2415 UNGCPRO;
2416 val = unbind_to (count, val);
2417 }
2418
2419 return val;
2420 }
2421
2422
2423 /* Call function FN with one argument ARG.
2424 Return the result, or nil if something went wrong. */
2425
2426 Lisp_Object
2427 safe_call1 (fn, arg)
2428 Lisp_Object fn, arg;
2429 {
2430 Lisp_Object args[2];
2431 args[0] = fn;
2432 args[1] = arg;
2433 return safe_call (2, args);
2434 }
2435
2436 static Lisp_Object Qeval;
2437
2438 Lisp_Object
2439 safe_eval (Lisp_Object sexpr)
2440 {
2441 return safe_call1 (Qeval, sexpr);
2442 }
2443
2444 /* Call function FN with one argument ARG.
2445 Return the result, or nil if something went wrong. */
2446
2447 Lisp_Object
2448 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2449 {
2450 Lisp_Object args[3];
2451 args[0] = fn;
2452 args[1] = arg1;
2453 args[2] = arg2;
2454 return safe_call (3, args);
2455 }
2456
2457
2458 \f
2459 /***********************************************************************
2460 Debugging
2461 ***********************************************************************/
2462
2463 #if 0
2464
2465 /* Define CHECK_IT to perform sanity checks on iterators.
2466 This is for debugging. It is too slow to do unconditionally. */
2467
2468 static void
2469 check_it (it)
2470 struct it *it;
2471 {
2472 if (it->method == GET_FROM_STRING)
2473 {
2474 xassert (STRINGP (it->string));
2475 xassert (IT_STRING_CHARPOS (*it) >= 0);
2476 }
2477 else
2478 {
2479 xassert (IT_STRING_CHARPOS (*it) < 0);
2480 if (it->method == GET_FROM_BUFFER)
2481 {
2482 /* Check that character and byte positions agree. */
2483 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2484 }
2485 }
2486
2487 if (it->dpvec)
2488 xassert (it->current.dpvec_index >= 0);
2489 else
2490 xassert (it->current.dpvec_index < 0);
2491 }
2492
2493 #define CHECK_IT(IT) check_it ((IT))
2494
2495 #else /* not 0 */
2496
2497 #define CHECK_IT(IT) (void) 0
2498
2499 #endif /* not 0 */
2500
2501
2502 #if GLYPH_DEBUG
2503
2504 /* Check that the window end of window W is what we expect it
2505 to be---the last row in the current matrix displaying text. */
2506
2507 static void
2508 check_window_end (w)
2509 struct window *w;
2510 {
2511 if (!MINI_WINDOW_P (w)
2512 && !NILP (w->window_end_valid))
2513 {
2514 struct glyph_row *row;
2515 xassert ((row = MATRIX_ROW (w->current_matrix,
2516 XFASTINT (w->window_end_vpos)),
2517 !row->enabled_p
2518 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2519 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2520 }
2521 }
2522
2523 #define CHECK_WINDOW_END(W) check_window_end ((W))
2524
2525 #else /* not GLYPH_DEBUG */
2526
2527 #define CHECK_WINDOW_END(W) (void) 0
2528
2529 #endif /* not GLYPH_DEBUG */
2530
2531
2532 \f
2533 /***********************************************************************
2534 Iterator initialization
2535 ***********************************************************************/
2536
2537 /* Initialize IT for displaying current_buffer in window W, starting
2538 at character position CHARPOS. CHARPOS < 0 means that no buffer
2539 position is specified which is useful when the iterator is assigned
2540 a position later. BYTEPOS is the byte position corresponding to
2541 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2542
2543 If ROW is not null, calls to produce_glyphs with IT as parameter
2544 will produce glyphs in that row.
2545
2546 BASE_FACE_ID is the id of a base face to use. It must be one of
2547 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2548 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2549 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2550
2551 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2552 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2553 will be initialized to use the corresponding mode line glyph row of
2554 the desired matrix of W. */
2555
2556 void
2557 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2558 struct it *it;
2559 struct window *w;
2560 int charpos, bytepos;
2561 struct glyph_row *row;
2562 enum face_id base_face_id;
2563 {
2564 int highlight_region_p;
2565 enum face_id remapped_base_face_id = base_face_id;
2566
2567 /* Some precondition checks. */
2568 xassert (w != NULL && it != NULL);
2569 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2570 && charpos <= ZV));
2571
2572 /* If face attributes have been changed since the last redisplay,
2573 free realized faces now because they depend on face definitions
2574 that might have changed. Don't free faces while there might be
2575 desired matrices pending which reference these faces. */
2576 if (face_change_count && !inhibit_free_realized_faces)
2577 {
2578 face_change_count = 0;
2579 free_all_realized_faces (Qnil);
2580 }
2581
2582 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2583 if (! NILP (Vface_remapping_alist))
2584 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2585
2586 /* Use one of the mode line rows of W's desired matrix if
2587 appropriate. */
2588 if (row == NULL)
2589 {
2590 if (base_face_id == MODE_LINE_FACE_ID
2591 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2592 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2593 else if (base_face_id == HEADER_LINE_FACE_ID)
2594 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2595 }
2596
2597 /* Clear IT. */
2598 bzero (it, sizeof *it);
2599 it->current.overlay_string_index = -1;
2600 it->current.dpvec_index = -1;
2601 it->base_face_id = remapped_base_face_id;
2602 it->string = Qnil;
2603 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2604
2605 /* The window in which we iterate over current_buffer: */
2606 XSETWINDOW (it->window, w);
2607 it->w = w;
2608 it->f = XFRAME (w->frame);
2609
2610 it->cmp_it.id = -1;
2611
2612 /* Extra space between lines (on window systems only). */
2613 if (base_face_id == DEFAULT_FACE_ID
2614 && FRAME_WINDOW_P (it->f))
2615 {
2616 if (NATNUMP (current_buffer->extra_line_spacing))
2617 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2618 else if (FLOATP (current_buffer->extra_line_spacing))
2619 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2620 * FRAME_LINE_HEIGHT (it->f));
2621 else if (it->f->extra_line_spacing > 0)
2622 it->extra_line_spacing = it->f->extra_line_spacing;
2623 it->max_extra_line_spacing = 0;
2624 }
2625
2626 /* If realized faces have been removed, e.g. because of face
2627 attribute changes of named faces, recompute them. When running
2628 in batch mode, the face cache of the initial frame is null. If
2629 we happen to get called, make a dummy face cache. */
2630 if (FRAME_FACE_CACHE (it->f) == NULL)
2631 init_frame_faces (it->f);
2632 if (FRAME_FACE_CACHE (it->f)->used == 0)
2633 recompute_basic_faces (it->f);
2634
2635 /* Current value of the `slice', `space-width', and 'height' properties. */
2636 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2637 it->space_width = Qnil;
2638 it->font_height = Qnil;
2639 it->override_ascent = -1;
2640
2641 /* Are control characters displayed as `^C'? */
2642 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2643
2644 /* -1 means everything between a CR and the following line end
2645 is invisible. >0 means lines indented more than this value are
2646 invisible. */
2647 it->selective = (INTEGERP (current_buffer->selective_display)
2648 ? XFASTINT (current_buffer->selective_display)
2649 : (!NILP (current_buffer->selective_display)
2650 ? -1 : 0));
2651 it->selective_display_ellipsis_p
2652 = !NILP (current_buffer->selective_display_ellipses);
2653
2654 /* Display table to use. */
2655 it->dp = window_display_table (w);
2656
2657 /* Are multibyte characters enabled in current_buffer? */
2658 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2659
2660 /* Non-zero if we should highlight the region. */
2661 highlight_region_p
2662 = (!NILP (Vtransient_mark_mode)
2663 && !NILP (current_buffer->mark_active)
2664 && XMARKER (current_buffer->mark)->buffer != 0);
2665
2666 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2667 start and end of a visible region in window IT->w. Set both to
2668 -1 to indicate no region. */
2669 if (highlight_region_p
2670 /* Maybe highlight only in selected window. */
2671 && (/* Either show region everywhere. */
2672 highlight_nonselected_windows
2673 /* Or show region in the selected window. */
2674 || w == XWINDOW (selected_window)
2675 /* Or show the region if we are in the mini-buffer and W is
2676 the window the mini-buffer refers to. */
2677 || (MINI_WINDOW_P (XWINDOW (selected_window))
2678 && WINDOWP (minibuf_selected_window)
2679 && w == XWINDOW (minibuf_selected_window))))
2680 {
2681 int charpos = marker_position (current_buffer->mark);
2682 it->region_beg_charpos = min (PT, charpos);
2683 it->region_end_charpos = max (PT, charpos);
2684 }
2685 else
2686 it->region_beg_charpos = it->region_end_charpos = -1;
2687
2688 /* Get the position at which the redisplay_end_trigger hook should
2689 be run, if it is to be run at all. */
2690 if (MARKERP (w->redisplay_end_trigger)
2691 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2692 it->redisplay_end_trigger_charpos
2693 = marker_position (w->redisplay_end_trigger);
2694 else if (INTEGERP (w->redisplay_end_trigger))
2695 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2696
2697 /* Correct bogus values of tab_width. */
2698 it->tab_width = XINT (current_buffer->tab_width);
2699 if (it->tab_width <= 0 || it->tab_width > 1000)
2700 it->tab_width = 8;
2701
2702 /* Are lines in the display truncated? */
2703 if (base_face_id != DEFAULT_FACE_ID
2704 || XINT (it->w->hscroll)
2705 || (! WINDOW_FULL_WIDTH_P (it->w)
2706 && ((!NILP (Vtruncate_partial_width_windows)
2707 && !INTEGERP (Vtruncate_partial_width_windows))
2708 || (INTEGERP (Vtruncate_partial_width_windows)
2709 && (WINDOW_TOTAL_COLS (it->w)
2710 < XINT (Vtruncate_partial_width_windows))))))
2711 it->line_wrap = TRUNCATE;
2712 else if (NILP (current_buffer->truncate_lines))
2713 it->line_wrap = NILP (current_buffer->word_wrap)
2714 ? WINDOW_WRAP : WORD_WRAP;
2715 else
2716 it->line_wrap = TRUNCATE;
2717
2718 /* Get dimensions of truncation and continuation glyphs. These are
2719 displayed as fringe bitmaps under X, so we don't need them for such
2720 frames. */
2721 if (!FRAME_WINDOW_P (it->f))
2722 {
2723 if (it->line_wrap == TRUNCATE)
2724 {
2725 /* We will need the truncation glyph. */
2726 xassert (it->glyph_row == NULL);
2727 produce_special_glyphs (it, IT_TRUNCATION);
2728 it->truncation_pixel_width = it->pixel_width;
2729 }
2730 else
2731 {
2732 /* We will need the continuation glyph. */
2733 xassert (it->glyph_row == NULL);
2734 produce_special_glyphs (it, IT_CONTINUATION);
2735 it->continuation_pixel_width = it->pixel_width;
2736 }
2737
2738 /* Reset these values to zero because the produce_special_glyphs
2739 above has changed them. */
2740 it->pixel_width = it->ascent = it->descent = 0;
2741 it->phys_ascent = it->phys_descent = 0;
2742 }
2743
2744 /* Set this after getting the dimensions of truncation and
2745 continuation glyphs, so that we don't produce glyphs when calling
2746 produce_special_glyphs, above. */
2747 it->glyph_row = row;
2748 it->area = TEXT_AREA;
2749
2750 /* Get the dimensions of the display area. The display area
2751 consists of the visible window area plus a horizontally scrolled
2752 part to the left of the window. All x-values are relative to the
2753 start of this total display area. */
2754 if (base_face_id != DEFAULT_FACE_ID)
2755 {
2756 /* Mode lines, menu bar in terminal frames. */
2757 it->first_visible_x = 0;
2758 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2759 }
2760 else
2761 {
2762 it->first_visible_x
2763 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2764 it->last_visible_x = (it->first_visible_x
2765 + window_box_width (w, TEXT_AREA));
2766
2767 /* If we truncate lines, leave room for the truncator glyph(s) at
2768 the right margin. Otherwise, leave room for the continuation
2769 glyph(s). Truncation and continuation glyphs are not inserted
2770 for window-based redisplay. */
2771 if (!FRAME_WINDOW_P (it->f))
2772 {
2773 if (it->line_wrap == TRUNCATE)
2774 it->last_visible_x -= it->truncation_pixel_width;
2775 else
2776 it->last_visible_x -= it->continuation_pixel_width;
2777 }
2778
2779 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2780 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2781 }
2782
2783 /* Leave room for a border glyph. */
2784 if (!FRAME_WINDOW_P (it->f)
2785 && !WINDOW_RIGHTMOST_P (it->w))
2786 it->last_visible_x -= 1;
2787
2788 it->last_visible_y = window_text_bottom_y (w);
2789
2790 /* For mode lines and alike, arrange for the first glyph having a
2791 left box line if the face specifies a box. */
2792 if (base_face_id != DEFAULT_FACE_ID)
2793 {
2794 struct face *face;
2795
2796 it->face_id = remapped_base_face_id;
2797
2798 /* If we have a boxed mode line, make the first character appear
2799 with a left box line. */
2800 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2801 if (face->box != FACE_NO_BOX)
2802 it->start_of_box_run_p = 1;
2803 }
2804
2805 /* If a buffer position was specified, set the iterator there,
2806 getting overlays and face properties from that position. */
2807 if (charpos >= BUF_BEG (current_buffer))
2808 {
2809 it->end_charpos = ZV;
2810 it->face_id = -1;
2811 IT_CHARPOS (*it) = charpos;
2812
2813 /* Compute byte position if not specified. */
2814 if (bytepos < charpos)
2815 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2816 else
2817 IT_BYTEPOS (*it) = bytepos;
2818
2819 it->start = it->current;
2820
2821 /* Compute faces etc. */
2822 reseat (it, it->current.pos, 1);
2823 }
2824
2825 CHECK_IT (it);
2826 }
2827
2828
2829 /* Initialize IT for the display of window W with window start POS. */
2830
2831 void
2832 start_display (it, w, pos)
2833 struct it *it;
2834 struct window *w;
2835 struct text_pos pos;
2836 {
2837 struct glyph_row *row;
2838 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2839
2840 row = w->desired_matrix->rows + first_vpos;
2841 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2842 it->first_vpos = first_vpos;
2843
2844 /* Don't reseat to previous visible line start if current start
2845 position is in a string or image. */
2846 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2847 {
2848 int start_at_line_beg_p;
2849 int first_y = it->current_y;
2850
2851 /* If window start is not at a line start, skip forward to POS to
2852 get the correct continuation lines width. */
2853 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2854 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2855 if (!start_at_line_beg_p)
2856 {
2857 int new_x;
2858
2859 reseat_at_previous_visible_line_start (it);
2860 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2861
2862 new_x = it->current_x + it->pixel_width;
2863
2864 /* If lines are continued, this line may end in the middle
2865 of a multi-glyph character (e.g. a control character
2866 displayed as \003, or in the middle of an overlay
2867 string). In this case move_it_to above will not have
2868 taken us to the start of the continuation line but to the
2869 end of the continued line. */
2870 if (it->current_x > 0
2871 && it->line_wrap != TRUNCATE /* Lines are continued. */
2872 && (/* And glyph doesn't fit on the line. */
2873 new_x > it->last_visible_x
2874 /* Or it fits exactly and we're on a window
2875 system frame. */
2876 || (new_x == it->last_visible_x
2877 && FRAME_WINDOW_P (it->f))))
2878 {
2879 if (it->current.dpvec_index >= 0
2880 || it->current.overlay_string_index >= 0)
2881 {
2882 set_iterator_to_next (it, 1);
2883 move_it_in_display_line_to (it, -1, -1, 0);
2884 }
2885
2886 it->continuation_lines_width += it->current_x;
2887 }
2888
2889 /* We're starting a new display line, not affected by the
2890 height of the continued line, so clear the appropriate
2891 fields in the iterator structure. */
2892 it->max_ascent = it->max_descent = 0;
2893 it->max_phys_ascent = it->max_phys_descent = 0;
2894
2895 it->current_y = first_y;
2896 it->vpos = 0;
2897 it->current_x = it->hpos = 0;
2898 }
2899 }
2900 }
2901
2902
2903 /* Return 1 if POS is a position in ellipses displayed for invisible
2904 text. W is the window we display, for text property lookup. */
2905
2906 static int
2907 in_ellipses_for_invisible_text_p (pos, w)
2908 struct display_pos *pos;
2909 struct window *w;
2910 {
2911 Lisp_Object prop, window;
2912 int ellipses_p = 0;
2913 int charpos = CHARPOS (pos->pos);
2914
2915 /* If POS specifies a position in a display vector, this might
2916 be for an ellipsis displayed for invisible text. We won't
2917 get the iterator set up for delivering that ellipsis unless
2918 we make sure that it gets aware of the invisible text. */
2919 if (pos->dpvec_index >= 0
2920 && pos->overlay_string_index < 0
2921 && CHARPOS (pos->string_pos) < 0
2922 && charpos > BEGV
2923 && (XSETWINDOW (window, w),
2924 prop = Fget_char_property (make_number (charpos),
2925 Qinvisible, window),
2926 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2927 {
2928 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2929 window);
2930 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2931 }
2932
2933 return ellipses_p;
2934 }
2935
2936
2937 /* Initialize IT for stepping through current_buffer in window W,
2938 starting at position POS that includes overlay string and display
2939 vector/ control character translation position information. Value
2940 is zero if there are overlay strings with newlines at POS. */
2941
2942 static int
2943 init_from_display_pos (it, w, pos)
2944 struct it *it;
2945 struct window *w;
2946 struct display_pos *pos;
2947 {
2948 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2949 int i, overlay_strings_with_newlines = 0;
2950
2951 /* If POS specifies a position in a display vector, this might
2952 be for an ellipsis displayed for invisible text. We won't
2953 get the iterator set up for delivering that ellipsis unless
2954 we make sure that it gets aware of the invisible text. */
2955 if (in_ellipses_for_invisible_text_p (pos, w))
2956 {
2957 --charpos;
2958 bytepos = 0;
2959 }
2960
2961 /* Keep in mind: the call to reseat in init_iterator skips invisible
2962 text, so we might end up at a position different from POS. This
2963 is only a problem when POS is a row start after a newline and an
2964 overlay starts there with an after-string, and the overlay has an
2965 invisible property. Since we don't skip invisible text in
2966 display_line and elsewhere immediately after consuming the
2967 newline before the row start, such a POS will not be in a string,
2968 but the call to init_iterator below will move us to the
2969 after-string. */
2970 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2971
2972 /* This only scans the current chunk -- it should scan all chunks.
2973 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2974 to 16 in 22.1 to make this a lesser problem. */
2975 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2976 {
2977 const char *s = SDATA (it->overlay_strings[i]);
2978 const char *e = s + SBYTES (it->overlay_strings[i]);
2979
2980 while (s < e && *s != '\n')
2981 ++s;
2982
2983 if (s < e)
2984 {
2985 overlay_strings_with_newlines = 1;
2986 break;
2987 }
2988 }
2989
2990 /* If position is within an overlay string, set up IT to the right
2991 overlay string. */
2992 if (pos->overlay_string_index >= 0)
2993 {
2994 int relative_index;
2995
2996 /* If the first overlay string happens to have a `display'
2997 property for an image, the iterator will be set up for that
2998 image, and we have to undo that setup first before we can
2999 correct the overlay string index. */
3000 if (it->method == GET_FROM_IMAGE)
3001 pop_it (it);
3002
3003 /* We already have the first chunk of overlay strings in
3004 IT->overlay_strings. Load more until the one for
3005 pos->overlay_string_index is in IT->overlay_strings. */
3006 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3007 {
3008 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3009 it->current.overlay_string_index = 0;
3010 while (n--)
3011 {
3012 load_overlay_strings (it, 0);
3013 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3014 }
3015 }
3016
3017 it->current.overlay_string_index = pos->overlay_string_index;
3018 relative_index = (it->current.overlay_string_index
3019 % OVERLAY_STRING_CHUNK_SIZE);
3020 it->string = it->overlay_strings[relative_index];
3021 xassert (STRINGP (it->string));
3022 it->current.string_pos = pos->string_pos;
3023 it->method = GET_FROM_STRING;
3024 }
3025
3026 if (CHARPOS (pos->string_pos) >= 0)
3027 {
3028 /* Recorded position is not in an overlay string, but in another
3029 string. This can only be a string from a `display' property.
3030 IT should already be filled with that string. */
3031 it->current.string_pos = pos->string_pos;
3032 xassert (STRINGP (it->string));
3033 }
3034
3035 /* Restore position in display vector translations, control
3036 character translations or ellipses. */
3037 if (pos->dpvec_index >= 0)
3038 {
3039 if (it->dpvec == NULL)
3040 get_next_display_element (it);
3041 xassert (it->dpvec && it->current.dpvec_index == 0);
3042 it->current.dpvec_index = pos->dpvec_index;
3043 }
3044
3045 CHECK_IT (it);
3046 return !overlay_strings_with_newlines;
3047 }
3048
3049
3050 /* Initialize IT for stepping through current_buffer in window W
3051 starting at ROW->start. */
3052
3053 static void
3054 init_to_row_start (it, w, row)
3055 struct it *it;
3056 struct window *w;
3057 struct glyph_row *row;
3058 {
3059 init_from_display_pos (it, w, &row->start);
3060 it->start = row->start;
3061 it->continuation_lines_width = row->continuation_lines_width;
3062 CHECK_IT (it);
3063 }
3064
3065
3066 /* Initialize IT for stepping through current_buffer in window W
3067 starting in the line following ROW, i.e. starting at ROW->end.
3068 Value is zero if there are overlay strings with newlines at ROW's
3069 end position. */
3070
3071 static int
3072 init_to_row_end (it, w, row)
3073 struct it *it;
3074 struct window *w;
3075 struct glyph_row *row;
3076 {
3077 int success = 0;
3078
3079 if (init_from_display_pos (it, w, &row->end))
3080 {
3081 if (row->continued_p)
3082 it->continuation_lines_width
3083 = row->continuation_lines_width + row->pixel_width;
3084 CHECK_IT (it);
3085 success = 1;
3086 }
3087
3088 return success;
3089 }
3090
3091
3092
3093 \f
3094 /***********************************************************************
3095 Text properties
3096 ***********************************************************************/
3097
3098 /* Called when IT reaches IT->stop_charpos. Handle text property and
3099 overlay changes. Set IT->stop_charpos to the next position where
3100 to stop. */
3101
3102 static void
3103 handle_stop (it)
3104 struct it *it;
3105 {
3106 enum prop_handled handled;
3107 int handle_overlay_change_p;
3108 struct props *p;
3109
3110 it->dpvec = NULL;
3111 it->current.dpvec_index = -1;
3112 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3113 it->ignore_overlay_strings_at_pos_p = 0;
3114 it->ellipsis_p = 0;
3115
3116 /* Use face of preceding text for ellipsis (if invisible) */
3117 if (it->selective_display_ellipsis_p)
3118 it->saved_face_id = it->face_id;
3119
3120 do
3121 {
3122 handled = HANDLED_NORMALLY;
3123
3124 /* Call text property handlers. */
3125 for (p = it_props; p->handler; ++p)
3126 {
3127 handled = p->handler (it);
3128
3129 if (handled == HANDLED_RECOMPUTE_PROPS)
3130 break;
3131 else if (handled == HANDLED_RETURN)
3132 {
3133 /* We still want to show before and after strings from
3134 overlays even if the actual buffer text is replaced. */
3135 if (!handle_overlay_change_p
3136 || it->sp > 1
3137 || !get_overlay_strings_1 (it, 0, 0))
3138 {
3139 if (it->ellipsis_p)
3140 setup_for_ellipsis (it, 0);
3141 /* When handling a display spec, we might load an
3142 empty string. In that case, discard it here. We
3143 used to discard it in handle_single_display_spec,
3144 but that causes get_overlay_strings_1, above, to
3145 ignore overlay strings that we must check. */
3146 if (STRINGP (it->string) && !SCHARS (it->string))
3147 pop_it (it);
3148 return;
3149 }
3150 else if (STRINGP (it->string) && !SCHARS (it->string))
3151 pop_it (it);
3152 else
3153 {
3154 it->ignore_overlay_strings_at_pos_p = 1;
3155 it->string_from_display_prop_p = 0;
3156 handle_overlay_change_p = 0;
3157 }
3158 handled = HANDLED_RECOMPUTE_PROPS;
3159 break;
3160 }
3161 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3162 handle_overlay_change_p = 0;
3163 }
3164
3165 if (handled != HANDLED_RECOMPUTE_PROPS)
3166 {
3167 /* Don't check for overlay strings below when set to deliver
3168 characters from a display vector. */
3169 if (it->method == GET_FROM_DISPLAY_VECTOR)
3170 handle_overlay_change_p = 0;
3171
3172 /* Handle overlay changes.
3173 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3174 if it finds overlays. */
3175 if (handle_overlay_change_p)
3176 handled = handle_overlay_change (it);
3177 }
3178
3179 if (it->ellipsis_p)
3180 {
3181 setup_for_ellipsis (it, 0);
3182 break;
3183 }
3184 }
3185 while (handled == HANDLED_RECOMPUTE_PROPS);
3186
3187 /* Determine where to stop next. */
3188 if (handled == HANDLED_NORMALLY)
3189 compute_stop_pos (it);
3190 }
3191
3192
3193 /* Compute IT->stop_charpos from text property and overlay change
3194 information for IT's current position. */
3195
3196 static void
3197 compute_stop_pos (it)
3198 struct it *it;
3199 {
3200 register INTERVAL iv, next_iv;
3201 Lisp_Object object, limit, position;
3202 EMACS_INT charpos, bytepos;
3203
3204 /* If nowhere else, stop at the end. */
3205 it->stop_charpos = it->end_charpos;
3206
3207 if (STRINGP (it->string))
3208 {
3209 /* Strings are usually short, so don't limit the search for
3210 properties. */
3211 object = it->string;
3212 limit = Qnil;
3213 charpos = IT_STRING_CHARPOS (*it);
3214 bytepos = IT_STRING_BYTEPOS (*it);
3215 }
3216 else
3217 {
3218 EMACS_INT pos;
3219
3220 /* If next overlay change is in front of the current stop pos
3221 (which is IT->end_charpos), stop there. Note: value of
3222 next_overlay_change is point-max if no overlay change
3223 follows. */
3224 charpos = IT_CHARPOS (*it);
3225 bytepos = IT_BYTEPOS (*it);
3226 pos = next_overlay_change (charpos);
3227 if (pos < it->stop_charpos)
3228 it->stop_charpos = pos;
3229
3230 /* If showing the region, we have to stop at the region
3231 start or end because the face might change there. */
3232 if (it->region_beg_charpos > 0)
3233 {
3234 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3235 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3236 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3237 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3238 }
3239
3240 /* Set up variables for computing the stop position from text
3241 property changes. */
3242 XSETBUFFER (object, current_buffer);
3243 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3244 }
3245
3246 /* Get the interval containing IT's position. Value is a null
3247 interval if there isn't such an interval. */
3248 position = make_number (charpos);
3249 iv = validate_interval_range (object, &position, &position, 0);
3250 if (!NULL_INTERVAL_P (iv))
3251 {
3252 Lisp_Object values_here[LAST_PROP_IDX];
3253 struct props *p;
3254
3255 /* Get properties here. */
3256 for (p = it_props; p->handler; ++p)
3257 values_here[p->idx] = textget (iv->plist, *p->name);
3258
3259 /* Look for an interval following iv that has different
3260 properties. */
3261 for (next_iv = next_interval (iv);
3262 (!NULL_INTERVAL_P (next_iv)
3263 && (NILP (limit)
3264 || XFASTINT (limit) > next_iv->position));
3265 next_iv = next_interval (next_iv))
3266 {
3267 for (p = it_props; p->handler; ++p)
3268 {
3269 Lisp_Object new_value;
3270
3271 new_value = textget (next_iv->plist, *p->name);
3272 if (!EQ (values_here[p->idx], new_value))
3273 break;
3274 }
3275
3276 if (p->handler)
3277 break;
3278 }
3279
3280 if (!NULL_INTERVAL_P (next_iv))
3281 {
3282 if (INTEGERP (limit)
3283 && next_iv->position >= XFASTINT (limit))
3284 /* No text property change up to limit. */
3285 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3286 else
3287 /* Text properties change in next_iv. */
3288 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3289 }
3290 }
3291
3292 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3293 it->stop_charpos, it->string);
3294
3295 xassert (STRINGP (it->string)
3296 || (it->stop_charpos >= BEGV
3297 && it->stop_charpos >= IT_CHARPOS (*it)));
3298 }
3299
3300
3301 /* Return the position of the next overlay change after POS in
3302 current_buffer. Value is point-max if no overlay change
3303 follows. This is like `next-overlay-change' but doesn't use
3304 xmalloc. */
3305
3306 static EMACS_INT
3307 next_overlay_change (pos)
3308 EMACS_INT pos;
3309 {
3310 int noverlays;
3311 EMACS_INT endpos;
3312 Lisp_Object *overlays;
3313 int i;
3314
3315 /* Get all overlays at the given position. */
3316 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3317
3318 /* If any of these overlays ends before endpos,
3319 use its ending point instead. */
3320 for (i = 0; i < noverlays; ++i)
3321 {
3322 Lisp_Object oend;
3323 EMACS_INT oendpos;
3324
3325 oend = OVERLAY_END (overlays[i]);
3326 oendpos = OVERLAY_POSITION (oend);
3327 endpos = min (endpos, oendpos);
3328 }
3329
3330 return endpos;
3331 }
3332
3333
3334 \f
3335 /***********************************************************************
3336 Fontification
3337 ***********************************************************************/
3338
3339 /* Handle changes in the `fontified' property of the current buffer by
3340 calling hook functions from Qfontification_functions to fontify
3341 regions of text. */
3342
3343 static enum prop_handled
3344 handle_fontified_prop (it)
3345 struct it *it;
3346 {
3347 Lisp_Object prop, pos;
3348 enum prop_handled handled = HANDLED_NORMALLY;
3349
3350 if (!NILP (Vmemory_full))
3351 return handled;
3352
3353 /* Get the value of the `fontified' property at IT's current buffer
3354 position. (The `fontified' property doesn't have a special
3355 meaning in strings.) If the value is nil, call functions from
3356 Qfontification_functions. */
3357 if (!STRINGP (it->string)
3358 && it->s == NULL
3359 && !NILP (Vfontification_functions)
3360 && !NILP (Vrun_hooks)
3361 && (pos = make_number (IT_CHARPOS (*it)),
3362 prop = Fget_char_property (pos, Qfontified, Qnil),
3363 /* Ignore the special cased nil value always present at EOB since
3364 no amount of fontifying will be able to change it. */
3365 NILP (prop) && IT_CHARPOS (*it) < Z))
3366 {
3367 int count = SPECPDL_INDEX ();
3368 Lisp_Object val;
3369
3370 val = Vfontification_functions;
3371 specbind (Qfontification_functions, Qnil);
3372
3373 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3374 safe_call1 (val, pos);
3375 else
3376 {
3377 Lisp_Object globals, fn;
3378 struct gcpro gcpro1, gcpro2;
3379
3380 globals = Qnil;
3381 GCPRO2 (val, globals);
3382
3383 for (; CONSP (val); val = XCDR (val))
3384 {
3385 fn = XCAR (val);
3386
3387 if (EQ (fn, Qt))
3388 {
3389 /* A value of t indicates this hook has a local
3390 binding; it means to run the global binding too.
3391 In a global value, t should not occur. If it
3392 does, we must ignore it to avoid an endless
3393 loop. */
3394 for (globals = Fdefault_value (Qfontification_functions);
3395 CONSP (globals);
3396 globals = XCDR (globals))
3397 {
3398 fn = XCAR (globals);
3399 if (!EQ (fn, Qt))
3400 safe_call1 (fn, pos);
3401 }
3402 }
3403 else
3404 safe_call1 (fn, pos);
3405 }
3406
3407 UNGCPRO;
3408 }
3409
3410 unbind_to (count, Qnil);
3411
3412 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3413 something. This avoids an endless loop if they failed to
3414 fontify the text for which reason ever. */
3415 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3416 handled = HANDLED_RECOMPUTE_PROPS;
3417 }
3418
3419 return handled;
3420 }
3421
3422
3423 \f
3424 /***********************************************************************
3425 Faces
3426 ***********************************************************************/
3427
3428 /* Set up iterator IT from face properties at its current position.
3429 Called from handle_stop. */
3430
3431 static enum prop_handled
3432 handle_face_prop (it)
3433 struct it *it;
3434 {
3435 int new_face_id;
3436 EMACS_INT next_stop;
3437
3438 if (!STRINGP (it->string))
3439 {
3440 new_face_id
3441 = face_at_buffer_position (it->w,
3442 IT_CHARPOS (*it),
3443 it->region_beg_charpos,
3444 it->region_end_charpos,
3445 &next_stop,
3446 (IT_CHARPOS (*it)
3447 + TEXT_PROP_DISTANCE_LIMIT),
3448 0, it->base_face_id);
3449
3450 /* Is this a start of a run of characters with box face?
3451 Caveat: this can be called for a freshly initialized
3452 iterator; face_id is -1 in this case. We know that the new
3453 face will not change until limit, i.e. if the new face has a
3454 box, all characters up to limit will have one. But, as
3455 usual, we don't know whether limit is really the end. */
3456 if (new_face_id != it->face_id)
3457 {
3458 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3459
3460 /* If new face has a box but old face has not, this is
3461 the start of a run of characters with box, i.e. it has
3462 a shadow on the left side. The value of face_id of the
3463 iterator will be -1 if this is the initial call that gets
3464 the face. In this case, we have to look in front of IT's
3465 position and see whether there is a face != new_face_id. */
3466 it->start_of_box_run_p
3467 = (new_face->box != FACE_NO_BOX
3468 && (it->face_id >= 0
3469 || IT_CHARPOS (*it) == BEG
3470 || new_face_id != face_before_it_pos (it)));
3471 it->face_box_p = new_face->box != FACE_NO_BOX;
3472 }
3473 }
3474 else
3475 {
3476 int base_face_id, bufpos;
3477 int i;
3478 Lisp_Object from_overlay
3479 = (it->current.overlay_string_index >= 0
3480 ? it->string_overlays[it->current.overlay_string_index]
3481 : Qnil);
3482
3483 /* See if we got to this string directly or indirectly from
3484 an overlay property. That includes the before-string or
3485 after-string of an overlay, strings in display properties
3486 provided by an overlay, their text properties, etc.
3487
3488 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3489 if (! NILP (from_overlay))
3490 for (i = it->sp - 1; i >= 0; i--)
3491 {
3492 if (it->stack[i].current.overlay_string_index >= 0)
3493 from_overlay
3494 = it->string_overlays[it->stack[i].current.overlay_string_index];
3495 else if (! NILP (it->stack[i].from_overlay))
3496 from_overlay = it->stack[i].from_overlay;
3497
3498 if (!NILP (from_overlay))
3499 break;
3500 }
3501
3502 if (! NILP (from_overlay))
3503 {
3504 bufpos = IT_CHARPOS (*it);
3505 /* For a string from an overlay, the base face depends
3506 only on text properties and ignores overlays. */
3507 base_face_id
3508 = face_for_overlay_string (it->w,
3509 IT_CHARPOS (*it),
3510 it->region_beg_charpos,
3511 it->region_end_charpos,
3512 &next_stop,
3513 (IT_CHARPOS (*it)
3514 + TEXT_PROP_DISTANCE_LIMIT),
3515 0,
3516 from_overlay);
3517 }
3518 else
3519 {
3520 bufpos = 0;
3521
3522 /* For strings from a `display' property, use the face at
3523 IT's current buffer position as the base face to merge
3524 with, so that overlay strings appear in the same face as
3525 surrounding text, unless they specify their own
3526 faces. */
3527 base_face_id = underlying_face_id (it);
3528 }
3529
3530 new_face_id = face_at_string_position (it->w,
3531 it->string,
3532 IT_STRING_CHARPOS (*it),
3533 bufpos,
3534 it->region_beg_charpos,
3535 it->region_end_charpos,
3536 &next_stop,
3537 base_face_id, 0);
3538
3539 /* Is this a start of a run of characters with box? Caveat:
3540 this can be called for a freshly allocated iterator; face_id
3541 is -1 is this case. We know that the new face will not
3542 change until the next check pos, i.e. if the new face has a
3543 box, all characters up to that position will have a
3544 box. But, as usual, we don't know whether that position
3545 is really the end. */
3546 if (new_face_id != it->face_id)
3547 {
3548 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3549 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3550
3551 /* If new face has a box but old face hasn't, this is the
3552 start of a run of characters with box, i.e. it has a
3553 shadow on the left side. */
3554 it->start_of_box_run_p
3555 = new_face->box && (old_face == NULL || !old_face->box);
3556 it->face_box_p = new_face->box != FACE_NO_BOX;
3557 }
3558 }
3559
3560 it->face_id = new_face_id;
3561 return HANDLED_NORMALLY;
3562 }
3563
3564
3565 /* Return the ID of the face ``underlying'' IT's current position,
3566 which is in a string. If the iterator is associated with a
3567 buffer, return the face at IT's current buffer position.
3568 Otherwise, use the iterator's base_face_id. */
3569
3570 static int
3571 underlying_face_id (it)
3572 struct it *it;
3573 {
3574 int face_id = it->base_face_id, i;
3575
3576 xassert (STRINGP (it->string));
3577
3578 for (i = it->sp - 1; i >= 0; --i)
3579 if (NILP (it->stack[i].string))
3580 face_id = it->stack[i].face_id;
3581
3582 return face_id;
3583 }
3584
3585
3586 /* Compute the face one character before or after the current position
3587 of IT. BEFORE_P non-zero means get the face in front of IT's
3588 position. Value is the id of the face. */
3589
3590 static int
3591 face_before_or_after_it_pos (it, before_p)
3592 struct it *it;
3593 int before_p;
3594 {
3595 int face_id, limit;
3596 EMACS_INT next_check_charpos;
3597 struct text_pos pos;
3598
3599 xassert (it->s == NULL);
3600
3601 if (STRINGP (it->string))
3602 {
3603 int bufpos, base_face_id;
3604
3605 /* No face change past the end of the string (for the case
3606 we are padding with spaces). No face change before the
3607 string start. */
3608 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3609 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3610 return it->face_id;
3611
3612 /* Set pos to the position before or after IT's current position. */
3613 if (before_p)
3614 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3615 else
3616 /* For composition, we must check the character after the
3617 composition. */
3618 pos = (it->what == IT_COMPOSITION
3619 ? string_pos (IT_STRING_CHARPOS (*it)
3620 + it->cmp_it.nchars, it->string)
3621 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3622
3623 if (it->current.overlay_string_index >= 0)
3624 bufpos = IT_CHARPOS (*it);
3625 else
3626 bufpos = 0;
3627
3628 base_face_id = underlying_face_id (it);
3629
3630 /* Get the face for ASCII, or unibyte. */
3631 face_id = face_at_string_position (it->w,
3632 it->string,
3633 CHARPOS (pos),
3634 bufpos,
3635 it->region_beg_charpos,
3636 it->region_end_charpos,
3637 &next_check_charpos,
3638 base_face_id, 0);
3639
3640 /* Correct the face for charsets different from ASCII. Do it
3641 for the multibyte case only. The face returned above is
3642 suitable for unibyte text if IT->string is unibyte. */
3643 if (STRING_MULTIBYTE (it->string))
3644 {
3645 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3646 int rest = SBYTES (it->string) - BYTEPOS (pos);
3647 int c, len;
3648 struct face *face = FACE_FROM_ID (it->f, face_id);
3649
3650 c = string_char_and_length (p, rest, &len);
3651 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3652 }
3653 }
3654 else
3655 {
3656 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3657 || (IT_CHARPOS (*it) <= BEGV && before_p))
3658 return it->face_id;
3659
3660 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3661 pos = it->current.pos;
3662
3663 if (before_p)
3664 DEC_TEXT_POS (pos, it->multibyte_p);
3665 else
3666 {
3667 if (it->what == IT_COMPOSITION)
3668 /* For composition, we must check the position after the
3669 composition. */
3670 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3671 else
3672 INC_TEXT_POS (pos, it->multibyte_p);
3673 }
3674
3675 /* Determine face for CHARSET_ASCII, or unibyte. */
3676 face_id = face_at_buffer_position (it->w,
3677 CHARPOS (pos),
3678 it->region_beg_charpos,
3679 it->region_end_charpos,
3680 &next_check_charpos,
3681 limit, 0, -1);
3682
3683 /* Correct the face for charsets different from ASCII. Do it
3684 for the multibyte case only. The face returned above is
3685 suitable for unibyte text if current_buffer is unibyte. */
3686 if (it->multibyte_p)
3687 {
3688 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3689 struct face *face = FACE_FROM_ID (it->f, face_id);
3690 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3691 }
3692 }
3693
3694 return face_id;
3695 }
3696
3697
3698 \f
3699 /***********************************************************************
3700 Invisible text
3701 ***********************************************************************/
3702
3703 /* Set up iterator IT from invisible properties at its current
3704 position. Called from handle_stop. */
3705
3706 static enum prop_handled
3707 handle_invisible_prop (it)
3708 struct it *it;
3709 {
3710 enum prop_handled handled = HANDLED_NORMALLY;
3711
3712 if (STRINGP (it->string))
3713 {
3714 extern Lisp_Object Qinvisible;
3715 Lisp_Object prop, end_charpos, limit, charpos;
3716
3717 /* Get the value of the invisible text property at the
3718 current position. Value will be nil if there is no such
3719 property. */
3720 charpos = make_number (IT_STRING_CHARPOS (*it));
3721 prop = Fget_text_property (charpos, Qinvisible, it->string);
3722
3723 if (!NILP (prop)
3724 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3725 {
3726 handled = HANDLED_RECOMPUTE_PROPS;
3727
3728 /* Get the position at which the next change of the
3729 invisible text property can be found in IT->string.
3730 Value will be nil if the property value is the same for
3731 all the rest of IT->string. */
3732 XSETINT (limit, SCHARS (it->string));
3733 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3734 it->string, limit);
3735
3736 /* Text at current position is invisible. The next
3737 change in the property is at position end_charpos.
3738 Move IT's current position to that position. */
3739 if (INTEGERP (end_charpos)
3740 && XFASTINT (end_charpos) < XFASTINT (limit))
3741 {
3742 struct text_pos old;
3743 old = it->current.string_pos;
3744 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3745 compute_string_pos (&it->current.string_pos, old, it->string);
3746 }
3747 else
3748 {
3749 /* The rest of the string is invisible. If this is an
3750 overlay string, proceed with the next overlay string
3751 or whatever comes and return a character from there. */
3752 if (it->current.overlay_string_index >= 0)
3753 {
3754 next_overlay_string (it);
3755 /* Don't check for overlay strings when we just
3756 finished processing them. */
3757 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3758 }
3759 else
3760 {
3761 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3762 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3763 }
3764 }
3765 }
3766 }
3767 else
3768 {
3769 int invis_p;
3770 EMACS_INT newpos, next_stop, start_charpos;
3771 Lisp_Object pos, prop, overlay;
3772
3773 /* First of all, is there invisible text at this position? */
3774 start_charpos = IT_CHARPOS (*it);
3775 pos = make_number (IT_CHARPOS (*it));
3776 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3777 &overlay);
3778 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3779
3780 /* If we are on invisible text, skip over it. */
3781 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3782 {
3783 /* Record whether we have to display an ellipsis for the
3784 invisible text. */
3785 int display_ellipsis_p = invis_p == 2;
3786
3787 handled = HANDLED_RECOMPUTE_PROPS;
3788
3789 /* Loop skipping over invisible text. The loop is left at
3790 ZV or with IT on the first char being visible again. */
3791 do
3792 {
3793 /* Try to skip some invisible text. Return value is the
3794 position reached which can be equal to IT's position
3795 if there is nothing invisible here. This skips both
3796 over invisible text properties and overlays with
3797 invisible property. */
3798 newpos = skip_invisible (IT_CHARPOS (*it),
3799 &next_stop, ZV, it->window);
3800
3801 /* If we skipped nothing at all we weren't at invisible
3802 text in the first place. If everything to the end of
3803 the buffer was skipped, end the loop. */
3804 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3805 invis_p = 0;
3806 else
3807 {
3808 /* We skipped some characters but not necessarily
3809 all there are. Check if we ended up on visible
3810 text. Fget_char_property returns the property of
3811 the char before the given position, i.e. if we
3812 get invis_p = 0, this means that the char at
3813 newpos is visible. */
3814 pos = make_number (newpos);
3815 prop = Fget_char_property (pos, Qinvisible, it->window);
3816 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3817 }
3818
3819 /* If we ended up on invisible text, proceed to
3820 skip starting with next_stop. */
3821 if (invis_p)
3822 IT_CHARPOS (*it) = next_stop;
3823
3824 /* If there are adjacent invisible texts, don't lose the
3825 second one's ellipsis. */
3826 if (invis_p == 2)
3827 display_ellipsis_p = 1;
3828 }
3829 while (invis_p);
3830
3831 /* The position newpos is now either ZV or on visible text. */
3832 IT_CHARPOS (*it) = newpos;
3833 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3834
3835 /* If there are before-strings at the start of invisible
3836 text, and the text is invisible because of a text
3837 property, arrange to show before-strings because 20.x did
3838 it that way. (If the text is invisible because of an
3839 overlay property instead of a text property, this is
3840 already handled in the overlay code.) */
3841 if (NILP (overlay)
3842 && get_overlay_strings (it, start_charpos))
3843 {
3844 handled = HANDLED_RECOMPUTE_PROPS;
3845 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3846 }
3847 else if (display_ellipsis_p)
3848 {
3849 /* Make sure that the glyphs of the ellipsis will get
3850 correct `charpos' values. If we would not update
3851 it->position here, the glyphs would belong to the
3852 last visible character _before_ the invisible
3853 text, which confuses `set_cursor_from_row'.
3854
3855 We use the last invisible position instead of the
3856 first because this way the cursor is always drawn on
3857 the first "." of the ellipsis, whenever PT is inside
3858 the invisible text. Otherwise the cursor would be
3859 placed _after_ the ellipsis when the point is after the
3860 first invisible character. */
3861 if (!STRINGP (it->object))
3862 {
3863 it->position.charpos = IT_CHARPOS (*it) - 1;
3864 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3865 }
3866 it->ellipsis_p = 1;
3867 /* Let the ellipsis display before
3868 considering any properties of the following char.
3869 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3870 handled = HANDLED_RETURN;
3871 }
3872 }
3873 }
3874
3875 return handled;
3876 }
3877
3878
3879 /* Make iterator IT return `...' next.
3880 Replaces LEN characters from buffer. */
3881
3882 static void
3883 setup_for_ellipsis (it, len)
3884 struct it *it;
3885 int len;
3886 {
3887 /* Use the display table definition for `...'. Invalid glyphs
3888 will be handled by the method returning elements from dpvec. */
3889 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3890 {
3891 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3892 it->dpvec = v->contents;
3893 it->dpend = v->contents + v->size;
3894 }
3895 else
3896 {
3897 /* Default `...'. */
3898 it->dpvec = default_invis_vector;
3899 it->dpend = default_invis_vector + 3;
3900 }
3901
3902 it->dpvec_char_len = len;
3903 it->current.dpvec_index = 0;
3904 it->dpvec_face_id = -1;
3905
3906 /* Remember the current face id in case glyphs specify faces.
3907 IT's face is restored in set_iterator_to_next.
3908 saved_face_id was set to preceding char's face in handle_stop. */
3909 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3910 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3911
3912 it->method = GET_FROM_DISPLAY_VECTOR;
3913 it->ellipsis_p = 1;
3914 }
3915
3916
3917 \f
3918 /***********************************************************************
3919 'display' property
3920 ***********************************************************************/
3921
3922 /* Set up iterator IT from `display' property at its current position.
3923 Called from handle_stop.
3924 We return HANDLED_RETURN if some part of the display property
3925 overrides the display of the buffer text itself.
3926 Otherwise we return HANDLED_NORMALLY. */
3927
3928 static enum prop_handled
3929 handle_display_prop (it)
3930 struct it *it;
3931 {
3932 Lisp_Object prop, object, overlay;
3933 struct text_pos *position;
3934 /* Nonzero if some property replaces the display of the text itself. */
3935 int display_replaced_p = 0;
3936
3937 if (STRINGP (it->string))
3938 {
3939 object = it->string;
3940 position = &it->current.string_pos;
3941 }
3942 else
3943 {
3944 XSETWINDOW (object, it->w);
3945 position = &it->current.pos;
3946 }
3947
3948 /* Reset those iterator values set from display property values. */
3949 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3950 it->space_width = Qnil;
3951 it->font_height = Qnil;
3952 it->voffset = 0;
3953
3954 /* We don't support recursive `display' properties, i.e. string
3955 values that have a string `display' property, that have a string
3956 `display' property etc. */
3957 if (!it->string_from_display_prop_p)
3958 it->area = TEXT_AREA;
3959
3960 prop = get_char_property_and_overlay (make_number (position->charpos),
3961 Qdisplay, object, &overlay);
3962 if (NILP (prop))
3963 return HANDLED_NORMALLY;
3964 /* Now OVERLAY is the overlay that gave us this property, or nil
3965 if it was a text property. */
3966
3967 if (!STRINGP (it->string))
3968 object = it->w->buffer;
3969
3970 if (CONSP (prop)
3971 /* Simple properties. */
3972 && !EQ (XCAR (prop), Qimage)
3973 && !EQ (XCAR (prop), Qspace)
3974 && !EQ (XCAR (prop), Qwhen)
3975 && !EQ (XCAR (prop), Qslice)
3976 && !EQ (XCAR (prop), Qspace_width)
3977 && !EQ (XCAR (prop), Qheight)
3978 && !EQ (XCAR (prop), Qraise)
3979 /* Marginal area specifications. */
3980 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3981 && !EQ (XCAR (prop), Qleft_fringe)
3982 && !EQ (XCAR (prop), Qright_fringe)
3983 && !NILP (XCAR (prop)))
3984 {
3985 for (; CONSP (prop); prop = XCDR (prop))
3986 {
3987 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3988 position, display_replaced_p))
3989 {
3990 display_replaced_p = 1;
3991 /* If some text in a string is replaced, `position' no
3992 longer points to the position of `object'. */
3993 if (STRINGP (object))
3994 break;
3995 }
3996 }
3997 }
3998 else if (VECTORP (prop))
3999 {
4000 int i;
4001 for (i = 0; i < ASIZE (prop); ++i)
4002 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4003 position, display_replaced_p))
4004 {
4005 display_replaced_p = 1;
4006 /* If some text in a string is replaced, `position' no
4007 longer points to the position of `object'. */
4008 if (STRINGP (object))
4009 break;
4010 }
4011 }
4012 else
4013 {
4014 if (handle_single_display_spec (it, prop, object, overlay,
4015 position, 0))
4016 display_replaced_p = 1;
4017 }
4018
4019 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4020 }
4021
4022
4023 /* Value is the position of the end of the `display' property starting
4024 at START_POS in OBJECT. */
4025
4026 static struct text_pos
4027 display_prop_end (it, object, start_pos)
4028 struct it *it;
4029 Lisp_Object object;
4030 struct text_pos start_pos;
4031 {
4032 Lisp_Object end;
4033 struct text_pos end_pos;
4034
4035 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4036 Qdisplay, object, Qnil);
4037 CHARPOS (end_pos) = XFASTINT (end);
4038 if (STRINGP (object))
4039 compute_string_pos (&end_pos, start_pos, it->string);
4040 else
4041 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4042
4043 return end_pos;
4044 }
4045
4046
4047 /* Set up IT from a single `display' specification PROP. OBJECT
4048 is the object in which the `display' property was found. *POSITION
4049 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4050 means that we previously saw a display specification which already
4051 replaced text display with something else, for example an image;
4052 we ignore such properties after the first one has been processed.
4053
4054 OVERLAY is the overlay this `display' property came from,
4055 or nil if it was a text property.
4056
4057 If PROP is a `space' or `image' specification, and in some other
4058 cases too, set *POSITION to the position where the `display'
4059 property ends.
4060
4061 Value is non-zero if something was found which replaces the display
4062 of buffer or string text. */
4063
4064 static int
4065 handle_single_display_spec (it, spec, object, overlay, position,
4066 display_replaced_before_p)
4067 struct it *it;
4068 Lisp_Object spec;
4069 Lisp_Object object;
4070 Lisp_Object overlay;
4071 struct text_pos *position;
4072 int display_replaced_before_p;
4073 {
4074 Lisp_Object form;
4075 Lisp_Object location, value;
4076 struct text_pos start_pos, save_pos;
4077 int valid_p;
4078
4079 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4080 If the result is non-nil, use VALUE instead of SPEC. */
4081 form = Qt;
4082 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4083 {
4084 spec = XCDR (spec);
4085 if (!CONSP (spec))
4086 return 0;
4087 form = XCAR (spec);
4088 spec = XCDR (spec);
4089 }
4090
4091 if (!NILP (form) && !EQ (form, Qt))
4092 {
4093 int count = SPECPDL_INDEX ();
4094 struct gcpro gcpro1;
4095
4096 /* Bind `object' to the object having the `display' property, a
4097 buffer or string. Bind `position' to the position in the
4098 object where the property was found, and `buffer-position'
4099 to the current position in the buffer. */
4100 specbind (Qobject, object);
4101 specbind (Qposition, make_number (CHARPOS (*position)));
4102 specbind (Qbuffer_position,
4103 make_number (STRINGP (object)
4104 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4105 GCPRO1 (form);
4106 form = safe_eval (form);
4107 UNGCPRO;
4108 unbind_to (count, Qnil);
4109 }
4110
4111 if (NILP (form))
4112 return 0;
4113
4114 /* Handle `(height HEIGHT)' specifications. */
4115 if (CONSP (spec)
4116 && EQ (XCAR (spec), Qheight)
4117 && CONSP (XCDR (spec)))
4118 {
4119 if (!FRAME_WINDOW_P (it->f))
4120 return 0;
4121
4122 it->font_height = XCAR (XCDR (spec));
4123 if (!NILP (it->font_height))
4124 {
4125 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4126 int new_height = -1;
4127
4128 if (CONSP (it->font_height)
4129 && (EQ (XCAR (it->font_height), Qplus)
4130 || EQ (XCAR (it->font_height), Qminus))
4131 && CONSP (XCDR (it->font_height))
4132 && INTEGERP (XCAR (XCDR (it->font_height))))
4133 {
4134 /* `(+ N)' or `(- N)' where N is an integer. */
4135 int steps = XINT (XCAR (XCDR (it->font_height)));
4136 if (EQ (XCAR (it->font_height), Qplus))
4137 steps = - steps;
4138 it->face_id = smaller_face (it->f, it->face_id, steps);
4139 }
4140 else if (FUNCTIONP (it->font_height))
4141 {
4142 /* Call function with current height as argument.
4143 Value is the new height. */
4144 Lisp_Object height;
4145 height = safe_call1 (it->font_height,
4146 face->lface[LFACE_HEIGHT_INDEX]);
4147 if (NUMBERP (height))
4148 new_height = XFLOATINT (height);
4149 }
4150 else if (NUMBERP (it->font_height))
4151 {
4152 /* Value is a multiple of the canonical char height. */
4153 struct face *face;
4154
4155 face = FACE_FROM_ID (it->f,
4156 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4157 new_height = (XFLOATINT (it->font_height)
4158 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4159 }
4160 else
4161 {
4162 /* Evaluate IT->font_height with `height' bound to the
4163 current specified height to get the new height. */
4164 int count = SPECPDL_INDEX ();
4165
4166 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4167 value = safe_eval (it->font_height);
4168 unbind_to (count, Qnil);
4169
4170 if (NUMBERP (value))
4171 new_height = XFLOATINT (value);
4172 }
4173
4174 if (new_height > 0)
4175 it->face_id = face_with_height (it->f, it->face_id, new_height);
4176 }
4177
4178 return 0;
4179 }
4180
4181 /* Handle `(space-width WIDTH)'. */
4182 if (CONSP (spec)
4183 && EQ (XCAR (spec), Qspace_width)
4184 && CONSP (XCDR (spec)))
4185 {
4186 if (!FRAME_WINDOW_P (it->f))
4187 return 0;
4188
4189 value = XCAR (XCDR (spec));
4190 if (NUMBERP (value) && XFLOATINT (value) > 0)
4191 it->space_width = value;
4192
4193 return 0;
4194 }
4195
4196 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4197 if (CONSP (spec)
4198 && EQ (XCAR (spec), Qslice))
4199 {
4200 Lisp_Object tem;
4201
4202 if (!FRAME_WINDOW_P (it->f))
4203 return 0;
4204
4205 if (tem = XCDR (spec), CONSP (tem))
4206 {
4207 it->slice.x = XCAR (tem);
4208 if (tem = XCDR (tem), CONSP (tem))
4209 {
4210 it->slice.y = XCAR (tem);
4211 if (tem = XCDR (tem), CONSP (tem))
4212 {
4213 it->slice.width = XCAR (tem);
4214 if (tem = XCDR (tem), CONSP (tem))
4215 it->slice.height = XCAR (tem);
4216 }
4217 }
4218 }
4219
4220 return 0;
4221 }
4222
4223 /* Handle `(raise FACTOR)'. */
4224 if (CONSP (spec)
4225 && EQ (XCAR (spec), Qraise)
4226 && CONSP (XCDR (spec)))
4227 {
4228 if (!FRAME_WINDOW_P (it->f))
4229 return 0;
4230
4231 #ifdef HAVE_WINDOW_SYSTEM
4232 value = XCAR (XCDR (spec));
4233 if (NUMBERP (value))
4234 {
4235 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4236 it->voffset = - (XFLOATINT (value)
4237 * (FONT_HEIGHT (face->font)));
4238 }
4239 #endif /* HAVE_WINDOW_SYSTEM */
4240
4241 return 0;
4242 }
4243
4244 /* Don't handle the other kinds of display specifications
4245 inside a string that we got from a `display' property. */
4246 if (it->string_from_display_prop_p)
4247 return 0;
4248
4249 /* Characters having this form of property are not displayed, so
4250 we have to find the end of the property. */
4251 start_pos = *position;
4252 *position = display_prop_end (it, object, start_pos);
4253 value = Qnil;
4254
4255 /* Stop the scan at that end position--we assume that all
4256 text properties change there. */
4257 it->stop_charpos = position->charpos;
4258
4259 /* Handle `(left-fringe BITMAP [FACE])'
4260 and `(right-fringe BITMAP [FACE])'. */
4261 if (CONSP (spec)
4262 && (EQ (XCAR (spec), Qleft_fringe)
4263 || EQ (XCAR (spec), Qright_fringe))
4264 && CONSP (XCDR (spec)))
4265 {
4266 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4267 int fringe_bitmap;
4268
4269 if (!FRAME_WINDOW_P (it->f))
4270 /* If we return here, POSITION has been advanced
4271 across the text with this property. */
4272 return 0;
4273
4274 #ifdef HAVE_WINDOW_SYSTEM
4275 value = XCAR (XCDR (spec));
4276 if (!SYMBOLP (value)
4277 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4278 /* If we return here, POSITION has been advanced
4279 across the text with this property. */
4280 return 0;
4281
4282 if (CONSP (XCDR (XCDR (spec))))
4283 {
4284 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4285 int face_id2 = lookup_derived_face (it->f, face_name,
4286 FRINGE_FACE_ID, 0);
4287 if (face_id2 >= 0)
4288 face_id = face_id2;
4289 }
4290
4291 /* Save current settings of IT so that we can restore them
4292 when we are finished with the glyph property value. */
4293
4294 save_pos = it->position;
4295 it->position = *position;
4296 push_it (it);
4297 it->position = save_pos;
4298
4299 it->area = TEXT_AREA;
4300 it->what = IT_IMAGE;
4301 it->image_id = -1; /* no image */
4302 it->position = start_pos;
4303 it->object = NILP (object) ? it->w->buffer : object;
4304 it->method = GET_FROM_IMAGE;
4305 it->from_overlay = Qnil;
4306 it->face_id = face_id;
4307
4308 /* Say that we haven't consumed the characters with
4309 `display' property yet. The call to pop_it in
4310 set_iterator_to_next will clean this up. */
4311 *position = start_pos;
4312
4313 if (EQ (XCAR (spec), Qleft_fringe))
4314 {
4315 it->left_user_fringe_bitmap = fringe_bitmap;
4316 it->left_user_fringe_face_id = face_id;
4317 }
4318 else
4319 {
4320 it->right_user_fringe_bitmap = fringe_bitmap;
4321 it->right_user_fringe_face_id = face_id;
4322 }
4323 #endif /* HAVE_WINDOW_SYSTEM */
4324 return 1;
4325 }
4326
4327 /* Prepare to handle `((margin left-margin) ...)',
4328 `((margin right-margin) ...)' and `((margin nil) ...)'
4329 prefixes for display specifications. */
4330 location = Qunbound;
4331 if (CONSP (spec) && CONSP (XCAR (spec)))
4332 {
4333 Lisp_Object tem;
4334
4335 value = XCDR (spec);
4336 if (CONSP (value))
4337 value = XCAR (value);
4338
4339 tem = XCAR (spec);
4340 if (EQ (XCAR (tem), Qmargin)
4341 && (tem = XCDR (tem),
4342 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4343 (NILP (tem)
4344 || EQ (tem, Qleft_margin)
4345 || EQ (tem, Qright_margin))))
4346 location = tem;
4347 }
4348
4349 if (EQ (location, Qunbound))
4350 {
4351 location = Qnil;
4352 value = spec;
4353 }
4354
4355 /* After this point, VALUE is the property after any
4356 margin prefix has been stripped. It must be a string,
4357 an image specification, or `(space ...)'.
4358
4359 LOCATION specifies where to display: `left-margin',
4360 `right-margin' or nil. */
4361
4362 valid_p = (STRINGP (value)
4363 #ifdef HAVE_WINDOW_SYSTEM
4364 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4365 #endif /* not HAVE_WINDOW_SYSTEM */
4366 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4367
4368 if (valid_p && !display_replaced_before_p)
4369 {
4370 /* Save current settings of IT so that we can restore them
4371 when we are finished with the glyph property value. */
4372 save_pos = it->position;
4373 it->position = *position;
4374 push_it (it);
4375 it->position = save_pos;
4376 it->from_overlay = overlay;
4377
4378 if (NILP (location))
4379 it->area = TEXT_AREA;
4380 else if (EQ (location, Qleft_margin))
4381 it->area = LEFT_MARGIN_AREA;
4382 else
4383 it->area = RIGHT_MARGIN_AREA;
4384
4385 if (STRINGP (value))
4386 {
4387 it->string = value;
4388 it->multibyte_p = STRING_MULTIBYTE (it->string);
4389 it->current.overlay_string_index = -1;
4390 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4391 it->end_charpos = it->string_nchars = SCHARS (it->string);
4392 it->method = GET_FROM_STRING;
4393 it->stop_charpos = 0;
4394 it->string_from_display_prop_p = 1;
4395 /* Say that we haven't consumed the characters with
4396 `display' property yet. The call to pop_it in
4397 set_iterator_to_next will clean this up. */
4398 if (BUFFERP (object))
4399 *position = start_pos;
4400 }
4401 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4402 {
4403 it->method = GET_FROM_STRETCH;
4404 it->object = value;
4405 *position = it->position = start_pos;
4406 }
4407 #ifdef HAVE_WINDOW_SYSTEM
4408 else
4409 {
4410 it->what = IT_IMAGE;
4411 it->image_id = lookup_image (it->f, value);
4412 it->position = start_pos;
4413 it->object = NILP (object) ? it->w->buffer : object;
4414 it->method = GET_FROM_IMAGE;
4415
4416 /* Say that we haven't consumed the characters with
4417 `display' property yet. The call to pop_it in
4418 set_iterator_to_next will clean this up. */
4419 *position = start_pos;
4420 }
4421 #endif /* HAVE_WINDOW_SYSTEM */
4422
4423 return 1;
4424 }
4425
4426 /* Invalid property or property not supported. Restore
4427 POSITION to what it was before. */
4428 *position = start_pos;
4429 return 0;
4430 }
4431
4432
4433 /* Check if SPEC is a display sub-property value whose text should be
4434 treated as intangible. */
4435
4436 static int
4437 single_display_spec_intangible_p (prop)
4438 Lisp_Object prop;
4439 {
4440 /* Skip over `when FORM'. */
4441 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4442 {
4443 prop = XCDR (prop);
4444 if (!CONSP (prop))
4445 return 0;
4446 prop = XCDR (prop);
4447 }
4448
4449 if (STRINGP (prop))
4450 return 1;
4451
4452 if (!CONSP (prop))
4453 return 0;
4454
4455 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4456 we don't need to treat text as intangible. */
4457 if (EQ (XCAR (prop), Qmargin))
4458 {
4459 prop = XCDR (prop);
4460 if (!CONSP (prop))
4461 return 0;
4462
4463 prop = XCDR (prop);
4464 if (!CONSP (prop)
4465 || EQ (XCAR (prop), Qleft_margin)
4466 || EQ (XCAR (prop), Qright_margin))
4467 return 0;
4468 }
4469
4470 return (CONSP (prop)
4471 && (EQ (XCAR (prop), Qimage)
4472 || EQ (XCAR (prop), Qspace)));
4473 }
4474
4475
4476 /* Check if PROP is a display property value whose text should be
4477 treated as intangible. */
4478
4479 int
4480 display_prop_intangible_p (prop)
4481 Lisp_Object prop;
4482 {
4483 if (CONSP (prop)
4484 && CONSP (XCAR (prop))
4485 && !EQ (Qmargin, XCAR (XCAR (prop))))
4486 {
4487 /* A list of sub-properties. */
4488 while (CONSP (prop))
4489 {
4490 if (single_display_spec_intangible_p (XCAR (prop)))
4491 return 1;
4492 prop = XCDR (prop);
4493 }
4494 }
4495 else if (VECTORP (prop))
4496 {
4497 /* A vector of sub-properties. */
4498 int i;
4499 for (i = 0; i < ASIZE (prop); ++i)
4500 if (single_display_spec_intangible_p (AREF (prop, i)))
4501 return 1;
4502 }
4503 else
4504 return single_display_spec_intangible_p (prop);
4505
4506 return 0;
4507 }
4508
4509
4510 /* Return 1 if PROP is a display sub-property value containing STRING. */
4511
4512 static int
4513 single_display_spec_string_p (prop, string)
4514 Lisp_Object prop, string;
4515 {
4516 if (EQ (string, prop))
4517 return 1;
4518
4519 /* Skip over `when FORM'. */
4520 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4521 {
4522 prop = XCDR (prop);
4523 if (!CONSP (prop))
4524 return 0;
4525 prop = XCDR (prop);
4526 }
4527
4528 if (CONSP (prop))
4529 /* Skip over `margin LOCATION'. */
4530 if (EQ (XCAR (prop), Qmargin))
4531 {
4532 prop = XCDR (prop);
4533 if (!CONSP (prop))
4534 return 0;
4535
4536 prop = XCDR (prop);
4537 if (!CONSP (prop))
4538 return 0;
4539 }
4540
4541 return CONSP (prop) && EQ (XCAR (prop), string);
4542 }
4543
4544
4545 /* Return 1 if STRING appears in the `display' property PROP. */
4546
4547 static int
4548 display_prop_string_p (prop, string)
4549 Lisp_Object prop, string;
4550 {
4551 if (CONSP (prop)
4552 && CONSP (XCAR (prop))
4553 && !EQ (Qmargin, XCAR (XCAR (prop))))
4554 {
4555 /* A list of sub-properties. */
4556 while (CONSP (prop))
4557 {
4558 if (single_display_spec_string_p (XCAR (prop), string))
4559 return 1;
4560 prop = XCDR (prop);
4561 }
4562 }
4563 else if (VECTORP (prop))
4564 {
4565 /* A vector of sub-properties. */
4566 int i;
4567 for (i = 0; i < ASIZE (prop); ++i)
4568 if (single_display_spec_string_p (AREF (prop, i), string))
4569 return 1;
4570 }
4571 else
4572 return single_display_spec_string_p (prop, string);
4573
4574 return 0;
4575 }
4576
4577
4578 /* Determine from which buffer position in W's buffer STRING comes
4579 from. AROUND_CHARPOS is an approximate position where it could
4580 be from. Value is the buffer position or 0 if it couldn't be
4581 determined.
4582
4583 W's buffer must be current.
4584
4585 This function is necessary because we don't record buffer positions
4586 in glyphs generated from strings (to keep struct glyph small).
4587 This function may only use code that doesn't eval because it is
4588 called asynchronously from note_mouse_highlight. */
4589
4590 int
4591 string_buffer_position (w, string, around_charpos)
4592 struct window *w;
4593 Lisp_Object string;
4594 int around_charpos;
4595 {
4596 Lisp_Object limit, prop, pos;
4597 const int MAX_DISTANCE = 1000;
4598 int found = 0;
4599
4600 pos = make_number (around_charpos);
4601 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4602 while (!found && !EQ (pos, limit))
4603 {
4604 prop = Fget_char_property (pos, Qdisplay, Qnil);
4605 if (!NILP (prop) && display_prop_string_p (prop, string))
4606 found = 1;
4607 else
4608 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4609 }
4610
4611 if (!found)
4612 {
4613 pos = make_number (around_charpos);
4614 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4615 while (!found && !EQ (pos, limit))
4616 {
4617 prop = Fget_char_property (pos, Qdisplay, Qnil);
4618 if (!NILP (prop) && display_prop_string_p (prop, string))
4619 found = 1;
4620 else
4621 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4622 limit);
4623 }
4624 }
4625
4626 return found ? XINT (pos) : 0;
4627 }
4628
4629
4630 \f
4631 /***********************************************************************
4632 `composition' property
4633 ***********************************************************************/
4634
4635 /* Set up iterator IT from `composition' property at its current
4636 position. Called from handle_stop. */
4637
4638 static enum prop_handled
4639 handle_composition_prop (it)
4640 struct it *it;
4641 {
4642 Lisp_Object prop, string;
4643 EMACS_INT pos, pos_byte, start, end;
4644
4645 if (STRINGP (it->string))
4646 {
4647 unsigned char *s;
4648
4649 pos = IT_STRING_CHARPOS (*it);
4650 pos_byte = IT_STRING_BYTEPOS (*it);
4651 string = it->string;
4652 s = SDATA (string) + pos_byte;
4653 it->c = STRING_CHAR (s, 0);
4654 }
4655 else
4656 {
4657 pos = IT_CHARPOS (*it);
4658 pos_byte = IT_BYTEPOS (*it);
4659 string = Qnil;
4660 it->c = FETCH_CHAR (pos_byte);
4661 }
4662
4663 /* If there's a valid composition and point is not inside of the
4664 composition (in the case that the composition is from the current
4665 buffer), draw a glyph composed from the composition components. */
4666 if (find_composition (pos, -1, &start, &end, &prop, string)
4667 && COMPOSITION_VALID_P (start, end, prop)
4668 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4669 {
4670 if (start != pos)
4671 {
4672 if (STRINGP (it->string))
4673 pos_byte = string_char_to_byte (it->string, start);
4674 else
4675 pos_byte = CHAR_TO_BYTE (start);
4676 }
4677 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4678 prop, string);
4679
4680 if (it->cmp_it.id >= 0)
4681 {
4682 it->cmp_it.ch = -1;
4683 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4684 it->cmp_it.nglyphs = -1;
4685 }
4686 }
4687
4688 return HANDLED_NORMALLY;
4689 }
4690
4691
4692 \f
4693 /***********************************************************************
4694 Overlay strings
4695 ***********************************************************************/
4696
4697 /* The following structure is used to record overlay strings for
4698 later sorting in load_overlay_strings. */
4699
4700 struct overlay_entry
4701 {
4702 Lisp_Object overlay;
4703 Lisp_Object string;
4704 int priority;
4705 int after_string_p;
4706 };
4707
4708
4709 /* Set up iterator IT from overlay strings at its current position.
4710 Called from handle_stop. */
4711
4712 static enum prop_handled
4713 handle_overlay_change (it)
4714 struct it *it;
4715 {
4716 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4717 return HANDLED_RECOMPUTE_PROPS;
4718 else
4719 return HANDLED_NORMALLY;
4720 }
4721
4722
4723 /* Set up the next overlay string for delivery by IT, if there is an
4724 overlay string to deliver. Called by set_iterator_to_next when the
4725 end of the current overlay string is reached. If there are more
4726 overlay strings to display, IT->string and
4727 IT->current.overlay_string_index are set appropriately here.
4728 Otherwise IT->string is set to nil. */
4729
4730 static void
4731 next_overlay_string (it)
4732 struct it *it;
4733 {
4734 ++it->current.overlay_string_index;
4735 if (it->current.overlay_string_index == it->n_overlay_strings)
4736 {
4737 /* No more overlay strings. Restore IT's settings to what
4738 they were before overlay strings were processed, and
4739 continue to deliver from current_buffer. */
4740
4741 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4742 pop_it (it);
4743 xassert (it->sp > 0
4744 || (NILP (it->string)
4745 && it->method == GET_FROM_BUFFER
4746 && it->stop_charpos >= BEGV
4747 && it->stop_charpos <= it->end_charpos));
4748 it->current.overlay_string_index = -1;
4749 it->n_overlay_strings = 0;
4750
4751 /* If we're at the end of the buffer, record that we have
4752 processed the overlay strings there already, so that
4753 next_element_from_buffer doesn't try it again. */
4754 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4755 it->overlay_strings_at_end_processed_p = 1;
4756 }
4757 else
4758 {
4759 /* There are more overlay strings to process. If
4760 IT->current.overlay_string_index has advanced to a position
4761 where we must load IT->overlay_strings with more strings, do
4762 it. */
4763 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4764
4765 if (it->current.overlay_string_index && i == 0)
4766 load_overlay_strings (it, 0);
4767
4768 /* Initialize IT to deliver display elements from the overlay
4769 string. */
4770 it->string = it->overlay_strings[i];
4771 it->multibyte_p = STRING_MULTIBYTE (it->string);
4772 SET_TEXT_POS (it->current.string_pos, 0, 0);
4773 it->method = GET_FROM_STRING;
4774 it->stop_charpos = 0;
4775 if (it->cmp_it.stop_pos >= 0)
4776 it->cmp_it.stop_pos = 0;
4777 }
4778
4779 CHECK_IT (it);
4780 }
4781
4782
4783 /* Compare two overlay_entry structures E1 and E2. Used as a
4784 comparison function for qsort in load_overlay_strings. Overlay
4785 strings for the same position are sorted so that
4786
4787 1. All after-strings come in front of before-strings, except
4788 when they come from the same overlay.
4789
4790 2. Within after-strings, strings are sorted so that overlay strings
4791 from overlays with higher priorities come first.
4792
4793 2. Within before-strings, strings are sorted so that overlay
4794 strings from overlays with higher priorities come last.
4795
4796 Value is analogous to strcmp. */
4797
4798
4799 static int
4800 compare_overlay_entries (e1, e2)
4801 void *e1, *e2;
4802 {
4803 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4804 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4805 int result;
4806
4807 if (entry1->after_string_p != entry2->after_string_p)
4808 {
4809 /* Let after-strings appear in front of before-strings if
4810 they come from different overlays. */
4811 if (EQ (entry1->overlay, entry2->overlay))
4812 result = entry1->after_string_p ? 1 : -1;
4813 else
4814 result = entry1->after_string_p ? -1 : 1;
4815 }
4816 else if (entry1->after_string_p)
4817 /* After-strings sorted in order of decreasing priority. */
4818 result = entry2->priority - entry1->priority;
4819 else
4820 /* Before-strings sorted in order of increasing priority. */
4821 result = entry1->priority - entry2->priority;
4822
4823 return result;
4824 }
4825
4826
4827 /* Load the vector IT->overlay_strings with overlay strings from IT's
4828 current buffer position, or from CHARPOS if that is > 0. Set
4829 IT->n_overlays to the total number of overlay strings found.
4830
4831 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4832 a time. On entry into load_overlay_strings,
4833 IT->current.overlay_string_index gives the number of overlay
4834 strings that have already been loaded by previous calls to this
4835 function.
4836
4837 IT->add_overlay_start contains an additional overlay start
4838 position to consider for taking overlay strings from, if non-zero.
4839 This position comes into play when the overlay has an `invisible'
4840 property, and both before and after-strings. When we've skipped to
4841 the end of the overlay, because of its `invisible' property, we
4842 nevertheless want its before-string to appear.
4843 IT->add_overlay_start will contain the overlay start position
4844 in this case.
4845
4846 Overlay strings are sorted so that after-string strings come in
4847 front of before-string strings. Within before and after-strings,
4848 strings are sorted by overlay priority. See also function
4849 compare_overlay_entries. */
4850
4851 static void
4852 load_overlay_strings (it, charpos)
4853 struct it *it;
4854 int charpos;
4855 {
4856 extern Lisp_Object Qwindow, Qpriority;
4857 Lisp_Object overlay, window, str, invisible;
4858 struct Lisp_Overlay *ov;
4859 int start, end;
4860 int size = 20;
4861 int n = 0, i, j, invis_p;
4862 struct overlay_entry *entries
4863 = (struct overlay_entry *) alloca (size * sizeof *entries);
4864
4865 if (charpos <= 0)
4866 charpos = IT_CHARPOS (*it);
4867
4868 /* Append the overlay string STRING of overlay OVERLAY to vector
4869 `entries' which has size `size' and currently contains `n'
4870 elements. AFTER_P non-zero means STRING is an after-string of
4871 OVERLAY. */
4872 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4873 do \
4874 { \
4875 Lisp_Object priority; \
4876 \
4877 if (n == size) \
4878 { \
4879 int new_size = 2 * size; \
4880 struct overlay_entry *old = entries; \
4881 entries = \
4882 (struct overlay_entry *) alloca (new_size \
4883 * sizeof *entries); \
4884 bcopy (old, entries, size * sizeof *entries); \
4885 size = new_size; \
4886 } \
4887 \
4888 entries[n].string = (STRING); \
4889 entries[n].overlay = (OVERLAY); \
4890 priority = Foverlay_get ((OVERLAY), Qpriority); \
4891 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4892 entries[n].after_string_p = (AFTER_P); \
4893 ++n; \
4894 } \
4895 while (0)
4896
4897 /* Process overlay before the overlay center. */
4898 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4899 {
4900 XSETMISC (overlay, ov);
4901 xassert (OVERLAYP (overlay));
4902 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4903 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4904
4905 if (end < charpos)
4906 break;
4907
4908 /* Skip this overlay if it doesn't start or end at IT's current
4909 position. */
4910 if (end != charpos && start != charpos)
4911 continue;
4912
4913 /* Skip this overlay if it doesn't apply to IT->w. */
4914 window = Foverlay_get (overlay, Qwindow);
4915 if (WINDOWP (window) && XWINDOW (window) != it->w)
4916 continue;
4917
4918 /* If the text ``under'' the overlay is invisible, both before-
4919 and after-strings from this overlay are visible; start and
4920 end position are indistinguishable. */
4921 invisible = Foverlay_get (overlay, Qinvisible);
4922 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4923
4924 /* If overlay has a non-empty before-string, record it. */
4925 if ((start == charpos || (end == charpos && invis_p))
4926 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4927 && SCHARS (str))
4928 RECORD_OVERLAY_STRING (overlay, str, 0);
4929
4930 /* If overlay has a non-empty after-string, record it. */
4931 if ((end == charpos || (start == charpos && invis_p))
4932 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4933 && SCHARS (str))
4934 RECORD_OVERLAY_STRING (overlay, str, 1);
4935 }
4936
4937 /* Process overlays after the overlay center. */
4938 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4939 {
4940 XSETMISC (overlay, ov);
4941 xassert (OVERLAYP (overlay));
4942 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4943 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4944
4945 if (start > charpos)
4946 break;
4947
4948 /* Skip this overlay if it doesn't start or end at IT's current
4949 position. */
4950 if (end != charpos && start != charpos)
4951 continue;
4952
4953 /* Skip this overlay if it doesn't apply to IT->w. */
4954 window = Foverlay_get (overlay, Qwindow);
4955 if (WINDOWP (window) && XWINDOW (window) != it->w)
4956 continue;
4957
4958 /* If the text ``under'' the overlay is invisible, it has a zero
4959 dimension, and both before- and after-strings apply. */
4960 invisible = Foverlay_get (overlay, Qinvisible);
4961 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4962
4963 /* If overlay has a non-empty before-string, record it. */
4964 if ((start == charpos || (end == charpos && invis_p))
4965 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4966 && SCHARS (str))
4967 RECORD_OVERLAY_STRING (overlay, str, 0);
4968
4969 /* If overlay has a non-empty after-string, record it. */
4970 if ((end == charpos || (start == charpos && invis_p))
4971 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4972 && SCHARS (str))
4973 RECORD_OVERLAY_STRING (overlay, str, 1);
4974 }
4975
4976 #undef RECORD_OVERLAY_STRING
4977
4978 /* Sort entries. */
4979 if (n > 1)
4980 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4981
4982 /* Record the total number of strings to process. */
4983 it->n_overlay_strings = n;
4984
4985 /* IT->current.overlay_string_index is the number of overlay strings
4986 that have already been consumed by IT. Copy some of the
4987 remaining overlay strings to IT->overlay_strings. */
4988 i = 0;
4989 j = it->current.overlay_string_index;
4990 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4991 {
4992 it->overlay_strings[i] = entries[j].string;
4993 it->string_overlays[i++] = entries[j++].overlay;
4994 }
4995
4996 CHECK_IT (it);
4997 }
4998
4999
5000 /* Get the first chunk of overlay strings at IT's current buffer
5001 position, or at CHARPOS if that is > 0. Value is non-zero if at
5002 least one overlay string was found. */
5003
5004 static int
5005 get_overlay_strings_1 (it, charpos, compute_stop_p)
5006 struct it *it;
5007 int charpos;
5008 int compute_stop_p;
5009 {
5010 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5011 process. This fills IT->overlay_strings with strings, and sets
5012 IT->n_overlay_strings to the total number of strings to process.
5013 IT->pos.overlay_string_index has to be set temporarily to zero
5014 because load_overlay_strings needs this; it must be set to -1
5015 when no overlay strings are found because a zero value would
5016 indicate a position in the first overlay string. */
5017 it->current.overlay_string_index = 0;
5018 load_overlay_strings (it, charpos);
5019
5020 /* If we found overlay strings, set up IT to deliver display
5021 elements from the first one. Otherwise set up IT to deliver
5022 from current_buffer. */
5023 if (it->n_overlay_strings)
5024 {
5025 /* Make sure we know settings in current_buffer, so that we can
5026 restore meaningful values when we're done with the overlay
5027 strings. */
5028 if (compute_stop_p)
5029 compute_stop_pos (it);
5030 xassert (it->face_id >= 0);
5031
5032 /* Save IT's settings. They are restored after all overlay
5033 strings have been processed. */
5034 xassert (!compute_stop_p || it->sp == 0);
5035
5036 /* When called from handle_stop, there might be an empty display
5037 string loaded. In that case, don't bother saving it. */
5038 if (!STRINGP (it->string) || SCHARS (it->string))
5039 push_it (it);
5040
5041 /* Set up IT to deliver display elements from the first overlay
5042 string. */
5043 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5044 it->string = it->overlay_strings[0];
5045 it->from_overlay = Qnil;
5046 it->stop_charpos = 0;
5047 xassert (STRINGP (it->string));
5048 it->end_charpos = SCHARS (it->string);
5049 it->multibyte_p = STRING_MULTIBYTE (it->string);
5050 it->method = GET_FROM_STRING;
5051 return 1;
5052 }
5053
5054 it->current.overlay_string_index = -1;
5055 return 0;
5056 }
5057
5058 static int
5059 get_overlay_strings (it, charpos)
5060 struct it *it;
5061 int charpos;
5062 {
5063 it->string = Qnil;
5064 it->method = GET_FROM_BUFFER;
5065
5066 (void) get_overlay_strings_1 (it, charpos, 1);
5067
5068 CHECK_IT (it);
5069
5070 /* Value is non-zero if we found at least one overlay string. */
5071 return STRINGP (it->string);
5072 }
5073
5074
5075 \f
5076 /***********************************************************************
5077 Saving and restoring state
5078 ***********************************************************************/
5079
5080 /* Save current settings of IT on IT->stack. Called, for example,
5081 before setting up IT for an overlay string, to be able to restore
5082 IT's settings to what they were after the overlay string has been
5083 processed. */
5084
5085 static void
5086 push_it (it)
5087 struct it *it;
5088 {
5089 struct iterator_stack_entry *p;
5090
5091 xassert (it->sp < IT_STACK_SIZE);
5092 p = it->stack + it->sp;
5093
5094 p->stop_charpos = it->stop_charpos;
5095 p->cmp_it = it->cmp_it;
5096 xassert (it->face_id >= 0);
5097 p->face_id = it->face_id;
5098 p->string = it->string;
5099 p->method = it->method;
5100 p->from_overlay = it->from_overlay;
5101 switch (p->method)
5102 {
5103 case GET_FROM_IMAGE:
5104 p->u.image.object = it->object;
5105 p->u.image.image_id = it->image_id;
5106 p->u.image.slice = it->slice;
5107 break;
5108 case GET_FROM_STRETCH:
5109 p->u.stretch.object = it->object;
5110 break;
5111 }
5112 p->position = it->position;
5113 p->current = it->current;
5114 p->end_charpos = it->end_charpos;
5115 p->string_nchars = it->string_nchars;
5116 p->area = it->area;
5117 p->multibyte_p = it->multibyte_p;
5118 p->avoid_cursor_p = it->avoid_cursor_p;
5119 p->space_width = it->space_width;
5120 p->font_height = it->font_height;
5121 p->voffset = it->voffset;
5122 p->string_from_display_prop_p = it->string_from_display_prop_p;
5123 p->display_ellipsis_p = 0;
5124 p->line_wrap = it->line_wrap;
5125 ++it->sp;
5126 }
5127
5128
5129 /* Restore IT's settings from IT->stack. Called, for example, when no
5130 more overlay strings must be processed, and we return to delivering
5131 display elements from a buffer, or when the end of a string from a
5132 `display' property is reached and we return to delivering display
5133 elements from an overlay string, or from a buffer. */
5134
5135 static void
5136 pop_it (it)
5137 struct it *it;
5138 {
5139 struct iterator_stack_entry *p;
5140
5141 xassert (it->sp > 0);
5142 --it->sp;
5143 p = it->stack + it->sp;
5144 it->stop_charpos = p->stop_charpos;
5145 it->cmp_it = p->cmp_it;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 it->from_overlay = p->from_overlay;
5151 if (NILP (it->string))
5152 SET_TEXT_POS (it->current.string_pos, -1, -1);
5153 it->method = p->method;
5154 switch (it->method)
5155 {
5156 case GET_FROM_IMAGE:
5157 it->image_id = p->u.image.image_id;
5158 it->object = p->u.image.object;
5159 it->slice = p->u.image.slice;
5160 break;
5161 case GET_FROM_STRETCH:
5162 it->object = p->u.comp.object;
5163 break;
5164 case GET_FROM_BUFFER:
5165 it->object = it->w->buffer;
5166 break;
5167 case GET_FROM_STRING:
5168 it->object = it->string;
5169 break;
5170 }
5171 it->end_charpos = p->end_charpos;
5172 it->string_nchars = p->string_nchars;
5173 it->area = p->area;
5174 it->multibyte_p = p->multibyte_p;
5175 it->avoid_cursor_p = p->avoid_cursor_p;
5176 it->space_width = p->space_width;
5177 it->font_height = p->font_height;
5178 it->voffset = p->voffset;
5179 it->string_from_display_prop_p = p->string_from_display_prop_p;
5180 it->line_wrap = p->line_wrap;
5181 }
5182
5183
5184 \f
5185 /***********************************************************************
5186 Moving over lines
5187 ***********************************************************************/
5188
5189 /* Set IT's current position to the previous line start. */
5190
5191 static void
5192 back_to_previous_line_start (it)
5193 struct it *it;
5194 {
5195 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5196 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5197 }
5198
5199
5200 /* Move IT to the next line start.
5201
5202 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5203 we skipped over part of the text (as opposed to moving the iterator
5204 continuously over the text). Otherwise, don't change the value
5205 of *SKIPPED_P.
5206
5207 Newlines may come from buffer text, overlay strings, or strings
5208 displayed via the `display' property. That's the reason we can't
5209 simply use find_next_newline_no_quit.
5210
5211 Note that this function may not skip over invisible text that is so
5212 because of text properties and immediately follows a newline. If
5213 it would, function reseat_at_next_visible_line_start, when called
5214 from set_iterator_to_next, would effectively make invisible
5215 characters following a newline part of the wrong glyph row, which
5216 leads to wrong cursor motion. */
5217
5218 static int
5219 forward_to_next_line_start (it, skipped_p)
5220 struct it *it;
5221 int *skipped_p;
5222 {
5223 int old_selective, newline_found_p, n;
5224 const int MAX_NEWLINE_DISTANCE = 500;
5225
5226 /* If already on a newline, just consume it to avoid unintended
5227 skipping over invisible text below. */
5228 if (it->what == IT_CHARACTER
5229 && it->c == '\n'
5230 && CHARPOS (it->position) == IT_CHARPOS (*it))
5231 {
5232 set_iterator_to_next (it, 0);
5233 it->c = 0;
5234 return 1;
5235 }
5236
5237 /* Don't handle selective display in the following. It's (a)
5238 unnecessary because it's done by the caller, and (b) leads to an
5239 infinite recursion because next_element_from_ellipsis indirectly
5240 calls this function. */
5241 old_selective = it->selective;
5242 it->selective = 0;
5243
5244 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5245 from buffer text. */
5246 for (n = newline_found_p = 0;
5247 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5248 n += STRINGP (it->string) ? 0 : 1)
5249 {
5250 if (!get_next_display_element (it))
5251 return 0;
5252 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5253 set_iterator_to_next (it, 0);
5254 }
5255
5256 /* If we didn't find a newline near enough, see if we can use a
5257 short-cut. */
5258 if (!newline_found_p)
5259 {
5260 int start = IT_CHARPOS (*it);
5261 int limit = find_next_newline_no_quit (start, 1);
5262 Lisp_Object pos;
5263
5264 xassert (!STRINGP (it->string));
5265
5266 /* If there isn't any `display' property in sight, and no
5267 overlays, we can just use the position of the newline in
5268 buffer text. */
5269 if (it->stop_charpos >= limit
5270 || ((pos = Fnext_single_property_change (make_number (start),
5271 Qdisplay,
5272 Qnil, make_number (limit)),
5273 NILP (pos))
5274 && next_overlay_change (start) == ZV))
5275 {
5276 IT_CHARPOS (*it) = limit;
5277 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5278 *skipped_p = newline_found_p = 1;
5279 }
5280 else
5281 {
5282 while (get_next_display_element (it)
5283 && !newline_found_p)
5284 {
5285 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5286 set_iterator_to_next (it, 0);
5287 }
5288 }
5289 }
5290
5291 it->selective = old_selective;
5292 return newline_found_p;
5293 }
5294
5295
5296 /* Set IT's current position to the previous visible line start. Skip
5297 invisible text that is so either due to text properties or due to
5298 selective display. Caution: this does not change IT->current_x and
5299 IT->hpos. */
5300
5301 static void
5302 back_to_previous_visible_line_start (it)
5303 struct it *it;
5304 {
5305 while (IT_CHARPOS (*it) > BEGV)
5306 {
5307 back_to_previous_line_start (it);
5308
5309 if (IT_CHARPOS (*it) <= BEGV)
5310 break;
5311
5312 /* If selective > 0, then lines indented more than that values
5313 are invisible. */
5314 if (it->selective > 0
5315 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5316 (double) it->selective)) /* iftc */
5317 continue;
5318
5319 /* Check the newline before point for invisibility. */
5320 {
5321 Lisp_Object prop;
5322 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5323 Qinvisible, it->window);
5324 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5325 continue;
5326 }
5327
5328 if (IT_CHARPOS (*it) <= BEGV)
5329 break;
5330
5331 {
5332 struct it it2;
5333 int pos;
5334 EMACS_INT beg, end;
5335 Lisp_Object val, overlay;
5336
5337 /* If newline is part of a composition, continue from start of composition */
5338 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5339 && beg < IT_CHARPOS (*it))
5340 goto replaced;
5341
5342 /* If newline is replaced by a display property, find start of overlay
5343 or interval and continue search from that point. */
5344 it2 = *it;
5345 pos = --IT_CHARPOS (it2);
5346 --IT_BYTEPOS (it2);
5347 it2.sp = 0;
5348 it2.string_from_display_prop_p = 0;
5349 if (handle_display_prop (&it2) == HANDLED_RETURN
5350 && !NILP (val = get_char_property_and_overlay
5351 (make_number (pos), Qdisplay, Qnil, &overlay))
5352 && (OVERLAYP (overlay)
5353 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5354 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5355 goto replaced;
5356
5357 /* Newline is not replaced by anything -- so we are done. */
5358 break;
5359
5360 replaced:
5361 if (beg < BEGV)
5362 beg = BEGV;
5363 IT_CHARPOS (*it) = beg;
5364 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5365 }
5366 }
5367
5368 it->continuation_lines_width = 0;
5369
5370 xassert (IT_CHARPOS (*it) >= BEGV);
5371 xassert (IT_CHARPOS (*it) == BEGV
5372 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5373 CHECK_IT (it);
5374 }
5375
5376
5377 /* Reseat iterator IT at the previous visible line start. Skip
5378 invisible text that is so either due to text properties or due to
5379 selective display. At the end, update IT's overlay information,
5380 face information etc. */
5381
5382 void
5383 reseat_at_previous_visible_line_start (it)
5384 struct it *it;
5385 {
5386 back_to_previous_visible_line_start (it);
5387 reseat (it, it->current.pos, 1);
5388 CHECK_IT (it);
5389 }
5390
5391
5392 /* Reseat iterator IT on the next visible line start in the current
5393 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5394 preceding the line start. Skip over invisible text that is so
5395 because of selective display. Compute faces, overlays etc at the
5396 new position. Note that this function does not skip over text that
5397 is invisible because of text properties. */
5398
5399 static void
5400 reseat_at_next_visible_line_start (it, on_newline_p)
5401 struct it *it;
5402 int on_newline_p;
5403 {
5404 int newline_found_p, skipped_p = 0;
5405
5406 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5407
5408 /* Skip over lines that are invisible because they are indented
5409 more than the value of IT->selective. */
5410 if (it->selective > 0)
5411 while (IT_CHARPOS (*it) < ZV
5412 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5413 (double) it->selective)) /* iftc */
5414 {
5415 xassert (IT_BYTEPOS (*it) == BEGV
5416 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5417 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5418 }
5419
5420 /* Position on the newline if that's what's requested. */
5421 if (on_newline_p && newline_found_p)
5422 {
5423 if (STRINGP (it->string))
5424 {
5425 if (IT_STRING_CHARPOS (*it) > 0)
5426 {
5427 --IT_STRING_CHARPOS (*it);
5428 --IT_STRING_BYTEPOS (*it);
5429 }
5430 }
5431 else if (IT_CHARPOS (*it) > BEGV)
5432 {
5433 --IT_CHARPOS (*it);
5434 --IT_BYTEPOS (*it);
5435 reseat (it, it->current.pos, 0);
5436 }
5437 }
5438 else if (skipped_p)
5439 reseat (it, it->current.pos, 0);
5440
5441 CHECK_IT (it);
5442 }
5443
5444
5445 \f
5446 /***********************************************************************
5447 Changing an iterator's position
5448 ***********************************************************************/
5449
5450 /* Change IT's current position to POS in current_buffer. If FORCE_P
5451 is non-zero, always check for text properties at the new position.
5452 Otherwise, text properties are only looked up if POS >=
5453 IT->check_charpos of a property. */
5454
5455 static void
5456 reseat (it, pos, force_p)
5457 struct it *it;
5458 struct text_pos pos;
5459 int force_p;
5460 {
5461 int original_pos = IT_CHARPOS (*it);
5462
5463 reseat_1 (it, pos, 0);
5464
5465 /* Determine where to check text properties. Avoid doing it
5466 where possible because text property lookup is very expensive. */
5467 if (force_p
5468 || CHARPOS (pos) > it->stop_charpos
5469 || CHARPOS (pos) < original_pos)
5470 handle_stop (it);
5471
5472 CHECK_IT (it);
5473 }
5474
5475
5476 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5477 IT->stop_pos to POS, also. */
5478
5479 static void
5480 reseat_1 (it, pos, set_stop_p)
5481 struct it *it;
5482 struct text_pos pos;
5483 int set_stop_p;
5484 {
5485 /* Don't call this function when scanning a C string. */
5486 xassert (it->s == NULL);
5487
5488 /* POS must be a reasonable value. */
5489 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5490
5491 it->current.pos = it->position = pos;
5492 it->end_charpos = ZV;
5493 it->dpvec = NULL;
5494 it->current.dpvec_index = -1;
5495 it->current.overlay_string_index = -1;
5496 IT_STRING_CHARPOS (*it) = -1;
5497 IT_STRING_BYTEPOS (*it) = -1;
5498 it->string = Qnil;
5499 it->string_from_display_prop_p = 0;
5500 it->method = GET_FROM_BUFFER;
5501 it->object = it->w->buffer;
5502 it->area = TEXT_AREA;
5503 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5504 it->sp = 0;
5505 it->string_from_display_prop_p = 0;
5506 it->face_before_selective_p = 0;
5507
5508 if (set_stop_p)
5509 it->stop_charpos = CHARPOS (pos);
5510 }
5511
5512
5513 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5514 If S is non-null, it is a C string to iterate over. Otherwise,
5515 STRING gives a Lisp string to iterate over.
5516
5517 If PRECISION > 0, don't return more then PRECISION number of
5518 characters from the string.
5519
5520 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5521 characters have been returned. FIELD_WIDTH < 0 means an infinite
5522 field width.
5523
5524 MULTIBYTE = 0 means disable processing of multibyte characters,
5525 MULTIBYTE > 0 means enable it,
5526 MULTIBYTE < 0 means use IT->multibyte_p.
5527
5528 IT must be initialized via a prior call to init_iterator before
5529 calling this function. */
5530
5531 static void
5532 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5533 struct it *it;
5534 unsigned char *s;
5535 Lisp_Object string;
5536 int charpos;
5537 int precision, field_width, multibyte;
5538 {
5539 /* No region in strings. */
5540 it->region_beg_charpos = it->region_end_charpos = -1;
5541
5542 /* No text property checks performed by default, but see below. */
5543 it->stop_charpos = -1;
5544
5545 /* Set iterator position and end position. */
5546 bzero (&it->current, sizeof it->current);
5547 it->current.overlay_string_index = -1;
5548 it->current.dpvec_index = -1;
5549 xassert (charpos >= 0);
5550
5551 /* If STRING is specified, use its multibyteness, otherwise use the
5552 setting of MULTIBYTE, if specified. */
5553 if (multibyte >= 0)
5554 it->multibyte_p = multibyte > 0;
5555
5556 if (s == NULL)
5557 {
5558 xassert (STRINGP (string));
5559 it->string = string;
5560 it->s = NULL;
5561 it->end_charpos = it->string_nchars = SCHARS (string);
5562 it->method = GET_FROM_STRING;
5563 it->current.string_pos = string_pos (charpos, string);
5564 }
5565 else
5566 {
5567 it->s = s;
5568 it->string = Qnil;
5569
5570 /* Note that we use IT->current.pos, not it->current.string_pos,
5571 for displaying C strings. */
5572 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5573 if (it->multibyte_p)
5574 {
5575 it->current.pos = c_string_pos (charpos, s, 1);
5576 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5577 }
5578 else
5579 {
5580 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5581 it->end_charpos = it->string_nchars = strlen (s);
5582 }
5583
5584 it->method = GET_FROM_C_STRING;
5585 }
5586
5587 /* PRECISION > 0 means don't return more than PRECISION characters
5588 from the string. */
5589 if (precision > 0 && it->end_charpos - charpos > precision)
5590 it->end_charpos = it->string_nchars = charpos + precision;
5591
5592 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5593 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5594 FIELD_WIDTH < 0 means infinite field width. This is useful for
5595 padding with `-' at the end of a mode line. */
5596 if (field_width < 0)
5597 field_width = INFINITY;
5598 if (field_width > it->end_charpos - charpos)
5599 it->end_charpos = charpos + field_width;
5600
5601 /* Use the standard display table for displaying strings. */
5602 if (DISP_TABLE_P (Vstandard_display_table))
5603 it->dp = XCHAR_TABLE (Vstandard_display_table);
5604
5605 it->stop_charpos = charpos;
5606 CHECK_IT (it);
5607 }
5608
5609
5610 \f
5611 /***********************************************************************
5612 Iteration
5613 ***********************************************************************/
5614
5615 /* Map enum it_method value to corresponding next_element_from_* function. */
5616
5617 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5618 {
5619 next_element_from_buffer,
5620 next_element_from_display_vector,
5621 next_element_from_string,
5622 next_element_from_c_string,
5623 next_element_from_image,
5624 next_element_from_stretch
5625 };
5626
5627 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5628
5629
5630 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5631 (possibly with the following characters). */
5632
5633 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5634 ((IT)->cmp_it.id >= 0 \
5635 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5636 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5637 (IT)->end_charpos, (IT)->w, \
5638 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5639 (IT)->string)))
5640
5641
5642 /* Load IT's display element fields with information about the next
5643 display element from the current position of IT. Value is zero if
5644 end of buffer (or C string) is reached. */
5645
5646 static struct frame *last_escape_glyph_frame = NULL;
5647 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5648 static int last_escape_glyph_merged_face_id = 0;
5649
5650 int
5651 get_next_display_element (it)
5652 struct it *it;
5653 {
5654 /* Non-zero means that we found a display element. Zero means that
5655 we hit the end of what we iterate over. Performance note: the
5656 function pointer `method' used here turns out to be faster than
5657 using a sequence of if-statements. */
5658 int success_p;
5659
5660 get_next:
5661 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5662
5663 if (it->what == IT_CHARACTER)
5664 {
5665 /* Map via display table or translate control characters.
5666 IT->c, IT->len etc. have been set to the next character by
5667 the function call above. If we have a display table, and it
5668 contains an entry for IT->c, translate it. Don't do this if
5669 IT->c itself comes from a display table, otherwise we could
5670 end up in an infinite recursion. (An alternative could be to
5671 count the recursion depth of this function and signal an
5672 error when a certain maximum depth is reached.) Is it worth
5673 it? */
5674 if (success_p && it->dpvec == NULL)
5675 {
5676 Lisp_Object dv;
5677 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5678
5679 if (it->dp
5680 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5681 VECTORP (dv)))
5682 {
5683 struct Lisp_Vector *v = XVECTOR (dv);
5684
5685 /* Return the first character from the display table
5686 entry, if not empty. If empty, don't display the
5687 current character. */
5688 if (v->size)
5689 {
5690 it->dpvec_char_len = it->len;
5691 it->dpvec = v->contents;
5692 it->dpend = v->contents + v->size;
5693 it->current.dpvec_index = 0;
5694 it->dpvec_face_id = -1;
5695 it->saved_face_id = it->face_id;
5696 it->method = GET_FROM_DISPLAY_VECTOR;
5697 it->ellipsis_p = 0;
5698 }
5699 else
5700 {
5701 set_iterator_to_next (it, 0);
5702 }
5703 goto get_next;
5704 }
5705
5706 /* Translate control characters into `\003' or `^C' form.
5707 Control characters coming from a display table entry are
5708 currently not translated because we use IT->dpvec to hold
5709 the translation. This could easily be changed but I
5710 don't believe that it is worth doing.
5711
5712 If it->multibyte_p is nonzero, non-printable non-ASCII
5713 characters are also translated to octal form.
5714
5715 If it->multibyte_p is zero, eight-bit characters that
5716 don't have corresponding multibyte char code are also
5717 translated to octal form. */
5718 else if ((it->c < ' '
5719 ? (it->area != TEXT_AREA
5720 /* In mode line, treat \n, \t like other crl chars. */
5721 || (it->c != '\t'
5722 && it->glyph_row
5723 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5724 || (it->c != '\n' && it->c != '\t'))
5725 : (it->multibyte_p
5726 ? (!CHAR_PRINTABLE_P (it->c)
5727 || (!NILP (Vnobreak_char_display)
5728 && (it->c == 0xA0 /* NO-BREAK SPACE */
5729 || it->c == 0xAD /* SOFT HYPHEN */)))
5730 : (it->c >= 127
5731 && (! unibyte_display_via_language_environment
5732 || (DECODE_CHAR (unibyte, it->c) <= 0xA0))))))
5733 {
5734 /* IT->c is a control character which must be displayed
5735 either as '\003' or as `^C' where the '\\' and '^'
5736 can be defined in the display table. Fill
5737 IT->ctl_chars with glyphs for what we have to
5738 display. Then, set IT->dpvec to these glyphs. */
5739 Lisp_Object gc;
5740 int ctl_len;
5741 int face_id, lface_id = 0 ;
5742 int escape_glyph;
5743
5744 /* Handle control characters with ^. */
5745
5746 if (it->c < 128 && it->ctl_arrow_p)
5747 {
5748 int g;
5749
5750 g = '^'; /* default glyph for Control */
5751 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5752 if (it->dp
5753 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5754 && GLYPH_CODE_CHAR_VALID_P (gc))
5755 {
5756 g = GLYPH_CODE_CHAR (gc);
5757 lface_id = GLYPH_CODE_FACE (gc);
5758 }
5759 if (lface_id)
5760 {
5761 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5762 }
5763 else if (it->f == last_escape_glyph_frame
5764 && it->face_id == last_escape_glyph_face_id)
5765 {
5766 face_id = last_escape_glyph_merged_face_id;
5767 }
5768 else
5769 {
5770 /* Merge the escape-glyph face into the current face. */
5771 face_id = merge_faces (it->f, Qescape_glyph, 0,
5772 it->face_id);
5773 last_escape_glyph_frame = it->f;
5774 last_escape_glyph_face_id = it->face_id;
5775 last_escape_glyph_merged_face_id = face_id;
5776 }
5777
5778 XSETINT (it->ctl_chars[0], g);
5779 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5780 ctl_len = 2;
5781 goto display_control;
5782 }
5783
5784 /* Handle non-break space in the mode where it only gets
5785 highlighting. */
5786
5787 if (EQ (Vnobreak_char_display, Qt)
5788 && it->c == 0xA0)
5789 {
5790 /* Merge the no-break-space face into the current face. */
5791 face_id = merge_faces (it->f, Qnobreak_space, 0,
5792 it->face_id);
5793
5794 it->c = ' ';
5795 XSETINT (it->ctl_chars[0], ' ');
5796 ctl_len = 1;
5797 goto display_control;
5798 }
5799
5800 /* Handle sequences that start with the "escape glyph". */
5801
5802 /* the default escape glyph is \. */
5803 escape_glyph = '\\';
5804
5805 if (it->dp
5806 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5807 && GLYPH_CODE_CHAR_VALID_P (gc))
5808 {
5809 escape_glyph = GLYPH_CODE_CHAR (gc);
5810 lface_id = GLYPH_CODE_FACE (gc);
5811 }
5812 if (lface_id)
5813 {
5814 /* The display table specified a face.
5815 Merge it into face_id and also into escape_glyph. */
5816 face_id = merge_faces (it->f, Qt, lface_id,
5817 it->face_id);
5818 }
5819 else if (it->f == last_escape_glyph_frame
5820 && it->face_id == last_escape_glyph_face_id)
5821 {
5822 face_id = last_escape_glyph_merged_face_id;
5823 }
5824 else
5825 {
5826 /* Merge the escape-glyph face into the current face. */
5827 face_id = merge_faces (it->f, Qescape_glyph, 0,
5828 it->face_id);
5829 last_escape_glyph_frame = it->f;
5830 last_escape_glyph_face_id = it->face_id;
5831 last_escape_glyph_merged_face_id = face_id;
5832 }
5833
5834 /* Handle soft hyphens in the mode where they only get
5835 highlighting. */
5836
5837 if (EQ (Vnobreak_char_display, Qt)
5838 && it->c == 0xAD)
5839 {
5840 it->c = '-';
5841 XSETINT (it->ctl_chars[0], '-');
5842 ctl_len = 1;
5843 goto display_control;
5844 }
5845
5846 /* Handle non-break space and soft hyphen
5847 with the escape glyph. */
5848
5849 if (it->c == 0xA0 || it->c == 0xAD)
5850 {
5851 XSETINT (it->ctl_chars[0], escape_glyph);
5852 it->c = (it->c == 0xA0 ? ' ' : '-');
5853 XSETINT (it->ctl_chars[1], it->c);
5854 ctl_len = 2;
5855 goto display_control;
5856 }
5857
5858 {
5859 unsigned char str[MAX_MULTIBYTE_LENGTH];
5860 int len;
5861 int i;
5862
5863 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5864 if (CHAR_BYTE8_P (it->c))
5865 {
5866 str[0] = CHAR_TO_BYTE8 (it->c);
5867 len = 1;
5868 }
5869 else if (it->c < 256)
5870 {
5871 str[0] = it->c;
5872 len = 1;
5873 }
5874 else
5875 {
5876 /* It's an invalid character, which shouldn't
5877 happen actually, but due to bugs it may
5878 happen. Let's print the char as is, there's
5879 not much meaningful we can do with it. */
5880 str[0] = it->c;
5881 str[1] = it->c >> 8;
5882 str[2] = it->c >> 16;
5883 str[3] = it->c >> 24;
5884 len = 4;
5885 }
5886
5887 for (i = 0; i < len; i++)
5888 {
5889 int g;
5890 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5891 /* Insert three more glyphs into IT->ctl_chars for
5892 the octal display of the character. */
5893 g = ((str[i] >> 6) & 7) + '0';
5894 XSETINT (it->ctl_chars[i * 4 + 1], g);
5895 g = ((str[i] >> 3) & 7) + '0';
5896 XSETINT (it->ctl_chars[i * 4 + 2], g);
5897 g = (str[i] & 7) + '0';
5898 XSETINT (it->ctl_chars[i * 4 + 3], g);
5899 }
5900 ctl_len = len * 4;
5901 }
5902
5903 display_control:
5904 /* Set up IT->dpvec and return first character from it. */
5905 it->dpvec_char_len = it->len;
5906 it->dpvec = it->ctl_chars;
5907 it->dpend = it->dpvec + ctl_len;
5908 it->current.dpvec_index = 0;
5909 it->dpvec_face_id = face_id;
5910 it->saved_face_id = it->face_id;
5911 it->method = GET_FROM_DISPLAY_VECTOR;
5912 it->ellipsis_p = 0;
5913 goto get_next;
5914 }
5915 }
5916 }
5917
5918 #ifdef HAVE_WINDOW_SYSTEM
5919 /* Adjust face id for a multibyte character. There are no multibyte
5920 character in unibyte text. */
5921 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5922 && it->multibyte_p
5923 && success_p
5924 && FRAME_WINDOW_P (it->f))
5925 {
5926 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5927
5928 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5929 {
5930 /* Automatic composition with glyph-string. */
5931 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5932
5933 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5934 }
5935 else
5936 {
5937 int pos = (it->s ? -1
5938 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5939 : IT_CHARPOS (*it));
5940
5941 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5942 }
5943 }
5944 #endif
5945
5946 /* Is this character the last one of a run of characters with
5947 box? If yes, set IT->end_of_box_run_p to 1. */
5948 if (it->face_box_p
5949 && it->s == NULL)
5950 {
5951 if (it->method == GET_FROM_STRING && it->sp)
5952 {
5953 int face_id = underlying_face_id (it);
5954 struct face *face = FACE_FROM_ID (it->f, face_id);
5955
5956 if (face)
5957 {
5958 if (face->box == FACE_NO_BOX)
5959 {
5960 /* If the box comes from face properties in a
5961 display string, check faces in that string. */
5962 int string_face_id = face_after_it_pos (it);
5963 it->end_of_box_run_p
5964 = (FACE_FROM_ID (it->f, string_face_id)->box
5965 == FACE_NO_BOX);
5966 }
5967 /* Otherwise, the box comes from the underlying face.
5968 If this is the last string character displayed, check
5969 the next buffer location. */
5970 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5971 && (it->current.overlay_string_index
5972 == it->n_overlay_strings - 1))
5973 {
5974 EMACS_INT ignore;
5975 int next_face_id;
5976 struct text_pos pos = it->current.pos;
5977 INC_TEXT_POS (pos, it->multibyte_p);
5978
5979 next_face_id = face_at_buffer_position
5980 (it->w, CHARPOS (pos), it->region_beg_charpos,
5981 it->region_end_charpos, &ignore,
5982 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5983 -1);
5984 it->end_of_box_run_p
5985 = (FACE_FROM_ID (it->f, next_face_id)->box
5986 == FACE_NO_BOX);
5987 }
5988 }
5989 }
5990 else
5991 {
5992 int face_id = face_after_it_pos (it);
5993 it->end_of_box_run_p
5994 = (face_id != it->face_id
5995 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
5996 }
5997 }
5998
5999 /* Value is 0 if end of buffer or string reached. */
6000 return success_p;
6001 }
6002
6003
6004 /* Move IT to the next display element.
6005
6006 RESEAT_P non-zero means if called on a newline in buffer text,
6007 skip to the next visible line start.
6008
6009 Functions get_next_display_element and set_iterator_to_next are
6010 separate because I find this arrangement easier to handle than a
6011 get_next_display_element function that also increments IT's
6012 position. The way it is we can first look at an iterator's current
6013 display element, decide whether it fits on a line, and if it does,
6014 increment the iterator position. The other way around we probably
6015 would either need a flag indicating whether the iterator has to be
6016 incremented the next time, or we would have to implement a
6017 decrement position function which would not be easy to write. */
6018
6019 void
6020 set_iterator_to_next (it, reseat_p)
6021 struct it *it;
6022 int reseat_p;
6023 {
6024 /* Reset flags indicating start and end of a sequence of characters
6025 with box. Reset them at the start of this function because
6026 moving the iterator to a new position might set them. */
6027 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6028
6029 switch (it->method)
6030 {
6031 case GET_FROM_BUFFER:
6032 /* The current display element of IT is a character from
6033 current_buffer. Advance in the buffer, and maybe skip over
6034 invisible lines that are so because of selective display. */
6035 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6036 reseat_at_next_visible_line_start (it, 0);
6037 else if (it->cmp_it.id >= 0)
6038 {
6039 IT_CHARPOS (*it) += it->cmp_it.nchars;
6040 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6041 if (it->cmp_it.to < it->cmp_it.nglyphs)
6042 it->cmp_it.from = it->cmp_it.to;
6043 else
6044 {
6045 it->cmp_it.id = -1;
6046 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6047 IT_BYTEPOS (*it), it->stop_charpos,
6048 Qnil);
6049 }
6050 }
6051 else
6052 {
6053 xassert (it->len != 0);
6054 IT_BYTEPOS (*it) += it->len;
6055 IT_CHARPOS (*it) += 1;
6056 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6057 }
6058 break;
6059
6060 case GET_FROM_C_STRING:
6061 /* Current display element of IT is from a C string. */
6062 IT_BYTEPOS (*it) += it->len;
6063 IT_CHARPOS (*it) += 1;
6064 break;
6065
6066 case GET_FROM_DISPLAY_VECTOR:
6067 /* Current display element of IT is from a display table entry.
6068 Advance in the display table definition. Reset it to null if
6069 end reached, and continue with characters from buffers/
6070 strings. */
6071 ++it->current.dpvec_index;
6072
6073 /* Restore face of the iterator to what they were before the
6074 display vector entry (these entries may contain faces). */
6075 it->face_id = it->saved_face_id;
6076
6077 if (it->dpvec + it->current.dpvec_index == it->dpend)
6078 {
6079 int recheck_faces = it->ellipsis_p;
6080
6081 if (it->s)
6082 it->method = GET_FROM_C_STRING;
6083 else if (STRINGP (it->string))
6084 it->method = GET_FROM_STRING;
6085 else
6086 {
6087 it->method = GET_FROM_BUFFER;
6088 it->object = it->w->buffer;
6089 }
6090
6091 it->dpvec = NULL;
6092 it->current.dpvec_index = -1;
6093
6094 /* Skip over characters which were displayed via IT->dpvec. */
6095 if (it->dpvec_char_len < 0)
6096 reseat_at_next_visible_line_start (it, 1);
6097 else if (it->dpvec_char_len > 0)
6098 {
6099 if (it->method == GET_FROM_STRING
6100 && it->n_overlay_strings > 0)
6101 it->ignore_overlay_strings_at_pos_p = 1;
6102 it->len = it->dpvec_char_len;
6103 set_iterator_to_next (it, reseat_p);
6104 }
6105
6106 /* Maybe recheck faces after display vector */
6107 if (recheck_faces)
6108 it->stop_charpos = IT_CHARPOS (*it);
6109 }
6110 break;
6111
6112 case GET_FROM_STRING:
6113 /* Current display element is a character from a Lisp string. */
6114 xassert (it->s == NULL && STRINGP (it->string));
6115 if (it->cmp_it.id >= 0)
6116 {
6117 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6118 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6119 if (it->cmp_it.to < it->cmp_it.nglyphs)
6120 it->cmp_it.from = it->cmp_it.to;
6121 else
6122 {
6123 it->cmp_it.id = -1;
6124 composition_compute_stop_pos (&it->cmp_it,
6125 IT_STRING_CHARPOS (*it),
6126 IT_STRING_BYTEPOS (*it),
6127 it->stop_charpos, it->string);
6128 }
6129 }
6130 else
6131 {
6132 IT_STRING_BYTEPOS (*it) += it->len;
6133 IT_STRING_CHARPOS (*it) += 1;
6134 }
6135
6136 consider_string_end:
6137
6138 if (it->current.overlay_string_index >= 0)
6139 {
6140 /* IT->string is an overlay string. Advance to the
6141 next, if there is one. */
6142 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6143 {
6144 it->ellipsis_p = 0;
6145 next_overlay_string (it);
6146 if (it->ellipsis_p)
6147 setup_for_ellipsis (it, 0);
6148 }
6149 }
6150 else
6151 {
6152 /* IT->string is not an overlay string. If we reached
6153 its end, and there is something on IT->stack, proceed
6154 with what is on the stack. This can be either another
6155 string, this time an overlay string, or a buffer. */
6156 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6157 && it->sp > 0)
6158 {
6159 pop_it (it);
6160 if (it->method == GET_FROM_STRING)
6161 goto consider_string_end;
6162 }
6163 }
6164 break;
6165
6166 case GET_FROM_IMAGE:
6167 case GET_FROM_STRETCH:
6168 /* The position etc with which we have to proceed are on
6169 the stack. The position may be at the end of a string,
6170 if the `display' property takes up the whole string. */
6171 xassert (it->sp > 0);
6172 pop_it (it);
6173 if (it->method == GET_FROM_STRING)
6174 goto consider_string_end;
6175 break;
6176
6177 default:
6178 /* There are no other methods defined, so this should be a bug. */
6179 abort ();
6180 }
6181
6182 xassert (it->method != GET_FROM_STRING
6183 || (STRINGP (it->string)
6184 && IT_STRING_CHARPOS (*it) >= 0));
6185 }
6186
6187 /* Load IT's display element fields with information about the next
6188 display element which comes from a display table entry or from the
6189 result of translating a control character to one of the forms `^C'
6190 or `\003'.
6191
6192 IT->dpvec holds the glyphs to return as characters.
6193 IT->saved_face_id holds the face id before the display vector--
6194 it is restored into IT->face_idin set_iterator_to_next. */
6195
6196 static int
6197 next_element_from_display_vector (it)
6198 struct it *it;
6199 {
6200 Lisp_Object gc;
6201
6202 /* Precondition. */
6203 xassert (it->dpvec && it->current.dpvec_index >= 0);
6204
6205 it->face_id = it->saved_face_id;
6206
6207 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6208 That seemed totally bogus - so I changed it... */
6209 gc = it->dpvec[it->current.dpvec_index];
6210
6211 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6212 {
6213 it->c = GLYPH_CODE_CHAR (gc);
6214 it->len = CHAR_BYTES (it->c);
6215
6216 /* The entry may contain a face id to use. Such a face id is
6217 the id of a Lisp face, not a realized face. A face id of
6218 zero means no face is specified. */
6219 if (it->dpvec_face_id >= 0)
6220 it->face_id = it->dpvec_face_id;
6221 else
6222 {
6223 int lface_id = GLYPH_CODE_FACE (gc);
6224 if (lface_id > 0)
6225 it->face_id = merge_faces (it->f, Qt, lface_id,
6226 it->saved_face_id);
6227 }
6228 }
6229 else
6230 /* Display table entry is invalid. Return a space. */
6231 it->c = ' ', it->len = 1;
6232
6233 /* Don't change position and object of the iterator here. They are
6234 still the values of the character that had this display table
6235 entry or was translated, and that's what we want. */
6236 it->what = IT_CHARACTER;
6237 return 1;
6238 }
6239
6240
6241 /* Load IT with the next display element from Lisp string IT->string.
6242 IT->current.string_pos is the current position within the string.
6243 If IT->current.overlay_string_index >= 0, the Lisp string is an
6244 overlay string. */
6245
6246 static int
6247 next_element_from_string (it)
6248 struct it *it;
6249 {
6250 struct text_pos position;
6251
6252 xassert (STRINGP (it->string));
6253 xassert (IT_STRING_CHARPOS (*it) >= 0);
6254 position = it->current.string_pos;
6255
6256 /* Time to check for invisible text? */
6257 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6258 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6259 {
6260 handle_stop (it);
6261
6262 /* Since a handler may have changed IT->method, we must
6263 recurse here. */
6264 return GET_NEXT_DISPLAY_ELEMENT (it);
6265 }
6266
6267 if (it->current.overlay_string_index >= 0)
6268 {
6269 /* Get the next character from an overlay string. In overlay
6270 strings, There is no field width or padding with spaces to
6271 do. */
6272 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6273 {
6274 it->what = IT_EOB;
6275 return 0;
6276 }
6277 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6278 IT_STRING_BYTEPOS (*it))
6279 && next_element_from_composition (it))
6280 {
6281 return 1;
6282 }
6283 else if (STRING_MULTIBYTE (it->string))
6284 {
6285 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6286 const unsigned char *s = (SDATA (it->string)
6287 + IT_STRING_BYTEPOS (*it));
6288 it->c = string_char_and_length (s, remaining, &it->len);
6289 }
6290 else
6291 {
6292 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6293 it->len = 1;
6294 }
6295 }
6296 else
6297 {
6298 /* Get the next character from a Lisp string that is not an
6299 overlay string. Such strings come from the mode line, for
6300 example. We may have to pad with spaces, or truncate the
6301 string. See also next_element_from_c_string. */
6302 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6303 {
6304 it->what = IT_EOB;
6305 return 0;
6306 }
6307 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6308 {
6309 /* Pad with spaces. */
6310 it->c = ' ', it->len = 1;
6311 CHARPOS (position) = BYTEPOS (position) = -1;
6312 }
6313 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6314 IT_STRING_BYTEPOS (*it))
6315 && next_element_from_composition (it))
6316 {
6317 return 1;
6318 }
6319 else if (STRING_MULTIBYTE (it->string))
6320 {
6321 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6322 const unsigned char *s = (SDATA (it->string)
6323 + IT_STRING_BYTEPOS (*it));
6324 it->c = string_char_and_length (s, maxlen, &it->len);
6325 }
6326 else
6327 {
6328 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6329 it->len = 1;
6330 }
6331 }
6332
6333 /* Record what we have and where it came from. */
6334 it->what = IT_CHARACTER;
6335 it->object = it->string;
6336 it->position = position;
6337 return 1;
6338 }
6339
6340
6341 /* Load IT with next display element from C string IT->s.
6342 IT->string_nchars is the maximum number of characters to return
6343 from the string. IT->end_charpos may be greater than
6344 IT->string_nchars when this function is called, in which case we
6345 may have to return padding spaces. Value is zero if end of string
6346 reached, including padding spaces. */
6347
6348 static int
6349 next_element_from_c_string (it)
6350 struct it *it;
6351 {
6352 int success_p = 1;
6353
6354 xassert (it->s);
6355 it->what = IT_CHARACTER;
6356 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6357 it->object = Qnil;
6358
6359 /* IT's position can be greater IT->string_nchars in case a field
6360 width or precision has been specified when the iterator was
6361 initialized. */
6362 if (IT_CHARPOS (*it) >= it->end_charpos)
6363 {
6364 /* End of the game. */
6365 it->what = IT_EOB;
6366 success_p = 0;
6367 }
6368 else if (IT_CHARPOS (*it) >= it->string_nchars)
6369 {
6370 /* Pad with spaces. */
6371 it->c = ' ', it->len = 1;
6372 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6373 }
6374 else if (it->multibyte_p)
6375 {
6376 /* Implementation note: The calls to strlen apparently aren't a
6377 performance problem because there is no noticeable performance
6378 difference between Emacs running in unibyte or multibyte mode. */
6379 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6380 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6381 maxlen, &it->len);
6382 }
6383 else
6384 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6385
6386 return success_p;
6387 }
6388
6389
6390 /* Set up IT to return characters from an ellipsis, if appropriate.
6391 The definition of the ellipsis glyphs may come from a display table
6392 entry. This function Fills IT with the first glyph from the
6393 ellipsis if an ellipsis is to be displayed. */
6394
6395 static int
6396 next_element_from_ellipsis (it)
6397 struct it *it;
6398 {
6399 if (it->selective_display_ellipsis_p)
6400 setup_for_ellipsis (it, it->len);
6401 else
6402 {
6403 /* The face at the current position may be different from the
6404 face we find after the invisible text. Remember what it
6405 was in IT->saved_face_id, and signal that it's there by
6406 setting face_before_selective_p. */
6407 it->saved_face_id = it->face_id;
6408 it->method = GET_FROM_BUFFER;
6409 it->object = it->w->buffer;
6410 reseat_at_next_visible_line_start (it, 1);
6411 it->face_before_selective_p = 1;
6412 }
6413
6414 return GET_NEXT_DISPLAY_ELEMENT (it);
6415 }
6416
6417
6418 /* Deliver an image display element. The iterator IT is already
6419 filled with image information (done in handle_display_prop). Value
6420 is always 1. */
6421
6422
6423 static int
6424 next_element_from_image (it)
6425 struct it *it;
6426 {
6427 it->what = IT_IMAGE;
6428 return 1;
6429 }
6430
6431
6432 /* Fill iterator IT with next display element from a stretch glyph
6433 property. IT->object is the value of the text property. Value is
6434 always 1. */
6435
6436 static int
6437 next_element_from_stretch (it)
6438 struct it *it;
6439 {
6440 it->what = IT_STRETCH;
6441 return 1;
6442 }
6443
6444
6445 /* Load IT with the next display element from current_buffer. Value
6446 is zero if end of buffer reached. IT->stop_charpos is the next
6447 position at which to stop and check for text properties or buffer
6448 end. */
6449
6450 static int
6451 next_element_from_buffer (it)
6452 struct it *it;
6453 {
6454 int success_p = 1;
6455
6456 xassert (IT_CHARPOS (*it) >= BEGV);
6457
6458 if (IT_CHARPOS (*it) >= it->stop_charpos)
6459 {
6460 if (IT_CHARPOS (*it) >= it->end_charpos)
6461 {
6462 int overlay_strings_follow_p;
6463
6464 /* End of the game, except when overlay strings follow that
6465 haven't been returned yet. */
6466 if (it->overlay_strings_at_end_processed_p)
6467 overlay_strings_follow_p = 0;
6468 else
6469 {
6470 it->overlay_strings_at_end_processed_p = 1;
6471 overlay_strings_follow_p = get_overlay_strings (it, 0);
6472 }
6473
6474 if (overlay_strings_follow_p)
6475 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6476 else
6477 {
6478 it->what = IT_EOB;
6479 it->position = it->current.pos;
6480 success_p = 0;
6481 }
6482 }
6483 else
6484 {
6485 handle_stop (it);
6486 return GET_NEXT_DISPLAY_ELEMENT (it);
6487 }
6488 }
6489 else
6490 {
6491 /* No face changes, overlays etc. in sight, so just return a
6492 character from current_buffer. */
6493 unsigned char *p;
6494
6495 /* Maybe run the redisplay end trigger hook. Performance note:
6496 This doesn't seem to cost measurable time. */
6497 if (it->redisplay_end_trigger_charpos
6498 && it->glyph_row
6499 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6500 run_redisplay_end_trigger_hook (it);
6501
6502 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6503 && next_element_from_composition (it))
6504 {
6505 return 1;
6506 }
6507
6508 /* Get the next character, maybe multibyte. */
6509 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6510 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6511 it->c = STRING_CHAR_AND_LENGTH (p, 0, it->len);
6512 else
6513 it->c = *p, it->len = 1;
6514
6515 /* Record what we have and where it came from. */
6516 it->what = IT_CHARACTER;
6517 it->object = it->w->buffer;
6518 it->position = it->current.pos;
6519
6520 /* Normally we return the character found above, except when we
6521 really want to return an ellipsis for selective display. */
6522 if (it->selective)
6523 {
6524 if (it->c == '\n')
6525 {
6526 /* A value of selective > 0 means hide lines indented more
6527 than that number of columns. */
6528 if (it->selective > 0
6529 && IT_CHARPOS (*it) + 1 < ZV
6530 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6531 IT_BYTEPOS (*it) + 1,
6532 (double) it->selective)) /* iftc */
6533 {
6534 success_p = next_element_from_ellipsis (it);
6535 it->dpvec_char_len = -1;
6536 }
6537 }
6538 else if (it->c == '\r' && it->selective == -1)
6539 {
6540 /* A value of selective == -1 means that everything from the
6541 CR to the end of the line is invisible, with maybe an
6542 ellipsis displayed for it. */
6543 success_p = next_element_from_ellipsis (it);
6544 it->dpvec_char_len = -1;
6545 }
6546 }
6547 }
6548
6549 /* Value is zero if end of buffer reached. */
6550 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6551 return success_p;
6552 }
6553
6554
6555 /* Run the redisplay end trigger hook for IT. */
6556
6557 static void
6558 run_redisplay_end_trigger_hook (it)
6559 struct it *it;
6560 {
6561 Lisp_Object args[3];
6562
6563 /* IT->glyph_row should be non-null, i.e. we should be actually
6564 displaying something, or otherwise we should not run the hook. */
6565 xassert (it->glyph_row);
6566
6567 /* Set up hook arguments. */
6568 args[0] = Qredisplay_end_trigger_functions;
6569 args[1] = it->window;
6570 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6571 it->redisplay_end_trigger_charpos = 0;
6572
6573 /* Since we are *trying* to run these functions, don't try to run
6574 them again, even if they get an error. */
6575 it->w->redisplay_end_trigger = Qnil;
6576 Frun_hook_with_args (3, args);
6577
6578 /* Notice if it changed the face of the character we are on. */
6579 handle_face_prop (it);
6580 }
6581
6582
6583 /* Deliver a composition display element. Unlike the other
6584 next_element_from_XXX, this function is not registered in the array
6585 get_next_element[]. It is called from next_element_from_buffer and
6586 next_element_from_string when necessary. */
6587
6588 static int
6589 next_element_from_composition (it)
6590 struct it *it;
6591 {
6592 it->what = IT_COMPOSITION;
6593 it->len = it->cmp_it.nbytes;
6594 if (STRINGP (it->string))
6595 {
6596 if (it->c < 0)
6597 {
6598 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6599 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6600 return 0;
6601 }
6602 it->position = it->current.string_pos;
6603 it->object = it->string;
6604 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6605 IT_STRING_BYTEPOS (*it), it->string);
6606 }
6607 else
6608 {
6609 if (it->c < 0)
6610 {
6611 IT_CHARPOS (*it) += it->cmp_it.nchars;
6612 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6613 return 0;
6614 }
6615 it->position = it->current.pos;
6616 it->object = it->w->buffer;
6617 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6618 IT_BYTEPOS (*it), Qnil);
6619 }
6620 return 1;
6621 }
6622
6623
6624 \f
6625 /***********************************************************************
6626 Moving an iterator without producing glyphs
6627 ***********************************************************************/
6628
6629 /* Check if iterator is at a position corresponding to a valid buffer
6630 position after some move_it_ call. */
6631
6632 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6633 ((it)->method == GET_FROM_STRING \
6634 ? IT_STRING_CHARPOS (*it) == 0 \
6635 : 1)
6636
6637
6638 /* Move iterator IT to a specified buffer or X position within one
6639 line on the display without producing glyphs.
6640
6641 OP should be a bit mask including some or all of these bits:
6642 MOVE_TO_X: Stop on reaching x-position TO_X.
6643 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6644 Regardless of OP's value, stop in reaching the end of the display line.
6645
6646 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6647 This means, in particular, that TO_X includes window's horizontal
6648 scroll amount.
6649
6650 The return value has several possible values that
6651 say what condition caused the scan to stop:
6652
6653 MOVE_POS_MATCH_OR_ZV
6654 - when TO_POS or ZV was reached.
6655
6656 MOVE_X_REACHED
6657 -when TO_X was reached before TO_POS or ZV were reached.
6658
6659 MOVE_LINE_CONTINUED
6660 - when we reached the end of the display area and the line must
6661 be continued.
6662
6663 MOVE_LINE_TRUNCATED
6664 - when we reached the end of the display area and the line is
6665 truncated.
6666
6667 MOVE_NEWLINE_OR_CR
6668 - when we stopped at a line end, i.e. a newline or a CR and selective
6669 display is on. */
6670
6671 static enum move_it_result
6672 move_it_in_display_line_to (struct it *it,
6673 EMACS_INT to_charpos, int to_x,
6674 enum move_operation_enum op)
6675 {
6676 enum move_it_result result = MOVE_UNDEFINED;
6677 struct glyph_row *saved_glyph_row;
6678 struct it wrap_it, atpos_it, atx_it;
6679 int may_wrap = 0;
6680
6681 /* Don't produce glyphs in produce_glyphs. */
6682 saved_glyph_row = it->glyph_row;
6683 it->glyph_row = NULL;
6684
6685 /* Use wrap_it to save a copy of IT wherever a word wrap could
6686 occur. Use atpos_it to save a copy of IT at the desired buffer
6687 position, if found, so that we can scan ahead and check if the
6688 word later overshoots the window edge. Use atx_it similarly, for
6689 pixel positions. */
6690 wrap_it.sp = -1;
6691 atpos_it.sp = -1;
6692 atx_it.sp = -1;
6693
6694 #define BUFFER_POS_REACHED_P() \
6695 ((op & MOVE_TO_POS) != 0 \
6696 && BUFFERP (it->object) \
6697 && IT_CHARPOS (*it) >= to_charpos \
6698 && (it->method == GET_FROM_BUFFER \
6699 || (it->method == GET_FROM_DISPLAY_VECTOR \
6700 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6701
6702 /* If there's a line-/wrap-prefix, handle it. */
6703 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6704 && it->current_y < it->last_visible_y)
6705 handle_line_prefix (it);
6706
6707 while (1)
6708 {
6709 int x, i, ascent = 0, descent = 0;
6710
6711 /* Utility macro to reset an iterator with x, ascent, and descent. */
6712 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6713 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6714 (IT)->max_descent = descent)
6715
6716 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6717 glyph). */
6718 if ((op & MOVE_TO_POS) != 0
6719 && BUFFERP (it->object)
6720 && it->method == GET_FROM_BUFFER
6721 && IT_CHARPOS (*it) > to_charpos)
6722 {
6723 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6724 {
6725 result = MOVE_POS_MATCH_OR_ZV;
6726 break;
6727 }
6728 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6729 /* If wrap_it is valid, the current position might be in a
6730 word that is wrapped. So, save the iterator in
6731 atpos_it and continue to see if wrapping happens. */
6732 atpos_it = *it;
6733 }
6734
6735 /* Stop when ZV reached.
6736 We used to stop here when TO_CHARPOS reached as well, but that is
6737 too soon if this glyph does not fit on this line. So we handle it
6738 explicitly below. */
6739 if (!get_next_display_element (it))
6740 {
6741 result = MOVE_POS_MATCH_OR_ZV;
6742 break;
6743 }
6744
6745 if (it->line_wrap == TRUNCATE)
6746 {
6747 if (BUFFER_POS_REACHED_P ())
6748 {
6749 result = MOVE_POS_MATCH_OR_ZV;
6750 break;
6751 }
6752 }
6753 else
6754 {
6755 if (it->line_wrap == WORD_WRAP)
6756 {
6757 if (IT_DISPLAYING_WHITESPACE (it))
6758 may_wrap = 1;
6759 else if (may_wrap)
6760 {
6761 /* We have reached a glyph that follows one or more
6762 whitespace characters. If the position is
6763 already found, we are done. */
6764 if (atpos_it.sp >= 0)
6765 {
6766 *it = atpos_it;
6767 result = MOVE_POS_MATCH_OR_ZV;
6768 goto done;
6769 }
6770 if (atx_it.sp >= 0)
6771 {
6772 *it = atx_it;
6773 result = MOVE_X_REACHED;
6774 goto done;
6775 }
6776 /* Otherwise, we can wrap here. */
6777 wrap_it = *it;
6778 may_wrap = 0;
6779 }
6780 }
6781 }
6782
6783 /* Remember the line height for the current line, in case
6784 the next element doesn't fit on the line. */
6785 ascent = it->max_ascent;
6786 descent = it->max_descent;
6787
6788 /* The call to produce_glyphs will get the metrics of the
6789 display element IT is loaded with. Record the x-position
6790 before this display element, in case it doesn't fit on the
6791 line. */
6792 x = it->current_x;
6793
6794 PRODUCE_GLYPHS (it);
6795
6796 if (it->area != TEXT_AREA)
6797 {
6798 set_iterator_to_next (it, 1);
6799 continue;
6800 }
6801
6802 /* The number of glyphs we get back in IT->nglyphs will normally
6803 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6804 character on a terminal frame, or (iii) a line end. For the
6805 second case, IT->nglyphs - 1 padding glyphs will be present
6806 (on X frames, there is only one glyph produced for a
6807 composite character.
6808
6809 The behavior implemented below means, for continuation lines,
6810 that as many spaces of a TAB as fit on the current line are
6811 displayed there. For terminal frames, as many glyphs of a
6812 multi-glyph character are displayed in the current line, too.
6813 This is what the old redisplay code did, and we keep it that
6814 way. Under X, the whole shape of a complex character must
6815 fit on the line or it will be completely displayed in the
6816 next line.
6817
6818 Note that both for tabs and padding glyphs, all glyphs have
6819 the same width. */
6820 if (it->nglyphs)
6821 {
6822 /* More than one glyph or glyph doesn't fit on line. All
6823 glyphs have the same width. */
6824 int single_glyph_width = it->pixel_width / it->nglyphs;
6825 int new_x;
6826 int x_before_this_char = x;
6827 int hpos_before_this_char = it->hpos;
6828
6829 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6830 {
6831 new_x = x + single_glyph_width;
6832
6833 /* We want to leave anything reaching TO_X to the caller. */
6834 if ((op & MOVE_TO_X) && new_x > to_x)
6835 {
6836 if (BUFFER_POS_REACHED_P ())
6837 {
6838 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6839 goto buffer_pos_reached;
6840 if (atpos_it.sp < 0)
6841 {
6842 atpos_it = *it;
6843 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6844 }
6845 }
6846 else
6847 {
6848 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6849 {
6850 it->current_x = x;
6851 result = MOVE_X_REACHED;
6852 break;
6853 }
6854 if (atx_it.sp < 0)
6855 {
6856 atx_it = *it;
6857 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6858 }
6859 }
6860 }
6861
6862 if (/* Lines are continued. */
6863 it->line_wrap != TRUNCATE
6864 && (/* And glyph doesn't fit on the line. */
6865 new_x > it->last_visible_x
6866 /* Or it fits exactly and we're on a window
6867 system frame. */
6868 || (new_x == it->last_visible_x
6869 && FRAME_WINDOW_P (it->f))))
6870 {
6871 if (/* IT->hpos == 0 means the very first glyph
6872 doesn't fit on the line, e.g. a wide image. */
6873 it->hpos == 0
6874 || (new_x == it->last_visible_x
6875 && FRAME_WINDOW_P (it->f)))
6876 {
6877 ++it->hpos;
6878 it->current_x = new_x;
6879
6880 /* The character's last glyph just barely fits
6881 in this row. */
6882 if (i == it->nglyphs - 1)
6883 {
6884 /* If this is the destination position,
6885 return a position *before* it in this row,
6886 now that we know it fits in this row. */
6887 if (BUFFER_POS_REACHED_P ())
6888 {
6889 if (it->line_wrap != WORD_WRAP
6890 || wrap_it.sp < 0)
6891 {
6892 it->hpos = hpos_before_this_char;
6893 it->current_x = x_before_this_char;
6894 result = MOVE_POS_MATCH_OR_ZV;
6895 break;
6896 }
6897 if (it->line_wrap == WORD_WRAP
6898 && atpos_it.sp < 0)
6899 {
6900 atpos_it = *it;
6901 atpos_it.current_x = x_before_this_char;
6902 atpos_it.hpos = hpos_before_this_char;
6903 }
6904 }
6905
6906 set_iterator_to_next (it, 1);
6907 /* One graphical terminals, newlines may
6908 "overflow" into the fringe if
6909 overflow-newline-into-fringe is non-nil.
6910 On text-only terminals, newlines may
6911 overflow into the last glyph on the
6912 display line.*/
6913 if (!FRAME_WINDOW_P (it->f)
6914 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6915 {
6916 if (!get_next_display_element (it))
6917 {
6918 result = MOVE_POS_MATCH_OR_ZV;
6919 break;
6920 }
6921 if (BUFFER_POS_REACHED_P ())
6922 {
6923 if (ITERATOR_AT_END_OF_LINE_P (it))
6924 result = MOVE_POS_MATCH_OR_ZV;
6925 else
6926 result = MOVE_LINE_CONTINUED;
6927 break;
6928 }
6929 if (ITERATOR_AT_END_OF_LINE_P (it))
6930 {
6931 result = MOVE_NEWLINE_OR_CR;
6932 break;
6933 }
6934 }
6935 }
6936 }
6937 else
6938 IT_RESET_X_ASCENT_DESCENT (it);
6939
6940 if (wrap_it.sp >= 0)
6941 {
6942 *it = wrap_it;
6943 atpos_it.sp = -1;
6944 atx_it.sp = -1;
6945 }
6946
6947 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6948 IT_CHARPOS (*it)));
6949 result = MOVE_LINE_CONTINUED;
6950 break;
6951 }
6952
6953 if (BUFFER_POS_REACHED_P ())
6954 {
6955 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6956 goto buffer_pos_reached;
6957 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6958 {
6959 atpos_it = *it;
6960 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6961 }
6962 }
6963
6964 if (new_x > it->first_visible_x)
6965 {
6966 /* Glyph is visible. Increment number of glyphs that
6967 would be displayed. */
6968 ++it->hpos;
6969 }
6970 }
6971
6972 if (result != MOVE_UNDEFINED)
6973 break;
6974 }
6975 else if (BUFFER_POS_REACHED_P ())
6976 {
6977 buffer_pos_reached:
6978 IT_RESET_X_ASCENT_DESCENT (it);
6979 result = MOVE_POS_MATCH_OR_ZV;
6980 break;
6981 }
6982 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6983 {
6984 /* Stop when TO_X specified and reached. This check is
6985 necessary here because of lines consisting of a line end,
6986 only. The line end will not produce any glyphs and we
6987 would never get MOVE_X_REACHED. */
6988 xassert (it->nglyphs == 0);
6989 result = MOVE_X_REACHED;
6990 break;
6991 }
6992
6993 /* Is this a line end? If yes, we're done. */
6994 if (ITERATOR_AT_END_OF_LINE_P (it))
6995 {
6996 result = MOVE_NEWLINE_OR_CR;
6997 break;
6998 }
6999
7000 /* The current display element has been consumed. Advance
7001 to the next. */
7002 set_iterator_to_next (it, 1);
7003
7004 /* Stop if lines are truncated and IT's current x-position is
7005 past the right edge of the window now. */
7006 if (it->line_wrap == TRUNCATE
7007 && it->current_x >= it->last_visible_x)
7008 {
7009 if (!FRAME_WINDOW_P (it->f)
7010 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7011 {
7012 if (!get_next_display_element (it)
7013 || BUFFER_POS_REACHED_P ())
7014 {
7015 result = MOVE_POS_MATCH_OR_ZV;
7016 break;
7017 }
7018 if (ITERATOR_AT_END_OF_LINE_P (it))
7019 {
7020 result = MOVE_NEWLINE_OR_CR;
7021 break;
7022 }
7023 }
7024 result = MOVE_LINE_TRUNCATED;
7025 break;
7026 }
7027 #undef IT_RESET_X_ASCENT_DESCENT
7028 }
7029
7030 #undef BUFFER_POS_REACHED_P
7031
7032 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7033 restore the saved iterator. */
7034 if (atpos_it.sp >= 0)
7035 *it = atpos_it;
7036 else if (atx_it.sp >= 0)
7037 *it = atx_it;
7038
7039 done:
7040
7041 /* Restore the iterator settings altered at the beginning of this
7042 function. */
7043 it->glyph_row = saved_glyph_row;
7044 return result;
7045 }
7046
7047 /* For external use. */
7048 void
7049 move_it_in_display_line (struct it *it,
7050 EMACS_INT to_charpos, int to_x,
7051 enum move_operation_enum op)
7052 {
7053 if (it->line_wrap == WORD_WRAP
7054 && (op & MOVE_TO_X))
7055 {
7056 struct it save_it = *it;
7057 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7058 /* When word-wrap is on, TO_X may lie past the end
7059 of a wrapped line. Then it->current is the
7060 character on the next line, so backtrack to the
7061 space before the wrap point. */
7062 if (skip == MOVE_LINE_CONTINUED)
7063 {
7064 int prev_x = max (it->current_x - 1, 0);
7065 *it = save_it;
7066 move_it_in_display_line_to
7067 (it, -1, prev_x, MOVE_TO_X);
7068 }
7069 }
7070 else
7071 move_it_in_display_line_to (it, to_charpos, to_x, op);
7072 }
7073
7074
7075 /* Move IT forward until it satisfies one or more of the criteria in
7076 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7077
7078 OP is a bit-mask that specifies where to stop, and in particular,
7079 which of those four position arguments makes a difference. See the
7080 description of enum move_operation_enum.
7081
7082 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7083 screen line, this function will set IT to the next position >
7084 TO_CHARPOS. */
7085
7086 void
7087 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7088 struct it *it;
7089 int to_charpos, to_x, to_y, to_vpos;
7090 int op;
7091 {
7092 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7093 int line_height, line_start_x = 0, reached = 0;
7094
7095 for (;;)
7096 {
7097 if (op & MOVE_TO_VPOS)
7098 {
7099 /* If no TO_CHARPOS and no TO_X specified, stop at the
7100 start of the line TO_VPOS. */
7101 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7102 {
7103 if (it->vpos == to_vpos)
7104 {
7105 reached = 1;
7106 break;
7107 }
7108 else
7109 skip = move_it_in_display_line_to (it, -1, -1, 0);
7110 }
7111 else
7112 {
7113 /* TO_VPOS >= 0 means stop at TO_X in the line at
7114 TO_VPOS, or at TO_POS, whichever comes first. */
7115 if (it->vpos == to_vpos)
7116 {
7117 reached = 2;
7118 break;
7119 }
7120
7121 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7122
7123 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7124 {
7125 reached = 3;
7126 break;
7127 }
7128 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7129 {
7130 /* We have reached TO_X but not in the line we want. */
7131 skip = move_it_in_display_line_to (it, to_charpos,
7132 -1, MOVE_TO_POS);
7133 if (skip == MOVE_POS_MATCH_OR_ZV)
7134 {
7135 reached = 4;
7136 break;
7137 }
7138 }
7139 }
7140 }
7141 else if (op & MOVE_TO_Y)
7142 {
7143 struct it it_backup;
7144
7145 if (it->line_wrap == WORD_WRAP)
7146 it_backup = *it;
7147
7148 /* TO_Y specified means stop at TO_X in the line containing
7149 TO_Y---or at TO_CHARPOS if this is reached first. The
7150 problem is that we can't really tell whether the line
7151 contains TO_Y before we have completely scanned it, and
7152 this may skip past TO_X. What we do is to first scan to
7153 TO_X.
7154
7155 If TO_X is not specified, use a TO_X of zero. The reason
7156 is to make the outcome of this function more predictable.
7157 If we didn't use TO_X == 0, we would stop at the end of
7158 the line which is probably not what a caller would expect
7159 to happen. */
7160 skip = move_it_in_display_line_to
7161 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7162 (MOVE_TO_X | (op & MOVE_TO_POS)));
7163
7164 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7165 if (skip == MOVE_POS_MATCH_OR_ZV)
7166 reached = 5;
7167 else if (skip == MOVE_X_REACHED)
7168 {
7169 /* If TO_X was reached, we want to know whether TO_Y is
7170 in the line. We know this is the case if the already
7171 scanned glyphs make the line tall enough. Otherwise,
7172 we must check by scanning the rest of the line. */
7173 line_height = it->max_ascent + it->max_descent;
7174 if (to_y >= it->current_y
7175 && to_y < it->current_y + line_height)
7176 {
7177 reached = 6;
7178 break;
7179 }
7180 it_backup = *it;
7181 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7182 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7183 op & MOVE_TO_POS);
7184 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7185 line_height = it->max_ascent + it->max_descent;
7186 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7187
7188 if (to_y >= it->current_y
7189 && to_y < it->current_y + line_height)
7190 {
7191 /* If TO_Y is in this line and TO_X was reached
7192 above, we scanned too far. We have to restore
7193 IT's settings to the ones before skipping. */
7194 *it = it_backup;
7195 reached = 6;
7196 }
7197 else
7198 {
7199 skip = skip2;
7200 if (skip == MOVE_POS_MATCH_OR_ZV)
7201 reached = 7;
7202 }
7203 }
7204 else
7205 {
7206 /* Check whether TO_Y is in this line. */
7207 line_height = it->max_ascent + it->max_descent;
7208 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7209
7210 if (to_y >= it->current_y
7211 && to_y < it->current_y + line_height)
7212 {
7213 /* When word-wrap is on, TO_X may lie past the end
7214 of a wrapped line. Then it->current is the
7215 character on the next line, so backtrack to the
7216 space before the wrap point. */
7217 if (skip == MOVE_LINE_CONTINUED
7218 && it->line_wrap == WORD_WRAP)
7219 {
7220 int prev_x = max (it->current_x - 1, 0);
7221 *it = it_backup;
7222 skip = move_it_in_display_line_to
7223 (it, -1, prev_x, MOVE_TO_X);
7224 }
7225 reached = 6;
7226 }
7227 }
7228
7229 if (reached)
7230 break;
7231 }
7232 else if (BUFFERP (it->object)
7233 && (it->method == GET_FROM_BUFFER
7234 || it->method == GET_FROM_STRETCH)
7235 && IT_CHARPOS (*it) >= to_charpos)
7236 skip = MOVE_POS_MATCH_OR_ZV;
7237 else
7238 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7239
7240 switch (skip)
7241 {
7242 case MOVE_POS_MATCH_OR_ZV:
7243 reached = 8;
7244 goto out;
7245
7246 case MOVE_NEWLINE_OR_CR:
7247 set_iterator_to_next (it, 1);
7248 it->continuation_lines_width = 0;
7249 break;
7250
7251 case MOVE_LINE_TRUNCATED:
7252 it->continuation_lines_width = 0;
7253 reseat_at_next_visible_line_start (it, 0);
7254 if ((op & MOVE_TO_POS) != 0
7255 && IT_CHARPOS (*it) > to_charpos)
7256 {
7257 reached = 9;
7258 goto out;
7259 }
7260 break;
7261
7262 case MOVE_LINE_CONTINUED:
7263 /* For continued lines ending in a tab, some of the glyphs
7264 associated with the tab are displayed on the current
7265 line. Since it->current_x does not include these glyphs,
7266 we use it->last_visible_x instead. */
7267 if (it->c == '\t')
7268 {
7269 it->continuation_lines_width += it->last_visible_x;
7270 /* When moving by vpos, ensure that the iterator really
7271 advances to the next line (bug#847, bug#969). Fixme:
7272 do we need to do this in other circumstances? */
7273 if (it->current_x != it->last_visible_x
7274 && (op & MOVE_TO_VPOS)
7275 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7276 {
7277 line_start_x = it->current_x + it->pixel_width
7278 - it->last_visible_x;
7279 set_iterator_to_next (it, 0);
7280 }
7281 }
7282 else
7283 it->continuation_lines_width += it->current_x;
7284 break;
7285
7286 default:
7287 abort ();
7288 }
7289
7290 /* Reset/increment for the next run. */
7291 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7292 it->current_x = line_start_x;
7293 line_start_x = 0;
7294 it->hpos = 0;
7295 it->current_y += it->max_ascent + it->max_descent;
7296 ++it->vpos;
7297 last_height = it->max_ascent + it->max_descent;
7298 last_max_ascent = it->max_ascent;
7299 it->max_ascent = it->max_descent = 0;
7300 }
7301
7302 out:
7303
7304 /* On text terminals, we may stop at the end of a line in the middle
7305 of a multi-character glyph. If the glyph itself is continued,
7306 i.e. it is actually displayed on the next line, don't treat this
7307 stopping point as valid; move to the next line instead (unless
7308 that brings us offscreen). */
7309 if (!FRAME_WINDOW_P (it->f)
7310 && op & MOVE_TO_POS
7311 && IT_CHARPOS (*it) == to_charpos
7312 && it->what == IT_CHARACTER
7313 && it->nglyphs > 1
7314 && it->line_wrap == WINDOW_WRAP
7315 && it->current_x == it->last_visible_x - 1
7316 && it->c != '\n'
7317 && it->c != '\t'
7318 && it->vpos < XFASTINT (it->w->window_end_vpos))
7319 {
7320 it->continuation_lines_width += it->current_x;
7321 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7322 it->current_y += it->max_ascent + it->max_descent;
7323 ++it->vpos;
7324 last_height = it->max_ascent + it->max_descent;
7325 last_max_ascent = it->max_ascent;
7326 }
7327
7328 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7329 }
7330
7331
7332 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7333
7334 If DY > 0, move IT backward at least that many pixels. DY = 0
7335 means move IT backward to the preceding line start or BEGV. This
7336 function may move over more than DY pixels if IT->current_y - DY
7337 ends up in the middle of a line; in this case IT->current_y will be
7338 set to the top of the line moved to. */
7339
7340 void
7341 move_it_vertically_backward (it, dy)
7342 struct it *it;
7343 int dy;
7344 {
7345 int nlines, h;
7346 struct it it2, it3;
7347 int start_pos;
7348
7349 move_further_back:
7350 xassert (dy >= 0);
7351
7352 start_pos = IT_CHARPOS (*it);
7353
7354 /* Estimate how many newlines we must move back. */
7355 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7356
7357 /* Set the iterator's position that many lines back. */
7358 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7359 back_to_previous_visible_line_start (it);
7360
7361 /* Reseat the iterator here. When moving backward, we don't want
7362 reseat to skip forward over invisible text, set up the iterator
7363 to deliver from overlay strings at the new position etc. So,
7364 use reseat_1 here. */
7365 reseat_1 (it, it->current.pos, 1);
7366
7367 /* We are now surely at a line start. */
7368 it->current_x = it->hpos = 0;
7369 it->continuation_lines_width = 0;
7370
7371 /* Move forward and see what y-distance we moved. First move to the
7372 start of the next line so that we get its height. We need this
7373 height to be able to tell whether we reached the specified
7374 y-distance. */
7375 it2 = *it;
7376 it2.max_ascent = it2.max_descent = 0;
7377 do
7378 {
7379 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7380 MOVE_TO_POS | MOVE_TO_VPOS);
7381 }
7382 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7383 xassert (IT_CHARPOS (*it) >= BEGV);
7384 it3 = it2;
7385
7386 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7387 xassert (IT_CHARPOS (*it) >= BEGV);
7388 /* H is the actual vertical distance from the position in *IT
7389 and the starting position. */
7390 h = it2.current_y - it->current_y;
7391 /* NLINES is the distance in number of lines. */
7392 nlines = it2.vpos - it->vpos;
7393
7394 /* Correct IT's y and vpos position
7395 so that they are relative to the starting point. */
7396 it->vpos -= nlines;
7397 it->current_y -= h;
7398
7399 if (dy == 0)
7400 {
7401 /* DY == 0 means move to the start of the screen line. The
7402 value of nlines is > 0 if continuation lines were involved. */
7403 if (nlines > 0)
7404 move_it_by_lines (it, nlines, 1);
7405 }
7406 else
7407 {
7408 /* The y-position we try to reach, relative to *IT.
7409 Note that H has been subtracted in front of the if-statement. */
7410 int target_y = it->current_y + h - dy;
7411 int y0 = it3.current_y;
7412 int y1 = line_bottom_y (&it3);
7413 int line_height = y1 - y0;
7414
7415 /* If we did not reach target_y, try to move further backward if
7416 we can. If we moved too far backward, try to move forward. */
7417 if (target_y < it->current_y
7418 /* This is heuristic. In a window that's 3 lines high, with
7419 a line height of 13 pixels each, recentering with point
7420 on the bottom line will try to move -39/2 = 19 pixels
7421 backward. Try to avoid moving into the first line. */
7422 && (it->current_y - target_y
7423 > min (window_box_height (it->w), line_height * 2 / 3))
7424 && IT_CHARPOS (*it) > BEGV)
7425 {
7426 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7427 target_y - it->current_y));
7428 dy = it->current_y - target_y;
7429 goto move_further_back;
7430 }
7431 else if (target_y >= it->current_y + line_height
7432 && IT_CHARPOS (*it) < ZV)
7433 {
7434 /* Should move forward by at least one line, maybe more.
7435
7436 Note: Calling move_it_by_lines can be expensive on
7437 terminal frames, where compute_motion is used (via
7438 vmotion) to do the job, when there are very long lines
7439 and truncate-lines is nil. That's the reason for
7440 treating terminal frames specially here. */
7441
7442 if (!FRAME_WINDOW_P (it->f))
7443 move_it_vertically (it, target_y - (it->current_y + line_height));
7444 else
7445 {
7446 do
7447 {
7448 move_it_by_lines (it, 1, 1);
7449 }
7450 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7451 }
7452 }
7453 }
7454 }
7455
7456
7457 /* Move IT by a specified amount of pixel lines DY. DY negative means
7458 move backwards. DY = 0 means move to start of screen line. At the
7459 end, IT will be on the start of a screen line. */
7460
7461 void
7462 move_it_vertically (it, dy)
7463 struct it *it;
7464 int dy;
7465 {
7466 if (dy <= 0)
7467 move_it_vertically_backward (it, -dy);
7468 else
7469 {
7470 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7471 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7472 MOVE_TO_POS | MOVE_TO_Y);
7473 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7474
7475 /* If buffer ends in ZV without a newline, move to the start of
7476 the line to satisfy the post-condition. */
7477 if (IT_CHARPOS (*it) == ZV
7478 && ZV > BEGV
7479 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7480 move_it_by_lines (it, 0, 0);
7481 }
7482 }
7483
7484
7485 /* Move iterator IT past the end of the text line it is in. */
7486
7487 void
7488 move_it_past_eol (it)
7489 struct it *it;
7490 {
7491 enum move_it_result rc;
7492
7493 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7494 if (rc == MOVE_NEWLINE_OR_CR)
7495 set_iterator_to_next (it, 0);
7496 }
7497
7498
7499 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7500 negative means move up. DVPOS == 0 means move to the start of the
7501 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7502 NEED_Y_P is zero, IT->current_y will be left unchanged.
7503
7504 Further optimization ideas: If we would know that IT->f doesn't use
7505 a face with proportional font, we could be faster for
7506 truncate-lines nil. */
7507
7508 void
7509 move_it_by_lines (it, dvpos, need_y_p)
7510 struct it *it;
7511 int dvpos, need_y_p;
7512 {
7513 struct position pos;
7514
7515 /* The commented-out optimization uses vmotion on terminals. This
7516 gives bad results, because elements like it->what, on which
7517 callers such as pos_visible_p rely, aren't updated. */
7518 /* if (!FRAME_WINDOW_P (it->f))
7519 {
7520 struct text_pos textpos;
7521
7522 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7523 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7524 reseat (it, textpos, 1);
7525 it->vpos += pos.vpos;
7526 it->current_y += pos.vpos;
7527 }
7528 else */
7529
7530 if (dvpos == 0)
7531 {
7532 /* DVPOS == 0 means move to the start of the screen line. */
7533 move_it_vertically_backward (it, 0);
7534 xassert (it->current_x == 0 && it->hpos == 0);
7535 /* Let next call to line_bottom_y calculate real line height */
7536 last_height = 0;
7537 }
7538 else if (dvpos > 0)
7539 {
7540 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7541 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7542 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7543 }
7544 else
7545 {
7546 struct it it2;
7547 int start_charpos, i;
7548
7549 /* Start at the beginning of the screen line containing IT's
7550 position. This may actually move vertically backwards,
7551 in case of overlays, so adjust dvpos accordingly. */
7552 dvpos += it->vpos;
7553 move_it_vertically_backward (it, 0);
7554 dvpos -= it->vpos;
7555
7556 /* Go back -DVPOS visible lines and reseat the iterator there. */
7557 start_charpos = IT_CHARPOS (*it);
7558 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7559 back_to_previous_visible_line_start (it);
7560 reseat (it, it->current.pos, 1);
7561
7562 /* Move further back if we end up in a string or an image. */
7563 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7564 {
7565 /* First try to move to start of display line. */
7566 dvpos += it->vpos;
7567 move_it_vertically_backward (it, 0);
7568 dvpos -= it->vpos;
7569 if (IT_POS_VALID_AFTER_MOVE_P (it))
7570 break;
7571 /* If start of line is still in string or image,
7572 move further back. */
7573 back_to_previous_visible_line_start (it);
7574 reseat (it, it->current.pos, 1);
7575 dvpos--;
7576 }
7577
7578 it->current_x = it->hpos = 0;
7579
7580 /* Above call may have moved too far if continuation lines
7581 are involved. Scan forward and see if it did. */
7582 it2 = *it;
7583 it2.vpos = it2.current_y = 0;
7584 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7585 it->vpos -= it2.vpos;
7586 it->current_y -= it2.current_y;
7587 it->current_x = it->hpos = 0;
7588
7589 /* If we moved too far back, move IT some lines forward. */
7590 if (it2.vpos > -dvpos)
7591 {
7592 int delta = it2.vpos + dvpos;
7593 it2 = *it;
7594 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7595 /* Move back again if we got too far ahead. */
7596 if (IT_CHARPOS (*it) >= start_charpos)
7597 *it = it2;
7598 }
7599 }
7600 }
7601
7602 /* Return 1 if IT points into the middle of a display vector. */
7603
7604 int
7605 in_display_vector_p (it)
7606 struct it *it;
7607 {
7608 return (it->method == GET_FROM_DISPLAY_VECTOR
7609 && it->current.dpvec_index > 0
7610 && it->dpvec + it->current.dpvec_index != it->dpend);
7611 }
7612
7613 \f
7614 /***********************************************************************
7615 Messages
7616 ***********************************************************************/
7617
7618
7619 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7620 to *Messages*. */
7621
7622 void
7623 add_to_log (format, arg1, arg2)
7624 char *format;
7625 Lisp_Object arg1, arg2;
7626 {
7627 Lisp_Object args[3];
7628 Lisp_Object msg, fmt;
7629 char *buffer;
7630 int len;
7631 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7632 USE_SAFE_ALLOCA;
7633
7634 /* Do nothing if called asynchronously. Inserting text into
7635 a buffer may call after-change-functions and alike and
7636 that would means running Lisp asynchronously. */
7637 if (handling_signal)
7638 return;
7639
7640 fmt = msg = Qnil;
7641 GCPRO4 (fmt, msg, arg1, arg2);
7642
7643 args[0] = fmt = build_string (format);
7644 args[1] = arg1;
7645 args[2] = arg2;
7646 msg = Fformat (3, args);
7647
7648 len = SBYTES (msg) + 1;
7649 SAFE_ALLOCA (buffer, char *, len);
7650 bcopy (SDATA (msg), buffer, len);
7651
7652 message_dolog (buffer, len - 1, 1, 0);
7653 SAFE_FREE ();
7654
7655 UNGCPRO;
7656 }
7657
7658
7659 /* Output a newline in the *Messages* buffer if "needs" one. */
7660
7661 void
7662 message_log_maybe_newline ()
7663 {
7664 if (message_log_need_newline)
7665 message_dolog ("", 0, 1, 0);
7666 }
7667
7668
7669 /* Add a string M of length NBYTES to the message log, optionally
7670 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7671 nonzero, means interpret the contents of M as multibyte. This
7672 function calls low-level routines in order to bypass text property
7673 hooks, etc. which might not be safe to run.
7674
7675 This may GC (insert may run before/after change hooks),
7676 so the buffer M must NOT point to a Lisp string. */
7677
7678 void
7679 message_dolog (m, nbytes, nlflag, multibyte)
7680 const char *m;
7681 int nbytes, nlflag, multibyte;
7682 {
7683 if (!NILP (Vmemory_full))
7684 return;
7685
7686 if (!NILP (Vmessage_log_max))
7687 {
7688 struct buffer *oldbuf;
7689 Lisp_Object oldpoint, oldbegv, oldzv;
7690 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7691 int point_at_end = 0;
7692 int zv_at_end = 0;
7693 Lisp_Object old_deactivate_mark, tem;
7694 struct gcpro gcpro1;
7695
7696 old_deactivate_mark = Vdeactivate_mark;
7697 oldbuf = current_buffer;
7698 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7699 current_buffer->undo_list = Qt;
7700
7701 oldpoint = message_dolog_marker1;
7702 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7703 oldbegv = message_dolog_marker2;
7704 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7705 oldzv = message_dolog_marker3;
7706 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7707 GCPRO1 (old_deactivate_mark);
7708
7709 if (PT == Z)
7710 point_at_end = 1;
7711 if (ZV == Z)
7712 zv_at_end = 1;
7713
7714 BEGV = BEG;
7715 BEGV_BYTE = BEG_BYTE;
7716 ZV = Z;
7717 ZV_BYTE = Z_BYTE;
7718 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7719
7720 /* Insert the string--maybe converting multibyte to single byte
7721 or vice versa, so that all the text fits the buffer. */
7722 if (multibyte
7723 && NILP (current_buffer->enable_multibyte_characters))
7724 {
7725 int i, c, char_bytes;
7726 unsigned char work[1];
7727
7728 /* Convert a multibyte string to single-byte
7729 for the *Message* buffer. */
7730 for (i = 0; i < nbytes; i += char_bytes)
7731 {
7732 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7733 work[0] = (ASCII_CHAR_P (c)
7734 ? c
7735 : multibyte_char_to_unibyte (c, Qnil));
7736 insert_1_both (work, 1, 1, 1, 0, 0);
7737 }
7738 }
7739 else if (! multibyte
7740 && ! NILP (current_buffer->enable_multibyte_characters))
7741 {
7742 int i, c, char_bytes;
7743 unsigned char *msg = (unsigned char *) m;
7744 unsigned char str[MAX_MULTIBYTE_LENGTH];
7745 /* Convert a single-byte string to multibyte
7746 for the *Message* buffer. */
7747 for (i = 0; i < nbytes; i++)
7748 {
7749 c = msg[i];
7750 c = unibyte_char_to_multibyte (c);
7751 char_bytes = CHAR_STRING (c, str);
7752 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7753 }
7754 }
7755 else if (nbytes)
7756 insert_1 (m, nbytes, 1, 0, 0);
7757
7758 if (nlflag)
7759 {
7760 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7761 insert_1 ("\n", 1, 1, 0, 0);
7762
7763 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7764 this_bol = PT;
7765 this_bol_byte = PT_BYTE;
7766
7767 /* See if this line duplicates the previous one.
7768 If so, combine duplicates. */
7769 if (this_bol > BEG)
7770 {
7771 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7772 prev_bol = PT;
7773 prev_bol_byte = PT_BYTE;
7774
7775 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7776 this_bol, this_bol_byte);
7777 if (dup)
7778 {
7779 del_range_both (prev_bol, prev_bol_byte,
7780 this_bol, this_bol_byte, 0);
7781 if (dup > 1)
7782 {
7783 char dupstr[40];
7784 int duplen;
7785
7786 /* If you change this format, don't forget to also
7787 change message_log_check_duplicate. */
7788 sprintf (dupstr, " [%d times]", dup);
7789 duplen = strlen (dupstr);
7790 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7791 insert_1 (dupstr, duplen, 1, 0, 1);
7792 }
7793 }
7794 }
7795
7796 /* If we have more than the desired maximum number of lines
7797 in the *Messages* buffer now, delete the oldest ones.
7798 This is safe because we don't have undo in this buffer. */
7799
7800 if (NATNUMP (Vmessage_log_max))
7801 {
7802 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7803 -XFASTINT (Vmessage_log_max) - 1, 0);
7804 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7805 }
7806 }
7807 BEGV = XMARKER (oldbegv)->charpos;
7808 BEGV_BYTE = marker_byte_position (oldbegv);
7809
7810 if (zv_at_end)
7811 {
7812 ZV = Z;
7813 ZV_BYTE = Z_BYTE;
7814 }
7815 else
7816 {
7817 ZV = XMARKER (oldzv)->charpos;
7818 ZV_BYTE = marker_byte_position (oldzv);
7819 }
7820
7821 if (point_at_end)
7822 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7823 else
7824 /* We can't do Fgoto_char (oldpoint) because it will run some
7825 Lisp code. */
7826 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7827 XMARKER (oldpoint)->bytepos);
7828
7829 UNGCPRO;
7830 unchain_marker (XMARKER (oldpoint));
7831 unchain_marker (XMARKER (oldbegv));
7832 unchain_marker (XMARKER (oldzv));
7833
7834 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7835 set_buffer_internal (oldbuf);
7836 if (NILP (tem))
7837 windows_or_buffers_changed = old_windows_or_buffers_changed;
7838 message_log_need_newline = !nlflag;
7839 Vdeactivate_mark = old_deactivate_mark;
7840 }
7841 }
7842
7843
7844 /* We are at the end of the buffer after just having inserted a newline.
7845 (Note: We depend on the fact we won't be crossing the gap.)
7846 Check to see if the most recent message looks a lot like the previous one.
7847 Return 0 if different, 1 if the new one should just replace it, or a
7848 value N > 1 if we should also append " [N times]". */
7849
7850 static int
7851 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7852 int prev_bol, this_bol;
7853 int prev_bol_byte, this_bol_byte;
7854 {
7855 int i;
7856 int len = Z_BYTE - 1 - this_bol_byte;
7857 int seen_dots = 0;
7858 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7859 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7860
7861 for (i = 0; i < len; i++)
7862 {
7863 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7864 seen_dots = 1;
7865 if (p1[i] != p2[i])
7866 return seen_dots;
7867 }
7868 p1 += len;
7869 if (*p1 == '\n')
7870 return 2;
7871 if (*p1++ == ' ' && *p1++ == '[')
7872 {
7873 int n = 0;
7874 while (*p1 >= '0' && *p1 <= '9')
7875 n = n * 10 + *p1++ - '0';
7876 if (strncmp (p1, " times]\n", 8) == 0)
7877 return n+1;
7878 }
7879 return 0;
7880 }
7881 \f
7882
7883 /* Display an echo area message M with a specified length of NBYTES
7884 bytes. The string may include null characters. If M is 0, clear
7885 out any existing message, and let the mini-buffer text show
7886 through.
7887
7888 This may GC, so the buffer M must NOT point to a Lisp string. */
7889
7890 void
7891 message2 (m, nbytes, multibyte)
7892 const char *m;
7893 int nbytes;
7894 int multibyte;
7895 {
7896 /* First flush out any partial line written with print. */
7897 message_log_maybe_newline ();
7898 if (m)
7899 message_dolog (m, nbytes, 1, multibyte);
7900 message2_nolog (m, nbytes, multibyte);
7901 }
7902
7903
7904 /* The non-logging counterpart of message2. */
7905
7906 void
7907 message2_nolog (m, nbytes, multibyte)
7908 const char *m;
7909 int nbytes, multibyte;
7910 {
7911 struct frame *sf = SELECTED_FRAME ();
7912 message_enable_multibyte = multibyte;
7913
7914 if (FRAME_INITIAL_P (sf))
7915 {
7916 if (noninteractive_need_newline)
7917 putc ('\n', stderr);
7918 noninteractive_need_newline = 0;
7919 if (m)
7920 fwrite (m, nbytes, 1, stderr);
7921 if (cursor_in_echo_area == 0)
7922 fprintf (stderr, "\n");
7923 fflush (stderr);
7924 }
7925 /* A null message buffer means that the frame hasn't really been
7926 initialized yet. Error messages get reported properly by
7927 cmd_error, so this must be just an informative message; toss it. */
7928 else if (INTERACTIVE
7929 && sf->glyphs_initialized_p
7930 && FRAME_MESSAGE_BUF (sf))
7931 {
7932 Lisp_Object mini_window;
7933 struct frame *f;
7934
7935 /* Get the frame containing the mini-buffer
7936 that the selected frame is using. */
7937 mini_window = FRAME_MINIBUF_WINDOW (sf);
7938 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7939
7940 FRAME_SAMPLE_VISIBILITY (f);
7941 if (FRAME_VISIBLE_P (sf)
7942 && ! FRAME_VISIBLE_P (f))
7943 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7944
7945 if (m)
7946 {
7947 set_message (m, Qnil, nbytes, multibyte);
7948 if (minibuffer_auto_raise)
7949 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7950 }
7951 else
7952 clear_message (1, 1);
7953
7954 do_pending_window_change (0);
7955 echo_area_display (1);
7956 do_pending_window_change (0);
7957 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7958 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7959 }
7960 }
7961
7962
7963 /* Display an echo area message M with a specified length of NBYTES
7964 bytes. The string may include null characters. If M is not a
7965 string, clear out any existing message, and let the mini-buffer
7966 text show through.
7967
7968 This function cancels echoing. */
7969
7970 void
7971 message3 (m, nbytes, multibyte)
7972 Lisp_Object m;
7973 int nbytes;
7974 int multibyte;
7975 {
7976 struct gcpro gcpro1;
7977
7978 GCPRO1 (m);
7979 clear_message (1,1);
7980 cancel_echoing ();
7981
7982 /* First flush out any partial line written with print. */
7983 message_log_maybe_newline ();
7984 if (STRINGP (m))
7985 {
7986 char *buffer;
7987 USE_SAFE_ALLOCA;
7988
7989 SAFE_ALLOCA (buffer, char *, nbytes);
7990 bcopy (SDATA (m), buffer, nbytes);
7991 message_dolog (buffer, nbytes, 1, multibyte);
7992 SAFE_FREE ();
7993 }
7994 message3_nolog (m, nbytes, multibyte);
7995
7996 UNGCPRO;
7997 }
7998
7999
8000 /* The non-logging version of message3.
8001 This does not cancel echoing, because it is used for echoing.
8002 Perhaps we need to make a separate function for echoing
8003 and make this cancel echoing. */
8004
8005 void
8006 message3_nolog (m, nbytes, multibyte)
8007 Lisp_Object m;
8008 int nbytes, multibyte;
8009 {
8010 struct frame *sf = SELECTED_FRAME ();
8011 message_enable_multibyte = multibyte;
8012
8013 if (FRAME_INITIAL_P (sf))
8014 {
8015 if (noninteractive_need_newline)
8016 putc ('\n', stderr);
8017 noninteractive_need_newline = 0;
8018 if (STRINGP (m))
8019 fwrite (SDATA (m), nbytes, 1, stderr);
8020 if (cursor_in_echo_area == 0)
8021 fprintf (stderr, "\n");
8022 fflush (stderr);
8023 }
8024 /* A null message buffer means that the frame hasn't really been
8025 initialized yet. Error messages get reported properly by
8026 cmd_error, so this must be just an informative message; toss it. */
8027 else if (INTERACTIVE
8028 && sf->glyphs_initialized_p
8029 && FRAME_MESSAGE_BUF (sf))
8030 {
8031 Lisp_Object mini_window;
8032 Lisp_Object frame;
8033 struct frame *f;
8034
8035 /* Get the frame containing the mini-buffer
8036 that the selected frame is using. */
8037 mini_window = FRAME_MINIBUF_WINDOW (sf);
8038 frame = XWINDOW (mini_window)->frame;
8039 f = XFRAME (frame);
8040
8041 FRAME_SAMPLE_VISIBILITY (f);
8042 if (FRAME_VISIBLE_P (sf)
8043 && !FRAME_VISIBLE_P (f))
8044 Fmake_frame_visible (frame);
8045
8046 if (STRINGP (m) && SCHARS (m) > 0)
8047 {
8048 set_message (NULL, m, nbytes, multibyte);
8049 if (minibuffer_auto_raise)
8050 Fraise_frame (frame);
8051 /* Assume we are not echoing.
8052 (If we are, echo_now will override this.) */
8053 echo_message_buffer = Qnil;
8054 }
8055 else
8056 clear_message (1, 1);
8057
8058 do_pending_window_change (0);
8059 echo_area_display (1);
8060 do_pending_window_change (0);
8061 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8062 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8063 }
8064 }
8065
8066
8067 /* Display a null-terminated echo area message M. If M is 0, clear
8068 out any existing message, and let the mini-buffer text show through.
8069
8070 The buffer M must continue to exist until after the echo area gets
8071 cleared or some other message gets displayed there. Do not pass
8072 text that is stored in a Lisp string. Do not pass text in a buffer
8073 that was alloca'd. */
8074
8075 void
8076 message1 (m)
8077 char *m;
8078 {
8079 message2 (m, (m ? strlen (m) : 0), 0);
8080 }
8081
8082
8083 /* The non-logging counterpart of message1. */
8084
8085 void
8086 message1_nolog (m)
8087 char *m;
8088 {
8089 message2_nolog (m, (m ? strlen (m) : 0), 0);
8090 }
8091
8092 /* Display a message M which contains a single %s
8093 which gets replaced with STRING. */
8094
8095 void
8096 message_with_string (m, string, log)
8097 char *m;
8098 Lisp_Object string;
8099 int log;
8100 {
8101 CHECK_STRING (string);
8102
8103 if (noninteractive)
8104 {
8105 if (m)
8106 {
8107 if (noninteractive_need_newline)
8108 putc ('\n', stderr);
8109 noninteractive_need_newline = 0;
8110 fprintf (stderr, m, SDATA (string));
8111 if (!cursor_in_echo_area)
8112 fprintf (stderr, "\n");
8113 fflush (stderr);
8114 }
8115 }
8116 else if (INTERACTIVE)
8117 {
8118 /* The frame whose minibuffer we're going to display the message on.
8119 It may be larger than the selected frame, so we need
8120 to use its buffer, not the selected frame's buffer. */
8121 Lisp_Object mini_window;
8122 struct frame *f, *sf = SELECTED_FRAME ();
8123
8124 /* Get the frame containing the minibuffer
8125 that the selected frame is using. */
8126 mini_window = FRAME_MINIBUF_WINDOW (sf);
8127 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8128
8129 /* A null message buffer means that the frame hasn't really been
8130 initialized yet. Error messages get reported properly by
8131 cmd_error, so this must be just an informative message; toss it. */
8132 if (FRAME_MESSAGE_BUF (f))
8133 {
8134 Lisp_Object args[2], message;
8135 struct gcpro gcpro1, gcpro2;
8136
8137 args[0] = build_string (m);
8138 args[1] = message = string;
8139 GCPRO2 (args[0], message);
8140 gcpro1.nvars = 2;
8141
8142 message = Fformat (2, args);
8143
8144 if (log)
8145 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8146 else
8147 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8148
8149 UNGCPRO;
8150
8151 /* Print should start at the beginning of the message
8152 buffer next time. */
8153 message_buf_print = 0;
8154 }
8155 }
8156 }
8157
8158
8159 /* Dump an informative message to the minibuf. If M is 0, clear out
8160 any existing message, and let the mini-buffer text show through. */
8161
8162 /* VARARGS 1 */
8163 void
8164 message (m, a1, a2, a3)
8165 char *m;
8166 EMACS_INT a1, a2, a3;
8167 {
8168 if (noninteractive)
8169 {
8170 if (m)
8171 {
8172 if (noninteractive_need_newline)
8173 putc ('\n', stderr);
8174 noninteractive_need_newline = 0;
8175 fprintf (stderr, m, a1, a2, a3);
8176 if (cursor_in_echo_area == 0)
8177 fprintf (stderr, "\n");
8178 fflush (stderr);
8179 }
8180 }
8181 else if (INTERACTIVE)
8182 {
8183 /* The frame whose mini-buffer we're going to display the message
8184 on. It may be larger than the selected frame, so we need to
8185 use its buffer, not the selected frame's buffer. */
8186 Lisp_Object mini_window;
8187 struct frame *f, *sf = SELECTED_FRAME ();
8188
8189 /* Get the frame containing the mini-buffer
8190 that the selected frame is using. */
8191 mini_window = FRAME_MINIBUF_WINDOW (sf);
8192 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8193
8194 /* A null message buffer means that the frame hasn't really been
8195 initialized yet. Error messages get reported properly by
8196 cmd_error, so this must be just an informative message; toss
8197 it. */
8198 if (FRAME_MESSAGE_BUF (f))
8199 {
8200 if (m)
8201 {
8202 int len;
8203 #ifdef NO_ARG_ARRAY
8204 char *a[3];
8205 a[0] = (char *) a1;
8206 a[1] = (char *) a2;
8207 a[2] = (char *) a3;
8208
8209 len = doprnt (FRAME_MESSAGE_BUF (f),
8210 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8211 #else
8212 len = doprnt (FRAME_MESSAGE_BUF (f),
8213 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8214 (char **) &a1);
8215 #endif /* NO_ARG_ARRAY */
8216
8217 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8218 }
8219 else
8220 message1 (0);
8221
8222 /* Print should start at the beginning of the message
8223 buffer next time. */
8224 message_buf_print = 0;
8225 }
8226 }
8227 }
8228
8229
8230 /* The non-logging version of message. */
8231
8232 void
8233 message_nolog (m, a1, a2, a3)
8234 char *m;
8235 EMACS_INT a1, a2, a3;
8236 {
8237 Lisp_Object old_log_max;
8238 old_log_max = Vmessage_log_max;
8239 Vmessage_log_max = Qnil;
8240 message (m, a1, a2, a3);
8241 Vmessage_log_max = old_log_max;
8242 }
8243
8244
8245 /* Display the current message in the current mini-buffer. This is
8246 only called from error handlers in process.c, and is not time
8247 critical. */
8248
8249 void
8250 update_echo_area ()
8251 {
8252 if (!NILP (echo_area_buffer[0]))
8253 {
8254 Lisp_Object string;
8255 string = Fcurrent_message ();
8256 message3 (string, SBYTES (string),
8257 !NILP (current_buffer->enable_multibyte_characters));
8258 }
8259 }
8260
8261
8262 /* Make sure echo area buffers in `echo_buffers' are live.
8263 If they aren't, make new ones. */
8264
8265 static void
8266 ensure_echo_area_buffers ()
8267 {
8268 int i;
8269
8270 for (i = 0; i < 2; ++i)
8271 if (!BUFFERP (echo_buffer[i])
8272 || NILP (XBUFFER (echo_buffer[i])->name))
8273 {
8274 char name[30];
8275 Lisp_Object old_buffer;
8276 int j;
8277
8278 old_buffer = echo_buffer[i];
8279 sprintf (name, " *Echo Area %d*", i);
8280 echo_buffer[i] = Fget_buffer_create (build_string (name));
8281 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8282 /* to force word wrap in echo area -
8283 it was decided to postpone this*/
8284 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8285
8286 for (j = 0; j < 2; ++j)
8287 if (EQ (old_buffer, echo_area_buffer[j]))
8288 echo_area_buffer[j] = echo_buffer[i];
8289 }
8290 }
8291
8292
8293 /* Call FN with args A1..A4 with either the current or last displayed
8294 echo_area_buffer as current buffer.
8295
8296 WHICH zero means use the current message buffer
8297 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8298 from echo_buffer[] and clear it.
8299
8300 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8301 suitable buffer from echo_buffer[] and clear it.
8302
8303 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8304 that the current message becomes the last displayed one, make
8305 choose a suitable buffer for echo_area_buffer[0], and clear it.
8306
8307 Value is what FN returns. */
8308
8309 static int
8310 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8311 struct window *w;
8312 int which;
8313 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8314 EMACS_INT a1;
8315 Lisp_Object a2;
8316 EMACS_INT a3, a4;
8317 {
8318 Lisp_Object buffer;
8319 int this_one, the_other, clear_buffer_p, rc;
8320 int count = SPECPDL_INDEX ();
8321
8322 /* If buffers aren't live, make new ones. */
8323 ensure_echo_area_buffers ();
8324
8325 clear_buffer_p = 0;
8326
8327 if (which == 0)
8328 this_one = 0, the_other = 1;
8329 else if (which > 0)
8330 this_one = 1, the_other = 0;
8331 else
8332 {
8333 this_one = 0, the_other = 1;
8334 clear_buffer_p = 1;
8335
8336 /* We need a fresh one in case the current echo buffer equals
8337 the one containing the last displayed echo area message. */
8338 if (!NILP (echo_area_buffer[this_one])
8339 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8340 echo_area_buffer[this_one] = Qnil;
8341 }
8342
8343 /* Choose a suitable buffer from echo_buffer[] is we don't
8344 have one. */
8345 if (NILP (echo_area_buffer[this_one]))
8346 {
8347 echo_area_buffer[this_one]
8348 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8349 ? echo_buffer[the_other]
8350 : echo_buffer[this_one]);
8351 clear_buffer_p = 1;
8352 }
8353
8354 buffer = echo_area_buffer[this_one];
8355
8356 /* Don't get confused by reusing the buffer used for echoing
8357 for a different purpose. */
8358 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8359 cancel_echoing ();
8360
8361 record_unwind_protect (unwind_with_echo_area_buffer,
8362 with_echo_area_buffer_unwind_data (w));
8363
8364 /* Make the echo area buffer current. Note that for display
8365 purposes, it is not necessary that the displayed window's buffer
8366 == current_buffer, except for text property lookup. So, let's
8367 only set that buffer temporarily here without doing a full
8368 Fset_window_buffer. We must also change w->pointm, though,
8369 because otherwise an assertions in unshow_buffer fails, and Emacs
8370 aborts. */
8371 set_buffer_internal_1 (XBUFFER (buffer));
8372 if (w)
8373 {
8374 w->buffer = buffer;
8375 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8376 }
8377
8378 current_buffer->undo_list = Qt;
8379 current_buffer->read_only = Qnil;
8380 specbind (Qinhibit_read_only, Qt);
8381 specbind (Qinhibit_modification_hooks, Qt);
8382
8383 if (clear_buffer_p && Z > BEG)
8384 del_range (BEG, Z);
8385
8386 xassert (BEGV >= BEG);
8387 xassert (ZV <= Z && ZV >= BEGV);
8388
8389 rc = fn (a1, a2, a3, a4);
8390
8391 xassert (BEGV >= BEG);
8392 xassert (ZV <= Z && ZV >= BEGV);
8393
8394 unbind_to (count, Qnil);
8395 return rc;
8396 }
8397
8398
8399 /* Save state that should be preserved around the call to the function
8400 FN called in with_echo_area_buffer. */
8401
8402 static Lisp_Object
8403 with_echo_area_buffer_unwind_data (w)
8404 struct window *w;
8405 {
8406 int i = 0;
8407 Lisp_Object vector, tmp;
8408
8409 /* Reduce consing by keeping one vector in
8410 Vwith_echo_area_save_vector. */
8411 vector = Vwith_echo_area_save_vector;
8412 Vwith_echo_area_save_vector = Qnil;
8413
8414 if (NILP (vector))
8415 vector = Fmake_vector (make_number (7), Qnil);
8416
8417 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8418 ASET (vector, i, Vdeactivate_mark); ++i;
8419 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8420
8421 if (w)
8422 {
8423 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8424 ASET (vector, i, w->buffer); ++i;
8425 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8426 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8427 }
8428 else
8429 {
8430 int end = i + 4;
8431 for (; i < end; ++i)
8432 ASET (vector, i, Qnil);
8433 }
8434
8435 xassert (i == ASIZE (vector));
8436 return vector;
8437 }
8438
8439
8440 /* Restore global state from VECTOR which was created by
8441 with_echo_area_buffer_unwind_data. */
8442
8443 static Lisp_Object
8444 unwind_with_echo_area_buffer (vector)
8445 Lisp_Object vector;
8446 {
8447 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8448 Vdeactivate_mark = AREF (vector, 1);
8449 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8450
8451 if (WINDOWP (AREF (vector, 3)))
8452 {
8453 struct window *w;
8454 Lisp_Object buffer, charpos, bytepos;
8455
8456 w = XWINDOW (AREF (vector, 3));
8457 buffer = AREF (vector, 4);
8458 charpos = AREF (vector, 5);
8459 bytepos = AREF (vector, 6);
8460
8461 w->buffer = buffer;
8462 set_marker_both (w->pointm, buffer,
8463 XFASTINT (charpos), XFASTINT (bytepos));
8464 }
8465
8466 Vwith_echo_area_save_vector = vector;
8467 return Qnil;
8468 }
8469
8470
8471 /* Set up the echo area for use by print functions. MULTIBYTE_P
8472 non-zero means we will print multibyte. */
8473
8474 void
8475 setup_echo_area_for_printing (multibyte_p)
8476 int multibyte_p;
8477 {
8478 /* If we can't find an echo area any more, exit. */
8479 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8480 Fkill_emacs (Qnil);
8481
8482 ensure_echo_area_buffers ();
8483
8484 if (!message_buf_print)
8485 {
8486 /* A message has been output since the last time we printed.
8487 Choose a fresh echo area buffer. */
8488 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8489 echo_area_buffer[0] = echo_buffer[1];
8490 else
8491 echo_area_buffer[0] = echo_buffer[0];
8492
8493 /* Switch to that buffer and clear it. */
8494 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8495 current_buffer->truncate_lines = Qnil;
8496
8497 if (Z > BEG)
8498 {
8499 int count = SPECPDL_INDEX ();
8500 specbind (Qinhibit_read_only, Qt);
8501 /* Note that undo recording is always disabled. */
8502 del_range (BEG, Z);
8503 unbind_to (count, Qnil);
8504 }
8505 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8506
8507 /* Set up the buffer for the multibyteness we need. */
8508 if (multibyte_p
8509 != !NILP (current_buffer->enable_multibyte_characters))
8510 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8511
8512 /* Raise the frame containing the echo area. */
8513 if (minibuffer_auto_raise)
8514 {
8515 struct frame *sf = SELECTED_FRAME ();
8516 Lisp_Object mini_window;
8517 mini_window = FRAME_MINIBUF_WINDOW (sf);
8518 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8519 }
8520
8521 message_log_maybe_newline ();
8522 message_buf_print = 1;
8523 }
8524 else
8525 {
8526 if (NILP (echo_area_buffer[0]))
8527 {
8528 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8529 echo_area_buffer[0] = echo_buffer[1];
8530 else
8531 echo_area_buffer[0] = echo_buffer[0];
8532 }
8533
8534 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8535 {
8536 /* Someone switched buffers between print requests. */
8537 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8538 current_buffer->truncate_lines = Qnil;
8539 }
8540 }
8541 }
8542
8543
8544 /* Display an echo area message in window W. Value is non-zero if W's
8545 height is changed. If display_last_displayed_message_p is
8546 non-zero, display the message that was last displayed, otherwise
8547 display the current message. */
8548
8549 static int
8550 display_echo_area (w)
8551 struct window *w;
8552 {
8553 int i, no_message_p, window_height_changed_p, count;
8554
8555 /* Temporarily disable garbage collections while displaying the echo
8556 area. This is done because a GC can print a message itself.
8557 That message would modify the echo area buffer's contents while a
8558 redisplay of the buffer is going on, and seriously confuse
8559 redisplay. */
8560 count = inhibit_garbage_collection ();
8561
8562 /* If there is no message, we must call display_echo_area_1
8563 nevertheless because it resizes the window. But we will have to
8564 reset the echo_area_buffer in question to nil at the end because
8565 with_echo_area_buffer will sets it to an empty buffer. */
8566 i = display_last_displayed_message_p ? 1 : 0;
8567 no_message_p = NILP (echo_area_buffer[i]);
8568
8569 window_height_changed_p
8570 = with_echo_area_buffer (w, display_last_displayed_message_p,
8571 display_echo_area_1,
8572 (EMACS_INT) w, Qnil, 0, 0);
8573
8574 if (no_message_p)
8575 echo_area_buffer[i] = Qnil;
8576
8577 unbind_to (count, Qnil);
8578 return window_height_changed_p;
8579 }
8580
8581
8582 /* Helper for display_echo_area. Display the current buffer which
8583 contains the current echo area message in window W, a mini-window,
8584 a pointer to which is passed in A1. A2..A4 are currently not used.
8585 Change the height of W so that all of the message is displayed.
8586 Value is non-zero if height of W was changed. */
8587
8588 static int
8589 display_echo_area_1 (a1, a2, a3, a4)
8590 EMACS_INT a1;
8591 Lisp_Object a2;
8592 EMACS_INT a3, a4;
8593 {
8594 struct window *w = (struct window *) a1;
8595 Lisp_Object window;
8596 struct text_pos start;
8597 int window_height_changed_p = 0;
8598
8599 /* Do this before displaying, so that we have a large enough glyph
8600 matrix for the display. If we can't get enough space for the
8601 whole text, display the last N lines. That works by setting w->start. */
8602 window_height_changed_p = resize_mini_window (w, 0);
8603
8604 /* Use the starting position chosen by resize_mini_window. */
8605 SET_TEXT_POS_FROM_MARKER (start, w->start);
8606
8607 /* Display. */
8608 clear_glyph_matrix (w->desired_matrix);
8609 XSETWINDOW (window, w);
8610 try_window (window, start, 0);
8611
8612 return window_height_changed_p;
8613 }
8614
8615
8616 /* Resize the echo area window to exactly the size needed for the
8617 currently displayed message, if there is one. If a mini-buffer
8618 is active, don't shrink it. */
8619
8620 void
8621 resize_echo_area_exactly ()
8622 {
8623 if (BUFFERP (echo_area_buffer[0])
8624 && WINDOWP (echo_area_window))
8625 {
8626 struct window *w = XWINDOW (echo_area_window);
8627 int resized_p;
8628 Lisp_Object resize_exactly;
8629
8630 if (minibuf_level == 0)
8631 resize_exactly = Qt;
8632 else
8633 resize_exactly = Qnil;
8634
8635 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8636 (EMACS_INT) w, resize_exactly, 0, 0);
8637 if (resized_p)
8638 {
8639 ++windows_or_buffers_changed;
8640 ++update_mode_lines;
8641 redisplay_internal (0);
8642 }
8643 }
8644 }
8645
8646
8647 /* Callback function for with_echo_area_buffer, when used from
8648 resize_echo_area_exactly. A1 contains a pointer to the window to
8649 resize, EXACTLY non-nil means resize the mini-window exactly to the
8650 size of the text displayed. A3 and A4 are not used. Value is what
8651 resize_mini_window returns. */
8652
8653 static int
8654 resize_mini_window_1 (a1, exactly, a3, a4)
8655 EMACS_INT a1;
8656 Lisp_Object exactly;
8657 EMACS_INT a3, a4;
8658 {
8659 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8660 }
8661
8662
8663 /* Resize mini-window W to fit the size of its contents. EXACT_P
8664 means size the window exactly to the size needed. Otherwise, it's
8665 only enlarged until W's buffer is empty.
8666
8667 Set W->start to the right place to begin display. If the whole
8668 contents fit, start at the beginning. Otherwise, start so as
8669 to make the end of the contents appear. This is particularly
8670 important for y-or-n-p, but seems desirable generally.
8671
8672 Value is non-zero if the window height has been changed. */
8673
8674 int
8675 resize_mini_window (w, exact_p)
8676 struct window *w;
8677 int exact_p;
8678 {
8679 struct frame *f = XFRAME (w->frame);
8680 int window_height_changed_p = 0;
8681
8682 xassert (MINI_WINDOW_P (w));
8683
8684 /* By default, start display at the beginning. */
8685 set_marker_both (w->start, w->buffer,
8686 BUF_BEGV (XBUFFER (w->buffer)),
8687 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8688
8689 /* Don't resize windows while redisplaying a window; it would
8690 confuse redisplay functions when the size of the window they are
8691 displaying changes from under them. Such a resizing can happen,
8692 for instance, when which-func prints a long message while
8693 we are running fontification-functions. We're running these
8694 functions with safe_call which binds inhibit-redisplay to t. */
8695 if (!NILP (Vinhibit_redisplay))
8696 return 0;
8697
8698 /* Nil means don't try to resize. */
8699 if (NILP (Vresize_mini_windows)
8700 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8701 return 0;
8702
8703 if (!FRAME_MINIBUF_ONLY_P (f))
8704 {
8705 struct it it;
8706 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8707 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8708 int height, max_height;
8709 int unit = FRAME_LINE_HEIGHT (f);
8710 struct text_pos start;
8711 struct buffer *old_current_buffer = NULL;
8712
8713 if (current_buffer != XBUFFER (w->buffer))
8714 {
8715 old_current_buffer = current_buffer;
8716 set_buffer_internal (XBUFFER (w->buffer));
8717 }
8718
8719 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8720
8721 /* Compute the max. number of lines specified by the user. */
8722 if (FLOATP (Vmax_mini_window_height))
8723 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8724 else if (INTEGERP (Vmax_mini_window_height))
8725 max_height = XINT (Vmax_mini_window_height);
8726 else
8727 max_height = total_height / 4;
8728
8729 /* Correct that max. height if it's bogus. */
8730 max_height = max (1, max_height);
8731 max_height = min (total_height, max_height);
8732
8733 /* Find out the height of the text in the window. */
8734 if (it.line_wrap == TRUNCATE)
8735 height = 1;
8736 else
8737 {
8738 last_height = 0;
8739 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8740 if (it.max_ascent == 0 && it.max_descent == 0)
8741 height = it.current_y + last_height;
8742 else
8743 height = it.current_y + it.max_ascent + it.max_descent;
8744 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8745 height = (height + unit - 1) / unit;
8746 }
8747
8748 /* Compute a suitable window start. */
8749 if (height > max_height)
8750 {
8751 height = max_height;
8752 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8753 move_it_vertically_backward (&it, (height - 1) * unit);
8754 start = it.current.pos;
8755 }
8756 else
8757 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8758 SET_MARKER_FROM_TEXT_POS (w->start, start);
8759
8760 if (EQ (Vresize_mini_windows, Qgrow_only))
8761 {
8762 /* Let it grow only, until we display an empty message, in which
8763 case the window shrinks again. */
8764 if (height > WINDOW_TOTAL_LINES (w))
8765 {
8766 int old_height = WINDOW_TOTAL_LINES (w);
8767 freeze_window_starts (f, 1);
8768 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8769 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8770 }
8771 else if (height < WINDOW_TOTAL_LINES (w)
8772 && (exact_p || BEGV == ZV))
8773 {
8774 int old_height = WINDOW_TOTAL_LINES (w);
8775 freeze_window_starts (f, 0);
8776 shrink_mini_window (w);
8777 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8778 }
8779 }
8780 else
8781 {
8782 /* Always resize to exact size needed. */
8783 if (height > WINDOW_TOTAL_LINES (w))
8784 {
8785 int old_height = WINDOW_TOTAL_LINES (w);
8786 freeze_window_starts (f, 1);
8787 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8788 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8789 }
8790 else if (height < WINDOW_TOTAL_LINES (w))
8791 {
8792 int old_height = WINDOW_TOTAL_LINES (w);
8793 freeze_window_starts (f, 0);
8794 shrink_mini_window (w);
8795
8796 if (height)
8797 {
8798 freeze_window_starts (f, 1);
8799 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8800 }
8801
8802 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8803 }
8804 }
8805
8806 if (old_current_buffer)
8807 set_buffer_internal (old_current_buffer);
8808 }
8809
8810 return window_height_changed_p;
8811 }
8812
8813
8814 /* Value is the current message, a string, or nil if there is no
8815 current message. */
8816
8817 Lisp_Object
8818 current_message ()
8819 {
8820 Lisp_Object msg;
8821
8822 if (!BUFFERP (echo_area_buffer[0]))
8823 msg = Qnil;
8824 else
8825 {
8826 with_echo_area_buffer (0, 0, current_message_1,
8827 (EMACS_INT) &msg, Qnil, 0, 0);
8828 if (NILP (msg))
8829 echo_area_buffer[0] = Qnil;
8830 }
8831
8832 return msg;
8833 }
8834
8835
8836 static int
8837 current_message_1 (a1, a2, a3, a4)
8838 EMACS_INT a1;
8839 Lisp_Object a2;
8840 EMACS_INT a3, a4;
8841 {
8842 Lisp_Object *msg = (Lisp_Object *) a1;
8843
8844 if (Z > BEG)
8845 *msg = make_buffer_string (BEG, Z, 1);
8846 else
8847 *msg = Qnil;
8848 return 0;
8849 }
8850
8851
8852 /* Push the current message on Vmessage_stack for later restauration
8853 by restore_message. Value is non-zero if the current message isn't
8854 empty. This is a relatively infrequent operation, so it's not
8855 worth optimizing. */
8856
8857 int
8858 push_message ()
8859 {
8860 Lisp_Object msg;
8861 msg = current_message ();
8862 Vmessage_stack = Fcons (msg, Vmessage_stack);
8863 return STRINGP (msg);
8864 }
8865
8866
8867 /* Restore message display from the top of Vmessage_stack. */
8868
8869 void
8870 restore_message ()
8871 {
8872 Lisp_Object msg;
8873
8874 xassert (CONSP (Vmessage_stack));
8875 msg = XCAR (Vmessage_stack);
8876 if (STRINGP (msg))
8877 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8878 else
8879 message3_nolog (msg, 0, 0);
8880 }
8881
8882
8883 /* Handler for record_unwind_protect calling pop_message. */
8884
8885 Lisp_Object
8886 pop_message_unwind (dummy)
8887 Lisp_Object dummy;
8888 {
8889 pop_message ();
8890 return Qnil;
8891 }
8892
8893 /* Pop the top-most entry off Vmessage_stack. */
8894
8895 void
8896 pop_message ()
8897 {
8898 xassert (CONSP (Vmessage_stack));
8899 Vmessage_stack = XCDR (Vmessage_stack);
8900 }
8901
8902
8903 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8904 exits. If the stack is not empty, we have a missing pop_message
8905 somewhere. */
8906
8907 void
8908 check_message_stack ()
8909 {
8910 if (!NILP (Vmessage_stack))
8911 abort ();
8912 }
8913
8914
8915 /* Truncate to NCHARS what will be displayed in the echo area the next
8916 time we display it---but don't redisplay it now. */
8917
8918 void
8919 truncate_echo_area (nchars)
8920 int nchars;
8921 {
8922 if (nchars == 0)
8923 echo_area_buffer[0] = Qnil;
8924 /* A null message buffer means that the frame hasn't really been
8925 initialized yet. Error messages get reported properly by
8926 cmd_error, so this must be just an informative message; toss it. */
8927 else if (!noninteractive
8928 && INTERACTIVE
8929 && !NILP (echo_area_buffer[0]))
8930 {
8931 struct frame *sf = SELECTED_FRAME ();
8932 if (FRAME_MESSAGE_BUF (sf))
8933 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8934 }
8935 }
8936
8937
8938 /* Helper function for truncate_echo_area. Truncate the current
8939 message to at most NCHARS characters. */
8940
8941 static int
8942 truncate_message_1 (nchars, a2, a3, a4)
8943 EMACS_INT nchars;
8944 Lisp_Object a2;
8945 EMACS_INT a3, a4;
8946 {
8947 if (BEG + nchars < Z)
8948 del_range (BEG + nchars, Z);
8949 if (Z == BEG)
8950 echo_area_buffer[0] = Qnil;
8951 return 0;
8952 }
8953
8954
8955 /* Set the current message to a substring of S or STRING.
8956
8957 If STRING is a Lisp string, set the message to the first NBYTES
8958 bytes from STRING. NBYTES zero means use the whole string. If
8959 STRING is multibyte, the message will be displayed multibyte.
8960
8961 If S is not null, set the message to the first LEN bytes of S. LEN
8962 zero means use the whole string. MULTIBYTE_P non-zero means S is
8963 multibyte. Display the message multibyte in that case.
8964
8965 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8966 to t before calling set_message_1 (which calls insert).
8967 */
8968
8969 void
8970 set_message (s, string, nbytes, multibyte_p)
8971 const char *s;
8972 Lisp_Object string;
8973 int nbytes, multibyte_p;
8974 {
8975 message_enable_multibyte
8976 = ((s && multibyte_p)
8977 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8978
8979 with_echo_area_buffer (0, -1, set_message_1,
8980 (EMACS_INT) s, string, nbytes, multibyte_p);
8981 message_buf_print = 0;
8982 help_echo_showing_p = 0;
8983 }
8984
8985
8986 /* Helper function for set_message. Arguments have the same meaning
8987 as there, with A1 corresponding to S and A2 corresponding to STRING
8988 This function is called with the echo area buffer being
8989 current. */
8990
8991 static int
8992 set_message_1 (a1, a2, nbytes, multibyte_p)
8993 EMACS_INT a1;
8994 Lisp_Object a2;
8995 EMACS_INT nbytes, multibyte_p;
8996 {
8997 const char *s = (const char *) a1;
8998 Lisp_Object string = a2;
8999
9000 /* Change multibyteness of the echo buffer appropriately. */
9001 if (message_enable_multibyte
9002 != !NILP (current_buffer->enable_multibyte_characters))
9003 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9004
9005 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9006
9007 /* Insert new message at BEG. */
9008 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9009
9010 if (STRINGP (string))
9011 {
9012 int nchars;
9013
9014 if (nbytes == 0)
9015 nbytes = SBYTES (string);
9016 nchars = string_byte_to_char (string, nbytes);
9017
9018 /* This function takes care of single/multibyte conversion. We
9019 just have to ensure that the echo area buffer has the right
9020 setting of enable_multibyte_characters. */
9021 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9022 }
9023 else if (s)
9024 {
9025 if (nbytes == 0)
9026 nbytes = strlen (s);
9027
9028 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9029 {
9030 /* Convert from multi-byte to single-byte. */
9031 int i, c, n;
9032 unsigned char work[1];
9033
9034 /* Convert a multibyte string to single-byte. */
9035 for (i = 0; i < nbytes; i += n)
9036 {
9037 c = string_char_and_length (s + i, nbytes - i, &n);
9038 work[0] = (ASCII_CHAR_P (c)
9039 ? c
9040 : multibyte_char_to_unibyte (c, Qnil));
9041 insert_1_both (work, 1, 1, 1, 0, 0);
9042 }
9043 }
9044 else if (!multibyte_p
9045 && !NILP (current_buffer->enable_multibyte_characters))
9046 {
9047 /* Convert from single-byte to multi-byte. */
9048 int i, c, n;
9049 const unsigned char *msg = (const unsigned char *) s;
9050 unsigned char str[MAX_MULTIBYTE_LENGTH];
9051
9052 /* Convert a single-byte string to multibyte. */
9053 for (i = 0; i < nbytes; i++)
9054 {
9055 c = msg[i];
9056 c = unibyte_char_to_multibyte (c);
9057 n = CHAR_STRING (c, str);
9058 insert_1_both (str, 1, n, 1, 0, 0);
9059 }
9060 }
9061 else
9062 insert_1 (s, nbytes, 1, 0, 0);
9063 }
9064
9065 return 0;
9066 }
9067
9068
9069 /* Clear messages. CURRENT_P non-zero means clear the current
9070 message. LAST_DISPLAYED_P non-zero means clear the message
9071 last displayed. */
9072
9073 void
9074 clear_message (current_p, last_displayed_p)
9075 int current_p, last_displayed_p;
9076 {
9077 if (current_p)
9078 {
9079 echo_area_buffer[0] = Qnil;
9080 message_cleared_p = 1;
9081 }
9082
9083 if (last_displayed_p)
9084 echo_area_buffer[1] = Qnil;
9085
9086 message_buf_print = 0;
9087 }
9088
9089 /* Clear garbaged frames.
9090
9091 This function is used where the old redisplay called
9092 redraw_garbaged_frames which in turn called redraw_frame which in
9093 turn called clear_frame. The call to clear_frame was a source of
9094 flickering. I believe a clear_frame is not necessary. It should
9095 suffice in the new redisplay to invalidate all current matrices,
9096 and ensure a complete redisplay of all windows. */
9097
9098 static void
9099 clear_garbaged_frames ()
9100 {
9101 if (frame_garbaged)
9102 {
9103 Lisp_Object tail, frame;
9104 int changed_count = 0;
9105
9106 FOR_EACH_FRAME (tail, frame)
9107 {
9108 struct frame *f = XFRAME (frame);
9109
9110 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9111 {
9112 if (f->resized_p)
9113 {
9114 Fredraw_frame (frame);
9115 f->force_flush_display_p = 1;
9116 }
9117 clear_current_matrices (f);
9118 changed_count++;
9119 f->garbaged = 0;
9120 f->resized_p = 0;
9121 }
9122 }
9123
9124 frame_garbaged = 0;
9125 if (changed_count)
9126 ++windows_or_buffers_changed;
9127 }
9128 }
9129
9130
9131 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9132 is non-zero update selected_frame. Value is non-zero if the
9133 mini-windows height has been changed. */
9134
9135 static int
9136 echo_area_display (update_frame_p)
9137 int update_frame_p;
9138 {
9139 Lisp_Object mini_window;
9140 struct window *w;
9141 struct frame *f;
9142 int window_height_changed_p = 0;
9143 struct frame *sf = SELECTED_FRAME ();
9144
9145 mini_window = FRAME_MINIBUF_WINDOW (sf);
9146 w = XWINDOW (mini_window);
9147 f = XFRAME (WINDOW_FRAME (w));
9148
9149 /* Don't display if frame is invisible or not yet initialized. */
9150 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9151 return 0;
9152
9153 #ifdef HAVE_WINDOW_SYSTEM
9154 /* When Emacs starts, selected_frame may be the initial terminal
9155 frame. If we let this through, a message would be displayed on
9156 the terminal. */
9157 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9158 return 0;
9159 #endif /* HAVE_WINDOW_SYSTEM */
9160
9161 /* Redraw garbaged frames. */
9162 if (frame_garbaged)
9163 clear_garbaged_frames ();
9164
9165 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9166 {
9167 echo_area_window = mini_window;
9168 window_height_changed_p = display_echo_area (w);
9169 w->must_be_updated_p = 1;
9170
9171 /* Update the display, unless called from redisplay_internal.
9172 Also don't update the screen during redisplay itself. The
9173 update will happen at the end of redisplay, and an update
9174 here could cause confusion. */
9175 if (update_frame_p && !redisplaying_p)
9176 {
9177 int n = 0;
9178
9179 /* If the display update has been interrupted by pending
9180 input, update mode lines in the frame. Due to the
9181 pending input, it might have been that redisplay hasn't
9182 been called, so that mode lines above the echo area are
9183 garbaged. This looks odd, so we prevent it here. */
9184 if (!display_completed)
9185 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9186
9187 if (window_height_changed_p
9188 /* Don't do this if Emacs is shutting down. Redisplay
9189 needs to run hooks. */
9190 && !NILP (Vrun_hooks))
9191 {
9192 /* Must update other windows. Likewise as in other
9193 cases, don't let this update be interrupted by
9194 pending input. */
9195 int count = SPECPDL_INDEX ();
9196 specbind (Qredisplay_dont_pause, Qt);
9197 windows_or_buffers_changed = 1;
9198 redisplay_internal (0);
9199 unbind_to (count, Qnil);
9200 }
9201 else if (FRAME_WINDOW_P (f) && n == 0)
9202 {
9203 /* Window configuration is the same as before.
9204 Can do with a display update of the echo area,
9205 unless we displayed some mode lines. */
9206 update_single_window (w, 1);
9207 FRAME_RIF (f)->flush_display (f);
9208 }
9209 else
9210 update_frame (f, 1, 1);
9211
9212 /* If cursor is in the echo area, make sure that the next
9213 redisplay displays the minibuffer, so that the cursor will
9214 be replaced with what the minibuffer wants. */
9215 if (cursor_in_echo_area)
9216 ++windows_or_buffers_changed;
9217 }
9218 }
9219 else if (!EQ (mini_window, selected_window))
9220 windows_or_buffers_changed++;
9221
9222 /* Last displayed message is now the current message. */
9223 echo_area_buffer[1] = echo_area_buffer[0];
9224 /* Inform read_char that we're not echoing. */
9225 echo_message_buffer = Qnil;
9226
9227 /* Prevent redisplay optimization in redisplay_internal by resetting
9228 this_line_start_pos. This is done because the mini-buffer now
9229 displays the message instead of its buffer text. */
9230 if (EQ (mini_window, selected_window))
9231 CHARPOS (this_line_start_pos) = 0;
9232
9233 return window_height_changed_p;
9234 }
9235
9236
9237 \f
9238 /***********************************************************************
9239 Mode Lines and Frame Titles
9240 ***********************************************************************/
9241
9242 /* A buffer for constructing non-propertized mode-line strings and
9243 frame titles in it; allocated from the heap in init_xdisp and
9244 resized as needed in store_mode_line_noprop_char. */
9245
9246 static char *mode_line_noprop_buf;
9247
9248 /* The buffer's end, and a current output position in it. */
9249
9250 static char *mode_line_noprop_buf_end;
9251 static char *mode_line_noprop_ptr;
9252
9253 #define MODE_LINE_NOPROP_LEN(start) \
9254 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9255
9256 static enum {
9257 MODE_LINE_DISPLAY = 0,
9258 MODE_LINE_TITLE,
9259 MODE_LINE_NOPROP,
9260 MODE_LINE_STRING
9261 } mode_line_target;
9262
9263 /* Alist that caches the results of :propertize.
9264 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9265 static Lisp_Object mode_line_proptrans_alist;
9266
9267 /* List of strings making up the mode-line. */
9268 static Lisp_Object mode_line_string_list;
9269
9270 /* Base face property when building propertized mode line string. */
9271 static Lisp_Object mode_line_string_face;
9272 static Lisp_Object mode_line_string_face_prop;
9273
9274
9275 /* Unwind data for mode line strings */
9276
9277 static Lisp_Object Vmode_line_unwind_vector;
9278
9279 static Lisp_Object
9280 format_mode_line_unwind_data (struct buffer *obuf,
9281 Lisp_Object owin,
9282 int save_proptrans)
9283 {
9284 Lisp_Object vector, tmp;
9285
9286 /* Reduce consing by keeping one vector in
9287 Vwith_echo_area_save_vector. */
9288 vector = Vmode_line_unwind_vector;
9289 Vmode_line_unwind_vector = Qnil;
9290
9291 if (NILP (vector))
9292 vector = Fmake_vector (make_number (8), Qnil);
9293
9294 ASET (vector, 0, make_number (mode_line_target));
9295 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9296 ASET (vector, 2, mode_line_string_list);
9297 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9298 ASET (vector, 4, mode_line_string_face);
9299 ASET (vector, 5, mode_line_string_face_prop);
9300
9301 if (obuf)
9302 XSETBUFFER (tmp, obuf);
9303 else
9304 tmp = Qnil;
9305 ASET (vector, 6, tmp);
9306 ASET (vector, 7, owin);
9307
9308 return vector;
9309 }
9310
9311 static Lisp_Object
9312 unwind_format_mode_line (vector)
9313 Lisp_Object vector;
9314 {
9315 mode_line_target = XINT (AREF (vector, 0));
9316 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9317 mode_line_string_list = AREF (vector, 2);
9318 if (! EQ (AREF (vector, 3), Qt))
9319 mode_line_proptrans_alist = AREF (vector, 3);
9320 mode_line_string_face = AREF (vector, 4);
9321 mode_line_string_face_prop = AREF (vector, 5);
9322
9323 if (!NILP (AREF (vector, 7)))
9324 /* Select window before buffer, since it may change the buffer. */
9325 Fselect_window (AREF (vector, 7), Qt);
9326
9327 if (!NILP (AREF (vector, 6)))
9328 {
9329 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9330 ASET (vector, 6, Qnil);
9331 }
9332
9333 Vmode_line_unwind_vector = vector;
9334 return Qnil;
9335 }
9336
9337
9338 /* Store a single character C for the frame title in mode_line_noprop_buf.
9339 Re-allocate mode_line_noprop_buf if necessary. */
9340
9341 static void
9342 #ifdef PROTOTYPES
9343 store_mode_line_noprop_char (char c)
9344 #else
9345 store_mode_line_noprop_char (c)
9346 char c;
9347 #endif
9348 {
9349 /* If output position has reached the end of the allocated buffer,
9350 double the buffer's size. */
9351 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9352 {
9353 int len = MODE_LINE_NOPROP_LEN (0);
9354 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9355 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9356 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9357 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9358 }
9359
9360 *mode_line_noprop_ptr++ = c;
9361 }
9362
9363
9364 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9365 mode_line_noprop_ptr. STR is the string to store. Do not copy
9366 characters that yield more columns than PRECISION; PRECISION <= 0
9367 means copy the whole string. Pad with spaces until FIELD_WIDTH
9368 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9369 pad. Called from display_mode_element when it is used to build a
9370 frame title. */
9371
9372 static int
9373 store_mode_line_noprop (str, field_width, precision)
9374 const unsigned char *str;
9375 int field_width, precision;
9376 {
9377 int n = 0;
9378 int dummy, nbytes;
9379
9380 /* Copy at most PRECISION chars from STR. */
9381 nbytes = strlen (str);
9382 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9383 while (nbytes--)
9384 store_mode_line_noprop_char (*str++);
9385
9386 /* Fill up with spaces until FIELD_WIDTH reached. */
9387 while (field_width > 0
9388 && n < field_width)
9389 {
9390 store_mode_line_noprop_char (' ');
9391 ++n;
9392 }
9393
9394 return n;
9395 }
9396
9397 /***********************************************************************
9398 Frame Titles
9399 ***********************************************************************/
9400
9401 #ifdef HAVE_WINDOW_SYSTEM
9402
9403 /* Set the title of FRAME, if it has changed. The title format is
9404 Vicon_title_format if FRAME is iconified, otherwise it is
9405 frame_title_format. */
9406
9407 static void
9408 x_consider_frame_title (frame)
9409 Lisp_Object frame;
9410 {
9411 struct frame *f = XFRAME (frame);
9412
9413 if (FRAME_WINDOW_P (f)
9414 || FRAME_MINIBUF_ONLY_P (f)
9415 || f->explicit_name)
9416 {
9417 /* Do we have more than one visible frame on this X display? */
9418 Lisp_Object tail;
9419 Lisp_Object fmt;
9420 int title_start;
9421 char *title;
9422 int len;
9423 struct it it;
9424 int count = SPECPDL_INDEX ();
9425
9426 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9427 {
9428 Lisp_Object other_frame = XCAR (tail);
9429 struct frame *tf = XFRAME (other_frame);
9430
9431 if (tf != f
9432 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9433 && !FRAME_MINIBUF_ONLY_P (tf)
9434 && !EQ (other_frame, tip_frame)
9435 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9436 break;
9437 }
9438
9439 /* Set global variable indicating that multiple frames exist. */
9440 multiple_frames = CONSP (tail);
9441
9442 /* Switch to the buffer of selected window of the frame. Set up
9443 mode_line_target so that display_mode_element will output into
9444 mode_line_noprop_buf; then display the title. */
9445 record_unwind_protect (unwind_format_mode_line,
9446 format_mode_line_unwind_data
9447 (current_buffer, selected_window, 0));
9448
9449 Fselect_window (f->selected_window, Qt);
9450 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9451 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9452
9453 mode_line_target = MODE_LINE_TITLE;
9454 title_start = MODE_LINE_NOPROP_LEN (0);
9455 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9456 NULL, DEFAULT_FACE_ID);
9457 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9458 len = MODE_LINE_NOPROP_LEN (title_start);
9459 title = mode_line_noprop_buf + title_start;
9460 unbind_to (count, Qnil);
9461
9462 /* Set the title only if it's changed. This avoids consing in
9463 the common case where it hasn't. (If it turns out that we've
9464 already wasted too much time by walking through the list with
9465 display_mode_element, then we might need to optimize at a
9466 higher level than this.) */
9467 if (! STRINGP (f->name)
9468 || SBYTES (f->name) != len
9469 || bcmp (title, SDATA (f->name), len) != 0)
9470 {
9471 #ifdef HAVE_NS
9472 if (FRAME_NS_P (f))
9473 {
9474 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9475 {
9476 if (EQ (fmt, Qt))
9477 ns_set_name_as_filename (f);
9478 else
9479 x_implicitly_set_name (f, make_string(title, len),
9480 Qnil);
9481 }
9482 }
9483 else
9484 #endif
9485 x_implicitly_set_name (f, make_string (title, len), Qnil);
9486 }
9487 #ifdef HAVE_NS
9488 if (FRAME_NS_P (f))
9489 {
9490 /* do this also for frames with explicit names */
9491 ns_implicitly_set_icon_type(f);
9492 ns_set_doc_edited(f, Fbuffer_modified_p
9493 (XWINDOW (f->selected_window)->buffer), Qnil);
9494 }
9495 #endif
9496 }
9497 }
9498
9499 #endif /* not HAVE_WINDOW_SYSTEM */
9500
9501
9502
9503 \f
9504 /***********************************************************************
9505 Menu Bars
9506 ***********************************************************************/
9507
9508
9509 /* Prepare for redisplay by updating menu-bar item lists when
9510 appropriate. This can call eval. */
9511
9512 void
9513 prepare_menu_bars ()
9514 {
9515 int all_windows;
9516 struct gcpro gcpro1, gcpro2;
9517 struct frame *f;
9518 Lisp_Object tooltip_frame;
9519
9520 #ifdef HAVE_WINDOW_SYSTEM
9521 tooltip_frame = tip_frame;
9522 #else
9523 tooltip_frame = Qnil;
9524 #endif
9525
9526 /* Update all frame titles based on their buffer names, etc. We do
9527 this before the menu bars so that the buffer-menu will show the
9528 up-to-date frame titles. */
9529 #ifdef HAVE_WINDOW_SYSTEM
9530 if (windows_or_buffers_changed || update_mode_lines)
9531 {
9532 Lisp_Object tail, frame;
9533
9534 FOR_EACH_FRAME (tail, frame)
9535 {
9536 f = XFRAME (frame);
9537 if (!EQ (frame, tooltip_frame)
9538 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9539 x_consider_frame_title (frame);
9540 }
9541 }
9542 #endif /* HAVE_WINDOW_SYSTEM */
9543
9544 /* Update the menu bar item lists, if appropriate. This has to be
9545 done before any actual redisplay or generation of display lines. */
9546 all_windows = (update_mode_lines
9547 || buffer_shared > 1
9548 || windows_or_buffers_changed);
9549 if (all_windows)
9550 {
9551 Lisp_Object tail, frame;
9552 int count = SPECPDL_INDEX ();
9553 /* 1 means that update_menu_bar has run its hooks
9554 so any further calls to update_menu_bar shouldn't do so again. */
9555 int menu_bar_hooks_run = 0;
9556
9557 record_unwind_save_match_data ();
9558
9559 FOR_EACH_FRAME (tail, frame)
9560 {
9561 f = XFRAME (frame);
9562
9563 /* Ignore tooltip frame. */
9564 if (EQ (frame, tooltip_frame))
9565 continue;
9566
9567 /* If a window on this frame changed size, report that to
9568 the user and clear the size-change flag. */
9569 if (FRAME_WINDOW_SIZES_CHANGED (f))
9570 {
9571 Lisp_Object functions;
9572
9573 /* Clear flag first in case we get an error below. */
9574 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9575 functions = Vwindow_size_change_functions;
9576 GCPRO2 (tail, functions);
9577
9578 while (CONSP (functions))
9579 {
9580 if (!EQ (XCAR (functions), Qt))
9581 call1 (XCAR (functions), frame);
9582 functions = XCDR (functions);
9583 }
9584 UNGCPRO;
9585 }
9586
9587 GCPRO1 (tail);
9588 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9589 #ifdef HAVE_WINDOW_SYSTEM
9590 update_tool_bar (f, 0);
9591 #endif
9592 UNGCPRO;
9593 }
9594
9595 unbind_to (count, Qnil);
9596 }
9597 else
9598 {
9599 struct frame *sf = SELECTED_FRAME ();
9600 update_menu_bar (sf, 1, 0);
9601 #ifdef HAVE_WINDOW_SYSTEM
9602 update_tool_bar (sf, 1);
9603 #endif
9604 }
9605
9606 /* Motif needs this. See comment in xmenu.c. Turn it off when
9607 pending_menu_activation is not defined. */
9608 #ifdef USE_X_TOOLKIT
9609 pending_menu_activation = 0;
9610 #endif
9611 }
9612
9613
9614 /* Update the menu bar item list for frame F. This has to be done
9615 before we start to fill in any display lines, because it can call
9616 eval.
9617
9618 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9619
9620 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9621 already ran the menu bar hooks for this redisplay, so there
9622 is no need to run them again. The return value is the
9623 updated value of this flag, to pass to the next call. */
9624
9625 static int
9626 update_menu_bar (f, save_match_data, hooks_run)
9627 struct frame *f;
9628 int save_match_data;
9629 int hooks_run;
9630 {
9631 Lisp_Object window;
9632 register struct window *w;
9633
9634 /* If called recursively during a menu update, do nothing. This can
9635 happen when, for instance, an activate-menubar-hook causes a
9636 redisplay. */
9637 if (inhibit_menubar_update)
9638 return hooks_run;
9639
9640 window = FRAME_SELECTED_WINDOW (f);
9641 w = XWINDOW (window);
9642
9643 if (FRAME_WINDOW_P (f)
9644 ?
9645 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9646 || defined (HAVE_NS) || defined (USE_GTK)
9647 FRAME_EXTERNAL_MENU_BAR (f)
9648 #else
9649 FRAME_MENU_BAR_LINES (f) > 0
9650 #endif
9651 : FRAME_MENU_BAR_LINES (f) > 0)
9652 {
9653 /* If the user has switched buffers or windows, we need to
9654 recompute to reflect the new bindings. But we'll
9655 recompute when update_mode_lines is set too; that means
9656 that people can use force-mode-line-update to request
9657 that the menu bar be recomputed. The adverse effect on
9658 the rest of the redisplay algorithm is about the same as
9659 windows_or_buffers_changed anyway. */
9660 if (windows_or_buffers_changed
9661 /* This used to test w->update_mode_line, but we believe
9662 there is no need to recompute the menu in that case. */
9663 || update_mode_lines
9664 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9665 < BUF_MODIFF (XBUFFER (w->buffer)))
9666 != !NILP (w->last_had_star))
9667 || ((!NILP (Vtransient_mark_mode)
9668 && !NILP (XBUFFER (w->buffer)->mark_active))
9669 != !NILP (w->region_showing)))
9670 {
9671 struct buffer *prev = current_buffer;
9672 int count = SPECPDL_INDEX ();
9673
9674 specbind (Qinhibit_menubar_update, Qt);
9675
9676 set_buffer_internal_1 (XBUFFER (w->buffer));
9677 if (save_match_data)
9678 record_unwind_save_match_data ();
9679 if (NILP (Voverriding_local_map_menu_flag))
9680 {
9681 specbind (Qoverriding_terminal_local_map, Qnil);
9682 specbind (Qoverriding_local_map, Qnil);
9683 }
9684
9685 if (!hooks_run)
9686 {
9687 /* Run the Lucid hook. */
9688 safe_run_hooks (Qactivate_menubar_hook);
9689
9690 /* If it has changed current-menubar from previous value,
9691 really recompute the menu-bar from the value. */
9692 if (! NILP (Vlucid_menu_bar_dirty_flag))
9693 call0 (Qrecompute_lucid_menubar);
9694
9695 safe_run_hooks (Qmenu_bar_update_hook);
9696
9697 hooks_run = 1;
9698 }
9699
9700 XSETFRAME (Vmenu_updating_frame, f);
9701 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9702
9703 /* Redisplay the menu bar in case we changed it. */
9704 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9705 || defined (HAVE_NS) || defined (USE_GTK)
9706 if (FRAME_WINDOW_P (f))
9707 {
9708 #if defined (HAVE_NS)
9709 /* All frames on Mac OS share the same menubar. So only
9710 the selected frame should be allowed to set it. */
9711 if (f == SELECTED_FRAME ())
9712 #endif
9713 set_frame_menubar (f, 0, 0);
9714 }
9715 else
9716 /* On a terminal screen, the menu bar is an ordinary screen
9717 line, and this makes it get updated. */
9718 w->update_mode_line = Qt;
9719 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9720 /* In the non-toolkit version, the menu bar is an ordinary screen
9721 line, and this makes it get updated. */
9722 w->update_mode_line = Qt;
9723 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9724
9725 unbind_to (count, Qnil);
9726 set_buffer_internal_1 (prev);
9727 }
9728 }
9729
9730 return hooks_run;
9731 }
9732
9733
9734 \f
9735 /***********************************************************************
9736 Output Cursor
9737 ***********************************************************************/
9738
9739 #ifdef HAVE_WINDOW_SYSTEM
9740
9741 /* EXPORT:
9742 Nominal cursor position -- where to draw output.
9743 HPOS and VPOS are window relative glyph matrix coordinates.
9744 X and Y are window relative pixel coordinates. */
9745
9746 struct cursor_pos output_cursor;
9747
9748
9749 /* EXPORT:
9750 Set the global variable output_cursor to CURSOR. All cursor
9751 positions are relative to updated_window. */
9752
9753 void
9754 set_output_cursor (cursor)
9755 struct cursor_pos *cursor;
9756 {
9757 output_cursor.hpos = cursor->hpos;
9758 output_cursor.vpos = cursor->vpos;
9759 output_cursor.x = cursor->x;
9760 output_cursor.y = cursor->y;
9761 }
9762
9763
9764 /* EXPORT for RIF:
9765 Set a nominal cursor position.
9766
9767 HPOS and VPOS are column/row positions in a window glyph matrix. X
9768 and Y are window text area relative pixel positions.
9769
9770 If this is done during an update, updated_window will contain the
9771 window that is being updated and the position is the future output
9772 cursor position for that window. If updated_window is null, use
9773 selected_window and display the cursor at the given position. */
9774
9775 void
9776 x_cursor_to (vpos, hpos, y, x)
9777 int vpos, hpos, y, x;
9778 {
9779 struct window *w;
9780
9781 /* If updated_window is not set, work on selected_window. */
9782 if (updated_window)
9783 w = updated_window;
9784 else
9785 w = XWINDOW (selected_window);
9786
9787 /* Set the output cursor. */
9788 output_cursor.hpos = hpos;
9789 output_cursor.vpos = vpos;
9790 output_cursor.x = x;
9791 output_cursor.y = y;
9792
9793 /* If not called as part of an update, really display the cursor.
9794 This will also set the cursor position of W. */
9795 if (updated_window == NULL)
9796 {
9797 BLOCK_INPUT;
9798 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9799 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9800 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9801 UNBLOCK_INPUT;
9802 }
9803 }
9804
9805 #endif /* HAVE_WINDOW_SYSTEM */
9806
9807 \f
9808 /***********************************************************************
9809 Tool-bars
9810 ***********************************************************************/
9811
9812 #ifdef HAVE_WINDOW_SYSTEM
9813
9814 /* Where the mouse was last time we reported a mouse event. */
9815
9816 FRAME_PTR last_mouse_frame;
9817
9818 /* Tool-bar item index of the item on which a mouse button was pressed
9819 or -1. */
9820
9821 int last_tool_bar_item;
9822
9823
9824 static Lisp_Object
9825 update_tool_bar_unwind (frame)
9826 Lisp_Object frame;
9827 {
9828 selected_frame = frame;
9829 return Qnil;
9830 }
9831
9832 /* Update the tool-bar item list for frame F. This has to be done
9833 before we start to fill in any display lines. Called from
9834 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9835 and restore it here. */
9836
9837 static void
9838 update_tool_bar (f, save_match_data)
9839 struct frame *f;
9840 int save_match_data;
9841 {
9842 #if defined (USE_GTK) || defined (HAVE_NS)
9843 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9844 #else
9845 int do_update = WINDOWP (f->tool_bar_window)
9846 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9847 #endif
9848
9849 if (do_update)
9850 {
9851 Lisp_Object window;
9852 struct window *w;
9853
9854 window = FRAME_SELECTED_WINDOW (f);
9855 w = XWINDOW (window);
9856
9857 /* If the user has switched buffers or windows, we need to
9858 recompute to reflect the new bindings. But we'll
9859 recompute when update_mode_lines is set too; that means
9860 that people can use force-mode-line-update to request
9861 that the menu bar be recomputed. The adverse effect on
9862 the rest of the redisplay algorithm is about the same as
9863 windows_or_buffers_changed anyway. */
9864 if (windows_or_buffers_changed
9865 || !NILP (w->update_mode_line)
9866 || update_mode_lines
9867 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9868 < BUF_MODIFF (XBUFFER (w->buffer)))
9869 != !NILP (w->last_had_star))
9870 || ((!NILP (Vtransient_mark_mode)
9871 && !NILP (XBUFFER (w->buffer)->mark_active))
9872 != !NILP (w->region_showing)))
9873 {
9874 struct buffer *prev = current_buffer;
9875 int count = SPECPDL_INDEX ();
9876 Lisp_Object frame, new_tool_bar;
9877 int new_n_tool_bar;
9878 struct gcpro gcpro1;
9879
9880 /* Set current_buffer to the buffer of the selected
9881 window of the frame, so that we get the right local
9882 keymaps. */
9883 set_buffer_internal_1 (XBUFFER (w->buffer));
9884
9885 /* Save match data, if we must. */
9886 if (save_match_data)
9887 record_unwind_save_match_data ();
9888
9889 /* Make sure that we don't accidentally use bogus keymaps. */
9890 if (NILP (Voverriding_local_map_menu_flag))
9891 {
9892 specbind (Qoverriding_terminal_local_map, Qnil);
9893 specbind (Qoverriding_local_map, Qnil);
9894 }
9895
9896 GCPRO1 (new_tool_bar);
9897
9898 /* We must temporarily set the selected frame to this frame
9899 before calling tool_bar_items, because the calculation of
9900 the tool-bar keymap uses the selected frame (see
9901 `tool-bar-make-keymap' in tool-bar.el). */
9902 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9903 XSETFRAME (frame, f);
9904 selected_frame = frame;
9905
9906 /* Build desired tool-bar items from keymaps. */
9907 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9908 &new_n_tool_bar);
9909
9910 /* Redisplay the tool-bar if we changed it. */
9911 if (new_n_tool_bar != f->n_tool_bar_items
9912 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9913 {
9914 /* Redisplay that happens asynchronously due to an expose event
9915 may access f->tool_bar_items. Make sure we update both
9916 variables within BLOCK_INPUT so no such event interrupts. */
9917 BLOCK_INPUT;
9918 f->tool_bar_items = new_tool_bar;
9919 f->n_tool_bar_items = new_n_tool_bar;
9920 w->update_mode_line = Qt;
9921 UNBLOCK_INPUT;
9922 }
9923
9924 UNGCPRO;
9925
9926 unbind_to (count, Qnil);
9927 set_buffer_internal_1 (prev);
9928 }
9929 }
9930 }
9931
9932
9933 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9934 F's desired tool-bar contents. F->tool_bar_items must have
9935 been set up previously by calling prepare_menu_bars. */
9936
9937 static void
9938 build_desired_tool_bar_string (f)
9939 struct frame *f;
9940 {
9941 int i, size, size_needed;
9942 struct gcpro gcpro1, gcpro2, gcpro3;
9943 Lisp_Object image, plist, props;
9944
9945 image = plist = props = Qnil;
9946 GCPRO3 (image, plist, props);
9947
9948 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9949 Otherwise, make a new string. */
9950
9951 /* The size of the string we might be able to reuse. */
9952 size = (STRINGP (f->desired_tool_bar_string)
9953 ? SCHARS (f->desired_tool_bar_string)
9954 : 0);
9955
9956 /* We need one space in the string for each image. */
9957 size_needed = f->n_tool_bar_items;
9958
9959 /* Reuse f->desired_tool_bar_string, if possible. */
9960 if (size < size_needed || NILP (f->desired_tool_bar_string))
9961 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9962 make_number (' '));
9963 else
9964 {
9965 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9966 Fremove_text_properties (make_number (0), make_number (size),
9967 props, f->desired_tool_bar_string);
9968 }
9969
9970 /* Put a `display' property on the string for the images to display,
9971 put a `menu_item' property on tool-bar items with a value that
9972 is the index of the item in F's tool-bar item vector. */
9973 for (i = 0; i < f->n_tool_bar_items; ++i)
9974 {
9975 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9976
9977 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9978 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9979 int hmargin, vmargin, relief, idx, end;
9980 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9981
9982 /* If image is a vector, choose the image according to the
9983 button state. */
9984 image = PROP (TOOL_BAR_ITEM_IMAGES);
9985 if (VECTORP (image))
9986 {
9987 if (enabled_p)
9988 idx = (selected_p
9989 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9990 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9991 else
9992 idx = (selected_p
9993 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9994 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9995
9996 xassert (ASIZE (image) >= idx);
9997 image = AREF (image, idx);
9998 }
9999 else
10000 idx = -1;
10001
10002 /* Ignore invalid image specifications. */
10003 if (!valid_image_p (image))
10004 continue;
10005
10006 /* Display the tool-bar button pressed, or depressed. */
10007 plist = Fcopy_sequence (XCDR (image));
10008
10009 /* Compute margin and relief to draw. */
10010 relief = (tool_bar_button_relief >= 0
10011 ? tool_bar_button_relief
10012 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10013 hmargin = vmargin = relief;
10014
10015 if (INTEGERP (Vtool_bar_button_margin)
10016 && XINT (Vtool_bar_button_margin) > 0)
10017 {
10018 hmargin += XFASTINT (Vtool_bar_button_margin);
10019 vmargin += XFASTINT (Vtool_bar_button_margin);
10020 }
10021 else if (CONSP (Vtool_bar_button_margin))
10022 {
10023 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10024 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10025 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10026
10027 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10028 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10029 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10030 }
10031
10032 if (auto_raise_tool_bar_buttons_p)
10033 {
10034 /* Add a `:relief' property to the image spec if the item is
10035 selected. */
10036 if (selected_p)
10037 {
10038 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10039 hmargin -= relief;
10040 vmargin -= relief;
10041 }
10042 }
10043 else
10044 {
10045 /* If image is selected, display it pressed, i.e. with a
10046 negative relief. If it's not selected, display it with a
10047 raised relief. */
10048 plist = Fplist_put (plist, QCrelief,
10049 (selected_p
10050 ? make_number (-relief)
10051 : make_number (relief)));
10052 hmargin -= relief;
10053 vmargin -= relief;
10054 }
10055
10056 /* Put a margin around the image. */
10057 if (hmargin || vmargin)
10058 {
10059 if (hmargin == vmargin)
10060 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10061 else
10062 plist = Fplist_put (plist, QCmargin,
10063 Fcons (make_number (hmargin),
10064 make_number (vmargin)));
10065 }
10066
10067 /* If button is not enabled, and we don't have special images
10068 for the disabled state, make the image appear disabled by
10069 applying an appropriate algorithm to it. */
10070 if (!enabled_p && idx < 0)
10071 plist = Fplist_put (plist, QCconversion, Qdisabled);
10072
10073 /* Put a `display' text property on the string for the image to
10074 display. Put a `menu-item' property on the string that gives
10075 the start of this item's properties in the tool-bar items
10076 vector. */
10077 image = Fcons (Qimage, plist);
10078 props = list4 (Qdisplay, image,
10079 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10080
10081 /* Let the last image hide all remaining spaces in the tool bar
10082 string. The string can be longer than needed when we reuse a
10083 previous string. */
10084 if (i + 1 == f->n_tool_bar_items)
10085 end = SCHARS (f->desired_tool_bar_string);
10086 else
10087 end = i + 1;
10088 Fadd_text_properties (make_number (i), make_number (end),
10089 props, f->desired_tool_bar_string);
10090 #undef PROP
10091 }
10092
10093 UNGCPRO;
10094 }
10095
10096
10097 /* Display one line of the tool-bar of frame IT->f.
10098
10099 HEIGHT specifies the desired height of the tool-bar line.
10100 If the actual height of the glyph row is less than HEIGHT, the
10101 row's height is increased to HEIGHT, and the icons are centered
10102 vertically in the new height.
10103
10104 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10105 count a final empty row in case the tool-bar width exactly matches
10106 the window width.
10107 */
10108
10109 static void
10110 display_tool_bar_line (it, height)
10111 struct it *it;
10112 int height;
10113 {
10114 struct glyph_row *row = it->glyph_row;
10115 int max_x = it->last_visible_x;
10116 struct glyph *last;
10117
10118 prepare_desired_row (row);
10119 row->y = it->current_y;
10120
10121 /* Note that this isn't made use of if the face hasn't a box,
10122 so there's no need to check the face here. */
10123 it->start_of_box_run_p = 1;
10124
10125 while (it->current_x < max_x)
10126 {
10127 int x, n_glyphs_before, i, nglyphs;
10128 struct it it_before;
10129
10130 /* Get the next display element. */
10131 if (!get_next_display_element (it))
10132 {
10133 /* Don't count empty row if we are counting needed tool-bar lines. */
10134 if (height < 0 && !it->hpos)
10135 return;
10136 break;
10137 }
10138
10139 /* Produce glyphs. */
10140 n_glyphs_before = row->used[TEXT_AREA];
10141 it_before = *it;
10142
10143 PRODUCE_GLYPHS (it);
10144
10145 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10146 i = 0;
10147 x = it_before.current_x;
10148 while (i < nglyphs)
10149 {
10150 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10151
10152 if (x + glyph->pixel_width > max_x)
10153 {
10154 /* Glyph doesn't fit on line. Backtrack. */
10155 row->used[TEXT_AREA] = n_glyphs_before;
10156 *it = it_before;
10157 /* If this is the only glyph on this line, it will never fit on the
10158 toolbar, so skip it. But ensure there is at least one glyph,
10159 so we don't accidentally disable the tool-bar. */
10160 if (n_glyphs_before == 0
10161 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10162 break;
10163 goto out;
10164 }
10165
10166 ++it->hpos;
10167 x += glyph->pixel_width;
10168 ++i;
10169 }
10170
10171 /* Stop at line ends. */
10172 if (ITERATOR_AT_END_OF_LINE_P (it))
10173 break;
10174
10175 set_iterator_to_next (it, 1);
10176 }
10177
10178 out:;
10179
10180 row->displays_text_p = row->used[TEXT_AREA] != 0;
10181
10182 /* Use default face for the border below the tool bar.
10183
10184 FIXME: When auto-resize-tool-bars is grow-only, there is
10185 no additional border below the possibly empty tool-bar lines.
10186 So to make the extra empty lines look "normal", we have to
10187 use the tool-bar face for the border too. */
10188 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10189 it->face_id = DEFAULT_FACE_ID;
10190
10191 extend_face_to_end_of_line (it);
10192 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10193 last->right_box_line_p = 1;
10194 if (last == row->glyphs[TEXT_AREA])
10195 last->left_box_line_p = 1;
10196
10197 /* Make line the desired height and center it vertically. */
10198 if ((height -= it->max_ascent + it->max_descent) > 0)
10199 {
10200 /* Don't add more than one line height. */
10201 height %= FRAME_LINE_HEIGHT (it->f);
10202 it->max_ascent += height / 2;
10203 it->max_descent += (height + 1) / 2;
10204 }
10205
10206 compute_line_metrics (it);
10207
10208 /* If line is empty, make it occupy the rest of the tool-bar. */
10209 if (!row->displays_text_p)
10210 {
10211 row->height = row->phys_height = it->last_visible_y - row->y;
10212 row->visible_height = row->height;
10213 row->ascent = row->phys_ascent = 0;
10214 row->extra_line_spacing = 0;
10215 }
10216
10217 row->full_width_p = 1;
10218 row->continued_p = 0;
10219 row->truncated_on_left_p = 0;
10220 row->truncated_on_right_p = 0;
10221
10222 it->current_x = it->hpos = 0;
10223 it->current_y += row->height;
10224 ++it->vpos;
10225 ++it->glyph_row;
10226 }
10227
10228
10229 /* Max tool-bar height. */
10230
10231 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10232 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10233
10234 /* Value is the number of screen lines needed to make all tool-bar
10235 items of frame F visible. The number of actual rows needed is
10236 returned in *N_ROWS if non-NULL. */
10237
10238 static int
10239 tool_bar_lines_needed (f, n_rows)
10240 struct frame *f;
10241 int *n_rows;
10242 {
10243 struct window *w = XWINDOW (f->tool_bar_window);
10244 struct it it;
10245 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10246 the desired matrix, so use (unused) mode-line row as temporary row to
10247 avoid destroying the first tool-bar row. */
10248 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10249
10250 /* Initialize an iterator for iteration over
10251 F->desired_tool_bar_string in the tool-bar window of frame F. */
10252 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10253 it.first_visible_x = 0;
10254 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10255 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10256
10257 while (!ITERATOR_AT_END_P (&it))
10258 {
10259 clear_glyph_row (temp_row);
10260 it.glyph_row = temp_row;
10261 display_tool_bar_line (&it, -1);
10262 }
10263 clear_glyph_row (temp_row);
10264
10265 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10266 if (n_rows)
10267 *n_rows = it.vpos > 0 ? it.vpos : -1;
10268
10269 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10270 }
10271
10272
10273 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10274 0, 1, 0,
10275 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10276 (frame)
10277 Lisp_Object frame;
10278 {
10279 struct frame *f;
10280 struct window *w;
10281 int nlines = 0;
10282
10283 if (NILP (frame))
10284 frame = selected_frame;
10285 else
10286 CHECK_FRAME (frame);
10287 f = XFRAME (frame);
10288
10289 if (WINDOWP (f->tool_bar_window)
10290 || (w = XWINDOW (f->tool_bar_window),
10291 WINDOW_TOTAL_LINES (w) > 0))
10292 {
10293 update_tool_bar (f, 1);
10294 if (f->n_tool_bar_items)
10295 {
10296 build_desired_tool_bar_string (f);
10297 nlines = tool_bar_lines_needed (f, NULL);
10298 }
10299 }
10300
10301 return make_number (nlines);
10302 }
10303
10304
10305 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10306 height should be changed. */
10307
10308 static int
10309 redisplay_tool_bar (f)
10310 struct frame *f;
10311 {
10312 struct window *w;
10313 struct it it;
10314 struct glyph_row *row;
10315
10316 #if defined (USE_GTK) || defined (HAVE_NS)
10317 if (FRAME_EXTERNAL_TOOL_BAR (f))
10318 update_frame_tool_bar (f);
10319 return 0;
10320 #endif
10321
10322 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10323 do anything. This means you must start with tool-bar-lines
10324 non-zero to get the auto-sizing effect. Or in other words, you
10325 can turn off tool-bars by specifying tool-bar-lines zero. */
10326 if (!WINDOWP (f->tool_bar_window)
10327 || (w = XWINDOW (f->tool_bar_window),
10328 WINDOW_TOTAL_LINES (w) == 0))
10329 return 0;
10330
10331 /* Set up an iterator for the tool-bar window. */
10332 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10333 it.first_visible_x = 0;
10334 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10335 row = it.glyph_row;
10336
10337 /* Build a string that represents the contents of the tool-bar. */
10338 build_desired_tool_bar_string (f);
10339 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10340
10341 if (f->n_tool_bar_rows == 0)
10342 {
10343 int nlines;
10344
10345 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10346 nlines != WINDOW_TOTAL_LINES (w)))
10347 {
10348 extern Lisp_Object Qtool_bar_lines;
10349 Lisp_Object frame;
10350 int old_height = WINDOW_TOTAL_LINES (w);
10351
10352 XSETFRAME (frame, f);
10353 Fmodify_frame_parameters (frame,
10354 Fcons (Fcons (Qtool_bar_lines,
10355 make_number (nlines)),
10356 Qnil));
10357 if (WINDOW_TOTAL_LINES (w) != old_height)
10358 {
10359 clear_glyph_matrix (w->desired_matrix);
10360 fonts_changed_p = 1;
10361 return 1;
10362 }
10363 }
10364 }
10365
10366 /* Display as many lines as needed to display all tool-bar items. */
10367
10368 if (f->n_tool_bar_rows > 0)
10369 {
10370 int border, rows, height, extra;
10371
10372 if (INTEGERP (Vtool_bar_border))
10373 border = XINT (Vtool_bar_border);
10374 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10375 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10376 else if (EQ (Vtool_bar_border, Qborder_width))
10377 border = f->border_width;
10378 else
10379 border = 0;
10380 if (border < 0)
10381 border = 0;
10382
10383 rows = f->n_tool_bar_rows;
10384 height = max (1, (it.last_visible_y - border) / rows);
10385 extra = it.last_visible_y - border - height * rows;
10386
10387 while (it.current_y < it.last_visible_y)
10388 {
10389 int h = 0;
10390 if (extra > 0 && rows-- > 0)
10391 {
10392 h = (extra + rows - 1) / rows;
10393 extra -= h;
10394 }
10395 display_tool_bar_line (&it, height + h);
10396 }
10397 }
10398 else
10399 {
10400 while (it.current_y < it.last_visible_y)
10401 display_tool_bar_line (&it, 0);
10402 }
10403
10404 /* It doesn't make much sense to try scrolling in the tool-bar
10405 window, so don't do it. */
10406 w->desired_matrix->no_scrolling_p = 1;
10407 w->must_be_updated_p = 1;
10408
10409 if (!NILP (Vauto_resize_tool_bars))
10410 {
10411 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10412 int change_height_p = 0;
10413
10414 /* If we couldn't display everything, change the tool-bar's
10415 height if there is room for more. */
10416 if (IT_STRING_CHARPOS (it) < it.end_charpos
10417 && it.current_y < max_tool_bar_height)
10418 change_height_p = 1;
10419
10420 row = it.glyph_row - 1;
10421
10422 /* If there are blank lines at the end, except for a partially
10423 visible blank line at the end that is smaller than
10424 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10425 if (!row->displays_text_p
10426 && row->height >= FRAME_LINE_HEIGHT (f))
10427 change_height_p = 1;
10428
10429 /* If row displays tool-bar items, but is partially visible,
10430 change the tool-bar's height. */
10431 if (row->displays_text_p
10432 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10433 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10434 change_height_p = 1;
10435
10436 /* Resize windows as needed by changing the `tool-bar-lines'
10437 frame parameter. */
10438 if (change_height_p)
10439 {
10440 extern Lisp_Object Qtool_bar_lines;
10441 Lisp_Object frame;
10442 int old_height = WINDOW_TOTAL_LINES (w);
10443 int nrows;
10444 int nlines = tool_bar_lines_needed (f, &nrows);
10445
10446 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10447 && !f->minimize_tool_bar_window_p)
10448 ? (nlines > old_height)
10449 : (nlines != old_height));
10450 f->minimize_tool_bar_window_p = 0;
10451
10452 if (change_height_p)
10453 {
10454 XSETFRAME (frame, f);
10455 Fmodify_frame_parameters (frame,
10456 Fcons (Fcons (Qtool_bar_lines,
10457 make_number (nlines)),
10458 Qnil));
10459 if (WINDOW_TOTAL_LINES (w) != old_height)
10460 {
10461 clear_glyph_matrix (w->desired_matrix);
10462 f->n_tool_bar_rows = nrows;
10463 fonts_changed_p = 1;
10464 return 1;
10465 }
10466 }
10467 }
10468 }
10469
10470 f->minimize_tool_bar_window_p = 0;
10471 return 0;
10472 }
10473
10474
10475 /* Get information about the tool-bar item which is displayed in GLYPH
10476 on frame F. Return in *PROP_IDX the index where tool-bar item
10477 properties start in F->tool_bar_items. Value is zero if
10478 GLYPH doesn't display a tool-bar item. */
10479
10480 static int
10481 tool_bar_item_info (f, glyph, prop_idx)
10482 struct frame *f;
10483 struct glyph *glyph;
10484 int *prop_idx;
10485 {
10486 Lisp_Object prop;
10487 int success_p;
10488 int charpos;
10489
10490 /* This function can be called asynchronously, which means we must
10491 exclude any possibility that Fget_text_property signals an
10492 error. */
10493 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10494 charpos = max (0, charpos);
10495
10496 /* Get the text property `menu-item' at pos. The value of that
10497 property is the start index of this item's properties in
10498 F->tool_bar_items. */
10499 prop = Fget_text_property (make_number (charpos),
10500 Qmenu_item, f->current_tool_bar_string);
10501 if (INTEGERP (prop))
10502 {
10503 *prop_idx = XINT (prop);
10504 success_p = 1;
10505 }
10506 else
10507 success_p = 0;
10508
10509 return success_p;
10510 }
10511
10512 \f
10513 /* Get information about the tool-bar item at position X/Y on frame F.
10514 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10515 the current matrix of the tool-bar window of F, or NULL if not
10516 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10517 item in F->tool_bar_items. Value is
10518
10519 -1 if X/Y is not on a tool-bar item
10520 0 if X/Y is on the same item that was highlighted before.
10521 1 otherwise. */
10522
10523 static int
10524 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10525 struct frame *f;
10526 int x, y;
10527 struct glyph **glyph;
10528 int *hpos, *vpos, *prop_idx;
10529 {
10530 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10531 struct window *w = XWINDOW (f->tool_bar_window);
10532 int area;
10533
10534 /* Find the glyph under X/Y. */
10535 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10536 if (*glyph == NULL)
10537 return -1;
10538
10539 /* Get the start of this tool-bar item's properties in
10540 f->tool_bar_items. */
10541 if (!tool_bar_item_info (f, *glyph, prop_idx))
10542 return -1;
10543
10544 /* Is mouse on the highlighted item? */
10545 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10546 && *vpos >= dpyinfo->mouse_face_beg_row
10547 && *vpos <= dpyinfo->mouse_face_end_row
10548 && (*vpos > dpyinfo->mouse_face_beg_row
10549 || *hpos >= dpyinfo->mouse_face_beg_col)
10550 && (*vpos < dpyinfo->mouse_face_end_row
10551 || *hpos < dpyinfo->mouse_face_end_col
10552 || dpyinfo->mouse_face_past_end))
10553 return 0;
10554
10555 return 1;
10556 }
10557
10558
10559 /* EXPORT:
10560 Handle mouse button event on the tool-bar of frame F, at
10561 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10562 0 for button release. MODIFIERS is event modifiers for button
10563 release. */
10564
10565 void
10566 handle_tool_bar_click (f, x, y, down_p, modifiers)
10567 struct frame *f;
10568 int x, y, down_p;
10569 unsigned int modifiers;
10570 {
10571 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10572 struct window *w = XWINDOW (f->tool_bar_window);
10573 int hpos, vpos, prop_idx;
10574 struct glyph *glyph;
10575 Lisp_Object enabled_p;
10576
10577 /* If not on the highlighted tool-bar item, return. */
10578 frame_to_window_pixel_xy (w, &x, &y);
10579 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10580 return;
10581
10582 /* If item is disabled, do nothing. */
10583 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10584 if (NILP (enabled_p))
10585 return;
10586
10587 if (down_p)
10588 {
10589 /* Show item in pressed state. */
10590 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10591 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10592 last_tool_bar_item = prop_idx;
10593 }
10594 else
10595 {
10596 Lisp_Object key, frame;
10597 struct input_event event;
10598 EVENT_INIT (event);
10599
10600 /* Show item in released state. */
10601 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10602 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10603
10604 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10605
10606 XSETFRAME (frame, f);
10607 event.kind = TOOL_BAR_EVENT;
10608 event.frame_or_window = frame;
10609 event.arg = frame;
10610 kbd_buffer_store_event (&event);
10611
10612 event.kind = TOOL_BAR_EVENT;
10613 event.frame_or_window = frame;
10614 event.arg = key;
10615 event.modifiers = modifiers;
10616 kbd_buffer_store_event (&event);
10617 last_tool_bar_item = -1;
10618 }
10619 }
10620
10621
10622 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10623 tool-bar window-relative coordinates X/Y. Called from
10624 note_mouse_highlight. */
10625
10626 static void
10627 note_tool_bar_highlight (f, x, y)
10628 struct frame *f;
10629 int x, y;
10630 {
10631 Lisp_Object window = f->tool_bar_window;
10632 struct window *w = XWINDOW (window);
10633 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10634 int hpos, vpos;
10635 struct glyph *glyph;
10636 struct glyph_row *row;
10637 int i;
10638 Lisp_Object enabled_p;
10639 int prop_idx;
10640 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10641 int mouse_down_p, rc;
10642
10643 /* Function note_mouse_highlight is called with negative x(y
10644 values when mouse moves outside of the frame. */
10645 if (x <= 0 || y <= 0)
10646 {
10647 clear_mouse_face (dpyinfo);
10648 return;
10649 }
10650
10651 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10652 if (rc < 0)
10653 {
10654 /* Not on tool-bar item. */
10655 clear_mouse_face (dpyinfo);
10656 return;
10657 }
10658 else if (rc == 0)
10659 /* On same tool-bar item as before. */
10660 goto set_help_echo;
10661
10662 clear_mouse_face (dpyinfo);
10663
10664 /* Mouse is down, but on different tool-bar item? */
10665 mouse_down_p = (dpyinfo->grabbed
10666 && f == last_mouse_frame
10667 && FRAME_LIVE_P (f));
10668 if (mouse_down_p
10669 && last_tool_bar_item != prop_idx)
10670 return;
10671
10672 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10673 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10674
10675 /* If tool-bar item is not enabled, don't highlight it. */
10676 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10677 if (!NILP (enabled_p))
10678 {
10679 /* Compute the x-position of the glyph. In front and past the
10680 image is a space. We include this in the highlighted area. */
10681 row = MATRIX_ROW (w->current_matrix, vpos);
10682 for (i = x = 0; i < hpos; ++i)
10683 x += row->glyphs[TEXT_AREA][i].pixel_width;
10684
10685 /* Record this as the current active region. */
10686 dpyinfo->mouse_face_beg_col = hpos;
10687 dpyinfo->mouse_face_beg_row = vpos;
10688 dpyinfo->mouse_face_beg_x = x;
10689 dpyinfo->mouse_face_beg_y = row->y;
10690 dpyinfo->mouse_face_past_end = 0;
10691
10692 dpyinfo->mouse_face_end_col = hpos + 1;
10693 dpyinfo->mouse_face_end_row = vpos;
10694 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10695 dpyinfo->mouse_face_end_y = row->y;
10696 dpyinfo->mouse_face_window = window;
10697 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10698
10699 /* Display it as active. */
10700 show_mouse_face (dpyinfo, draw);
10701 dpyinfo->mouse_face_image_state = draw;
10702 }
10703
10704 set_help_echo:
10705
10706 /* Set help_echo_string to a help string to display for this tool-bar item.
10707 XTread_socket does the rest. */
10708 help_echo_object = help_echo_window = Qnil;
10709 help_echo_pos = -1;
10710 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10711 if (NILP (help_echo_string))
10712 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10713 }
10714
10715 #endif /* HAVE_WINDOW_SYSTEM */
10716
10717
10718 \f
10719 /************************************************************************
10720 Horizontal scrolling
10721 ************************************************************************/
10722
10723 static int hscroll_window_tree P_ ((Lisp_Object));
10724 static int hscroll_windows P_ ((Lisp_Object));
10725
10726 /* For all leaf windows in the window tree rooted at WINDOW, set their
10727 hscroll value so that PT is (i) visible in the window, and (ii) so
10728 that it is not within a certain margin at the window's left and
10729 right border. Value is non-zero if any window's hscroll has been
10730 changed. */
10731
10732 static int
10733 hscroll_window_tree (window)
10734 Lisp_Object window;
10735 {
10736 int hscrolled_p = 0;
10737 int hscroll_relative_p = FLOATP (Vhscroll_step);
10738 int hscroll_step_abs = 0;
10739 double hscroll_step_rel = 0;
10740
10741 if (hscroll_relative_p)
10742 {
10743 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10744 if (hscroll_step_rel < 0)
10745 {
10746 hscroll_relative_p = 0;
10747 hscroll_step_abs = 0;
10748 }
10749 }
10750 else if (INTEGERP (Vhscroll_step))
10751 {
10752 hscroll_step_abs = XINT (Vhscroll_step);
10753 if (hscroll_step_abs < 0)
10754 hscroll_step_abs = 0;
10755 }
10756 else
10757 hscroll_step_abs = 0;
10758
10759 while (WINDOWP (window))
10760 {
10761 struct window *w = XWINDOW (window);
10762
10763 if (WINDOWP (w->hchild))
10764 hscrolled_p |= hscroll_window_tree (w->hchild);
10765 else if (WINDOWP (w->vchild))
10766 hscrolled_p |= hscroll_window_tree (w->vchild);
10767 else if (w->cursor.vpos >= 0)
10768 {
10769 int h_margin;
10770 int text_area_width;
10771 struct glyph_row *current_cursor_row
10772 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10773 struct glyph_row *desired_cursor_row
10774 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10775 struct glyph_row *cursor_row
10776 = (desired_cursor_row->enabled_p
10777 ? desired_cursor_row
10778 : current_cursor_row);
10779
10780 text_area_width = window_box_width (w, TEXT_AREA);
10781
10782 /* Scroll when cursor is inside this scroll margin. */
10783 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10784
10785 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10786 && ((XFASTINT (w->hscroll)
10787 && w->cursor.x <= h_margin)
10788 || (cursor_row->enabled_p
10789 && cursor_row->truncated_on_right_p
10790 && (w->cursor.x >= text_area_width - h_margin))))
10791 {
10792 struct it it;
10793 int hscroll;
10794 struct buffer *saved_current_buffer;
10795 int pt;
10796 int wanted_x;
10797
10798 /* Find point in a display of infinite width. */
10799 saved_current_buffer = current_buffer;
10800 current_buffer = XBUFFER (w->buffer);
10801
10802 if (w == XWINDOW (selected_window))
10803 pt = BUF_PT (current_buffer);
10804 else
10805 {
10806 pt = marker_position (w->pointm);
10807 pt = max (BEGV, pt);
10808 pt = min (ZV, pt);
10809 }
10810
10811 /* Move iterator to pt starting at cursor_row->start in
10812 a line with infinite width. */
10813 init_to_row_start (&it, w, cursor_row);
10814 it.last_visible_x = INFINITY;
10815 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10816 current_buffer = saved_current_buffer;
10817
10818 /* Position cursor in window. */
10819 if (!hscroll_relative_p && hscroll_step_abs == 0)
10820 hscroll = max (0, (it.current_x
10821 - (ITERATOR_AT_END_OF_LINE_P (&it)
10822 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10823 : (text_area_width / 2))))
10824 / FRAME_COLUMN_WIDTH (it.f);
10825 else if (w->cursor.x >= text_area_width - h_margin)
10826 {
10827 if (hscroll_relative_p)
10828 wanted_x = text_area_width * (1 - hscroll_step_rel)
10829 - h_margin;
10830 else
10831 wanted_x = text_area_width
10832 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10833 - h_margin;
10834 hscroll
10835 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10836 }
10837 else
10838 {
10839 if (hscroll_relative_p)
10840 wanted_x = text_area_width * hscroll_step_rel
10841 + h_margin;
10842 else
10843 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10844 + h_margin;
10845 hscroll
10846 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10847 }
10848 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10849
10850 /* Don't call Fset_window_hscroll if value hasn't
10851 changed because it will prevent redisplay
10852 optimizations. */
10853 if (XFASTINT (w->hscroll) != hscroll)
10854 {
10855 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10856 w->hscroll = make_number (hscroll);
10857 hscrolled_p = 1;
10858 }
10859 }
10860 }
10861
10862 window = w->next;
10863 }
10864
10865 /* Value is non-zero if hscroll of any leaf window has been changed. */
10866 return hscrolled_p;
10867 }
10868
10869
10870 /* Set hscroll so that cursor is visible and not inside horizontal
10871 scroll margins for all windows in the tree rooted at WINDOW. See
10872 also hscroll_window_tree above. Value is non-zero if any window's
10873 hscroll has been changed. If it has, desired matrices on the frame
10874 of WINDOW are cleared. */
10875
10876 static int
10877 hscroll_windows (window)
10878 Lisp_Object window;
10879 {
10880 int hscrolled_p = hscroll_window_tree (window);
10881 if (hscrolled_p)
10882 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10883 return hscrolled_p;
10884 }
10885
10886
10887 \f
10888 /************************************************************************
10889 Redisplay
10890 ************************************************************************/
10891
10892 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10893 to a non-zero value. This is sometimes handy to have in a debugger
10894 session. */
10895
10896 #if GLYPH_DEBUG
10897
10898 /* First and last unchanged row for try_window_id. */
10899
10900 int debug_first_unchanged_at_end_vpos;
10901 int debug_last_unchanged_at_beg_vpos;
10902
10903 /* Delta vpos and y. */
10904
10905 int debug_dvpos, debug_dy;
10906
10907 /* Delta in characters and bytes for try_window_id. */
10908
10909 int debug_delta, debug_delta_bytes;
10910
10911 /* Values of window_end_pos and window_end_vpos at the end of
10912 try_window_id. */
10913
10914 EMACS_INT debug_end_pos, debug_end_vpos;
10915
10916 /* Append a string to W->desired_matrix->method. FMT is a printf
10917 format string. A1...A9 are a supplement for a variable-length
10918 argument list. If trace_redisplay_p is non-zero also printf the
10919 resulting string to stderr. */
10920
10921 static void
10922 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10923 struct window *w;
10924 char *fmt;
10925 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10926 {
10927 char buffer[512];
10928 char *method = w->desired_matrix->method;
10929 int len = strlen (method);
10930 int size = sizeof w->desired_matrix->method;
10931 int remaining = size - len - 1;
10932
10933 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10934 if (len && remaining)
10935 {
10936 method[len] = '|';
10937 --remaining, ++len;
10938 }
10939
10940 strncpy (method + len, buffer, remaining);
10941
10942 if (trace_redisplay_p)
10943 fprintf (stderr, "%p (%s): %s\n",
10944 w,
10945 ((BUFFERP (w->buffer)
10946 && STRINGP (XBUFFER (w->buffer)->name))
10947 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10948 : "no buffer"),
10949 buffer);
10950 }
10951
10952 #endif /* GLYPH_DEBUG */
10953
10954
10955 /* Value is non-zero if all changes in window W, which displays
10956 current_buffer, are in the text between START and END. START is a
10957 buffer position, END is given as a distance from Z. Used in
10958 redisplay_internal for display optimization. */
10959
10960 static INLINE int
10961 text_outside_line_unchanged_p (w, start, end)
10962 struct window *w;
10963 int start, end;
10964 {
10965 int unchanged_p = 1;
10966
10967 /* If text or overlays have changed, see where. */
10968 if (XFASTINT (w->last_modified) < MODIFF
10969 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10970 {
10971 /* Gap in the line? */
10972 if (GPT < start || Z - GPT < end)
10973 unchanged_p = 0;
10974
10975 /* Changes start in front of the line, or end after it? */
10976 if (unchanged_p
10977 && (BEG_UNCHANGED < start - 1
10978 || END_UNCHANGED < end))
10979 unchanged_p = 0;
10980
10981 /* If selective display, can't optimize if changes start at the
10982 beginning of the line. */
10983 if (unchanged_p
10984 && INTEGERP (current_buffer->selective_display)
10985 && XINT (current_buffer->selective_display) > 0
10986 && (BEG_UNCHANGED < start || GPT <= start))
10987 unchanged_p = 0;
10988
10989 /* If there are overlays at the start or end of the line, these
10990 may have overlay strings with newlines in them. A change at
10991 START, for instance, may actually concern the display of such
10992 overlay strings as well, and they are displayed on different
10993 lines. So, quickly rule out this case. (For the future, it
10994 might be desirable to implement something more telling than
10995 just BEG/END_UNCHANGED.) */
10996 if (unchanged_p)
10997 {
10998 if (BEG + BEG_UNCHANGED == start
10999 && overlay_touches_p (start))
11000 unchanged_p = 0;
11001 if (END_UNCHANGED == end
11002 && overlay_touches_p (Z - end))
11003 unchanged_p = 0;
11004 }
11005 }
11006
11007 return unchanged_p;
11008 }
11009
11010
11011 /* Do a frame update, taking possible shortcuts into account. This is
11012 the main external entry point for redisplay.
11013
11014 If the last redisplay displayed an echo area message and that message
11015 is no longer requested, we clear the echo area or bring back the
11016 mini-buffer if that is in use. */
11017
11018 void
11019 redisplay ()
11020 {
11021 redisplay_internal (0);
11022 }
11023
11024
11025 static Lisp_Object
11026 overlay_arrow_string_or_property (var)
11027 Lisp_Object var;
11028 {
11029 Lisp_Object val;
11030
11031 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11032 return val;
11033
11034 return Voverlay_arrow_string;
11035 }
11036
11037 /* Return 1 if there are any overlay-arrows in current_buffer. */
11038 static int
11039 overlay_arrow_in_current_buffer_p ()
11040 {
11041 Lisp_Object vlist;
11042
11043 for (vlist = Voverlay_arrow_variable_list;
11044 CONSP (vlist);
11045 vlist = XCDR (vlist))
11046 {
11047 Lisp_Object var = XCAR (vlist);
11048 Lisp_Object val;
11049
11050 if (!SYMBOLP (var))
11051 continue;
11052 val = find_symbol_value (var);
11053 if (MARKERP (val)
11054 && current_buffer == XMARKER (val)->buffer)
11055 return 1;
11056 }
11057 return 0;
11058 }
11059
11060
11061 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11062 has changed. */
11063
11064 static int
11065 overlay_arrows_changed_p ()
11066 {
11067 Lisp_Object vlist;
11068
11069 for (vlist = Voverlay_arrow_variable_list;
11070 CONSP (vlist);
11071 vlist = XCDR (vlist))
11072 {
11073 Lisp_Object var = XCAR (vlist);
11074 Lisp_Object val, pstr;
11075
11076 if (!SYMBOLP (var))
11077 continue;
11078 val = find_symbol_value (var);
11079 if (!MARKERP (val))
11080 continue;
11081 if (! EQ (COERCE_MARKER (val),
11082 Fget (var, Qlast_arrow_position))
11083 || ! (pstr = overlay_arrow_string_or_property (var),
11084 EQ (pstr, Fget (var, Qlast_arrow_string))))
11085 return 1;
11086 }
11087 return 0;
11088 }
11089
11090 /* Mark overlay arrows to be updated on next redisplay. */
11091
11092 static void
11093 update_overlay_arrows (up_to_date)
11094 int up_to_date;
11095 {
11096 Lisp_Object vlist;
11097
11098 for (vlist = Voverlay_arrow_variable_list;
11099 CONSP (vlist);
11100 vlist = XCDR (vlist))
11101 {
11102 Lisp_Object var = XCAR (vlist);
11103
11104 if (!SYMBOLP (var))
11105 continue;
11106
11107 if (up_to_date > 0)
11108 {
11109 Lisp_Object val = find_symbol_value (var);
11110 Fput (var, Qlast_arrow_position,
11111 COERCE_MARKER (val));
11112 Fput (var, Qlast_arrow_string,
11113 overlay_arrow_string_or_property (var));
11114 }
11115 else if (up_to_date < 0
11116 || !NILP (Fget (var, Qlast_arrow_position)))
11117 {
11118 Fput (var, Qlast_arrow_position, Qt);
11119 Fput (var, Qlast_arrow_string, Qt);
11120 }
11121 }
11122 }
11123
11124
11125 /* Return overlay arrow string to display at row.
11126 Return integer (bitmap number) for arrow bitmap in left fringe.
11127 Return nil if no overlay arrow. */
11128
11129 static Lisp_Object
11130 overlay_arrow_at_row (it, row)
11131 struct it *it;
11132 struct glyph_row *row;
11133 {
11134 Lisp_Object vlist;
11135
11136 for (vlist = Voverlay_arrow_variable_list;
11137 CONSP (vlist);
11138 vlist = XCDR (vlist))
11139 {
11140 Lisp_Object var = XCAR (vlist);
11141 Lisp_Object val;
11142
11143 if (!SYMBOLP (var))
11144 continue;
11145
11146 val = find_symbol_value (var);
11147
11148 if (MARKERP (val)
11149 && current_buffer == XMARKER (val)->buffer
11150 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11151 {
11152 if (FRAME_WINDOW_P (it->f)
11153 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11154 {
11155 #ifdef HAVE_WINDOW_SYSTEM
11156 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11157 {
11158 int fringe_bitmap;
11159 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11160 return make_number (fringe_bitmap);
11161 }
11162 #endif
11163 return make_number (-1); /* Use default arrow bitmap */
11164 }
11165 return overlay_arrow_string_or_property (var);
11166 }
11167 }
11168
11169 return Qnil;
11170 }
11171
11172 /* Return 1 if point moved out of or into a composition. Otherwise
11173 return 0. PREV_BUF and PREV_PT are the last point buffer and
11174 position. BUF and PT are the current point buffer and position. */
11175
11176 int
11177 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11178 struct buffer *prev_buf, *buf;
11179 int prev_pt, pt;
11180 {
11181 EMACS_INT start, end;
11182 Lisp_Object prop;
11183 Lisp_Object buffer;
11184
11185 XSETBUFFER (buffer, buf);
11186 /* Check a composition at the last point if point moved within the
11187 same buffer. */
11188 if (prev_buf == buf)
11189 {
11190 if (prev_pt == pt)
11191 /* Point didn't move. */
11192 return 0;
11193
11194 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11195 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11196 && COMPOSITION_VALID_P (start, end, prop)
11197 && start < prev_pt && end > prev_pt)
11198 /* The last point was within the composition. Return 1 iff
11199 point moved out of the composition. */
11200 return (pt <= start || pt >= end);
11201 }
11202
11203 /* Check a composition at the current point. */
11204 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11205 && find_composition (pt, -1, &start, &end, &prop, buffer)
11206 && COMPOSITION_VALID_P (start, end, prop)
11207 && start < pt && end > pt);
11208 }
11209
11210
11211 /* Reconsider the setting of B->clip_changed which is displayed
11212 in window W. */
11213
11214 static INLINE void
11215 reconsider_clip_changes (w, b)
11216 struct window *w;
11217 struct buffer *b;
11218 {
11219 if (b->clip_changed
11220 && !NILP (w->window_end_valid)
11221 && w->current_matrix->buffer == b
11222 && w->current_matrix->zv == BUF_ZV (b)
11223 && w->current_matrix->begv == BUF_BEGV (b))
11224 b->clip_changed = 0;
11225
11226 /* If display wasn't paused, and W is not a tool bar window, see if
11227 point has been moved into or out of a composition. In that case,
11228 we set b->clip_changed to 1 to force updating the screen. If
11229 b->clip_changed has already been set to 1, we can skip this
11230 check. */
11231 if (!b->clip_changed
11232 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11233 {
11234 int pt;
11235
11236 if (w == XWINDOW (selected_window))
11237 pt = BUF_PT (current_buffer);
11238 else
11239 pt = marker_position (w->pointm);
11240
11241 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11242 || pt != XINT (w->last_point))
11243 && check_point_in_composition (w->current_matrix->buffer,
11244 XINT (w->last_point),
11245 XBUFFER (w->buffer), pt))
11246 b->clip_changed = 1;
11247 }
11248 }
11249 \f
11250
11251 /* Select FRAME to forward the values of frame-local variables into C
11252 variables so that the redisplay routines can access those values
11253 directly. */
11254
11255 static void
11256 select_frame_for_redisplay (frame)
11257 Lisp_Object frame;
11258 {
11259 Lisp_Object tail, symbol, val;
11260 Lisp_Object old = selected_frame;
11261 struct Lisp_Symbol *sym;
11262
11263 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11264
11265 selected_frame = frame;
11266
11267 do
11268 {
11269 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11270 if (CONSP (XCAR (tail))
11271 && (symbol = XCAR (XCAR (tail)),
11272 SYMBOLP (symbol))
11273 && (sym = indirect_variable (XSYMBOL (symbol)),
11274 val = sym->value,
11275 (BUFFER_LOCAL_VALUEP (val)))
11276 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11277 /* Use find_symbol_value rather than Fsymbol_value
11278 to avoid an error if it is void. */
11279 find_symbol_value (symbol);
11280 } while (!EQ (frame, old) && (frame = old, 1));
11281 }
11282
11283
11284 #define STOP_POLLING \
11285 do { if (! polling_stopped_here) stop_polling (); \
11286 polling_stopped_here = 1; } while (0)
11287
11288 #define RESUME_POLLING \
11289 do { if (polling_stopped_here) start_polling (); \
11290 polling_stopped_here = 0; } while (0)
11291
11292
11293 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11294 response to any user action; therefore, we should preserve the echo
11295 area. (Actually, our caller does that job.) Perhaps in the future
11296 avoid recentering windows if it is not necessary; currently that
11297 causes some problems. */
11298
11299 static void
11300 redisplay_internal (preserve_echo_area)
11301 int preserve_echo_area;
11302 {
11303 struct window *w = XWINDOW (selected_window);
11304 struct frame *f;
11305 int pause;
11306 int must_finish = 0;
11307 struct text_pos tlbufpos, tlendpos;
11308 int number_of_visible_frames;
11309 int count, count1;
11310 struct frame *sf;
11311 int polling_stopped_here = 0;
11312 Lisp_Object old_frame = selected_frame;
11313
11314 /* Non-zero means redisplay has to consider all windows on all
11315 frames. Zero means, only selected_window is considered. */
11316 int consider_all_windows_p;
11317
11318 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11319
11320 /* No redisplay if running in batch mode or frame is not yet fully
11321 initialized, or redisplay is explicitly turned off by setting
11322 Vinhibit_redisplay. */
11323 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11324 || !NILP (Vinhibit_redisplay))
11325 return;
11326
11327 /* Don't examine these until after testing Vinhibit_redisplay.
11328 When Emacs is shutting down, perhaps because its connection to
11329 X has dropped, we should not look at them at all. */
11330 f = XFRAME (w->frame);
11331 sf = SELECTED_FRAME ();
11332
11333 if (!f->glyphs_initialized_p)
11334 return;
11335
11336 /* The flag redisplay_performed_directly_p is set by
11337 direct_output_for_insert when it already did the whole screen
11338 update necessary. */
11339 if (redisplay_performed_directly_p)
11340 {
11341 redisplay_performed_directly_p = 0;
11342 if (!hscroll_windows (selected_window))
11343 return;
11344 }
11345
11346 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11347 if (popup_activated ())
11348 return;
11349 #endif
11350
11351 /* I don't think this happens but let's be paranoid. */
11352 if (redisplaying_p)
11353 return;
11354
11355 /* Record a function that resets redisplaying_p to its old value
11356 when we leave this function. */
11357 count = SPECPDL_INDEX ();
11358 record_unwind_protect (unwind_redisplay,
11359 Fcons (make_number (redisplaying_p), selected_frame));
11360 ++redisplaying_p;
11361 specbind (Qinhibit_free_realized_faces, Qnil);
11362
11363 {
11364 Lisp_Object tail, frame;
11365
11366 FOR_EACH_FRAME (tail, frame)
11367 {
11368 struct frame *f = XFRAME (frame);
11369 f->already_hscrolled_p = 0;
11370 }
11371 }
11372
11373 retry:
11374 if (!EQ (old_frame, selected_frame)
11375 && FRAME_LIVE_P (XFRAME (old_frame)))
11376 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11377 selected_frame and selected_window to be temporarily out-of-sync so
11378 when we come back here via `goto retry', we need to resync because we
11379 may need to run Elisp code (via prepare_menu_bars). */
11380 select_frame_for_redisplay (old_frame);
11381
11382 pause = 0;
11383 reconsider_clip_changes (w, current_buffer);
11384 last_escape_glyph_frame = NULL;
11385 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11386
11387 /* If new fonts have been loaded that make a glyph matrix adjustment
11388 necessary, do it. */
11389 if (fonts_changed_p)
11390 {
11391 adjust_glyphs (NULL);
11392 ++windows_or_buffers_changed;
11393 fonts_changed_p = 0;
11394 }
11395
11396 /* If face_change_count is non-zero, init_iterator will free all
11397 realized faces, which includes the faces referenced from current
11398 matrices. So, we can't reuse current matrices in this case. */
11399 if (face_change_count)
11400 ++windows_or_buffers_changed;
11401
11402 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11403 && FRAME_TTY (sf)->previous_frame != sf)
11404 {
11405 /* Since frames on a single ASCII terminal share the same
11406 display area, displaying a different frame means redisplay
11407 the whole thing. */
11408 windows_or_buffers_changed++;
11409 SET_FRAME_GARBAGED (sf);
11410 #ifndef DOS_NT
11411 set_tty_color_mode (FRAME_TTY (sf), sf);
11412 #endif
11413 FRAME_TTY (sf)->previous_frame = sf;
11414 }
11415
11416 /* Set the visible flags for all frames. Do this before checking
11417 for resized or garbaged frames; they want to know if their frames
11418 are visible. See the comment in frame.h for
11419 FRAME_SAMPLE_VISIBILITY. */
11420 {
11421 Lisp_Object tail, frame;
11422
11423 number_of_visible_frames = 0;
11424
11425 FOR_EACH_FRAME (tail, frame)
11426 {
11427 struct frame *f = XFRAME (frame);
11428
11429 FRAME_SAMPLE_VISIBILITY (f);
11430 if (FRAME_VISIBLE_P (f))
11431 ++number_of_visible_frames;
11432 clear_desired_matrices (f);
11433 }
11434 }
11435
11436 /* Notice any pending interrupt request to change frame size. */
11437 do_pending_window_change (1);
11438
11439 /* Clear frames marked as garbaged. */
11440 if (frame_garbaged)
11441 clear_garbaged_frames ();
11442
11443 /* Build menubar and tool-bar items. */
11444 if (NILP (Vmemory_full))
11445 prepare_menu_bars ();
11446
11447 if (windows_or_buffers_changed)
11448 update_mode_lines++;
11449
11450 /* Detect case that we need to write or remove a star in the mode line. */
11451 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11452 {
11453 w->update_mode_line = Qt;
11454 if (buffer_shared > 1)
11455 update_mode_lines++;
11456 }
11457
11458 /* Avoid invocation of point motion hooks by `current_column' below. */
11459 count1 = SPECPDL_INDEX ();
11460 specbind (Qinhibit_point_motion_hooks, Qt);
11461
11462 /* If %c is in the mode line, update it if needed. */
11463 if (!NILP (w->column_number_displayed)
11464 /* This alternative quickly identifies a common case
11465 where no change is needed. */
11466 && !(PT == XFASTINT (w->last_point)
11467 && XFASTINT (w->last_modified) >= MODIFF
11468 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11469 && (XFASTINT (w->column_number_displayed)
11470 != (int) current_column ())) /* iftc */
11471 w->update_mode_line = Qt;
11472
11473 unbind_to (count1, Qnil);
11474
11475 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11476
11477 /* The variable buffer_shared is set in redisplay_window and
11478 indicates that we redisplay a buffer in different windows. See
11479 there. */
11480 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11481 || cursor_type_changed);
11482
11483 /* If specs for an arrow have changed, do thorough redisplay
11484 to ensure we remove any arrow that should no longer exist. */
11485 if (overlay_arrows_changed_p ())
11486 consider_all_windows_p = windows_or_buffers_changed = 1;
11487
11488 /* Normally the message* functions will have already displayed and
11489 updated the echo area, but the frame may have been trashed, or
11490 the update may have been preempted, so display the echo area
11491 again here. Checking message_cleared_p captures the case that
11492 the echo area should be cleared. */
11493 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11494 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11495 || (message_cleared_p
11496 && minibuf_level == 0
11497 /* If the mini-window is currently selected, this means the
11498 echo-area doesn't show through. */
11499 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11500 {
11501 int window_height_changed_p = echo_area_display (0);
11502 must_finish = 1;
11503
11504 /* If we don't display the current message, don't clear the
11505 message_cleared_p flag, because, if we did, we wouldn't clear
11506 the echo area in the next redisplay which doesn't preserve
11507 the echo area. */
11508 if (!display_last_displayed_message_p)
11509 message_cleared_p = 0;
11510
11511 if (fonts_changed_p)
11512 goto retry;
11513 else if (window_height_changed_p)
11514 {
11515 consider_all_windows_p = 1;
11516 ++update_mode_lines;
11517 ++windows_or_buffers_changed;
11518
11519 /* If window configuration was changed, frames may have been
11520 marked garbaged. Clear them or we will experience
11521 surprises wrt scrolling. */
11522 if (frame_garbaged)
11523 clear_garbaged_frames ();
11524 }
11525 }
11526 else if (EQ (selected_window, minibuf_window)
11527 && (current_buffer->clip_changed
11528 || XFASTINT (w->last_modified) < MODIFF
11529 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11530 && resize_mini_window (w, 0))
11531 {
11532 /* Resized active mini-window to fit the size of what it is
11533 showing if its contents might have changed. */
11534 must_finish = 1;
11535 /* FIXME: this causes all frames to be updated, which seems unnecessary
11536 since only the current frame needs to be considered. This function needs
11537 to be rewritten with two variables, consider_all_windows and
11538 consider_all_frames. */
11539 consider_all_windows_p = 1;
11540 ++windows_or_buffers_changed;
11541 ++update_mode_lines;
11542
11543 /* If window configuration was changed, frames may have been
11544 marked garbaged. Clear them or we will experience
11545 surprises wrt scrolling. */
11546 if (frame_garbaged)
11547 clear_garbaged_frames ();
11548 }
11549
11550
11551 /* If showing the region, and mark has changed, we must redisplay
11552 the whole window. The assignment to this_line_start_pos prevents
11553 the optimization directly below this if-statement. */
11554 if (((!NILP (Vtransient_mark_mode)
11555 && !NILP (XBUFFER (w->buffer)->mark_active))
11556 != !NILP (w->region_showing))
11557 || (!NILP (w->region_showing)
11558 && !EQ (w->region_showing,
11559 Fmarker_position (XBUFFER (w->buffer)->mark))))
11560 CHARPOS (this_line_start_pos) = 0;
11561
11562 /* Optimize the case that only the line containing the cursor in the
11563 selected window has changed. Variables starting with this_ are
11564 set in display_line and record information about the line
11565 containing the cursor. */
11566 tlbufpos = this_line_start_pos;
11567 tlendpos = this_line_end_pos;
11568 if (!consider_all_windows_p
11569 && CHARPOS (tlbufpos) > 0
11570 && NILP (w->update_mode_line)
11571 && !current_buffer->clip_changed
11572 && !current_buffer->prevent_redisplay_optimizations_p
11573 && FRAME_VISIBLE_P (XFRAME (w->frame))
11574 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11575 /* Make sure recorded data applies to current buffer, etc. */
11576 && this_line_buffer == current_buffer
11577 && current_buffer == XBUFFER (w->buffer)
11578 && NILP (w->force_start)
11579 && NILP (w->optional_new_start)
11580 /* Point must be on the line that we have info recorded about. */
11581 && PT >= CHARPOS (tlbufpos)
11582 && PT <= Z - CHARPOS (tlendpos)
11583 /* All text outside that line, including its final newline,
11584 must be unchanged */
11585 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11586 CHARPOS (tlendpos)))
11587 {
11588 if (CHARPOS (tlbufpos) > BEGV
11589 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11590 && (CHARPOS (tlbufpos) == ZV
11591 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11592 /* Former continuation line has disappeared by becoming empty */
11593 goto cancel;
11594 else if (XFASTINT (w->last_modified) < MODIFF
11595 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11596 || MINI_WINDOW_P (w))
11597 {
11598 /* We have to handle the case of continuation around a
11599 wide-column character (See the comment in indent.c around
11600 line 885).
11601
11602 For instance, in the following case:
11603
11604 -------- Insert --------
11605 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11606 J_I_ ==> J_I_ `^^' are cursors.
11607 ^^ ^^
11608 -------- --------
11609
11610 As we have to redraw the line above, we should goto cancel. */
11611
11612 struct it it;
11613 int line_height_before = this_line_pixel_height;
11614
11615 /* Note that start_display will handle the case that the
11616 line starting at tlbufpos is a continuation lines. */
11617 start_display (&it, w, tlbufpos);
11618
11619 /* Implementation note: It this still necessary? */
11620 if (it.current_x != this_line_start_x)
11621 goto cancel;
11622
11623 TRACE ((stderr, "trying display optimization 1\n"));
11624 w->cursor.vpos = -1;
11625 overlay_arrow_seen = 0;
11626 it.vpos = this_line_vpos;
11627 it.current_y = this_line_y;
11628 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11629 display_line (&it);
11630
11631 /* If line contains point, is not continued,
11632 and ends at same distance from eob as before, we win */
11633 if (w->cursor.vpos >= 0
11634 /* Line is not continued, otherwise this_line_start_pos
11635 would have been set to 0 in display_line. */
11636 && CHARPOS (this_line_start_pos)
11637 /* Line ends as before. */
11638 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11639 /* Line has same height as before. Otherwise other lines
11640 would have to be shifted up or down. */
11641 && this_line_pixel_height == line_height_before)
11642 {
11643 /* If this is not the window's last line, we must adjust
11644 the charstarts of the lines below. */
11645 if (it.current_y < it.last_visible_y)
11646 {
11647 struct glyph_row *row
11648 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11649 int delta, delta_bytes;
11650
11651 if (Z - CHARPOS (tlendpos) == ZV)
11652 {
11653 /* This line ends at end of (accessible part of)
11654 buffer. There is no newline to count. */
11655 delta = (Z
11656 - CHARPOS (tlendpos)
11657 - MATRIX_ROW_START_CHARPOS (row));
11658 delta_bytes = (Z_BYTE
11659 - BYTEPOS (tlendpos)
11660 - MATRIX_ROW_START_BYTEPOS (row));
11661 }
11662 else
11663 {
11664 /* This line ends in a newline. Must take
11665 account of the newline and the rest of the
11666 text that follows. */
11667 delta = (Z
11668 - CHARPOS (tlendpos)
11669 - MATRIX_ROW_START_CHARPOS (row));
11670 delta_bytes = (Z_BYTE
11671 - BYTEPOS (tlendpos)
11672 - MATRIX_ROW_START_BYTEPOS (row));
11673 }
11674
11675 increment_matrix_positions (w->current_matrix,
11676 this_line_vpos + 1,
11677 w->current_matrix->nrows,
11678 delta, delta_bytes);
11679 }
11680
11681 /* If this row displays text now but previously didn't,
11682 or vice versa, w->window_end_vpos may have to be
11683 adjusted. */
11684 if ((it.glyph_row - 1)->displays_text_p)
11685 {
11686 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11687 XSETINT (w->window_end_vpos, this_line_vpos);
11688 }
11689 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11690 && this_line_vpos > 0)
11691 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11692 w->window_end_valid = Qnil;
11693
11694 /* Update hint: No need to try to scroll in update_window. */
11695 w->desired_matrix->no_scrolling_p = 1;
11696
11697 #if GLYPH_DEBUG
11698 *w->desired_matrix->method = 0;
11699 debug_method_add (w, "optimization 1");
11700 #endif
11701 #ifdef HAVE_WINDOW_SYSTEM
11702 update_window_fringes (w, 0);
11703 #endif
11704 goto update;
11705 }
11706 else
11707 goto cancel;
11708 }
11709 else if (/* Cursor position hasn't changed. */
11710 PT == XFASTINT (w->last_point)
11711 /* Make sure the cursor was last displayed
11712 in this window. Otherwise we have to reposition it. */
11713 && 0 <= w->cursor.vpos
11714 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11715 {
11716 if (!must_finish)
11717 {
11718 do_pending_window_change (1);
11719
11720 /* We used to always goto end_of_redisplay here, but this
11721 isn't enough if we have a blinking cursor. */
11722 if (w->cursor_off_p == w->last_cursor_off_p)
11723 goto end_of_redisplay;
11724 }
11725 goto update;
11726 }
11727 /* If highlighting the region, or if the cursor is in the echo area,
11728 then we can't just move the cursor. */
11729 else if (! (!NILP (Vtransient_mark_mode)
11730 && !NILP (current_buffer->mark_active))
11731 && (EQ (selected_window, current_buffer->last_selected_window)
11732 || highlight_nonselected_windows)
11733 && NILP (w->region_showing)
11734 && NILP (Vshow_trailing_whitespace)
11735 && !cursor_in_echo_area)
11736 {
11737 struct it it;
11738 struct glyph_row *row;
11739
11740 /* Skip from tlbufpos to PT and see where it is. Note that
11741 PT may be in invisible text. If so, we will end at the
11742 next visible position. */
11743 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11744 NULL, DEFAULT_FACE_ID);
11745 it.current_x = this_line_start_x;
11746 it.current_y = this_line_y;
11747 it.vpos = this_line_vpos;
11748
11749 /* The call to move_it_to stops in front of PT, but
11750 moves over before-strings. */
11751 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11752
11753 if (it.vpos == this_line_vpos
11754 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11755 row->enabled_p))
11756 {
11757 xassert (this_line_vpos == it.vpos);
11758 xassert (this_line_y == it.current_y);
11759 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11760 #if GLYPH_DEBUG
11761 *w->desired_matrix->method = 0;
11762 debug_method_add (w, "optimization 3");
11763 #endif
11764 goto update;
11765 }
11766 else
11767 goto cancel;
11768 }
11769
11770 cancel:
11771 /* Text changed drastically or point moved off of line. */
11772 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11773 }
11774
11775 CHARPOS (this_line_start_pos) = 0;
11776 consider_all_windows_p |= buffer_shared > 1;
11777 ++clear_face_cache_count;
11778 #ifdef HAVE_WINDOW_SYSTEM
11779 ++clear_image_cache_count;
11780 #endif
11781
11782 /* Build desired matrices, and update the display. If
11783 consider_all_windows_p is non-zero, do it for all windows on all
11784 frames. Otherwise do it for selected_window, only. */
11785
11786 if (consider_all_windows_p)
11787 {
11788 Lisp_Object tail, frame;
11789
11790 FOR_EACH_FRAME (tail, frame)
11791 XFRAME (frame)->updated_p = 0;
11792
11793 /* Recompute # windows showing selected buffer. This will be
11794 incremented each time such a window is displayed. */
11795 buffer_shared = 0;
11796
11797 FOR_EACH_FRAME (tail, frame)
11798 {
11799 struct frame *f = XFRAME (frame);
11800
11801 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11802 {
11803 if (! EQ (frame, selected_frame))
11804 /* Select the frame, for the sake of frame-local
11805 variables. */
11806 select_frame_for_redisplay (frame);
11807
11808 /* Mark all the scroll bars to be removed; we'll redeem
11809 the ones we want when we redisplay their windows. */
11810 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11811 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11812
11813 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11814 redisplay_windows (FRAME_ROOT_WINDOW (f));
11815
11816 /* The X error handler may have deleted that frame. */
11817 if (!FRAME_LIVE_P (f))
11818 continue;
11819
11820 /* Any scroll bars which redisplay_windows should have
11821 nuked should now go away. */
11822 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11823 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11824
11825 /* If fonts changed, display again. */
11826 /* ??? rms: I suspect it is a mistake to jump all the way
11827 back to retry here. It should just retry this frame. */
11828 if (fonts_changed_p)
11829 goto retry;
11830
11831 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11832 {
11833 /* See if we have to hscroll. */
11834 if (!f->already_hscrolled_p)
11835 {
11836 f->already_hscrolled_p = 1;
11837 if (hscroll_windows (f->root_window))
11838 goto retry;
11839 }
11840
11841 /* Prevent various kinds of signals during display
11842 update. stdio is not robust about handling
11843 signals, which can cause an apparent I/O
11844 error. */
11845 if (interrupt_input)
11846 unrequest_sigio ();
11847 STOP_POLLING;
11848
11849 /* Update the display. */
11850 set_window_update_flags (XWINDOW (f->root_window), 1);
11851 pause |= update_frame (f, 0, 0);
11852 f->updated_p = 1;
11853 }
11854 }
11855 }
11856
11857 if (!EQ (old_frame, selected_frame)
11858 && FRAME_LIVE_P (XFRAME (old_frame)))
11859 /* We played a bit fast-and-loose above and allowed selected_frame
11860 and selected_window to be temporarily out-of-sync but let's make
11861 sure this stays contained. */
11862 select_frame_for_redisplay (old_frame);
11863 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11864
11865 if (!pause)
11866 {
11867 /* Do the mark_window_display_accurate after all windows have
11868 been redisplayed because this call resets flags in buffers
11869 which are needed for proper redisplay. */
11870 FOR_EACH_FRAME (tail, frame)
11871 {
11872 struct frame *f = XFRAME (frame);
11873 if (f->updated_p)
11874 {
11875 mark_window_display_accurate (f->root_window, 1);
11876 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11877 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11878 }
11879 }
11880 }
11881 }
11882 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11883 {
11884 Lisp_Object mini_window;
11885 struct frame *mini_frame;
11886
11887 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11888 /* Use list_of_error, not Qerror, so that
11889 we catch only errors and don't run the debugger. */
11890 internal_condition_case_1 (redisplay_window_1, selected_window,
11891 list_of_error,
11892 redisplay_window_error);
11893
11894 /* Compare desired and current matrices, perform output. */
11895
11896 update:
11897 /* If fonts changed, display again. */
11898 if (fonts_changed_p)
11899 goto retry;
11900
11901 /* Prevent various kinds of signals during display update.
11902 stdio is not robust about handling signals,
11903 which can cause an apparent I/O error. */
11904 if (interrupt_input)
11905 unrequest_sigio ();
11906 STOP_POLLING;
11907
11908 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11909 {
11910 if (hscroll_windows (selected_window))
11911 goto retry;
11912
11913 XWINDOW (selected_window)->must_be_updated_p = 1;
11914 pause = update_frame (sf, 0, 0);
11915 }
11916
11917 /* We may have called echo_area_display at the top of this
11918 function. If the echo area is on another frame, that may
11919 have put text on a frame other than the selected one, so the
11920 above call to update_frame would not have caught it. Catch
11921 it here. */
11922 mini_window = FRAME_MINIBUF_WINDOW (sf);
11923 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11924
11925 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11926 {
11927 XWINDOW (mini_window)->must_be_updated_p = 1;
11928 pause |= update_frame (mini_frame, 0, 0);
11929 if (!pause && hscroll_windows (mini_window))
11930 goto retry;
11931 }
11932 }
11933
11934 /* If display was paused because of pending input, make sure we do a
11935 thorough update the next time. */
11936 if (pause)
11937 {
11938 /* Prevent the optimization at the beginning of
11939 redisplay_internal that tries a single-line update of the
11940 line containing the cursor in the selected window. */
11941 CHARPOS (this_line_start_pos) = 0;
11942
11943 /* Let the overlay arrow be updated the next time. */
11944 update_overlay_arrows (0);
11945
11946 /* If we pause after scrolling, some rows in the current
11947 matrices of some windows are not valid. */
11948 if (!WINDOW_FULL_WIDTH_P (w)
11949 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11950 update_mode_lines = 1;
11951 }
11952 else
11953 {
11954 if (!consider_all_windows_p)
11955 {
11956 /* This has already been done above if
11957 consider_all_windows_p is set. */
11958 mark_window_display_accurate_1 (w, 1);
11959
11960 /* Say overlay arrows are up to date. */
11961 update_overlay_arrows (1);
11962
11963 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11964 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11965 }
11966
11967 update_mode_lines = 0;
11968 windows_or_buffers_changed = 0;
11969 cursor_type_changed = 0;
11970 }
11971
11972 /* Start SIGIO interrupts coming again. Having them off during the
11973 code above makes it less likely one will discard output, but not
11974 impossible, since there might be stuff in the system buffer here.
11975 But it is much hairier to try to do anything about that. */
11976 if (interrupt_input)
11977 request_sigio ();
11978 RESUME_POLLING;
11979
11980 /* If a frame has become visible which was not before, redisplay
11981 again, so that we display it. Expose events for such a frame
11982 (which it gets when becoming visible) don't call the parts of
11983 redisplay constructing glyphs, so simply exposing a frame won't
11984 display anything in this case. So, we have to display these
11985 frames here explicitly. */
11986 if (!pause)
11987 {
11988 Lisp_Object tail, frame;
11989 int new_count = 0;
11990
11991 FOR_EACH_FRAME (tail, frame)
11992 {
11993 int this_is_visible = 0;
11994
11995 if (XFRAME (frame)->visible)
11996 this_is_visible = 1;
11997 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11998 if (XFRAME (frame)->visible)
11999 this_is_visible = 1;
12000
12001 if (this_is_visible)
12002 new_count++;
12003 }
12004
12005 if (new_count != number_of_visible_frames)
12006 windows_or_buffers_changed++;
12007 }
12008
12009 /* Change frame size now if a change is pending. */
12010 do_pending_window_change (1);
12011
12012 /* If we just did a pending size change, or have additional
12013 visible frames, redisplay again. */
12014 if (windows_or_buffers_changed && !pause)
12015 goto retry;
12016
12017 /* Clear the face cache eventually. */
12018 if (consider_all_windows_p)
12019 {
12020 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12021 {
12022 clear_face_cache (0);
12023 clear_face_cache_count = 0;
12024 }
12025 #ifdef HAVE_WINDOW_SYSTEM
12026 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12027 {
12028 clear_image_caches (Qnil);
12029 clear_image_cache_count = 0;
12030 }
12031 #endif /* HAVE_WINDOW_SYSTEM */
12032 }
12033
12034 end_of_redisplay:
12035 unbind_to (count, Qnil);
12036 RESUME_POLLING;
12037 }
12038
12039
12040 /* Redisplay, but leave alone any recent echo area message unless
12041 another message has been requested in its place.
12042
12043 This is useful in situations where you need to redisplay but no
12044 user action has occurred, making it inappropriate for the message
12045 area to be cleared. See tracking_off and
12046 wait_reading_process_output for examples of these situations.
12047
12048 FROM_WHERE is an integer saying from where this function was
12049 called. This is useful for debugging. */
12050
12051 void
12052 redisplay_preserve_echo_area (from_where)
12053 int from_where;
12054 {
12055 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12056
12057 if (!NILP (echo_area_buffer[1]))
12058 {
12059 /* We have a previously displayed message, but no current
12060 message. Redisplay the previous message. */
12061 display_last_displayed_message_p = 1;
12062 redisplay_internal (1);
12063 display_last_displayed_message_p = 0;
12064 }
12065 else
12066 redisplay_internal (1);
12067
12068 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12069 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12070 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12071 }
12072
12073
12074 /* Function registered with record_unwind_protect in
12075 redisplay_internal. Reset redisplaying_p to the value it had
12076 before redisplay_internal was called, and clear
12077 prevent_freeing_realized_faces_p. It also selects the previously
12078 selected frame, unless it has been deleted (by an X connection
12079 failure during redisplay, for example). */
12080
12081 static Lisp_Object
12082 unwind_redisplay (val)
12083 Lisp_Object val;
12084 {
12085 Lisp_Object old_redisplaying_p, old_frame;
12086
12087 old_redisplaying_p = XCAR (val);
12088 redisplaying_p = XFASTINT (old_redisplaying_p);
12089 old_frame = XCDR (val);
12090 if (! EQ (old_frame, selected_frame)
12091 && FRAME_LIVE_P (XFRAME (old_frame)))
12092 select_frame_for_redisplay (old_frame);
12093 return Qnil;
12094 }
12095
12096
12097 /* Mark the display of window W as accurate or inaccurate. If
12098 ACCURATE_P is non-zero mark display of W as accurate. If
12099 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12100 redisplay_internal is called. */
12101
12102 static void
12103 mark_window_display_accurate_1 (w, accurate_p)
12104 struct window *w;
12105 int accurate_p;
12106 {
12107 if (BUFFERP (w->buffer))
12108 {
12109 struct buffer *b = XBUFFER (w->buffer);
12110
12111 w->last_modified
12112 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12113 w->last_overlay_modified
12114 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12115 w->last_had_star
12116 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12117
12118 if (accurate_p)
12119 {
12120 b->clip_changed = 0;
12121 b->prevent_redisplay_optimizations_p = 0;
12122
12123 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12124 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12125 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12126 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12127
12128 w->current_matrix->buffer = b;
12129 w->current_matrix->begv = BUF_BEGV (b);
12130 w->current_matrix->zv = BUF_ZV (b);
12131
12132 w->last_cursor = w->cursor;
12133 w->last_cursor_off_p = w->cursor_off_p;
12134
12135 if (w == XWINDOW (selected_window))
12136 w->last_point = make_number (BUF_PT (b));
12137 else
12138 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12139 }
12140 }
12141
12142 if (accurate_p)
12143 {
12144 w->window_end_valid = w->buffer;
12145 w->update_mode_line = Qnil;
12146 }
12147 }
12148
12149
12150 /* Mark the display of windows in the window tree rooted at WINDOW as
12151 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12152 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12153 be redisplayed the next time redisplay_internal is called. */
12154
12155 void
12156 mark_window_display_accurate (window, accurate_p)
12157 Lisp_Object window;
12158 int accurate_p;
12159 {
12160 struct window *w;
12161
12162 for (; !NILP (window); window = w->next)
12163 {
12164 w = XWINDOW (window);
12165 mark_window_display_accurate_1 (w, accurate_p);
12166
12167 if (!NILP (w->vchild))
12168 mark_window_display_accurate (w->vchild, accurate_p);
12169 if (!NILP (w->hchild))
12170 mark_window_display_accurate (w->hchild, accurate_p);
12171 }
12172
12173 if (accurate_p)
12174 {
12175 update_overlay_arrows (1);
12176 }
12177 else
12178 {
12179 /* Force a thorough redisplay the next time by setting
12180 last_arrow_position and last_arrow_string to t, which is
12181 unequal to any useful value of Voverlay_arrow_... */
12182 update_overlay_arrows (-1);
12183 }
12184 }
12185
12186
12187 /* Return value in display table DP (Lisp_Char_Table *) for character
12188 C. Since a display table doesn't have any parent, we don't have to
12189 follow parent. Do not call this function directly but use the
12190 macro DISP_CHAR_VECTOR. */
12191
12192 Lisp_Object
12193 disp_char_vector (dp, c)
12194 struct Lisp_Char_Table *dp;
12195 int c;
12196 {
12197 Lisp_Object val;
12198
12199 if (ASCII_CHAR_P (c))
12200 {
12201 val = dp->ascii;
12202 if (SUB_CHAR_TABLE_P (val))
12203 val = XSUB_CHAR_TABLE (val)->contents[c];
12204 }
12205 else
12206 {
12207 Lisp_Object table;
12208
12209 XSETCHAR_TABLE (table, dp);
12210 val = char_table_ref (table, c);
12211 }
12212 if (NILP (val))
12213 val = dp->defalt;
12214 return val;
12215 }
12216
12217
12218 \f
12219 /***********************************************************************
12220 Window Redisplay
12221 ***********************************************************************/
12222
12223 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12224
12225 static void
12226 redisplay_windows (window)
12227 Lisp_Object window;
12228 {
12229 while (!NILP (window))
12230 {
12231 struct window *w = XWINDOW (window);
12232
12233 if (!NILP (w->hchild))
12234 redisplay_windows (w->hchild);
12235 else if (!NILP (w->vchild))
12236 redisplay_windows (w->vchild);
12237 else if (!NILP (w->buffer))
12238 {
12239 displayed_buffer = XBUFFER (w->buffer);
12240 /* Use list_of_error, not Qerror, so that
12241 we catch only errors and don't run the debugger. */
12242 internal_condition_case_1 (redisplay_window_0, window,
12243 list_of_error,
12244 redisplay_window_error);
12245 }
12246
12247 window = w->next;
12248 }
12249 }
12250
12251 static Lisp_Object
12252 redisplay_window_error ()
12253 {
12254 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12255 return Qnil;
12256 }
12257
12258 static Lisp_Object
12259 redisplay_window_0 (window)
12260 Lisp_Object window;
12261 {
12262 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12263 redisplay_window (window, 0);
12264 return Qnil;
12265 }
12266
12267 static Lisp_Object
12268 redisplay_window_1 (window)
12269 Lisp_Object window;
12270 {
12271 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12272 redisplay_window (window, 1);
12273 return Qnil;
12274 }
12275 \f
12276
12277 /* Increment GLYPH until it reaches END or CONDITION fails while
12278 adding (GLYPH)->pixel_width to X. */
12279
12280 #define SKIP_GLYPHS(glyph, end, x, condition) \
12281 do \
12282 { \
12283 (x) += (glyph)->pixel_width; \
12284 ++(glyph); \
12285 } \
12286 while ((glyph) < (end) && (condition))
12287
12288
12289 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12290 DELTA is the number of bytes by which positions recorded in ROW
12291 differ from current buffer positions.
12292
12293 Return 0 if cursor is not on this row. 1 otherwise. */
12294
12295 int
12296 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12297 struct window *w;
12298 struct glyph_row *row;
12299 struct glyph_matrix *matrix;
12300 int delta, delta_bytes, dy, dvpos;
12301 {
12302 struct glyph *glyph = row->glyphs[TEXT_AREA];
12303 struct glyph *end = glyph + row->used[TEXT_AREA];
12304 struct glyph *cursor = NULL;
12305 /* The first glyph that starts a sequence of glyphs from string. */
12306 struct glyph *string_start;
12307 /* The X coordinate of string_start. */
12308 int string_start_x;
12309 /* The last known character position. */
12310 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12311 /* The last known character position before string_start. */
12312 int string_before_pos;
12313 int x = row->x;
12314 int cursor_x = x;
12315 int cursor_from_overlay_pos = 0;
12316 int pt_old = PT - delta;
12317
12318 /* Skip over glyphs not having an object at the start of the row.
12319 These are special glyphs like truncation marks on terminal
12320 frames. */
12321 if (row->displays_text_p)
12322 while (glyph < end
12323 && INTEGERP (glyph->object)
12324 && glyph->charpos < 0)
12325 {
12326 x += glyph->pixel_width;
12327 ++glyph;
12328 }
12329
12330 string_start = NULL;
12331 while (glyph < end
12332 && !INTEGERP (glyph->object)
12333 && (!BUFFERP (glyph->object)
12334 || (last_pos = glyph->charpos) < pt_old
12335 || glyph->avoid_cursor_p))
12336 {
12337 if (! STRINGP (glyph->object))
12338 {
12339 string_start = NULL;
12340 x += glyph->pixel_width;
12341 ++glyph;
12342 if (cursor_from_overlay_pos
12343 && last_pos >= cursor_from_overlay_pos)
12344 {
12345 cursor_from_overlay_pos = 0;
12346 cursor = 0;
12347 }
12348 }
12349 else
12350 {
12351 if (string_start == NULL)
12352 {
12353 string_before_pos = last_pos;
12354 string_start = glyph;
12355 string_start_x = x;
12356 }
12357 /* Skip all glyphs from string. */
12358 do
12359 {
12360 Lisp_Object cprop;
12361 int pos;
12362 if ((cursor == NULL || glyph > cursor)
12363 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12364 Qcursor, (glyph)->object),
12365 !NILP (cprop))
12366 && (pos = string_buffer_position (w, glyph->object,
12367 string_before_pos),
12368 (pos == 0 /* From overlay */
12369 || pos == pt_old)))
12370 {
12371 /* Estimate overlay buffer position from the buffer
12372 positions of the glyphs before and after the overlay.
12373 Add 1 to last_pos so that if point corresponds to the
12374 glyph right after the overlay, we still use a 'cursor'
12375 property found in that overlay. */
12376 cursor_from_overlay_pos = (pos ? 0 : last_pos
12377 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12378 cursor = glyph;
12379 cursor_x = x;
12380 }
12381 x += glyph->pixel_width;
12382 ++glyph;
12383 }
12384 while (glyph < end && EQ (glyph->object, string_start->object));
12385 }
12386 }
12387
12388 if (cursor != NULL)
12389 {
12390 glyph = cursor;
12391 x = cursor_x;
12392 }
12393 else if (row->ends_in_ellipsis_p && glyph == end)
12394 {
12395 /* Scan back over the ellipsis glyphs, decrementing positions. */
12396 while (glyph > row->glyphs[TEXT_AREA]
12397 && (glyph - 1)->charpos == last_pos)
12398 glyph--, x -= glyph->pixel_width;
12399 /* That loop always goes one position too far,
12400 including the glyph before the ellipsis.
12401 So scan forward over that one. */
12402 x += glyph->pixel_width;
12403 glyph++;
12404 }
12405 else if (string_start
12406 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12407 {
12408 /* We may have skipped over point because the previous glyphs
12409 are from string. As there's no easy way to know the
12410 character position of the current glyph, find the correct
12411 glyph on point by scanning from string_start again. */
12412 Lisp_Object limit;
12413 Lisp_Object string;
12414 struct glyph *stop = glyph;
12415 int pos;
12416
12417 limit = make_number (pt_old + 1);
12418 glyph = string_start;
12419 x = string_start_x;
12420 string = glyph->object;
12421 pos = string_buffer_position (w, string, string_before_pos);
12422 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12423 because we always put cursor after overlay strings. */
12424 while (pos == 0 && glyph < stop)
12425 {
12426 string = glyph->object;
12427 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12428 if (glyph < stop)
12429 pos = string_buffer_position (w, glyph->object, string_before_pos);
12430 }
12431
12432 while (glyph < stop)
12433 {
12434 pos = XINT (Fnext_single_char_property_change
12435 (make_number (pos), Qdisplay, Qnil, limit));
12436 if (pos > pt_old)
12437 break;
12438 /* Skip glyphs from the same string. */
12439 string = glyph->object;
12440 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12441 /* Skip glyphs from an overlay. */
12442 while (glyph < stop
12443 && ! string_buffer_position (w, glyph->object, pos))
12444 {
12445 string = glyph->object;
12446 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12447 }
12448 }
12449
12450 /* If we reached the end of the line, and end was from a string,
12451 cursor is not on this line. */
12452 if (glyph == end && row->continued_p)
12453 return 0;
12454 }
12455
12456 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12457 w->cursor.x = x;
12458 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12459 w->cursor.y = row->y + dy;
12460
12461 if (w == XWINDOW (selected_window))
12462 {
12463 if (!row->continued_p
12464 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12465 && row->x == 0)
12466 {
12467 this_line_buffer = XBUFFER (w->buffer);
12468
12469 CHARPOS (this_line_start_pos)
12470 = MATRIX_ROW_START_CHARPOS (row) + delta;
12471 BYTEPOS (this_line_start_pos)
12472 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12473
12474 CHARPOS (this_line_end_pos)
12475 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12476 BYTEPOS (this_line_end_pos)
12477 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12478
12479 this_line_y = w->cursor.y;
12480 this_line_pixel_height = row->height;
12481 this_line_vpos = w->cursor.vpos;
12482 this_line_start_x = row->x;
12483 }
12484 else
12485 CHARPOS (this_line_start_pos) = 0;
12486 }
12487
12488 return 1;
12489 }
12490
12491
12492 /* Run window scroll functions, if any, for WINDOW with new window
12493 start STARTP. Sets the window start of WINDOW to that position.
12494
12495 We assume that the window's buffer is really current. */
12496
12497 static INLINE struct text_pos
12498 run_window_scroll_functions (window, startp)
12499 Lisp_Object window;
12500 struct text_pos startp;
12501 {
12502 struct window *w = XWINDOW (window);
12503 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12504
12505 if (current_buffer != XBUFFER (w->buffer))
12506 abort ();
12507
12508 if (!NILP (Vwindow_scroll_functions))
12509 {
12510 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12511 make_number (CHARPOS (startp)));
12512 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12513 /* In case the hook functions switch buffers. */
12514 if (current_buffer != XBUFFER (w->buffer))
12515 set_buffer_internal_1 (XBUFFER (w->buffer));
12516 }
12517
12518 return startp;
12519 }
12520
12521
12522 /* Make sure the line containing the cursor is fully visible.
12523 A value of 1 means there is nothing to be done.
12524 (Either the line is fully visible, or it cannot be made so,
12525 or we cannot tell.)
12526
12527 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12528 is higher than window.
12529
12530 A value of 0 means the caller should do scrolling
12531 as if point had gone off the screen. */
12532
12533 static int
12534 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12535 struct window *w;
12536 int force_p;
12537 int current_matrix_p;
12538 {
12539 struct glyph_matrix *matrix;
12540 struct glyph_row *row;
12541 int window_height;
12542
12543 if (!make_cursor_line_fully_visible_p)
12544 return 1;
12545
12546 /* It's not always possible to find the cursor, e.g, when a window
12547 is full of overlay strings. Don't do anything in that case. */
12548 if (w->cursor.vpos < 0)
12549 return 1;
12550
12551 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12552 row = MATRIX_ROW (matrix, w->cursor.vpos);
12553
12554 /* If the cursor row is not partially visible, there's nothing to do. */
12555 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12556 return 1;
12557
12558 /* If the row the cursor is in is taller than the window's height,
12559 it's not clear what to do, so do nothing. */
12560 window_height = window_box_height (w);
12561 if (row->height >= window_height)
12562 {
12563 if (!force_p || MINI_WINDOW_P (w)
12564 || w->vscroll || w->cursor.vpos == 0)
12565 return 1;
12566 }
12567 return 0;
12568 }
12569
12570
12571 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12572 non-zero means only WINDOW is redisplayed in redisplay_internal.
12573 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12574 in redisplay_window to bring a partially visible line into view in
12575 the case that only the cursor has moved.
12576
12577 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12578 last screen line's vertical height extends past the end of the screen.
12579
12580 Value is
12581
12582 1 if scrolling succeeded
12583
12584 0 if scrolling didn't find point.
12585
12586 -1 if new fonts have been loaded so that we must interrupt
12587 redisplay, adjust glyph matrices, and try again. */
12588
12589 enum
12590 {
12591 SCROLLING_SUCCESS,
12592 SCROLLING_FAILED,
12593 SCROLLING_NEED_LARGER_MATRICES
12594 };
12595
12596 static int
12597 try_scrolling (window, just_this_one_p, scroll_conservatively,
12598 scroll_step, temp_scroll_step, last_line_misfit)
12599 Lisp_Object window;
12600 int just_this_one_p;
12601 EMACS_INT scroll_conservatively, scroll_step;
12602 int temp_scroll_step;
12603 int last_line_misfit;
12604 {
12605 struct window *w = XWINDOW (window);
12606 struct frame *f = XFRAME (w->frame);
12607 struct text_pos pos, startp;
12608 struct it it;
12609 int this_scroll_margin, scroll_max, rc, height;
12610 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12611 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12612 Lisp_Object aggressive;
12613 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12614
12615 #if GLYPH_DEBUG
12616 debug_method_add (w, "try_scrolling");
12617 #endif
12618
12619 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12620
12621 /* Compute scroll margin height in pixels. We scroll when point is
12622 within this distance from the top or bottom of the window. */
12623 if (scroll_margin > 0)
12624 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12625 * FRAME_LINE_HEIGHT (f);
12626 else
12627 this_scroll_margin = 0;
12628
12629 /* Force scroll_conservatively to have a reasonable value, to avoid
12630 overflow while computing how much to scroll. Note that the user
12631 can supply scroll-conservatively equal to `most-positive-fixnum',
12632 which can be larger than INT_MAX. */
12633 if (scroll_conservatively > scroll_limit)
12634 {
12635 scroll_conservatively = scroll_limit;
12636 scroll_max = INT_MAX;
12637 }
12638 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12639 /* Compute how much we should try to scroll maximally to bring
12640 point into view. */
12641 scroll_max = (max (scroll_step,
12642 max (scroll_conservatively, temp_scroll_step))
12643 * FRAME_LINE_HEIGHT (f));
12644 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12645 || NUMBERP (current_buffer->scroll_up_aggressively))
12646 /* We're trying to scroll because of aggressive scrolling but no
12647 scroll_step is set. Choose an arbitrary one. */
12648 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12649 else
12650 scroll_max = 0;
12651
12652 too_near_end:
12653
12654 /* Decide whether to scroll down. */
12655 if (PT > CHARPOS (startp))
12656 {
12657 int scroll_margin_y;
12658
12659 /* Compute the pixel ypos of the scroll margin, then move it to
12660 either that ypos or PT, whichever comes first. */
12661 start_display (&it, w, startp);
12662 scroll_margin_y = it.last_visible_y - this_scroll_margin
12663 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12664 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12665 (MOVE_TO_POS | MOVE_TO_Y));
12666
12667 if (PT > CHARPOS (it.current.pos))
12668 {
12669 int y0 = line_bottom_y (&it);
12670
12671 /* Compute the distance from the scroll margin to PT
12672 (including the height of the cursor line). Moving the
12673 iterator unconditionally to PT can be slow if PT is far
12674 away, so stop 10 lines past the window bottom (is there a
12675 way to do the right thing quickly?). */
12676 move_it_to (&it, PT, -1,
12677 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12678 -1, MOVE_TO_POS | MOVE_TO_Y);
12679 dy = line_bottom_y (&it) - y0;
12680
12681 if (dy > scroll_max)
12682 return SCROLLING_FAILED;
12683
12684 scroll_down_p = 1;
12685 }
12686 }
12687
12688 if (scroll_down_p)
12689 {
12690 /* Point is in or below the bottom scroll margin, so move the
12691 window start down. If scrolling conservatively, move it just
12692 enough down to make point visible. If scroll_step is set,
12693 move it down by scroll_step. */
12694 if (scroll_conservatively)
12695 amount_to_scroll
12696 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12697 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12698 else if (scroll_step || temp_scroll_step)
12699 amount_to_scroll = scroll_max;
12700 else
12701 {
12702 aggressive = current_buffer->scroll_up_aggressively;
12703 height = WINDOW_BOX_TEXT_HEIGHT (w);
12704 if (NUMBERP (aggressive))
12705 {
12706 double float_amount = XFLOATINT (aggressive) * height;
12707 amount_to_scroll = float_amount;
12708 if (amount_to_scroll == 0 && float_amount > 0)
12709 amount_to_scroll = 1;
12710 }
12711 }
12712
12713 if (amount_to_scroll <= 0)
12714 return SCROLLING_FAILED;
12715
12716 start_display (&it, w, startp);
12717 move_it_vertically (&it, amount_to_scroll);
12718
12719 /* If STARTP is unchanged, move it down another screen line. */
12720 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12721 move_it_by_lines (&it, 1, 1);
12722 startp = it.current.pos;
12723 }
12724 else
12725 {
12726 struct text_pos scroll_margin_pos = startp;
12727
12728 /* See if point is inside the scroll margin at the top of the
12729 window. */
12730 if (this_scroll_margin)
12731 {
12732 start_display (&it, w, startp);
12733 move_it_vertically (&it, this_scroll_margin);
12734 scroll_margin_pos = it.current.pos;
12735 }
12736
12737 if (PT < CHARPOS (scroll_margin_pos))
12738 {
12739 /* Point is in the scroll margin at the top of the window or
12740 above what is displayed in the window. */
12741 int y0;
12742
12743 /* Compute the vertical distance from PT to the scroll
12744 margin position. Give up if distance is greater than
12745 scroll_max. */
12746 SET_TEXT_POS (pos, PT, PT_BYTE);
12747 start_display (&it, w, pos);
12748 y0 = it.current_y;
12749 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12750 it.last_visible_y, -1,
12751 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12752 dy = it.current_y - y0;
12753 if (dy > scroll_max)
12754 return SCROLLING_FAILED;
12755
12756 /* Compute new window start. */
12757 start_display (&it, w, startp);
12758
12759 if (scroll_conservatively)
12760 amount_to_scroll
12761 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12762 else if (scroll_step || temp_scroll_step)
12763 amount_to_scroll = scroll_max;
12764 else
12765 {
12766 aggressive = current_buffer->scroll_down_aggressively;
12767 height = WINDOW_BOX_TEXT_HEIGHT (w);
12768 if (NUMBERP (aggressive))
12769 {
12770 double float_amount = XFLOATINT (aggressive) * height;
12771 amount_to_scroll = float_amount;
12772 if (amount_to_scroll == 0 && float_amount > 0)
12773 amount_to_scroll = 1;
12774 }
12775 }
12776
12777 if (amount_to_scroll <= 0)
12778 return SCROLLING_FAILED;
12779
12780 move_it_vertically_backward (&it, amount_to_scroll);
12781 startp = it.current.pos;
12782 }
12783 }
12784
12785 /* Run window scroll functions. */
12786 startp = run_window_scroll_functions (window, startp);
12787
12788 /* Display the window. Give up if new fonts are loaded, or if point
12789 doesn't appear. */
12790 if (!try_window (window, startp, 0))
12791 rc = SCROLLING_NEED_LARGER_MATRICES;
12792 else if (w->cursor.vpos < 0)
12793 {
12794 clear_glyph_matrix (w->desired_matrix);
12795 rc = SCROLLING_FAILED;
12796 }
12797 else
12798 {
12799 /* Maybe forget recorded base line for line number display. */
12800 if (!just_this_one_p
12801 || current_buffer->clip_changed
12802 || BEG_UNCHANGED < CHARPOS (startp))
12803 w->base_line_number = Qnil;
12804
12805 /* If cursor ends up on a partially visible line,
12806 treat that as being off the bottom of the screen. */
12807 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12808 {
12809 clear_glyph_matrix (w->desired_matrix);
12810 ++extra_scroll_margin_lines;
12811 goto too_near_end;
12812 }
12813 rc = SCROLLING_SUCCESS;
12814 }
12815
12816 return rc;
12817 }
12818
12819
12820 /* Compute a suitable window start for window W if display of W starts
12821 on a continuation line. Value is non-zero if a new window start
12822 was computed.
12823
12824 The new window start will be computed, based on W's width, starting
12825 from the start of the continued line. It is the start of the
12826 screen line with the minimum distance from the old start W->start. */
12827
12828 static int
12829 compute_window_start_on_continuation_line (w)
12830 struct window *w;
12831 {
12832 struct text_pos pos, start_pos;
12833 int window_start_changed_p = 0;
12834
12835 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12836
12837 /* If window start is on a continuation line... Window start may be
12838 < BEGV in case there's invisible text at the start of the
12839 buffer (M-x rmail, for example). */
12840 if (CHARPOS (start_pos) > BEGV
12841 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12842 {
12843 struct it it;
12844 struct glyph_row *row;
12845
12846 /* Handle the case that the window start is out of range. */
12847 if (CHARPOS (start_pos) < BEGV)
12848 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12849 else if (CHARPOS (start_pos) > ZV)
12850 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12851
12852 /* Find the start of the continued line. This should be fast
12853 because scan_buffer is fast (newline cache). */
12854 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12855 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12856 row, DEFAULT_FACE_ID);
12857 reseat_at_previous_visible_line_start (&it);
12858
12859 /* If the line start is "too far" away from the window start,
12860 say it takes too much time to compute a new window start. */
12861 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12862 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12863 {
12864 int min_distance, distance;
12865
12866 /* Move forward by display lines to find the new window
12867 start. If window width was enlarged, the new start can
12868 be expected to be > the old start. If window width was
12869 decreased, the new window start will be < the old start.
12870 So, we're looking for the display line start with the
12871 minimum distance from the old window start. */
12872 pos = it.current.pos;
12873 min_distance = INFINITY;
12874 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12875 distance < min_distance)
12876 {
12877 min_distance = distance;
12878 pos = it.current.pos;
12879 move_it_by_lines (&it, 1, 0);
12880 }
12881
12882 /* Set the window start there. */
12883 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12884 window_start_changed_p = 1;
12885 }
12886 }
12887
12888 return window_start_changed_p;
12889 }
12890
12891
12892 /* Try cursor movement in case text has not changed in window WINDOW,
12893 with window start STARTP. Value is
12894
12895 CURSOR_MOVEMENT_SUCCESS if successful
12896
12897 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12898
12899 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12900 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12901 we want to scroll as if scroll-step were set to 1. See the code.
12902
12903 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12904 which case we have to abort this redisplay, and adjust matrices
12905 first. */
12906
12907 enum
12908 {
12909 CURSOR_MOVEMENT_SUCCESS,
12910 CURSOR_MOVEMENT_CANNOT_BE_USED,
12911 CURSOR_MOVEMENT_MUST_SCROLL,
12912 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12913 };
12914
12915 static int
12916 try_cursor_movement (window, startp, scroll_step)
12917 Lisp_Object window;
12918 struct text_pos startp;
12919 int *scroll_step;
12920 {
12921 struct window *w = XWINDOW (window);
12922 struct frame *f = XFRAME (w->frame);
12923 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12924
12925 #if GLYPH_DEBUG
12926 if (inhibit_try_cursor_movement)
12927 return rc;
12928 #endif
12929
12930 /* Handle case where text has not changed, only point, and it has
12931 not moved off the frame. */
12932 if (/* Point may be in this window. */
12933 PT >= CHARPOS (startp)
12934 /* Selective display hasn't changed. */
12935 && !current_buffer->clip_changed
12936 /* Function force-mode-line-update is used to force a thorough
12937 redisplay. It sets either windows_or_buffers_changed or
12938 update_mode_lines. So don't take a shortcut here for these
12939 cases. */
12940 && !update_mode_lines
12941 && !windows_or_buffers_changed
12942 && !cursor_type_changed
12943 /* Can't use this case if highlighting a region. When a
12944 region exists, cursor movement has to do more than just
12945 set the cursor. */
12946 && !(!NILP (Vtransient_mark_mode)
12947 && !NILP (current_buffer->mark_active))
12948 && NILP (w->region_showing)
12949 && NILP (Vshow_trailing_whitespace)
12950 /* Right after splitting windows, last_point may be nil. */
12951 && INTEGERP (w->last_point)
12952 /* This code is not used for mini-buffer for the sake of the case
12953 of redisplaying to replace an echo area message; since in
12954 that case the mini-buffer contents per se are usually
12955 unchanged. This code is of no real use in the mini-buffer
12956 since the handling of this_line_start_pos, etc., in redisplay
12957 handles the same cases. */
12958 && !EQ (window, minibuf_window)
12959 /* When splitting windows or for new windows, it happens that
12960 redisplay is called with a nil window_end_vpos or one being
12961 larger than the window. This should really be fixed in
12962 window.c. I don't have this on my list, now, so we do
12963 approximately the same as the old redisplay code. --gerd. */
12964 && INTEGERP (w->window_end_vpos)
12965 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12966 && (FRAME_WINDOW_P (f)
12967 || !overlay_arrow_in_current_buffer_p ()))
12968 {
12969 int this_scroll_margin, top_scroll_margin;
12970 struct glyph_row *row = NULL;
12971
12972 #if GLYPH_DEBUG
12973 debug_method_add (w, "cursor movement");
12974 #endif
12975
12976 /* Scroll if point within this distance from the top or bottom
12977 of the window. This is a pixel value. */
12978 if (scroll_margin > 0)
12979 {
12980 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12981 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12982 }
12983 else
12984 this_scroll_margin = 0;
12985
12986 top_scroll_margin = this_scroll_margin;
12987 if (WINDOW_WANTS_HEADER_LINE_P (w))
12988 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12989
12990 /* Start with the row the cursor was displayed during the last
12991 not paused redisplay. Give up if that row is not valid. */
12992 if (w->last_cursor.vpos < 0
12993 || w->last_cursor.vpos >= w->current_matrix->nrows)
12994 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12995 else
12996 {
12997 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12998 if (row->mode_line_p)
12999 ++row;
13000 if (!row->enabled_p)
13001 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13002 }
13003
13004 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13005 {
13006 int scroll_p = 0;
13007 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13008
13009 if (PT > XFASTINT (w->last_point))
13010 {
13011 /* Point has moved forward. */
13012 while (MATRIX_ROW_END_CHARPOS (row) < PT
13013 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13014 {
13015 xassert (row->enabled_p);
13016 ++row;
13017 }
13018
13019 /* The end position of a row equals the start position
13020 of the next row. If PT is there, we would rather
13021 display it in the next line. */
13022 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13023 && MATRIX_ROW_END_CHARPOS (row) == PT
13024 && !cursor_row_p (w, row))
13025 ++row;
13026
13027 /* If within the scroll margin, scroll. Note that
13028 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13029 the next line would be drawn, and that
13030 this_scroll_margin can be zero. */
13031 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13032 || PT > MATRIX_ROW_END_CHARPOS (row)
13033 /* Line is completely visible last line in window
13034 and PT is to be set in the next line. */
13035 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13036 && PT == MATRIX_ROW_END_CHARPOS (row)
13037 && !row->ends_at_zv_p
13038 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13039 scroll_p = 1;
13040 }
13041 else if (PT < XFASTINT (w->last_point))
13042 {
13043 /* Cursor has to be moved backward. Note that PT >=
13044 CHARPOS (startp) because of the outer if-statement. */
13045 while (!row->mode_line_p
13046 && (MATRIX_ROW_START_CHARPOS (row) > PT
13047 || (MATRIX_ROW_START_CHARPOS (row) == PT
13048 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13049 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13050 row > w->current_matrix->rows
13051 && (row-1)->ends_in_newline_from_string_p))))
13052 && (row->y > top_scroll_margin
13053 || CHARPOS (startp) == BEGV))
13054 {
13055 xassert (row->enabled_p);
13056 --row;
13057 }
13058
13059 /* Consider the following case: Window starts at BEGV,
13060 there is invisible, intangible text at BEGV, so that
13061 display starts at some point START > BEGV. It can
13062 happen that we are called with PT somewhere between
13063 BEGV and START. Try to handle that case. */
13064 if (row < w->current_matrix->rows
13065 || row->mode_line_p)
13066 {
13067 row = w->current_matrix->rows;
13068 if (row->mode_line_p)
13069 ++row;
13070 }
13071
13072 /* Due to newlines in overlay strings, we may have to
13073 skip forward over overlay strings. */
13074 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13075 && MATRIX_ROW_END_CHARPOS (row) == PT
13076 && !cursor_row_p (w, row))
13077 ++row;
13078
13079 /* If within the scroll margin, scroll. */
13080 if (row->y < top_scroll_margin
13081 && CHARPOS (startp) != BEGV)
13082 scroll_p = 1;
13083 }
13084 else
13085 {
13086 /* Cursor did not move. So don't scroll even if cursor line
13087 is partially visible, as it was so before. */
13088 rc = CURSOR_MOVEMENT_SUCCESS;
13089 }
13090
13091 if (PT < MATRIX_ROW_START_CHARPOS (row)
13092 || PT > MATRIX_ROW_END_CHARPOS (row))
13093 {
13094 /* if PT is not in the glyph row, give up. */
13095 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13096 }
13097 else if (rc != CURSOR_MOVEMENT_SUCCESS
13098 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13099 && make_cursor_line_fully_visible_p)
13100 {
13101 if (PT == MATRIX_ROW_END_CHARPOS (row)
13102 && !row->ends_at_zv_p
13103 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13104 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13105 else if (row->height > window_box_height (w))
13106 {
13107 /* If we end up in a partially visible line, let's
13108 make it fully visible, except when it's taller
13109 than the window, in which case we can't do much
13110 about it. */
13111 *scroll_step = 1;
13112 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13113 }
13114 else
13115 {
13116 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13117 if (!cursor_row_fully_visible_p (w, 0, 1))
13118 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13119 else
13120 rc = CURSOR_MOVEMENT_SUCCESS;
13121 }
13122 }
13123 else if (scroll_p)
13124 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13125 else
13126 {
13127 do
13128 {
13129 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13130 {
13131 rc = CURSOR_MOVEMENT_SUCCESS;
13132 break;
13133 }
13134 ++row;
13135 }
13136 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13137 && MATRIX_ROW_START_CHARPOS (row) == PT
13138 && cursor_row_p (w, row));
13139 }
13140 }
13141 }
13142
13143 return rc;
13144 }
13145
13146 void
13147 set_vertical_scroll_bar (w)
13148 struct window *w;
13149 {
13150 int start, end, whole;
13151
13152 /* Calculate the start and end positions for the current window.
13153 At some point, it would be nice to choose between scrollbars
13154 which reflect the whole buffer size, with special markers
13155 indicating narrowing, and scrollbars which reflect only the
13156 visible region.
13157
13158 Note that mini-buffers sometimes aren't displaying any text. */
13159 if (!MINI_WINDOW_P (w)
13160 || (w == XWINDOW (minibuf_window)
13161 && NILP (echo_area_buffer[0])))
13162 {
13163 struct buffer *buf = XBUFFER (w->buffer);
13164 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13165 start = marker_position (w->start) - BUF_BEGV (buf);
13166 /* I don't think this is guaranteed to be right. For the
13167 moment, we'll pretend it is. */
13168 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13169
13170 if (end < start)
13171 end = start;
13172 if (whole < (end - start))
13173 whole = end - start;
13174 }
13175 else
13176 start = end = whole = 0;
13177
13178 /* Indicate what this scroll bar ought to be displaying now. */
13179 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13180 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13181 (w, end - start, whole, start);
13182 }
13183
13184
13185 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13186 selected_window is redisplayed.
13187
13188 We can return without actually redisplaying the window if
13189 fonts_changed_p is nonzero. In that case, redisplay_internal will
13190 retry. */
13191
13192 static void
13193 redisplay_window (window, just_this_one_p)
13194 Lisp_Object window;
13195 int just_this_one_p;
13196 {
13197 struct window *w = XWINDOW (window);
13198 struct frame *f = XFRAME (w->frame);
13199 struct buffer *buffer = XBUFFER (w->buffer);
13200 struct buffer *old = current_buffer;
13201 struct text_pos lpoint, opoint, startp;
13202 int update_mode_line;
13203 int tem;
13204 struct it it;
13205 /* Record it now because it's overwritten. */
13206 int current_matrix_up_to_date_p = 0;
13207 int used_current_matrix_p = 0;
13208 /* This is less strict than current_matrix_up_to_date_p.
13209 It indictes that the buffer contents and narrowing are unchanged. */
13210 int buffer_unchanged_p = 0;
13211 int temp_scroll_step = 0;
13212 int count = SPECPDL_INDEX ();
13213 int rc;
13214 int centering_position = -1;
13215 int last_line_misfit = 0;
13216 int beg_unchanged, end_unchanged;
13217
13218 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13219 opoint = lpoint;
13220
13221 /* W must be a leaf window here. */
13222 xassert (!NILP (w->buffer));
13223 #if GLYPH_DEBUG
13224 *w->desired_matrix->method = 0;
13225 #endif
13226
13227 restart:
13228 reconsider_clip_changes (w, buffer);
13229
13230 /* Has the mode line to be updated? */
13231 update_mode_line = (!NILP (w->update_mode_line)
13232 || update_mode_lines
13233 || buffer->clip_changed
13234 || buffer->prevent_redisplay_optimizations_p);
13235
13236 if (MINI_WINDOW_P (w))
13237 {
13238 if (w == XWINDOW (echo_area_window)
13239 && !NILP (echo_area_buffer[0]))
13240 {
13241 if (update_mode_line)
13242 /* We may have to update a tty frame's menu bar or a
13243 tool-bar. Example `M-x C-h C-h C-g'. */
13244 goto finish_menu_bars;
13245 else
13246 /* We've already displayed the echo area glyphs in this window. */
13247 goto finish_scroll_bars;
13248 }
13249 else if ((w != XWINDOW (minibuf_window)
13250 || minibuf_level == 0)
13251 /* When buffer is nonempty, redisplay window normally. */
13252 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13253 /* Quail displays non-mini buffers in minibuffer window.
13254 In that case, redisplay the window normally. */
13255 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13256 {
13257 /* W is a mini-buffer window, but it's not active, so clear
13258 it. */
13259 int yb = window_text_bottom_y (w);
13260 struct glyph_row *row;
13261 int y;
13262
13263 for (y = 0, row = w->desired_matrix->rows;
13264 y < yb;
13265 y += row->height, ++row)
13266 blank_row (w, row, y);
13267 goto finish_scroll_bars;
13268 }
13269
13270 clear_glyph_matrix (w->desired_matrix);
13271 }
13272
13273 /* Otherwise set up data on this window; select its buffer and point
13274 value. */
13275 /* Really select the buffer, for the sake of buffer-local
13276 variables. */
13277 set_buffer_internal_1 (XBUFFER (w->buffer));
13278
13279 current_matrix_up_to_date_p
13280 = (!NILP (w->window_end_valid)
13281 && !current_buffer->clip_changed
13282 && !current_buffer->prevent_redisplay_optimizations_p
13283 && XFASTINT (w->last_modified) >= MODIFF
13284 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13285
13286 /* Run the window-bottom-change-functions
13287 if it is possible that the text on the screen has changed
13288 (either due to modification of the text, or any other reason). */
13289 if (!current_matrix_up_to_date_p
13290 && !NILP (Vwindow_text_change_functions))
13291 {
13292 safe_run_hooks (Qwindow_text_change_functions);
13293 goto restart;
13294 }
13295
13296 beg_unchanged = BEG_UNCHANGED;
13297 end_unchanged = END_UNCHANGED;
13298
13299 SET_TEXT_POS (opoint, PT, PT_BYTE);
13300
13301 specbind (Qinhibit_point_motion_hooks, Qt);
13302
13303 buffer_unchanged_p
13304 = (!NILP (w->window_end_valid)
13305 && !current_buffer->clip_changed
13306 && XFASTINT (w->last_modified) >= MODIFF
13307 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13308
13309 /* When windows_or_buffers_changed is non-zero, we can't rely on
13310 the window end being valid, so set it to nil there. */
13311 if (windows_or_buffers_changed)
13312 {
13313 /* If window starts on a continuation line, maybe adjust the
13314 window start in case the window's width changed. */
13315 if (XMARKER (w->start)->buffer == current_buffer)
13316 compute_window_start_on_continuation_line (w);
13317
13318 w->window_end_valid = Qnil;
13319 }
13320
13321 /* Some sanity checks. */
13322 CHECK_WINDOW_END (w);
13323 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13324 abort ();
13325 if (BYTEPOS (opoint) < CHARPOS (opoint))
13326 abort ();
13327
13328 /* If %c is in mode line, update it if needed. */
13329 if (!NILP (w->column_number_displayed)
13330 /* This alternative quickly identifies a common case
13331 where no change is needed. */
13332 && !(PT == XFASTINT (w->last_point)
13333 && XFASTINT (w->last_modified) >= MODIFF
13334 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13335 && (XFASTINT (w->column_number_displayed)
13336 != (int) current_column ())) /* iftc */
13337 update_mode_line = 1;
13338
13339 /* Count number of windows showing the selected buffer. An indirect
13340 buffer counts as its base buffer. */
13341 if (!just_this_one_p)
13342 {
13343 struct buffer *current_base, *window_base;
13344 current_base = current_buffer;
13345 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13346 if (current_base->base_buffer)
13347 current_base = current_base->base_buffer;
13348 if (window_base->base_buffer)
13349 window_base = window_base->base_buffer;
13350 if (current_base == window_base)
13351 buffer_shared++;
13352 }
13353
13354 /* Point refers normally to the selected window. For any other
13355 window, set up appropriate value. */
13356 if (!EQ (window, selected_window))
13357 {
13358 int new_pt = XMARKER (w->pointm)->charpos;
13359 int new_pt_byte = marker_byte_position (w->pointm);
13360 if (new_pt < BEGV)
13361 {
13362 new_pt = BEGV;
13363 new_pt_byte = BEGV_BYTE;
13364 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13365 }
13366 else if (new_pt > (ZV - 1))
13367 {
13368 new_pt = ZV;
13369 new_pt_byte = ZV_BYTE;
13370 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13371 }
13372
13373 /* We don't use SET_PT so that the point-motion hooks don't run. */
13374 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13375 }
13376
13377 /* If any of the character widths specified in the display table
13378 have changed, invalidate the width run cache. It's true that
13379 this may be a bit late to catch such changes, but the rest of
13380 redisplay goes (non-fatally) haywire when the display table is
13381 changed, so why should we worry about doing any better? */
13382 if (current_buffer->width_run_cache)
13383 {
13384 struct Lisp_Char_Table *disptab = buffer_display_table ();
13385
13386 if (! disptab_matches_widthtab (disptab,
13387 XVECTOR (current_buffer->width_table)))
13388 {
13389 invalidate_region_cache (current_buffer,
13390 current_buffer->width_run_cache,
13391 BEG, Z);
13392 recompute_width_table (current_buffer, disptab);
13393 }
13394 }
13395
13396 /* If window-start is screwed up, choose a new one. */
13397 if (XMARKER (w->start)->buffer != current_buffer)
13398 goto recenter;
13399
13400 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13401
13402 /* If someone specified a new starting point but did not insist,
13403 check whether it can be used. */
13404 if (!NILP (w->optional_new_start)
13405 && CHARPOS (startp) >= BEGV
13406 && CHARPOS (startp) <= ZV)
13407 {
13408 w->optional_new_start = Qnil;
13409 start_display (&it, w, startp);
13410 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13411 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13412 if (IT_CHARPOS (it) == PT)
13413 w->force_start = Qt;
13414 /* IT may overshoot PT if text at PT is invisible. */
13415 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13416 w->force_start = Qt;
13417 }
13418
13419 force_start:
13420
13421 /* Handle case where place to start displaying has been specified,
13422 unless the specified location is outside the accessible range. */
13423 if (!NILP (w->force_start)
13424 || w->frozen_window_start_p)
13425 {
13426 /* We set this later on if we have to adjust point. */
13427 int new_vpos = -1;
13428
13429 w->force_start = Qnil;
13430 w->vscroll = 0;
13431 w->window_end_valid = Qnil;
13432
13433 /* Forget any recorded base line for line number display. */
13434 if (!buffer_unchanged_p)
13435 w->base_line_number = Qnil;
13436
13437 /* Redisplay the mode line. Select the buffer properly for that.
13438 Also, run the hook window-scroll-functions
13439 because we have scrolled. */
13440 /* Note, we do this after clearing force_start because
13441 if there's an error, it is better to forget about force_start
13442 than to get into an infinite loop calling the hook functions
13443 and having them get more errors. */
13444 if (!update_mode_line
13445 || ! NILP (Vwindow_scroll_functions))
13446 {
13447 update_mode_line = 1;
13448 w->update_mode_line = Qt;
13449 startp = run_window_scroll_functions (window, startp);
13450 }
13451
13452 w->last_modified = make_number (0);
13453 w->last_overlay_modified = make_number (0);
13454 if (CHARPOS (startp) < BEGV)
13455 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13456 else if (CHARPOS (startp) > ZV)
13457 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13458
13459 /* Redisplay, then check if cursor has been set during the
13460 redisplay. Give up if new fonts were loaded. */
13461 /* We used to issue a CHECK_MARGINS argument to try_window here,
13462 but this causes scrolling to fail when point begins inside
13463 the scroll margin (bug#148) -- cyd */
13464 if (!try_window (window, startp, 0))
13465 {
13466 w->force_start = Qt;
13467 clear_glyph_matrix (w->desired_matrix);
13468 goto need_larger_matrices;
13469 }
13470
13471 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13472 {
13473 /* If point does not appear, try to move point so it does
13474 appear. The desired matrix has been built above, so we
13475 can use it here. */
13476 new_vpos = window_box_height (w) / 2;
13477 }
13478
13479 if (!cursor_row_fully_visible_p (w, 0, 0))
13480 {
13481 /* Point does appear, but on a line partly visible at end of window.
13482 Move it back to a fully-visible line. */
13483 new_vpos = window_box_height (w);
13484 }
13485
13486 /* If we need to move point for either of the above reasons,
13487 now actually do it. */
13488 if (new_vpos >= 0)
13489 {
13490 struct glyph_row *row;
13491
13492 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13493 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13494 ++row;
13495
13496 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13497 MATRIX_ROW_START_BYTEPOS (row));
13498
13499 if (w != XWINDOW (selected_window))
13500 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13501 else if (current_buffer == old)
13502 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13503
13504 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13505
13506 /* If we are highlighting the region, then we just changed
13507 the region, so redisplay to show it. */
13508 if (!NILP (Vtransient_mark_mode)
13509 && !NILP (current_buffer->mark_active))
13510 {
13511 clear_glyph_matrix (w->desired_matrix);
13512 if (!try_window (window, startp, 0))
13513 goto need_larger_matrices;
13514 }
13515 }
13516
13517 #if GLYPH_DEBUG
13518 debug_method_add (w, "forced window start");
13519 #endif
13520 goto done;
13521 }
13522
13523 /* Handle case where text has not changed, only point, and it has
13524 not moved off the frame, and we are not retrying after hscroll.
13525 (current_matrix_up_to_date_p is nonzero when retrying.) */
13526 if (current_matrix_up_to_date_p
13527 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13528 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13529 {
13530 switch (rc)
13531 {
13532 case CURSOR_MOVEMENT_SUCCESS:
13533 used_current_matrix_p = 1;
13534 goto done;
13535
13536 case CURSOR_MOVEMENT_MUST_SCROLL:
13537 goto try_to_scroll;
13538
13539 default:
13540 abort ();
13541 }
13542 }
13543 /* If current starting point was originally the beginning of a line
13544 but no longer is, find a new starting point. */
13545 else if (!NILP (w->start_at_line_beg)
13546 && !(CHARPOS (startp) <= BEGV
13547 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13548 {
13549 #if GLYPH_DEBUG
13550 debug_method_add (w, "recenter 1");
13551 #endif
13552 goto recenter;
13553 }
13554
13555 /* Try scrolling with try_window_id. Value is > 0 if update has
13556 been done, it is -1 if we know that the same window start will
13557 not work. It is 0 if unsuccessful for some other reason. */
13558 else if ((tem = try_window_id (w)) != 0)
13559 {
13560 #if GLYPH_DEBUG
13561 debug_method_add (w, "try_window_id %d", tem);
13562 #endif
13563
13564 if (fonts_changed_p)
13565 goto need_larger_matrices;
13566 if (tem > 0)
13567 goto done;
13568
13569 /* Otherwise try_window_id has returned -1 which means that we
13570 don't want the alternative below this comment to execute. */
13571 }
13572 else if (CHARPOS (startp) >= BEGV
13573 && CHARPOS (startp) <= ZV
13574 && PT >= CHARPOS (startp)
13575 && (CHARPOS (startp) < ZV
13576 /* Avoid starting at end of buffer. */
13577 || CHARPOS (startp) == BEGV
13578 || (XFASTINT (w->last_modified) >= MODIFF
13579 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13580 {
13581
13582 /* If first window line is a continuation line, and window start
13583 is inside the modified region, but the first change is before
13584 current window start, we must select a new window start.
13585
13586 However, if this is the result of a down-mouse event (e.g. by
13587 extending the mouse-drag-overlay), we don't want to select a
13588 new window start, since that would change the position under
13589 the mouse, resulting in an unwanted mouse-movement rather
13590 than a simple mouse-click. */
13591 if (NILP (w->start_at_line_beg)
13592 && NILP (do_mouse_tracking)
13593 && CHARPOS (startp) > BEGV
13594 && CHARPOS (startp) > BEG + beg_unchanged
13595 && CHARPOS (startp) <= Z - end_unchanged
13596 /* Even if w->start_at_line_beg is nil, a new window may
13597 start at a line_beg, since that's how set_buffer_window
13598 sets it. So, we need to check the return value of
13599 compute_window_start_on_continuation_line. (See also
13600 bug#197). */
13601 && XMARKER (w->start)->buffer == current_buffer
13602 && compute_window_start_on_continuation_line (w))
13603 {
13604 w->force_start = Qt;
13605 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13606 goto force_start;
13607 }
13608
13609 #if GLYPH_DEBUG
13610 debug_method_add (w, "same window start");
13611 #endif
13612
13613 /* Try to redisplay starting at same place as before.
13614 If point has not moved off frame, accept the results. */
13615 if (!current_matrix_up_to_date_p
13616 /* Don't use try_window_reusing_current_matrix in this case
13617 because a window scroll function can have changed the
13618 buffer. */
13619 || !NILP (Vwindow_scroll_functions)
13620 || MINI_WINDOW_P (w)
13621 || !(used_current_matrix_p
13622 = try_window_reusing_current_matrix (w)))
13623 {
13624 IF_DEBUG (debug_method_add (w, "1"));
13625 if (try_window (window, startp, 1) < 0)
13626 /* -1 means we need to scroll.
13627 0 means we need new matrices, but fonts_changed_p
13628 is set in that case, so we will detect it below. */
13629 goto try_to_scroll;
13630 }
13631
13632 if (fonts_changed_p)
13633 goto need_larger_matrices;
13634
13635 if (w->cursor.vpos >= 0)
13636 {
13637 if (!just_this_one_p
13638 || current_buffer->clip_changed
13639 || BEG_UNCHANGED < CHARPOS (startp))
13640 /* Forget any recorded base line for line number display. */
13641 w->base_line_number = Qnil;
13642
13643 if (!cursor_row_fully_visible_p (w, 1, 0))
13644 {
13645 clear_glyph_matrix (w->desired_matrix);
13646 last_line_misfit = 1;
13647 }
13648 /* Drop through and scroll. */
13649 else
13650 goto done;
13651 }
13652 else
13653 clear_glyph_matrix (w->desired_matrix);
13654 }
13655
13656 try_to_scroll:
13657
13658 w->last_modified = make_number (0);
13659 w->last_overlay_modified = make_number (0);
13660
13661 /* Redisplay the mode line. Select the buffer properly for that. */
13662 if (!update_mode_line)
13663 {
13664 update_mode_line = 1;
13665 w->update_mode_line = Qt;
13666 }
13667
13668 /* Try to scroll by specified few lines. */
13669 if ((scroll_conservatively
13670 || scroll_step
13671 || temp_scroll_step
13672 || NUMBERP (current_buffer->scroll_up_aggressively)
13673 || NUMBERP (current_buffer->scroll_down_aggressively))
13674 && !current_buffer->clip_changed
13675 && CHARPOS (startp) >= BEGV
13676 && CHARPOS (startp) <= ZV)
13677 {
13678 /* The function returns -1 if new fonts were loaded, 1 if
13679 successful, 0 if not successful. */
13680 int rc = try_scrolling (window, just_this_one_p,
13681 scroll_conservatively,
13682 scroll_step,
13683 temp_scroll_step, last_line_misfit);
13684 switch (rc)
13685 {
13686 case SCROLLING_SUCCESS:
13687 goto done;
13688
13689 case SCROLLING_NEED_LARGER_MATRICES:
13690 goto need_larger_matrices;
13691
13692 case SCROLLING_FAILED:
13693 break;
13694
13695 default:
13696 abort ();
13697 }
13698 }
13699
13700 /* Finally, just choose place to start which centers point */
13701
13702 recenter:
13703 if (centering_position < 0)
13704 centering_position = window_box_height (w) / 2;
13705
13706 #if GLYPH_DEBUG
13707 debug_method_add (w, "recenter");
13708 #endif
13709
13710 /* w->vscroll = 0; */
13711
13712 /* Forget any previously recorded base line for line number display. */
13713 if (!buffer_unchanged_p)
13714 w->base_line_number = Qnil;
13715
13716 /* Move backward half the height of the window. */
13717 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13718 it.current_y = it.last_visible_y;
13719 move_it_vertically_backward (&it, centering_position);
13720 xassert (IT_CHARPOS (it) >= BEGV);
13721
13722 /* The function move_it_vertically_backward may move over more
13723 than the specified y-distance. If it->w is small, e.g. a
13724 mini-buffer window, we may end up in front of the window's
13725 display area. Start displaying at the start of the line
13726 containing PT in this case. */
13727 if (it.current_y <= 0)
13728 {
13729 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13730 move_it_vertically_backward (&it, 0);
13731 it.current_y = 0;
13732 }
13733
13734 it.current_x = it.hpos = 0;
13735
13736 /* Set startp here explicitly in case that helps avoid an infinite loop
13737 in case the window-scroll-functions functions get errors. */
13738 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13739
13740 /* Run scroll hooks. */
13741 startp = run_window_scroll_functions (window, it.current.pos);
13742
13743 /* Redisplay the window. */
13744 if (!current_matrix_up_to_date_p
13745 || windows_or_buffers_changed
13746 || cursor_type_changed
13747 /* Don't use try_window_reusing_current_matrix in this case
13748 because it can have changed the buffer. */
13749 || !NILP (Vwindow_scroll_functions)
13750 || !just_this_one_p
13751 || MINI_WINDOW_P (w)
13752 || !(used_current_matrix_p
13753 = try_window_reusing_current_matrix (w)))
13754 try_window (window, startp, 0);
13755
13756 /* If new fonts have been loaded (due to fontsets), give up. We
13757 have to start a new redisplay since we need to re-adjust glyph
13758 matrices. */
13759 if (fonts_changed_p)
13760 goto need_larger_matrices;
13761
13762 /* If cursor did not appear assume that the middle of the window is
13763 in the first line of the window. Do it again with the next line.
13764 (Imagine a window of height 100, displaying two lines of height
13765 60. Moving back 50 from it->last_visible_y will end in the first
13766 line.) */
13767 if (w->cursor.vpos < 0)
13768 {
13769 if (!NILP (w->window_end_valid)
13770 && PT >= Z - XFASTINT (w->window_end_pos))
13771 {
13772 clear_glyph_matrix (w->desired_matrix);
13773 move_it_by_lines (&it, 1, 0);
13774 try_window (window, it.current.pos, 0);
13775 }
13776 else if (PT < IT_CHARPOS (it))
13777 {
13778 clear_glyph_matrix (w->desired_matrix);
13779 move_it_by_lines (&it, -1, 0);
13780 try_window (window, it.current.pos, 0);
13781 }
13782 else
13783 {
13784 /* Not much we can do about it. */
13785 }
13786 }
13787
13788 /* Consider the following case: Window starts at BEGV, there is
13789 invisible, intangible text at BEGV, so that display starts at
13790 some point START > BEGV. It can happen that we are called with
13791 PT somewhere between BEGV and START. Try to handle that case. */
13792 if (w->cursor.vpos < 0)
13793 {
13794 struct glyph_row *row = w->current_matrix->rows;
13795 if (row->mode_line_p)
13796 ++row;
13797 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13798 }
13799
13800 if (!cursor_row_fully_visible_p (w, 0, 0))
13801 {
13802 /* If vscroll is enabled, disable it and try again. */
13803 if (w->vscroll)
13804 {
13805 w->vscroll = 0;
13806 clear_glyph_matrix (w->desired_matrix);
13807 goto recenter;
13808 }
13809
13810 /* If centering point failed to make the whole line visible,
13811 put point at the top instead. That has to make the whole line
13812 visible, if it can be done. */
13813 if (centering_position == 0)
13814 goto done;
13815
13816 clear_glyph_matrix (w->desired_matrix);
13817 centering_position = 0;
13818 goto recenter;
13819 }
13820
13821 done:
13822
13823 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13824 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13825 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13826 ? Qt : Qnil);
13827
13828 /* Display the mode line, if we must. */
13829 if ((update_mode_line
13830 /* If window not full width, must redo its mode line
13831 if (a) the window to its side is being redone and
13832 (b) we do a frame-based redisplay. This is a consequence
13833 of how inverted lines are drawn in frame-based redisplay. */
13834 || (!just_this_one_p
13835 && !FRAME_WINDOW_P (f)
13836 && !WINDOW_FULL_WIDTH_P (w))
13837 /* Line number to display. */
13838 || INTEGERP (w->base_line_pos)
13839 /* Column number is displayed and different from the one displayed. */
13840 || (!NILP (w->column_number_displayed)
13841 && (XFASTINT (w->column_number_displayed)
13842 != (int) current_column ()))) /* iftc */
13843 /* This means that the window has a mode line. */
13844 && (WINDOW_WANTS_MODELINE_P (w)
13845 || WINDOW_WANTS_HEADER_LINE_P (w)))
13846 {
13847 display_mode_lines (w);
13848
13849 /* If mode line height has changed, arrange for a thorough
13850 immediate redisplay using the correct mode line height. */
13851 if (WINDOW_WANTS_MODELINE_P (w)
13852 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13853 {
13854 fonts_changed_p = 1;
13855 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13856 = DESIRED_MODE_LINE_HEIGHT (w);
13857 }
13858
13859 /* If top line height has changed, arrange for a thorough
13860 immediate redisplay using the correct mode line height. */
13861 if (WINDOW_WANTS_HEADER_LINE_P (w)
13862 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13863 {
13864 fonts_changed_p = 1;
13865 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13866 = DESIRED_HEADER_LINE_HEIGHT (w);
13867 }
13868
13869 if (fonts_changed_p)
13870 goto need_larger_matrices;
13871 }
13872
13873 if (!line_number_displayed
13874 && !BUFFERP (w->base_line_pos))
13875 {
13876 w->base_line_pos = Qnil;
13877 w->base_line_number = Qnil;
13878 }
13879
13880 finish_menu_bars:
13881
13882 /* When we reach a frame's selected window, redo the frame's menu bar. */
13883 if (update_mode_line
13884 && EQ (FRAME_SELECTED_WINDOW (f), window))
13885 {
13886 int redisplay_menu_p = 0;
13887 int redisplay_tool_bar_p = 0;
13888
13889 if (FRAME_WINDOW_P (f))
13890 {
13891 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13892 || defined (HAVE_NS) || defined (USE_GTK)
13893 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13894 #else
13895 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13896 #endif
13897 }
13898 else
13899 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13900
13901 if (redisplay_menu_p)
13902 display_menu_bar (w);
13903
13904 #ifdef HAVE_WINDOW_SYSTEM
13905 if (FRAME_WINDOW_P (f))
13906 {
13907 #if defined (USE_GTK) || defined (HAVE_NS)
13908 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13909 #else
13910 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13911 && (FRAME_TOOL_BAR_LINES (f) > 0
13912 || !NILP (Vauto_resize_tool_bars));
13913 #endif
13914
13915 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13916 {
13917 extern int ignore_mouse_drag_p;
13918 ignore_mouse_drag_p = 1;
13919 }
13920 }
13921 #endif
13922 }
13923
13924 #ifdef HAVE_WINDOW_SYSTEM
13925 if (FRAME_WINDOW_P (f)
13926 && update_window_fringes (w, (just_this_one_p
13927 || (!used_current_matrix_p && !overlay_arrow_seen)
13928 || w->pseudo_window_p)))
13929 {
13930 update_begin (f);
13931 BLOCK_INPUT;
13932 if (draw_window_fringes (w, 1))
13933 x_draw_vertical_border (w);
13934 UNBLOCK_INPUT;
13935 update_end (f);
13936 }
13937 #endif /* HAVE_WINDOW_SYSTEM */
13938
13939 /* We go to this label, with fonts_changed_p nonzero,
13940 if it is necessary to try again using larger glyph matrices.
13941 We have to redeem the scroll bar even in this case,
13942 because the loop in redisplay_internal expects that. */
13943 need_larger_matrices:
13944 ;
13945 finish_scroll_bars:
13946
13947 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13948 {
13949 /* Set the thumb's position and size. */
13950 set_vertical_scroll_bar (w);
13951
13952 /* Note that we actually used the scroll bar attached to this
13953 window, so it shouldn't be deleted at the end of redisplay. */
13954 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13955 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13956 }
13957
13958 /* Restore current_buffer and value of point in it. */
13959 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13960 set_buffer_internal_1 (old);
13961 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13962 shorter. This can be caused by log truncation in *Messages*. */
13963 if (CHARPOS (lpoint) <= ZV)
13964 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13965
13966 unbind_to (count, Qnil);
13967 }
13968
13969
13970 /* Build the complete desired matrix of WINDOW with a window start
13971 buffer position POS.
13972
13973 Value is 1 if successful. It is zero if fonts were loaded during
13974 redisplay which makes re-adjusting glyph matrices necessary, and -1
13975 if point would appear in the scroll margins.
13976 (We check that only if CHECK_MARGINS is nonzero. */
13977
13978 int
13979 try_window (window, pos, check_margins)
13980 Lisp_Object window;
13981 struct text_pos pos;
13982 int check_margins;
13983 {
13984 struct window *w = XWINDOW (window);
13985 struct it it;
13986 struct glyph_row *last_text_row = NULL;
13987 struct frame *f = XFRAME (w->frame);
13988
13989 /* Make POS the new window start. */
13990 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13991
13992 /* Mark cursor position as unknown. No overlay arrow seen. */
13993 w->cursor.vpos = -1;
13994 overlay_arrow_seen = 0;
13995
13996 /* Initialize iterator and info to start at POS. */
13997 start_display (&it, w, pos);
13998
13999 /* Display all lines of W. */
14000 while (it.current_y < it.last_visible_y)
14001 {
14002 if (display_line (&it))
14003 last_text_row = it.glyph_row - 1;
14004 if (fonts_changed_p)
14005 return 0;
14006 }
14007
14008 /* Don't let the cursor end in the scroll margins. */
14009 if (check_margins
14010 && !MINI_WINDOW_P (w))
14011 {
14012 int this_scroll_margin;
14013
14014 if (scroll_margin > 0)
14015 {
14016 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14017 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14018 }
14019 else
14020 this_scroll_margin = 0;
14021
14022 if ((w->cursor.y >= 0 /* not vscrolled */
14023 && w->cursor.y < this_scroll_margin
14024 && CHARPOS (pos) > BEGV
14025 && IT_CHARPOS (it) < ZV)
14026 /* rms: considering make_cursor_line_fully_visible_p here
14027 seems to give wrong results. We don't want to recenter
14028 when the last line is partly visible, we want to allow
14029 that case to be handled in the usual way. */
14030 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14031 {
14032 w->cursor.vpos = -1;
14033 clear_glyph_matrix (w->desired_matrix);
14034 return -1;
14035 }
14036 }
14037
14038 /* If bottom moved off end of frame, change mode line percentage. */
14039 if (XFASTINT (w->window_end_pos) <= 0
14040 && Z != IT_CHARPOS (it))
14041 w->update_mode_line = Qt;
14042
14043 /* Set window_end_pos to the offset of the last character displayed
14044 on the window from the end of current_buffer. Set
14045 window_end_vpos to its row number. */
14046 if (last_text_row)
14047 {
14048 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14049 w->window_end_bytepos
14050 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14051 w->window_end_pos
14052 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14053 w->window_end_vpos
14054 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14055 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14056 ->displays_text_p);
14057 }
14058 else
14059 {
14060 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14061 w->window_end_pos = make_number (Z - ZV);
14062 w->window_end_vpos = make_number (0);
14063 }
14064
14065 /* But that is not valid info until redisplay finishes. */
14066 w->window_end_valid = Qnil;
14067 return 1;
14068 }
14069
14070
14071 \f
14072 /************************************************************************
14073 Window redisplay reusing current matrix when buffer has not changed
14074 ************************************************************************/
14075
14076 /* Try redisplay of window W showing an unchanged buffer with a
14077 different window start than the last time it was displayed by
14078 reusing its current matrix. Value is non-zero if successful.
14079 W->start is the new window start. */
14080
14081 static int
14082 try_window_reusing_current_matrix (w)
14083 struct window *w;
14084 {
14085 struct frame *f = XFRAME (w->frame);
14086 struct glyph_row *row, *bottom_row;
14087 struct it it;
14088 struct run run;
14089 struct text_pos start, new_start;
14090 int nrows_scrolled, i;
14091 struct glyph_row *last_text_row;
14092 struct glyph_row *last_reused_text_row;
14093 struct glyph_row *start_row;
14094 int start_vpos, min_y, max_y;
14095
14096 #if GLYPH_DEBUG
14097 if (inhibit_try_window_reusing)
14098 return 0;
14099 #endif
14100
14101 if (/* This function doesn't handle terminal frames. */
14102 !FRAME_WINDOW_P (f)
14103 /* Don't try to reuse the display if windows have been split
14104 or such. */
14105 || windows_or_buffers_changed
14106 || cursor_type_changed)
14107 return 0;
14108
14109 /* Can't do this if region may have changed. */
14110 if ((!NILP (Vtransient_mark_mode)
14111 && !NILP (current_buffer->mark_active))
14112 || !NILP (w->region_showing)
14113 || !NILP (Vshow_trailing_whitespace))
14114 return 0;
14115
14116 /* If top-line visibility has changed, give up. */
14117 if (WINDOW_WANTS_HEADER_LINE_P (w)
14118 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14119 return 0;
14120
14121 /* Give up if old or new display is scrolled vertically. We could
14122 make this function handle this, but right now it doesn't. */
14123 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14124 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14125 return 0;
14126
14127 /* The variable new_start now holds the new window start. The old
14128 start `start' can be determined from the current matrix. */
14129 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14130 start = start_row->start.pos;
14131 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14132
14133 /* Clear the desired matrix for the display below. */
14134 clear_glyph_matrix (w->desired_matrix);
14135
14136 if (CHARPOS (new_start) <= CHARPOS (start))
14137 {
14138 int first_row_y;
14139
14140 /* Don't use this method if the display starts with an ellipsis
14141 displayed for invisible text. It's not easy to handle that case
14142 below, and it's certainly not worth the effort since this is
14143 not a frequent case. */
14144 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14145 return 0;
14146
14147 IF_DEBUG (debug_method_add (w, "twu1"));
14148
14149 /* Display up to a row that can be reused. The variable
14150 last_text_row is set to the last row displayed that displays
14151 text. Note that it.vpos == 0 if or if not there is a
14152 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14153 start_display (&it, w, new_start);
14154 first_row_y = it.current_y;
14155 w->cursor.vpos = -1;
14156 last_text_row = last_reused_text_row = NULL;
14157
14158 while (it.current_y < it.last_visible_y
14159 && !fonts_changed_p)
14160 {
14161 /* If we have reached into the characters in the START row,
14162 that means the line boundaries have changed. So we
14163 can't start copying with the row START. Maybe it will
14164 work to start copying with the following row. */
14165 while (IT_CHARPOS (it) > CHARPOS (start))
14166 {
14167 /* Advance to the next row as the "start". */
14168 start_row++;
14169 start = start_row->start.pos;
14170 /* If there are no more rows to try, or just one, give up. */
14171 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14172 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14173 || CHARPOS (start) == ZV)
14174 {
14175 clear_glyph_matrix (w->desired_matrix);
14176 return 0;
14177 }
14178
14179 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14180 }
14181 /* If we have reached alignment,
14182 we can copy the rest of the rows. */
14183 if (IT_CHARPOS (it) == CHARPOS (start))
14184 break;
14185
14186 if (display_line (&it))
14187 last_text_row = it.glyph_row - 1;
14188 }
14189
14190 /* A value of current_y < last_visible_y means that we stopped
14191 at the previous window start, which in turn means that we
14192 have at least one reusable row. */
14193 if (it.current_y < it.last_visible_y)
14194 {
14195 /* IT.vpos always starts from 0; it counts text lines. */
14196 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14197
14198 /* Find PT if not already found in the lines displayed. */
14199 if (w->cursor.vpos < 0)
14200 {
14201 int dy = it.current_y - start_row->y;
14202
14203 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14204 row = row_containing_pos (w, PT, row, NULL, dy);
14205 if (row)
14206 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14207 dy, nrows_scrolled);
14208 else
14209 {
14210 clear_glyph_matrix (w->desired_matrix);
14211 return 0;
14212 }
14213 }
14214
14215 /* Scroll the display. Do it before the current matrix is
14216 changed. The problem here is that update has not yet
14217 run, i.e. part of the current matrix is not up to date.
14218 scroll_run_hook will clear the cursor, and use the
14219 current matrix to get the height of the row the cursor is
14220 in. */
14221 run.current_y = start_row->y;
14222 run.desired_y = it.current_y;
14223 run.height = it.last_visible_y - it.current_y;
14224
14225 if (run.height > 0 && run.current_y != run.desired_y)
14226 {
14227 update_begin (f);
14228 FRAME_RIF (f)->update_window_begin_hook (w);
14229 FRAME_RIF (f)->clear_window_mouse_face (w);
14230 FRAME_RIF (f)->scroll_run_hook (w, &run);
14231 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14232 update_end (f);
14233 }
14234
14235 /* Shift current matrix down by nrows_scrolled lines. */
14236 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14237 rotate_matrix (w->current_matrix,
14238 start_vpos,
14239 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14240 nrows_scrolled);
14241
14242 /* Disable lines that must be updated. */
14243 for (i = 0; i < nrows_scrolled; ++i)
14244 (start_row + i)->enabled_p = 0;
14245
14246 /* Re-compute Y positions. */
14247 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14248 max_y = it.last_visible_y;
14249 for (row = start_row + nrows_scrolled;
14250 row < bottom_row;
14251 ++row)
14252 {
14253 row->y = it.current_y;
14254 row->visible_height = row->height;
14255
14256 if (row->y < min_y)
14257 row->visible_height -= min_y - row->y;
14258 if (row->y + row->height > max_y)
14259 row->visible_height -= row->y + row->height - max_y;
14260 row->redraw_fringe_bitmaps_p = 1;
14261
14262 it.current_y += row->height;
14263
14264 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14265 last_reused_text_row = row;
14266 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14267 break;
14268 }
14269
14270 /* Disable lines in the current matrix which are now
14271 below the window. */
14272 for (++row; row < bottom_row; ++row)
14273 row->enabled_p = row->mode_line_p = 0;
14274 }
14275
14276 /* Update window_end_pos etc.; last_reused_text_row is the last
14277 reused row from the current matrix containing text, if any.
14278 The value of last_text_row is the last displayed line
14279 containing text. */
14280 if (last_reused_text_row)
14281 {
14282 w->window_end_bytepos
14283 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14284 w->window_end_pos
14285 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14286 w->window_end_vpos
14287 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14288 w->current_matrix));
14289 }
14290 else if (last_text_row)
14291 {
14292 w->window_end_bytepos
14293 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14294 w->window_end_pos
14295 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14296 w->window_end_vpos
14297 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14298 }
14299 else
14300 {
14301 /* This window must be completely empty. */
14302 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14303 w->window_end_pos = make_number (Z - ZV);
14304 w->window_end_vpos = make_number (0);
14305 }
14306 w->window_end_valid = Qnil;
14307
14308 /* Update hint: don't try scrolling again in update_window. */
14309 w->desired_matrix->no_scrolling_p = 1;
14310
14311 #if GLYPH_DEBUG
14312 debug_method_add (w, "try_window_reusing_current_matrix 1");
14313 #endif
14314 return 1;
14315 }
14316 else if (CHARPOS (new_start) > CHARPOS (start))
14317 {
14318 struct glyph_row *pt_row, *row;
14319 struct glyph_row *first_reusable_row;
14320 struct glyph_row *first_row_to_display;
14321 int dy;
14322 int yb = window_text_bottom_y (w);
14323
14324 /* Find the row starting at new_start, if there is one. Don't
14325 reuse a partially visible line at the end. */
14326 first_reusable_row = start_row;
14327 while (first_reusable_row->enabled_p
14328 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14329 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14330 < CHARPOS (new_start)))
14331 ++first_reusable_row;
14332
14333 /* Give up if there is no row to reuse. */
14334 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14335 || !first_reusable_row->enabled_p
14336 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14337 != CHARPOS (new_start)))
14338 return 0;
14339
14340 /* We can reuse fully visible rows beginning with
14341 first_reusable_row to the end of the window. Set
14342 first_row_to_display to the first row that cannot be reused.
14343 Set pt_row to the row containing point, if there is any. */
14344 pt_row = NULL;
14345 for (first_row_to_display = first_reusable_row;
14346 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14347 ++first_row_to_display)
14348 {
14349 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14350 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14351 pt_row = first_row_to_display;
14352 }
14353
14354 /* Start displaying at the start of first_row_to_display. */
14355 xassert (first_row_to_display->y < yb);
14356 init_to_row_start (&it, w, first_row_to_display);
14357
14358 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14359 - start_vpos);
14360 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14361 - nrows_scrolled);
14362 it.current_y = (first_row_to_display->y - first_reusable_row->y
14363 + WINDOW_HEADER_LINE_HEIGHT (w));
14364
14365 /* Display lines beginning with first_row_to_display in the
14366 desired matrix. Set last_text_row to the last row displayed
14367 that displays text. */
14368 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14369 if (pt_row == NULL)
14370 w->cursor.vpos = -1;
14371 last_text_row = NULL;
14372 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14373 if (display_line (&it))
14374 last_text_row = it.glyph_row - 1;
14375
14376 /* If point is in a reused row, adjust y and vpos of the cursor
14377 position. */
14378 if (pt_row)
14379 {
14380 w->cursor.vpos -= nrows_scrolled;
14381 w->cursor.y -= first_reusable_row->y - start_row->y;
14382 }
14383
14384 /* Give up if point isn't in a row displayed or reused. (This
14385 also handles the case where w->cursor.vpos < nrows_scrolled
14386 after the calls to display_line, which can happen with scroll
14387 margins. See bug#1295.) */
14388 if (w->cursor.vpos < 0)
14389 {
14390 clear_glyph_matrix (w->desired_matrix);
14391 return 0;
14392 }
14393
14394 /* Scroll the display. */
14395 run.current_y = first_reusable_row->y;
14396 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14397 run.height = it.last_visible_y - run.current_y;
14398 dy = run.current_y - run.desired_y;
14399
14400 if (run.height)
14401 {
14402 update_begin (f);
14403 FRAME_RIF (f)->update_window_begin_hook (w);
14404 FRAME_RIF (f)->clear_window_mouse_face (w);
14405 FRAME_RIF (f)->scroll_run_hook (w, &run);
14406 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14407 update_end (f);
14408 }
14409
14410 /* Adjust Y positions of reused rows. */
14411 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14412 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14413 max_y = it.last_visible_y;
14414 for (row = first_reusable_row; row < first_row_to_display; ++row)
14415 {
14416 row->y -= dy;
14417 row->visible_height = row->height;
14418 if (row->y < min_y)
14419 row->visible_height -= min_y - row->y;
14420 if (row->y + row->height > max_y)
14421 row->visible_height -= row->y + row->height - max_y;
14422 row->redraw_fringe_bitmaps_p = 1;
14423 }
14424
14425 /* Scroll the current matrix. */
14426 xassert (nrows_scrolled > 0);
14427 rotate_matrix (w->current_matrix,
14428 start_vpos,
14429 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14430 -nrows_scrolled);
14431
14432 /* Disable rows not reused. */
14433 for (row -= nrows_scrolled; row < bottom_row; ++row)
14434 row->enabled_p = 0;
14435
14436 /* Point may have moved to a different line, so we cannot assume that
14437 the previous cursor position is valid; locate the correct row. */
14438 if (pt_row)
14439 {
14440 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14441 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14442 row++)
14443 {
14444 w->cursor.vpos++;
14445 w->cursor.y = row->y;
14446 }
14447 if (row < bottom_row)
14448 {
14449 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14450 struct glyph *end = glyph + row->used[TEXT_AREA];
14451
14452 for (; glyph < end
14453 && (!BUFFERP (glyph->object)
14454 || glyph->charpos < PT);
14455 glyph++)
14456 {
14457 w->cursor.hpos++;
14458 w->cursor.x += glyph->pixel_width;
14459 }
14460 }
14461 }
14462
14463 /* Adjust window end. A null value of last_text_row means that
14464 the window end is in reused rows which in turn means that
14465 only its vpos can have changed. */
14466 if (last_text_row)
14467 {
14468 w->window_end_bytepos
14469 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14470 w->window_end_pos
14471 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14472 w->window_end_vpos
14473 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14474 }
14475 else
14476 {
14477 w->window_end_vpos
14478 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14479 }
14480
14481 w->window_end_valid = Qnil;
14482 w->desired_matrix->no_scrolling_p = 1;
14483
14484 #if GLYPH_DEBUG
14485 debug_method_add (w, "try_window_reusing_current_matrix 2");
14486 #endif
14487 return 1;
14488 }
14489
14490 return 0;
14491 }
14492
14493
14494 \f
14495 /************************************************************************
14496 Window redisplay reusing current matrix when buffer has changed
14497 ************************************************************************/
14498
14499 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14500 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14501 int *, int *));
14502 static struct glyph_row *
14503 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14504 struct glyph_row *));
14505
14506
14507 /* Return the last row in MATRIX displaying text. If row START is
14508 non-null, start searching with that row. IT gives the dimensions
14509 of the display. Value is null if matrix is empty; otherwise it is
14510 a pointer to the row found. */
14511
14512 static struct glyph_row *
14513 find_last_row_displaying_text (matrix, it, start)
14514 struct glyph_matrix *matrix;
14515 struct it *it;
14516 struct glyph_row *start;
14517 {
14518 struct glyph_row *row, *row_found;
14519
14520 /* Set row_found to the last row in IT->w's current matrix
14521 displaying text. The loop looks funny but think of partially
14522 visible lines. */
14523 row_found = NULL;
14524 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14525 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14526 {
14527 xassert (row->enabled_p);
14528 row_found = row;
14529 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14530 break;
14531 ++row;
14532 }
14533
14534 return row_found;
14535 }
14536
14537
14538 /* Return the last row in the current matrix of W that is not affected
14539 by changes at the start of current_buffer that occurred since W's
14540 current matrix was built. Value is null if no such row exists.
14541
14542 BEG_UNCHANGED us the number of characters unchanged at the start of
14543 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14544 first changed character in current_buffer. Characters at positions <
14545 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14546 when the current matrix was built. */
14547
14548 static struct glyph_row *
14549 find_last_unchanged_at_beg_row (w)
14550 struct window *w;
14551 {
14552 int first_changed_pos = BEG + BEG_UNCHANGED;
14553 struct glyph_row *row;
14554 struct glyph_row *row_found = NULL;
14555 int yb = window_text_bottom_y (w);
14556
14557 /* Find the last row displaying unchanged text. */
14558 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14559 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14560 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14561 ++row)
14562 {
14563 if (/* If row ends before first_changed_pos, it is unchanged,
14564 except in some case. */
14565 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14566 /* When row ends in ZV and we write at ZV it is not
14567 unchanged. */
14568 && !row->ends_at_zv_p
14569 /* When first_changed_pos is the end of a continued line,
14570 row is not unchanged because it may be no longer
14571 continued. */
14572 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14573 && (row->continued_p
14574 || row->exact_window_width_line_p)))
14575 row_found = row;
14576
14577 /* Stop if last visible row. */
14578 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14579 break;
14580 }
14581
14582 return row_found;
14583 }
14584
14585
14586 /* Find the first glyph row in the current matrix of W that is not
14587 affected by changes at the end of current_buffer since the
14588 time W's current matrix was built.
14589
14590 Return in *DELTA the number of chars by which buffer positions in
14591 unchanged text at the end of current_buffer must be adjusted.
14592
14593 Return in *DELTA_BYTES the corresponding number of bytes.
14594
14595 Value is null if no such row exists, i.e. all rows are affected by
14596 changes. */
14597
14598 static struct glyph_row *
14599 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14600 struct window *w;
14601 int *delta, *delta_bytes;
14602 {
14603 struct glyph_row *row;
14604 struct glyph_row *row_found = NULL;
14605
14606 *delta = *delta_bytes = 0;
14607
14608 /* Display must not have been paused, otherwise the current matrix
14609 is not up to date. */
14610 eassert (!NILP (w->window_end_valid));
14611
14612 /* A value of window_end_pos >= END_UNCHANGED means that the window
14613 end is in the range of changed text. If so, there is no
14614 unchanged row at the end of W's current matrix. */
14615 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14616 return NULL;
14617
14618 /* Set row to the last row in W's current matrix displaying text. */
14619 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14620
14621 /* If matrix is entirely empty, no unchanged row exists. */
14622 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14623 {
14624 /* The value of row is the last glyph row in the matrix having a
14625 meaningful buffer position in it. The end position of row
14626 corresponds to window_end_pos. This allows us to translate
14627 buffer positions in the current matrix to current buffer
14628 positions for characters not in changed text. */
14629 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14630 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14631 int last_unchanged_pos, last_unchanged_pos_old;
14632 struct glyph_row *first_text_row
14633 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14634
14635 *delta = Z - Z_old;
14636 *delta_bytes = Z_BYTE - Z_BYTE_old;
14637
14638 /* Set last_unchanged_pos to the buffer position of the last
14639 character in the buffer that has not been changed. Z is the
14640 index + 1 of the last character in current_buffer, i.e. by
14641 subtracting END_UNCHANGED we get the index of the last
14642 unchanged character, and we have to add BEG to get its buffer
14643 position. */
14644 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14645 last_unchanged_pos_old = last_unchanged_pos - *delta;
14646
14647 /* Search backward from ROW for a row displaying a line that
14648 starts at a minimum position >= last_unchanged_pos_old. */
14649 for (; row > first_text_row; --row)
14650 {
14651 /* This used to abort, but it can happen.
14652 It is ok to just stop the search instead here. KFS. */
14653 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14654 break;
14655
14656 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14657 row_found = row;
14658 }
14659 }
14660
14661 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14662
14663 return row_found;
14664 }
14665
14666
14667 /* Make sure that glyph rows in the current matrix of window W
14668 reference the same glyph memory as corresponding rows in the
14669 frame's frame matrix. This function is called after scrolling W's
14670 current matrix on a terminal frame in try_window_id and
14671 try_window_reusing_current_matrix. */
14672
14673 static void
14674 sync_frame_with_window_matrix_rows (w)
14675 struct window *w;
14676 {
14677 struct frame *f = XFRAME (w->frame);
14678 struct glyph_row *window_row, *window_row_end, *frame_row;
14679
14680 /* Preconditions: W must be a leaf window and full-width. Its frame
14681 must have a frame matrix. */
14682 xassert (NILP (w->hchild) && NILP (w->vchild));
14683 xassert (WINDOW_FULL_WIDTH_P (w));
14684 xassert (!FRAME_WINDOW_P (f));
14685
14686 /* If W is a full-width window, glyph pointers in W's current matrix
14687 have, by definition, to be the same as glyph pointers in the
14688 corresponding frame matrix. Note that frame matrices have no
14689 marginal areas (see build_frame_matrix). */
14690 window_row = w->current_matrix->rows;
14691 window_row_end = window_row + w->current_matrix->nrows;
14692 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14693 while (window_row < window_row_end)
14694 {
14695 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14696 struct glyph *end = window_row->glyphs[LAST_AREA];
14697
14698 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14699 frame_row->glyphs[TEXT_AREA] = start;
14700 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14701 frame_row->glyphs[LAST_AREA] = end;
14702
14703 /* Disable frame rows whose corresponding window rows have
14704 been disabled in try_window_id. */
14705 if (!window_row->enabled_p)
14706 frame_row->enabled_p = 0;
14707
14708 ++window_row, ++frame_row;
14709 }
14710 }
14711
14712
14713 /* Find the glyph row in window W containing CHARPOS. Consider all
14714 rows between START and END (not inclusive). END null means search
14715 all rows to the end of the display area of W. Value is the row
14716 containing CHARPOS or null. */
14717
14718 struct glyph_row *
14719 row_containing_pos (w, charpos, start, end, dy)
14720 struct window *w;
14721 int charpos;
14722 struct glyph_row *start, *end;
14723 int dy;
14724 {
14725 struct glyph_row *row = start;
14726 int last_y;
14727
14728 /* If we happen to start on a header-line, skip that. */
14729 if (row->mode_line_p)
14730 ++row;
14731
14732 if ((end && row >= end) || !row->enabled_p)
14733 return NULL;
14734
14735 last_y = window_text_bottom_y (w) - dy;
14736
14737 while (1)
14738 {
14739 /* Give up if we have gone too far. */
14740 if (end && row >= end)
14741 return NULL;
14742 /* This formerly returned if they were equal.
14743 I think that both quantities are of a "last plus one" type;
14744 if so, when they are equal, the row is within the screen. -- rms. */
14745 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14746 return NULL;
14747
14748 /* If it is in this row, return this row. */
14749 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14750 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14751 /* The end position of a row equals the start
14752 position of the next row. If CHARPOS is there, we
14753 would rather display it in the next line, except
14754 when this line ends in ZV. */
14755 && !row->ends_at_zv_p
14756 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14757 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14758 return row;
14759 ++row;
14760 }
14761 }
14762
14763
14764 /* Try to redisplay window W by reusing its existing display. W's
14765 current matrix must be up to date when this function is called,
14766 i.e. window_end_valid must not be nil.
14767
14768 Value is
14769
14770 1 if display has been updated
14771 0 if otherwise unsuccessful
14772 -1 if redisplay with same window start is known not to succeed
14773
14774 The following steps are performed:
14775
14776 1. Find the last row in the current matrix of W that is not
14777 affected by changes at the start of current_buffer. If no such row
14778 is found, give up.
14779
14780 2. Find the first row in W's current matrix that is not affected by
14781 changes at the end of current_buffer. Maybe there is no such row.
14782
14783 3. Display lines beginning with the row + 1 found in step 1 to the
14784 row found in step 2 or, if step 2 didn't find a row, to the end of
14785 the window.
14786
14787 4. If cursor is not known to appear on the window, give up.
14788
14789 5. If display stopped at the row found in step 2, scroll the
14790 display and current matrix as needed.
14791
14792 6. Maybe display some lines at the end of W, if we must. This can
14793 happen under various circumstances, like a partially visible line
14794 becoming fully visible, or because newly displayed lines are displayed
14795 in smaller font sizes.
14796
14797 7. Update W's window end information. */
14798
14799 static int
14800 try_window_id (w)
14801 struct window *w;
14802 {
14803 struct frame *f = XFRAME (w->frame);
14804 struct glyph_matrix *current_matrix = w->current_matrix;
14805 struct glyph_matrix *desired_matrix = w->desired_matrix;
14806 struct glyph_row *last_unchanged_at_beg_row;
14807 struct glyph_row *first_unchanged_at_end_row;
14808 struct glyph_row *row;
14809 struct glyph_row *bottom_row;
14810 int bottom_vpos;
14811 struct it it;
14812 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14813 struct text_pos start_pos;
14814 struct run run;
14815 int first_unchanged_at_end_vpos = 0;
14816 struct glyph_row *last_text_row, *last_text_row_at_end;
14817 struct text_pos start;
14818 int first_changed_charpos, last_changed_charpos;
14819
14820 #if GLYPH_DEBUG
14821 if (inhibit_try_window_id)
14822 return 0;
14823 #endif
14824
14825 /* This is handy for debugging. */
14826 #if 0
14827 #define GIVE_UP(X) \
14828 do { \
14829 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14830 return 0; \
14831 } while (0)
14832 #else
14833 #define GIVE_UP(X) return 0
14834 #endif
14835
14836 SET_TEXT_POS_FROM_MARKER (start, w->start);
14837
14838 /* Don't use this for mini-windows because these can show
14839 messages and mini-buffers, and we don't handle that here. */
14840 if (MINI_WINDOW_P (w))
14841 GIVE_UP (1);
14842
14843 /* This flag is used to prevent redisplay optimizations. */
14844 if (windows_or_buffers_changed || cursor_type_changed)
14845 GIVE_UP (2);
14846
14847 /* Verify that narrowing has not changed.
14848 Also verify that we were not told to prevent redisplay optimizations.
14849 It would be nice to further
14850 reduce the number of cases where this prevents try_window_id. */
14851 if (current_buffer->clip_changed
14852 || current_buffer->prevent_redisplay_optimizations_p)
14853 GIVE_UP (3);
14854
14855 /* Window must either use window-based redisplay or be full width. */
14856 if (!FRAME_WINDOW_P (f)
14857 && (!FRAME_LINE_INS_DEL_OK (f)
14858 || !WINDOW_FULL_WIDTH_P (w)))
14859 GIVE_UP (4);
14860
14861 /* Give up if point is not known NOT to appear in W. */
14862 if (PT < CHARPOS (start))
14863 GIVE_UP (5);
14864
14865 /* Another way to prevent redisplay optimizations. */
14866 if (XFASTINT (w->last_modified) == 0)
14867 GIVE_UP (6);
14868
14869 /* Verify that window is not hscrolled. */
14870 if (XFASTINT (w->hscroll) != 0)
14871 GIVE_UP (7);
14872
14873 /* Verify that display wasn't paused. */
14874 if (NILP (w->window_end_valid))
14875 GIVE_UP (8);
14876
14877 /* Can't use this if highlighting a region because a cursor movement
14878 will do more than just set the cursor. */
14879 if (!NILP (Vtransient_mark_mode)
14880 && !NILP (current_buffer->mark_active))
14881 GIVE_UP (9);
14882
14883 /* Likewise if highlighting trailing whitespace. */
14884 if (!NILP (Vshow_trailing_whitespace))
14885 GIVE_UP (11);
14886
14887 /* Likewise if showing a region. */
14888 if (!NILP (w->region_showing))
14889 GIVE_UP (10);
14890
14891 /* Can use this if overlay arrow position and or string have changed. */
14892 if (overlay_arrows_changed_p ())
14893 GIVE_UP (12);
14894
14895 /* When word-wrap is on, adding a space to the first word of a
14896 wrapped line can change the wrap position, altering the line
14897 above it. It might be worthwhile to handle this more
14898 intelligently, but for now just redisplay from scratch. */
14899 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14900 GIVE_UP (21);
14901
14902 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14903 only if buffer has really changed. The reason is that the gap is
14904 initially at Z for freshly visited files. The code below would
14905 set end_unchanged to 0 in that case. */
14906 if (MODIFF > SAVE_MODIFF
14907 /* This seems to happen sometimes after saving a buffer. */
14908 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14909 {
14910 if (GPT - BEG < BEG_UNCHANGED)
14911 BEG_UNCHANGED = GPT - BEG;
14912 if (Z - GPT < END_UNCHANGED)
14913 END_UNCHANGED = Z - GPT;
14914 }
14915
14916 /* The position of the first and last character that has been changed. */
14917 first_changed_charpos = BEG + BEG_UNCHANGED;
14918 last_changed_charpos = Z - END_UNCHANGED;
14919
14920 /* If window starts after a line end, and the last change is in
14921 front of that newline, then changes don't affect the display.
14922 This case happens with stealth-fontification. Note that although
14923 the display is unchanged, glyph positions in the matrix have to
14924 be adjusted, of course. */
14925 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14926 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14927 && ((last_changed_charpos < CHARPOS (start)
14928 && CHARPOS (start) == BEGV)
14929 || (last_changed_charpos < CHARPOS (start) - 1
14930 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14931 {
14932 int Z_old, delta, Z_BYTE_old, delta_bytes;
14933 struct glyph_row *r0;
14934
14935 /* Compute how many chars/bytes have been added to or removed
14936 from the buffer. */
14937 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14938 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14939 delta = Z - Z_old;
14940 delta_bytes = Z_BYTE - Z_BYTE_old;
14941
14942 /* Give up if PT is not in the window. Note that it already has
14943 been checked at the start of try_window_id that PT is not in
14944 front of the window start. */
14945 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14946 GIVE_UP (13);
14947
14948 /* If window start is unchanged, we can reuse the whole matrix
14949 as is, after adjusting glyph positions. No need to compute
14950 the window end again, since its offset from Z hasn't changed. */
14951 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14952 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14953 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14954 /* PT must not be in a partially visible line. */
14955 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14956 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14957 {
14958 /* Adjust positions in the glyph matrix. */
14959 if (delta || delta_bytes)
14960 {
14961 struct glyph_row *r1
14962 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14963 increment_matrix_positions (w->current_matrix,
14964 MATRIX_ROW_VPOS (r0, current_matrix),
14965 MATRIX_ROW_VPOS (r1, current_matrix),
14966 delta, delta_bytes);
14967 }
14968
14969 /* Set the cursor. */
14970 row = row_containing_pos (w, PT, r0, NULL, 0);
14971 if (row)
14972 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14973 else
14974 abort ();
14975 return 1;
14976 }
14977 }
14978
14979 /* Handle the case that changes are all below what is displayed in
14980 the window, and that PT is in the window. This shortcut cannot
14981 be taken if ZV is visible in the window, and text has been added
14982 there that is visible in the window. */
14983 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14984 /* ZV is not visible in the window, or there are no
14985 changes at ZV, actually. */
14986 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14987 || first_changed_charpos == last_changed_charpos))
14988 {
14989 struct glyph_row *r0;
14990
14991 /* Give up if PT is not in the window. Note that it already has
14992 been checked at the start of try_window_id that PT is not in
14993 front of the window start. */
14994 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14995 GIVE_UP (14);
14996
14997 /* If window start is unchanged, we can reuse the whole matrix
14998 as is, without changing glyph positions since no text has
14999 been added/removed in front of the window end. */
15000 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15001 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15002 /* PT must not be in a partially visible line. */
15003 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15004 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15005 {
15006 /* We have to compute the window end anew since text
15007 can have been added/removed after it. */
15008 w->window_end_pos
15009 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15010 w->window_end_bytepos
15011 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15012
15013 /* Set the cursor. */
15014 row = row_containing_pos (w, PT, r0, NULL, 0);
15015 if (row)
15016 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15017 else
15018 abort ();
15019 return 2;
15020 }
15021 }
15022
15023 /* Give up if window start is in the changed area.
15024
15025 The condition used to read
15026
15027 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15028
15029 but why that was tested escapes me at the moment. */
15030 if (CHARPOS (start) >= first_changed_charpos
15031 && CHARPOS (start) <= last_changed_charpos)
15032 GIVE_UP (15);
15033
15034 /* Check that window start agrees with the start of the first glyph
15035 row in its current matrix. Check this after we know the window
15036 start is not in changed text, otherwise positions would not be
15037 comparable. */
15038 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15039 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15040 GIVE_UP (16);
15041
15042 /* Give up if the window ends in strings. Overlay strings
15043 at the end are difficult to handle, so don't try. */
15044 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15045 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15046 GIVE_UP (20);
15047
15048 /* Compute the position at which we have to start displaying new
15049 lines. Some of the lines at the top of the window might be
15050 reusable because they are not displaying changed text. Find the
15051 last row in W's current matrix not affected by changes at the
15052 start of current_buffer. Value is null if changes start in the
15053 first line of window. */
15054 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15055 if (last_unchanged_at_beg_row)
15056 {
15057 /* Avoid starting to display in the moddle of a character, a TAB
15058 for instance. This is easier than to set up the iterator
15059 exactly, and it's not a frequent case, so the additional
15060 effort wouldn't really pay off. */
15061 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15062 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15063 && last_unchanged_at_beg_row > w->current_matrix->rows)
15064 --last_unchanged_at_beg_row;
15065
15066 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15067 GIVE_UP (17);
15068
15069 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15070 GIVE_UP (18);
15071 start_pos = it.current.pos;
15072
15073 /* Start displaying new lines in the desired matrix at the same
15074 vpos we would use in the current matrix, i.e. below
15075 last_unchanged_at_beg_row. */
15076 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15077 current_matrix);
15078 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15079 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15080
15081 xassert (it.hpos == 0 && it.current_x == 0);
15082 }
15083 else
15084 {
15085 /* There are no reusable lines at the start of the window.
15086 Start displaying in the first text line. */
15087 start_display (&it, w, start);
15088 it.vpos = it.first_vpos;
15089 start_pos = it.current.pos;
15090 }
15091
15092 /* Find the first row that is not affected by changes at the end of
15093 the buffer. Value will be null if there is no unchanged row, in
15094 which case we must redisplay to the end of the window. delta
15095 will be set to the value by which buffer positions beginning with
15096 first_unchanged_at_end_row have to be adjusted due to text
15097 changes. */
15098 first_unchanged_at_end_row
15099 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15100 IF_DEBUG (debug_delta = delta);
15101 IF_DEBUG (debug_delta_bytes = delta_bytes);
15102
15103 /* Set stop_pos to the buffer position up to which we will have to
15104 display new lines. If first_unchanged_at_end_row != NULL, this
15105 is the buffer position of the start of the line displayed in that
15106 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15107 that we don't stop at a buffer position. */
15108 stop_pos = 0;
15109 if (first_unchanged_at_end_row)
15110 {
15111 xassert (last_unchanged_at_beg_row == NULL
15112 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15113
15114 /* If this is a continuation line, move forward to the next one
15115 that isn't. Changes in lines above affect this line.
15116 Caution: this may move first_unchanged_at_end_row to a row
15117 not displaying text. */
15118 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15119 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15120 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15121 < it.last_visible_y))
15122 ++first_unchanged_at_end_row;
15123
15124 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15125 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15126 >= it.last_visible_y))
15127 first_unchanged_at_end_row = NULL;
15128 else
15129 {
15130 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15131 + delta);
15132 first_unchanged_at_end_vpos
15133 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15134 xassert (stop_pos >= Z - END_UNCHANGED);
15135 }
15136 }
15137 else if (last_unchanged_at_beg_row == NULL)
15138 GIVE_UP (19);
15139
15140
15141 #if GLYPH_DEBUG
15142
15143 /* Either there is no unchanged row at the end, or the one we have
15144 now displays text. This is a necessary condition for the window
15145 end pos calculation at the end of this function. */
15146 xassert (first_unchanged_at_end_row == NULL
15147 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15148
15149 debug_last_unchanged_at_beg_vpos
15150 = (last_unchanged_at_beg_row
15151 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15152 : -1);
15153 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15154
15155 #endif /* GLYPH_DEBUG != 0 */
15156
15157
15158 /* Display new lines. Set last_text_row to the last new line
15159 displayed which has text on it, i.e. might end up as being the
15160 line where the window_end_vpos is. */
15161 w->cursor.vpos = -1;
15162 last_text_row = NULL;
15163 overlay_arrow_seen = 0;
15164 while (it.current_y < it.last_visible_y
15165 && !fonts_changed_p
15166 && (first_unchanged_at_end_row == NULL
15167 || IT_CHARPOS (it) < stop_pos))
15168 {
15169 if (display_line (&it))
15170 last_text_row = it.glyph_row - 1;
15171 }
15172
15173 if (fonts_changed_p)
15174 return -1;
15175
15176
15177 /* Compute differences in buffer positions, y-positions etc. for
15178 lines reused at the bottom of the window. Compute what we can
15179 scroll. */
15180 if (first_unchanged_at_end_row
15181 /* No lines reused because we displayed everything up to the
15182 bottom of the window. */
15183 && it.current_y < it.last_visible_y)
15184 {
15185 dvpos = (it.vpos
15186 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15187 current_matrix));
15188 dy = it.current_y - first_unchanged_at_end_row->y;
15189 run.current_y = first_unchanged_at_end_row->y;
15190 run.desired_y = run.current_y + dy;
15191 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15192 }
15193 else
15194 {
15195 delta = delta_bytes = dvpos = dy
15196 = run.current_y = run.desired_y = run.height = 0;
15197 first_unchanged_at_end_row = NULL;
15198 }
15199 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15200
15201
15202 /* Find the cursor if not already found. We have to decide whether
15203 PT will appear on this window (it sometimes doesn't, but this is
15204 not a very frequent case.) This decision has to be made before
15205 the current matrix is altered. A value of cursor.vpos < 0 means
15206 that PT is either in one of the lines beginning at
15207 first_unchanged_at_end_row or below the window. Don't care for
15208 lines that might be displayed later at the window end; as
15209 mentioned, this is not a frequent case. */
15210 if (w->cursor.vpos < 0)
15211 {
15212 /* Cursor in unchanged rows at the top? */
15213 if (PT < CHARPOS (start_pos)
15214 && last_unchanged_at_beg_row)
15215 {
15216 row = row_containing_pos (w, PT,
15217 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15218 last_unchanged_at_beg_row + 1, 0);
15219 if (row)
15220 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15221 }
15222
15223 /* Start from first_unchanged_at_end_row looking for PT. */
15224 else if (first_unchanged_at_end_row)
15225 {
15226 row = row_containing_pos (w, PT - delta,
15227 first_unchanged_at_end_row, NULL, 0);
15228 if (row)
15229 set_cursor_from_row (w, row, w->current_matrix, delta,
15230 delta_bytes, dy, dvpos);
15231 }
15232
15233 /* Give up if cursor was not found. */
15234 if (w->cursor.vpos < 0)
15235 {
15236 clear_glyph_matrix (w->desired_matrix);
15237 return -1;
15238 }
15239 }
15240
15241 /* Don't let the cursor end in the scroll margins. */
15242 {
15243 int this_scroll_margin, cursor_height;
15244
15245 this_scroll_margin = max (0, scroll_margin);
15246 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15247 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15248 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15249
15250 if ((w->cursor.y < this_scroll_margin
15251 && CHARPOS (start) > BEGV)
15252 /* Old redisplay didn't take scroll margin into account at the bottom,
15253 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15254 || (w->cursor.y + (make_cursor_line_fully_visible_p
15255 ? cursor_height + this_scroll_margin
15256 : 1)) > it.last_visible_y)
15257 {
15258 w->cursor.vpos = -1;
15259 clear_glyph_matrix (w->desired_matrix);
15260 return -1;
15261 }
15262 }
15263
15264 /* Scroll the display. Do it before changing the current matrix so
15265 that xterm.c doesn't get confused about where the cursor glyph is
15266 found. */
15267 if (dy && run.height)
15268 {
15269 update_begin (f);
15270
15271 if (FRAME_WINDOW_P (f))
15272 {
15273 FRAME_RIF (f)->update_window_begin_hook (w);
15274 FRAME_RIF (f)->clear_window_mouse_face (w);
15275 FRAME_RIF (f)->scroll_run_hook (w, &run);
15276 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15277 }
15278 else
15279 {
15280 /* Terminal frame. In this case, dvpos gives the number of
15281 lines to scroll by; dvpos < 0 means scroll up. */
15282 int first_unchanged_at_end_vpos
15283 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15284 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15285 int end = (WINDOW_TOP_EDGE_LINE (w)
15286 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15287 + window_internal_height (w));
15288
15289 /* Perform the operation on the screen. */
15290 if (dvpos > 0)
15291 {
15292 /* Scroll last_unchanged_at_beg_row to the end of the
15293 window down dvpos lines. */
15294 set_terminal_window (f, end);
15295
15296 /* On dumb terminals delete dvpos lines at the end
15297 before inserting dvpos empty lines. */
15298 if (!FRAME_SCROLL_REGION_OK (f))
15299 ins_del_lines (f, end - dvpos, -dvpos);
15300
15301 /* Insert dvpos empty lines in front of
15302 last_unchanged_at_beg_row. */
15303 ins_del_lines (f, from, dvpos);
15304 }
15305 else if (dvpos < 0)
15306 {
15307 /* Scroll up last_unchanged_at_beg_vpos to the end of
15308 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15309 set_terminal_window (f, end);
15310
15311 /* Delete dvpos lines in front of
15312 last_unchanged_at_beg_vpos. ins_del_lines will set
15313 the cursor to the given vpos and emit |dvpos| delete
15314 line sequences. */
15315 ins_del_lines (f, from + dvpos, dvpos);
15316
15317 /* On a dumb terminal insert dvpos empty lines at the
15318 end. */
15319 if (!FRAME_SCROLL_REGION_OK (f))
15320 ins_del_lines (f, end + dvpos, -dvpos);
15321 }
15322
15323 set_terminal_window (f, 0);
15324 }
15325
15326 update_end (f);
15327 }
15328
15329 /* Shift reused rows of the current matrix to the right position.
15330 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15331 text. */
15332 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15333 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15334 if (dvpos < 0)
15335 {
15336 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15337 bottom_vpos, dvpos);
15338 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15339 bottom_vpos, 0);
15340 }
15341 else if (dvpos > 0)
15342 {
15343 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15344 bottom_vpos, dvpos);
15345 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15346 first_unchanged_at_end_vpos + dvpos, 0);
15347 }
15348
15349 /* For frame-based redisplay, make sure that current frame and window
15350 matrix are in sync with respect to glyph memory. */
15351 if (!FRAME_WINDOW_P (f))
15352 sync_frame_with_window_matrix_rows (w);
15353
15354 /* Adjust buffer positions in reused rows. */
15355 if (delta || delta_bytes)
15356 increment_matrix_positions (current_matrix,
15357 first_unchanged_at_end_vpos + dvpos,
15358 bottom_vpos, delta, delta_bytes);
15359
15360 /* Adjust Y positions. */
15361 if (dy)
15362 shift_glyph_matrix (w, current_matrix,
15363 first_unchanged_at_end_vpos + dvpos,
15364 bottom_vpos, dy);
15365
15366 if (first_unchanged_at_end_row)
15367 {
15368 first_unchanged_at_end_row += dvpos;
15369 if (first_unchanged_at_end_row->y >= it.last_visible_y
15370 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15371 first_unchanged_at_end_row = NULL;
15372 }
15373
15374 /* If scrolling up, there may be some lines to display at the end of
15375 the window. */
15376 last_text_row_at_end = NULL;
15377 if (dy < 0)
15378 {
15379 /* Scrolling up can leave for example a partially visible line
15380 at the end of the window to be redisplayed. */
15381 /* Set last_row to the glyph row in the current matrix where the
15382 window end line is found. It has been moved up or down in
15383 the matrix by dvpos. */
15384 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15385 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15386
15387 /* If last_row is the window end line, it should display text. */
15388 xassert (last_row->displays_text_p);
15389
15390 /* If window end line was partially visible before, begin
15391 displaying at that line. Otherwise begin displaying with the
15392 line following it. */
15393 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15394 {
15395 init_to_row_start (&it, w, last_row);
15396 it.vpos = last_vpos;
15397 it.current_y = last_row->y;
15398 }
15399 else
15400 {
15401 init_to_row_end (&it, w, last_row);
15402 it.vpos = 1 + last_vpos;
15403 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15404 ++last_row;
15405 }
15406
15407 /* We may start in a continuation line. If so, we have to
15408 get the right continuation_lines_width and current_x. */
15409 it.continuation_lines_width = last_row->continuation_lines_width;
15410 it.hpos = it.current_x = 0;
15411
15412 /* Display the rest of the lines at the window end. */
15413 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15414 while (it.current_y < it.last_visible_y
15415 && !fonts_changed_p)
15416 {
15417 /* Is it always sure that the display agrees with lines in
15418 the current matrix? I don't think so, so we mark rows
15419 displayed invalid in the current matrix by setting their
15420 enabled_p flag to zero. */
15421 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15422 if (display_line (&it))
15423 last_text_row_at_end = it.glyph_row - 1;
15424 }
15425 }
15426
15427 /* Update window_end_pos and window_end_vpos. */
15428 if (first_unchanged_at_end_row
15429 && !last_text_row_at_end)
15430 {
15431 /* Window end line if one of the preserved rows from the current
15432 matrix. Set row to the last row displaying text in current
15433 matrix starting at first_unchanged_at_end_row, after
15434 scrolling. */
15435 xassert (first_unchanged_at_end_row->displays_text_p);
15436 row = find_last_row_displaying_text (w->current_matrix, &it,
15437 first_unchanged_at_end_row);
15438 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15439
15440 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15441 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15442 w->window_end_vpos
15443 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15444 xassert (w->window_end_bytepos >= 0);
15445 IF_DEBUG (debug_method_add (w, "A"));
15446 }
15447 else if (last_text_row_at_end)
15448 {
15449 w->window_end_pos
15450 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15451 w->window_end_bytepos
15452 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15453 w->window_end_vpos
15454 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15455 xassert (w->window_end_bytepos >= 0);
15456 IF_DEBUG (debug_method_add (w, "B"));
15457 }
15458 else if (last_text_row)
15459 {
15460 /* We have displayed either to the end of the window or at the
15461 end of the window, i.e. the last row with text is to be found
15462 in the desired matrix. */
15463 w->window_end_pos
15464 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15465 w->window_end_bytepos
15466 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15467 w->window_end_vpos
15468 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15469 xassert (w->window_end_bytepos >= 0);
15470 }
15471 else if (first_unchanged_at_end_row == NULL
15472 && last_text_row == NULL
15473 && last_text_row_at_end == NULL)
15474 {
15475 /* Displayed to end of window, but no line containing text was
15476 displayed. Lines were deleted at the end of the window. */
15477 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15478 int vpos = XFASTINT (w->window_end_vpos);
15479 struct glyph_row *current_row = current_matrix->rows + vpos;
15480 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15481
15482 for (row = NULL;
15483 row == NULL && vpos >= first_vpos;
15484 --vpos, --current_row, --desired_row)
15485 {
15486 if (desired_row->enabled_p)
15487 {
15488 if (desired_row->displays_text_p)
15489 row = desired_row;
15490 }
15491 else if (current_row->displays_text_p)
15492 row = current_row;
15493 }
15494
15495 xassert (row != NULL);
15496 w->window_end_vpos = make_number (vpos + 1);
15497 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15498 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15499 xassert (w->window_end_bytepos >= 0);
15500 IF_DEBUG (debug_method_add (w, "C"));
15501 }
15502 else
15503 abort ();
15504
15505 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15506 debug_end_vpos = XFASTINT (w->window_end_vpos));
15507
15508 /* Record that display has not been completed. */
15509 w->window_end_valid = Qnil;
15510 w->desired_matrix->no_scrolling_p = 1;
15511 return 3;
15512
15513 #undef GIVE_UP
15514 }
15515
15516
15517 \f
15518 /***********************************************************************
15519 More debugging support
15520 ***********************************************************************/
15521
15522 #if GLYPH_DEBUG
15523
15524 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15525 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15526 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15527
15528
15529 /* Dump the contents of glyph matrix MATRIX on stderr.
15530
15531 GLYPHS 0 means don't show glyph contents.
15532 GLYPHS 1 means show glyphs in short form
15533 GLYPHS > 1 means show glyphs in long form. */
15534
15535 void
15536 dump_glyph_matrix (matrix, glyphs)
15537 struct glyph_matrix *matrix;
15538 int glyphs;
15539 {
15540 int i;
15541 for (i = 0; i < matrix->nrows; ++i)
15542 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15543 }
15544
15545
15546 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15547 the glyph row and area where the glyph comes from. */
15548
15549 void
15550 dump_glyph (row, glyph, area)
15551 struct glyph_row *row;
15552 struct glyph *glyph;
15553 int area;
15554 {
15555 if (glyph->type == CHAR_GLYPH)
15556 {
15557 fprintf (stderr,
15558 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15559 glyph - row->glyphs[TEXT_AREA],
15560 'C',
15561 glyph->charpos,
15562 (BUFFERP (glyph->object)
15563 ? 'B'
15564 : (STRINGP (glyph->object)
15565 ? 'S'
15566 : '-')),
15567 glyph->pixel_width,
15568 glyph->u.ch,
15569 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15570 ? glyph->u.ch
15571 : '.'),
15572 glyph->face_id,
15573 glyph->left_box_line_p,
15574 glyph->right_box_line_p);
15575 }
15576 else if (glyph->type == STRETCH_GLYPH)
15577 {
15578 fprintf (stderr,
15579 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15580 glyph - row->glyphs[TEXT_AREA],
15581 'S',
15582 glyph->charpos,
15583 (BUFFERP (glyph->object)
15584 ? 'B'
15585 : (STRINGP (glyph->object)
15586 ? 'S'
15587 : '-')),
15588 glyph->pixel_width,
15589 0,
15590 '.',
15591 glyph->face_id,
15592 glyph->left_box_line_p,
15593 glyph->right_box_line_p);
15594 }
15595 else if (glyph->type == IMAGE_GLYPH)
15596 {
15597 fprintf (stderr,
15598 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15599 glyph - row->glyphs[TEXT_AREA],
15600 'I',
15601 glyph->charpos,
15602 (BUFFERP (glyph->object)
15603 ? 'B'
15604 : (STRINGP (glyph->object)
15605 ? 'S'
15606 : '-')),
15607 glyph->pixel_width,
15608 glyph->u.img_id,
15609 '.',
15610 glyph->face_id,
15611 glyph->left_box_line_p,
15612 glyph->right_box_line_p);
15613 }
15614 else if (glyph->type == COMPOSITE_GLYPH)
15615 {
15616 fprintf (stderr,
15617 " %5d %4c %6d %c %3d 0x%05x",
15618 glyph - row->glyphs[TEXT_AREA],
15619 '+',
15620 glyph->charpos,
15621 (BUFFERP (glyph->object)
15622 ? 'B'
15623 : (STRINGP (glyph->object)
15624 ? 'S'
15625 : '-')),
15626 glyph->pixel_width,
15627 glyph->u.cmp.id);
15628 if (glyph->u.cmp.automatic)
15629 fprintf (stderr,
15630 "[%d-%d]",
15631 glyph->u.cmp.from, glyph->u.cmp.to);
15632 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15633 glyph->face_id,
15634 glyph->left_box_line_p,
15635 glyph->right_box_line_p);
15636 }
15637 }
15638
15639
15640 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15641 GLYPHS 0 means don't show glyph contents.
15642 GLYPHS 1 means show glyphs in short form
15643 GLYPHS > 1 means show glyphs in long form. */
15644
15645 void
15646 dump_glyph_row (row, vpos, glyphs)
15647 struct glyph_row *row;
15648 int vpos, glyphs;
15649 {
15650 if (glyphs != 1)
15651 {
15652 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15653 fprintf (stderr, "======================================================================\n");
15654
15655 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15656 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15657 vpos,
15658 MATRIX_ROW_START_CHARPOS (row),
15659 MATRIX_ROW_END_CHARPOS (row),
15660 row->used[TEXT_AREA],
15661 row->contains_overlapping_glyphs_p,
15662 row->enabled_p,
15663 row->truncated_on_left_p,
15664 row->truncated_on_right_p,
15665 row->continued_p,
15666 MATRIX_ROW_CONTINUATION_LINE_P (row),
15667 row->displays_text_p,
15668 row->ends_at_zv_p,
15669 row->fill_line_p,
15670 row->ends_in_middle_of_char_p,
15671 row->starts_in_middle_of_char_p,
15672 row->mouse_face_p,
15673 row->x,
15674 row->y,
15675 row->pixel_width,
15676 row->height,
15677 row->visible_height,
15678 row->ascent,
15679 row->phys_ascent);
15680 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15681 row->end.overlay_string_index,
15682 row->continuation_lines_width);
15683 fprintf (stderr, "%9d %5d\n",
15684 CHARPOS (row->start.string_pos),
15685 CHARPOS (row->end.string_pos));
15686 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15687 row->end.dpvec_index);
15688 }
15689
15690 if (glyphs > 1)
15691 {
15692 int area;
15693
15694 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15695 {
15696 struct glyph *glyph = row->glyphs[area];
15697 struct glyph *glyph_end = glyph + row->used[area];
15698
15699 /* Glyph for a line end in text. */
15700 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15701 ++glyph_end;
15702
15703 if (glyph < glyph_end)
15704 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15705
15706 for (; glyph < glyph_end; ++glyph)
15707 dump_glyph (row, glyph, area);
15708 }
15709 }
15710 else if (glyphs == 1)
15711 {
15712 int area;
15713
15714 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15715 {
15716 char *s = (char *) alloca (row->used[area] + 1);
15717 int i;
15718
15719 for (i = 0; i < row->used[area]; ++i)
15720 {
15721 struct glyph *glyph = row->glyphs[area] + i;
15722 if (glyph->type == CHAR_GLYPH
15723 && glyph->u.ch < 0x80
15724 && glyph->u.ch >= ' ')
15725 s[i] = glyph->u.ch;
15726 else
15727 s[i] = '.';
15728 }
15729
15730 s[i] = '\0';
15731 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15732 }
15733 }
15734 }
15735
15736
15737 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15738 Sdump_glyph_matrix, 0, 1, "p",
15739 doc: /* Dump the current matrix of the selected window to stderr.
15740 Shows contents of glyph row structures. With non-nil
15741 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15742 glyphs in short form, otherwise show glyphs in long form. */)
15743 (glyphs)
15744 Lisp_Object glyphs;
15745 {
15746 struct window *w = XWINDOW (selected_window);
15747 struct buffer *buffer = XBUFFER (w->buffer);
15748
15749 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15750 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15751 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15752 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15753 fprintf (stderr, "=============================================\n");
15754 dump_glyph_matrix (w->current_matrix,
15755 NILP (glyphs) ? 0 : XINT (glyphs));
15756 return Qnil;
15757 }
15758
15759
15760 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15761 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15762 ()
15763 {
15764 struct frame *f = XFRAME (selected_frame);
15765 dump_glyph_matrix (f->current_matrix, 1);
15766 return Qnil;
15767 }
15768
15769
15770 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15771 doc: /* Dump glyph row ROW to stderr.
15772 GLYPH 0 means don't dump glyphs.
15773 GLYPH 1 means dump glyphs in short form.
15774 GLYPH > 1 or omitted means dump glyphs in long form. */)
15775 (row, glyphs)
15776 Lisp_Object row, glyphs;
15777 {
15778 struct glyph_matrix *matrix;
15779 int vpos;
15780
15781 CHECK_NUMBER (row);
15782 matrix = XWINDOW (selected_window)->current_matrix;
15783 vpos = XINT (row);
15784 if (vpos >= 0 && vpos < matrix->nrows)
15785 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15786 vpos,
15787 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15788 return Qnil;
15789 }
15790
15791
15792 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15793 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15794 GLYPH 0 means don't dump glyphs.
15795 GLYPH 1 means dump glyphs in short form.
15796 GLYPH > 1 or omitted means dump glyphs in long form. */)
15797 (row, glyphs)
15798 Lisp_Object row, glyphs;
15799 {
15800 struct frame *sf = SELECTED_FRAME ();
15801 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15802 int vpos;
15803
15804 CHECK_NUMBER (row);
15805 vpos = XINT (row);
15806 if (vpos >= 0 && vpos < m->nrows)
15807 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15808 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15809 return Qnil;
15810 }
15811
15812
15813 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15814 doc: /* Toggle tracing of redisplay.
15815 With ARG, turn tracing on if and only if ARG is positive. */)
15816 (arg)
15817 Lisp_Object arg;
15818 {
15819 if (NILP (arg))
15820 trace_redisplay_p = !trace_redisplay_p;
15821 else
15822 {
15823 arg = Fprefix_numeric_value (arg);
15824 trace_redisplay_p = XINT (arg) > 0;
15825 }
15826
15827 return Qnil;
15828 }
15829
15830
15831 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15832 doc: /* Like `format', but print result to stderr.
15833 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15834 (nargs, args)
15835 int nargs;
15836 Lisp_Object *args;
15837 {
15838 Lisp_Object s = Fformat (nargs, args);
15839 fprintf (stderr, "%s", SDATA (s));
15840 return Qnil;
15841 }
15842
15843 #endif /* GLYPH_DEBUG */
15844
15845
15846 \f
15847 /***********************************************************************
15848 Building Desired Matrix Rows
15849 ***********************************************************************/
15850
15851 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15852 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15853
15854 static struct glyph_row *
15855 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15856 struct window *w;
15857 Lisp_Object overlay_arrow_string;
15858 {
15859 struct frame *f = XFRAME (WINDOW_FRAME (w));
15860 struct buffer *buffer = XBUFFER (w->buffer);
15861 struct buffer *old = current_buffer;
15862 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15863 int arrow_len = SCHARS (overlay_arrow_string);
15864 const unsigned char *arrow_end = arrow_string + arrow_len;
15865 const unsigned char *p;
15866 struct it it;
15867 int multibyte_p;
15868 int n_glyphs_before;
15869
15870 set_buffer_temp (buffer);
15871 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15872 it.glyph_row->used[TEXT_AREA] = 0;
15873 SET_TEXT_POS (it.position, 0, 0);
15874
15875 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15876 p = arrow_string;
15877 while (p < arrow_end)
15878 {
15879 Lisp_Object face, ilisp;
15880
15881 /* Get the next character. */
15882 if (multibyte_p)
15883 it.c = string_char_and_length (p, arrow_len, &it.len);
15884 else
15885 it.c = *p, it.len = 1;
15886 p += it.len;
15887
15888 /* Get its face. */
15889 ilisp = make_number (p - arrow_string);
15890 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15891 it.face_id = compute_char_face (f, it.c, face);
15892
15893 /* Compute its width, get its glyphs. */
15894 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15895 SET_TEXT_POS (it.position, -1, -1);
15896 PRODUCE_GLYPHS (&it);
15897
15898 /* If this character doesn't fit any more in the line, we have
15899 to remove some glyphs. */
15900 if (it.current_x > it.last_visible_x)
15901 {
15902 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15903 break;
15904 }
15905 }
15906
15907 set_buffer_temp (old);
15908 return it.glyph_row;
15909 }
15910
15911
15912 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15913 glyphs are only inserted for terminal frames since we can't really
15914 win with truncation glyphs when partially visible glyphs are
15915 involved. Which glyphs to insert is determined by
15916 produce_special_glyphs. */
15917
15918 static void
15919 insert_left_trunc_glyphs (it)
15920 struct it *it;
15921 {
15922 struct it truncate_it;
15923 struct glyph *from, *end, *to, *toend;
15924
15925 xassert (!FRAME_WINDOW_P (it->f));
15926
15927 /* Get the truncation glyphs. */
15928 truncate_it = *it;
15929 truncate_it.current_x = 0;
15930 truncate_it.face_id = DEFAULT_FACE_ID;
15931 truncate_it.glyph_row = &scratch_glyph_row;
15932 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15933 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15934 truncate_it.object = make_number (0);
15935 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15936
15937 /* Overwrite glyphs from IT with truncation glyphs. */
15938 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15939 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15940 to = it->glyph_row->glyphs[TEXT_AREA];
15941 toend = to + it->glyph_row->used[TEXT_AREA];
15942
15943 while (from < end)
15944 *to++ = *from++;
15945
15946 /* There may be padding glyphs left over. Overwrite them too. */
15947 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15948 {
15949 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15950 while (from < end)
15951 *to++ = *from++;
15952 }
15953
15954 if (to > toend)
15955 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15956 }
15957
15958
15959 /* Compute the pixel height and width of IT->glyph_row.
15960
15961 Most of the time, ascent and height of a display line will be equal
15962 to the max_ascent and max_height values of the display iterator
15963 structure. This is not the case if
15964
15965 1. We hit ZV without displaying anything. In this case, max_ascent
15966 and max_height will be zero.
15967
15968 2. We have some glyphs that don't contribute to the line height.
15969 (The glyph row flag contributes_to_line_height_p is for future
15970 pixmap extensions).
15971
15972 The first case is easily covered by using default values because in
15973 these cases, the line height does not really matter, except that it
15974 must not be zero. */
15975
15976 static void
15977 compute_line_metrics (it)
15978 struct it *it;
15979 {
15980 struct glyph_row *row = it->glyph_row;
15981 int area, i;
15982
15983 if (FRAME_WINDOW_P (it->f))
15984 {
15985 int i, min_y, max_y;
15986
15987 /* The line may consist of one space only, that was added to
15988 place the cursor on it. If so, the row's height hasn't been
15989 computed yet. */
15990 if (row->height == 0)
15991 {
15992 if (it->max_ascent + it->max_descent == 0)
15993 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15994 row->ascent = it->max_ascent;
15995 row->height = it->max_ascent + it->max_descent;
15996 row->phys_ascent = it->max_phys_ascent;
15997 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15998 row->extra_line_spacing = it->max_extra_line_spacing;
15999 }
16000
16001 /* Compute the width of this line. */
16002 row->pixel_width = row->x;
16003 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16004 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16005
16006 xassert (row->pixel_width >= 0);
16007 xassert (row->ascent >= 0 && row->height > 0);
16008
16009 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16010 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16011
16012 /* If first line's physical ascent is larger than its logical
16013 ascent, use the physical ascent, and make the row taller.
16014 This makes accented characters fully visible. */
16015 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16016 && row->phys_ascent > row->ascent)
16017 {
16018 row->height += row->phys_ascent - row->ascent;
16019 row->ascent = row->phys_ascent;
16020 }
16021
16022 /* Compute how much of the line is visible. */
16023 row->visible_height = row->height;
16024
16025 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16026 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16027
16028 if (row->y < min_y)
16029 row->visible_height -= min_y - row->y;
16030 if (row->y + row->height > max_y)
16031 row->visible_height -= row->y + row->height - max_y;
16032 }
16033 else
16034 {
16035 row->pixel_width = row->used[TEXT_AREA];
16036 if (row->continued_p)
16037 row->pixel_width -= it->continuation_pixel_width;
16038 else if (row->truncated_on_right_p)
16039 row->pixel_width -= it->truncation_pixel_width;
16040 row->ascent = row->phys_ascent = 0;
16041 row->height = row->phys_height = row->visible_height = 1;
16042 row->extra_line_spacing = 0;
16043 }
16044
16045 /* Compute a hash code for this row. */
16046 row->hash = 0;
16047 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16048 for (i = 0; i < row->used[area]; ++i)
16049 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16050 + row->glyphs[area][i].u.val
16051 + row->glyphs[area][i].face_id
16052 + row->glyphs[area][i].padding_p
16053 + (row->glyphs[area][i].type << 2));
16054
16055 it->max_ascent = it->max_descent = 0;
16056 it->max_phys_ascent = it->max_phys_descent = 0;
16057 }
16058
16059
16060 /* Append one space to the glyph row of iterator IT if doing a
16061 window-based redisplay. The space has the same face as
16062 IT->face_id. Value is non-zero if a space was added.
16063
16064 This function is called to make sure that there is always one glyph
16065 at the end of a glyph row that the cursor can be set on under
16066 window-systems. (If there weren't such a glyph we would not know
16067 how wide and tall a box cursor should be displayed).
16068
16069 At the same time this space let's a nicely handle clearing to the
16070 end of the line if the row ends in italic text. */
16071
16072 static int
16073 append_space_for_newline (it, default_face_p)
16074 struct it *it;
16075 int default_face_p;
16076 {
16077 if (FRAME_WINDOW_P (it->f))
16078 {
16079 int n = it->glyph_row->used[TEXT_AREA];
16080
16081 if (it->glyph_row->glyphs[TEXT_AREA] + n
16082 < it->glyph_row->glyphs[1 + TEXT_AREA])
16083 {
16084 /* Save some values that must not be changed.
16085 Must save IT->c and IT->len because otherwise
16086 ITERATOR_AT_END_P wouldn't work anymore after
16087 append_space_for_newline has been called. */
16088 enum display_element_type saved_what = it->what;
16089 int saved_c = it->c, saved_len = it->len;
16090 int saved_x = it->current_x;
16091 int saved_face_id = it->face_id;
16092 struct text_pos saved_pos;
16093 Lisp_Object saved_object;
16094 struct face *face;
16095
16096 saved_object = it->object;
16097 saved_pos = it->position;
16098
16099 it->what = IT_CHARACTER;
16100 bzero (&it->position, sizeof it->position);
16101 it->object = make_number (0);
16102 it->c = ' ';
16103 it->len = 1;
16104
16105 if (default_face_p)
16106 it->face_id = DEFAULT_FACE_ID;
16107 else if (it->face_before_selective_p)
16108 it->face_id = it->saved_face_id;
16109 face = FACE_FROM_ID (it->f, it->face_id);
16110 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16111
16112 PRODUCE_GLYPHS (it);
16113
16114 it->override_ascent = -1;
16115 it->constrain_row_ascent_descent_p = 0;
16116 it->current_x = saved_x;
16117 it->object = saved_object;
16118 it->position = saved_pos;
16119 it->what = saved_what;
16120 it->face_id = saved_face_id;
16121 it->len = saved_len;
16122 it->c = saved_c;
16123 return 1;
16124 }
16125 }
16126
16127 return 0;
16128 }
16129
16130
16131 /* Extend the face of the last glyph in the text area of IT->glyph_row
16132 to the end of the display line. Called from display_line.
16133 If the glyph row is empty, add a space glyph to it so that we
16134 know the face to draw. Set the glyph row flag fill_line_p. */
16135
16136 static void
16137 extend_face_to_end_of_line (it)
16138 struct it *it;
16139 {
16140 struct face *face;
16141 struct frame *f = it->f;
16142
16143 /* If line is already filled, do nothing. */
16144 if (it->current_x >= it->last_visible_x)
16145 return;
16146
16147 /* Face extension extends the background and box of IT->face_id
16148 to the end of the line. If the background equals the background
16149 of the frame, we don't have to do anything. */
16150 if (it->face_before_selective_p)
16151 face = FACE_FROM_ID (it->f, it->saved_face_id);
16152 else
16153 face = FACE_FROM_ID (f, it->face_id);
16154
16155 if (FRAME_WINDOW_P (f)
16156 && it->glyph_row->displays_text_p
16157 && face->box == FACE_NO_BOX
16158 && face->background == FRAME_BACKGROUND_PIXEL (f)
16159 && !face->stipple)
16160 return;
16161
16162 /* Set the glyph row flag indicating that the face of the last glyph
16163 in the text area has to be drawn to the end of the text area. */
16164 it->glyph_row->fill_line_p = 1;
16165
16166 /* If current character of IT is not ASCII, make sure we have the
16167 ASCII face. This will be automatically undone the next time
16168 get_next_display_element returns a multibyte character. Note
16169 that the character will always be single byte in unibyte text. */
16170 if (!ASCII_CHAR_P (it->c))
16171 {
16172 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16173 }
16174
16175 if (FRAME_WINDOW_P (f))
16176 {
16177 /* If the row is empty, add a space with the current face of IT,
16178 so that we know which face to draw. */
16179 if (it->glyph_row->used[TEXT_AREA] == 0)
16180 {
16181 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16182 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16183 it->glyph_row->used[TEXT_AREA] = 1;
16184 }
16185 }
16186 else
16187 {
16188 /* Save some values that must not be changed. */
16189 int saved_x = it->current_x;
16190 struct text_pos saved_pos;
16191 Lisp_Object saved_object;
16192 enum display_element_type saved_what = it->what;
16193 int saved_face_id = it->face_id;
16194
16195 saved_object = it->object;
16196 saved_pos = it->position;
16197
16198 it->what = IT_CHARACTER;
16199 bzero (&it->position, sizeof it->position);
16200 it->object = make_number (0);
16201 it->c = ' ';
16202 it->len = 1;
16203 it->face_id = face->id;
16204
16205 PRODUCE_GLYPHS (it);
16206
16207 while (it->current_x <= it->last_visible_x)
16208 PRODUCE_GLYPHS (it);
16209
16210 /* Don't count these blanks really. It would let us insert a left
16211 truncation glyph below and make us set the cursor on them, maybe. */
16212 it->current_x = saved_x;
16213 it->object = saved_object;
16214 it->position = saved_pos;
16215 it->what = saved_what;
16216 it->face_id = saved_face_id;
16217 }
16218 }
16219
16220
16221 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16222 trailing whitespace. */
16223
16224 static int
16225 trailing_whitespace_p (charpos)
16226 int charpos;
16227 {
16228 int bytepos = CHAR_TO_BYTE (charpos);
16229 int c = 0;
16230
16231 while (bytepos < ZV_BYTE
16232 && (c = FETCH_CHAR (bytepos),
16233 c == ' ' || c == '\t'))
16234 ++bytepos;
16235
16236 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16237 {
16238 if (bytepos != PT_BYTE)
16239 return 1;
16240 }
16241 return 0;
16242 }
16243
16244
16245 /* Highlight trailing whitespace, if any, in ROW. */
16246
16247 void
16248 highlight_trailing_whitespace (f, row)
16249 struct frame *f;
16250 struct glyph_row *row;
16251 {
16252 int used = row->used[TEXT_AREA];
16253
16254 if (used)
16255 {
16256 struct glyph *start = row->glyphs[TEXT_AREA];
16257 struct glyph *glyph = start + used - 1;
16258
16259 /* Skip over glyphs inserted to display the cursor at the
16260 end of a line, for extending the face of the last glyph
16261 to the end of the line on terminals, and for truncation
16262 and continuation glyphs. */
16263 while (glyph >= start
16264 && glyph->type == CHAR_GLYPH
16265 && INTEGERP (glyph->object))
16266 --glyph;
16267
16268 /* If last glyph is a space or stretch, and it's trailing
16269 whitespace, set the face of all trailing whitespace glyphs in
16270 IT->glyph_row to `trailing-whitespace'. */
16271 if (glyph >= start
16272 && BUFFERP (glyph->object)
16273 && (glyph->type == STRETCH_GLYPH
16274 || (glyph->type == CHAR_GLYPH
16275 && glyph->u.ch == ' '))
16276 && trailing_whitespace_p (glyph->charpos))
16277 {
16278 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16279 if (face_id < 0)
16280 return;
16281
16282 while (glyph >= start
16283 && BUFFERP (glyph->object)
16284 && (glyph->type == STRETCH_GLYPH
16285 || (glyph->type == CHAR_GLYPH
16286 && glyph->u.ch == ' ')))
16287 (glyph--)->face_id = face_id;
16288 }
16289 }
16290 }
16291
16292
16293 /* Value is non-zero if glyph row ROW in window W should be
16294 used to hold the cursor. */
16295
16296 static int
16297 cursor_row_p (w, row)
16298 struct window *w;
16299 struct glyph_row *row;
16300 {
16301 int cursor_row_p = 1;
16302
16303 if (PT == MATRIX_ROW_END_CHARPOS (row))
16304 {
16305 /* Suppose the row ends on a string.
16306 Unless the row is continued, that means it ends on a newline
16307 in the string. If it's anything other than a display string
16308 (e.g. a before-string from an overlay), we don't want the
16309 cursor there. (This heuristic seems to give the optimal
16310 behavior for the various types of multi-line strings.) */
16311 if (CHARPOS (row->end.string_pos) >= 0)
16312 {
16313 if (row->continued_p)
16314 cursor_row_p = 1;
16315 else
16316 {
16317 /* Check for `display' property. */
16318 struct glyph *beg = row->glyphs[TEXT_AREA];
16319 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16320 struct glyph *glyph;
16321
16322 cursor_row_p = 0;
16323 for (glyph = end; glyph >= beg; --glyph)
16324 if (STRINGP (glyph->object))
16325 {
16326 Lisp_Object prop
16327 = Fget_char_property (make_number (PT),
16328 Qdisplay, Qnil);
16329 cursor_row_p =
16330 (!NILP (prop)
16331 && display_prop_string_p (prop, glyph->object));
16332 break;
16333 }
16334 }
16335 }
16336 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16337 {
16338 /* If the row ends in middle of a real character,
16339 and the line is continued, we want the cursor here.
16340 That's because MATRIX_ROW_END_CHARPOS would equal
16341 PT if PT is before the character. */
16342 if (!row->ends_in_ellipsis_p)
16343 cursor_row_p = row->continued_p;
16344 else
16345 /* If the row ends in an ellipsis, then
16346 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16347 We want that position to be displayed after the ellipsis. */
16348 cursor_row_p = 0;
16349 }
16350 /* If the row ends at ZV, display the cursor at the end of that
16351 row instead of at the start of the row below. */
16352 else if (row->ends_at_zv_p)
16353 cursor_row_p = 1;
16354 else
16355 cursor_row_p = 0;
16356 }
16357
16358 return cursor_row_p;
16359 }
16360
16361 \f
16362
16363 /* Push the display property PROP so that it will be rendered at the
16364 current position in IT. */
16365
16366 static void
16367 push_display_prop (struct it *it, Lisp_Object prop)
16368 {
16369 push_it (it);
16370
16371 /* Never display a cursor on the prefix. */
16372 it->avoid_cursor_p = 1;
16373
16374 if (STRINGP (prop))
16375 {
16376 if (SCHARS (prop) == 0)
16377 {
16378 pop_it (it);
16379 return;
16380 }
16381
16382 it->string = prop;
16383 it->multibyte_p = STRING_MULTIBYTE (it->string);
16384 it->current.overlay_string_index = -1;
16385 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16386 it->end_charpos = it->string_nchars = SCHARS (it->string);
16387 it->method = GET_FROM_STRING;
16388 it->stop_charpos = 0;
16389 }
16390 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16391 {
16392 it->method = GET_FROM_STRETCH;
16393 it->object = prop;
16394 }
16395 #ifdef HAVE_WINDOW_SYSTEM
16396 else if (IMAGEP (prop))
16397 {
16398 it->what = IT_IMAGE;
16399 it->image_id = lookup_image (it->f, prop);
16400 it->method = GET_FROM_IMAGE;
16401 }
16402 #endif /* HAVE_WINDOW_SYSTEM */
16403 else
16404 {
16405 pop_it (it); /* bogus display property, give up */
16406 return;
16407 }
16408 }
16409
16410 /* Return the character-property PROP at the current position in IT. */
16411
16412 static Lisp_Object
16413 get_it_property (it, prop)
16414 struct it *it;
16415 Lisp_Object prop;
16416 {
16417 Lisp_Object position;
16418
16419 if (STRINGP (it->object))
16420 position = make_number (IT_STRING_CHARPOS (*it));
16421 else if (BUFFERP (it->object))
16422 position = make_number (IT_CHARPOS (*it));
16423 else
16424 return Qnil;
16425
16426 return Fget_char_property (position, prop, it->object);
16427 }
16428
16429 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16430
16431 static void
16432 handle_line_prefix (struct it *it)
16433 {
16434 Lisp_Object prefix;
16435 if (it->continuation_lines_width > 0)
16436 {
16437 prefix = get_it_property (it, Qwrap_prefix);
16438 if (NILP (prefix))
16439 prefix = Vwrap_prefix;
16440 }
16441 else
16442 {
16443 prefix = get_it_property (it, Qline_prefix);
16444 if (NILP (prefix))
16445 prefix = Vline_prefix;
16446 }
16447 if (! NILP (prefix))
16448 {
16449 push_display_prop (it, prefix);
16450 /* If the prefix is wider than the window, and we try to wrap
16451 it, it would acquire its own wrap prefix, and so on till the
16452 iterator stack overflows. So, don't wrap the prefix. */
16453 it->line_wrap = TRUNCATE;
16454 }
16455 }
16456
16457 \f
16458
16459 /* Construct the glyph row IT->glyph_row in the desired matrix of
16460 IT->w from text at the current position of IT. See dispextern.h
16461 for an overview of struct it. Value is non-zero if
16462 IT->glyph_row displays text, as opposed to a line displaying ZV
16463 only. */
16464
16465 static int
16466 display_line (it)
16467 struct it *it;
16468 {
16469 struct glyph_row *row = it->glyph_row;
16470 Lisp_Object overlay_arrow_string;
16471 struct it wrap_it;
16472 int may_wrap = 0, wrap_x;
16473 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16474 int wrap_row_phys_ascent, wrap_row_phys_height;
16475 int wrap_row_extra_line_spacing;
16476
16477 /* We always start displaying at hpos zero even if hscrolled. */
16478 xassert (it->hpos == 0 && it->current_x == 0);
16479
16480 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16481 >= it->w->desired_matrix->nrows)
16482 {
16483 it->w->nrows_scale_factor++;
16484 fonts_changed_p = 1;
16485 return 0;
16486 }
16487
16488 /* Is IT->w showing the region? */
16489 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16490
16491 /* Clear the result glyph row and enable it. */
16492 prepare_desired_row (row);
16493
16494 row->y = it->current_y;
16495 row->start = it->start;
16496 row->continuation_lines_width = it->continuation_lines_width;
16497 row->displays_text_p = 1;
16498 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16499 it->starts_in_middle_of_char_p = 0;
16500
16501 /* Arrange the overlays nicely for our purposes. Usually, we call
16502 display_line on only one line at a time, in which case this
16503 can't really hurt too much, or we call it on lines which appear
16504 one after another in the buffer, in which case all calls to
16505 recenter_overlay_lists but the first will be pretty cheap. */
16506 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16507
16508 /* Move over display elements that are not visible because we are
16509 hscrolled. This may stop at an x-position < IT->first_visible_x
16510 if the first glyph is partially visible or if we hit a line end. */
16511 if (it->current_x < it->first_visible_x)
16512 {
16513 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16514 MOVE_TO_POS | MOVE_TO_X);
16515 }
16516 else
16517 {
16518 /* We only do this when not calling `move_it_in_display_line_to'
16519 above, because move_it_in_display_line_to calls
16520 handle_line_prefix itself. */
16521 handle_line_prefix (it);
16522 }
16523
16524 /* Get the initial row height. This is either the height of the
16525 text hscrolled, if there is any, or zero. */
16526 row->ascent = it->max_ascent;
16527 row->height = it->max_ascent + it->max_descent;
16528 row->phys_ascent = it->max_phys_ascent;
16529 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16530 row->extra_line_spacing = it->max_extra_line_spacing;
16531
16532 /* Loop generating characters. The loop is left with IT on the next
16533 character to display. */
16534 while (1)
16535 {
16536 int n_glyphs_before, hpos_before, x_before;
16537 int x, i, nglyphs;
16538 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16539
16540 /* Retrieve the next thing to display. Value is zero if end of
16541 buffer reached. */
16542 if (!get_next_display_element (it))
16543 {
16544 /* Maybe add a space at the end of this line that is used to
16545 display the cursor there under X. Set the charpos of the
16546 first glyph of blank lines not corresponding to any text
16547 to -1. */
16548 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16549 row->exact_window_width_line_p = 1;
16550 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16551 || row->used[TEXT_AREA] == 0)
16552 {
16553 row->glyphs[TEXT_AREA]->charpos = -1;
16554 row->displays_text_p = 0;
16555
16556 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16557 && (!MINI_WINDOW_P (it->w)
16558 || (minibuf_level && EQ (it->window, minibuf_window))))
16559 row->indicate_empty_line_p = 1;
16560 }
16561
16562 it->continuation_lines_width = 0;
16563 row->ends_at_zv_p = 1;
16564 break;
16565 }
16566
16567 /* Now, get the metrics of what we want to display. This also
16568 generates glyphs in `row' (which is IT->glyph_row). */
16569 n_glyphs_before = row->used[TEXT_AREA];
16570 x = it->current_x;
16571
16572 /* Remember the line height so far in case the next element doesn't
16573 fit on the line. */
16574 if (it->line_wrap != TRUNCATE)
16575 {
16576 ascent = it->max_ascent;
16577 descent = it->max_descent;
16578 phys_ascent = it->max_phys_ascent;
16579 phys_descent = it->max_phys_descent;
16580
16581 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16582 {
16583 if (IT_DISPLAYING_WHITESPACE (it))
16584 may_wrap = 1;
16585 else if (may_wrap)
16586 {
16587 wrap_it = *it;
16588 wrap_x = x;
16589 wrap_row_used = row->used[TEXT_AREA];
16590 wrap_row_ascent = row->ascent;
16591 wrap_row_height = row->height;
16592 wrap_row_phys_ascent = row->phys_ascent;
16593 wrap_row_phys_height = row->phys_height;
16594 wrap_row_extra_line_spacing = row->extra_line_spacing;
16595 may_wrap = 0;
16596 }
16597 }
16598 }
16599
16600 PRODUCE_GLYPHS (it);
16601
16602 /* If this display element was in marginal areas, continue with
16603 the next one. */
16604 if (it->area != TEXT_AREA)
16605 {
16606 row->ascent = max (row->ascent, it->max_ascent);
16607 row->height = max (row->height, it->max_ascent + it->max_descent);
16608 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16609 row->phys_height = max (row->phys_height,
16610 it->max_phys_ascent + it->max_phys_descent);
16611 row->extra_line_spacing = max (row->extra_line_spacing,
16612 it->max_extra_line_spacing);
16613 set_iterator_to_next (it, 1);
16614 continue;
16615 }
16616
16617 /* Does the display element fit on the line? If we truncate
16618 lines, we should draw past the right edge of the window. If
16619 we don't truncate, we want to stop so that we can display the
16620 continuation glyph before the right margin. If lines are
16621 continued, there are two possible strategies for characters
16622 resulting in more than 1 glyph (e.g. tabs): Display as many
16623 glyphs as possible in this line and leave the rest for the
16624 continuation line, or display the whole element in the next
16625 line. Original redisplay did the former, so we do it also. */
16626 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16627 hpos_before = it->hpos;
16628 x_before = x;
16629
16630 if (/* Not a newline. */
16631 nglyphs > 0
16632 /* Glyphs produced fit entirely in the line. */
16633 && it->current_x < it->last_visible_x)
16634 {
16635 it->hpos += nglyphs;
16636 row->ascent = max (row->ascent, it->max_ascent);
16637 row->height = max (row->height, it->max_ascent + it->max_descent);
16638 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16639 row->phys_height = max (row->phys_height,
16640 it->max_phys_ascent + it->max_phys_descent);
16641 row->extra_line_spacing = max (row->extra_line_spacing,
16642 it->max_extra_line_spacing);
16643 if (it->current_x - it->pixel_width < it->first_visible_x)
16644 row->x = x - it->first_visible_x;
16645 }
16646 else
16647 {
16648 int new_x;
16649 struct glyph *glyph;
16650
16651 for (i = 0; i < nglyphs; ++i, x = new_x)
16652 {
16653 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16654 new_x = x + glyph->pixel_width;
16655
16656 if (/* Lines are continued. */
16657 it->line_wrap != TRUNCATE
16658 && (/* Glyph doesn't fit on the line. */
16659 new_x > it->last_visible_x
16660 /* Or it fits exactly on a window system frame. */
16661 || (new_x == it->last_visible_x
16662 && FRAME_WINDOW_P (it->f))))
16663 {
16664 /* End of a continued line. */
16665
16666 if (it->hpos == 0
16667 || (new_x == it->last_visible_x
16668 && FRAME_WINDOW_P (it->f)))
16669 {
16670 /* Current glyph is the only one on the line or
16671 fits exactly on the line. We must continue
16672 the line because we can't draw the cursor
16673 after the glyph. */
16674 row->continued_p = 1;
16675 it->current_x = new_x;
16676 it->continuation_lines_width += new_x;
16677 ++it->hpos;
16678 if (i == nglyphs - 1)
16679 {
16680 /* If line-wrap is on, check if a previous
16681 wrap point was found. */
16682 if (wrap_row_used > 0
16683 /* Even if there is a previous wrap
16684 point, continue the line here as
16685 usual, if (i) the previous character
16686 was a space or tab AND (ii) the
16687 current character is not. */
16688 && (!may_wrap
16689 || IT_DISPLAYING_WHITESPACE (it)))
16690 goto back_to_wrap;
16691
16692 set_iterator_to_next (it, 1);
16693 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16694 {
16695 if (!get_next_display_element (it))
16696 {
16697 row->exact_window_width_line_p = 1;
16698 it->continuation_lines_width = 0;
16699 row->continued_p = 0;
16700 row->ends_at_zv_p = 1;
16701 }
16702 else if (ITERATOR_AT_END_OF_LINE_P (it))
16703 {
16704 row->continued_p = 0;
16705 row->exact_window_width_line_p = 1;
16706 }
16707 }
16708 }
16709 }
16710 else if (CHAR_GLYPH_PADDING_P (*glyph)
16711 && !FRAME_WINDOW_P (it->f))
16712 {
16713 /* A padding glyph that doesn't fit on this line.
16714 This means the whole character doesn't fit
16715 on the line. */
16716 row->used[TEXT_AREA] = n_glyphs_before;
16717
16718 /* Fill the rest of the row with continuation
16719 glyphs like in 20.x. */
16720 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16721 < row->glyphs[1 + TEXT_AREA])
16722 produce_special_glyphs (it, IT_CONTINUATION);
16723
16724 row->continued_p = 1;
16725 it->current_x = x_before;
16726 it->continuation_lines_width += x_before;
16727
16728 /* Restore the height to what it was before the
16729 element not fitting on the line. */
16730 it->max_ascent = ascent;
16731 it->max_descent = descent;
16732 it->max_phys_ascent = phys_ascent;
16733 it->max_phys_descent = phys_descent;
16734 }
16735 else if (wrap_row_used > 0)
16736 {
16737 back_to_wrap:
16738 *it = wrap_it;
16739 it->continuation_lines_width += wrap_x;
16740 row->used[TEXT_AREA] = wrap_row_used;
16741 row->ascent = wrap_row_ascent;
16742 row->height = wrap_row_height;
16743 row->phys_ascent = wrap_row_phys_ascent;
16744 row->phys_height = wrap_row_phys_height;
16745 row->extra_line_spacing = wrap_row_extra_line_spacing;
16746 row->continued_p = 1;
16747 row->ends_at_zv_p = 0;
16748 row->exact_window_width_line_p = 0;
16749 it->continuation_lines_width += x;
16750
16751 /* Make sure that a non-default face is extended
16752 up to the right margin of the window. */
16753 extend_face_to_end_of_line (it);
16754 }
16755 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16756 {
16757 /* A TAB that extends past the right edge of the
16758 window. This produces a single glyph on
16759 window system frames. We leave the glyph in
16760 this row and let it fill the row, but don't
16761 consume the TAB. */
16762 it->continuation_lines_width += it->last_visible_x;
16763 row->ends_in_middle_of_char_p = 1;
16764 row->continued_p = 1;
16765 glyph->pixel_width = it->last_visible_x - x;
16766 it->starts_in_middle_of_char_p = 1;
16767 }
16768 else
16769 {
16770 /* Something other than a TAB that draws past
16771 the right edge of the window. Restore
16772 positions to values before the element. */
16773 row->used[TEXT_AREA] = n_glyphs_before + i;
16774
16775 /* Display continuation glyphs. */
16776 if (!FRAME_WINDOW_P (it->f))
16777 produce_special_glyphs (it, IT_CONTINUATION);
16778 row->continued_p = 1;
16779
16780 it->current_x = x_before;
16781 it->continuation_lines_width += x;
16782 extend_face_to_end_of_line (it);
16783
16784 if (nglyphs > 1 && i > 0)
16785 {
16786 row->ends_in_middle_of_char_p = 1;
16787 it->starts_in_middle_of_char_p = 1;
16788 }
16789
16790 /* Restore the height to what it was before the
16791 element not fitting on the line. */
16792 it->max_ascent = ascent;
16793 it->max_descent = descent;
16794 it->max_phys_ascent = phys_ascent;
16795 it->max_phys_descent = phys_descent;
16796 }
16797
16798 break;
16799 }
16800 else if (new_x > it->first_visible_x)
16801 {
16802 /* Increment number of glyphs actually displayed. */
16803 ++it->hpos;
16804
16805 if (x < it->first_visible_x)
16806 /* Glyph is partially visible, i.e. row starts at
16807 negative X position. */
16808 row->x = x - it->first_visible_x;
16809 }
16810 else
16811 {
16812 /* Glyph is completely off the left margin of the
16813 window. This should not happen because of the
16814 move_it_in_display_line at the start of this
16815 function, unless the text display area of the
16816 window is empty. */
16817 xassert (it->first_visible_x <= it->last_visible_x);
16818 }
16819 }
16820
16821 row->ascent = max (row->ascent, it->max_ascent);
16822 row->height = max (row->height, it->max_ascent + it->max_descent);
16823 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16824 row->phys_height = max (row->phys_height,
16825 it->max_phys_ascent + it->max_phys_descent);
16826 row->extra_line_spacing = max (row->extra_line_spacing,
16827 it->max_extra_line_spacing);
16828
16829 /* End of this display line if row is continued. */
16830 if (row->continued_p || row->ends_at_zv_p)
16831 break;
16832 }
16833
16834 at_end_of_line:
16835 /* Is this a line end? If yes, we're also done, after making
16836 sure that a non-default face is extended up to the right
16837 margin of the window. */
16838 if (ITERATOR_AT_END_OF_LINE_P (it))
16839 {
16840 int used_before = row->used[TEXT_AREA];
16841
16842 row->ends_in_newline_from_string_p = STRINGP (it->object);
16843
16844 /* Add a space at the end of the line that is used to
16845 display the cursor there. */
16846 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16847 append_space_for_newline (it, 0);
16848
16849 /* Extend the face to the end of the line. */
16850 extend_face_to_end_of_line (it);
16851
16852 /* Make sure we have the position. */
16853 if (used_before == 0)
16854 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16855
16856 /* Consume the line end. This skips over invisible lines. */
16857 set_iterator_to_next (it, 1);
16858 it->continuation_lines_width = 0;
16859 break;
16860 }
16861
16862 /* Proceed with next display element. Note that this skips
16863 over lines invisible because of selective display. */
16864 set_iterator_to_next (it, 1);
16865
16866 /* If we truncate lines, we are done when the last displayed
16867 glyphs reach past the right margin of the window. */
16868 if (it->line_wrap == TRUNCATE
16869 && (FRAME_WINDOW_P (it->f)
16870 ? (it->current_x >= it->last_visible_x)
16871 : (it->current_x > it->last_visible_x)))
16872 {
16873 /* Maybe add truncation glyphs. */
16874 if (!FRAME_WINDOW_P (it->f))
16875 {
16876 int i, n;
16877
16878 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16879 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16880 break;
16881
16882 for (n = row->used[TEXT_AREA]; i < n; ++i)
16883 {
16884 row->used[TEXT_AREA] = i;
16885 produce_special_glyphs (it, IT_TRUNCATION);
16886 }
16887 }
16888 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16889 {
16890 /* Don't truncate if we can overflow newline into fringe. */
16891 if (!get_next_display_element (it))
16892 {
16893 it->continuation_lines_width = 0;
16894 row->ends_at_zv_p = 1;
16895 row->exact_window_width_line_p = 1;
16896 break;
16897 }
16898 if (ITERATOR_AT_END_OF_LINE_P (it))
16899 {
16900 row->exact_window_width_line_p = 1;
16901 goto at_end_of_line;
16902 }
16903 }
16904
16905 row->truncated_on_right_p = 1;
16906 it->continuation_lines_width = 0;
16907 reseat_at_next_visible_line_start (it, 0);
16908 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16909 it->hpos = hpos_before;
16910 it->current_x = x_before;
16911 break;
16912 }
16913 }
16914
16915 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16916 at the left window margin. */
16917 if (it->first_visible_x
16918 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16919 {
16920 if (!FRAME_WINDOW_P (it->f))
16921 insert_left_trunc_glyphs (it);
16922 row->truncated_on_left_p = 1;
16923 }
16924
16925 /* If the start of this line is the overlay arrow-position, then
16926 mark this glyph row as the one containing the overlay arrow.
16927 This is clearly a mess with variable size fonts. It would be
16928 better to let it be displayed like cursors under X. */
16929 if ((row->displays_text_p || !overlay_arrow_seen)
16930 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16931 !NILP (overlay_arrow_string)))
16932 {
16933 /* Overlay arrow in window redisplay is a fringe bitmap. */
16934 if (STRINGP (overlay_arrow_string))
16935 {
16936 struct glyph_row *arrow_row
16937 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16938 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16939 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16940 struct glyph *p = row->glyphs[TEXT_AREA];
16941 struct glyph *p2, *end;
16942
16943 /* Copy the arrow glyphs. */
16944 while (glyph < arrow_end)
16945 *p++ = *glyph++;
16946
16947 /* Throw away padding glyphs. */
16948 p2 = p;
16949 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16950 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16951 ++p2;
16952 if (p2 > p)
16953 {
16954 while (p2 < end)
16955 *p++ = *p2++;
16956 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16957 }
16958 }
16959 else
16960 {
16961 xassert (INTEGERP (overlay_arrow_string));
16962 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16963 }
16964 overlay_arrow_seen = 1;
16965 }
16966
16967 /* Compute pixel dimensions of this line. */
16968 compute_line_metrics (it);
16969
16970 /* Remember the position at which this line ends. */
16971 row->end = it->current;
16972
16973 /* Record whether this row ends inside an ellipsis. */
16974 row->ends_in_ellipsis_p
16975 = (it->method == GET_FROM_DISPLAY_VECTOR
16976 && it->ellipsis_p);
16977
16978 /* Save fringe bitmaps in this row. */
16979 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16980 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16981 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16982 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16983
16984 it->left_user_fringe_bitmap = 0;
16985 it->left_user_fringe_face_id = 0;
16986 it->right_user_fringe_bitmap = 0;
16987 it->right_user_fringe_face_id = 0;
16988
16989 /* Maybe set the cursor. */
16990 if (it->w->cursor.vpos < 0
16991 && PT >= MATRIX_ROW_START_CHARPOS (row)
16992 && PT <= MATRIX_ROW_END_CHARPOS (row)
16993 && cursor_row_p (it->w, row))
16994 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16995
16996 /* Highlight trailing whitespace. */
16997 if (!NILP (Vshow_trailing_whitespace))
16998 highlight_trailing_whitespace (it->f, it->glyph_row);
16999
17000 /* Prepare for the next line. This line starts horizontally at (X
17001 HPOS) = (0 0). Vertical positions are incremented. As a
17002 convenience for the caller, IT->glyph_row is set to the next
17003 row to be used. */
17004 it->current_x = it->hpos = 0;
17005 it->current_y += row->height;
17006 ++it->vpos;
17007 ++it->glyph_row;
17008 it->start = it->current;
17009 return row->displays_text_p;
17010 }
17011
17012
17013 \f
17014 /***********************************************************************
17015 Menu Bar
17016 ***********************************************************************/
17017
17018 /* Redisplay the menu bar in the frame for window W.
17019
17020 The menu bar of X frames that don't have X toolkit support is
17021 displayed in a special window W->frame->menu_bar_window.
17022
17023 The menu bar of terminal frames is treated specially as far as
17024 glyph matrices are concerned. Menu bar lines are not part of
17025 windows, so the update is done directly on the frame matrix rows
17026 for the menu bar. */
17027
17028 static void
17029 display_menu_bar (w)
17030 struct window *w;
17031 {
17032 struct frame *f = XFRAME (WINDOW_FRAME (w));
17033 struct it it;
17034 Lisp_Object items;
17035 int i;
17036
17037 /* Don't do all this for graphical frames. */
17038 #ifdef HAVE_NTGUI
17039 if (FRAME_W32_P (f))
17040 return;
17041 #endif
17042 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17043 if (FRAME_X_P (f))
17044 return;
17045 #endif
17046
17047 #ifdef HAVE_NS
17048 if (FRAME_NS_P (f))
17049 return;
17050 #endif /* HAVE_NS */
17051
17052 #ifdef USE_X_TOOLKIT
17053 xassert (!FRAME_WINDOW_P (f));
17054 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17055 it.first_visible_x = 0;
17056 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17057 #else /* not USE_X_TOOLKIT */
17058 if (FRAME_WINDOW_P (f))
17059 {
17060 /* Menu bar lines are displayed in the desired matrix of the
17061 dummy window menu_bar_window. */
17062 struct window *menu_w;
17063 xassert (WINDOWP (f->menu_bar_window));
17064 menu_w = XWINDOW (f->menu_bar_window);
17065 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17066 MENU_FACE_ID);
17067 it.first_visible_x = 0;
17068 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17069 }
17070 else
17071 {
17072 /* This is a TTY frame, i.e. character hpos/vpos are used as
17073 pixel x/y. */
17074 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17075 MENU_FACE_ID);
17076 it.first_visible_x = 0;
17077 it.last_visible_x = FRAME_COLS (f);
17078 }
17079 #endif /* not USE_X_TOOLKIT */
17080
17081 if (! mode_line_inverse_video)
17082 /* Force the menu-bar to be displayed in the default face. */
17083 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17084
17085 /* Clear all rows of the menu bar. */
17086 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17087 {
17088 struct glyph_row *row = it.glyph_row + i;
17089 clear_glyph_row (row);
17090 row->enabled_p = 1;
17091 row->full_width_p = 1;
17092 }
17093
17094 /* Display all items of the menu bar. */
17095 items = FRAME_MENU_BAR_ITEMS (it.f);
17096 for (i = 0; i < XVECTOR (items)->size; i += 4)
17097 {
17098 Lisp_Object string;
17099
17100 /* Stop at nil string. */
17101 string = AREF (items, i + 1);
17102 if (NILP (string))
17103 break;
17104
17105 /* Remember where item was displayed. */
17106 ASET (items, i + 3, make_number (it.hpos));
17107
17108 /* Display the item, pad with one space. */
17109 if (it.current_x < it.last_visible_x)
17110 display_string (NULL, string, Qnil, 0, 0, &it,
17111 SCHARS (string) + 1, 0, 0, -1);
17112 }
17113
17114 /* Fill out the line with spaces. */
17115 if (it.current_x < it.last_visible_x)
17116 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17117
17118 /* Compute the total height of the lines. */
17119 compute_line_metrics (&it);
17120 }
17121
17122
17123 \f
17124 /***********************************************************************
17125 Mode Line
17126 ***********************************************************************/
17127
17128 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17129 FORCE is non-zero, redisplay mode lines unconditionally.
17130 Otherwise, redisplay only mode lines that are garbaged. Value is
17131 the number of windows whose mode lines were redisplayed. */
17132
17133 static int
17134 redisplay_mode_lines (window, force)
17135 Lisp_Object window;
17136 int force;
17137 {
17138 int nwindows = 0;
17139
17140 while (!NILP (window))
17141 {
17142 struct window *w = XWINDOW (window);
17143
17144 if (WINDOWP (w->hchild))
17145 nwindows += redisplay_mode_lines (w->hchild, force);
17146 else if (WINDOWP (w->vchild))
17147 nwindows += redisplay_mode_lines (w->vchild, force);
17148 else if (force
17149 || FRAME_GARBAGED_P (XFRAME (w->frame))
17150 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17151 {
17152 struct text_pos lpoint;
17153 struct buffer *old = current_buffer;
17154
17155 /* Set the window's buffer for the mode line display. */
17156 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17157 set_buffer_internal_1 (XBUFFER (w->buffer));
17158
17159 /* Point refers normally to the selected window. For any
17160 other window, set up appropriate value. */
17161 if (!EQ (window, selected_window))
17162 {
17163 struct text_pos pt;
17164
17165 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17166 if (CHARPOS (pt) < BEGV)
17167 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17168 else if (CHARPOS (pt) > (ZV - 1))
17169 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17170 else
17171 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17172 }
17173
17174 /* Display mode lines. */
17175 clear_glyph_matrix (w->desired_matrix);
17176 if (display_mode_lines (w))
17177 {
17178 ++nwindows;
17179 w->must_be_updated_p = 1;
17180 }
17181
17182 /* Restore old settings. */
17183 set_buffer_internal_1 (old);
17184 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17185 }
17186
17187 window = w->next;
17188 }
17189
17190 return nwindows;
17191 }
17192
17193
17194 /* Display the mode and/or top line of window W. Value is the number
17195 of mode lines displayed. */
17196
17197 static int
17198 display_mode_lines (w)
17199 struct window *w;
17200 {
17201 Lisp_Object old_selected_window, old_selected_frame;
17202 int n = 0;
17203
17204 old_selected_frame = selected_frame;
17205 selected_frame = w->frame;
17206 old_selected_window = selected_window;
17207 XSETWINDOW (selected_window, w);
17208
17209 /* These will be set while the mode line specs are processed. */
17210 line_number_displayed = 0;
17211 w->column_number_displayed = Qnil;
17212
17213 if (WINDOW_WANTS_MODELINE_P (w))
17214 {
17215 struct window *sel_w = XWINDOW (old_selected_window);
17216
17217 /* Select mode line face based on the real selected window. */
17218 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17219 current_buffer->mode_line_format);
17220 ++n;
17221 }
17222
17223 if (WINDOW_WANTS_HEADER_LINE_P (w))
17224 {
17225 display_mode_line (w, HEADER_LINE_FACE_ID,
17226 current_buffer->header_line_format);
17227 ++n;
17228 }
17229
17230 selected_frame = old_selected_frame;
17231 selected_window = old_selected_window;
17232 return n;
17233 }
17234
17235
17236 /* Display mode or top line of window W. FACE_ID specifies which line
17237 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17238 FORMAT is the mode line format to display. Value is the pixel
17239 height of the mode line displayed. */
17240
17241 static int
17242 display_mode_line (w, face_id, format)
17243 struct window *w;
17244 enum face_id face_id;
17245 Lisp_Object format;
17246 {
17247 struct it it;
17248 struct face *face;
17249 int count = SPECPDL_INDEX ();
17250
17251 init_iterator (&it, w, -1, -1, NULL, face_id);
17252 /* Don't extend on a previously drawn mode-line.
17253 This may happen if called from pos_visible_p. */
17254 it.glyph_row->enabled_p = 0;
17255 prepare_desired_row (it.glyph_row);
17256
17257 it.glyph_row->mode_line_p = 1;
17258
17259 if (! mode_line_inverse_video)
17260 /* Force the mode-line to be displayed in the default face. */
17261 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17262
17263 record_unwind_protect (unwind_format_mode_line,
17264 format_mode_line_unwind_data (NULL, Qnil, 0));
17265
17266 mode_line_target = MODE_LINE_DISPLAY;
17267
17268 /* Temporarily make frame's keyboard the current kboard so that
17269 kboard-local variables in the mode_line_format will get the right
17270 values. */
17271 push_kboard (FRAME_KBOARD (it.f));
17272 record_unwind_save_match_data ();
17273 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17274 pop_kboard ();
17275
17276 unbind_to (count, Qnil);
17277
17278 /* Fill up with spaces. */
17279 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17280
17281 compute_line_metrics (&it);
17282 it.glyph_row->full_width_p = 1;
17283 it.glyph_row->continued_p = 0;
17284 it.glyph_row->truncated_on_left_p = 0;
17285 it.glyph_row->truncated_on_right_p = 0;
17286
17287 /* Make a 3D mode-line have a shadow at its right end. */
17288 face = FACE_FROM_ID (it.f, face_id);
17289 extend_face_to_end_of_line (&it);
17290 if (face->box != FACE_NO_BOX)
17291 {
17292 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17293 + it.glyph_row->used[TEXT_AREA] - 1);
17294 last->right_box_line_p = 1;
17295 }
17296
17297 return it.glyph_row->height;
17298 }
17299
17300 /* Move element ELT in LIST to the front of LIST.
17301 Return the updated list. */
17302
17303 static Lisp_Object
17304 move_elt_to_front (elt, list)
17305 Lisp_Object elt, list;
17306 {
17307 register Lisp_Object tail, prev;
17308 register Lisp_Object tem;
17309
17310 tail = list;
17311 prev = Qnil;
17312 while (CONSP (tail))
17313 {
17314 tem = XCAR (tail);
17315
17316 if (EQ (elt, tem))
17317 {
17318 /* Splice out the link TAIL. */
17319 if (NILP (prev))
17320 list = XCDR (tail);
17321 else
17322 Fsetcdr (prev, XCDR (tail));
17323
17324 /* Now make it the first. */
17325 Fsetcdr (tail, list);
17326 return tail;
17327 }
17328 else
17329 prev = tail;
17330 tail = XCDR (tail);
17331 QUIT;
17332 }
17333
17334 /* Not found--return unchanged LIST. */
17335 return list;
17336 }
17337
17338 /* Contribute ELT to the mode line for window IT->w. How it
17339 translates into text depends on its data type.
17340
17341 IT describes the display environment in which we display, as usual.
17342
17343 DEPTH is the depth in recursion. It is used to prevent
17344 infinite recursion here.
17345
17346 FIELD_WIDTH is the number of characters the display of ELT should
17347 occupy in the mode line, and PRECISION is the maximum number of
17348 characters to display from ELT's representation. See
17349 display_string for details.
17350
17351 Returns the hpos of the end of the text generated by ELT.
17352
17353 PROPS is a property list to add to any string we encounter.
17354
17355 If RISKY is nonzero, remove (disregard) any properties in any string
17356 we encounter, and ignore :eval and :propertize.
17357
17358 The global variable `mode_line_target' determines whether the
17359 output is passed to `store_mode_line_noprop',
17360 `store_mode_line_string', or `display_string'. */
17361
17362 static int
17363 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17364 struct it *it;
17365 int depth;
17366 int field_width, precision;
17367 Lisp_Object elt, props;
17368 int risky;
17369 {
17370 int n = 0, field, prec;
17371 int literal = 0;
17372
17373 tail_recurse:
17374 if (depth > 100)
17375 elt = build_string ("*too-deep*");
17376
17377 depth++;
17378
17379 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17380 {
17381 case Lisp_String:
17382 {
17383 /* A string: output it and check for %-constructs within it. */
17384 unsigned char c;
17385 int offset = 0;
17386
17387 if (SCHARS (elt) > 0
17388 && (!NILP (props) || risky))
17389 {
17390 Lisp_Object oprops, aelt;
17391 oprops = Ftext_properties_at (make_number (0), elt);
17392
17393 /* If the starting string's properties are not what
17394 we want, translate the string. Also, if the string
17395 is risky, do that anyway. */
17396
17397 if (NILP (Fequal (props, oprops)) || risky)
17398 {
17399 /* If the starting string has properties,
17400 merge the specified ones onto the existing ones. */
17401 if (! NILP (oprops) && !risky)
17402 {
17403 Lisp_Object tem;
17404
17405 oprops = Fcopy_sequence (oprops);
17406 tem = props;
17407 while (CONSP (tem))
17408 {
17409 oprops = Fplist_put (oprops, XCAR (tem),
17410 XCAR (XCDR (tem)));
17411 tem = XCDR (XCDR (tem));
17412 }
17413 props = oprops;
17414 }
17415
17416 aelt = Fassoc (elt, mode_line_proptrans_alist);
17417 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17418 {
17419 /* AELT is what we want. Move it to the front
17420 without consing. */
17421 elt = XCAR (aelt);
17422 mode_line_proptrans_alist
17423 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17424 }
17425 else
17426 {
17427 Lisp_Object tem;
17428
17429 /* If AELT has the wrong props, it is useless.
17430 so get rid of it. */
17431 if (! NILP (aelt))
17432 mode_line_proptrans_alist
17433 = Fdelq (aelt, mode_line_proptrans_alist);
17434
17435 elt = Fcopy_sequence (elt);
17436 Fset_text_properties (make_number (0), Flength (elt),
17437 props, elt);
17438 /* Add this item to mode_line_proptrans_alist. */
17439 mode_line_proptrans_alist
17440 = Fcons (Fcons (elt, props),
17441 mode_line_proptrans_alist);
17442 /* Truncate mode_line_proptrans_alist
17443 to at most 50 elements. */
17444 tem = Fnthcdr (make_number (50),
17445 mode_line_proptrans_alist);
17446 if (! NILP (tem))
17447 XSETCDR (tem, Qnil);
17448 }
17449 }
17450 }
17451
17452 offset = 0;
17453
17454 if (literal)
17455 {
17456 prec = precision - n;
17457 switch (mode_line_target)
17458 {
17459 case MODE_LINE_NOPROP:
17460 case MODE_LINE_TITLE:
17461 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17462 break;
17463 case MODE_LINE_STRING:
17464 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17465 break;
17466 case MODE_LINE_DISPLAY:
17467 n += display_string (NULL, elt, Qnil, 0, 0, it,
17468 0, prec, 0, STRING_MULTIBYTE (elt));
17469 break;
17470 }
17471
17472 break;
17473 }
17474
17475 /* Handle the non-literal case. */
17476
17477 while ((precision <= 0 || n < precision)
17478 && SREF (elt, offset) != 0
17479 && (mode_line_target != MODE_LINE_DISPLAY
17480 || it->current_x < it->last_visible_x))
17481 {
17482 int last_offset = offset;
17483
17484 /* Advance to end of string or next format specifier. */
17485 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17486 ;
17487
17488 if (offset - 1 != last_offset)
17489 {
17490 int nchars, nbytes;
17491
17492 /* Output to end of string or up to '%'. Field width
17493 is length of string. Don't output more than
17494 PRECISION allows us. */
17495 offset--;
17496
17497 prec = c_string_width (SDATA (elt) + last_offset,
17498 offset - last_offset, precision - n,
17499 &nchars, &nbytes);
17500
17501 switch (mode_line_target)
17502 {
17503 case MODE_LINE_NOPROP:
17504 case MODE_LINE_TITLE:
17505 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17506 break;
17507 case MODE_LINE_STRING:
17508 {
17509 int bytepos = last_offset;
17510 int charpos = string_byte_to_char (elt, bytepos);
17511 int endpos = (precision <= 0
17512 ? string_byte_to_char (elt, offset)
17513 : charpos + nchars);
17514
17515 n += store_mode_line_string (NULL,
17516 Fsubstring (elt, make_number (charpos),
17517 make_number (endpos)),
17518 0, 0, 0, Qnil);
17519 }
17520 break;
17521 case MODE_LINE_DISPLAY:
17522 {
17523 int bytepos = last_offset;
17524 int charpos = string_byte_to_char (elt, bytepos);
17525
17526 if (precision <= 0)
17527 nchars = string_byte_to_char (elt, offset) - charpos;
17528 n += display_string (NULL, elt, Qnil, 0, charpos,
17529 it, 0, nchars, 0,
17530 STRING_MULTIBYTE (elt));
17531 }
17532 break;
17533 }
17534 }
17535 else /* c == '%' */
17536 {
17537 int percent_position = offset;
17538
17539 /* Get the specified minimum width. Zero means
17540 don't pad. */
17541 field = 0;
17542 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17543 field = field * 10 + c - '0';
17544
17545 /* Don't pad beyond the total padding allowed. */
17546 if (field_width - n > 0 && field > field_width - n)
17547 field = field_width - n;
17548
17549 /* Note that either PRECISION <= 0 or N < PRECISION. */
17550 prec = precision - n;
17551
17552 if (c == 'M')
17553 n += display_mode_element (it, depth, field, prec,
17554 Vglobal_mode_string, props,
17555 risky);
17556 else if (c != 0)
17557 {
17558 int multibyte;
17559 int bytepos, charpos;
17560 unsigned char *spec;
17561
17562 bytepos = percent_position;
17563 charpos = (STRING_MULTIBYTE (elt)
17564 ? string_byte_to_char (elt, bytepos)
17565 : bytepos);
17566 spec
17567 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17568
17569 switch (mode_line_target)
17570 {
17571 case MODE_LINE_NOPROP:
17572 case MODE_LINE_TITLE:
17573 n += store_mode_line_noprop (spec, field, prec);
17574 break;
17575 case MODE_LINE_STRING:
17576 {
17577 int len = strlen (spec);
17578 Lisp_Object tem = make_string (spec, len);
17579 props = Ftext_properties_at (make_number (charpos), elt);
17580 /* Should only keep face property in props */
17581 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17582 }
17583 break;
17584 case MODE_LINE_DISPLAY:
17585 {
17586 int nglyphs_before, nwritten;
17587
17588 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17589 nwritten = display_string (spec, Qnil, elt,
17590 charpos, 0, it,
17591 field, prec, 0,
17592 multibyte);
17593
17594 /* Assign to the glyphs written above the
17595 string where the `%x' came from, position
17596 of the `%'. */
17597 if (nwritten > 0)
17598 {
17599 struct glyph *glyph
17600 = (it->glyph_row->glyphs[TEXT_AREA]
17601 + nglyphs_before);
17602 int i;
17603
17604 for (i = 0; i < nwritten; ++i)
17605 {
17606 glyph[i].object = elt;
17607 glyph[i].charpos = charpos;
17608 }
17609
17610 n += nwritten;
17611 }
17612 }
17613 break;
17614 }
17615 }
17616 else /* c == 0 */
17617 break;
17618 }
17619 }
17620 }
17621 break;
17622
17623 case Lisp_Symbol:
17624 /* A symbol: process the value of the symbol recursively
17625 as if it appeared here directly. Avoid error if symbol void.
17626 Special case: if value of symbol is a string, output the string
17627 literally. */
17628 {
17629 register Lisp_Object tem;
17630
17631 /* If the variable is not marked as risky to set
17632 then its contents are risky to use. */
17633 if (NILP (Fget (elt, Qrisky_local_variable)))
17634 risky = 1;
17635
17636 tem = Fboundp (elt);
17637 if (!NILP (tem))
17638 {
17639 tem = Fsymbol_value (elt);
17640 /* If value is a string, output that string literally:
17641 don't check for % within it. */
17642 if (STRINGP (tem))
17643 literal = 1;
17644
17645 if (!EQ (tem, elt))
17646 {
17647 /* Give up right away for nil or t. */
17648 elt = tem;
17649 goto tail_recurse;
17650 }
17651 }
17652 }
17653 break;
17654
17655 case Lisp_Cons:
17656 {
17657 register Lisp_Object car, tem;
17658
17659 /* A cons cell: five distinct cases.
17660 If first element is :eval or :propertize, do something special.
17661 If first element is a string or a cons, process all the elements
17662 and effectively concatenate them.
17663 If first element is a negative number, truncate displaying cdr to
17664 at most that many characters. If positive, pad (with spaces)
17665 to at least that many characters.
17666 If first element is a symbol, process the cadr or caddr recursively
17667 according to whether the symbol's value is non-nil or nil. */
17668 car = XCAR (elt);
17669 if (EQ (car, QCeval))
17670 {
17671 /* An element of the form (:eval FORM) means evaluate FORM
17672 and use the result as mode line elements. */
17673
17674 if (risky)
17675 break;
17676
17677 if (CONSP (XCDR (elt)))
17678 {
17679 Lisp_Object spec;
17680 spec = safe_eval (XCAR (XCDR (elt)));
17681 n += display_mode_element (it, depth, field_width - n,
17682 precision - n, spec, props,
17683 risky);
17684 }
17685 }
17686 else if (EQ (car, QCpropertize))
17687 {
17688 /* An element of the form (:propertize ELT PROPS...)
17689 means display ELT but applying properties PROPS. */
17690
17691 if (risky)
17692 break;
17693
17694 if (CONSP (XCDR (elt)))
17695 n += display_mode_element (it, depth, field_width - n,
17696 precision - n, XCAR (XCDR (elt)),
17697 XCDR (XCDR (elt)), risky);
17698 }
17699 else if (SYMBOLP (car))
17700 {
17701 tem = Fboundp (car);
17702 elt = XCDR (elt);
17703 if (!CONSP (elt))
17704 goto invalid;
17705 /* elt is now the cdr, and we know it is a cons cell.
17706 Use its car if CAR has a non-nil value. */
17707 if (!NILP (tem))
17708 {
17709 tem = Fsymbol_value (car);
17710 if (!NILP (tem))
17711 {
17712 elt = XCAR (elt);
17713 goto tail_recurse;
17714 }
17715 }
17716 /* Symbol's value is nil (or symbol is unbound)
17717 Get the cddr of the original list
17718 and if possible find the caddr and use that. */
17719 elt = XCDR (elt);
17720 if (NILP (elt))
17721 break;
17722 else if (!CONSP (elt))
17723 goto invalid;
17724 elt = XCAR (elt);
17725 goto tail_recurse;
17726 }
17727 else if (INTEGERP (car))
17728 {
17729 register int lim = XINT (car);
17730 elt = XCDR (elt);
17731 if (lim < 0)
17732 {
17733 /* Negative int means reduce maximum width. */
17734 if (precision <= 0)
17735 precision = -lim;
17736 else
17737 precision = min (precision, -lim);
17738 }
17739 else if (lim > 0)
17740 {
17741 /* Padding specified. Don't let it be more than
17742 current maximum. */
17743 if (precision > 0)
17744 lim = min (precision, lim);
17745
17746 /* If that's more padding than already wanted, queue it.
17747 But don't reduce padding already specified even if
17748 that is beyond the current truncation point. */
17749 field_width = max (lim, field_width);
17750 }
17751 goto tail_recurse;
17752 }
17753 else if (STRINGP (car) || CONSP (car))
17754 {
17755 register int limit = 50;
17756 /* Limit is to protect against circular lists. */
17757 while (CONSP (elt)
17758 && --limit > 0
17759 && (precision <= 0 || n < precision))
17760 {
17761 n += display_mode_element (it, depth,
17762 /* Do padding only after the last
17763 element in the list. */
17764 (! CONSP (XCDR (elt))
17765 ? field_width - n
17766 : 0),
17767 precision - n, XCAR (elt),
17768 props, risky);
17769 elt = XCDR (elt);
17770 }
17771 }
17772 }
17773 break;
17774
17775 default:
17776 invalid:
17777 elt = build_string ("*invalid*");
17778 goto tail_recurse;
17779 }
17780
17781 /* Pad to FIELD_WIDTH. */
17782 if (field_width > 0 && n < field_width)
17783 {
17784 switch (mode_line_target)
17785 {
17786 case MODE_LINE_NOPROP:
17787 case MODE_LINE_TITLE:
17788 n += store_mode_line_noprop ("", field_width - n, 0);
17789 break;
17790 case MODE_LINE_STRING:
17791 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17792 break;
17793 case MODE_LINE_DISPLAY:
17794 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17795 0, 0, 0);
17796 break;
17797 }
17798 }
17799
17800 return n;
17801 }
17802
17803 /* Store a mode-line string element in mode_line_string_list.
17804
17805 If STRING is non-null, display that C string. Otherwise, the Lisp
17806 string LISP_STRING is displayed.
17807
17808 FIELD_WIDTH is the minimum number of output glyphs to produce.
17809 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17810 with spaces. FIELD_WIDTH <= 0 means don't pad.
17811
17812 PRECISION is the maximum number of characters to output from
17813 STRING. PRECISION <= 0 means don't truncate the string.
17814
17815 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17816 properties to the string.
17817
17818 PROPS are the properties to add to the string.
17819 The mode_line_string_face face property is always added to the string.
17820 */
17821
17822 static int
17823 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17824 char *string;
17825 Lisp_Object lisp_string;
17826 int copy_string;
17827 int field_width;
17828 int precision;
17829 Lisp_Object props;
17830 {
17831 int len;
17832 int n = 0;
17833
17834 if (string != NULL)
17835 {
17836 len = strlen (string);
17837 if (precision > 0 && len > precision)
17838 len = precision;
17839 lisp_string = make_string (string, len);
17840 if (NILP (props))
17841 props = mode_line_string_face_prop;
17842 else if (!NILP (mode_line_string_face))
17843 {
17844 Lisp_Object face = Fplist_get (props, Qface);
17845 props = Fcopy_sequence (props);
17846 if (NILP (face))
17847 face = mode_line_string_face;
17848 else
17849 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17850 props = Fplist_put (props, Qface, face);
17851 }
17852 Fadd_text_properties (make_number (0), make_number (len),
17853 props, lisp_string);
17854 }
17855 else
17856 {
17857 len = XFASTINT (Flength (lisp_string));
17858 if (precision > 0 && len > precision)
17859 {
17860 len = precision;
17861 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17862 precision = -1;
17863 }
17864 if (!NILP (mode_line_string_face))
17865 {
17866 Lisp_Object face;
17867 if (NILP (props))
17868 props = Ftext_properties_at (make_number (0), lisp_string);
17869 face = Fplist_get (props, Qface);
17870 if (NILP (face))
17871 face = mode_line_string_face;
17872 else
17873 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17874 props = Fcons (Qface, Fcons (face, Qnil));
17875 if (copy_string)
17876 lisp_string = Fcopy_sequence (lisp_string);
17877 }
17878 if (!NILP (props))
17879 Fadd_text_properties (make_number (0), make_number (len),
17880 props, lisp_string);
17881 }
17882
17883 if (len > 0)
17884 {
17885 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17886 n += len;
17887 }
17888
17889 if (field_width > len)
17890 {
17891 field_width -= len;
17892 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17893 if (!NILP (props))
17894 Fadd_text_properties (make_number (0), make_number (field_width),
17895 props, lisp_string);
17896 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17897 n += field_width;
17898 }
17899
17900 return n;
17901 }
17902
17903
17904 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17905 1, 4, 0,
17906 doc: /* Format a string out of a mode line format specification.
17907 First arg FORMAT specifies the mode line format (see `mode-line-format'
17908 for details) to use.
17909
17910 Optional second arg FACE specifies the face property to put
17911 on all characters for which no face is specified.
17912 The value t means whatever face the window's mode line currently uses
17913 \(either `mode-line' or `mode-line-inactive', depending).
17914 A value of nil means the default is no face property.
17915 If FACE is an integer, the value string has no text properties.
17916
17917 Optional third and fourth args WINDOW and BUFFER specify the window
17918 and buffer to use as the context for the formatting (defaults
17919 are the selected window and the window's buffer). */)
17920 (format, face, window, buffer)
17921 Lisp_Object format, face, window, buffer;
17922 {
17923 struct it it;
17924 int len;
17925 struct window *w;
17926 struct buffer *old_buffer = NULL;
17927 int face_id = -1;
17928 int no_props = INTEGERP (face);
17929 int count = SPECPDL_INDEX ();
17930 Lisp_Object str;
17931 int string_start = 0;
17932
17933 if (NILP (window))
17934 window = selected_window;
17935 CHECK_WINDOW (window);
17936 w = XWINDOW (window);
17937
17938 if (NILP (buffer))
17939 buffer = w->buffer;
17940 CHECK_BUFFER (buffer);
17941
17942 /* Make formatting the modeline a non-op when noninteractive, otherwise
17943 there will be problems later caused by a partially initialized frame. */
17944 if (NILP (format) || noninteractive)
17945 return empty_unibyte_string;
17946
17947 if (no_props)
17948 face = Qnil;
17949
17950 if (!NILP (face))
17951 {
17952 if (EQ (face, Qt))
17953 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17954 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17955 }
17956
17957 if (face_id < 0)
17958 face_id = DEFAULT_FACE_ID;
17959
17960 if (XBUFFER (buffer) != current_buffer)
17961 old_buffer = current_buffer;
17962
17963 /* Save things including mode_line_proptrans_alist,
17964 and set that to nil so that we don't alter the outer value. */
17965 record_unwind_protect (unwind_format_mode_line,
17966 format_mode_line_unwind_data
17967 (old_buffer, selected_window, 1));
17968 mode_line_proptrans_alist = Qnil;
17969
17970 Fselect_window (window, Qt);
17971 if (old_buffer)
17972 set_buffer_internal_1 (XBUFFER (buffer));
17973
17974 init_iterator (&it, w, -1, -1, NULL, face_id);
17975
17976 if (no_props)
17977 {
17978 mode_line_target = MODE_LINE_NOPROP;
17979 mode_line_string_face_prop = Qnil;
17980 mode_line_string_list = Qnil;
17981 string_start = MODE_LINE_NOPROP_LEN (0);
17982 }
17983 else
17984 {
17985 mode_line_target = MODE_LINE_STRING;
17986 mode_line_string_list = Qnil;
17987 mode_line_string_face = face;
17988 mode_line_string_face_prop
17989 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17990 }
17991
17992 push_kboard (FRAME_KBOARD (it.f));
17993 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17994 pop_kboard ();
17995
17996 if (no_props)
17997 {
17998 len = MODE_LINE_NOPROP_LEN (string_start);
17999 str = make_string (mode_line_noprop_buf + string_start, len);
18000 }
18001 else
18002 {
18003 mode_line_string_list = Fnreverse (mode_line_string_list);
18004 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18005 empty_unibyte_string);
18006 }
18007
18008 unbind_to (count, Qnil);
18009 return str;
18010 }
18011
18012 /* Write a null-terminated, right justified decimal representation of
18013 the positive integer D to BUF using a minimal field width WIDTH. */
18014
18015 static void
18016 pint2str (buf, width, d)
18017 register char *buf;
18018 register int width;
18019 register int d;
18020 {
18021 register char *p = buf;
18022
18023 if (d <= 0)
18024 *p++ = '0';
18025 else
18026 {
18027 while (d > 0)
18028 {
18029 *p++ = d % 10 + '0';
18030 d /= 10;
18031 }
18032 }
18033
18034 for (width -= (int) (p - buf); width > 0; --width)
18035 *p++ = ' ';
18036 *p-- = '\0';
18037 while (p > buf)
18038 {
18039 d = *buf;
18040 *buf++ = *p;
18041 *p-- = d;
18042 }
18043 }
18044
18045 /* Write a null-terminated, right justified decimal and "human
18046 readable" representation of the nonnegative integer D to BUF using
18047 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18048
18049 static const char power_letter[] =
18050 {
18051 0, /* not used */
18052 'k', /* kilo */
18053 'M', /* mega */
18054 'G', /* giga */
18055 'T', /* tera */
18056 'P', /* peta */
18057 'E', /* exa */
18058 'Z', /* zetta */
18059 'Y' /* yotta */
18060 };
18061
18062 static void
18063 pint2hrstr (buf, width, d)
18064 char *buf;
18065 int width;
18066 int d;
18067 {
18068 /* We aim to represent the nonnegative integer D as
18069 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18070 int quotient = d;
18071 int remainder = 0;
18072 /* -1 means: do not use TENTHS. */
18073 int tenths = -1;
18074 int exponent = 0;
18075
18076 /* Length of QUOTIENT.TENTHS as a string. */
18077 int length;
18078
18079 char * psuffix;
18080 char * p;
18081
18082 if (1000 <= quotient)
18083 {
18084 /* Scale to the appropriate EXPONENT. */
18085 do
18086 {
18087 remainder = quotient % 1000;
18088 quotient /= 1000;
18089 exponent++;
18090 }
18091 while (1000 <= quotient);
18092
18093 /* Round to nearest and decide whether to use TENTHS or not. */
18094 if (quotient <= 9)
18095 {
18096 tenths = remainder / 100;
18097 if (50 <= remainder % 100)
18098 {
18099 if (tenths < 9)
18100 tenths++;
18101 else
18102 {
18103 quotient++;
18104 if (quotient == 10)
18105 tenths = -1;
18106 else
18107 tenths = 0;
18108 }
18109 }
18110 }
18111 else
18112 if (500 <= remainder)
18113 {
18114 if (quotient < 999)
18115 quotient++;
18116 else
18117 {
18118 quotient = 1;
18119 exponent++;
18120 tenths = 0;
18121 }
18122 }
18123 }
18124
18125 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18126 if (tenths == -1 && quotient <= 99)
18127 if (quotient <= 9)
18128 length = 1;
18129 else
18130 length = 2;
18131 else
18132 length = 3;
18133 p = psuffix = buf + max (width, length);
18134
18135 /* Print EXPONENT. */
18136 if (exponent)
18137 *psuffix++ = power_letter[exponent];
18138 *psuffix = '\0';
18139
18140 /* Print TENTHS. */
18141 if (tenths >= 0)
18142 {
18143 *--p = '0' + tenths;
18144 *--p = '.';
18145 }
18146
18147 /* Print QUOTIENT. */
18148 do
18149 {
18150 int digit = quotient % 10;
18151 *--p = '0' + digit;
18152 }
18153 while ((quotient /= 10) != 0);
18154
18155 /* Print leading spaces. */
18156 while (buf < p)
18157 *--p = ' ';
18158 }
18159
18160 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18161 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18162 type of CODING_SYSTEM. Return updated pointer into BUF. */
18163
18164 static unsigned char invalid_eol_type[] = "(*invalid*)";
18165
18166 static char *
18167 decode_mode_spec_coding (coding_system, buf, eol_flag)
18168 Lisp_Object coding_system;
18169 register char *buf;
18170 int eol_flag;
18171 {
18172 Lisp_Object val;
18173 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18174 const unsigned char *eol_str;
18175 int eol_str_len;
18176 /* The EOL conversion we are using. */
18177 Lisp_Object eoltype;
18178
18179 val = CODING_SYSTEM_SPEC (coding_system);
18180 eoltype = Qnil;
18181
18182 if (!VECTORP (val)) /* Not yet decided. */
18183 {
18184 if (multibyte)
18185 *buf++ = '-';
18186 if (eol_flag)
18187 eoltype = eol_mnemonic_undecided;
18188 /* Don't mention EOL conversion if it isn't decided. */
18189 }
18190 else
18191 {
18192 Lisp_Object attrs;
18193 Lisp_Object eolvalue;
18194
18195 attrs = AREF (val, 0);
18196 eolvalue = AREF (val, 2);
18197
18198 if (multibyte)
18199 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18200
18201 if (eol_flag)
18202 {
18203 /* The EOL conversion that is normal on this system. */
18204
18205 if (NILP (eolvalue)) /* Not yet decided. */
18206 eoltype = eol_mnemonic_undecided;
18207 else if (VECTORP (eolvalue)) /* Not yet decided. */
18208 eoltype = eol_mnemonic_undecided;
18209 else /* eolvalue is Qunix, Qdos, or Qmac. */
18210 eoltype = (EQ (eolvalue, Qunix)
18211 ? eol_mnemonic_unix
18212 : (EQ (eolvalue, Qdos) == 1
18213 ? eol_mnemonic_dos : eol_mnemonic_mac));
18214 }
18215 }
18216
18217 if (eol_flag)
18218 {
18219 /* Mention the EOL conversion if it is not the usual one. */
18220 if (STRINGP (eoltype))
18221 {
18222 eol_str = SDATA (eoltype);
18223 eol_str_len = SBYTES (eoltype);
18224 }
18225 else if (CHARACTERP (eoltype))
18226 {
18227 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18228 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18229 eol_str = tmp;
18230 }
18231 else
18232 {
18233 eol_str = invalid_eol_type;
18234 eol_str_len = sizeof (invalid_eol_type) - 1;
18235 }
18236 bcopy (eol_str, buf, eol_str_len);
18237 buf += eol_str_len;
18238 }
18239
18240 return buf;
18241 }
18242
18243 /* Return a string for the output of a mode line %-spec for window W,
18244 generated by character C. PRECISION >= 0 means don't return a
18245 string longer than that value. FIELD_WIDTH > 0 means pad the
18246 string returned with spaces to that value. Return 1 in *MULTIBYTE
18247 if the result is multibyte text.
18248
18249 Note we operate on the current buffer for most purposes,
18250 the exception being w->base_line_pos. */
18251
18252 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18253
18254 static char *
18255 decode_mode_spec (w, c, field_width, precision, multibyte)
18256 struct window *w;
18257 register int c;
18258 int field_width, precision;
18259 int *multibyte;
18260 {
18261 Lisp_Object obj;
18262 struct frame *f = XFRAME (WINDOW_FRAME (w));
18263 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18264 struct buffer *b = current_buffer;
18265
18266 obj = Qnil;
18267 *multibyte = 0;
18268
18269 switch (c)
18270 {
18271 case '*':
18272 if (!NILP (b->read_only))
18273 return "%";
18274 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18275 return "*";
18276 return "-";
18277
18278 case '+':
18279 /* This differs from %* only for a modified read-only buffer. */
18280 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18281 return "*";
18282 if (!NILP (b->read_only))
18283 return "%";
18284 return "-";
18285
18286 case '&':
18287 /* This differs from %* in ignoring read-only-ness. */
18288 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18289 return "*";
18290 return "-";
18291
18292 case '%':
18293 return "%";
18294
18295 case '[':
18296 {
18297 int i;
18298 char *p;
18299
18300 if (command_loop_level > 5)
18301 return "[[[... ";
18302 p = decode_mode_spec_buf;
18303 for (i = 0; i < command_loop_level; i++)
18304 *p++ = '[';
18305 *p = 0;
18306 return decode_mode_spec_buf;
18307 }
18308
18309 case ']':
18310 {
18311 int i;
18312 char *p;
18313
18314 if (command_loop_level > 5)
18315 return " ...]]]";
18316 p = decode_mode_spec_buf;
18317 for (i = 0; i < command_loop_level; i++)
18318 *p++ = ']';
18319 *p = 0;
18320 return decode_mode_spec_buf;
18321 }
18322
18323 case '-':
18324 {
18325 register int i;
18326
18327 /* Let lots_of_dashes be a string of infinite length. */
18328 if (mode_line_target == MODE_LINE_NOPROP ||
18329 mode_line_target == MODE_LINE_STRING)
18330 return "--";
18331 if (field_width <= 0
18332 || field_width > sizeof (lots_of_dashes))
18333 {
18334 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18335 decode_mode_spec_buf[i] = '-';
18336 decode_mode_spec_buf[i] = '\0';
18337 return decode_mode_spec_buf;
18338 }
18339 else
18340 return lots_of_dashes;
18341 }
18342
18343 case 'b':
18344 obj = b->name;
18345 break;
18346
18347 case 'c':
18348 /* %c and %l are ignored in `frame-title-format'.
18349 (In redisplay_internal, the frame title is drawn _before_ the
18350 windows are updated, so the stuff which depends on actual
18351 window contents (such as %l) may fail to render properly, or
18352 even crash emacs.) */
18353 if (mode_line_target == MODE_LINE_TITLE)
18354 return "";
18355 else
18356 {
18357 int col = (int) current_column (); /* iftc */
18358 w->column_number_displayed = make_number (col);
18359 pint2str (decode_mode_spec_buf, field_width, col);
18360 return decode_mode_spec_buf;
18361 }
18362
18363 case 'e':
18364 #ifndef SYSTEM_MALLOC
18365 {
18366 if (NILP (Vmemory_full))
18367 return "";
18368 else
18369 return "!MEM FULL! ";
18370 }
18371 #else
18372 return "";
18373 #endif
18374
18375 case 'F':
18376 /* %F displays the frame name. */
18377 if (!NILP (f->title))
18378 return (char *) SDATA (f->title);
18379 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18380 return (char *) SDATA (f->name);
18381 return "Emacs";
18382
18383 case 'f':
18384 obj = b->filename;
18385 break;
18386
18387 case 'i':
18388 {
18389 int size = ZV - BEGV;
18390 pint2str (decode_mode_spec_buf, field_width, size);
18391 return decode_mode_spec_buf;
18392 }
18393
18394 case 'I':
18395 {
18396 int size = ZV - BEGV;
18397 pint2hrstr (decode_mode_spec_buf, field_width, size);
18398 return decode_mode_spec_buf;
18399 }
18400
18401 case 'l':
18402 {
18403 int startpos, startpos_byte, line, linepos, linepos_byte;
18404 int topline, nlines, junk, height;
18405
18406 /* %c and %l are ignored in `frame-title-format'. */
18407 if (mode_line_target == MODE_LINE_TITLE)
18408 return "";
18409
18410 startpos = XMARKER (w->start)->charpos;
18411 startpos_byte = marker_byte_position (w->start);
18412 height = WINDOW_TOTAL_LINES (w);
18413
18414 /* If we decided that this buffer isn't suitable for line numbers,
18415 don't forget that too fast. */
18416 if (EQ (w->base_line_pos, w->buffer))
18417 goto no_value;
18418 /* But do forget it, if the window shows a different buffer now. */
18419 else if (BUFFERP (w->base_line_pos))
18420 w->base_line_pos = Qnil;
18421
18422 /* If the buffer is very big, don't waste time. */
18423 if (INTEGERP (Vline_number_display_limit)
18424 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18425 {
18426 w->base_line_pos = Qnil;
18427 w->base_line_number = Qnil;
18428 goto no_value;
18429 }
18430
18431 if (INTEGERP (w->base_line_number)
18432 && INTEGERP (w->base_line_pos)
18433 && XFASTINT (w->base_line_pos) <= startpos)
18434 {
18435 line = XFASTINT (w->base_line_number);
18436 linepos = XFASTINT (w->base_line_pos);
18437 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18438 }
18439 else
18440 {
18441 line = 1;
18442 linepos = BUF_BEGV (b);
18443 linepos_byte = BUF_BEGV_BYTE (b);
18444 }
18445
18446 /* Count lines from base line to window start position. */
18447 nlines = display_count_lines (linepos, linepos_byte,
18448 startpos_byte,
18449 startpos, &junk);
18450
18451 topline = nlines + line;
18452
18453 /* Determine a new base line, if the old one is too close
18454 or too far away, or if we did not have one.
18455 "Too close" means it's plausible a scroll-down would
18456 go back past it. */
18457 if (startpos == BUF_BEGV (b))
18458 {
18459 w->base_line_number = make_number (topline);
18460 w->base_line_pos = make_number (BUF_BEGV (b));
18461 }
18462 else if (nlines < height + 25 || nlines > height * 3 + 50
18463 || linepos == BUF_BEGV (b))
18464 {
18465 int limit = BUF_BEGV (b);
18466 int limit_byte = BUF_BEGV_BYTE (b);
18467 int position;
18468 int distance = (height * 2 + 30) * line_number_display_limit_width;
18469
18470 if (startpos - distance > limit)
18471 {
18472 limit = startpos - distance;
18473 limit_byte = CHAR_TO_BYTE (limit);
18474 }
18475
18476 nlines = display_count_lines (startpos, startpos_byte,
18477 limit_byte,
18478 - (height * 2 + 30),
18479 &position);
18480 /* If we couldn't find the lines we wanted within
18481 line_number_display_limit_width chars per line,
18482 give up on line numbers for this window. */
18483 if (position == limit_byte && limit == startpos - distance)
18484 {
18485 w->base_line_pos = w->buffer;
18486 w->base_line_number = Qnil;
18487 goto no_value;
18488 }
18489
18490 w->base_line_number = make_number (topline - nlines);
18491 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18492 }
18493
18494 /* Now count lines from the start pos to point. */
18495 nlines = display_count_lines (startpos, startpos_byte,
18496 PT_BYTE, PT, &junk);
18497
18498 /* Record that we did display the line number. */
18499 line_number_displayed = 1;
18500
18501 /* Make the string to show. */
18502 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18503 return decode_mode_spec_buf;
18504 no_value:
18505 {
18506 char* p = decode_mode_spec_buf;
18507 int pad = field_width - 2;
18508 while (pad-- > 0)
18509 *p++ = ' ';
18510 *p++ = '?';
18511 *p++ = '?';
18512 *p = '\0';
18513 return decode_mode_spec_buf;
18514 }
18515 }
18516 break;
18517
18518 case 'm':
18519 obj = b->mode_name;
18520 break;
18521
18522 case 'n':
18523 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18524 return " Narrow";
18525 break;
18526
18527 case 'p':
18528 {
18529 int pos = marker_position (w->start);
18530 int total = BUF_ZV (b) - BUF_BEGV (b);
18531
18532 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18533 {
18534 if (pos <= BUF_BEGV (b))
18535 return "All";
18536 else
18537 return "Bottom";
18538 }
18539 else if (pos <= BUF_BEGV (b))
18540 return "Top";
18541 else
18542 {
18543 if (total > 1000000)
18544 /* Do it differently for a large value, to avoid overflow. */
18545 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18546 else
18547 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18548 /* We can't normally display a 3-digit number,
18549 so get us a 2-digit number that is close. */
18550 if (total == 100)
18551 total = 99;
18552 sprintf (decode_mode_spec_buf, "%2d%%", total);
18553 return decode_mode_spec_buf;
18554 }
18555 }
18556
18557 /* Display percentage of size above the bottom of the screen. */
18558 case 'P':
18559 {
18560 int toppos = marker_position (w->start);
18561 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18562 int total = BUF_ZV (b) - BUF_BEGV (b);
18563
18564 if (botpos >= BUF_ZV (b))
18565 {
18566 if (toppos <= BUF_BEGV (b))
18567 return "All";
18568 else
18569 return "Bottom";
18570 }
18571 else
18572 {
18573 if (total > 1000000)
18574 /* Do it differently for a large value, to avoid overflow. */
18575 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18576 else
18577 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18578 /* We can't normally display a 3-digit number,
18579 so get us a 2-digit number that is close. */
18580 if (total == 100)
18581 total = 99;
18582 if (toppos <= BUF_BEGV (b))
18583 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18584 else
18585 sprintf (decode_mode_spec_buf, "%2d%%", total);
18586 return decode_mode_spec_buf;
18587 }
18588 }
18589
18590 case 's':
18591 /* status of process */
18592 obj = Fget_buffer_process (Fcurrent_buffer ());
18593 if (NILP (obj))
18594 return "no process";
18595 #ifdef subprocesses
18596 obj = Fsymbol_name (Fprocess_status (obj));
18597 #endif
18598 break;
18599
18600 case '@':
18601 {
18602 Lisp_Object val;
18603 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18604 if (NILP (val))
18605 return "-";
18606 else
18607 return "@";
18608 }
18609
18610 case 't': /* indicate TEXT or BINARY */
18611 #ifdef MODE_LINE_BINARY_TEXT
18612 return MODE_LINE_BINARY_TEXT (b);
18613 #else
18614 return "T";
18615 #endif
18616
18617 case 'z':
18618 /* coding-system (not including end-of-line format) */
18619 case 'Z':
18620 /* coding-system (including end-of-line type) */
18621 {
18622 int eol_flag = (c == 'Z');
18623 char *p = decode_mode_spec_buf;
18624
18625 if (! FRAME_WINDOW_P (f))
18626 {
18627 /* No need to mention EOL here--the terminal never needs
18628 to do EOL conversion. */
18629 p = decode_mode_spec_coding (CODING_ID_NAME
18630 (FRAME_KEYBOARD_CODING (f)->id),
18631 p, 0);
18632 p = decode_mode_spec_coding (CODING_ID_NAME
18633 (FRAME_TERMINAL_CODING (f)->id),
18634 p, 0);
18635 }
18636 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18637 p, eol_flag);
18638
18639 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18640 #ifdef subprocesses
18641 obj = Fget_buffer_process (Fcurrent_buffer ());
18642 if (PROCESSP (obj))
18643 {
18644 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18645 p, eol_flag);
18646 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18647 p, eol_flag);
18648 }
18649 #endif /* subprocesses */
18650 #endif /* 0 */
18651 *p = 0;
18652 return decode_mode_spec_buf;
18653 }
18654 }
18655
18656 if (STRINGP (obj))
18657 {
18658 *multibyte = STRING_MULTIBYTE (obj);
18659 return (char *) SDATA (obj);
18660 }
18661 else
18662 return "";
18663 }
18664
18665
18666 /* Count up to COUNT lines starting from START / START_BYTE.
18667 But don't go beyond LIMIT_BYTE.
18668 Return the number of lines thus found (always nonnegative).
18669
18670 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18671
18672 static int
18673 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18674 int start, start_byte, limit_byte, count;
18675 int *byte_pos_ptr;
18676 {
18677 register unsigned char *cursor;
18678 unsigned char *base;
18679
18680 register int ceiling;
18681 register unsigned char *ceiling_addr;
18682 int orig_count = count;
18683
18684 /* If we are not in selective display mode,
18685 check only for newlines. */
18686 int selective_display = (!NILP (current_buffer->selective_display)
18687 && !INTEGERP (current_buffer->selective_display));
18688
18689 if (count > 0)
18690 {
18691 while (start_byte < limit_byte)
18692 {
18693 ceiling = BUFFER_CEILING_OF (start_byte);
18694 ceiling = min (limit_byte - 1, ceiling);
18695 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18696 base = (cursor = BYTE_POS_ADDR (start_byte));
18697 while (1)
18698 {
18699 if (selective_display)
18700 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18701 ;
18702 else
18703 while (*cursor != '\n' && ++cursor != ceiling_addr)
18704 ;
18705
18706 if (cursor != ceiling_addr)
18707 {
18708 if (--count == 0)
18709 {
18710 start_byte += cursor - base + 1;
18711 *byte_pos_ptr = start_byte;
18712 return orig_count;
18713 }
18714 else
18715 if (++cursor == ceiling_addr)
18716 break;
18717 }
18718 else
18719 break;
18720 }
18721 start_byte += cursor - base;
18722 }
18723 }
18724 else
18725 {
18726 while (start_byte > limit_byte)
18727 {
18728 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18729 ceiling = max (limit_byte, ceiling);
18730 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18731 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18732 while (1)
18733 {
18734 if (selective_display)
18735 while (--cursor != ceiling_addr
18736 && *cursor != '\n' && *cursor != 015)
18737 ;
18738 else
18739 while (--cursor != ceiling_addr && *cursor != '\n')
18740 ;
18741
18742 if (cursor != ceiling_addr)
18743 {
18744 if (++count == 0)
18745 {
18746 start_byte += cursor - base + 1;
18747 *byte_pos_ptr = start_byte;
18748 /* When scanning backwards, we should
18749 not count the newline posterior to which we stop. */
18750 return - orig_count - 1;
18751 }
18752 }
18753 else
18754 break;
18755 }
18756 /* Here we add 1 to compensate for the last decrement
18757 of CURSOR, which took it past the valid range. */
18758 start_byte += cursor - base + 1;
18759 }
18760 }
18761
18762 *byte_pos_ptr = limit_byte;
18763
18764 if (count < 0)
18765 return - orig_count + count;
18766 return orig_count - count;
18767
18768 }
18769
18770
18771 \f
18772 /***********************************************************************
18773 Displaying strings
18774 ***********************************************************************/
18775
18776 /* Display a NUL-terminated string, starting with index START.
18777
18778 If STRING is non-null, display that C string. Otherwise, the Lisp
18779 string LISP_STRING is displayed.
18780
18781 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18782 FACE_STRING. Display STRING or LISP_STRING with the face at
18783 FACE_STRING_POS in FACE_STRING:
18784
18785 Display the string in the environment given by IT, but use the
18786 standard display table, temporarily.
18787
18788 FIELD_WIDTH is the minimum number of output glyphs to produce.
18789 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18790 with spaces. If STRING has more characters, more than FIELD_WIDTH
18791 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18792
18793 PRECISION is the maximum number of characters to output from
18794 STRING. PRECISION < 0 means don't truncate the string.
18795
18796 This is roughly equivalent to printf format specifiers:
18797
18798 FIELD_WIDTH PRECISION PRINTF
18799 ----------------------------------------
18800 -1 -1 %s
18801 -1 10 %.10s
18802 10 -1 %10s
18803 20 10 %20.10s
18804
18805 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18806 display them, and < 0 means obey the current buffer's value of
18807 enable_multibyte_characters.
18808
18809 Value is the number of columns displayed. */
18810
18811 static int
18812 display_string (string, lisp_string, face_string, face_string_pos,
18813 start, it, field_width, precision, max_x, multibyte)
18814 unsigned char *string;
18815 Lisp_Object lisp_string;
18816 Lisp_Object face_string;
18817 EMACS_INT face_string_pos;
18818 EMACS_INT start;
18819 struct it *it;
18820 int field_width, precision, max_x;
18821 int multibyte;
18822 {
18823 int hpos_at_start = it->hpos;
18824 int saved_face_id = it->face_id;
18825 struct glyph_row *row = it->glyph_row;
18826
18827 /* Initialize the iterator IT for iteration over STRING beginning
18828 with index START. */
18829 reseat_to_string (it, string, lisp_string, start,
18830 precision, field_width, multibyte);
18831
18832 /* If displaying STRING, set up the face of the iterator
18833 from LISP_STRING, if that's given. */
18834 if (STRINGP (face_string))
18835 {
18836 EMACS_INT endptr;
18837 struct face *face;
18838
18839 it->face_id
18840 = face_at_string_position (it->w, face_string, face_string_pos,
18841 0, it->region_beg_charpos,
18842 it->region_end_charpos,
18843 &endptr, it->base_face_id, 0);
18844 face = FACE_FROM_ID (it->f, it->face_id);
18845 it->face_box_p = face->box != FACE_NO_BOX;
18846 }
18847
18848 /* Set max_x to the maximum allowed X position. Don't let it go
18849 beyond the right edge of the window. */
18850 if (max_x <= 0)
18851 max_x = it->last_visible_x;
18852 else
18853 max_x = min (max_x, it->last_visible_x);
18854
18855 /* Skip over display elements that are not visible. because IT->w is
18856 hscrolled. */
18857 if (it->current_x < it->first_visible_x)
18858 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18859 MOVE_TO_POS | MOVE_TO_X);
18860
18861 row->ascent = it->max_ascent;
18862 row->height = it->max_ascent + it->max_descent;
18863 row->phys_ascent = it->max_phys_ascent;
18864 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18865 row->extra_line_spacing = it->max_extra_line_spacing;
18866
18867 /* This condition is for the case that we are called with current_x
18868 past last_visible_x. */
18869 while (it->current_x < max_x)
18870 {
18871 int x_before, x, n_glyphs_before, i, nglyphs;
18872
18873 /* Get the next display element. */
18874 if (!get_next_display_element (it))
18875 break;
18876
18877 /* Produce glyphs. */
18878 x_before = it->current_x;
18879 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18880 PRODUCE_GLYPHS (it);
18881
18882 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18883 i = 0;
18884 x = x_before;
18885 while (i < nglyphs)
18886 {
18887 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18888
18889 if (it->line_wrap != TRUNCATE
18890 && x + glyph->pixel_width > max_x)
18891 {
18892 /* End of continued line or max_x reached. */
18893 if (CHAR_GLYPH_PADDING_P (*glyph))
18894 {
18895 /* A wide character is unbreakable. */
18896 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18897 it->current_x = x_before;
18898 }
18899 else
18900 {
18901 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18902 it->current_x = x;
18903 }
18904 break;
18905 }
18906 else if (x + glyph->pixel_width >= it->first_visible_x)
18907 {
18908 /* Glyph is at least partially visible. */
18909 ++it->hpos;
18910 if (x < it->first_visible_x)
18911 it->glyph_row->x = x - it->first_visible_x;
18912 }
18913 else
18914 {
18915 /* Glyph is off the left margin of the display area.
18916 Should not happen. */
18917 abort ();
18918 }
18919
18920 row->ascent = max (row->ascent, it->max_ascent);
18921 row->height = max (row->height, it->max_ascent + it->max_descent);
18922 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18923 row->phys_height = max (row->phys_height,
18924 it->max_phys_ascent + it->max_phys_descent);
18925 row->extra_line_spacing = max (row->extra_line_spacing,
18926 it->max_extra_line_spacing);
18927 x += glyph->pixel_width;
18928 ++i;
18929 }
18930
18931 /* Stop if max_x reached. */
18932 if (i < nglyphs)
18933 break;
18934
18935 /* Stop at line ends. */
18936 if (ITERATOR_AT_END_OF_LINE_P (it))
18937 {
18938 it->continuation_lines_width = 0;
18939 break;
18940 }
18941
18942 set_iterator_to_next (it, 1);
18943
18944 /* Stop if truncating at the right edge. */
18945 if (it->line_wrap == TRUNCATE
18946 && it->current_x >= it->last_visible_x)
18947 {
18948 /* Add truncation mark, but don't do it if the line is
18949 truncated at a padding space. */
18950 if (IT_CHARPOS (*it) < it->string_nchars)
18951 {
18952 if (!FRAME_WINDOW_P (it->f))
18953 {
18954 int i, n;
18955
18956 if (it->current_x > it->last_visible_x)
18957 {
18958 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18959 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18960 break;
18961 for (n = row->used[TEXT_AREA]; i < n; ++i)
18962 {
18963 row->used[TEXT_AREA] = i;
18964 produce_special_glyphs (it, IT_TRUNCATION);
18965 }
18966 }
18967 produce_special_glyphs (it, IT_TRUNCATION);
18968 }
18969 it->glyph_row->truncated_on_right_p = 1;
18970 }
18971 break;
18972 }
18973 }
18974
18975 /* Maybe insert a truncation at the left. */
18976 if (it->first_visible_x
18977 && IT_CHARPOS (*it) > 0)
18978 {
18979 if (!FRAME_WINDOW_P (it->f))
18980 insert_left_trunc_glyphs (it);
18981 it->glyph_row->truncated_on_left_p = 1;
18982 }
18983
18984 it->face_id = saved_face_id;
18985
18986 /* Value is number of columns displayed. */
18987 return it->hpos - hpos_at_start;
18988 }
18989
18990
18991 \f
18992 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18993 appears as an element of LIST or as the car of an element of LIST.
18994 If PROPVAL is a list, compare each element against LIST in that
18995 way, and return 1/2 if any element of PROPVAL is found in LIST.
18996 Otherwise return 0. This function cannot quit.
18997 The return value is 2 if the text is invisible but with an ellipsis
18998 and 1 if it's invisible and without an ellipsis. */
18999
19000 int
19001 invisible_p (propval, list)
19002 register Lisp_Object propval;
19003 Lisp_Object list;
19004 {
19005 register Lisp_Object tail, proptail;
19006
19007 for (tail = list; CONSP (tail); tail = XCDR (tail))
19008 {
19009 register Lisp_Object tem;
19010 tem = XCAR (tail);
19011 if (EQ (propval, tem))
19012 return 1;
19013 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19014 return NILP (XCDR (tem)) ? 1 : 2;
19015 }
19016
19017 if (CONSP (propval))
19018 {
19019 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19020 {
19021 Lisp_Object propelt;
19022 propelt = XCAR (proptail);
19023 for (tail = list; CONSP (tail); tail = XCDR (tail))
19024 {
19025 register Lisp_Object tem;
19026 tem = XCAR (tail);
19027 if (EQ (propelt, tem))
19028 return 1;
19029 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19030 return NILP (XCDR (tem)) ? 1 : 2;
19031 }
19032 }
19033 }
19034
19035 return 0;
19036 }
19037
19038 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19039 doc: /* Non-nil if the property makes the text invisible.
19040 POS-OR-PROP can be a marker or number, in which case it is taken to be
19041 a position in the current buffer and the value of the `invisible' property
19042 is checked; or it can be some other value, which is then presumed to be the
19043 value of the `invisible' property of the text of interest.
19044 The non-nil value returned can be t for truly invisible text or something
19045 else if the text is replaced by an ellipsis. */)
19046 (pos_or_prop)
19047 Lisp_Object pos_or_prop;
19048 {
19049 Lisp_Object prop
19050 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19051 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19052 : pos_or_prop);
19053 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19054 return (invis == 0 ? Qnil
19055 : invis == 1 ? Qt
19056 : make_number (invis));
19057 }
19058
19059 /* Calculate a width or height in pixels from a specification using
19060 the following elements:
19061
19062 SPEC ::=
19063 NUM - a (fractional) multiple of the default font width/height
19064 (NUM) - specifies exactly NUM pixels
19065 UNIT - a fixed number of pixels, see below.
19066 ELEMENT - size of a display element in pixels, see below.
19067 (NUM . SPEC) - equals NUM * SPEC
19068 (+ SPEC SPEC ...) - add pixel values
19069 (- SPEC SPEC ...) - subtract pixel values
19070 (- SPEC) - negate pixel value
19071
19072 NUM ::=
19073 INT or FLOAT - a number constant
19074 SYMBOL - use symbol's (buffer local) variable binding.
19075
19076 UNIT ::=
19077 in - pixels per inch *)
19078 mm - pixels per 1/1000 meter *)
19079 cm - pixels per 1/100 meter *)
19080 width - width of current font in pixels.
19081 height - height of current font in pixels.
19082
19083 *) using the ratio(s) defined in display-pixels-per-inch.
19084
19085 ELEMENT ::=
19086
19087 left-fringe - left fringe width in pixels
19088 right-fringe - right fringe width in pixels
19089
19090 left-margin - left margin width in pixels
19091 right-margin - right margin width in pixels
19092
19093 scroll-bar - scroll-bar area width in pixels
19094
19095 Examples:
19096
19097 Pixels corresponding to 5 inches:
19098 (5 . in)
19099
19100 Total width of non-text areas on left side of window (if scroll-bar is on left):
19101 '(space :width (+ left-fringe left-margin scroll-bar))
19102
19103 Align to first text column (in header line):
19104 '(space :align-to 0)
19105
19106 Align to middle of text area minus half the width of variable `my-image'
19107 containing a loaded image:
19108 '(space :align-to (0.5 . (- text my-image)))
19109
19110 Width of left margin minus width of 1 character in the default font:
19111 '(space :width (- left-margin 1))
19112
19113 Width of left margin minus width of 2 characters in the current font:
19114 '(space :width (- left-margin (2 . width)))
19115
19116 Center 1 character over left-margin (in header line):
19117 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19118
19119 Different ways to express width of left fringe plus left margin minus one pixel:
19120 '(space :width (- (+ left-fringe left-margin) (1)))
19121 '(space :width (+ left-fringe left-margin (- (1))))
19122 '(space :width (+ left-fringe left-margin (-1)))
19123
19124 */
19125
19126 #define NUMVAL(X) \
19127 ((INTEGERP (X) || FLOATP (X)) \
19128 ? XFLOATINT (X) \
19129 : - 1)
19130
19131 int
19132 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19133 double *res;
19134 struct it *it;
19135 Lisp_Object prop;
19136 struct font *font;
19137 int width_p, *align_to;
19138 {
19139 double pixels;
19140
19141 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19142 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19143
19144 if (NILP (prop))
19145 return OK_PIXELS (0);
19146
19147 xassert (FRAME_LIVE_P (it->f));
19148
19149 if (SYMBOLP (prop))
19150 {
19151 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19152 {
19153 char *unit = SDATA (SYMBOL_NAME (prop));
19154
19155 if (unit[0] == 'i' && unit[1] == 'n')
19156 pixels = 1.0;
19157 else if (unit[0] == 'm' && unit[1] == 'm')
19158 pixels = 25.4;
19159 else if (unit[0] == 'c' && unit[1] == 'm')
19160 pixels = 2.54;
19161 else
19162 pixels = 0;
19163 if (pixels > 0)
19164 {
19165 double ppi;
19166 #ifdef HAVE_WINDOW_SYSTEM
19167 if (FRAME_WINDOW_P (it->f)
19168 && (ppi = (width_p
19169 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19170 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19171 ppi > 0))
19172 return OK_PIXELS (ppi / pixels);
19173 #endif
19174
19175 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19176 || (CONSP (Vdisplay_pixels_per_inch)
19177 && (ppi = (width_p
19178 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19179 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19180 ppi > 0)))
19181 return OK_PIXELS (ppi / pixels);
19182
19183 return 0;
19184 }
19185 }
19186
19187 #ifdef HAVE_WINDOW_SYSTEM
19188 if (EQ (prop, Qheight))
19189 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19190 if (EQ (prop, Qwidth))
19191 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19192 #else
19193 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19194 return OK_PIXELS (1);
19195 #endif
19196
19197 if (EQ (prop, Qtext))
19198 return OK_PIXELS (width_p
19199 ? window_box_width (it->w, TEXT_AREA)
19200 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19201
19202 if (align_to && *align_to < 0)
19203 {
19204 *res = 0;
19205 if (EQ (prop, Qleft))
19206 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19207 if (EQ (prop, Qright))
19208 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19209 if (EQ (prop, Qcenter))
19210 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19211 + window_box_width (it->w, TEXT_AREA) / 2);
19212 if (EQ (prop, Qleft_fringe))
19213 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19214 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19215 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19216 if (EQ (prop, Qright_fringe))
19217 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19218 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19219 : window_box_right_offset (it->w, TEXT_AREA));
19220 if (EQ (prop, Qleft_margin))
19221 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19222 if (EQ (prop, Qright_margin))
19223 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19224 if (EQ (prop, Qscroll_bar))
19225 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19226 ? 0
19227 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19228 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19229 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19230 : 0)));
19231 }
19232 else
19233 {
19234 if (EQ (prop, Qleft_fringe))
19235 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19236 if (EQ (prop, Qright_fringe))
19237 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19238 if (EQ (prop, Qleft_margin))
19239 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19240 if (EQ (prop, Qright_margin))
19241 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19242 if (EQ (prop, Qscroll_bar))
19243 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19244 }
19245
19246 prop = Fbuffer_local_value (prop, it->w->buffer);
19247 }
19248
19249 if (INTEGERP (prop) || FLOATP (prop))
19250 {
19251 int base_unit = (width_p
19252 ? FRAME_COLUMN_WIDTH (it->f)
19253 : FRAME_LINE_HEIGHT (it->f));
19254 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19255 }
19256
19257 if (CONSP (prop))
19258 {
19259 Lisp_Object car = XCAR (prop);
19260 Lisp_Object cdr = XCDR (prop);
19261
19262 if (SYMBOLP (car))
19263 {
19264 #ifdef HAVE_WINDOW_SYSTEM
19265 if (FRAME_WINDOW_P (it->f)
19266 && valid_image_p (prop))
19267 {
19268 int id = lookup_image (it->f, prop);
19269 struct image *img = IMAGE_FROM_ID (it->f, id);
19270
19271 return OK_PIXELS (width_p ? img->width : img->height);
19272 }
19273 #endif
19274 if (EQ (car, Qplus) || EQ (car, Qminus))
19275 {
19276 int first = 1;
19277 double px;
19278
19279 pixels = 0;
19280 while (CONSP (cdr))
19281 {
19282 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19283 font, width_p, align_to))
19284 return 0;
19285 if (first)
19286 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19287 else
19288 pixels += px;
19289 cdr = XCDR (cdr);
19290 }
19291 if (EQ (car, Qminus))
19292 pixels = -pixels;
19293 return OK_PIXELS (pixels);
19294 }
19295
19296 car = Fbuffer_local_value (car, it->w->buffer);
19297 }
19298
19299 if (INTEGERP (car) || FLOATP (car))
19300 {
19301 double fact;
19302 pixels = XFLOATINT (car);
19303 if (NILP (cdr))
19304 return OK_PIXELS (pixels);
19305 if (calc_pixel_width_or_height (&fact, it, cdr,
19306 font, width_p, align_to))
19307 return OK_PIXELS (pixels * fact);
19308 return 0;
19309 }
19310
19311 return 0;
19312 }
19313
19314 return 0;
19315 }
19316
19317 \f
19318 /***********************************************************************
19319 Glyph Display
19320 ***********************************************************************/
19321
19322 #ifdef HAVE_WINDOW_SYSTEM
19323
19324 #if GLYPH_DEBUG
19325
19326 void
19327 dump_glyph_string (s)
19328 struct glyph_string *s;
19329 {
19330 fprintf (stderr, "glyph string\n");
19331 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19332 s->x, s->y, s->width, s->height);
19333 fprintf (stderr, " ybase = %d\n", s->ybase);
19334 fprintf (stderr, " hl = %d\n", s->hl);
19335 fprintf (stderr, " left overhang = %d, right = %d\n",
19336 s->left_overhang, s->right_overhang);
19337 fprintf (stderr, " nchars = %d\n", s->nchars);
19338 fprintf (stderr, " extends to end of line = %d\n",
19339 s->extends_to_end_of_line_p);
19340 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19341 fprintf (stderr, " bg width = %d\n", s->background_width);
19342 }
19343
19344 #endif /* GLYPH_DEBUG */
19345
19346 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19347 of XChar2b structures for S; it can't be allocated in
19348 init_glyph_string because it must be allocated via `alloca'. W
19349 is the window on which S is drawn. ROW and AREA are the glyph row
19350 and area within the row from which S is constructed. START is the
19351 index of the first glyph structure covered by S. HL is a
19352 face-override for drawing S. */
19353
19354 #ifdef HAVE_NTGUI
19355 #define OPTIONAL_HDC(hdc) hdc,
19356 #define DECLARE_HDC(hdc) HDC hdc;
19357 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19358 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19359 #endif
19360
19361 #ifndef OPTIONAL_HDC
19362 #define OPTIONAL_HDC(hdc)
19363 #define DECLARE_HDC(hdc)
19364 #define ALLOCATE_HDC(hdc, f)
19365 #define RELEASE_HDC(hdc, f)
19366 #endif
19367
19368 static void
19369 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19370 struct glyph_string *s;
19371 DECLARE_HDC (hdc)
19372 XChar2b *char2b;
19373 struct window *w;
19374 struct glyph_row *row;
19375 enum glyph_row_area area;
19376 int start;
19377 enum draw_glyphs_face hl;
19378 {
19379 bzero (s, sizeof *s);
19380 s->w = w;
19381 s->f = XFRAME (w->frame);
19382 #ifdef HAVE_NTGUI
19383 s->hdc = hdc;
19384 #endif
19385 s->display = FRAME_X_DISPLAY (s->f);
19386 s->window = FRAME_X_WINDOW (s->f);
19387 s->char2b = char2b;
19388 s->hl = hl;
19389 s->row = row;
19390 s->area = area;
19391 s->first_glyph = row->glyphs[area] + start;
19392 s->height = row->height;
19393 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19394
19395 /* Display the internal border below the tool-bar window. */
19396 if (WINDOWP (s->f->tool_bar_window)
19397 && s->w == XWINDOW (s->f->tool_bar_window))
19398 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19399
19400 s->ybase = s->y + row->ascent;
19401 }
19402
19403
19404 /* Append the list of glyph strings with head H and tail T to the list
19405 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19406
19407 static INLINE void
19408 append_glyph_string_lists (head, tail, h, t)
19409 struct glyph_string **head, **tail;
19410 struct glyph_string *h, *t;
19411 {
19412 if (h)
19413 {
19414 if (*head)
19415 (*tail)->next = h;
19416 else
19417 *head = h;
19418 h->prev = *tail;
19419 *tail = t;
19420 }
19421 }
19422
19423
19424 /* Prepend the list of glyph strings with head H and tail T to the
19425 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19426 result. */
19427
19428 static INLINE void
19429 prepend_glyph_string_lists (head, tail, h, t)
19430 struct glyph_string **head, **tail;
19431 struct glyph_string *h, *t;
19432 {
19433 if (h)
19434 {
19435 if (*head)
19436 (*head)->prev = t;
19437 else
19438 *tail = t;
19439 t->next = *head;
19440 *head = h;
19441 }
19442 }
19443
19444
19445 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19446 Set *HEAD and *TAIL to the resulting list. */
19447
19448 static INLINE void
19449 append_glyph_string (head, tail, s)
19450 struct glyph_string **head, **tail;
19451 struct glyph_string *s;
19452 {
19453 s->next = s->prev = NULL;
19454 append_glyph_string_lists (head, tail, s, s);
19455 }
19456
19457
19458 /* Get face and two-byte form of character C in face FACE_ID on frame
19459 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19460 means we want to display multibyte text. DISPLAY_P non-zero means
19461 make sure that X resources for the face returned are allocated.
19462 Value is a pointer to a realized face that is ready for display if
19463 DISPLAY_P is non-zero. */
19464
19465 static INLINE struct face *
19466 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19467 struct frame *f;
19468 int c, face_id;
19469 XChar2b *char2b;
19470 int multibyte_p, display_p;
19471 {
19472 struct face *face = FACE_FROM_ID (f, face_id);
19473
19474 if (face->font)
19475 {
19476 unsigned code = face->font->driver->encode_char (face->font, c);
19477
19478 if (code != FONT_INVALID_CODE)
19479 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19480 else
19481 STORE_XCHAR2B (char2b, 0, 0);
19482 }
19483
19484 /* Make sure X resources of the face are allocated. */
19485 #ifdef HAVE_X_WINDOWS
19486 if (display_p)
19487 #endif
19488 {
19489 xassert (face != NULL);
19490 PREPARE_FACE_FOR_DISPLAY (f, face);
19491 }
19492
19493 return face;
19494 }
19495
19496
19497 /* Get face and two-byte form of character glyph GLYPH on frame F.
19498 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19499 a pointer to a realized face that is ready for display. */
19500
19501 static INLINE struct face *
19502 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19503 struct frame *f;
19504 struct glyph *glyph;
19505 XChar2b *char2b;
19506 int *two_byte_p;
19507 {
19508 struct face *face;
19509
19510 xassert (glyph->type == CHAR_GLYPH);
19511 face = FACE_FROM_ID (f, glyph->face_id);
19512
19513 if (two_byte_p)
19514 *two_byte_p = 0;
19515
19516 if (face->font)
19517 {
19518 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19519
19520 if (code != FONT_INVALID_CODE)
19521 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19522 else
19523 STORE_XCHAR2B (char2b, 0, 0);
19524 }
19525
19526 /* Make sure X resources of the face are allocated. */
19527 xassert (face != NULL);
19528 PREPARE_FACE_FOR_DISPLAY (f, face);
19529 return face;
19530 }
19531
19532
19533 /* Fill glyph string S with composition components specified by S->cmp.
19534
19535 BASE_FACE is the base face of the composition.
19536 S->cmp_from is the index of the first component for S.
19537
19538 OVERLAPS non-zero means S should draw the foreground only, and use
19539 its physical height for clipping. See also draw_glyphs.
19540
19541 Value is the index of a component not in S. */
19542
19543 static int
19544 fill_composite_glyph_string (s, base_face, overlaps)
19545 struct glyph_string *s;
19546 struct face *base_face;
19547 int overlaps;
19548 {
19549 int i;
19550 /* For all glyphs of this composition, starting at the offset
19551 S->cmp_from, until we reach the end of the definition or encounter a
19552 glyph that requires the different face, add it to S. */
19553 struct face *face;
19554
19555 xassert (s);
19556
19557 s->for_overlaps = overlaps;
19558 s->face = NULL;
19559 s->font = NULL;
19560 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19561 {
19562 int c = COMPOSITION_GLYPH (s->cmp, i);
19563
19564 if (c != '\t')
19565 {
19566 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19567 -1, Qnil);
19568
19569 face = get_char_face_and_encoding (s->f, c, face_id,
19570 s->char2b + i, 1, 1);
19571 if (face)
19572 {
19573 if (! s->face)
19574 {
19575 s->face = face;
19576 s->font = s->face->font;
19577 }
19578 else if (s->face != face)
19579 break;
19580 }
19581 }
19582 ++s->nchars;
19583 }
19584 s->cmp_to = i;
19585
19586 /* All glyph strings for the same composition has the same width,
19587 i.e. the width set for the first component of the composition. */
19588 s->width = s->first_glyph->pixel_width;
19589
19590 /* If the specified font could not be loaded, use the frame's
19591 default font, but record the fact that we couldn't load it in
19592 the glyph string so that we can draw rectangles for the
19593 characters of the glyph string. */
19594 if (s->font == NULL)
19595 {
19596 s->font_not_found_p = 1;
19597 s->font = FRAME_FONT (s->f);
19598 }
19599
19600 /* Adjust base line for subscript/superscript text. */
19601 s->ybase += s->first_glyph->voffset;
19602
19603 /* This glyph string must always be drawn with 16-bit functions. */
19604 s->two_byte_p = 1;
19605
19606 return s->cmp_to;
19607 }
19608
19609 static int
19610 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19611 struct glyph_string *s;
19612 int face_id;
19613 int start, end, overlaps;
19614 {
19615 struct glyph *glyph, *last;
19616 Lisp_Object lgstring;
19617 int i;
19618
19619 s->for_overlaps = overlaps;
19620 glyph = s->row->glyphs[s->area] + start;
19621 last = s->row->glyphs[s->area] + end;
19622 s->cmp_id = glyph->u.cmp.id;
19623 s->cmp_from = glyph->u.cmp.from;
19624 s->cmp_to = glyph->u.cmp.to + 1;
19625 s->face = FACE_FROM_ID (s->f, face_id);
19626 lgstring = composition_gstring_from_id (s->cmp_id);
19627 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19628 glyph++;
19629 while (glyph < last
19630 && glyph->u.cmp.automatic
19631 && glyph->u.cmp.id == s->cmp_id
19632 && s->cmp_to == glyph->u.cmp.from)
19633 s->cmp_to = (glyph++)->u.cmp.to + 1;
19634
19635 for (i = s->cmp_from; i < s->cmp_to; i++)
19636 {
19637 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19638 unsigned code = LGLYPH_CODE (lglyph);
19639
19640 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19641 }
19642 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19643 return glyph - s->row->glyphs[s->area];
19644 }
19645
19646
19647 /* Fill glyph string S from a sequence of character glyphs.
19648
19649 FACE_ID is the face id of the string. START is the index of the
19650 first glyph to consider, END is the index of the last + 1.
19651 OVERLAPS non-zero means S should draw the foreground only, and use
19652 its physical height for clipping. See also draw_glyphs.
19653
19654 Value is the index of the first glyph not in S. */
19655
19656 static int
19657 fill_glyph_string (s, face_id, start, end, overlaps)
19658 struct glyph_string *s;
19659 int face_id;
19660 int start, end, overlaps;
19661 {
19662 struct glyph *glyph, *last;
19663 int voffset;
19664 int glyph_not_available_p;
19665
19666 xassert (s->f == XFRAME (s->w->frame));
19667 xassert (s->nchars == 0);
19668 xassert (start >= 0 && end > start);
19669
19670 s->for_overlaps = overlaps;
19671 glyph = s->row->glyphs[s->area] + start;
19672 last = s->row->glyphs[s->area] + end;
19673 voffset = glyph->voffset;
19674 s->padding_p = glyph->padding_p;
19675 glyph_not_available_p = glyph->glyph_not_available_p;
19676
19677 while (glyph < last
19678 && glyph->type == CHAR_GLYPH
19679 && glyph->voffset == voffset
19680 /* Same face id implies same font, nowadays. */
19681 && glyph->face_id == face_id
19682 && glyph->glyph_not_available_p == glyph_not_available_p)
19683 {
19684 int two_byte_p;
19685
19686 s->face = get_glyph_face_and_encoding (s->f, glyph,
19687 s->char2b + s->nchars,
19688 &two_byte_p);
19689 s->two_byte_p = two_byte_p;
19690 ++s->nchars;
19691 xassert (s->nchars <= end - start);
19692 s->width += glyph->pixel_width;
19693 if (glyph++->padding_p != s->padding_p)
19694 break;
19695 }
19696
19697 s->font = s->face->font;
19698
19699 /* If the specified font could not be loaded, use the frame's font,
19700 but record the fact that we couldn't load it in
19701 S->font_not_found_p so that we can draw rectangles for the
19702 characters of the glyph string. */
19703 if (s->font == NULL || glyph_not_available_p)
19704 {
19705 s->font_not_found_p = 1;
19706 s->font = FRAME_FONT (s->f);
19707 }
19708
19709 /* Adjust base line for subscript/superscript text. */
19710 s->ybase += voffset;
19711
19712 xassert (s->face && s->face->gc);
19713 return glyph - s->row->glyphs[s->area];
19714 }
19715
19716
19717 /* Fill glyph string S from image glyph S->first_glyph. */
19718
19719 static void
19720 fill_image_glyph_string (s)
19721 struct glyph_string *s;
19722 {
19723 xassert (s->first_glyph->type == IMAGE_GLYPH);
19724 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19725 xassert (s->img);
19726 s->slice = s->first_glyph->slice;
19727 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19728 s->font = s->face->font;
19729 s->width = s->first_glyph->pixel_width;
19730
19731 /* Adjust base line for subscript/superscript text. */
19732 s->ybase += s->first_glyph->voffset;
19733 }
19734
19735
19736 /* Fill glyph string S from a sequence of stretch glyphs.
19737
19738 ROW is the glyph row in which the glyphs are found, AREA is the
19739 area within the row. START is the index of the first glyph to
19740 consider, END is the index of the last + 1.
19741
19742 Value is the index of the first glyph not in S. */
19743
19744 static int
19745 fill_stretch_glyph_string (s, row, area, start, end)
19746 struct glyph_string *s;
19747 struct glyph_row *row;
19748 enum glyph_row_area area;
19749 int start, end;
19750 {
19751 struct glyph *glyph, *last;
19752 int voffset, face_id;
19753
19754 xassert (s->first_glyph->type == STRETCH_GLYPH);
19755
19756 glyph = s->row->glyphs[s->area] + start;
19757 last = s->row->glyphs[s->area] + end;
19758 face_id = glyph->face_id;
19759 s->face = FACE_FROM_ID (s->f, face_id);
19760 s->font = s->face->font;
19761 s->width = glyph->pixel_width;
19762 s->nchars = 1;
19763 voffset = glyph->voffset;
19764
19765 for (++glyph;
19766 (glyph < last
19767 && glyph->type == STRETCH_GLYPH
19768 && glyph->voffset == voffset
19769 && glyph->face_id == face_id);
19770 ++glyph)
19771 s->width += glyph->pixel_width;
19772
19773 /* Adjust base line for subscript/superscript text. */
19774 s->ybase += voffset;
19775
19776 /* The case that face->gc == 0 is handled when drawing the glyph
19777 string by calling PREPARE_FACE_FOR_DISPLAY. */
19778 xassert (s->face);
19779 return glyph - s->row->glyphs[s->area];
19780 }
19781
19782 static struct font_metrics *
19783 get_per_char_metric (f, font, char2b)
19784 struct frame *f;
19785 struct font *font;
19786 XChar2b *char2b;
19787 {
19788 static struct font_metrics metrics;
19789 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19790
19791 if (! font || code == FONT_INVALID_CODE)
19792 return NULL;
19793 font->driver->text_extents (font, &code, 1, &metrics);
19794 return &metrics;
19795 }
19796
19797 /* EXPORT for RIF:
19798 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19799 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19800 assumed to be zero. */
19801
19802 void
19803 x_get_glyph_overhangs (glyph, f, left, right)
19804 struct glyph *glyph;
19805 struct frame *f;
19806 int *left, *right;
19807 {
19808 *left = *right = 0;
19809
19810 if (glyph->type == CHAR_GLYPH)
19811 {
19812 struct face *face;
19813 XChar2b char2b;
19814 struct font_metrics *pcm;
19815
19816 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19817 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19818 {
19819 if (pcm->rbearing > pcm->width)
19820 *right = pcm->rbearing - pcm->width;
19821 if (pcm->lbearing < 0)
19822 *left = -pcm->lbearing;
19823 }
19824 }
19825 else if (glyph->type == COMPOSITE_GLYPH)
19826 {
19827 if (! glyph->u.cmp.automatic)
19828 {
19829 struct composition *cmp = composition_table[glyph->u.cmp.id];
19830
19831 if (cmp->rbearing > cmp->pixel_width)
19832 *right = cmp->rbearing - cmp->pixel_width;
19833 if (cmp->lbearing < 0)
19834 *left = - cmp->lbearing;
19835 }
19836 else
19837 {
19838 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19839 struct font_metrics metrics;
19840
19841 composition_gstring_width (gstring, glyph->u.cmp.from,
19842 glyph->u.cmp.to + 1, &metrics);
19843 if (metrics.rbearing > metrics.width)
19844 *right = metrics.rbearing - metrics.width;
19845 if (metrics.lbearing < 0)
19846 *left = - metrics.lbearing;
19847 }
19848 }
19849 }
19850
19851
19852 /* Return the index of the first glyph preceding glyph string S that
19853 is overwritten by S because of S's left overhang. Value is -1
19854 if no glyphs are overwritten. */
19855
19856 static int
19857 left_overwritten (s)
19858 struct glyph_string *s;
19859 {
19860 int k;
19861
19862 if (s->left_overhang)
19863 {
19864 int x = 0, i;
19865 struct glyph *glyphs = s->row->glyphs[s->area];
19866 int first = s->first_glyph - glyphs;
19867
19868 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19869 x -= glyphs[i].pixel_width;
19870
19871 k = i + 1;
19872 }
19873 else
19874 k = -1;
19875
19876 return k;
19877 }
19878
19879
19880 /* Return the index of the first glyph preceding glyph string S that
19881 is overwriting S because of its right overhang. Value is -1 if no
19882 glyph in front of S overwrites S. */
19883
19884 static int
19885 left_overwriting (s)
19886 struct glyph_string *s;
19887 {
19888 int i, k, x;
19889 struct glyph *glyphs = s->row->glyphs[s->area];
19890 int first = s->first_glyph - glyphs;
19891
19892 k = -1;
19893 x = 0;
19894 for (i = first - 1; i >= 0; --i)
19895 {
19896 int left, right;
19897 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19898 if (x + right > 0)
19899 k = i;
19900 x -= glyphs[i].pixel_width;
19901 }
19902
19903 return k;
19904 }
19905
19906
19907 /* Return the index of the last glyph following glyph string S that is
19908 overwritten by S because of S's right overhang. Value is -1 if
19909 no such glyph is found. */
19910
19911 static int
19912 right_overwritten (s)
19913 struct glyph_string *s;
19914 {
19915 int k = -1;
19916
19917 if (s->right_overhang)
19918 {
19919 int x = 0, i;
19920 struct glyph *glyphs = s->row->glyphs[s->area];
19921 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19922 int end = s->row->used[s->area];
19923
19924 for (i = first; i < end && s->right_overhang > x; ++i)
19925 x += glyphs[i].pixel_width;
19926
19927 k = i;
19928 }
19929
19930 return k;
19931 }
19932
19933
19934 /* Return the index of the last glyph following glyph string S that
19935 overwrites S because of its left overhang. Value is negative
19936 if no such glyph is found. */
19937
19938 static int
19939 right_overwriting (s)
19940 struct glyph_string *s;
19941 {
19942 int i, k, x;
19943 int end = s->row->used[s->area];
19944 struct glyph *glyphs = s->row->glyphs[s->area];
19945 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19946
19947 k = -1;
19948 x = 0;
19949 for (i = first; i < end; ++i)
19950 {
19951 int left, right;
19952 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19953 if (x - left < 0)
19954 k = i;
19955 x += glyphs[i].pixel_width;
19956 }
19957
19958 return k;
19959 }
19960
19961
19962 /* Set background width of glyph string S. START is the index of the
19963 first glyph following S. LAST_X is the right-most x-position + 1
19964 in the drawing area. */
19965
19966 static INLINE void
19967 set_glyph_string_background_width (s, start, last_x)
19968 struct glyph_string *s;
19969 int start;
19970 int last_x;
19971 {
19972 /* If the face of this glyph string has to be drawn to the end of
19973 the drawing area, set S->extends_to_end_of_line_p. */
19974
19975 if (start == s->row->used[s->area]
19976 && s->area == TEXT_AREA
19977 && ((s->row->fill_line_p
19978 && (s->hl == DRAW_NORMAL_TEXT
19979 || s->hl == DRAW_IMAGE_RAISED
19980 || s->hl == DRAW_IMAGE_SUNKEN))
19981 || s->hl == DRAW_MOUSE_FACE))
19982 s->extends_to_end_of_line_p = 1;
19983
19984 /* If S extends its face to the end of the line, set its
19985 background_width to the distance to the right edge of the drawing
19986 area. */
19987 if (s->extends_to_end_of_line_p)
19988 s->background_width = last_x - s->x + 1;
19989 else
19990 s->background_width = s->width;
19991 }
19992
19993
19994 /* Compute overhangs and x-positions for glyph string S and its
19995 predecessors, or successors. X is the starting x-position for S.
19996 BACKWARD_P non-zero means process predecessors. */
19997
19998 static void
19999 compute_overhangs_and_x (s, x, backward_p)
20000 struct glyph_string *s;
20001 int x;
20002 int backward_p;
20003 {
20004 if (backward_p)
20005 {
20006 while (s)
20007 {
20008 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20009 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20010 x -= s->width;
20011 s->x = x;
20012 s = s->prev;
20013 }
20014 }
20015 else
20016 {
20017 while (s)
20018 {
20019 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20020 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20021 s->x = x;
20022 x += s->width;
20023 s = s->next;
20024 }
20025 }
20026 }
20027
20028
20029
20030 /* The following macros are only called from draw_glyphs below.
20031 They reference the following parameters of that function directly:
20032 `w', `row', `area', and `overlap_p'
20033 as well as the following local variables:
20034 `s', `f', and `hdc' (in W32) */
20035
20036 #ifdef HAVE_NTGUI
20037 /* On W32, silently add local `hdc' variable to argument list of
20038 init_glyph_string. */
20039 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20040 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20041 #else
20042 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20043 init_glyph_string (s, char2b, w, row, area, start, hl)
20044 #endif
20045
20046 /* Add a glyph string for a stretch glyph to the list of strings
20047 between HEAD and TAIL. START is the index of the stretch glyph in
20048 row area AREA of glyph row ROW. END is the index of the last glyph
20049 in that glyph row area. X is the current output position assigned
20050 to the new glyph string constructed. HL overrides that face of the
20051 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20052 is the right-most x-position of the drawing area. */
20053
20054 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20055 and below -- keep them on one line. */
20056 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20057 do \
20058 { \
20059 s = (struct glyph_string *) alloca (sizeof *s); \
20060 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20061 START = fill_stretch_glyph_string (s, row, area, START, END); \
20062 append_glyph_string (&HEAD, &TAIL, s); \
20063 s->x = (X); \
20064 } \
20065 while (0)
20066
20067
20068 /* Add a glyph string for an image glyph to the list of strings
20069 between HEAD and TAIL. START is the index of the image glyph in
20070 row area AREA of glyph row ROW. END is the index of the last glyph
20071 in that glyph row area. X is the current output position assigned
20072 to the new glyph string constructed. HL overrides that face of the
20073 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20074 is the right-most x-position of the drawing area. */
20075
20076 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20077 do \
20078 { \
20079 s = (struct glyph_string *) alloca (sizeof *s); \
20080 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20081 fill_image_glyph_string (s); \
20082 append_glyph_string (&HEAD, &TAIL, s); \
20083 ++START; \
20084 s->x = (X); \
20085 } \
20086 while (0)
20087
20088
20089 /* Add a glyph string for a sequence of character glyphs to the list
20090 of strings between HEAD and TAIL. START is the index of the first
20091 glyph in row area AREA of glyph row ROW that is part of the new
20092 glyph string. END is the index of the last glyph in that glyph row
20093 area. X is the current output position assigned to the new glyph
20094 string constructed. HL overrides that face of the glyph; e.g. it
20095 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20096 right-most x-position of the drawing area. */
20097
20098 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20099 do \
20100 { \
20101 int face_id; \
20102 XChar2b *char2b; \
20103 \
20104 face_id = (row)->glyphs[area][START].face_id; \
20105 \
20106 s = (struct glyph_string *) alloca (sizeof *s); \
20107 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20108 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20109 append_glyph_string (&HEAD, &TAIL, s); \
20110 s->x = (X); \
20111 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20112 } \
20113 while (0)
20114
20115
20116 /* Add a glyph string for a composite sequence to the list of strings
20117 between HEAD and TAIL. START is the index of the first glyph in
20118 row area AREA of glyph row ROW that is part of the new glyph
20119 string. END is the index of the last glyph in that glyph row area.
20120 X is the current output position assigned to the new glyph string
20121 constructed. HL overrides that face of the glyph; e.g. it is
20122 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20123 x-position of the drawing area. */
20124
20125 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20126 do { \
20127 int face_id = (row)->glyphs[area][START].face_id; \
20128 struct face *base_face = FACE_FROM_ID (f, face_id); \
20129 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20130 struct composition *cmp = composition_table[cmp_id]; \
20131 XChar2b *char2b; \
20132 struct glyph_string *first_s; \
20133 int n; \
20134 \
20135 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20136 \
20137 /* Make glyph_strings for each glyph sequence that is drawable by \
20138 the same face, and append them to HEAD/TAIL. */ \
20139 for (n = 0; n < cmp->glyph_len;) \
20140 { \
20141 s = (struct glyph_string *) alloca (sizeof *s); \
20142 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20143 append_glyph_string (&(HEAD), &(TAIL), s); \
20144 s->cmp = cmp; \
20145 s->cmp_from = n; \
20146 s->x = (X); \
20147 if (n == 0) \
20148 first_s = s; \
20149 n = fill_composite_glyph_string (s, base_face, overlaps); \
20150 } \
20151 \
20152 ++START; \
20153 s = first_s; \
20154 } while (0)
20155
20156
20157 /* Add a glyph string for a glyph-string sequence to the list of strings
20158 between HEAD and TAIL. */
20159
20160 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20161 do { \
20162 int face_id; \
20163 XChar2b *char2b; \
20164 Lisp_Object gstring; \
20165 \
20166 face_id = (row)->glyphs[area][START].face_id; \
20167 gstring = (composition_gstring_from_id \
20168 ((row)->glyphs[area][START].u.cmp.id)); \
20169 s = (struct glyph_string *) alloca (sizeof *s); \
20170 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20171 * LGSTRING_GLYPH_LEN (gstring)); \
20172 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20173 append_glyph_string (&(HEAD), &(TAIL), s); \
20174 s->x = (X); \
20175 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20176 } while (0)
20177
20178
20179 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20180 of AREA of glyph row ROW on window W between indices START and END.
20181 HL overrides the face for drawing glyph strings, e.g. it is
20182 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20183 x-positions of the drawing area.
20184
20185 This is an ugly monster macro construct because we must use alloca
20186 to allocate glyph strings (because draw_glyphs can be called
20187 asynchronously). */
20188
20189 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20190 do \
20191 { \
20192 HEAD = TAIL = NULL; \
20193 while (START < END) \
20194 { \
20195 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20196 switch (first_glyph->type) \
20197 { \
20198 case CHAR_GLYPH: \
20199 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20200 HL, X, LAST_X); \
20201 break; \
20202 \
20203 case COMPOSITE_GLYPH: \
20204 if (first_glyph->u.cmp.automatic) \
20205 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20206 HL, X, LAST_X); \
20207 else \
20208 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20209 HL, X, LAST_X); \
20210 break; \
20211 \
20212 case STRETCH_GLYPH: \
20213 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20214 HL, X, LAST_X); \
20215 break; \
20216 \
20217 case IMAGE_GLYPH: \
20218 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20219 HL, X, LAST_X); \
20220 break; \
20221 \
20222 default: \
20223 abort (); \
20224 } \
20225 \
20226 if (s) \
20227 { \
20228 set_glyph_string_background_width (s, START, LAST_X); \
20229 (X) += s->width; \
20230 } \
20231 } \
20232 } while (0)
20233
20234
20235 /* Draw glyphs between START and END in AREA of ROW on window W,
20236 starting at x-position X. X is relative to AREA in W. HL is a
20237 face-override with the following meaning:
20238
20239 DRAW_NORMAL_TEXT draw normally
20240 DRAW_CURSOR draw in cursor face
20241 DRAW_MOUSE_FACE draw in mouse face.
20242 DRAW_INVERSE_VIDEO draw in mode line face
20243 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20244 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20245
20246 If OVERLAPS is non-zero, draw only the foreground of characters and
20247 clip to the physical height of ROW. Non-zero value also defines
20248 the overlapping part to be drawn:
20249
20250 OVERLAPS_PRED overlap with preceding rows
20251 OVERLAPS_SUCC overlap with succeeding rows
20252 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20253 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20254
20255 Value is the x-position reached, relative to AREA of W. */
20256
20257 static int
20258 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20259 struct window *w;
20260 int x;
20261 struct glyph_row *row;
20262 enum glyph_row_area area;
20263 EMACS_INT start, end;
20264 enum draw_glyphs_face hl;
20265 int overlaps;
20266 {
20267 struct glyph_string *head, *tail;
20268 struct glyph_string *s;
20269 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20270 int i, j, x_reached, last_x, area_left = 0;
20271 struct frame *f = XFRAME (WINDOW_FRAME (w));
20272 DECLARE_HDC (hdc);
20273
20274 ALLOCATE_HDC (hdc, f);
20275
20276 /* Let's rather be paranoid than getting a SEGV. */
20277 end = min (end, row->used[area]);
20278 start = max (0, start);
20279 start = min (end, start);
20280
20281 /* Translate X to frame coordinates. Set last_x to the right
20282 end of the drawing area. */
20283 if (row->full_width_p)
20284 {
20285 /* X is relative to the left edge of W, without scroll bars
20286 or fringes. */
20287 area_left = WINDOW_LEFT_EDGE_X (w);
20288 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20289 }
20290 else
20291 {
20292 area_left = window_box_left (w, area);
20293 last_x = area_left + window_box_width (w, area);
20294 }
20295 x += area_left;
20296
20297 /* Build a doubly-linked list of glyph_string structures between
20298 head and tail from what we have to draw. Note that the macro
20299 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20300 the reason we use a separate variable `i'. */
20301 i = start;
20302 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20303 if (tail)
20304 x_reached = tail->x + tail->background_width;
20305 else
20306 x_reached = x;
20307
20308 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20309 the row, redraw some glyphs in front or following the glyph
20310 strings built above. */
20311 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20312 {
20313 struct glyph_string *h, *t;
20314 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20315 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20316 int dummy_x = 0;
20317
20318 /* If mouse highlighting is on, we may need to draw adjacent
20319 glyphs using mouse-face highlighting. */
20320 if (area == TEXT_AREA && row->mouse_face_p)
20321 {
20322 struct glyph_row *mouse_beg_row, *mouse_end_row;
20323
20324 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20325 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20326
20327 if (row >= mouse_beg_row && row <= mouse_end_row)
20328 {
20329 check_mouse_face = 1;
20330 mouse_beg_col = (row == mouse_beg_row)
20331 ? dpyinfo->mouse_face_beg_col : 0;
20332 mouse_end_col = (row == mouse_end_row)
20333 ? dpyinfo->mouse_face_end_col
20334 : row->used[TEXT_AREA];
20335 }
20336 }
20337
20338 /* Compute overhangs for all glyph strings. */
20339 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20340 for (s = head; s; s = s->next)
20341 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20342
20343 /* Prepend glyph strings for glyphs in front of the first glyph
20344 string that are overwritten because of the first glyph
20345 string's left overhang. The background of all strings
20346 prepended must be drawn because the first glyph string
20347 draws over it. */
20348 i = left_overwritten (head);
20349 if (i >= 0)
20350 {
20351 enum draw_glyphs_face overlap_hl;
20352
20353 /* If this row contains mouse highlighting, attempt to draw
20354 the overlapped glyphs with the correct highlight. This
20355 code fails if the overlap encompasses more than one glyph
20356 and mouse-highlight spans only some of these glyphs.
20357 However, making it work perfectly involves a lot more
20358 code, and I don't know if the pathological case occurs in
20359 practice, so we'll stick to this for now. --- cyd */
20360 if (check_mouse_face
20361 && mouse_beg_col < start && mouse_end_col > i)
20362 overlap_hl = DRAW_MOUSE_FACE;
20363 else
20364 overlap_hl = DRAW_NORMAL_TEXT;
20365
20366 j = i;
20367 BUILD_GLYPH_STRINGS (j, start, h, t,
20368 overlap_hl, dummy_x, last_x);
20369 compute_overhangs_and_x (t, head->x, 1);
20370 prepend_glyph_string_lists (&head, &tail, h, t);
20371 clip_head = head;
20372 }
20373
20374 /* Prepend glyph strings for glyphs in front of the first glyph
20375 string that overwrite that glyph string because of their
20376 right overhang. For these strings, only the foreground must
20377 be drawn, because it draws over the glyph string at `head'.
20378 The background must not be drawn because this would overwrite
20379 right overhangs of preceding glyphs for which no glyph
20380 strings exist. */
20381 i = left_overwriting (head);
20382 if (i >= 0)
20383 {
20384 enum draw_glyphs_face overlap_hl;
20385
20386 if (check_mouse_face
20387 && mouse_beg_col < start && mouse_end_col > i)
20388 overlap_hl = DRAW_MOUSE_FACE;
20389 else
20390 overlap_hl = DRAW_NORMAL_TEXT;
20391
20392 clip_head = head;
20393 BUILD_GLYPH_STRINGS (i, start, h, t,
20394 overlap_hl, dummy_x, last_x);
20395 for (s = h; s; s = s->next)
20396 s->background_filled_p = 1;
20397 compute_overhangs_and_x (t, head->x, 1);
20398 prepend_glyph_string_lists (&head, &tail, h, t);
20399 }
20400
20401 /* Append glyphs strings for glyphs following the last glyph
20402 string tail that are overwritten by tail. The background of
20403 these strings has to be drawn because tail's foreground draws
20404 over it. */
20405 i = right_overwritten (tail);
20406 if (i >= 0)
20407 {
20408 enum draw_glyphs_face overlap_hl;
20409
20410 if (check_mouse_face
20411 && mouse_beg_col < i && mouse_end_col > end)
20412 overlap_hl = DRAW_MOUSE_FACE;
20413 else
20414 overlap_hl = DRAW_NORMAL_TEXT;
20415
20416 BUILD_GLYPH_STRINGS (end, i, h, t,
20417 overlap_hl, x, last_x);
20418 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20419 append_glyph_string_lists (&head, &tail, h, t);
20420 clip_tail = tail;
20421 }
20422
20423 /* Append glyph strings for glyphs following the last glyph
20424 string tail that overwrite tail. The foreground of such
20425 glyphs has to be drawn because it writes into the background
20426 of tail. The background must not be drawn because it could
20427 paint over the foreground of following glyphs. */
20428 i = right_overwriting (tail);
20429 if (i >= 0)
20430 {
20431 enum draw_glyphs_face overlap_hl;
20432 if (check_mouse_face
20433 && mouse_beg_col < i && mouse_end_col > end)
20434 overlap_hl = DRAW_MOUSE_FACE;
20435 else
20436 overlap_hl = DRAW_NORMAL_TEXT;
20437
20438 clip_tail = tail;
20439 i++; /* We must include the Ith glyph. */
20440 BUILD_GLYPH_STRINGS (end, i, h, t,
20441 overlap_hl, x, last_x);
20442 for (s = h; s; s = s->next)
20443 s->background_filled_p = 1;
20444 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20445 append_glyph_string_lists (&head, &tail, h, t);
20446 }
20447 if (clip_head || clip_tail)
20448 for (s = head; s; s = s->next)
20449 {
20450 s->clip_head = clip_head;
20451 s->clip_tail = clip_tail;
20452 }
20453 }
20454
20455 /* Draw all strings. */
20456 for (s = head; s; s = s->next)
20457 FRAME_RIF (f)->draw_glyph_string (s);
20458
20459 #ifndef HAVE_NS
20460 /* When focus a sole frame and move horizontally, this sets on_p to 0
20461 causing a failure to erase prev cursor position. */
20462 if (area == TEXT_AREA
20463 && !row->full_width_p
20464 /* When drawing overlapping rows, only the glyph strings'
20465 foreground is drawn, which doesn't erase a cursor
20466 completely. */
20467 && !overlaps)
20468 {
20469 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20470 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20471 : (tail ? tail->x + tail->background_width : x));
20472 x0 -= area_left;
20473 x1 -= area_left;
20474
20475 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20476 row->y, MATRIX_ROW_BOTTOM_Y (row));
20477 }
20478 #endif
20479
20480 /* Value is the x-position up to which drawn, relative to AREA of W.
20481 This doesn't include parts drawn because of overhangs. */
20482 if (row->full_width_p)
20483 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20484 else
20485 x_reached -= area_left;
20486
20487 RELEASE_HDC (hdc, f);
20488
20489 return x_reached;
20490 }
20491
20492 /* Expand row matrix if too narrow. Don't expand if area
20493 is not present. */
20494
20495 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20496 { \
20497 if (!fonts_changed_p \
20498 && (it->glyph_row->glyphs[area] \
20499 < it->glyph_row->glyphs[area + 1])) \
20500 { \
20501 it->w->ncols_scale_factor++; \
20502 fonts_changed_p = 1; \
20503 } \
20504 }
20505
20506 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20507 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20508
20509 static INLINE void
20510 append_glyph (it)
20511 struct it *it;
20512 {
20513 struct glyph *glyph;
20514 enum glyph_row_area area = it->area;
20515
20516 xassert (it->glyph_row);
20517 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20518
20519 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20520 if (glyph < it->glyph_row->glyphs[area + 1])
20521 {
20522 glyph->charpos = CHARPOS (it->position);
20523 glyph->object = it->object;
20524 if (it->pixel_width > 0)
20525 {
20526 glyph->pixel_width = it->pixel_width;
20527 glyph->padding_p = 0;
20528 }
20529 else
20530 {
20531 /* Assure at least 1-pixel width. Otherwise, cursor can't
20532 be displayed correctly. */
20533 glyph->pixel_width = 1;
20534 glyph->padding_p = 1;
20535 }
20536 glyph->ascent = it->ascent;
20537 glyph->descent = it->descent;
20538 glyph->voffset = it->voffset;
20539 glyph->type = CHAR_GLYPH;
20540 glyph->avoid_cursor_p = it->avoid_cursor_p;
20541 glyph->multibyte_p = it->multibyte_p;
20542 glyph->left_box_line_p = it->start_of_box_run_p;
20543 glyph->right_box_line_p = it->end_of_box_run_p;
20544 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20545 || it->phys_descent > it->descent);
20546 glyph->glyph_not_available_p = it->glyph_not_available_p;
20547 glyph->face_id = it->face_id;
20548 glyph->u.ch = it->char_to_display;
20549 glyph->slice = null_glyph_slice;
20550 glyph->font_type = FONT_TYPE_UNKNOWN;
20551 ++it->glyph_row->used[area];
20552 }
20553 else
20554 IT_EXPAND_MATRIX_WIDTH (it, area);
20555 }
20556
20557 /* Store one glyph for the composition IT->cmp_it.id in
20558 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20559 non-null. */
20560
20561 static INLINE void
20562 append_composite_glyph (it)
20563 struct it *it;
20564 {
20565 struct glyph *glyph;
20566 enum glyph_row_area area = it->area;
20567
20568 xassert (it->glyph_row);
20569
20570 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20571 if (glyph < it->glyph_row->glyphs[area + 1])
20572 {
20573 glyph->charpos = CHARPOS (it->position);
20574 glyph->object = it->object;
20575 glyph->pixel_width = it->pixel_width;
20576 glyph->ascent = it->ascent;
20577 glyph->descent = it->descent;
20578 glyph->voffset = it->voffset;
20579 glyph->type = COMPOSITE_GLYPH;
20580 if (it->cmp_it.ch < 0)
20581 {
20582 glyph->u.cmp.automatic = 0;
20583 glyph->u.cmp.id = it->cmp_it.id;
20584 }
20585 else
20586 {
20587 glyph->u.cmp.automatic = 1;
20588 glyph->u.cmp.id = it->cmp_it.id;
20589 glyph->u.cmp.from = it->cmp_it.from;
20590 glyph->u.cmp.to = it->cmp_it.to - 1;
20591 }
20592 glyph->avoid_cursor_p = it->avoid_cursor_p;
20593 glyph->multibyte_p = it->multibyte_p;
20594 glyph->left_box_line_p = it->start_of_box_run_p;
20595 glyph->right_box_line_p = it->end_of_box_run_p;
20596 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20597 || it->phys_descent > it->descent);
20598 glyph->padding_p = 0;
20599 glyph->glyph_not_available_p = 0;
20600 glyph->face_id = it->face_id;
20601 glyph->slice = null_glyph_slice;
20602 glyph->font_type = FONT_TYPE_UNKNOWN;
20603 ++it->glyph_row->used[area];
20604 }
20605 else
20606 IT_EXPAND_MATRIX_WIDTH (it, area);
20607 }
20608
20609
20610 /* Change IT->ascent and IT->height according to the setting of
20611 IT->voffset. */
20612
20613 static INLINE void
20614 take_vertical_position_into_account (it)
20615 struct it *it;
20616 {
20617 if (it->voffset)
20618 {
20619 if (it->voffset < 0)
20620 /* Increase the ascent so that we can display the text higher
20621 in the line. */
20622 it->ascent -= it->voffset;
20623 else
20624 /* Increase the descent so that we can display the text lower
20625 in the line. */
20626 it->descent += it->voffset;
20627 }
20628 }
20629
20630
20631 /* Produce glyphs/get display metrics for the image IT is loaded with.
20632 See the description of struct display_iterator in dispextern.h for
20633 an overview of struct display_iterator. */
20634
20635 static void
20636 produce_image_glyph (it)
20637 struct it *it;
20638 {
20639 struct image *img;
20640 struct face *face;
20641 int glyph_ascent, crop;
20642 struct glyph_slice slice;
20643
20644 xassert (it->what == IT_IMAGE);
20645
20646 face = FACE_FROM_ID (it->f, it->face_id);
20647 xassert (face);
20648 /* Make sure X resources of the face is loaded. */
20649 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20650
20651 if (it->image_id < 0)
20652 {
20653 /* Fringe bitmap. */
20654 it->ascent = it->phys_ascent = 0;
20655 it->descent = it->phys_descent = 0;
20656 it->pixel_width = 0;
20657 it->nglyphs = 0;
20658 return;
20659 }
20660
20661 img = IMAGE_FROM_ID (it->f, it->image_id);
20662 xassert (img);
20663 /* Make sure X resources of the image is loaded. */
20664 prepare_image_for_display (it->f, img);
20665
20666 slice.x = slice.y = 0;
20667 slice.width = img->width;
20668 slice.height = img->height;
20669
20670 if (INTEGERP (it->slice.x))
20671 slice.x = XINT (it->slice.x);
20672 else if (FLOATP (it->slice.x))
20673 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20674
20675 if (INTEGERP (it->slice.y))
20676 slice.y = XINT (it->slice.y);
20677 else if (FLOATP (it->slice.y))
20678 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20679
20680 if (INTEGERP (it->slice.width))
20681 slice.width = XINT (it->slice.width);
20682 else if (FLOATP (it->slice.width))
20683 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20684
20685 if (INTEGERP (it->slice.height))
20686 slice.height = XINT (it->slice.height);
20687 else if (FLOATP (it->slice.height))
20688 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20689
20690 if (slice.x >= img->width)
20691 slice.x = img->width;
20692 if (slice.y >= img->height)
20693 slice.y = img->height;
20694 if (slice.x + slice.width >= img->width)
20695 slice.width = img->width - slice.x;
20696 if (slice.y + slice.height > img->height)
20697 slice.height = img->height - slice.y;
20698
20699 if (slice.width == 0 || slice.height == 0)
20700 return;
20701
20702 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20703
20704 it->descent = slice.height - glyph_ascent;
20705 if (slice.y == 0)
20706 it->descent += img->vmargin;
20707 if (slice.y + slice.height == img->height)
20708 it->descent += img->vmargin;
20709 it->phys_descent = it->descent;
20710
20711 it->pixel_width = slice.width;
20712 if (slice.x == 0)
20713 it->pixel_width += img->hmargin;
20714 if (slice.x + slice.width == img->width)
20715 it->pixel_width += img->hmargin;
20716
20717 /* It's quite possible for images to have an ascent greater than
20718 their height, so don't get confused in that case. */
20719 if (it->descent < 0)
20720 it->descent = 0;
20721
20722 it->nglyphs = 1;
20723
20724 if (face->box != FACE_NO_BOX)
20725 {
20726 if (face->box_line_width > 0)
20727 {
20728 if (slice.y == 0)
20729 it->ascent += face->box_line_width;
20730 if (slice.y + slice.height == img->height)
20731 it->descent += face->box_line_width;
20732 }
20733
20734 if (it->start_of_box_run_p && slice.x == 0)
20735 it->pixel_width += eabs (face->box_line_width);
20736 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20737 it->pixel_width += eabs (face->box_line_width);
20738 }
20739
20740 take_vertical_position_into_account (it);
20741
20742 /* Automatically crop wide image glyphs at right edge so we can
20743 draw the cursor on same display row. */
20744 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20745 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20746 {
20747 it->pixel_width -= crop;
20748 slice.width -= crop;
20749 }
20750
20751 if (it->glyph_row)
20752 {
20753 struct glyph *glyph;
20754 enum glyph_row_area area = it->area;
20755
20756 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20757 if (glyph < it->glyph_row->glyphs[area + 1])
20758 {
20759 glyph->charpos = CHARPOS (it->position);
20760 glyph->object = it->object;
20761 glyph->pixel_width = it->pixel_width;
20762 glyph->ascent = glyph_ascent;
20763 glyph->descent = it->descent;
20764 glyph->voffset = it->voffset;
20765 glyph->type = IMAGE_GLYPH;
20766 glyph->avoid_cursor_p = it->avoid_cursor_p;
20767 glyph->multibyte_p = it->multibyte_p;
20768 glyph->left_box_line_p = it->start_of_box_run_p;
20769 glyph->right_box_line_p = it->end_of_box_run_p;
20770 glyph->overlaps_vertically_p = 0;
20771 glyph->padding_p = 0;
20772 glyph->glyph_not_available_p = 0;
20773 glyph->face_id = it->face_id;
20774 glyph->u.img_id = img->id;
20775 glyph->slice = slice;
20776 glyph->font_type = FONT_TYPE_UNKNOWN;
20777 ++it->glyph_row->used[area];
20778 }
20779 else
20780 IT_EXPAND_MATRIX_WIDTH (it, area);
20781 }
20782 }
20783
20784
20785 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20786 of the glyph, WIDTH and HEIGHT are the width and height of the
20787 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20788
20789 static void
20790 append_stretch_glyph (it, object, width, height, ascent)
20791 struct it *it;
20792 Lisp_Object object;
20793 int width, height;
20794 int ascent;
20795 {
20796 struct glyph *glyph;
20797 enum glyph_row_area area = it->area;
20798
20799 xassert (ascent >= 0 && ascent <= height);
20800
20801 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20802 if (glyph < it->glyph_row->glyphs[area + 1])
20803 {
20804 glyph->charpos = CHARPOS (it->position);
20805 glyph->object = object;
20806 glyph->pixel_width = width;
20807 glyph->ascent = ascent;
20808 glyph->descent = height - ascent;
20809 glyph->voffset = it->voffset;
20810 glyph->type = STRETCH_GLYPH;
20811 glyph->avoid_cursor_p = it->avoid_cursor_p;
20812 glyph->multibyte_p = it->multibyte_p;
20813 glyph->left_box_line_p = it->start_of_box_run_p;
20814 glyph->right_box_line_p = it->end_of_box_run_p;
20815 glyph->overlaps_vertically_p = 0;
20816 glyph->padding_p = 0;
20817 glyph->glyph_not_available_p = 0;
20818 glyph->face_id = it->face_id;
20819 glyph->u.stretch.ascent = ascent;
20820 glyph->u.stretch.height = height;
20821 glyph->slice = null_glyph_slice;
20822 glyph->font_type = FONT_TYPE_UNKNOWN;
20823 ++it->glyph_row->used[area];
20824 }
20825 else
20826 IT_EXPAND_MATRIX_WIDTH (it, area);
20827 }
20828
20829
20830 /* Produce a stretch glyph for iterator IT. IT->object is the value
20831 of the glyph property displayed. The value must be a list
20832 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20833 being recognized:
20834
20835 1. `:width WIDTH' specifies that the space should be WIDTH *
20836 canonical char width wide. WIDTH may be an integer or floating
20837 point number.
20838
20839 2. `:relative-width FACTOR' specifies that the width of the stretch
20840 should be computed from the width of the first character having the
20841 `glyph' property, and should be FACTOR times that width.
20842
20843 3. `:align-to HPOS' specifies that the space should be wide enough
20844 to reach HPOS, a value in canonical character units.
20845
20846 Exactly one of the above pairs must be present.
20847
20848 4. `:height HEIGHT' specifies that the height of the stretch produced
20849 should be HEIGHT, measured in canonical character units.
20850
20851 5. `:relative-height FACTOR' specifies that the height of the
20852 stretch should be FACTOR times the height of the characters having
20853 the glyph property.
20854
20855 Either none or exactly one of 4 or 5 must be present.
20856
20857 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20858 of the stretch should be used for the ascent of the stretch.
20859 ASCENT must be in the range 0 <= ASCENT <= 100. */
20860
20861 static void
20862 produce_stretch_glyph (it)
20863 struct it *it;
20864 {
20865 /* (space :width WIDTH :height HEIGHT ...) */
20866 Lisp_Object prop, plist;
20867 int width = 0, height = 0, align_to = -1;
20868 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20869 int ascent = 0;
20870 double tem;
20871 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20872 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20873
20874 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20875
20876 /* List should start with `space'. */
20877 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20878 plist = XCDR (it->object);
20879
20880 /* Compute the width of the stretch. */
20881 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20882 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20883 {
20884 /* Absolute width `:width WIDTH' specified and valid. */
20885 zero_width_ok_p = 1;
20886 width = (int)tem;
20887 }
20888 else if (prop = Fplist_get (plist, QCrelative_width),
20889 NUMVAL (prop) > 0)
20890 {
20891 /* Relative width `:relative-width FACTOR' specified and valid.
20892 Compute the width of the characters having the `glyph'
20893 property. */
20894 struct it it2;
20895 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20896
20897 it2 = *it;
20898 if (it->multibyte_p)
20899 {
20900 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20901 - IT_BYTEPOS (*it));
20902 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20903 }
20904 else
20905 it2.c = *p, it2.len = 1;
20906
20907 it2.glyph_row = NULL;
20908 it2.what = IT_CHARACTER;
20909 x_produce_glyphs (&it2);
20910 width = NUMVAL (prop) * it2.pixel_width;
20911 }
20912 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20913 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20914 {
20915 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20916 align_to = (align_to < 0
20917 ? 0
20918 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20919 else if (align_to < 0)
20920 align_to = window_box_left_offset (it->w, TEXT_AREA);
20921 width = max (0, (int)tem + align_to - it->current_x);
20922 zero_width_ok_p = 1;
20923 }
20924 else
20925 /* Nothing specified -> width defaults to canonical char width. */
20926 width = FRAME_COLUMN_WIDTH (it->f);
20927
20928 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20929 width = 1;
20930
20931 /* Compute height. */
20932 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20933 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20934 {
20935 height = (int)tem;
20936 zero_height_ok_p = 1;
20937 }
20938 else if (prop = Fplist_get (plist, QCrelative_height),
20939 NUMVAL (prop) > 0)
20940 height = FONT_HEIGHT (font) * NUMVAL (prop);
20941 else
20942 height = FONT_HEIGHT (font);
20943
20944 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20945 height = 1;
20946
20947 /* Compute percentage of height used for ascent. If
20948 `:ascent ASCENT' is present and valid, use that. Otherwise,
20949 derive the ascent from the font in use. */
20950 if (prop = Fplist_get (plist, QCascent),
20951 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20952 ascent = height * NUMVAL (prop) / 100.0;
20953 else if (!NILP (prop)
20954 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20955 ascent = min (max (0, (int)tem), height);
20956 else
20957 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20958
20959 if (width > 0 && it->line_wrap != TRUNCATE
20960 && it->current_x + width > it->last_visible_x)
20961 width = it->last_visible_x - it->current_x - 1;
20962
20963 if (width > 0 && height > 0 && it->glyph_row)
20964 {
20965 Lisp_Object object = it->stack[it->sp - 1].string;
20966 if (!STRINGP (object))
20967 object = it->w->buffer;
20968 append_stretch_glyph (it, object, width, height, ascent);
20969 }
20970
20971 it->pixel_width = width;
20972 it->ascent = it->phys_ascent = ascent;
20973 it->descent = it->phys_descent = height - it->ascent;
20974 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20975
20976 take_vertical_position_into_account (it);
20977 }
20978
20979 /* Calculate line-height and line-spacing properties.
20980 An integer value specifies explicit pixel value.
20981 A float value specifies relative value to current face height.
20982 A cons (float . face-name) specifies relative value to
20983 height of specified face font.
20984
20985 Returns height in pixels, or nil. */
20986
20987
20988 static Lisp_Object
20989 calc_line_height_property (it, val, font, boff, override)
20990 struct it *it;
20991 Lisp_Object val;
20992 struct font *font;
20993 int boff, override;
20994 {
20995 Lisp_Object face_name = Qnil;
20996 int ascent, descent, height;
20997
20998 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20999 return val;
21000
21001 if (CONSP (val))
21002 {
21003 face_name = XCAR (val);
21004 val = XCDR (val);
21005 if (!NUMBERP (val))
21006 val = make_number (1);
21007 if (NILP (face_name))
21008 {
21009 height = it->ascent + it->descent;
21010 goto scale;
21011 }
21012 }
21013
21014 if (NILP (face_name))
21015 {
21016 font = FRAME_FONT (it->f);
21017 boff = FRAME_BASELINE_OFFSET (it->f);
21018 }
21019 else if (EQ (face_name, Qt))
21020 {
21021 override = 0;
21022 }
21023 else
21024 {
21025 int face_id;
21026 struct face *face;
21027
21028 face_id = lookup_named_face (it->f, face_name, 0);
21029 if (face_id < 0)
21030 return make_number (-1);
21031
21032 face = FACE_FROM_ID (it->f, face_id);
21033 font = face->font;
21034 if (font == NULL)
21035 return make_number (-1);
21036 boff = font->baseline_offset;
21037 if (font->vertical_centering)
21038 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21039 }
21040
21041 ascent = FONT_BASE (font) + boff;
21042 descent = FONT_DESCENT (font) - boff;
21043
21044 if (override)
21045 {
21046 it->override_ascent = ascent;
21047 it->override_descent = descent;
21048 it->override_boff = boff;
21049 }
21050
21051 height = ascent + descent;
21052
21053 scale:
21054 if (FLOATP (val))
21055 height = (int)(XFLOAT_DATA (val) * height);
21056 else if (INTEGERP (val))
21057 height *= XINT (val);
21058
21059 return make_number (height);
21060 }
21061
21062
21063 /* RIF:
21064 Produce glyphs/get display metrics for the display element IT is
21065 loaded with. See the description of struct it in dispextern.h
21066 for an overview of struct it. */
21067
21068 void
21069 x_produce_glyphs (it)
21070 struct it *it;
21071 {
21072 int extra_line_spacing = it->extra_line_spacing;
21073
21074 it->glyph_not_available_p = 0;
21075
21076 if (it->what == IT_CHARACTER)
21077 {
21078 XChar2b char2b;
21079 struct font *font;
21080 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21081 struct font_metrics *pcm;
21082 int font_not_found_p;
21083 int boff; /* baseline offset */
21084 /* We may change it->multibyte_p upon unibyte<->multibyte
21085 conversion. So, save the current value now and restore it
21086 later.
21087
21088 Note: It seems that we don't have to record multibyte_p in
21089 struct glyph because the character code itself tells if or
21090 not the character is multibyte. Thus, in the future, we must
21091 consider eliminating the field `multibyte_p' in the struct
21092 glyph. */
21093 int saved_multibyte_p = it->multibyte_p;
21094
21095 /* Maybe translate single-byte characters to multibyte, or the
21096 other way. */
21097 it->char_to_display = it->c;
21098 if (!ASCII_BYTE_P (it->c)
21099 && ! it->multibyte_p)
21100 {
21101 if (SINGLE_BYTE_CHAR_P (it->c)
21102 && unibyte_display_via_language_environment)
21103 {
21104 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21105
21106 /* get_next_display_element assures that this decoding
21107 never fails. */
21108 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21109 it->multibyte_p = 1;
21110 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21111 -1, Qnil);
21112 face = FACE_FROM_ID (it->f, it->face_id);
21113 }
21114 }
21115
21116 /* Get font to use. Encode IT->char_to_display. */
21117 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21118 &char2b, it->multibyte_p, 0);
21119 font = face->font;
21120
21121 /* When no suitable font found, use the default font. */
21122 font_not_found_p = font == NULL;
21123 if (font_not_found_p)
21124 {
21125 font = FRAME_FONT (it->f);
21126 boff = FRAME_BASELINE_OFFSET (it->f);
21127 }
21128 else
21129 {
21130 boff = font->baseline_offset;
21131 if (font->vertical_centering)
21132 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21133 }
21134
21135 if (it->char_to_display >= ' '
21136 && (!it->multibyte_p || it->char_to_display < 128))
21137 {
21138 /* Either unibyte or ASCII. */
21139 int stretched_p;
21140
21141 it->nglyphs = 1;
21142
21143 pcm = get_per_char_metric (it->f, font, &char2b);
21144
21145 if (it->override_ascent >= 0)
21146 {
21147 it->ascent = it->override_ascent;
21148 it->descent = it->override_descent;
21149 boff = it->override_boff;
21150 }
21151 else
21152 {
21153 it->ascent = FONT_BASE (font) + boff;
21154 it->descent = FONT_DESCENT (font) - boff;
21155 }
21156
21157 if (pcm)
21158 {
21159 it->phys_ascent = pcm->ascent + boff;
21160 it->phys_descent = pcm->descent - boff;
21161 it->pixel_width = pcm->width;
21162 }
21163 else
21164 {
21165 it->glyph_not_available_p = 1;
21166 it->phys_ascent = it->ascent;
21167 it->phys_descent = it->descent;
21168 it->pixel_width = FONT_WIDTH (font);
21169 }
21170
21171 if (it->constrain_row_ascent_descent_p)
21172 {
21173 if (it->descent > it->max_descent)
21174 {
21175 it->ascent += it->descent - it->max_descent;
21176 it->descent = it->max_descent;
21177 }
21178 if (it->ascent > it->max_ascent)
21179 {
21180 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21181 it->ascent = it->max_ascent;
21182 }
21183 it->phys_ascent = min (it->phys_ascent, it->ascent);
21184 it->phys_descent = min (it->phys_descent, it->descent);
21185 extra_line_spacing = 0;
21186 }
21187
21188 /* If this is a space inside a region of text with
21189 `space-width' property, change its width. */
21190 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21191 if (stretched_p)
21192 it->pixel_width *= XFLOATINT (it->space_width);
21193
21194 /* If face has a box, add the box thickness to the character
21195 height. If character has a box line to the left and/or
21196 right, add the box line width to the character's width. */
21197 if (face->box != FACE_NO_BOX)
21198 {
21199 int thick = face->box_line_width;
21200
21201 if (thick > 0)
21202 {
21203 it->ascent += thick;
21204 it->descent += thick;
21205 }
21206 else
21207 thick = -thick;
21208
21209 if (it->start_of_box_run_p)
21210 it->pixel_width += thick;
21211 if (it->end_of_box_run_p)
21212 it->pixel_width += thick;
21213 }
21214
21215 /* If face has an overline, add the height of the overline
21216 (1 pixel) and a 1 pixel margin to the character height. */
21217 if (face->overline_p)
21218 it->ascent += overline_margin;
21219
21220 if (it->constrain_row_ascent_descent_p)
21221 {
21222 if (it->ascent > it->max_ascent)
21223 it->ascent = it->max_ascent;
21224 if (it->descent > it->max_descent)
21225 it->descent = it->max_descent;
21226 }
21227
21228 take_vertical_position_into_account (it);
21229
21230 /* If we have to actually produce glyphs, do it. */
21231 if (it->glyph_row)
21232 {
21233 if (stretched_p)
21234 {
21235 /* Translate a space with a `space-width' property
21236 into a stretch glyph. */
21237 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21238 / FONT_HEIGHT (font));
21239 append_stretch_glyph (it, it->object, it->pixel_width,
21240 it->ascent + it->descent, ascent);
21241 }
21242 else
21243 append_glyph (it);
21244
21245 /* If characters with lbearing or rbearing are displayed
21246 in this line, record that fact in a flag of the
21247 glyph row. This is used to optimize X output code. */
21248 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21249 it->glyph_row->contains_overlapping_glyphs_p = 1;
21250 }
21251 if (! stretched_p && it->pixel_width == 0)
21252 /* We assure that all visible glyphs have at least 1-pixel
21253 width. */
21254 it->pixel_width = 1;
21255 }
21256 else if (it->char_to_display == '\n')
21257 {
21258 /* A newline has no width but we need the height of the line.
21259 But if previous part of the line set a height, don't
21260 increase that height */
21261
21262 Lisp_Object height;
21263 Lisp_Object total_height = Qnil;
21264
21265 it->override_ascent = -1;
21266 it->pixel_width = 0;
21267 it->nglyphs = 0;
21268
21269 height = get_it_property(it, Qline_height);
21270 /* Split (line-height total-height) list */
21271 if (CONSP (height)
21272 && CONSP (XCDR (height))
21273 && NILP (XCDR (XCDR (height))))
21274 {
21275 total_height = XCAR (XCDR (height));
21276 height = XCAR (height);
21277 }
21278 height = calc_line_height_property(it, height, font, boff, 1);
21279
21280 if (it->override_ascent >= 0)
21281 {
21282 it->ascent = it->override_ascent;
21283 it->descent = it->override_descent;
21284 boff = it->override_boff;
21285 }
21286 else
21287 {
21288 it->ascent = FONT_BASE (font) + boff;
21289 it->descent = FONT_DESCENT (font) - boff;
21290 }
21291
21292 if (EQ (height, Qt))
21293 {
21294 if (it->descent > it->max_descent)
21295 {
21296 it->ascent += it->descent - it->max_descent;
21297 it->descent = it->max_descent;
21298 }
21299 if (it->ascent > it->max_ascent)
21300 {
21301 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21302 it->ascent = it->max_ascent;
21303 }
21304 it->phys_ascent = min (it->phys_ascent, it->ascent);
21305 it->phys_descent = min (it->phys_descent, it->descent);
21306 it->constrain_row_ascent_descent_p = 1;
21307 extra_line_spacing = 0;
21308 }
21309 else
21310 {
21311 Lisp_Object spacing;
21312
21313 it->phys_ascent = it->ascent;
21314 it->phys_descent = it->descent;
21315
21316 if ((it->max_ascent > 0 || it->max_descent > 0)
21317 && face->box != FACE_NO_BOX
21318 && face->box_line_width > 0)
21319 {
21320 it->ascent += face->box_line_width;
21321 it->descent += face->box_line_width;
21322 }
21323 if (!NILP (height)
21324 && XINT (height) > it->ascent + it->descent)
21325 it->ascent = XINT (height) - it->descent;
21326
21327 if (!NILP (total_height))
21328 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21329 else
21330 {
21331 spacing = get_it_property(it, Qline_spacing);
21332 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21333 }
21334 if (INTEGERP (spacing))
21335 {
21336 extra_line_spacing = XINT (spacing);
21337 if (!NILP (total_height))
21338 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21339 }
21340 }
21341 }
21342 else if (it->char_to_display == '\t')
21343 {
21344 if (font->space_width > 0)
21345 {
21346 int tab_width = it->tab_width * font->space_width;
21347 int x = it->current_x + it->continuation_lines_width;
21348 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21349
21350 /* If the distance from the current position to the next tab
21351 stop is less than a space character width, use the
21352 tab stop after that. */
21353 if (next_tab_x - x < font->space_width)
21354 next_tab_x += tab_width;
21355
21356 it->pixel_width = next_tab_x - x;
21357 it->nglyphs = 1;
21358 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21359 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21360
21361 if (it->glyph_row)
21362 {
21363 append_stretch_glyph (it, it->object, it->pixel_width,
21364 it->ascent + it->descent, it->ascent);
21365 }
21366 }
21367 else
21368 {
21369 it->pixel_width = 0;
21370 it->nglyphs = 1;
21371 }
21372 }
21373 else
21374 {
21375 /* A multi-byte character. Assume that the display width of the
21376 character is the width of the character multiplied by the
21377 width of the font. */
21378
21379 /* If we found a font, this font should give us the right
21380 metrics. If we didn't find a font, use the frame's
21381 default font and calculate the width of the character by
21382 multiplying the width of font by the width of the
21383 character. */
21384
21385 pcm = get_per_char_metric (it->f, font, &char2b);
21386
21387 if (font_not_found_p || !pcm)
21388 {
21389 int char_width = CHAR_WIDTH (it->char_to_display);
21390
21391 if (char_width == 0)
21392 /* This is a non spacing character. But, as we are
21393 going to display an empty box, the box must occupy
21394 at least one column. */
21395 char_width = 1;
21396 it->glyph_not_available_p = 1;
21397 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21398 it->phys_ascent = FONT_BASE (font) + boff;
21399 it->phys_descent = FONT_DESCENT (font) - boff;
21400 }
21401 else
21402 {
21403 it->pixel_width = pcm->width;
21404 it->phys_ascent = pcm->ascent + boff;
21405 it->phys_descent = pcm->descent - boff;
21406 if (it->glyph_row
21407 && (pcm->lbearing < 0
21408 || pcm->rbearing > pcm->width))
21409 it->glyph_row->contains_overlapping_glyphs_p = 1;
21410 }
21411 it->nglyphs = 1;
21412 it->ascent = FONT_BASE (font) + boff;
21413 it->descent = FONT_DESCENT (font) - boff;
21414 if (face->box != FACE_NO_BOX)
21415 {
21416 int thick = face->box_line_width;
21417
21418 if (thick > 0)
21419 {
21420 it->ascent += thick;
21421 it->descent += thick;
21422 }
21423 else
21424 thick = - thick;
21425
21426 if (it->start_of_box_run_p)
21427 it->pixel_width += thick;
21428 if (it->end_of_box_run_p)
21429 it->pixel_width += thick;
21430 }
21431
21432 /* If face has an overline, add the height of the overline
21433 (1 pixel) and a 1 pixel margin to the character height. */
21434 if (face->overline_p)
21435 it->ascent += overline_margin;
21436
21437 take_vertical_position_into_account (it);
21438
21439 if (it->ascent < 0)
21440 it->ascent = 0;
21441 if (it->descent < 0)
21442 it->descent = 0;
21443
21444 if (it->glyph_row)
21445 append_glyph (it);
21446 if (it->pixel_width == 0)
21447 /* We assure that all visible glyphs have at least 1-pixel
21448 width. */
21449 it->pixel_width = 1;
21450 }
21451 it->multibyte_p = saved_multibyte_p;
21452 }
21453 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21454 {
21455 /* A static compositoin.
21456
21457 Note: A composition is represented as one glyph in the
21458 glyph matrix. There are no padding glyphs.
21459
21460 Important is that pixel_width, ascent, and descent are the
21461 values of what is drawn by draw_glyphs (i.e. the values of
21462 the overall glyphs composed). */
21463 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21464 int boff; /* baseline offset */
21465 struct composition *cmp = composition_table[it->cmp_it.id];
21466 int glyph_len = cmp->glyph_len;
21467 struct font *font = face->font;
21468
21469 it->nglyphs = 1;
21470
21471 /* If we have not yet calculated pixel size data of glyphs of
21472 the composition for the current face font, calculate them
21473 now. Theoretically, we have to check all fonts for the
21474 glyphs, but that requires much time and memory space. So,
21475 here we check only the font of the first glyph. This leads
21476 to incorrect display, but it's very rare, and C-l (recenter)
21477 can correct the display anyway. */
21478 if (! cmp->font || cmp->font != font)
21479 {
21480 /* Ascent and descent of the font of the first character
21481 of this composition (adjusted by baseline offset).
21482 Ascent and descent of overall glyphs should not be less
21483 than them respectively. */
21484 int font_ascent, font_descent, font_height;
21485 /* Bounding box of the overall glyphs. */
21486 int leftmost, rightmost, lowest, highest;
21487 int lbearing, rbearing;
21488 int i, width, ascent, descent;
21489 int left_padded = 0, right_padded = 0;
21490 int c;
21491 XChar2b char2b;
21492 struct font_metrics *pcm;
21493 int font_not_found_p;
21494 int pos;
21495
21496 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21497 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21498 break;
21499 if (glyph_len < cmp->glyph_len)
21500 right_padded = 1;
21501 for (i = 0; i < glyph_len; i++)
21502 {
21503 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21504 break;
21505 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21506 }
21507 if (i > 0)
21508 left_padded = 1;
21509
21510 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21511 : IT_CHARPOS (*it));
21512 /* When no suitable font found, use the default font. */
21513 font_not_found_p = font == NULL;
21514 if (font_not_found_p)
21515 {
21516 face = face->ascii_face;
21517 font = face->font;
21518 }
21519 boff = font->baseline_offset;
21520 if (font->vertical_centering)
21521 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21522 font_ascent = FONT_BASE (font) + boff;
21523 font_descent = FONT_DESCENT (font) - boff;
21524 font_height = FONT_HEIGHT (font);
21525
21526 cmp->font = (void *) font;
21527
21528 pcm = NULL;
21529 if (! font_not_found_p)
21530 {
21531 get_char_face_and_encoding (it->f, c, it->face_id,
21532 &char2b, it->multibyte_p, 0);
21533 pcm = get_per_char_metric (it->f, font, &char2b);
21534 }
21535
21536 /* Initialize the bounding box. */
21537 if (pcm)
21538 {
21539 width = pcm->width;
21540 ascent = pcm->ascent;
21541 descent = pcm->descent;
21542 lbearing = pcm->lbearing;
21543 rbearing = pcm->rbearing;
21544 }
21545 else
21546 {
21547 width = FONT_WIDTH (font);
21548 ascent = FONT_BASE (font);
21549 descent = FONT_DESCENT (font);
21550 lbearing = 0;
21551 rbearing = width;
21552 }
21553
21554 rightmost = width;
21555 leftmost = 0;
21556 lowest = - descent + boff;
21557 highest = ascent + boff;
21558
21559 if (! font_not_found_p
21560 && font->default_ascent
21561 && CHAR_TABLE_P (Vuse_default_ascent)
21562 && !NILP (Faref (Vuse_default_ascent,
21563 make_number (it->char_to_display))))
21564 highest = font->default_ascent + boff;
21565
21566 /* Draw the first glyph at the normal position. It may be
21567 shifted to right later if some other glyphs are drawn
21568 at the left. */
21569 cmp->offsets[i * 2] = 0;
21570 cmp->offsets[i * 2 + 1] = boff;
21571 cmp->lbearing = lbearing;
21572 cmp->rbearing = rbearing;
21573
21574 /* Set cmp->offsets for the remaining glyphs. */
21575 for (i++; i < glyph_len; i++)
21576 {
21577 int left, right, btm, top;
21578 int ch = COMPOSITION_GLYPH (cmp, i);
21579 int face_id;
21580 struct face *this_face;
21581 int this_boff;
21582
21583 if (ch == '\t')
21584 ch = ' ';
21585 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21586 this_face = FACE_FROM_ID (it->f, face_id);
21587 font = this_face->font;
21588
21589 if (font == NULL)
21590 pcm = NULL;
21591 else
21592 {
21593 this_boff = font->baseline_offset;
21594 if (font->vertical_centering)
21595 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21596 get_char_face_and_encoding (it->f, ch, face_id,
21597 &char2b, it->multibyte_p, 0);
21598 pcm = get_per_char_metric (it->f, font, &char2b);
21599 }
21600 if (! pcm)
21601 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21602 else
21603 {
21604 width = pcm->width;
21605 ascent = pcm->ascent;
21606 descent = pcm->descent;
21607 lbearing = pcm->lbearing;
21608 rbearing = pcm->rbearing;
21609 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21610 {
21611 /* Relative composition with or without
21612 alternate chars. */
21613 left = (leftmost + rightmost - width) / 2;
21614 btm = - descent + boff;
21615 if (font->relative_compose
21616 && (! CHAR_TABLE_P (Vignore_relative_composition)
21617 || NILP (Faref (Vignore_relative_composition,
21618 make_number (ch)))))
21619 {
21620
21621 if (- descent >= font->relative_compose)
21622 /* One extra pixel between two glyphs. */
21623 btm = highest + 1;
21624 else if (ascent <= 0)
21625 /* One extra pixel between two glyphs. */
21626 btm = lowest - 1 - ascent - descent;
21627 }
21628 }
21629 else
21630 {
21631 /* A composition rule is specified by an integer
21632 value that encodes global and new reference
21633 points (GREF and NREF). GREF and NREF are
21634 specified by numbers as below:
21635
21636 0---1---2 -- ascent
21637 | |
21638 | |
21639 | |
21640 9--10--11 -- center
21641 | |
21642 ---3---4---5--- baseline
21643 | |
21644 6---7---8 -- descent
21645 */
21646 int rule = COMPOSITION_RULE (cmp, i);
21647 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21648
21649 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21650 grefx = gref % 3, nrefx = nref % 3;
21651 grefy = gref / 3, nrefy = nref / 3;
21652 if (xoff)
21653 xoff = font_height * (xoff - 128) / 256;
21654 if (yoff)
21655 yoff = font_height * (yoff - 128) / 256;
21656
21657 left = (leftmost
21658 + grefx * (rightmost - leftmost) / 2
21659 - nrefx * width / 2
21660 + xoff);
21661
21662 btm = ((grefy == 0 ? highest
21663 : grefy == 1 ? 0
21664 : grefy == 2 ? lowest
21665 : (highest + lowest) / 2)
21666 - (nrefy == 0 ? ascent + descent
21667 : nrefy == 1 ? descent - boff
21668 : nrefy == 2 ? 0
21669 : (ascent + descent) / 2)
21670 + yoff);
21671 }
21672
21673 cmp->offsets[i * 2] = left;
21674 cmp->offsets[i * 2 + 1] = btm + descent;
21675
21676 /* Update the bounding box of the overall glyphs. */
21677 if (width > 0)
21678 {
21679 right = left + width;
21680 if (left < leftmost)
21681 leftmost = left;
21682 if (right > rightmost)
21683 rightmost = right;
21684 }
21685 top = btm + descent + ascent;
21686 if (top > highest)
21687 highest = top;
21688 if (btm < lowest)
21689 lowest = btm;
21690
21691 if (cmp->lbearing > left + lbearing)
21692 cmp->lbearing = left + lbearing;
21693 if (cmp->rbearing < left + rbearing)
21694 cmp->rbearing = left + rbearing;
21695 }
21696 }
21697
21698 /* If there are glyphs whose x-offsets are negative,
21699 shift all glyphs to the right and make all x-offsets
21700 non-negative. */
21701 if (leftmost < 0)
21702 {
21703 for (i = 0; i < cmp->glyph_len; i++)
21704 cmp->offsets[i * 2] -= leftmost;
21705 rightmost -= leftmost;
21706 cmp->lbearing -= leftmost;
21707 cmp->rbearing -= leftmost;
21708 }
21709
21710 if (left_padded && cmp->lbearing < 0)
21711 {
21712 for (i = 0; i < cmp->glyph_len; i++)
21713 cmp->offsets[i * 2] -= cmp->lbearing;
21714 rightmost -= cmp->lbearing;
21715 cmp->rbearing -= cmp->lbearing;
21716 cmp->lbearing = 0;
21717 }
21718 if (right_padded && rightmost < cmp->rbearing)
21719 {
21720 rightmost = cmp->rbearing;
21721 }
21722
21723 cmp->pixel_width = rightmost;
21724 cmp->ascent = highest;
21725 cmp->descent = - lowest;
21726 if (cmp->ascent < font_ascent)
21727 cmp->ascent = font_ascent;
21728 if (cmp->descent < font_descent)
21729 cmp->descent = font_descent;
21730 }
21731
21732 if (it->glyph_row
21733 && (cmp->lbearing < 0
21734 || cmp->rbearing > cmp->pixel_width))
21735 it->glyph_row->contains_overlapping_glyphs_p = 1;
21736
21737 it->pixel_width = cmp->pixel_width;
21738 it->ascent = it->phys_ascent = cmp->ascent;
21739 it->descent = it->phys_descent = cmp->descent;
21740 if (face->box != FACE_NO_BOX)
21741 {
21742 int thick = face->box_line_width;
21743
21744 if (thick > 0)
21745 {
21746 it->ascent += thick;
21747 it->descent += thick;
21748 }
21749 else
21750 thick = - thick;
21751
21752 if (it->start_of_box_run_p)
21753 it->pixel_width += thick;
21754 if (it->end_of_box_run_p)
21755 it->pixel_width += thick;
21756 }
21757
21758 /* If face has an overline, add the height of the overline
21759 (1 pixel) and a 1 pixel margin to the character height. */
21760 if (face->overline_p)
21761 it->ascent += overline_margin;
21762
21763 take_vertical_position_into_account (it);
21764 if (it->ascent < 0)
21765 it->ascent = 0;
21766 if (it->descent < 0)
21767 it->descent = 0;
21768
21769 if (it->glyph_row)
21770 append_composite_glyph (it);
21771 }
21772 else if (it->what == IT_COMPOSITION)
21773 {
21774 /* A dynamic (automatic) composition. */
21775 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21776 Lisp_Object gstring;
21777 struct font_metrics metrics;
21778
21779 gstring = composition_gstring_from_id (it->cmp_it.id);
21780 it->pixel_width
21781 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21782 &metrics);
21783 if (it->glyph_row
21784 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21785 it->glyph_row->contains_overlapping_glyphs_p = 1;
21786 it->ascent = it->phys_ascent = metrics.ascent;
21787 it->descent = it->phys_descent = metrics.descent;
21788 if (face->box != FACE_NO_BOX)
21789 {
21790 int thick = face->box_line_width;
21791
21792 if (thick > 0)
21793 {
21794 it->ascent += thick;
21795 it->descent += thick;
21796 }
21797 else
21798 thick = - thick;
21799
21800 if (it->start_of_box_run_p)
21801 it->pixel_width += thick;
21802 if (it->end_of_box_run_p)
21803 it->pixel_width += thick;
21804 }
21805 /* If face has an overline, add the height of the overline
21806 (1 pixel) and a 1 pixel margin to the character height. */
21807 if (face->overline_p)
21808 it->ascent += overline_margin;
21809 take_vertical_position_into_account (it);
21810 if (it->ascent < 0)
21811 it->ascent = 0;
21812 if (it->descent < 0)
21813 it->descent = 0;
21814
21815 if (it->glyph_row)
21816 append_composite_glyph (it);
21817 }
21818 else if (it->what == IT_IMAGE)
21819 produce_image_glyph (it);
21820 else if (it->what == IT_STRETCH)
21821 produce_stretch_glyph (it);
21822
21823 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21824 because this isn't true for images with `:ascent 100'. */
21825 xassert (it->ascent >= 0 && it->descent >= 0);
21826 if (it->area == TEXT_AREA)
21827 it->current_x += it->pixel_width;
21828
21829 if (extra_line_spacing > 0)
21830 {
21831 it->descent += extra_line_spacing;
21832 if (extra_line_spacing > it->max_extra_line_spacing)
21833 it->max_extra_line_spacing = extra_line_spacing;
21834 }
21835
21836 it->max_ascent = max (it->max_ascent, it->ascent);
21837 it->max_descent = max (it->max_descent, it->descent);
21838 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21839 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21840 }
21841
21842 /* EXPORT for RIF:
21843 Output LEN glyphs starting at START at the nominal cursor position.
21844 Advance the nominal cursor over the text. The global variable
21845 updated_window contains the window being updated, updated_row is
21846 the glyph row being updated, and updated_area is the area of that
21847 row being updated. */
21848
21849 void
21850 x_write_glyphs (start, len)
21851 struct glyph *start;
21852 int len;
21853 {
21854 int x, hpos;
21855
21856 xassert (updated_window && updated_row);
21857 BLOCK_INPUT;
21858
21859 /* Write glyphs. */
21860
21861 hpos = start - updated_row->glyphs[updated_area];
21862 x = draw_glyphs (updated_window, output_cursor.x,
21863 updated_row, updated_area,
21864 hpos, hpos + len,
21865 DRAW_NORMAL_TEXT, 0);
21866
21867 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21868 if (updated_area == TEXT_AREA
21869 && updated_window->phys_cursor_on_p
21870 && updated_window->phys_cursor.vpos == output_cursor.vpos
21871 && updated_window->phys_cursor.hpos >= hpos
21872 && updated_window->phys_cursor.hpos < hpos + len)
21873 updated_window->phys_cursor_on_p = 0;
21874
21875 UNBLOCK_INPUT;
21876
21877 /* Advance the output cursor. */
21878 output_cursor.hpos += len;
21879 output_cursor.x = x;
21880 }
21881
21882
21883 /* EXPORT for RIF:
21884 Insert LEN glyphs from START at the nominal cursor position. */
21885
21886 void
21887 x_insert_glyphs (start, len)
21888 struct glyph *start;
21889 int len;
21890 {
21891 struct frame *f;
21892 struct window *w;
21893 int line_height, shift_by_width, shifted_region_width;
21894 struct glyph_row *row;
21895 struct glyph *glyph;
21896 int frame_x, frame_y;
21897 EMACS_INT hpos;
21898
21899 xassert (updated_window && updated_row);
21900 BLOCK_INPUT;
21901 w = updated_window;
21902 f = XFRAME (WINDOW_FRAME (w));
21903
21904 /* Get the height of the line we are in. */
21905 row = updated_row;
21906 line_height = row->height;
21907
21908 /* Get the width of the glyphs to insert. */
21909 shift_by_width = 0;
21910 for (glyph = start; glyph < start + len; ++glyph)
21911 shift_by_width += glyph->pixel_width;
21912
21913 /* Get the width of the region to shift right. */
21914 shifted_region_width = (window_box_width (w, updated_area)
21915 - output_cursor.x
21916 - shift_by_width);
21917
21918 /* Shift right. */
21919 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21920 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21921
21922 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21923 line_height, shift_by_width);
21924
21925 /* Write the glyphs. */
21926 hpos = start - row->glyphs[updated_area];
21927 draw_glyphs (w, output_cursor.x, row, updated_area,
21928 hpos, hpos + len,
21929 DRAW_NORMAL_TEXT, 0);
21930
21931 /* Advance the output cursor. */
21932 output_cursor.hpos += len;
21933 output_cursor.x += shift_by_width;
21934 UNBLOCK_INPUT;
21935 }
21936
21937
21938 /* EXPORT for RIF:
21939 Erase the current text line from the nominal cursor position
21940 (inclusive) to pixel column TO_X (exclusive). The idea is that
21941 everything from TO_X onward is already erased.
21942
21943 TO_X is a pixel position relative to updated_area of
21944 updated_window. TO_X == -1 means clear to the end of this area. */
21945
21946 void
21947 x_clear_end_of_line (to_x)
21948 int to_x;
21949 {
21950 struct frame *f;
21951 struct window *w = updated_window;
21952 int max_x, min_y, max_y;
21953 int from_x, from_y, to_y;
21954
21955 xassert (updated_window && updated_row);
21956 f = XFRAME (w->frame);
21957
21958 if (updated_row->full_width_p)
21959 max_x = WINDOW_TOTAL_WIDTH (w);
21960 else
21961 max_x = window_box_width (w, updated_area);
21962 max_y = window_text_bottom_y (w);
21963
21964 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21965 of window. For TO_X > 0, truncate to end of drawing area. */
21966 if (to_x == 0)
21967 return;
21968 else if (to_x < 0)
21969 to_x = max_x;
21970 else
21971 to_x = min (to_x, max_x);
21972
21973 to_y = min (max_y, output_cursor.y + updated_row->height);
21974
21975 /* Notice if the cursor will be cleared by this operation. */
21976 if (!updated_row->full_width_p)
21977 notice_overwritten_cursor (w, updated_area,
21978 output_cursor.x, -1,
21979 updated_row->y,
21980 MATRIX_ROW_BOTTOM_Y (updated_row));
21981
21982 from_x = output_cursor.x;
21983
21984 /* Translate to frame coordinates. */
21985 if (updated_row->full_width_p)
21986 {
21987 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21988 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21989 }
21990 else
21991 {
21992 int area_left = window_box_left (w, updated_area);
21993 from_x += area_left;
21994 to_x += area_left;
21995 }
21996
21997 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21998 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21999 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22000
22001 /* Prevent inadvertently clearing to end of the X window. */
22002 if (to_x > from_x && to_y > from_y)
22003 {
22004 BLOCK_INPUT;
22005 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22006 to_x - from_x, to_y - from_y);
22007 UNBLOCK_INPUT;
22008 }
22009 }
22010
22011 #endif /* HAVE_WINDOW_SYSTEM */
22012
22013
22014 \f
22015 /***********************************************************************
22016 Cursor types
22017 ***********************************************************************/
22018
22019 /* Value is the internal representation of the specified cursor type
22020 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22021 of the bar cursor. */
22022
22023 static enum text_cursor_kinds
22024 get_specified_cursor_type (arg, width)
22025 Lisp_Object arg;
22026 int *width;
22027 {
22028 enum text_cursor_kinds type;
22029
22030 if (NILP (arg))
22031 return NO_CURSOR;
22032
22033 if (EQ (arg, Qbox))
22034 return FILLED_BOX_CURSOR;
22035
22036 if (EQ (arg, Qhollow))
22037 return HOLLOW_BOX_CURSOR;
22038
22039 if (EQ (arg, Qbar))
22040 {
22041 *width = 2;
22042 return BAR_CURSOR;
22043 }
22044
22045 if (CONSP (arg)
22046 && EQ (XCAR (arg), Qbar)
22047 && INTEGERP (XCDR (arg))
22048 && XINT (XCDR (arg)) >= 0)
22049 {
22050 *width = XINT (XCDR (arg));
22051 return BAR_CURSOR;
22052 }
22053
22054 if (EQ (arg, Qhbar))
22055 {
22056 *width = 2;
22057 return HBAR_CURSOR;
22058 }
22059
22060 if (CONSP (arg)
22061 && EQ (XCAR (arg), Qhbar)
22062 && INTEGERP (XCDR (arg))
22063 && XINT (XCDR (arg)) >= 0)
22064 {
22065 *width = XINT (XCDR (arg));
22066 return HBAR_CURSOR;
22067 }
22068
22069 /* Treat anything unknown as "hollow box cursor".
22070 It was bad to signal an error; people have trouble fixing
22071 .Xdefaults with Emacs, when it has something bad in it. */
22072 type = HOLLOW_BOX_CURSOR;
22073
22074 return type;
22075 }
22076
22077 /* Set the default cursor types for specified frame. */
22078 void
22079 set_frame_cursor_types (f, arg)
22080 struct frame *f;
22081 Lisp_Object arg;
22082 {
22083 int width;
22084 Lisp_Object tem;
22085
22086 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22087 FRAME_CURSOR_WIDTH (f) = width;
22088
22089 /* By default, set up the blink-off state depending on the on-state. */
22090
22091 tem = Fassoc (arg, Vblink_cursor_alist);
22092 if (!NILP (tem))
22093 {
22094 FRAME_BLINK_OFF_CURSOR (f)
22095 = get_specified_cursor_type (XCDR (tem), &width);
22096 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22097 }
22098 else
22099 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22100 }
22101
22102
22103 /* Return the cursor we want to be displayed in window W. Return
22104 width of bar/hbar cursor through WIDTH arg. Return with
22105 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22106 (i.e. if the `system caret' should track this cursor).
22107
22108 In a mini-buffer window, we want the cursor only to appear if we
22109 are reading input from this window. For the selected window, we
22110 want the cursor type given by the frame parameter or buffer local
22111 setting of cursor-type. If explicitly marked off, draw no cursor.
22112 In all other cases, we want a hollow box cursor. */
22113
22114 static enum text_cursor_kinds
22115 get_window_cursor_type (w, glyph, width, active_cursor)
22116 struct window *w;
22117 struct glyph *glyph;
22118 int *width;
22119 int *active_cursor;
22120 {
22121 struct frame *f = XFRAME (w->frame);
22122 struct buffer *b = XBUFFER (w->buffer);
22123 int cursor_type = DEFAULT_CURSOR;
22124 Lisp_Object alt_cursor;
22125 int non_selected = 0;
22126
22127 *active_cursor = 1;
22128
22129 /* Echo area */
22130 if (cursor_in_echo_area
22131 && FRAME_HAS_MINIBUF_P (f)
22132 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22133 {
22134 if (w == XWINDOW (echo_area_window))
22135 {
22136 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22137 {
22138 *width = FRAME_CURSOR_WIDTH (f);
22139 return FRAME_DESIRED_CURSOR (f);
22140 }
22141 else
22142 return get_specified_cursor_type (b->cursor_type, width);
22143 }
22144
22145 *active_cursor = 0;
22146 non_selected = 1;
22147 }
22148
22149 /* Detect a nonselected window or nonselected frame. */
22150 else if (w != XWINDOW (f->selected_window)
22151 #ifdef HAVE_WINDOW_SYSTEM
22152 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22153 #endif
22154 )
22155 {
22156 *active_cursor = 0;
22157
22158 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22159 return NO_CURSOR;
22160
22161 non_selected = 1;
22162 }
22163
22164 /* Never display a cursor in a window in which cursor-type is nil. */
22165 if (NILP (b->cursor_type))
22166 return NO_CURSOR;
22167
22168 /* Get the normal cursor type for this window. */
22169 if (EQ (b->cursor_type, Qt))
22170 {
22171 cursor_type = FRAME_DESIRED_CURSOR (f);
22172 *width = FRAME_CURSOR_WIDTH (f);
22173 }
22174 else
22175 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22176
22177 /* Use cursor-in-non-selected-windows instead
22178 for non-selected window or frame. */
22179 if (non_selected)
22180 {
22181 alt_cursor = b->cursor_in_non_selected_windows;
22182 if (!EQ (Qt, alt_cursor))
22183 return get_specified_cursor_type (alt_cursor, width);
22184 /* t means modify the normal cursor type. */
22185 if (cursor_type == FILLED_BOX_CURSOR)
22186 cursor_type = HOLLOW_BOX_CURSOR;
22187 else if (cursor_type == BAR_CURSOR && *width > 1)
22188 --*width;
22189 return cursor_type;
22190 }
22191
22192 /* Use normal cursor if not blinked off. */
22193 if (!w->cursor_off_p)
22194 {
22195 #ifdef HAVE_WINDOW_SYSTEM
22196 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22197 {
22198 if (cursor_type == FILLED_BOX_CURSOR)
22199 {
22200 /* Using a block cursor on large images can be very annoying.
22201 So use a hollow cursor for "large" images.
22202 If image is not transparent (no mask), also use hollow cursor. */
22203 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22204 if (img != NULL && IMAGEP (img->spec))
22205 {
22206 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22207 where N = size of default frame font size.
22208 This should cover most of the "tiny" icons people may use. */
22209 if (!img->mask
22210 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22211 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22212 cursor_type = HOLLOW_BOX_CURSOR;
22213 }
22214 }
22215 else if (cursor_type != NO_CURSOR)
22216 {
22217 /* Display current only supports BOX and HOLLOW cursors for images.
22218 So for now, unconditionally use a HOLLOW cursor when cursor is
22219 not a solid box cursor. */
22220 cursor_type = HOLLOW_BOX_CURSOR;
22221 }
22222 }
22223 #endif
22224 return cursor_type;
22225 }
22226
22227 /* Cursor is blinked off, so determine how to "toggle" it. */
22228
22229 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22230 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22231 return get_specified_cursor_type (XCDR (alt_cursor), width);
22232
22233 /* Then see if frame has specified a specific blink off cursor type. */
22234 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22235 {
22236 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22237 return FRAME_BLINK_OFF_CURSOR (f);
22238 }
22239
22240 #if 0
22241 /* Some people liked having a permanently visible blinking cursor,
22242 while others had very strong opinions against it. So it was
22243 decided to remove it. KFS 2003-09-03 */
22244
22245 /* Finally perform built-in cursor blinking:
22246 filled box <-> hollow box
22247 wide [h]bar <-> narrow [h]bar
22248 narrow [h]bar <-> no cursor
22249 other type <-> no cursor */
22250
22251 if (cursor_type == FILLED_BOX_CURSOR)
22252 return HOLLOW_BOX_CURSOR;
22253
22254 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22255 {
22256 *width = 1;
22257 return cursor_type;
22258 }
22259 #endif
22260
22261 return NO_CURSOR;
22262 }
22263
22264
22265 #ifdef HAVE_WINDOW_SYSTEM
22266
22267 /* Notice when the text cursor of window W has been completely
22268 overwritten by a drawing operation that outputs glyphs in AREA
22269 starting at X0 and ending at X1 in the line starting at Y0 and
22270 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22271 the rest of the line after X0 has been written. Y coordinates
22272 are window-relative. */
22273
22274 static void
22275 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22276 struct window *w;
22277 enum glyph_row_area area;
22278 int x0, y0, x1, y1;
22279 {
22280 int cx0, cx1, cy0, cy1;
22281 struct glyph_row *row;
22282
22283 if (!w->phys_cursor_on_p)
22284 return;
22285 if (area != TEXT_AREA)
22286 return;
22287
22288 if (w->phys_cursor.vpos < 0
22289 || w->phys_cursor.vpos >= w->current_matrix->nrows
22290 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22291 !(row->enabled_p && row->displays_text_p)))
22292 return;
22293
22294 if (row->cursor_in_fringe_p)
22295 {
22296 row->cursor_in_fringe_p = 0;
22297 draw_fringe_bitmap (w, row, 0);
22298 w->phys_cursor_on_p = 0;
22299 return;
22300 }
22301
22302 cx0 = w->phys_cursor.x;
22303 cx1 = cx0 + w->phys_cursor_width;
22304 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22305 return;
22306
22307 /* The cursor image will be completely removed from the
22308 screen if the output area intersects the cursor area in
22309 y-direction. When we draw in [y0 y1[, and some part of
22310 the cursor is at y < y0, that part must have been drawn
22311 before. When scrolling, the cursor is erased before
22312 actually scrolling, so we don't come here. When not
22313 scrolling, the rows above the old cursor row must have
22314 changed, and in this case these rows must have written
22315 over the cursor image.
22316
22317 Likewise if part of the cursor is below y1, with the
22318 exception of the cursor being in the first blank row at
22319 the buffer and window end because update_text_area
22320 doesn't draw that row. (Except when it does, but
22321 that's handled in update_text_area.) */
22322
22323 cy0 = w->phys_cursor.y;
22324 cy1 = cy0 + w->phys_cursor_height;
22325 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22326 return;
22327
22328 w->phys_cursor_on_p = 0;
22329 }
22330
22331 #endif /* HAVE_WINDOW_SYSTEM */
22332
22333 \f
22334 /************************************************************************
22335 Mouse Face
22336 ************************************************************************/
22337
22338 #ifdef HAVE_WINDOW_SYSTEM
22339
22340 /* EXPORT for RIF:
22341 Fix the display of area AREA of overlapping row ROW in window W
22342 with respect to the overlapping part OVERLAPS. */
22343
22344 void
22345 x_fix_overlapping_area (w, row, area, overlaps)
22346 struct window *w;
22347 struct glyph_row *row;
22348 enum glyph_row_area area;
22349 int overlaps;
22350 {
22351 int i, x;
22352
22353 BLOCK_INPUT;
22354
22355 x = 0;
22356 for (i = 0; i < row->used[area];)
22357 {
22358 if (row->glyphs[area][i].overlaps_vertically_p)
22359 {
22360 int start = i, start_x = x;
22361
22362 do
22363 {
22364 x += row->glyphs[area][i].pixel_width;
22365 ++i;
22366 }
22367 while (i < row->used[area]
22368 && row->glyphs[area][i].overlaps_vertically_p);
22369
22370 draw_glyphs (w, start_x, row, area,
22371 start, i,
22372 DRAW_NORMAL_TEXT, overlaps);
22373 }
22374 else
22375 {
22376 x += row->glyphs[area][i].pixel_width;
22377 ++i;
22378 }
22379 }
22380
22381 UNBLOCK_INPUT;
22382 }
22383
22384
22385 /* EXPORT:
22386 Draw the cursor glyph of window W in glyph row ROW. See the
22387 comment of draw_glyphs for the meaning of HL. */
22388
22389 void
22390 draw_phys_cursor_glyph (w, row, hl)
22391 struct window *w;
22392 struct glyph_row *row;
22393 enum draw_glyphs_face hl;
22394 {
22395 /* If cursor hpos is out of bounds, don't draw garbage. This can
22396 happen in mini-buffer windows when switching between echo area
22397 glyphs and mini-buffer. */
22398 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22399 {
22400 int on_p = w->phys_cursor_on_p;
22401 int x1;
22402 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22403 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22404 hl, 0);
22405 w->phys_cursor_on_p = on_p;
22406
22407 if (hl == DRAW_CURSOR)
22408 w->phys_cursor_width = x1 - w->phys_cursor.x;
22409 /* When we erase the cursor, and ROW is overlapped by other
22410 rows, make sure that these overlapping parts of other rows
22411 are redrawn. */
22412 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22413 {
22414 w->phys_cursor_width = x1 - w->phys_cursor.x;
22415
22416 if (row > w->current_matrix->rows
22417 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22418 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22419 OVERLAPS_ERASED_CURSOR);
22420
22421 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22422 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22423 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22424 OVERLAPS_ERASED_CURSOR);
22425 }
22426 }
22427 }
22428
22429
22430 /* EXPORT:
22431 Erase the image of a cursor of window W from the screen. */
22432
22433 void
22434 erase_phys_cursor (w)
22435 struct window *w;
22436 {
22437 struct frame *f = XFRAME (w->frame);
22438 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22439 int hpos = w->phys_cursor.hpos;
22440 int vpos = w->phys_cursor.vpos;
22441 int mouse_face_here_p = 0;
22442 struct glyph_matrix *active_glyphs = w->current_matrix;
22443 struct glyph_row *cursor_row;
22444 struct glyph *cursor_glyph;
22445 enum draw_glyphs_face hl;
22446
22447 /* No cursor displayed or row invalidated => nothing to do on the
22448 screen. */
22449 if (w->phys_cursor_type == NO_CURSOR)
22450 goto mark_cursor_off;
22451
22452 /* VPOS >= active_glyphs->nrows means that window has been resized.
22453 Don't bother to erase the cursor. */
22454 if (vpos >= active_glyphs->nrows)
22455 goto mark_cursor_off;
22456
22457 /* If row containing cursor is marked invalid, there is nothing we
22458 can do. */
22459 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22460 if (!cursor_row->enabled_p)
22461 goto mark_cursor_off;
22462
22463 /* If line spacing is > 0, old cursor may only be partially visible in
22464 window after split-window. So adjust visible height. */
22465 cursor_row->visible_height = min (cursor_row->visible_height,
22466 window_text_bottom_y (w) - cursor_row->y);
22467
22468 /* If row is completely invisible, don't attempt to delete a cursor which
22469 isn't there. This can happen if cursor is at top of a window, and
22470 we switch to a buffer with a header line in that window. */
22471 if (cursor_row->visible_height <= 0)
22472 goto mark_cursor_off;
22473
22474 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22475 if (cursor_row->cursor_in_fringe_p)
22476 {
22477 cursor_row->cursor_in_fringe_p = 0;
22478 draw_fringe_bitmap (w, cursor_row, 0);
22479 goto mark_cursor_off;
22480 }
22481
22482 /* This can happen when the new row is shorter than the old one.
22483 In this case, either draw_glyphs or clear_end_of_line
22484 should have cleared the cursor. Note that we wouldn't be
22485 able to erase the cursor in this case because we don't have a
22486 cursor glyph at hand. */
22487 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22488 goto mark_cursor_off;
22489
22490 /* If the cursor is in the mouse face area, redisplay that when
22491 we clear the cursor. */
22492 if (! NILP (dpyinfo->mouse_face_window)
22493 && w == XWINDOW (dpyinfo->mouse_face_window)
22494 && (vpos > dpyinfo->mouse_face_beg_row
22495 || (vpos == dpyinfo->mouse_face_beg_row
22496 && hpos >= dpyinfo->mouse_face_beg_col))
22497 && (vpos < dpyinfo->mouse_face_end_row
22498 || (vpos == dpyinfo->mouse_face_end_row
22499 && hpos < dpyinfo->mouse_face_end_col))
22500 /* Don't redraw the cursor's spot in mouse face if it is at the
22501 end of a line (on a newline). The cursor appears there, but
22502 mouse highlighting does not. */
22503 && cursor_row->used[TEXT_AREA] > hpos)
22504 mouse_face_here_p = 1;
22505
22506 /* Maybe clear the display under the cursor. */
22507 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22508 {
22509 int x, y, left_x;
22510 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22511 int width;
22512
22513 cursor_glyph = get_phys_cursor_glyph (w);
22514 if (cursor_glyph == NULL)
22515 goto mark_cursor_off;
22516
22517 width = cursor_glyph->pixel_width;
22518 left_x = window_box_left_offset (w, TEXT_AREA);
22519 x = w->phys_cursor.x;
22520 if (x < left_x)
22521 width -= left_x - x;
22522 width = min (width, window_box_width (w, TEXT_AREA) - x);
22523 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22524 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22525
22526 if (width > 0)
22527 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22528 }
22529
22530 /* Erase the cursor by redrawing the character underneath it. */
22531 if (mouse_face_here_p)
22532 hl = DRAW_MOUSE_FACE;
22533 else
22534 hl = DRAW_NORMAL_TEXT;
22535 draw_phys_cursor_glyph (w, cursor_row, hl);
22536
22537 mark_cursor_off:
22538 w->phys_cursor_on_p = 0;
22539 w->phys_cursor_type = NO_CURSOR;
22540 }
22541
22542
22543 /* EXPORT:
22544 Display or clear cursor of window W. If ON is zero, clear the
22545 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22546 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22547
22548 void
22549 display_and_set_cursor (w, on, hpos, vpos, x, y)
22550 struct window *w;
22551 int on, hpos, vpos, x, y;
22552 {
22553 struct frame *f = XFRAME (w->frame);
22554 int new_cursor_type;
22555 int new_cursor_width;
22556 int active_cursor;
22557 struct glyph_row *glyph_row;
22558 struct glyph *glyph;
22559
22560 /* This is pointless on invisible frames, and dangerous on garbaged
22561 windows and frames; in the latter case, the frame or window may
22562 be in the midst of changing its size, and x and y may be off the
22563 window. */
22564 if (! FRAME_VISIBLE_P (f)
22565 || FRAME_GARBAGED_P (f)
22566 || vpos >= w->current_matrix->nrows
22567 || hpos >= w->current_matrix->matrix_w)
22568 return;
22569
22570 /* If cursor is off and we want it off, return quickly. */
22571 if (!on && !w->phys_cursor_on_p)
22572 return;
22573
22574 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22575 /* If cursor row is not enabled, we don't really know where to
22576 display the cursor. */
22577 if (!glyph_row->enabled_p)
22578 {
22579 w->phys_cursor_on_p = 0;
22580 return;
22581 }
22582
22583 glyph = NULL;
22584 if (!glyph_row->exact_window_width_line_p
22585 || hpos < glyph_row->used[TEXT_AREA])
22586 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22587
22588 xassert (interrupt_input_blocked);
22589
22590 /* Set new_cursor_type to the cursor we want to be displayed. */
22591 new_cursor_type = get_window_cursor_type (w, glyph,
22592 &new_cursor_width, &active_cursor);
22593
22594 /* If cursor is currently being shown and we don't want it to be or
22595 it is in the wrong place, or the cursor type is not what we want,
22596 erase it. */
22597 if (w->phys_cursor_on_p
22598 && (!on
22599 || w->phys_cursor.x != x
22600 || w->phys_cursor.y != y
22601 || new_cursor_type != w->phys_cursor_type
22602 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22603 && new_cursor_width != w->phys_cursor_width)))
22604 erase_phys_cursor (w);
22605
22606 /* Don't check phys_cursor_on_p here because that flag is only set
22607 to zero in some cases where we know that the cursor has been
22608 completely erased, to avoid the extra work of erasing the cursor
22609 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22610 still not be visible, or it has only been partly erased. */
22611 if (on)
22612 {
22613 w->phys_cursor_ascent = glyph_row->ascent;
22614 w->phys_cursor_height = glyph_row->height;
22615
22616 /* Set phys_cursor_.* before x_draw_.* is called because some
22617 of them may need the information. */
22618 w->phys_cursor.x = x;
22619 w->phys_cursor.y = glyph_row->y;
22620 w->phys_cursor.hpos = hpos;
22621 w->phys_cursor.vpos = vpos;
22622 }
22623
22624 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22625 new_cursor_type, new_cursor_width,
22626 on, active_cursor);
22627 }
22628
22629
22630 /* Switch the display of W's cursor on or off, according to the value
22631 of ON. */
22632
22633 #ifndef HAVE_NS
22634 static
22635 #endif
22636 void
22637 update_window_cursor (w, on)
22638 struct window *w;
22639 int on;
22640 {
22641 /* Don't update cursor in windows whose frame is in the process
22642 of being deleted. */
22643 if (w->current_matrix)
22644 {
22645 BLOCK_INPUT;
22646 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22647 w->phys_cursor.x, w->phys_cursor.y);
22648 UNBLOCK_INPUT;
22649 }
22650 }
22651
22652
22653 /* Call update_window_cursor with parameter ON_P on all leaf windows
22654 in the window tree rooted at W. */
22655
22656 static void
22657 update_cursor_in_window_tree (w, on_p)
22658 struct window *w;
22659 int on_p;
22660 {
22661 while (w)
22662 {
22663 if (!NILP (w->hchild))
22664 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22665 else if (!NILP (w->vchild))
22666 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22667 else
22668 update_window_cursor (w, on_p);
22669
22670 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22671 }
22672 }
22673
22674
22675 /* EXPORT:
22676 Display the cursor on window W, or clear it, according to ON_P.
22677 Don't change the cursor's position. */
22678
22679 void
22680 x_update_cursor (f, on_p)
22681 struct frame *f;
22682 int on_p;
22683 {
22684 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22685 }
22686
22687
22688 /* EXPORT:
22689 Clear the cursor of window W to background color, and mark the
22690 cursor as not shown. This is used when the text where the cursor
22691 is is about to be rewritten. */
22692
22693 void
22694 x_clear_cursor (w)
22695 struct window *w;
22696 {
22697 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22698 update_window_cursor (w, 0);
22699 }
22700
22701
22702 /* EXPORT:
22703 Display the active region described by mouse_face_* according to DRAW. */
22704
22705 void
22706 show_mouse_face (dpyinfo, draw)
22707 Display_Info *dpyinfo;
22708 enum draw_glyphs_face draw;
22709 {
22710 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22711 struct frame *f = XFRAME (WINDOW_FRAME (w));
22712
22713 if (/* If window is in the process of being destroyed, don't bother
22714 to do anything. */
22715 w->current_matrix != NULL
22716 /* Don't update mouse highlight if hidden */
22717 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22718 /* Recognize when we are called to operate on rows that don't exist
22719 anymore. This can happen when a window is split. */
22720 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22721 {
22722 int phys_cursor_on_p = w->phys_cursor_on_p;
22723 struct glyph_row *row, *first, *last;
22724
22725 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22726 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22727
22728 for (row = first; row <= last && row->enabled_p; ++row)
22729 {
22730 int start_hpos, end_hpos, start_x;
22731
22732 /* For all but the first row, the highlight starts at column 0. */
22733 if (row == first)
22734 {
22735 start_hpos = dpyinfo->mouse_face_beg_col;
22736 start_x = dpyinfo->mouse_face_beg_x;
22737 }
22738 else
22739 {
22740 start_hpos = 0;
22741 start_x = 0;
22742 }
22743
22744 if (row == last)
22745 end_hpos = dpyinfo->mouse_face_end_col;
22746 else
22747 {
22748 end_hpos = row->used[TEXT_AREA];
22749 if (draw == DRAW_NORMAL_TEXT)
22750 row->fill_line_p = 1; /* Clear to end of line */
22751 }
22752
22753 if (end_hpos > start_hpos)
22754 {
22755 draw_glyphs (w, start_x, row, TEXT_AREA,
22756 start_hpos, end_hpos,
22757 draw, 0);
22758
22759 row->mouse_face_p
22760 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22761 }
22762 }
22763
22764 /* When we've written over the cursor, arrange for it to
22765 be displayed again. */
22766 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22767 {
22768 BLOCK_INPUT;
22769 display_and_set_cursor (w, 1,
22770 w->phys_cursor.hpos, w->phys_cursor.vpos,
22771 w->phys_cursor.x, w->phys_cursor.y);
22772 UNBLOCK_INPUT;
22773 }
22774 }
22775
22776 /* Change the mouse cursor. */
22777 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22778 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22779 else if (draw == DRAW_MOUSE_FACE)
22780 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22781 else
22782 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22783 }
22784
22785 /* EXPORT:
22786 Clear out the mouse-highlighted active region.
22787 Redraw it un-highlighted first. Value is non-zero if mouse
22788 face was actually drawn unhighlighted. */
22789
22790 int
22791 clear_mouse_face (dpyinfo)
22792 Display_Info *dpyinfo;
22793 {
22794 int cleared = 0;
22795
22796 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22797 {
22798 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22799 cleared = 1;
22800 }
22801
22802 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22803 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22804 dpyinfo->mouse_face_window = Qnil;
22805 dpyinfo->mouse_face_overlay = Qnil;
22806 return cleared;
22807 }
22808
22809
22810 /* EXPORT:
22811 Non-zero if physical cursor of window W is within mouse face. */
22812
22813 int
22814 cursor_in_mouse_face_p (w)
22815 struct window *w;
22816 {
22817 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22818 int in_mouse_face = 0;
22819
22820 if (WINDOWP (dpyinfo->mouse_face_window)
22821 && XWINDOW (dpyinfo->mouse_face_window) == w)
22822 {
22823 int hpos = w->phys_cursor.hpos;
22824 int vpos = w->phys_cursor.vpos;
22825
22826 if (vpos >= dpyinfo->mouse_face_beg_row
22827 && vpos <= dpyinfo->mouse_face_end_row
22828 && (vpos > dpyinfo->mouse_face_beg_row
22829 || hpos >= dpyinfo->mouse_face_beg_col)
22830 && (vpos < dpyinfo->mouse_face_end_row
22831 || hpos < dpyinfo->mouse_face_end_col
22832 || dpyinfo->mouse_face_past_end))
22833 in_mouse_face = 1;
22834 }
22835
22836 return in_mouse_face;
22837 }
22838
22839
22840
22841 \f
22842 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22843 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22844 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22845 for the overlay or run of text properties specifying the mouse
22846 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22847 before-string and after-string that must also be highlighted.
22848 DISPLAY_STRING, if non-nil, is a display string that may cover some
22849 or all of the highlighted text. */
22850
22851 static void
22852 mouse_face_from_buffer_pos (Lisp_Object window,
22853 Display_Info *dpyinfo,
22854 EMACS_INT mouse_charpos,
22855 EMACS_INT start_charpos,
22856 EMACS_INT end_charpos,
22857 Lisp_Object before_string,
22858 Lisp_Object after_string,
22859 Lisp_Object display_string)
22860 {
22861 struct window *w = XWINDOW (window);
22862 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22863 struct glyph_row *row;
22864 struct glyph *glyph, *end;
22865 EMACS_INT ignore;
22866 int x;
22867
22868 xassert (NILP (display_string) || STRINGP (display_string));
22869 xassert (NILP (before_string) || STRINGP (before_string));
22870 xassert (NILP (after_string) || STRINGP (after_string));
22871
22872 /* Find the first highlighted glyph. */
22873 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22874 {
22875 dpyinfo->mouse_face_beg_col = 0;
22876 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22877 dpyinfo->mouse_face_beg_x = first->x;
22878 dpyinfo->mouse_face_beg_y = first->y;
22879 }
22880 else
22881 {
22882 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22883 if (row == NULL)
22884 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22885
22886 /* If the before-string or display-string contains newlines,
22887 row_containing_pos skips to its last row. Move back. */
22888 if (!NILP (before_string) || !NILP (display_string))
22889 {
22890 struct glyph_row *prev;
22891 while ((prev = row - 1, prev >= first)
22892 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22893 && prev->used[TEXT_AREA] > 0)
22894 {
22895 struct glyph *beg = prev->glyphs[TEXT_AREA];
22896 glyph = beg + prev->used[TEXT_AREA];
22897 while (--glyph >= beg && INTEGERP (glyph->object));
22898 if (glyph < beg
22899 || !(EQ (glyph->object, before_string)
22900 || EQ (glyph->object, display_string)))
22901 break;
22902 row = prev;
22903 }
22904 }
22905
22906 glyph = row->glyphs[TEXT_AREA];
22907 end = glyph + row->used[TEXT_AREA];
22908 x = row->x;
22909 dpyinfo->mouse_face_beg_y = row->y;
22910 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22911
22912 /* Skip truncation glyphs at the start of the glyph row. */
22913 if (row->displays_text_p)
22914 for (; glyph < end
22915 && INTEGERP (glyph->object)
22916 && glyph->charpos < 0;
22917 ++glyph)
22918 x += glyph->pixel_width;
22919
22920 /* Scan the glyph row, stopping before BEFORE_STRING or
22921 DISPLAY_STRING or START_CHARPOS. */
22922 for (; glyph < end
22923 && !INTEGERP (glyph->object)
22924 && !EQ (glyph->object, before_string)
22925 && !EQ (glyph->object, display_string)
22926 && !(BUFFERP (glyph->object)
22927 && glyph->charpos >= start_charpos);
22928 ++glyph)
22929 x += glyph->pixel_width;
22930
22931 dpyinfo->mouse_face_beg_x = x;
22932 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22933 }
22934
22935 /* Find the last highlighted glyph. */
22936 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22937 if (row == NULL)
22938 {
22939 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22940 dpyinfo->mouse_face_past_end = 1;
22941 }
22942 else if (!NILP (after_string))
22943 {
22944 /* If the after-string has newlines, advance to its last row. */
22945 struct glyph_row *next;
22946 struct glyph_row *last
22947 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22948
22949 for (next = row + 1;
22950 next <= last
22951 && next->used[TEXT_AREA] > 0
22952 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22953 ++next)
22954 row = next;
22955 }
22956
22957 glyph = row->glyphs[TEXT_AREA];
22958 end = glyph + row->used[TEXT_AREA];
22959 x = row->x;
22960 dpyinfo->mouse_face_end_y = row->y;
22961 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22962
22963 /* Skip truncation glyphs at the start of the row. */
22964 if (row->displays_text_p)
22965 for (; glyph < end
22966 && INTEGERP (glyph->object)
22967 && glyph->charpos < 0;
22968 ++glyph)
22969 x += glyph->pixel_width;
22970
22971 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22972 AFTER_STRING. */
22973 for (; glyph < end
22974 && !INTEGERP (glyph->object)
22975 && !EQ (glyph->object, after_string)
22976 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22977 ++glyph)
22978 x += glyph->pixel_width;
22979
22980 /* If we found AFTER_STRING, consume it and stop. */
22981 if (EQ (glyph->object, after_string))
22982 {
22983 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22984 x += glyph->pixel_width;
22985 }
22986 else
22987 {
22988 /* If there's no after-string, we must check if we overshot,
22989 which might be the case if we stopped after a string glyph.
22990 That glyph may belong to a before-string or display-string
22991 associated with the end position, which must not be
22992 highlighted. */
22993 Lisp_Object prev_object;
22994 int pos;
22995
22996 while (glyph > row->glyphs[TEXT_AREA])
22997 {
22998 prev_object = (glyph - 1)->object;
22999 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23000 break;
23001
23002 pos = string_buffer_position (w, prev_object, end_charpos);
23003 if (pos && pos < end_charpos)
23004 break;
23005
23006 for (; glyph > row->glyphs[TEXT_AREA]
23007 && EQ ((glyph - 1)->object, prev_object);
23008 --glyph)
23009 x -= (glyph - 1)->pixel_width;
23010 }
23011 }
23012
23013 dpyinfo->mouse_face_end_x = x;
23014 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23015 dpyinfo->mouse_face_window = window;
23016 dpyinfo->mouse_face_face_id
23017 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23018 mouse_charpos + 1,
23019 !dpyinfo->mouse_face_hidden, -1);
23020 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23021 }
23022
23023
23024 /* Find the position of the glyph for position POS in OBJECT in
23025 window W's current matrix, and return in *X, *Y the pixel
23026 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23027
23028 RIGHT_P non-zero means return the position of the right edge of the
23029 glyph, RIGHT_P zero means return the left edge position.
23030
23031 If no glyph for POS exists in the matrix, return the position of
23032 the glyph with the next smaller position that is in the matrix, if
23033 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23034 exists in the matrix, return the position of the glyph with the
23035 next larger position in OBJECT.
23036
23037 Value is non-zero if a glyph was found. */
23038
23039 static int
23040 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23041 struct window *w;
23042 EMACS_INT pos;
23043 Lisp_Object object;
23044 int *hpos, *vpos, *x, *y;
23045 int right_p;
23046 {
23047 int yb = window_text_bottom_y (w);
23048 struct glyph_row *r;
23049 struct glyph *best_glyph = NULL;
23050 struct glyph_row *best_row = NULL;
23051 int best_x = 0;
23052
23053 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23054 r->enabled_p && r->y < yb;
23055 ++r)
23056 {
23057 struct glyph *g = r->glyphs[TEXT_AREA];
23058 struct glyph *e = g + r->used[TEXT_AREA];
23059 int gx;
23060
23061 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23062 if (EQ (g->object, object))
23063 {
23064 if (g->charpos == pos)
23065 {
23066 best_glyph = g;
23067 best_x = gx;
23068 best_row = r;
23069 goto found;
23070 }
23071 else if (best_glyph == NULL
23072 || ((eabs (g->charpos - pos)
23073 < eabs (best_glyph->charpos - pos))
23074 && (right_p
23075 ? g->charpos < pos
23076 : g->charpos > pos)))
23077 {
23078 best_glyph = g;
23079 best_x = gx;
23080 best_row = r;
23081 }
23082 }
23083 }
23084
23085 found:
23086
23087 if (best_glyph)
23088 {
23089 *x = best_x;
23090 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23091
23092 if (right_p)
23093 {
23094 *x += best_glyph->pixel_width;
23095 ++*hpos;
23096 }
23097
23098 *y = best_row->y;
23099 *vpos = best_row - w->current_matrix->rows;
23100 }
23101
23102 return best_glyph != NULL;
23103 }
23104
23105
23106 /* See if position X, Y is within a hot-spot of an image. */
23107
23108 static int
23109 on_hot_spot_p (hot_spot, x, y)
23110 Lisp_Object hot_spot;
23111 int x, y;
23112 {
23113 if (!CONSP (hot_spot))
23114 return 0;
23115
23116 if (EQ (XCAR (hot_spot), Qrect))
23117 {
23118 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23119 Lisp_Object rect = XCDR (hot_spot);
23120 Lisp_Object tem;
23121 if (!CONSP (rect))
23122 return 0;
23123 if (!CONSP (XCAR (rect)))
23124 return 0;
23125 if (!CONSP (XCDR (rect)))
23126 return 0;
23127 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23128 return 0;
23129 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23130 return 0;
23131 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23132 return 0;
23133 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23134 return 0;
23135 return 1;
23136 }
23137 else if (EQ (XCAR (hot_spot), Qcircle))
23138 {
23139 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23140 Lisp_Object circ = XCDR (hot_spot);
23141 Lisp_Object lr, lx0, ly0;
23142 if (CONSP (circ)
23143 && CONSP (XCAR (circ))
23144 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23145 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23146 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23147 {
23148 double r = XFLOATINT (lr);
23149 double dx = XINT (lx0) - x;
23150 double dy = XINT (ly0) - y;
23151 return (dx * dx + dy * dy <= r * r);
23152 }
23153 }
23154 else if (EQ (XCAR (hot_spot), Qpoly))
23155 {
23156 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23157 if (VECTORP (XCDR (hot_spot)))
23158 {
23159 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23160 Lisp_Object *poly = v->contents;
23161 int n = v->size;
23162 int i;
23163 int inside = 0;
23164 Lisp_Object lx, ly;
23165 int x0, y0;
23166
23167 /* Need an even number of coordinates, and at least 3 edges. */
23168 if (n < 6 || n & 1)
23169 return 0;
23170
23171 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23172 If count is odd, we are inside polygon. Pixels on edges
23173 may or may not be included depending on actual geometry of the
23174 polygon. */
23175 if ((lx = poly[n-2], !INTEGERP (lx))
23176 || (ly = poly[n-1], !INTEGERP (lx)))
23177 return 0;
23178 x0 = XINT (lx), y0 = XINT (ly);
23179 for (i = 0; i < n; i += 2)
23180 {
23181 int x1 = x0, y1 = y0;
23182 if ((lx = poly[i], !INTEGERP (lx))
23183 || (ly = poly[i+1], !INTEGERP (ly)))
23184 return 0;
23185 x0 = XINT (lx), y0 = XINT (ly);
23186
23187 /* Does this segment cross the X line? */
23188 if (x0 >= x)
23189 {
23190 if (x1 >= x)
23191 continue;
23192 }
23193 else if (x1 < x)
23194 continue;
23195 if (y > y0 && y > y1)
23196 continue;
23197 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23198 inside = !inside;
23199 }
23200 return inside;
23201 }
23202 }
23203 return 0;
23204 }
23205
23206 Lisp_Object
23207 find_hot_spot (map, x, y)
23208 Lisp_Object map;
23209 int x, y;
23210 {
23211 while (CONSP (map))
23212 {
23213 if (CONSP (XCAR (map))
23214 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23215 return XCAR (map);
23216 map = XCDR (map);
23217 }
23218
23219 return Qnil;
23220 }
23221
23222 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23223 3, 3, 0,
23224 doc: /* Lookup in image map MAP coordinates X and Y.
23225 An image map is an alist where each element has the format (AREA ID PLIST).
23226 An AREA is specified as either a rectangle, a circle, or a polygon:
23227 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23228 pixel coordinates of the upper left and bottom right corners.
23229 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23230 and the radius of the circle; r may be a float or integer.
23231 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23232 vector describes one corner in the polygon.
23233 Returns the alist element for the first matching AREA in MAP. */)
23234 (map, x, y)
23235 Lisp_Object map;
23236 Lisp_Object x, y;
23237 {
23238 if (NILP (map))
23239 return Qnil;
23240
23241 CHECK_NUMBER (x);
23242 CHECK_NUMBER (y);
23243
23244 return find_hot_spot (map, XINT (x), XINT (y));
23245 }
23246
23247
23248 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23249 static void
23250 define_frame_cursor1 (f, cursor, pointer)
23251 struct frame *f;
23252 Cursor cursor;
23253 Lisp_Object pointer;
23254 {
23255 /* Do not change cursor shape while dragging mouse. */
23256 if (!NILP (do_mouse_tracking))
23257 return;
23258
23259 if (!NILP (pointer))
23260 {
23261 if (EQ (pointer, Qarrow))
23262 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23263 else if (EQ (pointer, Qhand))
23264 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23265 else if (EQ (pointer, Qtext))
23266 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23267 else if (EQ (pointer, intern ("hdrag")))
23268 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23269 #ifdef HAVE_X_WINDOWS
23270 else if (EQ (pointer, intern ("vdrag")))
23271 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23272 #endif
23273 else if (EQ (pointer, intern ("hourglass")))
23274 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23275 else if (EQ (pointer, Qmodeline))
23276 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23277 else
23278 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23279 }
23280
23281 if (cursor != No_Cursor)
23282 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23283 }
23284
23285 /* Take proper action when mouse has moved to the mode or header line
23286 or marginal area AREA of window W, x-position X and y-position Y.
23287 X is relative to the start of the text display area of W, so the
23288 width of bitmap areas and scroll bars must be subtracted to get a
23289 position relative to the start of the mode line. */
23290
23291 static void
23292 note_mode_line_or_margin_highlight (window, x, y, area)
23293 Lisp_Object window;
23294 int x, y;
23295 enum window_part area;
23296 {
23297 struct window *w = XWINDOW (window);
23298 struct frame *f = XFRAME (w->frame);
23299 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23300 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23301 Lisp_Object pointer = Qnil;
23302 int charpos, dx, dy, width, height;
23303 Lisp_Object string, object = Qnil;
23304 Lisp_Object pos, help;
23305
23306 Lisp_Object mouse_face;
23307 int original_x_pixel = x;
23308 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23309 struct glyph_row *row;
23310
23311 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23312 {
23313 int x0;
23314 struct glyph *end;
23315
23316 string = mode_line_string (w, area, &x, &y, &charpos,
23317 &object, &dx, &dy, &width, &height);
23318
23319 row = (area == ON_MODE_LINE
23320 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23321 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23322
23323 /* Find glyph */
23324 if (row->mode_line_p && row->enabled_p)
23325 {
23326 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23327 end = glyph + row->used[TEXT_AREA];
23328
23329 for (x0 = original_x_pixel;
23330 glyph < end && x0 >= glyph->pixel_width;
23331 ++glyph)
23332 x0 -= glyph->pixel_width;
23333
23334 if (glyph >= end)
23335 glyph = NULL;
23336 }
23337 }
23338 else
23339 {
23340 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23341 string = marginal_area_string (w, area, &x, &y, &charpos,
23342 &object, &dx, &dy, &width, &height);
23343 }
23344
23345 help = Qnil;
23346
23347 if (IMAGEP (object))
23348 {
23349 Lisp_Object image_map, hotspot;
23350 if ((image_map = Fplist_get (XCDR (object), QCmap),
23351 !NILP (image_map))
23352 && (hotspot = find_hot_spot (image_map, dx, dy),
23353 CONSP (hotspot))
23354 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23355 {
23356 Lisp_Object area_id, plist;
23357
23358 area_id = XCAR (hotspot);
23359 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23360 If so, we could look for mouse-enter, mouse-leave
23361 properties in PLIST (and do something...). */
23362 hotspot = XCDR (hotspot);
23363 if (CONSP (hotspot)
23364 && (plist = XCAR (hotspot), CONSP (plist)))
23365 {
23366 pointer = Fplist_get (plist, Qpointer);
23367 if (NILP (pointer))
23368 pointer = Qhand;
23369 help = Fplist_get (plist, Qhelp_echo);
23370 if (!NILP (help))
23371 {
23372 help_echo_string = help;
23373 /* Is this correct? ++kfs */
23374 XSETWINDOW (help_echo_window, w);
23375 help_echo_object = w->buffer;
23376 help_echo_pos = charpos;
23377 }
23378 }
23379 }
23380 if (NILP (pointer))
23381 pointer = Fplist_get (XCDR (object), QCpointer);
23382 }
23383
23384 if (STRINGP (string))
23385 {
23386 pos = make_number (charpos);
23387 /* If we're on a string with `help-echo' text property, arrange
23388 for the help to be displayed. This is done by setting the
23389 global variable help_echo_string to the help string. */
23390 if (NILP (help))
23391 {
23392 help = Fget_text_property (pos, Qhelp_echo, string);
23393 if (!NILP (help))
23394 {
23395 help_echo_string = help;
23396 XSETWINDOW (help_echo_window, w);
23397 help_echo_object = string;
23398 help_echo_pos = charpos;
23399 }
23400 }
23401
23402 if (NILP (pointer))
23403 pointer = Fget_text_property (pos, Qpointer, string);
23404
23405 /* Change the mouse pointer according to what is under X/Y. */
23406 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23407 {
23408 Lisp_Object map;
23409 map = Fget_text_property (pos, Qlocal_map, string);
23410 if (!KEYMAPP (map))
23411 map = Fget_text_property (pos, Qkeymap, string);
23412 if (!KEYMAPP (map))
23413 cursor = dpyinfo->vertical_scroll_bar_cursor;
23414 }
23415
23416 /* Change the mouse face according to what is under X/Y. */
23417 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23418 if (!NILP (mouse_face)
23419 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23420 && glyph)
23421 {
23422 Lisp_Object b, e;
23423
23424 struct glyph * tmp_glyph;
23425
23426 int gpos;
23427 int gseq_length;
23428 int total_pixel_width;
23429 EMACS_INT ignore;
23430
23431 int vpos, hpos;
23432
23433 b = Fprevious_single_property_change (make_number (charpos + 1),
23434 Qmouse_face, string, Qnil);
23435 if (NILP (b))
23436 b = make_number (0);
23437
23438 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23439 if (NILP (e))
23440 e = make_number (SCHARS (string));
23441
23442 /* Calculate the position(glyph position: GPOS) of GLYPH in
23443 displayed string. GPOS is different from CHARPOS.
23444
23445 CHARPOS is the position of glyph in internal string
23446 object. A mode line string format has structures which
23447 is converted to a flatten by emacs lisp interpreter.
23448 The internal string is an element of the structures.
23449 The displayed string is the flatten string. */
23450 gpos = 0;
23451 if (glyph > row_start_glyph)
23452 {
23453 tmp_glyph = glyph - 1;
23454 while (tmp_glyph >= row_start_glyph
23455 && tmp_glyph->charpos >= XINT (b)
23456 && EQ (tmp_glyph->object, glyph->object))
23457 {
23458 tmp_glyph--;
23459 gpos++;
23460 }
23461 }
23462
23463 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23464 displayed string holding GLYPH.
23465
23466 GSEQ_LENGTH is different from SCHARS (STRING).
23467 SCHARS (STRING) returns the length of the internal string. */
23468 for (tmp_glyph = glyph, gseq_length = gpos;
23469 tmp_glyph->charpos < XINT (e);
23470 tmp_glyph++, gseq_length++)
23471 {
23472 if (!EQ (tmp_glyph->object, glyph->object))
23473 break;
23474 }
23475
23476 total_pixel_width = 0;
23477 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23478 total_pixel_width += tmp_glyph->pixel_width;
23479
23480 /* Pre calculation of re-rendering position */
23481 vpos = (x - gpos);
23482 hpos = (area == ON_MODE_LINE
23483 ? (w->current_matrix)->nrows - 1
23484 : 0);
23485
23486 /* If the re-rendering position is included in the last
23487 re-rendering area, we should do nothing. */
23488 if ( EQ (window, dpyinfo->mouse_face_window)
23489 && dpyinfo->mouse_face_beg_col <= vpos
23490 && vpos < dpyinfo->mouse_face_end_col
23491 && dpyinfo->mouse_face_beg_row == hpos )
23492 return;
23493
23494 if (clear_mouse_face (dpyinfo))
23495 cursor = No_Cursor;
23496
23497 dpyinfo->mouse_face_beg_col = vpos;
23498 dpyinfo->mouse_face_beg_row = hpos;
23499
23500 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23501 dpyinfo->mouse_face_beg_y = 0;
23502
23503 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23504 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23505
23506 dpyinfo->mouse_face_end_x = 0;
23507 dpyinfo->mouse_face_end_y = 0;
23508
23509 dpyinfo->mouse_face_past_end = 0;
23510 dpyinfo->mouse_face_window = window;
23511
23512 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23513 charpos,
23514 0, 0, 0, &ignore,
23515 glyph->face_id, 1);
23516 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23517
23518 if (NILP (pointer))
23519 pointer = Qhand;
23520 }
23521 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23522 clear_mouse_face (dpyinfo);
23523 }
23524 define_frame_cursor1 (f, cursor, pointer);
23525 }
23526
23527
23528 /* EXPORT:
23529 Take proper action when the mouse has moved to position X, Y on
23530 frame F as regards highlighting characters that have mouse-face
23531 properties. Also de-highlighting chars where the mouse was before.
23532 X and Y can be negative or out of range. */
23533
23534 void
23535 note_mouse_highlight (f, x, y)
23536 struct frame *f;
23537 int x, y;
23538 {
23539 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23540 enum window_part part;
23541 Lisp_Object window;
23542 struct window *w;
23543 Cursor cursor = No_Cursor;
23544 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23545 struct buffer *b;
23546
23547 /* When a menu is active, don't highlight because this looks odd. */
23548 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23549 if (popup_activated ())
23550 return;
23551 #endif
23552
23553 if (NILP (Vmouse_highlight)
23554 || !f->glyphs_initialized_p)
23555 return;
23556
23557 dpyinfo->mouse_face_mouse_x = x;
23558 dpyinfo->mouse_face_mouse_y = y;
23559 dpyinfo->mouse_face_mouse_frame = f;
23560
23561 if (dpyinfo->mouse_face_defer)
23562 return;
23563
23564 if (gc_in_progress)
23565 {
23566 dpyinfo->mouse_face_deferred_gc = 1;
23567 return;
23568 }
23569
23570 /* Which window is that in? */
23571 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23572
23573 /* If we were displaying active text in another window, clear that.
23574 Also clear if we move out of text area in same window. */
23575 if (! EQ (window, dpyinfo->mouse_face_window)
23576 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23577 && !NILP (dpyinfo->mouse_face_window)))
23578 clear_mouse_face (dpyinfo);
23579
23580 /* Not on a window -> return. */
23581 if (!WINDOWP (window))
23582 return;
23583
23584 /* Reset help_echo_string. It will get recomputed below. */
23585 help_echo_string = Qnil;
23586
23587 /* Convert to window-relative pixel coordinates. */
23588 w = XWINDOW (window);
23589 frame_to_window_pixel_xy (w, &x, &y);
23590
23591 /* Handle tool-bar window differently since it doesn't display a
23592 buffer. */
23593 if (EQ (window, f->tool_bar_window))
23594 {
23595 note_tool_bar_highlight (f, x, y);
23596 return;
23597 }
23598
23599 /* Mouse is on the mode, header line or margin? */
23600 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23601 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23602 {
23603 note_mode_line_or_margin_highlight (window, x, y, part);
23604 return;
23605 }
23606
23607 if (part == ON_VERTICAL_BORDER)
23608 {
23609 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23610 help_echo_string = build_string ("drag-mouse-1: resize");
23611 }
23612 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23613 || part == ON_SCROLL_BAR)
23614 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23615 else
23616 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23617
23618 /* Are we in a window whose display is up to date?
23619 And verify the buffer's text has not changed. */
23620 b = XBUFFER (w->buffer);
23621 if (part == ON_TEXT
23622 && EQ (w->window_end_valid, w->buffer)
23623 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23624 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23625 {
23626 int hpos, vpos, pos, i, dx, dy, area;
23627 struct glyph *glyph;
23628 Lisp_Object object;
23629 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23630 Lisp_Object *overlay_vec = NULL;
23631 int noverlays;
23632 struct buffer *obuf;
23633 int obegv, ozv, same_region;
23634
23635 /* Find the glyph under X/Y. */
23636 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23637
23638 /* Look for :pointer property on image. */
23639 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23640 {
23641 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23642 if (img != NULL && IMAGEP (img->spec))
23643 {
23644 Lisp_Object image_map, hotspot;
23645 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23646 !NILP (image_map))
23647 && (hotspot = find_hot_spot (image_map,
23648 glyph->slice.x + dx,
23649 glyph->slice.y + dy),
23650 CONSP (hotspot))
23651 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23652 {
23653 Lisp_Object area_id, plist;
23654
23655 area_id = XCAR (hotspot);
23656 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23657 If so, we could look for mouse-enter, mouse-leave
23658 properties in PLIST (and do something...). */
23659 hotspot = XCDR (hotspot);
23660 if (CONSP (hotspot)
23661 && (plist = XCAR (hotspot), CONSP (plist)))
23662 {
23663 pointer = Fplist_get (plist, Qpointer);
23664 if (NILP (pointer))
23665 pointer = Qhand;
23666 help_echo_string = Fplist_get (plist, Qhelp_echo);
23667 if (!NILP (help_echo_string))
23668 {
23669 help_echo_window = window;
23670 help_echo_object = glyph->object;
23671 help_echo_pos = glyph->charpos;
23672 }
23673 }
23674 }
23675 if (NILP (pointer))
23676 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23677 }
23678 }
23679
23680 /* Clear mouse face if X/Y not over text. */
23681 if (glyph == NULL
23682 || area != TEXT_AREA
23683 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23684 {
23685 if (clear_mouse_face (dpyinfo))
23686 cursor = No_Cursor;
23687 if (NILP (pointer))
23688 {
23689 if (area != TEXT_AREA)
23690 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23691 else
23692 pointer = Vvoid_text_area_pointer;
23693 }
23694 goto set_cursor;
23695 }
23696
23697 pos = glyph->charpos;
23698 object = glyph->object;
23699 if (!STRINGP (object) && !BUFFERP (object))
23700 goto set_cursor;
23701
23702 /* If we get an out-of-range value, return now; avoid an error. */
23703 if (BUFFERP (object) && pos > BUF_Z (b))
23704 goto set_cursor;
23705
23706 /* Make the window's buffer temporarily current for
23707 overlays_at and compute_char_face. */
23708 obuf = current_buffer;
23709 current_buffer = b;
23710 obegv = BEGV;
23711 ozv = ZV;
23712 BEGV = BEG;
23713 ZV = Z;
23714
23715 /* Is this char mouse-active or does it have help-echo? */
23716 position = make_number (pos);
23717
23718 if (BUFFERP (object))
23719 {
23720 /* Put all the overlays we want in a vector in overlay_vec. */
23721 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23722 /* Sort overlays into increasing priority order. */
23723 noverlays = sort_overlays (overlay_vec, noverlays, w);
23724 }
23725 else
23726 noverlays = 0;
23727
23728 same_region = (EQ (window, dpyinfo->mouse_face_window)
23729 && vpos >= dpyinfo->mouse_face_beg_row
23730 && vpos <= dpyinfo->mouse_face_end_row
23731 && (vpos > dpyinfo->mouse_face_beg_row
23732 || hpos >= dpyinfo->mouse_face_beg_col)
23733 && (vpos < dpyinfo->mouse_face_end_row
23734 || hpos < dpyinfo->mouse_face_end_col
23735 || dpyinfo->mouse_face_past_end));
23736
23737 if (same_region)
23738 cursor = No_Cursor;
23739
23740 /* Check mouse-face highlighting. */
23741 if (! same_region
23742 /* If there exists an overlay with mouse-face overlapping
23743 the one we are currently highlighting, we have to
23744 check if we enter the overlapping overlay, and then
23745 highlight only that. */
23746 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23747 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23748 {
23749 /* Find the highest priority overlay with a mouse-face. */
23750 overlay = Qnil;
23751 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23752 {
23753 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23754 if (!NILP (mouse_face))
23755 overlay = overlay_vec[i];
23756 }
23757
23758 /* If we're highlighting the same overlay as before, there's
23759 no need to do that again. */
23760 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23761 goto check_help_echo;
23762 dpyinfo->mouse_face_overlay = overlay;
23763
23764 /* Clear the display of the old active region, if any. */
23765 if (clear_mouse_face (dpyinfo))
23766 cursor = No_Cursor;
23767
23768 /* If no overlay applies, get a text property. */
23769 if (NILP (overlay))
23770 mouse_face = Fget_text_property (position, Qmouse_face, object);
23771
23772 /* Next, compute the bounds of the mouse highlighting and
23773 display it. */
23774 if (!NILP (mouse_face) && STRINGP (object))
23775 {
23776 /* The mouse-highlighting comes from a display string
23777 with a mouse-face. */
23778 Lisp_Object b, e;
23779 EMACS_INT ignore;
23780
23781 b = Fprevious_single_property_change
23782 (make_number (pos + 1), Qmouse_face, object, Qnil);
23783 e = Fnext_single_property_change
23784 (position, Qmouse_face, object, Qnil);
23785 if (NILP (b))
23786 b = make_number (0);
23787 if (NILP (e))
23788 e = make_number (SCHARS (object) - 1);
23789
23790 fast_find_string_pos (w, XINT (b), object,
23791 &dpyinfo->mouse_face_beg_col,
23792 &dpyinfo->mouse_face_beg_row,
23793 &dpyinfo->mouse_face_beg_x,
23794 &dpyinfo->mouse_face_beg_y, 0);
23795 fast_find_string_pos (w, XINT (e), object,
23796 &dpyinfo->mouse_face_end_col,
23797 &dpyinfo->mouse_face_end_row,
23798 &dpyinfo->mouse_face_end_x,
23799 &dpyinfo->mouse_face_end_y, 1);
23800 dpyinfo->mouse_face_past_end = 0;
23801 dpyinfo->mouse_face_window = window;
23802 dpyinfo->mouse_face_face_id
23803 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23804 glyph->face_id, 1);
23805 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23806 cursor = No_Cursor;
23807 }
23808 else
23809 {
23810 /* The mouse-highlighting, if any, comes from an overlay
23811 or text property in the buffer. */
23812 Lisp_Object buffer, display_string;
23813
23814 if (STRINGP (object))
23815 {
23816 /* If we are on a display string with no mouse-face,
23817 check if the text under it has one. */
23818 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23819 int start = MATRIX_ROW_START_CHARPOS (r);
23820 pos = string_buffer_position (w, object, start);
23821 if (pos > 0)
23822 {
23823 mouse_face = get_char_property_and_overlay
23824 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23825 buffer = w->buffer;
23826 display_string = object;
23827 }
23828 }
23829 else
23830 {
23831 buffer = object;
23832 display_string = Qnil;
23833 }
23834
23835 if (!NILP (mouse_face))
23836 {
23837 Lisp_Object before, after;
23838 Lisp_Object before_string, after_string;
23839
23840 if (NILP (overlay))
23841 {
23842 /* Handle the text property case. */
23843 before = Fprevious_single_property_change
23844 (make_number (pos + 1), Qmouse_face, buffer,
23845 Fmarker_position (w->start));
23846 after = Fnext_single_property_change
23847 (make_number (pos), Qmouse_face, buffer,
23848 make_number (BUF_Z (XBUFFER (buffer))
23849 - XFASTINT (w->window_end_pos)));
23850 before_string = after_string = Qnil;
23851 }
23852 else
23853 {
23854 /* Handle the overlay case. */
23855 before = Foverlay_start (overlay);
23856 after = Foverlay_end (overlay);
23857 before_string = Foverlay_get (overlay, Qbefore_string);
23858 after_string = Foverlay_get (overlay, Qafter_string);
23859
23860 if (!STRINGP (before_string)) before_string = Qnil;
23861 if (!STRINGP (after_string)) after_string = Qnil;
23862 }
23863
23864 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23865 XFASTINT (before),
23866 XFASTINT (after),
23867 before_string, after_string,
23868 display_string);
23869 cursor = No_Cursor;
23870 }
23871 }
23872 }
23873
23874 check_help_echo:
23875
23876 /* Look for a `help-echo' property. */
23877 if (NILP (help_echo_string)) {
23878 Lisp_Object help, overlay;
23879
23880 /* Check overlays first. */
23881 help = overlay = Qnil;
23882 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23883 {
23884 overlay = overlay_vec[i];
23885 help = Foverlay_get (overlay, Qhelp_echo);
23886 }
23887
23888 if (!NILP (help))
23889 {
23890 help_echo_string = help;
23891 help_echo_window = window;
23892 help_echo_object = overlay;
23893 help_echo_pos = pos;
23894 }
23895 else
23896 {
23897 Lisp_Object object = glyph->object;
23898 int charpos = glyph->charpos;
23899
23900 /* Try text properties. */
23901 if (STRINGP (object)
23902 && charpos >= 0
23903 && charpos < SCHARS (object))
23904 {
23905 help = Fget_text_property (make_number (charpos),
23906 Qhelp_echo, object);
23907 if (NILP (help))
23908 {
23909 /* If the string itself doesn't specify a help-echo,
23910 see if the buffer text ``under'' it does. */
23911 struct glyph_row *r
23912 = MATRIX_ROW (w->current_matrix, vpos);
23913 int start = MATRIX_ROW_START_CHARPOS (r);
23914 int pos = string_buffer_position (w, object, start);
23915 if (pos > 0)
23916 {
23917 help = Fget_char_property (make_number (pos),
23918 Qhelp_echo, w->buffer);
23919 if (!NILP (help))
23920 {
23921 charpos = pos;
23922 object = w->buffer;
23923 }
23924 }
23925 }
23926 }
23927 else if (BUFFERP (object)
23928 && charpos >= BEGV
23929 && charpos < ZV)
23930 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23931 object);
23932
23933 if (!NILP (help))
23934 {
23935 help_echo_string = help;
23936 help_echo_window = window;
23937 help_echo_object = object;
23938 help_echo_pos = charpos;
23939 }
23940 }
23941 }
23942
23943 /* Look for a `pointer' property. */
23944 if (NILP (pointer))
23945 {
23946 /* Check overlays first. */
23947 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23948 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23949
23950 if (NILP (pointer))
23951 {
23952 Lisp_Object object = glyph->object;
23953 int charpos = glyph->charpos;
23954
23955 /* Try text properties. */
23956 if (STRINGP (object)
23957 && charpos >= 0
23958 && charpos < SCHARS (object))
23959 {
23960 pointer = Fget_text_property (make_number (charpos),
23961 Qpointer, object);
23962 if (NILP (pointer))
23963 {
23964 /* If the string itself doesn't specify a pointer,
23965 see if the buffer text ``under'' it does. */
23966 struct glyph_row *r
23967 = MATRIX_ROW (w->current_matrix, vpos);
23968 int start = MATRIX_ROW_START_CHARPOS (r);
23969 int pos = string_buffer_position (w, object, start);
23970 if (pos > 0)
23971 pointer = Fget_char_property (make_number (pos),
23972 Qpointer, w->buffer);
23973 }
23974 }
23975 else if (BUFFERP (object)
23976 && charpos >= BEGV
23977 && charpos < ZV)
23978 pointer = Fget_text_property (make_number (charpos),
23979 Qpointer, object);
23980 }
23981 }
23982
23983 BEGV = obegv;
23984 ZV = ozv;
23985 current_buffer = obuf;
23986 }
23987
23988 set_cursor:
23989
23990 define_frame_cursor1 (f, cursor, pointer);
23991 }
23992
23993
23994 /* EXPORT for RIF:
23995 Clear any mouse-face on window W. This function is part of the
23996 redisplay interface, and is called from try_window_id and similar
23997 functions to ensure the mouse-highlight is off. */
23998
23999 void
24000 x_clear_window_mouse_face (w)
24001 struct window *w;
24002 {
24003 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24004 Lisp_Object window;
24005
24006 BLOCK_INPUT;
24007 XSETWINDOW (window, w);
24008 if (EQ (window, dpyinfo->mouse_face_window))
24009 clear_mouse_face (dpyinfo);
24010 UNBLOCK_INPUT;
24011 }
24012
24013
24014 /* EXPORT:
24015 Just discard the mouse face information for frame F, if any.
24016 This is used when the size of F is changed. */
24017
24018 void
24019 cancel_mouse_face (f)
24020 struct frame *f;
24021 {
24022 Lisp_Object window;
24023 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24024
24025 window = dpyinfo->mouse_face_window;
24026 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24027 {
24028 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24029 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24030 dpyinfo->mouse_face_window = Qnil;
24031 }
24032 }
24033
24034
24035 #endif /* HAVE_WINDOW_SYSTEM */
24036
24037 \f
24038 /***********************************************************************
24039 Exposure Events
24040 ***********************************************************************/
24041
24042 #ifdef HAVE_WINDOW_SYSTEM
24043
24044 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24045 which intersects rectangle R. R is in window-relative coordinates. */
24046
24047 static void
24048 expose_area (w, row, r, area)
24049 struct window *w;
24050 struct glyph_row *row;
24051 XRectangle *r;
24052 enum glyph_row_area area;
24053 {
24054 struct glyph *first = row->glyphs[area];
24055 struct glyph *end = row->glyphs[area] + row->used[area];
24056 struct glyph *last;
24057 int first_x, start_x, x;
24058
24059 if (area == TEXT_AREA && row->fill_line_p)
24060 /* If row extends face to end of line write the whole line. */
24061 draw_glyphs (w, 0, row, area,
24062 0, row->used[area],
24063 DRAW_NORMAL_TEXT, 0);
24064 else
24065 {
24066 /* Set START_X to the window-relative start position for drawing glyphs of
24067 AREA. The first glyph of the text area can be partially visible.
24068 The first glyphs of other areas cannot. */
24069 start_x = window_box_left_offset (w, area);
24070 x = start_x;
24071 if (area == TEXT_AREA)
24072 x += row->x;
24073
24074 /* Find the first glyph that must be redrawn. */
24075 while (first < end
24076 && x + first->pixel_width < r->x)
24077 {
24078 x += first->pixel_width;
24079 ++first;
24080 }
24081
24082 /* Find the last one. */
24083 last = first;
24084 first_x = x;
24085 while (last < end
24086 && x < r->x + r->width)
24087 {
24088 x += last->pixel_width;
24089 ++last;
24090 }
24091
24092 /* Repaint. */
24093 if (last > first)
24094 draw_glyphs (w, first_x - start_x, row, area,
24095 first - row->glyphs[area], last - row->glyphs[area],
24096 DRAW_NORMAL_TEXT, 0);
24097 }
24098 }
24099
24100
24101 /* Redraw the parts of the glyph row ROW on window W intersecting
24102 rectangle R. R is in window-relative coordinates. Value is
24103 non-zero if mouse-face was overwritten. */
24104
24105 static int
24106 expose_line (w, row, r)
24107 struct window *w;
24108 struct glyph_row *row;
24109 XRectangle *r;
24110 {
24111 xassert (row->enabled_p);
24112
24113 if (row->mode_line_p || w->pseudo_window_p)
24114 draw_glyphs (w, 0, row, TEXT_AREA,
24115 0, row->used[TEXT_AREA],
24116 DRAW_NORMAL_TEXT, 0);
24117 else
24118 {
24119 if (row->used[LEFT_MARGIN_AREA])
24120 expose_area (w, row, r, LEFT_MARGIN_AREA);
24121 if (row->used[TEXT_AREA])
24122 expose_area (w, row, r, TEXT_AREA);
24123 if (row->used[RIGHT_MARGIN_AREA])
24124 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24125 draw_row_fringe_bitmaps (w, row);
24126 }
24127
24128 return row->mouse_face_p;
24129 }
24130
24131
24132 /* Redraw those parts of glyphs rows during expose event handling that
24133 overlap other rows. Redrawing of an exposed line writes over parts
24134 of lines overlapping that exposed line; this function fixes that.
24135
24136 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24137 row in W's current matrix that is exposed and overlaps other rows.
24138 LAST_OVERLAPPING_ROW is the last such row. */
24139
24140 static void
24141 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24142 struct window *w;
24143 struct glyph_row *first_overlapping_row;
24144 struct glyph_row *last_overlapping_row;
24145 XRectangle *r;
24146 {
24147 struct glyph_row *row;
24148
24149 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24150 if (row->overlapping_p)
24151 {
24152 xassert (row->enabled_p && !row->mode_line_p);
24153
24154 row->clip = r;
24155 if (row->used[LEFT_MARGIN_AREA])
24156 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24157
24158 if (row->used[TEXT_AREA])
24159 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24160
24161 if (row->used[RIGHT_MARGIN_AREA])
24162 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24163 row->clip = NULL;
24164 }
24165 }
24166
24167
24168 /* Return non-zero if W's cursor intersects rectangle R. */
24169
24170 static int
24171 phys_cursor_in_rect_p (w, r)
24172 struct window *w;
24173 XRectangle *r;
24174 {
24175 XRectangle cr, result;
24176 struct glyph *cursor_glyph;
24177 struct glyph_row *row;
24178
24179 if (w->phys_cursor.vpos >= 0
24180 && w->phys_cursor.vpos < w->current_matrix->nrows
24181 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24182 row->enabled_p)
24183 && row->cursor_in_fringe_p)
24184 {
24185 /* Cursor is in the fringe. */
24186 cr.x = window_box_right_offset (w,
24187 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24188 ? RIGHT_MARGIN_AREA
24189 : TEXT_AREA));
24190 cr.y = row->y;
24191 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24192 cr.height = row->height;
24193 return x_intersect_rectangles (&cr, r, &result);
24194 }
24195
24196 cursor_glyph = get_phys_cursor_glyph (w);
24197 if (cursor_glyph)
24198 {
24199 /* r is relative to W's box, but w->phys_cursor.x is relative
24200 to left edge of W's TEXT area. Adjust it. */
24201 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24202 cr.y = w->phys_cursor.y;
24203 cr.width = cursor_glyph->pixel_width;
24204 cr.height = w->phys_cursor_height;
24205 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24206 I assume the effect is the same -- and this is portable. */
24207 return x_intersect_rectangles (&cr, r, &result);
24208 }
24209 /* If we don't understand the format, pretend we're not in the hot-spot. */
24210 return 0;
24211 }
24212
24213
24214 /* EXPORT:
24215 Draw a vertical window border to the right of window W if W doesn't
24216 have vertical scroll bars. */
24217
24218 void
24219 x_draw_vertical_border (w)
24220 struct window *w;
24221 {
24222 struct frame *f = XFRAME (WINDOW_FRAME (w));
24223
24224 /* We could do better, if we knew what type of scroll-bar the adjacent
24225 windows (on either side) have... But we don't :-(
24226 However, I think this works ok. ++KFS 2003-04-25 */
24227
24228 /* Redraw borders between horizontally adjacent windows. Don't
24229 do it for frames with vertical scroll bars because either the
24230 right scroll bar of a window, or the left scroll bar of its
24231 neighbor will suffice as a border. */
24232 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24233 return;
24234
24235 if (!WINDOW_RIGHTMOST_P (w)
24236 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24237 {
24238 int x0, x1, y0, y1;
24239
24240 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24241 y1 -= 1;
24242
24243 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24244 x1 -= 1;
24245
24246 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24247 }
24248 else if (!WINDOW_LEFTMOST_P (w)
24249 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24250 {
24251 int x0, x1, y0, y1;
24252
24253 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24254 y1 -= 1;
24255
24256 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24257 x0 -= 1;
24258
24259 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24260 }
24261 }
24262
24263
24264 /* Redraw the part of window W intersection rectangle FR. Pixel
24265 coordinates in FR are frame-relative. Call this function with
24266 input blocked. Value is non-zero if the exposure overwrites
24267 mouse-face. */
24268
24269 static int
24270 expose_window (w, fr)
24271 struct window *w;
24272 XRectangle *fr;
24273 {
24274 struct frame *f = XFRAME (w->frame);
24275 XRectangle wr, r;
24276 int mouse_face_overwritten_p = 0;
24277
24278 /* If window is not yet fully initialized, do nothing. This can
24279 happen when toolkit scroll bars are used and a window is split.
24280 Reconfiguring the scroll bar will generate an expose for a newly
24281 created window. */
24282 if (w->current_matrix == NULL)
24283 return 0;
24284
24285 /* When we're currently updating the window, display and current
24286 matrix usually don't agree. Arrange for a thorough display
24287 later. */
24288 if (w == updated_window)
24289 {
24290 SET_FRAME_GARBAGED (f);
24291 return 0;
24292 }
24293
24294 /* Frame-relative pixel rectangle of W. */
24295 wr.x = WINDOW_LEFT_EDGE_X (w);
24296 wr.y = WINDOW_TOP_EDGE_Y (w);
24297 wr.width = WINDOW_TOTAL_WIDTH (w);
24298 wr.height = WINDOW_TOTAL_HEIGHT (w);
24299
24300 if (x_intersect_rectangles (fr, &wr, &r))
24301 {
24302 int yb = window_text_bottom_y (w);
24303 struct glyph_row *row;
24304 int cursor_cleared_p;
24305 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24306
24307 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24308 r.x, r.y, r.width, r.height));
24309
24310 /* Convert to window coordinates. */
24311 r.x -= WINDOW_LEFT_EDGE_X (w);
24312 r.y -= WINDOW_TOP_EDGE_Y (w);
24313
24314 /* Turn off the cursor. */
24315 if (!w->pseudo_window_p
24316 && phys_cursor_in_rect_p (w, &r))
24317 {
24318 x_clear_cursor (w);
24319 cursor_cleared_p = 1;
24320 }
24321 else
24322 cursor_cleared_p = 0;
24323
24324 /* Update lines intersecting rectangle R. */
24325 first_overlapping_row = last_overlapping_row = NULL;
24326 for (row = w->current_matrix->rows;
24327 row->enabled_p;
24328 ++row)
24329 {
24330 int y0 = row->y;
24331 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24332
24333 if ((y0 >= r.y && y0 < r.y + r.height)
24334 || (y1 > r.y && y1 < r.y + r.height)
24335 || (r.y >= y0 && r.y < y1)
24336 || (r.y + r.height > y0 && r.y + r.height < y1))
24337 {
24338 /* A header line may be overlapping, but there is no need
24339 to fix overlapping areas for them. KFS 2005-02-12 */
24340 if (row->overlapping_p && !row->mode_line_p)
24341 {
24342 if (first_overlapping_row == NULL)
24343 first_overlapping_row = row;
24344 last_overlapping_row = row;
24345 }
24346
24347 row->clip = fr;
24348 if (expose_line (w, row, &r))
24349 mouse_face_overwritten_p = 1;
24350 row->clip = NULL;
24351 }
24352 else if (row->overlapping_p)
24353 {
24354 /* We must redraw a row overlapping the exposed area. */
24355 if (y0 < r.y
24356 ? y0 + row->phys_height > r.y
24357 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24358 {
24359 if (first_overlapping_row == NULL)
24360 first_overlapping_row = row;
24361 last_overlapping_row = row;
24362 }
24363 }
24364
24365 if (y1 >= yb)
24366 break;
24367 }
24368
24369 /* Display the mode line if there is one. */
24370 if (WINDOW_WANTS_MODELINE_P (w)
24371 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24372 row->enabled_p)
24373 && row->y < r.y + r.height)
24374 {
24375 if (expose_line (w, row, &r))
24376 mouse_face_overwritten_p = 1;
24377 }
24378
24379 if (!w->pseudo_window_p)
24380 {
24381 /* Fix the display of overlapping rows. */
24382 if (first_overlapping_row)
24383 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24384 fr);
24385
24386 /* Draw border between windows. */
24387 x_draw_vertical_border (w);
24388
24389 /* Turn the cursor on again. */
24390 if (cursor_cleared_p)
24391 update_window_cursor (w, 1);
24392 }
24393 }
24394
24395 return mouse_face_overwritten_p;
24396 }
24397
24398
24399
24400 /* Redraw (parts) of all windows in the window tree rooted at W that
24401 intersect R. R contains frame pixel coordinates. Value is
24402 non-zero if the exposure overwrites mouse-face. */
24403
24404 static int
24405 expose_window_tree (w, r)
24406 struct window *w;
24407 XRectangle *r;
24408 {
24409 struct frame *f = XFRAME (w->frame);
24410 int mouse_face_overwritten_p = 0;
24411
24412 while (w && !FRAME_GARBAGED_P (f))
24413 {
24414 if (!NILP (w->hchild))
24415 mouse_face_overwritten_p
24416 |= expose_window_tree (XWINDOW (w->hchild), r);
24417 else if (!NILP (w->vchild))
24418 mouse_face_overwritten_p
24419 |= expose_window_tree (XWINDOW (w->vchild), r);
24420 else
24421 mouse_face_overwritten_p |= expose_window (w, r);
24422
24423 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24424 }
24425
24426 return mouse_face_overwritten_p;
24427 }
24428
24429
24430 /* EXPORT:
24431 Redisplay an exposed area of frame F. X and Y are the upper-left
24432 corner of the exposed rectangle. W and H are width and height of
24433 the exposed area. All are pixel values. W or H zero means redraw
24434 the entire frame. */
24435
24436 void
24437 expose_frame (f, x, y, w, h)
24438 struct frame *f;
24439 int x, y, w, h;
24440 {
24441 XRectangle r;
24442 int mouse_face_overwritten_p = 0;
24443
24444 TRACE ((stderr, "expose_frame "));
24445
24446 /* No need to redraw if frame will be redrawn soon. */
24447 if (FRAME_GARBAGED_P (f))
24448 {
24449 TRACE ((stderr, " garbaged\n"));
24450 return;
24451 }
24452
24453 /* If basic faces haven't been realized yet, there is no point in
24454 trying to redraw anything. This can happen when we get an expose
24455 event while Emacs is starting, e.g. by moving another window. */
24456 if (FRAME_FACE_CACHE (f) == NULL
24457 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24458 {
24459 TRACE ((stderr, " no faces\n"));
24460 return;
24461 }
24462
24463 if (w == 0 || h == 0)
24464 {
24465 r.x = r.y = 0;
24466 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24467 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24468 }
24469 else
24470 {
24471 r.x = x;
24472 r.y = y;
24473 r.width = w;
24474 r.height = h;
24475 }
24476
24477 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24478 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24479
24480 if (WINDOWP (f->tool_bar_window))
24481 mouse_face_overwritten_p
24482 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24483
24484 #ifdef HAVE_X_WINDOWS
24485 #ifndef MSDOS
24486 #ifndef USE_X_TOOLKIT
24487 if (WINDOWP (f->menu_bar_window))
24488 mouse_face_overwritten_p
24489 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24490 #endif /* not USE_X_TOOLKIT */
24491 #endif
24492 #endif
24493
24494 /* Some window managers support a focus-follows-mouse style with
24495 delayed raising of frames. Imagine a partially obscured frame,
24496 and moving the mouse into partially obscured mouse-face on that
24497 frame. The visible part of the mouse-face will be highlighted,
24498 then the WM raises the obscured frame. With at least one WM, KDE
24499 2.1, Emacs is not getting any event for the raising of the frame
24500 (even tried with SubstructureRedirectMask), only Expose events.
24501 These expose events will draw text normally, i.e. not
24502 highlighted. Which means we must redo the highlight here.
24503 Subsume it under ``we love X''. --gerd 2001-08-15 */
24504 /* Included in Windows version because Windows most likely does not
24505 do the right thing if any third party tool offers
24506 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24507 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24508 {
24509 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24510 if (f == dpyinfo->mouse_face_mouse_frame)
24511 {
24512 int x = dpyinfo->mouse_face_mouse_x;
24513 int y = dpyinfo->mouse_face_mouse_y;
24514 clear_mouse_face (dpyinfo);
24515 note_mouse_highlight (f, x, y);
24516 }
24517 }
24518 }
24519
24520
24521 /* EXPORT:
24522 Determine the intersection of two rectangles R1 and R2. Return
24523 the intersection in *RESULT. Value is non-zero if RESULT is not
24524 empty. */
24525
24526 int
24527 x_intersect_rectangles (r1, r2, result)
24528 XRectangle *r1, *r2, *result;
24529 {
24530 XRectangle *left, *right;
24531 XRectangle *upper, *lower;
24532 int intersection_p = 0;
24533
24534 /* Rearrange so that R1 is the left-most rectangle. */
24535 if (r1->x < r2->x)
24536 left = r1, right = r2;
24537 else
24538 left = r2, right = r1;
24539
24540 /* X0 of the intersection is right.x0, if this is inside R1,
24541 otherwise there is no intersection. */
24542 if (right->x <= left->x + left->width)
24543 {
24544 result->x = right->x;
24545
24546 /* The right end of the intersection is the minimum of the
24547 the right ends of left and right. */
24548 result->width = (min (left->x + left->width, right->x + right->width)
24549 - result->x);
24550
24551 /* Same game for Y. */
24552 if (r1->y < r2->y)
24553 upper = r1, lower = r2;
24554 else
24555 upper = r2, lower = r1;
24556
24557 /* The upper end of the intersection is lower.y0, if this is inside
24558 of upper. Otherwise, there is no intersection. */
24559 if (lower->y <= upper->y + upper->height)
24560 {
24561 result->y = lower->y;
24562
24563 /* The lower end of the intersection is the minimum of the lower
24564 ends of upper and lower. */
24565 result->height = (min (lower->y + lower->height,
24566 upper->y + upper->height)
24567 - result->y);
24568 intersection_p = 1;
24569 }
24570 }
24571
24572 return intersection_p;
24573 }
24574
24575 #endif /* HAVE_WINDOW_SYSTEM */
24576
24577 \f
24578 /***********************************************************************
24579 Initialization
24580 ***********************************************************************/
24581
24582 void
24583 syms_of_xdisp ()
24584 {
24585 Vwith_echo_area_save_vector = Qnil;
24586 staticpro (&Vwith_echo_area_save_vector);
24587
24588 Vmessage_stack = Qnil;
24589 staticpro (&Vmessage_stack);
24590
24591 Qinhibit_redisplay = intern ("inhibit-redisplay");
24592 staticpro (&Qinhibit_redisplay);
24593
24594 message_dolog_marker1 = Fmake_marker ();
24595 staticpro (&message_dolog_marker1);
24596 message_dolog_marker2 = Fmake_marker ();
24597 staticpro (&message_dolog_marker2);
24598 message_dolog_marker3 = Fmake_marker ();
24599 staticpro (&message_dolog_marker3);
24600
24601 #if GLYPH_DEBUG
24602 defsubr (&Sdump_frame_glyph_matrix);
24603 defsubr (&Sdump_glyph_matrix);
24604 defsubr (&Sdump_glyph_row);
24605 defsubr (&Sdump_tool_bar_row);
24606 defsubr (&Strace_redisplay);
24607 defsubr (&Strace_to_stderr);
24608 #endif
24609 #ifdef HAVE_WINDOW_SYSTEM
24610 defsubr (&Stool_bar_lines_needed);
24611 defsubr (&Slookup_image_map);
24612 #endif
24613 defsubr (&Sformat_mode_line);
24614 defsubr (&Sinvisible_p);
24615
24616 staticpro (&Qmenu_bar_update_hook);
24617 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24618
24619 staticpro (&Qoverriding_terminal_local_map);
24620 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24621
24622 staticpro (&Qoverriding_local_map);
24623 Qoverriding_local_map = intern ("overriding-local-map");
24624
24625 staticpro (&Qwindow_scroll_functions);
24626 Qwindow_scroll_functions = intern ("window-scroll-functions");
24627
24628 staticpro (&Qwindow_text_change_functions);
24629 Qwindow_text_change_functions = intern ("window-text-change-functions");
24630
24631 staticpro (&Qredisplay_end_trigger_functions);
24632 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24633
24634 staticpro (&Qinhibit_point_motion_hooks);
24635 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24636
24637 Qeval = intern ("eval");
24638 staticpro (&Qeval);
24639
24640 QCdata = intern (":data");
24641 staticpro (&QCdata);
24642 Qdisplay = intern ("display");
24643 staticpro (&Qdisplay);
24644 Qspace_width = intern ("space-width");
24645 staticpro (&Qspace_width);
24646 Qraise = intern ("raise");
24647 staticpro (&Qraise);
24648 Qslice = intern ("slice");
24649 staticpro (&Qslice);
24650 Qspace = intern ("space");
24651 staticpro (&Qspace);
24652 Qmargin = intern ("margin");
24653 staticpro (&Qmargin);
24654 Qpointer = intern ("pointer");
24655 staticpro (&Qpointer);
24656 Qleft_margin = intern ("left-margin");
24657 staticpro (&Qleft_margin);
24658 Qright_margin = intern ("right-margin");
24659 staticpro (&Qright_margin);
24660 Qcenter = intern ("center");
24661 staticpro (&Qcenter);
24662 Qline_height = intern ("line-height");
24663 staticpro (&Qline_height);
24664 QCalign_to = intern (":align-to");
24665 staticpro (&QCalign_to);
24666 QCrelative_width = intern (":relative-width");
24667 staticpro (&QCrelative_width);
24668 QCrelative_height = intern (":relative-height");
24669 staticpro (&QCrelative_height);
24670 QCeval = intern (":eval");
24671 staticpro (&QCeval);
24672 QCpropertize = intern (":propertize");
24673 staticpro (&QCpropertize);
24674 QCfile = intern (":file");
24675 staticpro (&QCfile);
24676 Qfontified = intern ("fontified");
24677 staticpro (&Qfontified);
24678 Qfontification_functions = intern ("fontification-functions");
24679 staticpro (&Qfontification_functions);
24680 Qtrailing_whitespace = intern ("trailing-whitespace");
24681 staticpro (&Qtrailing_whitespace);
24682 Qescape_glyph = intern ("escape-glyph");
24683 staticpro (&Qescape_glyph);
24684 Qnobreak_space = intern ("nobreak-space");
24685 staticpro (&Qnobreak_space);
24686 Qimage = intern ("image");
24687 staticpro (&Qimage);
24688 QCmap = intern (":map");
24689 staticpro (&QCmap);
24690 QCpointer = intern (":pointer");
24691 staticpro (&QCpointer);
24692 Qrect = intern ("rect");
24693 staticpro (&Qrect);
24694 Qcircle = intern ("circle");
24695 staticpro (&Qcircle);
24696 Qpoly = intern ("poly");
24697 staticpro (&Qpoly);
24698 Qmessage_truncate_lines = intern ("message-truncate-lines");
24699 staticpro (&Qmessage_truncate_lines);
24700 Qgrow_only = intern ("grow-only");
24701 staticpro (&Qgrow_only);
24702 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24703 staticpro (&Qinhibit_menubar_update);
24704 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24705 staticpro (&Qinhibit_eval_during_redisplay);
24706 Qposition = intern ("position");
24707 staticpro (&Qposition);
24708 Qbuffer_position = intern ("buffer-position");
24709 staticpro (&Qbuffer_position);
24710 Qobject = intern ("object");
24711 staticpro (&Qobject);
24712 Qbar = intern ("bar");
24713 staticpro (&Qbar);
24714 Qhbar = intern ("hbar");
24715 staticpro (&Qhbar);
24716 Qbox = intern ("box");
24717 staticpro (&Qbox);
24718 Qhollow = intern ("hollow");
24719 staticpro (&Qhollow);
24720 Qhand = intern ("hand");
24721 staticpro (&Qhand);
24722 Qarrow = intern ("arrow");
24723 staticpro (&Qarrow);
24724 Qtext = intern ("text");
24725 staticpro (&Qtext);
24726 Qrisky_local_variable = intern ("risky-local-variable");
24727 staticpro (&Qrisky_local_variable);
24728 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24729 staticpro (&Qinhibit_free_realized_faces);
24730
24731 list_of_error = Fcons (Fcons (intern ("error"),
24732 Fcons (intern ("void-variable"), Qnil)),
24733 Qnil);
24734 staticpro (&list_of_error);
24735
24736 Qlast_arrow_position = intern ("last-arrow-position");
24737 staticpro (&Qlast_arrow_position);
24738 Qlast_arrow_string = intern ("last-arrow-string");
24739 staticpro (&Qlast_arrow_string);
24740
24741 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24742 staticpro (&Qoverlay_arrow_string);
24743 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24744 staticpro (&Qoverlay_arrow_bitmap);
24745
24746 echo_buffer[0] = echo_buffer[1] = Qnil;
24747 staticpro (&echo_buffer[0]);
24748 staticpro (&echo_buffer[1]);
24749
24750 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24751 staticpro (&echo_area_buffer[0]);
24752 staticpro (&echo_area_buffer[1]);
24753
24754 Vmessages_buffer_name = build_string ("*Messages*");
24755 staticpro (&Vmessages_buffer_name);
24756
24757 mode_line_proptrans_alist = Qnil;
24758 staticpro (&mode_line_proptrans_alist);
24759 mode_line_string_list = Qnil;
24760 staticpro (&mode_line_string_list);
24761 mode_line_string_face = Qnil;
24762 staticpro (&mode_line_string_face);
24763 mode_line_string_face_prop = Qnil;
24764 staticpro (&mode_line_string_face_prop);
24765 Vmode_line_unwind_vector = Qnil;
24766 staticpro (&Vmode_line_unwind_vector);
24767
24768 help_echo_string = Qnil;
24769 staticpro (&help_echo_string);
24770 help_echo_object = Qnil;
24771 staticpro (&help_echo_object);
24772 help_echo_window = Qnil;
24773 staticpro (&help_echo_window);
24774 previous_help_echo_string = Qnil;
24775 staticpro (&previous_help_echo_string);
24776 help_echo_pos = -1;
24777
24778 #ifdef HAVE_WINDOW_SYSTEM
24779 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24780 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24781 For example, if a block cursor is over a tab, it will be drawn as
24782 wide as that tab on the display. */);
24783 x_stretch_cursor_p = 0;
24784 #endif
24785
24786 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24787 doc: /* *Non-nil means highlight trailing whitespace.
24788 The face used for trailing whitespace is `trailing-whitespace'. */);
24789 Vshow_trailing_whitespace = Qnil;
24790
24791 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24792 doc: /* *Control highlighting of nobreak space and soft hyphen.
24793 A value of t means highlight the character itself (for nobreak space,
24794 use face `nobreak-space').
24795 A value of nil means no highlighting.
24796 Other values mean display the escape glyph followed by an ordinary
24797 space or ordinary hyphen. */);
24798 Vnobreak_char_display = Qt;
24799
24800 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24801 doc: /* *The pointer shape to show in void text areas.
24802 A value of nil means to show the text pointer. Other options are `arrow',
24803 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24804 Vvoid_text_area_pointer = Qarrow;
24805
24806 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24807 doc: /* Non-nil means don't actually do any redisplay.
24808 This is used for internal purposes. */);
24809 Vinhibit_redisplay = Qnil;
24810
24811 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24812 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24813 Vglobal_mode_string = Qnil;
24814
24815 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24816 doc: /* Marker for where to display an arrow on top of the buffer text.
24817 This must be the beginning of a line in order to work.
24818 See also `overlay-arrow-string'. */);
24819 Voverlay_arrow_position = Qnil;
24820
24821 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24822 doc: /* String to display as an arrow in non-window frames.
24823 See also `overlay-arrow-position'. */);
24824 Voverlay_arrow_string = build_string ("=>");
24825
24826 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24827 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24828 The symbols on this list are examined during redisplay to determine
24829 where to display overlay arrows. */);
24830 Voverlay_arrow_variable_list
24831 = Fcons (intern ("overlay-arrow-position"), Qnil);
24832
24833 DEFVAR_INT ("scroll-step", &scroll_step,
24834 doc: /* *The number of lines to try scrolling a window by when point moves out.
24835 If that fails to bring point back on frame, point is centered instead.
24836 If this is zero, point is always centered after it moves off frame.
24837 If you want scrolling to always be a line at a time, you should set
24838 `scroll-conservatively' to a large value rather than set this to 1. */);
24839
24840 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24841 doc: /* *Scroll up to this many lines, to bring point back on screen.
24842 If point moves off-screen, redisplay will scroll by up to
24843 `scroll-conservatively' lines in order to bring point just barely
24844 onto the screen again. If that cannot be done, then redisplay
24845 recenters point as usual.
24846
24847 A value of zero means always recenter point if it moves off screen. */);
24848 scroll_conservatively = 0;
24849
24850 DEFVAR_INT ("scroll-margin", &scroll_margin,
24851 doc: /* *Number of lines of margin at the top and bottom of a window.
24852 Recenter the window whenever point gets within this many lines
24853 of the top or bottom of the window. */);
24854 scroll_margin = 0;
24855
24856 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24857 doc: /* Pixels per inch value for non-window system displays.
24858 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24859 Vdisplay_pixels_per_inch = make_float (72.0);
24860
24861 #if GLYPH_DEBUG
24862 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24863 #endif
24864
24865 DEFVAR_LISP ("truncate-partial-width-windows",
24866 &Vtruncate_partial_width_windows,
24867 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24868 For an integer value, truncate lines in each window with less than the
24869 full frame width, provided the window width is less than that integer;
24870 otherwise, respect the value of `truncate-lines'.
24871
24872 For any other non-nil value, truncate lines in all windows with
24873 less than the full frame width.
24874
24875 A value of nil means to respect the value of `truncate-lines'.
24876
24877 If `word-wrap' is enabled, you might want to reduce this. */);
24878 Vtruncate_partial_width_windows = make_number (50);
24879
24880 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24881 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24882 Any other value means to use the appropriate face, `mode-line',
24883 `header-line', or `menu' respectively. */);
24884 mode_line_inverse_video = 1;
24885
24886 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24887 doc: /* *Maximum buffer size for which line number should be displayed.
24888 If the buffer is bigger than this, the line number does not appear
24889 in the mode line. A value of nil means no limit. */);
24890 Vline_number_display_limit = Qnil;
24891
24892 DEFVAR_INT ("line-number-display-limit-width",
24893 &line_number_display_limit_width,
24894 doc: /* *Maximum line width (in characters) for line number display.
24895 If the average length of the lines near point is bigger than this, then the
24896 line number may be omitted from the mode line. */);
24897 line_number_display_limit_width = 200;
24898
24899 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24900 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24901 highlight_nonselected_windows = 0;
24902
24903 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24904 doc: /* Non-nil if more than one frame is visible on this display.
24905 Minibuffer-only frames don't count, but iconified frames do.
24906 This variable is not guaranteed to be accurate except while processing
24907 `frame-title-format' and `icon-title-format'. */);
24908
24909 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24910 doc: /* Template for displaying the title bar of visible frames.
24911 \(Assuming the window manager supports this feature.)
24912
24913 This variable has the same structure as `mode-line-format', except that
24914 the %c and %l constructs are ignored. It is used only on frames for
24915 which no explicit name has been set \(see `modify-frame-parameters'). */);
24916
24917 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24918 doc: /* Template for displaying the title bar of an iconified frame.
24919 \(Assuming the window manager supports this feature.)
24920 This variable has the same structure as `mode-line-format' (which see),
24921 and is used only on frames for which no explicit name has been set
24922 \(see `modify-frame-parameters'). */);
24923 Vicon_title_format
24924 = Vframe_title_format
24925 = Fcons (intern ("multiple-frames"),
24926 Fcons (build_string ("%b"),
24927 Fcons (Fcons (empty_unibyte_string,
24928 Fcons (intern ("invocation-name"),
24929 Fcons (build_string ("@"),
24930 Fcons (intern ("system-name"),
24931 Qnil)))),
24932 Qnil)));
24933
24934 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24935 doc: /* Maximum number of lines to keep in the message log buffer.
24936 If nil, disable message logging. If t, log messages but don't truncate
24937 the buffer when it becomes large. */);
24938 Vmessage_log_max = make_number (100);
24939
24940 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24941 doc: /* Functions called before redisplay, if window sizes have changed.
24942 The value should be a list of functions that take one argument.
24943 Just before redisplay, for each frame, if any of its windows have changed
24944 size since the last redisplay, or have been split or deleted,
24945 all the functions in the list are called, with the frame as argument. */);
24946 Vwindow_size_change_functions = Qnil;
24947
24948 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24949 doc: /* List of functions to call before redisplaying a window with scrolling.
24950 Each function is called with two arguments, the window and its new
24951 display-start position. Note that these functions are also called by
24952 `set-window-buffer'. Also note that the value of `window-end' is not
24953 valid when these functions are called. */);
24954 Vwindow_scroll_functions = Qnil;
24955
24956 DEFVAR_LISP ("window-text-change-functions",
24957 &Vwindow_text_change_functions,
24958 doc: /* Functions to call in redisplay when text in the window might change. */);
24959 Vwindow_text_change_functions = Qnil;
24960
24961 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24962 doc: /* Functions called when redisplay of a window reaches the end trigger.
24963 Each function is called with two arguments, the window and the end trigger value.
24964 See `set-window-redisplay-end-trigger'. */);
24965 Vredisplay_end_trigger_functions = Qnil;
24966
24967 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24968 doc: /* *Non-nil means autoselect window with mouse pointer.
24969 If nil, do not autoselect windows.
24970 A positive number means delay autoselection by that many seconds: a
24971 window is autoselected only after the mouse has remained in that
24972 window for the duration of the delay.
24973 A negative number has a similar effect, but causes windows to be
24974 autoselected only after the mouse has stopped moving. \(Because of
24975 the way Emacs compares mouse events, you will occasionally wait twice
24976 that time before the window gets selected.\)
24977 Any other value means to autoselect window instantaneously when the
24978 mouse pointer enters it.
24979
24980 Autoselection selects the minibuffer only if it is active, and never
24981 unselects the minibuffer if it is active.
24982
24983 When customizing this variable make sure that the actual value of
24984 `focus-follows-mouse' matches the behavior of your window manager. */);
24985 Vmouse_autoselect_window = Qnil;
24986
24987 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24988 doc: /* *Non-nil means automatically resize tool-bars.
24989 This dynamically changes the tool-bar's height to the minimum height
24990 that is needed to make all tool-bar items visible.
24991 If value is `grow-only', the tool-bar's height is only increased
24992 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24993 Vauto_resize_tool_bars = Qt;
24994
24995 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24996 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24997 auto_raise_tool_bar_buttons_p = 1;
24998
24999 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25000 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25001 make_cursor_line_fully_visible_p = 1;
25002
25003 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25004 doc: /* *Border below tool-bar in pixels.
25005 If an integer, use it as the height of the border.
25006 If it is one of `internal-border-width' or `border-width', use the
25007 value of the corresponding frame parameter.
25008 Otherwise, no border is added below the tool-bar. */);
25009 Vtool_bar_border = Qinternal_border_width;
25010
25011 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25012 doc: /* *Margin around tool-bar buttons in pixels.
25013 If an integer, use that for both horizontal and vertical margins.
25014 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25015 HORZ specifying the horizontal margin, and VERT specifying the
25016 vertical margin. */);
25017 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25018
25019 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25020 doc: /* *Relief thickness of tool-bar buttons. */);
25021 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25022
25023 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25024 doc: /* List of functions to call to fontify regions of text.
25025 Each function is called with one argument POS. Functions must
25026 fontify a region starting at POS in the current buffer, and give
25027 fontified regions the property `fontified'. */);
25028 Vfontification_functions = Qnil;
25029 Fmake_variable_buffer_local (Qfontification_functions);
25030
25031 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25032 &unibyte_display_via_language_environment,
25033 doc: /* *Non-nil means display unibyte text according to language environment.
25034 Specifically this means that unibyte non-ASCII characters
25035 are displayed by converting them to the equivalent multibyte characters
25036 according to the current language environment. As a result, they are
25037 displayed according to the current fontset. */);
25038 unibyte_display_via_language_environment = 0;
25039
25040 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25041 doc: /* *Maximum height for resizing mini-windows.
25042 If a float, it specifies a fraction of the mini-window frame's height.
25043 If an integer, it specifies a number of lines. */);
25044 Vmax_mini_window_height = make_float (0.25);
25045
25046 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25047 doc: /* *How to resize mini-windows.
25048 A value of nil means don't automatically resize mini-windows.
25049 A value of t means resize them to fit the text displayed in them.
25050 A value of `grow-only', the default, means let mini-windows grow
25051 only, until their display becomes empty, at which point the windows
25052 go back to their normal size. */);
25053 Vresize_mini_windows = Qgrow_only;
25054
25055 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25056 doc: /* Alist specifying how to blink the cursor off.
25057 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25058 `cursor-type' frame-parameter or variable equals ON-STATE,
25059 comparing using `equal', Emacs uses OFF-STATE to specify
25060 how to blink it off. ON-STATE and OFF-STATE are values for
25061 the `cursor-type' frame parameter.
25062
25063 If a frame's ON-STATE has no entry in this list,
25064 the frame's other specifications determine how to blink the cursor off. */);
25065 Vblink_cursor_alist = Qnil;
25066
25067 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25068 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25069 automatic_hscrolling_p = 1;
25070 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25071 staticpro (&Qauto_hscroll_mode);
25072
25073 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25074 doc: /* *How many columns away from the window edge point is allowed to get
25075 before automatic hscrolling will horizontally scroll the window. */);
25076 hscroll_margin = 5;
25077
25078 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25079 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25080 When point is less than `hscroll-margin' columns from the window
25081 edge, automatic hscrolling will scroll the window by the amount of columns
25082 determined by this variable. If its value is a positive integer, scroll that
25083 many columns. If it's a positive floating-point number, it specifies the
25084 fraction of the window's width to scroll. If it's nil or zero, point will be
25085 centered horizontally after the scroll. Any other value, including negative
25086 numbers, are treated as if the value were zero.
25087
25088 Automatic hscrolling always moves point outside the scroll margin, so if
25089 point was more than scroll step columns inside the margin, the window will
25090 scroll more than the value given by the scroll step.
25091
25092 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25093 and `scroll-right' overrides this variable's effect. */);
25094 Vhscroll_step = make_number (0);
25095
25096 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25097 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25098 Bind this around calls to `message' to let it take effect. */);
25099 message_truncate_lines = 0;
25100
25101 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25102 doc: /* Normal hook run to update the menu bar definitions.
25103 Redisplay runs this hook before it redisplays the menu bar.
25104 This is used to update submenus such as Buffers,
25105 whose contents depend on various data. */);
25106 Vmenu_bar_update_hook = Qnil;
25107
25108 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25109 doc: /* Frame for which we are updating a menu.
25110 The enable predicate for a menu binding should check this variable. */);
25111 Vmenu_updating_frame = Qnil;
25112
25113 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25114 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25115 inhibit_menubar_update = 0;
25116
25117 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25118 doc: /* Prefix prepended to all continuation lines at display time.
25119 The value may be a string, an image, or a stretch-glyph; it is
25120 interpreted in the same way as the value of a `display' text property.
25121
25122 This variable is overridden by any `wrap-prefix' text or overlay
25123 property.
25124
25125 To add a prefix to non-continuation lines, use `line-prefix'. */);
25126 Vwrap_prefix = Qnil;
25127 staticpro (&Qwrap_prefix);
25128 Qwrap_prefix = intern ("wrap-prefix");
25129 Fmake_variable_buffer_local (Qwrap_prefix);
25130
25131 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25132 doc: /* Prefix prepended to all non-continuation lines at display time.
25133 The value may be a string, an image, or a stretch-glyph; it is
25134 interpreted in the same way as the value of a `display' text property.
25135
25136 This variable is overridden by any `line-prefix' text or overlay
25137 property.
25138
25139 To add a prefix to continuation lines, use `wrap-prefix'. */);
25140 Vline_prefix = Qnil;
25141 staticpro (&Qline_prefix);
25142 Qline_prefix = intern ("line-prefix");
25143 Fmake_variable_buffer_local (Qline_prefix);
25144
25145 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25146 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25147 inhibit_eval_during_redisplay = 0;
25148
25149 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25150 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25151 inhibit_free_realized_faces = 0;
25152
25153 #if GLYPH_DEBUG
25154 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25155 doc: /* Inhibit try_window_id display optimization. */);
25156 inhibit_try_window_id = 0;
25157
25158 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25159 doc: /* Inhibit try_window_reusing display optimization. */);
25160 inhibit_try_window_reusing = 0;
25161
25162 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25163 doc: /* Inhibit try_cursor_movement display optimization. */);
25164 inhibit_try_cursor_movement = 0;
25165 #endif /* GLYPH_DEBUG */
25166
25167 DEFVAR_INT ("overline-margin", &overline_margin,
25168 doc: /* *Space between overline and text, in pixels.
25169 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25170 margin to the caracter height. */);
25171 overline_margin = 2;
25172
25173 DEFVAR_INT ("underline-minimum-offset",
25174 &underline_minimum_offset,
25175 doc: /* Minimum distance between baseline and underline.
25176 This can improve legibility of underlined text at small font sizes,
25177 particularly when using variable `x-use-underline-position-properties'
25178 with fonts that specify an UNDERLINE_POSITION relatively close to the
25179 baseline. The default value is 1. */);
25180 underline_minimum_offset = 1;
25181
25182 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25183 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25184 display_hourglass_p = 1;
25185
25186 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25187 doc: /* *Seconds to wait before displaying an hourglass pointer.
25188 Value must be an integer or float. */);
25189 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25190
25191 hourglass_atimer = NULL;
25192 hourglass_shown_p = 0;
25193 }
25194
25195
25196 /* Initialize this module when Emacs starts. */
25197
25198 void
25199 init_xdisp ()
25200 {
25201 Lisp_Object root_window;
25202 struct window *mini_w;
25203
25204 current_header_line_height = current_mode_line_height = -1;
25205
25206 CHARPOS (this_line_start_pos) = 0;
25207
25208 mini_w = XWINDOW (minibuf_window);
25209 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25210
25211 if (!noninteractive)
25212 {
25213 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25214 int i;
25215
25216 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25217 set_window_height (root_window,
25218 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25219 0);
25220 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25221 set_window_height (minibuf_window, 1, 0);
25222
25223 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25224 mini_w->total_cols = make_number (FRAME_COLS (f));
25225
25226 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25227 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25228 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25229
25230 /* The default ellipsis glyphs `...'. */
25231 for (i = 0; i < 3; ++i)
25232 default_invis_vector[i] = make_number ('.');
25233 }
25234
25235 {
25236 /* Allocate the buffer for frame titles.
25237 Also used for `format-mode-line'. */
25238 int size = 100;
25239 mode_line_noprop_buf = (char *) xmalloc (size);
25240 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25241 mode_line_noprop_ptr = mode_line_noprop_buf;
25242 mode_line_target = MODE_LINE_DISPLAY;
25243 }
25244
25245 help_echo_showing_p = 0;
25246 }
25247
25248 /* Since w32 does not support atimers, it defines its own implementation of
25249 the following three functions in w32fns.c. */
25250 #ifndef WINDOWSNT
25251
25252 /* Platform-independent portion of hourglass implementation. */
25253
25254 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25255 int
25256 hourglass_started ()
25257 {
25258 return hourglass_shown_p || hourglass_atimer != NULL;
25259 }
25260
25261 /* Cancel a currently active hourglass timer, and start a new one. */
25262 void
25263 start_hourglass ()
25264 {
25265 #if defined (HAVE_WINDOW_SYSTEM)
25266 EMACS_TIME delay;
25267 int secs, usecs = 0;
25268
25269 cancel_hourglass ();
25270
25271 if (INTEGERP (Vhourglass_delay)
25272 && XINT (Vhourglass_delay) > 0)
25273 secs = XFASTINT (Vhourglass_delay);
25274 else if (FLOATP (Vhourglass_delay)
25275 && XFLOAT_DATA (Vhourglass_delay) > 0)
25276 {
25277 Lisp_Object tem;
25278 tem = Ftruncate (Vhourglass_delay, Qnil);
25279 secs = XFASTINT (tem);
25280 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25281 }
25282 else
25283 secs = DEFAULT_HOURGLASS_DELAY;
25284
25285 EMACS_SET_SECS_USECS (delay, secs, usecs);
25286 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25287 show_hourglass, NULL);
25288 #endif
25289 }
25290
25291
25292 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25293 shown. */
25294 void
25295 cancel_hourglass ()
25296 {
25297 #if defined (HAVE_WINDOW_SYSTEM)
25298 if (hourglass_atimer)
25299 {
25300 cancel_atimer (hourglass_atimer);
25301 hourglass_atimer = NULL;
25302 }
25303
25304 if (hourglass_shown_p)
25305 hide_hourglass ();
25306 #endif
25307 }
25308 #endif /* ! WINDOWSNT */
25309
25310 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25311 (do not change this comment) */