1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985, 1995, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 \(f.ex. 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 Some programming language modes have their own variable to control this,
53 e.g., `c-tab-always-indent', and do not respect this variable."
55 :type
'(choice (const nil
) (const t
) (const always
)))
57 (defun indent-according-to-mode ()
58 "Indent line in proper way for current major mode.
59 The buffer-local variable `indent-line-function' determines how to do this,
60 but the functions `indent-relative' and `indent-relative-maybe' are
61 special; we don't actually use them here."
63 (if (memq indent-line-function
64 '(indent-relative indent-relative-maybe
))
65 ;; These functions are used for tabbing, but can't be used for
66 ;; indenting. Replace with something ad-hoc.
67 (let ((column (save-excursion
69 (skip-chars-backward "\n \t")
71 (current-indentation))))
72 (if (<= (current-column) (current-indentation))
73 (indent-line-to column
)
74 (save-excursion (indent-line-to column
))))
76 (funcall indent-line-function
)))
78 (defun indent-for-tab-command (&optional arg
)
79 "Indent line or region in proper way for current major mode or insert a tab.
80 Depending on `tab-always-indent', either insert a tab or indent.
81 If initial point was within line's indentation, position after
82 the indentation. Else stay at same point in text.
84 If a prefix argument is given, also rigidly indent the entire
85 balanced expression which starts at the beginning the current
86 line to reflect the current line's change in indentation.
88 If `transient-mark-mode' is turned on and the region is active,
89 indent the region (in this case, any prefix argument is ignored).
91 The function actually called to indent the line is determined by the value of
92 `indent-line-function'."
95 ;; The region is active, indent it.
96 ((and transient-mark-mode mark-active
97 (not (eq (region-beginning) (region-end))))
98 (indent-region (region-beginning) (region-end)))
99 ((or ;; indent-to-left-margin is only meant for indenting,
100 ;; so we force it to always insert a tab here.
101 (eq indent-line-function
'indent-to-left-margin
)
102 (and (not tab-always-indent
)
103 (or (> (current-column) (current-indentation))
104 (eq this-command last-command
))))
110 (forward-line 0) (forward-sexp) (point-marker))))
112 (current-indentation)))
115 (funcall indent-line-function
)
117 ;; If a prefix argument was given, rigidly indent the following
118 ;; sexp to match the change in the current line's indentation.
121 (let ((indentation-change (- (current-indentation) old-indent
)))
122 (unless (zerop indentation-change
)
125 (when (< (point) end-marker
)
126 (indent-rigidly (point) end-marker indentation-change
))))))))))
128 (defun insert-tab (&optional arg
)
129 (let ((count (prefix-numeric-value arg
)))
131 (eq (char-syntax (preceding-char)) ?w
))
134 (insert-char ?
\t count
)
135 (indent-to (* tab-width
(+ count
(/ (current-column) tab-width
)))))))
137 (defun indent-rigidly (start end arg
)
138 "Indent all lines starting in the region sideways by ARG columns.
139 Called from a program, takes three arguments, START, END and ARG.
140 You can remove all indentation from a region by giving a large negative ARG."
144 (setq end
(point-marker))
146 (or (bolp) (forward-line 1))
147 (while (< (point) end
)
148 (let ((indent (current-indentation))
151 (skip-chars-forward " \t")
152 (setq eol-flag
(eolp)))
154 (indent-to (max 0 (+ indent arg
)) 0))
155 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
157 (move-marker end nil
)))
159 (defun indent-line-to (column)
160 "Indent current line to COLUMN.
161 This function removes or adds spaces and tabs at beginning of line
162 only if necessary. It leaves point at end of indentation."
163 (back-to-indentation)
164 (let ((cur-col (current-column)))
165 (cond ((< cur-col column
)
166 (if (>= (- column
(* (/ cur-col tab-width
) tab-width
)) tab-width
)
167 (delete-region (point)
168 (progn (skip-chars-backward " ") (point))))
170 ((> cur-col column
) ; too far right (after tab?)
171 (delete-region (progn (move-to-column column t
) (point))
172 (progn (back-to-indentation) (point)))))))
174 (defun current-left-margin ()
175 "Return the left margin to use for this line.
176 This is the value of the buffer-local variable `left-margin' plus the value
177 of the `left-margin' text-property at the start of the line."
179 (back-to-indentation)
181 (+ left-margin
(or (get-text-property
182 (if (and (eobp) (not (bobp)))
183 (1- (point)) (point))
186 (defun move-to-left-margin (&optional n force
)
187 "Move to the left margin of the current line.
188 With optional argument, move forward N-1 lines first.
189 The column moved to is the one given by the `current-left-margin' function.
190 If the line's indentation appears to be wrong, and this command is called
191 interactively or with optional argument FORCE, it will be fixed."
192 (interactive (list (prefix-numeric-value current-prefix-arg
) t
))
193 (beginning-of-line n
)
194 (skip-chars-forward " \t")
195 (if (minibufferp (current-buffer))
196 (if (save-excursion (beginning-of-line) (bobp))
197 (goto-char (minibuffer-prompt-end))
199 (let ((lm (current-left-margin))
200 (cc (current-column)))
202 (if (> (move-to-column lm force
) lm
)
203 ;; If lm is in a tab and we are not forcing, move before tab
205 ((and force
(< cc lm
))
206 (indent-to-left-margin))))))
208 ;; This used to be the default indent-line-function,
209 ;; used in Fundamental Mode, Text Mode, etc.
210 (defun indent-to-left-margin ()
211 "Indent current line to the column given by `current-left-margin'."
212 (save-excursion (indent-line-to (current-left-margin)))
213 ;; If we are within the indentation, move past it.
214 (when (save-excursion
215 (skip-chars-backward " \t")
217 (skip-chars-forward " \t")))
219 (defun delete-to-left-margin (&optional from to
)
220 "Remove left margin indentation from a region.
221 This deletes to the column given by `current-left-margin'.
222 In no case will it delete non-whitespace.
223 Args FROM and TO are optional; default is the whole buffer."
225 (goto-char (or to
(point-max)))
226 (setq to
(point-marker))
227 (goto-char (or from
(point-min)))
228 (or (bolp) (forward-line 1))
229 (while (< (point) to
)
230 (delete-region (point) (progn (move-to-left-margin nil t
) (point)))
232 (move-marker to nil
)))
234 (defun set-left-margin (from to width
)
235 "Set the left margin of the region to WIDTH.
236 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
238 Interactively, WIDTH is the prefix argument, if specified.
239 Without prefix argument, the command prompts for WIDTH."
240 (interactive "r\nNSet left margin to column: ")
242 ;; If inside indentation, start from BOL.
244 (skip-chars-backward " \t")
245 (if (bolp) (setq from
(point)))
246 ;; Place end after whitespace
248 (skip-chars-forward " \t")
249 (setq to
(point-marker)))
250 ;; Delete margin indentation first, but keep paragraph indentation.
251 (delete-to-left-margin from to
)
252 (put-text-property from to
'left-margin width
)
253 (indent-rigidly from to width
)
254 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
255 (move-marker to nil
))
257 (defun set-right-margin (from to width
)
258 "Set the right margin of the region to WIDTH.
259 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
261 Interactively, WIDTH is the prefix argument, if specified.
262 Without prefix argument, the command prompts for WIDTH."
263 (interactive "r\nNSet right margin to width: ")
266 (skip-chars-backward " \t")
267 (if (bolp) (setq from
(point))))
268 (put-text-property from to
'right-margin width
)
269 (if auto-fill-function
(save-excursion (fill-region from to nil t t
))))
271 (defun alter-text-property (from to prop func
&optional object
)
272 "Programmatically change value of a text-property.
273 For each region between FROM and TO that has a single value for PROPERTY,
274 apply FUNCTION to that value and sets the property to the function's result.
275 Optional fifth argument OBJECT specifies the string or buffer to operate on."
278 (while (setq val
(get-text-property begin prop object
)
279 end
(text-property-not-all begin to prop val object
))
280 (put-text-property begin end prop
(funcall func val
) object
)
283 (put-text-property begin to prop
(funcall func val
) object
))))
285 (defun increase-left-margin (from to inc
)
286 "Increase or decrease the left-margin of the region.
287 With no prefix argument, this adds `standard-indent' of indentation.
288 A prefix arg (optional third arg INC noninteractively) specifies the amount
289 to change the margin by, in characters.
290 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
291 (interactive "*r\nP")
292 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
295 (skip-chars-backward " \t")
296 (if (bolp) (setq from
(point)))
298 (setq to
(point-marker)))
299 (alter-text-property from to
'left-margin
300 (lambda (v) (max (- left-margin
) (+ inc
(or v
0)))))
301 (indent-rigidly from to inc
)
302 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
303 (move-marker to nil
))
305 (defun decrease-left-margin (from to inc
)
306 "Make the left margin of the region smaller.
307 With no prefix argument, decrease the indentation by `standard-indent'.
308 A prefix arg (optional third arg INC noninteractively) specifies the amount
309 to change the margin by, in characters.
310 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
311 (interactive "*r\nP")
312 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
313 (increase-left-margin from to
(- inc
)))
315 (defun increase-right-margin (from to inc
)
316 "Increase the right-margin of the region.
317 With no prefix argument, increase the right margin by `standard-indent'.
318 A prefix arg (optional third arg INC noninteractively) specifies the amount
319 to change the margin by, in characters. A negative argument decreases
320 the right margin width.
321 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
323 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
325 (alter-text-property from to
'right-margin
326 (lambda (v) (+ inc
(or v
0))))
327 (if auto-fill-function
328 (fill-region from to nil t t
))))
330 (defun decrease-right-margin (from to inc
)
331 "Make the right margin of the region smaller.
332 With no prefix argument, decrease the right margin by `standard-indent'.
333 A prefix arg (optional third arg INC noninteractively) specifies the amount
334 of width to remove, in characters. A negative argument increases
335 the right margin width.
336 If `auto-fill-mode' is active, re-fills region to fit in new margin."
337 (interactive "*r\nP")
338 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
339 (increase-right-margin from to
(- inc
)))
341 (defun beginning-of-line-text (&optional n
)
342 "Move to the beginning of the text on this line.
343 With optional argument, move forward N-1 lines first.
344 From the beginning of the line, moves past the left-margin indentation, the
345 fill-prefix, and any indentation used for centering or right-justifying the
346 line, but does not move past any whitespace that was explicitly inserted
347 \(such as a tab used to indent the first line of a paragraph)."
349 (beginning-of-line n
)
350 (skip-chars-forward " \t")
351 ;; Skip over fill-prefix.
353 (not (string-equal fill-prefix
"")))
354 (if (equal fill-prefix
356 (point) (min (point-max) (+ (length fill-prefix
) (point)))))
357 (forward-char (length fill-prefix
)))
358 (if (and adaptive-fill-mode adaptive-fill-regexp
359 (looking-at adaptive-fill-regexp
))
360 (goto-char (match-end 0))))
361 ;; Skip centering or flushright indentation
362 (if (memq (current-justification) '(center right
))
363 (skip-chars-forward " \t")))
365 (defvar indent-region-function nil
366 "Short cut function to indent region using `indent-according-to-mode'.
367 A value of nil means really run `indent-according-to-mode' on each line.")
369 (defun indent-region (start end
&optional column
)
370 "Indent each nonblank line in the region.
371 A numeric prefix argument specifies a column: indent each line to that column.
373 With no prefix argument, the command chooses one of these methods and
374 indents all the lines with it:
376 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
377 beginning of each line in the region that does not already begin
379 2) If `indent-region-function' is non-nil, call that function
380 to indent the region.
381 3) Indent each line as specified by the variable `indent-line-function'.
383 Called from a program, START and END specify the region to indent.
384 If the third argument COLUMN is an integer, it specifies the
385 column to indent to; if it is nil, use one of the three methods above."
391 (setq end
(point-marker))
393 (let ((regexp (regexp-quote fill-prefix
)))
394 (while (< (point) end
)
395 (or (looking-at regexp
)
397 (insert fill-prefix
))
399 (if indent-region-function
400 (funcall indent-region-function start end
)
402 (setq end
(copy-marker end
))
404 (while (< (point) end
)
405 (or (and (bolp) (eolp))
406 (funcall indent-line-function
))
408 (move-marker end nil
))))
409 (setq column
(prefix-numeric-value column
))
412 (setq end
(point-marker))
414 (or (bolp) (forward-line 1))
415 (while (< (point) end
)
416 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
418 (indent-to column
0))
420 (move-marker end nil
))))
422 (defun indent-relative-maybe ()
423 "Indent a new line like previous nonblank line.
424 If the previous nonblank line has no indent points beyond the
425 column point starts at, this command does nothing.
427 See also `indent-relative'."
431 (defun indent-relative (&optional unindented-ok
)
432 "Space out to under next indent point in previous nonblank line.
433 An indent point is a non-whitespace character following whitespace.
434 The following line shows the indentation points in this line.
436 If the previous nonblank line has no indent points beyond the
437 column point starts at, `tab-to-tab-stop' is done instead, unless
438 this command is invoked with a numeric argument, in which case it
441 See also `indent-relative-maybe'."
444 (eq (char-syntax (preceding-char)) ?w
))
446 (let ((start-column (current-column))
450 (if (re-search-backward "^[^\n]" nil t
)
451 (let ((end (save-excursion (forward-line 1) (point))))
452 (move-to-column start-column
)
453 ;; Is start-column inside a tab on this line?
454 (if (> (current-column) start-column
)
456 (or (looking-at "[ \t]")
458 (skip-chars-forward "^ \t" end
))
459 (skip-chars-forward " \t" end
)
460 (or (= (point) end
) (setq indent
(current-column))))))
462 (let ((opoint (point-marker)))
464 (if (> opoint
(point))
466 (move-marker opoint nil
))
469 (defcustom tab-stop-list
470 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
471 "*List of tab stop positions used by `tab-to-tab-stop'.
472 This should be a list of integers, ordered from smallest to largest."
474 :type
'(repeat integer
))
475 (put 'tab-stop-list
'safe-local-variable
'listp
)
477 (defvar edit-tab-stops-map
478 (let ((map (make-sparse-keymap)))
479 (define-key map
"\C-x\C-s" 'edit-tab-stops-note-changes
)
480 (define-key map
"\C-c\C-c" 'edit-tab-stops-note-changes
)
482 "Keymap used in `edit-tab-stops'.")
484 (defvar edit-tab-stops-buffer nil
485 "Buffer whose tab stops are being edited.
486 This matters if the variable `tab-stop-list' is local in that buffer.")
488 (defun edit-tab-stops ()
489 "Edit the tab stops used by `tab-to-tab-stop'.
490 Creates a buffer *Tab Stops* containing text describing the tab stops.
491 A colon indicates a column where there is a tab stop.
492 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
494 (setq edit-tab-stops-buffer
(current-buffer))
495 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
496 (use-local-map edit-tab-stops-map
)
497 (make-local-variable 'indent-tabs-mode
)
498 (setq indent-tabs-mode nil
)
500 (setq truncate-lines t
)
502 (let ((tabs tab-stop-list
))
504 (indent-to (car tabs
) 0)
506 (setq tabs
(cdr tabs
))))
510 (insert (+ count ?
0))
512 (setq count
(1+ count
)))
515 (insert "0123456789")
516 (setq count
(1- count
))))
517 (insert "\nTo install changes, type C-c C-c")
518 (goto-char (point-min)))
520 (defun edit-tab-stops-note-changes ()
521 "Put edited tab stops into effect."
527 (while (search-backward ":" nil t
)
528 (setq tabs
(cons (current-column) tabs
))))
529 (bury-buffer (prog1 (current-buffer)
530 (switch-to-buffer edit-tab-stops-buffer
)))
531 (setq tab-stop-list tabs
))
532 (message "Tab stops installed"))
534 (defun tab-to-tab-stop ()
535 "Insert spaces or tabs to next defined tab-stop column.
536 The variable `tab-stop-list' is a list of columns at which there are tab stops.
537 Use \\[edit-tab-stops] to edit them interactively."
539 (and abbrev-mode
(= (char-syntax (preceding-char)) ?w
)
541 (let ((tabs tab-stop-list
))
542 (while (and tabs
(>= (current-column) (car tabs
)))
543 (setq tabs
(cdr tabs
)))
545 (let ((opoint (point)))
546 (delete-horizontal-space t
)
547 (indent-to (car tabs
)))
550 (defun move-to-tab-stop ()
551 "Move point to next defined tab-stop column.
552 The variable `tab-stop-list' is a list of columns at which there are tab stops.
553 Use \\[edit-tab-stops] to edit them interactively."
555 (let ((tabs tab-stop-list
))
556 (while (and tabs
(>= (current-column) (car tabs
)))
557 (setq tabs
(cdr tabs
)))
559 (let ((before (point)))
560 (move-to-column (car tabs
) t
)
563 ;; If we just added a tab, or moved over one,
564 ;; delete any superfluous spaces before the old point.
565 (if (and (eq (preceding-char) ?\s
)
566 (eq (following-char) ?
\t))
567 (let ((tabend (* (/ (current-column) tab-width
) tab-width
)))
568 (while (and (> (current-column) tabend
)
569 (eq (preceding-char) ?\s
))
571 (delete-region (point) before
))))))))
573 (define-key global-map
"\t" 'indent-for-tab-command
)
574 (define-key esc-map
"\C-\\" 'indent-region
)
575 (define-key ctl-x-map
"\t" 'indent-rigidly
)
576 (define-key esc-map
"i" 'tab-to-tab-stop
)
578 ;; arch-tag: f402b2a7-e44f-492f-b5b8-38996020b7c3
579 ;;; indent.el ends here