Finish work on cursor motion in continuation lines.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171 #include <limits.h>
172 #include <setjmp.h>
173
174 #include "lisp.h"
175 #include "keyboard.h"
176 #include "frame.h"
177 #include "window.h"
178 #include "termchar.h"
179 #include "dispextern.h"
180 #include "buffer.h"
181 #include "character.h"
182 #include "charset.h"
183 #include "indent.h"
184 #include "commands.h"
185 #include "keymap.h"
186 #include "macros.h"
187 #include "disptab.h"
188 #include "termhooks.h"
189 #include "intervals.h"
190 #include "coding.h"
191 #include "process.h"
192 #include "region-cache.h"
193 #include "font.h"
194 #include "fontset.h"
195 #include "blockinput.h"
196
197 #ifdef HAVE_X_WINDOWS
198 #include "xterm.h"
199 #endif
200 #ifdef WINDOWSNT
201 #include "w32term.h"
202 #endif
203 #ifdef HAVE_NS
204 #include "nsterm.h"
205 #endif
206 #ifdef USE_GTK
207 #include "gtkutil.h"
208 #endif
209
210 #include "font.h"
211
212 #ifndef FRAME_X_OUTPUT
213 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
214 #endif
215
216 #define INFINITY 10000000
217
218 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
219 || defined(HAVE_NS) || defined (USE_GTK)
220 extern void set_frame_menubar P_ ((struct frame *f, int, int));
221 extern int pending_menu_activation;
222 #endif
223
224 extern int interrupt_input;
225 extern int command_loop_level;
226
227 extern Lisp_Object do_mouse_tracking;
228
229 extern int minibuffer_auto_raise;
230 extern Lisp_Object Vminibuffer_list;
231
232 extern Lisp_Object Qface;
233 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
234
235 extern Lisp_Object Voverriding_local_map;
236 extern Lisp_Object Voverriding_local_map_menu_flag;
237 extern Lisp_Object Qmenu_item;
238 extern Lisp_Object Qwhen;
239 extern Lisp_Object Qhelp_echo;
240 extern Lisp_Object Qbefore_string, Qafter_string;
241
242 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
243 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
244 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
245 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
246 Lisp_Object Qinhibit_point_motion_hooks;
247 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
248 Lisp_Object Qfontified;
249 Lisp_Object Qgrow_only;
250 Lisp_Object Qinhibit_eval_during_redisplay;
251 Lisp_Object Qbuffer_position, Qposition, Qobject;
252 Lisp_Object Qright_to_left, Qleft_to_right;
253
254 /* Cursor shapes */
255 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
256
257 /* Pointer shapes */
258 Lisp_Object Qarrow, Qhand, Qtext;
259
260 Lisp_Object Qrisky_local_variable;
261
262 /* Holds the list (error). */
263 Lisp_Object list_of_error;
264
265 /* Functions called to fontify regions of text. */
266
267 Lisp_Object Vfontification_functions;
268 Lisp_Object Qfontification_functions;
269
270 /* Non-nil means automatically select any window when the mouse
271 cursor moves into it. */
272 Lisp_Object Vmouse_autoselect_window;
273
274 Lisp_Object Vwrap_prefix, Qwrap_prefix;
275 Lisp_Object Vline_prefix, Qline_prefix;
276
277 /* Non-zero means draw tool bar buttons raised when the mouse moves
278 over them. */
279
280 int auto_raise_tool_bar_buttons_p;
281
282 /* Non-zero means to reposition window if cursor line is only partially visible. */
283
284 int make_cursor_line_fully_visible_p;
285
286 /* Margin below tool bar in pixels. 0 or nil means no margin.
287 If value is `internal-border-width' or `border-width',
288 the corresponding frame parameter is used. */
289
290 Lisp_Object Vtool_bar_border;
291
292 /* Margin around tool bar buttons in pixels. */
293
294 Lisp_Object Vtool_bar_button_margin;
295
296 /* Thickness of shadow to draw around tool bar buttons. */
297
298 EMACS_INT tool_bar_button_relief;
299
300 /* Non-nil means automatically resize tool-bars so that all tool-bar
301 items are visible, and no blank lines remain.
302
303 If value is `grow-only', only make tool-bar bigger. */
304
305 Lisp_Object Vauto_resize_tool_bars;
306
307 /* Non-zero means draw block and hollow cursor as wide as the glyph
308 under it. For example, if a block cursor is over a tab, it will be
309 drawn as wide as that tab on the display. */
310
311 int x_stretch_cursor_p;
312
313 /* Non-nil means don't actually do any redisplay. */
314
315 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
316
317 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
318
319 int inhibit_eval_during_redisplay;
320
321 /* Names of text properties relevant for redisplay. */
322
323 Lisp_Object Qdisplay;
324 extern Lisp_Object Qface, Qinvisible, Qwidth;
325
326 /* Symbols used in text property values. */
327
328 Lisp_Object Vdisplay_pixels_per_inch;
329 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
330 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
331 Lisp_Object Qslice;
332 Lisp_Object Qcenter;
333 Lisp_Object Qmargin, Qpointer;
334 Lisp_Object Qline_height;
335 extern Lisp_Object Qheight;
336 extern Lisp_Object QCwidth, QCheight, QCascent;
337 extern Lisp_Object Qscroll_bar;
338 extern Lisp_Object Qcursor;
339
340 /* Non-nil means highlight trailing whitespace. */
341
342 Lisp_Object Vshow_trailing_whitespace;
343
344 /* Non-nil means escape non-break space and hyphens. */
345
346 Lisp_Object Vnobreak_char_display;
347
348 #ifdef HAVE_WINDOW_SYSTEM
349 extern Lisp_Object Voverflow_newline_into_fringe;
350
351 /* Test if overflow newline into fringe. Called with iterator IT
352 at or past right window margin, and with IT->current_x set. */
353
354 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
355 (!NILP (Voverflow_newline_into_fringe) \
356 && FRAME_WINDOW_P (it->f) \
357 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
358 && it->current_x == it->last_visible_x \
359 && it->line_wrap != WORD_WRAP)
360
361 #else /* !HAVE_WINDOW_SYSTEM */
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
363 #endif /* HAVE_WINDOW_SYSTEM */
364
365 /* Test if the display element loaded in IT is a space or tab
366 character. This is used to determine word wrapping. */
367
368 #define IT_DISPLAYING_WHITESPACE(it) \
369 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
370
371 /* Non-nil means show the text cursor in void text areas
372 i.e. in blank areas after eol and eob. This used to be
373 the default in 21.3. */
374
375 Lisp_Object Vvoid_text_area_pointer;
376
377 /* Name of the face used to highlight trailing whitespace. */
378
379 Lisp_Object Qtrailing_whitespace;
380
381 /* Name and number of the face used to highlight escape glyphs. */
382
383 Lisp_Object Qescape_glyph;
384
385 /* Name and number of the face used to highlight non-breaking spaces. */
386
387 Lisp_Object Qnobreak_space;
388
389 /* The symbol `image' which is the car of the lists used to represent
390 images in Lisp. */
391
392 Lisp_Object Qimage;
393
394 /* The image map types. */
395 Lisp_Object QCmap, QCpointer;
396 Lisp_Object Qrect, Qcircle, Qpoly;
397
398 /* Non-zero means print newline to stdout before next mini-buffer
399 message. */
400
401 int noninteractive_need_newline;
402
403 /* Non-zero means print newline to message log before next message. */
404
405 static int message_log_need_newline;
406
407 /* Three markers that message_dolog uses.
408 It could allocate them itself, but that causes trouble
409 in handling memory-full errors. */
410 static Lisp_Object message_dolog_marker1;
411 static Lisp_Object message_dolog_marker2;
412 static Lisp_Object message_dolog_marker3;
413 \f
414 /* The buffer position of the first character appearing entirely or
415 partially on the line of the selected window which contains the
416 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
417 redisplay optimization in redisplay_internal. */
418
419 static struct text_pos this_line_start_pos;
420
421 /* Number of characters past the end of the line above, including the
422 terminating newline. */
423
424 static struct text_pos this_line_end_pos;
425
426 /* The vertical positions and the height of this line. */
427
428 static int this_line_vpos;
429 static int this_line_y;
430 static int this_line_pixel_height;
431
432 /* X position at which this display line starts. Usually zero;
433 negative if first character is partially visible. */
434
435 static int this_line_start_x;
436
437 /* Buffer that this_line_.* variables are referring to. */
438
439 static struct buffer *this_line_buffer;
440
441 /* Nonzero means truncate lines in all windows less wide than the
442 frame. */
443
444 Lisp_Object Vtruncate_partial_width_windows;
445
446 /* A flag to control how to display unibyte 8-bit character. */
447
448 int unibyte_display_via_language_environment;
449
450 /* Nonzero means we have more than one non-mini-buffer-only frame.
451 Not guaranteed to be accurate except while parsing
452 frame-title-format. */
453
454 int multiple_frames;
455
456 Lisp_Object Vglobal_mode_string;
457
458
459 /* List of variables (symbols) which hold markers for overlay arrows.
460 The symbols on this list are examined during redisplay to determine
461 where to display overlay arrows. */
462
463 Lisp_Object Voverlay_arrow_variable_list;
464
465 /* Marker for where to display an arrow on top of the buffer text. */
466
467 Lisp_Object Voverlay_arrow_position;
468
469 /* String to display for the arrow. Only used on terminal frames. */
470
471 Lisp_Object Voverlay_arrow_string;
472
473 /* Values of those variables at last redisplay are stored as
474 properties on `overlay-arrow-position' symbol. However, if
475 Voverlay_arrow_position is a marker, last-arrow-position is its
476 numerical position. */
477
478 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
479
480 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
481 properties on a symbol in overlay-arrow-variable-list. */
482
483 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
484
485 /* Like mode-line-format, but for the title bar on a visible frame. */
486
487 Lisp_Object Vframe_title_format;
488
489 /* Like mode-line-format, but for the title bar on an iconified frame. */
490
491 Lisp_Object Vicon_title_format;
492
493 /* List of functions to call when a window's size changes. These
494 functions get one arg, a frame on which one or more windows' sizes
495 have changed. */
496
497 static Lisp_Object Vwindow_size_change_functions;
498
499 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
500
501 /* Nonzero if an overlay arrow has been displayed in this window. */
502
503 static int overlay_arrow_seen;
504
505 /* Nonzero means highlight the region even in nonselected windows. */
506
507 int highlight_nonselected_windows;
508
509 /* If cursor motion alone moves point off frame, try scrolling this
510 many lines up or down if that will bring it back. */
511
512 static EMACS_INT scroll_step;
513
514 /* Nonzero means scroll just far enough to bring point back on the
515 screen, when appropriate. */
516
517 static EMACS_INT scroll_conservatively;
518
519 /* Recenter the window whenever point gets within this many lines of
520 the top or bottom of the window. This value is translated into a
521 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
522 that there is really a fixed pixel height scroll margin. */
523
524 EMACS_INT scroll_margin;
525
526 /* Number of windows showing the buffer of the selected window (or
527 another buffer with the same base buffer). keyboard.c refers to
528 this. */
529
530 int buffer_shared;
531
532 /* Vector containing glyphs for an ellipsis `...'. */
533
534 static Lisp_Object default_invis_vector[3];
535
536 /* Zero means display the mode-line/header-line/menu-bar in the default face
537 (this slightly odd definition is for compatibility with previous versions
538 of emacs), non-zero means display them using their respective faces.
539
540 This variable is deprecated. */
541
542 int mode_line_inverse_video;
543
544 /* Prompt to display in front of the mini-buffer contents. */
545
546 Lisp_Object minibuf_prompt;
547
548 /* Width of current mini-buffer prompt. Only set after display_line
549 of the line that contains the prompt. */
550
551 int minibuf_prompt_width;
552
553 /* This is the window where the echo area message was displayed. It
554 is always a mini-buffer window, but it may not be the same window
555 currently active as a mini-buffer. */
556
557 Lisp_Object echo_area_window;
558
559 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
560 pushes the current message and the value of
561 message_enable_multibyte on the stack, the function restore_message
562 pops the stack and displays MESSAGE again. */
563
564 Lisp_Object Vmessage_stack;
565
566 /* Nonzero means multibyte characters were enabled when the echo area
567 message was specified. */
568
569 int message_enable_multibyte;
570
571 /* Nonzero if we should redraw the mode lines on the next redisplay. */
572
573 int update_mode_lines;
574
575 /* Nonzero if window sizes or contents have changed since last
576 redisplay that finished. */
577
578 int windows_or_buffers_changed;
579
580 /* Nonzero means a frame's cursor type has been changed. */
581
582 int cursor_type_changed;
583
584 /* Nonzero after display_mode_line if %l was used and it displayed a
585 line number. */
586
587 int line_number_displayed;
588
589 /* Maximum buffer size for which to display line numbers. */
590
591 Lisp_Object Vline_number_display_limit;
592
593 /* Line width to consider when repositioning for line number display. */
594
595 static EMACS_INT line_number_display_limit_width;
596
597 /* Number of lines to keep in the message log buffer. t means
598 infinite. nil means don't log at all. */
599
600 Lisp_Object Vmessage_log_max;
601
602 /* The name of the *Messages* buffer, a string. */
603
604 static Lisp_Object Vmessages_buffer_name;
605
606 /* Current, index 0, and last displayed echo area message. Either
607 buffers from echo_buffers, or nil to indicate no message. */
608
609 Lisp_Object echo_area_buffer[2];
610
611 /* The buffers referenced from echo_area_buffer. */
612
613 static Lisp_Object echo_buffer[2];
614
615 /* A vector saved used in with_area_buffer to reduce consing. */
616
617 static Lisp_Object Vwith_echo_area_save_vector;
618
619 /* Non-zero means display_echo_area should display the last echo area
620 message again. Set by redisplay_preserve_echo_area. */
621
622 static int display_last_displayed_message_p;
623
624 /* Nonzero if echo area is being used by print; zero if being used by
625 message. */
626
627 int message_buf_print;
628
629 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
630
631 Lisp_Object Qinhibit_menubar_update;
632 int inhibit_menubar_update;
633
634 /* When evaluating expressions from menu bar items (enable conditions,
635 for instance), this is the frame they are being processed for. */
636
637 Lisp_Object Vmenu_updating_frame;
638
639 /* Maximum height for resizing mini-windows. Either a float
640 specifying a fraction of the available height, or an integer
641 specifying a number of lines. */
642
643 Lisp_Object Vmax_mini_window_height;
644
645 /* Non-zero means messages should be displayed with truncated
646 lines instead of being continued. */
647
648 int message_truncate_lines;
649 Lisp_Object Qmessage_truncate_lines;
650
651 /* Set to 1 in clear_message to make redisplay_internal aware
652 of an emptied echo area. */
653
654 static int message_cleared_p;
655
656 /* How to blink the default frame cursor off. */
657 Lisp_Object Vblink_cursor_alist;
658
659 /* A scratch glyph row with contents used for generating truncation
660 glyphs. Also used in direct_output_for_insert. */
661
662 #define MAX_SCRATCH_GLYPHS 100
663 struct glyph_row scratch_glyph_row;
664 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
665
666 /* Ascent and height of the last line processed by move_it_to. */
667
668 static int last_max_ascent, last_height;
669
670 /* Non-zero if there's a help-echo in the echo area. */
671
672 int help_echo_showing_p;
673
674 /* If >= 0, computed, exact values of mode-line and header-line height
675 to use in the macros CURRENT_MODE_LINE_HEIGHT and
676 CURRENT_HEADER_LINE_HEIGHT. */
677
678 int current_mode_line_height, current_header_line_height;
679
680 /* The maximum distance to look ahead for text properties. Values
681 that are too small let us call compute_char_face and similar
682 functions too often which is expensive. Values that are too large
683 let us call compute_char_face and alike too often because we
684 might not be interested in text properties that far away. */
685
686 #define TEXT_PROP_DISTANCE_LIMIT 100
687
688 #if GLYPH_DEBUG
689
690 /* Variables to turn off display optimizations from Lisp. */
691
692 int inhibit_try_window_id, inhibit_try_window_reusing;
693 int inhibit_try_cursor_movement;
694
695 /* Non-zero means print traces of redisplay if compiled with
696 GLYPH_DEBUG != 0. */
697
698 int trace_redisplay_p;
699
700 #endif /* GLYPH_DEBUG */
701
702 #ifdef DEBUG_TRACE_MOVE
703 /* Non-zero means trace with TRACE_MOVE to stderr. */
704 int trace_move;
705
706 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
707 #else
708 #define TRACE_MOVE(x) (void) 0
709 #endif
710
711 /* Non-zero means automatically scroll windows horizontally to make
712 point visible. */
713
714 int automatic_hscrolling_p;
715 Lisp_Object Qauto_hscroll_mode;
716
717 /* How close to the margin can point get before the window is scrolled
718 horizontally. */
719 EMACS_INT hscroll_margin;
720
721 /* How much to scroll horizontally when point is inside the above margin. */
722 Lisp_Object Vhscroll_step;
723
724 /* The variable `resize-mini-windows'. If nil, don't resize
725 mini-windows. If t, always resize them to fit the text they
726 display. If `grow-only', let mini-windows grow only until they
727 become empty. */
728
729 Lisp_Object Vresize_mini_windows;
730
731 /* Buffer being redisplayed -- for redisplay_window_error. */
732
733 struct buffer *displayed_buffer;
734
735 /* Space between overline and text. */
736
737 EMACS_INT overline_margin;
738
739 /* Require underline to be at least this many screen pixels below baseline
740 This to avoid underline "merging" with the base of letters at small
741 font sizes, particularly when x_use_underline_position_properties is on. */
742
743 EMACS_INT underline_minimum_offset;
744
745 /* Value returned from text property handlers (see below). */
746
747 enum prop_handled
748 {
749 HANDLED_NORMALLY,
750 HANDLED_RECOMPUTE_PROPS,
751 HANDLED_OVERLAY_STRING_CONSUMED,
752 HANDLED_RETURN
753 };
754
755 /* A description of text properties that redisplay is interested
756 in. */
757
758 struct props
759 {
760 /* The name of the property. */
761 Lisp_Object *name;
762
763 /* A unique index for the property. */
764 enum prop_idx idx;
765
766 /* A handler function called to set up iterator IT from the property
767 at IT's current position. Value is used to steer handle_stop. */
768 enum prop_handled (*handler) P_ ((struct it *it));
769 };
770
771 static enum prop_handled handle_face_prop P_ ((struct it *));
772 static enum prop_handled handle_invisible_prop P_ ((struct it *));
773 static enum prop_handled handle_display_prop P_ ((struct it *));
774 static enum prop_handled handle_composition_prop P_ ((struct it *));
775 static enum prop_handled handle_overlay_change P_ ((struct it *));
776 static enum prop_handled handle_fontified_prop P_ ((struct it *));
777
778 /* Properties handled by iterators. */
779
780 static struct props it_props[] =
781 {
782 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
783 /* Handle `face' before `display' because some sub-properties of
784 `display' need to know the face. */
785 {&Qface, FACE_PROP_IDX, handle_face_prop},
786 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
787 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
788 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
789 {NULL, 0, NULL}
790 };
791
792 /* Value is the position described by X. If X is a marker, value is
793 the marker_position of X. Otherwise, value is X. */
794
795 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
796
797 /* Enumeration returned by some move_it_.* functions internally. */
798
799 enum move_it_result
800 {
801 /* Not used. Undefined value. */
802 MOVE_UNDEFINED,
803
804 /* Move ended at the requested buffer position or ZV. */
805 MOVE_POS_MATCH_OR_ZV,
806
807 /* Move ended at the requested X pixel position. */
808 MOVE_X_REACHED,
809
810 /* Move within a line ended at the end of a line that must be
811 continued. */
812 MOVE_LINE_CONTINUED,
813
814 /* Move within a line ended at the end of a line that would
815 be displayed truncated. */
816 MOVE_LINE_TRUNCATED,
817
818 /* Move within a line ended at a line end. */
819 MOVE_NEWLINE_OR_CR
820 };
821
822 /* This counter is used to clear the face cache every once in a while
823 in redisplay_internal. It is incremented for each redisplay.
824 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
825 cleared. */
826
827 #define CLEAR_FACE_CACHE_COUNT 500
828 static int clear_face_cache_count;
829
830 /* Similarly for the image cache. */
831
832 #ifdef HAVE_WINDOW_SYSTEM
833 #define CLEAR_IMAGE_CACHE_COUNT 101
834 static int clear_image_cache_count;
835 #endif
836
837 /* Non-zero while redisplay_internal is in progress. */
838
839 int redisplaying_p;
840
841 /* Non-zero means don't free realized faces. Bound while freeing
842 realized faces is dangerous because glyph matrices might still
843 reference them. */
844
845 int inhibit_free_realized_faces;
846 Lisp_Object Qinhibit_free_realized_faces;
847
848 /* If a string, XTread_socket generates an event to display that string.
849 (The display is done in read_char.) */
850
851 Lisp_Object help_echo_string;
852 Lisp_Object help_echo_window;
853 Lisp_Object help_echo_object;
854 int help_echo_pos;
855
856 /* Temporary variable for XTread_socket. */
857
858 Lisp_Object previous_help_echo_string;
859
860 /* Null glyph slice */
861
862 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
863
864 /* Platform-independent portion of hourglass implementation. */
865
866 /* Non-zero means we're allowed to display a hourglass pointer. */
867 int display_hourglass_p;
868
869 /* Non-zero means an hourglass cursor is currently shown. */
870 int hourglass_shown_p;
871
872 /* If non-null, an asynchronous timer that, when it expires, displays
873 an hourglass cursor on all frames. */
874 struct atimer *hourglass_atimer;
875
876 /* Number of seconds to wait before displaying an hourglass cursor. */
877 Lisp_Object Vhourglass_delay;
878
879 /* Default number of seconds to wait before displaying an hourglass
880 cursor. */
881 #define DEFAULT_HOURGLASS_DELAY 1
882
883 \f
884 /* Function prototypes. */
885
886 static void setup_for_ellipsis P_ ((struct it *, int));
887 static void mark_window_display_accurate_1 P_ ((struct window *, int));
888 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
889 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
890 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
891 static int redisplay_mode_lines P_ ((Lisp_Object, int));
892 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
893
894 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
895
896 static void handle_line_prefix P_ ((struct it *));
897
898 static void pint2str P_ ((char *, int, int));
899 static void pint2hrstr P_ ((char *, int, int));
900 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
901 struct text_pos));
902 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
903 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
904 static void store_mode_line_noprop_char P_ ((char));
905 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
906 static void x_consider_frame_title P_ ((Lisp_Object));
907 static void handle_stop P_ ((struct it *));
908 static void handle_stop_backwards P_ ((struct it *, EMACS_INT));
909 static int tool_bar_lines_needed P_ ((struct frame *, int *));
910 static int single_display_spec_intangible_p P_ ((Lisp_Object));
911 static void ensure_echo_area_buffers P_ ((void));
912 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
913 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
914 static int with_echo_area_buffer P_ ((struct window *, int,
915 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
916 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static void clear_garbaged_frames P_ ((void));
918 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
920 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
921 static int display_echo_area P_ ((struct window *));
922 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
923 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
924 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
925 static int string_char_and_length P_ ((const unsigned char *, int *));
926 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
927 struct text_pos));
928 static int compute_window_start_on_continuation_line P_ ((struct window *));
929 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
930 static void insert_left_trunc_glyphs P_ ((struct it *));
931 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
932 Lisp_Object));
933 static void extend_face_to_end_of_line P_ ((struct it *));
934 static int append_space_for_newline P_ ((struct it *, int));
935 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
936 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
937 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
938 static int trailing_whitespace_p P_ ((int));
939 static int message_log_check_duplicate P_ ((int, int, int, int));
940 static void push_it P_ ((struct it *));
941 static void pop_it P_ ((struct it *));
942 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
943 static void select_frame_for_redisplay P_ ((Lisp_Object));
944 static void redisplay_internal P_ ((int));
945 static int echo_area_display P_ ((int));
946 static void redisplay_windows P_ ((Lisp_Object));
947 static void redisplay_window P_ ((Lisp_Object, int));
948 static Lisp_Object redisplay_window_error ();
949 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
950 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
951 static int update_menu_bar P_ ((struct frame *, int, int));
952 static int try_window_reusing_current_matrix P_ ((struct window *));
953 static int try_window_id P_ ((struct window *));
954 static int display_line P_ ((struct it *));
955 static int display_mode_lines P_ ((struct window *));
956 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
957 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
958 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
959 static char *decode_mode_spec P_ ((struct window *, int, int, int,
960 Lisp_Object *));
961 static void display_menu_bar P_ ((struct window *));
962 static int display_count_lines P_ ((int, int, int, int, int *));
963 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
964 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
965 static void compute_line_metrics P_ ((struct it *));
966 static void run_redisplay_end_trigger_hook P_ ((struct it *));
967 static int get_overlay_strings P_ ((struct it *, int));
968 static int get_overlay_strings_1 P_ ((struct it *, int, int));
969 static void next_overlay_string P_ ((struct it *));
970 static void reseat P_ ((struct it *, struct text_pos, int));
971 static void reseat_1 P_ ((struct it *, struct text_pos, int));
972 static void back_to_previous_visible_line_start P_ ((struct it *));
973 void reseat_at_previous_visible_line_start P_ ((struct it *));
974 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
975 static int next_element_from_ellipsis P_ ((struct it *));
976 static int next_element_from_display_vector P_ ((struct it *));
977 static int next_element_from_string P_ ((struct it *));
978 static int next_element_from_c_string P_ ((struct it *));
979 static int next_element_from_buffer P_ ((struct it *));
980 static int next_element_from_composition P_ ((struct it *));
981 static int next_element_from_image P_ ((struct it *));
982 static int next_element_from_stretch P_ ((struct it *));
983 static void load_overlay_strings P_ ((struct it *, int));
984 static int init_from_display_pos P_ ((struct it *, struct window *,
985 struct display_pos *));
986 static void reseat_to_string P_ ((struct it *, unsigned char *,
987 Lisp_Object, int, int, int, int));
988 static enum move_it_result
989 move_it_in_display_line_to (struct it *, EMACS_INT, int,
990 enum move_operation_enum);
991 void move_it_vertically_backward P_ ((struct it *, int));
992 static void init_to_row_start P_ ((struct it *, struct window *,
993 struct glyph_row *));
994 static int init_to_row_end P_ ((struct it *, struct window *,
995 struct glyph_row *));
996 static void back_to_previous_line_start P_ ((struct it *));
997 static int forward_to_next_line_start P_ ((struct it *, int *));
998 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
999 Lisp_Object, int));
1000 static struct text_pos string_pos P_ ((int, Lisp_Object));
1001 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
1002 static int number_of_chars P_ ((unsigned char *, int));
1003 static void compute_stop_pos P_ ((struct it *));
1004 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1005 Lisp_Object));
1006 static int face_before_or_after_it_pos P_ ((struct it *, int));
1007 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1008 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1009 Lisp_Object, Lisp_Object,
1010 struct text_pos *, int));
1011 static int underlying_face_id P_ ((struct it *));
1012 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1013 struct window *));
1014
1015 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1016 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1017
1018 #ifdef HAVE_WINDOW_SYSTEM
1019
1020 static void update_tool_bar P_ ((struct frame *, int));
1021 static void build_desired_tool_bar_string P_ ((struct frame *f));
1022 static int redisplay_tool_bar P_ ((struct frame *));
1023 static void display_tool_bar_line P_ ((struct it *, int));
1024 static void notice_overwritten_cursor P_ ((struct window *,
1025 enum glyph_row_area,
1026 int, int, int, int));
1027
1028
1029
1030 #endif /* HAVE_WINDOW_SYSTEM */
1031
1032 \f
1033 /***********************************************************************
1034 Window display dimensions
1035 ***********************************************************************/
1036
1037 /* Return the bottom boundary y-position for text lines in window W.
1038 This is the first y position at which a line cannot start.
1039 It is relative to the top of the window.
1040
1041 This is the height of W minus the height of a mode line, if any. */
1042
1043 INLINE int
1044 window_text_bottom_y (w)
1045 struct window *w;
1046 {
1047 int height = WINDOW_TOTAL_HEIGHT (w);
1048
1049 if (WINDOW_WANTS_MODELINE_P (w))
1050 height -= CURRENT_MODE_LINE_HEIGHT (w);
1051 return height;
1052 }
1053
1054 /* Return the pixel width of display area AREA of window W. AREA < 0
1055 means return the total width of W, not including fringes to
1056 the left and right of the window. */
1057
1058 INLINE int
1059 window_box_width (w, area)
1060 struct window *w;
1061 int area;
1062 {
1063 int cols = XFASTINT (w->total_cols);
1064 int pixels = 0;
1065
1066 if (!w->pseudo_window_p)
1067 {
1068 cols -= WINDOW_SCROLL_BAR_COLS (w);
1069
1070 if (area == TEXT_AREA)
1071 {
1072 if (INTEGERP (w->left_margin_cols))
1073 cols -= XFASTINT (w->left_margin_cols);
1074 if (INTEGERP (w->right_margin_cols))
1075 cols -= XFASTINT (w->right_margin_cols);
1076 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1077 }
1078 else if (area == LEFT_MARGIN_AREA)
1079 {
1080 cols = (INTEGERP (w->left_margin_cols)
1081 ? XFASTINT (w->left_margin_cols) : 0);
1082 pixels = 0;
1083 }
1084 else if (area == RIGHT_MARGIN_AREA)
1085 {
1086 cols = (INTEGERP (w->right_margin_cols)
1087 ? XFASTINT (w->right_margin_cols) : 0);
1088 pixels = 0;
1089 }
1090 }
1091
1092 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1093 }
1094
1095
1096 /* Return the pixel height of the display area of window W, not
1097 including mode lines of W, if any. */
1098
1099 INLINE int
1100 window_box_height (w)
1101 struct window *w;
1102 {
1103 struct frame *f = XFRAME (w->frame);
1104 int height = WINDOW_TOTAL_HEIGHT (w);
1105
1106 xassert (height >= 0);
1107
1108 /* Note: the code below that determines the mode-line/header-line
1109 height is essentially the same as that contained in the macro
1110 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1111 the appropriate glyph row has its `mode_line_p' flag set,
1112 and if it doesn't, uses estimate_mode_line_height instead. */
1113
1114 if (WINDOW_WANTS_MODELINE_P (w))
1115 {
1116 struct glyph_row *ml_row
1117 = (w->current_matrix && w->current_matrix->rows
1118 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1119 : 0);
1120 if (ml_row && ml_row->mode_line_p)
1121 height -= ml_row->height;
1122 else
1123 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1124 }
1125
1126 if (WINDOW_WANTS_HEADER_LINE_P (w))
1127 {
1128 struct glyph_row *hl_row
1129 = (w->current_matrix && w->current_matrix->rows
1130 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1131 : 0);
1132 if (hl_row && hl_row->mode_line_p)
1133 height -= hl_row->height;
1134 else
1135 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1136 }
1137
1138 /* With a very small font and a mode-line that's taller than
1139 default, we might end up with a negative height. */
1140 return max (0, height);
1141 }
1142
1143 /* Return the window-relative coordinate of the left edge of display
1144 area AREA of window W. AREA < 0 means return the left edge of the
1145 whole window, to the right of the left fringe of W. */
1146
1147 INLINE int
1148 window_box_left_offset (w, area)
1149 struct window *w;
1150 int area;
1151 {
1152 int x;
1153
1154 if (w->pseudo_window_p)
1155 return 0;
1156
1157 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1158
1159 if (area == TEXT_AREA)
1160 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1161 + window_box_width (w, LEFT_MARGIN_AREA));
1162 else if (area == RIGHT_MARGIN_AREA)
1163 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1164 + window_box_width (w, LEFT_MARGIN_AREA)
1165 + window_box_width (w, TEXT_AREA)
1166 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1167 ? 0
1168 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1169 else if (area == LEFT_MARGIN_AREA
1170 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1171 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1172
1173 return x;
1174 }
1175
1176
1177 /* Return the window-relative coordinate of the right edge of display
1178 area AREA of window W. AREA < 0 means return the left edge of the
1179 whole window, to the left of the right fringe of W. */
1180
1181 INLINE int
1182 window_box_right_offset (w, area)
1183 struct window *w;
1184 int area;
1185 {
1186 return window_box_left_offset (w, area) + window_box_width (w, area);
1187 }
1188
1189 /* Return the frame-relative coordinate of the left edge of display
1190 area AREA of window W. AREA < 0 means return the left edge of the
1191 whole window, to the right of the left fringe of W. */
1192
1193 INLINE int
1194 window_box_left (w, area)
1195 struct window *w;
1196 int area;
1197 {
1198 struct frame *f = XFRAME (w->frame);
1199 int x;
1200
1201 if (w->pseudo_window_p)
1202 return FRAME_INTERNAL_BORDER_WIDTH (f);
1203
1204 x = (WINDOW_LEFT_EDGE_X (w)
1205 + window_box_left_offset (w, area));
1206
1207 return x;
1208 }
1209
1210
1211 /* Return the frame-relative coordinate of the right edge of display
1212 area AREA of window W. AREA < 0 means return the left edge of the
1213 whole window, to the left of the right fringe of W. */
1214
1215 INLINE int
1216 window_box_right (w, area)
1217 struct window *w;
1218 int area;
1219 {
1220 return window_box_left (w, area) + window_box_width (w, area);
1221 }
1222
1223 /* Get the bounding box of the display area AREA of window W, without
1224 mode lines, in frame-relative coordinates. AREA < 0 means the
1225 whole window, not including the left and right fringes of
1226 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1227 coordinates of the upper-left corner of the box. Return in
1228 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1229
1230 INLINE void
1231 window_box (w, area, box_x, box_y, box_width, box_height)
1232 struct window *w;
1233 int area;
1234 int *box_x, *box_y, *box_width, *box_height;
1235 {
1236 if (box_width)
1237 *box_width = window_box_width (w, area);
1238 if (box_height)
1239 *box_height = window_box_height (w);
1240 if (box_x)
1241 *box_x = window_box_left (w, area);
1242 if (box_y)
1243 {
1244 *box_y = WINDOW_TOP_EDGE_Y (w);
1245 if (WINDOW_WANTS_HEADER_LINE_P (w))
1246 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1247 }
1248 }
1249
1250
1251 /* Get the bounding box of the display area AREA of window W, without
1252 mode lines. AREA < 0 means the whole window, not including the
1253 left and right fringe of the window. Return in *TOP_LEFT_X
1254 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1255 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1256 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1257 box. */
1258
1259 INLINE void
1260 window_box_edges (w, area, top_left_x, top_left_y,
1261 bottom_right_x, bottom_right_y)
1262 struct window *w;
1263 int area;
1264 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1265 {
1266 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1267 bottom_right_y);
1268 *bottom_right_x += *top_left_x;
1269 *bottom_right_y += *top_left_y;
1270 }
1271
1272
1273 \f
1274 /***********************************************************************
1275 Utilities
1276 ***********************************************************************/
1277
1278 /* Return the bottom y-position of the line the iterator IT is in.
1279 This can modify IT's settings. */
1280
1281 int
1282 line_bottom_y (it)
1283 struct it *it;
1284 {
1285 int line_height = it->max_ascent + it->max_descent;
1286 int line_top_y = it->current_y;
1287
1288 if (line_height == 0)
1289 {
1290 if (last_height)
1291 line_height = last_height;
1292 else if (IT_CHARPOS (*it) < ZV)
1293 {
1294 move_it_by_lines (it, 1, 1);
1295 line_height = (it->max_ascent || it->max_descent
1296 ? it->max_ascent + it->max_descent
1297 : last_height);
1298 }
1299 else
1300 {
1301 struct glyph_row *row = it->glyph_row;
1302
1303 /* Use the default character height. */
1304 it->glyph_row = NULL;
1305 it->what = IT_CHARACTER;
1306 it->c = ' ';
1307 it->len = 1;
1308 PRODUCE_GLYPHS (it);
1309 line_height = it->ascent + it->descent;
1310 it->glyph_row = row;
1311 }
1312 }
1313
1314 return line_top_y + line_height;
1315 }
1316
1317
1318 /* Return 1 if position CHARPOS is visible in window W.
1319 CHARPOS < 0 means return info about WINDOW_END position.
1320 If visible, set *X and *Y to pixel coordinates of top left corner.
1321 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1322 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1323
1324 int
1325 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1326 struct window *w;
1327 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1328 {
1329 struct it it;
1330 struct text_pos top;
1331 int visible_p = 0;
1332 struct buffer *old_buffer = NULL;
1333
1334 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1335 return visible_p;
1336
1337 if (XBUFFER (w->buffer) != current_buffer)
1338 {
1339 old_buffer = current_buffer;
1340 set_buffer_internal_1 (XBUFFER (w->buffer));
1341 }
1342
1343 SET_TEXT_POS_FROM_MARKER (top, w->start);
1344
1345 /* Compute exact mode line heights. */
1346 if (WINDOW_WANTS_MODELINE_P (w))
1347 current_mode_line_height
1348 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1349 current_buffer->mode_line_format);
1350
1351 if (WINDOW_WANTS_HEADER_LINE_P (w))
1352 current_header_line_height
1353 = display_mode_line (w, HEADER_LINE_FACE_ID,
1354 current_buffer->header_line_format);
1355
1356 start_display (&it, w, top);
1357 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1358 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1359
1360 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1361 {
1362 /* We have reached CHARPOS, or passed it. How the call to
1363 move_it_to can overshoot: (i) If CHARPOS is on invisible
1364 text, move_it_to stops at the end of the invisible text,
1365 after CHARPOS. (ii) If CHARPOS is in a display vector,
1366 move_it_to stops on its last glyph. */
1367 int top_x = it.current_x;
1368 int top_y = it.current_y;
1369 enum it_method it_method = it.method;
1370 /* Calling line_bottom_y may change it.method, it.position, etc. */
1371 int bottom_y = (last_height = 0, line_bottom_y (&it));
1372 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1373
1374 if (top_y < window_top_y)
1375 visible_p = bottom_y > window_top_y;
1376 else if (top_y < it.last_visible_y)
1377 visible_p = 1;
1378 if (visible_p)
1379 {
1380 if (it_method == GET_FROM_BUFFER)
1381 {
1382 Lisp_Object window, prop;
1383
1384 XSETWINDOW (window, w);
1385 prop = Fget_char_property (make_number (charpos),
1386 Qinvisible, window);
1387
1388 /* If charpos coincides with invisible text covered with an
1389 ellipsis, use the first glyph of the ellipsis to compute
1390 the pixel positions. */
1391 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1392 {
1393 struct glyph_row *row = it.glyph_row;
1394 struct glyph *glyph = row->glyphs[TEXT_AREA];
1395 struct glyph *end = glyph + row->used[TEXT_AREA];
1396 int x = row->x;
1397
1398 for (; glyph < end
1399 && (!BUFFERP (glyph->object)
1400 || glyph->charpos < charpos);
1401 glyph++)
1402 x += glyph->pixel_width;
1403 top_x = x;
1404 }
1405 }
1406 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1407 {
1408 /* We stopped on the last glyph of a display vector.
1409 Try and recompute. Hack alert! */
1410 if (charpos < 2 || top.charpos >= charpos)
1411 top_x = it.glyph_row->x;
1412 else
1413 {
1414 struct it it2;
1415 start_display (&it2, w, top);
1416 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1417 get_next_display_element (&it2);
1418 PRODUCE_GLYPHS (&it2);
1419 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1420 || it2.current_x > it2.last_visible_x)
1421 top_x = it.glyph_row->x;
1422 else
1423 {
1424 top_x = it2.current_x;
1425 top_y = it2.current_y;
1426 }
1427 }
1428 }
1429
1430 *x = top_x;
1431 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1432 *rtop = max (0, window_top_y - top_y);
1433 *rbot = max (0, bottom_y - it.last_visible_y);
1434 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1435 - max (top_y, window_top_y)));
1436 *vpos = it.vpos;
1437 }
1438 }
1439 else
1440 {
1441 struct it it2;
1442
1443 it2 = it;
1444 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1445 move_it_by_lines (&it, 1, 0);
1446 if (charpos < IT_CHARPOS (it)
1447 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1448 {
1449 visible_p = 1;
1450 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1451 *x = it2.current_x;
1452 *y = it2.current_y + it2.max_ascent - it2.ascent;
1453 *rtop = max (0, -it2.current_y);
1454 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1455 - it.last_visible_y));
1456 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1457 it.last_visible_y)
1458 - max (it2.current_y,
1459 WINDOW_HEADER_LINE_HEIGHT (w))));
1460 *vpos = it2.vpos;
1461 }
1462 }
1463
1464 if (old_buffer)
1465 set_buffer_internal_1 (old_buffer);
1466
1467 current_header_line_height = current_mode_line_height = -1;
1468
1469 if (visible_p && XFASTINT (w->hscroll) > 0)
1470 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1471
1472 #if 0
1473 /* Debugging code. */
1474 if (visible_p)
1475 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1476 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1477 else
1478 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1479 #endif
1480
1481 return visible_p;
1482 }
1483
1484
1485 /* Return the next character from STR which is MAXLEN bytes long.
1486 Return in *LEN the length of the character. This is like
1487 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1488 we find one, we return a `?', but with the length of the invalid
1489 character. */
1490
1491 static INLINE int
1492 string_char_and_length (str, len)
1493 const unsigned char *str;
1494 int *len;
1495 {
1496 int c;
1497
1498 c = STRING_CHAR_AND_LENGTH (str, *len);
1499 if (!CHAR_VALID_P (c, 1))
1500 /* We may not change the length here because other places in Emacs
1501 don't use this function, i.e. they silently accept invalid
1502 characters. */
1503 c = '?';
1504
1505 return c;
1506 }
1507
1508
1509
1510 /* Given a position POS containing a valid character and byte position
1511 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1512
1513 static struct text_pos
1514 string_pos_nchars_ahead (pos, string, nchars)
1515 struct text_pos pos;
1516 Lisp_Object string;
1517 int nchars;
1518 {
1519 xassert (STRINGP (string) && nchars >= 0);
1520
1521 if (STRING_MULTIBYTE (string))
1522 {
1523 int rest = SBYTES (string) - BYTEPOS (pos);
1524 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1525 int len;
1526
1527 while (nchars--)
1528 {
1529 string_char_and_length (p, &len);
1530 p += len, rest -= len;
1531 xassert (rest >= 0);
1532 CHARPOS (pos) += 1;
1533 BYTEPOS (pos) += len;
1534 }
1535 }
1536 else
1537 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1538
1539 return pos;
1540 }
1541
1542
1543 /* Value is the text position, i.e. character and byte position,
1544 for character position CHARPOS in STRING. */
1545
1546 static INLINE struct text_pos
1547 string_pos (charpos, string)
1548 int charpos;
1549 Lisp_Object string;
1550 {
1551 struct text_pos pos;
1552 xassert (STRINGP (string));
1553 xassert (charpos >= 0);
1554 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1555 return pos;
1556 }
1557
1558
1559 /* Value is a text position, i.e. character and byte position, for
1560 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1561 means recognize multibyte characters. */
1562
1563 static struct text_pos
1564 c_string_pos (charpos, s, multibyte_p)
1565 int charpos;
1566 unsigned char *s;
1567 int multibyte_p;
1568 {
1569 struct text_pos pos;
1570
1571 xassert (s != NULL);
1572 xassert (charpos >= 0);
1573
1574 if (multibyte_p)
1575 {
1576 int rest = strlen (s), len;
1577
1578 SET_TEXT_POS (pos, 0, 0);
1579 while (charpos--)
1580 {
1581 string_char_and_length (s, &len);
1582 s += len, rest -= len;
1583 xassert (rest >= 0);
1584 CHARPOS (pos) += 1;
1585 BYTEPOS (pos) += len;
1586 }
1587 }
1588 else
1589 SET_TEXT_POS (pos, charpos, charpos);
1590
1591 return pos;
1592 }
1593
1594
1595 /* Value is the number of characters in C string S. MULTIBYTE_P
1596 non-zero means recognize multibyte characters. */
1597
1598 static int
1599 number_of_chars (s, multibyte_p)
1600 unsigned char *s;
1601 int multibyte_p;
1602 {
1603 int nchars;
1604
1605 if (multibyte_p)
1606 {
1607 int rest = strlen (s), len;
1608 unsigned char *p = (unsigned char *) s;
1609
1610 for (nchars = 0; rest > 0; ++nchars)
1611 {
1612 string_char_and_length (p, &len);
1613 rest -= len, p += len;
1614 }
1615 }
1616 else
1617 nchars = strlen (s);
1618
1619 return nchars;
1620 }
1621
1622
1623 /* Compute byte position NEWPOS->bytepos corresponding to
1624 NEWPOS->charpos. POS is a known position in string STRING.
1625 NEWPOS->charpos must be >= POS.charpos. */
1626
1627 static void
1628 compute_string_pos (newpos, pos, string)
1629 struct text_pos *newpos, pos;
1630 Lisp_Object string;
1631 {
1632 xassert (STRINGP (string));
1633 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1634
1635 if (STRING_MULTIBYTE (string))
1636 *newpos = string_pos_nchars_ahead (pos, string,
1637 CHARPOS (*newpos) - CHARPOS (pos));
1638 else
1639 BYTEPOS (*newpos) = CHARPOS (*newpos);
1640 }
1641
1642 /* EXPORT:
1643 Return an estimation of the pixel height of mode or header lines on
1644 frame F. FACE_ID specifies what line's height to estimate. */
1645
1646 int
1647 estimate_mode_line_height (f, face_id)
1648 struct frame *f;
1649 enum face_id face_id;
1650 {
1651 #ifdef HAVE_WINDOW_SYSTEM
1652 if (FRAME_WINDOW_P (f))
1653 {
1654 int height = FONT_HEIGHT (FRAME_FONT (f));
1655
1656 /* This function is called so early when Emacs starts that the face
1657 cache and mode line face are not yet initialized. */
1658 if (FRAME_FACE_CACHE (f))
1659 {
1660 struct face *face = FACE_FROM_ID (f, face_id);
1661 if (face)
1662 {
1663 if (face->font)
1664 height = FONT_HEIGHT (face->font);
1665 if (face->box_line_width > 0)
1666 height += 2 * face->box_line_width;
1667 }
1668 }
1669
1670 return height;
1671 }
1672 #endif
1673
1674 return 1;
1675 }
1676
1677 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1678 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1679 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1680 not force the value into range. */
1681
1682 void
1683 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1684 FRAME_PTR f;
1685 register int pix_x, pix_y;
1686 int *x, *y;
1687 NativeRectangle *bounds;
1688 int noclip;
1689 {
1690
1691 #ifdef HAVE_WINDOW_SYSTEM
1692 if (FRAME_WINDOW_P (f))
1693 {
1694 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1695 even for negative values. */
1696 if (pix_x < 0)
1697 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1698 if (pix_y < 0)
1699 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1700
1701 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1702 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1703
1704 if (bounds)
1705 STORE_NATIVE_RECT (*bounds,
1706 FRAME_COL_TO_PIXEL_X (f, pix_x),
1707 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1708 FRAME_COLUMN_WIDTH (f) - 1,
1709 FRAME_LINE_HEIGHT (f) - 1);
1710
1711 if (!noclip)
1712 {
1713 if (pix_x < 0)
1714 pix_x = 0;
1715 else if (pix_x > FRAME_TOTAL_COLS (f))
1716 pix_x = FRAME_TOTAL_COLS (f);
1717
1718 if (pix_y < 0)
1719 pix_y = 0;
1720 else if (pix_y > FRAME_LINES (f))
1721 pix_y = FRAME_LINES (f);
1722 }
1723 }
1724 #endif
1725
1726 *x = pix_x;
1727 *y = pix_y;
1728 }
1729
1730
1731 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1732 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1733 can't tell the positions because W's display is not up to date,
1734 return 0. */
1735
1736 int
1737 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1738 struct window *w;
1739 int hpos, vpos;
1740 int *frame_x, *frame_y;
1741 {
1742 #ifdef HAVE_WINDOW_SYSTEM
1743 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1744 {
1745 int success_p;
1746
1747 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1748 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1749
1750 if (display_completed)
1751 {
1752 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1753 struct glyph *glyph = row->glyphs[TEXT_AREA];
1754 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1755
1756 hpos = row->x;
1757 vpos = row->y;
1758 while (glyph < end)
1759 {
1760 hpos += glyph->pixel_width;
1761 ++glyph;
1762 }
1763
1764 /* If first glyph is partially visible, its first visible position is still 0. */
1765 if (hpos < 0)
1766 hpos = 0;
1767
1768 success_p = 1;
1769 }
1770 else
1771 {
1772 hpos = vpos = 0;
1773 success_p = 0;
1774 }
1775
1776 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1777 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1778 return success_p;
1779 }
1780 #endif
1781
1782 *frame_x = hpos;
1783 *frame_y = vpos;
1784 return 1;
1785 }
1786
1787
1788 #ifdef HAVE_WINDOW_SYSTEM
1789
1790 /* Find the glyph under window-relative coordinates X/Y in window W.
1791 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1792 strings. Return in *HPOS and *VPOS the row and column number of
1793 the glyph found. Return in *AREA the glyph area containing X.
1794 Value is a pointer to the glyph found or null if X/Y is not on
1795 text, or we can't tell because W's current matrix is not up to
1796 date. */
1797
1798 static
1799 struct glyph *
1800 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1801 struct window *w;
1802 int x, y;
1803 int *hpos, *vpos, *dx, *dy, *area;
1804 {
1805 struct glyph *glyph, *end;
1806 struct glyph_row *row = NULL;
1807 int x0, i;
1808
1809 /* Find row containing Y. Give up if some row is not enabled. */
1810 for (i = 0; i < w->current_matrix->nrows; ++i)
1811 {
1812 row = MATRIX_ROW (w->current_matrix, i);
1813 if (!row->enabled_p)
1814 return NULL;
1815 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1816 break;
1817 }
1818
1819 *vpos = i;
1820 *hpos = 0;
1821
1822 /* Give up if Y is not in the window. */
1823 if (i == w->current_matrix->nrows)
1824 return NULL;
1825
1826 /* Get the glyph area containing X. */
1827 if (w->pseudo_window_p)
1828 {
1829 *area = TEXT_AREA;
1830 x0 = 0;
1831 }
1832 else
1833 {
1834 if (x < window_box_left_offset (w, TEXT_AREA))
1835 {
1836 *area = LEFT_MARGIN_AREA;
1837 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1838 }
1839 else if (x < window_box_right_offset (w, TEXT_AREA))
1840 {
1841 *area = TEXT_AREA;
1842 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1843 }
1844 else
1845 {
1846 *area = RIGHT_MARGIN_AREA;
1847 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1848 }
1849 }
1850
1851 /* Find glyph containing X. */
1852 glyph = row->glyphs[*area];
1853 end = glyph + row->used[*area];
1854 x -= x0;
1855 while (glyph < end && x >= glyph->pixel_width)
1856 {
1857 x -= glyph->pixel_width;
1858 ++glyph;
1859 }
1860
1861 if (glyph == end)
1862 return NULL;
1863
1864 if (dx)
1865 {
1866 *dx = x;
1867 *dy = y - (row->y + row->ascent - glyph->ascent);
1868 }
1869
1870 *hpos = glyph - row->glyphs[*area];
1871 return glyph;
1872 }
1873
1874
1875 /* EXPORT:
1876 Convert frame-relative x/y to coordinates relative to window W.
1877 Takes pseudo-windows into account. */
1878
1879 void
1880 frame_to_window_pixel_xy (w, x, y)
1881 struct window *w;
1882 int *x, *y;
1883 {
1884 if (w->pseudo_window_p)
1885 {
1886 /* A pseudo-window is always full-width, and starts at the
1887 left edge of the frame, plus a frame border. */
1888 struct frame *f = XFRAME (w->frame);
1889 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1890 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1891 }
1892 else
1893 {
1894 *x -= WINDOW_LEFT_EDGE_X (w);
1895 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1896 }
1897 }
1898
1899 /* EXPORT:
1900 Return in RECTS[] at most N clipping rectangles for glyph string S.
1901 Return the number of stored rectangles. */
1902
1903 int
1904 get_glyph_string_clip_rects (s, rects, n)
1905 struct glyph_string *s;
1906 NativeRectangle *rects;
1907 int n;
1908 {
1909 XRectangle r;
1910
1911 if (n <= 0)
1912 return 0;
1913
1914 if (s->row->full_width_p)
1915 {
1916 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1917 r.x = WINDOW_LEFT_EDGE_X (s->w);
1918 r.width = WINDOW_TOTAL_WIDTH (s->w);
1919
1920 /* Unless displaying a mode or menu bar line, which are always
1921 fully visible, clip to the visible part of the row. */
1922 if (s->w->pseudo_window_p)
1923 r.height = s->row->visible_height;
1924 else
1925 r.height = s->height;
1926 }
1927 else
1928 {
1929 /* This is a text line that may be partially visible. */
1930 r.x = window_box_left (s->w, s->area);
1931 r.width = window_box_width (s->w, s->area);
1932 r.height = s->row->visible_height;
1933 }
1934
1935 if (s->clip_head)
1936 if (r.x < s->clip_head->x)
1937 {
1938 if (r.width >= s->clip_head->x - r.x)
1939 r.width -= s->clip_head->x - r.x;
1940 else
1941 r.width = 0;
1942 r.x = s->clip_head->x;
1943 }
1944 if (s->clip_tail)
1945 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1946 {
1947 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1948 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1949 else
1950 r.width = 0;
1951 }
1952
1953 /* If S draws overlapping rows, it's sufficient to use the top and
1954 bottom of the window for clipping because this glyph string
1955 intentionally draws over other lines. */
1956 if (s->for_overlaps)
1957 {
1958 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1959 r.height = window_text_bottom_y (s->w) - r.y;
1960
1961 /* Alas, the above simple strategy does not work for the
1962 environments with anti-aliased text: if the same text is
1963 drawn onto the same place multiple times, it gets thicker.
1964 If the overlap we are processing is for the erased cursor, we
1965 take the intersection with the rectagle of the cursor. */
1966 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1967 {
1968 XRectangle rc, r_save = r;
1969
1970 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1971 rc.y = s->w->phys_cursor.y;
1972 rc.width = s->w->phys_cursor_width;
1973 rc.height = s->w->phys_cursor_height;
1974
1975 x_intersect_rectangles (&r_save, &rc, &r);
1976 }
1977 }
1978 else
1979 {
1980 /* Don't use S->y for clipping because it doesn't take partially
1981 visible lines into account. For example, it can be negative for
1982 partially visible lines at the top of a window. */
1983 if (!s->row->full_width_p
1984 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1985 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1986 else
1987 r.y = max (0, s->row->y);
1988 }
1989
1990 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1991
1992 /* If drawing the cursor, don't let glyph draw outside its
1993 advertised boundaries. Cleartype does this under some circumstances. */
1994 if (s->hl == DRAW_CURSOR)
1995 {
1996 struct glyph *glyph = s->first_glyph;
1997 int height, max_y;
1998
1999 if (s->x > r.x)
2000 {
2001 r.width -= s->x - r.x;
2002 r.x = s->x;
2003 }
2004 r.width = min (r.width, glyph->pixel_width);
2005
2006 /* If r.y is below window bottom, ensure that we still see a cursor. */
2007 height = min (glyph->ascent + glyph->descent,
2008 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2009 max_y = window_text_bottom_y (s->w) - height;
2010 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2011 if (s->ybase - glyph->ascent > max_y)
2012 {
2013 r.y = max_y;
2014 r.height = height;
2015 }
2016 else
2017 {
2018 /* Don't draw cursor glyph taller than our actual glyph. */
2019 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2020 if (height < r.height)
2021 {
2022 max_y = r.y + r.height;
2023 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2024 r.height = min (max_y - r.y, height);
2025 }
2026 }
2027 }
2028
2029 if (s->row->clip)
2030 {
2031 XRectangle r_save = r;
2032
2033 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2034 r.width = 0;
2035 }
2036
2037 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2038 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2039 {
2040 #ifdef CONVERT_FROM_XRECT
2041 CONVERT_FROM_XRECT (r, *rects);
2042 #else
2043 *rects = r;
2044 #endif
2045 return 1;
2046 }
2047 else
2048 {
2049 /* If we are processing overlapping and allowed to return
2050 multiple clipping rectangles, we exclude the row of the glyph
2051 string from the clipping rectangle. This is to avoid drawing
2052 the same text on the environment with anti-aliasing. */
2053 #ifdef CONVERT_FROM_XRECT
2054 XRectangle rs[2];
2055 #else
2056 XRectangle *rs = rects;
2057 #endif
2058 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2059
2060 if (s->for_overlaps & OVERLAPS_PRED)
2061 {
2062 rs[i] = r;
2063 if (r.y + r.height > row_y)
2064 {
2065 if (r.y < row_y)
2066 rs[i].height = row_y - r.y;
2067 else
2068 rs[i].height = 0;
2069 }
2070 i++;
2071 }
2072 if (s->for_overlaps & OVERLAPS_SUCC)
2073 {
2074 rs[i] = r;
2075 if (r.y < row_y + s->row->visible_height)
2076 {
2077 if (r.y + r.height > row_y + s->row->visible_height)
2078 {
2079 rs[i].y = row_y + s->row->visible_height;
2080 rs[i].height = r.y + r.height - rs[i].y;
2081 }
2082 else
2083 rs[i].height = 0;
2084 }
2085 i++;
2086 }
2087
2088 n = i;
2089 #ifdef CONVERT_FROM_XRECT
2090 for (i = 0; i < n; i++)
2091 CONVERT_FROM_XRECT (rs[i], rects[i]);
2092 #endif
2093 return n;
2094 }
2095 }
2096
2097 /* EXPORT:
2098 Return in *NR the clipping rectangle for glyph string S. */
2099
2100 void
2101 get_glyph_string_clip_rect (s, nr)
2102 struct glyph_string *s;
2103 NativeRectangle *nr;
2104 {
2105 get_glyph_string_clip_rects (s, nr, 1);
2106 }
2107
2108
2109 /* EXPORT:
2110 Return the position and height of the phys cursor in window W.
2111 Set w->phys_cursor_width to width of phys cursor.
2112 */
2113
2114 void
2115 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2116 struct window *w;
2117 struct glyph_row *row;
2118 struct glyph *glyph;
2119 int *xp, *yp, *heightp;
2120 {
2121 struct frame *f = XFRAME (WINDOW_FRAME (w));
2122 int x, y, wd, h, h0, y0;
2123
2124 /* Compute the width of the rectangle to draw. If on a stretch
2125 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2126 rectangle as wide as the glyph, but use a canonical character
2127 width instead. */
2128 wd = glyph->pixel_width - 1;
2129 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2130 wd++; /* Why? */
2131 #endif
2132
2133 x = w->phys_cursor.x;
2134 if (x < 0)
2135 {
2136 wd += x;
2137 x = 0;
2138 }
2139
2140 if (glyph->type == STRETCH_GLYPH
2141 && !x_stretch_cursor_p)
2142 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2143 w->phys_cursor_width = wd;
2144
2145 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2146
2147 /* If y is below window bottom, ensure that we still see a cursor. */
2148 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2149
2150 h = max (h0, glyph->ascent + glyph->descent);
2151 h0 = min (h0, glyph->ascent + glyph->descent);
2152
2153 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2154 if (y < y0)
2155 {
2156 h = max (h - (y0 - y) + 1, h0);
2157 y = y0 - 1;
2158 }
2159 else
2160 {
2161 y0 = window_text_bottom_y (w) - h0;
2162 if (y > y0)
2163 {
2164 h += y - y0;
2165 y = y0;
2166 }
2167 }
2168
2169 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2170 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2171 *heightp = h;
2172 }
2173
2174 /*
2175 * Remember which glyph the mouse is over.
2176 */
2177
2178 void
2179 remember_mouse_glyph (f, gx, gy, rect)
2180 struct frame *f;
2181 int gx, gy;
2182 NativeRectangle *rect;
2183 {
2184 Lisp_Object window;
2185 struct window *w;
2186 struct glyph_row *r, *gr, *end_row;
2187 enum window_part part;
2188 enum glyph_row_area area;
2189 int x, y, width, height;
2190
2191 /* Try to determine frame pixel position and size of the glyph under
2192 frame pixel coordinates X/Y on frame F. */
2193
2194 if (!f->glyphs_initialized_p
2195 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2196 NILP (window)))
2197 {
2198 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2199 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2200 goto virtual_glyph;
2201 }
2202
2203 w = XWINDOW (window);
2204 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2205 height = WINDOW_FRAME_LINE_HEIGHT (w);
2206
2207 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2208 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2209
2210 if (w->pseudo_window_p)
2211 {
2212 area = TEXT_AREA;
2213 part = ON_MODE_LINE; /* Don't adjust margin. */
2214 goto text_glyph;
2215 }
2216
2217 switch (part)
2218 {
2219 case ON_LEFT_MARGIN:
2220 area = LEFT_MARGIN_AREA;
2221 goto text_glyph;
2222
2223 case ON_RIGHT_MARGIN:
2224 area = RIGHT_MARGIN_AREA;
2225 goto text_glyph;
2226
2227 case ON_HEADER_LINE:
2228 case ON_MODE_LINE:
2229 gr = (part == ON_HEADER_LINE
2230 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2231 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2232 gy = gr->y;
2233 area = TEXT_AREA;
2234 goto text_glyph_row_found;
2235
2236 case ON_TEXT:
2237 area = TEXT_AREA;
2238
2239 text_glyph:
2240 gr = 0; gy = 0;
2241 for (; r <= end_row && r->enabled_p; ++r)
2242 if (r->y + r->height > y)
2243 {
2244 gr = r; gy = r->y;
2245 break;
2246 }
2247
2248 text_glyph_row_found:
2249 if (gr && gy <= y)
2250 {
2251 struct glyph *g = gr->glyphs[area];
2252 struct glyph *end = g + gr->used[area];
2253
2254 height = gr->height;
2255 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2256 if (gx + g->pixel_width > x)
2257 break;
2258
2259 if (g < end)
2260 {
2261 if (g->type == IMAGE_GLYPH)
2262 {
2263 /* Don't remember when mouse is over image, as
2264 image may have hot-spots. */
2265 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2266 return;
2267 }
2268 width = g->pixel_width;
2269 }
2270 else
2271 {
2272 /* Use nominal char spacing at end of line. */
2273 x -= gx;
2274 gx += (x / width) * width;
2275 }
2276
2277 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2278 gx += window_box_left_offset (w, area);
2279 }
2280 else
2281 {
2282 /* Use nominal line height at end of window. */
2283 gx = (x / width) * width;
2284 y -= gy;
2285 gy += (y / height) * height;
2286 }
2287 break;
2288
2289 case ON_LEFT_FRINGE:
2290 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2291 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2292 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2293 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2294 goto row_glyph;
2295
2296 case ON_RIGHT_FRINGE:
2297 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2298 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2299 : window_box_right_offset (w, TEXT_AREA));
2300 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2301 goto row_glyph;
2302
2303 case ON_SCROLL_BAR:
2304 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2305 ? 0
2306 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2307 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2308 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2309 : 0)));
2310 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2311
2312 row_glyph:
2313 gr = 0, gy = 0;
2314 for (; r <= end_row && r->enabled_p; ++r)
2315 if (r->y + r->height > y)
2316 {
2317 gr = r; gy = r->y;
2318 break;
2319 }
2320
2321 if (gr && gy <= y)
2322 height = gr->height;
2323 else
2324 {
2325 /* Use nominal line height at end of window. */
2326 y -= gy;
2327 gy += (y / height) * height;
2328 }
2329 break;
2330
2331 default:
2332 ;
2333 virtual_glyph:
2334 /* If there is no glyph under the mouse, then we divide the screen
2335 into a grid of the smallest glyph in the frame, and use that
2336 as our "glyph". */
2337
2338 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2339 round down even for negative values. */
2340 if (gx < 0)
2341 gx -= width - 1;
2342 if (gy < 0)
2343 gy -= height - 1;
2344
2345 gx = (gx / width) * width;
2346 gy = (gy / height) * height;
2347
2348 goto store_rect;
2349 }
2350
2351 gx += WINDOW_LEFT_EDGE_X (w);
2352 gy += WINDOW_TOP_EDGE_Y (w);
2353
2354 store_rect:
2355 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2356
2357 /* Visible feedback for debugging. */
2358 #if 0
2359 #if HAVE_X_WINDOWS
2360 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2361 f->output_data.x->normal_gc,
2362 gx, gy, width, height);
2363 #endif
2364 #endif
2365 }
2366
2367
2368 #endif /* HAVE_WINDOW_SYSTEM */
2369
2370 \f
2371 /***********************************************************************
2372 Lisp form evaluation
2373 ***********************************************************************/
2374
2375 /* Error handler for safe_eval and safe_call. */
2376
2377 static Lisp_Object
2378 safe_eval_handler (arg)
2379 Lisp_Object arg;
2380 {
2381 add_to_log ("Error during redisplay: %s", arg, Qnil);
2382 return Qnil;
2383 }
2384
2385
2386 /* Evaluate SEXPR and return the result, or nil if something went
2387 wrong. Prevent redisplay during the evaluation. */
2388
2389 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2390 Return the result, or nil if something went wrong. Prevent
2391 redisplay during the evaluation. */
2392
2393 Lisp_Object
2394 safe_call (nargs, args)
2395 int nargs;
2396 Lisp_Object *args;
2397 {
2398 Lisp_Object val;
2399
2400 if (inhibit_eval_during_redisplay)
2401 val = Qnil;
2402 else
2403 {
2404 int count = SPECPDL_INDEX ();
2405 struct gcpro gcpro1;
2406
2407 GCPRO1 (args[0]);
2408 gcpro1.nvars = nargs;
2409 specbind (Qinhibit_redisplay, Qt);
2410 /* Use Qt to ensure debugger does not run,
2411 so there is no possibility of wanting to redisplay. */
2412 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2413 safe_eval_handler);
2414 UNGCPRO;
2415 val = unbind_to (count, val);
2416 }
2417
2418 return val;
2419 }
2420
2421
2422 /* Call function FN with one argument ARG.
2423 Return the result, or nil if something went wrong. */
2424
2425 Lisp_Object
2426 safe_call1 (fn, arg)
2427 Lisp_Object fn, arg;
2428 {
2429 Lisp_Object args[2];
2430 args[0] = fn;
2431 args[1] = arg;
2432 return safe_call (2, args);
2433 }
2434
2435 static Lisp_Object Qeval;
2436
2437 Lisp_Object
2438 safe_eval (Lisp_Object sexpr)
2439 {
2440 return safe_call1 (Qeval, sexpr);
2441 }
2442
2443 /* Call function FN with one argument ARG.
2444 Return the result, or nil if something went wrong. */
2445
2446 Lisp_Object
2447 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2448 {
2449 Lisp_Object args[3];
2450 args[0] = fn;
2451 args[1] = arg1;
2452 args[2] = arg2;
2453 return safe_call (3, args);
2454 }
2455
2456
2457 \f
2458 /***********************************************************************
2459 Debugging
2460 ***********************************************************************/
2461
2462 #if 0
2463
2464 /* Define CHECK_IT to perform sanity checks on iterators.
2465 This is for debugging. It is too slow to do unconditionally. */
2466
2467 static void
2468 check_it (it)
2469 struct it *it;
2470 {
2471 if (it->method == GET_FROM_STRING)
2472 {
2473 xassert (STRINGP (it->string));
2474 xassert (IT_STRING_CHARPOS (*it) >= 0);
2475 }
2476 else
2477 {
2478 xassert (IT_STRING_CHARPOS (*it) < 0);
2479 if (it->method == GET_FROM_BUFFER)
2480 {
2481 /* Check that character and byte positions agree. */
2482 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2483 }
2484 }
2485
2486 if (it->dpvec)
2487 xassert (it->current.dpvec_index >= 0);
2488 else
2489 xassert (it->current.dpvec_index < 0);
2490 }
2491
2492 #define CHECK_IT(IT) check_it ((IT))
2493
2494 #else /* not 0 */
2495
2496 #define CHECK_IT(IT) (void) 0
2497
2498 #endif /* not 0 */
2499
2500
2501 #if GLYPH_DEBUG
2502
2503 /* Check that the window end of window W is what we expect it
2504 to be---the last row in the current matrix displaying text. */
2505
2506 static void
2507 check_window_end (w)
2508 struct window *w;
2509 {
2510 if (!MINI_WINDOW_P (w)
2511 && !NILP (w->window_end_valid))
2512 {
2513 struct glyph_row *row;
2514 xassert ((row = MATRIX_ROW (w->current_matrix,
2515 XFASTINT (w->window_end_vpos)),
2516 !row->enabled_p
2517 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2518 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2519 }
2520 }
2521
2522 #define CHECK_WINDOW_END(W) check_window_end ((W))
2523
2524 #else /* not GLYPH_DEBUG */
2525
2526 #define CHECK_WINDOW_END(W) (void) 0
2527
2528 #endif /* not GLYPH_DEBUG */
2529
2530
2531 \f
2532 /***********************************************************************
2533 Iterator initialization
2534 ***********************************************************************/
2535
2536 /* Initialize IT for displaying current_buffer in window W, starting
2537 at character position CHARPOS. CHARPOS < 0 means that no buffer
2538 position is specified which is useful when the iterator is assigned
2539 a position later. BYTEPOS is the byte position corresponding to
2540 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2541
2542 If ROW is not null, calls to produce_glyphs with IT as parameter
2543 will produce glyphs in that row.
2544
2545 BASE_FACE_ID is the id of a base face to use. It must be one of
2546 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2547 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2548 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2549
2550 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2551 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2552 will be initialized to use the corresponding mode line glyph row of
2553 the desired matrix of W. */
2554
2555 void
2556 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2557 struct it *it;
2558 struct window *w;
2559 int charpos, bytepos;
2560 struct glyph_row *row;
2561 enum face_id base_face_id;
2562 {
2563 int highlight_region_p;
2564 enum face_id remapped_base_face_id = base_face_id;
2565
2566 /* Some precondition checks. */
2567 xassert (w != NULL && it != NULL);
2568 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2569 && charpos <= ZV));
2570
2571 /* If face attributes have been changed since the last redisplay,
2572 free realized faces now because they depend on face definitions
2573 that might have changed. Don't free faces while there might be
2574 desired matrices pending which reference these faces. */
2575 if (face_change_count && !inhibit_free_realized_faces)
2576 {
2577 face_change_count = 0;
2578 free_all_realized_faces (Qnil);
2579 }
2580
2581 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2582 if (! NILP (Vface_remapping_alist))
2583 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2584
2585 /* Use one of the mode line rows of W's desired matrix if
2586 appropriate. */
2587 if (row == NULL)
2588 {
2589 if (base_face_id == MODE_LINE_FACE_ID
2590 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2591 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2592 else if (base_face_id == HEADER_LINE_FACE_ID)
2593 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2594 }
2595
2596 /* Clear IT. */
2597 bzero (it, sizeof *it);
2598 it->current.overlay_string_index = -1;
2599 it->current.dpvec_index = -1;
2600 it->base_face_id = remapped_base_face_id;
2601 it->string = Qnil;
2602 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2603
2604 /* The window in which we iterate over current_buffer: */
2605 XSETWINDOW (it->window, w);
2606 it->w = w;
2607 it->f = XFRAME (w->frame);
2608
2609 it->cmp_it.id = -1;
2610
2611 /* Extra space between lines (on window systems only). */
2612 if (base_face_id == DEFAULT_FACE_ID
2613 && FRAME_WINDOW_P (it->f))
2614 {
2615 if (NATNUMP (current_buffer->extra_line_spacing))
2616 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2617 else if (FLOATP (current_buffer->extra_line_spacing))
2618 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2619 * FRAME_LINE_HEIGHT (it->f));
2620 else if (it->f->extra_line_spacing > 0)
2621 it->extra_line_spacing = it->f->extra_line_spacing;
2622 it->max_extra_line_spacing = 0;
2623 }
2624
2625 /* If realized faces have been removed, e.g. because of face
2626 attribute changes of named faces, recompute them. When running
2627 in batch mode, the face cache of the initial frame is null. If
2628 we happen to get called, make a dummy face cache. */
2629 if (FRAME_FACE_CACHE (it->f) == NULL)
2630 init_frame_faces (it->f);
2631 if (FRAME_FACE_CACHE (it->f)->used == 0)
2632 recompute_basic_faces (it->f);
2633
2634 /* Current value of the `slice', `space-width', and 'height' properties. */
2635 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2636 it->space_width = Qnil;
2637 it->font_height = Qnil;
2638 it->override_ascent = -1;
2639
2640 /* Are control characters displayed as `^C'? */
2641 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2642
2643 /* -1 means everything between a CR and the following line end
2644 is invisible. >0 means lines indented more than this value are
2645 invisible. */
2646 it->selective = (INTEGERP (current_buffer->selective_display)
2647 ? XFASTINT (current_buffer->selective_display)
2648 : (!NILP (current_buffer->selective_display)
2649 ? -1 : 0));
2650 it->selective_display_ellipsis_p
2651 = !NILP (current_buffer->selective_display_ellipses);
2652
2653 /* Display table to use. */
2654 it->dp = window_display_table (w);
2655
2656 /* Are multibyte characters enabled in current_buffer? */
2657 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2658
2659 /* Do we need to reorder bidirectional text? */
2660 it->bidi_p = !NILP (current_buffer->bidi_display_reordering);
2661
2662 /* Non-zero if we should highlight the region. */
2663 highlight_region_p
2664 = (!NILP (Vtransient_mark_mode)
2665 && !NILP (current_buffer->mark_active)
2666 && XMARKER (current_buffer->mark)->buffer != 0);
2667
2668 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2669 start and end of a visible region in window IT->w. Set both to
2670 -1 to indicate no region. */
2671 if (highlight_region_p
2672 /* Maybe highlight only in selected window. */
2673 && (/* Either show region everywhere. */
2674 highlight_nonselected_windows
2675 /* Or show region in the selected window. */
2676 || w == XWINDOW (selected_window)
2677 /* Or show the region if we are in the mini-buffer and W is
2678 the window the mini-buffer refers to. */
2679 || (MINI_WINDOW_P (XWINDOW (selected_window))
2680 && WINDOWP (minibuf_selected_window)
2681 && w == XWINDOW (minibuf_selected_window))))
2682 {
2683 int charpos = marker_position (current_buffer->mark);
2684 it->region_beg_charpos = min (PT, charpos);
2685 it->region_end_charpos = max (PT, charpos);
2686 }
2687 else
2688 it->region_beg_charpos = it->region_end_charpos = -1;
2689
2690 /* Get the position at which the redisplay_end_trigger hook should
2691 be run, if it is to be run at all. */
2692 if (MARKERP (w->redisplay_end_trigger)
2693 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2694 it->redisplay_end_trigger_charpos
2695 = marker_position (w->redisplay_end_trigger);
2696 else if (INTEGERP (w->redisplay_end_trigger))
2697 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2698
2699 /* Correct bogus values of tab_width. */
2700 it->tab_width = XINT (current_buffer->tab_width);
2701 if (it->tab_width <= 0 || it->tab_width > 1000)
2702 it->tab_width = 8;
2703
2704 /* Are lines in the display truncated? */
2705 if (base_face_id != DEFAULT_FACE_ID
2706 || XINT (it->w->hscroll)
2707 || (! WINDOW_FULL_WIDTH_P (it->w)
2708 && ((!NILP (Vtruncate_partial_width_windows)
2709 && !INTEGERP (Vtruncate_partial_width_windows))
2710 || (INTEGERP (Vtruncate_partial_width_windows)
2711 && (WINDOW_TOTAL_COLS (it->w)
2712 < XINT (Vtruncate_partial_width_windows))))))
2713 it->line_wrap = TRUNCATE;
2714 else if (NILP (current_buffer->truncate_lines))
2715 it->line_wrap = NILP (current_buffer->word_wrap)
2716 ? WINDOW_WRAP : WORD_WRAP;
2717 else
2718 it->line_wrap = TRUNCATE;
2719
2720 /* Get dimensions of truncation and continuation glyphs. These are
2721 displayed as fringe bitmaps under X, so we don't need them for such
2722 frames. */
2723 if (!FRAME_WINDOW_P (it->f))
2724 {
2725 if (it->line_wrap == TRUNCATE)
2726 {
2727 /* We will need the truncation glyph. */
2728 xassert (it->glyph_row == NULL);
2729 produce_special_glyphs (it, IT_TRUNCATION);
2730 it->truncation_pixel_width = it->pixel_width;
2731 }
2732 else
2733 {
2734 /* We will need the continuation glyph. */
2735 xassert (it->glyph_row == NULL);
2736 produce_special_glyphs (it, IT_CONTINUATION);
2737 it->continuation_pixel_width = it->pixel_width;
2738 }
2739
2740 /* Reset these values to zero because the produce_special_glyphs
2741 above has changed them. */
2742 it->pixel_width = it->ascent = it->descent = 0;
2743 it->phys_ascent = it->phys_descent = 0;
2744 }
2745
2746 /* Set this after getting the dimensions of truncation and
2747 continuation glyphs, so that we don't produce glyphs when calling
2748 produce_special_glyphs, above. */
2749 it->glyph_row = row;
2750 it->area = TEXT_AREA;
2751
2752 /* Forget any previous info about this row being reversed. */
2753 if (it->glyph_row)
2754 it->glyph_row->reversed_p = 0;
2755
2756 /* Get the dimensions of the display area. The display area
2757 consists of the visible window area plus a horizontally scrolled
2758 part to the left of the window. All x-values are relative to the
2759 start of this total display area. */
2760 if (base_face_id != DEFAULT_FACE_ID)
2761 {
2762 /* Mode lines, menu bar in terminal frames. */
2763 it->first_visible_x = 0;
2764 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2765 }
2766 else
2767 {
2768 it->first_visible_x
2769 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2770 it->last_visible_x = (it->first_visible_x
2771 + window_box_width (w, TEXT_AREA));
2772
2773 /* If we truncate lines, leave room for the truncator glyph(s) at
2774 the right margin. Otherwise, leave room for the continuation
2775 glyph(s). Truncation and continuation glyphs are not inserted
2776 for window-based redisplay. */
2777 if (!FRAME_WINDOW_P (it->f))
2778 {
2779 if (it->line_wrap == TRUNCATE)
2780 it->last_visible_x -= it->truncation_pixel_width;
2781 else
2782 it->last_visible_x -= it->continuation_pixel_width;
2783 }
2784
2785 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2786 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2787 }
2788
2789 /* Leave room for a border glyph. */
2790 if (!FRAME_WINDOW_P (it->f)
2791 && !WINDOW_RIGHTMOST_P (it->w))
2792 it->last_visible_x -= 1;
2793
2794 it->last_visible_y = window_text_bottom_y (w);
2795
2796 /* For mode lines and alike, arrange for the first glyph having a
2797 left box line if the face specifies a box. */
2798 if (base_face_id != DEFAULT_FACE_ID)
2799 {
2800 struct face *face;
2801
2802 it->face_id = remapped_base_face_id;
2803
2804 /* If we have a boxed mode line, make the first character appear
2805 with a left box line. */
2806 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2807 if (face->box != FACE_NO_BOX)
2808 it->start_of_box_run_p = 1;
2809 }
2810
2811 /* If we are to reorder bidirectional text, init the bidi
2812 iterator. */
2813 if (it->bidi_p)
2814 {
2815 /* Note the paragraph direction that this buffer wants to
2816 use. */
2817 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2818 it->paragraph_embedding = L2R;
2819 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2820 it->paragraph_embedding = R2L;
2821 else
2822 it->paragraph_embedding = NEUTRAL_DIR;
2823 bidi_init_it (charpos, bytepos, &it->bidi_it);
2824 }
2825
2826 /* If a buffer position was specified, set the iterator there,
2827 getting overlays and face properties from that position. */
2828 if (charpos >= BUF_BEG (current_buffer))
2829 {
2830 it->end_charpos = ZV;
2831 it->face_id = -1;
2832 IT_CHARPOS (*it) = charpos;
2833
2834 /* Compute byte position if not specified. */
2835 if (bytepos < charpos)
2836 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2837 else
2838 IT_BYTEPOS (*it) = bytepos;
2839
2840 it->start = it->current;
2841
2842 /* Compute faces etc. */
2843 reseat (it, it->current.pos, 1);
2844 }
2845
2846 CHECK_IT (it);
2847 }
2848
2849
2850 /* Initialize IT for the display of window W with window start POS. */
2851
2852 void
2853 start_display (it, w, pos)
2854 struct it *it;
2855 struct window *w;
2856 struct text_pos pos;
2857 {
2858 struct glyph_row *row;
2859 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2860
2861 row = w->desired_matrix->rows + first_vpos;
2862 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2863 it->first_vpos = first_vpos;
2864
2865 /* Don't reseat to previous visible line start if current start
2866 position is in a string or image. */
2867 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2868 {
2869 int start_at_line_beg_p;
2870 int first_y = it->current_y;
2871
2872 /* If window start is not at a line start, skip forward to POS to
2873 get the correct continuation lines width. */
2874 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2875 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2876 if (!start_at_line_beg_p)
2877 {
2878 int new_x;
2879
2880 reseat_at_previous_visible_line_start (it);
2881 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2882
2883 new_x = it->current_x + it->pixel_width;
2884
2885 /* If lines are continued, this line may end in the middle
2886 of a multi-glyph character (e.g. a control character
2887 displayed as \003, or in the middle of an overlay
2888 string). In this case move_it_to above will not have
2889 taken us to the start of the continuation line but to the
2890 end of the continued line. */
2891 if (it->current_x > 0
2892 && it->line_wrap != TRUNCATE /* Lines are continued. */
2893 && (/* And glyph doesn't fit on the line. */
2894 new_x > it->last_visible_x
2895 /* Or it fits exactly and we're on a window
2896 system frame. */
2897 || (new_x == it->last_visible_x
2898 && FRAME_WINDOW_P (it->f))))
2899 {
2900 if (it->current.dpvec_index >= 0
2901 || it->current.overlay_string_index >= 0)
2902 {
2903 set_iterator_to_next (it, 1);
2904 move_it_in_display_line_to (it, -1, -1, 0);
2905 }
2906
2907 it->continuation_lines_width += it->current_x;
2908 }
2909
2910 /* We're starting a new display line, not affected by the
2911 height of the continued line, so clear the appropriate
2912 fields in the iterator structure. */
2913 it->max_ascent = it->max_descent = 0;
2914 it->max_phys_ascent = it->max_phys_descent = 0;
2915
2916 it->current_y = first_y;
2917 it->vpos = 0;
2918 it->current_x = it->hpos = 0;
2919 }
2920 }
2921 }
2922
2923
2924 /* Return 1 if POS is a position in ellipses displayed for invisible
2925 text. W is the window we display, for text property lookup. */
2926
2927 static int
2928 in_ellipses_for_invisible_text_p (pos, w)
2929 struct display_pos *pos;
2930 struct window *w;
2931 {
2932 Lisp_Object prop, window;
2933 int ellipses_p = 0;
2934 int charpos = CHARPOS (pos->pos);
2935
2936 /* If POS specifies a position in a display vector, this might
2937 be for an ellipsis displayed for invisible text. We won't
2938 get the iterator set up for delivering that ellipsis unless
2939 we make sure that it gets aware of the invisible text. */
2940 if (pos->dpvec_index >= 0
2941 && pos->overlay_string_index < 0
2942 && CHARPOS (pos->string_pos) < 0
2943 && charpos > BEGV
2944 && (XSETWINDOW (window, w),
2945 prop = Fget_char_property (make_number (charpos),
2946 Qinvisible, window),
2947 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2948 {
2949 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2950 window);
2951 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2952 }
2953
2954 return ellipses_p;
2955 }
2956
2957
2958 /* Initialize IT for stepping through current_buffer in window W,
2959 starting at position POS that includes overlay string and display
2960 vector/ control character translation position information. Value
2961 is zero if there are overlay strings with newlines at POS. */
2962
2963 static int
2964 init_from_display_pos (it, w, pos)
2965 struct it *it;
2966 struct window *w;
2967 struct display_pos *pos;
2968 {
2969 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2970 int i, overlay_strings_with_newlines = 0;
2971
2972 /* If POS specifies a position in a display vector, this might
2973 be for an ellipsis displayed for invisible text. We won't
2974 get the iterator set up for delivering that ellipsis unless
2975 we make sure that it gets aware of the invisible text. */
2976 if (in_ellipses_for_invisible_text_p (pos, w))
2977 {
2978 --charpos;
2979 bytepos = 0;
2980 }
2981
2982 /* Keep in mind: the call to reseat in init_iterator skips invisible
2983 text, so we might end up at a position different from POS. This
2984 is only a problem when POS is a row start after a newline and an
2985 overlay starts there with an after-string, and the overlay has an
2986 invisible property. Since we don't skip invisible text in
2987 display_line and elsewhere immediately after consuming the
2988 newline before the row start, such a POS will not be in a string,
2989 but the call to init_iterator below will move us to the
2990 after-string. */
2991 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2992
2993 /* This only scans the current chunk -- it should scan all chunks.
2994 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2995 to 16 in 22.1 to make this a lesser problem. */
2996 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2997 {
2998 const char *s = SDATA (it->overlay_strings[i]);
2999 const char *e = s + SBYTES (it->overlay_strings[i]);
3000
3001 while (s < e && *s != '\n')
3002 ++s;
3003
3004 if (s < e)
3005 {
3006 overlay_strings_with_newlines = 1;
3007 break;
3008 }
3009 }
3010
3011 /* If position is within an overlay string, set up IT to the right
3012 overlay string. */
3013 if (pos->overlay_string_index >= 0)
3014 {
3015 int relative_index;
3016
3017 /* If the first overlay string happens to have a `display'
3018 property for an image, the iterator will be set up for that
3019 image, and we have to undo that setup first before we can
3020 correct the overlay string index. */
3021 if (it->method == GET_FROM_IMAGE)
3022 pop_it (it);
3023
3024 /* We already have the first chunk of overlay strings in
3025 IT->overlay_strings. Load more until the one for
3026 pos->overlay_string_index is in IT->overlay_strings. */
3027 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3028 {
3029 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3030 it->current.overlay_string_index = 0;
3031 while (n--)
3032 {
3033 load_overlay_strings (it, 0);
3034 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3035 }
3036 }
3037
3038 it->current.overlay_string_index = pos->overlay_string_index;
3039 relative_index = (it->current.overlay_string_index
3040 % OVERLAY_STRING_CHUNK_SIZE);
3041 it->string = it->overlay_strings[relative_index];
3042 xassert (STRINGP (it->string));
3043 it->current.string_pos = pos->string_pos;
3044 it->method = GET_FROM_STRING;
3045 }
3046
3047 if (CHARPOS (pos->string_pos) >= 0)
3048 {
3049 /* Recorded position is not in an overlay string, but in another
3050 string. This can only be a string from a `display' property.
3051 IT should already be filled with that string. */
3052 it->current.string_pos = pos->string_pos;
3053 xassert (STRINGP (it->string));
3054 }
3055
3056 /* Restore position in display vector translations, control
3057 character translations or ellipses. */
3058 if (pos->dpvec_index >= 0)
3059 {
3060 if (it->dpvec == NULL)
3061 get_next_display_element (it);
3062 xassert (it->dpvec && it->current.dpvec_index == 0);
3063 it->current.dpvec_index = pos->dpvec_index;
3064 }
3065
3066 CHECK_IT (it);
3067 return !overlay_strings_with_newlines;
3068 }
3069
3070
3071 /* Initialize IT for stepping through current_buffer in window W
3072 starting at ROW->start. */
3073
3074 static void
3075 init_to_row_start (it, w, row)
3076 struct it *it;
3077 struct window *w;
3078 struct glyph_row *row;
3079 {
3080 init_from_display_pos (it, w, &row->start);
3081 it->start = row->start;
3082 it->continuation_lines_width = row->continuation_lines_width;
3083 CHECK_IT (it);
3084 }
3085
3086
3087 /* Initialize IT for stepping through current_buffer in window W
3088 starting in the line following ROW, i.e. starting at ROW->end.
3089 Value is zero if there are overlay strings with newlines at ROW's
3090 end position. */
3091
3092 static int
3093 init_to_row_end (it, w, row)
3094 struct it *it;
3095 struct window *w;
3096 struct glyph_row *row;
3097 {
3098 int success = 0;
3099
3100 if (init_from_display_pos (it, w, &row->end))
3101 {
3102 if (row->continued_p)
3103 it->continuation_lines_width
3104 = row->continuation_lines_width + row->pixel_width;
3105 CHECK_IT (it);
3106 success = 1;
3107 }
3108
3109 return success;
3110 }
3111
3112
3113
3114 \f
3115 /***********************************************************************
3116 Text properties
3117 ***********************************************************************/
3118
3119 /* Called when IT reaches IT->stop_charpos. Handle text property and
3120 overlay changes. Set IT->stop_charpos to the next position where
3121 to stop. */
3122
3123 static void
3124 handle_stop (it)
3125 struct it *it;
3126 {
3127 enum prop_handled handled;
3128 int handle_overlay_change_p;
3129 struct props *p;
3130
3131 it->dpvec = NULL;
3132 it->current.dpvec_index = -1;
3133 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3134 it->ignore_overlay_strings_at_pos_p = 0;
3135 it->ellipsis_p = 0;
3136
3137 /* Use face of preceding text for ellipsis (if invisible) */
3138 if (it->selective_display_ellipsis_p)
3139 it->saved_face_id = it->face_id;
3140
3141 do
3142 {
3143 handled = HANDLED_NORMALLY;
3144
3145 /* Call text property handlers. */
3146 for (p = it_props; p->handler; ++p)
3147 {
3148 handled = p->handler (it);
3149
3150 if (handled == HANDLED_RECOMPUTE_PROPS)
3151 break;
3152 else if (handled == HANDLED_RETURN)
3153 {
3154 /* We still want to show before and after strings from
3155 overlays even if the actual buffer text is replaced. */
3156 if (!handle_overlay_change_p
3157 || it->sp > 1
3158 || !get_overlay_strings_1 (it, 0, 0))
3159 {
3160 if (it->ellipsis_p)
3161 setup_for_ellipsis (it, 0);
3162 /* When handling a display spec, we might load an
3163 empty string. In that case, discard it here. We
3164 used to discard it in handle_single_display_spec,
3165 but that causes get_overlay_strings_1, above, to
3166 ignore overlay strings that we must check. */
3167 if (STRINGP (it->string) && !SCHARS (it->string))
3168 pop_it (it);
3169 return;
3170 }
3171 else if (STRINGP (it->string) && !SCHARS (it->string))
3172 pop_it (it);
3173 else
3174 {
3175 it->ignore_overlay_strings_at_pos_p = 1;
3176 it->string_from_display_prop_p = 0;
3177 handle_overlay_change_p = 0;
3178 }
3179 handled = HANDLED_RECOMPUTE_PROPS;
3180 break;
3181 }
3182 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3183 handle_overlay_change_p = 0;
3184 }
3185
3186 if (handled != HANDLED_RECOMPUTE_PROPS)
3187 {
3188 /* Don't check for overlay strings below when set to deliver
3189 characters from a display vector. */
3190 if (it->method == GET_FROM_DISPLAY_VECTOR)
3191 handle_overlay_change_p = 0;
3192
3193 /* Handle overlay changes.
3194 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3195 if it finds overlays. */
3196 if (handle_overlay_change_p)
3197 handled = handle_overlay_change (it);
3198 }
3199
3200 if (it->ellipsis_p)
3201 {
3202 setup_for_ellipsis (it, 0);
3203 break;
3204 }
3205 }
3206 while (handled == HANDLED_RECOMPUTE_PROPS);
3207
3208 /* Determine where to stop next. */
3209 if (handled == HANDLED_NORMALLY)
3210 compute_stop_pos (it);
3211 }
3212
3213
3214 /* Compute IT->stop_charpos from text property and overlay change
3215 information for IT's current position. */
3216
3217 static void
3218 compute_stop_pos (it)
3219 struct it *it;
3220 {
3221 register INTERVAL iv, next_iv;
3222 Lisp_Object object, limit, position;
3223 EMACS_INT charpos, bytepos;
3224
3225 /* If nowhere else, stop at the end. */
3226 it->stop_charpos = it->end_charpos;
3227
3228 if (STRINGP (it->string))
3229 {
3230 /* Strings are usually short, so don't limit the search for
3231 properties. */
3232 object = it->string;
3233 limit = Qnil;
3234 charpos = IT_STRING_CHARPOS (*it);
3235 bytepos = IT_STRING_BYTEPOS (*it);
3236 }
3237 else
3238 {
3239 EMACS_INT pos;
3240
3241 /* If next overlay change is in front of the current stop pos
3242 (which is IT->end_charpos), stop there. Note: value of
3243 next_overlay_change is point-max if no overlay change
3244 follows. */
3245 charpos = IT_CHARPOS (*it);
3246 bytepos = IT_BYTEPOS (*it);
3247 pos = next_overlay_change (charpos);
3248 if (pos < it->stop_charpos)
3249 it->stop_charpos = pos;
3250
3251 /* If showing the region, we have to stop at the region
3252 start or end because the face might change there. */
3253 if (it->region_beg_charpos > 0)
3254 {
3255 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3256 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3257 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3258 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3259 }
3260
3261 /* Set up variables for computing the stop position from text
3262 property changes. */
3263 XSETBUFFER (object, current_buffer);
3264 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3265 }
3266
3267 /* Get the interval containing IT's position. Value is a null
3268 interval if there isn't such an interval. */
3269 position = make_number (charpos);
3270 iv = validate_interval_range (object, &position, &position, 0);
3271 if (!NULL_INTERVAL_P (iv))
3272 {
3273 Lisp_Object values_here[LAST_PROP_IDX];
3274 struct props *p;
3275
3276 /* Get properties here. */
3277 for (p = it_props; p->handler; ++p)
3278 values_here[p->idx] = textget (iv->plist, *p->name);
3279
3280 /* Look for an interval following iv that has different
3281 properties. */
3282 for (next_iv = next_interval (iv);
3283 (!NULL_INTERVAL_P (next_iv)
3284 && (NILP (limit)
3285 || XFASTINT (limit) > next_iv->position));
3286 next_iv = next_interval (next_iv))
3287 {
3288 for (p = it_props; p->handler; ++p)
3289 {
3290 Lisp_Object new_value;
3291
3292 new_value = textget (next_iv->plist, *p->name);
3293 if (!EQ (values_here[p->idx], new_value))
3294 break;
3295 }
3296
3297 if (p->handler)
3298 break;
3299 }
3300
3301 if (!NULL_INTERVAL_P (next_iv))
3302 {
3303 if (INTEGERP (limit)
3304 && next_iv->position >= XFASTINT (limit))
3305 /* No text property change up to limit. */
3306 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3307 else
3308 /* Text properties change in next_iv. */
3309 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3310 }
3311 }
3312
3313 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3314 it->stop_charpos, it->string);
3315
3316 xassert (STRINGP (it->string)
3317 || (it->stop_charpos >= BEGV
3318 && it->stop_charpos >= IT_CHARPOS (*it)));
3319 }
3320
3321
3322 /* Return the position of the next overlay change after POS in
3323 current_buffer. Value is point-max if no overlay change
3324 follows. This is like `next-overlay-change' but doesn't use
3325 xmalloc. */
3326
3327 static EMACS_INT
3328 next_overlay_change (pos)
3329 EMACS_INT pos;
3330 {
3331 int noverlays;
3332 EMACS_INT endpos;
3333 Lisp_Object *overlays;
3334 int i;
3335
3336 /* Get all overlays at the given position. */
3337 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3338
3339 /* If any of these overlays ends before endpos,
3340 use its ending point instead. */
3341 for (i = 0; i < noverlays; ++i)
3342 {
3343 Lisp_Object oend;
3344 EMACS_INT oendpos;
3345
3346 oend = OVERLAY_END (overlays[i]);
3347 oendpos = OVERLAY_POSITION (oend);
3348 endpos = min (endpos, oendpos);
3349 }
3350
3351 return endpos;
3352 }
3353
3354
3355 \f
3356 /***********************************************************************
3357 Fontification
3358 ***********************************************************************/
3359
3360 /* Handle changes in the `fontified' property of the current buffer by
3361 calling hook functions from Qfontification_functions to fontify
3362 regions of text. */
3363
3364 static enum prop_handled
3365 handle_fontified_prop (it)
3366 struct it *it;
3367 {
3368 Lisp_Object prop, pos;
3369 enum prop_handled handled = HANDLED_NORMALLY;
3370
3371 if (!NILP (Vmemory_full))
3372 return handled;
3373
3374 /* Get the value of the `fontified' property at IT's current buffer
3375 position. (The `fontified' property doesn't have a special
3376 meaning in strings.) If the value is nil, call functions from
3377 Qfontification_functions. */
3378 if (!STRINGP (it->string)
3379 && it->s == NULL
3380 && !NILP (Vfontification_functions)
3381 && !NILP (Vrun_hooks)
3382 && (pos = make_number (IT_CHARPOS (*it)),
3383 prop = Fget_char_property (pos, Qfontified, Qnil),
3384 /* Ignore the special cased nil value always present at EOB since
3385 no amount of fontifying will be able to change it. */
3386 NILP (prop) && IT_CHARPOS (*it) < Z))
3387 {
3388 int count = SPECPDL_INDEX ();
3389 Lisp_Object val;
3390
3391 val = Vfontification_functions;
3392 specbind (Qfontification_functions, Qnil);
3393
3394 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3395 safe_call1 (val, pos);
3396 else
3397 {
3398 Lisp_Object globals, fn;
3399 struct gcpro gcpro1, gcpro2;
3400
3401 globals = Qnil;
3402 GCPRO2 (val, globals);
3403
3404 for (; CONSP (val); val = XCDR (val))
3405 {
3406 fn = XCAR (val);
3407
3408 if (EQ (fn, Qt))
3409 {
3410 /* A value of t indicates this hook has a local
3411 binding; it means to run the global binding too.
3412 In a global value, t should not occur. If it
3413 does, we must ignore it to avoid an endless
3414 loop. */
3415 for (globals = Fdefault_value (Qfontification_functions);
3416 CONSP (globals);
3417 globals = XCDR (globals))
3418 {
3419 fn = XCAR (globals);
3420 if (!EQ (fn, Qt))
3421 safe_call1 (fn, pos);
3422 }
3423 }
3424 else
3425 safe_call1 (fn, pos);
3426 }
3427
3428 UNGCPRO;
3429 }
3430
3431 unbind_to (count, Qnil);
3432
3433 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3434 something. This avoids an endless loop if they failed to
3435 fontify the text for which reason ever. */
3436 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3437 handled = HANDLED_RECOMPUTE_PROPS;
3438 }
3439
3440 return handled;
3441 }
3442
3443
3444 \f
3445 /***********************************************************************
3446 Faces
3447 ***********************************************************************/
3448
3449 /* Set up iterator IT from face properties at its current position.
3450 Called from handle_stop. */
3451
3452 static enum prop_handled
3453 handle_face_prop (it)
3454 struct it *it;
3455 {
3456 int new_face_id;
3457 EMACS_INT next_stop;
3458
3459 if (!STRINGP (it->string))
3460 {
3461 new_face_id
3462 = face_at_buffer_position (it->w,
3463 IT_CHARPOS (*it),
3464 it->region_beg_charpos,
3465 it->region_end_charpos,
3466 &next_stop,
3467 (IT_CHARPOS (*it)
3468 + TEXT_PROP_DISTANCE_LIMIT),
3469 0, it->base_face_id);
3470
3471 /* Is this a start of a run of characters with box face?
3472 Caveat: this can be called for a freshly initialized
3473 iterator; face_id is -1 in this case. We know that the new
3474 face will not change until limit, i.e. if the new face has a
3475 box, all characters up to limit will have one. But, as
3476 usual, we don't know whether limit is really the end. */
3477 if (new_face_id != it->face_id)
3478 {
3479 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3480
3481 /* If new face has a box but old face has not, this is
3482 the start of a run of characters with box, i.e. it has
3483 a shadow on the left side. The value of face_id of the
3484 iterator will be -1 if this is the initial call that gets
3485 the face. In this case, we have to look in front of IT's
3486 position and see whether there is a face != new_face_id. */
3487 it->start_of_box_run_p
3488 = (new_face->box != FACE_NO_BOX
3489 && (it->face_id >= 0
3490 || IT_CHARPOS (*it) == BEG
3491 || new_face_id != face_before_it_pos (it)));
3492 it->face_box_p = new_face->box != FACE_NO_BOX;
3493 }
3494 }
3495 else
3496 {
3497 int base_face_id, bufpos;
3498 int i;
3499 Lisp_Object from_overlay
3500 = (it->current.overlay_string_index >= 0
3501 ? it->string_overlays[it->current.overlay_string_index]
3502 : Qnil);
3503
3504 /* See if we got to this string directly or indirectly from
3505 an overlay property. That includes the before-string or
3506 after-string of an overlay, strings in display properties
3507 provided by an overlay, their text properties, etc.
3508
3509 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3510 if (! NILP (from_overlay))
3511 for (i = it->sp - 1; i >= 0; i--)
3512 {
3513 if (it->stack[i].current.overlay_string_index >= 0)
3514 from_overlay
3515 = it->string_overlays[it->stack[i].current.overlay_string_index];
3516 else if (! NILP (it->stack[i].from_overlay))
3517 from_overlay = it->stack[i].from_overlay;
3518
3519 if (!NILP (from_overlay))
3520 break;
3521 }
3522
3523 if (! NILP (from_overlay))
3524 {
3525 bufpos = IT_CHARPOS (*it);
3526 /* For a string from an overlay, the base face depends
3527 only on text properties and ignores overlays. */
3528 base_face_id
3529 = face_for_overlay_string (it->w,
3530 IT_CHARPOS (*it),
3531 it->region_beg_charpos,
3532 it->region_end_charpos,
3533 &next_stop,
3534 (IT_CHARPOS (*it)
3535 + TEXT_PROP_DISTANCE_LIMIT),
3536 0,
3537 from_overlay);
3538 }
3539 else
3540 {
3541 bufpos = 0;
3542
3543 /* For strings from a `display' property, use the face at
3544 IT's current buffer position as the base face to merge
3545 with, so that overlay strings appear in the same face as
3546 surrounding text, unless they specify their own
3547 faces. */
3548 base_face_id = underlying_face_id (it);
3549 }
3550
3551 new_face_id = face_at_string_position (it->w,
3552 it->string,
3553 IT_STRING_CHARPOS (*it),
3554 bufpos,
3555 it->region_beg_charpos,
3556 it->region_end_charpos,
3557 &next_stop,
3558 base_face_id, 0);
3559
3560 /* Is this a start of a run of characters with box? Caveat:
3561 this can be called for a freshly allocated iterator; face_id
3562 is -1 is this case. We know that the new face will not
3563 change until the next check pos, i.e. if the new face has a
3564 box, all characters up to that position will have a
3565 box. But, as usual, we don't know whether that position
3566 is really the end. */
3567 if (new_face_id != it->face_id)
3568 {
3569 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3570 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3571
3572 /* If new face has a box but old face hasn't, this is the
3573 start of a run of characters with box, i.e. it has a
3574 shadow on the left side. */
3575 it->start_of_box_run_p
3576 = new_face->box && (old_face == NULL || !old_face->box);
3577 it->face_box_p = new_face->box != FACE_NO_BOX;
3578 }
3579 }
3580
3581 it->face_id = new_face_id;
3582 return HANDLED_NORMALLY;
3583 }
3584
3585
3586 /* Return the ID of the face ``underlying'' IT's current position,
3587 which is in a string. If the iterator is associated with a
3588 buffer, return the face at IT's current buffer position.
3589 Otherwise, use the iterator's base_face_id. */
3590
3591 static int
3592 underlying_face_id (it)
3593 struct it *it;
3594 {
3595 int face_id = it->base_face_id, i;
3596
3597 xassert (STRINGP (it->string));
3598
3599 for (i = it->sp - 1; i >= 0; --i)
3600 if (NILP (it->stack[i].string))
3601 face_id = it->stack[i].face_id;
3602
3603 return face_id;
3604 }
3605
3606
3607 /* Compute the face one character before or after the current position
3608 of IT. BEFORE_P non-zero means get the face in front of IT's
3609 position. Value is the id of the face. */
3610
3611 static int
3612 face_before_or_after_it_pos (it, before_p)
3613 struct it *it;
3614 int before_p;
3615 {
3616 int face_id, limit;
3617 EMACS_INT next_check_charpos;
3618 struct text_pos pos;
3619
3620 xassert (it->s == NULL);
3621
3622 if (STRINGP (it->string))
3623 {
3624 int bufpos, base_face_id;
3625
3626 /* No face change past the end of the string (for the case
3627 we are padding with spaces). No face change before the
3628 string start. */
3629 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3630 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3631 return it->face_id;
3632
3633 /* Set pos to the position before or after IT's current position. */
3634 if (before_p)
3635 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3636 else
3637 /* For composition, we must check the character after the
3638 composition. */
3639 pos = (it->what == IT_COMPOSITION
3640 ? string_pos (IT_STRING_CHARPOS (*it)
3641 + it->cmp_it.nchars, it->string)
3642 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3643
3644 if (it->current.overlay_string_index >= 0)
3645 bufpos = IT_CHARPOS (*it);
3646 else
3647 bufpos = 0;
3648
3649 base_face_id = underlying_face_id (it);
3650
3651 /* Get the face for ASCII, or unibyte. */
3652 face_id = face_at_string_position (it->w,
3653 it->string,
3654 CHARPOS (pos),
3655 bufpos,
3656 it->region_beg_charpos,
3657 it->region_end_charpos,
3658 &next_check_charpos,
3659 base_face_id, 0);
3660
3661 /* Correct the face for charsets different from ASCII. Do it
3662 for the multibyte case only. The face returned above is
3663 suitable for unibyte text if IT->string is unibyte. */
3664 if (STRING_MULTIBYTE (it->string))
3665 {
3666 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3667 int rest = SBYTES (it->string) - BYTEPOS (pos);
3668 int c, len;
3669 struct face *face = FACE_FROM_ID (it->f, face_id);
3670
3671 c = string_char_and_length (p, &len);
3672 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3673 }
3674 }
3675 else
3676 {
3677 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3678 || (IT_CHARPOS (*it) <= BEGV && before_p))
3679 return it->face_id;
3680
3681 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3682 pos = it->current.pos;
3683
3684 if (before_p)
3685 DEC_TEXT_POS (pos, it->multibyte_p);
3686 else
3687 {
3688 if (it->what == IT_COMPOSITION)
3689 /* For composition, we must check the position after the
3690 composition. */
3691 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3692 else
3693 INC_TEXT_POS (pos, it->multibyte_p);
3694 }
3695
3696 /* Determine face for CHARSET_ASCII, or unibyte. */
3697 face_id = face_at_buffer_position (it->w,
3698 CHARPOS (pos),
3699 it->region_beg_charpos,
3700 it->region_end_charpos,
3701 &next_check_charpos,
3702 limit, 0, -1);
3703
3704 /* Correct the face for charsets different from ASCII. Do it
3705 for the multibyte case only. The face returned above is
3706 suitable for unibyte text if current_buffer is unibyte. */
3707 if (it->multibyte_p)
3708 {
3709 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3710 struct face *face = FACE_FROM_ID (it->f, face_id);
3711 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3712 }
3713 }
3714
3715 return face_id;
3716 }
3717
3718
3719 \f
3720 /***********************************************************************
3721 Invisible text
3722 ***********************************************************************/
3723
3724 /* Set up iterator IT from invisible properties at its current
3725 position. Called from handle_stop. */
3726
3727 static enum prop_handled
3728 handle_invisible_prop (it)
3729 struct it *it;
3730 {
3731 enum prop_handled handled = HANDLED_NORMALLY;
3732
3733 if (STRINGP (it->string))
3734 {
3735 extern Lisp_Object Qinvisible;
3736 Lisp_Object prop, end_charpos, limit, charpos;
3737
3738 /* Get the value of the invisible text property at the
3739 current position. Value will be nil if there is no such
3740 property. */
3741 charpos = make_number (IT_STRING_CHARPOS (*it));
3742 prop = Fget_text_property (charpos, Qinvisible, it->string);
3743
3744 if (!NILP (prop)
3745 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3746 {
3747 handled = HANDLED_RECOMPUTE_PROPS;
3748
3749 /* Get the position at which the next change of the
3750 invisible text property can be found in IT->string.
3751 Value will be nil if the property value is the same for
3752 all the rest of IT->string. */
3753 XSETINT (limit, SCHARS (it->string));
3754 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3755 it->string, limit);
3756
3757 /* Text at current position is invisible. The next
3758 change in the property is at position end_charpos.
3759 Move IT's current position to that position. */
3760 if (INTEGERP (end_charpos)
3761 && XFASTINT (end_charpos) < XFASTINT (limit))
3762 {
3763 struct text_pos old;
3764 old = it->current.string_pos;
3765 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3766 compute_string_pos (&it->current.string_pos, old, it->string);
3767 }
3768 else
3769 {
3770 /* The rest of the string is invisible. If this is an
3771 overlay string, proceed with the next overlay string
3772 or whatever comes and return a character from there. */
3773 if (it->current.overlay_string_index >= 0)
3774 {
3775 next_overlay_string (it);
3776 /* Don't check for overlay strings when we just
3777 finished processing them. */
3778 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3779 }
3780 else
3781 {
3782 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3783 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3784 }
3785 }
3786 }
3787 }
3788 else
3789 {
3790 int invis_p;
3791 EMACS_INT newpos, next_stop, start_charpos, tem;
3792 Lisp_Object pos, prop, overlay;
3793
3794 /* First of all, is there invisible text at this position? */
3795 tem = start_charpos = IT_CHARPOS (*it);
3796 pos = make_number (tem);
3797 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3798 &overlay);
3799 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3800
3801 /* If we are on invisible text, skip over it. */
3802 if (invis_p && start_charpos < it->end_charpos)
3803 {
3804 /* Record whether we have to display an ellipsis for the
3805 invisible text. */
3806 int display_ellipsis_p = invis_p == 2;
3807
3808 handled = HANDLED_RECOMPUTE_PROPS;
3809
3810 /* Loop skipping over invisible text. The loop is left at
3811 ZV or with IT on the first char being visible again. */
3812 do
3813 {
3814 /* Try to skip some invisible text. Return value is the
3815 position reached which can be equal to where we start
3816 if there is nothing invisible there. This skips both
3817 over invisible text properties and overlays with
3818 invisible property. */
3819 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3820
3821 /* If we skipped nothing at all we weren't at invisible
3822 text in the first place. If everything to the end of
3823 the buffer was skipped, end the loop. */
3824 if (newpos == tem || newpos >= ZV)
3825 invis_p = 0;
3826 else
3827 {
3828 /* We skipped some characters but not necessarily
3829 all there are. Check if we ended up on visible
3830 text. Fget_char_property returns the property of
3831 the char before the given position, i.e. if we
3832 get invis_p = 0, this means that the char at
3833 newpos is visible. */
3834 pos = make_number (newpos);
3835 prop = Fget_char_property (pos, Qinvisible, it->window);
3836 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3837 }
3838
3839 /* If we ended up on invisible text, proceed to
3840 skip starting with next_stop. */
3841 if (invis_p)
3842 tem = next_stop;
3843
3844 /* If there are adjacent invisible texts, don't lose the
3845 second one's ellipsis. */
3846 if (invis_p == 2)
3847 display_ellipsis_p = 1;
3848 }
3849 while (invis_p);
3850
3851 /* The position newpos is now either ZV or on visible text. */
3852 if (it->bidi_p && newpos < ZV)
3853 {
3854 /* With bidi iteration, the region of invisible text
3855 could start and/or end in the middle of a non-base
3856 embedding level. Therefore, we need to skip
3857 invisible text using the bidi iterator, starting at
3858 IT's current position, until we find ourselves
3859 outside the invisible text. Skipping invisible text
3860 _after_ bidi iteration avoids affecting the visual
3861 order of the displayed text when invisible properties
3862 are added or removed. */
3863 if (it->bidi_it.first_elt)
3864 {
3865 /* If we were `reseat'ed to a new paragraph,
3866 determine the paragraph base direction. We need
3867 to do it now because next_element_from_buffer may
3868 not have a chance to do it, if we are going to
3869 skip any text at the beginning, which resets the
3870 FIRST_ELT flag. */
3871 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
3872 /* If the paragraph base direction is R2L, its
3873 glyphs should be reversed. */
3874 if (it->glyph_row)
3875 {
3876 if (it->bidi_it.paragraph_dir == R2L)
3877 it->glyph_row->reversed_p = 1;
3878 else
3879 it->glyph_row->reversed_p = 0;
3880 }
3881 }
3882 do
3883 {
3884 bidi_get_next_char_visually (&it->bidi_it);
3885 }
3886 while (it->stop_charpos <= it->bidi_it.charpos
3887 && it->bidi_it.charpos < newpos);
3888 IT_CHARPOS (*it) = it->bidi_it.charpos;
3889 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3890 /* If we overstepped NEWPOS, record its position in the
3891 iterator, so that we skip invisible text if later the
3892 bidi iteration lands us in the invisible region
3893 again. */
3894 if (IT_CHARPOS (*it) >= newpos)
3895 it->prev_stop = newpos;
3896 }
3897 else
3898 {
3899 IT_CHARPOS (*it) = newpos;
3900 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3901 }
3902
3903 /* If there are before-strings at the start of invisible
3904 text, and the text is invisible because of a text
3905 property, arrange to show before-strings because 20.x did
3906 it that way. (If the text is invisible because of an
3907 overlay property instead of a text property, this is
3908 already handled in the overlay code.) */
3909 if (NILP (overlay)
3910 && get_overlay_strings (it, it->stop_charpos))
3911 {
3912 handled = HANDLED_RECOMPUTE_PROPS;
3913 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3914 }
3915 else if (display_ellipsis_p)
3916 {
3917 /* Make sure that the glyphs of the ellipsis will get
3918 correct `charpos' values. If we would not update
3919 it->position here, the glyphs would belong to the
3920 last visible character _before_ the invisible
3921 text, which confuses `set_cursor_from_row'.
3922
3923 We use the last invisible position instead of the
3924 first because this way the cursor is always drawn on
3925 the first "." of the ellipsis, whenever PT is inside
3926 the invisible text. Otherwise the cursor would be
3927 placed _after_ the ellipsis when the point is after the
3928 first invisible character. */
3929 if (!STRINGP (it->object))
3930 {
3931 it->position.charpos = newpos - 1;
3932 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3933 }
3934 it->ellipsis_p = 1;
3935 /* Let the ellipsis display before
3936 considering any properties of the following char.
3937 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3938 handled = HANDLED_RETURN;
3939 }
3940 }
3941 }
3942
3943 return handled;
3944 }
3945
3946
3947 /* Make iterator IT return `...' next.
3948 Replaces LEN characters from buffer. */
3949
3950 static void
3951 setup_for_ellipsis (it, len)
3952 struct it *it;
3953 int len;
3954 {
3955 /* Use the display table definition for `...'. Invalid glyphs
3956 will be handled by the method returning elements from dpvec. */
3957 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3958 {
3959 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3960 it->dpvec = v->contents;
3961 it->dpend = v->contents + v->size;
3962 }
3963 else
3964 {
3965 /* Default `...'. */
3966 it->dpvec = default_invis_vector;
3967 it->dpend = default_invis_vector + 3;
3968 }
3969
3970 it->dpvec_char_len = len;
3971 it->current.dpvec_index = 0;
3972 it->dpvec_face_id = -1;
3973
3974 /* Remember the current face id in case glyphs specify faces.
3975 IT's face is restored in set_iterator_to_next.
3976 saved_face_id was set to preceding char's face in handle_stop. */
3977 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3978 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3979
3980 it->method = GET_FROM_DISPLAY_VECTOR;
3981 it->ellipsis_p = 1;
3982 }
3983
3984
3985 \f
3986 /***********************************************************************
3987 'display' property
3988 ***********************************************************************/
3989
3990 /* Set up iterator IT from `display' property at its current position.
3991 Called from handle_stop.
3992 We return HANDLED_RETURN if some part of the display property
3993 overrides the display of the buffer text itself.
3994 Otherwise we return HANDLED_NORMALLY. */
3995
3996 static enum prop_handled
3997 handle_display_prop (it)
3998 struct it *it;
3999 {
4000 Lisp_Object prop, object, overlay;
4001 struct text_pos *position;
4002 /* Nonzero if some property replaces the display of the text itself. */
4003 int display_replaced_p = 0;
4004
4005 if (STRINGP (it->string))
4006 {
4007 object = it->string;
4008 position = &it->current.string_pos;
4009 }
4010 else
4011 {
4012 XSETWINDOW (object, it->w);
4013 position = &it->current.pos;
4014 }
4015
4016 /* Reset those iterator values set from display property values. */
4017 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4018 it->space_width = Qnil;
4019 it->font_height = Qnil;
4020 it->voffset = 0;
4021
4022 /* We don't support recursive `display' properties, i.e. string
4023 values that have a string `display' property, that have a string
4024 `display' property etc. */
4025 if (!it->string_from_display_prop_p)
4026 it->area = TEXT_AREA;
4027
4028 prop = get_char_property_and_overlay (make_number (position->charpos),
4029 Qdisplay, object, &overlay);
4030 if (NILP (prop))
4031 return HANDLED_NORMALLY;
4032 /* Now OVERLAY is the overlay that gave us this property, or nil
4033 if it was a text property. */
4034
4035 if (!STRINGP (it->string))
4036 object = it->w->buffer;
4037
4038 if (CONSP (prop)
4039 /* Simple properties. */
4040 && !EQ (XCAR (prop), Qimage)
4041 && !EQ (XCAR (prop), Qspace)
4042 && !EQ (XCAR (prop), Qwhen)
4043 && !EQ (XCAR (prop), Qslice)
4044 && !EQ (XCAR (prop), Qspace_width)
4045 && !EQ (XCAR (prop), Qheight)
4046 && !EQ (XCAR (prop), Qraise)
4047 /* Marginal area specifications. */
4048 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4049 && !EQ (XCAR (prop), Qleft_fringe)
4050 && !EQ (XCAR (prop), Qright_fringe)
4051 && !NILP (XCAR (prop)))
4052 {
4053 for (; CONSP (prop); prop = XCDR (prop))
4054 {
4055 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4056 position, display_replaced_p))
4057 {
4058 display_replaced_p = 1;
4059 /* If some text in a string is replaced, `position' no
4060 longer points to the position of `object'. */
4061 if (STRINGP (object))
4062 break;
4063 }
4064 }
4065 }
4066 else if (VECTORP (prop))
4067 {
4068 int i;
4069 for (i = 0; i < ASIZE (prop); ++i)
4070 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4071 position, display_replaced_p))
4072 {
4073 display_replaced_p = 1;
4074 /* If some text in a string is replaced, `position' no
4075 longer points to the position of `object'. */
4076 if (STRINGP (object))
4077 break;
4078 }
4079 }
4080 else
4081 {
4082 if (handle_single_display_spec (it, prop, object, overlay,
4083 position, 0))
4084 display_replaced_p = 1;
4085 }
4086
4087 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4088 }
4089
4090
4091 /* Value is the position of the end of the `display' property starting
4092 at START_POS in OBJECT. */
4093
4094 static struct text_pos
4095 display_prop_end (it, object, start_pos)
4096 struct it *it;
4097 Lisp_Object object;
4098 struct text_pos start_pos;
4099 {
4100 Lisp_Object end;
4101 struct text_pos end_pos;
4102
4103 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4104 Qdisplay, object, Qnil);
4105 CHARPOS (end_pos) = XFASTINT (end);
4106 if (STRINGP (object))
4107 compute_string_pos (&end_pos, start_pos, it->string);
4108 else
4109 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4110
4111 return end_pos;
4112 }
4113
4114
4115 /* Set up IT from a single `display' specification PROP. OBJECT
4116 is the object in which the `display' property was found. *POSITION
4117 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4118 means that we previously saw a display specification which already
4119 replaced text display with something else, for example an image;
4120 we ignore such properties after the first one has been processed.
4121
4122 OVERLAY is the overlay this `display' property came from,
4123 or nil if it was a text property.
4124
4125 If PROP is a `space' or `image' specification, and in some other
4126 cases too, set *POSITION to the position where the `display'
4127 property ends.
4128
4129 Value is non-zero if something was found which replaces the display
4130 of buffer or string text. */
4131
4132 static int
4133 handle_single_display_spec (it, spec, object, overlay, position,
4134 display_replaced_before_p)
4135 struct it *it;
4136 Lisp_Object spec;
4137 Lisp_Object object;
4138 Lisp_Object overlay;
4139 struct text_pos *position;
4140 int display_replaced_before_p;
4141 {
4142 Lisp_Object form;
4143 Lisp_Object location, value;
4144 struct text_pos start_pos, save_pos;
4145 int valid_p;
4146
4147 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4148 If the result is non-nil, use VALUE instead of SPEC. */
4149 form = Qt;
4150 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4151 {
4152 spec = XCDR (spec);
4153 if (!CONSP (spec))
4154 return 0;
4155 form = XCAR (spec);
4156 spec = XCDR (spec);
4157 }
4158
4159 if (!NILP (form) && !EQ (form, Qt))
4160 {
4161 int count = SPECPDL_INDEX ();
4162 struct gcpro gcpro1;
4163
4164 /* Bind `object' to the object having the `display' property, a
4165 buffer or string. Bind `position' to the position in the
4166 object where the property was found, and `buffer-position'
4167 to the current position in the buffer. */
4168 specbind (Qobject, object);
4169 specbind (Qposition, make_number (CHARPOS (*position)));
4170 specbind (Qbuffer_position,
4171 make_number (STRINGP (object)
4172 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4173 GCPRO1 (form);
4174 form = safe_eval (form);
4175 UNGCPRO;
4176 unbind_to (count, Qnil);
4177 }
4178
4179 if (NILP (form))
4180 return 0;
4181
4182 /* Handle `(height HEIGHT)' specifications. */
4183 if (CONSP (spec)
4184 && EQ (XCAR (spec), Qheight)
4185 && CONSP (XCDR (spec)))
4186 {
4187 if (!FRAME_WINDOW_P (it->f))
4188 return 0;
4189
4190 it->font_height = XCAR (XCDR (spec));
4191 if (!NILP (it->font_height))
4192 {
4193 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4194 int new_height = -1;
4195
4196 if (CONSP (it->font_height)
4197 && (EQ (XCAR (it->font_height), Qplus)
4198 || EQ (XCAR (it->font_height), Qminus))
4199 && CONSP (XCDR (it->font_height))
4200 && INTEGERP (XCAR (XCDR (it->font_height))))
4201 {
4202 /* `(+ N)' or `(- N)' where N is an integer. */
4203 int steps = XINT (XCAR (XCDR (it->font_height)));
4204 if (EQ (XCAR (it->font_height), Qplus))
4205 steps = - steps;
4206 it->face_id = smaller_face (it->f, it->face_id, steps);
4207 }
4208 else if (FUNCTIONP (it->font_height))
4209 {
4210 /* Call function with current height as argument.
4211 Value is the new height. */
4212 Lisp_Object height;
4213 height = safe_call1 (it->font_height,
4214 face->lface[LFACE_HEIGHT_INDEX]);
4215 if (NUMBERP (height))
4216 new_height = XFLOATINT (height);
4217 }
4218 else if (NUMBERP (it->font_height))
4219 {
4220 /* Value is a multiple of the canonical char height. */
4221 struct face *face;
4222
4223 face = FACE_FROM_ID (it->f,
4224 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4225 new_height = (XFLOATINT (it->font_height)
4226 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4227 }
4228 else
4229 {
4230 /* Evaluate IT->font_height with `height' bound to the
4231 current specified height to get the new height. */
4232 int count = SPECPDL_INDEX ();
4233
4234 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4235 value = safe_eval (it->font_height);
4236 unbind_to (count, Qnil);
4237
4238 if (NUMBERP (value))
4239 new_height = XFLOATINT (value);
4240 }
4241
4242 if (new_height > 0)
4243 it->face_id = face_with_height (it->f, it->face_id, new_height);
4244 }
4245
4246 return 0;
4247 }
4248
4249 /* Handle `(space-width WIDTH)'. */
4250 if (CONSP (spec)
4251 && EQ (XCAR (spec), Qspace_width)
4252 && CONSP (XCDR (spec)))
4253 {
4254 if (!FRAME_WINDOW_P (it->f))
4255 return 0;
4256
4257 value = XCAR (XCDR (spec));
4258 if (NUMBERP (value) && XFLOATINT (value) > 0)
4259 it->space_width = value;
4260
4261 return 0;
4262 }
4263
4264 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4265 if (CONSP (spec)
4266 && EQ (XCAR (spec), Qslice))
4267 {
4268 Lisp_Object tem;
4269
4270 if (!FRAME_WINDOW_P (it->f))
4271 return 0;
4272
4273 if (tem = XCDR (spec), CONSP (tem))
4274 {
4275 it->slice.x = XCAR (tem);
4276 if (tem = XCDR (tem), CONSP (tem))
4277 {
4278 it->slice.y = XCAR (tem);
4279 if (tem = XCDR (tem), CONSP (tem))
4280 {
4281 it->slice.width = XCAR (tem);
4282 if (tem = XCDR (tem), CONSP (tem))
4283 it->slice.height = XCAR (tem);
4284 }
4285 }
4286 }
4287
4288 return 0;
4289 }
4290
4291 /* Handle `(raise FACTOR)'. */
4292 if (CONSP (spec)
4293 && EQ (XCAR (spec), Qraise)
4294 && CONSP (XCDR (spec)))
4295 {
4296 if (!FRAME_WINDOW_P (it->f))
4297 return 0;
4298
4299 #ifdef HAVE_WINDOW_SYSTEM
4300 value = XCAR (XCDR (spec));
4301 if (NUMBERP (value))
4302 {
4303 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4304 it->voffset = - (XFLOATINT (value)
4305 * (FONT_HEIGHT (face->font)));
4306 }
4307 #endif /* HAVE_WINDOW_SYSTEM */
4308
4309 return 0;
4310 }
4311
4312 /* Don't handle the other kinds of display specifications
4313 inside a string that we got from a `display' property. */
4314 if (it->string_from_display_prop_p)
4315 return 0;
4316
4317 /* Characters having this form of property are not displayed, so
4318 we have to find the end of the property. */
4319 start_pos = *position;
4320 *position = display_prop_end (it, object, start_pos);
4321 value = Qnil;
4322
4323 /* Stop the scan at that end position--we assume that all
4324 text properties change there. */
4325 it->stop_charpos = position->charpos;
4326
4327 /* Handle `(left-fringe BITMAP [FACE])'
4328 and `(right-fringe BITMAP [FACE])'. */
4329 if (CONSP (spec)
4330 && (EQ (XCAR (spec), Qleft_fringe)
4331 || EQ (XCAR (spec), Qright_fringe))
4332 && CONSP (XCDR (spec)))
4333 {
4334 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4335 int fringe_bitmap;
4336
4337 if (!FRAME_WINDOW_P (it->f))
4338 /* If we return here, POSITION has been advanced
4339 across the text with this property. */
4340 return 0;
4341
4342 #ifdef HAVE_WINDOW_SYSTEM
4343 value = XCAR (XCDR (spec));
4344 if (!SYMBOLP (value)
4345 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4346 /* If we return here, POSITION has been advanced
4347 across the text with this property. */
4348 return 0;
4349
4350 if (CONSP (XCDR (XCDR (spec))))
4351 {
4352 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4353 int face_id2 = lookup_derived_face (it->f, face_name,
4354 FRINGE_FACE_ID, 0);
4355 if (face_id2 >= 0)
4356 face_id = face_id2;
4357 }
4358
4359 /* Save current settings of IT so that we can restore them
4360 when we are finished with the glyph property value. */
4361
4362 save_pos = it->position;
4363 it->position = *position;
4364 push_it (it);
4365 it->position = save_pos;
4366
4367 it->area = TEXT_AREA;
4368 it->what = IT_IMAGE;
4369 it->image_id = -1; /* no image */
4370 it->position = start_pos;
4371 it->object = NILP (object) ? it->w->buffer : object;
4372 it->method = GET_FROM_IMAGE;
4373 it->from_overlay = Qnil;
4374 it->face_id = face_id;
4375
4376 /* Say that we haven't consumed the characters with
4377 `display' property yet. The call to pop_it in
4378 set_iterator_to_next will clean this up. */
4379 *position = start_pos;
4380
4381 if (EQ (XCAR (spec), Qleft_fringe))
4382 {
4383 it->left_user_fringe_bitmap = fringe_bitmap;
4384 it->left_user_fringe_face_id = face_id;
4385 }
4386 else
4387 {
4388 it->right_user_fringe_bitmap = fringe_bitmap;
4389 it->right_user_fringe_face_id = face_id;
4390 }
4391 #endif /* HAVE_WINDOW_SYSTEM */
4392 return 1;
4393 }
4394
4395 /* Prepare to handle `((margin left-margin) ...)',
4396 `((margin right-margin) ...)' and `((margin nil) ...)'
4397 prefixes for display specifications. */
4398 location = Qunbound;
4399 if (CONSP (spec) && CONSP (XCAR (spec)))
4400 {
4401 Lisp_Object tem;
4402
4403 value = XCDR (spec);
4404 if (CONSP (value))
4405 value = XCAR (value);
4406
4407 tem = XCAR (spec);
4408 if (EQ (XCAR (tem), Qmargin)
4409 && (tem = XCDR (tem),
4410 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4411 (NILP (tem)
4412 || EQ (tem, Qleft_margin)
4413 || EQ (tem, Qright_margin))))
4414 location = tem;
4415 }
4416
4417 if (EQ (location, Qunbound))
4418 {
4419 location = Qnil;
4420 value = spec;
4421 }
4422
4423 /* After this point, VALUE is the property after any
4424 margin prefix has been stripped. It must be a string,
4425 an image specification, or `(space ...)'.
4426
4427 LOCATION specifies where to display: `left-margin',
4428 `right-margin' or nil. */
4429
4430 valid_p = (STRINGP (value)
4431 #ifdef HAVE_WINDOW_SYSTEM
4432 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4433 #endif /* not HAVE_WINDOW_SYSTEM */
4434 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4435
4436 if (valid_p && !display_replaced_before_p)
4437 {
4438 /* Save current settings of IT so that we can restore them
4439 when we are finished with the glyph property value. */
4440 save_pos = it->position;
4441 it->position = *position;
4442 push_it (it);
4443 it->position = save_pos;
4444 it->from_overlay = overlay;
4445
4446 if (NILP (location))
4447 it->area = TEXT_AREA;
4448 else if (EQ (location, Qleft_margin))
4449 it->area = LEFT_MARGIN_AREA;
4450 else
4451 it->area = RIGHT_MARGIN_AREA;
4452
4453 if (STRINGP (value))
4454 {
4455 it->string = value;
4456 it->multibyte_p = STRING_MULTIBYTE (it->string);
4457 it->current.overlay_string_index = -1;
4458 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4459 it->end_charpos = it->string_nchars = SCHARS (it->string);
4460 it->method = GET_FROM_STRING;
4461 it->stop_charpos = 0;
4462 it->string_from_display_prop_p = 1;
4463 /* Say that we haven't consumed the characters with
4464 `display' property yet. The call to pop_it in
4465 set_iterator_to_next will clean this up. */
4466 if (BUFFERP (object))
4467 *position = start_pos;
4468 }
4469 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4470 {
4471 it->method = GET_FROM_STRETCH;
4472 it->object = value;
4473 *position = it->position = start_pos;
4474 }
4475 #ifdef HAVE_WINDOW_SYSTEM
4476 else
4477 {
4478 it->what = IT_IMAGE;
4479 it->image_id = lookup_image (it->f, value);
4480 it->position = start_pos;
4481 it->object = NILP (object) ? it->w->buffer : object;
4482 it->method = GET_FROM_IMAGE;
4483
4484 /* Say that we haven't consumed the characters with
4485 `display' property yet. The call to pop_it in
4486 set_iterator_to_next will clean this up. */
4487 *position = start_pos;
4488 }
4489 #endif /* HAVE_WINDOW_SYSTEM */
4490
4491 return 1;
4492 }
4493
4494 /* Invalid property or property not supported. Restore
4495 POSITION to what it was before. */
4496 *position = start_pos;
4497 return 0;
4498 }
4499
4500
4501 /* Check if SPEC is a display sub-property value whose text should be
4502 treated as intangible. */
4503
4504 static int
4505 single_display_spec_intangible_p (prop)
4506 Lisp_Object prop;
4507 {
4508 /* Skip over `when FORM'. */
4509 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4510 {
4511 prop = XCDR (prop);
4512 if (!CONSP (prop))
4513 return 0;
4514 prop = XCDR (prop);
4515 }
4516
4517 if (STRINGP (prop))
4518 return 1;
4519
4520 if (!CONSP (prop))
4521 return 0;
4522
4523 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4524 we don't need to treat text as intangible. */
4525 if (EQ (XCAR (prop), Qmargin))
4526 {
4527 prop = XCDR (prop);
4528 if (!CONSP (prop))
4529 return 0;
4530
4531 prop = XCDR (prop);
4532 if (!CONSP (prop)
4533 || EQ (XCAR (prop), Qleft_margin)
4534 || EQ (XCAR (prop), Qright_margin))
4535 return 0;
4536 }
4537
4538 return (CONSP (prop)
4539 && (EQ (XCAR (prop), Qimage)
4540 || EQ (XCAR (prop), Qspace)));
4541 }
4542
4543
4544 /* Check if PROP is a display property value whose text should be
4545 treated as intangible. */
4546
4547 int
4548 display_prop_intangible_p (prop)
4549 Lisp_Object prop;
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_intangible_p (XCAR (prop)))
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_intangible_p (AREF (prop, i)))
4569 return 1;
4570 }
4571 else
4572 return single_display_spec_intangible_p (prop);
4573
4574 return 0;
4575 }
4576
4577
4578 /* Return 1 if PROP is a display sub-property value containing STRING. */
4579
4580 static int
4581 single_display_spec_string_p (prop, string)
4582 Lisp_Object prop, string;
4583 {
4584 if (EQ (string, prop))
4585 return 1;
4586
4587 /* Skip over `when FORM'. */
4588 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4589 {
4590 prop = XCDR (prop);
4591 if (!CONSP (prop))
4592 return 0;
4593 prop = XCDR (prop);
4594 }
4595
4596 if (CONSP (prop))
4597 /* Skip over `margin LOCATION'. */
4598 if (EQ (XCAR (prop), Qmargin))
4599 {
4600 prop = XCDR (prop);
4601 if (!CONSP (prop))
4602 return 0;
4603
4604 prop = XCDR (prop);
4605 if (!CONSP (prop))
4606 return 0;
4607 }
4608
4609 return CONSP (prop) && EQ (XCAR (prop), string);
4610 }
4611
4612
4613 /* Return 1 if STRING appears in the `display' property PROP. */
4614
4615 static int
4616 display_prop_string_p (prop, string)
4617 Lisp_Object prop, string;
4618 {
4619 if (CONSP (prop)
4620 && CONSP (XCAR (prop))
4621 && !EQ (Qmargin, XCAR (XCAR (prop))))
4622 {
4623 /* A list of sub-properties. */
4624 while (CONSP (prop))
4625 {
4626 if (single_display_spec_string_p (XCAR (prop), string))
4627 return 1;
4628 prop = XCDR (prop);
4629 }
4630 }
4631 else if (VECTORP (prop))
4632 {
4633 /* A vector of sub-properties. */
4634 int i;
4635 for (i = 0; i < ASIZE (prop); ++i)
4636 if (single_display_spec_string_p (AREF (prop, i), string))
4637 return 1;
4638 }
4639 else
4640 return single_display_spec_string_p (prop, string);
4641
4642 return 0;
4643 }
4644
4645 /* Look for STRING in overlays and text properties in W's buffer,
4646 between character positions FROM and TO (excluding TO).
4647 BACK_P non-zero means look back (in this case, TO is supposed to be
4648 less than FROM).
4649 Value is the first character position where STRING was found, or
4650 zero if it wasn't found before hitting TO.
4651
4652 W's buffer must be current.
4653
4654 This function may only use code that doesn't eval because it is
4655 called asynchronously from note_mouse_highlight. */
4656
4657 static EMACS_INT
4658 string_buffer_position_lim (w, string, from, to, back_p)
4659 struct window *w;
4660 Lisp_Object string;
4661 EMACS_INT from, to;
4662 int back_p;
4663 {
4664 Lisp_Object limit, prop, pos;
4665 int found = 0;
4666
4667 pos = make_number (from);
4668
4669 if (!back_p) /* looking forward */
4670 {
4671 limit = make_number (min (to, ZV));
4672 while (!found && !EQ (pos, limit))
4673 {
4674 prop = Fget_char_property (pos, Qdisplay, Qnil);
4675 if (!NILP (prop) && display_prop_string_p (prop, string))
4676 found = 1;
4677 else
4678 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4679 limit);
4680 }
4681 }
4682 else /* looking back */
4683 {
4684 limit = make_number (max (to, BEGV));
4685 while (!found && !EQ (pos, limit))
4686 {
4687 prop = Fget_char_property (pos, Qdisplay, Qnil);
4688 if (!NILP (prop) && display_prop_string_p (prop, string))
4689 found = 1;
4690 else
4691 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4692 limit);
4693 }
4694 }
4695
4696 return found ? XINT (pos) : 0;
4697 }
4698
4699 /* Determine which buffer position in W's buffer STRING comes from.
4700 AROUND_CHARPOS is an approximate position where it could come from.
4701 Value is the buffer position or 0 if it couldn't be determined.
4702
4703 W's buffer must be current.
4704
4705 This function is necessary because we don't record buffer positions
4706 in glyphs generated from strings (to keep struct glyph small).
4707 This function may only use code that doesn't eval because it is
4708 called asynchronously from note_mouse_highlight. */
4709
4710 EMACS_INT
4711 string_buffer_position (w, string, around_charpos)
4712 struct window *w;
4713 Lisp_Object string;
4714 EMACS_INT around_charpos;
4715 {
4716 Lisp_Object limit, prop, pos;
4717 const int MAX_DISTANCE = 1000;
4718 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4719 around_charpos + MAX_DISTANCE,
4720 0);
4721
4722 if (!found)
4723 found = string_buffer_position_lim (w, string, around_charpos,
4724 around_charpos - MAX_DISTANCE, 1);
4725 return found;
4726 }
4727
4728
4729 \f
4730 /***********************************************************************
4731 `composition' property
4732 ***********************************************************************/
4733
4734 /* Set up iterator IT from `composition' property at its current
4735 position. Called from handle_stop. */
4736
4737 static enum prop_handled
4738 handle_composition_prop (it)
4739 struct it *it;
4740 {
4741 Lisp_Object prop, string;
4742 EMACS_INT pos, pos_byte, start, end;
4743
4744 if (STRINGP (it->string))
4745 {
4746 unsigned char *s;
4747
4748 pos = IT_STRING_CHARPOS (*it);
4749 pos_byte = IT_STRING_BYTEPOS (*it);
4750 string = it->string;
4751 s = SDATA (string) + pos_byte;
4752 it->c = STRING_CHAR (s);
4753 }
4754 else
4755 {
4756 pos = IT_CHARPOS (*it);
4757 pos_byte = IT_BYTEPOS (*it);
4758 string = Qnil;
4759 it->c = FETCH_CHAR (pos_byte);
4760 }
4761
4762 /* If there's a valid composition and point is not inside of the
4763 composition (in the case that the composition is from the current
4764 buffer), draw a glyph composed from the composition components. */
4765 if (find_composition (pos, -1, &start, &end, &prop, string)
4766 && COMPOSITION_VALID_P (start, end, prop)
4767 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4768 {
4769 if (start != pos)
4770 {
4771 if (STRINGP (it->string))
4772 pos_byte = string_char_to_byte (it->string, start);
4773 else
4774 pos_byte = CHAR_TO_BYTE (start);
4775 }
4776 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4777 prop, string);
4778
4779 if (it->cmp_it.id >= 0)
4780 {
4781 it->cmp_it.ch = -1;
4782 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4783 it->cmp_it.nglyphs = -1;
4784 }
4785 }
4786
4787 return HANDLED_NORMALLY;
4788 }
4789
4790
4791 \f
4792 /***********************************************************************
4793 Overlay strings
4794 ***********************************************************************/
4795
4796 /* The following structure is used to record overlay strings for
4797 later sorting in load_overlay_strings. */
4798
4799 struct overlay_entry
4800 {
4801 Lisp_Object overlay;
4802 Lisp_Object string;
4803 int priority;
4804 int after_string_p;
4805 };
4806
4807
4808 /* Set up iterator IT from overlay strings at its current position.
4809 Called from handle_stop. */
4810
4811 static enum prop_handled
4812 handle_overlay_change (it)
4813 struct it *it;
4814 {
4815 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4816 return HANDLED_RECOMPUTE_PROPS;
4817 else
4818 return HANDLED_NORMALLY;
4819 }
4820
4821
4822 /* Set up the next overlay string for delivery by IT, if there is an
4823 overlay string to deliver. Called by set_iterator_to_next when the
4824 end of the current overlay string is reached. If there are more
4825 overlay strings to display, IT->string and
4826 IT->current.overlay_string_index are set appropriately here.
4827 Otherwise IT->string is set to nil. */
4828
4829 static void
4830 next_overlay_string (it)
4831 struct it *it;
4832 {
4833 ++it->current.overlay_string_index;
4834 if (it->current.overlay_string_index == it->n_overlay_strings)
4835 {
4836 /* No more overlay strings. Restore IT's settings to what
4837 they were before overlay strings were processed, and
4838 continue to deliver from current_buffer. */
4839
4840 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4841 pop_it (it);
4842 xassert (it->sp > 0
4843 || (NILP (it->string)
4844 && it->method == GET_FROM_BUFFER
4845 && it->stop_charpos >= BEGV
4846 && it->stop_charpos <= it->end_charpos));
4847 it->current.overlay_string_index = -1;
4848 it->n_overlay_strings = 0;
4849
4850 /* If we're at the end of the buffer, record that we have
4851 processed the overlay strings there already, so that
4852 next_element_from_buffer doesn't try it again. */
4853 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4854 it->overlay_strings_at_end_processed_p = 1;
4855 }
4856 else
4857 {
4858 /* There are more overlay strings to process. If
4859 IT->current.overlay_string_index has advanced to a position
4860 where we must load IT->overlay_strings with more strings, do
4861 it. */
4862 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4863
4864 if (it->current.overlay_string_index && i == 0)
4865 load_overlay_strings (it, 0);
4866
4867 /* Initialize IT to deliver display elements from the overlay
4868 string. */
4869 it->string = it->overlay_strings[i];
4870 it->multibyte_p = STRING_MULTIBYTE (it->string);
4871 SET_TEXT_POS (it->current.string_pos, 0, 0);
4872 it->method = GET_FROM_STRING;
4873 it->stop_charpos = 0;
4874 if (it->cmp_it.stop_pos >= 0)
4875 it->cmp_it.stop_pos = 0;
4876 }
4877
4878 CHECK_IT (it);
4879 }
4880
4881
4882 /* Compare two overlay_entry structures E1 and E2. Used as a
4883 comparison function for qsort in load_overlay_strings. Overlay
4884 strings for the same position are sorted so that
4885
4886 1. All after-strings come in front of before-strings, except
4887 when they come from the same overlay.
4888
4889 2. Within after-strings, strings are sorted so that overlay strings
4890 from overlays with higher priorities come first.
4891
4892 2. Within before-strings, strings are sorted so that overlay
4893 strings from overlays with higher priorities come last.
4894
4895 Value is analogous to strcmp. */
4896
4897
4898 static int
4899 compare_overlay_entries (e1, e2)
4900 void *e1, *e2;
4901 {
4902 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4903 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4904 int result;
4905
4906 if (entry1->after_string_p != entry2->after_string_p)
4907 {
4908 /* Let after-strings appear in front of before-strings if
4909 they come from different overlays. */
4910 if (EQ (entry1->overlay, entry2->overlay))
4911 result = entry1->after_string_p ? 1 : -1;
4912 else
4913 result = entry1->after_string_p ? -1 : 1;
4914 }
4915 else if (entry1->after_string_p)
4916 /* After-strings sorted in order of decreasing priority. */
4917 result = entry2->priority - entry1->priority;
4918 else
4919 /* Before-strings sorted in order of increasing priority. */
4920 result = entry1->priority - entry2->priority;
4921
4922 return result;
4923 }
4924
4925
4926 /* Load the vector IT->overlay_strings with overlay strings from IT's
4927 current buffer position, or from CHARPOS if that is > 0. Set
4928 IT->n_overlays to the total number of overlay strings found.
4929
4930 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4931 a time. On entry into load_overlay_strings,
4932 IT->current.overlay_string_index gives the number of overlay
4933 strings that have already been loaded by previous calls to this
4934 function.
4935
4936 IT->add_overlay_start contains an additional overlay start
4937 position to consider for taking overlay strings from, if non-zero.
4938 This position comes into play when the overlay has an `invisible'
4939 property, and both before and after-strings. When we've skipped to
4940 the end of the overlay, because of its `invisible' property, we
4941 nevertheless want its before-string to appear.
4942 IT->add_overlay_start will contain the overlay start position
4943 in this case.
4944
4945 Overlay strings are sorted so that after-string strings come in
4946 front of before-string strings. Within before and after-strings,
4947 strings are sorted by overlay priority. See also function
4948 compare_overlay_entries. */
4949
4950 static void
4951 load_overlay_strings (it, charpos)
4952 struct it *it;
4953 int charpos;
4954 {
4955 extern Lisp_Object Qwindow, Qpriority;
4956 Lisp_Object overlay, window, str, invisible;
4957 struct Lisp_Overlay *ov;
4958 int start, end;
4959 int size = 20;
4960 int n = 0, i, j, invis_p;
4961 struct overlay_entry *entries
4962 = (struct overlay_entry *) alloca (size * sizeof *entries);
4963
4964 if (charpos <= 0)
4965 charpos = IT_CHARPOS (*it);
4966
4967 /* Append the overlay string STRING of overlay OVERLAY to vector
4968 `entries' which has size `size' and currently contains `n'
4969 elements. AFTER_P non-zero means STRING is an after-string of
4970 OVERLAY. */
4971 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4972 do \
4973 { \
4974 Lisp_Object priority; \
4975 \
4976 if (n == size) \
4977 { \
4978 int new_size = 2 * size; \
4979 struct overlay_entry *old = entries; \
4980 entries = \
4981 (struct overlay_entry *) alloca (new_size \
4982 * sizeof *entries); \
4983 bcopy (old, entries, size * sizeof *entries); \
4984 size = new_size; \
4985 } \
4986 \
4987 entries[n].string = (STRING); \
4988 entries[n].overlay = (OVERLAY); \
4989 priority = Foverlay_get ((OVERLAY), Qpriority); \
4990 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4991 entries[n].after_string_p = (AFTER_P); \
4992 ++n; \
4993 } \
4994 while (0)
4995
4996 /* Process overlay before the overlay center. */
4997 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4998 {
4999 XSETMISC (overlay, ov);
5000 xassert (OVERLAYP (overlay));
5001 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5002 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5003
5004 if (end < charpos)
5005 break;
5006
5007 /* Skip this overlay if it doesn't start or end at IT's current
5008 position. */
5009 if (end != charpos && start != charpos)
5010 continue;
5011
5012 /* Skip this overlay if it doesn't apply to IT->w. */
5013 window = Foverlay_get (overlay, Qwindow);
5014 if (WINDOWP (window) && XWINDOW (window) != it->w)
5015 continue;
5016
5017 /* If the text ``under'' the overlay is invisible, both before-
5018 and after-strings from this overlay are visible; start and
5019 end position are indistinguishable. */
5020 invisible = Foverlay_get (overlay, Qinvisible);
5021 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5022
5023 /* If overlay has a non-empty before-string, record it. */
5024 if ((start == charpos || (end == charpos && invis_p))
5025 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5026 && SCHARS (str))
5027 RECORD_OVERLAY_STRING (overlay, str, 0);
5028
5029 /* If overlay has a non-empty after-string, record it. */
5030 if ((end == charpos || (start == charpos && invis_p))
5031 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5032 && SCHARS (str))
5033 RECORD_OVERLAY_STRING (overlay, str, 1);
5034 }
5035
5036 /* Process overlays after the overlay center. */
5037 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5038 {
5039 XSETMISC (overlay, ov);
5040 xassert (OVERLAYP (overlay));
5041 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5042 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5043
5044 if (start > charpos)
5045 break;
5046
5047 /* Skip this overlay if it doesn't start or end at IT's current
5048 position. */
5049 if (end != charpos && start != charpos)
5050 continue;
5051
5052 /* Skip this overlay if it doesn't apply to IT->w. */
5053 window = Foverlay_get (overlay, Qwindow);
5054 if (WINDOWP (window) && XWINDOW (window) != it->w)
5055 continue;
5056
5057 /* If the text ``under'' the overlay is invisible, it has a zero
5058 dimension, and both before- and after-strings apply. */
5059 invisible = Foverlay_get (overlay, Qinvisible);
5060 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5061
5062 /* If overlay has a non-empty before-string, record it. */
5063 if ((start == charpos || (end == charpos && invis_p))
5064 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5065 && SCHARS (str))
5066 RECORD_OVERLAY_STRING (overlay, str, 0);
5067
5068 /* If overlay has a non-empty after-string, record it. */
5069 if ((end == charpos || (start == charpos && invis_p))
5070 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5071 && SCHARS (str))
5072 RECORD_OVERLAY_STRING (overlay, str, 1);
5073 }
5074
5075 #undef RECORD_OVERLAY_STRING
5076
5077 /* Sort entries. */
5078 if (n > 1)
5079 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5080
5081 /* Record the total number of strings to process. */
5082 it->n_overlay_strings = n;
5083
5084 /* IT->current.overlay_string_index is the number of overlay strings
5085 that have already been consumed by IT. Copy some of the
5086 remaining overlay strings to IT->overlay_strings. */
5087 i = 0;
5088 j = it->current.overlay_string_index;
5089 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5090 {
5091 it->overlay_strings[i] = entries[j].string;
5092 it->string_overlays[i++] = entries[j++].overlay;
5093 }
5094
5095 CHECK_IT (it);
5096 }
5097
5098
5099 /* Get the first chunk of overlay strings at IT's current buffer
5100 position, or at CHARPOS if that is > 0. Value is non-zero if at
5101 least one overlay string was found. */
5102
5103 static int
5104 get_overlay_strings_1 (it, charpos, compute_stop_p)
5105 struct it *it;
5106 int charpos;
5107 int compute_stop_p;
5108 {
5109 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5110 process. This fills IT->overlay_strings with strings, and sets
5111 IT->n_overlay_strings to the total number of strings to process.
5112 IT->pos.overlay_string_index has to be set temporarily to zero
5113 because load_overlay_strings needs this; it must be set to -1
5114 when no overlay strings are found because a zero value would
5115 indicate a position in the first overlay string. */
5116 it->current.overlay_string_index = 0;
5117 load_overlay_strings (it, charpos);
5118
5119 /* If we found overlay strings, set up IT to deliver display
5120 elements from the first one. Otherwise set up IT to deliver
5121 from current_buffer. */
5122 if (it->n_overlay_strings)
5123 {
5124 /* Make sure we know settings in current_buffer, so that we can
5125 restore meaningful values when we're done with the overlay
5126 strings. */
5127 if (compute_stop_p)
5128 compute_stop_pos (it);
5129 xassert (it->face_id >= 0);
5130
5131 /* Save IT's settings. They are restored after all overlay
5132 strings have been processed. */
5133 xassert (!compute_stop_p || it->sp == 0);
5134
5135 /* When called from handle_stop, there might be an empty display
5136 string loaded. In that case, don't bother saving it. */
5137 if (!STRINGP (it->string) || SCHARS (it->string))
5138 push_it (it);
5139
5140 /* Set up IT to deliver display elements from the first overlay
5141 string. */
5142 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5143 it->string = it->overlay_strings[0];
5144 it->from_overlay = Qnil;
5145 it->stop_charpos = 0;
5146 xassert (STRINGP (it->string));
5147 it->end_charpos = SCHARS (it->string);
5148 it->multibyte_p = STRING_MULTIBYTE (it->string);
5149 it->method = GET_FROM_STRING;
5150 return 1;
5151 }
5152
5153 it->current.overlay_string_index = -1;
5154 return 0;
5155 }
5156
5157 static int
5158 get_overlay_strings (it, charpos)
5159 struct it *it;
5160 int charpos;
5161 {
5162 it->string = Qnil;
5163 it->method = GET_FROM_BUFFER;
5164
5165 (void) get_overlay_strings_1 (it, charpos, 1);
5166
5167 CHECK_IT (it);
5168
5169 /* Value is non-zero if we found at least one overlay string. */
5170 return STRINGP (it->string);
5171 }
5172
5173
5174 \f
5175 /***********************************************************************
5176 Saving and restoring state
5177 ***********************************************************************/
5178
5179 /* Save current settings of IT on IT->stack. Called, for example,
5180 before setting up IT for an overlay string, to be able to restore
5181 IT's settings to what they were after the overlay string has been
5182 processed. */
5183
5184 static void
5185 push_it (it)
5186 struct it *it;
5187 {
5188 struct iterator_stack_entry *p;
5189
5190 xassert (it->sp < IT_STACK_SIZE);
5191 p = it->stack + it->sp;
5192
5193 p->stop_charpos = it->stop_charpos;
5194 p->prev_stop = it->prev_stop;
5195 p->base_level_stop = it->base_level_stop;
5196 p->cmp_it = it->cmp_it;
5197 xassert (it->face_id >= 0);
5198 p->face_id = it->face_id;
5199 p->string = it->string;
5200 p->method = it->method;
5201 p->from_overlay = it->from_overlay;
5202 switch (p->method)
5203 {
5204 case GET_FROM_IMAGE:
5205 p->u.image.object = it->object;
5206 p->u.image.image_id = it->image_id;
5207 p->u.image.slice = it->slice;
5208 break;
5209 case GET_FROM_STRETCH:
5210 p->u.stretch.object = it->object;
5211 break;
5212 }
5213 p->position = it->position;
5214 p->current = it->current;
5215 p->end_charpos = it->end_charpos;
5216 p->string_nchars = it->string_nchars;
5217 p->area = it->area;
5218 p->multibyte_p = it->multibyte_p;
5219 p->avoid_cursor_p = it->avoid_cursor_p;
5220 p->space_width = it->space_width;
5221 p->font_height = it->font_height;
5222 p->voffset = it->voffset;
5223 p->string_from_display_prop_p = it->string_from_display_prop_p;
5224 p->display_ellipsis_p = 0;
5225 p->line_wrap = it->line_wrap;
5226 ++it->sp;
5227 }
5228
5229
5230 /* Restore IT's settings from IT->stack. Called, for example, when no
5231 more overlay strings must be processed, and we return to delivering
5232 display elements from a buffer, or when the end of a string from a
5233 `display' property is reached and we return to delivering display
5234 elements from an overlay string, or from a buffer. */
5235
5236 static void
5237 pop_it (it)
5238 struct it *it;
5239 {
5240 struct iterator_stack_entry *p;
5241
5242 xassert (it->sp > 0);
5243 --it->sp;
5244 p = it->stack + it->sp;
5245 it->stop_charpos = p->stop_charpos;
5246 it->prev_stop = p->prev_stop;
5247 it->base_level_stop = p->base_level_stop;
5248 it->cmp_it = p->cmp_it;
5249 it->face_id = p->face_id;
5250 it->current = p->current;
5251 it->position = p->position;
5252 it->string = p->string;
5253 it->from_overlay = p->from_overlay;
5254 if (NILP (it->string))
5255 SET_TEXT_POS (it->current.string_pos, -1, -1);
5256 it->method = p->method;
5257 switch (it->method)
5258 {
5259 case GET_FROM_IMAGE:
5260 it->image_id = p->u.image.image_id;
5261 it->object = p->u.image.object;
5262 it->slice = p->u.image.slice;
5263 break;
5264 case GET_FROM_STRETCH:
5265 it->object = p->u.comp.object;
5266 break;
5267 case GET_FROM_BUFFER:
5268 it->object = it->w->buffer;
5269 break;
5270 case GET_FROM_STRING:
5271 it->object = it->string;
5272 break;
5273 case GET_FROM_DISPLAY_VECTOR:
5274 if (it->s)
5275 it->method = GET_FROM_C_STRING;
5276 else if (STRINGP (it->string))
5277 it->method = GET_FROM_STRING;
5278 else
5279 {
5280 it->method = GET_FROM_BUFFER;
5281 it->object = it->w->buffer;
5282 }
5283 }
5284 it->end_charpos = p->end_charpos;
5285 it->string_nchars = p->string_nchars;
5286 it->area = p->area;
5287 it->multibyte_p = p->multibyte_p;
5288 it->avoid_cursor_p = p->avoid_cursor_p;
5289 it->space_width = p->space_width;
5290 it->font_height = p->font_height;
5291 it->voffset = p->voffset;
5292 it->string_from_display_prop_p = p->string_from_display_prop_p;
5293 it->line_wrap = p->line_wrap;
5294 }
5295
5296
5297 \f
5298 /***********************************************************************
5299 Moving over lines
5300 ***********************************************************************/
5301
5302 /* Set IT's current position to the previous line start. */
5303
5304 static void
5305 back_to_previous_line_start (it)
5306 struct it *it;
5307 {
5308 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5309 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5310 }
5311
5312
5313 /* Move IT to the next line start.
5314
5315 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5316 we skipped over part of the text (as opposed to moving the iterator
5317 continuously over the text). Otherwise, don't change the value
5318 of *SKIPPED_P.
5319
5320 Newlines may come from buffer text, overlay strings, or strings
5321 displayed via the `display' property. That's the reason we can't
5322 simply use find_next_newline_no_quit.
5323
5324 Note that this function may not skip over invisible text that is so
5325 because of text properties and immediately follows a newline. If
5326 it would, function reseat_at_next_visible_line_start, when called
5327 from set_iterator_to_next, would effectively make invisible
5328 characters following a newline part of the wrong glyph row, which
5329 leads to wrong cursor motion. */
5330
5331 static int
5332 forward_to_next_line_start (it, skipped_p)
5333 struct it *it;
5334 int *skipped_p;
5335 {
5336 int old_selective, newline_found_p, n;
5337 const int MAX_NEWLINE_DISTANCE = 500;
5338
5339 /* If already on a newline, just consume it to avoid unintended
5340 skipping over invisible text below. */
5341 if (it->what == IT_CHARACTER
5342 && it->c == '\n'
5343 && CHARPOS (it->position) == IT_CHARPOS (*it))
5344 {
5345 set_iterator_to_next (it, 0);
5346 it->c = 0;
5347 return 1;
5348 }
5349
5350 /* Don't handle selective display in the following. It's (a)
5351 unnecessary because it's done by the caller, and (b) leads to an
5352 infinite recursion because next_element_from_ellipsis indirectly
5353 calls this function. */
5354 old_selective = it->selective;
5355 it->selective = 0;
5356
5357 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5358 from buffer text. */
5359 for (n = newline_found_p = 0;
5360 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5361 n += STRINGP (it->string) ? 0 : 1)
5362 {
5363 if (!get_next_display_element (it))
5364 return 0;
5365 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5366 set_iterator_to_next (it, 0);
5367 }
5368
5369 /* If we didn't find a newline near enough, see if we can use a
5370 short-cut. */
5371 if (!newline_found_p)
5372 {
5373 int start = IT_CHARPOS (*it);
5374 int limit = find_next_newline_no_quit (start, 1);
5375 Lisp_Object pos;
5376
5377 xassert (!STRINGP (it->string));
5378
5379 /* If there isn't any `display' property in sight, and no
5380 overlays, we can just use the position of the newline in
5381 buffer text. */
5382 if (it->stop_charpos >= limit
5383 || ((pos = Fnext_single_property_change (make_number (start),
5384 Qdisplay,
5385 Qnil, make_number (limit)),
5386 NILP (pos))
5387 && next_overlay_change (start) == ZV))
5388 {
5389 IT_CHARPOS (*it) = limit;
5390 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5391 *skipped_p = newline_found_p = 1;
5392 }
5393 else
5394 {
5395 while (get_next_display_element (it)
5396 && !newline_found_p)
5397 {
5398 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5399 set_iterator_to_next (it, 0);
5400 }
5401 }
5402 }
5403
5404 it->selective = old_selective;
5405 return newline_found_p;
5406 }
5407
5408
5409 /* Set IT's current position to the previous visible line start. Skip
5410 invisible text that is so either due to text properties or due to
5411 selective display. Caution: this does not change IT->current_x and
5412 IT->hpos. */
5413
5414 static void
5415 back_to_previous_visible_line_start (it)
5416 struct it *it;
5417 {
5418 while (IT_CHARPOS (*it) > BEGV)
5419 {
5420 back_to_previous_line_start (it);
5421
5422 if (IT_CHARPOS (*it) <= BEGV)
5423 break;
5424
5425 /* If selective > 0, then lines indented more than its value are
5426 invisible. */
5427 if (it->selective > 0
5428 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5429 (double) it->selective)) /* iftc */
5430 continue;
5431
5432 /* Check the newline before point for invisibility. */
5433 {
5434 Lisp_Object prop;
5435 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5436 Qinvisible, it->window);
5437 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5438 continue;
5439 }
5440
5441 if (IT_CHARPOS (*it) <= BEGV)
5442 break;
5443
5444 {
5445 struct it it2;
5446 int pos;
5447 EMACS_INT beg, end;
5448 Lisp_Object val, overlay;
5449
5450 /* If newline is part of a composition, continue from start of composition */
5451 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5452 && beg < IT_CHARPOS (*it))
5453 goto replaced;
5454
5455 /* If newline is replaced by a display property, find start of overlay
5456 or interval and continue search from that point. */
5457 it2 = *it;
5458 pos = --IT_CHARPOS (it2);
5459 --IT_BYTEPOS (it2);
5460 it2.sp = 0;
5461 it2.string_from_display_prop_p = 0;
5462 if (handle_display_prop (&it2) == HANDLED_RETURN
5463 && !NILP (val = get_char_property_and_overlay
5464 (make_number (pos), Qdisplay, Qnil, &overlay))
5465 && (OVERLAYP (overlay)
5466 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5467 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5468 goto replaced;
5469
5470 /* Newline is not replaced by anything -- so we are done. */
5471 break;
5472
5473 replaced:
5474 if (beg < BEGV)
5475 beg = BEGV;
5476 IT_CHARPOS (*it) = beg;
5477 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5478 }
5479 }
5480
5481 it->continuation_lines_width = 0;
5482
5483 xassert (IT_CHARPOS (*it) >= BEGV);
5484 xassert (IT_CHARPOS (*it) == BEGV
5485 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5486 CHECK_IT (it);
5487 }
5488
5489
5490 /* Reseat iterator IT at the previous visible line start. Skip
5491 invisible text that is so either due to text properties or due to
5492 selective display. At the end, update IT's overlay information,
5493 face information etc. */
5494
5495 void
5496 reseat_at_previous_visible_line_start (it)
5497 struct it *it;
5498 {
5499 back_to_previous_visible_line_start (it);
5500 reseat (it, it->current.pos, 1);
5501 CHECK_IT (it);
5502 }
5503
5504
5505 /* Reseat iterator IT on the next visible line start in the current
5506 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5507 preceding the line start. Skip over invisible text that is so
5508 because of selective display. Compute faces, overlays etc at the
5509 new position. Note that this function does not skip over text that
5510 is invisible because of text properties. */
5511
5512 static void
5513 reseat_at_next_visible_line_start (it, on_newline_p)
5514 struct it *it;
5515 int on_newline_p;
5516 {
5517 int newline_found_p, skipped_p = 0;
5518
5519 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5520
5521 /* Skip over lines that are invisible because they are indented
5522 more than the value of IT->selective. */
5523 if (it->selective > 0)
5524 while (IT_CHARPOS (*it) < ZV
5525 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5526 (double) it->selective)) /* iftc */
5527 {
5528 xassert (IT_BYTEPOS (*it) == BEGV
5529 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5530 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5531 }
5532
5533 /* Position on the newline if that's what's requested. */
5534 if (on_newline_p && newline_found_p)
5535 {
5536 if (STRINGP (it->string))
5537 {
5538 if (IT_STRING_CHARPOS (*it) > 0)
5539 {
5540 --IT_STRING_CHARPOS (*it);
5541 --IT_STRING_BYTEPOS (*it);
5542 }
5543 }
5544 else if (IT_CHARPOS (*it) > BEGV)
5545 {
5546 --IT_CHARPOS (*it);
5547 --IT_BYTEPOS (*it);
5548 reseat (it, it->current.pos, 0);
5549 }
5550 }
5551 else if (skipped_p)
5552 reseat (it, it->current.pos, 0);
5553
5554 CHECK_IT (it);
5555 }
5556
5557
5558 \f
5559 /***********************************************************************
5560 Changing an iterator's position
5561 ***********************************************************************/
5562
5563 /* Change IT's current position to POS in current_buffer. If FORCE_P
5564 is non-zero, always check for text properties at the new position.
5565 Otherwise, text properties are only looked up if POS >=
5566 IT->check_charpos of a property. */
5567
5568 static void
5569 reseat (it, pos, force_p)
5570 struct it *it;
5571 struct text_pos pos;
5572 int force_p;
5573 {
5574 int original_pos = IT_CHARPOS (*it);
5575
5576 reseat_1 (it, pos, 0);
5577
5578 /* Determine where to check text properties. Avoid doing it
5579 where possible because text property lookup is very expensive. */
5580 if (force_p
5581 || CHARPOS (pos) > it->stop_charpos
5582 || CHARPOS (pos) < original_pos)
5583 {
5584 if (it->bidi_p)
5585 {
5586 /* For bidi iteration, we need to prime prev_stop and
5587 base_level_stop with our best estimations. */
5588 if (CHARPOS (pos) < it->prev_stop)
5589 {
5590 handle_stop_backwards (it, BEGV);
5591 if (CHARPOS (pos) < it->base_level_stop)
5592 it->base_level_stop = 0;
5593 }
5594 else if (CHARPOS (pos) > it->stop_charpos
5595 && it->stop_charpos >= BEGV)
5596 handle_stop_backwards (it, it->stop_charpos);
5597 else /* force_p */
5598 handle_stop (it);
5599 }
5600 else
5601 {
5602 handle_stop (it);
5603 it->prev_stop = it->base_level_stop = 0;
5604 }
5605
5606 }
5607
5608 CHECK_IT (it);
5609 }
5610
5611
5612 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5613 IT->stop_pos to POS, also. */
5614
5615 static void
5616 reseat_1 (it, pos, set_stop_p)
5617 struct it *it;
5618 struct text_pos pos;
5619 int set_stop_p;
5620 {
5621 /* Don't call this function when scanning a C string. */
5622 xassert (it->s == NULL);
5623
5624 /* POS must be a reasonable value. */
5625 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5626
5627 it->current.pos = it->position = pos;
5628 it->end_charpos = ZV;
5629 it->dpvec = NULL;
5630 it->current.dpvec_index = -1;
5631 it->current.overlay_string_index = -1;
5632 IT_STRING_CHARPOS (*it) = -1;
5633 IT_STRING_BYTEPOS (*it) = -1;
5634 it->string = Qnil;
5635 it->string_from_display_prop_p = 0;
5636 it->method = GET_FROM_BUFFER;
5637 it->object = it->w->buffer;
5638 it->area = TEXT_AREA;
5639 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5640 it->sp = 0;
5641 it->string_from_display_prop_p = 0;
5642 it->face_before_selective_p = 0;
5643 if (it->bidi_p)
5644 it->bidi_it.first_elt = 1;
5645
5646 if (set_stop_p)
5647 {
5648 it->stop_charpos = CHARPOS (pos);
5649 it->base_level_stop = CHARPOS (pos);
5650 }
5651 }
5652
5653
5654 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5655 If S is non-null, it is a C string to iterate over. Otherwise,
5656 STRING gives a Lisp string to iterate over.
5657
5658 If PRECISION > 0, don't return more then PRECISION number of
5659 characters from the string.
5660
5661 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5662 characters have been returned. FIELD_WIDTH < 0 means an infinite
5663 field width.
5664
5665 MULTIBYTE = 0 means disable processing of multibyte characters,
5666 MULTIBYTE > 0 means enable it,
5667 MULTIBYTE < 0 means use IT->multibyte_p.
5668
5669 IT must be initialized via a prior call to init_iterator before
5670 calling this function. */
5671
5672 static void
5673 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5674 struct it *it;
5675 unsigned char *s;
5676 Lisp_Object string;
5677 int charpos;
5678 int precision, field_width, multibyte;
5679 {
5680 /* No region in strings. */
5681 it->region_beg_charpos = it->region_end_charpos = -1;
5682
5683 /* No text property checks performed by default, but see below. */
5684 it->stop_charpos = -1;
5685
5686 /* Set iterator position and end position. */
5687 bzero (&it->current, sizeof it->current);
5688 it->current.overlay_string_index = -1;
5689 it->current.dpvec_index = -1;
5690 xassert (charpos >= 0);
5691
5692 /* If STRING is specified, use its multibyteness, otherwise use the
5693 setting of MULTIBYTE, if specified. */
5694 if (multibyte >= 0)
5695 it->multibyte_p = multibyte > 0;
5696
5697 if (s == NULL)
5698 {
5699 xassert (STRINGP (string));
5700 it->string = string;
5701 it->s = NULL;
5702 it->end_charpos = it->string_nchars = SCHARS (string);
5703 it->method = GET_FROM_STRING;
5704 it->current.string_pos = string_pos (charpos, string);
5705 }
5706 else
5707 {
5708 it->s = s;
5709 it->string = Qnil;
5710
5711 /* Note that we use IT->current.pos, not it->current.string_pos,
5712 for displaying C strings. */
5713 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5714 if (it->multibyte_p)
5715 {
5716 it->current.pos = c_string_pos (charpos, s, 1);
5717 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5718 }
5719 else
5720 {
5721 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5722 it->end_charpos = it->string_nchars = strlen (s);
5723 }
5724
5725 it->method = GET_FROM_C_STRING;
5726 }
5727
5728 /* PRECISION > 0 means don't return more than PRECISION characters
5729 from the string. */
5730 if (precision > 0 && it->end_charpos - charpos > precision)
5731 it->end_charpos = it->string_nchars = charpos + precision;
5732
5733 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5734 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5735 FIELD_WIDTH < 0 means infinite field width. This is useful for
5736 padding with `-' at the end of a mode line. */
5737 if (field_width < 0)
5738 field_width = INFINITY;
5739 if (field_width > it->end_charpos - charpos)
5740 it->end_charpos = charpos + field_width;
5741
5742 /* Use the standard display table for displaying strings. */
5743 if (DISP_TABLE_P (Vstandard_display_table))
5744 it->dp = XCHAR_TABLE (Vstandard_display_table);
5745
5746 it->stop_charpos = charpos;
5747 if (s == NULL && it->multibyte_p)
5748 {
5749 EMACS_INT endpos = SCHARS (it->string);
5750 if (endpos > it->end_charpos)
5751 endpos = it->end_charpos;
5752 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5753 it->string);
5754 }
5755 CHECK_IT (it);
5756 }
5757
5758
5759 \f
5760 /***********************************************************************
5761 Iteration
5762 ***********************************************************************/
5763
5764 /* Map enum it_method value to corresponding next_element_from_* function. */
5765
5766 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5767 {
5768 next_element_from_buffer,
5769 next_element_from_display_vector,
5770 next_element_from_string,
5771 next_element_from_c_string,
5772 next_element_from_image,
5773 next_element_from_stretch
5774 };
5775
5776 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5777
5778
5779 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5780 (possibly with the following characters). */
5781
5782 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5783 ((IT)->cmp_it.id >= 0 \
5784 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5785 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5786 END_CHARPOS, (IT)->w, \
5787 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5788 (IT)->string)))
5789
5790
5791 /* Load IT's display element fields with information about the next
5792 display element from the current position of IT. Value is zero if
5793 end of buffer (or C string) is reached. */
5794
5795 static struct frame *last_escape_glyph_frame = NULL;
5796 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5797 static int last_escape_glyph_merged_face_id = 0;
5798
5799 int
5800 get_next_display_element (it)
5801 struct it *it;
5802 {
5803 /* Non-zero means that we found a display element. Zero means that
5804 we hit the end of what we iterate over. Performance note: the
5805 function pointer `method' used here turns out to be faster than
5806 using a sequence of if-statements. */
5807 int success_p;
5808
5809 get_next:
5810 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5811
5812 if (it->what == IT_CHARACTER)
5813 {
5814 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5815 and only if (a) the resolved directionality of that character
5816 is R..." */
5817 /* FIXME: Do we need an exception for characters from display
5818 tables? */
5819 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5820 it->c = bidi_mirror_char (it->c);
5821 /* Map via display table or translate control characters.
5822 IT->c, IT->len etc. have been set to the next character by
5823 the function call above. If we have a display table, and it
5824 contains an entry for IT->c, translate it. Don't do this if
5825 IT->c itself comes from a display table, otherwise we could
5826 end up in an infinite recursion. (An alternative could be to
5827 count the recursion depth of this function and signal an
5828 error when a certain maximum depth is reached.) Is it worth
5829 it? */
5830 if (success_p && it->dpvec == NULL)
5831 {
5832 Lisp_Object dv;
5833 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5834 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5835 nbsp_or_shy = char_is_other;
5836 int decoded = it->c;
5837
5838 if (it->dp
5839 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5840 VECTORP (dv)))
5841 {
5842 struct Lisp_Vector *v = XVECTOR (dv);
5843
5844 /* Return the first character from the display table
5845 entry, if not empty. If empty, don't display the
5846 current character. */
5847 if (v->size)
5848 {
5849 it->dpvec_char_len = it->len;
5850 it->dpvec = v->contents;
5851 it->dpend = v->contents + v->size;
5852 it->current.dpvec_index = 0;
5853 it->dpvec_face_id = -1;
5854 it->saved_face_id = it->face_id;
5855 it->method = GET_FROM_DISPLAY_VECTOR;
5856 it->ellipsis_p = 0;
5857 }
5858 else
5859 {
5860 set_iterator_to_next (it, 0);
5861 }
5862 goto get_next;
5863 }
5864
5865 if (unibyte_display_via_language_environment
5866 && !ASCII_CHAR_P (it->c))
5867 decoded = DECODE_CHAR (unibyte, it->c);
5868
5869 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5870 {
5871 if (it->multibyte_p)
5872 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5873 : it->c == 0xAD ? char_is_soft_hyphen
5874 : char_is_other);
5875 else if (unibyte_display_via_language_environment)
5876 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5877 : decoded == 0xAD ? char_is_soft_hyphen
5878 : char_is_other);
5879 }
5880
5881 /* Translate control characters into `\003' or `^C' form.
5882 Control characters coming from a display table entry are
5883 currently not translated because we use IT->dpvec to hold
5884 the translation. This could easily be changed but I
5885 don't believe that it is worth doing.
5886
5887 If it->multibyte_p is nonzero, non-printable non-ASCII
5888 characters are also translated to octal form.
5889
5890 If it->multibyte_p is zero, eight-bit characters that
5891 don't have corresponding multibyte char code are also
5892 translated to octal form. */
5893 if ((it->c < ' '
5894 ? (it->area != TEXT_AREA
5895 /* In mode line, treat \n, \t like other crl chars. */
5896 || (it->c != '\t'
5897 && it->glyph_row
5898 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5899 || (it->c != '\n' && it->c != '\t'))
5900 : (nbsp_or_shy
5901 || (it->multibyte_p
5902 ? ! CHAR_PRINTABLE_P (it->c)
5903 : (! unibyte_display_via_language_environment
5904 ? it->c >= 0x80
5905 : (decoded >= 0x80 && decoded < 0xA0))))))
5906 {
5907 /* IT->c is a control character which must be displayed
5908 either as '\003' or as `^C' where the '\\' and '^'
5909 can be defined in the display table. Fill
5910 IT->ctl_chars with glyphs for what we have to
5911 display. Then, set IT->dpvec to these glyphs. */
5912 Lisp_Object gc;
5913 int ctl_len;
5914 int face_id, lface_id = 0 ;
5915 int escape_glyph;
5916
5917 /* Handle control characters with ^. */
5918
5919 if (it->c < 128 && it->ctl_arrow_p)
5920 {
5921 int g;
5922
5923 g = '^'; /* default glyph for Control */
5924 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5925 if (it->dp
5926 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5927 && GLYPH_CODE_CHAR_VALID_P (gc))
5928 {
5929 g = GLYPH_CODE_CHAR (gc);
5930 lface_id = GLYPH_CODE_FACE (gc);
5931 }
5932 if (lface_id)
5933 {
5934 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5935 }
5936 else if (it->f == last_escape_glyph_frame
5937 && it->face_id == last_escape_glyph_face_id)
5938 {
5939 face_id = last_escape_glyph_merged_face_id;
5940 }
5941 else
5942 {
5943 /* Merge the escape-glyph face into the current face. */
5944 face_id = merge_faces (it->f, Qescape_glyph, 0,
5945 it->face_id);
5946 last_escape_glyph_frame = it->f;
5947 last_escape_glyph_face_id = it->face_id;
5948 last_escape_glyph_merged_face_id = face_id;
5949 }
5950
5951 XSETINT (it->ctl_chars[0], g);
5952 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5953 ctl_len = 2;
5954 goto display_control;
5955 }
5956
5957 /* Handle non-break space in the mode where it only gets
5958 highlighting. */
5959
5960 if (EQ (Vnobreak_char_display, Qt)
5961 && nbsp_or_shy == char_is_nbsp)
5962 {
5963 /* Merge the no-break-space face into the current face. */
5964 face_id = merge_faces (it->f, Qnobreak_space, 0,
5965 it->face_id);
5966
5967 it->c = ' ';
5968 XSETINT (it->ctl_chars[0], ' ');
5969 ctl_len = 1;
5970 goto display_control;
5971 }
5972
5973 /* Handle sequences that start with the "escape glyph". */
5974
5975 /* the default escape glyph is \. */
5976 escape_glyph = '\\';
5977
5978 if (it->dp
5979 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5980 && GLYPH_CODE_CHAR_VALID_P (gc))
5981 {
5982 escape_glyph = GLYPH_CODE_CHAR (gc);
5983 lface_id = GLYPH_CODE_FACE (gc);
5984 }
5985 if (lface_id)
5986 {
5987 /* The display table specified a face.
5988 Merge it into face_id and also into escape_glyph. */
5989 face_id = merge_faces (it->f, Qt, lface_id,
5990 it->face_id);
5991 }
5992 else if (it->f == last_escape_glyph_frame
5993 && it->face_id == last_escape_glyph_face_id)
5994 {
5995 face_id = last_escape_glyph_merged_face_id;
5996 }
5997 else
5998 {
5999 /* Merge the escape-glyph face into the current face. */
6000 face_id = merge_faces (it->f, Qescape_glyph, 0,
6001 it->face_id);
6002 last_escape_glyph_frame = it->f;
6003 last_escape_glyph_face_id = it->face_id;
6004 last_escape_glyph_merged_face_id = face_id;
6005 }
6006
6007 /* Handle soft hyphens in the mode where they only get
6008 highlighting. */
6009
6010 if (EQ (Vnobreak_char_display, Qt)
6011 && nbsp_or_shy == char_is_soft_hyphen)
6012 {
6013 it->c = '-';
6014 XSETINT (it->ctl_chars[0], '-');
6015 ctl_len = 1;
6016 goto display_control;
6017 }
6018
6019 /* Handle non-break space and soft hyphen
6020 with the escape glyph. */
6021
6022 if (nbsp_or_shy)
6023 {
6024 XSETINT (it->ctl_chars[0], escape_glyph);
6025 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6026 XSETINT (it->ctl_chars[1], it->c);
6027 ctl_len = 2;
6028 goto display_control;
6029 }
6030
6031 {
6032 unsigned char str[MAX_MULTIBYTE_LENGTH];
6033 int len;
6034 int i;
6035
6036 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
6037 if (CHAR_BYTE8_P (it->c))
6038 {
6039 str[0] = CHAR_TO_BYTE8 (it->c);
6040 len = 1;
6041 }
6042 else if (it->c < 256)
6043 {
6044 str[0] = it->c;
6045 len = 1;
6046 }
6047 else
6048 {
6049 /* It's an invalid character, which shouldn't
6050 happen actually, but due to bugs it may
6051 happen. Let's print the char as is, there's
6052 not much meaningful we can do with it. */
6053 str[0] = it->c;
6054 str[1] = it->c >> 8;
6055 str[2] = it->c >> 16;
6056 str[3] = it->c >> 24;
6057 len = 4;
6058 }
6059
6060 for (i = 0; i < len; i++)
6061 {
6062 int g;
6063 XSETINT (it->ctl_chars[i * 4], escape_glyph);
6064 /* Insert three more glyphs into IT->ctl_chars for
6065 the octal display of the character. */
6066 g = ((str[i] >> 6) & 7) + '0';
6067 XSETINT (it->ctl_chars[i * 4 + 1], g);
6068 g = ((str[i] >> 3) & 7) + '0';
6069 XSETINT (it->ctl_chars[i * 4 + 2], g);
6070 g = (str[i] & 7) + '0';
6071 XSETINT (it->ctl_chars[i * 4 + 3], g);
6072 }
6073 ctl_len = len * 4;
6074 }
6075
6076 display_control:
6077 /* Set up IT->dpvec and return first character from it. */
6078 it->dpvec_char_len = it->len;
6079 it->dpvec = it->ctl_chars;
6080 it->dpend = it->dpvec + ctl_len;
6081 it->current.dpvec_index = 0;
6082 it->dpvec_face_id = face_id;
6083 it->saved_face_id = it->face_id;
6084 it->method = GET_FROM_DISPLAY_VECTOR;
6085 it->ellipsis_p = 0;
6086 goto get_next;
6087 }
6088 }
6089 }
6090
6091 #ifdef HAVE_WINDOW_SYSTEM
6092 /* Adjust face id for a multibyte character. There are no multibyte
6093 character in unibyte text. */
6094 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6095 && it->multibyte_p
6096 && success_p
6097 && FRAME_WINDOW_P (it->f))
6098 {
6099 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6100
6101 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6102 {
6103 /* Automatic composition with glyph-string. */
6104 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6105
6106 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6107 }
6108 else
6109 {
6110 int pos = (it->s ? -1
6111 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6112 : IT_CHARPOS (*it));
6113
6114 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6115 }
6116 }
6117 #endif
6118
6119 /* Is this character the last one of a run of characters with
6120 box? If yes, set IT->end_of_box_run_p to 1. */
6121 if (it->face_box_p
6122 && it->s == NULL)
6123 {
6124 if (it->method == GET_FROM_STRING && it->sp)
6125 {
6126 int face_id = underlying_face_id (it);
6127 struct face *face = FACE_FROM_ID (it->f, face_id);
6128
6129 if (face)
6130 {
6131 if (face->box == FACE_NO_BOX)
6132 {
6133 /* If the box comes from face properties in a
6134 display string, check faces in that string. */
6135 int string_face_id = face_after_it_pos (it);
6136 it->end_of_box_run_p
6137 = (FACE_FROM_ID (it->f, string_face_id)->box
6138 == FACE_NO_BOX);
6139 }
6140 /* Otherwise, the box comes from the underlying face.
6141 If this is the last string character displayed, check
6142 the next buffer location. */
6143 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6144 && (it->current.overlay_string_index
6145 == it->n_overlay_strings - 1))
6146 {
6147 EMACS_INT ignore;
6148 int next_face_id;
6149 struct text_pos pos = it->current.pos;
6150 INC_TEXT_POS (pos, it->multibyte_p);
6151
6152 next_face_id = face_at_buffer_position
6153 (it->w, CHARPOS (pos), it->region_beg_charpos,
6154 it->region_end_charpos, &ignore,
6155 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6156 -1);
6157 it->end_of_box_run_p
6158 = (FACE_FROM_ID (it->f, next_face_id)->box
6159 == FACE_NO_BOX);
6160 }
6161 }
6162 }
6163 else
6164 {
6165 int face_id = face_after_it_pos (it);
6166 it->end_of_box_run_p
6167 = (face_id != it->face_id
6168 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6169 }
6170 }
6171
6172 /* Value is 0 if end of buffer or string reached. */
6173 return success_p;
6174 }
6175
6176
6177 /* Move IT to the next display element.
6178
6179 RESEAT_P non-zero means if called on a newline in buffer text,
6180 skip to the next visible line start.
6181
6182 Functions get_next_display_element and set_iterator_to_next are
6183 separate because I find this arrangement easier to handle than a
6184 get_next_display_element function that also increments IT's
6185 position. The way it is we can first look at an iterator's current
6186 display element, decide whether it fits on a line, and if it does,
6187 increment the iterator position. The other way around we probably
6188 would either need a flag indicating whether the iterator has to be
6189 incremented the next time, or we would have to implement a
6190 decrement position function which would not be easy to write. */
6191
6192 void
6193 set_iterator_to_next (it, reseat_p)
6194 struct it *it;
6195 int reseat_p;
6196 {
6197 /* Reset flags indicating start and end of a sequence of characters
6198 with box. Reset them at the start of this function because
6199 moving the iterator to a new position might set them. */
6200 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6201
6202 switch (it->method)
6203 {
6204 case GET_FROM_BUFFER:
6205 /* The current display element of IT is a character from
6206 current_buffer. Advance in the buffer, and maybe skip over
6207 invisible lines that are so because of selective display. */
6208 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6209 reseat_at_next_visible_line_start (it, 0);
6210 else if (it->cmp_it.id >= 0)
6211 {
6212 IT_CHARPOS (*it) += it->cmp_it.nchars;
6213 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6214 if (it->cmp_it.to < it->cmp_it.nglyphs)
6215 it->cmp_it.from = it->cmp_it.to;
6216 else
6217 {
6218 it->cmp_it.id = -1;
6219 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6220 IT_BYTEPOS (*it), it->stop_charpos,
6221 Qnil);
6222 }
6223 }
6224 else
6225 {
6226 xassert (it->len != 0);
6227
6228 if (!it->bidi_p)
6229 {
6230 IT_BYTEPOS (*it) += it->len;
6231 IT_CHARPOS (*it) += 1;
6232 }
6233 else
6234 {
6235 /* If this is a new paragraph, determine its base
6236 direction (a.k.a. its base embedding level). */
6237 if (it->bidi_it.new_paragraph)
6238 {
6239 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6240 if (it->glyph_row)
6241 {
6242 if (it->bidi_it.paragraph_dir == R2L)
6243 it->glyph_row->reversed_p = 1;
6244 else
6245 it->glyph_row->reversed_p = 0;
6246 }
6247 }
6248 bidi_get_next_char_visually (&it->bidi_it);
6249 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6250 IT_CHARPOS (*it) = it->bidi_it.charpos;
6251 }
6252 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6253 }
6254 break;
6255
6256 case GET_FROM_C_STRING:
6257 /* Current display element of IT is from a C string. */
6258 IT_BYTEPOS (*it) += it->len;
6259 IT_CHARPOS (*it) += 1;
6260 break;
6261
6262 case GET_FROM_DISPLAY_VECTOR:
6263 /* Current display element of IT is from a display table entry.
6264 Advance in the display table definition. Reset it to null if
6265 end reached, and continue with characters from buffers/
6266 strings. */
6267 ++it->current.dpvec_index;
6268
6269 /* Restore face of the iterator to what they were before the
6270 display vector entry (these entries may contain faces). */
6271 it->face_id = it->saved_face_id;
6272
6273 if (it->dpvec + it->current.dpvec_index == it->dpend)
6274 {
6275 int recheck_faces = it->ellipsis_p;
6276
6277 if (it->s)
6278 it->method = GET_FROM_C_STRING;
6279 else if (STRINGP (it->string))
6280 it->method = GET_FROM_STRING;
6281 else
6282 {
6283 it->method = GET_FROM_BUFFER;
6284 it->object = it->w->buffer;
6285 }
6286
6287 it->dpvec = NULL;
6288 it->current.dpvec_index = -1;
6289
6290 /* Skip over characters which were displayed via IT->dpvec. */
6291 if (it->dpvec_char_len < 0)
6292 reseat_at_next_visible_line_start (it, 1);
6293 else if (it->dpvec_char_len > 0)
6294 {
6295 if (it->method == GET_FROM_STRING
6296 && it->n_overlay_strings > 0)
6297 it->ignore_overlay_strings_at_pos_p = 1;
6298 it->len = it->dpvec_char_len;
6299 set_iterator_to_next (it, reseat_p);
6300 }
6301
6302 /* Maybe recheck faces after display vector */
6303 if (recheck_faces)
6304 it->stop_charpos = IT_CHARPOS (*it);
6305 }
6306 break;
6307
6308 case GET_FROM_STRING:
6309 /* Current display element is a character from a Lisp string. */
6310 xassert (it->s == NULL && STRINGP (it->string));
6311 if (it->cmp_it.id >= 0)
6312 {
6313 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6314 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6315 if (it->cmp_it.to < it->cmp_it.nglyphs)
6316 it->cmp_it.from = it->cmp_it.to;
6317 else
6318 {
6319 it->cmp_it.id = -1;
6320 composition_compute_stop_pos (&it->cmp_it,
6321 IT_STRING_CHARPOS (*it),
6322 IT_STRING_BYTEPOS (*it),
6323 it->stop_charpos, it->string);
6324 }
6325 }
6326 else
6327 {
6328 IT_STRING_BYTEPOS (*it) += it->len;
6329 IT_STRING_CHARPOS (*it) += 1;
6330 }
6331
6332 consider_string_end:
6333
6334 if (it->current.overlay_string_index >= 0)
6335 {
6336 /* IT->string is an overlay string. Advance to the
6337 next, if there is one. */
6338 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6339 {
6340 it->ellipsis_p = 0;
6341 next_overlay_string (it);
6342 if (it->ellipsis_p)
6343 setup_for_ellipsis (it, 0);
6344 }
6345 }
6346 else
6347 {
6348 /* IT->string is not an overlay string. If we reached
6349 its end, and there is something on IT->stack, proceed
6350 with what is on the stack. This can be either another
6351 string, this time an overlay string, or a buffer. */
6352 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6353 && it->sp > 0)
6354 {
6355 pop_it (it);
6356 if (it->method == GET_FROM_STRING)
6357 goto consider_string_end;
6358 }
6359 }
6360 break;
6361
6362 case GET_FROM_IMAGE:
6363 case GET_FROM_STRETCH:
6364 /* The position etc with which we have to proceed are on
6365 the stack. The position may be at the end of a string,
6366 if the `display' property takes up the whole string. */
6367 xassert (it->sp > 0);
6368 pop_it (it);
6369 if (it->method == GET_FROM_STRING)
6370 goto consider_string_end;
6371 break;
6372
6373 default:
6374 /* There are no other methods defined, so this should be a bug. */
6375 abort ();
6376 }
6377
6378 xassert (it->method != GET_FROM_STRING
6379 || (STRINGP (it->string)
6380 && IT_STRING_CHARPOS (*it) >= 0));
6381 }
6382
6383 /* Load IT's display element fields with information about the next
6384 display element which comes from a display table entry or from the
6385 result of translating a control character to one of the forms `^C'
6386 or `\003'.
6387
6388 IT->dpvec holds the glyphs to return as characters.
6389 IT->saved_face_id holds the face id before the display vector--it
6390 is restored into IT->face_id in set_iterator_to_next. */
6391
6392 static int
6393 next_element_from_display_vector (it)
6394 struct it *it;
6395 {
6396 Lisp_Object gc;
6397
6398 /* Precondition. */
6399 xassert (it->dpvec && it->current.dpvec_index >= 0);
6400
6401 it->face_id = it->saved_face_id;
6402
6403 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6404 That seemed totally bogus - so I changed it... */
6405 gc = it->dpvec[it->current.dpvec_index];
6406
6407 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6408 {
6409 it->c = GLYPH_CODE_CHAR (gc);
6410 it->len = CHAR_BYTES (it->c);
6411
6412 /* The entry may contain a face id to use. Such a face id is
6413 the id of a Lisp face, not a realized face. A face id of
6414 zero means no face is specified. */
6415 if (it->dpvec_face_id >= 0)
6416 it->face_id = it->dpvec_face_id;
6417 else
6418 {
6419 int lface_id = GLYPH_CODE_FACE (gc);
6420 if (lface_id > 0)
6421 it->face_id = merge_faces (it->f, Qt, lface_id,
6422 it->saved_face_id);
6423 }
6424 }
6425 else
6426 /* Display table entry is invalid. Return a space. */
6427 it->c = ' ', it->len = 1;
6428
6429 /* Don't change position and object of the iterator here. They are
6430 still the values of the character that had this display table
6431 entry or was translated, and that's what we want. */
6432 it->what = IT_CHARACTER;
6433 return 1;
6434 }
6435
6436
6437 /* Load IT with the next display element from Lisp string IT->string.
6438 IT->current.string_pos is the current position within the string.
6439 If IT->current.overlay_string_index >= 0, the Lisp string is an
6440 overlay string. */
6441
6442 static int
6443 next_element_from_string (it)
6444 struct it *it;
6445 {
6446 struct text_pos position;
6447
6448 xassert (STRINGP (it->string));
6449 xassert (IT_STRING_CHARPOS (*it) >= 0);
6450 position = it->current.string_pos;
6451
6452 /* Time to check for invisible text? */
6453 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6454 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6455 {
6456 handle_stop (it);
6457
6458 /* Since a handler may have changed IT->method, we must
6459 recurse here. */
6460 return GET_NEXT_DISPLAY_ELEMENT (it);
6461 }
6462
6463 if (it->current.overlay_string_index >= 0)
6464 {
6465 /* Get the next character from an overlay string. In overlay
6466 strings, There is no field width or padding with spaces to
6467 do. */
6468 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6469 {
6470 it->what = IT_EOB;
6471 return 0;
6472 }
6473 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6474 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6475 && next_element_from_composition (it))
6476 {
6477 return 1;
6478 }
6479 else if (STRING_MULTIBYTE (it->string))
6480 {
6481 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6482 const unsigned char *s = (SDATA (it->string)
6483 + IT_STRING_BYTEPOS (*it));
6484 it->c = string_char_and_length (s, &it->len);
6485 }
6486 else
6487 {
6488 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6489 it->len = 1;
6490 }
6491 }
6492 else
6493 {
6494 /* Get the next character from a Lisp string that is not an
6495 overlay string. Such strings come from the mode line, for
6496 example. We may have to pad with spaces, or truncate the
6497 string. See also next_element_from_c_string. */
6498 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6499 {
6500 it->what = IT_EOB;
6501 return 0;
6502 }
6503 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6504 {
6505 /* Pad with spaces. */
6506 it->c = ' ', it->len = 1;
6507 CHARPOS (position) = BYTEPOS (position) = -1;
6508 }
6509 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6510 IT_STRING_BYTEPOS (*it), it->string_nchars)
6511 && next_element_from_composition (it))
6512 {
6513 return 1;
6514 }
6515 else if (STRING_MULTIBYTE (it->string))
6516 {
6517 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6518 const unsigned char *s = (SDATA (it->string)
6519 + IT_STRING_BYTEPOS (*it));
6520 it->c = string_char_and_length (s, &it->len);
6521 }
6522 else
6523 {
6524 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6525 it->len = 1;
6526 }
6527 }
6528
6529 /* Record what we have and where it came from. */
6530 it->what = IT_CHARACTER;
6531 it->object = it->string;
6532 it->position = position;
6533 return 1;
6534 }
6535
6536
6537 /* Load IT with next display element from C string IT->s.
6538 IT->string_nchars is the maximum number of characters to return
6539 from the string. IT->end_charpos may be greater than
6540 IT->string_nchars when this function is called, in which case we
6541 may have to return padding spaces. Value is zero if end of string
6542 reached, including padding spaces. */
6543
6544 static int
6545 next_element_from_c_string (it)
6546 struct it *it;
6547 {
6548 int success_p = 1;
6549
6550 xassert (it->s);
6551 it->what = IT_CHARACTER;
6552 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6553 it->object = Qnil;
6554
6555 /* IT's position can be greater IT->string_nchars in case a field
6556 width or precision has been specified when the iterator was
6557 initialized. */
6558 if (IT_CHARPOS (*it) >= it->end_charpos)
6559 {
6560 /* End of the game. */
6561 it->what = IT_EOB;
6562 success_p = 0;
6563 }
6564 else if (IT_CHARPOS (*it) >= it->string_nchars)
6565 {
6566 /* Pad with spaces. */
6567 it->c = ' ', it->len = 1;
6568 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6569 }
6570 else if (it->multibyte_p)
6571 {
6572 /* Implementation note: The calls to strlen apparently aren't a
6573 performance problem because there is no noticeable performance
6574 difference between Emacs running in unibyte or multibyte mode. */
6575 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6576 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6577 }
6578 else
6579 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6580
6581 return success_p;
6582 }
6583
6584
6585 /* Set up IT to return characters from an ellipsis, if appropriate.
6586 The definition of the ellipsis glyphs may come from a display table
6587 entry. This function fills IT with the first glyph from the
6588 ellipsis if an ellipsis is to be displayed. */
6589
6590 static int
6591 next_element_from_ellipsis (it)
6592 struct it *it;
6593 {
6594 if (it->selective_display_ellipsis_p)
6595 setup_for_ellipsis (it, it->len);
6596 else
6597 {
6598 /* The face at the current position may be different from the
6599 face we find after the invisible text. Remember what it
6600 was in IT->saved_face_id, and signal that it's there by
6601 setting face_before_selective_p. */
6602 it->saved_face_id = it->face_id;
6603 it->method = GET_FROM_BUFFER;
6604 it->object = it->w->buffer;
6605 reseat_at_next_visible_line_start (it, 1);
6606 it->face_before_selective_p = 1;
6607 }
6608
6609 return GET_NEXT_DISPLAY_ELEMENT (it);
6610 }
6611
6612
6613 /* Deliver an image display element. The iterator IT is already
6614 filled with image information (done in handle_display_prop). Value
6615 is always 1. */
6616
6617
6618 static int
6619 next_element_from_image (it)
6620 struct it *it;
6621 {
6622 it->what = IT_IMAGE;
6623 return 1;
6624 }
6625
6626
6627 /* Fill iterator IT with next display element from a stretch glyph
6628 property. IT->object is the value of the text property. Value is
6629 always 1. */
6630
6631 static int
6632 next_element_from_stretch (it)
6633 struct it *it;
6634 {
6635 it->what = IT_STRETCH;
6636 return 1;
6637 }
6638
6639 /* Scan forward from CHARPOS in the current buffer, until we find a
6640 stop position > current IT's position. Then handle the stop
6641 position before that. This is called when we bump into a stop
6642 position while reordering bidirectional text. */
6643
6644 static void
6645 handle_stop_backwards (it, charpos)
6646 struct it *it;
6647 EMACS_INT charpos;
6648 {
6649 EMACS_INT where_we_are = IT_CHARPOS (*it);
6650 struct display_pos save_current = it->current;
6651 struct text_pos save_position = it->position;
6652 struct text_pos pos1;
6653 EMACS_INT next_stop;
6654
6655 /* Scan in strict logical order. */
6656 it->bidi_p = 0;
6657 do
6658 {
6659 it->prev_stop = charpos;
6660 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6661 reseat_1 (it, pos1, 0);
6662 compute_stop_pos (it);
6663 /* We must advance forward, right? */
6664 if (it->stop_charpos <= it->prev_stop)
6665 abort ();
6666 charpos = it->stop_charpos;
6667 }
6668 while (charpos <= where_we_are);
6669
6670 next_stop = it->stop_charpos;
6671 it->stop_charpos = it->prev_stop;
6672 it->bidi_p = 1;
6673 it->current = save_current;
6674 it->position = save_position;
6675 handle_stop (it);
6676 it->stop_charpos = next_stop;
6677 }
6678
6679 /* Load IT with the next display element from current_buffer. Value
6680 is zero if end of buffer reached. IT->stop_charpos is the next
6681 position at which to stop and check for text properties or buffer
6682 end. */
6683
6684 static int
6685 next_element_from_buffer (it)
6686 struct it *it;
6687 {
6688 int success_p = 1;
6689
6690 xassert (IT_CHARPOS (*it) >= BEGV);
6691
6692 /* With bidi reordering, the character to display might not be the
6693 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6694 we were reseat()ed to a new buffer position, which is potentially
6695 a different paragraph. */
6696 if (it->bidi_p && it->bidi_it.first_elt)
6697 {
6698 it->bidi_it.charpos = IT_CHARPOS (*it);
6699 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6700 /* If we are at the beginning of a line, we can produce the next
6701 element right away. */
6702 if (it->bidi_it.bytepos == BEGV_BYTE
6703 /* FIXME: Should support all Unicode line separators. */
6704 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6705 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6706 {
6707 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6708 /* If the paragraph base direction is R2L, its glyphs should
6709 be reversed. */
6710 if (it->glyph_row)
6711 {
6712 if (it->bidi_it.paragraph_dir == R2L)
6713 it->glyph_row->reversed_p = 1;
6714 else
6715 it->glyph_row->reversed_p = 0;
6716 }
6717 bidi_get_next_char_visually (&it->bidi_it);
6718 }
6719 else
6720 {
6721 int orig_bytepos = IT_BYTEPOS (*it);
6722
6723 /* We need to prime the bidi iterator starting at the line's
6724 beginning, before we will be able to produce the next
6725 element. */
6726 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6727 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6728 it->bidi_it.charpos = IT_CHARPOS (*it);
6729 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6730 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6731 if (it->glyph_row)
6732 {
6733 if (it->bidi_it.paragraph_dir == R2L)
6734 it->glyph_row->reversed_p = 1;
6735 else
6736 it->glyph_row->reversed_p = 0;
6737 }
6738 do
6739 {
6740 /* Now return to buffer position where we were asked to
6741 get the next display element, and produce that. */
6742 bidi_get_next_char_visually (&it->bidi_it);
6743 }
6744 while (it->bidi_it.bytepos != orig_bytepos
6745 && it->bidi_it.bytepos < ZV_BYTE);
6746 }
6747
6748 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6749 /* Adjust IT's position information to where we ended up. */
6750 IT_CHARPOS (*it) = it->bidi_it.charpos;
6751 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6752 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6753 }
6754
6755 if (IT_CHARPOS (*it) >= it->stop_charpos)
6756 {
6757 if (IT_CHARPOS (*it) >= it->end_charpos)
6758 {
6759 int overlay_strings_follow_p;
6760
6761 /* End of the game, except when overlay strings follow that
6762 haven't been returned yet. */
6763 if (it->overlay_strings_at_end_processed_p)
6764 overlay_strings_follow_p = 0;
6765 else
6766 {
6767 it->overlay_strings_at_end_processed_p = 1;
6768 overlay_strings_follow_p = get_overlay_strings (it, 0);
6769 }
6770
6771 if (overlay_strings_follow_p)
6772 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6773 else
6774 {
6775 it->what = IT_EOB;
6776 it->position = it->current.pos;
6777 success_p = 0;
6778 }
6779 }
6780 else if (!(!it->bidi_p
6781 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6782 || IT_CHARPOS (*it) == it->stop_charpos))
6783 {
6784 /* With bidi non-linear iteration, we could find ourselves
6785 far beyond the last computed stop_charpos, with several
6786 other stop positions in between that we missed. Scan
6787 them all now, in buffer's logical order, until we find
6788 and handle the last stop_charpos that precedes our
6789 current position. */
6790 handle_stop_backwards (it, it->stop_charpos);
6791 return GET_NEXT_DISPLAY_ELEMENT (it);
6792 }
6793 else
6794 {
6795 if (it->bidi_p)
6796 {
6797 /* Take note of the stop position we just moved across,
6798 for when we will move back across it. */
6799 it->prev_stop = it->stop_charpos;
6800 /* If we are at base paragraph embedding level, take
6801 note of the last stop position seen at this
6802 level. */
6803 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6804 it->base_level_stop = it->stop_charpos;
6805 }
6806 handle_stop (it);
6807 return GET_NEXT_DISPLAY_ELEMENT (it);
6808 }
6809 }
6810 else if (it->bidi_p
6811 /* We can sometimes back up for reasons that have nothing
6812 to do with bidi reordering. E.g., compositions. The
6813 code below is only needed when we are above the base
6814 embedding level, so test for that explicitly. */
6815 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6816 && IT_CHARPOS (*it) < it->prev_stop)
6817 {
6818 if (it->base_level_stop <= 0)
6819 it->base_level_stop = BEGV;
6820 if (IT_CHARPOS (*it) < it->base_level_stop)
6821 abort ();
6822 handle_stop_backwards (it, it->base_level_stop);
6823 return GET_NEXT_DISPLAY_ELEMENT (it);
6824 }
6825 else
6826 {
6827 /* No face changes, overlays etc. in sight, so just return a
6828 character from current_buffer. */
6829 unsigned char *p;
6830
6831 /* Maybe run the redisplay end trigger hook. Performance note:
6832 This doesn't seem to cost measurable time. */
6833 if (it->redisplay_end_trigger_charpos
6834 && it->glyph_row
6835 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6836 run_redisplay_end_trigger_hook (it);
6837
6838 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6839 it->end_charpos)
6840 && next_element_from_composition (it))
6841 {
6842 return 1;
6843 }
6844
6845 /* Get the next character, maybe multibyte. */
6846 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6847 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6848 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6849 else
6850 it->c = *p, it->len = 1;
6851
6852 /* Record what we have and where it came from. */
6853 it->what = IT_CHARACTER;
6854 it->object = it->w->buffer;
6855 it->position = it->current.pos;
6856
6857 /* Normally we return the character found above, except when we
6858 really want to return an ellipsis for selective display. */
6859 if (it->selective)
6860 {
6861 if (it->c == '\n')
6862 {
6863 /* A value of selective > 0 means hide lines indented more
6864 than that number of columns. */
6865 if (it->selective > 0
6866 && IT_CHARPOS (*it) + 1 < ZV
6867 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6868 IT_BYTEPOS (*it) + 1,
6869 (double) it->selective)) /* iftc */
6870 {
6871 success_p = next_element_from_ellipsis (it);
6872 it->dpvec_char_len = -1;
6873 }
6874 }
6875 else if (it->c == '\r' && it->selective == -1)
6876 {
6877 /* A value of selective == -1 means that everything from the
6878 CR to the end of the line is invisible, with maybe an
6879 ellipsis displayed for it. */
6880 success_p = next_element_from_ellipsis (it);
6881 it->dpvec_char_len = -1;
6882 }
6883 }
6884 }
6885
6886 /* Value is zero if end of buffer reached. */
6887 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6888 return success_p;
6889 }
6890
6891
6892 /* Run the redisplay end trigger hook for IT. */
6893
6894 static void
6895 run_redisplay_end_trigger_hook (it)
6896 struct it *it;
6897 {
6898 Lisp_Object args[3];
6899
6900 /* IT->glyph_row should be non-null, i.e. we should be actually
6901 displaying something, or otherwise we should not run the hook. */
6902 xassert (it->glyph_row);
6903
6904 /* Set up hook arguments. */
6905 args[0] = Qredisplay_end_trigger_functions;
6906 args[1] = it->window;
6907 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6908 it->redisplay_end_trigger_charpos = 0;
6909
6910 /* Since we are *trying* to run these functions, don't try to run
6911 them again, even if they get an error. */
6912 it->w->redisplay_end_trigger = Qnil;
6913 Frun_hook_with_args (3, args);
6914
6915 /* Notice if it changed the face of the character we are on. */
6916 handle_face_prop (it);
6917 }
6918
6919
6920 /* Deliver a composition display element. Unlike the other
6921 next_element_from_XXX, this function is not registered in the array
6922 get_next_element[]. It is called from next_element_from_buffer and
6923 next_element_from_string when necessary. */
6924
6925 static int
6926 next_element_from_composition (it)
6927 struct it *it;
6928 {
6929 it->what = IT_COMPOSITION;
6930 it->len = it->cmp_it.nbytes;
6931 if (STRINGP (it->string))
6932 {
6933 if (it->c < 0)
6934 {
6935 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6936 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6937 return 0;
6938 }
6939 it->position = it->current.string_pos;
6940 it->object = it->string;
6941 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6942 IT_STRING_BYTEPOS (*it), it->string);
6943 }
6944 else
6945 {
6946 if (it->c < 0)
6947 {
6948 IT_CHARPOS (*it) += it->cmp_it.nchars;
6949 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6950 return 0;
6951 }
6952 it->position = it->current.pos;
6953 it->object = it->w->buffer;
6954 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6955 IT_BYTEPOS (*it), Qnil);
6956 }
6957 return 1;
6958 }
6959
6960
6961 \f
6962 /***********************************************************************
6963 Moving an iterator without producing glyphs
6964 ***********************************************************************/
6965
6966 /* Check if iterator is at a position corresponding to a valid buffer
6967 position after some move_it_ call. */
6968
6969 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6970 ((it)->method == GET_FROM_STRING \
6971 ? IT_STRING_CHARPOS (*it) == 0 \
6972 : 1)
6973
6974
6975 /* Move iterator IT to a specified buffer or X position within one
6976 line on the display without producing glyphs.
6977
6978 OP should be a bit mask including some or all of these bits:
6979 MOVE_TO_X: Stop upon reaching x-position TO_X.
6980 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6981 Regardless of OP's value, stop upon reaching the end of the display line.
6982
6983 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6984 This means, in particular, that TO_X includes window's horizontal
6985 scroll amount.
6986
6987 The return value has several possible values that
6988 say what condition caused the scan to stop:
6989
6990 MOVE_POS_MATCH_OR_ZV
6991 - when TO_POS or ZV was reached.
6992
6993 MOVE_X_REACHED
6994 -when TO_X was reached before TO_POS or ZV were reached.
6995
6996 MOVE_LINE_CONTINUED
6997 - when we reached the end of the display area and the line must
6998 be continued.
6999
7000 MOVE_LINE_TRUNCATED
7001 - when we reached the end of the display area and the line is
7002 truncated.
7003
7004 MOVE_NEWLINE_OR_CR
7005 - when we stopped at a line end, i.e. a newline or a CR and selective
7006 display is on. */
7007
7008 static enum move_it_result
7009 move_it_in_display_line_to (struct it *it,
7010 EMACS_INT to_charpos, int to_x,
7011 enum move_operation_enum op)
7012 {
7013 enum move_it_result result = MOVE_UNDEFINED;
7014 struct glyph_row *saved_glyph_row;
7015 struct it wrap_it, atpos_it, atx_it;
7016 int may_wrap = 0;
7017 enum it_method prev_method = it->method;
7018 EMACS_INT prev_pos = IT_CHARPOS (*it);
7019
7020 /* Don't produce glyphs in produce_glyphs. */
7021 saved_glyph_row = it->glyph_row;
7022 it->glyph_row = NULL;
7023
7024 /* Use wrap_it to save a copy of IT wherever a word wrap could
7025 occur. Use atpos_it to save a copy of IT at the desired buffer
7026 position, if found, so that we can scan ahead and check if the
7027 word later overshoots the window edge. Use atx_it similarly, for
7028 pixel positions. */
7029 wrap_it.sp = -1;
7030 atpos_it.sp = -1;
7031 atx_it.sp = -1;
7032
7033 #define BUFFER_POS_REACHED_P() \
7034 ((op & MOVE_TO_POS) != 0 \
7035 && BUFFERP (it->object) \
7036 && IT_CHARPOS (*it) == to_charpos \
7037 && (it->method == GET_FROM_BUFFER \
7038 || (it->method == GET_FROM_DISPLAY_VECTOR \
7039 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7040
7041 /* If there's a line-/wrap-prefix, handle it. */
7042 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7043 && it->current_y < it->last_visible_y)
7044 handle_line_prefix (it);
7045
7046 while (1)
7047 {
7048 int x, i, ascent = 0, descent = 0;
7049
7050 /* Utility macro to reset an iterator with x, ascent, and descent. */
7051 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7052 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7053 (IT)->max_descent = descent)
7054
7055 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7056 glyph). */
7057 if ((op & MOVE_TO_POS) != 0
7058 && BUFFERP (it->object)
7059 && it->method == GET_FROM_BUFFER
7060 && (prev_method == GET_FROM_IMAGE
7061 || prev_method == GET_FROM_STRETCH)
7062 /* Passed TO_CHARPOS from left to right. */
7063 && ((prev_pos < to_charpos
7064 && IT_CHARPOS (*it) > to_charpos)
7065 /* Passed TO_CHARPOS from right to left. */
7066 || (prev_pos > to_charpos)
7067 && IT_CHARPOS (*it) < to_charpos))
7068 {
7069 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7070 {
7071 result = MOVE_POS_MATCH_OR_ZV;
7072 break;
7073 }
7074 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7075 /* If wrap_it is valid, the current position might be in a
7076 word that is wrapped. So, save the iterator in
7077 atpos_it and continue to see if wrapping happens. */
7078 atpos_it = *it;
7079 }
7080
7081 prev_method = it->method;
7082 if (it->method == GET_FROM_BUFFER)
7083 prev_pos = IT_CHARPOS (*it);
7084 /* Stop when ZV reached.
7085 We used to stop here when TO_CHARPOS reached as well, but that is
7086 too soon if this glyph does not fit on this line. So we handle it
7087 explicitly below. */
7088 if (!get_next_display_element (it))
7089 {
7090 result = MOVE_POS_MATCH_OR_ZV;
7091 break;
7092 }
7093
7094 if (it->line_wrap == TRUNCATE)
7095 {
7096 if (BUFFER_POS_REACHED_P ())
7097 {
7098 result = MOVE_POS_MATCH_OR_ZV;
7099 break;
7100 }
7101 }
7102 else
7103 {
7104 if (it->line_wrap == WORD_WRAP)
7105 {
7106 if (IT_DISPLAYING_WHITESPACE (it))
7107 may_wrap = 1;
7108 else if (may_wrap)
7109 {
7110 /* We have reached a glyph that follows one or more
7111 whitespace characters. If the position is
7112 already found, we are done. */
7113 if (atpos_it.sp >= 0)
7114 {
7115 *it = atpos_it;
7116 result = MOVE_POS_MATCH_OR_ZV;
7117 goto done;
7118 }
7119 if (atx_it.sp >= 0)
7120 {
7121 *it = atx_it;
7122 result = MOVE_X_REACHED;
7123 goto done;
7124 }
7125 /* Otherwise, we can wrap here. */
7126 wrap_it = *it;
7127 may_wrap = 0;
7128 }
7129 }
7130 }
7131
7132 /* Remember the line height for the current line, in case
7133 the next element doesn't fit on the line. */
7134 ascent = it->max_ascent;
7135 descent = it->max_descent;
7136
7137 /* The call to produce_glyphs will get the metrics of the
7138 display element IT is loaded with. Record the x-position
7139 before this display element, in case it doesn't fit on the
7140 line. */
7141 x = it->current_x;
7142
7143 PRODUCE_GLYPHS (it);
7144
7145 if (it->area != TEXT_AREA)
7146 {
7147 set_iterator_to_next (it, 1);
7148 continue;
7149 }
7150
7151 /* The number of glyphs we get back in IT->nglyphs will normally
7152 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7153 character on a terminal frame, or (iii) a line end. For the
7154 second case, IT->nglyphs - 1 padding glyphs will be present.
7155 (On X frames, there is only one glyph produced for a
7156 composite character.)
7157
7158 The behavior implemented below means, for continuation lines,
7159 that as many spaces of a TAB as fit on the current line are
7160 displayed there. For terminal frames, as many glyphs of a
7161 multi-glyph character are displayed in the current line, too.
7162 This is what the old redisplay code did, and we keep it that
7163 way. Under X, the whole shape of a complex character must
7164 fit on the line or it will be completely displayed in the
7165 next line.
7166
7167 Note that both for tabs and padding glyphs, all glyphs have
7168 the same width. */
7169 if (it->nglyphs)
7170 {
7171 /* More than one glyph or glyph doesn't fit on line. All
7172 glyphs have the same width. */
7173 int single_glyph_width = it->pixel_width / it->nglyphs;
7174 int new_x;
7175 int x_before_this_char = x;
7176 int hpos_before_this_char = it->hpos;
7177
7178 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7179 {
7180 new_x = x + single_glyph_width;
7181
7182 /* We want to leave anything reaching TO_X to the caller. */
7183 if ((op & MOVE_TO_X) && new_x > to_x)
7184 {
7185 if (BUFFER_POS_REACHED_P ())
7186 {
7187 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7188 goto buffer_pos_reached;
7189 if (atpos_it.sp < 0)
7190 {
7191 atpos_it = *it;
7192 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7193 }
7194 }
7195 else
7196 {
7197 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7198 {
7199 it->current_x = x;
7200 result = MOVE_X_REACHED;
7201 break;
7202 }
7203 if (atx_it.sp < 0)
7204 {
7205 atx_it = *it;
7206 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7207 }
7208 }
7209 }
7210
7211 if (/* Lines are continued. */
7212 it->line_wrap != TRUNCATE
7213 && (/* And glyph doesn't fit on the line. */
7214 new_x > it->last_visible_x
7215 /* Or it fits exactly and we're on a window
7216 system frame. */
7217 || (new_x == it->last_visible_x
7218 && FRAME_WINDOW_P (it->f))))
7219 {
7220 if (/* IT->hpos == 0 means the very first glyph
7221 doesn't fit on the line, e.g. a wide image. */
7222 it->hpos == 0
7223 || (new_x == it->last_visible_x
7224 && FRAME_WINDOW_P (it->f)))
7225 {
7226 ++it->hpos;
7227 it->current_x = new_x;
7228
7229 /* The character's last glyph just barely fits
7230 in this row. */
7231 if (i == it->nglyphs - 1)
7232 {
7233 /* If this is the destination position,
7234 return a position *before* it in this row,
7235 now that we know it fits in this row. */
7236 if (BUFFER_POS_REACHED_P ())
7237 {
7238 if (it->line_wrap != WORD_WRAP
7239 || wrap_it.sp < 0)
7240 {
7241 it->hpos = hpos_before_this_char;
7242 it->current_x = x_before_this_char;
7243 result = MOVE_POS_MATCH_OR_ZV;
7244 break;
7245 }
7246 if (it->line_wrap == WORD_WRAP
7247 && atpos_it.sp < 0)
7248 {
7249 atpos_it = *it;
7250 atpos_it.current_x = x_before_this_char;
7251 atpos_it.hpos = hpos_before_this_char;
7252 }
7253 }
7254
7255 set_iterator_to_next (it, 1);
7256 /* On graphical terminals, newlines may
7257 "overflow" into the fringe if
7258 overflow-newline-into-fringe is non-nil.
7259 On text-only terminals, newlines may
7260 overflow into the last glyph on the
7261 display line.*/
7262 if (!FRAME_WINDOW_P (it->f)
7263 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7264 {
7265 if (!get_next_display_element (it))
7266 {
7267 result = MOVE_POS_MATCH_OR_ZV;
7268 break;
7269 }
7270 if (BUFFER_POS_REACHED_P ())
7271 {
7272 if (ITERATOR_AT_END_OF_LINE_P (it))
7273 result = MOVE_POS_MATCH_OR_ZV;
7274 else
7275 result = MOVE_LINE_CONTINUED;
7276 break;
7277 }
7278 if (ITERATOR_AT_END_OF_LINE_P (it))
7279 {
7280 result = MOVE_NEWLINE_OR_CR;
7281 break;
7282 }
7283 }
7284 }
7285 }
7286 else
7287 IT_RESET_X_ASCENT_DESCENT (it);
7288
7289 if (wrap_it.sp >= 0)
7290 {
7291 *it = wrap_it;
7292 atpos_it.sp = -1;
7293 atx_it.sp = -1;
7294 }
7295
7296 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7297 IT_CHARPOS (*it)));
7298 result = MOVE_LINE_CONTINUED;
7299 break;
7300 }
7301
7302 if (BUFFER_POS_REACHED_P ())
7303 {
7304 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7305 goto buffer_pos_reached;
7306 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7307 {
7308 atpos_it = *it;
7309 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7310 }
7311 }
7312
7313 if (new_x > it->first_visible_x)
7314 {
7315 /* Glyph is visible. Increment number of glyphs that
7316 would be displayed. */
7317 ++it->hpos;
7318 }
7319 }
7320
7321 if (result != MOVE_UNDEFINED)
7322 break;
7323 }
7324 else if (BUFFER_POS_REACHED_P ())
7325 {
7326 buffer_pos_reached:
7327 IT_RESET_X_ASCENT_DESCENT (it);
7328 result = MOVE_POS_MATCH_OR_ZV;
7329 break;
7330 }
7331 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7332 {
7333 /* Stop when TO_X specified and reached. This check is
7334 necessary here because of lines consisting of a line end,
7335 only. The line end will not produce any glyphs and we
7336 would never get MOVE_X_REACHED. */
7337 xassert (it->nglyphs == 0);
7338 result = MOVE_X_REACHED;
7339 break;
7340 }
7341
7342 /* Is this a line end? If yes, we're done. */
7343 if (ITERATOR_AT_END_OF_LINE_P (it))
7344 {
7345 result = MOVE_NEWLINE_OR_CR;
7346 break;
7347 }
7348
7349 if (it->method == GET_FROM_BUFFER)
7350 prev_pos = IT_CHARPOS (*it);
7351 /* The current display element has been consumed. Advance
7352 to the next. */
7353 set_iterator_to_next (it, 1);
7354
7355 /* Stop if lines are truncated and IT's current x-position is
7356 past the right edge of the window now. */
7357 if (it->line_wrap == TRUNCATE
7358 && it->current_x >= it->last_visible_x)
7359 {
7360 if (!FRAME_WINDOW_P (it->f)
7361 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7362 {
7363 if (!get_next_display_element (it)
7364 || BUFFER_POS_REACHED_P ())
7365 {
7366 result = MOVE_POS_MATCH_OR_ZV;
7367 break;
7368 }
7369 if (ITERATOR_AT_END_OF_LINE_P (it))
7370 {
7371 result = MOVE_NEWLINE_OR_CR;
7372 break;
7373 }
7374 }
7375 result = MOVE_LINE_TRUNCATED;
7376 break;
7377 }
7378 #undef IT_RESET_X_ASCENT_DESCENT
7379 }
7380
7381 #undef BUFFER_POS_REACHED_P
7382
7383 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7384 restore the saved iterator. */
7385 if (atpos_it.sp >= 0)
7386 *it = atpos_it;
7387 else if (atx_it.sp >= 0)
7388 *it = atx_it;
7389
7390 done:
7391
7392 /* Restore the iterator settings altered at the beginning of this
7393 function. */
7394 it->glyph_row = saved_glyph_row;
7395 return result;
7396 }
7397
7398 /* For external use. */
7399 void
7400 move_it_in_display_line (struct it *it,
7401 EMACS_INT to_charpos, int to_x,
7402 enum move_operation_enum op)
7403 {
7404 if (it->line_wrap == WORD_WRAP
7405 && (op & MOVE_TO_X))
7406 {
7407 struct it save_it = *it;
7408 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7409 /* When word-wrap is on, TO_X may lie past the end
7410 of a wrapped line. Then it->current is the
7411 character on the next line, so backtrack to the
7412 space before the wrap point. */
7413 if (skip == MOVE_LINE_CONTINUED)
7414 {
7415 int prev_x = max (it->current_x - 1, 0);
7416 *it = save_it;
7417 move_it_in_display_line_to
7418 (it, -1, prev_x, MOVE_TO_X);
7419 }
7420 }
7421 else
7422 move_it_in_display_line_to (it, to_charpos, to_x, op);
7423 }
7424
7425
7426 /* Move IT forward until it satisfies one or more of the criteria in
7427 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7428
7429 OP is a bit-mask that specifies where to stop, and in particular,
7430 which of those four position arguments makes a difference. See the
7431 description of enum move_operation_enum.
7432
7433 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7434 screen line, this function will set IT to the next position >
7435 TO_CHARPOS. */
7436
7437 void
7438 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7439 struct it *it;
7440 int to_charpos, to_x, to_y, to_vpos;
7441 int op;
7442 {
7443 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7444 int line_height, line_start_x = 0, reached = 0;
7445
7446 for (;;)
7447 {
7448 if (op & MOVE_TO_VPOS)
7449 {
7450 /* If no TO_CHARPOS and no TO_X specified, stop at the
7451 start of the line TO_VPOS. */
7452 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7453 {
7454 if (it->vpos == to_vpos)
7455 {
7456 reached = 1;
7457 break;
7458 }
7459 else
7460 skip = move_it_in_display_line_to (it, -1, -1, 0);
7461 }
7462 else
7463 {
7464 /* TO_VPOS >= 0 means stop at TO_X in the line at
7465 TO_VPOS, or at TO_POS, whichever comes first. */
7466 if (it->vpos == to_vpos)
7467 {
7468 reached = 2;
7469 break;
7470 }
7471
7472 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7473
7474 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7475 {
7476 reached = 3;
7477 break;
7478 }
7479 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7480 {
7481 /* We have reached TO_X but not in the line we want. */
7482 skip = move_it_in_display_line_to (it, to_charpos,
7483 -1, MOVE_TO_POS);
7484 if (skip == MOVE_POS_MATCH_OR_ZV)
7485 {
7486 reached = 4;
7487 break;
7488 }
7489 }
7490 }
7491 }
7492 else if (op & MOVE_TO_Y)
7493 {
7494 struct it it_backup;
7495
7496 if (it->line_wrap == WORD_WRAP)
7497 it_backup = *it;
7498
7499 /* TO_Y specified means stop at TO_X in the line containing
7500 TO_Y---or at TO_CHARPOS if this is reached first. The
7501 problem is that we can't really tell whether the line
7502 contains TO_Y before we have completely scanned it, and
7503 this may skip past TO_X. What we do is to first scan to
7504 TO_X.
7505
7506 If TO_X is not specified, use a TO_X of zero. The reason
7507 is to make the outcome of this function more predictable.
7508 If we didn't use TO_X == 0, we would stop at the end of
7509 the line which is probably not what a caller would expect
7510 to happen. */
7511 skip = move_it_in_display_line_to
7512 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7513 (MOVE_TO_X | (op & MOVE_TO_POS)));
7514
7515 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7516 if (skip == MOVE_POS_MATCH_OR_ZV)
7517 reached = 5;
7518 else if (skip == MOVE_X_REACHED)
7519 {
7520 /* If TO_X was reached, we want to know whether TO_Y is
7521 in the line. We know this is the case if the already
7522 scanned glyphs make the line tall enough. Otherwise,
7523 we must check by scanning the rest of the line. */
7524 line_height = it->max_ascent + it->max_descent;
7525 if (to_y >= it->current_y
7526 && to_y < it->current_y + line_height)
7527 {
7528 reached = 6;
7529 break;
7530 }
7531 it_backup = *it;
7532 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7533 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7534 op & MOVE_TO_POS);
7535 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7536 line_height = it->max_ascent + it->max_descent;
7537 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7538
7539 if (to_y >= it->current_y
7540 && to_y < it->current_y + line_height)
7541 {
7542 /* If TO_Y is in this line and TO_X was reached
7543 above, we scanned too far. We have to restore
7544 IT's settings to the ones before skipping. */
7545 *it = it_backup;
7546 reached = 6;
7547 }
7548 else
7549 {
7550 skip = skip2;
7551 if (skip == MOVE_POS_MATCH_OR_ZV)
7552 reached = 7;
7553 }
7554 }
7555 else
7556 {
7557 /* Check whether TO_Y is in this line. */
7558 line_height = it->max_ascent + it->max_descent;
7559 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7560
7561 if (to_y >= it->current_y
7562 && to_y < it->current_y + line_height)
7563 {
7564 /* When word-wrap is on, TO_X may lie past the end
7565 of a wrapped line. Then it->current is the
7566 character on the next line, so backtrack to the
7567 space before the wrap point. */
7568 if (skip == MOVE_LINE_CONTINUED
7569 && it->line_wrap == WORD_WRAP)
7570 {
7571 int prev_x = max (it->current_x - 1, 0);
7572 *it = it_backup;
7573 skip = move_it_in_display_line_to
7574 (it, -1, prev_x, MOVE_TO_X);
7575 }
7576 reached = 6;
7577 }
7578 }
7579
7580 if (reached)
7581 break;
7582 }
7583 else if (BUFFERP (it->object)
7584 && (it->method == GET_FROM_BUFFER
7585 || it->method == GET_FROM_STRETCH)
7586 && IT_CHARPOS (*it) >= to_charpos)
7587 skip = MOVE_POS_MATCH_OR_ZV;
7588 else
7589 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7590
7591 switch (skip)
7592 {
7593 case MOVE_POS_MATCH_OR_ZV:
7594 reached = 8;
7595 goto out;
7596
7597 case MOVE_NEWLINE_OR_CR:
7598 set_iterator_to_next (it, 1);
7599 it->continuation_lines_width = 0;
7600 break;
7601
7602 case MOVE_LINE_TRUNCATED:
7603 it->continuation_lines_width = 0;
7604 reseat_at_next_visible_line_start (it, 0);
7605 if ((op & MOVE_TO_POS) != 0
7606 && IT_CHARPOS (*it) > to_charpos)
7607 {
7608 reached = 9;
7609 goto out;
7610 }
7611 break;
7612
7613 case MOVE_LINE_CONTINUED:
7614 /* For continued lines ending in a tab, some of the glyphs
7615 associated with the tab are displayed on the current
7616 line. Since it->current_x does not include these glyphs,
7617 we use it->last_visible_x instead. */
7618 if (it->c == '\t')
7619 {
7620 it->continuation_lines_width += it->last_visible_x;
7621 /* When moving by vpos, ensure that the iterator really
7622 advances to the next line (bug#847, bug#969). Fixme:
7623 do we need to do this in other circumstances? */
7624 if (it->current_x != it->last_visible_x
7625 && (op & MOVE_TO_VPOS)
7626 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7627 {
7628 line_start_x = it->current_x + it->pixel_width
7629 - it->last_visible_x;
7630 set_iterator_to_next (it, 0);
7631 }
7632 }
7633 else
7634 it->continuation_lines_width += it->current_x;
7635 break;
7636
7637 default:
7638 abort ();
7639 }
7640
7641 /* Reset/increment for the next run. */
7642 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7643 it->current_x = line_start_x;
7644 line_start_x = 0;
7645 it->hpos = 0;
7646 it->current_y += it->max_ascent + it->max_descent;
7647 ++it->vpos;
7648 last_height = it->max_ascent + it->max_descent;
7649 last_max_ascent = it->max_ascent;
7650 it->max_ascent = it->max_descent = 0;
7651 }
7652
7653 out:
7654
7655 /* On text terminals, we may stop at the end of a line in the middle
7656 of a multi-character glyph. If the glyph itself is continued,
7657 i.e. it is actually displayed on the next line, don't treat this
7658 stopping point as valid; move to the next line instead (unless
7659 that brings us offscreen). */
7660 if (!FRAME_WINDOW_P (it->f)
7661 && op & MOVE_TO_POS
7662 && IT_CHARPOS (*it) == to_charpos
7663 && it->what == IT_CHARACTER
7664 && it->nglyphs > 1
7665 && it->line_wrap == WINDOW_WRAP
7666 && it->current_x == it->last_visible_x - 1
7667 && it->c != '\n'
7668 && it->c != '\t'
7669 && it->vpos < XFASTINT (it->w->window_end_vpos))
7670 {
7671 it->continuation_lines_width += it->current_x;
7672 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7673 it->current_y += it->max_ascent + it->max_descent;
7674 ++it->vpos;
7675 last_height = it->max_ascent + it->max_descent;
7676 last_max_ascent = it->max_ascent;
7677 }
7678
7679 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7680 }
7681
7682
7683 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7684
7685 If DY > 0, move IT backward at least that many pixels. DY = 0
7686 means move IT backward to the preceding line start or BEGV. This
7687 function may move over more than DY pixels if IT->current_y - DY
7688 ends up in the middle of a line; in this case IT->current_y will be
7689 set to the top of the line moved to. */
7690
7691 void
7692 move_it_vertically_backward (it, dy)
7693 struct it *it;
7694 int dy;
7695 {
7696 int nlines, h;
7697 struct it it2, it3;
7698 int start_pos;
7699
7700 move_further_back:
7701 xassert (dy >= 0);
7702
7703 start_pos = IT_CHARPOS (*it);
7704
7705 /* Estimate how many newlines we must move back. */
7706 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7707
7708 /* Set the iterator's position that many lines back. */
7709 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7710 back_to_previous_visible_line_start (it);
7711
7712 /* Reseat the iterator here. When moving backward, we don't want
7713 reseat to skip forward over invisible text, set up the iterator
7714 to deliver from overlay strings at the new position etc. So,
7715 use reseat_1 here. */
7716 reseat_1 (it, it->current.pos, 1);
7717
7718 /* We are now surely at a line start. */
7719 it->current_x = it->hpos = 0;
7720 it->continuation_lines_width = 0;
7721
7722 /* Move forward and see what y-distance we moved. First move to the
7723 start of the next line so that we get its height. We need this
7724 height to be able to tell whether we reached the specified
7725 y-distance. */
7726 it2 = *it;
7727 it2.max_ascent = it2.max_descent = 0;
7728 do
7729 {
7730 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7731 MOVE_TO_POS | MOVE_TO_VPOS);
7732 }
7733 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7734 xassert (IT_CHARPOS (*it) >= BEGV);
7735 it3 = it2;
7736
7737 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7738 xassert (IT_CHARPOS (*it) >= BEGV);
7739 /* H is the actual vertical distance from the position in *IT
7740 and the starting position. */
7741 h = it2.current_y - it->current_y;
7742 /* NLINES is the distance in number of lines. */
7743 nlines = it2.vpos - it->vpos;
7744
7745 /* Correct IT's y and vpos position
7746 so that they are relative to the starting point. */
7747 it->vpos -= nlines;
7748 it->current_y -= h;
7749
7750 if (dy == 0)
7751 {
7752 /* DY == 0 means move to the start of the screen line. The
7753 value of nlines is > 0 if continuation lines were involved. */
7754 if (nlines > 0)
7755 move_it_by_lines (it, nlines, 1);
7756 }
7757 else
7758 {
7759 /* The y-position we try to reach, relative to *IT.
7760 Note that H has been subtracted in front of the if-statement. */
7761 int target_y = it->current_y + h - dy;
7762 int y0 = it3.current_y;
7763 int y1 = line_bottom_y (&it3);
7764 int line_height = y1 - y0;
7765
7766 /* If we did not reach target_y, try to move further backward if
7767 we can. If we moved too far backward, try to move forward. */
7768 if (target_y < it->current_y
7769 /* This is heuristic. In a window that's 3 lines high, with
7770 a line height of 13 pixels each, recentering with point
7771 on the bottom line will try to move -39/2 = 19 pixels
7772 backward. Try to avoid moving into the first line. */
7773 && (it->current_y - target_y
7774 > min (window_box_height (it->w), line_height * 2 / 3))
7775 && IT_CHARPOS (*it) > BEGV)
7776 {
7777 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7778 target_y - it->current_y));
7779 dy = it->current_y - target_y;
7780 goto move_further_back;
7781 }
7782 else if (target_y >= it->current_y + line_height
7783 && IT_CHARPOS (*it) < ZV)
7784 {
7785 /* Should move forward by at least one line, maybe more.
7786
7787 Note: Calling move_it_by_lines can be expensive on
7788 terminal frames, where compute_motion is used (via
7789 vmotion) to do the job, when there are very long lines
7790 and truncate-lines is nil. That's the reason for
7791 treating terminal frames specially here. */
7792
7793 if (!FRAME_WINDOW_P (it->f))
7794 move_it_vertically (it, target_y - (it->current_y + line_height));
7795 else
7796 {
7797 do
7798 {
7799 move_it_by_lines (it, 1, 1);
7800 }
7801 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7802 }
7803 }
7804 }
7805 }
7806
7807
7808 /* Move IT by a specified amount of pixel lines DY. DY negative means
7809 move backwards. DY = 0 means move to start of screen line. At the
7810 end, IT will be on the start of a screen line. */
7811
7812 void
7813 move_it_vertically (it, dy)
7814 struct it *it;
7815 int dy;
7816 {
7817 if (dy <= 0)
7818 move_it_vertically_backward (it, -dy);
7819 else
7820 {
7821 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7822 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7823 MOVE_TO_POS | MOVE_TO_Y);
7824 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7825
7826 /* If buffer ends in ZV without a newline, move to the start of
7827 the line to satisfy the post-condition. */
7828 if (IT_CHARPOS (*it) == ZV
7829 && ZV > BEGV
7830 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7831 move_it_by_lines (it, 0, 0);
7832 }
7833 }
7834
7835
7836 /* Move iterator IT past the end of the text line it is in. */
7837
7838 void
7839 move_it_past_eol (it)
7840 struct it *it;
7841 {
7842 enum move_it_result rc;
7843
7844 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7845 if (rc == MOVE_NEWLINE_OR_CR)
7846 set_iterator_to_next (it, 0);
7847 }
7848
7849
7850 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7851 negative means move up. DVPOS == 0 means move to the start of the
7852 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7853 NEED_Y_P is zero, IT->current_y will be left unchanged.
7854
7855 Further optimization ideas: If we would know that IT->f doesn't use
7856 a face with proportional font, we could be faster for
7857 truncate-lines nil. */
7858
7859 void
7860 move_it_by_lines (it, dvpos, need_y_p)
7861 struct it *it;
7862 int dvpos, need_y_p;
7863 {
7864 struct position pos;
7865
7866 /* The commented-out optimization uses vmotion on terminals. This
7867 gives bad results, because elements like it->what, on which
7868 callers such as pos_visible_p rely, aren't updated. */
7869 /* if (!FRAME_WINDOW_P (it->f))
7870 {
7871 struct text_pos textpos;
7872
7873 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7874 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7875 reseat (it, textpos, 1);
7876 it->vpos += pos.vpos;
7877 it->current_y += pos.vpos;
7878 }
7879 else */
7880
7881 if (dvpos == 0)
7882 {
7883 /* DVPOS == 0 means move to the start of the screen line. */
7884 move_it_vertically_backward (it, 0);
7885 xassert (it->current_x == 0 && it->hpos == 0);
7886 /* Let next call to line_bottom_y calculate real line height */
7887 last_height = 0;
7888 }
7889 else if (dvpos > 0)
7890 {
7891 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7892 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7893 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7894 }
7895 else
7896 {
7897 struct it it2;
7898 int start_charpos, i;
7899
7900 /* Start at the beginning of the screen line containing IT's
7901 position. This may actually move vertically backwards,
7902 in case of overlays, so adjust dvpos accordingly. */
7903 dvpos += it->vpos;
7904 move_it_vertically_backward (it, 0);
7905 dvpos -= it->vpos;
7906
7907 /* Go back -DVPOS visible lines and reseat the iterator there. */
7908 start_charpos = IT_CHARPOS (*it);
7909 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7910 back_to_previous_visible_line_start (it);
7911 reseat (it, it->current.pos, 1);
7912
7913 /* Move further back if we end up in a string or an image. */
7914 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7915 {
7916 /* First try to move to start of display line. */
7917 dvpos += it->vpos;
7918 move_it_vertically_backward (it, 0);
7919 dvpos -= it->vpos;
7920 if (IT_POS_VALID_AFTER_MOVE_P (it))
7921 break;
7922 /* If start of line is still in string or image,
7923 move further back. */
7924 back_to_previous_visible_line_start (it);
7925 reseat (it, it->current.pos, 1);
7926 dvpos--;
7927 }
7928
7929 it->current_x = it->hpos = 0;
7930
7931 /* Above call may have moved too far if continuation lines
7932 are involved. Scan forward and see if it did. */
7933 it2 = *it;
7934 it2.vpos = it2.current_y = 0;
7935 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7936 it->vpos -= it2.vpos;
7937 it->current_y -= it2.current_y;
7938 it->current_x = it->hpos = 0;
7939
7940 /* If we moved too far back, move IT some lines forward. */
7941 if (it2.vpos > -dvpos)
7942 {
7943 int delta = it2.vpos + dvpos;
7944 it2 = *it;
7945 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7946 /* Move back again if we got too far ahead. */
7947 if (IT_CHARPOS (*it) >= start_charpos)
7948 *it = it2;
7949 }
7950 }
7951 }
7952
7953 /* Return 1 if IT points into the middle of a display vector. */
7954
7955 int
7956 in_display_vector_p (it)
7957 struct it *it;
7958 {
7959 return (it->method == GET_FROM_DISPLAY_VECTOR
7960 && it->current.dpvec_index > 0
7961 && it->dpvec + it->current.dpvec_index != it->dpend);
7962 }
7963
7964 \f
7965 /***********************************************************************
7966 Messages
7967 ***********************************************************************/
7968
7969
7970 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7971 to *Messages*. */
7972
7973 void
7974 add_to_log (format, arg1, arg2)
7975 char *format;
7976 Lisp_Object arg1, arg2;
7977 {
7978 Lisp_Object args[3];
7979 Lisp_Object msg, fmt;
7980 char *buffer;
7981 int len;
7982 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7983 USE_SAFE_ALLOCA;
7984
7985 /* Do nothing if called asynchronously. Inserting text into
7986 a buffer may call after-change-functions and alike and
7987 that would means running Lisp asynchronously. */
7988 if (handling_signal)
7989 return;
7990
7991 fmt = msg = Qnil;
7992 GCPRO4 (fmt, msg, arg1, arg2);
7993
7994 args[0] = fmt = build_string (format);
7995 args[1] = arg1;
7996 args[2] = arg2;
7997 msg = Fformat (3, args);
7998
7999 len = SBYTES (msg) + 1;
8000 SAFE_ALLOCA (buffer, char *, len);
8001 bcopy (SDATA (msg), buffer, len);
8002
8003 message_dolog (buffer, len - 1, 1, 0);
8004 SAFE_FREE ();
8005
8006 UNGCPRO;
8007 }
8008
8009
8010 /* Output a newline in the *Messages* buffer if "needs" one. */
8011
8012 void
8013 message_log_maybe_newline ()
8014 {
8015 if (message_log_need_newline)
8016 message_dolog ("", 0, 1, 0);
8017 }
8018
8019
8020 /* Add a string M of length NBYTES to the message log, optionally
8021 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8022 nonzero, means interpret the contents of M as multibyte. This
8023 function calls low-level routines in order to bypass text property
8024 hooks, etc. which might not be safe to run.
8025
8026 This may GC (insert may run before/after change hooks),
8027 so the buffer M must NOT point to a Lisp string. */
8028
8029 void
8030 message_dolog (m, nbytes, nlflag, multibyte)
8031 const char *m;
8032 int nbytes, nlflag, multibyte;
8033 {
8034 if (!NILP (Vmemory_full))
8035 return;
8036
8037 if (!NILP (Vmessage_log_max))
8038 {
8039 struct buffer *oldbuf;
8040 Lisp_Object oldpoint, oldbegv, oldzv;
8041 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8042 int point_at_end = 0;
8043 int zv_at_end = 0;
8044 Lisp_Object old_deactivate_mark, tem;
8045 struct gcpro gcpro1;
8046
8047 old_deactivate_mark = Vdeactivate_mark;
8048 oldbuf = current_buffer;
8049 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8050 current_buffer->undo_list = Qt;
8051
8052 oldpoint = message_dolog_marker1;
8053 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8054 oldbegv = message_dolog_marker2;
8055 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8056 oldzv = message_dolog_marker3;
8057 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8058 GCPRO1 (old_deactivate_mark);
8059
8060 if (PT == Z)
8061 point_at_end = 1;
8062 if (ZV == Z)
8063 zv_at_end = 1;
8064
8065 BEGV = BEG;
8066 BEGV_BYTE = BEG_BYTE;
8067 ZV = Z;
8068 ZV_BYTE = Z_BYTE;
8069 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8070
8071 /* Insert the string--maybe converting multibyte to single byte
8072 or vice versa, so that all the text fits the buffer. */
8073 if (multibyte
8074 && NILP (current_buffer->enable_multibyte_characters))
8075 {
8076 int i, c, char_bytes;
8077 unsigned char work[1];
8078
8079 /* Convert a multibyte string to single-byte
8080 for the *Message* buffer. */
8081 for (i = 0; i < nbytes; i += char_bytes)
8082 {
8083 c = string_char_and_length (m + i, &char_bytes);
8084 work[0] = (ASCII_CHAR_P (c)
8085 ? c
8086 : multibyte_char_to_unibyte (c, Qnil));
8087 insert_1_both (work, 1, 1, 1, 0, 0);
8088 }
8089 }
8090 else if (! multibyte
8091 && ! NILP (current_buffer->enable_multibyte_characters))
8092 {
8093 int i, c, char_bytes;
8094 unsigned char *msg = (unsigned char *) m;
8095 unsigned char str[MAX_MULTIBYTE_LENGTH];
8096 /* Convert a single-byte string to multibyte
8097 for the *Message* buffer. */
8098 for (i = 0; i < nbytes; i++)
8099 {
8100 c = msg[i];
8101 MAKE_CHAR_MULTIBYTE (c);
8102 char_bytes = CHAR_STRING (c, str);
8103 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8104 }
8105 }
8106 else if (nbytes)
8107 insert_1 (m, nbytes, 1, 0, 0);
8108
8109 if (nlflag)
8110 {
8111 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
8112 insert_1 ("\n", 1, 1, 0, 0);
8113
8114 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8115 this_bol = PT;
8116 this_bol_byte = PT_BYTE;
8117
8118 /* See if this line duplicates the previous one.
8119 If so, combine duplicates. */
8120 if (this_bol > BEG)
8121 {
8122 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8123 prev_bol = PT;
8124 prev_bol_byte = PT_BYTE;
8125
8126 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8127 this_bol, this_bol_byte);
8128 if (dup)
8129 {
8130 del_range_both (prev_bol, prev_bol_byte,
8131 this_bol, this_bol_byte, 0);
8132 if (dup > 1)
8133 {
8134 char dupstr[40];
8135 int duplen;
8136
8137 /* If you change this format, don't forget to also
8138 change message_log_check_duplicate. */
8139 sprintf (dupstr, " [%d times]", dup);
8140 duplen = strlen (dupstr);
8141 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8142 insert_1 (dupstr, duplen, 1, 0, 1);
8143 }
8144 }
8145 }
8146
8147 /* If we have more than the desired maximum number of lines
8148 in the *Messages* buffer now, delete the oldest ones.
8149 This is safe because we don't have undo in this buffer. */
8150
8151 if (NATNUMP (Vmessage_log_max))
8152 {
8153 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8154 -XFASTINT (Vmessage_log_max) - 1, 0);
8155 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8156 }
8157 }
8158 BEGV = XMARKER (oldbegv)->charpos;
8159 BEGV_BYTE = marker_byte_position (oldbegv);
8160
8161 if (zv_at_end)
8162 {
8163 ZV = Z;
8164 ZV_BYTE = Z_BYTE;
8165 }
8166 else
8167 {
8168 ZV = XMARKER (oldzv)->charpos;
8169 ZV_BYTE = marker_byte_position (oldzv);
8170 }
8171
8172 if (point_at_end)
8173 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8174 else
8175 /* We can't do Fgoto_char (oldpoint) because it will run some
8176 Lisp code. */
8177 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8178 XMARKER (oldpoint)->bytepos);
8179
8180 UNGCPRO;
8181 unchain_marker (XMARKER (oldpoint));
8182 unchain_marker (XMARKER (oldbegv));
8183 unchain_marker (XMARKER (oldzv));
8184
8185 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8186 set_buffer_internal (oldbuf);
8187 if (NILP (tem))
8188 windows_or_buffers_changed = old_windows_or_buffers_changed;
8189 message_log_need_newline = !nlflag;
8190 Vdeactivate_mark = old_deactivate_mark;
8191 }
8192 }
8193
8194
8195 /* We are at the end of the buffer after just having inserted a newline.
8196 (Note: We depend on the fact we won't be crossing the gap.)
8197 Check to see if the most recent message looks a lot like the previous one.
8198 Return 0 if different, 1 if the new one should just replace it, or a
8199 value N > 1 if we should also append " [N times]". */
8200
8201 static int
8202 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
8203 int prev_bol, this_bol;
8204 int prev_bol_byte, this_bol_byte;
8205 {
8206 int i;
8207 int len = Z_BYTE - 1 - this_bol_byte;
8208 int seen_dots = 0;
8209 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8210 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8211
8212 for (i = 0; i < len; i++)
8213 {
8214 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8215 seen_dots = 1;
8216 if (p1[i] != p2[i])
8217 return seen_dots;
8218 }
8219 p1 += len;
8220 if (*p1 == '\n')
8221 return 2;
8222 if (*p1++ == ' ' && *p1++ == '[')
8223 {
8224 int n = 0;
8225 while (*p1 >= '0' && *p1 <= '9')
8226 n = n * 10 + *p1++ - '0';
8227 if (strncmp (p1, " times]\n", 8) == 0)
8228 return n+1;
8229 }
8230 return 0;
8231 }
8232 \f
8233
8234 /* Display an echo area message M with a specified length of NBYTES
8235 bytes. The string may include null characters. If M is 0, clear
8236 out any existing message, and let the mini-buffer text show
8237 through.
8238
8239 This may GC, so the buffer M must NOT point to a Lisp string. */
8240
8241 void
8242 message2 (m, nbytes, multibyte)
8243 const char *m;
8244 int nbytes;
8245 int multibyte;
8246 {
8247 /* First flush out any partial line written with print. */
8248 message_log_maybe_newline ();
8249 if (m)
8250 message_dolog (m, nbytes, 1, multibyte);
8251 message2_nolog (m, nbytes, multibyte);
8252 }
8253
8254
8255 /* The non-logging counterpart of message2. */
8256
8257 void
8258 message2_nolog (m, nbytes, multibyte)
8259 const char *m;
8260 int nbytes, multibyte;
8261 {
8262 struct frame *sf = SELECTED_FRAME ();
8263 message_enable_multibyte = multibyte;
8264
8265 if (FRAME_INITIAL_P (sf))
8266 {
8267 if (noninteractive_need_newline)
8268 putc ('\n', stderr);
8269 noninteractive_need_newline = 0;
8270 if (m)
8271 fwrite (m, nbytes, 1, stderr);
8272 if (cursor_in_echo_area == 0)
8273 fprintf (stderr, "\n");
8274 fflush (stderr);
8275 }
8276 /* A null message buffer means that the frame hasn't really been
8277 initialized yet. Error messages get reported properly by
8278 cmd_error, so this must be just an informative message; toss it. */
8279 else if (INTERACTIVE
8280 && sf->glyphs_initialized_p
8281 && FRAME_MESSAGE_BUF (sf))
8282 {
8283 Lisp_Object mini_window;
8284 struct frame *f;
8285
8286 /* Get the frame containing the mini-buffer
8287 that the selected frame is using. */
8288 mini_window = FRAME_MINIBUF_WINDOW (sf);
8289 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8290
8291 FRAME_SAMPLE_VISIBILITY (f);
8292 if (FRAME_VISIBLE_P (sf)
8293 && ! FRAME_VISIBLE_P (f))
8294 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8295
8296 if (m)
8297 {
8298 set_message (m, Qnil, nbytes, multibyte);
8299 if (minibuffer_auto_raise)
8300 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8301 }
8302 else
8303 clear_message (1, 1);
8304
8305 do_pending_window_change (0);
8306 echo_area_display (1);
8307 do_pending_window_change (0);
8308 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8309 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8310 }
8311 }
8312
8313
8314 /* Display an echo area message M with a specified length of NBYTES
8315 bytes. The string may include null characters. If M is not a
8316 string, clear out any existing message, and let the mini-buffer
8317 text show through.
8318
8319 This function cancels echoing. */
8320
8321 void
8322 message3 (m, nbytes, multibyte)
8323 Lisp_Object m;
8324 int nbytes;
8325 int multibyte;
8326 {
8327 struct gcpro gcpro1;
8328
8329 GCPRO1 (m);
8330 clear_message (1,1);
8331 cancel_echoing ();
8332
8333 /* First flush out any partial line written with print. */
8334 message_log_maybe_newline ();
8335 if (STRINGP (m))
8336 {
8337 char *buffer;
8338 USE_SAFE_ALLOCA;
8339
8340 SAFE_ALLOCA (buffer, char *, nbytes);
8341 bcopy (SDATA (m), buffer, nbytes);
8342 message_dolog (buffer, nbytes, 1, multibyte);
8343 SAFE_FREE ();
8344 }
8345 message3_nolog (m, nbytes, multibyte);
8346
8347 UNGCPRO;
8348 }
8349
8350
8351 /* The non-logging version of message3.
8352 This does not cancel echoing, because it is used for echoing.
8353 Perhaps we need to make a separate function for echoing
8354 and make this cancel echoing. */
8355
8356 void
8357 message3_nolog (m, nbytes, multibyte)
8358 Lisp_Object m;
8359 int nbytes, multibyte;
8360 {
8361 struct frame *sf = SELECTED_FRAME ();
8362 message_enable_multibyte = multibyte;
8363
8364 if (FRAME_INITIAL_P (sf))
8365 {
8366 if (noninteractive_need_newline)
8367 putc ('\n', stderr);
8368 noninteractive_need_newline = 0;
8369 if (STRINGP (m))
8370 fwrite (SDATA (m), nbytes, 1, stderr);
8371 if (cursor_in_echo_area == 0)
8372 fprintf (stderr, "\n");
8373 fflush (stderr);
8374 }
8375 /* A null message buffer means that the frame hasn't really been
8376 initialized yet. Error messages get reported properly by
8377 cmd_error, so this must be just an informative message; toss it. */
8378 else if (INTERACTIVE
8379 && sf->glyphs_initialized_p
8380 && FRAME_MESSAGE_BUF (sf))
8381 {
8382 Lisp_Object mini_window;
8383 Lisp_Object frame;
8384 struct frame *f;
8385
8386 /* Get the frame containing the mini-buffer
8387 that the selected frame is using. */
8388 mini_window = FRAME_MINIBUF_WINDOW (sf);
8389 frame = XWINDOW (mini_window)->frame;
8390 f = XFRAME (frame);
8391
8392 FRAME_SAMPLE_VISIBILITY (f);
8393 if (FRAME_VISIBLE_P (sf)
8394 && !FRAME_VISIBLE_P (f))
8395 Fmake_frame_visible (frame);
8396
8397 if (STRINGP (m) && SCHARS (m) > 0)
8398 {
8399 set_message (NULL, m, nbytes, multibyte);
8400 if (minibuffer_auto_raise)
8401 Fraise_frame (frame);
8402 /* Assume we are not echoing.
8403 (If we are, echo_now will override this.) */
8404 echo_message_buffer = Qnil;
8405 }
8406 else
8407 clear_message (1, 1);
8408
8409 do_pending_window_change (0);
8410 echo_area_display (1);
8411 do_pending_window_change (0);
8412 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8413 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8414 }
8415 }
8416
8417
8418 /* Display a null-terminated echo area message M. If M is 0, clear
8419 out any existing message, and let the mini-buffer text show through.
8420
8421 The buffer M must continue to exist until after the echo area gets
8422 cleared or some other message gets displayed there. Do not pass
8423 text that is stored in a Lisp string. Do not pass text in a buffer
8424 that was alloca'd. */
8425
8426 void
8427 message1 (m)
8428 char *m;
8429 {
8430 message2 (m, (m ? strlen (m) : 0), 0);
8431 }
8432
8433
8434 /* The non-logging counterpart of message1. */
8435
8436 void
8437 message1_nolog (m)
8438 char *m;
8439 {
8440 message2_nolog (m, (m ? strlen (m) : 0), 0);
8441 }
8442
8443 /* Display a message M which contains a single %s
8444 which gets replaced with STRING. */
8445
8446 void
8447 message_with_string (m, string, log)
8448 char *m;
8449 Lisp_Object string;
8450 int log;
8451 {
8452 CHECK_STRING (string);
8453
8454 if (noninteractive)
8455 {
8456 if (m)
8457 {
8458 if (noninteractive_need_newline)
8459 putc ('\n', stderr);
8460 noninteractive_need_newline = 0;
8461 fprintf (stderr, m, SDATA (string));
8462 if (!cursor_in_echo_area)
8463 fprintf (stderr, "\n");
8464 fflush (stderr);
8465 }
8466 }
8467 else if (INTERACTIVE)
8468 {
8469 /* The frame whose minibuffer we're going to display the message on.
8470 It may be larger than the selected frame, so we need
8471 to use its buffer, not the selected frame's buffer. */
8472 Lisp_Object mini_window;
8473 struct frame *f, *sf = SELECTED_FRAME ();
8474
8475 /* Get the frame containing the minibuffer
8476 that the selected frame is using. */
8477 mini_window = FRAME_MINIBUF_WINDOW (sf);
8478 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8479
8480 /* A null message buffer means that the frame hasn't really been
8481 initialized yet. Error messages get reported properly by
8482 cmd_error, so this must be just an informative message; toss it. */
8483 if (FRAME_MESSAGE_BUF (f))
8484 {
8485 Lisp_Object args[2], message;
8486 struct gcpro gcpro1, gcpro2;
8487
8488 args[0] = build_string (m);
8489 args[1] = message = string;
8490 GCPRO2 (args[0], message);
8491 gcpro1.nvars = 2;
8492
8493 message = Fformat (2, args);
8494
8495 if (log)
8496 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8497 else
8498 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8499
8500 UNGCPRO;
8501
8502 /* Print should start at the beginning of the message
8503 buffer next time. */
8504 message_buf_print = 0;
8505 }
8506 }
8507 }
8508
8509
8510 /* Dump an informative message to the minibuf. If M is 0, clear out
8511 any existing message, and let the mini-buffer text show through. */
8512
8513 /* VARARGS 1 */
8514 void
8515 message (m, a1, a2, a3)
8516 char *m;
8517 EMACS_INT a1, a2, a3;
8518 {
8519 if (noninteractive)
8520 {
8521 if (m)
8522 {
8523 if (noninteractive_need_newline)
8524 putc ('\n', stderr);
8525 noninteractive_need_newline = 0;
8526 fprintf (stderr, m, a1, a2, a3);
8527 if (cursor_in_echo_area == 0)
8528 fprintf (stderr, "\n");
8529 fflush (stderr);
8530 }
8531 }
8532 else if (INTERACTIVE)
8533 {
8534 /* The frame whose mini-buffer we're going to display the message
8535 on. It may be larger than the selected frame, so we need to
8536 use its buffer, not the selected frame's buffer. */
8537 Lisp_Object mini_window;
8538 struct frame *f, *sf = SELECTED_FRAME ();
8539
8540 /* Get the frame containing the mini-buffer
8541 that the selected frame is using. */
8542 mini_window = FRAME_MINIBUF_WINDOW (sf);
8543 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8544
8545 /* A null message buffer means that the frame hasn't really been
8546 initialized yet. Error messages get reported properly by
8547 cmd_error, so this must be just an informative message; toss
8548 it. */
8549 if (FRAME_MESSAGE_BUF (f))
8550 {
8551 if (m)
8552 {
8553 int len;
8554 #ifdef NO_ARG_ARRAY
8555 char *a[3];
8556 a[0] = (char *) a1;
8557 a[1] = (char *) a2;
8558 a[2] = (char *) a3;
8559
8560 len = doprnt (FRAME_MESSAGE_BUF (f),
8561 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8562 #else
8563 len = doprnt (FRAME_MESSAGE_BUF (f),
8564 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8565 (char **) &a1);
8566 #endif /* NO_ARG_ARRAY */
8567
8568 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8569 }
8570 else
8571 message1 (0);
8572
8573 /* Print should start at the beginning of the message
8574 buffer next time. */
8575 message_buf_print = 0;
8576 }
8577 }
8578 }
8579
8580
8581 /* The non-logging version of message. */
8582
8583 void
8584 message_nolog (m, a1, a2, a3)
8585 char *m;
8586 EMACS_INT a1, a2, a3;
8587 {
8588 Lisp_Object old_log_max;
8589 old_log_max = Vmessage_log_max;
8590 Vmessage_log_max = Qnil;
8591 message (m, a1, a2, a3);
8592 Vmessage_log_max = old_log_max;
8593 }
8594
8595
8596 /* Display the current message in the current mini-buffer. This is
8597 only called from error handlers in process.c, and is not time
8598 critical. */
8599
8600 void
8601 update_echo_area ()
8602 {
8603 if (!NILP (echo_area_buffer[0]))
8604 {
8605 Lisp_Object string;
8606 string = Fcurrent_message ();
8607 message3 (string, SBYTES (string),
8608 !NILP (current_buffer->enable_multibyte_characters));
8609 }
8610 }
8611
8612
8613 /* Make sure echo area buffers in `echo_buffers' are live.
8614 If they aren't, make new ones. */
8615
8616 static void
8617 ensure_echo_area_buffers ()
8618 {
8619 int i;
8620
8621 for (i = 0; i < 2; ++i)
8622 if (!BUFFERP (echo_buffer[i])
8623 || NILP (XBUFFER (echo_buffer[i])->name))
8624 {
8625 char name[30];
8626 Lisp_Object old_buffer;
8627 int j;
8628
8629 old_buffer = echo_buffer[i];
8630 sprintf (name, " *Echo Area %d*", i);
8631 echo_buffer[i] = Fget_buffer_create (build_string (name));
8632 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8633 /* to force word wrap in echo area -
8634 it was decided to postpone this*/
8635 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8636
8637 for (j = 0; j < 2; ++j)
8638 if (EQ (old_buffer, echo_area_buffer[j]))
8639 echo_area_buffer[j] = echo_buffer[i];
8640 }
8641 }
8642
8643
8644 /* Call FN with args A1..A4 with either the current or last displayed
8645 echo_area_buffer as current buffer.
8646
8647 WHICH zero means use the current message buffer
8648 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8649 from echo_buffer[] and clear it.
8650
8651 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8652 suitable buffer from echo_buffer[] and clear it.
8653
8654 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8655 that the current message becomes the last displayed one, make
8656 choose a suitable buffer for echo_area_buffer[0], and clear it.
8657
8658 Value is what FN returns. */
8659
8660 static int
8661 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8662 struct window *w;
8663 int which;
8664 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8665 EMACS_INT a1;
8666 Lisp_Object a2;
8667 EMACS_INT a3, a4;
8668 {
8669 Lisp_Object buffer;
8670 int this_one, the_other, clear_buffer_p, rc;
8671 int count = SPECPDL_INDEX ();
8672
8673 /* If buffers aren't live, make new ones. */
8674 ensure_echo_area_buffers ();
8675
8676 clear_buffer_p = 0;
8677
8678 if (which == 0)
8679 this_one = 0, the_other = 1;
8680 else if (which > 0)
8681 this_one = 1, the_other = 0;
8682 else
8683 {
8684 this_one = 0, the_other = 1;
8685 clear_buffer_p = 1;
8686
8687 /* We need a fresh one in case the current echo buffer equals
8688 the one containing the last displayed echo area message. */
8689 if (!NILP (echo_area_buffer[this_one])
8690 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8691 echo_area_buffer[this_one] = Qnil;
8692 }
8693
8694 /* Choose a suitable buffer from echo_buffer[] is we don't
8695 have one. */
8696 if (NILP (echo_area_buffer[this_one]))
8697 {
8698 echo_area_buffer[this_one]
8699 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8700 ? echo_buffer[the_other]
8701 : echo_buffer[this_one]);
8702 clear_buffer_p = 1;
8703 }
8704
8705 buffer = echo_area_buffer[this_one];
8706
8707 /* Don't get confused by reusing the buffer used for echoing
8708 for a different purpose. */
8709 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8710 cancel_echoing ();
8711
8712 record_unwind_protect (unwind_with_echo_area_buffer,
8713 with_echo_area_buffer_unwind_data (w));
8714
8715 /* Make the echo area buffer current. Note that for display
8716 purposes, it is not necessary that the displayed window's buffer
8717 == current_buffer, except for text property lookup. So, let's
8718 only set that buffer temporarily here without doing a full
8719 Fset_window_buffer. We must also change w->pointm, though,
8720 because otherwise an assertions in unshow_buffer fails, and Emacs
8721 aborts. */
8722 set_buffer_internal_1 (XBUFFER (buffer));
8723 if (w)
8724 {
8725 w->buffer = buffer;
8726 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8727 }
8728
8729 current_buffer->undo_list = Qt;
8730 current_buffer->read_only = Qnil;
8731 specbind (Qinhibit_read_only, Qt);
8732 specbind (Qinhibit_modification_hooks, Qt);
8733
8734 if (clear_buffer_p && Z > BEG)
8735 del_range (BEG, Z);
8736
8737 xassert (BEGV >= BEG);
8738 xassert (ZV <= Z && ZV >= BEGV);
8739
8740 rc = fn (a1, a2, a3, a4);
8741
8742 xassert (BEGV >= BEG);
8743 xassert (ZV <= Z && ZV >= BEGV);
8744
8745 unbind_to (count, Qnil);
8746 return rc;
8747 }
8748
8749
8750 /* Save state that should be preserved around the call to the function
8751 FN called in with_echo_area_buffer. */
8752
8753 static Lisp_Object
8754 with_echo_area_buffer_unwind_data (w)
8755 struct window *w;
8756 {
8757 int i = 0;
8758 Lisp_Object vector, tmp;
8759
8760 /* Reduce consing by keeping one vector in
8761 Vwith_echo_area_save_vector. */
8762 vector = Vwith_echo_area_save_vector;
8763 Vwith_echo_area_save_vector = Qnil;
8764
8765 if (NILP (vector))
8766 vector = Fmake_vector (make_number (7), Qnil);
8767
8768 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8769 ASET (vector, i, Vdeactivate_mark); ++i;
8770 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8771
8772 if (w)
8773 {
8774 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8775 ASET (vector, i, w->buffer); ++i;
8776 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8777 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8778 }
8779 else
8780 {
8781 int end = i + 4;
8782 for (; i < end; ++i)
8783 ASET (vector, i, Qnil);
8784 }
8785
8786 xassert (i == ASIZE (vector));
8787 return vector;
8788 }
8789
8790
8791 /* Restore global state from VECTOR which was created by
8792 with_echo_area_buffer_unwind_data. */
8793
8794 static Lisp_Object
8795 unwind_with_echo_area_buffer (vector)
8796 Lisp_Object vector;
8797 {
8798 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8799 Vdeactivate_mark = AREF (vector, 1);
8800 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8801
8802 if (WINDOWP (AREF (vector, 3)))
8803 {
8804 struct window *w;
8805 Lisp_Object buffer, charpos, bytepos;
8806
8807 w = XWINDOW (AREF (vector, 3));
8808 buffer = AREF (vector, 4);
8809 charpos = AREF (vector, 5);
8810 bytepos = AREF (vector, 6);
8811
8812 w->buffer = buffer;
8813 set_marker_both (w->pointm, buffer,
8814 XFASTINT (charpos), XFASTINT (bytepos));
8815 }
8816
8817 Vwith_echo_area_save_vector = vector;
8818 return Qnil;
8819 }
8820
8821
8822 /* Set up the echo area for use by print functions. MULTIBYTE_P
8823 non-zero means we will print multibyte. */
8824
8825 void
8826 setup_echo_area_for_printing (multibyte_p)
8827 int multibyte_p;
8828 {
8829 /* If we can't find an echo area any more, exit. */
8830 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8831 Fkill_emacs (Qnil);
8832
8833 ensure_echo_area_buffers ();
8834
8835 if (!message_buf_print)
8836 {
8837 /* A message has been output since the last time we printed.
8838 Choose a fresh echo area buffer. */
8839 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8840 echo_area_buffer[0] = echo_buffer[1];
8841 else
8842 echo_area_buffer[0] = echo_buffer[0];
8843
8844 /* Switch to that buffer and clear it. */
8845 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8846 current_buffer->truncate_lines = Qnil;
8847
8848 if (Z > BEG)
8849 {
8850 int count = SPECPDL_INDEX ();
8851 specbind (Qinhibit_read_only, Qt);
8852 /* Note that undo recording is always disabled. */
8853 del_range (BEG, Z);
8854 unbind_to (count, Qnil);
8855 }
8856 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8857
8858 /* Set up the buffer for the multibyteness we need. */
8859 if (multibyte_p
8860 != !NILP (current_buffer->enable_multibyte_characters))
8861 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8862
8863 /* Raise the frame containing the echo area. */
8864 if (minibuffer_auto_raise)
8865 {
8866 struct frame *sf = SELECTED_FRAME ();
8867 Lisp_Object mini_window;
8868 mini_window = FRAME_MINIBUF_WINDOW (sf);
8869 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8870 }
8871
8872 message_log_maybe_newline ();
8873 message_buf_print = 1;
8874 }
8875 else
8876 {
8877 if (NILP (echo_area_buffer[0]))
8878 {
8879 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8880 echo_area_buffer[0] = echo_buffer[1];
8881 else
8882 echo_area_buffer[0] = echo_buffer[0];
8883 }
8884
8885 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8886 {
8887 /* Someone switched buffers between print requests. */
8888 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8889 current_buffer->truncate_lines = Qnil;
8890 }
8891 }
8892 }
8893
8894
8895 /* Display an echo area message in window W. Value is non-zero if W's
8896 height is changed. If display_last_displayed_message_p is
8897 non-zero, display the message that was last displayed, otherwise
8898 display the current message. */
8899
8900 static int
8901 display_echo_area (w)
8902 struct window *w;
8903 {
8904 int i, no_message_p, window_height_changed_p, count;
8905
8906 /* Temporarily disable garbage collections while displaying the echo
8907 area. This is done because a GC can print a message itself.
8908 That message would modify the echo area buffer's contents while a
8909 redisplay of the buffer is going on, and seriously confuse
8910 redisplay. */
8911 count = inhibit_garbage_collection ();
8912
8913 /* If there is no message, we must call display_echo_area_1
8914 nevertheless because it resizes the window. But we will have to
8915 reset the echo_area_buffer in question to nil at the end because
8916 with_echo_area_buffer will sets it to an empty buffer. */
8917 i = display_last_displayed_message_p ? 1 : 0;
8918 no_message_p = NILP (echo_area_buffer[i]);
8919
8920 window_height_changed_p
8921 = with_echo_area_buffer (w, display_last_displayed_message_p,
8922 display_echo_area_1,
8923 (EMACS_INT) w, Qnil, 0, 0);
8924
8925 if (no_message_p)
8926 echo_area_buffer[i] = Qnil;
8927
8928 unbind_to (count, Qnil);
8929 return window_height_changed_p;
8930 }
8931
8932
8933 /* Helper for display_echo_area. Display the current buffer which
8934 contains the current echo area message in window W, a mini-window,
8935 a pointer to which is passed in A1. A2..A4 are currently not used.
8936 Change the height of W so that all of the message is displayed.
8937 Value is non-zero if height of W was changed. */
8938
8939 static int
8940 display_echo_area_1 (a1, a2, a3, a4)
8941 EMACS_INT a1;
8942 Lisp_Object a2;
8943 EMACS_INT a3, a4;
8944 {
8945 struct window *w = (struct window *) a1;
8946 Lisp_Object window;
8947 struct text_pos start;
8948 int window_height_changed_p = 0;
8949
8950 /* Do this before displaying, so that we have a large enough glyph
8951 matrix for the display. If we can't get enough space for the
8952 whole text, display the last N lines. That works by setting w->start. */
8953 window_height_changed_p = resize_mini_window (w, 0);
8954
8955 /* Use the starting position chosen by resize_mini_window. */
8956 SET_TEXT_POS_FROM_MARKER (start, w->start);
8957
8958 /* Display. */
8959 clear_glyph_matrix (w->desired_matrix);
8960 XSETWINDOW (window, w);
8961 try_window (window, start, 0);
8962
8963 return window_height_changed_p;
8964 }
8965
8966
8967 /* Resize the echo area window to exactly the size needed for the
8968 currently displayed message, if there is one. If a mini-buffer
8969 is active, don't shrink it. */
8970
8971 void
8972 resize_echo_area_exactly ()
8973 {
8974 if (BUFFERP (echo_area_buffer[0])
8975 && WINDOWP (echo_area_window))
8976 {
8977 struct window *w = XWINDOW (echo_area_window);
8978 int resized_p;
8979 Lisp_Object resize_exactly;
8980
8981 if (minibuf_level == 0)
8982 resize_exactly = Qt;
8983 else
8984 resize_exactly = Qnil;
8985
8986 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8987 (EMACS_INT) w, resize_exactly, 0, 0);
8988 if (resized_p)
8989 {
8990 ++windows_or_buffers_changed;
8991 ++update_mode_lines;
8992 redisplay_internal (0);
8993 }
8994 }
8995 }
8996
8997
8998 /* Callback function for with_echo_area_buffer, when used from
8999 resize_echo_area_exactly. A1 contains a pointer to the window to
9000 resize, EXACTLY non-nil means resize the mini-window exactly to the
9001 size of the text displayed. A3 and A4 are not used. Value is what
9002 resize_mini_window returns. */
9003
9004 static int
9005 resize_mini_window_1 (a1, exactly, a3, a4)
9006 EMACS_INT a1;
9007 Lisp_Object exactly;
9008 EMACS_INT a3, a4;
9009 {
9010 return resize_mini_window ((struct window *) a1, !NILP (exactly));
9011 }
9012
9013
9014 /* Resize mini-window W to fit the size of its contents. EXACT_P
9015 means size the window exactly to the size needed. Otherwise, it's
9016 only enlarged until W's buffer is empty.
9017
9018 Set W->start to the right place to begin display. If the whole
9019 contents fit, start at the beginning. Otherwise, start so as
9020 to make the end of the contents appear. This is particularly
9021 important for y-or-n-p, but seems desirable generally.
9022
9023 Value is non-zero if the window height has been changed. */
9024
9025 int
9026 resize_mini_window (w, exact_p)
9027 struct window *w;
9028 int exact_p;
9029 {
9030 struct frame *f = XFRAME (w->frame);
9031 int window_height_changed_p = 0;
9032
9033 xassert (MINI_WINDOW_P (w));
9034
9035 /* By default, start display at the beginning. */
9036 set_marker_both (w->start, w->buffer,
9037 BUF_BEGV (XBUFFER (w->buffer)),
9038 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9039
9040 /* Don't resize windows while redisplaying a window; it would
9041 confuse redisplay functions when the size of the window they are
9042 displaying changes from under them. Such a resizing can happen,
9043 for instance, when which-func prints a long message while
9044 we are running fontification-functions. We're running these
9045 functions with safe_call which binds inhibit-redisplay to t. */
9046 if (!NILP (Vinhibit_redisplay))
9047 return 0;
9048
9049 /* Nil means don't try to resize. */
9050 if (NILP (Vresize_mini_windows)
9051 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9052 return 0;
9053
9054 if (!FRAME_MINIBUF_ONLY_P (f))
9055 {
9056 struct it it;
9057 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9058 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9059 int height, max_height;
9060 int unit = FRAME_LINE_HEIGHT (f);
9061 struct text_pos start;
9062 struct buffer *old_current_buffer = NULL;
9063
9064 if (current_buffer != XBUFFER (w->buffer))
9065 {
9066 old_current_buffer = current_buffer;
9067 set_buffer_internal (XBUFFER (w->buffer));
9068 }
9069
9070 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9071
9072 /* Compute the max. number of lines specified by the user. */
9073 if (FLOATP (Vmax_mini_window_height))
9074 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9075 else if (INTEGERP (Vmax_mini_window_height))
9076 max_height = XINT (Vmax_mini_window_height);
9077 else
9078 max_height = total_height / 4;
9079
9080 /* Correct that max. height if it's bogus. */
9081 max_height = max (1, max_height);
9082 max_height = min (total_height, max_height);
9083
9084 /* Find out the height of the text in the window. */
9085 if (it.line_wrap == TRUNCATE)
9086 height = 1;
9087 else
9088 {
9089 last_height = 0;
9090 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9091 if (it.max_ascent == 0 && it.max_descent == 0)
9092 height = it.current_y + last_height;
9093 else
9094 height = it.current_y + it.max_ascent + it.max_descent;
9095 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9096 height = (height + unit - 1) / unit;
9097 }
9098
9099 /* Compute a suitable window start. */
9100 if (height > max_height)
9101 {
9102 height = max_height;
9103 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9104 move_it_vertically_backward (&it, (height - 1) * unit);
9105 start = it.current.pos;
9106 }
9107 else
9108 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9109 SET_MARKER_FROM_TEXT_POS (w->start, start);
9110
9111 if (EQ (Vresize_mini_windows, Qgrow_only))
9112 {
9113 /* Let it grow only, until we display an empty message, in which
9114 case the window shrinks again. */
9115 if (height > WINDOW_TOTAL_LINES (w))
9116 {
9117 int old_height = WINDOW_TOTAL_LINES (w);
9118 freeze_window_starts (f, 1);
9119 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9120 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9121 }
9122 else if (height < WINDOW_TOTAL_LINES (w)
9123 && (exact_p || BEGV == ZV))
9124 {
9125 int old_height = WINDOW_TOTAL_LINES (w);
9126 freeze_window_starts (f, 0);
9127 shrink_mini_window (w);
9128 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9129 }
9130 }
9131 else
9132 {
9133 /* Always resize to exact size needed. */
9134 if (height > WINDOW_TOTAL_LINES (w))
9135 {
9136 int old_height = WINDOW_TOTAL_LINES (w);
9137 freeze_window_starts (f, 1);
9138 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9139 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9140 }
9141 else if (height < WINDOW_TOTAL_LINES (w))
9142 {
9143 int old_height = WINDOW_TOTAL_LINES (w);
9144 freeze_window_starts (f, 0);
9145 shrink_mini_window (w);
9146
9147 if (height)
9148 {
9149 freeze_window_starts (f, 1);
9150 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9151 }
9152
9153 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9154 }
9155 }
9156
9157 if (old_current_buffer)
9158 set_buffer_internal (old_current_buffer);
9159 }
9160
9161 return window_height_changed_p;
9162 }
9163
9164
9165 /* Value is the current message, a string, or nil if there is no
9166 current message. */
9167
9168 Lisp_Object
9169 current_message ()
9170 {
9171 Lisp_Object msg;
9172
9173 if (!BUFFERP (echo_area_buffer[0]))
9174 msg = Qnil;
9175 else
9176 {
9177 with_echo_area_buffer (0, 0, current_message_1,
9178 (EMACS_INT) &msg, Qnil, 0, 0);
9179 if (NILP (msg))
9180 echo_area_buffer[0] = Qnil;
9181 }
9182
9183 return msg;
9184 }
9185
9186
9187 static int
9188 current_message_1 (a1, a2, a3, a4)
9189 EMACS_INT a1;
9190 Lisp_Object a2;
9191 EMACS_INT a3, a4;
9192 {
9193 Lisp_Object *msg = (Lisp_Object *) a1;
9194
9195 if (Z > BEG)
9196 *msg = make_buffer_string (BEG, Z, 1);
9197 else
9198 *msg = Qnil;
9199 return 0;
9200 }
9201
9202
9203 /* Push the current message on Vmessage_stack for later restauration
9204 by restore_message. Value is non-zero if the current message isn't
9205 empty. This is a relatively infrequent operation, so it's not
9206 worth optimizing. */
9207
9208 int
9209 push_message ()
9210 {
9211 Lisp_Object msg;
9212 msg = current_message ();
9213 Vmessage_stack = Fcons (msg, Vmessage_stack);
9214 return STRINGP (msg);
9215 }
9216
9217
9218 /* Restore message display from the top of Vmessage_stack. */
9219
9220 void
9221 restore_message ()
9222 {
9223 Lisp_Object msg;
9224
9225 xassert (CONSP (Vmessage_stack));
9226 msg = XCAR (Vmessage_stack);
9227 if (STRINGP (msg))
9228 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9229 else
9230 message3_nolog (msg, 0, 0);
9231 }
9232
9233
9234 /* Handler for record_unwind_protect calling pop_message. */
9235
9236 Lisp_Object
9237 pop_message_unwind (dummy)
9238 Lisp_Object dummy;
9239 {
9240 pop_message ();
9241 return Qnil;
9242 }
9243
9244 /* Pop the top-most entry off Vmessage_stack. */
9245
9246 void
9247 pop_message ()
9248 {
9249 xassert (CONSP (Vmessage_stack));
9250 Vmessage_stack = XCDR (Vmessage_stack);
9251 }
9252
9253
9254 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9255 exits. If the stack is not empty, we have a missing pop_message
9256 somewhere. */
9257
9258 void
9259 check_message_stack ()
9260 {
9261 if (!NILP (Vmessage_stack))
9262 abort ();
9263 }
9264
9265
9266 /* Truncate to NCHARS what will be displayed in the echo area the next
9267 time we display it---but don't redisplay it now. */
9268
9269 void
9270 truncate_echo_area (nchars)
9271 int nchars;
9272 {
9273 if (nchars == 0)
9274 echo_area_buffer[0] = Qnil;
9275 /* A null message buffer means that the frame hasn't really been
9276 initialized yet. Error messages get reported properly by
9277 cmd_error, so this must be just an informative message; toss it. */
9278 else if (!noninteractive
9279 && INTERACTIVE
9280 && !NILP (echo_area_buffer[0]))
9281 {
9282 struct frame *sf = SELECTED_FRAME ();
9283 if (FRAME_MESSAGE_BUF (sf))
9284 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9285 }
9286 }
9287
9288
9289 /* Helper function for truncate_echo_area. Truncate the current
9290 message to at most NCHARS characters. */
9291
9292 static int
9293 truncate_message_1 (nchars, a2, a3, a4)
9294 EMACS_INT nchars;
9295 Lisp_Object a2;
9296 EMACS_INT a3, a4;
9297 {
9298 if (BEG + nchars < Z)
9299 del_range (BEG + nchars, Z);
9300 if (Z == BEG)
9301 echo_area_buffer[0] = Qnil;
9302 return 0;
9303 }
9304
9305
9306 /* Set the current message to a substring of S or STRING.
9307
9308 If STRING is a Lisp string, set the message to the first NBYTES
9309 bytes from STRING. NBYTES zero means use the whole string. If
9310 STRING is multibyte, the message will be displayed multibyte.
9311
9312 If S is not null, set the message to the first LEN bytes of S. LEN
9313 zero means use the whole string. MULTIBYTE_P non-zero means S is
9314 multibyte. Display the message multibyte in that case.
9315
9316 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9317 to t before calling set_message_1 (which calls insert).
9318 */
9319
9320 void
9321 set_message (s, string, nbytes, multibyte_p)
9322 const char *s;
9323 Lisp_Object string;
9324 int nbytes, multibyte_p;
9325 {
9326 message_enable_multibyte
9327 = ((s && multibyte_p)
9328 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9329
9330 with_echo_area_buffer (0, -1, set_message_1,
9331 (EMACS_INT) s, string, nbytes, multibyte_p);
9332 message_buf_print = 0;
9333 help_echo_showing_p = 0;
9334 }
9335
9336
9337 /* Helper function for set_message. Arguments have the same meaning
9338 as there, with A1 corresponding to S and A2 corresponding to STRING
9339 This function is called with the echo area buffer being
9340 current. */
9341
9342 static int
9343 set_message_1 (a1, a2, nbytes, multibyte_p)
9344 EMACS_INT a1;
9345 Lisp_Object a2;
9346 EMACS_INT nbytes, multibyte_p;
9347 {
9348 const char *s = (const char *) a1;
9349 Lisp_Object string = a2;
9350
9351 /* Change multibyteness of the echo buffer appropriately. */
9352 if (message_enable_multibyte
9353 != !NILP (current_buffer->enable_multibyte_characters))
9354 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9355
9356 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9357
9358 /* Insert new message at BEG. */
9359 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9360
9361 if (STRINGP (string))
9362 {
9363 int nchars;
9364
9365 if (nbytes == 0)
9366 nbytes = SBYTES (string);
9367 nchars = string_byte_to_char (string, nbytes);
9368
9369 /* This function takes care of single/multibyte conversion. We
9370 just have to ensure that the echo area buffer has the right
9371 setting of enable_multibyte_characters. */
9372 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9373 }
9374 else if (s)
9375 {
9376 if (nbytes == 0)
9377 nbytes = strlen (s);
9378
9379 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9380 {
9381 /* Convert from multi-byte to single-byte. */
9382 int i, c, n;
9383 unsigned char work[1];
9384
9385 /* Convert a multibyte string to single-byte. */
9386 for (i = 0; i < nbytes; i += n)
9387 {
9388 c = string_char_and_length (s + i, &n);
9389 work[0] = (ASCII_CHAR_P (c)
9390 ? c
9391 : multibyte_char_to_unibyte (c, Qnil));
9392 insert_1_both (work, 1, 1, 1, 0, 0);
9393 }
9394 }
9395 else if (!multibyte_p
9396 && !NILP (current_buffer->enable_multibyte_characters))
9397 {
9398 /* Convert from single-byte to multi-byte. */
9399 int i, c, n;
9400 const unsigned char *msg = (const unsigned char *) s;
9401 unsigned char str[MAX_MULTIBYTE_LENGTH];
9402
9403 /* Convert a single-byte string to multibyte. */
9404 for (i = 0; i < nbytes; i++)
9405 {
9406 c = msg[i];
9407 MAKE_CHAR_MULTIBYTE (c);
9408 n = CHAR_STRING (c, str);
9409 insert_1_both (str, 1, n, 1, 0, 0);
9410 }
9411 }
9412 else
9413 insert_1 (s, nbytes, 1, 0, 0);
9414 }
9415
9416 return 0;
9417 }
9418
9419
9420 /* Clear messages. CURRENT_P non-zero means clear the current
9421 message. LAST_DISPLAYED_P non-zero means clear the message
9422 last displayed. */
9423
9424 void
9425 clear_message (current_p, last_displayed_p)
9426 int current_p, last_displayed_p;
9427 {
9428 if (current_p)
9429 {
9430 echo_area_buffer[0] = Qnil;
9431 message_cleared_p = 1;
9432 }
9433
9434 if (last_displayed_p)
9435 echo_area_buffer[1] = Qnil;
9436
9437 message_buf_print = 0;
9438 }
9439
9440 /* Clear garbaged frames.
9441
9442 This function is used where the old redisplay called
9443 redraw_garbaged_frames which in turn called redraw_frame which in
9444 turn called clear_frame. The call to clear_frame was a source of
9445 flickering. I believe a clear_frame is not necessary. It should
9446 suffice in the new redisplay to invalidate all current matrices,
9447 and ensure a complete redisplay of all windows. */
9448
9449 static void
9450 clear_garbaged_frames ()
9451 {
9452 if (frame_garbaged)
9453 {
9454 Lisp_Object tail, frame;
9455 int changed_count = 0;
9456
9457 FOR_EACH_FRAME (tail, frame)
9458 {
9459 struct frame *f = XFRAME (frame);
9460
9461 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9462 {
9463 if (f->resized_p)
9464 {
9465 Fredraw_frame (frame);
9466 f->force_flush_display_p = 1;
9467 }
9468 clear_current_matrices (f);
9469 changed_count++;
9470 f->garbaged = 0;
9471 f->resized_p = 0;
9472 }
9473 }
9474
9475 frame_garbaged = 0;
9476 if (changed_count)
9477 ++windows_or_buffers_changed;
9478 }
9479 }
9480
9481
9482 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9483 is non-zero update selected_frame. Value is non-zero if the
9484 mini-windows height has been changed. */
9485
9486 static int
9487 echo_area_display (update_frame_p)
9488 int update_frame_p;
9489 {
9490 Lisp_Object mini_window;
9491 struct window *w;
9492 struct frame *f;
9493 int window_height_changed_p = 0;
9494 struct frame *sf = SELECTED_FRAME ();
9495
9496 mini_window = FRAME_MINIBUF_WINDOW (sf);
9497 w = XWINDOW (mini_window);
9498 f = XFRAME (WINDOW_FRAME (w));
9499
9500 /* Don't display if frame is invisible or not yet initialized. */
9501 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9502 return 0;
9503
9504 #ifdef HAVE_WINDOW_SYSTEM
9505 /* When Emacs starts, selected_frame may be the initial terminal
9506 frame. If we let this through, a message would be displayed on
9507 the terminal. */
9508 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9509 return 0;
9510 #endif /* HAVE_WINDOW_SYSTEM */
9511
9512 /* Redraw garbaged frames. */
9513 if (frame_garbaged)
9514 clear_garbaged_frames ();
9515
9516 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9517 {
9518 echo_area_window = mini_window;
9519 window_height_changed_p = display_echo_area (w);
9520 w->must_be_updated_p = 1;
9521
9522 /* Update the display, unless called from redisplay_internal.
9523 Also don't update the screen during redisplay itself. The
9524 update will happen at the end of redisplay, and an update
9525 here could cause confusion. */
9526 if (update_frame_p && !redisplaying_p)
9527 {
9528 int n = 0;
9529
9530 /* If the display update has been interrupted by pending
9531 input, update mode lines in the frame. Due to the
9532 pending input, it might have been that redisplay hasn't
9533 been called, so that mode lines above the echo area are
9534 garbaged. This looks odd, so we prevent it here. */
9535 if (!display_completed)
9536 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9537
9538 if (window_height_changed_p
9539 /* Don't do this if Emacs is shutting down. Redisplay
9540 needs to run hooks. */
9541 && !NILP (Vrun_hooks))
9542 {
9543 /* Must update other windows. Likewise as in other
9544 cases, don't let this update be interrupted by
9545 pending input. */
9546 int count = SPECPDL_INDEX ();
9547 specbind (Qredisplay_dont_pause, Qt);
9548 windows_or_buffers_changed = 1;
9549 redisplay_internal (0);
9550 unbind_to (count, Qnil);
9551 }
9552 else if (FRAME_WINDOW_P (f) && n == 0)
9553 {
9554 /* Window configuration is the same as before.
9555 Can do with a display update of the echo area,
9556 unless we displayed some mode lines. */
9557 update_single_window (w, 1);
9558 FRAME_RIF (f)->flush_display (f);
9559 }
9560 else
9561 update_frame (f, 1, 1);
9562
9563 /* If cursor is in the echo area, make sure that the next
9564 redisplay displays the minibuffer, so that the cursor will
9565 be replaced with what the minibuffer wants. */
9566 if (cursor_in_echo_area)
9567 ++windows_or_buffers_changed;
9568 }
9569 }
9570 else if (!EQ (mini_window, selected_window))
9571 windows_or_buffers_changed++;
9572
9573 /* Last displayed message is now the current message. */
9574 echo_area_buffer[1] = echo_area_buffer[0];
9575 /* Inform read_char that we're not echoing. */
9576 echo_message_buffer = Qnil;
9577
9578 /* Prevent redisplay optimization in redisplay_internal by resetting
9579 this_line_start_pos. This is done because the mini-buffer now
9580 displays the message instead of its buffer text. */
9581 if (EQ (mini_window, selected_window))
9582 CHARPOS (this_line_start_pos) = 0;
9583
9584 return window_height_changed_p;
9585 }
9586
9587
9588 \f
9589 /***********************************************************************
9590 Mode Lines and Frame Titles
9591 ***********************************************************************/
9592
9593 /* A buffer for constructing non-propertized mode-line strings and
9594 frame titles in it; allocated from the heap in init_xdisp and
9595 resized as needed in store_mode_line_noprop_char. */
9596
9597 static char *mode_line_noprop_buf;
9598
9599 /* The buffer's end, and a current output position in it. */
9600
9601 static char *mode_line_noprop_buf_end;
9602 static char *mode_line_noprop_ptr;
9603
9604 #define MODE_LINE_NOPROP_LEN(start) \
9605 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9606
9607 static enum {
9608 MODE_LINE_DISPLAY = 0,
9609 MODE_LINE_TITLE,
9610 MODE_LINE_NOPROP,
9611 MODE_LINE_STRING
9612 } mode_line_target;
9613
9614 /* Alist that caches the results of :propertize.
9615 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9616 static Lisp_Object mode_line_proptrans_alist;
9617
9618 /* List of strings making up the mode-line. */
9619 static Lisp_Object mode_line_string_list;
9620
9621 /* Base face property when building propertized mode line string. */
9622 static Lisp_Object mode_line_string_face;
9623 static Lisp_Object mode_line_string_face_prop;
9624
9625
9626 /* Unwind data for mode line strings */
9627
9628 static Lisp_Object Vmode_line_unwind_vector;
9629
9630 static Lisp_Object
9631 format_mode_line_unwind_data (struct buffer *obuf,
9632 Lisp_Object owin,
9633 int save_proptrans)
9634 {
9635 Lisp_Object vector, tmp;
9636
9637 /* Reduce consing by keeping one vector in
9638 Vwith_echo_area_save_vector. */
9639 vector = Vmode_line_unwind_vector;
9640 Vmode_line_unwind_vector = Qnil;
9641
9642 if (NILP (vector))
9643 vector = Fmake_vector (make_number (8), Qnil);
9644
9645 ASET (vector, 0, make_number (mode_line_target));
9646 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9647 ASET (vector, 2, mode_line_string_list);
9648 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9649 ASET (vector, 4, mode_line_string_face);
9650 ASET (vector, 5, mode_line_string_face_prop);
9651
9652 if (obuf)
9653 XSETBUFFER (tmp, obuf);
9654 else
9655 tmp = Qnil;
9656 ASET (vector, 6, tmp);
9657 ASET (vector, 7, owin);
9658
9659 return vector;
9660 }
9661
9662 static Lisp_Object
9663 unwind_format_mode_line (vector)
9664 Lisp_Object vector;
9665 {
9666 mode_line_target = XINT (AREF (vector, 0));
9667 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9668 mode_line_string_list = AREF (vector, 2);
9669 if (! EQ (AREF (vector, 3), Qt))
9670 mode_line_proptrans_alist = AREF (vector, 3);
9671 mode_line_string_face = AREF (vector, 4);
9672 mode_line_string_face_prop = AREF (vector, 5);
9673
9674 if (!NILP (AREF (vector, 7)))
9675 /* Select window before buffer, since it may change the buffer. */
9676 Fselect_window (AREF (vector, 7), Qt);
9677
9678 if (!NILP (AREF (vector, 6)))
9679 {
9680 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9681 ASET (vector, 6, Qnil);
9682 }
9683
9684 Vmode_line_unwind_vector = vector;
9685 return Qnil;
9686 }
9687
9688
9689 /* Store a single character C for the frame title in mode_line_noprop_buf.
9690 Re-allocate mode_line_noprop_buf if necessary. */
9691
9692 static void
9693 #ifdef PROTOTYPES
9694 store_mode_line_noprop_char (char c)
9695 #else
9696 store_mode_line_noprop_char (c)
9697 char c;
9698 #endif
9699 {
9700 /* If output position has reached the end of the allocated buffer,
9701 double the buffer's size. */
9702 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9703 {
9704 int len = MODE_LINE_NOPROP_LEN (0);
9705 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9706 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9707 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9708 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9709 }
9710
9711 *mode_line_noprop_ptr++ = c;
9712 }
9713
9714
9715 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9716 mode_line_noprop_ptr. STR is the string to store. Do not copy
9717 characters that yield more columns than PRECISION; PRECISION <= 0
9718 means copy the whole string. Pad with spaces until FIELD_WIDTH
9719 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9720 pad. Called from display_mode_element when it is used to build a
9721 frame title. */
9722
9723 static int
9724 store_mode_line_noprop (str, field_width, precision)
9725 const unsigned char *str;
9726 int field_width, precision;
9727 {
9728 int n = 0;
9729 int dummy, nbytes;
9730
9731 /* Copy at most PRECISION chars from STR. */
9732 nbytes = strlen (str);
9733 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9734 while (nbytes--)
9735 store_mode_line_noprop_char (*str++);
9736
9737 /* Fill up with spaces until FIELD_WIDTH reached. */
9738 while (field_width > 0
9739 && n < field_width)
9740 {
9741 store_mode_line_noprop_char (' ');
9742 ++n;
9743 }
9744
9745 return n;
9746 }
9747
9748 /***********************************************************************
9749 Frame Titles
9750 ***********************************************************************/
9751
9752 #ifdef HAVE_WINDOW_SYSTEM
9753
9754 /* Set the title of FRAME, if it has changed. The title format is
9755 Vicon_title_format if FRAME is iconified, otherwise it is
9756 frame_title_format. */
9757
9758 static void
9759 x_consider_frame_title (frame)
9760 Lisp_Object frame;
9761 {
9762 struct frame *f = XFRAME (frame);
9763
9764 if (FRAME_WINDOW_P (f)
9765 || FRAME_MINIBUF_ONLY_P (f)
9766 || f->explicit_name)
9767 {
9768 /* Do we have more than one visible frame on this X display? */
9769 Lisp_Object tail;
9770 Lisp_Object fmt;
9771 int title_start;
9772 char *title;
9773 int len;
9774 struct it it;
9775 int count = SPECPDL_INDEX ();
9776
9777 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9778 {
9779 Lisp_Object other_frame = XCAR (tail);
9780 struct frame *tf = XFRAME (other_frame);
9781
9782 if (tf != f
9783 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9784 && !FRAME_MINIBUF_ONLY_P (tf)
9785 && !EQ (other_frame, tip_frame)
9786 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9787 break;
9788 }
9789
9790 /* Set global variable indicating that multiple frames exist. */
9791 multiple_frames = CONSP (tail);
9792
9793 /* Switch to the buffer of selected window of the frame. Set up
9794 mode_line_target so that display_mode_element will output into
9795 mode_line_noprop_buf; then display the title. */
9796 record_unwind_protect (unwind_format_mode_line,
9797 format_mode_line_unwind_data
9798 (current_buffer, selected_window, 0));
9799
9800 Fselect_window (f->selected_window, Qt);
9801 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9802 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9803
9804 mode_line_target = MODE_LINE_TITLE;
9805 title_start = MODE_LINE_NOPROP_LEN (0);
9806 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9807 NULL, DEFAULT_FACE_ID);
9808 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9809 len = MODE_LINE_NOPROP_LEN (title_start);
9810 title = mode_line_noprop_buf + title_start;
9811 unbind_to (count, Qnil);
9812
9813 /* Set the title only if it's changed. This avoids consing in
9814 the common case where it hasn't. (If it turns out that we've
9815 already wasted too much time by walking through the list with
9816 display_mode_element, then we might need to optimize at a
9817 higher level than this.) */
9818 if (! STRINGP (f->name)
9819 || SBYTES (f->name) != len
9820 || bcmp (title, SDATA (f->name), len) != 0)
9821 {
9822 #ifdef HAVE_NS
9823 if (FRAME_NS_P (f))
9824 {
9825 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9826 {
9827 if (EQ (fmt, Qt))
9828 ns_set_name_as_filename (f);
9829 else
9830 x_implicitly_set_name (f, make_string(title, len),
9831 Qnil);
9832 }
9833 }
9834 else
9835 #endif
9836 x_implicitly_set_name (f, make_string (title, len), Qnil);
9837 }
9838 #ifdef HAVE_NS
9839 if (FRAME_NS_P (f))
9840 {
9841 /* do this also for frames with explicit names */
9842 ns_implicitly_set_icon_type(f);
9843 ns_set_doc_edited(f, Fbuffer_modified_p
9844 (XWINDOW (f->selected_window)->buffer), Qnil);
9845 }
9846 #endif
9847 }
9848 }
9849
9850 #endif /* not HAVE_WINDOW_SYSTEM */
9851
9852
9853
9854 \f
9855 /***********************************************************************
9856 Menu Bars
9857 ***********************************************************************/
9858
9859
9860 /* Prepare for redisplay by updating menu-bar item lists when
9861 appropriate. This can call eval. */
9862
9863 void
9864 prepare_menu_bars ()
9865 {
9866 int all_windows;
9867 struct gcpro gcpro1, gcpro2;
9868 struct frame *f;
9869 Lisp_Object tooltip_frame;
9870
9871 #ifdef HAVE_WINDOW_SYSTEM
9872 tooltip_frame = tip_frame;
9873 #else
9874 tooltip_frame = Qnil;
9875 #endif
9876
9877 /* Update all frame titles based on their buffer names, etc. We do
9878 this before the menu bars so that the buffer-menu will show the
9879 up-to-date frame titles. */
9880 #ifdef HAVE_WINDOW_SYSTEM
9881 if (windows_or_buffers_changed || update_mode_lines)
9882 {
9883 Lisp_Object tail, frame;
9884
9885 FOR_EACH_FRAME (tail, frame)
9886 {
9887 f = XFRAME (frame);
9888 if (!EQ (frame, tooltip_frame)
9889 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9890 x_consider_frame_title (frame);
9891 }
9892 }
9893 #endif /* HAVE_WINDOW_SYSTEM */
9894
9895 /* Update the menu bar item lists, if appropriate. This has to be
9896 done before any actual redisplay or generation of display lines. */
9897 all_windows = (update_mode_lines
9898 || buffer_shared > 1
9899 || windows_or_buffers_changed);
9900 if (all_windows)
9901 {
9902 Lisp_Object tail, frame;
9903 int count = SPECPDL_INDEX ();
9904 /* 1 means that update_menu_bar has run its hooks
9905 so any further calls to update_menu_bar shouldn't do so again. */
9906 int menu_bar_hooks_run = 0;
9907
9908 record_unwind_save_match_data ();
9909
9910 FOR_EACH_FRAME (tail, frame)
9911 {
9912 f = XFRAME (frame);
9913
9914 /* Ignore tooltip frame. */
9915 if (EQ (frame, tooltip_frame))
9916 continue;
9917
9918 /* If a window on this frame changed size, report that to
9919 the user and clear the size-change flag. */
9920 if (FRAME_WINDOW_SIZES_CHANGED (f))
9921 {
9922 Lisp_Object functions;
9923
9924 /* Clear flag first in case we get an error below. */
9925 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9926 functions = Vwindow_size_change_functions;
9927 GCPRO2 (tail, functions);
9928
9929 while (CONSP (functions))
9930 {
9931 if (!EQ (XCAR (functions), Qt))
9932 call1 (XCAR (functions), frame);
9933 functions = XCDR (functions);
9934 }
9935 UNGCPRO;
9936 }
9937
9938 GCPRO1 (tail);
9939 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9940 #ifdef HAVE_WINDOW_SYSTEM
9941 update_tool_bar (f, 0);
9942 #endif
9943 UNGCPRO;
9944 }
9945
9946 unbind_to (count, Qnil);
9947 }
9948 else
9949 {
9950 struct frame *sf = SELECTED_FRAME ();
9951 update_menu_bar (sf, 1, 0);
9952 #ifdef HAVE_WINDOW_SYSTEM
9953 update_tool_bar (sf, 1);
9954 #endif
9955 }
9956
9957 /* Motif needs this. See comment in xmenu.c. Turn it off when
9958 pending_menu_activation is not defined. */
9959 #ifdef USE_X_TOOLKIT
9960 pending_menu_activation = 0;
9961 #endif
9962 }
9963
9964
9965 /* Update the menu bar item list for frame F. This has to be done
9966 before we start to fill in any display lines, because it can call
9967 eval.
9968
9969 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9970
9971 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9972 already ran the menu bar hooks for this redisplay, so there
9973 is no need to run them again. The return value is the
9974 updated value of this flag, to pass to the next call. */
9975
9976 static int
9977 update_menu_bar (f, save_match_data, hooks_run)
9978 struct frame *f;
9979 int save_match_data;
9980 int hooks_run;
9981 {
9982 Lisp_Object window;
9983 register struct window *w;
9984
9985 /* If called recursively during a menu update, do nothing. This can
9986 happen when, for instance, an activate-menubar-hook causes a
9987 redisplay. */
9988 if (inhibit_menubar_update)
9989 return hooks_run;
9990
9991 window = FRAME_SELECTED_WINDOW (f);
9992 w = XWINDOW (window);
9993
9994 if (FRAME_WINDOW_P (f)
9995 ?
9996 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9997 || defined (HAVE_NS) || defined (USE_GTK)
9998 FRAME_EXTERNAL_MENU_BAR (f)
9999 #else
10000 FRAME_MENU_BAR_LINES (f) > 0
10001 #endif
10002 : FRAME_MENU_BAR_LINES (f) > 0)
10003 {
10004 /* If the user has switched buffers or windows, we need to
10005 recompute to reflect the new bindings. But we'll
10006 recompute when update_mode_lines is set too; that means
10007 that people can use force-mode-line-update to request
10008 that the menu bar be recomputed. The adverse effect on
10009 the rest of the redisplay algorithm is about the same as
10010 windows_or_buffers_changed anyway. */
10011 if (windows_or_buffers_changed
10012 /* This used to test w->update_mode_line, but we believe
10013 there is no need to recompute the menu in that case. */
10014 || update_mode_lines
10015 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10016 < BUF_MODIFF (XBUFFER (w->buffer)))
10017 != !NILP (w->last_had_star))
10018 || ((!NILP (Vtransient_mark_mode)
10019 && !NILP (XBUFFER (w->buffer)->mark_active))
10020 != !NILP (w->region_showing)))
10021 {
10022 struct buffer *prev = current_buffer;
10023 int count = SPECPDL_INDEX ();
10024
10025 specbind (Qinhibit_menubar_update, Qt);
10026
10027 set_buffer_internal_1 (XBUFFER (w->buffer));
10028 if (save_match_data)
10029 record_unwind_save_match_data ();
10030 if (NILP (Voverriding_local_map_menu_flag))
10031 {
10032 specbind (Qoverriding_terminal_local_map, Qnil);
10033 specbind (Qoverriding_local_map, Qnil);
10034 }
10035
10036 if (!hooks_run)
10037 {
10038 /* Run the Lucid hook. */
10039 safe_run_hooks (Qactivate_menubar_hook);
10040
10041 /* If it has changed current-menubar from previous value,
10042 really recompute the menu-bar from the value. */
10043 if (! NILP (Vlucid_menu_bar_dirty_flag))
10044 call0 (Qrecompute_lucid_menubar);
10045
10046 safe_run_hooks (Qmenu_bar_update_hook);
10047
10048 hooks_run = 1;
10049 }
10050
10051 XSETFRAME (Vmenu_updating_frame, f);
10052 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10053
10054 /* Redisplay the menu bar in case we changed it. */
10055 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10056 || defined (HAVE_NS) || defined (USE_GTK)
10057 if (FRAME_WINDOW_P (f))
10058 {
10059 #if defined (HAVE_NS)
10060 /* All frames on Mac OS share the same menubar. So only
10061 the selected frame should be allowed to set it. */
10062 if (f == SELECTED_FRAME ())
10063 #endif
10064 set_frame_menubar (f, 0, 0);
10065 }
10066 else
10067 /* On a terminal screen, the menu bar is an ordinary screen
10068 line, and this makes it get updated. */
10069 w->update_mode_line = Qt;
10070 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10071 /* In the non-toolkit version, the menu bar is an ordinary screen
10072 line, and this makes it get updated. */
10073 w->update_mode_line = Qt;
10074 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10075
10076 unbind_to (count, Qnil);
10077 set_buffer_internal_1 (prev);
10078 }
10079 }
10080
10081 return hooks_run;
10082 }
10083
10084
10085 \f
10086 /***********************************************************************
10087 Output Cursor
10088 ***********************************************************************/
10089
10090 #ifdef HAVE_WINDOW_SYSTEM
10091
10092 /* EXPORT:
10093 Nominal cursor position -- where to draw output.
10094 HPOS and VPOS are window relative glyph matrix coordinates.
10095 X and Y are window relative pixel coordinates. */
10096
10097 struct cursor_pos output_cursor;
10098
10099
10100 /* EXPORT:
10101 Set the global variable output_cursor to CURSOR. All cursor
10102 positions are relative to updated_window. */
10103
10104 void
10105 set_output_cursor (cursor)
10106 struct cursor_pos *cursor;
10107 {
10108 output_cursor.hpos = cursor->hpos;
10109 output_cursor.vpos = cursor->vpos;
10110 output_cursor.x = cursor->x;
10111 output_cursor.y = cursor->y;
10112 }
10113
10114
10115 /* EXPORT for RIF:
10116 Set a nominal cursor position.
10117
10118 HPOS and VPOS are column/row positions in a window glyph matrix. X
10119 and Y are window text area relative pixel positions.
10120
10121 If this is done during an update, updated_window will contain the
10122 window that is being updated and the position is the future output
10123 cursor position for that window. If updated_window is null, use
10124 selected_window and display the cursor at the given position. */
10125
10126 void
10127 x_cursor_to (vpos, hpos, y, x)
10128 int vpos, hpos, y, x;
10129 {
10130 struct window *w;
10131
10132 /* If updated_window is not set, work on selected_window. */
10133 if (updated_window)
10134 w = updated_window;
10135 else
10136 w = XWINDOW (selected_window);
10137
10138 /* Set the output cursor. */
10139 output_cursor.hpos = hpos;
10140 output_cursor.vpos = vpos;
10141 output_cursor.x = x;
10142 output_cursor.y = y;
10143
10144 /* If not called as part of an update, really display the cursor.
10145 This will also set the cursor position of W. */
10146 if (updated_window == NULL)
10147 {
10148 BLOCK_INPUT;
10149 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10150 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10151 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10152 UNBLOCK_INPUT;
10153 }
10154 }
10155
10156 #endif /* HAVE_WINDOW_SYSTEM */
10157
10158 \f
10159 /***********************************************************************
10160 Tool-bars
10161 ***********************************************************************/
10162
10163 #ifdef HAVE_WINDOW_SYSTEM
10164
10165 /* Where the mouse was last time we reported a mouse event. */
10166
10167 FRAME_PTR last_mouse_frame;
10168
10169 /* Tool-bar item index of the item on which a mouse button was pressed
10170 or -1. */
10171
10172 int last_tool_bar_item;
10173
10174
10175 static Lisp_Object
10176 update_tool_bar_unwind (frame)
10177 Lisp_Object frame;
10178 {
10179 selected_frame = frame;
10180 return Qnil;
10181 }
10182
10183 /* Update the tool-bar item list for frame F. This has to be done
10184 before we start to fill in any display lines. Called from
10185 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10186 and restore it here. */
10187
10188 static void
10189 update_tool_bar (f, save_match_data)
10190 struct frame *f;
10191 int save_match_data;
10192 {
10193 #if defined (USE_GTK) || defined (HAVE_NS)
10194 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10195 #else
10196 int do_update = WINDOWP (f->tool_bar_window)
10197 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10198 #endif
10199
10200 if (do_update)
10201 {
10202 Lisp_Object window;
10203 struct window *w;
10204
10205 window = FRAME_SELECTED_WINDOW (f);
10206 w = XWINDOW (window);
10207
10208 /* If the user has switched buffers or windows, we need to
10209 recompute to reflect the new bindings. But we'll
10210 recompute when update_mode_lines is set too; that means
10211 that people can use force-mode-line-update to request
10212 that the menu bar be recomputed. The adverse effect on
10213 the rest of the redisplay algorithm is about the same as
10214 windows_or_buffers_changed anyway. */
10215 if (windows_or_buffers_changed
10216 || !NILP (w->update_mode_line)
10217 || update_mode_lines
10218 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10219 < BUF_MODIFF (XBUFFER (w->buffer)))
10220 != !NILP (w->last_had_star))
10221 || ((!NILP (Vtransient_mark_mode)
10222 && !NILP (XBUFFER (w->buffer)->mark_active))
10223 != !NILP (w->region_showing)))
10224 {
10225 struct buffer *prev = current_buffer;
10226 int count = SPECPDL_INDEX ();
10227 Lisp_Object frame, new_tool_bar;
10228 int new_n_tool_bar;
10229 struct gcpro gcpro1;
10230
10231 /* Set current_buffer to the buffer of the selected
10232 window of the frame, so that we get the right local
10233 keymaps. */
10234 set_buffer_internal_1 (XBUFFER (w->buffer));
10235
10236 /* Save match data, if we must. */
10237 if (save_match_data)
10238 record_unwind_save_match_data ();
10239
10240 /* Make sure that we don't accidentally use bogus keymaps. */
10241 if (NILP (Voverriding_local_map_menu_flag))
10242 {
10243 specbind (Qoverriding_terminal_local_map, Qnil);
10244 specbind (Qoverriding_local_map, Qnil);
10245 }
10246
10247 GCPRO1 (new_tool_bar);
10248
10249 /* We must temporarily set the selected frame to this frame
10250 before calling tool_bar_items, because the calculation of
10251 the tool-bar keymap uses the selected frame (see
10252 `tool-bar-make-keymap' in tool-bar.el). */
10253 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10254 XSETFRAME (frame, f);
10255 selected_frame = frame;
10256
10257 /* Build desired tool-bar items from keymaps. */
10258 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10259 &new_n_tool_bar);
10260
10261 /* Redisplay the tool-bar if we changed it. */
10262 if (new_n_tool_bar != f->n_tool_bar_items
10263 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10264 {
10265 /* Redisplay that happens asynchronously due to an expose event
10266 may access f->tool_bar_items. Make sure we update both
10267 variables within BLOCK_INPUT so no such event interrupts. */
10268 BLOCK_INPUT;
10269 f->tool_bar_items = new_tool_bar;
10270 f->n_tool_bar_items = new_n_tool_bar;
10271 w->update_mode_line = Qt;
10272 UNBLOCK_INPUT;
10273 }
10274
10275 UNGCPRO;
10276
10277 unbind_to (count, Qnil);
10278 set_buffer_internal_1 (prev);
10279 }
10280 }
10281 }
10282
10283
10284 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10285 F's desired tool-bar contents. F->tool_bar_items must have
10286 been set up previously by calling prepare_menu_bars. */
10287
10288 static void
10289 build_desired_tool_bar_string (f)
10290 struct frame *f;
10291 {
10292 int i, size, size_needed;
10293 struct gcpro gcpro1, gcpro2, gcpro3;
10294 Lisp_Object image, plist, props;
10295
10296 image = plist = props = Qnil;
10297 GCPRO3 (image, plist, props);
10298
10299 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10300 Otherwise, make a new string. */
10301
10302 /* The size of the string we might be able to reuse. */
10303 size = (STRINGP (f->desired_tool_bar_string)
10304 ? SCHARS (f->desired_tool_bar_string)
10305 : 0);
10306
10307 /* We need one space in the string for each image. */
10308 size_needed = f->n_tool_bar_items;
10309
10310 /* Reuse f->desired_tool_bar_string, if possible. */
10311 if (size < size_needed || NILP (f->desired_tool_bar_string))
10312 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10313 make_number (' '));
10314 else
10315 {
10316 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10317 Fremove_text_properties (make_number (0), make_number (size),
10318 props, f->desired_tool_bar_string);
10319 }
10320
10321 /* Put a `display' property on the string for the images to display,
10322 put a `menu_item' property on tool-bar items with a value that
10323 is the index of the item in F's tool-bar item vector. */
10324 for (i = 0; i < f->n_tool_bar_items; ++i)
10325 {
10326 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10327
10328 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10329 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10330 int hmargin, vmargin, relief, idx, end;
10331 extern Lisp_Object QCrelief, QCmargin, QCconversion;
10332
10333 /* If image is a vector, choose the image according to the
10334 button state. */
10335 image = PROP (TOOL_BAR_ITEM_IMAGES);
10336 if (VECTORP (image))
10337 {
10338 if (enabled_p)
10339 idx = (selected_p
10340 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10341 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10342 else
10343 idx = (selected_p
10344 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10345 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10346
10347 xassert (ASIZE (image) >= idx);
10348 image = AREF (image, idx);
10349 }
10350 else
10351 idx = -1;
10352
10353 /* Ignore invalid image specifications. */
10354 if (!valid_image_p (image))
10355 continue;
10356
10357 /* Display the tool-bar button pressed, or depressed. */
10358 plist = Fcopy_sequence (XCDR (image));
10359
10360 /* Compute margin and relief to draw. */
10361 relief = (tool_bar_button_relief >= 0
10362 ? tool_bar_button_relief
10363 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10364 hmargin = vmargin = relief;
10365
10366 if (INTEGERP (Vtool_bar_button_margin)
10367 && XINT (Vtool_bar_button_margin) > 0)
10368 {
10369 hmargin += XFASTINT (Vtool_bar_button_margin);
10370 vmargin += XFASTINT (Vtool_bar_button_margin);
10371 }
10372 else if (CONSP (Vtool_bar_button_margin))
10373 {
10374 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10375 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10376 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10377
10378 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10379 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10380 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10381 }
10382
10383 if (auto_raise_tool_bar_buttons_p)
10384 {
10385 /* Add a `:relief' property to the image spec if the item is
10386 selected. */
10387 if (selected_p)
10388 {
10389 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10390 hmargin -= relief;
10391 vmargin -= relief;
10392 }
10393 }
10394 else
10395 {
10396 /* If image is selected, display it pressed, i.e. with a
10397 negative relief. If it's not selected, display it with a
10398 raised relief. */
10399 plist = Fplist_put (plist, QCrelief,
10400 (selected_p
10401 ? make_number (-relief)
10402 : make_number (relief)));
10403 hmargin -= relief;
10404 vmargin -= relief;
10405 }
10406
10407 /* Put a margin around the image. */
10408 if (hmargin || vmargin)
10409 {
10410 if (hmargin == vmargin)
10411 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10412 else
10413 plist = Fplist_put (plist, QCmargin,
10414 Fcons (make_number (hmargin),
10415 make_number (vmargin)));
10416 }
10417
10418 /* If button is not enabled, and we don't have special images
10419 for the disabled state, make the image appear disabled by
10420 applying an appropriate algorithm to it. */
10421 if (!enabled_p && idx < 0)
10422 plist = Fplist_put (plist, QCconversion, Qdisabled);
10423
10424 /* Put a `display' text property on the string for the image to
10425 display. Put a `menu-item' property on the string that gives
10426 the start of this item's properties in the tool-bar items
10427 vector. */
10428 image = Fcons (Qimage, plist);
10429 props = list4 (Qdisplay, image,
10430 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10431
10432 /* Let the last image hide all remaining spaces in the tool bar
10433 string. The string can be longer than needed when we reuse a
10434 previous string. */
10435 if (i + 1 == f->n_tool_bar_items)
10436 end = SCHARS (f->desired_tool_bar_string);
10437 else
10438 end = i + 1;
10439 Fadd_text_properties (make_number (i), make_number (end),
10440 props, f->desired_tool_bar_string);
10441 #undef PROP
10442 }
10443
10444 UNGCPRO;
10445 }
10446
10447
10448 /* Display one line of the tool-bar of frame IT->f.
10449
10450 HEIGHT specifies the desired height of the tool-bar line.
10451 If the actual height of the glyph row is less than HEIGHT, the
10452 row's height is increased to HEIGHT, and the icons are centered
10453 vertically in the new height.
10454
10455 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10456 count a final empty row in case the tool-bar width exactly matches
10457 the window width.
10458 */
10459
10460 static void
10461 display_tool_bar_line (it, height)
10462 struct it *it;
10463 int height;
10464 {
10465 struct glyph_row *row = it->glyph_row;
10466 int max_x = it->last_visible_x;
10467 struct glyph *last;
10468
10469 prepare_desired_row (row);
10470 row->y = it->current_y;
10471
10472 /* Note that this isn't made use of if the face hasn't a box,
10473 so there's no need to check the face here. */
10474 it->start_of_box_run_p = 1;
10475
10476 while (it->current_x < max_x)
10477 {
10478 int x, n_glyphs_before, i, nglyphs;
10479 struct it it_before;
10480
10481 /* Get the next display element. */
10482 if (!get_next_display_element (it))
10483 {
10484 /* Don't count empty row if we are counting needed tool-bar lines. */
10485 if (height < 0 && !it->hpos)
10486 return;
10487 break;
10488 }
10489
10490 /* Produce glyphs. */
10491 n_glyphs_before = row->used[TEXT_AREA];
10492 it_before = *it;
10493
10494 PRODUCE_GLYPHS (it);
10495
10496 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10497 i = 0;
10498 x = it_before.current_x;
10499 while (i < nglyphs)
10500 {
10501 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10502
10503 if (x + glyph->pixel_width > max_x)
10504 {
10505 /* Glyph doesn't fit on line. Backtrack. */
10506 row->used[TEXT_AREA] = n_glyphs_before;
10507 *it = it_before;
10508 /* If this is the only glyph on this line, it will never fit on the
10509 toolbar, so skip it. But ensure there is at least one glyph,
10510 so we don't accidentally disable the tool-bar. */
10511 if (n_glyphs_before == 0
10512 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10513 break;
10514 goto out;
10515 }
10516
10517 ++it->hpos;
10518 x += glyph->pixel_width;
10519 ++i;
10520 }
10521
10522 /* Stop at line ends. */
10523 if (ITERATOR_AT_END_OF_LINE_P (it))
10524 break;
10525
10526 set_iterator_to_next (it, 1);
10527 }
10528
10529 out:;
10530
10531 row->displays_text_p = row->used[TEXT_AREA] != 0;
10532
10533 /* Use default face for the border below the tool bar.
10534
10535 FIXME: When auto-resize-tool-bars is grow-only, there is
10536 no additional border below the possibly empty tool-bar lines.
10537 So to make the extra empty lines look "normal", we have to
10538 use the tool-bar face for the border too. */
10539 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10540 it->face_id = DEFAULT_FACE_ID;
10541
10542 extend_face_to_end_of_line (it);
10543 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10544 last->right_box_line_p = 1;
10545 if (last == row->glyphs[TEXT_AREA])
10546 last->left_box_line_p = 1;
10547
10548 /* Make line the desired height and center it vertically. */
10549 if ((height -= it->max_ascent + it->max_descent) > 0)
10550 {
10551 /* Don't add more than one line height. */
10552 height %= FRAME_LINE_HEIGHT (it->f);
10553 it->max_ascent += height / 2;
10554 it->max_descent += (height + 1) / 2;
10555 }
10556
10557 compute_line_metrics (it);
10558
10559 /* If line is empty, make it occupy the rest of the tool-bar. */
10560 if (!row->displays_text_p)
10561 {
10562 row->height = row->phys_height = it->last_visible_y - row->y;
10563 row->visible_height = row->height;
10564 row->ascent = row->phys_ascent = 0;
10565 row->extra_line_spacing = 0;
10566 }
10567
10568 row->full_width_p = 1;
10569 row->continued_p = 0;
10570 row->truncated_on_left_p = 0;
10571 row->truncated_on_right_p = 0;
10572
10573 it->current_x = it->hpos = 0;
10574 it->current_y += row->height;
10575 ++it->vpos;
10576 ++it->glyph_row;
10577 }
10578
10579
10580 /* Max tool-bar height. */
10581
10582 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10583 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10584
10585 /* Value is the number of screen lines needed to make all tool-bar
10586 items of frame F visible. The number of actual rows needed is
10587 returned in *N_ROWS if non-NULL. */
10588
10589 static int
10590 tool_bar_lines_needed (f, n_rows)
10591 struct frame *f;
10592 int *n_rows;
10593 {
10594 struct window *w = XWINDOW (f->tool_bar_window);
10595 struct it it;
10596 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10597 the desired matrix, so use (unused) mode-line row as temporary row to
10598 avoid destroying the first tool-bar row. */
10599 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10600
10601 /* Initialize an iterator for iteration over
10602 F->desired_tool_bar_string in the tool-bar window of frame F. */
10603 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10604 it.first_visible_x = 0;
10605 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10606 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10607
10608 while (!ITERATOR_AT_END_P (&it))
10609 {
10610 clear_glyph_row (temp_row);
10611 it.glyph_row = temp_row;
10612 display_tool_bar_line (&it, -1);
10613 }
10614 clear_glyph_row (temp_row);
10615
10616 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10617 if (n_rows)
10618 *n_rows = it.vpos > 0 ? it.vpos : -1;
10619
10620 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10621 }
10622
10623
10624 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10625 0, 1, 0,
10626 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10627 (frame)
10628 Lisp_Object frame;
10629 {
10630 struct frame *f;
10631 struct window *w;
10632 int nlines = 0;
10633
10634 if (NILP (frame))
10635 frame = selected_frame;
10636 else
10637 CHECK_FRAME (frame);
10638 f = XFRAME (frame);
10639
10640 if (WINDOWP (f->tool_bar_window)
10641 || (w = XWINDOW (f->tool_bar_window),
10642 WINDOW_TOTAL_LINES (w) > 0))
10643 {
10644 update_tool_bar (f, 1);
10645 if (f->n_tool_bar_items)
10646 {
10647 build_desired_tool_bar_string (f);
10648 nlines = tool_bar_lines_needed (f, NULL);
10649 }
10650 }
10651
10652 return make_number (nlines);
10653 }
10654
10655
10656 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10657 height should be changed. */
10658
10659 static int
10660 redisplay_tool_bar (f)
10661 struct frame *f;
10662 {
10663 struct window *w;
10664 struct it it;
10665 struct glyph_row *row;
10666
10667 #if defined (USE_GTK) || defined (HAVE_NS)
10668 if (FRAME_EXTERNAL_TOOL_BAR (f))
10669 update_frame_tool_bar (f);
10670 return 0;
10671 #endif
10672
10673 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10674 do anything. This means you must start with tool-bar-lines
10675 non-zero to get the auto-sizing effect. Or in other words, you
10676 can turn off tool-bars by specifying tool-bar-lines zero. */
10677 if (!WINDOWP (f->tool_bar_window)
10678 || (w = XWINDOW (f->tool_bar_window),
10679 WINDOW_TOTAL_LINES (w) == 0))
10680 return 0;
10681
10682 /* Set up an iterator for the tool-bar window. */
10683 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10684 it.first_visible_x = 0;
10685 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10686 row = it.glyph_row;
10687
10688 /* Build a string that represents the contents of the tool-bar. */
10689 build_desired_tool_bar_string (f);
10690 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10691
10692 if (f->n_tool_bar_rows == 0)
10693 {
10694 int nlines;
10695
10696 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10697 nlines != WINDOW_TOTAL_LINES (w)))
10698 {
10699 extern Lisp_Object Qtool_bar_lines;
10700 Lisp_Object frame;
10701 int old_height = WINDOW_TOTAL_LINES (w);
10702
10703 XSETFRAME (frame, f);
10704 Fmodify_frame_parameters (frame,
10705 Fcons (Fcons (Qtool_bar_lines,
10706 make_number (nlines)),
10707 Qnil));
10708 if (WINDOW_TOTAL_LINES (w) != old_height)
10709 {
10710 clear_glyph_matrix (w->desired_matrix);
10711 fonts_changed_p = 1;
10712 return 1;
10713 }
10714 }
10715 }
10716
10717 /* Display as many lines as needed to display all tool-bar items. */
10718
10719 if (f->n_tool_bar_rows > 0)
10720 {
10721 int border, rows, height, extra;
10722
10723 if (INTEGERP (Vtool_bar_border))
10724 border = XINT (Vtool_bar_border);
10725 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10726 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10727 else if (EQ (Vtool_bar_border, Qborder_width))
10728 border = f->border_width;
10729 else
10730 border = 0;
10731 if (border < 0)
10732 border = 0;
10733
10734 rows = f->n_tool_bar_rows;
10735 height = max (1, (it.last_visible_y - border) / rows);
10736 extra = it.last_visible_y - border - height * rows;
10737
10738 while (it.current_y < it.last_visible_y)
10739 {
10740 int h = 0;
10741 if (extra > 0 && rows-- > 0)
10742 {
10743 h = (extra + rows - 1) / rows;
10744 extra -= h;
10745 }
10746 display_tool_bar_line (&it, height + h);
10747 }
10748 }
10749 else
10750 {
10751 while (it.current_y < it.last_visible_y)
10752 display_tool_bar_line (&it, 0);
10753 }
10754
10755 /* It doesn't make much sense to try scrolling in the tool-bar
10756 window, so don't do it. */
10757 w->desired_matrix->no_scrolling_p = 1;
10758 w->must_be_updated_p = 1;
10759
10760 if (!NILP (Vauto_resize_tool_bars))
10761 {
10762 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10763 int change_height_p = 0;
10764
10765 /* If we couldn't display everything, change the tool-bar's
10766 height if there is room for more. */
10767 if (IT_STRING_CHARPOS (it) < it.end_charpos
10768 && it.current_y < max_tool_bar_height)
10769 change_height_p = 1;
10770
10771 row = it.glyph_row - 1;
10772
10773 /* If there are blank lines at the end, except for a partially
10774 visible blank line at the end that is smaller than
10775 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10776 if (!row->displays_text_p
10777 && row->height >= FRAME_LINE_HEIGHT (f))
10778 change_height_p = 1;
10779
10780 /* If row displays tool-bar items, but is partially visible,
10781 change the tool-bar's height. */
10782 if (row->displays_text_p
10783 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10784 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10785 change_height_p = 1;
10786
10787 /* Resize windows as needed by changing the `tool-bar-lines'
10788 frame parameter. */
10789 if (change_height_p)
10790 {
10791 extern Lisp_Object Qtool_bar_lines;
10792 Lisp_Object frame;
10793 int old_height = WINDOW_TOTAL_LINES (w);
10794 int nrows;
10795 int nlines = tool_bar_lines_needed (f, &nrows);
10796
10797 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10798 && !f->minimize_tool_bar_window_p)
10799 ? (nlines > old_height)
10800 : (nlines != old_height));
10801 f->minimize_tool_bar_window_p = 0;
10802
10803 if (change_height_p)
10804 {
10805 XSETFRAME (frame, f);
10806 Fmodify_frame_parameters (frame,
10807 Fcons (Fcons (Qtool_bar_lines,
10808 make_number (nlines)),
10809 Qnil));
10810 if (WINDOW_TOTAL_LINES (w) != old_height)
10811 {
10812 clear_glyph_matrix (w->desired_matrix);
10813 f->n_tool_bar_rows = nrows;
10814 fonts_changed_p = 1;
10815 return 1;
10816 }
10817 }
10818 }
10819 }
10820
10821 f->minimize_tool_bar_window_p = 0;
10822 return 0;
10823 }
10824
10825
10826 /* Get information about the tool-bar item which is displayed in GLYPH
10827 on frame F. Return in *PROP_IDX the index where tool-bar item
10828 properties start in F->tool_bar_items. Value is zero if
10829 GLYPH doesn't display a tool-bar item. */
10830
10831 static int
10832 tool_bar_item_info (f, glyph, prop_idx)
10833 struct frame *f;
10834 struct glyph *glyph;
10835 int *prop_idx;
10836 {
10837 Lisp_Object prop;
10838 int success_p;
10839 int charpos;
10840
10841 /* This function can be called asynchronously, which means we must
10842 exclude any possibility that Fget_text_property signals an
10843 error. */
10844 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10845 charpos = max (0, charpos);
10846
10847 /* Get the text property `menu-item' at pos. The value of that
10848 property is the start index of this item's properties in
10849 F->tool_bar_items. */
10850 prop = Fget_text_property (make_number (charpos),
10851 Qmenu_item, f->current_tool_bar_string);
10852 if (INTEGERP (prop))
10853 {
10854 *prop_idx = XINT (prop);
10855 success_p = 1;
10856 }
10857 else
10858 success_p = 0;
10859
10860 return success_p;
10861 }
10862
10863 \f
10864 /* Get information about the tool-bar item at position X/Y on frame F.
10865 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10866 the current matrix of the tool-bar window of F, or NULL if not
10867 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10868 item in F->tool_bar_items. Value is
10869
10870 -1 if X/Y is not on a tool-bar item
10871 0 if X/Y is on the same item that was highlighted before.
10872 1 otherwise. */
10873
10874 static int
10875 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10876 struct frame *f;
10877 int x, y;
10878 struct glyph **glyph;
10879 int *hpos, *vpos, *prop_idx;
10880 {
10881 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10882 struct window *w = XWINDOW (f->tool_bar_window);
10883 int area;
10884
10885 /* Find the glyph under X/Y. */
10886 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10887 if (*glyph == NULL)
10888 return -1;
10889
10890 /* Get the start of this tool-bar item's properties in
10891 f->tool_bar_items. */
10892 if (!tool_bar_item_info (f, *glyph, prop_idx))
10893 return -1;
10894
10895 /* Is mouse on the highlighted item? */
10896 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10897 && *vpos >= dpyinfo->mouse_face_beg_row
10898 && *vpos <= dpyinfo->mouse_face_end_row
10899 && (*vpos > dpyinfo->mouse_face_beg_row
10900 || *hpos >= dpyinfo->mouse_face_beg_col)
10901 && (*vpos < dpyinfo->mouse_face_end_row
10902 || *hpos < dpyinfo->mouse_face_end_col
10903 || dpyinfo->mouse_face_past_end))
10904 return 0;
10905
10906 return 1;
10907 }
10908
10909
10910 /* EXPORT:
10911 Handle mouse button event on the tool-bar of frame F, at
10912 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10913 0 for button release. MODIFIERS is event modifiers for button
10914 release. */
10915
10916 void
10917 handle_tool_bar_click (f, x, y, down_p, modifiers)
10918 struct frame *f;
10919 int x, y, down_p;
10920 unsigned int modifiers;
10921 {
10922 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10923 struct window *w = XWINDOW (f->tool_bar_window);
10924 int hpos, vpos, prop_idx;
10925 struct glyph *glyph;
10926 Lisp_Object enabled_p;
10927
10928 /* If not on the highlighted tool-bar item, return. */
10929 frame_to_window_pixel_xy (w, &x, &y);
10930 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10931 return;
10932
10933 /* If item is disabled, do nothing. */
10934 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10935 if (NILP (enabled_p))
10936 return;
10937
10938 if (down_p)
10939 {
10940 /* Show item in pressed state. */
10941 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10942 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10943 last_tool_bar_item = prop_idx;
10944 }
10945 else
10946 {
10947 Lisp_Object key, frame;
10948 struct input_event event;
10949 EVENT_INIT (event);
10950
10951 /* Show item in released state. */
10952 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10953 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10954
10955 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10956
10957 XSETFRAME (frame, f);
10958 event.kind = TOOL_BAR_EVENT;
10959 event.frame_or_window = frame;
10960 event.arg = frame;
10961 kbd_buffer_store_event (&event);
10962
10963 event.kind = TOOL_BAR_EVENT;
10964 event.frame_or_window = frame;
10965 event.arg = key;
10966 event.modifiers = modifiers;
10967 kbd_buffer_store_event (&event);
10968 last_tool_bar_item = -1;
10969 }
10970 }
10971
10972
10973 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10974 tool-bar window-relative coordinates X/Y. Called from
10975 note_mouse_highlight. */
10976
10977 static void
10978 note_tool_bar_highlight (f, x, y)
10979 struct frame *f;
10980 int x, y;
10981 {
10982 Lisp_Object window = f->tool_bar_window;
10983 struct window *w = XWINDOW (window);
10984 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10985 int hpos, vpos;
10986 struct glyph *glyph;
10987 struct glyph_row *row;
10988 int i;
10989 Lisp_Object enabled_p;
10990 int prop_idx;
10991 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10992 int mouse_down_p, rc;
10993
10994 /* Function note_mouse_highlight is called with negative x(y
10995 values when mouse moves outside of the frame. */
10996 if (x <= 0 || y <= 0)
10997 {
10998 clear_mouse_face (dpyinfo);
10999 return;
11000 }
11001
11002 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11003 if (rc < 0)
11004 {
11005 /* Not on tool-bar item. */
11006 clear_mouse_face (dpyinfo);
11007 return;
11008 }
11009 else if (rc == 0)
11010 /* On same tool-bar item as before. */
11011 goto set_help_echo;
11012
11013 clear_mouse_face (dpyinfo);
11014
11015 /* Mouse is down, but on different tool-bar item? */
11016 mouse_down_p = (dpyinfo->grabbed
11017 && f == last_mouse_frame
11018 && FRAME_LIVE_P (f));
11019 if (mouse_down_p
11020 && last_tool_bar_item != prop_idx)
11021 return;
11022
11023 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11024 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11025
11026 /* If tool-bar item is not enabled, don't highlight it. */
11027 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11028 if (!NILP (enabled_p))
11029 {
11030 /* Compute the x-position of the glyph. In front and past the
11031 image is a space. We include this in the highlighted area. */
11032 row = MATRIX_ROW (w->current_matrix, vpos);
11033 for (i = x = 0; i < hpos; ++i)
11034 x += row->glyphs[TEXT_AREA][i].pixel_width;
11035
11036 /* Record this as the current active region. */
11037 dpyinfo->mouse_face_beg_col = hpos;
11038 dpyinfo->mouse_face_beg_row = vpos;
11039 dpyinfo->mouse_face_beg_x = x;
11040 dpyinfo->mouse_face_beg_y = row->y;
11041 dpyinfo->mouse_face_past_end = 0;
11042
11043 dpyinfo->mouse_face_end_col = hpos + 1;
11044 dpyinfo->mouse_face_end_row = vpos;
11045 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
11046 dpyinfo->mouse_face_end_y = row->y;
11047 dpyinfo->mouse_face_window = window;
11048 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11049
11050 /* Display it as active. */
11051 show_mouse_face (dpyinfo, draw);
11052 dpyinfo->mouse_face_image_state = draw;
11053 }
11054
11055 set_help_echo:
11056
11057 /* Set help_echo_string to a help string to display for this tool-bar item.
11058 XTread_socket does the rest. */
11059 help_echo_object = help_echo_window = Qnil;
11060 help_echo_pos = -1;
11061 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11062 if (NILP (help_echo_string))
11063 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11064 }
11065
11066 #endif /* HAVE_WINDOW_SYSTEM */
11067
11068
11069 \f
11070 /************************************************************************
11071 Horizontal scrolling
11072 ************************************************************************/
11073
11074 static int hscroll_window_tree P_ ((Lisp_Object));
11075 static int hscroll_windows P_ ((Lisp_Object));
11076
11077 /* For all leaf windows in the window tree rooted at WINDOW, set their
11078 hscroll value so that PT is (i) visible in the window, and (ii) so
11079 that it is not within a certain margin at the window's left and
11080 right border. Value is non-zero if any window's hscroll has been
11081 changed. */
11082
11083 static int
11084 hscroll_window_tree (window)
11085 Lisp_Object window;
11086 {
11087 int hscrolled_p = 0;
11088 int hscroll_relative_p = FLOATP (Vhscroll_step);
11089 int hscroll_step_abs = 0;
11090 double hscroll_step_rel = 0;
11091
11092 if (hscroll_relative_p)
11093 {
11094 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11095 if (hscroll_step_rel < 0)
11096 {
11097 hscroll_relative_p = 0;
11098 hscroll_step_abs = 0;
11099 }
11100 }
11101 else if (INTEGERP (Vhscroll_step))
11102 {
11103 hscroll_step_abs = XINT (Vhscroll_step);
11104 if (hscroll_step_abs < 0)
11105 hscroll_step_abs = 0;
11106 }
11107 else
11108 hscroll_step_abs = 0;
11109
11110 while (WINDOWP (window))
11111 {
11112 struct window *w = XWINDOW (window);
11113
11114 if (WINDOWP (w->hchild))
11115 hscrolled_p |= hscroll_window_tree (w->hchild);
11116 else if (WINDOWP (w->vchild))
11117 hscrolled_p |= hscroll_window_tree (w->vchild);
11118 else if (w->cursor.vpos >= 0)
11119 {
11120 int h_margin;
11121 int text_area_width;
11122 struct glyph_row *current_cursor_row
11123 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11124 struct glyph_row *desired_cursor_row
11125 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11126 struct glyph_row *cursor_row
11127 = (desired_cursor_row->enabled_p
11128 ? desired_cursor_row
11129 : current_cursor_row);
11130
11131 text_area_width = window_box_width (w, TEXT_AREA);
11132
11133 /* Scroll when cursor is inside this scroll margin. */
11134 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11135
11136 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11137 && ((XFASTINT (w->hscroll)
11138 && w->cursor.x <= h_margin)
11139 || (cursor_row->enabled_p
11140 && cursor_row->truncated_on_right_p
11141 && (w->cursor.x >= text_area_width - h_margin))))
11142 {
11143 struct it it;
11144 int hscroll;
11145 struct buffer *saved_current_buffer;
11146 int pt;
11147 int wanted_x;
11148
11149 /* Find point in a display of infinite width. */
11150 saved_current_buffer = current_buffer;
11151 current_buffer = XBUFFER (w->buffer);
11152
11153 if (w == XWINDOW (selected_window))
11154 pt = BUF_PT (current_buffer);
11155 else
11156 {
11157 pt = marker_position (w->pointm);
11158 pt = max (BEGV, pt);
11159 pt = min (ZV, pt);
11160 }
11161
11162 /* Move iterator to pt starting at cursor_row->start in
11163 a line with infinite width. */
11164 init_to_row_start (&it, w, cursor_row);
11165 it.last_visible_x = INFINITY;
11166 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11167 current_buffer = saved_current_buffer;
11168
11169 /* Position cursor in window. */
11170 if (!hscroll_relative_p && hscroll_step_abs == 0)
11171 hscroll = max (0, (it.current_x
11172 - (ITERATOR_AT_END_OF_LINE_P (&it)
11173 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11174 : (text_area_width / 2))))
11175 / FRAME_COLUMN_WIDTH (it.f);
11176 else if (w->cursor.x >= text_area_width - h_margin)
11177 {
11178 if (hscroll_relative_p)
11179 wanted_x = text_area_width * (1 - hscroll_step_rel)
11180 - h_margin;
11181 else
11182 wanted_x = text_area_width
11183 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11184 - h_margin;
11185 hscroll
11186 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11187 }
11188 else
11189 {
11190 if (hscroll_relative_p)
11191 wanted_x = text_area_width * hscroll_step_rel
11192 + h_margin;
11193 else
11194 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11195 + h_margin;
11196 hscroll
11197 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11198 }
11199 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11200
11201 /* Don't call Fset_window_hscroll if value hasn't
11202 changed because it will prevent redisplay
11203 optimizations. */
11204 if (XFASTINT (w->hscroll) != hscroll)
11205 {
11206 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11207 w->hscroll = make_number (hscroll);
11208 hscrolled_p = 1;
11209 }
11210 }
11211 }
11212
11213 window = w->next;
11214 }
11215
11216 /* Value is non-zero if hscroll of any leaf window has been changed. */
11217 return hscrolled_p;
11218 }
11219
11220
11221 /* Set hscroll so that cursor is visible and not inside horizontal
11222 scroll margins for all windows in the tree rooted at WINDOW. See
11223 also hscroll_window_tree above. Value is non-zero if any window's
11224 hscroll has been changed. If it has, desired matrices on the frame
11225 of WINDOW are cleared. */
11226
11227 static int
11228 hscroll_windows (window)
11229 Lisp_Object window;
11230 {
11231 int hscrolled_p = hscroll_window_tree (window);
11232 if (hscrolled_p)
11233 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11234 return hscrolled_p;
11235 }
11236
11237
11238 \f
11239 /************************************************************************
11240 Redisplay
11241 ************************************************************************/
11242
11243 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11244 to a non-zero value. This is sometimes handy to have in a debugger
11245 session. */
11246
11247 #if GLYPH_DEBUG
11248
11249 /* First and last unchanged row for try_window_id. */
11250
11251 int debug_first_unchanged_at_end_vpos;
11252 int debug_last_unchanged_at_beg_vpos;
11253
11254 /* Delta vpos and y. */
11255
11256 int debug_dvpos, debug_dy;
11257
11258 /* Delta in characters and bytes for try_window_id. */
11259
11260 int debug_delta, debug_delta_bytes;
11261
11262 /* Values of window_end_pos and window_end_vpos at the end of
11263 try_window_id. */
11264
11265 EMACS_INT debug_end_pos, debug_end_vpos;
11266
11267 /* Append a string to W->desired_matrix->method. FMT is a printf
11268 format string. A1...A9 are a supplement for a variable-length
11269 argument list. If trace_redisplay_p is non-zero also printf the
11270 resulting string to stderr. */
11271
11272 static void
11273 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11274 struct window *w;
11275 char *fmt;
11276 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11277 {
11278 char buffer[512];
11279 char *method = w->desired_matrix->method;
11280 int len = strlen (method);
11281 int size = sizeof w->desired_matrix->method;
11282 int remaining = size - len - 1;
11283
11284 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11285 if (len && remaining)
11286 {
11287 method[len] = '|';
11288 --remaining, ++len;
11289 }
11290
11291 strncpy (method + len, buffer, remaining);
11292
11293 if (trace_redisplay_p)
11294 fprintf (stderr, "%p (%s): %s\n",
11295 w,
11296 ((BUFFERP (w->buffer)
11297 && STRINGP (XBUFFER (w->buffer)->name))
11298 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11299 : "no buffer"),
11300 buffer);
11301 }
11302
11303 #endif /* GLYPH_DEBUG */
11304
11305
11306 /* Value is non-zero if all changes in window W, which displays
11307 current_buffer, are in the text between START and END. START is a
11308 buffer position, END is given as a distance from Z. Used in
11309 redisplay_internal for display optimization. */
11310
11311 static INLINE int
11312 text_outside_line_unchanged_p (w, start, end)
11313 struct window *w;
11314 int start, end;
11315 {
11316 int unchanged_p = 1;
11317
11318 /* If text or overlays have changed, see where. */
11319 if (XFASTINT (w->last_modified) < MODIFF
11320 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11321 {
11322 /* Gap in the line? */
11323 if (GPT < start || Z - GPT < end)
11324 unchanged_p = 0;
11325
11326 /* Changes start in front of the line, or end after it? */
11327 if (unchanged_p
11328 && (BEG_UNCHANGED < start - 1
11329 || END_UNCHANGED < end))
11330 unchanged_p = 0;
11331
11332 /* If selective display, can't optimize if changes start at the
11333 beginning of the line. */
11334 if (unchanged_p
11335 && INTEGERP (current_buffer->selective_display)
11336 && XINT (current_buffer->selective_display) > 0
11337 && (BEG_UNCHANGED < start || GPT <= start))
11338 unchanged_p = 0;
11339
11340 /* If there are overlays at the start or end of the line, these
11341 may have overlay strings with newlines in them. A change at
11342 START, for instance, may actually concern the display of such
11343 overlay strings as well, and they are displayed on different
11344 lines. So, quickly rule out this case. (For the future, it
11345 might be desirable to implement something more telling than
11346 just BEG/END_UNCHANGED.) */
11347 if (unchanged_p)
11348 {
11349 if (BEG + BEG_UNCHANGED == start
11350 && overlay_touches_p (start))
11351 unchanged_p = 0;
11352 if (END_UNCHANGED == end
11353 && overlay_touches_p (Z - end))
11354 unchanged_p = 0;
11355 }
11356
11357 /* Under bidi reordering, adding or deleting a character in the
11358 beginning of a paragraph, before the first strong directional
11359 character, can change the base direction of the paragraph (unless
11360 the buffer specifies a fixed paragraph direction), which will
11361 require to redisplay the whole paragraph. It might be worthwhile
11362 to find the paragraph limits and widen the range of redisplayed
11363 lines to that, but for now just give up this optimization. */
11364 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11365 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11366 unchanged_p = 0;
11367 }
11368
11369 return unchanged_p;
11370 }
11371
11372
11373 /* Do a frame update, taking possible shortcuts into account. This is
11374 the main external entry point for redisplay.
11375
11376 If the last redisplay displayed an echo area message and that message
11377 is no longer requested, we clear the echo area or bring back the
11378 mini-buffer if that is in use. */
11379
11380 void
11381 redisplay ()
11382 {
11383 redisplay_internal (0);
11384 }
11385
11386
11387 static Lisp_Object
11388 overlay_arrow_string_or_property (var)
11389 Lisp_Object var;
11390 {
11391 Lisp_Object val;
11392
11393 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11394 return val;
11395
11396 return Voverlay_arrow_string;
11397 }
11398
11399 /* Return 1 if there are any overlay-arrows in current_buffer. */
11400 static int
11401 overlay_arrow_in_current_buffer_p ()
11402 {
11403 Lisp_Object vlist;
11404
11405 for (vlist = Voverlay_arrow_variable_list;
11406 CONSP (vlist);
11407 vlist = XCDR (vlist))
11408 {
11409 Lisp_Object var = XCAR (vlist);
11410 Lisp_Object val;
11411
11412 if (!SYMBOLP (var))
11413 continue;
11414 val = find_symbol_value (var);
11415 if (MARKERP (val)
11416 && current_buffer == XMARKER (val)->buffer)
11417 return 1;
11418 }
11419 return 0;
11420 }
11421
11422
11423 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11424 has changed. */
11425
11426 static int
11427 overlay_arrows_changed_p ()
11428 {
11429 Lisp_Object vlist;
11430
11431 for (vlist = Voverlay_arrow_variable_list;
11432 CONSP (vlist);
11433 vlist = XCDR (vlist))
11434 {
11435 Lisp_Object var = XCAR (vlist);
11436 Lisp_Object val, pstr;
11437
11438 if (!SYMBOLP (var))
11439 continue;
11440 val = find_symbol_value (var);
11441 if (!MARKERP (val))
11442 continue;
11443 if (! EQ (COERCE_MARKER (val),
11444 Fget (var, Qlast_arrow_position))
11445 || ! (pstr = overlay_arrow_string_or_property (var),
11446 EQ (pstr, Fget (var, Qlast_arrow_string))))
11447 return 1;
11448 }
11449 return 0;
11450 }
11451
11452 /* Mark overlay arrows to be updated on next redisplay. */
11453
11454 static void
11455 update_overlay_arrows (up_to_date)
11456 int up_to_date;
11457 {
11458 Lisp_Object vlist;
11459
11460 for (vlist = Voverlay_arrow_variable_list;
11461 CONSP (vlist);
11462 vlist = XCDR (vlist))
11463 {
11464 Lisp_Object var = XCAR (vlist);
11465
11466 if (!SYMBOLP (var))
11467 continue;
11468
11469 if (up_to_date > 0)
11470 {
11471 Lisp_Object val = find_symbol_value (var);
11472 Fput (var, Qlast_arrow_position,
11473 COERCE_MARKER (val));
11474 Fput (var, Qlast_arrow_string,
11475 overlay_arrow_string_or_property (var));
11476 }
11477 else if (up_to_date < 0
11478 || !NILP (Fget (var, Qlast_arrow_position)))
11479 {
11480 Fput (var, Qlast_arrow_position, Qt);
11481 Fput (var, Qlast_arrow_string, Qt);
11482 }
11483 }
11484 }
11485
11486
11487 /* Return overlay arrow string to display at row.
11488 Return integer (bitmap number) for arrow bitmap in left fringe.
11489 Return nil if no overlay arrow. */
11490
11491 static Lisp_Object
11492 overlay_arrow_at_row (it, row)
11493 struct it *it;
11494 struct glyph_row *row;
11495 {
11496 Lisp_Object vlist;
11497
11498 for (vlist = Voverlay_arrow_variable_list;
11499 CONSP (vlist);
11500 vlist = XCDR (vlist))
11501 {
11502 Lisp_Object var = XCAR (vlist);
11503 Lisp_Object val;
11504
11505 if (!SYMBOLP (var))
11506 continue;
11507
11508 val = find_symbol_value (var);
11509
11510 if (MARKERP (val)
11511 && current_buffer == XMARKER (val)->buffer
11512 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11513 {
11514 if (FRAME_WINDOW_P (it->f)
11515 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11516 {
11517 #ifdef HAVE_WINDOW_SYSTEM
11518 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11519 {
11520 int fringe_bitmap;
11521 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11522 return make_number (fringe_bitmap);
11523 }
11524 #endif
11525 return make_number (-1); /* Use default arrow bitmap */
11526 }
11527 return overlay_arrow_string_or_property (var);
11528 }
11529 }
11530
11531 return Qnil;
11532 }
11533
11534 /* Return 1 if point moved out of or into a composition. Otherwise
11535 return 0. PREV_BUF and PREV_PT are the last point buffer and
11536 position. BUF and PT are the current point buffer and position. */
11537
11538 int
11539 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11540 struct buffer *prev_buf, *buf;
11541 int prev_pt, pt;
11542 {
11543 EMACS_INT start, end;
11544 Lisp_Object prop;
11545 Lisp_Object buffer;
11546
11547 XSETBUFFER (buffer, buf);
11548 /* Check a composition at the last point if point moved within the
11549 same buffer. */
11550 if (prev_buf == buf)
11551 {
11552 if (prev_pt == pt)
11553 /* Point didn't move. */
11554 return 0;
11555
11556 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11557 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11558 && COMPOSITION_VALID_P (start, end, prop)
11559 && start < prev_pt && end > prev_pt)
11560 /* The last point was within the composition. Return 1 iff
11561 point moved out of the composition. */
11562 return (pt <= start || pt >= end);
11563 }
11564
11565 /* Check a composition at the current point. */
11566 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11567 && find_composition (pt, -1, &start, &end, &prop, buffer)
11568 && COMPOSITION_VALID_P (start, end, prop)
11569 && start < pt && end > pt);
11570 }
11571
11572
11573 /* Reconsider the setting of B->clip_changed which is displayed
11574 in window W. */
11575
11576 static INLINE void
11577 reconsider_clip_changes (w, b)
11578 struct window *w;
11579 struct buffer *b;
11580 {
11581 if (b->clip_changed
11582 && !NILP (w->window_end_valid)
11583 && w->current_matrix->buffer == b
11584 && w->current_matrix->zv == BUF_ZV (b)
11585 && w->current_matrix->begv == BUF_BEGV (b))
11586 b->clip_changed = 0;
11587
11588 /* If display wasn't paused, and W is not a tool bar window, see if
11589 point has been moved into or out of a composition. In that case,
11590 we set b->clip_changed to 1 to force updating the screen. If
11591 b->clip_changed has already been set to 1, we can skip this
11592 check. */
11593 if (!b->clip_changed
11594 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11595 {
11596 int pt;
11597
11598 if (w == XWINDOW (selected_window))
11599 pt = BUF_PT (current_buffer);
11600 else
11601 pt = marker_position (w->pointm);
11602
11603 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11604 || pt != XINT (w->last_point))
11605 && check_point_in_composition (w->current_matrix->buffer,
11606 XINT (w->last_point),
11607 XBUFFER (w->buffer), pt))
11608 b->clip_changed = 1;
11609 }
11610 }
11611 \f
11612
11613 /* Select FRAME to forward the values of frame-local variables into C
11614 variables so that the redisplay routines can access those values
11615 directly. */
11616
11617 static void
11618 select_frame_for_redisplay (frame)
11619 Lisp_Object frame;
11620 {
11621 Lisp_Object tail, symbol, val;
11622 Lisp_Object old = selected_frame;
11623 struct Lisp_Symbol *sym;
11624
11625 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11626
11627 selected_frame = frame;
11628
11629 do
11630 {
11631 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11632 if (CONSP (XCAR (tail))
11633 && (symbol = XCAR (XCAR (tail)),
11634 SYMBOLP (symbol))
11635 && (sym = indirect_variable (XSYMBOL (symbol)),
11636 val = sym->value,
11637 (BUFFER_LOCAL_VALUEP (val)))
11638 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11639 /* Use find_symbol_value rather than Fsymbol_value
11640 to avoid an error if it is void. */
11641 find_symbol_value (symbol);
11642 } while (!EQ (frame, old) && (frame = old, 1));
11643 }
11644
11645
11646 #define STOP_POLLING \
11647 do { if (! polling_stopped_here) stop_polling (); \
11648 polling_stopped_here = 1; } while (0)
11649
11650 #define RESUME_POLLING \
11651 do { if (polling_stopped_here) start_polling (); \
11652 polling_stopped_here = 0; } while (0)
11653
11654
11655 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11656 response to any user action; therefore, we should preserve the echo
11657 area. (Actually, our caller does that job.) Perhaps in the future
11658 avoid recentering windows if it is not necessary; currently that
11659 causes some problems. */
11660
11661 static void
11662 redisplay_internal (preserve_echo_area)
11663 int preserve_echo_area;
11664 {
11665 struct window *w = XWINDOW (selected_window);
11666 struct frame *f;
11667 int pause;
11668 int must_finish = 0;
11669 struct text_pos tlbufpos, tlendpos;
11670 int number_of_visible_frames;
11671 int count, count1;
11672 struct frame *sf;
11673 int polling_stopped_here = 0;
11674 Lisp_Object old_frame = selected_frame;
11675
11676 /* Non-zero means redisplay has to consider all windows on all
11677 frames. Zero means, only selected_window is considered. */
11678 int consider_all_windows_p;
11679
11680 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11681
11682 /* No redisplay if running in batch mode or frame is not yet fully
11683 initialized, or redisplay is explicitly turned off by setting
11684 Vinhibit_redisplay. */
11685 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11686 || !NILP (Vinhibit_redisplay))
11687 return;
11688
11689 /* Don't examine these until after testing Vinhibit_redisplay.
11690 When Emacs is shutting down, perhaps because its connection to
11691 X has dropped, we should not look at them at all. */
11692 f = XFRAME (w->frame);
11693 sf = SELECTED_FRAME ();
11694
11695 if (!f->glyphs_initialized_p)
11696 return;
11697
11698 /* The flag redisplay_performed_directly_p is set by
11699 direct_output_for_insert when it already did the whole screen
11700 update necessary. */
11701 if (redisplay_performed_directly_p)
11702 {
11703 redisplay_performed_directly_p = 0;
11704 if (!hscroll_windows (selected_window))
11705 return;
11706 }
11707
11708 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11709 if (popup_activated ())
11710 return;
11711 #endif
11712
11713 /* I don't think this happens but let's be paranoid. */
11714 if (redisplaying_p)
11715 return;
11716
11717 /* Record a function that resets redisplaying_p to its old value
11718 when we leave this function. */
11719 count = SPECPDL_INDEX ();
11720 record_unwind_protect (unwind_redisplay,
11721 Fcons (make_number (redisplaying_p), selected_frame));
11722 ++redisplaying_p;
11723 specbind (Qinhibit_free_realized_faces, Qnil);
11724
11725 {
11726 Lisp_Object tail, frame;
11727
11728 FOR_EACH_FRAME (tail, frame)
11729 {
11730 struct frame *f = XFRAME (frame);
11731 f->already_hscrolled_p = 0;
11732 }
11733 }
11734
11735 retry:
11736 if (!EQ (old_frame, selected_frame)
11737 && FRAME_LIVE_P (XFRAME (old_frame)))
11738 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11739 selected_frame and selected_window to be temporarily out-of-sync so
11740 when we come back here via `goto retry', we need to resync because we
11741 may need to run Elisp code (via prepare_menu_bars). */
11742 select_frame_for_redisplay (old_frame);
11743
11744 pause = 0;
11745 reconsider_clip_changes (w, current_buffer);
11746 last_escape_glyph_frame = NULL;
11747 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11748
11749 /* If new fonts have been loaded that make a glyph matrix adjustment
11750 necessary, do it. */
11751 if (fonts_changed_p)
11752 {
11753 adjust_glyphs (NULL);
11754 ++windows_or_buffers_changed;
11755 fonts_changed_p = 0;
11756 }
11757
11758 /* If face_change_count is non-zero, init_iterator will free all
11759 realized faces, which includes the faces referenced from current
11760 matrices. So, we can't reuse current matrices in this case. */
11761 if (face_change_count)
11762 ++windows_or_buffers_changed;
11763
11764 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11765 && FRAME_TTY (sf)->previous_frame != sf)
11766 {
11767 /* Since frames on a single ASCII terminal share the same
11768 display area, displaying a different frame means redisplay
11769 the whole thing. */
11770 windows_or_buffers_changed++;
11771 SET_FRAME_GARBAGED (sf);
11772 #ifndef DOS_NT
11773 set_tty_color_mode (FRAME_TTY (sf), sf);
11774 #endif
11775 FRAME_TTY (sf)->previous_frame = sf;
11776 }
11777
11778 /* Set the visible flags for all frames. Do this before checking
11779 for resized or garbaged frames; they want to know if their frames
11780 are visible. See the comment in frame.h for
11781 FRAME_SAMPLE_VISIBILITY. */
11782 {
11783 Lisp_Object tail, frame;
11784
11785 number_of_visible_frames = 0;
11786
11787 FOR_EACH_FRAME (tail, frame)
11788 {
11789 struct frame *f = XFRAME (frame);
11790
11791 FRAME_SAMPLE_VISIBILITY (f);
11792 if (FRAME_VISIBLE_P (f))
11793 ++number_of_visible_frames;
11794 clear_desired_matrices (f);
11795 }
11796 }
11797
11798 /* Notice any pending interrupt request to change frame size. */
11799 do_pending_window_change (1);
11800
11801 /* Clear frames marked as garbaged. */
11802 if (frame_garbaged)
11803 clear_garbaged_frames ();
11804
11805 /* Build menubar and tool-bar items. */
11806 if (NILP (Vmemory_full))
11807 prepare_menu_bars ();
11808
11809 if (windows_or_buffers_changed)
11810 update_mode_lines++;
11811
11812 /* Detect case that we need to write or remove a star in the mode line. */
11813 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11814 {
11815 w->update_mode_line = Qt;
11816 if (buffer_shared > 1)
11817 update_mode_lines++;
11818 }
11819
11820 /* Avoid invocation of point motion hooks by `current_column' below. */
11821 count1 = SPECPDL_INDEX ();
11822 specbind (Qinhibit_point_motion_hooks, Qt);
11823
11824 /* If %c is in the mode line, update it if needed. */
11825 if (!NILP (w->column_number_displayed)
11826 /* This alternative quickly identifies a common case
11827 where no change is needed. */
11828 && !(PT == XFASTINT (w->last_point)
11829 && XFASTINT (w->last_modified) >= MODIFF
11830 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11831 && (XFASTINT (w->column_number_displayed)
11832 != (int) current_column ())) /* iftc */
11833 w->update_mode_line = Qt;
11834
11835 unbind_to (count1, Qnil);
11836
11837 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11838
11839 /* The variable buffer_shared is set in redisplay_window and
11840 indicates that we redisplay a buffer in different windows. See
11841 there. */
11842 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11843 || cursor_type_changed);
11844
11845 /* If specs for an arrow have changed, do thorough redisplay
11846 to ensure we remove any arrow that should no longer exist. */
11847 if (overlay_arrows_changed_p ())
11848 consider_all_windows_p = windows_or_buffers_changed = 1;
11849
11850 /* Normally the message* functions will have already displayed and
11851 updated the echo area, but the frame may have been trashed, or
11852 the update may have been preempted, so display the echo area
11853 again here. Checking message_cleared_p captures the case that
11854 the echo area should be cleared. */
11855 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11856 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11857 || (message_cleared_p
11858 && minibuf_level == 0
11859 /* If the mini-window is currently selected, this means the
11860 echo-area doesn't show through. */
11861 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11862 {
11863 int window_height_changed_p = echo_area_display (0);
11864 must_finish = 1;
11865
11866 /* If we don't display the current message, don't clear the
11867 message_cleared_p flag, because, if we did, we wouldn't clear
11868 the echo area in the next redisplay which doesn't preserve
11869 the echo area. */
11870 if (!display_last_displayed_message_p)
11871 message_cleared_p = 0;
11872
11873 if (fonts_changed_p)
11874 goto retry;
11875 else if (window_height_changed_p)
11876 {
11877 consider_all_windows_p = 1;
11878 ++update_mode_lines;
11879 ++windows_or_buffers_changed;
11880
11881 /* If window configuration was changed, frames may have been
11882 marked garbaged. Clear them or we will experience
11883 surprises wrt scrolling. */
11884 if (frame_garbaged)
11885 clear_garbaged_frames ();
11886 }
11887 }
11888 else if (EQ (selected_window, minibuf_window)
11889 && (current_buffer->clip_changed
11890 || XFASTINT (w->last_modified) < MODIFF
11891 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11892 && resize_mini_window (w, 0))
11893 {
11894 /* Resized active mini-window to fit the size of what it is
11895 showing if its contents might have changed. */
11896 must_finish = 1;
11897 /* FIXME: this causes all frames to be updated, which seems unnecessary
11898 since only the current frame needs to be considered. This function needs
11899 to be rewritten with two variables, consider_all_windows and
11900 consider_all_frames. */
11901 consider_all_windows_p = 1;
11902 ++windows_or_buffers_changed;
11903 ++update_mode_lines;
11904
11905 /* If window configuration was changed, frames may have been
11906 marked garbaged. Clear them or we will experience
11907 surprises wrt scrolling. */
11908 if (frame_garbaged)
11909 clear_garbaged_frames ();
11910 }
11911
11912
11913 /* If showing the region, and mark has changed, we must redisplay
11914 the whole window. The assignment to this_line_start_pos prevents
11915 the optimization directly below this if-statement. */
11916 if (((!NILP (Vtransient_mark_mode)
11917 && !NILP (XBUFFER (w->buffer)->mark_active))
11918 != !NILP (w->region_showing))
11919 || (!NILP (w->region_showing)
11920 && !EQ (w->region_showing,
11921 Fmarker_position (XBUFFER (w->buffer)->mark))))
11922 CHARPOS (this_line_start_pos) = 0;
11923
11924 /* Optimize the case that only the line containing the cursor in the
11925 selected window has changed. Variables starting with this_ are
11926 set in display_line and record information about the line
11927 containing the cursor. */
11928 tlbufpos = this_line_start_pos;
11929 tlendpos = this_line_end_pos;
11930 if (!consider_all_windows_p
11931 && CHARPOS (tlbufpos) > 0
11932 && NILP (w->update_mode_line)
11933 && !current_buffer->clip_changed
11934 && !current_buffer->prevent_redisplay_optimizations_p
11935 && FRAME_VISIBLE_P (XFRAME (w->frame))
11936 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11937 /* Make sure recorded data applies to current buffer, etc. */
11938 && this_line_buffer == current_buffer
11939 && current_buffer == XBUFFER (w->buffer)
11940 && NILP (w->force_start)
11941 && NILP (w->optional_new_start)
11942 /* Point must be on the line that we have info recorded about. */
11943 && PT >= CHARPOS (tlbufpos)
11944 && PT <= Z - CHARPOS (tlendpos)
11945 /* All text outside that line, including its final newline,
11946 must be unchanged. */
11947 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11948 CHARPOS (tlendpos)))
11949 {
11950 if (CHARPOS (tlbufpos) > BEGV
11951 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11952 && (CHARPOS (tlbufpos) == ZV
11953 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11954 /* Former continuation line has disappeared by becoming empty. */
11955 goto cancel;
11956 else if (XFASTINT (w->last_modified) < MODIFF
11957 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11958 || MINI_WINDOW_P (w))
11959 {
11960 /* We have to handle the case of continuation around a
11961 wide-column character (see the comment in indent.c around
11962 line 1340).
11963
11964 For instance, in the following case:
11965
11966 -------- Insert --------
11967 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11968 J_I_ ==> J_I_ `^^' are cursors.
11969 ^^ ^^
11970 -------- --------
11971
11972 As we have to redraw the line above, we cannot use this
11973 optimization. */
11974
11975 struct it it;
11976 int line_height_before = this_line_pixel_height;
11977
11978 /* Note that start_display will handle the case that the
11979 line starting at tlbufpos is a continuation line. */
11980 start_display (&it, w, tlbufpos);
11981
11982 /* Implementation note: It this still necessary? */
11983 if (it.current_x != this_line_start_x)
11984 goto cancel;
11985
11986 TRACE ((stderr, "trying display optimization 1\n"));
11987 w->cursor.vpos = -1;
11988 overlay_arrow_seen = 0;
11989 it.vpos = this_line_vpos;
11990 it.current_y = this_line_y;
11991 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11992 display_line (&it);
11993
11994 /* If line contains point, is not continued,
11995 and ends at same distance from eob as before, we win. */
11996 if (w->cursor.vpos >= 0
11997 /* Line is not continued, otherwise this_line_start_pos
11998 would have been set to 0 in display_line. */
11999 && CHARPOS (this_line_start_pos)
12000 /* Line ends as before. */
12001 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12002 /* Line has same height as before. Otherwise other lines
12003 would have to be shifted up or down. */
12004 && this_line_pixel_height == line_height_before)
12005 {
12006 /* If this is not the window's last line, we must adjust
12007 the charstarts of the lines below. */
12008 if (it.current_y < it.last_visible_y)
12009 {
12010 struct glyph_row *row
12011 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12012 int delta, delta_bytes;
12013
12014 /* We used to distinguish between two cases here,
12015 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12016 when the line ends in a newline or the end of the
12017 buffer's accessible portion. But both cases did
12018 the same, so they were collapsed. */
12019 delta = (Z
12020 - CHARPOS (tlendpos)
12021 - MATRIX_ROW_START_CHARPOS (row));
12022 delta_bytes = (Z_BYTE
12023 - BYTEPOS (tlendpos)
12024 - MATRIX_ROW_START_BYTEPOS (row));
12025
12026 increment_matrix_positions (w->current_matrix,
12027 this_line_vpos + 1,
12028 w->current_matrix->nrows,
12029 delta, delta_bytes);
12030 }
12031
12032 /* If this row displays text now but previously didn't,
12033 or vice versa, w->window_end_vpos may have to be
12034 adjusted. */
12035 if ((it.glyph_row - 1)->displays_text_p)
12036 {
12037 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12038 XSETINT (w->window_end_vpos, this_line_vpos);
12039 }
12040 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12041 && this_line_vpos > 0)
12042 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12043 w->window_end_valid = Qnil;
12044
12045 /* Update hint: No need to try to scroll in update_window. */
12046 w->desired_matrix->no_scrolling_p = 1;
12047
12048 #if GLYPH_DEBUG
12049 *w->desired_matrix->method = 0;
12050 debug_method_add (w, "optimization 1");
12051 #endif
12052 #ifdef HAVE_WINDOW_SYSTEM
12053 update_window_fringes (w, 0);
12054 #endif
12055 goto update;
12056 }
12057 else
12058 goto cancel;
12059 }
12060 else if (/* Cursor position hasn't changed. */
12061 PT == XFASTINT (w->last_point)
12062 /* Make sure the cursor was last displayed
12063 in this window. Otherwise we have to reposition it. */
12064 && 0 <= w->cursor.vpos
12065 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12066 {
12067 if (!must_finish)
12068 {
12069 do_pending_window_change (1);
12070
12071 /* We used to always goto end_of_redisplay here, but this
12072 isn't enough if we have a blinking cursor. */
12073 if (w->cursor_off_p == w->last_cursor_off_p)
12074 goto end_of_redisplay;
12075 }
12076 goto update;
12077 }
12078 /* If highlighting the region, or if the cursor is in the echo area,
12079 then we can't just move the cursor. */
12080 else if (! (!NILP (Vtransient_mark_mode)
12081 && !NILP (current_buffer->mark_active))
12082 && (EQ (selected_window, current_buffer->last_selected_window)
12083 || highlight_nonselected_windows)
12084 && NILP (w->region_showing)
12085 && NILP (Vshow_trailing_whitespace)
12086 && !cursor_in_echo_area)
12087 {
12088 struct it it;
12089 struct glyph_row *row;
12090
12091 /* Skip from tlbufpos to PT and see where it is. Note that
12092 PT may be in invisible text. If so, we will end at the
12093 next visible position. */
12094 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12095 NULL, DEFAULT_FACE_ID);
12096 it.current_x = this_line_start_x;
12097 it.current_y = this_line_y;
12098 it.vpos = this_line_vpos;
12099
12100 /* The call to move_it_to stops in front of PT, but
12101 moves over before-strings. */
12102 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12103
12104 if (it.vpos == this_line_vpos
12105 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12106 row->enabled_p))
12107 {
12108 xassert (this_line_vpos == it.vpos);
12109 xassert (this_line_y == it.current_y);
12110 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12111 #if GLYPH_DEBUG
12112 *w->desired_matrix->method = 0;
12113 debug_method_add (w, "optimization 3");
12114 #endif
12115 goto update;
12116 }
12117 else
12118 goto cancel;
12119 }
12120
12121 cancel:
12122 /* Text changed drastically or point moved off of line. */
12123 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12124 }
12125
12126 CHARPOS (this_line_start_pos) = 0;
12127 consider_all_windows_p |= buffer_shared > 1;
12128 ++clear_face_cache_count;
12129 #ifdef HAVE_WINDOW_SYSTEM
12130 ++clear_image_cache_count;
12131 #endif
12132
12133 /* Build desired matrices, and update the display. If
12134 consider_all_windows_p is non-zero, do it for all windows on all
12135 frames. Otherwise do it for selected_window, only. */
12136
12137 if (consider_all_windows_p)
12138 {
12139 Lisp_Object tail, frame;
12140
12141 FOR_EACH_FRAME (tail, frame)
12142 XFRAME (frame)->updated_p = 0;
12143
12144 /* Recompute # windows showing selected buffer. This will be
12145 incremented each time such a window is displayed. */
12146 buffer_shared = 0;
12147
12148 FOR_EACH_FRAME (tail, frame)
12149 {
12150 struct frame *f = XFRAME (frame);
12151
12152 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12153 {
12154 if (! EQ (frame, selected_frame))
12155 /* Select the frame, for the sake of frame-local
12156 variables. */
12157 select_frame_for_redisplay (frame);
12158
12159 /* Mark all the scroll bars to be removed; we'll redeem
12160 the ones we want when we redisplay their windows. */
12161 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12162 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12163
12164 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12165 redisplay_windows (FRAME_ROOT_WINDOW (f));
12166
12167 /* The X error handler may have deleted that frame. */
12168 if (!FRAME_LIVE_P (f))
12169 continue;
12170
12171 /* Any scroll bars which redisplay_windows should have
12172 nuked should now go away. */
12173 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12174 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12175
12176 /* If fonts changed, display again. */
12177 /* ??? rms: I suspect it is a mistake to jump all the way
12178 back to retry here. It should just retry this frame. */
12179 if (fonts_changed_p)
12180 goto retry;
12181
12182 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12183 {
12184 /* See if we have to hscroll. */
12185 if (!f->already_hscrolled_p)
12186 {
12187 f->already_hscrolled_p = 1;
12188 if (hscroll_windows (f->root_window))
12189 goto retry;
12190 }
12191
12192 /* Prevent various kinds of signals during display
12193 update. stdio is not robust about handling
12194 signals, which can cause an apparent I/O
12195 error. */
12196 if (interrupt_input)
12197 unrequest_sigio ();
12198 STOP_POLLING;
12199
12200 /* Update the display. */
12201 set_window_update_flags (XWINDOW (f->root_window), 1);
12202 pause |= update_frame (f, 0, 0);
12203 f->updated_p = 1;
12204 }
12205 }
12206 }
12207
12208 if (!EQ (old_frame, selected_frame)
12209 && FRAME_LIVE_P (XFRAME (old_frame)))
12210 /* We played a bit fast-and-loose above and allowed selected_frame
12211 and selected_window to be temporarily out-of-sync but let's make
12212 sure this stays contained. */
12213 select_frame_for_redisplay (old_frame);
12214 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12215
12216 if (!pause)
12217 {
12218 /* Do the mark_window_display_accurate after all windows have
12219 been redisplayed because this call resets flags in buffers
12220 which are needed for proper redisplay. */
12221 FOR_EACH_FRAME (tail, frame)
12222 {
12223 struct frame *f = XFRAME (frame);
12224 if (f->updated_p)
12225 {
12226 mark_window_display_accurate (f->root_window, 1);
12227 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12228 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12229 }
12230 }
12231 }
12232 }
12233 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12234 {
12235 Lisp_Object mini_window;
12236 struct frame *mini_frame;
12237
12238 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12239 /* Use list_of_error, not Qerror, so that
12240 we catch only errors and don't run the debugger. */
12241 internal_condition_case_1 (redisplay_window_1, selected_window,
12242 list_of_error,
12243 redisplay_window_error);
12244
12245 /* Compare desired and current matrices, perform output. */
12246
12247 update:
12248 /* If fonts changed, display again. */
12249 if (fonts_changed_p)
12250 goto retry;
12251
12252 /* Prevent various kinds of signals during display update.
12253 stdio is not robust about handling signals,
12254 which can cause an apparent I/O error. */
12255 if (interrupt_input)
12256 unrequest_sigio ();
12257 STOP_POLLING;
12258
12259 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12260 {
12261 if (hscroll_windows (selected_window))
12262 goto retry;
12263
12264 XWINDOW (selected_window)->must_be_updated_p = 1;
12265 pause = update_frame (sf, 0, 0);
12266 }
12267
12268 /* We may have called echo_area_display at the top of this
12269 function. If the echo area is on another frame, that may
12270 have put text on a frame other than the selected one, so the
12271 above call to update_frame would not have caught it. Catch
12272 it here. */
12273 mini_window = FRAME_MINIBUF_WINDOW (sf);
12274 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12275
12276 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12277 {
12278 XWINDOW (mini_window)->must_be_updated_p = 1;
12279 pause |= update_frame (mini_frame, 0, 0);
12280 if (!pause && hscroll_windows (mini_window))
12281 goto retry;
12282 }
12283 }
12284
12285 /* If display was paused because of pending input, make sure we do a
12286 thorough update the next time. */
12287 if (pause)
12288 {
12289 /* Prevent the optimization at the beginning of
12290 redisplay_internal that tries a single-line update of the
12291 line containing the cursor in the selected window. */
12292 CHARPOS (this_line_start_pos) = 0;
12293
12294 /* Let the overlay arrow be updated the next time. */
12295 update_overlay_arrows (0);
12296
12297 /* If we pause after scrolling, some rows in the current
12298 matrices of some windows are not valid. */
12299 if (!WINDOW_FULL_WIDTH_P (w)
12300 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12301 update_mode_lines = 1;
12302 }
12303 else
12304 {
12305 if (!consider_all_windows_p)
12306 {
12307 /* This has already been done above if
12308 consider_all_windows_p is set. */
12309 mark_window_display_accurate_1 (w, 1);
12310
12311 /* Say overlay arrows are up to date. */
12312 update_overlay_arrows (1);
12313
12314 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12315 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12316 }
12317
12318 update_mode_lines = 0;
12319 windows_or_buffers_changed = 0;
12320 cursor_type_changed = 0;
12321 }
12322
12323 /* Start SIGIO interrupts coming again. Having them off during the
12324 code above makes it less likely one will discard output, but not
12325 impossible, since there might be stuff in the system buffer here.
12326 But it is much hairier to try to do anything about that. */
12327 if (interrupt_input)
12328 request_sigio ();
12329 RESUME_POLLING;
12330
12331 /* If a frame has become visible which was not before, redisplay
12332 again, so that we display it. Expose events for such a frame
12333 (which it gets when becoming visible) don't call the parts of
12334 redisplay constructing glyphs, so simply exposing a frame won't
12335 display anything in this case. So, we have to display these
12336 frames here explicitly. */
12337 if (!pause)
12338 {
12339 Lisp_Object tail, frame;
12340 int new_count = 0;
12341
12342 FOR_EACH_FRAME (tail, frame)
12343 {
12344 int this_is_visible = 0;
12345
12346 if (XFRAME (frame)->visible)
12347 this_is_visible = 1;
12348 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12349 if (XFRAME (frame)->visible)
12350 this_is_visible = 1;
12351
12352 if (this_is_visible)
12353 new_count++;
12354 }
12355
12356 if (new_count != number_of_visible_frames)
12357 windows_or_buffers_changed++;
12358 }
12359
12360 /* Change frame size now if a change is pending. */
12361 do_pending_window_change (1);
12362
12363 /* If we just did a pending size change, or have additional
12364 visible frames, redisplay again. */
12365 if (windows_or_buffers_changed && !pause)
12366 goto retry;
12367
12368 /* Clear the face cache eventually. */
12369 if (consider_all_windows_p)
12370 {
12371 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12372 {
12373 clear_face_cache (0);
12374 clear_face_cache_count = 0;
12375 }
12376 #ifdef HAVE_WINDOW_SYSTEM
12377 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12378 {
12379 clear_image_caches (Qnil);
12380 clear_image_cache_count = 0;
12381 }
12382 #endif /* HAVE_WINDOW_SYSTEM */
12383 }
12384
12385 end_of_redisplay:
12386 unbind_to (count, Qnil);
12387 RESUME_POLLING;
12388 }
12389
12390
12391 /* Redisplay, but leave alone any recent echo area message unless
12392 another message has been requested in its place.
12393
12394 This is useful in situations where you need to redisplay but no
12395 user action has occurred, making it inappropriate for the message
12396 area to be cleared. See tracking_off and
12397 wait_reading_process_output for examples of these situations.
12398
12399 FROM_WHERE is an integer saying from where this function was
12400 called. This is useful for debugging. */
12401
12402 void
12403 redisplay_preserve_echo_area (from_where)
12404 int from_where;
12405 {
12406 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12407
12408 if (!NILP (echo_area_buffer[1]))
12409 {
12410 /* We have a previously displayed message, but no current
12411 message. Redisplay the previous message. */
12412 display_last_displayed_message_p = 1;
12413 redisplay_internal (1);
12414 display_last_displayed_message_p = 0;
12415 }
12416 else
12417 redisplay_internal (1);
12418
12419 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12420 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12421 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12422 }
12423
12424
12425 /* Function registered with record_unwind_protect in
12426 redisplay_internal. Reset redisplaying_p to the value it had
12427 before redisplay_internal was called, and clear
12428 prevent_freeing_realized_faces_p. It also selects the previously
12429 selected frame, unless it has been deleted (by an X connection
12430 failure during redisplay, for example). */
12431
12432 static Lisp_Object
12433 unwind_redisplay (val)
12434 Lisp_Object val;
12435 {
12436 Lisp_Object old_redisplaying_p, old_frame;
12437
12438 old_redisplaying_p = XCAR (val);
12439 redisplaying_p = XFASTINT (old_redisplaying_p);
12440 old_frame = XCDR (val);
12441 if (! EQ (old_frame, selected_frame)
12442 && FRAME_LIVE_P (XFRAME (old_frame)))
12443 select_frame_for_redisplay (old_frame);
12444 return Qnil;
12445 }
12446
12447
12448 /* Mark the display of window W as accurate or inaccurate. If
12449 ACCURATE_P is non-zero mark display of W as accurate. If
12450 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12451 redisplay_internal is called. */
12452
12453 static void
12454 mark_window_display_accurate_1 (w, accurate_p)
12455 struct window *w;
12456 int accurate_p;
12457 {
12458 if (BUFFERP (w->buffer))
12459 {
12460 struct buffer *b = XBUFFER (w->buffer);
12461
12462 w->last_modified
12463 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12464 w->last_overlay_modified
12465 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12466 w->last_had_star
12467 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12468
12469 if (accurate_p)
12470 {
12471 b->clip_changed = 0;
12472 b->prevent_redisplay_optimizations_p = 0;
12473
12474 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12475 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12476 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12477 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12478
12479 w->current_matrix->buffer = b;
12480 w->current_matrix->begv = BUF_BEGV (b);
12481 w->current_matrix->zv = BUF_ZV (b);
12482
12483 w->last_cursor = w->cursor;
12484 w->last_cursor_off_p = w->cursor_off_p;
12485
12486 if (w == XWINDOW (selected_window))
12487 w->last_point = make_number (BUF_PT (b));
12488 else
12489 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12490 }
12491 }
12492
12493 if (accurate_p)
12494 {
12495 w->window_end_valid = w->buffer;
12496 w->update_mode_line = Qnil;
12497 }
12498 }
12499
12500
12501 /* Mark the display of windows in the window tree rooted at WINDOW as
12502 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12503 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12504 be redisplayed the next time redisplay_internal is called. */
12505
12506 void
12507 mark_window_display_accurate (window, accurate_p)
12508 Lisp_Object window;
12509 int accurate_p;
12510 {
12511 struct window *w;
12512
12513 for (; !NILP (window); window = w->next)
12514 {
12515 w = XWINDOW (window);
12516 mark_window_display_accurate_1 (w, accurate_p);
12517
12518 if (!NILP (w->vchild))
12519 mark_window_display_accurate (w->vchild, accurate_p);
12520 if (!NILP (w->hchild))
12521 mark_window_display_accurate (w->hchild, accurate_p);
12522 }
12523
12524 if (accurate_p)
12525 {
12526 update_overlay_arrows (1);
12527 }
12528 else
12529 {
12530 /* Force a thorough redisplay the next time by setting
12531 last_arrow_position and last_arrow_string to t, which is
12532 unequal to any useful value of Voverlay_arrow_... */
12533 update_overlay_arrows (-1);
12534 }
12535 }
12536
12537
12538 /* Return value in display table DP (Lisp_Char_Table *) for character
12539 C. Since a display table doesn't have any parent, we don't have to
12540 follow parent. Do not call this function directly but use the
12541 macro DISP_CHAR_VECTOR. */
12542
12543 Lisp_Object
12544 disp_char_vector (dp, c)
12545 struct Lisp_Char_Table *dp;
12546 int c;
12547 {
12548 Lisp_Object val;
12549
12550 if (ASCII_CHAR_P (c))
12551 {
12552 val = dp->ascii;
12553 if (SUB_CHAR_TABLE_P (val))
12554 val = XSUB_CHAR_TABLE (val)->contents[c];
12555 }
12556 else
12557 {
12558 Lisp_Object table;
12559
12560 XSETCHAR_TABLE (table, dp);
12561 val = char_table_ref (table, c);
12562 }
12563 if (NILP (val))
12564 val = dp->defalt;
12565 return val;
12566 }
12567
12568
12569 \f
12570 /***********************************************************************
12571 Window Redisplay
12572 ***********************************************************************/
12573
12574 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12575
12576 static void
12577 redisplay_windows (window)
12578 Lisp_Object window;
12579 {
12580 while (!NILP (window))
12581 {
12582 struct window *w = XWINDOW (window);
12583
12584 if (!NILP (w->hchild))
12585 redisplay_windows (w->hchild);
12586 else if (!NILP (w->vchild))
12587 redisplay_windows (w->vchild);
12588 else if (!NILP (w->buffer))
12589 {
12590 displayed_buffer = XBUFFER (w->buffer);
12591 /* Use list_of_error, not Qerror, so that
12592 we catch only errors and don't run the debugger. */
12593 internal_condition_case_1 (redisplay_window_0, window,
12594 list_of_error,
12595 redisplay_window_error);
12596 }
12597
12598 window = w->next;
12599 }
12600 }
12601
12602 static Lisp_Object
12603 redisplay_window_error ()
12604 {
12605 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12606 return Qnil;
12607 }
12608
12609 static Lisp_Object
12610 redisplay_window_0 (window)
12611 Lisp_Object window;
12612 {
12613 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12614 redisplay_window (window, 0);
12615 return Qnil;
12616 }
12617
12618 static Lisp_Object
12619 redisplay_window_1 (window)
12620 Lisp_Object window;
12621 {
12622 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12623 redisplay_window (window, 1);
12624 return Qnil;
12625 }
12626 \f
12627
12628 /* Increment GLYPH until it reaches END or CONDITION fails while
12629 adding (GLYPH)->pixel_width to X. */
12630
12631 #define SKIP_GLYPHS(glyph, end, x, condition) \
12632 do \
12633 { \
12634 (x) += (glyph)->pixel_width; \
12635 ++(glyph); \
12636 } \
12637 while ((glyph) < (end) && (condition))
12638
12639
12640 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12641 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12642 which positions recorded in ROW differ from current buffer
12643 positions.
12644
12645 Return 0 if cursor is not on this row, 1 otherwise. */
12646
12647 int
12648 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12649 struct window *w;
12650 struct glyph_row *row;
12651 struct glyph_matrix *matrix;
12652 int delta, delta_bytes, dy, dvpos;
12653 {
12654 struct glyph *glyph = row->glyphs[TEXT_AREA];
12655 struct glyph *end = glyph + row->used[TEXT_AREA];
12656 struct glyph *cursor = NULL;
12657 /* The last known character position in row. */
12658 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12659 int x = row->x;
12660 int cursor_x = x;
12661 EMACS_INT pt_old = PT - delta;
12662 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12663 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12664 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12665 /* Non-zero means we've found a match for cursor position, but that
12666 glyph has the avoid_cursor_p flag set. */
12667 int match_with_avoid_cursor = 0;
12668 /* Non-zero means we've seen at least one glyph that came from a
12669 display string. */
12670 int string_seen = 0;
12671 /* Largest buffer position seen so far during scan of glyph row. */
12672 EMACS_INT bpos_max = last_pos;
12673 /* Last buffer position covered by an overlay string with an integer
12674 `cursor' property. */
12675 EMACS_INT bpos_covered = 0;
12676
12677 /* Skip over glyphs not having an object at the start and the end of
12678 the row. These are special glyphs like truncation marks on
12679 terminal frames. */
12680 if (row->displays_text_p)
12681 {
12682 if (!row->reversed_p)
12683 {
12684 while (glyph < end
12685 && INTEGERP (glyph->object)
12686 && glyph->charpos < 0)
12687 {
12688 x += glyph->pixel_width;
12689 ++glyph;
12690 }
12691 while (end > glyph
12692 && INTEGERP ((end - 1)->object)
12693 /* CHARPOS is zero for blanks inserted by
12694 extend_face_to_end_of_line. */
12695 && (end - 1)->charpos <= 0)
12696 --end;
12697 glyph_before = glyph - 1;
12698 glyph_after = end;
12699 }
12700 else
12701 {
12702 struct glyph *g;
12703
12704 /* If the glyph row is reversed, we need to process it from back
12705 to front, so swap the edge pointers. */
12706 end = glyph - 1;
12707 glyph += row->used[TEXT_AREA] - 1;
12708 /* Reverse the known positions in the row. */
12709 last_pos = pos_after = MATRIX_ROW_START_CHARPOS (row) + delta;
12710 pos_before = MATRIX_ROW_END_CHARPOS (row) + delta;
12711
12712 while (glyph > end + 1
12713 && INTEGERP (glyph->object)
12714 && glyph->charpos < 0)
12715 {
12716 --glyph;
12717 x -= glyph->pixel_width;
12718 }
12719 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12720 --glyph;
12721 /* By default, put the cursor on the rightmost glyph. */
12722 for (g = end + 1; g < glyph; g++)
12723 x += g->pixel_width;
12724 cursor_x = x;
12725 while (end < glyph
12726 && INTEGERP ((end + 1)->object)
12727 && (end + 1)->charpos <= 0)
12728 ++end;
12729 glyph_before = glyph + 1;
12730 glyph_after = end;
12731 }
12732 }
12733 else if (row->reversed_p)
12734 {
12735 /* In R2L rows that don't display text, put the cursor on the
12736 rightmost glyph. Case in point: an empty last line that is
12737 part of an R2L paragraph. */
12738 cursor = end - 1;
12739 x = -1; /* will be computed below, at lable compute_x */
12740 }
12741
12742 /* Step 1: Try to find the glyph whose character position
12743 corresponds to point. If that's not possible, find 2 glyphs
12744 whose character positions are the closest to point, one before
12745 point, the other after it. */
12746 if (!row->reversed_p)
12747 while (/* not marched to end of glyph row */
12748 glyph < end
12749 /* glyph was not inserted by redisplay for internal purposes */
12750 && !INTEGERP (glyph->object))
12751 {
12752 if (BUFFERP (glyph->object))
12753 {
12754 EMACS_INT dpos = glyph->charpos - pt_old;
12755
12756 if (glyph->charpos > bpos_max)
12757 bpos_max = glyph->charpos;
12758 if (!glyph->avoid_cursor_p)
12759 {
12760 /* If we hit point, we've found the glyph on which to
12761 display the cursor. */
12762 if (dpos == 0)
12763 {
12764 match_with_avoid_cursor = 0;
12765 break;
12766 }
12767 /* See if we've found a better approximation to
12768 POS_BEFORE or to POS_AFTER. Note that we want the
12769 first (leftmost) glyph of all those that are the
12770 closest from below, and the last (rightmost) of all
12771 those from above. */
12772 if (0 > dpos && dpos > pos_before - pt_old)
12773 {
12774 pos_before = glyph->charpos;
12775 glyph_before = glyph;
12776 }
12777 else if (0 < dpos && dpos <= pos_after - pt_old)
12778 {
12779 pos_after = glyph->charpos;
12780 glyph_after = glyph;
12781 }
12782 }
12783 else if (dpos == 0)
12784 match_with_avoid_cursor = 1;
12785 }
12786 else if (STRINGP (glyph->object))
12787 {
12788 Lisp_Object chprop;
12789 int glyph_pos = glyph->charpos;
12790
12791 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12792 glyph->object);
12793 if (INTEGERP (chprop))
12794 {
12795 bpos_covered = bpos_max + XINT (chprop);
12796 /* If the `cursor' property covers buffer positions up
12797 to and including point, we should display cursor on
12798 this glyph. */
12799 /* Implementation note: bpos_max == pt_old when, e.g.,
12800 we are in an empty line, where bpos_max is set to
12801 MATRIX_ROW_START_CHARPOS, see above. */
12802 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12803 {
12804 cursor = glyph;
12805 break;
12806 }
12807 }
12808
12809 string_seen = 1;
12810 }
12811 x += glyph->pixel_width;
12812 ++glyph;
12813 }
12814 else if (glyph > end) /* row is reversed */
12815 while (!INTEGERP (glyph->object))
12816 {
12817 if (BUFFERP (glyph->object))
12818 {
12819 EMACS_INT dpos = glyph->charpos - pt_old;
12820
12821 if (glyph->charpos > bpos_max)
12822 bpos_max = glyph->charpos;
12823 if (!glyph->avoid_cursor_p)
12824 {
12825 if (dpos == 0)
12826 {
12827 match_with_avoid_cursor = 0;
12828 break;
12829 }
12830 if (0 > dpos && dpos > pos_before - pt_old)
12831 {
12832 pos_before = glyph->charpos;
12833 glyph_before = glyph;
12834 }
12835 else if (0 < dpos && dpos <= pos_after - pt_old)
12836 {
12837 pos_after = glyph->charpos;
12838 glyph_after = glyph;
12839 }
12840 }
12841 else if (dpos == 0)
12842 match_with_avoid_cursor = 1;
12843 }
12844 else if (STRINGP (glyph->object))
12845 {
12846 Lisp_Object chprop;
12847 int glyph_pos = glyph->charpos;
12848
12849 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12850 glyph->object);
12851 if (INTEGERP (chprop))
12852 {
12853 bpos_covered = bpos_max + XINT (chprop);
12854 /* If the `cursor' property covers buffer positions up
12855 to and including point, we should display cursor on
12856 this glyph. */
12857 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12858 {
12859 cursor = glyph;
12860 break;
12861 }
12862 }
12863 string_seen = 1;
12864 }
12865 --glyph;
12866 if (glyph == end)
12867 break;
12868 x -= glyph->pixel_width;
12869 }
12870
12871 /* Step 2: If we didn't find an exact match for point, we need to
12872 look for a proper place to put the cursor among glyphs between
12873 GLYPH_BEFORE and GLYPH_AFTER. */
12874 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12875 && bpos_covered < pt_old)
12876 {
12877 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12878 {
12879 EMACS_INT ellipsis_pos;
12880
12881 /* Scan back over the ellipsis glyphs. */
12882 if (!row->reversed_p)
12883 {
12884 ellipsis_pos = (glyph - 1)->charpos;
12885 while (glyph > row->glyphs[TEXT_AREA]
12886 && (glyph - 1)->charpos == ellipsis_pos)
12887 glyph--, x -= glyph->pixel_width;
12888 /* That loop always goes one position too far, including
12889 the glyph before the ellipsis. So scan forward over
12890 that one. */
12891 x += glyph->pixel_width;
12892 glyph++;
12893 }
12894 else /* row is reversed */
12895 {
12896 ellipsis_pos = (glyph + 1)->charpos;
12897 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12898 && (glyph + 1)->charpos == ellipsis_pos)
12899 glyph++, x += glyph->pixel_width;
12900 x -= glyph->pixel_width;
12901 glyph--;
12902 }
12903 }
12904 else if (match_with_avoid_cursor
12905 /* zero-width characters produce no glyphs */
12906 || eabs (glyph_after - glyph_before) == 1)
12907 {
12908 cursor = glyph_after;
12909 x = -1;
12910 }
12911 else if (string_seen)
12912 {
12913 int incr = row->reversed_p ? -1 : +1;
12914
12915 /* Need to find the glyph that came out of a string which is
12916 present at point. That glyph is somewhere between
12917 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12918 positioned between POS_BEFORE and POS_AFTER in the
12919 buffer. */
12920 struct glyph *stop = glyph_after;
12921 EMACS_INT pos = pos_before;
12922
12923 x = -1;
12924 for (glyph = glyph_before + incr;
12925 row->reversed_p ? glyph > stop : glyph < stop; )
12926 {
12927
12928 /* Any glyphs that come from the buffer are here because
12929 of bidi reordering. Skip them, and only pay
12930 attention to glyphs that came from some string. */
12931 if (STRINGP (glyph->object))
12932 {
12933 Lisp_Object str;
12934 EMACS_INT tem;
12935
12936 str = glyph->object;
12937 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12938 if (pos <= tem)
12939 {
12940 /* If the string from which this glyph came is
12941 found in the buffer at point, then we've
12942 found the glyph we've been looking for. */
12943 if (tem == pt_old)
12944 {
12945 /* The glyphs from this string could have
12946 been reordered. Find the one with the
12947 smallest string position. Or there could
12948 be a character in the string with the
12949 `cursor' property, which means display
12950 cursor on that character's glyph. */
12951 int strpos = glyph->charpos;
12952
12953 cursor = glyph;
12954 for (glyph += incr;
12955 EQ (glyph->object, str);
12956 glyph += incr)
12957 {
12958 Lisp_Object cprop;
12959 int gpos = glyph->charpos;
12960
12961 cprop = Fget_char_property (make_number (gpos),
12962 Qcursor,
12963 glyph->object);
12964 if (!NILP (cprop))
12965 {
12966 cursor = glyph;
12967 break;
12968 }
12969 if (glyph->charpos < strpos)
12970 {
12971 strpos = glyph->charpos;
12972 cursor = glyph;
12973 }
12974 }
12975
12976 goto compute_x;
12977 }
12978 pos = tem + 1; /* don't find previous instances */
12979 }
12980 /* This string is not what we want; skip all of the
12981 glyphs that came from it. */
12982 do
12983 glyph += incr;
12984 while ((row->reversed_p ? glyph > stop : glyph < stop)
12985 && EQ (glyph->object, str));
12986 }
12987 else
12988 glyph += incr;
12989 }
12990
12991 /* If we reached the end of the line, and END was from a string,
12992 the cursor is not on this line. */
12993 if (glyph == end
12994 && STRINGP ((glyph - incr)->object)
12995 && row->continued_p)
12996 return 0;
12997 }
12998 }
12999
13000 compute_x:
13001 if (cursor != NULL)
13002 glyph = cursor;
13003 if (x < 0)
13004 {
13005 struct glyph *g;
13006
13007 /* Need to compute x that corresponds to GLYPH. */
13008 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13009 {
13010 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13011 abort ();
13012 x += g->pixel_width;
13013 }
13014 }
13015
13016 /* ROW could be part of a continued line, which might have other
13017 rows whose start and end charpos occlude point. Only set
13018 w->cursor if we found a better approximation to the cursor
13019 position than we have from previously examined rows. */
13020 if (w->cursor.vpos >= 0
13021 /* Make sure cursor.vpos specifies a row whose start and end
13022 charpos occlude point. This is because some callers of this
13023 function leave cursor.vpos at the row where the cursor was
13024 displayed during the last redisplay cycle. */
13025 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13026 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13027 {
13028 struct glyph *g1 =
13029 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13030
13031 /* Keep the candidate whose buffer position is the closest to
13032 point. */
13033 if (BUFFERP (g1->object)
13034 && (g1->charpos == pt_old /* an exact match always wins */
13035 || (BUFFERP (glyph->object)
13036 && eabs (g1->charpos - pt_old)
13037 < eabs (glyph->charpos - pt_old))))
13038 return 0;
13039 /* If this candidate gives an exact match, use that. */
13040 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13041 /* Otherwise, keep the candidate that comes from a row
13042 spanning less buffer positions. This may win when one or
13043 both candidate positions are on glyphs that came from
13044 display strings, for which we cannot compare buffer
13045 positions. */
13046 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13047 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13048 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13049 return 0;
13050 }
13051 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13052 w->cursor.x = x;
13053 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13054 w->cursor.y = row->y + dy;
13055
13056 if (w == XWINDOW (selected_window))
13057 {
13058 if (!row->continued_p
13059 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13060 && row->x == 0)
13061 {
13062 this_line_buffer = XBUFFER (w->buffer);
13063
13064 CHARPOS (this_line_start_pos)
13065 = MATRIX_ROW_START_CHARPOS (row) + delta;
13066 BYTEPOS (this_line_start_pos)
13067 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13068
13069 CHARPOS (this_line_end_pos)
13070 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13071 BYTEPOS (this_line_end_pos)
13072 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13073
13074 this_line_y = w->cursor.y;
13075 this_line_pixel_height = row->height;
13076 this_line_vpos = w->cursor.vpos;
13077 this_line_start_x = row->x;
13078 }
13079 else
13080 CHARPOS (this_line_start_pos) = 0;
13081 }
13082
13083 return 1;
13084 }
13085
13086
13087 /* Run window scroll functions, if any, for WINDOW with new window
13088 start STARTP. Sets the window start of WINDOW to that position.
13089
13090 We assume that the window's buffer is really current. */
13091
13092 static INLINE struct text_pos
13093 run_window_scroll_functions (window, startp)
13094 Lisp_Object window;
13095 struct text_pos startp;
13096 {
13097 struct window *w = XWINDOW (window);
13098 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13099
13100 if (current_buffer != XBUFFER (w->buffer))
13101 abort ();
13102
13103 if (!NILP (Vwindow_scroll_functions))
13104 {
13105 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13106 make_number (CHARPOS (startp)));
13107 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13108 /* In case the hook functions switch buffers. */
13109 if (current_buffer != XBUFFER (w->buffer))
13110 set_buffer_internal_1 (XBUFFER (w->buffer));
13111 }
13112
13113 return startp;
13114 }
13115
13116
13117 /* Make sure the line containing the cursor is fully visible.
13118 A value of 1 means there is nothing to be done.
13119 (Either the line is fully visible, or it cannot be made so,
13120 or we cannot tell.)
13121
13122 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13123 is higher than window.
13124
13125 A value of 0 means the caller should do scrolling
13126 as if point had gone off the screen. */
13127
13128 static int
13129 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
13130 struct window *w;
13131 int force_p;
13132 int current_matrix_p;
13133 {
13134 struct glyph_matrix *matrix;
13135 struct glyph_row *row;
13136 int window_height;
13137
13138 if (!make_cursor_line_fully_visible_p)
13139 return 1;
13140
13141 /* It's not always possible to find the cursor, e.g, when a window
13142 is full of overlay strings. Don't do anything in that case. */
13143 if (w->cursor.vpos < 0)
13144 return 1;
13145
13146 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13147 row = MATRIX_ROW (matrix, w->cursor.vpos);
13148
13149 /* If the cursor row is not partially visible, there's nothing to do. */
13150 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13151 return 1;
13152
13153 /* If the row the cursor is in is taller than the window's height,
13154 it's not clear what to do, so do nothing. */
13155 window_height = window_box_height (w);
13156 if (row->height >= window_height)
13157 {
13158 if (!force_p || MINI_WINDOW_P (w)
13159 || w->vscroll || w->cursor.vpos == 0)
13160 return 1;
13161 }
13162 return 0;
13163 }
13164
13165
13166 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13167 non-zero means only WINDOW is redisplayed in redisplay_internal.
13168 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13169 in redisplay_window to bring a partially visible line into view in
13170 the case that only the cursor has moved.
13171
13172 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13173 last screen line's vertical height extends past the end of the screen.
13174
13175 Value is
13176
13177 1 if scrolling succeeded
13178
13179 0 if scrolling didn't find point.
13180
13181 -1 if new fonts have been loaded so that we must interrupt
13182 redisplay, adjust glyph matrices, and try again. */
13183
13184 enum
13185 {
13186 SCROLLING_SUCCESS,
13187 SCROLLING_FAILED,
13188 SCROLLING_NEED_LARGER_MATRICES
13189 };
13190
13191 static int
13192 try_scrolling (window, just_this_one_p, scroll_conservatively,
13193 scroll_step, temp_scroll_step, last_line_misfit)
13194 Lisp_Object window;
13195 int just_this_one_p;
13196 EMACS_INT scroll_conservatively, scroll_step;
13197 int temp_scroll_step;
13198 int last_line_misfit;
13199 {
13200 struct window *w = XWINDOW (window);
13201 struct frame *f = XFRAME (w->frame);
13202 struct text_pos pos, startp;
13203 struct it it;
13204 int this_scroll_margin, scroll_max, rc, height;
13205 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13206 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13207 Lisp_Object aggressive;
13208 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13209
13210 #if GLYPH_DEBUG
13211 debug_method_add (w, "try_scrolling");
13212 #endif
13213
13214 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13215
13216 /* Compute scroll margin height in pixels. We scroll when point is
13217 within this distance from the top or bottom of the window. */
13218 if (scroll_margin > 0)
13219 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13220 * FRAME_LINE_HEIGHT (f);
13221 else
13222 this_scroll_margin = 0;
13223
13224 /* Force scroll_conservatively to have a reasonable value, to avoid
13225 overflow while computing how much to scroll. Note that the user
13226 can supply scroll-conservatively equal to `most-positive-fixnum',
13227 which can be larger than INT_MAX. */
13228 if (scroll_conservatively > scroll_limit)
13229 {
13230 scroll_conservatively = scroll_limit;
13231 scroll_max = INT_MAX;
13232 }
13233 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13234 /* Compute how much we should try to scroll maximally to bring
13235 point into view. */
13236 scroll_max = (max (scroll_step,
13237 max (scroll_conservatively, temp_scroll_step))
13238 * FRAME_LINE_HEIGHT (f));
13239 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13240 || NUMBERP (current_buffer->scroll_up_aggressively))
13241 /* We're trying to scroll because of aggressive scrolling but no
13242 scroll_step is set. Choose an arbitrary one. */
13243 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13244 else
13245 scroll_max = 0;
13246
13247 too_near_end:
13248
13249 /* Decide whether to scroll down. */
13250 if (PT > CHARPOS (startp))
13251 {
13252 int scroll_margin_y;
13253
13254 /* Compute the pixel ypos of the scroll margin, then move it to
13255 either that ypos or PT, whichever comes first. */
13256 start_display (&it, w, startp);
13257 scroll_margin_y = it.last_visible_y - this_scroll_margin
13258 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13259 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13260 (MOVE_TO_POS | MOVE_TO_Y));
13261
13262 if (PT > CHARPOS (it.current.pos))
13263 {
13264 int y0 = line_bottom_y (&it);
13265
13266 /* Compute the distance from the scroll margin to PT
13267 (including the height of the cursor line). Moving the
13268 iterator unconditionally to PT can be slow if PT is far
13269 away, so stop 10 lines past the window bottom (is there a
13270 way to do the right thing quickly?). */
13271 move_it_to (&it, PT, -1,
13272 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
13273 -1, MOVE_TO_POS | MOVE_TO_Y);
13274 dy = line_bottom_y (&it) - y0;
13275
13276 if (dy > scroll_max)
13277 return SCROLLING_FAILED;
13278
13279 scroll_down_p = 1;
13280 }
13281 }
13282
13283 if (scroll_down_p)
13284 {
13285 /* Point is in or below the bottom scroll margin, so move the
13286 window start down. If scrolling conservatively, move it just
13287 enough down to make point visible. If scroll_step is set,
13288 move it down by scroll_step. */
13289 if (scroll_conservatively)
13290 amount_to_scroll
13291 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13292 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13293 else if (scroll_step || temp_scroll_step)
13294 amount_to_scroll = scroll_max;
13295 else
13296 {
13297 aggressive = current_buffer->scroll_up_aggressively;
13298 height = WINDOW_BOX_TEXT_HEIGHT (w);
13299 if (NUMBERP (aggressive))
13300 {
13301 double float_amount = XFLOATINT (aggressive) * height;
13302 amount_to_scroll = float_amount;
13303 if (amount_to_scroll == 0 && float_amount > 0)
13304 amount_to_scroll = 1;
13305 }
13306 }
13307
13308 if (amount_to_scroll <= 0)
13309 return SCROLLING_FAILED;
13310
13311 start_display (&it, w, startp);
13312 move_it_vertically (&it, amount_to_scroll);
13313
13314 /* If STARTP is unchanged, move it down another screen line. */
13315 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13316 move_it_by_lines (&it, 1, 1);
13317 startp = it.current.pos;
13318 }
13319 else
13320 {
13321 struct text_pos scroll_margin_pos = startp;
13322
13323 /* See if point is inside the scroll margin at the top of the
13324 window. */
13325 if (this_scroll_margin)
13326 {
13327 start_display (&it, w, startp);
13328 move_it_vertically (&it, this_scroll_margin);
13329 scroll_margin_pos = it.current.pos;
13330 }
13331
13332 if (PT < CHARPOS (scroll_margin_pos))
13333 {
13334 /* Point is in the scroll margin at the top of the window or
13335 above what is displayed in the window. */
13336 int y0;
13337
13338 /* Compute the vertical distance from PT to the scroll
13339 margin position. Give up if distance is greater than
13340 scroll_max. */
13341 SET_TEXT_POS (pos, PT, PT_BYTE);
13342 start_display (&it, w, pos);
13343 y0 = it.current_y;
13344 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13345 it.last_visible_y, -1,
13346 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13347 dy = it.current_y - y0;
13348 if (dy > scroll_max)
13349 return SCROLLING_FAILED;
13350
13351 /* Compute new window start. */
13352 start_display (&it, w, startp);
13353
13354 if (scroll_conservatively)
13355 amount_to_scroll
13356 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13357 else if (scroll_step || temp_scroll_step)
13358 amount_to_scroll = scroll_max;
13359 else
13360 {
13361 aggressive = current_buffer->scroll_down_aggressively;
13362 height = WINDOW_BOX_TEXT_HEIGHT (w);
13363 if (NUMBERP (aggressive))
13364 {
13365 double float_amount = XFLOATINT (aggressive) * height;
13366 amount_to_scroll = float_amount;
13367 if (amount_to_scroll == 0 && float_amount > 0)
13368 amount_to_scroll = 1;
13369 }
13370 }
13371
13372 if (amount_to_scroll <= 0)
13373 return SCROLLING_FAILED;
13374
13375 move_it_vertically_backward (&it, amount_to_scroll);
13376 startp = it.current.pos;
13377 }
13378 }
13379
13380 /* Run window scroll functions. */
13381 startp = run_window_scroll_functions (window, startp);
13382
13383 /* Display the window. Give up if new fonts are loaded, or if point
13384 doesn't appear. */
13385 if (!try_window (window, startp, 0))
13386 rc = SCROLLING_NEED_LARGER_MATRICES;
13387 else if (w->cursor.vpos < 0)
13388 {
13389 clear_glyph_matrix (w->desired_matrix);
13390 rc = SCROLLING_FAILED;
13391 }
13392 else
13393 {
13394 /* Maybe forget recorded base line for line number display. */
13395 if (!just_this_one_p
13396 || current_buffer->clip_changed
13397 || BEG_UNCHANGED < CHARPOS (startp))
13398 w->base_line_number = Qnil;
13399
13400 /* If cursor ends up on a partially visible line,
13401 treat that as being off the bottom of the screen. */
13402 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13403 {
13404 clear_glyph_matrix (w->desired_matrix);
13405 ++extra_scroll_margin_lines;
13406 goto too_near_end;
13407 }
13408 rc = SCROLLING_SUCCESS;
13409 }
13410
13411 return rc;
13412 }
13413
13414
13415 /* Compute a suitable window start for window W if display of W starts
13416 on a continuation line. Value is non-zero if a new window start
13417 was computed.
13418
13419 The new window start will be computed, based on W's width, starting
13420 from the start of the continued line. It is the start of the
13421 screen line with the minimum distance from the old start W->start. */
13422
13423 static int
13424 compute_window_start_on_continuation_line (w)
13425 struct window *w;
13426 {
13427 struct text_pos pos, start_pos;
13428 int window_start_changed_p = 0;
13429
13430 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13431
13432 /* If window start is on a continuation line... Window start may be
13433 < BEGV in case there's invisible text at the start of the
13434 buffer (M-x rmail, for example). */
13435 if (CHARPOS (start_pos) > BEGV
13436 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13437 {
13438 struct it it;
13439 struct glyph_row *row;
13440
13441 /* Handle the case that the window start is out of range. */
13442 if (CHARPOS (start_pos) < BEGV)
13443 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13444 else if (CHARPOS (start_pos) > ZV)
13445 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13446
13447 /* Find the start of the continued line. This should be fast
13448 because scan_buffer is fast (newline cache). */
13449 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13450 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13451 row, DEFAULT_FACE_ID);
13452 reseat_at_previous_visible_line_start (&it);
13453
13454 /* If the line start is "too far" away from the window start,
13455 say it takes too much time to compute a new window start. */
13456 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13457 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13458 {
13459 int min_distance, distance;
13460
13461 /* Move forward by display lines to find the new window
13462 start. If window width was enlarged, the new start can
13463 be expected to be > the old start. If window width was
13464 decreased, the new window start will be < the old start.
13465 So, we're looking for the display line start with the
13466 minimum distance from the old window start. */
13467 pos = it.current.pos;
13468 min_distance = INFINITY;
13469 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13470 distance < min_distance)
13471 {
13472 min_distance = distance;
13473 pos = it.current.pos;
13474 move_it_by_lines (&it, 1, 0);
13475 }
13476
13477 /* Set the window start there. */
13478 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13479 window_start_changed_p = 1;
13480 }
13481 }
13482
13483 return window_start_changed_p;
13484 }
13485
13486
13487 /* Try cursor movement in case text has not changed in window WINDOW,
13488 with window start STARTP. Value is
13489
13490 CURSOR_MOVEMENT_SUCCESS if successful
13491
13492 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13493
13494 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13495 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13496 we want to scroll as if scroll-step were set to 1. See the code.
13497
13498 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13499 which case we have to abort this redisplay, and adjust matrices
13500 first. */
13501
13502 enum
13503 {
13504 CURSOR_MOVEMENT_SUCCESS,
13505 CURSOR_MOVEMENT_CANNOT_BE_USED,
13506 CURSOR_MOVEMENT_MUST_SCROLL,
13507 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13508 };
13509
13510 static int
13511 try_cursor_movement (window, startp, scroll_step)
13512 Lisp_Object window;
13513 struct text_pos startp;
13514 int *scroll_step;
13515 {
13516 struct window *w = XWINDOW (window);
13517 struct frame *f = XFRAME (w->frame);
13518 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13519
13520 #if GLYPH_DEBUG
13521 if (inhibit_try_cursor_movement)
13522 return rc;
13523 #endif
13524
13525 /* Handle case where text has not changed, only point, and it has
13526 not moved off the frame. */
13527 if (/* Point may be in this window. */
13528 PT >= CHARPOS (startp)
13529 /* Selective display hasn't changed. */
13530 && !current_buffer->clip_changed
13531 /* Function force-mode-line-update is used to force a thorough
13532 redisplay. It sets either windows_or_buffers_changed or
13533 update_mode_lines. So don't take a shortcut here for these
13534 cases. */
13535 && !update_mode_lines
13536 && !windows_or_buffers_changed
13537 && !cursor_type_changed
13538 /* Can't use this case if highlighting a region. When a
13539 region exists, cursor movement has to do more than just
13540 set the cursor. */
13541 && !(!NILP (Vtransient_mark_mode)
13542 && !NILP (current_buffer->mark_active))
13543 && NILP (w->region_showing)
13544 && NILP (Vshow_trailing_whitespace)
13545 /* Right after splitting windows, last_point may be nil. */
13546 && INTEGERP (w->last_point)
13547 /* This code is not used for mini-buffer for the sake of the case
13548 of redisplaying to replace an echo area message; since in
13549 that case the mini-buffer contents per se are usually
13550 unchanged. This code is of no real use in the mini-buffer
13551 since the handling of this_line_start_pos, etc., in redisplay
13552 handles the same cases. */
13553 && !EQ (window, minibuf_window)
13554 /* When splitting windows or for new windows, it happens that
13555 redisplay is called with a nil window_end_vpos or one being
13556 larger than the window. This should really be fixed in
13557 window.c. I don't have this on my list, now, so we do
13558 approximately the same as the old redisplay code. --gerd. */
13559 && INTEGERP (w->window_end_vpos)
13560 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13561 && (FRAME_WINDOW_P (f)
13562 || !overlay_arrow_in_current_buffer_p ()))
13563 {
13564 int this_scroll_margin, top_scroll_margin;
13565 struct glyph_row *row = NULL;
13566
13567 #if GLYPH_DEBUG
13568 debug_method_add (w, "cursor movement");
13569 #endif
13570
13571 /* Scroll if point within this distance from the top or bottom
13572 of the window. This is a pixel value. */
13573 if (scroll_margin > 0)
13574 {
13575 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13576 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13577 }
13578 else
13579 this_scroll_margin = 0;
13580
13581 top_scroll_margin = this_scroll_margin;
13582 if (WINDOW_WANTS_HEADER_LINE_P (w))
13583 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13584
13585 /* Start with the row the cursor was displayed during the last
13586 not paused redisplay. Give up if that row is not valid. */
13587 if (w->last_cursor.vpos < 0
13588 || w->last_cursor.vpos >= w->current_matrix->nrows)
13589 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13590 else
13591 {
13592 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13593 if (row->mode_line_p)
13594 ++row;
13595 if (!row->enabled_p)
13596 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13597 /* If rows are bidi-reordered, back up until we find a row
13598 that does not belong to a continuation line. This is
13599 because we must consider all rows of a continued line as
13600 candidates for cursor positioning, since row start and
13601 end positions change non-linearly with vertical position
13602 in such rows. */
13603 /* FIXME: Revisit this when glyph ``spilling'' in
13604 continuation lines' rows is implemented for
13605 bidi-reordered rows. */
13606 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13607 {
13608 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13609 {
13610 xassert (row->enabled_p);
13611 --row;
13612 /* If we hit the beginning of the displayed portion
13613 without finding the first row of a continued
13614 line, give up. */
13615 if (row <= w->current_matrix->rows)
13616 {
13617 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13618 break;
13619 }
13620
13621 }
13622 }
13623 }
13624
13625 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13626 {
13627 int scroll_p = 0;
13628 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13629
13630 if (PT > XFASTINT (w->last_point))
13631 {
13632 /* Point has moved forward. */
13633 while (MATRIX_ROW_END_CHARPOS (row) < PT
13634 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13635 {
13636 xassert (row->enabled_p);
13637 ++row;
13638 }
13639
13640 /* The end position of a row equals the start position
13641 of the next row. If PT is there, we would rather
13642 display it in the next line. */
13643 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13644 && MATRIX_ROW_END_CHARPOS (row) == PT
13645 && !cursor_row_p (w, row))
13646 ++row;
13647
13648 /* If within the scroll margin, scroll. Note that
13649 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13650 the next line would be drawn, and that
13651 this_scroll_margin can be zero. */
13652 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13653 || PT > MATRIX_ROW_END_CHARPOS (row)
13654 /* Line is completely visible last line in window
13655 and PT is to be set in the next line. */
13656 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13657 && PT == MATRIX_ROW_END_CHARPOS (row)
13658 && !row->ends_at_zv_p
13659 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13660 scroll_p = 1;
13661 }
13662 else if (PT < XFASTINT (w->last_point))
13663 {
13664 /* Cursor has to be moved backward. Note that PT >=
13665 CHARPOS (startp) because of the outer if-statement. */
13666 while (!row->mode_line_p
13667 && (MATRIX_ROW_START_CHARPOS (row) > PT
13668 || (MATRIX_ROW_START_CHARPOS (row) == PT
13669 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13670 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13671 row > w->current_matrix->rows
13672 && (row-1)->ends_in_newline_from_string_p))))
13673 && (row->y > top_scroll_margin
13674 || CHARPOS (startp) == BEGV))
13675 {
13676 xassert (row->enabled_p);
13677 --row;
13678 }
13679
13680 /* Consider the following case: Window starts at BEGV,
13681 there is invisible, intangible text at BEGV, so that
13682 display starts at some point START > BEGV. It can
13683 happen that we are called with PT somewhere between
13684 BEGV and START. Try to handle that case. */
13685 if (row < w->current_matrix->rows
13686 || row->mode_line_p)
13687 {
13688 row = w->current_matrix->rows;
13689 if (row->mode_line_p)
13690 ++row;
13691 }
13692
13693 /* Due to newlines in overlay strings, we may have to
13694 skip forward over overlay strings. */
13695 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13696 && MATRIX_ROW_END_CHARPOS (row) == PT
13697 && !cursor_row_p (w, row))
13698 ++row;
13699
13700 /* If within the scroll margin, scroll. */
13701 if (row->y < top_scroll_margin
13702 && CHARPOS (startp) != BEGV)
13703 scroll_p = 1;
13704 }
13705 else
13706 {
13707 /* Cursor did not move. So don't scroll even if cursor line
13708 is partially visible, as it was so before. */
13709 rc = CURSOR_MOVEMENT_SUCCESS;
13710 }
13711
13712 if (PT < MATRIX_ROW_START_CHARPOS (row)
13713 || PT > MATRIX_ROW_END_CHARPOS (row))
13714 {
13715 /* if PT is not in the glyph row, give up. */
13716 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13717 }
13718 else if (rc != CURSOR_MOVEMENT_SUCCESS
13719 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13720 && make_cursor_line_fully_visible_p)
13721 {
13722 if (PT == MATRIX_ROW_END_CHARPOS (row)
13723 && !row->ends_at_zv_p
13724 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13725 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13726 else if (row->height > window_box_height (w))
13727 {
13728 /* If we end up in a partially visible line, let's
13729 make it fully visible, except when it's taller
13730 than the window, in which case we can't do much
13731 about it. */
13732 *scroll_step = 1;
13733 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13734 }
13735 else
13736 {
13737 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13738 if (!cursor_row_fully_visible_p (w, 0, 1))
13739 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13740 else
13741 rc = CURSOR_MOVEMENT_SUCCESS;
13742 }
13743 }
13744 else if (scroll_p)
13745 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13746 else if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13747 {
13748 /* With bidi-reordered rows, there could be more than
13749 one candidate row whose start and end positions
13750 occlude point. We need to let set_cursor_from_row
13751 find the best candidate. */
13752 /* FIXME: Revisit this when glyph ``spilling'' in
13753 continuation lines' rows is implemented for
13754 bidi-reordered rows. */
13755 int rv = 0;
13756
13757 do
13758 {
13759 rv |= set_cursor_from_row (w, row, w->current_matrix,
13760 0, 0, 0, 0);
13761 /* As soon as we've found the first suitable row
13762 whose ends_at_zv_p flag is set, we are done. */
13763 if (rv
13764 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13765 {
13766 rc = CURSOR_MOVEMENT_SUCCESS;
13767 break;
13768 }
13769 ++row;
13770 }
13771 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13772 && MATRIX_ROW_START_CHARPOS (row) <= PT
13773 && PT <= MATRIX_ROW_END_CHARPOS (row)
13774 && cursor_row_p (w, row));
13775 /* If we didn't find any candidate rows, or exited the
13776 loop before all the candidates were examined, signal
13777 to the caller that this method failed. */
13778 if (rc != CURSOR_MOVEMENT_SUCCESS
13779 && (!rv
13780 || (MATRIX_ROW_START_CHARPOS (row) <= PT
13781 && PT <= MATRIX_ROW_END_CHARPOS (row))))
13782 rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13783 else
13784 rc = CURSOR_MOVEMENT_SUCCESS;
13785 }
13786 else
13787 {
13788 do
13789 {
13790 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13791 {
13792 rc = CURSOR_MOVEMENT_SUCCESS;
13793 break;
13794 }
13795 ++row;
13796 }
13797 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13798 && MATRIX_ROW_START_CHARPOS (row) == PT
13799 && cursor_row_p (w, row));
13800 }
13801 }
13802 }
13803
13804 return rc;
13805 }
13806
13807 void
13808 set_vertical_scroll_bar (w)
13809 struct window *w;
13810 {
13811 int start, end, whole;
13812
13813 /* Calculate the start and end positions for the current window.
13814 At some point, it would be nice to choose between scrollbars
13815 which reflect the whole buffer size, with special markers
13816 indicating narrowing, and scrollbars which reflect only the
13817 visible region.
13818
13819 Note that mini-buffers sometimes aren't displaying any text. */
13820 if (!MINI_WINDOW_P (w)
13821 || (w == XWINDOW (minibuf_window)
13822 && NILP (echo_area_buffer[0])))
13823 {
13824 struct buffer *buf = XBUFFER (w->buffer);
13825 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13826 start = marker_position (w->start) - BUF_BEGV (buf);
13827 /* I don't think this is guaranteed to be right. For the
13828 moment, we'll pretend it is. */
13829 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13830
13831 if (end < start)
13832 end = start;
13833 if (whole < (end - start))
13834 whole = end - start;
13835 }
13836 else
13837 start = end = whole = 0;
13838
13839 /* Indicate what this scroll bar ought to be displaying now. */
13840 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13841 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13842 (w, end - start, whole, start);
13843 }
13844
13845
13846 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13847 selected_window is redisplayed.
13848
13849 We can return without actually redisplaying the window if
13850 fonts_changed_p is nonzero. In that case, redisplay_internal will
13851 retry. */
13852
13853 static void
13854 redisplay_window (window, just_this_one_p)
13855 Lisp_Object window;
13856 int just_this_one_p;
13857 {
13858 struct window *w = XWINDOW (window);
13859 struct frame *f = XFRAME (w->frame);
13860 struct buffer *buffer = XBUFFER (w->buffer);
13861 struct buffer *old = current_buffer;
13862 struct text_pos lpoint, opoint, startp;
13863 int update_mode_line;
13864 int tem;
13865 struct it it;
13866 /* Record it now because it's overwritten. */
13867 int current_matrix_up_to_date_p = 0;
13868 int used_current_matrix_p = 0;
13869 /* This is less strict than current_matrix_up_to_date_p.
13870 It indictes that the buffer contents and narrowing are unchanged. */
13871 int buffer_unchanged_p = 0;
13872 int temp_scroll_step = 0;
13873 int count = SPECPDL_INDEX ();
13874 int rc;
13875 int centering_position = -1;
13876 int last_line_misfit = 0;
13877 int beg_unchanged, end_unchanged;
13878
13879 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13880 opoint = lpoint;
13881
13882 /* W must be a leaf window here. */
13883 xassert (!NILP (w->buffer));
13884 #if GLYPH_DEBUG
13885 *w->desired_matrix->method = 0;
13886 #endif
13887
13888 restart:
13889 reconsider_clip_changes (w, buffer);
13890
13891 /* Has the mode line to be updated? */
13892 update_mode_line = (!NILP (w->update_mode_line)
13893 || update_mode_lines
13894 || buffer->clip_changed
13895 || buffer->prevent_redisplay_optimizations_p);
13896
13897 if (MINI_WINDOW_P (w))
13898 {
13899 if (w == XWINDOW (echo_area_window)
13900 && !NILP (echo_area_buffer[0]))
13901 {
13902 if (update_mode_line)
13903 /* We may have to update a tty frame's menu bar or a
13904 tool-bar. Example `M-x C-h C-h C-g'. */
13905 goto finish_menu_bars;
13906 else
13907 /* We've already displayed the echo area glyphs in this window. */
13908 goto finish_scroll_bars;
13909 }
13910 else if ((w != XWINDOW (minibuf_window)
13911 || minibuf_level == 0)
13912 /* When buffer is nonempty, redisplay window normally. */
13913 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13914 /* Quail displays non-mini buffers in minibuffer window.
13915 In that case, redisplay the window normally. */
13916 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13917 {
13918 /* W is a mini-buffer window, but it's not active, so clear
13919 it. */
13920 int yb = window_text_bottom_y (w);
13921 struct glyph_row *row;
13922 int y;
13923
13924 for (y = 0, row = w->desired_matrix->rows;
13925 y < yb;
13926 y += row->height, ++row)
13927 blank_row (w, row, y);
13928 goto finish_scroll_bars;
13929 }
13930
13931 clear_glyph_matrix (w->desired_matrix);
13932 }
13933
13934 /* Otherwise set up data on this window; select its buffer and point
13935 value. */
13936 /* Really select the buffer, for the sake of buffer-local
13937 variables. */
13938 set_buffer_internal_1 (XBUFFER (w->buffer));
13939
13940 current_matrix_up_to_date_p
13941 = (!NILP (w->window_end_valid)
13942 && !current_buffer->clip_changed
13943 && !current_buffer->prevent_redisplay_optimizations_p
13944 && XFASTINT (w->last_modified) >= MODIFF
13945 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13946
13947 /* Run the window-bottom-change-functions
13948 if it is possible that the text on the screen has changed
13949 (either due to modification of the text, or any other reason). */
13950 if (!current_matrix_up_to_date_p
13951 && !NILP (Vwindow_text_change_functions))
13952 {
13953 safe_run_hooks (Qwindow_text_change_functions);
13954 goto restart;
13955 }
13956
13957 beg_unchanged = BEG_UNCHANGED;
13958 end_unchanged = END_UNCHANGED;
13959
13960 SET_TEXT_POS (opoint, PT, PT_BYTE);
13961
13962 specbind (Qinhibit_point_motion_hooks, Qt);
13963
13964 buffer_unchanged_p
13965 = (!NILP (w->window_end_valid)
13966 && !current_buffer->clip_changed
13967 && XFASTINT (w->last_modified) >= MODIFF
13968 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13969
13970 /* When windows_or_buffers_changed is non-zero, we can't rely on
13971 the window end being valid, so set it to nil there. */
13972 if (windows_or_buffers_changed)
13973 {
13974 /* If window starts on a continuation line, maybe adjust the
13975 window start in case the window's width changed. */
13976 if (XMARKER (w->start)->buffer == current_buffer)
13977 compute_window_start_on_continuation_line (w);
13978
13979 w->window_end_valid = Qnil;
13980 }
13981
13982 /* Some sanity checks. */
13983 CHECK_WINDOW_END (w);
13984 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13985 abort ();
13986 if (BYTEPOS (opoint) < CHARPOS (opoint))
13987 abort ();
13988
13989 /* If %c is in mode line, update it if needed. */
13990 if (!NILP (w->column_number_displayed)
13991 /* This alternative quickly identifies a common case
13992 where no change is needed. */
13993 && !(PT == XFASTINT (w->last_point)
13994 && XFASTINT (w->last_modified) >= MODIFF
13995 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13996 && (XFASTINT (w->column_number_displayed)
13997 != (int) current_column ())) /* iftc */
13998 update_mode_line = 1;
13999
14000 /* Count number of windows showing the selected buffer. An indirect
14001 buffer counts as its base buffer. */
14002 if (!just_this_one_p)
14003 {
14004 struct buffer *current_base, *window_base;
14005 current_base = current_buffer;
14006 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14007 if (current_base->base_buffer)
14008 current_base = current_base->base_buffer;
14009 if (window_base->base_buffer)
14010 window_base = window_base->base_buffer;
14011 if (current_base == window_base)
14012 buffer_shared++;
14013 }
14014
14015 /* Point refers normally to the selected window. For any other
14016 window, set up appropriate value. */
14017 if (!EQ (window, selected_window))
14018 {
14019 int new_pt = XMARKER (w->pointm)->charpos;
14020 int new_pt_byte = marker_byte_position (w->pointm);
14021 if (new_pt < BEGV)
14022 {
14023 new_pt = BEGV;
14024 new_pt_byte = BEGV_BYTE;
14025 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14026 }
14027 else if (new_pt > (ZV - 1))
14028 {
14029 new_pt = ZV;
14030 new_pt_byte = ZV_BYTE;
14031 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14032 }
14033
14034 /* We don't use SET_PT so that the point-motion hooks don't run. */
14035 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14036 }
14037
14038 /* If any of the character widths specified in the display table
14039 have changed, invalidate the width run cache. It's true that
14040 this may be a bit late to catch such changes, but the rest of
14041 redisplay goes (non-fatally) haywire when the display table is
14042 changed, so why should we worry about doing any better? */
14043 if (current_buffer->width_run_cache)
14044 {
14045 struct Lisp_Char_Table *disptab = buffer_display_table ();
14046
14047 if (! disptab_matches_widthtab (disptab,
14048 XVECTOR (current_buffer->width_table)))
14049 {
14050 invalidate_region_cache (current_buffer,
14051 current_buffer->width_run_cache,
14052 BEG, Z);
14053 recompute_width_table (current_buffer, disptab);
14054 }
14055 }
14056
14057 /* If window-start is screwed up, choose a new one. */
14058 if (XMARKER (w->start)->buffer != current_buffer)
14059 goto recenter;
14060
14061 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14062
14063 /* If someone specified a new starting point but did not insist,
14064 check whether it can be used. */
14065 if (!NILP (w->optional_new_start)
14066 && CHARPOS (startp) >= BEGV
14067 && CHARPOS (startp) <= ZV)
14068 {
14069 w->optional_new_start = Qnil;
14070 start_display (&it, w, startp);
14071 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14072 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14073 if (IT_CHARPOS (it) == PT)
14074 w->force_start = Qt;
14075 /* IT may overshoot PT if text at PT is invisible. */
14076 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14077 w->force_start = Qt;
14078 }
14079
14080 force_start:
14081
14082 /* Handle case where place to start displaying has been specified,
14083 unless the specified location is outside the accessible range. */
14084 if (!NILP (w->force_start)
14085 || w->frozen_window_start_p)
14086 {
14087 /* We set this later on if we have to adjust point. */
14088 int new_vpos = -1;
14089
14090 w->force_start = Qnil;
14091 w->vscroll = 0;
14092 w->window_end_valid = Qnil;
14093
14094 /* Forget any recorded base line for line number display. */
14095 if (!buffer_unchanged_p)
14096 w->base_line_number = Qnil;
14097
14098 /* Redisplay the mode line. Select the buffer properly for that.
14099 Also, run the hook window-scroll-functions
14100 because we have scrolled. */
14101 /* Note, we do this after clearing force_start because
14102 if there's an error, it is better to forget about force_start
14103 than to get into an infinite loop calling the hook functions
14104 and having them get more errors. */
14105 if (!update_mode_line
14106 || ! NILP (Vwindow_scroll_functions))
14107 {
14108 update_mode_line = 1;
14109 w->update_mode_line = Qt;
14110 startp = run_window_scroll_functions (window, startp);
14111 }
14112
14113 w->last_modified = make_number (0);
14114 w->last_overlay_modified = make_number (0);
14115 if (CHARPOS (startp) < BEGV)
14116 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14117 else if (CHARPOS (startp) > ZV)
14118 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14119
14120 /* Redisplay, then check if cursor has been set during the
14121 redisplay. Give up if new fonts were loaded. */
14122 /* We used to issue a CHECK_MARGINS argument to try_window here,
14123 but this causes scrolling to fail when point begins inside
14124 the scroll margin (bug#148) -- cyd */
14125 if (!try_window (window, startp, 0))
14126 {
14127 w->force_start = Qt;
14128 clear_glyph_matrix (w->desired_matrix);
14129 goto need_larger_matrices;
14130 }
14131
14132 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14133 {
14134 /* If point does not appear, try to move point so it does
14135 appear. The desired matrix has been built above, so we
14136 can use it here. */
14137 new_vpos = window_box_height (w) / 2;
14138 }
14139
14140 if (!cursor_row_fully_visible_p (w, 0, 0))
14141 {
14142 /* Point does appear, but on a line partly visible at end of window.
14143 Move it back to a fully-visible line. */
14144 new_vpos = window_box_height (w);
14145 }
14146
14147 /* If we need to move point for either of the above reasons,
14148 now actually do it. */
14149 if (new_vpos >= 0)
14150 {
14151 struct glyph_row *row;
14152
14153 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14154 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14155 ++row;
14156
14157 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14158 MATRIX_ROW_START_BYTEPOS (row));
14159
14160 if (w != XWINDOW (selected_window))
14161 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14162 else if (current_buffer == old)
14163 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14164
14165 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14166
14167 /* If we are highlighting the region, then we just changed
14168 the region, so redisplay to show it. */
14169 if (!NILP (Vtransient_mark_mode)
14170 && !NILP (current_buffer->mark_active))
14171 {
14172 clear_glyph_matrix (w->desired_matrix);
14173 if (!try_window (window, startp, 0))
14174 goto need_larger_matrices;
14175 }
14176 }
14177
14178 #if GLYPH_DEBUG
14179 debug_method_add (w, "forced window start");
14180 #endif
14181 goto done;
14182 }
14183
14184 /* Handle case where text has not changed, only point, and it has
14185 not moved off the frame, and we are not retrying after hscroll.
14186 (current_matrix_up_to_date_p is nonzero when retrying.) */
14187 if (current_matrix_up_to_date_p
14188 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14189 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14190 {
14191 switch (rc)
14192 {
14193 case CURSOR_MOVEMENT_SUCCESS:
14194 used_current_matrix_p = 1;
14195 goto done;
14196
14197 case CURSOR_MOVEMENT_MUST_SCROLL:
14198 goto try_to_scroll;
14199
14200 default:
14201 abort ();
14202 }
14203 }
14204 /* If current starting point was originally the beginning of a line
14205 but no longer is, find a new starting point. */
14206 else if (!NILP (w->start_at_line_beg)
14207 && !(CHARPOS (startp) <= BEGV
14208 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14209 {
14210 #if GLYPH_DEBUG
14211 debug_method_add (w, "recenter 1");
14212 #endif
14213 goto recenter;
14214 }
14215
14216 /* Try scrolling with try_window_id. Value is > 0 if update has
14217 been done, it is -1 if we know that the same window start will
14218 not work. It is 0 if unsuccessful for some other reason. */
14219 else if ((tem = try_window_id (w)) != 0)
14220 {
14221 #if GLYPH_DEBUG
14222 debug_method_add (w, "try_window_id %d", tem);
14223 #endif
14224
14225 if (fonts_changed_p)
14226 goto need_larger_matrices;
14227 if (tem > 0)
14228 goto done;
14229
14230 /* Otherwise try_window_id has returned -1 which means that we
14231 don't want the alternative below this comment to execute. */
14232 }
14233 else if (CHARPOS (startp) >= BEGV
14234 && CHARPOS (startp) <= ZV
14235 && PT >= CHARPOS (startp)
14236 && (CHARPOS (startp) < ZV
14237 /* Avoid starting at end of buffer. */
14238 || CHARPOS (startp) == BEGV
14239 || (XFASTINT (w->last_modified) >= MODIFF
14240 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14241 {
14242
14243 /* If first window line is a continuation line, and window start
14244 is inside the modified region, but the first change is before
14245 current window start, we must select a new window start.
14246
14247 However, if this is the result of a down-mouse event (e.g. by
14248 extending the mouse-drag-overlay), we don't want to select a
14249 new window start, since that would change the position under
14250 the mouse, resulting in an unwanted mouse-movement rather
14251 than a simple mouse-click. */
14252 if (NILP (w->start_at_line_beg)
14253 && NILP (do_mouse_tracking)
14254 && CHARPOS (startp) > BEGV
14255 && CHARPOS (startp) > BEG + beg_unchanged
14256 && CHARPOS (startp) <= Z - end_unchanged
14257 /* Even if w->start_at_line_beg is nil, a new window may
14258 start at a line_beg, since that's how set_buffer_window
14259 sets it. So, we need to check the return value of
14260 compute_window_start_on_continuation_line. (See also
14261 bug#197). */
14262 && XMARKER (w->start)->buffer == current_buffer
14263 && compute_window_start_on_continuation_line (w))
14264 {
14265 w->force_start = Qt;
14266 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14267 goto force_start;
14268 }
14269
14270 #if GLYPH_DEBUG
14271 debug_method_add (w, "same window start");
14272 #endif
14273
14274 /* Try to redisplay starting at same place as before.
14275 If point has not moved off frame, accept the results. */
14276 if (!current_matrix_up_to_date_p
14277 /* Don't use try_window_reusing_current_matrix in this case
14278 because a window scroll function can have changed the
14279 buffer. */
14280 || !NILP (Vwindow_scroll_functions)
14281 || MINI_WINDOW_P (w)
14282 || !(used_current_matrix_p
14283 = try_window_reusing_current_matrix (w)))
14284 {
14285 IF_DEBUG (debug_method_add (w, "1"));
14286 if (try_window (window, startp, 1) < 0)
14287 /* -1 means we need to scroll.
14288 0 means we need new matrices, but fonts_changed_p
14289 is set in that case, so we will detect it below. */
14290 goto try_to_scroll;
14291 }
14292
14293 if (fonts_changed_p)
14294 goto need_larger_matrices;
14295
14296 if (w->cursor.vpos >= 0)
14297 {
14298 if (!just_this_one_p
14299 || current_buffer->clip_changed
14300 || BEG_UNCHANGED < CHARPOS (startp))
14301 /* Forget any recorded base line for line number display. */
14302 w->base_line_number = Qnil;
14303
14304 if (!cursor_row_fully_visible_p (w, 1, 0))
14305 {
14306 clear_glyph_matrix (w->desired_matrix);
14307 last_line_misfit = 1;
14308 }
14309 /* Drop through and scroll. */
14310 else
14311 goto done;
14312 }
14313 else
14314 clear_glyph_matrix (w->desired_matrix);
14315 }
14316
14317 try_to_scroll:
14318
14319 w->last_modified = make_number (0);
14320 w->last_overlay_modified = make_number (0);
14321
14322 /* Redisplay the mode line. Select the buffer properly for that. */
14323 if (!update_mode_line)
14324 {
14325 update_mode_line = 1;
14326 w->update_mode_line = Qt;
14327 }
14328
14329 /* Try to scroll by specified few lines. */
14330 if ((scroll_conservatively
14331 || scroll_step
14332 || temp_scroll_step
14333 || NUMBERP (current_buffer->scroll_up_aggressively)
14334 || NUMBERP (current_buffer->scroll_down_aggressively))
14335 && !current_buffer->clip_changed
14336 && CHARPOS (startp) >= BEGV
14337 && CHARPOS (startp) <= ZV)
14338 {
14339 /* The function returns -1 if new fonts were loaded, 1 if
14340 successful, 0 if not successful. */
14341 int rc = try_scrolling (window, just_this_one_p,
14342 scroll_conservatively,
14343 scroll_step,
14344 temp_scroll_step, last_line_misfit);
14345 switch (rc)
14346 {
14347 case SCROLLING_SUCCESS:
14348 goto done;
14349
14350 case SCROLLING_NEED_LARGER_MATRICES:
14351 goto need_larger_matrices;
14352
14353 case SCROLLING_FAILED:
14354 break;
14355
14356 default:
14357 abort ();
14358 }
14359 }
14360
14361 /* Finally, just choose place to start which centers point */
14362
14363 recenter:
14364 if (centering_position < 0)
14365 centering_position = window_box_height (w) / 2;
14366
14367 #if GLYPH_DEBUG
14368 debug_method_add (w, "recenter");
14369 #endif
14370
14371 /* w->vscroll = 0; */
14372
14373 /* Forget any previously recorded base line for line number display. */
14374 if (!buffer_unchanged_p)
14375 w->base_line_number = Qnil;
14376
14377 /* Move backward half the height of the window. */
14378 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14379 it.current_y = it.last_visible_y;
14380 move_it_vertically_backward (&it, centering_position);
14381 xassert (IT_CHARPOS (it) >= BEGV);
14382
14383 /* The function move_it_vertically_backward may move over more
14384 than the specified y-distance. If it->w is small, e.g. a
14385 mini-buffer window, we may end up in front of the window's
14386 display area. Start displaying at the start of the line
14387 containing PT in this case. */
14388 if (it.current_y <= 0)
14389 {
14390 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14391 move_it_vertically_backward (&it, 0);
14392 it.current_y = 0;
14393 }
14394
14395 it.current_x = it.hpos = 0;
14396
14397 /* Set startp here explicitly in case that helps avoid an infinite loop
14398 in case the window-scroll-functions functions get errors. */
14399 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14400
14401 /* Run scroll hooks. */
14402 startp = run_window_scroll_functions (window, it.current.pos);
14403
14404 /* Redisplay the window. */
14405 if (!current_matrix_up_to_date_p
14406 || windows_or_buffers_changed
14407 || cursor_type_changed
14408 /* Don't use try_window_reusing_current_matrix in this case
14409 because it can have changed the buffer. */
14410 || !NILP (Vwindow_scroll_functions)
14411 || !just_this_one_p
14412 || MINI_WINDOW_P (w)
14413 || !(used_current_matrix_p
14414 = try_window_reusing_current_matrix (w)))
14415 try_window (window, startp, 0);
14416
14417 /* If new fonts have been loaded (due to fontsets), give up. We
14418 have to start a new redisplay since we need to re-adjust glyph
14419 matrices. */
14420 if (fonts_changed_p)
14421 goto need_larger_matrices;
14422
14423 /* If cursor did not appear assume that the middle of the window is
14424 in the first line of the window. Do it again with the next line.
14425 (Imagine a window of height 100, displaying two lines of height
14426 60. Moving back 50 from it->last_visible_y will end in the first
14427 line.) */
14428 if (w->cursor.vpos < 0)
14429 {
14430 if (!NILP (w->window_end_valid)
14431 && PT >= Z - XFASTINT (w->window_end_pos))
14432 {
14433 clear_glyph_matrix (w->desired_matrix);
14434 move_it_by_lines (&it, 1, 0);
14435 try_window (window, it.current.pos, 0);
14436 }
14437 else if (PT < IT_CHARPOS (it))
14438 {
14439 clear_glyph_matrix (w->desired_matrix);
14440 move_it_by_lines (&it, -1, 0);
14441 try_window (window, it.current.pos, 0);
14442 }
14443 else
14444 {
14445 /* Not much we can do about it. */
14446 }
14447 }
14448
14449 /* Consider the following case: Window starts at BEGV, there is
14450 invisible, intangible text at BEGV, so that display starts at
14451 some point START > BEGV. It can happen that we are called with
14452 PT somewhere between BEGV and START. Try to handle that case. */
14453 if (w->cursor.vpos < 0)
14454 {
14455 struct glyph_row *row = w->current_matrix->rows;
14456 if (row->mode_line_p)
14457 ++row;
14458 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14459 }
14460
14461 if (!cursor_row_fully_visible_p (w, 0, 0))
14462 {
14463 /* If vscroll is enabled, disable it and try again. */
14464 if (w->vscroll)
14465 {
14466 w->vscroll = 0;
14467 clear_glyph_matrix (w->desired_matrix);
14468 goto recenter;
14469 }
14470
14471 /* If centering point failed to make the whole line visible,
14472 put point at the top instead. That has to make the whole line
14473 visible, if it can be done. */
14474 if (centering_position == 0)
14475 goto done;
14476
14477 clear_glyph_matrix (w->desired_matrix);
14478 centering_position = 0;
14479 goto recenter;
14480 }
14481
14482 done:
14483
14484 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14485 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14486 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14487 ? Qt : Qnil);
14488
14489 /* Display the mode line, if we must. */
14490 if ((update_mode_line
14491 /* If window not full width, must redo its mode line
14492 if (a) the window to its side is being redone and
14493 (b) we do a frame-based redisplay. This is a consequence
14494 of how inverted lines are drawn in frame-based redisplay. */
14495 || (!just_this_one_p
14496 && !FRAME_WINDOW_P (f)
14497 && !WINDOW_FULL_WIDTH_P (w))
14498 /* Line number to display. */
14499 || INTEGERP (w->base_line_pos)
14500 /* Column number is displayed and different from the one displayed. */
14501 || (!NILP (w->column_number_displayed)
14502 && (XFASTINT (w->column_number_displayed)
14503 != (int) current_column ()))) /* iftc */
14504 /* This means that the window has a mode line. */
14505 && (WINDOW_WANTS_MODELINE_P (w)
14506 || WINDOW_WANTS_HEADER_LINE_P (w)))
14507 {
14508 display_mode_lines (w);
14509
14510 /* If mode line height has changed, arrange for a thorough
14511 immediate redisplay using the correct mode line height. */
14512 if (WINDOW_WANTS_MODELINE_P (w)
14513 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14514 {
14515 fonts_changed_p = 1;
14516 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14517 = DESIRED_MODE_LINE_HEIGHT (w);
14518 }
14519
14520 /* If header line height has changed, arrange for a thorough
14521 immediate redisplay using the correct header line height. */
14522 if (WINDOW_WANTS_HEADER_LINE_P (w)
14523 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14524 {
14525 fonts_changed_p = 1;
14526 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14527 = DESIRED_HEADER_LINE_HEIGHT (w);
14528 }
14529
14530 if (fonts_changed_p)
14531 goto need_larger_matrices;
14532 }
14533
14534 if (!line_number_displayed
14535 && !BUFFERP (w->base_line_pos))
14536 {
14537 w->base_line_pos = Qnil;
14538 w->base_line_number = Qnil;
14539 }
14540
14541 finish_menu_bars:
14542
14543 /* When we reach a frame's selected window, redo the frame's menu bar. */
14544 if (update_mode_line
14545 && EQ (FRAME_SELECTED_WINDOW (f), window))
14546 {
14547 int redisplay_menu_p = 0;
14548 int redisplay_tool_bar_p = 0;
14549
14550 if (FRAME_WINDOW_P (f))
14551 {
14552 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14553 || defined (HAVE_NS) || defined (USE_GTK)
14554 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14555 #else
14556 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14557 #endif
14558 }
14559 else
14560 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14561
14562 if (redisplay_menu_p)
14563 display_menu_bar (w);
14564
14565 #ifdef HAVE_WINDOW_SYSTEM
14566 if (FRAME_WINDOW_P (f))
14567 {
14568 #if defined (USE_GTK) || defined (HAVE_NS)
14569 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14570 #else
14571 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14572 && (FRAME_TOOL_BAR_LINES (f) > 0
14573 || !NILP (Vauto_resize_tool_bars));
14574 #endif
14575
14576 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14577 {
14578 extern int ignore_mouse_drag_p;
14579 ignore_mouse_drag_p = 1;
14580 }
14581 }
14582 #endif
14583 }
14584
14585 #ifdef HAVE_WINDOW_SYSTEM
14586 if (FRAME_WINDOW_P (f)
14587 && update_window_fringes (w, (just_this_one_p
14588 || (!used_current_matrix_p && !overlay_arrow_seen)
14589 || w->pseudo_window_p)))
14590 {
14591 update_begin (f);
14592 BLOCK_INPUT;
14593 if (draw_window_fringes (w, 1))
14594 x_draw_vertical_border (w);
14595 UNBLOCK_INPUT;
14596 update_end (f);
14597 }
14598 #endif /* HAVE_WINDOW_SYSTEM */
14599
14600 /* We go to this label, with fonts_changed_p nonzero,
14601 if it is necessary to try again using larger glyph matrices.
14602 We have to redeem the scroll bar even in this case,
14603 because the loop in redisplay_internal expects that. */
14604 need_larger_matrices:
14605 ;
14606 finish_scroll_bars:
14607
14608 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14609 {
14610 /* Set the thumb's position and size. */
14611 set_vertical_scroll_bar (w);
14612
14613 /* Note that we actually used the scroll bar attached to this
14614 window, so it shouldn't be deleted at the end of redisplay. */
14615 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14616 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14617 }
14618
14619 /* Restore current_buffer and value of point in it. */
14620 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14621 set_buffer_internal_1 (old);
14622 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14623 shorter. This can be caused by log truncation in *Messages*. */
14624 if (CHARPOS (lpoint) <= ZV)
14625 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14626
14627 unbind_to (count, Qnil);
14628 }
14629
14630
14631 /* Build the complete desired matrix of WINDOW with a window start
14632 buffer position POS.
14633
14634 Value is 1 if successful. It is zero if fonts were loaded during
14635 redisplay which makes re-adjusting glyph matrices necessary, and -1
14636 if point would appear in the scroll margins.
14637 (We check that only if CHECK_MARGINS is nonzero. */
14638
14639 int
14640 try_window (window, pos, check_margins)
14641 Lisp_Object window;
14642 struct text_pos pos;
14643 int check_margins;
14644 {
14645 struct window *w = XWINDOW (window);
14646 struct it it;
14647 struct glyph_row *last_text_row = NULL;
14648 struct frame *f = XFRAME (w->frame);
14649
14650 /* Make POS the new window start. */
14651 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14652
14653 /* Mark cursor position as unknown. No overlay arrow seen. */
14654 w->cursor.vpos = -1;
14655 overlay_arrow_seen = 0;
14656
14657 /* Initialize iterator and info to start at POS. */
14658 start_display (&it, w, pos);
14659
14660 /* Display all lines of W. */
14661 while (it.current_y < it.last_visible_y)
14662 {
14663 if (display_line (&it))
14664 last_text_row = it.glyph_row - 1;
14665 if (fonts_changed_p)
14666 return 0;
14667 }
14668
14669 /* Don't let the cursor end in the scroll margins. */
14670 if (check_margins
14671 && !MINI_WINDOW_P (w))
14672 {
14673 int this_scroll_margin;
14674
14675 if (scroll_margin > 0)
14676 {
14677 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14678 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14679 }
14680 else
14681 this_scroll_margin = 0;
14682
14683 if ((w->cursor.y >= 0 /* not vscrolled */
14684 && w->cursor.y < this_scroll_margin
14685 && CHARPOS (pos) > BEGV
14686 && IT_CHARPOS (it) < ZV)
14687 /* rms: considering make_cursor_line_fully_visible_p here
14688 seems to give wrong results. We don't want to recenter
14689 when the last line is partly visible, we want to allow
14690 that case to be handled in the usual way. */
14691 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14692 {
14693 w->cursor.vpos = -1;
14694 clear_glyph_matrix (w->desired_matrix);
14695 return -1;
14696 }
14697 }
14698
14699 /* If bottom moved off end of frame, change mode line percentage. */
14700 if (XFASTINT (w->window_end_pos) <= 0
14701 && Z != IT_CHARPOS (it))
14702 w->update_mode_line = Qt;
14703
14704 /* Set window_end_pos to the offset of the last character displayed
14705 on the window from the end of current_buffer. Set
14706 window_end_vpos to its row number. */
14707 if (last_text_row)
14708 {
14709 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14710 w->window_end_bytepos
14711 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14712 w->window_end_pos
14713 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14714 w->window_end_vpos
14715 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14716 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14717 ->displays_text_p);
14718 }
14719 else
14720 {
14721 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14722 w->window_end_pos = make_number (Z - ZV);
14723 w->window_end_vpos = make_number (0);
14724 }
14725
14726 /* But that is not valid info until redisplay finishes. */
14727 w->window_end_valid = Qnil;
14728 return 1;
14729 }
14730
14731
14732 \f
14733 /************************************************************************
14734 Window redisplay reusing current matrix when buffer has not changed
14735 ************************************************************************/
14736
14737 /* Try redisplay of window W showing an unchanged buffer with a
14738 different window start than the last time it was displayed by
14739 reusing its current matrix. Value is non-zero if successful.
14740 W->start is the new window start. */
14741
14742 static int
14743 try_window_reusing_current_matrix (w)
14744 struct window *w;
14745 {
14746 struct frame *f = XFRAME (w->frame);
14747 struct glyph_row *row, *bottom_row;
14748 struct it it;
14749 struct run run;
14750 struct text_pos start, new_start;
14751 int nrows_scrolled, i;
14752 struct glyph_row *last_text_row;
14753 struct glyph_row *last_reused_text_row;
14754 struct glyph_row *start_row;
14755 int start_vpos, min_y, max_y;
14756
14757 #if GLYPH_DEBUG
14758 if (inhibit_try_window_reusing)
14759 return 0;
14760 #endif
14761
14762 if (/* This function doesn't handle terminal frames. */
14763 !FRAME_WINDOW_P (f)
14764 /* Don't try to reuse the display if windows have been split
14765 or such. */
14766 || windows_or_buffers_changed
14767 || cursor_type_changed)
14768 return 0;
14769
14770 /* Can't do this if region may have changed. */
14771 if ((!NILP (Vtransient_mark_mode)
14772 && !NILP (current_buffer->mark_active))
14773 || !NILP (w->region_showing)
14774 || !NILP (Vshow_trailing_whitespace))
14775 return 0;
14776
14777 /* If top-line visibility has changed, give up. */
14778 if (WINDOW_WANTS_HEADER_LINE_P (w)
14779 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14780 return 0;
14781
14782 /* Give up if old or new display is scrolled vertically. We could
14783 make this function handle this, but right now it doesn't. */
14784 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14785 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14786 return 0;
14787
14788 /* The variable new_start now holds the new window start. The old
14789 start `start' can be determined from the current matrix. */
14790 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14791 start = start_row->start.pos;
14792 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14793
14794 /* Clear the desired matrix for the display below. */
14795 clear_glyph_matrix (w->desired_matrix);
14796
14797 if (CHARPOS (new_start) <= CHARPOS (start))
14798 {
14799 int first_row_y;
14800
14801 /* Don't use this method if the display starts with an ellipsis
14802 displayed for invisible text. It's not easy to handle that case
14803 below, and it's certainly not worth the effort since this is
14804 not a frequent case. */
14805 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14806 return 0;
14807
14808 IF_DEBUG (debug_method_add (w, "twu1"));
14809
14810 /* Display up to a row that can be reused. The variable
14811 last_text_row is set to the last row displayed that displays
14812 text. Note that it.vpos == 0 if or if not there is a
14813 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14814 start_display (&it, w, new_start);
14815 first_row_y = it.current_y;
14816 w->cursor.vpos = -1;
14817 last_text_row = last_reused_text_row = NULL;
14818
14819 while (it.current_y < it.last_visible_y
14820 && !fonts_changed_p)
14821 {
14822 /* If we have reached into the characters in the START row,
14823 that means the line boundaries have changed. So we
14824 can't start copying with the row START. Maybe it will
14825 work to start copying with the following row. */
14826 while (IT_CHARPOS (it) > CHARPOS (start))
14827 {
14828 /* Advance to the next row as the "start". */
14829 start_row++;
14830 start = start_row->start.pos;
14831 /* If there are no more rows to try, or just one, give up. */
14832 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14833 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14834 || CHARPOS (start) == ZV)
14835 {
14836 clear_glyph_matrix (w->desired_matrix);
14837 return 0;
14838 }
14839
14840 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14841 }
14842 /* If we have reached alignment,
14843 we can copy the rest of the rows. */
14844 if (IT_CHARPOS (it) == CHARPOS (start))
14845 break;
14846
14847 if (display_line (&it))
14848 last_text_row = it.glyph_row - 1;
14849 }
14850
14851 /* A value of current_y < last_visible_y means that we stopped
14852 at the previous window start, which in turn means that we
14853 have at least one reusable row. */
14854 if (it.current_y < it.last_visible_y)
14855 {
14856 /* IT.vpos always starts from 0; it counts text lines. */
14857 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14858
14859 /* Find PT if not already found in the lines displayed. */
14860 if (w->cursor.vpos < 0)
14861 {
14862 int dy = it.current_y - start_row->y;
14863
14864 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14865 row = row_containing_pos (w, PT, row, NULL, dy);
14866 if (row)
14867 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14868 dy, nrows_scrolled);
14869 else
14870 {
14871 clear_glyph_matrix (w->desired_matrix);
14872 return 0;
14873 }
14874 }
14875
14876 /* Scroll the display. Do it before the current matrix is
14877 changed. The problem here is that update has not yet
14878 run, i.e. part of the current matrix is not up to date.
14879 scroll_run_hook will clear the cursor, and use the
14880 current matrix to get the height of the row the cursor is
14881 in. */
14882 run.current_y = start_row->y;
14883 run.desired_y = it.current_y;
14884 run.height = it.last_visible_y - it.current_y;
14885
14886 if (run.height > 0 && run.current_y != run.desired_y)
14887 {
14888 update_begin (f);
14889 FRAME_RIF (f)->update_window_begin_hook (w);
14890 FRAME_RIF (f)->clear_window_mouse_face (w);
14891 FRAME_RIF (f)->scroll_run_hook (w, &run);
14892 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14893 update_end (f);
14894 }
14895
14896 /* Shift current matrix down by nrows_scrolled lines. */
14897 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14898 rotate_matrix (w->current_matrix,
14899 start_vpos,
14900 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14901 nrows_scrolled);
14902
14903 /* Disable lines that must be updated. */
14904 for (i = 0; i < nrows_scrolled; ++i)
14905 (start_row + i)->enabled_p = 0;
14906
14907 /* Re-compute Y positions. */
14908 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14909 max_y = it.last_visible_y;
14910 for (row = start_row + nrows_scrolled;
14911 row < bottom_row;
14912 ++row)
14913 {
14914 row->y = it.current_y;
14915 row->visible_height = row->height;
14916
14917 if (row->y < min_y)
14918 row->visible_height -= min_y - row->y;
14919 if (row->y + row->height > max_y)
14920 row->visible_height -= row->y + row->height - max_y;
14921 row->redraw_fringe_bitmaps_p = 1;
14922
14923 it.current_y += row->height;
14924
14925 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14926 last_reused_text_row = row;
14927 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14928 break;
14929 }
14930
14931 /* Disable lines in the current matrix which are now
14932 below the window. */
14933 for (++row; row < bottom_row; ++row)
14934 row->enabled_p = row->mode_line_p = 0;
14935 }
14936
14937 /* Update window_end_pos etc.; last_reused_text_row is the last
14938 reused row from the current matrix containing text, if any.
14939 The value of last_text_row is the last displayed line
14940 containing text. */
14941 if (last_reused_text_row)
14942 {
14943 w->window_end_bytepos
14944 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14945 w->window_end_pos
14946 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14947 w->window_end_vpos
14948 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14949 w->current_matrix));
14950 }
14951 else if (last_text_row)
14952 {
14953 w->window_end_bytepos
14954 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14955 w->window_end_pos
14956 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14957 w->window_end_vpos
14958 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14959 }
14960 else
14961 {
14962 /* This window must be completely empty. */
14963 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14964 w->window_end_pos = make_number (Z - ZV);
14965 w->window_end_vpos = make_number (0);
14966 }
14967 w->window_end_valid = Qnil;
14968
14969 /* Update hint: don't try scrolling again in update_window. */
14970 w->desired_matrix->no_scrolling_p = 1;
14971
14972 #if GLYPH_DEBUG
14973 debug_method_add (w, "try_window_reusing_current_matrix 1");
14974 #endif
14975 return 1;
14976 }
14977 else if (CHARPOS (new_start) > CHARPOS (start))
14978 {
14979 struct glyph_row *pt_row, *row;
14980 struct glyph_row *first_reusable_row;
14981 struct glyph_row *first_row_to_display;
14982 int dy;
14983 int yb = window_text_bottom_y (w);
14984
14985 /* Find the row starting at new_start, if there is one. Don't
14986 reuse a partially visible line at the end. */
14987 first_reusable_row = start_row;
14988 while (first_reusable_row->enabled_p
14989 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14990 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14991 < CHARPOS (new_start)))
14992 ++first_reusable_row;
14993
14994 /* Give up if there is no row to reuse. */
14995 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14996 || !first_reusable_row->enabled_p
14997 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14998 != CHARPOS (new_start)))
14999 return 0;
15000
15001 /* We can reuse fully visible rows beginning with
15002 first_reusable_row to the end of the window. Set
15003 first_row_to_display to the first row that cannot be reused.
15004 Set pt_row to the row containing point, if there is any. */
15005 pt_row = NULL;
15006 for (first_row_to_display = first_reusable_row;
15007 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15008 ++first_row_to_display)
15009 {
15010 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15011 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15012 pt_row = first_row_to_display;
15013 }
15014
15015 /* Start displaying at the start of first_row_to_display. */
15016 xassert (first_row_to_display->y < yb);
15017 init_to_row_start (&it, w, first_row_to_display);
15018
15019 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15020 - start_vpos);
15021 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15022 - nrows_scrolled);
15023 it.current_y = (first_row_to_display->y - first_reusable_row->y
15024 + WINDOW_HEADER_LINE_HEIGHT (w));
15025
15026 /* Display lines beginning with first_row_to_display in the
15027 desired matrix. Set last_text_row to the last row displayed
15028 that displays text. */
15029 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15030 if (pt_row == NULL)
15031 w->cursor.vpos = -1;
15032 last_text_row = NULL;
15033 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15034 if (display_line (&it))
15035 last_text_row = it.glyph_row - 1;
15036
15037 /* If point is in a reused row, adjust y and vpos of the cursor
15038 position. */
15039 if (pt_row)
15040 {
15041 w->cursor.vpos -= nrows_scrolled;
15042 w->cursor.y -= first_reusable_row->y - start_row->y;
15043 }
15044
15045 /* Give up if point isn't in a row displayed or reused. (This
15046 also handles the case where w->cursor.vpos < nrows_scrolled
15047 after the calls to display_line, which can happen with scroll
15048 margins. See bug#1295.) */
15049 if (w->cursor.vpos < 0)
15050 {
15051 clear_glyph_matrix (w->desired_matrix);
15052 return 0;
15053 }
15054
15055 /* Scroll the display. */
15056 run.current_y = first_reusable_row->y;
15057 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15058 run.height = it.last_visible_y - run.current_y;
15059 dy = run.current_y - run.desired_y;
15060
15061 if (run.height)
15062 {
15063 update_begin (f);
15064 FRAME_RIF (f)->update_window_begin_hook (w);
15065 FRAME_RIF (f)->clear_window_mouse_face (w);
15066 FRAME_RIF (f)->scroll_run_hook (w, &run);
15067 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15068 update_end (f);
15069 }
15070
15071 /* Adjust Y positions of reused rows. */
15072 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15073 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15074 max_y = it.last_visible_y;
15075 for (row = first_reusable_row; row < first_row_to_display; ++row)
15076 {
15077 row->y -= dy;
15078 row->visible_height = row->height;
15079 if (row->y < min_y)
15080 row->visible_height -= min_y - row->y;
15081 if (row->y + row->height > max_y)
15082 row->visible_height -= row->y + row->height - max_y;
15083 row->redraw_fringe_bitmaps_p = 1;
15084 }
15085
15086 /* Scroll the current matrix. */
15087 xassert (nrows_scrolled > 0);
15088 rotate_matrix (w->current_matrix,
15089 start_vpos,
15090 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15091 -nrows_scrolled);
15092
15093 /* Disable rows not reused. */
15094 for (row -= nrows_scrolled; row < bottom_row; ++row)
15095 row->enabled_p = 0;
15096
15097 /* Point may have moved to a different line, so we cannot assume that
15098 the previous cursor position is valid; locate the correct row. */
15099 if (pt_row)
15100 {
15101 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15102 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15103 row++)
15104 {
15105 w->cursor.vpos++;
15106 w->cursor.y = row->y;
15107 }
15108 if (row < bottom_row)
15109 {
15110 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15111 struct glyph *end = glyph + row->used[TEXT_AREA];
15112 struct glyph *orig_glyph = glyph;
15113 struct cursor_pos orig_cursor = w->cursor;
15114
15115 for (; glyph < end
15116 && (!BUFFERP (glyph->object)
15117 || glyph->charpos != PT);
15118 glyph++)
15119 {
15120 w->cursor.hpos++;
15121 w->cursor.x += glyph->pixel_width;
15122 }
15123 /* With bidi reordering, charpos changes non-linearly
15124 with hpos, so the right glyph could be to the
15125 left. */
15126 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15127 && (!BUFFERP (glyph->object) || glyph->charpos != PT))
15128 {
15129 struct glyph *start_glyph = row->glyphs[TEXT_AREA];
15130
15131 glyph = orig_glyph - 1;
15132 orig_cursor.hpos--;
15133 orig_cursor.x -= glyph->pixel_width;
15134 for (; glyph >= start_glyph
15135 && (!BUFFERP (glyph->object)
15136 || glyph->charpos != PT);
15137 glyph--)
15138 {
15139 w->cursor.hpos--;
15140 w->cursor.x -= glyph->pixel_width;
15141 }
15142 if (BUFFERP (glyph->object) && glyph->charpos == PT)
15143 w->cursor = orig_cursor;
15144 }
15145 }
15146 }
15147
15148 /* Adjust window end. A null value of last_text_row means that
15149 the window end is in reused rows which in turn means that
15150 only its vpos can have changed. */
15151 if (last_text_row)
15152 {
15153 w->window_end_bytepos
15154 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15155 w->window_end_pos
15156 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15157 w->window_end_vpos
15158 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15159 }
15160 else
15161 {
15162 w->window_end_vpos
15163 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15164 }
15165
15166 w->window_end_valid = Qnil;
15167 w->desired_matrix->no_scrolling_p = 1;
15168
15169 #if GLYPH_DEBUG
15170 debug_method_add (w, "try_window_reusing_current_matrix 2");
15171 #endif
15172 return 1;
15173 }
15174
15175 return 0;
15176 }
15177
15178
15179 \f
15180 /************************************************************************
15181 Window redisplay reusing current matrix when buffer has changed
15182 ************************************************************************/
15183
15184 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
15185 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
15186 int *, int *));
15187 static struct glyph_row *
15188 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
15189 struct glyph_row *));
15190
15191
15192 /* Return the last row in MATRIX displaying text. If row START is
15193 non-null, start searching with that row. IT gives the dimensions
15194 of the display. Value is null if matrix is empty; otherwise it is
15195 a pointer to the row found. */
15196
15197 static struct glyph_row *
15198 find_last_row_displaying_text (matrix, it, start)
15199 struct glyph_matrix *matrix;
15200 struct it *it;
15201 struct glyph_row *start;
15202 {
15203 struct glyph_row *row, *row_found;
15204
15205 /* Set row_found to the last row in IT->w's current matrix
15206 displaying text. The loop looks funny but think of partially
15207 visible lines. */
15208 row_found = NULL;
15209 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15210 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15211 {
15212 xassert (row->enabled_p);
15213 row_found = row;
15214 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15215 break;
15216 ++row;
15217 }
15218
15219 return row_found;
15220 }
15221
15222
15223 /* Return the last row in the current matrix of W that is not affected
15224 by changes at the start of current_buffer that occurred since W's
15225 current matrix was built. Value is null if no such row exists.
15226
15227 BEG_UNCHANGED us the number of characters unchanged at the start of
15228 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15229 first changed character in current_buffer. Characters at positions <
15230 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15231 when the current matrix was built. */
15232
15233 static struct glyph_row *
15234 find_last_unchanged_at_beg_row (w)
15235 struct window *w;
15236 {
15237 int first_changed_pos = BEG + BEG_UNCHANGED;
15238 struct glyph_row *row;
15239 struct glyph_row *row_found = NULL;
15240 int yb = window_text_bottom_y (w);
15241
15242 /* Find the last row displaying unchanged text. */
15243 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15244 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15245 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15246 ++row)
15247 {
15248 if (/* If row ends before first_changed_pos, it is unchanged,
15249 except in some case. */
15250 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15251 /* When row ends in ZV and we write at ZV it is not
15252 unchanged. */
15253 && !row->ends_at_zv_p
15254 /* When first_changed_pos is the end of a continued line,
15255 row is not unchanged because it may be no longer
15256 continued. */
15257 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15258 && (row->continued_p
15259 || row->exact_window_width_line_p)))
15260 row_found = row;
15261
15262 /* Stop if last visible row. */
15263 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15264 break;
15265 }
15266
15267 return row_found;
15268 }
15269
15270
15271 /* Find the first glyph row in the current matrix of W that is not
15272 affected by changes at the end of current_buffer since the
15273 time W's current matrix was built.
15274
15275 Return in *DELTA the number of chars by which buffer positions in
15276 unchanged text at the end of current_buffer must be adjusted.
15277
15278 Return in *DELTA_BYTES the corresponding number of bytes.
15279
15280 Value is null if no such row exists, i.e. all rows are affected by
15281 changes. */
15282
15283 static struct glyph_row *
15284 find_first_unchanged_at_end_row (w, delta, delta_bytes)
15285 struct window *w;
15286 int *delta, *delta_bytes;
15287 {
15288 struct glyph_row *row;
15289 struct glyph_row *row_found = NULL;
15290
15291 *delta = *delta_bytes = 0;
15292
15293 /* Display must not have been paused, otherwise the current matrix
15294 is not up to date. */
15295 eassert (!NILP (w->window_end_valid));
15296
15297 /* A value of window_end_pos >= END_UNCHANGED means that the window
15298 end is in the range of changed text. If so, there is no
15299 unchanged row at the end of W's current matrix. */
15300 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15301 return NULL;
15302
15303 /* Set row to the last row in W's current matrix displaying text. */
15304 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15305
15306 /* If matrix is entirely empty, no unchanged row exists. */
15307 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15308 {
15309 /* The value of row is the last glyph row in the matrix having a
15310 meaningful buffer position in it. The end position of row
15311 corresponds to window_end_pos. This allows us to translate
15312 buffer positions in the current matrix to current buffer
15313 positions for characters not in changed text. */
15314 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15315 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15316 int last_unchanged_pos, last_unchanged_pos_old;
15317 struct glyph_row *first_text_row
15318 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15319
15320 *delta = Z - Z_old;
15321 *delta_bytes = Z_BYTE - Z_BYTE_old;
15322
15323 /* Set last_unchanged_pos to the buffer position of the last
15324 character in the buffer that has not been changed. Z is the
15325 index + 1 of the last character in current_buffer, i.e. by
15326 subtracting END_UNCHANGED we get the index of the last
15327 unchanged character, and we have to add BEG to get its buffer
15328 position. */
15329 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15330 last_unchanged_pos_old = last_unchanged_pos - *delta;
15331
15332 /* Search backward from ROW for a row displaying a line that
15333 starts at a minimum position >= last_unchanged_pos_old. */
15334 for (; row > first_text_row; --row)
15335 {
15336 /* This used to abort, but it can happen.
15337 It is ok to just stop the search instead here. KFS. */
15338 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15339 break;
15340
15341 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15342 row_found = row;
15343 }
15344 }
15345
15346 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15347
15348 return row_found;
15349 }
15350
15351
15352 /* Make sure that glyph rows in the current matrix of window W
15353 reference the same glyph memory as corresponding rows in the
15354 frame's frame matrix. This function is called after scrolling W's
15355 current matrix on a terminal frame in try_window_id and
15356 try_window_reusing_current_matrix. */
15357
15358 static void
15359 sync_frame_with_window_matrix_rows (w)
15360 struct window *w;
15361 {
15362 struct frame *f = XFRAME (w->frame);
15363 struct glyph_row *window_row, *window_row_end, *frame_row;
15364
15365 /* Preconditions: W must be a leaf window and full-width. Its frame
15366 must have a frame matrix. */
15367 xassert (NILP (w->hchild) && NILP (w->vchild));
15368 xassert (WINDOW_FULL_WIDTH_P (w));
15369 xassert (!FRAME_WINDOW_P (f));
15370
15371 /* If W is a full-width window, glyph pointers in W's current matrix
15372 have, by definition, to be the same as glyph pointers in the
15373 corresponding frame matrix. Note that frame matrices have no
15374 marginal areas (see build_frame_matrix). */
15375 window_row = w->current_matrix->rows;
15376 window_row_end = window_row + w->current_matrix->nrows;
15377 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15378 while (window_row < window_row_end)
15379 {
15380 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15381 struct glyph *end = window_row->glyphs[LAST_AREA];
15382
15383 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15384 frame_row->glyphs[TEXT_AREA] = start;
15385 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15386 frame_row->glyphs[LAST_AREA] = end;
15387
15388 /* Disable frame rows whose corresponding window rows have
15389 been disabled in try_window_id. */
15390 if (!window_row->enabled_p)
15391 frame_row->enabled_p = 0;
15392
15393 ++window_row, ++frame_row;
15394 }
15395 }
15396
15397
15398 /* Find the glyph row in window W containing CHARPOS. Consider all
15399 rows between START and END (not inclusive). END null means search
15400 all rows to the end of the display area of W. Value is the row
15401 containing CHARPOS or null. */
15402
15403 struct glyph_row *
15404 row_containing_pos (w, charpos, start, end, dy)
15405 struct window *w;
15406 int charpos;
15407 struct glyph_row *start, *end;
15408 int dy;
15409 {
15410 struct glyph_row *row = start;
15411 struct glyph_row *best_row = NULL;
15412 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15413 int last_y;
15414
15415 /* If we happen to start on a header-line, skip that. */
15416 if (row->mode_line_p)
15417 ++row;
15418
15419 if ((end && row >= end) || !row->enabled_p)
15420 return NULL;
15421
15422 last_y = window_text_bottom_y (w) - dy;
15423
15424 while (1)
15425 {
15426 /* Give up if we have gone too far. */
15427 if (end && row >= end)
15428 return NULL;
15429 /* This formerly returned if they were equal.
15430 I think that both quantities are of a "last plus one" type;
15431 if so, when they are equal, the row is within the screen. -- rms. */
15432 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15433 return NULL;
15434
15435 /* If it is in this row, return this row. */
15436 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15437 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15438 /* The end position of a row equals the start
15439 position of the next row. If CHARPOS is there, we
15440 would rather display it in the next line, except
15441 when this line ends in ZV. */
15442 && !row->ends_at_zv_p
15443 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15444 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15445 {
15446 struct glyph *g;
15447
15448 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15449 return row;
15450 /* In bidi-reordered rows, there could be several rows
15451 occluding point. We need to find the one which fits
15452 CHARPOS the best. */
15453 for (g = row->glyphs[TEXT_AREA];
15454 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15455 g++)
15456 {
15457 if (!STRINGP (g->object))
15458 {
15459 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15460 {
15461 mindif = eabs (g->charpos - charpos);
15462 best_row = row;
15463 }
15464 }
15465 }
15466 }
15467 else if (best_row)
15468 return best_row;
15469 ++row;
15470 }
15471 }
15472
15473
15474 /* Try to redisplay window W by reusing its existing display. W's
15475 current matrix must be up to date when this function is called,
15476 i.e. window_end_valid must not be nil.
15477
15478 Value is
15479
15480 1 if display has been updated
15481 0 if otherwise unsuccessful
15482 -1 if redisplay with same window start is known not to succeed
15483
15484 The following steps are performed:
15485
15486 1. Find the last row in the current matrix of W that is not
15487 affected by changes at the start of current_buffer. If no such row
15488 is found, give up.
15489
15490 2. Find the first row in W's current matrix that is not affected by
15491 changes at the end of current_buffer. Maybe there is no such row.
15492
15493 3. Display lines beginning with the row + 1 found in step 1 to the
15494 row found in step 2 or, if step 2 didn't find a row, to the end of
15495 the window.
15496
15497 4. If cursor is not known to appear on the window, give up.
15498
15499 5. If display stopped at the row found in step 2, scroll the
15500 display and current matrix as needed.
15501
15502 6. Maybe display some lines at the end of W, if we must. This can
15503 happen under various circumstances, like a partially visible line
15504 becoming fully visible, or because newly displayed lines are displayed
15505 in smaller font sizes.
15506
15507 7. Update W's window end information. */
15508
15509 static int
15510 try_window_id (w)
15511 struct window *w;
15512 {
15513 struct frame *f = XFRAME (w->frame);
15514 struct glyph_matrix *current_matrix = w->current_matrix;
15515 struct glyph_matrix *desired_matrix = w->desired_matrix;
15516 struct glyph_row *last_unchanged_at_beg_row;
15517 struct glyph_row *first_unchanged_at_end_row;
15518 struct glyph_row *row;
15519 struct glyph_row *bottom_row;
15520 int bottom_vpos;
15521 struct it it;
15522 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
15523 struct text_pos start_pos;
15524 struct run run;
15525 int first_unchanged_at_end_vpos = 0;
15526 struct glyph_row *last_text_row, *last_text_row_at_end;
15527 struct text_pos start;
15528 int first_changed_charpos, last_changed_charpos;
15529
15530 #if GLYPH_DEBUG
15531 if (inhibit_try_window_id)
15532 return 0;
15533 #endif
15534
15535 /* This is handy for debugging. */
15536 #if 0
15537 #define GIVE_UP(X) \
15538 do { \
15539 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15540 return 0; \
15541 } while (0)
15542 #else
15543 #define GIVE_UP(X) return 0
15544 #endif
15545
15546 SET_TEXT_POS_FROM_MARKER (start, w->start);
15547
15548 /* Don't use this for mini-windows because these can show
15549 messages and mini-buffers, and we don't handle that here. */
15550 if (MINI_WINDOW_P (w))
15551 GIVE_UP (1);
15552
15553 /* This flag is used to prevent redisplay optimizations. */
15554 if (windows_or_buffers_changed || cursor_type_changed)
15555 GIVE_UP (2);
15556
15557 /* Verify that narrowing has not changed.
15558 Also verify that we were not told to prevent redisplay optimizations.
15559 It would be nice to further
15560 reduce the number of cases where this prevents try_window_id. */
15561 if (current_buffer->clip_changed
15562 || current_buffer->prevent_redisplay_optimizations_p)
15563 GIVE_UP (3);
15564
15565 /* Window must either use window-based redisplay or be full width. */
15566 if (!FRAME_WINDOW_P (f)
15567 && (!FRAME_LINE_INS_DEL_OK (f)
15568 || !WINDOW_FULL_WIDTH_P (w)))
15569 GIVE_UP (4);
15570
15571 /* Give up if point is known NOT to appear in W. */
15572 if (PT < CHARPOS (start))
15573 GIVE_UP (5);
15574
15575 /* Another way to prevent redisplay optimizations. */
15576 if (XFASTINT (w->last_modified) == 0)
15577 GIVE_UP (6);
15578
15579 /* Verify that window is not hscrolled. */
15580 if (XFASTINT (w->hscroll) != 0)
15581 GIVE_UP (7);
15582
15583 /* Verify that display wasn't paused. */
15584 if (NILP (w->window_end_valid))
15585 GIVE_UP (8);
15586
15587 /* Can't use this if highlighting a region because a cursor movement
15588 will do more than just set the cursor. */
15589 if (!NILP (Vtransient_mark_mode)
15590 && !NILP (current_buffer->mark_active))
15591 GIVE_UP (9);
15592
15593 /* Likewise if highlighting trailing whitespace. */
15594 if (!NILP (Vshow_trailing_whitespace))
15595 GIVE_UP (11);
15596
15597 /* Likewise if showing a region. */
15598 if (!NILP (w->region_showing))
15599 GIVE_UP (10);
15600
15601 /* Can't use this if overlay arrow position and/or string have
15602 changed. */
15603 if (overlay_arrows_changed_p ())
15604 GIVE_UP (12);
15605
15606 /* When word-wrap is on, adding a space to the first word of a
15607 wrapped line can change the wrap position, altering the line
15608 above it. It might be worthwhile to handle this more
15609 intelligently, but for now just redisplay from scratch. */
15610 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15611 GIVE_UP (21);
15612
15613 /* Under bidi reordering, adding or deleting a character in the
15614 beginning of a paragraph, before the first strong directional
15615 character, can change the base direction of the paragraph (unless
15616 the buffer specifies a fixed paragraph direction), which will
15617 require to redisplay the whole paragraph. It might be worthwhile
15618 to find the paragraph limits and widen the range of redisplayed
15619 lines to that, but for now just give up this optimization and
15620 redisplay from scratch. */
15621 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15622 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15623 GIVE_UP (22);
15624
15625 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15626 only if buffer has really changed. The reason is that the gap is
15627 initially at Z for freshly visited files. The code below would
15628 set end_unchanged to 0 in that case. */
15629 if (MODIFF > SAVE_MODIFF
15630 /* This seems to happen sometimes after saving a buffer. */
15631 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15632 {
15633 if (GPT - BEG < BEG_UNCHANGED)
15634 BEG_UNCHANGED = GPT - BEG;
15635 if (Z - GPT < END_UNCHANGED)
15636 END_UNCHANGED = Z - GPT;
15637 }
15638
15639 /* The position of the first and last character that has been changed. */
15640 first_changed_charpos = BEG + BEG_UNCHANGED;
15641 last_changed_charpos = Z - END_UNCHANGED;
15642
15643 /* If window starts after a line end, and the last change is in
15644 front of that newline, then changes don't affect the display.
15645 This case happens with stealth-fontification. Note that although
15646 the display is unchanged, glyph positions in the matrix have to
15647 be adjusted, of course. */
15648 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15649 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15650 && ((last_changed_charpos < CHARPOS (start)
15651 && CHARPOS (start) == BEGV)
15652 || (last_changed_charpos < CHARPOS (start) - 1
15653 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15654 {
15655 int Z_old, delta, Z_BYTE_old, delta_bytes;
15656 struct glyph_row *r0;
15657
15658 /* Compute how many chars/bytes have been added to or removed
15659 from the buffer. */
15660 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15661 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15662 delta = Z - Z_old;
15663 delta_bytes = Z_BYTE - Z_BYTE_old;
15664
15665 /* Give up if PT is not in the window. Note that it already has
15666 been checked at the start of try_window_id that PT is not in
15667 front of the window start. */
15668 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15669 GIVE_UP (13);
15670
15671 /* If window start is unchanged, we can reuse the whole matrix
15672 as is, after adjusting glyph positions. No need to compute
15673 the window end again, since its offset from Z hasn't changed. */
15674 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15675 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15676 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15677 /* PT must not be in a partially visible line. */
15678 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15679 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15680 {
15681 /* Adjust positions in the glyph matrix. */
15682 if (delta || delta_bytes)
15683 {
15684 struct glyph_row *r1
15685 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15686 increment_matrix_positions (w->current_matrix,
15687 MATRIX_ROW_VPOS (r0, current_matrix),
15688 MATRIX_ROW_VPOS (r1, current_matrix),
15689 delta, delta_bytes);
15690 }
15691
15692 /* Set the cursor. */
15693 row = row_containing_pos (w, PT, r0, NULL, 0);
15694 if (row)
15695 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15696 else
15697 abort ();
15698 return 1;
15699 }
15700 }
15701
15702 /* Handle the case that changes are all below what is displayed in
15703 the window, and that PT is in the window. This shortcut cannot
15704 be taken if ZV is visible in the window, and text has been added
15705 there that is visible in the window. */
15706 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15707 /* ZV is not visible in the window, or there are no
15708 changes at ZV, actually. */
15709 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15710 || first_changed_charpos == last_changed_charpos))
15711 {
15712 struct glyph_row *r0;
15713
15714 /* Give up if PT is not in the window. Note that it already has
15715 been checked at the start of try_window_id that PT is not in
15716 front of the window start. */
15717 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15718 GIVE_UP (14);
15719
15720 /* If window start is unchanged, we can reuse the whole matrix
15721 as is, without changing glyph positions since no text has
15722 been added/removed in front of the window end. */
15723 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15724 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15725 /* PT must not be in a partially visible line. */
15726 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15727 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15728 {
15729 /* We have to compute the window end anew since text
15730 can have been added/removed after it. */
15731 w->window_end_pos
15732 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15733 w->window_end_bytepos
15734 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15735
15736 /* Set the cursor. */
15737 row = row_containing_pos (w, PT, r0, NULL, 0);
15738 if (row)
15739 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15740 else
15741 abort ();
15742 return 2;
15743 }
15744 }
15745
15746 /* Give up if window start is in the changed area.
15747
15748 The condition used to read
15749
15750 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15751
15752 but why that was tested escapes me at the moment. */
15753 if (CHARPOS (start) >= first_changed_charpos
15754 && CHARPOS (start) <= last_changed_charpos)
15755 GIVE_UP (15);
15756
15757 /* Check that window start agrees with the start of the first glyph
15758 row in its current matrix. Check this after we know the window
15759 start is not in changed text, otherwise positions would not be
15760 comparable. */
15761 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15762 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15763 GIVE_UP (16);
15764
15765 /* Give up if the window ends in strings. Overlay strings
15766 at the end are difficult to handle, so don't try. */
15767 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15768 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15769 GIVE_UP (20);
15770
15771 /* Compute the position at which we have to start displaying new
15772 lines. Some of the lines at the top of the window might be
15773 reusable because they are not displaying changed text. Find the
15774 last row in W's current matrix not affected by changes at the
15775 start of current_buffer. Value is null if changes start in the
15776 first line of window. */
15777 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15778 if (last_unchanged_at_beg_row)
15779 {
15780 /* Avoid starting to display in the moddle of a character, a TAB
15781 for instance. This is easier than to set up the iterator
15782 exactly, and it's not a frequent case, so the additional
15783 effort wouldn't really pay off. */
15784 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15785 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15786 && last_unchanged_at_beg_row > w->current_matrix->rows)
15787 --last_unchanged_at_beg_row;
15788
15789 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15790 GIVE_UP (17);
15791
15792 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15793 GIVE_UP (18);
15794 start_pos = it.current.pos;
15795
15796 /* Start displaying new lines in the desired matrix at the same
15797 vpos we would use in the current matrix, i.e. below
15798 last_unchanged_at_beg_row. */
15799 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15800 current_matrix);
15801 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15802 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15803
15804 xassert (it.hpos == 0 && it.current_x == 0);
15805 }
15806 else
15807 {
15808 /* There are no reusable lines at the start of the window.
15809 Start displaying in the first text line. */
15810 start_display (&it, w, start);
15811 it.vpos = it.first_vpos;
15812 start_pos = it.current.pos;
15813 }
15814
15815 /* Find the first row that is not affected by changes at the end of
15816 the buffer. Value will be null if there is no unchanged row, in
15817 which case we must redisplay to the end of the window. delta
15818 will be set to the value by which buffer positions beginning with
15819 first_unchanged_at_end_row have to be adjusted due to text
15820 changes. */
15821 first_unchanged_at_end_row
15822 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15823 IF_DEBUG (debug_delta = delta);
15824 IF_DEBUG (debug_delta_bytes = delta_bytes);
15825
15826 /* Set stop_pos to the buffer position up to which we will have to
15827 display new lines. If first_unchanged_at_end_row != NULL, this
15828 is the buffer position of the start of the line displayed in that
15829 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15830 that we don't stop at a buffer position. */
15831 stop_pos = 0;
15832 if (first_unchanged_at_end_row)
15833 {
15834 xassert (last_unchanged_at_beg_row == NULL
15835 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15836
15837 /* If this is a continuation line, move forward to the next one
15838 that isn't. Changes in lines above affect this line.
15839 Caution: this may move first_unchanged_at_end_row to a row
15840 not displaying text. */
15841 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15842 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15843 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15844 < it.last_visible_y))
15845 ++first_unchanged_at_end_row;
15846
15847 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15848 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15849 >= it.last_visible_y))
15850 first_unchanged_at_end_row = NULL;
15851 else
15852 {
15853 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15854 + delta);
15855 first_unchanged_at_end_vpos
15856 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15857 xassert (stop_pos >= Z - END_UNCHANGED);
15858 }
15859 }
15860 else if (last_unchanged_at_beg_row == NULL)
15861 GIVE_UP (19);
15862
15863
15864 #if GLYPH_DEBUG
15865
15866 /* Either there is no unchanged row at the end, or the one we have
15867 now displays text. This is a necessary condition for the window
15868 end pos calculation at the end of this function. */
15869 xassert (first_unchanged_at_end_row == NULL
15870 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15871
15872 debug_last_unchanged_at_beg_vpos
15873 = (last_unchanged_at_beg_row
15874 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15875 : -1);
15876 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15877
15878 #endif /* GLYPH_DEBUG != 0 */
15879
15880
15881 /* Display new lines. Set last_text_row to the last new line
15882 displayed which has text on it, i.e. might end up as being the
15883 line where the window_end_vpos is. */
15884 w->cursor.vpos = -1;
15885 last_text_row = NULL;
15886 overlay_arrow_seen = 0;
15887 while (it.current_y < it.last_visible_y
15888 && !fonts_changed_p
15889 && (first_unchanged_at_end_row == NULL
15890 || IT_CHARPOS (it) < stop_pos))
15891 {
15892 if (display_line (&it))
15893 last_text_row = it.glyph_row - 1;
15894 }
15895
15896 if (fonts_changed_p)
15897 return -1;
15898
15899
15900 /* Compute differences in buffer positions, y-positions etc. for
15901 lines reused at the bottom of the window. Compute what we can
15902 scroll. */
15903 if (first_unchanged_at_end_row
15904 /* No lines reused because we displayed everything up to the
15905 bottom of the window. */
15906 && it.current_y < it.last_visible_y)
15907 {
15908 dvpos = (it.vpos
15909 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15910 current_matrix));
15911 dy = it.current_y - first_unchanged_at_end_row->y;
15912 run.current_y = first_unchanged_at_end_row->y;
15913 run.desired_y = run.current_y + dy;
15914 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15915 }
15916 else
15917 {
15918 delta = delta_bytes = dvpos = dy
15919 = run.current_y = run.desired_y = run.height = 0;
15920 first_unchanged_at_end_row = NULL;
15921 }
15922 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15923
15924
15925 /* Find the cursor if not already found. We have to decide whether
15926 PT will appear on this window (it sometimes doesn't, but this is
15927 not a very frequent case.) This decision has to be made before
15928 the current matrix is altered. A value of cursor.vpos < 0 means
15929 that PT is either in one of the lines beginning at
15930 first_unchanged_at_end_row or below the window. Don't care for
15931 lines that might be displayed later at the window end; as
15932 mentioned, this is not a frequent case. */
15933 if (w->cursor.vpos < 0)
15934 {
15935 /* Cursor in unchanged rows at the top? */
15936 if (PT < CHARPOS (start_pos)
15937 && last_unchanged_at_beg_row)
15938 {
15939 row = row_containing_pos (w, PT,
15940 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15941 last_unchanged_at_beg_row + 1, 0);
15942 if (row)
15943 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15944 }
15945
15946 /* Start from first_unchanged_at_end_row looking for PT. */
15947 else if (first_unchanged_at_end_row)
15948 {
15949 row = row_containing_pos (w, PT - delta,
15950 first_unchanged_at_end_row, NULL, 0);
15951 if (row)
15952 set_cursor_from_row (w, row, w->current_matrix, delta,
15953 delta_bytes, dy, dvpos);
15954 }
15955
15956 /* Give up if cursor was not found. */
15957 if (w->cursor.vpos < 0)
15958 {
15959 clear_glyph_matrix (w->desired_matrix);
15960 return -1;
15961 }
15962 }
15963
15964 /* Don't let the cursor end in the scroll margins. */
15965 {
15966 int this_scroll_margin, cursor_height;
15967
15968 this_scroll_margin = max (0, scroll_margin);
15969 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15970 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15971 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15972
15973 if ((w->cursor.y < this_scroll_margin
15974 && CHARPOS (start) > BEGV)
15975 /* Old redisplay didn't take scroll margin into account at the bottom,
15976 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15977 || (w->cursor.y + (make_cursor_line_fully_visible_p
15978 ? cursor_height + this_scroll_margin
15979 : 1)) > it.last_visible_y)
15980 {
15981 w->cursor.vpos = -1;
15982 clear_glyph_matrix (w->desired_matrix);
15983 return -1;
15984 }
15985 }
15986
15987 /* Scroll the display. Do it before changing the current matrix so
15988 that xterm.c doesn't get confused about where the cursor glyph is
15989 found. */
15990 if (dy && run.height)
15991 {
15992 update_begin (f);
15993
15994 if (FRAME_WINDOW_P (f))
15995 {
15996 FRAME_RIF (f)->update_window_begin_hook (w);
15997 FRAME_RIF (f)->clear_window_mouse_face (w);
15998 FRAME_RIF (f)->scroll_run_hook (w, &run);
15999 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16000 }
16001 else
16002 {
16003 /* Terminal frame. In this case, dvpos gives the number of
16004 lines to scroll by; dvpos < 0 means scroll up. */
16005 int first_unchanged_at_end_vpos
16006 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16007 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
16008 int end = (WINDOW_TOP_EDGE_LINE (w)
16009 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16010 + window_internal_height (w));
16011
16012 /* Perform the operation on the screen. */
16013 if (dvpos > 0)
16014 {
16015 /* Scroll last_unchanged_at_beg_row to the end of the
16016 window down dvpos lines. */
16017 set_terminal_window (f, end);
16018
16019 /* On dumb terminals delete dvpos lines at the end
16020 before inserting dvpos empty lines. */
16021 if (!FRAME_SCROLL_REGION_OK (f))
16022 ins_del_lines (f, end - dvpos, -dvpos);
16023
16024 /* Insert dvpos empty lines in front of
16025 last_unchanged_at_beg_row. */
16026 ins_del_lines (f, from, dvpos);
16027 }
16028 else if (dvpos < 0)
16029 {
16030 /* Scroll up last_unchanged_at_beg_vpos to the end of
16031 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16032 set_terminal_window (f, end);
16033
16034 /* Delete dvpos lines in front of
16035 last_unchanged_at_beg_vpos. ins_del_lines will set
16036 the cursor to the given vpos and emit |dvpos| delete
16037 line sequences. */
16038 ins_del_lines (f, from + dvpos, dvpos);
16039
16040 /* On a dumb terminal insert dvpos empty lines at the
16041 end. */
16042 if (!FRAME_SCROLL_REGION_OK (f))
16043 ins_del_lines (f, end + dvpos, -dvpos);
16044 }
16045
16046 set_terminal_window (f, 0);
16047 }
16048
16049 update_end (f);
16050 }
16051
16052 /* Shift reused rows of the current matrix to the right position.
16053 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16054 text. */
16055 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16056 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16057 if (dvpos < 0)
16058 {
16059 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16060 bottom_vpos, dvpos);
16061 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16062 bottom_vpos, 0);
16063 }
16064 else if (dvpos > 0)
16065 {
16066 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16067 bottom_vpos, dvpos);
16068 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16069 first_unchanged_at_end_vpos + dvpos, 0);
16070 }
16071
16072 /* For frame-based redisplay, make sure that current frame and window
16073 matrix are in sync with respect to glyph memory. */
16074 if (!FRAME_WINDOW_P (f))
16075 sync_frame_with_window_matrix_rows (w);
16076
16077 /* Adjust buffer positions in reused rows. */
16078 if (delta || delta_bytes)
16079 increment_matrix_positions (current_matrix,
16080 first_unchanged_at_end_vpos + dvpos,
16081 bottom_vpos, delta, delta_bytes);
16082
16083 /* Adjust Y positions. */
16084 if (dy)
16085 shift_glyph_matrix (w, current_matrix,
16086 first_unchanged_at_end_vpos + dvpos,
16087 bottom_vpos, dy);
16088
16089 if (first_unchanged_at_end_row)
16090 {
16091 first_unchanged_at_end_row += dvpos;
16092 if (first_unchanged_at_end_row->y >= it.last_visible_y
16093 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16094 first_unchanged_at_end_row = NULL;
16095 }
16096
16097 /* If scrolling up, there may be some lines to display at the end of
16098 the window. */
16099 last_text_row_at_end = NULL;
16100 if (dy < 0)
16101 {
16102 /* Scrolling up can leave for example a partially visible line
16103 at the end of the window to be redisplayed. */
16104 /* Set last_row to the glyph row in the current matrix where the
16105 window end line is found. It has been moved up or down in
16106 the matrix by dvpos. */
16107 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16108 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16109
16110 /* If last_row is the window end line, it should display text. */
16111 xassert (last_row->displays_text_p);
16112
16113 /* If window end line was partially visible before, begin
16114 displaying at that line. Otherwise begin displaying with the
16115 line following it. */
16116 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16117 {
16118 init_to_row_start (&it, w, last_row);
16119 it.vpos = last_vpos;
16120 it.current_y = last_row->y;
16121 }
16122 else
16123 {
16124 init_to_row_end (&it, w, last_row);
16125 it.vpos = 1 + last_vpos;
16126 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16127 ++last_row;
16128 }
16129
16130 /* We may start in a continuation line. If so, we have to
16131 get the right continuation_lines_width and current_x. */
16132 it.continuation_lines_width = last_row->continuation_lines_width;
16133 it.hpos = it.current_x = 0;
16134
16135 /* Display the rest of the lines at the window end. */
16136 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16137 while (it.current_y < it.last_visible_y
16138 && !fonts_changed_p)
16139 {
16140 /* Is it always sure that the display agrees with lines in
16141 the current matrix? I don't think so, so we mark rows
16142 displayed invalid in the current matrix by setting their
16143 enabled_p flag to zero. */
16144 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16145 if (display_line (&it))
16146 last_text_row_at_end = it.glyph_row - 1;
16147 }
16148 }
16149
16150 /* Update window_end_pos and window_end_vpos. */
16151 if (first_unchanged_at_end_row
16152 && !last_text_row_at_end)
16153 {
16154 /* Window end line if one of the preserved rows from the current
16155 matrix. Set row to the last row displaying text in current
16156 matrix starting at first_unchanged_at_end_row, after
16157 scrolling. */
16158 xassert (first_unchanged_at_end_row->displays_text_p);
16159 row = find_last_row_displaying_text (w->current_matrix, &it,
16160 first_unchanged_at_end_row);
16161 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16162
16163 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16164 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16165 w->window_end_vpos
16166 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16167 xassert (w->window_end_bytepos >= 0);
16168 IF_DEBUG (debug_method_add (w, "A"));
16169 }
16170 else if (last_text_row_at_end)
16171 {
16172 w->window_end_pos
16173 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16174 w->window_end_bytepos
16175 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16176 w->window_end_vpos
16177 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16178 xassert (w->window_end_bytepos >= 0);
16179 IF_DEBUG (debug_method_add (w, "B"));
16180 }
16181 else if (last_text_row)
16182 {
16183 /* We have displayed either to the end of the window or at the
16184 end of the window, i.e. the last row with text is to be found
16185 in the desired matrix. */
16186 w->window_end_pos
16187 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16188 w->window_end_bytepos
16189 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16190 w->window_end_vpos
16191 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16192 xassert (w->window_end_bytepos >= 0);
16193 }
16194 else if (first_unchanged_at_end_row == NULL
16195 && last_text_row == NULL
16196 && last_text_row_at_end == NULL)
16197 {
16198 /* Displayed to end of window, but no line containing text was
16199 displayed. Lines were deleted at the end of the window. */
16200 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16201 int vpos = XFASTINT (w->window_end_vpos);
16202 struct glyph_row *current_row = current_matrix->rows + vpos;
16203 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16204
16205 for (row = NULL;
16206 row == NULL && vpos >= first_vpos;
16207 --vpos, --current_row, --desired_row)
16208 {
16209 if (desired_row->enabled_p)
16210 {
16211 if (desired_row->displays_text_p)
16212 row = desired_row;
16213 }
16214 else if (current_row->displays_text_p)
16215 row = current_row;
16216 }
16217
16218 xassert (row != NULL);
16219 w->window_end_vpos = make_number (vpos + 1);
16220 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16221 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16222 xassert (w->window_end_bytepos >= 0);
16223 IF_DEBUG (debug_method_add (w, "C"));
16224 }
16225 else
16226 abort ();
16227
16228 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16229 debug_end_vpos = XFASTINT (w->window_end_vpos));
16230
16231 /* Record that display has not been completed. */
16232 w->window_end_valid = Qnil;
16233 w->desired_matrix->no_scrolling_p = 1;
16234 return 3;
16235
16236 #undef GIVE_UP
16237 }
16238
16239
16240 \f
16241 /***********************************************************************
16242 More debugging support
16243 ***********************************************************************/
16244
16245 #if GLYPH_DEBUG
16246
16247 void dump_glyph_row P_ ((struct glyph_row *, int, int));
16248 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
16249 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
16250
16251
16252 /* Dump the contents of glyph matrix MATRIX on stderr.
16253
16254 GLYPHS 0 means don't show glyph contents.
16255 GLYPHS 1 means show glyphs in short form
16256 GLYPHS > 1 means show glyphs in long form. */
16257
16258 void
16259 dump_glyph_matrix (matrix, glyphs)
16260 struct glyph_matrix *matrix;
16261 int glyphs;
16262 {
16263 int i;
16264 for (i = 0; i < matrix->nrows; ++i)
16265 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16266 }
16267
16268
16269 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16270 the glyph row and area where the glyph comes from. */
16271
16272 void
16273 dump_glyph (row, glyph, area)
16274 struct glyph_row *row;
16275 struct glyph *glyph;
16276 int area;
16277 {
16278 if (glyph->type == CHAR_GLYPH)
16279 {
16280 fprintf (stderr,
16281 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16282 glyph - row->glyphs[TEXT_AREA],
16283 'C',
16284 glyph->charpos,
16285 (BUFFERP (glyph->object)
16286 ? 'B'
16287 : (STRINGP (glyph->object)
16288 ? 'S'
16289 : '-')),
16290 glyph->pixel_width,
16291 glyph->u.ch,
16292 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16293 ? glyph->u.ch
16294 : '.'),
16295 glyph->face_id,
16296 glyph->left_box_line_p,
16297 glyph->right_box_line_p);
16298 }
16299 else if (glyph->type == STRETCH_GLYPH)
16300 {
16301 fprintf (stderr,
16302 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16303 glyph - row->glyphs[TEXT_AREA],
16304 'S',
16305 glyph->charpos,
16306 (BUFFERP (glyph->object)
16307 ? 'B'
16308 : (STRINGP (glyph->object)
16309 ? 'S'
16310 : '-')),
16311 glyph->pixel_width,
16312 0,
16313 '.',
16314 glyph->face_id,
16315 glyph->left_box_line_p,
16316 glyph->right_box_line_p);
16317 }
16318 else if (glyph->type == IMAGE_GLYPH)
16319 {
16320 fprintf (stderr,
16321 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16322 glyph - row->glyphs[TEXT_AREA],
16323 'I',
16324 glyph->charpos,
16325 (BUFFERP (glyph->object)
16326 ? 'B'
16327 : (STRINGP (glyph->object)
16328 ? 'S'
16329 : '-')),
16330 glyph->pixel_width,
16331 glyph->u.img_id,
16332 '.',
16333 glyph->face_id,
16334 glyph->left_box_line_p,
16335 glyph->right_box_line_p);
16336 }
16337 else if (glyph->type == COMPOSITE_GLYPH)
16338 {
16339 fprintf (stderr,
16340 " %5d %4c %6d %c %3d 0x%05x",
16341 glyph - row->glyphs[TEXT_AREA],
16342 '+',
16343 glyph->charpos,
16344 (BUFFERP (glyph->object)
16345 ? 'B'
16346 : (STRINGP (glyph->object)
16347 ? 'S'
16348 : '-')),
16349 glyph->pixel_width,
16350 glyph->u.cmp.id);
16351 if (glyph->u.cmp.automatic)
16352 fprintf (stderr,
16353 "[%d-%d]",
16354 glyph->u.cmp.from, glyph->u.cmp.to);
16355 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16356 glyph->face_id,
16357 glyph->left_box_line_p,
16358 glyph->right_box_line_p);
16359 }
16360 }
16361
16362
16363 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16364 GLYPHS 0 means don't show glyph contents.
16365 GLYPHS 1 means show glyphs in short form
16366 GLYPHS > 1 means show glyphs in long form. */
16367
16368 void
16369 dump_glyph_row (row, vpos, glyphs)
16370 struct glyph_row *row;
16371 int vpos, glyphs;
16372 {
16373 if (glyphs != 1)
16374 {
16375 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16376 fprintf (stderr, "======================================================================\n");
16377
16378 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16379 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16380 vpos,
16381 MATRIX_ROW_START_CHARPOS (row),
16382 MATRIX_ROW_END_CHARPOS (row),
16383 row->used[TEXT_AREA],
16384 row->contains_overlapping_glyphs_p,
16385 row->enabled_p,
16386 row->truncated_on_left_p,
16387 row->truncated_on_right_p,
16388 row->continued_p,
16389 MATRIX_ROW_CONTINUATION_LINE_P (row),
16390 row->displays_text_p,
16391 row->ends_at_zv_p,
16392 row->fill_line_p,
16393 row->ends_in_middle_of_char_p,
16394 row->starts_in_middle_of_char_p,
16395 row->mouse_face_p,
16396 row->x,
16397 row->y,
16398 row->pixel_width,
16399 row->height,
16400 row->visible_height,
16401 row->ascent,
16402 row->phys_ascent);
16403 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16404 row->end.overlay_string_index,
16405 row->continuation_lines_width);
16406 fprintf (stderr, "%9d %5d\n",
16407 CHARPOS (row->start.string_pos),
16408 CHARPOS (row->end.string_pos));
16409 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16410 row->end.dpvec_index);
16411 }
16412
16413 if (glyphs > 1)
16414 {
16415 int area;
16416
16417 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16418 {
16419 struct glyph *glyph = row->glyphs[area];
16420 struct glyph *glyph_end = glyph + row->used[area];
16421
16422 /* Glyph for a line end in text. */
16423 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16424 ++glyph_end;
16425
16426 if (glyph < glyph_end)
16427 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16428
16429 for (; glyph < glyph_end; ++glyph)
16430 dump_glyph (row, glyph, area);
16431 }
16432 }
16433 else if (glyphs == 1)
16434 {
16435 int area;
16436
16437 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16438 {
16439 char *s = (char *) alloca (row->used[area] + 1);
16440 int i;
16441
16442 for (i = 0; i < row->used[area]; ++i)
16443 {
16444 struct glyph *glyph = row->glyphs[area] + i;
16445 if (glyph->type == CHAR_GLYPH
16446 && glyph->u.ch < 0x80
16447 && glyph->u.ch >= ' ')
16448 s[i] = glyph->u.ch;
16449 else
16450 s[i] = '.';
16451 }
16452
16453 s[i] = '\0';
16454 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16455 }
16456 }
16457 }
16458
16459
16460 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16461 Sdump_glyph_matrix, 0, 1, "p",
16462 doc: /* Dump the current matrix of the selected window to stderr.
16463 Shows contents of glyph row structures. With non-nil
16464 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16465 glyphs in short form, otherwise show glyphs in long form. */)
16466 (glyphs)
16467 Lisp_Object glyphs;
16468 {
16469 struct window *w = XWINDOW (selected_window);
16470 struct buffer *buffer = XBUFFER (w->buffer);
16471
16472 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16473 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16474 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16475 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16476 fprintf (stderr, "=============================================\n");
16477 dump_glyph_matrix (w->current_matrix,
16478 NILP (glyphs) ? 0 : XINT (glyphs));
16479 return Qnil;
16480 }
16481
16482
16483 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16484 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16485 ()
16486 {
16487 struct frame *f = XFRAME (selected_frame);
16488 dump_glyph_matrix (f->current_matrix, 1);
16489 return Qnil;
16490 }
16491
16492
16493 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16494 doc: /* Dump glyph row ROW to stderr.
16495 GLYPH 0 means don't dump glyphs.
16496 GLYPH 1 means dump glyphs in short form.
16497 GLYPH > 1 or omitted means dump glyphs in long form. */)
16498 (row, glyphs)
16499 Lisp_Object row, glyphs;
16500 {
16501 struct glyph_matrix *matrix;
16502 int vpos;
16503
16504 CHECK_NUMBER (row);
16505 matrix = XWINDOW (selected_window)->current_matrix;
16506 vpos = XINT (row);
16507 if (vpos >= 0 && vpos < matrix->nrows)
16508 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16509 vpos,
16510 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16511 return Qnil;
16512 }
16513
16514
16515 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16516 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16517 GLYPH 0 means don't dump glyphs.
16518 GLYPH 1 means dump glyphs in short form.
16519 GLYPH > 1 or omitted means dump glyphs in long form. */)
16520 (row, glyphs)
16521 Lisp_Object row, glyphs;
16522 {
16523 struct frame *sf = SELECTED_FRAME ();
16524 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16525 int vpos;
16526
16527 CHECK_NUMBER (row);
16528 vpos = XINT (row);
16529 if (vpos >= 0 && vpos < m->nrows)
16530 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16531 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16532 return Qnil;
16533 }
16534
16535
16536 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16537 doc: /* Toggle tracing of redisplay.
16538 With ARG, turn tracing on if and only if ARG is positive. */)
16539 (arg)
16540 Lisp_Object arg;
16541 {
16542 if (NILP (arg))
16543 trace_redisplay_p = !trace_redisplay_p;
16544 else
16545 {
16546 arg = Fprefix_numeric_value (arg);
16547 trace_redisplay_p = XINT (arg) > 0;
16548 }
16549
16550 return Qnil;
16551 }
16552
16553
16554 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16555 doc: /* Like `format', but print result to stderr.
16556 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16557 (nargs, args)
16558 int nargs;
16559 Lisp_Object *args;
16560 {
16561 Lisp_Object s = Fformat (nargs, args);
16562 fprintf (stderr, "%s", SDATA (s));
16563 return Qnil;
16564 }
16565
16566 #endif /* GLYPH_DEBUG */
16567
16568
16569 \f
16570 /***********************************************************************
16571 Building Desired Matrix Rows
16572 ***********************************************************************/
16573
16574 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16575 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16576
16577 static struct glyph_row *
16578 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
16579 struct window *w;
16580 Lisp_Object overlay_arrow_string;
16581 {
16582 struct frame *f = XFRAME (WINDOW_FRAME (w));
16583 struct buffer *buffer = XBUFFER (w->buffer);
16584 struct buffer *old = current_buffer;
16585 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16586 int arrow_len = SCHARS (overlay_arrow_string);
16587 const unsigned char *arrow_end = arrow_string + arrow_len;
16588 const unsigned char *p;
16589 struct it it;
16590 int multibyte_p;
16591 int n_glyphs_before;
16592
16593 set_buffer_temp (buffer);
16594 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16595 it.glyph_row->used[TEXT_AREA] = 0;
16596 SET_TEXT_POS (it.position, 0, 0);
16597
16598 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16599 p = arrow_string;
16600 while (p < arrow_end)
16601 {
16602 Lisp_Object face, ilisp;
16603
16604 /* Get the next character. */
16605 if (multibyte_p)
16606 it.c = string_char_and_length (p, &it.len);
16607 else
16608 it.c = *p, it.len = 1;
16609 p += it.len;
16610
16611 /* Get its face. */
16612 ilisp = make_number (p - arrow_string);
16613 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16614 it.face_id = compute_char_face (f, it.c, face);
16615
16616 /* Compute its width, get its glyphs. */
16617 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16618 SET_TEXT_POS (it.position, -1, -1);
16619 PRODUCE_GLYPHS (&it);
16620
16621 /* If this character doesn't fit any more in the line, we have
16622 to remove some glyphs. */
16623 if (it.current_x > it.last_visible_x)
16624 {
16625 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16626 break;
16627 }
16628 }
16629
16630 set_buffer_temp (old);
16631 return it.glyph_row;
16632 }
16633
16634
16635 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16636 glyphs are only inserted for terminal frames since we can't really
16637 win with truncation glyphs when partially visible glyphs are
16638 involved. Which glyphs to insert is determined by
16639 produce_special_glyphs. */
16640
16641 static void
16642 insert_left_trunc_glyphs (it)
16643 struct it *it;
16644 {
16645 struct it truncate_it;
16646 struct glyph *from, *end, *to, *toend;
16647
16648 xassert (!FRAME_WINDOW_P (it->f));
16649
16650 /* Get the truncation glyphs. */
16651 truncate_it = *it;
16652 truncate_it.current_x = 0;
16653 truncate_it.face_id = DEFAULT_FACE_ID;
16654 truncate_it.glyph_row = &scratch_glyph_row;
16655 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16656 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16657 truncate_it.object = make_number (0);
16658 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16659
16660 /* Overwrite glyphs from IT with truncation glyphs. */
16661 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16662 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16663 to = it->glyph_row->glyphs[TEXT_AREA];
16664 toend = to + it->glyph_row->used[TEXT_AREA];
16665
16666 while (from < end)
16667 *to++ = *from++;
16668
16669 /* There may be padding glyphs left over. Overwrite them too. */
16670 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16671 {
16672 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16673 while (from < end)
16674 *to++ = *from++;
16675 }
16676
16677 if (to > toend)
16678 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16679 }
16680
16681
16682 /* Compute the pixel height and width of IT->glyph_row.
16683
16684 Most of the time, ascent and height of a display line will be equal
16685 to the max_ascent and max_height values of the display iterator
16686 structure. This is not the case if
16687
16688 1. We hit ZV without displaying anything. In this case, max_ascent
16689 and max_height will be zero.
16690
16691 2. We have some glyphs that don't contribute to the line height.
16692 (The glyph row flag contributes_to_line_height_p is for future
16693 pixmap extensions).
16694
16695 The first case is easily covered by using default values because in
16696 these cases, the line height does not really matter, except that it
16697 must not be zero. */
16698
16699 static void
16700 compute_line_metrics (it)
16701 struct it *it;
16702 {
16703 struct glyph_row *row = it->glyph_row;
16704 int area, i;
16705
16706 if (FRAME_WINDOW_P (it->f))
16707 {
16708 int i, min_y, max_y;
16709
16710 /* The line may consist of one space only, that was added to
16711 place the cursor on it. If so, the row's height hasn't been
16712 computed yet. */
16713 if (row->height == 0)
16714 {
16715 if (it->max_ascent + it->max_descent == 0)
16716 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16717 row->ascent = it->max_ascent;
16718 row->height = it->max_ascent + it->max_descent;
16719 row->phys_ascent = it->max_phys_ascent;
16720 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16721 row->extra_line_spacing = it->max_extra_line_spacing;
16722 }
16723
16724 /* Compute the width of this line. */
16725 row->pixel_width = row->x;
16726 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16727 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16728
16729 xassert (row->pixel_width >= 0);
16730 xassert (row->ascent >= 0 && row->height > 0);
16731
16732 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16733 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16734
16735 /* If first line's physical ascent is larger than its logical
16736 ascent, use the physical ascent, and make the row taller.
16737 This makes accented characters fully visible. */
16738 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16739 && row->phys_ascent > row->ascent)
16740 {
16741 row->height += row->phys_ascent - row->ascent;
16742 row->ascent = row->phys_ascent;
16743 }
16744
16745 /* Compute how much of the line is visible. */
16746 row->visible_height = row->height;
16747
16748 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16749 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16750
16751 if (row->y < min_y)
16752 row->visible_height -= min_y - row->y;
16753 if (row->y + row->height > max_y)
16754 row->visible_height -= row->y + row->height - max_y;
16755 }
16756 else
16757 {
16758 row->pixel_width = row->used[TEXT_AREA];
16759 if (row->continued_p)
16760 row->pixel_width -= it->continuation_pixel_width;
16761 else if (row->truncated_on_right_p)
16762 row->pixel_width -= it->truncation_pixel_width;
16763 row->ascent = row->phys_ascent = 0;
16764 row->height = row->phys_height = row->visible_height = 1;
16765 row->extra_line_spacing = 0;
16766 }
16767
16768 /* Compute a hash code for this row. */
16769 row->hash = 0;
16770 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16771 for (i = 0; i < row->used[area]; ++i)
16772 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16773 + row->glyphs[area][i].u.val
16774 + row->glyphs[area][i].face_id
16775 + row->glyphs[area][i].padding_p
16776 + (row->glyphs[area][i].type << 2));
16777
16778 it->max_ascent = it->max_descent = 0;
16779 it->max_phys_ascent = it->max_phys_descent = 0;
16780 }
16781
16782
16783 /* Append one space to the glyph row of iterator IT if doing a
16784 window-based redisplay. The space has the same face as
16785 IT->face_id. Value is non-zero if a space was added.
16786
16787 This function is called to make sure that there is always one glyph
16788 at the end of a glyph row that the cursor can be set on under
16789 window-systems. (If there weren't such a glyph we would not know
16790 how wide and tall a box cursor should be displayed).
16791
16792 At the same time this space let's a nicely handle clearing to the
16793 end of the line if the row ends in italic text. */
16794
16795 static int
16796 append_space_for_newline (it, default_face_p)
16797 struct it *it;
16798 int default_face_p;
16799 {
16800 if (FRAME_WINDOW_P (it->f))
16801 {
16802 int n = it->glyph_row->used[TEXT_AREA];
16803
16804 if (it->glyph_row->glyphs[TEXT_AREA] + n
16805 < it->glyph_row->glyphs[1 + TEXT_AREA])
16806 {
16807 /* Save some values that must not be changed.
16808 Must save IT->c and IT->len because otherwise
16809 ITERATOR_AT_END_P wouldn't work anymore after
16810 append_space_for_newline has been called. */
16811 enum display_element_type saved_what = it->what;
16812 int saved_c = it->c, saved_len = it->len;
16813 int saved_x = it->current_x;
16814 int saved_face_id = it->face_id;
16815 struct text_pos saved_pos;
16816 Lisp_Object saved_object;
16817 struct face *face;
16818
16819 saved_object = it->object;
16820 saved_pos = it->position;
16821
16822 it->what = IT_CHARACTER;
16823 bzero (&it->position, sizeof it->position);
16824 it->object = make_number (0);
16825 it->c = ' ';
16826 it->len = 1;
16827
16828 if (default_face_p)
16829 it->face_id = DEFAULT_FACE_ID;
16830 else if (it->face_before_selective_p)
16831 it->face_id = it->saved_face_id;
16832 face = FACE_FROM_ID (it->f, it->face_id);
16833 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16834
16835 PRODUCE_GLYPHS (it);
16836
16837 it->override_ascent = -1;
16838 it->constrain_row_ascent_descent_p = 0;
16839 it->current_x = saved_x;
16840 it->object = saved_object;
16841 it->position = saved_pos;
16842 it->what = saved_what;
16843 it->face_id = saved_face_id;
16844 it->len = saved_len;
16845 it->c = saved_c;
16846 return 1;
16847 }
16848 }
16849
16850 return 0;
16851 }
16852
16853
16854 /* Extend the face of the last glyph in the text area of IT->glyph_row
16855 to the end of the display line. Called from display_line.
16856 If the glyph row is empty, add a space glyph to it so that we
16857 know the face to draw. Set the glyph row flag fill_line_p. */
16858
16859 static void
16860 extend_face_to_end_of_line (it)
16861 struct it *it;
16862 {
16863 struct face *face;
16864 struct frame *f = it->f;
16865
16866 /* If line is already filled, do nothing. */
16867 if (it->current_x >= it->last_visible_x)
16868 return;
16869
16870 /* Face extension extends the background and box of IT->face_id
16871 to the end of the line. If the background equals the background
16872 of the frame, we don't have to do anything. */
16873 if (it->face_before_selective_p)
16874 face = FACE_FROM_ID (it->f, it->saved_face_id);
16875 else
16876 face = FACE_FROM_ID (f, it->face_id);
16877
16878 if (FRAME_WINDOW_P (f)
16879 && it->glyph_row->displays_text_p
16880 && face->box == FACE_NO_BOX
16881 && face->background == FRAME_BACKGROUND_PIXEL (f)
16882 && !face->stipple)
16883 return;
16884
16885 /* Set the glyph row flag indicating that the face of the last glyph
16886 in the text area has to be drawn to the end of the text area. */
16887 it->glyph_row->fill_line_p = 1;
16888
16889 /* If current character of IT is not ASCII, make sure we have the
16890 ASCII face. This will be automatically undone the next time
16891 get_next_display_element returns a multibyte character. Note
16892 that the character will always be single byte in unibyte
16893 text. */
16894 if (!ASCII_CHAR_P (it->c))
16895 {
16896 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16897 }
16898
16899 if (FRAME_WINDOW_P (f))
16900 {
16901 /* If the row is empty, add a space with the current face of IT,
16902 so that we know which face to draw. */
16903 if (it->glyph_row->used[TEXT_AREA] == 0)
16904 {
16905 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16906 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16907 it->glyph_row->used[TEXT_AREA] = 1;
16908 }
16909 }
16910 else
16911 {
16912 /* Save some values that must not be changed. */
16913 int saved_x = it->current_x;
16914 struct text_pos saved_pos;
16915 Lisp_Object saved_object;
16916 enum display_element_type saved_what = it->what;
16917 int saved_face_id = it->face_id;
16918
16919 saved_object = it->object;
16920 saved_pos = it->position;
16921
16922 it->what = IT_CHARACTER;
16923 bzero (&it->position, sizeof it->position);
16924 it->object = make_number (0);
16925 it->c = ' ';
16926 it->len = 1;
16927 it->face_id = face->id;
16928
16929 PRODUCE_GLYPHS (it);
16930
16931 while (it->current_x <= it->last_visible_x)
16932 PRODUCE_GLYPHS (it);
16933
16934 /* Don't count these blanks really. It would let us insert a left
16935 truncation glyph below and make us set the cursor on them, maybe. */
16936 it->current_x = saved_x;
16937 it->object = saved_object;
16938 it->position = saved_pos;
16939 it->what = saved_what;
16940 it->face_id = saved_face_id;
16941 }
16942 }
16943
16944
16945 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16946 trailing whitespace. */
16947
16948 static int
16949 trailing_whitespace_p (charpos)
16950 int charpos;
16951 {
16952 int bytepos = CHAR_TO_BYTE (charpos);
16953 int c = 0;
16954
16955 while (bytepos < ZV_BYTE
16956 && (c = FETCH_CHAR (bytepos),
16957 c == ' ' || c == '\t'))
16958 ++bytepos;
16959
16960 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16961 {
16962 if (bytepos != PT_BYTE)
16963 return 1;
16964 }
16965 return 0;
16966 }
16967
16968
16969 /* Highlight trailing whitespace, if any, in ROW. */
16970
16971 void
16972 highlight_trailing_whitespace (f, row)
16973 struct frame *f;
16974 struct glyph_row *row;
16975 {
16976 int used = row->used[TEXT_AREA];
16977
16978 if (used)
16979 {
16980 struct glyph *start = row->glyphs[TEXT_AREA];
16981 struct glyph *glyph = start + used - 1;
16982
16983 /* Skip over glyphs inserted to display the cursor at the
16984 end of a line, for extending the face of the last glyph
16985 to the end of the line on terminals, and for truncation
16986 and continuation glyphs. */
16987 while (glyph >= start
16988 && glyph->type == CHAR_GLYPH
16989 && INTEGERP (glyph->object))
16990 --glyph;
16991
16992 /* If last glyph is a space or stretch, and it's trailing
16993 whitespace, set the face of all trailing whitespace glyphs in
16994 IT->glyph_row to `trailing-whitespace'. */
16995 if (glyph >= start
16996 && BUFFERP (glyph->object)
16997 && (glyph->type == STRETCH_GLYPH
16998 || (glyph->type == CHAR_GLYPH
16999 && glyph->u.ch == ' '))
17000 && trailing_whitespace_p (glyph->charpos))
17001 {
17002 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17003 if (face_id < 0)
17004 return;
17005
17006 while (glyph >= start
17007 && BUFFERP (glyph->object)
17008 && (glyph->type == STRETCH_GLYPH
17009 || (glyph->type == CHAR_GLYPH
17010 && glyph->u.ch == ' ')))
17011 (glyph--)->face_id = face_id;
17012 }
17013 }
17014 }
17015
17016
17017 /* Value is non-zero if glyph row ROW in window W should be
17018 used to hold the cursor. */
17019
17020 static int
17021 cursor_row_p (w, row)
17022 struct window *w;
17023 struct glyph_row *row;
17024 {
17025 int cursor_row_p = 1;
17026
17027 if (PT == MATRIX_ROW_END_CHARPOS (row))
17028 {
17029 /* Suppose the row ends on a string.
17030 Unless the row is continued, that means it ends on a newline
17031 in the string. If it's anything other than a display string
17032 (e.g. a before-string from an overlay), we don't want the
17033 cursor there. (This heuristic seems to give the optimal
17034 behavior for the various types of multi-line strings.) */
17035 if (CHARPOS (row->end.string_pos) >= 0)
17036 {
17037 if (row->continued_p)
17038 cursor_row_p = 1;
17039 else
17040 {
17041 /* Check for `display' property. */
17042 struct glyph *beg = row->glyphs[TEXT_AREA];
17043 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17044 struct glyph *glyph;
17045
17046 cursor_row_p = 0;
17047 for (glyph = end; glyph >= beg; --glyph)
17048 if (STRINGP (glyph->object))
17049 {
17050 Lisp_Object prop
17051 = Fget_char_property (make_number (PT),
17052 Qdisplay, Qnil);
17053 cursor_row_p =
17054 (!NILP (prop)
17055 && display_prop_string_p (prop, glyph->object));
17056 break;
17057 }
17058 }
17059 }
17060 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17061 {
17062 /* If the row ends in middle of a real character,
17063 and the line is continued, we want the cursor here.
17064 That's because MATRIX_ROW_END_CHARPOS would equal
17065 PT if PT is before the character. */
17066 if (!row->ends_in_ellipsis_p)
17067 cursor_row_p = row->continued_p;
17068 else
17069 /* If the row ends in an ellipsis, then
17070 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
17071 We want that position to be displayed after the ellipsis. */
17072 cursor_row_p = 0;
17073 }
17074 /* If the row ends at ZV, display the cursor at the end of that
17075 row instead of at the start of the row below. */
17076 else if (row->ends_at_zv_p)
17077 cursor_row_p = 1;
17078 else
17079 cursor_row_p = 0;
17080 }
17081
17082 return cursor_row_p;
17083 }
17084
17085 \f
17086
17087 /* Push the display property PROP so that it will be rendered at the
17088 current position in IT. Return 1 if PROP was successfully pushed,
17089 0 otherwise. */
17090
17091 static int
17092 push_display_prop (struct it *it, Lisp_Object prop)
17093 {
17094 push_it (it);
17095
17096 if (STRINGP (prop))
17097 {
17098 if (SCHARS (prop) == 0)
17099 {
17100 pop_it (it);
17101 return 0;
17102 }
17103
17104 it->string = prop;
17105 it->multibyte_p = STRING_MULTIBYTE (it->string);
17106 it->current.overlay_string_index = -1;
17107 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17108 it->end_charpos = it->string_nchars = SCHARS (it->string);
17109 it->method = GET_FROM_STRING;
17110 it->stop_charpos = 0;
17111 }
17112 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17113 {
17114 it->method = GET_FROM_STRETCH;
17115 it->object = prop;
17116 }
17117 #ifdef HAVE_WINDOW_SYSTEM
17118 else if (IMAGEP (prop))
17119 {
17120 it->what = IT_IMAGE;
17121 it->image_id = lookup_image (it->f, prop);
17122 it->method = GET_FROM_IMAGE;
17123 }
17124 #endif /* HAVE_WINDOW_SYSTEM */
17125 else
17126 {
17127 pop_it (it); /* bogus display property, give up */
17128 return 0;
17129 }
17130
17131 return 1;
17132 }
17133
17134 /* Return the character-property PROP at the current position in IT. */
17135
17136 static Lisp_Object
17137 get_it_property (it, prop)
17138 struct it *it;
17139 Lisp_Object prop;
17140 {
17141 Lisp_Object position;
17142
17143 if (STRINGP (it->object))
17144 position = make_number (IT_STRING_CHARPOS (*it));
17145 else if (BUFFERP (it->object))
17146 position = make_number (IT_CHARPOS (*it));
17147 else
17148 return Qnil;
17149
17150 return Fget_char_property (position, prop, it->object);
17151 }
17152
17153 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17154
17155 static void
17156 handle_line_prefix (struct it *it)
17157 {
17158 Lisp_Object prefix;
17159 if (it->continuation_lines_width > 0)
17160 {
17161 prefix = get_it_property (it, Qwrap_prefix);
17162 if (NILP (prefix))
17163 prefix = Vwrap_prefix;
17164 }
17165 else
17166 {
17167 prefix = get_it_property (it, Qline_prefix);
17168 if (NILP (prefix))
17169 prefix = Vline_prefix;
17170 }
17171 if (! NILP (prefix) && push_display_prop (it, prefix))
17172 {
17173 /* If the prefix is wider than the window, and we try to wrap
17174 it, it would acquire its own wrap prefix, and so on till the
17175 iterator stack overflows. So, don't wrap the prefix. */
17176 it->line_wrap = TRUNCATE;
17177 it->avoid_cursor_p = 1;
17178 }
17179 }
17180
17181 \f
17182
17183 /* Construct the glyph row IT->glyph_row in the desired matrix of
17184 IT->w from text at the current position of IT. See dispextern.h
17185 for an overview of struct it. Value is non-zero if
17186 IT->glyph_row displays text, as opposed to a line displaying ZV
17187 only. */
17188
17189 static int
17190 display_line (it)
17191 struct it *it;
17192 {
17193 struct glyph_row *row = it->glyph_row;
17194 Lisp_Object overlay_arrow_string;
17195 struct it wrap_it;
17196 int may_wrap = 0, wrap_x;
17197 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17198 int wrap_row_phys_ascent, wrap_row_phys_height;
17199 int wrap_row_extra_line_spacing;
17200 struct display_pos row_end;
17201 int cvpos;
17202
17203 /* We always start displaying at hpos zero even if hscrolled. */
17204 xassert (it->hpos == 0 && it->current_x == 0);
17205
17206 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17207 >= it->w->desired_matrix->nrows)
17208 {
17209 it->w->nrows_scale_factor++;
17210 fonts_changed_p = 1;
17211 return 0;
17212 }
17213
17214 /* Is IT->w showing the region? */
17215 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17216
17217 /* Clear the result glyph row and enable it. */
17218 prepare_desired_row (row);
17219
17220 row->y = it->current_y;
17221 row->start = it->start;
17222 row->continuation_lines_width = it->continuation_lines_width;
17223 row->displays_text_p = 1;
17224 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17225 it->starts_in_middle_of_char_p = 0;
17226
17227 /* Arrange the overlays nicely for our purposes. Usually, we call
17228 display_line on only one line at a time, in which case this
17229 can't really hurt too much, or we call it on lines which appear
17230 one after another in the buffer, in which case all calls to
17231 recenter_overlay_lists but the first will be pretty cheap. */
17232 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17233
17234 /* Move over display elements that are not visible because we are
17235 hscrolled. This may stop at an x-position < IT->first_visible_x
17236 if the first glyph is partially visible or if we hit a line end. */
17237 if (it->current_x < it->first_visible_x)
17238 {
17239 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17240 MOVE_TO_POS | MOVE_TO_X);
17241 }
17242 else
17243 {
17244 /* We only do this when not calling `move_it_in_display_line_to'
17245 above, because move_it_in_display_line_to calls
17246 handle_line_prefix itself. */
17247 handle_line_prefix (it);
17248 }
17249
17250 /* Get the initial row height. This is either the height of the
17251 text hscrolled, if there is any, or zero. */
17252 row->ascent = it->max_ascent;
17253 row->height = it->max_ascent + it->max_descent;
17254 row->phys_ascent = it->max_phys_ascent;
17255 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17256 row->extra_line_spacing = it->max_extra_line_spacing;
17257
17258 /* Loop generating characters. The loop is left with IT on the next
17259 character to display. */
17260 while (1)
17261 {
17262 int n_glyphs_before, hpos_before, x_before;
17263 int x, i, nglyphs;
17264 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17265
17266 /* Retrieve the next thing to display. Value is zero if end of
17267 buffer reached. */
17268 if (!get_next_display_element (it))
17269 {
17270 /* Maybe add a space at the end of this line that is used to
17271 display the cursor there under X. Set the charpos of the
17272 first glyph of blank lines not corresponding to any text
17273 to -1. */
17274 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17275 row->exact_window_width_line_p = 1;
17276 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17277 || row->used[TEXT_AREA] == 0)
17278 {
17279 row->glyphs[TEXT_AREA]->charpos = -1;
17280 row->displays_text_p = 0;
17281
17282 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17283 && (!MINI_WINDOW_P (it->w)
17284 || (minibuf_level && EQ (it->window, minibuf_window))))
17285 row->indicate_empty_line_p = 1;
17286 }
17287
17288 it->continuation_lines_width = 0;
17289 row->ends_at_zv_p = 1;
17290 /* A row that displays right-to-left text must always have
17291 its last face extended all the way to the end of line,
17292 even if this row ends in ZV. */
17293 if (row->reversed_p)
17294 extend_face_to_end_of_line (it);
17295 break;
17296 }
17297
17298 /* Now, get the metrics of what we want to display. This also
17299 generates glyphs in `row' (which is IT->glyph_row). */
17300 n_glyphs_before = row->used[TEXT_AREA];
17301 x = it->current_x;
17302
17303 /* Remember the line height so far in case the next element doesn't
17304 fit on the line. */
17305 if (it->line_wrap != TRUNCATE)
17306 {
17307 ascent = it->max_ascent;
17308 descent = it->max_descent;
17309 phys_ascent = it->max_phys_ascent;
17310 phys_descent = it->max_phys_descent;
17311
17312 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17313 {
17314 if (IT_DISPLAYING_WHITESPACE (it))
17315 may_wrap = 1;
17316 else if (may_wrap)
17317 {
17318 wrap_it = *it;
17319 wrap_x = x;
17320 wrap_row_used = row->used[TEXT_AREA];
17321 wrap_row_ascent = row->ascent;
17322 wrap_row_height = row->height;
17323 wrap_row_phys_ascent = row->phys_ascent;
17324 wrap_row_phys_height = row->phys_height;
17325 wrap_row_extra_line_spacing = row->extra_line_spacing;
17326 may_wrap = 0;
17327 }
17328 }
17329 }
17330
17331 PRODUCE_GLYPHS (it);
17332
17333 /* If this display element was in marginal areas, continue with
17334 the next one. */
17335 if (it->area != TEXT_AREA)
17336 {
17337 row->ascent = max (row->ascent, it->max_ascent);
17338 row->height = max (row->height, it->max_ascent + it->max_descent);
17339 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17340 row->phys_height = max (row->phys_height,
17341 it->max_phys_ascent + it->max_phys_descent);
17342 row->extra_line_spacing = max (row->extra_line_spacing,
17343 it->max_extra_line_spacing);
17344 set_iterator_to_next (it, 1);
17345 continue;
17346 }
17347
17348 /* Does the display element fit on the line? If we truncate
17349 lines, we should draw past the right edge of the window. If
17350 we don't truncate, we want to stop so that we can display the
17351 continuation glyph before the right margin. If lines are
17352 continued, there are two possible strategies for characters
17353 resulting in more than 1 glyph (e.g. tabs): Display as many
17354 glyphs as possible in this line and leave the rest for the
17355 continuation line, or display the whole element in the next
17356 line. Original redisplay did the former, so we do it also. */
17357 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17358 hpos_before = it->hpos;
17359 x_before = x;
17360
17361 if (/* Not a newline. */
17362 nglyphs > 0
17363 /* Glyphs produced fit entirely in the line. */
17364 && it->current_x < it->last_visible_x)
17365 {
17366 it->hpos += nglyphs;
17367 row->ascent = max (row->ascent, it->max_ascent);
17368 row->height = max (row->height, it->max_ascent + it->max_descent);
17369 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17370 row->phys_height = max (row->phys_height,
17371 it->max_phys_ascent + it->max_phys_descent);
17372 row->extra_line_spacing = max (row->extra_line_spacing,
17373 it->max_extra_line_spacing);
17374 if (it->current_x - it->pixel_width < it->first_visible_x)
17375 row->x = x - it->first_visible_x;
17376 }
17377 else
17378 {
17379 int new_x;
17380 struct glyph *glyph;
17381
17382 for (i = 0; i < nglyphs; ++i, x = new_x)
17383 {
17384 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17385 new_x = x + glyph->pixel_width;
17386
17387 if (/* Lines are continued. */
17388 it->line_wrap != TRUNCATE
17389 && (/* Glyph doesn't fit on the line. */
17390 new_x > it->last_visible_x
17391 /* Or it fits exactly on a window system frame. */
17392 || (new_x == it->last_visible_x
17393 && FRAME_WINDOW_P (it->f))))
17394 {
17395 /* End of a continued line. */
17396
17397 if (it->hpos == 0
17398 || (new_x == it->last_visible_x
17399 && FRAME_WINDOW_P (it->f)))
17400 {
17401 /* Current glyph is the only one on the line or
17402 fits exactly on the line. We must continue
17403 the line because we can't draw the cursor
17404 after the glyph. */
17405 row->continued_p = 1;
17406 it->current_x = new_x;
17407 it->continuation_lines_width += new_x;
17408 ++it->hpos;
17409 if (i == nglyphs - 1)
17410 {
17411 /* If line-wrap is on, check if a previous
17412 wrap point was found. */
17413 if (wrap_row_used > 0
17414 /* Even if there is a previous wrap
17415 point, continue the line here as
17416 usual, if (i) the previous character
17417 was a space or tab AND (ii) the
17418 current character is not. */
17419 && (!may_wrap
17420 || IT_DISPLAYING_WHITESPACE (it)))
17421 goto back_to_wrap;
17422
17423 set_iterator_to_next (it, 1);
17424 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17425 {
17426 if (!get_next_display_element (it))
17427 {
17428 row->exact_window_width_line_p = 1;
17429 it->continuation_lines_width = 0;
17430 row->continued_p = 0;
17431 row->ends_at_zv_p = 1;
17432 }
17433 else if (ITERATOR_AT_END_OF_LINE_P (it))
17434 {
17435 row->continued_p = 0;
17436 row->exact_window_width_line_p = 1;
17437 }
17438 }
17439 }
17440 }
17441 else if (CHAR_GLYPH_PADDING_P (*glyph)
17442 && !FRAME_WINDOW_P (it->f))
17443 {
17444 /* A padding glyph that doesn't fit on this line.
17445 This means the whole character doesn't fit
17446 on the line. */
17447 row->used[TEXT_AREA] = n_glyphs_before;
17448
17449 /* Fill the rest of the row with continuation
17450 glyphs like in 20.x. */
17451 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17452 < row->glyphs[1 + TEXT_AREA])
17453 produce_special_glyphs (it, IT_CONTINUATION);
17454
17455 row->continued_p = 1;
17456 it->current_x = x_before;
17457 it->continuation_lines_width += x_before;
17458
17459 /* Restore the height to what it was before the
17460 element not fitting on the line. */
17461 it->max_ascent = ascent;
17462 it->max_descent = descent;
17463 it->max_phys_ascent = phys_ascent;
17464 it->max_phys_descent = phys_descent;
17465 }
17466 else if (wrap_row_used > 0)
17467 {
17468 back_to_wrap:
17469 *it = wrap_it;
17470 it->continuation_lines_width += wrap_x;
17471 row->used[TEXT_AREA] = wrap_row_used;
17472 row->ascent = wrap_row_ascent;
17473 row->height = wrap_row_height;
17474 row->phys_ascent = wrap_row_phys_ascent;
17475 row->phys_height = wrap_row_phys_height;
17476 row->extra_line_spacing = wrap_row_extra_line_spacing;
17477 row->continued_p = 1;
17478 row->ends_at_zv_p = 0;
17479 row->exact_window_width_line_p = 0;
17480 it->continuation_lines_width += x;
17481
17482 /* Make sure that a non-default face is extended
17483 up to the right margin of the window. */
17484 extend_face_to_end_of_line (it);
17485 }
17486 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17487 {
17488 /* A TAB that extends past the right edge of the
17489 window. This produces a single glyph on
17490 window system frames. We leave the glyph in
17491 this row and let it fill the row, but don't
17492 consume the TAB. */
17493 it->continuation_lines_width += it->last_visible_x;
17494 row->ends_in_middle_of_char_p = 1;
17495 row->continued_p = 1;
17496 glyph->pixel_width = it->last_visible_x - x;
17497 it->starts_in_middle_of_char_p = 1;
17498 }
17499 else
17500 {
17501 /* Something other than a TAB that draws past
17502 the right edge of the window. Restore
17503 positions to values before the element. */
17504 row->used[TEXT_AREA] = n_glyphs_before + i;
17505
17506 /* Display continuation glyphs. */
17507 if (!FRAME_WINDOW_P (it->f))
17508 produce_special_glyphs (it, IT_CONTINUATION);
17509 row->continued_p = 1;
17510
17511 it->current_x = x_before;
17512 it->continuation_lines_width += x;
17513 extend_face_to_end_of_line (it);
17514
17515 if (nglyphs > 1 && i > 0)
17516 {
17517 row->ends_in_middle_of_char_p = 1;
17518 it->starts_in_middle_of_char_p = 1;
17519 }
17520
17521 /* Restore the height to what it was before the
17522 element not fitting on the line. */
17523 it->max_ascent = ascent;
17524 it->max_descent = descent;
17525 it->max_phys_ascent = phys_ascent;
17526 it->max_phys_descent = phys_descent;
17527 }
17528
17529 break;
17530 }
17531 else if (new_x > it->first_visible_x)
17532 {
17533 /* Increment number of glyphs actually displayed. */
17534 ++it->hpos;
17535
17536 if (x < it->first_visible_x)
17537 /* Glyph is partially visible, i.e. row starts at
17538 negative X position. */
17539 row->x = x - it->first_visible_x;
17540 }
17541 else
17542 {
17543 /* Glyph is completely off the left margin of the
17544 window. This should not happen because of the
17545 move_it_in_display_line at the start of this
17546 function, unless the text display area of the
17547 window is empty. */
17548 xassert (it->first_visible_x <= it->last_visible_x);
17549 }
17550 }
17551
17552 row->ascent = max (row->ascent, it->max_ascent);
17553 row->height = max (row->height, it->max_ascent + it->max_descent);
17554 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17555 row->phys_height = max (row->phys_height,
17556 it->max_phys_ascent + it->max_phys_descent);
17557 row->extra_line_spacing = max (row->extra_line_spacing,
17558 it->max_extra_line_spacing);
17559
17560 /* End of this display line if row is continued. */
17561 if (row->continued_p || row->ends_at_zv_p)
17562 break;
17563 }
17564
17565 at_end_of_line:
17566 /* Is this a line end? If yes, we're also done, after making
17567 sure that a non-default face is extended up to the right
17568 margin of the window. */
17569 if (ITERATOR_AT_END_OF_LINE_P (it))
17570 {
17571 int used_before = row->used[TEXT_AREA];
17572
17573 row->ends_in_newline_from_string_p = STRINGP (it->object);
17574
17575 /* Add a space at the end of the line that is used to
17576 display the cursor there. */
17577 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17578 append_space_for_newline (it, 0);
17579
17580 /* Extend the face to the end of the line. */
17581 extend_face_to_end_of_line (it);
17582
17583 /* Make sure we have the position. */
17584 if (used_before == 0)
17585 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17586
17587 /* Consume the line end. This skips over invisible lines. */
17588 set_iterator_to_next (it, 1);
17589 it->continuation_lines_width = 0;
17590 break;
17591 }
17592
17593 /* Proceed with next display element. Note that this skips
17594 over lines invisible because of selective display. */
17595 set_iterator_to_next (it, 1);
17596
17597 /* If we truncate lines, we are done when the last displayed
17598 glyphs reach past the right margin of the window. */
17599 if (it->line_wrap == TRUNCATE
17600 && (FRAME_WINDOW_P (it->f)
17601 ? (it->current_x >= it->last_visible_x)
17602 : (it->current_x > it->last_visible_x)))
17603 {
17604 /* Maybe add truncation glyphs. */
17605 if (!FRAME_WINDOW_P (it->f))
17606 {
17607 int i, n;
17608
17609 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17610 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17611 break;
17612
17613 for (n = row->used[TEXT_AREA]; i < n; ++i)
17614 {
17615 row->used[TEXT_AREA] = i;
17616 produce_special_glyphs (it, IT_TRUNCATION);
17617 }
17618 }
17619 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17620 {
17621 /* Don't truncate if we can overflow newline into fringe. */
17622 if (!get_next_display_element (it))
17623 {
17624 it->continuation_lines_width = 0;
17625 row->ends_at_zv_p = 1;
17626 row->exact_window_width_line_p = 1;
17627 break;
17628 }
17629 if (ITERATOR_AT_END_OF_LINE_P (it))
17630 {
17631 row->exact_window_width_line_p = 1;
17632 goto at_end_of_line;
17633 }
17634 }
17635
17636 row->truncated_on_right_p = 1;
17637 it->continuation_lines_width = 0;
17638 reseat_at_next_visible_line_start (it, 0);
17639 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17640 it->hpos = hpos_before;
17641 it->current_x = x_before;
17642 break;
17643 }
17644 }
17645
17646 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17647 at the left window margin. */
17648 if (it->first_visible_x
17649 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
17650 {
17651 if (!FRAME_WINDOW_P (it->f))
17652 insert_left_trunc_glyphs (it);
17653 row->truncated_on_left_p = 1;
17654 }
17655
17656 /* If the start of this line is the overlay arrow-position, then
17657 mark this glyph row as the one containing the overlay arrow.
17658 This is clearly a mess with variable size fonts. It would be
17659 better to let it be displayed like cursors under X. */
17660 if ((row->displays_text_p || !overlay_arrow_seen)
17661 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17662 !NILP (overlay_arrow_string)))
17663 {
17664 /* Overlay arrow in window redisplay is a fringe bitmap. */
17665 if (STRINGP (overlay_arrow_string))
17666 {
17667 struct glyph_row *arrow_row
17668 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17669 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17670 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17671 struct glyph *p = row->glyphs[TEXT_AREA];
17672 struct glyph *p2, *end;
17673
17674 /* Copy the arrow glyphs. */
17675 while (glyph < arrow_end)
17676 *p++ = *glyph++;
17677
17678 /* Throw away padding glyphs. */
17679 p2 = p;
17680 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17681 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17682 ++p2;
17683 if (p2 > p)
17684 {
17685 while (p2 < end)
17686 *p++ = *p2++;
17687 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17688 }
17689 }
17690 else
17691 {
17692 xassert (INTEGERP (overlay_arrow_string));
17693 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17694 }
17695 overlay_arrow_seen = 1;
17696 }
17697
17698 /* Compute pixel dimensions of this line. */
17699 compute_line_metrics (it);
17700
17701 /* Remember the position at which this line ends. */
17702 row->end = row_end = it->current;
17703 if (it->bidi_p)
17704 {
17705 /* ROW->start and ROW->end must be the smallest and largest
17706 buffer positions in ROW. But if ROW was bidi-reordered,
17707 these two positions can be anywhere in the row, so we must
17708 rescan all of the ROW's glyphs to find them. */
17709 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17710 lines' rows is implemented for bidi-reordered rows. */
17711 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17712 struct glyph *g;
17713 struct it save_it;
17714 struct text_pos tpos;
17715
17716 for (g = row->glyphs[TEXT_AREA];
17717 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17718 g++)
17719 {
17720 if (BUFFERP (g->object))
17721 {
17722 if (g->charpos > 0 && g->charpos < min_pos)
17723 min_pos = g->charpos;
17724 if (g->charpos > max_pos)
17725 max_pos = g->charpos;
17726 }
17727 }
17728 /* Empty lines have a valid buffer position at their first
17729 glyph, but that glyph's OBJECT is zero, as if it didn't come
17730 from a buffer. If we didn't find any valid buffer positions
17731 in this row, maybe we have such an empty line. */
17732 if (min_pos == ZV + 1 && row->used[TEXT_AREA])
17733 {
17734 for (g = row->glyphs[TEXT_AREA];
17735 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17736 g++)
17737 {
17738 if (INTEGERP (g->object))
17739 {
17740 if (g->charpos > 0 && g->charpos < min_pos)
17741 min_pos = g->charpos;
17742 if (g->charpos > max_pos)
17743 max_pos = g->charpos;
17744 }
17745 }
17746 }
17747 if (min_pos <= ZV)
17748 {
17749 if (min_pos != row->start.pos.charpos)
17750 {
17751 row->start.pos.charpos = min_pos;
17752 row->start.pos.bytepos = CHAR_TO_BYTE (min_pos);
17753 }
17754 if (max_pos == 0)
17755 max_pos = min_pos;
17756 }
17757 /* For ROW->end, we need the position that is _after_ max_pos,
17758 in the logical order, unless we are at ZV. */
17759 if (row->ends_at_zv_p)
17760 {
17761 row_end = row->end = it->current;
17762 if (!row->used[TEXT_AREA])
17763 {
17764 row->start.pos.charpos = row_end.pos.charpos;
17765 row->start.pos.bytepos = row_end.pos.bytepos;
17766 }
17767 }
17768 else if (row->used[TEXT_AREA] && max_pos)
17769 {
17770 SET_TEXT_POS (tpos, max_pos + 1, CHAR_TO_BYTE (max_pos + 1));
17771 row_end = it->current;
17772 row_end.pos = tpos;
17773 /* If the character at max_pos+1 is a newline, skip that as
17774 well. Note that this may skip some invisible text. */
17775 if (FETCH_CHAR (tpos.bytepos) == '\n'
17776 || (FETCH_CHAR (tpos.bytepos) == '\r' && it->selective))
17777 {
17778 save_it = *it;
17779 it->bidi_p = 0;
17780 reseat_1 (it, tpos, 0);
17781 set_iterator_to_next (it, 1);
17782 /* Record the position after the newline of a continued
17783 row. We will need that to set ROW->end of the last
17784 row produced for a continued line. */
17785 if (row->continued_p)
17786 {
17787 save_it.eol_pos.charpos = IT_CHARPOS (*it);
17788 save_it.eol_pos.bytepos = IT_BYTEPOS (*it);
17789 }
17790 else
17791 {
17792 row_end = it->current;
17793 save_it.eol_pos.charpos = save_it.eol_pos.bytepos = 0;
17794 }
17795 *it = save_it;
17796 }
17797 else if (!row->continued_p
17798 && row->continuation_lines_width
17799 && it->eol_pos.charpos > 0)
17800 {
17801 /* Last row of a continued line. Use the position
17802 recorded in ROW->eol_pos, to the effect that the
17803 newline belongs to this row, not to the row which
17804 displays the character with the largest buffer
17805 position. */
17806 row_end.pos = it->eol_pos;
17807 it->eol_pos.charpos = it->eol_pos.bytepos = 0;
17808 }
17809 row->end = row_end;
17810 }
17811 }
17812
17813 /* Record whether this row ends inside an ellipsis. */
17814 row->ends_in_ellipsis_p
17815 = (it->method == GET_FROM_DISPLAY_VECTOR
17816 && it->ellipsis_p);
17817
17818 /* Save fringe bitmaps in this row. */
17819 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17820 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17821 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17822 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17823
17824 it->left_user_fringe_bitmap = 0;
17825 it->left_user_fringe_face_id = 0;
17826 it->right_user_fringe_bitmap = 0;
17827 it->right_user_fringe_face_id = 0;
17828
17829 /* Maybe set the cursor. */
17830 cvpos = it->w->cursor.vpos;
17831 if ((cvpos < 0
17832 /* In bidi-reordered rows, keep checking for proper cursor
17833 position even if one has been found already, because buffer
17834 positions in such rows change non-linearly with ROW->VPOS,
17835 when a line is continued. One exception: when we are at ZV,
17836 display cursor on the first suitable glyph row, since all
17837 the empty rows after that also have their position set to ZV. */
17838 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17839 lines' rows is implemented for bidi-reordered rows. */
17840 || (it->bidi_p
17841 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17842 && PT >= MATRIX_ROW_START_CHARPOS (row)
17843 && PT <= MATRIX_ROW_END_CHARPOS (row)
17844 && cursor_row_p (it->w, row))
17845 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17846
17847 /* Highlight trailing whitespace. */
17848 if (!NILP (Vshow_trailing_whitespace))
17849 highlight_trailing_whitespace (it->f, it->glyph_row);
17850
17851 /* Prepare for the next line. This line starts horizontally at (X
17852 HPOS) = (0 0). Vertical positions are incremented. As a
17853 convenience for the caller, IT->glyph_row is set to the next
17854 row to be used. */
17855 it->current_x = it->hpos = 0;
17856 it->current_y += row->height;
17857 ++it->vpos;
17858 ++it->glyph_row;
17859 /* The next row should use same value of the reversed_p flag as this
17860 one. set_iterator_to_next decides when it's a new paragraph and
17861 recomputes the value of the flag accordingly. */
17862 it->glyph_row->reversed_p = row->reversed_p;
17863 it->start = row_end;
17864 return row->displays_text_p;
17865 }
17866
17867
17868 \f
17869 /***********************************************************************
17870 Menu Bar
17871 ***********************************************************************/
17872
17873 /* Redisplay the menu bar in the frame for window W.
17874
17875 The menu bar of X frames that don't have X toolkit support is
17876 displayed in a special window W->frame->menu_bar_window.
17877
17878 The menu bar of terminal frames is treated specially as far as
17879 glyph matrices are concerned. Menu bar lines are not part of
17880 windows, so the update is done directly on the frame matrix rows
17881 for the menu bar. */
17882
17883 static void
17884 display_menu_bar (w)
17885 struct window *w;
17886 {
17887 struct frame *f = XFRAME (WINDOW_FRAME (w));
17888 struct it it;
17889 Lisp_Object items;
17890 int i;
17891
17892 /* Don't do all this for graphical frames. */
17893 #ifdef HAVE_NTGUI
17894 if (FRAME_W32_P (f))
17895 return;
17896 #endif
17897 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17898 if (FRAME_X_P (f))
17899 return;
17900 #endif
17901
17902 #ifdef HAVE_NS
17903 if (FRAME_NS_P (f))
17904 return;
17905 #endif /* HAVE_NS */
17906
17907 #ifdef USE_X_TOOLKIT
17908 xassert (!FRAME_WINDOW_P (f));
17909 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17910 it.first_visible_x = 0;
17911 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17912 #else /* not USE_X_TOOLKIT */
17913 if (FRAME_WINDOW_P (f))
17914 {
17915 /* Menu bar lines are displayed in the desired matrix of the
17916 dummy window menu_bar_window. */
17917 struct window *menu_w;
17918 xassert (WINDOWP (f->menu_bar_window));
17919 menu_w = XWINDOW (f->menu_bar_window);
17920 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17921 MENU_FACE_ID);
17922 it.first_visible_x = 0;
17923 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17924 }
17925 else
17926 {
17927 /* This is a TTY frame, i.e. character hpos/vpos are used as
17928 pixel x/y. */
17929 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17930 MENU_FACE_ID);
17931 it.first_visible_x = 0;
17932 it.last_visible_x = FRAME_COLS (f);
17933 }
17934 #endif /* not USE_X_TOOLKIT */
17935
17936 if (! mode_line_inverse_video)
17937 /* Force the menu-bar to be displayed in the default face. */
17938 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17939
17940 /* Clear all rows of the menu bar. */
17941 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17942 {
17943 struct glyph_row *row = it.glyph_row + i;
17944 clear_glyph_row (row);
17945 row->enabled_p = 1;
17946 row->full_width_p = 1;
17947 }
17948
17949 /* Display all items of the menu bar. */
17950 items = FRAME_MENU_BAR_ITEMS (it.f);
17951 for (i = 0; i < XVECTOR (items)->size; i += 4)
17952 {
17953 Lisp_Object string;
17954
17955 /* Stop at nil string. */
17956 string = AREF (items, i + 1);
17957 if (NILP (string))
17958 break;
17959
17960 /* Remember where item was displayed. */
17961 ASET (items, i + 3, make_number (it.hpos));
17962
17963 /* Display the item, pad with one space. */
17964 if (it.current_x < it.last_visible_x)
17965 display_string (NULL, string, Qnil, 0, 0, &it,
17966 SCHARS (string) + 1, 0, 0, -1);
17967 }
17968
17969 /* Fill out the line with spaces. */
17970 if (it.current_x < it.last_visible_x)
17971 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17972
17973 /* Compute the total height of the lines. */
17974 compute_line_metrics (&it);
17975 }
17976
17977
17978 \f
17979 /***********************************************************************
17980 Mode Line
17981 ***********************************************************************/
17982
17983 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17984 FORCE is non-zero, redisplay mode lines unconditionally.
17985 Otherwise, redisplay only mode lines that are garbaged. Value is
17986 the number of windows whose mode lines were redisplayed. */
17987
17988 static int
17989 redisplay_mode_lines (window, force)
17990 Lisp_Object window;
17991 int force;
17992 {
17993 int nwindows = 0;
17994
17995 while (!NILP (window))
17996 {
17997 struct window *w = XWINDOW (window);
17998
17999 if (WINDOWP (w->hchild))
18000 nwindows += redisplay_mode_lines (w->hchild, force);
18001 else if (WINDOWP (w->vchild))
18002 nwindows += redisplay_mode_lines (w->vchild, force);
18003 else if (force
18004 || FRAME_GARBAGED_P (XFRAME (w->frame))
18005 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18006 {
18007 struct text_pos lpoint;
18008 struct buffer *old = current_buffer;
18009
18010 /* Set the window's buffer for the mode line display. */
18011 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18012 set_buffer_internal_1 (XBUFFER (w->buffer));
18013
18014 /* Point refers normally to the selected window. For any
18015 other window, set up appropriate value. */
18016 if (!EQ (window, selected_window))
18017 {
18018 struct text_pos pt;
18019
18020 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18021 if (CHARPOS (pt) < BEGV)
18022 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18023 else if (CHARPOS (pt) > (ZV - 1))
18024 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18025 else
18026 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18027 }
18028
18029 /* Display mode lines. */
18030 clear_glyph_matrix (w->desired_matrix);
18031 if (display_mode_lines (w))
18032 {
18033 ++nwindows;
18034 w->must_be_updated_p = 1;
18035 }
18036
18037 /* Restore old settings. */
18038 set_buffer_internal_1 (old);
18039 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18040 }
18041
18042 window = w->next;
18043 }
18044
18045 return nwindows;
18046 }
18047
18048
18049 /* Display the mode and/or header line of window W. Value is the
18050 sum number of mode lines and header lines displayed. */
18051
18052 static int
18053 display_mode_lines (w)
18054 struct window *w;
18055 {
18056 Lisp_Object old_selected_window, old_selected_frame;
18057 int n = 0;
18058
18059 old_selected_frame = selected_frame;
18060 selected_frame = w->frame;
18061 old_selected_window = selected_window;
18062 XSETWINDOW (selected_window, w);
18063
18064 /* These will be set while the mode line specs are processed. */
18065 line_number_displayed = 0;
18066 w->column_number_displayed = Qnil;
18067
18068 if (WINDOW_WANTS_MODELINE_P (w))
18069 {
18070 struct window *sel_w = XWINDOW (old_selected_window);
18071
18072 /* Select mode line face based on the real selected window. */
18073 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18074 current_buffer->mode_line_format);
18075 ++n;
18076 }
18077
18078 if (WINDOW_WANTS_HEADER_LINE_P (w))
18079 {
18080 display_mode_line (w, HEADER_LINE_FACE_ID,
18081 current_buffer->header_line_format);
18082 ++n;
18083 }
18084
18085 selected_frame = old_selected_frame;
18086 selected_window = old_selected_window;
18087 return n;
18088 }
18089
18090
18091 /* Display mode or header line of window W. FACE_ID specifies which
18092 line to display; it is either MODE_LINE_FACE_ID or
18093 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18094 display. Value is the pixel height of the mode/header line
18095 displayed. */
18096
18097 static int
18098 display_mode_line (w, face_id, format)
18099 struct window *w;
18100 enum face_id face_id;
18101 Lisp_Object format;
18102 {
18103 struct it it;
18104 struct face *face;
18105 int count = SPECPDL_INDEX ();
18106
18107 init_iterator (&it, w, -1, -1, NULL, face_id);
18108 /* Don't extend on a previously drawn mode-line.
18109 This may happen if called from pos_visible_p. */
18110 it.glyph_row->enabled_p = 0;
18111 prepare_desired_row (it.glyph_row);
18112
18113 it.glyph_row->mode_line_p = 1;
18114
18115 if (! mode_line_inverse_video)
18116 /* Force the mode-line to be displayed in the default face. */
18117 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18118
18119 record_unwind_protect (unwind_format_mode_line,
18120 format_mode_line_unwind_data (NULL, Qnil, 0));
18121
18122 mode_line_target = MODE_LINE_DISPLAY;
18123
18124 /* Temporarily make frame's keyboard the current kboard so that
18125 kboard-local variables in the mode_line_format will get the right
18126 values. */
18127 push_kboard (FRAME_KBOARD (it.f));
18128 record_unwind_save_match_data ();
18129 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18130 pop_kboard ();
18131
18132 unbind_to (count, Qnil);
18133
18134 /* Fill up with spaces. */
18135 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18136
18137 compute_line_metrics (&it);
18138 it.glyph_row->full_width_p = 1;
18139 it.glyph_row->continued_p = 0;
18140 it.glyph_row->truncated_on_left_p = 0;
18141 it.glyph_row->truncated_on_right_p = 0;
18142
18143 /* Make a 3D mode-line have a shadow at its right end. */
18144 face = FACE_FROM_ID (it.f, face_id);
18145 extend_face_to_end_of_line (&it);
18146 if (face->box != FACE_NO_BOX)
18147 {
18148 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18149 + it.glyph_row->used[TEXT_AREA] - 1);
18150 last->right_box_line_p = 1;
18151 }
18152
18153 return it.glyph_row->height;
18154 }
18155
18156 /* Move element ELT in LIST to the front of LIST.
18157 Return the updated list. */
18158
18159 static Lisp_Object
18160 move_elt_to_front (elt, list)
18161 Lisp_Object elt, list;
18162 {
18163 register Lisp_Object tail, prev;
18164 register Lisp_Object tem;
18165
18166 tail = list;
18167 prev = Qnil;
18168 while (CONSP (tail))
18169 {
18170 tem = XCAR (tail);
18171
18172 if (EQ (elt, tem))
18173 {
18174 /* Splice out the link TAIL. */
18175 if (NILP (prev))
18176 list = XCDR (tail);
18177 else
18178 Fsetcdr (prev, XCDR (tail));
18179
18180 /* Now make it the first. */
18181 Fsetcdr (tail, list);
18182 return tail;
18183 }
18184 else
18185 prev = tail;
18186 tail = XCDR (tail);
18187 QUIT;
18188 }
18189
18190 /* Not found--return unchanged LIST. */
18191 return list;
18192 }
18193
18194 /* Contribute ELT to the mode line for window IT->w. How it
18195 translates into text depends on its data type.
18196
18197 IT describes the display environment in which we display, as usual.
18198
18199 DEPTH is the depth in recursion. It is used to prevent
18200 infinite recursion here.
18201
18202 FIELD_WIDTH is the number of characters the display of ELT should
18203 occupy in the mode line, and PRECISION is the maximum number of
18204 characters to display from ELT's representation. See
18205 display_string for details.
18206
18207 Returns the hpos of the end of the text generated by ELT.
18208
18209 PROPS is a property list to add to any string we encounter.
18210
18211 If RISKY is nonzero, remove (disregard) any properties in any string
18212 we encounter, and ignore :eval and :propertize.
18213
18214 The global variable `mode_line_target' determines whether the
18215 output is passed to `store_mode_line_noprop',
18216 `store_mode_line_string', or `display_string'. */
18217
18218 static int
18219 display_mode_element (it, depth, field_width, precision, elt, props, risky)
18220 struct it *it;
18221 int depth;
18222 int field_width, precision;
18223 Lisp_Object elt, props;
18224 int risky;
18225 {
18226 int n = 0, field, prec;
18227 int literal = 0;
18228
18229 tail_recurse:
18230 if (depth > 100)
18231 elt = build_string ("*too-deep*");
18232
18233 depth++;
18234
18235 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18236 {
18237 case Lisp_String:
18238 {
18239 /* A string: output it and check for %-constructs within it. */
18240 unsigned char c;
18241 int offset = 0;
18242
18243 if (SCHARS (elt) > 0
18244 && (!NILP (props) || risky))
18245 {
18246 Lisp_Object oprops, aelt;
18247 oprops = Ftext_properties_at (make_number (0), elt);
18248
18249 /* If the starting string's properties are not what
18250 we want, translate the string. Also, if the string
18251 is risky, do that anyway. */
18252
18253 if (NILP (Fequal (props, oprops)) || risky)
18254 {
18255 /* If the starting string has properties,
18256 merge the specified ones onto the existing ones. */
18257 if (! NILP (oprops) && !risky)
18258 {
18259 Lisp_Object tem;
18260
18261 oprops = Fcopy_sequence (oprops);
18262 tem = props;
18263 while (CONSP (tem))
18264 {
18265 oprops = Fplist_put (oprops, XCAR (tem),
18266 XCAR (XCDR (tem)));
18267 tem = XCDR (XCDR (tem));
18268 }
18269 props = oprops;
18270 }
18271
18272 aelt = Fassoc (elt, mode_line_proptrans_alist);
18273 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18274 {
18275 /* AELT is what we want. Move it to the front
18276 without consing. */
18277 elt = XCAR (aelt);
18278 mode_line_proptrans_alist
18279 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18280 }
18281 else
18282 {
18283 Lisp_Object tem;
18284
18285 /* If AELT has the wrong props, it is useless.
18286 so get rid of it. */
18287 if (! NILP (aelt))
18288 mode_line_proptrans_alist
18289 = Fdelq (aelt, mode_line_proptrans_alist);
18290
18291 elt = Fcopy_sequence (elt);
18292 Fset_text_properties (make_number (0), Flength (elt),
18293 props, elt);
18294 /* Add this item to mode_line_proptrans_alist. */
18295 mode_line_proptrans_alist
18296 = Fcons (Fcons (elt, props),
18297 mode_line_proptrans_alist);
18298 /* Truncate mode_line_proptrans_alist
18299 to at most 50 elements. */
18300 tem = Fnthcdr (make_number (50),
18301 mode_line_proptrans_alist);
18302 if (! NILP (tem))
18303 XSETCDR (tem, Qnil);
18304 }
18305 }
18306 }
18307
18308 offset = 0;
18309
18310 if (literal)
18311 {
18312 prec = precision - n;
18313 switch (mode_line_target)
18314 {
18315 case MODE_LINE_NOPROP:
18316 case MODE_LINE_TITLE:
18317 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18318 break;
18319 case MODE_LINE_STRING:
18320 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18321 break;
18322 case MODE_LINE_DISPLAY:
18323 n += display_string (NULL, elt, Qnil, 0, 0, it,
18324 0, prec, 0, STRING_MULTIBYTE (elt));
18325 break;
18326 }
18327
18328 break;
18329 }
18330
18331 /* Handle the non-literal case. */
18332
18333 while ((precision <= 0 || n < precision)
18334 && SREF (elt, offset) != 0
18335 && (mode_line_target != MODE_LINE_DISPLAY
18336 || it->current_x < it->last_visible_x))
18337 {
18338 int last_offset = offset;
18339
18340 /* Advance to end of string or next format specifier. */
18341 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18342 ;
18343
18344 if (offset - 1 != last_offset)
18345 {
18346 int nchars, nbytes;
18347
18348 /* Output to end of string or up to '%'. Field width
18349 is length of string. Don't output more than
18350 PRECISION allows us. */
18351 offset--;
18352
18353 prec = c_string_width (SDATA (elt) + last_offset,
18354 offset - last_offset, precision - n,
18355 &nchars, &nbytes);
18356
18357 switch (mode_line_target)
18358 {
18359 case MODE_LINE_NOPROP:
18360 case MODE_LINE_TITLE:
18361 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18362 break;
18363 case MODE_LINE_STRING:
18364 {
18365 int bytepos = last_offset;
18366 int charpos = string_byte_to_char (elt, bytepos);
18367 int endpos = (precision <= 0
18368 ? string_byte_to_char (elt, offset)
18369 : charpos + nchars);
18370
18371 n += store_mode_line_string (NULL,
18372 Fsubstring (elt, make_number (charpos),
18373 make_number (endpos)),
18374 0, 0, 0, Qnil);
18375 }
18376 break;
18377 case MODE_LINE_DISPLAY:
18378 {
18379 int bytepos = last_offset;
18380 int charpos = string_byte_to_char (elt, bytepos);
18381
18382 if (precision <= 0)
18383 nchars = string_byte_to_char (elt, offset) - charpos;
18384 n += display_string (NULL, elt, Qnil, 0, charpos,
18385 it, 0, nchars, 0,
18386 STRING_MULTIBYTE (elt));
18387 }
18388 break;
18389 }
18390 }
18391 else /* c == '%' */
18392 {
18393 int percent_position = offset;
18394
18395 /* Get the specified minimum width. Zero means
18396 don't pad. */
18397 field = 0;
18398 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18399 field = field * 10 + c - '0';
18400
18401 /* Don't pad beyond the total padding allowed. */
18402 if (field_width - n > 0 && field > field_width - n)
18403 field = field_width - n;
18404
18405 /* Note that either PRECISION <= 0 or N < PRECISION. */
18406 prec = precision - n;
18407
18408 if (c == 'M')
18409 n += display_mode_element (it, depth, field, prec,
18410 Vglobal_mode_string, props,
18411 risky);
18412 else if (c != 0)
18413 {
18414 int multibyte;
18415 int bytepos, charpos;
18416 unsigned char *spec;
18417 Lisp_Object string;
18418
18419 bytepos = percent_position;
18420 charpos = (STRING_MULTIBYTE (elt)
18421 ? string_byte_to_char (elt, bytepos)
18422 : bytepos);
18423 spec = decode_mode_spec (it->w, c, field, prec, &string);
18424 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18425
18426 switch (mode_line_target)
18427 {
18428 case MODE_LINE_NOPROP:
18429 case MODE_LINE_TITLE:
18430 n += store_mode_line_noprop (spec, field, prec);
18431 break;
18432 case MODE_LINE_STRING:
18433 {
18434 int len = strlen (spec);
18435 Lisp_Object tem = make_string (spec, len);
18436 props = Ftext_properties_at (make_number (charpos), elt);
18437 /* Should only keep face property in props */
18438 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18439 }
18440 break;
18441 case MODE_LINE_DISPLAY:
18442 {
18443 int nglyphs_before, nwritten;
18444
18445 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18446 nwritten = display_string (spec, string, elt,
18447 charpos, 0, it,
18448 field, prec, 0,
18449 multibyte);
18450
18451 /* Assign to the glyphs written above the
18452 string where the `%x' came from, position
18453 of the `%'. */
18454 if (nwritten > 0)
18455 {
18456 struct glyph *glyph
18457 = (it->glyph_row->glyphs[TEXT_AREA]
18458 + nglyphs_before);
18459 int i;
18460
18461 for (i = 0; i < nwritten; ++i)
18462 {
18463 glyph[i].object = elt;
18464 glyph[i].charpos = charpos;
18465 }
18466
18467 n += nwritten;
18468 }
18469 }
18470 break;
18471 }
18472 }
18473 else /* c == 0 */
18474 break;
18475 }
18476 }
18477 }
18478 break;
18479
18480 case Lisp_Symbol:
18481 /* A symbol: process the value of the symbol recursively
18482 as if it appeared here directly. Avoid error if symbol void.
18483 Special case: if value of symbol is a string, output the string
18484 literally. */
18485 {
18486 register Lisp_Object tem;
18487
18488 /* If the variable is not marked as risky to set
18489 then its contents are risky to use. */
18490 if (NILP (Fget (elt, Qrisky_local_variable)))
18491 risky = 1;
18492
18493 tem = Fboundp (elt);
18494 if (!NILP (tem))
18495 {
18496 tem = Fsymbol_value (elt);
18497 /* If value is a string, output that string literally:
18498 don't check for % within it. */
18499 if (STRINGP (tem))
18500 literal = 1;
18501
18502 if (!EQ (tem, elt))
18503 {
18504 /* Give up right away for nil or t. */
18505 elt = tem;
18506 goto tail_recurse;
18507 }
18508 }
18509 }
18510 break;
18511
18512 case Lisp_Cons:
18513 {
18514 register Lisp_Object car, tem;
18515
18516 /* A cons cell: five distinct cases.
18517 If first element is :eval or :propertize, do something special.
18518 If first element is a string or a cons, process all the elements
18519 and effectively concatenate them.
18520 If first element is a negative number, truncate displaying cdr to
18521 at most that many characters. If positive, pad (with spaces)
18522 to at least that many characters.
18523 If first element is a symbol, process the cadr or caddr recursively
18524 according to whether the symbol's value is non-nil or nil. */
18525 car = XCAR (elt);
18526 if (EQ (car, QCeval))
18527 {
18528 /* An element of the form (:eval FORM) means evaluate FORM
18529 and use the result as mode line elements. */
18530
18531 if (risky)
18532 break;
18533
18534 if (CONSP (XCDR (elt)))
18535 {
18536 Lisp_Object spec;
18537 spec = safe_eval (XCAR (XCDR (elt)));
18538 n += display_mode_element (it, depth, field_width - n,
18539 precision - n, spec, props,
18540 risky);
18541 }
18542 }
18543 else if (EQ (car, QCpropertize))
18544 {
18545 /* An element of the form (:propertize ELT PROPS...)
18546 means display ELT but applying properties PROPS. */
18547
18548 if (risky)
18549 break;
18550
18551 if (CONSP (XCDR (elt)))
18552 n += display_mode_element (it, depth, field_width - n,
18553 precision - n, XCAR (XCDR (elt)),
18554 XCDR (XCDR (elt)), risky);
18555 }
18556 else if (SYMBOLP (car))
18557 {
18558 tem = Fboundp (car);
18559 elt = XCDR (elt);
18560 if (!CONSP (elt))
18561 goto invalid;
18562 /* elt is now the cdr, and we know it is a cons cell.
18563 Use its car if CAR has a non-nil value. */
18564 if (!NILP (tem))
18565 {
18566 tem = Fsymbol_value (car);
18567 if (!NILP (tem))
18568 {
18569 elt = XCAR (elt);
18570 goto tail_recurse;
18571 }
18572 }
18573 /* Symbol's value is nil (or symbol is unbound)
18574 Get the cddr of the original list
18575 and if possible find the caddr and use that. */
18576 elt = XCDR (elt);
18577 if (NILP (elt))
18578 break;
18579 else if (!CONSP (elt))
18580 goto invalid;
18581 elt = XCAR (elt);
18582 goto tail_recurse;
18583 }
18584 else if (INTEGERP (car))
18585 {
18586 register int lim = XINT (car);
18587 elt = XCDR (elt);
18588 if (lim < 0)
18589 {
18590 /* Negative int means reduce maximum width. */
18591 if (precision <= 0)
18592 precision = -lim;
18593 else
18594 precision = min (precision, -lim);
18595 }
18596 else if (lim > 0)
18597 {
18598 /* Padding specified. Don't let it be more than
18599 current maximum. */
18600 if (precision > 0)
18601 lim = min (precision, lim);
18602
18603 /* If that's more padding than already wanted, queue it.
18604 But don't reduce padding already specified even if
18605 that is beyond the current truncation point. */
18606 field_width = max (lim, field_width);
18607 }
18608 goto tail_recurse;
18609 }
18610 else if (STRINGP (car) || CONSP (car))
18611 {
18612 Lisp_Object halftail = elt;
18613 int len = 0;
18614
18615 while (CONSP (elt)
18616 && (precision <= 0 || n < precision))
18617 {
18618 n += display_mode_element (it, depth,
18619 /* Do padding only after the last
18620 element in the list. */
18621 (! CONSP (XCDR (elt))
18622 ? field_width - n
18623 : 0),
18624 precision - n, XCAR (elt),
18625 props, risky);
18626 elt = XCDR (elt);
18627 len++;
18628 if ((len & 1) == 0)
18629 halftail = XCDR (halftail);
18630 /* Check for cycle. */
18631 if (EQ (halftail, elt))
18632 break;
18633 }
18634 }
18635 }
18636 break;
18637
18638 default:
18639 invalid:
18640 elt = build_string ("*invalid*");
18641 goto tail_recurse;
18642 }
18643
18644 /* Pad to FIELD_WIDTH. */
18645 if (field_width > 0 && n < field_width)
18646 {
18647 switch (mode_line_target)
18648 {
18649 case MODE_LINE_NOPROP:
18650 case MODE_LINE_TITLE:
18651 n += store_mode_line_noprop ("", field_width - n, 0);
18652 break;
18653 case MODE_LINE_STRING:
18654 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18655 break;
18656 case MODE_LINE_DISPLAY:
18657 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18658 0, 0, 0);
18659 break;
18660 }
18661 }
18662
18663 return n;
18664 }
18665
18666 /* Store a mode-line string element in mode_line_string_list.
18667
18668 If STRING is non-null, display that C string. Otherwise, the Lisp
18669 string LISP_STRING is displayed.
18670
18671 FIELD_WIDTH is the minimum number of output glyphs to produce.
18672 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18673 with spaces. FIELD_WIDTH <= 0 means don't pad.
18674
18675 PRECISION is the maximum number of characters to output from
18676 STRING. PRECISION <= 0 means don't truncate the string.
18677
18678 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18679 properties to the string.
18680
18681 PROPS are the properties to add to the string.
18682 The mode_line_string_face face property is always added to the string.
18683 */
18684
18685 static int
18686 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
18687 char *string;
18688 Lisp_Object lisp_string;
18689 int copy_string;
18690 int field_width;
18691 int precision;
18692 Lisp_Object props;
18693 {
18694 int len;
18695 int n = 0;
18696
18697 if (string != NULL)
18698 {
18699 len = strlen (string);
18700 if (precision > 0 && len > precision)
18701 len = precision;
18702 lisp_string = make_string (string, len);
18703 if (NILP (props))
18704 props = mode_line_string_face_prop;
18705 else if (!NILP (mode_line_string_face))
18706 {
18707 Lisp_Object face = Fplist_get (props, Qface);
18708 props = Fcopy_sequence (props);
18709 if (NILP (face))
18710 face = mode_line_string_face;
18711 else
18712 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18713 props = Fplist_put (props, Qface, face);
18714 }
18715 Fadd_text_properties (make_number (0), make_number (len),
18716 props, lisp_string);
18717 }
18718 else
18719 {
18720 len = XFASTINT (Flength (lisp_string));
18721 if (precision > 0 && len > precision)
18722 {
18723 len = precision;
18724 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18725 precision = -1;
18726 }
18727 if (!NILP (mode_line_string_face))
18728 {
18729 Lisp_Object face;
18730 if (NILP (props))
18731 props = Ftext_properties_at (make_number (0), lisp_string);
18732 face = Fplist_get (props, Qface);
18733 if (NILP (face))
18734 face = mode_line_string_face;
18735 else
18736 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18737 props = Fcons (Qface, Fcons (face, Qnil));
18738 if (copy_string)
18739 lisp_string = Fcopy_sequence (lisp_string);
18740 }
18741 if (!NILP (props))
18742 Fadd_text_properties (make_number (0), make_number (len),
18743 props, lisp_string);
18744 }
18745
18746 if (len > 0)
18747 {
18748 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18749 n += len;
18750 }
18751
18752 if (field_width > len)
18753 {
18754 field_width -= len;
18755 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18756 if (!NILP (props))
18757 Fadd_text_properties (make_number (0), make_number (field_width),
18758 props, lisp_string);
18759 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18760 n += field_width;
18761 }
18762
18763 return n;
18764 }
18765
18766
18767 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18768 1, 4, 0,
18769 doc: /* Format a string out of a mode line format specification.
18770 First arg FORMAT specifies the mode line format (see `mode-line-format'
18771 for details) to use.
18772
18773 Optional second arg FACE specifies the face property to put
18774 on all characters for which no face is specified.
18775 The value t means whatever face the window's mode line currently uses
18776 \(either `mode-line' or `mode-line-inactive', depending).
18777 A value of nil means the default is no face property.
18778 If FACE is an integer, the value string has no text properties.
18779
18780 Optional third and fourth args WINDOW and BUFFER specify the window
18781 and buffer to use as the context for the formatting (defaults
18782 are the selected window and the window's buffer). */)
18783 (format, face, window, buffer)
18784 Lisp_Object format, face, window, buffer;
18785 {
18786 struct it it;
18787 int len;
18788 struct window *w;
18789 struct buffer *old_buffer = NULL;
18790 int face_id = -1;
18791 int no_props = INTEGERP (face);
18792 int count = SPECPDL_INDEX ();
18793 Lisp_Object str;
18794 int string_start = 0;
18795
18796 if (NILP (window))
18797 window = selected_window;
18798 CHECK_WINDOW (window);
18799 w = XWINDOW (window);
18800
18801 if (NILP (buffer))
18802 buffer = w->buffer;
18803 CHECK_BUFFER (buffer);
18804
18805 /* Make formatting the modeline a non-op when noninteractive, otherwise
18806 there will be problems later caused by a partially initialized frame. */
18807 if (NILP (format) || noninteractive)
18808 return empty_unibyte_string;
18809
18810 if (no_props)
18811 face = Qnil;
18812
18813 if (!NILP (face))
18814 {
18815 if (EQ (face, Qt))
18816 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18817 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18818 }
18819
18820 if (face_id < 0)
18821 face_id = DEFAULT_FACE_ID;
18822
18823 if (XBUFFER (buffer) != current_buffer)
18824 old_buffer = current_buffer;
18825
18826 /* Save things including mode_line_proptrans_alist,
18827 and set that to nil so that we don't alter the outer value. */
18828 record_unwind_protect (unwind_format_mode_line,
18829 format_mode_line_unwind_data
18830 (old_buffer, selected_window, 1));
18831 mode_line_proptrans_alist = Qnil;
18832
18833 Fselect_window (window, Qt);
18834 if (old_buffer)
18835 set_buffer_internal_1 (XBUFFER (buffer));
18836
18837 init_iterator (&it, w, -1, -1, NULL, face_id);
18838
18839 if (no_props)
18840 {
18841 mode_line_target = MODE_LINE_NOPROP;
18842 mode_line_string_face_prop = Qnil;
18843 mode_line_string_list = Qnil;
18844 string_start = MODE_LINE_NOPROP_LEN (0);
18845 }
18846 else
18847 {
18848 mode_line_target = MODE_LINE_STRING;
18849 mode_line_string_list = Qnil;
18850 mode_line_string_face = face;
18851 mode_line_string_face_prop
18852 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18853 }
18854
18855 push_kboard (FRAME_KBOARD (it.f));
18856 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18857 pop_kboard ();
18858
18859 if (no_props)
18860 {
18861 len = MODE_LINE_NOPROP_LEN (string_start);
18862 str = make_string (mode_line_noprop_buf + string_start, len);
18863 }
18864 else
18865 {
18866 mode_line_string_list = Fnreverse (mode_line_string_list);
18867 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18868 empty_unibyte_string);
18869 }
18870
18871 unbind_to (count, Qnil);
18872 return str;
18873 }
18874
18875 /* Write a null-terminated, right justified decimal representation of
18876 the positive integer D to BUF using a minimal field width WIDTH. */
18877
18878 static void
18879 pint2str (buf, width, d)
18880 register char *buf;
18881 register int width;
18882 register int d;
18883 {
18884 register char *p = buf;
18885
18886 if (d <= 0)
18887 *p++ = '0';
18888 else
18889 {
18890 while (d > 0)
18891 {
18892 *p++ = d % 10 + '0';
18893 d /= 10;
18894 }
18895 }
18896
18897 for (width -= (int) (p - buf); width > 0; --width)
18898 *p++ = ' ';
18899 *p-- = '\0';
18900 while (p > buf)
18901 {
18902 d = *buf;
18903 *buf++ = *p;
18904 *p-- = d;
18905 }
18906 }
18907
18908 /* Write a null-terminated, right justified decimal and "human
18909 readable" representation of the nonnegative integer D to BUF using
18910 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18911
18912 static const char power_letter[] =
18913 {
18914 0, /* not used */
18915 'k', /* kilo */
18916 'M', /* mega */
18917 'G', /* giga */
18918 'T', /* tera */
18919 'P', /* peta */
18920 'E', /* exa */
18921 'Z', /* zetta */
18922 'Y' /* yotta */
18923 };
18924
18925 static void
18926 pint2hrstr (buf, width, d)
18927 char *buf;
18928 int width;
18929 int d;
18930 {
18931 /* We aim to represent the nonnegative integer D as
18932 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18933 int quotient = d;
18934 int remainder = 0;
18935 /* -1 means: do not use TENTHS. */
18936 int tenths = -1;
18937 int exponent = 0;
18938
18939 /* Length of QUOTIENT.TENTHS as a string. */
18940 int length;
18941
18942 char * psuffix;
18943 char * p;
18944
18945 if (1000 <= quotient)
18946 {
18947 /* Scale to the appropriate EXPONENT. */
18948 do
18949 {
18950 remainder = quotient % 1000;
18951 quotient /= 1000;
18952 exponent++;
18953 }
18954 while (1000 <= quotient);
18955
18956 /* Round to nearest and decide whether to use TENTHS or not. */
18957 if (quotient <= 9)
18958 {
18959 tenths = remainder / 100;
18960 if (50 <= remainder % 100)
18961 {
18962 if (tenths < 9)
18963 tenths++;
18964 else
18965 {
18966 quotient++;
18967 if (quotient == 10)
18968 tenths = -1;
18969 else
18970 tenths = 0;
18971 }
18972 }
18973 }
18974 else
18975 if (500 <= remainder)
18976 {
18977 if (quotient < 999)
18978 quotient++;
18979 else
18980 {
18981 quotient = 1;
18982 exponent++;
18983 tenths = 0;
18984 }
18985 }
18986 }
18987
18988 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18989 if (tenths == -1 && quotient <= 99)
18990 if (quotient <= 9)
18991 length = 1;
18992 else
18993 length = 2;
18994 else
18995 length = 3;
18996 p = psuffix = buf + max (width, length);
18997
18998 /* Print EXPONENT. */
18999 if (exponent)
19000 *psuffix++ = power_letter[exponent];
19001 *psuffix = '\0';
19002
19003 /* Print TENTHS. */
19004 if (tenths >= 0)
19005 {
19006 *--p = '0' + tenths;
19007 *--p = '.';
19008 }
19009
19010 /* Print QUOTIENT. */
19011 do
19012 {
19013 int digit = quotient % 10;
19014 *--p = '0' + digit;
19015 }
19016 while ((quotient /= 10) != 0);
19017
19018 /* Print leading spaces. */
19019 while (buf < p)
19020 *--p = ' ';
19021 }
19022
19023 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19024 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19025 type of CODING_SYSTEM. Return updated pointer into BUF. */
19026
19027 static unsigned char invalid_eol_type[] = "(*invalid*)";
19028
19029 static char *
19030 decode_mode_spec_coding (coding_system, buf, eol_flag)
19031 Lisp_Object coding_system;
19032 register char *buf;
19033 int eol_flag;
19034 {
19035 Lisp_Object val;
19036 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19037 const unsigned char *eol_str;
19038 int eol_str_len;
19039 /* The EOL conversion we are using. */
19040 Lisp_Object eoltype;
19041
19042 val = CODING_SYSTEM_SPEC (coding_system);
19043 eoltype = Qnil;
19044
19045 if (!VECTORP (val)) /* Not yet decided. */
19046 {
19047 if (multibyte)
19048 *buf++ = '-';
19049 if (eol_flag)
19050 eoltype = eol_mnemonic_undecided;
19051 /* Don't mention EOL conversion if it isn't decided. */
19052 }
19053 else
19054 {
19055 Lisp_Object attrs;
19056 Lisp_Object eolvalue;
19057
19058 attrs = AREF (val, 0);
19059 eolvalue = AREF (val, 2);
19060
19061 if (multibyte)
19062 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19063
19064 if (eol_flag)
19065 {
19066 /* The EOL conversion that is normal on this system. */
19067
19068 if (NILP (eolvalue)) /* Not yet decided. */
19069 eoltype = eol_mnemonic_undecided;
19070 else if (VECTORP (eolvalue)) /* Not yet decided. */
19071 eoltype = eol_mnemonic_undecided;
19072 else /* eolvalue is Qunix, Qdos, or Qmac. */
19073 eoltype = (EQ (eolvalue, Qunix)
19074 ? eol_mnemonic_unix
19075 : (EQ (eolvalue, Qdos) == 1
19076 ? eol_mnemonic_dos : eol_mnemonic_mac));
19077 }
19078 }
19079
19080 if (eol_flag)
19081 {
19082 /* Mention the EOL conversion if it is not the usual one. */
19083 if (STRINGP (eoltype))
19084 {
19085 eol_str = SDATA (eoltype);
19086 eol_str_len = SBYTES (eoltype);
19087 }
19088 else if (CHARACTERP (eoltype))
19089 {
19090 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19091 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19092 eol_str = tmp;
19093 }
19094 else
19095 {
19096 eol_str = invalid_eol_type;
19097 eol_str_len = sizeof (invalid_eol_type) - 1;
19098 }
19099 bcopy (eol_str, buf, eol_str_len);
19100 buf += eol_str_len;
19101 }
19102
19103 return buf;
19104 }
19105
19106 /* Return a string for the output of a mode line %-spec for window W,
19107 generated by character C. PRECISION >= 0 means don't return a
19108 string longer than that value. FIELD_WIDTH > 0 means pad the
19109 string returned with spaces to that value. Return a Lisp string in
19110 *STRING if the resulting string is taken from that Lisp string.
19111
19112 Note we operate on the current buffer for most purposes,
19113 the exception being w->base_line_pos. */
19114
19115 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19116
19117 static char *
19118 decode_mode_spec (w, c, field_width, precision, string)
19119 struct window *w;
19120 register int c;
19121 int field_width, precision;
19122 Lisp_Object *string;
19123 {
19124 Lisp_Object obj;
19125 struct frame *f = XFRAME (WINDOW_FRAME (w));
19126 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19127 struct buffer *b = current_buffer;
19128
19129 obj = Qnil;
19130 *string = Qnil;
19131
19132 switch (c)
19133 {
19134 case '*':
19135 if (!NILP (b->read_only))
19136 return "%";
19137 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19138 return "*";
19139 return "-";
19140
19141 case '+':
19142 /* This differs from %* only for a modified read-only buffer. */
19143 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19144 return "*";
19145 if (!NILP (b->read_only))
19146 return "%";
19147 return "-";
19148
19149 case '&':
19150 /* This differs from %* in ignoring read-only-ness. */
19151 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19152 return "*";
19153 return "-";
19154
19155 case '%':
19156 return "%";
19157
19158 case '[':
19159 {
19160 int i;
19161 char *p;
19162
19163 if (command_loop_level > 5)
19164 return "[[[... ";
19165 p = decode_mode_spec_buf;
19166 for (i = 0; i < command_loop_level; i++)
19167 *p++ = '[';
19168 *p = 0;
19169 return decode_mode_spec_buf;
19170 }
19171
19172 case ']':
19173 {
19174 int i;
19175 char *p;
19176
19177 if (command_loop_level > 5)
19178 return " ...]]]";
19179 p = decode_mode_spec_buf;
19180 for (i = 0; i < command_loop_level; i++)
19181 *p++ = ']';
19182 *p = 0;
19183 return decode_mode_spec_buf;
19184 }
19185
19186 case '-':
19187 {
19188 register int i;
19189
19190 /* Let lots_of_dashes be a string of infinite length. */
19191 if (mode_line_target == MODE_LINE_NOPROP ||
19192 mode_line_target == MODE_LINE_STRING)
19193 return "--";
19194 if (field_width <= 0
19195 || field_width > sizeof (lots_of_dashes))
19196 {
19197 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19198 decode_mode_spec_buf[i] = '-';
19199 decode_mode_spec_buf[i] = '\0';
19200 return decode_mode_spec_buf;
19201 }
19202 else
19203 return lots_of_dashes;
19204 }
19205
19206 case 'b':
19207 obj = b->name;
19208 break;
19209
19210 case 'c':
19211 /* %c and %l are ignored in `frame-title-format'.
19212 (In redisplay_internal, the frame title is drawn _before_ the
19213 windows are updated, so the stuff which depends on actual
19214 window contents (such as %l) may fail to render properly, or
19215 even crash emacs.) */
19216 if (mode_line_target == MODE_LINE_TITLE)
19217 return "";
19218 else
19219 {
19220 int col = (int) current_column (); /* iftc */
19221 w->column_number_displayed = make_number (col);
19222 pint2str (decode_mode_spec_buf, field_width, col);
19223 return decode_mode_spec_buf;
19224 }
19225
19226 case 'e':
19227 #ifndef SYSTEM_MALLOC
19228 {
19229 if (NILP (Vmemory_full))
19230 return "";
19231 else
19232 return "!MEM FULL! ";
19233 }
19234 #else
19235 return "";
19236 #endif
19237
19238 case 'F':
19239 /* %F displays the frame name. */
19240 if (!NILP (f->title))
19241 return (char *) SDATA (f->title);
19242 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19243 return (char *) SDATA (f->name);
19244 return "Emacs";
19245
19246 case 'f':
19247 obj = b->filename;
19248 break;
19249
19250 case 'i':
19251 {
19252 int size = ZV - BEGV;
19253 pint2str (decode_mode_spec_buf, field_width, size);
19254 return decode_mode_spec_buf;
19255 }
19256
19257 case 'I':
19258 {
19259 int size = ZV - BEGV;
19260 pint2hrstr (decode_mode_spec_buf, field_width, size);
19261 return decode_mode_spec_buf;
19262 }
19263
19264 case 'l':
19265 {
19266 int startpos, startpos_byte, line, linepos, linepos_byte;
19267 int topline, nlines, junk, height;
19268
19269 /* %c and %l are ignored in `frame-title-format'. */
19270 if (mode_line_target == MODE_LINE_TITLE)
19271 return "";
19272
19273 startpos = XMARKER (w->start)->charpos;
19274 startpos_byte = marker_byte_position (w->start);
19275 height = WINDOW_TOTAL_LINES (w);
19276
19277 /* If we decided that this buffer isn't suitable for line numbers,
19278 don't forget that too fast. */
19279 if (EQ (w->base_line_pos, w->buffer))
19280 goto no_value;
19281 /* But do forget it, if the window shows a different buffer now. */
19282 else if (BUFFERP (w->base_line_pos))
19283 w->base_line_pos = Qnil;
19284
19285 /* If the buffer is very big, don't waste time. */
19286 if (INTEGERP (Vline_number_display_limit)
19287 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19288 {
19289 w->base_line_pos = Qnil;
19290 w->base_line_number = Qnil;
19291 goto no_value;
19292 }
19293
19294 if (INTEGERP (w->base_line_number)
19295 && INTEGERP (w->base_line_pos)
19296 && XFASTINT (w->base_line_pos) <= startpos)
19297 {
19298 line = XFASTINT (w->base_line_number);
19299 linepos = XFASTINT (w->base_line_pos);
19300 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19301 }
19302 else
19303 {
19304 line = 1;
19305 linepos = BUF_BEGV (b);
19306 linepos_byte = BUF_BEGV_BYTE (b);
19307 }
19308
19309 /* Count lines from base line to window start position. */
19310 nlines = display_count_lines (linepos, linepos_byte,
19311 startpos_byte,
19312 startpos, &junk);
19313
19314 topline = nlines + line;
19315
19316 /* Determine a new base line, if the old one is too close
19317 or too far away, or if we did not have one.
19318 "Too close" means it's plausible a scroll-down would
19319 go back past it. */
19320 if (startpos == BUF_BEGV (b))
19321 {
19322 w->base_line_number = make_number (topline);
19323 w->base_line_pos = make_number (BUF_BEGV (b));
19324 }
19325 else if (nlines < height + 25 || nlines > height * 3 + 50
19326 || linepos == BUF_BEGV (b))
19327 {
19328 int limit = BUF_BEGV (b);
19329 int limit_byte = BUF_BEGV_BYTE (b);
19330 int position;
19331 int distance = (height * 2 + 30) * line_number_display_limit_width;
19332
19333 if (startpos - distance > limit)
19334 {
19335 limit = startpos - distance;
19336 limit_byte = CHAR_TO_BYTE (limit);
19337 }
19338
19339 nlines = display_count_lines (startpos, startpos_byte,
19340 limit_byte,
19341 - (height * 2 + 30),
19342 &position);
19343 /* If we couldn't find the lines we wanted within
19344 line_number_display_limit_width chars per line,
19345 give up on line numbers for this window. */
19346 if (position == limit_byte && limit == startpos - distance)
19347 {
19348 w->base_line_pos = w->buffer;
19349 w->base_line_number = Qnil;
19350 goto no_value;
19351 }
19352
19353 w->base_line_number = make_number (topline - nlines);
19354 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19355 }
19356
19357 /* Now count lines from the start pos to point. */
19358 nlines = display_count_lines (startpos, startpos_byte,
19359 PT_BYTE, PT, &junk);
19360
19361 /* Record that we did display the line number. */
19362 line_number_displayed = 1;
19363
19364 /* Make the string to show. */
19365 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19366 return decode_mode_spec_buf;
19367 no_value:
19368 {
19369 char* p = decode_mode_spec_buf;
19370 int pad = field_width - 2;
19371 while (pad-- > 0)
19372 *p++ = ' ';
19373 *p++ = '?';
19374 *p++ = '?';
19375 *p = '\0';
19376 return decode_mode_spec_buf;
19377 }
19378 }
19379 break;
19380
19381 case 'm':
19382 obj = b->mode_name;
19383 break;
19384
19385 case 'n':
19386 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19387 return " Narrow";
19388 break;
19389
19390 case 'p':
19391 {
19392 int pos = marker_position (w->start);
19393 int total = BUF_ZV (b) - BUF_BEGV (b);
19394
19395 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19396 {
19397 if (pos <= BUF_BEGV (b))
19398 return "All";
19399 else
19400 return "Bottom";
19401 }
19402 else if (pos <= BUF_BEGV (b))
19403 return "Top";
19404 else
19405 {
19406 if (total > 1000000)
19407 /* Do it differently for a large value, to avoid overflow. */
19408 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19409 else
19410 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19411 /* We can't normally display a 3-digit number,
19412 so get us a 2-digit number that is close. */
19413 if (total == 100)
19414 total = 99;
19415 sprintf (decode_mode_spec_buf, "%2d%%", total);
19416 return decode_mode_spec_buf;
19417 }
19418 }
19419
19420 /* Display percentage of size above the bottom of the screen. */
19421 case 'P':
19422 {
19423 int toppos = marker_position (w->start);
19424 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19425 int total = BUF_ZV (b) - BUF_BEGV (b);
19426
19427 if (botpos >= BUF_ZV (b))
19428 {
19429 if (toppos <= BUF_BEGV (b))
19430 return "All";
19431 else
19432 return "Bottom";
19433 }
19434 else
19435 {
19436 if (total > 1000000)
19437 /* Do it differently for a large value, to avoid overflow. */
19438 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19439 else
19440 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19441 /* We can't normally display a 3-digit number,
19442 so get us a 2-digit number that is close. */
19443 if (total == 100)
19444 total = 99;
19445 if (toppos <= BUF_BEGV (b))
19446 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
19447 else
19448 sprintf (decode_mode_spec_buf, "%2d%%", total);
19449 return decode_mode_spec_buf;
19450 }
19451 }
19452
19453 case 's':
19454 /* status of process */
19455 obj = Fget_buffer_process (Fcurrent_buffer ());
19456 if (NILP (obj))
19457 return "no process";
19458 #ifdef subprocesses
19459 obj = Fsymbol_name (Fprocess_status (obj));
19460 #endif
19461 break;
19462
19463 case '@':
19464 {
19465 int count = inhibit_garbage_collection ();
19466 Lisp_Object val = call1 (intern ("file-remote-p"),
19467 current_buffer->directory);
19468 unbind_to (count, Qnil);
19469
19470 if (NILP (val))
19471 return "-";
19472 else
19473 return "@";
19474 }
19475
19476 case 't': /* indicate TEXT or BINARY */
19477 #ifdef MODE_LINE_BINARY_TEXT
19478 return MODE_LINE_BINARY_TEXT (b);
19479 #else
19480 return "T";
19481 #endif
19482
19483 case 'z':
19484 /* coding-system (not including end-of-line format) */
19485 case 'Z':
19486 /* coding-system (including end-of-line type) */
19487 {
19488 int eol_flag = (c == 'Z');
19489 char *p = decode_mode_spec_buf;
19490
19491 if (! FRAME_WINDOW_P (f))
19492 {
19493 /* No need to mention EOL here--the terminal never needs
19494 to do EOL conversion. */
19495 p = decode_mode_spec_coding (CODING_ID_NAME
19496 (FRAME_KEYBOARD_CODING (f)->id),
19497 p, 0);
19498 p = decode_mode_spec_coding (CODING_ID_NAME
19499 (FRAME_TERMINAL_CODING (f)->id),
19500 p, 0);
19501 }
19502 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19503 p, eol_flag);
19504
19505 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19506 #ifdef subprocesses
19507 obj = Fget_buffer_process (Fcurrent_buffer ());
19508 if (PROCESSP (obj))
19509 {
19510 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19511 p, eol_flag);
19512 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19513 p, eol_flag);
19514 }
19515 #endif /* subprocesses */
19516 #endif /* 0 */
19517 *p = 0;
19518 return decode_mode_spec_buf;
19519 }
19520 }
19521
19522 if (STRINGP (obj))
19523 {
19524 *string = obj;
19525 return (char *) SDATA (obj);
19526 }
19527 else
19528 return "";
19529 }
19530
19531
19532 /* Count up to COUNT lines starting from START / START_BYTE.
19533 But don't go beyond LIMIT_BYTE.
19534 Return the number of lines thus found (always nonnegative).
19535
19536 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19537
19538 static int
19539 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
19540 int start, start_byte, limit_byte, count;
19541 int *byte_pos_ptr;
19542 {
19543 register unsigned char *cursor;
19544 unsigned char *base;
19545
19546 register int ceiling;
19547 register unsigned char *ceiling_addr;
19548 int orig_count = count;
19549
19550 /* If we are not in selective display mode,
19551 check only for newlines. */
19552 int selective_display = (!NILP (current_buffer->selective_display)
19553 && !INTEGERP (current_buffer->selective_display));
19554
19555 if (count > 0)
19556 {
19557 while (start_byte < limit_byte)
19558 {
19559 ceiling = BUFFER_CEILING_OF (start_byte);
19560 ceiling = min (limit_byte - 1, ceiling);
19561 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19562 base = (cursor = BYTE_POS_ADDR (start_byte));
19563 while (1)
19564 {
19565 if (selective_display)
19566 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19567 ;
19568 else
19569 while (*cursor != '\n' && ++cursor != ceiling_addr)
19570 ;
19571
19572 if (cursor != ceiling_addr)
19573 {
19574 if (--count == 0)
19575 {
19576 start_byte += cursor - base + 1;
19577 *byte_pos_ptr = start_byte;
19578 return orig_count;
19579 }
19580 else
19581 if (++cursor == ceiling_addr)
19582 break;
19583 }
19584 else
19585 break;
19586 }
19587 start_byte += cursor - base;
19588 }
19589 }
19590 else
19591 {
19592 while (start_byte > limit_byte)
19593 {
19594 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19595 ceiling = max (limit_byte, ceiling);
19596 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19597 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19598 while (1)
19599 {
19600 if (selective_display)
19601 while (--cursor != ceiling_addr
19602 && *cursor != '\n' && *cursor != 015)
19603 ;
19604 else
19605 while (--cursor != ceiling_addr && *cursor != '\n')
19606 ;
19607
19608 if (cursor != ceiling_addr)
19609 {
19610 if (++count == 0)
19611 {
19612 start_byte += cursor - base + 1;
19613 *byte_pos_ptr = start_byte;
19614 /* When scanning backwards, we should
19615 not count the newline posterior to which we stop. */
19616 return - orig_count - 1;
19617 }
19618 }
19619 else
19620 break;
19621 }
19622 /* Here we add 1 to compensate for the last decrement
19623 of CURSOR, which took it past the valid range. */
19624 start_byte += cursor - base + 1;
19625 }
19626 }
19627
19628 *byte_pos_ptr = limit_byte;
19629
19630 if (count < 0)
19631 return - orig_count + count;
19632 return orig_count - count;
19633
19634 }
19635
19636
19637 \f
19638 /***********************************************************************
19639 Displaying strings
19640 ***********************************************************************/
19641
19642 /* Display a NUL-terminated string, starting with index START.
19643
19644 If STRING is non-null, display that C string. Otherwise, the Lisp
19645 string LISP_STRING is displayed. There's a case that STRING is
19646 non-null and LISP_STRING is not nil. It means STRING is a string
19647 data of LISP_STRING. In that case, we display LISP_STRING while
19648 ignoring its text properties.
19649
19650 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19651 FACE_STRING. Display STRING or LISP_STRING with the face at
19652 FACE_STRING_POS in FACE_STRING:
19653
19654 Display the string in the environment given by IT, but use the
19655 standard display table, temporarily.
19656
19657 FIELD_WIDTH is the minimum number of output glyphs to produce.
19658 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19659 with spaces. If STRING has more characters, more than FIELD_WIDTH
19660 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19661
19662 PRECISION is the maximum number of characters to output from
19663 STRING. PRECISION < 0 means don't truncate the string.
19664
19665 This is roughly equivalent to printf format specifiers:
19666
19667 FIELD_WIDTH PRECISION PRINTF
19668 ----------------------------------------
19669 -1 -1 %s
19670 -1 10 %.10s
19671 10 -1 %10s
19672 20 10 %20.10s
19673
19674 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19675 display them, and < 0 means obey the current buffer's value of
19676 enable_multibyte_characters.
19677
19678 Value is the number of columns displayed. */
19679
19680 static int
19681 display_string (string, lisp_string, face_string, face_string_pos,
19682 start, it, field_width, precision, max_x, multibyte)
19683 unsigned char *string;
19684 Lisp_Object lisp_string;
19685 Lisp_Object face_string;
19686 EMACS_INT face_string_pos;
19687 EMACS_INT start;
19688 struct it *it;
19689 int field_width, precision, max_x;
19690 int multibyte;
19691 {
19692 int hpos_at_start = it->hpos;
19693 int saved_face_id = it->face_id;
19694 struct glyph_row *row = it->glyph_row;
19695
19696 /* Initialize the iterator IT for iteration over STRING beginning
19697 with index START. */
19698 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19699 precision, field_width, multibyte);
19700 if (string && STRINGP (lisp_string))
19701 /* LISP_STRING is the one returned by decode_mode_spec. We should
19702 ignore its text properties. */
19703 it->stop_charpos = -1;
19704
19705 /* If displaying STRING, set up the face of the iterator
19706 from LISP_STRING, if that's given. */
19707 if (STRINGP (face_string))
19708 {
19709 EMACS_INT endptr;
19710 struct face *face;
19711
19712 it->face_id
19713 = face_at_string_position (it->w, face_string, face_string_pos,
19714 0, it->region_beg_charpos,
19715 it->region_end_charpos,
19716 &endptr, it->base_face_id, 0);
19717 face = FACE_FROM_ID (it->f, it->face_id);
19718 it->face_box_p = face->box != FACE_NO_BOX;
19719 }
19720
19721 /* Set max_x to the maximum allowed X position. Don't let it go
19722 beyond the right edge of the window. */
19723 if (max_x <= 0)
19724 max_x = it->last_visible_x;
19725 else
19726 max_x = min (max_x, it->last_visible_x);
19727
19728 /* Skip over display elements that are not visible. because IT->w is
19729 hscrolled. */
19730 if (it->current_x < it->first_visible_x)
19731 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19732 MOVE_TO_POS | MOVE_TO_X);
19733
19734 row->ascent = it->max_ascent;
19735 row->height = it->max_ascent + it->max_descent;
19736 row->phys_ascent = it->max_phys_ascent;
19737 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19738 row->extra_line_spacing = it->max_extra_line_spacing;
19739
19740 /* This condition is for the case that we are called with current_x
19741 past last_visible_x. */
19742 while (it->current_x < max_x)
19743 {
19744 int x_before, x, n_glyphs_before, i, nglyphs;
19745
19746 /* Get the next display element. */
19747 if (!get_next_display_element (it))
19748 break;
19749
19750 /* Produce glyphs. */
19751 x_before = it->current_x;
19752 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19753 PRODUCE_GLYPHS (it);
19754
19755 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19756 i = 0;
19757 x = x_before;
19758 while (i < nglyphs)
19759 {
19760 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19761
19762 if (it->line_wrap != TRUNCATE
19763 && x + glyph->pixel_width > max_x)
19764 {
19765 /* End of continued line or max_x reached. */
19766 if (CHAR_GLYPH_PADDING_P (*glyph))
19767 {
19768 /* A wide character is unbreakable. */
19769 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19770 it->current_x = x_before;
19771 }
19772 else
19773 {
19774 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19775 it->current_x = x;
19776 }
19777 break;
19778 }
19779 else if (x + glyph->pixel_width >= it->first_visible_x)
19780 {
19781 /* Glyph is at least partially visible. */
19782 ++it->hpos;
19783 if (x < it->first_visible_x)
19784 it->glyph_row->x = x - it->first_visible_x;
19785 }
19786 else
19787 {
19788 /* Glyph is off the left margin of the display area.
19789 Should not happen. */
19790 abort ();
19791 }
19792
19793 row->ascent = max (row->ascent, it->max_ascent);
19794 row->height = max (row->height, it->max_ascent + it->max_descent);
19795 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19796 row->phys_height = max (row->phys_height,
19797 it->max_phys_ascent + it->max_phys_descent);
19798 row->extra_line_spacing = max (row->extra_line_spacing,
19799 it->max_extra_line_spacing);
19800 x += glyph->pixel_width;
19801 ++i;
19802 }
19803
19804 /* Stop if max_x reached. */
19805 if (i < nglyphs)
19806 break;
19807
19808 /* Stop at line ends. */
19809 if (ITERATOR_AT_END_OF_LINE_P (it))
19810 {
19811 it->continuation_lines_width = 0;
19812 break;
19813 }
19814
19815 set_iterator_to_next (it, 1);
19816
19817 /* Stop if truncating at the right edge. */
19818 if (it->line_wrap == TRUNCATE
19819 && it->current_x >= it->last_visible_x)
19820 {
19821 /* Add truncation mark, but don't do it if the line is
19822 truncated at a padding space. */
19823 if (IT_CHARPOS (*it) < it->string_nchars)
19824 {
19825 if (!FRAME_WINDOW_P (it->f))
19826 {
19827 int i, n;
19828
19829 if (it->current_x > it->last_visible_x)
19830 {
19831 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19832 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19833 break;
19834 for (n = row->used[TEXT_AREA]; i < n; ++i)
19835 {
19836 row->used[TEXT_AREA] = i;
19837 produce_special_glyphs (it, IT_TRUNCATION);
19838 }
19839 }
19840 produce_special_glyphs (it, IT_TRUNCATION);
19841 }
19842 it->glyph_row->truncated_on_right_p = 1;
19843 }
19844 break;
19845 }
19846 }
19847
19848 /* Maybe insert a truncation at the left. */
19849 if (it->first_visible_x
19850 && IT_CHARPOS (*it) > 0)
19851 {
19852 if (!FRAME_WINDOW_P (it->f))
19853 insert_left_trunc_glyphs (it);
19854 it->glyph_row->truncated_on_left_p = 1;
19855 }
19856
19857 it->face_id = saved_face_id;
19858
19859 /* Value is number of columns displayed. */
19860 return it->hpos - hpos_at_start;
19861 }
19862
19863
19864 \f
19865 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19866 appears as an element of LIST or as the car of an element of LIST.
19867 If PROPVAL is a list, compare each element against LIST in that
19868 way, and return 1/2 if any element of PROPVAL is found in LIST.
19869 Otherwise return 0. This function cannot quit.
19870 The return value is 2 if the text is invisible but with an ellipsis
19871 and 1 if it's invisible and without an ellipsis. */
19872
19873 int
19874 invisible_p (propval, list)
19875 register Lisp_Object propval;
19876 Lisp_Object list;
19877 {
19878 register Lisp_Object tail, proptail;
19879
19880 for (tail = list; CONSP (tail); tail = XCDR (tail))
19881 {
19882 register Lisp_Object tem;
19883 tem = XCAR (tail);
19884 if (EQ (propval, tem))
19885 return 1;
19886 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19887 return NILP (XCDR (tem)) ? 1 : 2;
19888 }
19889
19890 if (CONSP (propval))
19891 {
19892 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19893 {
19894 Lisp_Object propelt;
19895 propelt = XCAR (proptail);
19896 for (tail = list; CONSP (tail); tail = XCDR (tail))
19897 {
19898 register Lisp_Object tem;
19899 tem = XCAR (tail);
19900 if (EQ (propelt, tem))
19901 return 1;
19902 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19903 return NILP (XCDR (tem)) ? 1 : 2;
19904 }
19905 }
19906 }
19907
19908 return 0;
19909 }
19910
19911 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19912 doc: /* Non-nil if the property makes the text invisible.
19913 POS-OR-PROP can be a marker or number, in which case it is taken to be
19914 a position in the current buffer and the value of the `invisible' property
19915 is checked; or it can be some other value, which is then presumed to be the
19916 value of the `invisible' property of the text of interest.
19917 The non-nil value returned can be t for truly invisible text or something
19918 else if the text is replaced by an ellipsis. */)
19919 (pos_or_prop)
19920 Lisp_Object pos_or_prop;
19921 {
19922 Lisp_Object prop
19923 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19924 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19925 : pos_or_prop);
19926 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19927 return (invis == 0 ? Qnil
19928 : invis == 1 ? Qt
19929 : make_number (invis));
19930 }
19931
19932 /* Calculate a width or height in pixels from a specification using
19933 the following elements:
19934
19935 SPEC ::=
19936 NUM - a (fractional) multiple of the default font width/height
19937 (NUM) - specifies exactly NUM pixels
19938 UNIT - a fixed number of pixels, see below.
19939 ELEMENT - size of a display element in pixels, see below.
19940 (NUM . SPEC) - equals NUM * SPEC
19941 (+ SPEC SPEC ...) - add pixel values
19942 (- SPEC SPEC ...) - subtract pixel values
19943 (- SPEC) - negate pixel value
19944
19945 NUM ::=
19946 INT or FLOAT - a number constant
19947 SYMBOL - use symbol's (buffer local) variable binding.
19948
19949 UNIT ::=
19950 in - pixels per inch *)
19951 mm - pixels per 1/1000 meter *)
19952 cm - pixels per 1/100 meter *)
19953 width - width of current font in pixels.
19954 height - height of current font in pixels.
19955
19956 *) using the ratio(s) defined in display-pixels-per-inch.
19957
19958 ELEMENT ::=
19959
19960 left-fringe - left fringe width in pixels
19961 right-fringe - right fringe width in pixels
19962
19963 left-margin - left margin width in pixels
19964 right-margin - right margin width in pixels
19965
19966 scroll-bar - scroll-bar area width in pixels
19967
19968 Examples:
19969
19970 Pixels corresponding to 5 inches:
19971 (5 . in)
19972
19973 Total width of non-text areas on left side of window (if scroll-bar is on left):
19974 '(space :width (+ left-fringe left-margin scroll-bar))
19975
19976 Align to first text column (in header line):
19977 '(space :align-to 0)
19978
19979 Align to middle of text area minus half the width of variable `my-image'
19980 containing a loaded image:
19981 '(space :align-to (0.5 . (- text my-image)))
19982
19983 Width of left margin minus width of 1 character in the default font:
19984 '(space :width (- left-margin 1))
19985
19986 Width of left margin minus width of 2 characters in the current font:
19987 '(space :width (- left-margin (2 . width)))
19988
19989 Center 1 character over left-margin (in header line):
19990 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19991
19992 Different ways to express width of left fringe plus left margin minus one pixel:
19993 '(space :width (- (+ left-fringe left-margin) (1)))
19994 '(space :width (+ left-fringe left-margin (- (1))))
19995 '(space :width (+ left-fringe left-margin (-1)))
19996
19997 */
19998
19999 #define NUMVAL(X) \
20000 ((INTEGERP (X) || FLOATP (X)) \
20001 ? XFLOATINT (X) \
20002 : - 1)
20003
20004 int
20005 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
20006 double *res;
20007 struct it *it;
20008 Lisp_Object prop;
20009 struct font *font;
20010 int width_p, *align_to;
20011 {
20012 double pixels;
20013
20014 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20015 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20016
20017 if (NILP (prop))
20018 return OK_PIXELS (0);
20019
20020 xassert (FRAME_LIVE_P (it->f));
20021
20022 if (SYMBOLP (prop))
20023 {
20024 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20025 {
20026 char *unit = SDATA (SYMBOL_NAME (prop));
20027
20028 if (unit[0] == 'i' && unit[1] == 'n')
20029 pixels = 1.0;
20030 else if (unit[0] == 'm' && unit[1] == 'm')
20031 pixels = 25.4;
20032 else if (unit[0] == 'c' && unit[1] == 'm')
20033 pixels = 2.54;
20034 else
20035 pixels = 0;
20036 if (pixels > 0)
20037 {
20038 double ppi;
20039 #ifdef HAVE_WINDOW_SYSTEM
20040 if (FRAME_WINDOW_P (it->f)
20041 && (ppi = (width_p
20042 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20043 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20044 ppi > 0))
20045 return OK_PIXELS (ppi / pixels);
20046 #endif
20047
20048 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20049 || (CONSP (Vdisplay_pixels_per_inch)
20050 && (ppi = (width_p
20051 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20052 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20053 ppi > 0)))
20054 return OK_PIXELS (ppi / pixels);
20055
20056 return 0;
20057 }
20058 }
20059
20060 #ifdef HAVE_WINDOW_SYSTEM
20061 if (EQ (prop, Qheight))
20062 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20063 if (EQ (prop, Qwidth))
20064 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20065 #else
20066 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20067 return OK_PIXELS (1);
20068 #endif
20069
20070 if (EQ (prop, Qtext))
20071 return OK_PIXELS (width_p
20072 ? window_box_width (it->w, TEXT_AREA)
20073 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20074
20075 if (align_to && *align_to < 0)
20076 {
20077 *res = 0;
20078 if (EQ (prop, Qleft))
20079 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20080 if (EQ (prop, Qright))
20081 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20082 if (EQ (prop, Qcenter))
20083 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20084 + window_box_width (it->w, TEXT_AREA) / 2);
20085 if (EQ (prop, Qleft_fringe))
20086 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20087 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20088 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20089 if (EQ (prop, Qright_fringe))
20090 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20091 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20092 : window_box_right_offset (it->w, TEXT_AREA));
20093 if (EQ (prop, Qleft_margin))
20094 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20095 if (EQ (prop, Qright_margin))
20096 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20097 if (EQ (prop, Qscroll_bar))
20098 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20099 ? 0
20100 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20101 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20102 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20103 : 0)));
20104 }
20105 else
20106 {
20107 if (EQ (prop, Qleft_fringe))
20108 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20109 if (EQ (prop, Qright_fringe))
20110 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20111 if (EQ (prop, Qleft_margin))
20112 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20113 if (EQ (prop, Qright_margin))
20114 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20115 if (EQ (prop, Qscroll_bar))
20116 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20117 }
20118
20119 prop = Fbuffer_local_value (prop, it->w->buffer);
20120 }
20121
20122 if (INTEGERP (prop) || FLOATP (prop))
20123 {
20124 int base_unit = (width_p
20125 ? FRAME_COLUMN_WIDTH (it->f)
20126 : FRAME_LINE_HEIGHT (it->f));
20127 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20128 }
20129
20130 if (CONSP (prop))
20131 {
20132 Lisp_Object car = XCAR (prop);
20133 Lisp_Object cdr = XCDR (prop);
20134
20135 if (SYMBOLP (car))
20136 {
20137 #ifdef HAVE_WINDOW_SYSTEM
20138 if (FRAME_WINDOW_P (it->f)
20139 && valid_image_p (prop))
20140 {
20141 int id = lookup_image (it->f, prop);
20142 struct image *img = IMAGE_FROM_ID (it->f, id);
20143
20144 return OK_PIXELS (width_p ? img->width : img->height);
20145 }
20146 #endif
20147 if (EQ (car, Qplus) || EQ (car, Qminus))
20148 {
20149 int first = 1;
20150 double px;
20151
20152 pixels = 0;
20153 while (CONSP (cdr))
20154 {
20155 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20156 font, width_p, align_to))
20157 return 0;
20158 if (first)
20159 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20160 else
20161 pixels += px;
20162 cdr = XCDR (cdr);
20163 }
20164 if (EQ (car, Qminus))
20165 pixels = -pixels;
20166 return OK_PIXELS (pixels);
20167 }
20168
20169 car = Fbuffer_local_value (car, it->w->buffer);
20170 }
20171
20172 if (INTEGERP (car) || FLOATP (car))
20173 {
20174 double fact;
20175 pixels = XFLOATINT (car);
20176 if (NILP (cdr))
20177 return OK_PIXELS (pixels);
20178 if (calc_pixel_width_or_height (&fact, it, cdr,
20179 font, width_p, align_to))
20180 return OK_PIXELS (pixels * fact);
20181 return 0;
20182 }
20183
20184 return 0;
20185 }
20186
20187 return 0;
20188 }
20189
20190 \f
20191 /***********************************************************************
20192 Glyph Display
20193 ***********************************************************************/
20194
20195 #ifdef HAVE_WINDOW_SYSTEM
20196
20197 #if GLYPH_DEBUG
20198
20199 void
20200 dump_glyph_string (s)
20201 struct glyph_string *s;
20202 {
20203 fprintf (stderr, "glyph string\n");
20204 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20205 s->x, s->y, s->width, s->height);
20206 fprintf (stderr, " ybase = %d\n", s->ybase);
20207 fprintf (stderr, " hl = %d\n", s->hl);
20208 fprintf (stderr, " left overhang = %d, right = %d\n",
20209 s->left_overhang, s->right_overhang);
20210 fprintf (stderr, " nchars = %d\n", s->nchars);
20211 fprintf (stderr, " extends to end of line = %d\n",
20212 s->extends_to_end_of_line_p);
20213 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20214 fprintf (stderr, " bg width = %d\n", s->background_width);
20215 }
20216
20217 #endif /* GLYPH_DEBUG */
20218
20219 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20220 of XChar2b structures for S; it can't be allocated in
20221 init_glyph_string because it must be allocated via `alloca'. W
20222 is the window on which S is drawn. ROW and AREA are the glyph row
20223 and area within the row from which S is constructed. START is the
20224 index of the first glyph structure covered by S. HL is a
20225 face-override for drawing S. */
20226
20227 #ifdef HAVE_NTGUI
20228 #define OPTIONAL_HDC(hdc) hdc,
20229 #define DECLARE_HDC(hdc) HDC hdc;
20230 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20231 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20232 #endif
20233
20234 #ifndef OPTIONAL_HDC
20235 #define OPTIONAL_HDC(hdc)
20236 #define DECLARE_HDC(hdc)
20237 #define ALLOCATE_HDC(hdc, f)
20238 #define RELEASE_HDC(hdc, f)
20239 #endif
20240
20241 static void
20242 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
20243 struct glyph_string *s;
20244 DECLARE_HDC (hdc)
20245 XChar2b *char2b;
20246 struct window *w;
20247 struct glyph_row *row;
20248 enum glyph_row_area area;
20249 int start;
20250 enum draw_glyphs_face hl;
20251 {
20252 bzero (s, sizeof *s);
20253 s->w = w;
20254 s->f = XFRAME (w->frame);
20255 #ifdef HAVE_NTGUI
20256 s->hdc = hdc;
20257 #endif
20258 s->display = FRAME_X_DISPLAY (s->f);
20259 s->window = FRAME_X_WINDOW (s->f);
20260 s->char2b = char2b;
20261 s->hl = hl;
20262 s->row = row;
20263 s->area = area;
20264 s->first_glyph = row->glyphs[area] + start;
20265 s->height = row->height;
20266 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20267 s->ybase = s->y + row->ascent;
20268 }
20269
20270
20271 /* Append the list of glyph strings with head H and tail T to the list
20272 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20273
20274 static INLINE void
20275 append_glyph_string_lists (head, tail, h, t)
20276 struct glyph_string **head, **tail;
20277 struct glyph_string *h, *t;
20278 {
20279 if (h)
20280 {
20281 if (*head)
20282 (*tail)->next = h;
20283 else
20284 *head = h;
20285 h->prev = *tail;
20286 *tail = t;
20287 }
20288 }
20289
20290
20291 /* Prepend the list of glyph strings with head H and tail T to the
20292 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20293 result. */
20294
20295 static INLINE void
20296 prepend_glyph_string_lists (head, tail, h, t)
20297 struct glyph_string **head, **tail;
20298 struct glyph_string *h, *t;
20299 {
20300 if (h)
20301 {
20302 if (*head)
20303 (*head)->prev = t;
20304 else
20305 *tail = t;
20306 t->next = *head;
20307 *head = h;
20308 }
20309 }
20310
20311
20312 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20313 Set *HEAD and *TAIL to the resulting list. */
20314
20315 static INLINE void
20316 append_glyph_string (head, tail, s)
20317 struct glyph_string **head, **tail;
20318 struct glyph_string *s;
20319 {
20320 s->next = s->prev = NULL;
20321 append_glyph_string_lists (head, tail, s, s);
20322 }
20323
20324
20325 /* Get face and two-byte form of character C in face FACE_ID on frame
20326 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20327 means we want to display multibyte text. DISPLAY_P non-zero means
20328 make sure that X resources for the face returned are allocated.
20329 Value is a pointer to a realized face that is ready for display if
20330 DISPLAY_P is non-zero. */
20331
20332 static INLINE struct face *
20333 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
20334 struct frame *f;
20335 int c, face_id;
20336 XChar2b *char2b;
20337 int multibyte_p, display_p;
20338 {
20339 struct face *face = FACE_FROM_ID (f, face_id);
20340
20341 if (face->font)
20342 {
20343 unsigned code = face->font->driver->encode_char (face->font, c);
20344
20345 if (code != FONT_INVALID_CODE)
20346 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20347 else
20348 STORE_XCHAR2B (char2b, 0, 0);
20349 }
20350
20351 /* Make sure X resources of the face are allocated. */
20352 #ifdef HAVE_X_WINDOWS
20353 if (display_p)
20354 #endif
20355 {
20356 xassert (face != NULL);
20357 PREPARE_FACE_FOR_DISPLAY (f, face);
20358 }
20359
20360 return face;
20361 }
20362
20363
20364 /* Get face and two-byte form of character glyph GLYPH on frame F.
20365 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20366 a pointer to a realized face that is ready for display. */
20367
20368 static INLINE struct face *
20369 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
20370 struct frame *f;
20371 struct glyph *glyph;
20372 XChar2b *char2b;
20373 int *two_byte_p;
20374 {
20375 struct face *face;
20376
20377 xassert (glyph->type == CHAR_GLYPH);
20378 face = FACE_FROM_ID (f, glyph->face_id);
20379
20380 if (two_byte_p)
20381 *two_byte_p = 0;
20382
20383 if (face->font)
20384 {
20385 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
20386
20387 if (code != FONT_INVALID_CODE)
20388 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20389 else
20390 STORE_XCHAR2B (char2b, 0, 0);
20391 }
20392
20393 /* Make sure X resources of the face are allocated. */
20394 xassert (face != NULL);
20395 PREPARE_FACE_FOR_DISPLAY (f, face);
20396 return face;
20397 }
20398
20399
20400 /* Fill glyph string S with composition components specified by S->cmp.
20401
20402 BASE_FACE is the base face of the composition.
20403 S->cmp_from is the index of the first component for S.
20404
20405 OVERLAPS non-zero means S should draw the foreground only, and use
20406 its physical height for clipping. See also draw_glyphs.
20407
20408 Value is the index of a component not in S. */
20409
20410 static int
20411 fill_composite_glyph_string (s, base_face, overlaps)
20412 struct glyph_string *s;
20413 struct face *base_face;
20414 int overlaps;
20415 {
20416 int i;
20417 /* For all glyphs of this composition, starting at the offset
20418 S->cmp_from, until we reach the end of the definition or encounter a
20419 glyph that requires the different face, add it to S. */
20420 struct face *face;
20421
20422 xassert (s);
20423
20424 s->for_overlaps = overlaps;
20425 s->face = NULL;
20426 s->font = NULL;
20427 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20428 {
20429 int c = COMPOSITION_GLYPH (s->cmp, i);
20430
20431 if (c != '\t')
20432 {
20433 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20434 -1, Qnil);
20435
20436 face = get_char_face_and_encoding (s->f, c, face_id,
20437 s->char2b + i, 1, 1);
20438 if (face)
20439 {
20440 if (! s->face)
20441 {
20442 s->face = face;
20443 s->font = s->face->font;
20444 }
20445 else if (s->face != face)
20446 break;
20447 }
20448 }
20449 ++s->nchars;
20450 }
20451 s->cmp_to = i;
20452
20453 /* All glyph strings for the same composition has the same width,
20454 i.e. the width set for the first component of the composition. */
20455 s->width = s->first_glyph->pixel_width;
20456
20457 /* If the specified font could not be loaded, use the frame's
20458 default font, but record the fact that we couldn't load it in
20459 the glyph string so that we can draw rectangles for the
20460 characters of the glyph string. */
20461 if (s->font == NULL)
20462 {
20463 s->font_not_found_p = 1;
20464 s->font = FRAME_FONT (s->f);
20465 }
20466
20467 /* Adjust base line for subscript/superscript text. */
20468 s->ybase += s->first_glyph->voffset;
20469
20470 /* This glyph string must always be drawn with 16-bit functions. */
20471 s->two_byte_p = 1;
20472
20473 return s->cmp_to;
20474 }
20475
20476 static int
20477 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
20478 struct glyph_string *s;
20479 int face_id;
20480 int start, end, overlaps;
20481 {
20482 struct glyph *glyph, *last;
20483 Lisp_Object lgstring;
20484 int i;
20485
20486 s->for_overlaps = overlaps;
20487 glyph = s->row->glyphs[s->area] + start;
20488 last = s->row->glyphs[s->area] + end;
20489 s->cmp_id = glyph->u.cmp.id;
20490 s->cmp_from = glyph->u.cmp.from;
20491 s->cmp_to = glyph->u.cmp.to + 1;
20492 s->face = FACE_FROM_ID (s->f, face_id);
20493 lgstring = composition_gstring_from_id (s->cmp_id);
20494 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20495 glyph++;
20496 while (glyph < last
20497 && glyph->u.cmp.automatic
20498 && glyph->u.cmp.id == s->cmp_id
20499 && s->cmp_to == glyph->u.cmp.from)
20500 s->cmp_to = (glyph++)->u.cmp.to + 1;
20501
20502 for (i = s->cmp_from; i < s->cmp_to; i++)
20503 {
20504 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20505 unsigned code = LGLYPH_CODE (lglyph);
20506
20507 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20508 }
20509 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20510 return glyph - s->row->glyphs[s->area];
20511 }
20512
20513
20514 /* Fill glyph string S from a sequence of character glyphs.
20515
20516 FACE_ID is the face id of the string. START is the index of the
20517 first glyph to consider, END is the index of the last + 1.
20518 OVERLAPS non-zero means S should draw the foreground only, and use
20519 its physical height for clipping. See also draw_glyphs.
20520
20521 Value is the index of the first glyph not in S. */
20522
20523 static int
20524 fill_glyph_string (s, face_id, start, end, overlaps)
20525 struct glyph_string *s;
20526 int face_id;
20527 int start, end, overlaps;
20528 {
20529 struct glyph *glyph, *last;
20530 int voffset;
20531 int glyph_not_available_p;
20532
20533 xassert (s->f == XFRAME (s->w->frame));
20534 xassert (s->nchars == 0);
20535 xassert (start >= 0 && end > start);
20536
20537 s->for_overlaps = overlaps;
20538 glyph = s->row->glyphs[s->area] + start;
20539 last = s->row->glyphs[s->area] + end;
20540 voffset = glyph->voffset;
20541 s->padding_p = glyph->padding_p;
20542 glyph_not_available_p = glyph->glyph_not_available_p;
20543
20544 while (glyph < last
20545 && glyph->type == CHAR_GLYPH
20546 && glyph->voffset == voffset
20547 /* Same face id implies same font, nowadays. */
20548 && glyph->face_id == face_id
20549 && glyph->glyph_not_available_p == glyph_not_available_p)
20550 {
20551 int two_byte_p;
20552
20553 s->face = get_glyph_face_and_encoding (s->f, glyph,
20554 s->char2b + s->nchars,
20555 &two_byte_p);
20556 s->two_byte_p = two_byte_p;
20557 ++s->nchars;
20558 xassert (s->nchars <= end - start);
20559 s->width += glyph->pixel_width;
20560 if (glyph++->padding_p != s->padding_p)
20561 break;
20562 }
20563
20564 s->font = s->face->font;
20565
20566 /* If the specified font could not be loaded, use the frame's font,
20567 but record the fact that we couldn't load it in
20568 S->font_not_found_p so that we can draw rectangles for the
20569 characters of the glyph string. */
20570 if (s->font == NULL || glyph_not_available_p)
20571 {
20572 s->font_not_found_p = 1;
20573 s->font = FRAME_FONT (s->f);
20574 }
20575
20576 /* Adjust base line for subscript/superscript text. */
20577 s->ybase += voffset;
20578
20579 xassert (s->face && s->face->gc);
20580 return glyph - s->row->glyphs[s->area];
20581 }
20582
20583
20584 /* Fill glyph string S from image glyph S->first_glyph. */
20585
20586 static void
20587 fill_image_glyph_string (s)
20588 struct glyph_string *s;
20589 {
20590 xassert (s->first_glyph->type == IMAGE_GLYPH);
20591 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20592 xassert (s->img);
20593 s->slice = s->first_glyph->slice;
20594 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20595 s->font = s->face->font;
20596 s->width = s->first_glyph->pixel_width;
20597
20598 /* Adjust base line for subscript/superscript text. */
20599 s->ybase += s->first_glyph->voffset;
20600 }
20601
20602
20603 /* Fill glyph string S from a sequence of stretch glyphs.
20604
20605 ROW is the glyph row in which the glyphs are found, AREA is the
20606 area within the row. START is the index of the first glyph to
20607 consider, END is the index of the last + 1.
20608
20609 Value is the index of the first glyph not in S. */
20610
20611 static int
20612 fill_stretch_glyph_string (s, row, area, start, end)
20613 struct glyph_string *s;
20614 struct glyph_row *row;
20615 enum glyph_row_area area;
20616 int start, end;
20617 {
20618 struct glyph *glyph, *last;
20619 int voffset, face_id;
20620
20621 xassert (s->first_glyph->type == STRETCH_GLYPH);
20622
20623 glyph = s->row->glyphs[s->area] + start;
20624 last = s->row->glyphs[s->area] + end;
20625 face_id = glyph->face_id;
20626 s->face = FACE_FROM_ID (s->f, face_id);
20627 s->font = s->face->font;
20628 s->width = glyph->pixel_width;
20629 s->nchars = 1;
20630 voffset = glyph->voffset;
20631
20632 for (++glyph;
20633 (glyph < last
20634 && glyph->type == STRETCH_GLYPH
20635 && glyph->voffset == voffset
20636 && glyph->face_id == face_id);
20637 ++glyph)
20638 s->width += glyph->pixel_width;
20639
20640 /* Adjust base line for subscript/superscript text. */
20641 s->ybase += voffset;
20642
20643 /* The case that face->gc == 0 is handled when drawing the glyph
20644 string by calling PREPARE_FACE_FOR_DISPLAY. */
20645 xassert (s->face);
20646 return glyph - s->row->glyphs[s->area];
20647 }
20648
20649 static struct font_metrics *
20650 get_per_char_metric (f, font, char2b)
20651 struct frame *f;
20652 struct font *font;
20653 XChar2b *char2b;
20654 {
20655 static struct font_metrics metrics;
20656 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20657
20658 if (! font || code == FONT_INVALID_CODE)
20659 return NULL;
20660 font->driver->text_extents (font, &code, 1, &metrics);
20661 return &metrics;
20662 }
20663
20664 /* EXPORT for RIF:
20665 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20666 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20667 assumed to be zero. */
20668
20669 void
20670 x_get_glyph_overhangs (glyph, f, left, right)
20671 struct glyph *glyph;
20672 struct frame *f;
20673 int *left, *right;
20674 {
20675 *left = *right = 0;
20676
20677 if (glyph->type == CHAR_GLYPH)
20678 {
20679 struct face *face;
20680 XChar2b char2b;
20681 struct font_metrics *pcm;
20682
20683 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20684 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20685 {
20686 if (pcm->rbearing > pcm->width)
20687 *right = pcm->rbearing - pcm->width;
20688 if (pcm->lbearing < 0)
20689 *left = -pcm->lbearing;
20690 }
20691 }
20692 else if (glyph->type == COMPOSITE_GLYPH)
20693 {
20694 if (! glyph->u.cmp.automatic)
20695 {
20696 struct composition *cmp = composition_table[glyph->u.cmp.id];
20697
20698 if (cmp->rbearing > cmp->pixel_width)
20699 *right = cmp->rbearing - cmp->pixel_width;
20700 if (cmp->lbearing < 0)
20701 *left = - cmp->lbearing;
20702 }
20703 else
20704 {
20705 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20706 struct font_metrics metrics;
20707
20708 composition_gstring_width (gstring, glyph->u.cmp.from,
20709 glyph->u.cmp.to + 1, &metrics);
20710 if (metrics.rbearing > metrics.width)
20711 *right = metrics.rbearing - metrics.width;
20712 if (metrics.lbearing < 0)
20713 *left = - metrics.lbearing;
20714 }
20715 }
20716 }
20717
20718
20719 /* Return the index of the first glyph preceding glyph string S that
20720 is overwritten by S because of S's left overhang. Value is -1
20721 if no glyphs are overwritten. */
20722
20723 static int
20724 left_overwritten (s)
20725 struct glyph_string *s;
20726 {
20727 int k;
20728
20729 if (s->left_overhang)
20730 {
20731 int x = 0, i;
20732 struct glyph *glyphs = s->row->glyphs[s->area];
20733 int first = s->first_glyph - glyphs;
20734
20735 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20736 x -= glyphs[i].pixel_width;
20737
20738 k = i + 1;
20739 }
20740 else
20741 k = -1;
20742
20743 return k;
20744 }
20745
20746
20747 /* Return the index of the first glyph preceding glyph string S that
20748 is overwriting S because of its right overhang. Value is -1 if no
20749 glyph in front of S overwrites S. */
20750
20751 static int
20752 left_overwriting (s)
20753 struct glyph_string *s;
20754 {
20755 int i, k, x;
20756 struct glyph *glyphs = s->row->glyphs[s->area];
20757 int first = s->first_glyph - glyphs;
20758
20759 k = -1;
20760 x = 0;
20761 for (i = first - 1; i >= 0; --i)
20762 {
20763 int left, right;
20764 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20765 if (x + right > 0)
20766 k = i;
20767 x -= glyphs[i].pixel_width;
20768 }
20769
20770 return k;
20771 }
20772
20773
20774 /* Return the index of the last glyph following glyph string S that is
20775 overwritten by S because of S's right overhang. Value is -1 if
20776 no such glyph is found. */
20777
20778 static int
20779 right_overwritten (s)
20780 struct glyph_string *s;
20781 {
20782 int k = -1;
20783
20784 if (s->right_overhang)
20785 {
20786 int x = 0, i;
20787 struct glyph *glyphs = s->row->glyphs[s->area];
20788 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20789 int end = s->row->used[s->area];
20790
20791 for (i = first; i < end && s->right_overhang > x; ++i)
20792 x += glyphs[i].pixel_width;
20793
20794 k = i;
20795 }
20796
20797 return k;
20798 }
20799
20800
20801 /* Return the index of the last glyph following glyph string S that
20802 overwrites S because of its left overhang. Value is negative
20803 if no such glyph is found. */
20804
20805 static int
20806 right_overwriting (s)
20807 struct glyph_string *s;
20808 {
20809 int i, k, x;
20810 int end = s->row->used[s->area];
20811 struct glyph *glyphs = s->row->glyphs[s->area];
20812 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20813
20814 k = -1;
20815 x = 0;
20816 for (i = first; i < end; ++i)
20817 {
20818 int left, right;
20819 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20820 if (x - left < 0)
20821 k = i;
20822 x += glyphs[i].pixel_width;
20823 }
20824
20825 return k;
20826 }
20827
20828
20829 /* Set background width of glyph string S. START is the index of the
20830 first glyph following S. LAST_X is the right-most x-position + 1
20831 in the drawing area. */
20832
20833 static INLINE void
20834 set_glyph_string_background_width (s, start, last_x)
20835 struct glyph_string *s;
20836 int start;
20837 int last_x;
20838 {
20839 /* If the face of this glyph string has to be drawn to the end of
20840 the drawing area, set S->extends_to_end_of_line_p. */
20841
20842 if (start == s->row->used[s->area]
20843 && s->area == TEXT_AREA
20844 && ((s->row->fill_line_p
20845 && (s->hl == DRAW_NORMAL_TEXT
20846 || s->hl == DRAW_IMAGE_RAISED
20847 || s->hl == DRAW_IMAGE_SUNKEN))
20848 || s->hl == DRAW_MOUSE_FACE))
20849 s->extends_to_end_of_line_p = 1;
20850
20851 /* If S extends its face to the end of the line, set its
20852 background_width to the distance to the right edge of the drawing
20853 area. */
20854 if (s->extends_to_end_of_line_p)
20855 s->background_width = last_x - s->x + 1;
20856 else
20857 s->background_width = s->width;
20858 }
20859
20860
20861 /* Compute overhangs and x-positions for glyph string S and its
20862 predecessors, or successors. X is the starting x-position for S.
20863 BACKWARD_P non-zero means process predecessors. */
20864
20865 static void
20866 compute_overhangs_and_x (s, x, backward_p)
20867 struct glyph_string *s;
20868 int x;
20869 int backward_p;
20870 {
20871 if (backward_p)
20872 {
20873 while (s)
20874 {
20875 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20876 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20877 x -= s->width;
20878 s->x = x;
20879 s = s->prev;
20880 }
20881 }
20882 else
20883 {
20884 while (s)
20885 {
20886 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20887 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20888 s->x = x;
20889 x += s->width;
20890 s = s->next;
20891 }
20892 }
20893 }
20894
20895
20896
20897 /* The following macros are only called from draw_glyphs below.
20898 They reference the following parameters of that function directly:
20899 `w', `row', `area', and `overlap_p'
20900 as well as the following local variables:
20901 `s', `f', and `hdc' (in W32) */
20902
20903 #ifdef HAVE_NTGUI
20904 /* On W32, silently add local `hdc' variable to argument list of
20905 init_glyph_string. */
20906 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20907 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20908 #else
20909 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20910 init_glyph_string (s, char2b, w, row, area, start, hl)
20911 #endif
20912
20913 /* Add a glyph string for a stretch glyph to the list of strings
20914 between HEAD and TAIL. START is the index of the stretch glyph in
20915 row area AREA of glyph row ROW. END is the index of the last glyph
20916 in that glyph row area. X is the current output position assigned
20917 to the new glyph string constructed. HL overrides that face of the
20918 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20919 is the right-most x-position of the drawing area. */
20920
20921 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20922 and below -- keep them on one line. */
20923 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20924 do \
20925 { \
20926 s = (struct glyph_string *) alloca (sizeof *s); \
20927 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20928 START = fill_stretch_glyph_string (s, row, area, START, END); \
20929 append_glyph_string (&HEAD, &TAIL, s); \
20930 s->x = (X); \
20931 } \
20932 while (0)
20933
20934
20935 /* Add a glyph string for an image glyph to the list of strings
20936 between HEAD and TAIL. START is the index of the image glyph in
20937 row area AREA of glyph row ROW. END is the index of the last glyph
20938 in that glyph row area. X is the current output position assigned
20939 to the new glyph string constructed. HL overrides that face of the
20940 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20941 is the right-most x-position of the drawing area. */
20942
20943 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20944 do \
20945 { \
20946 s = (struct glyph_string *) alloca (sizeof *s); \
20947 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20948 fill_image_glyph_string (s); \
20949 append_glyph_string (&HEAD, &TAIL, s); \
20950 ++START; \
20951 s->x = (X); \
20952 } \
20953 while (0)
20954
20955
20956 /* Add a glyph string for a sequence of character glyphs to the list
20957 of strings between HEAD and TAIL. START is the index of the first
20958 glyph in row area AREA of glyph row ROW that is part of the new
20959 glyph string. END is the index of the last glyph in that glyph row
20960 area. X is the current output position assigned to the new glyph
20961 string constructed. HL overrides that face of the glyph; e.g. it
20962 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20963 right-most x-position of the drawing area. */
20964
20965 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20966 do \
20967 { \
20968 int face_id; \
20969 XChar2b *char2b; \
20970 \
20971 face_id = (row)->glyphs[area][START].face_id; \
20972 \
20973 s = (struct glyph_string *) alloca (sizeof *s); \
20974 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20975 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20976 append_glyph_string (&HEAD, &TAIL, s); \
20977 s->x = (X); \
20978 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20979 } \
20980 while (0)
20981
20982
20983 /* Add a glyph string for a composite sequence to the list of strings
20984 between HEAD and TAIL. START is the index of the first glyph in
20985 row area AREA of glyph row ROW that is part of the new glyph
20986 string. END is the index of the last glyph in that glyph row area.
20987 X is the current output position assigned to the new glyph string
20988 constructed. HL overrides that face of the glyph; e.g. it is
20989 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20990 x-position of the drawing area. */
20991
20992 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20993 do { \
20994 int face_id = (row)->glyphs[area][START].face_id; \
20995 struct face *base_face = FACE_FROM_ID (f, face_id); \
20996 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20997 struct composition *cmp = composition_table[cmp_id]; \
20998 XChar2b *char2b; \
20999 struct glyph_string *first_s; \
21000 int n; \
21001 \
21002 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21003 \
21004 /* Make glyph_strings for each glyph sequence that is drawable by \
21005 the same face, and append them to HEAD/TAIL. */ \
21006 for (n = 0; n < cmp->glyph_len;) \
21007 { \
21008 s = (struct glyph_string *) alloca (sizeof *s); \
21009 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21010 append_glyph_string (&(HEAD), &(TAIL), s); \
21011 s->cmp = cmp; \
21012 s->cmp_from = n; \
21013 s->x = (X); \
21014 if (n == 0) \
21015 first_s = s; \
21016 n = fill_composite_glyph_string (s, base_face, overlaps); \
21017 } \
21018 \
21019 ++START; \
21020 s = first_s; \
21021 } while (0)
21022
21023
21024 /* Add a glyph string for a glyph-string sequence to the list of strings
21025 between HEAD and TAIL. */
21026
21027 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21028 do { \
21029 int face_id; \
21030 XChar2b *char2b; \
21031 Lisp_Object gstring; \
21032 \
21033 face_id = (row)->glyphs[area][START].face_id; \
21034 gstring = (composition_gstring_from_id \
21035 ((row)->glyphs[area][START].u.cmp.id)); \
21036 s = (struct glyph_string *) alloca (sizeof *s); \
21037 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21038 * LGSTRING_GLYPH_LEN (gstring)); \
21039 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21040 append_glyph_string (&(HEAD), &(TAIL), s); \
21041 s->x = (X); \
21042 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21043 } while (0)
21044
21045
21046 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21047 of AREA of glyph row ROW on window W between indices START and END.
21048 HL overrides the face for drawing glyph strings, e.g. it is
21049 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21050 x-positions of the drawing area.
21051
21052 This is an ugly monster macro construct because we must use alloca
21053 to allocate glyph strings (because draw_glyphs can be called
21054 asynchronously). */
21055
21056 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21057 do \
21058 { \
21059 HEAD = TAIL = NULL; \
21060 while (START < END) \
21061 { \
21062 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21063 switch (first_glyph->type) \
21064 { \
21065 case CHAR_GLYPH: \
21066 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21067 HL, X, LAST_X); \
21068 break; \
21069 \
21070 case COMPOSITE_GLYPH: \
21071 if (first_glyph->u.cmp.automatic) \
21072 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21073 HL, X, LAST_X); \
21074 else \
21075 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21076 HL, X, LAST_X); \
21077 break; \
21078 \
21079 case STRETCH_GLYPH: \
21080 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21081 HL, X, LAST_X); \
21082 break; \
21083 \
21084 case IMAGE_GLYPH: \
21085 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21086 HL, X, LAST_X); \
21087 break; \
21088 \
21089 default: \
21090 abort (); \
21091 } \
21092 \
21093 if (s) \
21094 { \
21095 set_glyph_string_background_width (s, START, LAST_X); \
21096 (X) += s->width; \
21097 } \
21098 } \
21099 } while (0)
21100
21101
21102 /* Draw glyphs between START and END in AREA of ROW on window W,
21103 starting at x-position X. X is relative to AREA in W. HL is a
21104 face-override with the following meaning:
21105
21106 DRAW_NORMAL_TEXT draw normally
21107 DRAW_CURSOR draw in cursor face
21108 DRAW_MOUSE_FACE draw in mouse face.
21109 DRAW_INVERSE_VIDEO draw in mode line face
21110 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21111 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21112
21113 If OVERLAPS is non-zero, draw only the foreground of characters and
21114 clip to the physical height of ROW. Non-zero value also defines
21115 the overlapping part to be drawn:
21116
21117 OVERLAPS_PRED overlap with preceding rows
21118 OVERLAPS_SUCC overlap with succeeding rows
21119 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21120 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21121
21122 Value is the x-position reached, relative to AREA of W. */
21123
21124 static int
21125 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
21126 struct window *w;
21127 int x;
21128 struct glyph_row *row;
21129 enum glyph_row_area area;
21130 EMACS_INT start, end;
21131 enum draw_glyphs_face hl;
21132 int overlaps;
21133 {
21134 struct glyph_string *head, *tail;
21135 struct glyph_string *s;
21136 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21137 int i, j, x_reached, last_x, area_left = 0;
21138 struct frame *f = XFRAME (WINDOW_FRAME (w));
21139 DECLARE_HDC (hdc);
21140
21141 ALLOCATE_HDC (hdc, f);
21142
21143 /* Let's rather be paranoid than getting a SEGV. */
21144 end = min (end, row->used[area]);
21145 start = max (0, start);
21146 start = min (end, start);
21147
21148 /* Translate X to frame coordinates. Set last_x to the right
21149 end of the drawing area. */
21150 if (row->full_width_p)
21151 {
21152 /* X is relative to the left edge of W, without scroll bars
21153 or fringes. */
21154 area_left = WINDOW_LEFT_EDGE_X (w);
21155 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21156 }
21157 else
21158 {
21159 area_left = window_box_left (w, area);
21160 last_x = area_left + window_box_width (w, area);
21161 }
21162 x += area_left;
21163
21164 /* Build a doubly-linked list of glyph_string structures between
21165 head and tail from what we have to draw. Note that the macro
21166 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21167 the reason we use a separate variable `i'. */
21168 i = start;
21169 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21170 if (tail)
21171 x_reached = tail->x + tail->background_width;
21172 else
21173 x_reached = x;
21174
21175 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21176 the row, redraw some glyphs in front or following the glyph
21177 strings built above. */
21178 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21179 {
21180 struct glyph_string *h, *t;
21181 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21182 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21183 int dummy_x = 0;
21184
21185 /* If mouse highlighting is on, we may need to draw adjacent
21186 glyphs using mouse-face highlighting. */
21187 if (area == TEXT_AREA && row->mouse_face_p)
21188 {
21189 struct glyph_row *mouse_beg_row, *mouse_end_row;
21190
21191 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21192 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21193
21194 if (row >= mouse_beg_row && row <= mouse_end_row)
21195 {
21196 check_mouse_face = 1;
21197 mouse_beg_col = (row == mouse_beg_row)
21198 ? dpyinfo->mouse_face_beg_col : 0;
21199 mouse_end_col = (row == mouse_end_row)
21200 ? dpyinfo->mouse_face_end_col
21201 : row->used[TEXT_AREA];
21202 }
21203 }
21204
21205 /* Compute overhangs for all glyph strings. */
21206 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21207 for (s = head; s; s = s->next)
21208 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21209
21210 /* Prepend glyph strings for glyphs in front of the first glyph
21211 string that are overwritten because of the first glyph
21212 string's left overhang. The background of all strings
21213 prepended must be drawn because the first glyph string
21214 draws over it. */
21215 i = left_overwritten (head);
21216 if (i >= 0)
21217 {
21218 enum draw_glyphs_face overlap_hl;
21219
21220 /* If this row contains mouse highlighting, attempt to draw
21221 the overlapped glyphs with the correct highlight. This
21222 code fails if the overlap encompasses more than one glyph
21223 and mouse-highlight spans only some of these glyphs.
21224 However, making it work perfectly involves a lot more
21225 code, and I don't know if the pathological case occurs in
21226 practice, so we'll stick to this for now. --- cyd */
21227 if (check_mouse_face
21228 && mouse_beg_col < start && mouse_end_col > i)
21229 overlap_hl = DRAW_MOUSE_FACE;
21230 else
21231 overlap_hl = DRAW_NORMAL_TEXT;
21232
21233 j = i;
21234 BUILD_GLYPH_STRINGS (j, start, h, t,
21235 overlap_hl, dummy_x, last_x);
21236 start = i;
21237 compute_overhangs_and_x (t, head->x, 1);
21238 prepend_glyph_string_lists (&head, &tail, h, t);
21239 clip_head = head;
21240 }
21241
21242 /* Prepend glyph strings for glyphs in front of the first glyph
21243 string that overwrite that glyph string because of their
21244 right overhang. For these strings, only the foreground must
21245 be drawn, because it draws over the glyph string at `head'.
21246 The background must not be drawn because this would overwrite
21247 right overhangs of preceding glyphs for which no glyph
21248 strings exist. */
21249 i = left_overwriting (head);
21250 if (i >= 0)
21251 {
21252 enum draw_glyphs_face overlap_hl;
21253
21254 if (check_mouse_face
21255 && mouse_beg_col < start && mouse_end_col > i)
21256 overlap_hl = DRAW_MOUSE_FACE;
21257 else
21258 overlap_hl = DRAW_NORMAL_TEXT;
21259
21260 clip_head = head;
21261 BUILD_GLYPH_STRINGS (i, start, h, t,
21262 overlap_hl, dummy_x, last_x);
21263 for (s = h; s; s = s->next)
21264 s->background_filled_p = 1;
21265 compute_overhangs_and_x (t, head->x, 1);
21266 prepend_glyph_string_lists (&head, &tail, h, t);
21267 }
21268
21269 /* Append glyphs strings for glyphs following the last glyph
21270 string tail that are overwritten by tail. The background of
21271 these strings has to be drawn because tail's foreground draws
21272 over it. */
21273 i = right_overwritten (tail);
21274 if (i >= 0)
21275 {
21276 enum draw_glyphs_face overlap_hl;
21277
21278 if (check_mouse_face
21279 && mouse_beg_col < i && mouse_end_col > end)
21280 overlap_hl = DRAW_MOUSE_FACE;
21281 else
21282 overlap_hl = DRAW_NORMAL_TEXT;
21283
21284 BUILD_GLYPH_STRINGS (end, i, h, t,
21285 overlap_hl, x, last_x);
21286 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21287 we don't have `end = i;' here. */
21288 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21289 append_glyph_string_lists (&head, &tail, h, t);
21290 clip_tail = tail;
21291 }
21292
21293 /* Append glyph strings for glyphs following the last glyph
21294 string tail that overwrite tail. The foreground of such
21295 glyphs has to be drawn because it writes into the background
21296 of tail. The background must not be drawn because it could
21297 paint over the foreground of following glyphs. */
21298 i = right_overwriting (tail);
21299 if (i >= 0)
21300 {
21301 enum draw_glyphs_face overlap_hl;
21302 if (check_mouse_face
21303 && mouse_beg_col < i && mouse_end_col > end)
21304 overlap_hl = DRAW_MOUSE_FACE;
21305 else
21306 overlap_hl = DRAW_NORMAL_TEXT;
21307
21308 clip_tail = tail;
21309 i++; /* We must include the Ith glyph. */
21310 BUILD_GLYPH_STRINGS (end, i, h, t,
21311 overlap_hl, x, last_x);
21312 for (s = h; s; s = s->next)
21313 s->background_filled_p = 1;
21314 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21315 append_glyph_string_lists (&head, &tail, h, t);
21316 }
21317 if (clip_head || clip_tail)
21318 for (s = head; s; s = s->next)
21319 {
21320 s->clip_head = clip_head;
21321 s->clip_tail = clip_tail;
21322 }
21323 }
21324
21325 /* Draw all strings. */
21326 for (s = head; s; s = s->next)
21327 FRAME_RIF (f)->draw_glyph_string (s);
21328
21329 #ifndef HAVE_NS
21330 /* When focus a sole frame and move horizontally, this sets on_p to 0
21331 causing a failure to erase prev cursor position. */
21332 if (area == TEXT_AREA
21333 && !row->full_width_p
21334 /* When drawing overlapping rows, only the glyph strings'
21335 foreground is drawn, which doesn't erase a cursor
21336 completely. */
21337 && !overlaps)
21338 {
21339 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21340 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21341 : (tail ? tail->x + tail->background_width : x));
21342 x0 -= area_left;
21343 x1 -= area_left;
21344
21345 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21346 row->y, MATRIX_ROW_BOTTOM_Y (row));
21347 }
21348 #endif
21349
21350 /* Value is the x-position up to which drawn, relative to AREA of W.
21351 This doesn't include parts drawn because of overhangs. */
21352 if (row->full_width_p)
21353 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21354 else
21355 x_reached -= area_left;
21356
21357 RELEASE_HDC (hdc, f);
21358
21359 return x_reached;
21360 }
21361
21362 /* Expand row matrix if too narrow. Don't expand if area
21363 is not present. */
21364
21365 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21366 { \
21367 if (!fonts_changed_p \
21368 && (it->glyph_row->glyphs[area] \
21369 < it->glyph_row->glyphs[area + 1])) \
21370 { \
21371 it->w->ncols_scale_factor++; \
21372 fonts_changed_p = 1; \
21373 } \
21374 }
21375
21376 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21377 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21378
21379 static INLINE void
21380 append_glyph (it)
21381 struct it *it;
21382 {
21383 struct glyph *glyph;
21384 enum glyph_row_area area = it->area;
21385
21386 xassert (it->glyph_row);
21387 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21388
21389 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21390 if (glyph < it->glyph_row->glyphs[area + 1])
21391 {
21392 glyph->charpos = CHARPOS (it->position);
21393 glyph->object = it->object;
21394 if (it->pixel_width > 0)
21395 {
21396 glyph->pixel_width = it->pixel_width;
21397 glyph->padding_p = 0;
21398 }
21399 else
21400 {
21401 /* Assure at least 1-pixel width. Otherwise, cursor can't
21402 be displayed correctly. */
21403 glyph->pixel_width = 1;
21404 glyph->padding_p = 1;
21405 }
21406 glyph->ascent = it->ascent;
21407 glyph->descent = it->descent;
21408 glyph->voffset = it->voffset;
21409 glyph->type = CHAR_GLYPH;
21410 glyph->avoid_cursor_p = it->avoid_cursor_p;
21411 glyph->multibyte_p = it->multibyte_p;
21412 glyph->left_box_line_p = it->start_of_box_run_p;
21413 glyph->right_box_line_p = it->end_of_box_run_p;
21414 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21415 || it->phys_descent > it->descent);
21416 glyph->glyph_not_available_p = it->glyph_not_available_p;
21417 glyph->face_id = it->face_id;
21418 glyph->u.ch = it->char_to_display;
21419 glyph->slice = null_glyph_slice;
21420 glyph->font_type = FONT_TYPE_UNKNOWN;
21421 if (it->bidi_p)
21422 {
21423 glyph->resolved_level = it->bidi_it.resolved_level;
21424 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21425 abort ();
21426 glyph->bidi_type = it->bidi_it.type;
21427 }
21428 ++it->glyph_row->used[area];
21429 }
21430 else
21431 IT_EXPAND_MATRIX_WIDTH (it, area);
21432 }
21433
21434 /* Store one glyph for the composition IT->cmp_it.id in
21435 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21436 non-null. */
21437
21438 static INLINE void
21439 append_composite_glyph (it)
21440 struct it *it;
21441 {
21442 struct glyph *glyph;
21443 enum glyph_row_area area = it->area;
21444
21445 xassert (it->glyph_row);
21446
21447 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21448 if (glyph < it->glyph_row->glyphs[area + 1])
21449 {
21450 glyph->charpos = CHARPOS (it->position);
21451 glyph->object = it->object;
21452 glyph->pixel_width = it->pixel_width;
21453 glyph->ascent = it->ascent;
21454 glyph->descent = it->descent;
21455 glyph->voffset = it->voffset;
21456 glyph->type = COMPOSITE_GLYPH;
21457 if (it->cmp_it.ch < 0)
21458 {
21459 glyph->u.cmp.automatic = 0;
21460 glyph->u.cmp.id = it->cmp_it.id;
21461 }
21462 else
21463 {
21464 glyph->u.cmp.automatic = 1;
21465 glyph->u.cmp.id = it->cmp_it.id;
21466 glyph->u.cmp.from = it->cmp_it.from;
21467 glyph->u.cmp.to = it->cmp_it.to - 1;
21468 }
21469 glyph->avoid_cursor_p = it->avoid_cursor_p;
21470 glyph->multibyte_p = it->multibyte_p;
21471 glyph->left_box_line_p = it->start_of_box_run_p;
21472 glyph->right_box_line_p = it->end_of_box_run_p;
21473 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21474 || it->phys_descent > it->descent);
21475 glyph->padding_p = 0;
21476 glyph->glyph_not_available_p = 0;
21477 glyph->face_id = it->face_id;
21478 glyph->slice = null_glyph_slice;
21479 glyph->font_type = FONT_TYPE_UNKNOWN;
21480 if (it->bidi_p)
21481 {
21482 glyph->resolved_level = it->bidi_it.resolved_level;
21483 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21484 abort ();
21485 glyph->bidi_type = it->bidi_it.type;
21486 }
21487 ++it->glyph_row->used[area];
21488 }
21489 else
21490 IT_EXPAND_MATRIX_WIDTH (it, area);
21491 }
21492
21493
21494 /* Change IT->ascent and IT->height according to the setting of
21495 IT->voffset. */
21496
21497 static INLINE void
21498 take_vertical_position_into_account (it)
21499 struct it *it;
21500 {
21501 if (it->voffset)
21502 {
21503 if (it->voffset < 0)
21504 /* Increase the ascent so that we can display the text higher
21505 in the line. */
21506 it->ascent -= it->voffset;
21507 else
21508 /* Increase the descent so that we can display the text lower
21509 in the line. */
21510 it->descent += it->voffset;
21511 }
21512 }
21513
21514
21515 /* Produce glyphs/get display metrics for the image IT is loaded with.
21516 See the description of struct display_iterator in dispextern.h for
21517 an overview of struct display_iterator. */
21518
21519 static void
21520 produce_image_glyph (it)
21521 struct it *it;
21522 {
21523 struct image *img;
21524 struct face *face;
21525 int glyph_ascent, crop;
21526 struct glyph_slice slice;
21527
21528 xassert (it->what == IT_IMAGE);
21529
21530 face = FACE_FROM_ID (it->f, it->face_id);
21531 xassert (face);
21532 /* Make sure X resources of the face is loaded. */
21533 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21534
21535 if (it->image_id < 0)
21536 {
21537 /* Fringe bitmap. */
21538 it->ascent = it->phys_ascent = 0;
21539 it->descent = it->phys_descent = 0;
21540 it->pixel_width = 0;
21541 it->nglyphs = 0;
21542 return;
21543 }
21544
21545 img = IMAGE_FROM_ID (it->f, it->image_id);
21546 xassert (img);
21547 /* Make sure X resources of the image is loaded. */
21548 prepare_image_for_display (it->f, img);
21549
21550 slice.x = slice.y = 0;
21551 slice.width = img->width;
21552 slice.height = img->height;
21553
21554 if (INTEGERP (it->slice.x))
21555 slice.x = XINT (it->slice.x);
21556 else if (FLOATP (it->slice.x))
21557 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21558
21559 if (INTEGERP (it->slice.y))
21560 slice.y = XINT (it->slice.y);
21561 else if (FLOATP (it->slice.y))
21562 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21563
21564 if (INTEGERP (it->slice.width))
21565 slice.width = XINT (it->slice.width);
21566 else if (FLOATP (it->slice.width))
21567 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21568
21569 if (INTEGERP (it->slice.height))
21570 slice.height = XINT (it->slice.height);
21571 else if (FLOATP (it->slice.height))
21572 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21573
21574 if (slice.x >= img->width)
21575 slice.x = img->width;
21576 if (slice.y >= img->height)
21577 slice.y = img->height;
21578 if (slice.x + slice.width >= img->width)
21579 slice.width = img->width - slice.x;
21580 if (slice.y + slice.height > img->height)
21581 slice.height = img->height - slice.y;
21582
21583 if (slice.width == 0 || slice.height == 0)
21584 return;
21585
21586 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21587
21588 it->descent = slice.height - glyph_ascent;
21589 if (slice.y == 0)
21590 it->descent += img->vmargin;
21591 if (slice.y + slice.height == img->height)
21592 it->descent += img->vmargin;
21593 it->phys_descent = it->descent;
21594
21595 it->pixel_width = slice.width;
21596 if (slice.x == 0)
21597 it->pixel_width += img->hmargin;
21598 if (slice.x + slice.width == img->width)
21599 it->pixel_width += img->hmargin;
21600
21601 /* It's quite possible for images to have an ascent greater than
21602 their height, so don't get confused in that case. */
21603 if (it->descent < 0)
21604 it->descent = 0;
21605
21606 it->nglyphs = 1;
21607
21608 if (face->box != FACE_NO_BOX)
21609 {
21610 if (face->box_line_width > 0)
21611 {
21612 if (slice.y == 0)
21613 it->ascent += face->box_line_width;
21614 if (slice.y + slice.height == img->height)
21615 it->descent += face->box_line_width;
21616 }
21617
21618 if (it->start_of_box_run_p && slice.x == 0)
21619 it->pixel_width += eabs (face->box_line_width);
21620 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21621 it->pixel_width += eabs (face->box_line_width);
21622 }
21623
21624 take_vertical_position_into_account (it);
21625
21626 /* Automatically crop wide image glyphs at right edge so we can
21627 draw the cursor on same display row. */
21628 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21629 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21630 {
21631 it->pixel_width -= crop;
21632 slice.width -= crop;
21633 }
21634
21635 if (it->glyph_row)
21636 {
21637 struct glyph *glyph;
21638 enum glyph_row_area area = it->area;
21639
21640 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21641 if (glyph < it->glyph_row->glyphs[area + 1])
21642 {
21643 glyph->charpos = CHARPOS (it->position);
21644 glyph->object = it->object;
21645 glyph->pixel_width = it->pixel_width;
21646 glyph->ascent = glyph_ascent;
21647 glyph->descent = it->descent;
21648 glyph->voffset = it->voffset;
21649 glyph->type = IMAGE_GLYPH;
21650 glyph->avoid_cursor_p = it->avoid_cursor_p;
21651 glyph->multibyte_p = it->multibyte_p;
21652 glyph->left_box_line_p = it->start_of_box_run_p;
21653 glyph->right_box_line_p = it->end_of_box_run_p;
21654 glyph->overlaps_vertically_p = 0;
21655 glyph->padding_p = 0;
21656 glyph->glyph_not_available_p = 0;
21657 glyph->face_id = it->face_id;
21658 glyph->u.img_id = img->id;
21659 glyph->slice = slice;
21660 glyph->font_type = FONT_TYPE_UNKNOWN;
21661 if (it->bidi_p)
21662 {
21663 glyph->resolved_level = it->bidi_it.resolved_level;
21664 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21665 abort ();
21666 glyph->bidi_type = it->bidi_it.type;
21667 }
21668 ++it->glyph_row->used[area];
21669 }
21670 else
21671 IT_EXPAND_MATRIX_WIDTH (it, area);
21672 }
21673 }
21674
21675
21676 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21677 of the glyph, WIDTH and HEIGHT are the width and height of the
21678 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21679
21680 static void
21681 append_stretch_glyph (it, object, width, height, ascent)
21682 struct it *it;
21683 Lisp_Object object;
21684 int width, height;
21685 int ascent;
21686 {
21687 struct glyph *glyph;
21688 enum glyph_row_area area = it->area;
21689
21690 xassert (ascent >= 0 && ascent <= height);
21691
21692 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21693 if (glyph < it->glyph_row->glyphs[area + 1])
21694 {
21695 glyph->charpos = CHARPOS (it->position);
21696 glyph->object = object;
21697 glyph->pixel_width = width;
21698 glyph->ascent = ascent;
21699 glyph->descent = height - ascent;
21700 glyph->voffset = it->voffset;
21701 glyph->type = STRETCH_GLYPH;
21702 glyph->avoid_cursor_p = it->avoid_cursor_p;
21703 glyph->multibyte_p = it->multibyte_p;
21704 glyph->left_box_line_p = it->start_of_box_run_p;
21705 glyph->right_box_line_p = it->end_of_box_run_p;
21706 glyph->overlaps_vertically_p = 0;
21707 glyph->padding_p = 0;
21708 glyph->glyph_not_available_p = 0;
21709 glyph->face_id = it->face_id;
21710 glyph->u.stretch.ascent = ascent;
21711 glyph->u.stretch.height = height;
21712 glyph->slice = null_glyph_slice;
21713 glyph->font_type = FONT_TYPE_UNKNOWN;
21714 if (it->bidi_p)
21715 {
21716 glyph->resolved_level = it->bidi_it.resolved_level;
21717 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21718 abort ();
21719 glyph->bidi_type = it->bidi_it.type;
21720 }
21721 ++it->glyph_row->used[area];
21722 }
21723 else
21724 IT_EXPAND_MATRIX_WIDTH (it, area);
21725 }
21726
21727
21728 /* Produce a stretch glyph for iterator IT. IT->object is the value
21729 of the glyph property displayed. The value must be a list
21730 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21731 being recognized:
21732
21733 1. `:width WIDTH' specifies that the space should be WIDTH *
21734 canonical char width wide. WIDTH may be an integer or floating
21735 point number.
21736
21737 2. `:relative-width FACTOR' specifies that the width of the stretch
21738 should be computed from the width of the first character having the
21739 `glyph' property, and should be FACTOR times that width.
21740
21741 3. `:align-to HPOS' specifies that the space should be wide enough
21742 to reach HPOS, a value in canonical character units.
21743
21744 Exactly one of the above pairs must be present.
21745
21746 4. `:height HEIGHT' specifies that the height of the stretch produced
21747 should be HEIGHT, measured in canonical character units.
21748
21749 5. `:relative-height FACTOR' specifies that the height of the
21750 stretch should be FACTOR times the height of the characters having
21751 the glyph property.
21752
21753 Either none or exactly one of 4 or 5 must be present.
21754
21755 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21756 of the stretch should be used for the ascent of the stretch.
21757 ASCENT must be in the range 0 <= ASCENT <= 100. */
21758
21759 static void
21760 produce_stretch_glyph (it)
21761 struct it *it;
21762 {
21763 /* (space :width WIDTH :height HEIGHT ...) */
21764 Lisp_Object prop, plist;
21765 int width = 0, height = 0, align_to = -1;
21766 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21767 int ascent = 0;
21768 double tem;
21769 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21770 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21771
21772 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21773
21774 /* List should start with `space'. */
21775 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21776 plist = XCDR (it->object);
21777
21778 /* Compute the width of the stretch. */
21779 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21780 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21781 {
21782 /* Absolute width `:width WIDTH' specified and valid. */
21783 zero_width_ok_p = 1;
21784 width = (int)tem;
21785 }
21786 else if (prop = Fplist_get (plist, QCrelative_width),
21787 NUMVAL (prop) > 0)
21788 {
21789 /* Relative width `:relative-width FACTOR' specified and valid.
21790 Compute the width of the characters having the `glyph'
21791 property. */
21792 struct it it2;
21793 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21794
21795 it2 = *it;
21796 if (it->multibyte_p)
21797 {
21798 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
21799 - IT_BYTEPOS (*it));
21800 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
21801 }
21802 else
21803 it2.c = *p, it2.len = 1;
21804
21805 it2.glyph_row = NULL;
21806 it2.what = IT_CHARACTER;
21807 x_produce_glyphs (&it2);
21808 width = NUMVAL (prop) * it2.pixel_width;
21809 }
21810 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21811 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21812 {
21813 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21814 align_to = (align_to < 0
21815 ? 0
21816 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21817 else if (align_to < 0)
21818 align_to = window_box_left_offset (it->w, TEXT_AREA);
21819 width = max (0, (int)tem + align_to - it->current_x);
21820 zero_width_ok_p = 1;
21821 }
21822 else
21823 /* Nothing specified -> width defaults to canonical char width. */
21824 width = FRAME_COLUMN_WIDTH (it->f);
21825
21826 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21827 width = 1;
21828
21829 /* Compute height. */
21830 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21831 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21832 {
21833 height = (int)tem;
21834 zero_height_ok_p = 1;
21835 }
21836 else if (prop = Fplist_get (plist, QCrelative_height),
21837 NUMVAL (prop) > 0)
21838 height = FONT_HEIGHT (font) * NUMVAL (prop);
21839 else
21840 height = FONT_HEIGHT (font);
21841
21842 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21843 height = 1;
21844
21845 /* Compute percentage of height used for ascent. If
21846 `:ascent ASCENT' is present and valid, use that. Otherwise,
21847 derive the ascent from the font in use. */
21848 if (prop = Fplist_get (plist, QCascent),
21849 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21850 ascent = height * NUMVAL (prop) / 100.0;
21851 else if (!NILP (prop)
21852 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21853 ascent = min (max (0, (int)tem), height);
21854 else
21855 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21856
21857 if (width > 0 && it->line_wrap != TRUNCATE
21858 && it->current_x + width > it->last_visible_x)
21859 width = it->last_visible_x - it->current_x - 1;
21860
21861 if (width > 0 && height > 0 && it->glyph_row)
21862 {
21863 Lisp_Object object = it->stack[it->sp - 1].string;
21864 if (!STRINGP (object))
21865 object = it->w->buffer;
21866 append_stretch_glyph (it, object, width, height, ascent);
21867 }
21868
21869 it->pixel_width = width;
21870 it->ascent = it->phys_ascent = ascent;
21871 it->descent = it->phys_descent = height - it->ascent;
21872 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21873
21874 take_vertical_position_into_account (it);
21875 }
21876
21877 /* Calculate line-height and line-spacing properties.
21878 An integer value specifies explicit pixel value.
21879 A float value specifies relative value to current face height.
21880 A cons (float . face-name) specifies relative value to
21881 height of specified face font.
21882
21883 Returns height in pixels, or nil. */
21884
21885
21886 static Lisp_Object
21887 calc_line_height_property (it, val, font, boff, override)
21888 struct it *it;
21889 Lisp_Object val;
21890 struct font *font;
21891 int boff, override;
21892 {
21893 Lisp_Object face_name = Qnil;
21894 int ascent, descent, height;
21895
21896 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21897 return val;
21898
21899 if (CONSP (val))
21900 {
21901 face_name = XCAR (val);
21902 val = XCDR (val);
21903 if (!NUMBERP (val))
21904 val = make_number (1);
21905 if (NILP (face_name))
21906 {
21907 height = it->ascent + it->descent;
21908 goto scale;
21909 }
21910 }
21911
21912 if (NILP (face_name))
21913 {
21914 font = FRAME_FONT (it->f);
21915 boff = FRAME_BASELINE_OFFSET (it->f);
21916 }
21917 else if (EQ (face_name, Qt))
21918 {
21919 override = 0;
21920 }
21921 else
21922 {
21923 int face_id;
21924 struct face *face;
21925
21926 face_id = lookup_named_face (it->f, face_name, 0);
21927 if (face_id < 0)
21928 return make_number (-1);
21929
21930 face = FACE_FROM_ID (it->f, face_id);
21931 font = face->font;
21932 if (font == NULL)
21933 return make_number (-1);
21934 boff = font->baseline_offset;
21935 if (font->vertical_centering)
21936 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21937 }
21938
21939 ascent = FONT_BASE (font) + boff;
21940 descent = FONT_DESCENT (font) - boff;
21941
21942 if (override)
21943 {
21944 it->override_ascent = ascent;
21945 it->override_descent = descent;
21946 it->override_boff = boff;
21947 }
21948
21949 height = ascent + descent;
21950
21951 scale:
21952 if (FLOATP (val))
21953 height = (int)(XFLOAT_DATA (val) * height);
21954 else if (INTEGERP (val))
21955 height *= XINT (val);
21956
21957 return make_number (height);
21958 }
21959
21960
21961 /* RIF:
21962 Produce glyphs/get display metrics for the display element IT is
21963 loaded with. See the description of struct it in dispextern.h
21964 for an overview of struct it. */
21965
21966 void
21967 x_produce_glyphs (it)
21968 struct it *it;
21969 {
21970 int extra_line_spacing = it->extra_line_spacing;
21971
21972 it->glyph_not_available_p = 0;
21973
21974 if (it->what == IT_CHARACTER)
21975 {
21976 XChar2b char2b;
21977 struct font *font;
21978 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21979 struct font_metrics *pcm;
21980 int font_not_found_p;
21981 int boff; /* baseline offset */
21982 /* We may change it->multibyte_p upon unibyte<->multibyte
21983 conversion. So, save the current value now and restore it
21984 later.
21985
21986 Note: It seems that we don't have to record multibyte_p in
21987 struct glyph because the character code itself tells whether
21988 or not the character is multibyte. Thus, in the future, we
21989 must consider eliminating the field `multibyte_p' in the
21990 struct glyph. */
21991 int saved_multibyte_p = it->multibyte_p;
21992
21993 /* Maybe translate single-byte characters to multibyte, or the
21994 other way. */
21995 it->char_to_display = it->c;
21996 if (!ASCII_BYTE_P (it->c)
21997 && ! it->multibyte_p)
21998 {
21999 if (SINGLE_BYTE_CHAR_P (it->c)
22000 && unibyte_display_via_language_environment)
22001 {
22002 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
22003
22004 /* get_next_display_element assures that this decoding
22005 never fails. */
22006 it->char_to_display = DECODE_CHAR (unibyte, it->c);
22007 it->multibyte_p = 1;
22008 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
22009 -1, Qnil);
22010 face = FACE_FROM_ID (it->f, it->face_id);
22011 }
22012 }
22013
22014 /* Get font to use. Encode IT->char_to_display. */
22015 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
22016 &char2b, it->multibyte_p, 0);
22017 font = face->font;
22018
22019 font_not_found_p = font == NULL;
22020 if (font_not_found_p)
22021 {
22022 /* When no suitable font found, display an empty box based
22023 on the metrics of the font of the default face (or what
22024 remapped). */
22025 struct face *no_font_face
22026 = FACE_FROM_ID (it->f,
22027 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
22028 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
22029 font = no_font_face->font;
22030 boff = font->baseline_offset;
22031 }
22032 else
22033 {
22034 boff = font->baseline_offset;
22035 if (font->vertical_centering)
22036 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22037 }
22038
22039 if (it->char_to_display >= ' '
22040 && (!it->multibyte_p || it->char_to_display < 128))
22041 {
22042 /* Either unibyte or ASCII. */
22043 int stretched_p;
22044
22045 it->nglyphs = 1;
22046
22047 pcm = get_per_char_metric (it->f, font, &char2b);
22048
22049 if (it->override_ascent >= 0)
22050 {
22051 it->ascent = it->override_ascent;
22052 it->descent = it->override_descent;
22053 boff = it->override_boff;
22054 }
22055 else
22056 {
22057 it->ascent = FONT_BASE (font) + boff;
22058 it->descent = FONT_DESCENT (font) - boff;
22059 }
22060
22061 if (pcm)
22062 {
22063 it->phys_ascent = pcm->ascent + boff;
22064 it->phys_descent = pcm->descent - boff;
22065 it->pixel_width = pcm->width;
22066 }
22067 else
22068 {
22069 it->glyph_not_available_p = 1;
22070 it->phys_ascent = it->ascent;
22071 it->phys_descent = it->descent;
22072 it->pixel_width = FONT_WIDTH (font);
22073 }
22074
22075 if (it->constrain_row_ascent_descent_p)
22076 {
22077 if (it->descent > it->max_descent)
22078 {
22079 it->ascent += it->descent - it->max_descent;
22080 it->descent = it->max_descent;
22081 }
22082 if (it->ascent > it->max_ascent)
22083 {
22084 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22085 it->ascent = it->max_ascent;
22086 }
22087 it->phys_ascent = min (it->phys_ascent, it->ascent);
22088 it->phys_descent = min (it->phys_descent, it->descent);
22089 extra_line_spacing = 0;
22090 }
22091
22092 /* If this is a space inside a region of text with
22093 `space-width' property, change its width. */
22094 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22095 if (stretched_p)
22096 it->pixel_width *= XFLOATINT (it->space_width);
22097
22098 /* If face has a box, add the box thickness to the character
22099 height. If character has a box line to the left and/or
22100 right, add the box line width to the character's width. */
22101 if (face->box != FACE_NO_BOX)
22102 {
22103 int thick = face->box_line_width;
22104
22105 if (thick > 0)
22106 {
22107 it->ascent += thick;
22108 it->descent += thick;
22109 }
22110 else
22111 thick = -thick;
22112
22113 if (it->start_of_box_run_p)
22114 it->pixel_width += thick;
22115 if (it->end_of_box_run_p)
22116 it->pixel_width += thick;
22117 }
22118
22119 /* If face has an overline, add the height of the overline
22120 (1 pixel) and a 1 pixel margin to the character height. */
22121 if (face->overline_p)
22122 it->ascent += overline_margin;
22123
22124 if (it->constrain_row_ascent_descent_p)
22125 {
22126 if (it->ascent > it->max_ascent)
22127 it->ascent = it->max_ascent;
22128 if (it->descent > it->max_descent)
22129 it->descent = it->max_descent;
22130 }
22131
22132 take_vertical_position_into_account (it);
22133
22134 /* If we have to actually produce glyphs, do it. */
22135 if (it->glyph_row)
22136 {
22137 if (stretched_p)
22138 {
22139 /* Translate a space with a `space-width' property
22140 into a stretch glyph. */
22141 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22142 / FONT_HEIGHT (font));
22143 append_stretch_glyph (it, it->object, it->pixel_width,
22144 it->ascent + it->descent, ascent);
22145 }
22146 else
22147 append_glyph (it);
22148
22149 /* If characters with lbearing or rbearing are displayed
22150 in this line, record that fact in a flag of the
22151 glyph row. This is used to optimize X output code. */
22152 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22153 it->glyph_row->contains_overlapping_glyphs_p = 1;
22154 }
22155 if (! stretched_p && it->pixel_width == 0)
22156 /* We assure that all visible glyphs have at least 1-pixel
22157 width. */
22158 it->pixel_width = 1;
22159 }
22160 else if (it->char_to_display == '\n')
22161 {
22162 /* A newline has no width, but we need the height of the
22163 line. But if previous part of the line sets a height,
22164 don't increase that height */
22165
22166 Lisp_Object height;
22167 Lisp_Object total_height = Qnil;
22168
22169 it->override_ascent = -1;
22170 it->pixel_width = 0;
22171 it->nglyphs = 0;
22172
22173 height = get_it_property(it, Qline_height);
22174 /* Split (line-height total-height) list */
22175 if (CONSP (height)
22176 && CONSP (XCDR (height))
22177 && NILP (XCDR (XCDR (height))))
22178 {
22179 total_height = XCAR (XCDR (height));
22180 height = XCAR (height);
22181 }
22182 height = calc_line_height_property(it, height, font, boff, 1);
22183
22184 if (it->override_ascent >= 0)
22185 {
22186 it->ascent = it->override_ascent;
22187 it->descent = it->override_descent;
22188 boff = it->override_boff;
22189 }
22190 else
22191 {
22192 it->ascent = FONT_BASE (font) + boff;
22193 it->descent = FONT_DESCENT (font) - boff;
22194 }
22195
22196 if (EQ (height, Qt))
22197 {
22198 if (it->descent > it->max_descent)
22199 {
22200 it->ascent += it->descent - it->max_descent;
22201 it->descent = it->max_descent;
22202 }
22203 if (it->ascent > it->max_ascent)
22204 {
22205 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22206 it->ascent = it->max_ascent;
22207 }
22208 it->phys_ascent = min (it->phys_ascent, it->ascent);
22209 it->phys_descent = min (it->phys_descent, it->descent);
22210 it->constrain_row_ascent_descent_p = 1;
22211 extra_line_spacing = 0;
22212 }
22213 else
22214 {
22215 Lisp_Object spacing;
22216
22217 it->phys_ascent = it->ascent;
22218 it->phys_descent = it->descent;
22219
22220 if ((it->max_ascent > 0 || it->max_descent > 0)
22221 && face->box != FACE_NO_BOX
22222 && face->box_line_width > 0)
22223 {
22224 it->ascent += face->box_line_width;
22225 it->descent += face->box_line_width;
22226 }
22227 if (!NILP (height)
22228 && XINT (height) > it->ascent + it->descent)
22229 it->ascent = XINT (height) - it->descent;
22230
22231 if (!NILP (total_height))
22232 spacing = calc_line_height_property(it, total_height, font, boff, 0);
22233 else
22234 {
22235 spacing = get_it_property(it, Qline_spacing);
22236 spacing = calc_line_height_property(it, spacing, font, boff, 0);
22237 }
22238 if (INTEGERP (spacing))
22239 {
22240 extra_line_spacing = XINT (spacing);
22241 if (!NILP (total_height))
22242 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22243 }
22244 }
22245 }
22246 else if (it->char_to_display == '\t')
22247 {
22248 if (font->space_width > 0)
22249 {
22250 int tab_width = it->tab_width * font->space_width;
22251 int x = it->current_x + it->continuation_lines_width;
22252 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22253
22254 /* If the distance from the current position to the next tab
22255 stop is less than a space character width, use the
22256 tab stop after that. */
22257 if (next_tab_x - x < font->space_width)
22258 next_tab_x += tab_width;
22259
22260 it->pixel_width = next_tab_x - x;
22261 it->nglyphs = 1;
22262 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22263 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22264
22265 if (it->glyph_row)
22266 {
22267 append_stretch_glyph (it, it->object, it->pixel_width,
22268 it->ascent + it->descent, it->ascent);
22269 }
22270 }
22271 else
22272 {
22273 it->pixel_width = 0;
22274 it->nglyphs = 1;
22275 }
22276 }
22277 else
22278 {
22279 /* A multi-byte character. Assume that the display width of the
22280 character is the width of the character multiplied by the
22281 width of the font. */
22282
22283 /* If we found a font, this font should give us the right
22284 metrics. If we didn't find a font, use the frame's
22285 default font and calculate the width of the character by
22286 multiplying the width of font by the width of the
22287 character. */
22288
22289 pcm = get_per_char_metric (it->f, font, &char2b);
22290
22291 if (font_not_found_p || !pcm)
22292 {
22293 int char_width = CHAR_WIDTH (it->char_to_display);
22294
22295 if (char_width == 0)
22296 /* This is a non spacing character. But, as we are
22297 going to display an empty box, the box must occupy
22298 at least one column. */
22299 char_width = 1;
22300 it->glyph_not_available_p = 1;
22301 it->pixel_width = font->space_width * char_width;
22302 it->phys_ascent = FONT_BASE (font) + boff;
22303 it->phys_descent = FONT_DESCENT (font) - boff;
22304 }
22305 else
22306 {
22307 it->pixel_width = pcm->width;
22308 it->phys_ascent = pcm->ascent + boff;
22309 it->phys_descent = pcm->descent - boff;
22310 if (it->glyph_row
22311 && (pcm->lbearing < 0
22312 || pcm->rbearing > pcm->width))
22313 it->glyph_row->contains_overlapping_glyphs_p = 1;
22314 }
22315 it->nglyphs = 1;
22316 it->ascent = FONT_BASE (font) + boff;
22317 it->descent = FONT_DESCENT (font) - boff;
22318 if (face->box != FACE_NO_BOX)
22319 {
22320 int thick = face->box_line_width;
22321
22322 if (thick > 0)
22323 {
22324 it->ascent += thick;
22325 it->descent += thick;
22326 }
22327 else
22328 thick = - thick;
22329
22330 if (it->start_of_box_run_p)
22331 it->pixel_width += thick;
22332 if (it->end_of_box_run_p)
22333 it->pixel_width += thick;
22334 }
22335
22336 /* If face has an overline, add the height of the overline
22337 (1 pixel) and a 1 pixel margin to the character height. */
22338 if (face->overline_p)
22339 it->ascent += overline_margin;
22340
22341 take_vertical_position_into_account (it);
22342
22343 if (it->ascent < 0)
22344 it->ascent = 0;
22345 if (it->descent < 0)
22346 it->descent = 0;
22347
22348 if (it->glyph_row)
22349 append_glyph (it);
22350 if (it->pixel_width == 0)
22351 /* We assure that all visible glyphs have at least 1-pixel
22352 width. */
22353 it->pixel_width = 1;
22354 }
22355 it->multibyte_p = saved_multibyte_p;
22356 }
22357 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22358 {
22359 /* A static composition.
22360
22361 Note: A composition is represented as one glyph in the
22362 glyph matrix. There are no padding glyphs.
22363
22364 Important note: pixel_width, ascent, and descent are the
22365 values of what is drawn by draw_glyphs (i.e. the values of
22366 the overall glyphs composed). */
22367 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22368 int boff; /* baseline offset */
22369 struct composition *cmp = composition_table[it->cmp_it.id];
22370 int glyph_len = cmp->glyph_len;
22371 struct font *font = face->font;
22372
22373 it->nglyphs = 1;
22374
22375 /* If we have not yet calculated pixel size data of glyphs of
22376 the composition for the current face font, calculate them
22377 now. Theoretically, we have to check all fonts for the
22378 glyphs, but that requires much time and memory space. So,
22379 here we check only the font of the first glyph. This may
22380 lead to incorrect display, but it's very rare, and C-l
22381 (recenter-top-bottom) can correct the display anyway. */
22382 if (! cmp->font || cmp->font != font)
22383 {
22384 /* Ascent and descent of the font of the first character
22385 of this composition (adjusted by baseline offset).
22386 Ascent and descent of overall glyphs should not be less
22387 than these, respectively. */
22388 int font_ascent, font_descent, font_height;
22389 /* Bounding box of the overall glyphs. */
22390 int leftmost, rightmost, lowest, highest;
22391 int lbearing, rbearing;
22392 int i, width, ascent, descent;
22393 int left_padded = 0, right_padded = 0;
22394 int c;
22395 XChar2b char2b;
22396 struct font_metrics *pcm;
22397 int font_not_found_p;
22398 int pos;
22399
22400 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22401 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22402 break;
22403 if (glyph_len < cmp->glyph_len)
22404 right_padded = 1;
22405 for (i = 0; i < glyph_len; i++)
22406 {
22407 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22408 break;
22409 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22410 }
22411 if (i > 0)
22412 left_padded = 1;
22413
22414 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22415 : IT_CHARPOS (*it));
22416 /* If no suitable font is found, use the default font. */
22417 font_not_found_p = font == NULL;
22418 if (font_not_found_p)
22419 {
22420 face = face->ascii_face;
22421 font = face->font;
22422 }
22423 boff = font->baseline_offset;
22424 if (font->vertical_centering)
22425 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22426 font_ascent = FONT_BASE (font) + boff;
22427 font_descent = FONT_DESCENT (font) - boff;
22428 font_height = FONT_HEIGHT (font);
22429
22430 cmp->font = (void *) font;
22431
22432 pcm = NULL;
22433 if (! font_not_found_p)
22434 {
22435 get_char_face_and_encoding (it->f, c, it->face_id,
22436 &char2b, it->multibyte_p, 0);
22437 pcm = get_per_char_metric (it->f, font, &char2b);
22438 }
22439
22440 /* Initialize the bounding box. */
22441 if (pcm)
22442 {
22443 width = pcm->width;
22444 ascent = pcm->ascent;
22445 descent = pcm->descent;
22446 lbearing = pcm->lbearing;
22447 rbearing = pcm->rbearing;
22448 }
22449 else
22450 {
22451 width = FONT_WIDTH (font);
22452 ascent = FONT_BASE (font);
22453 descent = FONT_DESCENT (font);
22454 lbearing = 0;
22455 rbearing = width;
22456 }
22457
22458 rightmost = width;
22459 leftmost = 0;
22460 lowest = - descent + boff;
22461 highest = ascent + boff;
22462
22463 if (! font_not_found_p
22464 && font->default_ascent
22465 && CHAR_TABLE_P (Vuse_default_ascent)
22466 && !NILP (Faref (Vuse_default_ascent,
22467 make_number (it->char_to_display))))
22468 highest = font->default_ascent + boff;
22469
22470 /* Draw the first glyph at the normal position. It may be
22471 shifted to right later if some other glyphs are drawn
22472 at the left. */
22473 cmp->offsets[i * 2] = 0;
22474 cmp->offsets[i * 2 + 1] = boff;
22475 cmp->lbearing = lbearing;
22476 cmp->rbearing = rbearing;
22477
22478 /* Set cmp->offsets for the remaining glyphs. */
22479 for (i++; i < glyph_len; i++)
22480 {
22481 int left, right, btm, top;
22482 int ch = COMPOSITION_GLYPH (cmp, i);
22483 int face_id;
22484 struct face *this_face;
22485 int this_boff;
22486
22487 if (ch == '\t')
22488 ch = ' ';
22489 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22490 this_face = FACE_FROM_ID (it->f, face_id);
22491 font = this_face->font;
22492
22493 if (font == NULL)
22494 pcm = NULL;
22495 else
22496 {
22497 this_boff = font->baseline_offset;
22498 if (font->vertical_centering)
22499 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22500 get_char_face_and_encoding (it->f, ch, face_id,
22501 &char2b, it->multibyte_p, 0);
22502 pcm = get_per_char_metric (it->f, font, &char2b);
22503 }
22504 if (! pcm)
22505 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22506 else
22507 {
22508 width = pcm->width;
22509 ascent = pcm->ascent;
22510 descent = pcm->descent;
22511 lbearing = pcm->lbearing;
22512 rbearing = pcm->rbearing;
22513 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22514 {
22515 /* Relative composition with or without
22516 alternate chars. */
22517 left = (leftmost + rightmost - width) / 2;
22518 btm = - descent + boff;
22519 if (font->relative_compose
22520 && (! CHAR_TABLE_P (Vignore_relative_composition)
22521 || NILP (Faref (Vignore_relative_composition,
22522 make_number (ch)))))
22523 {
22524
22525 if (- descent >= font->relative_compose)
22526 /* One extra pixel between two glyphs. */
22527 btm = highest + 1;
22528 else if (ascent <= 0)
22529 /* One extra pixel between two glyphs. */
22530 btm = lowest - 1 - ascent - descent;
22531 }
22532 }
22533 else
22534 {
22535 /* A composition rule is specified by an integer
22536 value that encodes global and new reference
22537 points (GREF and NREF). GREF and NREF are
22538 specified by numbers as below:
22539
22540 0---1---2 -- ascent
22541 | |
22542 | |
22543 | |
22544 9--10--11 -- center
22545 | |
22546 ---3---4---5--- baseline
22547 | |
22548 6---7---8 -- descent
22549 */
22550 int rule = COMPOSITION_RULE (cmp, i);
22551 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22552
22553 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22554 grefx = gref % 3, nrefx = nref % 3;
22555 grefy = gref / 3, nrefy = nref / 3;
22556 if (xoff)
22557 xoff = font_height * (xoff - 128) / 256;
22558 if (yoff)
22559 yoff = font_height * (yoff - 128) / 256;
22560
22561 left = (leftmost
22562 + grefx * (rightmost - leftmost) / 2
22563 - nrefx * width / 2
22564 + xoff);
22565
22566 btm = ((grefy == 0 ? highest
22567 : grefy == 1 ? 0
22568 : grefy == 2 ? lowest
22569 : (highest + lowest) / 2)
22570 - (nrefy == 0 ? ascent + descent
22571 : nrefy == 1 ? descent - boff
22572 : nrefy == 2 ? 0
22573 : (ascent + descent) / 2)
22574 + yoff);
22575 }
22576
22577 cmp->offsets[i * 2] = left;
22578 cmp->offsets[i * 2 + 1] = btm + descent;
22579
22580 /* Update the bounding box of the overall glyphs. */
22581 if (width > 0)
22582 {
22583 right = left + width;
22584 if (left < leftmost)
22585 leftmost = left;
22586 if (right > rightmost)
22587 rightmost = right;
22588 }
22589 top = btm + descent + ascent;
22590 if (top > highest)
22591 highest = top;
22592 if (btm < lowest)
22593 lowest = btm;
22594
22595 if (cmp->lbearing > left + lbearing)
22596 cmp->lbearing = left + lbearing;
22597 if (cmp->rbearing < left + rbearing)
22598 cmp->rbearing = left + rbearing;
22599 }
22600 }
22601
22602 /* If there are glyphs whose x-offsets are negative,
22603 shift all glyphs to the right and make all x-offsets
22604 non-negative. */
22605 if (leftmost < 0)
22606 {
22607 for (i = 0; i < cmp->glyph_len; i++)
22608 cmp->offsets[i * 2] -= leftmost;
22609 rightmost -= leftmost;
22610 cmp->lbearing -= leftmost;
22611 cmp->rbearing -= leftmost;
22612 }
22613
22614 if (left_padded && cmp->lbearing < 0)
22615 {
22616 for (i = 0; i < cmp->glyph_len; i++)
22617 cmp->offsets[i * 2] -= cmp->lbearing;
22618 rightmost -= cmp->lbearing;
22619 cmp->rbearing -= cmp->lbearing;
22620 cmp->lbearing = 0;
22621 }
22622 if (right_padded && rightmost < cmp->rbearing)
22623 {
22624 rightmost = cmp->rbearing;
22625 }
22626
22627 cmp->pixel_width = rightmost;
22628 cmp->ascent = highest;
22629 cmp->descent = - lowest;
22630 if (cmp->ascent < font_ascent)
22631 cmp->ascent = font_ascent;
22632 if (cmp->descent < font_descent)
22633 cmp->descent = font_descent;
22634 }
22635
22636 if (it->glyph_row
22637 && (cmp->lbearing < 0
22638 || cmp->rbearing > cmp->pixel_width))
22639 it->glyph_row->contains_overlapping_glyphs_p = 1;
22640
22641 it->pixel_width = cmp->pixel_width;
22642 it->ascent = it->phys_ascent = cmp->ascent;
22643 it->descent = it->phys_descent = cmp->descent;
22644 if (face->box != FACE_NO_BOX)
22645 {
22646 int thick = face->box_line_width;
22647
22648 if (thick > 0)
22649 {
22650 it->ascent += thick;
22651 it->descent += thick;
22652 }
22653 else
22654 thick = - thick;
22655
22656 if (it->start_of_box_run_p)
22657 it->pixel_width += thick;
22658 if (it->end_of_box_run_p)
22659 it->pixel_width += thick;
22660 }
22661
22662 /* If face has an overline, add the height of the overline
22663 (1 pixel) and a 1 pixel margin to the character height. */
22664 if (face->overline_p)
22665 it->ascent += overline_margin;
22666
22667 take_vertical_position_into_account (it);
22668 if (it->ascent < 0)
22669 it->ascent = 0;
22670 if (it->descent < 0)
22671 it->descent = 0;
22672
22673 if (it->glyph_row)
22674 append_composite_glyph (it);
22675 }
22676 else if (it->what == IT_COMPOSITION)
22677 {
22678 /* A dynamic (automatic) composition. */
22679 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22680 Lisp_Object gstring;
22681 struct font_metrics metrics;
22682
22683 gstring = composition_gstring_from_id (it->cmp_it.id);
22684 it->pixel_width
22685 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22686 &metrics);
22687 if (it->glyph_row
22688 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22689 it->glyph_row->contains_overlapping_glyphs_p = 1;
22690 it->ascent = it->phys_ascent = metrics.ascent;
22691 it->descent = it->phys_descent = metrics.descent;
22692 if (face->box != FACE_NO_BOX)
22693 {
22694 int thick = face->box_line_width;
22695
22696 if (thick > 0)
22697 {
22698 it->ascent += thick;
22699 it->descent += thick;
22700 }
22701 else
22702 thick = - thick;
22703
22704 if (it->start_of_box_run_p)
22705 it->pixel_width += thick;
22706 if (it->end_of_box_run_p)
22707 it->pixel_width += thick;
22708 }
22709 /* If face has an overline, add the height of the overline
22710 (1 pixel) and a 1 pixel margin to the character height. */
22711 if (face->overline_p)
22712 it->ascent += overline_margin;
22713 take_vertical_position_into_account (it);
22714 if (it->ascent < 0)
22715 it->ascent = 0;
22716 if (it->descent < 0)
22717 it->descent = 0;
22718
22719 if (it->glyph_row)
22720 append_composite_glyph (it);
22721 }
22722 else if (it->what == IT_IMAGE)
22723 produce_image_glyph (it);
22724 else if (it->what == IT_STRETCH)
22725 produce_stretch_glyph (it);
22726
22727 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22728 because this isn't true for images with `:ascent 100'. */
22729 xassert (it->ascent >= 0 && it->descent >= 0);
22730 if (it->area == TEXT_AREA)
22731 it->current_x += it->pixel_width;
22732
22733 if (extra_line_spacing > 0)
22734 {
22735 it->descent += extra_line_spacing;
22736 if (extra_line_spacing > it->max_extra_line_spacing)
22737 it->max_extra_line_spacing = extra_line_spacing;
22738 }
22739
22740 it->max_ascent = max (it->max_ascent, it->ascent);
22741 it->max_descent = max (it->max_descent, it->descent);
22742 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22743 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22744 }
22745
22746 /* EXPORT for RIF:
22747 Output LEN glyphs starting at START at the nominal cursor position.
22748 Advance the nominal cursor over the text. The global variable
22749 updated_window contains the window being updated, updated_row is
22750 the glyph row being updated, and updated_area is the area of that
22751 row being updated. */
22752
22753 void
22754 x_write_glyphs (start, len)
22755 struct glyph *start;
22756 int len;
22757 {
22758 int x, hpos;
22759
22760 xassert (updated_window && updated_row);
22761 BLOCK_INPUT;
22762
22763 /* Write glyphs. */
22764
22765 hpos = start - updated_row->glyphs[updated_area];
22766 x = draw_glyphs (updated_window, output_cursor.x,
22767 updated_row, updated_area,
22768 hpos, hpos + len,
22769 DRAW_NORMAL_TEXT, 0);
22770
22771 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22772 if (updated_area == TEXT_AREA
22773 && updated_window->phys_cursor_on_p
22774 && updated_window->phys_cursor.vpos == output_cursor.vpos
22775 && updated_window->phys_cursor.hpos >= hpos
22776 && updated_window->phys_cursor.hpos < hpos + len)
22777 updated_window->phys_cursor_on_p = 0;
22778
22779 UNBLOCK_INPUT;
22780
22781 /* Advance the output cursor. */
22782 output_cursor.hpos += len;
22783 output_cursor.x = x;
22784 }
22785
22786
22787 /* EXPORT for RIF:
22788 Insert LEN glyphs from START at the nominal cursor position. */
22789
22790 void
22791 x_insert_glyphs (start, len)
22792 struct glyph *start;
22793 int len;
22794 {
22795 struct frame *f;
22796 struct window *w;
22797 int line_height, shift_by_width, shifted_region_width;
22798 struct glyph_row *row;
22799 struct glyph *glyph;
22800 int frame_x, frame_y;
22801 EMACS_INT hpos;
22802
22803 xassert (updated_window && updated_row);
22804 BLOCK_INPUT;
22805 w = updated_window;
22806 f = XFRAME (WINDOW_FRAME (w));
22807
22808 /* Get the height of the line we are in. */
22809 row = updated_row;
22810 line_height = row->height;
22811
22812 /* Get the width of the glyphs to insert. */
22813 shift_by_width = 0;
22814 for (glyph = start; glyph < start + len; ++glyph)
22815 shift_by_width += glyph->pixel_width;
22816
22817 /* Get the width of the region to shift right. */
22818 shifted_region_width = (window_box_width (w, updated_area)
22819 - output_cursor.x
22820 - shift_by_width);
22821
22822 /* Shift right. */
22823 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22824 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22825
22826 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22827 line_height, shift_by_width);
22828
22829 /* Write the glyphs. */
22830 hpos = start - row->glyphs[updated_area];
22831 draw_glyphs (w, output_cursor.x, row, updated_area,
22832 hpos, hpos + len,
22833 DRAW_NORMAL_TEXT, 0);
22834
22835 /* Advance the output cursor. */
22836 output_cursor.hpos += len;
22837 output_cursor.x += shift_by_width;
22838 UNBLOCK_INPUT;
22839 }
22840
22841
22842 /* EXPORT for RIF:
22843 Erase the current text line from the nominal cursor position
22844 (inclusive) to pixel column TO_X (exclusive). The idea is that
22845 everything from TO_X onward is already erased.
22846
22847 TO_X is a pixel position relative to updated_area of
22848 updated_window. TO_X == -1 means clear to the end of this area. */
22849
22850 void
22851 x_clear_end_of_line (to_x)
22852 int to_x;
22853 {
22854 struct frame *f;
22855 struct window *w = updated_window;
22856 int max_x, min_y, max_y;
22857 int from_x, from_y, to_y;
22858
22859 xassert (updated_window && updated_row);
22860 f = XFRAME (w->frame);
22861
22862 if (updated_row->full_width_p)
22863 max_x = WINDOW_TOTAL_WIDTH (w);
22864 else
22865 max_x = window_box_width (w, updated_area);
22866 max_y = window_text_bottom_y (w);
22867
22868 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22869 of window. For TO_X > 0, truncate to end of drawing area. */
22870 if (to_x == 0)
22871 return;
22872 else if (to_x < 0)
22873 to_x = max_x;
22874 else
22875 to_x = min (to_x, max_x);
22876
22877 to_y = min (max_y, output_cursor.y + updated_row->height);
22878
22879 /* Notice if the cursor will be cleared by this operation. */
22880 if (!updated_row->full_width_p)
22881 notice_overwritten_cursor (w, updated_area,
22882 output_cursor.x, -1,
22883 updated_row->y,
22884 MATRIX_ROW_BOTTOM_Y (updated_row));
22885
22886 from_x = output_cursor.x;
22887
22888 /* Translate to frame coordinates. */
22889 if (updated_row->full_width_p)
22890 {
22891 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22892 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22893 }
22894 else
22895 {
22896 int area_left = window_box_left (w, updated_area);
22897 from_x += area_left;
22898 to_x += area_left;
22899 }
22900
22901 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22902 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22903 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22904
22905 /* Prevent inadvertently clearing to end of the X window. */
22906 if (to_x > from_x && to_y > from_y)
22907 {
22908 BLOCK_INPUT;
22909 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22910 to_x - from_x, to_y - from_y);
22911 UNBLOCK_INPUT;
22912 }
22913 }
22914
22915 #endif /* HAVE_WINDOW_SYSTEM */
22916
22917
22918 \f
22919 /***********************************************************************
22920 Cursor types
22921 ***********************************************************************/
22922
22923 /* Value is the internal representation of the specified cursor type
22924 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22925 of the bar cursor. */
22926
22927 static enum text_cursor_kinds
22928 get_specified_cursor_type (arg, width)
22929 Lisp_Object arg;
22930 int *width;
22931 {
22932 enum text_cursor_kinds type;
22933
22934 if (NILP (arg))
22935 return NO_CURSOR;
22936
22937 if (EQ (arg, Qbox))
22938 return FILLED_BOX_CURSOR;
22939
22940 if (EQ (arg, Qhollow))
22941 return HOLLOW_BOX_CURSOR;
22942
22943 if (EQ (arg, Qbar))
22944 {
22945 *width = 2;
22946 return BAR_CURSOR;
22947 }
22948
22949 if (CONSP (arg)
22950 && EQ (XCAR (arg), Qbar)
22951 && INTEGERP (XCDR (arg))
22952 && XINT (XCDR (arg)) >= 0)
22953 {
22954 *width = XINT (XCDR (arg));
22955 return BAR_CURSOR;
22956 }
22957
22958 if (EQ (arg, Qhbar))
22959 {
22960 *width = 2;
22961 return HBAR_CURSOR;
22962 }
22963
22964 if (CONSP (arg)
22965 && EQ (XCAR (arg), Qhbar)
22966 && INTEGERP (XCDR (arg))
22967 && XINT (XCDR (arg)) >= 0)
22968 {
22969 *width = XINT (XCDR (arg));
22970 return HBAR_CURSOR;
22971 }
22972
22973 /* Treat anything unknown as "hollow box cursor".
22974 It was bad to signal an error; people have trouble fixing
22975 .Xdefaults with Emacs, when it has something bad in it. */
22976 type = HOLLOW_BOX_CURSOR;
22977
22978 return type;
22979 }
22980
22981 /* Set the default cursor types for specified frame. */
22982 void
22983 set_frame_cursor_types (f, arg)
22984 struct frame *f;
22985 Lisp_Object arg;
22986 {
22987 int width;
22988 Lisp_Object tem;
22989
22990 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22991 FRAME_CURSOR_WIDTH (f) = width;
22992
22993 /* By default, set up the blink-off state depending on the on-state. */
22994
22995 tem = Fassoc (arg, Vblink_cursor_alist);
22996 if (!NILP (tem))
22997 {
22998 FRAME_BLINK_OFF_CURSOR (f)
22999 = get_specified_cursor_type (XCDR (tem), &width);
23000 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23001 }
23002 else
23003 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23004 }
23005
23006
23007 /* Return the cursor we want to be displayed in window W. Return
23008 width of bar/hbar cursor through WIDTH arg. Return with
23009 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23010 (i.e. if the `system caret' should track this cursor).
23011
23012 In a mini-buffer window, we want the cursor only to appear if we
23013 are reading input from this window. For the selected window, we
23014 want the cursor type given by the frame parameter or buffer local
23015 setting of cursor-type. If explicitly marked off, draw no cursor.
23016 In all other cases, we want a hollow box cursor. */
23017
23018 static enum text_cursor_kinds
23019 get_window_cursor_type (w, glyph, width, active_cursor)
23020 struct window *w;
23021 struct glyph *glyph;
23022 int *width;
23023 int *active_cursor;
23024 {
23025 struct frame *f = XFRAME (w->frame);
23026 struct buffer *b = XBUFFER (w->buffer);
23027 int cursor_type = DEFAULT_CURSOR;
23028 Lisp_Object alt_cursor;
23029 int non_selected = 0;
23030
23031 *active_cursor = 1;
23032
23033 /* Echo area */
23034 if (cursor_in_echo_area
23035 && FRAME_HAS_MINIBUF_P (f)
23036 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23037 {
23038 if (w == XWINDOW (echo_area_window))
23039 {
23040 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23041 {
23042 *width = FRAME_CURSOR_WIDTH (f);
23043 return FRAME_DESIRED_CURSOR (f);
23044 }
23045 else
23046 return get_specified_cursor_type (b->cursor_type, width);
23047 }
23048
23049 *active_cursor = 0;
23050 non_selected = 1;
23051 }
23052
23053 /* Detect a nonselected window or nonselected frame. */
23054 else if (w != XWINDOW (f->selected_window)
23055 #ifdef HAVE_WINDOW_SYSTEM
23056 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
23057 #endif
23058 )
23059 {
23060 *active_cursor = 0;
23061
23062 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23063 return NO_CURSOR;
23064
23065 non_selected = 1;
23066 }
23067
23068 /* Never display a cursor in a window in which cursor-type is nil. */
23069 if (NILP (b->cursor_type))
23070 return NO_CURSOR;
23071
23072 /* Get the normal cursor type for this window. */
23073 if (EQ (b->cursor_type, Qt))
23074 {
23075 cursor_type = FRAME_DESIRED_CURSOR (f);
23076 *width = FRAME_CURSOR_WIDTH (f);
23077 }
23078 else
23079 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23080
23081 /* Use cursor-in-non-selected-windows instead
23082 for non-selected window or frame. */
23083 if (non_selected)
23084 {
23085 alt_cursor = b->cursor_in_non_selected_windows;
23086 if (!EQ (Qt, alt_cursor))
23087 return get_specified_cursor_type (alt_cursor, width);
23088 /* t means modify the normal cursor type. */
23089 if (cursor_type == FILLED_BOX_CURSOR)
23090 cursor_type = HOLLOW_BOX_CURSOR;
23091 else if (cursor_type == BAR_CURSOR && *width > 1)
23092 --*width;
23093 return cursor_type;
23094 }
23095
23096 /* Use normal cursor if not blinked off. */
23097 if (!w->cursor_off_p)
23098 {
23099 #ifdef HAVE_WINDOW_SYSTEM
23100 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23101 {
23102 if (cursor_type == FILLED_BOX_CURSOR)
23103 {
23104 /* Using a block cursor on large images can be very annoying.
23105 So use a hollow cursor for "large" images.
23106 If image is not transparent (no mask), also use hollow cursor. */
23107 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23108 if (img != NULL && IMAGEP (img->spec))
23109 {
23110 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23111 where N = size of default frame font size.
23112 This should cover most of the "tiny" icons people may use. */
23113 if (!img->mask
23114 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23115 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23116 cursor_type = HOLLOW_BOX_CURSOR;
23117 }
23118 }
23119 else if (cursor_type != NO_CURSOR)
23120 {
23121 /* Display current only supports BOX and HOLLOW cursors for images.
23122 So for now, unconditionally use a HOLLOW cursor when cursor is
23123 not a solid box cursor. */
23124 cursor_type = HOLLOW_BOX_CURSOR;
23125 }
23126 }
23127 #endif
23128 return cursor_type;
23129 }
23130
23131 /* Cursor is blinked off, so determine how to "toggle" it. */
23132
23133 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23134 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23135 return get_specified_cursor_type (XCDR (alt_cursor), width);
23136
23137 /* Then see if frame has specified a specific blink off cursor type. */
23138 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23139 {
23140 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23141 return FRAME_BLINK_OFF_CURSOR (f);
23142 }
23143
23144 #if 0
23145 /* Some people liked having a permanently visible blinking cursor,
23146 while others had very strong opinions against it. So it was
23147 decided to remove it. KFS 2003-09-03 */
23148
23149 /* Finally perform built-in cursor blinking:
23150 filled box <-> hollow box
23151 wide [h]bar <-> narrow [h]bar
23152 narrow [h]bar <-> no cursor
23153 other type <-> no cursor */
23154
23155 if (cursor_type == FILLED_BOX_CURSOR)
23156 return HOLLOW_BOX_CURSOR;
23157
23158 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23159 {
23160 *width = 1;
23161 return cursor_type;
23162 }
23163 #endif
23164
23165 return NO_CURSOR;
23166 }
23167
23168
23169 #ifdef HAVE_WINDOW_SYSTEM
23170
23171 /* Notice when the text cursor of window W has been completely
23172 overwritten by a drawing operation that outputs glyphs in AREA
23173 starting at X0 and ending at X1 in the line starting at Y0 and
23174 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23175 the rest of the line after X0 has been written. Y coordinates
23176 are window-relative. */
23177
23178 static void
23179 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
23180 struct window *w;
23181 enum glyph_row_area area;
23182 int x0, y0, x1, y1;
23183 {
23184 int cx0, cx1, cy0, cy1;
23185 struct glyph_row *row;
23186
23187 if (!w->phys_cursor_on_p)
23188 return;
23189 if (area != TEXT_AREA)
23190 return;
23191
23192 if (w->phys_cursor.vpos < 0
23193 || w->phys_cursor.vpos >= w->current_matrix->nrows
23194 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23195 !(row->enabled_p && row->displays_text_p)))
23196 return;
23197
23198 if (row->cursor_in_fringe_p)
23199 {
23200 row->cursor_in_fringe_p = 0;
23201 draw_fringe_bitmap (w, row, 0);
23202 w->phys_cursor_on_p = 0;
23203 return;
23204 }
23205
23206 cx0 = w->phys_cursor.x;
23207 cx1 = cx0 + w->phys_cursor_width;
23208 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23209 return;
23210
23211 /* The cursor image will be completely removed from the
23212 screen if the output area intersects the cursor area in
23213 y-direction. When we draw in [y0 y1[, and some part of
23214 the cursor is at y < y0, that part must have been drawn
23215 before. When scrolling, the cursor is erased before
23216 actually scrolling, so we don't come here. When not
23217 scrolling, the rows above the old cursor row must have
23218 changed, and in this case these rows must have written
23219 over the cursor image.
23220
23221 Likewise if part of the cursor is below y1, with the
23222 exception of the cursor being in the first blank row at
23223 the buffer and window end because update_text_area
23224 doesn't draw that row. (Except when it does, but
23225 that's handled in update_text_area.) */
23226
23227 cy0 = w->phys_cursor.y;
23228 cy1 = cy0 + w->phys_cursor_height;
23229 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23230 return;
23231
23232 w->phys_cursor_on_p = 0;
23233 }
23234
23235 #endif /* HAVE_WINDOW_SYSTEM */
23236
23237 \f
23238 /************************************************************************
23239 Mouse Face
23240 ************************************************************************/
23241
23242 #ifdef HAVE_WINDOW_SYSTEM
23243
23244 /* EXPORT for RIF:
23245 Fix the display of area AREA of overlapping row ROW in window W
23246 with respect to the overlapping part OVERLAPS. */
23247
23248 void
23249 x_fix_overlapping_area (w, row, area, overlaps)
23250 struct window *w;
23251 struct glyph_row *row;
23252 enum glyph_row_area area;
23253 int overlaps;
23254 {
23255 int i, x;
23256
23257 BLOCK_INPUT;
23258
23259 x = 0;
23260 for (i = 0; i < row->used[area];)
23261 {
23262 if (row->glyphs[area][i].overlaps_vertically_p)
23263 {
23264 int start = i, start_x = x;
23265
23266 do
23267 {
23268 x += row->glyphs[area][i].pixel_width;
23269 ++i;
23270 }
23271 while (i < row->used[area]
23272 && row->glyphs[area][i].overlaps_vertically_p);
23273
23274 draw_glyphs (w, start_x, row, area,
23275 start, i,
23276 DRAW_NORMAL_TEXT, overlaps);
23277 }
23278 else
23279 {
23280 x += row->glyphs[area][i].pixel_width;
23281 ++i;
23282 }
23283 }
23284
23285 UNBLOCK_INPUT;
23286 }
23287
23288
23289 /* EXPORT:
23290 Draw the cursor glyph of window W in glyph row ROW. See the
23291 comment of draw_glyphs for the meaning of HL. */
23292
23293 void
23294 draw_phys_cursor_glyph (w, row, hl)
23295 struct window *w;
23296 struct glyph_row *row;
23297 enum draw_glyphs_face hl;
23298 {
23299 /* If cursor hpos is out of bounds, don't draw garbage. This can
23300 happen in mini-buffer windows when switching between echo area
23301 glyphs and mini-buffer. */
23302 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
23303 {
23304 int on_p = w->phys_cursor_on_p;
23305 int x1;
23306 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23307 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23308 hl, 0);
23309 w->phys_cursor_on_p = on_p;
23310
23311 if (hl == DRAW_CURSOR)
23312 w->phys_cursor_width = x1 - w->phys_cursor.x;
23313 /* When we erase the cursor, and ROW is overlapped by other
23314 rows, make sure that these overlapping parts of other rows
23315 are redrawn. */
23316 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23317 {
23318 w->phys_cursor_width = x1 - w->phys_cursor.x;
23319
23320 if (row > w->current_matrix->rows
23321 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23322 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23323 OVERLAPS_ERASED_CURSOR);
23324
23325 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23326 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23327 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23328 OVERLAPS_ERASED_CURSOR);
23329 }
23330 }
23331 }
23332
23333
23334 /* EXPORT:
23335 Erase the image of a cursor of window W from the screen. */
23336
23337 void
23338 erase_phys_cursor (w)
23339 struct window *w;
23340 {
23341 struct frame *f = XFRAME (w->frame);
23342 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23343 int hpos = w->phys_cursor.hpos;
23344 int vpos = w->phys_cursor.vpos;
23345 int mouse_face_here_p = 0;
23346 struct glyph_matrix *active_glyphs = w->current_matrix;
23347 struct glyph_row *cursor_row;
23348 struct glyph *cursor_glyph;
23349 enum draw_glyphs_face hl;
23350
23351 /* No cursor displayed or row invalidated => nothing to do on the
23352 screen. */
23353 if (w->phys_cursor_type == NO_CURSOR)
23354 goto mark_cursor_off;
23355
23356 /* VPOS >= active_glyphs->nrows means that window has been resized.
23357 Don't bother to erase the cursor. */
23358 if (vpos >= active_glyphs->nrows)
23359 goto mark_cursor_off;
23360
23361 /* If row containing cursor is marked invalid, there is nothing we
23362 can do. */
23363 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23364 if (!cursor_row->enabled_p)
23365 goto mark_cursor_off;
23366
23367 /* If line spacing is > 0, old cursor may only be partially visible in
23368 window after split-window. So adjust visible height. */
23369 cursor_row->visible_height = min (cursor_row->visible_height,
23370 window_text_bottom_y (w) - cursor_row->y);
23371
23372 /* If row is completely invisible, don't attempt to delete a cursor which
23373 isn't there. This can happen if cursor is at top of a window, and
23374 we switch to a buffer with a header line in that window. */
23375 if (cursor_row->visible_height <= 0)
23376 goto mark_cursor_off;
23377
23378 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23379 if (cursor_row->cursor_in_fringe_p)
23380 {
23381 cursor_row->cursor_in_fringe_p = 0;
23382 draw_fringe_bitmap (w, cursor_row, 0);
23383 goto mark_cursor_off;
23384 }
23385
23386 /* This can happen when the new row is shorter than the old one.
23387 In this case, either draw_glyphs or clear_end_of_line
23388 should have cleared the cursor. Note that we wouldn't be
23389 able to erase the cursor in this case because we don't have a
23390 cursor glyph at hand. */
23391 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
23392 goto mark_cursor_off;
23393
23394 /* If the cursor is in the mouse face area, redisplay that when
23395 we clear the cursor. */
23396 if (! NILP (dpyinfo->mouse_face_window)
23397 && w == XWINDOW (dpyinfo->mouse_face_window)
23398 && (vpos > dpyinfo->mouse_face_beg_row
23399 || (vpos == dpyinfo->mouse_face_beg_row
23400 && hpos >= dpyinfo->mouse_face_beg_col))
23401 && (vpos < dpyinfo->mouse_face_end_row
23402 || (vpos == dpyinfo->mouse_face_end_row
23403 && hpos < dpyinfo->mouse_face_end_col))
23404 /* Don't redraw the cursor's spot in mouse face if it is at the
23405 end of a line (on a newline). The cursor appears there, but
23406 mouse highlighting does not. */
23407 && cursor_row->used[TEXT_AREA] > hpos)
23408 mouse_face_here_p = 1;
23409
23410 /* Maybe clear the display under the cursor. */
23411 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23412 {
23413 int x, y, left_x;
23414 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23415 int width;
23416
23417 cursor_glyph = get_phys_cursor_glyph (w);
23418 if (cursor_glyph == NULL)
23419 goto mark_cursor_off;
23420
23421 width = cursor_glyph->pixel_width;
23422 left_x = window_box_left_offset (w, TEXT_AREA);
23423 x = w->phys_cursor.x;
23424 if (x < left_x)
23425 width -= left_x - x;
23426 width = min (width, window_box_width (w, TEXT_AREA) - x);
23427 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23428 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23429
23430 if (width > 0)
23431 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23432 }
23433
23434 /* Erase the cursor by redrawing the character underneath it. */
23435 if (mouse_face_here_p)
23436 hl = DRAW_MOUSE_FACE;
23437 else
23438 hl = DRAW_NORMAL_TEXT;
23439 draw_phys_cursor_glyph (w, cursor_row, hl);
23440
23441 mark_cursor_off:
23442 w->phys_cursor_on_p = 0;
23443 w->phys_cursor_type = NO_CURSOR;
23444 }
23445
23446
23447 /* EXPORT:
23448 Display or clear cursor of window W. If ON is zero, clear the
23449 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23450 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23451
23452 void
23453 display_and_set_cursor (w, on, hpos, vpos, x, y)
23454 struct window *w;
23455 int on, hpos, vpos, x, y;
23456 {
23457 struct frame *f = XFRAME (w->frame);
23458 int new_cursor_type;
23459 int new_cursor_width;
23460 int active_cursor;
23461 struct glyph_row *glyph_row;
23462 struct glyph *glyph;
23463
23464 /* This is pointless on invisible frames, and dangerous on garbaged
23465 windows and frames; in the latter case, the frame or window may
23466 be in the midst of changing its size, and x and y may be off the
23467 window. */
23468 if (! FRAME_VISIBLE_P (f)
23469 || FRAME_GARBAGED_P (f)
23470 || vpos >= w->current_matrix->nrows
23471 || hpos >= w->current_matrix->matrix_w)
23472 return;
23473
23474 /* If cursor is off and we want it off, return quickly. */
23475 if (!on && !w->phys_cursor_on_p)
23476 return;
23477
23478 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23479 /* If cursor row is not enabled, we don't really know where to
23480 display the cursor. */
23481 if (!glyph_row->enabled_p)
23482 {
23483 w->phys_cursor_on_p = 0;
23484 return;
23485 }
23486
23487 glyph = NULL;
23488 if (!glyph_row->exact_window_width_line_p
23489 || hpos < glyph_row->used[TEXT_AREA])
23490 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23491
23492 xassert (interrupt_input_blocked);
23493
23494 /* Set new_cursor_type to the cursor we want to be displayed. */
23495 new_cursor_type = get_window_cursor_type (w, glyph,
23496 &new_cursor_width, &active_cursor);
23497
23498 /* If cursor is currently being shown and we don't want it to be or
23499 it is in the wrong place, or the cursor type is not what we want,
23500 erase it. */
23501 if (w->phys_cursor_on_p
23502 && (!on
23503 || w->phys_cursor.x != x
23504 || w->phys_cursor.y != y
23505 || new_cursor_type != w->phys_cursor_type
23506 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23507 && new_cursor_width != w->phys_cursor_width)))
23508 erase_phys_cursor (w);
23509
23510 /* Don't check phys_cursor_on_p here because that flag is only set
23511 to zero in some cases where we know that the cursor has been
23512 completely erased, to avoid the extra work of erasing the cursor
23513 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23514 still not be visible, or it has only been partly erased. */
23515 if (on)
23516 {
23517 w->phys_cursor_ascent = glyph_row->ascent;
23518 w->phys_cursor_height = glyph_row->height;
23519
23520 /* Set phys_cursor_.* before x_draw_.* is called because some
23521 of them may need the information. */
23522 w->phys_cursor.x = x;
23523 w->phys_cursor.y = glyph_row->y;
23524 w->phys_cursor.hpos = hpos;
23525 w->phys_cursor.vpos = vpos;
23526 }
23527
23528 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23529 new_cursor_type, new_cursor_width,
23530 on, active_cursor);
23531 }
23532
23533
23534 /* Switch the display of W's cursor on or off, according to the value
23535 of ON. */
23536
23537 #ifndef HAVE_NS
23538 static
23539 #endif
23540 void
23541 update_window_cursor (w, on)
23542 struct window *w;
23543 int on;
23544 {
23545 /* Don't update cursor in windows whose frame is in the process
23546 of being deleted. */
23547 if (w->current_matrix)
23548 {
23549 BLOCK_INPUT;
23550 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23551 w->phys_cursor.x, w->phys_cursor.y);
23552 UNBLOCK_INPUT;
23553 }
23554 }
23555
23556
23557 /* Call update_window_cursor with parameter ON_P on all leaf windows
23558 in the window tree rooted at W. */
23559
23560 static void
23561 update_cursor_in_window_tree (w, on_p)
23562 struct window *w;
23563 int on_p;
23564 {
23565 while (w)
23566 {
23567 if (!NILP (w->hchild))
23568 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23569 else if (!NILP (w->vchild))
23570 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23571 else
23572 update_window_cursor (w, on_p);
23573
23574 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23575 }
23576 }
23577
23578
23579 /* EXPORT:
23580 Display the cursor on window W, or clear it, according to ON_P.
23581 Don't change the cursor's position. */
23582
23583 void
23584 x_update_cursor (f, on_p)
23585 struct frame *f;
23586 int on_p;
23587 {
23588 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23589 }
23590
23591
23592 /* EXPORT:
23593 Clear the cursor of window W to background color, and mark the
23594 cursor as not shown. This is used when the text where the cursor
23595 is about to be rewritten. */
23596
23597 void
23598 x_clear_cursor (w)
23599 struct window *w;
23600 {
23601 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23602 update_window_cursor (w, 0);
23603 }
23604
23605
23606 /* EXPORT:
23607 Display the active region described by mouse_face_* according to DRAW. */
23608
23609 void
23610 show_mouse_face (dpyinfo, draw)
23611 Display_Info *dpyinfo;
23612 enum draw_glyphs_face draw;
23613 {
23614 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23615 struct frame *f = XFRAME (WINDOW_FRAME (w));
23616
23617 if (/* If window is in the process of being destroyed, don't bother
23618 to do anything. */
23619 w->current_matrix != NULL
23620 /* Don't update mouse highlight if hidden */
23621 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23622 /* Recognize when we are called to operate on rows that don't exist
23623 anymore. This can happen when a window is split. */
23624 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23625 {
23626 int phys_cursor_on_p = w->phys_cursor_on_p;
23627 struct glyph_row *row, *first, *last;
23628
23629 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23630 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23631
23632 for (row = first; row <= last && row->enabled_p; ++row)
23633 {
23634 int start_hpos, end_hpos, start_x;
23635
23636 /* For all but the first row, the highlight starts at column 0. */
23637 if (row == first)
23638 {
23639 start_hpos = dpyinfo->mouse_face_beg_col;
23640 start_x = dpyinfo->mouse_face_beg_x;
23641 }
23642 else
23643 {
23644 start_hpos = 0;
23645 start_x = 0;
23646 }
23647
23648 if (row == last)
23649 end_hpos = dpyinfo->mouse_face_end_col;
23650 else
23651 {
23652 end_hpos = row->used[TEXT_AREA];
23653 if (draw == DRAW_NORMAL_TEXT)
23654 row->fill_line_p = 1; /* Clear to end of line */
23655 }
23656
23657 if (end_hpos > start_hpos)
23658 {
23659 draw_glyphs (w, start_x, row, TEXT_AREA,
23660 start_hpos, end_hpos,
23661 draw, 0);
23662
23663 row->mouse_face_p
23664 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23665 }
23666 }
23667
23668 /* When we've written over the cursor, arrange for it to
23669 be displayed again. */
23670 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23671 {
23672 BLOCK_INPUT;
23673 display_and_set_cursor (w, 1,
23674 w->phys_cursor.hpos, w->phys_cursor.vpos,
23675 w->phys_cursor.x, w->phys_cursor.y);
23676 UNBLOCK_INPUT;
23677 }
23678 }
23679
23680 /* Change the mouse cursor. */
23681 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23682 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23683 else if (draw == DRAW_MOUSE_FACE)
23684 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23685 else
23686 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23687 }
23688
23689 /* EXPORT:
23690 Clear out the mouse-highlighted active region.
23691 Redraw it un-highlighted first. Value is non-zero if mouse
23692 face was actually drawn unhighlighted. */
23693
23694 int
23695 clear_mouse_face (dpyinfo)
23696 Display_Info *dpyinfo;
23697 {
23698 int cleared = 0;
23699
23700 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23701 {
23702 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23703 cleared = 1;
23704 }
23705
23706 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23707 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23708 dpyinfo->mouse_face_window = Qnil;
23709 dpyinfo->mouse_face_overlay = Qnil;
23710 return cleared;
23711 }
23712
23713
23714 /* EXPORT:
23715 Non-zero if physical cursor of window W is within mouse face. */
23716
23717 int
23718 cursor_in_mouse_face_p (w)
23719 struct window *w;
23720 {
23721 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23722 int in_mouse_face = 0;
23723
23724 if (WINDOWP (dpyinfo->mouse_face_window)
23725 && XWINDOW (dpyinfo->mouse_face_window) == w)
23726 {
23727 int hpos = w->phys_cursor.hpos;
23728 int vpos = w->phys_cursor.vpos;
23729
23730 if (vpos >= dpyinfo->mouse_face_beg_row
23731 && vpos <= dpyinfo->mouse_face_end_row
23732 && (vpos > dpyinfo->mouse_face_beg_row
23733 || hpos >= dpyinfo->mouse_face_beg_col)
23734 && (vpos < dpyinfo->mouse_face_end_row
23735 || hpos < dpyinfo->mouse_face_end_col
23736 || dpyinfo->mouse_face_past_end))
23737 in_mouse_face = 1;
23738 }
23739
23740 return in_mouse_face;
23741 }
23742
23743
23744
23745 \f
23746 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23747 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23748 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23749 for the overlay or run of text properties specifying the mouse
23750 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23751 before-string and after-string that must also be highlighted.
23752 DISPLAY_STRING, if non-nil, is a display string that may cover some
23753 or all of the highlighted text. */
23754
23755 static void
23756 mouse_face_from_buffer_pos (Lisp_Object window,
23757 Display_Info *dpyinfo,
23758 EMACS_INT mouse_charpos,
23759 EMACS_INT start_charpos,
23760 EMACS_INT end_charpos,
23761 Lisp_Object before_string,
23762 Lisp_Object after_string,
23763 Lisp_Object display_string)
23764 {
23765 struct window *w = XWINDOW (window);
23766 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23767 struct glyph_row *row;
23768 struct glyph *glyph, *end;
23769 EMACS_INT ignore;
23770 int x;
23771
23772 xassert (NILP (display_string) || STRINGP (display_string));
23773 xassert (NILP (before_string) || STRINGP (before_string));
23774 xassert (NILP (after_string) || STRINGP (after_string));
23775
23776 /* Find the first highlighted glyph. */
23777 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
23778 {
23779 dpyinfo->mouse_face_beg_col = 0;
23780 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23781 dpyinfo->mouse_face_beg_x = first->x;
23782 dpyinfo->mouse_face_beg_y = first->y;
23783 }
23784 else
23785 {
23786 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23787 if (row == NULL)
23788 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23789
23790 /* If the before-string or display-string contains newlines,
23791 row_containing_pos skips to its last row. Move back. */
23792 if (!NILP (before_string) || !NILP (display_string))
23793 {
23794 struct glyph_row *prev;
23795 while ((prev = row - 1, prev >= first)
23796 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23797 && prev->used[TEXT_AREA] > 0)
23798 {
23799 struct glyph *beg = prev->glyphs[TEXT_AREA];
23800 glyph = beg + prev->used[TEXT_AREA];
23801 while (--glyph >= beg && INTEGERP (glyph->object));
23802 if (glyph < beg
23803 || !(EQ (glyph->object, before_string)
23804 || EQ (glyph->object, display_string)))
23805 break;
23806 row = prev;
23807 }
23808 }
23809
23810 glyph = row->glyphs[TEXT_AREA];
23811 end = glyph + row->used[TEXT_AREA];
23812 x = row->x;
23813 dpyinfo->mouse_face_beg_y = row->y;
23814 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23815
23816 /* Skip truncation glyphs at the start of the glyph row. */
23817 if (row->displays_text_p)
23818 for (; glyph < end
23819 && INTEGERP (glyph->object)
23820 && glyph->charpos < 0;
23821 ++glyph)
23822 x += glyph->pixel_width;
23823
23824 /* Scan the glyph row, stopping before BEFORE_STRING or
23825 DISPLAY_STRING or START_CHARPOS. */
23826 for (; glyph < end
23827 && !INTEGERP (glyph->object)
23828 && !EQ (glyph->object, before_string)
23829 && !EQ (glyph->object, display_string)
23830 && !(BUFFERP (glyph->object)
23831 && glyph->charpos >= start_charpos);
23832 ++glyph)
23833 x += glyph->pixel_width;
23834
23835 dpyinfo->mouse_face_beg_x = x;
23836 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23837 }
23838
23839 /* Find the last highlighted glyph. */
23840 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23841 if (row == NULL)
23842 {
23843 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23844 dpyinfo->mouse_face_past_end = 1;
23845 }
23846 else if (!NILP (after_string))
23847 {
23848 /* If the after-string has newlines, advance to its last row. */
23849 struct glyph_row *next;
23850 struct glyph_row *last
23851 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23852
23853 for (next = row + 1;
23854 next <= last
23855 && next->used[TEXT_AREA] > 0
23856 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23857 ++next)
23858 row = next;
23859 }
23860
23861 glyph = row->glyphs[TEXT_AREA];
23862 end = glyph + row->used[TEXT_AREA];
23863 x = row->x;
23864 dpyinfo->mouse_face_end_y = row->y;
23865 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23866
23867 /* Skip truncation glyphs at the start of the row. */
23868 if (row->displays_text_p)
23869 for (; glyph < end
23870 && INTEGERP (glyph->object)
23871 && glyph->charpos < 0;
23872 ++glyph)
23873 x += glyph->pixel_width;
23874
23875 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23876 AFTER_STRING. */
23877 for (; glyph < end
23878 && !INTEGERP (glyph->object)
23879 && !EQ (glyph->object, after_string)
23880 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23881 ++glyph)
23882 x += glyph->pixel_width;
23883
23884 /* If we found AFTER_STRING, consume it and stop. */
23885 if (EQ (glyph->object, after_string))
23886 {
23887 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23888 x += glyph->pixel_width;
23889 }
23890 else
23891 {
23892 /* If there's no after-string, we must check if we overshot,
23893 which might be the case if we stopped after a string glyph.
23894 That glyph may belong to a before-string or display-string
23895 associated with the end position, which must not be
23896 highlighted. */
23897 Lisp_Object prev_object;
23898 EMACS_INT pos;
23899
23900 while (glyph > row->glyphs[TEXT_AREA])
23901 {
23902 prev_object = (glyph - 1)->object;
23903 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23904 break;
23905
23906 pos = string_buffer_position (w, prev_object, end_charpos);
23907 if (pos && pos < end_charpos)
23908 break;
23909
23910 for (; glyph > row->glyphs[TEXT_AREA]
23911 && EQ ((glyph - 1)->object, prev_object);
23912 --glyph)
23913 x -= (glyph - 1)->pixel_width;
23914 }
23915 }
23916
23917 dpyinfo->mouse_face_end_x = x;
23918 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23919 dpyinfo->mouse_face_window = window;
23920 dpyinfo->mouse_face_face_id
23921 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23922 mouse_charpos + 1,
23923 !dpyinfo->mouse_face_hidden, -1);
23924 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23925 }
23926
23927
23928 /* Find the position of the glyph for position POS in OBJECT in
23929 window W's current matrix, and return in *X, *Y the pixel
23930 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23931
23932 RIGHT_P non-zero means return the position of the right edge of the
23933 glyph, RIGHT_P zero means return the left edge position.
23934
23935 If no glyph for POS exists in the matrix, return the position of
23936 the glyph with the next smaller position that is in the matrix, if
23937 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23938 exists in the matrix, return the position of the glyph with the
23939 next larger position in OBJECT.
23940
23941 Value is non-zero if a glyph was found. */
23942
23943 static int
23944 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23945 struct window *w;
23946 EMACS_INT pos;
23947 Lisp_Object object;
23948 int *hpos, *vpos, *x, *y;
23949 int right_p;
23950 {
23951 int yb = window_text_bottom_y (w);
23952 struct glyph_row *r;
23953 struct glyph *best_glyph = NULL;
23954 struct glyph_row *best_row = NULL;
23955 int best_x = 0;
23956
23957 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23958 r->enabled_p && r->y < yb;
23959 ++r)
23960 {
23961 struct glyph *g = r->glyphs[TEXT_AREA];
23962 struct glyph *e = g + r->used[TEXT_AREA];
23963 int gx;
23964
23965 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23966 if (EQ (g->object, object))
23967 {
23968 if (g->charpos == pos)
23969 {
23970 best_glyph = g;
23971 best_x = gx;
23972 best_row = r;
23973 goto found;
23974 }
23975 else if (best_glyph == NULL
23976 || ((eabs (g->charpos - pos)
23977 < eabs (best_glyph->charpos - pos))
23978 && (right_p
23979 ? g->charpos < pos
23980 : g->charpos > pos)))
23981 {
23982 best_glyph = g;
23983 best_x = gx;
23984 best_row = r;
23985 }
23986 }
23987 }
23988
23989 found:
23990
23991 if (best_glyph)
23992 {
23993 *x = best_x;
23994 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23995
23996 if (right_p)
23997 {
23998 *x += best_glyph->pixel_width;
23999 ++*hpos;
24000 }
24001
24002 *y = best_row->y;
24003 *vpos = best_row - w->current_matrix->rows;
24004 }
24005
24006 return best_glyph != NULL;
24007 }
24008
24009
24010 /* See if position X, Y is within a hot-spot of an image. */
24011
24012 static int
24013 on_hot_spot_p (hot_spot, x, y)
24014 Lisp_Object hot_spot;
24015 int x, y;
24016 {
24017 if (!CONSP (hot_spot))
24018 return 0;
24019
24020 if (EQ (XCAR (hot_spot), Qrect))
24021 {
24022 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24023 Lisp_Object rect = XCDR (hot_spot);
24024 Lisp_Object tem;
24025 if (!CONSP (rect))
24026 return 0;
24027 if (!CONSP (XCAR (rect)))
24028 return 0;
24029 if (!CONSP (XCDR (rect)))
24030 return 0;
24031 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24032 return 0;
24033 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24034 return 0;
24035 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24036 return 0;
24037 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24038 return 0;
24039 return 1;
24040 }
24041 else if (EQ (XCAR (hot_spot), Qcircle))
24042 {
24043 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24044 Lisp_Object circ = XCDR (hot_spot);
24045 Lisp_Object lr, lx0, ly0;
24046 if (CONSP (circ)
24047 && CONSP (XCAR (circ))
24048 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24049 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24050 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24051 {
24052 double r = XFLOATINT (lr);
24053 double dx = XINT (lx0) - x;
24054 double dy = XINT (ly0) - y;
24055 return (dx * dx + dy * dy <= r * r);
24056 }
24057 }
24058 else if (EQ (XCAR (hot_spot), Qpoly))
24059 {
24060 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24061 if (VECTORP (XCDR (hot_spot)))
24062 {
24063 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24064 Lisp_Object *poly = v->contents;
24065 int n = v->size;
24066 int i;
24067 int inside = 0;
24068 Lisp_Object lx, ly;
24069 int x0, y0;
24070
24071 /* Need an even number of coordinates, and at least 3 edges. */
24072 if (n < 6 || n & 1)
24073 return 0;
24074
24075 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24076 If count is odd, we are inside polygon. Pixels on edges
24077 may or may not be included depending on actual geometry of the
24078 polygon. */
24079 if ((lx = poly[n-2], !INTEGERP (lx))
24080 || (ly = poly[n-1], !INTEGERP (lx)))
24081 return 0;
24082 x0 = XINT (lx), y0 = XINT (ly);
24083 for (i = 0; i < n; i += 2)
24084 {
24085 int x1 = x0, y1 = y0;
24086 if ((lx = poly[i], !INTEGERP (lx))
24087 || (ly = poly[i+1], !INTEGERP (ly)))
24088 return 0;
24089 x0 = XINT (lx), y0 = XINT (ly);
24090
24091 /* Does this segment cross the X line? */
24092 if (x0 >= x)
24093 {
24094 if (x1 >= x)
24095 continue;
24096 }
24097 else if (x1 < x)
24098 continue;
24099 if (y > y0 && y > y1)
24100 continue;
24101 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24102 inside = !inside;
24103 }
24104 return inside;
24105 }
24106 }
24107 return 0;
24108 }
24109
24110 Lisp_Object
24111 find_hot_spot (map, x, y)
24112 Lisp_Object map;
24113 int x, y;
24114 {
24115 while (CONSP (map))
24116 {
24117 if (CONSP (XCAR (map))
24118 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24119 return XCAR (map);
24120 map = XCDR (map);
24121 }
24122
24123 return Qnil;
24124 }
24125
24126 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24127 3, 3, 0,
24128 doc: /* Lookup in image map MAP coordinates X and Y.
24129 An image map is an alist where each element has the format (AREA ID PLIST).
24130 An AREA is specified as either a rectangle, a circle, or a polygon:
24131 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24132 pixel coordinates of the upper left and bottom right corners.
24133 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24134 and the radius of the circle; r may be a float or integer.
24135 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24136 vector describes one corner in the polygon.
24137 Returns the alist element for the first matching AREA in MAP. */)
24138 (map, x, y)
24139 Lisp_Object map;
24140 Lisp_Object x, y;
24141 {
24142 if (NILP (map))
24143 return Qnil;
24144
24145 CHECK_NUMBER (x);
24146 CHECK_NUMBER (y);
24147
24148 return find_hot_spot (map, XINT (x), XINT (y));
24149 }
24150
24151
24152 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24153 static void
24154 define_frame_cursor1 (f, cursor, pointer)
24155 struct frame *f;
24156 Cursor cursor;
24157 Lisp_Object pointer;
24158 {
24159 /* Do not change cursor shape while dragging mouse. */
24160 if (!NILP (do_mouse_tracking))
24161 return;
24162
24163 if (!NILP (pointer))
24164 {
24165 if (EQ (pointer, Qarrow))
24166 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24167 else if (EQ (pointer, Qhand))
24168 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24169 else if (EQ (pointer, Qtext))
24170 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24171 else if (EQ (pointer, intern ("hdrag")))
24172 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24173 #ifdef HAVE_X_WINDOWS
24174 else if (EQ (pointer, intern ("vdrag")))
24175 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24176 #endif
24177 else if (EQ (pointer, intern ("hourglass")))
24178 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24179 else if (EQ (pointer, Qmodeline))
24180 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24181 else
24182 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24183 }
24184
24185 if (cursor != No_Cursor)
24186 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24187 }
24188
24189 /* Take proper action when mouse has moved to the mode or header line
24190 or marginal area AREA of window W, x-position X and y-position Y.
24191 X is relative to the start of the text display area of W, so the
24192 width of bitmap areas and scroll bars must be subtracted to get a
24193 position relative to the start of the mode line. */
24194
24195 static void
24196 note_mode_line_or_margin_highlight (window, x, y, area)
24197 Lisp_Object window;
24198 int x, y;
24199 enum window_part area;
24200 {
24201 struct window *w = XWINDOW (window);
24202 struct frame *f = XFRAME (w->frame);
24203 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24204 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24205 Lisp_Object pointer = Qnil;
24206 int charpos, dx, dy, width, height;
24207 Lisp_Object string, object = Qnil;
24208 Lisp_Object pos, help;
24209
24210 Lisp_Object mouse_face;
24211 int original_x_pixel = x;
24212 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24213 struct glyph_row *row;
24214
24215 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24216 {
24217 int x0;
24218 struct glyph *end;
24219
24220 string = mode_line_string (w, area, &x, &y, &charpos,
24221 &object, &dx, &dy, &width, &height);
24222
24223 row = (area == ON_MODE_LINE
24224 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24225 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24226
24227 /* Find glyph */
24228 if (row->mode_line_p && row->enabled_p)
24229 {
24230 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24231 end = glyph + row->used[TEXT_AREA];
24232
24233 for (x0 = original_x_pixel;
24234 glyph < end && x0 >= glyph->pixel_width;
24235 ++glyph)
24236 x0 -= glyph->pixel_width;
24237
24238 if (glyph >= end)
24239 glyph = NULL;
24240 }
24241 }
24242 else
24243 {
24244 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
24245 string = marginal_area_string (w, area, &x, &y, &charpos,
24246 &object, &dx, &dy, &width, &height);
24247 }
24248
24249 help = Qnil;
24250
24251 if (IMAGEP (object))
24252 {
24253 Lisp_Object image_map, hotspot;
24254 if ((image_map = Fplist_get (XCDR (object), QCmap),
24255 !NILP (image_map))
24256 && (hotspot = find_hot_spot (image_map, dx, dy),
24257 CONSP (hotspot))
24258 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24259 {
24260 Lisp_Object area_id, plist;
24261
24262 area_id = XCAR (hotspot);
24263 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24264 If so, we could look for mouse-enter, mouse-leave
24265 properties in PLIST (and do something...). */
24266 hotspot = XCDR (hotspot);
24267 if (CONSP (hotspot)
24268 && (plist = XCAR (hotspot), CONSP (plist)))
24269 {
24270 pointer = Fplist_get (plist, Qpointer);
24271 if (NILP (pointer))
24272 pointer = Qhand;
24273 help = Fplist_get (plist, Qhelp_echo);
24274 if (!NILP (help))
24275 {
24276 help_echo_string = help;
24277 /* Is this correct? ++kfs */
24278 XSETWINDOW (help_echo_window, w);
24279 help_echo_object = w->buffer;
24280 help_echo_pos = charpos;
24281 }
24282 }
24283 }
24284 if (NILP (pointer))
24285 pointer = Fplist_get (XCDR (object), QCpointer);
24286 }
24287
24288 if (STRINGP (string))
24289 {
24290 pos = make_number (charpos);
24291 /* If we're on a string with `help-echo' text property, arrange
24292 for the help to be displayed. This is done by setting the
24293 global variable help_echo_string to the help string. */
24294 if (NILP (help))
24295 {
24296 help = Fget_text_property (pos, Qhelp_echo, string);
24297 if (!NILP (help))
24298 {
24299 help_echo_string = help;
24300 XSETWINDOW (help_echo_window, w);
24301 help_echo_object = string;
24302 help_echo_pos = charpos;
24303 }
24304 }
24305
24306 if (NILP (pointer))
24307 pointer = Fget_text_property (pos, Qpointer, string);
24308
24309 /* Change the mouse pointer according to what is under X/Y. */
24310 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24311 {
24312 Lisp_Object map;
24313 map = Fget_text_property (pos, Qlocal_map, string);
24314 if (!KEYMAPP (map))
24315 map = Fget_text_property (pos, Qkeymap, string);
24316 if (!KEYMAPP (map))
24317 cursor = dpyinfo->vertical_scroll_bar_cursor;
24318 }
24319
24320 /* Change the mouse face according to what is under X/Y. */
24321 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24322 if (!NILP (mouse_face)
24323 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24324 && glyph)
24325 {
24326 Lisp_Object b, e;
24327
24328 struct glyph * tmp_glyph;
24329
24330 int gpos;
24331 int gseq_length;
24332 int total_pixel_width;
24333 EMACS_INT ignore;
24334
24335 int vpos, hpos;
24336
24337 b = Fprevious_single_property_change (make_number (charpos + 1),
24338 Qmouse_face, string, Qnil);
24339 if (NILP (b))
24340 b = make_number (0);
24341
24342 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24343 if (NILP (e))
24344 e = make_number (SCHARS (string));
24345
24346 /* Calculate the position(glyph position: GPOS) of GLYPH in
24347 displayed string. GPOS is different from CHARPOS.
24348
24349 CHARPOS is the position of glyph in internal string
24350 object. A mode line string format has structures which
24351 is converted to a flatten by emacs lisp interpreter.
24352 The internal string is an element of the structures.
24353 The displayed string is the flatten string. */
24354 gpos = 0;
24355 if (glyph > row_start_glyph)
24356 {
24357 tmp_glyph = glyph - 1;
24358 while (tmp_glyph >= row_start_glyph
24359 && tmp_glyph->charpos >= XINT (b)
24360 && EQ (tmp_glyph->object, glyph->object))
24361 {
24362 tmp_glyph--;
24363 gpos++;
24364 }
24365 }
24366
24367 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
24368 displayed string holding GLYPH.
24369
24370 GSEQ_LENGTH is different from SCHARS (STRING).
24371 SCHARS (STRING) returns the length of the internal string. */
24372 for (tmp_glyph = glyph, gseq_length = gpos;
24373 tmp_glyph->charpos < XINT (e);
24374 tmp_glyph++, gseq_length++)
24375 {
24376 if (!EQ (tmp_glyph->object, glyph->object))
24377 break;
24378 }
24379
24380 total_pixel_width = 0;
24381 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24382 total_pixel_width += tmp_glyph->pixel_width;
24383
24384 /* Pre calculation of re-rendering position */
24385 vpos = (x - gpos);
24386 hpos = (area == ON_MODE_LINE
24387 ? (w->current_matrix)->nrows - 1
24388 : 0);
24389
24390 /* If the re-rendering position is included in the last
24391 re-rendering area, we should do nothing. */
24392 if ( EQ (window, dpyinfo->mouse_face_window)
24393 && dpyinfo->mouse_face_beg_col <= vpos
24394 && vpos < dpyinfo->mouse_face_end_col
24395 && dpyinfo->mouse_face_beg_row == hpos )
24396 return;
24397
24398 if (clear_mouse_face (dpyinfo))
24399 cursor = No_Cursor;
24400
24401 dpyinfo->mouse_face_beg_col = vpos;
24402 dpyinfo->mouse_face_beg_row = hpos;
24403
24404 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
24405 dpyinfo->mouse_face_beg_y = 0;
24406
24407 dpyinfo->mouse_face_end_col = vpos + gseq_length;
24408 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24409
24410 dpyinfo->mouse_face_end_x = 0;
24411 dpyinfo->mouse_face_end_y = 0;
24412
24413 dpyinfo->mouse_face_past_end = 0;
24414 dpyinfo->mouse_face_window = window;
24415
24416 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24417 charpos,
24418 0, 0, 0, &ignore,
24419 glyph->face_id, 1);
24420 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24421
24422 if (NILP (pointer))
24423 pointer = Qhand;
24424 }
24425 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24426 clear_mouse_face (dpyinfo);
24427 }
24428 define_frame_cursor1 (f, cursor, pointer);
24429 }
24430
24431
24432 /* EXPORT:
24433 Take proper action when the mouse has moved to position X, Y on
24434 frame F as regards highlighting characters that have mouse-face
24435 properties. Also de-highlighting chars where the mouse was before.
24436 X and Y can be negative or out of range. */
24437
24438 void
24439 note_mouse_highlight (f, x, y)
24440 struct frame *f;
24441 int x, y;
24442 {
24443 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24444 enum window_part part;
24445 Lisp_Object window;
24446 struct window *w;
24447 Cursor cursor = No_Cursor;
24448 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24449 struct buffer *b;
24450
24451 /* When a menu is active, don't highlight because this looks odd. */
24452 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24453 if (popup_activated ())
24454 return;
24455 #endif
24456
24457 if (NILP (Vmouse_highlight)
24458 || !f->glyphs_initialized_p)
24459 return;
24460
24461 dpyinfo->mouse_face_mouse_x = x;
24462 dpyinfo->mouse_face_mouse_y = y;
24463 dpyinfo->mouse_face_mouse_frame = f;
24464
24465 if (dpyinfo->mouse_face_defer)
24466 return;
24467
24468 if (gc_in_progress)
24469 {
24470 dpyinfo->mouse_face_deferred_gc = 1;
24471 return;
24472 }
24473
24474 /* Which window is that in? */
24475 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24476
24477 /* If we were displaying active text in another window, clear that.
24478 Also clear if we move out of text area in same window. */
24479 if (! EQ (window, dpyinfo->mouse_face_window)
24480 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24481 && !NILP (dpyinfo->mouse_face_window)))
24482 clear_mouse_face (dpyinfo);
24483
24484 /* Not on a window -> return. */
24485 if (!WINDOWP (window))
24486 return;
24487
24488 /* Reset help_echo_string. It will get recomputed below. */
24489 help_echo_string = Qnil;
24490
24491 /* Convert to window-relative pixel coordinates. */
24492 w = XWINDOW (window);
24493 frame_to_window_pixel_xy (w, &x, &y);
24494
24495 /* Handle tool-bar window differently since it doesn't display a
24496 buffer. */
24497 if (EQ (window, f->tool_bar_window))
24498 {
24499 note_tool_bar_highlight (f, x, y);
24500 return;
24501 }
24502
24503 /* Mouse is on the mode, header line or margin? */
24504 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24505 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24506 {
24507 note_mode_line_or_margin_highlight (window, x, y, part);
24508 return;
24509 }
24510
24511 if (part == ON_VERTICAL_BORDER)
24512 {
24513 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24514 help_echo_string = build_string ("drag-mouse-1: resize");
24515 }
24516 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24517 || part == ON_SCROLL_BAR)
24518 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24519 else
24520 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24521
24522 /* Are we in a window whose display is up to date?
24523 And verify the buffer's text has not changed. */
24524 b = XBUFFER (w->buffer);
24525 if (part == ON_TEXT
24526 && EQ (w->window_end_valid, w->buffer)
24527 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24528 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24529 {
24530 int hpos, vpos, i, dx, dy, area;
24531 EMACS_INT pos;
24532 struct glyph *glyph;
24533 Lisp_Object object;
24534 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24535 Lisp_Object *overlay_vec = NULL;
24536 int noverlays;
24537 struct buffer *obuf;
24538 int obegv, ozv, same_region;
24539
24540 /* Find the glyph under X/Y. */
24541 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24542
24543 /* Look for :pointer property on image. */
24544 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24545 {
24546 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24547 if (img != NULL && IMAGEP (img->spec))
24548 {
24549 Lisp_Object image_map, hotspot;
24550 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24551 !NILP (image_map))
24552 && (hotspot = find_hot_spot (image_map,
24553 glyph->slice.x + dx,
24554 glyph->slice.y + dy),
24555 CONSP (hotspot))
24556 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24557 {
24558 Lisp_Object area_id, plist;
24559
24560 area_id = XCAR (hotspot);
24561 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24562 If so, we could look for mouse-enter, mouse-leave
24563 properties in PLIST (and do something...). */
24564 hotspot = XCDR (hotspot);
24565 if (CONSP (hotspot)
24566 && (plist = XCAR (hotspot), CONSP (plist)))
24567 {
24568 pointer = Fplist_get (plist, Qpointer);
24569 if (NILP (pointer))
24570 pointer = Qhand;
24571 help_echo_string = Fplist_get (plist, Qhelp_echo);
24572 if (!NILP (help_echo_string))
24573 {
24574 help_echo_window = window;
24575 help_echo_object = glyph->object;
24576 help_echo_pos = glyph->charpos;
24577 }
24578 }
24579 }
24580 if (NILP (pointer))
24581 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24582 }
24583 }
24584
24585 /* Clear mouse face if X/Y not over text. */
24586 if (glyph == NULL
24587 || area != TEXT_AREA
24588 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24589 {
24590 if (clear_mouse_face (dpyinfo))
24591 cursor = No_Cursor;
24592 if (NILP (pointer))
24593 {
24594 if (area != TEXT_AREA)
24595 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24596 else
24597 pointer = Vvoid_text_area_pointer;
24598 }
24599 goto set_cursor;
24600 }
24601
24602 pos = glyph->charpos;
24603 object = glyph->object;
24604 if (!STRINGP (object) && !BUFFERP (object))
24605 goto set_cursor;
24606
24607 /* If we get an out-of-range value, return now; avoid an error. */
24608 if (BUFFERP (object) && pos > BUF_Z (b))
24609 goto set_cursor;
24610
24611 /* Make the window's buffer temporarily current for
24612 overlays_at and compute_char_face. */
24613 obuf = current_buffer;
24614 current_buffer = b;
24615 obegv = BEGV;
24616 ozv = ZV;
24617 BEGV = BEG;
24618 ZV = Z;
24619
24620 /* Is this char mouse-active or does it have help-echo? */
24621 position = make_number (pos);
24622
24623 if (BUFFERP (object))
24624 {
24625 /* Put all the overlays we want in a vector in overlay_vec. */
24626 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24627 /* Sort overlays into increasing priority order. */
24628 noverlays = sort_overlays (overlay_vec, noverlays, w);
24629 }
24630 else
24631 noverlays = 0;
24632
24633 same_region = (EQ (window, dpyinfo->mouse_face_window)
24634 && vpos >= dpyinfo->mouse_face_beg_row
24635 && vpos <= dpyinfo->mouse_face_end_row
24636 && (vpos > dpyinfo->mouse_face_beg_row
24637 || hpos >= dpyinfo->mouse_face_beg_col)
24638 && (vpos < dpyinfo->mouse_face_end_row
24639 || hpos < dpyinfo->mouse_face_end_col
24640 || dpyinfo->mouse_face_past_end));
24641
24642 if (same_region)
24643 cursor = No_Cursor;
24644
24645 /* Check mouse-face highlighting. */
24646 if (! same_region
24647 /* If there exists an overlay with mouse-face overlapping
24648 the one we are currently highlighting, we have to
24649 check if we enter the overlapping overlay, and then
24650 highlight only that. */
24651 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24652 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24653 {
24654 /* Find the highest priority overlay with a mouse-face. */
24655 overlay = Qnil;
24656 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24657 {
24658 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24659 if (!NILP (mouse_face))
24660 overlay = overlay_vec[i];
24661 }
24662
24663 /* If we're highlighting the same overlay as before, there's
24664 no need to do that again. */
24665 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24666 goto check_help_echo;
24667 dpyinfo->mouse_face_overlay = overlay;
24668
24669 /* Clear the display of the old active region, if any. */
24670 if (clear_mouse_face (dpyinfo))
24671 cursor = No_Cursor;
24672
24673 /* If no overlay applies, get a text property. */
24674 if (NILP (overlay))
24675 mouse_face = Fget_text_property (position, Qmouse_face, object);
24676
24677 /* Next, compute the bounds of the mouse highlighting and
24678 display it. */
24679 if (!NILP (mouse_face) && STRINGP (object))
24680 {
24681 /* The mouse-highlighting comes from a display string
24682 with a mouse-face. */
24683 Lisp_Object b, e;
24684 EMACS_INT ignore;
24685
24686 b = Fprevious_single_property_change
24687 (make_number (pos + 1), Qmouse_face, object, Qnil);
24688 e = Fnext_single_property_change
24689 (position, Qmouse_face, object, Qnil);
24690 if (NILP (b))
24691 b = make_number (0);
24692 if (NILP (e))
24693 e = make_number (SCHARS (object) - 1);
24694
24695 fast_find_string_pos (w, XINT (b), object,
24696 &dpyinfo->mouse_face_beg_col,
24697 &dpyinfo->mouse_face_beg_row,
24698 &dpyinfo->mouse_face_beg_x,
24699 &dpyinfo->mouse_face_beg_y, 0);
24700 fast_find_string_pos (w, XINT (e), object,
24701 &dpyinfo->mouse_face_end_col,
24702 &dpyinfo->mouse_face_end_row,
24703 &dpyinfo->mouse_face_end_x,
24704 &dpyinfo->mouse_face_end_y, 1);
24705 dpyinfo->mouse_face_past_end = 0;
24706 dpyinfo->mouse_face_window = window;
24707 dpyinfo->mouse_face_face_id
24708 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24709 glyph->face_id, 1);
24710 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24711 cursor = No_Cursor;
24712 }
24713 else
24714 {
24715 /* The mouse-highlighting, if any, comes from an overlay
24716 or text property in the buffer. */
24717 Lisp_Object buffer, display_string;
24718
24719 if (STRINGP (object))
24720 {
24721 /* If we are on a display string with no mouse-face,
24722 check if the text under it has one. */
24723 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24724 int start = MATRIX_ROW_START_CHARPOS (r);
24725 pos = string_buffer_position (w, object, start);
24726 if (pos > 0)
24727 {
24728 mouse_face = get_char_property_and_overlay
24729 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24730 buffer = w->buffer;
24731 display_string = object;
24732 }
24733 }
24734 else
24735 {
24736 buffer = object;
24737 display_string = Qnil;
24738 }
24739
24740 if (!NILP (mouse_face))
24741 {
24742 Lisp_Object before, after;
24743 Lisp_Object before_string, after_string;
24744
24745 if (NILP (overlay))
24746 {
24747 /* Handle the text property case. */
24748 before = Fprevious_single_property_change
24749 (make_number (pos + 1), Qmouse_face, buffer,
24750 Fmarker_position (w->start));
24751 after = Fnext_single_property_change
24752 (make_number (pos), Qmouse_face, buffer,
24753 make_number (BUF_Z (XBUFFER (buffer))
24754 - XFASTINT (w->window_end_pos)));
24755 before_string = after_string = Qnil;
24756 }
24757 else
24758 {
24759 /* Handle the overlay case. */
24760 before = Foverlay_start (overlay);
24761 after = Foverlay_end (overlay);
24762 before_string = Foverlay_get (overlay, Qbefore_string);
24763 after_string = Foverlay_get (overlay, Qafter_string);
24764
24765 if (!STRINGP (before_string)) before_string = Qnil;
24766 if (!STRINGP (after_string)) after_string = Qnil;
24767 }
24768
24769 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24770 XFASTINT (before),
24771 XFASTINT (after),
24772 before_string, after_string,
24773 display_string);
24774 cursor = No_Cursor;
24775 }
24776 }
24777 }
24778
24779 check_help_echo:
24780
24781 /* Look for a `help-echo' property. */
24782 if (NILP (help_echo_string)) {
24783 Lisp_Object help, overlay;
24784
24785 /* Check overlays first. */
24786 help = overlay = Qnil;
24787 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24788 {
24789 overlay = overlay_vec[i];
24790 help = Foverlay_get (overlay, Qhelp_echo);
24791 }
24792
24793 if (!NILP (help))
24794 {
24795 help_echo_string = help;
24796 help_echo_window = window;
24797 help_echo_object = overlay;
24798 help_echo_pos = pos;
24799 }
24800 else
24801 {
24802 Lisp_Object object = glyph->object;
24803 int charpos = glyph->charpos;
24804
24805 /* Try text properties. */
24806 if (STRINGP (object)
24807 && charpos >= 0
24808 && charpos < SCHARS (object))
24809 {
24810 help = Fget_text_property (make_number (charpos),
24811 Qhelp_echo, object);
24812 if (NILP (help))
24813 {
24814 /* If the string itself doesn't specify a help-echo,
24815 see if the buffer text ``under'' it does. */
24816 struct glyph_row *r
24817 = MATRIX_ROW (w->current_matrix, vpos);
24818 int start = MATRIX_ROW_START_CHARPOS (r);
24819 EMACS_INT pos = string_buffer_position (w, object, start);
24820 if (pos > 0)
24821 {
24822 help = Fget_char_property (make_number (pos),
24823 Qhelp_echo, w->buffer);
24824 if (!NILP (help))
24825 {
24826 charpos = pos;
24827 object = w->buffer;
24828 }
24829 }
24830 }
24831 }
24832 else if (BUFFERP (object)
24833 && charpos >= BEGV
24834 && charpos < ZV)
24835 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24836 object);
24837
24838 if (!NILP (help))
24839 {
24840 help_echo_string = help;
24841 help_echo_window = window;
24842 help_echo_object = object;
24843 help_echo_pos = charpos;
24844 }
24845 }
24846 }
24847
24848 /* Look for a `pointer' property. */
24849 if (NILP (pointer))
24850 {
24851 /* Check overlays first. */
24852 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24853 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24854
24855 if (NILP (pointer))
24856 {
24857 Lisp_Object object = glyph->object;
24858 int charpos = glyph->charpos;
24859
24860 /* Try text properties. */
24861 if (STRINGP (object)
24862 && charpos >= 0
24863 && charpos < SCHARS (object))
24864 {
24865 pointer = Fget_text_property (make_number (charpos),
24866 Qpointer, object);
24867 if (NILP (pointer))
24868 {
24869 /* If the string itself doesn't specify a pointer,
24870 see if the buffer text ``under'' it does. */
24871 struct glyph_row *r
24872 = MATRIX_ROW (w->current_matrix, vpos);
24873 int start = MATRIX_ROW_START_CHARPOS (r);
24874 EMACS_INT pos = string_buffer_position (w, object,
24875 start);
24876 if (pos > 0)
24877 pointer = Fget_char_property (make_number (pos),
24878 Qpointer, w->buffer);
24879 }
24880 }
24881 else if (BUFFERP (object)
24882 && charpos >= BEGV
24883 && charpos < ZV)
24884 pointer = Fget_text_property (make_number (charpos),
24885 Qpointer, object);
24886 }
24887 }
24888
24889 BEGV = obegv;
24890 ZV = ozv;
24891 current_buffer = obuf;
24892 }
24893
24894 set_cursor:
24895
24896 define_frame_cursor1 (f, cursor, pointer);
24897 }
24898
24899
24900 /* EXPORT for RIF:
24901 Clear any mouse-face on window W. This function is part of the
24902 redisplay interface, and is called from try_window_id and similar
24903 functions to ensure the mouse-highlight is off. */
24904
24905 void
24906 x_clear_window_mouse_face (w)
24907 struct window *w;
24908 {
24909 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24910 Lisp_Object window;
24911
24912 BLOCK_INPUT;
24913 XSETWINDOW (window, w);
24914 if (EQ (window, dpyinfo->mouse_face_window))
24915 clear_mouse_face (dpyinfo);
24916 UNBLOCK_INPUT;
24917 }
24918
24919
24920 /* EXPORT:
24921 Just discard the mouse face information for frame F, if any.
24922 This is used when the size of F is changed. */
24923
24924 void
24925 cancel_mouse_face (f)
24926 struct frame *f;
24927 {
24928 Lisp_Object window;
24929 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24930
24931 window = dpyinfo->mouse_face_window;
24932 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24933 {
24934 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24935 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24936 dpyinfo->mouse_face_window = Qnil;
24937 }
24938 }
24939
24940
24941 #endif /* HAVE_WINDOW_SYSTEM */
24942
24943 \f
24944 /***********************************************************************
24945 Exposure Events
24946 ***********************************************************************/
24947
24948 #ifdef HAVE_WINDOW_SYSTEM
24949
24950 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24951 which intersects rectangle R. R is in window-relative coordinates. */
24952
24953 static void
24954 expose_area (w, row, r, area)
24955 struct window *w;
24956 struct glyph_row *row;
24957 XRectangle *r;
24958 enum glyph_row_area area;
24959 {
24960 struct glyph *first = row->glyphs[area];
24961 struct glyph *end = row->glyphs[area] + row->used[area];
24962 struct glyph *last;
24963 int first_x, start_x, x;
24964
24965 if (area == TEXT_AREA && row->fill_line_p)
24966 /* If row extends face to end of line write the whole line. */
24967 draw_glyphs (w, 0, row, area,
24968 0, row->used[area],
24969 DRAW_NORMAL_TEXT, 0);
24970 else
24971 {
24972 /* Set START_X to the window-relative start position for drawing glyphs of
24973 AREA. The first glyph of the text area can be partially visible.
24974 The first glyphs of other areas cannot. */
24975 start_x = window_box_left_offset (w, area);
24976 x = start_x;
24977 if (area == TEXT_AREA)
24978 x += row->x;
24979
24980 /* Find the first glyph that must be redrawn. */
24981 while (first < end
24982 && x + first->pixel_width < r->x)
24983 {
24984 x += first->pixel_width;
24985 ++first;
24986 }
24987
24988 /* Find the last one. */
24989 last = first;
24990 first_x = x;
24991 while (last < end
24992 && x < r->x + r->width)
24993 {
24994 x += last->pixel_width;
24995 ++last;
24996 }
24997
24998 /* Repaint. */
24999 if (last > first)
25000 draw_glyphs (w, first_x - start_x, row, area,
25001 first - row->glyphs[area], last - row->glyphs[area],
25002 DRAW_NORMAL_TEXT, 0);
25003 }
25004 }
25005
25006
25007 /* Redraw the parts of the glyph row ROW on window W intersecting
25008 rectangle R. R is in window-relative coordinates. Value is
25009 non-zero if mouse-face was overwritten. */
25010
25011 static int
25012 expose_line (w, row, r)
25013 struct window *w;
25014 struct glyph_row *row;
25015 XRectangle *r;
25016 {
25017 xassert (row->enabled_p);
25018
25019 if (row->mode_line_p || w->pseudo_window_p)
25020 draw_glyphs (w, 0, row, TEXT_AREA,
25021 0, row->used[TEXT_AREA],
25022 DRAW_NORMAL_TEXT, 0);
25023 else
25024 {
25025 if (row->used[LEFT_MARGIN_AREA])
25026 expose_area (w, row, r, LEFT_MARGIN_AREA);
25027 if (row->used[TEXT_AREA])
25028 expose_area (w, row, r, TEXT_AREA);
25029 if (row->used[RIGHT_MARGIN_AREA])
25030 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25031 draw_row_fringe_bitmaps (w, row);
25032 }
25033
25034 return row->mouse_face_p;
25035 }
25036
25037
25038 /* Redraw those parts of glyphs rows during expose event handling that
25039 overlap other rows. Redrawing of an exposed line writes over parts
25040 of lines overlapping that exposed line; this function fixes that.
25041
25042 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25043 row in W's current matrix that is exposed and overlaps other rows.
25044 LAST_OVERLAPPING_ROW is the last such row. */
25045
25046 static void
25047 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
25048 struct window *w;
25049 struct glyph_row *first_overlapping_row;
25050 struct glyph_row *last_overlapping_row;
25051 XRectangle *r;
25052 {
25053 struct glyph_row *row;
25054
25055 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25056 if (row->overlapping_p)
25057 {
25058 xassert (row->enabled_p && !row->mode_line_p);
25059
25060 row->clip = r;
25061 if (row->used[LEFT_MARGIN_AREA])
25062 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25063
25064 if (row->used[TEXT_AREA])
25065 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25066
25067 if (row->used[RIGHT_MARGIN_AREA])
25068 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25069 row->clip = NULL;
25070 }
25071 }
25072
25073
25074 /* Return non-zero if W's cursor intersects rectangle R. */
25075
25076 static int
25077 phys_cursor_in_rect_p (w, r)
25078 struct window *w;
25079 XRectangle *r;
25080 {
25081 XRectangle cr, result;
25082 struct glyph *cursor_glyph;
25083 struct glyph_row *row;
25084
25085 if (w->phys_cursor.vpos >= 0
25086 && w->phys_cursor.vpos < w->current_matrix->nrows
25087 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25088 row->enabled_p)
25089 && row->cursor_in_fringe_p)
25090 {
25091 /* Cursor is in the fringe. */
25092 cr.x = window_box_right_offset (w,
25093 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25094 ? RIGHT_MARGIN_AREA
25095 : TEXT_AREA));
25096 cr.y = row->y;
25097 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25098 cr.height = row->height;
25099 return x_intersect_rectangles (&cr, r, &result);
25100 }
25101
25102 cursor_glyph = get_phys_cursor_glyph (w);
25103 if (cursor_glyph)
25104 {
25105 /* r is relative to W's box, but w->phys_cursor.x is relative
25106 to left edge of W's TEXT area. Adjust it. */
25107 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25108 cr.y = w->phys_cursor.y;
25109 cr.width = cursor_glyph->pixel_width;
25110 cr.height = w->phys_cursor_height;
25111 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25112 I assume the effect is the same -- and this is portable. */
25113 return x_intersect_rectangles (&cr, r, &result);
25114 }
25115 /* If we don't understand the format, pretend we're not in the hot-spot. */
25116 return 0;
25117 }
25118
25119
25120 /* EXPORT:
25121 Draw a vertical window border to the right of window W if W doesn't
25122 have vertical scroll bars. */
25123
25124 void
25125 x_draw_vertical_border (w)
25126 struct window *w;
25127 {
25128 struct frame *f = XFRAME (WINDOW_FRAME (w));
25129
25130 /* We could do better, if we knew what type of scroll-bar the adjacent
25131 windows (on either side) have... But we don't :-(
25132 However, I think this works ok. ++KFS 2003-04-25 */
25133
25134 /* Redraw borders between horizontally adjacent windows. Don't
25135 do it for frames with vertical scroll bars because either the
25136 right scroll bar of a window, or the left scroll bar of its
25137 neighbor will suffice as a border. */
25138 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25139 return;
25140
25141 if (!WINDOW_RIGHTMOST_P (w)
25142 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25143 {
25144 int x0, x1, y0, y1;
25145
25146 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25147 y1 -= 1;
25148
25149 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25150 x1 -= 1;
25151
25152 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25153 }
25154 else if (!WINDOW_LEFTMOST_P (w)
25155 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25156 {
25157 int x0, x1, y0, y1;
25158
25159 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25160 y1 -= 1;
25161
25162 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25163 x0 -= 1;
25164
25165 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25166 }
25167 }
25168
25169
25170 /* Redraw the part of window W intersection rectangle FR. Pixel
25171 coordinates in FR are frame-relative. Call this function with
25172 input blocked. Value is non-zero if the exposure overwrites
25173 mouse-face. */
25174
25175 static int
25176 expose_window (w, fr)
25177 struct window *w;
25178 XRectangle *fr;
25179 {
25180 struct frame *f = XFRAME (w->frame);
25181 XRectangle wr, r;
25182 int mouse_face_overwritten_p = 0;
25183
25184 /* If window is not yet fully initialized, do nothing. This can
25185 happen when toolkit scroll bars are used and a window is split.
25186 Reconfiguring the scroll bar will generate an expose for a newly
25187 created window. */
25188 if (w->current_matrix == NULL)
25189 return 0;
25190
25191 /* When we're currently updating the window, display and current
25192 matrix usually don't agree. Arrange for a thorough display
25193 later. */
25194 if (w == updated_window)
25195 {
25196 SET_FRAME_GARBAGED (f);
25197 return 0;
25198 }
25199
25200 /* Frame-relative pixel rectangle of W. */
25201 wr.x = WINDOW_LEFT_EDGE_X (w);
25202 wr.y = WINDOW_TOP_EDGE_Y (w);
25203 wr.width = WINDOW_TOTAL_WIDTH (w);
25204 wr.height = WINDOW_TOTAL_HEIGHT (w);
25205
25206 if (x_intersect_rectangles (fr, &wr, &r))
25207 {
25208 int yb = window_text_bottom_y (w);
25209 struct glyph_row *row;
25210 int cursor_cleared_p;
25211 struct glyph_row *first_overlapping_row, *last_overlapping_row;
25212
25213 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
25214 r.x, r.y, r.width, r.height));
25215
25216 /* Convert to window coordinates. */
25217 r.x -= WINDOW_LEFT_EDGE_X (w);
25218 r.y -= WINDOW_TOP_EDGE_Y (w);
25219
25220 /* Turn off the cursor. */
25221 if (!w->pseudo_window_p
25222 && phys_cursor_in_rect_p (w, &r))
25223 {
25224 x_clear_cursor (w);
25225 cursor_cleared_p = 1;
25226 }
25227 else
25228 cursor_cleared_p = 0;
25229
25230 /* Update lines intersecting rectangle R. */
25231 first_overlapping_row = last_overlapping_row = NULL;
25232 for (row = w->current_matrix->rows;
25233 row->enabled_p;
25234 ++row)
25235 {
25236 int y0 = row->y;
25237 int y1 = MATRIX_ROW_BOTTOM_Y (row);
25238
25239 if ((y0 >= r.y && y0 < r.y + r.height)
25240 || (y1 > r.y && y1 < r.y + r.height)
25241 || (r.y >= y0 && r.y < y1)
25242 || (r.y + r.height > y0 && r.y + r.height < y1))
25243 {
25244 /* A header line may be overlapping, but there is no need
25245 to fix overlapping areas for them. KFS 2005-02-12 */
25246 if (row->overlapping_p && !row->mode_line_p)
25247 {
25248 if (first_overlapping_row == NULL)
25249 first_overlapping_row = row;
25250 last_overlapping_row = row;
25251 }
25252
25253 row->clip = fr;
25254 if (expose_line (w, row, &r))
25255 mouse_face_overwritten_p = 1;
25256 row->clip = NULL;
25257 }
25258 else if (row->overlapping_p)
25259 {
25260 /* We must redraw a row overlapping the exposed area. */
25261 if (y0 < r.y
25262 ? y0 + row->phys_height > r.y
25263 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
25264 {
25265 if (first_overlapping_row == NULL)
25266 first_overlapping_row = row;
25267 last_overlapping_row = row;
25268 }
25269 }
25270
25271 if (y1 >= yb)
25272 break;
25273 }
25274
25275 /* Display the mode line if there is one. */
25276 if (WINDOW_WANTS_MODELINE_P (w)
25277 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
25278 row->enabled_p)
25279 && row->y < r.y + r.height)
25280 {
25281 if (expose_line (w, row, &r))
25282 mouse_face_overwritten_p = 1;
25283 }
25284
25285 if (!w->pseudo_window_p)
25286 {
25287 /* Fix the display of overlapping rows. */
25288 if (first_overlapping_row)
25289 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
25290 fr);
25291
25292 /* Draw border between windows. */
25293 x_draw_vertical_border (w);
25294
25295 /* Turn the cursor on again. */
25296 if (cursor_cleared_p)
25297 update_window_cursor (w, 1);
25298 }
25299 }
25300
25301 return mouse_face_overwritten_p;
25302 }
25303
25304
25305
25306 /* Redraw (parts) of all windows in the window tree rooted at W that
25307 intersect R. R contains frame pixel coordinates. Value is
25308 non-zero if the exposure overwrites mouse-face. */
25309
25310 static int
25311 expose_window_tree (w, r)
25312 struct window *w;
25313 XRectangle *r;
25314 {
25315 struct frame *f = XFRAME (w->frame);
25316 int mouse_face_overwritten_p = 0;
25317
25318 while (w && !FRAME_GARBAGED_P (f))
25319 {
25320 if (!NILP (w->hchild))
25321 mouse_face_overwritten_p
25322 |= expose_window_tree (XWINDOW (w->hchild), r);
25323 else if (!NILP (w->vchild))
25324 mouse_face_overwritten_p
25325 |= expose_window_tree (XWINDOW (w->vchild), r);
25326 else
25327 mouse_face_overwritten_p |= expose_window (w, r);
25328
25329 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25330 }
25331
25332 return mouse_face_overwritten_p;
25333 }
25334
25335
25336 /* EXPORT:
25337 Redisplay an exposed area of frame F. X and Y are the upper-left
25338 corner of the exposed rectangle. W and H are width and height of
25339 the exposed area. All are pixel values. W or H zero means redraw
25340 the entire frame. */
25341
25342 void
25343 expose_frame (f, x, y, w, h)
25344 struct frame *f;
25345 int x, y, w, h;
25346 {
25347 XRectangle r;
25348 int mouse_face_overwritten_p = 0;
25349
25350 TRACE ((stderr, "expose_frame "));
25351
25352 /* No need to redraw if frame will be redrawn soon. */
25353 if (FRAME_GARBAGED_P (f))
25354 {
25355 TRACE ((stderr, " garbaged\n"));
25356 return;
25357 }
25358
25359 /* If basic faces haven't been realized yet, there is no point in
25360 trying to redraw anything. This can happen when we get an expose
25361 event while Emacs is starting, e.g. by moving another window. */
25362 if (FRAME_FACE_CACHE (f) == NULL
25363 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25364 {
25365 TRACE ((stderr, " no faces\n"));
25366 return;
25367 }
25368
25369 if (w == 0 || h == 0)
25370 {
25371 r.x = r.y = 0;
25372 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25373 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25374 }
25375 else
25376 {
25377 r.x = x;
25378 r.y = y;
25379 r.width = w;
25380 r.height = h;
25381 }
25382
25383 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25384 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25385
25386 if (WINDOWP (f->tool_bar_window))
25387 mouse_face_overwritten_p
25388 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25389
25390 #ifdef HAVE_X_WINDOWS
25391 #ifndef MSDOS
25392 #ifndef USE_X_TOOLKIT
25393 if (WINDOWP (f->menu_bar_window))
25394 mouse_face_overwritten_p
25395 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25396 #endif /* not USE_X_TOOLKIT */
25397 #endif
25398 #endif
25399
25400 /* Some window managers support a focus-follows-mouse style with
25401 delayed raising of frames. Imagine a partially obscured frame,
25402 and moving the mouse into partially obscured mouse-face on that
25403 frame. The visible part of the mouse-face will be highlighted,
25404 then the WM raises the obscured frame. With at least one WM, KDE
25405 2.1, Emacs is not getting any event for the raising of the frame
25406 (even tried with SubstructureRedirectMask), only Expose events.
25407 These expose events will draw text normally, i.e. not
25408 highlighted. Which means we must redo the highlight here.
25409 Subsume it under ``we love X''. --gerd 2001-08-15 */
25410 /* Included in Windows version because Windows most likely does not
25411 do the right thing if any third party tool offers
25412 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25413 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25414 {
25415 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25416 if (f == dpyinfo->mouse_face_mouse_frame)
25417 {
25418 int x = dpyinfo->mouse_face_mouse_x;
25419 int y = dpyinfo->mouse_face_mouse_y;
25420 clear_mouse_face (dpyinfo);
25421 note_mouse_highlight (f, x, y);
25422 }
25423 }
25424 }
25425
25426
25427 /* EXPORT:
25428 Determine the intersection of two rectangles R1 and R2. Return
25429 the intersection in *RESULT. Value is non-zero if RESULT is not
25430 empty. */
25431
25432 int
25433 x_intersect_rectangles (r1, r2, result)
25434 XRectangle *r1, *r2, *result;
25435 {
25436 XRectangle *left, *right;
25437 XRectangle *upper, *lower;
25438 int intersection_p = 0;
25439
25440 /* Rearrange so that R1 is the left-most rectangle. */
25441 if (r1->x < r2->x)
25442 left = r1, right = r2;
25443 else
25444 left = r2, right = r1;
25445
25446 /* X0 of the intersection is right.x0, if this is inside R1,
25447 otherwise there is no intersection. */
25448 if (right->x <= left->x + left->width)
25449 {
25450 result->x = right->x;
25451
25452 /* The right end of the intersection is the minimum of the
25453 the right ends of left and right. */
25454 result->width = (min (left->x + left->width, right->x + right->width)
25455 - result->x);
25456
25457 /* Same game for Y. */
25458 if (r1->y < r2->y)
25459 upper = r1, lower = r2;
25460 else
25461 upper = r2, lower = r1;
25462
25463 /* The upper end of the intersection is lower.y0, if this is inside
25464 of upper. Otherwise, there is no intersection. */
25465 if (lower->y <= upper->y + upper->height)
25466 {
25467 result->y = lower->y;
25468
25469 /* The lower end of the intersection is the minimum of the lower
25470 ends of upper and lower. */
25471 result->height = (min (lower->y + lower->height,
25472 upper->y + upper->height)
25473 - result->y);
25474 intersection_p = 1;
25475 }
25476 }
25477
25478 return intersection_p;
25479 }
25480
25481 #endif /* HAVE_WINDOW_SYSTEM */
25482
25483 \f
25484 /***********************************************************************
25485 Initialization
25486 ***********************************************************************/
25487
25488 void
25489 syms_of_xdisp ()
25490 {
25491 Vwith_echo_area_save_vector = Qnil;
25492 staticpro (&Vwith_echo_area_save_vector);
25493
25494 Vmessage_stack = Qnil;
25495 staticpro (&Vmessage_stack);
25496
25497 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25498 staticpro (&Qinhibit_redisplay);
25499
25500 message_dolog_marker1 = Fmake_marker ();
25501 staticpro (&message_dolog_marker1);
25502 message_dolog_marker2 = Fmake_marker ();
25503 staticpro (&message_dolog_marker2);
25504 message_dolog_marker3 = Fmake_marker ();
25505 staticpro (&message_dolog_marker3);
25506
25507 #if GLYPH_DEBUG
25508 defsubr (&Sdump_frame_glyph_matrix);
25509 defsubr (&Sdump_glyph_matrix);
25510 defsubr (&Sdump_glyph_row);
25511 defsubr (&Sdump_tool_bar_row);
25512 defsubr (&Strace_redisplay);
25513 defsubr (&Strace_to_stderr);
25514 #endif
25515 #ifdef HAVE_WINDOW_SYSTEM
25516 defsubr (&Stool_bar_lines_needed);
25517 defsubr (&Slookup_image_map);
25518 #endif
25519 defsubr (&Sformat_mode_line);
25520 defsubr (&Sinvisible_p);
25521
25522 staticpro (&Qmenu_bar_update_hook);
25523 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25524
25525 staticpro (&Qoverriding_terminal_local_map);
25526 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25527
25528 staticpro (&Qoverriding_local_map);
25529 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25530
25531 staticpro (&Qwindow_scroll_functions);
25532 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25533
25534 staticpro (&Qwindow_text_change_functions);
25535 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25536
25537 staticpro (&Qredisplay_end_trigger_functions);
25538 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25539
25540 staticpro (&Qinhibit_point_motion_hooks);
25541 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25542
25543 Qeval = intern_c_string ("eval");
25544 staticpro (&Qeval);
25545
25546 QCdata = intern_c_string (":data");
25547 staticpro (&QCdata);
25548 Qdisplay = intern_c_string ("display");
25549 staticpro (&Qdisplay);
25550 Qspace_width = intern_c_string ("space-width");
25551 staticpro (&Qspace_width);
25552 Qraise = intern_c_string ("raise");
25553 staticpro (&Qraise);
25554 Qslice = intern_c_string ("slice");
25555 staticpro (&Qslice);
25556 Qspace = intern_c_string ("space");
25557 staticpro (&Qspace);
25558 Qmargin = intern_c_string ("margin");
25559 staticpro (&Qmargin);
25560 Qpointer = intern_c_string ("pointer");
25561 staticpro (&Qpointer);
25562 Qleft_margin = intern_c_string ("left-margin");
25563 staticpro (&Qleft_margin);
25564 Qright_margin = intern_c_string ("right-margin");
25565 staticpro (&Qright_margin);
25566 Qcenter = intern_c_string ("center");
25567 staticpro (&Qcenter);
25568 Qline_height = intern_c_string ("line-height");
25569 staticpro (&Qline_height);
25570 QCalign_to = intern_c_string (":align-to");
25571 staticpro (&QCalign_to);
25572 QCrelative_width = intern_c_string (":relative-width");
25573 staticpro (&QCrelative_width);
25574 QCrelative_height = intern_c_string (":relative-height");
25575 staticpro (&QCrelative_height);
25576 QCeval = intern_c_string (":eval");
25577 staticpro (&QCeval);
25578 QCpropertize = intern_c_string (":propertize");
25579 staticpro (&QCpropertize);
25580 QCfile = intern_c_string (":file");
25581 staticpro (&QCfile);
25582 Qfontified = intern_c_string ("fontified");
25583 staticpro (&Qfontified);
25584 Qfontification_functions = intern_c_string ("fontification-functions");
25585 staticpro (&Qfontification_functions);
25586 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25587 staticpro (&Qtrailing_whitespace);
25588 Qescape_glyph = intern_c_string ("escape-glyph");
25589 staticpro (&Qescape_glyph);
25590 Qnobreak_space = intern_c_string ("nobreak-space");
25591 staticpro (&Qnobreak_space);
25592 Qimage = intern_c_string ("image");
25593 staticpro (&Qimage);
25594 QCmap = intern_c_string (":map");
25595 staticpro (&QCmap);
25596 QCpointer = intern_c_string (":pointer");
25597 staticpro (&QCpointer);
25598 Qrect = intern_c_string ("rect");
25599 staticpro (&Qrect);
25600 Qcircle = intern_c_string ("circle");
25601 staticpro (&Qcircle);
25602 Qpoly = intern_c_string ("poly");
25603 staticpro (&Qpoly);
25604 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25605 staticpro (&Qmessage_truncate_lines);
25606 Qgrow_only = intern_c_string ("grow-only");
25607 staticpro (&Qgrow_only);
25608 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25609 staticpro (&Qinhibit_menubar_update);
25610 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25611 staticpro (&Qinhibit_eval_during_redisplay);
25612 Qposition = intern_c_string ("position");
25613 staticpro (&Qposition);
25614 Qbuffer_position = intern_c_string ("buffer-position");
25615 staticpro (&Qbuffer_position);
25616 Qobject = intern_c_string ("object");
25617 staticpro (&Qobject);
25618 Qbar = intern_c_string ("bar");
25619 staticpro (&Qbar);
25620 Qhbar = intern_c_string ("hbar");
25621 staticpro (&Qhbar);
25622 Qbox = intern_c_string ("box");
25623 staticpro (&Qbox);
25624 Qhollow = intern_c_string ("hollow");
25625 staticpro (&Qhollow);
25626 Qhand = intern_c_string ("hand");
25627 staticpro (&Qhand);
25628 Qarrow = intern_c_string ("arrow");
25629 staticpro (&Qarrow);
25630 Qtext = intern_c_string ("text");
25631 staticpro (&Qtext);
25632 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25633 staticpro (&Qrisky_local_variable);
25634 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25635 staticpro (&Qinhibit_free_realized_faces);
25636
25637 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25638 Fcons (intern_c_string ("void-variable"), Qnil)),
25639 Qnil);
25640 staticpro (&list_of_error);
25641
25642 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25643 staticpro (&Qlast_arrow_position);
25644 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25645 staticpro (&Qlast_arrow_string);
25646
25647 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25648 staticpro (&Qoverlay_arrow_string);
25649 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25650 staticpro (&Qoverlay_arrow_bitmap);
25651
25652 echo_buffer[0] = echo_buffer[1] = Qnil;
25653 staticpro (&echo_buffer[0]);
25654 staticpro (&echo_buffer[1]);
25655
25656 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25657 staticpro (&echo_area_buffer[0]);
25658 staticpro (&echo_area_buffer[1]);
25659
25660 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25661 staticpro (&Vmessages_buffer_name);
25662
25663 mode_line_proptrans_alist = Qnil;
25664 staticpro (&mode_line_proptrans_alist);
25665 mode_line_string_list = Qnil;
25666 staticpro (&mode_line_string_list);
25667 mode_line_string_face = Qnil;
25668 staticpro (&mode_line_string_face);
25669 mode_line_string_face_prop = Qnil;
25670 staticpro (&mode_line_string_face_prop);
25671 Vmode_line_unwind_vector = Qnil;
25672 staticpro (&Vmode_line_unwind_vector);
25673
25674 help_echo_string = Qnil;
25675 staticpro (&help_echo_string);
25676 help_echo_object = Qnil;
25677 staticpro (&help_echo_object);
25678 help_echo_window = Qnil;
25679 staticpro (&help_echo_window);
25680 previous_help_echo_string = Qnil;
25681 staticpro (&previous_help_echo_string);
25682 help_echo_pos = -1;
25683
25684 Qright_to_left = intern ("right-to-left");
25685 staticpro (&Qright_to_left);
25686 Qleft_to_right = intern ("left-to-right");
25687 staticpro (&Qleft_to_right);
25688
25689 #ifdef HAVE_WINDOW_SYSTEM
25690 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25691 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25692 For example, if a block cursor is over a tab, it will be drawn as
25693 wide as that tab on the display. */);
25694 x_stretch_cursor_p = 0;
25695 #endif
25696
25697 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25698 doc: /* *Non-nil means highlight trailing whitespace.
25699 The face used for trailing whitespace is `trailing-whitespace'. */);
25700 Vshow_trailing_whitespace = Qnil;
25701
25702 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25703 doc: /* *Control highlighting of nobreak space and soft hyphen.
25704 A value of t means highlight the character itself (for nobreak space,
25705 use face `nobreak-space').
25706 A value of nil means no highlighting.
25707 Other values mean display the escape glyph followed by an ordinary
25708 space or ordinary hyphen. */);
25709 Vnobreak_char_display = Qt;
25710
25711 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25712 doc: /* *The pointer shape to show in void text areas.
25713 A value of nil means to show the text pointer. Other options are `arrow',
25714 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25715 Vvoid_text_area_pointer = Qarrow;
25716
25717 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25718 doc: /* Non-nil means don't actually do any redisplay.
25719 This is used for internal purposes. */);
25720 Vinhibit_redisplay = Qnil;
25721
25722 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25723 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25724 Vglobal_mode_string = Qnil;
25725
25726 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25727 doc: /* Marker for where to display an arrow on top of the buffer text.
25728 This must be the beginning of a line in order to work.
25729 See also `overlay-arrow-string'. */);
25730 Voverlay_arrow_position = Qnil;
25731
25732 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25733 doc: /* String to display as an arrow in non-window frames.
25734 See also `overlay-arrow-position'. */);
25735 Voverlay_arrow_string = make_pure_c_string ("=>");
25736
25737 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25738 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25739 The symbols on this list are examined during redisplay to determine
25740 where to display overlay arrows. */);
25741 Voverlay_arrow_variable_list
25742 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25743
25744 DEFVAR_INT ("scroll-step", &scroll_step,
25745 doc: /* *The number of lines to try scrolling a window by when point moves out.
25746 If that fails to bring point back on frame, point is centered instead.
25747 If this is zero, point is always centered after it moves off frame.
25748 If you want scrolling to always be a line at a time, you should set
25749 `scroll-conservatively' to a large value rather than set this to 1. */);
25750
25751 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25752 doc: /* *Scroll up to this many lines, to bring point back on screen.
25753 If point moves off-screen, redisplay will scroll by up to
25754 `scroll-conservatively' lines in order to bring point just barely
25755 onto the screen again. If that cannot be done, then redisplay
25756 recenters point as usual.
25757
25758 A value of zero means always recenter point if it moves off screen. */);
25759 scroll_conservatively = 0;
25760
25761 DEFVAR_INT ("scroll-margin", &scroll_margin,
25762 doc: /* *Number of lines of margin at the top and bottom of a window.
25763 Recenter the window whenever point gets within this many lines
25764 of the top or bottom of the window. */);
25765 scroll_margin = 0;
25766
25767 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25768 doc: /* Pixels per inch value for non-window system displays.
25769 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25770 Vdisplay_pixels_per_inch = make_float (72.0);
25771
25772 #if GLYPH_DEBUG
25773 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25774 #endif
25775
25776 DEFVAR_LISP ("truncate-partial-width-windows",
25777 &Vtruncate_partial_width_windows,
25778 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25779 For an integer value, truncate lines in each window narrower than the
25780 full frame width, provided the window width is less than that integer;
25781 otherwise, respect the value of `truncate-lines'.
25782
25783 For any other non-nil value, truncate lines in all windows that do
25784 not span the full frame width.
25785
25786 A value of nil means to respect the value of `truncate-lines'.
25787
25788 If `word-wrap' is enabled, you might want to reduce this. */);
25789 Vtruncate_partial_width_windows = make_number (50);
25790
25791 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25792 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25793 Any other value means to use the appropriate face, `mode-line',
25794 `header-line', or `menu' respectively. */);
25795 mode_line_inverse_video = 1;
25796
25797 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25798 doc: /* *Maximum buffer size for which line number should be displayed.
25799 If the buffer is bigger than this, the line number does not appear
25800 in the mode line. A value of nil means no limit. */);
25801 Vline_number_display_limit = Qnil;
25802
25803 DEFVAR_INT ("line-number-display-limit-width",
25804 &line_number_display_limit_width,
25805 doc: /* *Maximum line width (in characters) for line number display.
25806 If the average length of the lines near point is bigger than this, then the
25807 line number may be omitted from the mode line. */);
25808 line_number_display_limit_width = 200;
25809
25810 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25811 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25812 highlight_nonselected_windows = 0;
25813
25814 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25815 doc: /* Non-nil if more than one frame is visible on this display.
25816 Minibuffer-only frames don't count, but iconified frames do.
25817 This variable is not guaranteed to be accurate except while processing
25818 `frame-title-format' and `icon-title-format'. */);
25819
25820 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25821 doc: /* Template for displaying the title bar of visible frames.
25822 \(Assuming the window manager supports this feature.)
25823
25824 This variable has the same structure as `mode-line-format', except that
25825 the %c and %l constructs are ignored. It is used only on frames for
25826 which no explicit name has been set \(see `modify-frame-parameters'). */);
25827
25828 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25829 doc: /* Template for displaying the title bar of an iconified frame.
25830 \(Assuming the window manager supports this feature.)
25831 This variable has the same structure as `mode-line-format' (which see),
25832 and is used only on frames for which no explicit name has been set
25833 \(see `modify-frame-parameters'). */);
25834 Vicon_title_format
25835 = Vframe_title_format
25836 = pure_cons (intern_c_string ("multiple-frames"),
25837 pure_cons (make_pure_c_string ("%b"),
25838 pure_cons (pure_cons (empty_unibyte_string,
25839 pure_cons (intern_c_string ("invocation-name"),
25840 pure_cons (make_pure_c_string ("@"),
25841 pure_cons (intern_c_string ("system-name"),
25842 Qnil)))),
25843 Qnil)));
25844
25845 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25846 doc: /* Maximum number of lines to keep in the message log buffer.
25847 If nil, disable message logging. If t, log messages but don't truncate
25848 the buffer when it becomes large. */);
25849 Vmessage_log_max = make_number (100);
25850
25851 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25852 doc: /* Functions called before redisplay, if window sizes have changed.
25853 The value should be a list of functions that take one argument.
25854 Just before redisplay, for each frame, if any of its windows have changed
25855 size since the last redisplay, or have been split or deleted,
25856 all the functions in the list are called, with the frame as argument. */);
25857 Vwindow_size_change_functions = Qnil;
25858
25859 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25860 doc: /* List of functions to call before redisplaying a window with scrolling.
25861 Each function is called with two arguments, the window and its new
25862 display-start position. Note that these functions are also called by
25863 `set-window-buffer'. Also note that the value of `window-end' is not
25864 valid when these functions are called. */);
25865 Vwindow_scroll_functions = Qnil;
25866
25867 DEFVAR_LISP ("window-text-change-functions",
25868 &Vwindow_text_change_functions,
25869 doc: /* Functions to call in redisplay when text in the window might change. */);
25870 Vwindow_text_change_functions = Qnil;
25871
25872 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25873 doc: /* Functions called when redisplay of a window reaches the end trigger.
25874 Each function is called with two arguments, the window and the end trigger value.
25875 See `set-window-redisplay-end-trigger'. */);
25876 Vredisplay_end_trigger_functions = Qnil;
25877
25878 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25879 doc: /* *Non-nil means autoselect window with mouse pointer.
25880 If nil, do not autoselect windows.
25881 A positive number means delay autoselection by that many seconds: a
25882 window is autoselected only after the mouse has remained in that
25883 window for the duration of the delay.
25884 A negative number has a similar effect, but causes windows to be
25885 autoselected only after the mouse has stopped moving. \(Because of
25886 the way Emacs compares mouse events, you will occasionally wait twice
25887 that time before the window gets selected.\)
25888 Any other value means to autoselect window instantaneously when the
25889 mouse pointer enters it.
25890
25891 Autoselection selects the minibuffer only if it is active, and never
25892 unselects the minibuffer if it is active.
25893
25894 When customizing this variable make sure that the actual value of
25895 `focus-follows-mouse' matches the behavior of your window manager. */);
25896 Vmouse_autoselect_window = Qnil;
25897
25898 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25899 doc: /* *Non-nil means automatically resize tool-bars.
25900 This dynamically changes the tool-bar's height to the minimum height
25901 that is needed to make all tool-bar items visible.
25902 If value is `grow-only', the tool-bar's height is only increased
25903 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25904 Vauto_resize_tool_bars = Qt;
25905
25906 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25907 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25908 auto_raise_tool_bar_buttons_p = 1;
25909
25910 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25911 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25912 make_cursor_line_fully_visible_p = 1;
25913
25914 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25915 doc: /* *Border below tool-bar in pixels.
25916 If an integer, use it as the height of the border.
25917 If it is one of `internal-border-width' or `border-width', use the
25918 value of the corresponding frame parameter.
25919 Otherwise, no border is added below the tool-bar. */);
25920 Vtool_bar_border = Qinternal_border_width;
25921
25922 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25923 doc: /* *Margin around tool-bar buttons in pixels.
25924 If an integer, use that for both horizontal and vertical margins.
25925 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25926 HORZ specifying the horizontal margin, and VERT specifying the
25927 vertical margin. */);
25928 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25929
25930 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25931 doc: /* *Relief thickness of tool-bar buttons. */);
25932 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25933
25934 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25935 doc: /* List of functions to call to fontify regions of text.
25936 Each function is called with one argument POS. Functions must
25937 fontify a region starting at POS in the current buffer, and give
25938 fontified regions the property `fontified'. */);
25939 Vfontification_functions = Qnil;
25940 Fmake_variable_buffer_local (Qfontification_functions);
25941
25942 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25943 &unibyte_display_via_language_environment,
25944 doc: /* *Non-nil means display unibyte text according to language environment.
25945 Specifically, this means that raw bytes in the range 160-255 decimal
25946 are displayed by converting them to the equivalent multibyte characters
25947 according to the current language environment. As a result, they are
25948 displayed according to the current fontset.
25949
25950 Note that this variable affects only how these bytes are displayed,
25951 but does not change the fact they are interpreted as raw bytes. */);
25952 unibyte_display_via_language_environment = 0;
25953
25954 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25955 doc: /* *Maximum height for resizing mini-windows.
25956 If a float, it specifies a fraction of the mini-window frame's height.
25957 If an integer, it specifies a number of lines. */);
25958 Vmax_mini_window_height = make_float (0.25);
25959
25960 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25961 doc: /* *How to resize mini-windows.
25962 A value of nil means don't automatically resize mini-windows.
25963 A value of t means resize them to fit the text displayed in them.
25964 A value of `grow-only', the default, means let mini-windows grow
25965 only, until their display becomes empty, at which point the windows
25966 go back to their normal size. */);
25967 Vresize_mini_windows = Qgrow_only;
25968
25969 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25970 doc: /* Alist specifying how to blink the cursor off.
25971 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25972 `cursor-type' frame-parameter or variable equals ON-STATE,
25973 comparing using `equal', Emacs uses OFF-STATE to specify
25974 how to blink it off. ON-STATE and OFF-STATE are values for
25975 the `cursor-type' frame parameter.
25976
25977 If a frame's ON-STATE has no entry in this list,
25978 the frame's other specifications determine how to blink the cursor off. */);
25979 Vblink_cursor_alist = Qnil;
25980
25981 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25982 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25983 automatic_hscrolling_p = 1;
25984 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25985 staticpro (&Qauto_hscroll_mode);
25986
25987 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25988 doc: /* *How many columns away from the window edge point is allowed to get
25989 before automatic hscrolling will horizontally scroll the window. */);
25990 hscroll_margin = 5;
25991
25992 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25993 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25994 When point is less than `hscroll-margin' columns from the window
25995 edge, automatic hscrolling will scroll the window by the amount of columns
25996 determined by this variable. If its value is a positive integer, scroll that
25997 many columns. If it's a positive floating-point number, it specifies the
25998 fraction of the window's width to scroll. If it's nil or zero, point will be
25999 centered horizontally after the scroll. Any other value, including negative
26000 numbers, are treated as if the value were zero.
26001
26002 Automatic hscrolling always moves point outside the scroll margin, so if
26003 point was more than scroll step columns inside the margin, the window will
26004 scroll more than the value given by the scroll step.
26005
26006 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26007 and `scroll-right' overrides this variable's effect. */);
26008 Vhscroll_step = make_number (0);
26009
26010 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26011 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26012 Bind this around calls to `message' to let it take effect. */);
26013 message_truncate_lines = 0;
26014
26015 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26016 doc: /* Normal hook run to update the menu bar definitions.
26017 Redisplay runs this hook before it redisplays the menu bar.
26018 This is used to update submenus such as Buffers,
26019 whose contents depend on various data. */);
26020 Vmenu_bar_update_hook = Qnil;
26021
26022 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26023 doc: /* Frame for which we are updating a menu.
26024 The enable predicate for a menu binding should check this variable. */);
26025 Vmenu_updating_frame = Qnil;
26026
26027 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26028 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26029 inhibit_menubar_update = 0;
26030
26031 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26032 doc: /* Prefix prepended to all continuation lines at display time.
26033 The value may be a string, an image, or a stretch-glyph; it is
26034 interpreted in the same way as the value of a `display' text property.
26035
26036 This variable is overridden by any `wrap-prefix' text or overlay
26037 property.
26038
26039 To add a prefix to non-continuation lines, use `line-prefix'. */);
26040 Vwrap_prefix = Qnil;
26041 staticpro (&Qwrap_prefix);
26042 Qwrap_prefix = intern_c_string ("wrap-prefix");
26043 Fmake_variable_buffer_local (Qwrap_prefix);
26044
26045 DEFVAR_LISP ("line-prefix", &Vline_prefix,
26046 doc: /* Prefix prepended to all non-continuation lines at display time.
26047 The value may be a string, an image, or a stretch-glyph; it is
26048 interpreted in the same way as the value of a `display' text property.
26049
26050 This variable is overridden by any `line-prefix' text or overlay
26051 property.
26052
26053 To add a prefix to continuation lines, use `wrap-prefix'. */);
26054 Vline_prefix = Qnil;
26055 staticpro (&Qline_prefix);
26056 Qline_prefix = intern_c_string ("line-prefix");
26057 Fmake_variable_buffer_local (Qline_prefix);
26058
26059 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
26060 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26061 inhibit_eval_during_redisplay = 0;
26062
26063 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
26064 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26065 inhibit_free_realized_faces = 0;
26066
26067 #if GLYPH_DEBUG
26068 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
26069 doc: /* Inhibit try_window_id display optimization. */);
26070 inhibit_try_window_id = 0;
26071
26072 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
26073 doc: /* Inhibit try_window_reusing display optimization. */);
26074 inhibit_try_window_reusing = 0;
26075
26076 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
26077 doc: /* Inhibit try_cursor_movement display optimization. */);
26078 inhibit_try_cursor_movement = 0;
26079 #endif /* GLYPH_DEBUG */
26080
26081 DEFVAR_INT ("overline-margin", &overline_margin,
26082 doc: /* *Space between overline and text, in pixels.
26083 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26084 margin to the caracter height. */);
26085 overline_margin = 2;
26086
26087 DEFVAR_INT ("underline-minimum-offset",
26088 &underline_minimum_offset,
26089 doc: /* Minimum distance between baseline and underline.
26090 This can improve legibility of underlined text at small font sizes,
26091 particularly when using variable `x-use-underline-position-properties'
26092 with fonts that specify an UNDERLINE_POSITION relatively close to the
26093 baseline. The default value is 1. */);
26094 underline_minimum_offset = 1;
26095
26096 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
26097 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
26098 display_hourglass_p = 1;
26099
26100 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
26101 doc: /* *Seconds to wait before displaying an hourglass pointer.
26102 Value must be an integer or float. */);
26103 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26104
26105 hourglass_atimer = NULL;
26106 hourglass_shown_p = 0;
26107 }
26108
26109
26110 /* Initialize this module when Emacs starts. */
26111
26112 void
26113 init_xdisp ()
26114 {
26115 Lisp_Object root_window;
26116 struct window *mini_w;
26117
26118 current_header_line_height = current_mode_line_height = -1;
26119
26120 CHARPOS (this_line_start_pos) = 0;
26121
26122 mini_w = XWINDOW (minibuf_window);
26123 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26124
26125 if (!noninteractive)
26126 {
26127 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26128 int i;
26129
26130 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26131 set_window_height (root_window,
26132 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
26133 0);
26134 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
26135 set_window_height (minibuf_window, 1, 0);
26136
26137 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
26138 mini_w->total_cols = make_number (FRAME_COLS (f));
26139
26140 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
26141 scratch_glyph_row.glyphs[TEXT_AREA + 1]
26142 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
26143
26144 /* The default ellipsis glyphs `...'. */
26145 for (i = 0; i < 3; ++i)
26146 default_invis_vector[i] = make_number ('.');
26147 }
26148
26149 {
26150 /* Allocate the buffer for frame titles.
26151 Also used for `format-mode-line'. */
26152 int size = 100;
26153 mode_line_noprop_buf = (char *) xmalloc (size);
26154 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
26155 mode_line_noprop_ptr = mode_line_noprop_buf;
26156 mode_line_target = MODE_LINE_DISPLAY;
26157 }
26158
26159 help_echo_showing_p = 0;
26160 }
26161
26162 /* Since w32 does not support atimers, it defines its own implementation of
26163 the following three functions in w32fns.c. */
26164 #ifndef WINDOWSNT
26165
26166 /* Platform-independent portion of hourglass implementation. */
26167
26168 /* Return non-zero if houglass timer has been started or hourglass is shown. */
26169 int
26170 hourglass_started ()
26171 {
26172 return hourglass_shown_p || hourglass_atimer != NULL;
26173 }
26174
26175 /* Cancel a currently active hourglass timer, and start a new one. */
26176 void
26177 start_hourglass ()
26178 {
26179 #if defined (HAVE_WINDOW_SYSTEM)
26180 EMACS_TIME delay;
26181 int secs, usecs = 0;
26182
26183 cancel_hourglass ();
26184
26185 if (INTEGERP (Vhourglass_delay)
26186 && XINT (Vhourglass_delay) > 0)
26187 secs = XFASTINT (Vhourglass_delay);
26188 else if (FLOATP (Vhourglass_delay)
26189 && XFLOAT_DATA (Vhourglass_delay) > 0)
26190 {
26191 Lisp_Object tem;
26192 tem = Ftruncate (Vhourglass_delay, Qnil);
26193 secs = XFASTINT (tem);
26194 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
26195 }
26196 else
26197 secs = DEFAULT_HOURGLASS_DELAY;
26198
26199 EMACS_SET_SECS_USECS (delay, secs, usecs);
26200 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
26201 show_hourglass, NULL);
26202 #endif
26203 }
26204
26205
26206 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
26207 shown. */
26208 void
26209 cancel_hourglass ()
26210 {
26211 #if defined (HAVE_WINDOW_SYSTEM)
26212 if (hourglass_atimer)
26213 {
26214 cancel_atimer (hourglass_atimer);
26215 hourglass_atimer = NULL;
26216 }
26217
26218 if (hourglass_shown_p)
26219 hide_hourglass ();
26220 #endif
26221 }
26222 #endif /* ! WINDOWSNT */
26223
26224 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
26225 (do not change this comment) */