reddit / emacs

[ORG] How can I remove link at point and trash linked file?

thiscarhasfourtires · 2022-03-14 08:10
Sometimes, when I decide to remove a link, I also need to remove the local file it is pointing at. Would it be at all possible for me to remove both using a single command, say, `org-remove-link-and-trash-linked-file`? For a safety measure, I'd rather use `move-file-to-trash` than to actually delete the associated file. I already have this function to trash the current file, which works on my system (Linux Manjaro): (defun my-move-to-trash () (interactive) (if (y-or-n-p "Trash this file?") (progn (setq this-buffer buffer-file-truename) (move-file-to-trash (buffer-file-name)) (kill-current-buffer) (message "%s was trashed" this-buffer))) nil)) Thanks!
linwaytin · 2022-03-14 09:45
I have the same question. Does anybody have an answer?
organic-bookworm · 2022-03-14 13:19
Start with this snippet (defun org-remove-link-and-trash-linked-file () "Remove `org-mode' link at point and trash linked file." (interactive) (let* ((link (org-element-context)) (path (org-element-property :path link))) (move-file-to-trash path) (delete-region (org-element-property :begin link) (org-element-property :end link)) ) )