-(defun epa-find-file ()
- (when (string-match epa-file-name-regexp (buffer-file-name))
- (if (file-exists-p (expand-file-name (buffer-file-name)))
- (let ((context (epg-make-context))
- passphrase)
- (if (fboundp 'set-buffer-multibyte)
- (set-buffer-multibyte t))
- (goto-char (point-min))
- (epg-context-set-passphrase-callback
- context
- (cons #'epa-file-passphrase-callback-function
- (current-buffer)))
- (make-local-variable 'epa-passphrase)
- (insert (epg-decrypt-file context
- (expand-file-name (buffer-file-name))
- nil))
- (setq passphrase epa-passphrase)
- (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)
- (make-local-variable 'epa-passphrase)
- (setq epa-passphrase passphrase)))
- (make-local-variable 'epa-passphrase)
- (make-local-variable 'epa-file)
- (setq epa-file (buffer-file-name))))
-
-(defun epa-write-file ()
- (if 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))
- (context (epg-make-context))
- (coding-system-for-write 'binary))
- (epg-context-set-passphrase-callback
+(defun epa-file-insert-file-contents (file &optional visit beg end replace)
+ (barf-if-buffer-read-only)
+ (if (and visit (or beg end))
+ (error "Attempt to visit less than an entire file"))
+ (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))
+ (epg-context-set-progress-callback context
+ #'epa-progress-callback-function)
+ (unwind-protect
+ (progn
+ (if replace
+ (goto-char (point-min)))
+ (condition-case error
+ (setq string (epg-decrypt-file context file nil))
+ (error
+ (if (setq entry (assoc file epa-file-passphrase-alist))
+ (setcdr entry nil))
+ (signal 'file-error
+ (cons "Opening input file" (cdr error)))))
+ (make-local-variable 'epa-file-encrypt-to)
+ (setq epa-file-encrypt-to
+ (mapcar #'car (epg-context-result-for context 'encrypted-to)))
+ (if (or beg end)
+ (setq string (substring string (or beg 0) end)))
+ (save-excursion
+ (save-restriction
+ (narrow-to-region (point) (point))
+ (epa-file-decode-and-insert string file visit beg end replace)
+ (setq length (- (point-max) (point-min))))
+ (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 (or coding-system-for-write
+ (if (fboundp 'select-safe-coding-system)
+ ;; This is needed since Emacs 22 has
+ ;; no-conversion setting for *.gpg in
+ ;; `auto-coding-alist'.
+ (let ((buffer-file-name
+ (file-name-sans-extension file)))
+ (select-safe-coding-system
+ (point-min) (point-max)))
+ buffer-file-coding-system)))
+ (context (epg-make-context))
+ (coding-system-for-write 'binary)
+ string entry
+ (recipients
+ (cond
+ ((listp epa-file-encrypt-to) epa-file-encrypt-to)
+ ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))
+ (epg-context-set-passphrase-callback
+ context
+ (cons #'epa-file-passphrase-callback-function
+ file))
+ (epg-context-set-progress-callback context
+ #'epa-progress-callback-function)
+ (condition-case error
+ (setq string
+ (epg-encrypt-string
+ context
+ (if (stringp start)
+ (epa-file--encode-coding-string start coding-system)
+ (epa-file--encode-coding-string (buffer-substring start end)
+ coding-system))
+ (if (or epa-file-select-keys
+ (not (local-variable-p 'epa-file-encrypt-to)))
+ (epa-select-keys
+ context
+ "Select recipents for encryption.
+If no one is selected, symmetric encryption will be performed. "
+ recipients)
+ (if epa-file-encrypt-to
+ (epg-list-keys context recipients)))))
+ (error
+ (if (setq entry (assoc file epa-file-passphrase-alist))
+ (setcdr entry nil))
+ (signal 'file-error (cons "Opening output file" (cdr error)))))
+ (epa-file-run-real-handler
+ #'write-region
+ (list string nil file append visit lockname mustbenew))
+ (if (boundp 'last-coding-system-used)
+ (setq last-coding-system-used coding-system))
+ (if (eq visit t)
+ (progn
+ (setq buffer-file-name file)
+ (set-visited-file-modtime))
+ (if (stringp visit)
+ (progn
+ (set-visited-file-modtime)
+ (setq buffer-file-name visit))))
+ (if (or (eq visit t)
+ (eq visit nil)
+ (stringp visit))
+ (message "Wrote %s" buffer-file-name))))
+(put 'write-region 'epa-file 'epa-file-write-region)
+
+(defun epa-file-find-file-hook ()
+ (if (and buffer-file-name
+ (string-match epa-file-name-regexp buffer-file-name)
+ epa-file-inhibit-auto-save)
+ (auto-save-mode 0)))
+
+(defun epa-file-select-keys ()
+ "Select recipients for encryption."
+ (interactive)
+ (make-local-variable 'epa-file-encrypt-to)
+ (setq epa-file-encrypt-to
+ (epa-select-keys