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