From mboxrd@z Thu Jan 1 00:00:00 1970 From: Samuel Loury Subject: [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 Message-ID: <87zjee1hx1.fsf@konixwork.incubateur.ens-lyon.fr> References: <8738c7ni42.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:34438) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XPqk6-0000HO-Cm for emacs-orgmode@gnu.org; Fri, 05 Sep 2014 06:27:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XPqjx-0001XO-1I for emacs-orgmode@gnu.org; Fri, 05 Sep 2014 06:27:46 -0400 Received: from mail-we0-x22f.google.com ([2a00:1450:400c:c03::22f]:58877) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XPqjw-0001WS-M1 for emacs-orgmode@gnu.org; Fri, 05 Sep 2014 06:27:36 -0400 Received: by mail-we0-f175.google.com with SMTP id k48so11520048wev.34 for ; Fri, 05 Sep 2014 03:27:35 -0700 (PDT) In-Reply-To: <8738c7ni42.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Darlan Cavalcante Moreira , Mirko Vukovic Cc: emacs-orgmode@gnu.org --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Hi, Darlan Cavalcante Moreira 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. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Get-the-part-finding-an-attachment-out-of-org-attach.patch Content-Transfer-Encoding: quoted-printable From=20d4b87312d96d4d1ec44562df5b951aa4df1f7a16 Mon Sep 17 00:00:00 2001 From: Konubinix 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. =2D-- 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 =2D-- 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))) =20 + +(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 n= ame." + (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 + (=3D (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. =2DIf 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") =2D (let* ((attach-dir (org-attach-dir t)) =2D (files (org-attach-file-list attach-dir)) =2D (file (if (=3D (length files) 1) =2D (car files) =2D (org-icompleting-read "Open attachment: " =2D (mapcar 'list files) nil t)))) =2D (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")))) =20 (defun org-attach-open-in-emacs () "Open attachment, force opening in Emacs. =2D-=20 2.1.0 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0002-Add-a-support-for-linking-against-an-attachment-of-c.patch Content-Transfer-Encoding: quoted-printable From=20916a86e8db6ce6b2dc02ea663d70d07f952bbc33 Mon Sep 17 00:00:00 2001 From: Konubinix 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): Crea= ted Add a call to `org-add-link-type' to make use of the feature. =2D-- 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 =2D-- a/lisp/org-attach.el +++ b/lisp/org-attach.el @@ -494,6 +494,19 @@ Basically, this adds the path to the attachment direct= ory, and a \"file:\" prefix." (concat "file:" (org-attach-expand file))) =20 +(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) =20 ;; Local variables: =2D-=20 2.1.0 --=-=-= Content-Type: text/plain Best regards, -- Konubinix GPG Key : 7439106A Fingerprint: 5993 BE7A DA65 E2D9 06CE 5C36 75D2 3CED 7439 106A --=-=-=-- --==-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJUCZALAAoJEHXSPO10ORBqgRgH/i/+MTswGLjAhco9jAVpouLa CAqmShs7y5f4C2735cf+k3OEoatpELo/UJ+SrMFFyWakWdZ8kbvibc4a3fjIUw+2 0du0ojKls5A2c/T9FOGPbIWrEric71cBN8iuRUdyxFI5rXmo30yWX1AQKDg9HAUC fiBeb4Zp0Kl+c0wUvfFPgq3uuRWkl86baqRE3d6SglHW28c/PVhn5QwcnUcMK9b5 hg45IDiQp36wq6IrqzqVgNTlWda2BtpGky8N3nFxW5uyU07BTkQh3VQs2mFNA04F PRakTD2Uac1PcG44gMd8e7VH7S6qRGzlaYBR0YQecbdQJKTDxvhVEk82GM0YtlY= =F4fY -----END PGP SIGNATURE----- --==-=-=--