Mercurial > emacs
annotate lisp/indent.el @ 4865:4baa7107909b
Initial revision
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Wed, 20 Oct 1993 18:02:46 +0000 |
parents | 600d0d832d99 |
children | 1fa576a274b5 |
rev | line source |
---|---|
660
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
269
diff
changeset
|
1 ;;; indent.el --- indentation commands for Emacs |
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
269
diff
changeset
|
2 |
845 | 3 ;; Copyright (C) 1985 Free Software Foundation, Inc. |
4 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
5 ;; Maintainer: FSF |
263 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
11 ;; the Free Software Foundation; either version 2, or (at your option) |
263 | 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 | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2201
diff
changeset
|
23 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2201
diff
changeset
|
24 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2201
diff
changeset
|
25 ;; Commands for making and changing indentation in text. These are |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2201
diff
changeset
|
26 ;; described in the Emacs manual. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2201
diff
changeset
|
27 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
28 ;;; Code: |
263 | 29 |
269 | 30 (defvar indent-line-function 'indent-to-left-margin "\ |
2192
96adb71c61ec
* indent.el (indent-line-function): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
845
diff
changeset
|
31 Function to indent current line.") |
263 | 32 |
33 (defun indent-according-to-mode () | |
34 "Indent line in proper way for current major mode." | |
35 (interactive) | |
36 (funcall indent-line-function)) | |
37 | |
38 (defun indent-for-tab-command () | |
39 "Indent line in proper way for current major mode." | |
40 (interactive) | |
41 (if (eq indent-line-function 'indent-to-left-margin) | |
42 (insert-tab) | |
43 (funcall indent-line-function))) | |
44 | |
45 (defun insert-tab () | |
46 (if abbrev-mode | |
47 (expand-abbrev)) | |
48 (if indent-tabs-mode | |
49 (insert ?\t) | |
50 (indent-to (* tab-width (1+ (/ (current-column) tab-width)))))) | |
51 | |
52 (defun indent-rigidly (start end arg) | |
53 "Indent all lines starting in the region sideways by ARG columns. | |
54 Called from a program, takes three arguments, START, END and ARG." | |
55 (interactive "r\np") | |
56 (save-excursion | |
57 (goto-char end) | |
58 (setq end (point-marker)) | |
59 (goto-char start) | |
60 (or (bolp) (forward-line 1)) | |
61 (while (< (point) end) | |
62 (let ((indent (current-indentation))) | |
63 (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
64 (or (eolp) | |
65 (indent-to (max 0 (+ indent arg)) 0))) | |
66 (forward-line 1)) | |
67 (move-marker end nil))) | |
68 | |
69 ;; This is the default indent-line-function, | |
70 ;; used in Fundamental Mode, Text Mode, etc. | |
71 (defun indent-to-left-margin () | |
72 (or (= (current-indentation) left-margin) | |
73 (let (epos) | |
74 (save-excursion | |
75 (beginning-of-line) | |
76 (delete-region (point) | |
77 (progn (skip-chars-forward " \t") | |
78 (point))) | |
79 (indent-to left-margin) | |
80 (setq epos (point))) | |
81 (if (< (point) epos) | |
82 (goto-char epos))))) | |
83 | |
84 (defvar indent-region-function nil | |
2201
bfe994df6c27
* indent.el (indent-region, indent-region-function): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
2192
diff
changeset
|
85 "Function which is short cut to indent region using indent-according-to-mode. |
bfe994df6c27
* indent.el (indent-region, indent-region-function): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
2192
diff
changeset
|
86 A value of nil means really run indent-according-to-mode on each line.") |
263 | 87 |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
88 (defun indent-region (start end column) |
263 | 89 "Indent each nonblank line in the region. |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
90 With no argument, indent each line using `indent-according-to-mode', |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
91 or use `indent-region-function' to do the whole region if that's non-nil. |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
92 If there is a fill prefix, make each line start with the fill prefix. |
263 | 93 With argument COLUMN, indent each line to that column. |
94 Called from a program, takes three args: START, END and COLUMN." | |
95 (interactive "r\nP") | |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
96 (if (null column) |
263 | 97 (if fill-prefix |
98 (save-excursion | |
99 (goto-char end) | |
100 (setq end (point-marker)) | |
101 (goto-char start) | |
102 (let ((regexp (regexp-quote fill-prefix))) | |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
103 (while (< (point) end) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
104 (or (looking-at regexp) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
105 (and (bolp) (eolp)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
106 (insert fill-prefix)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
107 (forward-line 1)))) |
263 | 108 (if indent-region-function |
109 (funcall indent-region-function start end) | |
110 (save-excursion | |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
111 (goto-char end) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
112 (setq end (point-marker)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
113 (goto-char start) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
114 (or (bolp) (forward-line 1)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
115 (while (< (point) end) |
4473
600d0d832d99
(indent-region): Fix paren error in last change.
Richard M. Stallman <rms@gnu.org>
parents:
4465
diff
changeset
|
116 (or (and (bolp) (eolp)) |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
117 (funcall indent-line-function)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
118 (forward-line 1)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
119 (move-marker end nil)))) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
120 (setq column (prefix-numeric-value column)) |
263 | 121 (save-excursion |
122 (goto-char end) | |
123 (setq end (point-marker)) | |
124 (goto-char start) | |
125 (or (bolp) (forward-line 1)) | |
126 (while (< (point) end) | |
127 (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
128 (or (eolp) | |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
129 (indent-to column 0)) |
263 | 130 (forward-line 1)) |
131 (move-marker end nil)))) | |
132 | |
133 (defun indent-relative-maybe () | |
134 "Indent a new line like previous nonblank line." | |
135 (interactive) | |
136 (indent-relative t)) | |
137 | |
138 (defun indent-relative (&optional unindented-ok) | |
139 "Space out to under next indent point in previous nonblank line. | |
140 An indent point is a non-whitespace character following whitespace. | |
141 If the previous nonblank line has no indent points beyond the | |
142 column point starts at, `tab-to-tab-stop' is done instead." | |
143 (interactive "P") | |
144 (if abbrev-mode (expand-abbrev)) | |
145 (let ((start-column (current-column)) | |
146 indent) | |
147 (save-excursion | |
148 (beginning-of-line) | |
149 (if (re-search-backward "^[^\n]" nil t) | |
150 (let ((end (save-excursion (forward-line 1) (point)))) | |
151 (move-to-column start-column) | |
152 ;; Is start-column inside a tab on this line? | |
153 (if (> (current-column) start-column) | |
154 (backward-char 1)) | |
155 (or (looking-at "[ \t]") | |
156 unindented-ok | |
157 (skip-chars-forward "^ \t" end)) | |
158 (skip-chars-forward " \t" end) | |
159 (or (= (point) end) (setq indent (current-column)))))) | |
160 (if indent | |
161 (let ((opoint (point-marker))) | |
162 (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
163 (indent-to indent 0) | |
164 (if (> opoint (point)) | |
165 (goto-char opoint)) | |
166 (move-marker opoint nil)) | |
167 (tab-to-tab-stop)))) | |
168 | |
169 (defvar tab-stop-list | |
170 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120) | |
171 "*List of tab stop positions used by `tab-to-tab-stops'.") | |
172 | |
173 (defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.") | |
174 (if edit-tab-stops-map | |
175 nil | |
176 (setq edit-tab-stops-map (make-sparse-keymap)) | |
177 (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes) | |
178 (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes)) | |
179 | |
180 (defvar edit-tab-stops-buffer nil | |
181 "Buffer whose tab stops are being edited--in case | |
182 the variable `tab-stop-list' is local in that buffer.") | |
183 | |
184 (defun edit-tab-stops () | |
185 "Edit the tab stops used by `tab-to-tab-stop'. | |
186 Creates a buffer *Tab Stops* containing text describing the tab stops. | |
187 A colon indicates a column where there is a tab stop. | |
188 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect." | |
189 (interactive) | |
190 (setq edit-tab-stops-buffer (current-buffer)) | |
191 (switch-to-buffer (get-buffer-create "*Tab Stops*")) | |
192 (use-local-map edit-tab-stops-map) | |
193 (make-local-variable 'indent-tabs-mode) | |
194 (setq indent-tabs-mode nil) | |
195 (overwrite-mode 1) | |
196 (setq truncate-lines t) | |
197 (erase-buffer) | |
198 (let ((tabs tab-stop-list)) | |
199 (while tabs | |
200 (indent-to (car tabs) 0) | |
201 (insert ?:) | |
202 (setq tabs (cdr tabs)))) | |
203 (let ((count 0)) | |
204 (insert ?\n) | |
205 (while (< count 8) | |
206 (insert (+ count ?0)) | |
207 (insert " ") | |
208 (setq count (1+ count))) | |
209 (insert ?\n) | |
210 (while (> count 0) | |
211 (insert "0123456789") | |
212 (setq count (1- count)))) | |
213 (insert "\nTo install changes, type C-c C-c") | |
214 (goto-char (point-min))) | |
215 | |
216 (defun edit-tab-stops-note-changes () | |
217 "Put edited tab stops into effect." | |
218 (interactive) | |
219 (let (tabs) | |
220 (save-excursion | |
221 (goto-char 1) | |
222 (end-of-line) | |
223 (while (search-backward ":" nil t) | |
224 (setq tabs (cons (current-column) tabs)))) | |
225 (bury-buffer (prog1 (current-buffer) | |
226 (switch-to-buffer edit-tab-stops-buffer))) | |
227 (setq tab-stop-list tabs)) | |
228 (message "Tab stops installed")) | |
229 | |
230 (defun tab-to-tab-stop () | |
231 "Insert spaces or tabs to next defined tab-stop column. | |
232 The variable `tab-stop-list' is a list of columns at which there are tab stops. | |
233 Use \\[edit-tab-stops] to edit them interactively." | |
234 (interactive) | |
235 (if abbrev-mode (expand-abbrev)) | |
236 (let ((tabs tab-stop-list)) | |
237 (while (and tabs (>= (current-column) (car tabs))) | |
238 (setq tabs (cdr tabs))) | |
239 (if tabs | |
240 (indent-to (car tabs)) | |
241 (insert ?\ )))) | |
242 | |
243 (defun move-to-tab-stop () | |
244 "Move point to next defined tab-stop column. | |
245 The variable `tab-stop-list' is a list of columns at which there are tab stops. | |
246 Use \\[edit-tab-stops] to edit them interactively." | |
247 (interactive) | |
248 (let ((tabs tab-stop-list)) | |
249 (while (and tabs (>= (current-column) (car tabs))) | |
250 (setq tabs (cdr tabs))) | |
251 (if tabs | |
252 (move-to-column (car tabs) t)))) | |
253 | |
254 (define-key global-map "\t" 'indent-for-tab-command) | |
255 (define-key esc-map "\034" 'indent-region) | |
256 (define-key ctl-x-map "\t" 'indent-rigidly) | |
257 (define-key esc-map "i" 'tab-to-tab-stop) | |
660
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
269
diff
changeset
|
258 |
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
269
diff
changeset
|
259 ;;; indent.el ends here |