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