emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Move or rename a file in a link
@ 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
  0 siblings, 2 replies; 7+ messages in thread
From: Juan Manuel Macías @ 2022-03-05 19:58 UTC (permalink / raw)
  To: orgmode

Hi all,

I have written this simple function to move or rename a destination file
in an external link at point. I share it here in case it is useful to
someone.

Best regards,

Juan Manuel 

#+begin_src emacs-lisp
(defun my-org-replace-link-file (from to)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward org-bracket-link-regexp nil t)
      (when (string-match-p from (match-string 1))
	(replace-match (concat "[[file:" to "]]"))))))

(defun my-org-rename-link-file-at-point ()
  "Rename or move a file in an external link at point and
  update the link path"
  (interactive)
  (let* ((curr-dir (abbreviate-file-name default-directory))
	 (current-path (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))
    (my-org-replace-link-file current-path
			      (replace-regexp-in-string curr-dir "" new-path))))

#+end_src


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-03-22 15:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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

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).