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