ChangeLog fixes from M-x authors
[emacs.git] / lisp / indent.el
blobc7e2c72950aa4672ceb868ae892b4309f37b04a5
1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985, 1995, 2001-2013 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 (start end arg)
158 "Indent all lines starting in the region sideways by ARG columns.
159 Called from a program, takes three arguments, START, END and ARG.
160 You can remove all indentation from a region by giving a large negative ARG."
161 (interactive "r\np")
162 (save-excursion
163 (goto-char end)
164 (setq end (point-marker))
165 (goto-char start)
166 (or (bolp) (forward-line 1))
167 (while (< (point) end)
168 (let ((indent (current-indentation))
169 eol-flag)
170 (save-excursion
171 (skip-chars-forward " \t")
172 (setq eol-flag (eolp)))
173 (or eol-flag
174 (indent-to (max 0 (+ indent arg)) 0))
175 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
176 (forward-line 1))
177 (move-marker end nil)))
179 (defun indent-line-to (column)
180 "Indent current line to COLUMN.
181 This function removes or adds spaces and tabs at beginning of line
182 only if necessary. It leaves point at end of indentation."
183 (back-to-indentation)
184 (let ((cur-col (current-column)))
185 (cond ((< cur-col column)
186 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
187 (delete-region (point)
188 (progn (skip-chars-backward " ") (point))))
189 (indent-to column))
190 ((> cur-col column) ; too far right (after tab?)
191 (delete-region (progn (move-to-column column t) (point))
192 (progn (back-to-indentation) (point)))))))
194 (defun current-left-margin ()
195 "Return the left margin to use for this line.
196 This is the value of the buffer-local variable `left-margin' plus the value
197 of the `left-margin' text-property at the start of the line."
198 (save-excursion
199 (back-to-indentation)
200 (max 0
201 (+ left-margin (or (get-text-property
202 (if (and (eobp) (not (bobp)))
203 (1- (point)) (point))
204 'left-margin) 0)))))
206 (defun move-to-left-margin (&optional n force)
207 "Move to the left margin of the current line.
208 With optional argument, move forward N-1 lines first.
209 The column moved to is the one given by the `current-left-margin' function.
210 If the line's indentation appears to be wrong, and this command is called
211 interactively or with optional argument FORCE, it will be fixed."
212 (interactive (list (prefix-numeric-value current-prefix-arg) t))
213 (beginning-of-line n)
214 (skip-chars-forward " \t")
215 (if (minibufferp (current-buffer))
216 (if (save-excursion (beginning-of-line) (bobp))
217 (goto-char (minibuffer-prompt-end))
218 (beginning-of-line))
219 (let ((lm (current-left-margin))
220 (cc (current-column)))
221 (cond ((> cc lm)
222 (if (> (move-to-column lm force) lm)
223 ;; If lm is in a tab and we are not forcing, move before tab
224 (backward-char 1)))
225 ((and force (< cc lm))
226 (indent-to-left-margin))))))
228 ;; This used to be the default indent-line-function,
229 ;; used in Fundamental Mode, Text Mode, etc.
230 (defun indent-to-left-margin ()
231 "Indent current line to the column given by `current-left-margin'."
232 (save-excursion (indent-line-to (current-left-margin)))
233 ;; If we are within the indentation, move past it.
234 (when (save-excursion
235 (skip-chars-backward " \t")
236 (bolp))
237 (skip-chars-forward " \t")))
239 (defun delete-to-left-margin (&optional from to)
240 "Remove left margin indentation from a region.
241 This deletes to the column given by `current-left-margin'.
242 In no case will it delete non-whitespace.
243 Args FROM and TO are optional; default is the whole buffer."
244 (save-excursion
245 (goto-char (or to (point-max)))
246 (setq to (point-marker))
247 (goto-char (or from (point-min)))
248 (or (bolp) (forward-line 1))
249 (while (< (point) to)
250 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
251 (forward-line 1))
252 (move-marker to nil)))
254 (defun set-left-margin (from to width)
255 "Set the left margin of the region to WIDTH.
256 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
258 Interactively, WIDTH is the prefix argument, if specified.
259 Without prefix argument, the command prompts for WIDTH."
260 (interactive "r\nNSet left margin to column: ")
261 (save-excursion
262 ;; If inside indentation, start from BOL.
263 (goto-char from)
264 (skip-chars-backward " \t")
265 (if (bolp) (setq from (point)))
266 ;; Place end after whitespace
267 (goto-char to)
268 (skip-chars-forward " \t")
269 (setq to (point-marker)))
270 ;; Delete margin indentation first, but keep paragraph indentation.
271 (delete-to-left-margin from to)
272 (put-text-property from to 'left-margin width)
273 (indent-rigidly from to width)
274 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
275 (move-marker to nil))
277 (defun set-right-margin (from to width)
278 "Set the right margin of the region to WIDTH.
279 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
281 Interactively, WIDTH is the prefix argument, if specified.
282 Without prefix argument, the command prompts for WIDTH."
283 (interactive "r\nNSet right margin to width: ")
284 (save-excursion
285 (goto-char from)
286 (skip-chars-backward " \t")
287 (if (bolp) (setq from (point))))
288 (put-text-property from to 'right-margin width)
289 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
291 (defun alter-text-property (from to prop func &optional object)
292 "Programmatically change value of a text-property.
293 For each region between FROM and TO that has a single value for PROPERTY,
294 apply FUNCTION to that value and sets the property to the function's result.
295 Optional fifth argument OBJECT specifies the string or buffer to operate on."
296 (let ((begin from)
297 end val)
298 (while (setq val (get-text-property begin prop object)
299 end (text-property-not-all begin to prop val object))
300 (put-text-property begin end prop (funcall func val) object)
301 (setq begin end))
302 (if (< begin to)
303 (put-text-property begin to prop (funcall func val) object))))
305 (defun increase-left-margin (from to inc)
306 "Increase or decrease the left-margin of the region.
307 With no prefix argument, this adds `standard-indent' of indentation.
308 A prefix arg (optional third arg INC noninteractively) specifies the amount
309 to change the margin by, in characters.
310 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
311 (interactive "*r\nP")
312 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
313 (save-excursion
314 (goto-char from)
315 (skip-chars-backward " \t")
316 (if (bolp) (setq from (point)))
317 (goto-char to)
318 (setq to (point-marker)))
319 (alter-text-property from to 'left-margin
320 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
321 (indent-rigidly from to inc)
322 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
323 (move-marker to nil))
325 (defun decrease-left-margin (from to inc)
326 "Make the left margin of the region smaller.
327 With no prefix argument, decrease the indentation by `standard-indent'.
328 A prefix arg (optional third arg INC noninteractively) specifies the amount
329 to change the margin by, in characters.
330 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
331 (interactive "*r\nP")
332 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
333 (increase-left-margin from to (- inc)))
335 (defun increase-right-margin (from to inc)
336 "Increase the right-margin of the region.
337 With no prefix argument, increase the right margin by `standard-indent'.
338 A prefix arg (optional third arg INC noninteractively) specifies the amount
339 to change the margin by, in characters. A negative argument decreases
340 the right margin width.
341 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
342 (interactive "r\nP")
343 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
344 (save-excursion
345 (alter-text-property from to 'right-margin
346 (lambda (v) (+ inc (or v 0))))
347 (if auto-fill-function
348 (fill-region from to nil t t))))
350 (defun decrease-right-margin (from to inc)
351 "Make the right margin of the region smaller.
352 With no prefix argument, decrease the right margin by `standard-indent'.
353 A prefix arg (optional third arg INC noninteractively) specifies the amount
354 of width to remove, in characters. A negative argument increases
355 the right margin width.
356 If `auto-fill-mode' is active, re-fills region to fit in new margin."
357 (interactive "*r\nP")
358 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
359 (increase-right-margin from to (- inc)))
361 (defun beginning-of-line-text (&optional n)
362 "Move to the beginning of the text on this line.
363 With optional argument, move forward N-1 lines first.
364 From the beginning of the line, moves past the left-margin indentation, the
365 fill-prefix, and any indentation used for centering or right-justifying the
366 line, but does not move past any whitespace that was explicitly inserted
367 \(such as a tab used to indent the first line of a paragraph)."
368 (interactive "p")
369 (beginning-of-line n)
370 (skip-chars-forward " \t")
371 ;; Skip over fill-prefix.
372 (if (and fill-prefix
373 (not (string-equal fill-prefix "")))
374 (if (equal fill-prefix
375 (buffer-substring
376 (point) (min (point-max) (+ (length fill-prefix) (point)))))
377 (forward-char (length fill-prefix)))
378 (if (and adaptive-fill-mode adaptive-fill-regexp
379 (looking-at adaptive-fill-regexp))
380 (goto-char (match-end 0))))
381 ;; Skip centering or flushright indentation
382 (if (memq (current-justification) '(center right))
383 (skip-chars-forward " \t")))
385 (defvar indent-region-function nil
386 "Short cut function to indent region using `indent-according-to-mode'.
387 A value of nil means really run `indent-according-to-mode' on each line.")
389 (defun indent-region (start end &optional column)
390 "Indent each nonblank line in the region.
391 A numeric prefix argument specifies a column: indent each line to that column.
393 With no prefix argument, the command chooses one of these methods and
394 indents all the lines with it:
396 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
397 beginning of each line in the region that does not already begin
398 with it.
399 2) If `indent-region-function' is non-nil, call that function
400 to indent the region.
401 3) Indent each line via `indent-according-to-mode'.
403 Called from a program, START and END specify the region to indent.
404 If the third argument COLUMN is an integer, it specifies the
405 column to indent to; if it is nil, use one of the three methods above."
406 (interactive "r\nP")
407 (cond
408 (column
409 (setq column (prefix-numeric-value column))
410 (save-excursion
411 (goto-char end)
412 (setq end (point-marker))
413 (goto-char start)
414 (or (bolp) (forward-line 1))
415 (while (< (point) end)
416 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
417 (or (eolp)
418 (indent-to column 0))
419 (forward-line 1))
420 (move-marker end nil)))
421 (fill-prefix
422 (save-excursion
423 (goto-char end)
424 (setq end (point-marker))
425 (goto-char start)
426 (let ((regexp (regexp-quote fill-prefix)))
427 (while (< (point) end)
428 (or (looking-at regexp)
429 (and (bolp) (eolp))
430 (insert fill-prefix))
431 (forward-line 1)))))
432 (indent-region-function
433 (funcall indent-region-function start end))
435 (save-excursion
436 (setq end (copy-marker end))
437 (goto-char start)
438 (while (< (point) end)
439 (or (and (bolp) (eolp))
440 (indent-according-to-mode))
441 (forward-line 1))
442 (move-marker end nil))))
443 ;; In most cases, reindenting modifies the buffer, but it may also
444 ;; leave it unmodified, in which case we have to deactivate the mark
445 ;; by hand.
446 (deactivate-mark))
448 (defun indent-relative-maybe ()
449 "Indent a new line like previous nonblank line.
450 If the previous nonblank line has no indent points beyond the
451 column point starts at, this command does nothing.
453 See also `indent-relative'."
454 (interactive)
455 (indent-relative t))
457 (defun indent-relative (&optional unindented-ok)
458 "Space out to under next indent point in previous nonblank line.
459 An indent point is a non-whitespace character following whitespace.
460 The following line shows the indentation points in this line.
461 ^ ^ ^ ^ ^ ^ ^ ^ ^
462 If the previous nonblank line has no indent points beyond the
463 column point starts at, `tab-to-tab-stop' is done instead, unless
464 this command is invoked with a numeric argument, in which case it
465 does nothing.
467 See also `indent-relative-maybe'."
468 (interactive "P")
469 (if (and abbrev-mode
470 (eq (char-syntax (preceding-char)) ?w))
471 (expand-abbrev))
472 (let ((start-column (current-column))
473 indent)
474 (save-excursion
475 (beginning-of-line)
476 (if (re-search-backward "^[^\n]" nil t)
477 (let ((end (save-excursion (forward-line 1) (point))))
478 (move-to-column start-column)
479 ;; Is start-column inside a tab on this line?
480 (if (> (current-column) start-column)
481 (backward-char 1))
482 (or (looking-at "[ \t]")
483 unindented-ok
484 (skip-chars-forward "^ \t" end))
485 (skip-chars-forward " \t" end)
486 (or (= (point) end) (setq indent (current-column))))))
487 (if indent
488 (let ((opoint (point-marker)))
489 (indent-to indent 0)
490 (if (> opoint (point))
491 (goto-char opoint))
492 (move-marker opoint nil))
493 (tab-to-tab-stop))))
495 (defcustom tab-stop-list
496 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
497 "List of tab stop positions used by `tab-to-tab-stop'.
498 This should be a list of integers, ordered from smallest to largest."
499 :group 'indent
500 :type '(repeat integer))
501 (put 'tab-stop-list 'safe-local-variable 'listp)
503 (defvar edit-tab-stops-map
504 (let ((map (make-sparse-keymap)))
505 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
506 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
507 map)
508 "Keymap used in `edit-tab-stops'.")
510 (defvar edit-tab-stops-buffer nil
511 "Buffer whose tab stops are being edited.
512 This matters if the variable `tab-stop-list' is local in that buffer.")
514 (defun edit-tab-stops ()
515 "Edit the tab stops used by `tab-to-tab-stop'.
516 Creates a buffer *Tab Stops* containing text describing the tab stops.
517 A colon indicates a column where there is a tab stop.
518 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
519 (interactive)
520 (setq edit-tab-stops-buffer (current-buffer))
521 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
522 (use-local-map edit-tab-stops-map)
523 (make-local-variable 'indent-tabs-mode)
524 (setq indent-tabs-mode nil)
525 (overwrite-mode 1)
526 (setq truncate-lines t)
527 (erase-buffer)
528 (let ((tabs tab-stop-list))
529 (while tabs
530 (indent-to (car tabs) 0)
531 (insert ?:)
532 (setq tabs (cdr tabs))))
533 (let ((count 0))
534 (insert ?\n)
535 (while (< count 8)
536 (insert (+ count ?0))
537 (insert " ")
538 (setq count (1+ count)))
539 (insert ?\n)
540 (while (> count 0)
541 (insert "0123456789")
542 (setq count (1- count))))
543 (insert "\nTo install changes, type C-c C-c")
544 (goto-char (point-min)))
546 (defun edit-tab-stops-note-changes ()
547 "Put edited tab stops into effect."
548 (interactive)
549 (let (tabs)
550 (save-excursion
551 (goto-char 1)
552 (end-of-line)
553 (while (search-backward ":" nil t)
554 (setq tabs (cons (current-column) tabs))))
555 (bury-buffer (prog1 (current-buffer)
556 (switch-to-buffer edit-tab-stops-buffer)))
557 (setq tab-stop-list tabs))
558 (message "Tab stops installed"))
560 (defun tab-to-tab-stop ()
561 "Insert spaces or tabs to next defined tab-stop column.
562 The variable `tab-stop-list' is a list of columns at which there are tab stops.
563 Use \\[edit-tab-stops] to edit them interactively."
564 (interactive)
565 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
566 (expand-abbrev))
567 (let ((tabs tab-stop-list))
568 (while (and tabs (>= (current-column) (car tabs)))
569 (setq tabs (cdr tabs)))
570 (if tabs
571 (progn
572 (delete-horizontal-space t)
573 (indent-to (car tabs)))
574 (insert ?\s))))
576 (defun move-to-tab-stop ()
577 "Move point to next defined tab-stop column.
578 The variable `tab-stop-list' is a list of columns at which there are tab stops.
579 Use \\[edit-tab-stops] to edit them interactively."
580 (interactive)
581 (let ((tabs tab-stop-list))
582 (while (and tabs (>= (current-column) (car tabs)))
583 (setq tabs (cdr tabs)))
584 (if tabs
585 (let ((before (point)))
586 (move-to-column (car tabs) t)
587 (save-excursion
588 (goto-char before)
589 ;; If we just added a tab, or moved over one,
590 ;; delete any superfluous spaces before the old point.
591 (if (and (eq (preceding-char) ?\s)
592 (eq (following-char) ?\t))
593 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
594 (while (and (> (current-column) tabend)
595 (eq (preceding-char) ?\s))
596 (forward-char -1))
597 (delete-region (point) before))))))))
599 (define-key global-map "\t" 'indent-for-tab-command)
600 (define-key esc-map "\C-\\" 'indent-region)
601 (define-key ctl-x-map "\t" 'indent-rigidly)
602 (define-key esc-map "i" 'tab-to-tab-stop)
604 ;;; indent.el ends here