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