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