660
|
1 ;;; indent.el --- indentation commands for Emacs
|
|
2
|
57297
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
3 ;; Copyright (C) 1985, 1995, 2001, 2004 Free Software Foundation, Inc.
|
845
|
4
|
807
|
5 ;; Maintainer: FSF
|
263
|
6
|
|
7 ;; This file is part of GNU Emacs.
|
|
8
|
|
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
|
807
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
263
|
12 ;; any later version.
|
|
13
|
|
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.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
14169
|
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
64091
|
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
22 ;; Boston, MA 02110-1301, USA.
|
263
|
23
|
2307
|
24 ;;; Commentary:
|
|
25
|
|
26 ;; Commands for making and changing indentation in text. These are
|
|
27 ;; described in the Emacs manual.
|
|
28
|
807
|
29 ;;; Code:
|
263
|
30
|
19420
|
31 (defgroup indent nil
|
64012
|
32 "Indentation commands."
|
19420
|
33 :group 'editing)
|
10467
|
34
|
19420
|
35 (defcustom standard-indent 4
|
|
36 "*Default number of columns for margin-changing functions to indent."
|
|
37 :group 'indent
|
|
38 :type 'integer)
|
|
39
|
41845
23120b6a0225
(indent-line-function): Default is indent-relative again.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
40 (defvar indent-line-function 'indent-relative
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
41 "Function to indent the current line.
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
42 This function will be called with no arguments.
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
43 If it is called somewhere where auto-indentation cannot be done
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
44 \(f.ex. inside a string), the function should simply return `noindent'.
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
45 Setting this function is all you need to make TAB indent appropriately.
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
46 Don't rebind TAB unless you really need to.")
|
263
|
47
|
32242
|
48 (defcustom tab-always-indent t
|
|
49 "*Controls the operation of the TAB key.
|
|
50 If t, hitting TAB always just indents the current line.
|
|
51 If nil, hitting TAB indents the current line if point is at the left margin
|
50140
f94aa8667875
(indent-for-tab-command): If tab-always-indent is non-nil
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
52 or in the line's indentation, otherwise it insert a \"real\" tab character."
|
32242
|
53 :group 'indent
|
40636
|
54 :type '(choice (const nil) (const t) (const always)))
|
32242
|
55
|
263
|
56 (defun indent-according-to-mode ()
|
48581
|
57 "Indent line in proper way for current major mode.
|
|
58 The buffer-local variable `indent-line-function' determines how to do this,
|
|
59 but the functions `indent-relative' and `indent-relative-maybe' are
|
|
60 special; we don't actually use them here."
|
263
|
61 (interactive)
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
62 (if (memq indent-line-function
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
63 '(indent-relative indent-relative-maybe))
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
64 ;; These functions are used for tabbing, but can't be used for
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
65 ;; indenting. Replace with something ad-hoc.
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
66 (let ((column (save-excursion
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
67 (beginning-of-line)
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
68 (skip-chars-backward "\n \t")
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
69 (beginning-of-line)
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
70 (current-indentation))))
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
71 (if (<= (current-column) (current-indentation))
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
72 (indent-line-to column)
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
73 (save-excursion (indent-line-to column))))
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
74 ;; The normal case.
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
75 (funcall indent-line-function)))
|
263
|
76
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
77 (defun indent-for-tab-command (&optional arg)
|
32242
|
78 "Indent line in proper way for current major mode or insert a tab.
|
|
79 Depending on `tab-always-indent', either insert a tab or indent.
|
31593
|
80 If initial point was within line's indentation, position after
|
|
81 the indentation. Else stay at same point in text.
|
32242
|
82 The function actually called to indent is determined by the value of
|
30690
|
83 `indent-line-function'."
|
13035
|
84 (interactive "P")
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
85 (cond
|
40636
|
86 ((or ;; indent-to-left-margin is only meant for indenting,
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
87 ;; so we force it to always insert a tab here.
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
88 (eq indent-line-function 'indent-to-left-margin)
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
89 (and (not tab-always-indent)
|
50140
f94aa8667875
(indent-for-tab-command): If tab-always-indent is non-nil
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
90 (or (> (current-column) (current-indentation))
|
f94aa8667875
(indent-for-tab-command): If tab-always-indent is non-nil
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
91 (eq this-command last-command))))
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
92 (insert-tab arg))
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
93 ;; Those functions are meant specifically for tabbing and not for
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
94 ;; indenting, so we can't pass them to indent-according-to-mode.
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
95 ((memq indent-line-function '(indent-relative indent-relative-maybe))
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
96 (funcall indent-line-function))
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
97 (t ;; The normal case.
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
98 (indent-according-to-mode))))
|
263
|
99
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
100 (defun insert-tab (&optional arg)
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
101 (let ((count (prefix-numeric-value arg)))
|
19508
|
102 (if (and abbrev-mode
|
|
103 (eq (char-syntax (preceding-char)) ?w))
|
15106
|
104 (expand-abbrev))
|
|
105 (if indent-tabs-mode
|
15113
|
106 (insert-char ?\t count)
|
15106
|
107 (indent-to (* tab-width (+ count (/ (current-column) tab-width)))))))
|
263
|
108
|
|
109 (defun indent-rigidly (start end arg)
|
|
110 "Indent all lines starting in the region sideways by ARG columns.
|
45438
|
111 Called from a program, takes three arguments, START, END and ARG.
|
|
112 You can remove all indentation from a region by giving a large negative ARG."
|
263
|
113 (interactive "r\np")
|
|
114 (save-excursion
|
|
115 (goto-char end)
|
|
116 (setq end (point-marker))
|
|
117 (goto-char start)
|
|
118 (or (bolp) (forward-line 1))
|
|
119 (while (< (point) end)
|
8642
|
120 (let ((indent (current-indentation))
|
|
121 eol-flag)
|
|
122 (save-excursion
|
|
123 (skip-chars-forward " \t")
|
|
124 (setq eol-flag (eolp)))
|
|
125 (or eol-flag
|
|
126 (indent-to (max 0 (+ indent arg)) 0))
|
|
127 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
|
263
|
128 (forward-line 1))
|
|
129 (move-marker end nil)))
|
|
130
|
10467
|
131 (defun indent-line-to (column)
|
|
132 "Indent current line to COLUMN.
|
|
133 This function removes or adds spaces and tabs at beginning of line
|
|
134 only if necessary. It leaves point at end of indentation."
|
11043
4c0a98538670
(indent-line-to): move to end of indentation, even if it didn't change.
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
135 (back-to-indentation)
|
4c0a98538670
(indent-line-to): move to end of indentation, even if it didn't change.
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
136 (let ((cur-col (current-column)))
|
4c0a98538670
(indent-line-to): move to end of indentation, even if it didn't change.
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
137 (cond ((< cur-col column)
|
17155
|
138 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
|
13545
|
139 (delete-region (point)
|
|
140 (progn (skip-chars-backward " ") (point))))
|
11043
4c0a98538670
(indent-line-to): move to end of indentation, even if it didn't change.
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
141 (indent-to column))
|
4c0a98538670
(indent-line-to): move to end of indentation, even if it didn't change.
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
142 ((> cur-col column) ; too far right (after tab?)
|
10812
|
143 (delete-region (progn (move-to-column column t) (point))
|
11043
4c0a98538670
(indent-line-to): move to end of indentation, even if it didn't change.
Boris Goldowsky <boris@gnu.org>
diff
changeset
|
144 (progn (back-to-indentation) (point)))))))
|
10467
|
145
|
|
146 (defun current-left-margin ()
|
|
147 "Return the left margin to use for this line.
|
|
148 This is the value of the buffer-local variable `left-margin' plus the value
|
|
149 of the `left-margin' text-property at the start of the line."
|
|
150 (save-excursion
|
|
151 (back-to-indentation)
|
|
152 (max 0
|
10812
|
153 (+ left-margin (or (get-text-property
|
|
154 (if (and (eobp) (not (bobp)))
|
|
155 (1- (point)) (point))
|
|
156 'left-margin) 0)))))
|
10467
|
157
|
10812
|
158 (defun move-to-left-margin (&optional n force)
|
10467
|
159 "Move to the left margin of the current line.
|
|
160 With optional argument, move forward N-1 lines first.
|
10812
|
161 The column moved to is the one given by the `current-left-margin' function.
|
|
162 If the line's indentation appears to be wrong, and this command is called
|
|
163 interactively or with optional argument FORCE, it will be fixed."
|
|
164 (interactive (list (prefix-numeric-value current-prefix-arg) t))
|
10467
|
165 (beginning-of-line n)
|
13577
|
166 (skip-chars-forward " \t")
|
57164
|
167 (if (minibufferp (current-buffer))
|
|
168 (if (save-excursion (beginning-of-line) (bobp))
|
|
169 (goto-char (minibuffer-prompt-end))
|
|
170 (beginning-of-line))
|
|
171 (let ((lm (current-left-margin))
|
|
172 (cc (current-column)))
|
|
173 (cond ((> cc lm)
|
|
174 (if (> (move-to-column lm force) lm)
|
|
175 ;; If lm is in a tab and we are not forcing, move before tab
|
|
176 (backward-char 1)))
|
|
177 ((and force (< cc lm))
|
|
178 (indent-to-left-margin))))))
|
10467
|
179
|
50140
f94aa8667875
(indent-for-tab-command): If tab-always-indent is non-nil
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
180 ;; This used to be the default indent-line-function,
|
263
|
181 ;; used in Fundamental Mode, Text Mode, etc.
|
|
182 (defun indent-to-left-margin ()
|
10812
|
183 "Indent current line to the column given by `current-left-margin'."
|
10467
|
184 (indent-line-to (current-left-margin)))
|
|
185
|
10812
|
186 (defun delete-to-left-margin (&optional from to)
|
|
187 "Remove left margin indentation from a region.
|
|
188 This deletes to the column given by `current-left-margin'.
|
|
189 In no case will it delete non-whitespace.
|
|
190 Args FROM and TO are optional; default is the whole buffer."
|
10467
|
191 (save-excursion
|
10812
|
192 (goto-char (or to (point-max)))
|
10467
|
193 (setq to (point-marker))
|
10812
|
194 (goto-char (or from (point-min)))
|
10467
|
195 (or (bolp) (forward-line 1))
|
|
196 (while (< (point) to)
|
10812
|
197 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
|
10467
|
198 (forward-line 1))
|
|
199 (move-marker to nil)))
|
|
200
|
57297
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
201 (defun set-left-margin (from to width)
|
10467
|
202 "Set the left margin of the region to WIDTH.
|
57297
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
203 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
|
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
204
|
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
205 Interactively, WIDTH is the prefix argument, if specified.
|
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
206 Without prefix argument, the command prompts for WIDTH."
|
10467
|
207 (interactive "r\nNSet left margin to column: ")
|
|
208 (save-excursion
|
|
209 ;; If inside indentation, start from BOL.
|
|
210 (goto-char from)
|
|
211 (skip-chars-backward " \t")
|
|
212 (if (bolp) (setq from (point)))
|
10812
|
213 ;; Place end after whitespace
|
10467
|
214 (goto-char to)
|
10812
|
215 (skip-chars-forward " \t")
|
10467
|
216 (setq to (point-marker)))
|
10812
|
217 ;; Delete margin indentation first, but keep paragraph indentation.
|
|
218 (delete-to-left-margin from to)
|
57297
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
219 (put-text-property from to 'left-margin width)
|
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
220 (indent-rigidly from to width)
|
10812
|
221 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
|
10467
|
222 (move-marker to nil))
|
|
223
|
57297
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
224 (defun set-right-margin (from to width)
|
10467
|
225 "Set the right margin of the region to WIDTH.
|
57297
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
226 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
|
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
227
|
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
228 Interactively, WIDTH is the prefix argument, if specified.
|
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
229 Without prefix argument, the command prompts for WIDTH."
|
11281
|
230 (interactive "r\nNSet right margin to width: ")
|
10467
|
231 (save-excursion
|
|
232 (goto-char from)
|
|
233 (skip-chars-backward " \t")
|
|
234 (if (bolp) (setq from (point))))
|
57297
42311c6c4bdb
(set-left-margin, set-right-margin): Rename `lm' arg to `width' for
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
235 (put-text-property from to 'right-margin width)
|
10812
|
236 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
|
10467
|
237
|
|
238 (defun alter-text-property (from to prop func &optional object)
|
|
239 "Programmatically change value of a text-property.
|
|
240 For each region between FROM and TO that has a single value for PROPERTY,
|
|
241 apply FUNCTION to that value and sets the property to the function's result.
|
|
242 Optional fifth argument OBJECT specifies the string or buffer to operate on."
|
|
243 (let ((begin from)
|
|
244 end val)
|
|
245 (while (setq val (get-text-property begin prop object)
|
|
246 end (text-property-not-all begin to prop val object))
|
|
247 (put-text-property begin end prop (funcall func val) object)
|
|
248 (setq begin end))
|
|
249 (if (< begin to)
|
|
250 (put-text-property begin to prop (funcall func val) object))))
|
|
251
|
|
252 (defun increase-left-margin (from to inc)
|
|
253 "Increase or decrease the left-margin of the region.
|
|
254 With no prefix argument, this adds `standard-indent' of indentation.
|
|
255 A prefix arg (optional third arg INC noninteractively) specifies the amount
|
|
256 to change the margin by, in characters.
|
|
257 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
|
|
258 (interactive "*r\nP")
|
|
259 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
|
|
260 (save-excursion
|
|
261 (goto-char from)
|
|
262 (skip-chars-backward " \t")
|
|
263 (if (bolp) (setq from (point)))
|
|
264 (goto-char to)
|
|
265 (setq to (point-marker)))
|
|
266 (alter-text-property from to 'left-margin
|
10812
|
267 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
|
|
268 (indent-rigidly from to inc)
|
|
269 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
|
10467
|
270 (move-marker to nil))
|
|
271
|
|
272 (defun decrease-left-margin (from to inc)
|
|
273 "Make the left margin of the region smaller.
|
|
274 With no prefix argument, decrease the indentation by `standard-indent'.
|
|
275 A prefix arg (optional third arg INC noninteractively) specifies the amount
|
|
276 to change the margin by, in characters.
|
|
277 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
|
|
278 (interactive "*r\nP")
|
|
279 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
|
|
280 (increase-left-margin from to (- inc)))
|
|
281
|
|
282 (defun increase-right-margin (from to inc)
|
|
283 "Increase the right-margin of the region.
|
|
284 With no prefix argument, increase the right margin by `standard-indent'.
|
|
285 A prefix arg (optional third arg INC noninteractively) specifies the amount
|
|
286 to change the margin by, in characters. A negative argument decreases
|
|
287 the right margin width.
|
|
288 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
|
|
289 (interactive "r\nP")
|
57676
|
290 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
|
10467
|
291 (save-excursion
|
|
292 (alter-text-property from to 'right-margin
|
57676
|
293 (lambda (v) (+ inc (or v 0))))
|
10467
|
294 (if auto-fill-function
|
|
295 (fill-region from to nil t t))))
|
|
296
|
|
297 (defun decrease-right-margin (from to inc)
|
|
298 "Make the right margin of the region smaller.
|
|
299 With no prefix argument, decrease the right margin by `standard-indent'.
|
|
300 A prefix arg (optional third arg INC noninteractively) specifies the amount
|
|
301 of width to remove, in characters. A negative argument increases
|
|
302 the right margin width.
|
|
303 If `auto-fill-mode' is active, re-fills region to fit in new margin."
|
|
304 (interactive "*r\nP")
|
|
305 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
|
|
306 (increase-right-margin from to (- inc)))
|
263
|
307
|
10812
|
308 (defun beginning-of-line-text (&optional n)
|
|
309 "Move to the beginning of the text on this line.
|
|
310 With optional argument, move forward N-1 lines first.
|
|
311 From the beginning of the line, moves past the left-margin indentation, the
|
|
312 fill-prefix, and any indentation used for centering or right-justifying the
|
49597
|
313 line, but does not move past any whitespace that was explicitly inserted
|
10812
|
314 \(such as a tab used to indent the first line of a paragraph)."
|
|
315 (interactive "p")
|
|
316 (beginning-of-line n)
|
|
317 (skip-chars-forward " \t")
|
|
318 ;; Skip over fill-prefix.
|
49597
|
319 (if (and fill-prefix
|
10812
|
320 (not (string-equal fill-prefix "")))
|
|
321 (if (equal fill-prefix
|
49597
|
322 (buffer-substring
|
10812
|
323 (point) (min (point-max) (+ (length fill-prefix) (point)))))
|
|
324 (forward-char (length fill-prefix)))
|
14770
e3567339daa7
(beginning-of-line-text): Check adaptive-fill-regexp is non-nil.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
325 (if (and adaptive-fill-mode adaptive-fill-regexp
|
10812
|
326 (looking-at adaptive-fill-regexp))
|
|
327 (goto-char (match-end 0))))
|
|
328 ;; Skip centering or flushright indentation
|
|
329 (if (memq (current-justification) '(center right))
|
|
330 (skip-chars-forward " \t")))
|
|
331
|
263
|
332 (defvar indent-region-function nil
|
12683
|
333 "Short cut function to indent region using `indent-according-to-mode'.
|
|
334 A value of nil means really run `indent-according-to-mode' on each line.")
|
263
|
335
|
45367
|
336 (defun indent-region (start end &optional column)
|
263
|
337 "Indent each nonblank line in the region.
|
48581
|
338 A numeric prefix argument specifies a column: indent each line to that column.
|
|
339
|
|
340 With no prefix argument, the command chooses one of these methods and
|
|
341 indents all the lines with it:
|
26169
|
342
|
48581
|
343 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
|
|
344 beginning of each line in the region that does not already begin
|
|
345 with it.
|
|
346 2) If `indent-region-function' is non-nil, call that function
|
|
347 to indent the region.
|
|
348 3) Indent each line as specified by the variable `indent-line-function'.
|
|
349
|
|
350 Called from a program, START and END specify the region to indent.
|
|
351 If the third argument COLUMN is an integer, it specifies the
|
|
352 column to indent to; if it is nil, use one of the three methods above."
|
263
|
353 (interactive "r\nP")
|
4465
|
354 (if (null column)
|
263
|
355 (if fill-prefix
|
|
356 (save-excursion
|
|
357 (goto-char end)
|
|
358 (setq end (point-marker))
|
|
359 (goto-char start)
|
|
360 (let ((regexp (regexp-quote fill-prefix)))
|
4465
|
361 (while (< (point) end)
|
|
362 (or (looking-at regexp)
|
|
363 (and (bolp) (eolp))
|
|
364 (insert fill-prefix))
|
|
365 (forward-line 1))))
|
263
|
366 (if indent-region-function
|
|
367 (funcall indent-region-function start end)
|
|
368 (save-excursion
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
369 (setq end (copy-marker end))
|
4465
|
370 (goto-char start)
|
|
371 (while (< (point) end)
|
4473
|
372 (or (and (bolp) (eolp))
|
4465
|
373 (funcall indent-line-function))
|
|
374 (forward-line 1))
|
|
375 (move-marker end nil))))
|
|
376 (setq column (prefix-numeric-value column))
|
263
|
377 (save-excursion
|
|
378 (goto-char end)
|
|
379 (setq end (point-marker))
|
|
380 (goto-char start)
|
|
381 (or (bolp) (forward-line 1))
|
|
382 (while (< (point) end)
|
|
383 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
|
|
384 (or (eolp)
|
10467
|
385 (indent-to column 0))
|
263
|
386 (forward-line 1))
|
|
387 (move-marker end nil))))
|
|
388
|
|
389 (defun indent-relative-maybe ()
|
38151
|
390 "Indent a new line like previous nonblank line.
|
|
391 If the previous nonblank line has no indent points beyond the
|
|
392 column point starts at, this command does nothing.
|
|
393
|
|
394 See also `indent-relative'."
|
263
|
395 (interactive)
|
|
396 (indent-relative t))
|
|
397
|
|
398 (defun indent-relative (&optional unindented-ok)
|
|
399 "Space out to under next indent point in previous nonblank line.
|
|
400 An indent point is a non-whitespace character following whitespace.
|
25498
|
401 The following line shows the indentation points in this line.
|
|
402 ^ ^ ^ ^ ^ ^ ^ ^ ^
|
263
|
403 If the previous nonblank line has no indent points beyond the
|
38151
|
404 column point starts at, `tab-to-tab-stop' is done instead, unless
|
|
405 this command is invoked with a numeric argument, in which case it
|
|
406 does nothing.
|
|
407
|
|
408 See also `indent-relative-maybe'."
|
263
|
409 (interactive "P")
|
19508
|
410 (if (and abbrev-mode
|
|
411 (eq (char-syntax (preceding-char)) ?w))
|
|
412 (expand-abbrev))
|
263
|
413 (let ((start-column (current-column))
|
|
414 indent)
|
|
415 (save-excursion
|
|
416 (beginning-of-line)
|
|
417 (if (re-search-backward "^[^\n]" nil t)
|
|
418 (let ((end (save-excursion (forward-line 1) (point))))
|
|
419 (move-to-column start-column)
|
|
420 ;; Is start-column inside a tab on this line?
|
|
421 (if (> (current-column) start-column)
|
|
422 (backward-char 1))
|
|
423 (or (looking-at "[ \t]")
|
|
424 unindented-ok
|
|
425 (skip-chars-forward "^ \t" end))
|
|
426 (skip-chars-forward " \t" end)
|
|
427 (or (= (point) end) (setq indent (current-column))))))
|
|
428 (if indent
|
|
429 (let ((opoint (point-marker)))
|
|
430 (indent-to indent 0)
|
|
431 (if (> opoint (point))
|
|
432 (goto-char opoint))
|
|
433 (move-marker opoint nil))
|
|
434 (tab-to-tab-stop))))
|
|
435
|
19420
|
436 (defcustom tab-stop-list
|
263
|
437 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
|
19895
|
438 "*List of tab stop positions used by `tab-to-tab-stop'.
|
19420
|
439 This should be a list of integers, ordered from smallest to largest."
|
|
440 :group 'indent
|
|
441 :type '(repeat integer))
|
263
|
442
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
443 (defvar edit-tab-stops-map
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
444 (let ((map (make-sparse-keymap)))
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
445 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
446 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
447 map)
|
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
448 "Keymap used in `edit-tab-stops'.")
|
263
|
449
|
|
450 (defvar edit-tab-stops-buffer nil
|
56831
|
451 "Buffer whose tab stops are being edited.
|
|
452 This matters if the variable `tab-stop-list' is local in that buffer.")
|
263
|
453
|
|
454 (defun edit-tab-stops ()
|
|
455 "Edit the tab stops used by `tab-to-tab-stop'.
|
|
456 Creates a buffer *Tab Stops* containing text describing the tab stops.
|
|
457 A colon indicates a column where there is a tab stop.
|
|
458 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
|
|
459 (interactive)
|
|
460 (setq edit-tab-stops-buffer (current-buffer))
|
|
461 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
|
|
462 (use-local-map edit-tab-stops-map)
|
|
463 (make-local-variable 'indent-tabs-mode)
|
|
464 (setq indent-tabs-mode nil)
|
|
465 (overwrite-mode 1)
|
|
466 (setq truncate-lines t)
|
|
467 (erase-buffer)
|
|
468 (let ((tabs tab-stop-list))
|
|
469 (while tabs
|
|
470 (indent-to (car tabs) 0)
|
|
471 (insert ?:)
|
|
472 (setq tabs (cdr tabs))))
|
|
473 (let ((count 0))
|
|
474 (insert ?\n)
|
|
475 (while (< count 8)
|
|
476 (insert (+ count ?0))
|
|
477 (insert " ")
|
|
478 (setq count (1+ count)))
|
|
479 (insert ?\n)
|
|
480 (while (> count 0)
|
|
481 (insert "0123456789")
|
|
482 (setq count (1- count))))
|
|
483 (insert "\nTo install changes, type C-c C-c")
|
|
484 (goto-char (point-min)))
|
|
485
|
|
486 (defun edit-tab-stops-note-changes ()
|
|
487 "Put edited tab stops into effect."
|
|
488 (interactive)
|
|
489 (let (tabs)
|
|
490 (save-excursion
|
|
491 (goto-char 1)
|
|
492 (end-of-line)
|
|
493 (while (search-backward ":" nil t)
|
|
494 (setq tabs (cons (current-column) tabs))))
|
|
495 (bury-buffer (prog1 (current-buffer)
|
|
496 (switch-to-buffer edit-tab-stops-buffer)))
|
|
497 (setq tab-stop-list tabs))
|
|
498 (message "Tab stops installed"))
|
|
499
|
|
500 (defun tab-to-tab-stop ()
|
|
501 "Insert spaces or tabs to next defined tab-stop column.
|
|
502 The variable `tab-stop-list' is a list of columns at which there are tab stops.
|
|
503 Use \\[edit-tab-stops] to edit them interactively."
|
|
504 (interactive)
|
12778
|
505 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
|
|
506 (expand-abbrev))
|
263
|
507 (let ((tabs tab-stop-list))
|
|
508 (while (and tabs (>= (current-column) (car tabs)))
|
|
509 (setq tabs (cdr tabs)))
|
|
510 (if tabs
|
7973
|
511 (let ((opoint (point)))
|
41758
|
512 (delete-horizontal-space t)
|
7973
|
513 (indent-to (car tabs)))
|
263
|
514 (insert ?\ ))))
|
|
515
|
|
516 (defun move-to-tab-stop ()
|
|
517 "Move point to next defined tab-stop column.
|
|
518 The variable `tab-stop-list' is a list of columns at which there are tab stops.
|
|
519 Use \\[edit-tab-stops] to edit them interactively."
|
|
520 (interactive)
|
|
521 (let ((tabs tab-stop-list))
|
|
522 (while (and tabs (>= (current-column) (car tabs)))
|
|
523 (setq tabs (cdr tabs)))
|
|
524 (if tabs
|
8037
|
525 (let ((before (point)))
|
|
526 (move-to-column (car tabs) t)
|
|
527 (save-excursion
|
|
528 (goto-char before)
|
|
529 ;; If we just added a tab, or moved over one,
|
|
530 ;; delete any superfluous spaces before the old point.
|
|
531 (if (and (eq (preceding-char) ?\ )
|
|
532 (eq (following-char) ?\t))
|
|
533 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
|
|
534 (while (and (> (current-column) tabend)
|
|
535 (eq (preceding-char) ?\ ))
|
|
536 (forward-char -1))
|
|
537 (delete-region (point) before))))))))
|
|
538
|
263
|
539 (define-key global-map "\t" 'indent-for-tab-command)
|
40482
ec3016b59706
(indent-line-function): Change default to indent-relative.
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
540 (define-key esc-map "\C-\\" 'indent-region)
|
263
|
541 (define-key ctl-x-map "\t" 'indent-rigidly)
|
|
542 (define-key esc-map "i" 'tab-to-tab-stop)
|
660
|
543
|
52401
|
544 ;;; arch-tag: f402b2a7-e44f-492f-b5b8-38996020b7c3
|
660
|
545 ;;; indent.el ends here
|