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