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