1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 (defvar indent-line-function
'indent-to-left-margin
"\
24 Function to indent current line.");Now in loaddefs.el
26 (defun indent-according-to-mode ()
27 "Indent line in proper way for current major mode."
29 (funcall indent-line-function
))
31 (defun indent-for-tab-command ()
32 "Indent line in proper way for current major mode."
34 (if (eq indent-line-function
'indent-to-left-margin
)
36 (funcall indent-line-function
)))
43 (indent-to (* tab-width
(1+ (/ (current-column) tab-width
))))))
45 (defun indent-rigidly (start end arg
)
46 "Indent all lines starting in the region sideways by ARG columns.
47 Called from a program, takes three arguments, START, END and ARG."
51 (setq end
(point-marker))
53 (or (bolp) (forward-line 1))
54 (while (< (point) end
)
55 (let ((indent (current-indentation)))
56 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
58 (indent-to (max 0 (+ indent arg
)) 0)))
60 (move-marker end nil
)))
62 ;; This is the default indent-line-function,
63 ;; used in Fundamental Mode, Text Mode, etc.
64 (defun indent-to-left-margin ()
65 (or (= (current-indentation) left-margin
)
69 (delete-region (point)
70 (progn (skip-chars-forward " \t")
72 (indent-to left-margin
)
77 (defvar indent-region-function nil
78 "Function which is short cut to indent each line in region with TAB.
79 A value of nil means really perform TAB on each line.")
81 (defun indent-region (start end arg
)
82 "Indent each nonblank line in the region.
83 With no argument, indent each line with TAB.
84 \(If there is a fill prefix, make each line start with the fill prefix.)
85 With argument COLUMN, indent each line to that column.
86 Called from a program, takes three args: START, END and COLUMN."
92 (setq end
(point-marker))
94 (let ((regexp (regexp-quote fill-prefix
)))
95 (while (< (point) end
)
96 (or (looking-at regexp
)
99 (if indent-region-function
100 (funcall indent-region-function start end
)
103 (setq end
(point-marker))
105 (or (bolp) (forward-line 1))
106 (while (< (point) end
)
107 (funcall indent-line-function
)
109 (move-marker end nil
))))
110 (setq arg
(prefix-numeric-value arg
))
113 (setq end
(point-marker))
115 (or (bolp) (forward-line 1))
116 (while (< (point) end
)
117 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
121 (move-marker end nil
))))
123 (defun indent-relative-maybe ()
124 "Indent a new line like previous nonblank line."
128 (defun indent-relative (&optional unindented-ok
)
129 "Space out to under next indent point in previous nonblank line.
130 An indent point is a non-whitespace character following whitespace.
131 If the previous nonblank line has no indent points beyond the
132 column point starts at, `tab-to-tab-stop' is done instead."
134 (if abbrev-mode
(expand-abbrev))
135 (let ((start-column (current-column))
139 (if (re-search-backward "^[^\n]" nil t
)
140 (let ((end (save-excursion (forward-line 1) (point))))
141 (move-to-column start-column
)
142 ;; Is start-column inside a tab on this line?
143 (if (> (current-column) start-column
)
145 (or (looking-at "[ \t]")
147 (skip-chars-forward "^ \t" end
))
148 (skip-chars-forward " \t" end
)
149 (or (= (point) end
) (setq indent
(current-column))))))
151 (let ((opoint (point-marker)))
152 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
154 (if (> opoint
(point))
156 (move-marker opoint nil
))
159 (defvar tab-stop-list
160 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
161 "*List of tab stop positions used by `tab-to-tab-stops'.")
163 (defvar edit-tab-stops-map nil
"Keymap used in `edit-tab-stops'.")
164 (if edit-tab-stops-map
166 (setq edit-tab-stops-map
(make-sparse-keymap))
167 (define-key edit-tab-stops-map
"\C-x\C-s" 'edit-tab-stops-note-changes
)
168 (define-key edit-tab-stops-map
"\C-c\C-c" 'edit-tab-stops-note-changes
))
170 (defvar edit-tab-stops-buffer nil
171 "Buffer whose tab stops are being edited--in case
172 the variable `tab-stop-list' is local in that buffer.")
174 (defun edit-tab-stops ()
175 "Edit the tab stops used by `tab-to-tab-stop'.
176 Creates a buffer *Tab Stops* containing text describing the tab stops.
177 A colon indicates a column where there is a tab stop.
178 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
180 (setq edit-tab-stops-buffer
(current-buffer))
181 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
182 (use-local-map edit-tab-stops-map
)
183 (make-local-variable 'indent-tabs-mode
)
184 (setq indent-tabs-mode nil
)
186 (setq truncate-lines t
)
188 (let ((tabs tab-stop-list
))
190 (indent-to (car tabs
) 0)
192 (setq tabs
(cdr tabs
))))
196 (insert (+ count ?
0))
198 (setq count
(1+ count
)))
201 (insert "0123456789")
202 (setq count
(1- count
))))
203 (insert "\nTo install changes, type C-c C-c")
204 (goto-char (point-min)))
206 (defun edit-tab-stops-note-changes ()
207 "Put edited tab stops into effect."
213 (while (search-backward ":" nil t
)
214 (setq tabs
(cons (current-column) tabs
))))
215 (bury-buffer (prog1 (current-buffer)
216 (switch-to-buffer edit-tab-stops-buffer
)))
217 (setq tab-stop-list tabs
))
218 (message "Tab stops installed"))
220 (defun tab-to-tab-stop ()
221 "Insert spaces or tabs to next defined tab-stop column.
222 The variable `tab-stop-list' is a list of columns at which there are tab stops.
223 Use \\[edit-tab-stops] to edit them interactively."
225 (if abbrev-mode
(expand-abbrev))
226 (let ((tabs tab-stop-list
))
227 (while (and tabs
(>= (current-column) (car tabs
)))
228 (setq tabs
(cdr tabs
)))
230 (indent-to (car tabs
))
233 (defun move-to-tab-stop ()
234 "Move point to next defined tab-stop column.
235 The variable `tab-stop-list' is a list of columns at which there are tab stops.
236 Use \\[edit-tab-stops] to edit them interactively."
238 (let ((tabs tab-stop-list
))
239 (while (and tabs
(>= (current-column) (car tabs
)))
240 (setq tabs
(cdr tabs
)))
242 (move-to-column (car tabs
) t
))))
244 (define-key global-map
"\t" 'indent-for-tab-command
)
245 (define-key esc-map
"\034" 'indent-region
)
246 (define-key ctl-x-map
"\t" 'indent-rigidly
)
247 (define-key esc-map
"i" 'tab-to-tab-stop
)
249 ;;; indent.el ends here