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 (defvar indent-line-function
'indent-to-left-margin
"\
26 Function to indent current line.");Now in loaddefs.el
28 (defun indent-according-to-mode ()
29 "Indent line in proper way for current major mode."
31 (funcall indent-line-function
))
33 (defun indent-for-tab-command ()
34 "Indent line in proper way for current major mode."
36 (if (eq indent-line-function
'indent-to-left-margin
)
38 (funcall indent-line-function
)))
45 (indent-to (* tab-width
(1+ (/ (current-column) tab-width
))))))
47 (defun indent-rigidly (start end arg
)
48 "Indent all lines starting in the region sideways by ARG columns.
49 Called from a program, takes three arguments, START, END and ARG."
53 (setq end
(point-marker))
55 (or (bolp) (forward-line 1))
56 (while (< (point) end
)
57 (let ((indent (current-indentation)))
58 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
60 (indent-to (max 0 (+ indent arg
)) 0)))
62 (move-marker end nil
)))
64 ;; This is the default indent-line-function,
65 ;; used in Fundamental Mode, Text Mode, etc.
66 (defun indent-to-left-margin ()
67 (or (= (current-indentation) left-margin
)
71 (delete-region (point)
72 (progn (skip-chars-forward " \t")
74 (indent-to left-margin
)
79 (defvar indent-region-function nil
80 "Function which is short cut to indent each line in region with TAB.
81 A value of nil means really perform TAB on each line.")
83 (defun indent-region (start end arg
)
84 "Indent each nonblank line in the region.
85 With no argument, indent each line with TAB.
86 \(If there is a fill prefix, make each line start with the fill prefix.)
87 With argument COLUMN, indent each line to that column.
88 Called from a program, takes three args: START, END and COLUMN."
94 (setq end
(point-marker))
96 (let ((regexp (regexp-quote fill-prefix
)))
97 (while (< (point) end
)
98 (or (looking-at regexp
)
101 (if indent-region-function
102 (funcall indent-region-function start end
)
105 (setq end
(point-marker))
107 (or (bolp) (forward-line 1))
108 (while (< (point) end
)
109 (funcall indent-line-function
)
111 (move-marker end nil
))))
112 (setq arg
(prefix-numeric-value arg
))
115 (setq end
(point-marker))
117 (or (bolp) (forward-line 1))
118 (while (< (point) end
)
119 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
123 (move-marker end nil
))))
125 (defun indent-relative-maybe ()
126 "Indent a new line like previous nonblank line."
130 (defun indent-relative (&optional unindented-ok
)
131 "Space out to under next indent point in previous nonblank line.
132 An indent point is a non-whitespace character following whitespace.
133 If the previous nonblank line has no indent points beyond the
134 column point starts at, `tab-to-tab-stop' is done instead."
136 (if abbrev-mode
(expand-abbrev))
137 (let ((start-column (current-column))
141 (if (re-search-backward "^[^\n]" nil t
)
142 (let ((end (save-excursion (forward-line 1) (point))))
143 (move-to-column start-column
)
144 ;; Is start-column inside a tab on this line?
145 (if (> (current-column) start-column
)
147 (or (looking-at "[ \t]")
149 (skip-chars-forward "^ \t" end
))
150 (skip-chars-forward " \t" end
)
151 (or (= (point) end
) (setq indent
(current-column))))))
153 (let ((opoint (point-marker)))
154 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
156 (if (> opoint
(point))
158 (move-marker opoint nil
))
161 (defvar tab-stop-list
162 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
163 "*List of tab stop positions used by `tab-to-tab-stops'.")
165 (defvar edit-tab-stops-map nil
"Keymap used in `edit-tab-stops'.")
166 (if edit-tab-stops-map
168 (setq edit-tab-stops-map
(make-sparse-keymap))
169 (define-key edit-tab-stops-map
"\C-x\C-s" 'edit-tab-stops-note-changes
)
170 (define-key edit-tab-stops-map
"\C-c\C-c" 'edit-tab-stops-note-changes
))
172 (defvar edit-tab-stops-buffer nil
173 "Buffer whose tab stops are being edited--in case
174 the variable `tab-stop-list' is local in that buffer.")
176 (defun edit-tab-stops ()
177 "Edit the tab stops used by `tab-to-tab-stop'.
178 Creates a buffer *Tab Stops* containing text describing the tab stops.
179 A colon indicates a column where there is a tab stop.
180 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
182 (setq edit-tab-stops-buffer
(current-buffer))
183 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
184 (use-local-map edit-tab-stops-map
)
185 (make-local-variable 'indent-tabs-mode
)
186 (setq indent-tabs-mode nil
)
188 (setq truncate-lines t
)
190 (let ((tabs tab-stop-list
))
192 (indent-to (car tabs
) 0)
194 (setq tabs
(cdr tabs
))))
198 (insert (+ count ?
0))
200 (setq count
(1+ count
)))
203 (insert "0123456789")
204 (setq count
(1- count
))))
205 (insert "\nTo install changes, type C-c C-c")
206 (goto-char (point-min)))
208 (defun edit-tab-stops-note-changes ()
209 "Put edited tab stops into effect."
215 (while (search-backward ":" nil t
)
216 (setq tabs
(cons (current-column) tabs
))))
217 (bury-buffer (prog1 (current-buffer)
218 (switch-to-buffer edit-tab-stops-buffer
)))
219 (setq tab-stop-list tabs
))
220 (message "Tab stops installed"))
222 (defun tab-to-tab-stop ()
223 "Insert spaces or tabs to next defined tab-stop column.
224 The variable `tab-stop-list' is a list of columns at which there are tab stops.
225 Use \\[edit-tab-stops] to edit them interactively."
227 (if abbrev-mode
(expand-abbrev))
228 (let ((tabs tab-stop-list
))
229 (while (and tabs
(>= (current-column) (car tabs
)))
230 (setq tabs
(cdr tabs
)))
232 (indent-to (car tabs
))
235 (defun move-to-tab-stop ()
236 "Move point to next defined tab-stop column.
237 The variable `tab-stop-list' is a list of columns at which there are tab stops.
238 Use \\[edit-tab-stops] to edit them interactively."
240 (let ((tabs tab-stop-list
))
241 (while (and tabs
(>= (current-column) (car tabs
)))
242 (setq tabs
(cdr tabs
)))
244 (move-to-column (car tabs
) t
))))
246 (define-key global-map
"\t" 'indent-for-tab-command
)
247 (define-key esc-map
"\034" 'indent-region
)
248 (define-key ctl-x-map
"\t" 'indent-rigidly
)
249 (define-key esc-map
"i" 'tab-to-tab-stop
)
251 ;;; indent.el ends here