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