Minor fixes to version-control material suggseted by RMS and Robert
[emacs.git] / lisp / indent.el
blobdb8958bd1ca5faae9fabe82aa87679360f127540
1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985, 1995, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
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, or (at your option)
13 ;; 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; 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.
25 ;;; Commentary:
27 ;; Commands for making and changing indentation in text. These are
28 ;; described in the Emacs manual.
30 ;;; Code:
32 (defgroup indent nil
33 "Indentation commands."
34 :group 'editing)
36 (defcustom standard-indent 4
37 "*Default number of columns for margin-changing functions to indent."
38 :group 'indent
39 :type 'integer)
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.")
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)))
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)))
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 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.
108 (indent-according-to-mode))))
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)))))))
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)))
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)))))))
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)))))
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))))))
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 (indent-line-to (current-left-margin)))
196 (defun delete-to-left-margin (&optional from to)
197 "Remove left margin indentation from a region.
198 This deletes to the column given by `current-left-margin'.
199 In no case will it delete non-whitespace.
200 Args FROM and TO are optional; default is the whole buffer."
201 (save-excursion
202 (goto-char (or to (point-max)))
203 (setq to (point-marker))
204 (goto-char (or from (point-min)))
205 (or (bolp) (forward-line 1))
206 (while (< (point) to)
207 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
208 (forward-line 1))
209 (move-marker to nil)))
211 (defun set-left-margin (from to width)
212 "Set the left margin of the region to WIDTH.
213 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
215 Interactively, WIDTH is the prefix argument, if specified.
216 Without prefix argument, the command prompts for WIDTH."
217 (interactive "r\nNSet left margin to column: ")
218 (save-excursion
219 ;; If inside indentation, start from BOL.
220 (goto-char from)
221 (skip-chars-backward " \t")
222 (if (bolp) (setq from (point)))
223 ;; Place end after whitespace
224 (goto-char to)
225 (skip-chars-forward " \t")
226 (setq to (point-marker)))
227 ;; Delete margin indentation first, but keep paragraph indentation.
228 (delete-to-left-margin from to)
229 (put-text-property from to 'left-margin width)
230 (indent-rigidly from to width)
231 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
232 (move-marker to nil))
234 (defun set-right-margin (from to width)
235 "Set the right margin of the region to WIDTH.
236 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
238 Interactively, WIDTH is the prefix argument, if specified.
239 Without prefix argument, the command prompts for WIDTH."
240 (interactive "r\nNSet right margin to width: ")
241 (save-excursion
242 (goto-char from)
243 (skip-chars-backward " \t")
244 (if (bolp) (setq from (point))))
245 (put-text-property from to 'right-margin width)
246 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
248 (defun alter-text-property (from to prop func &optional object)
249 "Programmatically change value of a text-property.
250 For each region between FROM and TO that has a single value for PROPERTY,
251 apply FUNCTION to that value and sets the property to the function's result.
252 Optional fifth argument OBJECT specifies the string or buffer to operate on."
253 (let ((begin from)
254 end val)
255 (while (setq val (get-text-property begin prop object)
256 end (text-property-not-all begin to prop val object))
257 (put-text-property begin end prop (funcall func val) object)
258 (setq begin end))
259 (if (< begin to)
260 (put-text-property begin to prop (funcall func val) object))))
262 (defun increase-left-margin (from to inc)
263 "Increase or decrease the left-margin of the region.
264 With no prefix argument, this adds `standard-indent' of indentation.
265 A prefix arg (optional third arg INC noninteractively) specifies the amount
266 to change the margin by, in characters.
267 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
268 (interactive "*r\nP")
269 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
270 (save-excursion
271 (goto-char from)
272 (skip-chars-backward " \t")
273 (if (bolp) (setq from (point)))
274 (goto-char to)
275 (setq to (point-marker)))
276 (alter-text-property from to 'left-margin
277 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
278 (indent-rigidly from to inc)
279 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
280 (move-marker to nil))
282 (defun decrease-left-margin (from to inc)
283 "Make the left margin of the region smaller.
284 With no prefix argument, decrease the indentation by `standard-indent'.
285 A prefix arg (optional third arg INC noninteractively) specifies the amount
286 to change the margin by, in characters.
287 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
288 (interactive "*r\nP")
289 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
290 (increase-left-margin from to (- inc)))
292 (defun increase-right-margin (from to inc)
293 "Increase the right-margin of the region.
294 With no prefix argument, increase the right margin by `standard-indent'.
295 A prefix arg (optional third arg INC noninteractively) specifies the amount
296 to change the margin by, in characters. A negative argument decreases
297 the right margin width.
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 (alter-text-property from to 'right-margin
303 (lambda (v) (+ inc (or v 0))))
304 (if auto-fill-function
305 (fill-region from to nil t t))))
307 (defun decrease-right-margin (from to inc)
308 "Make the right margin of the region smaller.
309 With no prefix argument, decrease the right margin by `standard-indent'.
310 A prefix arg (optional third arg INC noninteractively) specifies the amount
311 of width to remove, in characters. A negative argument increases
312 the right margin width.
313 If `auto-fill-mode' is active, re-fills region to fit in new margin."
314 (interactive "*r\nP")
315 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
316 (increase-right-margin from to (- inc)))
318 (defun beginning-of-line-text (&optional n)
319 "Move to the beginning of the text on this line.
320 With optional argument, move forward N-1 lines first.
321 From the beginning of the line, moves past the left-margin indentation, the
322 fill-prefix, and any indentation used for centering or right-justifying the
323 line, but does not move past any whitespace that was explicitly inserted
324 \(such as a tab used to indent the first line of a paragraph)."
325 (interactive "p")
326 (beginning-of-line n)
327 (skip-chars-forward " \t")
328 ;; Skip over fill-prefix.
329 (if (and fill-prefix
330 (not (string-equal fill-prefix "")))
331 (if (equal fill-prefix
332 (buffer-substring
333 (point) (min (point-max) (+ (length fill-prefix) (point)))))
334 (forward-char (length fill-prefix)))
335 (if (and adaptive-fill-mode adaptive-fill-regexp
336 (looking-at adaptive-fill-regexp))
337 (goto-char (match-end 0))))
338 ;; Skip centering or flushright indentation
339 (if (memq (current-justification) '(center right))
340 (skip-chars-forward " \t")))
342 (defvar indent-region-function nil
343 "Short cut function to indent region using `indent-according-to-mode'.
344 A value of nil means really run `indent-according-to-mode' on each line.")
346 (defun indent-region (start end &optional column)
347 "Indent each nonblank line in the region.
348 A numeric prefix argument specifies a column: indent each line to that column.
350 With no prefix argument, the command chooses one of these methods and
351 indents all the lines with it:
353 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
354 beginning of each line in the region that does not already begin
355 with it.
356 2) If `indent-region-function' is non-nil, call that function
357 to indent the region.
358 3) Indent each line as specified by the variable `indent-line-function'.
360 Called from a program, START and END specify the region to indent.
361 If the third argument COLUMN is an integer, it specifies the
362 column to indent to; if it is nil, use one of the three methods above."
363 (interactive "r\nP")
364 (if (null column)
365 (if fill-prefix
366 (save-excursion
367 (goto-char end)
368 (setq end (point-marker))
369 (goto-char start)
370 (let ((regexp (regexp-quote fill-prefix)))
371 (while (< (point) end)
372 (or (looking-at regexp)
373 (and (bolp) (eolp))
374 (insert fill-prefix))
375 (forward-line 1))))
376 (if indent-region-function
377 (funcall indent-region-function start end)
378 (save-excursion
379 (setq end (copy-marker end))
380 (goto-char start)
381 (while (< (point) end)
382 (or (and (bolp) (eolp))
383 (funcall indent-line-function))
384 (forward-line 1))
385 (move-marker end nil))))
386 (setq column (prefix-numeric-value column))
387 (save-excursion
388 (goto-char end)
389 (setq end (point-marker))
390 (goto-char start)
391 (or (bolp) (forward-line 1))
392 (while (< (point) end)
393 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
394 (or (eolp)
395 (indent-to column 0))
396 (forward-line 1))
397 (move-marker end nil))))
399 (defun indent-relative-maybe ()
400 "Indent a new line like previous nonblank line.
401 If the previous nonblank line has no indent points beyond the
402 column point starts at, this command does nothing.
404 See also `indent-relative'."
405 (interactive)
406 (indent-relative t))
408 (defun indent-relative (&optional unindented-ok)
409 "Space out to under next indent point in previous nonblank line.
410 An indent point is a non-whitespace character following whitespace.
411 The following line shows the indentation points in this line.
412 ^ ^ ^ ^ ^ ^ ^ ^ ^
413 If the previous nonblank line has no indent points beyond the
414 column point starts at, `tab-to-tab-stop' is done instead, unless
415 this command is invoked with a numeric argument, in which case it
416 does nothing.
418 See also `indent-relative-maybe'."
419 (interactive "P")
420 (if (and abbrev-mode
421 (eq (char-syntax (preceding-char)) ?w))
422 (expand-abbrev))
423 (let ((start-column (current-column))
424 indent)
425 (save-excursion
426 (beginning-of-line)
427 (if (re-search-backward "^[^\n]" nil t)
428 (let ((end (save-excursion (forward-line 1) (point))))
429 (move-to-column start-column)
430 ;; Is start-column inside a tab on this line?
431 (if (> (current-column) start-column)
432 (backward-char 1))
433 (or (looking-at "[ \t]")
434 unindented-ok
435 (skip-chars-forward "^ \t" end))
436 (skip-chars-forward " \t" end)
437 (or (= (point) end) (setq indent (current-column))))))
438 (if indent
439 (let ((opoint (point-marker)))
440 (indent-to indent 0)
441 (if (> opoint (point))
442 (goto-char opoint))
443 (move-marker opoint nil))
444 (tab-to-tab-stop))))
446 (defcustom tab-stop-list
447 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
448 "*List of tab stop positions used by `tab-to-tab-stop'.
449 This should be a list of integers, ordered from smallest to largest."
450 :group 'indent
451 :type '(repeat integer))
453 (defvar edit-tab-stops-map
454 (let ((map (make-sparse-keymap)))
455 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
456 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
457 map)
458 "Keymap used in `edit-tab-stops'.")
460 (defvar edit-tab-stops-buffer nil
461 "Buffer whose tab stops are being edited.
462 This matters if the variable `tab-stop-list' is local in that buffer.")
464 (defun edit-tab-stops ()
465 "Edit the tab stops used by `tab-to-tab-stop'.
466 Creates a buffer *Tab Stops* containing text describing the tab stops.
467 A colon indicates a column where there is a tab stop.
468 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
469 (interactive)
470 (setq edit-tab-stops-buffer (current-buffer))
471 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
472 (use-local-map edit-tab-stops-map)
473 (make-local-variable 'indent-tabs-mode)
474 (setq indent-tabs-mode nil)
475 (overwrite-mode 1)
476 (setq truncate-lines t)
477 (erase-buffer)
478 (let ((tabs tab-stop-list))
479 (while tabs
480 (indent-to (car tabs) 0)
481 (insert ?:)
482 (setq tabs (cdr tabs))))
483 (let ((count 0))
484 (insert ?\n)
485 (while (< count 8)
486 (insert (+ count ?0))
487 (insert " ")
488 (setq count (1+ count)))
489 (insert ?\n)
490 (while (> count 0)
491 (insert "0123456789")
492 (setq count (1- count))))
493 (insert "\nTo install changes, type C-c C-c")
494 (goto-char (point-min)))
496 (defun edit-tab-stops-note-changes ()
497 "Put edited tab stops into effect."
498 (interactive)
499 (let (tabs)
500 (save-excursion
501 (goto-char 1)
502 (end-of-line)
503 (while (search-backward ":" nil t)
504 (setq tabs (cons (current-column) tabs))))
505 (bury-buffer (prog1 (current-buffer)
506 (switch-to-buffer edit-tab-stops-buffer)))
507 (setq tab-stop-list tabs))
508 (message "Tab stops installed"))
510 (defun tab-to-tab-stop ()
511 "Insert spaces or tabs to next defined tab-stop column.
512 The variable `tab-stop-list' is a list of columns at which there are tab stops.
513 Use \\[edit-tab-stops] to edit them interactively."
514 (interactive)
515 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
516 (expand-abbrev))
517 (let ((tabs tab-stop-list))
518 (while (and tabs (>= (current-column) (car tabs)))
519 (setq tabs (cdr tabs)))
520 (if tabs
521 (let ((opoint (point)))
522 (delete-horizontal-space t)
523 (indent-to (car tabs)))
524 (insert ?\s))))
526 (defun move-to-tab-stop ()
527 "Move point to next defined tab-stop column.
528 The variable `tab-stop-list' is a list of columns at which there are tab stops.
529 Use \\[edit-tab-stops] to edit them interactively."
530 (interactive)
531 (let ((tabs tab-stop-list))
532 (while (and tabs (>= (current-column) (car tabs)))
533 (setq tabs (cdr tabs)))
534 (if tabs
535 (let ((before (point)))
536 (move-to-column (car tabs) t)
537 (save-excursion
538 (goto-char before)
539 ;; If we just added a tab, or moved over one,
540 ;; delete any superfluous spaces before the old point.
541 (if (and (eq (preceding-char) ?\s)
542 (eq (following-char) ?\t))
543 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
544 (while (and (> (current-column) tabend)
545 (eq (preceding-char) ?\s))
546 (forward-char -1))
547 (delete-region (point) before))))))))
549 (define-key global-map "\t" 'indent-for-tab-command)
550 (define-key esc-map "\C-\\" 'indent-region)
551 (define-key ctl-x-map "\t" 'indent-rigidly)
552 (define-key esc-map "i" 'tab-to-tab-stop)
554 ;;; arch-tag: f402b2a7-e44f-492f-b5b8-38996020b7c3
555 ;;; indent.el ends here