]> code.delx.au - gnu-emacs/blob - lisp/indent.el
Update FSF's address.
[gnu-emacs] / lisp / indent.el
1 ;;; indent.el --- indentation commands for Emacs
2
3 ;; Copyright (C) 1985, 1995 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 (defvar standard-indent 4 "\
32 Default number of columns for margin-changing functions to indent.")
33
34 (defvar indent-line-function 'indent-to-left-margin "\
35 Function to indent current line.")
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
42 (defun indent-for-tab-command (&optional prefix-arg)
43 "Indent line in proper way for current major mode."
44 (interactive "P")
45 (if (eq indent-line-function 'indent-to-left-margin)
46 (insert-tab)
47 (if prefix-arg
48 (funcall indent-line-function prefix-arg)
49 (funcall indent-line-function))))
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.
60 Called 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)
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))))
76 (forward-line 1))
77 (move-marker end nil)))
78
79 (defun indent-line-to (column)
80 "Indent current line to COLUMN.
81 This function removes or adds spaces and tabs at beginning of line
82 only if necessary. It leaves point at end of indentation."
83 (back-to-indentation)
84 (let ((cur-col (current-column)))
85 (cond ((< cur-col column)
86 (if (> (- column (* (/ cur-col tab-width) tab-width)) tab-width)
87 (delete-region (point)
88 (progn (skip-chars-backward " ") (point))))
89 (indent-to column))
90 ((> cur-col column) ; too far right (after tab?)
91 (delete-region (progn (move-to-column column t) (point))
92 (progn (back-to-indentation) (point)))))))
93
94 (defun current-left-margin ()
95 "Return the left margin to use for this line.
96 This is the value of the buffer-local variable `left-margin' plus the value
97 of the `left-margin' text-property at the start of the line."
98 (save-excursion
99 (back-to-indentation)
100 (max 0
101 (+ left-margin (or (get-text-property
102 (if (and (eobp) (not (bobp)))
103 (1- (point)) (point))
104 'left-margin) 0)))))
105
106 (defun move-to-left-margin (&optional n force)
107 "Move to the left margin of the current line.
108 With optional argument, move forward N-1 lines first.
109 The column moved to is the one given by the `current-left-margin' function.
110 If the line's indentation appears to be wrong, and this command is called
111 interactively or with optional argument FORCE, it will be fixed."
112 (interactive (list (prefix-numeric-value current-prefix-arg) t))
113 (beginning-of-line n)
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)))))
123
124 ;; This is the default indent-line-function,
125 ;; used in Fundamental Mode, Text Mode, etc.
126 (defun indent-to-left-margin ()
127 "Indent current line to the column given by `current-left-margin'."
128 (indent-line-to (current-left-margin)))
129
130 (defun delete-to-left-margin (&optional from to)
131 "Remove left margin indentation from a region.
132 This deletes to the column given by `current-left-margin'.
133 In no case will it delete non-whitespace.
134 Args FROM and TO are optional; default is the whole buffer."
135 (save-excursion
136 (goto-char (or to (point-max)))
137 (setq to (point-marker))
138 (goto-char (or from (point-min)))
139 (or (bolp) (forward-line 1))
140 (while (< (point) to)
141 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
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.
147 If `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)))
155 ;; Place end after whitespace
156 (goto-char to)
157 (skip-chars-forward " \t")
158 (setq to (point-marker)))
159 ;; Delete margin indentation first, but keep paragraph indentation.
160 (delete-to-left-margin from to)
161 (put-text-property from to 'left-margin lm)
162 (indent-rigidly from to lm)
163 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
164 (move-marker to nil))
165
166 (defun set-right-margin (from to lm)
167 "Set the right 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 right margin to width: ")
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)
176 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
177
178 (defun alter-text-property (from to prop func &optional object)
179 "Programmatically change value of a text-property.
180 For each region between FROM and TO that has a single value for PROPERTY,
181 apply FUNCTION to that value and sets the property to the function's result.
182 Optional 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.
194 With no prefix argument, this adds `standard-indent' of indentation.
195 A prefix arg (optional third arg INC noninteractively) specifies the amount
196 to change the margin by, in characters.
197 If `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)))
206 (alter-text-property from to 'left-margin
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)))
210 (move-marker to nil))
211
212 (defun decrease-left-margin (from to inc)
213 "Make the left margin of the region smaller.
214 With no prefix argument, decrease the indentation by `standard-indent'.
215 A prefix arg (optional third arg INC noninteractively) specifies the amount
216 to change the margin by, in characters.
217 If `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.
224 With no prefix argument, increase the right margin by `standard-indent'.
225 A prefix arg (optional third arg INC noninteractively) specifies the amount
226 to change the margin by, in characters. A negative argument decreases
227 the right margin width.
228 If `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
235 (lambda (v) (+ inc (or v 0))))
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.
241 With no prefix argument, decrease the right margin by `standard-indent'.
242 A prefix arg (optional third arg INC noninteractively) specifies the amount
243 of width to remove, in characters. A negative argument increases
244 the right margin width.
245 If `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)))
249
250 (defun beginning-of-line-text (&optional n)
251 "Move to the beginning of the text on this line.
252 With optional argument, move forward N-1 lines first.
253 From the beginning of the line, moves past the left-margin indentation, the
254 fill-prefix, and any indentation used for centering or right-justifying the
255 line, 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
274 (defvar indent-region-function nil
275 "Short cut function to indent region using `indent-according-to-mode'.
276 A value of nil means really run `indent-according-to-mode' on each line.")
277
278 (defun indent-region (start end column)
279 "Indent each nonblank line in the region.
280 With no argument, indent each line using `indent-according-to-mode',
281 or use `indent-region-function' to do the whole region if that's non-nil.
282 If there is a fill prefix, make each line start with the fill prefix.
283 With argument COLUMN, indent each line to that column.
284 Called from a program, takes three args: START, END and COLUMN."
285 (interactive "r\nP")
286 (if (null column)
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)))
293 (while (< (point) end)
294 (or (looking-at regexp)
295 (and (bolp) (eolp))
296 (insert fill-prefix))
297 (forward-line 1))))
298 (if indent-region-function
299 (funcall indent-region-function start end)
300 (save-excursion
301 (goto-char end)
302 (setq end (point-marker))
303 (goto-char start)
304 (or (bolp) (forward-line 1))
305 (while (< (point) end)
306 (or (and (bolp) (eolp))
307 (funcall indent-line-function))
308 (forward-line 1))
309 (move-marker end nil))))
310 (setq column (prefix-numeric-value column))
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)
319 (indent-to column 0))
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.
330 An indent point is a non-whitespace character following whitespace.
331 If the previous nonblank line has no indent points beyond the
332 column 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)
361 "*List of tab stop positions used by `tab-to-tab-stops'.
362 This should be a list of integers, ordered from smallest to largest.")
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
373 the 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'.
377 Creates a buffer *Tab Stops* containing text describing the tab stops.
378 A colon indicates a column where there is a tab stop.
379 You 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.
423 The variable `tab-stop-list' is a list of columns at which there are tab stops.
424 Use \\[edit-tab-stops] to edit them interactively."
425 (interactive)
426 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
427 (expand-abbrev))
428 (let ((tabs tab-stop-list))
429 (while (and tabs (>= (current-column) (car tabs)))
430 (setq tabs (cdr tabs)))
431 (if tabs
432 (let ((opoint (point)))
433 (skip-chars-backward " \t")
434 (delete-region (point) opoint)
435 (indent-to (car tabs)))
436 (insert ?\ ))))
437
438 (defun move-to-tab-stop ()
439 "Move point to next defined tab-stop column.
440 The variable `tab-stop-list' is a list of columns at which there are tab stops.
441 Use \\[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
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)
465
466 ;;; indent.el ends here