Avoid altering window configuration when saving edit buffer.
[rgr-org-mode.git] / lisp / org-crypt.el
blob53bc042dd450419c73a1cfdd4bd28792a9f44ed6
1 ;;; org-crypt.el --- Public key encryption for org-mode entries
3 ;; Copyright (C) 2007, 2009 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-crypt.el
7 ;; Version: 6.32trans
8 ;; Keywords: org-mode
9 ;; Author: John Wiegley <johnw@gnu.org>
10 ;; Maintainer: Peter Jones <pjones@pmade.com>
11 ;; Description: Adds public key encryption to org-mode buffers
12 ;; URL: http://www.newartisans.com/software/emacs.html
13 ;; Compatibility: Emacs22
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;;; Commentary:
32 ;; Right now this is just a set of functions to play with. It depends
33 ;; on the epg library. Here's how you would use it:
35 ;; 1. To mark an entry for encryption, tag the heading with "crypt".
36 ;; You can change the tag to any complex tag matching string by
37 ;; setting the `org-crypt-tag-matcher' variable.
39 ;; 2. Set the encryption key to use in the `org-crypt-key' variable,
40 ;; or use `M-x org-set-property' to set the property CRYPTKEY to
41 ;; any address in your public keyring. The text of the entry (but
42 ;; not its properties or headline) will be encrypted for this user.
43 ;; For them to read it, the corresponding secret key must be
44 ;; located in the secret key ring of the account where you try to
45 ;; decrypt it. This makes it possible to leave secure notes that
46 ;; only the intended recipient can read in a shared-org-mode-files
47 ;; scenario.
49 ;; 3. To later decrypt an entry, use `org-decrypt-entries' or
50 ;; `org-decrypt-entry'. It might be useful to bind this to a key,
51 ;; like C-c C-/. I hope that in the future, C-c C-r can be might
52 ;; overloaded to also decrypt an entry if it's encrypted, since
53 ;; that fits nicely with the meaning of "reveal".
55 ;; 4. To automatically encrypt all necessary entries when saving a
56 ;; file, call `org-crypt-use-before-save-magic' after loading
57 ;; org-crypt.el.
59 ;; TODO:
60 ;; - Allow symmetric encryption as well
62 ;;; Thanks:
64 ;; - Carsten Dominik
65 ;; - Vitaly Ostanin
67 (require 'org)
69 (declare-function epg-decrypt-string "epg" (context cipher))
70 (declare-function epg-list-keys "epg" (context &optional name mode))
71 (declare-function epg-make-context "epg"
72 (&optional protocol armor textmode include-certs
73 cipher-algorithm digest-algorithm
74 compress-algorithm))
75 (declare-function epg-encrypt-string "epg"
76 (context plain recipients &optional sign always-trust))
78 (defgroup org-crypt nil
79 "Org Crypt"
80 :tag "Org Crypt" :group 'org)
82 (defcustom org-crypt-tag-matcher "crypt"
83 "The tag matcher used to find headings whose contents should be
84 encrypted. See the \"Match syntax\" section of the org manual
85 for more details."
86 :type 'string :group 'org-crypt)
88 (defcustom org-crypt-key nil
89 "The default key to use when encrypting the contents of a
90 heading. This can also be overridden in the CRYPTKEY property."
91 :type 'string :group 'org-crypt)
93 (defun org-crypt-key-for-heading ()
94 "Returns the encryption key for the current heading."
95 (save-excursion
96 (org-back-to-heading t)
97 (or (org-entry-get nil "CRYPTKEY" 'selective)
98 org-crypt-key
99 (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to)
100 (error "No crypt key set"))))
102 (defun org-encrypt-entry ()
103 "Encrypt the content of the current headline."
104 (interactive)
105 (require 'epg)
106 (save-excursion
107 (org-back-to-heading t)
108 (forward-line)
109 (when (not (looking-at "-----BEGIN PGP MESSAGE-----"))
110 (let ((folded (org-invisible-p))
111 (epg-context (epg-make-context nil t t))
112 (crypt-key (org-crypt-key-for-heading))
113 (beg (point))
114 end encrypted-text)
115 (org-end-of-subtree t t)
116 (org-back-over-empty-lines)
117 (setq end (point)
118 encrypted-text
119 (epg-encrypt-string
120 epg-context
121 (buffer-substring-no-properties beg end)
122 (epg-list-keys epg-context crypt-key)))
123 (delete-region beg end)
124 (insert encrypted-text)
125 (when folded
126 (save-excursion
127 (org-back-to-heading t)
128 (hide-subtree)))
129 nil))))
131 (defun org-decrypt-entry ()
132 (interactive)
133 (require 'epg)
134 (save-excursion
135 (org-back-to-heading t)
136 (forward-line)
137 (when (looking-at "-----BEGIN PGP MESSAGE-----")
138 (let* ((beg (point))
139 (end (save-excursion
140 (search-forward "-----END PGP MESSAGE-----")
141 (forward-line)
142 (point)))
143 (epg-context (epg-make-context nil t t))
144 (decrypted-text
145 (decode-coding-string
146 (epg-decrypt-string
147 epg-context
148 (buffer-substring-no-properties beg end))
149 'utf-8)))
150 (delete-region beg end)
151 (insert decrypted-text)
152 nil))))
154 (defun org-encrypt-entries ()
155 (interactive)
156 (org-scan-tags
157 'org-encrypt-entry
158 (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
160 (defun org-decrypt-entries ()
161 (interactive)
162 (org-scan-tags
163 'org-decrypt-entry
164 (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
166 (defun org-crypt-use-before-save-magic ()
167 "Adds a hook that will automatically encrypt entries before a
168 file is saved to disk."
169 (add-hook
170 'org-mode-hook
171 (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
173 (provide 'org-crypt)
175 ;; arch-tag: 8202ed2c-221e-4001-9e4b-54674a7e846e
177 ;;; org-crypt.el ends here