org-roam v1.2.3 链接 headline

之前有回复过类似的功能,用 ivy 实现了一个特性

主要的使用场景是插入概念性的笔记。因为这些笔记的层级关系太过明显。缺点 是,时间久了以后,流畅性还是不足。

Helm 有可以把多种搜索后端整合到一起的功能,如果把单个大文件单拿出来, 以 headline 搜索为结果,并入到 helm 的 org-roam-find-file 也许是一个不 错的选择。

(setq-default wr--head-var-a "headline name")

(defun wr/counsel-outline-action (x)
  (setq wr--head-var-a (car x)))

(defun wr/counsel-outline ()
  (interactive)
  (let ((settings (cdr (assq major-mode counsel-outline-settings))))
    (ivy-read "Outline: " (counsel-outline-candidates settings)
              :action (or (plist-get settings :action)
                          #'wr/counsel-outline-action)
              :history (or (plist-get settings :history)
                           'counsel-outline-history)
              :preselect (max (1- counsel-outline--preselect) 0)
              :caller (or (plist-get settings :caller)
                          'wr/counsel-outline))))

(defun wr/insert-a-head-from-a-file (x)
  (interactive)
  (xref-push-marker-stack)
  (save-restriction
    (save-excursion
      (with-ivy-window
        (pcase (cdr x)
          (`(:path ,foo :title ,bar) (progn
                                       (with-temp-buffer
                                         (erase-buffer)
                                         (insert-file-contents foo)
                                         (call-interactively #'wr/counsel-outline))
                                       (insert (format "[[file:%s::*%s][%s]]" foo (car (last (split-string (substring-no-properties wr--head-var-a) " → ")))
                                                       (string-trim-left (car (last (split-string (substring-no-properties wr--head-var-a) " → "))))))))))
      (message "A head has been inserted.")
      (xref-goto-xref))))


(ivy-set-actions
 t
 '(("h" wr/insert-a-head-from-a-file "insert")))