1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985, 1995, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; Commands for making and changing indentation in text. These are
28 ;; described in the Emacs manual.
33 "Indentation commands."
36 (defcustom standard-indent
4
37 "*Default number of columns for margin-changing functions to indent."
41 (defvar indent-line-function
'indent-relative
42 "Function to indent the current line.
43 This function will be called with no arguments.
44 If it is called somewhere where auto-indentation cannot be done
45 \(f.ex. inside a string), the function should simply return `noindent'.
46 Setting this function is all you need to make TAB indent appropriately.
47 Don't rebind TAB unless you really need to.")
49 (defcustom tab-always-indent t
50 "*Controls the operation of the TAB key.
51 If t, hitting TAB always just indents the current line.
52 If nil, hitting TAB indents the current line if point is at the left margin
53 or in the line's indentation, otherwise it inserts a \"real\" TAB character.
54 Some programming language modes have their own variable to control this,
55 e.g., `c-tab-always-indent', and do not respect this variable."
57 :type
'(choice (const nil
) (const t
) (const always
)))
59 (defun indent-according-to-mode ()
60 "Indent line in proper way for current major mode.
61 The buffer-local variable `indent-line-function' determines how to do this,
62 but the functions `indent-relative' and `indent-relative-maybe' are
63 special; we don't actually use them here."
65 (if (memq indent-line-function
66 '(indent-relative indent-relative-maybe
))
67 ;; These functions are used for tabbing, but can't be used for
68 ;; indenting. Replace with something ad-hoc.
69 (let ((column (save-excursion
71 (skip-chars-backward "\n \t")
73 (current-indentation))))
74 (if (<= (current-column) (current-indentation))
75 (indent-line-to column
)
76 (save-excursion (indent-line-to column
))))
78 (funcall indent-line-function
)))
80 (defun indent-for-tab-command (&optional arg
)
81 "Indent line or region in proper way for current major mode or insert a tab.
82 Depending on `tab-always-indent', either insert a tab or indent.
83 If initial point was within line's indentation, position after
84 the indentation. Else stay at same point in text.
85 If `transient-mark-mode' is turned on the region is active,
87 The function actually called to indent the line is determined by the value of
88 `indent-line-function'."
91 ;; The region is active, indent it.
92 ((and arg transient-mark-mode mark-active
93 (not (eq (region-beginning) (region-end))))
94 (indent-region (region-beginning) (region-end)))
95 ((or ;; indent-to-left-margin is only meant for indenting,
96 ;; so we force it to always insert a tab here.
97 (eq indent-line-function
'indent-to-left-margin
)
98 (and (not tab-always-indent
)
99 (or (> (current-column) (current-indentation))
100 (eq this-command last-command
))))
102 ;; Those functions are meant specifically for tabbing and not for
103 ;; indenting, so we can't pass them to indent-according-to-mode.
104 ((memq indent-line-function
'(indent-relative indent-relative-maybe
))
105 (funcall indent-line-function
))
108 (indent-according-to-mode))))
110 (defun insert-tab (&optional arg
)
111 (let ((count (prefix-numeric-value arg
)))
113 (eq (char-syntax (preceding-char)) ?w
))
116 (insert-char ?
\t count
)
117 (indent-to (* tab-width
(+ count
(/ (current-column) tab-width
)))))))
119 (defun indent-rigidly (start end arg
)
120 "Indent all lines starting in the region sideways by ARG columns.
121 Called from a program, takes three arguments, START, END and ARG.
122 You can remove all indentation from a region by giving a large negative ARG."
126 (setq end
(point-marker))
128 (or (bolp) (forward-line 1))
129 (while (< (point) end
)
130 (let ((indent (current-indentation))
133 (skip-chars-forward " \t")
134 (setq eol-flag
(eolp)))
136 (indent-to (max 0 (+ indent arg
)) 0))
137 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
139 (move-marker end nil
)))
141 (defun indent-line-to (column)
142 "Indent current line to COLUMN.
143 This function removes or adds spaces and tabs at beginning of line
144 only if necessary. It leaves point at end of indentation."
145 (back-to-indentation)
146 (let ((cur-col (current-column)))
147 (cond ((< cur-col column
)
148 (if (>= (- column
(* (/ cur-col tab-width
) tab-width
)) tab-width
)
149 (delete-region (point)
150 (progn (skip-chars-backward " ") (point))))
152 ((> cur-col column
) ; too far right (after tab?)
153 (delete-region (progn (move-to-column column t
) (point))
154 (progn (back-to-indentation) (point)))))))
156 (defun current-left-margin ()
157 "Return the left margin to use for this line.
158 This is the value of the buffer-local variable `left-margin' plus the value
159 of the `left-margin' text-property at the start of the line."
161 (back-to-indentation)
163 (+ left-margin
(or (get-text-property
164 (if (and (eobp) (not (bobp)))
165 (1- (point)) (point))
168 (defun move-to-left-margin (&optional n force
)
169 "Move to the left margin of the current line.
170 With optional argument, move forward N-1 lines first.
171 The column moved to is the one given by the `current-left-margin' function.
172 If the line's indentation appears to be wrong, and this command is called
173 interactively or with optional argument FORCE, it will be fixed."
174 (interactive (list (prefix-numeric-value current-prefix-arg
) t
))
175 (beginning-of-line n
)
176 (skip-chars-forward " \t")
177 (if (minibufferp (current-buffer))
178 (if (save-excursion (beginning-of-line) (bobp))
179 (goto-char (minibuffer-prompt-end))
181 (let ((lm (current-left-margin))
182 (cc (current-column)))
184 (if (> (move-to-column lm force
) lm
)
185 ;; If lm is in a tab and we are not forcing, move before tab
187 ((and force
(< cc lm
))
188 (indent-to-left-margin))))))
190 ;; This used to be the default indent-line-function,
191 ;; used in Fundamental Mode, Text Mode, etc.
192 (defun indent-to-left-margin ()
193 "Indent current line to the column given by `current-left-margin'."
194 (save-excursion (indent-line-to (current-left-margin)))
195 ;; If we are within the indentation, move past it.
196 (when (save-excursion
197 (skip-chars-backward " \t")
199 (skip-chars-forward " \t")))
201 (defun delete-to-left-margin (&optional from to
)
202 "Remove left margin indentation from a region.
203 This deletes to the column given by `current-left-margin'.
204 In no case will it delete non-whitespace.
205 Args FROM and TO are optional; default is the whole buffer."
207 (goto-char (or to
(point-max)))
208 (setq to
(point-marker))
209 (goto-char (or from
(point-min)))
210 (or (bolp) (forward-line 1))
211 (while (< (point) to
)
212 (delete-region (point) (progn (move-to-left-margin nil t
) (point)))
214 (move-marker to nil
)))
216 (defun set-left-margin (from to width
)
217 "Set the left margin of the region to WIDTH.
218 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
220 Interactively, WIDTH is the prefix argument, if specified.
221 Without prefix argument, the command prompts for WIDTH."
222 (interactive "r\nNSet left margin to column: ")
224 ;; If inside indentation, start from BOL.
226 (skip-chars-backward " \t")
227 (if (bolp) (setq from
(point)))
228 ;; Place end after whitespace
230 (skip-chars-forward " \t")
231 (setq to
(point-marker)))
232 ;; Delete margin indentation first, but keep paragraph indentation.
233 (delete-to-left-margin from to
)
234 (put-text-property from to
'left-margin width
)
235 (indent-rigidly from to width
)
236 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
237 (move-marker to nil
))
239 (defun set-right-margin (from to width
)
240 "Set the right margin of the region to WIDTH.
241 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
243 Interactively, WIDTH is the prefix argument, if specified.
244 Without prefix argument, the command prompts for WIDTH."
245 (interactive "r\nNSet right margin to width: ")
248 (skip-chars-backward " \t")
249 (if (bolp) (setq from
(point))))
250 (put-text-property from to
'right-margin width
)
251 (if auto-fill-function
(save-excursion (fill-region from to nil t t
))))
253 (defun alter-text-property (from to prop func
&optional object
)
254 "Programmatically change value of a text-property.
255 For each region between FROM and TO that has a single value for PROPERTY,
256 apply FUNCTION to that value and sets the property to the function's result.
257 Optional fifth argument OBJECT specifies the string or buffer to operate on."
260 (while (setq val
(get-text-property begin prop object
)
261 end
(text-property-not-all begin to prop val object
))
262 (put-text-property begin end prop
(funcall func val
) object
)
265 (put-text-property begin to prop
(funcall func val
) object
))))
267 (defun increase-left-margin (from to inc
)
268 "Increase or decrease the left-margin of the region.
269 With no prefix argument, this adds `standard-indent' of indentation.
270 A prefix arg (optional third arg INC noninteractively) specifies the amount
271 to change the margin by, in characters.
272 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
273 (interactive "*r\nP")
274 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
277 (skip-chars-backward " \t")
278 (if (bolp) (setq from
(point)))
280 (setq to
(point-marker)))
281 (alter-text-property from to
'left-margin
282 (lambda (v) (max (- left-margin
) (+ inc
(or v
0)))))
283 (indent-rigidly from to inc
)
284 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
285 (move-marker to nil
))
287 (defun decrease-left-margin (from to inc
)
288 "Make the left margin of the region smaller.
289 With no prefix argument, decrease the indentation by `standard-indent'.
290 A prefix arg (optional third arg INC noninteractively) specifies the amount
291 to change the margin by, in characters.
292 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
293 (interactive "*r\nP")
294 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
295 (increase-left-margin from to
(- inc
)))
297 (defun increase-right-margin (from to inc
)
298 "Increase the right-margin of the region.
299 With no prefix argument, increase the right margin by `standard-indent'.
300 A prefix arg (optional third arg INC noninteractively) specifies the amount
301 to change the margin by, in characters. A negative argument decreases
302 the right margin width.
303 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
305 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
307 (alter-text-property from to
'right-margin
308 (lambda (v) (+ inc
(or v
0))))
309 (if auto-fill-function
310 (fill-region from to nil t t
))))
312 (defun decrease-right-margin (from to inc
)
313 "Make the right margin of the region smaller.
314 With no prefix argument, decrease the right margin by `standard-indent'.
315 A prefix arg (optional third arg INC noninteractively) specifies the amount
316 of width to remove, in characters. A negative argument increases
317 the right margin width.
318 If `auto-fill-mode' is active, re-fills region to fit in new margin."
319 (interactive "*r\nP")
320 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
321 (increase-right-margin from to
(- inc
)))
323 (defun beginning-of-line-text (&optional n
)
324 "Move to the beginning of the text on this line.
325 With optional argument, move forward N-1 lines first.
326 From the beginning of the line, moves past the left-margin indentation, the
327 fill-prefix, and any indentation used for centering or right-justifying the
328 line, but does not move past any whitespace that was explicitly inserted
329 \(such as a tab used to indent the first line of a paragraph)."
331 (beginning-of-line n
)
332 (skip-chars-forward " \t")
333 ;; Skip over fill-prefix.
335 (not (string-equal fill-prefix
"")))
336 (if (equal fill-prefix
338 (point) (min (point-max) (+ (length fill-prefix
) (point)))))
339 (forward-char (length fill-prefix
)))
340 (if (and adaptive-fill-mode adaptive-fill-regexp
341 (looking-at adaptive-fill-regexp
))
342 (goto-char (match-end 0))))
343 ;; Skip centering or flushright indentation
344 (if (memq (current-justification) '(center right
))
345 (skip-chars-forward " \t")))
347 (defvar indent-region-function nil
348 "Short cut function to indent region using `indent-according-to-mode'.
349 A value of nil means really run `indent-according-to-mode' on each line.")
351 (defun indent-region (start end
&optional column
)
352 "Indent each nonblank line in the region.
353 A numeric prefix argument specifies a column: indent each line to that column.
355 With no prefix argument, the command chooses one of these methods and
356 indents all the lines with it:
358 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
359 beginning of each line in the region that does not already begin
361 2) If `indent-region-function' is non-nil, call that function
362 to indent the region.
363 3) Indent each line as specified by the variable `indent-line-function'.
365 Called from a program, START and END specify the region to indent.
366 If the third argument COLUMN is an integer, it specifies the
367 column to indent to; if it is nil, use one of the three methods above."
373 (setq end
(point-marker))
375 (let ((regexp (regexp-quote fill-prefix
)))
376 (while (< (point) end
)
377 (or (looking-at regexp
)
379 (insert fill-prefix
))
381 (if indent-region-function
382 (funcall indent-region-function start end
)
384 (setq end
(copy-marker end
))
386 (while (< (point) end
)
387 (or (and (bolp) (eolp))
388 (funcall indent-line-function
))
390 (move-marker end nil
))))
391 (setq column
(prefix-numeric-value column
))
394 (setq end
(point-marker))
396 (or (bolp) (forward-line 1))
397 (while (< (point) end
)
398 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
400 (indent-to column
0))
402 (move-marker end nil
))))
404 (defun indent-relative-maybe ()
405 "Indent a new line like previous nonblank line.
406 If the previous nonblank line has no indent points beyond the
407 column point starts at, this command does nothing.
409 See also `indent-relative'."
413 (defun indent-relative (&optional unindented-ok
)
414 "Space out to under next indent point in previous nonblank line.
415 An indent point is a non-whitespace character following whitespace.
416 The following line shows the indentation points in this line.
418 If the previous nonblank line has no indent points beyond the
419 column point starts at, `tab-to-tab-stop' is done instead, unless
420 this command is invoked with a numeric argument, in which case it
423 See also `indent-relative-maybe'."
426 (eq (char-syntax (preceding-char)) ?w
))
428 (let ((start-column (current-column))
432 (if (re-search-backward "^[^\n]" nil t
)
433 (let ((end (save-excursion (forward-line 1) (point))))
434 (move-to-column start-column
)
435 ;; Is start-column inside a tab on this line?
436 (if (> (current-column) start-column
)
438 (or (looking-at "[ \t]")
440 (skip-chars-forward "^ \t" end
))
441 (skip-chars-forward " \t" end
)
442 (or (= (point) end
) (setq indent
(current-column))))))
444 (let ((opoint (point-marker)))
446 (if (> opoint
(point))
448 (move-marker opoint nil
))
451 (defcustom tab-stop-list
452 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
453 "*List of tab stop positions used by `tab-to-tab-stop'.
454 This should be a list of integers, ordered from smallest to largest."
456 :type
'(repeat integer
))
458 (defvar edit-tab-stops-map
459 (let ((map (make-sparse-keymap)))
460 (define-key map
"\C-x\C-s" 'edit-tab-stops-note-changes
)
461 (define-key map
"\C-c\C-c" 'edit-tab-stops-note-changes
)
463 "Keymap used in `edit-tab-stops'.")
465 (defvar edit-tab-stops-buffer nil
466 "Buffer whose tab stops are being edited.
467 This matters if the variable `tab-stop-list' is local in that buffer.")
469 (defun edit-tab-stops ()
470 "Edit the tab stops used by `tab-to-tab-stop'.
471 Creates a buffer *Tab Stops* containing text describing the tab stops.
472 A colon indicates a column where there is a tab stop.
473 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
475 (setq edit-tab-stops-buffer
(current-buffer))
476 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
477 (use-local-map edit-tab-stops-map
)
478 (make-local-variable 'indent-tabs-mode
)
479 (setq indent-tabs-mode nil
)
481 (setq truncate-lines t
)
483 (let ((tabs tab-stop-list
))
485 (indent-to (car tabs
) 0)
487 (setq tabs
(cdr tabs
))))
491 (insert (+ count ?
0))
493 (setq count
(1+ count
)))
496 (insert "0123456789")
497 (setq count
(1- count
))))
498 (insert "\nTo install changes, type C-c C-c")
499 (goto-char (point-min)))
501 (defun edit-tab-stops-note-changes ()
502 "Put edited tab stops into effect."
508 (while (search-backward ":" nil t
)
509 (setq tabs
(cons (current-column) tabs
))))
510 (bury-buffer (prog1 (current-buffer)
511 (switch-to-buffer edit-tab-stops-buffer
)))
512 (setq tab-stop-list tabs
))
513 (message "Tab stops installed"))
515 (defun tab-to-tab-stop ()
516 "Insert spaces or tabs to next defined tab-stop column.
517 The variable `tab-stop-list' is a list of columns at which there are tab stops.
518 Use \\[edit-tab-stops] to edit them interactively."
520 (and abbrev-mode
(= (char-syntax (preceding-char)) ?w
)
522 (let ((tabs tab-stop-list
))
523 (while (and tabs
(>= (current-column) (car tabs
)))
524 (setq tabs
(cdr tabs
)))
526 (let ((opoint (point)))
527 (delete-horizontal-space t
)
528 (indent-to (car tabs
)))
531 (defun move-to-tab-stop ()
532 "Move point to next defined tab-stop column.
533 The variable `tab-stop-list' is a list of columns at which there are tab stops.
534 Use \\[edit-tab-stops] to edit them interactively."
536 (let ((tabs tab-stop-list
))
537 (while (and tabs
(>= (current-column) (car tabs
)))
538 (setq tabs
(cdr tabs
)))
540 (let ((before (point)))
541 (move-to-column (car tabs
) t
)
544 ;; If we just added a tab, or moved over one,
545 ;; delete any superfluous spaces before the old point.
546 (if (and (eq (preceding-char) ?\s
)
547 (eq (following-char) ?
\t))
548 (let ((tabend (* (/ (current-column) tab-width
) tab-width
)))
549 (while (and (> (current-column) tabend
)
550 (eq (preceding-char) ?\s
))
552 (delete-region (point) before
))))))))
554 (define-key global-map
"\t" 'indent-for-tab-command
)
555 (define-key esc-map
"\C-\\" 'indent-region
)
556 (define-key ctl-x-map
"\t" 'indent-rigidly
)
557 (define-key esc-map
"i" 'tab-to-tab-stop
)
559 ;;; arch-tag: f402b2a7-e44f-492f-b5b8-38996020b7c3
560 ;;; indent.el ends here