From mboxrd@z Thu Jan 1 00:00:00 1970 From: Adam Porter Subject: [PATCH] Add org-attach-download command Date: Thu, 13 Sep 2018 04:51:28 -0500 Message-ID: <87in39ohcv.fsf@alphapapa.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:59525) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g0OHo-000313-U7 for emacs-orgmode@gnu.org; Thu, 13 Sep 2018 05:51:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g0OHm-0002xQ-7A for emacs-orgmode@gnu.org; Thu, 13 Sep 2018 05:51:44 -0400 Received: from [195.159.176.226] (port=52116 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g0OHl-0002wR-Uh for emacs-orgmode@gnu.org; Thu, 13 Sep 2018 05:51:42 -0400 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1g0OFb-0002uv-1u for emacs-orgmode@gnu.org; Thu, 13 Sep 2018 11:49:27 +0200 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" To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hi, This patch adds a new org-attach-download command, which downloads remote files with url.el and attaches them with org-attach. When called without a prefix, it first looks at point for a link, and prompts the user for a URL if none is found. When called with prefix, it prompts for the URL. Before downloading, it asks for confirmation. Thanks, Adam --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-org-attach-Add-org-attach-download-command.patch Content-Description: patch >From 426f95caa8e1f728327809cad2f9539857bb8567 Mon Sep 17 00:00:00 2001 From: Adam Porter Date: Thu, 13 Sep 2018 04:44:48 -0500 Subject: [PATCH] org-attach: Add org-attach-download command * lisp/org-attach.el: Require "files" and "url-handlers". (org-attach-download): Add command. (org-attach): Add to dispatcher under "L". * etc/ORG-NEWS: Add entry. --- etc/ORG-NEWS | 6 ++++++ lisp/org-attach.el | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 2a22be0..5b999c5 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -312,6 +312,12 @@ beginning of a headline when using Org speed commands. Now, if there is already a restriction at point, hitting =<= again (or =C-x C-x <=) will remove it. +*** ~org-attach-download~ downloads and attaches remote files + +The new command ~org-attach-download~, available through the +~org-attach~ dispatcher with =C-c C-a L=, downloads remote files and +attaches them with ~org-attach~. + ** New commands and functions *** ~org-insert-structure-template~ diff --git a/lisp/org-attach.el b/lisp/org-attach.el index 53389f7..5f80ac0 100644 --- a/lisp/org-attach.el +++ b/lisp/org-attach.el @@ -41,6 +41,8 @@ (require 'org) (require 'org-id) (require 'vc-git) +(require 'files) +(require 'url-handlers) (declare-function dired-dwim-target-directory "dired-aux") @@ -181,6 +183,9 @@ n Create a new attachment, as an Emacs buffer. z Synchronize the current task with its attachment directory, in case you added attachments yourself. +L Download file and attach it. Look for link at point, + or prompt for URL if not found or called with prefix. + o Open current task's attachments. O Like \"o\", but force opening in Emacs. f Open current task's attachment directory. @@ -210,6 +215,7 @@ i Make children of the current entry inherit its attachment directory."))) (let ((org-attach-method 'url)) (call-interactively 'org-attach-url))) ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new)) ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync)) + ((eq c ?L) (call-interactively 'org-attach-download)) ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open)) ((eq c ?O) (call-interactively 'org-attach-open-in-emacs)) ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal)) @@ -224,6 +230,30 @@ i Make children of the current entry inherit its attachment directory."))) 'org-attach-set-inherit)) (t (error "No such attachment command %c" c)))))) +(defun org-attach-download (url) + "Download file at URL and attach with `org-attach'. +Interactively, look for URL at point, prompting if not found. +With prefix, prompt for URL. Always prompt before downloading +file." + (interactive (list (if current-prefix-arg + (read-string "URL: ") + (or (org-element-property :raw-link (org-element-context)) + (read-string "URL: "))))) + (when (yes-or-no-p (concat "Attach file at URL: " url)) + (let* ((temp-dir (make-temp-file "org-attach-download-link-" 'dir)) + (basename (file-name-nondirectory (directory-file-name url))) + (local-path (expand-file-name basename temp-dir)) + size) + (unwind-protect + (progn + (url-copy-file url local-path 'ok-if-exists 'keep-time) + (setq size (file-size-human-readable + (file-attribute-size + (file-attributes local-path)))) + (org-attach-attach local-path nil 'mv) + (message "Attached %s (%s)" url size)) + (delete-directory temp-dir))))) + (defun org-attach-dir (&optional create-if-not-exists-p) "Return the directory associated with the current entry. This first checks for a local property ATTACH_DIR, and then for an inherited -- 2.7.4 --=-=-=--