]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs--devo--0
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192 #include "fontset.h"
193 #include "blockinput.h"
194
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef MAC_OS
202 #include "macterm.h"
203 #endif
204
205 #ifdef HAVE_WINDOW_SYSTEM
206 #ifdef USE_FONT_BACKEND
207 #include "font.h"
208 #endif /* USE_FONT_BACKEND */
209 #endif /* HAVE_WINDOW_SYSTEM */
210
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
214
215 #define INFINITY 10000000
216
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
218 || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
222
223 extern int interrupt_input;
224 extern int command_loop_level;
225
226 extern Lisp_Object do_mouse_tracking;
227
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
230
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
239
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
243 Lisp_Object Qinhibit_point_motion_hooks;
244 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
245 Lisp_Object Qfontified;
246 Lisp_Object Qgrow_only;
247 Lisp_Object Qinhibit_eval_during_redisplay;
248 Lisp_Object Qbuffer_position, Qposition, Qobject;
249
250 /* Cursor shapes */
251 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
252
253 /* Pointer shapes */
254 Lisp_Object Qarrow, Qhand, Qtext;
255
256 Lisp_Object Qrisky_local_variable;
257
258 /* Holds the list (error). */
259 Lisp_Object list_of_error;
260
261 /* Functions called to fontify regions of text. */
262
263 Lisp_Object Vfontification_functions;
264 Lisp_Object Qfontification_functions;
265
266 /* Non-zero means automatically select any window when the mouse
267 cursor moves into it. */
268 int mouse_autoselect_window;
269
270 /* Non-zero means draw tool bar buttons raised when the mouse moves
271 over them. */
272
273 int auto_raise_tool_bar_buttons_p;
274
275 /* Non-zero means to reposition window if cursor line is only partially visible. */
276
277 int make_cursor_line_fully_visible_p;
278
279 /* Margin below tool bar in pixels. 0 or nil means no margin.
280 If value is `internal-border-width' or `border-width',
281 the corresponding frame parameter is used. */
282
283 Lisp_Object Vtool_bar_border;
284
285 /* Margin around tool bar buttons in pixels. */
286
287 Lisp_Object Vtool_bar_button_margin;
288
289 /* Thickness of shadow to draw around tool bar buttons. */
290
291 EMACS_INT tool_bar_button_relief;
292
293 /* Non-zero means automatically resize tool-bars so that all tool-bar
294 items are visible, and no blank lines remain. */
295
296 int auto_resize_tool_bars_p;
297
298 /* Non-zero means draw block and hollow cursor as wide as the glyph
299 under it. For example, if a block cursor is over a tab, it will be
300 drawn as wide as that tab on the display. */
301
302 int x_stretch_cursor_p;
303
304 /* Non-nil means don't actually do any redisplay. */
305
306 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
307
308 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
309
310 int inhibit_eval_during_redisplay;
311
312 /* Names of text properties relevant for redisplay. */
313
314 Lisp_Object Qdisplay;
315 extern Lisp_Object Qface, Qinvisible, Qwidth;
316
317 /* Symbols used in text property values. */
318
319 Lisp_Object Vdisplay_pixels_per_inch;
320 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
321 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
322 Lisp_Object Qslice;
323 Lisp_Object Qcenter;
324 Lisp_Object Qmargin, Qpointer;
325 Lisp_Object Qline_height;
326 extern Lisp_Object Qheight;
327 extern Lisp_Object QCwidth, QCheight, QCascent;
328 extern Lisp_Object Qscroll_bar;
329 extern Lisp_Object Qcursor;
330
331 /* Non-nil means highlight trailing whitespace. */
332
333 Lisp_Object Vshow_trailing_whitespace;
334
335 /* Non-nil means escape non-break space and hyphens. */
336
337 Lisp_Object Vnobreak_char_display;
338
339 #ifdef HAVE_WINDOW_SYSTEM
340 extern Lisp_Object Voverflow_newline_into_fringe;
341
342 /* Test if overflow newline into fringe. Called with iterator IT
343 at or past right window margin, and with IT->current_x set. */
344
345 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
346 (!NILP (Voverflow_newline_into_fringe) \
347 && FRAME_WINDOW_P (it->f) \
348 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
349 && it->current_x == it->last_visible_x)
350
351 #endif /* HAVE_WINDOW_SYSTEM */
352
353 /* Non-nil means show the text cursor in void text areas
354 i.e. in blank areas after eol and eob. This used to be
355 the default in 21.3. */
356
357 Lisp_Object Vvoid_text_area_pointer;
358
359 /* Name of the face used to highlight trailing whitespace. */
360
361 Lisp_Object Qtrailing_whitespace;
362
363 /* Name and number of the face used to highlight escape glyphs. */
364
365 Lisp_Object Qescape_glyph;
366
367 /* Name and number of the face used to highlight non-breaking spaces. */
368
369 Lisp_Object Qnobreak_space;
370
371 /* The symbol `image' which is the car of the lists used to represent
372 images in Lisp. */
373
374 Lisp_Object Qimage;
375
376 /* The image map types. */
377 Lisp_Object QCmap, QCpointer;
378 Lisp_Object Qrect, Qcircle, Qpoly;
379
380 /* Non-zero means print newline to stdout before next mini-buffer
381 message. */
382
383 int noninteractive_need_newline;
384
385 /* Non-zero means print newline to message log before next message. */
386
387 static int message_log_need_newline;
388
389 /* Three markers that message_dolog uses.
390 It could allocate them itself, but that causes trouble
391 in handling memory-full errors. */
392 static Lisp_Object message_dolog_marker1;
393 static Lisp_Object message_dolog_marker2;
394 static Lisp_Object message_dolog_marker3;
395 \f
396 /* The buffer position of the first character appearing entirely or
397 partially on the line of the selected window which contains the
398 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
399 redisplay optimization in redisplay_internal. */
400
401 static struct text_pos this_line_start_pos;
402
403 /* Number of characters past the end of the line above, including the
404 terminating newline. */
405
406 static struct text_pos this_line_end_pos;
407
408 /* The vertical positions and the height of this line. */
409
410 static int this_line_vpos;
411 static int this_line_y;
412 static int this_line_pixel_height;
413
414 /* X position at which this display line starts. Usually zero;
415 negative if first character is partially visible. */
416
417 static int this_line_start_x;
418
419 /* Buffer that this_line_.* variables are referring to. */
420
421 static struct buffer *this_line_buffer;
422
423 /* Nonzero means truncate lines in all windows less wide than the
424 frame. */
425
426 int truncate_partial_width_windows;
427
428 /* A flag to control how to display unibyte 8-bit character. */
429
430 int unibyte_display_via_language_environment;
431
432 /* Nonzero means we have more than one non-mini-buffer-only frame.
433 Not guaranteed to be accurate except while parsing
434 frame-title-format. */
435
436 int multiple_frames;
437
438 Lisp_Object Vglobal_mode_string;
439
440
441 /* List of variables (symbols) which hold markers for overlay arrows.
442 The symbols on this list are examined during redisplay to determine
443 where to display overlay arrows. */
444
445 Lisp_Object Voverlay_arrow_variable_list;
446
447 /* Marker for where to display an arrow on top of the buffer text. */
448
449 Lisp_Object Voverlay_arrow_position;
450
451 /* String to display for the arrow. Only used on terminal frames. */
452
453 Lisp_Object Voverlay_arrow_string;
454
455 /* Values of those variables at last redisplay are stored as
456 properties on `overlay-arrow-position' symbol. However, if
457 Voverlay_arrow_position is a marker, last-arrow-position is its
458 numerical position. */
459
460 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
461
462 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
463 properties on a symbol in overlay-arrow-variable-list. */
464
465 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
466
467 /* Like mode-line-format, but for the title bar on a visible frame. */
468
469 Lisp_Object Vframe_title_format;
470
471 /* Like mode-line-format, but for the title bar on an iconified frame. */
472
473 Lisp_Object Vicon_title_format;
474
475 /* List of functions to call when a window's size changes. These
476 functions get one arg, a frame on which one or more windows' sizes
477 have changed. */
478
479 static Lisp_Object Vwindow_size_change_functions;
480
481 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
482
483 /* Nonzero if an overlay arrow has been displayed in this window. */
484
485 static int overlay_arrow_seen;
486
487 /* Nonzero means highlight the region even in nonselected windows. */
488
489 int highlight_nonselected_windows;
490
491 /* If cursor motion alone moves point off frame, try scrolling this
492 many lines up or down if that will bring it back. */
493
494 static EMACS_INT scroll_step;
495
496 /* Nonzero means scroll just far enough to bring point back on the
497 screen, when appropriate. */
498
499 static EMACS_INT scroll_conservatively;
500
501 /* Recenter the window whenever point gets within this many lines of
502 the top or bottom of the window. This value is translated into a
503 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
504 that there is really a fixed pixel height scroll margin. */
505
506 EMACS_INT scroll_margin;
507
508 /* Number of windows showing the buffer of the selected window (or
509 another buffer with the same base buffer). keyboard.c refers to
510 this. */
511
512 int buffer_shared;
513
514 /* Vector containing glyphs for an ellipsis `...'. */
515
516 static Lisp_Object default_invis_vector[3];
517
518 /* Zero means display the mode-line/header-line/menu-bar in the default face
519 (this slightly odd definition is for compatibility with previous versions
520 of emacs), non-zero means display them using their respective faces.
521
522 This variable is deprecated. */
523
524 int mode_line_inverse_video;
525
526 /* Prompt to display in front of the mini-buffer contents. */
527
528 Lisp_Object minibuf_prompt;
529
530 /* Width of current mini-buffer prompt. Only set after display_line
531 of the line that contains the prompt. */
532
533 int minibuf_prompt_width;
534
535 /* This is the window where the echo area message was displayed. It
536 is always a mini-buffer window, but it may not be the same window
537 currently active as a mini-buffer. */
538
539 Lisp_Object echo_area_window;
540
541 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
542 pushes the current message and the value of
543 message_enable_multibyte on the stack, the function restore_message
544 pops the stack and displays MESSAGE again. */
545
546 Lisp_Object Vmessage_stack;
547
548 /* Nonzero means multibyte characters were enabled when the echo area
549 message was specified. */
550
551 int message_enable_multibyte;
552
553 /* Nonzero if we should redraw the mode lines on the next redisplay. */
554
555 int update_mode_lines;
556
557 /* Nonzero if window sizes or contents have changed since last
558 redisplay that finished. */
559
560 int windows_or_buffers_changed;
561
562 /* Nonzero means a frame's cursor type has been changed. */
563
564 int cursor_type_changed;
565
566 /* Nonzero after display_mode_line if %l was used and it displayed a
567 line number. */
568
569 int line_number_displayed;
570
571 /* Maximum buffer size for which to display line numbers. */
572
573 Lisp_Object Vline_number_display_limit;
574
575 /* Line width to consider when repositioning for line number display. */
576
577 static EMACS_INT line_number_display_limit_width;
578
579 /* Number of lines to keep in the message log buffer. t means
580 infinite. nil means don't log at all. */
581
582 Lisp_Object Vmessage_log_max;
583
584 /* The name of the *Messages* buffer, a string. */
585
586 static Lisp_Object Vmessages_buffer_name;
587
588 /* Index 0 is the buffer that holds the current (desired) echo area message,
589 or nil if none is desired right now.
590
591 Index 1 is the buffer that holds the previously displayed echo area message,
592 or nil to indicate no message. This is normally what's on the screen now.
593
594 These two can point to the same buffer. That happens when the last
595 message output by the user (or made by echoing) has been displayed. */
596
597 Lisp_Object echo_area_buffer[2];
598
599 /* Permanent pointers to the two buffers that are used for echo area
600 purposes. Once the two buffers are made, and their pointers are
601 placed here, these two slots remain unchanged unless those buffers
602 need to be created afresh. */
603
604 static Lisp_Object echo_buffer[2];
605
606 /* A vector saved used in with_area_buffer to reduce consing. */
607
608 static Lisp_Object Vwith_echo_area_save_vector;
609
610 /* Non-zero means display_echo_area should display the last echo area
611 message again. Set by redisplay_preserve_echo_area. */
612
613 static int display_last_displayed_message_p;
614
615 /* Nonzero if echo area is being used by print; zero if being used by
616 message. */
617
618 int message_buf_print;
619
620 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
621
622 Lisp_Object Qinhibit_menubar_update;
623 int inhibit_menubar_update;
624
625 /* Maximum height for resizing mini-windows. Either a float
626 specifying a fraction of the available height, or an integer
627 specifying a number of lines. */
628
629 Lisp_Object Vmax_mini_window_height;
630
631 /* Non-zero means messages should be displayed with truncated
632 lines instead of being continued. */
633
634 int message_truncate_lines;
635 Lisp_Object Qmessage_truncate_lines;
636
637 /* Set to 1 in clear_message to make redisplay_internal aware
638 of an emptied echo area. */
639
640 static int message_cleared_p;
641
642 /* How to blink the default frame cursor off. */
643 Lisp_Object Vblink_cursor_alist;
644
645 /* A scratch glyph row with contents used for generating truncation
646 glyphs. Also used in direct_output_for_insert. */
647
648 #define MAX_SCRATCH_GLYPHS 100
649 struct glyph_row scratch_glyph_row;
650 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
651
652 /* Ascent and height of the last line processed by move_it_to. */
653
654 static int last_max_ascent, last_height;
655
656 /* Non-zero if there's a help-echo in the echo area. */
657
658 int help_echo_showing_p;
659
660 /* If >= 0, computed, exact values of mode-line and header-line height
661 to use in the macros CURRENT_MODE_LINE_HEIGHT and
662 CURRENT_HEADER_LINE_HEIGHT. */
663
664 int current_mode_line_height, current_header_line_height;
665
666 /* The maximum distance to look ahead for text properties. Values
667 that are too small let us call compute_char_face and similar
668 functions too often which is expensive. Values that are too large
669 let us call compute_char_face and alike too often because we
670 might not be interested in text properties that far away. */
671
672 #define TEXT_PROP_DISTANCE_LIMIT 100
673
674 #if GLYPH_DEBUG
675
676 /* Variables to turn off display optimizations from Lisp. */
677
678 int inhibit_try_window_id, inhibit_try_window_reusing;
679 int inhibit_try_cursor_movement;
680
681 /* Non-zero means print traces of redisplay if compiled with
682 GLYPH_DEBUG != 0. */
683
684 int trace_redisplay_p;
685
686 #endif /* GLYPH_DEBUG */
687
688 #ifdef DEBUG_TRACE_MOVE
689 /* Non-zero means trace with TRACE_MOVE to stderr. */
690 int trace_move;
691
692 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
693 #else
694 #define TRACE_MOVE(x) (void) 0
695 #endif
696
697 /* Non-zero means automatically scroll windows horizontally to make
698 point visible. */
699
700 int automatic_hscrolling_p;
701
702 /* How close to the margin can point get before the window is scrolled
703 horizontally. */
704 EMACS_INT hscroll_margin;
705
706 /* How much to scroll horizontally when point is inside the above margin. */
707 Lisp_Object Vhscroll_step;
708
709 /* The variable `resize-mini-windows'. If nil, don't resize
710 mini-windows. If t, always resize them to fit the text they
711 display. If `grow-only', let mini-windows grow only until they
712 become empty. */
713
714 Lisp_Object Vresize_mini_windows;
715
716 /* Buffer being redisplayed -- for redisplay_window_error. */
717
718 struct buffer *displayed_buffer;
719
720 /* Value returned from text property handlers (see below). */
721
722 enum prop_handled
723 {
724 HANDLED_NORMALLY,
725 HANDLED_RECOMPUTE_PROPS,
726 HANDLED_OVERLAY_STRING_CONSUMED,
727 HANDLED_RETURN
728 };
729
730 /* A description of text properties that redisplay is interested
731 in. */
732
733 struct props
734 {
735 /* The name of the property. */
736 Lisp_Object *name;
737
738 /* A unique index for the property. */
739 enum prop_idx idx;
740
741 /* A handler function called to set up iterator IT from the property
742 at IT's current position. Value is used to steer handle_stop. */
743 enum prop_handled (*handler) P_ ((struct it *it));
744 };
745
746 static enum prop_handled handle_face_prop P_ ((struct it *));
747 static enum prop_handled handle_invisible_prop P_ ((struct it *));
748 static enum prop_handled handle_display_prop P_ ((struct it *));
749 static enum prop_handled handle_composition_prop P_ ((struct it *));
750 static enum prop_handled handle_overlay_change P_ ((struct it *));
751 static enum prop_handled handle_fontified_prop P_ ((struct it *));
752 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
753
754 /* Properties handled by iterators. */
755
756 static struct props it_props[] =
757 {
758 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
759 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
760 /* Handle `face' before `display' because some sub-properties of
761 `display' need to know the face. */
762 {&Qface, FACE_PROP_IDX, handle_face_prop},
763 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
764 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
765 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
766 {NULL, 0, NULL}
767 };
768
769 /* Value is the position described by X. If X is a marker, value is
770 the marker_position of X. Otherwise, value is X. */
771
772 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
773
774 /* Enumeration returned by some move_it_.* functions internally. */
775
776 enum move_it_result
777 {
778 /* Not used. Undefined value. */
779 MOVE_UNDEFINED,
780
781 /* Move ended at the requested buffer position or ZV. */
782 MOVE_POS_MATCH_OR_ZV,
783
784 /* Move ended at the requested X pixel position. */
785 MOVE_X_REACHED,
786
787 /* Move within a line ended at the end of a line that must be
788 continued. */
789 MOVE_LINE_CONTINUED,
790
791 /* Move within a line ended at the end of a line that would
792 be displayed truncated. */
793 MOVE_LINE_TRUNCATED,
794
795 /* Move within a line ended at a line end. */
796 MOVE_NEWLINE_OR_CR
797 };
798
799 /* This counter is used to clear the face cache every once in a while
800 in redisplay_internal. It is incremented for each redisplay.
801 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
802 cleared. */
803
804 #define CLEAR_FACE_CACHE_COUNT 500
805 static int clear_face_cache_count;
806
807 /* Similarly for the image cache. */
808
809 #ifdef HAVE_WINDOW_SYSTEM
810 #define CLEAR_IMAGE_CACHE_COUNT 101
811 static int clear_image_cache_count;
812 #endif
813
814 /* Record the previous terminal frame we displayed. */
815
816 static struct frame *previous_terminal_frame;
817
818 /* Non-zero while redisplay_internal is in progress. */
819
820 int redisplaying_p;
821
822 /* Non-zero means don't free realized faces. Bound while freeing
823 realized faces is dangerous because glyph matrices might still
824 reference them. */
825
826 int inhibit_free_realized_faces;
827 Lisp_Object Qinhibit_free_realized_faces;
828
829 /* If a string, XTread_socket generates an event to display that string.
830 (The display is done in read_char.) */
831
832 Lisp_Object help_echo_string;
833 Lisp_Object help_echo_window;
834 Lisp_Object help_echo_object;
835 int help_echo_pos;
836
837 /* Temporary variable for XTread_socket. */
838
839 Lisp_Object previous_help_echo_string;
840
841 /* Null glyph slice */
842
843 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
844
845 \f
846 /* Function prototypes. */
847
848 static void setup_for_ellipsis P_ ((struct it *, int));
849 static void mark_window_display_accurate_1 P_ ((struct window *, int));
850 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
851 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
852 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
853 static int redisplay_mode_lines P_ ((Lisp_Object, int));
854 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
855
856 #if 0
857 static int invisible_text_between_p P_ ((struct it *, int, int));
858 #endif
859
860 static void pint2str P_ ((char *, int, int));
861 static void pint2hrstr P_ ((char *, int, int));
862 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
863 struct text_pos));
864 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
865 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
866 static void store_mode_line_noprop_char P_ ((char));
867 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
868 static void x_consider_frame_title P_ ((Lisp_Object));
869 static void handle_stop P_ ((struct it *));
870 static int tool_bar_lines_needed P_ ((struct frame *, int *));
871 static int single_display_spec_intangible_p P_ ((Lisp_Object));
872 static void ensure_echo_area_buffers P_ ((void));
873 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
874 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
875 static int with_echo_area_buffer P_ ((struct window *, int,
876 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
877 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
878 static void clear_garbaged_frames P_ ((void));
879 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
881 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int display_echo_area P_ ((struct window *));
883 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
884 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
885 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
886 static int string_char_and_length P_ ((const unsigned char *, int, int *));
887 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
888 struct text_pos));
889 static int compute_window_start_on_continuation_line P_ ((struct window *));
890 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
891 static void insert_left_trunc_glyphs P_ ((struct it *));
892 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
893 Lisp_Object));
894 static void extend_face_to_end_of_line P_ ((struct it *));
895 static int append_space_for_newline P_ ((struct it *, int));
896 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
897 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
898 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
899 static int trailing_whitespace_p P_ ((int));
900 static int message_log_check_duplicate P_ ((int, int, int, int));
901 static void push_it P_ ((struct it *));
902 static void pop_it P_ ((struct it *));
903 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
904 static void select_frame_for_redisplay P_ ((Lisp_Object));
905 static void redisplay_internal P_ ((int));
906 static int echo_area_display P_ ((int));
907 static void redisplay_windows P_ ((Lisp_Object));
908 static void redisplay_window P_ ((Lisp_Object, int));
909 static Lisp_Object redisplay_window_error ();
910 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
911 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
912 static void update_menu_bar P_ ((struct frame *, int));
913 static int try_window_reusing_current_matrix P_ ((struct window *));
914 static int try_window_id P_ ((struct window *));
915 static int display_line P_ ((struct it *));
916 static int display_mode_lines P_ ((struct window *));
917 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
918 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
919 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
920 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
921 static void display_menu_bar P_ ((struct window *));
922 static int display_count_lines P_ ((int, int, int, int, int *));
923 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
924 int, int, struct it *, int, int, int, int));
925 static void compute_line_metrics P_ ((struct it *));
926 static void run_redisplay_end_trigger_hook P_ ((struct it *));
927 static int get_overlay_strings P_ ((struct it *, int));
928 static void next_overlay_string P_ ((struct it *));
929 static void reseat P_ ((struct it *, struct text_pos, int));
930 static void reseat_1 P_ ((struct it *, struct text_pos, int));
931 static void back_to_previous_visible_line_start P_ ((struct it *));
932 void reseat_at_previous_visible_line_start P_ ((struct it *));
933 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
934 static int next_element_from_ellipsis P_ ((struct it *));
935 static int next_element_from_display_vector P_ ((struct it *));
936 static int next_element_from_string P_ ((struct it *));
937 static int next_element_from_c_string P_ ((struct it *));
938 static int next_element_from_buffer P_ ((struct it *));
939 static int next_element_from_composition P_ ((struct it *));
940 static int next_element_from_image P_ ((struct it *));
941 static int next_element_from_stretch P_ ((struct it *));
942 static void load_overlay_strings P_ ((struct it *, int));
943 static int init_from_display_pos P_ ((struct it *, struct window *,
944 struct display_pos *));
945 static void reseat_to_string P_ ((struct it *, unsigned char *,
946 Lisp_Object, int, int, int, int));
947 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
948 int, int, int));
949 void move_it_vertically_backward P_ ((struct it *, int));
950 static void init_to_row_start P_ ((struct it *, struct window *,
951 struct glyph_row *));
952 static int init_to_row_end P_ ((struct it *, struct window *,
953 struct glyph_row *));
954 static void back_to_previous_line_start P_ ((struct it *));
955 static int forward_to_next_line_start P_ ((struct it *, int *));
956 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
957 Lisp_Object, int));
958 static struct text_pos string_pos P_ ((int, Lisp_Object));
959 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
960 static int number_of_chars P_ ((unsigned char *, int));
961 static void compute_stop_pos P_ ((struct it *));
962 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
963 Lisp_Object));
964 static int face_before_or_after_it_pos P_ ((struct it *, int));
965 static int next_overlay_change P_ ((int));
966 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
967 Lisp_Object, struct text_pos *,
968 int));
969 static int underlying_face_id P_ ((struct it *));
970 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
971 struct window *));
972
973 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
974 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
975
976 #ifdef HAVE_WINDOW_SYSTEM
977
978 static void update_tool_bar P_ ((struct frame *, int));
979 static void build_desired_tool_bar_string P_ ((struct frame *f));
980 static int redisplay_tool_bar P_ ((struct frame *));
981 static void display_tool_bar_line P_ ((struct it *, int));
982 static void notice_overwritten_cursor P_ ((struct window *,
983 enum glyph_row_area,
984 int, int, int, int));
985
986
987
988 #endif /* HAVE_WINDOW_SYSTEM */
989
990 \f
991 /***********************************************************************
992 Window display dimensions
993 ***********************************************************************/
994
995 /* Return the bottom boundary y-position for text lines in window W.
996 This is the first y position at which a line cannot start.
997 It is relative to the top of the window.
998
999 This is the height of W minus the height of a mode line, if any. */
1000
1001 INLINE int
1002 window_text_bottom_y (w)
1003 struct window *w;
1004 {
1005 int height = WINDOW_TOTAL_HEIGHT (w);
1006
1007 if (WINDOW_WANTS_MODELINE_P (w))
1008 height -= CURRENT_MODE_LINE_HEIGHT (w);
1009 return height;
1010 }
1011
1012 /* Return the pixel width of display area AREA of window W. AREA < 0
1013 means return the total width of W, not including fringes to
1014 the left and right of the window. */
1015
1016 INLINE int
1017 window_box_width (w, area)
1018 struct window *w;
1019 int area;
1020 {
1021 int cols = XFASTINT (w->total_cols);
1022 int pixels = 0;
1023
1024 if (!w->pseudo_window_p)
1025 {
1026 cols -= WINDOW_SCROLL_BAR_COLS (w);
1027
1028 if (area == TEXT_AREA)
1029 {
1030 if (INTEGERP (w->left_margin_cols))
1031 cols -= XFASTINT (w->left_margin_cols);
1032 if (INTEGERP (w->right_margin_cols))
1033 cols -= XFASTINT (w->right_margin_cols);
1034 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1035 }
1036 else if (area == LEFT_MARGIN_AREA)
1037 {
1038 cols = (INTEGERP (w->left_margin_cols)
1039 ? XFASTINT (w->left_margin_cols) : 0);
1040 pixels = 0;
1041 }
1042 else if (area == RIGHT_MARGIN_AREA)
1043 {
1044 cols = (INTEGERP (w->right_margin_cols)
1045 ? XFASTINT (w->right_margin_cols) : 0);
1046 pixels = 0;
1047 }
1048 }
1049
1050 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1051 }
1052
1053
1054 /* Return the pixel height of the display area of window W, not
1055 including mode lines of W, if any. */
1056
1057 INLINE int
1058 window_box_height (w)
1059 struct window *w;
1060 {
1061 struct frame *f = XFRAME (w->frame);
1062 int height = WINDOW_TOTAL_HEIGHT (w);
1063
1064 xassert (height >= 0);
1065
1066 /* Note: the code below that determines the mode-line/header-line
1067 height is essentially the same as that contained in the macro
1068 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1069 the appropriate glyph row has its `mode_line_p' flag set,
1070 and if it doesn't, uses estimate_mode_line_height instead. */
1071
1072 if (WINDOW_WANTS_MODELINE_P (w))
1073 {
1074 struct glyph_row *ml_row
1075 = (w->current_matrix && w->current_matrix->rows
1076 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1077 : 0);
1078 if (ml_row && ml_row->mode_line_p)
1079 height -= ml_row->height;
1080 else
1081 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1082 }
1083
1084 if (WINDOW_WANTS_HEADER_LINE_P (w))
1085 {
1086 struct glyph_row *hl_row
1087 = (w->current_matrix && w->current_matrix->rows
1088 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1089 : 0);
1090 if (hl_row && hl_row->mode_line_p)
1091 height -= hl_row->height;
1092 else
1093 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1094 }
1095
1096 /* With a very small font and a mode-line that's taller than
1097 default, we might end up with a negative height. */
1098 return max (0, height);
1099 }
1100
1101 /* Return the window-relative coordinate of the left edge of display
1102 area AREA of window W. AREA < 0 means return the left edge of the
1103 whole window, to the right of the left fringe of W. */
1104
1105 INLINE int
1106 window_box_left_offset (w, area)
1107 struct window *w;
1108 int area;
1109 {
1110 int x;
1111
1112 if (w->pseudo_window_p)
1113 return 0;
1114
1115 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1116
1117 if (area == TEXT_AREA)
1118 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1119 + window_box_width (w, LEFT_MARGIN_AREA));
1120 else if (area == RIGHT_MARGIN_AREA)
1121 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1122 + window_box_width (w, LEFT_MARGIN_AREA)
1123 + window_box_width (w, TEXT_AREA)
1124 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1125 ? 0
1126 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1127 else if (area == LEFT_MARGIN_AREA
1128 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1129 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1130
1131 return x;
1132 }
1133
1134
1135 /* Return the window-relative coordinate of the right edge of display
1136 area AREA of window W. AREA < 0 means return the left edge of the
1137 whole window, to the left of the right fringe of W. */
1138
1139 INLINE int
1140 window_box_right_offset (w, area)
1141 struct window *w;
1142 int area;
1143 {
1144 return window_box_left_offset (w, area) + window_box_width (w, area);
1145 }
1146
1147 /* Return the frame-relative coordinate of the left edge of display
1148 area AREA of window W. AREA < 0 means return the left edge of the
1149 whole window, to the right of the left fringe of W. */
1150
1151 INLINE int
1152 window_box_left (w, area)
1153 struct window *w;
1154 int area;
1155 {
1156 struct frame *f = XFRAME (w->frame);
1157 int x;
1158
1159 if (w->pseudo_window_p)
1160 return FRAME_INTERNAL_BORDER_WIDTH (f);
1161
1162 x = (WINDOW_LEFT_EDGE_X (w)
1163 + window_box_left_offset (w, area));
1164
1165 return x;
1166 }
1167
1168
1169 /* Return the frame-relative coordinate of the right edge of display
1170 area AREA of window W. AREA < 0 means return the left edge of the
1171 whole window, to the left of the right fringe of W. */
1172
1173 INLINE int
1174 window_box_right (w, area)
1175 struct window *w;
1176 int area;
1177 {
1178 return window_box_left (w, area) + window_box_width (w, area);
1179 }
1180
1181 /* Get the bounding box of the display area AREA of window W, without
1182 mode lines, in frame-relative coordinates. AREA < 0 means the
1183 whole window, not including the left and right fringes of
1184 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1185 coordinates of the upper-left corner of the box. Return in
1186 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1187
1188 INLINE void
1189 window_box (w, area, box_x, box_y, box_width, box_height)
1190 struct window *w;
1191 int area;
1192 int *box_x, *box_y, *box_width, *box_height;
1193 {
1194 if (box_width)
1195 *box_width = window_box_width (w, area);
1196 if (box_height)
1197 *box_height = window_box_height (w);
1198 if (box_x)
1199 *box_x = window_box_left (w, area);
1200 if (box_y)
1201 {
1202 *box_y = WINDOW_TOP_EDGE_Y (w);
1203 if (WINDOW_WANTS_HEADER_LINE_P (w))
1204 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1205 }
1206 }
1207
1208
1209 /* Get the bounding box of the display area AREA of window W, without
1210 mode lines. AREA < 0 means the whole window, not including the
1211 left and right fringe of the window. Return in *TOP_LEFT_X
1212 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1213 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1214 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1215 box. */
1216
1217 INLINE void
1218 window_box_edges (w, area, top_left_x, top_left_y,
1219 bottom_right_x, bottom_right_y)
1220 struct window *w;
1221 int area;
1222 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1223 {
1224 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1225 bottom_right_y);
1226 *bottom_right_x += *top_left_x;
1227 *bottom_right_y += *top_left_y;
1228 }
1229
1230
1231 \f
1232 /***********************************************************************
1233 Utilities
1234 ***********************************************************************/
1235
1236 /* Return the bottom y-position of the line the iterator IT is in.
1237 This can modify IT's settings. */
1238
1239 int
1240 line_bottom_y (it)
1241 struct it *it;
1242 {
1243 int line_height = it->max_ascent + it->max_descent;
1244 int line_top_y = it->current_y;
1245
1246 if (line_height == 0)
1247 {
1248 if (last_height)
1249 line_height = last_height;
1250 else if (IT_CHARPOS (*it) < ZV)
1251 {
1252 move_it_by_lines (it, 1, 1);
1253 line_height = (it->max_ascent || it->max_descent
1254 ? it->max_ascent + it->max_descent
1255 : last_height);
1256 }
1257 else
1258 {
1259 struct glyph_row *row = it->glyph_row;
1260
1261 /* Use the default character height. */
1262 it->glyph_row = NULL;
1263 it->what = IT_CHARACTER;
1264 it->c = ' ';
1265 it->len = 1;
1266 PRODUCE_GLYPHS (it);
1267 line_height = it->ascent + it->descent;
1268 it->glyph_row = row;
1269 }
1270 }
1271
1272 return line_top_y + line_height;
1273 }
1274
1275
1276 /* Return 1 if position CHARPOS is visible in window W.
1277 If visible, set *X and *Y to pixel coordinates of top left corner.
1278 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1279 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1280 and header-lines heights. */
1281
1282 int
1283 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1284 struct window *w;
1285 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1286 {
1287 struct it it;
1288 struct text_pos top;
1289 int visible_p = 0;
1290 struct buffer *old_buffer = NULL;
1291
1292 if (noninteractive)
1293 return visible_p;
1294
1295 if (XBUFFER (w->buffer) != current_buffer)
1296 {
1297 old_buffer = current_buffer;
1298 set_buffer_internal_1 (XBUFFER (w->buffer));
1299 }
1300
1301 SET_TEXT_POS_FROM_MARKER (top, w->start);
1302
1303 /* Compute exact mode line heights, if requested. */
1304 if (exact_mode_line_heights_p)
1305 {
1306 if (WINDOW_WANTS_MODELINE_P (w))
1307 current_mode_line_height
1308 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1309 current_buffer->mode_line_format);
1310
1311 if (WINDOW_WANTS_HEADER_LINE_P (w))
1312 current_header_line_height
1313 = display_mode_line (w, HEADER_LINE_FACE_ID,
1314 current_buffer->header_line_format);
1315 }
1316
1317 start_display (&it, w, top);
1318 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1319 MOVE_TO_POS | MOVE_TO_Y);
1320
1321 /* Note that we may overshoot because of invisible text. */
1322 if (IT_CHARPOS (it) >= charpos)
1323 {
1324 int top_x = it.current_x;
1325 int top_y = it.current_y;
1326 int bottom_y = (last_height = 0, line_bottom_y (&it));
1327 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1328
1329 if (top_y < window_top_y)
1330 visible_p = bottom_y > window_top_y;
1331 else if (top_y < it.last_visible_y)
1332 visible_p = 1;
1333 if (visible_p)
1334 {
1335 *x = top_x;
1336 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1337 *rtop = max (0, window_top_y - top_y);
1338 *rbot = max (0, bottom_y - it.last_visible_y);
1339 }
1340 }
1341 else
1342 {
1343 struct it it2;
1344
1345 it2 = it;
1346 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1347 move_it_by_lines (&it, 1, 0);
1348 if (charpos < IT_CHARPOS (it))
1349 {
1350 visible_p = 1;
1351 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1352 *x = it2.current_x;
1353 *y = it2.current_y + it2.max_ascent - it2.ascent;
1354 *rtop = max (0, -it2.current_y);
1355 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1356 - it.last_visible_y));
1357 }
1358 }
1359
1360 if (old_buffer)
1361 set_buffer_internal_1 (old_buffer);
1362
1363 current_header_line_height = current_mode_line_height = -1;
1364
1365 if (visible_p && XFASTINT (w->hscroll) > 0)
1366 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1367
1368 return visible_p;
1369 }
1370
1371
1372 /* Return the next character from STR which is MAXLEN bytes long.
1373 Return in *LEN the length of the character. This is like
1374 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1375 we find one, we return a `?', but with the length of the invalid
1376 character. */
1377
1378 static INLINE int
1379 string_char_and_length (str, maxlen, len)
1380 const unsigned char *str;
1381 int maxlen, *len;
1382 {
1383 int c;
1384
1385 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1386 if (!CHAR_VALID_P (c, 1))
1387 /* We may not change the length here because other places in Emacs
1388 don't use this function, i.e. they silently accept invalid
1389 characters. */
1390 c = '?';
1391
1392 return c;
1393 }
1394
1395
1396
1397 /* Given a position POS containing a valid character and byte position
1398 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1399
1400 static struct text_pos
1401 string_pos_nchars_ahead (pos, string, nchars)
1402 struct text_pos pos;
1403 Lisp_Object string;
1404 int nchars;
1405 {
1406 xassert (STRINGP (string) && nchars >= 0);
1407
1408 if (STRING_MULTIBYTE (string))
1409 {
1410 int rest = SBYTES (string) - BYTEPOS (pos);
1411 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1412 int len;
1413
1414 while (nchars--)
1415 {
1416 string_char_and_length (p, rest, &len);
1417 p += len, rest -= len;
1418 xassert (rest >= 0);
1419 CHARPOS (pos) += 1;
1420 BYTEPOS (pos) += len;
1421 }
1422 }
1423 else
1424 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1425
1426 return pos;
1427 }
1428
1429
1430 /* Value is the text position, i.e. character and byte position,
1431 for character position CHARPOS in STRING. */
1432
1433 static INLINE struct text_pos
1434 string_pos (charpos, string)
1435 int charpos;
1436 Lisp_Object string;
1437 {
1438 struct text_pos pos;
1439 xassert (STRINGP (string));
1440 xassert (charpos >= 0);
1441 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1442 return pos;
1443 }
1444
1445
1446 /* Value is a text position, i.e. character and byte position, for
1447 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1448 means recognize multibyte characters. */
1449
1450 static struct text_pos
1451 c_string_pos (charpos, s, multibyte_p)
1452 int charpos;
1453 unsigned char *s;
1454 int multibyte_p;
1455 {
1456 struct text_pos pos;
1457
1458 xassert (s != NULL);
1459 xassert (charpos >= 0);
1460
1461 if (multibyte_p)
1462 {
1463 int rest = strlen (s), len;
1464
1465 SET_TEXT_POS (pos, 0, 0);
1466 while (charpos--)
1467 {
1468 string_char_and_length (s, rest, &len);
1469 s += len, rest -= len;
1470 xassert (rest >= 0);
1471 CHARPOS (pos) += 1;
1472 BYTEPOS (pos) += len;
1473 }
1474 }
1475 else
1476 SET_TEXT_POS (pos, charpos, charpos);
1477
1478 return pos;
1479 }
1480
1481
1482 /* Value is the number of characters in C string S. MULTIBYTE_P
1483 non-zero means recognize multibyte characters. */
1484
1485 static int
1486 number_of_chars (s, multibyte_p)
1487 unsigned char *s;
1488 int multibyte_p;
1489 {
1490 int nchars;
1491
1492 if (multibyte_p)
1493 {
1494 int rest = strlen (s), len;
1495 unsigned char *p = (unsigned char *) s;
1496
1497 for (nchars = 0; rest > 0; ++nchars)
1498 {
1499 string_char_and_length (p, rest, &len);
1500 rest -= len, p += len;
1501 }
1502 }
1503 else
1504 nchars = strlen (s);
1505
1506 return nchars;
1507 }
1508
1509
1510 /* Compute byte position NEWPOS->bytepos corresponding to
1511 NEWPOS->charpos. POS is a known position in string STRING.
1512 NEWPOS->charpos must be >= POS.charpos. */
1513
1514 static void
1515 compute_string_pos (newpos, pos, string)
1516 struct text_pos *newpos, pos;
1517 Lisp_Object string;
1518 {
1519 xassert (STRINGP (string));
1520 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1521
1522 if (STRING_MULTIBYTE (string))
1523 *newpos = string_pos_nchars_ahead (pos, string,
1524 CHARPOS (*newpos) - CHARPOS (pos));
1525 else
1526 BYTEPOS (*newpos) = CHARPOS (*newpos);
1527 }
1528
1529 /* EXPORT:
1530 Return an estimation of the pixel height of mode or top lines on
1531 frame F. FACE_ID specifies what line's height to estimate. */
1532
1533 int
1534 estimate_mode_line_height (f, face_id)
1535 struct frame *f;
1536 enum face_id face_id;
1537 {
1538 #ifdef HAVE_WINDOW_SYSTEM
1539 if (FRAME_WINDOW_P (f))
1540 {
1541 int height = FONT_HEIGHT (FRAME_FONT (f));
1542
1543 /* This function is called so early when Emacs starts that the face
1544 cache and mode line face are not yet initialized. */
1545 if (FRAME_FACE_CACHE (f))
1546 {
1547 struct face *face = FACE_FROM_ID (f, face_id);
1548 if (face)
1549 {
1550 if (face->font)
1551 height = FONT_HEIGHT (face->font);
1552 if (face->box_line_width > 0)
1553 height += 2 * face->box_line_width;
1554 }
1555 }
1556
1557 return height;
1558 }
1559 #endif
1560
1561 return 1;
1562 }
1563
1564 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1565 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1566 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1567 not force the value into range. */
1568
1569 void
1570 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1571 FRAME_PTR f;
1572 register int pix_x, pix_y;
1573 int *x, *y;
1574 NativeRectangle *bounds;
1575 int noclip;
1576 {
1577
1578 #ifdef HAVE_WINDOW_SYSTEM
1579 if (FRAME_WINDOW_P (f))
1580 {
1581 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1582 even for negative values. */
1583 if (pix_x < 0)
1584 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1585 if (pix_y < 0)
1586 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1587
1588 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1589 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1590
1591 if (bounds)
1592 STORE_NATIVE_RECT (*bounds,
1593 FRAME_COL_TO_PIXEL_X (f, pix_x),
1594 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1595 FRAME_COLUMN_WIDTH (f) - 1,
1596 FRAME_LINE_HEIGHT (f) - 1);
1597
1598 if (!noclip)
1599 {
1600 if (pix_x < 0)
1601 pix_x = 0;
1602 else if (pix_x > FRAME_TOTAL_COLS (f))
1603 pix_x = FRAME_TOTAL_COLS (f);
1604
1605 if (pix_y < 0)
1606 pix_y = 0;
1607 else if (pix_y > FRAME_LINES (f))
1608 pix_y = FRAME_LINES (f);
1609 }
1610 }
1611 #endif
1612
1613 *x = pix_x;
1614 *y = pix_y;
1615 }
1616
1617
1618 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1619 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1620 can't tell the positions because W's display is not up to date,
1621 return 0. */
1622
1623 int
1624 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1625 struct window *w;
1626 int hpos, vpos;
1627 int *frame_x, *frame_y;
1628 {
1629 #ifdef HAVE_WINDOW_SYSTEM
1630 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1631 {
1632 int success_p;
1633
1634 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1635 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1636
1637 if (display_completed)
1638 {
1639 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1640 struct glyph *glyph = row->glyphs[TEXT_AREA];
1641 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1642
1643 hpos = row->x;
1644 vpos = row->y;
1645 while (glyph < end)
1646 {
1647 hpos += glyph->pixel_width;
1648 ++glyph;
1649 }
1650
1651 /* If first glyph is partially visible, its first visible position is still 0. */
1652 if (hpos < 0)
1653 hpos = 0;
1654
1655 success_p = 1;
1656 }
1657 else
1658 {
1659 hpos = vpos = 0;
1660 success_p = 0;
1661 }
1662
1663 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1664 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1665 return success_p;
1666 }
1667 #endif
1668
1669 *frame_x = hpos;
1670 *frame_y = vpos;
1671 return 1;
1672 }
1673
1674
1675 #ifdef HAVE_WINDOW_SYSTEM
1676
1677 /* Find the glyph under window-relative coordinates X/Y in window W.
1678 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1679 strings. Return in *HPOS and *VPOS the row and column number of
1680 the glyph found. Return in *AREA the glyph area containing X.
1681 Value is a pointer to the glyph found or null if X/Y is not on
1682 text, or we can't tell because W's current matrix is not up to
1683 date. */
1684
1685 static struct glyph *
1686 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1687 struct window *w;
1688 int x, y;
1689 int *hpos, *vpos, *dx, *dy, *area;
1690 {
1691 struct glyph *glyph, *end;
1692 struct glyph_row *row = NULL;
1693 int x0, i;
1694
1695 /* Find row containing Y. Give up if some row is not enabled. */
1696 for (i = 0; i < w->current_matrix->nrows; ++i)
1697 {
1698 row = MATRIX_ROW (w->current_matrix, i);
1699 if (!row->enabled_p)
1700 return NULL;
1701 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1702 break;
1703 }
1704
1705 *vpos = i;
1706 *hpos = 0;
1707
1708 /* Give up if Y is not in the window. */
1709 if (i == w->current_matrix->nrows)
1710 return NULL;
1711
1712 /* Get the glyph area containing X. */
1713 if (w->pseudo_window_p)
1714 {
1715 *area = TEXT_AREA;
1716 x0 = 0;
1717 }
1718 else
1719 {
1720 if (x < window_box_left_offset (w, TEXT_AREA))
1721 {
1722 *area = LEFT_MARGIN_AREA;
1723 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1724 }
1725 else if (x < window_box_right_offset (w, TEXT_AREA))
1726 {
1727 *area = TEXT_AREA;
1728 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1729 }
1730 else
1731 {
1732 *area = RIGHT_MARGIN_AREA;
1733 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1734 }
1735 }
1736
1737 /* Find glyph containing X. */
1738 glyph = row->glyphs[*area];
1739 end = glyph + row->used[*area];
1740 x -= x0;
1741 while (glyph < end && x >= glyph->pixel_width)
1742 {
1743 x -= glyph->pixel_width;
1744 ++glyph;
1745 }
1746
1747 if (glyph == end)
1748 return NULL;
1749
1750 if (dx)
1751 {
1752 *dx = x;
1753 *dy = y - (row->y + row->ascent - glyph->ascent);
1754 }
1755
1756 *hpos = glyph - row->glyphs[*area];
1757 return glyph;
1758 }
1759
1760
1761 /* EXPORT:
1762 Convert frame-relative x/y to coordinates relative to window W.
1763 Takes pseudo-windows into account. */
1764
1765 void
1766 frame_to_window_pixel_xy (w, x, y)
1767 struct window *w;
1768 int *x, *y;
1769 {
1770 if (w->pseudo_window_p)
1771 {
1772 /* A pseudo-window is always full-width, and starts at the
1773 left edge of the frame, plus a frame border. */
1774 struct frame *f = XFRAME (w->frame);
1775 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1776 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1777 }
1778 else
1779 {
1780 *x -= WINDOW_LEFT_EDGE_X (w);
1781 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1782 }
1783 }
1784
1785 /* EXPORT:
1786 Return in RECTS[] at most N clipping rectangles for glyph string S.
1787 Return the number of stored rectangles. */
1788
1789 int
1790 get_glyph_string_clip_rects (s, rects, n)
1791 struct glyph_string *s;
1792 NativeRectangle *rects;
1793 int n;
1794 {
1795 XRectangle r;
1796
1797 if (n <= 0)
1798 return 0;
1799
1800 if (s->row->full_width_p)
1801 {
1802 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1803 r.x = WINDOW_LEFT_EDGE_X (s->w);
1804 r.width = WINDOW_TOTAL_WIDTH (s->w);
1805
1806 /* Unless displaying a mode or menu bar line, which are always
1807 fully visible, clip to the visible part of the row. */
1808 if (s->w->pseudo_window_p)
1809 r.height = s->row->visible_height;
1810 else
1811 r.height = s->height;
1812 }
1813 else
1814 {
1815 /* This is a text line that may be partially visible. */
1816 r.x = window_box_left (s->w, s->area);
1817 r.width = window_box_width (s->w, s->area);
1818 r.height = s->row->visible_height;
1819 }
1820
1821 if (s->clip_head)
1822 if (r.x < s->clip_head->x)
1823 {
1824 if (r.width >= s->clip_head->x - r.x)
1825 r.width -= s->clip_head->x - r.x;
1826 else
1827 r.width = 0;
1828 r.x = s->clip_head->x;
1829 }
1830 if (s->clip_tail)
1831 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1832 {
1833 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1834 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1835 else
1836 r.width = 0;
1837 }
1838
1839 /* If S draws overlapping rows, it's sufficient to use the top and
1840 bottom of the window for clipping because this glyph string
1841 intentionally draws over other lines. */
1842 if (s->for_overlaps)
1843 {
1844 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1845 r.height = window_text_bottom_y (s->w) - r.y;
1846
1847 /* Alas, the above simple strategy does not work for the
1848 environments with anti-aliased text: if the same text is
1849 drawn onto the same place multiple times, it gets thicker.
1850 If the overlap we are processing is for the erased cursor, we
1851 take the intersection with the rectagle of the cursor. */
1852 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1853 {
1854 XRectangle rc, r_save = r;
1855
1856 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1857 rc.y = s->w->phys_cursor.y;
1858 rc.width = s->w->phys_cursor_width;
1859 rc.height = s->w->phys_cursor_height;
1860
1861 x_intersect_rectangles (&r_save, &rc, &r);
1862 }
1863 }
1864 else
1865 {
1866 /* Don't use S->y for clipping because it doesn't take partially
1867 visible lines into account. For example, it can be negative for
1868 partially visible lines at the top of a window. */
1869 if (!s->row->full_width_p
1870 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1871 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1872 else
1873 r.y = max (0, s->row->y);
1874
1875 /* If drawing a tool-bar window, draw it over the internal border
1876 at the top of the window. */
1877 if (WINDOWP (s->f->tool_bar_window)
1878 && s->w == XWINDOW (s->f->tool_bar_window))
1879 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1880 }
1881
1882 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1883
1884 /* If drawing the cursor, don't let glyph draw outside its
1885 advertised boundaries. Cleartype does this under some circumstances. */
1886 if (s->hl == DRAW_CURSOR)
1887 {
1888 struct glyph *glyph = s->first_glyph;
1889 int height, max_y;
1890
1891 if (s->x > r.x)
1892 {
1893 r.width -= s->x - r.x;
1894 r.x = s->x;
1895 }
1896 r.width = min (r.width, glyph->pixel_width);
1897
1898 /* If r.y is below window bottom, ensure that we still see a cursor. */
1899 height = min (glyph->ascent + glyph->descent,
1900 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1901 max_y = window_text_bottom_y (s->w) - height;
1902 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1903 if (s->ybase - glyph->ascent > max_y)
1904 {
1905 r.y = max_y;
1906 r.height = height;
1907 }
1908 else
1909 {
1910 /* Don't draw cursor glyph taller than our actual glyph. */
1911 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1912 if (height < r.height)
1913 {
1914 max_y = r.y + r.height;
1915 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1916 r.height = min (max_y - r.y, height);
1917 }
1918 }
1919 }
1920
1921 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1922 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1923 {
1924 #ifdef CONVERT_FROM_XRECT
1925 CONVERT_FROM_XRECT (r, *rects);
1926 #else
1927 *rects = r;
1928 #endif
1929 return 1;
1930 }
1931 else
1932 {
1933 /* If we are processing overlapping and allowed to return
1934 multiple clipping rectangles, we exclude the row of the glyph
1935 string from the clipping rectangle. This is to avoid drawing
1936 the same text on the environment with anti-aliasing. */
1937 #ifdef CONVERT_FROM_XRECT
1938 XRectangle rs[2];
1939 #else
1940 XRectangle *rs = rects;
1941 #endif
1942 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1943
1944 if (s->for_overlaps & OVERLAPS_PRED)
1945 {
1946 rs[i] = r;
1947 if (r.y + r.height > row_y)
1948 {
1949 if (r.y < row_y)
1950 rs[i].height = row_y - r.y;
1951 else
1952 rs[i].height = 0;
1953 }
1954 i++;
1955 }
1956 if (s->for_overlaps & OVERLAPS_SUCC)
1957 {
1958 rs[i] = r;
1959 if (r.y < row_y + s->row->visible_height)
1960 {
1961 if (r.y + r.height > row_y + s->row->visible_height)
1962 {
1963 rs[i].y = row_y + s->row->visible_height;
1964 rs[i].height = r.y + r.height - rs[i].y;
1965 }
1966 else
1967 rs[i].height = 0;
1968 }
1969 i++;
1970 }
1971
1972 n = i;
1973 #ifdef CONVERT_FROM_XRECT
1974 for (i = 0; i < n; i++)
1975 CONVERT_FROM_XRECT (rs[i], rects[i]);
1976 #endif
1977 return n;
1978 }
1979 }
1980
1981 /* EXPORT:
1982 Return in *NR the clipping rectangle for glyph string S. */
1983
1984 void
1985 get_glyph_string_clip_rect (s, nr)
1986 struct glyph_string *s;
1987 NativeRectangle *nr;
1988 {
1989 get_glyph_string_clip_rects (s, nr, 1);
1990 }
1991
1992
1993 /* EXPORT:
1994 Return the position and height of the phys cursor in window W.
1995 Set w->phys_cursor_width to width of phys cursor.
1996 */
1997
1998 void
1999 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2000 struct window *w;
2001 struct glyph_row *row;
2002 struct glyph *glyph;
2003 int *xp, *yp, *heightp;
2004 {
2005 struct frame *f = XFRAME (WINDOW_FRAME (w));
2006 int x, y, wd, h, h0, y0;
2007
2008 /* Compute the width of the rectangle to draw. If on a stretch
2009 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2010 rectangle as wide as the glyph, but use a canonical character
2011 width instead. */
2012 wd = glyph->pixel_width - 1;
2013 #ifdef HAVE_NTGUI
2014 wd++; /* Why? */
2015 #endif
2016
2017 x = w->phys_cursor.x;
2018 if (x < 0)
2019 {
2020 wd += x;
2021 x = 0;
2022 }
2023
2024 if (glyph->type == STRETCH_GLYPH
2025 && !x_stretch_cursor_p)
2026 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2027 w->phys_cursor_width = wd;
2028
2029 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2030
2031 /* If y is below window bottom, ensure that we still see a cursor. */
2032 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2033
2034 h = max (h0, glyph->ascent + glyph->descent);
2035 h0 = min (h0, glyph->ascent + glyph->descent);
2036
2037 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2038 if (y < y0)
2039 {
2040 h = max (h - (y0 - y) + 1, h0);
2041 y = y0 - 1;
2042 }
2043 else
2044 {
2045 y0 = window_text_bottom_y (w) - h0;
2046 if (y > y0)
2047 {
2048 h += y - y0;
2049 y = y0;
2050 }
2051 }
2052
2053 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2054 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2055 *heightp = h;
2056 }
2057
2058 /*
2059 * Remember which glyph the mouse is over.
2060 */
2061
2062 void
2063 remember_mouse_glyph (f, gx, gy, rect)
2064 struct frame *f;
2065 int gx, gy;
2066 NativeRectangle *rect;
2067 {
2068 Lisp_Object window;
2069 struct window *w;
2070 struct glyph_row *r, *gr, *end_row;
2071 enum window_part part;
2072 enum glyph_row_area area;
2073 int x, y, width, height;
2074
2075 /* Try to determine frame pixel position and size of the glyph under
2076 frame pixel coordinates X/Y on frame F. */
2077
2078 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2079 if (NILP (window))
2080 {
2081 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2082 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2083 goto virtual_glyph;
2084 }
2085
2086 w = XWINDOW (window);
2087 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2088 height = WINDOW_FRAME_LINE_HEIGHT (w);
2089
2090 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2091 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2092
2093 if (w->pseudo_window_p)
2094 {
2095 area = TEXT_AREA;
2096 part = ON_MODE_LINE; /* Don't adjust margin. */
2097 goto text_glyph;
2098 }
2099
2100 switch (part)
2101 {
2102 case ON_LEFT_MARGIN:
2103 area = LEFT_MARGIN_AREA;
2104 goto text_glyph;
2105
2106 case ON_RIGHT_MARGIN:
2107 area = RIGHT_MARGIN_AREA;
2108 goto text_glyph;
2109
2110 case ON_HEADER_LINE:
2111 case ON_MODE_LINE:
2112 gr = (part == ON_HEADER_LINE
2113 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2114 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2115 gy = gr->y;
2116 area = TEXT_AREA;
2117 goto text_glyph_row_found;
2118
2119 case ON_TEXT:
2120 area = TEXT_AREA;
2121
2122 text_glyph:
2123 gr = 0; gy = 0;
2124 for (; r <= end_row && r->enabled_p; ++r)
2125 if (r->y + r->height > y)
2126 {
2127 gr = r; gy = r->y;
2128 break;
2129 }
2130
2131 text_glyph_row_found:
2132 if (gr && gy <= y)
2133 {
2134 struct glyph *g = gr->glyphs[area];
2135 struct glyph *end = g + gr->used[area];
2136
2137 height = gr->height;
2138 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2139 if (gx + g->pixel_width > x)
2140 break;
2141
2142 if (g < end)
2143 {
2144 if (g->type == IMAGE_GLYPH)
2145 {
2146 /* Don't remember when mouse is over image, as
2147 image may have hot-spots. */
2148 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2149 return;
2150 }
2151 width = g->pixel_width;
2152 }
2153 else
2154 {
2155 /* Use nominal char spacing at end of line. */
2156 x -= gx;
2157 gx += (x / width) * width;
2158 }
2159
2160 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2161 gx += window_box_left_offset (w, area);
2162 }
2163 else
2164 {
2165 /* Use nominal line height at end of window. */
2166 gx = (x / width) * width;
2167 y -= gy;
2168 gy += (y / height) * height;
2169 }
2170 break;
2171
2172 case ON_LEFT_FRINGE:
2173 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2174 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2175 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2176 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2177 goto row_glyph;
2178
2179 case ON_RIGHT_FRINGE:
2180 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2181 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2182 : window_box_right_offset (w, TEXT_AREA));
2183 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2184 goto row_glyph;
2185
2186 case ON_SCROLL_BAR:
2187 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2188 ? 0
2189 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2190 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2191 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2192 : 0)));
2193 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2194
2195 row_glyph:
2196 gr = 0, gy = 0;
2197 for (; r <= end_row && r->enabled_p; ++r)
2198 if (r->y + r->height > y)
2199 {
2200 gr = r; gy = r->y;
2201 break;
2202 }
2203
2204 if (gr && gy <= y)
2205 height = gr->height;
2206 else
2207 {
2208 /* Use nominal line height at end of window. */
2209 y -= gy;
2210 gy += (y / height) * height;
2211 }
2212 break;
2213
2214 default:
2215 ;
2216 virtual_glyph:
2217 /* If there is no glyph under the mouse, then we divide the screen
2218 into a grid of the smallest glyph in the frame, and use that
2219 as our "glyph". */
2220
2221 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2222 round down even for negative values. */
2223 if (gx < 0)
2224 gx -= width - 1;
2225 if (gy < 0)
2226 gy -= height - 1;
2227
2228 gx = (gx / width) * width;
2229 gy = (gy / height) * height;
2230
2231 goto store_rect;
2232 }
2233
2234 gx += WINDOW_LEFT_EDGE_X (w);
2235 gy += WINDOW_TOP_EDGE_Y (w);
2236
2237 store_rect:
2238 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2239
2240 /* Visible feedback for debugging. */
2241 #if 0
2242 #if HAVE_X_WINDOWS
2243 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2244 f->output_data.x->normal_gc,
2245 gx, gy, width, height);
2246 #endif
2247 #endif
2248 }
2249
2250
2251 #endif /* HAVE_WINDOW_SYSTEM */
2252
2253 \f
2254 /***********************************************************************
2255 Lisp form evaluation
2256 ***********************************************************************/
2257
2258 /* Error handler for safe_eval and safe_call. */
2259
2260 static Lisp_Object
2261 safe_eval_handler (arg)
2262 Lisp_Object arg;
2263 {
2264 add_to_log ("Error during redisplay: %s", arg, Qnil);
2265 return Qnil;
2266 }
2267
2268
2269 /* Evaluate SEXPR and return the result, or nil if something went
2270 wrong. Prevent redisplay during the evaluation. */
2271
2272 Lisp_Object
2273 safe_eval (sexpr)
2274 Lisp_Object sexpr;
2275 {
2276 Lisp_Object val;
2277
2278 if (inhibit_eval_during_redisplay)
2279 val = Qnil;
2280 else
2281 {
2282 int count = SPECPDL_INDEX ();
2283 struct gcpro gcpro1;
2284
2285 GCPRO1 (sexpr);
2286 specbind (Qinhibit_redisplay, Qt);
2287 /* Use Qt to ensure debugger does not run,
2288 so there is no possibility of wanting to redisplay. */
2289 val = internal_condition_case_1 (Feval, sexpr, Qt,
2290 safe_eval_handler);
2291 UNGCPRO;
2292 val = unbind_to (count, val);
2293 }
2294
2295 return val;
2296 }
2297
2298
2299 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2300 Return the result, or nil if something went wrong. Prevent
2301 redisplay during the evaluation. */
2302
2303 Lisp_Object
2304 safe_call (nargs, args)
2305 int nargs;
2306 Lisp_Object *args;
2307 {
2308 Lisp_Object val;
2309
2310 if (inhibit_eval_during_redisplay)
2311 val = Qnil;
2312 else
2313 {
2314 int count = SPECPDL_INDEX ();
2315 struct gcpro gcpro1;
2316
2317 GCPRO1 (args[0]);
2318 gcpro1.nvars = nargs;
2319 specbind (Qinhibit_redisplay, Qt);
2320 /* Use Qt to ensure debugger does not run,
2321 so there is no possibility of wanting to redisplay. */
2322 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2323 safe_eval_handler);
2324 UNGCPRO;
2325 val = unbind_to (count, val);
2326 }
2327
2328 return val;
2329 }
2330
2331
2332 /* Call function FN with one argument ARG.
2333 Return the result, or nil if something went wrong. */
2334
2335 Lisp_Object
2336 safe_call1 (fn, arg)
2337 Lisp_Object fn, arg;
2338 {
2339 Lisp_Object args[2];
2340 args[0] = fn;
2341 args[1] = arg;
2342 return safe_call (2, args);
2343 }
2344
2345
2346 \f
2347 /***********************************************************************
2348 Debugging
2349 ***********************************************************************/
2350
2351 #if 0
2352
2353 /* Define CHECK_IT to perform sanity checks on iterators.
2354 This is for debugging. It is too slow to do unconditionally. */
2355
2356 static void
2357 check_it (it)
2358 struct it *it;
2359 {
2360 if (it->method == GET_FROM_STRING)
2361 {
2362 xassert (STRINGP (it->string));
2363 xassert (IT_STRING_CHARPOS (*it) >= 0);
2364 }
2365 else
2366 {
2367 xassert (IT_STRING_CHARPOS (*it) < 0);
2368 if (it->method == GET_FROM_BUFFER)
2369 {
2370 /* Check that character and byte positions agree. */
2371 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2372 }
2373 }
2374
2375 if (it->dpvec)
2376 xassert (it->current.dpvec_index >= 0);
2377 else
2378 xassert (it->current.dpvec_index < 0);
2379 }
2380
2381 #define CHECK_IT(IT) check_it ((IT))
2382
2383 #else /* not 0 */
2384
2385 #define CHECK_IT(IT) (void) 0
2386
2387 #endif /* not 0 */
2388
2389
2390 #if GLYPH_DEBUG
2391
2392 /* Check that the window end of window W is what we expect it
2393 to be---the last row in the current matrix displaying text. */
2394
2395 static void
2396 check_window_end (w)
2397 struct window *w;
2398 {
2399 if (!MINI_WINDOW_P (w)
2400 && !NILP (w->window_end_valid))
2401 {
2402 struct glyph_row *row;
2403 xassert ((row = MATRIX_ROW (w->current_matrix,
2404 XFASTINT (w->window_end_vpos)),
2405 !row->enabled_p
2406 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2407 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2408 }
2409 }
2410
2411 #define CHECK_WINDOW_END(W) check_window_end ((W))
2412
2413 #else /* not GLYPH_DEBUG */
2414
2415 #define CHECK_WINDOW_END(W) (void) 0
2416
2417 #endif /* not GLYPH_DEBUG */
2418
2419
2420 \f
2421 /***********************************************************************
2422 Iterator initialization
2423 ***********************************************************************/
2424
2425 /* Initialize IT for displaying current_buffer in window W, starting
2426 at character position CHARPOS. CHARPOS < 0 means that no buffer
2427 position is specified which is useful when the iterator is assigned
2428 a position later. BYTEPOS is the byte position corresponding to
2429 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2430
2431 If ROW is not null, calls to produce_glyphs with IT as parameter
2432 will produce glyphs in that row.
2433
2434 BASE_FACE_ID is the id of a base face to use. It must be one of
2435 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2436 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2437 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2438
2439 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2440 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2441 will be initialized to use the corresponding mode line glyph row of
2442 the desired matrix of W. */
2443
2444 void
2445 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2446 struct it *it;
2447 struct window *w;
2448 int charpos, bytepos;
2449 struct glyph_row *row;
2450 enum face_id base_face_id;
2451 {
2452 int highlight_region_p;
2453
2454 /* Some precondition checks. */
2455 xassert (w != NULL && it != NULL);
2456 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2457 && charpos <= ZV));
2458
2459 /* If face attributes have been changed since the last redisplay,
2460 free realized faces now because they depend on face definitions
2461 that might have changed. Don't free faces while there might be
2462 desired matrices pending which reference these faces. */
2463 if (face_change_count && !inhibit_free_realized_faces)
2464 {
2465 face_change_count = 0;
2466 free_all_realized_faces (Qnil);
2467 }
2468
2469 /* Use one of the mode line rows of W's desired matrix if
2470 appropriate. */
2471 if (row == NULL)
2472 {
2473 if (base_face_id == MODE_LINE_FACE_ID
2474 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2475 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2476 else if (base_face_id == HEADER_LINE_FACE_ID)
2477 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2478 }
2479
2480 /* Clear IT. */
2481 bzero (it, sizeof *it);
2482 it->current.overlay_string_index = -1;
2483 it->current.dpvec_index = -1;
2484 it->base_face_id = base_face_id;
2485 it->string = Qnil;
2486 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2487
2488 /* The window in which we iterate over current_buffer: */
2489 XSETWINDOW (it->window, w);
2490 it->w = w;
2491 it->f = XFRAME (w->frame);
2492
2493 /* Extra space between lines (on window systems only). */
2494 if (base_face_id == DEFAULT_FACE_ID
2495 && FRAME_WINDOW_P (it->f))
2496 {
2497 if (NATNUMP (current_buffer->extra_line_spacing))
2498 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2499 else if (FLOATP (current_buffer->extra_line_spacing))
2500 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2501 * FRAME_LINE_HEIGHT (it->f));
2502 else if (it->f->extra_line_spacing > 0)
2503 it->extra_line_spacing = it->f->extra_line_spacing;
2504 it->max_extra_line_spacing = 0;
2505 }
2506
2507 /* If realized faces have been removed, e.g. because of face
2508 attribute changes of named faces, recompute them. When running
2509 in batch mode, the face cache of Vterminal_frame is null. If
2510 we happen to get called, make a dummy face cache. */
2511 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2512 init_frame_faces (it->f);
2513 if (FRAME_FACE_CACHE (it->f)->used == 0)
2514 recompute_basic_faces (it->f);
2515
2516 /* Current value of the `slice', `space-width', and 'height' properties. */
2517 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2518 it->space_width = Qnil;
2519 it->font_height = Qnil;
2520 it->override_ascent = -1;
2521
2522 /* Are control characters displayed as `^C'? */
2523 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2524
2525 /* -1 means everything between a CR and the following line end
2526 is invisible. >0 means lines indented more than this value are
2527 invisible. */
2528 it->selective = (INTEGERP (current_buffer->selective_display)
2529 ? XFASTINT (current_buffer->selective_display)
2530 : (!NILP (current_buffer->selective_display)
2531 ? -1 : 0));
2532 it->selective_display_ellipsis_p
2533 = !NILP (current_buffer->selective_display_ellipses);
2534
2535 /* Display table to use. */
2536 it->dp = window_display_table (w);
2537
2538 /* Are multibyte characters enabled in current_buffer? */
2539 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2540
2541 /* Non-zero if we should highlight the region. */
2542 highlight_region_p
2543 = (!NILP (Vtransient_mark_mode)
2544 && !NILP (current_buffer->mark_active)
2545 && XMARKER (current_buffer->mark)->buffer != 0);
2546
2547 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2548 start and end of a visible region in window IT->w. Set both to
2549 -1 to indicate no region. */
2550 if (highlight_region_p
2551 /* Maybe highlight only in selected window. */
2552 && (/* Either show region everywhere. */
2553 highlight_nonselected_windows
2554 /* Or show region in the selected window. */
2555 || w == XWINDOW (selected_window)
2556 /* Or show the region if we are in the mini-buffer and W is
2557 the window the mini-buffer refers to. */
2558 || (MINI_WINDOW_P (XWINDOW (selected_window))
2559 && WINDOWP (minibuf_selected_window)
2560 && w == XWINDOW (minibuf_selected_window))))
2561 {
2562 int charpos = marker_position (current_buffer->mark);
2563 it->region_beg_charpos = min (PT, charpos);
2564 it->region_end_charpos = max (PT, charpos);
2565 }
2566 else
2567 it->region_beg_charpos = it->region_end_charpos = -1;
2568
2569 /* Get the position at which the redisplay_end_trigger hook should
2570 be run, if it is to be run at all. */
2571 if (MARKERP (w->redisplay_end_trigger)
2572 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2573 it->redisplay_end_trigger_charpos
2574 = marker_position (w->redisplay_end_trigger);
2575 else if (INTEGERP (w->redisplay_end_trigger))
2576 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2577
2578 /* Correct bogus values of tab_width. */
2579 it->tab_width = XINT (current_buffer->tab_width);
2580 if (it->tab_width <= 0 || it->tab_width > 1000)
2581 it->tab_width = 8;
2582
2583 /* Are lines in the display truncated? */
2584 it->truncate_lines_p
2585 = (base_face_id != DEFAULT_FACE_ID
2586 || XINT (it->w->hscroll)
2587 || (truncate_partial_width_windows
2588 && !WINDOW_FULL_WIDTH_P (it->w))
2589 || !NILP (current_buffer->truncate_lines));
2590
2591 /* Get dimensions of truncation and continuation glyphs. These are
2592 displayed as fringe bitmaps under X, so we don't need them for such
2593 frames. */
2594 if (!FRAME_WINDOW_P (it->f))
2595 {
2596 if (it->truncate_lines_p)
2597 {
2598 /* We will need the truncation glyph. */
2599 xassert (it->glyph_row == NULL);
2600 produce_special_glyphs (it, IT_TRUNCATION);
2601 it->truncation_pixel_width = it->pixel_width;
2602 }
2603 else
2604 {
2605 /* We will need the continuation glyph. */
2606 xassert (it->glyph_row == NULL);
2607 produce_special_glyphs (it, IT_CONTINUATION);
2608 it->continuation_pixel_width = it->pixel_width;
2609 }
2610
2611 /* Reset these values to zero because the produce_special_glyphs
2612 above has changed them. */
2613 it->pixel_width = it->ascent = it->descent = 0;
2614 it->phys_ascent = it->phys_descent = 0;
2615 }
2616
2617 /* Set this after getting the dimensions of truncation and
2618 continuation glyphs, so that we don't produce glyphs when calling
2619 produce_special_glyphs, above. */
2620 it->glyph_row = row;
2621 it->area = TEXT_AREA;
2622
2623 /* Get the dimensions of the display area. The display area
2624 consists of the visible window area plus a horizontally scrolled
2625 part to the left of the window. All x-values are relative to the
2626 start of this total display area. */
2627 if (base_face_id != DEFAULT_FACE_ID)
2628 {
2629 /* Mode lines, menu bar in terminal frames. */
2630 it->first_visible_x = 0;
2631 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2632 }
2633 else
2634 {
2635 it->first_visible_x
2636 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2637 it->last_visible_x = (it->first_visible_x
2638 + window_box_width (w, TEXT_AREA));
2639
2640 /* If we truncate lines, leave room for the truncator glyph(s) at
2641 the right margin. Otherwise, leave room for the continuation
2642 glyph(s). Truncation and continuation glyphs are not inserted
2643 for window-based redisplay. */
2644 if (!FRAME_WINDOW_P (it->f))
2645 {
2646 if (it->truncate_lines_p)
2647 it->last_visible_x -= it->truncation_pixel_width;
2648 else
2649 it->last_visible_x -= it->continuation_pixel_width;
2650 }
2651
2652 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2653 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2654 }
2655
2656 /* Leave room for a border glyph. */
2657 if (!FRAME_WINDOW_P (it->f)
2658 && !WINDOW_RIGHTMOST_P (it->w))
2659 it->last_visible_x -= 1;
2660
2661 it->last_visible_y = window_text_bottom_y (w);
2662
2663 /* For mode lines and alike, arrange for the first glyph having a
2664 left box line if the face specifies a box. */
2665 if (base_face_id != DEFAULT_FACE_ID)
2666 {
2667 struct face *face;
2668
2669 it->face_id = base_face_id;
2670
2671 /* If we have a boxed mode line, make the first character appear
2672 with a left box line. */
2673 face = FACE_FROM_ID (it->f, base_face_id);
2674 if (face->box != FACE_NO_BOX)
2675 it->start_of_box_run_p = 1;
2676 }
2677
2678 /* If a buffer position was specified, set the iterator there,
2679 getting overlays and face properties from that position. */
2680 if (charpos >= BUF_BEG (current_buffer))
2681 {
2682 it->end_charpos = ZV;
2683 it->face_id = -1;
2684 IT_CHARPOS (*it) = charpos;
2685
2686 /* Compute byte position if not specified. */
2687 if (bytepos < charpos)
2688 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2689 else
2690 IT_BYTEPOS (*it) = bytepos;
2691
2692 it->start = it->current;
2693
2694 /* Compute faces etc. */
2695 reseat (it, it->current.pos, 1);
2696 }
2697
2698 CHECK_IT (it);
2699 }
2700
2701
2702 /* Initialize IT for the display of window W with window start POS. */
2703
2704 void
2705 start_display (it, w, pos)
2706 struct it *it;
2707 struct window *w;
2708 struct text_pos pos;
2709 {
2710 struct glyph_row *row;
2711 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2712
2713 row = w->desired_matrix->rows + first_vpos;
2714 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2715 it->first_vpos = first_vpos;
2716
2717 /* Don't reseat to previous visible line start if current start
2718 position is in a string or image. */
2719 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2720 {
2721 int start_at_line_beg_p;
2722 int first_y = it->current_y;
2723
2724 /* If window start is not at a line start, skip forward to POS to
2725 get the correct continuation lines width. */
2726 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2727 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2728 if (!start_at_line_beg_p)
2729 {
2730 int new_x;
2731
2732 reseat_at_previous_visible_line_start (it);
2733 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2734
2735 new_x = it->current_x + it->pixel_width;
2736
2737 /* If lines are continued, this line may end in the middle
2738 of a multi-glyph character (e.g. a control character
2739 displayed as \003, or in the middle of an overlay
2740 string). In this case move_it_to above will not have
2741 taken us to the start of the continuation line but to the
2742 end of the continued line. */
2743 if (it->current_x > 0
2744 && !it->truncate_lines_p /* Lines are continued. */
2745 && (/* And glyph doesn't fit on the line. */
2746 new_x > it->last_visible_x
2747 /* Or it fits exactly and we're on a window
2748 system frame. */
2749 || (new_x == it->last_visible_x
2750 && FRAME_WINDOW_P (it->f))))
2751 {
2752 if (it->current.dpvec_index >= 0
2753 || it->current.overlay_string_index >= 0)
2754 {
2755 set_iterator_to_next (it, 1);
2756 move_it_in_display_line_to (it, -1, -1, 0);
2757 }
2758
2759 it->continuation_lines_width += it->current_x;
2760 }
2761
2762 /* We're starting a new display line, not affected by the
2763 height of the continued line, so clear the appropriate
2764 fields in the iterator structure. */
2765 it->max_ascent = it->max_descent = 0;
2766 it->max_phys_ascent = it->max_phys_descent = 0;
2767
2768 it->current_y = first_y;
2769 it->vpos = 0;
2770 it->current_x = it->hpos = 0;
2771 }
2772 }
2773
2774 #if 0 /* Don't assert the following because start_display is sometimes
2775 called intentionally with a window start that is not at a
2776 line start. Please leave this code in as a comment. */
2777
2778 /* Window start should be on a line start, now. */
2779 xassert (it->continuation_lines_width
2780 || IT_CHARPOS (it) == BEGV
2781 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2782 #endif /* 0 */
2783 }
2784
2785
2786 /* Return 1 if POS is a position in ellipses displayed for invisible
2787 text. W is the window we display, for text property lookup. */
2788
2789 static int
2790 in_ellipses_for_invisible_text_p (pos, w)
2791 struct display_pos *pos;
2792 struct window *w;
2793 {
2794 Lisp_Object prop, window;
2795 int ellipses_p = 0;
2796 int charpos = CHARPOS (pos->pos);
2797
2798 /* If POS specifies a position in a display vector, this might
2799 be for an ellipsis displayed for invisible text. We won't
2800 get the iterator set up for delivering that ellipsis unless
2801 we make sure that it gets aware of the invisible text. */
2802 if (pos->dpvec_index >= 0
2803 && pos->overlay_string_index < 0
2804 && CHARPOS (pos->string_pos) < 0
2805 && charpos > BEGV
2806 && (XSETWINDOW (window, w),
2807 prop = Fget_char_property (make_number (charpos),
2808 Qinvisible, window),
2809 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2810 {
2811 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2812 window);
2813 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2814 }
2815
2816 return ellipses_p;
2817 }
2818
2819
2820 /* Initialize IT for stepping through current_buffer in window W,
2821 starting at position POS that includes overlay string and display
2822 vector/ control character translation position information. Value
2823 is zero if there are overlay strings with newlines at POS. */
2824
2825 static int
2826 init_from_display_pos (it, w, pos)
2827 struct it *it;
2828 struct window *w;
2829 struct display_pos *pos;
2830 {
2831 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2832 int i, overlay_strings_with_newlines = 0;
2833
2834 /* If POS specifies a position in a display vector, this might
2835 be for an ellipsis displayed for invisible text. We won't
2836 get the iterator set up for delivering that ellipsis unless
2837 we make sure that it gets aware of the invisible text. */
2838 if (in_ellipses_for_invisible_text_p (pos, w))
2839 {
2840 --charpos;
2841 bytepos = 0;
2842 }
2843
2844 /* Keep in mind: the call to reseat in init_iterator skips invisible
2845 text, so we might end up at a position different from POS. This
2846 is only a problem when POS is a row start after a newline and an
2847 overlay starts there with an after-string, and the overlay has an
2848 invisible property. Since we don't skip invisible text in
2849 display_line and elsewhere immediately after consuming the
2850 newline before the row start, such a POS will not be in a string,
2851 but the call to init_iterator below will move us to the
2852 after-string. */
2853 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2854
2855 /* This only scans the current chunk -- it should scan all chunks.
2856 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2857 to 16 in 22.1 to make this a lesser problem. */
2858 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2859 {
2860 const char *s = SDATA (it->overlay_strings[i]);
2861 const char *e = s + SBYTES (it->overlay_strings[i]);
2862
2863 while (s < e && *s != '\n')
2864 ++s;
2865
2866 if (s < e)
2867 {
2868 overlay_strings_with_newlines = 1;
2869 break;
2870 }
2871 }
2872
2873 /* If position is within an overlay string, set up IT to the right
2874 overlay string. */
2875 if (pos->overlay_string_index >= 0)
2876 {
2877 int relative_index;
2878
2879 /* If the first overlay string happens to have a `display'
2880 property for an image, the iterator will be set up for that
2881 image, and we have to undo that setup first before we can
2882 correct the overlay string index. */
2883 if (it->method == GET_FROM_IMAGE)
2884 pop_it (it);
2885
2886 /* We already have the first chunk of overlay strings in
2887 IT->overlay_strings. Load more until the one for
2888 pos->overlay_string_index is in IT->overlay_strings. */
2889 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2890 {
2891 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2892 it->current.overlay_string_index = 0;
2893 while (n--)
2894 {
2895 load_overlay_strings (it, 0);
2896 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2897 }
2898 }
2899
2900 it->current.overlay_string_index = pos->overlay_string_index;
2901 relative_index = (it->current.overlay_string_index
2902 % OVERLAY_STRING_CHUNK_SIZE);
2903 it->string = it->overlay_strings[relative_index];
2904 xassert (STRINGP (it->string));
2905 it->current.string_pos = pos->string_pos;
2906 it->method = GET_FROM_STRING;
2907 }
2908
2909 #if 0 /* This is bogus because POS not having an overlay string
2910 position does not mean it's after the string. Example: A
2911 line starting with a before-string and initialization of IT
2912 to the previous row's end position. */
2913 else if (it->current.overlay_string_index >= 0)
2914 {
2915 /* If POS says we're already after an overlay string ending at
2916 POS, make sure to pop the iterator because it will be in
2917 front of that overlay string. When POS is ZV, we've thereby
2918 also ``processed'' overlay strings at ZV. */
2919 while (it->sp)
2920 pop_it (it);
2921 it->current.overlay_string_index = -1;
2922 it->method = GET_FROM_BUFFER;
2923 if (CHARPOS (pos->pos) == ZV)
2924 it->overlay_strings_at_end_processed_p = 1;
2925 }
2926 #endif /* 0 */
2927
2928 if (CHARPOS (pos->string_pos) >= 0)
2929 {
2930 /* Recorded position is not in an overlay string, but in another
2931 string. This can only be a string from a `display' property.
2932 IT should already be filled with that string. */
2933 it->current.string_pos = pos->string_pos;
2934 xassert (STRINGP (it->string));
2935 }
2936
2937 /* Restore position in display vector translations, control
2938 character translations or ellipses. */
2939 if (pos->dpvec_index >= 0)
2940 {
2941 if (it->dpvec == NULL)
2942 get_next_display_element (it);
2943 xassert (it->dpvec && it->current.dpvec_index == 0);
2944 it->current.dpvec_index = pos->dpvec_index;
2945 }
2946
2947 CHECK_IT (it);
2948 return !overlay_strings_with_newlines;
2949 }
2950
2951
2952 /* Initialize IT for stepping through current_buffer in window W
2953 starting at ROW->start. */
2954
2955 static void
2956 init_to_row_start (it, w, row)
2957 struct it *it;
2958 struct window *w;
2959 struct glyph_row *row;
2960 {
2961 init_from_display_pos (it, w, &row->start);
2962 it->start = row->start;
2963 it->continuation_lines_width = row->continuation_lines_width;
2964 CHECK_IT (it);
2965 }
2966
2967
2968 /* Initialize IT for stepping through current_buffer in window W
2969 starting in the line following ROW, i.e. starting at ROW->end.
2970 Value is zero if there are overlay strings with newlines at ROW's
2971 end position. */
2972
2973 static int
2974 init_to_row_end (it, w, row)
2975 struct it *it;
2976 struct window *w;
2977 struct glyph_row *row;
2978 {
2979 int success = 0;
2980
2981 if (init_from_display_pos (it, w, &row->end))
2982 {
2983 if (row->continued_p)
2984 it->continuation_lines_width
2985 = row->continuation_lines_width + row->pixel_width;
2986 CHECK_IT (it);
2987 success = 1;
2988 }
2989
2990 return success;
2991 }
2992
2993
2994
2995 \f
2996 /***********************************************************************
2997 Text properties
2998 ***********************************************************************/
2999
3000 /* Called when IT reaches IT->stop_charpos. Handle text property and
3001 overlay changes. Set IT->stop_charpos to the next position where
3002 to stop. */
3003
3004 static void
3005 handle_stop (it)
3006 struct it *it;
3007 {
3008 enum prop_handled handled;
3009 int handle_overlay_change_p;
3010 struct props *p;
3011
3012 it->dpvec = NULL;
3013 it->current.dpvec_index = -1;
3014 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3015 it->ignore_overlay_strings_at_pos_p = 0;
3016
3017 /* Use face of preceding text for ellipsis (if invisible) */
3018 if (it->selective_display_ellipsis_p)
3019 it->saved_face_id = it->face_id;
3020
3021 do
3022 {
3023 handled = HANDLED_NORMALLY;
3024
3025 /* Call text property handlers. */
3026 for (p = it_props; p->handler; ++p)
3027 {
3028 handled = p->handler (it);
3029
3030 if (handled == HANDLED_RECOMPUTE_PROPS)
3031 break;
3032 else if (handled == HANDLED_RETURN)
3033 return;
3034 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3035 handle_overlay_change_p = 0;
3036 }
3037
3038 if (handled != HANDLED_RECOMPUTE_PROPS)
3039 {
3040 /* Don't check for overlay strings below when set to deliver
3041 characters from a display vector. */
3042 if (it->method == GET_FROM_DISPLAY_VECTOR)
3043 handle_overlay_change_p = 0;
3044
3045 /* Handle overlay changes. */
3046 if (handle_overlay_change_p)
3047 handled = handle_overlay_change (it);
3048
3049 /* Determine where to stop next. */
3050 if (handled == HANDLED_NORMALLY)
3051 compute_stop_pos (it);
3052 }
3053 }
3054 while (handled == HANDLED_RECOMPUTE_PROPS);
3055 }
3056
3057
3058 /* Compute IT->stop_charpos from text property and overlay change
3059 information for IT's current position. */
3060
3061 static void
3062 compute_stop_pos (it)
3063 struct it *it;
3064 {
3065 register INTERVAL iv, next_iv;
3066 Lisp_Object object, limit, position;
3067
3068 /* If nowhere else, stop at the end. */
3069 it->stop_charpos = it->end_charpos;
3070
3071 if (STRINGP (it->string))
3072 {
3073 /* Strings are usually short, so don't limit the search for
3074 properties. */
3075 object = it->string;
3076 limit = Qnil;
3077 position = make_number (IT_STRING_CHARPOS (*it));
3078 }
3079 else
3080 {
3081 int charpos;
3082
3083 /* If next overlay change is in front of the current stop pos
3084 (which is IT->end_charpos), stop there. Note: value of
3085 next_overlay_change is point-max if no overlay change
3086 follows. */
3087 charpos = next_overlay_change (IT_CHARPOS (*it));
3088 if (charpos < it->stop_charpos)
3089 it->stop_charpos = charpos;
3090
3091 /* If showing the region, we have to stop at the region
3092 start or end because the face might change there. */
3093 if (it->region_beg_charpos > 0)
3094 {
3095 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3096 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3097 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3098 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3099 }
3100
3101 /* Set up variables for computing the stop position from text
3102 property changes. */
3103 XSETBUFFER (object, current_buffer);
3104 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3105 position = make_number (IT_CHARPOS (*it));
3106
3107 }
3108
3109 /* Get the interval containing IT's position. Value is a null
3110 interval if there isn't such an interval. */
3111 iv = validate_interval_range (object, &position, &position, 0);
3112 if (!NULL_INTERVAL_P (iv))
3113 {
3114 Lisp_Object values_here[LAST_PROP_IDX];
3115 struct props *p;
3116
3117 /* Get properties here. */
3118 for (p = it_props; p->handler; ++p)
3119 values_here[p->idx] = textget (iv->plist, *p->name);
3120
3121 /* Look for an interval following iv that has different
3122 properties. */
3123 for (next_iv = next_interval (iv);
3124 (!NULL_INTERVAL_P (next_iv)
3125 && (NILP (limit)
3126 || XFASTINT (limit) > next_iv->position));
3127 next_iv = next_interval (next_iv))
3128 {
3129 for (p = it_props; p->handler; ++p)
3130 {
3131 Lisp_Object new_value;
3132
3133 new_value = textget (next_iv->plist, *p->name);
3134 if (!EQ (values_here[p->idx], new_value))
3135 break;
3136 }
3137
3138 if (p->handler)
3139 break;
3140 }
3141
3142 if (!NULL_INTERVAL_P (next_iv))
3143 {
3144 if (INTEGERP (limit)
3145 && next_iv->position >= XFASTINT (limit))
3146 /* No text property change up to limit. */
3147 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3148 else
3149 /* Text properties change in next_iv. */
3150 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3151 }
3152 }
3153
3154 xassert (STRINGP (it->string)
3155 || (it->stop_charpos >= BEGV
3156 && it->stop_charpos >= IT_CHARPOS (*it)));
3157 }
3158
3159
3160 /* Return the position of the next overlay change after POS in
3161 current_buffer. Value is point-max if no overlay change
3162 follows. This is like `next-overlay-change' but doesn't use
3163 xmalloc. */
3164
3165 static int
3166 next_overlay_change (pos)
3167 int pos;
3168 {
3169 int noverlays;
3170 int endpos;
3171 Lisp_Object *overlays;
3172 int i;
3173
3174 /* Get all overlays at the given position. */
3175 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3176
3177 /* If any of these overlays ends before endpos,
3178 use its ending point instead. */
3179 for (i = 0; i < noverlays; ++i)
3180 {
3181 Lisp_Object oend;
3182 int oendpos;
3183
3184 oend = OVERLAY_END (overlays[i]);
3185 oendpos = OVERLAY_POSITION (oend);
3186 endpos = min (endpos, oendpos);
3187 }
3188
3189 return endpos;
3190 }
3191
3192
3193 \f
3194 /***********************************************************************
3195 Fontification
3196 ***********************************************************************/
3197
3198 /* Handle changes in the `fontified' property of the current buffer by
3199 calling hook functions from Qfontification_functions to fontify
3200 regions of text. */
3201
3202 static enum prop_handled
3203 handle_fontified_prop (it)
3204 struct it *it;
3205 {
3206 Lisp_Object prop, pos;
3207 enum prop_handled handled = HANDLED_NORMALLY;
3208
3209 if (!NILP (Vmemory_full))
3210 return handled;
3211
3212 /* Get the value of the `fontified' property at IT's current buffer
3213 position. (The `fontified' property doesn't have a special
3214 meaning in strings.) If the value is nil, call functions from
3215 Qfontification_functions. */
3216 if (!STRINGP (it->string)
3217 && it->s == NULL
3218 && !NILP (Vfontification_functions)
3219 && !NILP (Vrun_hooks)
3220 && (pos = make_number (IT_CHARPOS (*it)),
3221 prop = Fget_char_property (pos, Qfontified, Qnil),
3222 NILP (prop)))
3223 {
3224 int count = SPECPDL_INDEX ();
3225 Lisp_Object val;
3226
3227 val = Vfontification_functions;
3228 specbind (Qfontification_functions, Qnil);
3229
3230 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3231 safe_call1 (val, pos);
3232 else
3233 {
3234 Lisp_Object globals, fn;
3235 struct gcpro gcpro1, gcpro2;
3236
3237 globals = Qnil;
3238 GCPRO2 (val, globals);
3239
3240 for (; CONSP (val); val = XCDR (val))
3241 {
3242 fn = XCAR (val);
3243
3244 if (EQ (fn, Qt))
3245 {
3246 /* A value of t indicates this hook has a local
3247 binding; it means to run the global binding too.
3248 In a global value, t should not occur. If it
3249 does, we must ignore it to avoid an endless
3250 loop. */
3251 for (globals = Fdefault_value (Qfontification_functions);
3252 CONSP (globals);
3253 globals = XCDR (globals))
3254 {
3255 fn = XCAR (globals);
3256 if (!EQ (fn, Qt))
3257 safe_call1 (fn, pos);
3258 }
3259 }
3260 else
3261 safe_call1 (fn, pos);
3262 }
3263
3264 UNGCPRO;
3265 }
3266
3267 unbind_to (count, Qnil);
3268
3269 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3270 something. This avoids an endless loop if they failed to
3271 fontify the text for which reason ever. */
3272 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3273 handled = HANDLED_RECOMPUTE_PROPS;
3274 }
3275
3276 return handled;
3277 }
3278
3279
3280 \f
3281 /***********************************************************************
3282 Faces
3283 ***********************************************************************/
3284
3285 /* Set up iterator IT from face properties at its current position.
3286 Called from handle_stop. */
3287
3288 static enum prop_handled
3289 handle_face_prop (it)
3290 struct it *it;
3291 {
3292 int new_face_id, next_stop;
3293
3294 if (!STRINGP (it->string))
3295 {
3296 new_face_id
3297 = face_at_buffer_position (it->w,
3298 IT_CHARPOS (*it),
3299 it->region_beg_charpos,
3300 it->region_end_charpos,
3301 &next_stop,
3302 (IT_CHARPOS (*it)
3303 + TEXT_PROP_DISTANCE_LIMIT),
3304 0);
3305
3306 /* Is this a start of a run of characters with box face?
3307 Caveat: this can be called for a freshly initialized
3308 iterator; face_id is -1 in this case. We know that the new
3309 face will not change until limit, i.e. if the new face has a
3310 box, all characters up to limit will have one. But, as
3311 usual, we don't know whether limit is really the end. */
3312 if (new_face_id != it->face_id)
3313 {
3314 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3315
3316 /* If new face has a box but old face has not, this is
3317 the start of a run of characters with box, i.e. it has
3318 a shadow on the left side. The value of face_id of the
3319 iterator will be -1 if this is the initial call that gets
3320 the face. In this case, we have to look in front of IT's
3321 position and see whether there is a face != new_face_id. */
3322 it->start_of_box_run_p
3323 = (new_face->box != FACE_NO_BOX
3324 && (it->face_id >= 0
3325 || IT_CHARPOS (*it) == BEG
3326 || new_face_id != face_before_it_pos (it)));
3327 it->face_box_p = new_face->box != FACE_NO_BOX;
3328 }
3329 }
3330 else
3331 {
3332 int base_face_id, bufpos;
3333
3334 if (it->current.overlay_string_index >= 0)
3335 bufpos = IT_CHARPOS (*it);
3336 else
3337 bufpos = 0;
3338
3339 /* For strings from a buffer, i.e. overlay strings or strings
3340 from a `display' property, use the face at IT's current
3341 buffer position as the base face to merge with, so that
3342 overlay strings appear in the same face as surrounding
3343 text, unless they specify their own faces. */
3344 base_face_id = underlying_face_id (it);
3345
3346 new_face_id = face_at_string_position (it->w,
3347 it->string,
3348 IT_STRING_CHARPOS (*it),
3349 bufpos,
3350 it->region_beg_charpos,
3351 it->region_end_charpos,
3352 &next_stop,
3353 base_face_id, 0);
3354
3355 #if 0 /* This shouldn't be neccessary. Let's check it. */
3356 /* If IT is used to display a mode line we would really like to
3357 use the mode line face instead of the frame's default face. */
3358 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3359 && new_face_id == DEFAULT_FACE_ID)
3360 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3361 #endif
3362
3363 /* Is this a start of a run of characters with box? Caveat:
3364 this can be called for a freshly allocated iterator; face_id
3365 is -1 is this case. We know that the new face will not
3366 change until the next check pos, i.e. if the new face has a
3367 box, all characters up to that position will have a
3368 box. But, as usual, we don't know whether that position
3369 is really the end. */
3370 if (new_face_id != it->face_id)
3371 {
3372 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3373 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3374
3375 /* If new face has a box but old face hasn't, this is the
3376 start of a run of characters with box, i.e. it has a
3377 shadow on the left side. */
3378 it->start_of_box_run_p
3379 = new_face->box && (old_face == NULL || !old_face->box);
3380 it->face_box_p = new_face->box != FACE_NO_BOX;
3381 }
3382 }
3383
3384 it->face_id = new_face_id;
3385 return HANDLED_NORMALLY;
3386 }
3387
3388
3389 /* Return the ID of the face ``underlying'' IT's current position,
3390 which is in a string. If the iterator is associated with a
3391 buffer, return the face at IT's current buffer position.
3392 Otherwise, use the iterator's base_face_id. */
3393
3394 static int
3395 underlying_face_id (it)
3396 struct it *it;
3397 {
3398 int face_id = it->base_face_id, i;
3399
3400 xassert (STRINGP (it->string));
3401
3402 for (i = it->sp - 1; i >= 0; --i)
3403 if (NILP (it->stack[i].string))
3404 face_id = it->stack[i].face_id;
3405
3406 return face_id;
3407 }
3408
3409
3410 /* Compute the face one character before or after the current position
3411 of IT. BEFORE_P non-zero means get the face in front of IT's
3412 position. Value is the id of the face. */
3413
3414 static int
3415 face_before_or_after_it_pos (it, before_p)
3416 struct it *it;
3417 int before_p;
3418 {
3419 int face_id, limit;
3420 int next_check_charpos;
3421 struct text_pos pos;
3422
3423 xassert (it->s == NULL);
3424
3425 if (STRINGP (it->string))
3426 {
3427 int bufpos, base_face_id;
3428
3429 /* No face change past the end of the string (for the case
3430 we are padding with spaces). No face change before the
3431 string start. */
3432 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3433 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3434 return it->face_id;
3435
3436 /* Set pos to the position before or after IT's current position. */
3437 if (before_p)
3438 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3439 else
3440 /* For composition, we must check the character after the
3441 composition. */
3442 pos = (it->what == IT_COMPOSITION
3443 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3444 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3445
3446 if (it->current.overlay_string_index >= 0)
3447 bufpos = IT_CHARPOS (*it);
3448 else
3449 bufpos = 0;
3450
3451 base_face_id = underlying_face_id (it);
3452
3453 /* Get the face for ASCII, or unibyte. */
3454 face_id = face_at_string_position (it->w,
3455 it->string,
3456 CHARPOS (pos),
3457 bufpos,
3458 it->region_beg_charpos,
3459 it->region_end_charpos,
3460 &next_check_charpos,
3461 base_face_id, 0);
3462
3463 /* Correct the face for charsets different from ASCII. Do it
3464 for the multibyte case only. The face returned above is
3465 suitable for unibyte text if IT->string is unibyte. */
3466 if (STRING_MULTIBYTE (it->string))
3467 {
3468 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3469 int rest = SBYTES (it->string) - BYTEPOS (pos);
3470 int c, len;
3471 struct face *face = FACE_FROM_ID (it->f, face_id);
3472
3473 c = string_char_and_length (p, rest, &len);
3474 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3475 }
3476 }
3477 else
3478 {
3479 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3480 || (IT_CHARPOS (*it) <= BEGV && before_p))
3481 return it->face_id;
3482
3483 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3484 pos = it->current.pos;
3485
3486 if (before_p)
3487 DEC_TEXT_POS (pos, it->multibyte_p);
3488 else
3489 {
3490 if (it->what == IT_COMPOSITION)
3491 /* For composition, we must check the position after the
3492 composition. */
3493 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3494 else
3495 INC_TEXT_POS (pos, it->multibyte_p);
3496 }
3497
3498 /* Determine face for CHARSET_ASCII, or unibyte. */
3499 face_id = face_at_buffer_position (it->w,
3500 CHARPOS (pos),
3501 it->region_beg_charpos,
3502 it->region_end_charpos,
3503 &next_check_charpos,
3504 limit, 0);
3505
3506 /* Correct the face for charsets different from ASCII. Do it
3507 for the multibyte case only. The face returned above is
3508 suitable for unibyte text if current_buffer is unibyte. */
3509 if (it->multibyte_p)
3510 {
3511 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3512 struct face *face = FACE_FROM_ID (it->f, face_id);
3513 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3514 }
3515 }
3516
3517 return face_id;
3518 }
3519
3520
3521 \f
3522 /***********************************************************************
3523 Invisible text
3524 ***********************************************************************/
3525
3526 /* Set up iterator IT from invisible properties at its current
3527 position. Called from handle_stop. */
3528
3529 static enum prop_handled
3530 handle_invisible_prop (it)
3531 struct it *it;
3532 {
3533 enum prop_handled handled = HANDLED_NORMALLY;
3534
3535 if (STRINGP (it->string))
3536 {
3537 extern Lisp_Object Qinvisible;
3538 Lisp_Object prop, end_charpos, limit, charpos;
3539
3540 /* Get the value of the invisible text property at the
3541 current position. Value will be nil if there is no such
3542 property. */
3543 charpos = make_number (IT_STRING_CHARPOS (*it));
3544 prop = Fget_text_property (charpos, Qinvisible, it->string);
3545
3546 if (!NILP (prop)
3547 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3548 {
3549 handled = HANDLED_RECOMPUTE_PROPS;
3550
3551 /* Get the position at which the next change of the
3552 invisible text property can be found in IT->string.
3553 Value will be nil if the property value is the same for
3554 all the rest of IT->string. */
3555 XSETINT (limit, SCHARS (it->string));
3556 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3557 it->string, limit);
3558
3559 /* Text at current position is invisible. The next
3560 change in the property is at position end_charpos.
3561 Move IT's current position to that position. */
3562 if (INTEGERP (end_charpos)
3563 && XFASTINT (end_charpos) < XFASTINT (limit))
3564 {
3565 struct text_pos old;
3566 old = it->current.string_pos;
3567 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3568 compute_string_pos (&it->current.string_pos, old, it->string);
3569 }
3570 else
3571 {
3572 /* The rest of the string is invisible. If this is an
3573 overlay string, proceed with the next overlay string
3574 or whatever comes and return a character from there. */
3575 if (it->current.overlay_string_index >= 0)
3576 {
3577 next_overlay_string (it);
3578 /* Don't check for overlay strings when we just
3579 finished processing them. */
3580 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3581 }
3582 else
3583 {
3584 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3585 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3586 }
3587 }
3588 }
3589 }
3590 else
3591 {
3592 int invis_p, newpos, next_stop, start_charpos;
3593 Lisp_Object pos, prop, overlay;
3594
3595 /* First of all, is there invisible text at this position? */
3596 start_charpos = IT_CHARPOS (*it);
3597 pos = make_number (IT_CHARPOS (*it));
3598 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3599 &overlay);
3600 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3601
3602 /* If we are on invisible text, skip over it. */
3603 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3604 {
3605 /* Record whether we have to display an ellipsis for the
3606 invisible text. */
3607 int display_ellipsis_p = invis_p == 2;
3608
3609 handled = HANDLED_RECOMPUTE_PROPS;
3610
3611 /* Loop skipping over invisible text. The loop is left at
3612 ZV or with IT on the first char being visible again. */
3613 do
3614 {
3615 /* Try to skip some invisible text. Return value is the
3616 position reached which can be equal to IT's position
3617 if there is nothing invisible here. This skips both
3618 over invisible text properties and overlays with
3619 invisible property. */
3620 newpos = skip_invisible (IT_CHARPOS (*it),
3621 &next_stop, ZV, it->window);
3622
3623 /* If we skipped nothing at all we weren't at invisible
3624 text in the first place. If everything to the end of
3625 the buffer was skipped, end the loop. */
3626 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3627 invis_p = 0;
3628 else
3629 {
3630 /* We skipped some characters but not necessarily
3631 all there are. Check if we ended up on visible
3632 text. Fget_char_property returns the property of
3633 the char before the given position, i.e. if we
3634 get invis_p = 0, this means that the char at
3635 newpos is visible. */
3636 pos = make_number (newpos);
3637 prop = Fget_char_property (pos, Qinvisible, it->window);
3638 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3639 }
3640
3641 /* If we ended up on invisible text, proceed to
3642 skip starting with next_stop. */
3643 if (invis_p)
3644 IT_CHARPOS (*it) = next_stop;
3645
3646 /* If there are adjacent invisible texts, don't lose the
3647 second one's ellipsis. */
3648 if (invis_p == 2)
3649 display_ellipsis_p = 1;
3650 }
3651 while (invis_p);
3652
3653 /* The position newpos is now either ZV or on visible text. */
3654 IT_CHARPOS (*it) = newpos;
3655 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3656
3657 /* If there are before-strings at the start of invisible
3658 text, and the text is invisible because of a text
3659 property, arrange to show before-strings because 20.x did
3660 it that way. (If the text is invisible because of an
3661 overlay property instead of a text property, this is
3662 already handled in the overlay code.) */
3663 if (NILP (overlay)
3664 && get_overlay_strings (it, start_charpos))
3665 {
3666 handled = HANDLED_RECOMPUTE_PROPS;
3667 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3668 }
3669 else if (display_ellipsis_p)
3670 {
3671 /* Make sure that the glyphs of the ellipsis will get
3672 correct `charpos' values. If we would not update
3673 it->position here, the glyphs would belong to the
3674 last visible character _before_ the invisible
3675 text, which confuses `set_cursor_from_row'.
3676
3677 We use the last invisible position instead of the
3678 first because this way the cursor is always drawn on
3679 the first "." of the ellipsis, whenever PT is inside
3680 the invisible text. Otherwise the cursor would be
3681 placed _after_ the ellipsis when the point is after the
3682 first invisible character. */
3683 if (!STRINGP (it->object))
3684 {
3685 it->position.charpos = IT_CHARPOS (*it) - 1;
3686 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3687 }
3688 setup_for_ellipsis (it, 0);
3689 }
3690 }
3691 }
3692
3693 return handled;
3694 }
3695
3696
3697 /* Make iterator IT return `...' next.
3698 Replaces LEN characters from buffer. */
3699
3700 static void
3701 setup_for_ellipsis (it, len)
3702 struct it *it;
3703 int len;
3704 {
3705 /* Use the display table definition for `...'. Invalid glyphs
3706 will be handled by the method returning elements from dpvec. */
3707 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3708 {
3709 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3710 it->dpvec = v->contents;
3711 it->dpend = v->contents + v->size;
3712 }
3713 else
3714 {
3715 /* Default `...'. */
3716 it->dpvec = default_invis_vector;
3717 it->dpend = default_invis_vector + 3;
3718 }
3719
3720 it->dpvec_char_len = len;
3721 it->current.dpvec_index = 0;
3722 it->dpvec_face_id = -1;
3723
3724 /* Remember the current face id in case glyphs specify faces.
3725 IT's face is restored in set_iterator_to_next.
3726 saved_face_id was set to preceding char's face in handle_stop. */
3727 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3728 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3729
3730 it->method = GET_FROM_DISPLAY_VECTOR;
3731 it->ellipsis_p = 1;
3732 }
3733
3734
3735 \f
3736 /***********************************************************************
3737 'display' property
3738 ***********************************************************************/
3739
3740 /* Set up iterator IT from `display' property at its current position.
3741 Called from handle_stop.
3742 We return HANDLED_RETURN if some part of the display property
3743 overrides the display of the buffer text itself.
3744 Otherwise we return HANDLED_NORMALLY. */
3745
3746 static enum prop_handled
3747 handle_display_prop (it)
3748 struct it *it;
3749 {
3750 Lisp_Object prop, object;
3751 struct text_pos *position;
3752 /* Nonzero if some property replaces the display of the text itself. */
3753 int display_replaced_p = 0;
3754
3755 if (STRINGP (it->string))
3756 {
3757 object = it->string;
3758 position = &it->current.string_pos;
3759 }
3760 else
3761 {
3762 XSETWINDOW (object, it->w);
3763 position = &it->current.pos;
3764 }
3765
3766 /* Reset those iterator values set from display property values. */
3767 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3768 it->space_width = Qnil;
3769 it->font_height = Qnil;
3770 it->voffset = 0;
3771
3772 /* We don't support recursive `display' properties, i.e. string
3773 values that have a string `display' property, that have a string
3774 `display' property etc. */
3775 if (!it->string_from_display_prop_p)
3776 it->area = TEXT_AREA;
3777
3778 prop = Fget_char_property (make_number (position->charpos),
3779 Qdisplay, object);
3780 if (NILP (prop))
3781 return HANDLED_NORMALLY;
3782
3783 if (!STRINGP (it->string))
3784 object = it->w->buffer;
3785
3786 if (CONSP (prop)
3787 /* Simple properties. */
3788 && !EQ (XCAR (prop), Qimage)
3789 && !EQ (XCAR (prop), Qspace)
3790 && !EQ (XCAR (prop), Qwhen)
3791 && !EQ (XCAR (prop), Qslice)
3792 && !EQ (XCAR (prop), Qspace_width)
3793 && !EQ (XCAR (prop), Qheight)
3794 && !EQ (XCAR (prop), Qraise)
3795 /* Marginal area specifications. */
3796 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3797 && !EQ (XCAR (prop), Qleft_fringe)
3798 && !EQ (XCAR (prop), Qright_fringe)
3799 && !NILP (XCAR (prop)))
3800 {
3801 for (; CONSP (prop); prop = XCDR (prop))
3802 {
3803 if (handle_single_display_spec (it, XCAR (prop), object,
3804 position, display_replaced_p))
3805 display_replaced_p = 1;
3806 }
3807 }
3808 else if (VECTORP (prop))
3809 {
3810 int i;
3811 for (i = 0; i < ASIZE (prop); ++i)
3812 if (handle_single_display_spec (it, AREF (prop, i), object,
3813 position, display_replaced_p))
3814 display_replaced_p = 1;
3815 }
3816 else
3817 {
3818 int ret = handle_single_display_spec (it, prop, object, position, 0);
3819 if (ret < 0) /* Replaced by "", i.e. nothing. */
3820 return HANDLED_RECOMPUTE_PROPS;
3821 if (ret)
3822 display_replaced_p = 1;
3823 }
3824
3825 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3826 }
3827
3828
3829 /* Value is the position of the end of the `display' property starting
3830 at START_POS in OBJECT. */
3831
3832 static struct text_pos
3833 display_prop_end (it, object, start_pos)
3834 struct it *it;
3835 Lisp_Object object;
3836 struct text_pos start_pos;
3837 {
3838 Lisp_Object end;
3839 struct text_pos end_pos;
3840
3841 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3842 Qdisplay, object, Qnil);
3843 CHARPOS (end_pos) = XFASTINT (end);
3844 if (STRINGP (object))
3845 compute_string_pos (&end_pos, start_pos, it->string);
3846 else
3847 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3848
3849 return end_pos;
3850 }
3851
3852
3853 /* Set up IT from a single `display' specification PROP. OBJECT
3854 is the object in which the `display' property was found. *POSITION
3855 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3856 means that we previously saw a display specification which already
3857 replaced text display with something else, for example an image;
3858 we ignore such properties after the first one has been processed.
3859
3860 If PROP is a `space' or `image' specification, and in some other
3861 cases too, set *POSITION to the position where the `display'
3862 property ends.
3863
3864 Value is non-zero if something was found which replaces the display
3865 of buffer or string text. Specifically, the value is -1 if that
3866 "something" is "nothing". */
3867
3868 static int
3869 handle_single_display_spec (it, spec, object, position,
3870 display_replaced_before_p)
3871 struct it *it;
3872 Lisp_Object spec;
3873 Lisp_Object object;
3874 struct text_pos *position;
3875 int display_replaced_before_p;
3876 {
3877 Lisp_Object form;
3878 Lisp_Object location, value;
3879 struct text_pos start_pos;
3880 int valid_p;
3881
3882 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3883 If the result is non-nil, use VALUE instead of SPEC. */
3884 form = Qt;
3885 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3886 {
3887 spec = XCDR (spec);
3888 if (!CONSP (spec))
3889 return 0;
3890 form = XCAR (spec);
3891 spec = XCDR (spec);
3892 }
3893
3894 if (!NILP (form) && !EQ (form, Qt))
3895 {
3896 int count = SPECPDL_INDEX ();
3897 struct gcpro gcpro1;
3898
3899 /* Bind `object' to the object having the `display' property, a
3900 buffer or string. Bind `position' to the position in the
3901 object where the property was found, and `buffer-position'
3902 to the current position in the buffer. */
3903 specbind (Qobject, object);
3904 specbind (Qposition, make_number (CHARPOS (*position)));
3905 specbind (Qbuffer_position,
3906 make_number (STRINGP (object)
3907 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3908 GCPRO1 (form);
3909 form = safe_eval (form);
3910 UNGCPRO;
3911 unbind_to (count, Qnil);
3912 }
3913
3914 if (NILP (form))
3915 return 0;
3916
3917 /* Handle `(height HEIGHT)' specifications. */
3918 if (CONSP (spec)
3919 && EQ (XCAR (spec), Qheight)
3920 && CONSP (XCDR (spec)))
3921 {
3922 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3923 return 0;
3924
3925 it->font_height = XCAR (XCDR (spec));
3926 if (!NILP (it->font_height))
3927 {
3928 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3929 int new_height = -1;
3930
3931 if (CONSP (it->font_height)
3932 && (EQ (XCAR (it->font_height), Qplus)
3933 || EQ (XCAR (it->font_height), Qminus))
3934 && CONSP (XCDR (it->font_height))
3935 && INTEGERP (XCAR (XCDR (it->font_height))))
3936 {
3937 /* `(+ N)' or `(- N)' where N is an integer. */
3938 int steps = XINT (XCAR (XCDR (it->font_height)));
3939 if (EQ (XCAR (it->font_height), Qplus))
3940 steps = - steps;
3941 it->face_id = smaller_face (it->f, it->face_id, steps);
3942 }
3943 else if (FUNCTIONP (it->font_height))
3944 {
3945 /* Call function with current height as argument.
3946 Value is the new height. */
3947 Lisp_Object height;
3948 height = safe_call1 (it->font_height,
3949 face->lface[LFACE_HEIGHT_INDEX]);
3950 if (NUMBERP (height))
3951 new_height = XFLOATINT (height);
3952 }
3953 else if (NUMBERP (it->font_height))
3954 {
3955 /* Value is a multiple of the canonical char height. */
3956 struct face *face;
3957
3958 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3959 new_height = (XFLOATINT (it->font_height)
3960 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3961 }
3962 else
3963 {
3964 /* Evaluate IT->font_height with `height' bound to the
3965 current specified height to get the new height. */
3966 int count = SPECPDL_INDEX ();
3967
3968 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3969 value = safe_eval (it->font_height);
3970 unbind_to (count, Qnil);
3971
3972 if (NUMBERP (value))
3973 new_height = XFLOATINT (value);
3974 }
3975
3976 if (new_height > 0)
3977 it->face_id = face_with_height (it->f, it->face_id, new_height);
3978 }
3979
3980 return 0;
3981 }
3982
3983 /* Handle `(space_width WIDTH)'. */
3984 if (CONSP (spec)
3985 && EQ (XCAR (spec), Qspace_width)
3986 && CONSP (XCDR (spec)))
3987 {
3988 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3989 return 0;
3990
3991 value = XCAR (XCDR (spec));
3992 if (NUMBERP (value) && XFLOATINT (value) > 0)
3993 it->space_width = value;
3994
3995 return 0;
3996 }
3997
3998 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3999 if (CONSP (spec)
4000 && EQ (XCAR (spec), Qslice))
4001 {
4002 Lisp_Object tem;
4003
4004 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4005 return 0;
4006
4007 if (tem = XCDR (spec), CONSP (tem))
4008 {
4009 it->slice.x = XCAR (tem);
4010 if (tem = XCDR (tem), CONSP (tem))
4011 {
4012 it->slice.y = XCAR (tem);
4013 if (tem = XCDR (tem), CONSP (tem))
4014 {
4015 it->slice.width = XCAR (tem);
4016 if (tem = XCDR (tem), CONSP (tem))
4017 it->slice.height = XCAR (tem);
4018 }
4019 }
4020 }
4021
4022 return 0;
4023 }
4024
4025 /* Handle `(raise FACTOR)'. */
4026 if (CONSP (spec)
4027 && EQ (XCAR (spec), Qraise)
4028 && CONSP (XCDR (spec)))
4029 {
4030 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4031 return 0;
4032
4033 #ifdef HAVE_WINDOW_SYSTEM
4034 value = XCAR (XCDR (spec));
4035 if (NUMBERP (value))
4036 {
4037 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4038 it->voffset = - (XFLOATINT (value)
4039 * (FONT_HEIGHT (face->font)));
4040 }
4041 #endif /* HAVE_WINDOW_SYSTEM */
4042
4043 return 0;
4044 }
4045
4046 /* Don't handle the other kinds of display specifications
4047 inside a string that we got from a `display' property. */
4048 if (it->string_from_display_prop_p)
4049 return 0;
4050
4051 /* Characters having this form of property are not displayed, so
4052 we have to find the end of the property. */
4053 start_pos = *position;
4054 *position = display_prop_end (it, object, start_pos);
4055 value = Qnil;
4056
4057 /* Stop the scan at that end position--we assume that all
4058 text properties change there. */
4059 it->stop_charpos = position->charpos;
4060
4061 /* Handle `(left-fringe BITMAP [FACE])'
4062 and `(right-fringe BITMAP [FACE])'. */
4063 if (CONSP (spec)
4064 && (EQ (XCAR (spec), Qleft_fringe)
4065 || EQ (XCAR (spec), Qright_fringe))
4066 && CONSP (XCDR (spec)))
4067 {
4068 int face_id = DEFAULT_FACE_ID;
4069 int fringe_bitmap;
4070
4071 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4072 /* If we return here, POSITION has been advanced
4073 across the text with this property. */
4074 return 0;
4075
4076 #ifdef HAVE_WINDOW_SYSTEM
4077 value = XCAR (XCDR (spec));
4078 if (!SYMBOLP (value)
4079 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4080 /* If we return here, POSITION has been advanced
4081 across the text with this property. */
4082 return 0;
4083
4084 if (CONSP (XCDR (XCDR (spec))))
4085 {
4086 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4087 int face_id2 = lookup_derived_face (it->f, face_name,
4088 FRINGE_FACE_ID, 0);
4089 if (face_id2 >= 0)
4090 face_id = face_id2;
4091 }
4092
4093 /* Save current settings of IT so that we can restore them
4094 when we are finished with the glyph property value. */
4095
4096 push_it (it);
4097
4098 it->area = TEXT_AREA;
4099 it->what = IT_IMAGE;
4100 it->image_id = -1; /* no image */
4101 it->position = start_pos;
4102 it->object = NILP (object) ? it->w->buffer : object;
4103 it->method = GET_FROM_IMAGE;
4104 it->face_id = face_id;
4105
4106 /* Say that we haven't consumed the characters with
4107 `display' property yet. The call to pop_it in
4108 set_iterator_to_next will clean this up. */
4109 *position = start_pos;
4110
4111 if (EQ (XCAR (spec), Qleft_fringe))
4112 {
4113 it->left_user_fringe_bitmap = fringe_bitmap;
4114 it->left_user_fringe_face_id = face_id;
4115 }
4116 else
4117 {
4118 it->right_user_fringe_bitmap = fringe_bitmap;
4119 it->right_user_fringe_face_id = face_id;
4120 }
4121 #endif /* HAVE_WINDOW_SYSTEM */
4122 return 1;
4123 }
4124
4125 /* Prepare to handle `((margin left-margin) ...)',
4126 `((margin right-margin) ...)' and `((margin nil) ...)'
4127 prefixes for display specifications. */
4128 location = Qunbound;
4129 if (CONSP (spec) && CONSP (XCAR (spec)))
4130 {
4131 Lisp_Object tem;
4132
4133 value = XCDR (spec);
4134 if (CONSP (value))
4135 value = XCAR (value);
4136
4137 tem = XCAR (spec);
4138 if (EQ (XCAR (tem), Qmargin)
4139 && (tem = XCDR (tem),
4140 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4141 (NILP (tem)
4142 || EQ (tem, Qleft_margin)
4143 || EQ (tem, Qright_margin))))
4144 location = tem;
4145 }
4146
4147 if (EQ (location, Qunbound))
4148 {
4149 location = Qnil;
4150 value = spec;
4151 }
4152
4153 /* After this point, VALUE is the property after any
4154 margin prefix has been stripped. It must be a string,
4155 an image specification, or `(space ...)'.
4156
4157 LOCATION specifies where to display: `left-margin',
4158 `right-margin' or nil. */
4159
4160 valid_p = (STRINGP (value)
4161 #ifdef HAVE_WINDOW_SYSTEM
4162 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4163 #endif /* not HAVE_WINDOW_SYSTEM */
4164 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4165
4166 if (valid_p && !display_replaced_before_p)
4167 {
4168 /* Save current settings of IT so that we can restore them
4169 when we are finished with the glyph property value. */
4170 push_it (it);
4171
4172 if (NILP (location))
4173 it->area = TEXT_AREA;
4174 else if (EQ (location, Qleft_margin))
4175 it->area = LEFT_MARGIN_AREA;
4176 else
4177 it->area = RIGHT_MARGIN_AREA;
4178
4179 if (STRINGP (value))
4180 {
4181 if (SCHARS (value) == 0)
4182 {
4183 pop_it (it);
4184 return -1; /* Replaced by "", i.e. nothing. */
4185 }
4186 it->string = value;
4187 it->multibyte_p = STRING_MULTIBYTE (it->string);
4188 it->current.overlay_string_index = -1;
4189 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4190 it->end_charpos = it->string_nchars = SCHARS (it->string);
4191 it->method = GET_FROM_STRING;
4192 it->stop_charpos = 0;
4193 it->string_from_display_prop_p = 1;
4194 /* Say that we haven't consumed the characters with
4195 `display' property yet. The call to pop_it in
4196 set_iterator_to_next will clean this up. */
4197 *position = start_pos;
4198 }
4199 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4200 {
4201 it->method = GET_FROM_STRETCH;
4202 it->object = value;
4203 *position = it->position = start_pos;
4204 }
4205 #ifdef HAVE_WINDOW_SYSTEM
4206 else
4207 {
4208 it->what = IT_IMAGE;
4209 it->image_id = lookup_image (it->f, value);
4210 it->position = start_pos;
4211 it->object = NILP (object) ? it->w->buffer : object;
4212 it->method = GET_FROM_IMAGE;
4213
4214 /* Say that we haven't consumed the characters with
4215 `display' property yet. The call to pop_it in
4216 set_iterator_to_next will clean this up. */
4217 *position = start_pos;
4218 }
4219 #endif /* HAVE_WINDOW_SYSTEM */
4220
4221 return 1;
4222 }
4223
4224 /* Invalid property or property not supported. Restore
4225 POSITION to what it was before. */
4226 *position = start_pos;
4227 return 0;
4228 }
4229
4230
4231 /* Check if SPEC is a display specification value whose text should be
4232 treated as intangible. */
4233
4234 static int
4235 single_display_spec_intangible_p (prop)
4236 Lisp_Object prop;
4237 {
4238 /* Skip over `when FORM'. */
4239 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4240 {
4241 prop = XCDR (prop);
4242 if (!CONSP (prop))
4243 return 0;
4244 prop = XCDR (prop);
4245 }
4246
4247 if (STRINGP (prop))
4248 return 1;
4249
4250 if (!CONSP (prop))
4251 return 0;
4252
4253 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4254 we don't need to treat text as intangible. */
4255 if (EQ (XCAR (prop), Qmargin))
4256 {
4257 prop = XCDR (prop);
4258 if (!CONSP (prop))
4259 return 0;
4260
4261 prop = XCDR (prop);
4262 if (!CONSP (prop)
4263 || EQ (XCAR (prop), Qleft_margin)
4264 || EQ (XCAR (prop), Qright_margin))
4265 return 0;
4266 }
4267
4268 return (CONSP (prop)
4269 && (EQ (XCAR (prop), Qimage)
4270 || EQ (XCAR (prop), Qspace)));
4271 }
4272
4273
4274 /* Check if PROP is a display property value whose text should be
4275 treated as intangible. */
4276
4277 int
4278 display_prop_intangible_p (prop)
4279 Lisp_Object prop;
4280 {
4281 if (CONSP (prop)
4282 && CONSP (XCAR (prop))
4283 && !EQ (Qmargin, XCAR (XCAR (prop))))
4284 {
4285 /* A list of sub-properties. */
4286 while (CONSP (prop))
4287 {
4288 if (single_display_spec_intangible_p (XCAR (prop)))
4289 return 1;
4290 prop = XCDR (prop);
4291 }
4292 }
4293 else if (VECTORP (prop))
4294 {
4295 /* A vector of sub-properties. */
4296 int i;
4297 for (i = 0; i < ASIZE (prop); ++i)
4298 if (single_display_spec_intangible_p (AREF (prop, i)))
4299 return 1;
4300 }
4301 else
4302 return single_display_spec_intangible_p (prop);
4303
4304 return 0;
4305 }
4306
4307
4308 /* Return 1 if PROP is a display sub-property value containing STRING. */
4309
4310 static int
4311 single_display_spec_string_p (prop, string)
4312 Lisp_Object prop, string;
4313 {
4314 if (EQ (string, prop))
4315 return 1;
4316
4317 /* Skip over `when FORM'. */
4318 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4319 {
4320 prop = XCDR (prop);
4321 if (!CONSP (prop))
4322 return 0;
4323 prop = XCDR (prop);
4324 }
4325
4326 if (CONSP (prop))
4327 /* Skip over `margin LOCATION'. */
4328 if (EQ (XCAR (prop), Qmargin))
4329 {
4330 prop = XCDR (prop);
4331 if (!CONSP (prop))
4332 return 0;
4333
4334 prop = XCDR (prop);
4335 if (!CONSP (prop))
4336 return 0;
4337 }
4338
4339 return CONSP (prop) && EQ (XCAR (prop), string);
4340 }
4341
4342
4343 /* Return 1 if STRING appears in the `display' property PROP. */
4344
4345 static int
4346 display_prop_string_p (prop, string)
4347 Lisp_Object prop, string;
4348 {
4349 if (CONSP (prop)
4350 && CONSP (XCAR (prop))
4351 && !EQ (Qmargin, XCAR (XCAR (prop))))
4352 {
4353 /* A list of sub-properties. */
4354 while (CONSP (prop))
4355 {
4356 if (single_display_spec_string_p (XCAR (prop), string))
4357 return 1;
4358 prop = XCDR (prop);
4359 }
4360 }
4361 else if (VECTORP (prop))
4362 {
4363 /* A vector of sub-properties. */
4364 int i;
4365 for (i = 0; i < ASIZE (prop); ++i)
4366 if (single_display_spec_string_p (AREF (prop, i), string))
4367 return 1;
4368 }
4369 else
4370 return single_display_spec_string_p (prop, string);
4371
4372 return 0;
4373 }
4374
4375
4376 /* Determine from which buffer position in W's buffer STRING comes
4377 from. AROUND_CHARPOS is an approximate position where it could
4378 be from. Value is the buffer position or 0 if it couldn't be
4379 determined.
4380
4381 W's buffer must be current.
4382
4383 This function is necessary because we don't record buffer positions
4384 in glyphs generated from strings (to keep struct glyph small).
4385 This function may only use code that doesn't eval because it is
4386 called asynchronously from note_mouse_highlight. */
4387
4388 int
4389 string_buffer_position (w, string, around_charpos)
4390 struct window *w;
4391 Lisp_Object string;
4392 int around_charpos;
4393 {
4394 Lisp_Object limit, prop, pos;
4395 const int MAX_DISTANCE = 1000;
4396 int found = 0;
4397
4398 pos = make_number (around_charpos);
4399 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4400 while (!found && !EQ (pos, limit))
4401 {
4402 prop = Fget_char_property (pos, Qdisplay, Qnil);
4403 if (!NILP (prop) && display_prop_string_p (prop, string))
4404 found = 1;
4405 else
4406 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4407 }
4408
4409 if (!found)
4410 {
4411 pos = make_number (around_charpos);
4412 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4413 while (!found && !EQ (pos, limit))
4414 {
4415 prop = Fget_char_property (pos, Qdisplay, Qnil);
4416 if (!NILP (prop) && display_prop_string_p (prop, string))
4417 found = 1;
4418 else
4419 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4420 limit);
4421 }
4422 }
4423
4424 return found ? XINT (pos) : 0;
4425 }
4426
4427
4428 \f
4429 /***********************************************************************
4430 `composition' property
4431 ***********************************************************************/
4432
4433 static enum prop_handled
4434 handle_auto_composed_prop (it)
4435 struct it *it;
4436 {
4437 enum prop_handled handled = HANDLED_NORMALLY;
4438
4439 if (FUNCTIONP (Vauto_composition_function))
4440 {
4441 Lisp_Object val;
4442 EMACS_INT pos, this_pos;
4443
4444 if (STRINGP (it->string))
4445 pos = IT_STRING_CHARPOS (*it);
4446 else
4447 pos = IT_CHARPOS (*it);
4448 this_pos = pos;
4449
4450 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
4451 if (! NILP (val))
4452 {
4453 Lisp_Object limit = Qnil, next;
4454
4455 /* As Fnext_single_char_property_change is very slow, we
4456 limit the search to the current line. */
4457 if (STRINGP (it->string))
4458 limit = make_number (SCHARS (it->string));
4459 else
4460 limit = make_number (find_next_newline_no_quit (pos, 1));
4461
4462 next = (Fnext_single_property_change
4463 (make_number (pos), Qauto_composed, it->string, limit));
4464 if (XINT (next) < XINT (limit))
4465 {
4466 /* The current point is auto-composed, but there exist
4467 characters not yet composed beyond the auto-composed
4468 region. There's a possiblity that the last
4469 characters in the region may be newly composed. */
4470 int charpos = XINT (next) - 1, bytepos, c;
4471
4472 if (STRINGP (it->string))
4473 {
4474 bytepos = string_char_to_byte (it->string, charpos);
4475 c = SDATA (it->string)[bytepos];
4476 }
4477 else
4478 {
4479 bytepos = CHAR_TO_BYTE (charpos);
4480 c = FETCH_BYTE (bytepos);
4481 }
4482 if (c != '\n')
4483 /* If the last character is not newline, it may be
4484 composed with the following characters. */
4485 val = Qnil, pos = charpos + 1;
4486 }
4487 }
4488 if (NILP (val))
4489 {
4490 int count = SPECPDL_INDEX ();
4491 Lisp_Object args[3];
4492
4493 args[0] = Vauto_composition_function;
4494 specbind (Qauto_composition_function, Qnil);
4495 args[1] = make_number (pos);
4496 args[2] = it->string;
4497 safe_call (3, args);
4498 unbind_to (count, Qnil);
4499
4500 if (this_pos == pos)
4501 {
4502 val = Fget_char_property (args[1], Qauto_composed, it->string);
4503 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
4504 something. This avoids an endless loop if they failed to
4505 fontify the text for which reason ever. */
4506 if (! NILP (val))
4507 handled = HANDLED_RECOMPUTE_PROPS;
4508 }
4509 else
4510 handled = HANDLED_RECOMPUTE_PROPS;
4511 }
4512 }
4513
4514 return handled;
4515 }
4516
4517 /* Set up iterator IT from `composition' property at its current
4518 position. Called from handle_stop. */
4519
4520 static enum prop_handled
4521 handle_composition_prop (it)
4522 struct it *it;
4523 {
4524 Lisp_Object prop, string;
4525 EMACS_INT pos, pos_byte, start, end;
4526 enum prop_handled handled = HANDLED_NORMALLY;
4527
4528 if (STRINGP (it->string))
4529 {
4530 pos = IT_STRING_CHARPOS (*it);
4531 pos_byte = IT_STRING_BYTEPOS (*it);
4532 string = it->string;
4533 }
4534 else
4535 {
4536 pos = IT_CHARPOS (*it);
4537 pos_byte = IT_BYTEPOS (*it);
4538 string = Qnil;
4539 }
4540
4541 /* If there's a valid composition and point is not inside of the
4542 composition (in the case that the composition is from the current
4543 buffer), draw a glyph composed from the composition components. */
4544 if (find_composition (pos, -1, &start, &end, &prop, string)
4545 && COMPOSITION_VALID_P (start, end, prop)
4546 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4547 {
4548 int id;
4549
4550 if (start != pos)
4551 {
4552 if (STRINGP (it->string))
4553 pos_byte = string_char_to_byte (it->string, start);
4554 else
4555 pos_byte = CHAR_TO_BYTE (start);
4556 }
4557 id = get_composition_id (start, pos_byte, end - start, prop, string);
4558
4559 if (id >= 0)
4560 {
4561 struct composition *cmp = composition_table[id];
4562
4563 if (cmp->glyph_len == 0)
4564 {
4565 /* No glyph. */
4566 if (STRINGP (it->string))
4567 {
4568 IT_STRING_CHARPOS (*it) = end;
4569 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4570 end);
4571 }
4572 else
4573 {
4574 IT_CHARPOS (*it) = end;
4575 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4576 }
4577 return HANDLED_RECOMPUTE_PROPS;
4578 }
4579 it->method = GET_FROM_COMPOSITION;
4580 it->cmp_id = id;
4581 it->cmp_len = COMPOSITION_LENGTH (prop);
4582 #ifdef USE_FONT_BACKEND
4583 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4584 it->c = ' ';
4585 else
4586 #endif /* USE_FONT_BACKEND */
4587 /* For a terminal, draw only the first character of the
4588 components. */
4589 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4590 it->len = (STRINGP (it->string)
4591 ? string_char_to_byte (it->string, end)
4592 : CHAR_TO_BYTE (end)) - pos_byte;
4593 it->stop_charpos = end;
4594 handled = HANDLED_RETURN;
4595 }
4596 }
4597
4598 return handled;
4599 }
4600
4601
4602 \f
4603 /***********************************************************************
4604 Overlay strings
4605 ***********************************************************************/
4606
4607 /* The following structure is used to record overlay strings for
4608 later sorting in load_overlay_strings. */
4609
4610 struct overlay_entry
4611 {
4612 Lisp_Object overlay;
4613 Lisp_Object string;
4614 int priority;
4615 int after_string_p;
4616 };
4617
4618
4619 /* Set up iterator IT from overlay strings at its current position.
4620 Called from handle_stop. */
4621
4622 static enum prop_handled
4623 handle_overlay_change (it)
4624 struct it *it;
4625 {
4626 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4627 return HANDLED_RECOMPUTE_PROPS;
4628 else
4629 return HANDLED_NORMALLY;
4630 }
4631
4632
4633 /* Set up the next overlay string for delivery by IT, if there is an
4634 overlay string to deliver. Called by set_iterator_to_next when the
4635 end of the current overlay string is reached. If there are more
4636 overlay strings to display, IT->string and
4637 IT->current.overlay_string_index are set appropriately here.
4638 Otherwise IT->string is set to nil. */
4639
4640 static void
4641 next_overlay_string (it)
4642 struct it *it;
4643 {
4644 ++it->current.overlay_string_index;
4645 if (it->current.overlay_string_index == it->n_overlay_strings)
4646 {
4647 /* No more overlay strings. Restore IT's settings to what
4648 they were before overlay strings were processed, and
4649 continue to deliver from current_buffer. */
4650 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4651
4652 pop_it (it);
4653 xassert (it->stop_charpos >= BEGV
4654 && it->stop_charpos <= it->end_charpos);
4655 it->string = Qnil;
4656 it->current.overlay_string_index = -1;
4657 SET_TEXT_POS (it->current.string_pos, -1, -1);
4658 it->n_overlay_strings = 0;
4659 it->method = GET_FROM_BUFFER;
4660
4661 /* If we're at the end of the buffer, record that we have
4662 processed the overlay strings there already, so that
4663 next_element_from_buffer doesn't try it again. */
4664 if (IT_CHARPOS (*it) >= it->end_charpos)
4665 it->overlay_strings_at_end_processed_p = 1;
4666
4667 /* If we have to display `...' for invisible text, set
4668 the iterator up for that. */
4669 if (display_ellipsis_p)
4670 setup_for_ellipsis (it, 0);
4671 }
4672 else
4673 {
4674 /* There are more overlay strings to process. If
4675 IT->current.overlay_string_index has advanced to a position
4676 where we must load IT->overlay_strings with more strings, do
4677 it. */
4678 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4679
4680 if (it->current.overlay_string_index && i == 0)
4681 load_overlay_strings (it, 0);
4682
4683 /* Initialize IT to deliver display elements from the overlay
4684 string. */
4685 it->string = it->overlay_strings[i];
4686 it->multibyte_p = STRING_MULTIBYTE (it->string);
4687 SET_TEXT_POS (it->current.string_pos, 0, 0);
4688 it->method = GET_FROM_STRING;
4689 it->stop_charpos = 0;
4690 }
4691
4692 CHECK_IT (it);
4693 }
4694
4695
4696 /* Compare two overlay_entry structures E1 and E2. Used as a
4697 comparison function for qsort in load_overlay_strings. Overlay
4698 strings for the same position are sorted so that
4699
4700 1. All after-strings come in front of before-strings, except
4701 when they come from the same overlay.
4702
4703 2. Within after-strings, strings are sorted so that overlay strings
4704 from overlays with higher priorities come first.
4705
4706 2. Within before-strings, strings are sorted so that overlay
4707 strings from overlays with higher priorities come last.
4708
4709 Value is analogous to strcmp. */
4710
4711
4712 static int
4713 compare_overlay_entries (e1, e2)
4714 void *e1, *e2;
4715 {
4716 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4717 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4718 int result;
4719
4720 if (entry1->after_string_p != entry2->after_string_p)
4721 {
4722 /* Let after-strings appear in front of before-strings if
4723 they come from different overlays. */
4724 if (EQ (entry1->overlay, entry2->overlay))
4725 result = entry1->after_string_p ? 1 : -1;
4726 else
4727 result = entry1->after_string_p ? -1 : 1;
4728 }
4729 else if (entry1->after_string_p)
4730 /* After-strings sorted in order of decreasing priority. */
4731 result = entry2->priority - entry1->priority;
4732 else
4733 /* Before-strings sorted in order of increasing priority. */
4734 result = entry1->priority - entry2->priority;
4735
4736 return result;
4737 }
4738
4739
4740 /* Load the vector IT->overlay_strings with overlay strings from IT's
4741 current buffer position, or from CHARPOS if that is > 0. Set
4742 IT->n_overlays to the total number of overlay strings found.
4743
4744 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4745 a time. On entry into load_overlay_strings,
4746 IT->current.overlay_string_index gives the number of overlay
4747 strings that have already been loaded by previous calls to this
4748 function.
4749
4750 IT->add_overlay_start contains an additional overlay start
4751 position to consider for taking overlay strings from, if non-zero.
4752 This position comes into play when the overlay has an `invisible'
4753 property, and both before and after-strings. When we've skipped to
4754 the end of the overlay, because of its `invisible' property, we
4755 nevertheless want its before-string to appear.
4756 IT->add_overlay_start will contain the overlay start position
4757 in this case.
4758
4759 Overlay strings are sorted so that after-string strings come in
4760 front of before-string strings. Within before and after-strings,
4761 strings are sorted by overlay priority. See also function
4762 compare_overlay_entries. */
4763
4764 static void
4765 load_overlay_strings (it, charpos)
4766 struct it *it;
4767 int charpos;
4768 {
4769 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4770 Lisp_Object overlay, window, str, invisible;
4771 struct Lisp_Overlay *ov;
4772 int start, end;
4773 int size = 20;
4774 int n = 0, i, j, invis_p;
4775 struct overlay_entry *entries
4776 = (struct overlay_entry *) alloca (size * sizeof *entries);
4777
4778 if (charpos <= 0)
4779 charpos = IT_CHARPOS (*it);
4780
4781 /* Append the overlay string STRING of overlay OVERLAY to vector
4782 `entries' which has size `size' and currently contains `n'
4783 elements. AFTER_P non-zero means STRING is an after-string of
4784 OVERLAY. */
4785 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4786 do \
4787 { \
4788 Lisp_Object priority; \
4789 \
4790 if (n == size) \
4791 { \
4792 int new_size = 2 * size; \
4793 struct overlay_entry *old = entries; \
4794 entries = \
4795 (struct overlay_entry *) alloca (new_size \
4796 * sizeof *entries); \
4797 bcopy (old, entries, size * sizeof *entries); \
4798 size = new_size; \
4799 } \
4800 \
4801 entries[n].string = (STRING); \
4802 entries[n].overlay = (OVERLAY); \
4803 priority = Foverlay_get ((OVERLAY), Qpriority); \
4804 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4805 entries[n].after_string_p = (AFTER_P); \
4806 ++n; \
4807 } \
4808 while (0)
4809
4810 /* Process overlay before the overlay center. */
4811 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4812 {
4813 XSETMISC (overlay, ov);
4814 xassert (OVERLAYP (overlay));
4815 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4816 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4817
4818 if (end < charpos)
4819 break;
4820
4821 /* Skip this overlay if it doesn't start or end at IT's current
4822 position. */
4823 if (end != charpos && start != charpos)
4824 continue;
4825
4826 /* Skip this overlay if it doesn't apply to IT->w. */
4827 window = Foverlay_get (overlay, Qwindow);
4828 if (WINDOWP (window) && XWINDOW (window) != it->w)
4829 continue;
4830
4831 /* If the text ``under'' the overlay is invisible, both before-
4832 and after-strings from this overlay are visible; start and
4833 end position are indistinguishable. */
4834 invisible = Foverlay_get (overlay, Qinvisible);
4835 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4836
4837 /* If overlay has a non-empty before-string, record it. */
4838 if ((start == charpos || (end == charpos && invis_p))
4839 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4840 && SCHARS (str))
4841 RECORD_OVERLAY_STRING (overlay, str, 0);
4842
4843 /* If overlay has a non-empty after-string, record it. */
4844 if ((end == charpos || (start == charpos && invis_p))
4845 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4846 && SCHARS (str))
4847 RECORD_OVERLAY_STRING (overlay, str, 1);
4848 }
4849
4850 /* Process overlays after the overlay center. */
4851 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4852 {
4853 XSETMISC (overlay, ov);
4854 xassert (OVERLAYP (overlay));
4855 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4856 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4857
4858 if (start > charpos)
4859 break;
4860
4861 /* Skip this overlay if it doesn't start or end at IT's current
4862 position. */
4863 if (end != charpos && start != charpos)
4864 continue;
4865
4866 /* Skip this overlay if it doesn't apply to IT->w. */
4867 window = Foverlay_get (overlay, Qwindow);
4868 if (WINDOWP (window) && XWINDOW (window) != it->w)
4869 continue;
4870
4871 /* If the text ``under'' the overlay is invisible, it has a zero
4872 dimension, and both before- and after-strings apply. */
4873 invisible = Foverlay_get (overlay, Qinvisible);
4874 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4875
4876 /* If overlay has a non-empty before-string, record it. */
4877 if ((start == charpos || (end == charpos && invis_p))
4878 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4879 && SCHARS (str))
4880 RECORD_OVERLAY_STRING (overlay, str, 0);
4881
4882 /* If overlay has a non-empty after-string, record it. */
4883 if ((end == charpos || (start == charpos && invis_p))
4884 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4885 && SCHARS (str))
4886 RECORD_OVERLAY_STRING (overlay, str, 1);
4887 }
4888
4889 #undef RECORD_OVERLAY_STRING
4890
4891 /* Sort entries. */
4892 if (n > 1)
4893 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4894
4895 /* Record the total number of strings to process. */
4896 it->n_overlay_strings = n;
4897
4898 /* IT->current.overlay_string_index is the number of overlay strings
4899 that have already been consumed by IT. Copy some of the
4900 remaining overlay strings to IT->overlay_strings. */
4901 i = 0;
4902 j = it->current.overlay_string_index;
4903 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4904 it->overlay_strings[i++] = entries[j++].string;
4905
4906 CHECK_IT (it);
4907 }
4908
4909
4910 /* Get the first chunk of overlay strings at IT's current buffer
4911 position, or at CHARPOS if that is > 0. Value is non-zero if at
4912 least one overlay string was found. */
4913
4914 static int
4915 get_overlay_strings (it, charpos)
4916 struct it *it;
4917 int charpos;
4918 {
4919 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4920 process. This fills IT->overlay_strings with strings, and sets
4921 IT->n_overlay_strings to the total number of strings to process.
4922 IT->pos.overlay_string_index has to be set temporarily to zero
4923 because load_overlay_strings needs this; it must be set to -1
4924 when no overlay strings are found because a zero value would
4925 indicate a position in the first overlay string. */
4926 it->current.overlay_string_index = 0;
4927 load_overlay_strings (it, charpos);
4928
4929 /* If we found overlay strings, set up IT to deliver display
4930 elements from the first one. Otherwise set up IT to deliver
4931 from current_buffer. */
4932 if (it->n_overlay_strings)
4933 {
4934 /* Make sure we know settings in current_buffer, so that we can
4935 restore meaningful values when we're done with the overlay
4936 strings. */
4937 compute_stop_pos (it);
4938 xassert (it->face_id >= 0);
4939
4940 /* Save IT's settings. They are restored after all overlay
4941 strings have been processed. */
4942 xassert (it->sp == 0);
4943 push_it (it);
4944
4945 /* Set up IT to deliver display elements from the first overlay
4946 string. */
4947 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4948 it->string = it->overlay_strings[0];
4949 it->stop_charpos = 0;
4950 xassert (STRINGP (it->string));
4951 it->end_charpos = SCHARS (it->string);
4952 it->multibyte_p = STRING_MULTIBYTE (it->string);
4953 it->method = GET_FROM_STRING;
4954 }
4955 else
4956 {
4957 it->string = Qnil;
4958 it->current.overlay_string_index = -1;
4959 it->method = GET_FROM_BUFFER;
4960 }
4961
4962 CHECK_IT (it);
4963
4964 /* Value is non-zero if we found at least one overlay string. */
4965 return STRINGP (it->string);
4966 }
4967
4968
4969 \f
4970 /***********************************************************************
4971 Saving and restoring state
4972 ***********************************************************************/
4973
4974 /* Save current settings of IT on IT->stack. Called, for example,
4975 before setting up IT for an overlay string, to be able to restore
4976 IT's settings to what they were after the overlay string has been
4977 processed. */
4978
4979 static void
4980 push_it (it)
4981 struct it *it;
4982 {
4983 struct iterator_stack_entry *p;
4984
4985 xassert (it->sp < 2);
4986 p = it->stack + it->sp;
4987
4988 p->stop_charpos = it->stop_charpos;
4989 xassert (it->face_id >= 0);
4990 p->face_id = it->face_id;
4991 p->string = it->string;
4992 p->pos = it->current;
4993 p->end_charpos = it->end_charpos;
4994 p->string_nchars = it->string_nchars;
4995 p->area = it->area;
4996 p->multibyte_p = it->multibyte_p;
4997 p->slice = it->slice;
4998 p->space_width = it->space_width;
4999 p->font_height = it->font_height;
5000 p->voffset = it->voffset;
5001 p->string_from_display_prop_p = it->string_from_display_prop_p;
5002 p->display_ellipsis_p = 0;
5003 ++it->sp;
5004 }
5005
5006
5007 /* Restore IT's settings from IT->stack. Called, for example, when no
5008 more overlay strings must be processed, and we return to delivering
5009 display elements from a buffer, or when the end of a string from a
5010 `display' property is reached and we return to delivering display
5011 elements from an overlay string, or from a buffer. */
5012
5013 static void
5014 pop_it (it)
5015 struct it *it;
5016 {
5017 struct iterator_stack_entry *p;
5018
5019 xassert (it->sp > 0);
5020 --it->sp;
5021 p = it->stack + it->sp;
5022 it->stop_charpos = p->stop_charpos;
5023 it->face_id = p->face_id;
5024 it->string = p->string;
5025 it->current = p->pos;
5026 it->end_charpos = p->end_charpos;
5027 it->string_nchars = p->string_nchars;
5028 it->area = p->area;
5029 it->multibyte_p = p->multibyte_p;
5030 it->slice = p->slice;
5031 it->space_width = p->space_width;
5032 it->font_height = p->font_height;
5033 it->voffset = p->voffset;
5034 it->string_from_display_prop_p = p->string_from_display_prop_p;
5035 }
5036
5037
5038 \f
5039 /***********************************************************************
5040 Moving over lines
5041 ***********************************************************************/
5042
5043 /* Set IT's current position to the previous line start. */
5044
5045 static void
5046 back_to_previous_line_start (it)
5047 struct it *it;
5048 {
5049 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5050 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5051 }
5052
5053
5054 /* Move IT to the next line start.
5055
5056 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5057 we skipped over part of the text (as opposed to moving the iterator
5058 continuously over the text). Otherwise, don't change the value
5059 of *SKIPPED_P.
5060
5061 Newlines may come from buffer text, overlay strings, or strings
5062 displayed via the `display' property. That's the reason we can't
5063 simply use find_next_newline_no_quit.
5064
5065 Note that this function may not skip over invisible text that is so
5066 because of text properties and immediately follows a newline. If
5067 it would, function reseat_at_next_visible_line_start, when called
5068 from set_iterator_to_next, would effectively make invisible
5069 characters following a newline part of the wrong glyph row, which
5070 leads to wrong cursor motion. */
5071
5072 static int
5073 forward_to_next_line_start (it, skipped_p)
5074 struct it *it;
5075 int *skipped_p;
5076 {
5077 int old_selective, newline_found_p, n;
5078 const int MAX_NEWLINE_DISTANCE = 500;
5079
5080 /* If already on a newline, just consume it to avoid unintended
5081 skipping over invisible text below. */
5082 if (it->what == IT_CHARACTER
5083 && it->c == '\n'
5084 && CHARPOS (it->position) == IT_CHARPOS (*it))
5085 {
5086 set_iterator_to_next (it, 0);
5087 it->c = 0;
5088 return 1;
5089 }
5090
5091 /* Don't handle selective display in the following. It's (a)
5092 unnecessary because it's done by the caller, and (b) leads to an
5093 infinite recursion because next_element_from_ellipsis indirectly
5094 calls this function. */
5095 old_selective = it->selective;
5096 it->selective = 0;
5097
5098 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5099 from buffer text. */
5100 for (n = newline_found_p = 0;
5101 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5102 n += STRINGP (it->string) ? 0 : 1)
5103 {
5104 if (!get_next_display_element (it))
5105 return 0;
5106 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5107 set_iterator_to_next (it, 0);
5108 }
5109
5110 /* If we didn't find a newline near enough, see if we can use a
5111 short-cut. */
5112 if (!newline_found_p)
5113 {
5114 int start = IT_CHARPOS (*it);
5115 int limit = find_next_newline_no_quit (start, 1);
5116 Lisp_Object pos;
5117
5118 xassert (!STRINGP (it->string));
5119
5120 /* If there isn't any `display' property in sight, and no
5121 overlays, we can just use the position of the newline in
5122 buffer text. */
5123 if (it->stop_charpos >= limit
5124 || ((pos = Fnext_single_property_change (make_number (start),
5125 Qdisplay,
5126 Qnil, make_number (limit)),
5127 NILP (pos))
5128 && next_overlay_change (start) == ZV))
5129 {
5130 IT_CHARPOS (*it) = limit;
5131 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5132 *skipped_p = newline_found_p = 1;
5133 }
5134 else
5135 {
5136 while (get_next_display_element (it)
5137 && !newline_found_p)
5138 {
5139 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5140 set_iterator_to_next (it, 0);
5141 }
5142 }
5143 }
5144
5145 it->selective = old_selective;
5146 return newline_found_p;
5147 }
5148
5149
5150 /* Set IT's current position to the previous visible line start. Skip
5151 invisible text that is so either due to text properties or due to
5152 selective display. Caution: this does not change IT->current_x and
5153 IT->hpos. */
5154
5155 static void
5156 back_to_previous_visible_line_start (it)
5157 struct it *it;
5158 {
5159 while (IT_CHARPOS (*it) > BEGV)
5160 {
5161 back_to_previous_line_start (it);
5162 if (IT_CHARPOS (*it) <= BEGV)
5163 break;
5164
5165 /* If selective > 0, then lines indented more than that values
5166 are invisible. */
5167 if (it->selective > 0
5168 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5169 (double) it->selective)) /* iftc */
5170 continue;
5171
5172 /* Check the newline before point for invisibility. */
5173 {
5174 Lisp_Object prop;
5175 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5176 Qinvisible, it->window);
5177 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5178 continue;
5179 }
5180
5181 /* If newline has a display property that replaces the newline with something
5182 else (image or text), find start of overlay or interval and continue search
5183 from that point. */
5184 if (IT_CHARPOS (*it) > BEGV)
5185 {
5186 struct it it2 = *it;
5187 int pos;
5188 int beg, end;
5189 Lisp_Object val, overlay;
5190
5191 pos = --IT_CHARPOS (it2);
5192 --IT_BYTEPOS (it2);
5193 it2.sp = 0;
5194 if (handle_display_prop (&it2) == HANDLED_RETURN
5195 && !NILP (val = get_char_property_and_overlay
5196 (make_number (pos), Qdisplay, Qnil, &overlay))
5197 && (OVERLAYP (overlay)
5198 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5199 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5200 {
5201 if (beg < BEGV)
5202 beg = BEGV;
5203 IT_CHARPOS (*it) = beg;
5204 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5205 continue;
5206 }
5207 }
5208
5209 break;
5210 }
5211
5212 xassert (IT_CHARPOS (*it) >= BEGV);
5213 xassert (IT_CHARPOS (*it) == BEGV
5214 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5215 CHECK_IT (it);
5216 }
5217
5218
5219 /* Reseat iterator IT at the previous visible line start. Skip
5220 invisible text that is so either due to text properties or due to
5221 selective display. At the end, update IT's overlay information,
5222 face information etc. */
5223
5224 void
5225 reseat_at_previous_visible_line_start (it)
5226 struct it *it;
5227 {
5228 back_to_previous_visible_line_start (it);
5229 reseat (it, it->current.pos, 1);
5230 CHECK_IT (it);
5231 }
5232
5233
5234 /* Reseat iterator IT on the next visible line start in the current
5235 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5236 preceding the line start. Skip over invisible text that is so
5237 because of selective display. Compute faces, overlays etc at the
5238 new position. Note that this function does not skip over text that
5239 is invisible because of text properties. */
5240
5241 static void
5242 reseat_at_next_visible_line_start (it, on_newline_p)
5243 struct it *it;
5244 int on_newline_p;
5245 {
5246 int newline_found_p, skipped_p = 0;
5247
5248 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5249
5250 /* Skip over lines that are invisible because they are indented
5251 more than the value of IT->selective. */
5252 if (it->selective > 0)
5253 while (IT_CHARPOS (*it) < ZV
5254 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5255 (double) it->selective)) /* iftc */
5256 {
5257 xassert (IT_BYTEPOS (*it) == BEGV
5258 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5259 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5260 }
5261
5262 /* Position on the newline if that's what's requested. */
5263 if (on_newline_p && newline_found_p)
5264 {
5265 if (STRINGP (it->string))
5266 {
5267 if (IT_STRING_CHARPOS (*it) > 0)
5268 {
5269 --IT_STRING_CHARPOS (*it);
5270 --IT_STRING_BYTEPOS (*it);
5271 }
5272 }
5273 else if (IT_CHARPOS (*it) > BEGV)
5274 {
5275 --IT_CHARPOS (*it);
5276 --IT_BYTEPOS (*it);
5277 reseat (it, it->current.pos, 0);
5278 }
5279 }
5280 else if (skipped_p)
5281 reseat (it, it->current.pos, 0);
5282
5283 CHECK_IT (it);
5284 }
5285
5286
5287 \f
5288 /***********************************************************************
5289 Changing an iterator's position
5290 ***********************************************************************/
5291
5292 /* Change IT's current position to POS in current_buffer. If FORCE_P
5293 is non-zero, always check for text properties at the new position.
5294 Otherwise, text properties are only looked up if POS >=
5295 IT->check_charpos of a property. */
5296
5297 static void
5298 reseat (it, pos, force_p)
5299 struct it *it;
5300 struct text_pos pos;
5301 int force_p;
5302 {
5303 int original_pos = IT_CHARPOS (*it);
5304
5305 reseat_1 (it, pos, 0);
5306
5307 /* Determine where to check text properties. Avoid doing it
5308 where possible because text property lookup is very expensive. */
5309 if (force_p
5310 || CHARPOS (pos) > it->stop_charpos
5311 || CHARPOS (pos) < original_pos)
5312 handle_stop (it);
5313
5314 CHECK_IT (it);
5315 }
5316
5317
5318 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5319 IT->stop_pos to POS, also. */
5320
5321 static void
5322 reseat_1 (it, pos, set_stop_p)
5323 struct it *it;
5324 struct text_pos pos;
5325 int set_stop_p;
5326 {
5327 /* Don't call this function when scanning a C string. */
5328 xassert (it->s == NULL);
5329
5330 /* POS must be a reasonable value. */
5331 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5332
5333 it->current.pos = it->position = pos;
5334 XSETBUFFER (it->object, current_buffer);
5335 it->end_charpos = ZV;
5336 it->dpvec = NULL;
5337 it->current.dpvec_index = -1;
5338 it->current.overlay_string_index = -1;
5339 IT_STRING_CHARPOS (*it) = -1;
5340 IT_STRING_BYTEPOS (*it) = -1;
5341 it->string = Qnil;
5342 it->method = GET_FROM_BUFFER;
5343 /* RMS: I added this to fix a bug in move_it_vertically_backward
5344 where it->area continued to relate to the starting point
5345 for the backward motion. Bug report from
5346 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5347 However, I am not sure whether reseat still does the right thing
5348 in general after this change. */
5349 it->area = TEXT_AREA;
5350 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5351 it->sp = 0;
5352 it->face_before_selective_p = 0;
5353
5354 if (set_stop_p)
5355 it->stop_charpos = CHARPOS (pos);
5356 }
5357
5358
5359 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5360 If S is non-null, it is a C string to iterate over. Otherwise,
5361 STRING gives a Lisp string to iterate over.
5362
5363 If PRECISION > 0, don't return more then PRECISION number of
5364 characters from the string.
5365
5366 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5367 characters have been returned. FIELD_WIDTH < 0 means an infinite
5368 field width.
5369
5370 MULTIBYTE = 0 means disable processing of multibyte characters,
5371 MULTIBYTE > 0 means enable it,
5372 MULTIBYTE < 0 means use IT->multibyte_p.
5373
5374 IT must be initialized via a prior call to init_iterator before
5375 calling this function. */
5376
5377 static void
5378 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5379 struct it *it;
5380 unsigned char *s;
5381 Lisp_Object string;
5382 int charpos;
5383 int precision, field_width, multibyte;
5384 {
5385 /* No region in strings. */
5386 it->region_beg_charpos = it->region_end_charpos = -1;
5387
5388 /* No text property checks performed by default, but see below. */
5389 it->stop_charpos = -1;
5390
5391 /* Set iterator position and end position. */
5392 bzero (&it->current, sizeof it->current);
5393 it->current.overlay_string_index = -1;
5394 it->current.dpvec_index = -1;
5395 xassert (charpos >= 0);
5396
5397 /* If STRING is specified, use its multibyteness, otherwise use the
5398 setting of MULTIBYTE, if specified. */
5399 if (multibyte >= 0)
5400 it->multibyte_p = multibyte > 0;
5401
5402 if (s == NULL)
5403 {
5404 xassert (STRINGP (string));
5405 it->string = string;
5406 it->s = NULL;
5407 it->end_charpos = it->string_nchars = SCHARS (string);
5408 it->method = GET_FROM_STRING;
5409 it->current.string_pos = string_pos (charpos, string);
5410 }
5411 else
5412 {
5413 it->s = s;
5414 it->string = Qnil;
5415
5416 /* Note that we use IT->current.pos, not it->current.string_pos,
5417 for displaying C strings. */
5418 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5419 if (it->multibyte_p)
5420 {
5421 it->current.pos = c_string_pos (charpos, s, 1);
5422 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5423 }
5424 else
5425 {
5426 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5427 it->end_charpos = it->string_nchars = strlen (s);
5428 }
5429
5430 it->method = GET_FROM_C_STRING;
5431 }
5432
5433 /* PRECISION > 0 means don't return more than PRECISION characters
5434 from the string. */
5435 if (precision > 0 && it->end_charpos - charpos > precision)
5436 it->end_charpos = it->string_nchars = charpos + precision;
5437
5438 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5439 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5440 FIELD_WIDTH < 0 means infinite field width. This is useful for
5441 padding with `-' at the end of a mode line. */
5442 if (field_width < 0)
5443 field_width = INFINITY;
5444 if (field_width > it->end_charpos - charpos)
5445 it->end_charpos = charpos + field_width;
5446
5447 /* Use the standard display table for displaying strings. */
5448 if (DISP_TABLE_P (Vstandard_display_table))
5449 it->dp = XCHAR_TABLE (Vstandard_display_table);
5450
5451 it->stop_charpos = charpos;
5452 CHECK_IT (it);
5453 }
5454
5455
5456 \f
5457 /***********************************************************************
5458 Iteration
5459 ***********************************************************************/
5460
5461 /* Map enum it_method value to corresponding next_element_from_* function. */
5462
5463 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5464 {
5465 next_element_from_buffer,
5466 next_element_from_display_vector,
5467 next_element_from_composition,
5468 next_element_from_string,
5469 next_element_from_c_string,
5470 next_element_from_image,
5471 next_element_from_stretch
5472 };
5473
5474
5475 /* Load IT's display element fields with information about the next
5476 display element from the current position of IT. Value is zero if
5477 end of buffer (or C string) is reached. */
5478
5479 static struct frame *last_escape_glyph_frame = NULL;
5480 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5481 static int last_escape_glyph_merged_face_id = 0;
5482
5483 int
5484 get_next_display_element (it)
5485 struct it *it;
5486 {
5487 /* Non-zero means that we found a display element. Zero means that
5488 we hit the end of what we iterate over. Performance note: the
5489 function pointer `method' used here turns out to be faster than
5490 using a sequence of if-statements. */
5491 int success_p;
5492
5493 get_next:
5494 success_p = (*get_next_element[it->method]) (it);
5495
5496 if (it->what == IT_CHARACTER)
5497 {
5498 /* Map via display table or translate control characters.
5499 IT->c, IT->len etc. have been set to the next character by
5500 the function call above. If we have a display table, and it
5501 contains an entry for IT->c, translate it. Don't do this if
5502 IT->c itself comes from a display table, otherwise we could
5503 end up in an infinite recursion. (An alternative could be to
5504 count the recursion depth of this function and signal an
5505 error when a certain maximum depth is reached.) Is it worth
5506 it? */
5507 if (success_p && it->dpvec == NULL)
5508 {
5509 Lisp_Object dv;
5510
5511 if (it->dp
5512 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5513 VECTORP (dv)))
5514 {
5515 struct Lisp_Vector *v = XVECTOR (dv);
5516
5517 /* Return the first character from the display table
5518 entry, if not empty. If empty, don't display the
5519 current character. */
5520 if (v->size)
5521 {
5522 it->dpvec_char_len = it->len;
5523 it->dpvec = v->contents;
5524 it->dpend = v->contents + v->size;
5525 it->current.dpvec_index = 0;
5526 it->dpvec_face_id = -1;
5527 it->saved_face_id = it->face_id;
5528 it->method = GET_FROM_DISPLAY_VECTOR;
5529 it->ellipsis_p = 0;
5530 }
5531 else
5532 {
5533 set_iterator_to_next (it, 0);
5534 }
5535 goto get_next;
5536 }
5537
5538 /* Translate control characters into `\003' or `^C' form.
5539 Control characters coming from a display table entry are
5540 currently not translated because we use IT->dpvec to hold
5541 the translation. This could easily be changed but I
5542 don't believe that it is worth doing.
5543
5544 If it->multibyte_p is nonzero, non-printable non-ASCII
5545 characters are also translated to octal form.
5546
5547 If it->multibyte_p is zero, eight-bit characters that
5548 don't have corresponding multibyte char code are also
5549 translated to octal form. */
5550 else if ((it->c < ' '
5551 ? (it->area != TEXT_AREA
5552 /* In mode line, treat \n, \t like other crl chars. */
5553 || (it->c != '\t'
5554 && it->glyph_row && it->glyph_row->mode_line_p)
5555 || (it->c != '\n' && it->c != '\t'))
5556 : (it->multibyte_p
5557 ? (!CHAR_PRINTABLE_P (it->c)
5558 || (!NILP (Vnobreak_char_display)
5559 && (it->c == 0xA0 /* NO-BREAK SPACE */
5560 || it->c == 0xAD /* SOFT HYPHEN */)))
5561 : (it->c >= 127
5562 && (! unibyte_display_via_language_environment
5563 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5564 {
5565 /* IT->c is a control character which must be displayed
5566 either as '\003' or as `^C' where the '\\' and '^'
5567 can be defined in the display table. Fill
5568 IT->ctl_chars with glyphs for what we have to
5569 display. Then, set IT->dpvec to these glyphs. */
5570 GLYPH g;
5571 int ctl_len;
5572 int face_id, lface_id = 0 ;
5573 GLYPH escape_glyph;
5574
5575 /* Handle control characters with ^. */
5576
5577 if (it->c < 128 && it->ctl_arrow_p)
5578 {
5579 g = '^'; /* default glyph for Control */
5580 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5581 if (it->dp
5582 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5583 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5584 {
5585 g = XINT (DISP_CTRL_GLYPH (it->dp));
5586 lface_id = FAST_GLYPH_FACE (g);
5587 }
5588 if (lface_id)
5589 {
5590 g = FAST_GLYPH_CHAR (g);
5591 face_id = merge_faces (it->f, Qt, lface_id,
5592 it->face_id);
5593 }
5594 else if (it->f == last_escape_glyph_frame
5595 && it->face_id == last_escape_glyph_face_id)
5596 {
5597 face_id = last_escape_glyph_merged_face_id;
5598 }
5599 else
5600 {
5601 /* Merge the escape-glyph face into the current face. */
5602 face_id = merge_faces (it->f, Qescape_glyph, 0,
5603 it->face_id);
5604 last_escape_glyph_frame = it->f;
5605 last_escape_glyph_face_id = it->face_id;
5606 last_escape_glyph_merged_face_id = face_id;
5607 }
5608
5609 XSETINT (it->ctl_chars[0], g);
5610 g = it->c ^ 0100;
5611 XSETINT (it->ctl_chars[1], g);
5612 ctl_len = 2;
5613 goto display_control;
5614 }
5615
5616 /* Handle non-break space in the mode where it only gets
5617 highlighting. */
5618
5619 if (EQ (Vnobreak_char_display, Qt)
5620 && it->c == 0xA0)
5621 {
5622 /* Merge the no-break-space face into the current face. */
5623 face_id = merge_faces (it->f, Qnobreak_space, 0,
5624 it->face_id);
5625
5626 g = it->c = ' ';
5627 XSETINT (it->ctl_chars[0], g);
5628 ctl_len = 1;
5629 goto display_control;
5630 }
5631
5632 /* Handle sequences that start with the "escape glyph". */
5633
5634 /* the default escape glyph is \. */
5635 escape_glyph = '\\';
5636
5637 if (it->dp
5638 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5639 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5640 {
5641 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5642 lface_id = FAST_GLYPH_FACE (escape_glyph);
5643 }
5644 if (lface_id)
5645 {
5646 /* The display table specified a face.
5647 Merge it into face_id and also into escape_glyph. */
5648 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5649 face_id = merge_faces (it->f, Qt, lface_id,
5650 it->face_id);
5651 }
5652 else if (it->f == last_escape_glyph_frame
5653 && it->face_id == last_escape_glyph_face_id)
5654 {
5655 face_id = last_escape_glyph_merged_face_id;
5656 }
5657 else
5658 {
5659 /* Merge the escape-glyph face into the current face. */
5660 face_id = merge_faces (it->f, Qescape_glyph, 0,
5661 it->face_id);
5662 last_escape_glyph_frame = it->f;
5663 last_escape_glyph_face_id = it->face_id;
5664 last_escape_glyph_merged_face_id = face_id;
5665 }
5666
5667 /* Handle soft hyphens in the mode where they only get
5668 highlighting. */
5669
5670 if (EQ (Vnobreak_char_display, Qt)
5671 && it->c == 0xAD)
5672 {
5673 g = it->c = '-';
5674 XSETINT (it->ctl_chars[0], g);
5675 ctl_len = 1;
5676 goto display_control;
5677 }
5678
5679 /* Handle non-break space and soft hyphen
5680 with the escape glyph. */
5681
5682 if (it->c == 0xA0 || it->c == 0xAD)
5683 {
5684 XSETINT (it->ctl_chars[0], escape_glyph);
5685 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5686 XSETINT (it->ctl_chars[1], g);
5687 ctl_len = 2;
5688 goto display_control;
5689 }
5690
5691 {
5692 unsigned char str[MAX_MULTIBYTE_LENGTH];
5693 int len;
5694 int i;
5695
5696 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5697 if (CHAR_BYTE8_P (it->c))
5698 {
5699 str[0] = CHAR_TO_BYTE8 (it->c);
5700 len = 1;
5701 }
5702 else if (it->c < 256)
5703 {
5704 str[0] = it->c;
5705 len = 1;
5706 }
5707 else
5708 {
5709 /* It's an invalid character, which shouldn't
5710 happen actually, but due to bugs it may
5711 happen. Let's print the char as is, there's
5712 not much meaningful we can do with it. */
5713 str[0] = it->c;
5714 str[1] = it->c >> 8;
5715 str[2] = it->c >> 16;
5716 str[3] = it->c >> 24;
5717 len = 4;
5718 }
5719
5720 for (i = 0; i < len; i++)
5721 {
5722 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5723 /* Insert three more glyphs into IT->ctl_chars for
5724 the octal display of the character. */
5725 g = ((str[i] >> 6) & 7) + '0';
5726 XSETINT (it->ctl_chars[i * 4 + 1], g);
5727 g = ((str[i] >> 3) & 7) + '0';
5728 XSETINT (it->ctl_chars[i * 4 + 2], g);
5729 g = (str[i] & 7) + '0';
5730 XSETINT (it->ctl_chars[i * 4 + 3], g);
5731 }
5732 ctl_len = len * 4;
5733 }
5734
5735 display_control:
5736 /* Set up IT->dpvec and return first character from it. */
5737 it->dpvec_char_len = it->len;
5738 it->dpvec = it->ctl_chars;
5739 it->dpend = it->dpvec + ctl_len;
5740 it->current.dpvec_index = 0;
5741 it->dpvec_face_id = face_id;
5742 it->saved_face_id = it->face_id;
5743 it->method = GET_FROM_DISPLAY_VECTOR;
5744 it->ellipsis_p = 0;
5745 goto get_next;
5746 }
5747 }
5748
5749 /* Adjust face id for a multibyte character. There are no
5750 multibyte character in unibyte text. */
5751 if (it->multibyte_p
5752 && success_p
5753 && FRAME_WINDOW_P (it->f))
5754 {
5755 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5756 int pos = (it->s ? -1
5757 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5758 : IT_CHARPOS (*it));
5759
5760 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5761 }
5762 }
5763
5764 /* Is this character the last one of a run of characters with
5765 box? If yes, set IT->end_of_box_run_p to 1. */
5766 if (it->face_box_p
5767 && it->s == NULL)
5768 {
5769 int face_id;
5770 struct face *face;
5771
5772 it->end_of_box_run_p
5773 = ((face_id = face_after_it_pos (it),
5774 face_id != it->face_id)
5775 && (face = FACE_FROM_ID (it->f, face_id),
5776 face->box == FACE_NO_BOX));
5777 }
5778
5779 /* Value is 0 if end of buffer or string reached. */
5780 return success_p;
5781 }
5782
5783
5784 /* Move IT to the next display element.
5785
5786 RESEAT_P non-zero means if called on a newline in buffer text,
5787 skip to the next visible line start.
5788
5789 Functions get_next_display_element and set_iterator_to_next are
5790 separate because I find this arrangement easier to handle than a
5791 get_next_display_element function that also increments IT's
5792 position. The way it is we can first look at an iterator's current
5793 display element, decide whether it fits on a line, and if it does,
5794 increment the iterator position. The other way around we probably
5795 would either need a flag indicating whether the iterator has to be
5796 incremented the next time, or we would have to implement a
5797 decrement position function which would not be easy to write. */
5798
5799 void
5800 set_iterator_to_next (it, reseat_p)
5801 struct it *it;
5802 int reseat_p;
5803 {
5804 /* Reset flags indicating start and end of a sequence of characters
5805 with box. Reset them at the start of this function because
5806 moving the iterator to a new position might set them. */
5807 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5808
5809 switch (it->method)
5810 {
5811 case GET_FROM_BUFFER:
5812 /* The current display element of IT is a character from
5813 current_buffer. Advance in the buffer, and maybe skip over
5814 invisible lines that are so because of selective display. */
5815 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5816 reseat_at_next_visible_line_start (it, 0);
5817 else
5818 {
5819 xassert (it->len != 0);
5820 IT_BYTEPOS (*it) += it->len;
5821 IT_CHARPOS (*it) += 1;
5822 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5823 }
5824 break;
5825
5826 case GET_FROM_COMPOSITION:
5827 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5828 if (STRINGP (it->string))
5829 {
5830 IT_STRING_BYTEPOS (*it) += it->len;
5831 IT_STRING_CHARPOS (*it) += it->cmp_len;
5832 it->method = GET_FROM_STRING;
5833 goto consider_string_end;
5834 }
5835 else
5836 {
5837 IT_BYTEPOS (*it) += it->len;
5838 IT_CHARPOS (*it) += it->cmp_len;
5839 it->method = GET_FROM_BUFFER;
5840 }
5841 break;
5842
5843 case GET_FROM_C_STRING:
5844 /* Current display element of IT is from a C string. */
5845 IT_BYTEPOS (*it) += it->len;
5846 IT_CHARPOS (*it) += 1;
5847 break;
5848
5849 case GET_FROM_DISPLAY_VECTOR:
5850 /* Current display element of IT is from a display table entry.
5851 Advance in the display table definition. Reset it to null if
5852 end reached, and continue with characters from buffers/
5853 strings. */
5854 ++it->current.dpvec_index;
5855
5856 /* Restore face of the iterator to what they were before the
5857 display vector entry (these entries may contain faces). */
5858 it->face_id = it->saved_face_id;
5859
5860 if (it->dpvec + it->current.dpvec_index == it->dpend)
5861 {
5862 int recheck_faces = it->ellipsis_p;
5863
5864 if (it->s)
5865 it->method = GET_FROM_C_STRING;
5866 else if (STRINGP (it->string))
5867 it->method = GET_FROM_STRING;
5868 else
5869 it->method = GET_FROM_BUFFER;
5870
5871 it->dpvec = NULL;
5872 it->current.dpvec_index = -1;
5873
5874 /* Skip over characters which were displayed via IT->dpvec. */
5875 if (it->dpvec_char_len < 0)
5876 reseat_at_next_visible_line_start (it, 1);
5877 else if (it->dpvec_char_len > 0)
5878 {
5879 if (it->method == GET_FROM_STRING
5880 && it->n_overlay_strings > 0)
5881 it->ignore_overlay_strings_at_pos_p = 1;
5882 it->len = it->dpvec_char_len;
5883 set_iterator_to_next (it, reseat_p);
5884 }
5885
5886 /* Maybe recheck faces after display vector */
5887 if (recheck_faces)
5888 it->stop_charpos = IT_CHARPOS (*it);
5889 }
5890 break;
5891
5892 case GET_FROM_STRING:
5893 /* Current display element is a character from a Lisp string. */
5894 xassert (it->s == NULL && STRINGP (it->string));
5895 IT_STRING_BYTEPOS (*it) += it->len;
5896 IT_STRING_CHARPOS (*it) += 1;
5897
5898 consider_string_end:
5899
5900 if (it->current.overlay_string_index >= 0)
5901 {
5902 /* IT->string is an overlay string. Advance to the
5903 next, if there is one. */
5904 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5905 next_overlay_string (it);
5906 }
5907 else
5908 {
5909 /* IT->string is not an overlay string. If we reached
5910 its end, and there is something on IT->stack, proceed
5911 with what is on the stack. This can be either another
5912 string, this time an overlay string, or a buffer. */
5913 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5914 && it->sp > 0)
5915 {
5916 pop_it (it);
5917 if (STRINGP (it->string))
5918 goto consider_string_end;
5919 it->method = GET_FROM_BUFFER;
5920 }
5921 }
5922 break;
5923
5924 case GET_FROM_IMAGE:
5925 case GET_FROM_STRETCH:
5926 /* The position etc with which we have to proceed are on
5927 the stack. The position may be at the end of a string,
5928 if the `display' property takes up the whole string. */
5929 xassert (it->sp > 0);
5930 pop_it (it);
5931 it->image_id = 0;
5932 if (STRINGP (it->string))
5933 {
5934 it->method = GET_FROM_STRING;
5935 goto consider_string_end;
5936 }
5937 it->method = GET_FROM_BUFFER;
5938 break;
5939
5940 default:
5941 /* There are no other methods defined, so this should be a bug. */
5942 abort ();
5943 }
5944
5945 xassert (it->method != GET_FROM_STRING
5946 || (STRINGP (it->string)
5947 && IT_STRING_CHARPOS (*it) >= 0));
5948 }
5949
5950 /* Load IT's display element fields with information about the next
5951 display element which comes from a display table entry or from the
5952 result of translating a control character to one of the forms `^C'
5953 or `\003'.
5954
5955 IT->dpvec holds the glyphs to return as characters.
5956 IT->saved_face_id holds the face id before the display vector--
5957 it is restored into IT->face_idin set_iterator_to_next. */
5958
5959 static int
5960 next_element_from_display_vector (it)
5961 struct it *it;
5962 {
5963 /* Precondition. */
5964 xassert (it->dpvec && it->current.dpvec_index >= 0);
5965
5966 it->face_id = it->saved_face_id;
5967
5968 if (INTEGERP (*it->dpvec)
5969 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5970 {
5971 GLYPH g;
5972
5973 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5974 it->c = FAST_GLYPH_CHAR (g);
5975 it->len = CHAR_BYTES (it->c);
5976
5977 /* The entry may contain a face id to use. Such a face id is
5978 the id of a Lisp face, not a realized face. A face id of
5979 zero means no face is specified. */
5980 if (it->dpvec_face_id >= 0)
5981 it->face_id = it->dpvec_face_id;
5982 else
5983 {
5984 int lface_id = FAST_GLYPH_FACE (g);
5985 if (lface_id > 0)
5986 it->face_id = merge_faces (it->f, Qt, lface_id,
5987 it->saved_face_id);
5988 }
5989 }
5990 else
5991 /* Display table entry is invalid. Return a space. */
5992 it->c = ' ', it->len = 1;
5993
5994 /* Don't change position and object of the iterator here. They are
5995 still the values of the character that had this display table
5996 entry or was translated, and that's what we want. */
5997 it->what = IT_CHARACTER;
5998 return 1;
5999 }
6000
6001
6002 /* Load IT with the next display element from Lisp string IT->string.
6003 IT->current.string_pos is the current position within the string.
6004 If IT->current.overlay_string_index >= 0, the Lisp string is an
6005 overlay string. */
6006
6007 static int
6008 next_element_from_string (it)
6009 struct it *it;
6010 {
6011 struct text_pos position;
6012
6013 xassert (STRINGP (it->string));
6014 xassert (IT_STRING_CHARPOS (*it) >= 0);
6015 position = it->current.string_pos;
6016
6017 /* Time to check for invisible text? */
6018 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6019 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6020 {
6021 handle_stop (it);
6022
6023 /* Since a handler may have changed IT->method, we must
6024 recurse here. */
6025 return get_next_display_element (it);
6026 }
6027
6028 if (it->current.overlay_string_index >= 0)
6029 {
6030 /* Get the next character from an overlay string. In overlay
6031 strings, There is no field width or padding with spaces to
6032 do. */
6033 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6034 {
6035 it->what = IT_EOB;
6036 return 0;
6037 }
6038 else if (STRING_MULTIBYTE (it->string))
6039 {
6040 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6041 const unsigned char *s = (SDATA (it->string)
6042 + IT_STRING_BYTEPOS (*it));
6043 it->c = string_char_and_length (s, remaining, &it->len);
6044 }
6045 else
6046 {
6047 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6048 it->len = 1;
6049 }
6050 }
6051 else
6052 {
6053 /* Get the next character from a Lisp string that is not an
6054 overlay string. Such strings come from the mode line, for
6055 example. We may have to pad with spaces, or truncate the
6056 string. See also next_element_from_c_string. */
6057 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6058 {
6059 it->what = IT_EOB;
6060 return 0;
6061 }
6062 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6063 {
6064 /* Pad with spaces. */
6065 it->c = ' ', it->len = 1;
6066 CHARPOS (position) = BYTEPOS (position) = -1;
6067 }
6068 else if (STRING_MULTIBYTE (it->string))
6069 {
6070 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6071 const unsigned char *s = (SDATA (it->string)
6072 + IT_STRING_BYTEPOS (*it));
6073 it->c = string_char_and_length (s, maxlen, &it->len);
6074 }
6075 else
6076 {
6077 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6078 it->len = 1;
6079 }
6080 }
6081
6082 /* Record what we have and where it came from. Note that we store a
6083 buffer position in IT->position although it could arguably be a
6084 string position. */
6085 it->what = IT_CHARACTER;
6086 it->object = it->string;
6087 it->position = position;
6088 return 1;
6089 }
6090
6091
6092 /* Load IT with next display element from C string IT->s.
6093 IT->string_nchars is the maximum number of characters to return
6094 from the string. IT->end_charpos may be greater than
6095 IT->string_nchars when this function is called, in which case we
6096 may have to return padding spaces. Value is zero if end of string
6097 reached, including padding spaces. */
6098
6099 static int
6100 next_element_from_c_string (it)
6101 struct it *it;
6102 {
6103 int success_p = 1;
6104
6105 xassert (it->s);
6106 it->what = IT_CHARACTER;
6107 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6108 it->object = Qnil;
6109
6110 /* IT's position can be greater IT->string_nchars in case a field
6111 width or precision has been specified when the iterator was
6112 initialized. */
6113 if (IT_CHARPOS (*it) >= it->end_charpos)
6114 {
6115 /* End of the game. */
6116 it->what = IT_EOB;
6117 success_p = 0;
6118 }
6119 else if (IT_CHARPOS (*it) >= it->string_nchars)
6120 {
6121 /* Pad with spaces. */
6122 it->c = ' ', it->len = 1;
6123 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6124 }
6125 else if (it->multibyte_p)
6126 {
6127 /* Implementation note: The calls to strlen apparently aren't a
6128 performance problem because there is no noticeable performance
6129 difference between Emacs running in unibyte or multibyte mode. */
6130 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6131 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6132 maxlen, &it->len);
6133 }
6134 else
6135 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6136
6137 return success_p;
6138 }
6139
6140
6141 /* Set up IT to return characters from an ellipsis, if appropriate.
6142 The definition of the ellipsis glyphs may come from a display table
6143 entry. This function Fills IT with the first glyph from the
6144 ellipsis if an ellipsis is to be displayed. */
6145
6146 static int
6147 next_element_from_ellipsis (it)
6148 struct it *it;
6149 {
6150 if (it->selective_display_ellipsis_p)
6151 setup_for_ellipsis (it, it->len);
6152 else
6153 {
6154 /* The face at the current position may be different from the
6155 face we find after the invisible text. Remember what it
6156 was in IT->saved_face_id, and signal that it's there by
6157 setting face_before_selective_p. */
6158 it->saved_face_id = it->face_id;
6159 it->method = GET_FROM_BUFFER;
6160 reseat_at_next_visible_line_start (it, 1);
6161 it->face_before_selective_p = 1;
6162 }
6163
6164 return get_next_display_element (it);
6165 }
6166
6167
6168 /* Deliver an image display element. The iterator IT is already
6169 filled with image information (done in handle_display_prop). Value
6170 is always 1. */
6171
6172
6173 static int
6174 next_element_from_image (it)
6175 struct it *it;
6176 {
6177 it->what = IT_IMAGE;
6178 return 1;
6179 }
6180
6181
6182 /* Fill iterator IT with next display element from a stretch glyph
6183 property. IT->object is the value of the text property. Value is
6184 always 1. */
6185
6186 static int
6187 next_element_from_stretch (it)
6188 struct it *it;
6189 {
6190 it->what = IT_STRETCH;
6191 return 1;
6192 }
6193
6194
6195 /* Load IT with the next display element from current_buffer. Value
6196 is zero if end of buffer reached. IT->stop_charpos is the next
6197 position at which to stop and check for text properties or buffer
6198 end. */
6199
6200 static int
6201 next_element_from_buffer (it)
6202 struct it *it;
6203 {
6204 int success_p = 1;
6205
6206 /* Check this assumption, otherwise, we would never enter the
6207 if-statement, below. */
6208 xassert (IT_CHARPOS (*it) >= BEGV
6209 && IT_CHARPOS (*it) <= it->stop_charpos);
6210
6211 if (IT_CHARPOS (*it) >= it->stop_charpos)
6212 {
6213 if (IT_CHARPOS (*it) >= it->end_charpos)
6214 {
6215 int overlay_strings_follow_p;
6216
6217 /* End of the game, except when overlay strings follow that
6218 haven't been returned yet. */
6219 if (it->overlay_strings_at_end_processed_p)
6220 overlay_strings_follow_p = 0;
6221 else
6222 {
6223 it->overlay_strings_at_end_processed_p = 1;
6224 overlay_strings_follow_p = get_overlay_strings (it, 0);
6225 }
6226
6227 if (overlay_strings_follow_p)
6228 success_p = get_next_display_element (it);
6229 else
6230 {
6231 it->what = IT_EOB;
6232 it->position = it->current.pos;
6233 success_p = 0;
6234 }
6235 }
6236 else
6237 {
6238 handle_stop (it);
6239 return get_next_display_element (it);
6240 }
6241 }
6242 else
6243 {
6244 /* No face changes, overlays etc. in sight, so just return a
6245 character from current_buffer. */
6246 unsigned char *p;
6247
6248 /* Maybe run the redisplay end trigger hook. Performance note:
6249 This doesn't seem to cost measurable time. */
6250 if (it->redisplay_end_trigger_charpos
6251 && it->glyph_row
6252 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6253 run_redisplay_end_trigger_hook (it);
6254
6255 /* Get the next character, maybe multibyte. */
6256 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6257 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6258 {
6259 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6260 - IT_BYTEPOS (*it));
6261 it->c = string_char_and_length (p, maxlen, &it->len);
6262 }
6263 else
6264 it->c = *p, it->len = 1;
6265
6266 /* Record what we have and where it came from. */
6267 it->what = IT_CHARACTER;;
6268 it->object = it->w->buffer;
6269 it->position = it->current.pos;
6270
6271 /* Normally we return the character found above, except when we
6272 really want to return an ellipsis for selective display. */
6273 if (it->selective)
6274 {
6275 if (it->c == '\n')
6276 {
6277 /* A value of selective > 0 means hide lines indented more
6278 than that number of columns. */
6279 if (it->selective > 0
6280 && IT_CHARPOS (*it) + 1 < ZV
6281 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6282 IT_BYTEPOS (*it) + 1,
6283 (double) it->selective)) /* iftc */
6284 {
6285 success_p = next_element_from_ellipsis (it);
6286 it->dpvec_char_len = -1;
6287 }
6288 }
6289 else if (it->c == '\r' && it->selective == -1)
6290 {
6291 /* A value of selective == -1 means that everything from the
6292 CR to the end of the line is invisible, with maybe an
6293 ellipsis displayed for it. */
6294 success_p = next_element_from_ellipsis (it);
6295 it->dpvec_char_len = -1;
6296 }
6297 }
6298 }
6299
6300 /* Value is zero if end of buffer reached. */
6301 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6302 return success_p;
6303 }
6304
6305
6306 /* Run the redisplay end trigger hook for IT. */
6307
6308 static void
6309 run_redisplay_end_trigger_hook (it)
6310 struct it *it;
6311 {
6312 Lisp_Object args[3];
6313
6314 /* IT->glyph_row should be non-null, i.e. we should be actually
6315 displaying something, or otherwise we should not run the hook. */
6316 xassert (it->glyph_row);
6317
6318 /* Set up hook arguments. */
6319 args[0] = Qredisplay_end_trigger_functions;
6320 args[1] = it->window;
6321 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6322 it->redisplay_end_trigger_charpos = 0;
6323
6324 /* Since we are *trying* to run these functions, don't try to run
6325 them again, even if they get an error. */
6326 it->w->redisplay_end_trigger = Qnil;
6327 Frun_hook_with_args (3, args);
6328
6329 /* Notice if it changed the face of the character we are on. */
6330 handle_face_prop (it);
6331 }
6332
6333
6334 /* Deliver a composition display element. The iterator IT is already
6335 filled with composition information (done in
6336 handle_composition_prop). Value is always 1. */
6337
6338 static int
6339 next_element_from_composition (it)
6340 struct it *it;
6341 {
6342 it->what = IT_COMPOSITION;
6343 it->position = (STRINGP (it->string)
6344 ? it->current.string_pos
6345 : it->current.pos);
6346 if (STRINGP (it->string))
6347 it->object = it->string;
6348 return 1;
6349 }
6350
6351
6352 \f
6353 /***********************************************************************
6354 Moving an iterator without producing glyphs
6355 ***********************************************************************/
6356
6357 /* Check if iterator is at a position corresponding to a valid buffer
6358 position after some move_it_ call. */
6359
6360 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6361 ((it)->method == GET_FROM_STRING \
6362 ? IT_STRING_CHARPOS (*it) == 0 \
6363 : 1)
6364
6365
6366 /* Move iterator IT to a specified buffer or X position within one
6367 line on the display without producing glyphs.
6368
6369 OP should be a bit mask including some or all of these bits:
6370 MOVE_TO_X: Stop on reaching x-position TO_X.
6371 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6372 Regardless of OP's value, stop in reaching the end of the display line.
6373
6374 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6375 This means, in particular, that TO_X includes window's horizontal
6376 scroll amount.
6377
6378 The return value has several possible values that
6379 say what condition caused the scan to stop:
6380
6381 MOVE_POS_MATCH_OR_ZV
6382 - when TO_POS or ZV was reached.
6383
6384 MOVE_X_REACHED
6385 -when TO_X was reached before TO_POS or ZV were reached.
6386
6387 MOVE_LINE_CONTINUED
6388 - when we reached the end of the display area and the line must
6389 be continued.
6390
6391 MOVE_LINE_TRUNCATED
6392 - when we reached the end of the display area and the line is
6393 truncated.
6394
6395 MOVE_NEWLINE_OR_CR
6396 - when we stopped at a line end, i.e. a newline or a CR and selective
6397 display is on. */
6398
6399 static enum move_it_result
6400 move_it_in_display_line_to (it, to_charpos, to_x, op)
6401 struct it *it;
6402 int to_charpos, to_x, op;
6403 {
6404 enum move_it_result result = MOVE_UNDEFINED;
6405 struct glyph_row *saved_glyph_row;
6406
6407 /* Don't produce glyphs in produce_glyphs. */
6408 saved_glyph_row = it->glyph_row;
6409 it->glyph_row = NULL;
6410
6411 #define BUFFER_POS_REACHED_P() \
6412 ((op & MOVE_TO_POS) != 0 \
6413 && BUFFERP (it->object) \
6414 && IT_CHARPOS (*it) >= to_charpos \
6415 && (it->method == GET_FROM_BUFFER \
6416 || (it->method == GET_FROM_DISPLAY_VECTOR \
6417 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6418
6419
6420 while (1)
6421 {
6422 int x, i, ascent = 0, descent = 0;
6423
6424 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6425 if ((op & MOVE_TO_POS) != 0
6426 && BUFFERP (it->object)
6427 && it->method == GET_FROM_BUFFER
6428 && IT_CHARPOS (*it) > to_charpos)
6429 {
6430 result = MOVE_POS_MATCH_OR_ZV;
6431 break;
6432 }
6433
6434 /* Stop when ZV reached.
6435 We used to stop here when TO_CHARPOS reached as well, but that is
6436 too soon if this glyph does not fit on this line. So we handle it
6437 explicitly below. */
6438 if (!get_next_display_element (it)
6439 || (it->truncate_lines_p
6440 && BUFFER_POS_REACHED_P ()))
6441 {
6442 result = MOVE_POS_MATCH_OR_ZV;
6443 break;
6444 }
6445
6446 /* The call to produce_glyphs will get the metrics of the
6447 display element IT is loaded with. We record in x the
6448 x-position before this display element in case it does not
6449 fit on the line. */
6450 x = it->current_x;
6451
6452 /* Remember the line height so far in case the next element doesn't
6453 fit on the line. */
6454 if (!it->truncate_lines_p)
6455 {
6456 ascent = it->max_ascent;
6457 descent = it->max_descent;
6458 }
6459
6460 PRODUCE_GLYPHS (it);
6461
6462 if (it->area != TEXT_AREA)
6463 {
6464 set_iterator_to_next (it, 1);
6465 continue;
6466 }
6467
6468 /* The number of glyphs we get back in IT->nglyphs will normally
6469 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6470 character on a terminal frame, or (iii) a line end. For the
6471 second case, IT->nglyphs - 1 padding glyphs will be present
6472 (on X frames, there is only one glyph produced for a
6473 composite character.
6474
6475 The behavior implemented below means, for continuation lines,
6476 that as many spaces of a TAB as fit on the current line are
6477 displayed there. For terminal frames, as many glyphs of a
6478 multi-glyph character are displayed in the current line, too.
6479 This is what the old redisplay code did, and we keep it that
6480 way. Under X, the whole shape of a complex character must
6481 fit on the line or it will be completely displayed in the
6482 next line.
6483
6484 Note that both for tabs and padding glyphs, all glyphs have
6485 the same width. */
6486 if (it->nglyphs)
6487 {
6488 /* More than one glyph or glyph doesn't fit on line. All
6489 glyphs have the same width. */
6490 int single_glyph_width = it->pixel_width / it->nglyphs;
6491 int new_x;
6492 int x_before_this_char = x;
6493 int hpos_before_this_char = it->hpos;
6494
6495 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6496 {
6497 new_x = x + single_glyph_width;
6498
6499 /* We want to leave anything reaching TO_X to the caller. */
6500 if ((op & MOVE_TO_X) && new_x > to_x)
6501 {
6502 if (BUFFER_POS_REACHED_P ())
6503 goto buffer_pos_reached;
6504 it->current_x = x;
6505 result = MOVE_X_REACHED;
6506 break;
6507 }
6508 else if (/* Lines are continued. */
6509 !it->truncate_lines_p
6510 && (/* And glyph doesn't fit on the line. */
6511 new_x > it->last_visible_x
6512 /* Or it fits exactly and we're on a window
6513 system frame. */
6514 || (new_x == it->last_visible_x
6515 && FRAME_WINDOW_P (it->f))))
6516 {
6517 if (/* IT->hpos == 0 means the very first glyph
6518 doesn't fit on the line, e.g. a wide image. */
6519 it->hpos == 0
6520 || (new_x == it->last_visible_x
6521 && FRAME_WINDOW_P (it->f)))
6522 {
6523 ++it->hpos;
6524 it->current_x = new_x;
6525
6526 /* The character's last glyph just barely fits
6527 in this row. */
6528 if (i == it->nglyphs - 1)
6529 {
6530 /* If this is the destination position,
6531 return a position *before* it in this row,
6532 now that we know it fits in this row. */
6533 if (BUFFER_POS_REACHED_P ())
6534 {
6535 it->hpos = hpos_before_this_char;
6536 it->current_x = x_before_this_char;
6537 result = MOVE_POS_MATCH_OR_ZV;
6538 break;
6539 }
6540
6541 set_iterator_to_next (it, 1);
6542 #ifdef HAVE_WINDOW_SYSTEM
6543 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6544 {
6545 if (!get_next_display_element (it))
6546 {
6547 result = MOVE_POS_MATCH_OR_ZV;
6548 break;
6549 }
6550 if (BUFFER_POS_REACHED_P ())
6551 {
6552 if (ITERATOR_AT_END_OF_LINE_P (it))
6553 result = MOVE_POS_MATCH_OR_ZV;
6554 else
6555 result = MOVE_LINE_CONTINUED;
6556 break;
6557 }
6558 if (ITERATOR_AT_END_OF_LINE_P (it))
6559 {
6560 result = MOVE_NEWLINE_OR_CR;
6561 break;
6562 }
6563 }
6564 #endif /* HAVE_WINDOW_SYSTEM */
6565 }
6566 }
6567 else
6568 {
6569 it->current_x = x;
6570 it->max_ascent = ascent;
6571 it->max_descent = descent;
6572 }
6573
6574 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6575 IT_CHARPOS (*it)));
6576 result = MOVE_LINE_CONTINUED;
6577 break;
6578 }
6579 else if (BUFFER_POS_REACHED_P ())
6580 goto buffer_pos_reached;
6581 else if (new_x > it->first_visible_x)
6582 {
6583 /* Glyph is visible. Increment number of glyphs that
6584 would be displayed. */
6585 ++it->hpos;
6586 }
6587 else
6588 {
6589 /* Glyph is completely off the left margin of the display
6590 area. Nothing to do. */
6591 }
6592 }
6593
6594 if (result != MOVE_UNDEFINED)
6595 break;
6596 }
6597 else if (BUFFER_POS_REACHED_P ())
6598 {
6599 buffer_pos_reached:
6600 it->current_x = x;
6601 it->max_ascent = ascent;
6602 it->max_descent = descent;
6603 result = MOVE_POS_MATCH_OR_ZV;
6604 break;
6605 }
6606 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6607 {
6608 /* Stop when TO_X specified and reached. This check is
6609 necessary here because of lines consisting of a line end,
6610 only. The line end will not produce any glyphs and we
6611 would never get MOVE_X_REACHED. */
6612 xassert (it->nglyphs == 0);
6613 result = MOVE_X_REACHED;
6614 break;
6615 }
6616
6617 /* Is this a line end? If yes, we're done. */
6618 if (ITERATOR_AT_END_OF_LINE_P (it))
6619 {
6620 result = MOVE_NEWLINE_OR_CR;
6621 break;
6622 }
6623
6624 /* The current display element has been consumed. Advance
6625 to the next. */
6626 set_iterator_to_next (it, 1);
6627
6628 /* Stop if lines are truncated and IT's current x-position is
6629 past the right edge of the window now. */
6630 if (it->truncate_lines_p
6631 && it->current_x >= it->last_visible_x)
6632 {
6633 #ifdef HAVE_WINDOW_SYSTEM
6634 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6635 {
6636 if (!get_next_display_element (it)
6637 || BUFFER_POS_REACHED_P ())
6638 {
6639 result = MOVE_POS_MATCH_OR_ZV;
6640 break;
6641 }
6642 if (ITERATOR_AT_END_OF_LINE_P (it))
6643 {
6644 result = MOVE_NEWLINE_OR_CR;
6645 break;
6646 }
6647 }
6648 #endif /* HAVE_WINDOW_SYSTEM */
6649 result = MOVE_LINE_TRUNCATED;
6650 break;
6651 }
6652 }
6653
6654 #undef BUFFER_POS_REACHED_P
6655
6656 /* Restore the iterator settings altered at the beginning of this
6657 function. */
6658 it->glyph_row = saved_glyph_row;
6659 return result;
6660 }
6661
6662
6663 /* Move IT forward until it satisfies one or more of the criteria in
6664 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6665
6666 OP is a bit-mask that specifies where to stop, and in particular,
6667 which of those four position arguments makes a difference. See the
6668 description of enum move_operation_enum.
6669
6670 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6671 screen line, this function will set IT to the next position >
6672 TO_CHARPOS. */
6673
6674 void
6675 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6676 struct it *it;
6677 int to_charpos, to_x, to_y, to_vpos;
6678 int op;
6679 {
6680 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6681 int line_height;
6682 int reached = 0;
6683
6684 for (;;)
6685 {
6686 if (op & MOVE_TO_VPOS)
6687 {
6688 /* If no TO_CHARPOS and no TO_X specified, stop at the
6689 start of the line TO_VPOS. */
6690 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6691 {
6692 if (it->vpos == to_vpos)
6693 {
6694 reached = 1;
6695 break;
6696 }
6697 else
6698 skip = move_it_in_display_line_to (it, -1, -1, 0);
6699 }
6700 else
6701 {
6702 /* TO_VPOS >= 0 means stop at TO_X in the line at
6703 TO_VPOS, or at TO_POS, whichever comes first. */
6704 if (it->vpos == to_vpos)
6705 {
6706 reached = 2;
6707 break;
6708 }
6709
6710 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6711
6712 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6713 {
6714 reached = 3;
6715 break;
6716 }
6717 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6718 {
6719 /* We have reached TO_X but not in the line we want. */
6720 skip = move_it_in_display_line_to (it, to_charpos,
6721 -1, MOVE_TO_POS);
6722 if (skip == MOVE_POS_MATCH_OR_ZV)
6723 {
6724 reached = 4;
6725 break;
6726 }
6727 }
6728 }
6729 }
6730 else if (op & MOVE_TO_Y)
6731 {
6732 struct it it_backup;
6733
6734 /* TO_Y specified means stop at TO_X in the line containing
6735 TO_Y---or at TO_CHARPOS if this is reached first. The
6736 problem is that we can't really tell whether the line
6737 contains TO_Y before we have completely scanned it, and
6738 this may skip past TO_X. What we do is to first scan to
6739 TO_X.
6740
6741 If TO_X is not specified, use a TO_X of zero. The reason
6742 is to make the outcome of this function more predictable.
6743 If we didn't use TO_X == 0, we would stop at the end of
6744 the line which is probably not what a caller would expect
6745 to happen. */
6746 skip = move_it_in_display_line_to (it, to_charpos,
6747 ((op & MOVE_TO_X)
6748 ? to_x : 0),
6749 (MOVE_TO_X
6750 | (op & MOVE_TO_POS)));
6751
6752 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6753 if (skip == MOVE_POS_MATCH_OR_ZV)
6754 {
6755 reached = 5;
6756 break;
6757 }
6758
6759 /* If TO_X was reached, we would like to know whether TO_Y
6760 is in the line. This can only be said if we know the
6761 total line height which requires us to scan the rest of
6762 the line. */
6763 if (skip == MOVE_X_REACHED)
6764 {
6765 /* Wait! We can conclude that TO_Y is in the line if
6766 the already scanned glyphs make the line tall enough
6767 because further scanning doesn't make it shorter. */
6768 line_height = it->max_ascent + it->max_descent;
6769 if (to_y >= it->current_y
6770 && to_y < it->current_y + line_height)
6771 {
6772 reached = 6;
6773 break;
6774 }
6775 it_backup = *it;
6776 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6777 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6778 op & MOVE_TO_POS);
6779 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6780 }
6781
6782 /* Now, decide whether TO_Y is in this line. */
6783 line_height = it->max_ascent + it->max_descent;
6784 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6785
6786 if (to_y >= it->current_y
6787 && to_y < it->current_y + line_height)
6788 {
6789 if (skip == MOVE_X_REACHED)
6790 /* If TO_Y is in this line and TO_X was reached above,
6791 we scanned too far. We have to restore IT's settings
6792 to the ones before skipping. */
6793 *it = it_backup;
6794 reached = 6;
6795 }
6796 else if (skip == MOVE_X_REACHED)
6797 {
6798 skip = skip2;
6799 if (skip == MOVE_POS_MATCH_OR_ZV)
6800 reached = 7;
6801 }
6802
6803 if (reached)
6804 break;
6805 }
6806 else
6807 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6808
6809 switch (skip)
6810 {
6811 case MOVE_POS_MATCH_OR_ZV:
6812 reached = 8;
6813 goto out;
6814
6815 case MOVE_NEWLINE_OR_CR:
6816 set_iterator_to_next (it, 1);
6817 it->continuation_lines_width = 0;
6818 break;
6819
6820 case MOVE_LINE_TRUNCATED:
6821 it->continuation_lines_width = 0;
6822 reseat_at_next_visible_line_start (it, 0);
6823 if ((op & MOVE_TO_POS) != 0
6824 && IT_CHARPOS (*it) > to_charpos)
6825 {
6826 reached = 9;
6827 goto out;
6828 }
6829 break;
6830
6831 case MOVE_LINE_CONTINUED:
6832 it->continuation_lines_width += it->current_x;
6833 break;
6834
6835 default:
6836 abort ();
6837 }
6838
6839 /* Reset/increment for the next run. */
6840 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6841 it->current_x = it->hpos = 0;
6842 it->current_y += it->max_ascent + it->max_descent;
6843 ++it->vpos;
6844 last_height = it->max_ascent + it->max_descent;
6845 last_max_ascent = it->max_ascent;
6846 it->max_ascent = it->max_descent = 0;
6847 }
6848
6849 out:
6850
6851 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6852 }
6853
6854
6855 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6856
6857 If DY > 0, move IT backward at least that many pixels. DY = 0
6858 means move IT backward to the preceding line start or BEGV. This
6859 function may move over more than DY pixels if IT->current_y - DY
6860 ends up in the middle of a line; in this case IT->current_y will be
6861 set to the top of the line moved to. */
6862
6863 void
6864 move_it_vertically_backward (it, dy)
6865 struct it *it;
6866 int dy;
6867 {
6868 int nlines, h;
6869 struct it it2, it3;
6870 int start_pos;
6871
6872 move_further_back:
6873 xassert (dy >= 0);
6874
6875 start_pos = IT_CHARPOS (*it);
6876
6877 /* Estimate how many newlines we must move back. */
6878 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6879
6880 /* Set the iterator's position that many lines back. */
6881 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6882 back_to_previous_visible_line_start (it);
6883
6884 /* Reseat the iterator here. When moving backward, we don't want
6885 reseat to skip forward over invisible text, set up the iterator
6886 to deliver from overlay strings at the new position etc. So,
6887 use reseat_1 here. */
6888 reseat_1 (it, it->current.pos, 1);
6889
6890 /* We are now surely at a line start. */
6891 it->current_x = it->hpos = 0;
6892 it->continuation_lines_width = 0;
6893
6894 /* Move forward and see what y-distance we moved. First move to the
6895 start of the next line so that we get its height. We need this
6896 height to be able to tell whether we reached the specified
6897 y-distance. */
6898 it2 = *it;
6899 it2.max_ascent = it2.max_descent = 0;
6900 do
6901 {
6902 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6903 MOVE_TO_POS | MOVE_TO_VPOS);
6904 }
6905 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6906 xassert (IT_CHARPOS (*it) >= BEGV);
6907 it3 = it2;
6908
6909 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6910 xassert (IT_CHARPOS (*it) >= BEGV);
6911 /* H is the actual vertical distance from the position in *IT
6912 and the starting position. */
6913 h = it2.current_y - it->current_y;
6914 /* NLINES is the distance in number of lines. */
6915 nlines = it2.vpos - it->vpos;
6916
6917 /* Correct IT's y and vpos position
6918 so that they are relative to the starting point. */
6919 it->vpos -= nlines;
6920 it->current_y -= h;
6921
6922 if (dy == 0)
6923 {
6924 /* DY == 0 means move to the start of the screen line. The
6925 value of nlines is > 0 if continuation lines were involved. */
6926 if (nlines > 0)
6927 move_it_by_lines (it, nlines, 1);
6928 #if 0
6929 /* I think this assert is bogus if buffer contains
6930 invisible text or images. KFS. */
6931 xassert (IT_CHARPOS (*it) <= start_pos);
6932 #endif
6933 }
6934 else
6935 {
6936 /* The y-position we try to reach, relative to *IT.
6937 Note that H has been subtracted in front of the if-statement. */
6938 int target_y = it->current_y + h - dy;
6939 int y0 = it3.current_y;
6940 int y1 = line_bottom_y (&it3);
6941 int line_height = y1 - y0;
6942
6943 /* If we did not reach target_y, try to move further backward if
6944 we can. If we moved too far backward, try to move forward. */
6945 if (target_y < it->current_y
6946 /* This is heuristic. In a window that's 3 lines high, with
6947 a line height of 13 pixels each, recentering with point
6948 on the bottom line will try to move -39/2 = 19 pixels
6949 backward. Try to avoid moving into the first line. */
6950 && (it->current_y - target_y
6951 > min (window_box_height (it->w), line_height * 2 / 3))
6952 && IT_CHARPOS (*it) > BEGV)
6953 {
6954 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6955 target_y - it->current_y));
6956 dy = it->current_y - target_y;
6957 goto move_further_back;
6958 }
6959 else if (target_y >= it->current_y + line_height
6960 && IT_CHARPOS (*it) < ZV)
6961 {
6962 /* Should move forward by at least one line, maybe more.
6963
6964 Note: Calling move_it_by_lines can be expensive on
6965 terminal frames, where compute_motion is used (via
6966 vmotion) to do the job, when there are very long lines
6967 and truncate-lines is nil. That's the reason for
6968 treating terminal frames specially here. */
6969
6970 if (!FRAME_WINDOW_P (it->f))
6971 move_it_vertically (it, target_y - (it->current_y + line_height));
6972 else
6973 {
6974 do
6975 {
6976 move_it_by_lines (it, 1, 1);
6977 }
6978 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6979 }
6980
6981 #if 0
6982 /* I think this assert is bogus if buffer contains
6983 invisible text or images. KFS. */
6984 xassert (IT_CHARPOS (*it) >= BEGV);
6985 #endif
6986 }
6987 }
6988 }
6989
6990
6991 /* Move IT by a specified amount of pixel lines DY. DY negative means
6992 move backwards. DY = 0 means move to start of screen line. At the
6993 end, IT will be on the start of a screen line. */
6994
6995 void
6996 move_it_vertically (it, dy)
6997 struct it *it;
6998 int dy;
6999 {
7000 if (dy <= 0)
7001 move_it_vertically_backward (it, -dy);
7002 else
7003 {
7004 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7005 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7006 MOVE_TO_POS | MOVE_TO_Y);
7007 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7008
7009 /* If buffer ends in ZV without a newline, move to the start of
7010 the line to satisfy the post-condition. */
7011 if (IT_CHARPOS (*it) == ZV
7012 && ZV > BEGV
7013 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7014 move_it_by_lines (it, 0, 0);
7015 }
7016 }
7017
7018
7019 /* Move iterator IT past the end of the text line it is in. */
7020
7021 void
7022 move_it_past_eol (it)
7023 struct it *it;
7024 {
7025 enum move_it_result rc;
7026
7027 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7028 if (rc == MOVE_NEWLINE_OR_CR)
7029 set_iterator_to_next (it, 0);
7030 }
7031
7032
7033 #if 0 /* Currently not used. */
7034
7035 /* Return non-zero if some text between buffer positions START_CHARPOS
7036 and END_CHARPOS is invisible. IT->window is the window for text
7037 property lookup. */
7038
7039 static int
7040 invisible_text_between_p (it, start_charpos, end_charpos)
7041 struct it *it;
7042 int start_charpos, end_charpos;
7043 {
7044 Lisp_Object prop, limit;
7045 int invisible_found_p;
7046
7047 xassert (it != NULL && start_charpos <= end_charpos);
7048
7049 /* Is text at START invisible? */
7050 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7051 it->window);
7052 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7053 invisible_found_p = 1;
7054 else
7055 {
7056 limit = Fnext_single_char_property_change (make_number (start_charpos),
7057 Qinvisible, Qnil,
7058 make_number (end_charpos));
7059 invisible_found_p = XFASTINT (limit) < end_charpos;
7060 }
7061
7062 return invisible_found_p;
7063 }
7064
7065 #endif /* 0 */
7066
7067
7068 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7069 negative means move up. DVPOS == 0 means move to the start of the
7070 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7071 NEED_Y_P is zero, IT->current_y will be left unchanged.
7072
7073 Further optimization ideas: If we would know that IT->f doesn't use
7074 a face with proportional font, we could be faster for
7075 truncate-lines nil. */
7076
7077 void
7078 move_it_by_lines (it, dvpos, need_y_p)
7079 struct it *it;
7080 int dvpos, need_y_p;
7081 {
7082 struct position pos;
7083
7084 if (!FRAME_WINDOW_P (it->f))
7085 {
7086 struct text_pos textpos;
7087
7088 /* We can use vmotion on frames without proportional fonts. */
7089 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7090 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7091 reseat (it, textpos, 1);
7092 it->vpos += pos.vpos;
7093 it->current_y += pos.vpos;
7094 }
7095 else if (dvpos == 0)
7096 {
7097 /* DVPOS == 0 means move to the start of the screen line. */
7098 move_it_vertically_backward (it, 0);
7099 xassert (it->current_x == 0 && it->hpos == 0);
7100 /* Let next call to line_bottom_y calculate real line height */
7101 last_height = 0;
7102 }
7103 else if (dvpos > 0)
7104 {
7105 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7106 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7107 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7108 }
7109 else
7110 {
7111 struct it it2;
7112 int start_charpos, i;
7113
7114 /* Start at the beginning of the screen line containing IT's
7115 position. This may actually move vertically backwards,
7116 in case of overlays, so adjust dvpos accordingly. */
7117 dvpos += it->vpos;
7118 move_it_vertically_backward (it, 0);
7119 dvpos -= it->vpos;
7120
7121 /* Go back -DVPOS visible lines and reseat the iterator there. */
7122 start_charpos = IT_CHARPOS (*it);
7123 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7124 back_to_previous_visible_line_start (it);
7125 reseat (it, it->current.pos, 1);
7126
7127 /* Move further back if we end up in a string or an image. */
7128 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7129 {
7130 /* First try to move to start of display line. */
7131 dvpos += it->vpos;
7132 move_it_vertically_backward (it, 0);
7133 dvpos -= it->vpos;
7134 if (IT_POS_VALID_AFTER_MOVE_P (it))
7135 break;
7136 /* If start of line is still in string or image,
7137 move further back. */
7138 back_to_previous_visible_line_start (it);
7139 reseat (it, it->current.pos, 1);
7140 dvpos--;
7141 }
7142
7143 it->current_x = it->hpos = 0;
7144
7145 /* Above call may have moved too far if continuation lines
7146 are involved. Scan forward and see if it did. */
7147 it2 = *it;
7148 it2.vpos = it2.current_y = 0;
7149 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7150 it->vpos -= it2.vpos;
7151 it->current_y -= it2.current_y;
7152 it->current_x = it->hpos = 0;
7153
7154 /* If we moved too far back, move IT some lines forward. */
7155 if (it2.vpos > -dvpos)
7156 {
7157 int delta = it2.vpos + dvpos;
7158 it2 = *it;
7159 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7160 /* Move back again if we got too far ahead. */
7161 if (IT_CHARPOS (*it) >= start_charpos)
7162 *it = it2;
7163 }
7164 }
7165 }
7166
7167 /* Return 1 if IT points into the middle of a display vector. */
7168
7169 int
7170 in_display_vector_p (it)
7171 struct it *it;
7172 {
7173 return (it->method == GET_FROM_DISPLAY_VECTOR
7174 && it->current.dpvec_index > 0
7175 && it->dpvec + it->current.dpvec_index != it->dpend);
7176 }
7177
7178 \f
7179 /***********************************************************************
7180 Messages
7181 ***********************************************************************/
7182
7183
7184 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7185 to *Messages*. */
7186
7187 void
7188 add_to_log (format, arg1, arg2)
7189 char *format;
7190 Lisp_Object arg1, arg2;
7191 {
7192 Lisp_Object args[3];
7193 Lisp_Object msg, fmt;
7194 char *buffer;
7195 int len;
7196 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7197 USE_SAFE_ALLOCA;
7198
7199 /* Do nothing if called asynchronously. Inserting text into
7200 a buffer may call after-change-functions and alike and
7201 that would means running Lisp asynchronously. */
7202 if (handling_signal)
7203 return;
7204
7205 fmt = msg = Qnil;
7206 GCPRO4 (fmt, msg, arg1, arg2);
7207
7208 args[0] = fmt = build_string (format);
7209 args[1] = arg1;
7210 args[2] = arg2;
7211 msg = Fformat (3, args);
7212
7213 len = SBYTES (msg) + 1;
7214 SAFE_ALLOCA (buffer, char *, len);
7215 bcopy (SDATA (msg), buffer, len);
7216
7217 message_dolog (buffer, len - 1, 1, 0);
7218 SAFE_FREE ();
7219
7220 UNGCPRO;
7221 }
7222
7223
7224 /* Output a newline in the *Messages* buffer if "needs" one. */
7225
7226 void
7227 message_log_maybe_newline ()
7228 {
7229 if (message_log_need_newline)
7230 message_dolog ("", 0, 1, 0);
7231 }
7232
7233
7234 /* Add a string M of length NBYTES to the message log, optionally
7235 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7236 nonzero, means interpret the contents of M as multibyte. This
7237 function calls low-level routines in order to bypass text property
7238 hooks, etc. which might not be safe to run.
7239
7240 This may GC (insert may run before/after change hooks),
7241 so the buffer M must NOT point to a Lisp string. */
7242
7243 void
7244 message_dolog (m, nbytes, nlflag, multibyte)
7245 const char *m;
7246 int nbytes, nlflag, multibyte;
7247 {
7248 if (!NILP (Vmemory_full))
7249 return;
7250
7251 if (!NILP (Vmessage_log_max))
7252 {
7253 struct buffer *oldbuf;
7254 Lisp_Object oldpoint, oldbegv, oldzv;
7255 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7256 int point_at_end = 0;
7257 int zv_at_end = 0;
7258 Lisp_Object old_deactivate_mark, tem;
7259 struct gcpro gcpro1;
7260
7261 old_deactivate_mark = Vdeactivate_mark;
7262 oldbuf = current_buffer;
7263 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7264 current_buffer->undo_list = Qt;
7265
7266 oldpoint = message_dolog_marker1;
7267 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7268 oldbegv = message_dolog_marker2;
7269 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7270 oldzv = message_dolog_marker3;
7271 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7272 GCPRO1 (old_deactivate_mark);
7273
7274 if (PT == Z)
7275 point_at_end = 1;
7276 if (ZV == Z)
7277 zv_at_end = 1;
7278
7279 BEGV = BEG;
7280 BEGV_BYTE = BEG_BYTE;
7281 ZV = Z;
7282 ZV_BYTE = Z_BYTE;
7283 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7284
7285 /* Insert the string--maybe converting multibyte to single byte
7286 or vice versa, so that all the text fits the buffer. */
7287 if (multibyte
7288 && NILP (current_buffer->enable_multibyte_characters))
7289 {
7290 int i, c, char_bytes;
7291 unsigned char work[1];
7292
7293 /* Convert a multibyte string to single-byte
7294 for the *Message* buffer. */
7295 for (i = 0; i < nbytes; i += char_bytes)
7296 {
7297 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7298 work[0] = (ASCII_CHAR_P (c)
7299 ? c
7300 : multibyte_char_to_unibyte (c, Qnil));
7301 insert_1_both (work, 1, 1, 1, 0, 0);
7302 }
7303 }
7304 else if (! multibyte
7305 && ! NILP (current_buffer->enable_multibyte_characters))
7306 {
7307 int i, c, char_bytes;
7308 unsigned char *msg = (unsigned char *) m;
7309 unsigned char str[MAX_MULTIBYTE_LENGTH];
7310 /* Convert a single-byte string to multibyte
7311 for the *Message* buffer. */
7312 for (i = 0; i < nbytes; i++)
7313 {
7314 c = msg[i];
7315 c = unibyte_char_to_multibyte (c);
7316 char_bytes = CHAR_STRING (c, str);
7317 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7318 }
7319 }
7320 else if (nbytes)
7321 insert_1 (m, nbytes, 1, 0, 0);
7322
7323 if (nlflag)
7324 {
7325 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7326 insert_1 ("\n", 1, 1, 0, 0);
7327
7328 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7329 this_bol = PT;
7330 this_bol_byte = PT_BYTE;
7331
7332 /* See if this line duplicates the previous one.
7333 If so, combine duplicates. */
7334 if (this_bol > BEG)
7335 {
7336 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7337 prev_bol = PT;
7338 prev_bol_byte = PT_BYTE;
7339
7340 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7341 this_bol, this_bol_byte);
7342 if (dup)
7343 {
7344 del_range_both (prev_bol, prev_bol_byte,
7345 this_bol, this_bol_byte, 0);
7346 if (dup > 1)
7347 {
7348 char dupstr[40];
7349 int duplen;
7350
7351 /* If you change this format, don't forget to also
7352 change message_log_check_duplicate. */
7353 sprintf (dupstr, " [%d times]", dup);
7354 duplen = strlen (dupstr);
7355 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7356 insert_1 (dupstr, duplen, 1, 0, 1);
7357 }
7358 }
7359 }
7360
7361 /* If we have more than the desired maximum number of lines
7362 in the *Messages* buffer now, delete the oldest ones.
7363 This is safe because we don't have undo in this buffer. */
7364
7365 if (NATNUMP (Vmessage_log_max))
7366 {
7367 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7368 -XFASTINT (Vmessage_log_max) - 1, 0);
7369 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7370 }
7371 }
7372 BEGV = XMARKER (oldbegv)->charpos;
7373 BEGV_BYTE = marker_byte_position (oldbegv);
7374
7375 if (zv_at_end)
7376 {
7377 ZV = Z;
7378 ZV_BYTE = Z_BYTE;
7379 }
7380 else
7381 {
7382 ZV = XMARKER (oldzv)->charpos;
7383 ZV_BYTE = marker_byte_position (oldzv);
7384 }
7385
7386 if (point_at_end)
7387 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7388 else
7389 /* We can't do Fgoto_char (oldpoint) because it will run some
7390 Lisp code. */
7391 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7392 XMARKER (oldpoint)->bytepos);
7393
7394 UNGCPRO;
7395 unchain_marker (XMARKER (oldpoint));
7396 unchain_marker (XMARKER (oldbegv));
7397 unchain_marker (XMARKER (oldzv));
7398
7399 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7400 set_buffer_internal (oldbuf);
7401 if (NILP (tem))
7402 windows_or_buffers_changed = old_windows_or_buffers_changed;
7403 message_log_need_newline = !nlflag;
7404 Vdeactivate_mark = old_deactivate_mark;
7405 }
7406 }
7407
7408
7409 /* We are at the end of the buffer after just having inserted a newline.
7410 (Note: We depend on the fact we won't be crossing the gap.)
7411 Check to see if the most recent message looks a lot like the previous one.
7412 Return 0 if different, 1 if the new one should just replace it, or a
7413 value N > 1 if we should also append " [N times]". */
7414
7415 static int
7416 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7417 int prev_bol, this_bol;
7418 int prev_bol_byte, this_bol_byte;
7419 {
7420 int i;
7421 int len = Z_BYTE - 1 - this_bol_byte;
7422 int seen_dots = 0;
7423 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7424 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7425
7426 for (i = 0; i < len; i++)
7427 {
7428 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7429 seen_dots = 1;
7430 if (p1[i] != p2[i])
7431 return seen_dots;
7432 }
7433 p1 += len;
7434 if (*p1 == '\n')
7435 return 2;
7436 if (*p1++ == ' ' && *p1++ == '[')
7437 {
7438 int n = 0;
7439 while (*p1 >= '0' && *p1 <= '9')
7440 n = n * 10 + *p1++ - '0';
7441 if (strncmp (p1, " times]\n", 8) == 0)
7442 return n+1;
7443 }
7444 return 0;
7445 }
7446 \f
7447
7448 /* Display an echo area message M with a specified length of NBYTES
7449 bytes. The string may include null characters. If M is 0, clear
7450 out any existing message, and let the mini-buffer text show
7451 through.
7452
7453 This may GC, so the buffer M must NOT point to a Lisp string. */
7454
7455 void
7456 message2 (m, nbytes, multibyte)
7457 const char *m;
7458 int nbytes;
7459 int multibyte;
7460 {
7461 /* First flush out any partial line written with print. */
7462 message_log_maybe_newline ();
7463 if (m)
7464 message_dolog (m, nbytes, 1, multibyte);
7465 message2_nolog (m, nbytes, multibyte);
7466 }
7467
7468
7469 /* The non-logging counterpart of message2. */
7470
7471 void
7472 message2_nolog (m, nbytes, multibyte)
7473 const char *m;
7474 int nbytes, multibyte;
7475 {
7476 struct frame *sf = SELECTED_FRAME ();
7477 message_enable_multibyte = multibyte;
7478
7479 if (noninteractive)
7480 {
7481 if (noninteractive_need_newline)
7482 putc ('\n', stderr);
7483 noninteractive_need_newline = 0;
7484 if (m)
7485 fwrite (m, nbytes, 1, stderr);
7486 if (cursor_in_echo_area == 0)
7487 fprintf (stderr, "\n");
7488 fflush (stderr);
7489 }
7490 /* A null message buffer means that the frame hasn't really been
7491 initialized yet. Error messages get reported properly by
7492 cmd_error, so this must be just an informative message; toss it. */
7493 else if (INTERACTIVE
7494 && sf->glyphs_initialized_p
7495 && FRAME_MESSAGE_BUF (sf))
7496 {
7497 Lisp_Object mini_window;
7498 struct frame *f;
7499
7500 /* Get the frame containing the mini-buffer
7501 that the selected frame is using. */
7502 mini_window = FRAME_MINIBUF_WINDOW (sf);
7503 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7504
7505 FRAME_SAMPLE_VISIBILITY (f);
7506 if (FRAME_VISIBLE_P (sf)
7507 && ! FRAME_VISIBLE_P (f))
7508 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7509
7510 if (m)
7511 {
7512 set_message (m, Qnil, nbytes, multibyte);
7513 if (minibuffer_auto_raise)
7514 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7515 }
7516 else
7517 clear_message (1, 1);
7518
7519 do_pending_window_change (0);
7520 echo_area_display (1);
7521 do_pending_window_change (0);
7522 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7523 (*frame_up_to_date_hook) (f);
7524 }
7525 }
7526
7527
7528 /* Display an echo area message M with a specified length of NBYTES
7529 bytes. The string may include null characters. If M is not a
7530 string, clear out any existing message, and let the mini-buffer
7531 text show through.
7532
7533 This function cancels echoing. */
7534
7535 void
7536 message3 (m, nbytes, multibyte)
7537 Lisp_Object m;
7538 int nbytes;
7539 int multibyte;
7540 {
7541 struct gcpro gcpro1;
7542
7543 GCPRO1 (m);
7544 clear_message (1,1);
7545 cancel_echoing ();
7546
7547 /* First flush out any partial line written with print. */
7548 message_log_maybe_newline ();
7549 if (STRINGP (m))
7550 {
7551 char *buffer;
7552 USE_SAFE_ALLOCA;
7553
7554 SAFE_ALLOCA (buffer, char *, nbytes);
7555 bcopy (SDATA (m), buffer, nbytes);
7556 message_dolog (buffer, nbytes, 1, multibyte);
7557 SAFE_FREE ();
7558 }
7559 message3_nolog (m, nbytes, multibyte);
7560
7561 UNGCPRO;
7562 }
7563
7564
7565 /* The non-logging version of message3.
7566 This does not cancel echoing, because it is used for echoing.
7567 Perhaps we need to make a separate function for echoing
7568 and make this cancel echoing. */
7569
7570 void
7571 message3_nolog (m, nbytes, multibyte)
7572 Lisp_Object m;
7573 int nbytes, multibyte;
7574 {
7575 struct frame *sf = SELECTED_FRAME ();
7576 message_enable_multibyte = multibyte;
7577
7578 if (noninteractive)
7579 {
7580 if (noninteractive_need_newline)
7581 putc ('\n', stderr);
7582 noninteractive_need_newline = 0;
7583 if (STRINGP (m))
7584 fwrite (SDATA (m), nbytes, 1, stderr);
7585 if (cursor_in_echo_area == 0)
7586 fprintf (stderr, "\n");
7587 fflush (stderr);
7588 }
7589 /* A null message buffer means that the frame hasn't really been
7590 initialized yet. Error messages get reported properly by
7591 cmd_error, so this must be just an informative message; toss it. */
7592 else if (INTERACTIVE
7593 && sf->glyphs_initialized_p
7594 && FRAME_MESSAGE_BUF (sf))
7595 {
7596 Lisp_Object mini_window;
7597 Lisp_Object frame;
7598 struct frame *f;
7599
7600 /* Get the frame containing the mini-buffer
7601 that the selected frame is using. */
7602 mini_window = FRAME_MINIBUF_WINDOW (sf);
7603 frame = XWINDOW (mini_window)->frame;
7604 f = XFRAME (frame);
7605
7606 FRAME_SAMPLE_VISIBILITY (f);
7607 if (FRAME_VISIBLE_P (sf)
7608 && !FRAME_VISIBLE_P (f))
7609 Fmake_frame_visible (frame);
7610
7611 if (STRINGP (m) && SCHARS (m) > 0)
7612 {
7613 set_message (NULL, m, nbytes, multibyte);
7614 if (minibuffer_auto_raise)
7615 Fraise_frame (frame);
7616 /* Assume we are not echoing.
7617 (If we are, echo_now will override this.) */
7618 echo_message_buffer = Qnil;
7619 }
7620 else
7621 clear_message (1, 1);
7622
7623 do_pending_window_change (0);
7624 echo_area_display (1);
7625 do_pending_window_change (0);
7626 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7627 (*frame_up_to_date_hook) (f);
7628 }
7629 }
7630
7631
7632 /* Display a null-terminated echo area message M. If M is 0, clear
7633 out any existing message, and let the mini-buffer text show through.
7634
7635 The buffer M must continue to exist until after the echo area gets
7636 cleared or some other message gets displayed there. Do not pass
7637 text that is stored in a Lisp string. Do not pass text in a buffer
7638 that was alloca'd. */
7639
7640 void
7641 message1 (m)
7642 char *m;
7643 {
7644 message2 (m, (m ? strlen (m) : 0), 0);
7645 }
7646
7647
7648 /* The non-logging counterpart of message1. */
7649
7650 void
7651 message1_nolog (m)
7652 char *m;
7653 {
7654 message2_nolog (m, (m ? strlen (m) : 0), 0);
7655 }
7656
7657 /* Display a message M which contains a single %s
7658 which gets replaced with STRING. */
7659
7660 void
7661 message_with_string (m, string, log)
7662 char *m;
7663 Lisp_Object string;
7664 int log;
7665 {
7666 CHECK_STRING (string);
7667
7668 if (noninteractive)
7669 {
7670 if (m)
7671 {
7672 if (noninteractive_need_newline)
7673 putc ('\n', stderr);
7674 noninteractive_need_newline = 0;
7675 fprintf (stderr, m, SDATA (string));
7676 if (cursor_in_echo_area == 0)
7677 fprintf (stderr, "\n");
7678 fflush (stderr);
7679 }
7680 }
7681 else if (INTERACTIVE)
7682 {
7683 /* The frame whose minibuffer we're going to display the message on.
7684 It may be larger than the selected frame, so we need
7685 to use its buffer, not the selected frame's buffer. */
7686 Lisp_Object mini_window;
7687 struct frame *f, *sf = SELECTED_FRAME ();
7688
7689 /* Get the frame containing the minibuffer
7690 that the selected frame is using. */
7691 mini_window = FRAME_MINIBUF_WINDOW (sf);
7692 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7693
7694 /* A null message buffer means that the frame hasn't really been
7695 initialized yet. Error messages get reported properly by
7696 cmd_error, so this must be just an informative message; toss it. */
7697 if (FRAME_MESSAGE_BUF (f))
7698 {
7699 Lisp_Object args[2], message;
7700 struct gcpro gcpro1, gcpro2;
7701
7702 args[0] = build_string (m);
7703 args[1] = message = string;
7704 GCPRO2 (args[0], message);
7705 gcpro1.nvars = 2;
7706
7707 message = Fformat (2, args);
7708
7709 if (log)
7710 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7711 else
7712 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7713
7714 UNGCPRO;
7715
7716 /* Print should start at the beginning of the message
7717 buffer next time. */
7718 message_buf_print = 0;
7719 }
7720 }
7721 }
7722
7723
7724 /* Dump an informative message to the minibuf. If M is 0, clear out
7725 any existing message, and let the mini-buffer text show through. */
7726
7727 /* VARARGS 1 */
7728 void
7729 message (m, a1, a2, a3)
7730 char *m;
7731 EMACS_INT a1, a2, a3;
7732 {
7733 if (noninteractive)
7734 {
7735 if (m)
7736 {
7737 if (noninteractive_need_newline)
7738 putc ('\n', stderr);
7739 noninteractive_need_newline = 0;
7740 fprintf (stderr, m, a1, a2, a3);
7741 if (cursor_in_echo_area == 0)
7742 fprintf (stderr, "\n");
7743 fflush (stderr);
7744 }
7745 }
7746 else if (INTERACTIVE)
7747 {
7748 /* The frame whose mini-buffer we're going to display the message
7749 on. It may be larger than the selected frame, so we need to
7750 use its buffer, not the selected frame's buffer. */
7751 Lisp_Object mini_window;
7752 struct frame *f, *sf = SELECTED_FRAME ();
7753
7754 /* Get the frame containing the mini-buffer
7755 that the selected frame is using. */
7756 mini_window = FRAME_MINIBUF_WINDOW (sf);
7757 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7758
7759 /* A null message buffer means that the frame hasn't really been
7760 initialized yet. Error messages get reported properly by
7761 cmd_error, so this must be just an informative message; toss
7762 it. */
7763 if (FRAME_MESSAGE_BUF (f))
7764 {
7765 if (m)
7766 {
7767 int len;
7768 #ifdef NO_ARG_ARRAY
7769 char *a[3];
7770 a[0] = (char *) a1;
7771 a[1] = (char *) a2;
7772 a[2] = (char *) a3;
7773
7774 len = doprnt (FRAME_MESSAGE_BUF (f),
7775 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7776 #else
7777 len = doprnt (FRAME_MESSAGE_BUF (f),
7778 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7779 (char **) &a1);
7780 #endif /* NO_ARG_ARRAY */
7781
7782 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7783 }
7784 else
7785 message1 (0);
7786
7787 /* Print should start at the beginning of the message
7788 buffer next time. */
7789 message_buf_print = 0;
7790 }
7791 }
7792 }
7793
7794
7795 /* The non-logging version of message. */
7796
7797 void
7798 message_nolog (m, a1, a2, a3)
7799 char *m;
7800 EMACS_INT a1, a2, a3;
7801 {
7802 Lisp_Object old_log_max;
7803 old_log_max = Vmessage_log_max;
7804 Vmessage_log_max = Qnil;
7805 message (m, a1, a2, a3);
7806 Vmessage_log_max = old_log_max;
7807 }
7808
7809
7810 /* Display the current message in the current mini-buffer. This is
7811 only called from error handlers in process.c, and is not time
7812 critical. */
7813
7814 void
7815 update_echo_area ()
7816 {
7817 if (!NILP (echo_area_buffer[0]))
7818 {
7819 Lisp_Object string;
7820 string = Fcurrent_message ();
7821 message3 (string, SBYTES (string),
7822 !NILP (current_buffer->enable_multibyte_characters));
7823 }
7824 }
7825
7826
7827 /* Make sure echo area buffers in `echo_buffers' are live.
7828 If they aren't, make new ones. */
7829
7830 static void
7831 ensure_echo_area_buffers ()
7832 {
7833 int i;
7834
7835 for (i = 0; i < 2; ++i)
7836 if (!BUFFERP (echo_buffer[i])
7837 || NILP (XBUFFER (echo_buffer[i])->name))
7838 {
7839 char name[30];
7840 Lisp_Object old_buffer;
7841 int j;
7842
7843 old_buffer = echo_buffer[i];
7844 sprintf (name, " *Echo Area %d*", i);
7845 echo_buffer[i] = Fget_buffer_create (build_string (name));
7846 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7847
7848 for (j = 0; j < 2; ++j)
7849 if (EQ (old_buffer, echo_area_buffer[j]))
7850 echo_area_buffer[j] = echo_buffer[i];
7851 }
7852 }
7853
7854
7855 /* Call FN with args A1..A4 with either the current or last displayed
7856 echo_area_buffer as current buffer.
7857
7858 WHICH zero means use the current message buffer
7859 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7860 from echo_buffer[] and clear it.
7861
7862 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7863 suitable buffer from echo_buffer[] and clear it.
7864
7865 Value is what FN returns. */
7866
7867 static int
7868 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7869 struct window *w;
7870 int which;
7871 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7872 EMACS_INT a1;
7873 Lisp_Object a2;
7874 EMACS_INT a3, a4;
7875 {
7876 Lisp_Object buffer;
7877 int this_one, the_other, clear_buffer_p, rc;
7878 int count = SPECPDL_INDEX ();
7879
7880 /* If buffers aren't live, make new ones. */
7881 ensure_echo_area_buffers ();
7882
7883 clear_buffer_p = 0;
7884
7885 if (which == 0)
7886 this_one = 0, the_other = 1;
7887 else if (which > 0)
7888 this_one = 1, the_other = 0;
7889
7890 /* Choose a suitable buffer from echo_buffer[] is we don't
7891 have one. */
7892 if (NILP (echo_area_buffer[this_one]))
7893 {
7894 echo_area_buffer[this_one]
7895 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7896 ? echo_buffer[the_other]
7897 : echo_buffer[this_one]);
7898 clear_buffer_p = 1;
7899 }
7900
7901 buffer = echo_area_buffer[this_one];
7902
7903 /* Don't get confused by reusing the buffer used for echoing
7904 for a different purpose. */
7905 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7906 cancel_echoing ();
7907
7908 record_unwind_protect (unwind_with_echo_area_buffer,
7909 with_echo_area_buffer_unwind_data (w));
7910
7911 /* Make the echo area buffer current. Note that for display
7912 purposes, it is not necessary that the displayed window's buffer
7913 == current_buffer, except for text property lookup. So, let's
7914 only set that buffer temporarily here without doing a full
7915 Fset_window_buffer. We must also change w->pointm, though,
7916 because otherwise an assertions in unshow_buffer fails, and Emacs
7917 aborts. */
7918 set_buffer_internal_1 (XBUFFER (buffer));
7919 if (w)
7920 {
7921 w->buffer = buffer;
7922 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7923 }
7924
7925 current_buffer->undo_list = Qt;
7926 current_buffer->read_only = Qnil;
7927 specbind (Qinhibit_read_only, Qt);
7928 specbind (Qinhibit_modification_hooks, Qt);
7929
7930 if (clear_buffer_p && Z > BEG)
7931 del_range (BEG, Z);
7932
7933 xassert (BEGV >= BEG);
7934 xassert (ZV <= Z && ZV >= BEGV);
7935
7936 rc = fn (a1, a2, a3, a4);
7937
7938 xassert (BEGV >= BEG);
7939 xassert (ZV <= Z && ZV >= BEGV);
7940
7941 unbind_to (count, Qnil);
7942 return rc;
7943 }
7944
7945
7946 /* Save state that should be preserved around the call to the function
7947 FN called in with_echo_area_buffer. */
7948
7949 static Lisp_Object
7950 with_echo_area_buffer_unwind_data (w)
7951 struct window *w;
7952 {
7953 int i = 0;
7954 Lisp_Object vector;
7955
7956 /* Reduce consing by keeping one vector in
7957 Vwith_echo_area_save_vector. */
7958 vector = Vwith_echo_area_save_vector;
7959 Vwith_echo_area_save_vector = Qnil;
7960
7961 if (NILP (vector))
7962 vector = Fmake_vector (make_number (7), Qnil);
7963
7964 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7965 AREF (vector, i) = Vdeactivate_mark, ++i;
7966 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7967
7968 if (w)
7969 {
7970 XSETWINDOW (AREF (vector, i), w); ++i;
7971 AREF (vector, i) = w->buffer; ++i;
7972 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7973 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7974 }
7975 else
7976 {
7977 int end = i + 4;
7978 for (; i < end; ++i)
7979 AREF (vector, i) = Qnil;
7980 }
7981
7982 xassert (i == ASIZE (vector));
7983 return vector;
7984 }
7985
7986
7987 /* Restore global state from VECTOR which was created by
7988 with_echo_area_buffer_unwind_data. */
7989
7990 static Lisp_Object
7991 unwind_with_echo_area_buffer (vector)
7992 Lisp_Object vector;
7993 {
7994 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7995 Vdeactivate_mark = AREF (vector, 1);
7996 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7997
7998 if (WINDOWP (AREF (vector, 3)))
7999 {
8000 struct window *w;
8001 Lisp_Object buffer, charpos, bytepos;
8002
8003 w = XWINDOW (AREF (vector, 3));
8004 buffer = AREF (vector, 4);
8005 charpos = AREF (vector, 5);
8006 bytepos = AREF (vector, 6);
8007
8008 w->buffer = buffer;
8009 set_marker_both (w->pointm, buffer,
8010 XFASTINT (charpos), XFASTINT (bytepos));
8011 }
8012
8013 Vwith_echo_area_save_vector = vector;
8014 return Qnil;
8015 }
8016
8017
8018 /* Set up the echo area for use by print functions. MULTIBYTE_P
8019 non-zero means we will print multibyte. */
8020
8021 void
8022 setup_echo_area_for_printing (multibyte_p)
8023 int multibyte_p;
8024 {
8025 /* If we can't find an echo area any more, exit. */
8026 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8027 Fkill_emacs (Qnil);
8028
8029 ensure_echo_area_buffers ();
8030
8031 if (!message_buf_print)
8032 {
8033 /* A message has been output since the last time we printed.
8034 Choose a fresh echo area buffer. */
8035 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8036 echo_area_buffer[0] = echo_buffer[1];
8037 else
8038 echo_area_buffer[0] = echo_buffer[0];
8039
8040 /* Switch to that buffer and clear it. */
8041 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8042 current_buffer->truncate_lines = Qnil;
8043
8044 if (Z > BEG)
8045 {
8046 int count = SPECPDL_INDEX ();
8047 specbind (Qinhibit_read_only, Qt);
8048 /* Note that undo recording is always disabled. */
8049 del_range (BEG, Z);
8050 unbind_to (count, Qnil);
8051 }
8052 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8053
8054 /* Set up the buffer for the multibyteness we need. */
8055 if (multibyte_p
8056 != !NILP (current_buffer->enable_multibyte_characters))
8057 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8058
8059 /* Raise the frame containing the echo area. */
8060 if (minibuffer_auto_raise)
8061 {
8062 struct frame *sf = SELECTED_FRAME ();
8063 Lisp_Object mini_window;
8064 mini_window = FRAME_MINIBUF_WINDOW (sf);
8065 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8066 }
8067
8068 message_log_maybe_newline ();
8069 message_buf_print = 1;
8070 }
8071 else
8072 {
8073 if (NILP (echo_area_buffer[0]))
8074 {
8075 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8076 echo_area_buffer[0] = echo_buffer[1];
8077 else
8078 echo_area_buffer[0] = echo_buffer[0];
8079 }
8080
8081 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8082 {
8083 /* Someone switched buffers between print requests. */
8084 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8085 current_buffer->truncate_lines = Qnil;
8086 }
8087 }
8088 }
8089
8090
8091 /* Display an echo area message in window W. Value is non-zero if W's
8092 height is changed. If display_last_displayed_message_p is
8093 non-zero, display the message that was last displayed, otherwise
8094 display the current message. */
8095
8096 static int
8097 display_echo_area (w)
8098 struct window *w;
8099 {
8100 int i, no_message_p, window_height_changed_p, count;
8101
8102 /* Temporarily disable garbage collections while displaying the echo
8103 area. This is done because a GC can print a message itself.
8104 That message would modify the echo area buffer's contents while a
8105 redisplay of the buffer is going on, and seriously confuse
8106 redisplay. */
8107 count = inhibit_garbage_collection ();
8108
8109 /* If there is no message, we must call display_echo_area_1
8110 nevertheless because it resizes the window. But we will have to
8111 reset the echo_area_buffer in question to nil at the end because
8112 with_echo_area_buffer will sets it to an empty buffer. */
8113 i = display_last_displayed_message_p ? 1 : 0;
8114 no_message_p = NILP (echo_area_buffer[i]);
8115
8116 window_height_changed_p
8117 = with_echo_area_buffer (w, display_last_displayed_message_p,
8118 display_echo_area_1,
8119 (EMACS_INT) w, Qnil, 0, 0);
8120
8121 if (no_message_p)
8122 echo_area_buffer[i] = Qnil;
8123
8124 unbind_to (count, Qnil);
8125 return window_height_changed_p;
8126 }
8127
8128
8129 /* Helper for display_echo_area. Display the current buffer which
8130 contains the current echo area message in window W, a mini-window,
8131 a pointer to which is passed in A1. A2..A4 are currently not used.
8132 Change the height of W so that all of the message is displayed.
8133 Value is non-zero if height of W was changed. */
8134
8135 static int
8136 display_echo_area_1 (a1, a2, a3, a4)
8137 EMACS_INT a1;
8138 Lisp_Object a2;
8139 EMACS_INT a3, a4;
8140 {
8141 struct window *w = (struct window *) a1;
8142 Lisp_Object window;
8143 struct text_pos start;
8144 int window_height_changed_p = 0;
8145
8146 /* Do this before displaying, so that we have a large enough glyph
8147 matrix for the display. If we can't get enough space for the
8148 whole text, display the last N lines. That works by setting w->start. */
8149 window_height_changed_p = resize_mini_window (w, 0);
8150
8151 /* Use the starting position chosen by resize_mini_window. */
8152 SET_TEXT_POS_FROM_MARKER (start, w->start);
8153
8154 /* Display. */
8155 clear_glyph_matrix (w->desired_matrix);
8156 XSETWINDOW (window, w);
8157 try_window (window, start, 0);
8158
8159 return window_height_changed_p;
8160 }
8161
8162
8163 /* Resize the echo area window to exactly the size needed for the
8164 currently displayed message, if there is one. If a mini-buffer
8165 is active, don't shrink it. */
8166
8167 void
8168 resize_echo_area_exactly ()
8169 {
8170 if (BUFFERP (echo_area_buffer[0])
8171 && WINDOWP (echo_area_window))
8172 {
8173 struct window *w = XWINDOW (echo_area_window);
8174 int resized_p;
8175 Lisp_Object resize_exactly;
8176
8177 if (minibuf_level == 0)
8178 resize_exactly = Qt;
8179 else
8180 resize_exactly = Qnil;
8181
8182 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8183 (EMACS_INT) w, resize_exactly, 0, 0);
8184 if (resized_p)
8185 {
8186 ++windows_or_buffers_changed;
8187 ++update_mode_lines;
8188 redisplay_internal (0);
8189 }
8190 }
8191 }
8192
8193
8194 /* Callback function for with_echo_area_buffer, when used from
8195 resize_echo_area_exactly. A1 contains a pointer to the window to
8196 resize, EXACTLY non-nil means resize the mini-window exactly to the
8197 size of the text displayed. A3 and A4 are not used. Value is what
8198 resize_mini_window returns. */
8199
8200 static int
8201 resize_mini_window_1 (a1, exactly, a3, a4)
8202 EMACS_INT a1;
8203 Lisp_Object exactly;
8204 EMACS_INT a3, a4;
8205 {
8206 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8207 }
8208
8209
8210 /* Resize mini-window W to fit the size of its contents. EXACT:P
8211 means size the window exactly to the size needed. Otherwise, it's
8212 only enlarged until W's buffer is empty.
8213
8214 Set W->start to the right place to begin display. If the whole
8215 contents fit, start at the beginning. Otherwise, start so as
8216 to make the end of the contents appear. This is particularly
8217 important for y-or-n-p, but seems desirable generally.
8218
8219 Value is non-zero if the window height has been changed. */
8220
8221 int
8222 resize_mini_window (w, exact_p)
8223 struct window *w;
8224 int exact_p;
8225 {
8226 struct frame *f = XFRAME (w->frame);
8227 int window_height_changed_p = 0;
8228
8229 xassert (MINI_WINDOW_P (w));
8230
8231 /* By default, start display at the beginning. */
8232 set_marker_both (w->start, w->buffer,
8233 BUF_BEGV (XBUFFER (w->buffer)),
8234 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8235
8236 /* Don't resize windows while redisplaying a window; it would
8237 confuse redisplay functions when the size of the window they are
8238 displaying changes from under them. Such a resizing can happen,
8239 for instance, when which-func prints a long message while
8240 we are running fontification-functions. We're running these
8241 functions with safe_call which binds inhibit-redisplay to t. */
8242 if (!NILP (Vinhibit_redisplay))
8243 return 0;
8244
8245 /* Nil means don't try to resize. */
8246 if (NILP (Vresize_mini_windows)
8247 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8248 return 0;
8249
8250 if (!FRAME_MINIBUF_ONLY_P (f))
8251 {
8252 struct it it;
8253 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8254 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8255 int height, max_height;
8256 int unit = FRAME_LINE_HEIGHT (f);
8257 struct text_pos start;
8258 struct buffer *old_current_buffer = NULL;
8259
8260 if (current_buffer != XBUFFER (w->buffer))
8261 {
8262 old_current_buffer = current_buffer;
8263 set_buffer_internal (XBUFFER (w->buffer));
8264 }
8265
8266 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8267
8268 /* Compute the max. number of lines specified by the user. */
8269 if (FLOATP (Vmax_mini_window_height))
8270 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8271 else if (INTEGERP (Vmax_mini_window_height))
8272 max_height = XINT (Vmax_mini_window_height);
8273 else
8274 max_height = total_height / 4;
8275
8276 /* Correct that max. height if it's bogus. */
8277 max_height = max (1, max_height);
8278 max_height = min (total_height, max_height);
8279
8280 /* Find out the height of the text in the window. */
8281 if (it.truncate_lines_p)
8282 height = 1;
8283 else
8284 {
8285 last_height = 0;
8286 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8287 if (it.max_ascent == 0 && it.max_descent == 0)
8288 height = it.current_y + last_height;
8289 else
8290 height = it.current_y + it.max_ascent + it.max_descent;
8291 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8292 height = (height + unit - 1) / unit;
8293 }
8294
8295 /* Compute a suitable window start. */
8296 if (height > max_height)
8297 {
8298 height = max_height;
8299 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8300 move_it_vertically_backward (&it, (height - 1) * unit);
8301 start = it.current.pos;
8302 }
8303 else
8304 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8305 SET_MARKER_FROM_TEXT_POS (w->start, start);
8306
8307 if (EQ (Vresize_mini_windows, Qgrow_only))
8308 {
8309 /* Let it grow only, until we display an empty message, in which
8310 case the window shrinks again. */
8311 if (height > WINDOW_TOTAL_LINES (w))
8312 {
8313 int old_height = WINDOW_TOTAL_LINES (w);
8314 freeze_window_starts (f, 1);
8315 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8316 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8317 }
8318 else if (height < WINDOW_TOTAL_LINES (w)
8319 && (exact_p || BEGV == ZV))
8320 {
8321 int old_height = WINDOW_TOTAL_LINES (w);
8322 freeze_window_starts (f, 0);
8323 shrink_mini_window (w);
8324 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8325 }
8326 }
8327 else
8328 {
8329 /* Always resize to exact size needed. */
8330 if (height > WINDOW_TOTAL_LINES (w))
8331 {
8332 int old_height = WINDOW_TOTAL_LINES (w);
8333 freeze_window_starts (f, 1);
8334 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8335 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8336 }
8337 else if (height < WINDOW_TOTAL_LINES (w))
8338 {
8339 int old_height = WINDOW_TOTAL_LINES (w);
8340 freeze_window_starts (f, 0);
8341 shrink_mini_window (w);
8342
8343 if (height)
8344 {
8345 freeze_window_starts (f, 1);
8346 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8347 }
8348
8349 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8350 }
8351 }
8352
8353 if (old_current_buffer)
8354 set_buffer_internal (old_current_buffer);
8355 }
8356
8357 return window_height_changed_p;
8358 }
8359
8360
8361 /* Value is the current message, a string, or nil if there is no
8362 current message. */
8363
8364 Lisp_Object
8365 current_message ()
8366 {
8367 Lisp_Object msg;
8368
8369 if (NILP (echo_area_buffer[0]))
8370 msg = Qnil;
8371 else
8372 {
8373 with_echo_area_buffer (0, 0, current_message_1,
8374 (EMACS_INT) &msg, Qnil, 0, 0);
8375 if (NILP (msg))
8376 echo_area_buffer[0] = Qnil;
8377 }
8378
8379 return msg;
8380 }
8381
8382
8383 static int
8384 current_message_1 (a1, a2, a3, a4)
8385 EMACS_INT a1;
8386 Lisp_Object a2;
8387 EMACS_INT a3, a4;
8388 {
8389 Lisp_Object *msg = (Lisp_Object *) a1;
8390
8391 if (Z > BEG)
8392 *msg = make_buffer_string (BEG, Z, 1);
8393 else
8394 *msg = Qnil;
8395 return 0;
8396 }
8397
8398
8399 /* Push the current message on Vmessage_stack for later restauration
8400 by restore_message. Value is non-zero if the current message isn't
8401 empty. This is a relatively infrequent operation, so it's not
8402 worth optimizing. */
8403
8404 int
8405 push_message ()
8406 {
8407 Lisp_Object msg;
8408 msg = current_message ();
8409 Vmessage_stack = Fcons (msg, Vmessage_stack);
8410 return STRINGP (msg);
8411 }
8412
8413
8414 /* Restore message display from the top of Vmessage_stack. */
8415
8416 void
8417 restore_message ()
8418 {
8419 Lisp_Object msg;
8420
8421 xassert (CONSP (Vmessage_stack));
8422 msg = XCAR (Vmessage_stack);
8423 if (STRINGP (msg))
8424 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8425 else
8426 message3_nolog (msg, 0, 0);
8427 }
8428
8429
8430 /* Handler for record_unwind_protect calling pop_message. */
8431
8432 Lisp_Object
8433 pop_message_unwind (dummy)
8434 Lisp_Object dummy;
8435 {
8436 pop_message ();
8437 return Qnil;
8438 }
8439
8440 /* Pop the top-most entry off Vmessage_stack. */
8441
8442 void
8443 pop_message ()
8444 {
8445 xassert (CONSP (Vmessage_stack));
8446 Vmessage_stack = XCDR (Vmessage_stack);
8447 }
8448
8449
8450 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8451 exits. If the stack is not empty, we have a missing pop_message
8452 somewhere. */
8453
8454 void
8455 check_message_stack ()
8456 {
8457 if (!NILP (Vmessage_stack))
8458 abort ();
8459 }
8460
8461
8462 /* Truncate to NCHARS what will be displayed in the echo area the next
8463 time we display it---but don't redisplay it now. */
8464
8465 void
8466 truncate_echo_area (nchars)
8467 int nchars;
8468 {
8469 if (nchars == 0)
8470 echo_area_buffer[0] = Qnil;
8471 /* A null message buffer means that the frame hasn't really been
8472 initialized yet. Error messages get reported properly by
8473 cmd_error, so this must be just an informative message; toss it. */
8474 else if (!noninteractive
8475 && INTERACTIVE
8476 && !NILP (echo_area_buffer[0]))
8477 {
8478 struct frame *sf = SELECTED_FRAME ();
8479 if (FRAME_MESSAGE_BUF (sf))
8480 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8481 }
8482 }
8483
8484
8485 /* Helper function for truncate_echo_area. Truncate the current
8486 message to at most NCHARS characters. */
8487
8488 static int
8489 truncate_message_1 (nchars, a2, a3, a4)
8490 EMACS_INT nchars;
8491 Lisp_Object a2;
8492 EMACS_INT a3, a4;
8493 {
8494 if (BEG + nchars < Z)
8495 del_range (BEG + nchars, Z);
8496 if (Z == BEG)
8497 echo_area_buffer[0] = Qnil;
8498 return 0;
8499 }
8500
8501
8502 /* Set the current message to a substring of S or STRING.
8503
8504 If STRING is a Lisp string, set the message to the first NBYTES
8505 bytes from STRING. NBYTES zero means use the whole string. If
8506 STRING is multibyte, the message will be displayed multibyte.
8507
8508 If S is not null, set the message to the first LEN bytes of S. LEN
8509 zero means use the whole string. MULTIBYTE_P non-zero means S is
8510 multibyte. Display the message multibyte in that case.
8511
8512 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8513 to t before calling set_message_1 (which calls insert).
8514 */
8515
8516 void
8517 set_message (s, string, nbytes, multibyte_p)
8518 const char *s;
8519 Lisp_Object string;
8520 int nbytes, multibyte_p;
8521 {
8522 message_enable_multibyte
8523 = ((s && multibyte_p)
8524 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8525
8526 with_echo_area_buffer (0, 0, set_message_1,
8527 (EMACS_INT) s, string, nbytes, multibyte_p);
8528 message_buf_print = 0;
8529 help_echo_showing_p = 0;
8530 }
8531
8532
8533 /* Helper function for set_message. Arguments have the same meaning
8534 as there, with A1 corresponding to S and A2 corresponding to STRING
8535 This function is called with the echo area buffer being
8536 current. */
8537
8538 static int
8539 set_message_1 (a1, a2, nbytes, multibyte_p)
8540 EMACS_INT a1;
8541 Lisp_Object a2;
8542 EMACS_INT nbytes, multibyte_p;
8543 {
8544 const char *s = (const char *) a1;
8545 Lisp_Object string = a2;
8546
8547 /* Change multibyteness of the echo buffer appropriately. */
8548 if (message_enable_multibyte
8549 != !NILP (current_buffer->enable_multibyte_characters))
8550 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8551
8552 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8553
8554 /* Insert new message at BEG. */
8555 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8556 Ferase_buffer ();
8557
8558 if (STRINGP (string))
8559 {
8560 int nchars;
8561
8562 if (nbytes == 0)
8563 nbytes = SBYTES (string);
8564 nchars = string_byte_to_char (string, nbytes);
8565
8566 /* This function takes care of single/multibyte conversion. We
8567 just have to ensure that the echo area buffer has the right
8568 setting of enable_multibyte_characters. */
8569 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8570 }
8571 else if (s)
8572 {
8573 if (nbytes == 0)
8574 nbytes = strlen (s);
8575
8576 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8577 {
8578 /* Convert from multi-byte to single-byte. */
8579 int i, c, n;
8580 unsigned char work[1];
8581
8582 /* Convert a multibyte string to single-byte. */
8583 for (i = 0; i < nbytes; i += n)
8584 {
8585 c = string_char_and_length (s + i, nbytes - i, &n);
8586 work[0] = (ASCII_CHAR_P (c)
8587 ? c
8588 : multibyte_char_to_unibyte (c, Qnil));
8589 insert_1_both (work, 1, 1, 1, 0, 0);
8590 }
8591 }
8592 else if (!multibyte_p
8593 && !NILP (current_buffer->enable_multibyte_characters))
8594 {
8595 /* Convert from single-byte to multi-byte. */
8596 int i, c, n;
8597 const unsigned char *msg = (const unsigned char *) s;
8598 unsigned char str[MAX_MULTIBYTE_LENGTH];
8599
8600 /* Convert a single-byte string to multibyte. */
8601 for (i = 0; i < nbytes; i++)
8602 {
8603 c = msg[i];
8604 c = unibyte_char_to_multibyte (c);
8605 n = CHAR_STRING (c, str);
8606 insert_1_both (str, 1, n, 1, 0, 0);
8607 }
8608 }
8609 else
8610 insert_1 (s, nbytes, 1, 0, 0);
8611 }
8612
8613 return 0;
8614 }
8615
8616
8617 /* Clear messages. CURRENT_P non-zero means clear the current
8618 message. LAST_DISPLAYED_P non-zero means clear the message
8619 last displayed. */
8620
8621 void
8622 clear_message (current_p, last_displayed_p)
8623 int current_p, last_displayed_p;
8624 {
8625 if (current_p)
8626 {
8627 echo_area_buffer[0] = Qnil;
8628 message_cleared_p = 1;
8629 }
8630
8631 if (last_displayed_p)
8632 echo_area_buffer[1] = Qnil;
8633
8634 message_buf_print = 0;
8635 }
8636
8637 /* Clear garbaged frames.
8638
8639 This function is used where the old redisplay called
8640 redraw_garbaged_frames which in turn called redraw_frame which in
8641 turn called clear_frame. The call to clear_frame was a source of
8642 flickering. I believe a clear_frame is not necessary. It should
8643 suffice in the new redisplay to invalidate all current matrices,
8644 and ensure a complete redisplay of all windows. */
8645
8646 static void
8647 clear_garbaged_frames ()
8648 {
8649 if (frame_garbaged)
8650 {
8651 Lisp_Object tail, frame;
8652 int changed_count = 0;
8653
8654 FOR_EACH_FRAME (tail, frame)
8655 {
8656 struct frame *f = XFRAME (frame);
8657
8658 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8659 {
8660 if (f->resized_p)
8661 {
8662 Fredraw_frame (frame);
8663 f->force_flush_display_p = 1;
8664 }
8665 clear_current_matrices (f);
8666 changed_count++;
8667 f->garbaged = 0;
8668 f->resized_p = 0;
8669 }
8670 }
8671
8672 frame_garbaged = 0;
8673 if (changed_count)
8674 ++windows_or_buffers_changed;
8675 }
8676 }
8677
8678
8679 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8680 is non-zero update selected_frame. Value is non-zero if the
8681 mini-windows height has been changed. */
8682
8683 static int
8684 echo_area_display (update_frame_p)
8685 int update_frame_p;
8686 {
8687 Lisp_Object mini_window;
8688 struct window *w;
8689 struct frame *f;
8690 int window_height_changed_p = 0;
8691 struct frame *sf = SELECTED_FRAME ();
8692
8693 mini_window = FRAME_MINIBUF_WINDOW (sf);
8694 w = XWINDOW (mini_window);
8695 f = XFRAME (WINDOW_FRAME (w));
8696
8697 /* Don't display if frame is invisible or not yet initialized. */
8698 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8699 return 0;
8700
8701 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8702 #ifndef MAC_OS8
8703 #ifdef HAVE_WINDOW_SYSTEM
8704 /* When Emacs starts, selected_frame may be a visible terminal
8705 frame, even if we run under a window system. If we let this
8706 through, a message would be displayed on the terminal. */
8707 if (EQ (selected_frame, Vterminal_frame)
8708 && !NILP (Vwindow_system))
8709 return 0;
8710 #endif /* HAVE_WINDOW_SYSTEM */
8711 #endif
8712
8713 /* Redraw garbaged frames. */
8714 if (frame_garbaged)
8715 clear_garbaged_frames ();
8716
8717 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8718 {
8719 echo_area_window = mini_window;
8720 window_height_changed_p = display_echo_area (w);
8721 w->must_be_updated_p = 1;
8722
8723 /* Update the display, unless called from redisplay_internal.
8724 Also don't update the screen during redisplay itself. The
8725 update will happen at the end of redisplay, and an update
8726 here could cause confusion. */
8727 if (update_frame_p && !redisplaying_p)
8728 {
8729 int n = 0;
8730
8731 /* If the display update has been interrupted by pending
8732 input, update mode lines in the frame. Due to the
8733 pending input, it might have been that redisplay hasn't
8734 been called, so that mode lines above the echo area are
8735 garbaged. This looks odd, so we prevent it here. */
8736 if (!display_completed)
8737 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8738
8739 if (window_height_changed_p
8740 /* Don't do this if Emacs is shutting down. Redisplay
8741 needs to run hooks. */
8742 && !NILP (Vrun_hooks))
8743 {
8744 /* Must update other windows. Likewise as in other
8745 cases, don't let this update be interrupted by
8746 pending input. */
8747 int count = SPECPDL_INDEX ();
8748 specbind (Qredisplay_dont_pause, Qt);
8749 windows_or_buffers_changed = 1;
8750 redisplay_internal (0);
8751 unbind_to (count, Qnil);
8752 }
8753 else if (FRAME_WINDOW_P (f) && n == 0)
8754 {
8755 /* Window configuration is the same as before.
8756 Can do with a display update of the echo area,
8757 unless we displayed some mode lines. */
8758 update_single_window (w, 1);
8759 rif->flush_display (f);
8760 }
8761 else
8762 update_frame (f, 1, 1);
8763
8764 /* If cursor is in the echo area, make sure that the next
8765 redisplay displays the minibuffer, so that the cursor will
8766 be replaced with what the minibuffer wants. */
8767 if (cursor_in_echo_area)
8768 ++windows_or_buffers_changed;
8769 }
8770 }
8771 else if (!EQ (mini_window, selected_window))
8772 windows_or_buffers_changed++;
8773
8774 /* The current message is now also the last one displayed. */
8775 echo_area_buffer[1] = echo_area_buffer[0];
8776
8777 /* Prevent redisplay optimization in redisplay_internal by resetting
8778 this_line_start_pos. This is done because the mini-buffer now
8779 displays the message instead of its buffer text. */
8780 if (EQ (mini_window, selected_window))
8781 CHARPOS (this_line_start_pos) = 0;
8782
8783 return window_height_changed_p;
8784 }
8785
8786
8787 \f
8788 /***********************************************************************
8789 Mode Lines and Frame Titles
8790 ***********************************************************************/
8791
8792 /* A buffer for constructing non-propertized mode-line strings and
8793 frame titles in it; allocated from the heap in init_xdisp and
8794 resized as needed in store_mode_line_noprop_char. */
8795
8796 static char *mode_line_noprop_buf;
8797
8798 /* The buffer's end, and a current output position in it. */
8799
8800 static char *mode_line_noprop_buf_end;
8801 static char *mode_line_noprop_ptr;
8802
8803 #define MODE_LINE_NOPROP_LEN(start) \
8804 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8805
8806 static enum {
8807 MODE_LINE_DISPLAY = 0,
8808 MODE_LINE_TITLE,
8809 MODE_LINE_NOPROP,
8810 MODE_LINE_STRING
8811 } mode_line_target;
8812
8813 /* Alist that caches the results of :propertize.
8814 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8815 static Lisp_Object mode_line_proptrans_alist;
8816
8817 /* List of strings making up the mode-line. */
8818 static Lisp_Object mode_line_string_list;
8819
8820 /* Base face property when building propertized mode line string. */
8821 static Lisp_Object mode_line_string_face;
8822 static Lisp_Object mode_line_string_face_prop;
8823
8824
8825 /* Unwind data for mode line strings */
8826
8827 static Lisp_Object Vmode_line_unwind_vector;
8828
8829 static Lisp_Object
8830 format_mode_line_unwind_data (obuf, save_proptrans)
8831 struct buffer *obuf;
8832 {
8833 Lisp_Object vector;
8834
8835 /* Reduce consing by keeping one vector in
8836 Vwith_echo_area_save_vector. */
8837 vector = Vmode_line_unwind_vector;
8838 Vmode_line_unwind_vector = Qnil;
8839
8840 if (NILP (vector))
8841 vector = Fmake_vector (make_number (7), Qnil);
8842
8843 AREF (vector, 0) = make_number (mode_line_target);
8844 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8845 AREF (vector, 2) = mode_line_string_list;
8846 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8847 AREF (vector, 4) = mode_line_string_face;
8848 AREF (vector, 5) = mode_line_string_face_prop;
8849
8850 if (obuf)
8851 XSETBUFFER (AREF (vector, 6), obuf);
8852 else
8853 AREF (vector, 6) = Qnil;
8854
8855 return vector;
8856 }
8857
8858 static Lisp_Object
8859 unwind_format_mode_line (vector)
8860 Lisp_Object vector;
8861 {
8862 mode_line_target = XINT (AREF (vector, 0));
8863 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8864 mode_line_string_list = AREF (vector, 2);
8865 if (! EQ (AREF (vector, 3), Qt))
8866 mode_line_proptrans_alist = AREF (vector, 3);
8867 mode_line_string_face = AREF (vector, 4);
8868 mode_line_string_face_prop = AREF (vector, 5);
8869
8870 if (!NILP (AREF (vector, 6)))
8871 {
8872 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8873 AREF (vector, 6) = Qnil;
8874 }
8875
8876 Vmode_line_unwind_vector = vector;
8877 return Qnil;
8878 }
8879
8880
8881 /* Store a single character C for the frame title in mode_line_noprop_buf.
8882 Re-allocate mode_line_noprop_buf if necessary. */
8883
8884 static void
8885 #ifdef PROTOTYPES
8886 store_mode_line_noprop_char (char c)
8887 #else
8888 store_mode_line_noprop_char (c)
8889 char c;
8890 #endif
8891 {
8892 /* If output position has reached the end of the allocated buffer,
8893 double the buffer's size. */
8894 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8895 {
8896 int len = MODE_LINE_NOPROP_LEN (0);
8897 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8898 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8899 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8900 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8901 }
8902
8903 *mode_line_noprop_ptr++ = c;
8904 }
8905
8906
8907 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8908 mode_line_noprop_ptr. STR is the string to store. Do not copy
8909 characters that yield more columns than PRECISION; PRECISION <= 0
8910 means copy the whole string. Pad with spaces until FIELD_WIDTH
8911 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8912 pad. Called from display_mode_element when it is used to build a
8913 frame title. */
8914
8915 static int
8916 store_mode_line_noprop (str, field_width, precision)
8917 const unsigned char *str;
8918 int field_width, precision;
8919 {
8920 int n = 0;
8921 int dummy, nbytes;
8922
8923 /* Copy at most PRECISION chars from STR. */
8924 nbytes = strlen (str);
8925 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8926 while (nbytes--)
8927 store_mode_line_noprop_char (*str++);
8928
8929 /* Fill up with spaces until FIELD_WIDTH reached. */
8930 while (field_width > 0
8931 && n < field_width)
8932 {
8933 store_mode_line_noprop_char (' ');
8934 ++n;
8935 }
8936
8937 return n;
8938 }
8939
8940 /***********************************************************************
8941 Frame Titles
8942 ***********************************************************************/
8943
8944 #ifdef HAVE_WINDOW_SYSTEM
8945
8946 /* Set the title of FRAME, if it has changed. The title format is
8947 Vicon_title_format if FRAME is iconified, otherwise it is
8948 frame_title_format. */
8949
8950 static void
8951 x_consider_frame_title (frame)
8952 Lisp_Object frame;
8953 {
8954 struct frame *f = XFRAME (frame);
8955
8956 if (FRAME_WINDOW_P (f)
8957 || FRAME_MINIBUF_ONLY_P (f)
8958 || f->explicit_name)
8959 {
8960 /* Do we have more than one visible frame on this X display? */
8961 Lisp_Object tail;
8962 Lisp_Object fmt;
8963 int title_start;
8964 char *title;
8965 int len;
8966 struct it it;
8967 int count = SPECPDL_INDEX ();
8968
8969 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8970 {
8971 Lisp_Object other_frame = XCAR (tail);
8972 struct frame *tf = XFRAME (other_frame);
8973
8974 if (tf != f
8975 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8976 && !FRAME_MINIBUF_ONLY_P (tf)
8977 && !EQ (other_frame, tip_frame)
8978 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8979 break;
8980 }
8981
8982 /* Set global variable indicating that multiple frames exist. */
8983 multiple_frames = CONSP (tail);
8984
8985 /* Switch to the buffer of selected window of the frame. Set up
8986 mode_line_target so that display_mode_element will output into
8987 mode_line_noprop_buf; then display the title. */
8988 record_unwind_protect (unwind_format_mode_line,
8989 format_mode_line_unwind_data (current_buffer, 0));
8990
8991 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8992 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8993
8994 mode_line_target = MODE_LINE_TITLE;
8995 title_start = MODE_LINE_NOPROP_LEN (0);
8996 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8997 NULL, DEFAULT_FACE_ID);
8998 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8999 len = MODE_LINE_NOPROP_LEN (title_start);
9000 title = mode_line_noprop_buf + title_start;
9001 unbind_to (count, Qnil);
9002
9003 /* Set the title only if it's changed. This avoids consing in
9004 the common case where it hasn't. (If it turns out that we've
9005 already wasted too much time by walking through the list with
9006 display_mode_element, then we might need to optimize at a
9007 higher level than this.) */
9008 if (! STRINGP (f->name)
9009 || SBYTES (f->name) != len
9010 || bcmp (title, SDATA (f->name), len) != 0)
9011 x_implicitly_set_name (f, make_string (title, len), Qnil);
9012 }
9013 }
9014
9015 #endif /* not HAVE_WINDOW_SYSTEM */
9016
9017
9018
9019 \f
9020 /***********************************************************************
9021 Menu Bars
9022 ***********************************************************************/
9023
9024
9025 /* Prepare for redisplay by updating menu-bar item lists when
9026 appropriate. This can call eval. */
9027
9028 void
9029 prepare_menu_bars ()
9030 {
9031 int all_windows;
9032 struct gcpro gcpro1, gcpro2;
9033 struct frame *f;
9034 Lisp_Object tooltip_frame;
9035
9036 #ifdef HAVE_WINDOW_SYSTEM
9037 tooltip_frame = tip_frame;
9038 #else
9039 tooltip_frame = Qnil;
9040 #endif
9041
9042 /* Update all frame titles based on their buffer names, etc. We do
9043 this before the menu bars so that the buffer-menu will show the
9044 up-to-date frame titles. */
9045 #ifdef HAVE_WINDOW_SYSTEM
9046 if (windows_or_buffers_changed || update_mode_lines)
9047 {
9048 Lisp_Object tail, frame;
9049
9050 FOR_EACH_FRAME (tail, frame)
9051 {
9052 f = XFRAME (frame);
9053 if (!EQ (frame, tooltip_frame)
9054 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9055 x_consider_frame_title (frame);
9056 }
9057 }
9058 #endif /* HAVE_WINDOW_SYSTEM */
9059
9060 /* Update the menu bar item lists, if appropriate. This has to be
9061 done before any actual redisplay or generation of display lines. */
9062 all_windows = (update_mode_lines
9063 || buffer_shared > 1
9064 || windows_or_buffers_changed);
9065 if (all_windows)
9066 {
9067 Lisp_Object tail, frame;
9068 int count = SPECPDL_INDEX ();
9069
9070 record_unwind_save_match_data ();
9071
9072 FOR_EACH_FRAME (tail, frame)
9073 {
9074 f = XFRAME (frame);
9075
9076 /* Ignore tooltip frame. */
9077 if (EQ (frame, tooltip_frame))
9078 continue;
9079
9080 /* If a window on this frame changed size, report that to
9081 the user and clear the size-change flag. */
9082 if (FRAME_WINDOW_SIZES_CHANGED (f))
9083 {
9084 Lisp_Object functions;
9085
9086 /* Clear flag first in case we get an error below. */
9087 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9088 functions = Vwindow_size_change_functions;
9089 GCPRO2 (tail, functions);
9090
9091 while (CONSP (functions))
9092 {
9093 call1 (XCAR (functions), frame);
9094 functions = XCDR (functions);
9095 }
9096 UNGCPRO;
9097 }
9098
9099 GCPRO1 (tail);
9100 update_menu_bar (f, 0);
9101 #ifdef HAVE_WINDOW_SYSTEM
9102 update_tool_bar (f, 0);
9103 #ifdef MAC_OS
9104 mac_update_title_bar (f, 0);
9105 #endif
9106 #endif
9107 UNGCPRO;
9108 }
9109
9110 unbind_to (count, Qnil);
9111 }
9112 else
9113 {
9114 struct frame *sf = SELECTED_FRAME ();
9115 update_menu_bar (sf, 1);
9116 #ifdef HAVE_WINDOW_SYSTEM
9117 update_tool_bar (sf, 1);
9118 #ifdef MAC_OS
9119 mac_update_title_bar (sf, 1);
9120 #endif
9121 #endif
9122 }
9123
9124 /* Motif needs this. See comment in xmenu.c. Turn it off when
9125 pending_menu_activation is not defined. */
9126 #ifdef USE_X_TOOLKIT
9127 pending_menu_activation = 0;
9128 #endif
9129 }
9130
9131
9132 /* Update the menu bar item list for frame F. This has to be done
9133 before we start to fill in any display lines, because it can call
9134 eval.
9135
9136 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
9137
9138 static void
9139 update_menu_bar (f, save_match_data)
9140 struct frame *f;
9141 int save_match_data;
9142 {
9143 Lisp_Object window;
9144 register struct window *w;
9145
9146 /* If called recursively during a menu update, do nothing. This can
9147 happen when, for instance, an activate-menubar-hook causes a
9148 redisplay. */
9149 if (inhibit_menubar_update)
9150 return;
9151
9152 window = FRAME_SELECTED_WINDOW (f);
9153 w = XWINDOW (window);
9154
9155 #if 0 /* The if statement below this if statement used to include the
9156 condition !NILP (w->update_mode_line), rather than using
9157 update_mode_lines directly, and this if statement may have
9158 been added to make that condition work. Now the if
9159 statement below matches its comment, this isn't needed. */
9160 if (update_mode_lines)
9161 w->update_mode_line = Qt;
9162 #endif
9163
9164 if (FRAME_WINDOW_P (f)
9165 ?
9166 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9167 || defined (USE_GTK)
9168 FRAME_EXTERNAL_MENU_BAR (f)
9169 #else
9170 FRAME_MENU_BAR_LINES (f) > 0
9171 #endif
9172 : FRAME_MENU_BAR_LINES (f) > 0)
9173 {
9174 /* If the user has switched buffers or windows, we need to
9175 recompute to reflect the new bindings. But we'll
9176 recompute when update_mode_lines is set too; that means
9177 that people can use force-mode-line-update to request
9178 that the menu bar be recomputed. The adverse effect on
9179 the rest of the redisplay algorithm is about the same as
9180 windows_or_buffers_changed anyway. */
9181 if (windows_or_buffers_changed
9182 /* This used to test w->update_mode_line, but we believe
9183 there is no need to recompute the menu in that case. */
9184 || update_mode_lines
9185 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9186 < BUF_MODIFF (XBUFFER (w->buffer)))
9187 != !NILP (w->last_had_star))
9188 || ((!NILP (Vtransient_mark_mode)
9189 && !NILP (XBUFFER (w->buffer)->mark_active))
9190 != !NILP (w->region_showing)))
9191 {
9192 struct buffer *prev = current_buffer;
9193 int count = SPECPDL_INDEX ();
9194
9195 specbind (Qinhibit_menubar_update, Qt);
9196
9197 set_buffer_internal_1 (XBUFFER (w->buffer));
9198 if (save_match_data)
9199 record_unwind_save_match_data ();
9200 if (NILP (Voverriding_local_map_menu_flag))
9201 {
9202 specbind (Qoverriding_terminal_local_map, Qnil);
9203 specbind (Qoverriding_local_map, Qnil);
9204 }
9205
9206 /* Run the Lucid hook. */
9207 safe_run_hooks (Qactivate_menubar_hook);
9208
9209 /* If it has changed current-menubar from previous value,
9210 really recompute the menu-bar from the value. */
9211 if (! NILP (Vlucid_menu_bar_dirty_flag))
9212 call0 (Qrecompute_lucid_menubar);
9213
9214 safe_run_hooks (Qmenu_bar_update_hook);
9215 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9216
9217 /* Redisplay the menu bar in case we changed it. */
9218 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9219 || defined (USE_GTK)
9220 if (FRAME_WINDOW_P (f))
9221 {
9222 #ifdef MAC_OS
9223 /* All frames on Mac OS share the same menubar. So only
9224 the selected frame should be allowed to set it. */
9225 if (f == SELECTED_FRAME ())
9226 #endif
9227 set_frame_menubar (f, 0, 0);
9228 }
9229 else
9230 /* On a terminal screen, the menu bar is an ordinary screen
9231 line, and this makes it get updated. */
9232 w->update_mode_line = Qt;
9233 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9234 /* In the non-toolkit version, the menu bar is an ordinary screen
9235 line, and this makes it get updated. */
9236 w->update_mode_line = Qt;
9237 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9238
9239 unbind_to (count, Qnil);
9240 set_buffer_internal_1 (prev);
9241 }
9242 }
9243 }
9244
9245
9246 \f
9247 /***********************************************************************
9248 Output Cursor
9249 ***********************************************************************/
9250
9251 #ifdef HAVE_WINDOW_SYSTEM
9252
9253 /* EXPORT:
9254 Nominal cursor position -- where to draw output.
9255 HPOS and VPOS are window relative glyph matrix coordinates.
9256 X and Y are window relative pixel coordinates. */
9257
9258 struct cursor_pos output_cursor;
9259
9260
9261 /* EXPORT:
9262 Set the global variable output_cursor to CURSOR. All cursor
9263 positions are relative to updated_window. */
9264
9265 void
9266 set_output_cursor (cursor)
9267 struct cursor_pos *cursor;
9268 {
9269 output_cursor.hpos = cursor->hpos;
9270 output_cursor.vpos = cursor->vpos;
9271 output_cursor.x = cursor->x;
9272 output_cursor.y = cursor->y;
9273 }
9274
9275
9276 /* EXPORT for RIF:
9277 Set a nominal cursor position.
9278
9279 HPOS and VPOS are column/row positions in a window glyph matrix. X
9280 and Y are window text area relative pixel positions.
9281
9282 If this is done during an update, updated_window will contain the
9283 window that is being updated and the position is the future output
9284 cursor position for that window. If updated_window is null, use
9285 selected_window and display the cursor at the given position. */
9286
9287 void
9288 x_cursor_to (vpos, hpos, y, x)
9289 int vpos, hpos, y, x;
9290 {
9291 struct window *w;
9292
9293 /* If updated_window is not set, work on selected_window. */
9294 if (updated_window)
9295 w = updated_window;
9296 else
9297 w = XWINDOW (selected_window);
9298
9299 /* Set the output cursor. */
9300 output_cursor.hpos = hpos;
9301 output_cursor.vpos = vpos;
9302 output_cursor.x = x;
9303 output_cursor.y = y;
9304
9305 /* If not called as part of an update, really display the cursor.
9306 This will also set the cursor position of W. */
9307 if (updated_window == NULL)
9308 {
9309 BLOCK_INPUT;
9310 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9311 if (rif->flush_display_optional)
9312 rif->flush_display_optional (SELECTED_FRAME ());
9313 UNBLOCK_INPUT;
9314 }
9315 }
9316
9317 #endif /* HAVE_WINDOW_SYSTEM */
9318
9319 \f
9320 /***********************************************************************
9321 Tool-bars
9322 ***********************************************************************/
9323
9324 #ifdef HAVE_WINDOW_SYSTEM
9325
9326 /* Where the mouse was last time we reported a mouse event. */
9327
9328 FRAME_PTR last_mouse_frame;
9329
9330 /* Tool-bar item index of the item on which a mouse button was pressed
9331 or -1. */
9332
9333 int last_tool_bar_item;
9334
9335
9336 /* Update the tool-bar item list for frame F. This has to be done
9337 before we start to fill in any display lines. Called from
9338 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9339 and restore it here. */
9340
9341 static void
9342 update_tool_bar (f, save_match_data)
9343 struct frame *f;
9344 int save_match_data;
9345 {
9346 #ifdef USE_GTK
9347 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9348 #else
9349 int do_update = WINDOWP (f->tool_bar_window)
9350 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9351 #endif
9352
9353 if (do_update)
9354 {
9355 Lisp_Object window;
9356 struct window *w;
9357
9358 window = FRAME_SELECTED_WINDOW (f);
9359 w = XWINDOW (window);
9360
9361 /* If the user has switched buffers or windows, we need to
9362 recompute to reflect the new bindings. But we'll
9363 recompute when update_mode_lines is set too; that means
9364 that people can use force-mode-line-update to request
9365 that the menu bar be recomputed. The adverse effect on
9366 the rest of the redisplay algorithm is about the same as
9367 windows_or_buffers_changed anyway. */
9368 if (windows_or_buffers_changed
9369 || !NILP (w->update_mode_line)
9370 || update_mode_lines
9371 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9372 < BUF_MODIFF (XBUFFER (w->buffer)))
9373 != !NILP (w->last_had_star))
9374 || ((!NILP (Vtransient_mark_mode)
9375 && !NILP (XBUFFER (w->buffer)->mark_active))
9376 != !NILP (w->region_showing)))
9377 {
9378 struct buffer *prev = current_buffer;
9379 int count = SPECPDL_INDEX ();
9380 Lisp_Object new_tool_bar;
9381 int new_n_tool_bar;
9382 struct gcpro gcpro1;
9383
9384 /* Set current_buffer to the buffer of the selected
9385 window of the frame, so that we get the right local
9386 keymaps. */
9387 set_buffer_internal_1 (XBUFFER (w->buffer));
9388
9389 /* Save match data, if we must. */
9390 if (save_match_data)
9391 record_unwind_save_match_data ();
9392
9393 /* Make sure that we don't accidentally use bogus keymaps. */
9394 if (NILP (Voverriding_local_map_menu_flag))
9395 {
9396 specbind (Qoverriding_terminal_local_map, Qnil);
9397 specbind (Qoverriding_local_map, Qnil);
9398 }
9399
9400 GCPRO1 (new_tool_bar);
9401
9402 /* Build desired tool-bar items from keymaps. */
9403 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9404 &new_n_tool_bar);
9405
9406 /* Redisplay the tool-bar if we changed it. */
9407 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9408 {
9409 /* Redisplay that happens asynchronously due to an expose event
9410 may access f->tool_bar_items. Make sure we update both
9411 variables within BLOCK_INPUT so no such event interrupts. */
9412 BLOCK_INPUT;
9413 f->tool_bar_items = new_tool_bar;
9414 f->n_tool_bar_items = new_n_tool_bar;
9415 w->update_mode_line = Qt;
9416 UNBLOCK_INPUT;
9417 }
9418
9419 UNGCPRO;
9420
9421 unbind_to (count, Qnil);
9422 set_buffer_internal_1 (prev);
9423 }
9424 }
9425 }
9426
9427
9428 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9429 F's desired tool-bar contents. F->tool_bar_items must have
9430 been set up previously by calling prepare_menu_bars. */
9431
9432 static void
9433 build_desired_tool_bar_string (f)
9434 struct frame *f;
9435 {
9436 int i, size, size_needed;
9437 struct gcpro gcpro1, gcpro2, gcpro3;
9438 Lisp_Object image, plist, props;
9439
9440 image = plist = props = Qnil;
9441 GCPRO3 (image, plist, props);
9442
9443 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9444 Otherwise, make a new string. */
9445
9446 /* The size of the string we might be able to reuse. */
9447 size = (STRINGP (f->desired_tool_bar_string)
9448 ? SCHARS (f->desired_tool_bar_string)
9449 : 0);
9450
9451 /* We need one space in the string for each image. */
9452 size_needed = f->n_tool_bar_items;
9453
9454 /* Reuse f->desired_tool_bar_string, if possible. */
9455 if (size < size_needed || NILP (f->desired_tool_bar_string))
9456 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9457 make_number (' '));
9458 else
9459 {
9460 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9461 Fremove_text_properties (make_number (0), make_number (size),
9462 props, f->desired_tool_bar_string);
9463 }
9464
9465 /* Put a `display' property on the string for the images to display,
9466 put a `menu_item' property on tool-bar items with a value that
9467 is the index of the item in F's tool-bar item vector. */
9468 for (i = 0; i < f->n_tool_bar_items; ++i)
9469 {
9470 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9471
9472 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9473 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9474 int hmargin, vmargin, relief, idx, end;
9475 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9476
9477 /* If image is a vector, choose the image according to the
9478 button state. */
9479 image = PROP (TOOL_BAR_ITEM_IMAGES);
9480 if (VECTORP (image))
9481 {
9482 if (enabled_p)
9483 idx = (selected_p
9484 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9485 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9486 else
9487 idx = (selected_p
9488 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9489 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9490
9491 xassert (ASIZE (image) >= idx);
9492 image = AREF (image, idx);
9493 }
9494 else
9495 idx = -1;
9496
9497 /* Ignore invalid image specifications. */
9498 if (!valid_image_p (image))
9499 continue;
9500
9501 /* Display the tool-bar button pressed, or depressed. */
9502 plist = Fcopy_sequence (XCDR (image));
9503
9504 /* Compute margin and relief to draw. */
9505 relief = (tool_bar_button_relief >= 0
9506 ? tool_bar_button_relief
9507 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9508 hmargin = vmargin = relief;
9509
9510 if (INTEGERP (Vtool_bar_button_margin)
9511 && XINT (Vtool_bar_button_margin) > 0)
9512 {
9513 hmargin += XFASTINT (Vtool_bar_button_margin);
9514 vmargin += XFASTINT (Vtool_bar_button_margin);
9515 }
9516 else if (CONSP (Vtool_bar_button_margin))
9517 {
9518 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9519 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9520 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9521
9522 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9523 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9524 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9525 }
9526
9527 if (auto_raise_tool_bar_buttons_p)
9528 {
9529 /* Add a `:relief' property to the image spec if the item is
9530 selected. */
9531 if (selected_p)
9532 {
9533 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9534 hmargin -= relief;
9535 vmargin -= relief;
9536 }
9537 }
9538 else
9539 {
9540 /* If image is selected, display it pressed, i.e. with a
9541 negative relief. If it's not selected, display it with a
9542 raised relief. */
9543 plist = Fplist_put (plist, QCrelief,
9544 (selected_p
9545 ? make_number (-relief)
9546 : make_number (relief)));
9547 hmargin -= relief;
9548 vmargin -= relief;
9549 }
9550
9551 /* Put a margin around the image. */
9552 if (hmargin || vmargin)
9553 {
9554 if (hmargin == vmargin)
9555 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9556 else
9557 plist = Fplist_put (plist, QCmargin,
9558 Fcons (make_number (hmargin),
9559 make_number (vmargin)));
9560 }
9561
9562 /* If button is not enabled, and we don't have special images
9563 for the disabled state, make the image appear disabled by
9564 applying an appropriate algorithm to it. */
9565 if (!enabled_p && idx < 0)
9566 plist = Fplist_put (plist, QCconversion, Qdisabled);
9567
9568 /* Put a `display' text property on the string for the image to
9569 display. Put a `menu-item' property on the string that gives
9570 the start of this item's properties in the tool-bar items
9571 vector. */
9572 image = Fcons (Qimage, plist);
9573 props = list4 (Qdisplay, image,
9574 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9575
9576 /* Let the last image hide all remaining spaces in the tool bar
9577 string. The string can be longer than needed when we reuse a
9578 previous string. */
9579 if (i + 1 == f->n_tool_bar_items)
9580 end = SCHARS (f->desired_tool_bar_string);
9581 else
9582 end = i + 1;
9583 Fadd_text_properties (make_number (i), make_number (end),
9584 props, f->desired_tool_bar_string);
9585 #undef PROP
9586 }
9587
9588 UNGCPRO;
9589 }
9590
9591
9592 /* Display one line of the tool-bar of frame IT->f.
9593
9594 HEIGHT specifies the desired height of the tool-bar line.
9595 If the actual height of the glyph row is less than HEIGHT, the
9596 row's height is increased to HEIGHT, and the icons are centered
9597 vertically in the new height.
9598
9599 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9600 count a final empty row in case the tool-bar width exactly matches
9601 the window width.
9602 */
9603
9604 static void
9605 display_tool_bar_line (it, height)
9606 struct it *it;
9607 int height;
9608 {
9609 struct glyph_row *row = it->glyph_row;
9610 int max_x = it->last_visible_x;
9611 struct glyph *last;
9612
9613 prepare_desired_row (row);
9614 row->y = it->current_y;
9615
9616 /* Note that this isn't made use of if the face hasn't a box,
9617 so there's no need to check the face here. */
9618 it->start_of_box_run_p = 1;
9619
9620 while (it->current_x < max_x)
9621 {
9622 int x, n_glyphs_before, i, nglyphs;
9623 struct it it_before;
9624
9625 /* Get the next display element. */
9626 if (!get_next_display_element (it))
9627 {
9628 /* Don't count empty row if we are counting needed tool-bar lines. */
9629 if (height < 0 && !it->hpos)
9630 return;
9631 break;
9632 }
9633
9634 /* Produce glyphs. */
9635 n_glyphs_before = row->used[TEXT_AREA];
9636 it_before = *it;
9637
9638 PRODUCE_GLYPHS (it);
9639
9640 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9641 i = 0;
9642 x = it_before.current_x;
9643 while (i < nglyphs)
9644 {
9645 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9646
9647 if (x + glyph->pixel_width > max_x)
9648 {
9649 /* Glyph doesn't fit on line. Backtrack. */
9650 row->used[TEXT_AREA] = n_glyphs_before;
9651 *it = it_before;
9652 goto out;
9653 }
9654
9655 ++it->hpos;
9656 x += glyph->pixel_width;
9657 ++i;
9658 }
9659
9660 /* Stop at line ends. */
9661 if (ITERATOR_AT_END_OF_LINE_P (it))
9662 break;
9663
9664 set_iterator_to_next (it, 1);
9665 }
9666
9667 out:;
9668
9669 row->displays_text_p = row->used[TEXT_AREA] != 0;
9670 /* Use default face for the border below the tool bar. */
9671 if (!row->displays_text_p)
9672 it->face_id = DEFAULT_FACE_ID;
9673 extend_face_to_end_of_line (it);
9674 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9675 last->right_box_line_p = 1;
9676 if (last == row->glyphs[TEXT_AREA])
9677 last->left_box_line_p = 1;
9678
9679 /* Make line the desired height and center it vertically. */
9680 if ((height -= it->max_ascent + it->max_descent) > 0)
9681 {
9682 /* Don't add more than one line height. */
9683 height %= FRAME_LINE_HEIGHT (it->f);
9684 it->max_ascent += height / 2;
9685 it->max_descent += (height + 1) / 2;
9686 }
9687
9688 compute_line_metrics (it);
9689
9690 /* If line is empty, make it occupy the rest of the tool-bar. */
9691 if (!row->displays_text_p)
9692 {
9693 row->height = row->phys_height = it->last_visible_y - row->y;
9694 row->ascent = row->phys_ascent = 0;
9695 row->extra_line_spacing = 0;
9696 }
9697
9698 row->full_width_p = 1;
9699 row->continued_p = 0;
9700 row->truncated_on_left_p = 0;
9701 row->truncated_on_right_p = 0;
9702
9703 it->current_x = it->hpos = 0;
9704 it->current_y += row->height;
9705 ++it->vpos;
9706 ++it->glyph_row;
9707 }
9708
9709
9710 /* Value is the number of screen lines needed to make all tool-bar
9711 items of frame F visible. The number of actual rows needed is
9712 returned in *N_ROWS if non-NULL. */
9713
9714 static int
9715 tool_bar_lines_needed (f, n_rows)
9716 struct frame *f;
9717 int *n_rows;
9718 {
9719 struct window *w = XWINDOW (f->tool_bar_window);
9720 struct it it;
9721 struct glyph_row *temp_row = w->desired_matrix->rows;
9722
9723 /* Initialize an iterator for iteration over
9724 F->desired_tool_bar_string in the tool-bar window of frame F. */
9725 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9726 it.first_visible_x = 0;
9727 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9728 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9729
9730 while (!ITERATOR_AT_END_P (&it))
9731 {
9732 clear_glyph_row (temp_row);
9733 it.glyph_row = temp_row;
9734 display_tool_bar_line (&it, -1);
9735 }
9736 clear_glyph_row (temp_row);
9737
9738 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9739 if (n_rows)
9740 *n_rows = it.vpos > 0 ? it.vpos : -1;
9741
9742 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9743 }
9744
9745
9746 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9747 0, 1, 0,
9748 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9749 (frame)
9750 Lisp_Object frame;
9751 {
9752 struct frame *f;
9753 struct window *w;
9754 int nlines = 0;
9755
9756 if (NILP (frame))
9757 frame = selected_frame;
9758 else
9759 CHECK_FRAME (frame);
9760 f = XFRAME (frame);
9761
9762 if (WINDOWP (f->tool_bar_window)
9763 || (w = XWINDOW (f->tool_bar_window),
9764 WINDOW_TOTAL_LINES (w) > 0))
9765 {
9766 update_tool_bar (f, 1);
9767 if (f->n_tool_bar_items)
9768 {
9769 build_desired_tool_bar_string (f);
9770 nlines = tool_bar_lines_needed (f, NULL);
9771 }
9772 }
9773
9774 return make_number (nlines);
9775 }
9776
9777
9778 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9779 height should be changed. */
9780
9781 static int
9782 redisplay_tool_bar (f)
9783 struct frame *f;
9784 {
9785 struct window *w;
9786 struct it it;
9787 struct glyph_row *row;
9788 int change_height_p = 0;
9789
9790 #ifdef USE_GTK
9791 if (FRAME_EXTERNAL_TOOL_BAR (f))
9792 update_frame_tool_bar (f);
9793 return 0;
9794 #endif
9795
9796 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9797 do anything. This means you must start with tool-bar-lines
9798 non-zero to get the auto-sizing effect. Or in other words, you
9799 can turn off tool-bars by specifying tool-bar-lines zero. */
9800 if (!WINDOWP (f->tool_bar_window)
9801 || (w = XWINDOW (f->tool_bar_window),
9802 WINDOW_TOTAL_LINES (w) == 0))
9803 return 0;
9804
9805 /* Set up an iterator for the tool-bar window. */
9806 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9807 it.first_visible_x = 0;
9808 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9809 row = it.glyph_row;
9810
9811 /* Build a string that represents the contents of the tool-bar. */
9812 build_desired_tool_bar_string (f);
9813 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9814
9815 if (f->n_tool_bar_rows == 0)
9816 {
9817 int nlines;
9818
9819 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9820 nlines != WINDOW_TOTAL_LINES (w)))
9821 {
9822 extern Lisp_Object Qtool_bar_lines;
9823 Lisp_Object frame;
9824 int old_height = WINDOW_TOTAL_LINES (w);
9825
9826 XSETFRAME (frame, f);
9827 clear_glyph_matrix (w->desired_matrix);
9828 Fmodify_frame_parameters (frame,
9829 Fcons (Fcons (Qtool_bar_lines,
9830 make_number (nlines)),
9831 Qnil));
9832 if (WINDOW_TOTAL_LINES (w) != old_height)
9833 {
9834 fonts_changed_p = 1;
9835 return 1;
9836 }
9837 }
9838 }
9839
9840 /* Display as many lines as needed to display all tool-bar items. */
9841
9842 if (f->n_tool_bar_rows > 0)
9843 {
9844 int border, rows, height, extra;
9845
9846 if (INTEGERP (Vtool_bar_border))
9847 border = XINT (Vtool_bar_border);
9848 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9849 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9850 else if (EQ (Vtool_bar_border, Qborder_width))
9851 border = f->border_width;
9852 else
9853 border = 0;
9854 if (border < 0)
9855 border = 0;
9856
9857 rows = f->n_tool_bar_rows;
9858 height = max (1, (it.last_visible_y - border) / rows);
9859 extra = it.last_visible_y - border - height * rows;
9860
9861 while (it.current_y < it.last_visible_y)
9862 {
9863 int h = 0;
9864 if (extra > 0 && rows-- > 0)
9865 {
9866 h = (extra + rows - 1) / rows;
9867 extra -= h;
9868 }
9869 display_tool_bar_line (&it, height + h);
9870 }
9871 }
9872 else
9873 {
9874 while (it.current_y < it.last_visible_y)
9875 display_tool_bar_line (&it, 0);
9876 }
9877
9878 /* It doesn't make much sense to try scrolling in the tool-bar
9879 window, so don't do it. */
9880 w->desired_matrix->no_scrolling_p = 1;
9881 w->must_be_updated_p = 1;
9882
9883 if (auto_resize_tool_bars_p)
9884 {
9885 int nlines;
9886
9887 /* If we couldn't display everything, change the tool-bar's
9888 height. */
9889 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9890 change_height_p = 1;
9891
9892 /* If there are blank lines at the end, except for a partially
9893 visible blank line at the end that is smaller than
9894 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9895 row = it.glyph_row - 1;
9896 if (!row->displays_text_p
9897 && row->height >= FRAME_LINE_HEIGHT (f))
9898 change_height_p = 1;
9899
9900 /* If row displays tool-bar items, but is partially visible,
9901 change the tool-bar's height. */
9902 if (row->displays_text_p
9903 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9904 change_height_p = 1;
9905
9906 /* Resize windows as needed by changing the `tool-bar-lines'
9907 frame parameter. */
9908 if (change_height_p
9909 && (nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9910 nlines != WINDOW_TOTAL_LINES (w)))
9911 {
9912 extern Lisp_Object Qtool_bar_lines;
9913 Lisp_Object frame;
9914 int old_height = WINDOW_TOTAL_LINES (w);
9915
9916 XSETFRAME (frame, f);
9917 clear_glyph_matrix (w->desired_matrix);
9918 Fmodify_frame_parameters (frame,
9919 Fcons (Fcons (Qtool_bar_lines,
9920 make_number (nlines)),
9921 Qnil));
9922 if (WINDOW_TOTAL_LINES (w) != old_height)
9923 fonts_changed_p = 1;
9924 }
9925 }
9926
9927 return change_height_p;
9928 }
9929
9930
9931 /* Get information about the tool-bar item which is displayed in GLYPH
9932 on frame F. Return in *PROP_IDX the index where tool-bar item
9933 properties start in F->tool_bar_items. Value is zero if
9934 GLYPH doesn't display a tool-bar item. */
9935
9936 static int
9937 tool_bar_item_info (f, glyph, prop_idx)
9938 struct frame *f;
9939 struct glyph *glyph;
9940 int *prop_idx;
9941 {
9942 Lisp_Object prop;
9943 int success_p;
9944 int charpos;
9945
9946 /* This function can be called asynchronously, which means we must
9947 exclude any possibility that Fget_text_property signals an
9948 error. */
9949 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9950 charpos = max (0, charpos);
9951
9952 /* Get the text property `menu-item' at pos. The value of that
9953 property is the start index of this item's properties in
9954 F->tool_bar_items. */
9955 prop = Fget_text_property (make_number (charpos),
9956 Qmenu_item, f->current_tool_bar_string);
9957 if (INTEGERP (prop))
9958 {
9959 *prop_idx = XINT (prop);
9960 success_p = 1;
9961 }
9962 else
9963 success_p = 0;
9964
9965 return success_p;
9966 }
9967
9968 \f
9969 /* Get information about the tool-bar item at position X/Y on frame F.
9970 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9971 the current matrix of the tool-bar window of F, or NULL if not
9972 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9973 item in F->tool_bar_items. Value is
9974
9975 -1 if X/Y is not on a tool-bar item
9976 0 if X/Y is on the same item that was highlighted before.
9977 1 otherwise. */
9978
9979 static int
9980 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9981 struct frame *f;
9982 int x, y;
9983 struct glyph **glyph;
9984 int *hpos, *vpos, *prop_idx;
9985 {
9986 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9987 struct window *w = XWINDOW (f->tool_bar_window);
9988 int area;
9989
9990 /* Find the glyph under X/Y. */
9991 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9992 if (*glyph == NULL)
9993 return -1;
9994
9995 /* Get the start of this tool-bar item's properties in
9996 f->tool_bar_items. */
9997 if (!tool_bar_item_info (f, *glyph, prop_idx))
9998 return -1;
9999
10000 /* Is mouse on the highlighted item? */
10001 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10002 && *vpos >= dpyinfo->mouse_face_beg_row
10003 && *vpos <= dpyinfo->mouse_face_end_row
10004 && (*vpos > dpyinfo->mouse_face_beg_row
10005 || *hpos >= dpyinfo->mouse_face_beg_col)
10006 && (*vpos < dpyinfo->mouse_face_end_row
10007 || *hpos < dpyinfo->mouse_face_end_col
10008 || dpyinfo->mouse_face_past_end))
10009 return 0;
10010
10011 return 1;
10012 }
10013
10014
10015 /* EXPORT:
10016 Handle mouse button event on the tool-bar of frame F, at
10017 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10018 0 for button release. MODIFIERS is event modifiers for button
10019 release. */
10020
10021 void
10022 handle_tool_bar_click (f, x, y, down_p, modifiers)
10023 struct frame *f;
10024 int x, y, down_p;
10025 unsigned int modifiers;
10026 {
10027 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10028 struct window *w = XWINDOW (f->tool_bar_window);
10029 int hpos, vpos, prop_idx;
10030 struct glyph *glyph;
10031 Lisp_Object enabled_p;
10032
10033 /* If not on the highlighted tool-bar item, return. */
10034 frame_to_window_pixel_xy (w, &x, &y);
10035 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10036 return;
10037
10038 /* If item is disabled, do nothing. */
10039 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10040 if (NILP (enabled_p))
10041 return;
10042
10043 if (down_p)
10044 {
10045 /* Show item in pressed state. */
10046 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10047 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10048 last_tool_bar_item = prop_idx;
10049 }
10050 else
10051 {
10052 Lisp_Object key, frame;
10053 struct input_event event;
10054 EVENT_INIT (event);
10055
10056 /* Show item in released state. */
10057 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10058 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10059
10060 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10061
10062 XSETFRAME (frame, f);
10063 event.kind = TOOL_BAR_EVENT;
10064 event.frame_or_window = frame;
10065 event.arg = frame;
10066 kbd_buffer_store_event (&event);
10067
10068 event.kind = TOOL_BAR_EVENT;
10069 event.frame_or_window = frame;
10070 event.arg = key;
10071 event.modifiers = modifiers;
10072 kbd_buffer_store_event (&event);
10073 last_tool_bar_item = -1;
10074 }
10075 }
10076
10077
10078 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10079 tool-bar window-relative coordinates X/Y. Called from
10080 note_mouse_highlight. */
10081
10082 static void
10083 note_tool_bar_highlight (f, x, y)
10084 struct frame *f;
10085 int x, y;
10086 {
10087 Lisp_Object window = f->tool_bar_window;
10088 struct window *w = XWINDOW (window);
10089 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10090 int hpos, vpos;
10091 struct glyph *glyph;
10092 struct glyph_row *row;
10093 int i;
10094 Lisp_Object enabled_p;
10095 int prop_idx;
10096 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10097 int mouse_down_p, rc;
10098
10099 /* Function note_mouse_highlight is called with negative x(y
10100 values when mouse moves outside of the frame. */
10101 if (x <= 0 || y <= 0)
10102 {
10103 clear_mouse_face (dpyinfo);
10104 return;
10105 }
10106
10107 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10108 if (rc < 0)
10109 {
10110 /* Not on tool-bar item. */
10111 clear_mouse_face (dpyinfo);
10112 return;
10113 }
10114 else if (rc == 0)
10115 /* On same tool-bar item as before. */
10116 goto set_help_echo;
10117
10118 clear_mouse_face (dpyinfo);
10119
10120 /* Mouse is down, but on different tool-bar item? */
10121 mouse_down_p = (dpyinfo->grabbed
10122 && f == last_mouse_frame
10123 && FRAME_LIVE_P (f));
10124 if (mouse_down_p
10125 && last_tool_bar_item != prop_idx)
10126 return;
10127
10128 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10129 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10130
10131 /* If tool-bar item is not enabled, don't highlight it. */
10132 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10133 if (!NILP (enabled_p))
10134 {
10135 /* Compute the x-position of the glyph. In front and past the
10136 image is a space. We include this in the highlighted area. */
10137 row = MATRIX_ROW (w->current_matrix, vpos);
10138 for (i = x = 0; i < hpos; ++i)
10139 x += row->glyphs[TEXT_AREA][i].pixel_width;
10140
10141 /* Record this as the current active region. */
10142 dpyinfo->mouse_face_beg_col = hpos;
10143 dpyinfo->mouse_face_beg_row = vpos;
10144 dpyinfo->mouse_face_beg_x = x;
10145 dpyinfo->mouse_face_beg_y = row->y;
10146 dpyinfo->mouse_face_past_end = 0;
10147
10148 dpyinfo->mouse_face_end_col = hpos + 1;
10149 dpyinfo->mouse_face_end_row = vpos;
10150 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10151 dpyinfo->mouse_face_end_y = row->y;
10152 dpyinfo->mouse_face_window = window;
10153 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10154
10155 /* Display it as active. */
10156 show_mouse_face (dpyinfo, draw);
10157 dpyinfo->mouse_face_image_state = draw;
10158 }
10159
10160 set_help_echo:
10161
10162 /* Set help_echo_string to a help string to display for this tool-bar item.
10163 XTread_socket does the rest. */
10164 help_echo_object = help_echo_window = Qnil;
10165 help_echo_pos = -1;
10166 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10167 if (NILP (help_echo_string))
10168 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10169 }
10170
10171 #endif /* HAVE_WINDOW_SYSTEM */
10172
10173
10174 \f
10175 /************************************************************************
10176 Horizontal scrolling
10177 ************************************************************************/
10178
10179 static int hscroll_window_tree P_ ((Lisp_Object));
10180 static int hscroll_windows P_ ((Lisp_Object));
10181
10182 /* For all leaf windows in the window tree rooted at WINDOW, set their
10183 hscroll value so that PT is (i) visible in the window, and (ii) so
10184 that it is not within a certain margin at the window's left and
10185 right border. Value is non-zero if any window's hscroll has been
10186 changed. */
10187
10188 static int
10189 hscroll_window_tree (window)
10190 Lisp_Object window;
10191 {
10192 int hscrolled_p = 0;
10193 int hscroll_relative_p = FLOATP (Vhscroll_step);
10194 int hscroll_step_abs = 0;
10195 double hscroll_step_rel = 0;
10196
10197 if (hscroll_relative_p)
10198 {
10199 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10200 if (hscroll_step_rel < 0)
10201 {
10202 hscroll_relative_p = 0;
10203 hscroll_step_abs = 0;
10204 }
10205 }
10206 else if (INTEGERP (Vhscroll_step))
10207 {
10208 hscroll_step_abs = XINT (Vhscroll_step);
10209 if (hscroll_step_abs < 0)
10210 hscroll_step_abs = 0;
10211 }
10212 else
10213 hscroll_step_abs = 0;
10214
10215 while (WINDOWP (window))
10216 {
10217 struct window *w = XWINDOW (window);
10218
10219 if (WINDOWP (w->hchild))
10220 hscrolled_p |= hscroll_window_tree (w->hchild);
10221 else if (WINDOWP (w->vchild))
10222 hscrolled_p |= hscroll_window_tree (w->vchild);
10223 else if (w->cursor.vpos >= 0)
10224 {
10225 int h_margin;
10226 int text_area_width;
10227 struct glyph_row *current_cursor_row
10228 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10229 struct glyph_row *desired_cursor_row
10230 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10231 struct glyph_row *cursor_row
10232 = (desired_cursor_row->enabled_p
10233 ? desired_cursor_row
10234 : current_cursor_row);
10235
10236 text_area_width = window_box_width (w, TEXT_AREA);
10237
10238 /* Scroll when cursor is inside this scroll margin. */
10239 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10240
10241 if ((XFASTINT (w->hscroll)
10242 && w->cursor.x <= h_margin)
10243 || (cursor_row->enabled_p
10244 && cursor_row->truncated_on_right_p
10245 && (w->cursor.x >= text_area_width - h_margin)))
10246 {
10247 struct it it;
10248 int hscroll;
10249 struct buffer *saved_current_buffer;
10250 int pt;
10251 int wanted_x;
10252
10253 /* Find point in a display of infinite width. */
10254 saved_current_buffer = current_buffer;
10255 current_buffer = XBUFFER (w->buffer);
10256
10257 if (w == XWINDOW (selected_window))
10258 pt = BUF_PT (current_buffer);
10259 else
10260 {
10261 pt = marker_position (w->pointm);
10262 pt = max (BEGV, pt);
10263 pt = min (ZV, pt);
10264 }
10265
10266 /* Move iterator to pt starting at cursor_row->start in
10267 a line with infinite width. */
10268 init_to_row_start (&it, w, cursor_row);
10269 it.last_visible_x = INFINITY;
10270 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10271 current_buffer = saved_current_buffer;
10272
10273 /* Position cursor in window. */
10274 if (!hscroll_relative_p && hscroll_step_abs == 0)
10275 hscroll = max (0, (it.current_x
10276 - (ITERATOR_AT_END_OF_LINE_P (&it)
10277 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10278 : (text_area_width / 2))))
10279 / FRAME_COLUMN_WIDTH (it.f);
10280 else if (w->cursor.x >= text_area_width - h_margin)
10281 {
10282 if (hscroll_relative_p)
10283 wanted_x = text_area_width * (1 - hscroll_step_rel)
10284 - h_margin;
10285 else
10286 wanted_x = text_area_width
10287 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10288 - h_margin;
10289 hscroll
10290 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10291 }
10292 else
10293 {
10294 if (hscroll_relative_p)
10295 wanted_x = text_area_width * hscroll_step_rel
10296 + h_margin;
10297 else
10298 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10299 + h_margin;
10300 hscroll
10301 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10302 }
10303 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10304
10305 /* Don't call Fset_window_hscroll if value hasn't
10306 changed because it will prevent redisplay
10307 optimizations. */
10308 if (XFASTINT (w->hscroll) != hscroll)
10309 {
10310 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10311 w->hscroll = make_number (hscroll);
10312 hscrolled_p = 1;
10313 }
10314 }
10315 }
10316
10317 window = w->next;
10318 }
10319
10320 /* Value is non-zero if hscroll of any leaf window has been changed. */
10321 return hscrolled_p;
10322 }
10323
10324
10325 /* Set hscroll so that cursor is visible and not inside horizontal
10326 scroll margins for all windows in the tree rooted at WINDOW. See
10327 also hscroll_window_tree above. Value is non-zero if any window's
10328 hscroll has been changed. If it has, desired matrices on the frame
10329 of WINDOW are cleared. */
10330
10331 static int
10332 hscroll_windows (window)
10333 Lisp_Object window;
10334 {
10335 int hscrolled_p;
10336
10337 if (automatic_hscrolling_p)
10338 {
10339 hscrolled_p = hscroll_window_tree (window);
10340 if (hscrolled_p)
10341 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10342 }
10343 else
10344 hscrolled_p = 0;
10345 return hscrolled_p;
10346 }
10347
10348
10349 \f
10350 /************************************************************************
10351 Redisplay
10352 ************************************************************************/
10353
10354 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10355 to a non-zero value. This is sometimes handy to have in a debugger
10356 session. */
10357
10358 #if GLYPH_DEBUG
10359
10360 /* First and last unchanged row for try_window_id. */
10361
10362 int debug_first_unchanged_at_end_vpos;
10363 int debug_last_unchanged_at_beg_vpos;
10364
10365 /* Delta vpos and y. */
10366
10367 int debug_dvpos, debug_dy;
10368
10369 /* Delta in characters and bytes for try_window_id. */
10370
10371 int debug_delta, debug_delta_bytes;
10372
10373 /* Values of window_end_pos and window_end_vpos at the end of
10374 try_window_id. */
10375
10376 EMACS_INT debug_end_pos, debug_end_vpos;
10377
10378 /* Append a string to W->desired_matrix->method. FMT is a printf
10379 format string. A1...A9 are a supplement for a variable-length
10380 argument list. If trace_redisplay_p is non-zero also printf the
10381 resulting string to stderr. */
10382
10383 static void
10384 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10385 struct window *w;
10386 char *fmt;
10387 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10388 {
10389 char buffer[512];
10390 char *method = w->desired_matrix->method;
10391 int len = strlen (method);
10392 int size = sizeof w->desired_matrix->method;
10393 int remaining = size - len - 1;
10394
10395 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10396 if (len && remaining)
10397 {
10398 method[len] = '|';
10399 --remaining, ++len;
10400 }
10401
10402 strncpy (method + len, buffer, remaining);
10403
10404 if (trace_redisplay_p)
10405 fprintf (stderr, "%p (%s): %s\n",
10406 w,
10407 ((BUFFERP (w->buffer)
10408 && STRINGP (XBUFFER (w->buffer)->name))
10409 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10410 : "no buffer"),
10411 buffer);
10412 }
10413
10414 #endif /* GLYPH_DEBUG */
10415
10416
10417 /* Value is non-zero if all changes in window W, which displays
10418 current_buffer, are in the text between START and END. START is a
10419 buffer position, END is given as a distance from Z. Used in
10420 redisplay_internal for display optimization. */
10421
10422 static INLINE int
10423 text_outside_line_unchanged_p (w, start, end)
10424 struct window *w;
10425 int start, end;
10426 {
10427 int unchanged_p = 1;
10428
10429 /* If text or overlays have changed, see where. */
10430 if (XFASTINT (w->last_modified) < MODIFF
10431 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10432 {
10433 /* Gap in the line? */
10434 if (GPT < start || Z - GPT < end)
10435 unchanged_p = 0;
10436
10437 /* Changes start in front of the line, or end after it? */
10438 if (unchanged_p
10439 && (BEG_UNCHANGED < start - 1
10440 || END_UNCHANGED < end))
10441 unchanged_p = 0;
10442
10443 /* If selective display, can't optimize if changes start at the
10444 beginning of the line. */
10445 if (unchanged_p
10446 && INTEGERP (current_buffer->selective_display)
10447 && XINT (current_buffer->selective_display) > 0
10448 && (BEG_UNCHANGED < start || GPT <= start))
10449 unchanged_p = 0;
10450
10451 /* If there are overlays at the start or end of the line, these
10452 may have overlay strings with newlines in them. A change at
10453 START, for instance, may actually concern the display of such
10454 overlay strings as well, and they are displayed on different
10455 lines. So, quickly rule out this case. (For the future, it
10456 might be desirable to implement something more telling than
10457 just BEG/END_UNCHANGED.) */
10458 if (unchanged_p)
10459 {
10460 if (BEG + BEG_UNCHANGED == start
10461 && overlay_touches_p (start))
10462 unchanged_p = 0;
10463 if (END_UNCHANGED == end
10464 && overlay_touches_p (Z - end))
10465 unchanged_p = 0;
10466 }
10467 }
10468
10469 return unchanged_p;
10470 }
10471
10472
10473 /* Do a frame update, taking possible shortcuts into account. This is
10474 the main external entry point for redisplay.
10475
10476 If the last redisplay displayed an echo area message and that message
10477 is no longer requested, we clear the echo area or bring back the
10478 mini-buffer if that is in use. */
10479
10480 void
10481 redisplay ()
10482 {
10483 redisplay_internal (0);
10484 }
10485
10486
10487 static Lisp_Object
10488 overlay_arrow_string_or_property (var)
10489 Lisp_Object var;
10490 {
10491 Lisp_Object val;
10492
10493 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10494 return val;
10495
10496 return Voverlay_arrow_string;
10497 }
10498
10499 /* Return 1 if there are any overlay-arrows in current_buffer. */
10500 static int
10501 overlay_arrow_in_current_buffer_p ()
10502 {
10503 Lisp_Object vlist;
10504
10505 for (vlist = Voverlay_arrow_variable_list;
10506 CONSP (vlist);
10507 vlist = XCDR (vlist))
10508 {
10509 Lisp_Object var = XCAR (vlist);
10510 Lisp_Object val;
10511
10512 if (!SYMBOLP (var))
10513 continue;
10514 val = find_symbol_value (var);
10515 if (MARKERP (val)
10516 && current_buffer == XMARKER (val)->buffer)
10517 return 1;
10518 }
10519 return 0;
10520 }
10521
10522
10523 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10524 has changed. */
10525
10526 static int
10527 overlay_arrows_changed_p ()
10528 {
10529 Lisp_Object vlist;
10530
10531 for (vlist = Voverlay_arrow_variable_list;
10532 CONSP (vlist);
10533 vlist = XCDR (vlist))
10534 {
10535 Lisp_Object var = XCAR (vlist);
10536 Lisp_Object val, pstr;
10537
10538 if (!SYMBOLP (var))
10539 continue;
10540 val = find_symbol_value (var);
10541 if (!MARKERP (val))
10542 continue;
10543 if (! EQ (COERCE_MARKER (val),
10544 Fget (var, Qlast_arrow_position))
10545 || ! (pstr = overlay_arrow_string_or_property (var),
10546 EQ (pstr, Fget (var, Qlast_arrow_string))))
10547 return 1;
10548 }
10549 return 0;
10550 }
10551
10552 /* Mark overlay arrows to be updated on next redisplay. */
10553
10554 static void
10555 update_overlay_arrows (up_to_date)
10556 int up_to_date;
10557 {
10558 Lisp_Object vlist;
10559
10560 for (vlist = Voverlay_arrow_variable_list;
10561 CONSP (vlist);
10562 vlist = XCDR (vlist))
10563 {
10564 Lisp_Object var = XCAR (vlist);
10565
10566 if (!SYMBOLP (var))
10567 continue;
10568
10569 if (up_to_date > 0)
10570 {
10571 Lisp_Object val = find_symbol_value (var);
10572 Fput (var, Qlast_arrow_position,
10573 COERCE_MARKER (val));
10574 Fput (var, Qlast_arrow_string,
10575 overlay_arrow_string_or_property (var));
10576 }
10577 else if (up_to_date < 0
10578 || !NILP (Fget (var, Qlast_arrow_position)))
10579 {
10580 Fput (var, Qlast_arrow_position, Qt);
10581 Fput (var, Qlast_arrow_string, Qt);
10582 }
10583 }
10584 }
10585
10586
10587 /* Return overlay arrow string to display at row.
10588 Return integer (bitmap number) for arrow bitmap in left fringe.
10589 Return nil if no overlay arrow. */
10590
10591 static Lisp_Object
10592 overlay_arrow_at_row (it, row)
10593 struct it *it;
10594 struct glyph_row *row;
10595 {
10596 Lisp_Object vlist;
10597
10598 for (vlist = Voverlay_arrow_variable_list;
10599 CONSP (vlist);
10600 vlist = XCDR (vlist))
10601 {
10602 Lisp_Object var = XCAR (vlist);
10603 Lisp_Object val;
10604
10605 if (!SYMBOLP (var))
10606 continue;
10607
10608 val = find_symbol_value (var);
10609
10610 if (MARKERP (val)
10611 && current_buffer == XMARKER (val)->buffer
10612 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10613 {
10614 if (FRAME_WINDOW_P (it->f)
10615 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10616 {
10617 #ifdef HAVE_WINDOW_SYSTEM
10618 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10619 {
10620 int fringe_bitmap;
10621 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10622 return make_number (fringe_bitmap);
10623 }
10624 #endif
10625 return make_number (-1); /* Use default arrow bitmap */
10626 }
10627 return overlay_arrow_string_or_property (var);
10628 }
10629 }
10630
10631 return Qnil;
10632 }
10633
10634 /* Return 1 if point moved out of or into a composition. Otherwise
10635 return 0. PREV_BUF and PREV_PT are the last point buffer and
10636 position. BUF and PT are the current point buffer and position. */
10637
10638 int
10639 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10640 struct buffer *prev_buf, *buf;
10641 int prev_pt, pt;
10642 {
10643 EMACS_INT start, end;
10644 Lisp_Object prop;
10645 Lisp_Object buffer;
10646
10647 XSETBUFFER (buffer, buf);
10648 /* Check a composition at the last point if point moved within the
10649 same buffer. */
10650 if (prev_buf == buf)
10651 {
10652 if (prev_pt == pt)
10653 /* Point didn't move. */
10654 return 0;
10655
10656 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10657 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10658 && COMPOSITION_VALID_P (start, end, prop)
10659 && start < prev_pt && end > prev_pt)
10660 /* The last point was within the composition. Return 1 iff
10661 point moved out of the composition. */
10662 return (pt <= start || pt >= end);
10663 }
10664
10665 /* Check a composition at the current point. */
10666 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10667 && find_composition (pt, -1, &start, &end, &prop, buffer)
10668 && COMPOSITION_VALID_P (start, end, prop)
10669 && start < pt && end > pt);
10670 }
10671
10672
10673 /* Reconsider the setting of B->clip_changed which is displayed
10674 in window W. */
10675
10676 static INLINE void
10677 reconsider_clip_changes (w, b)
10678 struct window *w;
10679 struct buffer *b;
10680 {
10681 if (b->clip_changed
10682 && !NILP (w->window_end_valid)
10683 && w->current_matrix->buffer == b
10684 && w->current_matrix->zv == BUF_ZV (b)
10685 && w->current_matrix->begv == BUF_BEGV (b))
10686 b->clip_changed = 0;
10687
10688 /* If display wasn't paused, and W is not a tool bar window, see if
10689 point has been moved into or out of a composition. In that case,
10690 we set b->clip_changed to 1 to force updating the screen. If
10691 b->clip_changed has already been set to 1, we can skip this
10692 check. */
10693 if (!b->clip_changed
10694 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10695 {
10696 int pt;
10697
10698 if (w == XWINDOW (selected_window))
10699 pt = BUF_PT (current_buffer);
10700 else
10701 pt = marker_position (w->pointm);
10702
10703 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10704 || pt != XINT (w->last_point))
10705 && check_point_in_composition (w->current_matrix->buffer,
10706 XINT (w->last_point),
10707 XBUFFER (w->buffer), pt))
10708 b->clip_changed = 1;
10709 }
10710 }
10711 \f
10712
10713 /* Select FRAME to forward the values of frame-local variables into C
10714 variables so that the redisplay routines can access those values
10715 directly. */
10716
10717 static void
10718 select_frame_for_redisplay (frame)
10719 Lisp_Object frame;
10720 {
10721 Lisp_Object tail, sym, val;
10722 Lisp_Object old = selected_frame;
10723
10724 selected_frame = frame;
10725
10726 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10727 if (CONSP (XCAR (tail))
10728 && (sym = XCAR (XCAR (tail)),
10729 SYMBOLP (sym))
10730 && (sym = indirect_variable (sym),
10731 val = SYMBOL_VALUE (sym),
10732 (BUFFER_LOCAL_VALUEP (val)
10733 || SOME_BUFFER_LOCAL_VALUEP (val)))
10734 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10735 /* Use find_symbol_value rather than Fsymbol_value
10736 to avoid an error if it is void. */
10737 find_symbol_value (sym);
10738
10739 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10740 if (CONSP (XCAR (tail))
10741 && (sym = XCAR (XCAR (tail)),
10742 SYMBOLP (sym))
10743 && (sym = indirect_variable (sym),
10744 val = SYMBOL_VALUE (sym),
10745 (BUFFER_LOCAL_VALUEP (val)
10746 || SOME_BUFFER_LOCAL_VALUEP (val)))
10747 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10748 find_symbol_value (sym);
10749 }
10750
10751
10752 #define STOP_POLLING \
10753 do { if (! polling_stopped_here) stop_polling (); \
10754 polling_stopped_here = 1; } while (0)
10755
10756 #define RESUME_POLLING \
10757 do { if (polling_stopped_here) start_polling (); \
10758 polling_stopped_here = 0; } while (0)
10759
10760
10761 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10762 response to any user action; therefore, we should preserve the echo
10763 area. (Actually, our caller does that job.) Perhaps in the future
10764 avoid recentering windows if it is not necessary; currently that
10765 causes some problems. */
10766
10767 static void
10768 redisplay_internal (preserve_echo_area)
10769 int preserve_echo_area;
10770 {
10771 struct window *w = XWINDOW (selected_window);
10772 struct frame *f = XFRAME (w->frame);
10773 int pause;
10774 int must_finish = 0;
10775 struct text_pos tlbufpos, tlendpos;
10776 int number_of_visible_frames;
10777 int count;
10778 struct frame *sf = SELECTED_FRAME ();
10779 int polling_stopped_here = 0;
10780
10781 /* Non-zero means redisplay has to consider all windows on all
10782 frames. Zero means, only selected_window is considered. */
10783 int consider_all_windows_p;
10784
10785 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10786
10787 /* No redisplay if running in batch mode or frame is not yet fully
10788 initialized, or redisplay is explicitly turned off by setting
10789 Vinhibit_redisplay. */
10790 if (noninteractive
10791 || !NILP (Vinhibit_redisplay)
10792 || !f->glyphs_initialized_p)
10793 return;
10794
10795 /* The flag redisplay_performed_directly_p is set by
10796 direct_output_for_insert when it already did the whole screen
10797 update necessary. */
10798 if (redisplay_performed_directly_p)
10799 {
10800 redisplay_performed_directly_p = 0;
10801 if (!hscroll_windows (selected_window))
10802 return;
10803 }
10804
10805 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10806 if (popup_activated ())
10807 return;
10808 #endif
10809
10810 /* I don't think this happens but let's be paranoid. */
10811 if (redisplaying_p)
10812 return;
10813
10814 /* Record a function that resets redisplaying_p to its old value
10815 when we leave this function. */
10816 count = SPECPDL_INDEX ();
10817 record_unwind_protect (unwind_redisplay,
10818 Fcons (make_number (redisplaying_p), selected_frame));
10819 ++redisplaying_p;
10820 specbind (Qinhibit_free_realized_faces, Qnil);
10821
10822 {
10823 Lisp_Object tail, frame;
10824
10825 FOR_EACH_FRAME (tail, frame)
10826 {
10827 struct frame *f = XFRAME (frame);
10828 f->already_hscrolled_p = 0;
10829 }
10830 }
10831
10832 retry:
10833 pause = 0;
10834 reconsider_clip_changes (w, current_buffer);
10835 last_escape_glyph_frame = NULL;
10836 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10837
10838 /* If new fonts have been loaded that make a glyph matrix adjustment
10839 necessary, do it. */
10840 if (fonts_changed_p)
10841 {
10842 adjust_glyphs (NULL);
10843 ++windows_or_buffers_changed;
10844 fonts_changed_p = 0;
10845 }
10846
10847 /* If face_change_count is non-zero, init_iterator will free all
10848 realized faces, which includes the faces referenced from current
10849 matrices. So, we can't reuse current matrices in this case. */
10850 if (face_change_count)
10851 ++windows_or_buffers_changed;
10852
10853 if (! FRAME_WINDOW_P (sf)
10854 && previous_terminal_frame != sf)
10855 {
10856 /* Since frames on an ASCII terminal share the same display
10857 area, displaying a different frame means redisplay the whole
10858 thing. */
10859 windows_or_buffers_changed++;
10860 SET_FRAME_GARBAGED (sf);
10861 XSETFRAME (Vterminal_frame, sf);
10862 }
10863 previous_terminal_frame = sf;
10864
10865 /* Set the visible flags for all frames. Do this before checking
10866 for resized or garbaged frames; they want to know if their frames
10867 are visible. See the comment in frame.h for
10868 FRAME_SAMPLE_VISIBILITY. */
10869 {
10870 Lisp_Object tail, frame;
10871
10872 number_of_visible_frames = 0;
10873
10874 FOR_EACH_FRAME (tail, frame)
10875 {
10876 struct frame *f = XFRAME (frame);
10877
10878 FRAME_SAMPLE_VISIBILITY (f);
10879 if (FRAME_VISIBLE_P (f))
10880 ++number_of_visible_frames;
10881 clear_desired_matrices (f);
10882 }
10883 }
10884
10885 /* Notice any pending interrupt request to change frame size. */
10886 do_pending_window_change (1);
10887
10888 /* Clear frames marked as garbaged. */
10889 if (frame_garbaged)
10890 clear_garbaged_frames ();
10891
10892 /* Build menubar and tool-bar items. */
10893 if (NILP (Vmemory_full))
10894 prepare_menu_bars ();
10895
10896 if (windows_or_buffers_changed)
10897 update_mode_lines++;
10898
10899 /* Detect case that we need to write or remove a star in the mode line. */
10900 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10901 {
10902 w->update_mode_line = Qt;
10903 if (buffer_shared > 1)
10904 update_mode_lines++;
10905 }
10906
10907 /* If %c is in the mode line, update it if needed. */
10908 if (!NILP (w->column_number_displayed)
10909 /* This alternative quickly identifies a common case
10910 where no change is needed. */
10911 && !(PT == XFASTINT (w->last_point)
10912 && XFASTINT (w->last_modified) >= MODIFF
10913 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10914 && (XFASTINT (w->column_number_displayed)
10915 != (int) current_column ())) /* iftc */
10916 w->update_mode_line = Qt;
10917
10918 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10919
10920 /* The variable buffer_shared is set in redisplay_window and
10921 indicates that we redisplay a buffer in different windows. See
10922 there. */
10923 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10924 || cursor_type_changed);
10925
10926 /* If specs for an arrow have changed, do thorough redisplay
10927 to ensure we remove any arrow that should no longer exist. */
10928 if (overlay_arrows_changed_p ())
10929 consider_all_windows_p = windows_or_buffers_changed = 1;
10930
10931 /* Normally the message* functions will have already displayed and
10932 updated the echo area, but the frame may have been trashed, or
10933 the update may have been preempted, so display the echo area
10934 again here. Checking message_cleared_p captures the case that
10935 the echo area should be cleared. */
10936 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10937 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10938 || (message_cleared_p
10939 && minibuf_level == 0
10940 /* If the mini-window is currently selected, this means the
10941 echo-area doesn't show through. */
10942 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10943 {
10944 int window_height_changed_p = echo_area_display (0);
10945 must_finish = 1;
10946
10947 /* If we don't display the current message, don't clear the
10948 message_cleared_p flag, because, if we did, we wouldn't clear
10949 the echo area in the next redisplay which doesn't preserve
10950 the echo area. */
10951 if (!display_last_displayed_message_p)
10952 message_cleared_p = 0;
10953
10954 if (fonts_changed_p)
10955 goto retry;
10956 else if (window_height_changed_p)
10957 {
10958 consider_all_windows_p = 1;
10959 ++update_mode_lines;
10960 ++windows_or_buffers_changed;
10961
10962 /* If window configuration was changed, frames may have been
10963 marked garbaged. Clear them or we will experience
10964 surprises wrt scrolling. */
10965 if (frame_garbaged)
10966 clear_garbaged_frames ();
10967 }
10968 }
10969 else if (EQ (selected_window, minibuf_window)
10970 && (current_buffer->clip_changed
10971 || XFASTINT (w->last_modified) < MODIFF
10972 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10973 && resize_mini_window (w, 0))
10974 {
10975 /* Resized active mini-window to fit the size of what it is
10976 showing if its contents might have changed. */
10977 must_finish = 1;
10978 consider_all_windows_p = 1;
10979 ++windows_or_buffers_changed;
10980 ++update_mode_lines;
10981
10982 /* If window configuration was changed, frames may have been
10983 marked garbaged. Clear them or we will experience
10984 surprises wrt scrolling. */
10985 if (frame_garbaged)
10986 clear_garbaged_frames ();
10987 }
10988
10989
10990 /* If showing the region, and mark has changed, we must redisplay
10991 the whole window. The assignment to this_line_start_pos prevents
10992 the optimization directly below this if-statement. */
10993 if (((!NILP (Vtransient_mark_mode)
10994 && !NILP (XBUFFER (w->buffer)->mark_active))
10995 != !NILP (w->region_showing))
10996 || (!NILP (w->region_showing)
10997 && !EQ (w->region_showing,
10998 Fmarker_position (XBUFFER (w->buffer)->mark))))
10999 CHARPOS (this_line_start_pos) = 0;
11000
11001 /* Optimize the case that only the line containing the cursor in the
11002 selected window has changed. Variables starting with this_ are
11003 set in display_line and record information about the line
11004 containing the cursor. */
11005 tlbufpos = this_line_start_pos;
11006 tlendpos = this_line_end_pos;
11007 if (!consider_all_windows_p
11008 && CHARPOS (tlbufpos) > 0
11009 && NILP (w->update_mode_line)
11010 && !current_buffer->clip_changed
11011 && !current_buffer->prevent_redisplay_optimizations_p
11012 && FRAME_VISIBLE_P (XFRAME (w->frame))
11013 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11014 /* Make sure recorded data applies to current buffer, etc. */
11015 && this_line_buffer == current_buffer
11016 && current_buffer == XBUFFER (w->buffer)
11017 && NILP (w->force_start)
11018 && NILP (w->optional_new_start)
11019 /* Point must be on the line that we have info recorded about. */
11020 && PT >= CHARPOS (tlbufpos)
11021 && PT <= Z - CHARPOS (tlendpos)
11022 /* All text outside that line, including its final newline,
11023 must be unchanged */
11024 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11025 CHARPOS (tlendpos)))
11026 {
11027 if (CHARPOS (tlbufpos) > BEGV
11028 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11029 && (CHARPOS (tlbufpos) == ZV
11030 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11031 /* Former continuation line has disappeared by becoming empty */
11032 goto cancel;
11033 else if (XFASTINT (w->last_modified) < MODIFF
11034 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11035 || MINI_WINDOW_P (w))
11036 {
11037 /* We have to handle the case of continuation around a
11038 wide-column character (See the comment in indent.c around
11039 line 885).
11040
11041 For instance, in the following case:
11042
11043 -------- Insert --------
11044 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11045 J_I_ ==> J_I_ `^^' are cursors.
11046 ^^ ^^
11047 -------- --------
11048
11049 As we have to redraw the line above, we should goto cancel. */
11050
11051 struct it it;
11052 int line_height_before = this_line_pixel_height;
11053
11054 /* Note that start_display will handle the case that the
11055 line starting at tlbufpos is a continuation lines. */
11056 start_display (&it, w, tlbufpos);
11057
11058 /* Implementation note: It this still necessary? */
11059 if (it.current_x != this_line_start_x)
11060 goto cancel;
11061
11062 TRACE ((stderr, "trying display optimization 1\n"));
11063 w->cursor.vpos = -1;
11064 overlay_arrow_seen = 0;
11065 it.vpos = this_line_vpos;
11066 it.current_y = this_line_y;
11067 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11068 display_line (&it);
11069
11070 /* If line contains point, is not continued,
11071 and ends at same distance from eob as before, we win */
11072 if (w->cursor.vpos >= 0
11073 /* Line is not continued, otherwise this_line_start_pos
11074 would have been set to 0 in display_line. */
11075 && CHARPOS (this_line_start_pos)
11076 /* Line ends as before. */
11077 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11078 /* Line has same height as before. Otherwise other lines
11079 would have to be shifted up or down. */
11080 && this_line_pixel_height == line_height_before)
11081 {
11082 /* If this is not the window's last line, we must adjust
11083 the charstarts of the lines below. */
11084 if (it.current_y < it.last_visible_y)
11085 {
11086 struct glyph_row *row
11087 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11088 int delta, delta_bytes;
11089
11090 if (Z - CHARPOS (tlendpos) == ZV)
11091 {
11092 /* This line ends at end of (accessible part of)
11093 buffer. There is no newline to count. */
11094 delta = (Z
11095 - CHARPOS (tlendpos)
11096 - MATRIX_ROW_START_CHARPOS (row));
11097 delta_bytes = (Z_BYTE
11098 - BYTEPOS (tlendpos)
11099 - MATRIX_ROW_START_BYTEPOS (row));
11100 }
11101 else
11102 {
11103 /* This line ends in a newline. Must take
11104 account of the newline and the rest of the
11105 text that follows. */
11106 delta = (Z
11107 - CHARPOS (tlendpos)
11108 - MATRIX_ROW_START_CHARPOS (row));
11109 delta_bytes = (Z_BYTE
11110 - BYTEPOS (tlendpos)
11111 - MATRIX_ROW_START_BYTEPOS (row));
11112 }
11113
11114 increment_matrix_positions (w->current_matrix,
11115 this_line_vpos + 1,
11116 w->current_matrix->nrows,
11117 delta, delta_bytes);
11118 }
11119
11120 /* If this row displays text now but previously didn't,
11121 or vice versa, w->window_end_vpos may have to be
11122 adjusted. */
11123 if ((it.glyph_row - 1)->displays_text_p)
11124 {
11125 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11126 XSETINT (w->window_end_vpos, this_line_vpos);
11127 }
11128 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11129 && this_line_vpos > 0)
11130 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11131 w->window_end_valid = Qnil;
11132
11133 /* Update hint: No need to try to scroll in update_window. */
11134 w->desired_matrix->no_scrolling_p = 1;
11135
11136 #if GLYPH_DEBUG
11137 *w->desired_matrix->method = 0;
11138 debug_method_add (w, "optimization 1");
11139 #endif
11140 #ifdef HAVE_WINDOW_SYSTEM
11141 update_window_fringes (w, 0);
11142 #endif
11143 goto update;
11144 }
11145 else
11146 goto cancel;
11147 }
11148 else if (/* Cursor position hasn't changed. */
11149 PT == XFASTINT (w->last_point)
11150 /* Make sure the cursor was last displayed
11151 in this window. Otherwise we have to reposition it. */
11152 && 0 <= w->cursor.vpos
11153 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11154 {
11155 if (!must_finish)
11156 {
11157 do_pending_window_change (1);
11158
11159 /* We used to always goto end_of_redisplay here, but this
11160 isn't enough if we have a blinking cursor. */
11161 if (w->cursor_off_p == w->last_cursor_off_p)
11162 goto end_of_redisplay;
11163 }
11164 goto update;
11165 }
11166 /* If highlighting the region, or if the cursor is in the echo area,
11167 then we can't just move the cursor. */
11168 else if (! (!NILP (Vtransient_mark_mode)
11169 && !NILP (current_buffer->mark_active))
11170 && (EQ (selected_window, current_buffer->last_selected_window)
11171 || highlight_nonselected_windows)
11172 && NILP (w->region_showing)
11173 && NILP (Vshow_trailing_whitespace)
11174 && !cursor_in_echo_area)
11175 {
11176 struct it it;
11177 struct glyph_row *row;
11178
11179 /* Skip from tlbufpos to PT and see where it is. Note that
11180 PT may be in invisible text. If so, we will end at the
11181 next visible position. */
11182 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11183 NULL, DEFAULT_FACE_ID);
11184 it.current_x = this_line_start_x;
11185 it.current_y = this_line_y;
11186 it.vpos = this_line_vpos;
11187
11188 /* The call to move_it_to stops in front of PT, but
11189 moves over before-strings. */
11190 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11191
11192 if (it.vpos == this_line_vpos
11193 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11194 row->enabled_p))
11195 {
11196 xassert (this_line_vpos == it.vpos);
11197 xassert (this_line_y == it.current_y);
11198 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11199 #if GLYPH_DEBUG
11200 *w->desired_matrix->method = 0;
11201 debug_method_add (w, "optimization 3");
11202 #endif
11203 goto update;
11204 }
11205 else
11206 goto cancel;
11207 }
11208
11209 cancel:
11210 /* Text changed drastically or point moved off of line. */
11211 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11212 }
11213
11214 CHARPOS (this_line_start_pos) = 0;
11215 consider_all_windows_p |= buffer_shared > 1;
11216 ++clear_face_cache_count;
11217 #ifdef HAVE_WINDOW_SYSTEM
11218 ++clear_image_cache_count;
11219 #endif
11220
11221 /* Build desired matrices, and update the display. If
11222 consider_all_windows_p is non-zero, do it for all windows on all
11223 frames. Otherwise do it for selected_window, only. */
11224
11225 if (consider_all_windows_p)
11226 {
11227 Lisp_Object tail, frame;
11228
11229 FOR_EACH_FRAME (tail, frame)
11230 XFRAME (frame)->updated_p = 0;
11231
11232 /* Recompute # windows showing selected buffer. This will be
11233 incremented each time such a window is displayed. */
11234 buffer_shared = 0;
11235
11236 FOR_EACH_FRAME (tail, frame)
11237 {
11238 struct frame *f = XFRAME (frame);
11239
11240 if (FRAME_WINDOW_P (f) || f == sf)
11241 {
11242 if (! EQ (frame, selected_frame))
11243 /* Select the frame, for the sake of frame-local
11244 variables. */
11245 select_frame_for_redisplay (frame);
11246
11247 /* Mark all the scroll bars to be removed; we'll redeem
11248 the ones we want when we redisplay their windows. */
11249 if (condemn_scroll_bars_hook)
11250 condemn_scroll_bars_hook (f);
11251
11252 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11253 redisplay_windows (FRAME_ROOT_WINDOW (f));
11254
11255 /* Any scroll bars which redisplay_windows should have
11256 nuked should now go away. */
11257 if (judge_scroll_bars_hook)
11258 judge_scroll_bars_hook (f);
11259
11260 /* If fonts changed, display again. */
11261 /* ??? rms: I suspect it is a mistake to jump all the way
11262 back to retry here. It should just retry this frame. */
11263 if (fonts_changed_p)
11264 goto retry;
11265
11266 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11267 {
11268 /* See if we have to hscroll. */
11269 if (!f->already_hscrolled_p)
11270 {
11271 f->already_hscrolled_p = 1;
11272 if (hscroll_windows (f->root_window))
11273 goto retry;
11274 }
11275
11276 /* Prevent various kinds of signals during display
11277 update. stdio is not robust about handling
11278 signals, which can cause an apparent I/O
11279 error. */
11280 if (interrupt_input)
11281 unrequest_sigio ();
11282 STOP_POLLING;
11283
11284 /* Update the display. */
11285 set_window_update_flags (XWINDOW (f->root_window), 1);
11286 pause |= update_frame (f, 0, 0);
11287 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11288 if (pause)
11289 break;
11290 #endif
11291
11292 f->updated_p = 1;
11293 }
11294 }
11295 }
11296
11297 if (!pause)
11298 {
11299 /* Do the mark_window_display_accurate after all windows have
11300 been redisplayed because this call resets flags in buffers
11301 which are needed for proper redisplay. */
11302 FOR_EACH_FRAME (tail, frame)
11303 {
11304 struct frame *f = XFRAME (frame);
11305 if (f->updated_p)
11306 {
11307 mark_window_display_accurate (f->root_window, 1);
11308 if (frame_up_to_date_hook)
11309 frame_up_to_date_hook (f);
11310 }
11311 }
11312 }
11313 }
11314 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11315 {
11316 Lisp_Object mini_window;
11317 struct frame *mini_frame;
11318
11319 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11320 /* Use list_of_error, not Qerror, so that
11321 we catch only errors and don't run the debugger. */
11322 internal_condition_case_1 (redisplay_window_1, selected_window,
11323 list_of_error,
11324 redisplay_window_error);
11325
11326 /* Compare desired and current matrices, perform output. */
11327
11328 update:
11329 /* If fonts changed, display again. */
11330 if (fonts_changed_p)
11331 goto retry;
11332
11333 /* Prevent various kinds of signals during display update.
11334 stdio is not robust about handling signals,
11335 which can cause an apparent I/O error. */
11336 if (interrupt_input)
11337 unrequest_sigio ();
11338 STOP_POLLING;
11339
11340 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11341 {
11342 if (hscroll_windows (selected_window))
11343 goto retry;
11344
11345 XWINDOW (selected_window)->must_be_updated_p = 1;
11346 pause = update_frame (sf, 0, 0);
11347 }
11348
11349 /* We may have called echo_area_display at the top of this
11350 function. If the echo area is on another frame, that may
11351 have put text on a frame other than the selected one, so the
11352 above call to update_frame would not have caught it. Catch
11353 it here. */
11354 mini_window = FRAME_MINIBUF_WINDOW (sf);
11355 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11356
11357 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11358 {
11359 XWINDOW (mini_window)->must_be_updated_p = 1;
11360 pause |= update_frame (mini_frame, 0, 0);
11361 if (!pause && hscroll_windows (mini_window))
11362 goto retry;
11363 }
11364 }
11365
11366 /* If display was paused because of pending input, make sure we do a
11367 thorough update the next time. */
11368 if (pause)
11369 {
11370 /* Prevent the optimization at the beginning of
11371 redisplay_internal that tries a single-line update of the
11372 line containing the cursor in the selected window. */
11373 CHARPOS (this_line_start_pos) = 0;
11374
11375 /* Let the overlay arrow be updated the next time. */
11376 update_overlay_arrows (0);
11377
11378 /* If we pause after scrolling, some rows in the current
11379 matrices of some windows are not valid. */
11380 if (!WINDOW_FULL_WIDTH_P (w)
11381 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11382 update_mode_lines = 1;
11383 }
11384 else
11385 {
11386 if (!consider_all_windows_p)
11387 {
11388 /* This has already been done above if
11389 consider_all_windows_p is set. */
11390 mark_window_display_accurate_1 (w, 1);
11391
11392 /* Say overlay arrows are up to date. */
11393 update_overlay_arrows (1);
11394
11395 if (frame_up_to_date_hook != 0)
11396 frame_up_to_date_hook (sf);
11397 }
11398
11399 update_mode_lines = 0;
11400 windows_or_buffers_changed = 0;
11401 cursor_type_changed = 0;
11402 }
11403
11404 /* Start SIGIO interrupts coming again. Having them off during the
11405 code above makes it less likely one will discard output, but not
11406 impossible, since there might be stuff in the system buffer here.
11407 But it is much hairier to try to do anything about that. */
11408 if (interrupt_input)
11409 request_sigio ();
11410 RESUME_POLLING;
11411
11412 /* If a frame has become visible which was not before, redisplay
11413 again, so that we display it. Expose events for such a frame
11414 (which it gets when becoming visible) don't call the parts of
11415 redisplay constructing glyphs, so simply exposing a frame won't
11416 display anything in this case. So, we have to display these
11417 frames here explicitly. */
11418 if (!pause)
11419 {
11420 Lisp_Object tail, frame;
11421 int new_count = 0;
11422
11423 FOR_EACH_FRAME (tail, frame)
11424 {
11425 int this_is_visible = 0;
11426
11427 if (XFRAME (frame)->visible)
11428 this_is_visible = 1;
11429 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11430 if (XFRAME (frame)->visible)
11431 this_is_visible = 1;
11432
11433 if (this_is_visible)
11434 new_count++;
11435 }
11436
11437 if (new_count != number_of_visible_frames)
11438 windows_or_buffers_changed++;
11439 }
11440
11441 /* Change frame size now if a change is pending. */
11442 do_pending_window_change (1);
11443
11444 /* If we just did a pending size change, or have additional
11445 visible frames, redisplay again. */
11446 if (windows_or_buffers_changed && !pause)
11447 goto retry;
11448
11449 /* Clear the face cache eventually. */
11450 if (consider_all_windows_p)
11451 {
11452 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11453 {
11454 clear_face_cache (0);
11455 clear_face_cache_count = 0;
11456 }
11457 #ifdef HAVE_WINDOW_SYSTEM
11458 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11459 {
11460 Lisp_Object tail, frame;
11461 FOR_EACH_FRAME (tail, frame)
11462 {
11463 struct frame *f = XFRAME (frame);
11464 if (FRAME_WINDOW_P (f))
11465 clear_image_cache (f, 0);
11466 }
11467 clear_image_cache_count = 0;
11468 }
11469 #endif /* HAVE_WINDOW_SYSTEM */
11470 }
11471
11472 end_of_redisplay:
11473 unbind_to (count, Qnil);
11474 RESUME_POLLING;
11475 }
11476
11477
11478 /* Redisplay, but leave alone any recent echo area message unless
11479 another message has been requested in its place.
11480
11481 This is useful in situations where you need to redisplay but no
11482 user action has occurred, making it inappropriate for the message
11483 area to be cleared. See tracking_off and
11484 wait_reading_process_output for examples of these situations.
11485
11486 FROM_WHERE is an integer saying from where this function was
11487 called. This is useful for debugging. */
11488
11489 void
11490 redisplay_preserve_echo_area (from_where)
11491 int from_where;
11492 {
11493 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11494
11495 if (!NILP (echo_area_buffer[1]))
11496 {
11497 /* We have a previously displayed message, but no current
11498 message. Redisplay the previous message. */
11499 display_last_displayed_message_p = 1;
11500 redisplay_internal (1);
11501 display_last_displayed_message_p = 0;
11502 }
11503 else
11504 redisplay_internal (1);
11505
11506 if (rif != NULL && rif->flush_display_optional)
11507 rif->flush_display_optional (NULL);
11508 }
11509
11510
11511 /* Function registered with record_unwind_protect in
11512 redisplay_internal. Reset redisplaying_p to the value it had
11513 before redisplay_internal was called, and clear
11514 prevent_freeing_realized_faces_p. It also selects the previously
11515 selected frame. */
11516
11517 static Lisp_Object
11518 unwind_redisplay (val)
11519 Lisp_Object val;
11520 {
11521 Lisp_Object old_redisplaying_p, old_frame;
11522
11523 old_redisplaying_p = XCAR (val);
11524 redisplaying_p = XFASTINT (old_redisplaying_p);
11525 old_frame = XCDR (val);
11526 if (! EQ (old_frame, selected_frame))
11527 select_frame_for_redisplay (old_frame);
11528 return Qnil;
11529 }
11530
11531
11532 /* Mark the display of window W as accurate or inaccurate. If
11533 ACCURATE_P is non-zero mark display of W as accurate. If
11534 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11535 redisplay_internal is called. */
11536
11537 static void
11538 mark_window_display_accurate_1 (w, accurate_p)
11539 struct window *w;
11540 int accurate_p;
11541 {
11542 if (BUFFERP (w->buffer))
11543 {
11544 struct buffer *b = XBUFFER (w->buffer);
11545
11546 w->last_modified
11547 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11548 w->last_overlay_modified
11549 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11550 w->last_had_star
11551 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11552
11553 if (accurate_p)
11554 {
11555 b->clip_changed = 0;
11556 b->prevent_redisplay_optimizations_p = 0;
11557
11558 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11559 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11560 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11561 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11562
11563 w->current_matrix->buffer = b;
11564 w->current_matrix->begv = BUF_BEGV (b);
11565 w->current_matrix->zv = BUF_ZV (b);
11566
11567 w->last_cursor = w->cursor;
11568 w->last_cursor_off_p = w->cursor_off_p;
11569
11570 if (w == XWINDOW (selected_window))
11571 w->last_point = make_number (BUF_PT (b));
11572 else
11573 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11574 }
11575 }
11576
11577 if (accurate_p)
11578 {
11579 w->window_end_valid = w->buffer;
11580 #if 0 /* This is incorrect with variable-height lines. */
11581 xassert (XINT (w->window_end_vpos)
11582 < (WINDOW_TOTAL_LINES (w)
11583 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11584 #endif
11585 w->update_mode_line = Qnil;
11586 }
11587 }
11588
11589
11590 /* Mark the display of windows in the window tree rooted at WINDOW as
11591 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11592 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11593 be redisplayed the next time redisplay_internal is called. */
11594
11595 void
11596 mark_window_display_accurate (window, accurate_p)
11597 Lisp_Object window;
11598 int accurate_p;
11599 {
11600 struct window *w;
11601
11602 for (; !NILP (window); window = w->next)
11603 {
11604 w = XWINDOW (window);
11605 mark_window_display_accurate_1 (w, accurate_p);
11606
11607 if (!NILP (w->vchild))
11608 mark_window_display_accurate (w->vchild, accurate_p);
11609 if (!NILP (w->hchild))
11610 mark_window_display_accurate (w->hchild, accurate_p);
11611 }
11612
11613 if (accurate_p)
11614 {
11615 update_overlay_arrows (1);
11616 }
11617 else
11618 {
11619 /* Force a thorough redisplay the next time by setting
11620 last_arrow_position and last_arrow_string to t, which is
11621 unequal to any useful value of Voverlay_arrow_... */
11622 update_overlay_arrows (-1);
11623 }
11624 }
11625
11626
11627 /* Return value in display table DP (Lisp_Char_Table *) for character
11628 C. Since a display table doesn't have any parent, we don't have to
11629 follow parent. Do not call this function directly but use the
11630 macro DISP_CHAR_VECTOR. */
11631
11632 Lisp_Object
11633 disp_char_vector (dp, c)
11634 struct Lisp_Char_Table *dp;
11635 int c;
11636 {
11637 Lisp_Object val;
11638
11639 if (ASCII_CHAR_P (c))
11640 {
11641 val = dp->ascii;
11642 if (SUB_CHAR_TABLE_P (val))
11643 val = XSUB_CHAR_TABLE (val)->contents[c];
11644 }
11645 else
11646 {
11647 Lisp_Object table;
11648
11649 XSETCHAR_TABLE (table, dp);
11650 val = char_table_ref (table, c);
11651 }
11652 if (NILP (val))
11653 val = dp->defalt;
11654 return val;
11655 }
11656
11657
11658 \f
11659 /***********************************************************************
11660 Window Redisplay
11661 ***********************************************************************/
11662
11663 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11664
11665 static void
11666 redisplay_windows (window)
11667 Lisp_Object window;
11668 {
11669 while (!NILP (window))
11670 {
11671 struct window *w = XWINDOW (window);
11672
11673 if (!NILP (w->hchild))
11674 redisplay_windows (w->hchild);
11675 else if (!NILP (w->vchild))
11676 redisplay_windows (w->vchild);
11677 else
11678 {
11679 displayed_buffer = XBUFFER (w->buffer);
11680 /* Use list_of_error, not Qerror, so that
11681 we catch only errors and don't run the debugger. */
11682 internal_condition_case_1 (redisplay_window_0, window,
11683 list_of_error,
11684 redisplay_window_error);
11685 }
11686
11687 window = w->next;
11688 }
11689 }
11690
11691 static Lisp_Object
11692 redisplay_window_error ()
11693 {
11694 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11695 return Qnil;
11696 }
11697
11698 static Lisp_Object
11699 redisplay_window_0 (window)
11700 Lisp_Object window;
11701 {
11702 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11703 redisplay_window (window, 0);
11704 return Qnil;
11705 }
11706
11707 static Lisp_Object
11708 redisplay_window_1 (window)
11709 Lisp_Object window;
11710 {
11711 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11712 redisplay_window (window, 1);
11713 return Qnil;
11714 }
11715 \f
11716
11717 /* Increment GLYPH until it reaches END or CONDITION fails while
11718 adding (GLYPH)->pixel_width to X. */
11719
11720 #define SKIP_GLYPHS(glyph, end, x, condition) \
11721 do \
11722 { \
11723 (x) += (glyph)->pixel_width; \
11724 ++(glyph); \
11725 } \
11726 while ((glyph) < (end) && (condition))
11727
11728
11729 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11730 DELTA is the number of bytes by which positions recorded in ROW
11731 differ from current buffer positions.
11732
11733 Return 0 if cursor is not on this row. 1 otherwise. */
11734
11735 int
11736 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11737 struct window *w;
11738 struct glyph_row *row;
11739 struct glyph_matrix *matrix;
11740 int delta, delta_bytes, dy, dvpos;
11741 {
11742 struct glyph *glyph = row->glyphs[TEXT_AREA];
11743 struct glyph *end = glyph + row->used[TEXT_AREA];
11744 struct glyph *cursor = NULL;
11745 /* The first glyph that starts a sequence of glyphs from string. */
11746 struct glyph *string_start;
11747 /* The X coordinate of string_start. */
11748 int string_start_x;
11749 /* The last known character position. */
11750 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11751 /* The last known character position before string_start. */
11752 int string_before_pos;
11753 int x = row->x;
11754 int cursor_x = x;
11755 int cursor_from_overlay_pos = 0;
11756 int pt_old = PT - delta;
11757
11758 /* Skip over glyphs not having an object at the start of the row.
11759 These are special glyphs like truncation marks on terminal
11760 frames. */
11761 if (row->displays_text_p)
11762 while (glyph < end
11763 && INTEGERP (glyph->object)
11764 && glyph->charpos < 0)
11765 {
11766 x += glyph->pixel_width;
11767 ++glyph;
11768 }
11769
11770 string_start = NULL;
11771 while (glyph < end
11772 && !INTEGERP (glyph->object)
11773 && (!BUFFERP (glyph->object)
11774 || (last_pos = glyph->charpos) < pt_old))
11775 {
11776 if (! STRINGP (glyph->object))
11777 {
11778 string_start = NULL;
11779 x += glyph->pixel_width;
11780 ++glyph;
11781 if (cursor_from_overlay_pos
11782 && last_pos >= cursor_from_overlay_pos)
11783 {
11784 cursor_from_overlay_pos = 0;
11785 cursor = 0;
11786 }
11787 }
11788 else
11789 {
11790 string_before_pos = last_pos;
11791 string_start = glyph;
11792 string_start_x = x;
11793 /* Skip all glyphs from string. */
11794 do
11795 {
11796 Lisp_Object cprop;
11797 int pos;
11798 if ((cursor == NULL || glyph > cursor)
11799 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11800 Qcursor, (glyph)->object),
11801 !NILP (cprop))
11802 && (pos = string_buffer_position (w, glyph->object,
11803 string_before_pos),
11804 (pos == 0 /* From overlay */
11805 || pos == pt_old)))
11806 {
11807 /* Estimate overlay buffer position from the buffer
11808 positions of the glyphs before and after the overlay.
11809 Add 1 to last_pos so that if point corresponds to the
11810 glyph right after the overlay, we still use a 'cursor'
11811 property found in that overlay. */
11812 cursor_from_overlay_pos = (pos ? 0 : last_pos
11813 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11814 cursor = glyph;
11815 cursor_x = x;
11816 }
11817 x += glyph->pixel_width;
11818 ++glyph;
11819 }
11820 while (glyph < end && EQ (glyph->object, string_start->object));
11821 }
11822 }
11823
11824 if (cursor != NULL)
11825 {
11826 glyph = cursor;
11827 x = cursor_x;
11828 }
11829 else if (row->ends_in_ellipsis_p && glyph == end)
11830 {
11831 /* Scan back over the ellipsis glyphs, decrementing positions. */
11832 while (glyph > row->glyphs[TEXT_AREA]
11833 && (glyph - 1)->charpos == last_pos)
11834 glyph--, x -= glyph->pixel_width;
11835 /* That loop always goes one position too far,
11836 including the glyph before the ellipsis.
11837 So scan forward over that one. */
11838 x += glyph->pixel_width;
11839 glyph++;
11840 }
11841 else if (string_start
11842 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11843 {
11844 /* We may have skipped over point because the previous glyphs
11845 are from string. As there's no easy way to know the
11846 character position of the current glyph, find the correct
11847 glyph on point by scanning from string_start again. */
11848 Lisp_Object limit;
11849 Lisp_Object string;
11850 int pos;
11851
11852 limit = make_number (pt_old + 1);
11853 end = glyph;
11854 glyph = string_start;
11855 x = string_start_x;
11856 string = glyph->object;
11857 pos = string_buffer_position (w, string, string_before_pos);
11858 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11859 because we always put cursor after overlay strings. */
11860 while (pos == 0 && glyph < end)
11861 {
11862 string = glyph->object;
11863 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11864 if (glyph < end)
11865 pos = string_buffer_position (w, glyph->object, string_before_pos);
11866 }
11867
11868 while (glyph < end)
11869 {
11870 pos = XINT (Fnext_single_char_property_change
11871 (make_number (pos), Qdisplay, Qnil, limit));
11872 if (pos > pt_old)
11873 break;
11874 /* Skip glyphs from the same string. */
11875 string = glyph->object;
11876 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11877 /* Skip glyphs from an overlay. */
11878 while (glyph < end
11879 && ! string_buffer_position (w, glyph->object, pos))
11880 {
11881 string = glyph->object;
11882 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11883 }
11884 }
11885
11886 /* If we reached the end of the line, and end was from a string,
11887 cursor is not on this line. */
11888 if (glyph == end && row->continued_p)
11889 return 0;
11890 }
11891
11892 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11893 w->cursor.x = x;
11894 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11895 w->cursor.y = row->y + dy;
11896
11897 if (w == XWINDOW (selected_window))
11898 {
11899 if (!row->continued_p
11900 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11901 && row->x == 0)
11902 {
11903 this_line_buffer = XBUFFER (w->buffer);
11904
11905 CHARPOS (this_line_start_pos)
11906 = MATRIX_ROW_START_CHARPOS (row) + delta;
11907 BYTEPOS (this_line_start_pos)
11908 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11909
11910 CHARPOS (this_line_end_pos)
11911 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11912 BYTEPOS (this_line_end_pos)
11913 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11914
11915 this_line_y = w->cursor.y;
11916 this_line_pixel_height = row->height;
11917 this_line_vpos = w->cursor.vpos;
11918 this_line_start_x = row->x;
11919 }
11920 else
11921 CHARPOS (this_line_start_pos) = 0;
11922 }
11923
11924 return 1;
11925 }
11926
11927
11928 /* Run window scroll functions, if any, for WINDOW with new window
11929 start STARTP. Sets the window start of WINDOW to that position.
11930
11931 We assume that the window's buffer is really current. */
11932
11933 static INLINE struct text_pos
11934 run_window_scroll_functions (window, startp)
11935 Lisp_Object window;
11936 struct text_pos startp;
11937 {
11938 struct window *w = XWINDOW (window);
11939 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11940
11941 if (current_buffer != XBUFFER (w->buffer))
11942 abort ();
11943
11944 if (!NILP (Vwindow_scroll_functions))
11945 {
11946 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11947 make_number (CHARPOS (startp)));
11948 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11949 /* In case the hook functions switch buffers. */
11950 if (current_buffer != XBUFFER (w->buffer))
11951 set_buffer_internal_1 (XBUFFER (w->buffer));
11952 }
11953
11954 return startp;
11955 }
11956
11957
11958 /* Make sure the line containing the cursor is fully visible.
11959 A value of 1 means there is nothing to be done.
11960 (Either the line is fully visible, or it cannot be made so,
11961 or we cannot tell.)
11962
11963 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11964 is higher than window.
11965
11966 A value of 0 means the caller should do scrolling
11967 as if point had gone off the screen. */
11968
11969 static int
11970 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11971 struct window *w;
11972 int force_p;
11973 int current_matrix_p;
11974 {
11975 struct glyph_matrix *matrix;
11976 struct glyph_row *row;
11977 int window_height;
11978
11979 if (!make_cursor_line_fully_visible_p)
11980 return 1;
11981
11982 /* It's not always possible to find the cursor, e.g, when a window
11983 is full of overlay strings. Don't do anything in that case. */
11984 if (w->cursor.vpos < 0)
11985 return 1;
11986
11987 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11988 row = MATRIX_ROW (matrix, w->cursor.vpos);
11989
11990 /* If the cursor row is not partially visible, there's nothing to do. */
11991 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11992 return 1;
11993
11994 /* If the row the cursor is in is taller than the window's height,
11995 it's not clear what to do, so do nothing. */
11996 window_height = window_box_height (w);
11997 if (row->height >= window_height)
11998 {
11999 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
12000 return 1;
12001 }
12002 return 0;
12003
12004 #if 0
12005 /* This code used to try to scroll the window just enough to make
12006 the line visible. It returned 0 to say that the caller should
12007 allocate larger glyph matrices. */
12008
12009 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12010 {
12011 int dy = row->height - row->visible_height;
12012 w->vscroll = 0;
12013 w->cursor.y += dy;
12014 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12015 }
12016 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12017 {
12018 int dy = - (row->height - row->visible_height);
12019 w->vscroll = dy;
12020 w->cursor.y += dy;
12021 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12022 }
12023
12024 /* When we change the cursor y-position of the selected window,
12025 change this_line_y as well so that the display optimization for
12026 the cursor line of the selected window in redisplay_internal uses
12027 the correct y-position. */
12028 if (w == XWINDOW (selected_window))
12029 this_line_y = w->cursor.y;
12030
12031 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12032 redisplay with larger matrices. */
12033 if (matrix->nrows < required_matrix_height (w))
12034 {
12035 fonts_changed_p = 1;
12036 return 0;
12037 }
12038
12039 return 1;
12040 #endif /* 0 */
12041 }
12042
12043
12044 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12045 non-zero means only WINDOW is redisplayed in redisplay_internal.
12046 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12047 in redisplay_window to bring a partially visible line into view in
12048 the case that only the cursor has moved.
12049
12050 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12051 last screen line's vertical height extends past the end of the screen.
12052
12053 Value is
12054
12055 1 if scrolling succeeded
12056
12057 0 if scrolling didn't find point.
12058
12059 -1 if new fonts have been loaded so that we must interrupt
12060 redisplay, adjust glyph matrices, and try again. */
12061
12062 enum
12063 {
12064 SCROLLING_SUCCESS,
12065 SCROLLING_FAILED,
12066 SCROLLING_NEED_LARGER_MATRICES
12067 };
12068
12069 static int
12070 try_scrolling (window, just_this_one_p, scroll_conservatively,
12071 scroll_step, temp_scroll_step, last_line_misfit)
12072 Lisp_Object window;
12073 int just_this_one_p;
12074 EMACS_INT scroll_conservatively, scroll_step;
12075 int temp_scroll_step;
12076 int last_line_misfit;
12077 {
12078 struct window *w = XWINDOW (window);
12079 struct frame *f = XFRAME (w->frame);
12080 struct text_pos scroll_margin_pos;
12081 struct text_pos pos;
12082 struct text_pos startp;
12083 struct it it;
12084 Lisp_Object window_end;
12085 int this_scroll_margin;
12086 int dy = 0;
12087 int scroll_max;
12088 int rc;
12089 int amount_to_scroll = 0;
12090 Lisp_Object aggressive;
12091 int height;
12092 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12093
12094 #if GLYPH_DEBUG
12095 debug_method_add (w, "try_scrolling");
12096 #endif
12097
12098 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12099
12100 /* Compute scroll margin height in pixels. We scroll when point is
12101 within this distance from the top or bottom of the window. */
12102 if (scroll_margin > 0)
12103 {
12104 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12105 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12106 }
12107 else
12108 this_scroll_margin = 0;
12109
12110 /* Force scroll_conservatively to have a reasonable value so it doesn't
12111 cause an overflow while computing how much to scroll. */
12112 if (scroll_conservatively)
12113 scroll_conservatively = min (scroll_conservatively,
12114 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12115
12116 /* Compute how much we should try to scroll maximally to bring point
12117 into view. */
12118 if (scroll_step || scroll_conservatively || temp_scroll_step)
12119 scroll_max = max (scroll_step,
12120 max (scroll_conservatively, temp_scroll_step));
12121 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12122 || NUMBERP (current_buffer->scroll_up_aggressively))
12123 /* We're trying to scroll because of aggressive scrolling
12124 but no scroll_step is set. Choose an arbitrary one. Maybe
12125 there should be a variable for this. */
12126 scroll_max = 10;
12127 else
12128 scroll_max = 0;
12129 scroll_max *= FRAME_LINE_HEIGHT (f);
12130
12131 /* Decide whether we have to scroll down. Start at the window end
12132 and move this_scroll_margin up to find the position of the scroll
12133 margin. */
12134 window_end = Fwindow_end (window, Qt);
12135
12136 too_near_end:
12137
12138 CHARPOS (scroll_margin_pos) = XINT (window_end);
12139 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12140
12141 if (this_scroll_margin || extra_scroll_margin_lines)
12142 {
12143 start_display (&it, w, scroll_margin_pos);
12144 if (this_scroll_margin)
12145 move_it_vertically_backward (&it, this_scroll_margin);
12146 if (extra_scroll_margin_lines)
12147 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12148 scroll_margin_pos = it.current.pos;
12149 }
12150
12151 if (PT >= CHARPOS (scroll_margin_pos))
12152 {
12153 int y0;
12154
12155 /* Point is in the scroll margin at the bottom of the window, or
12156 below. Compute a new window start that makes point visible. */
12157
12158 /* Compute the distance from the scroll margin to PT.
12159 Give up if the distance is greater than scroll_max. */
12160 start_display (&it, w, scroll_margin_pos);
12161 y0 = it.current_y;
12162 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12163 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12164
12165 /* To make point visible, we have to move the window start
12166 down so that the line the cursor is in is visible, which
12167 means we have to add in the height of the cursor line. */
12168 dy = line_bottom_y (&it) - y0;
12169
12170 if (dy > scroll_max)
12171 return SCROLLING_FAILED;
12172
12173 /* Move the window start down. If scrolling conservatively,
12174 move it just enough down to make point visible. If
12175 scroll_step is set, move it down by scroll_step. */
12176 start_display (&it, w, startp);
12177
12178 if (scroll_conservatively)
12179 /* Set AMOUNT_TO_SCROLL to at least one line,
12180 and at most scroll_conservatively lines. */
12181 amount_to_scroll
12182 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12183 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12184 else if (scroll_step || temp_scroll_step)
12185 amount_to_scroll = scroll_max;
12186 else
12187 {
12188 aggressive = current_buffer->scroll_up_aggressively;
12189 height = WINDOW_BOX_TEXT_HEIGHT (w);
12190 if (NUMBERP (aggressive))
12191 {
12192 double float_amount = XFLOATINT (aggressive) * height;
12193 amount_to_scroll = float_amount;
12194 if (amount_to_scroll == 0 && float_amount > 0)
12195 amount_to_scroll = 1;
12196 }
12197 }
12198
12199 if (amount_to_scroll <= 0)
12200 return SCROLLING_FAILED;
12201
12202 /* If moving by amount_to_scroll leaves STARTP unchanged,
12203 move it down one screen line. */
12204
12205 move_it_vertically (&it, amount_to_scroll);
12206 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12207 move_it_by_lines (&it, 1, 1);
12208 startp = it.current.pos;
12209 }
12210 else
12211 {
12212 /* See if point is inside the scroll margin at the top of the
12213 window. */
12214 scroll_margin_pos = startp;
12215 if (this_scroll_margin)
12216 {
12217 start_display (&it, w, startp);
12218 move_it_vertically (&it, this_scroll_margin);
12219 scroll_margin_pos = it.current.pos;
12220 }
12221
12222 if (PT < CHARPOS (scroll_margin_pos))
12223 {
12224 /* Point is in the scroll margin at the top of the window or
12225 above what is displayed in the window. */
12226 int y0;
12227
12228 /* Compute the vertical distance from PT to the scroll
12229 margin position. Give up if distance is greater than
12230 scroll_max. */
12231 SET_TEXT_POS (pos, PT, PT_BYTE);
12232 start_display (&it, w, pos);
12233 y0 = it.current_y;
12234 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12235 it.last_visible_y, -1,
12236 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12237 dy = it.current_y - y0;
12238 if (dy > scroll_max)
12239 return SCROLLING_FAILED;
12240
12241 /* Compute new window start. */
12242 start_display (&it, w, startp);
12243
12244 if (scroll_conservatively)
12245 amount_to_scroll
12246 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12247 else if (scroll_step || temp_scroll_step)
12248 amount_to_scroll = scroll_max;
12249 else
12250 {
12251 aggressive = current_buffer->scroll_down_aggressively;
12252 height = WINDOW_BOX_TEXT_HEIGHT (w);
12253 if (NUMBERP (aggressive))
12254 {
12255 double float_amount = XFLOATINT (aggressive) * height;
12256 amount_to_scroll = float_amount;
12257 if (amount_to_scroll == 0 && float_amount > 0)
12258 amount_to_scroll = 1;
12259 }
12260 }
12261
12262 if (amount_to_scroll <= 0)
12263 return SCROLLING_FAILED;
12264
12265 move_it_vertically_backward (&it, amount_to_scroll);
12266 startp = it.current.pos;
12267 }
12268 }
12269
12270 /* Run window scroll functions. */
12271 startp = run_window_scroll_functions (window, startp);
12272
12273 /* Display the window. Give up if new fonts are loaded, or if point
12274 doesn't appear. */
12275 if (!try_window (window, startp, 0))
12276 rc = SCROLLING_NEED_LARGER_MATRICES;
12277 else if (w->cursor.vpos < 0)
12278 {
12279 clear_glyph_matrix (w->desired_matrix);
12280 rc = SCROLLING_FAILED;
12281 }
12282 else
12283 {
12284 /* Maybe forget recorded base line for line number display. */
12285 if (!just_this_one_p
12286 || current_buffer->clip_changed
12287 || BEG_UNCHANGED < CHARPOS (startp))
12288 w->base_line_number = Qnil;
12289
12290 /* If cursor ends up on a partially visible line,
12291 treat that as being off the bottom of the screen. */
12292 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12293 {
12294 clear_glyph_matrix (w->desired_matrix);
12295 ++extra_scroll_margin_lines;
12296 goto too_near_end;
12297 }
12298 rc = SCROLLING_SUCCESS;
12299 }
12300
12301 return rc;
12302 }
12303
12304
12305 /* Compute a suitable window start for window W if display of W starts
12306 on a continuation line. Value is non-zero if a new window start
12307 was computed.
12308
12309 The new window start will be computed, based on W's width, starting
12310 from the start of the continued line. It is the start of the
12311 screen line with the minimum distance from the old start W->start. */
12312
12313 static int
12314 compute_window_start_on_continuation_line (w)
12315 struct window *w;
12316 {
12317 struct text_pos pos, start_pos;
12318 int window_start_changed_p = 0;
12319
12320 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12321
12322 /* If window start is on a continuation line... Window start may be
12323 < BEGV in case there's invisible text at the start of the
12324 buffer (M-x rmail, for example). */
12325 if (CHARPOS (start_pos) > BEGV
12326 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12327 {
12328 struct it it;
12329 struct glyph_row *row;
12330
12331 /* Handle the case that the window start is out of range. */
12332 if (CHARPOS (start_pos) < BEGV)
12333 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12334 else if (CHARPOS (start_pos) > ZV)
12335 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12336
12337 /* Find the start of the continued line. This should be fast
12338 because scan_buffer is fast (newline cache). */
12339 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12340 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12341 row, DEFAULT_FACE_ID);
12342 reseat_at_previous_visible_line_start (&it);
12343
12344 /* If the line start is "too far" away from the window start,
12345 say it takes too much time to compute a new window start. */
12346 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12347 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12348 {
12349 int min_distance, distance;
12350
12351 /* Move forward by display lines to find the new window
12352 start. If window width was enlarged, the new start can
12353 be expected to be > the old start. If window width was
12354 decreased, the new window start will be < the old start.
12355 So, we're looking for the display line start with the
12356 minimum distance from the old window start. */
12357 pos = it.current.pos;
12358 min_distance = INFINITY;
12359 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12360 distance < min_distance)
12361 {
12362 min_distance = distance;
12363 pos = it.current.pos;
12364 move_it_by_lines (&it, 1, 0);
12365 }
12366
12367 /* Set the window start there. */
12368 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12369 window_start_changed_p = 1;
12370 }
12371 }
12372
12373 return window_start_changed_p;
12374 }
12375
12376
12377 /* Try cursor movement in case text has not changed in window WINDOW,
12378 with window start STARTP. Value is
12379
12380 CURSOR_MOVEMENT_SUCCESS if successful
12381
12382 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12383
12384 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12385 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12386 we want to scroll as if scroll-step were set to 1. See the code.
12387
12388 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12389 which case we have to abort this redisplay, and adjust matrices
12390 first. */
12391
12392 enum
12393 {
12394 CURSOR_MOVEMENT_SUCCESS,
12395 CURSOR_MOVEMENT_CANNOT_BE_USED,
12396 CURSOR_MOVEMENT_MUST_SCROLL,
12397 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12398 };
12399
12400 static int
12401 try_cursor_movement (window, startp, scroll_step)
12402 Lisp_Object window;
12403 struct text_pos startp;
12404 int *scroll_step;
12405 {
12406 struct window *w = XWINDOW (window);
12407 struct frame *f = XFRAME (w->frame);
12408 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12409
12410 #if GLYPH_DEBUG
12411 if (inhibit_try_cursor_movement)
12412 return rc;
12413 #endif
12414
12415 /* Handle case where text has not changed, only point, and it has
12416 not moved off the frame. */
12417 if (/* Point may be in this window. */
12418 PT >= CHARPOS (startp)
12419 /* Selective display hasn't changed. */
12420 && !current_buffer->clip_changed
12421 /* Function force-mode-line-update is used to force a thorough
12422 redisplay. It sets either windows_or_buffers_changed or
12423 update_mode_lines. So don't take a shortcut here for these
12424 cases. */
12425 && !update_mode_lines
12426 && !windows_or_buffers_changed
12427 && !cursor_type_changed
12428 /* Can't use this case if highlighting a region. When a
12429 region exists, cursor movement has to do more than just
12430 set the cursor. */
12431 && !(!NILP (Vtransient_mark_mode)
12432 && !NILP (current_buffer->mark_active))
12433 && NILP (w->region_showing)
12434 && NILP (Vshow_trailing_whitespace)
12435 /* Right after splitting windows, last_point may be nil. */
12436 && INTEGERP (w->last_point)
12437 /* This code is not used for mini-buffer for the sake of the case
12438 of redisplaying to replace an echo area message; since in
12439 that case the mini-buffer contents per se are usually
12440 unchanged. This code is of no real use in the mini-buffer
12441 since the handling of this_line_start_pos, etc., in redisplay
12442 handles the same cases. */
12443 && !EQ (window, minibuf_window)
12444 /* When splitting windows or for new windows, it happens that
12445 redisplay is called with a nil window_end_vpos or one being
12446 larger than the window. This should really be fixed in
12447 window.c. I don't have this on my list, now, so we do
12448 approximately the same as the old redisplay code. --gerd. */
12449 && INTEGERP (w->window_end_vpos)
12450 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12451 && (FRAME_WINDOW_P (f)
12452 || !overlay_arrow_in_current_buffer_p ()))
12453 {
12454 int this_scroll_margin, top_scroll_margin;
12455 struct glyph_row *row = NULL;
12456
12457 #if GLYPH_DEBUG
12458 debug_method_add (w, "cursor movement");
12459 #endif
12460
12461 /* Scroll if point within this distance from the top or bottom
12462 of the window. This is a pixel value. */
12463 this_scroll_margin = max (0, scroll_margin);
12464 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12465 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12466
12467 top_scroll_margin = this_scroll_margin;
12468 if (WINDOW_WANTS_HEADER_LINE_P (w))
12469 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12470
12471 /* Start with the row the cursor was displayed during the last
12472 not paused redisplay. Give up if that row is not valid. */
12473 if (w->last_cursor.vpos < 0
12474 || w->last_cursor.vpos >= w->current_matrix->nrows)
12475 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12476 else
12477 {
12478 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12479 if (row->mode_line_p)
12480 ++row;
12481 if (!row->enabled_p)
12482 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12483 }
12484
12485 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12486 {
12487 int scroll_p = 0;
12488 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12489
12490 if (PT > XFASTINT (w->last_point))
12491 {
12492 /* Point has moved forward. */
12493 while (MATRIX_ROW_END_CHARPOS (row) < PT
12494 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12495 {
12496 xassert (row->enabled_p);
12497 ++row;
12498 }
12499
12500 /* The end position of a row equals the start position
12501 of the next row. If PT is there, we would rather
12502 display it in the next line. */
12503 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12504 && MATRIX_ROW_END_CHARPOS (row) == PT
12505 && !cursor_row_p (w, row))
12506 ++row;
12507
12508 /* If within the scroll margin, scroll. Note that
12509 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12510 the next line would be drawn, and that
12511 this_scroll_margin can be zero. */
12512 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12513 || PT > MATRIX_ROW_END_CHARPOS (row)
12514 /* Line is completely visible last line in window
12515 and PT is to be set in the next line. */
12516 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12517 && PT == MATRIX_ROW_END_CHARPOS (row)
12518 && !row->ends_at_zv_p
12519 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12520 scroll_p = 1;
12521 }
12522 else if (PT < XFASTINT (w->last_point))
12523 {
12524 /* Cursor has to be moved backward. Note that PT >=
12525 CHARPOS (startp) because of the outer if-statement. */
12526 while (!row->mode_line_p
12527 && (MATRIX_ROW_START_CHARPOS (row) > PT
12528 || (MATRIX_ROW_START_CHARPOS (row) == PT
12529 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12530 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12531 row > w->current_matrix->rows
12532 && (row-1)->ends_in_newline_from_string_p))))
12533 && (row->y > top_scroll_margin
12534 || CHARPOS (startp) == BEGV))
12535 {
12536 xassert (row->enabled_p);
12537 --row;
12538 }
12539
12540 /* Consider the following case: Window starts at BEGV,
12541 there is invisible, intangible text at BEGV, so that
12542 display starts at some point START > BEGV. It can
12543 happen that we are called with PT somewhere between
12544 BEGV and START. Try to handle that case. */
12545 if (row < w->current_matrix->rows
12546 || row->mode_line_p)
12547 {
12548 row = w->current_matrix->rows;
12549 if (row->mode_line_p)
12550 ++row;
12551 }
12552
12553 /* Due to newlines in overlay strings, we may have to
12554 skip forward over overlay strings. */
12555 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12556 && MATRIX_ROW_END_CHARPOS (row) == PT
12557 && !cursor_row_p (w, row))
12558 ++row;
12559
12560 /* If within the scroll margin, scroll. */
12561 if (row->y < top_scroll_margin
12562 && CHARPOS (startp) != BEGV)
12563 scroll_p = 1;
12564 }
12565 else
12566 {
12567 /* Cursor did not move. So don't scroll even if cursor line
12568 is partially visible, as it was so before. */
12569 rc = CURSOR_MOVEMENT_SUCCESS;
12570 }
12571
12572 if (PT < MATRIX_ROW_START_CHARPOS (row)
12573 || PT > MATRIX_ROW_END_CHARPOS (row))
12574 {
12575 /* if PT is not in the glyph row, give up. */
12576 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12577 }
12578 else if (rc != CURSOR_MOVEMENT_SUCCESS
12579 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12580 && make_cursor_line_fully_visible_p)
12581 {
12582 if (PT == MATRIX_ROW_END_CHARPOS (row)
12583 && !row->ends_at_zv_p
12584 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12585 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12586 else if (row->height > window_box_height (w))
12587 {
12588 /* If we end up in a partially visible line, let's
12589 make it fully visible, except when it's taller
12590 than the window, in which case we can't do much
12591 about it. */
12592 *scroll_step = 1;
12593 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12594 }
12595 else
12596 {
12597 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12598 if (!cursor_row_fully_visible_p (w, 0, 1))
12599 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12600 else
12601 rc = CURSOR_MOVEMENT_SUCCESS;
12602 }
12603 }
12604 else if (scroll_p)
12605 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12606 else
12607 {
12608 do
12609 {
12610 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12611 {
12612 rc = CURSOR_MOVEMENT_SUCCESS;
12613 break;
12614 }
12615 ++row;
12616 }
12617 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12618 && MATRIX_ROW_START_CHARPOS (row) == PT
12619 && cursor_row_p (w, row));
12620 }
12621 }
12622 }
12623
12624 return rc;
12625 }
12626
12627 void
12628 set_vertical_scroll_bar (w)
12629 struct window *w;
12630 {
12631 int start, end, whole;
12632
12633 /* Calculate the start and end positions for the current window.
12634 At some point, it would be nice to choose between scrollbars
12635 which reflect the whole buffer size, with special markers
12636 indicating narrowing, and scrollbars which reflect only the
12637 visible region.
12638
12639 Note that mini-buffers sometimes aren't displaying any text. */
12640 if (!MINI_WINDOW_P (w)
12641 || (w == XWINDOW (minibuf_window)
12642 && NILP (echo_area_buffer[0])))
12643 {
12644 struct buffer *buf = XBUFFER (w->buffer);
12645 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12646 start = marker_position (w->start) - BUF_BEGV (buf);
12647 /* I don't think this is guaranteed to be right. For the
12648 moment, we'll pretend it is. */
12649 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12650
12651 if (end < start)
12652 end = start;
12653 if (whole < (end - start))
12654 whole = end - start;
12655 }
12656 else
12657 start = end = whole = 0;
12658
12659 /* Indicate what this scroll bar ought to be displaying now. */
12660 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12661 }
12662
12663
12664 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12665 selected_window is redisplayed.
12666
12667 We can return without actually redisplaying the window if
12668 fonts_changed_p is nonzero. In that case, redisplay_internal will
12669 retry. */
12670
12671 static void
12672 redisplay_window (window, just_this_one_p)
12673 Lisp_Object window;
12674 int just_this_one_p;
12675 {
12676 struct window *w = XWINDOW (window);
12677 struct frame *f = XFRAME (w->frame);
12678 struct buffer *buffer = XBUFFER (w->buffer);
12679 struct buffer *old = current_buffer;
12680 struct text_pos lpoint, opoint, startp;
12681 int update_mode_line;
12682 int tem;
12683 struct it it;
12684 /* Record it now because it's overwritten. */
12685 int current_matrix_up_to_date_p = 0;
12686 int used_current_matrix_p = 0;
12687 /* This is less strict than current_matrix_up_to_date_p.
12688 It indictes that the buffer contents and narrowing are unchanged. */
12689 int buffer_unchanged_p = 0;
12690 int temp_scroll_step = 0;
12691 int count = SPECPDL_INDEX ();
12692 int rc;
12693 int centering_position = -1;
12694 int last_line_misfit = 0;
12695
12696 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12697 opoint = lpoint;
12698
12699 /* W must be a leaf window here. */
12700 xassert (!NILP (w->buffer));
12701 #if GLYPH_DEBUG
12702 *w->desired_matrix->method = 0;
12703 #endif
12704
12705 specbind (Qinhibit_point_motion_hooks, Qt);
12706
12707 reconsider_clip_changes (w, buffer);
12708
12709 /* Has the mode line to be updated? */
12710 update_mode_line = (!NILP (w->update_mode_line)
12711 || update_mode_lines
12712 || buffer->clip_changed
12713 || buffer->prevent_redisplay_optimizations_p);
12714
12715 if (MINI_WINDOW_P (w))
12716 {
12717 if (w == XWINDOW (echo_area_window)
12718 && !NILP (echo_area_buffer[0]))
12719 {
12720 if (update_mode_line)
12721 /* We may have to update a tty frame's menu bar or a
12722 tool-bar. Example `M-x C-h C-h C-g'. */
12723 goto finish_menu_bars;
12724 else
12725 /* We've already displayed the echo area glyphs in this window. */
12726 goto finish_scroll_bars;
12727 }
12728 else if ((w != XWINDOW (minibuf_window)
12729 || minibuf_level == 0)
12730 /* When buffer is nonempty, redisplay window normally. */
12731 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12732 /* Quail displays non-mini buffers in minibuffer window.
12733 In that case, redisplay the window normally. */
12734 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12735 {
12736 /* W is a mini-buffer window, but it's not active, so clear
12737 it. */
12738 int yb = window_text_bottom_y (w);
12739 struct glyph_row *row;
12740 int y;
12741
12742 for (y = 0, row = w->desired_matrix->rows;
12743 y < yb;
12744 y += row->height, ++row)
12745 blank_row (w, row, y);
12746 goto finish_scroll_bars;
12747 }
12748
12749 clear_glyph_matrix (w->desired_matrix);
12750 }
12751
12752 /* Otherwise set up data on this window; select its buffer and point
12753 value. */
12754 /* Really select the buffer, for the sake of buffer-local
12755 variables. */
12756 set_buffer_internal_1 (XBUFFER (w->buffer));
12757 SET_TEXT_POS (opoint, PT, PT_BYTE);
12758
12759 current_matrix_up_to_date_p
12760 = (!NILP (w->window_end_valid)
12761 && !current_buffer->clip_changed
12762 && !current_buffer->prevent_redisplay_optimizations_p
12763 && XFASTINT (w->last_modified) >= MODIFF
12764 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12765
12766 buffer_unchanged_p
12767 = (!NILP (w->window_end_valid)
12768 && !current_buffer->clip_changed
12769 && XFASTINT (w->last_modified) >= MODIFF
12770 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12771
12772 /* When windows_or_buffers_changed is non-zero, we can't rely on
12773 the window end being valid, so set it to nil there. */
12774 if (windows_or_buffers_changed)
12775 {
12776 /* If window starts on a continuation line, maybe adjust the
12777 window start in case the window's width changed. */
12778 if (XMARKER (w->start)->buffer == current_buffer)
12779 compute_window_start_on_continuation_line (w);
12780
12781 w->window_end_valid = Qnil;
12782 }
12783
12784 /* Some sanity checks. */
12785 CHECK_WINDOW_END (w);
12786 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12787 abort ();
12788 if (BYTEPOS (opoint) < CHARPOS (opoint))
12789 abort ();
12790
12791 /* If %c is in mode line, update it if needed. */
12792 if (!NILP (w->column_number_displayed)
12793 /* This alternative quickly identifies a common case
12794 where no change is needed. */
12795 && !(PT == XFASTINT (w->last_point)
12796 && XFASTINT (w->last_modified) >= MODIFF
12797 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12798 && (XFASTINT (w->column_number_displayed)
12799 != (int) current_column ())) /* iftc */
12800 update_mode_line = 1;
12801
12802 /* Count number of windows showing the selected buffer. An indirect
12803 buffer counts as its base buffer. */
12804 if (!just_this_one_p)
12805 {
12806 struct buffer *current_base, *window_base;
12807 current_base = current_buffer;
12808 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12809 if (current_base->base_buffer)
12810 current_base = current_base->base_buffer;
12811 if (window_base->base_buffer)
12812 window_base = window_base->base_buffer;
12813 if (current_base == window_base)
12814 buffer_shared++;
12815 }
12816
12817 /* Point refers normally to the selected window. For any other
12818 window, set up appropriate value. */
12819 if (!EQ (window, selected_window))
12820 {
12821 int new_pt = XMARKER (w->pointm)->charpos;
12822 int new_pt_byte = marker_byte_position (w->pointm);
12823 if (new_pt < BEGV)
12824 {
12825 new_pt = BEGV;
12826 new_pt_byte = BEGV_BYTE;
12827 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12828 }
12829 else if (new_pt > (ZV - 1))
12830 {
12831 new_pt = ZV;
12832 new_pt_byte = ZV_BYTE;
12833 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12834 }
12835
12836 /* We don't use SET_PT so that the point-motion hooks don't run. */
12837 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12838 }
12839
12840 /* If any of the character widths specified in the display table
12841 have changed, invalidate the width run cache. It's true that
12842 this may be a bit late to catch such changes, but the rest of
12843 redisplay goes (non-fatally) haywire when the display table is
12844 changed, so why should we worry about doing any better? */
12845 if (current_buffer->width_run_cache)
12846 {
12847 struct Lisp_Char_Table *disptab = buffer_display_table ();
12848
12849 if (! disptab_matches_widthtab (disptab,
12850 XVECTOR (current_buffer->width_table)))
12851 {
12852 invalidate_region_cache (current_buffer,
12853 current_buffer->width_run_cache,
12854 BEG, Z);
12855 recompute_width_table (current_buffer, disptab);
12856 }
12857 }
12858
12859 /* If window-start is screwed up, choose a new one. */
12860 if (XMARKER (w->start)->buffer != current_buffer)
12861 goto recenter;
12862
12863 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12864
12865 /* If someone specified a new starting point but did not insist,
12866 check whether it can be used. */
12867 if (!NILP (w->optional_new_start)
12868 && CHARPOS (startp) >= BEGV
12869 && CHARPOS (startp) <= ZV)
12870 {
12871 w->optional_new_start = Qnil;
12872 start_display (&it, w, startp);
12873 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12874 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12875 if (IT_CHARPOS (it) == PT)
12876 w->force_start = Qt;
12877 /* IT may overshoot PT if text at PT is invisible. */
12878 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12879 w->force_start = Qt;
12880 }
12881
12882 /* Handle case where place to start displaying has been specified,
12883 unless the specified location is outside the accessible range. */
12884 if (!NILP (w->force_start)
12885 || w->frozen_window_start_p)
12886 {
12887 /* We set this later on if we have to adjust point. */
12888 int new_vpos = -1;
12889 int val;
12890
12891 w->force_start = Qnil;
12892 w->vscroll = 0;
12893 w->window_end_valid = Qnil;
12894
12895 /* Forget any recorded base line for line number display. */
12896 if (!buffer_unchanged_p)
12897 w->base_line_number = Qnil;
12898
12899 /* Redisplay the mode line. Select the buffer properly for that.
12900 Also, run the hook window-scroll-functions
12901 because we have scrolled. */
12902 /* Note, we do this after clearing force_start because
12903 if there's an error, it is better to forget about force_start
12904 than to get into an infinite loop calling the hook functions
12905 and having them get more errors. */
12906 if (!update_mode_line
12907 || ! NILP (Vwindow_scroll_functions))
12908 {
12909 update_mode_line = 1;
12910 w->update_mode_line = Qt;
12911 startp = run_window_scroll_functions (window, startp);
12912 }
12913
12914 w->last_modified = make_number (0);
12915 w->last_overlay_modified = make_number (0);
12916 if (CHARPOS (startp) < BEGV)
12917 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12918 else if (CHARPOS (startp) > ZV)
12919 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12920
12921 /* Redisplay, then check if cursor has been set during the
12922 redisplay. Give up if new fonts were loaded. */
12923 val = try_window (window, startp, 1);
12924 if (!val)
12925 {
12926 w->force_start = Qt;
12927 clear_glyph_matrix (w->desired_matrix);
12928 goto need_larger_matrices;
12929 }
12930 /* Point was outside the scroll margins. */
12931 if (val < 0)
12932 new_vpos = window_box_height (w) / 2;
12933
12934 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12935 {
12936 /* If point does not appear, try to move point so it does
12937 appear. The desired matrix has been built above, so we
12938 can use it here. */
12939 new_vpos = window_box_height (w) / 2;
12940 }
12941
12942 if (!cursor_row_fully_visible_p (w, 0, 0))
12943 {
12944 /* Point does appear, but on a line partly visible at end of window.
12945 Move it back to a fully-visible line. */
12946 new_vpos = window_box_height (w);
12947 }
12948
12949 /* If we need to move point for either of the above reasons,
12950 now actually do it. */
12951 if (new_vpos >= 0)
12952 {
12953 struct glyph_row *row;
12954
12955 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12956 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12957 ++row;
12958
12959 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12960 MATRIX_ROW_START_BYTEPOS (row));
12961
12962 if (w != XWINDOW (selected_window))
12963 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12964 else if (current_buffer == old)
12965 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12966
12967 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12968
12969 /* If we are highlighting the region, then we just changed
12970 the region, so redisplay to show it. */
12971 if (!NILP (Vtransient_mark_mode)
12972 && !NILP (current_buffer->mark_active))
12973 {
12974 clear_glyph_matrix (w->desired_matrix);
12975 if (!try_window (window, startp, 0))
12976 goto need_larger_matrices;
12977 }
12978 }
12979
12980 #if GLYPH_DEBUG
12981 debug_method_add (w, "forced window start");
12982 #endif
12983 goto done;
12984 }
12985
12986 /* Handle case where text has not changed, only point, and it has
12987 not moved off the frame, and we are not retrying after hscroll.
12988 (current_matrix_up_to_date_p is nonzero when retrying.) */
12989 if (current_matrix_up_to_date_p
12990 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12991 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12992 {
12993 switch (rc)
12994 {
12995 case CURSOR_MOVEMENT_SUCCESS:
12996 used_current_matrix_p = 1;
12997 goto done;
12998
12999 #if 0 /* try_cursor_movement never returns this value. */
13000 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13001 goto need_larger_matrices;
13002 #endif
13003
13004 case CURSOR_MOVEMENT_MUST_SCROLL:
13005 goto try_to_scroll;
13006
13007 default:
13008 abort ();
13009 }
13010 }
13011 /* If current starting point was originally the beginning of a line
13012 but no longer is, find a new starting point. */
13013 else if (!NILP (w->start_at_line_beg)
13014 && !(CHARPOS (startp) <= BEGV
13015 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13016 {
13017 #if GLYPH_DEBUG
13018 debug_method_add (w, "recenter 1");
13019 #endif
13020 goto recenter;
13021 }
13022
13023 /* Try scrolling with try_window_id. Value is > 0 if update has
13024 been done, it is -1 if we know that the same window start will
13025 not work. It is 0 if unsuccessful for some other reason. */
13026 else if ((tem = try_window_id (w)) != 0)
13027 {
13028 #if GLYPH_DEBUG
13029 debug_method_add (w, "try_window_id %d", tem);
13030 #endif
13031
13032 if (fonts_changed_p)
13033 goto need_larger_matrices;
13034 if (tem > 0)
13035 goto done;
13036
13037 /* Otherwise try_window_id has returned -1 which means that we
13038 don't want the alternative below this comment to execute. */
13039 }
13040 else if (CHARPOS (startp) >= BEGV
13041 && CHARPOS (startp) <= ZV
13042 && PT >= CHARPOS (startp)
13043 && (CHARPOS (startp) < ZV
13044 /* Avoid starting at end of buffer. */
13045 || CHARPOS (startp) == BEGV
13046 || (XFASTINT (w->last_modified) >= MODIFF
13047 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13048 {
13049
13050 /* If first window line is a continuation line, and window start
13051 is inside the modified region, but the first change is before
13052 current window start, we must select a new window start.*/
13053 if (NILP (w->start_at_line_beg)
13054 && CHARPOS (startp) > BEGV)
13055 {
13056 /* Make sure beg_unchanged and end_unchanged are up to date.
13057 Do it only if buffer has really changed. This may or may
13058 not have been done by try_window_id (see which) already. */
13059 if (MODIFF > SAVE_MODIFF
13060 /* This seems to happen sometimes after saving a buffer. */
13061 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13062 {
13063 if (GPT - BEG < BEG_UNCHANGED)
13064 BEG_UNCHANGED = GPT - BEG;
13065 if (Z - GPT < END_UNCHANGED)
13066 END_UNCHANGED = Z - GPT;
13067 }
13068
13069 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13070 && CHARPOS (startp) <= Z - END_UNCHANGED)
13071 {
13072 /* There doesn't seems to be a simple way to find a new
13073 window start that is near the old window start, so
13074 we just recenter. */
13075 goto recenter;
13076 }
13077 }
13078
13079 #if GLYPH_DEBUG
13080 debug_method_add (w, "same window start");
13081 #endif
13082
13083 /* Try to redisplay starting at same place as before.
13084 If point has not moved off frame, accept the results. */
13085 if (!current_matrix_up_to_date_p
13086 /* Don't use try_window_reusing_current_matrix in this case
13087 because a window scroll function can have changed the
13088 buffer. */
13089 || !NILP (Vwindow_scroll_functions)
13090 || MINI_WINDOW_P (w)
13091 || !(used_current_matrix_p
13092 = try_window_reusing_current_matrix (w)))
13093 {
13094 IF_DEBUG (debug_method_add (w, "1"));
13095 if (try_window (window, startp, 1) < 0)
13096 /* -1 means we need to scroll.
13097 0 means we need new matrices, but fonts_changed_p
13098 is set in that case, so we will detect it below. */
13099 goto try_to_scroll;
13100 }
13101
13102 if (fonts_changed_p)
13103 goto need_larger_matrices;
13104
13105 if (w->cursor.vpos >= 0)
13106 {
13107 if (!just_this_one_p
13108 || current_buffer->clip_changed
13109 || BEG_UNCHANGED < CHARPOS (startp))
13110 /* Forget any recorded base line for line number display. */
13111 w->base_line_number = Qnil;
13112
13113 if (!cursor_row_fully_visible_p (w, 1, 0))
13114 {
13115 clear_glyph_matrix (w->desired_matrix);
13116 last_line_misfit = 1;
13117 }
13118 /* Drop through and scroll. */
13119 else
13120 goto done;
13121 }
13122 else
13123 clear_glyph_matrix (w->desired_matrix);
13124 }
13125
13126 try_to_scroll:
13127
13128 w->last_modified = make_number (0);
13129 w->last_overlay_modified = make_number (0);
13130
13131 /* Redisplay the mode line. Select the buffer properly for that. */
13132 if (!update_mode_line)
13133 {
13134 update_mode_line = 1;
13135 w->update_mode_line = Qt;
13136 }
13137
13138 /* Try to scroll by specified few lines. */
13139 if ((scroll_conservatively
13140 || scroll_step
13141 || temp_scroll_step
13142 || NUMBERP (current_buffer->scroll_up_aggressively)
13143 || NUMBERP (current_buffer->scroll_down_aggressively))
13144 && !current_buffer->clip_changed
13145 && CHARPOS (startp) >= BEGV
13146 && CHARPOS (startp) <= ZV)
13147 {
13148 /* The function returns -1 if new fonts were loaded, 1 if
13149 successful, 0 if not successful. */
13150 int rc = try_scrolling (window, just_this_one_p,
13151 scroll_conservatively,
13152 scroll_step,
13153 temp_scroll_step, last_line_misfit);
13154 switch (rc)
13155 {
13156 case SCROLLING_SUCCESS:
13157 goto done;
13158
13159 case SCROLLING_NEED_LARGER_MATRICES:
13160 goto need_larger_matrices;
13161
13162 case SCROLLING_FAILED:
13163 break;
13164
13165 default:
13166 abort ();
13167 }
13168 }
13169
13170 /* Finally, just choose place to start which centers point */
13171
13172 recenter:
13173 if (centering_position < 0)
13174 centering_position = window_box_height (w) / 2;
13175
13176 #if GLYPH_DEBUG
13177 debug_method_add (w, "recenter");
13178 #endif
13179
13180 /* w->vscroll = 0; */
13181
13182 /* Forget any previously recorded base line for line number display. */
13183 if (!buffer_unchanged_p)
13184 w->base_line_number = Qnil;
13185
13186 /* Move backward half the height of the window. */
13187 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13188 it.current_y = it.last_visible_y;
13189 move_it_vertically_backward (&it, centering_position);
13190 xassert (IT_CHARPOS (it) >= BEGV);
13191
13192 /* The function move_it_vertically_backward may move over more
13193 than the specified y-distance. If it->w is small, e.g. a
13194 mini-buffer window, we may end up in front of the window's
13195 display area. Start displaying at the start of the line
13196 containing PT in this case. */
13197 if (it.current_y <= 0)
13198 {
13199 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13200 move_it_vertically_backward (&it, 0);
13201 #if 0
13202 /* I think this assert is bogus if buffer contains
13203 invisible text or images. KFS. */
13204 xassert (IT_CHARPOS (it) <= PT);
13205 #endif
13206 it.current_y = 0;
13207 }
13208
13209 it.current_x = it.hpos = 0;
13210
13211 /* Set startp here explicitly in case that helps avoid an infinite loop
13212 in case the window-scroll-functions functions get errors. */
13213 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13214
13215 /* Run scroll hooks. */
13216 startp = run_window_scroll_functions (window, it.current.pos);
13217
13218 /* Redisplay the window. */
13219 if (!current_matrix_up_to_date_p
13220 || windows_or_buffers_changed
13221 || cursor_type_changed
13222 /* Don't use try_window_reusing_current_matrix in this case
13223 because it can have changed the buffer. */
13224 || !NILP (Vwindow_scroll_functions)
13225 || !just_this_one_p
13226 || MINI_WINDOW_P (w)
13227 || !(used_current_matrix_p
13228 = try_window_reusing_current_matrix (w)))
13229 try_window (window, startp, 0);
13230
13231 /* If new fonts have been loaded (due to fontsets), give up. We
13232 have to start a new redisplay since we need to re-adjust glyph
13233 matrices. */
13234 if (fonts_changed_p)
13235 goto need_larger_matrices;
13236
13237 /* If cursor did not appear assume that the middle of the window is
13238 in the first line of the window. Do it again with the next line.
13239 (Imagine a window of height 100, displaying two lines of height
13240 60. Moving back 50 from it->last_visible_y will end in the first
13241 line.) */
13242 if (w->cursor.vpos < 0)
13243 {
13244 if (!NILP (w->window_end_valid)
13245 && PT >= Z - XFASTINT (w->window_end_pos))
13246 {
13247 clear_glyph_matrix (w->desired_matrix);
13248 move_it_by_lines (&it, 1, 0);
13249 try_window (window, it.current.pos, 0);
13250 }
13251 else if (PT < IT_CHARPOS (it))
13252 {
13253 clear_glyph_matrix (w->desired_matrix);
13254 move_it_by_lines (&it, -1, 0);
13255 try_window (window, it.current.pos, 0);
13256 }
13257 else
13258 {
13259 /* Not much we can do about it. */
13260 }
13261 }
13262
13263 /* Consider the following case: Window starts at BEGV, there is
13264 invisible, intangible text at BEGV, so that display starts at
13265 some point START > BEGV. It can happen that we are called with
13266 PT somewhere between BEGV and START. Try to handle that case. */
13267 if (w->cursor.vpos < 0)
13268 {
13269 struct glyph_row *row = w->current_matrix->rows;
13270 if (row->mode_line_p)
13271 ++row;
13272 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13273 }
13274
13275 if (!cursor_row_fully_visible_p (w, 0, 0))
13276 {
13277 /* If vscroll is enabled, disable it and try again. */
13278 if (w->vscroll)
13279 {
13280 w->vscroll = 0;
13281 clear_glyph_matrix (w->desired_matrix);
13282 goto recenter;
13283 }
13284
13285 /* If centering point failed to make the whole line visible,
13286 put point at the top instead. That has to make the whole line
13287 visible, if it can be done. */
13288 if (centering_position == 0)
13289 goto done;
13290
13291 clear_glyph_matrix (w->desired_matrix);
13292 centering_position = 0;
13293 goto recenter;
13294 }
13295
13296 done:
13297
13298 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13299 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13300 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13301 ? Qt : Qnil);
13302
13303 /* Display the mode line, if we must. */
13304 if ((update_mode_line
13305 /* If window not full width, must redo its mode line
13306 if (a) the window to its side is being redone and
13307 (b) we do a frame-based redisplay. This is a consequence
13308 of how inverted lines are drawn in frame-based redisplay. */
13309 || (!just_this_one_p
13310 && !FRAME_WINDOW_P (f)
13311 && !WINDOW_FULL_WIDTH_P (w))
13312 /* Line number to display. */
13313 || INTEGERP (w->base_line_pos)
13314 /* Column number is displayed and different from the one displayed. */
13315 || (!NILP (w->column_number_displayed)
13316 && (XFASTINT (w->column_number_displayed)
13317 != (int) current_column ()))) /* iftc */
13318 /* This means that the window has a mode line. */
13319 && (WINDOW_WANTS_MODELINE_P (w)
13320 || WINDOW_WANTS_HEADER_LINE_P (w)))
13321 {
13322 display_mode_lines (w);
13323
13324 /* If mode line height has changed, arrange for a thorough
13325 immediate redisplay using the correct mode line height. */
13326 if (WINDOW_WANTS_MODELINE_P (w)
13327 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13328 {
13329 fonts_changed_p = 1;
13330 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13331 = DESIRED_MODE_LINE_HEIGHT (w);
13332 }
13333
13334 /* If top line height has changed, arrange for a thorough
13335 immediate redisplay using the correct mode line height. */
13336 if (WINDOW_WANTS_HEADER_LINE_P (w)
13337 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13338 {
13339 fonts_changed_p = 1;
13340 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13341 = DESIRED_HEADER_LINE_HEIGHT (w);
13342 }
13343
13344 if (fonts_changed_p)
13345 goto need_larger_matrices;
13346 }
13347
13348 if (!line_number_displayed
13349 && !BUFFERP (w->base_line_pos))
13350 {
13351 w->base_line_pos = Qnil;
13352 w->base_line_number = Qnil;
13353 }
13354
13355 finish_menu_bars:
13356
13357 /* When we reach a frame's selected window, redo the frame's menu bar. */
13358 if (update_mode_line
13359 && EQ (FRAME_SELECTED_WINDOW (f), window))
13360 {
13361 int redisplay_menu_p = 0;
13362 int redisplay_tool_bar_p = 0;
13363
13364 if (FRAME_WINDOW_P (f))
13365 {
13366 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13367 || defined (USE_GTK)
13368 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13369 #else
13370 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13371 #endif
13372 }
13373 else
13374 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13375
13376 if (redisplay_menu_p)
13377 display_menu_bar (w);
13378
13379 #ifdef HAVE_WINDOW_SYSTEM
13380 #ifdef USE_GTK
13381 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13382 #else
13383 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13384 && (FRAME_TOOL_BAR_LINES (f) > 0
13385 || auto_resize_tool_bars_p);
13386
13387 #endif
13388
13389 if (redisplay_tool_bar_p)
13390 redisplay_tool_bar (f);
13391 #endif
13392 }
13393
13394 #ifdef HAVE_WINDOW_SYSTEM
13395 if (FRAME_WINDOW_P (f)
13396 && update_window_fringes (w, (just_this_one_p
13397 || (!used_current_matrix_p && !overlay_arrow_seen)
13398 || w->pseudo_window_p)))
13399 {
13400 update_begin (f);
13401 BLOCK_INPUT;
13402 if (draw_window_fringes (w, 1))
13403 x_draw_vertical_border (w);
13404 UNBLOCK_INPUT;
13405 update_end (f);
13406 }
13407 #endif /* HAVE_WINDOW_SYSTEM */
13408
13409 /* We go to this label, with fonts_changed_p nonzero,
13410 if it is necessary to try again using larger glyph matrices.
13411 We have to redeem the scroll bar even in this case,
13412 because the loop in redisplay_internal expects that. */
13413 need_larger_matrices:
13414 ;
13415 finish_scroll_bars:
13416
13417 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13418 {
13419 /* Set the thumb's position and size. */
13420 set_vertical_scroll_bar (w);
13421
13422 /* Note that we actually used the scroll bar attached to this
13423 window, so it shouldn't be deleted at the end of redisplay. */
13424 redeem_scroll_bar_hook (w);
13425 }
13426
13427 /* Restore current_buffer and value of point in it. */
13428 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13429 set_buffer_internal_1 (old);
13430 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13431
13432 unbind_to (count, Qnil);
13433 }
13434
13435
13436 /* Build the complete desired matrix of WINDOW with a window start
13437 buffer position POS.
13438
13439 Value is 1 if successful. It is zero if fonts were loaded during
13440 redisplay which makes re-adjusting glyph matrices necessary, and -1
13441 if point would appear in the scroll margins.
13442 (We check that only if CHECK_MARGINS is nonzero. */
13443
13444 int
13445 try_window (window, pos, check_margins)
13446 Lisp_Object window;
13447 struct text_pos pos;
13448 int check_margins;
13449 {
13450 struct window *w = XWINDOW (window);
13451 struct it it;
13452 struct glyph_row *last_text_row = NULL;
13453
13454 /* Make POS the new window start. */
13455 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13456
13457 /* Mark cursor position as unknown. No overlay arrow seen. */
13458 w->cursor.vpos = -1;
13459 overlay_arrow_seen = 0;
13460
13461 /* Initialize iterator and info to start at POS. */
13462 start_display (&it, w, pos);
13463
13464 /* Display all lines of W. */
13465 while (it.current_y < it.last_visible_y)
13466 {
13467 if (display_line (&it))
13468 last_text_row = it.glyph_row - 1;
13469 if (fonts_changed_p)
13470 return 0;
13471 }
13472
13473 /* Don't let the cursor end in the scroll margins. */
13474 if (check_margins
13475 && !MINI_WINDOW_P (w))
13476 {
13477 int this_scroll_margin;
13478
13479 this_scroll_margin = max (0, scroll_margin);
13480 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13481 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13482
13483 if ((w->cursor.y < this_scroll_margin
13484 && CHARPOS (pos) > BEGV
13485 && IT_CHARPOS (it) < ZV)
13486 /* rms: considering make_cursor_line_fully_visible_p here
13487 seems to give wrong results. We don't want to recenter
13488 when the last line is partly visible, we want to allow
13489 that case to be handled in the usual way. */
13490 || (w->cursor.y + 1) > it.last_visible_y)
13491 {
13492 w->cursor.vpos = -1;
13493 clear_glyph_matrix (w->desired_matrix);
13494 return -1;
13495 }
13496 }
13497
13498 /* If bottom moved off end of frame, change mode line percentage. */
13499 if (XFASTINT (w->window_end_pos) <= 0
13500 && Z != IT_CHARPOS (it))
13501 w->update_mode_line = Qt;
13502
13503 /* Set window_end_pos to the offset of the last character displayed
13504 on the window from the end of current_buffer. Set
13505 window_end_vpos to its row number. */
13506 if (last_text_row)
13507 {
13508 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13509 w->window_end_bytepos
13510 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13511 w->window_end_pos
13512 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13513 w->window_end_vpos
13514 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13515 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13516 ->displays_text_p);
13517 }
13518 else
13519 {
13520 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13521 w->window_end_pos = make_number (Z - ZV);
13522 w->window_end_vpos = make_number (0);
13523 }
13524
13525 /* But that is not valid info until redisplay finishes. */
13526 w->window_end_valid = Qnil;
13527 return 1;
13528 }
13529
13530
13531 \f
13532 /************************************************************************
13533 Window redisplay reusing current matrix when buffer has not changed
13534 ************************************************************************/
13535
13536 /* Try redisplay of window W showing an unchanged buffer with a
13537 different window start than the last time it was displayed by
13538 reusing its current matrix. Value is non-zero if successful.
13539 W->start is the new window start. */
13540
13541 static int
13542 try_window_reusing_current_matrix (w)
13543 struct window *w;
13544 {
13545 struct frame *f = XFRAME (w->frame);
13546 struct glyph_row *row, *bottom_row;
13547 struct it it;
13548 struct run run;
13549 struct text_pos start, new_start;
13550 int nrows_scrolled, i;
13551 struct glyph_row *last_text_row;
13552 struct glyph_row *last_reused_text_row;
13553 struct glyph_row *start_row;
13554 int start_vpos, min_y, max_y;
13555
13556 #if GLYPH_DEBUG
13557 if (inhibit_try_window_reusing)
13558 return 0;
13559 #endif
13560
13561 if (/* This function doesn't handle terminal frames. */
13562 !FRAME_WINDOW_P (f)
13563 /* Don't try to reuse the display if windows have been split
13564 or such. */
13565 || windows_or_buffers_changed
13566 || cursor_type_changed)
13567 return 0;
13568
13569 /* Can't do this if region may have changed. */
13570 if ((!NILP (Vtransient_mark_mode)
13571 && !NILP (current_buffer->mark_active))
13572 || !NILP (w->region_showing)
13573 || !NILP (Vshow_trailing_whitespace))
13574 return 0;
13575
13576 /* If top-line visibility has changed, give up. */
13577 if (WINDOW_WANTS_HEADER_LINE_P (w)
13578 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13579 return 0;
13580
13581 /* Give up if old or new display is scrolled vertically. We could
13582 make this function handle this, but right now it doesn't. */
13583 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13584 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13585 return 0;
13586
13587 /* The variable new_start now holds the new window start. The old
13588 start `start' can be determined from the current matrix. */
13589 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13590 start = start_row->start.pos;
13591 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13592
13593 /* Clear the desired matrix for the display below. */
13594 clear_glyph_matrix (w->desired_matrix);
13595
13596 if (CHARPOS (new_start) <= CHARPOS (start))
13597 {
13598 int first_row_y;
13599
13600 /* Don't use this method if the display starts with an ellipsis
13601 displayed for invisible text. It's not easy to handle that case
13602 below, and it's certainly not worth the effort since this is
13603 not a frequent case. */
13604 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13605 return 0;
13606
13607 IF_DEBUG (debug_method_add (w, "twu1"));
13608
13609 /* Display up to a row that can be reused. The variable
13610 last_text_row is set to the last row displayed that displays
13611 text. Note that it.vpos == 0 if or if not there is a
13612 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13613 start_display (&it, w, new_start);
13614 first_row_y = it.current_y;
13615 w->cursor.vpos = -1;
13616 last_text_row = last_reused_text_row = NULL;
13617
13618 while (it.current_y < it.last_visible_y
13619 && !fonts_changed_p)
13620 {
13621 /* If we have reached into the characters in the START row,
13622 that means the line boundaries have changed. So we
13623 can't start copying with the row START. Maybe it will
13624 work to start copying with the following row. */
13625 while (IT_CHARPOS (it) > CHARPOS (start))
13626 {
13627 /* Advance to the next row as the "start". */
13628 start_row++;
13629 start = start_row->start.pos;
13630 /* If there are no more rows to try, or just one, give up. */
13631 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13632 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13633 || CHARPOS (start) == ZV)
13634 {
13635 clear_glyph_matrix (w->desired_matrix);
13636 return 0;
13637 }
13638
13639 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13640 }
13641 /* If we have reached alignment,
13642 we can copy the rest of the rows. */
13643 if (IT_CHARPOS (it) == CHARPOS (start))
13644 break;
13645
13646 if (display_line (&it))
13647 last_text_row = it.glyph_row - 1;
13648 }
13649
13650 /* A value of current_y < last_visible_y means that we stopped
13651 at the previous window start, which in turn means that we
13652 have at least one reusable row. */
13653 if (it.current_y < it.last_visible_y)
13654 {
13655 /* IT.vpos always starts from 0; it counts text lines. */
13656 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13657
13658 /* Find PT if not already found in the lines displayed. */
13659 if (w->cursor.vpos < 0)
13660 {
13661 int dy = it.current_y - start_row->y;
13662
13663 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13664 row = row_containing_pos (w, PT, row, NULL, dy);
13665 if (row)
13666 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13667 dy, nrows_scrolled);
13668 else
13669 {
13670 clear_glyph_matrix (w->desired_matrix);
13671 return 0;
13672 }
13673 }
13674
13675 /* Scroll the display. Do it before the current matrix is
13676 changed. The problem here is that update has not yet
13677 run, i.e. part of the current matrix is not up to date.
13678 scroll_run_hook will clear the cursor, and use the
13679 current matrix to get the height of the row the cursor is
13680 in. */
13681 run.current_y = start_row->y;
13682 run.desired_y = it.current_y;
13683 run.height = it.last_visible_y - it.current_y;
13684
13685 if (run.height > 0 && run.current_y != run.desired_y)
13686 {
13687 update_begin (f);
13688 rif->update_window_begin_hook (w);
13689 rif->clear_window_mouse_face (w);
13690 rif->scroll_run_hook (w, &run);
13691 rif->update_window_end_hook (w, 0, 0);
13692 update_end (f);
13693 }
13694
13695 /* Shift current matrix down by nrows_scrolled lines. */
13696 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13697 rotate_matrix (w->current_matrix,
13698 start_vpos,
13699 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13700 nrows_scrolled);
13701
13702 /* Disable lines that must be updated. */
13703 for (i = 0; i < it.vpos; ++i)
13704 (start_row + i)->enabled_p = 0;
13705
13706 /* Re-compute Y positions. */
13707 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13708 max_y = it.last_visible_y;
13709 for (row = start_row + nrows_scrolled;
13710 row < bottom_row;
13711 ++row)
13712 {
13713 row->y = it.current_y;
13714 row->visible_height = row->height;
13715
13716 if (row->y < min_y)
13717 row->visible_height -= min_y - row->y;
13718 if (row->y + row->height > max_y)
13719 row->visible_height -= row->y + row->height - max_y;
13720 row->redraw_fringe_bitmaps_p = 1;
13721
13722 it.current_y += row->height;
13723
13724 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13725 last_reused_text_row = row;
13726 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13727 break;
13728 }
13729
13730 /* Disable lines in the current matrix which are now
13731 below the window. */
13732 for (++row; row < bottom_row; ++row)
13733 row->enabled_p = row->mode_line_p = 0;
13734 }
13735
13736 /* Update window_end_pos etc.; last_reused_text_row is the last
13737 reused row from the current matrix containing text, if any.
13738 The value of last_text_row is the last displayed line
13739 containing text. */
13740 if (last_reused_text_row)
13741 {
13742 w->window_end_bytepos
13743 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13744 w->window_end_pos
13745 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13746 w->window_end_vpos
13747 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13748 w->current_matrix));
13749 }
13750 else if (last_text_row)
13751 {
13752 w->window_end_bytepos
13753 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13754 w->window_end_pos
13755 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13756 w->window_end_vpos
13757 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13758 }
13759 else
13760 {
13761 /* This window must be completely empty. */
13762 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13763 w->window_end_pos = make_number (Z - ZV);
13764 w->window_end_vpos = make_number (0);
13765 }
13766 w->window_end_valid = Qnil;
13767
13768 /* Update hint: don't try scrolling again in update_window. */
13769 w->desired_matrix->no_scrolling_p = 1;
13770
13771 #if GLYPH_DEBUG
13772 debug_method_add (w, "try_window_reusing_current_matrix 1");
13773 #endif
13774 return 1;
13775 }
13776 else if (CHARPOS (new_start) > CHARPOS (start))
13777 {
13778 struct glyph_row *pt_row, *row;
13779 struct glyph_row *first_reusable_row;
13780 struct glyph_row *first_row_to_display;
13781 int dy;
13782 int yb = window_text_bottom_y (w);
13783
13784 /* Find the row starting at new_start, if there is one. Don't
13785 reuse a partially visible line at the end. */
13786 first_reusable_row = start_row;
13787 while (first_reusable_row->enabled_p
13788 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13789 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13790 < CHARPOS (new_start)))
13791 ++first_reusable_row;
13792
13793 /* Give up if there is no row to reuse. */
13794 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13795 || !first_reusable_row->enabled_p
13796 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13797 != CHARPOS (new_start)))
13798 return 0;
13799
13800 /* We can reuse fully visible rows beginning with
13801 first_reusable_row to the end of the window. Set
13802 first_row_to_display to the first row that cannot be reused.
13803 Set pt_row to the row containing point, if there is any. */
13804 pt_row = NULL;
13805 for (first_row_to_display = first_reusable_row;
13806 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13807 ++first_row_to_display)
13808 {
13809 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13810 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13811 pt_row = first_row_to_display;
13812 }
13813
13814 /* Start displaying at the start of first_row_to_display. */
13815 xassert (first_row_to_display->y < yb);
13816 init_to_row_start (&it, w, first_row_to_display);
13817
13818 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13819 - start_vpos);
13820 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13821 - nrows_scrolled);
13822 it.current_y = (first_row_to_display->y - first_reusable_row->y
13823 + WINDOW_HEADER_LINE_HEIGHT (w));
13824
13825 /* Display lines beginning with first_row_to_display in the
13826 desired matrix. Set last_text_row to the last row displayed
13827 that displays text. */
13828 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13829 if (pt_row == NULL)
13830 w->cursor.vpos = -1;
13831 last_text_row = NULL;
13832 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13833 if (display_line (&it))
13834 last_text_row = it.glyph_row - 1;
13835
13836 /* Give up If point isn't in a row displayed or reused. */
13837 if (w->cursor.vpos < 0)
13838 {
13839 clear_glyph_matrix (w->desired_matrix);
13840 return 0;
13841 }
13842
13843 /* If point is in a reused row, adjust y and vpos of the cursor
13844 position. */
13845 if (pt_row)
13846 {
13847 w->cursor.vpos -= nrows_scrolled;
13848 w->cursor.y -= first_reusable_row->y - start_row->y;
13849 }
13850
13851 /* Scroll the display. */
13852 run.current_y = first_reusable_row->y;
13853 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13854 run.height = it.last_visible_y - run.current_y;
13855 dy = run.current_y - run.desired_y;
13856
13857 if (run.height)
13858 {
13859 update_begin (f);
13860 rif->update_window_begin_hook (w);
13861 rif->clear_window_mouse_face (w);
13862 rif->scroll_run_hook (w, &run);
13863 rif->update_window_end_hook (w, 0, 0);
13864 update_end (f);
13865 }
13866
13867 /* Adjust Y positions of reused rows. */
13868 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13869 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13870 max_y = it.last_visible_y;
13871 for (row = first_reusable_row; row < first_row_to_display; ++row)
13872 {
13873 row->y -= dy;
13874 row->visible_height = row->height;
13875 if (row->y < min_y)
13876 row->visible_height -= min_y - row->y;
13877 if (row->y + row->height > max_y)
13878 row->visible_height -= row->y + row->height - max_y;
13879 row->redraw_fringe_bitmaps_p = 1;
13880 }
13881
13882 /* Scroll the current matrix. */
13883 xassert (nrows_scrolled > 0);
13884 rotate_matrix (w->current_matrix,
13885 start_vpos,
13886 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13887 -nrows_scrolled);
13888
13889 /* Disable rows not reused. */
13890 for (row -= nrows_scrolled; row < bottom_row; ++row)
13891 row->enabled_p = 0;
13892
13893 /* Point may have moved to a different line, so we cannot assume that
13894 the previous cursor position is valid; locate the correct row. */
13895 if (pt_row)
13896 {
13897 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13898 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13899 row++)
13900 {
13901 w->cursor.vpos++;
13902 w->cursor.y = row->y;
13903 }
13904 if (row < bottom_row)
13905 {
13906 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13907 while (glyph->charpos < PT)
13908 {
13909 w->cursor.hpos++;
13910 w->cursor.x += glyph->pixel_width;
13911 glyph++;
13912 }
13913 }
13914 }
13915
13916 /* Adjust window end. A null value of last_text_row means that
13917 the window end is in reused rows which in turn means that
13918 only its vpos can have changed. */
13919 if (last_text_row)
13920 {
13921 w->window_end_bytepos
13922 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13923 w->window_end_pos
13924 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13925 w->window_end_vpos
13926 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13927 }
13928 else
13929 {
13930 w->window_end_vpos
13931 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13932 }
13933
13934 w->window_end_valid = Qnil;
13935 w->desired_matrix->no_scrolling_p = 1;
13936
13937 #if GLYPH_DEBUG
13938 debug_method_add (w, "try_window_reusing_current_matrix 2");
13939 #endif
13940 return 1;
13941 }
13942
13943 return 0;
13944 }
13945
13946
13947 \f
13948 /************************************************************************
13949 Window redisplay reusing current matrix when buffer has changed
13950 ************************************************************************/
13951
13952 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13953 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13954 int *, int *));
13955 static struct glyph_row *
13956 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13957 struct glyph_row *));
13958
13959
13960 /* Return the last row in MATRIX displaying text. If row START is
13961 non-null, start searching with that row. IT gives the dimensions
13962 of the display. Value is null if matrix is empty; otherwise it is
13963 a pointer to the row found. */
13964
13965 static struct glyph_row *
13966 find_last_row_displaying_text (matrix, it, start)
13967 struct glyph_matrix *matrix;
13968 struct it *it;
13969 struct glyph_row *start;
13970 {
13971 struct glyph_row *row, *row_found;
13972
13973 /* Set row_found to the last row in IT->w's current matrix
13974 displaying text. The loop looks funny but think of partially
13975 visible lines. */
13976 row_found = NULL;
13977 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13978 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13979 {
13980 xassert (row->enabled_p);
13981 row_found = row;
13982 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13983 break;
13984 ++row;
13985 }
13986
13987 return row_found;
13988 }
13989
13990
13991 /* Return the last row in the current matrix of W that is not affected
13992 by changes at the start of current_buffer that occurred since W's
13993 current matrix was built. Value is null if no such row exists.
13994
13995 BEG_UNCHANGED us the number of characters unchanged at the start of
13996 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13997 first changed character in current_buffer. Characters at positions <
13998 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13999 when the current matrix was built. */
14000
14001 static struct glyph_row *
14002 find_last_unchanged_at_beg_row (w)
14003 struct window *w;
14004 {
14005 int first_changed_pos = BEG + BEG_UNCHANGED;
14006 struct glyph_row *row;
14007 struct glyph_row *row_found = NULL;
14008 int yb = window_text_bottom_y (w);
14009
14010 /* Find the last row displaying unchanged text. */
14011 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14012 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14013 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14014 {
14015 if (/* If row ends before first_changed_pos, it is unchanged,
14016 except in some case. */
14017 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14018 /* When row ends in ZV and we write at ZV it is not
14019 unchanged. */
14020 && !row->ends_at_zv_p
14021 /* When first_changed_pos is the end of a continued line,
14022 row is not unchanged because it may be no longer
14023 continued. */
14024 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14025 && (row->continued_p
14026 || row->exact_window_width_line_p)))
14027 row_found = row;
14028
14029 /* Stop if last visible row. */
14030 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14031 break;
14032
14033 ++row;
14034 }
14035
14036 return row_found;
14037 }
14038
14039
14040 /* Find the first glyph row in the current matrix of W that is not
14041 affected by changes at the end of current_buffer since the
14042 time W's current matrix was built.
14043
14044 Return in *DELTA the number of chars by which buffer positions in
14045 unchanged text at the end of current_buffer must be adjusted.
14046
14047 Return in *DELTA_BYTES the corresponding number of bytes.
14048
14049 Value is null if no such row exists, i.e. all rows are affected by
14050 changes. */
14051
14052 static struct glyph_row *
14053 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14054 struct window *w;
14055 int *delta, *delta_bytes;
14056 {
14057 struct glyph_row *row;
14058 struct glyph_row *row_found = NULL;
14059
14060 *delta = *delta_bytes = 0;
14061
14062 /* Display must not have been paused, otherwise the current matrix
14063 is not up to date. */
14064 if (NILP (w->window_end_valid))
14065 abort ();
14066
14067 /* A value of window_end_pos >= END_UNCHANGED means that the window
14068 end is in the range of changed text. If so, there is no
14069 unchanged row at the end of W's current matrix. */
14070 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14071 return NULL;
14072
14073 /* Set row to the last row in W's current matrix displaying text. */
14074 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14075
14076 /* If matrix is entirely empty, no unchanged row exists. */
14077 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14078 {
14079 /* The value of row is the last glyph row in the matrix having a
14080 meaningful buffer position in it. The end position of row
14081 corresponds to window_end_pos. This allows us to translate
14082 buffer positions in the current matrix to current buffer
14083 positions for characters not in changed text. */
14084 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14085 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14086 int last_unchanged_pos, last_unchanged_pos_old;
14087 struct glyph_row *first_text_row
14088 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14089
14090 *delta = Z - Z_old;
14091 *delta_bytes = Z_BYTE - Z_BYTE_old;
14092
14093 /* Set last_unchanged_pos to the buffer position of the last
14094 character in the buffer that has not been changed. Z is the
14095 index + 1 of the last character in current_buffer, i.e. by
14096 subtracting END_UNCHANGED we get the index of the last
14097 unchanged character, and we have to add BEG to get its buffer
14098 position. */
14099 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14100 last_unchanged_pos_old = last_unchanged_pos - *delta;
14101
14102 /* Search backward from ROW for a row displaying a line that
14103 starts at a minimum position >= last_unchanged_pos_old. */
14104 for (; row > first_text_row; --row)
14105 {
14106 /* This used to abort, but it can happen.
14107 It is ok to just stop the search instead here. KFS. */
14108 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14109 break;
14110
14111 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14112 row_found = row;
14113 }
14114 }
14115
14116 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14117 abort ();
14118
14119 return row_found;
14120 }
14121
14122
14123 /* Make sure that glyph rows in the current matrix of window W
14124 reference the same glyph memory as corresponding rows in the
14125 frame's frame matrix. This function is called after scrolling W's
14126 current matrix on a terminal frame in try_window_id and
14127 try_window_reusing_current_matrix. */
14128
14129 static void
14130 sync_frame_with_window_matrix_rows (w)
14131 struct window *w;
14132 {
14133 struct frame *f = XFRAME (w->frame);
14134 struct glyph_row *window_row, *window_row_end, *frame_row;
14135
14136 /* Preconditions: W must be a leaf window and full-width. Its frame
14137 must have a frame matrix. */
14138 xassert (NILP (w->hchild) && NILP (w->vchild));
14139 xassert (WINDOW_FULL_WIDTH_P (w));
14140 xassert (!FRAME_WINDOW_P (f));
14141
14142 /* If W is a full-width window, glyph pointers in W's current matrix
14143 have, by definition, to be the same as glyph pointers in the
14144 corresponding frame matrix. Note that frame matrices have no
14145 marginal areas (see build_frame_matrix). */
14146 window_row = w->current_matrix->rows;
14147 window_row_end = window_row + w->current_matrix->nrows;
14148 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14149 while (window_row < window_row_end)
14150 {
14151 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14152 struct glyph *end = window_row->glyphs[LAST_AREA];
14153
14154 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14155 frame_row->glyphs[TEXT_AREA] = start;
14156 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14157 frame_row->glyphs[LAST_AREA] = end;
14158
14159 /* Disable frame rows whose corresponding window rows have
14160 been disabled in try_window_id. */
14161 if (!window_row->enabled_p)
14162 frame_row->enabled_p = 0;
14163
14164 ++window_row, ++frame_row;
14165 }
14166 }
14167
14168
14169 /* Find the glyph row in window W containing CHARPOS. Consider all
14170 rows between START and END (not inclusive). END null means search
14171 all rows to the end of the display area of W. Value is the row
14172 containing CHARPOS or null. */
14173
14174 struct glyph_row *
14175 row_containing_pos (w, charpos, start, end, dy)
14176 struct window *w;
14177 int charpos;
14178 struct glyph_row *start, *end;
14179 int dy;
14180 {
14181 struct glyph_row *row = start;
14182 int last_y;
14183
14184 /* If we happen to start on a header-line, skip that. */
14185 if (row->mode_line_p)
14186 ++row;
14187
14188 if ((end && row >= end) || !row->enabled_p)
14189 return NULL;
14190
14191 last_y = window_text_bottom_y (w) - dy;
14192
14193 while (1)
14194 {
14195 /* Give up if we have gone too far. */
14196 if (end && row >= end)
14197 return NULL;
14198 /* This formerly returned if they were equal.
14199 I think that both quantities are of a "last plus one" type;
14200 if so, when they are equal, the row is within the screen. -- rms. */
14201 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14202 return NULL;
14203
14204 /* If it is in this row, return this row. */
14205 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14206 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14207 /* The end position of a row equals the start
14208 position of the next row. If CHARPOS is there, we
14209 would rather display it in the next line, except
14210 when this line ends in ZV. */
14211 && !row->ends_at_zv_p
14212 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14213 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14214 return row;
14215 ++row;
14216 }
14217 }
14218
14219
14220 /* Try to redisplay window W by reusing its existing display. W's
14221 current matrix must be up to date when this function is called,
14222 i.e. window_end_valid must not be nil.
14223
14224 Value is
14225
14226 1 if display has been updated
14227 0 if otherwise unsuccessful
14228 -1 if redisplay with same window start is known not to succeed
14229
14230 The following steps are performed:
14231
14232 1. Find the last row in the current matrix of W that is not
14233 affected by changes at the start of current_buffer. If no such row
14234 is found, give up.
14235
14236 2. Find the first row in W's current matrix that is not affected by
14237 changes at the end of current_buffer. Maybe there is no such row.
14238
14239 3. Display lines beginning with the row + 1 found in step 1 to the
14240 row found in step 2 or, if step 2 didn't find a row, to the end of
14241 the window.
14242
14243 4. If cursor is not known to appear on the window, give up.
14244
14245 5. If display stopped at the row found in step 2, scroll the
14246 display and current matrix as needed.
14247
14248 6. Maybe display some lines at the end of W, if we must. This can
14249 happen under various circumstances, like a partially visible line
14250 becoming fully visible, or because newly displayed lines are displayed
14251 in smaller font sizes.
14252
14253 7. Update W's window end information. */
14254
14255 static int
14256 try_window_id (w)
14257 struct window *w;
14258 {
14259 struct frame *f = XFRAME (w->frame);
14260 struct glyph_matrix *current_matrix = w->current_matrix;
14261 struct glyph_matrix *desired_matrix = w->desired_matrix;
14262 struct glyph_row *last_unchanged_at_beg_row;
14263 struct glyph_row *first_unchanged_at_end_row;
14264 struct glyph_row *row;
14265 struct glyph_row *bottom_row;
14266 int bottom_vpos;
14267 struct it it;
14268 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14269 struct text_pos start_pos;
14270 struct run run;
14271 int first_unchanged_at_end_vpos = 0;
14272 struct glyph_row *last_text_row, *last_text_row_at_end;
14273 struct text_pos start;
14274 int first_changed_charpos, last_changed_charpos;
14275
14276 #if GLYPH_DEBUG
14277 if (inhibit_try_window_id)
14278 return 0;
14279 #endif
14280
14281 /* This is handy for debugging. */
14282 #if 0
14283 #define GIVE_UP(X) \
14284 do { \
14285 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14286 return 0; \
14287 } while (0)
14288 #else
14289 #define GIVE_UP(X) return 0
14290 #endif
14291
14292 SET_TEXT_POS_FROM_MARKER (start, w->start);
14293
14294 /* Don't use this for mini-windows because these can show
14295 messages and mini-buffers, and we don't handle that here. */
14296 if (MINI_WINDOW_P (w))
14297 GIVE_UP (1);
14298
14299 /* This flag is used to prevent redisplay optimizations. */
14300 if (windows_or_buffers_changed || cursor_type_changed)
14301 GIVE_UP (2);
14302
14303 /* Verify that narrowing has not changed.
14304 Also verify that we were not told to prevent redisplay optimizations.
14305 It would be nice to further
14306 reduce the number of cases where this prevents try_window_id. */
14307 if (current_buffer->clip_changed
14308 || current_buffer->prevent_redisplay_optimizations_p)
14309 GIVE_UP (3);
14310
14311 /* Window must either use window-based redisplay or be full width. */
14312 if (!FRAME_WINDOW_P (f)
14313 && (!line_ins_del_ok
14314 || !WINDOW_FULL_WIDTH_P (w)))
14315 GIVE_UP (4);
14316
14317 /* Give up if point is not known NOT to appear in W. */
14318 if (PT < CHARPOS (start))
14319 GIVE_UP (5);
14320
14321 /* Another way to prevent redisplay optimizations. */
14322 if (XFASTINT (w->last_modified) == 0)
14323 GIVE_UP (6);
14324
14325 /* Verify that window is not hscrolled. */
14326 if (XFASTINT (w->hscroll) != 0)
14327 GIVE_UP (7);
14328
14329 /* Verify that display wasn't paused. */
14330 if (NILP (w->window_end_valid))
14331 GIVE_UP (8);
14332
14333 /* Can't use this if highlighting a region because a cursor movement
14334 will do more than just set the cursor. */
14335 if (!NILP (Vtransient_mark_mode)
14336 && !NILP (current_buffer->mark_active))
14337 GIVE_UP (9);
14338
14339 /* Likewise if highlighting trailing whitespace. */
14340 if (!NILP (Vshow_trailing_whitespace))
14341 GIVE_UP (11);
14342
14343 /* Likewise if showing a region. */
14344 if (!NILP (w->region_showing))
14345 GIVE_UP (10);
14346
14347 /* Can use this if overlay arrow position and or string have changed. */
14348 if (overlay_arrows_changed_p ())
14349 GIVE_UP (12);
14350
14351
14352 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14353 only if buffer has really changed. The reason is that the gap is
14354 initially at Z for freshly visited files. The code below would
14355 set end_unchanged to 0 in that case. */
14356 if (MODIFF > SAVE_MODIFF
14357 /* This seems to happen sometimes after saving a buffer. */
14358 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14359 {
14360 if (GPT - BEG < BEG_UNCHANGED)
14361 BEG_UNCHANGED = GPT - BEG;
14362 if (Z - GPT < END_UNCHANGED)
14363 END_UNCHANGED = Z - GPT;
14364 }
14365
14366 /* The position of the first and last character that has been changed. */
14367 first_changed_charpos = BEG + BEG_UNCHANGED;
14368 last_changed_charpos = Z - END_UNCHANGED;
14369
14370 /* If window starts after a line end, and the last change is in
14371 front of that newline, then changes don't affect the display.
14372 This case happens with stealth-fontification. Note that although
14373 the display is unchanged, glyph positions in the matrix have to
14374 be adjusted, of course. */
14375 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14376 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14377 && ((last_changed_charpos < CHARPOS (start)
14378 && CHARPOS (start) == BEGV)
14379 || (last_changed_charpos < CHARPOS (start) - 1
14380 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14381 {
14382 int Z_old, delta, Z_BYTE_old, delta_bytes;
14383 struct glyph_row *r0;
14384
14385 /* Compute how many chars/bytes have been added to or removed
14386 from the buffer. */
14387 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14388 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14389 delta = Z - Z_old;
14390 delta_bytes = Z_BYTE - Z_BYTE_old;
14391
14392 /* Give up if PT is not in the window. Note that it already has
14393 been checked at the start of try_window_id that PT is not in
14394 front of the window start. */
14395 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14396 GIVE_UP (13);
14397
14398 /* If window start is unchanged, we can reuse the whole matrix
14399 as is, after adjusting glyph positions. No need to compute
14400 the window end again, since its offset from Z hasn't changed. */
14401 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14402 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14403 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14404 /* PT must not be in a partially visible line. */
14405 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14406 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14407 {
14408 /* Adjust positions in the glyph matrix. */
14409 if (delta || delta_bytes)
14410 {
14411 struct glyph_row *r1
14412 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14413 increment_matrix_positions (w->current_matrix,
14414 MATRIX_ROW_VPOS (r0, current_matrix),
14415 MATRIX_ROW_VPOS (r1, current_matrix),
14416 delta, delta_bytes);
14417 }
14418
14419 /* Set the cursor. */
14420 row = row_containing_pos (w, PT, r0, NULL, 0);
14421 if (row)
14422 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14423 else
14424 abort ();
14425 return 1;
14426 }
14427 }
14428
14429 /* Handle the case that changes are all below what is displayed in
14430 the window, and that PT is in the window. This shortcut cannot
14431 be taken if ZV is visible in the window, and text has been added
14432 there that is visible in the window. */
14433 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14434 /* ZV is not visible in the window, or there are no
14435 changes at ZV, actually. */
14436 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14437 || first_changed_charpos == last_changed_charpos))
14438 {
14439 struct glyph_row *r0;
14440
14441 /* Give up if PT is not in the window. Note that it already has
14442 been checked at the start of try_window_id that PT is not in
14443 front of the window start. */
14444 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14445 GIVE_UP (14);
14446
14447 /* If window start is unchanged, we can reuse the whole matrix
14448 as is, without changing glyph positions since no text has
14449 been added/removed in front of the window end. */
14450 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14451 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14452 /* PT must not be in a partially visible line. */
14453 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14454 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14455 {
14456 /* We have to compute the window end anew since text
14457 can have been added/removed after it. */
14458 w->window_end_pos
14459 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14460 w->window_end_bytepos
14461 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14462
14463 /* Set the cursor. */
14464 row = row_containing_pos (w, PT, r0, NULL, 0);
14465 if (row)
14466 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14467 else
14468 abort ();
14469 return 2;
14470 }
14471 }
14472
14473 /* Give up if window start is in the changed area.
14474
14475 The condition used to read
14476
14477 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14478
14479 but why that was tested escapes me at the moment. */
14480 if (CHARPOS (start) >= first_changed_charpos
14481 && CHARPOS (start) <= last_changed_charpos)
14482 GIVE_UP (15);
14483
14484 /* Check that window start agrees with the start of the first glyph
14485 row in its current matrix. Check this after we know the window
14486 start is not in changed text, otherwise positions would not be
14487 comparable. */
14488 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14489 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14490 GIVE_UP (16);
14491
14492 /* Give up if the window ends in strings. Overlay strings
14493 at the end are difficult to handle, so don't try. */
14494 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14495 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14496 GIVE_UP (20);
14497
14498 /* Compute the position at which we have to start displaying new
14499 lines. Some of the lines at the top of the window might be
14500 reusable because they are not displaying changed text. Find the
14501 last row in W's current matrix not affected by changes at the
14502 start of current_buffer. Value is null if changes start in the
14503 first line of window. */
14504 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14505 if (last_unchanged_at_beg_row)
14506 {
14507 /* Avoid starting to display in the moddle of a character, a TAB
14508 for instance. This is easier than to set up the iterator
14509 exactly, and it's not a frequent case, so the additional
14510 effort wouldn't really pay off. */
14511 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14512 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14513 && last_unchanged_at_beg_row > w->current_matrix->rows)
14514 --last_unchanged_at_beg_row;
14515
14516 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14517 GIVE_UP (17);
14518
14519 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14520 GIVE_UP (18);
14521 start_pos = it.current.pos;
14522
14523 /* Start displaying new lines in the desired matrix at the same
14524 vpos we would use in the current matrix, i.e. below
14525 last_unchanged_at_beg_row. */
14526 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14527 current_matrix);
14528 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14529 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14530
14531 xassert (it.hpos == 0 && it.current_x == 0);
14532 }
14533 else
14534 {
14535 /* There are no reusable lines at the start of the window.
14536 Start displaying in the first text line. */
14537 start_display (&it, w, start);
14538 it.vpos = it.first_vpos;
14539 start_pos = it.current.pos;
14540 }
14541
14542 /* Find the first row that is not affected by changes at the end of
14543 the buffer. Value will be null if there is no unchanged row, in
14544 which case we must redisplay to the end of the window. delta
14545 will be set to the value by which buffer positions beginning with
14546 first_unchanged_at_end_row have to be adjusted due to text
14547 changes. */
14548 first_unchanged_at_end_row
14549 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14550 IF_DEBUG (debug_delta = delta);
14551 IF_DEBUG (debug_delta_bytes = delta_bytes);
14552
14553 /* Set stop_pos to the buffer position up to which we will have to
14554 display new lines. If first_unchanged_at_end_row != NULL, this
14555 is the buffer position of the start of the line displayed in that
14556 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14557 that we don't stop at a buffer position. */
14558 stop_pos = 0;
14559 if (first_unchanged_at_end_row)
14560 {
14561 xassert (last_unchanged_at_beg_row == NULL
14562 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14563
14564 /* If this is a continuation line, move forward to the next one
14565 that isn't. Changes in lines above affect this line.
14566 Caution: this may move first_unchanged_at_end_row to a row
14567 not displaying text. */
14568 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14569 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14570 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14571 < it.last_visible_y))
14572 ++first_unchanged_at_end_row;
14573
14574 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14575 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14576 >= it.last_visible_y))
14577 first_unchanged_at_end_row = NULL;
14578 else
14579 {
14580 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14581 + delta);
14582 first_unchanged_at_end_vpos
14583 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14584 xassert (stop_pos >= Z - END_UNCHANGED);
14585 }
14586 }
14587 else if (last_unchanged_at_beg_row == NULL)
14588 GIVE_UP (19);
14589
14590
14591 #if GLYPH_DEBUG
14592
14593 /* Either there is no unchanged row at the end, or the one we have
14594 now displays text. This is a necessary condition for the window
14595 end pos calculation at the end of this function. */
14596 xassert (first_unchanged_at_end_row == NULL
14597 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14598
14599 debug_last_unchanged_at_beg_vpos
14600 = (last_unchanged_at_beg_row
14601 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14602 : -1);
14603 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14604
14605 #endif /* GLYPH_DEBUG != 0 */
14606
14607
14608 /* Display new lines. Set last_text_row to the last new line
14609 displayed which has text on it, i.e. might end up as being the
14610 line where the window_end_vpos is. */
14611 w->cursor.vpos = -1;
14612 last_text_row = NULL;
14613 overlay_arrow_seen = 0;
14614 while (it.current_y < it.last_visible_y
14615 && !fonts_changed_p
14616 && (first_unchanged_at_end_row == NULL
14617 || IT_CHARPOS (it) < stop_pos))
14618 {
14619 if (display_line (&it))
14620 last_text_row = it.glyph_row - 1;
14621 }
14622
14623 if (fonts_changed_p)
14624 return -1;
14625
14626
14627 /* Compute differences in buffer positions, y-positions etc. for
14628 lines reused at the bottom of the window. Compute what we can
14629 scroll. */
14630 if (first_unchanged_at_end_row
14631 /* No lines reused because we displayed everything up to the
14632 bottom of the window. */
14633 && it.current_y < it.last_visible_y)
14634 {
14635 dvpos = (it.vpos
14636 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14637 current_matrix));
14638 dy = it.current_y - first_unchanged_at_end_row->y;
14639 run.current_y = first_unchanged_at_end_row->y;
14640 run.desired_y = run.current_y + dy;
14641 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14642 }
14643 else
14644 {
14645 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14646 first_unchanged_at_end_row = NULL;
14647 }
14648 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14649
14650
14651 /* Find the cursor if not already found. We have to decide whether
14652 PT will appear on this window (it sometimes doesn't, but this is
14653 not a very frequent case.) This decision has to be made before
14654 the current matrix is altered. A value of cursor.vpos < 0 means
14655 that PT is either in one of the lines beginning at
14656 first_unchanged_at_end_row or below the window. Don't care for
14657 lines that might be displayed later at the window end; as
14658 mentioned, this is not a frequent case. */
14659 if (w->cursor.vpos < 0)
14660 {
14661 /* Cursor in unchanged rows at the top? */
14662 if (PT < CHARPOS (start_pos)
14663 && last_unchanged_at_beg_row)
14664 {
14665 row = row_containing_pos (w, PT,
14666 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14667 last_unchanged_at_beg_row + 1, 0);
14668 if (row)
14669 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14670 }
14671
14672 /* Start from first_unchanged_at_end_row looking for PT. */
14673 else if (first_unchanged_at_end_row)
14674 {
14675 row = row_containing_pos (w, PT - delta,
14676 first_unchanged_at_end_row, NULL, 0);
14677 if (row)
14678 set_cursor_from_row (w, row, w->current_matrix, delta,
14679 delta_bytes, dy, dvpos);
14680 }
14681
14682 /* Give up if cursor was not found. */
14683 if (w->cursor.vpos < 0)
14684 {
14685 clear_glyph_matrix (w->desired_matrix);
14686 return -1;
14687 }
14688 }
14689
14690 /* Don't let the cursor end in the scroll margins. */
14691 {
14692 int this_scroll_margin, cursor_height;
14693
14694 this_scroll_margin = max (0, scroll_margin);
14695 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14696 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14697 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14698
14699 if ((w->cursor.y < this_scroll_margin
14700 && CHARPOS (start) > BEGV)
14701 /* Old redisplay didn't take scroll margin into account at the bottom,
14702 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14703 || (w->cursor.y + (make_cursor_line_fully_visible_p
14704 ? cursor_height + this_scroll_margin
14705 : 1)) > it.last_visible_y)
14706 {
14707 w->cursor.vpos = -1;
14708 clear_glyph_matrix (w->desired_matrix);
14709 return -1;
14710 }
14711 }
14712
14713 /* Scroll the display. Do it before changing the current matrix so
14714 that xterm.c doesn't get confused about where the cursor glyph is
14715 found. */
14716 if (dy && run.height)
14717 {
14718 update_begin (f);
14719
14720 if (FRAME_WINDOW_P (f))
14721 {
14722 rif->update_window_begin_hook (w);
14723 rif->clear_window_mouse_face (w);
14724 rif->scroll_run_hook (w, &run);
14725 rif->update_window_end_hook (w, 0, 0);
14726 }
14727 else
14728 {
14729 /* Terminal frame. In this case, dvpos gives the number of
14730 lines to scroll by; dvpos < 0 means scroll up. */
14731 int first_unchanged_at_end_vpos
14732 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14733 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14734 int end = (WINDOW_TOP_EDGE_LINE (w)
14735 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14736 + window_internal_height (w));
14737
14738 /* Perform the operation on the screen. */
14739 if (dvpos > 0)
14740 {
14741 /* Scroll last_unchanged_at_beg_row to the end of the
14742 window down dvpos lines. */
14743 set_terminal_window (end);
14744
14745 /* On dumb terminals delete dvpos lines at the end
14746 before inserting dvpos empty lines. */
14747 if (!scroll_region_ok)
14748 ins_del_lines (end - dvpos, -dvpos);
14749
14750 /* Insert dvpos empty lines in front of
14751 last_unchanged_at_beg_row. */
14752 ins_del_lines (from, dvpos);
14753 }
14754 else if (dvpos < 0)
14755 {
14756 /* Scroll up last_unchanged_at_beg_vpos to the end of
14757 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14758 set_terminal_window (end);
14759
14760 /* Delete dvpos lines in front of
14761 last_unchanged_at_beg_vpos. ins_del_lines will set
14762 the cursor to the given vpos and emit |dvpos| delete
14763 line sequences. */
14764 ins_del_lines (from + dvpos, dvpos);
14765
14766 /* On a dumb terminal insert dvpos empty lines at the
14767 end. */
14768 if (!scroll_region_ok)
14769 ins_del_lines (end + dvpos, -dvpos);
14770 }
14771
14772 set_terminal_window (0);
14773 }
14774
14775 update_end (f);
14776 }
14777
14778 /* Shift reused rows of the current matrix to the right position.
14779 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14780 text. */
14781 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14782 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14783 if (dvpos < 0)
14784 {
14785 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14786 bottom_vpos, dvpos);
14787 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14788 bottom_vpos, 0);
14789 }
14790 else if (dvpos > 0)
14791 {
14792 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14793 bottom_vpos, dvpos);
14794 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14795 first_unchanged_at_end_vpos + dvpos, 0);
14796 }
14797
14798 /* For frame-based redisplay, make sure that current frame and window
14799 matrix are in sync with respect to glyph memory. */
14800 if (!FRAME_WINDOW_P (f))
14801 sync_frame_with_window_matrix_rows (w);
14802
14803 /* Adjust buffer positions in reused rows. */
14804 if (delta)
14805 increment_matrix_positions (current_matrix,
14806 first_unchanged_at_end_vpos + dvpos,
14807 bottom_vpos, delta, delta_bytes);
14808
14809 /* Adjust Y positions. */
14810 if (dy)
14811 shift_glyph_matrix (w, current_matrix,
14812 first_unchanged_at_end_vpos + dvpos,
14813 bottom_vpos, dy);
14814
14815 if (first_unchanged_at_end_row)
14816 {
14817 first_unchanged_at_end_row += dvpos;
14818 if (first_unchanged_at_end_row->y >= it.last_visible_y
14819 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14820 first_unchanged_at_end_row = NULL;
14821 }
14822
14823 /* If scrolling up, there may be some lines to display at the end of
14824 the window. */
14825 last_text_row_at_end = NULL;
14826 if (dy < 0)
14827 {
14828 /* Scrolling up can leave for example a partially visible line
14829 at the end of the window to be redisplayed. */
14830 /* Set last_row to the glyph row in the current matrix where the
14831 window end line is found. It has been moved up or down in
14832 the matrix by dvpos. */
14833 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14834 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14835
14836 /* If last_row is the window end line, it should display text. */
14837 xassert (last_row->displays_text_p);
14838
14839 /* If window end line was partially visible before, begin
14840 displaying at that line. Otherwise begin displaying with the
14841 line following it. */
14842 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14843 {
14844 init_to_row_start (&it, w, last_row);
14845 it.vpos = last_vpos;
14846 it.current_y = last_row->y;
14847 }
14848 else
14849 {
14850 init_to_row_end (&it, w, last_row);
14851 it.vpos = 1 + last_vpos;
14852 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14853 ++last_row;
14854 }
14855
14856 /* We may start in a continuation line. If so, we have to
14857 get the right continuation_lines_width and current_x. */
14858 it.continuation_lines_width = last_row->continuation_lines_width;
14859 it.hpos = it.current_x = 0;
14860
14861 /* Display the rest of the lines at the window end. */
14862 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14863 while (it.current_y < it.last_visible_y
14864 && !fonts_changed_p)
14865 {
14866 /* Is it always sure that the display agrees with lines in
14867 the current matrix? I don't think so, so we mark rows
14868 displayed invalid in the current matrix by setting their
14869 enabled_p flag to zero. */
14870 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14871 if (display_line (&it))
14872 last_text_row_at_end = it.glyph_row - 1;
14873 }
14874 }
14875
14876 /* Update window_end_pos and window_end_vpos. */
14877 if (first_unchanged_at_end_row
14878 && !last_text_row_at_end)
14879 {
14880 /* Window end line if one of the preserved rows from the current
14881 matrix. Set row to the last row displaying text in current
14882 matrix starting at first_unchanged_at_end_row, after
14883 scrolling. */
14884 xassert (first_unchanged_at_end_row->displays_text_p);
14885 row = find_last_row_displaying_text (w->current_matrix, &it,
14886 first_unchanged_at_end_row);
14887 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14888
14889 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14890 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14891 w->window_end_vpos
14892 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14893 xassert (w->window_end_bytepos >= 0);
14894 IF_DEBUG (debug_method_add (w, "A"));
14895 }
14896 else if (last_text_row_at_end)
14897 {
14898 w->window_end_pos
14899 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14900 w->window_end_bytepos
14901 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14902 w->window_end_vpos
14903 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14904 xassert (w->window_end_bytepos >= 0);
14905 IF_DEBUG (debug_method_add (w, "B"));
14906 }
14907 else if (last_text_row)
14908 {
14909 /* We have displayed either to the end of the window or at the
14910 end of the window, i.e. the last row with text is to be found
14911 in the desired matrix. */
14912 w->window_end_pos
14913 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14914 w->window_end_bytepos
14915 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14916 w->window_end_vpos
14917 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14918 xassert (w->window_end_bytepos >= 0);
14919 }
14920 else if (first_unchanged_at_end_row == NULL
14921 && last_text_row == NULL
14922 && last_text_row_at_end == NULL)
14923 {
14924 /* Displayed to end of window, but no line containing text was
14925 displayed. Lines were deleted at the end of the window. */
14926 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14927 int vpos = XFASTINT (w->window_end_vpos);
14928 struct glyph_row *current_row = current_matrix->rows + vpos;
14929 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14930
14931 for (row = NULL;
14932 row == NULL && vpos >= first_vpos;
14933 --vpos, --current_row, --desired_row)
14934 {
14935 if (desired_row->enabled_p)
14936 {
14937 if (desired_row->displays_text_p)
14938 row = desired_row;
14939 }
14940 else if (current_row->displays_text_p)
14941 row = current_row;
14942 }
14943
14944 xassert (row != NULL);
14945 w->window_end_vpos = make_number (vpos + 1);
14946 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14947 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14948 xassert (w->window_end_bytepos >= 0);
14949 IF_DEBUG (debug_method_add (w, "C"));
14950 }
14951 else
14952 abort ();
14953
14954 #if 0 /* This leads to problems, for instance when the cursor is
14955 at ZV, and the cursor line displays no text. */
14956 /* Disable rows below what's displayed in the window. This makes
14957 debugging easier. */
14958 enable_glyph_matrix_rows (current_matrix,
14959 XFASTINT (w->window_end_vpos) + 1,
14960 bottom_vpos, 0);
14961 #endif
14962
14963 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14964 debug_end_vpos = XFASTINT (w->window_end_vpos));
14965
14966 /* Record that display has not been completed. */
14967 w->window_end_valid = Qnil;
14968 w->desired_matrix->no_scrolling_p = 1;
14969 return 3;
14970
14971 #undef GIVE_UP
14972 }
14973
14974
14975 \f
14976 /***********************************************************************
14977 More debugging support
14978 ***********************************************************************/
14979
14980 #if GLYPH_DEBUG
14981
14982 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14983 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14984 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14985
14986
14987 /* Dump the contents of glyph matrix MATRIX on stderr.
14988
14989 GLYPHS 0 means don't show glyph contents.
14990 GLYPHS 1 means show glyphs in short form
14991 GLYPHS > 1 means show glyphs in long form. */
14992
14993 void
14994 dump_glyph_matrix (matrix, glyphs)
14995 struct glyph_matrix *matrix;
14996 int glyphs;
14997 {
14998 int i;
14999 for (i = 0; i < matrix->nrows; ++i)
15000 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15001 }
15002
15003
15004 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15005 the glyph row and area where the glyph comes from. */
15006
15007 void
15008 dump_glyph (row, glyph, area)
15009 struct glyph_row *row;
15010 struct glyph *glyph;
15011 int area;
15012 {
15013 if (glyph->type == CHAR_GLYPH)
15014 {
15015 fprintf (stderr,
15016 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15017 glyph - row->glyphs[TEXT_AREA],
15018 'C',
15019 glyph->charpos,
15020 (BUFFERP (glyph->object)
15021 ? 'B'
15022 : (STRINGP (glyph->object)
15023 ? 'S'
15024 : '-')),
15025 glyph->pixel_width,
15026 glyph->u.ch,
15027 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15028 ? glyph->u.ch
15029 : '.'),
15030 glyph->face_id,
15031 glyph->left_box_line_p,
15032 glyph->right_box_line_p);
15033 }
15034 else if (glyph->type == STRETCH_GLYPH)
15035 {
15036 fprintf (stderr,
15037 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15038 glyph - row->glyphs[TEXT_AREA],
15039 'S',
15040 glyph->charpos,
15041 (BUFFERP (glyph->object)
15042 ? 'B'
15043 : (STRINGP (glyph->object)
15044 ? 'S'
15045 : '-')),
15046 glyph->pixel_width,
15047 0,
15048 '.',
15049 glyph->face_id,
15050 glyph->left_box_line_p,
15051 glyph->right_box_line_p);
15052 }
15053 else if (glyph->type == IMAGE_GLYPH)
15054 {
15055 fprintf (stderr,
15056 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15057 glyph - row->glyphs[TEXT_AREA],
15058 'I',
15059 glyph->charpos,
15060 (BUFFERP (glyph->object)
15061 ? 'B'
15062 : (STRINGP (glyph->object)
15063 ? 'S'
15064 : '-')),
15065 glyph->pixel_width,
15066 glyph->u.img_id,
15067 '.',
15068 glyph->face_id,
15069 glyph->left_box_line_p,
15070 glyph->right_box_line_p);
15071 }
15072 else if (glyph->type == COMPOSITE_GLYPH)
15073 {
15074 fprintf (stderr,
15075 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15076 glyph - row->glyphs[TEXT_AREA],
15077 '+',
15078 glyph->charpos,
15079 (BUFFERP (glyph->object)
15080 ? 'B'
15081 : (STRINGP (glyph->object)
15082 ? 'S'
15083 : '-')),
15084 glyph->pixel_width,
15085 glyph->u.cmp_id,
15086 '.',
15087 glyph->face_id,
15088 glyph->left_box_line_p,
15089 glyph->right_box_line_p);
15090 }
15091 }
15092
15093
15094 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15095 GLYPHS 0 means don't show glyph contents.
15096 GLYPHS 1 means show glyphs in short form
15097 GLYPHS > 1 means show glyphs in long form. */
15098
15099 void
15100 dump_glyph_row (row, vpos, glyphs)
15101 struct glyph_row *row;
15102 int vpos, glyphs;
15103 {
15104 if (glyphs != 1)
15105 {
15106 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15107 fprintf (stderr, "======================================================================\n");
15108
15109 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15110 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15111 vpos,
15112 MATRIX_ROW_START_CHARPOS (row),
15113 MATRIX_ROW_END_CHARPOS (row),
15114 row->used[TEXT_AREA],
15115 row->contains_overlapping_glyphs_p,
15116 row->enabled_p,
15117 row->truncated_on_left_p,
15118 row->truncated_on_right_p,
15119 row->continued_p,
15120 MATRIX_ROW_CONTINUATION_LINE_P (row),
15121 row->displays_text_p,
15122 row->ends_at_zv_p,
15123 row->fill_line_p,
15124 row->ends_in_middle_of_char_p,
15125 row->starts_in_middle_of_char_p,
15126 row->mouse_face_p,
15127 row->x,
15128 row->y,
15129 row->pixel_width,
15130 row->height,
15131 row->visible_height,
15132 row->ascent,
15133 row->phys_ascent);
15134 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15135 row->end.overlay_string_index,
15136 row->continuation_lines_width);
15137 fprintf (stderr, "%9d %5d\n",
15138 CHARPOS (row->start.string_pos),
15139 CHARPOS (row->end.string_pos));
15140 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15141 row->end.dpvec_index);
15142 }
15143
15144 if (glyphs > 1)
15145 {
15146 int area;
15147
15148 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15149 {
15150 struct glyph *glyph = row->glyphs[area];
15151 struct glyph *glyph_end = glyph + row->used[area];
15152
15153 /* Glyph for a line end in text. */
15154 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15155 ++glyph_end;
15156
15157 if (glyph < glyph_end)
15158 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15159
15160 for (; glyph < glyph_end; ++glyph)
15161 dump_glyph (row, glyph, area);
15162 }
15163 }
15164 else if (glyphs == 1)
15165 {
15166 int area;
15167
15168 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15169 {
15170 char *s = (char *) alloca (row->used[area] + 1);
15171 int i;
15172
15173 for (i = 0; i < row->used[area]; ++i)
15174 {
15175 struct glyph *glyph = row->glyphs[area] + i;
15176 if (glyph->type == CHAR_GLYPH
15177 && glyph->u.ch < 0x80
15178 && glyph->u.ch >= ' ')
15179 s[i] = glyph->u.ch;
15180 else
15181 s[i] = '.';
15182 }
15183
15184 s[i] = '\0';
15185 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15186 }
15187 }
15188 }
15189
15190
15191 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15192 Sdump_glyph_matrix, 0, 1, "p",
15193 doc: /* Dump the current matrix of the selected window to stderr.
15194 Shows contents of glyph row structures. With non-nil
15195 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15196 glyphs in short form, otherwise show glyphs in long form. */)
15197 (glyphs)
15198 Lisp_Object glyphs;
15199 {
15200 struct window *w = XWINDOW (selected_window);
15201 struct buffer *buffer = XBUFFER (w->buffer);
15202
15203 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15204 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15205 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15206 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15207 fprintf (stderr, "=============================================\n");
15208 dump_glyph_matrix (w->current_matrix,
15209 NILP (glyphs) ? 0 : XINT (glyphs));
15210 return Qnil;
15211 }
15212
15213
15214 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15215 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15216 ()
15217 {
15218 struct frame *f = XFRAME (selected_frame);
15219 dump_glyph_matrix (f->current_matrix, 1);
15220 return Qnil;
15221 }
15222
15223
15224 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15225 doc: /* Dump glyph row ROW to stderr.
15226 GLYPH 0 means don't dump glyphs.
15227 GLYPH 1 means dump glyphs in short form.
15228 GLYPH > 1 or omitted means dump glyphs in long form. */)
15229 (row, glyphs)
15230 Lisp_Object row, glyphs;
15231 {
15232 struct glyph_matrix *matrix;
15233 int vpos;
15234
15235 CHECK_NUMBER (row);
15236 matrix = XWINDOW (selected_window)->current_matrix;
15237 vpos = XINT (row);
15238 if (vpos >= 0 && vpos < matrix->nrows)
15239 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15240 vpos,
15241 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15242 return Qnil;
15243 }
15244
15245
15246 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15247 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15248 GLYPH 0 means don't dump glyphs.
15249 GLYPH 1 means dump glyphs in short form.
15250 GLYPH > 1 or omitted means dump glyphs in long form. */)
15251 (row, glyphs)
15252 Lisp_Object row, glyphs;
15253 {
15254 struct frame *sf = SELECTED_FRAME ();
15255 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15256 int vpos;
15257
15258 CHECK_NUMBER (row);
15259 vpos = XINT (row);
15260 if (vpos >= 0 && vpos < m->nrows)
15261 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15262 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15263 return Qnil;
15264 }
15265
15266
15267 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15268 doc: /* Toggle tracing of redisplay.
15269 With ARG, turn tracing on if and only if ARG is positive. */)
15270 (arg)
15271 Lisp_Object arg;
15272 {
15273 if (NILP (arg))
15274 trace_redisplay_p = !trace_redisplay_p;
15275 else
15276 {
15277 arg = Fprefix_numeric_value (arg);
15278 trace_redisplay_p = XINT (arg) > 0;
15279 }
15280
15281 return Qnil;
15282 }
15283
15284
15285 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15286 doc: /* Like `format', but print result to stderr.
15287 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15288 (nargs, args)
15289 int nargs;
15290 Lisp_Object *args;
15291 {
15292 Lisp_Object s = Fformat (nargs, args);
15293 fprintf (stderr, "%s", SDATA (s));
15294 return Qnil;
15295 }
15296
15297 #endif /* GLYPH_DEBUG */
15298
15299
15300 \f
15301 /***********************************************************************
15302 Building Desired Matrix Rows
15303 ***********************************************************************/
15304
15305 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15306 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15307
15308 static struct glyph_row *
15309 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15310 struct window *w;
15311 Lisp_Object overlay_arrow_string;
15312 {
15313 struct frame *f = XFRAME (WINDOW_FRAME (w));
15314 struct buffer *buffer = XBUFFER (w->buffer);
15315 struct buffer *old = current_buffer;
15316 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15317 int arrow_len = SCHARS (overlay_arrow_string);
15318 const unsigned char *arrow_end = arrow_string + arrow_len;
15319 const unsigned char *p;
15320 struct it it;
15321 int multibyte_p;
15322 int n_glyphs_before;
15323
15324 set_buffer_temp (buffer);
15325 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15326 it.glyph_row->used[TEXT_AREA] = 0;
15327 SET_TEXT_POS (it.position, 0, 0);
15328
15329 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15330 p = arrow_string;
15331 while (p < arrow_end)
15332 {
15333 Lisp_Object face, ilisp;
15334
15335 /* Get the next character. */
15336 if (multibyte_p)
15337 it.c = string_char_and_length (p, arrow_len, &it.len);
15338 else
15339 it.c = *p, it.len = 1;
15340 p += it.len;
15341
15342 /* Get its face. */
15343 ilisp = make_number (p - arrow_string);
15344 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15345 it.face_id = compute_char_face (f, it.c, face);
15346
15347 /* Compute its width, get its glyphs. */
15348 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15349 SET_TEXT_POS (it.position, -1, -1);
15350 PRODUCE_GLYPHS (&it);
15351
15352 /* If this character doesn't fit any more in the line, we have
15353 to remove some glyphs. */
15354 if (it.current_x > it.last_visible_x)
15355 {
15356 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15357 break;
15358 }
15359 }
15360
15361 set_buffer_temp (old);
15362 return it.glyph_row;
15363 }
15364
15365
15366 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15367 glyphs are only inserted for terminal frames since we can't really
15368 win with truncation glyphs when partially visible glyphs are
15369 involved. Which glyphs to insert is determined by
15370 produce_special_glyphs. */
15371
15372 static void
15373 insert_left_trunc_glyphs (it)
15374 struct it *it;
15375 {
15376 struct it truncate_it;
15377 struct glyph *from, *end, *to, *toend;
15378
15379 xassert (!FRAME_WINDOW_P (it->f));
15380
15381 /* Get the truncation glyphs. */
15382 truncate_it = *it;
15383 truncate_it.current_x = 0;
15384 truncate_it.face_id = DEFAULT_FACE_ID;
15385 truncate_it.glyph_row = &scratch_glyph_row;
15386 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15387 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15388 truncate_it.object = make_number (0);
15389 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15390
15391 /* Overwrite glyphs from IT with truncation glyphs. */
15392 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15393 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15394 to = it->glyph_row->glyphs[TEXT_AREA];
15395 toend = to + it->glyph_row->used[TEXT_AREA];
15396
15397 while (from < end)
15398 *to++ = *from++;
15399
15400 /* There may be padding glyphs left over. Overwrite them too. */
15401 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15402 {
15403 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15404 while (from < end)
15405 *to++ = *from++;
15406 }
15407
15408 if (to > toend)
15409 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15410 }
15411
15412
15413 /* Compute the pixel height and width of IT->glyph_row.
15414
15415 Most of the time, ascent and height of a display line will be equal
15416 to the max_ascent and max_height values of the display iterator
15417 structure. This is not the case if
15418
15419 1. We hit ZV without displaying anything. In this case, max_ascent
15420 and max_height will be zero.
15421
15422 2. We have some glyphs that don't contribute to the line height.
15423 (The glyph row flag contributes_to_line_height_p is for future
15424 pixmap extensions).
15425
15426 The first case is easily covered by using default values because in
15427 these cases, the line height does not really matter, except that it
15428 must not be zero. */
15429
15430 static void
15431 compute_line_metrics (it)
15432 struct it *it;
15433 {
15434 struct glyph_row *row = it->glyph_row;
15435 int area, i;
15436
15437 if (FRAME_WINDOW_P (it->f))
15438 {
15439 int i, min_y, max_y;
15440
15441 /* The line may consist of one space only, that was added to
15442 place the cursor on it. If so, the row's height hasn't been
15443 computed yet. */
15444 if (row->height == 0)
15445 {
15446 if (it->max_ascent + it->max_descent == 0)
15447 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15448 row->ascent = it->max_ascent;
15449 row->height = it->max_ascent + it->max_descent;
15450 row->phys_ascent = it->max_phys_ascent;
15451 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15452 row->extra_line_spacing = it->max_extra_line_spacing;
15453 }
15454
15455 /* Compute the width of this line. */
15456 row->pixel_width = row->x;
15457 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15458 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15459
15460 xassert (row->pixel_width >= 0);
15461 xassert (row->ascent >= 0 && row->height > 0);
15462
15463 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15464 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15465
15466 /* If first line's physical ascent is larger than its logical
15467 ascent, use the physical ascent, and make the row taller.
15468 This makes accented characters fully visible. */
15469 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15470 && row->phys_ascent > row->ascent)
15471 {
15472 row->height += row->phys_ascent - row->ascent;
15473 row->ascent = row->phys_ascent;
15474 }
15475
15476 /* Compute how much of the line is visible. */
15477 row->visible_height = row->height;
15478
15479 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15480 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15481
15482 if (row->y < min_y)
15483 row->visible_height -= min_y - row->y;
15484 if (row->y + row->height > max_y)
15485 row->visible_height -= row->y + row->height - max_y;
15486 }
15487 else
15488 {
15489 row->pixel_width = row->used[TEXT_AREA];
15490 if (row->continued_p)
15491 row->pixel_width -= it->continuation_pixel_width;
15492 else if (row->truncated_on_right_p)
15493 row->pixel_width -= it->truncation_pixel_width;
15494 row->ascent = row->phys_ascent = 0;
15495 row->height = row->phys_height = row->visible_height = 1;
15496 row->extra_line_spacing = 0;
15497 }
15498
15499 /* Compute a hash code for this row. */
15500 row->hash = 0;
15501 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15502 for (i = 0; i < row->used[area]; ++i)
15503 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15504 + row->glyphs[area][i].u.val
15505 + row->glyphs[area][i].face_id
15506 + row->glyphs[area][i].padding_p
15507 + (row->glyphs[area][i].type << 2));
15508
15509 it->max_ascent = it->max_descent = 0;
15510 it->max_phys_ascent = it->max_phys_descent = 0;
15511 }
15512
15513
15514 /* Append one space to the glyph row of iterator IT if doing a
15515 window-based redisplay. The space has the same face as
15516 IT->face_id. Value is non-zero if a space was added.
15517
15518 This function is called to make sure that there is always one glyph
15519 at the end of a glyph row that the cursor can be set on under
15520 window-systems. (If there weren't such a glyph we would not know
15521 how wide and tall a box cursor should be displayed).
15522
15523 At the same time this space let's a nicely handle clearing to the
15524 end of the line if the row ends in italic text. */
15525
15526 static int
15527 append_space_for_newline (it, default_face_p)
15528 struct it *it;
15529 int default_face_p;
15530 {
15531 if (FRAME_WINDOW_P (it->f))
15532 {
15533 int n = it->glyph_row->used[TEXT_AREA];
15534
15535 if (it->glyph_row->glyphs[TEXT_AREA] + n
15536 < it->glyph_row->glyphs[1 + TEXT_AREA])
15537 {
15538 /* Save some values that must not be changed.
15539 Must save IT->c and IT->len because otherwise
15540 ITERATOR_AT_END_P wouldn't work anymore after
15541 append_space_for_newline has been called. */
15542 enum display_element_type saved_what = it->what;
15543 int saved_c = it->c, saved_len = it->len;
15544 int saved_x = it->current_x;
15545 int saved_face_id = it->face_id;
15546 struct text_pos saved_pos;
15547 Lisp_Object saved_object;
15548 struct face *face;
15549
15550 saved_object = it->object;
15551 saved_pos = it->position;
15552
15553 it->what = IT_CHARACTER;
15554 bzero (&it->position, sizeof it->position);
15555 it->object = make_number (0);
15556 it->c = ' ';
15557 it->len = 1;
15558
15559 if (default_face_p)
15560 it->face_id = DEFAULT_FACE_ID;
15561 else if (it->face_before_selective_p)
15562 it->face_id = it->saved_face_id;
15563 face = FACE_FROM_ID (it->f, it->face_id);
15564 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15565
15566 PRODUCE_GLYPHS (it);
15567
15568 it->override_ascent = -1;
15569 it->constrain_row_ascent_descent_p = 0;
15570 it->current_x = saved_x;
15571 it->object = saved_object;
15572 it->position = saved_pos;
15573 it->what = saved_what;
15574 it->face_id = saved_face_id;
15575 it->len = saved_len;
15576 it->c = saved_c;
15577 return 1;
15578 }
15579 }
15580
15581 return 0;
15582 }
15583
15584
15585 /* Extend the face of the last glyph in the text area of IT->glyph_row
15586 to the end of the display line. Called from display_line.
15587 If the glyph row is empty, add a space glyph to it so that we
15588 know the face to draw. Set the glyph row flag fill_line_p. */
15589
15590 static void
15591 extend_face_to_end_of_line (it)
15592 struct it *it;
15593 {
15594 struct face *face;
15595 struct frame *f = it->f;
15596
15597 /* If line is already filled, do nothing. */
15598 if (it->current_x >= it->last_visible_x)
15599 return;
15600
15601 /* Face extension extends the background and box of IT->face_id
15602 to the end of the line. If the background equals the background
15603 of the frame, we don't have to do anything. */
15604 if (it->face_before_selective_p)
15605 face = FACE_FROM_ID (it->f, it->saved_face_id);
15606 else
15607 face = FACE_FROM_ID (f, it->face_id);
15608
15609 if (FRAME_WINDOW_P (f)
15610 && it->glyph_row->displays_text_p
15611 && face->box == FACE_NO_BOX
15612 && face->background == FRAME_BACKGROUND_PIXEL (f)
15613 && !face->stipple)
15614 return;
15615
15616 /* Set the glyph row flag indicating that the face of the last glyph
15617 in the text area has to be drawn to the end of the text area. */
15618 it->glyph_row->fill_line_p = 1;
15619
15620 /* If current character of IT is not ASCII, make sure we have the
15621 ASCII face. This will be automatically undone the next time
15622 get_next_display_element returns a multibyte character. Note
15623 that the character will always be single byte in unibyte text. */
15624 if (!ASCII_CHAR_P (it->c))
15625 {
15626 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15627 }
15628
15629 if (FRAME_WINDOW_P (f))
15630 {
15631 /* If the row is empty, add a space with the current face of IT,
15632 so that we know which face to draw. */
15633 if (it->glyph_row->used[TEXT_AREA] == 0)
15634 {
15635 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15636 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15637 it->glyph_row->used[TEXT_AREA] = 1;
15638 }
15639 }
15640 else
15641 {
15642 /* Save some values that must not be changed. */
15643 int saved_x = it->current_x;
15644 struct text_pos saved_pos;
15645 Lisp_Object saved_object;
15646 enum display_element_type saved_what = it->what;
15647 int saved_face_id = it->face_id;
15648
15649 saved_object = it->object;
15650 saved_pos = it->position;
15651
15652 it->what = IT_CHARACTER;
15653 bzero (&it->position, sizeof it->position);
15654 it->object = make_number (0);
15655 it->c = ' ';
15656 it->len = 1;
15657 it->face_id = face->id;
15658
15659 PRODUCE_GLYPHS (it);
15660
15661 while (it->current_x <= it->last_visible_x)
15662 PRODUCE_GLYPHS (it);
15663
15664 /* Don't count these blanks really. It would let us insert a left
15665 truncation glyph below and make us set the cursor on them, maybe. */
15666 it->current_x = saved_x;
15667 it->object = saved_object;
15668 it->position = saved_pos;
15669 it->what = saved_what;
15670 it->face_id = saved_face_id;
15671 }
15672 }
15673
15674
15675 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15676 trailing whitespace. */
15677
15678 static int
15679 trailing_whitespace_p (charpos)
15680 int charpos;
15681 {
15682 int bytepos = CHAR_TO_BYTE (charpos);
15683 int c = 0;
15684
15685 while (bytepos < ZV_BYTE
15686 && (c = FETCH_CHAR (bytepos),
15687 c == ' ' || c == '\t'))
15688 ++bytepos;
15689
15690 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15691 {
15692 if (bytepos != PT_BYTE)
15693 return 1;
15694 }
15695 return 0;
15696 }
15697
15698
15699 /* Highlight trailing whitespace, if any, in ROW. */
15700
15701 void
15702 highlight_trailing_whitespace (f, row)
15703 struct frame *f;
15704 struct glyph_row *row;
15705 {
15706 int used = row->used[TEXT_AREA];
15707
15708 if (used)
15709 {
15710 struct glyph *start = row->glyphs[TEXT_AREA];
15711 struct glyph *glyph = start + used - 1;
15712
15713 /* Skip over glyphs inserted to display the cursor at the
15714 end of a line, for extending the face of the last glyph
15715 to the end of the line on terminals, and for truncation
15716 and continuation glyphs. */
15717 while (glyph >= start
15718 && glyph->type == CHAR_GLYPH
15719 && INTEGERP (glyph->object))
15720 --glyph;
15721
15722 /* If last glyph is a space or stretch, and it's trailing
15723 whitespace, set the face of all trailing whitespace glyphs in
15724 IT->glyph_row to `trailing-whitespace'. */
15725 if (glyph >= start
15726 && BUFFERP (glyph->object)
15727 && (glyph->type == STRETCH_GLYPH
15728 || (glyph->type == CHAR_GLYPH
15729 && glyph->u.ch == ' '))
15730 && trailing_whitespace_p (glyph->charpos))
15731 {
15732 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
15733 if (face_id < 0)
15734 return;
15735
15736 while (glyph >= start
15737 && BUFFERP (glyph->object)
15738 && (glyph->type == STRETCH_GLYPH
15739 || (glyph->type == CHAR_GLYPH
15740 && glyph->u.ch == ' ')))
15741 (glyph--)->face_id = face_id;
15742 }
15743 }
15744 }
15745
15746
15747 /* Value is non-zero if glyph row ROW in window W should be
15748 used to hold the cursor. */
15749
15750 static int
15751 cursor_row_p (w, row)
15752 struct window *w;
15753 struct glyph_row *row;
15754 {
15755 int cursor_row_p = 1;
15756
15757 if (PT == MATRIX_ROW_END_CHARPOS (row))
15758 {
15759 /* If the row ends with a newline from a string, we don't want
15760 the cursor there, but we still want it at the start of the
15761 string if the string starts in this row.
15762 If the row is continued it doesn't end in a newline. */
15763 if (CHARPOS (row->end.string_pos) >= 0)
15764 cursor_row_p = (row->continued_p
15765 || PT >= MATRIX_ROW_START_CHARPOS (row));
15766 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15767 {
15768 /* If the row ends in middle of a real character,
15769 and the line is continued, we want the cursor here.
15770 That's because MATRIX_ROW_END_CHARPOS would equal
15771 PT if PT is before the character. */
15772 if (!row->ends_in_ellipsis_p)
15773 cursor_row_p = row->continued_p;
15774 else
15775 /* If the row ends in an ellipsis, then
15776 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15777 We want that position to be displayed after the ellipsis. */
15778 cursor_row_p = 0;
15779 }
15780 /* If the row ends at ZV, display the cursor at the end of that
15781 row instead of at the start of the row below. */
15782 else if (row->ends_at_zv_p)
15783 cursor_row_p = 1;
15784 else
15785 cursor_row_p = 0;
15786 }
15787
15788 return cursor_row_p;
15789 }
15790
15791
15792 /* Construct the glyph row IT->glyph_row in the desired matrix of
15793 IT->w from text at the current position of IT. See dispextern.h
15794 for an overview of struct it. Value is non-zero if
15795 IT->glyph_row displays text, as opposed to a line displaying ZV
15796 only. */
15797
15798 static int
15799 display_line (it)
15800 struct it *it;
15801 {
15802 struct glyph_row *row = it->glyph_row;
15803 Lisp_Object overlay_arrow_string;
15804
15805 /* We always start displaying at hpos zero even if hscrolled. */
15806 xassert (it->hpos == 0 && it->current_x == 0);
15807
15808 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15809 >= it->w->desired_matrix->nrows)
15810 {
15811 it->w->nrows_scale_factor++;
15812 fonts_changed_p = 1;
15813 return 0;
15814 }
15815
15816 /* Is IT->w showing the region? */
15817 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15818
15819 /* Clear the result glyph row and enable it. */
15820 prepare_desired_row (row);
15821
15822 row->y = it->current_y;
15823 row->start = it->start;
15824 row->continuation_lines_width = it->continuation_lines_width;
15825 row->displays_text_p = 1;
15826 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15827 it->starts_in_middle_of_char_p = 0;
15828
15829 /* Arrange the overlays nicely for our purposes. Usually, we call
15830 display_line on only one line at a time, in which case this
15831 can't really hurt too much, or we call it on lines which appear
15832 one after another in the buffer, in which case all calls to
15833 recenter_overlay_lists but the first will be pretty cheap. */
15834 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15835
15836 /* Move over display elements that are not visible because we are
15837 hscrolled. This may stop at an x-position < IT->first_visible_x
15838 if the first glyph is partially visible or if we hit a line end. */
15839 if (it->current_x < it->first_visible_x)
15840 {
15841 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15842 MOVE_TO_POS | MOVE_TO_X);
15843 }
15844
15845 /* Get the initial row height. This is either the height of the
15846 text hscrolled, if there is any, or zero. */
15847 row->ascent = it->max_ascent;
15848 row->height = it->max_ascent + it->max_descent;
15849 row->phys_ascent = it->max_phys_ascent;
15850 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15851 row->extra_line_spacing = it->max_extra_line_spacing;
15852
15853 /* Loop generating characters. The loop is left with IT on the next
15854 character to display. */
15855 while (1)
15856 {
15857 int n_glyphs_before, hpos_before, x_before;
15858 int x, i, nglyphs;
15859 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15860
15861 /* Retrieve the next thing to display. Value is zero if end of
15862 buffer reached. */
15863 if (!get_next_display_element (it))
15864 {
15865 /* Maybe add a space at the end of this line that is used to
15866 display the cursor there under X. Set the charpos of the
15867 first glyph of blank lines not corresponding to any text
15868 to -1. */
15869 #ifdef HAVE_WINDOW_SYSTEM
15870 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15871 row->exact_window_width_line_p = 1;
15872 else
15873 #endif /* HAVE_WINDOW_SYSTEM */
15874 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15875 || row->used[TEXT_AREA] == 0)
15876 {
15877 row->glyphs[TEXT_AREA]->charpos = -1;
15878 row->displays_text_p = 0;
15879
15880 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15881 && (!MINI_WINDOW_P (it->w)
15882 || (minibuf_level && EQ (it->window, minibuf_window))))
15883 row->indicate_empty_line_p = 1;
15884 }
15885
15886 it->continuation_lines_width = 0;
15887 row->ends_at_zv_p = 1;
15888 break;
15889 }
15890
15891 /* Now, get the metrics of what we want to display. This also
15892 generates glyphs in `row' (which is IT->glyph_row). */
15893 n_glyphs_before = row->used[TEXT_AREA];
15894 x = it->current_x;
15895
15896 /* Remember the line height so far in case the next element doesn't
15897 fit on the line. */
15898 if (!it->truncate_lines_p)
15899 {
15900 ascent = it->max_ascent;
15901 descent = it->max_descent;
15902 phys_ascent = it->max_phys_ascent;
15903 phys_descent = it->max_phys_descent;
15904 }
15905
15906 PRODUCE_GLYPHS (it);
15907
15908 /* If this display element was in marginal areas, continue with
15909 the next one. */
15910 if (it->area != TEXT_AREA)
15911 {
15912 row->ascent = max (row->ascent, it->max_ascent);
15913 row->height = max (row->height, it->max_ascent + it->max_descent);
15914 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15915 row->phys_height = max (row->phys_height,
15916 it->max_phys_ascent + it->max_phys_descent);
15917 row->extra_line_spacing = max (row->extra_line_spacing,
15918 it->max_extra_line_spacing);
15919 set_iterator_to_next (it, 1);
15920 continue;
15921 }
15922
15923 /* Does the display element fit on the line? If we truncate
15924 lines, we should draw past the right edge of the window. If
15925 we don't truncate, we want to stop so that we can display the
15926 continuation glyph before the right margin. If lines are
15927 continued, there are two possible strategies for characters
15928 resulting in more than 1 glyph (e.g. tabs): Display as many
15929 glyphs as possible in this line and leave the rest for the
15930 continuation line, or display the whole element in the next
15931 line. Original redisplay did the former, so we do it also. */
15932 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15933 hpos_before = it->hpos;
15934 x_before = x;
15935
15936 if (/* Not a newline. */
15937 nglyphs > 0
15938 /* Glyphs produced fit entirely in the line. */
15939 && it->current_x < it->last_visible_x)
15940 {
15941 it->hpos += nglyphs;
15942 row->ascent = max (row->ascent, it->max_ascent);
15943 row->height = max (row->height, it->max_ascent + it->max_descent);
15944 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15945 row->phys_height = max (row->phys_height,
15946 it->max_phys_ascent + it->max_phys_descent);
15947 row->extra_line_spacing = max (row->extra_line_spacing,
15948 it->max_extra_line_spacing);
15949 if (it->current_x - it->pixel_width < it->first_visible_x)
15950 row->x = x - it->first_visible_x;
15951 }
15952 else
15953 {
15954 int new_x;
15955 struct glyph *glyph;
15956
15957 for (i = 0; i < nglyphs; ++i, x = new_x)
15958 {
15959 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15960 new_x = x + glyph->pixel_width;
15961
15962 if (/* Lines are continued. */
15963 !it->truncate_lines_p
15964 && (/* Glyph doesn't fit on the line. */
15965 new_x > it->last_visible_x
15966 /* Or it fits exactly on a window system frame. */
15967 || (new_x == it->last_visible_x
15968 && FRAME_WINDOW_P (it->f))))
15969 {
15970 /* End of a continued line. */
15971
15972 if (it->hpos == 0
15973 || (new_x == it->last_visible_x
15974 && FRAME_WINDOW_P (it->f)))
15975 {
15976 /* Current glyph is the only one on the line or
15977 fits exactly on the line. We must continue
15978 the line because we can't draw the cursor
15979 after the glyph. */
15980 row->continued_p = 1;
15981 it->current_x = new_x;
15982 it->continuation_lines_width += new_x;
15983 ++it->hpos;
15984 if (i == nglyphs - 1)
15985 {
15986 set_iterator_to_next (it, 1);
15987 #ifdef HAVE_WINDOW_SYSTEM
15988 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15989 {
15990 if (!get_next_display_element (it))
15991 {
15992 row->exact_window_width_line_p = 1;
15993 it->continuation_lines_width = 0;
15994 row->continued_p = 0;
15995 row->ends_at_zv_p = 1;
15996 }
15997 else if (ITERATOR_AT_END_OF_LINE_P (it))
15998 {
15999 row->continued_p = 0;
16000 row->exact_window_width_line_p = 1;
16001 }
16002 }
16003 #endif /* HAVE_WINDOW_SYSTEM */
16004 }
16005 }
16006 else if (CHAR_GLYPH_PADDING_P (*glyph)
16007 && !FRAME_WINDOW_P (it->f))
16008 {
16009 /* A padding glyph that doesn't fit on this line.
16010 This means the whole character doesn't fit
16011 on the line. */
16012 row->used[TEXT_AREA] = n_glyphs_before;
16013
16014 /* Fill the rest of the row with continuation
16015 glyphs like in 20.x. */
16016 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16017 < row->glyphs[1 + TEXT_AREA])
16018 produce_special_glyphs (it, IT_CONTINUATION);
16019
16020 row->continued_p = 1;
16021 it->current_x = x_before;
16022 it->continuation_lines_width += x_before;
16023
16024 /* Restore the height to what it was before the
16025 element not fitting on the line. */
16026 it->max_ascent = ascent;
16027 it->max_descent = descent;
16028 it->max_phys_ascent = phys_ascent;
16029 it->max_phys_descent = phys_descent;
16030 }
16031 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16032 {
16033 /* A TAB that extends past the right edge of the
16034 window. This produces a single glyph on
16035 window system frames. We leave the glyph in
16036 this row and let it fill the row, but don't
16037 consume the TAB. */
16038 it->continuation_lines_width += it->last_visible_x;
16039 row->ends_in_middle_of_char_p = 1;
16040 row->continued_p = 1;
16041 glyph->pixel_width = it->last_visible_x - x;
16042 it->starts_in_middle_of_char_p = 1;
16043 }
16044 else
16045 {
16046 /* Something other than a TAB that draws past
16047 the right edge of the window. Restore
16048 positions to values before the element. */
16049 row->used[TEXT_AREA] = n_glyphs_before + i;
16050
16051 /* Display continuation glyphs. */
16052 if (!FRAME_WINDOW_P (it->f))
16053 produce_special_glyphs (it, IT_CONTINUATION);
16054 row->continued_p = 1;
16055
16056 it->current_x = x_before;
16057 it->continuation_lines_width += x;
16058 extend_face_to_end_of_line (it);
16059
16060 if (nglyphs > 1 && i > 0)
16061 {
16062 row->ends_in_middle_of_char_p = 1;
16063 it->starts_in_middle_of_char_p = 1;
16064 }
16065
16066 /* Restore the height to what it was before the
16067 element not fitting on the line. */
16068 it->max_ascent = ascent;
16069 it->max_descent = descent;
16070 it->max_phys_ascent = phys_ascent;
16071 it->max_phys_descent = phys_descent;
16072 }
16073
16074 break;
16075 }
16076 else if (new_x > it->first_visible_x)
16077 {
16078 /* Increment number of glyphs actually displayed. */
16079 ++it->hpos;
16080
16081 if (x < it->first_visible_x)
16082 /* Glyph is partially visible, i.e. row starts at
16083 negative X position. */
16084 row->x = x - it->first_visible_x;
16085 }
16086 else
16087 {
16088 /* Glyph is completely off the left margin of the
16089 window. This should not happen because of the
16090 move_it_in_display_line at the start of this
16091 function, unless the text display area of the
16092 window is empty. */
16093 xassert (it->first_visible_x <= it->last_visible_x);
16094 }
16095 }
16096
16097 row->ascent = max (row->ascent, it->max_ascent);
16098 row->height = max (row->height, it->max_ascent + it->max_descent);
16099 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16100 row->phys_height = max (row->phys_height,
16101 it->max_phys_ascent + it->max_phys_descent);
16102 row->extra_line_spacing = max (row->extra_line_spacing,
16103 it->max_extra_line_spacing);
16104
16105 /* End of this display line if row is continued. */
16106 if (row->continued_p || row->ends_at_zv_p)
16107 break;
16108 }
16109
16110 at_end_of_line:
16111 /* Is this a line end? If yes, we're also done, after making
16112 sure that a non-default face is extended up to the right
16113 margin of the window. */
16114 if (ITERATOR_AT_END_OF_LINE_P (it))
16115 {
16116 int used_before = row->used[TEXT_AREA];
16117
16118 row->ends_in_newline_from_string_p = STRINGP (it->object);
16119
16120 #ifdef HAVE_WINDOW_SYSTEM
16121 /* Add a space at the end of the line that is used to
16122 display the cursor there. */
16123 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16124 append_space_for_newline (it, 0);
16125 #endif /* HAVE_WINDOW_SYSTEM */
16126
16127 /* Extend the face to the end of the line. */
16128 extend_face_to_end_of_line (it);
16129
16130 /* Make sure we have the position. */
16131 if (used_before == 0)
16132 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16133
16134 /* Consume the line end. This skips over invisible lines. */
16135 set_iterator_to_next (it, 1);
16136 it->continuation_lines_width = 0;
16137 break;
16138 }
16139
16140 /* Proceed with next display element. Note that this skips
16141 over lines invisible because of selective display. */
16142 set_iterator_to_next (it, 1);
16143
16144 /* If we truncate lines, we are done when the last displayed
16145 glyphs reach past the right margin of the window. */
16146 if (it->truncate_lines_p
16147 && (FRAME_WINDOW_P (it->f)
16148 ? (it->current_x >= it->last_visible_x)
16149 : (it->current_x > it->last_visible_x)))
16150 {
16151 /* Maybe add truncation glyphs. */
16152 if (!FRAME_WINDOW_P (it->f))
16153 {
16154 int i, n;
16155
16156 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16157 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16158 break;
16159
16160 for (n = row->used[TEXT_AREA]; i < n; ++i)
16161 {
16162 row->used[TEXT_AREA] = i;
16163 produce_special_glyphs (it, IT_TRUNCATION);
16164 }
16165 }
16166 #ifdef HAVE_WINDOW_SYSTEM
16167 else
16168 {
16169 /* Don't truncate if we can overflow newline into fringe. */
16170 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16171 {
16172 if (!get_next_display_element (it))
16173 {
16174 it->continuation_lines_width = 0;
16175 row->ends_at_zv_p = 1;
16176 row->exact_window_width_line_p = 1;
16177 break;
16178 }
16179 if (ITERATOR_AT_END_OF_LINE_P (it))
16180 {
16181 row->exact_window_width_line_p = 1;
16182 goto at_end_of_line;
16183 }
16184 }
16185 }
16186 #endif /* HAVE_WINDOW_SYSTEM */
16187
16188 row->truncated_on_right_p = 1;
16189 it->continuation_lines_width = 0;
16190 reseat_at_next_visible_line_start (it, 0);
16191 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16192 it->hpos = hpos_before;
16193 it->current_x = x_before;
16194 break;
16195 }
16196 }
16197
16198 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16199 at the left window margin. */
16200 if (it->first_visible_x
16201 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16202 {
16203 if (!FRAME_WINDOW_P (it->f))
16204 insert_left_trunc_glyphs (it);
16205 row->truncated_on_left_p = 1;
16206 }
16207
16208 /* If the start of this line is the overlay arrow-position, then
16209 mark this glyph row as the one containing the overlay arrow.
16210 This is clearly a mess with variable size fonts. It would be
16211 better to let it be displayed like cursors under X. */
16212 if ((row->displays_text_p || !overlay_arrow_seen)
16213 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16214 !NILP (overlay_arrow_string)))
16215 {
16216 /* Overlay arrow in window redisplay is a fringe bitmap. */
16217 if (STRINGP (overlay_arrow_string))
16218 {
16219 struct glyph_row *arrow_row
16220 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16221 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16222 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16223 struct glyph *p = row->glyphs[TEXT_AREA];
16224 struct glyph *p2, *end;
16225
16226 /* Copy the arrow glyphs. */
16227 while (glyph < arrow_end)
16228 *p++ = *glyph++;
16229
16230 /* Throw away padding glyphs. */
16231 p2 = p;
16232 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16233 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16234 ++p2;
16235 if (p2 > p)
16236 {
16237 while (p2 < end)
16238 *p++ = *p2++;
16239 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16240 }
16241 }
16242 else
16243 {
16244 xassert (INTEGERP (overlay_arrow_string));
16245 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16246 }
16247 overlay_arrow_seen = 1;
16248 }
16249
16250 /* Compute pixel dimensions of this line. */
16251 compute_line_metrics (it);
16252
16253 /* Remember the position at which this line ends. */
16254 row->end = it->current;
16255
16256 /* Record whether this row ends inside an ellipsis. */
16257 row->ends_in_ellipsis_p
16258 = (it->method == GET_FROM_DISPLAY_VECTOR
16259 && it->ellipsis_p);
16260
16261 /* Save fringe bitmaps in this row. */
16262 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16263 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16264 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16265 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16266
16267 it->left_user_fringe_bitmap = 0;
16268 it->left_user_fringe_face_id = 0;
16269 it->right_user_fringe_bitmap = 0;
16270 it->right_user_fringe_face_id = 0;
16271
16272 /* Maybe set the cursor. */
16273 if (it->w->cursor.vpos < 0
16274 && PT >= MATRIX_ROW_START_CHARPOS (row)
16275 && PT <= MATRIX_ROW_END_CHARPOS (row)
16276 && cursor_row_p (it->w, row))
16277 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16278
16279 /* Highlight trailing whitespace. */
16280 if (!NILP (Vshow_trailing_whitespace))
16281 highlight_trailing_whitespace (it->f, it->glyph_row);
16282
16283 /* Prepare for the next line. This line starts horizontally at (X
16284 HPOS) = (0 0). Vertical positions are incremented. As a
16285 convenience for the caller, IT->glyph_row is set to the next
16286 row to be used. */
16287 it->current_x = it->hpos = 0;
16288 it->current_y += row->height;
16289 ++it->vpos;
16290 ++it->glyph_row;
16291 it->start = it->current;
16292 return row->displays_text_p;
16293 }
16294
16295
16296 \f
16297 /***********************************************************************
16298 Menu Bar
16299 ***********************************************************************/
16300
16301 /* Redisplay the menu bar in the frame for window W.
16302
16303 The menu bar of X frames that don't have X toolkit support is
16304 displayed in a special window W->frame->menu_bar_window.
16305
16306 The menu bar of terminal frames is treated specially as far as
16307 glyph matrices are concerned. Menu bar lines are not part of
16308 windows, so the update is done directly on the frame matrix rows
16309 for the menu bar. */
16310
16311 static void
16312 display_menu_bar (w)
16313 struct window *w;
16314 {
16315 struct frame *f = XFRAME (WINDOW_FRAME (w));
16316 struct it it;
16317 Lisp_Object items;
16318 int i;
16319
16320 /* Don't do all this for graphical frames. */
16321 #ifdef HAVE_NTGUI
16322 if (!NILP (Vwindow_system))
16323 return;
16324 #endif
16325 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16326 if (FRAME_X_P (f))
16327 return;
16328 #endif
16329 #ifdef MAC_OS
16330 if (FRAME_MAC_P (f))
16331 return;
16332 #endif
16333
16334 #ifdef USE_X_TOOLKIT
16335 xassert (!FRAME_WINDOW_P (f));
16336 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16337 it.first_visible_x = 0;
16338 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16339 #else /* not USE_X_TOOLKIT */
16340 if (FRAME_WINDOW_P (f))
16341 {
16342 /* Menu bar lines are displayed in the desired matrix of the
16343 dummy window menu_bar_window. */
16344 struct window *menu_w;
16345 xassert (WINDOWP (f->menu_bar_window));
16346 menu_w = XWINDOW (f->menu_bar_window);
16347 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16348 MENU_FACE_ID);
16349 it.first_visible_x = 0;
16350 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16351 }
16352 else
16353 {
16354 /* This is a TTY frame, i.e. character hpos/vpos are used as
16355 pixel x/y. */
16356 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16357 MENU_FACE_ID);
16358 it.first_visible_x = 0;
16359 it.last_visible_x = FRAME_COLS (f);
16360 }
16361 #endif /* not USE_X_TOOLKIT */
16362
16363 if (! mode_line_inverse_video)
16364 /* Force the menu-bar to be displayed in the default face. */
16365 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16366
16367 /* Clear all rows of the menu bar. */
16368 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16369 {
16370 struct glyph_row *row = it.glyph_row + i;
16371 clear_glyph_row (row);
16372 row->enabled_p = 1;
16373 row->full_width_p = 1;
16374 }
16375
16376 /* Display all items of the menu bar. */
16377 items = FRAME_MENU_BAR_ITEMS (it.f);
16378 for (i = 0; i < XVECTOR (items)->size; i += 4)
16379 {
16380 Lisp_Object string;
16381
16382 /* Stop at nil string. */
16383 string = AREF (items, i + 1);
16384 if (NILP (string))
16385 break;
16386
16387 /* Remember where item was displayed. */
16388 AREF (items, i + 3) = make_number (it.hpos);
16389
16390 /* Display the item, pad with one space. */
16391 if (it.current_x < it.last_visible_x)
16392 display_string (NULL, string, Qnil, 0, 0, &it,
16393 SCHARS (string) + 1, 0, 0, -1);
16394 }
16395
16396 /* Fill out the line with spaces. */
16397 if (it.current_x < it.last_visible_x)
16398 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16399
16400 /* Compute the total height of the lines. */
16401 compute_line_metrics (&it);
16402 }
16403
16404
16405 \f
16406 /***********************************************************************
16407 Mode Line
16408 ***********************************************************************/
16409
16410 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16411 FORCE is non-zero, redisplay mode lines unconditionally.
16412 Otherwise, redisplay only mode lines that are garbaged. Value is
16413 the number of windows whose mode lines were redisplayed. */
16414
16415 static int
16416 redisplay_mode_lines (window, force)
16417 Lisp_Object window;
16418 int force;
16419 {
16420 int nwindows = 0;
16421
16422 while (!NILP (window))
16423 {
16424 struct window *w = XWINDOW (window);
16425
16426 if (WINDOWP (w->hchild))
16427 nwindows += redisplay_mode_lines (w->hchild, force);
16428 else if (WINDOWP (w->vchild))
16429 nwindows += redisplay_mode_lines (w->vchild, force);
16430 else if (force
16431 || FRAME_GARBAGED_P (XFRAME (w->frame))
16432 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16433 {
16434 struct text_pos lpoint;
16435 struct buffer *old = current_buffer;
16436
16437 /* Set the window's buffer for the mode line display. */
16438 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16439 set_buffer_internal_1 (XBUFFER (w->buffer));
16440
16441 /* Point refers normally to the selected window. For any
16442 other window, set up appropriate value. */
16443 if (!EQ (window, selected_window))
16444 {
16445 struct text_pos pt;
16446
16447 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16448 if (CHARPOS (pt) < BEGV)
16449 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16450 else if (CHARPOS (pt) > (ZV - 1))
16451 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16452 else
16453 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16454 }
16455
16456 /* Display mode lines. */
16457 clear_glyph_matrix (w->desired_matrix);
16458 if (display_mode_lines (w))
16459 {
16460 ++nwindows;
16461 w->must_be_updated_p = 1;
16462 }
16463
16464 /* Restore old settings. */
16465 set_buffer_internal_1 (old);
16466 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16467 }
16468
16469 window = w->next;
16470 }
16471
16472 return nwindows;
16473 }
16474
16475
16476 /* Display the mode and/or top line of window W. Value is the number
16477 of mode lines displayed. */
16478
16479 static int
16480 display_mode_lines (w)
16481 struct window *w;
16482 {
16483 Lisp_Object old_selected_window, old_selected_frame;
16484 int n = 0;
16485
16486 old_selected_frame = selected_frame;
16487 selected_frame = w->frame;
16488 old_selected_window = selected_window;
16489 XSETWINDOW (selected_window, w);
16490
16491 /* These will be set while the mode line specs are processed. */
16492 line_number_displayed = 0;
16493 w->column_number_displayed = Qnil;
16494
16495 if (WINDOW_WANTS_MODELINE_P (w))
16496 {
16497 struct window *sel_w = XWINDOW (old_selected_window);
16498
16499 /* Select mode line face based on the real selected window. */
16500 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16501 current_buffer->mode_line_format);
16502 ++n;
16503 }
16504
16505 if (WINDOW_WANTS_HEADER_LINE_P (w))
16506 {
16507 display_mode_line (w, HEADER_LINE_FACE_ID,
16508 current_buffer->header_line_format);
16509 ++n;
16510 }
16511
16512 selected_frame = old_selected_frame;
16513 selected_window = old_selected_window;
16514 return n;
16515 }
16516
16517
16518 /* Display mode or top line of window W. FACE_ID specifies which line
16519 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16520 FORMAT is the mode line format to display. Value is the pixel
16521 height of the mode line displayed. */
16522
16523 static int
16524 display_mode_line (w, face_id, format)
16525 struct window *w;
16526 enum face_id face_id;
16527 Lisp_Object format;
16528 {
16529 struct it it;
16530 struct face *face;
16531 int count = SPECPDL_INDEX ();
16532
16533 init_iterator (&it, w, -1, -1, NULL, face_id);
16534 prepare_desired_row (it.glyph_row);
16535
16536 it.glyph_row->mode_line_p = 1;
16537
16538 if (! mode_line_inverse_video)
16539 /* Force the mode-line to be displayed in the default face. */
16540 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16541
16542 record_unwind_protect (unwind_format_mode_line,
16543 format_mode_line_unwind_data (NULL, 0));
16544
16545 mode_line_target = MODE_LINE_DISPLAY;
16546
16547 /* Temporarily make frame's keyboard the current kboard so that
16548 kboard-local variables in the mode_line_format will get the right
16549 values. */
16550 push_frame_kboard (it.f);
16551 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16552 pop_frame_kboard ();
16553
16554 unbind_to (count, Qnil);
16555
16556 /* Fill up with spaces. */
16557 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16558
16559 compute_line_metrics (&it);
16560 it.glyph_row->full_width_p = 1;
16561 it.glyph_row->continued_p = 0;
16562 it.glyph_row->truncated_on_left_p = 0;
16563 it.glyph_row->truncated_on_right_p = 0;
16564
16565 /* Make a 3D mode-line have a shadow at its right end. */
16566 face = FACE_FROM_ID (it.f, face_id);
16567 extend_face_to_end_of_line (&it);
16568 if (face->box != FACE_NO_BOX)
16569 {
16570 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16571 + it.glyph_row->used[TEXT_AREA] - 1);
16572 last->right_box_line_p = 1;
16573 }
16574
16575 return it.glyph_row->height;
16576 }
16577
16578 /* Move element ELT in LIST to the front of LIST.
16579 Return the updated list. */
16580
16581 static Lisp_Object
16582 move_elt_to_front (elt, list)
16583 Lisp_Object elt, list;
16584 {
16585 register Lisp_Object tail, prev;
16586 register Lisp_Object tem;
16587
16588 tail = list;
16589 prev = Qnil;
16590 while (CONSP (tail))
16591 {
16592 tem = XCAR (tail);
16593
16594 if (EQ (elt, tem))
16595 {
16596 /* Splice out the link TAIL. */
16597 if (NILP (prev))
16598 list = XCDR (tail);
16599 else
16600 Fsetcdr (prev, XCDR (tail));
16601
16602 /* Now make it the first. */
16603 Fsetcdr (tail, list);
16604 return tail;
16605 }
16606 else
16607 prev = tail;
16608 tail = XCDR (tail);
16609 QUIT;
16610 }
16611
16612 /* Not found--return unchanged LIST. */
16613 return list;
16614 }
16615
16616 /* Contribute ELT to the mode line for window IT->w. How it
16617 translates into text depends on its data type.
16618
16619 IT describes the display environment in which we display, as usual.
16620
16621 DEPTH is the depth in recursion. It is used to prevent
16622 infinite recursion here.
16623
16624 FIELD_WIDTH is the number of characters the display of ELT should
16625 occupy in the mode line, and PRECISION is the maximum number of
16626 characters to display from ELT's representation. See
16627 display_string for details.
16628
16629 Returns the hpos of the end of the text generated by ELT.
16630
16631 PROPS is a property list to add to any string we encounter.
16632
16633 If RISKY is nonzero, remove (disregard) any properties in any string
16634 we encounter, and ignore :eval and :propertize.
16635
16636 The global variable `mode_line_target' determines whether the
16637 output is passed to `store_mode_line_noprop',
16638 `store_mode_line_string', or `display_string'. */
16639
16640 static int
16641 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16642 struct it *it;
16643 int depth;
16644 int field_width, precision;
16645 Lisp_Object elt, props;
16646 int risky;
16647 {
16648 int n = 0, field, prec;
16649 int literal = 0;
16650
16651 tail_recurse:
16652 if (depth > 100)
16653 elt = build_string ("*too-deep*");
16654
16655 depth++;
16656
16657 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16658 {
16659 case Lisp_String:
16660 {
16661 /* A string: output it and check for %-constructs within it. */
16662 unsigned char c;
16663 int offset = 0;
16664
16665 if (SCHARS (elt) > 0
16666 && (!NILP (props) || risky))
16667 {
16668 Lisp_Object oprops, aelt;
16669 oprops = Ftext_properties_at (make_number (0), elt);
16670
16671 /* If the starting string's properties are not what
16672 we want, translate the string. Also, if the string
16673 is risky, do that anyway. */
16674
16675 if (NILP (Fequal (props, oprops)) || risky)
16676 {
16677 /* If the starting string has properties,
16678 merge the specified ones onto the existing ones. */
16679 if (! NILP (oprops) && !risky)
16680 {
16681 Lisp_Object tem;
16682
16683 oprops = Fcopy_sequence (oprops);
16684 tem = props;
16685 while (CONSP (tem))
16686 {
16687 oprops = Fplist_put (oprops, XCAR (tem),
16688 XCAR (XCDR (tem)));
16689 tem = XCDR (XCDR (tem));
16690 }
16691 props = oprops;
16692 }
16693
16694 aelt = Fassoc (elt, mode_line_proptrans_alist);
16695 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16696 {
16697 /* AELT is what we want. Move it to the front
16698 without consing. */
16699 elt = XCAR (aelt);
16700 mode_line_proptrans_alist
16701 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16702 }
16703 else
16704 {
16705 Lisp_Object tem;
16706
16707 /* If AELT has the wrong props, it is useless.
16708 so get rid of it. */
16709 if (! NILP (aelt))
16710 mode_line_proptrans_alist
16711 = Fdelq (aelt, mode_line_proptrans_alist);
16712
16713 elt = Fcopy_sequence (elt);
16714 Fset_text_properties (make_number (0), Flength (elt),
16715 props, elt);
16716 /* Add this item to mode_line_proptrans_alist. */
16717 mode_line_proptrans_alist
16718 = Fcons (Fcons (elt, props),
16719 mode_line_proptrans_alist);
16720 /* Truncate mode_line_proptrans_alist
16721 to at most 50 elements. */
16722 tem = Fnthcdr (make_number (50),
16723 mode_line_proptrans_alist);
16724 if (! NILP (tem))
16725 XSETCDR (tem, Qnil);
16726 }
16727 }
16728 }
16729
16730 offset = 0;
16731
16732 if (literal)
16733 {
16734 prec = precision - n;
16735 switch (mode_line_target)
16736 {
16737 case MODE_LINE_NOPROP:
16738 case MODE_LINE_TITLE:
16739 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16740 break;
16741 case MODE_LINE_STRING:
16742 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16743 break;
16744 case MODE_LINE_DISPLAY:
16745 n += display_string (NULL, elt, Qnil, 0, 0, it,
16746 0, prec, 0, STRING_MULTIBYTE (elt));
16747 break;
16748 }
16749
16750 break;
16751 }
16752
16753 /* Handle the non-literal case. */
16754
16755 while ((precision <= 0 || n < precision)
16756 && SREF (elt, offset) != 0
16757 && (mode_line_target != MODE_LINE_DISPLAY
16758 || it->current_x < it->last_visible_x))
16759 {
16760 int last_offset = offset;
16761
16762 /* Advance to end of string or next format specifier. */
16763 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16764 ;
16765
16766 if (offset - 1 != last_offset)
16767 {
16768 int nchars, nbytes;
16769
16770 /* Output to end of string or up to '%'. Field width
16771 is length of string. Don't output more than
16772 PRECISION allows us. */
16773 offset--;
16774
16775 prec = c_string_width (SDATA (elt) + last_offset,
16776 offset - last_offset, precision - n,
16777 &nchars, &nbytes);
16778
16779 switch (mode_line_target)
16780 {
16781 case MODE_LINE_NOPROP:
16782 case MODE_LINE_TITLE:
16783 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16784 break;
16785 case MODE_LINE_STRING:
16786 {
16787 int bytepos = last_offset;
16788 int charpos = string_byte_to_char (elt, bytepos);
16789 int endpos = (precision <= 0
16790 ? string_byte_to_char (elt, offset)
16791 : charpos + nchars);
16792
16793 n += store_mode_line_string (NULL,
16794 Fsubstring (elt, make_number (charpos),
16795 make_number (endpos)),
16796 0, 0, 0, Qnil);
16797 }
16798 break;
16799 case MODE_LINE_DISPLAY:
16800 {
16801 int bytepos = last_offset;
16802 int charpos = string_byte_to_char (elt, bytepos);
16803
16804 if (precision <= 0)
16805 nchars = string_byte_to_char (elt, offset) - charpos;
16806 n += display_string (NULL, elt, Qnil, 0, charpos,
16807 it, 0, nchars, 0,
16808 STRING_MULTIBYTE (elt));
16809 }
16810 break;
16811 }
16812 }
16813 else /* c == '%' */
16814 {
16815 int percent_position = offset;
16816
16817 /* Get the specified minimum width. Zero means
16818 don't pad. */
16819 field = 0;
16820 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16821 field = field * 10 + c - '0';
16822
16823 /* Don't pad beyond the total padding allowed. */
16824 if (field_width - n > 0 && field > field_width - n)
16825 field = field_width - n;
16826
16827 /* Note that either PRECISION <= 0 or N < PRECISION. */
16828 prec = precision - n;
16829
16830 if (c == 'M')
16831 n += display_mode_element (it, depth, field, prec,
16832 Vglobal_mode_string, props,
16833 risky);
16834 else if (c != 0)
16835 {
16836 int multibyte;
16837 int bytepos, charpos;
16838 unsigned char *spec;
16839
16840 bytepos = percent_position;
16841 charpos = (STRING_MULTIBYTE (elt)
16842 ? string_byte_to_char (elt, bytepos)
16843 : bytepos);
16844
16845 spec
16846 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16847
16848 switch (mode_line_target)
16849 {
16850 case MODE_LINE_NOPROP:
16851 case MODE_LINE_TITLE:
16852 n += store_mode_line_noprop (spec, field, prec);
16853 break;
16854 case MODE_LINE_STRING:
16855 {
16856 int len = strlen (spec);
16857 Lisp_Object tem = make_string (spec, len);
16858 props = Ftext_properties_at (make_number (charpos), elt);
16859 /* Should only keep face property in props */
16860 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16861 }
16862 break;
16863 case MODE_LINE_DISPLAY:
16864 {
16865 int nglyphs_before, nwritten;
16866
16867 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16868 nwritten = display_string (spec, Qnil, elt,
16869 charpos, 0, it,
16870 field, prec, 0,
16871 multibyte);
16872
16873 /* Assign to the glyphs written above the
16874 string where the `%x' came from, position
16875 of the `%'. */
16876 if (nwritten > 0)
16877 {
16878 struct glyph *glyph
16879 = (it->glyph_row->glyphs[TEXT_AREA]
16880 + nglyphs_before);
16881 int i;
16882
16883 for (i = 0; i < nwritten; ++i)
16884 {
16885 glyph[i].object = elt;
16886 glyph[i].charpos = charpos;
16887 }
16888
16889 n += nwritten;
16890 }
16891 }
16892 break;
16893 }
16894 }
16895 else /* c == 0 */
16896 break;
16897 }
16898 }
16899 }
16900 break;
16901
16902 case Lisp_Symbol:
16903 /* A symbol: process the value of the symbol recursively
16904 as if it appeared here directly. Avoid error if symbol void.
16905 Special case: if value of symbol is a string, output the string
16906 literally. */
16907 {
16908 register Lisp_Object tem;
16909
16910 /* If the variable is not marked as risky to set
16911 then its contents are risky to use. */
16912 if (NILP (Fget (elt, Qrisky_local_variable)))
16913 risky = 1;
16914
16915 tem = Fboundp (elt);
16916 if (!NILP (tem))
16917 {
16918 tem = Fsymbol_value (elt);
16919 /* If value is a string, output that string literally:
16920 don't check for % within it. */
16921 if (STRINGP (tem))
16922 literal = 1;
16923
16924 if (!EQ (tem, elt))
16925 {
16926 /* Give up right away for nil or t. */
16927 elt = tem;
16928 goto tail_recurse;
16929 }
16930 }
16931 }
16932 break;
16933
16934 case Lisp_Cons:
16935 {
16936 register Lisp_Object car, tem;
16937
16938 /* A cons cell: five distinct cases.
16939 If first element is :eval or :propertize, do something special.
16940 If first element is a string or a cons, process all the elements
16941 and effectively concatenate them.
16942 If first element is a negative number, truncate displaying cdr to
16943 at most that many characters. If positive, pad (with spaces)
16944 to at least that many characters.
16945 If first element is a symbol, process the cadr or caddr recursively
16946 according to whether the symbol's value is non-nil or nil. */
16947 car = XCAR (elt);
16948 if (EQ (car, QCeval))
16949 {
16950 /* An element of the form (:eval FORM) means evaluate FORM
16951 and use the result as mode line elements. */
16952
16953 if (risky)
16954 break;
16955
16956 if (CONSP (XCDR (elt)))
16957 {
16958 Lisp_Object spec;
16959 spec = safe_eval (XCAR (XCDR (elt)));
16960 n += display_mode_element (it, depth, field_width - n,
16961 precision - n, spec, props,
16962 risky);
16963 }
16964 }
16965 else if (EQ (car, QCpropertize))
16966 {
16967 /* An element of the form (:propertize ELT PROPS...)
16968 means display ELT but applying properties PROPS. */
16969
16970 if (risky)
16971 break;
16972
16973 if (CONSP (XCDR (elt)))
16974 n += display_mode_element (it, depth, field_width - n,
16975 precision - n, XCAR (XCDR (elt)),
16976 XCDR (XCDR (elt)), risky);
16977 }
16978 else if (SYMBOLP (car))
16979 {
16980 tem = Fboundp (car);
16981 elt = XCDR (elt);
16982 if (!CONSP (elt))
16983 goto invalid;
16984 /* elt is now the cdr, and we know it is a cons cell.
16985 Use its car if CAR has a non-nil value. */
16986 if (!NILP (tem))
16987 {
16988 tem = Fsymbol_value (car);
16989 if (!NILP (tem))
16990 {
16991 elt = XCAR (elt);
16992 goto tail_recurse;
16993 }
16994 }
16995 /* Symbol's value is nil (or symbol is unbound)
16996 Get the cddr of the original list
16997 and if possible find the caddr and use that. */
16998 elt = XCDR (elt);
16999 if (NILP (elt))
17000 break;
17001 else if (!CONSP (elt))
17002 goto invalid;
17003 elt = XCAR (elt);
17004 goto tail_recurse;
17005 }
17006 else if (INTEGERP (car))
17007 {
17008 register int lim = XINT (car);
17009 elt = XCDR (elt);
17010 if (lim < 0)
17011 {
17012 /* Negative int means reduce maximum width. */
17013 if (precision <= 0)
17014 precision = -lim;
17015 else
17016 precision = min (precision, -lim);
17017 }
17018 else if (lim > 0)
17019 {
17020 /* Padding specified. Don't let it be more than
17021 current maximum. */
17022 if (precision > 0)
17023 lim = min (precision, lim);
17024
17025 /* If that's more padding than already wanted, queue it.
17026 But don't reduce padding already specified even if
17027 that is beyond the current truncation point. */
17028 field_width = max (lim, field_width);
17029 }
17030 goto tail_recurse;
17031 }
17032 else if (STRINGP (car) || CONSP (car))
17033 {
17034 register int limit = 50;
17035 /* Limit is to protect against circular lists. */
17036 while (CONSP (elt)
17037 && --limit > 0
17038 && (precision <= 0 || n < precision))
17039 {
17040 n += display_mode_element (it, depth,
17041 /* Do padding only after the last
17042 element in the list. */
17043 (! CONSP (XCDR (elt))
17044 ? field_width - n
17045 : 0),
17046 precision - n, XCAR (elt),
17047 props, risky);
17048 elt = XCDR (elt);
17049 }
17050 }
17051 }
17052 break;
17053
17054 default:
17055 invalid:
17056 elt = build_string ("*invalid*");
17057 goto tail_recurse;
17058 }
17059
17060 /* Pad to FIELD_WIDTH. */
17061 if (field_width > 0 && n < field_width)
17062 {
17063 switch (mode_line_target)
17064 {
17065 case MODE_LINE_NOPROP:
17066 case MODE_LINE_TITLE:
17067 n += store_mode_line_noprop ("", field_width - n, 0);
17068 break;
17069 case MODE_LINE_STRING:
17070 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17071 break;
17072 case MODE_LINE_DISPLAY:
17073 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17074 0, 0, 0);
17075 break;
17076 }
17077 }
17078
17079 return n;
17080 }
17081
17082 /* Store a mode-line string element in mode_line_string_list.
17083
17084 If STRING is non-null, display that C string. Otherwise, the Lisp
17085 string LISP_STRING is displayed.
17086
17087 FIELD_WIDTH is the minimum number of output glyphs to produce.
17088 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17089 with spaces. FIELD_WIDTH <= 0 means don't pad.
17090
17091 PRECISION is the maximum number of characters to output from
17092 STRING. PRECISION <= 0 means don't truncate the string.
17093
17094 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17095 properties to the string.
17096
17097 PROPS are the properties to add to the string.
17098 The mode_line_string_face face property is always added to the string.
17099 */
17100
17101 static int
17102 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17103 char *string;
17104 Lisp_Object lisp_string;
17105 int copy_string;
17106 int field_width;
17107 int precision;
17108 Lisp_Object props;
17109 {
17110 int len;
17111 int n = 0;
17112
17113 if (string != NULL)
17114 {
17115 len = strlen (string);
17116 if (precision > 0 && len > precision)
17117 len = precision;
17118 lisp_string = make_string (string, len);
17119 if (NILP (props))
17120 props = mode_line_string_face_prop;
17121 else if (!NILP (mode_line_string_face))
17122 {
17123 Lisp_Object face = Fplist_get (props, Qface);
17124 props = Fcopy_sequence (props);
17125 if (NILP (face))
17126 face = mode_line_string_face;
17127 else
17128 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17129 props = Fplist_put (props, Qface, face);
17130 }
17131 Fadd_text_properties (make_number (0), make_number (len),
17132 props, lisp_string);
17133 }
17134 else
17135 {
17136 len = XFASTINT (Flength (lisp_string));
17137 if (precision > 0 && len > precision)
17138 {
17139 len = precision;
17140 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17141 precision = -1;
17142 }
17143 if (!NILP (mode_line_string_face))
17144 {
17145 Lisp_Object face;
17146 if (NILP (props))
17147 props = Ftext_properties_at (make_number (0), lisp_string);
17148 face = Fplist_get (props, Qface);
17149 if (NILP (face))
17150 face = mode_line_string_face;
17151 else
17152 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17153 props = Fcons (Qface, Fcons (face, Qnil));
17154 if (copy_string)
17155 lisp_string = Fcopy_sequence (lisp_string);
17156 }
17157 if (!NILP (props))
17158 Fadd_text_properties (make_number (0), make_number (len),
17159 props, lisp_string);
17160 }
17161
17162 if (len > 0)
17163 {
17164 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17165 n += len;
17166 }
17167
17168 if (field_width > len)
17169 {
17170 field_width -= len;
17171 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17172 if (!NILP (props))
17173 Fadd_text_properties (make_number (0), make_number (field_width),
17174 props, lisp_string);
17175 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17176 n += field_width;
17177 }
17178
17179 return n;
17180 }
17181
17182
17183 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17184 1, 4, 0,
17185 doc: /* Format a string out of a mode line format specification.
17186 First arg FORMAT specifies the mode line format (see `mode-line-format'
17187 for details) to use.
17188
17189 Optional second arg FACE specifies the face property to put
17190 on all characters for which no face is specified.
17191 t means whatever face the window's mode line currently uses
17192 \(either `mode-line' or `mode-line-inactive', depending).
17193 nil means the default is no face property.
17194 If FACE is an integer, the value string has no text properties.
17195
17196 Optional third and fourth args WINDOW and BUFFER specify the window
17197 and buffer to use as the context for the formatting (defaults
17198 are the selected window and the window's buffer). */)
17199 (format, face, window, buffer)
17200 Lisp_Object format, face, window, buffer;
17201 {
17202 struct it it;
17203 int len;
17204 struct window *w;
17205 struct buffer *old_buffer = NULL;
17206 int face_id = -1;
17207 int no_props = INTEGERP (face);
17208 int count = SPECPDL_INDEX ();
17209 Lisp_Object str;
17210 int string_start = 0;
17211
17212 if (NILP (window))
17213 window = selected_window;
17214 CHECK_WINDOW (window);
17215 w = XWINDOW (window);
17216
17217 if (NILP (buffer))
17218 buffer = w->buffer;
17219 CHECK_BUFFER (buffer);
17220
17221 if (NILP (format))
17222 return build_string ("");
17223
17224 if (no_props)
17225 face = Qnil;
17226
17227 if (!NILP (face))
17228 {
17229 if (EQ (face, Qt))
17230 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17231 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17232 }
17233
17234 if (face_id < 0)
17235 face_id = DEFAULT_FACE_ID;
17236
17237 if (XBUFFER (buffer) != current_buffer)
17238 old_buffer = current_buffer;
17239
17240 /* Save things including mode_line_proptrans_alist,
17241 and set that to nil so that we don't alter the outer value. */
17242 record_unwind_protect (unwind_format_mode_line,
17243 format_mode_line_unwind_data (old_buffer, 1));
17244 mode_line_proptrans_alist = Qnil;
17245
17246 if (old_buffer)
17247 set_buffer_internal_1 (XBUFFER (buffer));
17248
17249 init_iterator (&it, w, -1, -1, NULL, face_id);
17250
17251 if (no_props)
17252 {
17253 mode_line_target = MODE_LINE_NOPROP;
17254 mode_line_string_face_prop = Qnil;
17255 mode_line_string_list = Qnil;
17256 string_start = MODE_LINE_NOPROP_LEN (0);
17257 }
17258 else
17259 {
17260 mode_line_target = MODE_LINE_STRING;
17261 mode_line_string_list = Qnil;
17262 mode_line_string_face = face;
17263 mode_line_string_face_prop
17264 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17265 }
17266
17267 push_frame_kboard (it.f);
17268 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17269 pop_frame_kboard ();
17270
17271 if (no_props)
17272 {
17273 len = MODE_LINE_NOPROP_LEN (string_start);
17274 str = make_string (mode_line_noprop_buf + string_start, len);
17275 }
17276 else
17277 {
17278 mode_line_string_list = Fnreverse (mode_line_string_list);
17279 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17280 make_string ("", 0));
17281 }
17282
17283 unbind_to (count, Qnil);
17284 return str;
17285 }
17286
17287 /* Write a null-terminated, right justified decimal representation of
17288 the positive integer D to BUF using a minimal field width WIDTH. */
17289
17290 static void
17291 pint2str (buf, width, d)
17292 register char *buf;
17293 register int width;
17294 register int d;
17295 {
17296 register char *p = buf;
17297
17298 if (d <= 0)
17299 *p++ = '0';
17300 else
17301 {
17302 while (d > 0)
17303 {
17304 *p++ = d % 10 + '0';
17305 d /= 10;
17306 }
17307 }
17308
17309 for (width -= (int) (p - buf); width > 0; --width)
17310 *p++ = ' ';
17311 *p-- = '\0';
17312 while (p > buf)
17313 {
17314 d = *buf;
17315 *buf++ = *p;
17316 *p-- = d;
17317 }
17318 }
17319
17320 /* Write a null-terminated, right justified decimal and "human
17321 readable" representation of the nonnegative integer D to BUF using
17322 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17323
17324 static const char power_letter[] =
17325 {
17326 0, /* not used */
17327 'k', /* kilo */
17328 'M', /* mega */
17329 'G', /* giga */
17330 'T', /* tera */
17331 'P', /* peta */
17332 'E', /* exa */
17333 'Z', /* zetta */
17334 'Y' /* yotta */
17335 };
17336
17337 static void
17338 pint2hrstr (buf, width, d)
17339 char *buf;
17340 int width;
17341 int d;
17342 {
17343 /* We aim to represent the nonnegative integer D as
17344 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17345 int quotient = d;
17346 int remainder = 0;
17347 /* -1 means: do not use TENTHS. */
17348 int tenths = -1;
17349 int exponent = 0;
17350
17351 /* Length of QUOTIENT.TENTHS as a string. */
17352 int length;
17353
17354 char * psuffix;
17355 char * p;
17356
17357 if (1000 <= quotient)
17358 {
17359 /* Scale to the appropriate EXPONENT. */
17360 do
17361 {
17362 remainder = quotient % 1000;
17363 quotient /= 1000;
17364 exponent++;
17365 }
17366 while (1000 <= quotient);
17367
17368 /* Round to nearest and decide whether to use TENTHS or not. */
17369 if (quotient <= 9)
17370 {
17371 tenths = remainder / 100;
17372 if (50 <= remainder % 100)
17373 {
17374 if (tenths < 9)
17375 tenths++;
17376 else
17377 {
17378 quotient++;
17379 if (quotient == 10)
17380 tenths = -1;
17381 else
17382 tenths = 0;
17383 }
17384 }
17385 }
17386 else
17387 if (500 <= remainder)
17388 {
17389 if (quotient < 999)
17390 quotient++;
17391 else
17392 {
17393 quotient = 1;
17394 exponent++;
17395 tenths = 0;
17396 }
17397 }
17398 }
17399
17400 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17401 if (tenths == -1 && quotient <= 99)
17402 if (quotient <= 9)
17403 length = 1;
17404 else
17405 length = 2;
17406 else
17407 length = 3;
17408 p = psuffix = buf + max (width, length);
17409
17410 /* Print EXPONENT. */
17411 if (exponent)
17412 *psuffix++ = power_letter[exponent];
17413 *psuffix = '\0';
17414
17415 /* Print TENTHS. */
17416 if (tenths >= 0)
17417 {
17418 *--p = '0' + tenths;
17419 *--p = '.';
17420 }
17421
17422 /* Print QUOTIENT. */
17423 do
17424 {
17425 int digit = quotient % 10;
17426 *--p = '0' + digit;
17427 }
17428 while ((quotient /= 10) != 0);
17429
17430 /* Print leading spaces. */
17431 while (buf < p)
17432 *--p = ' ';
17433 }
17434
17435 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17436 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17437 type of CODING_SYSTEM. Return updated pointer into BUF. */
17438
17439 static unsigned char invalid_eol_type[] = "(*invalid*)";
17440
17441 static char *
17442 decode_mode_spec_coding (coding_system, buf, eol_flag)
17443 Lisp_Object coding_system;
17444 register char *buf;
17445 int eol_flag;
17446 {
17447 Lisp_Object val;
17448 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17449 const unsigned char *eol_str;
17450 int eol_str_len;
17451 /* The EOL conversion we are using. */
17452 Lisp_Object eoltype;
17453
17454 val = CODING_SYSTEM_SPEC (coding_system);
17455 eoltype = Qnil;
17456
17457 if (!VECTORP (val)) /* Not yet decided. */
17458 {
17459 if (multibyte)
17460 *buf++ = '-';
17461 if (eol_flag)
17462 eoltype = eol_mnemonic_undecided;
17463 /* Don't mention EOL conversion if it isn't decided. */
17464 }
17465 else
17466 {
17467 Lisp_Object attrs;
17468 Lisp_Object eolvalue;
17469
17470 attrs = AREF (val, 0);
17471 eolvalue = AREF (val, 2);
17472
17473 if (multibyte)
17474 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17475
17476 if (eol_flag)
17477 {
17478 /* The EOL conversion that is normal on this system. */
17479
17480 if (NILP (eolvalue)) /* Not yet decided. */
17481 eoltype = eol_mnemonic_undecided;
17482 else if (VECTORP (eolvalue)) /* Not yet decided. */
17483 eoltype = eol_mnemonic_undecided;
17484 else /* eolvalue is Qunix, Qdos, or Qmac. */
17485 eoltype = (EQ (eolvalue, Qunix)
17486 ? eol_mnemonic_unix
17487 : (EQ (eolvalue, Qdos) == 1
17488 ? eol_mnemonic_dos : eol_mnemonic_mac));
17489 }
17490 }
17491
17492 if (eol_flag)
17493 {
17494 /* Mention the EOL conversion if it is not the usual one. */
17495 if (STRINGP (eoltype))
17496 {
17497 eol_str = SDATA (eoltype);
17498 eol_str_len = SBYTES (eoltype);
17499 }
17500 else if (CHARACTERP (eoltype))
17501 {
17502 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17503 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17504 eol_str = tmp;
17505 }
17506 else
17507 {
17508 eol_str = invalid_eol_type;
17509 eol_str_len = sizeof (invalid_eol_type) - 1;
17510 }
17511 bcopy (eol_str, buf, eol_str_len);
17512 buf += eol_str_len;
17513 }
17514
17515 return buf;
17516 }
17517
17518 /* Return a string for the output of a mode line %-spec for window W,
17519 generated by character C. PRECISION >= 0 means don't return a
17520 string longer than that value. FIELD_WIDTH > 0 means pad the
17521 string returned with spaces to that value. Return 1 in *MULTIBYTE
17522 if the result is multibyte text.
17523
17524 Note we operate on the current buffer for most purposes,
17525 the exception being w->base_line_pos. */
17526
17527 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17528
17529 static char *
17530 decode_mode_spec (w, c, field_width, precision, multibyte)
17531 struct window *w;
17532 register int c;
17533 int field_width, precision;
17534 int *multibyte;
17535 {
17536 Lisp_Object obj;
17537 struct frame *f = XFRAME (WINDOW_FRAME (w));
17538 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17539 struct buffer *b = current_buffer;
17540
17541 obj = Qnil;
17542 *multibyte = 0;
17543
17544 switch (c)
17545 {
17546 case '*':
17547 if (!NILP (b->read_only))
17548 return "%";
17549 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17550 return "*";
17551 return "-";
17552
17553 case '+':
17554 /* This differs from %* only for a modified read-only buffer. */
17555 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17556 return "*";
17557 if (!NILP (b->read_only))
17558 return "%";
17559 return "-";
17560
17561 case '&':
17562 /* This differs from %* in ignoring read-only-ness. */
17563 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17564 return "*";
17565 return "-";
17566
17567 case '%':
17568 return "%";
17569
17570 case '[':
17571 {
17572 int i;
17573 char *p;
17574
17575 if (command_loop_level > 5)
17576 return "[[[... ";
17577 p = decode_mode_spec_buf;
17578 for (i = 0; i < command_loop_level; i++)
17579 *p++ = '[';
17580 *p = 0;
17581 return decode_mode_spec_buf;
17582 }
17583
17584 case ']':
17585 {
17586 int i;
17587 char *p;
17588
17589 if (command_loop_level > 5)
17590 return " ...]]]";
17591 p = decode_mode_spec_buf;
17592 for (i = 0; i < command_loop_level; i++)
17593 *p++ = ']';
17594 *p = 0;
17595 return decode_mode_spec_buf;
17596 }
17597
17598 case '-':
17599 {
17600 register int i;
17601
17602 /* Let lots_of_dashes be a string of infinite length. */
17603 if (mode_line_target == MODE_LINE_NOPROP ||
17604 mode_line_target == MODE_LINE_STRING)
17605 return "--";
17606 if (field_width <= 0
17607 || field_width > sizeof (lots_of_dashes))
17608 {
17609 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17610 decode_mode_spec_buf[i] = '-';
17611 decode_mode_spec_buf[i] = '\0';
17612 return decode_mode_spec_buf;
17613 }
17614 else
17615 return lots_of_dashes;
17616 }
17617
17618 case 'b':
17619 obj = b->name;
17620 break;
17621
17622 case 'c':
17623 {
17624 int col = (int) current_column (); /* iftc */
17625 w->column_number_displayed = make_number (col);
17626 pint2str (decode_mode_spec_buf, field_width, col);
17627 return decode_mode_spec_buf;
17628 }
17629
17630 case 'e':
17631 #ifndef SYSTEM_MALLOC
17632 {
17633 if (NILP (Vmemory_full))
17634 return "";
17635 else
17636 return "!MEM FULL! ";
17637 }
17638 #else
17639 return "";
17640 #endif
17641
17642 case 'F':
17643 /* %F displays the frame name. */
17644 if (!NILP (f->title))
17645 return (char *) SDATA (f->title);
17646 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17647 return (char *) SDATA (f->name);
17648 return "Emacs";
17649
17650 case 'f':
17651 obj = b->filename;
17652 break;
17653
17654 case 'i':
17655 {
17656 int size = ZV - BEGV;
17657 pint2str (decode_mode_spec_buf, field_width, size);
17658 return decode_mode_spec_buf;
17659 }
17660
17661 case 'I':
17662 {
17663 int size = ZV - BEGV;
17664 pint2hrstr (decode_mode_spec_buf, field_width, size);
17665 return decode_mode_spec_buf;
17666 }
17667
17668 case 'l':
17669 {
17670 int startpos = XMARKER (w->start)->charpos;
17671 int startpos_byte = marker_byte_position (w->start);
17672 int line, linepos, linepos_byte, topline;
17673 int nlines, junk;
17674 int height = WINDOW_TOTAL_LINES (w);
17675
17676 /* If we decided that this buffer isn't suitable for line numbers,
17677 don't forget that too fast. */
17678 if (EQ (w->base_line_pos, w->buffer))
17679 goto no_value;
17680 /* But do forget it, if the window shows a different buffer now. */
17681 else if (BUFFERP (w->base_line_pos))
17682 w->base_line_pos = Qnil;
17683
17684 /* If the buffer is very big, don't waste time. */
17685 if (INTEGERP (Vline_number_display_limit)
17686 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17687 {
17688 w->base_line_pos = Qnil;
17689 w->base_line_number = Qnil;
17690 goto no_value;
17691 }
17692
17693 if (!NILP (w->base_line_number)
17694 && !NILP (w->base_line_pos)
17695 && XFASTINT (w->base_line_pos) <= startpos)
17696 {
17697 line = XFASTINT (w->base_line_number);
17698 linepos = XFASTINT (w->base_line_pos);
17699 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17700 }
17701 else
17702 {
17703 line = 1;
17704 linepos = BUF_BEGV (b);
17705 linepos_byte = BUF_BEGV_BYTE (b);
17706 }
17707
17708 /* Count lines from base line to window start position. */
17709 nlines = display_count_lines (linepos, linepos_byte,
17710 startpos_byte,
17711 startpos, &junk);
17712
17713 topline = nlines + line;
17714
17715 /* Determine a new base line, if the old one is too close
17716 or too far away, or if we did not have one.
17717 "Too close" means it's plausible a scroll-down would
17718 go back past it. */
17719 if (startpos == BUF_BEGV (b))
17720 {
17721 w->base_line_number = make_number (topline);
17722 w->base_line_pos = make_number (BUF_BEGV (b));
17723 }
17724 else if (nlines < height + 25 || nlines > height * 3 + 50
17725 || linepos == BUF_BEGV (b))
17726 {
17727 int limit = BUF_BEGV (b);
17728 int limit_byte = BUF_BEGV_BYTE (b);
17729 int position;
17730 int distance = (height * 2 + 30) * line_number_display_limit_width;
17731
17732 if (startpos - distance > limit)
17733 {
17734 limit = startpos - distance;
17735 limit_byte = CHAR_TO_BYTE (limit);
17736 }
17737
17738 nlines = display_count_lines (startpos, startpos_byte,
17739 limit_byte,
17740 - (height * 2 + 30),
17741 &position);
17742 /* If we couldn't find the lines we wanted within
17743 line_number_display_limit_width chars per line,
17744 give up on line numbers for this window. */
17745 if (position == limit_byte && limit == startpos - distance)
17746 {
17747 w->base_line_pos = w->buffer;
17748 w->base_line_number = Qnil;
17749 goto no_value;
17750 }
17751
17752 w->base_line_number = make_number (topline - nlines);
17753 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17754 }
17755
17756 /* Now count lines from the start pos to point. */
17757 nlines = display_count_lines (startpos, startpos_byte,
17758 PT_BYTE, PT, &junk);
17759
17760 /* Record that we did display the line number. */
17761 line_number_displayed = 1;
17762
17763 /* Make the string to show. */
17764 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17765 return decode_mode_spec_buf;
17766 no_value:
17767 {
17768 char* p = decode_mode_spec_buf;
17769 int pad = field_width - 2;
17770 while (pad-- > 0)
17771 *p++ = ' ';
17772 *p++ = '?';
17773 *p++ = '?';
17774 *p = '\0';
17775 return decode_mode_spec_buf;
17776 }
17777 }
17778 break;
17779
17780 case 'm':
17781 obj = b->mode_name;
17782 break;
17783
17784 case 'n':
17785 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17786 return " Narrow";
17787 break;
17788
17789 case 'p':
17790 {
17791 int pos = marker_position (w->start);
17792 int total = BUF_ZV (b) - BUF_BEGV (b);
17793
17794 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17795 {
17796 if (pos <= BUF_BEGV (b))
17797 return "All";
17798 else
17799 return "Bottom";
17800 }
17801 else if (pos <= BUF_BEGV (b))
17802 return "Top";
17803 else
17804 {
17805 if (total > 1000000)
17806 /* Do it differently for a large value, to avoid overflow. */
17807 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17808 else
17809 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17810 /* We can't normally display a 3-digit number,
17811 so get us a 2-digit number that is close. */
17812 if (total == 100)
17813 total = 99;
17814 sprintf (decode_mode_spec_buf, "%2d%%", total);
17815 return decode_mode_spec_buf;
17816 }
17817 }
17818
17819 /* Display percentage of size above the bottom of the screen. */
17820 case 'P':
17821 {
17822 int toppos = marker_position (w->start);
17823 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17824 int total = BUF_ZV (b) - BUF_BEGV (b);
17825
17826 if (botpos >= BUF_ZV (b))
17827 {
17828 if (toppos <= BUF_BEGV (b))
17829 return "All";
17830 else
17831 return "Bottom";
17832 }
17833 else
17834 {
17835 if (total > 1000000)
17836 /* Do it differently for a large value, to avoid overflow. */
17837 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17838 else
17839 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17840 /* We can't normally display a 3-digit number,
17841 so get us a 2-digit number that is close. */
17842 if (total == 100)
17843 total = 99;
17844 if (toppos <= BUF_BEGV (b))
17845 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17846 else
17847 sprintf (decode_mode_spec_buf, "%2d%%", total);
17848 return decode_mode_spec_buf;
17849 }
17850 }
17851
17852 case 's':
17853 /* status of process */
17854 obj = Fget_buffer_process (Fcurrent_buffer ());
17855 if (NILP (obj))
17856 return "no process";
17857 #ifdef subprocesses
17858 obj = Fsymbol_name (Fprocess_status (obj));
17859 #endif
17860 break;
17861
17862 case 't': /* indicate TEXT or BINARY */
17863 #ifdef MODE_LINE_BINARY_TEXT
17864 return MODE_LINE_BINARY_TEXT (b);
17865 #else
17866 return "T";
17867 #endif
17868
17869 case 'z':
17870 /* coding-system (not including end-of-line format) */
17871 case 'Z':
17872 /* coding-system (including end-of-line type) */
17873 {
17874 int eol_flag = (c == 'Z');
17875 char *p = decode_mode_spec_buf;
17876
17877 if (! FRAME_WINDOW_P (f))
17878 {
17879 /* No need to mention EOL here--the terminal never needs
17880 to do EOL conversion. */
17881 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
17882 p, 0);
17883 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
17884 p, 0);
17885 }
17886 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17887 p, eol_flag);
17888
17889 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17890 #ifdef subprocesses
17891 obj = Fget_buffer_process (Fcurrent_buffer ());
17892 if (PROCESSP (obj))
17893 {
17894 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17895 p, eol_flag);
17896 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17897 p, eol_flag);
17898 }
17899 #endif /* subprocesses */
17900 #endif /* 0 */
17901 *p = 0;
17902 return decode_mode_spec_buf;
17903 }
17904 }
17905
17906 if (STRINGP (obj))
17907 {
17908 *multibyte = STRING_MULTIBYTE (obj);
17909 return (char *) SDATA (obj);
17910 }
17911 else
17912 return "";
17913 }
17914
17915
17916 /* Count up to COUNT lines starting from START / START_BYTE.
17917 But don't go beyond LIMIT_BYTE.
17918 Return the number of lines thus found (always nonnegative).
17919
17920 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17921
17922 static int
17923 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17924 int start, start_byte, limit_byte, count;
17925 int *byte_pos_ptr;
17926 {
17927 register unsigned char *cursor;
17928 unsigned char *base;
17929
17930 register int ceiling;
17931 register unsigned char *ceiling_addr;
17932 int orig_count = count;
17933
17934 /* If we are not in selective display mode,
17935 check only for newlines. */
17936 int selective_display = (!NILP (current_buffer->selective_display)
17937 && !INTEGERP (current_buffer->selective_display));
17938
17939 if (count > 0)
17940 {
17941 while (start_byte < limit_byte)
17942 {
17943 ceiling = BUFFER_CEILING_OF (start_byte);
17944 ceiling = min (limit_byte - 1, ceiling);
17945 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17946 base = (cursor = BYTE_POS_ADDR (start_byte));
17947 while (1)
17948 {
17949 if (selective_display)
17950 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17951 ;
17952 else
17953 while (*cursor != '\n' && ++cursor != ceiling_addr)
17954 ;
17955
17956 if (cursor != ceiling_addr)
17957 {
17958 if (--count == 0)
17959 {
17960 start_byte += cursor - base + 1;
17961 *byte_pos_ptr = start_byte;
17962 return orig_count;
17963 }
17964 else
17965 if (++cursor == ceiling_addr)
17966 break;
17967 }
17968 else
17969 break;
17970 }
17971 start_byte += cursor - base;
17972 }
17973 }
17974 else
17975 {
17976 while (start_byte > limit_byte)
17977 {
17978 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17979 ceiling = max (limit_byte, ceiling);
17980 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17981 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17982 while (1)
17983 {
17984 if (selective_display)
17985 while (--cursor != ceiling_addr
17986 && *cursor != '\n' && *cursor != 015)
17987 ;
17988 else
17989 while (--cursor != ceiling_addr && *cursor != '\n')
17990 ;
17991
17992 if (cursor != ceiling_addr)
17993 {
17994 if (++count == 0)
17995 {
17996 start_byte += cursor - base + 1;
17997 *byte_pos_ptr = start_byte;
17998 /* When scanning backwards, we should
17999 not count the newline posterior to which we stop. */
18000 return - orig_count - 1;
18001 }
18002 }
18003 else
18004 break;
18005 }
18006 /* Here we add 1 to compensate for the last decrement
18007 of CURSOR, which took it past the valid range. */
18008 start_byte += cursor - base + 1;
18009 }
18010 }
18011
18012 *byte_pos_ptr = limit_byte;
18013
18014 if (count < 0)
18015 return - orig_count + count;
18016 return orig_count - count;
18017
18018 }
18019
18020
18021 \f
18022 /***********************************************************************
18023 Displaying strings
18024 ***********************************************************************/
18025
18026 /* Display a NUL-terminated string, starting with index START.
18027
18028 If STRING is non-null, display that C string. Otherwise, the Lisp
18029 string LISP_STRING is displayed.
18030
18031 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18032 FACE_STRING. Display STRING or LISP_STRING with the face at
18033 FACE_STRING_POS in FACE_STRING:
18034
18035 Display the string in the environment given by IT, but use the
18036 standard display table, temporarily.
18037
18038 FIELD_WIDTH is the minimum number of output glyphs to produce.
18039 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18040 with spaces. If STRING has more characters, more than FIELD_WIDTH
18041 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18042
18043 PRECISION is the maximum number of characters to output from
18044 STRING. PRECISION < 0 means don't truncate the string.
18045
18046 This is roughly equivalent to printf format specifiers:
18047
18048 FIELD_WIDTH PRECISION PRINTF
18049 ----------------------------------------
18050 -1 -1 %s
18051 -1 10 %.10s
18052 10 -1 %10s
18053 20 10 %20.10s
18054
18055 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18056 display them, and < 0 means obey the current buffer's value of
18057 enable_multibyte_characters.
18058
18059 Value is the number of columns displayed. */
18060
18061 static int
18062 display_string (string, lisp_string, face_string, face_string_pos,
18063 start, it, field_width, precision, max_x, multibyte)
18064 unsigned char *string;
18065 Lisp_Object lisp_string;
18066 Lisp_Object face_string;
18067 int face_string_pos;
18068 int start;
18069 struct it *it;
18070 int field_width, precision, max_x;
18071 int multibyte;
18072 {
18073 int hpos_at_start = it->hpos;
18074 int saved_face_id = it->face_id;
18075 struct glyph_row *row = it->glyph_row;
18076
18077 /* Initialize the iterator IT for iteration over STRING beginning
18078 with index START. */
18079 reseat_to_string (it, string, lisp_string, start,
18080 precision, field_width, multibyte);
18081
18082 /* If displaying STRING, set up the face of the iterator
18083 from LISP_STRING, if that's given. */
18084 if (STRINGP (face_string))
18085 {
18086 int endptr;
18087 struct face *face;
18088
18089 it->face_id
18090 = face_at_string_position (it->w, face_string, face_string_pos,
18091 0, it->region_beg_charpos,
18092 it->region_end_charpos,
18093 &endptr, it->base_face_id, 0);
18094 face = FACE_FROM_ID (it->f, it->face_id);
18095 it->face_box_p = face->box != FACE_NO_BOX;
18096 }
18097
18098 /* Set max_x to the maximum allowed X position. Don't let it go
18099 beyond the right edge of the window. */
18100 if (max_x <= 0)
18101 max_x = it->last_visible_x;
18102 else
18103 max_x = min (max_x, it->last_visible_x);
18104
18105 /* Skip over display elements that are not visible. because IT->w is
18106 hscrolled. */
18107 if (it->current_x < it->first_visible_x)
18108 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18109 MOVE_TO_POS | MOVE_TO_X);
18110
18111 row->ascent = it->max_ascent;
18112 row->height = it->max_ascent + it->max_descent;
18113 row->phys_ascent = it->max_phys_ascent;
18114 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18115 row->extra_line_spacing = it->max_extra_line_spacing;
18116
18117 /* This condition is for the case that we are called with current_x
18118 past last_visible_x. */
18119 while (it->current_x < max_x)
18120 {
18121 int x_before, x, n_glyphs_before, i, nglyphs;
18122
18123 /* Get the next display element. */
18124 if (!get_next_display_element (it))
18125 break;
18126
18127 /* Produce glyphs. */
18128 x_before = it->current_x;
18129 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18130 PRODUCE_GLYPHS (it);
18131
18132 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18133 i = 0;
18134 x = x_before;
18135 while (i < nglyphs)
18136 {
18137 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18138
18139 if (!it->truncate_lines_p
18140 && x + glyph->pixel_width > max_x)
18141 {
18142 /* End of continued line or max_x reached. */
18143 if (CHAR_GLYPH_PADDING_P (*glyph))
18144 {
18145 /* A wide character is unbreakable. */
18146 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18147 it->current_x = x_before;
18148 }
18149 else
18150 {
18151 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18152 it->current_x = x;
18153 }
18154 break;
18155 }
18156 else if (x + glyph->pixel_width >= it->first_visible_x)
18157 {
18158 /* Glyph is at least partially visible. */
18159 ++it->hpos;
18160 if (x < it->first_visible_x)
18161 it->glyph_row->x = x - it->first_visible_x;
18162 }
18163 else
18164 {
18165 /* Glyph is off the left margin of the display area.
18166 Should not happen. */
18167 abort ();
18168 }
18169
18170 row->ascent = max (row->ascent, it->max_ascent);
18171 row->height = max (row->height, it->max_ascent + it->max_descent);
18172 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18173 row->phys_height = max (row->phys_height,
18174 it->max_phys_ascent + it->max_phys_descent);
18175 row->extra_line_spacing = max (row->extra_line_spacing,
18176 it->max_extra_line_spacing);
18177 x += glyph->pixel_width;
18178 ++i;
18179 }
18180
18181 /* Stop if max_x reached. */
18182 if (i < nglyphs)
18183 break;
18184
18185 /* Stop at line ends. */
18186 if (ITERATOR_AT_END_OF_LINE_P (it))
18187 {
18188 it->continuation_lines_width = 0;
18189 break;
18190 }
18191
18192 set_iterator_to_next (it, 1);
18193
18194 /* Stop if truncating at the right edge. */
18195 if (it->truncate_lines_p
18196 && it->current_x >= it->last_visible_x)
18197 {
18198 /* Add truncation mark, but don't do it if the line is
18199 truncated at a padding space. */
18200 if (IT_CHARPOS (*it) < it->string_nchars)
18201 {
18202 if (!FRAME_WINDOW_P (it->f))
18203 {
18204 int i, n;
18205
18206 if (it->current_x > it->last_visible_x)
18207 {
18208 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18209 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18210 break;
18211 for (n = row->used[TEXT_AREA]; i < n; ++i)
18212 {
18213 row->used[TEXT_AREA] = i;
18214 produce_special_glyphs (it, IT_TRUNCATION);
18215 }
18216 }
18217 produce_special_glyphs (it, IT_TRUNCATION);
18218 }
18219 it->glyph_row->truncated_on_right_p = 1;
18220 }
18221 break;
18222 }
18223 }
18224
18225 /* Maybe insert a truncation at the left. */
18226 if (it->first_visible_x
18227 && IT_CHARPOS (*it) > 0)
18228 {
18229 if (!FRAME_WINDOW_P (it->f))
18230 insert_left_trunc_glyphs (it);
18231 it->glyph_row->truncated_on_left_p = 1;
18232 }
18233
18234 it->face_id = saved_face_id;
18235
18236 /* Value is number of columns displayed. */
18237 return it->hpos - hpos_at_start;
18238 }
18239
18240
18241 \f
18242 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18243 appears as an element of LIST or as the car of an element of LIST.
18244 If PROPVAL is a list, compare each element against LIST in that
18245 way, and return 1/2 if any element of PROPVAL is found in LIST.
18246 Otherwise return 0. This function cannot quit.
18247 The return value is 2 if the text is invisible but with an ellipsis
18248 and 1 if it's invisible and without an ellipsis. */
18249
18250 int
18251 invisible_p (propval, list)
18252 register Lisp_Object propval;
18253 Lisp_Object list;
18254 {
18255 register Lisp_Object tail, proptail;
18256
18257 for (tail = list; CONSP (tail); tail = XCDR (tail))
18258 {
18259 register Lisp_Object tem;
18260 tem = XCAR (tail);
18261 if (EQ (propval, tem))
18262 return 1;
18263 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18264 return NILP (XCDR (tem)) ? 1 : 2;
18265 }
18266
18267 if (CONSP (propval))
18268 {
18269 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18270 {
18271 Lisp_Object propelt;
18272 propelt = XCAR (proptail);
18273 for (tail = list; CONSP (tail); tail = XCDR (tail))
18274 {
18275 register Lisp_Object tem;
18276 tem = XCAR (tail);
18277 if (EQ (propelt, tem))
18278 return 1;
18279 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18280 return NILP (XCDR (tem)) ? 1 : 2;
18281 }
18282 }
18283 }
18284
18285 return 0;
18286 }
18287
18288 /* Calculate a width or height in pixels from a specification using
18289 the following elements:
18290
18291 SPEC ::=
18292 NUM - a (fractional) multiple of the default font width/height
18293 (NUM) - specifies exactly NUM pixels
18294 UNIT - a fixed number of pixels, see below.
18295 ELEMENT - size of a display element in pixels, see below.
18296 (NUM . SPEC) - equals NUM * SPEC
18297 (+ SPEC SPEC ...) - add pixel values
18298 (- SPEC SPEC ...) - subtract pixel values
18299 (- SPEC) - negate pixel value
18300
18301 NUM ::=
18302 INT or FLOAT - a number constant
18303 SYMBOL - use symbol's (buffer local) variable binding.
18304
18305 UNIT ::=
18306 in - pixels per inch *)
18307 mm - pixels per 1/1000 meter *)
18308 cm - pixels per 1/100 meter *)
18309 width - width of current font in pixels.
18310 height - height of current font in pixels.
18311
18312 *) using the ratio(s) defined in display-pixels-per-inch.
18313
18314 ELEMENT ::=
18315
18316 left-fringe - left fringe width in pixels
18317 right-fringe - right fringe width in pixels
18318
18319 left-margin - left margin width in pixels
18320 right-margin - right margin width in pixels
18321
18322 scroll-bar - scroll-bar area width in pixels
18323
18324 Examples:
18325
18326 Pixels corresponding to 5 inches:
18327 (5 . in)
18328
18329 Total width of non-text areas on left side of window (if scroll-bar is on left):
18330 '(space :width (+ left-fringe left-margin scroll-bar))
18331
18332 Align to first text column (in header line):
18333 '(space :align-to 0)
18334
18335 Align to middle of text area minus half the width of variable `my-image'
18336 containing a loaded image:
18337 '(space :align-to (0.5 . (- text my-image)))
18338
18339 Width of left margin minus width of 1 character in the default font:
18340 '(space :width (- left-margin 1))
18341
18342 Width of left margin minus width of 2 characters in the current font:
18343 '(space :width (- left-margin (2 . width)))
18344
18345 Center 1 character over left-margin (in header line):
18346 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18347
18348 Different ways to express width of left fringe plus left margin minus one pixel:
18349 '(space :width (- (+ left-fringe left-margin) (1)))
18350 '(space :width (+ left-fringe left-margin (- (1))))
18351 '(space :width (+ left-fringe left-margin (-1)))
18352
18353 */
18354
18355 #define NUMVAL(X) \
18356 ((INTEGERP (X) || FLOATP (X)) \
18357 ? XFLOATINT (X) \
18358 : - 1)
18359
18360 int
18361 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18362 double *res;
18363 struct it *it;
18364 Lisp_Object prop;
18365 void *font;
18366 int width_p, *align_to;
18367 {
18368 double pixels;
18369
18370 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18371 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18372
18373 if (NILP (prop))
18374 return OK_PIXELS (0);
18375
18376 if (SYMBOLP (prop))
18377 {
18378 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18379 {
18380 char *unit = SDATA (SYMBOL_NAME (prop));
18381
18382 if (unit[0] == 'i' && unit[1] == 'n')
18383 pixels = 1.0;
18384 else if (unit[0] == 'm' && unit[1] == 'm')
18385 pixels = 25.4;
18386 else if (unit[0] == 'c' && unit[1] == 'm')
18387 pixels = 2.54;
18388 else
18389 pixels = 0;
18390 if (pixels > 0)
18391 {
18392 double ppi;
18393 #ifdef HAVE_WINDOW_SYSTEM
18394 if (FRAME_WINDOW_P (it->f)
18395 && (ppi = (width_p
18396 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18397 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18398 ppi > 0))
18399 return OK_PIXELS (ppi / pixels);
18400 #endif
18401
18402 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18403 || (CONSP (Vdisplay_pixels_per_inch)
18404 && (ppi = (width_p
18405 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18406 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18407 ppi > 0)))
18408 return OK_PIXELS (ppi / pixels);
18409
18410 return 0;
18411 }
18412 }
18413
18414 #ifdef HAVE_WINDOW_SYSTEM
18415 if (EQ (prop, Qheight))
18416 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18417 if (EQ (prop, Qwidth))
18418 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18419 #else
18420 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18421 return OK_PIXELS (1);
18422 #endif
18423
18424 if (EQ (prop, Qtext))
18425 return OK_PIXELS (width_p
18426 ? window_box_width (it->w, TEXT_AREA)
18427 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18428
18429 if (align_to && *align_to < 0)
18430 {
18431 *res = 0;
18432 if (EQ (prop, Qleft))
18433 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18434 if (EQ (prop, Qright))
18435 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18436 if (EQ (prop, Qcenter))
18437 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18438 + window_box_width (it->w, TEXT_AREA) / 2);
18439 if (EQ (prop, Qleft_fringe))
18440 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18441 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18442 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18443 if (EQ (prop, Qright_fringe))
18444 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18445 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18446 : window_box_right_offset (it->w, TEXT_AREA));
18447 if (EQ (prop, Qleft_margin))
18448 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18449 if (EQ (prop, Qright_margin))
18450 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18451 if (EQ (prop, Qscroll_bar))
18452 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18453 ? 0
18454 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18455 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18456 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18457 : 0)));
18458 }
18459 else
18460 {
18461 if (EQ (prop, Qleft_fringe))
18462 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18463 if (EQ (prop, Qright_fringe))
18464 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18465 if (EQ (prop, Qleft_margin))
18466 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18467 if (EQ (prop, Qright_margin))
18468 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18469 if (EQ (prop, Qscroll_bar))
18470 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18471 }
18472
18473 prop = Fbuffer_local_value (prop, it->w->buffer);
18474 }
18475
18476 if (INTEGERP (prop) || FLOATP (prop))
18477 {
18478 int base_unit = (width_p
18479 ? FRAME_COLUMN_WIDTH (it->f)
18480 : FRAME_LINE_HEIGHT (it->f));
18481 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18482 }
18483
18484 if (CONSP (prop))
18485 {
18486 Lisp_Object car = XCAR (prop);
18487 Lisp_Object cdr = XCDR (prop);
18488
18489 if (SYMBOLP (car))
18490 {
18491 #ifdef HAVE_WINDOW_SYSTEM
18492 if (valid_image_p (prop))
18493 {
18494 int id = lookup_image (it->f, prop);
18495 struct image *img = IMAGE_FROM_ID (it->f, id);
18496
18497 return OK_PIXELS (width_p ? img->width : img->height);
18498 }
18499 #endif
18500 if (EQ (car, Qplus) || EQ (car, Qminus))
18501 {
18502 int first = 1;
18503 double px;
18504
18505 pixels = 0;
18506 while (CONSP (cdr))
18507 {
18508 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18509 font, width_p, align_to))
18510 return 0;
18511 if (first)
18512 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18513 else
18514 pixels += px;
18515 cdr = XCDR (cdr);
18516 }
18517 if (EQ (car, Qminus))
18518 pixels = -pixels;
18519 return OK_PIXELS (pixels);
18520 }
18521
18522 car = Fbuffer_local_value (car, it->w->buffer);
18523 }
18524
18525 if (INTEGERP (car) || FLOATP (car))
18526 {
18527 double fact;
18528 pixels = XFLOATINT (car);
18529 if (NILP (cdr))
18530 return OK_PIXELS (pixels);
18531 if (calc_pixel_width_or_height (&fact, it, cdr,
18532 font, width_p, align_to))
18533 return OK_PIXELS (pixels * fact);
18534 return 0;
18535 }
18536
18537 return 0;
18538 }
18539
18540 return 0;
18541 }
18542
18543 \f
18544 /***********************************************************************
18545 Glyph Display
18546 ***********************************************************************/
18547
18548 #ifdef HAVE_WINDOW_SYSTEM
18549
18550 #if GLYPH_DEBUG
18551
18552 void
18553 dump_glyph_string (s)
18554 struct glyph_string *s;
18555 {
18556 fprintf (stderr, "glyph string\n");
18557 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18558 s->x, s->y, s->width, s->height);
18559 fprintf (stderr, " ybase = %d\n", s->ybase);
18560 fprintf (stderr, " hl = %d\n", s->hl);
18561 fprintf (stderr, " left overhang = %d, right = %d\n",
18562 s->left_overhang, s->right_overhang);
18563 fprintf (stderr, " nchars = %d\n", s->nchars);
18564 fprintf (stderr, " extends to end of line = %d\n",
18565 s->extends_to_end_of_line_p);
18566 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18567 fprintf (stderr, " bg width = %d\n", s->background_width);
18568 }
18569
18570 #endif /* GLYPH_DEBUG */
18571
18572 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18573 of XChar2b structures for S; it can't be allocated in
18574 init_glyph_string because it must be allocated via `alloca'. W
18575 is the window on which S is drawn. ROW and AREA are the glyph row
18576 and area within the row from which S is constructed. START is the
18577 index of the first glyph structure covered by S. HL is a
18578 face-override for drawing S. */
18579
18580 #ifdef HAVE_NTGUI
18581 #define OPTIONAL_HDC(hdc) hdc,
18582 #define DECLARE_HDC(hdc) HDC hdc;
18583 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18584 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18585 #endif
18586
18587 #ifndef OPTIONAL_HDC
18588 #define OPTIONAL_HDC(hdc)
18589 #define DECLARE_HDC(hdc)
18590 #define ALLOCATE_HDC(hdc, f)
18591 #define RELEASE_HDC(hdc, f)
18592 #endif
18593
18594 static void
18595 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18596 struct glyph_string *s;
18597 DECLARE_HDC (hdc)
18598 XChar2b *char2b;
18599 struct window *w;
18600 struct glyph_row *row;
18601 enum glyph_row_area area;
18602 int start;
18603 enum draw_glyphs_face hl;
18604 {
18605 bzero (s, sizeof *s);
18606 s->w = w;
18607 s->f = XFRAME (w->frame);
18608 #ifdef HAVE_NTGUI
18609 s->hdc = hdc;
18610 #endif
18611 s->display = FRAME_X_DISPLAY (s->f);
18612 s->window = FRAME_X_WINDOW (s->f);
18613 s->char2b = char2b;
18614 s->hl = hl;
18615 s->row = row;
18616 s->area = area;
18617 s->first_glyph = row->glyphs[area] + start;
18618 s->height = row->height;
18619 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18620
18621 /* Display the internal border below the tool-bar window. */
18622 if (WINDOWP (s->f->tool_bar_window)
18623 && s->w == XWINDOW (s->f->tool_bar_window))
18624 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18625
18626 s->ybase = s->y + row->ascent;
18627 }
18628
18629
18630 /* Append the list of glyph strings with head H and tail T to the list
18631 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18632
18633 static INLINE void
18634 append_glyph_string_lists (head, tail, h, t)
18635 struct glyph_string **head, **tail;
18636 struct glyph_string *h, *t;
18637 {
18638 if (h)
18639 {
18640 if (*head)
18641 (*tail)->next = h;
18642 else
18643 *head = h;
18644 h->prev = *tail;
18645 *tail = t;
18646 }
18647 }
18648
18649
18650 /* Prepend the list of glyph strings with head H and tail T to the
18651 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18652 result. */
18653
18654 static INLINE void
18655 prepend_glyph_string_lists (head, tail, h, t)
18656 struct glyph_string **head, **tail;
18657 struct glyph_string *h, *t;
18658 {
18659 if (h)
18660 {
18661 if (*head)
18662 (*head)->prev = t;
18663 else
18664 *tail = t;
18665 t->next = *head;
18666 *head = h;
18667 }
18668 }
18669
18670
18671 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18672 Set *HEAD and *TAIL to the resulting list. */
18673
18674 static INLINE void
18675 append_glyph_string (head, tail, s)
18676 struct glyph_string **head, **tail;
18677 struct glyph_string *s;
18678 {
18679 s->next = s->prev = NULL;
18680 append_glyph_string_lists (head, tail, s, s);
18681 }
18682
18683
18684 /* Get face and two-byte form of character glyph GLYPH on frame F.
18685 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18686 a pointer to a realized face that is ready for display. */
18687
18688 static INLINE struct face *
18689 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18690 struct frame *f;
18691 struct glyph *glyph;
18692 XChar2b *char2b;
18693 int *two_byte_p;
18694 {
18695 struct face *face;
18696
18697 xassert (glyph->type == CHAR_GLYPH);
18698 face = FACE_FROM_ID (f, glyph->face_id);
18699
18700 if (two_byte_p)
18701 *two_byte_p = 0;
18702
18703 #ifdef USE_FONT_BACKEND
18704 if (enable_font_backend)
18705 {
18706 struct font *font = (struct font *) face->font_info;
18707
18708 if (font)
18709 {
18710 unsigned code = font->driver->encode_char (font, glyph->u.ch);
18711
18712 if (code != FONT_INVALID_CODE)
18713 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
18714 else
18715 STORE_XCHAR2B (char2b, 0, code);
18716 }
18717 }
18718 else
18719 #endif /* USE_FONT_BACKEND */
18720 if (!glyph->multibyte_p)
18721 {
18722 /* Unibyte case. We don't have to encode, but we have to make
18723 sure to use a face suitable for unibyte. */
18724 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18725 }
18726 else if (glyph->u.ch < 128)
18727 {
18728 /* Case of ASCII in a face known to fit ASCII. */
18729 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18730 }
18731 else
18732 {
18733 struct font_info *font_info
18734 = FONT_INFO_FROM_ID (f, face->font_info_id);
18735 if (font_info)
18736 {
18737 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
18738 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
18739
18740 if (CHARSET_DIMENSION (charset) == 1)
18741 STORE_XCHAR2B (char2b, 0, code);
18742 else
18743 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
18744
18745 /* Maybe encode the character in *CHAR2B. */
18746 if (CHARSET_ID (charset) != charset_ascii)
18747 {
18748 glyph->font_type
18749 = rif->encode_char (glyph->u.ch, char2b, font_info, charset,
18750 two_byte_p);
18751 }
18752 }
18753 }
18754
18755 /* Make sure X resources of the face are allocated. */
18756 xassert (face != NULL);
18757 PREPARE_FACE_FOR_DISPLAY (f, face);
18758 return face;
18759 }
18760
18761
18762 /* Fill glyph string S with composition components specified by S->cmp.
18763
18764 FACES is an array of faces for all components of this composition.
18765 S->gidx is the index of the first component for S.
18766
18767 OVERLAPS non-zero means S should draw the foreground only, and use
18768 its physical height for clipping. See also draw_glyphs.
18769
18770 Value is the index of a component not in S. */
18771
18772 static int
18773 fill_composite_glyph_string (s, faces, overlaps)
18774 struct glyph_string *s;
18775 struct face **faces;
18776 int overlaps;
18777 {
18778 int i;
18779
18780 xassert (s);
18781
18782 s->for_overlaps = overlaps;
18783
18784 s->face = faces[s->gidx];
18785 if (s->face == NULL)
18786 {
18787 s->font = NULL;
18788 s->font_info = NULL;
18789 }
18790 else
18791 {
18792 s->font = s->face->font;
18793 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
18794 }
18795
18796 #ifdef USE_FONT_BACKEND
18797 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
18798 {
18799 Lisp_Object gstring
18800 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
18801 s->cmp->hash_index * 2);
18802
18803 for (i = 0; i < s->cmp->glyph_len; i++)
18804 {
18805 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
18806 unsigned code = XUINT (LGLYPH_CODE (g));
18807
18808 STORE_XCHAR2B (s->char2b + i, code >> 8, code & 0xFF);
18809 }
18810 s->nchars = s->cmp->glyph_len;
18811 s->width = s->cmp->pixel_width;
18812 }
18813 else
18814 {
18815 #endif /* USE_FONT_BACKEND */
18816 /* For all glyphs of this composition, starting at the offset
18817 S->gidx, until we reach the end of the definition or encounter a
18818 glyph that requires the different face, add it to S. */
18819 ++s->nchars;
18820 for (i = s->gidx + 1;
18821 i < s->cmp->glyph_len && (faces[i] == s->face || ! faces[i] || ! s->face);
18822 ++i)
18823 ++s->nchars;
18824
18825 /* All glyph strings for the same composition has the same width,
18826 i.e. the width set for the first component of the composition. */
18827
18828 s->width = s->first_glyph->pixel_width;
18829 #ifdef USE_FONT_BACKEND
18830 }
18831 #endif /* USE_FONT_BACKEND */
18832
18833 /* If the specified font could not be loaded, use the frame's
18834 default font, but record the fact that we couldn't load it in
18835 the glyph string so that we can draw rectangles for the
18836 characters of the glyph string. */
18837 if (s->font == NULL)
18838 {
18839 s->font_not_found_p = 1;
18840 s->font = FRAME_FONT (s->f);
18841 }
18842
18843 /* Adjust base line for subscript/superscript text. */
18844 s->ybase += s->first_glyph->voffset;
18845
18846 /* This glyph string must always be drawn with 16-bit functions. */
18847 s->two_byte_p = 1;
18848
18849 return s->gidx + s->nchars;
18850 }
18851
18852
18853 /* Fill glyph string S from a sequence of character glyphs.
18854
18855 FACE_ID is the face id of the string. START is the index of the
18856 first glyph to consider, END is the index of the last + 1.
18857 OVERLAPS non-zero means S should draw the foreground only, and use
18858 its physical height for clipping. See also draw_glyphs.
18859
18860 Value is the index of the first glyph not in S. */
18861
18862 static int
18863 fill_glyph_string (s, face_id, start, end, overlaps)
18864 struct glyph_string *s;
18865 int face_id;
18866 int start, end, overlaps;
18867 {
18868 struct glyph *glyph, *last;
18869 int voffset;
18870 int glyph_not_available_p;
18871
18872 xassert (s->f == XFRAME (s->w->frame));
18873 xassert (s->nchars == 0);
18874 xassert (start >= 0 && end > start);
18875
18876 s->for_overlaps = overlaps,
18877 glyph = s->row->glyphs[s->area] + start;
18878 last = s->row->glyphs[s->area] + end;
18879 voffset = glyph->voffset;
18880
18881 glyph_not_available_p = glyph->glyph_not_available_p;
18882
18883 while (glyph < last
18884 && glyph->type == CHAR_GLYPH
18885 && glyph->voffset == voffset
18886 /* Same face id implies same font, nowadays. */
18887 && glyph->face_id == face_id
18888 && glyph->glyph_not_available_p == glyph_not_available_p)
18889 {
18890 int two_byte_p;
18891
18892 s->face = get_glyph_face_and_encoding (s->f, glyph,
18893 s->char2b + s->nchars,
18894 &two_byte_p);
18895 s->two_byte_p = two_byte_p;
18896 ++s->nchars;
18897 xassert (s->nchars <= end - start);
18898 s->width += glyph->pixel_width;
18899 ++glyph;
18900 }
18901
18902 s->font = s->face->font;
18903 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
18904
18905 /* If the specified font could not be loaded, use the frame's font,
18906 but record the fact that we couldn't load it in
18907 S->font_not_found_p so that we can draw rectangles for the
18908 characters of the glyph string. */
18909 if (s->font == NULL || glyph_not_available_p)
18910 {
18911 s->font_not_found_p = 1;
18912 s->font = FRAME_FONT (s->f);
18913 }
18914
18915 /* Adjust base line for subscript/superscript text. */
18916 s->ybase += voffset;
18917
18918 xassert (s->face && s->face->gc);
18919 return glyph - s->row->glyphs[s->area];
18920 }
18921
18922
18923 /* Fill glyph string S from image glyph S->first_glyph. */
18924
18925 static void
18926 fill_image_glyph_string (s)
18927 struct glyph_string *s;
18928 {
18929 xassert (s->first_glyph->type == IMAGE_GLYPH);
18930 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18931 xassert (s->img);
18932 s->slice = s->first_glyph->slice;
18933 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18934 s->font = s->face->font;
18935 s->width = s->first_glyph->pixel_width;
18936
18937 /* Adjust base line for subscript/superscript text. */
18938 s->ybase += s->first_glyph->voffset;
18939 }
18940
18941
18942 /* Fill glyph string S from a sequence of stretch glyphs.
18943
18944 ROW is the glyph row in which the glyphs are found, AREA is the
18945 area within the row. START is the index of the first glyph to
18946 consider, END is the index of the last + 1.
18947
18948 Value is the index of the first glyph not in S. */
18949
18950 static int
18951 fill_stretch_glyph_string (s, row, area, start, end)
18952 struct glyph_string *s;
18953 struct glyph_row *row;
18954 enum glyph_row_area area;
18955 int start, end;
18956 {
18957 struct glyph *glyph, *last;
18958 int voffset, face_id;
18959
18960 xassert (s->first_glyph->type == STRETCH_GLYPH);
18961
18962 glyph = s->row->glyphs[s->area] + start;
18963 last = s->row->glyphs[s->area] + end;
18964 face_id = glyph->face_id;
18965 s->face = FACE_FROM_ID (s->f, face_id);
18966 s->font = s->face->font;
18967 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
18968 s->width = glyph->pixel_width;
18969 s->nchars = 1;
18970 voffset = glyph->voffset;
18971
18972 for (++glyph;
18973 (glyph < last
18974 && glyph->type == STRETCH_GLYPH
18975 && glyph->voffset == voffset
18976 && glyph->face_id == face_id);
18977 ++glyph)
18978 s->width += glyph->pixel_width;
18979
18980 /* Adjust base line for subscript/superscript text. */
18981 s->ybase += voffset;
18982
18983 /* The case that face->gc == 0 is handled when drawing the glyph
18984 string by calling PREPARE_FACE_FOR_DISPLAY. */
18985 xassert (s->face);
18986 return glyph - s->row->glyphs[s->area];
18987 }
18988
18989 static XCharStruct *
18990 get_per_char_metric (font, font_info, char2b, font_type)
18991 XFontStruct *font;
18992 struct font_info *font_info;
18993 XChar2b *char2b;
18994 int font_type;
18995 {
18996 #ifdef USE_FONT_BACKEND
18997 if (enable_font_backend)
18998 {
18999 static XCharStruct pcm_value;
19000 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19001 struct font *fontp;
19002 struct font_metrics metrics;
19003
19004 if (! font_info || code == FONT_INVALID_CODE)
19005 return NULL;
19006 fontp = (struct font *) font_info;
19007 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19008 pcm_value.lbearing = metrics.lbearing;
19009 pcm_value.rbearing = metrics.rbearing;
19010 pcm_value.ascent = metrics.ascent;
19011 pcm_value.descent = metrics.descent;
19012 pcm_value.width = metrics.width;
19013 return &pcm_value;
19014 }
19015 #endif /* USE_FONT_BACKEND */
19016 return rif->per_char_metric (font, char2b, font_type);
19017 }
19018
19019 /* EXPORT for RIF:
19020 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19021 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19022 assumed to be zero. */
19023
19024 void
19025 x_get_glyph_overhangs (glyph, f, left, right)
19026 struct glyph *glyph;
19027 struct frame *f;
19028 int *left, *right;
19029 {
19030 *left = *right = 0;
19031
19032 if (glyph->type == CHAR_GLYPH)
19033 {
19034 XFontStruct *font;
19035 struct face *face;
19036 struct font_info *font_info;
19037 XChar2b char2b;
19038 XCharStruct *pcm;
19039
19040 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19041 font = face->font;
19042 font_info = FONT_INFO_FROM_FACE (f, face);
19043 if (font /* ++KFS: Should this be font_info ? */
19044 && (pcm = get_per_char_metric (font, font_info, &char2b, glyph->font_type)))
19045 {
19046 if (pcm->rbearing > pcm->width)
19047 *right = pcm->rbearing - pcm->width;
19048 if (pcm->lbearing < 0)
19049 *left = -pcm->lbearing;
19050 }
19051 }
19052 else if (glyph->type == COMPOSITE_GLYPH)
19053 {
19054 struct composition *cmp = composition_table[glyph->u.cmp_id];
19055
19056 *right = cmp->rbearing - cmp->pixel_width;
19057 *left = - cmp->lbearing;
19058 }
19059 }
19060
19061
19062 /* Return the index of the first glyph preceding glyph string S that
19063 is overwritten by S because of S's left overhang. Value is -1
19064 if no glyphs are overwritten. */
19065
19066 static int
19067 left_overwritten (s)
19068 struct glyph_string *s;
19069 {
19070 int k;
19071
19072 if (s->left_overhang)
19073 {
19074 int x = 0, i;
19075 struct glyph *glyphs = s->row->glyphs[s->area];
19076 int first = s->first_glyph - glyphs;
19077
19078 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19079 x -= glyphs[i].pixel_width;
19080
19081 k = i + 1;
19082 }
19083 else
19084 k = -1;
19085
19086 return k;
19087 }
19088
19089
19090 /* Return the index of the first glyph preceding glyph string S that
19091 is overwriting S because of its right overhang. Value is -1 if no
19092 glyph in front of S overwrites S. */
19093
19094 static int
19095 left_overwriting (s)
19096 struct glyph_string *s;
19097 {
19098 int i, k, x;
19099 struct glyph *glyphs = s->row->glyphs[s->area];
19100 int first = s->first_glyph - glyphs;
19101
19102 k = -1;
19103 x = 0;
19104 for (i = first - 1; i >= 0; --i)
19105 {
19106 int left, right;
19107 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19108 if (x + right > 0)
19109 k = i;
19110 x -= glyphs[i].pixel_width;
19111 }
19112
19113 return k;
19114 }
19115
19116
19117 /* Return the index of the last glyph following glyph string S that is
19118 not overwritten by S because of S's right overhang. Value is -1 if
19119 no such glyph is found. */
19120
19121 static int
19122 right_overwritten (s)
19123 struct glyph_string *s;
19124 {
19125 int k = -1;
19126
19127 if (s->right_overhang)
19128 {
19129 int x = 0, i;
19130 struct glyph *glyphs = s->row->glyphs[s->area];
19131 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19132 int end = s->row->used[s->area];
19133
19134 for (i = first; i < end && s->right_overhang > x; ++i)
19135 x += glyphs[i].pixel_width;
19136
19137 k = i;
19138 }
19139
19140 return k;
19141 }
19142
19143
19144 /* Return the index of the last glyph following glyph string S that
19145 overwrites S because of its left overhang. Value is negative
19146 if no such glyph is found. */
19147
19148 static int
19149 right_overwriting (s)
19150 struct glyph_string *s;
19151 {
19152 int i, k, x;
19153 int end = s->row->used[s->area];
19154 struct glyph *glyphs = s->row->glyphs[s->area];
19155 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19156
19157 k = -1;
19158 x = 0;
19159 for (i = first; i < end; ++i)
19160 {
19161 int left, right;
19162 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19163 if (x - left < 0)
19164 k = i;
19165 x += glyphs[i].pixel_width;
19166 }
19167
19168 return k;
19169 }
19170
19171
19172 /* Get face and two-byte form of character C in face FACE_ID on frame
19173 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19174 means we want to display multibyte text. DISPLAY_P non-zero means
19175 make sure that X resources for the face returned are allocated.
19176 Value is a pointer to a realized face that is ready for display if
19177 DISPLAY_P is non-zero. */
19178
19179 static INLINE struct face *
19180 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19181 struct frame *f;
19182 int c, face_id;
19183 XChar2b *char2b;
19184 int multibyte_p, display_p;
19185 {
19186 struct face *face = FACE_FROM_ID (f, face_id);
19187
19188 #ifdef USE_FONT_BACKEND
19189 if (enable_font_backend)
19190 {
19191 struct font *font = (struct font *) face->font_info;
19192
19193 if (font)
19194 {
19195 unsigned code = font->driver->encode_char (font, c);
19196
19197 if (code != FONT_INVALID_CODE)
19198 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19199 else
19200 STORE_XCHAR2B (char2b, 0, 0);
19201 }
19202 }
19203 else
19204 #endif /* USE_FONT_BACKEND */
19205 if (!multibyte_p)
19206 {
19207 /* Unibyte case. We don't have to encode, but we have to make
19208 sure to use a face suitable for unibyte. */
19209 STORE_XCHAR2B (char2b, 0, c);
19210 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
19211 face = FACE_FROM_ID (f, face_id);
19212 }
19213 else if (c < 128)
19214 {
19215 /* Case of ASCII in a face known to fit ASCII. */
19216 STORE_XCHAR2B (char2b, 0, c);
19217 }
19218 else if (face->font != NULL)
19219 {
19220 struct font_info *font_info
19221 = FONT_INFO_FROM_ID (f, face->font_info_id);
19222 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19223 unsigned code = ENCODE_CHAR (charset, c);
19224
19225 if (CHARSET_DIMENSION (charset) == 1)
19226 STORE_XCHAR2B (char2b, 0, code);
19227 else
19228 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19229 /* Maybe encode the character in *CHAR2B. */
19230 rif->encode_char (c, char2b, font_info, charset, NULL);
19231 }
19232
19233 /* Make sure X resources of the face are allocated. */
19234 #ifdef HAVE_X_WINDOWS
19235 if (display_p)
19236 #endif
19237 {
19238 xassert (face != NULL);
19239 PREPARE_FACE_FOR_DISPLAY (f, face);
19240 }
19241
19242 return face;
19243 }
19244
19245
19246 /* Set background width of glyph string S. START is the index of the
19247 first glyph following S. LAST_X is the right-most x-position + 1
19248 in the drawing area. */
19249
19250 static INLINE void
19251 set_glyph_string_background_width (s, start, last_x)
19252 struct glyph_string *s;
19253 int start;
19254 int last_x;
19255 {
19256 /* If the face of this glyph string has to be drawn to the end of
19257 the drawing area, set S->extends_to_end_of_line_p. */
19258
19259 if (start == s->row->used[s->area]
19260 && s->area == TEXT_AREA
19261 && ((s->row->fill_line_p
19262 && (s->hl == DRAW_NORMAL_TEXT
19263 || s->hl == DRAW_IMAGE_RAISED
19264 || s->hl == DRAW_IMAGE_SUNKEN))
19265 || s->hl == DRAW_MOUSE_FACE))
19266 s->extends_to_end_of_line_p = 1;
19267
19268 /* If S extends its face to the end of the line, set its
19269 background_width to the distance to the right edge of the drawing
19270 area. */
19271 if (s->extends_to_end_of_line_p)
19272 s->background_width = last_x - s->x + 1;
19273 else
19274 s->background_width = s->width;
19275 }
19276
19277
19278 /* Compute overhangs and x-positions for glyph string S and its
19279 predecessors, or successors. X is the starting x-position for S.
19280 BACKWARD_P non-zero means process predecessors. */
19281
19282 static void
19283 compute_overhangs_and_x (s, x, backward_p)
19284 struct glyph_string *s;
19285 int x;
19286 int backward_p;
19287 {
19288 if (backward_p)
19289 {
19290 while (s)
19291 {
19292 if (rif->compute_glyph_string_overhangs)
19293 rif->compute_glyph_string_overhangs (s);
19294 x -= s->width;
19295 s->x = x;
19296 s = s->prev;
19297 }
19298 }
19299 else
19300 {
19301 while (s)
19302 {
19303 if (rif->compute_glyph_string_overhangs)
19304 rif->compute_glyph_string_overhangs (s);
19305 s->x = x;
19306 x += s->width;
19307 s = s->next;
19308 }
19309 }
19310 }
19311
19312
19313
19314 /* The following macros are only called from draw_glyphs below.
19315 They reference the following parameters of that function directly:
19316 `w', `row', `area', and `overlap_p'
19317 as well as the following local variables:
19318 `s', `f', and `hdc' (in W32) */
19319
19320 #ifdef HAVE_NTGUI
19321 /* On W32, silently add local `hdc' variable to argument list of
19322 init_glyph_string. */
19323 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19324 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19325 #else
19326 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19327 init_glyph_string (s, char2b, w, row, area, start, hl)
19328 #endif
19329
19330 /* Add a glyph string for a stretch glyph to the list of strings
19331 between HEAD and TAIL. START is the index of the stretch glyph in
19332 row area AREA of glyph row ROW. END is the index of the last glyph
19333 in that glyph row area. X is the current output position assigned
19334 to the new glyph string constructed. HL overrides that face of the
19335 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19336 is the right-most x-position of the drawing area. */
19337
19338 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19339 and below -- keep them on one line. */
19340 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19341 do \
19342 { \
19343 s = (struct glyph_string *) alloca (sizeof *s); \
19344 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19345 START = fill_stretch_glyph_string (s, row, area, START, END); \
19346 append_glyph_string (&HEAD, &TAIL, s); \
19347 s->x = (X); \
19348 } \
19349 while (0)
19350
19351
19352 /* Add a glyph string for an image glyph to the list of strings
19353 between HEAD and TAIL. START is the index of the image glyph in
19354 row area AREA of glyph row ROW. END is the index of the last glyph
19355 in that glyph row area. X is the current output position assigned
19356 to the new glyph string constructed. HL overrides that face of the
19357 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19358 is the right-most x-position of the drawing area. */
19359
19360 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19361 do \
19362 { \
19363 s = (struct glyph_string *) alloca (sizeof *s); \
19364 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19365 fill_image_glyph_string (s); \
19366 append_glyph_string (&HEAD, &TAIL, s); \
19367 ++START; \
19368 s->x = (X); \
19369 } \
19370 while (0)
19371
19372
19373 /* Add a glyph string for a sequence of character glyphs to the list
19374 of strings between HEAD and TAIL. START is the index of the first
19375 glyph in row area AREA of glyph row ROW that is part of the new
19376 glyph string. END is the index of the last glyph in that glyph row
19377 area. X is the current output position assigned to the new glyph
19378 string constructed. HL overrides that face of the glyph; e.g. it
19379 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19380 right-most x-position of the drawing area. */
19381
19382 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19383 do \
19384 { \
19385 int face_id; \
19386 XChar2b *char2b; \
19387 \
19388 face_id = (row)->glyphs[area][START].face_id; \
19389 \
19390 s = (struct glyph_string *) alloca (sizeof *s); \
19391 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19392 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19393 append_glyph_string (&HEAD, &TAIL, s); \
19394 s->x = (X); \
19395 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19396 } \
19397 while (0)
19398
19399
19400 /* Add a glyph string for a composite sequence to the list of strings
19401 between HEAD and TAIL. START is the index of the first glyph in
19402 row area AREA of glyph row ROW that is part of the new glyph
19403 string. END is the index of the last glyph in that glyph row area.
19404 X is the current output position assigned to the new glyph string
19405 constructed. HL overrides that face of the glyph; e.g. it is
19406 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19407 x-position of the drawing area. */
19408
19409 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19410 do { \
19411 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19412 int face_id = (row)->glyphs[area][START].face_id; \
19413 struct face *base_face = FACE_FROM_ID (f, face_id); \
19414 struct composition *cmp = composition_table[cmp_id]; \
19415 int glyph_len = cmp->glyph_len; \
19416 XChar2b *char2b; \
19417 struct face **faces; \
19418 struct glyph_string *first_s = NULL; \
19419 int n; \
19420 \
19421 if (cmp->method > COMPOSITION_WITH_RULE_ALTCHARS) \
19422 { \
19423 /* This happens only when USE_FONT_BACKEND is defined. */ \
19424 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19425 faces = &base_face; \
19426 } \
19427 else \
19428 { \
19429 base_face = base_face->ascii_face; \
19430 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19431 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19432 /* At first, fill in `char2b' and `faces'. */ \
19433 for (n = 0; n < glyph_len; n++) \
19434 { \
19435 int c = COMPOSITION_GLYPH (cmp, n); \
19436 \
19437 if (c == '\t') \
19438 faces[n] = NULL; \
19439 else \
19440 { \
19441 int this_face_id = FACE_FOR_CHAR (f, base_face, c, -1, Qnil); \
19442 faces[n] = FACE_FROM_ID (f, this_face_id); \
19443 get_char_face_and_encoding (f, c, this_face_id, \
19444 char2b + n, 1, 1); \
19445 } \
19446 } \
19447 } \
19448 \
19449 /* Make glyph_strings for each glyph sequence that is drawable by \
19450 the same face, and append them to HEAD/TAIL. */ \
19451 for (n = 0; n < cmp->glyph_len;) \
19452 { \
19453 s = (struct glyph_string *) alloca (sizeof *s); \
19454 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19455 append_glyph_string (&(HEAD), &(TAIL), s); \
19456 s->cmp = cmp; \
19457 s->gidx = n; \
19458 s->x = (X); \
19459 \
19460 if (n == 0) \
19461 first_s = s; \
19462 \
19463 n = fill_composite_glyph_string (s, faces, overlaps); \
19464 } \
19465 \
19466 ++START; \
19467 s = first_s; \
19468 } while (0)
19469
19470
19471 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19472 of AREA of glyph row ROW on window W between indices START and END.
19473 HL overrides the face for drawing glyph strings, e.g. it is
19474 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19475 x-positions of the drawing area.
19476
19477 This is an ugly monster macro construct because we must use alloca
19478 to allocate glyph strings (because draw_glyphs can be called
19479 asynchronously). */
19480
19481 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19482 do \
19483 { \
19484 HEAD = TAIL = NULL; \
19485 while (START < END) \
19486 { \
19487 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19488 switch (first_glyph->type) \
19489 { \
19490 case CHAR_GLYPH: \
19491 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19492 HL, X, LAST_X); \
19493 break; \
19494 \
19495 case COMPOSITE_GLYPH: \
19496 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19497 HL, X, LAST_X); \
19498 break; \
19499 \
19500 case STRETCH_GLYPH: \
19501 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19502 HL, X, LAST_X); \
19503 break; \
19504 \
19505 case IMAGE_GLYPH: \
19506 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19507 HL, X, LAST_X); \
19508 break; \
19509 \
19510 default: \
19511 abort (); \
19512 } \
19513 \
19514 if (s) \
19515 { \
19516 set_glyph_string_background_width (s, START, LAST_X); \
19517 (X) += s->width; \
19518 } \
19519 } \
19520 } \
19521 while (0)
19522
19523
19524 /* Draw glyphs between START and END in AREA of ROW on window W,
19525 starting at x-position X. X is relative to AREA in W. HL is a
19526 face-override with the following meaning:
19527
19528 DRAW_NORMAL_TEXT draw normally
19529 DRAW_CURSOR draw in cursor face
19530 DRAW_MOUSE_FACE draw in mouse face.
19531 DRAW_INVERSE_VIDEO draw in mode line face
19532 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19533 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19534
19535 If OVERLAPS is non-zero, draw only the foreground of characters and
19536 clip to the physical height of ROW. Non-zero value also defines
19537 the overlapping part to be drawn:
19538
19539 OVERLAPS_PRED overlap with preceding rows
19540 OVERLAPS_SUCC overlap with succeeding rows
19541 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19542 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19543
19544 Value is the x-position reached, relative to AREA of W. */
19545
19546 static int
19547 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19548 struct window *w;
19549 int x;
19550 struct glyph_row *row;
19551 enum glyph_row_area area;
19552 EMACS_INT start, end;
19553 enum draw_glyphs_face hl;
19554 int overlaps;
19555 {
19556 struct glyph_string *head, *tail;
19557 struct glyph_string *s;
19558 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19559 int last_x, area_width;
19560 int x_reached;
19561 int i, j;
19562 struct frame *f = XFRAME (WINDOW_FRAME (w));
19563 DECLARE_HDC (hdc);
19564
19565 ALLOCATE_HDC (hdc, f);
19566
19567 /* Let's rather be paranoid than getting a SEGV. */
19568 end = min (end, row->used[area]);
19569 start = max (0, start);
19570 start = min (end, start);
19571
19572 /* Translate X to frame coordinates. Set last_x to the right
19573 end of the drawing area. */
19574 if (row->full_width_p)
19575 {
19576 /* X is relative to the left edge of W, without scroll bars
19577 or fringes. */
19578 x += WINDOW_LEFT_EDGE_X (w);
19579 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19580 }
19581 else
19582 {
19583 int area_left = window_box_left (w, area);
19584 x += area_left;
19585 area_width = window_box_width (w, area);
19586 last_x = area_left + area_width;
19587 }
19588
19589 /* Build a doubly-linked list of glyph_string structures between
19590 head and tail from what we have to draw. Note that the macro
19591 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19592 the reason we use a separate variable `i'. */
19593 i = start;
19594 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19595 if (tail)
19596 x_reached = tail->x + tail->background_width;
19597 else
19598 x_reached = x;
19599
19600 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19601 the row, redraw some glyphs in front or following the glyph
19602 strings built above. */
19603 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19604 {
19605 int dummy_x = 0;
19606 struct glyph_string *h, *t;
19607
19608 /* Compute overhangs for all glyph strings. */
19609 if (rif->compute_glyph_string_overhangs)
19610 for (s = head; s; s = s->next)
19611 rif->compute_glyph_string_overhangs (s);
19612
19613 /* Prepend glyph strings for glyphs in front of the first glyph
19614 string that are overwritten because of the first glyph
19615 string's left overhang. The background of all strings
19616 prepended must be drawn because the first glyph string
19617 draws over it. */
19618 i = left_overwritten (head);
19619 if (i >= 0)
19620 {
19621 j = i;
19622 BUILD_GLYPH_STRINGS (j, start, h, t,
19623 DRAW_NORMAL_TEXT, dummy_x, last_x);
19624 start = i;
19625 compute_overhangs_and_x (t, head->x, 1);
19626 prepend_glyph_string_lists (&head, &tail, h, t);
19627 clip_head = head;
19628 }
19629
19630 /* Prepend glyph strings for glyphs in front of the first glyph
19631 string that overwrite that glyph string because of their
19632 right overhang. For these strings, only the foreground must
19633 be drawn, because it draws over the glyph string at `head'.
19634 The background must not be drawn because this would overwrite
19635 right overhangs of preceding glyphs for which no glyph
19636 strings exist. */
19637 i = left_overwriting (head);
19638 if (i >= 0)
19639 {
19640 clip_head = head;
19641 BUILD_GLYPH_STRINGS (i, start, h, t,
19642 DRAW_NORMAL_TEXT, dummy_x, last_x);
19643 for (s = h; s; s = s->next)
19644 s->background_filled_p = 1;
19645 compute_overhangs_and_x (t, head->x, 1);
19646 prepend_glyph_string_lists (&head, &tail, h, t);
19647 }
19648
19649 /* Append glyphs strings for glyphs following the last glyph
19650 string tail that are overwritten by tail. The background of
19651 these strings has to be drawn because tail's foreground draws
19652 over it. */
19653 i = right_overwritten (tail);
19654 if (i >= 0)
19655 {
19656 BUILD_GLYPH_STRINGS (end, i, h, t,
19657 DRAW_NORMAL_TEXT, x, last_x);
19658 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19659 append_glyph_string_lists (&head, &tail, h, t);
19660 clip_tail = tail;
19661 }
19662
19663 /* Append glyph strings for glyphs following the last glyph
19664 string tail that overwrite tail. The foreground of such
19665 glyphs has to be drawn because it writes into the background
19666 of tail. The background must not be drawn because it could
19667 paint over the foreground of following glyphs. */
19668 i = right_overwriting (tail);
19669 if (i >= 0)
19670 {
19671 clip_tail = tail;
19672 i++; /* We must include the Ith glyph. */
19673 BUILD_GLYPH_STRINGS (end, i, h, t,
19674 DRAW_NORMAL_TEXT, x, last_x);
19675 for (s = h; s; s = s->next)
19676 s->background_filled_p = 1;
19677 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19678 append_glyph_string_lists (&head, &tail, h, t);
19679 }
19680 if (clip_head || clip_tail)
19681 for (s = head; s; s = s->next)
19682 {
19683 s->clip_head = clip_head;
19684 s->clip_tail = clip_tail;
19685 }
19686 }
19687
19688 /* Draw all strings. */
19689 for (s = head; s; s = s->next)
19690 rif->draw_glyph_string (s);
19691
19692 if (area == TEXT_AREA
19693 && !row->full_width_p
19694 /* When drawing overlapping rows, only the glyph strings'
19695 foreground is drawn, which doesn't erase a cursor
19696 completely. */
19697 && !overlaps)
19698 {
19699 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19700 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19701 : (tail ? tail->x + tail->background_width : x));
19702
19703 int text_left = window_box_left (w, TEXT_AREA);
19704 x0 -= text_left;
19705 x1 -= text_left;
19706
19707 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19708 row->y, MATRIX_ROW_BOTTOM_Y (row));
19709 }
19710
19711 /* Value is the x-position up to which drawn, relative to AREA of W.
19712 This doesn't include parts drawn because of overhangs. */
19713 if (row->full_width_p)
19714 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19715 else
19716 x_reached -= window_box_left (w, area);
19717
19718 RELEASE_HDC (hdc, f);
19719
19720 return x_reached;
19721 }
19722
19723 /* Expand row matrix if too narrow. Don't expand if area
19724 is not present. */
19725
19726 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19727 { \
19728 if (!fonts_changed_p \
19729 && (it->glyph_row->glyphs[area] \
19730 < it->glyph_row->glyphs[area + 1])) \
19731 { \
19732 it->w->ncols_scale_factor++; \
19733 fonts_changed_p = 1; \
19734 } \
19735 }
19736
19737 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19738 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19739
19740 static INLINE void
19741 append_glyph (it)
19742 struct it *it;
19743 {
19744 struct glyph *glyph;
19745 enum glyph_row_area area = it->area;
19746
19747 xassert (it->glyph_row);
19748 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19749
19750 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19751 if (glyph < it->glyph_row->glyphs[area + 1])
19752 {
19753 glyph->charpos = CHARPOS (it->position);
19754 glyph->object = it->object;
19755 glyph->pixel_width = it->pixel_width;
19756 glyph->ascent = it->ascent;
19757 glyph->descent = it->descent;
19758 glyph->voffset = it->voffset;
19759 glyph->type = CHAR_GLYPH;
19760 glyph->multibyte_p = it->multibyte_p;
19761 glyph->left_box_line_p = it->start_of_box_run_p;
19762 glyph->right_box_line_p = it->end_of_box_run_p;
19763 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19764 || it->phys_descent > it->descent);
19765 glyph->padding_p = 0;
19766 glyph->glyph_not_available_p = it->glyph_not_available_p;
19767 glyph->face_id = it->face_id;
19768 glyph->u.ch = it->char_to_display;
19769 glyph->slice = null_glyph_slice;
19770 glyph->font_type = FONT_TYPE_UNKNOWN;
19771 ++it->glyph_row->used[area];
19772 }
19773 else
19774 IT_EXPAND_MATRIX_WIDTH (it, area);
19775 }
19776
19777 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19778 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19779
19780 static INLINE void
19781 append_composite_glyph (it)
19782 struct it *it;
19783 {
19784 struct glyph *glyph;
19785 enum glyph_row_area area = it->area;
19786
19787 xassert (it->glyph_row);
19788
19789 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19790 if (glyph < it->glyph_row->glyphs[area + 1])
19791 {
19792 glyph->charpos = CHARPOS (it->position);
19793 glyph->object = it->object;
19794 glyph->pixel_width = it->pixel_width;
19795 glyph->ascent = it->ascent;
19796 glyph->descent = it->descent;
19797 glyph->voffset = it->voffset;
19798 glyph->type = COMPOSITE_GLYPH;
19799 glyph->multibyte_p = it->multibyte_p;
19800 glyph->left_box_line_p = it->start_of_box_run_p;
19801 glyph->right_box_line_p = it->end_of_box_run_p;
19802 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19803 || it->phys_descent > it->descent);
19804 glyph->padding_p = 0;
19805 glyph->glyph_not_available_p = 0;
19806 glyph->face_id = it->face_id;
19807 glyph->u.cmp_id = it->cmp_id;
19808 glyph->slice = null_glyph_slice;
19809 glyph->font_type = FONT_TYPE_UNKNOWN;
19810 ++it->glyph_row->used[area];
19811 }
19812 else
19813 IT_EXPAND_MATRIX_WIDTH (it, area);
19814 }
19815
19816
19817 /* Change IT->ascent and IT->height according to the setting of
19818 IT->voffset. */
19819
19820 static INLINE void
19821 take_vertical_position_into_account (it)
19822 struct it *it;
19823 {
19824 if (it->voffset)
19825 {
19826 if (it->voffset < 0)
19827 /* Increase the ascent so that we can display the text higher
19828 in the line. */
19829 it->ascent -= it->voffset;
19830 else
19831 /* Increase the descent so that we can display the text lower
19832 in the line. */
19833 it->descent += it->voffset;
19834 }
19835 }
19836
19837
19838 /* Produce glyphs/get display metrics for the image IT is loaded with.
19839 See the description of struct display_iterator in dispextern.h for
19840 an overview of struct display_iterator. */
19841
19842 static void
19843 produce_image_glyph (it)
19844 struct it *it;
19845 {
19846 struct image *img;
19847 struct face *face;
19848 int glyph_ascent;
19849 struct glyph_slice slice;
19850
19851 xassert (it->what == IT_IMAGE);
19852
19853 face = FACE_FROM_ID (it->f, it->face_id);
19854 xassert (face);
19855 /* Make sure X resources of the face is loaded. */
19856 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19857
19858 if (it->image_id < 0)
19859 {
19860 /* Fringe bitmap. */
19861 it->ascent = it->phys_ascent = 0;
19862 it->descent = it->phys_descent = 0;
19863 it->pixel_width = 0;
19864 it->nglyphs = 0;
19865 return;
19866 }
19867
19868 img = IMAGE_FROM_ID (it->f, it->image_id);
19869 xassert (img);
19870 /* Make sure X resources of the image is loaded. */
19871 prepare_image_for_display (it->f, img);
19872
19873 slice.x = slice.y = 0;
19874 slice.width = img->width;
19875 slice.height = img->height;
19876
19877 if (INTEGERP (it->slice.x))
19878 slice.x = XINT (it->slice.x);
19879 else if (FLOATP (it->slice.x))
19880 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19881
19882 if (INTEGERP (it->slice.y))
19883 slice.y = XINT (it->slice.y);
19884 else if (FLOATP (it->slice.y))
19885 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19886
19887 if (INTEGERP (it->slice.width))
19888 slice.width = XINT (it->slice.width);
19889 else if (FLOATP (it->slice.width))
19890 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19891
19892 if (INTEGERP (it->slice.height))
19893 slice.height = XINT (it->slice.height);
19894 else if (FLOATP (it->slice.height))
19895 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19896
19897 if (slice.x >= img->width)
19898 slice.x = img->width;
19899 if (slice.y >= img->height)
19900 slice.y = img->height;
19901 if (slice.x + slice.width >= img->width)
19902 slice.width = img->width - slice.x;
19903 if (slice.y + slice.height > img->height)
19904 slice.height = img->height - slice.y;
19905
19906 if (slice.width == 0 || slice.height == 0)
19907 return;
19908
19909 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19910
19911 it->descent = slice.height - glyph_ascent;
19912 if (slice.y == 0)
19913 it->descent += img->vmargin;
19914 if (slice.y + slice.height == img->height)
19915 it->descent += img->vmargin;
19916 it->phys_descent = it->descent;
19917
19918 it->pixel_width = slice.width;
19919 if (slice.x == 0)
19920 it->pixel_width += img->hmargin;
19921 if (slice.x + slice.width == img->width)
19922 it->pixel_width += img->hmargin;
19923
19924 /* It's quite possible for images to have an ascent greater than
19925 their height, so don't get confused in that case. */
19926 if (it->descent < 0)
19927 it->descent = 0;
19928
19929 #if 0 /* this breaks image tiling */
19930 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19931 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19932 if (face_ascent > it->ascent)
19933 it->ascent = it->phys_ascent = face_ascent;
19934 #endif
19935
19936 it->nglyphs = 1;
19937
19938 if (face->box != FACE_NO_BOX)
19939 {
19940 if (face->box_line_width > 0)
19941 {
19942 if (slice.y == 0)
19943 it->ascent += face->box_line_width;
19944 if (slice.y + slice.height == img->height)
19945 it->descent += face->box_line_width;
19946 }
19947
19948 if (it->start_of_box_run_p && slice.x == 0)
19949 it->pixel_width += abs (face->box_line_width);
19950 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19951 it->pixel_width += abs (face->box_line_width);
19952 }
19953
19954 take_vertical_position_into_account (it);
19955
19956 if (it->glyph_row)
19957 {
19958 struct glyph *glyph;
19959 enum glyph_row_area area = it->area;
19960
19961 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19962 if (glyph < it->glyph_row->glyphs[area + 1])
19963 {
19964 glyph->charpos = CHARPOS (it->position);
19965 glyph->object = it->object;
19966 glyph->pixel_width = it->pixel_width;
19967 glyph->ascent = glyph_ascent;
19968 glyph->descent = it->descent;
19969 glyph->voffset = it->voffset;
19970 glyph->type = IMAGE_GLYPH;
19971 glyph->multibyte_p = it->multibyte_p;
19972 glyph->left_box_line_p = it->start_of_box_run_p;
19973 glyph->right_box_line_p = it->end_of_box_run_p;
19974 glyph->overlaps_vertically_p = 0;
19975 glyph->padding_p = 0;
19976 glyph->glyph_not_available_p = 0;
19977 glyph->face_id = it->face_id;
19978 glyph->u.img_id = img->id;
19979 glyph->slice = slice;
19980 glyph->font_type = FONT_TYPE_UNKNOWN;
19981 ++it->glyph_row->used[area];
19982 }
19983 else
19984 IT_EXPAND_MATRIX_WIDTH (it, area);
19985 }
19986 }
19987
19988
19989 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19990 of the glyph, WIDTH and HEIGHT are the width and height of the
19991 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19992
19993 static void
19994 append_stretch_glyph (it, object, width, height, ascent)
19995 struct it *it;
19996 Lisp_Object object;
19997 int width, height;
19998 int ascent;
19999 {
20000 struct glyph *glyph;
20001 enum glyph_row_area area = it->area;
20002
20003 xassert (ascent >= 0 && ascent <= height);
20004
20005 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20006 if (glyph < it->glyph_row->glyphs[area + 1])
20007 {
20008 glyph->charpos = CHARPOS (it->position);
20009 glyph->object = object;
20010 glyph->pixel_width = width;
20011 glyph->ascent = ascent;
20012 glyph->descent = height - ascent;
20013 glyph->voffset = it->voffset;
20014 glyph->type = STRETCH_GLYPH;
20015 glyph->multibyte_p = it->multibyte_p;
20016 glyph->left_box_line_p = it->start_of_box_run_p;
20017 glyph->right_box_line_p = it->end_of_box_run_p;
20018 glyph->overlaps_vertically_p = 0;
20019 glyph->padding_p = 0;
20020 glyph->glyph_not_available_p = 0;
20021 glyph->face_id = it->face_id;
20022 glyph->u.stretch.ascent = ascent;
20023 glyph->u.stretch.height = height;
20024 glyph->slice = null_glyph_slice;
20025 glyph->font_type = FONT_TYPE_UNKNOWN;
20026 ++it->glyph_row->used[area];
20027 }
20028 else
20029 IT_EXPAND_MATRIX_WIDTH (it, area);
20030 }
20031
20032
20033 /* Produce a stretch glyph for iterator IT. IT->object is the value
20034 of the glyph property displayed. The value must be a list
20035 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20036 being recognized:
20037
20038 1. `:width WIDTH' specifies that the space should be WIDTH *
20039 canonical char width wide. WIDTH may be an integer or floating
20040 point number.
20041
20042 2. `:relative-width FACTOR' specifies that the width of the stretch
20043 should be computed from the width of the first character having the
20044 `glyph' property, and should be FACTOR times that width.
20045
20046 3. `:align-to HPOS' specifies that the space should be wide enough
20047 to reach HPOS, a value in canonical character units.
20048
20049 Exactly one of the above pairs must be present.
20050
20051 4. `:height HEIGHT' specifies that the height of the stretch produced
20052 should be HEIGHT, measured in canonical character units.
20053
20054 5. `:relative-height FACTOR' specifies that the height of the
20055 stretch should be FACTOR times the height of the characters having
20056 the glyph property.
20057
20058 Either none or exactly one of 4 or 5 must be present.
20059
20060 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20061 of the stretch should be used for the ascent of the stretch.
20062 ASCENT must be in the range 0 <= ASCENT <= 100. */
20063
20064 static void
20065 produce_stretch_glyph (it)
20066 struct it *it;
20067 {
20068 /* (space :width WIDTH :height HEIGHT ...) */
20069 Lisp_Object prop, plist;
20070 int width = 0, height = 0, align_to = -1;
20071 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20072 int ascent = 0;
20073 double tem;
20074 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20075 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20076
20077 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20078
20079 /* List should start with `space'. */
20080 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20081 plist = XCDR (it->object);
20082
20083 /* Compute the width of the stretch. */
20084 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20085 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20086 {
20087 /* Absolute width `:width WIDTH' specified and valid. */
20088 zero_width_ok_p = 1;
20089 width = (int)tem;
20090 }
20091 else if (prop = Fplist_get (plist, QCrelative_width),
20092 NUMVAL (prop) > 0)
20093 {
20094 /* Relative width `:relative-width FACTOR' specified and valid.
20095 Compute the width of the characters having the `glyph'
20096 property. */
20097 struct it it2;
20098 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20099
20100 it2 = *it;
20101 if (it->multibyte_p)
20102 {
20103 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20104 - IT_BYTEPOS (*it));
20105 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20106 }
20107 else
20108 it2.c = *p, it2.len = 1;
20109
20110 it2.glyph_row = NULL;
20111 it2.what = IT_CHARACTER;
20112 x_produce_glyphs (&it2);
20113 width = NUMVAL (prop) * it2.pixel_width;
20114 }
20115 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20116 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20117 {
20118 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20119 align_to = (align_to < 0
20120 ? 0
20121 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20122 else if (align_to < 0)
20123 align_to = window_box_left_offset (it->w, TEXT_AREA);
20124 width = max (0, (int)tem + align_to - it->current_x);
20125 zero_width_ok_p = 1;
20126 }
20127 else
20128 /* Nothing specified -> width defaults to canonical char width. */
20129 width = FRAME_COLUMN_WIDTH (it->f);
20130
20131 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20132 width = 1;
20133
20134 /* Compute height. */
20135 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20136 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20137 {
20138 height = (int)tem;
20139 zero_height_ok_p = 1;
20140 }
20141 else if (prop = Fplist_get (plist, QCrelative_height),
20142 NUMVAL (prop) > 0)
20143 height = FONT_HEIGHT (font) * NUMVAL (prop);
20144 else
20145 height = FONT_HEIGHT (font);
20146
20147 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20148 height = 1;
20149
20150 /* Compute percentage of height used for ascent. If
20151 `:ascent ASCENT' is present and valid, use that. Otherwise,
20152 derive the ascent from the font in use. */
20153 if (prop = Fplist_get (plist, QCascent),
20154 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20155 ascent = height * NUMVAL (prop) / 100.0;
20156 else if (!NILP (prop)
20157 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20158 ascent = min (max (0, (int)tem), height);
20159 else
20160 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20161
20162 if (width > 0 && !it->truncate_lines_p
20163 && it->current_x + width > it->last_visible_x)
20164 width = it->last_visible_x - it->current_x - 1;
20165
20166 if (width > 0 && height > 0 && it->glyph_row)
20167 {
20168 Lisp_Object object = it->stack[it->sp - 1].string;
20169 if (!STRINGP (object))
20170 object = it->w->buffer;
20171 append_stretch_glyph (it, object, width, height, ascent);
20172 }
20173
20174 it->pixel_width = width;
20175 it->ascent = it->phys_ascent = ascent;
20176 it->descent = it->phys_descent = height - it->ascent;
20177 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20178
20179 take_vertical_position_into_account (it);
20180 }
20181
20182 /* Get line-height and line-spacing property at point.
20183 If line-height has format (HEIGHT TOTAL), return TOTAL
20184 in TOTAL_HEIGHT. */
20185
20186 static Lisp_Object
20187 get_line_height_property (it, prop)
20188 struct it *it;
20189 Lisp_Object prop;
20190 {
20191 Lisp_Object position;
20192
20193 if (STRINGP (it->object))
20194 position = make_number (IT_STRING_CHARPOS (*it));
20195 else if (BUFFERP (it->object))
20196 position = make_number (IT_CHARPOS (*it));
20197 else
20198 return Qnil;
20199
20200 return Fget_char_property (position, prop, it->object);
20201 }
20202
20203 /* Calculate line-height and line-spacing properties.
20204 An integer value specifies explicit pixel value.
20205 A float value specifies relative value to current face height.
20206 A cons (float . face-name) specifies relative value to
20207 height of specified face font.
20208
20209 Returns height in pixels, or nil. */
20210
20211
20212 static Lisp_Object
20213 calc_line_height_property (it, val, font, boff, override)
20214 struct it *it;
20215 Lisp_Object val;
20216 XFontStruct *font;
20217 int boff, override;
20218 {
20219 Lisp_Object face_name = Qnil;
20220 int ascent, descent, height;
20221
20222 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20223 return val;
20224
20225 if (CONSP (val))
20226 {
20227 face_name = XCAR (val);
20228 val = XCDR (val);
20229 if (!NUMBERP (val))
20230 val = make_number (1);
20231 if (NILP (face_name))
20232 {
20233 height = it->ascent + it->descent;
20234 goto scale;
20235 }
20236 }
20237
20238 if (NILP (face_name))
20239 {
20240 font = FRAME_FONT (it->f);
20241 boff = FRAME_BASELINE_OFFSET (it->f);
20242 }
20243 else if (EQ (face_name, Qt))
20244 {
20245 override = 0;
20246 }
20247 else
20248 {
20249 int face_id;
20250 struct face *face;
20251 struct font_info *font_info;
20252
20253 face_id = lookup_named_face (it->f, face_name, 0);
20254 if (face_id < 0)
20255 return make_number (-1);
20256
20257 face = FACE_FROM_ID (it->f, face_id);
20258 font = face->font;
20259 if (font == NULL)
20260 return make_number (-1);
20261
20262 font_info = FONT_INFO_FROM_FACE (it->f, face);
20263 boff = font_info->baseline_offset;
20264 if (font_info->vertical_centering)
20265 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20266 }
20267
20268 ascent = FONT_BASE (font) + boff;
20269 descent = FONT_DESCENT (font) - boff;
20270
20271 if (override)
20272 {
20273 it->override_ascent = ascent;
20274 it->override_descent = descent;
20275 it->override_boff = boff;
20276 }
20277
20278 height = ascent + descent;
20279
20280 scale:
20281 if (FLOATP (val))
20282 height = (int)(XFLOAT_DATA (val) * height);
20283 else if (INTEGERP (val))
20284 height *= XINT (val);
20285
20286 return make_number (height);
20287 }
20288
20289
20290 /* RIF:
20291 Produce glyphs/get display metrics for the display element IT is
20292 loaded with. See the description of struct it in dispextern.h
20293 for an overview of struct it. */
20294
20295 void
20296 x_produce_glyphs (it)
20297 struct it *it;
20298 {
20299 int extra_line_spacing = it->extra_line_spacing;
20300
20301 it->glyph_not_available_p = 0;
20302
20303 if (it->what == IT_CHARACTER)
20304 {
20305 XChar2b char2b;
20306 XFontStruct *font;
20307 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20308 XCharStruct *pcm;
20309 int font_not_found_p;
20310 struct font_info *font_info;
20311 int boff; /* baseline offset */
20312 /* We may change it->multibyte_p upon unibyte<->multibyte
20313 conversion. So, save the current value now and restore it
20314 later.
20315
20316 Note: It seems that we don't have to record multibyte_p in
20317 struct glyph because the character code itself tells if or
20318 not the character is multibyte. Thus, in the future, we must
20319 consider eliminating the field `multibyte_p' in the struct
20320 glyph. */
20321 int saved_multibyte_p = it->multibyte_p;
20322
20323 /* Maybe translate single-byte characters to multibyte, or the
20324 other way. */
20325 it->char_to_display = it->c;
20326 if (!ASCII_BYTE_P (it->c)
20327 && ! it->multibyte_p)
20328 {
20329 if (SINGLE_BYTE_CHAR_P (it->c)
20330 && unibyte_display_via_language_environment)
20331 it->char_to_display = unibyte_char_to_multibyte (it->c);
20332 if (! SINGLE_BYTE_CHAR_P (it->c))
20333 {
20334 it->multibyte_p = 1;
20335 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20336 -1, Qnil);
20337 face = FACE_FROM_ID (it->f, it->face_id);
20338 }
20339 }
20340
20341 /* Get font to use. Encode IT->char_to_display. */
20342 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20343 &char2b, it->multibyte_p, 0);
20344 font = face->font;
20345
20346 /* When no suitable font found, use the default font. */
20347 font_not_found_p = font == NULL;
20348 if (font_not_found_p)
20349 {
20350 font = FRAME_FONT (it->f);
20351 boff = FRAME_BASELINE_OFFSET (it->f);
20352 font_info = NULL;
20353 }
20354 else
20355 {
20356 font_info = FONT_INFO_FROM_FACE (it->f, face);
20357 boff = font_info->baseline_offset;
20358 if (font_info->vertical_centering)
20359 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20360 }
20361
20362 if (it->char_to_display >= ' '
20363 && (!it->multibyte_p || it->char_to_display < 128))
20364 {
20365 /* Either unibyte or ASCII. */
20366 int stretched_p;
20367
20368 it->nglyphs = 1;
20369
20370 pcm = get_per_char_metric (font, font_info, &char2b,
20371 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20372
20373 if (it->override_ascent >= 0)
20374 {
20375 it->ascent = it->override_ascent;
20376 it->descent = it->override_descent;
20377 boff = it->override_boff;
20378 }
20379 else
20380 {
20381 it->ascent = FONT_BASE (font) + boff;
20382 it->descent = FONT_DESCENT (font) - boff;
20383 }
20384
20385 if (pcm)
20386 {
20387 it->phys_ascent = pcm->ascent + boff;
20388 it->phys_descent = pcm->descent - boff;
20389 it->pixel_width = pcm->width;
20390 }
20391 else
20392 {
20393 it->glyph_not_available_p = 1;
20394 it->phys_ascent = it->ascent;
20395 it->phys_descent = it->descent;
20396 it->pixel_width = FONT_WIDTH (font);
20397 }
20398
20399 if (it->constrain_row_ascent_descent_p)
20400 {
20401 if (it->descent > it->max_descent)
20402 {
20403 it->ascent += it->descent - it->max_descent;
20404 it->descent = it->max_descent;
20405 }
20406 if (it->ascent > it->max_ascent)
20407 {
20408 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20409 it->ascent = it->max_ascent;
20410 }
20411 it->phys_ascent = min (it->phys_ascent, it->ascent);
20412 it->phys_descent = min (it->phys_descent, it->descent);
20413 extra_line_spacing = 0;
20414 }
20415
20416 /* If this is a space inside a region of text with
20417 `space-width' property, change its width. */
20418 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20419 if (stretched_p)
20420 it->pixel_width *= XFLOATINT (it->space_width);
20421
20422 /* If face has a box, add the box thickness to the character
20423 height. If character has a box line to the left and/or
20424 right, add the box line width to the character's width. */
20425 if (face->box != FACE_NO_BOX)
20426 {
20427 int thick = face->box_line_width;
20428
20429 if (thick > 0)
20430 {
20431 it->ascent += thick;
20432 it->descent += thick;
20433 }
20434 else
20435 thick = -thick;
20436
20437 if (it->start_of_box_run_p)
20438 it->pixel_width += thick;
20439 if (it->end_of_box_run_p)
20440 it->pixel_width += thick;
20441 }
20442
20443 /* If face has an overline, add the height of the overline
20444 (1 pixel) and a 1 pixel margin to the character height. */
20445 if (face->overline_p)
20446 it->ascent += 2;
20447
20448 if (it->constrain_row_ascent_descent_p)
20449 {
20450 if (it->ascent > it->max_ascent)
20451 it->ascent = it->max_ascent;
20452 if (it->descent > it->max_descent)
20453 it->descent = it->max_descent;
20454 }
20455
20456 take_vertical_position_into_account (it);
20457
20458 /* If we have to actually produce glyphs, do it. */
20459 if (it->glyph_row)
20460 {
20461 if (stretched_p)
20462 {
20463 /* Translate a space with a `space-width' property
20464 into a stretch glyph. */
20465 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20466 / FONT_HEIGHT (font));
20467 append_stretch_glyph (it, it->object, it->pixel_width,
20468 it->ascent + it->descent, ascent);
20469 }
20470 else
20471 append_glyph (it);
20472
20473 /* If characters with lbearing or rbearing are displayed
20474 in this line, record that fact in a flag of the
20475 glyph row. This is used to optimize X output code. */
20476 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20477 it->glyph_row->contains_overlapping_glyphs_p = 1;
20478 }
20479 }
20480 else if (it->char_to_display == '\n')
20481 {
20482 /* A newline has no width but we need the height of the line.
20483 But if previous part of the line set a height, don't
20484 increase that height */
20485
20486 Lisp_Object height;
20487 Lisp_Object total_height = Qnil;
20488
20489 it->override_ascent = -1;
20490 it->pixel_width = 0;
20491 it->nglyphs = 0;
20492
20493 height = get_line_height_property(it, Qline_height);
20494 /* Split (line-height total-height) list */
20495 if (CONSP (height)
20496 && CONSP (XCDR (height))
20497 && NILP (XCDR (XCDR (height))))
20498 {
20499 total_height = XCAR (XCDR (height));
20500 height = XCAR (height);
20501 }
20502 height = calc_line_height_property(it, height, font, boff, 1);
20503
20504 if (it->override_ascent >= 0)
20505 {
20506 it->ascent = it->override_ascent;
20507 it->descent = it->override_descent;
20508 boff = it->override_boff;
20509 }
20510 else
20511 {
20512 it->ascent = FONT_BASE (font) + boff;
20513 it->descent = FONT_DESCENT (font) - boff;
20514 }
20515
20516 if (EQ (height, Qt))
20517 {
20518 if (it->descent > it->max_descent)
20519 {
20520 it->ascent += it->descent - it->max_descent;
20521 it->descent = it->max_descent;
20522 }
20523 if (it->ascent > it->max_ascent)
20524 {
20525 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20526 it->ascent = it->max_ascent;
20527 }
20528 it->phys_ascent = min (it->phys_ascent, it->ascent);
20529 it->phys_descent = min (it->phys_descent, it->descent);
20530 it->constrain_row_ascent_descent_p = 1;
20531 extra_line_spacing = 0;
20532 }
20533 else
20534 {
20535 Lisp_Object spacing;
20536
20537 it->phys_ascent = it->ascent;
20538 it->phys_descent = it->descent;
20539
20540 if ((it->max_ascent > 0 || it->max_descent > 0)
20541 && face->box != FACE_NO_BOX
20542 && face->box_line_width > 0)
20543 {
20544 it->ascent += face->box_line_width;
20545 it->descent += face->box_line_width;
20546 }
20547 if (!NILP (height)
20548 && XINT (height) > it->ascent + it->descent)
20549 it->ascent = XINT (height) - it->descent;
20550
20551 if (!NILP (total_height))
20552 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20553 else
20554 {
20555 spacing = get_line_height_property(it, Qline_spacing);
20556 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20557 }
20558 if (INTEGERP (spacing))
20559 {
20560 extra_line_spacing = XINT (spacing);
20561 if (!NILP (total_height))
20562 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20563 }
20564 }
20565 }
20566 else if (it->char_to_display == '\t')
20567 {
20568 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20569 int x = it->current_x + it->continuation_lines_width;
20570 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20571
20572 /* If the distance from the current position to the next tab
20573 stop is less than a space character width, use the
20574 tab stop after that. */
20575 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20576 next_tab_x += tab_width;
20577
20578 it->pixel_width = next_tab_x - x;
20579 it->nglyphs = 1;
20580 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20581 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20582
20583 if (it->glyph_row)
20584 {
20585 append_stretch_glyph (it, it->object, it->pixel_width,
20586 it->ascent + it->descent, it->ascent);
20587 }
20588 }
20589 else
20590 {
20591 /* A multi-byte character. Assume that the display width of the
20592 character is the width of the character multiplied by the
20593 width of the font. */
20594
20595 /* If we found a font, this font should give us the right
20596 metrics. If we didn't find a font, use the frame's
20597 default font and calculate the width of the character by
20598 multiplying the width of font by the width of the
20599 character. */
20600
20601 pcm = get_per_char_metric (font, font_info, &char2b,
20602 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20603
20604 if (font_not_found_p || !pcm)
20605 {
20606 it->glyph_not_available_p = 1;
20607 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20608 * CHAR_WIDTH (it->char_to_display));
20609 it->phys_ascent = FONT_BASE (font) + boff;
20610 it->phys_descent = FONT_DESCENT (font) - boff;
20611 }
20612 else
20613 {
20614 it->pixel_width = pcm->width;
20615 it->phys_ascent = pcm->ascent + boff;
20616 it->phys_descent = pcm->descent - boff;
20617 if (it->glyph_row
20618 && (pcm->lbearing < 0
20619 || pcm->rbearing > pcm->width))
20620 it->glyph_row->contains_overlapping_glyphs_p = 1;
20621 }
20622 it->nglyphs = 1;
20623 it->ascent = FONT_BASE (font) + boff;
20624 it->descent = FONT_DESCENT (font) - boff;
20625 if (face->box != FACE_NO_BOX)
20626 {
20627 int thick = face->box_line_width;
20628
20629 if (thick > 0)
20630 {
20631 it->ascent += thick;
20632 it->descent += thick;
20633 }
20634 else
20635 thick = - thick;
20636
20637 if (it->start_of_box_run_p)
20638 it->pixel_width += thick;
20639 if (it->end_of_box_run_p)
20640 it->pixel_width += thick;
20641 }
20642
20643 /* If face has an overline, add the height of the overline
20644 (1 pixel) and a 1 pixel margin to the character height. */
20645 if (face->overline_p)
20646 it->ascent += 2;
20647
20648 take_vertical_position_into_account (it);
20649
20650 if (it->glyph_row)
20651 append_glyph (it);
20652 }
20653 it->multibyte_p = saved_multibyte_p;
20654 }
20655 else if (it->what == IT_COMPOSITION)
20656 {
20657 /* Note: A composition is represented as one glyph in the
20658 glyph matrix. There are no padding glyphs. */
20659 XChar2b char2b;
20660 XFontStruct *font;
20661 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20662 XCharStruct *pcm;
20663 int font_not_found_p;
20664 struct font_info *font_info;
20665 int boff; /* baseline offset */
20666 struct composition *cmp = composition_table[it->cmp_id];
20667 int pos;
20668
20669 /* Maybe translate single-byte characters to multibyte. */
20670 it->char_to_display = it->c;
20671 if (unibyte_display_via_language_environment
20672 && it->c >= 0200)
20673 {
20674 it->char_to_display = unibyte_char_to_multibyte (it->c);
20675 }
20676
20677 /* Get face and font to use. Encode IT->char_to_display. */
20678 pos = STRINGP (it->string) ? IT_STRING_CHARPOS (*it) : IT_CHARPOS (*it);
20679 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20680 pos, it->string);
20681 face = FACE_FROM_ID (it->f, it->face_id);
20682 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20683 &char2b, it->multibyte_p, 0);
20684 font = face->font;
20685
20686 /* When no suitable font found, use the default font. */
20687 font_not_found_p = font == NULL;
20688 if (font_not_found_p)
20689 {
20690 font = FRAME_FONT (it->f);
20691 boff = FRAME_BASELINE_OFFSET (it->f);
20692 font_info = NULL;
20693 }
20694 else
20695 {
20696 font_info = FONT_INFO_FROM_FACE (it->f, face);
20697 boff = font_info->baseline_offset;
20698 if (font_info->vertical_centering)
20699 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20700 }
20701
20702 /* There are no padding glyphs, so there is only one glyph to
20703 produce for the composition. Important is that pixel_width,
20704 ascent and descent are the values of what is drawn by
20705 draw_glyphs (i.e. the values of the overall glyphs composed). */
20706 it->nglyphs = 1;
20707
20708 /* If we have not yet calculated pixel size data of glyphs of
20709 the composition for the current face font, calculate them
20710 now. Theoretically, we have to check all fonts for the
20711 glyphs, but that requires much time and memory space. So,
20712 here we check only the font of the first glyph. This leads
20713 to incorrect display, but it's very rare, and C-l (recenter)
20714 can correct the display anyway. */
20715 if (cmp->glyph_len == 0)
20716 {
20717 cmp->lbearing = cmp->rbearing = 0;
20718 cmp->pixel_width = cmp->ascent = cmp->descent = 0;
20719 }
20720 #ifdef USE_FONT_BACKEND
20721 else if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
20722 {
20723 if (! cmp->font)
20724 font_prepare_composition (cmp);
20725 }
20726 #endif /* USE_FONT_BACKEND */
20727 else if (cmp->font != (void *) font)
20728 {
20729 /* Ascent and descent of the font of the first character of
20730 this composition (adjusted by baseline offset). Ascent
20731 and descent of overall glyphs should not be less than
20732 them respectively. */
20733 int font_ascent = FONT_BASE (font) + boff;
20734 int font_descent = FONT_DESCENT (font) - boff;
20735 int font_height = FONT_HEIGHT (font);
20736 /* Bounding box of the overall glyphs. */
20737 int leftmost, rightmost, lowest, highest;
20738 int lbearing, rbearing;
20739 int i, width, ascent, descent;
20740 int fully_padded = 0;
20741
20742 cmp->font = (void *) font;
20743
20744 /* Initialize the bounding box. */
20745 if (font_info
20746 && (pcm = get_per_char_metric (font, font_info, &char2b,
20747 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20748 {
20749 width = pcm->width;
20750 ascent = pcm->ascent;
20751 descent = pcm->descent;
20752 lbearing = pcm->lbearing;
20753 if (lbearing > 0)
20754 lbearing = 0;
20755 rbearing = pcm->rbearing;
20756 if (rbearing < width)
20757 rbearing = width;
20758 }
20759 else
20760 {
20761 width = FONT_WIDTH (font);
20762 ascent = FONT_BASE (font);
20763 descent = FONT_DESCENT (font);
20764 lbearing = 0;
20765 rbearing = width;
20766 }
20767
20768 rightmost = width;
20769 lowest = - descent + boff;
20770 highest = ascent + boff;
20771 leftmost = 0;
20772
20773 if (font_info
20774 && font_info->default_ascent
20775 && CHAR_TABLE_P (Vuse_default_ascent)
20776 && !NILP (Faref (Vuse_default_ascent,
20777 make_number (it->char_to_display))))
20778 highest = font_info->default_ascent + boff;
20779
20780 /* Draw the first glyph at the normal position. It may be
20781 shifted to right later if some other glyphs are drawn at
20782 the left. */
20783 cmp->offsets[0] = 0;
20784 cmp->offsets[1] = boff;
20785 cmp->lbearing = lbearing;
20786 cmp->rbearing = rbearing;
20787
20788 /* Set cmp->offsets for the remaining glyphs. */
20789 for (i = 1; i < cmp->glyph_len; i++)
20790 {
20791 int left, right, btm, top;
20792 int ch = COMPOSITION_GLYPH (cmp, i);
20793 int face_id;
20794
20795 if (ch == '\t')
20796 {
20797 fully_padded = 1;
20798 cmp->offsets[i * 2] = 0;
20799 cmp->offsets[i * 2 + 1] = boff;
20800 continue;
20801 }
20802
20803 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
20804 face = FACE_FROM_ID (it->f, face_id);
20805 get_char_face_and_encoding (it->f, ch, face->id,
20806 &char2b, it->multibyte_p, 0);
20807 font = face->font;
20808 if (font == NULL)
20809 {
20810 font = FRAME_FONT (it->f);
20811 boff = FRAME_BASELINE_OFFSET (it->f);
20812 font_info = NULL;
20813 }
20814 else
20815 {
20816 font_info
20817 = FONT_INFO_FROM_FACE (it->f, face);
20818 boff = font_info->baseline_offset;
20819 if (font_info->vertical_centering)
20820 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20821 }
20822
20823 if (font_info
20824 && (pcm = get_per_char_metric (font, font_info, &char2b,
20825 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20826 {
20827 width = pcm->width;
20828 ascent = pcm->ascent;
20829 descent = pcm->descent;
20830 lbearing = pcm->lbearing;
20831 if (lbearing > 0)
20832 lbearing = 0;
20833 rbearing = pcm->rbearing;
20834 if (rbearing < width)
20835 rbearing = width;
20836 }
20837 else
20838 {
20839 width = FONT_WIDTH (font);
20840 ascent = 1;
20841 descent = 0;
20842 lbearing = 0;
20843 rbearing = width;
20844 }
20845
20846 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20847 {
20848 /* Relative composition with or without
20849 alternate chars. */
20850 left = (leftmost + rightmost - width) / 2;
20851 btm = - descent + boff;
20852 if (font_info && font_info->relative_compose
20853 && (! CHAR_TABLE_P (Vignore_relative_composition)
20854 || NILP (Faref (Vignore_relative_composition,
20855 make_number (ch)))))
20856 {
20857
20858 if (- descent >= font_info->relative_compose)
20859 /* One extra pixel between two glyphs. */
20860 btm = highest + 1;
20861 else if (ascent <= 0)
20862 /* One extra pixel between two glyphs. */
20863 btm = lowest - 1 - ascent - descent;
20864 }
20865 }
20866 else
20867 {
20868 /* A composition rule is specified by an integer
20869 value that encodes global and new reference
20870 points (GREF and NREF). GREF and NREF are
20871 specified by numbers as below:
20872
20873 0---1---2 -- ascent
20874 | |
20875 | |
20876 | |
20877 9--10--11 -- center
20878 | |
20879 ---3---4---5--- baseline
20880 | |
20881 6---7---8 -- descent
20882 */
20883 int rule = COMPOSITION_RULE (cmp, i);
20884 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
20885
20886 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
20887 grefx = gref % 3, nrefx = nref % 3;
20888 grefy = gref / 3, nrefy = nref / 3;
20889 if (xoff)
20890 xoff = font_height * (xoff - 128) / 256;
20891 if (yoff)
20892 yoff = font_height * (yoff - 128) / 256;
20893
20894 left = (leftmost
20895 + grefx * (rightmost - leftmost) / 2
20896 - nrefx * width / 2
20897 + xoff);
20898
20899 btm = ((grefy == 0 ? highest
20900 : grefy == 1 ? 0
20901 : grefy == 2 ? lowest
20902 : (highest + lowest) / 2)
20903 - (nrefy == 0 ? ascent + descent
20904 : nrefy == 1 ? descent - boff
20905 : nrefy == 2 ? 0
20906 : (ascent + descent) / 2)
20907 + yoff);
20908 }
20909
20910 cmp->offsets[i * 2] = left;
20911 cmp->offsets[i * 2 + 1] = btm + descent;
20912
20913 /* Update the bounding box of the overall glyphs. */
20914 if (width > 0)
20915 {
20916 right = left + width;
20917 if (left < leftmost)
20918 leftmost = left;
20919 if (right > rightmost)
20920 rightmost = right;
20921 }
20922 top = btm + descent + ascent;
20923 if (top > highest)
20924 highest = top;
20925 if (btm < lowest)
20926 lowest = btm;
20927
20928 if (cmp->lbearing > left + lbearing)
20929 cmp->lbearing = left + lbearing;
20930 if (cmp->rbearing < left + rbearing)
20931 cmp->rbearing = left + rbearing;
20932 }
20933
20934 /* If there are glyphs whose x-offsets are negative,
20935 shift all glyphs to the right and make all x-offsets
20936 non-negative. */
20937 if (leftmost < 0)
20938 {
20939 for (i = 0; i < cmp->glyph_len; i++)
20940 cmp->offsets[i * 2] -= leftmost;
20941 rightmost -= leftmost;
20942 cmp->lbearing -= leftmost;
20943 cmp->rbearing -= leftmost;
20944 }
20945
20946 if (fully_padded)
20947 {
20948 for (i = 0; i < cmp->glyph_len; i++)
20949 cmp->offsets[i * 2] -= cmp->lbearing;
20950 rightmost = cmp->rbearing - cmp->lbearing;
20951 cmp->lbearing = 0;
20952 cmp->rbearing = rightmost;
20953 }
20954
20955 cmp->pixel_width = rightmost;
20956 cmp->ascent = highest;
20957 cmp->descent = - lowest;
20958 if (cmp->ascent < font_ascent)
20959 cmp->ascent = font_ascent;
20960 if (cmp->descent < font_descent)
20961 cmp->descent = font_descent;
20962 }
20963
20964 if (it->glyph_row
20965 && (cmp->lbearing < 0
20966 || cmp->rbearing > cmp->pixel_width))
20967 it->glyph_row->contains_overlapping_glyphs_p = 1;
20968
20969 it->pixel_width = cmp->pixel_width;
20970 it->ascent = it->phys_ascent = cmp->ascent;
20971 it->descent = it->phys_descent = cmp->descent;
20972
20973 if (face->box != FACE_NO_BOX)
20974 {
20975 int thick = face->box_line_width;
20976
20977 if (thick > 0)
20978 {
20979 it->ascent += thick;
20980 it->descent += thick;
20981 }
20982 else
20983 thick = - thick;
20984
20985 if (it->start_of_box_run_p)
20986 it->pixel_width += thick;
20987 if (it->end_of_box_run_p)
20988 it->pixel_width += thick;
20989 }
20990
20991 /* If face has an overline, add the height of the overline
20992 (1 pixel) and a 1 pixel margin to the character height. */
20993 if (face->overline_p)
20994 it->ascent += 2;
20995
20996 take_vertical_position_into_account (it);
20997
20998 if (it->glyph_row)
20999 append_composite_glyph (it);
21000 }
21001 else if (it->what == IT_IMAGE)
21002 produce_image_glyph (it);
21003 else if (it->what == IT_STRETCH)
21004 produce_stretch_glyph (it);
21005
21006 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21007 because this isn't true for images with `:ascent 100'. */
21008 xassert (it->ascent >= 0 && it->descent >= 0);
21009 if (it->area == TEXT_AREA)
21010 it->current_x += it->pixel_width;
21011
21012 if (extra_line_spacing > 0)
21013 {
21014 it->descent += extra_line_spacing;
21015 if (extra_line_spacing > it->max_extra_line_spacing)
21016 it->max_extra_line_spacing = extra_line_spacing;
21017 }
21018
21019 it->max_ascent = max (it->max_ascent, it->ascent);
21020 it->max_descent = max (it->max_descent, it->descent);
21021 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21022 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21023 }
21024
21025 /* EXPORT for RIF:
21026 Output LEN glyphs starting at START at the nominal cursor position.
21027 Advance the nominal cursor over the text. The global variable
21028 updated_window contains the window being updated, updated_row is
21029 the glyph row being updated, and updated_area is the area of that
21030 row being updated. */
21031
21032 void
21033 x_write_glyphs (start, len)
21034 struct glyph *start;
21035 int len;
21036 {
21037 int x, hpos;
21038
21039 xassert (updated_window && updated_row);
21040 BLOCK_INPUT;
21041
21042 /* Write glyphs. */
21043
21044 hpos = start - updated_row->glyphs[updated_area];
21045 x = draw_glyphs (updated_window, output_cursor.x,
21046 updated_row, updated_area,
21047 hpos, hpos + len,
21048 DRAW_NORMAL_TEXT, 0);
21049
21050 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21051 if (updated_area == TEXT_AREA
21052 && updated_window->phys_cursor_on_p
21053 && updated_window->phys_cursor.vpos == output_cursor.vpos
21054 && updated_window->phys_cursor.hpos >= hpos
21055 && updated_window->phys_cursor.hpos < hpos + len)
21056 updated_window->phys_cursor_on_p = 0;
21057
21058 UNBLOCK_INPUT;
21059
21060 /* Advance the output cursor. */
21061 output_cursor.hpos += len;
21062 output_cursor.x = x;
21063 }
21064
21065
21066 /* EXPORT for RIF:
21067 Insert LEN glyphs from START at the nominal cursor position. */
21068
21069 void
21070 x_insert_glyphs (start, len)
21071 struct glyph *start;
21072 int len;
21073 {
21074 struct frame *f;
21075 struct window *w;
21076 int line_height, shift_by_width, shifted_region_width;
21077 struct glyph_row *row;
21078 struct glyph *glyph;
21079 int frame_x, frame_y;
21080 EMACS_INT hpos;
21081
21082 xassert (updated_window && updated_row);
21083 BLOCK_INPUT;
21084 w = updated_window;
21085 f = XFRAME (WINDOW_FRAME (w));
21086
21087 /* Get the height of the line we are in. */
21088 row = updated_row;
21089 line_height = row->height;
21090
21091 /* Get the width of the glyphs to insert. */
21092 shift_by_width = 0;
21093 for (glyph = start; glyph < start + len; ++glyph)
21094 shift_by_width += glyph->pixel_width;
21095
21096 /* Get the width of the region to shift right. */
21097 shifted_region_width = (window_box_width (w, updated_area)
21098 - output_cursor.x
21099 - shift_by_width);
21100
21101 /* Shift right. */
21102 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21103 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21104
21105 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21106 line_height, shift_by_width);
21107
21108 /* Write the glyphs. */
21109 hpos = start - row->glyphs[updated_area];
21110 draw_glyphs (w, output_cursor.x, row, updated_area,
21111 hpos, hpos + len,
21112 DRAW_NORMAL_TEXT, 0);
21113
21114 /* Advance the output cursor. */
21115 output_cursor.hpos += len;
21116 output_cursor.x += shift_by_width;
21117 UNBLOCK_INPUT;
21118 }
21119
21120
21121 /* EXPORT for RIF:
21122 Erase the current text line from the nominal cursor position
21123 (inclusive) to pixel column TO_X (exclusive). The idea is that
21124 everything from TO_X onward is already erased.
21125
21126 TO_X is a pixel position relative to updated_area of
21127 updated_window. TO_X == -1 means clear to the end of this area. */
21128
21129 void
21130 x_clear_end_of_line (to_x)
21131 int to_x;
21132 {
21133 struct frame *f;
21134 struct window *w = updated_window;
21135 int max_x, min_y, max_y;
21136 int from_x, from_y, to_y;
21137
21138 xassert (updated_window && updated_row);
21139 f = XFRAME (w->frame);
21140
21141 if (updated_row->full_width_p)
21142 max_x = WINDOW_TOTAL_WIDTH (w);
21143 else
21144 max_x = window_box_width (w, updated_area);
21145 max_y = window_text_bottom_y (w);
21146
21147 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21148 of window. For TO_X > 0, truncate to end of drawing area. */
21149 if (to_x == 0)
21150 return;
21151 else if (to_x < 0)
21152 to_x = max_x;
21153 else
21154 to_x = min (to_x, max_x);
21155
21156 to_y = min (max_y, output_cursor.y + updated_row->height);
21157
21158 /* Notice if the cursor will be cleared by this operation. */
21159 if (!updated_row->full_width_p)
21160 notice_overwritten_cursor (w, updated_area,
21161 output_cursor.x, -1,
21162 updated_row->y,
21163 MATRIX_ROW_BOTTOM_Y (updated_row));
21164
21165 from_x = output_cursor.x;
21166
21167 /* Translate to frame coordinates. */
21168 if (updated_row->full_width_p)
21169 {
21170 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21171 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21172 }
21173 else
21174 {
21175 int area_left = window_box_left (w, updated_area);
21176 from_x += area_left;
21177 to_x += area_left;
21178 }
21179
21180 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21181 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21182 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21183
21184 /* Prevent inadvertently clearing to end of the X window. */
21185 if (to_x > from_x && to_y > from_y)
21186 {
21187 BLOCK_INPUT;
21188 rif->clear_frame_area (f, from_x, from_y,
21189 to_x - from_x, to_y - from_y);
21190 UNBLOCK_INPUT;
21191 }
21192 }
21193
21194 #endif /* HAVE_WINDOW_SYSTEM */
21195
21196
21197 \f
21198 /***********************************************************************
21199 Cursor types
21200 ***********************************************************************/
21201
21202 /* Value is the internal representation of the specified cursor type
21203 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21204 of the bar cursor. */
21205
21206 static enum text_cursor_kinds
21207 get_specified_cursor_type (arg, width)
21208 Lisp_Object arg;
21209 int *width;
21210 {
21211 enum text_cursor_kinds type;
21212
21213 if (NILP (arg))
21214 return NO_CURSOR;
21215
21216 if (EQ (arg, Qbox))
21217 return FILLED_BOX_CURSOR;
21218
21219 if (EQ (arg, Qhollow))
21220 return HOLLOW_BOX_CURSOR;
21221
21222 if (EQ (arg, Qbar))
21223 {
21224 *width = 2;
21225 return BAR_CURSOR;
21226 }
21227
21228 if (CONSP (arg)
21229 && EQ (XCAR (arg), Qbar)
21230 && INTEGERP (XCDR (arg))
21231 && XINT (XCDR (arg)) >= 0)
21232 {
21233 *width = XINT (XCDR (arg));
21234 return BAR_CURSOR;
21235 }
21236
21237 if (EQ (arg, Qhbar))
21238 {
21239 *width = 2;
21240 return HBAR_CURSOR;
21241 }
21242
21243 if (CONSP (arg)
21244 && EQ (XCAR (arg), Qhbar)
21245 && INTEGERP (XCDR (arg))
21246 && XINT (XCDR (arg)) >= 0)
21247 {
21248 *width = XINT (XCDR (arg));
21249 return HBAR_CURSOR;
21250 }
21251
21252 /* Treat anything unknown as "hollow box cursor".
21253 It was bad to signal an error; people have trouble fixing
21254 .Xdefaults with Emacs, when it has something bad in it. */
21255 type = HOLLOW_BOX_CURSOR;
21256
21257 return type;
21258 }
21259
21260 /* Set the default cursor types for specified frame. */
21261 void
21262 set_frame_cursor_types (f, arg)
21263 struct frame *f;
21264 Lisp_Object arg;
21265 {
21266 int width;
21267 Lisp_Object tem;
21268
21269 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21270 FRAME_CURSOR_WIDTH (f) = width;
21271
21272 /* By default, set up the blink-off state depending on the on-state. */
21273
21274 tem = Fassoc (arg, Vblink_cursor_alist);
21275 if (!NILP (tem))
21276 {
21277 FRAME_BLINK_OFF_CURSOR (f)
21278 = get_specified_cursor_type (XCDR (tem), &width);
21279 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21280 }
21281 else
21282 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21283 }
21284
21285
21286 /* Return the cursor we want to be displayed in window W. Return
21287 width of bar/hbar cursor through WIDTH arg. Return with
21288 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21289 (i.e. if the `system caret' should track this cursor).
21290
21291 In a mini-buffer window, we want the cursor only to appear if we
21292 are reading input from this window. For the selected window, we
21293 want the cursor type given by the frame parameter or buffer local
21294 setting of cursor-type. If explicitly marked off, draw no cursor.
21295 In all other cases, we want a hollow box cursor. */
21296
21297 static enum text_cursor_kinds
21298 get_window_cursor_type (w, glyph, width, active_cursor)
21299 struct window *w;
21300 struct glyph *glyph;
21301 int *width;
21302 int *active_cursor;
21303 {
21304 struct frame *f = XFRAME (w->frame);
21305 struct buffer *b = XBUFFER (w->buffer);
21306 int cursor_type = DEFAULT_CURSOR;
21307 Lisp_Object alt_cursor;
21308 int non_selected = 0;
21309
21310 *active_cursor = 1;
21311
21312 /* Echo area */
21313 if (cursor_in_echo_area
21314 && FRAME_HAS_MINIBUF_P (f)
21315 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21316 {
21317 if (w == XWINDOW (echo_area_window))
21318 {
21319 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21320 {
21321 *width = FRAME_CURSOR_WIDTH (f);
21322 return FRAME_DESIRED_CURSOR (f);
21323 }
21324 else
21325 return get_specified_cursor_type (b->cursor_type, width);
21326 }
21327
21328 *active_cursor = 0;
21329 non_selected = 1;
21330 }
21331
21332 /* Nonselected window or nonselected frame. */
21333 else if (w != XWINDOW (f->selected_window)
21334 #ifdef HAVE_WINDOW_SYSTEM
21335 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21336 #endif
21337 )
21338 {
21339 *active_cursor = 0;
21340
21341 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21342 return NO_CURSOR;
21343
21344 non_selected = 1;
21345 }
21346
21347 /* Never display a cursor in a window in which cursor-type is nil. */
21348 if (NILP (b->cursor_type))
21349 return NO_CURSOR;
21350
21351 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21352 if (non_selected)
21353 {
21354 alt_cursor = b->cursor_in_non_selected_windows;
21355 return get_specified_cursor_type (alt_cursor, width);
21356 }
21357
21358 /* Get the normal cursor type for this window. */
21359 if (EQ (b->cursor_type, Qt))
21360 {
21361 cursor_type = FRAME_DESIRED_CURSOR (f);
21362 *width = FRAME_CURSOR_WIDTH (f);
21363 }
21364 else
21365 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21366
21367 /* Use normal cursor if not blinked off. */
21368 if (!w->cursor_off_p)
21369 {
21370 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21371 if (cursor_type == FILLED_BOX_CURSOR)
21372 cursor_type = HOLLOW_BOX_CURSOR;
21373 }
21374 return cursor_type;
21375 }
21376
21377 /* Cursor is blinked off, so determine how to "toggle" it. */
21378
21379 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21380 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21381 return get_specified_cursor_type (XCDR (alt_cursor), width);
21382
21383 /* Then see if frame has specified a specific blink off cursor type. */
21384 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21385 {
21386 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21387 return FRAME_BLINK_OFF_CURSOR (f);
21388 }
21389
21390 #if 0
21391 /* Some people liked having a permanently visible blinking cursor,
21392 while others had very strong opinions against it. So it was
21393 decided to remove it. KFS 2003-09-03 */
21394
21395 /* Finally perform built-in cursor blinking:
21396 filled box <-> hollow box
21397 wide [h]bar <-> narrow [h]bar
21398 narrow [h]bar <-> no cursor
21399 other type <-> no cursor */
21400
21401 if (cursor_type == FILLED_BOX_CURSOR)
21402 return HOLLOW_BOX_CURSOR;
21403
21404 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21405 {
21406 *width = 1;
21407 return cursor_type;
21408 }
21409 #endif
21410
21411 return NO_CURSOR;
21412 }
21413
21414
21415 #ifdef HAVE_WINDOW_SYSTEM
21416
21417 /* Notice when the text cursor of window W has been completely
21418 overwritten by a drawing operation that outputs glyphs in AREA
21419 starting at X0 and ending at X1 in the line starting at Y0 and
21420 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21421 the rest of the line after X0 has been written. Y coordinates
21422 are window-relative. */
21423
21424 static void
21425 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21426 struct window *w;
21427 enum glyph_row_area area;
21428 int x0, y0, x1, y1;
21429 {
21430 int cx0, cx1, cy0, cy1;
21431 struct glyph_row *row;
21432
21433 if (!w->phys_cursor_on_p)
21434 return;
21435 if (area != TEXT_AREA)
21436 return;
21437
21438 if (w->phys_cursor.vpos < 0
21439 || w->phys_cursor.vpos >= w->current_matrix->nrows
21440 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21441 !(row->enabled_p && row->displays_text_p)))
21442 return;
21443
21444 if (row->cursor_in_fringe_p)
21445 {
21446 row->cursor_in_fringe_p = 0;
21447 draw_fringe_bitmap (w, row, 0);
21448 w->phys_cursor_on_p = 0;
21449 return;
21450 }
21451
21452 cx0 = w->phys_cursor.x;
21453 cx1 = cx0 + w->phys_cursor_width;
21454 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21455 return;
21456
21457 /* The cursor image will be completely removed from the
21458 screen if the output area intersects the cursor area in
21459 y-direction. When we draw in [y0 y1[, and some part of
21460 the cursor is at y < y0, that part must have been drawn
21461 before. When scrolling, the cursor is erased before
21462 actually scrolling, so we don't come here. When not
21463 scrolling, the rows above the old cursor row must have
21464 changed, and in this case these rows must have written
21465 over the cursor image.
21466
21467 Likewise if part of the cursor is below y1, with the
21468 exception of the cursor being in the first blank row at
21469 the buffer and window end because update_text_area
21470 doesn't draw that row. (Except when it does, but
21471 that's handled in update_text_area.) */
21472
21473 cy0 = w->phys_cursor.y;
21474 cy1 = cy0 + w->phys_cursor_height;
21475 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21476 return;
21477
21478 w->phys_cursor_on_p = 0;
21479 }
21480
21481 #endif /* HAVE_WINDOW_SYSTEM */
21482
21483 \f
21484 /************************************************************************
21485 Mouse Face
21486 ************************************************************************/
21487
21488 #ifdef HAVE_WINDOW_SYSTEM
21489
21490 /* EXPORT for RIF:
21491 Fix the display of area AREA of overlapping row ROW in window W
21492 with respect to the overlapping part OVERLAPS. */
21493
21494 void
21495 x_fix_overlapping_area (w, row, area, overlaps)
21496 struct window *w;
21497 struct glyph_row *row;
21498 enum glyph_row_area area;
21499 int overlaps;
21500 {
21501 int i, x;
21502
21503 BLOCK_INPUT;
21504
21505 x = 0;
21506 for (i = 0; i < row->used[area];)
21507 {
21508 if (row->glyphs[area][i].overlaps_vertically_p)
21509 {
21510 int start = i, start_x = x;
21511
21512 do
21513 {
21514 x += row->glyphs[area][i].pixel_width;
21515 ++i;
21516 }
21517 while (i < row->used[area]
21518 && row->glyphs[area][i].overlaps_vertically_p);
21519
21520 draw_glyphs (w, start_x, row, area,
21521 start, i,
21522 DRAW_NORMAL_TEXT, overlaps);
21523 }
21524 else
21525 {
21526 x += row->glyphs[area][i].pixel_width;
21527 ++i;
21528 }
21529 }
21530
21531 UNBLOCK_INPUT;
21532 }
21533
21534
21535 /* EXPORT:
21536 Draw the cursor glyph of window W in glyph row ROW. See the
21537 comment of draw_glyphs for the meaning of HL. */
21538
21539 void
21540 draw_phys_cursor_glyph (w, row, hl)
21541 struct window *w;
21542 struct glyph_row *row;
21543 enum draw_glyphs_face hl;
21544 {
21545 /* If cursor hpos is out of bounds, don't draw garbage. This can
21546 happen in mini-buffer windows when switching between echo area
21547 glyphs and mini-buffer. */
21548 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21549 {
21550 int on_p = w->phys_cursor_on_p;
21551 int x1;
21552 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21553 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21554 hl, 0);
21555 w->phys_cursor_on_p = on_p;
21556
21557 if (hl == DRAW_CURSOR)
21558 w->phys_cursor_width = x1 - w->phys_cursor.x;
21559 /* When we erase the cursor, and ROW is overlapped by other
21560 rows, make sure that these overlapping parts of other rows
21561 are redrawn. */
21562 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21563 {
21564 w->phys_cursor_width = x1 - w->phys_cursor.x;
21565
21566 if (row > w->current_matrix->rows
21567 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21568 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21569 OVERLAPS_ERASED_CURSOR);
21570
21571 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21572 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21573 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21574 OVERLAPS_ERASED_CURSOR);
21575 }
21576 }
21577 }
21578
21579
21580 /* EXPORT:
21581 Erase the image of a cursor of window W from the screen. */
21582
21583 void
21584 erase_phys_cursor (w)
21585 struct window *w;
21586 {
21587 struct frame *f = XFRAME (w->frame);
21588 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21589 int hpos = w->phys_cursor.hpos;
21590 int vpos = w->phys_cursor.vpos;
21591 int mouse_face_here_p = 0;
21592 struct glyph_matrix *active_glyphs = w->current_matrix;
21593 struct glyph_row *cursor_row;
21594 struct glyph *cursor_glyph;
21595 enum draw_glyphs_face hl;
21596
21597 /* No cursor displayed or row invalidated => nothing to do on the
21598 screen. */
21599 if (w->phys_cursor_type == NO_CURSOR)
21600 goto mark_cursor_off;
21601
21602 /* VPOS >= active_glyphs->nrows means that window has been resized.
21603 Don't bother to erase the cursor. */
21604 if (vpos >= active_glyphs->nrows)
21605 goto mark_cursor_off;
21606
21607 /* If row containing cursor is marked invalid, there is nothing we
21608 can do. */
21609 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21610 if (!cursor_row->enabled_p)
21611 goto mark_cursor_off;
21612
21613 /* If line spacing is > 0, old cursor may only be partially visible in
21614 window after split-window. So adjust visible height. */
21615 cursor_row->visible_height = min (cursor_row->visible_height,
21616 window_text_bottom_y (w) - cursor_row->y);
21617
21618 /* If row is completely invisible, don't attempt to delete a cursor which
21619 isn't there. This can happen if cursor is at top of a window, and
21620 we switch to a buffer with a header line in that window. */
21621 if (cursor_row->visible_height <= 0)
21622 goto mark_cursor_off;
21623
21624 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21625 if (cursor_row->cursor_in_fringe_p)
21626 {
21627 cursor_row->cursor_in_fringe_p = 0;
21628 draw_fringe_bitmap (w, cursor_row, 0);
21629 goto mark_cursor_off;
21630 }
21631
21632 /* This can happen when the new row is shorter than the old one.
21633 In this case, either draw_glyphs or clear_end_of_line
21634 should have cleared the cursor. Note that we wouldn't be
21635 able to erase the cursor in this case because we don't have a
21636 cursor glyph at hand. */
21637 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21638 goto mark_cursor_off;
21639
21640 /* If the cursor is in the mouse face area, redisplay that when
21641 we clear the cursor. */
21642 if (! NILP (dpyinfo->mouse_face_window)
21643 && w == XWINDOW (dpyinfo->mouse_face_window)
21644 && (vpos > dpyinfo->mouse_face_beg_row
21645 || (vpos == dpyinfo->mouse_face_beg_row
21646 && hpos >= dpyinfo->mouse_face_beg_col))
21647 && (vpos < dpyinfo->mouse_face_end_row
21648 || (vpos == dpyinfo->mouse_face_end_row
21649 && hpos < dpyinfo->mouse_face_end_col))
21650 /* Don't redraw the cursor's spot in mouse face if it is at the
21651 end of a line (on a newline). The cursor appears there, but
21652 mouse highlighting does not. */
21653 && cursor_row->used[TEXT_AREA] > hpos)
21654 mouse_face_here_p = 1;
21655
21656 /* Maybe clear the display under the cursor. */
21657 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21658 {
21659 int x, y, left_x;
21660 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21661 int width;
21662
21663 cursor_glyph = get_phys_cursor_glyph (w);
21664 if (cursor_glyph == NULL)
21665 goto mark_cursor_off;
21666
21667 width = cursor_glyph->pixel_width;
21668 left_x = window_box_left_offset (w, TEXT_AREA);
21669 x = w->phys_cursor.x;
21670 if (x < left_x)
21671 width -= left_x - x;
21672 width = min (width, window_box_width (w, TEXT_AREA) - x);
21673 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21674 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21675
21676 if (width > 0)
21677 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21678 }
21679
21680 /* Erase the cursor by redrawing the character underneath it. */
21681 if (mouse_face_here_p)
21682 hl = DRAW_MOUSE_FACE;
21683 else
21684 hl = DRAW_NORMAL_TEXT;
21685 draw_phys_cursor_glyph (w, cursor_row, hl);
21686
21687 mark_cursor_off:
21688 w->phys_cursor_on_p = 0;
21689 w->phys_cursor_type = NO_CURSOR;
21690 }
21691
21692
21693 /* EXPORT:
21694 Display or clear cursor of window W. If ON is zero, clear the
21695 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21696 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21697
21698 void
21699 display_and_set_cursor (w, on, hpos, vpos, x, y)
21700 struct window *w;
21701 int on, hpos, vpos, x, y;
21702 {
21703 struct frame *f = XFRAME (w->frame);
21704 int new_cursor_type;
21705 int new_cursor_width;
21706 int active_cursor;
21707 struct glyph_row *glyph_row;
21708 struct glyph *glyph;
21709
21710 /* This is pointless on invisible frames, and dangerous on garbaged
21711 windows and frames; in the latter case, the frame or window may
21712 be in the midst of changing its size, and x and y may be off the
21713 window. */
21714 if (! FRAME_VISIBLE_P (f)
21715 || FRAME_GARBAGED_P (f)
21716 || vpos >= w->current_matrix->nrows
21717 || hpos >= w->current_matrix->matrix_w)
21718 return;
21719
21720 /* If cursor is off and we want it off, return quickly. */
21721 if (!on && !w->phys_cursor_on_p)
21722 return;
21723
21724 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21725 /* If cursor row is not enabled, we don't really know where to
21726 display the cursor. */
21727 if (!glyph_row->enabled_p)
21728 {
21729 w->phys_cursor_on_p = 0;
21730 return;
21731 }
21732
21733 glyph = NULL;
21734 if (!glyph_row->exact_window_width_line_p
21735 || hpos < glyph_row->used[TEXT_AREA])
21736 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21737
21738 xassert (interrupt_input_blocked);
21739
21740 /* Set new_cursor_type to the cursor we want to be displayed. */
21741 new_cursor_type = get_window_cursor_type (w, glyph,
21742 &new_cursor_width, &active_cursor);
21743
21744 /* If cursor is currently being shown and we don't want it to be or
21745 it is in the wrong place, or the cursor type is not what we want,
21746 erase it. */
21747 if (w->phys_cursor_on_p
21748 && (!on
21749 || w->phys_cursor.x != x
21750 || w->phys_cursor.y != y
21751 || new_cursor_type != w->phys_cursor_type
21752 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21753 && new_cursor_width != w->phys_cursor_width)))
21754 erase_phys_cursor (w);
21755
21756 /* Don't check phys_cursor_on_p here because that flag is only set
21757 to zero in some cases where we know that the cursor has been
21758 completely erased, to avoid the extra work of erasing the cursor
21759 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21760 still not be visible, or it has only been partly erased. */
21761 if (on)
21762 {
21763 w->phys_cursor_ascent = glyph_row->ascent;
21764 w->phys_cursor_height = glyph_row->height;
21765
21766 /* Set phys_cursor_.* before x_draw_.* is called because some
21767 of them may need the information. */
21768 w->phys_cursor.x = x;
21769 w->phys_cursor.y = glyph_row->y;
21770 w->phys_cursor.hpos = hpos;
21771 w->phys_cursor.vpos = vpos;
21772 }
21773
21774 rif->draw_window_cursor (w, glyph_row, x, y,
21775 new_cursor_type, new_cursor_width,
21776 on, active_cursor);
21777 }
21778
21779
21780 /* Switch the display of W's cursor on or off, according to the value
21781 of ON. */
21782
21783 static void
21784 update_window_cursor (w, on)
21785 struct window *w;
21786 int on;
21787 {
21788 /* Don't update cursor in windows whose frame is in the process
21789 of being deleted. */
21790 if (w->current_matrix)
21791 {
21792 BLOCK_INPUT;
21793 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21794 w->phys_cursor.x, w->phys_cursor.y);
21795 UNBLOCK_INPUT;
21796 }
21797 }
21798
21799
21800 /* Call update_window_cursor with parameter ON_P on all leaf windows
21801 in the window tree rooted at W. */
21802
21803 static void
21804 update_cursor_in_window_tree (w, on_p)
21805 struct window *w;
21806 int on_p;
21807 {
21808 while (w)
21809 {
21810 if (!NILP (w->hchild))
21811 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21812 else if (!NILP (w->vchild))
21813 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21814 else
21815 update_window_cursor (w, on_p);
21816
21817 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21818 }
21819 }
21820
21821
21822 /* EXPORT:
21823 Display the cursor on window W, or clear it, according to ON_P.
21824 Don't change the cursor's position. */
21825
21826 void
21827 x_update_cursor (f, on_p)
21828 struct frame *f;
21829 int on_p;
21830 {
21831 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21832 }
21833
21834
21835 /* EXPORT:
21836 Clear the cursor of window W to background color, and mark the
21837 cursor as not shown. This is used when the text where the cursor
21838 is is about to be rewritten. */
21839
21840 void
21841 x_clear_cursor (w)
21842 struct window *w;
21843 {
21844 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21845 update_window_cursor (w, 0);
21846 }
21847
21848
21849 /* EXPORT:
21850 Display the active region described by mouse_face_* according to DRAW. */
21851
21852 void
21853 show_mouse_face (dpyinfo, draw)
21854 Display_Info *dpyinfo;
21855 enum draw_glyphs_face draw;
21856 {
21857 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21858 struct frame *f = XFRAME (WINDOW_FRAME (w));
21859
21860 if (/* If window is in the process of being destroyed, don't bother
21861 to do anything. */
21862 w->current_matrix != NULL
21863 /* Don't update mouse highlight if hidden */
21864 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21865 /* Recognize when we are called to operate on rows that don't exist
21866 anymore. This can happen when a window is split. */
21867 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21868 {
21869 int phys_cursor_on_p = w->phys_cursor_on_p;
21870 struct glyph_row *row, *first, *last;
21871
21872 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21873 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21874
21875 for (row = first; row <= last && row->enabled_p; ++row)
21876 {
21877 int start_hpos, end_hpos, start_x;
21878
21879 /* For all but the first row, the highlight starts at column 0. */
21880 if (row == first)
21881 {
21882 start_hpos = dpyinfo->mouse_face_beg_col;
21883 start_x = dpyinfo->mouse_face_beg_x;
21884 }
21885 else
21886 {
21887 start_hpos = 0;
21888 start_x = 0;
21889 }
21890
21891 if (row == last)
21892 end_hpos = dpyinfo->mouse_face_end_col;
21893 else
21894 {
21895 end_hpos = row->used[TEXT_AREA];
21896 if (draw == DRAW_NORMAL_TEXT)
21897 row->fill_line_p = 1; /* Clear to end of line */
21898 }
21899
21900 if (end_hpos > start_hpos)
21901 {
21902 draw_glyphs (w, start_x, row, TEXT_AREA,
21903 start_hpos, end_hpos,
21904 draw, 0);
21905
21906 row->mouse_face_p
21907 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21908 }
21909 }
21910
21911 /* When we've written over the cursor, arrange for it to
21912 be displayed again. */
21913 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21914 {
21915 BLOCK_INPUT;
21916 display_and_set_cursor (w, 1,
21917 w->phys_cursor.hpos, w->phys_cursor.vpos,
21918 w->phys_cursor.x, w->phys_cursor.y);
21919 UNBLOCK_INPUT;
21920 }
21921 }
21922
21923 /* Change the mouse cursor. */
21924 if (draw == DRAW_NORMAL_TEXT)
21925 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21926 else if (draw == DRAW_MOUSE_FACE)
21927 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21928 else
21929 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21930 }
21931
21932 /* EXPORT:
21933 Clear out the mouse-highlighted active region.
21934 Redraw it un-highlighted first. Value is non-zero if mouse
21935 face was actually drawn unhighlighted. */
21936
21937 int
21938 clear_mouse_face (dpyinfo)
21939 Display_Info *dpyinfo;
21940 {
21941 int cleared = 0;
21942
21943 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21944 {
21945 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21946 cleared = 1;
21947 }
21948
21949 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21950 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21951 dpyinfo->mouse_face_window = Qnil;
21952 dpyinfo->mouse_face_overlay = Qnil;
21953 return cleared;
21954 }
21955
21956
21957 /* EXPORT:
21958 Non-zero if physical cursor of window W is within mouse face. */
21959
21960 int
21961 cursor_in_mouse_face_p (w)
21962 struct window *w;
21963 {
21964 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21965 int in_mouse_face = 0;
21966
21967 if (WINDOWP (dpyinfo->mouse_face_window)
21968 && XWINDOW (dpyinfo->mouse_face_window) == w)
21969 {
21970 int hpos = w->phys_cursor.hpos;
21971 int vpos = w->phys_cursor.vpos;
21972
21973 if (vpos >= dpyinfo->mouse_face_beg_row
21974 && vpos <= dpyinfo->mouse_face_end_row
21975 && (vpos > dpyinfo->mouse_face_beg_row
21976 || hpos >= dpyinfo->mouse_face_beg_col)
21977 && (vpos < dpyinfo->mouse_face_end_row
21978 || hpos < dpyinfo->mouse_face_end_col
21979 || dpyinfo->mouse_face_past_end))
21980 in_mouse_face = 1;
21981 }
21982
21983 return in_mouse_face;
21984 }
21985
21986
21987
21988 \f
21989 /* Find the glyph matrix position of buffer position CHARPOS in window
21990 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21991 current glyphs must be up to date. If CHARPOS is above window
21992 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21993 of last line in W. In the row containing CHARPOS, stop before glyphs
21994 having STOP as object. */
21995
21996 #if 1 /* This is a version of fast_find_position that's more correct
21997 in the presence of hscrolling, for example. I didn't install
21998 it right away because the problem fixed is minor, it failed
21999 in 20.x as well, and I think it's too risky to install
22000 so near the release of 21.1. 2001-09-25 gerd. */
22001
22002 static int
22003 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22004 struct window *w;
22005 EMACS_INT charpos;
22006 int *hpos, *vpos, *x, *y;
22007 Lisp_Object stop;
22008 {
22009 struct glyph_row *row, *first;
22010 struct glyph *glyph, *end;
22011 int past_end = 0;
22012
22013 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22014 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22015 {
22016 *x = first->x;
22017 *y = first->y;
22018 *hpos = 0;
22019 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22020 return 1;
22021 }
22022
22023 row = row_containing_pos (w, charpos, first, NULL, 0);
22024 if (row == NULL)
22025 {
22026 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22027 past_end = 1;
22028 }
22029
22030 /* If whole rows or last part of a row came from a display overlay,
22031 row_containing_pos will skip over such rows because their end pos
22032 equals the start pos of the overlay or interval.
22033
22034 Move back if we have a STOP object and previous row's
22035 end glyph came from STOP. */
22036 if (!NILP (stop))
22037 {
22038 struct glyph_row *prev;
22039 while ((prev = row - 1, prev >= first)
22040 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22041 && prev->used[TEXT_AREA] > 0)
22042 {
22043 struct glyph *beg = prev->glyphs[TEXT_AREA];
22044 glyph = beg + prev->used[TEXT_AREA];
22045 while (--glyph >= beg
22046 && INTEGERP (glyph->object));
22047 if (glyph < beg
22048 || !EQ (stop, glyph->object))
22049 break;
22050 row = prev;
22051 }
22052 }
22053
22054 *x = row->x;
22055 *y = row->y;
22056 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22057
22058 glyph = row->glyphs[TEXT_AREA];
22059 end = glyph + row->used[TEXT_AREA];
22060
22061 /* Skip over glyphs not having an object at the start of the row.
22062 These are special glyphs like truncation marks on terminal
22063 frames. */
22064 if (row->displays_text_p)
22065 while (glyph < end
22066 && INTEGERP (glyph->object)
22067 && !EQ (stop, glyph->object)
22068 && glyph->charpos < 0)
22069 {
22070 *x += glyph->pixel_width;
22071 ++glyph;
22072 }
22073
22074 while (glyph < end
22075 && !INTEGERP (glyph->object)
22076 && !EQ (stop, glyph->object)
22077 && (!BUFFERP (glyph->object)
22078 || glyph->charpos < charpos))
22079 {
22080 *x += glyph->pixel_width;
22081 ++glyph;
22082 }
22083
22084 *hpos = glyph - row->glyphs[TEXT_AREA];
22085 return !past_end;
22086 }
22087
22088 #else /* not 1 */
22089
22090 static int
22091 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22092 struct window *w;
22093 EMACS_INT pos;
22094 int *hpos, *vpos, *x, *y;
22095 Lisp_Object stop;
22096 {
22097 int i;
22098 int lastcol;
22099 int maybe_next_line_p = 0;
22100 int line_start_position;
22101 int yb = window_text_bottom_y (w);
22102 struct glyph_row *row, *best_row;
22103 int row_vpos, best_row_vpos;
22104 int current_x;
22105
22106 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22107 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22108
22109 while (row->y < yb)
22110 {
22111 if (row->used[TEXT_AREA])
22112 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22113 else
22114 line_start_position = 0;
22115
22116 if (line_start_position > pos)
22117 break;
22118 /* If the position sought is the end of the buffer,
22119 don't include the blank lines at the bottom of the window. */
22120 else if (line_start_position == pos
22121 && pos == BUF_ZV (XBUFFER (w->buffer)))
22122 {
22123 maybe_next_line_p = 1;
22124 break;
22125 }
22126 else if (line_start_position > 0)
22127 {
22128 best_row = row;
22129 best_row_vpos = row_vpos;
22130 }
22131
22132 if (row->y + row->height >= yb)
22133 break;
22134
22135 ++row;
22136 ++row_vpos;
22137 }
22138
22139 /* Find the right column within BEST_ROW. */
22140 lastcol = 0;
22141 current_x = best_row->x;
22142 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22143 {
22144 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22145 int charpos = glyph->charpos;
22146
22147 if (BUFFERP (glyph->object))
22148 {
22149 if (charpos == pos)
22150 {
22151 *hpos = i;
22152 *vpos = best_row_vpos;
22153 *x = current_x;
22154 *y = best_row->y;
22155 return 1;
22156 }
22157 else if (charpos > pos)
22158 break;
22159 }
22160 else if (EQ (glyph->object, stop))
22161 break;
22162
22163 if (charpos > 0)
22164 lastcol = i;
22165 current_x += glyph->pixel_width;
22166 }
22167
22168 /* If we're looking for the end of the buffer,
22169 and we didn't find it in the line we scanned,
22170 use the start of the following line. */
22171 if (maybe_next_line_p)
22172 {
22173 ++best_row;
22174 ++best_row_vpos;
22175 lastcol = 0;
22176 current_x = best_row->x;
22177 }
22178
22179 *vpos = best_row_vpos;
22180 *hpos = lastcol + 1;
22181 *x = current_x;
22182 *y = best_row->y;
22183 return 0;
22184 }
22185
22186 #endif /* not 1 */
22187
22188
22189 /* Find the position of the glyph for position POS in OBJECT in
22190 window W's current matrix, and return in *X, *Y the pixel
22191 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22192
22193 RIGHT_P non-zero means return the position of the right edge of the
22194 glyph, RIGHT_P zero means return the left edge position.
22195
22196 If no glyph for POS exists in the matrix, return the position of
22197 the glyph with the next smaller position that is in the matrix, if
22198 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22199 exists in the matrix, return the position of the glyph with the
22200 next larger position in OBJECT.
22201
22202 Value is non-zero if a glyph was found. */
22203
22204 static int
22205 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22206 struct window *w;
22207 EMACS_INT pos;
22208 Lisp_Object object;
22209 int *hpos, *vpos, *x, *y;
22210 int right_p;
22211 {
22212 int yb = window_text_bottom_y (w);
22213 struct glyph_row *r;
22214 struct glyph *best_glyph = NULL;
22215 struct glyph_row *best_row = NULL;
22216 int best_x = 0;
22217
22218 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22219 r->enabled_p && r->y < yb;
22220 ++r)
22221 {
22222 struct glyph *g = r->glyphs[TEXT_AREA];
22223 struct glyph *e = g + r->used[TEXT_AREA];
22224 int gx;
22225
22226 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22227 if (EQ (g->object, object))
22228 {
22229 if (g->charpos == pos)
22230 {
22231 best_glyph = g;
22232 best_x = gx;
22233 best_row = r;
22234 goto found;
22235 }
22236 else if (best_glyph == NULL
22237 || ((abs (g->charpos - pos)
22238 < abs (best_glyph->charpos - pos))
22239 && (right_p
22240 ? g->charpos < pos
22241 : g->charpos > pos)))
22242 {
22243 best_glyph = g;
22244 best_x = gx;
22245 best_row = r;
22246 }
22247 }
22248 }
22249
22250 found:
22251
22252 if (best_glyph)
22253 {
22254 *x = best_x;
22255 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22256
22257 if (right_p)
22258 {
22259 *x += best_glyph->pixel_width;
22260 ++*hpos;
22261 }
22262
22263 *y = best_row->y;
22264 *vpos = best_row - w->current_matrix->rows;
22265 }
22266
22267 return best_glyph != NULL;
22268 }
22269
22270
22271 /* See if position X, Y is within a hot-spot of an image. */
22272
22273 static int
22274 on_hot_spot_p (hot_spot, x, y)
22275 Lisp_Object hot_spot;
22276 int x, y;
22277 {
22278 if (!CONSP (hot_spot))
22279 return 0;
22280
22281 if (EQ (XCAR (hot_spot), Qrect))
22282 {
22283 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22284 Lisp_Object rect = XCDR (hot_spot);
22285 Lisp_Object tem;
22286 if (!CONSP (rect))
22287 return 0;
22288 if (!CONSP (XCAR (rect)))
22289 return 0;
22290 if (!CONSP (XCDR (rect)))
22291 return 0;
22292 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22293 return 0;
22294 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22295 return 0;
22296 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22297 return 0;
22298 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22299 return 0;
22300 return 1;
22301 }
22302 else if (EQ (XCAR (hot_spot), Qcircle))
22303 {
22304 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22305 Lisp_Object circ = XCDR (hot_spot);
22306 Lisp_Object lr, lx0, ly0;
22307 if (CONSP (circ)
22308 && CONSP (XCAR (circ))
22309 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22310 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22311 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22312 {
22313 double r = XFLOATINT (lr);
22314 double dx = XINT (lx0) - x;
22315 double dy = XINT (ly0) - y;
22316 return (dx * dx + dy * dy <= r * r);
22317 }
22318 }
22319 else if (EQ (XCAR (hot_spot), Qpoly))
22320 {
22321 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22322 if (VECTORP (XCDR (hot_spot)))
22323 {
22324 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22325 Lisp_Object *poly = v->contents;
22326 int n = v->size;
22327 int i;
22328 int inside = 0;
22329 Lisp_Object lx, ly;
22330 int x0, y0;
22331
22332 /* Need an even number of coordinates, and at least 3 edges. */
22333 if (n < 6 || n & 1)
22334 return 0;
22335
22336 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22337 If count is odd, we are inside polygon. Pixels on edges
22338 may or may not be included depending on actual geometry of the
22339 polygon. */
22340 if ((lx = poly[n-2], !INTEGERP (lx))
22341 || (ly = poly[n-1], !INTEGERP (lx)))
22342 return 0;
22343 x0 = XINT (lx), y0 = XINT (ly);
22344 for (i = 0; i < n; i += 2)
22345 {
22346 int x1 = x0, y1 = y0;
22347 if ((lx = poly[i], !INTEGERP (lx))
22348 || (ly = poly[i+1], !INTEGERP (ly)))
22349 return 0;
22350 x0 = XINT (lx), y0 = XINT (ly);
22351
22352 /* Does this segment cross the X line? */
22353 if (x0 >= x)
22354 {
22355 if (x1 >= x)
22356 continue;
22357 }
22358 else if (x1 < x)
22359 continue;
22360 if (y > y0 && y > y1)
22361 continue;
22362 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22363 inside = !inside;
22364 }
22365 return inside;
22366 }
22367 }
22368 /* If we don't understand the format, pretend we're not in the hot-spot. */
22369 return 0;
22370 }
22371
22372 Lisp_Object
22373 find_hot_spot (map, x, y)
22374 Lisp_Object map;
22375 int x, y;
22376 {
22377 while (CONSP (map))
22378 {
22379 if (CONSP (XCAR (map))
22380 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22381 return XCAR (map);
22382 map = XCDR (map);
22383 }
22384
22385 return Qnil;
22386 }
22387
22388 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22389 3, 3, 0,
22390 doc: /* Lookup in image map MAP coordinates X and Y.
22391 An image map is an alist where each element has the format (AREA ID PLIST).
22392 An AREA is specified as either a rectangle, a circle, or a polygon:
22393 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22394 pixel coordinates of the upper left and bottom right corners.
22395 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22396 and the radius of the circle; r may be a float or integer.
22397 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22398 vector describes one corner in the polygon.
22399 Returns the alist element for the first matching AREA in MAP. */)
22400 (map, x, y)
22401 Lisp_Object map;
22402 Lisp_Object x, y;
22403 {
22404 if (NILP (map))
22405 return Qnil;
22406
22407 CHECK_NUMBER (x);
22408 CHECK_NUMBER (y);
22409
22410 return find_hot_spot (map, XINT (x), XINT (y));
22411 }
22412
22413
22414 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22415 static void
22416 define_frame_cursor1 (f, cursor, pointer)
22417 struct frame *f;
22418 Cursor cursor;
22419 Lisp_Object pointer;
22420 {
22421 /* Do not change cursor shape while dragging mouse. */
22422 if (!NILP (do_mouse_tracking))
22423 return;
22424
22425 if (!NILP (pointer))
22426 {
22427 if (EQ (pointer, Qarrow))
22428 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22429 else if (EQ (pointer, Qhand))
22430 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22431 else if (EQ (pointer, Qtext))
22432 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22433 else if (EQ (pointer, intern ("hdrag")))
22434 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22435 #ifdef HAVE_X_WINDOWS
22436 else if (EQ (pointer, intern ("vdrag")))
22437 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22438 #endif
22439 else if (EQ (pointer, intern ("hourglass")))
22440 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22441 else if (EQ (pointer, Qmodeline))
22442 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22443 else
22444 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22445 }
22446
22447 if (cursor != No_Cursor)
22448 rif->define_frame_cursor (f, cursor);
22449 }
22450
22451 /* Take proper action when mouse has moved to the mode or header line
22452 or marginal area AREA of window W, x-position X and y-position Y.
22453 X is relative to the start of the text display area of W, so the
22454 width of bitmap areas and scroll bars must be subtracted to get a
22455 position relative to the start of the mode line. */
22456
22457 static void
22458 note_mode_line_or_margin_highlight (window, x, y, area)
22459 Lisp_Object window;
22460 int x, y;
22461 enum window_part area;
22462 {
22463 struct window *w = XWINDOW (window);
22464 struct frame *f = XFRAME (w->frame);
22465 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22466 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22467 Lisp_Object pointer = Qnil;
22468 int charpos, dx, dy, width, height;
22469 Lisp_Object string, object = Qnil;
22470 Lisp_Object pos, help;
22471
22472 Lisp_Object mouse_face;
22473 int original_x_pixel = x;
22474 struct glyph * glyph = NULL;
22475 struct glyph_row *row;
22476
22477 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22478 {
22479 int x0;
22480 struct glyph *end;
22481
22482 string = mode_line_string (w, area, &x, &y, &charpos,
22483 &object, &dx, &dy, &width, &height);
22484
22485 row = (area == ON_MODE_LINE
22486 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22487 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22488
22489 /* Find glyph */
22490 if (row->mode_line_p && row->enabled_p)
22491 {
22492 glyph = row->glyphs[TEXT_AREA];
22493 end = glyph + row->used[TEXT_AREA];
22494
22495 for (x0 = original_x_pixel;
22496 glyph < end && x0 >= glyph->pixel_width;
22497 ++glyph)
22498 x0 -= glyph->pixel_width;
22499
22500 if (glyph >= end)
22501 glyph = NULL;
22502 }
22503 }
22504 else
22505 {
22506 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22507 string = marginal_area_string (w, area, &x, &y, &charpos,
22508 &object, &dx, &dy, &width, &height);
22509 }
22510
22511 help = Qnil;
22512
22513 if (IMAGEP (object))
22514 {
22515 Lisp_Object image_map, hotspot;
22516 if ((image_map = Fplist_get (XCDR (object), QCmap),
22517 !NILP (image_map))
22518 && (hotspot = find_hot_spot (image_map, dx, dy),
22519 CONSP (hotspot))
22520 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22521 {
22522 Lisp_Object area_id, plist;
22523
22524 area_id = XCAR (hotspot);
22525 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22526 If so, we could look for mouse-enter, mouse-leave
22527 properties in PLIST (and do something...). */
22528 hotspot = XCDR (hotspot);
22529 if (CONSP (hotspot)
22530 && (plist = XCAR (hotspot), CONSP (plist)))
22531 {
22532 pointer = Fplist_get (plist, Qpointer);
22533 if (NILP (pointer))
22534 pointer = Qhand;
22535 help = Fplist_get (plist, Qhelp_echo);
22536 if (!NILP (help))
22537 {
22538 help_echo_string = help;
22539 /* Is this correct? ++kfs */
22540 XSETWINDOW (help_echo_window, w);
22541 help_echo_object = w->buffer;
22542 help_echo_pos = charpos;
22543 }
22544 }
22545 }
22546 if (NILP (pointer))
22547 pointer = Fplist_get (XCDR (object), QCpointer);
22548 }
22549
22550 if (STRINGP (string))
22551 {
22552 pos = make_number (charpos);
22553 /* If we're on a string with `help-echo' text property, arrange
22554 for the help to be displayed. This is done by setting the
22555 global variable help_echo_string to the help string. */
22556 if (NILP (help))
22557 {
22558 help = Fget_text_property (pos, Qhelp_echo, string);
22559 if (!NILP (help))
22560 {
22561 help_echo_string = help;
22562 XSETWINDOW (help_echo_window, w);
22563 help_echo_object = string;
22564 help_echo_pos = charpos;
22565 }
22566 }
22567
22568 if (NILP (pointer))
22569 pointer = Fget_text_property (pos, Qpointer, string);
22570
22571 /* Change the mouse pointer according to what is under X/Y. */
22572 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22573 {
22574 Lisp_Object map;
22575 map = Fget_text_property (pos, Qlocal_map, string);
22576 if (!KEYMAPP (map))
22577 map = Fget_text_property (pos, Qkeymap, string);
22578 if (!KEYMAPP (map))
22579 cursor = dpyinfo->vertical_scroll_bar_cursor;
22580 }
22581
22582 /* Change the mouse face according to what is under X/Y. */
22583 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22584 if (!NILP (mouse_face)
22585 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22586 && glyph)
22587 {
22588 Lisp_Object b, e;
22589
22590 struct glyph * tmp_glyph;
22591
22592 int gpos;
22593 int gseq_length;
22594 int total_pixel_width;
22595 int ignore;
22596
22597 int vpos, hpos;
22598
22599 b = Fprevious_single_property_change (make_number (charpos + 1),
22600 Qmouse_face, string, Qnil);
22601 if (NILP (b))
22602 b = make_number (0);
22603
22604 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22605 if (NILP (e))
22606 e = make_number (SCHARS (string));
22607
22608 /* Calculate the position(glyph position: GPOS) of GLYPH in
22609 displayed string. GPOS is different from CHARPOS.
22610
22611 CHARPOS is the position of glyph in internal string
22612 object. A mode line string format has structures which
22613 is converted to a flatten by emacs lisp interpreter.
22614 The internal string is an element of the structures.
22615 The displayed string is the flatten string. */
22616 for (tmp_glyph = glyph - 1, gpos = 0;
22617 tmp_glyph->charpos >= XINT (b);
22618 tmp_glyph--, gpos++)
22619 {
22620 if (!EQ (tmp_glyph->object, glyph->object))
22621 break;
22622 }
22623
22624 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22625 displayed string holding GLYPH.
22626
22627 GSEQ_LENGTH is different from SCHARS (STRING).
22628 SCHARS (STRING) returns the length of the internal string. */
22629 for (tmp_glyph = glyph, gseq_length = gpos;
22630 tmp_glyph->charpos < XINT (e);
22631 tmp_glyph++, gseq_length++)
22632 {
22633 if (!EQ (tmp_glyph->object, glyph->object))
22634 break;
22635 }
22636
22637 total_pixel_width = 0;
22638 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22639 total_pixel_width += tmp_glyph->pixel_width;
22640
22641 /* Pre calculation of re-rendering position */
22642 vpos = (x - gpos);
22643 hpos = (area == ON_MODE_LINE
22644 ? (w->current_matrix)->nrows - 1
22645 : 0);
22646
22647 /* If the re-rendering position is included in the last
22648 re-rendering area, we should do nothing. */
22649 if ( EQ (window, dpyinfo->mouse_face_window)
22650 && dpyinfo->mouse_face_beg_col <= vpos
22651 && vpos < dpyinfo->mouse_face_end_col
22652 && dpyinfo->mouse_face_beg_row == hpos )
22653 return;
22654
22655 if (clear_mouse_face (dpyinfo))
22656 cursor = No_Cursor;
22657
22658 dpyinfo->mouse_face_beg_col = vpos;
22659 dpyinfo->mouse_face_beg_row = hpos;
22660
22661 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22662 dpyinfo->mouse_face_beg_y = 0;
22663
22664 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22665 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22666
22667 dpyinfo->mouse_face_end_x = 0;
22668 dpyinfo->mouse_face_end_y = 0;
22669
22670 dpyinfo->mouse_face_past_end = 0;
22671 dpyinfo->mouse_face_window = window;
22672
22673 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22674 charpos,
22675 0, 0, 0, &ignore,
22676 glyph->face_id, 1);
22677 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22678
22679 if (NILP (pointer))
22680 pointer = Qhand;
22681 }
22682 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22683 clear_mouse_face (dpyinfo);
22684 }
22685 define_frame_cursor1 (f, cursor, pointer);
22686 }
22687
22688
22689 /* EXPORT:
22690 Take proper action when the mouse has moved to position X, Y on
22691 frame F as regards highlighting characters that have mouse-face
22692 properties. Also de-highlighting chars where the mouse was before.
22693 X and Y can be negative or out of range. */
22694
22695 void
22696 note_mouse_highlight (f, x, y)
22697 struct frame *f;
22698 int x, y;
22699 {
22700 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22701 enum window_part part;
22702 Lisp_Object window;
22703 struct window *w;
22704 Cursor cursor = No_Cursor;
22705 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22706 struct buffer *b;
22707
22708 /* When a menu is active, don't highlight because this looks odd. */
22709 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22710 if (popup_activated ())
22711 return;
22712 #endif
22713
22714 if (NILP (Vmouse_highlight)
22715 || !f->glyphs_initialized_p)
22716 return;
22717
22718 dpyinfo->mouse_face_mouse_x = x;
22719 dpyinfo->mouse_face_mouse_y = y;
22720 dpyinfo->mouse_face_mouse_frame = f;
22721
22722 if (dpyinfo->mouse_face_defer)
22723 return;
22724
22725 if (gc_in_progress)
22726 {
22727 dpyinfo->mouse_face_deferred_gc = 1;
22728 return;
22729 }
22730
22731 /* Which window is that in? */
22732 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22733
22734 /* If we were displaying active text in another window, clear that.
22735 Also clear if we move out of text area in same window. */
22736 if (! EQ (window, dpyinfo->mouse_face_window)
22737 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22738 && !NILP (dpyinfo->mouse_face_window)))
22739 clear_mouse_face (dpyinfo);
22740
22741 /* Not on a window -> return. */
22742 if (!WINDOWP (window))
22743 return;
22744
22745 /* Reset help_echo_string. It will get recomputed below. */
22746 help_echo_string = Qnil;
22747
22748 /* Convert to window-relative pixel coordinates. */
22749 w = XWINDOW (window);
22750 frame_to_window_pixel_xy (w, &x, &y);
22751
22752 /* Handle tool-bar window differently since it doesn't display a
22753 buffer. */
22754 if (EQ (window, f->tool_bar_window))
22755 {
22756 note_tool_bar_highlight (f, x, y);
22757 return;
22758 }
22759
22760 /* Mouse is on the mode, header line or margin? */
22761 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22762 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22763 {
22764 note_mode_line_or_margin_highlight (window, x, y, part);
22765 return;
22766 }
22767
22768 if (part == ON_VERTICAL_BORDER)
22769 {
22770 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22771 help_echo_string = build_string ("drag-mouse-1: resize");
22772 }
22773 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22774 || part == ON_SCROLL_BAR)
22775 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22776 else
22777 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22778
22779 /* Are we in a window whose display is up to date?
22780 And verify the buffer's text has not changed. */
22781 b = XBUFFER (w->buffer);
22782 if (part == ON_TEXT
22783 && EQ (w->window_end_valid, w->buffer)
22784 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22785 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22786 {
22787 int hpos, vpos, pos, i, dx, dy, area;
22788 struct glyph *glyph;
22789 Lisp_Object object;
22790 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22791 Lisp_Object *overlay_vec = NULL;
22792 int noverlays;
22793 struct buffer *obuf;
22794 int obegv, ozv, same_region;
22795
22796 /* Find the glyph under X/Y. */
22797 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22798
22799 /* Look for :pointer property on image. */
22800 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22801 {
22802 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22803 if (img != NULL && IMAGEP (img->spec))
22804 {
22805 Lisp_Object image_map, hotspot;
22806 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22807 !NILP (image_map))
22808 && (hotspot = find_hot_spot (image_map,
22809 glyph->slice.x + dx,
22810 glyph->slice.y + dy),
22811 CONSP (hotspot))
22812 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22813 {
22814 Lisp_Object area_id, plist;
22815
22816 area_id = XCAR (hotspot);
22817 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22818 If so, we could look for mouse-enter, mouse-leave
22819 properties in PLIST (and do something...). */
22820 hotspot = XCDR (hotspot);
22821 if (CONSP (hotspot)
22822 && (plist = XCAR (hotspot), CONSP (plist)))
22823 {
22824 pointer = Fplist_get (plist, Qpointer);
22825 if (NILP (pointer))
22826 pointer = Qhand;
22827 help_echo_string = Fplist_get (plist, Qhelp_echo);
22828 if (!NILP (help_echo_string))
22829 {
22830 help_echo_window = window;
22831 help_echo_object = glyph->object;
22832 help_echo_pos = glyph->charpos;
22833 }
22834 }
22835 }
22836 if (NILP (pointer))
22837 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22838 }
22839 }
22840
22841 /* Clear mouse face if X/Y not over text. */
22842 if (glyph == NULL
22843 || area != TEXT_AREA
22844 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22845 {
22846 if (clear_mouse_face (dpyinfo))
22847 cursor = No_Cursor;
22848 if (NILP (pointer))
22849 {
22850 if (area != TEXT_AREA)
22851 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22852 else
22853 pointer = Vvoid_text_area_pointer;
22854 }
22855 goto set_cursor;
22856 }
22857
22858 pos = glyph->charpos;
22859 object = glyph->object;
22860 if (!STRINGP (object) && !BUFFERP (object))
22861 goto set_cursor;
22862
22863 /* If we get an out-of-range value, return now; avoid an error. */
22864 if (BUFFERP (object) && pos > BUF_Z (b))
22865 goto set_cursor;
22866
22867 /* Make the window's buffer temporarily current for
22868 overlays_at and compute_char_face. */
22869 obuf = current_buffer;
22870 current_buffer = b;
22871 obegv = BEGV;
22872 ozv = ZV;
22873 BEGV = BEG;
22874 ZV = Z;
22875
22876 /* Is this char mouse-active or does it have help-echo? */
22877 position = make_number (pos);
22878
22879 if (BUFFERP (object))
22880 {
22881 /* Put all the overlays we want in a vector in overlay_vec. */
22882 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22883 /* Sort overlays into increasing priority order. */
22884 noverlays = sort_overlays (overlay_vec, noverlays, w);
22885 }
22886 else
22887 noverlays = 0;
22888
22889 same_region = (EQ (window, dpyinfo->mouse_face_window)
22890 && vpos >= dpyinfo->mouse_face_beg_row
22891 && vpos <= dpyinfo->mouse_face_end_row
22892 && (vpos > dpyinfo->mouse_face_beg_row
22893 || hpos >= dpyinfo->mouse_face_beg_col)
22894 && (vpos < dpyinfo->mouse_face_end_row
22895 || hpos < dpyinfo->mouse_face_end_col
22896 || dpyinfo->mouse_face_past_end));
22897
22898 if (same_region)
22899 cursor = No_Cursor;
22900
22901 /* Check mouse-face highlighting. */
22902 if (! same_region
22903 /* If there exists an overlay with mouse-face overlapping
22904 the one we are currently highlighting, we have to
22905 check if we enter the overlapping overlay, and then
22906 highlight only that. */
22907 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22908 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22909 {
22910 /* Find the highest priority overlay that has a mouse-face
22911 property. */
22912 overlay = Qnil;
22913 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22914 {
22915 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22916 if (!NILP (mouse_face))
22917 overlay = overlay_vec[i];
22918 }
22919
22920 /* If we're actually highlighting the same overlay as
22921 before, there's no need to do that again. */
22922 if (!NILP (overlay)
22923 && EQ (overlay, dpyinfo->mouse_face_overlay))
22924 goto check_help_echo;
22925
22926 dpyinfo->mouse_face_overlay = overlay;
22927
22928 /* Clear the display of the old active region, if any. */
22929 if (clear_mouse_face (dpyinfo))
22930 cursor = No_Cursor;
22931
22932 /* If no overlay applies, get a text property. */
22933 if (NILP (overlay))
22934 mouse_face = Fget_text_property (position, Qmouse_face, object);
22935
22936 /* Handle the overlay case. */
22937 if (!NILP (overlay))
22938 {
22939 /* Find the range of text around this char that
22940 should be active. */
22941 Lisp_Object before, after;
22942 int ignore;
22943
22944 before = Foverlay_start (overlay);
22945 after = Foverlay_end (overlay);
22946 /* Record this as the current active region. */
22947 fast_find_position (w, XFASTINT (before),
22948 &dpyinfo->mouse_face_beg_col,
22949 &dpyinfo->mouse_face_beg_row,
22950 &dpyinfo->mouse_face_beg_x,
22951 &dpyinfo->mouse_face_beg_y, Qnil);
22952
22953 dpyinfo->mouse_face_past_end
22954 = !fast_find_position (w, XFASTINT (after),
22955 &dpyinfo->mouse_face_end_col,
22956 &dpyinfo->mouse_face_end_row,
22957 &dpyinfo->mouse_face_end_x,
22958 &dpyinfo->mouse_face_end_y, Qnil);
22959 dpyinfo->mouse_face_window = window;
22960
22961 dpyinfo->mouse_face_face_id
22962 = face_at_buffer_position (w, pos, 0, 0,
22963 &ignore, pos + 1,
22964 !dpyinfo->mouse_face_hidden);
22965
22966 /* Display it as active. */
22967 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22968 cursor = No_Cursor;
22969 }
22970 /* Handle the text property case. */
22971 else if (!NILP (mouse_face) && BUFFERP (object))
22972 {
22973 /* Find the range of text around this char that
22974 should be active. */
22975 Lisp_Object before, after, beginning, end;
22976 int ignore;
22977
22978 beginning = Fmarker_position (w->start);
22979 end = make_number (BUF_Z (XBUFFER (object))
22980 - XFASTINT (w->window_end_pos));
22981 before
22982 = Fprevious_single_property_change (make_number (pos + 1),
22983 Qmouse_face,
22984 object, beginning);
22985 after
22986 = Fnext_single_property_change (position, Qmouse_face,
22987 object, end);
22988
22989 /* Record this as the current active region. */
22990 fast_find_position (w, XFASTINT (before),
22991 &dpyinfo->mouse_face_beg_col,
22992 &dpyinfo->mouse_face_beg_row,
22993 &dpyinfo->mouse_face_beg_x,
22994 &dpyinfo->mouse_face_beg_y, Qnil);
22995 dpyinfo->mouse_face_past_end
22996 = !fast_find_position (w, XFASTINT (after),
22997 &dpyinfo->mouse_face_end_col,
22998 &dpyinfo->mouse_face_end_row,
22999 &dpyinfo->mouse_face_end_x,
23000 &dpyinfo->mouse_face_end_y, Qnil);
23001 dpyinfo->mouse_face_window = window;
23002
23003 if (BUFFERP (object))
23004 dpyinfo->mouse_face_face_id
23005 = face_at_buffer_position (w, pos, 0, 0,
23006 &ignore, pos + 1,
23007 !dpyinfo->mouse_face_hidden);
23008
23009 /* Display it as active. */
23010 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23011 cursor = No_Cursor;
23012 }
23013 else if (!NILP (mouse_face) && STRINGP (object))
23014 {
23015 Lisp_Object b, e;
23016 int ignore;
23017
23018 b = Fprevious_single_property_change (make_number (pos + 1),
23019 Qmouse_face,
23020 object, Qnil);
23021 e = Fnext_single_property_change (position, Qmouse_face,
23022 object, Qnil);
23023 if (NILP (b))
23024 b = make_number (0);
23025 if (NILP (e))
23026 e = make_number (SCHARS (object) - 1);
23027
23028 fast_find_string_pos (w, XINT (b), object,
23029 &dpyinfo->mouse_face_beg_col,
23030 &dpyinfo->mouse_face_beg_row,
23031 &dpyinfo->mouse_face_beg_x,
23032 &dpyinfo->mouse_face_beg_y, 0);
23033 fast_find_string_pos (w, XINT (e), object,
23034 &dpyinfo->mouse_face_end_col,
23035 &dpyinfo->mouse_face_end_row,
23036 &dpyinfo->mouse_face_end_x,
23037 &dpyinfo->mouse_face_end_y, 1);
23038 dpyinfo->mouse_face_past_end = 0;
23039 dpyinfo->mouse_face_window = window;
23040 dpyinfo->mouse_face_face_id
23041 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23042 glyph->face_id, 1);
23043 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23044 cursor = No_Cursor;
23045 }
23046 else if (STRINGP (object) && NILP (mouse_face))
23047 {
23048 /* A string which doesn't have mouse-face, but
23049 the text ``under'' it might have. */
23050 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23051 int start = MATRIX_ROW_START_CHARPOS (r);
23052
23053 pos = string_buffer_position (w, object, start);
23054 if (pos > 0)
23055 mouse_face = get_char_property_and_overlay (make_number (pos),
23056 Qmouse_face,
23057 w->buffer,
23058 &overlay);
23059 if (!NILP (mouse_face) && !NILP (overlay))
23060 {
23061 Lisp_Object before = Foverlay_start (overlay);
23062 Lisp_Object after = Foverlay_end (overlay);
23063 int ignore;
23064
23065 /* Note that we might not be able to find position
23066 BEFORE in the glyph matrix if the overlay is
23067 entirely covered by a `display' property. In
23068 this case, we overshoot. So let's stop in
23069 the glyph matrix before glyphs for OBJECT. */
23070 fast_find_position (w, XFASTINT (before),
23071 &dpyinfo->mouse_face_beg_col,
23072 &dpyinfo->mouse_face_beg_row,
23073 &dpyinfo->mouse_face_beg_x,
23074 &dpyinfo->mouse_face_beg_y,
23075 object);
23076
23077 dpyinfo->mouse_face_past_end
23078 = !fast_find_position (w, XFASTINT (after),
23079 &dpyinfo->mouse_face_end_col,
23080 &dpyinfo->mouse_face_end_row,
23081 &dpyinfo->mouse_face_end_x,
23082 &dpyinfo->mouse_face_end_y,
23083 Qnil);
23084 dpyinfo->mouse_face_window = window;
23085 dpyinfo->mouse_face_face_id
23086 = face_at_buffer_position (w, pos, 0, 0,
23087 &ignore, pos + 1,
23088 !dpyinfo->mouse_face_hidden);
23089
23090 /* Display it as active. */
23091 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23092 cursor = No_Cursor;
23093 }
23094 }
23095 }
23096
23097 check_help_echo:
23098
23099 /* Look for a `help-echo' property. */
23100 if (NILP (help_echo_string)) {
23101 Lisp_Object help, overlay;
23102
23103 /* Check overlays first. */
23104 help = overlay = Qnil;
23105 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23106 {
23107 overlay = overlay_vec[i];
23108 help = Foverlay_get (overlay, Qhelp_echo);
23109 }
23110
23111 if (!NILP (help))
23112 {
23113 help_echo_string = help;
23114 help_echo_window = window;
23115 help_echo_object = overlay;
23116 help_echo_pos = pos;
23117 }
23118 else
23119 {
23120 Lisp_Object object = glyph->object;
23121 int charpos = glyph->charpos;
23122
23123 /* Try text properties. */
23124 if (STRINGP (object)
23125 && charpos >= 0
23126 && charpos < SCHARS (object))
23127 {
23128 help = Fget_text_property (make_number (charpos),
23129 Qhelp_echo, object);
23130 if (NILP (help))
23131 {
23132 /* If the string itself doesn't specify a help-echo,
23133 see if the buffer text ``under'' it does. */
23134 struct glyph_row *r
23135 = MATRIX_ROW (w->current_matrix, vpos);
23136 int start = MATRIX_ROW_START_CHARPOS (r);
23137 int pos = string_buffer_position (w, object, start);
23138 if (pos > 0)
23139 {
23140 help = Fget_char_property (make_number (pos),
23141 Qhelp_echo, w->buffer);
23142 if (!NILP (help))
23143 {
23144 charpos = pos;
23145 object = w->buffer;
23146 }
23147 }
23148 }
23149 }
23150 else if (BUFFERP (object)
23151 && charpos >= BEGV
23152 && charpos < ZV)
23153 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23154 object);
23155
23156 if (!NILP (help))
23157 {
23158 help_echo_string = help;
23159 help_echo_window = window;
23160 help_echo_object = object;
23161 help_echo_pos = charpos;
23162 }
23163 }
23164 }
23165
23166 /* Look for a `pointer' property. */
23167 if (NILP (pointer))
23168 {
23169 /* Check overlays first. */
23170 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23171 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23172
23173 if (NILP (pointer))
23174 {
23175 Lisp_Object object = glyph->object;
23176 int charpos = glyph->charpos;
23177
23178 /* Try text properties. */
23179 if (STRINGP (object)
23180 && charpos >= 0
23181 && charpos < SCHARS (object))
23182 {
23183 pointer = Fget_text_property (make_number (charpos),
23184 Qpointer, object);
23185 if (NILP (pointer))
23186 {
23187 /* If the string itself doesn't specify a pointer,
23188 see if the buffer text ``under'' it does. */
23189 struct glyph_row *r
23190 = MATRIX_ROW (w->current_matrix, vpos);
23191 int start = MATRIX_ROW_START_CHARPOS (r);
23192 int pos = string_buffer_position (w, object, start);
23193 if (pos > 0)
23194 pointer = Fget_char_property (make_number (pos),
23195 Qpointer, w->buffer);
23196 }
23197 }
23198 else if (BUFFERP (object)
23199 && charpos >= BEGV
23200 && charpos < ZV)
23201 pointer = Fget_text_property (make_number (charpos),
23202 Qpointer, object);
23203 }
23204 }
23205
23206 BEGV = obegv;
23207 ZV = ozv;
23208 current_buffer = obuf;
23209 }
23210
23211 set_cursor:
23212
23213 define_frame_cursor1 (f, cursor, pointer);
23214 }
23215
23216
23217 /* EXPORT for RIF:
23218 Clear any mouse-face on window W. This function is part of the
23219 redisplay interface, and is called from try_window_id and similar
23220 functions to ensure the mouse-highlight is off. */
23221
23222 void
23223 x_clear_window_mouse_face (w)
23224 struct window *w;
23225 {
23226 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23227 Lisp_Object window;
23228
23229 BLOCK_INPUT;
23230 XSETWINDOW (window, w);
23231 if (EQ (window, dpyinfo->mouse_face_window))
23232 clear_mouse_face (dpyinfo);
23233 UNBLOCK_INPUT;
23234 }
23235
23236
23237 /* EXPORT:
23238 Just discard the mouse face information for frame F, if any.
23239 This is used when the size of F is changed. */
23240
23241 void
23242 cancel_mouse_face (f)
23243 struct frame *f;
23244 {
23245 Lisp_Object window;
23246 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23247
23248 window = dpyinfo->mouse_face_window;
23249 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23250 {
23251 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23252 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23253 dpyinfo->mouse_face_window = Qnil;
23254 }
23255 }
23256
23257
23258 #endif /* HAVE_WINDOW_SYSTEM */
23259
23260 \f
23261 /***********************************************************************
23262 Exposure Events
23263 ***********************************************************************/
23264
23265 #ifdef HAVE_WINDOW_SYSTEM
23266
23267 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23268 which intersects rectangle R. R is in window-relative coordinates. */
23269
23270 static void
23271 expose_area (w, row, r, area)
23272 struct window *w;
23273 struct glyph_row *row;
23274 XRectangle *r;
23275 enum glyph_row_area area;
23276 {
23277 struct glyph *first = row->glyphs[area];
23278 struct glyph *end = row->glyphs[area] + row->used[area];
23279 struct glyph *last;
23280 int first_x, start_x, x;
23281
23282 if (area == TEXT_AREA && row->fill_line_p)
23283 /* If row extends face to end of line write the whole line. */
23284 draw_glyphs (w, 0, row, area,
23285 0, row->used[area],
23286 DRAW_NORMAL_TEXT, 0);
23287 else
23288 {
23289 /* Set START_X to the window-relative start position for drawing glyphs of
23290 AREA. The first glyph of the text area can be partially visible.
23291 The first glyphs of other areas cannot. */
23292 start_x = window_box_left_offset (w, area);
23293 x = start_x;
23294 if (area == TEXT_AREA)
23295 x += row->x;
23296
23297 /* Find the first glyph that must be redrawn. */
23298 while (first < end
23299 && x + first->pixel_width < r->x)
23300 {
23301 x += first->pixel_width;
23302 ++first;
23303 }
23304
23305 /* Find the last one. */
23306 last = first;
23307 first_x = x;
23308 while (last < end
23309 && x < r->x + r->width)
23310 {
23311 x += last->pixel_width;
23312 ++last;
23313 }
23314
23315 /* Repaint. */
23316 if (last > first)
23317 draw_glyphs (w, first_x - start_x, row, area,
23318 first - row->glyphs[area], last - row->glyphs[area],
23319 DRAW_NORMAL_TEXT, 0);
23320 }
23321 }
23322
23323
23324 /* Redraw the parts of the glyph row ROW on window W intersecting
23325 rectangle R. R is in window-relative coordinates. Value is
23326 non-zero if mouse-face was overwritten. */
23327
23328 static int
23329 expose_line (w, row, r)
23330 struct window *w;
23331 struct glyph_row *row;
23332 XRectangle *r;
23333 {
23334 xassert (row->enabled_p);
23335
23336 if (row->mode_line_p || w->pseudo_window_p)
23337 draw_glyphs (w, 0, row, TEXT_AREA,
23338 0, row->used[TEXT_AREA],
23339 DRAW_NORMAL_TEXT, 0);
23340 else
23341 {
23342 if (row->used[LEFT_MARGIN_AREA])
23343 expose_area (w, row, r, LEFT_MARGIN_AREA);
23344 if (row->used[TEXT_AREA])
23345 expose_area (w, row, r, TEXT_AREA);
23346 if (row->used[RIGHT_MARGIN_AREA])
23347 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23348 draw_row_fringe_bitmaps (w, row);
23349 }
23350
23351 return row->mouse_face_p;
23352 }
23353
23354
23355 /* Redraw those parts of glyphs rows during expose event handling that
23356 overlap other rows. Redrawing of an exposed line writes over parts
23357 of lines overlapping that exposed line; this function fixes that.
23358
23359 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23360 row in W's current matrix that is exposed and overlaps other rows.
23361 LAST_OVERLAPPING_ROW is the last such row. */
23362
23363 static void
23364 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23365 struct window *w;
23366 struct glyph_row *first_overlapping_row;
23367 struct glyph_row *last_overlapping_row;
23368 {
23369 struct glyph_row *row;
23370
23371 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23372 if (row->overlapping_p)
23373 {
23374 xassert (row->enabled_p && !row->mode_line_p);
23375
23376 if (row->used[LEFT_MARGIN_AREA])
23377 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23378
23379 if (row->used[TEXT_AREA])
23380 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23381
23382 if (row->used[RIGHT_MARGIN_AREA])
23383 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23384 }
23385 }
23386
23387
23388 /* Return non-zero if W's cursor intersects rectangle R. */
23389
23390 static int
23391 phys_cursor_in_rect_p (w, r)
23392 struct window *w;
23393 XRectangle *r;
23394 {
23395 XRectangle cr, result;
23396 struct glyph *cursor_glyph;
23397
23398 cursor_glyph = get_phys_cursor_glyph (w);
23399 if (cursor_glyph)
23400 {
23401 /* r is relative to W's box, but w->phys_cursor.x is relative
23402 to left edge of W's TEXT area. Adjust it. */
23403 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23404 cr.y = w->phys_cursor.y;
23405 cr.width = cursor_glyph->pixel_width;
23406 cr.height = w->phys_cursor_height;
23407 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23408 I assume the effect is the same -- and this is portable. */
23409 return x_intersect_rectangles (&cr, r, &result);
23410 }
23411 else
23412 return 0;
23413 }
23414
23415
23416 /* EXPORT:
23417 Draw a vertical window border to the right of window W if W doesn't
23418 have vertical scroll bars. */
23419
23420 void
23421 x_draw_vertical_border (w)
23422 struct window *w;
23423 {
23424 /* We could do better, if we knew what type of scroll-bar the adjacent
23425 windows (on either side) have... But we don't :-(
23426 However, I think this works ok. ++KFS 2003-04-25 */
23427
23428 /* Redraw borders between horizontally adjacent windows. Don't
23429 do it for frames with vertical scroll bars because either the
23430 right scroll bar of a window, or the left scroll bar of its
23431 neighbor will suffice as a border. */
23432 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23433 return;
23434
23435 if (!WINDOW_RIGHTMOST_P (w)
23436 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23437 {
23438 int x0, x1, y0, y1;
23439
23440 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23441 y1 -= 1;
23442
23443 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23444 x1 -= 1;
23445
23446 rif->draw_vertical_window_border (w, x1, y0, y1);
23447 }
23448 else if (!WINDOW_LEFTMOST_P (w)
23449 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23450 {
23451 int x0, x1, y0, y1;
23452
23453 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23454 y1 -= 1;
23455
23456 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23457 x0 -= 1;
23458
23459 rif->draw_vertical_window_border (w, x0, y0, y1);
23460 }
23461 }
23462
23463
23464 /* Redraw the part of window W intersection rectangle FR. Pixel
23465 coordinates in FR are frame-relative. Call this function with
23466 input blocked. Value is non-zero if the exposure overwrites
23467 mouse-face. */
23468
23469 static int
23470 expose_window (w, fr)
23471 struct window *w;
23472 XRectangle *fr;
23473 {
23474 struct frame *f = XFRAME (w->frame);
23475 XRectangle wr, r;
23476 int mouse_face_overwritten_p = 0;
23477
23478 /* If window is not yet fully initialized, do nothing. This can
23479 happen when toolkit scroll bars are used and a window is split.
23480 Reconfiguring the scroll bar will generate an expose for a newly
23481 created window. */
23482 if (w->current_matrix == NULL)
23483 return 0;
23484
23485 /* When we're currently updating the window, display and current
23486 matrix usually don't agree. Arrange for a thorough display
23487 later. */
23488 if (w == updated_window)
23489 {
23490 SET_FRAME_GARBAGED (f);
23491 return 0;
23492 }
23493
23494 /* Frame-relative pixel rectangle of W. */
23495 wr.x = WINDOW_LEFT_EDGE_X (w);
23496 wr.y = WINDOW_TOP_EDGE_Y (w);
23497 wr.width = WINDOW_TOTAL_WIDTH (w);
23498 wr.height = WINDOW_TOTAL_HEIGHT (w);
23499
23500 if (x_intersect_rectangles (fr, &wr, &r))
23501 {
23502 int yb = window_text_bottom_y (w);
23503 struct glyph_row *row;
23504 int cursor_cleared_p;
23505 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23506
23507 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23508 r.x, r.y, r.width, r.height));
23509
23510 /* Convert to window coordinates. */
23511 r.x -= WINDOW_LEFT_EDGE_X (w);
23512 r.y -= WINDOW_TOP_EDGE_Y (w);
23513
23514 /* Turn off the cursor. */
23515 if (!w->pseudo_window_p
23516 && phys_cursor_in_rect_p (w, &r))
23517 {
23518 x_clear_cursor (w);
23519 cursor_cleared_p = 1;
23520 }
23521 else
23522 cursor_cleared_p = 0;
23523
23524 /* Update lines intersecting rectangle R. */
23525 first_overlapping_row = last_overlapping_row = NULL;
23526 for (row = w->current_matrix->rows;
23527 row->enabled_p;
23528 ++row)
23529 {
23530 int y0 = row->y;
23531 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23532
23533 if ((y0 >= r.y && y0 < r.y + r.height)
23534 || (y1 > r.y && y1 < r.y + r.height)
23535 || (r.y >= y0 && r.y < y1)
23536 || (r.y + r.height > y0 && r.y + r.height < y1))
23537 {
23538 /* A header line may be overlapping, but there is no need
23539 to fix overlapping areas for them. KFS 2005-02-12 */
23540 if (row->overlapping_p && !row->mode_line_p)
23541 {
23542 if (first_overlapping_row == NULL)
23543 first_overlapping_row = row;
23544 last_overlapping_row = row;
23545 }
23546
23547 if (expose_line (w, row, &r))
23548 mouse_face_overwritten_p = 1;
23549 }
23550
23551 if (y1 >= yb)
23552 break;
23553 }
23554
23555 /* Display the mode line if there is one. */
23556 if (WINDOW_WANTS_MODELINE_P (w)
23557 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23558 row->enabled_p)
23559 && row->y < r.y + r.height)
23560 {
23561 if (expose_line (w, row, &r))
23562 mouse_face_overwritten_p = 1;
23563 }
23564
23565 if (!w->pseudo_window_p)
23566 {
23567 /* Fix the display of overlapping rows. */
23568 if (first_overlapping_row)
23569 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23570
23571 /* Draw border between windows. */
23572 x_draw_vertical_border (w);
23573
23574 /* Turn the cursor on again. */
23575 if (cursor_cleared_p)
23576 update_window_cursor (w, 1);
23577 }
23578 }
23579
23580 return mouse_face_overwritten_p;
23581 }
23582
23583
23584
23585 /* Redraw (parts) of all windows in the window tree rooted at W that
23586 intersect R. R contains frame pixel coordinates. Value is
23587 non-zero if the exposure overwrites mouse-face. */
23588
23589 static int
23590 expose_window_tree (w, r)
23591 struct window *w;
23592 XRectangle *r;
23593 {
23594 struct frame *f = XFRAME (w->frame);
23595 int mouse_face_overwritten_p = 0;
23596
23597 while (w && !FRAME_GARBAGED_P (f))
23598 {
23599 if (!NILP (w->hchild))
23600 mouse_face_overwritten_p
23601 |= expose_window_tree (XWINDOW (w->hchild), r);
23602 else if (!NILP (w->vchild))
23603 mouse_face_overwritten_p
23604 |= expose_window_tree (XWINDOW (w->vchild), r);
23605 else
23606 mouse_face_overwritten_p |= expose_window (w, r);
23607
23608 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23609 }
23610
23611 return mouse_face_overwritten_p;
23612 }
23613
23614
23615 /* EXPORT:
23616 Redisplay an exposed area of frame F. X and Y are the upper-left
23617 corner of the exposed rectangle. W and H are width and height of
23618 the exposed area. All are pixel values. W or H zero means redraw
23619 the entire frame. */
23620
23621 void
23622 expose_frame (f, x, y, w, h)
23623 struct frame *f;
23624 int x, y, w, h;
23625 {
23626 XRectangle r;
23627 int mouse_face_overwritten_p = 0;
23628
23629 TRACE ((stderr, "expose_frame "));
23630
23631 /* No need to redraw if frame will be redrawn soon. */
23632 if (FRAME_GARBAGED_P (f))
23633 {
23634 TRACE ((stderr, " garbaged\n"));
23635 return;
23636 }
23637
23638 /* If basic faces haven't been realized yet, there is no point in
23639 trying to redraw anything. This can happen when we get an expose
23640 event while Emacs is starting, e.g. by moving another window. */
23641 if (FRAME_FACE_CACHE (f) == NULL
23642 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23643 {
23644 TRACE ((stderr, " no faces\n"));
23645 return;
23646 }
23647
23648 if (w == 0 || h == 0)
23649 {
23650 r.x = r.y = 0;
23651 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23652 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23653 }
23654 else
23655 {
23656 r.x = x;
23657 r.y = y;
23658 r.width = w;
23659 r.height = h;
23660 }
23661
23662 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23663 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23664
23665 if (WINDOWP (f->tool_bar_window))
23666 mouse_face_overwritten_p
23667 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23668
23669 #ifdef HAVE_X_WINDOWS
23670 #ifndef MSDOS
23671 #ifndef USE_X_TOOLKIT
23672 if (WINDOWP (f->menu_bar_window))
23673 mouse_face_overwritten_p
23674 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23675 #endif /* not USE_X_TOOLKIT */
23676 #endif
23677 #endif
23678
23679 /* Some window managers support a focus-follows-mouse style with
23680 delayed raising of frames. Imagine a partially obscured frame,
23681 and moving the mouse into partially obscured mouse-face on that
23682 frame. The visible part of the mouse-face will be highlighted,
23683 then the WM raises the obscured frame. With at least one WM, KDE
23684 2.1, Emacs is not getting any event for the raising of the frame
23685 (even tried with SubstructureRedirectMask), only Expose events.
23686 These expose events will draw text normally, i.e. not
23687 highlighted. Which means we must redo the highlight here.
23688 Subsume it under ``we love X''. --gerd 2001-08-15 */
23689 /* Included in Windows version because Windows most likely does not
23690 do the right thing if any third party tool offers
23691 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23692 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23693 {
23694 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23695 if (f == dpyinfo->mouse_face_mouse_frame)
23696 {
23697 int x = dpyinfo->mouse_face_mouse_x;
23698 int y = dpyinfo->mouse_face_mouse_y;
23699 clear_mouse_face (dpyinfo);
23700 note_mouse_highlight (f, x, y);
23701 }
23702 }
23703 }
23704
23705
23706 /* EXPORT:
23707 Determine the intersection of two rectangles R1 and R2. Return
23708 the intersection in *RESULT. Value is non-zero if RESULT is not
23709 empty. */
23710
23711 int
23712 x_intersect_rectangles (r1, r2, result)
23713 XRectangle *r1, *r2, *result;
23714 {
23715 XRectangle *left, *right;
23716 XRectangle *upper, *lower;
23717 int intersection_p = 0;
23718
23719 /* Rearrange so that R1 is the left-most rectangle. */
23720 if (r1->x < r2->x)
23721 left = r1, right = r2;
23722 else
23723 left = r2, right = r1;
23724
23725 /* X0 of the intersection is right.x0, if this is inside R1,
23726 otherwise there is no intersection. */
23727 if (right->x <= left->x + left->width)
23728 {
23729 result->x = right->x;
23730
23731 /* The right end of the intersection is the minimum of the
23732 the right ends of left and right. */
23733 result->width = (min (left->x + left->width, right->x + right->width)
23734 - result->x);
23735
23736 /* Same game for Y. */
23737 if (r1->y < r2->y)
23738 upper = r1, lower = r2;
23739 else
23740 upper = r2, lower = r1;
23741
23742 /* The upper end of the intersection is lower.y0, if this is inside
23743 of upper. Otherwise, there is no intersection. */
23744 if (lower->y <= upper->y + upper->height)
23745 {
23746 result->y = lower->y;
23747
23748 /* The lower end of the intersection is the minimum of the lower
23749 ends of upper and lower. */
23750 result->height = (min (lower->y + lower->height,
23751 upper->y + upper->height)
23752 - result->y);
23753 intersection_p = 1;
23754 }
23755 }
23756
23757 return intersection_p;
23758 }
23759
23760 #endif /* HAVE_WINDOW_SYSTEM */
23761
23762 \f
23763 /***********************************************************************
23764 Initialization
23765 ***********************************************************************/
23766
23767 void
23768 syms_of_xdisp ()
23769 {
23770 Vwith_echo_area_save_vector = Qnil;
23771 staticpro (&Vwith_echo_area_save_vector);
23772
23773 Vmessage_stack = Qnil;
23774 staticpro (&Vmessage_stack);
23775
23776 Qinhibit_redisplay = intern ("inhibit-redisplay");
23777 staticpro (&Qinhibit_redisplay);
23778
23779 message_dolog_marker1 = Fmake_marker ();
23780 staticpro (&message_dolog_marker1);
23781 message_dolog_marker2 = Fmake_marker ();
23782 staticpro (&message_dolog_marker2);
23783 message_dolog_marker3 = Fmake_marker ();
23784 staticpro (&message_dolog_marker3);
23785
23786 #if GLYPH_DEBUG
23787 defsubr (&Sdump_frame_glyph_matrix);
23788 defsubr (&Sdump_glyph_matrix);
23789 defsubr (&Sdump_glyph_row);
23790 defsubr (&Sdump_tool_bar_row);
23791 defsubr (&Strace_redisplay);
23792 defsubr (&Strace_to_stderr);
23793 #endif
23794 #ifdef HAVE_WINDOW_SYSTEM
23795 defsubr (&Stool_bar_lines_needed);
23796 defsubr (&Slookup_image_map);
23797 #endif
23798 defsubr (&Sformat_mode_line);
23799
23800 staticpro (&Qmenu_bar_update_hook);
23801 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23802
23803 staticpro (&Qoverriding_terminal_local_map);
23804 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23805
23806 staticpro (&Qoverriding_local_map);
23807 Qoverriding_local_map = intern ("overriding-local-map");
23808
23809 staticpro (&Qwindow_scroll_functions);
23810 Qwindow_scroll_functions = intern ("window-scroll-functions");
23811
23812 staticpro (&Qredisplay_end_trigger_functions);
23813 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23814
23815 staticpro (&Qinhibit_point_motion_hooks);
23816 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23817
23818 QCdata = intern (":data");
23819 staticpro (&QCdata);
23820 Qdisplay = intern ("display");
23821 staticpro (&Qdisplay);
23822 Qspace_width = intern ("space-width");
23823 staticpro (&Qspace_width);
23824 Qraise = intern ("raise");
23825 staticpro (&Qraise);
23826 Qslice = intern ("slice");
23827 staticpro (&Qslice);
23828 Qspace = intern ("space");
23829 staticpro (&Qspace);
23830 Qmargin = intern ("margin");
23831 staticpro (&Qmargin);
23832 Qpointer = intern ("pointer");
23833 staticpro (&Qpointer);
23834 Qleft_margin = intern ("left-margin");
23835 staticpro (&Qleft_margin);
23836 Qright_margin = intern ("right-margin");
23837 staticpro (&Qright_margin);
23838 Qcenter = intern ("center");
23839 staticpro (&Qcenter);
23840 Qline_height = intern ("line-height");
23841 staticpro (&Qline_height);
23842 QCalign_to = intern (":align-to");
23843 staticpro (&QCalign_to);
23844 QCrelative_width = intern (":relative-width");
23845 staticpro (&QCrelative_width);
23846 QCrelative_height = intern (":relative-height");
23847 staticpro (&QCrelative_height);
23848 QCeval = intern (":eval");
23849 staticpro (&QCeval);
23850 QCpropertize = intern (":propertize");
23851 staticpro (&QCpropertize);
23852 QCfile = intern (":file");
23853 staticpro (&QCfile);
23854 Qfontified = intern ("fontified");
23855 staticpro (&Qfontified);
23856 Qfontification_functions = intern ("fontification-functions");
23857 staticpro (&Qfontification_functions);
23858 Qtrailing_whitespace = intern ("trailing-whitespace");
23859 staticpro (&Qtrailing_whitespace);
23860 Qescape_glyph = intern ("escape-glyph");
23861 staticpro (&Qescape_glyph);
23862 Qnobreak_space = intern ("nobreak-space");
23863 staticpro (&Qnobreak_space);
23864 Qimage = intern ("image");
23865 staticpro (&Qimage);
23866 QCmap = intern (":map");
23867 staticpro (&QCmap);
23868 QCpointer = intern (":pointer");
23869 staticpro (&QCpointer);
23870 Qrect = intern ("rect");
23871 staticpro (&Qrect);
23872 Qcircle = intern ("circle");
23873 staticpro (&Qcircle);
23874 Qpoly = intern ("poly");
23875 staticpro (&Qpoly);
23876 Qmessage_truncate_lines = intern ("message-truncate-lines");
23877 staticpro (&Qmessage_truncate_lines);
23878 Qgrow_only = intern ("grow-only");
23879 staticpro (&Qgrow_only);
23880 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23881 staticpro (&Qinhibit_menubar_update);
23882 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23883 staticpro (&Qinhibit_eval_during_redisplay);
23884 Qposition = intern ("position");
23885 staticpro (&Qposition);
23886 Qbuffer_position = intern ("buffer-position");
23887 staticpro (&Qbuffer_position);
23888 Qobject = intern ("object");
23889 staticpro (&Qobject);
23890 Qbar = intern ("bar");
23891 staticpro (&Qbar);
23892 Qhbar = intern ("hbar");
23893 staticpro (&Qhbar);
23894 Qbox = intern ("box");
23895 staticpro (&Qbox);
23896 Qhollow = intern ("hollow");
23897 staticpro (&Qhollow);
23898 Qhand = intern ("hand");
23899 staticpro (&Qhand);
23900 Qarrow = intern ("arrow");
23901 staticpro (&Qarrow);
23902 Qtext = intern ("text");
23903 staticpro (&Qtext);
23904 Qrisky_local_variable = intern ("risky-local-variable");
23905 staticpro (&Qrisky_local_variable);
23906 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23907 staticpro (&Qinhibit_free_realized_faces);
23908
23909 list_of_error = Fcons (Fcons (intern ("error"),
23910 Fcons (intern ("void-variable"), Qnil)),
23911 Qnil);
23912 staticpro (&list_of_error);
23913
23914 Qlast_arrow_position = intern ("last-arrow-position");
23915 staticpro (&Qlast_arrow_position);
23916 Qlast_arrow_string = intern ("last-arrow-string");
23917 staticpro (&Qlast_arrow_string);
23918
23919 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23920 staticpro (&Qoverlay_arrow_string);
23921 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23922 staticpro (&Qoverlay_arrow_bitmap);
23923
23924 echo_buffer[0] = echo_buffer[1] = Qnil;
23925 staticpro (&echo_buffer[0]);
23926 staticpro (&echo_buffer[1]);
23927
23928 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23929 staticpro (&echo_area_buffer[0]);
23930 staticpro (&echo_area_buffer[1]);
23931
23932 Vmessages_buffer_name = build_string ("*Messages*");
23933 staticpro (&Vmessages_buffer_name);
23934
23935 mode_line_proptrans_alist = Qnil;
23936 staticpro (&mode_line_proptrans_alist);
23937 mode_line_string_list = Qnil;
23938 staticpro (&mode_line_string_list);
23939 mode_line_string_face = Qnil;
23940 staticpro (&mode_line_string_face);
23941 mode_line_string_face_prop = Qnil;
23942 staticpro (&mode_line_string_face_prop);
23943 Vmode_line_unwind_vector = Qnil;
23944 staticpro (&Vmode_line_unwind_vector);
23945
23946 help_echo_string = Qnil;
23947 staticpro (&help_echo_string);
23948 help_echo_object = Qnil;
23949 staticpro (&help_echo_object);
23950 help_echo_window = Qnil;
23951 staticpro (&help_echo_window);
23952 previous_help_echo_string = Qnil;
23953 staticpro (&previous_help_echo_string);
23954 help_echo_pos = -1;
23955
23956 #ifdef HAVE_WINDOW_SYSTEM
23957 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23958 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23959 For example, if a block cursor is over a tab, it will be drawn as
23960 wide as that tab on the display. */);
23961 x_stretch_cursor_p = 0;
23962 #endif
23963
23964 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23965 doc: /* *Non-nil means highlight trailing whitespace.
23966 The face used for trailing whitespace is `trailing-whitespace'. */);
23967 Vshow_trailing_whitespace = Qnil;
23968
23969 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23970 doc: /* *Control highlighting of nobreak space and soft hyphen.
23971 A value of t means highlight the character itself (for nobreak space,
23972 use face `nobreak-space').
23973 A value of nil means no highlighting.
23974 Other values mean display the escape glyph followed by an ordinary
23975 space or ordinary hyphen. */);
23976 Vnobreak_char_display = Qt;
23977
23978 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23979 doc: /* *The pointer shape to show in void text areas.
23980 A value of nil means to show the text pointer. Other options are `arrow',
23981 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23982 Vvoid_text_area_pointer = Qarrow;
23983
23984 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23985 doc: /* Non-nil means don't actually do any redisplay.
23986 This is used for internal purposes. */);
23987 Vinhibit_redisplay = Qnil;
23988
23989 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23990 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23991 Vglobal_mode_string = Qnil;
23992
23993 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23994 doc: /* Marker for where to display an arrow on top of the buffer text.
23995 This must be the beginning of a line in order to work.
23996 See also `overlay-arrow-string'. */);
23997 Voverlay_arrow_position = Qnil;
23998
23999 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24000 doc: /* String to display as an arrow in non-window frames.
24001 See also `overlay-arrow-position'. */);
24002 Voverlay_arrow_string = build_string ("=>");
24003
24004 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24005 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24006 The symbols on this list are examined during redisplay to determine
24007 where to display overlay arrows. */);
24008 Voverlay_arrow_variable_list
24009 = Fcons (intern ("overlay-arrow-position"), Qnil);
24010
24011 DEFVAR_INT ("scroll-step", &scroll_step,
24012 doc: /* *The number of lines to try scrolling a window by when point moves out.
24013 If that fails to bring point back on frame, point is centered instead.
24014 If this is zero, point is always centered after it moves off frame.
24015 If you want scrolling to always be a line at a time, you should set
24016 `scroll-conservatively' to a large value rather than set this to 1. */);
24017
24018 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24019 doc: /* *Scroll up to this many lines, to bring point back on screen.
24020 A value of zero means to scroll the text to center point vertically
24021 in the window. */);
24022 scroll_conservatively = 0;
24023
24024 DEFVAR_INT ("scroll-margin", &scroll_margin,
24025 doc: /* *Number of lines of margin at the top and bottom of a window.
24026 Recenter the window whenever point gets within this many lines
24027 of the top or bottom of the window. */);
24028 scroll_margin = 0;
24029
24030 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24031 doc: /* Pixels per inch value for non-window system displays.
24032 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24033 Vdisplay_pixels_per_inch = make_float (72.0);
24034
24035 #if GLYPH_DEBUG
24036 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24037 #endif
24038
24039 DEFVAR_BOOL ("truncate-partial-width-windows",
24040 &truncate_partial_width_windows,
24041 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24042 truncate_partial_width_windows = 1;
24043
24044 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24045 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
24046 Any other value means to use the appropriate face, `mode-line',
24047 `header-line', or `menu' respectively. */);
24048 mode_line_inverse_video = 1;
24049
24050 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24051 doc: /* *Maximum buffer size for which line number should be displayed.
24052 If the buffer is bigger than this, the line number does not appear
24053 in the mode line. A value of nil means no limit. */);
24054 Vline_number_display_limit = Qnil;
24055
24056 DEFVAR_INT ("line-number-display-limit-width",
24057 &line_number_display_limit_width,
24058 doc: /* *Maximum line width (in characters) for line number display.
24059 If the average length of the lines near point is bigger than this, then the
24060 line number may be omitted from the mode line. */);
24061 line_number_display_limit_width = 200;
24062
24063 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24064 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24065 highlight_nonselected_windows = 0;
24066
24067 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24068 doc: /* Non-nil if more than one frame is visible on this display.
24069 Minibuffer-only frames don't count, but iconified frames do.
24070 This variable is not guaranteed to be accurate except while processing
24071 `frame-title-format' and `icon-title-format'. */);
24072
24073 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24074 doc: /* Template for displaying the title bar of visible frames.
24075 \(Assuming the window manager supports this feature.)
24076 This variable has the same structure as `mode-line-format' (which see),
24077 and is used only on frames for which no explicit name has been set
24078 \(see `modify-frame-parameters'). */);
24079
24080 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24081 doc: /* Template for displaying the title bar of an iconified frame.
24082 \(Assuming the window manager supports this feature.)
24083 This variable has the same structure as `mode-line-format' (which see),
24084 and is used only on frames for which no explicit name has been set
24085 \(see `modify-frame-parameters'). */);
24086 Vicon_title_format
24087 = Vframe_title_format
24088 = Fcons (intern ("multiple-frames"),
24089 Fcons (build_string ("%b"),
24090 Fcons (Fcons (empty_string,
24091 Fcons (intern ("invocation-name"),
24092 Fcons (build_string ("@"),
24093 Fcons (intern ("system-name"),
24094 Qnil)))),
24095 Qnil)));
24096
24097 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24098 doc: /* Maximum number of lines to keep in the message log buffer.
24099 If nil, disable message logging. If t, log messages but don't truncate
24100 the buffer when it becomes large. */);
24101 Vmessage_log_max = make_number (50);
24102
24103 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24104 doc: /* Functions called before redisplay, if window sizes have changed.
24105 The value should be a list of functions that take one argument.
24106 Just before redisplay, for each frame, if any of its windows have changed
24107 size since the last redisplay, or have been split or deleted,
24108 all the functions in the list are called, with the frame as argument. */);
24109 Vwindow_size_change_functions = Qnil;
24110
24111 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24112 doc: /* List of functions to call before redisplaying a window with scrolling.
24113 Each function is called with two arguments, the window
24114 and its new display-start position. Note that the value of `window-end'
24115 is not valid when these functions are called. */);
24116 Vwindow_scroll_functions = Qnil;
24117
24118 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24119 doc: /* Functions called when redisplay of a window reaches the end trigger.
24120 Each function is called with two arguments, the window and the end trigger value.
24121 See `set-window-redisplay-end-trigger'. */);
24122 Vredisplay_end_trigger_functions = Qnil;
24123
24124 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
24125 doc: /* *Non-nil means autoselect window with mouse pointer. */);
24126 mouse_autoselect_window = 0;
24127
24128 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
24129 doc: /* *Non-nil means automatically resize tool-bars.
24130 This increases a tool-bar's height if not all tool-bar items are visible.
24131 It decreases a tool-bar's height when it would display blank lines
24132 otherwise. */);
24133 auto_resize_tool_bars_p = 1;
24134
24135 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24136 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24137 auto_raise_tool_bar_buttons_p = 1;
24138
24139 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24140 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24141 make_cursor_line_fully_visible_p = 1;
24142
24143 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24144 doc: /* *Border below tool-bar in pixels.
24145 If an integer, use it as the height of the border.
24146 If it is one of `internal-border-width' or `border-width', use the
24147 value of the corresponding frame parameter.
24148 Otherwise, no border is added below the tool-bar. */);
24149 Vtool_bar_border = Qinternal_border_width;
24150
24151 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24152 doc: /* *Margin around tool-bar buttons in pixels.
24153 If an integer, use that for both horizontal and vertical margins.
24154 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24155 HORZ specifying the horizontal margin, and VERT specifying the
24156 vertical margin. */);
24157 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24158
24159 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24160 doc: /* *Relief thickness of tool-bar buttons. */);
24161 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24162
24163 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24164 doc: /* List of functions to call to fontify regions of text.
24165 Each function is called with one argument POS. Functions must
24166 fontify a region starting at POS in the current buffer, and give
24167 fontified regions the property `fontified'. */);
24168 Vfontification_functions = Qnil;
24169 Fmake_variable_buffer_local (Qfontification_functions);
24170
24171 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24172 &unibyte_display_via_language_environment,
24173 doc: /* *Non-nil means display unibyte text according to language environment.
24174 Specifically this means that unibyte non-ASCII characters
24175 are displayed by converting them to the equivalent multibyte characters
24176 according to the current language environment. As a result, they are
24177 displayed according to the current fontset. */);
24178 unibyte_display_via_language_environment = 0;
24179
24180 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24181 doc: /* *Maximum height for resizing mini-windows.
24182 If a float, it specifies a fraction of the mini-window frame's height.
24183 If an integer, it specifies a number of lines. */);
24184 Vmax_mini_window_height = make_float (0.25);
24185
24186 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24187 doc: /* *How to resize mini-windows.
24188 A value of nil means don't automatically resize mini-windows.
24189 A value of t means resize them to fit the text displayed in them.
24190 A value of `grow-only', the default, means let mini-windows grow
24191 only, until their display becomes empty, at which point the windows
24192 go back to their normal size. */);
24193 Vresize_mini_windows = Qgrow_only;
24194
24195 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24196 doc: /* Alist specifying how to blink the cursor off.
24197 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24198 `cursor-type' frame-parameter or variable equals ON-STATE,
24199 comparing using `equal', Emacs uses OFF-STATE to specify
24200 how to blink it off. ON-STATE and OFF-STATE are values for
24201 the `cursor-type' frame parameter.
24202
24203 If a frame's ON-STATE has no entry in this list,
24204 the frame's other specifications determine how to blink the cursor off. */);
24205 Vblink_cursor_alist = Qnil;
24206
24207 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24208 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24209 automatic_hscrolling_p = 1;
24210
24211 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24212 doc: /* *How many columns away from the window edge point is allowed to get
24213 before automatic hscrolling will horizontally scroll the window. */);
24214 hscroll_margin = 5;
24215
24216 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24217 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24218 When point is less than `hscroll-margin' columns from the window
24219 edge, automatic hscrolling will scroll the window by the amount of columns
24220 determined by this variable. If its value is a positive integer, scroll that
24221 many columns. If it's a positive floating-point number, it specifies the
24222 fraction of the window's width to scroll. If it's nil or zero, point will be
24223 centered horizontally after the scroll. Any other value, including negative
24224 numbers, are treated as if the value were zero.
24225
24226 Automatic hscrolling always moves point outside the scroll margin, so if
24227 point was more than scroll step columns inside the margin, the window will
24228 scroll more than the value given by the scroll step.
24229
24230 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24231 and `scroll-right' overrides this variable's effect. */);
24232 Vhscroll_step = make_number (0);
24233
24234 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24235 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24236 Bind this around calls to `message' to let it take effect. */);
24237 message_truncate_lines = 0;
24238
24239 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24240 doc: /* Normal hook run to update the menu bar definitions.
24241 Redisplay runs this hook before it redisplays the menu bar.
24242 This is used to update submenus such as Buffers,
24243 whose contents depend on various data. */);
24244 Vmenu_bar_update_hook = Qnil;
24245
24246 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24247 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24248 inhibit_menubar_update = 0;
24249
24250 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24251 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24252 inhibit_eval_during_redisplay = 0;
24253
24254 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24255 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24256 inhibit_free_realized_faces = 0;
24257
24258 #if GLYPH_DEBUG
24259 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24260 doc: /* Inhibit try_window_id display optimization. */);
24261 inhibit_try_window_id = 0;
24262
24263 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24264 doc: /* Inhibit try_window_reusing display optimization. */);
24265 inhibit_try_window_reusing = 0;
24266
24267 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24268 doc: /* Inhibit try_cursor_movement display optimization. */);
24269 inhibit_try_cursor_movement = 0;
24270 #endif /* GLYPH_DEBUG */
24271 }
24272
24273
24274 /* Initialize this module when Emacs starts. */
24275
24276 void
24277 init_xdisp ()
24278 {
24279 Lisp_Object root_window;
24280 struct window *mini_w;
24281
24282 current_header_line_height = current_mode_line_height = -1;
24283
24284 CHARPOS (this_line_start_pos) = 0;
24285
24286 mini_w = XWINDOW (minibuf_window);
24287 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24288
24289 if (!noninteractive)
24290 {
24291 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24292 int i;
24293
24294 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24295 set_window_height (root_window,
24296 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24297 0);
24298 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24299 set_window_height (minibuf_window, 1, 0);
24300
24301 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24302 mini_w->total_cols = make_number (FRAME_COLS (f));
24303
24304 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24305 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24306 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24307
24308 /* The default ellipsis glyphs `...'. */
24309 for (i = 0; i < 3; ++i)
24310 default_invis_vector[i] = make_number ('.');
24311 }
24312
24313 {
24314 /* Allocate the buffer for frame titles.
24315 Also used for `format-mode-line'. */
24316 int size = 100;
24317 mode_line_noprop_buf = (char *) xmalloc (size);
24318 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24319 mode_line_noprop_ptr = mode_line_noprop_buf;
24320 mode_line_target = MODE_LINE_DISPLAY;
24321 }
24322
24323 help_echo_showing_p = 0;
24324 }
24325
24326
24327 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24328 (do not change this comment) */