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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; Commands for making and changing indentation in text. These are
26 ;; described in the Emacs manual.
30 (defvar standard-indent
4 "\
31 Default number of columns for margin-changing functions to indent.")
33 (defvar indent-line-function
'indent-to-left-margin
"\
34 Function to indent current line.")
36 (defun indent-according-to-mode ()
37 "Indent line in proper way for current major mode."
39 (funcall indent-line-function
))
41 (defun indent-for-tab-command ()
42 "Indent line in proper way for current major mode."
44 (if (eq indent-line-function
'indent-to-left-margin
)
46 (funcall indent-line-function
)))
53 (indent-to (* tab-width
(1+ (/ (current-column) tab-width
))))))
55 (defun indent-rigidly (start end arg
)
56 "Indent all lines starting in the region sideways by ARG columns.
57 Called from a program, takes three arguments, START, END and ARG."
61 (setq end
(point-marker))
63 (or (bolp) (forward-line 1))
64 (while (< (point) end
)
65 (let ((indent (current-indentation))
68 (skip-chars-forward " \t")
69 (setq eol-flag
(eolp)))
71 (indent-to (max 0 (+ indent arg
)) 0))
72 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
74 (move-marker end nil
)))
76 (defun indent-line-to (column)
77 "Indent current line to COLUMN.
78 This function removes or adds spaces and tabs at beginning of line
79 only if necessary. It leaves point at end of indentation."
81 (let ((cur-col (current-column)))
82 (cond ((< cur-col column
)
84 ((> cur-col column
) ; too far right (after tab?)
85 (delete-region (progn (move-to-column column t
) (point))
86 (progn (back-to-indentation) (point)))))))
88 (defun current-left-margin ()
89 "Return the left margin to use for this line.
90 This is the value of the buffer-local variable `left-margin' plus the value
91 of the `left-margin' text-property at the start of the line."
95 (+ left-margin
(or (get-text-property
96 (if (and (eobp) (not (bobp)))
100 (defun move-to-left-margin (&optional n force
)
101 "Move to the left margin of the current line.
102 With optional argument, move forward N-1 lines first.
103 The column moved to is the one given by the `current-left-margin' function.
104 If the line's indentation appears to be wrong, and this command is called
105 interactively or with optional argument FORCE, it will be fixed."
106 (interactive (list (prefix-numeric-value current-prefix-arg
) t
))
107 (beginning-of-line n
)
108 (let ((lm (current-left-margin)))
109 (if (memq (current-justification) '(right center
))
111 (skip-chars-forward " \t"))
112 (let ((cc (current-column)))
114 (if (> (move-to-column lm force
) lm
)
115 ;; If lm is in a tab and we are not forcing, move before tab
117 ((and force
(< cc lm
))
118 (indent-to-left-margin))))))
120 ;; This is the default indent-line-function,
121 ;; used in Fundamental Mode, Text Mode, etc.
122 (defun indent-to-left-margin ()
123 "Indent current line to the column given by `current-left-margin'."
124 (indent-line-to (current-left-margin)))
126 (defun delete-to-left-margin (&optional from to
)
127 "Remove left margin indentation from a region.
128 This deletes to the column given by `current-left-margin'.
129 In no case will it delete non-whitespace.
130 Args FROM and TO are optional; default is the whole buffer."
132 (goto-char (or to
(point-max)))
133 (setq to
(point-marker))
134 (goto-char (or from
(point-min)))
135 (or (bolp) (forward-line 1))
136 (while (< (point) to
)
137 (delete-region (point) (progn (move-to-left-margin nil t
) (point)))
139 (move-marker to nil
)))
141 (defun set-left-margin (from to lm
)
142 "Set the left margin of the region to WIDTH.
143 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
144 (interactive "r\nNSet left margin to column: ")
145 (if (interactive-p) (setq lm
(prefix-numeric-value lm
)))
147 ;; If inside indentation, start from BOL.
149 (skip-chars-backward " \t")
150 (if (bolp) (setq from
(point)))
151 ;; Place end after whitespace
153 (skip-chars-forward " \t")
154 (setq to
(point-marker)))
155 ;; Delete margin indentation first, but keep paragraph indentation.
156 (delete-to-left-margin from to
)
157 (put-text-property from to
'left-margin lm
)
158 (indent-rigidly from to lm
)
159 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
160 (move-marker to nil
))
162 (defun set-right-margin (from to lm
)
163 "Set the right margin of the region to WIDTH.
164 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
165 (interactive "r\nNSet right margin to width: ")
166 (if (interactive-p) (setq lm
(prefix-numeric-value lm
)))
169 (skip-chars-backward " \t")
170 (if (bolp) (setq from
(point))))
171 (put-text-property from to
'right-margin lm
)
172 (if auto-fill-function
(save-excursion (fill-region from to nil t t
))))
174 (defun alter-text-property (from to prop func
&optional object
)
175 "Programmatically change value of a text-property.
176 For each region between FROM and TO that has a single value for PROPERTY,
177 apply FUNCTION to that value and sets the property to the function's result.
178 Optional fifth argument OBJECT specifies the string or buffer to operate on."
181 (while (setq val
(get-text-property begin prop object
)
182 end
(text-property-not-all begin to prop val object
))
183 (put-text-property begin end prop
(funcall func val
) object
)
186 (put-text-property begin to prop
(funcall func val
) object
))))
188 (defun increase-left-margin (from to inc
)
189 "Increase or decrease the left-margin of the region.
190 With no prefix argument, this adds `standard-indent' of indentation.
191 A prefix arg (optional third arg INC noninteractively) specifies the amount
192 to change the margin by, in characters.
193 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
194 (interactive "*r\nP")
195 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
198 (skip-chars-backward " \t")
199 (if (bolp) (setq from
(point)))
201 (setq to
(point-marker)))
202 (alter-text-property from to
'left-margin
203 (lambda (v) (max (- left-margin
) (+ inc
(or v
0)))))
204 (indent-rigidly from to inc
)
205 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
206 (move-marker to nil
))
208 (defun decrease-left-margin (from to inc
)
209 "Make the left margin of the region smaller.
210 With no prefix argument, decrease the indentation by `standard-indent'.
211 A prefix arg (optional third arg INC noninteractively) specifies the amount
212 to change the margin by, in characters.
213 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
214 (interactive "*r\nP")
215 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
216 (increase-left-margin from to
(- inc
)))
218 (defun increase-right-margin (from to inc
)
219 "Increase the right-margin of the region.
220 With no prefix argument, increase the right margin by `standard-indent'.
221 A prefix arg (optional third arg INC noninteractively) specifies the amount
222 to change the margin by, in characters. A negative argument decreases
223 the right margin width.
224 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
227 (setq inc
(if inc
(prefix-numeric-value current-prefix-arg
)
230 (alter-text-property from to
'right-margin
231 (lambda (v) (+ inc
(or v
0))))
232 (if auto-fill-function
233 (fill-region from to nil t t
))))
235 (defun decrease-right-margin (from to inc
)
236 "Make the right margin of the region smaller.
237 With no prefix argument, decrease the right margin by `standard-indent'.
238 A prefix arg (optional third arg INC noninteractively) specifies the amount
239 of width to remove, in characters. A negative argument increases
240 the right margin width.
241 If `auto-fill-mode' is active, re-fills region to fit in new margin."
242 (interactive "*r\nP")
243 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
244 (increase-right-margin from to
(- inc
)))
246 (defun beginning-of-line-text (&optional n
)
247 "Move to the beginning of the text on this line.
248 With optional argument, move forward N-1 lines first.
249 From the beginning of the line, moves past the left-margin indentation, the
250 fill-prefix, and any indentation used for centering or right-justifying the
251 line, but does not move past any whitespace that was explicitly inserted
252 \(such as a tab used to indent the first line of a paragraph)."
254 (beginning-of-line n
)
255 (skip-chars-forward " \t")
256 ;; Skip over fill-prefix.
258 (not (string-equal fill-prefix
"")))
259 (if (equal fill-prefix
261 (point) (min (point-max) (+ (length fill-prefix
) (point)))))
262 (forward-char (length fill-prefix
)))
263 (if (and adaptive-fill-mode
264 (looking-at adaptive-fill-regexp
))
265 (goto-char (match-end 0))))
266 ;; Skip centering or flushright indentation
267 (if (memq (current-justification) '(center right
))
268 (skip-chars-forward " \t")))
270 (defvar indent-region-function nil
271 "Function which is short cut to indent region using indent-according-to-mode.
272 A value of nil means really run indent-according-to-mode on each line.")
274 (defun indent-region (start end column
)
275 "Indent each nonblank line in the region.
276 With no argument, indent each line using `indent-according-to-mode',
277 or use `indent-region-function' to do the whole region if that's non-nil.
278 If there is a fill prefix, make each line start with the fill prefix.
279 With argument COLUMN, indent each line to that column.
280 Called from a program, takes three args: START, END and COLUMN."
286 (setq end
(point-marker))
288 (let ((regexp (regexp-quote fill-prefix
)))
289 (while (< (point) end
)
290 (or (looking-at regexp
)
292 (insert fill-prefix
))
294 (if indent-region-function
295 (funcall indent-region-function start end
)
298 (setq end
(point-marker))
300 (or (bolp) (forward-line 1))
301 (while (< (point) end
)
302 (or (and (bolp) (eolp))
303 (funcall indent-line-function
))
305 (move-marker end nil
))))
306 (setq column
(prefix-numeric-value column
))
309 (setq end
(point-marker))
311 (or (bolp) (forward-line 1))
312 (while (< (point) end
)
313 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
315 (indent-to column
0))
317 (move-marker end nil
))))
319 (defun indent-relative-maybe ()
320 "Indent a new line like previous nonblank line."
324 (defun indent-relative (&optional unindented-ok
)
325 "Space out to under next indent point in previous nonblank line.
326 An indent point is a non-whitespace character following whitespace.
327 If the previous nonblank line has no indent points beyond the
328 column point starts at, `tab-to-tab-stop' is done instead."
330 (if abbrev-mode
(expand-abbrev))
331 (let ((start-column (current-column))
335 (if (re-search-backward "^[^\n]" nil t
)
336 (let ((end (save-excursion (forward-line 1) (point))))
337 (move-to-column start-column
)
338 ;; Is start-column inside a tab on this line?
339 (if (> (current-column) start-column
)
341 (or (looking-at "[ \t]")
343 (skip-chars-forward "^ \t" end
))
344 (skip-chars-forward " \t" end
)
345 (or (= (point) end
) (setq indent
(current-column))))))
347 (let ((opoint (point-marker)))
348 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
350 (if (> opoint
(point))
352 (move-marker opoint nil
))
355 (defvar tab-stop-list
356 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
357 "*List of tab stop positions used by `tab-to-tab-stops'.
358 This should be a list of integers, ordered from smallest to largest.")
360 (defvar edit-tab-stops-map nil
"Keymap used in `edit-tab-stops'.")
361 (if edit-tab-stops-map
363 (setq edit-tab-stops-map
(make-sparse-keymap))
364 (define-key edit-tab-stops-map
"\C-x\C-s" 'edit-tab-stops-note-changes
)
365 (define-key edit-tab-stops-map
"\C-c\C-c" 'edit-tab-stops-note-changes
))
367 (defvar edit-tab-stops-buffer nil
368 "Buffer whose tab stops are being edited--in case
369 the variable `tab-stop-list' is local in that buffer.")
371 (defun edit-tab-stops ()
372 "Edit the tab stops used by `tab-to-tab-stop'.
373 Creates a buffer *Tab Stops* containing text describing the tab stops.
374 A colon indicates a column where there is a tab stop.
375 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
377 (setq edit-tab-stops-buffer
(current-buffer))
378 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
379 (use-local-map edit-tab-stops-map
)
380 (make-local-variable 'indent-tabs-mode
)
381 (setq indent-tabs-mode nil
)
383 (setq truncate-lines t
)
385 (let ((tabs tab-stop-list
))
387 (indent-to (car tabs
) 0)
389 (setq tabs
(cdr tabs
))))
393 (insert (+ count ?
0))
395 (setq count
(1+ count
)))
398 (insert "0123456789")
399 (setq count
(1- count
))))
400 (insert "\nTo install changes, type C-c C-c")
401 (goto-char (point-min)))
403 (defun edit-tab-stops-note-changes ()
404 "Put edited tab stops into effect."
410 (while (search-backward ":" nil t
)
411 (setq tabs
(cons (current-column) tabs
))))
412 (bury-buffer (prog1 (current-buffer)
413 (switch-to-buffer edit-tab-stops-buffer
)))
414 (setq tab-stop-list tabs
))
415 (message "Tab stops installed"))
417 (defun tab-to-tab-stop ()
418 "Insert spaces or tabs to next defined tab-stop column.
419 The variable `tab-stop-list' is a list of columns at which there are tab stops.
420 Use \\[edit-tab-stops] to edit them interactively."
422 (if abbrev-mode
(expand-abbrev))
423 (let ((tabs tab-stop-list
))
424 (while (and tabs
(>= (current-column) (car tabs
)))
425 (setq tabs
(cdr tabs
)))
427 (let ((opoint (point)))
428 (skip-chars-backward " \t")
429 (delete-region (point) opoint
)
430 (indent-to (car tabs
)))
433 (defun move-to-tab-stop ()
434 "Move point to next defined tab-stop column.
435 The variable `tab-stop-list' is a list of columns at which there are tab stops.
436 Use \\[edit-tab-stops] to edit them interactively."
438 (let ((tabs tab-stop-list
))
439 (while (and tabs
(>= (current-column) (car tabs
)))
440 (setq tabs
(cdr tabs
)))
442 (let ((before (point)))
443 (move-to-column (car tabs
) t
)
446 ;; If we just added a tab, or moved over one,
447 ;; delete any superfluous spaces before the old point.
448 (if (and (eq (preceding-char) ?\
)
449 (eq (following-char) ?
\t))
450 (let ((tabend (* (/ (current-column) tab-width
) tab-width
)))
451 (while (and (> (current-column) tabend
)
452 (eq (preceding-char) ?\
))
454 (delete-region (point) before
))))))))
456 (define-key global-map
"\t" 'indent-for-tab-command
)
457 (define-key esc-map
"\034" 'indent-region
)
458 (define-key ctl-x-map
"\t" 'indent-rigidly
)
459 (define-key esc-map
"i" 'tab-to-tab-stop
)
461 ;;; indent.el ends here