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