emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] [PATCH] Add a support for linking against an attachment of current e


From: Samuel Loury
Subject: [O] [PATCH] Add a support for linking against an attachment of current entry (was: easy way to link to file in attachment directory?)
Date: Fri, 05 Sep 2014 12:27:22 +0200
User-agent: Notmuch/0.17 (http://notmuchmail.org) Emacs/24.3.1 (x86_64-pc-linux-gnu)

Hi,
Darlan Cavalcante Moreira <address@hidden> writes:

> I have this
>   #+LINK: attach elisp:(org-open-file (org-attach-expand "%s"))
> in all of my org-mode files. In fact, I have this line, among others, in
> an org-mode setup file which is included in all of my org-mode files
> using "#+SETUPFILE:"
>
> Then I can create a link to an attachment with
> [[attach:filename_without_any_path.extension][description]]
>
> Also, just after attaching a new file org will automatically store the
> link so that you can use "C-c C-l" to include the link.
I like your solution, but I was quite frustrated by the lack of
completion against the attached files,

Here, I propose a solution using `org-add-link-type' and adding
"attach:" kind of links.

I actually propose to patch org mode to add this feature. I realized
that the part about finding the attached file was already implemented
into `org-attach-open', so I first extracted the functionality into a
separated function, then I made use of it to implement the new "attach:"
link and the associated completion.

For that reason, I split the work into two commits that you'll find
attached to this mail.

From d4b87312d96d4d1ec44562df5b951aa4df1f7a16 Mon Sep 17 00:00:00 2001
From: Konubinix <address@hidden>
Date: Fri, 5 Sep 2014 12:11:41 +0200
Subject: [PATCH 1/2] Get the part finding an attachment out of
 `org-attach-open'.

* lisp/org-attach.el (org-attach-find-file): Created
* lisp/org-attach.el (org-attach-open): Make use of `org-attach-open'

This will allow other functions to make use of the `org-attach-find-file'
feature.
---
 lisp/org-attach.el | 35 +++++++++++++++++++++++++++--------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index bcf7ba7..eb22b39 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -443,20 +443,39 @@ This will attempt to use an external program to show the 
directory."
   (let ((attach-dir (org-attach-dir t)))
     (dired attach-dir)))
 
+
+(defun org-attach-find-file (prompt)
+  "Provides the name of a file available in the attachments of current
+heading.
+If there are more than one attachment, you will be prompted for the file name."
+  (let* ((attach-dir (org-attach-dir t))
+        (files (org-attach-file-list attach-dir)))
+    (and
+     ;; return nil if not files
+     files
+     (or
+      ;; return the only available file
+      (and
+       (= (length files) 1)
+       (car files)
+       )
+      ;; or return the result of the completion
+      (org-icompleting-read
+       prompt
+       (mapcar 'list files) nil t)))))
+
 (defun org-attach-open (&optional in-emacs)
   "Open an attachment of the current task.
-If there are more than one attachment, you will be prompted for the file name.
+Use `org-attach-complete-file' to find the desired file.
 This command will open the file using the settings in `org-file-apps'
 and in the system-specific variants of this variable.
 If IN-EMACS is non-nil, force opening in Emacs."
   (interactive "P")
-  (let* ((attach-dir (org-attach-dir t))
-        (files (org-attach-file-list attach-dir))
-        (file (if (= (length files) 1)
-                  (car files)
-                (org-icompleting-read "Open attachment: "
-                                      (mapcar 'list files) nil t))))
-    (org-open-file (expand-file-name file attach-dir) in-emacs)))
+  (let* ((file (org-attach-find-file "Open attachment: "))
+        (attach-dir (org-attach-dir t)))
+    (if file
+       (org-open-file (expand-file-name file attach-dir) in-emacs)
+      (user-error "Current heading has no attached file"))))
 
 (defun org-attach-open-in-emacs ()
   "Open attachment, force opening in Emacs.
-- 
2.1.0

From 916a86e8db6ce6b2dc02ea663d70d07f952bbc33 Mon Sep 17 00:00:00 2001
From: Konubinix <address@hidden>
Date: Fri, 5 Sep 2014 12:19:12 +0200
Subject: [PATCH 2/2] Add a support for linking against an attachment of
 current entry.

* lisp/org-attach.el (org-attach-open-link, org-attach-complete-link): Created

Add a call to `org-add-link-type' to make use of the feature.
---
 lisp/org-attach.el | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index eb22b39..3d6dc71 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -494,6 +494,19 @@ Basically, this adds the path to the attachment directory, 
and a \"file:\"
 prefix."
   (concat "file:" (org-attach-expand file)))
 
+(defun org-attach-open-link (file-name)
+  "Open a link to a file attached to the current entry."
+  (let ((attach-dir (org-attach-dir t)))
+       (org-open-file (expand-file-name file-name attach-dir))))
+
+(defun org-attach-complete-link ()
+  "Completion on the attachments when creating a link."
+  (let* ((file (org-attach-find-file "Attached file:")))
+    (format "attach:%s" file)))
+
+;; Install the link type
+(org-add-link-type "attach" 'org-attach-open-link)
+
 (provide 'org-attach)
 
 ;; Local variables:
-- 
2.1.0

Best regards,
--
Konubinix
GPG Key    : 7439106A
Fingerprint: 5993 BE7A DA65 E2D9 06CE  5C36 75D2 3CED 7439 106A

Attachment: pgpidoLv_eN3h.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]