* epa-file-hook.el: New file split from epa-file.el.
[bpt/emacs.git] / lisp / epa-file-hook.el
1 ;;; epa-file-hook.el --- preloaded code to enable epa-file.el
2 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs 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 3, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs 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 (defgroup epa-file nil
27 "The EasyPG Assistant hooks for transparent file encryption"
28 :version "23.1"
29 :group 'epa)
30
31 (defun epa-file--file-name-regexp-set (variable value)
32 (set-default variable value)
33 (if (fboundp 'epa-file-name-regexp-update)
34 (epa-file-name-regexp-update)))
35
36 (defcustom epa-file-name-regexp "\\.gpg\\(~\\|\\.~[0-9]+~\\)?\\'"
37 "Regexp which matches filenames to be encrypted with GnuPG.
38
39 If you set this outside Custom while epa-file is already enabled, you
40 have to call `epa-file-name-regexp-update' after setting it to
41 properly update file-name-handler-alist. Setting this through Custom
42 does that automatically."
43 :type 'regexp
44 :group 'epa-file
45 :set 'epa-file--file-name-regexp-set)
46
47 (defcustom epa-file-inhibit-auto-save t
48 "If non-nil, disable auto-saving when opening an encrypted file."
49 :type 'boolean
50 :group 'epa-file)
51
52 (defvar epa-file-encrypt-to nil
53 "*Recipient(s) used for encrypting files.
54 May either be a string or a list of strings.")
55
56 (put 'epa-file-encrypt-to 'safe-local-variable
57 (lambda (val)
58 (or (stringp val)
59 (and (listp val)
60 (catch 'safe
61 (mapc (lambda (elt)
62 (unless (stringp elt)
63 (throw 'safe nil)))
64 val)
65 t)))))
66
67 (put 'epa-file-encrypt-to 'permanent-local t)
68
69 (defvar epa-file-handler
70 (cons epa-file-name-regexp 'epa-file-handler))
71
72 (defvar epa-file-auto-mode-alist-entry
73 (list epa-file-name-regexp nil 'epa-file))
74
75 (defun epa-file-name-regexp-update ()
76 (interactive)
77 (unless (equal (car epa-file-handler) epa-file-name-regexp)
78 (setcar epa-file-handler epa-file-name-regexp)))
79
80 (defun epa-file-find-file-hook ()
81 (if (and buffer-file-name
82 (string-match epa-file-name-regexp buffer-file-name)
83 epa-file-inhibit-auto-save)
84 (auto-save-mode 0))
85 (set-buffer-modified-p nil))
86
87 (define-minor-mode auto-encryption-mode
88 "Toggle automatic file encryption and decryption.
89 With prefix argument ARG, turn auto encryption on if positive, else off.
90 Return the new status of auto encryption (non-nil means on)."
91 :global t :init-value t :group 'epa-file :version "23.1"
92 (setq file-name-handler-alist
93 (delq epa-file-handler file-name-handler-alist))
94 (remove-hook 'find-file-hooks 'epa-file-find-file-hook)
95 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry
96 auto-mode-alist))
97 (when auto-encryption-mode
98 (setq file-name-handler-alist
99 (cons epa-file-handler file-name-handler-alist))
100 (add-hook 'find-file-hook 'epa-file-find-file-hook)
101 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry
102 auto-mode-alist))))
103
104 (put 'epa-file-handler 'safe-magic t)
105 (put 'epa-file-handler 'operations '(write-region insert-file-contents))
106
107 (provide 'epa-file-hook)
108
109 ;;; epa-file-hook.el ends here