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