Source

XEmacs / src / indent.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/* Indentation functions.
   Copyright (C) 1995 Board of Trustees, University of Illinois.
   Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995
   Free Software Foundation, Inc.

This file is part of XEmacs.

XEmacs is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

XEmacs 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 XEmacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* This file has been Mule-ized. */

/* Synched up with: 19.30.  Diverges significantly from FSF. */


#include <config.h>
#include "lisp.h"

#include "buffer.h"
#include "device.h"
#include "extents.h"
#include "faces.h"
#include "frame.h"
#include "glyphs.h"
#include "insdel.h"
#ifdef REGION_CACHE_NEEDS_WORK
#include "region-cache.h"
#endif
#include "window.h"

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

/* Avoid recalculation by remembering things in these variables. */

/* Last value returned by current_column.

   Some things set last_known_column_point to -1
   to mark the memoized value as invalid */
static int last_known_column;

/* Last buffer searched by current_column */
static struct buffer *last_known_column_buffer;

/* Value of point when current_column was called */
static Bufpos last_known_column_point;

/* Value of MODIFF when current_column was called */
static int last_known_column_modified;

static Bufpos
last_visible_position (Bufpos pos, struct buffer *buf)
{
  Lisp_Object buffer;
  Lisp_Object value;

  XSETBUFFER (buffer, buf);
  value = Fprevious_single_property_change (make_int (pos), Qinvisible,
					    buffer, Qnil);
  if (NILP (value))
    return 0; /* no visible position found */
  else
    /* #### bug bug bug!!! This will return the position of the beginning
       of an invisible extent; this extent is very likely to be start-closed,
       and thus the spaces inserted in `indent-to' will go inside the
       invisible extent.

       Not sure what the correct solution is here.  Rethink indent-to? */
    return XINT (value);
}

#ifdef REGION_CACHE_NEEDS_WORK

/* 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 (struct buffer *buf)
{
  if (NILP (buf->cache_long_line_scans))
    {
      /* It should be off.  */
      if (buf->width_run_cache)
        {
          free_region_cache (buf->width_run_cache);
          buf->width_run_cache = 0;
          buf->width_table = Qnil;
        }
    }
  else
    {
      /* It should be on.  */
      if (buf->width_run_cache == 0)
        {
          buf->width_run_cache = new_region_cache ();
          recompute_width_table (buf, buffer_display_table ());
        }
    }
}

#endif /* REGION_CACHE_NEEDS_WORK */


/* Cancel any recorded value of the horizontal position.  */
 
void
invalidate_current_column (void)
{
  last_known_column_point = -1;
}

int
column_at_point (struct buffer *buf, Bufpos init_pos, int cur_col)
{
  int col;
  int tab_seen;
  int tab_width = XINT (buf->tab_width);
  int post_tab;
  Bufpos pos = init_pos;
  Emchar c;

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

  while (1)
    {
      if (pos <= BUF_BEGV (buf))
	break;

      pos--;
      c = BUF_FETCH_CHAR (buf, pos);
      if (c == '\t')
	{
	  if (tab_seen)
	    col = ((col + tab_width) / tab_width) * tab_width;

	  post_tab += col;
	  col = 0;
	  tab_seen = 1;
	}
      else if (c == '\n' ||
	       (EQ (buf->selective_display, Qt) && c == '\r'))
	break;
      else
	{
	  /* #### This needs updating to handle the new redisplay. */
	  /* #### FSFmacs looks at ctl_arrow, display tables.
	     We need to do similar. */
#if 0
	  displayed_glyphs = glyphs_from_bufpos (sel_frame, buf,
						 XWINDOW (selected_window),
						 pos, dp, 0, col, 0, 0, 0);
	  col += (displayed_glyphs->columns
		  - (displayed_glyphs->begin_columns
		     + displayed_glyphs->end_columns));
#else /* XEmacs */
	  col ++;
#endif /* XEmacs */
	}
    }

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

  if (cur_col)
    {
      last_known_column_buffer = buf;
      last_known_column = col;
      last_known_column_point = BUF_PT (buf);
      last_known_column_modified = BUF_MODIFF (buf);
    }

  return col;
}

int
current_column (struct buffer *buf)
{
  if (buf == last_known_column_buffer
      && BUF_PT (buf) == last_known_column_point
      && BUF_MODIFF (buf) == last_known_column_modified)
    return last_known_column;

  return column_at_point (buf, BUF_PT (buf), 1);
}

DEFUN ("current-column", Fcurrent_column, 0, 1, 0, /*
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
 and point. (e.g. control characters will have a width of 2 or 4, tabs
 will have a variable width.)
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;
 however, ^M is treated as end of line when `selective-display' is t.
If BUFFER is nil, the current buffer is assumed.
*/
       (buffer))
{
  return (make_int (current_column (decode_buffer (buffer, 0))));
}


DEFUN ("indent-to", Findent_to, 1, 3, "NIndent to column: ", /*
Indent from point with tabs and spaces until COLUMN is reached.
Optional second argument MIN says always do at least MIN spaces
 even if that goes past COLUMN; by default, MIN is zero.
If BUFFER is nil, the current buffer is assumed.
*/
       (col, minimum, buffer))
{
  /* This function can GC */
  int mincol;
  int fromcol;
  struct buffer *buf = decode_buffer (buffer, 0);
  int tab_width = XINT (buf->tab_width);
  Bufpos opoint = 0;

  CHECK_INT (col);
  if (NILP (minimum))
    minimum = Qzero;
  else
    CHECK_INT (minimum);

  XSETBUFFER (buffer, buf);
  
  fromcol = current_column (buf);
  mincol = fromcol + XINT (minimum);
  if (mincol < XINT (col)) mincol = XINT (col);

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

  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  
  if (!NILP (Fextent_at (make_int (BUF_PT (buf)), buffer, Qinvisible,
			 Qnil, Qnil)))
    {
      Bufpos last_visible = last_visible_position (BUF_PT (buf), buf);

      opoint = BUF_PT (buf);
      if (last_visible >= BUF_BEGV (buf))
	BUF_SET_PT (buf, last_visible);
      else 
        error ("Visible portion of buffer not modifiable");
    }

  if (indent_tabs_mode)
    {
      int n = mincol / tab_width - fromcol / tab_width;
      if (n != 0)
	{
	  Finsert_char (make_char ('\t'), make_int (n), Qnil, buffer);

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

  Finsert_char (make_char (' '), make_int (mincol - fromcol), Qnil, buffer);

  last_known_column_buffer = buf;
  last_known_column = mincol;
  last_known_column_point = BUF_PT (buf);
  last_known_column_modified = BUF_MODIFF (buf);

  /* Not in FSF: */
  if (opoint > 0)
    BUF_SET_PT (buf, opoint);

  return (make_int (mincol));
}

int
bi_spaces_at_point (struct buffer *b, Bytind bi_pos)
{
  Bytind bi_end = BI_BUF_ZV (b);
  int col = 0;
  Emchar c;
  int tab_width = XINT (b->tab_width);

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

  while (bi_pos < bi_end &&
	 (c = BI_BUF_FETCH_CHAR (b, bi_pos),
	  (c == '\t'
	   ? (col += tab_width - col % tab_width)
	   : (c == ' ' ? ++col : 0))))
    INC_BYTIND (b, bi_pos);

  return col;
}


DEFUN ("current-indentation", Fcurrent_indentation, 0, 1, 0, /*
Return the indentation of the current line.
This is the horizontal position of the character
following any initial whitespace.
*/
       (buffer))
{
  struct buffer *buf = decode_buffer (buffer, 0);
  Bufpos pos = find_next_newline (buf, BUF_PT (buf), -1);

  XSETBUFFER (buffer, buf);

  if (!NILP (Fextent_at (make_int (pos), buffer, Qinvisible, Qnil, Qnil)))
    return Qzero;

  return make_int (bi_spaces_at_point (buf, bufpos_to_bytind (buf, pos)));
}


DEFUN ("move-to-column", Fmove_to_column, 1, 3, 0, /*
Move point to column COLUMN in the current line.
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.

A non-nil second (optional) argument FORCE means, if the line
is too short to reach column COLUMN then add spaces/tabs to get there,
and if COLUMN is in the middle of a tab character, change it to spaces.
Returns the actual column that it moved to.
*/
       (column, force, buffer))
{
  /* This function can GC */
  Bufpos pos;
  struct buffer *buf = decode_buffer (buffer, 0);
  int col = current_column (buf);
  int goal;
  Bufpos end;
  int tab_width = XINT (buf->tab_width);

  int prev_col = 0;
  Emchar c;

  XSETBUFFER (buffer, buf);
  if (tab_width <= 0 || tab_width > 1000) tab_width = 8;
  CHECK_NATNUM (column);
  goal = XINT (column);

 retry:
  pos = BUF_PT (buf);
  end = BUF_ZV (buf);

  /* If we're starting past the desired column,
     back up to beginning of line and scan from there.  */
  if (col > goal)
    {
      pos = find_next_newline (buf, pos, -1);
      col = 0;
    }

  while (col < goal && pos < end)
    {
      c = BUF_FETCH_CHAR (buf, pos);
      if (c == '\n')
	break;
      if (c == '\r' && EQ (buf->selective_display, Qt))
	break;
      if (c == '\t')
	{
	  prev_col = col;
	  col += tab_width;
	  col = col / tab_width * tab_width;
	}
      else
	{
	  /* #### oh for the days of the complete new redisplay */
	  /* #### FSFmacs looks at ctl_arrow, display tables.
	     We need to do similar. */
#if 0
	  displayed_glyphs = glyphs_from_bufpos (selected_frame (),
						 buf,
						 XWINDOW (Fselected_window (Qnil)),
						 pos, dp, 0, col, 0, 0, 0);
	  col += (displayed_glyphs->columns
		  - (displayed_glyphs->begin_columns
		     + displayed_glyphs->end_columns));
#else /* XEmacs */
	  col ++;
#endif /* XEmacs */
	}

      pos++;
    }

  BUF_SET_PT (buf, pos);

  /* If a tab char made us overshoot, change it to spaces
     and scan through it again.  */
  if (!NILP (force) && col > goal && c == '\t' && prev_col < goal)
    {
      buffer_delete_range (buf, BUF_PT (buf) - 1, BUF_PT (buf), 0);
      Findent_to (make_int (col - 1), Qzero, buffer);
      buffer_insert_emacs_char (buf, ' ');
      goto retry;
    }

  /* If line ends prematurely, add space to the end.  */
  if (col < goal && !NILP (force))
    {
      col = goal;
      Findent_to (make_int (col), Qzero, buffer);
    }

  last_known_column_buffer = buf;
  last_known_column = col;
  last_known_column_point = BUF_PT (buf);
  last_known_column_modified = BUF_MODIFF (buf);

  return (make_int (col));
}

#if 0 /* #### OK boys, this function needs to be present, I think.
	 It was there before the 19.12 redisplay rewrite. */

xxDEFUN ("compute-motion", Fcompute_motion, Scompute_motion, 7, 7, 0 /*
  "Scan through the current buffer, calculating screen position.
Scan the current buffer forward from offset FROM,
assuming it is at position FROMPOS--a cons of the form (HPOS . VPOS)--
to position TO or position TOPOS--another cons of the form (HPOS . VPOS)--
and return the ending buffer position and screen location.

There are three additional arguments:

WIDTH is the number of columns available to display text;
this affects handling of continuation lines.
This is usually the value returned by `window-width', less one (to allow
for the continuation glyph).

OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
HSCROLL is the number of columns not being displayed at the left
margin; this is usually taken from a window's hscroll member.
TAB-OFFSET is the number of columns of the first tab that aren't
being displayed, perhaps because the line was continued within it.
If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.

WINDOW is the window to operate on.  Currently this is used only to
find the display table.  It does not matter what buffer WINDOW displays;
`compute-motion' always operates on the current buffer.

The value is a list of five elements:
  (POS HPOS VPOS PREVHPOS CONTIN)
POS is the buffer position where the scan stopped.
VPOS is the vertical position where the scan stopped.
HPOS is the horizontal position where the scan stopped.

PREVHPOS is the horizontal position one character back from POS.
CONTIN is t if a line was continued after (or within) the previous character.

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 FROMPOS.
Pass the buffer's (point-max) as TO, to limit the scan to the end of the
visible section of the buffer, and pass LINE and COL as TOPOS.
*/ )
  (from, frompos, to, topos, width, offsets, window)
     Lisp_Object from, frompos, to, topos;
     Lisp_Object width, offsets, window;
{
  Lisp_Object bufpos, hpos, vpos, prevhpos, contin;
  struct position *pos;
  int hscroll, tab_offset;
  struct window *w = decode_window (window);

  CHECK_INT_COERCE_MARKER (from);
  CHECK_CONS (frompos);
  CHECK_INT (XCAR (frompos));
  CHECK_INT (XCDR (frompos));
  CHECK_INT_COERCE_MARKER (to);
  CHECK_CONS (topos);
  CHECK_INT (XCAR (topos));
  CHECK_INT (XCDR (topos));
  CHECK_INT (width);
  if (!NILP (offsets))
    {
      CHECK_CONS (offsets);
      CHECK_INT (XCAR (offsets));
      CHECK_INT (XCDR (offsets));
      hscroll = XINT (XCAR (offsets));
      tab_offset = XINT (XCDR (offsets));
    }
  else
    hscroll = tab_offset = 0;

  pos = compute_motion (XINT (from), XINT (XCDR (frompos)),
			XINT (XCAR (frompos)),
			XINT (to), XINT (XCDR (topos)),
			XINT (XCAR (topos)),
			XINT (width), hscroll, tab_offset, w);

  XSETINT (bufpos, pos->bufpos);
  XSETINT (hpos, pos->hpos);
  XSETINT (vpos, pos->vpos);
  XSETINT (prevhpos, pos->prevhpos);

  return list5 (bufpos,
		hpos,
		vpos,
		prevhpos,
		pos->contin ? Qt : Qnil);

}

#endif /* 0 */

/*****************************************************************************
 vmotion

 Given a starting position ORIG, move point VTARGET lines in WINDOW.
 Returns the new value for point.  If the arg ret_vpos is not nil, it is
 taken to be a pointer to an int and the number of lines actually moved is
 returned in it.
 ****************************************************************************/
Bufpos
vmotion (struct window *w, Bufpos orig, int vtarget, int *ret_vpos)
{
  struct buffer *b = XBUFFER (w->buffer);
  int elt;

  elt = point_in_line_start_cache (w, orig, (vtarget < 0
					     ? -vtarget
					     : vtarget));

  /* #### This assertion must be true before the if statements are hit
     but may possibly be wrong after the call to
     point_in_line_start_cache if orig is outside of the visible
     region of the buffer.  Handle this. */
  assert (elt >= 0);

  /* Moving downward. */
  if (vtarget > 0)
    {
      int cur_line = Dynarr_length (w->line_start_cache) - 1 - elt;
      Bufpos ret_pt;

      if (cur_line > vtarget)
	cur_line = vtarget;

      /* The traditional FSF behavior is to return the end of buffer
         position if we couldn't move far enough because we hit it.  */
      if (cur_line < vtarget)
	ret_pt = BUF_ZV (b);
      else
	ret_pt = Dynarr_atp (w->line_start_cache, cur_line + elt)->start;

      while (ret_pt > BUF_ZV (b) && cur_line > 0)
	{
	  cur_line--;
	  ret_pt = Dynarr_atp (w->line_start_cache, cur_line + elt)->start;
	}

      if (ret_vpos) *ret_vpos = cur_line;
      return (ret_pt);
    }
  else if (vtarget < 0)
    {
      if (elt < -vtarget)
	{
	  if (ret_vpos) *ret_vpos = -elt;
	  /* #### This should be BUF_BEGV (b), right? */
	  return (Dynarr_atp (w->line_start_cache, 0)->start);
	}
      else
	{
	  if (ret_vpos) *ret_vpos = vtarget;
	  return (Dynarr_atp (w->line_start_cache, elt + vtarget)->start);
	}
    }
  else
    {
      /* No vertical motion requested so we just return the position
         of the beginning of the current line. */
      if (ret_vpos) *ret_vpos = 0;

      return (Dynarr_atp (w->line_start_cache, elt)->start);
    }

  RETURN_NOT_REACHED(0)	/* shut up compiler */
}

DEFUN ("vertical-motion", Fvertical_motion, 1, 2, 0, /*
Move to start of frame line LINES lines down.
If LINES is negative, this is moving up.

The optional second argument WINDOW specifies the window to use for
parameters such as width, horizontal scrolling, and so on.
the default is the selected window.
Note that `vertical-motion' sets WINDOW's buffer's point, not
WINDOW's point. (This differs from FSF Emacs, which buggily always
sets current buffer's point, regardless of WINDOW.)

Sets point to position found; this may be start of line
 or just the start of a continuation line.
Returns number of lines moved; may be closer to zero than LINES
 if beginning or end of buffer was reached.
Optional second argument is WINDOW to move in.
*/
       (lines, window))
{
  if (NILP (window))
    window = Fselected_window (Qnil);
  CHECK_WINDOW (window);
  {
    Bufpos bufpos;
    int vpos;
    struct window *w  = XWINDOW (window);

    CHECK_INT (lines);

    bufpos = vmotion (XWINDOW (window), BUF_PT (XBUFFER (w->buffer)),
		      XINT (lines), &vpos);

    /* Note that the buffer's point is set, not the window's point. */
    BUF_SET_PT (XBUFFER (w->buffer), bufpos);

    return make_int (vpos);
  }
}


void
syms_of_indent (void)
{
  DEFSUBR (Fcurrent_indentation);
  DEFSUBR (Findent_to);
  DEFSUBR (Fcurrent_column);
  DEFSUBR (Fmove_to_column);
#if 0 /* #### */
  DEFSUBR (Fcompute_motion);
#endif
  DEFSUBR (Fvertical_motion);
}

void
vars_of_indent (void)
{
  DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode /*
*Indentation can insert tabs if this is non-nil.
Setting this variable automatically makes it local to the current buffer.
*/ );
  indent_tabs_mode = 1;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.