- (if (null format)
- ;; Figure out which format it is in, remember list in `format'.
- (let ((try format-alist))
- (while try
- (let* ((f (car try))
- (regexp (nth 2 f))
- (p (point)))
- (if (and regexp (looking-at regexp)
- (< (match-end 0) (+ begin length)))
- (progn
- (setq format (cons (car f) format))
- ;; Decode it
- (if (nth 3 f)
- (setq end (format-decode-run-method (nth 3 f) begin end)))
- ;; Call visit function if required
- (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1))
- ;; Safeguard against either of the functions changing pt.
- (goto-char p)
- ;; Rewind list to look for another format
- (setq try format-alist))
- (setq try (cdr try))))))
- ;; Deal with given format(s)
- (or (listp format) (setq format (list format)))
- (let ((do format) f)
- (while do
- (or (setq f (assq (car do) format-alist))
- (error "Unknown format" (car do)))
- ;; Decode:
- (if (nth 3 f)
- (setq end (format-decode-run-method (nth 3 f) begin end)))
- ;; Call visit function if required
- (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1))
- (setq do (cdr do)))))
- (if visit-flag
- (setq buffer-file-format format))
- (set-buffer-modified-p mod)
- ;; Return new length of region
+ (unwind-protect
+ (progn
+ ;; Don't record undo information for the decoding.
+
+ (if (null format)
+ ;; Figure out which format it is in, remember list in `format'.
+ (let ((try format-alist))
+ (while try
+ (let* ((f (car try))
+ (regexp (nth 2 f))
+ (p (point)))
+ (if (and regexp (looking-at regexp)
+ (< (match-end 0) (+ begin length)))
+ (progn
+ (push (car f) format)
+ ;; Decode it
+ (if (nth 3 f)
+ (setq end (format-decode-run-method (nth 3 f) begin end)))
+ ;; Call visit function if required
+ (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1))
+ ;; Safeguard against either of the functions changing pt.
+ (goto-char p)
+ ;; Rewind list to look for another format
+ (setq try format-alist))
+ (setq try (cdr try))))))
+ ;; Deal with given format(s)
+ (or (listp format) (setq format (list format)))
+ (let ((do format) f)
+ (while do
+ (or (setq f (assq (car do) format-alist))
+ (error "Unknown format" (car do)))
+ ;; Decode:
+ (if (nth 3 f)
+ (setq end (format-decode-run-method (nth 3 f) begin end)))
+ ;; Call visit function if required
+ (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1))
+ (setq do (cdr do))))
+ ;; Encode in the opposite order.
+ (setq format (reverse format)))
+ (if visit-flag
+ (setq buffer-file-format format)))
+
+ (set-buffer-modified-p mod))
+
+ ;; Return new length of region