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