-
Notifications
You must be signed in to change notification settings - Fork 52
/
enchive-mode.el
62 lines (50 loc) · 2.33 KB
/
enchive-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
;;; enchive-mode.el --- automatic encrypt/decrypt -*- lexical-binding: t; -*-
;; This is free and unencumbered software released into the public domain.
;;; Commentary:
;; Load this file, then M-x `enchive-mode' (global minor mode) to
;; enable automatic encryption and decryption of Enchive files.
;;; Code:
(defgroup enchive ()
"Interface to Enchive subprocess."
:group 'data)
(defcustom enchive-program-name "enchive"
"Path to the locally installed enchive binary.")
(defvar enchive-handler-entry (cons "\\.enchive\\'" #'enchive-file-handler)
"Entry for `enchive-mode' in `file-name-handler-alist'.")
(defvar enchive-auto-mode-entry (list "\\.enchive\\'" nil 'enchive)
"Entry for `enchive-mode' in `auto-mode-alist'.")
(defun enchive-file-handler (operation &rest args)
"Handler for `file-name-handler-alist' for automatic encrypt/decrypt."
(cond ((eq operation 'insert-file-contents)
(let ((file (car args)))
(unless (= 0 (call-process enchive-program-name file '(t nil) nil
"--pinentry" "--agent" "extract"))
(error "Enchive subprocess failed"))
(setf buffer-file-name file)
(list file (buffer-size))))
((eq operation 'write-region)
(call-process-region (nth 0 args) (nth 1 args)
enchive-program-name nil nil nil
"archive" "/dev/stdin" (nth 2 args)))
;; Handle any operation we don’t know about.
(t (let ((inhibit-file-name-handlers
(cons 'enchive-file-handler
(and (eq inhibit-file-name-operation operation)
inhibit-file-name-handlers)))
(inhibit-file-name-operation operation))
(apply operation args)))))
;;;###autoload
(define-minor-mode enchive-mode
"Global minor mode to automatically encrypt/decrypt enchive files."
:global t
(setf file-name-handler-alist
(delq enchive-handler-entry file-name-handler-alist))
(setf auto-mode-alist
(delq enchive-auto-mode-entry auto-mode-alist))
(when enchive-mode
(setf file-name-handler-alist
(cons enchive-handler-entry file-name-handler-alist))
(setf auto-mode-alist
(cons enchive-auto-mode-entry auto-mode-alist))))
(provide 'enchive-mode)
;;; enchive-mode.el ends here