Don't call set-buffer-multibyte if it is not fbound.
[elisp/epg.git] / epg-file.el
1 (require 'epg)
2
3 (defcustom epg-file-name-regexp "\\.gpg\\'"
4   "Regexp that matches filenames that are assumed to be encrypted
5 with GnuPG."
6   :type 'regexp
7   :group 'epg-file)
8
9 (defun epg-file-handler (operation &rest args)
10   (let ((epg-file-operation (get operation 'epg-file)))
11     (if epg-file-operation
12         (apply epg-file-operation args)
13       (epg-file-run-real-handler operation args))))
14
15 (defun epg-file-run-real-handler (operation args)
16   (let ((inhibit-file-name-handlers
17          (cons 'epg-file-handler
18                (and (eq inhibit-file-name-operation operation)
19                     inhibit-file-name-handlers)))
20         (inhibit-file-name-operation operation))
21     (apply operation args)))
22
23 (defvar buffer-file-type)
24 (defvar last-coding-system-used)
25 (defun epg-file-write-region (start end filename &optional append visit
26                                     lockname mustbenew)
27   (let* ((visit-file (if (stringp visit)
28                          (expand-file-name visit)
29                        (expand-file-name filename)))
30          ;; XXX: Obtain the value returned by choose_write_coding_system
31          (coding-system (condition-case nil
32                             (epg-file-run-real-handler
33                              'write-region (list start end "/"))
34                           (file-error (if (boundp 'last-coding-system-used)
35                                           last-coding-system-used))))
36          ;; start and end are normally buffer positions
37          ;; specifying the part of the buffer to write.
38          ;; If start is nil, that means to use the entire buffer contents.
39          ;; If start is a string, then output that string to the file
40          ;; instead of any buffer contents; end is ignored.
41          (string (encode-coding-string (cond
42                                         ((stringp start)
43                                          start)
44                                         ((null start)
45                                          (buffer-string))
46                                         (t
47                                          (buffer-substring start end)))
48                                        coding-system)))
49     (with-temp-buffer
50       (if (fboundp 'set-buffer-multibyte)
51           (set-buffer-multibyte nil))
52       ;; Optional fourth argument append if non-nil means
53       ;;   append to existing file contents (if any).  If it is an integer,
54       ;;   seek to that offset in the file before writing.
55       (if (and append (file-exists-p filename))
56           ;; Enable passphrase cache on this temp buffer
57           (let ((coding-system-for-read 'binary))
58             ;; set visit to t so that passphrase is cached
59             (insert-file-contents filename t)
60             (setq buffer-file-name nil)))
61       ;; Insert data to encrypt
62       (goto-char (if (integerp append) (1+ append) (point-max)))
63       (delete-region (point) (min (+ (point) (length string)) (point-max)))
64       (insert string)
65
66       (let ((coding-system-for-write 'binary)
67             (coding-system-for-read 'binary)
68             (context (epg-make-context))
69             cipher)
70         (when (setq cipher (epg-encrypt-string context (buffer-string) nil))
71           (if (and (memq system-type '(ms-dos windows-nt))
72                    (boundp 'buffer-file-type))
73               (setq buffer-file-type t))
74           (epg-file-run-real-handler
75            'write-region
76            (list cipher nil filename nil 'not-visit lockname mustbenew)))))
77     ;; Optional fifth argument visit, if t or a string, means
78     ;;   set the last-save-file-modtime of buffer to this file's modtime
79     ;;   and mark buffer not modified.
80     ;; If visit is a string, it is a second file name;
81     ;;   the output goes to filename, but the buffer is marked as visiting visit.
82     ;;   visit is also the file name to lock and unlock for clash detection.
83     ;; If visit is neither t nor nil nor a string,
84     ;;   that means do not display the "Wrote file" message.
85     (when (or (eq visit t) (stringp visit))
86       (setq buffer-file-name filename)
87       (set-visited-file-modtime))
88     (when (stringp visit)
89       (setq buffer-file-name visit))
90     (when (or (eq visit t) (eq visit nil) (stringp visit))
91       (message "Wrote %s" visit-file))
92     (if (boundp 'last-coding-system-used)
93         (setq last-coding-system-used coding-system))
94     nil))
95
96 (defun epg-file-insert-file-contents (filename &optional visit beg end replace)
97   (barf-if-buffer-read-only)
98   (setq filename (expand-file-name filename))
99   (let ((filename (expand-file-name filename))
100         (length 0))
101     (if (file-exists-p filename)
102         (let ((local-file
103                (let ((inhibit-file-name-operation
104                       (when (eq inhibit-file-name-operation
105                                 'insert-file-contents)
106                         'file-local-copy)))
107                  (file-local-copy filename)))
108               (coding-system-for-read 'binary)
109               (context (epg-make-context))
110               string)
111           (unwind-protect
112               (progn
113                 (setq string (epg-decrypt-file context (or local-file
114                                                            filename))
115                       length (length string))
116                 (if replace
117                     (goto-char (point-min)))
118                 (save-excursion
119                   (let ((buffer-file-name (if visit nil buffer-file-name)))
120                     (save-restriction
121                       (narrow-to-region (point) (point))
122                       (insert (decode-coding-string string 'undecided)))
123                     (if replace
124                         (delete-region (point) (point-max))))))
125             (when (and local-file (file-exists-p local-file))
126               (delete-file local-file)))))
127     ;; If second argument visit is non-nil, the buffer's visited filename
128     ;; and last save file modtime are set, and it is marked unmodified.
129     (when visit
130       (unlock-buffer)
131       (setq buffer-file-name filename)
132       (set-visited-file-modtime))
133
134     ;; If visiting and the file does not exist, visiting is completed
135     ;; before the error is signaled.
136     (if (and visit (not (file-exists-p filename)))
137         (signal 'file-error (list "Opening input file" filename)))
138
139     ;; Returns list of absolute file name and number of characters inserted.
140     (list filename length)))
141
142 (put 'write-region 'epg-file 'epg-file-write-region)
143 (put 'insert-file-contents 'epg-file 'epg-file-insert-file-contents)
144
145 (unless (assoc epg-file-name-regexp file-name-handler-alist)
146   (setq file-name-handler-alist
147         (cons (cons epg-file-name-regexp 'epg-file-handler)
148               file-name-handler-alist)))
149
150 (unless (assoc epg-file-name-regexp auto-mode-alist)
151   (setq auto-mode-alist
152         (cons (list epg-file-name-regexp nil 'strip-suffix)
153               auto-mode-alist)))