8

移动文件路径却不破坏org file link的方法

 2 years ago
source link: https://www.lujun9972.win/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
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

移动文件路径却不破坏org file link的方法

我习惯在Org file中记录电子书的文件路径、阅读耗时、笔记等信息。 电子书放在统一的目录中,按照分类分子目录进行存放。

但这会带来一个问题,就是我在整理电子书时经常会在不同分类之间移动电子书,这就会破坏Org file中电子书的文件路径。

如果每次都需要手工修改文件中的链接地址的话,那就太麻烦了。好在通过自定义link,我们可以自定义搜索函数来动态搜索电子书路径,而不是写死电子书的存放路径。

方法如下:

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

这就自定义了一个 ebook 的link,访问这个 link 会自动从 MY-EBOOK 定义的目录(~/ebooks)中搜索匹配的文件并打开。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK