(paragraph-indent-minor-mode): Don't set paragraph-separate.
[bpt/emacs.git] / lisp / indent.el
CommitLineData
1a06eabd
ER
1;;; indent.el --- indentation commands for Emacs
2
61294601 3;; Copyright (C) 1985, 1995, 2001 Free Software Foundation, Inc.
483e630e 4
9750e079
ER
5;; Maintainer: FSF
6
483e630e
RM
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
e5167999 11;; the Free Software Foundation; either version 2, or (at your option)
483e630e
RM
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
b578f267
EN
20;; along with GNU Emacs; see the file COPYING. If not, write to the
21;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22;; Boston, MA 02111-1307, USA.
483e630e 23
e41b2db1
ER
24;;; Commentary:
25
26;; Commands for making and changing indentation in text. These are
27;; described in the Emacs manual.
28
e5167999 29;;; Code:
483e630e 30
ddb3136e
RS
31(defgroup indent nil
32 "Indentation commands"
33 :group 'editing)
106b6d0e 34
ddb3136e
RS
35(defcustom standard-indent 4
36 "*Default number of columns for margin-changing functions to indent."
37 :group 'indent
38 :type 'integer)
39
40(defvar indent-line-function 'indent-to-left-margin
41 "Function to indent current line.")
483e630e 42
20b1d079
SM
43(defcustom tab-always-indent t
44 "*Controls the operation of the TAB key.
45If t, hitting TAB always just indents the current line.
46If nil, hitting TAB indents the current line if point is at the left margin
47 or in the line's indentation, otherwise it insert a `real' tab character."
48 :group 'indent
49 :type 'boolean)
50
483e630e
RM
51(defun indent-according-to-mode ()
52 "Indent line in proper way for current major mode."
53 (interactive)
54 (funcall indent-line-function))
55
34a71c1e 56(defun indent-for-tab-command (&optional prefix-arg)
20b1d079
SM
57 "Indent line in proper way for current major mode or insert a tab.
58Depending on `tab-always-indent', either insert a tab or indent.
b9ce5694
GM
59If initial point was within line's indentation, position after
60the indentation. Else stay at same point in text.
20b1d079 61The function actually called to indent is determined by the value of
eca1e29e 62`indent-line-function'."
34a71c1e 63 (interactive "P")
20b1d079
SM
64 (if (or (eq indent-line-function 'indent-to-left-margin)
65 (and (not tab-always-indent)
66 (> (current-column) (current-indentation))))
10992b01 67 (insert-tab prefix-arg)
61294601 68 (funcall indent-line-function)))
483e630e 69
10992b01
RS
70(defun insert-tab (&optional prefix-arg)
71 (let ((count (prefix-numeric-value prefix-arg)))
5e9f80a8
RS
72 (if (and abbrev-mode
73 (eq (char-syntax (preceding-char)) ?w))
10992b01
RS
74 (expand-abbrev))
75 (if indent-tabs-mode
4424ce6f 76 (insert-char ?\t count)
10992b01 77 (indent-to (* tab-width (+ count (/ (current-column) tab-width)))))))
483e630e
RM
78
79(defun indent-rigidly (start end arg)
80 "Indent all lines starting in the region sideways by ARG columns.
81Called from a program, takes three arguments, START, END and ARG."
82 (interactive "r\np")
83 (save-excursion
84 (goto-char end)
85 (setq end (point-marker))
86 (goto-char start)
87 (or (bolp) (forward-line 1))
88 (while (< (point) end)
c1194ae8
RS
89 (let ((indent (current-indentation))
90 eol-flag)
91 (save-excursion
92 (skip-chars-forward " \t")
93 (setq eol-flag (eolp)))
94 (or eol-flag
95 (indent-to (max 0 (+ indent arg)) 0))
96 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
483e630e
RM
97 (forward-line 1))
98 (move-marker end nil)))
99
106b6d0e
RS
100(defun indent-line-to (column)
101 "Indent current line to COLUMN.
102This function removes or adds spaces and tabs at beginning of line
103only if necessary. It leaves point at end of indentation."
ca2a3cb7
BG
104 (back-to-indentation)
105 (let ((cur-col (current-column)))
106 (cond ((< cur-col column)
cd6d305e 107 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
a80a30b3
RS
108 (delete-region (point)
109 (progn (skip-chars-backward " ") (point))))
ca2a3cb7
BG
110 (indent-to column))
111 ((> cur-col column) ; too far right (after tab?)
34c3f2b8 112 (delete-region (progn (move-to-column column t) (point))
ca2a3cb7 113 (progn (back-to-indentation) (point)))))))
106b6d0e
RS
114
115(defun current-left-margin ()
116 "Return the left margin to use for this line.
117This is the value of the buffer-local variable `left-margin' plus the value
118of the `left-margin' text-property at the start of the line."
119 (save-excursion
120 (back-to-indentation)
121 (max 0
34c3f2b8
BG
122 (+ left-margin (or (get-text-property
123 (if (and (eobp) (not (bobp)))
124 (1- (point)) (point))
125 'left-margin) 0)))))
106b6d0e 126
34c3f2b8 127(defun move-to-left-margin (&optional n force)
106b6d0e
RS
128 "Move to the left margin of the current line.
129With optional argument, move forward N-1 lines first.
34c3f2b8
BG
130The column moved to is the one given by the `current-left-margin' function.
131If the line's indentation appears to be wrong, and this command is called
132interactively or with optional argument FORCE, it will be fixed."
133 (interactive (list (prefix-numeric-value current-prefix-arg) t))
106b6d0e 134 (beginning-of-line n)
54505d72
RS
135 (skip-chars-forward " \t")
136 (let ((lm (current-left-margin))
137 (cc (current-column)))
138 (cond ((> cc lm)
139 (if (> (move-to-column lm force) lm)
140 ;; If lm is in a tab and we are not forcing, move before tab
141 (backward-char 1)))
142 ((and force (< cc lm))
143 (indent-to-left-margin)))))
106b6d0e 144
483e630e
RM
145;; This is the default indent-line-function,
146;; used in Fundamental Mode, Text Mode, etc.
147(defun indent-to-left-margin ()
34c3f2b8 148 "Indent current line to the column given by `current-left-margin'."
106b6d0e
RS
149 (indent-line-to (current-left-margin)))
150
34c3f2b8
BG
151(defun delete-to-left-margin (&optional from to)
152 "Remove left margin indentation from a region.
153This deletes to the column given by `current-left-margin'.
154In no case will it delete non-whitespace.
155Args FROM and TO are optional; default is the whole buffer."
106b6d0e 156 (save-excursion
34c3f2b8 157 (goto-char (or to (point-max)))
106b6d0e 158 (setq to (point-marker))
34c3f2b8 159 (goto-char (or from (point-min)))
106b6d0e
RS
160 (or (bolp) (forward-line 1))
161 (while (< (point) to)
34c3f2b8 162 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
106b6d0e
RS
163 (forward-line 1))
164 (move-marker to nil)))
165
166(defun set-left-margin (from to lm)
167 "Set the left margin of the region to WIDTH.
168If `auto-fill-mode' is active, re-fill the region to fit the new margin."
169 (interactive "r\nNSet left margin to column: ")
170 (if (interactive-p) (setq lm (prefix-numeric-value lm)))
171 (save-excursion
172 ;; If inside indentation, start from BOL.
173 (goto-char from)
174 (skip-chars-backward " \t")
175 (if (bolp) (setq from (point)))
34c3f2b8 176 ;; Place end after whitespace
106b6d0e 177 (goto-char to)
34c3f2b8 178 (skip-chars-forward " \t")
106b6d0e 179 (setq to (point-marker)))
34c3f2b8
BG
180 ;; Delete margin indentation first, but keep paragraph indentation.
181 (delete-to-left-margin from to)
106b6d0e 182 (put-text-property from to 'left-margin lm)
34c3f2b8
BG
183 (indent-rigidly from to lm)
184 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
106b6d0e
RS
185 (move-marker to nil))
186
187(defun set-right-margin (from to lm)
188 "Set the right margin of the region to WIDTH.
189If `auto-fill-mode' is active, re-fill the region to fit the new margin."
8cf19007 190 (interactive "r\nNSet right margin to width: ")
106b6d0e
RS
191 (if (interactive-p) (setq lm (prefix-numeric-value lm)))
192 (save-excursion
193 (goto-char from)
194 (skip-chars-backward " \t")
195 (if (bolp) (setq from (point))))
196 (put-text-property from to 'right-margin lm)
34c3f2b8 197 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
106b6d0e
RS
198
199(defun alter-text-property (from to prop func &optional object)
200 "Programmatically change value of a text-property.
201For each region between FROM and TO that has a single value for PROPERTY,
202apply FUNCTION to that value and sets the property to the function's result.
203Optional fifth argument OBJECT specifies the string or buffer to operate on."
204 (let ((begin from)
205 end val)
206 (while (setq val (get-text-property begin prop object)
207 end (text-property-not-all begin to prop val object))
208 (put-text-property begin end prop (funcall func val) object)
209 (setq begin end))
210 (if (< begin to)
211 (put-text-property begin to prop (funcall func val) object))))
212
213(defun increase-left-margin (from to inc)
214 "Increase or decrease the left-margin of the region.
215With no prefix argument, this adds `standard-indent' of indentation.
216A prefix arg (optional third arg INC noninteractively) specifies the amount
217to change the margin by, in characters.
218If `auto-fill-mode' is active, re-fill the region to fit the new margin."
219 (interactive "*r\nP")
220 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
221 (save-excursion
222 (goto-char from)
223 (skip-chars-backward " \t")
224 (if (bolp) (setq from (point)))
225 (goto-char to)
226 (setq to (point-marker)))
106b6d0e 227 (alter-text-property from to 'left-margin
34c3f2b8
BG
228 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
229 (indent-rigidly from to inc)
230 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
106b6d0e
RS
231 (move-marker to nil))
232
233(defun decrease-left-margin (from to inc)
234 "Make the left margin of the region smaller.
235With no prefix argument, decrease the indentation by `standard-indent'.
236A prefix arg (optional third arg INC noninteractively) specifies the amount
237to change the margin by, in characters.
238If `auto-fill-mode' is active, re-fill the region to fit the new margin."
239 (interactive "*r\nP")
240 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
241 (increase-left-margin from to (- inc)))
242
243(defun increase-right-margin (from to inc)
244 "Increase the right-margin of the region.
245With no prefix argument, increase the right margin by `standard-indent'.
246A prefix arg (optional third arg INC noninteractively) specifies the amount
247to change the margin by, in characters. A negative argument decreases
248the right margin width.
249If `auto-fill-mode' is active, re-fill the region to fit the new margin."
250 (interactive "r\nP")
251 (if (interactive-p)
252 (setq inc (if inc (prefix-numeric-value current-prefix-arg)
253 standard-indent)))
254 (save-excursion
255 (alter-text-property from to 'right-margin
34c3f2b8 256 (lambda (v) (+ inc (or v 0))))
106b6d0e
RS
257 (if auto-fill-function
258 (fill-region from to nil t t))))
259
260(defun decrease-right-margin (from to inc)
261 "Make the right margin of the region smaller.
262With no prefix argument, decrease the right margin by `standard-indent'.
263A prefix arg (optional third arg INC noninteractively) specifies the amount
264of width to remove, in characters. A negative argument increases
265the right margin width.
266If `auto-fill-mode' is active, re-fills region to fit in new margin."
267 (interactive "*r\nP")
268 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
269 (increase-right-margin from to (- inc)))
483e630e 270
34c3f2b8
BG
271(defun beginning-of-line-text (&optional n)
272 "Move to the beginning of the text on this line.
273With optional argument, move forward N-1 lines first.
274From the beginning of the line, moves past the left-margin indentation, the
275fill-prefix, and any indentation used for centering or right-justifying the
276line, but does not move past any whitespace that was explicitly inserted
277\(such as a tab used to indent the first line of a paragraph)."
278 (interactive "p")
279 (beginning-of-line n)
280 (skip-chars-forward " \t")
281 ;; Skip over fill-prefix.
282 (if (and fill-prefix
283 (not (string-equal fill-prefix "")))
284 (if (equal fill-prefix
285 (buffer-substring
286 (point) (min (point-max) (+ (length fill-prefix) (point)))))
287 (forward-char (length fill-prefix)))
7d43e2ad 288 (if (and adaptive-fill-mode adaptive-fill-regexp
34c3f2b8
BG
289 (looking-at adaptive-fill-regexp))
290 (goto-char (match-end 0))))
291 ;; Skip centering or flushright indentation
292 (if (memq (current-justification) '(center right))
293 (skip-chars-forward " \t")))
294
483e630e 295(defvar indent-region-function nil
3704e77e
RS
296 "Short cut function to indent region using `indent-according-to-mode'.
297A value of nil means really run `indent-according-to-mode' on each line.")
483e630e 298
88a2603a 299(defun indent-region (start end column)
483e630e 300 "Indent each nonblank line in the region.
6528f1ca 301With prefix no argument, indent each line using `indent-according-to-mode',
88a2603a
RS
302or use `indent-region-function' to do the whole region if that's non-nil.
303If there is a fill prefix, make each line start with the fill prefix.
483e630e 304With argument COLUMN, indent each line to that column.
6528f1ca
GM
305
306When you call this from a program, START and END specify
307the region to indent, and COLUMN specifies the indentation column.
308If COLUMN is nil, then indent each line according to the mode."
483e630e 309 (interactive "r\nP")
88a2603a 310 (if (null column)
483e630e
RM
311 (if fill-prefix
312 (save-excursion
313 (goto-char end)
314 (setq end (point-marker))
315 (goto-char start)
316 (let ((regexp (regexp-quote fill-prefix)))
88a2603a
RS
317 (while (< (point) end)
318 (or (looking-at regexp)
319 (and (bolp) (eolp))
320 (insert fill-prefix))
321 (forward-line 1))))
483e630e
RM
322 (if indent-region-function
323 (funcall indent-region-function start end)
324 (save-excursion
88a2603a
RS
325 (goto-char end)
326 (setq end (point-marker))
327 (goto-char start)
328 (or (bolp) (forward-line 1))
329 (while (< (point) end)
232acca7 330 (or (and (bolp) (eolp))
88a2603a
RS
331 (funcall indent-line-function))
332 (forward-line 1))
333 (move-marker end nil))))
334 (setq column (prefix-numeric-value column))
483e630e
RM
335 (save-excursion
336 (goto-char end)
337 (setq end (point-marker))
338 (goto-char start)
339 (or (bolp) (forward-line 1))
340 (while (< (point) end)
341 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
342 (or (eolp)
106b6d0e 343 (indent-to column 0))
483e630e
RM
344 (forward-line 1))
345 (move-marker end nil))))
346
347(defun indent-relative-maybe ()
1fd63d9b
EZ
348 "Indent a new line like previous nonblank line.
349If the previous nonblank line has no indent points beyond the
350column point starts at, this command does nothing.
351
352See also `indent-relative'."
483e630e
RM
353 (interactive)
354 (indent-relative t))
355
356(defun indent-relative (&optional unindented-ok)
357 "Space out to under next indent point in previous nonblank line.
358An indent point is a non-whitespace character following whitespace.
f6f53207
RS
359The following line shows the indentation points in this line.
360 ^ ^ ^ ^ ^ ^ ^ ^ ^
483e630e 361If the previous nonblank line has no indent points beyond the
1fd63d9b
EZ
362column point starts at, `tab-to-tab-stop' is done instead, unless
363this command is invoked with a numeric argument, in which case it
364does nothing.
365
366See also `indent-relative-maybe'."
483e630e 367 (interactive "P")
5e9f80a8
RS
368 (if (and abbrev-mode
369 (eq (char-syntax (preceding-char)) ?w))
370 (expand-abbrev))
483e630e
RM
371 (let ((start-column (current-column))
372 indent)
373 (save-excursion
374 (beginning-of-line)
375 (if (re-search-backward "^[^\n]" nil t)
376 (let ((end (save-excursion (forward-line 1) (point))))
377 (move-to-column start-column)
378 ;; Is start-column inside a tab on this line?
379 (if (> (current-column) start-column)
380 (backward-char 1))
381 (or (looking-at "[ \t]")
382 unindented-ok
383 (skip-chars-forward "^ \t" end))
384 (skip-chars-forward " \t" end)
385 (or (= (point) end) (setq indent (current-column))))))
386 (if indent
387 (let ((opoint (point-marker)))
388 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
389 (indent-to indent 0)
390 (if (> opoint (point))
391 (goto-char opoint))
392 (move-marker opoint nil))
393 (tab-to-tab-stop))))
394
ddb3136e 395(defcustom tab-stop-list
483e630e 396 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
0f6ca6cf 397 "*List of tab stop positions used by `tab-to-tab-stop'.
ddb3136e
RS
398This should be a list of integers, ordered from smallest to largest."
399 :group 'indent
400 :type '(repeat integer))
483e630e
RM
401
402(defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.")
403(if edit-tab-stops-map
404 nil
405 (setq edit-tab-stops-map (make-sparse-keymap))
406 (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
407 (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
408
409(defvar edit-tab-stops-buffer nil
410 "Buffer whose tab stops are being edited--in case
411the variable `tab-stop-list' is local in that buffer.")
412
413(defun edit-tab-stops ()
414 "Edit the tab stops used by `tab-to-tab-stop'.
415Creates a buffer *Tab Stops* containing text describing the tab stops.
416A colon indicates a column where there is a tab stop.
417You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
418 (interactive)
419 (setq edit-tab-stops-buffer (current-buffer))
420 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
421 (use-local-map edit-tab-stops-map)
422 (make-local-variable 'indent-tabs-mode)
423 (setq indent-tabs-mode nil)
424 (overwrite-mode 1)
425 (setq truncate-lines t)
426 (erase-buffer)
427 (let ((tabs tab-stop-list))
428 (while tabs
429 (indent-to (car tabs) 0)
430 (insert ?:)
431 (setq tabs (cdr tabs))))
432 (let ((count 0))
433 (insert ?\n)
434 (while (< count 8)
435 (insert (+ count ?0))
436 (insert " ")
437 (setq count (1+ count)))
438 (insert ?\n)
439 (while (> count 0)
440 (insert "0123456789")
441 (setq count (1- count))))
442 (insert "\nTo install changes, type C-c C-c")
443 (goto-char (point-min)))
444
445(defun edit-tab-stops-note-changes ()
446 "Put edited tab stops into effect."
447 (interactive)
448 (let (tabs)
449 (save-excursion
450 (goto-char 1)
451 (end-of-line)
452 (while (search-backward ":" nil t)
453 (setq tabs (cons (current-column) tabs))))
454 (bury-buffer (prog1 (current-buffer)
455 (switch-to-buffer edit-tab-stops-buffer)))
456 (setq tab-stop-list tabs))
457 (message "Tab stops installed"))
458
459(defun tab-to-tab-stop ()
460 "Insert spaces or tabs to next defined tab-stop column.
461The variable `tab-stop-list' is a list of columns at which there are tab stops.
462Use \\[edit-tab-stops] to edit them interactively."
463 (interactive)
a2964053
RS
464 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
465 (expand-abbrev))
483e630e
RM
466 (let ((tabs tab-stop-list))
467 (while (and tabs (>= (current-column) (car tabs)))
468 (setq tabs (cdr tabs)))
469 (if tabs
9d840bec
RS
470 (let ((opoint (point)))
471 (skip-chars-backward " \t")
472 (delete-region (point) opoint)
473 (indent-to (car tabs)))
483e630e
RM
474 (insert ?\ ))))
475
476(defun move-to-tab-stop ()
477 "Move point to next defined tab-stop column.
fbf8f564
RS
478The variable `tab-stop-list' is a list of columns at which there are tab stops.
479Use \\[edit-tab-stops] to edit them interactively."
480 (interactive)
481 (let ((tabs tab-stop-list))
482 (while (and tabs (>= (current-column) (car tabs)))
483 (setq tabs (cdr tabs)))
484 (if tabs
485 (let ((before (point)))
486 (move-to-column (car tabs) t)
487 (save-excursion
488 (goto-char before)
489 ;; If we just added a tab, or moved over one,
490 ;; delete any superfluous spaces before the old point.
491 (if (and (eq (preceding-char) ?\ )
492 (eq (following-char) ?\t))
493 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
494 (while (and (> (current-column) tabend)
495 (eq (preceding-char) ?\ ))
496 (forward-char -1))
497 (delete-region (point) before))))))))
498
483e630e
RM
499(define-key global-map "\t" 'indent-for-tab-command)
500(define-key esc-map "\034" 'indent-region)
501(define-key ctl-x-map "\t" 'indent-rigidly)
502(define-key esc-map "i" 'tab-to-tab-stop)
1a06eabd
ER
503
504;;; indent.el ends here