|
|
1.1 root 1: ;; Indentation commands for Emacs
2: ;; Copyright (C) 1985 Richard M. Stallman.
3:
4: ;; This file is part of GNU Emacs.
5:
6: ;; GNU Emacs is distributed in the hope that it will be useful,
7: ;; but WITHOUT ANY WARRANTY. No author or distributor
8: ;; accepts responsibility to anyone for the consequences of using it
9: ;; or for whether it serves any particular purpose or works at all,
10: ;; unless he says so in writing. Refer to the GNU Emacs General Public
11: ;; License for full details.
12:
13: ;; Everyone is granted permission to copy, modify and redistribute
14: ;; GNU Emacs, but only under the conditions described in the
15: ;; GNU Emacs General Public License. A copy of this license is
16: ;; supposed to have been given to you along with GNU Emacs so you
17: ;; can know your rights and responsibilities. It should be in a
18: ;; file named COPYING. Among other things, the copyright notice
19: ;; and this notice must be preserved on all copies.
20:
21:
22: ;Now in loaddefs.el
23: ;(defvar indent-line-function
24: ; 'indent-to-left-margin
25: ; "Function to indent current line.")
26:
27: (defun indent-according-to-mode ()
28: "Indent line in proper way for current major mode."
29: (interactive)
30: (funcall indent-line-function))
31:
32: (defun indent-for-tab-command ()
33: "Indent line in proper way for current major mode."
34: (interactive)
35: (if (eq indent-line-function 'indent-to-left-margin)
36: (insert-tab)
37: (funcall indent-line-function)))
38:
39: (defun insert-tab ()
40: (if abbrev-mode
41: (expand-abbrev))
42: (if indent-tabs-mode
43: (insert ?\t)
44: (indent-to (* tab-width (1+ (/ (current-column) tab-width))))))
45:
46: (defun indent-rigidly (start end arg)
47: "Indent all lines starting in the region sideways by ARG columns.
48: Called from a program, takes three arguments, START, END and ARG."
49: (interactive "r\np")
50: (save-excursion
51: (goto-char end)
52: (setq end (point-marker))
53: (goto-char start)
54: (or (bolp) (forward-line 1))
55: (while (< (point) end)
56: (let ((indent (current-indentation)))
57: (delete-region (point) (progn (skip-chars-forward " \t") (point)))
58: (or (eolp)
59: (indent-to (max 0 (+ indent arg)) 0)))
60: (forward-line 1))
61: (move-marker end nil)))
62:
63: ;; This is the default indent-line-function,
64: ;; used in Fundamental Mode, Text Mode, etc.
65: (defun indent-to-left-margin ()
66: (or (= (current-indentation) left-margin)
67: (let (epos)
68: (save-excursion
69: (beginning-of-line)
70: (delete-region (point)
71: (progn (skip-chars-forward " \t")
72: (point)))
73: (indent-to left-margin)
74: (setq epos (point)))
75: (if (< (point) epos)
76: (goto-char epos)))))
77:
78: (defvar indent-region-function nil
79: "Function which is short cut to indent each line in region with Tab.
80: nil means really call Tab on each line.")
81:
82: (defun indent-region (start end arg)
83: "Indent each nonblank line in the region.
84: With no argument, indent each line with Tab.
85: With argument COLUMN, indent each line to that column.
86: Called from a program, takes three args: START, END and COLUMN."
87: (interactive "r\nP")
88: (if (null arg)
89: (if indent-region-function
90: (funcall indent-region-function start end)
91: (save-excursion
92: (goto-char end)
93: (setq end (point-marker))
94: (goto-char start)
95: (or (bolp) (forward-line 1))
96: (while (< (point) end)
97: (funcall indent-line-function)
98: (forward-line 1))
99: (move-marker end nil)))
100: (setq arg (prefix-numeric-value arg))
101: (save-excursion
102: (goto-char end)
103: (setq end (point-marker))
104: (goto-char start)
105: (or (bolp) (forward-line 1))
106: (while (< (point) end)
107: (delete-region (point) (progn (skip-chars-forward " \t") (point)))
108: (or (eolp)
109: (indent-to arg 0))
110: (forward-line 1))
111: (move-marker end nil))))
112:
113: (defun indent-relative-maybe ()
114: "Indent a new line like previous nonblank line."
115: (interactive)
116: (indent-relative t))
117:
118: (defun indent-relative (&optional unindented-ok)
119: "Space out to under next indent point in previous nonblank line.
120: An indent point is a non-whitespace character following whitespace.
121: If the previous nonblank line has no indent points beyond
122: the column point starts at, tab-to-tab-stop is done instead."
123: (interactive "P")
124: (if abbrev-mode (expand-abbrev))
125: (let ((start-column (current-column))
126: indent)
127: (save-excursion
128: (beginning-of-line)
129: (if (re-search-backward "^[^\n]" nil t)
130: (let ((end (scan-buffer (point) 1 ?\n)))
131: (move-to-column start-column)
132: (or (looking-at "[ \t]")
133: unindented-ok
134: (skip-chars-forward "^ \t" end))
135: (skip-chars-forward " \t" end)
136: (or (= (point) end) (setq indent (current-column))))))
137: (if indent
138: (let ((opoint (point-marker)))
139: (delete-region (point) (progn (skip-chars-backward " \t") (point)))
140: (indent-to indent 0)
141: (if (> opoint (point))
142: (goto-char opoint))
143: (move-marker opoint nil))
144: (tab-to-tab-stop))))
145:
146: (defvar tab-stop-list
147: '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
148: "List of tab stop positions used by tab-to-tab-stops.")
149:
150: (defvar edit-tab-stops-map nil "Keymap used in edit-tab-stops.")
151: (if edit-tab-stops-map
152: nil
153: (setq edit-tab-stops-map (make-sparse-keymap))
154: (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
155: (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
156:
157: (defvar edit-tab-stops-buffer nil
158: "Buffer whose tab stops are being edited--in case
159: the variable tab-stop-list is local in that buffer.")
160:
161: (defun edit-tab-stops ()
162: "Edit the tab stops used by tab-to-tab-stop.
163: Creates a buffer *Tab Stops* containing text describing the tab stops.
164: A colon indicates a column where there is a tab stop.
165: You can add or remove colons and then do C-x C-s to make changes take effect."
166: (interactive)
167: (setq edit-tab-stops-buffer (current-buffer))
168: (switch-to-buffer (get-buffer-create "*Tab Stops*"))
169: (use-local-map edit-tab-stops-map)
170: (make-local-variable 'indent-tabs-mode)
171: (setq indent-tabs-mode nil)
172: (overwrite-mode 1)
173: (setq truncate-lines t)
174: (erase-buffer)
175: (let ((tabs tab-stop-list))
176: (while tabs
177: (indent-to (car tabs) 0)
178: (insert ?:)
179: (setq tabs (cdr tabs))))
180: (let ((count 0))
181: (insert ?\n)
182: (while (< count 8)
183: (insert (+ count ?0))
184: (insert " ")
185: (setq count (1+ count)))
186: (insert ?\n)
187: (while (> count 0)
188: (insert "0123456789")
189: (setq count (1- count))))
190: (insert "\nTo install changes, type C-x C-s")
191: (goto-char (point-min)))
192:
193: (defun edit-tab-stops-note-changes ()
194: "Put edited tab stops into effect."
195: (interactive)
196: (let (tabs)
197: (save-excursion
198: (goto-char 1)
199: (end-of-line)
200: (while (search-backward ":" nil t)
201: (setq tabs (cons (current-column) tabs))))
202: (bury-buffer (prog1 (current-buffer)
203: (switch-to-buffer edit-tab-stops-buffer)))
204: (setq tab-stop-list tabs))
205: (message "Tab stops installed"))
206:
207: (defun tab-to-tab-stop ()
208: "Insert spaces or tabs to next defined tab-stop column.
209: The variable tab-stop-list is a list of columns at which there are tab stops.
210: Use \\[edit-tab-stops] to edit them interactively."
211: (interactive)
212: (if abbrev-mode (expand-abbrev))
213: (let ((tabs tab-stop-list))
214: (while (and tabs (>= (current-column) (car tabs)))
215: (setq tabs (cdr tabs)))
216: (if tabs
217: (indent-to (car tabs))
218: (insert ? ))))
219:
220: (define-key global-map "\t" 'indent-for-tab-command)
221: (define-key esc-map "\034" 'indent-region)
222: (define-key ctl-x-map "\t" 'indent-rigidly)
223: (define-key esc-map "i" 'tab-to-tab-stop)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.