Spawn new process with ADDR_NO_RANDOMIZE personality if not already set
[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))
322     (save-excursion
323       (goto-char end)
324       (setq end (point-marker))
325       (goto-char start)
326       (or (bolp) (forward-line 1))
327       (while (< (point) end)
328         (delete-region (point) (progn (skip-chars-forward " \t") (point)))
329         (or (eolp)
330             (indent-to column 0))
331         (forward-line 1))
332       (move-marker end nil))))
333
334 (defun indent-relative-maybe ()
335   "Indent a new line like previous nonblank line."
336   (interactive)
337   (indent-relative t))
338
339 (defun indent-relative (&optional unindented-ok)
340   "Space out to under next indent point in previous nonblank line.
341 An indent point is a non-whitespace character following whitespace.
342 If the previous nonblank line has no indent points beyond the
343 column point starts at, `tab-to-tab-stop' is done instead."
344   (interactive "P")
345   (if abbrev-mode (expand-abbrev))
346   (let ((start-column (current-column))
347         indent)
348     (save-excursion
349       (beginning-of-line)
350       (if (re-search-backward "^[^\n]" nil t)
351           (let ((end (save-excursion (forward-line 1) (point))))
352             (move-to-column start-column)
353             ;; Is start-column inside a tab on this line?
354             (if (> (current-column) start-column)
355                 (backward-char 1))
356             (or (looking-at "[ \t]")
357                 unindented-ok
358                 (skip-chars-forward "^ \t" end))
359             (skip-chars-forward " \t" end)
360             (or (= (point) end) (setq indent (current-column))))))
361     (if indent
362         (let ((opoint (point-marker)))
363           (delete-region (point) (progn (skip-chars-backward " \t") (point)))
364           (indent-to indent 0)
365           (if (> opoint (point))
366               (goto-char opoint))
367           (move-marker opoint nil))
368       (tab-to-tab-stop))))
369
370 (defvar tab-stop-list
371   '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120)
372   "*List of tab stop positions used by `tab-to-tab-stops'.
373 This should be a list of integers, ordered from smallest to largest.")
374
375 (defvar edit-tab-stops-map nil "Keymap used in `edit-tab-stops'.")
376 (if edit-tab-stops-map
377     nil
378   (setq edit-tab-stops-map (make-sparse-keymap))
379   (define-key edit-tab-stops-map "\C-x\C-s" 'edit-tab-stops-note-changes)
380   (define-key edit-tab-stops-map "\C-c\C-c" 'edit-tab-stops-note-changes))
381
382 (defvar edit-tab-stops-buffer nil
383   "Buffer whose tab stops are being edited--in case
384 the variable `tab-stop-list' is local in that buffer.")
385
386 (defun edit-tab-stops ()
387   "Edit the tab stops used by `tab-to-tab-stop'.
388 Creates a buffer *Tab Stops* containing text describing the tab stops.
389 A colon indicates a column where there is a tab stop.
390 You can add or remove colons and then do \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes] to make changes take effect."
391   (interactive)
392   (setq edit-tab-stops-buffer (current-buffer))
393   (switch-to-buffer (get-buffer-create "*Tab Stops*"))
394   ;; #### I18N3 should mark buffer as output-translating
395   (use-local-map edit-tab-stops-map)
396   (make-local-variable 'indent-tabs-mode)
397   (setq indent-tabs-mode nil)
398   (overwrite-mode 1)
399   (setq truncate-lines t)
400   (erase-buffer)
401   (let ((tabs tab-stop-list))
402     (while tabs
403       (indent-to (car tabs) 0)
404       (insert ?:)
405       (setq tabs (cdr tabs))))
406   (let ((count 0))
407     (insert ?\n)
408     (while (< count 8)
409       (insert (+ count ?0))
410     (insert "         ")
411       (setq count (1+ count)))
412     (insert ?\n)
413     (while (> count 0)
414       (insert "0123456789")
415       (setq count (1- count))))
416   ;; XEmacs
417   (insert (substitute-command-keys "\nTo install changes, type \\<edit-tab-stops-map>\\[edit-tab-stops-note-changes]"))
418   (goto-char (point-min)))
419
420 (defun edit-tab-stops-note-changes ()
421   "Put edited tab stops into effect."
422   (interactive)
423     (let (tabs)
424       (save-excursion
425         (goto-char 1)
426         (end-of-line)
427         (while (search-backward ":" nil t)
428           (setq tabs (cons (current-column) tabs))))
429       (bury-buffer (prog1 (current-buffer)
430                           (switch-to-buffer edit-tab-stops-buffer)))
431       (setq tab-stop-list tabs))
432   (message "Tab stops installed"))
433
434 (defun tab-to-tab-stop ()
435   "Insert spaces or tabs to next defined tab-stop column.
436 The variable `tab-stop-list' is a list of columns at which there are tab stops.
437 Use \\[edit-tab-stops] to edit them interactively."
438   (interactive)
439   (and abbrev-mode (eq (char-syntax (char-before (point))) ?w)
440        (expand-abbrev))
441   (let ((tabs tab-stop-list))
442     (while (and tabs (>= (current-column) (car tabs)))
443       (setq tabs (cdr tabs)))
444     (if tabs
445         (let ((opoint (point)))
446           (skip-chars-backward " \t")
447           (delete-region (point) opoint)
448           (indent-to (car tabs)))
449       (insert ?\ ))))
450
451 (defun move-to-tab-stop ()
452   "Move point to next defined tab-stop column.
453 The variable `tab-stop-list' is a list of columns at which there are tab stops.
454 Use \\[edit-tab-stops] to edit them interactively."
455   (interactive)
456   (let ((tabs tab-stop-list))
457     (while (and tabs (>= (current-column) (car tabs)))
458       (setq tabs (cdr tabs)))
459     (if tabs
460         (let ((before (point)))
461           (move-to-column (car tabs) t)
462           (save-excursion
463             (goto-char before)
464             ;; If we just added a tab, or moved over one,
465             ;; delete any superfluous spaces before the old point.
466             (if (and (eq (char-before (point)) ?\ )
467                      (eq (char-after (point)) ?\t))
468                 (let ((tabend (* (/ (current-column) tab-width) tab-width)))
469                   (while (and (> (current-column) tabend)
470                               (eq (char-before (point)) ?\ ))
471                     (backward-char 1))
472                   (delete-region (point) before))))))))
473
474 ;(define-key global-map "\t" 'indent-for-tab-command)
475 ;(define-key esc-map "\034" 'indent-region)
476 ;(define-key ctl-x-map "\t" 'indent-rigidly)
477 ;(define-key esc-map "i" 'tab-to-tab-stop)
478
479 ;;; indent.el ends here