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