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