1

When in a dired buffer, I use R or dired-do-rename to move files from one directory to another (typically my "refile" directory to where the file(s) actually belong(s)).

When I do this I'd like to store an org-link using org-store-link to the "refiled" document(s) in it's/their new location preferable using th prefix argument, C-u.

Any idea how to accomplish this? Ideally it would work for a single or multiple files marked with m. If performing this action on multiple files, all links should be stored.

I imagine this is possible with defadvice since I can't find any obvious hooks but don't have the experience to implement it.

  • 1
    I suggest defining a new command and bind that, instead of advising. – YoungFrog May 9 '16 at 5:51
  • I think that's a good idea too. Any idea which function I could use to get the new file path? – sk8ingdom May 9 '16 at 6:24
  • Good question. When renaming, dired adds a mark to the destination file. It seems dired-add-file is responsible for this. You could advise (yes, I know...) that function to make it log/record the filenames it marks into some dynamically scoped variable. Then you can let-bind that variable around the call to dired-do-rename. Not sure if that is super clean, but I don't know better anyway. – YoungFrog May 9 '16 at 9:24
0

Although I couldn't get it to work with a prefix argument (C-u), I advised dired-rename-file to automatically store every renamed file (including those bulk moved using marks) to org-stored-links which can then be inserted into an org-mode file with org-insert-link using the following:

;; Have dired store org-link when a file or files(s) are renamed
(defun my/dired-rename-file (file newname ok-if-already-exists)
  "Store org-link to the renamed file."
  (push (list newname (file-name-nondirectory newname)) org-stored-links))

(advice-add #'dired-rename-file :after #'my/dired-rename-file)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.