比如我现在用的是这样的
(defun org-wiki-open (path _)
"定义回车打开链接"
(eye/open-git-file-v2 path))
(defun eye--org-wiki-completion (&optional arg)
"定义complete,支持用C-c C-l的方式自动插入链接"
(let* ((file-list (eye--get-dir-file-list (concat locale-notebook-dir "/org")))
sel-name)
(setq sel-name (ivy-read "Select wiki note: " file-list))
(setq sel-name (file-name-base sel-name))
;; (insert (format "[[wiki:%s]]" sel-name))
(format "wiki:%s" sel-name)
))
(org-link-set-parameters "wiki"
:face '(:foreground "orange" :underline t)
:follow #'org-wiki-open
:complete 'eye--org-wiki-completion)
在 follow指定的函数中,path就是链接中的路径,例如链接 [[wiki:test-file.org]]
,org-wiki-open中解析这个path后想做什么事情都可以,完全可以自定义一个格式,[[wiki:test-file.org::@header]]
解析到::@
时,就在打开文件后再搜索跳到那个位置,甚至可以传给swiper自动弹出提示选择。
我上面的 eye/open-git-file-v2 函数中,是通过git 查找文件,并弹出ivy选择。
(defun eye/open-git-file-v2 (&optional filename)
(interactive)
(let* ((default-directory (concat locale-notebook-dir "/org"))
(current-filename (file-name-base (buffer-name)))
temp-path)
(shell-command "git ls-tree -r HEAD --name-only > list_files")
(setq temp-path
(ivy-read "select:"
(s-split "\n" (get-string-from-file "list_files"))
:initial-input filename
))
(delete-file "list_files")
(when (file-exists-p temp-path)
(find-file temp-path)
)))