From 5985548736b3907a9d2d6fdd70bd9a7c2607ef4b Mon Sep 17 00:00:00 2001
From: Eric Danan <eric.danan@u-cergy.fr>
Date: Thu, 21 Jun 2018 15:45:32 +0200
Subject: [PATCH] org-attach: Allow attaching file from visited buffer

* lisp/org-attach.el (org-attach-attach): Add a new method `'buf' to
write attachment file from a buffer.
(org-attach-buffer): New function calling `org-attach-attach' to
create attachment from buffer.
(org-attach): Allow attaching from buffer by selecting `b' from the
dispatcher (in passing, add missing `y', `u', and `n' in the
dispatcher message).

When the file to attach is already open in emacs, it is generally
faster to select it from the buffer list than navigating to it through
the file system.  Also, this allows attaching buffers that are not
visiting a file.
---
 lisp/org-attach.el | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index 192815f4..0e52526f 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -177,6 +177,7 @@ (defun org-attach ()
 a       Select a file and attach it to the task, using `org-attach-method'.
 c/m/l/y Attach a file using copy/move/link/symbolic-link method.
 u       Attach a file from URL (downloading it).
+b       Attach a file fron a currently visited buffer.
 n       Create a new attachment, as an Emacs buffer.
 z       Synchronize the current task with its attachment
         directory, in case you added attachments yourself.
@@ -193,7 +194,7 @@ (defun org-attach ()
 s       Set a specific attachment directory for this entry or reset to default.
 i       Make children of the current entry inherit its attachment directory.")))
 	  (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
-	  (message "Select command: [acmlzoOfFdD]")
+	  (message "Select command: [acmlyubnzoOfFdD]")
 	  (setq c (read-char-exclusive))
 	  (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
       (cond
@@ -208,6 +209,8 @@ (defun org-attach ()
 	(let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
        ((memq c '(?u ?\C-u))
         (let ((org-attach-method 'url)) (call-interactively 'org-attach-url)))
+       ((memq c '(?b ?\C-b))
+        (let ((org-attach-method 'buf)) (call-interactively 'org-attach-buffer)))
        ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
        ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
        ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
@@ -390,6 +393,10 @@ (defun org-attach-url (url)
   (interactive "MURL of the file to attach: \n")
   (org-attach-attach url))
 
+(defun org-attach-buffer (buffer)
+  (interactive "bBuffer to keep as an attachment:")
+  (org-attach-attach buffer))
+  
 (defun org-attach-attach (file &optional visit-dir method)
   "Move/copy/link FILE into the attachment directory of the current task.
 If VISIT-DIR is non-nil, visit the directory with dired.
@@ -416,7 +423,9 @@ (defun org-attach-attach (file &optional visit-dir method)
        ((eq method 'cp) (copy-file file fname))
        ((eq method 'ln) (add-name-to-file file fname))
        ((eq method 'lns) (make-symbolic-link file fname))
-       ((eq method 'url) (url-copy-file file fname)))
+       ((eq method 'url) (url-copy-file file fname))
+       ((eq method 'buf) (with-current-buffer file
+			   (write-file fname t))))
       (when org-attach-commit
         (org-attach-commit))
       (org-attach-tag)
-- 
2.17.0

