Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / indent.el
blob4cd669ccf37e5e8d934fd85230ea0c363bb1da4e
1 ;;; indent.el --- indentation commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985, 1995, 2001-2014 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Package: emacs
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/>.
23 ;;; Commentary:
25 ;; Commands for making and changing indentation in text. These are
26 ;; described in the Emacs manual.
28 ;;; Code:
30 (defgroup indent nil
31 "Indentation commands."
32 :group 'editing)
34 (defcustom standard-indent 4
35 "Default number of columns for margin-changing functions to indent."
36 :group 'indent
37 :type 'integer)
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."
57 :group 'indent
58 :type '(choice
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."
71 (interactive)
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
78 (beginning-of-line)
79 (skip-chars-backward "\n \t")
80 (beginning-of-line)
81 (current-indentation))))
82 (if (<= (current-column) (current-indentation))
83 (indent-line-to column)
84 (save-excursion (indent-line-to column))))
85 ;; The normal case.
86 (funcall indent-line-function)))
88 (defun indent-for-tab-command (&optional arg)
89 "Indent the current line or region, or insert a tab, as appropriate.
90 This function either inserts a tab, or indents the current line,
91 or performs symbol completion, depending on `tab-always-indent'.
92 The function called to actually indent the line or insert a tab
93 is given by the variable `indent-line-function'.
95 If a prefix argument is given, after this function indents the
96 current line or inserts a tab, it also rigidly indents the entire
97 balanced expression which starts at the beginning of the current
98 line, to reflect the current line's indentation.
100 In most major modes, if point was in the current line's
101 indentation, it is moved to the first non-whitespace character
102 after indenting; otherwise it stays at the same position relative
103 to the text.
105 If `transient-mark-mode' is turned on and the region is active,
106 this function instead calls `indent-region'. In this case, any
107 prefix argument is ignored."
108 (interactive "P")
109 (cond
110 ;; The region is active, indent it.
111 ((use-region-p)
112 (indent-region (region-beginning) (region-end)))
113 ((or ;; indent-to-left-margin is only meant for indenting,
114 ;; so we force it to always insert a tab here.
115 (eq indent-line-function 'indent-to-left-margin)
116 (and (not tab-always-indent)
117 (or (> (current-column) (current-indentation))
118 (eq this-command last-command))))
119 (insert-tab arg))
121 (let ((old-tick (buffer-chars-modified-tick))
122 (old-point (point))
123 (old-indent (current-indentation)))
125 ;; Indent the line.
126 (funcall indent-line-function)
128 (cond
129 ;; If the text was already indented right, try completion.
130 ((and (eq tab-always-indent 'complete)
131 (eq old-point (point))
132 (eq old-tick (buffer-chars-modified-tick)))
133 (completion-at-point))
135 ;; If a prefix argument was given, rigidly indent the following
136 ;; sexp to match the change in the current line's indentation.
137 (arg
138 (let ((end-marker
139 (save-excursion
140 (forward-line 0) (forward-sexp) (point-marker)))
141 (indentation-change (- (current-indentation) old-indent)))
142 (save-excursion
143 (forward-line 1)
144 (when (and (not (zerop indentation-change))
145 (< (point) end-marker))
146 (indent-rigidly (point) end-marker indentation-change))))))))))
148 (defun insert-tab (&optional arg)
149 (let ((count (prefix-numeric-value arg)))
150 (if (and abbrev-mode
151 (eq (char-syntax (preceding-char)) ?w))
152 (expand-abbrev))
153 (if indent-tabs-mode
154 (insert-char ?\t count)
155 (indent-to (* tab-width (+ count (/ (current-column) tab-width)))))))
157 (defun indent-rigidly--current-indentation (beg end)
158 "Return the smallest indentation in range from BEG to END.
159 Blank lines are ignored."
160 (save-excursion
161 (save-match-data
162 (let ((beg (progn (goto-char beg) (line-beginning-position)))
163 indent)
164 (goto-char beg)
165 (while (re-search-forward "^\\s-*[[:print:]]" end t)
166 (setq indent (min (or indent (current-indentation))
167 (current-indentation))))
168 indent))))
170 (defvar indent-rigidly-map
171 (let ((map (make-sparse-keymap)))
172 (define-key map [left] 'indent-rigidly-left)
173 (define-key map [right] 'indent-rigidly-right)
174 (define-key map [S-left] 'indent-rigidly-left-to-tab-stop)
175 (define-key map [S-right] 'indent-rigidly-right-to-tab-stop)
176 map)
177 "Transient keymap for adjusting indentation interactively.
178 It is activated by calling `indent-rigidly' interactively.")
180 (defun indent-rigidly (start end arg &optional interactive)
181 "Indent all lines starting in the region.
182 If called interactively with no prefix argument, activate a
183 transient mode in which the indentation can be adjusted interactively
184 by typing \\<indent-rigidly-map>\\[indent-rigidly-left], \\[indent-rigidly-right], \\[indent-rigidly-left-to-tab-stop], or \\[indent-rigidly-right-to-tab-stop].
185 Typing any other key deactivates the transient mode.
187 If called from a program, or interactively with prefix ARG,
188 indent all lines starting in the region forward by ARG columns.
189 If called from a program, START and END specify the beginning and
190 end of the text to act on, in place of the region.
192 Negative values of ARG indent backward, so you can remove all
193 indentation by specifying a large negative ARG."
194 (interactive "r\nP\np")
195 (if (and (not arg) interactive)
196 (progn
197 (message
198 (substitute-command-keys
199 "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]."))
200 (set-transient-map indent-rigidly-map t))
201 (save-excursion
202 (goto-char end)
203 (setq end (point-marker))
204 (goto-char start)
205 (or (bolp) (forward-line 1))
206 (while (< (point) end)
207 (let ((indent (current-indentation))
208 eol-flag)
209 (save-excursion
210 (skip-chars-forward " \t")
211 (setq eol-flag (eolp)))
212 (or eol-flag
213 (indent-to (max 0 (+ indent (prefix-numeric-value arg))) 0))
214 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
215 (forward-line 1))
216 (move-marker end nil))))
218 (defun indent-rigidly--pop-undo ()
219 (and (memq last-command '(indent-rigidly-left indent-rigidly-right
220 indent-rigidly-left-to-tab-stop
221 indent-rigidly-right-to-tab-stop))
222 (consp buffer-undo-list)
223 (eq (car buffer-undo-list) nil)
224 (pop buffer-undo-list)))
226 (defun indent-rigidly-left (beg end)
227 "Indent all lines between BEG and END leftward by one space."
228 (interactive "r")
229 (indent-rigidly--pop-undo)
230 (indent-rigidly
231 beg end
232 (if (eq (current-bidi-paragraph-direction) 'right-to-left) 1 -1)))
234 (defun indent-rigidly-right (beg end)
235 "Indent all lines between BEG and END rightward by one space."
236 (interactive "r")
237 (indent-rigidly--pop-undo)
238 (indent-rigidly
239 beg end
240 (if (eq (current-bidi-paragraph-direction) 'right-to-left) -1 1)))
242 (defun indent-rigidly-left-to-tab-stop (beg end)
243 "Indent all lines between BEG and END leftward to a tab stop."
244 (interactive "r")
245 (indent-rigidly--pop-undo)
246 (let* ((current (indent-rigidly--current-indentation beg end))
247 (rtl (eq (current-bidi-paragraph-direction) 'right-to-left))
248 (next (indent--next-tab-stop current (if rtl nil 'prev))))
249 (indent-rigidly beg end (- next current))))
251 (defun indent-rigidly-right-to-tab-stop (beg end)
252 "Indent all lines between BEG and END rightward to a tab stop."
253 (interactive "r")
254 (indent-rigidly--pop-undo)
255 (let* ((current (indent-rigidly--current-indentation beg end))
256 (rtl (eq (current-bidi-paragraph-direction) 'right-to-left))
257 (next (indent--next-tab-stop current (if rtl 'prev))))
258 (indent-rigidly beg end (- next current))))
260 (defun indent-line-to (column)
261 "Indent current line to COLUMN.
262 This function removes or adds spaces and tabs at beginning of line
263 only if necessary. It leaves point at end of indentation."
264 (back-to-indentation)
265 (let ((cur-col (current-column)))
266 (cond ((< cur-col column)
267 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
268 (delete-region (point)
269 (progn (skip-chars-backward " ") (point))))
270 (indent-to column))
271 ((> cur-col column) ; too far right (after tab?)
272 (delete-region (progn (move-to-column column t) (point))
273 (progn (back-to-indentation) (point)))))))
275 (defun current-left-margin ()
276 "Return the left margin to use for this line.
277 This is the value of the buffer-local variable `left-margin' plus the value
278 of the `left-margin' text-property at the start of the line."
279 (save-excursion
280 (back-to-indentation)
281 (max 0
282 (+ left-margin (or (get-text-property
283 (if (and (eobp) (not (bobp)))
284 (1- (point)) (point))
285 'left-margin) 0)))))
287 (defun move-to-left-margin (&optional n force)
288 "Move to the left margin of the current line.
289 With optional argument, move forward N-1 lines first.
290 The column moved to is the one given by the `current-left-margin' function.
291 If the line's indentation appears to be wrong, and this command is called
292 interactively or with optional argument FORCE, it will be fixed."
293 (interactive (list (prefix-numeric-value current-prefix-arg) t))
294 (beginning-of-line n)
295 (skip-chars-forward " \t")
296 (if (minibufferp (current-buffer))
297 (if (save-excursion (beginning-of-line) (bobp))
298 (goto-char (minibuffer-prompt-end))
299 (beginning-of-line))
300 (let ((lm (current-left-margin))
301 (cc (current-column)))
302 (cond ((> cc lm)
303 (if (> (move-to-column lm force) lm)
304 ;; If lm is in a tab and we are not forcing, move before tab
305 (backward-char 1)))
306 ((and force (< cc lm))
307 (indent-to-left-margin))))))
309 ;; This used to be the default indent-line-function,
310 ;; used in Fundamental Mode, Text Mode, etc.
311 (defun indent-to-left-margin ()
312 "Indent current line to the column given by `current-left-margin'."
313 (save-excursion (indent-line-to (current-left-margin)))
314 ;; If we are within the indentation, move past it.
315 (when (save-excursion
316 (skip-chars-backward " \t")
317 (bolp))
318 (skip-chars-forward " \t")))
320 (defun delete-to-left-margin (&optional from to)
321 "Remove left margin indentation from a region.
322 This deletes to the column given by `current-left-margin'.
323 In no case will it delete non-whitespace.
324 Args FROM and TO are optional; default is the whole buffer."
325 (save-excursion
326 (goto-char (or to (point-max)))
327 (setq to (point-marker))
328 (goto-char (or from (point-min)))
329 (or (bolp) (forward-line 1))
330 (while (< (point) to)
331 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
332 (forward-line 1))
333 (move-marker to nil)))
335 (defun set-left-margin (from to width)
336 "Set the left margin of the region to WIDTH.
337 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
339 Interactively, WIDTH is the prefix argument, if specified.
340 Without prefix argument, the command prompts for WIDTH."
341 (interactive "r\nNSet left margin to column: ")
342 (save-excursion
343 ;; If inside indentation, start from BOL.
344 (goto-char from)
345 (skip-chars-backward " \t")
346 (if (bolp) (setq from (point)))
347 ;; Place end after whitespace
348 (goto-char to)
349 (skip-chars-forward " \t")
350 (setq to (point-marker)))
351 ;; Delete margin indentation first, but keep paragraph indentation.
352 (delete-to-left-margin from to)
353 (put-text-property from to 'left-margin width)
354 (indent-rigidly from to width)
355 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
356 (move-marker to nil))
358 (defun set-right-margin (from to width)
359 "Set the right margin of the region to WIDTH.
360 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
362 Interactively, WIDTH is the prefix argument, if specified.
363 Without prefix argument, the command prompts for WIDTH."
364 (interactive "r\nNSet right margin to width: ")
365 (save-excursion
366 (goto-char from)
367 (skip-chars-backward " \t")
368 (if (bolp) (setq from (point))))
369 (put-text-property from to 'right-margin width)
370 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
372 (defun alter-text-property (from to prop func &optional object)
373 "Programmatically change value of a text-property.
374 For each region between FROM and TO that has a single value for PROPERTY,
375 apply FUNCTION to that value and sets the property to the function's result.
376 Optional fifth argument OBJECT specifies the string or buffer to operate on."
377 (let ((begin from)
378 end val)
379 (while (setq val (get-text-property begin prop object)
380 end (text-property-not-all begin to prop val object))
381 (put-text-property begin end prop (funcall func val) object)
382 (setq begin end))
383 (if (< begin to)
384 (put-text-property begin to prop (funcall func val) object))))
386 (defun increase-left-margin (from to inc)
387 "Increase or decrease the left-margin of the region.
388 With no prefix argument, this adds `standard-indent' of indentation.
389 A prefix arg (optional third arg INC noninteractively) specifies the amount
390 to change the margin by, in characters.
391 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
392 (interactive "*r\nP")
393 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
394 (save-excursion
395 (goto-char from)
396 (skip-chars-backward " \t")
397 (if (bolp) (setq from (point)))
398 (goto-char to)
399 (setq to (point-marker)))
400 (alter-text-property from to 'left-margin
401 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
402 (indent-rigidly from to inc)
403 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
404 (move-marker to nil))
406 (defun decrease-left-margin (from to inc)
407 "Make the left margin of the region smaller.
408 With no prefix argument, decrease the indentation by `standard-indent'.
409 A prefix arg (optional third arg INC noninteractively) specifies the amount
410 to change the margin by, in characters.
411 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
412 (interactive "*r\nP")
413 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
414 (increase-left-margin from to (- inc)))
416 (defun increase-right-margin (from to inc)
417 "Increase the right-margin of the region.
418 With no prefix argument, increase the right margin by `standard-indent'.
419 A prefix arg (optional third arg INC noninteractively) specifies the amount
420 to change the margin by, in characters. A negative argument decreases
421 the right margin width.
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))
425 (save-excursion
426 (alter-text-property from to 'right-margin
427 (lambda (v) (+ inc (or v 0))))
428 (if auto-fill-function
429 (fill-region from to nil t t))))
431 (defun decrease-right-margin (from to inc)
432 "Make the right margin of the region smaller.
433 With no prefix argument, decrease the right margin by `standard-indent'.
434 A prefix arg (optional third arg INC noninteractively) specifies the amount
435 of width to remove, in characters. A negative argument increases
436 the right margin width.
437 If `auto-fill-mode' is active, re-fills region to fit in new margin."
438 (interactive "*r\nP")
439 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
440 (increase-right-margin from to (- inc)))
442 (defun beginning-of-line-text (&optional n)
443 "Move to the beginning of the text on this line.
444 With optional argument, move forward N-1 lines first.
445 From the beginning of the line, moves past the left-margin indentation, the
446 fill-prefix, and any indentation used for centering or right-justifying the
447 line, but does not move past any whitespace that was explicitly inserted
448 \(such as a tab used to indent the first line of a paragraph)."
449 (interactive "p")
450 (beginning-of-line n)
451 (skip-chars-forward " \t")
452 ;; Skip over fill-prefix.
453 (if (and fill-prefix
454 (not (string-equal fill-prefix "")))
455 (if (equal fill-prefix
456 (buffer-substring
457 (point) (min (point-max) (+ (length fill-prefix) (point)))))
458 (forward-char (length fill-prefix)))
459 (if (and adaptive-fill-mode adaptive-fill-regexp
460 (looking-at adaptive-fill-regexp))
461 (goto-char (match-end 0))))
462 ;; Skip centering or flushright indentation
463 (if (memq (current-justification) '(center right))
464 (skip-chars-forward " \t")))
466 (defvar indent-region-function nil
467 "Short cut function to indent region using `indent-according-to-mode'.
468 A value of nil means really run `indent-according-to-mode' on each line.")
470 (defun indent-region (start end &optional column)
471 "Indent each nonblank line in the region.
472 A numeric prefix argument specifies a column: indent each line to that column.
474 With no prefix argument, the command chooses one of these methods and
475 indents all the lines with it:
477 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
478 beginning of each line in the region that does not already begin
479 with it.
480 2) If `indent-region-function' is non-nil, call that function
481 to indent the region.
482 3) Indent each line via `indent-according-to-mode'.
484 Called from a program, START and END specify the region to indent.
485 If the third argument COLUMN is an integer, it specifies the
486 column to indent to; if it is nil, use one of the three methods above."
487 (interactive "r\nP")
488 (cond
489 ;; If a numeric prefix is given, indent to that column.
490 (column
491 (setq column (prefix-numeric-value column))
492 (save-excursion
493 (goto-char end)
494 (setq end (point-marker))
495 (goto-char start)
496 (or (bolp) (forward-line 1))
497 (while (< (point) end)
498 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
499 (or (eolp)
500 (indent-to column 0))
501 (forward-line 1))
502 (move-marker end nil)))
503 ;; If a fill-prefix is specified, use it.
504 (fill-prefix
505 (save-excursion
506 (goto-char end)
507 (setq end (point-marker))
508 (goto-char start)
509 (let ((regexp (regexp-quote fill-prefix)))
510 (while (< (point) end)
511 (or (looking-at regexp)
512 (and (bolp) (eolp))
513 (insert fill-prefix))
514 (forward-line 1)))))
515 ;; Use indent-region-function is available.
516 (indent-region-function
517 (funcall indent-region-function start end))
518 ;; Else, use a default implementation that calls indent-line-function on
519 ;; each line.
521 (save-excursion
522 (setq end (copy-marker end))
523 (goto-char start)
524 (let ((pr (unless (minibufferp)
525 (make-progress-reporter "Indenting region..." (point) end))))
526 (while (< (point) end)
527 (or (and (bolp) (eolp))
528 (indent-according-to-mode))
529 (forward-line 1)
530 (and pr (progress-reporter-update pr (point))))
531 (and pr (progress-reporter-done pr))
532 (move-marker end nil)))))
533 ;; In most cases, reindenting modifies the buffer, but it may also
534 ;; leave it unmodified, in which case we have to deactivate the mark
535 ;; by hand.
536 (deactivate-mark))
538 (defun indent-relative-maybe ()
539 "Indent a new line like previous nonblank line.
540 If the previous nonblank line has no indent points beyond the
541 column point starts at, this command does nothing.
543 See also `indent-relative'."
544 (interactive)
545 (indent-relative t))
547 (defun indent-relative (&optional unindented-ok)
548 "Space out to under next indent point in previous nonblank line.
549 An indent point is a non-whitespace character following whitespace.
550 The following line shows the indentation points in this line.
551 ^ ^ ^ ^ ^ ^ ^ ^ ^
552 If the previous nonblank line has no indent points beyond the
553 column point starts at, `tab-to-tab-stop' is done instead, unless
554 this command is invoked with a numeric argument, in which case it
555 does nothing.
557 See also `indent-relative-maybe'."
558 (interactive "P")
559 (if (and abbrev-mode
560 (eq (char-syntax (preceding-char)) ?w))
561 (expand-abbrev))
562 (let ((start-column (current-column))
563 indent)
564 (save-excursion
565 (beginning-of-line)
566 (if (re-search-backward "^[^\n]" nil t)
567 (let ((end (save-excursion (forward-line 1) (point))))
568 (move-to-column start-column)
569 ;; Is start-column inside a tab on this line?
570 (if (> (current-column) start-column)
571 (backward-char 1))
572 (or (looking-at "[ \t]")
573 unindented-ok
574 (skip-chars-forward "^ \t" end))
575 (skip-chars-forward " \t" end)
576 (or (= (point) end) (setq indent (current-column))))))
577 (if indent
578 (let ((opoint (point-marker)))
579 (indent-to indent 0)
580 (if (> opoint (point))
581 (goto-char opoint))
582 (move-marker opoint nil))
583 (tab-to-tab-stop))))
585 (defcustom tab-stop-list
587 "List of tab stop positions used by `tab-to-tab-stop'.
588 This should be a list of integers, ordered from smallest to largest.
589 It implicitly extends to infinity by repeating the last step (e.g. '(1 2 5)
590 is equivalent to '(1 2 5 8 11)).
591 If the list has less than 2 elements, `tab-width' is used as the \"last step\"."
592 :group 'indent
593 :type '(repeat integer))
594 (put 'tab-stop-list 'safe-local-variable 'listp)
596 (defvar edit-tab-stops-map
597 (let ((map (make-sparse-keymap)))
598 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
599 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
600 map)
601 "Keymap used in `edit-tab-stops'.")
603 (defvar edit-tab-stops-buffer nil
604 "Buffer whose tab stops are being edited.
605 This matters if the variable `tab-stop-list' is local in that buffer.")
607 (defun edit-tab-stops ()
608 "Edit the tab stops used by `tab-to-tab-stop'.
609 Creates a buffer *Tab Stops* containing text describing the tab stops.
610 A colon indicates a column where there is a tab stop.
611 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
612 (interactive)
613 (setq edit-tab-stops-buffer (current-buffer))
614 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
615 (use-local-map edit-tab-stops-map)
616 (setq-local indent-tabs-mode nil)
617 (overwrite-mode 1)
618 (setq truncate-lines t)
619 (erase-buffer)
620 (let ((tabs tab-stop-list))
621 (while tabs
622 (indent-to (car tabs) 0)
623 (insert ?:)
624 (setq tabs (cdr tabs))))
625 (let ((count 0))
626 (insert ?\n)
627 (while (< count 8)
628 (insert (+ count ?0))
629 (insert " ")
630 (setq count (1+ count)))
631 (insert ?\n)
632 (while (> count 0)
633 (insert "0123456789")
634 (setq count (1- count))))
635 (insert "\nTo install changes, type C-c C-c")
636 (goto-char (point-min)))
638 (defun edit-tab-stops-note-changes ()
639 "Put edited tab stops into effect."
640 (interactive)
641 (let (tabs)
642 (save-excursion
643 (goto-char 1)
644 (end-of-line)
645 (while (search-backward ":" nil t)
646 (setq tabs (cons (current-column) tabs))))
647 (bury-buffer (prog1 (current-buffer)
648 (switch-to-buffer edit-tab-stops-buffer)))
649 (setq tab-stop-list tabs))
650 (message "Tab stops installed"))
652 (defun indent--next-tab-stop (column &optional prev)
653 "Return the next tab stop after COLUMN.
654 If PREV is non-nil, return the previous one instead."
655 (let ((tabs tab-stop-list))
656 (while (and tabs (>= column (car tabs)))
657 (setq tabs (cdr tabs)))
658 (if tabs
659 (if (not prev)
660 (car tabs)
661 (let ((prevtabs (cdr (memq (car tabs) (reverse tab-stop-list)))))
662 (if (null prevtabs) 0
663 (if (= column (car prevtabs))
664 (or (nth 1 prevtabs) 0)
665 (car prevtabs)))))
666 ;; We passed the end of tab-stop-list: guess a continuation.
667 (let* ((last2 (last tab-stop-list 2))
668 (step (if (cdr last2) (- (cadr last2) (car last2)) tab-width))
669 (last (or (cadr last2) (car last2) 0)))
670 ;; Repeat the last tab's length.
671 (+ last (* step (if prev
672 (if (<= column last) -1 (/ (- column last 1) step))
673 (1+ (/ (- column last) step)))))))))
675 (defun tab-to-tab-stop ()
676 "Insert spaces or tabs to next defined tab-stop column.
677 The variable `tab-stop-list' is a list of columns at which there are tab stops.
678 Use \\[edit-tab-stops] to edit them interactively."
679 (interactive)
680 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
681 (expand-abbrev))
682 (let ((nexttab (indent--next-tab-stop (current-column))))
683 (delete-horizontal-space t)
684 (indent-to nexttab)))
686 (defun move-to-tab-stop ()
687 "Move point to next defined tab-stop column.
688 The variable `tab-stop-list' is a list of columns at which there are tab stops.
689 Use \\[edit-tab-stops] to edit them interactively."
690 (interactive)
691 (let ((nexttab (indent--next-tab-stop (current-column))))
692 (let ((before (point)))
693 (move-to-column nexttab t)
694 (save-excursion
695 (goto-char before)
696 ;; If we just added a tab, or moved over one,
697 ;; delete any superfluous spaces before the old point.
698 (if (and (eq (preceding-char) ?\s)
699 (eq (following-char) ?\t))
700 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
701 (while (and (> (current-column) tabend)
702 (eq (preceding-char) ?\s))
703 (forward-char -1))
704 (delete-region (point) before)))))))
706 (define-key global-map "\t" 'indent-for-tab-command)
707 (define-key esc-map "\C-\\" 'indent-region)
708 (define-key ctl-x-map "\t" 'indent-rigidly)
709 (define-key esc-map "i" 'tab-to-tab-stop)
711 ;;; indent.el ends here