[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Move or rename a file in a link
From: |
Juan Manuel Macías |
Subject: |
Re: Move or rename a file in a link |
Date: |
Sat, 19 Mar 2022 11:25:12 +0000 |
Hi João,
João Pedro de Amorim Paula writes:
> I mean org attachments. I use org-attach extensively to store documents
> with notes. So I'd have a heading like so
>
> * Documents
> :PROPERTIES:
> :DIR: data/docs/
> :END:
>
> - [[Registration][attachment:registration.pdf]] :: My registration.
>
> And say I'd like to rename the file. I would need to rename it inside
> data/docs and I would also need to edit the link, so I would like to
> have a way to do it automatically. I just got a computer back so I will
> be trying to adapt what you did to work with org-attach, but if you
> figure out a way to do it as well, please, let me know.
I see. This is a new version that would also work with org attachment
links. I haven't tested it much and the function is a bit tricky, but I
think it works fine:
#+begin_src emacs-lisp
(defun my-org-rename-link-file-at-point ()
(interactive)
(let* ((curr-dir (if (equal (org-element-property :type
(org-element-context)) "attachment")
(concat (abbreviate-file-name (org-attach-dir)) "/")
(abbreviate-file-name default-directory)))
(current-path (if (equal (org-element-property :type
(org-element-context)) "attachment")
(concat curr-dir (org-element-property :path
(org-element-context)))
(org-element-property :path (org-element-context))))
(new-path (read-file-name "Rename file at point to: " current-path)))
(rename-file current-path new-path)
(message (concat "moved to: " new-path))
(if (directory-name-p new-path)
(setq new-path (concat new-path (file-name-nondirectory
current-path)))
(setq new-path new-path))
(if (equal (org-element-property :type (org-element-context))
"attachment")
(my-org-replace-link-file (file-name-nondirectory current-path)
(replace-regexp-in-string
curr-dir "" new-path))
(my-org-replace-link-file current-path
(replace-regexp-in-string
curr-dir "" new-path)))))
(defun my-org-replace-link-file (from to)
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-bracket-link-regexp nil t)
(cond ((string-match-p (concat "attachment:" from) (match-string 1))
(replace-match (concat "[[attachment:" to "]]")))
((string-match-p from (match-string 1))
(replace-match (concat "[[file:" to "]]")))))))
#+end_src
Best regards,
Juan Manuel