emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Wash output of org-encrypt-entry
@ 2011-03-18 17:05 Óscar Fuentes
  2011-03-18 17:22 ` Óscar Fuentes
  0 siblings, 1 reply; 2+ messages in thread
From: Óscar Fuentes @ 2011-03-18 17:05 UTC (permalink / raw)
  To: emacs-orgmode

With a gpg executable with default settings, org-encrypt-entry produces
output like this:

-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.10 (GNU/Linux)

jA0EAwMCBWZVym6QMPVgyTxreTb1AEL3uTO+qCh2lR9/Qxk4nEMpPr9/RwNk95Gb
slUra9X+N+qSWghEHvvxY0Ol8Yw9Ko4n7JVhHFs=
=E4vw
-----END PGP MESSAGE-----

The first line (Version:...) can change from machine to machine and over
time (as gpg is updated with a new version.) This is problematic when
the file is stored under version control, because as you decrypt and
encrypt an entry that line will change and create differences among the
file on the workspace and the file stored on VC.

Second, the empty line just wastes space and it is plain ugly once we
remove the first one with the Version text.

Finally, on some systems (mostly Windows) depending on how your Emacs
and gpg are configured, ^M characters may appear at the end of every
line of gpg output once it is inserted on the Emacs buffer. This happens
when the buffer uses Unix line-endings but gpg uses DOS line-endings.

The patch removes all that junk from the encrypted text just before it
is inserted on the buffer.

I'm assuming that the transformations made by this patch are
uncontroversial and desirable. If anyone actually prefers to keep that
noise on his encrypted org entries, an alternative implementation that
uses a configurable list of regexps is trivial to implement, but then
every user would have to do some job for achieving the same result.

diff --git a/lisp/org-crypt.el b/lisp/org-crypt.el
index b649e39..8ba382e 100644
--- a/lisp/org-crypt.el
+++ b/lisp/org-crypt.el
@@ -112,6 +112,17 @@ This setting can also be overridden in the CRYPTKEY property."
     (let ((epg-context (epg-make-context nil t t)))
       (epg-encrypt-string epg-context str (epg-list-keys epg-context crypt-key)))))
 
+(defun org-crypt-wash-encrypted-string (str)
+  "Remove superfluos and annoying text from the encrypted string."
+  ;; Remove gpg version information:
+  (setq str (replace-regexp-in-string "^Version:.*$" "" str))
+  ;; Remove ^M characters created by end-of-line type mismatch
+  ;; (Windows suffer from this):
+  (setq str (replace-regexp-in-string "\^M" "" str))
+  ;; Remove empty lines:
+  (setq str (replace-regexp-in-string "^\n" "" str))
+  str)
+
 (defun org-encrypt-entry ()
   "Encrypt the content of the current headline."
   (interactive)
@@ -133,6 +144,7 @@ This setting can also be overridden in the CRYPTKEY property."
                 encrypted-text
 		(org-encrypt-string (buffer-substring beg end) crypt-key))
           (delete-region beg end)
+	  (setq encrypted-text (org-crypt-wash-encrypted-string encrypted-text))
           (insert encrypted-text)
           (when folded
             (goto-char start-heading)

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Wash output of org-encrypt-entry
  2011-03-18 17:05 [PATCH] Wash output of org-encrypt-entry Óscar Fuentes
@ 2011-03-18 17:22 ` Óscar Fuentes
  0 siblings, 0 replies; 2+ messages in thread
From: Óscar Fuentes @ 2011-03-18 17:22 UTC (permalink / raw)
  To: emacs-orgmode

Óscar Fuentes <ofv@wanadoo.es> writes:

[snip]

> +  (setq str (replace-regexp-in-string "^Version:.*$" "" str))

Ugh... replace-regexp-in-string does not exist on XEmacs. Time for a
rewrite. Still, it would good to know if the goal is acceptable.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-03-18 17:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-18 17:05 [PATCH] Wash output of org-encrypt-entry Óscar Fuentes
2011-03-18 17:22 ` Óscar Fuentes

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).