diff --git a/modules/lib/defuns-org-attach.el b/modules/lib/defuns-org-attach.el index 715b99140..840595322 100644 --- a/modules/lib/defuns-org-attach.el +++ b/modules/lib/defuns-org-attach.el @@ -1,10 +1,12 @@ ;;; defuns-org-attach.el --- custom attachment system +;; I know Org has its own attachment system, but I don't like it. + ;;;###autoload (autoload 'narf:org-attach "defuns-org-attach" nil t) (evil-define-command narf:org-attach (&optional bang link) (interactive "") (if (not link) - (narf/org-attachment-list) + (narf:org-attachment-list bang) (require 'org-download) (let ((new-path (if bang (format "%s/%s" (expand-file-name org-download-image-dir org-directory) @@ -13,14 +15,33 @@ (format-time-string org-download-timestamp) (file-name-extension link))) buffer-file-name (org-download--fullname link)))) - (when new-path - (cond ((string-match-p "^https?://" link) - (url-copy-file link new-path)) - (t (copy-file link new-path))) - (insert (format "[[./%s]]" (f-relative new-path default-directory))))))) + (unless new-path + (user-error "No file was provided")) + (cond ((string-match-p "^https?://" link) + (url-copy-file link new-path)) + (t (copy-file link new-path))) + (insert (format "[[./%s]]" (f-relative new-path default-directory)))))) + +;;;###autoload (autoload 'narf:org-attachment-list "defuns-org-attach" nil t) +(evil-define-command narf:org-attachment-list (&optional bang) + (interactive "") + (if bang + (narf:org-attachment-cleanup) + (let ((attachments (narf-org--get-attachments))) + (unless attachments + (user-error "No attachments in this file")) + (helm :sources + (helm-build-sync-source "Attachments" + :candidates attachments + :real-to-display 'narf-org--attachment-real-to-display + :action '(("Go to Attachment in Buffer" . narf-org--attachment-find) + ("Reveal Attachment in Finder" . narf-org--attachment-reveal) + ("Open Attachment" . narf-org--attachment-open) + ("Delete Attachment" . narf-org--attachment-delete))))))) ;; TODO Improve -(defun narf/org-reveal-attachments () +;;;###autoload +(defun narf/org-attachment-reveal () (interactive) (let ((context (org-element-context))) (narf-open-with @@ -29,8 +50,7 @@ (f-dirname (org-element-property :path context)) org-download-image-dir)))) -;;;###autoload -(defun narf--org-attachments () +(defun narf-org--get-attachments () (org-element-map (org-element-parse-buffer) 'link (lambda (link) (when (and (string= (org-element-property :type link) "file") @@ -38,44 +58,26 @@ (org-element-property :path link))) (org-element-property :path link))))) -;; (defun narf--org-attachments-in-dir () -;; (-map (lambda (f) (concat "./" (f-relative f))) -;; (append (f-entries org-download-image-dir) -;; (unless (f-same? -;; (expand-file-name org-download-image-dir) -;; (expand-file-name org-download-image-dir org-directory)) -;; (f-entries (expand-file-name org-download-image-dir org-directory))))) -;; ) +(defun narf-org--attachment-real-to-display (real) + (propertize (f-filename real) 'face (if (file-exists-p real) 'helm-ff-file 'shadow))) -(defun narf--org-attachment-real-to-display (real) - (format "[%s] %s" - (if (file-exists-p real) "X" "") - (f-filename real))) +(defun narf-org--attachment-find (file) + (search-forward-regexp (format "[[\\(file:\\)?%s]]" file) nil t) + (ignore-errors + (save-excursion + (outline-previous-visible-heading 1) + (org-show-subtree)))) -;; TODO Add delete action -;; TODO Goto link on select -(defun narf-org-attachment-source () - (helm-build-sync-source "Attachments" - :candidates (narf--org-attachments) - :real-to-display 'narf--org-attachment-real-to-display - :action (lambda (f) (narf-open-with nil (f-dirname f))))) +(defun narf-org--attachment-reveal (file) + (narf-open-with nil (f-dirname file))) -;; TODO Organize this better -;;;###autoload -(defun narf/org-attachment-list () - (interactive) - (helm :sources (narf-org-attachment-source))) +(defun narf-org--attachment-open (file) + (narf-open-with nil file)) -;; TODO -;; (defun narf/org-attachment-cleanup (&optional file) -;; (interactive) -;; ) - -;; TODO -;; (defun narf/org-attachment-cleanup-all () -;; (interactive) -;; (dolist (file org-agenda-files) -;; (narf/org-attachment-cleanup file))) +(defun narf-org--attachment-delete (file) + (delete-file file) + (narf-org--attachment-find file) + (message "File deleted, now delete the link! (%s)" file)) (provide 'defuns-org-attach) ;;; defuns-org-attach.el ends here