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