1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985, 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
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/>.
25 ;; Commands for making and changing indentation in text. These are
26 ;; described in the Emacs manual.
31 "Indentation commands."
34 (defcustom standard-indent
4
35 "Default number of columns for margin-changing functions to indent."
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."
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 The buffer-local variable `indent-line-function' determines how to do this,
67 but the functions `indent-relative' and `indent-relative-maybe' are
68 special; we don't actually use them here."
70 (if (memq indent-line-function
71 '(indent-relative indent-relative-maybe
))
72 ;; These functions are used for tabbing, but can't be used for
73 ;; indenting. Replace with something ad-hoc.
74 (let ((column (save-excursion
76 (skip-chars-backward "\n \t")
78 (current-indentation))))
79 (if (<= (current-column) (current-indentation))
80 (indent-line-to column
)
81 (save-excursion (indent-line-to column
))))
83 (funcall indent-line-function
)))
85 (defun indent-for-tab-command (&optional arg
)
86 "Indent line or region in proper way for current major mode or insert a tab.
87 Depending on `tab-always-indent', either insert a tab or indent.
89 In most major modes, if point was in the current line's indentation,
90 it is moved to the first non-whitespace character after indenting;
91 otherwise it stays at the same position in the text.
93 If a prefix argument is given, also rigidly indent the entire
94 balanced expression which starts at the beginning of the current
95 line to reflect the current line's change in indentation.
97 If `transient-mark-mode' is turned on and the region is active,
98 indent the region (in this case, any prefix argument is ignored).
100 The function actually called to indent the line is determined by the value of
101 `indent-line-function'."
104 ;; The region is active, indent it.
106 (indent-region (region-beginning) (region-end)))
107 ((or ;; indent-to-left-margin is only meant for indenting,
108 ;; so we force it to always insert a tab here.
109 (eq indent-line-function
'indent-to-left-margin
)
110 (and (not tab-always-indent
)
111 (or (> (current-column) (current-indentation))
112 (eq this-command last-command
))))
115 (let ((old-tick (buffer-chars-modified-tick))
117 (old-indent (current-indentation)))
120 (funcall indent-line-function
)
123 ;; If the text was already indented right, try completion.
124 ((and (eq tab-always-indent
'complete
)
125 (eq old-point
(point))
126 (eq old-tick
(buffer-chars-modified-tick)))
127 (completion-at-point))
129 ;; If a prefix argument was given, rigidly indent the following
130 ;; sexp to match the change in the current line's indentation.
134 (forward-line 0) (forward-sexp) (point-marker)))
135 (indentation-change (- (current-indentation) old-indent
)))
138 (when (and (not (zerop indentation-change
))
139 (< (point) end-marker
))
140 (indent-rigidly (point) end-marker indentation-change
))))))))))
142 (defun insert-tab (&optional arg
)
143 (let ((count (prefix-numeric-value arg
)))
145 (eq (char-syntax (preceding-char)) ?w
))
148 (insert-char ?
\t count
)
149 (indent-to (* tab-width
(+ count
(/ (current-column) tab-width
)))))))
151 (defun indent-rigidly (start end arg
)
152 "Indent all lines starting in the region sideways by ARG columns.
153 Called from a program, takes three arguments, START, END and ARG.
154 You can remove all indentation from a region by giving a large negative ARG."
158 (setq end
(point-marker))
160 (or (bolp) (forward-line 1))
161 (while (< (point) end
)
162 (let ((indent (current-indentation))
165 (skip-chars-forward " \t")
166 (setq eol-flag
(eolp)))
168 (indent-to (max 0 (+ indent arg
)) 0))
169 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
171 (move-marker end nil
)))
173 (defun indent-line-to (column)
174 "Indent current line to COLUMN.
175 This function removes or adds spaces and tabs at beginning of line
176 only if necessary. It leaves point at end of indentation."
177 (back-to-indentation)
178 (let ((cur-col (current-column)))
179 (cond ((< cur-col column
)
180 (if (>= (- column
(* (/ cur-col tab-width
) tab-width
)) tab-width
)
181 (delete-region (point)
182 (progn (skip-chars-backward " ") (point))))
184 ((> cur-col column
) ; too far right (after tab?)
185 (delete-region (progn (move-to-column column t
) (point))
186 (progn (back-to-indentation) (point)))))))
188 (defun current-left-margin ()
189 "Return the left margin to use for this line.
190 This is the value of the buffer-local variable `left-margin' plus the value
191 of the `left-margin' text-property at the start of the line."
193 (back-to-indentation)
195 (+ left-margin
(or (get-text-property
196 (if (and (eobp) (not (bobp)))
197 (1- (point)) (point))
200 (defun move-to-left-margin (&optional n force
)
201 "Move to the left margin of the current line.
202 With optional argument, move forward N-1 lines first.
203 The column moved to is the one given by the `current-left-margin' function.
204 If the line's indentation appears to be wrong, and this command is called
205 interactively or with optional argument FORCE, it will be fixed."
206 (interactive (list (prefix-numeric-value current-prefix-arg
) t
))
207 (beginning-of-line n
)
208 (skip-chars-forward " \t")
209 (if (minibufferp (current-buffer))
210 (if (save-excursion (beginning-of-line) (bobp))
211 (goto-char (minibuffer-prompt-end))
213 (let ((lm (current-left-margin))
214 (cc (current-column)))
216 (if (> (move-to-column lm force
) lm
)
217 ;; If lm is in a tab and we are not forcing, move before tab
219 ((and force
(< cc lm
))
220 (indent-to-left-margin))))))
222 ;; This used to be the default indent-line-function,
223 ;; used in Fundamental Mode, Text Mode, etc.
224 (defun indent-to-left-margin ()
225 "Indent current line to the column given by `current-left-margin'."
226 (save-excursion (indent-line-to (current-left-margin)))
227 ;; If we are within the indentation, move past it.
228 (when (save-excursion
229 (skip-chars-backward " \t")
231 (skip-chars-forward " \t")))
233 (defun delete-to-left-margin (&optional from to
)
234 "Remove left margin indentation from a region.
235 This deletes to the column given by `current-left-margin'.
236 In no case will it delete non-whitespace.
237 Args FROM and TO are optional; default is the whole buffer."
239 (goto-char (or to
(point-max)))
240 (setq to
(point-marker))
241 (goto-char (or from
(point-min)))
242 (or (bolp) (forward-line 1))
243 (while (< (point) to
)
244 (delete-region (point) (progn (move-to-left-margin nil t
) (point)))
246 (move-marker to nil
)))
248 (defun set-left-margin (from to width
)
249 "Set the left margin of the region to WIDTH.
250 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
252 Interactively, WIDTH is the prefix argument, if specified.
253 Without prefix argument, the command prompts for WIDTH."
254 (interactive "r\nNSet left margin to column: ")
256 ;; If inside indentation, start from BOL.
258 (skip-chars-backward " \t")
259 (if (bolp) (setq from
(point)))
260 ;; Place end after whitespace
262 (skip-chars-forward " \t")
263 (setq to
(point-marker)))
264 ;; Delete margin indentation first, but keep paragraph indentation.
265 (delete-to-left-margin from to
)
266 (put-text-property from to
'left-margin width
)
267 (indent-rigidly from to width
)
268 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
269 (move-marker to nil
))
271 (defun set-right-margin (from to width
)
272 "Set the right margin of the region to WIDTH.
273 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
275 Interactively, WIDTH is the prefix argument, if specified.
276 Without prefix argument, the command prompts for WIDTH."
277 (interactive "r\nNSet right margin to width: ")
280 (skip-chars-backward " \t")
281 (if (bolp) (setq from
(point))))
282 (put-text-property from to
'right-margin width
)
283 (if auto-fill-function
(save-excursion (fill-region from to nil t t
))))
285 (defun alter-text-property (from to prop func
&optional object
)
286 "Programmatically change value of a text-property.
287 For each region between FROM and TO that has a single value for PROPERTY,
288 apply FUNCTION to that value and sets the property to the function's result.
289 Optional fifth argument OBJECT specifies the string or buffer to operate on."
292 (while (setq val
(get-text-property begin prop object
)
293 end
(text-property-not-all begin to prop val object
))
294 (put-text-property begin end prop
(funcall func val
) object
)
297 (put-text-property begin to prop
(funcall func val
) object
))))
299 (defun increase-left-margin (from to inc
)
300 "Increase or decrease the left-margin of the region.
301 With no prefix argument, this adds `standard-indent' of indentation.
302 A prefix arg (optional third arg INC noninteractively) specifies the amount
303 to change the margin by, in characters.
304 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
305 (interactive "*r\nP")
306 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
309 (skip-chars-backward " \t")
310 (if (bolp) (setq from
(point)))
312 (setq to
(point-marker)))
313 (alter-text-property from to
'left-margin
314 (lambda (v) (max (- left-margin
) (+ inc
(or v
0)))))
315 (indent-rigidly from to inc
)
316 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
317 (move-marker to nil
))
319 (defun decrease-left-margin (from to inc
)
320 "Make the left margin of the region smaller.
321 With no prefix argument, decrease the indentation by `standard-indent'.
322 A prefix arg (optional third arg INC noninteractively) specifies the amount
323 to change the margin by, in characters.
324 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
325 (interactive "*r\nP")
326 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
327 (increase-left-margin from to
(- inc
)))
329 (defun increase-right-margin (from to inc
)
330 "Increase the right-margin of the region.
331 With no prefix argument, increase the right margin by `standard-indent'.
332 A prefix arg (optional third arg INC noninteractively) specifies the amount
333 to change the margin by, in characters. A negative argument decreases
334 the right margin width.
335 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
337 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
339 (alter-text-property from to
'right-margin
340 (lambda (v) (+ inc
(or v
0))))
341 (if auto-fill-function
342 (fill-region from to nil t t
))))
344 (defun decrease-right-margin (from to inc
)
345 "Make the right margin of the region smaller.
346 With no prefix argument, decrease the right margin by `standard-indent'.
347 A prefix arg (optional third arg INC noninteractively) specifies the amount
348 of width to remove, in characters. A negative argument increases
349 the right margin width.
350 If `auto-fill-mode' is active, re-fills region to fit in new margin."
351 (interactive "*r\nP")
352 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
353 (increase-right-margin from to
(- inc
)))
355 (defun beginning-of-line-text (&optional n
)
356 "Move to the beginning of the text on this line.
357 With optional argument, move forward N-1 lines first.
358 From the beginning of the line, moves past the left-margin indentation, the
359 fill-prefix, and any indentation used for centering or right-justifying the
360 line, but does not move past any whitespace that was explicitly inserted
361 \(such as a tab used to indent the first line of a paragraph)."
363 (beginning-of-line n
)
364 (skip-chars-forward " \t")
365 ;; Skip over fill-prefix.
367 (not (string-equal fill-prefix
"")))
368 (if (equal fill-prefix
370 (point) (min (point-max) (+ (length fill-prefix
) (point)))))
371 (forward-char (length fill-prefix
)))
372 (if (and adaptive-fill-mode adaptive-fill-regexp
373 (looking-at adaptive-fill-regexp
))
374 (goto-char (match-end 0))))
375 ;; Skip centering or flushright indentation
376 (if (memq (current-justification) '(center right
))
377 (skip-chars-forward " \t")))
379 (defvar indent-region-function nil
380 "Short cut function to indent region using `indent-according-to-mode'.
381 A value of nil means really run `indent-according-to-mode' on each line.")
383 (defun indent-region (start end
&optional column
)
384 "Indent each nonblank line in the region.
385 A numeric prefix argument specifies a column: indent each line to that column.
387 With no prefix argument, the command chooses one of these methods and
388 indents all the lines with it:
390 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
391 beginning of each line in the region that does not already begin
393 2) If `indent-region-function' is non-nil, call that function
394 to indent the region.
395 3) Indent each line as specified by the variable `indent-line-function'.
397 Called from a program, START and END specify the region to indent.
398 If the third argument COLUMN is an integer, it specifies the
399 column to indent to; if it is nil, use one of the three methods above."
405 (setq end
(point-marker))
407 (let ((regexp (regexp-quote fill-prefix
)))
408 (while (< (point) end
)
409 (or (looking-at regexp
)
411 (insert fill-prefix
))
413 (if indent-region-function
414 (funcall indent-region-function start end
)
416 (setq end
(copy-marker end
))
418 (while (< (point) end
)
419 (or (and (bolp) (eolp))
420 (funcall indent-line-function
))
422 (move-marker end nil
))))
423 (setq column
(prefix-numeric-value column
))
426 (setq end
(point-marker))
428 (or (bolp) (forward-line 1))
429 (while (< (point) end
)
430 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
432 (indent-to column
0))
434 (move-marker end nil
)))
435 ;; In most cases, reindenting modifies the buffer, but it may also
436 ;; leave it unmodified, in which case we have to deactivate the mark
440 (defun indent-relative-maybe ()
441 "Indent a new line like previous nonblank line.
442 If the previous nonblank line has no indent points beyond the
443 column point starts at, this command does nothing.
445 See also `indent-relative'."
449 (defun indent-relative (&optional unindented-ok
)
450 "Space out to under next indent point in previous nonblank line.
451 An indent point is a non-whitespace character following whitespace.
452 The following line shows the indentation points in this line.
454 If the previous nonblank line has no indent points beyond the
455 column point starts at, `tab-to-tab-stop' is done instead, unless
456 this command is invoked with a numeric argument, in which case it
459 See also `indent-relative-maybe'."
462 (eq (char-syntax (preceding-char)) ?w
))
464 (let ((start-column (current-column))
468 (if (re-search-backward "^[^\n]" nil t
)
469 (let ((end (save-excursion (forward-line 1) (point))))
470 (move-to-column start-column
)
471 ;; Is start-column inside a tab on this line?
472 (if (> (current-column) start-column
)
474 (or (looking-at "[ \t]")
476 (skip-chars-forward "^ \t" end
))
477 (skip-chars-forward " \t" end
)
478 (or (= (point) end
) (setq indent
(current-column))))))
480 (let ((opoint (point-marker)))
482 (if (> opoint
(point))
484 (move-marker opoint nil
))
487 (defcustom tab-stop-list
488 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
489 "List of tab stop positions used by `tab-to-tab-stop'.
490 This should be a list of integers, ordered from smallest to largest."
492 :type
'(repeat integer
))
493 (put 'tab-stop-list
'safe-local-variable
'listp
)
495 (defvar edit-tab-stops-map
496 (let ((map (make-sparse-keymap)))
497 (define-key map
"\C-x\C-s" 'edit-tab-stops-note-changes
)
498 (define-key map
"\C-c\C-c" 'edit-tab-stops-note-changes
)
500 "Keymap used in `edit-tab-stops'.")
502 (defvar edit-tab-stops-buffer nil
503 "Buffer whose tab stops are being edited.
504 This matters if the variable `tab-stop-list' is local in that buffer.")
506 (defun edit-tab-stops ()
507 "Edit the tab stops used by `tab-to-tab-stop'.
508 Creates a buffer *Tab Stops* containing text describing the tab stops.
509 A colon indicates a column where there is a tab stop.
510 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
512 (setq edit-tab-stops-buffer
(current-buffer))
513 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
514 (use-local-map edit-tab-stops-map
)
515 (make-local-variable 'indent-tabs-mode
)
516 (setq indent-tabs-mode nil
)
518 (setq truncate-lines t
)
520 (let ((tabs tab-stop-list
))
522 (indent-to (car tabs
) 0)
524 (setq tabs
(cdr tabs
))))
528 (insert (+ count ?
0))
530 (setq count
(1+ count
)))
533 (insert "0123456789")
534 (setq count
(1- count
))))
535 (insert "\nTo install changes, type C-c C-c")
536 (goto-char (point-min)))
538 (defun edit-tab-stops-note-changes ()
539 "Put edited tab stops into effect."
545 (while (search-backward ":" nil t
)
546 (setq tabs
(cons (current-column) tabs
))))
547 (bury-buffer (prog1 (current-buffer)
548 (switch-to-buffer edit-tab-stops-buffer
)))
549 (setq tab-stop-list tabs
))
550 (message "Tab stops installed"))
552 (defun tab-to-tab-stop ()
553 "Insert spaces or tabs to next defined tab-stop column.
554 The variable `tab-stop-list' is a list of columns at which there are tab stops.
555 Use \\[edit-tab-stops] to edit them interactively."
557 (and abbrev-mode
(= (char-syntax (preceding-char)) ?w
)
559 (let ((tabs tab-stop-list
))
560 (while (and tabs
(>= (current-column) (car tabs
)))
561 (setq tabs
(cdr tabs
)))
563 (let ((opoint (point)))
564 (delete-horizontal-space t
)
565 (indent-to (car tabs
)))
568 (defun move-to-tab-stop ()
569 "Move point to next defined tab-stop column.
570 The variable `tab-stop-list' is a list of columns at which there are tab stops.
571 Use \\[edit-tab-stops] to edit them interactively."
573 (let ((tabs tab-stop-list
))
574 (while (and tabs
(>= (current-column) (car tabs
)))
575 (setq tabs
(cdr tabs
)))
577 (let ((before (point)))
578 (move-to-column (car tabs
) t
)
581 ;; If we just added a tab, or moved over one,
582 ;; delete any superfluous spaces before the old point.
583 (if (and (eq (preceding-char) ?\s
)
584 (eq (following-char) ?
\t))
585 (let ((tabend (* (/ (current-column) tab-width
) tab-width
)))
586 (while (and (> (current-column) tabend
)
587 (eq (preceding-char) ?\s
))
589 (delete-region (point) before
))))))))
591 (define-key global-map
"\t" 'indent-for-tab-command
)
592 (define-key esc-map
"\C-\\" 'indent-region
)
593 (define-key ctl-x-map
"\t" 'indent-rigidly
)
594 (define-key esc-map
"i" 'tab-to-tab-stop
)
596 ;; arch-tag: f402b2a7-e44f-492f-b5b8-38996020b7c3
597 ;;; indent.el ends here