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