diff --git a/gists.org b/gists.org index 3723c59..073c8fe 100644 --- a/gists.org +++ b/gists.org @@ -496,6 +496,37 @@ It turns out it didn't work as well as I hoped, paredit steals the RET binding s (advice-add #'tempel-insert :around #'my/store-region) #+end_src +* dwim-shell-command to encrypt files with age :emacs:age: + +#+begin_src elisp :tangle gists/dwim-shell-command-encrypt-with-age.el + (defconst my/cygwin-p (string-equal system-type "cygwin")) + (defconst my/age-pubkey "ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") + + (defun my/dwim-shell-command/convert-path-cygwin (template unix-path) + "Convert a Unix path to a Windows path. + + This can be used as a :post-process-template of a + dwim-shell-command-on-marked-files function, where <> + templates expand to Unix-like paths. However, a non-Cygwin binary + does not understand Unix paths so use cygpath to convert it." + (if my/cygwin-p + (let* ((command (format "cygpath -w %s" unix-path)) + (cygwin-path (string-trim (shell-command-to-string command))) + (escaped-cygwin-path (string-replace "\\" "\\\\" cygwin-path))) + (string-replace unix-path escaped-cygwin-path template)) + + ;; just return the template as-is + template)) + + (defun my/dwim-shell-command/age-encrypt () + "Encrypt marked files with the age encryption tool." + (interactive) + (dwim-shell-command-on-marked-files "Age Encypt" + (format "age -e -r %s -o <>.age <>" my/age-pubkey) + :utils "age" + :post-process-template #'my/dwim-shell-command/convert-path-cygwin)) +#+end_src + * Meta ** License diff --git a/gists/dwim-shell-command-encrypt-with-age.el b/gists/dwim-shell-command-encrypt-with-age.el new file mode 100644 index 0000000..b15cb8b --- /dev/null +++ b/gists/dwim-shell-command-encrypt-with-age.el @@ -0,0 +1,26 @@ +(defconst my/cygwin-p (string-equal system-type "cygwin")) +(defconst my/age-pubkey "ageXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") + +(defun my/dwim-shell-command/convert-path-cygwin (template unix-path) + "Convert a Unix path to a Windows path. + +This can be used as a :post-process-template of a +dwim-shell-command-on-marked-files function, where <> +templates expand to Unix-like paths. However, a non-Cygwin binary +does not understand Unix paths so use cygpath to convert it." + (if my/cygwin-p + (let* ((command (format "cygpath -w %s" unix-path)) + (cygwin-path (string-trim (shell-command-to-string command))) + (escaped-cygwin-path (string-replace "\\" "\\\\" cygwin-path))) + (string-replace unix-path escaped-cygwin-path template)) + + ;; just return the template as-is + template)) + +(defun my/dwim-shell-command/age-encrypt () + "Encrypt marked files with the age encryption tool." + (interactive) + (dwim-shell-command-on-marked-files "Age Encypt" + (format "age -e -r %s -o <>.age <>" my/age-pubkey) + :utils "age" + :post-process-template #'my/dwim-shell-command/convert-path-cygwin))