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