xdisp.c 364 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Display generation from window structure and buffer text.
Gerd Moellmann's avatar
Gerd Moellmann committed
2 3
   Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99
   Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6 7 8

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
10 11 12 13 14 15 16 17 18
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
19 20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
21

Gerd Moellmann's avatar
Gerd Moellmann committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/* New redisplay written by Gerd Moellmann <gerd@gnu.org>.

   Redisplay.

   Emacs separates the task of updating the display from code
   modifying global state, e.g. buffer text.  This way functions
   operating on buffers don't also have to be concerned with updating
   the display.

   Updating the display is triggered by the Lisp interpreter when it
   decides it's time to do it.  This is done either automatically for
   you as part of the interpreter's command loop or as the result of
   calling Lisp functions like `sit-for'.  The C function `redisplay'
   in xdisp.c is the only entry into the inner redisplay code.  (Or,
   let's say almost---see the the description of direct update
   operations, below.).

   The following diagram shows how redisplay code is invoked.  As you
   can see, Lisp calls redisplay and vice versa.  Under window systems
   like X, some portions of the redisplay code are also called
   asynchronously during mouse movement or expose events.  It is very
   important that these code parts do NOT use the C library (malloc,
   free) because many C libraries under Unix are not reentrant.  They
   may also NOT call functions of the Lisp interpreter which could
   change the interpreter's state.  If you don't follow these rules,
   you will encounter bugs which are very hard to explain.

	     (Direct functions, see below)
             direct_output_for_insert, 
             direct_forward_char (dispnew.c)
   	  +---------------------------------+
          |                                 |
	  |                                 V
   +--------------+   redisplay()   +----------------+
   | Lisp machine |---------------->| Redisplay code |<--+
   +--------------+   (xdisp.c)     +----------------+   |
	  ^				     |		 |
	  +----------------------------------+           |
	    Don't use this path when called		 |
	    asynchronously!				 |
                                                         |
                           expose_window (asynchronous)  |
                                                         |
			           X expose events  -----+

   What does redisplay?  Obviously, it has to figure out somehow what
   has been changed since the last time the display has been updated,
   and to make these changes visible.  Preferably it would do that in
   a moderately intelligent way, i.e. fast.

   Changes in buffer text can be deduced from window and buffer
   structures, and from some global variables like `beg_unchanged' and
   `end_unchanged'.  The contents of the display are additionally
   recorded in a `glyph matrix', a two-dimensional matrix of glyph
   structures.  Each row in such a matrix corresponds to a line on the
   display, and each glyph in a row corresponds to a column displaying
   a character, an image, or what else.  This matrix is called the
   `current glyph matrix' or `current matrix' in redisplay
   terminology.

   For buffer parts that have been changed since the last update, a
   second glyph matrix is constructed, the so called `desired glyph
   matrix' or short `desired matrix'.  Current and desired matrix are
   then compared to find a cheap way to update the display, e.g. by
   reusing part of the display by scrolling lines.


   Direct operations.

   You will find a lot of of redisplay optimizations when you start
   looking at the innards of redisplay.  The overall goal of all these
   optimizations is to make redisplay fast because it is done
   frequently.

   Two optimizations are not found in xdisp.c.  These are the direct
   operations mentioned above.  As the name suggests they follow a
   different principle than the rest of redisplay.  Instead of
   building a desired matrix and then comparing it with the current
   display, they perform their actions directly on the display and on
   the current matrix.

   One direct operation updates the display after one character has
   been entered.  The other one moves the cursor by one position
   forward or backward.  You find these functions under the names
   `direct_output_for_insert' and `direct_output_forward_char' in
   dispnew.c.

   
   Desired matrices.

   Desired matrices are always built per Emacs window.  The function
   `display_line' is the central function to look at if you are
   interested.  It constructs one row in a desired matrix given an
   iterator structure containing both a buffer position and a
   description of the environment in which the text is to be
   displayed.  But this is too early, read on.

   Characters and pixmaps displayed for a range of buffer text depend
   on various settings of buffers and windows, on overlays and text
   properties, on display tables, on selective display.  The good news
   is that all this hairy stuff is hidden behind a small set of
   interface functions taking a iterator structure (struct it)
   argument.

   Iteration over things to be be displayed is then simple.  It is
   started by initializing an iterator with a call to init_iterator
   (or init_string_iterator for that matter).  Calls to
   get_next_display_element fill the iterator structure with relevant
   information about the next thing to display.  Calls to
   set_iterator_to_next move the iterator to the next thing.

   Besides this, an iterator also contains information about the
   display environment in which glyphs for display elements are to be
   produced.  It has fields for the width and height of the display,
   the information whether long lines are truncated or continued, a
   current X and Y position, and lots of other stuff you can better
   see in dispextern.h.

   Glyphs in a desired matrix are normally constructed in a loop
   calling get_next_display_element and then produce_glyphs.  The call
   to produce_glyphs will fill the iterator structure with pixel
   information about the element being displayed and at the same time
   produce glyphs for it.  If the display element fits on the line
   being displayed, set_iterator_to_next is called next, otherwise the
   glyphs produced are discarded.


   Frame matrices.

   That just couldn't be all, could it?  What about terminal types not
   supporting operations on sub-windows of the screen?  To update the
   display on such a terminal, window-based glyph matrices are not
   well suited.  To be able to reuse part of the display (scrolling
   lines up and down), we must instead have a view of the whole
   screen.  This is what `frame matrices' are for.  They are a trick.

   Frames on terminals like above have a glyph pool.  Windows on such
   a frame sub-allocate their glyph memory from their frame's glyph
   pool.  The frame itself is given its own glyph matrices.  By
   coincidence---or maybe something else---rows in window glyph
   matrices are slices of corresponding rows in frame matrices.  Thus
   writing to window matrices implicitly updates a frame matrix which
   provides us with the view of the whole screen that we originally
   wanted to have without having to move many bytes around.  To be
   honest, there is a little bit more done, but not much more.  If you
   plan to extend that code, take a look at dispnew.c.  The function
   build_frame_matrix is a good starting point.  */
Jim Blandy's avatar
Jim Blandy committed
169

170
#include <config.h>
Jim Blandy's avatar
Jim Blandy committed
171
#include <stdio.h>
Gerd Moellmann's avatar
Gerd Moellmann committed
172 173 174
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
Jim Blandy's avatar
Jim Blandy committed
175
#include "lisp.h"
Jim Blandy's avatar
Jim Blandy committed
176
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
177 178 179 180
#include "window.h"
#include "termchar.h"
#include "dispextern.h"
#include "buffer.h"
181
#include "charset.h"
Jim Blandy's avatar
Jim Blandy committed
182 183 184 185
#include "indent.h"
#include "commands.h"
#include "macros.h"
#include "disptab.h"
186
#include "termhooks.h"
187
#include "intervals.h"
Karl Heuer's avatar
Karl Heuer committed
188
#include "keyboard.h"
189 190
#include "coding.h"
#include "process.h"
Andreas Schwab's avatar
Andreas Schwab committed
191 192
#include "region-cache.h"

193
#ifdef HAVE_X_WINDOWS
Andreas Schwab's avatar
Andreas Schwab committed
194 195
#include "xterm.h"
#endif
Jim Blandy's avatar
Jim Blandy committed
196

Gerd Moellmann's avatar
Gerd Moellmann committed
197 198 199 200 201
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

#define INFINITY 10000000

202
#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
203
extern void set_frame_menubar ();
204
extern int pending_menu_activation;
205 206
#endif

Jim Blandy's avatar
Jim Blandy committed
207 208 209
extern int interrupt_input;
extern int command_loop_level;

210 211
extern int minibuffer_auto_raise;

212 213
extern Lisp_Object Qface;

214 215
extern Lisp_Object Voverriding_local_map;
extern Lisp_Object Voverriding_local_map_menu_flag;
Gerd Moellmann's avatar
Gerd Moellmann committed
216
extern Lisp_Object Qmenu_item;
217

218
Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
219
Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
220
Lisp_Object Qredisplay_end_trigger_functions;
221
Lisp_Object Qinhibit_point_motion_hooks;
Gerd Moellmann's avatar
Gerd Moellmann committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
Lisp_Object QCeval, QCwhen;
Lisp_Object Qfontified;

/* Functions called to fontify regions of text.  */

Lisp_Object Vfontification_functions;
Lisp_Object Qfontification_functions;

/* Non-zero means draw toolbar buttons raised when the mouse moves
   over them.  */

int auto_raise_toolbar_buttons_p;

/* Margin around toolbar buttons in pixels.  */

int toolbar_button_margin;

/* Thickness of shadow to draw around toolbar buttons.  */

int toolbar_button_relief;

/* Non-zero means automatically resize toolbars so that all toolbar
   items are visible, and no blank lines remain.  */

int auto_resize_toolbars_p;
247

248 249 250 251
/* Non-nil means don't actually do any redisplay.  */

Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;

Gerd Moellmann's avatar
Gerd Moellmann committed
252 253 254 255 256 257 258 259 260 261
/* Names of text properties relevant for redisplay.  */

Lisp_Object Qdisplay, Qrelative_width, Qwidth, Qalign_to;
extern Lisp_Object Qface, Qinvisible, Qimage;

/* Symbols used in text property values.  */

Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qheight, Qraise;

262
/* Non-nil means highlight trailing whitespace.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
263

264
Lisp_Object Vshow_trailing_whitespace;
Gerd Moellmann's avatar
Gerd Moellmann committed
265 266 267 268 269 270 271 272 273 274 275 276

/* Name of the face used to highlight trailing whitespace.  */

Lisp_Object Qtrailing_whitespace;

/* The symbol `image' which is the car of the lists used to represent
   images in Lisp.  */

Lisp_Object Qimage;

/* Non-zero means print newline to stdout before next mini-buffer
   message.  */
Jim Blandy's avatar
Jim Blandy committed
277 278 279

int noninteractive_need_newline;

Gerd Moellmann's avatar
Gerd Moellmann committed
280
/* Non-zero means print newline to message log before next message.  */
281

282
static int message_log_need_newline;
283

Gerd Moellmann's avatar
Gerd Moellmann committed
284 285 286 287 288

/* The buffer position of the first character appearing entirely or
   partially on the line of the selected window which contains the
   cursor; <= 0 if not known.  Set by set_cursor_from_row, used for
   redisplay optimization in redisplay_internal.  */
Jim Blandy's avatar
Jim Blandy committed
289

Gerd Moellmann's avatar
Gerd Moellmann committed
290
static struct text_pos this_line_start_pos;
Jim Blandy's avatar
Jim Blandy committed
291

Gerd Moellmann's avatar
Gerd Moellmann committed
292 293 294 295 296 297
/* Number of characters past the end of the line above, including the
   terminating newline.  */

static struct text_pos this_line_end_pos;

/* The vertical positions and the height of this line.  */
Jim Blandy's avatar
Jim Blandy committed
298 299

static int this_line_vpos;
Gerd Moellmann's avatar
Gerd Moellmann committed
300 301 302 303 304 305 306
static int this_line_y;
static int this_line_pixel_height;

/* X position at which this display line starts.  Usually zero;
   negative if first character is partially visible.  */

static int this_line_start_x;
Jim Blandy's avatar
Jim Blandy committed
307

Gerd Moellmann's avatar
Gerd Moellmann committed
308
/* Buffer that this_line_.* variables are referring to.  */
Jim Blandy's avatar
Jim Blandy committed
309 310 311

static struct buffer *this_line_buffer;

Gerd Moellmann's avatar
Gerd Moellmann committed
312 313
/* Nonzero means truncate lines in all windows less wide than the
   frame.  */
Jim Blandy's avatar
Jim Blandy committed
314 315 316

int truncate_partial_width_windows;

317
/* A flag to control how to display unibyte 8-bit character.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
318

319
int unibyte_display_via_language_environment;
Gerd Moellmann's avatar
Gerd Moellmann committed
320 321 322 323
 
/* Nonzero means we have more than one non-mini-buffer-only frame.
   Not guaranteed to be accurate except while parsing
   frame-title-format.  */
324

325 326
int multiple_frames;

Jim Blandy's avatar
Jim Blandy committed
327 328 329
Lisp_Object Vglobal_mode_string;

/* Marker for where to display an arrow on top of the buffer text.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
330

Jim Blandy's avatar
Jim Blandy committed
331 332
Lisp_Object Voverlay_arrow_position;

Gerd Moellmann's avatar
Gerd Moellmann committed
333 334
/* String to display for the arrow.  Only used on terminal frames.  */

Jim Blandy's avatar
Jim Blandy committed
335 336
Lisp_Object Voverlay_arrow_string;

Gerd Moellmann's avatar
Gerd Moellmann committed
337 338 339 340
/* Values of those variables at last redisplay.  However, if
   Voverlay_arrow_position is a marker, last_arrow_position is its
   numerical position.  */

341 342
static Lisp_Object last_arrow_position, last_arrow_string;

Gerd Moellmann's avatar
Gerd Moellmann committed
343 344
/* Like mode-line-format, but for the title bar on a visible frame.  */

345 346
Lisp_Object Vframe_title_format;

Gerd Moellmann's avatar
Gerd Moellmann committed
347 348
/* Like mode-line-format, but for the title bar on an iconified frame.  */

349 350
Lisp_Object Vicon_title_format;

351 352 353
/* List of functions to call when a window's size changes.  These
   functions get one arg, a frame on which one or more windows' sizes
   have changed.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
354

355 356
static Lisp_Object Vwindow_size_change_functions;

357 358
Lisp_Object Qmenu_bar_update_hook;

Jim Blandy's avatar
Jim Blandy committed
359 360
/* Nonzero if overlay arrow has been displayed once in this window.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
361
static int overlay_arrow_seen;
Karl Heuer's avatar
Karl Heuer committed
362

363 364
/* Nonzero means highlight the region even in nonselected windows.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
365 366 367 368 369
int highlight_nonselected_windows;

/* If cursor motion alone moves point off frame, try scrolling this
   many lines up or down if that will bring it back.  */

370
static int scroll_step;
Jim Blandy's avatar
Jim Blandy committed
371

Gerd Moellmann's avatar
Gerd Moellmann committed
372 373 374
/* Non-0 means scroll just far enough to bring point back on the
   screen, when appropriate.  */

375 376
static int scroll_conservatively;

Gerd Moellmann's avatar
Gerd Moellmann committed
377 378 379 380 381
/* Recenter the window whenever point gets within this many lines of
   the top or bottom of the window.  This value is translated into a
   pixel value by multiplying it with CANON_Y_UNIT, which means that
   there is really a fixed pixel height scroll margin.  */

382 383
int scroll_margin;

Gerd Moellmann's avatar
Gerd Moellmann committed
384 385 386
/* Number of characters of overlap to show, when scrolling a one-line
   window such as a minibuffer.  */

387 388
static int minibuffer_scroll_overlap;

Gerd Moellmann's avatar
Gerd Moellmann committed
389 390 391
/* Number of windows showing the buffer of the selected window (or
   another buffer with the same base buffer).  keyboard.c refers to
   this.  */
Jim Blandy's avatar
Jim Blandy committed
392 393 394

int buffer_shared;

Gerd Moellmann's avatar
Gerd Moellmann committed
395
/* Vector containing glyphs for an ellipsis `...'.  */
Jim Blandy's avatar
Jim Blandy committed
396

Gerd Moellmann's avatar
Gerd Moellmann committed
397
static Lisp_Object default_invis_vector[3];
Jim Blandy's avatar
Jim Blandy committed
398

Gerd Moellmann's avatar
Gerd Moellmann committed
399
/* Nonzero means display mode line highlighted.  */
Jim Blandy's avatar
Jim Blandy committed
400 401 402

int mode_line_inverse_video;

Gerd Moellmann's avatar
Gerd Moellmann committed
403 404
/* Prompt to display in front of the mini-buffer contents.  */

405
Lisp_Object minibuf_prompt;
Jim Blandy's avatar
Jim Blandy committed
406

Gerd Moellmann's avatar
Gerd Moellmann committed
407 408 409
/* Width of current mini-buffer prompt.  Only set after display_line
   of the line that contains the prompt.  */

Jim Blandy's avatar
Jim Blandy committed
410
int minibuf_prompt_width;
Gerd Moellmann's avatar
Gerd Moellmann committed
411 412 413 414 415
int minibuf_prompt_pixel_width;

/* Message to display instead of mini-buffer contents.  This is what
   the functions error and message make, and command echoing uses it
   as well.  It overrides the minibuf_prompt as well as the buffer.  */
Jim Blandy's avatar
Jim Blandy committed
416 417 418

char *echo_area_glyphs;

Gerd Moellmann's avatar
Gerd Moellmann committed
419 420 421 422 423 424 425 426 427
/* A Lisp string to display instead of mini-buffer contents, analogous
   to echo_area_glyphs.  If this is a string, display that string.
   Otherwise, if echo_area_glyphs is non-null, display that.  */

Lisp_Object echo_area_message;

/* This is the length of the message in echo_area_glyphs or
   echo_area_message.  */

428 429
int echo_area_glyphs_length;

Gerd Moellmann's avatar
Gerd Moellmann committed
430 431 432 433 434 435 436 437 438 439 440 441
/* Value of echo_area_glyphs when it was last acted on.  If this is
   nonzero, there is a message on the frame in the mini-buffer and it
   should be erased as soon as it is no longer requested to appear.  */

char *previous_echo_glyphs;
Lisp_Object previous_echo_area_message;
static int previous_echo_glyphs_length;

/* This is the window where the echo area message was displayed.  It
   is always a mini-buffer window, but it may not be the same window
   currently active as a mini-buffer.  */

442 443
Lisp_Object echo_area_window;

444 445
/* Nonzero means multibyte characters were enabled when the echo area
   message was specified.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
446

447 448
int message_enable_multibyte;

Gerd Moellmann's avatar
Gerd Moellmann committed
449 450
/* True if we should redraw the mode lines on the next redisplay.  */

Jim Blandy's avatar
Jim Blandy committed
451 452
int update_mode_lines;

Gerd Moellmann's avatar
Gerd Moellmann committed
453 454 455 456
/* Smallest number of characters before the gap at any time since last
   redisplay that finished.  Valid for current buffer when
   try_window_id can be called.  */

Jim Blandy's avatar
Jim Blandy committed
457 458
int beg_unchanged;

Gerd Moellmann's avatar
Gerd Moellmann committed
459 460 461 462
/* Smallest number of characters after the gap at any time since last
   redisplay that finished.  Valid for current buffer when
   try_window_id can be called.  */

Jim Blandy's avatar
Jim Blandy committed
463 464
int end_unchanged;

Gerd Moellmann's avatar
Gerd Moellmann committed
465 466 467 468
/* MODIFF as of last redisplay that finished; if it matches MODIFF,
   and overlay_unchanged_modified matches OVERLAY_MODIFF, that means
   beg_unchanged and end_unchanged contain no useful information.  */

Jim Blandy's avatar
Jim Blandy committed
469 470
int unchanged_modified;

471
/* OVERLAY_MODIFF as of last redisplay that finished.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
472

473 474
int overlay_unchanged_modified;

Gerd Moellmann's avatar
Gerd Moellmann committed
475 476 477
/* Nonzero if window sizes or contents have changed since last
   redisplay that finished */

Jim Blandy's avatar
Jim Blandy committed
478 479
int windows_or_buffers_changed;

Gerd Moellmann's avatar
Gerd Moellmann committed
480 481 482
/* Nonzero after display_mode_line if %l was used and it displayed a
   line number.  */

483 484 485
int line_number_displayed;

/* Maximum buffer size for which to display line numbers.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
486

487
static int line_number_display_limit;
Karl Heuer's avatar
Karl Heuer committed
488

489 490 491 492
/* line width to consider when repostioning for line number display */

static int line_number_display_limit_width;

Gerd Moellmann's avatar
Gerd Moellmann committed
493 494 495
/* Number of lines to keep in the message log buffer.  t means
   infinite.  nil means don't log at all.  */

Karl Heuer's avatar
Karl Heuer committed
496
Lisp_Object Vmessage_log_max;
497

Gerd Moellmann's avatar
Gerd Moellmann committed
498 499
/* A scratch glyph row with contents used for generating truncation
   glyphs.  Also used in direct_output_for_insert.  */
500

Gerd Moellmann's avatar
Gerd Moellmann committed
501 502 503
#define MAX_SCRATCH_GLYPHS 100
struct glyph_row scratch_glyph_row;
static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
504

Gerd Moellmann's avatar
Gerd Moellmann committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
/* Ascent and height of the last line processed by move_it_to.  */

static int last_max_ascent, last_height;

/* The maximum distance to look ahead for text properties.  Values
   that are too small let us call compute_char_face and similar 
   functions too often which is expensive.  Values that are too large
   let us call compute_char_face and alike too often because we
   might not be interested in text properties that far away.  */

#define TEXT_PROP_DISTANCE_LIMIT 100

/* Non-zero means print traces of redisplay if compiled with
   GLYPH_DEBUG != 0.  */

#if GLYPH_DEBUG
int trace_redisplay_p;
#endif

/* Value returned from text property handlers (see below).  */

enum prop_handled
527
{
Gerd Moellmann's avatar
Gerd Moellmann committed
528 529 530 531 532
  HANDLED_NORMALLY,
  HANDLED_RECOMPUTE_PROPS,
  HANDLED_OVERLAY_STRING_CONSUMED,
  HANDLED_RETURN
};
533

Gerd Moellmann's avatar
Gerd Moellmann committed
534 535
/* A description of text properties that redisplay is interested
   in.  */
536

Gerd Moellmann's avatar
Gerd Moellmann committed
537 538 539 540
struct props
{
  /* The name of the property.  */
  Lisp_Object *name;
541

Gerd Moellmann's avatar
Gerd Moellmann committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
  /* A unique index for the property.  */
  enum prop_idx idx;

  /* A handler function called to set up iterator IT from the property
     at IT's current position.  Value is used to steer handle_stop.  */
  enum prop_handled (*handler) P_ ((struct it *it));
};

static enum prop_handled handle_face_prop P_ ((struct it *));
static enum prop_handled handle_invisible_prop P_ ((struct it *));
static enum prop_handled handle_display_prop P_ ((struct it *));
static enum prop_handled handle_overlay_change P_ ((struct it *));
static enum prop_handled handle_fontified_prop P_ ((struct it *));

/* Properties handled by iterators.  */

static struct props it_props[] =
Karl Heuer's avatar
Karl Heuer committed
559
{
Gerd Moellmann's avatar
Gerd Moellmann committed
560 561 562 563 564 565 566 567
  {&Qfontified,		FONTIFIED_PROP_IDX,	handle_fontified_prop},
  /* Handle `face' before `display' because some sub-properties of
     `display' need to know the face.  */
  {&Qface,		FACE_PROP_IDX,		handle_face_prop},
  {&Qdisplay,		DISPLAY_PROP_IDX,	handle_display_prop},
  {&Qinvisible,		INVISIBLE_PROP_IDX,	handle_invisible_prop},
  {NULL,		0,			NULL}
};
Karl Heuer's avatar
Karl Heuer committed
568

Gerd Moellmann's avatar
Gerd Moellmann committed
569 570
/* Value is the position described by X.  If X is a marker, value is
   the marker_position of X.  Otherwise, value is X.  */
571

Gerd Moellmann's avatar
Gerd Moellmann committed
572
#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
573

Gerd Moellmann's avatar
Gerd Moellmann committed
574
/* Enumeration returned by some move_it_.* functions internally.  */
575

Gerd Moellmann's avatar
Gerd Moellmann committed
576 577 578 579
enum move_it_result
{
  /* Not used.  Undefined value.  */
  MOVE_UNDEFINED,
580

Gerd Moellmann's avatar
Gerd Moellmann committed
581 582
  /* Move ended at the requested buffer position or ZV.  */
  MOVE_POS_MATCH_OR_ZV,
583

Gerd Moellmann's avatar
Gerd Moellmann committed
584 585
  /* Move ended at the requested X pixel position.  */
  MOVE_X_REACHED,
586

Gerd Moellmann's avatar
Gerd Moellmann committed
587 588 589 590 591 592 593
  /* Move within a line ended at the end of a line that must be
     continued.  */
  MOVE_LINE_CONTINUED,
  
  /* Move within a line ended at the end of a line that would
     be displayed truncated.  */
  MOVE_LINE_TRUNCATED,
594

Gerd Moellmann's avatar
Gerd Moellmann committed
595 596 597
  /* Move within a line ended at a line end.  */
  MOVE_NEWLINE_OR_CR
};
598

599

Gerd Moellmann's avatar
Gerd Moellmann committed
600 601 602

/* Function prototypes.  */

603
static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
604
static int string_char_and_length P_ ((unsigned char *, int, int *));
Gerd Moellmann's avatar
Gerd Moellmann committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
					     struct text_pos));
static int compute_window_start_on_continuation_line P_ ((struct window *));
static Lisp_Object eval_handler P_ ((Lisp_Object));
static Lisp_Object eval_form P_ ((Lisp_Object));
static void insert_left_trunc_glyphs P_ ((struct it *));
static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
static void extend_face_to_end_of_line P_ ((struct it *));
static void append_space P_ ((struct it *, int));
static void make_cursor_line_fully_visible P_ ((struct window *));
static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
static int trailing_whitespace_p P_ ((int));
static int message_log_check_duplicate P_ ((int, int, int, int));
int invisible_p P_ ((Lisp_Object, Lisp_Object));
int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
static void push_it P_ ((struct it *));
static void pop_it P_ ((struct it *));
static void sync_frame_with_window_matrix_rows P_ ((struct window *));
static void redisplay_internal P_ ((int));
static void echo_area_display P_ ((int));
static void redisplay_windows P_ ((Lisp_Object));
static void redisplay_window P_ ((Lisp_Object, int));
static void update_menu_bar P_ ((struct frame *, int));
static int try_window_reusing_current_matrix P_ ((struct window *));
static int try_window_id P_ ((struct window *));
static int display_line P_ ((struct it *));
static void display_mode_lines P_ ((struct window *));
static void display_mode_line P_ ((struct window *, enum face_id,
				   Lisp_Object));
static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
static char *decode_mode_spec P_ ((struct window *, char, int, int));
static void display_menu_bar P_ ((struct window *));
static int display_count_lines P_ ((int, int, int, int, int *));
static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
			       int, int, struct it *, int, int, int, int));
static void compute_line_metrics P_ ((struct it *));
static void run_redisplay_end_trigger_hook P_ ((struct it *));
static int get_overlay_strings P_ ((struct it *));
static void next_overlay_string P_ ((struct it *));
void set_iterator_to_next P_ ((struct it *));
static void reseat P_ ((struct it *, struct text_pos, int));
static void reseat_1 P_ ((struct it *, struct text_pos, int));
static void back_to_previous_visible_line_start P_ ((struct it *));
static void reseat_at_previous_visible_line_start P_ ((struct it *));
649
static void reseat_at_next_visible_line_start P_ ((struct it *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
static int next_element_from_display_vector P_ ((struct it *));
static int next_element_from_string P_ ((struct it *));
static int next_element_from_c_string P_ ((struct it *));
static int next_element_from_buffer P_ ((struct it *));
static int next_element_from_image P_ ((struct it *));
static int next_element_from_stretch P_ ((struct it *));
static void load_overlay_strings P_ ((struct it *));
static void init_from_display_pos P_ ((struct it *, struct window *,
				       struct display_pos *));
static void reseat_to_string P_ ((struct it *, unsigned char *,
				  Lisp_Object, int, int, int, int));
static int charset_at_position P_ ((struct text_pos));
static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
							   int, int, int));
void move_it_vertically_backward P_ ((struct it *, int));
static void init_to_row_start P_ ((struct it *, struct window *,
				   struct glyph_row *));
static void init_to_row_end P_ ((struct it *, struct window *,
				 struct glyph_row *));
static void back_to_previous_line_start P_ ((struct it *));
static void forward_to_next_line_start P_ ((struct it *));
static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
						    Lisp_Object, int));
static struct text_pos string_pos P_ ((int, Lisp_Object));
static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
static int number_of_chars P_ ((unsigned char *, int));
static void compute_stop_pos P_ ((struct it *));
static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
				    Lisp_Object));
static int face_before_or_after_it_pos P_ ((struct it *, int));
static int next_overlay_change P_ ((int));
static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
					   Lisp_Object, struct text_pos *));

#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
#define face_after_it_pos(IT)  face_before_or_after_it_pos ((IT), 0)
686

Gerd Moellmann's avatar
Gerd Moellmann committed
687
#ifdef HAVE_WINDOW_SYSTEM
688

Gerd Moellmann's avatar
Gerd Moellmann committed
689 690 691 692
static void update_toolbar P_ ((struct frame *, int));
static void build_desired_toolbar_string P_ ((struct frame *f));
static int redisplay_toolbar P_ ((struct frame *));
static void display_toolbar_line P_ ((struct it *));
693

Gerd Moellmann's avatar
Gerd Moellmann committed
694
#endif /* HAVE_WINDOW_SYSTEM */
695

Gerd Moellmann's avatar
Gerd Moellmann committed
696 697 698 699

/***********************************************************************
		      Window display dimensions
 ***********************************************************************/
700

Gerd Moellmann's avatar
Gerd Moellmann committed
701 702 703 704 705 706 707 708 709 710 711 712 713 714
/* Return the window-relative maximum y + 1 for glyph rows displaying
   text in window W.  This is the height of W minus the height of a
   mode line, if any.  */

INLINE int
window_text_bottom_y (w)
     struct window *w;
{
  struct frame *f = XFRAME (w->frame);
  int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
  
  if (WINDOW_WANTS_MODELINE_P (w))
    height -= CURRENT_MODE_LINE_HEIGHT (w);
  return height;
715 716
}

717

Gerd Moellmann's avatar
Gerd Moellmann committed
718 719 720
/* Return the pixel width of display area AREA of window W.  AREA < 0
   means return the total width of W, not including bitmap areas to
   the left and right of the window.  */
721

Gerd Moellmann's avatar
Gerd Moellmann committed
722 723 724 725 726 727 728 729 730
INLINE int
window_box_width (w, area)
     struct window *w;
     int area;
{
  struct frame *f = XFRAME (w->frame);
  int width = XFASTINT (w->width);
  
  if (!w->pseudo_window_p)
731
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
      width -= FRAME_SCROLL_BAR_WIDTH (f) + 2 * FRAME_FLAGS_AREA_COLS (f);
      
      if (area == TEXT_AREA)
	{
	  if (INTEGERP (w->left_margin_width))
	    width -= XFASTINT (w->left_margin_width);
	  if (INTEGERP (w->right_margin_width))
	    width -= XFASTINT (w->right_margin_width);
	}
      else if (area == LEFT_MARGIN_AREA)
	width = (INTEGERP (w->left_margin_width)
		 ? XFASTINT (w->left_margin_width) : 0);
      else if (area == RIGHT_MARGIN_AREA)
	width = (INTEGERP (w->right_margin_width)
		 ? XFASTINT (w->right_margin_width) : 0);
747
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
748 749

  return width * CANON_X_UNIT (f);
750
}
751 752


Gerd Moellmann's avatar
Gerd Moellmann committed
753 754
/* Return the pixel height of the display area of window W, not
   including mode lines of W, if any..  */
755

Gerd Moellmann's avatar
Gerd Moellmann committed
756 757 758
INLINE int
window_box_height (w)
     struct window *w;
759
{
Gerd Moellmann's avatar
Gerd Moellmann committed
760 761 762 763 764 765 766 767 768 769
  struct frame *f = XFRAME (w->frame);
  int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
  
  if (WINDOW_WANTS_MODELINE_P (w))
    height -= CURRENT_MODE_LINE_HEIGHT (w);

  if (WINDOW_WANTS_TOP_LINE_P (w))
    height -= CURRENT_TOP_LINE_HEIGHT (w);

  return height;
Karl Heuer's avatar
Karl Heuer committed
770 771 772
}


Gerd Moellmann's avatar
Gerd Moellmann committed
773 774 775 776
/* Return the frame-relative coordinate of the left edge of display
   area AREA of window W.  AREA < 0 means return the left edge of the
   whole window, to the right of any bitmap area at the left side of
   W.  */
Karl Heuer's avatar
Karl Heuer committed
777

Gerd Moellmann's avatar
Gerd Moellmann committed
778 779 780 781
INLINE int
window_box_left (w, area)
     struct window *w;
     int area;
782
{
Gerd Moellmann's avatar
Gerd Moellmann committed
783 784
  struct frame *f = XFRAME (w->frame);
  int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
785

Gerd Moellmann's avatar
Gerd Moellmann committed
786
  if (!w->pseudo_window_p)
787
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
788 789 790 791 792 793 794 795
      x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
	    + FRAME_FLAGS_AREA_WIDTH (f));
      
      if (area == TEXT_AREA)
	x += window_box_width (w, LEFT_MARGIN_AREA);
      else if (area == RIGHT_MARGIN_AREA)
	x += (window_box_width (w, LEFT_MARGIN_AREA)
	      + window_box_width (w, TEXT_AREA));
796
    }
797

Gerd Moellmann's avatar
Gerd Moellmann committed
798 799
  return x;
}     
800

801

Gerd Moellmann's avatar
Gerd Moellmann committed
802 803 804 805
/* Return the frame-relative coordinate of the right edge of display
   area AREA of window W.  AREA < 0 means return the left edge of the
   whole window, to the left of any bitmap area at the right side of
   W.  */
806

Gerd Moellmann's avatar
Gerd Moellmann committed
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
INLINE int
window_box_right (w, area)
     struct window *w;
     int area;
{
  return window_box_left (w, area) + window_box_width (w, area);
}     
     

/* Get the bounding box of the display area AREA of window W, without
   mode lines, in frame-relative coordinates.  AREA < 0 means the
   whole window, not including bitmap areas to the left and right of
   the window.  Return in *BOX_X and *BOX_Y the frame-relative pixel
   coordinates of the upper-left corner of the box.  Return in
   *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box.  */

INLINE void
window_box (w, area, box_x, box_y, box_width, box_height)
     struct window *w;
     int area;
     int *box_x, *box_y, *box_width, *box_height;
{
  struct frame *f = XFRAME (w->frame);
  
  *box_width = window_box_width (w, area);
  *box_height = window_box_height (w);
  *box_x = window_box_left (w, area);
  *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
	    + XFASTINT (w->top) * CANON_Y_UNIT (f));
  if (WINDOW_WANTS_TOP_LINE_P (w))
    *box_y += CURRENT_TOP_LINE_HEIGHT (w);
838
}
839 840


Gerd Moellmann's avatar
Gerd Moellmann committed
841 842 843 844 845 846 847
/* Get the bounding box of the display area AREA of window W, without
   mode lines.  AREA < 0 means the whole window, not including bitmap
   areas to the left and right of the window.  Return in *TOP_LEFT_X
   and TOP_LEFT_Y the frame-relative pixel coordinates of the
   upper-left corner of the box.  Return in *BOTTOM_RIGHT_X, and
   *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
   box.  */
848

Gerd Moellmann's avatar
Gerd Moellmann committed
849 850 851 852 853 854
INLINE void
window_box_edges (w, area, top_left_x, top_left_y,
		  bottom_right_x, bottom_right_y)
     struct window *w;
     int area;
     int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
855
{
Gerd Moellmann's avatar
Gerd Moellmann committed
856 857 858 859
  window_box (w, area, top_left_x, top_left_y, bottom_right_x,
	      bottom_right_y);
  *bottom_right_x += *top_left_x;
  *bottom_right_y += *top_left_y;
860 861
}

Gerd Moellmann's avatar
Gerd Moellmann committed
862 863 864 865 866 867


/***********************************************************************
			      Utilities
 ***********************************************************************/

868 869 870 871 872 873 874
/* Return the next character from STR which is MAXLEN bytes long.
   Return in *LEN the length of the character.  This is like
   STRING_CHAR_AND_LENGTH but never returns an invalid character.  If
   we find one, we return a `?', but with the length of the illegal
   character.  */

static INLINE int
875
string_char_and_length (str, maxlen, len)
876
     unsigned char *str;
877
     int maxlen, *len;
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
{
  int c;

  c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
  if (!CHAR_VALID_P (c, 1))
    /* We may not change the length here because other places in Emacs
       don't use this function, i.e. they silently accept illegal
       characters.  */
    c = '?';

  return c;
}



Gerd Moellmann's avatar
Gerd Moellmann committed
893 894 895 896 897 898 899 900
/* Given a position POS containing a valid character and byte position
   in STRING, return the position NCHARS ahead (NCHARS >= 0).  */

static struct text_pos
string_pos_nchars_ahead (pos, string, nchars)
     struct text_pos pos;
     Lisp_Object string;
     int nchars;
Karl Heuer's avatar
Karl Heuer committed
901
{
Gerd Moellmann's avatar
Gerd Moellmann committed
902 903 904 905 906 907 908 909 910 911
  xassert (STRINGP (string) && nchars >= 0);

  if (STRING_MULTIBYTE (string))
    {
      int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
      unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
      int len;

      while (nchars--)
	{
912
	  string_char_and_length (p, rest, &len);
Gerd Moellmann's avatar
Gerd Moellmann committed
913 914 915 916 917 918 919 920 921 922
	  p += len, rest -= len;
	  xassert (rest >= 0);
	  CHARPOS (pos) += 1;
	  BYTEPOS (pos) += len;
	}
    }
  else
    SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);

  return pos;
923 924 925
}


Gerd Moellmann's avatar
Gerd Moellmann committed
926 927 928 929 930 931
/* Value is the text position, i.e. character and byte position,
   for character position CHARPOS in STRING.  */

static INLINE struct text_pos
string_pos (charpos, string)
     int charpos;
932 933
     Lisp_Object string;
{
Gerd Moellmann's avatar
Gerd Moellmann committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
  struct text_pos pos;
  xassert (STRINGP (string));
  xassert (charpos >= 0);
  SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
  return pos;
}


/* Value is a text position, i.e. character and byte position, for
   character position CHARPOS in C string S.  MULTIBYTE_P non-zero
   means recognize multibyte characters.  */

static struct text_pos
c_string_pos (charpos, s, multibyte_p)
     int charpos;
     unsigned char *s;
     int multibyte_p;
{
  struct text_pos pos;

  xassert (s != NULL);
  xassert (charpos >= 0);

  if (multibyte_p)
958
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
959 960 961 962
      int rest = strlen (s), len;

      SET_TEXT_POS (pos, 0, 0);
      while (charpos--)
963
	{
964
	  string_char_and_length (s, rest, &len);
Gerd Moellmann's avatar
Gerd Moellmann committed
965 966 967 968
	  s += len, rest -= len;
	  xassert (rest >= 0);
	  CHARPOS (pos) += 1;
	  BYTEPOS (pos) += len;
969 970
	}
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
971 972
  else
    SET_TEXT_POS (pos, charpos, charpos);
973

Gerd Moellmann's avatar
Gerd Moellmann committed
974 975
  return pos;
}
976 977


Gerd Moellmann's avatar
Gerd Moellmann committed
978 979
/* Value is the number of characters in C string S.  MULTIBYTE_P
   non-zero means recognize multibyte characters.  */
980

Gerd Moellmann's avatar
Gerd Moellmann committed
981 982 983 984 985 986 987 988 989 990 991
static int
number_of_chars (s, multibyte_p)
     unsigned char *s;
     int multibyte_p;
{
  int nchars;
  
  if (multibyte_p)
    {
      int rest = strlen (s), len;
      unsigned char *p = (unsigned char *) s;
992

Gerd Moellmann's avatar
Gerd Moellmann committed
993 994
      for (nchars = 0; rest > 0; ++nchars)
	{
995
	  string_char_and_length (p, rest, &len);
Gerd Moellmann's avatar
Gerd Moellmann committed
996
	  rest -= len, p += len;
997 998
	}
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
999 1000 1001 1002
  else
    nchars = strlen (s);

  return nchars;
Karl Heuer's avatar
Karl Heuer committed
1003 1004
}

Gerd Moellmann's avatar
Gerd Moellmann committed
1005 1006 1007 1008
     
/* Compute byte position NEWPOS->bytepos corresponding to
   NEWPOS->charpos.  POS is a known position in string STRING.
   NEWPOS->charpos must be >= POS.charpos.  */
1009

Gerd Moellmann's avatar
Gerd Moellmann committed
1010 1011 1012 1013
static void
compute_string_pos (newpos, pos, string)
     struct text_pos *newpos, pos;
     Lisp_Object string;
1014
{
Gerd Moellmann's avatar
Gerd Moellmann committed
1015 1016 1017 1018 1019 1020 1021 1022
  xassert (STRINGP (string));
  xassert (CHARPOS (*newpos) >= CHARPOS (pos));
  
  if (STRING_MULTIBYTE (string))
    *newpos = string_pos_nchars_ahead (pos, CHARPOS (*newpos) - CHARPOS (pos),
				       string);
  else
    BYTEPOS (*newpos) = CHARPOS (*newpos);
1023 1024
}

Jim Blandy's avatar
Jim Blandy committed
1025

Gerd Moellmann's avatar
Gerd Moellmann committed
1026 1027
/* Return the charset of the character at position POS in
   current_buffer.  */
1028

Gerd Moellmann's avatar
Gerd Moellmann committed
1029 1030 1031
static int
charset_at_position (pos)
     struct text_pos pos;
Jim Blandy's avatar
Jim Blandy committed
1032
{
Gerd Moellmann's avatar
Gerd Moellmann committed
1033 1034 1035 1036 1037
  int c, multibyte_p;
  unsigned char *p = BYTE_POS_ADDR (BYTEPOS (pos));

  multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
  if (multibyte_p)
Jim Blandy's avatar
Jim Blandy committed
1038
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
1039 1040 1041
      int maxlen = ((BYTEPOS (pos) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
		    - BYTEPOS (pos));
      int len;
1042
      c = string_char_and_length (p, maxlen, &len);
Jim Blandy's avatar
Jim Blandy committed
1043
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
  else
    c = *p;

  return CHAR_CHARSET (c);
}



/***********************************************************************
			Lisp form evaluation
 ***********************************************************************/

/* Error handler for eval_form.  */

static Lisp_Object
eval_handler (arg)
     Lisp_Object arg;
{
  return Qnil;
}


/* Evaluate SEXPR and return the result, or nil if something went
   wrong.  */

static Lisp_Object
eval_form (sexpr)
     Lisp_Object sexpr;
{
  int count = specpdl_ptr - specpdl;
  Lisp_Object val;
  specbind (Qinhibit_redisplay, Qt);
  val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
  return unbind_to (count, val);
}



/***********************************************************************
			      Debugging
 ***********************************************************************/

#if 0

/* Define CHECK_IT to perform sanity checks on iterators.
   This is for debugging.  It is too slow to do unconditionally.  */

static void
check_it (it)
     struct it *it;
{
  if (it->method == next_element_from_string)
Jim Blandy's avatar
Jim Blandy committed
1096
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
1097 1098 1099 1100 1101 1102 1103 1104
      xassert (STRINGP (it->string));
      xassert (IT_STRING_CHARPOS (*it) >= 0);
    }
  else if (it->method == next_element_from_buffer)
    {
      /* Check that character and byte positions agree.  */
      xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
    }
1105

Gerd Moellmann's avatar
Gerd Moellmann committed
1106 1107 1108 1109 1110
  if (it->dpvec)
    xassert (it->current.dpvec_index >= 0);
  else
    xassert (it->current.dpvec_index < 0);
}
1111

Gerd Moellmann's avatar
Gerd Moellmann committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
#define CHECK_IT(IT)	check_it ((IT))

#else /* not 0 */

#define CHECK_IT(IT)	(void) 0

#endif /* not 0 */


#if GLYPH_DEBUG

/* Check that the window end of window W is what we expect it
   to be---the last row in the current matrix displaying text.  */

static void
check_window_end (w)
     struct window *w;
{
  if (!MINI_WINDOW_P (w)
      && !NILP (w->window_end_valid))
    {
      struct glyph_row *row;
      xassert ((row = MATRIX_ROW (w->current_matrix,
				  XFASTINT (w->window_end_vpos)),
		!row->enabled_p
		|| MATRIX_ROW_DISPLAYS_TEXT_P (row)
		|| MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
    }
}

#define CHECK_WINDOW_END(W)	check_window_end ((W))

#else /* not GLYPH_DEBUG */

#define CHECK_WINDOW_END(W)	(void) 0

#endif /* not GLYPH_DEBUG */



/***********************************************************************
		       Iterator initialization
 ***********************************************************************/

/* Initialize IT for displaying current_buffer in window W, starting
   at character position CHARPOS.  CHARPOS < 0 means that no buffer
   position is specified which is useful when the iterator is assigned
   a position later.  BYTEPOS is the byte position corresponding to
   CHARPOS.  BYTEPOS <= 0 means compute it from CHARPOS.

   If ROW is not null, calls to produce_glyphs with IT as parameter
   will produce glyphs in that row.

   BASE_FACE_ID is the id of a base face to use.  It must be one of
   DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
   TOP_LINE_FACE_ID for displaying mode lines, or TOOLBAR_FACE_ID for
   displaying the toolbar.
   
   If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
   TOP_LINE_FACE_ID, the iterator will be initialized to use the
   corresponding mode line glyph row of the desired matrix of W.  */

void
init_iterator (it, w, charpos, bytepos, row, base_face_id)
     struct it *it;
     struct window *w;
     int charpos, bytepos;
     struct glyph_row *row;
     enum face_id base_face_id;
{
  int highlight_region_p;
  Lisp_Object value;

  /* Some precondition checks.  */
  xassert (w != NULL && it != NULL);
  xassert (charpos < 0 || current_buffer == XBUFFER (w->buffer));
  xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));

  /* If face attributes have been changed since the last redisplay,
     free realized faces now because they depend on face definitions
     that might have changed.  */
  if (face_change_count)
    {
      face_change_count = 0;
      free_all_realized_faces (Qnil);
    }

  /* Use one of the mode line rows of W's desired matrix if
     appropriate.  */
  if (row == NULL)
    {
      if (base_face_id == MODE_LINE_FACE_ID)
	row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
      else if (base_face_id == TOP_LINE_FACE_ID)
	row = MATRIX_TOP_LINE_ROW (w->desired_matrix);
    }
  
  /* Clear IT.  */
  bzero (it, sizeof *it);
  it->current.overlay_string_index = -1;
  it->current.dpvec_index = -1;
  it->charset = CHARSET_ASCII;
  it->base_face_id = base_face_id;

  /* The window in which we iterate over current_buffer:  */
  XSETWINDOW (it->window, w);
  it->w = w;
  it->f = XFRAME (w->frame);

  /* If realized faces have been removed, e.g. because of face
     attribute changes of named faces, recompute them.  */
  if (FRAME_FACE_CACHE (it->f)->used == 0)
    recompute_basic_faces (it->f);

  /* Current value of the `space-width', and 'height' properties.  */
  it->space_width = Qnil;
  it->font_height = Qnil;
  
  /* Are control characters displayed as `^C'?  */
  it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);

  /* -1 means everything between a CR and the following line end
     is invisible.  >0 means lines indented more than this value are
     invisible.  */
  it->selective = (INTEGERP (current_buffer->selective_display)
		   ? XFASTINT (current_buffer->selective_display)
		   : (!NILP (current_buffer->selective_display) 
		      ? -1 : 0));
  it->selective_display_ellipsis_p
    = !NILP (current_buffer->selective_display_ellipses);

  /* Display table to use.  */
  it->dp = window_display_table (w);

  /* Are multibyte characters enabled in current_buffer?  */
  it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);

  /* Non-zero if we should highlight the region.  */
  highlight_region_p
    = (!NILP (Vtransient_mark_mode) 
       && !NILP (current_buffer->mark_active)
       && XMARKER (current_buffer->mark)->buffer != 0);

  /* Set IT->region_beg_charpos and IT->region_end_charpos to the
     start and end of a visible region in window IT->w.  Set both to
     -1 to indicate no region.  */
  if (highlight_region_p
      /* Maybe highlight only in selected window.  */
      && (/* Either show region everywhere.  */ 
	  highlight_nonselected_windows
	  /* Or show region in the selected window.  */
	  || w == XWINDOW (selected_window)
	  /* Or show the region if we are in the mini-buffer and W is
	     the window the mini-buffer refers to.  */
	  || (MINI_WINDOW_P (XWINDOW (selected_window))
	      && w == XWINDOW (Vminibuf_scroll_window))))
    {
      int charpos = marker_position (current_buffer->mark);
      it->region_beg_charpos = min (PT, charpos);
      it->region_end_charpos = max (PT, charpos);
    }
  else
    it->region_beg_charpos = it->region_end_charpos = -1;

  /* Get the position at which the redisplay_end_trigger hook should
     be run, if it is to be run at all.  */
  if (MARKERP (w->redisplay_end_trigger)
      && XMARKER (w->redisplay_end_trigger)->buffer != 0)
    it->redisplay_end_trigger_charpos
      = marker_position (w->redisplay_end_trigger);
  else if (INTEGERP (w->redisplay_end_trigger))
    it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);

  /* Correct bogus values of tab_width.  */
  it->tab_width = XINT (current_buffer->tab_width);
  if (it->tab_width <= 0 || it->tab_width > 1000)
    it->tab_width = 8;

  /* Are lines in the display truncated?  */
  it->truncate_lines_p
    = (base_face_id != DEFAULT_FACE_ID
       || XINT (it->w->hscroll)
       || (truncate_partial_width_windows
	   && !WINDOW_FULL_WIDTH_P (it->w))
       || !NILP (current_buffer->truncate_lines));

  /* Get dimensions of truncation and continuation glyphs.  These are
     displayed as bitmaps under X, so we don't need them for such
     frames.  */
  if (!FRAME_WINDOW_P (it->f))
    {
      if (it->truncate_lines_p)
	{
	  /* We will need the truncation glyph.  */
	  xassert (it->glyph_row == NULL);
	  produce_special_glyphs (it, IT_TRUNCATION);
	  it->truncation_pixel_width = it->pixel_width;
	}
      else
	{
	  /* We will need the continuation glyph.  */
	  xassert (it->glyph_row == NULL);
	  produce_special_glyphs (it, IT_CONTINUATION);
	  it->continuation_pixel_width = it->pixel_width;
	}

      /* Reset these values to zero becaue the produce_special_glyphs
	 above has changed them.  */
      it->pixel_width = it->ascent = it->descent = 0;
1321
      it->phys_ascent = it->phys_descent = 0;
Gerd Moellmann's avatar
Gerd Moellmann committed
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597