emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "João Pedro de Amorim Paula" <jpedrodeamorim@gmail.com>
To: "Juan Manuel Macías" <maciaschain@posteo.net>
Cc: orgmode <emacs-orgmode@gnu.org>
Subject: Re: Move or rename a file in a link
Date: Tue, 22 Mar 2022 12:30:32 -0300	[thread overview]
Message-ID: <87cziem2cn.fsf@gmail.com> (raw)
In-Reply-To: <87ilsadw13.fsf@posteo.net>

[-- Attachment #1: Type: text/plain, Size: 1779 bytes --]

On 19 March 2022 11:25, Juan Manuel Macías <maciaschain@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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-rename-link.patch --]
[-- Type: text/x-patch, Size: 1012 bytes --]

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

[-- Attachment #3: Type: text/plain, Size: 439 bytes --]


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)

      reply	other threads:[~2022-03-22 15:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-05 19:58 Move or rename a file in a link Juan Manuel Macías
2022-03-06  8:09 ` Uwe Brauer
2022-03-09 20:30 ` João Pedro de Amorim Paula
2022-03-10 14:58   ` Juan Manuel Macías
2022-03-18 18:52     ` João Pedro de Amorim Paula
2022-03-19 11:25       ` Juan Manuel Macías
2022-03-22 15:30         ` João Pedro de Amorim Paula [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87cziem2cn.fsf@gmail.com \
    --to=jpedrodeamorim@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=maciaschain@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).