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