emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org-attach: Allow attaching file from visited buffer
@ 2018-06-21 14:27 Eric Danan
  2018-06-21 15:47 ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Danan @ 2018-06-21 14:27 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 296 bytes --]

Hello,

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. This patch adds a `b' command to the attach
dispatcher that reads a buffer and writes it in the attachment
directory.

Regards,

Eric

[-- Attachment #2: 0001-org-attach-Allow-attaching-file-from-visited-buffer.patch --]
[-- Type: application/octet-stream, Size: 3492 bytes --]

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


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

* Re: [PATCH] org-attach: Allow attaching file from visited buffer
  2018-06-21 14:27 [PATCH] org-attach: Allow attaching file from visited buffer Eric Danan
@ 2018-06-21 15:47 ` Nicolas Goaziou
  2018-06-25 13:04   ` Eric Danan
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2018-06-21 15:47 UTC (permalink / raw)
  To: Eric Danan; +Cc: emacs-orgmode

Hello,

Eric Danan <eric.danan@u-cergy.fr> writes:

> 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. This patch adds a `b' command to the attach
> dispatcher that reads a buffer and writes it in the attachment
> directory.

Thank you.

One issue is that we're losing the method choice (symlink, copy,
hardlink).

However, `org-attach-attach' already offers to start file system
navigation from a Dired side buffer, if any. We may extend this idea: if
there is a buffer currently displayed in another window, but no Dired
buffer, offer its default directory as a start for completion.

Another idea is to create a new function that will attach the current
file to the subtree in another window. See
`org-attach-dired-to-subtree', for example.

If you haven't signed FSF papers, you need to add TINYCHANGE at the end
of the commit message.

Regards,

-- 
Nicolas Goaziou

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

* Re: [PATCH] org-attach: Allow attaching file from visited buffer
  2018-06-21 15:47 ` Nicolas Goaziou
@ 2018-06-25 13:04   ` Eric Danan
  2018-11-02  9:24     ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Danan @ 2018-06-25 13:04 UTC (permalink / raw)
  To: mail; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 3692 bytes --]

Sorry for the slow reply.

I tried something similar to your second proposal (mimicking
`org-attach-dired-to-subtree'), but more convenient in my opinion and
not losing the method choice. It mimicks the mechanism to store links:
there is a new command `org-store-attachment' (to which one could give
a global keybinding such as `C-c a`) that stores an attachment to the
current buffer in a new variable `org-stored-attachments', and
`org-attach-attach', when called interactively, prompts to select an
attachment from this variable if it is non nil (the attachment is then
removed from the variable). See attached patch (yes I've signed the
FSF papers).

"Storing an attachment" simply means storing the buffer file name or,
if there is none, the buffer name, to the variable (there may be a
better terminology for that). Then `org-attach-attach' detects if the
"file" is actually the name of a non-file-visiting buffer and uses
`write-file' rather than the specified or default method in that case.
So we keep the method choice and don't need to add a specific command
for buffers to the dispatcher. If you think it's not worth introducing
complications for allowing to attach non-file-visiting buffers, we
could simply prevent this in `org-store-attachment'. I also made it so
that if `org-store-attachment' is called from a dired buffer, then the
file at point is stored rather than the dired buffer itself.

This can certainly be improved, but I hope it is sufficient for you to
judge whether you think it is a good idea or not. Possible
improvements I thought about:
- When `org-store-attachment' is called from a dired buffer and there
are marked files, we could store these rather than the file at point
(similar to `org-attach-dired-to-subtree').
- We could add a mechanism for making `org-attach-attach'
unconditionally read a file name, ignoring `org-stored-attachments'
(i.e. a prefix argument, distinct from the one to visit the attachment
directory). We could also add a command to clear
`org-stored-attachments' from the dispatcher.
- We could check whether the file to attach is modified and prompt the
user to save it in that case (this is actually independent from the
present discussion).

I understand that other users may not like this mechanism and prefer
different solutions (or leaving things as they are). There was a
recent suggestion to make the list of commands in the attachment
dispatcher customizable:

http://lists.gnu.org/archive/html/emacs-orgmode/2018-06/msg00139.html

Do you think it would make sense to do so?

Thank you for your attention.
On Thu, Jun 21, 2018 at 5:47 PM Nicolas Goaziou <mail@nicolasgoaziou.fr> wrote:
>
> Hello,
>
> Eric Danan <eric.danan@u-cergy.fr> writes:
>
> > 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. This patch adds a `b' command to the attach
> > dispatcher that reads a buffer and writes it in the attachment
> > directory.
>
> Thank you.
>
> One issue is that we're losing the method choice (symlink, copy,
> hardlink).
>
> However, `org-attach-attach' already offers to start file system
> navigation from a Dired side buffer, if any. We may extend this idea: if
> there is a buffer currently displayed in another window, but no Dired
> buffer, offer its default directory as a start for completion.
>
> Another idea is to create a new function that will attach the current
> file to the subtree in another window. See
> `org-attach-dired-to-subtree', for example.
>
> If you haven't signed FSF papers, you need to add TINYCHANGE at the end
> of the commit message.
>
> Regards,
>
> --
> Nicolas Goaziou

[-- Attachment #2: 0001-org-attach-Add-mechanism-to-store-attachment-to-curr.patch --]
[-- Type: application/octet-stream, Size: 4509 bytes --]

From 3bafeda53c52c6b9fc3a611cbcb23bfb4594ca64 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: Add mechanism to store attachment to current
 buffer

* lisp/org-attach.el (org-stored-attachments): New variable.
(org-store-attachment): New autoloaded, interactive command to store
an attachment to the current buffer in the above variable.
(org-attach-attach): If called interactively and there are stored
attachments, prompt to select one of them.  Also if the attachment is
to a buffer not visiting a file, fallback to writing the buffer rather
than using the specified or default attachment method.
---
 lisp/org-attach.el | 47 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 8 deletions(-)

diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index 192815f4..df8e458d 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -152,6 +152,9 @@ (defcustom org-attach-annex-auto-get 'ask
 	  (const :tag "always get from annex if necessary" t)
 	  (const :tag "never get from annex" nil)))
 
+(defvar org-stored-attachments nil
+  "Contains the attachments stored with `org-store-attachment'.")
+
 ;;;###autoload
 (defun org-attach ()
   "The dispatcher for attachment commands.
@@ -193,7 +196,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: [acmlyunzoOfFdD]")
 	  (setq c (read-char-exclusive))
 	  (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
       (cond
@@ -397,14 +400,22 @@ (defun org-attach-attach (file &optional visit-dir method)
 `org-attach-method'."
   (interactive
    (list
-    (read-file-name "File to keep as an attachment:"
-                    (or (progn
-                          (require 'dired-aux)
-                          (dired-dwim-target-directory))
-                        default-directory))
+    (if org-stored-attachments
+	(let ((file (org-completing-read "Attachment: "
+					 org-stored-attachments)))
+	  (setq org-stored-attachments
+		(remove file org-stored-attachments))
+	  file)
+      (read-file-name "File to keep as an attachment:"
+                      (or (progn
+                            (require 'dired-aux)
+                            (dired-dwim-target-directory))
+                          default-directory)))
     current-prefix-arg
     nil))
-  (setq method (or method org-attach-method))
+  (setq method (if (file-exists-p file)
+		   (or method org-attach-method)
+		 'buf))
   (let ((basename (file-name-nondirectory file)))
     (when (and org-attach-file-list-property (not org-attach-inherited))
       (org-entry-add-to-multivalued-property
@@ -416,12 +427,15 @@ (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)
       (cond ((eq org-attach-store-link-p 'attached)
              (org-attach-store-link fname))
+	    ((eq method 'buf))
             ((eq org-attach-store-link-p t)
              (org-attach-store-link file)))
       (if visit-dir
@@ -613,6 +627,23 @@ (defun org-attach-dired-to-subtree (files)
       (org-attach-attach file))
     (select-window start-win)))
 
+;;;###autoload
+(defun org-store-attachment (file)
+ "Store an attachment to the current buffer.
+\\<org-mode-map>
+This attachment is added to `org-stored-attachments' and can later be inserted
+into an Org buffer with `org-attach' (`\\[org-attach]').
+
+If the current buffer is a dired buffer, store an attachments to
+the file at point instead."
+ (interactive
+  (list (if (derived-mode-p 'dired-mode)
+	    (ignore-errors (dired-get-filename))
+	  (or (buffer-file-name (buffer-base-buffer))
+	      (buffer-name (buffer-base-buffer))))))
+ (when file
+   (push file org-stored-attachments)))
+   
 \f
 
 (add-hook 'org-archive-hook 'org-attach-archive-delete-maybe)
-- 
2.17.0


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

* Re: [PATCH] org-attach: Allow attaching file from visited buffer
  2018-06-25 13:04   ` Eric Danan
@ 2018-11-02  9:24     ` Nicolas Goaziou
  2018-11-02 13:14       ` Eric Danan
  0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2018-11-02  9:24 UTC (permalink / raw)
  To: Eric Danan; +Cc: emacs-orgmode

Hello,

Eric Danan <eric.danan@u-cergy.fr> writes:

> Sorry for the slow reply.

Sorry for the even slower reply.

> I tried something similar to your second proposal (mimicking
> `org-attach-dired-to-subtree'), but more convenient in my opinion and
> not losing the method choice. It mimicks the mechanism to store links:
> there is a new command `org-store-attachment' (to which one could give
> a global keybinding such as `C-c a`) that stores an attachment to the
> current buffer in a new variable `org-stored-attachments', and
> `org-attach-attach', when called interactively, prompts to select an
> attachment from this variable if it is non nil (the attachment is then
> removed from the variable). See attached patch (yes I've signed the
> FSF papers).
>
> "Storing an attachment" simply means storing the buffer file name or,
> if there is none, the buffer name, to the variable (there may be a
> better terminology for that). Then `org-attach-attach' detects if the
> "file" is actually the name of a non-file-visiting buffer and uses
> `write-file' rather than the specified or default method in that case.
> So we keep the method choice and don't need to add a specific command
> for buffers to the dispatcher. If you think it's not worth introducing
> complications for allowing to attach non-file-visiting buffers, we
> could simply prevent this in `org-store-attachment'. I also made it so
> that if `org-store-attachment' is called from a dired buffer, then the
> file at point is stored rather than the dired buffer itself.

This sounds interesting, but I have the feeling that would be a bit
complicated in practice.

I assume that when you want to attach a document to a node, you're
already at the node. Ideally, it should be possible to attach something
in one step from here. With your proposal, you need to first mark
a buffer as an attachment, then go back to the node and attach it
properly.

Moreover, when you marked something as an attachment, changed your mind,
and now want to attach something else, `org-store-attachment' prevents
from doing this. The variable shadows other options when it is non-nil.

There are also issues, e.g., what happens if the buffer marked as an
attachment has been killed?

> This can certainly be improved, but I hope it is sufficient for you to
> judge whether you think it is a good idea or not.

I eventually added, in "next" branch (Org 9.3), something much simpler.
Pressing <b> from the attach dispatcher offers to choose a buffer from
the list of active buffers, and save its contents to a file in the
attachment directory.

Does it suit your needs?

> - We could add a mechanism for making `org-attach-attach'
> unconditionally read a file name, ignoring `org-stored-attachments'
> (i.e. a prefix argument, distinct from the one to visit the attachment
> directory). We could also add a command to clear
> `org-stored-attachments' from the dispatcher.

That would solve second problem, but not the others.

> I understand that other users may not like this mechanism and prefer
> different solutions (or leaving things as they are). There was a
> recent suggestion to make the list of commands in the attachment
> dispatcher customizable:
>
> http://lists.gnu.org/archive/html/emacs-orgmode/2018-06/msg00139.html
>
> Do you think it would make sense to do so?

It could make sense to allow customizing attachment actions, if anyone
wants try implementing it.

Thank you for your work and for the ideas!

Regards,

-- 
Nicolas Goaziou

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

* Re: [PATCH] org-attach: Allow attaching file from visited buffer
  2018-11-02  9:24     ` Nicolas Goaziou
@ 2018-11-02 13:14       ` Eric Danan
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Danan @ 2018-11-02 13:14 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

Hello,

> I eventually added, in "next" branch (Org 9.3), something much 
> simpler.
> Pressing <b> from the attach dispatcher offers to choose a 
> buffer from
> the list of active buffers, and save its contents to a file in 
> the
> attachment directory.
>
> Does it suit your needs?

Yes, thanks.

> It could make sense to allow customizing attachment actions, if 
> anyone
> wants try implementing it.

I may try it when I find some time but probably not very soon.

Eric

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

end of thread, other threads:[~2018-11-02 13:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-21 14:27 [PATCH] org-attach: Allow attaching file from visited buffer Eric Danan
2018-06-21 15:47 ` Nicolas Goaziou
2018-06-25 13:04   ` Eric Danan
2018-11-02  9:24     ` Nicolas Goaziou
2018-11-02 13:14       ` Eric Danan

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).