emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: "João Pedro de Amorim Paula" <jpedrodeamorim@gmail.com>
Cc: orgmode <emacs-orgmode@gnu.org>
Subject: Re: Move or rename a file in a link
Date: Sat, 19 Mar 2022 11:25:12 +0000	[thread overview]
Message-ID: <87ilsadw13.fsf@posteo.net> (raw)
In-Reply-To: <87pmmjgkjy.fsf@gmail.com> (=?utf-8?Q?=22Jo=C3=A3o?= Pedro de Amorim Paula"'s message of "Fri, 18 Mar 2022 15:52:33 -0300")

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 


  reply	other threads:[~2022-03-19 11:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-05 19:58 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 [this message]
2022-03-22 15:30         ` João Pedro de Amorim Paula

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=87ilsadw13.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    --cc=jpedrodeamorim@gmail.com \
    /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).