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.
31 (defvar standard-indent
4 "\
32 Default number of columns for margin-changing functions to indent.")
34 (defvar indent-line-function
'indent-to-left-margin
"\
35 Function to indent current line.")
37 (defun indent-according-to-mode ()
38 "Indent line in proper way for current major mode."
40 (funcall indent-line-function
))
42 (defun indent-for-tab-command (&optional prefix-arg
)
43 "Indent line in proper way for current major mode."
45 (if (eq indent-line-function
'indent-to-left-margin
)
48 (funcall indent-line-function prefix-arg
)
49 (funcall indent-line-function
))))
56 (indent-to (* tab-width
(1+ (/ (current-column) tab-width
))))))
58 (defun indent-rigidly (start end arg
)
59 "Indent all lines starting in the region sideways by ARG columns.
60 Called from a program, takes three arguments, START, END and ARG."
64 (setq end
(point-marker))
66 (or (bolp) (forward-line 1))
67 (while (< (point) end
)
68 (let ((indent (current-indentation))
71 (skip-chars-forward " \t")
72 (setq eol-flag
(eolp)))
74 (indent-to (max 0 (+ indent arg
)) 0))
75 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
77 (move-marker end nil
)))
79 (defun indent-line-to (column)
80 "Indent current line to COLUMN.
81 This function removes or adds spaces and tabs at beginning of line
82 only if necessary. It leaves point at end of indentation."
84 (let ((cur-col (current-column)))
85 (cond ((< cur-col column
)
86 (if (> (- column
(* (/ cur-col tab-width
) tab-width
)) tab-width
)
87 (delete-region (point)
88 (progn (skip-chars-backward " ") (point))))
90 ((> cur-col column
) ; too far right (after tab?)
91 (delete-region (progn (move-to-column column t
) (point))
92 (progn (back-to-indentation) (point)))))))
94 (defun current-left-margin ()
95 "Return the left margin to use for this line.
96 This is the value of the buffer-local variable `left-margin' plus the value
97 of the `left-margin' text-property at the start of the line."
101 (+ left-margin
(or (get-text-property
102 (if (and (eobp) (not (bobp)))
103 (1- (point)) (point))
106 (defun move-to-left-margin (&optional n force
)
107 "Move to the left margin of the current line.
108 With optional argument, move forward N-1 lines first.
109 The column moved to is the one given by the `current-left-margin' function.
110 If the line's indentation appears to be wrong, and this command is called
111 interactively or with optional argument FORCE, it will be fixed."
112 (interactive (list (prefix-numeric-value current-prefix-arg
) t
))
113 (beginning-of-line n
)
114 (skip-chars-forward " \t")
115 (let ((lm (current-left-margin))
116 (cc (current-column)))
118 (if (> (move-to-column lm force
) lm
)
119 ;; If lm is in a tab and we are not forcing, move before tab
121 ((and force
(< cc lm
))
122 (indent-to-left-margin)))))
124 ;; This is the default indent-line-function,
125 ;; used in Fundamental Mode, Text Mode, etc.
126 (defun indent-to-left-margin ()
127 "Indent current line to the column given by `current-left-margin'."
128 (indent-line-to (current-left-margin)))
130 (defun delete-to-left-margin (&optional from to
)
131 "Remove left margin indentation from a region.
132 This deletes to the column given by `current-left-margin'.
133 In no case will it delete non-whitespace.
134 Args FROM and TO are optional; default is the whole buffer."
136 (goto-char (or to
(point-max)))
137 (setq to
(point-marker))
138 (goto-char (or from
(point-min)))
139 (or (bolp) (forward-line 1))
140 (while (< (point) to
)
141 (delete-region (point) (progn (move-to-left-margin nil t
) (point)))
143 (move-marker to nil
)))
145 (defun set-left-margin (from to lm
)
146 "Set the left margin of the region to WIDTH.
147 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
148 (interactive "r\nNSet left margin to column: ")
149 (if (interactive-p) (setq lm
(prefix-numeric-value lm
)))
151 ;; If inside indentation, start from BOL.
153 (skip-chars-backward " \t")
154 (if (bolp) (setq from
(point)))
155 ;; Place end after whitespace
157 (skip-chars-forward " \t")
158 (setq to
(point-marker)))
159 ;; Delete margin indentation first, but keep paragraph indentation.
160 (delete-to-left-margin from to
)
161 (put-text-property from to
'left-margin lm
)
162 (indent-rigidly from to lm
)
163 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
164 (move-marker to nil
))
166 (defun set-right-margin (from to lm
)
167 "Set the right margin of the region to WIDTH.
168 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
169 (interactive "r\nNSet right margin to width: ")
170 (if (interactive-p) (setq lm
(prefix-numeric-value lm
)))
173 (skip-chars-backward " \t")
174 (if (bolp) (setq from
(point))))
175 (put-text-property from to
'right-margin lm
)
176 (if auto-fill-function
(save-excursion (fill-region from to nil t t
))))
178 (defun alter-text-property (from to prop func
&optional object
)
179 "Programmatically change value of a text-property.
180 For each region between FROM and TO that has a single value for PROPERTY,
181 apply FUNCTION to that value and sets the property to the function's result.
182 Optional fifth argument OBJECT specifies the string or buffer to operate on."
185 (while (setq val
(get-text-property begin prop object
)
186 end
(text-property-not-all begin to prop val object
))
187 (put-text-property begin end prop
(funcall func val
) object
)
190 (put-text-property begin to prop
(funcall func val
) object
))))
192 (defun increase-left-margin (from to inc
)
193 "Increase or decrease the left-margin of the region.
194 With no prefix argument, this adds `standard-indent' of indentation.
195 A prefix arg (optional third arg INC noninteractively) specifies the amount
196 to change the margin by, in characters.
197 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
198 (interactive "*r\nP")
199 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
202 (skip-chars-backward " \t")
203 (if (bolp) (setq from
(point)))
205 (setq to
(point-marker)))
206 (alter-text-property from to
'left-margin
207 (lambda (v) (max (- left-margin
) (+ inc
(or v
0)))))
208 (indent-rigidly from to inc
)
209 (if auto-fill-function
(save-excursion (fill-region from to nil t t
)))
210 (move-marker to nil
))
212 (defun decrease-left-margin (from to inc
)
213 "Make the left margin of the region smaller.
214 With no prefix argument, decrease the indentation by `standard-indent'.
215 A prefix arg (optional third arg INC noninteractively) specifies the amount
216 to change the margin by, in characters.
217 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
218 (interactive "*r\nP")
219 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
220 (increase-left-margin from to
(- inc
)))
222 (defun increase-right-margin (from to inc
)
223 "Increase the right-margin of the region.
224 With no prefix argument, increase the right margin by `standard-indent'.
225 A prefix arg (optional third arg INC noninteractively) specifies the amount
226 to change the margin by, in characters. A negative argument decreases
227 the right margin width.
228 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
231 (setq inc
(if inc
(prefix-numeric-value current-prefix-arg
)
234 (alter-text-property from to
'right-margin
235 (lambda (v) (+ inc
(or v
0))))
236 (if auto-fill-function
237 (fill-region from to nil t t
))))
239 (defun decrease-right-margin (from to inc
)
240 "Make the right margin of the region smaller.
241 With no prefix argument, decrease the right margin by `standard-indent'.
242 A prefix arg (optional third arg INC noninteractively) specifies the amount
243 of width to remove, in characters. A negative argument increases
244 the right margin width.
245 If `auto-fill-mode' is active, re-fills region to fit in new margin."
246 (interactive "*r\nP")
247 (setq inc
(if inc
(prefix-numeric-value inc
) standard-indent
))
248 (increase-right-margin from to
(- inc
)))
250 (defun beginning-of-line-text (&optional n
)
251 "Move to the beginning of the text on this line.
252 With optional argument, move forward N-1 lines first.
253 From the beginning of the line, moves past the left-margin indentation, the
254 fill-prefix, and any indentation used for centering or right-justifying the
255 line, but does not move past any whitespace that was explicitly inserted
256 \(such as a tab used to indent the first line of a paragraph)."
258 (beginning-of-line n
)
259 (skip-chars-forward " \t")
260 ;; Skip over fill-prefix.
262 (not (string-equal fill-prefix
"")))
263 (if (equal fill-prefix
265 (point) (min (point-max) (+ (length fill-prefix
) (point)))))
266 (forward-char (length fill-prefix
)))
267 (if (and adaptive-fill-mode
268 (looking-at adaptive-fill-regexp
))
269 (goto-char (match-end 0))))
270 ;; Skip centering or flushright indentation
271 (if (memq (current-justification) '(center right
))
272 (skip-chars-forward " \t")))
274 (defvar indent-region-function nil
275 "Short cut function to indent region using `indent-according-to-mode'.
276 A value of nil means really run `indent-according-to-mode' on each line.")
278 (defun indent-region (start end column
)
279 "Indent each nonblank line in the region.
280 With no argument, indent each line using `indent-according-to-mode',
281 or use `indent-region-function' to do the whole region if that's non-nil.
282 If there is a fill prefix, make each line start with the fill prefix.
283 With argument COLUMN, indent each line to that column.
284 Called from a program, takes three args: START, END and COLUMN."
290 (setq end
(point-marker))
292 (let ((regexp (regexp-quote fill-prefix
)))
293 (while (< (point) end
)
294 (or (looking-at regexp
)
296 (insert fill-prefix
))
298 (if indent-region-function
299 (funcall indent-region-function start end
)
302 (setq end
(point-marker))
304 (or (bolp) (forward-line 1))
305 (while (< (point) end
)
306 (or (and (bolp) (eolp))
307 (funcall indent-line-function
))
309 (move-marker end nil
))))
310 (setq column
(prefix-numeric-value column
))
313 (setq end
(point-marker))
315 (or (bolp) (forward-line 1))
316 (while (< (point) end
)
317 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
319 (indent-to column
0))
321 (move-marker end nil
))))
323 (defun indent-relative-maybe ()
324 "Indent a new line like previous nonblank line."
328 (defun indent-relative (&optional unindented-ok
)
329 "Space out to under next indent point in previous nonblank line.
330 An indent point is a non-whitespace character following whitespace.
331 If the previous nonblank line has no indent points beyond the
332 column point starts at, `tab-to-tab-stop' is done instead."
334 (if abbrev-mode
(expand-abbrev))
335 (let ((start-column (current-column))
339 (if (re-search-backward "^[^\n]" nil t
)
340 (let ((end (save-excursion (forward-line 1) (point))))
341 (move-to-column start-column
)
342 ;; Is start-column inside a tab on this line?
343 (if (> (current-column) start-column
)
345 (or (looking-at "[ \t]")
347 (skip-chars-forward "^ \t" end
))
348 (skip-chars-forward " \t" end
)
349 (or (= (point) end
) (setq indent
(current-column))))))
351 (let ((opoint (point-marker)))
352 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
354 (if (> opoint
(point))
356 (move-marker opoint nil
))
359 (defvar tab-stop-list
360 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
361 "*List of tab stop positions used by `tab-to-tab-stops'.
362 This should be a list of integers, ordered from smallest to largest.")
364 (defvar edit-tab-stops-map nil
"Keymap used in `edit-tab-stops'.")
365 (if edit-tab-stops-map
367 (setq edit-tab-stops-map
(make-sparse-keymap))
368 (define-key edit-tab-stops-map
"\C-x\C-s" 'edit-tab-stops-note-changes
)
369 (define-key edit-tab-stops-map
"\C-c\C-c" 'edit-tab-stops-note-changes
))
371 (defvar edit-tab-stops-buffer nil
372 "Buffer whose tab stops are being edited--in case
373 the variable `tab-stop-list' is local in that buffer.")
375 (defun edit-tab-stops ()
376 "Edit the tab stops used by `tab-to-tab-stop'.
377 Creates a buffer *Tab Stops* containing text describing the tab stops.
378 A colon indicates a column where there is a tab stop.
379 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
381 (setq edit-tab-stops-buffer
(current-buffer))
382 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
383 (use-local-map edit-tab-stops-map
)
384 (make-local-variable 'indent-tabs-mode
)
385 (setq indent-tabs-mode nil
)
387 (setq truncate-lines t
)
389 (let ((tabs tab-stop-list
))
391 (indent-to (car tabs
) 0)
393 (setq tabs
(cdr tabs
))))
397 (insert (+ count ?
0))
399 (setq count
(1+ count
)))
402 (insert "0123456789")
403 (setq count
(1- count
))))
404 (insert "\nTo install changes, type C-c C-c")
405 (goto-char (point-min)))
407 (defun edit-tab-stops-note-changes ()
408 "Put edited tab stops into effect."
414 (while (search-backward ":" nil t
)
415 (setq tabs
(cons (current-column) tabs
))))
416 (bury-buffer (prog1 (current-buffer)
417 (switch-to-buffer edit-tab-stops-buffer
)))
418 (setq tab-stop-list tabs
))
419 (message "Tab stops installed"))
421 (defun tab-to-tab-stop ()
422 "Insert spaces or tabs to next defined tab-stop column.
423 The variable `tab-stop-list' is a list of columns at which there are tab stops.
424 Use \\[edit-tab-stops] to edit them interactively."
426 (and abbrev-mode
(= (char-syntax (preceding-char)) ?w
)
428 (let ((tabs tab-stop-list
))
429 (while (and tabs
(>= (current-column) (car tabs
)))
430 (setq tabs
(cdr tabs
)))
432 (let ((opoint (point)))
433 (skip-chars-backward " \t")
434 (delete-region (point) opoint
)
435 (indent-to (car tabs
)))
438 (defun move-to-tab-stop ()
439 "Move point to next defined tab-stop column.
440 The variable `tab-stop-list' is a list of columns at which there are tab stops.
441 Use \\[edit-tab-stops] to edit them interactively."
443 (let ((tabs tab-stop-list
))
444 (while (and tabs
(>= (current-column) (car tabs
)))
445 (setq tabs
(cdr tabs
)))
447 (let ((before (point)))
448 (move-to-column (car tabs
) t
)
451 ;; If we just added a tab, or moved over one,
452 ;; delete any superfluous spaces before the old point.
453 (if (and (eq (preceding-char) ?\
)
454 (eq (following-char) ?
\t))
455 (let ((tabend (* (/ (current-column) tab-width
) tab-width
)))
456 (while (and (> (current-column) tabend
)
457 (eq (preceding-char) ?\
))
459 (delete-region (point) before
))))))))
461 (define-key global-map
"\t" 'indent-for-tab-command
)
462 (define-key esc-map
"\034" 'indent-region
)
463 (define-key ctl-x-map
"\t" 'indent-rigidly
)
464 (define-key esc-map
"i" 'tab-to-tab-stop
)
466 ;;; indent.el ends here