Fixed.
[elisp/epg.git] / epa-file.el
1 ;;; epa-file.el --- the EasyPG Assistant hooks for transparent file encryption
2 ;; Copyright (C) 2006 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
6
7 ;; This file is part of EasyPG.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Code:
25
26 (require 'epa)
27
28 (defgroup epa-file nil
29   "The EasyPG Assistant hooks for transparent file encryption"
30   :group 'epa)
31
32 (defcustom epa-file-name-regexp "\\.gpg\\'"
33   "Regexp which matches filenames to be encrypted with GnuPG."
34   :type 'regexp
35   :group 'epa-file)
36   
37 (defvar epa-file nil)
38
39 (defun epa-find-file ()
40   (when (string-match epa-file-name-regexp (buffer-file-name))
41     (if (= (buffer-size) 0)
42         (progn
43           (set-auto-mode)
44           (hack-local-variables)
45           (auto-save-mode nil))
46       (goto-char (point-min))
47       (if (fboundp 'set-buffer-multibyte)
48           (set-buffer-multibyte t))
49       (insert (epg-decrypt-file (epg-make-context)
50                                 (expand-file-name (buffer-file-name))
51                                 nil))
52       (delete-region (point) (point-max))
53       (decode-coding-region (point-min) (point-max) 'undecided)
54       (set-buffer-modified-p nil))
55     (make-local-variable 'epa-file)
56     (setq epa-file (buffer-file-name))))
57
58 (defun epa-write-file ()
59   (when epa-file
60     (write-region
61      (epg-encrypt-string
62       (epg-make-context)
63       (buffer-string)
64       (mapcar (lambda (key)
65                 (epg-sub-key-id
66                  (car (epg-key-sub-key-list key))))
67               (epa-select-keys
68                  "Select recipents for encryption.
69 If no one is selected, symmetric encryption will be performed.  ")))
70      nil (expand-file-name (buffer-file-name)))
71     (set-buffer-modified-p nil)
72     t))
73
74 (provide 'epa-file)
75
76 ;;; epa-file.el ends here