xdisp.c 409 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Display generation from window structure and buffer text.
2
   Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
Gerd Moellmann's avatar
Gerd Moellmann committed
3
   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 172
#include <stdio.h>
#include "lisp.h"
173
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
174
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
175 176 177 178
#include "window.h"
#include "termchar.h"
#include "dispextern.h"
#include "buffer.h"
179
#include "charset.h"
Jim Blandy's avatar
Jim Blandy committed
180 181 182 183
#include "indent.h"
#include "commands.h"
#include "macros.h"
#include "disptab.h"
184
#include "termhooks.h"
185
#include "intervals.h"
186 187
#include "coding.h"
#include "process.h"
Andreas Schwab's avatar
Andreas Schwab committed
188
#include "region-cache.h"
Dave Love's avatar
Dave Love committed
189
#include "fontset.h"
Andreas Schwab's avatar
Andreas Schwab committed
190

191
#ifdef HAVE_X_WINDOWS
Andreas Schwab's avatar
Andreas Schwab committed
192 193
#include "xterm.h"
#endif
194 195 196
#ifdef WINDOWSNT
#include "w32term.h"
#endif
197 198 199
#ifdef macintosh
#include "macterm.h"
#endif
Jim Blandy's avatar
Jim Blandy committed
200

Gerd Moellmann's avatar
Gerd Moellmann committed
201 202 203 204 205
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

#define INFINITY 10000000

206
#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207
extern void set_frame_menubar P_ ((struct frame *f, int, int));
208
extern int pending_menu_activation;
209 210
#endif

Jim Blandy's avatar
Jim Blandy committed
211 212 213
extern int interrupt_input;
extern int command_loop_level;

214 215
extern int minibuffer_auto_raise;

216 217
extern Lisp_Object Qface;

218 219
extern Lisp_Object Voverriding_local_map;
extern Lisp_Object Voverriding_local_map_menu_flag;
Gerd Moellmann's avatar
Gerd Moellmann committed
220
extern Lisp_Object Qmenu_item;
221

222
Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223
Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224
Lisp_Object Qredisplay_end_trigger_functions;
225
Lisp_Object Qinhibit_point_motion_hooks;
226
Lisp_Object QCeval, Qwhen, QCfile, QCdata;
Gerd Moellmann's avatar
Gerd Moellmann committed
227 228 229 230 231 232 233
Lisp_Object Qfontified;

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

Lisp_Object Vfontification_functions;
Lisp_Object Qfontification_functions;

Gerd Moellmann's avatar
Gerd Moellmann committed
234
/* Non-zero means draw tool bar buttons raised when the mouse moves
Gerd Moellmann's avatar
Gerd Moellmann committed
235 236
   over them.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
237
int auto_raise_tool_bar_buttons_p;
Gerd Moellmann's avatar
Gerd Moellmann committed
238

Gerd Moellmann's avatar
Gerd Moellmann committed
239
/* Margin around tool bar buttons in pixels.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
240

Gerd Moellmann's avatar
Gerd Moellmann committed
241
int tool_bar_button_margin;
Gerd Moellmann's avatar
Gerd Moellmann committed
242

Gerd Moellmann's avatar
Gerd Moellmann committed
243
/* Thickness of shadow to draw around tool bar buttons.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
244

Gerd Moellmann's avatar
Gerd Moellmann committed
245
int tool_bar_button_relief;
Gerd Moellmann's avatar
Gerd Moellmann committed
246

Gerd Moellmann's avatar
Gerd Moellmann committed
247
/* Non-zero means automatically resize tool-bars so that all tool-bar
Gerd Moellmann's avatar
Gerd Moellmann committed
248 249
   items are visible, and no blank lines remain.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
250
int auto_resize_tool_bars_p;
251

252 253 254 255
/* Non-nil means don't actually do any redisplay.  */

Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;

Gerd Moellmann's avatar
Gerd Moellmann committed
256 257
/* Names of text properties relevant for redisplay.  */

258 259
Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
Gerd Moellmann's avatar
Gerd Moellmann committed
260 261 262 263

/* Symbols used in text property values.  */

Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
264
Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
265
Lisp_Object Qmargin;
266
extern Lisp_Object Qheight;
Gerd Moellmann's avatar
Gerd Moellmann committed
267

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

270
Lisp_Object Vshow_trailing_whitespace;
Gerd Moellmann's avatar
Gerd Moellmann committed
271 272 273 274 275 276 277 278 279 280 281 282

/* 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
283 284 285

int noninteractive_need_newline;

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

288
static int message_log_need_newline;
289

Gerd Moellmann's avatar
Gerd Moellmann committed
290 291 292 293 294

/* 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
295

Gerd Moellmann's avatar
Gerd Moellmann committed
296
static struct text_pos this_line_start_pos;
Jim Blandy's avatar
Jim Blandy committed
297

Gerd Moellmann's avatar
Gerd Moellmann committed
298 299 300 301 302 303
/* 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
304 305

static int this_line_vpos;
Gerd Moellmann's avatar
Gerd Moellmann committed
306 307 308 309 310 311 312
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
313

Gerd Moellmann's avatar
Gerd Moellmann committed
314
/* Buffer that this_line_.* variables are referring to.  */
Jim Blandy's avatar
Jim Blandy committed
315 316 317

static struct buffer *this_line_buffer;

Gerd Moellmann's avatar
Gerd Moellmann committed
318 319
/* Nonzero means truncate lines in all windows less wide than the
   frame.  */
Jim Blandy's avatar
Jim Blandy committed
320 321 322

int truncate_partial_width_windows;

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

325
int unibyte_display_via_language_environment;
Gerd Moellmann's avatar
Gerd Moellmann committed
326 327 328 329
 
/* Nonzero means we have more than one non-mini-buffer-only frame.
   Not guaranteed to be accurate except while parsing
   frame-title-format.  */
330

331 332
int multiple_frames;

Jim Blandy's avatar
Jim Blandy committed
333 334 335
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
336

Jim Blandy's avatar
Jim Blandy committed
337 338
Lisp_Object Voverlay_arrow_position;

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

Jim Blandy's avatar
Jim Blandy committed
341 342
Lisp_Object Voverlay_arrow_string;

Gerd Moellmann's avatar
Gerd Moellmann committed
343 344 345 346
/* Values of those variables at last redisplay.  However, if
   Voverlay_arrow_position is a marker, last_arrow_position is its
   numerical position.  */

347 348
static Lisp_Object last_arrow_position, last_arrow_string;

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

351 352
Lisp_Object Vframe_title_format;

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

355 356
Lisp_Object Vicon_title_format;

357 358 359
/* 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
360

361 362
static Lisp_Object Vwindow_size_change_functions;

363
Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
364

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

Gerd Moellmann's avatar
Gerd Moellmann committed
367
static int overlay_arrow_seen;
Karl Heuer's avatar
Karl Heuer committed
368

369 370
/* Nonzero means highlight the region even in nonselected windows.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
371 372 373 374 375
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.  */

376
static int scroll_step;
Jim Blandy's avatar
Jim Blandy committed
377

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

381 382
static int scroll_conservatively;

Gerd Moellmann's avatar
Gerd Moellmann committed
383 384 385 386 387
/* 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.  */

388 389
int scroll_margin;

Gerd Moellmann's avatar
Gerd Moellmann committed
390 391 392
/* 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
393 394 395

int buffer_shared;

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

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

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

int mode_line_inverse_video;

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

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

Gerd Moellmann's avatar
Gerd Moellmann committed
408 409 410
/* 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
411
int minibuf_prompt_width;
Gerd Moellmann's avatar
Gerd Moellmann committed
412 413 414 415 416 417
int minibuf_prompt_pixel_width;

/* 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.  */

418 419
Lisp_Object echo_area_window;

420 421 422 423 424 425 426
/* List of pairs (MESSAGE . MULTIBYTE).  The function save_message
   pushes the current message and the value of
   message_enable_multibyte on the stack, the function restore_message
   pops the stack and displays MESSAGE again.  */

Lisp_Object Vmessage_stack;

427 428
/* Nonzero means multibyte characters were enabled when the echo area
   message was specified.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
429

430 431
int message_enable_multibyte;

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

Jim Blandy's avatar
Jim Blandy committed
434 435
int update_mode_lines;

Gerd Moellmann's avatar
Gerd Moellmann committed
436 437 438
/* Nonzero if window sizes or contents have changed since last
   redisplay that finished */

Jim Blandy's avatar
Jim Blandy committed
439 440
int windows_or_buffers_changed;

Gerd Moellmann's avatar
Gerd Moellmann committed
441 442 443
/* Nonzero after display_mode_line if %l was used and it displayed a
   line number.  */

444 445 446
int line_number_displayed;

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

448
Lisp_Object Vline_number_display_limit;
Karl Heuer's avatar
Karl Heuer committed
449

450 451 452 453
/* line width to consider when repostioning for line number display */

static int line_number_display_limit_width;

Gerd Moellmann's avatar
Gerd Moellmann committed
454 455 456
/* 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
457
Lisp_Object Vmessage_log_max;
458

459 460 461 462
/* The name of the *Messages* buffer, a string.  */

static Lisp_Object Vmessages_buffer_name;

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
/* Current, index 0, and last displayed echo area message.  Either
   buffers from echo_buffers, or nil to indicate no message.  */

Lisp_Object echo_area_buffer[2];

/* The buffers referenced from echo_area_buffer.  */

static Lisp_Object echo_buffer[2];

/* A vector saved used in with_area_buffer to reduce consing.  */

static Lisp_Object Vwith_echo_area_save_vector;

/* Non-zero means display_echo_area should display the last echo area
   message again.  Set by redisplay_preserve_echo_area.  */

static int display_last_displayed_message_p;

/* Nonzero if echo area is being used by print; zero if being used by
   message.  */

int message_buf_print;

486 487 488
/* Maximum height for resizing mini-windows.  Either a float
   specifying a fraction of the available height, or an integer
   specifying a number of lines.  */
489

490 491 492 493 494 495 496
Lisp_Object Vmax_mini_window_height;

/* Non-zero means messages should be displayed with truncated
   lines instead of being continued.  */

int message_truncate_lines;
Lisp_Object Qmessage_truncate_lines;
497

498 499 500 501 502
/* Non-zero means we want a hollow cursor in windows that are not
   selected.  Zero means there's no cursor in such windows.  */

int cursor_in_non_selected_windows;

Gerd Moellmann's avatar
Gerd Moellmann committed
503 504
/* A scratch glyph row with contents used for generating truncation
   glyphs.  Also used in direct_output_for_insert.  */
505

Gerd Moellmann's avatar
Gerd Moellmann committed
506 507 508
#define MAX_SCRATCH_GLYPHS 100
struct glyph_row scratch_glyph_row;
static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
509

Gerd Moellmann's avatar
Gerd Moellmann committed
510 511 512 513
/* Ascent and height of the last line processed by move_it_to.  */

static int last_max_ascent, last_height;

514 515 516 517
/* Non-zero if there's a help-echo in the echo area.  */

int help_echo_showing_p;

Gerd Moellmann's avatar
Gerd Moellmann committed
518 519 520 521 522 523 524 525
/* 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

526 527
#if GLYPH_DEBUG

Gerd Moellmann's avatar
Gerd Moellmann committed
528 529 530 531
/* Non-zero means print traces of redisplay if compiled with
   GLYPH_DEBUG != 0.  */

int trace_redisplay_p;
532

533
#endif /* GLYPH_DEBUG */
534

535 536
#ifdef DEBUG_TRACE_MOVE
/* Non-zero means trace with TRACE_MOVE to stderr.  */
537 538 539 540
int trace_move;

#define TRACE_MOVE(x)	if (trace_move) fprintf x; else (void) 0
#else
541
#define TRACE_MOVE(x)	(void) 0
Gerd Moellmann's avatar
Gerd Moellmann committed
542
#endif
543
 
544 545 546 547 548
/* Non-zero means automatically scroll windows horizontally to make
   point visible.  */

int automatic_hscrolling_p;

549 550 551 552
/* A list of symbols, one for each supported image type.  */

Lisp_Object Vimage_types;

Gerd Moellmann's avatar
Gerd Moellmann committed
553 554 555
/* Value returned from text property handlers (see below).  */

enum prop_handled
556
{
Gerd Moellmann's avatar
Gerd Moellmann committed
557 558 559 560 561
  HANDLED_NORMALLY,
  HANDLED_RECOMPUTE_PROPS,
  HANDLED_OVERLAY_STRING_CONSUMED,
  HANDLED_RETURN
};
562

Gerd Moellmann's avatar
Gerd Moellmann committed
563 564
/* A description of text properties that redisplay is interested
   in.  */
565

Gerd Moellmann's avatar
Gerd Moellmann committed
566 567 568 569
struct props
{
  /* The name of the property.  */
  Lisp_Object *name;
570

Gerd Moellmann's avatar
Gerd Moellmann committed
571 572 573 574 575 576 577 578 579 580 581
  /* 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 *));
582
static enum prop_handled handle_composition_prop P_ ((struct it *));
Gerd Moellmann's avatar
Gerd Moellmann committed
583 584 585 586 587 588
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
589
{
Gerd Moellmann's avatar
Gerd Moellmann committed
590 591 592 593 594 595
  {&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},
596
  {&Qcomposition,	COMPOSITION_PROP_IDX,	handle_composition_prop},
Gerd Moellmann's avatar
Gerd Moellmann committed
597 598
  {NULL,		0,			NULL}
};
Karl Heuer's avatar
Karl Heuer committed
599

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

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

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

Gerd Moellmann's avatar
Gerd Moellmann committed
607 608 609 610
enum move_it_result
{
  /* Not used.  Undefined value.  */
  MOVE_UNDEFINED,
611

Gerd Moellmann's avatar
Gerd Moellmann committed
612 613
  /* Move ended at the requested buffer position or ZV.  */
  MOVE_POS_MATCH_OR_ZV,
614

Gerd Moellmann's avatar
Gerd Moellmann committed
615 616
  /* Move ended at the requested X pixel position.  */
  MOVE_X_REACHED,
617

Gerd Moellmann's avatar
Gerd Moellmann committed
618 619 620 621 622 623 624
  /* 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,
625

Gerd Moellmann's avatar
Gerd Moellmann committed
626 627 628
  /* Move within a line ended at a line end.  */
  MOVE_NEWLINE_OR_CR
};
629

630

Gerd Moellmann's avatar
Gerd Moellmann committed
631 632 633

/* Function prototypes.  */

Gerd Moellmann's avatar
Gerd Moellmann committed
634
static int cursor_row_p P_ ((struct window *, struct glyph_row *));
635
static int redisplay_mode_lines P_ ((Lisp_Object, int));
636 637 638 639 640 641 642 643 644 645 646 647 648
static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
static int invisible_text_between_p P_ ((struct it *, int, int));
static int next_element_from_ellipsis P_ ((struct it *));
static void pint2str P_ ((char *, int, int));
static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
							struct text_pos));
static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
static void store_frame_title_char P_ ((char));
static int store_frame_title P_ ((unsigned char *, int, int));
static void x_consider_frame_title P_ ((Lisp_Object));
static void handle_stop P_ ((struct it *));
static int tool_bar_lines_needed P_ ((struct frame *));
649
static int single_display_prop_intangible_p P_ ((Lisp_Object));
650
static void ensure_echo_area_buffers P_ ((void));
Gerd Moellmann's avatar
Gerd Moellmann committed
651 652 653
static struct glyph_row *row_containing_pos P_ ((struct window *, int,
						 struct glyph_row *,
						 struct glyph_row *));
654 655
static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
656
static int with_echo_area_buffer P_ ((struct window *, int,
657 658
				      int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
				      EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
659
static void clear_garbaged_frames P_ ((void));
660 661 662
static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
663
static int display_echo_area P_ ((struct window *));
664 665
static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
666
static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
667
static int string_char_and_length P_ ((unsigned char *, int, int *));
Gerd Moellmann's avatar
Gerd Moellmann committed
668 669 670
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 *));
671
static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
Gerd Moellmann's avatar
Gerd Moellmann committed
672 673 674
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 *));
675
static int append_space P_ ((struct it *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
676 677
static void make_cursor_line_fully_visible P_ ((struct window *));
static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
678
static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
Gerd Moellmann's avatar
Gerd Moellmann committed
679 680 681 682 683 684 685 686
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));
687
static int echo_area_display P_ ((int));
Gerd Moellmann's avatar
Gerd Moellmann committed
688 689 690 691 692 693
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 *));
694
static int display_mode_lines P_ ((struct window *));
Gerd Moellmann's avatar
Gerd Moellmann committed
695 696 697
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));
698
static char *decode_mode_spec P_ ((struct window *, int, int, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
699 700 701 702 703 704 705 706 707 708 709 710
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 *));
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 *));
711
static void reseat_at_next_visible_line_start P_ ((struct it *, int));
Gerd Moellmann's avatar
Gerd Moellmann committed
712 713 714 715
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 *));
716
static int next_element_from_composition P_ ((struct it *));
Gerd Moellmann's avatar
Gerd Moellmann committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
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 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 *));
Gerd Moellmann's avatar
Gerd Moellmann committed
732
static int forward_to_next_line_start P_ ((struct it *, int *));
Gerd Moellmann's avatar
Gerd Moellmann committed
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
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)
748

Gerd Moellmann's avatar
Gerd Moellmann committed
749
#ifdef HAVE_WINDOW_SYSTEM
750

Gerd Moellmann's avatar
Gerd Moellmann committed
751 752 753 754
static void update_tool_bar P_ ((struct frame *, int));
static void build_desired_tool_bar_string P_ ((struct frame *f));
static int redisplay_tool_bar P_ ((struct frame *));
static void display_tool_bar_line P_ ((struct it *));
755

Gerd Moellmann's avatar
Gerd Moellmann committed
756
#endif /* HAVE_WINDOW_SYSTEM */
757

Gerd Moellmann's avatar
Gerd Moellmann committed
758 759 760 761

/***********************************************************************
		      Window display dimensions
 ***********************************************************************/
762

Gerd Moellmann's avatar
Gerd Moellmann committed
763 764 765 766 767 768 769 770 771 772
/* 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);
773

Gerd Moellmann's avatar
Gerd Moellmann committed
774 775 776
  if (WINDOW_WANTS_MODELINE_P (w))
    height -= CURRENT_MODE_LINE_HEIGHT (w);
  return height;
777 778
}

779

Gerd Moellmann's avatar
Gerd Moellmann committed
780 781 782
/* 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.  */
783

Gerd Moellmann's avatar
Gerd Moellmann committed
784 785 786 787 788 789 790 791 792
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)
793
    {
794
      width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
Gerd Moellmann's avatar
Gerd Moellmann committed
795 796 797 798 799 800 801 802 803 804 805 806 807 808
      
      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);
809
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
810 811

  return width * CANON_X_UNIT (f);
812
}
813 814


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

Gerd Moellmann's avatar
Gerd Moellmann committed
818 819 820
INLINE int
window_box_height (w)
     struct window *w;
821
{
Gerd Moellmann's avatar
Gerd Moellmann committed
822 823
  struct frame *f = XFRAME (w->frame);
  int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
824 825

  xassert (height >= 0);
Gerd Moellmann's avatar
Gerd Moellmann committed
826 827 828 829
  
  if (WINDOW_WANTS_MODELINE_P (w))
    height -= CURRENT_MODE_LINE_HEIGHT (w);

Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
830 831
  if (WINDOW_WANTS_HEADER_LINE_P (w))
    height -= CURRENT_HEADER_LINE_HEIGHT (w);
Gerd Moellmann's avatar
Gerd Moellmann committed
832 833

  return height;
Karl Heuer's avatar
Karl Heuer committed
834 835 836
}


Gerd Moellmann's avatar
Gerd Moellmann committed
837 838 839 840
/* 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
841

Gerd Moellmann's avatar
Gerd Moellmann committed
842 843 844 845
INLINE int
window_box_left (w, area)
     struct window *w;
     int area;
846
{
Gerd Moellmann's avatar
Gerd Moellmann committed
847 848
  struct frame *f = XFRAME (w->frame);
  int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
849

Gerd Moellmann's avatar
Gerd Moellmann committed
850
  if (!w->pseudo_window_p)
851
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
852
      x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
853
	    + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
Gerd Moellmann's avatar
Gerd Moellmann committed
854 855 856 857 858 859
      
      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));
860
    }
861

Gerd Moellmann's avatar
Gerd Moellmann committed
862 863
  return x;
}     
864

865

Gerd Moellmann's avatar
Gerd Moellmann committed
866 867 868 869
/* 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.  */
870

Gerd Moellmann's avatar
Gerd Moellmann committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
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));
Gerd Moellmann's avatar
Change  
Gerd Moellmann committed
900 901
  if (WINDOW_WANTS_HEADER_LINE_P (w))
    *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
902
}
903 904


Gerd Moellmann's avatar
Gerd Moellmann committed
905 906 907 908 909 910 911
/* 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.  */
912

Gerd Moellmann's avatar
Gerd Moellmann committed
913 914 915 916 917 918
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;
919
{
Gerd Moellmann's avatar
Gerd Moellmann committed
920 921 922 923
  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;
924 925
}

Gerd Moellmann's avatar
Gerd Moellmann committed
926 927 928 929 930 931

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

932 933 934
/* 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
935
   we find one, we return a `?', but with the length of the invalid
936 937 938
   character.  */

static INLINE int
939
string_char_and_length (str, maxlen, len)
940
     unsigned char *str;
941
     int maxlen, *len;
942 943 944 945 946 947
{
  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
948
       don't use this function, i.e. they silently accept invalid
949 950 951 952 953 954 955 956
       characters.  */
    c = '?';

  return c;
}



Gerd Moellmann's avatar
Gerd Moellmann committed
957 958 959 960 961 962 963 964
/* 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
965
{
Gerd Moellmann's avatar
Gerd Moellmann committed
966 967 968 969 970 971 972 973 974 975
  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--)
	{
976
	  string_char_and_length (p, rest, &len);
Gerd Moellmann's avatar
Gerd Moellmann committed
977 978 979 980 981 982 983 984 985 986
	  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;
987 988 989
}


Gerd Moellmann's avatar
Gerd Moellmann committed
990 991 992 993 994 995
/* 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;
996 997
     Lisp_Object string;
{
Gerd Moellmann's avatar
Gerd Moellmann committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
  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)
1022
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
1023 1024 1025 1026
      int rest = strlen (s), len;

      SET_TEXT_POS (pos, 0, 0);
      while (charpos--)
1027
	{
1028
	  string_char_and_length (s, rest, &len);
Gerd Moellmann's avatar
Gerd Moellmann committed
1029 1030 1031 1032
	  s += len, rest -= len;
	  xassert (rest >= 0);
	  CHARPOS (pos) += 1;
	  BYTEPOS (pos) += len;
1033 1034
	}
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
1035 1036
  else
    SET_TEXT_POS (pos, charpos, charpos);
1037

Gerd Moellmann's avatar
Gerd Moellmann committed
1038 1039
  return pos;
}
1040 1041


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

Gerd Moellmann's avatar
Gerd Moellmann committed
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
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;
1056

Gerd Moellmann's avatar
Gerd Moellmann committed
1057 1058
      for (nchars = 0; rest > 0; ++nchars)
	{
1059
	  string_char_and_length (p, rest, &len);
Gerd Moellmann's avatar
Gerd Moellmann committed
1060
	  rest -= len, p += len;
1061 1062
	}
    }
Gerd Moellmann's avatar
Gerd Moellmann committed
1063 1064 1065 1066
  else
    nchars = strlen (s);

  return nchars;
Karl Heuer's avatar
Karl Heuer committed
1067 1068
}

Gerd Moellmann's avatar
Gerd Moellmann committed
1069 1070 1071 1072
     
/* Compute byte position NEWPOS->bytepos corresponding to
   NEWPOS->charpos.  POS is a known position in string STRING.
   NEWPOS->charpos must be >= POS.charpos.  */
1073

Gerd Moellmann's avatar
Gerd Moellmann committed
1074 1075 1076 1077
static void
compute_string_pos (newpos, pos, string)
     struct text_pos *newpos, pos;
     Lisp_Object string;
1078
{
Gerd Moellmann's avatar
Gerd Moellmann committed
1079 1080 1081 1082
  xassert (STRINGP (string));
  xassert (CHARPOS (*newpos) >= CHARPOS (pos));
  
  if (STRING_MULTIBYTE (string))
1083 1084
    *newpos = string_pos_nchars_ahead (pos, string,
				       CHARPOS (*newpos) - CHARPOS (pos));
Gerd Moellmann's avatar
Gerd Moellmann committed
1085 1086
  else
    BYTEPOS (*newpos) = CHARPOS (*newpos);
1087 1088
}

Jim Blandy's avatar
Jim Blandy committed
1089

Gerd Moellmann's avatar
Gerd Moellmann committed
1090 1091 1092 1093 1094

/***********************************************************************
			Lisp form evaluation
 ***********************************************************************/

1095
/* Error handler for safe_eval and safe_call.  */
Gerd Moellmann's avatar
Gerd Moellmann committed
1096 1097

static Lisp_Object
1098
safe_eval_handler (arg)
Gerd Moellmann's avatar
Gerd Moellmann committed
1099 1100 1101 1102 1103 1104 1105 1106 1107
     Lisp_Object arg;
{
  return Qnil;
}


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

1108
Lisp_Object
1109
safe_eval (sexpr)
Gerd Moellmann's avatar
Gerd Moellmann committed
1110 1111 1112
     Lisp_Object sexpr;
{
  int count = specpdl_ptr - specpdl;
1113
  struct gcpro gcpro1;
Gerd Moellmann's avatar
Gerd Moellmann committed
1114
  Lisp_Object val;
1115 1116

  GCPRO1 (sexpr);
Gerd Moellmann's avatar
Gerd Moellmann committed
1117
  specbind (Qinhibit_redisplay, Qt);
1118
  val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1119 1120 1121 1122 1123 1124 1125 1126 1127
  UNGCPRO;
  return unbind_to (count, val);
}


/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
   Return the result, or nil if something went wrong.  */

Lisp_Object
1128
safe_call (nargs, args)
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
     int nargs;
     Lisp_Object *args;
{
  int count = specpdl_ptr - specpdl;
  Lisp_Object val;
  struct gcpro gcpro1;

  GCPRO1 (args[0]);
  gcpro1.nvars = nargs;
  specbind (Qinhibit_redisplay, Qt);
  val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1140
				   safe_eval_handler);
1141
  UNGCPRO;
Gerd Moellmann's avatar
Gerd Moellmann committed
1142 1143 1144 1145
  return unbind_to (count, val);
}


1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
/* Call function FN with one argument ARG.
   Return the result, or nil if something went wrong.  */

Lisp_Object
safe_call1 (fn, arg)
     Lisp_Object fn, arg;
{
  Lisp_Object args[2];
  args[0] = fn;
  args[1] = arg;
  return safe_call (2, args);
}


Gerd Moellmann's avatar
Gerd Moellmann committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

/***********************************************************************
			      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
1175
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
1176 1177 1178 1179 1180 1181 1182 1183
      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)));
    }