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