Commit | Line | Data |
---|---|---|
74f50695 | 1 | ;;; epa-hook.el --- preloaded code to enable epa-file.el -*- lexical-binding: t -*- |
ba318903 | 2 | ;; Copyright (C) 2006-2014 Free Software Foundation, Inc. |
6f8a4190 DU |
3 | |
4 | ;; Author: Daiki Ueno <ueno@unixuser.org> | |
5 | ;; Keywords: PGP, GnuPG | |
bd78fa1d | 6 | ;; Package: emacs |
6f8a4190 DU |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
6f8a4190 | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
6f8a4190 DU |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
6f8a4190 DU |
22 | |
23 | ;;; Code: | |
24 | ||
25 | (defgroup epa-file nil | |
26 | "The EasyPG Assistant hooks for transparent file encryption" | |
27 | :version "23.1" | |
28 | :group 'epa) | |
29 | ||
30 | (defun epa-file--file-name-regexp-set (variable value) | |
31 | (set-default variable value) | |
32 | (if (fboundp 'epa-file-name-regexp-update) | |
33 | (epa-file-name-regexp-update))) | |
34 | ||
1e8780b1 | 35 | (defcustom epa-file-name-regexp (purecopy "\\.gpg\\(~\\|\\.~[0-9]+~\\)?\\'") |
6f8a4190 DU |
36 | "Regexp which matches filenames to be encrypted with GnuPG. |
37 | ||
38 | If you set this outside Custom while epa-file is already enabled, you | |
39 | have to call `epa-file-name-regexp-update' after setting it to | |
40 | properly update file-name-handler-alist. Setting this through Custom | |
41 | does that automatically." | |
42 | :type 'regexp | |
43 | :group 'epa-file | |
44 | :set 'epa-file--file-name-regexp-set) | |
45 | ||
46 | (defcustom epa-file-inhibit-auto-save t | |
47 | "If non-nil, disable auto-saving when opening an encrypted file." | |
48 | :type 'boolean | |
49 | :group 'epa-file) | |
50 | ||
51 | (defvar epa-file-encrypt-to nil | |
06e21633 | 52 | "Recipient(s) used for encrypting files. |
6f8a4190 DU |
53 | May either be a string or a list of strings.") |
54 | ||
55 | (put 'epa-file-encrypt-to 'safe-local-variable | |
edca97cd TH |
56 | #'(lambda (val) |
57 | (or (stringp val) | |
58 | (and (listp val) | |
59 | (catch 'safe | |
60 | (mapc (lambda (elt) | |
61 | (unless (stringp elt) | |
62 | (throw 'safe nil))) | |
63 | val) | |
64 | t))))) | |
6f8a4190 DU |
65 | |
66 | (put 'epa-file-encrypt-to 'permanent-local t) | |
67 | ||
68 | (defvar epa-file-handler | |
69 | (cons epa-file-name-regexp 'epa-file-handler)) | |
70 | ||
71 | (defvar epa-file-auto-mode-alist-entry | |
72 | (list epa-file-name-regexp nil 'epa-file)) | |
73 | ||
74 | (defun epa-file-name-regexp-update () | |
75 | (interactive) | |
76 | (unless (equal (car epa-file-handler) epa-file-name-regexp) | |
77 | (setcar epa-file-handler epa-file-name-regexp))) | |
78 | ||
79 | (defun epa-file-find-file-hook () | |
80 | (if (and buffer-file-name | |
81 | (string-match epa-file-name-regexp buffer-file-name) | |
82 | epa-file-inhibit-auto-save) | |
8e3efc87 | 83 | (auto-save-mode 0))) |
6f8a4190 DU |
84 | |
85 | (define-minor-mode auto-encryption-mode | |
06e21633 CY |
86 | "Toggle automatic file encryption/decryption (Auto Encryption mode). |
87 | With a prefix argument ARG, enable Auto Encryption mode if ARG is | |
88 | positive, and disable it otherwise. If called from Lisp, enable | |
89 | the mode if ARG is omitted or nil." | |
6f8a4190 | 90 | :global t :init-value t :group 'epa-file :version "23.1" |
adba8116 SM |
91 | ;; We'd like to use custom-initialize-set here so the setup is done |
92 | ;; before dumping, but at the point where the defcustom is evaluated, | |
93 | ;; the corresponding function isn't defined yet, so | |
94 | ;; custom-initialize-set signals an error. | |
95 | :initialize 'custom-initialize-delay | |
6f8a4190 DU |
96 | (setq file-name-handler-alist |
97 | (delq epa-file-handler file-name-handler-alist)) | |
98 | (remove-hook 'find-file-hooks 'epa-file-find-file-hook) | |
99 | (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry | |
100 | auto-mode-alist)) | |
101 | (when auto-encryption-mode | |
102 | (setq file-name-handler-alist | |
103 | (cons epa-file-handler file-name-handler-alist)) | |
104 | (add-hook 'find-file-hook 'epa-file-find-file-hook) | |
105 | (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry | |
106 | auto-mode-alist)))) | |
107 | ||
108 | (put 'epa-file-handler 'safe-magic t) | |
109 | (put 'epa-file-handler 'operations '(write-region insert-file-contents)) | |
110 | ||
3fe68728 | 111 | (provide 'epa-hook) |
6f8a4190 | 112 | |
8f018f60 | 113 | ;;; epa-hook.el ends here |