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