Mercurial > emacs
annotate lisp/indent.el @ 10087:20769d80bc88
(get_time): Fix up whitespace.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 28 Nov 1994 21:03:41 +0000 |
parents | ae3b75bae5d7 |
children | b8053ce05ab5 |
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) | |
8642
ae3b75bae5d7
(indent-rigidly): Insert new indent, then delete old.
Richard M. Stallman <rms@gnu.org>
parents:
8208
diff
changeset
|
62 (let ((indent (current-indentation)) |
ae3b75bae5d7
(indent-rigidly): Insert new indent, then delete old.
Richard M. Stallman <rms@gnu.org>
parents:
8208
diff
changeset
|
63 eol-flag) |
ae3b75bae5d7
(indent-rigidly): Insert new indent, then delete old.
Richard M. Stallman <rms@gnu.org>
parents:
8208
diff
changeset
|
64 (save-excursion |
ae3b75bae5d7
(indent-rigidly): Insert new indent, then delete old.
Richard M. Stallman <rms@gnu.org>
parents:
8208
diff
changeset
|
65 (skip-chars-forward " \t") |
ae3b75bae5d7
(indent-rigidly): Insert new indent, then delete old.
Richard M. Stallman <rms@gnu.org>
parents:
8208
diff
changeset
|
66 (setq eol-flag (eolp))) |
ae3b75bae5d7
(indent-rigidly): Insert new indent, then delete old.
Richard M. Stallman <rms@gnu.org>
parents:
8208
diff
changeset
|
67 (or eol-flag |
ae3b75bae5d7
(indent-rigidly): Insert new indent, then delete old.
Richard M. Stallman <rms@gnu.org>
parents:
8208
diff
changeset
|
68 (indent-to (max 0 (+ indent arg)) 0)) |
ae3b75bae5d7
(indent-rigidly): Insert new indent, then delete old.
Richard M. Stallman <rms@gnu.org>
parents:
8208
diff
changeset
|
69 (delete-region (point) (progn (skip-chars-forward " \t") (point)))) |
263 | 70 (forward-line 1)) |
71 (move-marker end nil))) | |
72 | |
73 ;; This is the default indent-line-function, | |
74 ;; used in Fundamental Mode, Text Mode, etc. | |
75 (defun indent-to-left-margin () | |
76 (or (= (current-indentation) left-margin) | |
77 (let (epos) | |
78 (save-excursion | |
79 (beginning-of-line) | |
80 (delete-region (point) | |
81 (progn (skip-chars-forward " \t") | |
82 (point))) | |
83 (indent-to left-margin) | |
84 (setq epos (point))) | |
85 (if (< (point) epos) | |
86 (goto-char epos))))) | |
87 | |
88 (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
|
89 "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
|
90 A value of nil means really run indent-according-to-mode on each line.") |
263 | 91 |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
92 (defun indent-region (start end column) |
263 | 93 "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
|
94 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
|
95 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
|
96 If there is a fill prefix, make each line start with the fill prefix. |
263 | 97 With argument COLUMN, indent each line to that column. |
98 Called from a program, takes three args: START, END and COLUMN." | |
99 (interactive "r\nP") | |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
100 (if (null column) |
263 | 101 (if fill-prefix |
102 (save-excursion | |
103 (goto-char end) | |
104 (setq end (point-marker)) | |
105 (goto-char start) | |
106 (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
|
107 (while (< (point) end) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
108 (or (looking-at regexp) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
109 (and (bolp) (eolp)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
110 (insert fill-prefix)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
111 (forward-line 1)))) |
263 | 112 (if indent-region-function |
113 (funcall indent-region-function start end) | |
114 (save-excursion | |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
115 (goto-char end) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
116 (setq end (point-marker)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
117 (goto-char start) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
118 (or (bolp) (forward-line 1)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
119 (while (< (point) end) |
4473
600d0d832d99
(indent-region): Fix paren error in last change.
Richard M. Stallman <rms@gnu.org>
parents:
4465
diff
changeset
|
120 (or (and (bolp) (eolp)) |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
121 (funcall indent-line-function)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
122 (forward-line 1)) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
123 (move-marker end nil)))) |
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
124 (setq column (prefix-numeric-value column)) |
263 | 125 (save-excursion |
126 (goto-char end) | |
127 (setq end (point-marker)) | |
128 (goto-char start) | |
129 (or (bolp) (forward-line 1)) | |
130 (while (< (point) end) | |
131 (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
132 (or (eolp) | |
4465
794498c64928
(indent-region): Rename arg ARG to COLUMN.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
133 (indent-to column 0)) |
263 | 134 (forward-line 1)) |
135 (move-marker end nil)))) | |
136 | |
137 (defun indent-relative-maybe () | |
138 "Indent a new line like previous nonblank line." | |
139 (interactive) | |
140 (indent-relative t)) | |
141 | |
142 (defun indent-relative (&optional unindented-ok) | |
143 "Space out to under next indent point in previous nonblank line. | |
144 An indent point is a non-whitespace character following whitespace. | |
145 If the previous nonblank line has no indent points beyond the | |
146 column point starts at, `tab-to-tab-stop' is done instead." | |
147 (interactive "P") | |
148 (if abbrev-mode (expand-abbrev)) | |
149 (let ((start-column (current-column)) | |
150 indent) | |
151 (save-excursion | |
152 (beginning-of-line) | |
153 (if (re-search-backward "^[^\n]" nil t) | |
154 (let ((end (save-excursion (forward-line 1) (point)))) | |
155 (move-to-column start-column) | |
156 ;; Is start-column inside a tab on this line? | |
157 (if (> (current-column) start-column) | |
158 (backward-char 1)) | |
159 (or (looking-at "[ \t]") | |
160 unindented-ok | |
161 (skip-chars-forward "^ \t" end)) | |
162 (skip-chars-forward " \t" end) | |
163 (or (= (point) end) (setq indent (current-column)))))) | |
164 (if indent | |
165 (let ((opoint (point-marker))) | |
166 (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
167 (indent-to indent 0) | |
168 (if (> opoint (point)) | |
169 (goto-char opoint)) | |
170 (move-marker opoint nil)) | |
171 (tab-to-tab-stop)))) | |
172 | |
173 (defvar tab-stop-list | |
174 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120) | |
7813
1fa576a274b5
(tab-stop-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
4473
diff
changeset
|
175 "*List of tab stop positions used by `tab-to-tab-stops'. |
1fa576a274b5
(tab-stop-list): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
4473
diff
changeset
|
176 This should be a list of integers, ordered from smallest to largest.") |
263 | 177 |
178 (defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.") | |
179 (if edit-tab-stops-map | |
180 nil | |
181 (setq edit-tab-stops-map (make-sparse-keymap)) | |
182 (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes) | |
183 (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes)) | |
184 | |
185 (defvar edit-tab-stops-buffer nil | |
186 "Buffer whose tab stops are being edited--in case | |
187 the variable `tab-stop-list' is local in that buffer.") | |
188 | |
189 (defun edit-tab-stops () | |
190 "Edit the tab stops used by `tab-to-tab-stop'. | |
191 Creates a buffer *Tab Stops* containing text describing the tab stops. | |
192 A colon indicates a column where there is a tab stop. | |
193 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect." | |
194 (interactive) | |
195 (setq edit-tab-stops-buffer (current-buffer)) | |
196 (switch-to-buffer (get-buffer-create "*Tab Stops*")) | |
197 (use-local-map edit-tab-stops-map) | |
198 (make-local-variable 'indent-tabs-mode) | |
199 (setq indent-tabs-mode nil) | |
200 (overwrite-mode 1) | |
201 (setq truncate-lines t) | |
202 (erase-buffer) | |
203 (let ((tabs tab-stop-list)) | |
204 (while tabs | |
205 (indent-to (car tabs) 0) | |
206 (insert ?:) | |
207 (setq tabs (cdr tabs)))) | |
208 (let ((count 0)) | |
209 (insert ?\n) | |
210 (while (< count 8) | |
211 (insert (+ count ?0)) | |
212 (insert " ") | |
213 (setq count (1+ count))) | |
214 (insert ?\n) | |
215 (while (> count 0) | |
216 (insert "0123456789") | |
217 (setq count (1- count)))) | |
218 (insert "\nTo install changes, type C-c C-c") | |
219 (goto-char (point-min))) | |
220 | |
221 (defun edit-tab-stops-note-changes () | |
222 "Put edited tab stops into effect." | |
223 (interactive) | |
224 (let (tabs) | |
225 (save-excursion | |
226 (goto-char 1) | |
227 (end-of-line) | |
228 (while (search-backward ":" nil t) | |
229 (setq tabs (cons (current-column) tabs)))) | |
230 (bury-buffer (prog1 (current-buffer) | |
231 (switch-to-buffer edit-tab-stops-buffer))) | |
232 (setq tab-stop-list tabs)) | |
233 (message "Tab stops installed")) | |
234 | |
235 (defun tab-to-tab-stop () | |
236 "Insert spaces or tabs to next defined tab-stop column. | |
237 The variable `tab-stop-list' is a list of columns at which there are tab stops. | |
238 Use \\[edit-tab-stops] to edit them interactively." | |
239 (interactive) | |
240 (if abbrev-mode (expand-abbrev)) | |
241 (let ((tabs tab-stop-list)) | |
242 (while (and tabs (>= (current-column) (car tabs))) | |
243 (setq tabs (cdr tabs))) | |
244 (if tabs | |
7973
b7ea350590a7
(tab-to-tab-stop): Combine new indentation with previous.
Richard M. Stallman <rms@gnu.org>
parents:
7813
diff
changeset
|
245 (let ((opoint (point))) |
b7ea350590a7
(tab-to-tab-stop): Combine new indentation with previous.
Richard M. Stallman <rms@gnu.org>
parents:
7813
diff
changeset
|
246 (skip-chars-backward " \t") |
b7ea350590a7
(tab-to-tab-stop): Combine new indentation with previous.
Richard M. Stallman <rms@gnu.org>
parents:
7813
diff
changeset
|
247 (delete-region (point) opoint) |
b7ea350590a7
(tab-to-tab-stop): Combine new indentation with previous.
Richard M. Stallman <rms@gnu.org>
parents:
7813
diff
changeset
|
248 (indent-to (car tabs))) |
263 | 249 (insert ?\ )))) |
250 | |
251 (defun move-to-tab-stop () | |
252 "Move point to next defined tab-stop column. | |
253 The variable `tab-stop-list' is a list of columns at which there are tab stops. | |
254 Use \\[edit-tab-stops] to edit them interactively." | |
255 (interactive) | |
256 (let ((tabs tab-stop-list)) | |
257 (while (and tabs (>= (current-column) (car tabs))) | |
258 (setq tabs (cdr tabs))) | |
259 (if tabs | |
8037
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
260 (let ((before (point))) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
261 (move-to-column (car tabs) t) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
262 (save-excursion |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
263 (goto-char before) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
264 ;; If we just added a tab, or moved over one, |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
265 ;; delete any superfluous spaces before the old point. |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
266 (if (and (eq (preceding-char) ?\ ) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
267 (eq (following-char) ?\t)) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
268 (let ((tabend (* (/ (current-column) tab-width) tab-width))) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
269 (while (and (> (current-column) tabend) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
270 (eq (preceding-char) ?\ )) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
271 (forward-char -1)) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
272 (delete-region (point) before)))))))) |
fd5fb80a940d
(move-to-tab-stop): Delete unnecessary spaces
Richard M. Stallman <rms@gnu.org>
parents:
7984
diff
changeset
|
273 |
263 | 274 (define-key global-map "\t" 'indent-for-tab-command) |
275 (define-key esc-map "\034" 'indent-region) | |
276 (define-key ctl-x-map "\t" 'indent-rigidly) | |
277 (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
|
278 |
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
269
diff
changeset
|
279 ;;; indent.el ends here |