1 ;;; indent.el --- indentation commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985, 1995, 2001-2016 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Commands for making and changing indentation in text. These are
26 ;; described in the Emacs manual.
31 "Indentation commands."
34 (defcustom standard-indent
4
35 "Default number of columns for margin-changing functions to indent."
39 (defvar indent-line-function
'indent-relative
40 "Function to indent the current line.
41 This function will be called with no arguments.
42 If it is called somewhere where auto-indentation cannot be done
43 \(e.g. inside a string), the function should simply return `noindent'.
44 Setting this function is all you need to make TAB indent appropriately.
45 Don't rebind TAB unless you really need to.")
47 (defcustom tab-always-indent t
48 "Controls the operation of the TAB key.
49 If t, hitting TAB always just indents the current line.
50 If nil, hitting TAB indents the current line if point is at the left margin
51 or in the line's indentation, otherwise it inserts a \"real\" TAB character.
52 If `complete', TAB first tries to indent the current line, and if the line
53 was already indented, then try to complete the thing at point.
55 Some programming language modes have their own variable to control this,
56 e.g., `c-tab-always-indent', and do not respect this variable."
59 (const :tag
"Always indent" t
)
60 (const :tag
"Indent if inside indentation, else TAB" nil
)
61 (const :tag
"Indent, or if already indented complete" complete
)))
64 (defun indent-according-to-mode ()
65 "Indent line in proper way for current major mode.
66 Normally, this is done by calling the function specified by the
67 variable `indent-line-function'. However, if the value of that
68 variable is `indent-relative' or `indent-relative-maybe', handle
69 it specially (since those functions are used for tabbing); in
70 that case, indent by aligning to the previous non-blank line."
72 (syntax-propertize (line-end-position))
73 (if (memq indent-line-function
74 '(indent-relative indent-relative-maybe
))
75 ;; These functions are used for tabbing, but can't be used for
76 ;; indenting. Replace with something ad-hoc.
77 (let ((column (save-excursion
81 (if (looking-at "[ \t]*$") 0
82 (current-indentation))))))
83 (if (<= (current-column) (current-indentation))
84 (indent-line-to column
)
85 (save-excursion (indent-line-to column
))))
87 (funcall indent-line-function
)))
89 (defun indent--default-inside-comment ()
90 (unless (or (> (current-column) (current-indentation))
91 (eq this-command last-command
))
92 (let ((ppss (syntax-ppss)))
97 (skip-chars-forward " \t")
98 (when (< (1- (point)) (nth 8 ppss
) (line-end-position))
99 (goto-char (nth 8 ppss
))
100 (when (looking-at comment-start-skip
)
101 (goto-char (match-end 0))))
105 (defun indent-for-tab-command (&optional arg
)
106 "Indent the current line or region, or insert a tab, as appropriate.
107 This function either inserts a tab, or indents the current line,
108 or performs symbol completion, depending on `tab-always-indent'.
109 The function called to actually indent the line or insert a tab
110 is given by the variable `indent-line-function'.
112 If a prefix argument is given, after this function indents the
113 current line or inserts a tab, it also rigidly indents the entire
114 balanced expression which starts at the beginning of the current
115 line, to reflect the current line's indentation.
117 In most major modes, if point was in the current line's
118 indentation, it is moved to the first non-whitespace character
119 after indenting; otherwise it stays at the same position relative
122 If `transient-mark-mode' is turned on and the region is active,
123 this function instead calls `indent-region'. In this case, any
124 prefix argument is ignored."
127 ;; The region is active, indent it.
129 (indent-region (region-beginning) (region-end)))
130 ((or ;; indent-to-left-margin is only meant for indenting,
131 ;; so we force it to always insert a tab here.
132 (eq indent-line-function
'indent-to-left-margin
)
133 (and (not tab-always-indent
)
134 (or (> (current-column) (current-indentation))
135 (eq this-command last-command
))))
138 (let ((old-tick (buffer-chars-modified-tick))
140 (old-indent (current-indentation)))
143 (or (not (eq (funcall indent-line-function
) 'noindent
))
144 (indent--default-inside-comment)
145 (when (or (<= (current-column) (current-indentation))
146 (not (eq tab-always-indent
'complete
)))
147 (funcall (default-value 'indent-line-function
))))
150 ;; If the text was already indented right, try completion.
151 ((and (eq tab-always-indent
'complete
)
152 (eq old-point
(point))
153 (eq old-tick
(buffer-chars-modified-tick)))
154 (completion-at-point))
156 ;; If a prefix argument was given, rigidly indent the following
157 ;; sexp to match the change in the current line's indentation.
161 (forward-line 0) (forward-sexp) (point-marker)))
162 (indentation-change (- (current-indentation) old-indent
)))
165 (when (and (not (zerop indentation-change
))
166 (< (point) end-marker
))
167 (indent-rigidly (point) end-marker indentation-change
))))))))))
169 (defun insert-tab (&optional arg
)
170 (let ((count (prefix-numeric-value arg
)))
172 (eq (char-syntax (preceding-char)) ?w
))
175 (insert-char ?
\t count
)
176 (indent-to (* tab-width
(+ count
(/ (current-column) tab-width
)))))))
178 (defun indent-rigidly--current-indentation (beg end
)
179 "Return the smallest indentation in range from BEG to END.
180 Blank lines are ignored."
183 (let ((beg (progn (goto-char beg
) (line-beginning-position)))
186 (while (re-search-forward "^\\s-*[[:print:]]" end t
)
187 (setq indent
(min (or indent
(current-indentation))
188 (current-indentation))))
191 (defvar indent-rigidly-map
192 (let ((map (make-sparse-keymap)))
193 (define-key map
[left] 'indent-rigidly-left)
194 (define-key map [right] 'indent-rigidly-right)
195 (define-key map [S-left] 'indent-rigidly-left-to-tab-stop)
196 (define-key map [S-right] 'indent-rigidly-right-to-tab-stop)
198 "Transient keymap for adjusting indentation interactively.
199 It is activated by calling `indent-rigidly' interactively.")
201 (defun indent-rigidly (start end arg &optional interactive)
202 "Indent all lines starting in the region.
203 If called interactively with no prefix argument, activate a
204 transient mode in which the indentation can be adjusted interactively
205 by typing \\<indent-rigidly-map>\\[indent-rigidly-left], \\[indent-rigidly-right], \\[indent-rigidly-left-to-tab-stop], or \\[indent-rigidly-right-to-tab-stop].
206 Typing any other key deactivates the transient mode.
208 If called from a program, or interactively with prefix ARG,
209 indent all lines starting in the region forward by ARG columns.
210 If called from a program, START and END specify the beginning and
211 end of the text to act on, in place of the region.
213 Negative values of ARG indent backward, so you can remove all
214 indentation by specifying a large negative ARG."
215 (interactive "r\nP\np")
216 (if (and (not arg) interactive)
219 (substitute-command-keys
220 "Indent region with \\<indent-rigidly-map>\\[indent-rigidly-left], \\[indent-rigidly-right], \\[indent-rigidly-left-to-tab-stop], or \\[indent-rigidly-right-to-tab-stop]."))
221 (set-transient-map indent-rigidly-map t #'deactivate-mark))
224 (setq end (point-marker))
226 (or (bolp) (forward-line 1))
227 (while (< (point) end)
228 (let ((indent (current-indentation))
231 (skip-chars-forward " \t")
232 (setq eol-flag (eolp)))
234 (indent-to (max 0 (+ indent (prefix-numeric-value arg))) 0))
235 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
237 (move-marker end nil)
238 ;; Keep the active region in transient mode.
239 (when (eq (cadr overriding-terminal-local-map) indent-rigidly-map)
240 (setq deactivate-mark nil)))))
242 (defun indent-rigidly--pop-undo ()
243 (and (memq last-command '(indent-rigidly-left indent-rigidly-right
244 indent-rigidly-left-to-tab-stop
245 indent-rigidly-right-to-tab-stop))
246 (consp buffer-undo-list)
247 (eq (car buffer-undo-list) nil)
248 (pop buffer-undo-list)))
250 (defun indent-rigidly-left (beg end)
251 "Indent all lines between BEG and END leftward by one space."
253 (indent-rigidly--pop-undo)
256 (if (eq (current-bidi-paragraph-direction) 'right-to-left) 1 -1)))
258 (defun indent-rigidly-right (beg end)
259 "Indent all lines between BEG and END rightward by one space."
261 (indent-rigidly--pop-undo)
264 (if (eq (current-bidi-paragraph-direction) 'right-to-left) -1 1)))
266 (defun indent-rigidly-left-to-tab-stop (beg end)
267 "Indent all lines between BEG and END leftward to a tab stop."
269 (indent-rigidly--pop-undo)
270 (let* ((current (indent-rigidly--current-indentation beg end))
271 (rtl (eq (current-bidi-paragraph-direction) 'right-to-left))
272 (next (indent-next-tab-stop current (if rtl nil 'prev))))
273 (indent-rigidly beg end (- next current))))
275 (defun indent-rigidly-right-to-tab-stop (beg end)
276 "Indent all lines between BEG and END rightward to a tab stop."
278 (indent-rigidly--pop-undo)
279 (let* ((current (indent-rigidly--current-indentation beg end))
280 (rtl (eq (current-bidi-paragraph-direction) 'right-to-left))
281 (next (indent-next-tab-stop current (if rtl 'prev))))
282 (indent-rigidly beg end (- next current))))
284 (defun indent-line-to (column)
285 "Indent current line to COLUMN.
286 This function removes or adds spaces and tabs at beginning of line
287 only if necessary. It leaves point at end of indentation."
288 (backward-to-indentation 0)
289 (let ((cur-col (current-column)))
290 (cond ((< cur-col column)
291 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
292 (delete-region (point)
293 (progn (skip-chars-backward " ") (point))))
295 ((> cur-col column) ; too far right (after tab?)
296 (delete-region (progn (move-to-column column t) (point))
297 (progn (backward-to-indentation 0) (point)))))))
299 (defun current-left-margin ()
300 "Return the left margin to use for this line.
301 This is the value of the buffer-local variable `left-margin' plus the value
302 of the `left-margin' text-property at the start of the line."
304 (back-to-indentation)
306 (+ left-margin (or (get-text-property
307 (if (and (eobp) (not (bobp)))
308 (1- (point)) (point))
311 (defun move-to-left-margin (&optional n force)
312 "Move to the left margin of the current line.
313 With optional argument, move forward N-1 lines first.
314 The column moved to is the one given by the `current-left-margin' function.
315 If the line's indentation appears to be wrong, and this command is called
316 interactively or with optional argument FORCE, it will be fixed."
317 (interactive (list (prefix-numeric-value current-prefix-arg) t))
318 (beginning-of-line n)
319 (skip-chars-forward " \t")
320 (if (minibufferp (current-buffer))
321 (if (save-excursion (beginning-of-line) (bobp))
322 (goto-char (minibuffer-prompt-end))
324 (let ((lm (current-left-margin))
325 (cc (current-column)))
327 (if (> (move-to-column lm force) lm)
328 ;; If lm is in a tab and we are not forcing, move before tab
330 ((and force (< cc lm))
331 (indent-to-left-margin))))))
333 ;; This used to be the default indent-line-function,
334 ;; used in Fundamental Mode, Text Mode, etc.
335 (defun indent-to-left-margin ()
336 "Indent current line to the column given by `current-left-margin'."
337 (save-excursion (indent-line-to (current-left-margin)))
338 ;; If we are within the indentation, move past it.
339 (when (save-excursion
340 (skip-chars-backward " \t")
342 (skip-chars-forward " \t")))
344 (defun delete-to-left-margin (&optional from to)
345 "Remove left margin indentation from a region.
346 This deletes to the column given by `current-left-margin'.
347 In no case will it delete non-whitespace.
348 Args FROM and TO are optional; default is the whole buffer."
350 (goto-char (or to (point-max)))
351 (setq to (point-marker))
352 (goto-char (or from (point-min)))
353 (or (bolp) (forward-line 1))
354 (while (< (point) to)
355 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
357 (move-marker to nil)))
359 (defun set-left-margin (from to width)
360 "Set the left margin of the region to WIDTH.
361 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
363 Interactively, WIDTH is the prefix argument, if specified.
364 Without prefix argument, the command prompts for WIDTH."
365 (interactive "r\nNSet left margin to column: ")
367 ;; If inside indentation, start from BOL.
369 (skip-chars-backward " \t")
370 (if (bolp) (setq from (point)))
371 ;; Place end after whitespace
373 (skip-chars-forward " \t")
374 (setq to (point-marker)))
375 ;; Delete margin indentation first, but keep paragraph indentation.
376 (delete-to-left-margin from to)
377 (put-text-property from to 'left-margin width)
378 (indent-rigidly from to width)
379 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
380 (move-marker to nil))
382 (defun set-right-margin (from to width)
383 "Set the right margin of the region to WIDTH.
384 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
386 Interactively, WIDTH is the prefix argument, if specified.
387 Without prefix argument, the command prompts for WIDTH."
388 (interactive "r\nNSet right margin to width: ")
391 (skip-chars-backward " \t")
392 (if (bolp) (setq from (point))))
393 (put-text-property from to 'right-margin width)
394 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
396 (defun alter-text-property (from to prop func &optional object)
397 "Programmatically change value of a text-property.
398 For each region between FROM and TO that has a single value for PROPERTY,
399 apply FUNCTION to that value and sets the property to the function's result.
400 Optional fifth argument OBJECT specifies the string or buffer to operate on."
403 (while (setq val (get-text-property begin prop object)
404 end (text-property-not-all begin to prop val object))
405 (put-text-property begin end prop (funcall func val) object)
408 (put-text-property begin to prop (funcall func val) object))))
410 (defun increase-left-margin (from to inc)
411 "Increase or decrease the left-margin of the region.
412 With no prefix argument, this adds `standard-indent' of indentation.
413 A prefix arg (optional third arg INC noninteractively) specifies the amount
414 to change the margin by, in characters.
415 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
416 (interactive "*r\nP")
417 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
420 (skip-chars-backward " \t")
421 (if (bolp) (setq from (point)))
423 (setq to (point-marker)))
424 (alter-text-property from to 'left-margin
425 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
426 (indent-rigidly from to inc)
427 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
428 (move-marker to nil))
430 (defun decrease-left-margin (from to inc)
431 "Make the left margin of the region smaller.
432 With no prefix argument, decrease the indentation by `standard-indent'.
433 A prefix arg (optional third arg INC noninteractively) specifies the amount
434 to change the margin by, in characters.
435 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
436 (interactive "*r\nP")
437 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
438 (increase-left-margin from to (- inc)))
440 (defun increase-right-margin (from to inc)
441 "Increase the right-margin of the region.
442 With no prefix argument, increase the right margin by `standard-indent'.
443 A prefix arg (optional third arg INC noninteractively) specifies the amount
444 to change the margin by, in characters. A negative argument decreases
445 the right margin width.
446 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
448 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
450 (alter-text-property from to 'right-margin
451 (lambda (v) (+ inc (or v 0))))
452 (if auto-fill-function
453 (fill-region from to nil t t))))
455 (defun decrease-right-margin (from to inc)
456 "Make the right margin of the region smaller.
457 With no prefix argument, decrease the right margin by `standard-indent'.
458 A prefix arg (optional third arg INC noninteractively) specifies the amount
459 of width to remove, in characters. A negative argument increases
460 the right margin width.
461 If `auto-fill-mode' is active, re-fills region to fit in new margin."
462 (interactive "*r\nP")
463 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
464 (increase-right-margin from to (- inc)))
466 (defun beginning-of-line-text (&optional n)
467 "Move to the beginning of the text on this line.
468 With optional argument, move forward N-1 lines first.
469 From the beginning of the line, moves past the left-margin indentation, the
470 fill-prefix, and any indentation used for centering or right-justifying the
471 line, but does not move past any whitespace that was explicitly inserted
472 \(such as a tab used to indent the first line of a paragraph)."
474 (beginning-of-line n)
475 (skip-chars-forward " \t")
476 ;; Skip over fill-prefix.
478 (not (string-equal fill-prefix "")))
479 (if (equal fill-prefix
481 (point) (min (point-max) (+ (length fill-prefix) (point)))))
482 (forward-char (length fill-prefix)))
483 (if (and adaptive-fill-mode adaptive-fill-regexp
484 (looking-at adaptive-fill-regexp))
485 (goto-char (match-end 0))))
486 ;; Skip centering or flushright indentation
487 (if (memq (current-justification) '(center right))
488 (skip-chars-forward " \t")))
490 (defvar indent-region-function nil
491 "Short cut function to indent region using `indent-according-to-mode'.
492 A value of nil means really run `indent-according-to-mode' on each line.")
494 (defun indent-region (start end &optional column)
495 "Indent each nonblank line in the region.
496 A numeric prefix argument specifies a column: indent each line to that column.
498 With no prefix argument, the command chooses one of these methods and
499 indents all the lines with it:
501 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
502 beginning of each line in the region that does not already begin
504 2) If `indent-region-function' is non-nil, call that function
505 to indent the region.
506 3) Indent each line via `indent-according-to-mode'.
508 Called from a program, START and END specify the region to indent.
509 If the third argument COLUMN is an integer, it specifies the
510 column to indent to; if it is nil, use one of the three methods above."
513 ;; If a numeric prefix is given, indent to that column.
515 (setq column (prefix-numeric-value column))
518 (setq end (point-marker))
520 (or (bolp) (forward-line 1))
521 (while (< (point) end)
522 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
524 (indent-to column 0))
526 (move-marker end nil)))
527 ;; If a fill-prefix is specified, use it.
531 (setq end (point-marker))
533 (let ((regexp (regexp-quote fill-prefix)))
534 (while (< (point) end)
535 (or (looking-at regexp)
537 (insert fill-prefix))
539 ;; Use indent-region-function is available.
540 (indent-region-function
541 (funcall indent-region-function start end))
542 ;; Else, use a default implementation that calls indent-line-function on
546 (setq end (copy-marker end))
548 (let ((pr (unless (minibufferp)
549 (make-progress-reporter "Indenting region..." (point) end))))
550 (while (< (point) end)
551 (or (and (bolp) (eolp))
552 (indent-according-to-mode))
554 (and pr (progress-reporter-update pr (point))))
555 (and pr (progress-reporter-done pr))
556 (move-marker end nil)))))
557 ;; In most cases, reindenting modifies the buffer, but it may also
558 ;; leave it unmodified, in which case we have to deactivate the mark
560 (setq deactivate-mark t))
562 (defun indent-relative-maybe ()
563 "Indent a new line like previous nonblank line.
564 If the previous nonblank line has no indent points beyond the
565 column point starts at, this command does nothing.
567 See also `indent-relative'."
571 (defun indent-relative (&optional unindented-ok)
572 "Space out to under next indent point in previous nonblank line.
573 An indent point is a non-whitespace character following whitespace.
574 The following line shows the indentation points in this line.
576 If the previous nonblank line has no indent points beyond the
577 column point starts at, `tab-to-tab-stop' is done instead, unless
578 this command is invoked with a numeric argument, in which case it
581 See also `indent-relative-maybe'."
584 (eq (char-syntax (preceding-char)) ?w))
586 (let ((start-column (current-column))
590 (if (re-search-backward "^[^\n]" nil t)
591 (let ((end (save-excursion (forward-line 1) (point))))
592 (move-to-column start-column)
593 ;; Is start-column inside a tab on this line?
594 (if (> (current-column) start-column)
596 (or (looking-at "[ \t]")
598 (skip-chars-forward "^ \t" end))
599 (skip-chars-forward " \t" end)
600 (or (= (point) end) (setq indent (current-column))))))
602 (let ((opoint (point-marker)))
604 (if (> opoint (point))
606 (move-marker opoint nil))
609 (defcustom tab-stop-list nil
610 "List of tab stop positions used by `tab-to-tab-stop'.
611 This should be nil, or a list of integers, ordered from smallest to largest.
612 It implicitly extends to infinity through repetition of the last step.
613 For example, (1 2 5) is equivalent to (1 2 5 8 11 ...). If the list has
614 fewer than 2 elements, `tab-width' is used as the \"last step\".
615 A value of nil means a tab stop every `tab-width' columns."
617 :version "24.4" ; from explicit list to nil
619 :type '(repeat integer))
621 (defvar edit-tab-stops-map
622 (let ((map (make-sparse-keymap)))
623 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
624 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
626 "Keymap used in `edit-tab-stops'.")
628 (defvar edit-tab-stops-buffer nil
629 "Buffer whose tab stops are being edited.
630 This matters if the variable `tab-stop-list' is local in that buffer.")
632 (defun edit-tab-stops ()
633 "Edit the tab stops used by `tab-to-tab-stop'.
634 Creates a buffer *Tab Stops* containing text describing the tab stops.
635 A colon indicates a column where there is a tab stop.
636 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
638 (setq edit-tab-stops-buffer (current-buffer))
639 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
640 (use-local-map edit-tab-stops-map)
641 (setq-local indent-tabs-mode nil)
643 (setq truncate-lines t)
645 (let ((tabs tab-stop-list))
647 (indent-to (car tabs) 0)
649 (setq tabs (cdr tabs))))
653 (insert (+ count ?0))
655 (setq count (1+ count)))
658 (insert "0123456789")
659 (setq count (1- count))))
660 (insert "\nTo install changes, type C-c C-c")
661 (goto-char (point-min)))
663 (defun edit-tab-stops-note-changes ()
664 "Put edited tab stops into effect."
670 (while (search-backward ":" nil t)
671 (setq tabs (cons (current-column) tabs))))
672 (bury-buffer (prog1 (current-buffer)
673 (switch-to-buffer edit-tab-stops-buffer)))
674 (setq tab-stop-list tabs))
675 (message "Tab stops installed"))
677 (defun indent-next-tab-stop (column &optional prev)
678 "Return the next tab stop after COLUMN.
679 If PREV is non-nil, return the previous one instead."
680 (let ((tabs tab-stop-list))
681 (while (and tabs (>= column (car tabs)))
682 (setq tabs (cdr tabs)))
686 (let ((prevtabs (cdr (memq (car tabs) (reverse tab-stop-list)))))
687 (if (null prevtabs) 0
688 (if (= column (car prevtabs))
689 (or (nth 1 prevtabs) 0)
691 ;; We passed the end of tab-stop-list: guess a continuation.
692 (let* ((last2 (last tab-stop-list 2))
693 (step (if (cdr last2) (- (cadr last2) (car last2)) tab-width))
694 (last (or (cadr last2) (car last2) 0)))
695 ;; Repeat the last tab's length.
696 (+ last (* step (if prev
697 (if (<= column last) -1 (/ (- column last 1) step))
698 (1+ (/ (- column last) step)))))))))
700 (defun indent-accumulate-tab-stops (limit)
701 "Get a list of tab stops before LIMIT (inclusive)."
702 (let ((tab 0) (tab-stops))
703 (while (<= (setq tab (indent-next-tab-stop tab)) limit)
704 (push tab tab-stops))
705 (nreverse tab-stops)))
707 (defun tab-to-tab-stop ()
708 "Insert spaces or tabs to next defined tab-stop column.
709 The variable `tab-stop-list' is a list of columns at which there are tab stops.
710 Use \\[edit-tab-stops] to edit them interactively."
712 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
714 (let ((nexttab (indent-next-tab-stop (current-column))))
715 (delete-horizontal-space t)
716 (indent-to nexttab)))
718 (defun move-to-tab-stop ()
719 "Move point to next defined tab-stop column.
720 The variable `tab-stop-list' is a list of columns at which there are tab stops.
721 Use \\[edit-tab-stops] to edit them interactively."
723 (let ((nexttab (indent-next-tab-stop (current-column))))
724 (let ((before (point)))
725 (move-to-column nexttab t)
728 ;; If we just added a tab, or moved over one,
729 ;; delete any superfluous spaces before the old point.
730 (if (and (eq (preceding-char) ?\s)
731 (eq (following-char) ?\t))
732 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
733 (while (and (> (current-column) tabend)
734 (eq (preceding-char) ?\s))
736 (delete-region (point) before)))))))
738 (define-key global-map "\t" 'indent-for-tab-command)
739 (define-key esc-map "\C-\\" 'indent-region)
740 (define-key ctl-x-map "\t" 'indent-rigidly)
741 (define-key esc-map "i" 'tab-to-tab-stop)
743 ;;; indent.el ends here