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