]> code.delx.au - gnu-emacs/blob - lisp/indent.el
* bindings.el (visual-order-cursor-movement): Fix version.
[gnu-emacs] / lisp / indent.el
1 ;;; indent.el --- indentation commands for Emacs -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 1985, 1995, 2001-2013 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Package: emacs
7
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
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) 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
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Commands for making and changing indentation in text. These are
26 ;; described in the Emacs manual.
27
28 ;;; Code:
29
30 (defgroup indent nil
31 "Indentation commands."
32 :group 'editing)
33
34 (defcustom standard-indent 4
35 "Default number of columns for margin-changing functions to indent."
36 :group 'indent
37 :type 'integer)
38
39 (defvar indent-line-function 'indent-relative
40 "Function to indent the current line.
41 This function will be called with no arguments.
42 If it is called somewhere where auto-indentation cannot be done
43 \(e.g. inside a string), the function should simply return `noindent'.
44 Setting this function is all you need to make TAB indent appropriately.
45 Don't rebind TAB unless you really need to.")
46
47 (defcustom tab-always-indent t
48 "Controls the operation of the TAB key.
49 If t, hitting TAB always just indents the current line.
50 If nil, hitting TAB indents the current line if point is at the left margin
51 or in the line's indentation, otherwise it inserts a \"real\" TAB character.
52 If `complete', TAB first tries to indent the current line, and if the line
53 was already indented, then try to complete the thing at point.
54
55 Some programming language modes have their own variable to control this,
56 e.g., `c-tab-always-indent', and do not respect this variable."
57 :group 'indent
58 :type '(choice
59 (const :tag "Always indent" t)
60 (const :tag "Indent if inside indentation, else TAB" nil)
61 (const :tag "Indent, or if already indented complete" complete)))
62
63
64 (defun indent-according-to-mode ()
65 "Indent line in proper way for current major mode.
66 Normally, this is done by calling the function specified by the
67 variable `indent-line-function'. However, if the value of that
68 variable is `indent-relative' or `indent-relative-maybe', handle
69 it specially (since those functions are used for tabbing); in
70 that case, indent by aligning to the previous non-blank line."
71 (interactive)
72 (syntax-propertize (line-end-position))
73 (if (memq indent-line-function
74 '(indent-relative indent-relative-maybe))
75 ;; These functions are used for tabbing, but can't be used for
76 ;; indenting. Replace with something ad-hoc.
77 (let ((column (save-excursion
78 (beginning-of-line)
79 (skip-chars-backward "\n \t")
80 (beginning-of-line)
81 (current-indentation))))
82 (if (<= (current-column) (current-indentation))
83 (indent-line-to column)
84 (save-excursion (indent-line-to column))))
85 ;; The normal case.
86 (funcall indent-line-function)))
87
88 (defun indent-for-tab-command (&optional arg)
89 "Indent the current line or region, or insert a tab, as appropriate.
90 This function either inserts a tab, or indents the current line,
91 or performs symbol completion, depending on `tab-always-indent'.
92 The function called to actually indent the line or insert a tab
93 is given by the variable `indent-line-function'.
94
95 If a prefix argument is given, after this function indents the
96 current line or inserts a tab, it also rigidly indents the entire
97 balanced expression which starts at the beginning of the current
98 line, to reflect the current line's indentation.
99
100 In most major modes, if point was in the current line's
101 indentation, it is moved to the first non-whitespace character
102 after indenting; otherwise it stays at the same position relative
103 to the text.
104
105 If `transient-mark-mode' is turned on and the region is active,
106 this function instead calls `indent-region'. In this case, any
107 prefix argument is ignored."
108 (interactive "P")
109 (cond
110 ;; The region is active, indent it.
111 ((use-region-p)
112 (indent-region (region-beginning) (region-end)))
113 ((or ;; indent-to-left-margin is only meant for indenting,
114 ;; so we force it to always insert a tab here.
115 (eq indent-line-function 'indent-to-left-margin)
116 (and (not tab-always-indent)
117 (or (> (current-column) (current-indentation))
118 (eq this-command last-command))))
119 (insert-tab arg))
120 (t
121 (let ((old-tick (buffer-chars-modified-tick))
122 (old-point (point))
123 (old-indent (current-indentation)))
124
125 ;; Indent the line.
126 (funcall indent-line-function)
127
128 (cond
129 ;; If the text was already indented right, try completion.
130 ((and (eq tab-always-indent 'complete)
131 (eq old-point (point))
132 (eq old-tick (buffer-chars-modified-tick)))
133 (completion-at-point))
134
135 ;; If a prefix argument was given, rigidly indent the following
136 ;; sexp to match the change in the current line's indentation.
137 (arg
138 (let ((end-marker
139 (save-excursion
140 (forward-line 0) (forward-sexp) (point-marker)))
141 (indentation-change (- (current-indentation) old-indent)))
142 (save-excursion
143 (forward-line 1)
144 (when (and (not (zerop indentation-change))
145 (< (point) end-marker))
146 (indent-rigidly (point) end-marker indentation-change))))))))))
147
148 (defun insert-tab (&optional arg)
149 (let ((count (prefix-numeric-value arg)))
150 (if (and abbrev-mode
151 (eq (char-syntax (preceding-char)) ?w))
152 (expand-abbrev))
153 (if indent-tabs-mode
154 (insert-char ?\t count)
155 (indent-to (* tab-width (+ count (/ (current-column) tab-width)))))))
156
157 (defun indent-rigidly--current-indentation (beg end)
158 "Return the smallest indentation in range from BEG to END.
159 Blank lines are ignored."
160 (save-excursion
161 (save-match-data
162 (let ((beg (progn (goto-char beg) (line-beginning-position)))
163 indent)
164 (goto-char beg)
165 (while (re-search-forward "^\\s-*[[:print:]]" end t)
166 (setq indent (min (or indent (current-indentation))
167 (current-indentation))))
168 indent))))
169
170 (defvar indent-rigidly-map
171 (let ((map (make-sparse-keymap)))
172 (define-key map [left]
173 (lambda (beg end) (interactive "r") (indent-rigidly beg end -1)))
174
175 (define-key map [right]
176 (lambda (beg end) (interactive "r") (indent-rigidly beg end 1)))
177
178 (define-key map [S-right]
179 (lambda (beg end) (interactive "r")
180 (let* ((current (indent-rigidly--current-indentation beg end))
181 (next (indent--next-tab-stop current)))
182 (indent-rigidly beg end (- next current)))))
183
184 (define-key map [S-left]
185 (lambda (beg end) (interactive "r")
186 (let* ((current (indent-rigidly--current-indentation beg end))
187 (next (indent--next-tab-stop current 'prev)))
188 (indent-rigidly beg end (- next current)))))
189 map))
190
191 (defun indent-rigidly (start end arg &optional interactive)
192 "Indent all lines starting in the region sideways by ARG columns.
193 Called from a program, takes three arguments, START, END and ARG.
194 You can remove all indentation from a region by giving a large negative ARG.
195 If used interactively and no prefix argument is given, use a transient
196 mode that lets you move the text with cursor keys."
197 (interactive "r\nP\np")
198 (if (and (not arg) interactive)
199 (progn
200 (message "Edit region indentation with <left>, <right>, <S-left> \
201 and <S-right>.")
202 (set-temporary-overlay-map indent-rigidly-map t))
203 (save-excursion
204 (goto-char end)
205 (setq end (point-marker))
206 (goto-char start)
207 (or (bolp) (forward-line 1))
208 (while (< (point) end)
209 (let ((indent (current-indentation))
210 eol-flag)
211 (save-excursion
212 (skip-chars-forward " \t")
213 (setq eol-flag (eolp)))
214 (or eol-flag
215 (indent-to (max 0 (+ indent (prefix-numeric-value arg))) 0))
216 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
217 (forward-line 1))
218 (move-marker end nil))))
219
220 (defun indent-line-to (column)
221 "Indent current line to COLUMN.
222 This function removes or adds spaces and tabs at beginning of line
223 only if necessary. It leaves point at end of indentation."
224 (back-to-indentation)
225 (let ((cur-col (current-column)))
226 (cond ((< cur-col column)
227 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
228 (delete-region (point)
229 (progn (skip-chars-backward " ") (point))))
230 (indent-to column))
231 ((> cur-col column) ; too far right (after tab?)
232 (delete-region (progn (move-to-column column t) (point))
233 (progn (back-to-indentation) (point)))))))
234
235 (defun current-left-margin ()
236 "Return the left margin to use for this line.
237 This is the value of the buffer-local variable `left-margin' plus the value
238 of the `left-margin' text-property at the start of the line."
239 (save-excursion
240 (back-to-indentation)
241 (max 0
242 (+ left-margin (or (get-text-property
243 (if (and (eobp) (not (bobp)))
244 (1- (point)) (point))
245 'left-margin) 0)))))
246
247 (defun move-to-left-margin (&optional n force)
248 "Move to the left margin of the current line.
249 With optional argument, move forward N-1 lines first.
250 The column moved to is the one given by the `current-left-margin' function.
251 If the line's indentation appears to be wrong, and this command is called
252 interactively or with optional argument FORCE, it will be fixed."
253 (interactive (list (prefix-numeric-value current-prefix-arg) t))
254 (beginning-of-line n)
255 (skip-chars-forward " \t")
256 (if (minibufferp (current-buffer))
257 (if (save-excursion (beginning-of-line) (bobp))
258 (goto-char (minibuffer-prompt-end))
259 (beginning-of-line))
260 (let ((lm (current-left-margin))
261 (cc (current-column)))
262 (cond ((> cc lm)
263 (if (> (move-to-column lm force) lm)
264 ;; If lm is in a tab and we are not forcing, move before tab
265 (backward-char 1)))
266 ((and force (< cc lm))
267 (indent-to-left-margin))))))
268
269 ;; This used to be the default indent-line-function,
270 ;; used in Fundamental Mode, Text Mode, etc.
271 (defun indent-to-left-margin ()
272 "Indent current line to the column given by `current-left-margin'."
273 (save-excursion (indent-line-to (current-left-margin)))
274 ;; If we are within the indentation, move past it.
275 (when (save-excursion
276 (skip-chars-backward " \t")
277 (bolp))
278 (skip-chars-forward " \t")))
279
280 (defun delete-to-left-margin (&optional from to)
281 "Remove left margin indentation from a region.
282 This deletes to the column given by `current-left-margin'.
283 In no case will it delete non-whitespace.
284 Args FROM and TO are optional; default is the whole buffer."
285 (save-excursion
286 (goto-char (or to (point-max)))
287 (setq to (point-marker))
288 (goto-char (or from (point-min)))
289 (or (bolp) (forward-line 1))
290 (while (< (point) to)
291 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
292 (forward-line 1))
293 (move-marker to nil)))
294
295 (defun set-left-margin (from to width)
296 "Set the left margin of the region to WIDTH.
297 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
298
299 Interactively, WIDTH is the prefix argument, if specified.
300 Without prefix argument, the command prompts for WIDTH."
301 (interactive "r\nNSet left margin to column: ")
302 (save-excursion
303 ;; If inside indentation, start from BOL.
304 (goto-char from)
305 (skip-chars-backward " \t")
306 (if (bolp) (setq from (point)))
307 ;; Place end after whitespace
308 (goto-char to)
309 (skip-chars-forward " \t")
310 (setq to (point-marker)))
311 ;; Delete margin indentation first, but keep paragraph indentation.
312 (delete-to-left-margin from to)
313 (put-text-property from to 'left-margin width)
314 (indent-rigidly from to width)
315 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
316 (move-marker to nil))
317
318 (defun set-right-margin (from to width)
319 "Set the right margin of the region to WIDTH.
320 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
321
322 Interactively, WIDTH is the prefix argument, if specified.
323 Without prefix argument, the command prompts for WIDTH."
324 (interactive "r\nNSet right margin to width: ")
325 (save-excursion
326 (goto-char from)
327 (skip-chars-backward " \t")
328 (if (bolp) (setq from (point))))
329 (put-text-property from to 'right-margin width)
330 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
331
332 (defun alter-text-property (from to prop func &optional object)
333 "Programmatically change value of a text-property.
334 For each region between FROM and TO that has a single value for PROPERTY,
335 apply FUNCTION to that value and sets the property to the function's result.
336 Optional fifth argument OBJECT specifies the string or buffer to operate on."
337 (let ((begin from)
338 end val)
339 (while (setq val (get-text-property begin prop object)
340 end (text-property-not-all begin to prop val object))
341 (put-text-property begin end prop (funcall func val) object)
342 (setq begin end))
343 (if (< begin to)
344 (put-text-property begin to prop (funcall func val) object))))
345
346 (defun increase-left-margin (from to inc)
347 "Increase or decrease the left-margin of the region.
348 With no prefix argument, this adds `standard-indent' of indentation.
349 A prefix arg (optional third arg INC noninteractively) specifies the amount
350 to change the margin by, in characters.
351 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
352 (interactive "*r\nP")
353 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
354 (save-excursion
355 (goto-char from)
356 (skip-chars-backward " \t")
357 (if (bolp) (setq from (point)))
358 (goto-char to)
359 (setq to (point-marker)))
360 (alter-text-property from to 'left-margin
361 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
362 (indent-rigidly from to inc)
363 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
364 (move-marker to nil))
365
366 (defun decrease-left-margin (from to inc)
367 "Make the left margin of the region smaller.
368 With no prefix argument, decrease the indentation by `standard-indent'.
369 A prefix arg (optional third arg INC noninteractively) specifies the amount
370 to change the margin by, in characters.
371 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
372 (interactive "*r\nP")
373 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
374 (increase-left-margin from to (- inc)))
375
376 (defun increase-right-margin (from to inc)
377 "Increase the right-margin of the region.
378 With no prefix argument, increase the right margin by `standard-indent'.
379 A prefix arg (optional third arg INC noninteractively) specifies the amount
380 to change the margin by, in characters. A negative argument decreases
381 the right margin width.
382 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
383 (interactive "r\nP")
384 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
385 (save-excursion
386 (alter-text-property from to 'right-margin
387 (lambda (v) (+ inc (or v 0))))
388 (if auto-fill-function
389 (fill-region from to nil t t))))
390
391 (defun decrease-right-margin (from to inc)
392 "Make the right margin of the region smaller.
393 With no prefix argument, decrease the right margin by `standard-indent'.
394 A prefix arg (optional third arg INC noninteractively) specifies the amount
395 of width to remove, in characters. A negative argument increases
396 the right margin width.
397 If `auto-fill-mode' is active, re-fills region to fit in new margin."
398 (interactive "*r\nP")
399 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
400 (increase-right-margin from to (- inc)))
401
402 (defun beginning-of-line-text (&optional n)
403 "Move to the beginning of the text on this line.
404 With optional argument, move forward N-1 lines first.
405 From the beginning of the line, moves past the left-margin indentation, the
406 fill-prefix, and any indentation used for centering or right-justifying the
407 line, but does not move past any whitespace that was explicitly inserted
408 \(such as a tab used to indent the first line of a paragraph)."
409 (interactive "p")
410 (beginning-of-line n)
411 (skip-chars-forward " \t")
412 ;; Skip over fill-prefix.
413 (if (and fill-prefix
414 (not (string-equal fill-prefix "")))
415 (if (equal fill-prefix
416 (buffer-substring
417 (point) (min (point-max) (+ (length fill-prefix) (point)))))
418 (forward-char (length fill-prefix)))
419 (if (and adaptive-fill-mode adaptive-fill-regexp
420 (looking-at adaptive-fill-regexp))
421 (goto-char (match-end 0))))
422 ;; Skip centering or flushright indentation
423 (if (memq (current-justification) '(center right))
424 (skip-chars-forward " \t")))
425
426 (defvar indent-region-function nil
427 "Short cut function to indent region using `indent-according-to-mode'.
428 A value of nil means really run `indent-according-to-mode' on each line.")
429
430 (defun indent-region (start end &optional column)
431 "Indent each nonblank line in the region.
432 A numeric prefix argument specifies a column: indent each line to that column.
433
434 With no prefix argument, the command chooses one of these methods and
435 indents all the lines with it:
436
437 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
438 beginning of each line in the region that does not already begin
439 with it.
440 2) If `indent-region-function' is non-nil, call that function
441 to indent the region.
442 3) Indent each line via `indent-according-to-mode'.
443
444 Called from a program, START and END specify the region to indent.
445 If the third argument COLUMN is an integer, it specifies the
446 column to indent to; if it is nil, use one of the three methods above."
447 (interactive "r\nP")
448 (cond
449 ;; If a numeric prefix is given, indent to that column.
450 (column
451 (setq column (prefix-numeric-value column))
452 (save-excursion
453 (goto-char end)
454 (setq end (point-marker))
455 (goto-char start)
456 (or (bolp) (forward-line 1))
457 (while (< (point) end)
458 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
459 (or (eolp)
460 (indent-to column 0))
461 (forward-line 1))
462 (move-marker end nil)))
463 ;; If a fill-prefix is specified, use it.
464 (fill-prefix
465 (save-excursion
466 (goto-char end)
467 (setq end (point-marker))
468 (goto-char start)
469 (let ((regexp (regexp-quote fill-prefix)))
470 (while (< (point) end)
471 (or (looking-at regexp)
472 (and (bolp) (eolp))
473 (insert fill-prefix))
474 (forward-line 1)))))
475 ;; Use indent-region-function is available.
476 (indent-region-function
477 (funcall indent-region-function start end))
478 ;; Else, use a default implementation that calls indent-line-function on
479 ;; each line.
480 (t
481 (save-excursion
482 (setq end (copy-marker end))
483 (goto-char start)
484 (let ((pr (unless (minibufferp)
485 (make-progress-reporter "Indenting region..." (point) end))))
486 (while (< (point) end)
487 (or (and (bolp) (eolp))
488 (indent-according-to-mode))
489 (forward-line 1)
490 (and pr (progress-reporter-update pr (point))))
491 (and pr (progress-reporter-done pr))
492 (move-marker end nil)))))
493 ;; In most cases, reindenting modifies the buffer, but it may also
494 ;; leave it unmodified, in which case we have to deactivate the mark
495 ;; by hand.
496 (deactivate-mark))
497
498 (defun indent-relative-maybe ()
499 "Indent a new line like previous nonblank line.
500 If the previous nonblank line has no indent points beyond the
501 column point starts at, this command does nothing.
502
503 See also `indent-relative'."
504 (interactive)
505 (indent-relative t))
506
507 (defun indent-relative (&optional unindented-ok)
508 "Space out to under next indent point in previous nonblank line.
509 An indent point is a non-whitespace character following whitespace.
510 The following line shows the indentation points in this line.
511 ^ ^ ^ ^ ^ ^ ^ ^ ^
512 If the previous nonblank line has no indent points beyond the
513 column point starts at, `tab-to-tab-stop' is done instead, unless
514 this command is invoked with a numeric argument, in which case it
515 does nothing.
516
517 See also `indent-relative-maybe'."
518 (interactive "P")
519 (if (and abbrev-mode
520 (eq (char-syntax (preceding-char)) ?w))
521 (expand-abbrev))
522 (let ((start-column (current-column))
523 indent)
524 (save-excursion
525 (beginning-of-line)
526 (if (re-search-backward "^[^\n]" nil t)
527 (let ((end (save-excursion (forward-line 1) (point))))
528 (move-to-column start-column)
529 ;; Is start-column inside a tab on this line?
530 (if (> (current-column) start-column)
531 (backward-char 1))
532 (or (looking-at "[ \t]")
533 unindented-ok
534 (skip-chars-forward "^ \t" end))
535 (skip-chars-forward " \t" end)
536 (or (= (point) end) (setq indent (current-column))))))
537 (if indent
538 (let ((opoint (point-marker)))
539 (indent-to indent 0)
540 (if (> opoint (point))
541 (goto-char opoint))
542 (move-marker opoint nil))
543 (tab-to-tab-stop))))
544
545 (defcustom tab-stop-list
546 nil
547 "List of tab stop positions used by `tab-to-tab-stop'.
548 This should be a list of integers, ordered from smallest to largest.
549 It implicitly extends to infinity by repeating the last step (e.g. '(1 2 5)
550 is equivalent to '(1 2 5 8 11)).
551 If the list has less than 2 elements, `tab-width' is used as the \"last step\"."
552 :group 'indent
553 :type '(repeat integer))
554 (put 'tab-stop-list 'safe-local-variable 'listp)
555
556 (defvar edit-tab-stops-map
557 (let ((map (make-sparse-keymap)))
558 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
559 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
560 map)
561 "Keymap used in `edit-tab-stops'.")
562
563 (defvar edit-tab-stops-buffer nil
564 "Buffer whose tab stops are being edited.
565 This matters if the variable `tab-stop-list' is local in that buffer.")
566
567 (defun edit-tab-stops ()
568 "Edit the tab stops used by `tab-to-tab-stop'.
569 Creates a buffer *Tab Stops* containing text describing the tab stops.
570 A colon indicates a column where there is a tab stop.
571 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
572 (interactive)
573 (setq edit-tab-stops-buffer (current-buffer))
574 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
575 (use-local-map edit-tab-stops-map)
576 (setq-local indent-tabs-mode nil)
577 (overwrite-mode 1)
578 (setq truncate-lines t)
579 (erase-buffer)
580 (let ((tabs tab-stop-list))
581 (while tabs
582 (indent-to (car tabs) 0)
583 (insert ?:)
584 (setq tabs (cdr tabs))))
585 (let ((count 0))
586 (insert ?\n)
587 (while (< count 8)
588 (insert (+ count ?0))
589 (insert " ")
590 (setq count (1+ count)))
591 (insert ?\n)
592 (while (> count 0)
593 (insert "0123456789")
594 (setq count (1- count))))
595 (insert "\nTo install changes, type C-c C-c")
596 (goto-char (point-min)))
597
598 (defun edit-tab-stops-note-changes ()
599 "Put edited tab stops into effect."
600 (interactive)
601 (let (tabs)
602 (save-excursion
603 (goto-char 1)
604 (end-of-line)
605 (while (search-backward ":" nil t)
606 (setq tabs (cons (current-column) tabs))))
607 (bury-buffer (prog1 (current-buffer)
608 (switch-to-buffer edit-tab-stops-buffer)))
609 (setq tab-stop-list tabs))
610 (message "Tab stops installed"))
611
612 (defun indent--next-tab-stop (column &optional prev)
613 "Return the next tab stop after COLUMN.
614 If PREV is non-nil, return the previous one instead."
615 (let ((tabs tab-stop-list))
616 (while (and tabs (>= column (car tabs)))
617 (setq tabs (cdr tabs)))
618 (if tabs
619 (if (not prev)
620 (car tabs)
621 (let ((prevtabs (cdr (memq (car tabs) (reverse tab-stop-list)))))
622 (if (null prevtabs) 0
623 (if (= column (car prevtabs))
624 (or (nth 1 prevtabs) 0)
625 (car prevtabs)))))
626 ;; We passed the end of tab-stop-list: guess a continuation.
627 (let* ((last2 (last tab-stop-list 2))
628 (step (if (cdr last2) (- (cadr last2) (car last2)) tab-width))
629 (last (or (cadr last2) (car last2) 0)))
630 ;; Repeat the last tab's length.
631 (+ last (* step (if prev
632 (if (<= column last) -1 (/ (- column last 1) step))
633 (1+ (/ (- column last) step)))))))))
634
635 (defun tab-to-tab-stop ()
636 "Insert spaces or tabs to next defined tab-stop column.
637 The variable `tab-stop-list' is a list of columns at which there are tab stops.
638 Use \\[edit-tab-stops] to edit them interactively."
639 (interactive)
640 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
641 (expand-abbrev))
642 (let ((nexttab (indent--next-tab-stop (current-column))))
643 (delete-horizontal-space t)
644 (indent-to nexttab)))
645
646 (defun move-to-tab-stop ()
647 "Move point to next defined tab-stop column.
648 The variable `tab-stop-list' is a list of columns at which there are tab stops.
649 Use \\[edit-tab-stops] to edit them interactively."
650 (interactive)
651 (let ((nexttab (indent--next-tab-stop (current-column))))
652 (let ((before (point)))
653 (move-to-column nexttab t)
654 (save-excursion
655 (goto-char before)
656 ;; If we just added a tab, or moved over one,
657 ;; delete any superfluous spaces before the old point.
658 (if (and (eq (preceding-char) ?\s)
659 (eq (following-char) ?\t))
660 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
661 (while (and (> (current-column) tabend)
662 (eq (preceding-char) ?\s))
663 (forward-char -1))
664 (delete-region (point) before)))))))
665
666 (define-key global-map "\t" 'indent-for-tab-command)
667 (define-key esc-map "\C-\\" 'indent-region)
668 (define-key ctl-x-map "\t" 'indent-rigidly)
669 (define-key esc-map "i" 'tab-to-tab-stop)
670
671 ;;; indent.el ends here