GTK eradication -- file removal
[sxemacs] / lisp / indent.el
1 ;;; indent.el --- indentation commands for SXEmacs
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 SXEmacs.
9
10 ;; SXEmacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; SXEmacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Synched up with: FSF 19.30.
24
25 ;;; Commentary:
26
27 ;; This file is dumped with SXEmacs.
28
29 ;; Commands for making and changing indentation in text.  These are
30 ;; described in the XEmacs Reference Manual.
31
32 ;; 06/11/1997 - Convert (preceding|following)-char to char-(before|after) -slb
33
34 ;;; Code:
35
36 (defvar standard-indent 4
37   "Default number of columns for margin-changing functions to indent.")
38
39 (defvar indent-line-function 'indent-to-left-margin
40   "Function to indent current line.")
41
42 (defun indent-according-to-mode ()
43   "Indent line in proper way for current major mode."
44   (interactive)
45   (funcall indent-line-function))
46
47 (defun indent-for-tab-command (&optional prefix-arg)
48   "Indent line in proper way for current major mode."
49   (interactive "P")
50   (if (eq indent-line-function 'indent-to-left-margin)
51       (insert-tab prefix-arg)
52     (if prefix-arg
53         (funcall indent-line-function prefix-arg)
54       (funcall indent-line-function))))
55
56 (defun insert-tab (&optional prefix-arg)
57   (let ((count (prefix-numeric-value prefix-arg)))
58     (if abbrev-mode
59         (expand-abbrev))
60     (if indent-tabs-mode
61         (insert-char ?\t count)
62       ;; XEmacs: (Need the `1+')
63       (indent-to (* tab-width (1+ (/ (current-column) tab-width)))))))
64
65 (defun indent-rigidly (start end count)
66   "Indent all lines starting in the region sideways by COUNT columns.
67 Called from a program, takes three arguments, START, END and COUNT."
68   (interactive "r\np")
69   (save-excursion
70     (goto-char end)
71     (setq end (point-marker))
72     (goto-char start)
73     (or (bolp) (forward-line 1))
74     (while (< (point) end)
75       (let ((indent (current-indentation))
76             eol-flag)
77         (save-excursion
78           (skip-chars-forward " \t")
79           (setq eol-flag (eolp)))
80         (or eol-flag
81             (indent-to (max 0 (+ indent count)) 0))
82         (delete-region (point) (progn (skip-chars-forward " \t") (point))))
83       (forward-line 1))
84     (move-marker end nil)
85     (setq zmacs-region-stays nil))) ; XEmacs
86
87 (defun indent-line-to (column)
88   "Indent current line to COLUMN.
89 This function removes or adds spaces and tabs at beginning of line
90 only if necessary.  It leaves point at end of indentation."
91   (back-to-indentation)
92   (let ((cur-col (current-column)))
93     (cond ((< cur-col column)
94            (if (>= (- column (* (/ cur-col tab-width) tab-width)) tab-width)
95                (delete-region (point)
96                               (progn (skip-chars-backward " ") (point))))
97            (indent-to column))
98           ((> cur-col column) ; too far right (after tab?)
99            (delete-region (progn (move-to-column column t) (point))
100                           (progn (back-to-indentation) (point)))))))
101
102 (defun current-left-margin ()
103   "Return the left margin to use for this line.
104 This is the value of the buffer-local variable `left-margin' plus the value
105 of the `left-margin' text-property at the start of the line."
106   (save-excursion
107     (back-to-indentation)
108     (max 0
109          (+ left-margin (or (get-text-property
110                              (if (and (eobp) (not (bobp)))
111                                  (1- (point)) (point))
112                              'left-margin) 0)))))
113
114 (defun move-to-left-margin (&optional n force)
115   "Move to the left margin of the current line.
116 With optional argument, move forward N-1 lines first.
117 The column moved to is the one given by the `current-left-margin' function.
118 If the line's indentation appears to be wrong, and this command is called
119 interactively or with optional argument FORCE, it will be fixed."
120   (interactive (list (prefix-numeric-value current-prefix-arg) t))
121   (beginning-of-line n)
122   (skip-chars-forward " \t")
123   (let ((lm (current-left-margin))
124         (cc (current-column)))
125     (cond ((> cc lm)
126            (if (> (move-to-column lm force) lm)
127                ;; If lm is in a tab and we are not forcing, move before tab
128                (backward-char 1)))
129           ((and force (< cc lm))
130            (indent-to-left-margin)))))
131
132 ;; This is the default indent-line-function,
133 ;; used in Fundamental Mode, Text Mode, etc.
134 (defun indent-to-left-margin ()
135   "Indent current line to the column given by `current-left-margin'."
136   (indent-line-to (current-left-margin)))
137
138 (defun delete-to-left-margin (&optional from to)
139   "Remove left margin indentation from a region.
140 The amount of indentation to delete is determined by calling the
141 function `current-left-margin'.
142 In no case will it delete non-whitespace.
143 Args FROM and TO are optional; default is the whole buffer."
144   (save-excursion
145     (goto-char (or to (point-max)))
146     (setq to (point-marker))
147     (goto-char (or from (point-min)))
148     (or (bolp) (forward-line 1))
149     (while (< (point) to)
150       (delete-region (point) (progn (move-to-left-margin nil t) (point)))
151       (forward-line 1))
152     (move-marker to nil)))
153
154 (defun set-left-margin (from to lm)
155   "Set the left margin of the region to WIDTH.
156 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
157   (interactive "r\nNSet left margin to column: ")
158   (if (interactive-p) (setq lm (prefix-numeric-value lm)))
159   (save-excursion
160     ;; If inside indentation, start from BOL.
161     (goto-char from)
162     (skip-chars-backward " \t")
163     (if (bolp) (setq from (point)))
164     ;; Place end after whitespace
165     (goto-char to)
166     (skip-chars-forward " \t")
167     (setq to (point-marker)))
168   ;; Delete margin indentation first, but keep paragraph indentation.
169   (delete-to-left-margin from to)
170   (put-text-property from to 'left-margin lm)
171   (indent-rigidly from to lm)
172   (if auto-fill-function (save-excursion (fill-region from to nil t t)))
173   (move-marker to nil))
174
175 (defun set-right-margin (from to lm)
176   "Set the right margin of the region to WIDTH.
177 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
178   (interactive "r\nNSet right margin to width: ")
179   (if (interactive-p) (setq lm (prefix-numeric-value lm)))
180   (save-excursion
181     (goto-char from)
182     (skip-chars-backward " \t")
183     (if (bolp) (setq from (point))))
184   (put-text-property from to 'right-margin lm)
185   (if auto-fill-function (save-excursion (fill-region from to nil t t))))
186
187 (defun alter-text-property (from to prop func &optional object)
188   "Programmatically change value of a text-property.
189 For each region between FROM and TO that has a single value for PROPERTY,
190 apply FUNCTION to that value and sets the property to the function's result.
191 Optional fifth argument OBJECT specifies the string or buffer to operate on."
192   (let ((begin from)
193         end val)
194     (while (setq val (get-text-property begin prop object)
195                  end (text-property-not-all begin to prop val object))
196       (put-text-property begin end prop (funcall func val) object)
197       (setq begin end))
198     (if (< begin to)
199         (put-text-property begin to prop (funcall func val) object))))
200
201 (defun increase-left-margin (from to inc)
202   "Increase or decrease the left-margin of the region.
203 With no prefix argument, this adds `standard-indent' of indentation.
204 A prefix arg (optional third arg INC noninteractively) specifies the amount
205 to change the margin by, in characters.
206 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
207   (interactive "*r\nP")
208   (setq inc (if inc (prefix-numeric-value inc) standard-indent))
209   (save-excursion
210     (goto-char from)
211     (skip-chars-backward " \t")
212     (if (bolp) (setq from (point)))
213     (goto-char to)
214     (setq to (point-marker)))
215   (alter-text-property from (marker-position to) 'left-margin ; XEmacs
216                        (lambda (v) (max (- left-margin) (+ inc (or v 0)))))
217   (indent-rigidly from (marker-position to) inc) ; XEmacs
218   (if auto-fill-function
219       (save-excursion
220         (fill-region from (marker-position to) nil t t))) ; XEmacs
221   (move-marker to nil))
222
223 (defun decrease-left-margin (from to inc)
224   "Make the left margin of the region smaller.
225 With no prefix argument, decrease the indentation by `standard-indent'.
226 A prefix arg (optional third arg INC noninteractively) specifies the amount
227 to change the margin by, in characters.
228 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
229   (interactive "*r\nP")
230   (setq inc (if inc (prefix-numeric-value inc) standard-indent))
231   (increase-left-margin from to (- inc)))
232
233 (defun increase-right-margin (from to inc)
234   "Increase the right-margin of the region.
235 With no prefix argument, increase the right margin by `standard-indent'.
236 A prefix arg (optional third arg INC noninteractively) specifies the amount
237 to change the margin by, in characters.  A negative argument decreases
238 the right margin width.
239 If `auto-fill-mode' is active, re-fill the region to fit the new margin."
240   (interactive "r\nP")
241   (if (interactive-p)
242       (setq inc (if inc (prefix-numeric-value current-prefix-arg)
243                   standard-indent)))
244   (save-excursion
245     (alter-text-property from to 'right-margin
246        (lambda (v) (+ inc (or v 0))))
247     (if auto-fill-function
248         (fill-region from to nil t t))))
249
250 (defun decrease-right-margin (from to inc)
251   "Make the right margin of the region smaller.
252 With no prefix argument, decrease the right margin by `standard-indent'.
253 A prefix arg (optional third arg INC noninteractively) specifies the amount
254 of width to remove, in characters.  A negative argument increases
255 the right margin width.
256 If `auto-fill-mode' is active, re-fills region to fit in new margin."
257   (interactive "*r\nP")
258   (setq inc (if inc (prefix-numeric-value inc) standard-indent))
259   (increase-right-margin from to (- inc)))
260
261 (defun beginning-of-line-text (&optional n)
262   "Move to the beginning of the text on this line.
263 With optional argument, move forward N-1 lines first.
264 From the beginning of the line, moves past the left-margin indentation, the
265 fill-prefix, and any indentation used for centering or right-justifying the
266 line, but does not move past any whitespace that was explicitly inserted
267 \(such as a tab used to indent the first line of a paragraph)."
268   (interactive "p")
269   (beginning-of-line n)
270   (skip-chars-forward " \t")
271   ;; Skip over fill-prefix.
272   (if (and fill-prefix
273            (not (string-equal fill-prefix "")))
274       (if (equal fill-prefix
275                  (buffer-substring
276                   (point) (min (point-max) (+ (length fill-prefix) (point)))))
277           (forward-char (length fill-prefix)))
278     (if (and adaptive-fill-mode adaptive-fill-regexp
279              (looking-at adaptive-fill-regexp))
280         (goto-char (match-end 0))))
281   ;; Skip centering or flushright indentation
282   (if (memq (current-justification) '(center right))
283       (skip-chars-forward " \t")))
284
285 (defvar indent-region-function nil
286   "Short cut function to indent region using `indent-according-to-mode'.
287 A value of nil means really run `indent-according-to-mode' on each line.")
288
289 (defun indent-region (start end column)
290   "Indent each nonblank line in the region.
291 With no argument, indent each line using `indent-according-to-mode',
292 or use `indent-region-function' to do the whole region if that's non-nil.
293 If there is a fill prefix, make each line start with the fill prefix.
294 With argument COLUMN, indent each line to that column.
295 Called from a program, takes three args: START, END and COLUMN."
296   (interactive "r\nP")
297   (if (null column)
298       (if fill-prefix
299           (save-excursion
300             (goto-char end)
301             (setq end (point-marker))
302             (goto-char start)
303             (let ((regexp (regexp-quote fill-prefix)))
304             (while (< (point) end)
305               (or (looking-at regexp)
306                   (and (bolp) (eolp))
307                   (insert fill-prefix))
308               (forward-line 1))))
309         (if indent-region-function
310             (funcall indent-region-function start end)
311           (save-excursion
312           (goto-char end)
313           (setq end (point-marker))
314           (goto-char start)
315           (or (bolp) (forward-line 1))
316           (while (< (point) end)
317             (or (and (bolp) (eolp))
318                 (funcall indent-line-function))
319             (forward-line 1))
320           (move-marker end nil))))
321     (setq column (prefix-numeric-value column))