Document reserved keys
[emacs.git] / lisp / indent.el
blob398585e1f90b7daa0b64dace146fe8c1299658ce
1 ;;; indent.el --- indentation commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985, 1995, 2001-2018 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 <https://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 (save-restriction
73 (widen)
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
80 (beginning-of-line)
81 (if (bobp) 0
82 (beginning-of-line 0)
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))))
88 ;; The normal case.
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)))
95 (when (nth 4 ppss)
96 (indent-line-to
97 (save-excursion
98 (forward-line -1)
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))))
104 (current-column)))
105 t))))
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
122 to the text.
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."
127 (interactive "P")
128 (cond
129 ;; The region is active, indent it.
130 ((use-region-p)
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))))
138 (insert-tab arg))
140 (let ((old-tick (buffer-chars-modified-tick))
141 (old-point (point))
142 (old-indent (current-indentation)))
144 ;; Indent the line.
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))))
151 (cond
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.
160 (arg
161 (let ((end-marker
162 (save-excursion
163 (forward-line 0) (forward-sexp) (point-marker)))
164 (indentation-change (- (current-indentation) old-indent)))
165 (save-excursion
166 (forward-line 1)
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)
172 (save-restriction
173 (widen)
174 (funcall func)))
176 (defun insert-tab (&optional arg)
177 (let ((count (prefix-numeric-value arg)))
178 (if (and abbrev-mode
179 (eq (char-syntax (preceding-char)) ?w))
180 (expand-abbrev))
181 (if indent-tabs-mode
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."
188 (save-excursion
189 (save-match-data
190 (let ((beg (progn (goto-char beg) (line-beginning-position)))
191 indent)
192 (goto-char beg)
193 (while (re-search-forward "^\\s-*[[:print:]]" end t)
194 (setq indent (min (or indent (current-indentation))
195 (current-indentation))))
196 indent))))
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)
204 map)
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)
224 (progn
225 (message
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))
229 (save-excursion
230 (goto-char end)
231 (setq end (point-marker))
232 (goto-char start)
233 (or (bolp) (forward-line 1))
234 (while (< (point) end)
235 (let ((indent (current-indentation))
236 eol-flag)
237 (save-excursion
238 (skip-chars-forward " \t")
239 (setq eol-flag (eolp)))
240 (or eol-flag
241 (indent-to (max 0 (+ indent (prefix-numeric-value arg))) 0))
242 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
243 (forward-line 1))
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."
259 (interactive "r")
260 (indent-rigidly--pop-undo)
261 (indent-rigidly
262 beg end
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."
267 (interactive "r")
268 (indent-rigidly--pop-undo)
269 (indent-rigidly
270 beg end
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."
275 (interactive "r")
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."
284 (interactive "r")
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))))
301 (indent-to column))
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."
310 (save-excursion
311 (back-to-indentation)
312 (max 0
313 (+ left-margin (or (get-text-property
314 (if (and (eobp) (not (bobp)))
315 (1- (point)) (point))
316 'left-margin) 0)))))
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))
330 (beginning-of-line))
331 (let ((lm (current-left-margin))
332 (cc (current-column)))
333 (cond ((> cc lm)
334 (if (> (move-to-column lm force) lm)
335 ;; If lm is in a tab and we are not forcing, move before tab
336 (backward-char 1)))
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")
348 (bolp))
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."
356 (save-excursion
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)))
363 (forward-line 1))
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: ")
373 (save-excursion
374 ;; If inside indentation, start from BOL.
375 (goto-char from)
376 (skip-chars-backward " \t")
377 (if (bolp) (setq from (point)))
378 ;; Place end after whitespace
379 (goto-char to)
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: ")
396 (save-excursion
397 (goto-char from)
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."
408 (let ((begin from)
409 end val)
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)
413 (setq begin end))
414 (if (< begin to)
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))
425 (save-excursion
426 (goto-char from)
427 (skip-chars-backward " \t")
428 (if (bolp) (setq from (point)))
429 (goto-char to)
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."
454 (interactive "r\nP")
455 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
456 (save-excursion
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)."
480 (interactive "p")
481 (beginning-of-line n)
482 (skip-chars-forward " \t")
483 ;; Skip over fill-prefix.
484 (if (and fill-prefix
485 (not (string-equal fill-prefix "")))
486 (if (equal fill-prefix
487 (buffer-substring
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
510 with it.
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."
518 (interactive "r\nP")
519 (cond
520 ;; If a numeric prefix is given, indent to that column.
521 (column
522 (setq column (prefix-numeric-value column))
523 (save-excursion
524 (goto-char end)
525 (setq end (point-marker))
526 (goto-char start)
527 (or (bolp) (forward-line 1))
528 (while (< (point) end)
529 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
530 (or (eolp)
531 (indent-to column 0))
532 (forward-line 1))
533 (move-marker end nil)))
534 ;; If a fill-prefix is specified, use it.
535 (fill-prefix
536 (save-excursion
537 (goto-char end)
538 (setq end (point-marker))
539 (goto-char start)
540 (let ((regexp (regexp-quote fill-prefix)))
541 (while (< (point) end)
542 (or (looking-at regexp)
543 (and (bolp) (eolp))
544 (insert fill-prefix))
545 (forward-line 1)))))
546 ;; Use indent-region-function is available.
547 (indent-region-function
548 (save-restriction
549 (widen)
550 (funcall indent-region-function start end)))
551 ;; Else, use a default implementation that calls indent-line-function on
552 ;; each line.
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
556 ;; by hand.
557 (setq deactivate-mark t))
559 (defun indent-region-line-by-line (start end)
560 (save-excursion
561 (setq end (copy-marker end))
562 (goto-char start)
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))
568 (forward-line 1)
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'."
582 (interactive)
583 (indent-relative t))
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.
589 ^ ^ ^ ^ ^ ^ ^ ^ ^
590 If FIRST-ONLY is non-nil, then only the first indent point is
591 considered.
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
596 in this case.
598 See also `indent-relative-first-indent-point'."
599 (interactive "P")
600 (if (and abbrev-mode
601 (eq (char-syntax (preceding-char)) ?w))
602 (expand-abbrev))
603 (let ((start-column (current-column))
604 indent)
605 (save-excursion
606 (beginning-of-line)
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)
612 (backward-char 1))
613 (or (looking-at "[ \t]")
614 first-only
615 (skip-chars-forward "^ \t" end))
616 (skip-chars-forward " \t" end)
617 (or (= (point) end) (setq indent (current-column))))))
618 (cond (indent
619 (let ((opoint (point-marker)))
620 (indent-to indent 0)
621 (if (> opoint (point))
622 (goto-char opoint))
623 (move-marker opoint nil)))
624 (unindented-ok 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."
634 :group 'indent
635 :version "24.4" ; from explicit list to nil
636 :safe 'listp
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)
643 map)
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."
655 (interactive)
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)
660 (overwrite-mode 1)
661 (setq truncate-lines t)
662 (erase-buffer)
663 (let ((tabs tab-stop-list))
664 (while tabs
665 (indent-to (car tabs) 0)
666 (insert ?:)
667 (setq tabs (cdr tabs))))
668 (let ((count 0))
669 (insert ?\n)
670 (while (< count 8)
671 (insert (+ count ?0))
672 (insert " ")
673 (setq count (1+ count)))
674 (insert ?\n)
675 (while (> count 0)
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."
683 (interactive)
684 (let (tabs)
685 (save-excursion
686 (goto-char 1)
687 (end-of-line)
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)))
701 (if tabs
702 (if (not prev)
703 (car 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)
708 (car prevtabs)))))
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."
729 (interactive)
730 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
731 (expand-abbrev))
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."
740 (interactive)
741 (let ((nexttab (indent-next-tab-stop (current-column))))
742 (let ((before (point)))
743 (move-to-column nexttab t)
744 (save-excursion
745 (goto-char before)
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))
753 (forward-char -1))
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