indent.c 54 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Indentation functions.
Richard M. Stallman's avatar
Richard M. Stallman committed
2
   Copyright (C) 1985,86,87,88,93,94,95,98 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
3 4 5 6 7

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
Karl Heuer's avatar
Karl Heuer committed
8
the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
9 10 11 12 13 14 15 16 17
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
18 19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Jim Blandy's avatar
Jim Blandy committed
20 21


22
#include <config.h>
Jim Blandy's avatar
Jim Blandy committed
23 24
#include "lisp.h"
#include "buffer.h"
25
#include "charset.h"
26
#include "category.h"
Jim Blandy's avatar
Jim Blandy committed
27
#include "indent.h"
Jim Blandy's avatar
Jim Blandy committed
28
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
29 30 31 32
#include "window.h"
#include "termchar.h"
#include "termopts.h"
#include "disptab.h"
33
#include "intervals.h"
34
#include "region-cache.h"
Jim Blandy's avatar
Jim Blandy committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

/* Indentation can insert tabs if this is non-zero;
   otherwise always uses spaces */
int indent_tabs_mode;

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

#define CR 015

/* These three values memoize the current column to avoid recalculation */
/* Some things in set last_known_column_point to -1
  to mark the memoized value as invalid */
/* Last value returned by current_column */
int last_known_column;
/* Value of point when current_column was called */
int last_known_column_point;
/* Value of MODIFF when current_column was called */
int last_known_column_modified;

55
static int current_column_1 ();
56
static int position_indentation ();
57

58 59 60 61
/* Cache of beginning of line found by the last call of
   current_column. */
int current_column_bol_cache;

Jim Blandy's avatar
Jim Blandy committed
62 63
/* Get the display table to use for the current buffer.  */

64
struct Lisp_Char_Table *
Jim Blandy's avatar
Jim Blandy committed
65 66 67 68 69
buffer_display_table ()
{
  Lisp_Object thisbuf;

  thisbuf = current_buffer->display_table;
70 71 72 73
  if (DISP_TABLE_P (thisbuf))
    return XCHAR_TABLE (thisbuf);
  if (DISP_TABLE_P (Vstandard_display_table))
    return XCHAR_TABLE (Vstandard_display_table);
Jim Blandy's avatar
Jim Blandy committed
74 75
  return 0;
}
76 77 78 79

/* Width run cache considerations.  */

/* Return the width of character C under display table DP.  */
80

81 82 83
static int
character_width (c, dp)
     int c;
84
     struct Lisp_Char_Table *dp;
85 86 87 88 89 90
{
  Lisp_Object elt;

  /* These width computations were determined by examining the cases
     in display_text_line.  */

91
  /* Everything can be handled by the display table, if it's
92
     present and the element is right.  */
93
  if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt)))
94 95
    return XVECTOR (elt)->size;

96 97 98 99 100
  /* Some characters are special.  */
  if (c == '\n' || c == '\t' || c == '\015')
    return 0;

  /* Printing characters have width 1.  */
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  else if (c >= 040 && c < 0177)
    return 1;

  /* Everybody else (control characters, metacharacters) has other
     widths.  We could return their actual widths here, but they
     depend on things like ctl_arrow and crud like that, and they're
     not very common at all.  So we'll just claim we don't know their
     widths.  */
  else
    return 0;
}

/* Return true iff the display table DISPTAB specifies the same widths
   for characters as WIDTHTAB.  We use this to decide when to
   invalidate the buffer's width_run_cache.  */
int
disptab_matches_widthtab (disptab, widthtab)
118
     struct Lisp_Char_Table *disptab;
119 120 121 122 123 124 125 126 127 128 129 130 131
     struct Lisp_Vector *widthtab;
{
  int i;

  if (widthtab->size != 256)
    abort ();

  for (i = 0; i < 256; i++)
    if (character_width (i, disptab)
        != XFASTINT (widthtab->contents[i]))
      return 0;

  return 1;
132
}
133 134 135 136 137

/* Recompute BUF's width table, using the display table DISPTAB.  */
void
recompute_width_table (buf, disptab)
     struct buffer *buf;
138
     struct Lisp_Char_Table *disptab;
139 140
{
  int i;
141
  struct Lisp_Vector *widthtab;
142

143 144 145
  if (!VECTORP (buf->width_table))
    buf->width_table = Fmake_vector (make_number (256), make_number (0));
  widthtab = XVECTOR (buf->width_table);
146 147 148 149
  if (widthtab->size != 256)
    abort ();

  for (i = 0; i < 256; i++)
150
    XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
151 152 153 154 155 156 157
}

/* Allocate or free the width run cache, as requested by the current
   state of current_buffer's cache_long_line_scans variable.  */
static void
width_run_cache_on_off ()
{
158 159 160 161
  if (NILP (current_buffer->cache_long_line_scans)
      /* And, for the moment, this feature doesn't work on multibyte
         characters.  */
      || !NILP (current_buffer->enable_multibyte_characters))
162 163 164 165 166 167 168 169 170 171 172 173 174
    {
      /* It should be off.  */
      if (current_buffer->width_run_cache)
        {
          free_region_cache (current_buffer->width_run_cache);
          current_buffer->width_run_cache = 0;
          current_buffer->width_table = Qnil;
        }
    }
  else
    {
      /* It should be on.  */
      if (current_buffer->width_run_cache == 0)
175
        {
176 177 178 179 180 181
          current_buffer->width_run_cache = new_region_cache ();
          recompute_width_table (current_buffer, buffer_display_table ());
        }
    }
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

/* Skip some invisible characters starting from POS.
   This includes characters invisible because of text properties
   and characters invisible because of overlays.

   If position POS is followed by invisible characters,
   skip some of them and return the position after them.
   Otherwise return POS itself.

   Set *NEXT_BOUNDARY_P to the next position at which
   it will be necessary to call this function again.

   Don't scan past TO, and don't set *NEXT_BOUNDARY_P
   to a value greater than TO.

   If WINDOW is non-nil, and this buffer is displayed in WINDOW,
   take account of overlays that apply only in WINDOW.

   We don't necessarily skip all the invisible characters after POS
   because that could take a long time.  We skip a reasonable number
   which can be skipped quickly.  If there might be more invisible
   characters immediately following, then *NEXT_BOUNDARY_P
   will equal the return value.  */

206
int
207 208 209 210 211 212
skip_invisible (pos, next_boundary_p, to, window)
     int pos;
     int *next_boundary_p;
     int to;
     Lisp_Object window;
{
213
  Lisp_Object prop, position, overlay_limit, proplimit;
214
  Lisp_Object buffer;
215
  int end;
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

  XSETFASTINT (position, pos);
  XSETBUFFER (buffer, current_buffer);

  /* Give faster response for overlay lookup near POS.  */
  recenter_overlay_lists (current_buffer, pos);

  /* We must not advance farther than the next overlay change.
     The overlay change might change the invisible property;
     or there might be overlay strings to be displayed there.  */
  overlay_limit = Fnext_overlay_change (position);
  /* As for text properties, this gives a lower bound
     for where the invisible text property could change.  */
  proplimit = Fnext_property_change (position, buffer, Qt);
  if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
    proplimit = overlay_limit;
  /* PROPLIMIT is now a lower bound for the next change
     in invisible status.  If that is plenty far away,
     use that lower bound.  */
  if (XFASTINT (proplimit) > pos + 100 || XFASTINT (proplimit) >= to)
    *next_boundary_p = XFASTINT (proplimit);
  /* Otherwise, scan for the next `invisible' property change.  */
  else
    {
      /* Don't scan terribly far.  */
      XSETFASTINT (proplimit, min (pos + 100, to));
      /* No matter what. don't go past next overlay change.  */
      if (XFASTINT (overlay_limit) < XFASTINT (proplimit))
	proplimit = overlay_limit;
245 246
      end = XFASTINT (Fnext_single_property_change (position, Qinvisible,
						    buffer, proplimit));
247
#if 0
248 249 250 251 252 253 254
      /* Don't put the boundary in the middle of multibyte form if
         there is no actual property change.  */
      if (end == pos + 100
	  && !NILP (current_buffer->enable_multibyte_characters)
	  && end < ZV)
	while (pos < end && !CHAR_HEAD_P (POS_ADDR (end)))
	  end--;
255
#endif
256
      *next_boundary_p = end;
257 258 259 260 261 262 263 264 265 266 267
    }
  /* if the `invisible' property is set, we can skip to
     the next property change */
  if (!NILP (window) && EQ (XWINDOW (window)->buffer, buffer))
    prop = Fget_char_property (position, Qinvisible, window);
  else
    prop = Fget_char_property (position, Qinvisible, buffer);
  if (TEXT_PROP_MEANS_INVISIBLE (prop))
    return *next_boundary_p;
  return pos;
}
Jim Blandy's avatar
Jim Blandy committed
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/* Set variables WIDTH and BYTES for a multibyte sequence starting at P.

   C is *P which should satisfy `BASE_LEADING_CODE_P (c)'.

   DP is a display table or NULL.

   This macro is used in current_column_1, Fmove_to_column, and
   compute_motion.  */

#define MULTIBYTE_BYTES_WIDTH(p, c, dp)					\
  do {									\
    unsigned char *pend = p + 1;					\
    									\
    wide_column = 0;							\
    while (! CHAR_HEAD_P (*pend)) pend++;				\
    									\
    if (c == LEADING_CODE_COMPOSITION)					\
      {									\
	int id = str_cmpchar_id (p, pend - p);				\
	int ch = MAKE_COMPOSITE_CHAR (id);				\
									\
	if (id >= 0)							\
	  {								\
	    bytes = cmpchar_table[id]->len;				\
	    if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, ch)))		\
	      width = XVECTOR (DISP_CHAR_VECTOR (dp, ch))->size;	\
	    else							\
296 297 298
	      width = cmpchar_table[id]->width;				\
	    if (width > 1)						\
	      wide_column = width;					\
299 300 301 302 303 304 305 306 307 308 309 310
	  }								\
	else								\
	  {								\
	    bytes = 1;							\
	    width = 4;							\
	  }								\
      }									\
    else								\
      {									\
	bytes = BYTES_BY_CHAR_HEAD (c);					\
	if (bytes >= 2 && bytes <= pend - p)				\
	  {								\
311
	    int ch = STRING_CHAR (p, bytes);				\
312
	    								\
313 314 315 316 317 318 319
	    if (CHAR_VALID_P (ch, 0))					\
	      {								\
		if (dp && VECTORP (DISP_CHAR_VECTOR (dp, ch)))		\
		  width = XVECTOR (DISP_CHAR_VECTOR (dp, ch))->size;	\
		else							\
		  width = WIDTH_BY_CHAR_HEAD (c);			\
	      }								\
320
	    else							\
321
	      width = bytes * 4;					\
322 323
	    if (width > 1)						\
	      wide_column = width;					\
324 325 326 327 328 329 330 331 332 333 334 335 336 337
	  }								\
	else								\
	  {								\
	    bytes = 1;							\
	    width = 4;							\
	  }								\
      }									\
    if (p + bytes < pend)						\
      {									\
	width += 4 * (pend - (p + bytes));				\
	bytes = pend - p;						\
      }									\
  } while (0)

Jim Blandy's avatar
Jim Blandy committed
338 339 340 341 342 343
DEFUN ("current-column", Fcurrent_column, Scurrent_column, 0, 0, 0,
  "Return the horizontal position of point.  Beginning of line is column 0.\n\
This is calculated by adding together the widths of all the displayed\n\
representations of the character between the start of the previous line\n\
and point.  (eg control characters will have a width of 2 or 4, tabs\n\
will have a variable width)\n\
Jim Blandy's avatar
Jim Blandy committed
344 345
Ignores finite width of frame, which means that this function may return\n\
values greater than (frame-width).\n\
Jim Blandy's avatar
Jim Blandy committed
346 347 348 349 350
Whether the line is visible (if `selective-display' is t) has no effect;\n\
however, ^M is treated as end of line when `selective-display' is t.")
  ()
{
  Lisp_Object temp;
351
  XSETFASTINT (temp, current_column ());
Jim Blandy's avatar
Jim Blandy committed
352 353 354
  return temp;
}

Jim Blandy's avatar
Jim Blandy committed
355 356
/* Cancel any recorded value of the horizontal position.  */

357
void
Jim Blandy's avatar
Jim Blandy committed
358 359 360 361 362
invalidate_current_column ()
{
  last_known_column_point = 0;
}

Jim Blandy's avatar
Jim Blandy committed
363 364 365 366 367 368 369 370 371
int
current_column ()
{
  register int col;
  register unsigned char *ptr, *stop;
  register int tab_seen;
  int post_tab;
  register int c;
  register int tab_width = XINT (current_buffer->tab_width);
Jim Blandy's avatar
Jim Blandy committed
372
  int ctl_arrow = !NILP (current_buffer->ctl_arrow);
373
  register struct Lisp_Char_Table *dp = buffer_display_table ();
Jim Blandy's avatar
Jim Blandy committed
374 375
  int stopchar;

376
  if (PT == last_known_column_point
Jim Blandy's avatar
Jim Blandy committed
377 378 379
      && MODIFF == last_known_column_modified)
    return last_known_column;

380 381
  /* If the buffer has overlays, text properties,
     or multibyte characters, use a more general algorithm.  */
382 383
  if (BUF_INTERVALS (current_buffer)
      || !NILP (current_buffer->overlays_before)
384
      || !NILP (current_buffer->overlays_after)
385 386
      || Z != Z_BYTE)
    return current_column_1 ();
387 388 389 390

  /* Scan backwards from point to the previous newline,
     counting width.  Tab characters are the only complicated case.  */

Jim Blandy's avatar
Jim Blandy committed
391
  /* Make a pointer for decrementing through the chars before point.  */
392
  ptr = BYTE_POS_ADDR (PT_BYTE - 1) + 1;
Jim Blandy's avatar
Jim Blandy committed
393 394
  /* Make a pointer to where consecutive chars leave off,
     going backwards from point.  */
395
  if (PT == BEGV)
Jim Blandy's avatar
Jim Blandy committed
396
    stop = ptr;
397
  else if (PT <= GPT || BEGV > GPT)
Jim Blandy's avatar
Jim Blandy committed
398 399 400 401
    stop = BEGV_ADDR;
  else
    stop = GAP_END_ADDR;

402
  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
Jim Blandy's avatar
Jim Blandy committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

  col = 0, tab_seen = 0, post_tab = 0;

  while (1)
    {
      if (ptr == stop)
	{
	  /* We stopped either for the beginning of the buffer
	     or for the gap.  */
	  if (ptr == BEGV_ADDR)
	    break;
	  /* It was the gap.  Jump back over it.  */
	  stop = BEGV_ADDR;
	  ptr = GPT_ADDR;
	  /* Check whether that brings us to beginning of buffer.  */
	  if (BEGV >= GPT) break;
	}

      c = *--ptr;
422 423 424 425
      if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
	col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
      else if (c >= 040 && c < 0177)
	col++;
426 427 428 429 430 431
      else if (c == '\n'
	       || (c == '\r' && EQ (current_buffer->selective_display, Qt)))
	{
	  ptr++;
	  break;
	}
Jim Blandy's avatar
Jim Blandy committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
      else if (c == '\t')
	{
	  if (tab_seen)
	    col = ((col + tab_width) / tab_width) * tab_width;

	  post_tab += col;
	  col = 0;
	  tab_seen = 1;
	}
      else
	col += (ctl_arrow && c < 0200) ? 2 : 4;
    }

  if (tab_seen)
    {
      col = ((col + tab_width) / tab_width) * tab_width;
      col += post_tab;
    }

451 452 453
  if (ptr == BEGV_ADDR)
    current_column_bol_cache = BEGV;
  else
454 455
    current_column_bol_cache = BYTE_TO_CHAR (PTR_BYTE_POS (ptr));

Jim Blandy's avatar
Jim Blandy committed
456
  last_known_column = col;
457
  last_known_column_point = PT;
Jim Blandy's avatar
Jim Blandy committed
458 459 460 461 462
  last_known_column_modified = MODIFF;

  return col;
}

463 464 465 466 467 468
/* Return the column number of position POS
   by scanning forward from the beginning of the line.
   This function handles characters that are invisible
   due to text properties or overlays.  */

static int
469
current_column_1 ()
470 471 472 473
{
  register int tab_width = XINT (current_buffer->tab_width);
  register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
  register struct Lisp_Char_Table *dp = buffer_display_table ();
474
  int multibyte = !NILP (current_buffer->enable_multibyte_characters);
475 476 477

  /* Start the scan at the beginning of this line with column number 0.  */
  register int col = 0;
478 479 480 481
  int scan, scan_byte;
  int next_boundary, next_boundary_byte;
  int opoint = PT, opoint_byte = PT_BYTE;

482
  scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
483 484 485 486 487
  current_column_bol_cache = PT;
  scan = PT, scan_byte = PT_BYTE;
  SET_PT_BOTH (opoint, opoint_byte);
  next_boundary = scan;
  next_boundary_byte = scan_byte;
488 489 490 491

  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;

  /* Scan forward to the target position.  */
492
  while (scan < opoint)
493 494 495 496 497 498
    {
      int c;

      /* Occasionally we may need to skip invisible text.  */
      while (scan == next_boundary)
	{
499
	  int old_scan = scan;
500 501
	  /* This updates NEXT_BOUNDARY to the next place
	     where we might need to skip more invisible text.  */
502 503
	  scan = skip_invisible (scan, &next_boundary, opoint, Qnil);
	  if (scan >= opoint)
504
	    goto endloop;
505 506 507
	  if (scan != old_scan)
	    scan_byte = CHAR_TO_BYTE (scan);
	  next_boundary_byte = CHAR_TO_BYTE (next_boundary);
508 509
	}

510
      c = FETCH_BYTE (scan_byte);
511 512 513
      if (dp != 0
	  && ! (multibyte && BASE_LEADING_CODE_P (c))
	  && VECTORP (DISP_CHAR_VECTOR (dp, c)))
514 515 516
	{
	  col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
	  scan++;
517
	  scan_byte++;
518 519 520 521 522 523 524
	  continue;
	}
      if (c == '\n')
	break;
      if (c == '\r' && EQ (current_buffer->selective_display, Qt))
	break;
      scan++;
525
      scan_byte++;
526 527 528 529 530 531
      if (c == '\t')
	{
	  int prev_col = col;
	  col += tab_width;
	  col = col / tab_width * tab_width;
	}
532 533
      else if (multibyte && BASE_LEADING_CODE_P (c))
	{
534 535
	  unsigned char *ptr;
	  int bytes, width, wide_column;
536

537 538 539 540 541
	  scan_byte--;
	  ptr = BYTE_POS_ADDR (scan_byte);
	  MULTIBYTE_BYTES_WIDTH (ptr, c, dp);
	  scan_byte += bytes;
	  col += width;
542
	}
543 544 545 546 547 548 549 550 551 552
      else if (ctl_arrow && (c < 040 || c == 0177))
        col += 2;
      else if (c < 040 || c >= 0177)
        col += 4;
      else
	col++;
    }
 endloop:

  last_known_column = col;
553
  last_known_column_point = PT;
554 555 556 557 558
  last_known_column_modified = MODIFF;

  return col;
}

559 560 561 562 563
/* Return the width in columns of the part of STRING from BEG to END.
   If BEG is nil, that stands for the beginning of STRING.
   If END is nil, that stands for the end of STRING.  */

static int
564
string_display_width (string, beg, end)
565 566 567 568 569 570 571 572 573
     Lisp_Object string, beg, end;
{
  register int col;
  register unsigned char *ptr, *stop;
  register int tab_seen;
  int post_tab;
  register int c;
  register int tab_width = XINT (current_buffer->tab_width);
  int ctl_arrow = !NILP (current_buffer->ctl_arrow);
574
  register struct Lisp_Char_Table *dp = buffer_display_table ();
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 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
  int b, e;

  if (NILP (end))
    e = XSTRING (string)->size;
  else
    {
      CHECK_NUMBER (end, 0);
      e = XINT (end);
    }

  if (NILP (beg))
    b = 0;
  else
    {
      CHECK_NUMBER (beg, 0);
      b = XINT (beg);
    }

  /* Make a pointer for decrementing through the chars before point.  */
  ptr = XSTRING (string)->data + e;
  /* Make a pointer to where consecutive chars leave off,
     going backwards from point.  */
  stop = XSTRING (string)->data + b;

  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;

  col = 0, tab_seen = 0, post_tab = 0;

  while (1)
    {
      if (ptr == stop)
	break;

      c = *--ptr;
      if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))
	col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
      else if (c >= 040 && c < 0177)
	col++;
      else if (c == '\n')
	break;
      else if (c == '\t')
	{
	  if (tab_seen)
	    col = ((col + tab_width) / tab_width) * tab_width;

	  post_tab += col;
	  col = 0;
	  tab_seen = 1;
	}
      else
	col += (ctl_arrow && c < 0200) ? 2 : 4;
    }

  if (tab_seen)
    {
      col = ((col + tab_width) / tab_width) * tab_width;
      col += post_tab;
    }

  return col;
}

Jim Blandy's avatar
Jim Blandy committed
637 638
DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
  "Indent from point with tabs and spaces until COLUMN is reached.\n\
639 640 641 642
Optional second argument MININUM says always do at least MININUM spaces\n\
even if that goes past COLUMN; by default, MININUM is zero.")
  (column, minimum)
     Lisp_Object column, minimum;
Jim Blandy's avatar
Jim Blandy committed
643 644 645 646 647
{
  int mincol;
  register int fromcol;
  register int tab_width = XINT (current_buffer->tab_width);

648
  CHECK_NUMBER (column, 0);
Jim Blandy's avatar
Jim Blandy committed
649
  if (NILP (minimum))
650
    XSETFASTINT (minimum, 0);
Jim Blandy's avatar
Jim Blandy committed
651 652 653 654
  CHECK_NUMBER (minimum, 1);

  fromcol = current_column ();
  mincol = fromcol + XINT (minimum);
655
  if (mincol < XINT (column)) mincol = XINT (column);
Jim Blandy's avatar
Jim Blandy committed
656 657 658 659

  if (fromcol == mincol)
    return make_number (mincol);

660
  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
Jim Blandy's avatar
Jim Blandy committed
661 662 663 664

  if (indent_tabs_mode)
    {
      Lisp_Object n;
665
      XSETFASTINT (n, mincol / tab_width - fromcol / tab_width);
Jim Blandy's avatar
Jim Blandy committed
666 667
      if (XFASTINT (n) != 0)
	{
668
	  Finsert_char (make_number ('\t'), n, Qt);
Jim Blandy's avatar
Jim Blandy committed
669 670 671 672 673

	  fromcol = (mincol / tab_width) * tab_width;
	}
    }

674 675
  XSETFASTINT (column, mincol - fromcol);
  Finsert_char (make_number (' '), column, Qt);
Jim Blandy's avatar
Jim Blandy committed
676 677

  last_known_column = mincol;
678
  last_known_column_point = PT;
Jim Blandy's avatar
Jim Blandy committed
679 680
  last_known_column_modified = MODIFF;

681 682
  XSETINT (column, mincol);
  return column;
Jim Blandy's avatar
Jim Blandy committed
683
}
684

Jim Blandy's avatar
Jim Blandy committed
685

Andreas Schwab's avatar
Andreas Schwab committed
686 687
static int position_indentation P_ ((int));

Jim Blandy's avatar
Jim Blandy committed
688 689 690 691 692 693 694 695
DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
  0, 0, 0,
  "Return the indentation of the current line.\n\
This is the horizontal position of the character\n\
following any initial whitespace.")
  ()
{
  Lisp_Object val;
696 697 698
  int opoint = PT, opoint_byte = PT_BYTE;

  scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
Jim Blandy's avatar
Jim Blandy committed
699

700 701
  XSETFASTINT (val, position_indentation (PT_BYTE));
  SET_PT_BOTH (opoint, opoint_byte);
Jim Blandy's avatar
Jim Blandy committed
702 703 704
  return val;
}

705 706 707
static int
position_indentation (pos_byte)
     register int pos_byte;
Jim Blandy's avatar
Jim Blandy committed
708 709 710 711 712
{
  register int column = 0;
  register int tab_width = XINT (current_buffer->tab_width);
  register unsigned char *p;
  register unsigned char *stop;
713
  unsigned char *start;
714 715
  int next_boundary_byte = pos_byte;
  int ceiling = next_boundary_byte;
716

717
  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
718

719
  p = BYTE_POS_ADDR (pos_byte);
720 721 722 723 724 725
  /* STOP records the value of P at which we will need
     to think about the gap, or about invisible text,
     or about the end of the buffer.  */
  stop = p;
  /* START records the starting value of P.  */
  start = p;
Jim Blandy's avatar
Jim Blandy committed
726 727 728 729
  while (1)
    {
      while (p == stop)
	{
730
	  int stop_pos_byte;
731

732 733
	  /* If we have updated P, set POS_BYTE to match.
	     The first time we enter the loop, POS_BYTE is already right.  */
734
	  if (p != start)
735
	    pos_byte = PTR_BYTE_POS (p);
736
	  /* Consider the various reasons STOP might have been set here.  */
737
	  if (pos_byte == ZV_BYTE)
Jim Blandy's avatar
Jim Blandy committed
738
	    return column;
739 740 741 742 743 744 745 746 747 748
	  if (pos_byte == next_boundary_byte)
	    {
	      int next_boundary;
	      int pos = BYTE_TO_CHAR (pos_byte);
	      pos = skip_invisible (pos, &next_boundary, ZV, Qnil);
	      pos_byte = CHAR_TO_BYTE (pos);
	      next_boundary_byte = CHAR_TO_BYTE (next_boundary);
	    }
	  if (pos_byte >= ceiling)
	    ceiling = BUFFER_CEILING_OF (pos_byte) + 1;
749 750
	  /* Compute the next place we need to stop and think,
	     and set STOP accordingly.  */
751
	  stop_pos_byte = min (ceiling, next_boundary_byte);
752
	  /* The -1 and +1 arrange to point at the first byte of gap
753
	     (if STOP_POS_BYTE is the position of the gap)
754 755
	     rather than at the data after the gap.  */
	     
756 757
	  stop = BYTE_POS_ADDR (stop_pos_byte - 1) + 1;
	  p = BYTE_POS_ADDR (pos_byte);
Jim Blandy's avatar
Jim Blandy committed
758 759 760
	}
      switch (*p++)
	{
761 762 763
	case 0240:
	  if (! NILP (current_buffer->enable_multibyte_characters))
	    return column;
Jim Blandy's avatar
Jim Blandy committed
764 765 766 767 768 769 770
	case ' ':
	  column++;
	  break;
	case '\t':
	  column += tab_width - column % tab_width;
	  break;
	default:
771 772 773 774
	  if (ASCII_BYTE_P (p[-1])
	      || NILP (current_buffer->enable_multibyte_characters))
	    return column;
	  {
775 776 777
	    int c;
	    pos_byte = PTR_BYTE_POS (p - 1);
	    c = FETCH_MULTIBYTE_CHAR (pos_byte);
778 779 780
	    if (CHAR_HAS_CATEGORY (c, ' '))
	      {
		column++;
781 782
		INC_POS (pos_byte);
		p = BYTE_POS_ADDR (pos_byte);
783 784 785 786
	      }
	    else
	      return column;
	  }
Jim Blandy's avatar
Jim Blandy committed
787 788 789
	}
    }
}
790 791 792 793

/* Test whether the line beginning at POS is indented beyond COLUMN.
   Blank lines are treated as if they had the same indentation as the
   preceding line.  */
794

795
int
796 797
indented_beyond_p (pos, pos_byte, column)
     int pos, pos_byte, column;
798
{
799
  int val;
800 801 802 803 804 805
  int opoint = PT, opoint_byte = PT_BYTE;

  SET_PT_BOTH (pos, pos_byte);
  while (PT > BEGV && FETCH_BYTE (PT_BYTE) == '\n')
    scan_newline (PT - 1, PT_BYTE - 1, BEGV, BEGV_BYTE, -1, 0);

806
  val = position_indentation (PT_BYTE);
807
  SET_PT_BOTH (opoint, opoint_byte);
808
  return val >= column;
809
}
Jim Blandy's avatar
Jim Blandy committed
810

811
DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p",
Jim Blandy's avatar
Jim Blandy committed
812 813 814 815 816
  "Move point to column COLUMN in the current line.\n\
The column of a character is calculated by adding together the widths\n\
as displayed of the previous characters in the line.\n\
This function ignores line-continuation;\n\
there is no upper limit on the column number a character can have\n\
817 818
and horizontal scrolling has no effect.\n\
\n\
Jim Blandy's avatar
Jim Blandy committed
819 820
If specified column is within a character, point goes after that character.\n\
If it's past end of line, point goes to end of line.\n\n\
821 822 823 824
A non-nil second (optional) argument FORCE means,\n\
if COLUMN is in the middle of a tab character, change it to spaces.\n\
In addition, if FORCE is t, and the line is too short\n\
to reach column COLUMN, add spaces/tabs to get there.\n\
825 826
\n\
The return value is the current column.")
Jim Blandy's avatar
Jim Blandy committed
827 828 829 830 831 832 833 834
  (column, force)
     Lisp_Object column, force;
{
  register int pos;
  register int col = current_column ();
  register int goal;
  register int end;
  register int tab_width = XINT (current_buffer->tab_width);
Jim Blandy's avatar
Jim Blandy committed
835
  register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
836
  register struct Lisp_Char_Table *dp = buffer_display_table ();
837
  register int multibyte = !NILP (current_buffer->enable_multibyte_characters);
Jim Blandy's avatar
Jim Blandy committed
838 839 840 841

  Lisp_Object val;
  int prev_col;
  int c;
842 843
  int next_boundary;

844 845
  int pos_byte, end_byte, next_boundary_byte;

846
  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
Jim Blandy's avatar
Jim Blandy committed
847 848 849
  CHECK_NATNUM (column, 0);
  goal = XINT (column);

850
  pos = PT;
851
  pos_byte = PT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
852
  end = ZV;
853
  end_byte = ZV_BYTE;
854
  next_boundary = pos;
855
  next_boundary_byte = PT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
856 857 858 859 860

  /* If we're starting past the desired column,
     back up to beginning of line and scan from there.  */
  if (col > goal)
    {
861
      end = pos;
862
      pos = current_column_bol_cache;
863
      pos_byte = CHAR_TO_BYTE (pos);
Jim Blandy's avatar
Jim Blandy committed
864 865 866
      col = 0;
    }

867
  while (pos < end)
Jim Blandy's avatar
Jim Blandy committed
868
    {
869 870
      while (pos == next_boundary)
	{
871
	  int prev = pos;
872
	  pos = skip_invisible (pos, &next_boundary, end, Qnil);
873 874 875
	  if (pos != prev)
	    pos_byte = CHAR_TO_BYTE (pos);
	  next_boundary_byte = CHAR_TO_BYTE (next_boundary);
876 877 878 879
	  if (pos >= end)
	    goto endloop;
	}

880 881 882 883 884 885
      /* Test reaching the goal column.  We do this after skipping
	 invisible characters, so that we put point before the
	 character on which the cursor will appear.  */
      if (col >= goal)
	break;

886
      c = FETCH_BYTE (pos_byte);
887 888 889
      if (dp != 0
	  && ! (multibyte && BASE_LEADING_CODE_P (c))
	  && VECTORP (DISP_CHAR_VECTOR (dp, c)))
890 891
	{
	  col += XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;
892
	  pos_byte++;
893
	  pos++;
894
	  continue;
895
	}
Jim Blandy's avatar
Jim Blandy committed
896 897 898 899 900
      if (c == '\n')
	break;
      if (c == '\r' && EQ (current_buffer->selective_display, Qt))
	break;
      pos++;
901
      pos_byte++;
Jim Blandy's avatar
Jim Blandy committed
902 903 904 905 906 907 908
      if (c == '\t')
	{
	  prev_col = col;
	  col += tab_width;
	  col = col / tab_width * tab_width;
	}
      else if (ctl_arrow && (c < 040 || c == 0177))
909
        col += 2;
910
      else if (c < 040 || c == 0177)
911
        col += 4;
912
      else if (c < 0177)
Jim Blandy's avatar
Jim Blandy committed
913
	col++;
914 915 916 917
      else if (multibyte && BASE_LEADING_CODE_P (c))
	{
	  /* Start of multi-byte form.  */
	  unsigned char *ptr;
918
	  int bytes, width, wide_column;
919

920
	  pos_byte--;
921
	  ptr = BYTE_POS_ADDR (pos_byte);
922 923 924
	  MULTIBYTE_BYTES_WIDTH (ptr, c, dp);
	  pos_byte += bytes;
	  col += width;
925 926 927
	}
      else
	col += 4;
Jim Blandy's avatar
Jim Blandy committed
928
    }
929
 endloop:
Jim Blandy's avatar
Jim Blandy committed
930

931
  SET_PT_BOTH (pos, pos_byte);
Jim Blandy's avatar
Jim Blandy committed
932 933 934

  /* If a tab char made us overshoot, change it to spaces
     and scan through it again.  */
Jim Blandy's avatar
Jim Blandy committed
935
  if (!NILP (force) && col > goal && c == '\t' && prev_col < goal)
Jim Blandy's avatar
Jim Blandy committed
936
    {
937
      int old_point, old_point_byte;
Jim Blandy's avatar
Jim Blandy committed
938

939
      del_range (PT - 1, PT);
Jim Blandy's avatar
Jim Blandy committed
940
      Findent_to (make_number (goal), Qnil);
941
      old_point = PT;
942
      old_point_byte = PT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
943
      Findent_to (make_number (col), Qnil);
944
      SET_PT_BOTH (old_point, old_point_byte);
945 946
      /* Set the last_known... vars consistently.  */
      col = goal;
Jim Blandy's avatar
Jim Blandy committed
947 948 949
    }

  /* If line ends prematurely, add space to the end.  */
950
  if (col < goal && EQ (force, Qt))
951
    Findent_to (make_number (col = goal), Qnil);
Jim Blandy's avatar
Jim Blandy committed
952 953

  last_known_column = col;
954
  last_known_column_point = PT;
Jim Blandy's avatar
Jim Blandy committed
955 956
  last_known_column_modified = MODIFF;

957
  XSETFASTINT (val, col);
Jim Blandy's avatar
Jim Blandy committed
958 959 960
  return val;
}

961 962
/* compute_motion: compute buffer posn given screen posn and vice versa */

Jim Blandy's avatar
Jim Blandy committed
963 964 965 966 967
struct position val_compute_motion;

/* Scan the current buffer forward from offset FROM, pretending that
   this is at line FROMVPOS, column FROMHPOS, until reaching buffer
   offset TO or line TOVPOS, column TOHPOS (whichever comes first),
968 969 970
   and return the ending buffer position and screen location.  If we
   can't hit the requested column exactly (because of a tab or other
   multi-column character), overshoot.
Jim Blandy's avatar
Jim Blandy committed
971

972 973 974 975 976 977
   DID_MOTION is 1 if FROMHPOS has already accounted for overlay strings
   at FROM.  This is the case if FROMVPOS and FROMVPOS came from an
   earlier call to compute_motion.  The other common case is that FROMHPOS
   is zero and FROM is a position that "belongs" at column zero, but might
   be shifted by overlay strings; in this case DID_MOTION should be 0.

Jim Blandy's avatar
Jim Blandy committed
978 979 980 981
   WIDTH is the number of columns available to display text;
   compute_motion uses this to handle continuation lines and such.
   HSCROLL is the number of columns not being displayed at the left
   margin; this is usually taken from a window's hscroll member.
Jim Blandy's avatar
Jim Blandy committed
982 983 984
   TAB_OFFSET is the number of columns of the first tab that aren't
   being displayed, perhaps because of a continuation line or
   something.
Jim Blandy's avatar
Jim Blandy committed
985 986 987

   compute_motion returns a pointer to a struct position.  The bufpos
   member gives the buffer position at the end of the scan, and hpos
988 989 990 991 992 993 994 995
   and vpos give its cartesian location.  prevhpos is the column at
   which the character before bufpos started, and contin is non-zero
   if we reached the current line by continuing the previous.

   Note that FROMHPOS and TOHPOS should be expressed in real screen
   columns, taking HSCROLL and the truncation glyph at the left margin
   into account.  That is, beginning-of-line moves you to the hpos
   -HSCROLL + (HSCROLL > 0).
Jim Blandy's avatar
Jim Blandy committed
996 997 998 999 1000 1001

   For example, to find the buffer position of column COL of line LINE
   of a certain window, pass the window's starting location as FROM
   and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
   Pass the buffer's ZV as TO, to limit the scan to the end of the
   visible section of the buffer, and pass LINE and COL as TOVPOS and
1002
   TOHPOS.
Jim Blandy's avatar
Jim Blandy committed
1003 1004 1005 1006

   When displaying in window w, a typical formula for WIDTH is:

	window_width - 1
1007
	 - (has_vertical_scroll_bars
Karl Heuer's avatar
Karl Heuer committed
1008
	    ? FRAME_SCROLL_BAR_COLS (XFRAME (window->frame))
1009
	    : (window_width + window_left != frame_width))
Jim Blandy's avatar
Jim Blandy committed
1010 1011 1012 1013

	where
	  window_width is XFASTINT (w->width),
	  window_left is XFASTINT (w->left),
1014 1015
	  has_vertical_scroll_bars is
	    FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
1016
	  and frame_width = FRAME_WIDTH (XFRAME (window->frame))
Jim Blandy's avatar
Jim Blandy committed
1017

Karl Heuer's avatar
Karl Heuer committed
1018 1019
   Or you can let window_internal_width do this all for you, and write:
	window_internal_width (w) - 1
1020 1021

   The `-1' accounts for the continuation-line backslashes; the rest
Karl Heuer's avatar
Karl Heuer committed
1022
   accounts for window borders if the window is split horizontally, and
Karl Heuer's avatar
Karl Heuer committed
1023
   the scroll bars if they are turned on.  */
Jim Blandy's avatar
Jim Blandy committed
1024 1025

struct position *
1026
compute_motion (from, fromvpos, fromhpos, did_motion, to, tovpos, tohpos, width, hscroll, tab_offset, win)
Jim Blandy's avatar
Jim Blandy committed
1027
     int from, fromvpos, fromhpos, to, tovpos, tohpos;
1028
     int did_motion;
Jim Blandy's avatar
Jim Blandy committed
1029 1030
     register int width;
     int hscroll, tab_offset;
1031
     struct window *win;
Jim Blandy's avatar
Jim Blandy committed
1032
{
Jim Blandy's avatar
Jim Blandy committed
1033 1034 1035
  register int hpos = fromhpos;
  register int vpos = fromvpos;

Jim Blandy's avatar
Jim Blandy committed
1036
  register int pos;
1037
  int pos_byte;
Jim Blandy's avatar
Jim Blandy committed
1038 1039
  register int c;
  register int tab_width = XFASTINT (current_buffer->tab_width);
Jim Blandy's avatar
Jim Blandy committed
1040
  register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
1041
  register struct Lisp_Char_Table *dp = window_display_table (win);
Jim Blandy's avatar
Jim Blandy committed
1042
  int selective
1043
    = (INTEGERP (current_buffer->selective_display)
1044 1045
       ? XINT (current_buffer->selective_display)
       : !NILP (current_buffer->selective_display) ? -1 : 0);
1046
  int prev_hpos = 0;
Jim Blandy's avatar
Jim Blandy committed
1047
  int selective_rlen
1048
    = (selective && dp && VECTORP (DISP_INVIS_VECTOR (dp))
1049
       ? XVECTOR (DISP_INVIS_VECTOR (dp))->size : 0);
1050 1051 1052
  /* The next location where the `invisible' property changes, or an
     overlay starts or ends.  */
  int next_boundary = from;
Jim Blandy's avatar
Jim Blandy committed
1053

1054 1055
  /* For computing runs of characters with similar widths.
     Invariant: width_run_width is zero, or all the characters
1056
     from width_run_start to width_run_end have a fixed width of
1057 1058 1059 1060 1061
     width_run_width.  */
  int width_run_start = from;
  int width_run_end   = from;
  int width_run_width = 0;
  Lisp_Object *width_table;
1062