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