* lisp/progmodes/ruby-mode.el (ruby-smie--forward-token)
[emacs.git] / lisp / indent.el
blobe733ece66d5b62670e7e8c2642e50676147f4a99
1 ;;; indent.el --- indentation commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985, 1995, 2001-2013 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Package: emacs
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;; Commands for making and changing indentation in text. These are
26 ;; described in the Emacs manual.
28 ;;; Code:
30 (defgroup indent nil
31 "Indentation commands."
32 :group 'editing)
34 (defcustom standard-indent 4
35 "Default number of columns for margin-changing functions to indent."
36 :group 'indent
37 :type 'integer)
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.")
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.
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)))
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)))
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'.
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.
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.
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))
121 (let ((old-tick (buffer-chars-modified-tick))
122 (old-point (point))
123 (old-indent (current-indentation)))
125 ;; Indent the line.
126 (funcall indent-line-function)
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))
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))))))))))
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)))))))
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))))
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)))
175 (define-key map [right]
176 (lambda (beg end) (interactive "r") (indent-rigidly beg end 1)))
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)))))
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))
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))))
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)))))))
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)))))
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))))))
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")))
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)))
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.
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))
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.
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))))
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))))
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))
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)))
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))))
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)))
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")))
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.")
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.
434 With no prefix argument, the command chooses one of these methods and
435 indents all the lines with it:
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'.
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.
481 (save-excursion
482 (setq end (copy-marker end))
483 (goto-char start)
484 (let ((pr (make-progress-reporter "Indenting region..." (point) end)))
485 (while (< (point) end)
486 (or (and (bolp) (eolp))
487 (indent-according-to-mode))
488 (forward-line 1)
489 (progress-reporter-update pr (point)))
490 (progress-reporter-done pr)
491 (move-marker end nil)))))
492 ;; In most cases, reindenting modifies the buffer, but it may also
493 ;; leave it unmodified, in which case we have to deactivate the mark
494 ;; by hand.
495 (deactivate-mark))
497 (defun indent-relative-maybe ()
498 "Indent a new line like previous nonblank line.
499 If the previous nonblank line has no indent points beyond the
500 column point starts at, this command does nothing.
502 See also `indent-relative'."
503 (interactive)
504 (indent-relative t))
506 (defun indent-relative (&optional unindented-ok)
507 "Space out to under next indent point in previous nonblank line.
508 An indent point is a non-whitespace character following whitespace.
509 The following line shows the indentation points in this line.
510 ^ ^ ^ ^ ^ ^ ^ ^ ^
511 If the previous nonblank line has no indent points beyond the
512 column point starts at, `tab-to-tab-stop' is done instead, unless
513 this command is invoked with a numeric argument, in which case it
514 does nothing.
516 See also `indent-relative-maybe'."
517 (interactive "P")
518 (if (and abbrev-mode
519 (eq (char-syntax (preceding-char)) ?w))
520 (expand-abbrev))
521 (let ((start-column (current-column))
522 indent)
523 (save-excursion
524 (beginning-of-line)
525 (if (re-search-backward "^[^\n]" nil t)
526 (let ((end (save-excursion (forward-line 1) (point))))
527 (move-to-column start-column)
528 ;; Is start-column inside a tab on this line?
529 (if (> (current-column) start-column)
530 (backward-char 1))
531 (or (looking-at "[ \t]")
532 unindented-ok
533 (skip-chars-forward "^ \t" end))
534 (skip-chars-forward " \t" end)
535 (or (= (point) end) (setq indent (current-column))))))
536 (if indent
537 (let ((opoint (point-marker)))
538 (indent-to indent 0)
539 (if (> opoint (point))
540 (goto-char opoint))
541 (move-marker opoint nil))
542 (tab-to-tab-stop))))
544 (defcustom tab-stop-list
546 "List of tab stop positions used by `tab-to-tab-stop'.
547 This should be a list of integers, ordered from smallest to largest.
548 It implicitly extends to infinity by repeating the last step (e.g. '(1 2 5)
549 is equivalent to '(1 2 5 8 11)).
550 If the list has less than 2 elements, `tab-width' is used as the \"last step\"."
551 :group 'indent
552 :type '(repeat integer))
553 (put 'tab-stop-list 'safe-local-variable 'listp)
555 (defvar edit-tab-stops-map
556 (let ((map (make-sparse-keymap)))
557 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
558 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
559 map)
560 "Keymap used in `edit-tab-stops'.")
562 (defvar edit-tab-stops-buffer nil
563 "Buffer whose tab stops are being edited.
564 This matters if the variable `tab-stop-list' is local in that buffer.")
566 (defun edit-tab-stops ()
567 "Edit the tab stops used by `tab-to-tab-stop'.
568 Creates a buffer *Tab Stops* containing text describing the tab stops.
569 A colon indicates a column where there is a tab stop.
570 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
571 (interactive)
572 (setq edit-tab-stops-buffer (current-buffer))
573 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
574 (use-local-map edit-tab-stops-map)
575 (setq-local indent-tabs-mode nil)
576 (overwrite-mode 1)
577 (setq truncate-lines t)
578 (erase-buffer)
579 (let ((tabs tab-stop-list))
580 (while tabs
581 (indent-to (car tabs) 0)
582 (insert ?:)
583 (setq tabs (cdr tabs))))
584 (let ((count 0))
585 (insert ?\n)
586 (while (< count 8)
587 (insert (+ count ?0))
588 (insert " ")
589 (setq count (1+ count)))
590 (insert ?\n)
591 (while (> count 0)
592 (insert "0123456789")
593 (setq count (1- count))))
594 (insert "\nTo install changes, type C-c C-c")
595 (goto-char (point-min)))
597 (defun edit-tab-stops-note-changes ()
598 "Put edited tab stops into effect."
599 (interactive)
600 (let (tabs)
601 (save-excursion
602 (goto-char 1)
603 (end-of-line)
604 (while (search-backward ":" nil t)
605 (setq tabs (cons (current-column) tabs))))
606 (bury-buffer (prog1 (current-buffer)
607 (switch-to-buffer edit-tab-stops-buffer)))
608 (setq tab-stop-list tabs))
609 (message "Tab stops installed"))
611 (defun indent--next-tab-stop (column &optional prev)
612 "Return the next tab stop after COLUMN.
613 If PREV is non-nil, return the previous one instead."
614 (let ((tabs tab-stop-list))
615 (while (and tabs (>= column (car tabs)))
616 (setq tabs (cdr tabs)))
617 (if tabs
618 (if (not prev)
619 (car tabs)
620 (let ((prevtabs (cdr (memq (car tabs) (reverse tab-stop-list)))))
621 (if (null prevtabs) 0
622 (if (= column (car prevtabs))
623 (or (nth 1 prevtabs) 0)
624 (car prevtabs)))))
625 ;; We passed the end of tab-stop-list: guess a continuation.
626 (let* ((last2 (last tab-stop-list 2))
627 (step (if (cdr last2) (- (cadr last2) (car last2)) tab-width))
628 (last (or (cadr last2) (car last2) 0)))
629 ;; Repeat the last tab's length.
630 (+ last (* step (if prev
631 (if (<= column last) -1 (/ (- column last 1) step))
632 (1+ (/ (- column last) step)))))))))
634 (defun tab-to-tab-stop ()
635 "Insert spaces or tabs to next defined tab-stop column.
636 The variable `tab-stop-list' is a list of columns at which there are tab stops.
637 Use \\[edit-tab-stops] to edit them interactively."
638 (interactive)
639 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
640 (expand-abbrev))
641 (let ((nexttab (indent--next-tab-stop (current-column))))
642 (delete-horizontal-space t)
643 (indent-to nexttab)))
645 (defun move-to-tab-stop ()
646 "Move point to next defined tab-stop column.
647 The variable `tab-stop-list' is a list of columns at which there are tab stops.
648 Use \\[edit-tab-stops] to edit them interactively."
649 (interactive)
650 (let ((nexttab (indent--next-tab-stop (current-column))))
651 (let ((before (point)))
652 (move-to-column nexttab t)
653 (save-excursion
654 (goto-char before)
655 ;; If we just added a tab, or moved over one,
656 ;; delete any superfluous spaces before the old point.
657 (if (and (eq (preceding-char) ?\s)
658 (eq (following-char) ?\t))
659 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
660 (while (and (> (current-column) tabend)
661 (eq (preceding-char) ?\s))
662 (forward-char -1))
663 (delete-region (point) before)))))))
665 (define-key global-map "\t" 'indent-for-tab-command)
666 (define-key esc-map "\C-\\" 'indent-region)
667 (define-key ctl-x-map "\t" 'indent-rigidly)
668 (define-key esc-map "i" 'tab-to-tab-stop)
670 ;;; indent.el ends here