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