diff options
| -rw-r--r-- | lisp/format.el | 106 |
1 files changed, 60 insertions, 46 deletions
diff --git a/lisp/format.el b/lisp/format.el index fccbdb22cb3..dba7ecbff2a 100644 --- a/lisp/format.el +++ b/lisp/format.el | |||
| @@ -154,14 +154,15 @@ BUFFER should be the buffer that the output originally came from." | |||
| 154 | (with-current-buffer error-buff | 154 | (with-current-buffer error-buff |
| 155 | (widen) | 155 | (widen) |
| 156 | (erase-buffer)) | 156 | (erase-buffer)) |
| 157 | (if (and (zerop (shell-command-on-region from to method t t | 157 | (if (and (zerop (save-window-excursion |
| 158 | error-buff)) | 158 | (shell-command-on-region from to method t t |
| 159 | error-buff))) | ||
| 159 | ;; gzip gives zero exit status with bad args, for instance. | 160 | ;; gzip gives zero exit status with bad args, for instance. |
| 160 | (zerop (with-current-buffer error-buff | 161 | (zerop (with-current-buffer error-buff |
| 161 | (buffer-size)))) | 162 | (buffer-size)))) |
| 162 | (bury-buffer error-buff) | 163 | (bury-buffer error-buff) |
| 163 | (switch-to-buffer-other-window error-buff) | 164 | (switch-to-buffer-other-window error-buff) |
| 164 | (error "Format decoding failed"))) | 165 | (error "Format encoding failed"))) |
| 165 | (funcall method from to buffer))) | 166 | (funcall method from to buffer))) |
| 166 | 167 | ||
| 167 | (defun format-decode-run-method (method from to &optional buffer) | 168 | (defun format-decode-run-method (method from to &optional buffer) |
| @@ -190,26 +191,32 @@ a Lisp function. Decoding is done for the given BUFFER." | |||
| 190 | (point)) | 191 | (point)) |
| 191 | (funcall method from to))) | 192 | (funcall method from to))) |
| 192 | 193 | ||
| 193 | (defun format-annotate-function (format from to orig-buf) | 194 | (defun format-annotate-function (format from to orig-buf format-count) |
| 194 | "Return annotations for writing region as FORMAT. | 195 | "Return annotations for writing region as FORMAT. |
| 195 | FORMAT is a symbol naming one of the formats defined in `format-alist', | 196 | FORMAT is a symbol naming one of the formats defined in `format-alist', |
| 196 | it must be a single symbol, not a list like `buffer-file-format'. | 197 | it must be a single symbol, not a list like `buffer-file-format'. |
| 197 | FROM and TO delimit the region to be operated on in the current buffer. | 198 | FROM and TO delimit the region to be operated on in the current buffer. |
| 198 | ORIG-BUF is the original buffer that the data came from. | 199 | ORIG-BUF is the original buffer that the data came from. |
| 200 | |||
| 201 | FORMAT-COUNT is an integer specifying how many times this function has | ||
| 202 | been called in the process of decoding ORIG-BUF. | ||
| 203 | |||
| 199 | This function works like a function on `write-region-annotate-functions': | 204 | This function works like a function on `write-region-annotate-functions': |
| 200 | it either returns a list of annotations, or returns with a different buffer | 205 | it either returns a list of annotations, or returns with a different buffer |
| 201 | current, which contains the modified text to write. | 206 | current, which contains the modified text to write. In the latter case, |
| 207 | this function's value is nil. | ||
| 202 | 208 | ||
| 203 | For most purposes, consider using `format-encode-region' instead." | 209 | For most purposes, consider using `format-encode-region' instead." |
| 204 | ;; This function is called by write-region (actually build-annotations) | 210 | ;; This function is called by write-region (actually |
| 205 | ;; for each element of buffer-file-format. | 211 | ;; build_annotations) for each element of buffer-file-format. |
| 206 | (let* ((info (assq format format-alist)) | 212 | (let* ((info (assq format format-alist)) |
| 207 | (to-fn (nth 4 info)) | 213 | (to-fn (nth 4 info)) |
| 208 | (modify (nth 5 info))) | 214 | (modify (nth 5 info))) |
| 209 | (if to-fn | 215 | (if to-fn |
| 210 | (if modify | 216 | (if modify |
| 211 | ;; To-function wants to modify region. Copy to safe place. | 217 | ;; To-function wants to modify region. Copy to safe place. |
| 212 | (let ((copy-buf (get-buffer-create " *Format Temp*"))) | 218 | (let ((copy-buf (get-buffer-create (format " *Format Temp %d*" |
| 219 | format-count)))) | ||
| 213 | (copy-to-buffer copy-buf from to) | 220 | (copy-to-buffer copy-buf from to) |
| 214 | (set-buffer copy-buf) | 221 | (set-buffer copy-buf) |
| 215 | (format-insert-annotations write-region-annotations-so-far from) | 222 | (format-insert-annotations write-region-annotations-so-far from) |
| @@ -236,45 +243,52 @@ Returns the new length of the decoded region. | |||
| 236 | 243 | ||
| 237 | For most purposes, consider using `format-decode-region' instead." | 244 | For most purposes, consider using `format-decode-region' instead." |
| 238 | (let ((mod (buffer-modified-p)) | 245 | (let ((mod (buffer-modified-p)) |
| 239 | (begin (point)) | 246 | (begin (point)) |
| 240 | (end (+ (point) length))) | 247 | (end (+ (point) length))) |
| 241 | (if (null format) | 248 | (unwind-protect |
| 242 | ;; Figure out which format it is in, remember list in `format'. | 249 | (progn |
| 243 | (let ((try format-alist)) | 250 | ;; Don't record undo information for the decoding. |
| 244 | (while try | 251 | (setq buffer-undo-list t) |
| 245 | (let* ((f (car try)) | 252 | |
| 246 | (regexp (nth 2 f)) | 253 | (if (null format) |
| 247 | (p (point))) | 254 | ;; Figure out which format it is in, remember list in `format'. |
| 248 | (if (and regexp (looking-at regexp) | 255 | (let ((try format-alist)) |
| 249 | (< (match-end 0) (+ begin length))) | 256 | (while try |
| 250 | (progn | 257 | (let* ((f (car try)) |
| 251 | (setq format (cons (car f) format)) | 258 | (regexp (nth 2 f)) |
| 252 | ;; Decode it | 259 | (p (point))) |
| 253 | (if (nth 3 f) | 260 | (if (and regexp (looking-at regexp) |
| 254 | (setq end (format-decode-run-method (nth 3 f) begin end))) | 261 | (< (match-end 0) (+ begin length))) |
| 255 | ;; Call visit function if required | 262 | (progn |
| 256 | (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) | 263 | (setq format (cons (car f) format)) |
| 257 | ;; Safeguard against either of the functions changing pt. | 264 | ;; Decode it |
| 258 | (goto-char p) | 265 | (if (nth 3 f) |
| 259 | ;; Rewind list to look for another format | 266 | (setq end (format-decode-run-method (nth 3 f) begin end))) |
| 260 | (setq try format-alist)) | 267 | ;; Call visit function if required |
| 261 | (setq try (cdr try)))))) | 268 | (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) |
| 262 | ;; Deal with given format(s) | 269 | ;; Safeguard against either of the functions changing pt. |
| 263 | (or (listp format) (setq format (list format))) | 270 | (goto-char p) |
| 264 | (let ((do format) f) | 271 | ;; Rewind list to look for another format |
| 265 | (while do | 272 | (setq try format-alist)) |
| 266 | (or (setq f (assq (car do) format-alist)) | 273 | (setq try (cdr try)))))) |
| 267 | (error "Unknown format" (car do))) | 274 | ;; Deal with given format(s) |
| 268 | ;; Decode: | 275 | (or (listp format) (setq format (list format))) |
| 269 | (if (nth 3 f) | 276 | (let ((do format) f) |
| 270 | (setq end (format-decode-run-method (nth 3 f) begin end))) | 277 | (while do |
| 271 | ;; Call visit function if required | 278 | (or (setq f (assq (car do) format-alist)) |
| 272 | (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) | 279 | (error "Unknown format" (car do))) |
| 273 | (setq do (cdr do))))) | 280 | ;; Decode: |
| 274 | (if visit-flag | 281 | (if (nth 3 f) |
| 275 | (setq buffer-file-format format)) | 282 | (setq end (format-decode-run-method (nth 3 f) begin end))) |
| 276 | (set-buffer-modified-p mod) | 283 | ;; Call visit function if required |
| 277 | ;; Return new length of region | 284 | (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) |
| 285 | (setq do (cdr do))))) | ||
| 286 | (if visit-flag | ||
| 287 | (setq buffer-file-format format))) | ||
| 288 | |||
| 289 | (set-buffer-modified-p mod) | ||
| 290 | (setq buffer-undo-list nil)) | ||
| 291 | ;; Return new length of region | ||
| 278 | (- end begin))) | 292 | (- end begin))) |
| 279 | 293 | ||
| 280 | ;;; | 294 | ;;; |