(paragraph-indent-minor-mode): Don't set paragraph-separate.
[bpt/emacs.git] / lisp / indent.el
1 ;;; indent.el --- indentation commands for Emacs
2
3 ;; Copyright (C) 1985, 1995, 2001 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
23
24 ;;; Commentary:
25
26 ;; Commands for making and changing indentation in text. These are
27 ;; described in the Emacs manual.
28
29 ;;; Code:
30
31 (defgroup indent nil
32 "Indentation commands"
33 :group 'editing)
34
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.")
42
43 (defcustom tab-always-indent t
44 "*Controls the operation of the TAB key.
45 If t, hitting TAB always just indents the current line.
46 If 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
51 (defun indent-according-to-mode ()
52 "Indent line in proper way for current major mode."
53 (interactive)
54 (funcall indent-line-function))
55
56 (defun indent-for-tab-command (&optional prefix-arg)
57 "Indent line in proper way for current major mode or insert a tab.
58 Depending on `tab-always-indent', either insert a tab or indent.
59 If initial point was within line's indentation, position after
60 the indentation. Else stay at same point in text.
61 The function actually called to indent is determined by the value of
62 `indent-line-function'."
63 (interactive "P")
64 (if (or (eq indent-line-function 'indent-to-left-margin)
65 (and (not tab-always-indent)
66 (> (current-column) (current-indentation))))
67 (insert-tab prefix-arg)
68 (funcall indent-line-function)))
69
70 (defun insert-tab (&optional prefix-arg)
71 (let ((count (prefix-numeric-value prefix-arg)))
72 (if (and abbrev-mode
73 (eq (char-syntax (preceding-char)) ?w))
74 (expand-abbrev))
75 (if indent-tabs-mode
76 (insert-char ?\t count)
77 (indent-to (* tab-width (+ count (/ (current-column) tab-width)))))))
78
79 (defun indent-rigidly (start end arg)
80 "Indent all lines starting in the region sideways by ARG columns.
81 Called 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)
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))))
97 (forward-line 1))
98 (move-marker end nil)))
99
100 (defun indent-line-to (column)
101 "Indent current line to COLUMN.
102 This function removes or adds spaces and tabs at beginning of line
103 only if necessary. It leaves point at end of indentation."
104 (back-to-indentation)
105 (let ((cur-col (current-column)))
106 (cond ((< cur-col column)
107 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
108 (delete-region (point)
109 (progn (skip-chars-backward " ") (point))))
110 (indent-to column))
111 ((> cur-col column) ; too far right (after tab?)
112 (delete-region (progn (move-to-column column t) (point))
113 (progn (back-to-indentation) (point)))))))
114
115 (defun current-left-margin ()
116 "Return the left margin to use for this line.
117 This is the value of the buffer-local variable `left-margin' plus the value
118 of the `left-margin' text-property at the start of the line."
119 (save-excursion
120 (back-to-indentation)
121 (max 0
122 (+ left-margin (or (get-text-property
123 (if (and (eobp) (not (bobp)))
124 (1- (point)) (point))
125 'left-margin) 0)))))
126
127 (defun move-to-left-margin (&optional n force)
128 "Move to the left margin of the current line.
129 With optional argument, move forward N-1 lines first.
130 The column moved to is the one given by the `current-left-margin' function.
131 If the line's indentation appears to be wrong, and this command is called
132 interactively or with optional argument FORCE, it will be fixed."
133 (interactive (list (prefix-numeric-value current-prefix-arg) t))
134 (beginning-of-line n)
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)))))
144
145 ;; This is the default indent-line-function,
146 ;; used in Fundamental Mode, Text Mode, etc.
147 (defun indent-to-left-margin ()
148 "Indent current line to the column given by `current-left-margin'."
149 (indent-line-to (current-left-margin)))
150
151 (defun delete-to-left-margin (&optional from to)
152 "Remove left margin indentation from a region.
153 This deletes to the column given by `current-left-margin'.
154 In no case will it delete non-whitespace.
155 Args FROM and TO are optional; default is the whole buffer."
156 (save-excursion
157 (goto-char (or to (point-max)))
158 (setq to (point-marker))
159 (goto-char (or from (point-min)))
160 (or (bolp) (forward-line 1))
161 (while (< (point) to)
162 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
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.
168 If `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)))
176 ;; Place end after whitespace
177 (goto-char to)
178 (skip-chars-forward " \t")
179 (setq to (point-marker)))
180 ;; Delete margin indentation first, but keep paragraph indentation.
181 (delete-to-left-margin from to)
182 (put-text-property from to 'left-margin lm)
183 (indent-rigidly from to lm)
184 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
185 (move-marker to nil))
186
187 (defun set-right-margin (from to lm)
188 "Set the right margin of the region to WIDTH.
189 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
190 (interactive "r\nNSet right margin to width: ")
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)
197 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
198
199 (defun alter-text-property (from to prop func &optional object)
200 "Programmatically change value of a text-property.
201 For each region between FROM and TO that has a single value for PROPERTY,
202 apply FUNCTION to that value and sets the property to the function's result.
203 Optional 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.
215 With no prefix argument, this adds `standard-indent' of indentation.
216 A prefix arg (optional third arg INC noninteractively) specifies the amount
217 to change the margin by, in characters.
218 If `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)))
227 (alter-text-property from to 'left-margin
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)))
231 (move-marker to nil))
232
233 (defun decrease-left-margin (from to inc)
234 "Make the left margin of the region smaller.
235 With no prefix argument, decrease the indentation by `standard-indent'.
236 A prefix arg (optional third arg INC noninteractively) specifies the amount
237 to change the margin by, in characters.
238 If `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.
245 With no prefix argument, increase the right margin by `standard-indent'.
246 A prefix arg (optional third arg INC noninteractively) specifies the amount
247 to change the margin by, in characters. A negative argument decreases
248 the right margin width.
249 If `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
256 (lambda (v) (+ inc (or v 0))))
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.
262 With no prefix argument, decrease the right margin by `standard-indent'.
263 A prefix arg (optional third arg INC noninteractively) specifies the amount
264 of width to remove, in characters. A negative argument increases
265 the right margin width.
266 If `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)))
270
271 (defun beginning-of-line-text (&optional n)
272 "Move to the beginning of the text on this line.
273 With optional argument, move forward N-1 lines first.
274 From the beginning of the line, moves past the left-margin indentation, the
275 fill-prefix, and any indentation used for centering or right-justifying the
276 line, 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)))
288 (if (and adaptive-fill-mode adaptive-fill-regexp
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
295 (defvar indent-region-function nil
296 "Short cut function to indent region using `indent-according-to-mode'.
297 A value of nil means really run `indent-according-to-mode' on each line.")
298
299 (defun indent-region (start end column)
300 "Indent each nonblank line in the region.
301 With prefix no argument, indent each line using `indent-according-to-mode',
302 or use `indent-region-function' to do the whole region if that's non-nil.
303 If there is a fill prefix, make each line start with the fill prefix.
304 With argument COLUMN, indent each line to that column.
305
306 When you call this from a program, START and END specify
307 the region to indent, and COLUMN specifies the indentation column.
308 If COLUMN is nil, then indent each line according to the mode."
309 (interactive "r\nP")
310 (if (null column)
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)))
317 (while (< (point) end)
318 (or (looking-at regexp)
319 (and (bolp) (eolp))
320 (insert fill-prefix))
321 (forward-line 1))))
322 (if indent-region-function
323 (funcall indent-region-function start end)
324 (save-excursion
325 (goto-char end)
326 (setq end (point-marker))
327 (goto-char start)
328 (or (bolp) (forward-line 1))
329 (while (< (point) end)
330 (or (and (bolp) (eolp))
331 (funcall indent-line-function))
332 (forward-line 1))
333 (move-marker end nil))))
334 (setq column (prefix-numeric-value column))
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)
343 (indent-to column 0))
344 (forward-line 1))
345 (move-marker end nil))))
346
347 (defun indent-relative-maybe ()
348 "Indent a new line like previous nonblank line.
349 If the previous nonblank line has no indent points beyond the
350 column point starts at, this command does nothing.
351
352 See also `indent-relative'."
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.
358 An indent point is a non-whitespace character following whitespace.
359 The following line shows the indentation points in this line.
360 ^ ^ ^ ^ ^ ^ ^ ^ ^
361 If the previous nonblank line has no indent points beyond the
362 column point starts at, `tab-to-tab-stop' is done instead, unless
363 this command is invoked with a numeric argument, in which case it
364 does nothing.
365
366 See also `indent-relative-maybe'."
367 (interactive "P")
368 (if (and abbrev-mode
369 (eq (char-syntax (preceding-char)) ?w))
370 (expand-abbrev))
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
395 (defcustom tab-stop-list
396 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
397 "*List of tab stop positions used by `tab-to-tab-stop'.
398 This should be a list of integers, ordered from smallest to largest."
399 :group 'indent
400 :type '(repeat integer))
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
411 the 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'.
415 Creates a buffer *Tab Stops* containing text describing the tab stops.
416 A colon indicates a column where there is a tab stop.
417 You 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.
461 The variable `tab-stop-list' is a list of columns at which there are tab stops.
462 Use \\[edit-tab-stops] to edit them interactively."
463 (interactive)
464 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
465 (expand-abbrev))
466 (let ((tabs tab-stop-list))
467 (while (and tabs (>= (current-column) (car tabs)))
468 (setq tabs (cdr tabs)))
469 (if tabs
470 (let ((opoint (point)))
471 (skip-chars-backward " \t")
472 (delete-region (point) opoint)
473 (indent-to (car tabs)))
474 (insert ?\ ))))
475
476 (defun move-to-tab-stop ()
477 "Move point to next defined tab-stop column.
478 The variable `tab-stop-list' is a list of columns at which there are tab stops.
479 Use \\[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
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)
503
504 ;;; indent.el ends here