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