On 19 March 2022 11:25, Juan Manuel Macías <maciasch...@posteo.net> wrote:
> #+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 Just a couple of fixes and it seems to be working fine.
diff -u a/org-rename-link.el b/org-rename-link.el --- a/org-rename-link.el 2022-03-22 12:08:45.790727092 -0300 +++ b/org-rename-link.el 2022-03-22 12:25:31.500218923 -0300 @@ -23,8 +23,11 @@ (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 "]]"))))))) + (while (re-search-forward org-link-bracket-re nil t) + (replace-match + (cond + ((string-match-p (format "^attachment:%s\\'" from) (match-string 1)) + (format "attachment:%s" (file-name-nondirectory to))) + ((string-match-p (format "^file.*:%s\\'" from) (match-string 1)) + (format "file:%s" to))) + nil nil nil 1)))) Diff finished. Tue Mar 22 12:26:56 2022
All this patch does is to remove the directory name from the =attachment:file.ext= -- given that the path is calculated by org-attach --, and use the group 1 of the match string (as well as updating the use of an obsolete variable), which corresponds to the actual link portion, leaving the description unchanged. Cordially, -- João Pedro de A. Paula IT undergraduate at Universidade Federal do Rio Grande do Norte (UFRN)