annotate lisp/epa-file.el @ 103016:15551118906e

epa-file: fix garble with decode-coding-inserted-region * epa-file.el (epa-file-decode-and-insert): Use string-to-multibyte instead of set-buffer-multibyte.
author Daiki Ueno <ueno@unixuser.org>
date Sat, 18 Apr 2009 04:55:50 +0000
parents a9dc0e7c3f2b
children e9b2a988e884
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption
100908
a9dc0e7c3f2b Add 2009 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 100456
diff changeset
2 ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
3
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
5 ;; Keywords: PGP, GnuPG
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
6
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
7 ;; This file is part of GNU Emacs.
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
8
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 94512
diff changeset
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
10 ;; it under the terms of the GNU General Public License as published by
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 94512
diff changeset
11 ;; the Free Software Foundation, either version 3 of the License, or
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 94512
diff changeset
12 ;; (at your option) any later version.
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
13
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
14 ;; GNU Emacs is distributed in the hope that it will be useful,
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
17 ;; GNU General Public License for more details.
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
18
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
19 ;; You should have received a copy of the GNU General Public License
94678
ee5932bf781d Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 94512
diff changeset
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
21
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
22 ;;; Code:
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
23
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
24 (require 'epa)
94755
14eda1195ec3 Provide/require epa-hook, not epa-file-hook.
Eli Zaretskii <eliz@gnu.org>
parents: 94678
diff changeset
25 (require 'epa-hook)
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
26
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
27 (defcustom epa-file-cache-passphrase-for-symmetric-encryption nil
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
28 "If non-nil, cache passphrase for symmetric encryption."
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
29 :type 'boolean
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
30 :group 'epa-file)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
31
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
32 (defcustom epa-file-select-keys nil
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
33 "If non-nil, always asks user to select recipients."
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
34 :type 'boolean
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
35 :group 'epa-file)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
36
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
37 (defvar epa-file-passphrase-alist nil)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
38
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
39 (eval-and-compile
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
40 (if (fboundp 'encode-coding-string)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
41 (defalias 'epa-file--encode-coding-string 'encode-coding-string)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
42 (defalias 'epa-file--encode-coding-string 'identity)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
43
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
44 (eval-and-compile
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
45 (if (fboundp 'decode-coding-string)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
46 (defalias 'epa-file--decode-coding-string 'decode-coding-string)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
47 (defalias 'epa-file--decode-coding-string 'identity)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
48
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
49 (defun epa-file-passphrase-callback-function (context key-id file)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
50 (if (and epa-file-cache-passphrase-for-symmetric-encryption
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
51 (eq key-id 'SYM))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
52 (progn
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
53 (setq file (file-truename file))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
54 (let ((entry (assoc file epa-file-passphrase-alist))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
55 passphrase)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
56 (or (copy-sequence (cdr entry))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
57 (progn
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
58 (unless entry
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
59 (setq entry (list file)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
60 epa-file-passphrase-alist
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
61 (cons entry
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
62 epa-file-passphrase-alist)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
63 (setq passphrase (epa-passphrase-callback-function context
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
64 key-id nil))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
65 (setcdr entry (copy-sequence passphrase))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
66 passphrase))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
67 (epa-passphrase-callback-function context key-id nil)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
68
94512
d10cd2d8d9ae * epa-file-hook.el: New file split from epa-file.el.
Daiki Ueno <ueno@unixuser.org>
parents: 94423
diff changeset
69 ;;;###autoload
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
70 (defun epa-file-handler (operation &rest args)
94423
3898a5665f50 Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents: 94419
diff changeset
71 (save-match-data
3898a5665f50 Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents: 94419
diff changeset
72 (let ((op (get operation 'epa-file)))
3898a5665f50 Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents: 94419
diff changeset
73 (if op
3898a5665f50 Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents: 94419
diff changeset
74 (apply op args)
3898a5665f50 Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents: 94419
diff changeset
75 (epa-file-run-real-handler operation args)))))
3898a5665f50 Daiki Ueno <ueno at unixuser.org>
Glenn Morris <rgm@gnu.org>
parents: 94419
diff changeset
76
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
77 (defun epa-file-run-real-handler (operation args)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
78 (let ((inhibit-file-name-handlers
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
79 (cons 'epa-file-handler
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
80 (and (eq inhibit-file-name-operation operation)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
81 inhibit-file-name-handlers)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
82 (inhibit-file-name-operation operation))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
83 (apply operation args)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
84
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
85 (defun epa-file-decode-and-insert (string file visit beg end replace)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
86 (if (fboundp 'decode-coding-inserted-region)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
87 (save-restriction
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
88 (narrow-to-region (point) (point))
103016
15551118906e epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents: 100908
diff changeset
89 (insert (if enable-multibyte-characters
15551118906e epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents: 100908
diff changeset
90 (string-to-multibyte string)
15551118906e epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents: 100908
diff changeset
91 string))
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
92 (decode-coding-inserted-region
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
93 (point-min) (point-max)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
94 (substring file 0 (string-match epa-file-name-regexp file))
103016
15551118906e epa-file: fix garble with decode-coding-inserted-region
Daiki Ueno <ueno@unixuser.org>
parents: 100908
diff changeset
95 visit beg end replace))
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
96 (insert (epa-file--decode-coding-string string (or coding-system-for-read
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
97 'undecided)))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
98
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
99 (defvar last-coding-system-used)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
100 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
101 (barf-if-buffer-read-only)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
102 (if (and visit (or beg end))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
103 (error "Attempt to visit less than an entire file"))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
104 (setq file (expand-file-name file))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
105 (let* ((local-copy
98409
cb5ef7767703 (epa-file-insert-file-contents): Fix typo.
Daiki Ueno <ueno@unixuser.org>
parents: 95478
diff changeset
106 (condition-case nil
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
107 (epa-file-run-real-handler #'file-local-copy (list file))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
108 (error)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
109 (local-file (or local-copy file))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
110 (context (epg-make-context))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
111 string length entry)
100456
e4798aef9a53 * epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents: 100304
diff changeset
112 (if visit
e4798aef9a53 * epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents: 100304
diff changeset
113 (setq buffer-file-name file))
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
114 (epg-context-set-passphrase-callback
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
115 context
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
116 (cons #'epa-file-passphrase-callback-function
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
117 local-file))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
118 (epg-context-set-progress-callback context
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
119 #'epa-progress-callback-function)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
120 (unwind-protect
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
121 (progn
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
122 (if replace
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
123 (goto-char (point-min)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
124 (condition-case error
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
125 (setq string (epg-decrypt-file context local-file nil))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
126 (error
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
127 (if (setq entry (assoc file epa-file-passphrase-alist))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
128 (setcdr entry nil))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
129 (signal 'file-error
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
130 (cons "Opening input file" (cdr error)))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
131 (make-local-variable 'epa-file-encrypt-to)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
132 (setq epa-file-encrypt-to
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
133 (mapcar #'car (epg-context-result-for context 'encrypted-to)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
134 (if (or beg end)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
135 (setq string (substring string (or beg 0) end)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
136 (save-excursion
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
137 (save-restriction
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
138 (narrow-to-region (point) (point))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
139 (epa-file-decode-and-insert string file visit beg end replace)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
140 (setq length (- (point-max) (point-min))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
141 (if replace
100304
4dfef179eed3 * epa-hook.el (epa-file-find-file-hook): Don't mark the current
Daiki Ueno <ueno@unixuser.org>
parents: 98409
diff changeset
142 (delete-region (point) (point-max)))
100456
e4798aef9a53 * epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents: 100304
diff changeset
143 (if visit
e4798aef9a53 * epa-file.el (epa-file-insert-file-contents): Set
Daiki Ueno <ueno@unixuser.org>
parents: 100304
diff changeset
144 (set-visited-file-modtime))))
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
145 (if (and local-copy
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
146 (file-exists-p local-copy))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
147 (delete-file local-copy)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
148 (list file length)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
149 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
150
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
151 (defun epa-file-write-region (start end file &optional append visit lockname
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
152 mustbenew)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
153 (if append
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
154 (error "Can't append to the file."))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
155 (setq file (expand-file-name file))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
156 (let* ((coding-system (or coding-system-for-write
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
157 (if (fboundp 'select-safe-coding-system)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
158 ;; This is needed since Emacs 22 has
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
159 ;; no-conversion setting for *.gpg in
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
160 ;; `auto-coding-alist'.
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
161 (let ((buffer-file-name
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
162 (file-name-sans-extension file)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
163 (select-safe-coding-system
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
164 (point-min) (point-max)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
165 buffer-file-coding-system)))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
166 (context (epg-make-context))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
167 (coding-system-for-write 'binary)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
168 string entry
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
169 (recipients
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
170 (cond
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
171 ((listp epa-file-encrypt-to) epa-file-encrypt-to)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
172 ((stringp epa-file-encrypt-to) (list epa-file-encrypt-to)))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
173 (epg-context-set-passphrase-callback
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
174 context
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
175 (cons #'epa-file-passphrase-callback-function
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
176 file))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
177 (epg-context-set-progress-callback context
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
178 #'epa-progress-callback-function)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
179 (epg-context-set-armor context epa-armor)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
180 (condition-case error
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
181 (setq string
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
182 (epg-encrypt-string
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
183 context
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
184 (if (stringp start)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
185 (epa-file--encode-coding-string start coding-system)
95477
57f3dde2b9be (epa-file-write-region): Write the entire buffer
Daiki Ueno <ueno@unixuser.org>
parents: 94755
diff changeset
186 (unless start
95478
e21a3997eb7c Simplify the last change.
Daiki Ueno <ueno@unixuser.org>
parents: 95477
diff changeset
187 (setq start (point-min)
e21a3997eb7c Simplify the last change.
Daiki Ueno <ueno@unixuser.org>
parents: 95477
diff changeset
188 end (point-max)))
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
189 (epa-file--encode-coding-string (buffer-substring start end)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
190 coding-system))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
191 (if (or epa-file-select-keys
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
192 (not (local-variable-p 'epa-file-encrypt-to
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
193 (current-buffer))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
194 (epa-select-keys
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
195 context
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
196 "Select recipents for encryption.
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
197 If no one is selected, symmetric encryption will be performed. "
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
198 recipients)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
199 (if epa-file-encrypt-to
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
200 (epg-list-keys context recipients)))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
201 (error
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
202 (if (setq entry (assoc file epa-file-passphrase-alist))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
203 (setcdr entry nil))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
204 (signal 'file-error (cons "Opening output file" (cdr error)))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
205 (epa-file-run-real-handler
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
206 #'write-region
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
207 (list string nil file append visit lockname mustbenew))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
208 (if (boundp 'last-coding-system-used)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
209 (setq last-coding-system-used coding-system))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
210 (if (eq visit t)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
211 (progn
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
212 (setq buffer-file-name file)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
213 (set-visited-file-modtime))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
214 (if (stringp visit)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
215 (progn
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
216 (set-visited-file-modtime)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
217 (setq buffer-file-name visit))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
218 (if (or (eq visit t)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
219 (eq visit nil)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
220 (stringp visit))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
221 (message "Wrote %s" buffer-file-name))))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
222 (put 'write-region 'epa-file 'epa-file-write-region)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
223
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
224 (defun epa-file-select-keys ()
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
225 "Select recipients for encryption."
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
226 (interactive)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
227 (make-local-variable 'epa-file-encrypt-to)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
228 (setq epa-file-encrypt-to
93002
d876f5372472 EasyPG: Fix bug with C-x C-s after M-x epa-file-select-keys.
Michael Olson <mwolson@gnu.org>
parents: 91731
diff changeset
229 (mapcar
d876f5372472 EasyPG: Fix bug with C-x C-s after M-x epa-file-select-keys.
Michael Olson <mwolson@gnu.org>
parents: 91731
diff changeset
230 (lambda (key)
d876f5372472 EasyPG: Fix bug with C-x C-s after M-x epa-file-select-keys.
Michael Olson <mwolson@gnu.org>
parents: 91731
diff changeset
231 (epg-sub-key-id (car (epg-key-sub-key-list key))))
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
232 (epa-select-keys
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
233 (epg-make-context)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
234 "Select recipents for encryption.
93002
d876f5372472 EasyPG: Fix bug with C-x C-s after M-x epa-file-select-keys.
Michael Olson <mwolson@gnu.org>
parents: 91731
diff changeset
235 If no one is selected, symmetric encryption will be performed. "))))
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
236
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
237 ;;;###autoload
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
238 (defun epa-file-enable ()
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
239 (interactive)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
240 (if (memq epa-file-handler file-name-handler-alist)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
241 (message "`epa-file' already enabled")
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
242 (setq file-name-handler-alist
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
243 (cons epa-file-handler file-name-handler-alist))
94417
1a8916b995cf * epa-file.el (auto-encryption-mode): Rename from epa-file-mode.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 93002
diff changeset
244 (add-hook 'find-file-hook 'epa-file-find-file-hook)
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
245 (setq auto-mode-alist (cons epa-file-auto-mode-alist-entry auto-mode-alist))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
246 (message "`epa-file' enabled")))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
247
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
248 ;;;###autoload
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
249 (defun epa-file-disable ()
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
250 (interactive)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
251 (if (memq epa-file-handler file-name-handler-alist)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
252 (progn
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
253 (setq file-name-handler-alist
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
254 (delq epa-file-handler file-name-handler-alist))
94417
1a8916b995cf * epa-file.el (auto-encryption-mode): Rename from epa-file-mode.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 93002
diff changeset
255 (remove-hook 'find-file-hook 'epa-file-find-file-hook)
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
256 (setq auto-mode-alist (delq epa-file-auto-mode-alist-entry
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
257 auto-mode-alist))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
258 (message "`epa-file' disabled"))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
259 (message "`epa-file' already disabled")))
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
260
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
261 (provide 'epa-file)
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
262
91687
d125b90283d2 Add arch tagline
Miles Bader <miles@gnu.org>
parents: 91647
diff changeset
263 ;; arch-tag: 5715152f-0eb1-4dbc-9008-07098775314d
91647
f9692dfe86e7 EasyPG: Initial check-in.
Michael Olson <mwolson@gnu.org>
parents:
diff changeset
264 ;;; epa-file.el ends here