1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985 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 indent-line-function
'indent-to-left-margin
"\
31 Function to indent current line.")
33 (defun indent-according-to-mode ()
34 "Indent line in proper way for current major mode."
36 (funcall indent-line-function
))
38 (defun indent-for-tab-command ()
39 "Indent line in proper way for current major mode."
41 (if (eq indent-line-function
'indent-to-left-margin
)
43 (funcall indent-line-function
)))
50 (indent-to (* tab-width
(1+ (/ (current-column) tab-width
))))))
52 (defun indent-rigidly (start end arg
)
53 "Indent all lines starting in the region sideways by ARG columns.
54 Called from a program, takes three arguments, START, END and ARG."
58 (setq end
(point-marker))
60 (or (bolp) (forward-line 1))
61 (while (< (point) end
)
62 (let ((indent (current-indentation)))
63 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
65 (indent-to (max 0 (+ indent arg
)) 0)))
67 (move-marker end nil
)))
69 ;; This is the default indent-line-function,
70 ;; used in Fundamental Mode, Text Mode, etc.
71 (defun indent-to-left-margin ()
72 (or (= (current-indentation) left-margin
)
76 (delete-region (point)
77 (progn (skip-chars-forward " \t")
79 (indent-to left-margin
)
84 (defvar indent-region-function nil
85 "Function which is short cut to indent region using indent-according-to-mode.
86 A value of nil means really run indent-according-to-mode on each line.")
88 (defun indent-region (start end column
)
89 "Indent each nonblank line in the region.
90 With no argument, indent each line using `indent-according-to-mode',
91 or use `indent-region-function' to do the whole region if that's non-nil.
92 If there is a fill prefix, make each line start with the fill prefix.
93 With argument COLUMN, indent each line to that column.
94 Called from a program, takes three args: START, END and COLUMN."
100 (setq end
(point-marker))
102 (let ((regexp (regexp-quote fill-prefix
)))
103 (while (< (point) end
)
104 (or (looking-at regexp
)
106 (insert fill-prefix
))
108 (if indent-region-function
109 (funcall indent-region-function start end
)
112 (setq end
(point-marker))
114 (or (bolp) (forward-line 1))
115 (while (< (point) end
)
116 (or (and (bolp) (eolp))
117 (funcall indent-line-function
))
119 (move-marker end nil
))))
120 (setq column
(prefix-numeric-value column
))
123 (setq end
(point-marker))
125 (or (bolp) (forward-line 1))
126 (while (< (point) end
)
127 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
129 (indent-to column
0))
131 (move-marker end nil
))))
133 (defun indent-relative-maybe ()
134 "Indent a new line like previous nonblank line."
138 (defun indent-relative (&optional unindented-ok
)
139 "Space out to under next indent point in previous nonblank line.
140 An indent point is a non-whitespace character following whitespace.
141 If the previous nonblank line has no indent points beyond the
142 column point starts at, `tab-to-tab-stop' is done instead."
144 (if abbrev-mode
(expand-abbrev))
145 (let ((start-column (current-column))
149 (if (re-search-backward "^[^\n]" nil t
)
150 (let ((end (save-excursion (forward-line 1) (point))))
151 (move-to-column start-column
)
152 ;; Is start-column inside a tab on this line?
153 (if (> (current-column) start-column
)
155 (or (looking-at "[ \t]")
157 (skip-chars-forward "^ \t" end
))
158 (skip-chars-forward " \t" end
)
159 (or (= (point) end
) (setq indent
(current-column))))))
161 (let ((opoint (point-marker)))
162 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
164 (if (> opoint
(point))
166 (move-marker opoint nil
))
169 (defvar tab-stop-list
170 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
171 "*List of tab stop positions used by `tab-to-tab-stops'.
172 This should be a list of integers, ordered from smallest to largest.")
174 (defvar edit-tab-stops-map nil
"Keymap used in `edit-tab-stops'.")
175 (if edit-tab-stops-map
177 (setq edit-tab-stops-map
(make-sparse-keymap))
178 (define-key edit-tab-stops-map
"\C-x\C-s" 'edit-tab-stops-note-changes
)
179 (define-key edit-tab-stops-map
"\C-c\C-c" 'edit-tab-stops-note-changes
))
181 (defvar edit-tab-stops-buffer nil
182 "Buffer whose tab stops are being edited--in case
183 the variable `tab-stop-list' is local in that buffer.")
185 (defun edit-tab-stops ()
186 "Edit the tab stops used by `tab-to-tab-stop'.
187 Creates a buffer *Tab Stops* containing text describing the tab stops.
188 A colon indicates a column where there is a tab stop.
189 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
191 (setq edit-tab-stops-buffer
(current-buffer))
192 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
193 (use-local-map edit-tab-stops-map
)
194 (make-local-variable 'indent-tabs-mode
)
195 (setq indent-tabs-mode nil
)
197 (setq truncate-lines t
)
199 (let ((tabs tab-stop-list
))
201 (indent-to (car tabs
) 0)
203 (setq tabs
(cdr tabs
))))
207 (insert (+ count ?
0))
209 (setq count
(1+ count
)))
212 (insert "0123456789")
213 (setq count
(1- count
))))
214 (insert "\nTo install changes, type C-c C-c")
215 (goto-char (point-min)))
217 (defun edit-tab-stops-note-changes ()
218 "Put edited tab stops into effect."
224 (while (search-backward ":" nil t
)
225 (setq tabs
(cons (current-column) tabs
))))
226 (bury-buffer (prog1 (current-buffer)
227 (switch-to-buffer edit-tab-stops-buffer
)))
228 (setq tab-stop-list tabs
))
229 (message "Tab stops installed"))
231 (defun tab-to-tab-stop ()
232 "Insert spaces or tabs to next defined tab-stop column.
233 The variable `tab-stop-list' is a list of columns at which there are tab stops.
234 Use \\[edit-tab-stops] to edit them interactively."
236 (if abbrev-mode
(expand-abbrev))
237 (let ((tabs tab-stop-list
))
238 (while (and tabs
(>= (current-column) (car tabs
)))
239 (setq tabs
(cdr tabs
)))
241 (let ((opoint (point)))
242 (skip-chars-backward " \t")
243 (delete-region (point) opoint
)
244 (indent-to (car tabs
)))
247 (defun move-to-tab-stop ()
248 "Move point to next defined tab-stop column.
249 The variable `tab-stop-list' is a list of columns at which there are tab stops.
250 Use \\[edit-tab-stops] to edit them interactively."
252 (let ((tabs tab-stop-list
))
253 (while (and tabs
(>= (current-column) (car tabs
)))
254 (setq tabs
(cdr tabs
)))
257 (if (eq last-command
'move-to-tab-stop
)
258 (let ((opoint (point)))
259 (skip-chars-backward " \t")
260 (delete-region (point) opoint
)))
261 (move-to-column (car tabs
) t
)))))
263 (define-key global-map
"\t" 'indent-for-tab-command
)
264 (define-key esc-map
"\034" 'indent-region
)
265 (define-key ctl-x-map
"\t" 'indent-rigidly
)
266 (define-key esc-map
"i" 'tab-to-tab-stop
)
268 ;;; indent.el ends here