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