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