From d4b87312d96d4d1ec44562df5b951aa4df1f7a16 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. --- 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