Fix an error in Tramp for rsync
[emacs.git] / lisp / indent.el
blob0bbb5209e8aaa6b45e992150c637c9a50928e7fd
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
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 (if (bobp) 0
80 (beginning-of-line 0)
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))))
86 ;; The normal case.
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)))
93 (when (nth 4 ppss)
94 (indent-line-to
95 (save-excursion
96 (forward-line -1)
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))))
102 (current-column)))
103 t))))
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
120 to the text.
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."
125 (interactive "P")
126 (cond
127 ;; The region is active, indent it.
128 ((use-region-p)
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))))
136 (insert-tab arg))
138 (let ((old-tick (buffer-chars-modified-tick))
139 (old-point (point))
140 (old-indent (current-indentation)))
142 ;; Indent the line.
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))))
149 (cond
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.
158 (arg
159 (let ((end-marker
160 (save-excursion
161 (forward-line 0) (forward-sexp) (point-marker)))
162 (indentation-change (- (current-indentation) old-indent)))
163 (save-excursion
164 (forward-line 1)
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)))
171 (if (and abbrev-mode
172 (eq (char-syntax (preceding-char)) ?w))
173 (expand-abbrev))
174 (if indent-tabs-mode
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."
181 (save-excursion
182 (save-match-data
183 (let ((beg (progn (goto-char beg) (line-beginning-position)))
184 indent)
185 (goto-char beg)
186 (while (re-search-forward "^\\s-*[[:print:]]" end t)
187 (setq indent (min (or indent (current-indentation))
188 (current-indentation))))
189 indent))))
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)
197 map)
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)
217 (progn
218 (message
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))
222 (save-excursion
223 (goto-char end)
224 (setq end (point-marker))
225 (goto-char start)
226 (or (bolp) (forward-line 1))
227 (while (< (point) end)
228 (let ((indent (current-indentation))
229 eol-flag)
230 (save-excursion
231 (skip-chars-forward " \t")
232 (setq eol-flag (eolp)))
233 (or eol-flag
234 (indent-to (max 0 (+ indent (prefix-numeric-value arg))) 0))
235 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
236 (forward-line 1))
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."
252 (interactive "r")
253 (indent-rigidly--pop-undo)
254 (indent-rigidly
255 beg end
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."
260 (interactive "r")
261 (indent-rigidly--pop-undo)
262 (indent-rigidly
263 beg end
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."
268 (interactive "r")
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."
277 (interactive "r")
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))))
294 (indent-to column))
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."
303 (save-excursion
304 (back-to-indentation)
305 (max 0
306 (+ left-margin (or (get-text-property
307 (if (and (eobp) (not (bobp)))
308 (1- (point)) (point))
309 'left-margin) 0)))))
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))
323 (beginning-of-line))
324 (let ((lm (current-left-margin))
325 (cc (current-column)))
326 (cond ((> cc lm)
327 (if (> (move-to-column lm force) lm)
328 ;; If lm is in a tab and we are not forcing, move before tab
329 (backward-char 1)))
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")
341 (bolp))
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."
349 (save-excursion
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)))
356 (forward-line 1))
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: ")
366 (save-excursion
367 ;; If inside indentation, start from BOL.
368 (goto-char from)
369 (skip-chars-backward " \t")
370 (if (bolp) (setq from (point)))
371 ;; Place end after whitespace
372 (goto-char to)
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: ")
389 (save-excursion
390 (goto-char from)
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."
401 (let ((begin from)
402 end val)
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)
406 (setq begin end))
407 (if (< begin to)
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))
418 (save-excursion
419 (goto-char from)
420 (skip-chars-backward " \t")
421 (if (bolp) (setq from (point)))
422 (goto-char to)
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."
447 (interactive "r\nP")
448 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
449 (save-excursion
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)."
473 (interactive "p")
474 (beginning-of-line n)
475 (skip-chars-forward " \t")
476 ;; Skip over fill-prefix.
477 (if (and fill-prefix
478 (not (string-equal fill-prefix "")))
479 (if (equal fill-prefix
480 (buffer-substring
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
503 with it.
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."
511 (interactive "r\nP")
512 (cond
513 ;; If a numeric prefix is given, indent to that column.
514 (column
515 (setq column (prefix-numeric-value column))
516 (save-excursion
517 (goto-char end)
518 (setq end (point-marker))
519 (goto-char start)
520 (or (bolp) (forward-line 1))
521 (while (< (point) end)
522 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
523 (or (eolp)
524 (indent-to column 0))
525 (forward-line 1))
526 (move-marker end nil)))
527 ;; If a fill-prefix is specified, use it.
528 (fill-prefix
529 (save-excursion
530 (goto-char end)
531 (setq end (point-marker))
532 (goto-char start)
533 (let ((regexp (regexp-quote fill-prefix)))
534 (while (< (point) end)
535 (or (looking-at regexp)
536 (and (bolp) (eolp))
537 (insert fill-prefix))
538 (forward-line 1)))))
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
543 ;; each line.
545 (save-excursion
546 (setq end (copy-marker end))
547 (goto-char start)
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))
553 (forward-line 1)
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
559 ;; by hand.
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'."
568 (interactive)
569 (indent-relative t))
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.
575 ^ ^ ^ ^ ^ ^ ^ ^ ^
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
579 does nothing.
581 See also `indent-relative-maybe'."
582 (interactive "P")
583 (if (and abbrev-mode
584 (eq (char-syntax (preceding-char)) ?w))
585 (expand-abbrev))
586 (let ((start-column (current-column))
587 indent)
588 (save-excursion
589 (beginning-of-line)
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)
595 (backward-char 1))
596 (or (looking-at "[ \t]")
597 unindented-ok
598 (skip-chars-forward "^ \t" end))
599 (skip-chars-forward " \t" end)
600 (or (= (point) end) (setq indent (current-column))))))
601 (if indent
602 (let ((opoint (point-marker)))
603 (indent-to indent 0)
604 (if (> opoint (point))
605 (goto-char opoint))
606 (move-marker opoint nil))
607 (tab-to-tab-stop))))
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."
616 :group 'indent
617 :version "24.4" ; from explicit list to nil
618 :safe 'listp
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)
625 map)
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."
637 (interactive)
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)
642 (overwrite-mode 1)
643 (setq truncate-lines t)
644 (erase-buffer)
645 (let ((tabs tab-stop-list))
646 (while tabs
647 (indent-to (car tabs) 0)
648 (insert ?:)
649 (setq tabs (cdr tabs))))
650 (let ((count 0))
651 (insert ?\n)
652 (while (< count 8)
653 (insert (+ count ?0))
654 (insert " ")
655 (setq count (1+ count)))
656 (insert ?\n)
657 (while (> count 0)
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."
665 (interactive)
666 (let (tabs)
667 (save-excursion
668 (goto-char 1)
669 (end-of-line)
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)))
683 (if tabs
684 (if (not prev)
685 (car 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)
690 (car prevtabs)))))
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."
711 (interactive)
712 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
713 (expand-abbrev))
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."
722 (interactive)
723 (let ((nexttab (indent-next-tab-stop (current-column))))
724 (let ((before (point)))
725 (move-to-column nexttab t)
726 (save-excursion
727 (goto-char before)
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))
735 (forward-char -1))
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