update.
[chise/xemacs-chise.git.1] / lisp / indent.el
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 count)
68   "Indent all lines starting in the region sideways by COUNT columns.
69 Called from a program, takes three arguments, START, END and COUNT."
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 count)) 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)
96            (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
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 The amount of indentation to delete is determined by calling the
143 function `current-left-margin'.
144 In no case will it delete non-whitespace.
145 Args FROM and TO are optional; default is the whole buffer."
146   (save-excursion
147     (goto-char (or to (point-max)))
148     (setq to (point-marker))
149     (goto-char (or from (point-min)))
150     (or (bolp) (forward-line 1))
151     (while (< (point) to)
152       (delete-region (point) (progn (move-to-left-margin nil t) (point)))
153       (forward-line 1))
154     (move-marker to nil)))
155
156 (defun set-left-margin (from to lm)
157   "Set the left margin of the region to WIDTH.
158 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
159   (interactive "r\nNSet left margin to column: ")
160   (if (interactive-p) (setq lm (prefix-numeric-value lm)))
161   (save-excursion
162     ;; If inside indentation, start from BOL.
163     (goto-char from)
164     (skip-chars-backward " \t")
165     (if (bolp) (setq from (point)))
166     ;; Place end after whitespace
167     (goto-char to)
168     (skip-chars-forward " \t")
169     (setq to (point-marker)))
170   ;; Delete margin indentation first, but keep paragraph indentation.
171   (delete-to-left-margin from to)
172   (put-text-property from to 'left-margin lm)
173   (indent-rigidly from to lm)
174   (if auto-fill-function (save-excursion (fill-region from to nil t t)))
175   (move-marker to nil))
176
177 (defun set-right-margin (from to lm)
178   "Set the right margin of the region to WIDTH.
179 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
180   (interactive "r\nNSet right margin to width: ")
181   (if (interactive-p) (setq lm (prefix-numeric-value lm)))
182   (save-excursion
183     (goto-char from)
184     (skip-chars-backward " \t")
185     (if (bolp) (setq from (point))))
186   (put-text-property from to 'right-margin lm)
187   (if auto-fill-function (save-excursion (fill-region from to nil t t))))
188
189 (defun alter-text-property (from to prop func &optional object)
190   "Programmatically change value of a text-property.
191 For each region between FROM and TO that has a single value for PROPERTY,
192 apply FUNCTION to that value and sets the property to the function's result.
193 Optional fifth argument OBJECT specifies the string or buffer to operate on."
194   (let ((begin from)
195         end val)
196     (while (setq val (get-text-property begin prop object)
197                  end (text-property-not-all begin to prop val object))
198       (put-text-property begin end prop (funcall func val) object)
199       (setq begin end))
200     (if (< begin to)
201         (put-text-property begin to prop (funcall func val) object))))
202
203 (defun increase-left-margin (from to inc)
204   "Increase or decrease the left-margin of the region.
205 With no prefix argument, this adds `standard-indent' of indentation.
206 A prefix arg (optional third arg INC noninteractively) specifies the amount
207 to change the margin by, in characters.
208 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
209   (interactive "*r\nP")
210   (setq inc (if inc (prefix-numeric-value inc) standard-indent))
211   (save-excursion
212     (goto-char from)
213     (skip-chars-backward " \t")
214     (if (bolp) (setq from (point)))
215     (goto-char to)
216     (setq to (point-marker)))
217   (alter-text-property from (marker-position to) 'left-margin ; XEmacs
218                        (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
219   (indent-rigidly from (marker-position to) inc) ; XEmacs
220   (if auto-fill-function
221       (save-excursion
222         (fill-region from (marker-position to) nil t t))) ; XEmacs
223   (move-marker to nil))
224
225 (defun decrease-left-margin (from to inc)
226   "Make the left margin of the region smaller.
227 With no prefix argument, decrease the indentation by `standard-indent'.
228 A prefix arg (optional third arg INC noninteractively) specifies the amount
229 to change the margin by, in characters.
230 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
231   (interactive "*r\nP")
232   (setq inc (if inc (prefix-numeric-value inc) standard-indent))
233   (increase-left-margin from to (- inc)))
234
235 (defun increase-right-margin (from to inc)
236   "Increase the right-margin of the region.
237 With no prefix argument, increase the right margin by `standard-indent'.
238 A prefix arg (optional third arg INC noninteractively) specifies the amount
239 to change the margin by, in characters.  A negative argument decreases
240 the right margin width.
241 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
242   (interactive "r\nP")
243   (if (interactive-p)
244       (setq inc (if inc (prefix-numeric-value current-prefix-arg)
245                   standard-indent)))
246   (save-excursion
247     (alter-text-property from to 'right-margin
248        (lambda (v) (+ inc (or v 0))))
249     (if auto-fill-function
250         (fill-region from to nil t t))))
251
252 (defun decrease-right-margin (from to inc)
253   "Make the right margin of the region smaller.
254 With no prefix argument, decrease the right margin by `standard-indent'.
255 A prefix arg (optional third arg INC noninteractively) specifies the amount
256 of width to remove, in characters.  A negative argument increases
257 the right margin width.
258 If `auto-fill-mode' is active, re-fills region to fit in new margin."
259   (interactive "*r\nP")
260   (setq inc (if inc (prefix-numeric-value inc) standard-indent))
261   (increase-right-margin from to (- inc)))
262
263 (defun beginning-of-line-text (&optional n)
264   "Move to the beginning of the text on this line.
265 With optional argument, move forward N-1 lines first.
266 From the beginning of the line, moves past the left-margin indentation, the
267 fill-prefix, and any indentation used for centering or right-justifying the
268 line, but does not move past any whitespace that was explicitly inserted
269 \(such as a tab used to indent the first line of a paragraph)."
270   (interactive "p")
271   (beginning-of-line n)
272   (skip-chars-forward " \t")
273   ;; Skip over fill-prefix.
274   (if (and fill-prefix
275            (not (string-equal fill-prefix "")))
276       (if (equal fill-prefix
277                  (buffer-substring
278                   (point) (min (point-max) (+ (length fill-prefix) (point)))))
279           (forward-char (length fill-prefix)))
280     (if (and adaptive-fill-mode adaptive-fill-regexp
281              (looking-at adaptive-fill-regexp))
282         (goto-char (match-end 0))))
283   ;; Skip centering or flushright indentation
284   (if (memq (current-justification) '(center right))
285       (skip-chars-forward " \t")))
286
287 (defvar indent-region-function nil
288   "Short cut function to indent region using `indent-according-to-mode'.
289 A value of nil means really run `indent-according-to-mode' on each line.")
290
291 (defun indent-region (start end column)
292   "Indent each nonblank line in the region.
293 With no argument, indent each line using `indent-according-to-mode',
294 or use `indent-region-function' to do the whole region if that's non-nil.
295 If there is a fill prefix, make each line start with the fill prefix.
296 With argument COLUMN, indent each line to that column.
297 Called from a program, takes three args: START, END and COLUMN."
298   (interactive "r\nP")
299   (if (null column)
300       (if fill-prefix
301           (save-excursion
302             (goto-char end)
303             (setq end (point-marker))
304             (goto-char start)
305             (let ((regexp (regexp-quote fill-prefix)))
306             (while (< (point) end)
307               (or (looking-at regexp)
308                   (and (bolp) (eolp))
309                   (insert fill-prefix))
310               (forward-line 1))))
311         (if indent-region-function
312             (funcall indent-region-function start end)
313           (save-excursion
314           (goto-char end)
315           (setq end (point-marker))
316           (goto-char start)
317           (or (bolp) (forward-line 1))
318           (while (< (point) end)
319             (or (and (bolp) (eolp))
320                 (funcall indent-line-function))
321             (forward-line 1))
322           (move-marker end nil))))
323     (setq column (prefix-numeric-value column))
324     (save-excursion
325       (goto-char end)
326       (setq end (point-marker))
327       (goto-char start)
328       (or (bolp) (forward-line 1))
329       (while (< (point) end)
330         (delete-region (point) (progn (skip-chars-forward " \t") (point)))
331         (or (eolp)
332             (indent-to column 0))
333         (forward-line 1))
334       (move-marker end nil))))
335
336 (defun indent-relative-maybe ()
337   "Indent a new line like previous nonblank line."
338   (interactive)
339   (indent-relative t))
340
341 (defun indent-relative (&optional unindented-ok)
342   "Space out to under next indent point in previous nonblank line.
343 An indent point is a non-whitespace character following whitespace.
344 If the previous nonblank line has no indent points beyond the
345 column point starts at, `tab-to-tab-stop' is done instead."
346   (interactive "P")
347   (if abbrev-mode (expand-abbrev))
348   (let ((start-column (current-column))
349         indent)
350     (save-excursion
351       (beginning-of-line)
352       (if (re-search-backward "^[^\n]" nil t)
353           (let ((end (save-excursion (forward-line 1) (point))))
354             (move-to-column start-column)
355             ;; Is start-column inside a tab on this line?
356             (if (> (current-column) start-column)
357                 (backward-char 1))
358             (or (looking-at "[ \t]")
359                 unindented-ok
360                 (skip-chars-forward "^ \t" end))
361             (skip-chars-forward " \t" end)
362             (or (= (point) end) (setq indent (current-column))))))
363     (if indent
364         (let ((opoint (point-marker)))
365           (delete-region (point) (progn (skip-chars-backward " \t") (point)))
366           (indent-to indent 0)
367           (if (> opoint (point))
368               (goto-char opoint))
369           (move-marker opoint nil))
370       (tab-to-tab-stop))))
371
372 (defvar tab-stop-list
373   '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
374   "*List of tab stop positions used by `tab-to-tab-stops'.
375 This should be a list of integers, ordered from smallest to largest.")
376
377 (defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.")
378 (if edit-tab-stops-map
379     nil
380   (setq edit-tab-stops-map (make-sparse-keymap))
381   (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
382   (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
383
384 (defvar edit-tab-stops-buffer nil
385   "Buffer whose tab stops are being edited--in case
386 the variable `tab-stop-list' is local in that buffer.")
387
388 (defun edit-tab-stops ()
389   "Edit the tab stops used by `tab-to-tab-stop'.
390 Creates a buffer *Tab Stops* containing text describing the tab stops.
391 A colon indicates a column where there is a tab stop.
392 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
393   (interactive)
394   (setq edit-tab-stops-buffer (current-buffer))
395   (switch-to-buffer (get-buffer-create "*Tab Stops*"))
396   ;; #### I18N3 should mark buffer as output-translating
397   (use-local-map edit-tab-stops-map)
398   (make-local-variable 'indent-tabs-mode)
399   (setq indent-tabs-mode nil)
400   (overwrite-mode 1)
401   (setq truncate-lines t)
402   (erase-buffer)
403   (let ((tabs tab-stop-list))
404     (while tabs
405       (indent-to (car tabs) 0)
406       (insert ?:)
407       (setq tabs (cdr tabs))))
408   (let ((count 0))
409     (insert ?\n)
410     (while (< count 8)
411       (insert (+ count ?0))
412     (insert "         ")
413       (setq count (1+ count)))
414     (insert ?\n)
415     (while (> count 0)
416       (insert "0123456789")
417       (setq count (1- count))))
418   ;; XEmacs
419   (insert (substitute-command-keys "\nTo install changes, type \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes]"))
420   (goto-char (point-min)))
421
422 (defun edit-tab-stops-note-changes ()
423   "Put edited tab stops into effect."
424   (interactive)
425     (let (tabs)
426       (save-excursion
427         (goto-char 1)
428         (end-of-line)
429         (while (search-backward ":" nil t)
430           (setq tabs (cons (current-column) tabs))))
431       (bury-buffer (prog1 (current-buffer)
432                           (switch-to-buffer edit-tab-stops-buffer)))
433       (setq tab-stop-list tabs))
434   (message "Tab stops installed"))
435
436 (defun tab-to-tab-stop ()
437   "Insert spaces or tabs to next defined tab-stop column.
438 The variable `tab-stop-list' is a list of columns at which there are tab stops.
439 Use \\[edit-tab-stops] to edit them interactively."
440   (interactive)
441   (and abbrev-mode (eq (char-syntax (char-before (point))) ?w)
442        (expand-abbrev))
443   (let ((tabs tab-stop-list))
444     (while (and tabs (>= (current-column) (car tabs)))
445       (setq tabs (cdr tabs)))
446     (if tabs
447         (let ((opoint (point)))
448           (skip-chars-backward " \t")
449           (delete-region (point) opoint)
450           (indent-to (car tabs)))
451       (insert ?\ ))))
452
453 (defun move-to-tab-stop ()
454   "Move point to next defined tab-stop column.
455 The variable `tab-stop-list' is a list of columns at which there are tab stops.
456 Use \\[edit-tab-stops] to edit them interactively."
457   (interactive)
458   (let ((tabs tab-stop-list))
459     (while (and tabs (>= (current-column) (car tabs)))
460       (setq tabs (cdr tabs)))
461     (if tabs
462         (let ((before (point)))
463           (move-to-column (car tabs) t)
464           (save-excursion
465             (goto-char before)
466             ;; If we just added a tab, or moved over one,
467             ;; delete any superfluous spaces before the old point.
468             (if (and (eq (char-before (point)) ?\ )
469                      (eq (char-after (point)) ?\t))
470                 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
471                   (while (and (> (current-column) tabend)
472                               (eq (char-before (point)) ?\ ))
473                     (backward-char 1))
474                   (delete-region (point) before))))))))
475
476 ;(define-key global-map "\t" 'indent-for-tab-command)
477 ;(define-key esc-map "\034" 'indent-region)
478 ;(define-key ctl-x-map "\t" 'indent-rigidly)
479 ;(define-key esc-map "i" 'tab-to-tab-stop)
480
481 ;;; indent.el ends here