1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985, 1995 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; Commands for making and changing indentation in text. These are
27 ;; described in the Emacs manual.
32 "Indentation commands"
35 (defcustom standard-indent
4
36 "*Default number of columns for margin-changing functions to indent."
40 (defvar indent-line-function
'indent-to-left-margin
41 "Function to indent current line.")
43 (defcustom tab-always-indent t
44 "*Controls the operation of the TAB key.
45 If t, hitting TAB always just indents the current line.
46 If nil, hitting TAB indents the current line if point is at the left margin
47 or in the line's indentation, otherwise it insert a `real' tab character."
51 (defun indent-according-to-mode ()
52 "Indent line in proper way for current major mode."
54 (funcall indent-line-function
))
56 (defun indent-for-tab-command (&optional prefix-arg
)
57 "Indent line in proper way for current major mode or insert a tab.
58 Depending on `tab-always-indent', either insert a tab or indent.
59 If initial point was within line's indentation, position after
60 the indentation. Else stay at same point in text.
61 The function actually called to indent is determined by the value of
62 `indent-line-function'."
64 (if (or (eq indent-line-function
'indent-to-left-margin
)
65 (and (not tab-always-indent
)
66 (> (current-column) (current-indentation))))
67 (insert-tab prefix-arg
)
69 (funcall indent-line-function prefix-arg
)
70 (funcall indent-line-function
))))
72 (defun insert-tab (&optional prefix-arg
)
73 (let ((count (prefix-numeric-value prefix-arg
)))
75 (eq (char-syntax (preceding-char)) ?w
))
78 (insert-char ?
\t count
)
79 (indent-to (* tab-width
(+ count
(/ (current-column) tab-width
)))))))
81 (defun indent-rigidly (start end arg
)
82 "Indent all lines starting in the region sideways by ARG columns.
83 Called from a program, takes three arguments, START, END and ARG."
87 (setq end
(point-marker))
89 (or (bolp) (forward-line 1))
90 (while (< (point) end
)
91 (let ((indent (current-indentation))
94 (skip-chars-forward " \t")
95 (setq eol-flag
(eolp)))
97 (indent-to (max 0 (+ indent arg
)) 0))
98 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
100 (move-marker end nil
)))
102 (defun indent-line-to (column)
103 "Indent current line to COLUMN.
104 This function removes or adds spaces and tabs at beginning of line
105 only if necessary. It leaves point at end of indentation."
106 (back-to-indentation)
107 (let ((cur-col (current-column)))
108 (cond ((< cur-col column
)
109 (if (>= (- column
(* (/ cur-col tab-width
) tab-width
)) tab-width
)
110 (delete-region (point)
111 (progn (skip-chars-backward " ") (point))))
113 ((> cur-col column
) ; too far right (after tab?)
114 (delete-region (progn (move-to-column column t
) (point))
115 (progn (back-to-indentation) (point)))))))
117 (defun current-left-margin ()
118 "Return the left margin to use for this line.
119 This is the value of the buffer-local variable `left-margin' plus the value
120 of the `left-margin' text-property at the start of the line."
122 (back-to-indentation)
124 (+ left-margin
(or (get-text-property
125 (if (and (eobp) (not (bobp)))
126 (1- (point)) (point))
129 (defun move-to-left-margin (&optional n force
)
130 "Move to the left margin of the current line.
131 With optional argument, move forward N-1 lines first.
132 The column moved to is the one given by the `current-left-margin' function.
133 If the line's indentation appears to be wrong, and this command is called
134 interactively or with optional argument FORCE, it will be fixed."
135 (interactive (list (prefix-numeric-value current-prefix-arg
) t
))
136 (beginning-of-line n
)
137 (skip-chars-forward " \t")
138 (let ((lm (current-left-margin))
139 (cc (current-column)))
141 (if (> (move-to-column lm force
) lm
)
142 ;; If lm is in a tab and we are not forcing, move before tab
144 ((and force
(< cc lm
))
145 (indent-to-left-margin)))))
147 ;; This is the default indent-line-function,
148 ;; used in Fundamental Mode, Text Mode, etc.
149 (defun indent-to-left-margin ()
150 "Indent current line to the column given by `current-left-margin'."
151 (indent-line-to (current-left-margin)))
153 (defun delete-to-left-margin (&optional from to
)
154 "Remove left margin indentation from a region.
155 This deletes to the column given by `current-left-margin'.
156 In no case will it delete non-whitespace.
157 Args FROM and TO are optional; default is the whole buffer."
159 (goto-char (or to
(point-max)))
160 (setq to
(point-marker))
161 (goto-char (or from
(point-min)))
162 (or (bolp) (forward-line 1))
163 (while (< (point) to
)
164 (delete-region (point) (progn (move-to-left-margin nil t
) (point)))
166 (move-marker to nil
)))
168 (defun set-left-margin (from to lm
)
169 "Set the left margin of the region to WIDTH.
170 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
171 (interactive "r\nNSet left margin to column: ")
172 (if (interactive-p) (setq lm
(prefix-numeric-value lm
)))
174 ;; If inside indentation, start from BOL.
176 (skip-chars-backward " \t")
177 (if (bolp) (setq from
(point)))
178 ;; Place end after whitespace
180 (skip-chars-forward " \t")
181 (setq to
(point-marker)))
182 ;; Delete margin indentation first, but keep paragraph indentation.
183 (delete-to-left-margin from to
)
184 (put-text-property from to
'left-margin lm
)
185 (indent-rigidly from to lm
)
186 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
187 (move-marker to nil
))
189 (defun set-right-margin (from to lm
)
190 "Set the right margin of the region to WIDTH.
191 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
192 (interactive "r\nNSet right margin to width: ")
193 (if (interactive-p) (setq lm
(prefix-numeric-value lm
)))
196 (skip-chars-backward " \t")
197 (if (bolp) (setq from
(point))))
198 (put-text-property from to
'right-margin lm
)
199 (if auto-fill-function
(save-excursion (fill-region from to nil t t
))))
201 (defun alter-text-property (from to prop func
&optional object
)
202 "Programmatically change value of a text-property.
203 For each region between FROM and TO that has a single value for PROPERTY,
204 apply FUNCTION to that value and sets the property to the function's result.
205 Optional fifth argument OBJECT specifies the string or buffer to operate on."
208 (while (setq val
(get-text-property begin prop object
)
209 end
(text-property-not-all begin to prop val object
))
210 (put-text-property begin end prop
(funcall func val
) object
)
213 (put-text-property begin to prop
(funcall func val
) object
))))
215 (defun increase-left-margin (from to inc
)
216 "Increase or decrease the left-margin of the region.
217 With no prefix argument, this adds `standard-indent' of indentation.
218 A prefix arg (optional third arg INC noninteractively) specifies the amount
219 to change the margin by, in characters.
220 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
221 (interactive "*r\nP")
222 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
225 (skip-chars-backward " \t")
226 (if (bolp) (setq from
(point)))
228 (setq to
(point-marker)))
229 (alter-text-property from to
'left-margin
230 (lambda (v) (max (- left-margin
) (+ inc
(or v
0)))))
231 (indent-rigidly from to inc
)
232 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
233 (move-marker to nil
))
235 (defun decrease-left-margin (from to inc
)
236 "Make the left margin of the region smaller.
237 With no prefix argument, decrease the indentation by `standard-indent'.
238 A prefix arg (optional third arg INC noninteractively) specifies the amount
239 to change the margin by, in characters.
240 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
241 (interactive "*r\nP")
242 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
243 (increase-left-margin from to
(- inc
)))
245 (defun increase-right-margin (from to inc
)
246 "Increase the right-margin of the region.
247 With no prefix argument, increase the right margin by `standard-indent'.
248 A prefix arg (optional third arg INC noninteractively) specifies the amount
249 to change the margin by, in characters. A negative argument decreases
250 the right margin width.
251 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
254 (setq inc
(if inc
(prefix-numeric-value current-prefix-arg
)
257 (alter-text-property from to
'right-margin
258 (lambda (v) (+ inc
(or v
0))))
259 (if auto-fill-function
260 (fill-region from to nil t t
))))
262 (defun decrease-right-margin (from to inc
)
263 "Make the right margin of the region smaller.
264 With no prefix argument, decrease the right margin by `standard-indent'.
265 A prefix arg (optional third arg INC noninteractively) specifies the amount
266 of width to remove, in characters. A negative argument increases
267 the right margin width.
268 If `auto-fill-mode' is active, re-fills region to fit in new margin."
269 (interactive "*r\nP")
270 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
271 (increase-right-margin from to
(- inc
)))
273 (defun beginning-of-line-text (&optional n
)
274 "Move to the beginning of the text on this line.
275 With optional argument, move forward N-1 lines first.
276 From the beginning of the line, moves past the left-margin indentation, the
277 fill-prefix, and any indentation used for centering or right-justifying the
278 line, but does not move past any whitespace that was explicitly inserted
279 \(such as a tab used to indent the first line of a paragraph)."
281 (beginning-of-line n
)
282 (skip-chars-forward " \t")
283 ;; Skip over fill-prefix.
285 (not (string-equal fill-prefix
"")))
286 (if (equal fill-prefix
288 (point) (min (point-max) (+ (length fill-prefix
) (point)))))
289 (forward-char (length fill-prefix
)))
290 (if (and adaptive-fill-mode adaptive-fill-regexp
291 (looking-at adaptive-fill-regexp
))
292 (goto-char (match-end 0))))
293 ;; Skip centering or flushright indentation
294 (if (memq (current-justification) '(center right
))
295 (skip-chars-forward " \t")))
297 (defvar indent-region-function nil
298 "Short cut function to indent region using `indent-according-to-mode'.
299 A value of nil means really run `indent-according-to-mode' on each line.")
301 (defun indent-region (start end column
)
302 "Indent each nonblank line in the region.
303 With prefix no argument, indent each line using `indent-according-to-mode',
304 or use `indent-region-function' to do the whole region if that's non-nil.
305 If there is a fill prefix, make each line start with the fill prefix.
306 With argument COLUMN, indent each line to that column.
308 When you call this from a program, START and END specify
309 the region to indent, and COLUMN specifies the indentation column.
310 If COLUMN is nil, then indent each line according to the mode."
316 (setq end
(point-marker))
318 (let ((regexp (regexp-quote fill-prefix
)))
319 (while (< (point) end
)
320 (or (looking-at regexp
)
322 (insert fill-prefix
))
324 (if indent-region-function
325 (funcall indent-region-function start end
)
328 (setq end
(point-marker))
330 (or (bolp) (forward-line 1))
331 (while (< (point) end
)
332 (or (and (bolp) (eolp))
333 (funcall indent-line-function
))
335 (move-marker end nil
))))
336 (setq column
(prefix-numeric-value column
))
339 (setq end
(point-marker))
341 (or (bolp) (forward-line 1))
342 (while (< (point) end
)
343 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
345 (indent-to column
0))
347 (move-marker end nil
))))
349 (defun indent-relative-maybe ()
350 "Indent a new line like previous nonblank line."
354 (defun indent-relative (&optional unindented-ok
)
355 "Space out to under next indent point in previous nonblank line.
356 An indent point is a non-whitespace character following whitespace.
357 The following line shows the indentation points in this line.
359 If the previous nonblank line has no indent points beyond the
360 column point starts at, `tab-to-tab-stop' is done instead."
363 (eq (char-syntax (preceding-char)) ?w
))
365 (let ((start-column (current-column))
369 (if (re-search-backward "^[^\n]" nil t
)
370 (let ((end (save-excursion (forward-line 1) (point))))
371 (move-to-column start-column
)
372 ;; Is start-column inside a tab on this line?
373 (if (> (current-column) start-column
)
375 (or (looking-at "[ \t]")
377 (skip-chars-forward "^ \t" end
))
378 (skip-chars-forward " \t" end
)
379 (or (= (point) end
) (setq indent
(current-column))))))
381 (let ((opoint (point-marker)))
382 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
384 (if (> opoint
(point))
386 (move-marker opoint nil
))
389 (defcustom tab-stop-list
390 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
391 "*List of tab stop positions used by `tab-to-tab-stop'.
392 This should be a list of integers, ordered from smallest to largest."
394 :type
'(repeat integer
))
396 (defvar edit-tab-stops-map nil
"Keymap used in `edit-tab-stops'.")
397 (if edit-tab-stops-map
399 (setq edit-tab-stops-map
(make-sparse-keymap))
400 (define-key edit-tab-stops-map
"\C-x\C-s" 'edit-tab-stops-note-changes
)
401 (define-key edit-tab-stops-map
"\C-c\C-c" 'edit-tab-stops-note-changes
))
403 (defvar edit-tab-stops-buffer nil
404 "Buffer whose tab stops are being edited--in case
405 the variable `tab-stop-list' is local in that buffer.")
407 (defun edit-tab-stops ()
408 "Edit the tab stops used by `tab-to-tab-stop'.
409 Creates a buffer *Tab Stops* containing text describing the tab stops.
410 A colon indicates a column where there is a tab stop.
411 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
413 (setq edit-tab-stops-buffer
(current-buffer))
414 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
415 (use-local-map edit-tab-stops-map
)
416 (make-local-variable 'indent-tabs-mode
)
417 (setq indent-tabs-mode nil
)
419 (setq truncate-lines t
)
421 (let ((tabs tab-stop-list
))
423 (indent-to (car tabs
) 0)
425 (setq tabs
(cdr tabs
))))
429 (insert (+ count ?
0))
431 (setq count
(1+ count
)))
434 (insert "0123456789")
435 (setq count
(1- count
))))
436 (insert "\nTo install changes, type C-c C-c")
437 (goto-char (point-min)))
439 (defun edit-tab-stops-note-changes ()
440 "Put edited tab stops into effect."
446 (while (search-backward ":" nil t
)
447 (setq tabs
(cons (current-column) tabs
))))
448 (bury-buffer (prog1 (current-buffer)
449 (switch-to-buffer edit-tab-stops-buffer
)))
450 (setq tab-stop-list tabs
))
451 (message "Tab stops installed"))
453 (defun tab-to-tab-stop ()
454 "Insert spaces or tabs to next defined tab-stop column.
455 The variable `tab-stop-list' is a list of columns at which there are tab stops.
456 Use \\[edit-tab-stops] to edit them interactively."
458 (and abbrev-mode
(= (char-syntax (preceding-char)) ?w
)
460 (let ((tabs tab-stop-list
))
461 (while (and tabs
(>= (current-column) (car tabs
)))
462 (setq tabs
(cdr tabs
)))
464 (let ((opoint (point)))
465 (skip-chars-backward " \t")
466 (delete-region (point) opoint
)
467 (indent-to (car tabs
)))
470 (defun move-to-tab-stop ()
471 "Move point to next defined tab-stop column.
472 The variable `tab-stop-list' is a list of columns at which there are tab stops.
473 Use \\[edit-tab-stops] to edit them interactively."
475 (let ((tabs tab-stop-list
))
476 (while (and tabs
(>= (current-column) (car tabs
)))
477 (setq tabs
(cdr tabs
)))
479 (let ((before (point)))
480 (move-to-column (car tabs
) t
)
483 ;; If we just added a tab, or moved over one,
484 ;; delete any superfluous spaces before the old point.
485 (if (and (eq (preceding-char) ?\
)
486 (eq (following-char) ?
\t))
487 (let ((tabend (* (/ (current-column) tab-width
) tab-width
)))
488 (while (and (> (current-column) tabend
)
489 (eq (preceding-char) ?\
))
491 (delete-region (point) before
))))))))
493 (define-key global-map
"\t" 'indent-for-tab-command
)
494 (define-key esc-map
"\034" 'indent-region
)
495 (define-key ctl-x-map
"\t" 'indent-rigidly
)
496 (define-key esc-map
"i" 'tab-to-tab-stop
)
498 ;;; indent.el ends here