diff options
| author | Richard M. Stallman | 1995-01-19 04:15:32 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1995-01-19 04:15:32 +0000 |
| commit | 106b6d0e36341b7b6a438c8a1df546205bb59726 (patch) | |
| tree | b920bf93c1cfe934e84b7604ee60efdb53ba05bf | |
| parent | 11fbdf1f6c98b375f3a300dbda120cfae3bccade (diff) | |
| download | emacs-106b6d0e36341b7b6a438c8a1df546205bb59726.tar.gz emacs-106b6d0e36341b7b6a438c8a1df546205bb59726.zip | |
(standard-indent): New variable.
(alter-text-property, current-left-margin, move-to-left-margin)
(delete-to-left-margin, set-left-margin, set-right-margin)
(increase-left-margin, decrease-left-margin, increase-right-margin)
(decrease-right-margin, indent-line-to): New functions.
(indent-to-left-margin): Use indent-line-to. Don't delete or
insert anything unless necessary.
| -rw-r--r-- | lisp/indent.el | 180 |
1 files changed, 169 insertions, 11 deletions
diff --git a/lisp/indent.el b/lisp/indent.el index 27270fb4110..0f3c70ef366 100644 --- a/lisp/indent.el +++ b/lisp/indent.el | |||
| @@ -27,6 +27,9 @@ | |||
| 27 | 27 | ||
| 28 | ;;; Code: | 28 | ;;; Code: |
| 29 | 29 | ||
| 30 | (defvar standard-indent 4 "\ | ||
| 31 | Default number of columns for margin-changing functions to indent.") | ||
| 32 | |||
| 30 | (defvar indent-line-function 'indent-to-left-margin "\ | 33 | (defvar indent-line-function 'indent-to-left-margin "\ |
| 31 | Function to indent current line.") | 34 | Function to indent current line.") |
| 32 | 35 | ||
| @@ -70,20 +73,175 @@ Called from a program, takes three arguments, START, END and ARG." | |||
| 70 | (forward-line 1)) | 73 | (forward-line 1)) |
| 71 | (move-marker end nil))) | 74 | (move-marker end nil))) |
| 72 | 75 | ||
| 76 | (defun indent-line-to (column) | ||
| 77 | "Indent current line to COLUMN. | ||
| 78 | This function removes or adds spaces and tabs at beginning of line | ||
| 79 | only if necessary. It leaves point at end of indentation." | ||
| 80 | (beginning-of-line) | ||
| 81 | (let ((bol (point)) | ||
| 82 | (cur-col (current-indentation))) | ||
| 83 | (cond ((> cur-col column) ; too far right (after tab?) | ||
| 84 | (let ((beg (progn (move-to-column column t) (point)))) | ||
| 85 | (back-to-indentation) | ||
| 86 | (delete-region beg (point)))) | ||
| 87 | ((< cur-col column) | ||
| 88 | (back-to-indentation) | ||
| 89 | (indent-to column))))) | ||
| 90 | |||
| 91 | (defun current-left-margin () | ||
| 92 | "Return the left margin to use for this line. | ||
| 93 | This is the value of the buffer-local variable `left-margin' plus the value | ||
| 94 | of the `left-margin' text-property at the start of the line." | ||
| 95 | (save-excursion | ||
| 96 | (back-to-indentation) | ||
| 97 | (max 0 | ||
| 98 | (+ left-margin (or (get-text-property (point) 'left-margin) 0))))) | ||
| 99 | |||
| 100 | (defun move-to-left-margin (&optional n) | ||
| 101 | "Move to the left margin of the current line. | ||
| 102 | With optional argument, move forward N-1 lines first. | ||
| 103 | The column moved to is the one given by the `left-margin' function, or the | ||
| 104 | column where text actually starts if the region is centered or right-justified. | ||
| 105 | When called interactively, this function corrects the line's indentation | ||
| 106 | if it appears to be incorrect. | ||
| 107 | When called noninteractively, however, it just moves to the beginning of | ||
| 108 | the text in this case." | ||
| 109 | (interactive "p") | ||
| 110 | (beginning-of-line n) | ||
| 111 | (skip-chars-forward " \t") | ||
| 112 | (if (not (memq (justification) '(right center))) | ||
| 113 | (let ((cc (current-column)) | ||
| 114 | (lm (current-left-margin))) | ||
| 115 | (cond ((> cc lm) | ||
| 116 | (move-to-column lm t)) | ||
| 117 | ((and (< cc lm) (interactive-p)) | ||
| 118 | (indent-to-left-margin)))))) | ||
| 119 | |||
| 73 | ;; This is the default indent-line-function, | 120 | ;; This is the default indent-line-function, |
| 74 | ;; used in Fundamental Mode, Text Mode, etc. | 121 | ;; used in Fundamental Mode, Text Mode, etc. |
| 75 | (defun indent-to-left-margin () | 122 | (defun indent-to-left-margin () |
| 76 | (or (= (current-indentation) left-margin) | 123 | "Indent current line to `left-margin'." |
| 77 | (let (epos) | 124 | (indent-line-to (current-left-margin))) |
| 78 | (save-excursion | 125 | |
| 79 | (beginning-of-line) | 126 | (defun delete-to-left-margin (from to) |
| 80 | (delete-region (point) | 127 | "Remove left-margin indentation from region. |
| 81 | (progn (skip-chars-forward " \t") | 128 | This is careful only to delete whitespace, and not to delete any more than |
| 129 | the \(current-left-margin) value for each line." | ||
| 130 | (save-excursion | ||
| 131 | (goto-char to) | ||
| 132 | (setq to (point-marker)) | ||
| 133 | (goto-char from) | ||
| 134 | (or (bolp) (forward-line 1)) | ||
| 135 | (while (< (point) to) | ||
| 136 | (delete-region (point) (let ((lm (current-left-margin))) | ||
| 137 | (skip-chars-forward " \t") | ||
| 138 | (if (> (current-column) lm) | ||
| 139 | (move-to-column lm)) | ||
| 82 | (point))) | 140 | (point))) |
| 83 | (indent-to left-margin) | 141 | (forward-line 1)) |
| 84 | (setq epos (point))) | 142 | (move-marker to nil))) |
| 85 | (if (< (point) epos) | 143 | |
| 86 | (goto-char epos))))) | 144 | (defun set-left-margin (from to lm) |
| 145 | "Set the left margin of the region to WIDTH. | ||
| 146 | If `auto-fill-mode' is active, re-fill the region to fit the new margin." | ||
| 147 | (interactive "r\nNSet left margin to column: ") | ||
| 148 | (if (interactive-p) (setq lm (prefix-numeric-value lm))) | ||
| 149 | (save-excursion | ||
| 150 | ;; If inside indentation, start from BOL. | ||
| 151 | (goto-char from) | ||
| 152 | (skip-chars-backward " \t") | ||
| 153 | (if (bolp) (setq from (point))) | ||
| 154 | (goto-char to) | ||
| 155 | (setq to (point-marker))) | ||
| 156 | ;; Delete indentation first, so that paragraph indentation is preserved. | ||
| 157 | (if auto-fill-function (delete-to-left-margin from to)) | ||
| 158 | (put-text-property from to 'left-margin lm) | ||
| 159 | (if auto-fill-function (fill-region from to nil t t)) | ||
| 160 | (move-marker to nil)) | ||
| 161 | |||
| 162 | (defun set-right-margin (from to lm) | ||
| 163 | "Set the right margin of the region to WIDTH. | ||
| 164 | If `auto-fill-mode' is active, re-fill the region to fit the new margin." | ||
| 165 | (interactive "r\nNSet left margin to column: ") | ||
| 166 | (if (interactive-p) (setq lm (prefix-numeric-value lm))) | ||
| 167 | (save-excursion | ||
| 168 | (goto-char from) | ||
| 169 | (skip-chars-backward " \t") | ||
| 170 | (if (bolp) (setq from (point)))) | ||
| 171 | (put-text-property from to 'right-margin lm) | ||
| 172 | (if auto-fill-function (fill-region from to nil t t))) | ||
| 173 | |||
| 174 | (defun alter-text-property (from to prop func &optional object) | ||
| 175 | "Programmatically change value of a text-property. | ||
| 176 | For each region between FROM and TO that has a single value for PROPERTY, | ||
| 177 | apply FUNCTION to that value and sets the property to the function's result. | ||
| 178 | Optional fifth argument OBJECT specifies the string or buffer to operate on." | ||
| 179 | (let ((begin from) | ||
| 180 | end val) | ||
| 181 | (while (setq val (get-text-property begin prop object) | ||
| 182 | end (text-property-not-all begin to prop val object)) | ||
| 183 | (put-text-property begin end prop (funcall func val) object) | ||
| 184 | (setq begin end)) | ||
| 185 | (if (< begin to) | ||
| 186 | (put-text-property begin to prop (funcall func val) object)))) | ||
| 187 | |||
| 188 | (defun increase-left-margin (from to inc) | ||
| 189 | "Increase or decrease the left-margin of the region. | ||
| 190 | With no prefix argument, this adds `standard-indent' of indentation. | ||
| 191 | A prefix arg (optional third arg INC noninteractively) specifies the amount | ||
| 192 | to change the margin by, in characters. | ||
| 193 | If `auto-fill-mode' is active, re-fill the region to fit the new margin." | ||
| 194 | (interactive "*r\nP") | ||
| 195 | (setq inc (if inc (prefix-numeric-value inc) standard-indent)) | ||
| 196 | (save-excursion | ||
| 197 | (goto-char from) | ||
| 198 | (skip-chars-backward " \t") | ||
| 199 | (if (bolp) (setq from (point))) | ||
| 200 | (goto-char to) | ||
| 201 | (setq to (point-marker))) | ||
| 202 | (if auto-fill-function (delete-to-left-margin from to)) | ||
| 203 | (alter-text-property from to 'left-margin | ||
| 204 | (lambda (v) (max 0 (+ inc (or v 0))))) | ||
| 205 | (if auto-fill-function (fill-region from to nil t t)) | ||
| 206 | (move-marker to nil)) | ||
| 207 | |||
| 208 | (defun decrease-left-margin (from to inc) | ||
| 209 | "Make the left margin of the region smaller. | ||
| 210 | With no prefix argument, decrease the indentation by `standard-indent'. | ||
| 211 | A prefix arg (optional third arg INC noninteractively) specifies the amount | ||
| 212 | to change the margin by, in characters. | ||
| 213 | If `auto-fill-mode' is active, re-fill the region to fit the new margin." | ||
| 214 | (interactive "*r\nP") | ||
| 215 | (setq inc (if inc (prefix-numeric-value inc) standard-indent)) | ||
| 216 | (increase-left-margin from to (- inc))) | ||
| 217 | |||
| 218 | (defun increase-right-margin (from to inc) | ||
| 219 | "Increase the right-margin of the region. | ||
| 220 | With no prefix argument, increase the right margin by `standard-indent'. | ||
| 221 | A prefix arg (optional third arg INC noninteractively) specifies the amount | ||
| 222 | to change the margin by, in characters. A negative argument decreases | ||
| 223 | the right margin width. | ||
| 224 | If `auto-fill-mode' is active, re-fill the region to fit the new margin." | ||
| 225 | (interactive "r\nP") | ||
| 226 | (if (interactive-p) | ||
| 227 | (setq inc (if inc (prefix-numeric-value current-prefix-arg) | ||
| 228 | standard-indent))) | ||
| 229 | (save-excursion | ||
| 230 | (alter-text-property from to 'right-margin | ||
| 231 | (lambda (v) (max 0 (+ inc (or v 0))))) | ||
| 232 | (if auto-fill-function | ||
| 233 | (fill-region from to nil t t)))) | ||
| 234 | |||
| 235 | (defun decrease-right-margin (from to inc) | ||
| 236 | "Make the right margin of the region smaller. | ||
| 237 | With no prefix argument, decrease the right margin by `standard-indent'. | ||
| 238 | A prefix arg (optional third arg INC noninteractively) specifies the amount | ||
| 239 | of width to remove, in characters. A negative argument increases | ||
| 240 | the right margin width. | ||
| 241 | If `auto-fill-mode' is active, re-fills region to fit in new margin." | ||
| 242 | (interactive "*r\nP") | ||
| 243 | (setq inc (if inc (prefix-numeric-value inc) standard-indent)) | ||
| 244 | (increase-right-margin from to (- inc))) | ||
| 87 | 245 | ||
| 88 | (defvar indent-region-function nil | 246 | (defvar indent-region-function nil |
| 89 | "Function which is short cut to indent region using indent-according-to-mode. | 247 | "Function which is short cut to indent region using indent-according-to-mode. |
| @@ -130,7 +288,7 @@ Called from a program, takes three args: START, END and COLUMN." | |||
| 130 | (while (< (point) end) | 288 | (while (< (point) end) |
| 131 | (delete-region (point) (progn (skip-chars-forward " \t") (point))) | 289 | (delete-region (point) (progn (skip-chars-forward " \t") (point))) |
| 132 | (or (eolp) | 290 | (or (eolp) |
| 133 | (indent-to column 0)) | 291 | (indent-to column 0)) |
| 134 | (forward-line 1)) | 292 | (forward-line 1)) |
| 135 | (move-marker end nil)))) | 293 | (move-marker end nil)))) |
| 136 | 294 | ||