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