indent.c 61.7 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Indentation functions.
Stefan Monnier's avatar
Stefan Monnier committed
2
   Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1998, 2000, 2001,
Glenn Morris's avatar
Glenn Morris committed
3
                 2002, 2003, 2004, 2005, 2006, 2007 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 3, 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
Lute Kamstra's avatar
Lute Kamstra committed
19 20
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
Jim Blandy's avatar
Jim Blandy committed
21

22
#include <config.h>
23 24
#include <stdio.h>

Jim Blandy's avatar
Jim Blandy committed
25 26
#include "lisp.h"
#include "buffer.h"
27
#include "charset.h"
28
#include "category.h"
Jim Blandy's avatar
Jim Blandy committed
29
#include "indent.h"
30
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
31
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
32 33 34 35
#include "window.h"
#include "termchar.h"
#include "termopts.h"
#include "disptab.h"
36
#include "intervals.h"
37
#include "region-cache.h"
Jim Blandy's avatar
Jim Blandy committed
38 39

/* Indentation can insert tabs if this is non-zero;
40 41
   otherwise always uses spaces.  */

Jim Blandy's avatar
Jim Blandy committed
42 43 44 45
int indent_tabs_mode;

#define CR 015

Eli Zaretskii's avatar
Eli Zaretskii committed
46
/* These three values memorize the current column to avoid recalculation.  */
47 48 49

/* Last value returned by current_column.
   Some things in set last_known_column_point to -1
Eli Zaretskii's avatar
Eli Zaretskii committed
50
   to mark the memorized value as invalid.  */
51

52
double last_known_column;
53 54 55

/* Value of point when current_column was called.  */

Stefan Monnier's avatar
Stefan Monnier committed
56
EMACS_INT last_known_column_point;
57 58 59

/* Value of MODIFF when current_column was called.  */

Jim Blandy's avatar
Jim Blandy committed
60 61
int last_known_column_modified;

62 63
static double current_column_1 P_ ((void));
static double position_indentation P_ ((int));
64

65 66
/* Cache of beginning of line found by the last call of
   current_column. */
67

Stefan Monnier's avatar
Stefan Monnier committed
68
static EMACS_INT current_column_bol_cache;
69

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

72
struct Lisp_Char_Table *
Jim Blandy's avatar
Jim Blandy committed
73 74 75 76 77
buffer_display_table ()
{
  Lisp_Object thisbuf;

  thisbuf = current_buffer->display_table;
78 79 80 81
  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
82 83
  return 0;
}
84 85 86 87

/* Width run cache considerations.  */

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

89 90 91
static int
character_width (c, dp)
     int c;
92
     struct Lisp_Char_Table *dp;
93 94 95 96 97 98
{
  Lisp_Object elt;

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

99
  /* Everything can be handled by the display table, if it's
100
     present and the element is right.  */
101
  if (dp && (elt = DISP_CHAR_VECTOR (dp, c), VECTORP (elt)))
102 103
    return XVECTOR (elt)->size;

104 105 106 107 108
  /* Some characters are special.  */
  if (c == '\n' || c == '\t' || c == '\015')
    return 0;

  /* Printing characters have width 1.  */
109 110 111 112 113 114 115 116 117 118 119 120
  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;
}

Glenn Morris's avatar
Glenn Morris committed
121
/* Return true if the display table DISPTAB specifies the same widths
122 123
   for characters as WIDTHTAB.  We use this to decide when to
   invalidate the buffer's width_run_cache.  */
124

125 126
int
disptab_matches_widthtab (disptab, widthtab)
127
     struct Lisp_Char_Table *disptab;
128 129 130 131 132 133 134 135 136 137 138 139 140
     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;
141
}
142 143

/* Recompute BUF's width table, using the display table DISPTAB.  */
144

145 146 147
void
recompute_width_table (buf, disptab)
     struct buffer *buf;
148
     struct Lisp_Char_Table *disptab;
149 150
{
  int i;
151
  struct Lisp_Vector *widthtab;
152

153 154 155
  if (!VECTORP (buf->width_table))
    buf->width_table = Fmake_vector (make_number (256), make_number (0));
  widthtab = XVECTOR (buf->width_table);
156 157 158 159
  if (widthtab->size != 256)
    abort ();

  for (i = 0; i < 256; i++)
160
    XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
161 162 163 164
}

/* Allocate or free the width run cache, as requested by the current
   state of current_buffer's cache_long_line_scans variable.  */
165

166 167 168
static void
width_run_cache_on_off ()
{
169 170 171 172
  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))
173 174 175 176 177 178 179 180 181 182 183 184 185
    {
      /* 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)
186
        {
187 188 189 190 191 192
          current_buffer->width_run_cache = new_region_cache ();
          recompute_width_table (current_buffer, buffer_display_table ());
        }
    }
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

/* 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.  */

Stefan Monnier's avatar
Stefan Monnier committed
217
EMACS_INT
218
skip_invisible (pos, next_boundary_p, to, window)
Stefan Monnier's avatar
Stefan Monnier committed
219 220 221
     EMACS_INT pos;
     EMACS_INT *next_boundary_p;
     EMACS_INT to;
222 223
     Lisp_Object window;
{
224
  Lisp_Object prop, position, overlay_limit, proplimit;
225
  Lisp_Object buffer, tmp;
Stefan Monnier's avatar
Stefan Monnier committed
226 227
  EMACS_INT end;
  int inv_p;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

  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;
257 258 259
      tmp = Fnext_single_property_change (position, Qinvisible,
					  buffer, proplimit);
      end = XFASTINT (tmp);
260
#if 0
261 262 263 264 265 266 267
      /* 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--;
268
#endif
269
      *next_boundary_p = end;
270 271 272
    }
  /* if the `invisible' property is set, we can skip to
     the next property change */
273 274 275 276 277 278 279
  prop = Fget_char_property (position, Qinvisible,
			     (!NILP (window)
			      && EQ (XWINDOW (window)->buffer, buffer))
			     ? window : buffer);
  inv_p = TEXT_PROP_MEANS_INVISIBLE (prop);
  /* When counting columns (window == nil), don't skip over ellipsis text.  */
  if (NILP (window) ? inv_p == 1 : inv_p)
280 281 282
    return *next_boundary_p;
  return pos;
}
Jim Blandy's avatar
Jim Blandy committed
283

284
/* If a composition starts at POS/POS_BYTE and it doesn't stride over
Dave Love's avatar
Dave Love committed
285
   POINT, set *LEN / *LEN_BYTE to the character and byte lengths, *WIDTH
286 287 288 289 290 291 292 293 294 295 296 297
   to the width, and return 1.  Otherwise, return 0.  */

static int
check_composition (pos, pos_byte, point, len, len_byte, width)
     int pos, pos_byte, point;
     int *len, *len_byte, *width;
{
  Lisp_Object prop;
  int start, end;
  int id;

  if (! find_composition (pos, -1, &start, &end, &prop, Qnil)
298 299
      || pos != start || point < end
      || !COMPOSITION_VALID_P (start, end, prop))
300 301 302 303 304 305 306 307 308 309
    return 0;
  if ((id = get_composition_id (pos, pos_byte, end - pos, prop, Qnil)) < 0)
    return 0;

  *len = COMPOSITION_LENGTH (prop);
  *len_byte = CHAR_TO_BYTE (end) - pos_byte;
  *width = composition_table[id]->width;
  return 1;
}

310 311 312 313 314 315 316
/* Set variables WIDTH and BYTES for a multibyte sequence starting at P.

   DP is a display table or NULL.

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

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
#define MULTIBYTE_BYTES_WIDTH(p, dp)					\
  do {									\
    int c;								\
    									\
    wide_column = 0;							\
    c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, bytes);	\
    if (BYTES_BY_CHAR_HEAD (*p) != bytes)				\
      width = bytes * 4;						\
    else								\
      {									\
	if (dp != 0 && VECTORP (DISP_CHAR_VECTOR (dp, c)))		\
	  width = XVECTOR (DISP_CHAR_VECTOR (dp, c))->size;		\
	else								\
	  width = WIDTH_BY_CHAR_HEAD (*p);				\
	if (width > 1)							\
	  wide_column = width;						\
      }									\
334 335
  } while (0)

336

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

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

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

364
double
Jim Blandy's avatar
Jim Blandy committed
365 366 367 368 369 370 371 372
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
373
  int ctl_arrow = !NILP (current_buffer->ctl_arrow);
374
  register struct Lisp_Char_Table *dp = buffer_display_table ();
Jim Blandy's avatar
Jim Blandy committed
375

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
  if (BUF_INTERVALS (current_buffer)
383 384
      || current_buffer->overlays_before
      || 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 403
  if (tab_width <= 0 || tab_width > 1000)
    tab_width = 8;
Jim Blandy's avatar
Jim Blandy committed
404 405 406 407 408

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

  while (1)
    {
409 410
      EMACS_INT i, n;
      Lisp_Object charvec;
411

Jim Blandy's avatar
Jim Blandy committed
412 413 414 415 416 417
      if (ptr == stop)
	{
	  /* We stopped either for the beginning of the buffer
	     or for the gap.  */
	  if (ptr == BEGV_ADDR)
	    break;
418

Jim Blandy's avatar
Jim Blandy committed
419 420 421
	  /* It was the gap.  Jump back over it.  */
	  stop = BEGV_ADDR;
	  ptr = GPT_ADDR;
422

Jim Blandy's avatar
Jim Blandy committed
423
	  /* Check whether that brings us to beginning of buffer.  */
424 425
	  if (BEGV >= GPT)
	    break;
Jim Blandy's avatar
Jim Blandy committed
426 427 428
	}

      c = *--ptr;
429

430
      if (dp && VECTORP (DISP_CHAR_VECTOR (dp, c)))
431
	{
432 433
	  charvec = DISP_CHAR_VECTOR (dp, c);
	  n = ASIZE (charvec);
434
	}
435
      else
Jim Blandy's avatar
Jim Blandy committed
436
	{
437 438 439
	  charvec = Qnil;
	  n = 1;
	}
440

441 442 443 444 445 446 447
      for (i = n - 1; i >= 0; --i)
	{
	  if (VECTORP (charvec))
	    {
	      /* This should be handled the same as
		 next_element_from_display_vector does it.  */
	      Lisp_Object entry = AREF (charvec, i);
448

449 450 451 452 453 454
	      if (INTEGERP (entry)
		  && GLYPH_CHAR_VALID_P (XFASTINT (entry)))
		c = FAST_GLYPH_CHAR (XFASTINT (entry));
	      else
		c = ' ';
	    }
455

456 457 458 459 460 461 462 463 464 465 466 467 468
	  if (c >= 040 && c < 0177)
	    col++;
	  else if (c == '\n'
		   || (c == '\r'
		       && EQ (current_buffer->selective_display, Qt)))
	    {
	      ptr++;
	      goto start_of_line_found;
	    }
	  else if (c == '\t')
	    {
	      if (tab_seen)
		col = ((col + tab_width) / tab_width) * tab_width;
469

470 471 472 473
	      post_tab += col;
	      col = 0;
	      tab_seen = 1;
	    }
474 475 476 477 478 479 480
	  else if (VECTORP (charvec))
	    /* With a display table entry, C is displayed as is, and
	       not displayed as \NNN or as ^N.  If C is a single-byte
	       character, it takes one column.  If C is multi-byte in
	       an unibyte buffer, it's translated to unibyte, so it
	       also takes one column.  */
	    ++col;
481 482
	  else
	    col += (ctl_arrow && c < 0200) ? 2 : 4;
Jim Blandy's avatar
Jim Blandy committed
483 484 485
	}
    }

486 487
 start_of_line_found:

Jim Blandy's avatar
Jim Blandy committed
488 489 490 491 492 493
  if (tab_seen)
    {
      col = ((col + tab_width) / tab_width) * tab_width;
      col += post_tab;
    }

494 495 496
  if (ptr == BEGV_ADDR)
    current_column_bol_cache = BEGV;
  else
497 498
    current_column_bol_cache = BYTE_TO_CHAR (PTR_BYTE_POS (ptr));

Jim Blandy's avatar
Jim Blandy committed
499
  last_known_column = col;
500
  last_known_column_point = PT;
Jim Blandy's avatar
Jim Blandy committed
501 502 503 504 505
  last_known_column_modified = MODIFF;

  return col;
}

506 507 508 509 510
/* 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.  */

511
static double
512
current_column_1 ()
513
{
Stefan Monnier's avatar
Stefan Monnier committed
514
  register EMACS_INT tab_width = XINT (current_buffer->tab_width);
515 516
  register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
  register struct Lisp_Char_Table *dp = buffer_display_table ();
517
  int multibyte = !NILP (current_buffer->enable_multibyte_characters);
518 519

  /* Start the scan at the beginning of this line with column number 0.  */
Stefan Monnier's avatar
Stefan Monnier committed
520 521 522 523
  register EMACS_INT col = 0;
  EMACS_INT scan, scan_byte;
  EMACS_INT next_boundary;
  EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
524

525
  scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -1, 1);
526 527 528 529
  current_column_bol_cache = PT;
  scan = PT, scan_byte = PT_BYTE;
  SET_PT_BOTH (opoint, opoint_byte);
  next_boundary = scan;
530 531 532 533

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

  /* Scan forward to the target position.  */
534
  while (scan < opoint)
535 536 537 538 539 540
    {
      int c;

      /* Occasionally we may need to skip invisible text.  */
      while (scan == next_boundary)
	{
541
	  int old_scan = scan;
542 543
	  /* This updates NEXT_BOUNDARY to the next place
	     where we might need to skip more invisible text.  */
544 545
	  scan = skip_invisible (scan, &next_boundary, opoint, Qnil);
	  if (scan >= opoint)
546
	    goto endloop;
547 548
	  if (scan != old_scan)
	    scan_byte = CHAR_TO_BYTE (scan);
549 550
	}

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
      /* Check composition sequence.  */
      {
	int len, len_byte, width;

	if (check_composition (scan, scan_byte, opoint,
			       &len, &len_byte, &width))
	  {
	    scan += len;
	    scan_byte += len_byte;
	    if (scan <= opoint)
	      col += width;
	    continue;
	  }
      }

566
      c = FETCH_BYTE (scan_byte);
567

568 569 570
      if (dp != 0
	  && ! (multibyte && BASE_LEADING_CODE_P (c))
	  && VECTORP (DISP_CHAR_VECTOR (dp, c)))
571
	{
572 573 574 575 576 577
	  Lisp_Object charvec;
	  EMACS_INT i, n;

	  /* This character is displayed using a vector of glyphs.
	     Update the column based on those glyphs.  */

578 579 580
	  charvec = DISP_CHAR_VECTOR (dp, c);
	  n = ASIZE (charvec);

581
	  for (i = 0; i < n; i++)
582 583 584
	    {
	      /* This should be handled the same as
		 next_element_from_display_vector does it.  */
585 586 587
	      Lisp_Object entry;
	      entry = AREF (charvec, i);

588 589 590 591 592
	      if (INTEGERP (entry)
		  && GLYPH_CHAR_VALID_P (XFASTINT (entry)))
		c = FAST_GLYPH_CHAR (XFASTINT (entry));
	      else
		c = ' ';
593 594 595 596 597 598 599 600 601 602 603 604

	      if (c == '\n')
		goto endloop;
	      if (c == '\r' && EQ (current_buffer->selective_display, Qt))
		goto endloop;
	      if (c == '\t')
		{
		  col += tab_width;
		  col = col / tab_width * tab_width;
		}
	      else
		++col;
605
	    }
606 607 608 609 610 611
	}
      else
	{
	  /* The display table says nothing for this character.
	     Display it as itself.  */

612 613 614 615 616 617 618 619 620 621 622 623 624
	  if (c == '\n')
	    goto endloop;
	  if (c == '\r' && EQ (current_buffer->selective_display, Qt))
	    goto endloop;
	  if (c == '\t')
	    {
	      col += tab_width;
	      col = col / tab_width * tab_width;
	    }
	  else if (multibyte && BASE_LEADING_CODE_P (c))
	    {
	      unsigned char *ptr;
	      int bytes, width, wide_column;
625

626 627 628
	      ptr = BYTE_POS_ADDR (scan_byte);
	      MULTIBYTE_BYTES_WIDTH (ptr, dp);
	      scan_byte += bytes;
629 630 631
	      /* Subtract one to compensate for the increment
		 that is going to happen below.  */
	      scan_byte--;
632 633 634 635 636 637 638 639
	      col += width;
	    }
	  else if (ctl_arrow && (c < 040 || c == 0177))
	    col += 2;
	  else if (c < 040 || c >= 0177)
	    col += 4;
	  else
	    col++;
640
	}
641 642 643
      scan++;
      scan_byte++;

644 645 646 647
    }
 endloop:

  last_known_column = col;
648
  last_known_column_point = PT;
649 650 651 652 653
  last_known_column_modified = MODIFF;

  return col;
}

654 655 656

#if 0 /* Not used.  */

657 658 659 660
/* 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.  */

661
static double
662
string_display_width (string, beg, end)
663 664 665 666 667 668 669 670 671
     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);
672
  register struct Lisp_Char_Table *dp = buffer_display_table ();
673 674 675
  int b, e;

  if (NILP (end))
676
    e = SCHARS (string);
677 678
  else
    {
679
      CHECK_NUMBER (end);
680 681 682 683 684 685 686
      e = XINT (end);
    }

  if (NILP (beg))
    b = 0;
  else
    {
687
      CHECK_NUMBER (beg);
688 689 690 691
      b = XINT (beg);
    }

  /* Make a pointer for decrementing through the chars before point.  */
692
  ptr = SDATA (string) + e;
693 694
  /* Make a pointer to where consecutive chars leave off,
     going backwards from point.  */
695
  stop = SDATA (string) + b;
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733

  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;
}
734 735 736

#endif /* 0 */

737

Jim Blandy's avatar
Jim Blandy committed
738
DEFUN ("indent-to", Findent_to, Sindent_to, 1, 2, "NIndent to column: ",
739
       doc: /* Indent from point with tabs and spaces until COLUMN is reached.
740
Optional second argument MINIMUM says always do at least MINIMUM spaces
Eli Zaretskii's avatar
Eli Zaretskii committed
741 742 743
even if that goes past COLUMN; by default, MINIMUM is zero.

The return value is COLUMN.  */)
744
     (column, minimum)
745
     Lisp_Object column, minimum;
Jim Blandy's avatar
Jim Blandy committed
746 747 748 749 750
{
  int mincol;
  register int fromcol;
  register int tab_width = XINT (current_buffer->tab_width);

751
  CHECK_NUMBER (column);
Jim Blandy's avatar
Jim Blandy committed
752
  if (NILP (minimum))
753
    XSETFASTINT (minimum, 0);
754
  CHECK_NUMBER (minimum);
Jim Blandy's avatar
Jim Blandy committed
755 756 757

  fromcol = current_column ();
  mincol = fromcol + XINT (minimum);
758
  if (mincol < XINT (column)) mincol = XINT (column);
Jim Blandy's avatar
Jim Blandy committed
759 760 761 762

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

763
  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
Jim Blandy's avatar
Jim Blandy committed
764 765 766 767

  if (indent_tabs_mode)
    {
      Lisp_Object n;
768
      XSETFASTINT (n, mincol / tab_width - fromcol / tab_width);
Jim Blandy's avatar
Jim Blandy committed
769 770
      if (XFASTINT (n) != 0)
	{
771
	  Finsert_char (make_number ('\t'), n, Qt);
Jim Blandy's avatar
Jim Blandy committed
772 773 774 775 776

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

777 778
  XSETFASTINT (column, mincol - fromcol);
  Finsert_char (make_number (' '), column, Qt);
Jim Blandy's avatar
Jim Blandy committed
779 780

  last_known_column = mincol;
781
  last_known_column_point = PT;
Jim Blandy's avatar
Jim Blandy committed
782 783
  last_known_column_modified = MODIFF;

784 785
  XSETINT (column, mincol);
  return column;
Jim Blandy's avatar
Jim Blandy committed
786
}
787

Jim Blandy's avatar
Jim Blandy committed
788

789
static double position_indentation P_ ((int));
Andreas Schwab's avatar
Andreas Schwab committed
790

Jim Blandy's avatar
Jim Blandy committed
791
DEFUN ("current-indentation", Fcurrent_indentation, Scurrent_indentation,
792 793 794 795 796
       0, 0, 0,
       doc: /* Return the indentation of the current line.
This is the horizontal position of the character
following any initial whitespace.  */)
     ()
Jim Blandy's avatar
Jim Blandy committed
797 798
{
  Lisp_Object val;
799 800 801
  int opoint = PT, opoint_byte = PT_BYTE;

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

803
  XSETFASTINT (val, (int) position_indentation (PT_BYTE)); /* iftc */
804
  SET_PT_BOTH (opoint, opoint_byte);
Jim Blandy's avatar
Jim Blandy committed
805 806 807
  return val;
}

808
static double
809 810
position_indentation (pos_byte)
     register int pos_byte;
Jim Blandy's avatar
Jim Blandy committed
811
{
Stefan Monnier's avatar
Stefan Monnier committed
812 813
  register EMACS_INT column = 0;
  register EMACS_INT tab_width = XINT (current_buffer->tab_width);
Jim Blandy's avatar
Jim Blandy committed
814 815
  register unsigned char *p;
  register unsigned char *stop;
816
  unsigned char *start;
Stefan Monnier's avatar
Stefan Monnier committed
817 818
  EMACS_INT next_boundary_byte = pos_byte;
  EMACS_INT ceiling = next_boundary_byte;
819

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

822
  p = BYTE_POS_ADDR (pos_byte);
823 824 825 826 827 828
  /* 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
829 830 831 832
  while (1)
    {
      while (p == stop)
	{
Stefan Monnier's avatar
Stefan Monnier committed
833
	  EMACS_INT stop_pos_byte;
834

835 836
	  /* If we have updated P, set POS_BYTE to match.
	     The first time we enter the loop, POS_BYTE is already right.  */
837
	  if (p != start)
838
	    pos_byte = PTR_BYTE_POS (p);
839
	  /* Consider the various reasons STOP might have been set here.  */
840
	  if (pos_byte == ZV_BYTE)
Jim Blandy's avatar
Jim Blandy committed
841
	    return column;
842 843
	  if (pos_byte == next_boundary_byte)
	    {
Stefan Monnier's avatar
Stefan Monnier committed
844 845
	      EMACS_INT next_boundary;
	      EMACS_INT pos = BYTE_TO_CHAR (pos_byte);
846 847 848 849 850 851
	      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;
852 853
	  /* Compute the next place we need to stop and think,
	     and set STOP accordingly.  */
854
	  stop_pos_byte = min (ceiling, next_boundary_byte);
855
	  /* The -1 and +1 arrange to point at the first byte of gap
856
	     (if STOP_POS_BYTE is the position of the gap)
857
	     rather than at the data after the gap.  */
858

859 860
	  stop = BYTE_POS_ADDR (stop_pos_byte - 1) + 1;
	  p = BYTE_POS_ADDR (pos_byte);
Jim Blandy's avatar
Jim Blandy committed
861 862 863
	}
      switch (*p++)
	{
864 865 866
	case 0240:
	  if (! NILP (current_buffer->enable_multibyte_characters))
	    return column;
Jim Blandy's avatar
Jim Blandy committed
867 868 869 870 871 872 873
	case ' ':
	  column++;
	  break;
	case '\t':
	  column += tab_width - column % tab_width;
	  break;
	default:
874 875 876 877
	  if (ASCII_BYTE_P (p[-1])
	      || NILP (current_buffer->enable_multibyte_characters))
	    return column;
	  {
878 879 880
	    int c;
	    pos_byte = PTR_BYTE_POS (p - 1);
	    c = FETCH_MULTIBYTE_CHAR (pos_byte);
881 882 883
	    if (CHAR_HAS_CATEGORY (c, ' '))
	      {
		column++;
884 885
		INC_POS (pos_byte);
		p = BYTE_POS_ADDR (pos_byte);
886 887 888 889
	      }
	    else
	      return column;
	  }
Jim Blandy's avatar
Jim Blandy committed
890 891 892
	}
    }
}
893 894 895 896

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

898
int
899
indented_beyond_p (pos, pos_byte, column)
900
     int pos, pos_byte;
901
     double column;
902
{
903
  double val;
904 905 906 907 908 909
  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);

910
  val = position_indentation (PT_BYTE);
911
  SET_PT_BOTH (opoint, opoint_byte);
912
  return val >= column;                 /* hmm, float comparison */
913
}
Jim Blandy's avatar
Jim Blandy committed
914

915
DEFUN ("move-to-column", Fmove_to_column, Smove_to_column, 1, 2, "p",
916
       doc: /* Move point to column COLUMN in the current line.
917
Interactively, COLUMN is the value of prefix numeric argument.
918 919 920 921 922 923 924 925 926
The column of a character is calculated by adding together the widths
as displayed of the previous characters in the line.
This function ignores line-continuation;
there is no upper limit on the column number a character can have
and horizontal scrolling has no effect.

If specified column is within a character, point goes after that character.
If it's past end of line, point goes to end of line.

927 928 929 930
Optional second argument FORCE non-nil means if COLUMN is in the
middle of a tab character, change it to spaces.
In addition, if FORCE is t, and the line is too short to reach
COLUMN, add spaces/tabs to get there.
931 932 933

The return value is the current column.  */)
     (column, force)
Jim Blandy's avatar
Jim Blandy committed
934 935
     Lisp_Object column, force;
{
936 937 938 939
  register EMACS_INT pos;
  register EMACS_INT col = current_column ();
  register EMACS_INT goal;
  register EMACS_INT end;
Jim Blandy's avatar
Jim Blandy committed
940
  register int tab_width = XINT (current_buffer->tab_width);
Jim Blandy's avatar
Jim Blandy committed
941
  register int ctl_arrow = !NILP (current_buffer->ctl_arrow);
942
  register struct Lisp_Char_Table *dp = buffer_display_table ();
943
  register int multibyte = !NILP (current_buffer->enable_multibyte_characters);
Jim Blandy's avatar
Jim Blandy committed
944 945

  Lisp_Object val;
946
  EMACS_INT prev_col = 0;
947
  int c = 0;
948
  EMACS_INT next_boundary, pos_byte;
949

950
  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
951
  CHECK_NATNUM (column);
Jim Blandy's avatar
Jim Blandy committed
952 953
  goal = XINT (column);

954
  pos = PT;
955
  pos_byte = PT_BYTE;
Jim Blandy's avatar
Jim Blandy committed
956 957 958 959 960 961
  end = ZV;

  /* If we're starting past the desired column,
     back up to beginning of line and scan from there.  */
  if (col > goal)
    {
962
      end = pos;
963
      pos = current_column_bol_cache;
964
      pos_byte = CHAR_TO_BYTE (pos);
Jim Blandy's avatar
Jim Blandy committed
965 966 967
      col = 0;
    }

968 969
  next_boundary = pos;

970
  while (pos < end)
Jim Blandy's avatar
Jim Blandy committed
971
    {
972 973
      while (pos == next_boundary)
	{
974
	  EMACS_INT prev = pos;
975
	  pos = skip_invisible (pos, &next_boundary, end, Qnil);
976 977
	  if (pos != prev)
	    pos_byte = CHAR_TO_BYTE (pos);
978 979 980 981
	  if (pos >= end)
	    goto endloop;
	}

982 983 984 985 986 987
      /* 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;

988 989 990 991 992 993 994 995 996 997 998 999 1000
      /* Check composition sequence.  */
      {
	int len, len_byte, width;

	if (check_composition (pos, pos_byte, Z, &len, &len_byte, &width))
	  {
	    pos += len;
	    pos_byte += len_byte;
	    col += width;
	    continue;
	  }
      }

1001
      c = FETCH_BYTE (pos_byte);
1002

1003 1004 1005
      /* See if there is a display table and it relates
	 to this character.  */

1006 1007 1008
      if (dp != 0
	  && ! (multibyte && BASE_LEADING_CODE_P (c))
	  && VECTORP (DISP_CHAR_VECTOR (dp, c)))
1009
	{
1010 1011 1012 1013 1014 1015
	  Lisp_Object charvec;
	  EMACS_INT i, n;

	  /* This character is displayed using a vector of glyphs.
	     Update the position based on those glyphs.  */

1016 1017 1018
	  charvec =