*** empty log message ***
[emacs.git] / lisp / indent.el
blob76f16ac4555dd16791b7e99fbb9ed0d60b464f19
1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
10 ;; any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ;;;###autoload
23 (defvar indent-line-function 'indent-to-left-margin "\
24 Function to indent current line.");Now in loaddefs.el
26 (defun indent-according-to-mode ()
27 "Indent line in proper way for current major mode."
28 (interactive)
29 (funcall indent-line-function))
31 (defun indent-for-tab-command ()
32 "Indent line in proper way for current major mode."
33 (interactive)
34 (if (eq indent-line-function 'indent-to-left-margin)
35 (insert-tab)
36 (funcall indent-line-function)))
38 (defun insert-tab ()
39 (if abbrev-mode
40 (expand-abbrev))
41 (if indent-tabs-mode
42 (insert ?\t)
43 (indent-to (* tab-width (1+ (/ (current-column) tab-width))))))
45 (defun indent-rigidly (start end arg)
46 "Indent all lines starting in the region sideways by ARG columns.
47 Called from a program, takes three arguments, START, END and ARG."
48 (interactive "r\np")
49 (save-excursion
50 (goto-char end)
51 (setq end (point-marker))
52 (goto-char start)
53 (or (bolp) (forward-line 1))
54 (while (< (point) end)
55 (let ((indent (current-indentation)))
56 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
57 (or (eolp)
58 (indent-to (max 0 (+ indent arg)) 0)))
59 (forward-line 1))
60 (move-marker end nil)))
62 ;; This is the default indent-line-function,
63 ;; used in Fundamental Mode, Text Mode, etc.
64 (defun indent-to-left-margin ()
65 (or (= (current-indentation) left-margin)
66 (let (epos)
67 (save-excursion
68 (beginning-of-line)
69 (delete-region (point)
70 (progn (skip-chars-forward " \t")
71 (point)))
72 (indent-to left-margin)
73 (setq epos (point)))
74 (if (< (point) epos)
75 (goto-char epos)))))
77 (defvar indent-region-function nil
78 "Function which is short cut to indent each line in region with TAB.
79 A value of nil means really perform TAB on each line.")
81 (defun indent-region (start end arg)
82 "Indent each nonblank line in the region.
83 With no argument, indent each line with TAB.
84 \(If there is a fill prefix, make each line start with the fill prefix.)
85 With argument COLUMN, indent each line to that column.
86 Called from a program, takes three args: START, END and COLUMN."
87 (interactive "r\nP")
88 (if (null arg)
89 (if fill-prefix
90 (save-excursion
91 (goto-char end)
92 (setq end (point-marker))
93 (goto-char start)
94 (let ((regexp (regexp-quote fill-prefix)))
95 (while (< (point) end)
96 (or (looking-at regexp)
97 (insert fill-prefix))
98 (forward-line 1))))
99 (if indent-region-function
100 (funcall indent-region-function start end)
101 (save-excursion
102 (goto-char end)
103 (setq end (point-marker))
104 (goto-char start)
105 (or (bolp) (forward-line 1))
106 (while (< (point) end)
107 (funcall indent-line-function)
108 (forward-line 1))
109 (move-marker end nil))))
110 (setq arg (prefix-numeric-value arg))
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 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
118 (or (eolp)
119 (indent-to arg 0))
120 (forward-line 1))
121 (move-marker end nil))))
123 (defun indent-relative-maybe ()
124 "Indent a new line like previous nonblank line."
125 (interactive)
126 (indent-relative t))
128 (defun indent-relative (&optional unindented-ok)
129 "Space out to under next indent point in previous nonblank line.
130 An indent point is a non-whitespace character following whitespace.
131 If the previous nonblank line has no indent points beyond the
132 column point starts at, `tab-to-tab-stop' is done instead."
133 (interactive "P")
134 (if abbrev-mode (expand-abbrev))
135 (let ((start-column (current-column))
136 indent)
137 (save-excursion
138 (beginning-of-line)
139 (if (re-search-backward "^[^\n]" nil t)
140 (let ((end (save-excursion (forward-line 1) (point))))
141 (move-to-column start-column)
142 ;; Is start-column inside a tab on this line?
143 (if (> (current-column) start-column)
144 (backward-char 1))
145 (or (looking-at "[ \t]")
146 unindented-ok
147 (skip-chars-forward "^ \t" end))
148 (skip-chars-forward " \t" end)
149 (or (= (point) end) (setq indent (current-column))))))
150 (if indent
151 (let ((opoint (point-marker)))
152 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
153 (indent-to indent 0)
154 (if (> opoint (point))
155 (goto-char opoint))
156 (move-marker opoint nil))
157 (tab-to-tab-stop))))
159 (defvar tab-stop-list
160 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
161 "*List of tab stop positions used by `tab-to-tab-stops'.")
163 (defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.")
164 (if edit-tab-stops-map
166 (setq edit-tab-stops-map (make-sparse-keymap))
167 (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
168 (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
170 (defvar edit-tab-stops-buffer nil
171 "Buffer whose tab stops are being edited--in case
172 the variable `tab-stop-list' is local in that buffer.")
174 (defun edit-tab-stops ()
175 "Edit the tab stops used by `tab-to-tab-stop'.
176 Creates a buffer *Tab Stops* containing text describing the tab stops.
177 A colon indicates a column where there is a tab stop.
178 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
179 (interactive)
180 (setq edit-tab-stops-buffer (current-buffer))
181 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
182 (use-local-map edit-tab-stops-map)
183 (make-local-variable 'indent-tabs-mode)
184 (setq indent-tabs-mode nil)
185 (overwrite-mode 1)
186 (setq truncate-lines t)
187 (erase-buffer)
188 (let ((tabs tab-stop-list))
189 (while tabs
190 (indent-to (car tabs) 0)
191 (insert ?:)
192 (setq tabs (cdr tabs))))
193 (let ((count 0))
194 (insert ?\n)
195 (while (< count 8)
196 (insert (+ count ?0))
197 (insert " ")
198 (setq count (1+ count)))
199 (insert ?\n)
200 (while (> count 0)
201 (insert "0123456789")
202 (setq count (1- count))))
203 (insert "\nTo install changes, type C-c C-c")
204 (goto-char (point-min)))
206 (defun edit-tab-stops-note-changes ()
207 "Put edited tab stops into effect."
208 (interactive)
209 (let (tabs)
210 (save-excursion
211 (goto-char 1)
212 (end-of-line)
213 (while (search-backward ":" nil t)
214 (setq tabs (cons (current-column) tabs))))
215 (bury-buffer (prog1 (current-buffer)
216 (switch-to-buffer edit-tab-stops-buffer)))
217 (setq tab-stop-list tabs))
218 (message "Tab stops installed"))
220 (defun tab-to-tab-stop ()
221 "Insert spaces or tabs to next defined tab-stop column.
222 The variable `tab-stop-list' is a list of columns at which there are tab stops.
223 Use \\[edit-tab-stops] to edit them interactively."
224 (interactive)
225 (if abbrev-mode (expand-abbrev))
226 (let ((tabs tab-stop-list))
227 (while (and tabs (>= (current-column) (car tabs)))
228 (setq tabs (cdr tabs)))
229 (if tabs
230 (indent-to (car tabs))
231 (insert ?\ ))))
233 (defun move-to-tab-stop ()
234 "Move point to next defined tab-stop column.
235 The variable `tab-stop-list' is a list of columns at which there are tab stops.
236 Use \\[edit-tab-stops] to edit them interactively."
237 (interactive)
238 (let ((tabs tab-stop-list))
239 (while (and tabs (>= (current-column) (car tabs)))
240 (setq tabs (cdr tabs)))
241 (if tabs
242 (move-to-column (car tabs) t))))
244 (define-key global-map "\t" 'indent-for-tab-command)
245 (define-key esc-map "\034" 'indent-region)
246 (define-key ctl-x-map "\t" 'indent-rigidly)
247 (define-key esc-map "i" 'tab-to-tab-stop)
249 ;;; indent.el ends here