Method to move file path without breaking org file link

Original link: https://lujun9972.github.io/blog/2022/06/01/%E7%A7%BB%E5%8A%A8%E6%96%87%E4%BB%B6%E8%B7% AF%E5%BE%84%E5%8D%B4%E4%B8%8D%E7%A0%B4%E5%9D%8Forg-file-link%E7%9A%84%E6%96%B9%E6% B3%95/index.html

I am used to recording the file path, reading time, notes and other information of the e-book in the Org file. E-books are placed in a unified directory and stored according to the classified molecular directory.

But this creates a problem that I often move ebooks between different categories when organizing ebooks, which destroys the ebook file paths in the Org file.

If you need to manually modify the link address in the file every time, it will be too troublesome. Fortunately, by customizing the link, we can customize the search function to dynamically search the e-book path, instead of writing the storage path of the e-book.

Methods as below:

#+begin_src emacs-lisp
(defvar MY-EBOOK “~/ebooks”)
(defun my-search-ebook (pattern &optional _)
(let* ((files (directory-files-recursively MY-EBOOK pattern))
(count (length files)))
(cond ((= count 0)
(message “Can’t found file mathcing %s” pattern))
((= count 1)
(find-file (car files)))
((> count 1)
(find-file (completing-read “Please pick a file: ” files nil t))))))

(org-link-set-parameters “ebook”
:follow #’my-search-ebook
:help-echo (format “Open the file in %s” MY-EBOOK)
:face ‘(underline t))
#+end_src

This customizes a link of =ebook=, and visiting this link will automatically search and open the matching file from the directory (~/ebooks) defined by =MY-EBOOK=.

This article is reprinted from: https://lujun9972.github.io/blog/2022/06/01/%E7%A7%BB%E5%8A%A8%E6%96%87%E4%BB%B6%E8%B7% AF%E5%BE%84%E5%8D%B4%E4%B8%8D%E7%A0%B4%E5%9D%8Forg-file-link%E7%9A%84%E6%96%B9%E6% B3%95/index.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment