epa-hook.el 3.9 KB

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