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