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