* epa.texi (Caching Passphrases): New section.
[emacs.git] / lisp / indent.el
blobe57d6068ef568721353f305f02b2ec150055abce
1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985, 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Commands for making and changing indentation in text. These are
27 ;; described in the Emacs manual.
29 ;;; Code:
31 (defgroup indent nil
32 "Indentation commands."
33 :group 'editing)
35 (defcustom standard-indent 4
36 "Default number of columns for margin-changing functions to indent."
37 :group 'indent
38 :type 'integer)
40 (defvar indent-line-function 'indent-relative
41 "Function to indent the current line.
42 This function will be called with no arguments.
43 If it is called somewhere where auto-indentation cannot be done
44 \(e.g. inside a string), the function should simply return `noindent'.
45 Setting this function is all you need to make TAB indent appropriately.
46 Don't rebind TAB unless you really need to.")
48 (defcustom tab-always-indent t
49 "Controls the operation of the TAB key.
50 If t, hitting TAB always just indents the current line.
51 If nil, hitting TAB indents the current line if point is at the left margin
52 or in the line's indentation, otherwise it inserts a \"real\" TAB character.
53 If `complete', TAB first tries to indent the current line, and if the line
54 was already indented, then try to complete the thing at point.
56 Some programming language modes have their own variable to control this,
57 e.g., `c-tab-always-indent', and do not respect this variable."
58 :group 'indent
59 :type '(choice
60 (const :tag "Always indent" t)
61 (const :tag "Indent if inside indentation, else TAB" nil)
62 (const :tag "Indent, or if already indented complete" complete)))
65 (defun indent-according-to-mode ()
66 "Indent line in proper way for current major mode.
67 The buffer-local variable `indent-line-function' determines how to do this,
68 but the functions `indent-relative' and `indent-relative-maybe' are
69 special; we don't actually use them here."
70 (interactive)
71 (syntax-propertize (line-end-position))
72 (if (memq indent-line-function
73 '(indent-relative indent-relative-maybe))
74 ;; These functions are used for tabbing, but can't be used for
75 ;; indenting. Replace with something ad-hoc.
76 (let ((column (save-excursion
77 (beginning-of-line)
78 (skip-chars-backward "\n \t")
79 (beginning-of-line)
80 (current-indentation))))
81 (if (<= (current-column) (current-indentation))
82 (indent-line-to column)
83 (save-excursion (indent-line-to column))))
84 ;; The normal case.
85 (funcall indent-line-function)))
87 (defun indent-for-tab-command (&optional arg)
88 "Indent line or region in proper way for current major mode or insert a tab.
89 Depending on `tab-always-indent', either insert a tab or indent.
91 In most major modes, if point was in the current line's indentation,
92 it is moved to the first non-whitespace character after indenting;
93 otherwise it stays at the same position in the text.
95 If a prefix argument is given, also rigidly indent the entire
96 balanced expression which starts at the beginning of the current
97 line to reflect the current line's change in indentation.
99 If `transient-mark-mode' is turned on and the region is active,
100 indent the region (in this case, any prefix argument is ignored).
102 The function actually called to indent the line is determined by the value of
103 `indent-line-function'."
104 (interactive "P")
105 (cond
106 ;; The region is active, indent it.
107 ((use-region-p)
108 (indent-region (region-beginning) (region-end)))
109 ((or ;; indent-to-left-margin is only meant for indenting,
110 ;; so we force it to always insert a tab here.
111 (eq indent-line-function 'indent-to-left-margin)
112 (and (not tab-always-indent)
113 (or (> (current-column) (current-indentation))
114 (eq this-command last-command))))
115 (insert-tab arg))
117 (let ((old-tick (buffer-chars-modified-tick))
118 (old-point (point))
119 (old-indent (current-indentation)))
121 ;; Indent the line.
122 (funcall indent-line-function)
124 (cond
125 ;; If the text was already indented right, try completion.
126 ((and (eq tab-always-indent 'complete)
127 (eq old-point (point))
128 (eq old-tick (buffer-chars-modified-tick)))
129 (completion-at-point))
131 ;; If a prefix argument was given, rigidly indent the following
132 ;; sexp to match the change in the current line's indentation.
133 (arg
134 (let ((end-marker
135 (save-excursion
136 (forward-line 0) (forward-sexp) (point-marker)))
137 (indentation-change (- (current-indentation) old-indent)))
138 (save-excursion
139 (forward-line 1)
140 (when (and (not (zerop indentation-change))
141 (< (point) end-marker))
142 (indent-rigidly (point) end-marker indentation-change))))))))))
144 (defun insert-tab (&optional arg)
145 (let ((count (prefix-numeric-value arg)))
146 (if (and abbrev-mode
147 (eq (char-syntax (preceding-char)) ?w))
148 (expand-abbrev))
149 (if indent-tabs-mode
150 (insert-char ?\t count)
151 (indent-to (* tab-width (+ count (/ (current-column) tab-width)))))))
153 (defun indent-rigidly (start end arg)
154 "Indent all lines starting in the region sideways by ARG columns.
155 Called from a program, takes three arguments, START, END and ARG.
156 You can remove all indentation from a region by giving a large negative ARG."
157 (interactive "r\np")
158 (save-excursion
159 (goto-char end)
160 (setq end (point-marker))
161 (goto-char start)
162 (or (bolp) (forward-line 1))
163 (while (< (point) end)
164 (let ((indent (current-indentation))
165 eol-flag)
166 (save-excursion
167 (skip-chars-forward " \t")
168 (setq eol-flag (eolp)))
169 (or eol-flag
170 (indent-to (max 0 (+ indent arg)) 0))
171 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
172 (forward-line 1))
173 (move-marker end nil)))
175 (defun indent-line-to (column)
176 "Indent current line to COLUMN.
177 This function removes or adds spaces and tabs at beginning of line
178 only if necessary. It leaves point at end of indentation."
179 (back-to-indentation)
180 (let ((cur-col (current-column)))
181 (cond ((< cur-col column)
182 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
183 (delete-region (point)
184 (progn (skip-chars-backward " ") (point))))
185 (indent-to column))
186 ((> cur-col column) ; too far right (after tab?)
187 (delete-region (progn (move-to-column column t) (point))
188 (progn (back-to-indentation) (point)))))))
190 (defun current-left-margin ()
191 "Return the left margin to use for this line.
192 This is the value of the buffer-local variable `left-margin' plus the value
193 of the `left-margin' text-property at the start of the line."
194 (save-excursion
195 (back-to-indentation)
196 (max 0
197 (+ left-margin (or (get-text-property
198 (if (and (eobp) (not (bobp)))
199 (1- (point)) (point))
200 'left-margin) 0)))))
202 (defun move-to-left-margin (&optional n force)
203 "Move to the left margin of the current line.
204 With optional argument, move forward N-1 lines first.
205 The column moved to is the one given by the `current-left-margin' function.
206 If the line's indentation appears to be wrong, and this command is called
207 interactively or with optional argument FORCE, it will be fixed."
208 (interactive (list (prefix-numeric-value current-prefix-arg) t))
209 (beginning-of-line n)
210 (skip-chars-forward " \t")
211 (if (minibufferp (current-buffer))
212 (if (save-excursion (beginning-of-line) (bobp))
213 (goto-char (minibuffer-prompt-end))
214 (beginning-of-line))
215 (let ((lm (current-left-margin))
216 (cc (current-column)))
217 (cond ((> cc lm)
218 (if (> (move-to-column lm force) lm)
219 ;; If lm is in a tab and we are not forcing, move before tab
220 (backward-char 1)))
221 ((and force (< cc lm))
222 (indent-to-left-margin))))))
224 ;; This used to be the default indent-line-function,
225 ;; used in Fundamental Mode, Text Mode, etc.
226 (defun indent-to-left-margin ()
227 "Indent current line to the column given by `current-left-margin'."
228 (save-excursion (indent-line-to (current-left-margin)))
229 ;; If we are within the indentation, move past it.
230 (when (save-excursion
231 (skip-chars-backward " \t")
232 (bolp))
233 (skip-chars-forward " \t")))
235 (defun delete-to-left-margin (&optional from to)
236 "Remove left margin indentation from a region.
237 This deletes to the column given by `current-left-margin'.
238 In no case will it delete non-whitespace.
239 Args FROM and TO are optional; default is the whole buffer."
240 (save-excursion
241 (goto-char (or to (point-max)))
242 (setq to (point-marker))
243 (goto-char (or from (point-min)))
244 (or (bolp) (forward-line 1))
245 (while (< (point) to)
246 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
247 (forward-line 1))
248 (move-marker to nil)))
250 (defun set-left-margin (from to width)
251 "Set the left margin of the region to WIDTH.
252 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
254 Interactively, WIDTH is the prefix argument, if specified.
255 Without prefix argument, the command prompts for WIDTH."
256 (interactive "r\nNSet left margin to column: ")
257 (save-excursion
258 ;; If inside indentation, start from BOL.
259 (goto-char from)
260 (skip-chars-backward " \t")
261 (if (bolp) (setq from (point)))
262 ;; Place end after whitespace
263 (goto-char to)
264 (skip-chars-forward " \t")
265 (setq to (point-marker)))
266 ;; Delete margin indentation first, but keep paragraph indentation.
267 (delete-to-left-margin from to)
268 (put-text-property from to 'left-margin width)
269 (indent-rigidly from to width)
270 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
271 (move-marker to nil))
273 (defun set-right-margin (from to width)
274 "Set the right margin of the region to WIDTH.
275 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
277 Interactively, WIDTH is the prefix argument, if specified.
278 Without prefix argument, the command prompts for WIDTH."
279 (interactive "r\nNSet right margin to width: ")
280 (save-excursion
281 (goto-char from)
282 (skip-chars-backward " \t")
283 (if (bolp) (setq from (point))))
284 (put-text-property from to 'right-margin width)
285 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
287 (defun alter-text-property (from to prop func &optional object)
288 "Programmatically change value of a text-property.
289 For each region between FROM and TO that has a single value for PROPERTY,
290 apply FUNCTION to that value and sets the property to the function's result.
291 Optional fifth argument OBJECT specifies the string or buffer to operate on."
292 (let ((begin from)
293 end val)
294 (while (setq val (get-text-property begin prop object)
295 end (text-property-not-all begin to prop val object))
296 (put-text-property begin end prop (funcall func val) object)
297 (setq begin end))
298 (if (< begin to)
299 (put-text-property begin to prop (funcall func val) object))))
301 (defun increase-left-margin (from to inc)
302 "Increase or decrease the left-margin of the region.
303 With no prefix argument, this adds `standard-indent' of indentation.
304 A prefix arg (optional third arg INC noninteractively) specifies the amount
305 to change the margin by, in characters.
306 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
307 (interactive "*r\nP")
308 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
309 (save-excursion
310 (goto-char from)
311 (skip-chars-backward " \t")
312 (if (bolp) (setq from (point)))
313 (goto-char to)
314 (setq to (point-marker)))
315 (alter-text-property from to 'left-margin
316 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
317 (indent-rigidly from to inc)
318 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
319 (move-marker to nil))
321 (defun decrease-left-margin (from to inc)
322 "Make the left margin of the region smaller.
323 With no prefix argument, decrease the indentation by `standard-indent'.
324 A prefix arg (optional third arg INC noninteractively) specifies the amount
325 to change the margin by, in characters.
326 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
327 (interactive "*r\nP")
328 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
329 (increase-left-margin from to (- inc)))
331 (defun increase-right-margin (from to inc)
332 "Increase the right-margin of the region.
333 With no prefix argument, increase the right margin by `standard-indent'.
334 A prefix arg (optional third arg INC noninteractively) specifies the amount
335 to change the margin by, in characters. A negative argument decreases
336 the right margin width.
337 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
338 (interactive "r\nP")
339 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
340 (save-excursion
341 (alter-text-property from to 'right-margin
342 (lambda (v) (+ inc (or v 0))))
343 (if auto-fill-function
344 (fill-region from to nil t t))))
346 (defun decrease-right-margin (from to inc)
347 "Make the right margin of the region smaller.
348 With no prefix argument, decrease the right margin by `standard-indent'.
349 A prefix arg (optional third arg INC noninteractively) specifies the amount
350 of width to remove, in characters. A negative argument increases
351 the right margin width.
352 If `auto-fill-mode' is active, re-fills region to fit in new margin."
353 (interactive "*r\nP")
354 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
355 (increase-right-margin from to (- inc)))
357 (defun beginning-of-line-text (&optional n)
358 "Move to the beginning of the text on this line.
359 With optional argument, move forward N-1 lines first.
360 From the beginning of the line, moves past the left-margin indentation, the
361 fill-prefix, and any indentation used for centering or right-justifying the
362 line, but does not move past any whitespace that was explicitly inserted
363 \(such as a tab used to indent the first line of a paragraph)."
364 (interactive "p")
365 (beginning-of-line n)
366 (skip-chars-forward " \t")
367 ;; Skip over fill-prefix.
368 (if (and fill-prefix
369 (not (string-equal fill-prefix "")))
370 (if (equal fill-prefix
371 (buffer-substring
372 (point) (min (point-max) (+ (length fill-prefix) (point)))))
373 (forward-char (length fill-prefix)))
374 (if (and adaptive-fill-mode adaptive-fill-regexp
375 (looking-at adaptive-fill-regexp))
376 (goto-char (match-end 0))))
377 ;; Skip centering or flushright indentation
378 (if (memq (current-justification) '(center right))
379 (skip-chars-forward " \t")))
381 (defvar indent-region-function nil
382 "Short cut function to indent region using `indent-according-to-mode'.
383 A value of nil means really run `indent-according-to-mode' on each line.")
385 (defun indent-region (start end &optional column)
386 "Indent each nonblank line in the region.
387 A numeric prefix argument specifies a column: indent each line to that column.
389 With no prefix argument, the command chooses one of these methods and
390 indents all the lines with it:
392 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
393 beginning of each line in the region that does not already begin
394 with it.
395 2) If `indent-region-function' is non-nil, call that function
396 to indent the region.
397 3) Indent each line as specified by the variable `indent-line-function'.
399 Called from a program, START and END specify the region to indent.
400 If the third argument COLUMN is an integer, it specifies the
401 column to indent to; if it is nil, use one of the three methods above."
402 (interactive "r\nP")
403 (if (null column)
404 (if fill-prefix
405 (save-excursion
406 (goto-char end)
407 (setq end (point-marker))
408 (goto-char start)
409 (let ((regexp (regexp-quote fill-prefix)))
410 (while (< (point) end)
411 (or (looking-at regexp)
412 (and (bolp) (eolp))
413 (insert fill-prefix))
414 (forward-line 1))))
415 (if indent-region-function
416 (funcall indent-region-function start end)
417 (save-excursion
418 (setq end (copy-marker end))
419 (goto-char start)
420 (while (< (point) end)
421 (or (and (bolp) (eolp))
422 (indent-according-to-mode))
423 (forward-line 1))
424 (move-marker end nil))))
425 (setq column (prefix-numeric-value column))
426 (save-excursion
427 (goto-char end)
428 (setq end (point-marker))
429 (goto-char start)
430 (or (bolp) (forward-line 1))
431 (while (< (point) end)
432 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
433 (or (eolp)
434 (indent-to column 0))
435 (forward-line 1))
436 (move-marker end nil)))
437 ;; In most cases, reindenting modifies the buffer, but it may also
438 ;; leave it unmodified, in which case we have to deactivate the mark
439 ;; by hand.
440 (deactivate-mark))
442 (defun indent-relative-maybe ()
443 "Indent a new line like previous nonblank line.
444 If the previous nonblank line has no indent points beyond the
445 column point starts at, this command does nothing.
447 See also `indent-relative'."
448 (interactive)
449 (indent-relative t))
451 (defun indent-relative (&optional unindented-ok)
452 "Space out to under next indent point in previous nonblank line.
453 An indent point is a non-whitespace character following whitespace.
454 The following line shows the indentation points in this line.
455 ^ ^ ^ ^ ^ ^ ^ ^ ^
456 If the previous nonblank line has no indent points beyond the
457 column point starts at, `tab-to-tab-stop' is done instead, unless
458 this command is invoked with a numeric argument, in which case it
459 does nothing.
461 See also `indent-relative-maybe'."
462 (interactive "P")
463 (if (and abbrev-mode
464 (eq (char-syntax (preceding-char)) ?w))
465 (expand-abbrev))
466 (let ((start-column (current-column))
467 indent)
468 (save-excursion
469 (beginning-of-line)
470 (if (re-search-backward "^[^\n]" nil t)
471 (let ((end (save-excursion (forward-line 1) (point))))
472 (move-to-column start-column)
473 ;; Is start-column inside a tab on this line?
474 (if (> (current-column) start-column)
475 (backward-char 1))
476 (or (looking-at "[ \t]")
477 unindented-ok
478 (skip-chars-forward "^ \t" end))
479 (skip-chars-forward " \t" end)
480 (or (= (point) end) (setq indent (current-column))))))
481 (if indent
482 (let ((opoint (point-marker)))
483 (indent-to indent 0)
484 (if (> opoint (point))
485 (goto-char opoint))
486 (move-marker opoint nil))
487 (tab-to-tab-stop))))
489 (defcustom tab-stop-list
490 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
491 "List of tab stop positions used by `tab-to-tab-stop'.
492 This should be a list of integers, ordered from smallest to largest."
493 :group 'indent
494 :type '(repeat integer))
495 (put 'tab-stop-list 'safe-local-variable 'listp)
497 (defvar edit-tab-stops-map
498 (let ((map (make-sparse-keymap)))
499 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
500 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
501 map)
502 "Keymap used in `edit-tab-stops'.")
504 (defvar edit-tab-stops-buffer nil
505 "Buffer whose tab stops are being edited.
506 This matters if the variable `tab-stop-list' is local in that buffer.")
508 (defun edit-tab-stops ()
509 "Edit the tab stops used by `tab-to-tab-stop'.
510 Creates a buffer *Tab Stops* containing text describing the tab stops.
511 A colon indicates a column where there is a tab stop.
512 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
513 (interactive)
514 (setq edit-tab-stops-buffer (current-buffer))
515 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
516 (use-local-map edit-tab-stops-map)
517 (make-local-variable 'indent-tabs-mode)
518 (setq indent-tabs-mode nil)
519 (overwrite-mode 1)
520 (setq truncate-lines t)
521 (erase-buffer)
522 (let ((tabs tab-stop-list))
523 (while tabs
524 (indent-to (car tabs) 0)
525 (insert ?:)
526 (setq tabs (cdr tabs))))
527 (let ((count 0))
528 (insert ?\n)
529 (while (< count 8)
530 (insert (+ count ?0))
531 (insert " ")
532 (setq count (1+ count)))
533 (insert ?\n)
534 (while (> count 0)
535 (insert "0123456789")
536 (setq count (1- count))))
537 (insert "\nTo install changes, type C-c C-c")
538 (goto-char (point-min)))
540 (defun edit-tab-stops-note-changes ()
541 "Put edited tab stops into effect."
542 (interactive)
543 (let (tabs)
544 (save-excursion
545 (goto-char 1)
546 (end-of-line)
547 (while (search-backward ":" nil t)
548 (setq tabs (cons (current-column) tabs))))
549 (bury-buffer (prog1 (current-buffer)
550 (switch-to-buffer edit-tab-stops-buffer)))
551 (setq tab-stop-list tabs))
552 (message "Tab stops installed"))
554 (defun tab-to-tab-stop ()
555 "Insert spaces or tabs to next defined tab-stop column.
556 The variable `tab-stop-list' is a list of columns at which there are tab stops.
557 Use \\[edit-tab-stops] to edit them interactively."
558 (interactive)
559 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
560 (expand-abbrev))
561 (let ((tabs tab-stop-list))
562 (while (and tabs (>= (current-column) (car tabs)))
563 (setq tabs (cdr tabs)))
564 (if tabs
565 (let ((opoint (point)))
566 (delete-horizontal-space t)
567 (indent-to (car tabs)))
568 (insert ?\s))))
570 (defun move-to-tab-stop ()
571 "Move point to next defined tab-stop column.
572 The variable `tab-stop-list' is a list of columns at which there are tab stops.
573 Use \\[edit-tab-stops] to edit them interactively."
574 (interactive)
575 (let ((tabs tab-stop-list))
576 (while (and tabs (>= (current-column) (car tabs)))
577 (setq tabs (cdr tabs)))
578 (if tabs
579 (let ((before (point)))
580 (move-to-column (car tabs) t)
581 (save-excursion
582 (goto-char before)
583 ;; If we just added a tab, or moved over one,
584 ;; delete any superfluous spaces before the old point.
585 (if (and (eq (preceding-char) ?\s)
586 (eq (following-char) ?\t))
587 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
588 (while (and (> (current-column) tabend)
589 (eq (preceding-char) ?\s))
590 (forward-char -1))
591 (delete-region (point) before))))))))
593 (define-key global-map "\t" 'indent-for-tab-command)
594 (define-key esc-map "\C-\\" 'indent-region)
595 (define-key ctl-x-map "\t" 'indent-rigidly)
596 (define-key esc-map "i" 'tab-to-tab-stop)
598 ;; arch-tag: f402b2a7-e44f-492f-b5b8-38996020b7c3
599 ;;; indent.el ends here