Commit | Line | Data |
---|---|---|
1a06eabd ER |
1 | ;;; indent.el --- indentation commands for Emacs |
2 | ||
0d30b337 | 3 | ;; Copyright (C) 1985, 1995, 2001, 2002, 2003, 2004, |
409cc4a3 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
483e630e | 5 | |
9750e079 ER |
6 | ;; Maintainer: FSF |
7 | ||
483e630e RM |
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 | |
b4aa6026 | 12 | ;; the Free Software Foundation; either version 3, 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 | |
b578f267 | 21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
22 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 | ;; Boston, MA 02110-1301, USA. | |
483e630e | 24 | |
e41b2db1 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;; Commands for making and changing indentation in text. These are | |
28 | ;; described in the Emacs manual. | |
29 | ||
e5167999 | 30 | ;;; Code: |
483e630e | 31 | |
ddb3136e | 32 | (defgroup indent nil |
21751995 | 33 | "Indentation commands." |
ddb3136e | 34 | :group 'editing) |
106b6d0e | 35 | |
ddb3136e RS |
36 | (defcustom standard-indent 4 |
37 | "*Default number of columns for margin-changing functions to indent." | |
38 | :group 'indent | |
39 | :type 'integer) | |
40 | ||
f059f703 | 41 | (defvar indent-line-function 'indent-relative |
a17b712b SM |
42 | "Function to indent the current line. |
43 | This function will be called with no arguments. | |
44 | If it is called somewhere where auto-indentation cannot be done | |
45 | \(f.ex. inside a string), the function should simply return `noindent'. | |
46 | Setting this function is all you need to make TAB indent appropriately. | |
47 | Don't rebind TAB unless you really need to.") | |
483e630e | 48 | |
20b1d079 SM |
49 | (defcustom tab-always-indent t |
50 | "*Controls the operation of the TAB key. | |
51 | If t, hitting TAB always just indents the current line. | |
52 | If nil, hitting TAB indents the current line if point is at the left margin | |
f321348b EZ |
53 | or in the line's indentation, otherwise it insert a \"real\" TAB character. |
54 | Most programming language modes have their own variable to control this, | |
55 | e.g., `c-tab-always-indent', and do not respect this variable." | |
20b1d079 | 56 | :group 'indent |
fe552b4c | 57 | :type '(choice (const nil) (const t) (const always))) |
20b1d079 | 58 | |
483e630e | 59 | (defun indent-according-to-mode () |
1a2f8b73 RS |
60 | "Indent line in proper way for current major mode. |
61 | The buffer-local variable `indent-line-function' determines how to do this, | |
62 | but the functions `indent-relative' and `indent-relative-maybe' are | |
63 | special; we don't actually use them here." | |
483e630e | 64 | (interactive) |
a17b712b SM |
65 | (if (memq indent-line-function |
66 | '(indent-relative indent-relative-maybe)) | |
67 | ;; These functions are used for tabbing, but can't be used for | |
68 | ;; indenting. Replace with something ad-hoc. | |
69 | (let ((column (save-excursion | |
70 | (beginning-of-line) | |
71 | (skip-chars-backward "\n \t") | |
72 | (beginning-of-line) | |
73 | (current-indentation)))) | |
74 | (if (<= (current-column) (current-indentation)) | |
75 | (indent-line-to column) | |
76 | (save-excursion (indent-line-to column)))) | |
77 | ;; The normal case. | |
78 | (funcall indent-line-function))) | |
483e630e | 79 | |
a17b712b | 80 | (defun indent-for-tab-command (&optional arg) |
20b1d079 SM |
81 | "Indent line in proper way for current major mode or insert a tab. |
82 | Depending on `tab-always-indent', either insert a tab or indent. | |
b9ce5694 GM |
83 | If initial point was within line's indentation, position after |
84 | the indentation. Else stay at same point in text. | |
20b1d079 | 85 | The function actually called to indent is determined by the value of |
eca1e29e | 86 | `indent-line-function'." |
34a71c1e | 87 | (interactive "P") |
a17b712b | 88 | (cond |
fe552b4c | 89 | ((or ;; indent-to-left-margin is only meant for indenting, |
a17b712b SM |
90 | ;; so we force it to always insert a tab here. |
91 | (eq indent-line-function 'indent-to-left-margin) | |
92 | (and (not tab-always-indent) | |
f202a7a2 SM |
93 | (or (> (current-column) (current-indentation)) |
94 | (eq this-command last-command)))) | |
a17b712b SM |
95 | (insert-tab arg)) |
96 | ;; Those functions are meant specifically for tabbing and not for | |
97 | ;; indenting, so we can't pass them to indent-according-to-mode. | |
98 | ((memq indent-line-function '(indent-relative indent-relative-maybe)) | |
99 | (funcall indent-line-function)) | |
100 | (t ;; The normal case. | |
101 | (indent-according-to-mode)))) | |
102 | ||
103 | (defun insert-tab (&optional arg) | |
104 | (let ((count (prefix-numeric-value arg))) | |
5e9f80a8 RS |
105 | (if (and abbrev-mode |
106 | (eq (char-syntax (preceding-char)) ?w)) | |
10992b01 RS |
107 | (expand-abbrev)) |
108 | (if indent-tabs-mode | |
4424ce6f | 109 | (insert-char ?\t count) |
10992b01 | 110 | (indent-to (* tab-width (+ count (/ (current-column) tab-width))))))) |
483e630e RM |
111 | |
112 | (defun indent-rigidly (start end arg) | |
113 | "Indent all lines starting in the region sideways by ARG columns. | |
5a0f9388 EZ |
114 | Called from a program, takes three arguments, START, END and ARG. |
115 | You can remove all indentation from a region by giving a large negative ARG." | |
483e630e RM |
116 | (interactive "r\np") |
117 | (save-excursion | |
118 | (goto-char end) | |
119 | (setq end (point-marker)) | |
120 | (goto-char start) | |
121 | (or (bolp) (forward-line 1)) | |
122 | (while (< (point) end) | |
c1194ae8 RS |
123 | (let ((indent (current-indentation)) |
124 | eol-flag) | |
125 | (save-excursion | |
126 | (skip-chars-forward " \t") | |
127 | (setq eol-flag (eolp))) | |
128 | (or eol-flag | |
129 | (indent-to (max 0 (+ indent arg)) 0)) | |
130 | (delete-region (point) (progn (skip-chars-forward " \t") (point)))) | |
483e630e RM |
131 | (forward-line 1)) |
132 | (move-marker end nil))) | |
133 | ||
106b6d0e RS |
134 | (defun indent-line-to (column) |
135 | "Indent current line to COLUMN. | |
136 | This function removes or adds spaces and tabs at beginning of line | |
137 | only if necessary. It leaves point at end of indentation." | |
ca2a3cb7 BG |
138 | (back-to-indentation) |
139 | (let ((cur-col (current-column))) | |
140 | (cond ((< cur-col column) | |
cd6d305e | 141 | (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width) |
a80a30b3 RS |
142 | (delete-region (point) |
143 | (progn (skip-chars-backward " ") (point)))) | |
ca2a3cb7 BG |
144 | (indent-to column)) |
145 | ((> cur-col column) ; too far right (after tab?) | |
34c3f2b8 | 146 | (delete-region (progn (move-to-column column t) (point)) |
ca2a3cb7 | 147 | (progn (back-to-indentation) (point))))))) |
106b6d0e RS |
148 | |
149 | (defun current-left-margin () | |
150 | "Return the left margin to use for this line. | |
151 | This is the value of the buffer-local variable `left-margin' plus the value | |
152 | of the `left-margin' text-property at the start of the line." | |
153 | (save-excursion | |
154 | (back-to-indentation) | |
155 | (max 0 | |
34c3f2b8 BG |
156 | (+ left-margin (or (get-text-property |
157 | (if (and (eobp) (not (bobp))) | |
158 | (1- (point)) (point)) | |
159 | 'left-margin) 0))))) | |
106b6d0e | 160 | |
34c3f2b8 | 161 | (defun move-to-left-margin (&optional n force) |
106b6d0e RS |
162 | "Move to the left margin of the current line. |
163 | With optional argument, move forward N-1 lines first. | |
34c3f2b8 BG |
164 | The column moved to is the one given by the `current-left-margin' function. |
165 | If the line's indentation appears to be wrong, and this command is called | |
166 | interactively or with optional argument FORCE, it will be fixed." | |
167 | (interactive (list (prefix-numeric-value current-prefix-arg) t)) | |
106b6d0e | 168 | (beginning-of-line n) |
54505d72 | 169 | (skip-chars-forward " \t") |
1d1c5af9 RS |
170 | (if (minibufferp (current-buffer)) |
171 | (if (save-excursion (beginning-of-line) (bobp)) | |
172 | (goto-char (minibuffer-prompt-end)) | |
173 | (beginning-of-line)) | |
174 | (let ((lm (current-left-margin)) | |
175 | (cc (current-column))) | |
176 | (cond ((> cc lm) | |
177 | (if (> (move-to-column lm force) lm) | |
178 | ;; If lm is in a tab and we are not forcing, move before tab | |
179 | (backward-char 1))) | |
180 | ((and force (< cc lm)) | |
181 | (indent-to-left-margin)))))) | |
106b6d0e | 182 | |
f202a7a2 | 183 | ;; This used to be the default indent-line-function, |
483e630e RM |
184 | ;; used in Fundamental Mode, Text Mode, etc. |
185 | (defun indent-to-left-margin () | |
34c3f2b8 | 186 | "Indent current line to the column given by `current-left-margin'." |
caadec43 RS |
187 | (save-excursion (indent-line-to (current-left-margin))) |
188 | ;; If we are within the indentation, move past it. | |
189 | (when (save-excursion | |
190 | (skip-chars-backward " \t") | |
191 | (bolp)) | |
192 | (skip-chars-forward " \t"))) | |
106b6d0e | 193 | |
34c3f2b8 BG |
194 | (defun delete-to-left-margin (&optional from to) |
195 | "Remove left margin indentation from a region. | |
196 | This deletes to the column given by `current-left-margin'. | |
197 | In no case will it delete non-whitespace. | |
198 | Args FROM and TO are optional; default is the whole buffer." | |
106b6d0e | 199 | (save-excursion |
34c3f2b8 | 200 | (goto-char (or to (point-max))) |
106b6d0e | 201 | (setq to (point-marker)) |
34c3f2b8 | 202 | (goto-char (or from (point-min))) |
106b6d0e RS |
203 | (or (bolp) (forward-line 1)) |
204 | (while (< (point) to) | |
34c3f2b8 | 205 | (delete-region (point) (progn (move-to-left-margin nil t) (point))) |
106b6d0e RS |
206 | (forward-line 1)) |
207 | (move-marker to nil))) | |
208 | ||
a5b3d1b8 | 209 | (defun set-left-margin (from to width) |
106b6d0e | 210 | "Set the left margin of the region to WIDTH. |
a5b3d1b8 LT |
211 | If `auto-fill-mode' is active, re-fill the region to fit the new margin. |
212 | ||
213 | Interactively, WIDTH is the prefix argument, if specified. | |
214 | Without prefix argument, the command prompts for WIDTH." | |
106b6d0e | 215 | (interactive "r\nNSet left margin to column: ") |
106b6d0e RS |
216 | (save-excursion |
217 | ;; If inside indentation, start from BOL. | |
218 | (goto-char from) | |
219 | (skip-chars-backward " \t") | |
220 | (if (bolp) (setq from (point))) | |
34c3f2b8 | 221 | ;; Place end after whitespace |
106b6d0e | 222 | (goto-char to) |
34c3f2b8 | 223 | (skip-chars-forward " \t") |
106b6d0e | 224 | (setq to (point-marker))) |
34c3f2b8 BG |
225 | ;; Delete margin indentation first, but keep paragraph indentation. |
226 | (delete-to-left-margin from to) | |
a5b3d1b8 LT |
227 | (put-text-property from to 'left-margin width) |
228 | (indent-rigidly from to width) | |
34c3f2b8 | 229 | (if auto-fill-function (save-excursion (fill-region from to nil t t))) |
106b6d0e RS |
230 | (move-marker to nil)) |
231 | ||
a5b3d1b8 | 232 | (defun set-right-margin (from to width) |
106b6d0e | 233 | "Set the right margin of the region to WIDTH. |
a5b3d1b8 LT |
234 | If `auto-fill-mode' is active, re-fill the region to fit the new margin. |
235 | ||
236 | Interactively, WIDTH is the prefix argument, if specified. | |
237 | Without prefix argument, the command prompts for WIDTH." | |
8cf19007 | 238 | (interactive "r\nNSet right margin to width: ") |
106b6d0e RS |
239 | (save-excursion |
240 | (goto-char from) | |
241 | (skip-chars-backward " \t") | |
242 | (if (bolp) (setq from (point)))) | |
a5b3d1b8 | 243 | (put-text-property from to 'right-margin width) |
34c3f2b8 | 244 | (if auto-fill-function (save-excursion (fill-region from to nil t t)))) |
106b6d0e RS |
245 | |
246 | (defun alter-text-property (from to prop func &optional object) | |
247 | "Programmatically change value of a text-property. | |
248 | For each region between FROM and TO that has a single value for PROPERTY, | |
249 | apply FUNCTION to that value and sets the property to the function's result. | |
250 | Optional fifth argument OBJECT specifies the string or buffer to operate on." | |
251 | (let ((begin from) | |
252 | end val) | |
253 | (while (setq val (get-text-property begin prop object) | |
254 | end (text-property-not-all begin to prop val object)) | |
255 | (put-text-property begin end prop (funcall func val) object) | |
256 | (setq begin end)) | |
257 | (if (< begin to) | |
258 | (put-text-property begin to prop (funcall func val) object)))) | |
259 | ||
260 | (defun increase-left-margin (from to inc) | |
261 | "Increase or decrease the left-margin of the region. | |
262 | With no prefix argument, this adds `standard-indent' of indentation. | |
263 | A prefix arg (optional third arg INC noninteractively) specifies the amount | |
264 | to change the margin by, in characters. | |
265 | If `auto-fill-mode' is active, re-fill the region to fit the new margin." | |
266 | (interactive "*r\nP") | |
267 | (setq inc (if inc (prefix-numeric-value inc) standard-indent)) | |
268 | (save-excursion | |
269 | (goto-char from) | |
270 | (skip-chars-backward " \t") | |
271 | (if (bolp) (setq from (point))) | |
272 | (goto-char to) | |
273 | (setq to (point-marker))) | |
106b6d0e | 274 | (alter-text-property from to 'left-margin |
34c3f2b8 BG |
275 | (lambda (v) (max (- left-margin) (+ inc (or v 0))))) |
276 | (indent-rigidly from to inc) | |
277 | (if auto-fill-function (save-excursion (fill-region from to nil t t))) | |
106b6d0e RS |
278 | (move-marker to nil)) |
279 | ||
280 | (defun decrease-left-margin (from to inc) | |
281 | "Make the left margin of the region smaller. | |
282 | With no prefix argument, decrease the indentation by `standard-indent'. | |
283 | A prefix arg (optional third arg INC noninteractively) specifies the amount | |
284 | to change the margin by, in characters. | |
285 | If `auto-fill-mode' is active, re-fill the region to fit the new margin." | |
286 | (interactive "*r\nP") | |
287 | (setq inc (if inc (prefix-numeric-value inc) standard-indent)) | |
288 | (increase-left-margin from to (- inc))) | |
289 | ||
290 | (defun increase-right-margin (from to inc) | |
291 | "Increase the right-margin of the region. | |
292 | With no prefix argument, increase the right margin by `standard-indent'. | |
293 | A prefix arg (optional third arg INC noninteractively) specifies the amount | |
294 | to change the margin by, in characters. A negative argument decreases | |
295 | the right margin width. | |
296 | If `auto-fill-mode' is active, re-fill the region to fit the new margin." | |
297 | (interactive "r\nP") | |
6faab05f | 298 | (setq inc (if inc (prefix-numeric-value inc) standard-indent)) |
106b6d0e RS |
299 | (save-excursion |
300 | (alter-text-property from to 'right-margin | |
6faab05f | 301 | (lambda (v) (+ inc (or v 0)))) |
106b6d0e RS |
302 | (if auto-fill-function |
303 | (fill-region from to nil t t)))) | |
304 | ||
305 | (defun decrease-right-margin (from to inc) | |
306 | "Make the right margin of the region smaller. | |
307 | With no prefix argument, decrease the right margin by `standard-indent'. | |
308 | A prefix arg (optional third arg INC noninteractively) specifies the amount | |
309 | of width to remove, in characters. A negative argument increases | |
310 | the right margin width. | |
311 | If `auto-fill-mode' is active, re-fills region to fit in new margin." | |
312 | (interactive "*r\nP") | |
313 | (setq inc (if inc (prefix-numeric-value inc) standard-indent)) | |
314 | (increase-right-margin from to (- inc))) | |
483e630e | 315 | |
34c3f2b8 BG |
316 | (defun beginning-of-line-text (&optional n) |
317 | "Move to the beginning of the text on this line. | |
318 | With optional argument, move forward N-1 lines first. | |
319 | From the beginning of the line, moves past the left-margin indentation, the | |
320 | fill-prefix, and any indentation used for centering or right-justifying the | |
f1180544 | 321 | line, but does not move past any whitespace that was explicitly inserted |
34c3f2b8 BG |
322 | \(such as a tab used to indent the first line of a paragraph)." |
323 | (interactive "p") | |
324 | (beginning-of-line n) | |
325 | (skip-chars-forward " \t") | |
326 | ;; Skip over fill-prefix. | |
f1180544 | 327 | (if (and fill-prefix |
34c3f2b8 BG |
328 | (not (string-equal fill-prefix ""))) |
329 | (if (equal fill-prefix | |
f1180544 | 330 | (buffer-substring |
34c3f2b8 BG |
331 | (point) (min (point-max) (+ (length fill-prefix) (point))))) |
332 | (forward-char (length fill-prefix))) | |
7d43e2ad | 333 | (if (and adaptive-fill-mode adaptive-fill-regexp |
34c3f2b8 BG |
334 | (looking-at adaptive-fill-regexp)) |
335 | (goto-char (match-end 0)))) | |
336 | ;; Skip centering or flushright indentation | |
337 | (if (memq (current-justification) '(center right)) | |
338 | (skip-chars-forward " \t"))) | |
339 | ||
483e630e | 340 | (defvar indent-region-function nil |
3704e77e RS |
341 | "Short cut function to indent region using `indent-according-to-mode'. |
342 | A value of nil means really run `indent-according-to-mode' on each line.") | |
483e630e | 343 | |
1d9cc345 | 344 | (defun indent-region (start end &optional column) |
483e630e | 345 | "Indent each nonblank line in the region. |
1a2f8b73 RS |
346 | A numeric prefix argument specifies a column: indent each line to that column. |
347 | ||
348 | With no prefix argument, the command chooses one of these methods and | |
349 | indents all the lines with it: | |
350 | ||
351 | 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the | |
352 | beginning of each line in the region that does not already begin | |
353 | with it. | |
354 | 2) If `indent-region-function' is non-nil, call that function | |
355 | to indent the region. | |
356 | 3) Indent each line as specified by the variable `indent-line-function'. | |
357 | ||
358 | Called from a program, START and END specify the region to indent. | |
359 | If the third argument COLUMN is an integer, it specifies the | |
360 | column to indent to; if it is nil, use one of the three methods above." | |
483e630e | 361 | (interactive "r\nP") |
88a2603a | 362 | (if (null column) |
483e630e RM |
363 | (if fill-prefix |
364 | (save-excursion | |
365 | (goto-char end) | |
366 | (setq end (point-marker)) | |
367 | (goto-char start) | |
368 | (let ((regexp (regexp-quote fill-prefix))) | |
88a2603a RS |
369 | (while (< (point) end) |
370 | (or (looking-at regexp) | |
371 | (and (bolp) (eolp)) | |
372 | (insert fill-prefix)) | |
373 | (forward-line 1)))) | |
483e630e RM |
374 | (if indent-region-function |
375 | (funcall indent-region-function start end) | |
376 | (save-excursion | |
a17b712b | 377 | (setq end (copy-marker end)) |
88a2603a | 378 | (goto-char start) |
88a2603a | 379 | (while (< (point) end) |
232acca7 | 380 | (or (and (bolp) (eolp)) |
88a2603a RS |
381 | (funcall indent-line-function)) |
382 | (forward-line 1)) | |
383 | (move-marker end nil)))) | |
384 | (setq column (prefix-numeric-value column)) | |
483e630e RM |
385 | (save-excursion |
386 | (goto-char end) | |
387 | (setq end (point-marker)) | |
388 | (goto-char start) | |
389 | (or (bolp) (forward-line 1)) | |
390 | (while (< (point) end) | |
391 | (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
392 | (or (eolp) | |
106b6d0e | 393 | (indent-to column 0)) |
483e630e RM |
394 | (forward-line 1)) |
395 | (move-marker end nil)))) | |
396 | ||
397 | (defun indent-relative-maybe () | |
1fd63d9b EZ |
398 | "Indent a new line like previous nonblank line. |
399 | If the previous nonblank line has no indent points beyond the | |
400 | column point starts at, this command does nothing. | |
401 | ||
402 | See also `indent-relative'." | |
483e630e RM |
403 | (interactive) |
404 | (indent-relative t)) | |
405 | ||
406 | (defun indent-relative (&optional unindented-ok) | |
407 | "Space out to under next indent point in previous nonblank line. | |
408 | An indent point is a non-whitespace character following whitespace. | |
f6f53207 RS |
409 | The following line shows the indentation points in this line. |
410 | ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
483e630e | 411 | If the previous nonblank line has no indent points beyond the |
1fd63d9b EZ |
412 | column point starts at, `tab-to-tab-stop' is done instead, unless |
413 | this command is invoked with a numeric argument, in which case it | |
414 | does nothing. | |
415 | ||
416 | See also `indent-relative-maybe'." | |
483e630e | 417 | (interactive "P") |
5e9f80a8 RS |
418 | (if (and abbrev-mode |
419 | (eq (char-syntax (preceding-char)) ?w)) | |
420 | (expand-abbrev)) | |
483e630e RM |
421 | (let ((start-column (current-column)) |
422 | indent) | |
423 | (save-excursion | |
424 | (beginning-of-line) | |
425 | (if (re-search-backward "^[^\n]" nil t) | |
426 | (let ((end (save-excursion (forward-line 1) (point)))) | |
427 | (move-to-column start-column) | |
428 | ;; Is start-column inside a tab on this line? | |
429 | (if (> (current-column) start-column) | |
430 | (backward-char 1)) | |
431 | (or (looking-at "[ \t]") | |
432 | unindented-ok | |
433 | (skip-chars-forward "^ \t" end)) | |
434 | (skip-chars-forward " \t" end) | |
435 | (or (= (point) end) (setq indent (current-column)))))) | |
436 | (if indent | |
437 | (let ((opoint (point-marker))) | |
483e630e RM |
438 | (indent-to indent 0) |
439 | (if (> opoint (point)) | |
440 | (goto-char opoint)) | |
441 | (move-marker opoint nil)) | |
442 | (tab-to-tab-stop)))) | |
443 | ||
ddb3136e | 444 | (defcustom tab-stop-list |
483e630e | 445 | '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120) |
0f6ca6cf | 446 | "*List of tab stop positions used by `tab-to-tab-stop'. |
ddb3136e RS |
447 | This should be a list of integers, ordered from smallest to largest." |
448 | :group 'indent | |
449 | :type '(repeat integer)) | |
483e630e | 450 | |
a17b712b SM |
451 | (defvar edit-tab-stops-map |
452 | (let ((map (make-sparse-keymap))) | |
453 | (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes) | |
454 | (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes) | |
455 | map) | |
456 | "Keymap used in `edit-tab-stops'.") | |
483e630e RM |
457 | |
458 | (defvar edit-tab-stops-buffer nil | |
bc57b735 LT |
459 | "Buffer whose tab stops are being edited. |
460 | This matters if the variable `tab-stop-list' is local in that buffer.") | |
483e630e RM |
461 | |
462 | (defun edit-tab-stops () | |
463 | "Edit the tab stops used by `tab-to-tab-stop'. | |
464 | Creates a buffer *Tab Stops* containing text describing the tab stops. | |
465 | A colon indicates a column where there is a tab stop. | |
466 | You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect." | |
467 | (interactive) | |
468 | (setq edit-tab-stops-buffer (current-buffer)) | |
469 | (switch-to-buffer (get-buffer-create "*Tab Stops*")) | |
470 | (use-local-map edit-tab-stops-map) | |
471 | (make-local-variable 'indent-tabs-mode) | |
472 | (setq indent-tabs-mode nil) | |
473 | (overwrite-mode 1) | |
474 | (setq truncate-lines t) | |
475 | (erase-buffer) | |
476 | (let ((tabs tab-stop-list)) | |
477 | (while tabs | |
478 | (indent-to (car tabs) 0) | |
479 | (insert ?:) | |
480 | (setq tabs (cdr tabs)))) | |
481 | (let ((count 0)) | |
482 | (insert ?\n) | |
483 | (while (< count 8) | |
484 | (insert (+ count ?0)) | |
485 | (insert " ") | |
486 | (setq count (1+ count))) | |
487 | (insert ?\n) | |
488 | (while (> count 0) | |
489 | (insert "0123456789") | |
490 | (setq count (1- count)))) | |
491 | (insert "\nTo install changes, type C-c C-c") | |
492 | (goto-char (point-min))) | |
493 | ||
494 | (defun edit-tab-stops-note-changes () | |
495 | "Put edited tab stops into effect." | |
496 | (interactive) | |
497 | (let (tabs) | |
498 | (save-excursion | |
499 | (goto-char 1) | |
500 | (end-of-line) | |
501 | (while (search-backward ":" nil t) | |
502 | (setq tabs (cons (current-column) tabs)))) | |
503 | (bury-buffer (prog1 (current-buffer) | |
504 | (switch-to-buffer edit-tab-stops-buffer))) | |
505 | (setq tab-stop-list tabs)) | |
506 | (message "Tab stops installed")) | |
507 | ||
508 | (defun tab-to-tab-stop () | |
509 | "Insert spaces or tabs to next defined tab-stop column. | |
510 | The variable `tab-stop-list' is a list of columns at which there are tab stops. | |
511 | Use \\[edit-tab-stops] to edit them interactively." | |
512 | (interactive) | |
a2964053 RS |
513 | (and abbrev-mode (= (char-syntax (preceding-char)) ?w) |
514 | (expand-abbrev)) | |
483e630e RM |
515 | (let ((tabs tab-stop-list)) |
516 | (while (and tabs (>= (current-column) (car tabs))) | |
517 | (setq tabs (cdr tabs))) | |
518 | (if tabs | |
9d840bec | 519 | (let ((opoint (point))) |
19b70460 | 520 | (delete-horizontal-space t) |
9d840bec | 521 | (indent-to (car tabs))) |
b379356a | 522 | (insert ?\s)))) |
483e630e RM |
523 | |
524 | (defun move-to-tab-stop () | |
525 | "Move point to next defined tab-stop column. | |
fbf8f564 RS |
526 | The variable `tab-stop-list' is a list of columns at which there are tab stops. |
527 | Use \\[edit-tab-stops] to edit them interactively." | |
528 | (interactive) | |
529 | (let ((tabs tab-stop-list)) | |
530 | (while (and tabs (>= (current-column) (car tabs))) | |
531 | (setq tabs (cdr tabs))) | |
532 | (if tabs | |
533 | (let ((before (point))) | |
534 | (move-to-column (car tabs) t) | |
535 | (save-excursion | |
536 | (goto-char before) | |
537 | ;; If we just added a tab, or moved over one, | |
538 | ;; delete any superfluous spaces before the old point. | |
b379356a | 539 | (if (and (eq (preceding-char) ?\s) |
fbf8f564 RS |
540 | (eq (following-char) ?\t)) |
541 | (let ((tabend (* (/ (current-column) tab-width) tab-width))) | |
542 | (while (and (> (current-column) tabend) | |
b379356a | 543 | (eq (preceding-char) ?\s)) |
fbf8f564 RS |
544 | (forward-char -1)) |
545 | (delete-region (point) before)))))))) | |
546 | ||
483e630e | 547 | (define-key global-map "\t" 'indent-for-tab-command) |
a17b712b | 548 | (define-key esc-map "\C-\\" 'indent-region) |
483e630e RM |
549 | (define-key ctl-x-map "\t" 'indent-rigidly) |
550 | (define-key esc-map "i" 'tab-to-tab-stop) | |
1a06eabd | 551 | |
ab5796a9 | 552 | ;;; arch-tag: f402b2a7-e44f-492f-b5b8-38996020b7c3 |
1a06eabd | 553 | ;;; indent.el ends here |