-(defun epa-find-file ()
- (when (string-match epa-file-name-regexp (buffer-file-name))
- (when (file-exists-p (expand-file-name (buffer-file-name)))
- (make-local-variable 'epa-file)
- (make-local-variable 'epa-passphrase)
- (if (fboundp 'set-buffer-multibyte)
- (set-buffer-multibyte t))
- (goto-char (point-min))
- (let ((context (epg-make-context)))
- (epg-context-set-passphrase-callback
- context
- (cons #'epa-file-passphrase-callback-function
- (current-buffer)))
- (insert (epg-decrypt-file context
- (expand-file-name (buffer-file-name))
- nil)))
- (delete-region (point) (point-max))
- (decode-coding-region (point-min) (point-max) 'undecided)
- (if (boundp 'last-coding-system-used)
- (set-buffer-file-coding-system last-coding-system-used)
- (set-buffer-file-coding-system default-buffer-file-coding-system))
- (set-auto-mode)
- (hack-local-variables)
- (auto-save-mode nil)
- (set-buffer-modified-p nil)
- (setq buffer-undo-list nil
- epa-file (buffer-file-name)))))
-
-(defun epa-write-file ()
- (when epa-file
- (let* ((coding-system (if (boundp 'last-coding-system-used)
- (condition-case nil
- (write-region (point-min) (point-max) "/")
- (error last-coding-system-used))
- buffer-file-coding-system))
- (coding-system-for-write 'binary)
- (context (epg-make-context)))
- (epg-context-set-passphrase-callback
- context
- (cons #'epa-file-passphrase-callback-function
- (current-buffer)))
- (write-region
- (epg-encrypt-string
- context
- (encode-coding-string (buffer-string) coding-system)
- (mapcar (lambda (key)
- (epg-sub-key-id (car (epg-key-sub-key-list key))))
- (unless epa-passphrase
- (epa-select-keys
- "Select recipents for encryption.
+(defun epa-file-insert-file-contents (file &optional visit beg end replace)
+ (barf-if-buffer-read-only)
+ (if (or beg end)
+ (error "Can't read the file partially."))
+ (setq file (expand-file-name file))
+ (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
+ (context (epg-make-context))
+ string length entry)
+ (if visit
+ (setq buffer-file-name file))
+ (epg-context-set-passphrase-callback
+ context
+ (cons #'epa-file-passphrase-callback-function
+ file))
+ (unwind-protect
+ (progn
+ (if replace
+ (goto-char (point-min)))
+ (condition-case error
+ (setq string (decode-coding-string
+ (epg-decrypt-file context file nil)
+ 'undecided))
+ (error
+ (if (setq entry (assoc file epa-file-passphrase-alist))
+ (setcdr entry nil))
+ (signal 'file-error
+ (cons "Opening input file" (cdr error)))))
+ (if (boundp 'last-coding-system-used)
+ (set-buffer-file-coding-system last-coding-system-used)
+ (set-buffer-file-coding-system default-buffer-file-coding-system))
+ (insert string)
+ (setq length (length string))
+ (if replace
+ (delete-region (point) (point-max))))
+ (if (and local-copy
+ (file-exists-p local-copy))
+ (delete-file local-copy)))
+ (list file length)))
+(put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
+
+(defun epa-file-write-region (start end file &optional append visit lockname
+ mustbenew)
+ (if append
+ (error "Can't append to the file."))
+ (setq file (expand-file-name file))
+ (let* ((coding-system (if (boundp 'last-coding-system-used)
+ (condition-case nil
+ (write-region (point-min) (point-max) "/")
+ (error last-coding-system-used))
+ buffer-file-coding-system))
+ (context (epg-make-context))
+ (coding-system-for-write 'binary)
+ string entry)
+ (epg-context-set-passphrase-callback
+ context
+ (cons #'epa-file-passphrase-callback-function
+ file))
+ (condition-case error
+ (setq string
+ (epg-encrypt-string
+ context
+ (if (stringp start)
+ (encode-coding-string start coding-system)
+ (encode-coding-string (buffer-substring start end)
+ coding-system))
+ (unless (assoc file epa-file-passphrase-alist)
+ (epa-select-keys
+ "Select recipents for encryption.