1 ;;; indent.el --- indentation commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985, 1995, 2001-2017 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 <https://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."
74 (syntax-propertize (line-end-position))
75 (if (memq indent-line-function
76 '(indent-relative indent-relative-maybe
))
77 ;; These functions are used for tabbing, but can't be used for
78 ;; indenting. Replace with something ad-hoc.
79 (let ((column (save-excursion
83 (if (looking-at "[ \t]*$") 0
84 (current-indentation))))))
85 (if (<= (current-column) (current-indentation))
86 (indent-line-to column
)
87 (save-excursion (indent-line-to column
))))
89 (funcall indent-line-function
))))
91 (defun indent--default-inside-comment ()
92 (unless (or (> (current-column) (current-indentation))
93 (eq this-command last-command
))
94 (let ((ppss (syntax-ppss)))
99 (skip-chars-forward " \t")
100 (when (< (1- (point)) (nth 8 ppss
) (line-end-position))
101 (goto-char (nth 8 ppss
))
102 (when (looking-at comment-start-skip
)
103 (goto-char (match-end 0))))
107 (defun indent-for-tab-command (&optional arg
)
108 "Indent the current line or region, or insert a tab, as appropriate.
109 This function either inserts a tab, or indents the current line,
110 or performs symbol completion, depending on `tab-always-indent'.
111 The function called to actually indent the line or insert a tab
112 is given by the variable `indent-line-function'.
114 If a prefix argument is given, after this function indents the
115 current line or inserts a tab, it also rigidly indents the entire
116 balanced expression which starts at the beginning of the current
117 line, to reflect the current line's indentation.
119 In most major modes, if point was in the current line's
120 indentation, it is moved to the first non-whitespace character
121 after indenting; otherwise it stays at the same position relative
124 If `transient-mark-mode' is turned on and the region is active,
125 this function instead calls `indent-region'. In this case, any
126 prefix argument is ignored."
129 ;; The region is active, indent it.
131 (indent-region (region-beginning) (region-end)))
132 ((or ;; indent-to-left-margin is only meant for indenting,
133 ;; so we force it to always insert a tab here.
134 (eq indent-line-function
'indent-to-left-margin
)
135 (and (not tab-always-indent
)
136 (or (> (current-column) (current-indentation))
137 (eq this-command last-command
))))
140 (let ((old-tick (buffer-chars-modified-tick))
142 (old-indent (current-indentation)))
145 (or (not (eq (indent--funcall-widened indent-line-function
) 'noindent
))
146 (indent--default-inside-comment)
147 (when (or (<= (current-column) (current-indentation))
148 (not (eq tab-always-indent
'complete
)))
149 (indent--funcall-widened (default-value 'indent-line-function
))))
152 ;; If the text was already indented right, try completion.
153 ((and (eq tab-always-indent
'complete
)
154 (eq old-point
(point))
155 (eq old-tick
(buffer-chars-modified-tick)))
156 (completion-at-point))
158 ;; If a prefix argument was given, rigidly indent the following
159 ;; sexp to match the change in the current line's indentation.
163 (forward-line 0) (forward-sexp) (point-marker)))
164 (indentation-change (- (current-indentation) old-indent
)))
167 (when (and (not (zerop indentation-change
))
168 (< (point) end-marker
))
169 (indent-rigidly (point) end-marker indentation-change
))))))))))
171 (defun indent--funcall-widened (func)
176 (defun insert-tab (&optional arg
)
177 (let ((count (prefix-numeric-value arg
)))
179 (eq (char-syntax (preceding-char)) ?w
))
182 (insert-char ?
\t count
)
183 (indent-to (* tab-width
(+ count
(/ (current-column) tab-width
)))))))
185 (defun indent-rigidly--current-indentation (beg end
)
186 "Return the smallest indentation in range from BEG to END.
187 Blank lines are ignored."
190 (let ((beg (progn (goto-char beg
) (line-beginning-position)))
193 (while (re-search-forward "^\\s-*[[:print:]]" end t
)
194 (setq indent
(min (or indent
(current-indentation))
195 (current-indentation))))
198 (defvar indent-rigidly-map
199 (let ((map (make-sparse-keymap)))
200 (define-key map
[left] 'indent-rigidly-left)
201 (define-key map [right] 'indent-rigidly-right)
202 (define-key map [S-left] 'indent-rigidly-left-to-tab-stop)
203 (define-key map [S-right] 'indent-rigidly-right-to-tab-stop)
205 "Transient keymap for adjusting indentation interactively.
206 It is activated by calling `indent-rigidly' interactively.")
208 (defun indent-rigidly (start end arg &optional interactive)
209 "Indent all lines starting in the region.
210 If called interactively with no prefix argument, activate a
211 transient mode in which the indentation can be adjusted interactively
212 by typing \\<indent-rigidly-map>\\[indent-rigidly-left], \\[indent-rigidly-right], \\[indent-rigidly-left-to-tab-stop], or \\[indent-rigidly-right-to-tab-stop].
213 Typing any other key deactivates the transient mode.
215 If called from a program, or interactively with prefix ARG,
216 indent all lines starting in the region forward by ARG columns.
217 If called from a program, START and END specify the beginning and
218 end of the text to act on, in place of the region.
220 Negative values of ARG indent backward, so you can remove all
221 indentation by specifying a large negative ARG."
222 (interactive "r\nP\np")
223 (if (and (not arg) interactive)
226 (substitute-command-keys
227 "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]."))
228 (set-transient-map indent-rigidly-map t #'deactivate-mark))
231 (setq end (point-marker))
233 (or (bolp) (forward-line 1))
234 (while (< (point) end)
235 (let ((indent (current-indentation))
238 (skip-chars-forward " \t")
239 (setq eol-flag (eolp)))
241 (indent-to (max 0 (+ indent (prefix-numeric-value arg))) 0))
242 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
244 (move-marker end nil)
245 ;; Keep the active region in transient mode.
246 (when (eq (cadr overriding-terminal-local-map) indent-rigidly-map)
247 (setq deactivate-mark nil)))))
249 (defun indent-rigidly--pop-undo ()
250 (and (memq last-command '(indent-rigidly-left indent-rigidly-right
251 indent-rigidly-left-to-tab-stop
252 indent-rigidly-right-to-tab-stop))
253 (consp buffer-undo-list)
254 (eq (car buffer-undo-list) nil)
255 (pop buffer-undo-list)))
257 (defun indent-rigidly-left (beg end)
258 "Indent all lines between BEG and END leftward by one space."
260 (indent-rigidly--pop-undo)
263 (if (eq (current-bidi-paragraph-direction) 'right-to-left) 1 -1)))
265 (defun indent-rigidly-right (beg end)
266 "Indent all lines between BEG and END rightward by one space."
268 (indent-rigidly--pop-undo)
271 (if (eq (current-bidi-paragraph-direction) 'right-to-left) -1 1)))
273 (defun indent-rigidly-left-to-tab-stop (beg end)
274 "Indent all lines between BEG and END leftward to a tab stop."
276 (indent-rigidly--pop-undo)
277 (let* ((current (indent-rigidly--current-indentation beg end))
278 (rtl (eq (current-bidi-paragraph-direction) 'right-to-left))
279 (next (indent-next-tab-stop current (if rtl nil 'prev))))
280 (indent-rigidly beg end (- next current))))
282 (defun indent-rigidly-right-to-tab-stop (beg end)
283 "Indent all lines between BEG and END rightward to a tab stop."
285 (indent-rigidly--pop-undo)
286 (let* ((current (indent-rigidly--current-indentation beg end))
287 (rtl (eq (current-bidi-paragraph-direction) 'right-to-left))
288 (next (indent-next-tab-stop current (if rtl 'prev))))
289 (indent-rigidly beg end (- next current))))
291 (defun indent-line-to (column)
292 "Indent current line to COLUMN.
293 This function removes or adds spaces and tabs at beginning of line
294 only if necessary. It leaves point at end of indentation."
295 (back-to-indentation)
296 (let ((cur-col (current-column)))
297 (cond ((< cur-col column)
298 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
299 (delete-region (point)
300 (progn (skip-chars-backward " ") (point))))
302 ((> cur-col column) ; too far right (after tab?)
303 (delete-region (progn (move-to-column column t) (point))
304 (progn (backward-to-indentation 0) (point)))))))
306 (defun current-left-margin ()
307 "Return the left margin to use for this line.
308 This is the value of the buffer-local variable `left-margin' plus the value
309 of the `left-margin' text-property at the start of the line."
311 (back-to-indentation)
313 (+ left-margin (or (get-text-property
314 (if (and (eobp) (not (bobp)))
315 (1- (point)) (point))
318 (defun move-to-left-margin (&optional n force)
319 "Move to the left margin of the current line.
320 With optional argument, move forward N-1 lines first.
321 The column moved to is the one given by the `current-left-margin' function.
322 If the line's indentation appears to be wrong, and this command is called
323 interactively or with optional argument FORCE, it will be fixed."
324 (interactive (list (prefix-numeric-value current-prefix-arg) t))
325 (beginning-of-line n)
326 (skip-chars-forward " \t")
327 (if (minibufferp (current-buffer))
328 (if (save-excursion (beginning-of-line) (bobp))
329 (goto-char (minibuffer-prompt-end))
331 (let ((lm (current-left-margin))
332 (cc (current-column)))
334 (if (> (move-to-column lm force) lm)
335 ;; If lm is in a tab and we are not forcing, move before tab
337 ((and force (< cc lm))
338 (indent-to-left-margin))))))
340 ;; This used to be the default indent-line-function,
341 ;; used in Fundamental Mode, Text Mode, etc.
342 (defun indent-to-left-margin ()
343 "Indent current line to the column given by `current-left-margin'."
344 (save-excursion (indent-line-to (current-left-margin)))
345 ;; If we are within the indentation, move past it.
346 (when (save-excursion
347 (skip-chars-backward " \t")
349 (skip-chars-forward " \t")))
351 (defun delete-to-left-margin (&optional from to)
352 "Remove left margin indentation from a region.
353 This deletes to the column given by `current-left-margin'.
354 In no case will it delete non-whitespace.
355 Args FROM and TO are optional; default is the whole buffer."
357 (goto-char (or to (point-max)))
358 (setq to (point-marker))
359 (goto-char (or from (point-min)))
360 (or (bolp) (forward-line 1))
361 (while (< (point) to)
362 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
364 (move-marker to nil)))
366 (defun set-left-margin (from to width)
367 "Set the left margin of the region to WIDTH.
368 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
370 Interactively, WIDTH is the prefix argument, if specified.
371 Without prefix argument, the command prompts for WIDTH."
372 (interactive "r\nNSet left margin to column: ")
374 ;; If inside indentation, start from BOL.
376 (skip-chars-backward " \t")
377 (if (bolp) (setq from (point)))
378 ;; Place end after whitespace
380 (skip-chars-forward " \t")
381 (setq to (point-marker)))
382 ;; Delete margin indentation first, but keep paragraph indentation.
383 (delete-to-left-margin from to)
384 (put-text-property from to 'left-margin width)
385 (indent-rigidly from to width)
386 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
387 (move-marker to nil))
389 (defun set-right-margin (from to width)
390 "Set the right margin of the region to WIDTH.
391 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
393 Interactively, WIDTH is the prefix argument, if specified.
394 Without prefix argument, the command prompts for WIDTH."
395 (interactive "r\nNSet right margin to width: ")
398 (skip-chars-backward " \t")
399 (if (bolp) (setq from (point))))
400 (put-text-property from to 'right-margin width)
401 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
403 (defun alter-text-property (from to prop func &optional object)
404 "Programmatically change value of a text-property.
405 For each region between FROM and TO that has a single value for PROPERTY,
406 apply FUNCTION to that value and sets the property to the function's result.
407 Optional fifth argument OBJECT specifies the string or buffer to operate on."
410 (while (setq val (get-text-property begin prop object)
411 end (text-property-not-all begin to prop val object))
412 (put-text-property begin end prop (funcall func val) object)
415 (put-text-property begin to prop (funcall func val) object))))
417 (defun increase-left-margin (from to inc)
418 "Increase or decrease the left-margin of the region.
419 With no prefix argument, this adds `standard-indent' of indentation.
420 A prefix arg (optional third arg INC noninteractively) specifies the amount
421 to change the margin by, in characters.
422 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
423 (interactive "*r\nP")
424 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
427 (skip-chars-backward " \t")
428 (if (bolp) (setq from (point)))
430 (setq to (point-marker)))
431 (alter-text-property from to 'left-margin
432 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
433 (indent-rigidly from to inc)
434 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
435 (move-marker to nil))
437 (defun decrease-left-margin (from to inc)
438 "Make the left margin of the region smaller.
439 With no prefix argument, decrease the indentation by `standard-indent'.
440 A prefix arg (optional third arg INC noninteractively) specifies the amount
441 to change the margin by, in characters.
442 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
443 (interactive "*r\nP")
444 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
445 (increase-left-margin from to (- inc)))
447 (defun increase-right-margin (from to inc)
448 "Increase the right-margin of the region.
449 With no prefix argument, increase the right margin by `standard-indent'.
450 A prefix arg (optional third arg INC noninteractively) specifies the amount
451 to change the margin by, in characters. A negative argument decreases
452 the right margin width.
453 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
455 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
457 (alter-text-property from to 'right-margin
458 (lambda (v) (+ inc (or v 0))))
459 (if auto-fill-function
460 (fill-region from to nil t t))))
462 (defun decrease-right-margin (from to inc)
463 "Make the right margin of the region smaller.
464 With no prefix argument, decrease the right margin by `standard-indent'.
465 A prefix arg (optional third arg INC noninteractively) specifies the amount
466 of width to remove, in characters. A negative argument increases
467 the right margin width.
468 If `auto-fill-mode' is active, re-fills region to fit in new margin."
469 (interactive "*r\nP")
470 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
471 (increase-right-margin from to (- inc)))
473 (defun beginning-of-line-text (&optional n)
474 "Move to the beginning of the text on this line.
475 With optional argument, move forward N-1 lines first.
476 From the beginning of the line, moves past the left-margin indentation, the
477 fill-prefix, and any indentation used for centering or right-justifying the
478 line, but does not move past any whitespace that was explicitly inserted
479 \(such as a tab used to indent the first line of a paragraph)."
481 (beginning-of-line n)
482 (skip-chars-forward " \t")
483 ;; Skip over fill-prefix.
485 (not (string-equal fill-prefix "")))
486 (if (equal fill-prefix
488 (point) (min (point-max) (+ (length fill-prefix) (point)))))
489 (forward-char (length fill-prefix)))
490 (if (and adaptive-fill-mode adaptive-fill-regexp
491 (looking-at adaptive-fill-regexp))
492 (goto-char (match-end 0))))
493 ;; Skip centering or flushright indentation
494 (if (memq (current-justification) '(center right))
495 (skip-chars-forward " \t")))
497 (defvar indent-region-function #'indent-region-line-by-line
498 "Short cut function to indent region using `indent-according-to-mode'.
499 Default is to really run `indent-according-to-mode' on each line.")
501 (defun indent-region (start end &optional column)
502 "Indent each nonblank line in the region.
503 A numeric prefix argument specifies a column: indent each line to that column.
505 With no prefix argument, the command chooses one of these methods and
506 indents all the lines with it:
508 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
509 beginning of each line in the region that does not already begin
511 2) If `indent-region-function' is non-nil, call that function
512 to indent the region.
513 3) Indent each line via `indent-according-to-mode'.
515 Called from a program, START and END specify the region to indent.
516 If the third argument COLUMN is an integer, it specifies the
517 column to indent to; if it is nil, use one of the three methods above."
520 ;; If a numeric prefix is given, indent to that column.
522 (setq column (prefix-numeric-value column))
525 (setq end (point-marker))
527 (or (bolp) (forward-line 1))
528 (while (< (point) end)
529 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
531 (indent-to column 0))
533 (move-marker end nil)))
534 ;; If a fill-prefix is specified, use it.
538 (setq end (point-marker))
540 (let ((regexp (regexp-quote fill-prefix)))
541 (while (< (point) end)
542 (or (looking-at regexp)
544 (insert fill-prefix))
546 ;; Use indent-region-function is available.
547 (indent-region-function
550 (funcall indent-region-function start end)))
551 ;; Else, use a default implementation that calls indent-line-function on
553 (t (indent-region-line-by-line start end)))
554 ;; In most cases, reindenting modifies the buffer, but it may also
555 ;; leave it unmodified, in which case we have to deactivate the mark
557 (setq deactivate-mark t))
559 (defun indent-region-line-by-line (start end)
561 (setq end (copy-marker end))
563 (let ((pr (unless (minibufferp)
564 (make-progress-reporter "Indenting region..." (point) end))))
565 (while (< (point) end)
566 (or (and (bolp) (eolp))
567 (indent-according-to-mode))
569 (and pr (progress-reporter-update pr (point))))
570 (and pr (progress-reporter-done pr))
571 (move-marker end nil))))
573 (define-obsolete-function-alias 'indent-relative-maybe
574 'indent-relative-first-indent-point "26.1")
576 (defun indent-relative-first-indent-point ()
577 "Indent the current line like the previous nonblank line.
578 Indent to the first indentation position in the previous nonblank
579 line if that position is greater than the current column.
581 See also `indent-relative'."
585 (defun indent-relative (&optional first-only unindented-ok)
586 "Space out to under next indent point in previous nonblank line.
587 An indent point is a non-whitespace character following whitespace.
588 The following line shows the indentation points in this line.
590 If FIRST-ONLY is non-nil, then only the first indent point is
593 If the previous nonblank line has no indent points beyond the
594 column point starts at, then `tab-to-tab-stop' is done, if both
595 FIRST-ONLY and UNINDENTED-OK are nil, otherwise nothing is done
598 See also `indent-relative-first-indent-point'."
601 (eq (char-syntax (preceding-char)) ?w))
603 (let ((start-column (current-column))
607 (if (re-search-backward "^[^\n]" nil t)
608 (let ((end (save-excursion (forward-line 1) (point))))
609 (move-to-column start-column)
610 ;; Is start-column inside a tab on this line?
611 (if (> (current-column) start-column)
613 (or (looking-at "[ \t]")
615 (skip-chars-forward "^ \t" end))
616 (skip-chars-forward " \t" end)
617 (or (= (point) end) (setq indent (current-column))))))
619 (let ((opoint (point-marker)))
621 (if (> opoint (point))
623 (move-marker opoint nil)))
625 (t (tab-to-tab-stop)))))
627 (defcustom tab-stop-list nil
628 "List of tab stop positions used by `tab-to-tab-stop'.
629 This should be nil, or a list of integers, ordered from smallest to largest.
630 It implicitly extends to infinity through repetition of the last step.
631 For example, (1 2 5) is equivalent to (1 2 5 8 11 ...). If the list has
632 fewer than 2 elements, `tab-width' is used as the \"last step\".
633 A value of nil means a tab stop every `tab-width' columns."
635 :version "24.4" ; from explicit list to nil
637 :type '(repeat integer))
639 (defvar edit-tab-stops-map
640 (let ((map (make-sparse-keymap)))
641 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
642 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
644 "Keymap used in `edit-tab-stops'.")
646 (defvar edit-tab-stops-buffer nil
647 "Buffer whose tab stops are being edited.
648 This matters if the variable `tab-stop-list' is local in that buffer.")
650 (defun edit-tab-stops ()
651 "Edit the tab stops used by `tab-to-tab-stop'.
652 Creates a buffer *Tab Stops* containing text describing the tab stops.
653 A colon indicates a column where there is a tab stop.
654 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
656 (setq edit-tab-stops-buffer (current-buffer))
657 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
658 (use-local-map edit-tab-stops-map)
659 (setq-local indent-tabs-mode nil)
661 (setq truncate-lines t)
663 (let ((tabs tab-stop-list))
665 (indent-to (car tabs) 0)
667 (setq tabs (cdr tabs))))
671 (insert (+ count ?0))
673 (setq count (1+ count)))
676 (insert "0123456789")
677 (setq count (1- count))))
678 (insert "\nTo install changes, type C-c C-c")
679 (goto-char (point-min)))
681 (defun edit-tab-stops-note-changes ()
682 "Put edited tab stops into effect."
688 (while (search-backward ":" nil t)
689 (setq tabs (cons (current-column) tabs))))
690 (bury-buffer (prog1 (current-buffer)
691 (switch-to-buffer edit-tab-stops-buffer)))
692 (setq tab-stop-list tabs))
693 (message "Tab stops installed"))
695 (defun indent-next-tab-stop (column &optional prev)
696 "Return the next tab stop after COLUMN.
697 If PREV is non-nil, return the previous one instead."
698 (let ((tabs tab-stop-list))
699 (while (and tabs (>= column (car tabs)))
700 (setq tabs (cdr tabs)))
704 (let ((prevtabs (cdr (memq (car tabs) (reverse tab-stop-list)))))
705 (if (null prevtabs) 0
706 (if (= column (car prevtabs))
707 (or (nth 1 prevtabs) 0)
709 ;; We passed the end of tab-stop-list: guess a continuation.
710 (let* ((last2 (last tab-stop-list 2))
711 (step (if (cdr last2) (- (cadr last2) (car last2)) tab-width))
712 (last (or (cadr last2) (car last2) 0)))
713 ;; Repeat the last tab's length.
714 (+ last (* step (if prev
715 (if (<= column last) -1 (/ (- column last 1) step))
716 (1+ (/ (- column last) step)))))))))
718 (defun indent-accumulate-tab-stops (limit)
719 "Get a list of tab stops before LIMIT (inclusive)."
720 (let ((tab 0) (tab-stops))
721 (while (<= (setq tab (indent-next-tab-stop tab)) limit)
722 (push tab tab-stops))
723 (nreverse tab-stops)))
725 (defun tab-to-tab-stop ()
726 "Insert spaces or tabs to next defined tab-stop column.
727 The variable `tab-stop-list' is a list of columns at which there are tab stops.
728 Use \\[edit-tab-stops] to edit them interactively."
730 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
732 (let ((nexttab (indent-next-tab-stop (current-column))))
733 (delete-horizontal-space t)
734 (indent-to nexttab)))
736 (defun move-to-tab-stop ()
737 "Move point to next defined tab-stop column.
738 The variable `tab-stop-list' is a list of columns at which there are tab stops.
739 Use \\[edit-tab-stops] to edit them interactively."
741 (let ((nexttab (indent-next-tab-stop (current-column))))
742 (let ((before (point)))
743 (move-to-column nexttab t)
746 ;; If we just added a tab, or moved over one,
747 ;; delete any superfluous spaces before the old point.
748 (if (and (eq (preceding-char) ?\s)
749 (eq (following-char) ?\t))
750 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
751 (while (and (> (current-column) tabend)
752 (eq (preceding-char) ?\s))
754 (delete-region (point) before)))))))
756 (define-key global-map "\t" 'indent-for-tab-command)
757 (define-key esc-map "\C-\\" 'indent-region)
758 (define-key ctl-x-map "\t" 'indent-rigidly)
759 (define-key esc-map "i" 'tab-to-tab-stop)
761 ;;; indent.el ends here