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