1 /* Indentation functions.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
30 #include "intervals.h"
32 /* Indentation can insert tabs if this is non-zero;
33 otherwise always uses spaces */
36 #define min(a, b) ((a) < (b) ? (a) : (b))
37 #define max(a, b) ((a) > (b) ? (a) : (b))
41 /* These three values memoize the current column to avoid recalculation */
42 /* Some things in set last_known_column_point to -1
43 to mark the memoized value as invalid */
44 /* Last value returned by current_column */
45 int last_known_column
;
46 /* Value of point when current_column was called */
47 int last_known_column_point
;
48 /* Value of MODIFF when current_column was called */
49 int last_known_column_modified
;
51 /* Get the display table to use for the current buffer. */
54 buffer_display_table ()
58 thisbuf
= current_buffer
->display_table
;
59 if (XTYPE (thisbuf
) == Lisp_Vector
60 && XVECTOR (thisbuf
)->size
== DISP_TABLE_SIZE
)
61 return XVECTOR (thisbuf
);
62 if (XTYPE (Vstandard_display_table
) == Lisp_Vector
63 && XVECTOR (Vstandard_display_table
)->size
== DISP_TABLE_SIZE
)
64 return XVECTOR (Vstandard_display_table
);
68 DEFUN ("current-column", Fcurrent_column
, Scurrent_column
, 0, 0, 0,
69 "Return the horizontal position of point. Beginning of line is column 0.\n\
70 This is calculated by adding together the widths of all the displayed\n\
71 representations of the character between the start of the previous line\n\
72 and point. (eg control characters will have a width of 2 or 4, tabs\n\
73 will have a variable width)\n\
74 Ignores finite width of frame, which means that this function may return\n\
75 values greater than (frame-width).\n\
76 Whether the line is visible (if `selective-display' is t) has no effect;\n\
77 however, ^M is treated as end of line when `selective-display' is t.")
81 XFASTINT (temp
) = current_column ();
85 /* Cancel any recorded value of the horizontal position. */
87 invalidate_current_column ()
89 last_known_column_point
= 0;
96 register unsigned char *ptr
, *stop
;
97 register int tab_seen
;
100 register int tab_width
= XINT (current_buffer
->tab_width
);
101 int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
102 register struct Lisp_Vector
*dp
= buffer_display_table ();
105 if (point
== last_known_column_point
106 && MODIFF
== last_known_column_modified
)
107 return last_known_column
;
109 /* Make a pointer for decrementing through the chars before point. */
110 ptr
= &FETCH_CHAR (point
- 1) + 1;
111 /* Make a pointer to where consecutive chars leave off,
112 going backwards from point. */
115 else if (point
<= GPT
|| BEGV
> GPT
)
120 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
122 col
= 0, tab_seen
= 0, post_tab
= 0;
128 /* We stopped either for the beginning of the buffer
130 if (ptr
== BEGV_ADDR
)
132 /* It was the gap. Jump back over it. */
135 /* Check whether that brings us to beginning of buffer. */
136 if (BEGV
>= GPT
) break;
140 if (c
>= 040 && c
< 0177
141 && (dp
== 0 || XTYPE (DISP_CHAR_VECTOR (dp
, c
)) != Lisp_Vector
))
147 else if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
152 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
158 else if (dp
!= 0 && XTYPE (DISP_CHAR_VECTOR (dp
, c
)) == Lisp_Vector
)
159 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
161 col
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
166 col
= ((col
+ tab_width
) / tab_width
) * tab_width
;
170 last_known_column
= col
;
171 last_known_column_point
= point
;
172 last_known_column_modified
= MODIFF
;
178 DEFUN ("indent-to", Findent_to
, Sindent_to
, 1, 2, "NIndent to column: ",
179 "Indent from point with tabs and spaces until COLUMN is reached.\n\
180 Optional second argument MIN says always do at least MIN spaces\n\
181 even if that goes past COLUMN; by default, MIN is zero.")
183 Lisp_Object col
, minimum
;
186 register int fromcol
;
187 register int tab_width
= XINT (current_buffer
->tab_width
);
189 CHECK_NUMBER (col
, 0);
191 XFASTINT (minimum
) = 0;
192 CHECK_NUMBER (minimum
, 1);
194 fromcol
= current_column ();
195 mincol
= fromcol
+ XINT (minimum
);
196 if (mincol
< XINT (col
)) mincol
= XINT (col
);
198 if (fromcol
== mincol
)
199 return make_number (mincol
);
201 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
203 if (indent_tabs_mode
)
206 XFASTINT (n
) = mincol
/ tab_width
- fromcol
/ tab_width
;
207 if (XFASTINT (n
) != 0)
209 Finsert_char (make_number ('\t'), n
);
211 fromcol
= (mincol
/ tab_width
) * tab_width
;
215 XFASTINT (col
) = mincol
- fromcol
;
216 Finsert_char (make_number (' '), col
);
218 last_known_column
= mincol
;
219 last_known_column_point
= point
;
220 last_known_column_modified
= MODIFF
;
222 XSETINT (col
, mincol
);
226 DEFUN ("current-indentation", Fcurrent_indentation
, Scurrent_indentation
,
228 "Return the indentation of the current line.\n\
229 This is the horizontal position of the character\n\
230 following any initial whitespace.")
235 XFASTINT (val
) = position_indentation (find_next_newline (point
, -1));
239 position_indentation (pos
)
242 register int column
= 0;
243 register int tab_width
= XINT (current_buffer
->tab_width
);
244 register unsigned char *p
;
245 register unsigned char *stop
;
247 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
249 stop
= &FETCH_CHAR (BUFFER_CEILING_OF (pos
)) + 1;
250 p
= &FETCH_CHAR (pos
);
257 pos
+= p
- &FETCH_CHAR (pos
);
258 p
= &FETCH_CHAR (pos
);
259 stop
= &FETCH_CHAR (BUFFER_CEILING_OF (pos
)) + 1;
267 column
+= tab_width
- column
% tab_width
;
275 /* Test whether the line beginning at POS is indented beyond COLUMN.
276 Blank lines are treated as if they had the same indentation as the
279 indented_beyond_p (pos
, column
)
282 while (pos
> BEGV
&& FETCH_CHAR (pos
) == '\n')
283 pos
= find_next_newline (pos
- 1, -1);
284 return (position_indentation (pos
) >= column
);
287 DEFUN ("move-to-column", Fmove_to_column
, Smove_to_column
, 1, 2, 0,
288 "Move point to column COLUMN in the current line.\n\
289 The column of a character is calculated by adding together the widths\n\
290 as displayed of the previous characters in the line.\n\
291 This function ignores line-continuation;\n\
292 there is no upper limit on the column number a character can have\n\
293 and horizontal scrolling has no effect.\n\
295 If specified column is within a character, point goes after that character.\n\
296 If it's past end of line, point goes to end of line.\n\n\
297 A non-nil second (optional) argument FORCE means, if the line\n\
298 is too short to reach column COLUMN then add spaces/tabs to get there,\n\
299 and if COLUMN is in the middle of a tab character, change it to spaces.")
301 Lisp_Object column
, force
;
304 register int col
= current_column ();
307 register int tab_width
= XINT (current_buffer
->tab_width
);
308 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
309 register struct Lisp_Vector
*dp
= buffer_display_table ();
315 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
316 CHECK_NATNUM (column
, 0);
317 goal
= XINT (column
);
323 /* If we're starting past the desired column,
324 back up to beginning of line and scan from there. */
327 pos
= find_next_newline (pos
, -1);
331 while (col
< goal
&& pos
< end
)
333 c
= FETCH_CHAR (pos
);
336 if (c
== '\r' && EQ (current_buffer
->selective_display
, Qt
))
343 col
= col
/ tab_width
* tab_width
;
345 else if (dp
!= 0 && XTYPE (DISP_CHAR_VECTOR (dp
, c
)) == Lisp_Vector
)
346 col
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
347 else if (ctl_arrow
&& (c
< 040 || c
== 0177))
349 else if (c
< 040 || c
>= 0177)
357 /* If a tab char made us overshoot, change it to spaces
358 and scan through it again. */
359 if (!NILP (force
) && col
> goal
&& c
== '\t' && prev_col
< goal
)
363 del_range (point
- 1, point
);
364 Findent_to (make_number (goal
), Qnil
);
366 Findent_to (make_number (col
), Qnil
);
368 /* Set the last_known... vars consistently. */
372 /* If line ends prematurely, add space to the end. */
373 if (col
< goal
&& !NILP (force
))
374 Findent_to (make_number (col
= goal
), Qnil
);
376 last_known_column
= col
;
377 last_known_column_point
= point
;
378 last_known_column_modified
= MODIFF
;
380 XFASTINT (val
) = col
;
384 struct position val_compute_motion
;
386 /* Scan the current buffer forward from offset FROM, pretending that
387 this is at line FROMVPOS, column FROMHPOS, until reaching buffer
388 offset TO or line TOVPOS, column TOHPOS (whichever comes first),
389 and return the ending buffer position and screen location.
391 WIDTH is the number of columns available to display text;
392 compute_motion uses this to handle continuation lines and such.
393 HSCROLL is the number of columns not being displayed at the left
394 margin; this is usually taken from a window's hscroll member.
395 TAB_OFFSET is the number of columns of the first tab that aren't
396 being displayed, perhaps because of a continuation line or
399 compute_motion returns a pointer to a struct position. The bufpos
400 member gives the buffer position at the end of the scan, and hpos
401 and vpos give its cartesian location. I'm not clear on what the
404 For example, to find the buffer position of column COL of line LINE
405 of a certain window, pass the window's starting location as FROM
406 and the window's upper-left coordinates as FROMVPOS and FROMHPOS.
407 Pass the buffer's ZV as TO, to limit the scan to the end of the
408 visible section of the buffer, and pass LINE and COL as TOVPOS and
411 When displaying in window w, a typical formula for WIDTH is:
414 - (has_vertical_scroll_bars
415 ? VERTICAL_SCROLL_BAR_WIDTH
416 : (window_width + window_left != frame_width))
419 window_width is XFASTINT (w->width),
420 window_left is XFASTINT (w->left),
421 has_vertical_scroll_bars is
422 FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WINDOW_FRAME (window)))
423 and frame_width = FRAME_WIDTH (XFRAME (window->frame))
426 window_internal_width (w) - 1
428 The `-1' accounts for the continuation-line backslashes; the rest
429 accounts for window borders if the window is split horizontally, and
430 the scroll bars if the frame supports them. */
433 compute_motion (from
, fromvpos
, fromhpos
, to
, tovpos
, tohpos
, width
, hscroll
, tab_offset
)
434 int from
, fromvpos
, fromhpos
, to
, tovpos
, tohpos
;
436 int hscroll
, tab_offset
;
438 register int hpos
= fromhpos
;
439 register int vpos
= fromvpos
;
443 register int tab_width
= XFASTINT (current_buffer
->tab_width
);
444 register int ctl_arrow
= !NILP (current_buffer
->ctl_arrow
);
445 register struct Lisp_Vector
*dp
= buffer_display_table ();
447 = XTYPE (current_buffer
->selective_display
) == Lisp_Int
448 ? XINT (current_buffer
->selective_display
)
449 : !NILP (current_buffer
->selective_display
) ? -1 : 0;
450 int prev_vpos
, prev_hpos
;
452 = (selective
&& dp
&& XTYPE (DISP_INVIS_VECTOR (dp
)) == Lisp_Vector
453 ? XVECTOR (DISP_INVIS_VECTOR (dp
))->size
: 0);
454 #ifdef USE_TEXT_PROPERTIES
455 /* The next location where the `invisible' property changes */
456 int next_invisible
= from
;
457 Lisp_Object prop
, position
;
460 if (tab_width
<= 0 || tab_width
> 1000) tab_width
= 8;
461 for (pos
= from
; pos
< to
; pos
++)
463 /* Stop if past the target screen position. */
465 || (vpos
== tovpos
&& hpos
>= tohpos
))
471 #ifdef USE_TEXT_PROPERTIES
472 /* if the `invisible' property is set, we can skip to
473 the next property change */
474 while (pos
== next_invisible
&& pos
< to
)
476 XFASTINT (position
) = pos
;
477 prop
= Fget_text_property (position
,
481 Lisp_Object end
, limit
;
483 /* This is just an estimate to give reasonable
484 performance; nothing should go wrong if it is too small. */
485 XFASTINT (limit
) = pos
+ 100;
486 end
= Fnext_single_property_change (position
, Qinvisible
,
487 Fcurrent_buffer (), limit
);
489 next_invisible
= XINT (end
);
493 pos
= next_invisible
;
499 c
= FETCH_CHAR (pos
);
500 if (c
>= 040 && c
< 0177
501 && (dp
== 0 || XTYPE (DISP_CHAR_VECTOR (dp
, c
)) != Lisp_Vector
))
505 hpos
+= tab_width
- ((hpos
+ tab_offset
+ hscroll
- (hscroll
> 0)
506 /* Add tab_width here to make sure positive.
507 hpos can be negative after continuation
508 but can't be less than -tab_width. */
514 if (selective
> 0 && indented_beyond_p (pos
+ 1, selective
))
516 /* Skip any number of invisible lines all at once */
519 while (++pos
< to
&& FETCH_CHAR (pos
) != '\n');
521 while (pos
< to
&& indented_beyond_p (pos
+ 1, selective
));
522 pos
--; /* Reread the newline on the next pass. */
523 /* Allow for the " ..." that is displayed for them. */
526 hpos
+= selective_rlen
;
530 /* We have skipped the invis text, but not the newline after. */
534 /* A visible line. */
538 if (hscroll
> 0) hpos
++; /* Count the $ on column 0 */
542 else if (c
== CR
&& selective
< 0)
544 /* In selective display mode,
545 everything from a ^M to the end of the line is invisible */
546 while (pos
< to
&& FETCH_CHAR (pos
) != '\n') pos
++;
547 /* Stop *before* the real newline. */
549 /* Allow for the " ..." that is displayed for them. */
552 hpos
+= selective_rlen
;
557 else if (dp
!= 0 && XTYPE (DISP_CHAR_VECTOR (dp
, c
)) == Lisp_Vector
)
558 hpos
+= XVECTOR (DISP_CHAR_VECTOR (dp
, c
))->size
;
560 hpos
+= (ctl_arrow
&& c
< 0200) ? 2 : 4;
562 /* Handle right margin. */
566 && FETCH_CHAR (pos
+ 1) != '\n')))
569 || (vpos
== tovpos
&& hpos
>= tohpos
))
572 || (truncate_partial_width_windows
573 && width
+ 1 < FRAME_WIDTH (selected_frame
))
574 || !NILP (current_buffer
->truncate_lines
))
576 /* Truncating: skip to newline. */
577 while (pos
< to
&& FETCH_CHAR (pos
) != '\n') pos
++;
592 val_compute_motion
.bufpos
= pos
;
593 val_compute_motion
.hpos
= hpos
;
594 val_compute_motion
.vpos
= vpos
;
595 val_compute_motion
.prevhpos
= prev_hpos
;
597 /* Nonzero if have just continued a line */
598 val_compute_motion
.contin
600 && (val_compute_motion
.vpos
!= prev_vpos
)
603 return &val_compute_motion
;
607 /* Return the column of position POS in window W's buffer,
608 rounded down to a multiple of the internal width of W.
609 This is the amount of indentation of position POS
610 that is not visible in its horizontal position in the window. */
613 pos_tab_offset (w
, pos
)
619 int width
= window_internal_width (w
) - 1;
621 if (pos
== BEGV
|| FETCH_CHAR (pos
- 1) == '\n')
624 col
= current_column ();
626 return col
- (col
% width
);
629 /* start_hpos is the hpos of the first character of the buffer:
630 zero except for the minibuffer window,
631 where it is the width of the prompt. */
633 struct position val_vmotion
;
636 vmotion (from
, vtarget
, width
, hscroll
, window
)
637 register int from
, vtarget
, width
;
642 /* vpos is cumulative vertical position, changed as from is changed */
643 register int vpos
= 0;
644 register int prevline
;
646 int lmargin
= hscroll
> 0 ? 1 - hscroll
: 0;
648 = XTYPE (current_buffer
->selective_display
) == Lisp_Int
649 ? XINT (current_buffer
->selective_display
)
650 : !NILP (current_buffer
->selective_display
) ? -1 : 0;
651 int start_hpos
= (EQ (window
, minibuf_window
) ? minibuf_prompt_width
: 0);
656 /* Moving downward is simple, but must calculate from beg of line
657 to determine hpos of starting point */
658 if (from
> BEGV
&& FETCH_CHAR (from
- 1) != '\n')
660 prevline
= find_next_newline (from
, -1);
661 while (prevline
> BEGV
663 && indented_beyond_p (prevline
, selective
))
664 #ifdef USE_TEXT_PROPERTIES
665 /* watch out for newlines with `invisible' property */
666 || ! NILP (Fget_text_property (XFASTINT (prevline
),
671 prevline
= find_next_newline (prevline
- 1, -1);
672 pos
= *compute_motion (prevline
, 0,
673 lmargin
+ (prevline
== 1 ? start_hpos
: 0),
674 from
, 1 << (INTBITS
- 2), 0,
679 pos
.hpos
= lmargin
+ (from
== 1 ? start_hpos
: 0);
682 return compute_motion (from
, vpos
, pos
.hpos
,
683 ZV
, vtarget
, - (1 << (INTBITS
- 2)),
684 width
, hscroll
, pos
.vpos
* width
);
687 /* To move upward, go a line at a time until
688 we have gone at least far enough */
692 while ((vpos
> vtarget
|| first
) && from
> BEGV
)
697 prevline
= find_next_newline (prevline
- 1, -1);
700 || ! indented_beyond_p (prevline
, selective
))
701 #ifdef USE_TEXT_PROPERTIES
702 /* watch out for newlines with `invisible' property */
703 && NILP (Fget_text_property (XFASTINT (prevline
),
710 pos
= *compute_motion (prevline
, 0,
711 lmargin
+ (prevline
== 1 ? start_hpos
: 0),
712 from
, 1 << (INTBITS
- 2), 0,
719 /* If we made exactly the desired vertical distance,
720 or if we hit beginning of buffer,
721 return point found */
724 val_vmotion
.bufpos
= from
;
725 val_vmotion
.vpos
= vpos
;
726 val_vmotion
.hpos
= lmargin
;
727 val_vmotion
.contin
= 0;
728 val_vmotion
.prevhpos
= 0;
732 /* Otherwise find the correct spot by moving down */
736 DEFUN ("vertical-motion", Fvertical_motion
, Svertical_motion
, 1, 1, 0,
737 "Move to start of screen line LINES lines down.\n\
738 If LINES is negative, this is moving up.\n\
739 Sets point to position found; this may be start of line\n\
740 or just the start of a continuation line.\n\
741 Returns number of lines moved; may be closer to zero than LINES\n\
742 if beginning or end of buffer was reached.")
747 register struct window
*w
= XWINDOW (selected_window
);
748 int width
= window_internal_width (w
) - 1;
750 CHECK_NUMBER (lines
, 0);
752 pos
= *vmotion (point
, XINT (lines
), width
,
753 /* Not XFASTINT since perhaps could be negative */
754 XINT (w
->hscroll
), selected_window
);
757 return make_number (pos
.vpos
);
762 DEFVAR_BOOL ("indent-tabs-mode", &indent_tabs_mode
,
763 "*Indentation can insert tabs if this is non-nil.\n\
764 Setting this variable automatically makes it local to the current buffer.");
765 indent_tabs_mode
= 1;
767 defsubr (&Scurrent_indentation
);
768 defsubr (&Sindent_to
);
769 defsubr (&Scurrent_column
);
770 defsubr (&Smove_to_column
);
771 defsubr (&Svertical_motion
);