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