* dired.el (dired-pop-to-buffer): Bind pop-up-frames to nil (Bug#7533).
[emacs/old-mirror.git] / lisp / indent.el
blobadecbfaeb1238db9f6674a779fd1a2c6b6c81b85
1 ;;; indent.el --- indentation commands for Emacs
3 ;; Copyright (C) 1985, 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Commands for making and changing indentation in text. These are
26 ;; described in the Emacs manual.
28 ;;; Code:
30 (defgroup indent nil
31 "Indentation commands."
32 :group 'editing)
34 (defcustom standard-indent 4
35 "Default number of columns for margin-changing functions to indent."
36 :group 'indent
37 :type 'integer)
39 (defvar indent-line-function 'indent-relative
40 "Function to indent the current line.
41 This function will be called with no arguments.
42 If it is called somewhere where auto-indentation cannot be done
43 \(e.g. inside a string), the function should simply return `noindent'.
44 Setting this function is all you need to make TAB indent appropriately.
45 Don't rebind TAB unless you really need to.")
47 (defcustom tab-always-indent t
48 "Controls the operation of the TAB key.
49 If t, hitting TAB always just indents the current line.
50 If nil, hitting TAB indents the current line if point is at the left margin
51 or in the line's indentation, otherwise it inserts a \"real\" TAB character.
52 If `complete', TAB first tries to indent the current line, and if the line
53 was already indented, then try to complete the thing at point.
55 Some programming language modes have their own variable to control this,
56 e.g., `c-tab-always-indent', and do not respect this variable."
57 :group 'indent
58 :type '(choice
59 (const :tag "Always indent" t)
60 (const :tag "Indent if inside indentation, else TAB" nil)
61 (const :tag "Indent, or if already indented complete" complete)))
64 (defun indent-according-to-mode ()
65 "Indent line in proper way for current major mode.
66 The buffer-local variable `indent-line-function' determines how to do this,
67 but the functions `indent-relative' and `indent-relative-maybe' are
68 special; we don't actually use them here."
69 (interactive)
70 (if (memq indent-line-function
71 '(indent-relative indent-relative-maybe))
72 ;; These functions are used for tabbing, but can't be used for
73 ;; indenting. Replace with something ad-hoc.
74 (let ((column (save-excursion
75 (beginning-of-line)
76 (skip-chars-backward "\n \t")
77 (beginning-of-line)
78 (current-indentation))))
79 (if (<= (current-column) (current-indentation))
80 (indent-line-to column)
81 (save-excursion (indent-line-to column))))
82 ;; The normal case.
83 (funcall indent-line-function)))
85 (defun indent-for-tab-command (&optional arg)
86 "Indent line or region in proper way for current major mode or insert a tab.
87 Depending on `tab-always-indent', either insert a tab or indent.
89 In most major modes, if point was in the current line's indentation,
90 it is moved to the first non-whitespace character after indenting;
91 otherwise it stays at the same position in the text.
93 If a prefix argument is given, also rigidly indent the entire
94 balanced expression which starts at the beginning of the current
95 line to reflect the current line's change in indentation.
97 If `transient-mark-mode' is turned on and the region is active,
98 indent the region (in this case, any prefix argument is ignored).
100 The function actually called to indent the line is determined by the value of
101 `indent-line-function'."
102 (interactive "P")
103 (cond
104 ;; The region is active, indent it.
105 ((use-region-p)
106 (indent-region (region-beginning) (region-end)))
107 ((or ;; indent-to-left-margin is only meant for indenting,
108 ;; so we force it to always insert a tab here.
109 (eq indent-line-function 'indent-to-left-margin)
110 (and (not tab-always-indent)
111 (or (> (current-column) (current-indentation))
112 (eq this-command last-command))))
113 (insert-tab arg))
115 (let ((old-tick (buffer-chars-modified-tick))
116 (old-point (point))
117 (old-indent (current-indentation)))
119 ;; Indent the line.
120 (funcall indent-line-function)
122 (cond
123 ;; If the text was already indented right, try completion.
124 ((and (eq tab-always-indent 'complete)
125 (eq old-point (point))
126 (eq old-tick (buffer-chars-modified-tick)))
127 (completion-at-point))
129 ;; If a prefix argument was given, rigidly indent the following
130 ;; sexp to match the change in the current line's indentation.
131 (arg
132 (let ((end-marker
133 (save-excursion
134 (forward-line 0) (forward-sexp) (point-marker)))
135 (indentation-change (- (current-indentation) old-indent)))
136 (save-excursion
137 (forward-line 1)
138 (when (and (not (zerop indentation-change))
139 (< (point) end-marker))
140 (indent-rigidly (point) end-marker indentation-change))))))))))
142 (defun insert-tab (&optional arg)
143 (let ((count (prefix-numeric-value arg)))
144 (if (and abbrev-mode
145 (eq (char-syntax (preceding-char)) ?w))
146 (expand-abbrev))
147 (if indent-tabs-mode
148 (insert-char ?\t count)
149 (indent-to (* tab-width (+ count (/ (current-column) tab-width)))))))
151 (defun indent-rigidly (start end arg)
152 "Indent all lines starting in the region sideways by ARG columns.
153 Called from a program, takes three arguments, START, END and ARG.
154 You can remove all indentation from a region by giving a large negative ARG."
155 (interactive "r\np")
156 (save-excursion
157 (goto-char end)
158 (setq end (point-marker))
159 (goto-char start)
160 (or (bolp) (forward-line 1))
161 (while (< (point) end)
162 (let ((indent (current-indentation))
163 eol-flag)
164 (save-excursion
165 (skip-chars-forward " \t")
166 (setq eol-flag (eolp)))
167 (or eol-flag
168 (indent-to (max 0 (+ indent arg)) 0))
169 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
170 (forward-line 1))
171 (move-marker end nil)))
173 (defun indent-line-to (column)
174 "Indent current line to COLUMN.
175 This function removes or adds spaces and tabs at beginning of line
176 only if necessary. It leaves point at end of indentation."
177 (back-to-indentation)
178 (let ((cur-col (current-column)))
179 (cond ((< cur-col column)
180 (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
181 (delete-region (point)
182 (progn (skip-chars-backward " ") (point))))
183 (indent-to column))
184 ((> cur-col column) ; too far right (after tab?)
185 (delete-region (progn (move-to-column column t) (point))
186 (progn (back-to-indentation) (point)))))))
188 (defun current-left-margin ()
189 "Return the left margin to use for this line.
190 This is the value of the buffer-local variable `left-margin' plus the value
191 of the `left-margin' text-property at the start of the line."
192 (save-excursion
193 (back-to-indentation)
194 (max 0
195 (+ left-margin (or (get-text-property
196 (if (and (eobp) (not (bobp)))
197 (1- (point)) (point))
198 'left-margin) 0)))))
200 (defun move-to-left-margin (&optional n force)
201 "Move to the left margin of the current line.
202 With optional argument, move forward N-1 lines first.
203 The column moved to is the one given by the `current-left-margin' function.
204 If the line's indentation appears to be wrong, and this command is called
205 interactively or with optional argument FORCE, it will be fixed."
206 (interactive (list (prefix-numeric-value current-prefix-arg) t))
207 (beginning-of-line n)
208 (skip-chars-forward " \t")
209 (if (minibufferp (current-buffer))
210 (if (save-excursion (beginning-of-line) (bobp))
211 (goto-char (minibuffer-prompt-end))
212 (beginning-of-line))
213 (let ((lm (current-left-margin))
214 (cc (current-column)))
215 (cond ((> cc lm)
216 (if (> (move-to-column lm force) lm)
217 ;; If lm is in a tab and we are not forcing, move before tab
218 (backward-char 1)))
219 ((and force (< cc lm))
220 (indent-to-left-margin))))))
222 ;; This used to be the default indent-line-function,
223 ;; used in Fundamental Mode, Text Mode, etc.
224 (defun indent-to-left-margin ()
225 "Indent current line to the column given by `current-left-margin'."
226 (save-excursion (indent-line-to (current-left-margin)))
227 ;; If we are within the indentation, move past it.
228 (when (save-excursion
229 (skip-chars-backward " \t")
230 (bolp))
231 (skip-chars-forward " \t")))
233 (defun delete-to-left-margin (&optional from to)
234 "Remove left margin indentation from a region.
235 This deletes to the column given by `current-left-margin'.
236 In no case will it delete non-whitespace.
237 Args FROM and TO are optional; default is the whole buffer."
238 (save-excursion
239 (goto-char (or to (point-max)))
240 (setq to (point-marker))
241 (goto-char (or from (point-min)))
242 (or (bolp) (forward-line 1))
243 (while (< (point) to)
244 (delete-region (point) (progn (move-to-left-margin nil t) (point)))
245 (forward-line 1))
246 (move-marker to nil)))
248 (defun set-left-margin (from to width)
249 "Set the left margin of the region to WIDTH.
250 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
252 Interactively, WIDTH is the prefix argument, if specified.
253 Without prefix argument, the command prompts for WIDTH."
254 (interactive "r\nNSet left margin to column: ")
255 (save-excursion
256 ;; If inside indentation, start from BOL.
257 (goto-char from)
258 (skip-chars-backward " \t")
259 (if (bolp) (setq from (point)))
260 ;; Place end after whitespace
261 (goto-char to)
262 (skip-chars-forward " \t")
263 (setq to (point-marker)))
264 ;; Delete margin indentation first, but keep paragraph indentation.
265 (delete-to-left-margin from to)
266 (put-text-property from to 'left-margin width)
267 (indent-rigidly from to width)
268 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
269 (move-marker to nil))
271 (defun set-right-margin (from to width)
272 "Set the right margin of the region to WIDTH.
273 If `auto-fill-mode' is active, re-fill the region to fit the new margin.
275 Interactively, WIDTH is the prefix argument, if specified.
276 Without prefix argument, the command prompts for WIDTH."
277 (interactive "r\nNSet right margin to width: ")
278 (save-excursion
279 (goto-char from)
280 (skip-chars-backward " \t")
281 (if (bolp) (setq from (point))))
282 (put-text-property from to 'right-margin width)
283 (if auto-fill-function (save-excursion (fill-region from to nil t t))))
285 (defun alter-text-property (from to prop func &optional object)
286 "Programmatically change value of a text-property.
287 For each region between FROM and TO that has a single value for PROPERTY,
288 apply FUNCTION to that value and sets the property to the function's result.
289 Optional fifth argument OBJECT specifies the string or buffer to operate on."
290 (let ((begin from)
291 end val)
292 (while (setq val (get-text-property begin prop object)
293 end (text-property-not-all begin to prop val object))
294 (put-text-property begin end prop (funcall func val) object)
295 (setq begin end))
296 (if (< begin to)
297 (put-text-property begin to prop (funcall func val) object))))
299 (defun increase-left-margin (from to inc)
300 "Increase or decrease the left-margin of the region.
301 With no prefix argument, this adds `standard-indent' of indentation.
302 A prefix arg (optional third arg INC noninteractively) specifies the amount
303 to change the margin by, in characters.
304 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
305 (interactive "*r\nP")
306 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
307 (save-excursion
308 (goto-char from)
309 (skip-chars-backward " \t")
310 (if (bolp) (setq from (point)))
311 (goto-char to)
312 (setq to (point-marker)))
313 (alter-text-property from to 'left-margin
314 (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
315 (indent-rigidly from to inc)
316 (if auto-fill-function (save-excursion (fill-region from to nil t t)))
317 (move-marker to nil))
319 (defun decrease-left-margin (from to inc)
320 "Make the left margin of the region smaller.
321 With no prefix argument, decrease the indentation by `standard-indent'.
322 A prefix arg (optional third arg INC noninteractively) specifies the amount
323 to change the margin by, in characters.
324 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
325 (interactive "*r\nP")
326 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
327 (increase-left-margin from to (- inc)))
329 (defun increase-right-margin (from to inc)
330 "Increase the right-margin of the region.
331 With no prefix argument, increase the right margin by `standard-indent'.
332 A prefix arg (optional third arg INC noninteractively) specifies the amount
333 to change the margin by, in characters. A negative argument decreases
334 the right margin width.
335 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
336 (interactive "r\nP")
337 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
338 (save-excursion
339 (alter-text-property from to 'right-margin
340 (lambda (v) (+ inc (or v 0))))
341 (if auto-fill-function
342 (fill-region from to nil t t))))
344 (defun decrease-right-margin (from to inc)
345 "Make the right margin of the region smaller.
346 With no prefix argument, decrease the right margin by `standard-indent'.
347 A prefix arg (optional third arg INC noninteractively) specifies the amount
348 of width to remove, in characters. A negative argument increases
349 the right margin width.
350 If `auto-fill-mode' is active, re-fills region to fit in new margin."
351 (interactive "*r\nP")
352 (setq inc (if inc (prefix-numeric-value inc) standard-indent))
353 (increase-right-margin from to (- inc)))
355 (defun beginning-of-line-text (&optional n)
356 "Move to the beginning of the text on this line.
357 With optional argument, move forward N-1 lines first.
358 From the beginning of the line, moves past the left-margin indentation, the
359 fill-prefix, and any indentation used for centering or right-justifying the
360 line, but does not move past any whitespace that was explicitly inserted
361 \(such as a tab used to indent the first line of a paragraph)."
362 (interactive "p")
363 (beginning-of-line n)
364 (skip-chars-forward " \t")
365 ;; Skip over fill-prefix.
366 (if (and fill-prefix
367 (not (string-equal fill-prefix "")))
368 (if (equal fill-prefix
369 (buffer-substring
370 (point) (min (point-max) (+ (length fill-prefix) (point)))))
371 (forward-char (length fill-prefix)))
372 (if (and adaptive-fill-mode adaptive-fill-regexp
373 (looking-at adaptive-fill-regexp))
374 (goto-char (match-end 0))))
375 ;; Skip centering or flushright indentation
376 (if (memq (current-justification) '(center right))
377 (skip-chars-forward " \t")))
379 (defvar indent-region-function nil
380 "Short cut function to indent region using `indent-according-to-mode'.
381 A value of nil means really run `indent-according-to-mode' on each line.")
383 (defun indent-region (start end &optional column)
384 "Indent each nonblank line in the region.
385 A numeric prefix argument specifies a column: indent each line to that column.
387 With no prefix argument, the command chooses one of these methods and
388 indents all the lines with it:
390 1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
391 beginning of each line in the region that does not already begin
392 with it.
393 2) If `indent-region-function' is non-nil, call that function
394 to indent the region.
395 3) Indent each line as specified by the variable `indent-line-function'.
397 Called from a program, START and END specify the region to indent.
398 If the third argument COLUMN is an integer, it specifies the
399 column to indent to; if it is nil, use one of the three methods above."
400 (interactive "r\nP")
401 (if (null column)
402 (if fill-prefix
403 (save-excursion
404 (goto-char end)
405 (setq end (point-marker))
406 (goto-char start)
407 (let ((regexp (regexp-quote fill-prefix)))
408 (while (< (point) end)
409 (or (looking-at regexp)
410 (and (bolp) (eolp))
411 (insert fill-prefix))
412 (forward-line 1))))
413 (if indent-region-function
414 (funcall indent-region-function start end)
415 (save-excursion
416 (setq end (copy-marker end))
417 (goto-char start)
418 (while (< (point) end)
419 (or (and (bolp) (eolp))
420 (funcall indent-line-function))
421 (forward-line 1))
422 (move-marker end nil))))
423 (setq column (prefix-numeric-value column))
424 (save-excursion
425 (goto-char end)
426 (setq end (point-marker))
427 (goto-char start)
428 (or (bolp) (forward-line 1))
429 (while (< (point) end)
430 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
431 (or (eolp)
432 (indent-to column 0))
433 (forward-line 1))
434 (move-marker end nil))))
436 (defun indent-relative-maybe ()
437 "Indent a new line like previous nonblank line.
438 If the previous nonblank line has no indent points beyond the
439 column point starts at, this command does nothing.
441 See also `indent-relative'."
442 (interactive)
443 (indent-relative t))
445 (defun indent-relative (&optional unindented-ok)
446 "Space out to under next indent point in previous nonblank line.
447 An indent point is a non-whitespace character following whitespace.
448 The following line shows the indentation points in this line.
449 ^ ^ ^ ^ ^ ^ ^ ^ ^
450 If the previous nonblank line has no indent points beyond the
451 column point starts at, `tab-to-tab-stop' is done instead, unless
452 this command is invoked with a numeric argument, in which case it
453 does nothing.
455 See also `indent-relative-maybe'."
456 (interactive "P")
457 (if (and abbrev-mode
458 (eq (char-syntax (preceding-char)) ?w))
459 (expand-abbrev))
460 (let ((start-column (current-column))
461 indent)
462 (save-excursion
463 (beginning-of-line)
464 (if (re-search-backward "^[^\n]" nil t)
465 (let ((end (save-excursion (forward-line 1) (point))))
466 (move-to-column start-column)
467 ;; Is start-column inside a tab on this line?
468 (if (> (current-column) start-column)
469 (backward-char 1))
470 (or (looking-at "[ \t]")
471 unindented-ok
472 (skip-chars-forward "^ \t" end))
473 (skip-chars-forward " \t" end)
474 (or (= (point) end) (setq indent (current-column))))))
475 (if indent
476 (let ((opoint (point-marker)))
477 (indent-to indent 0)
478 (if (> opoint (point))
479 (goto-char opoint))
480 (move-marker opoint nil))
481 (tab-to-tab-stop))))
483 (defcustom tab-stop-list
484 '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
485 "List of tab stop positions used by `tab-to-tab-stop'.
486 This should be a list of integers, ordered from smallest to largest."
487 :group 'indent
488 :type '(repeat integer))
489 (put 'tab-stop-list 'safe-local-variable 'listp)
491 (defvar edit-tab-stops-map
492 (let ((map (make-sparse-keymap)))
493 (define-key map "\C-x\C-s" 'edit-tab-stops-note-changes)
494 (define-key map "\C-c\C-c" 'edit-tab-stops-note-changes)
495 map)
496 "Keymap used in `edit-tab-stops'.")
498 (defvar edit-tab-stops-buffer nil
499 "Buffer whose tab stops are being edited.
500 This matters if the variable `tab-stop-list' is local in that buffer.")
502 (defun edit-tab-stops ()
503 "Edit the tab stops used by `tab-to-tab-stop'.
504 Creates a buffer *Tab Stops* containing text describing the tab stops.
505 A colon indicates a column where there is a tab stop.
506 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
507 (interactive)
508 (setq edit-tab-stops-buffer (current-buffer))
509 (switch-to-buffer (get-buffer-create "*Tab Stops*"))
510 (use-local-map edit-tab-stops-map)
511 (make-local-variable 'indent-tabs-mode)
512 (setq indent-tabs-mode nil)
513 (overwrite-mode 1)
514 (setq truncate-lines t)
515 (erase-buffer)
516 (let ((tabs tab-stop-list))
517 (while tabs
518 (indent-to (car tabs) 0)
519 (insert ?:)
520 (setq tabs (cdr tabs))))
521 (let ((count 0))
522 (insert ?\n)
523 (while (< count 8)
524 (insert (+ count ?0))
525 (insert " ")
526 (setq count (1+ count)))
527 (insert ?\n)
528 (while (> count 0)
529 (insert "0123456789")
530 (setq count (1- count))))
531 (insert "\nTo install changes, type C-c C-c")
532 (goto-char (point-min)))
534 (defun edit-tab-stops-note-changes ()
535 "Put edited tab stops into effect."
536 (interactive)
537 (let (tabs)
538 (save-excursion
539 (goto-char 1)
540 (end-of-line)
541 (while (search-backward ":" nil t)
542 (setq tabs (cons (current-column) tabs))))
543 (bury-buffer (prog1 (current-buffer)
544 (switch-to-buffer edit-tab-stops-buffer)))
545 (setq tab-stop-list tabs))
546 (message "Tab stops installed"))
548 (defun tab-to-tab-stop ()
549 "Insert spaces or tabs to next defined tab-stop column.
550 The variable `tab-stop-list' is a list of columns at which there are tab stops.
551 Use \\[edit-tab-stops] to edit them interactively."
552 (interactive)
553 (and abbrev-mode (= (char-syntax (preceding-char)) ?w)
554 (expand-abbrev))
555 (let ((tabs tab-stop-list))
556 (while (and tabs (>= (current-column) (car tabs)))
557 (setq tabs (cdr tabs)))
558 (if tabs
559 (let ((opoint (point)))
560 (delete-horizontal-space t)
561 (indent-to (car tabs)))
562 (insert ?\s))))
564 (defun move-to-tab-stop ()
565 "Move point to next defined tab-stop column.
566 The variable `tab-stop-list' is a list of columns at which there are tab stops.
567 Use \\[edit-tab-stops] to edit them interactively."
568 (interactive)
569 (let ((tabs tab-stop-list))
570 (while (and tabs (>= (current-column) (car tabs)))
571 (setq tabs (cdr tabs)))
572 (if tabs
573 (let ((before (point)))
574 (move-to-column (car tabs) t)
575 (save-excursion
576 (goto-char before)
577 ;; If we just added a tab, or moved over one,
578 ;; delete any superfluous spaces before the old point.
579 (if (and (eq (preceding-char) ?\s)
580 (eq (following-char) ?\t))
581 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
582 (while (and (> (current-column) tabend)
583 (eq (preceding-char) ?\s))
584 (forward-char -1))
585 (delete-region (point) before))))))))
587 (define-key global-map "\t" 'indent-for-tab-command)
588 (define-key esc-map "\C-\\" 'indent-region)
589 (define-key ctl-x-map "\t" 'indent-rigidly)
590 (define-key esc-map "i" 'tab-to-tab-stop)
592 ;; arch-tag: f402b2a7-e44f-492f-b5b8-38996020b7c3
593 ;;; indent.el ends here