(feat): enable completions for both roam and fuzzy links (#1133)

Co-authored-by: Jethro Kuan <jethrokuan95@gmail.com>
This commit is contained in:
Noboru Ota
2020-09-26 12:05:13 +02:00
committed by GitHub
parent 176b2bf19d
commit 346bbf50a1

View File

@ -236,27 +236,29 @@ DESC is the link description."
"Do appropriate completion for the link at point." "Do appropriate completion for the link at point."
(let ((end (point)) (let ((end (point))
(start (point)) (start (point))
(exit-fn (lambda (&rest _) nil)) collection link-type)
collection)
(when (org-in-regexp org-link-bracket-re 1) (when (org-in-regexp org-link-bracket-re 1)
(setq start (+ (match-beginning 1) (length "roam:")) (setq start (match-beginning 1)
end (match-end 1)) end (match-end 1))
(let ((context (org-element-context))) (let ((context (org-element-context)))
(pcase (org-element-lineage context '(link) t) (pcase (org-element-lineage context '(link) t)
(`nil nil) (`nil nil)
(link (when (string-equal "roam" (org-element-property :type link)) (link
(pcase-let ((`(,type ,title _ ,star-idx) (setq link-type (org-element-property :type link))
(org-roam-link--split-path (org-element-property :path link)))) (when (member link-type '("roam" "fuzzy"))
(pcase type (when (string= link-type "roam") (setq start (+ start (length "roam:"))))
('title+headline (pcase-let ((`(,type ,title _ ,star-idx)
(when-let ((file (org-roam-link--get-file-from-title title t))) (org-roam-link--split-path (org-element-property :path link))))
(setq collection (apply-partially #'org-roam-link--get-headlines file)) (pcase type
(setq start (+ start star-idx 1)))) ('title+headline
('title (when-let ((file (org-roam-link--get-file-from-title title t)))
(setq collection #'org-roam-link--get-titles)) (setq collection (apply-partially #'org-roam-link--get-headlines file))
('headline (setq start (+ start star-idx 1))))
(setq collection #'org-roam-link--get-headlines) ('title
(setq start (+ start star-idx 1)))))))))) (setq collection #'org-roam-link--get-titles))
('headline
(setq collection #'org-roam-link--get-headlines)
(setq start (+ start star-idx 1))))))))))
(when collection (when collection
(let ((prefix (buffer-substring-no-properties start end))) (let ((prefix (buffer-substring-no-properties start end)))
(list start end (list start end
@ -268,7 +270,10 @@ DESC is the link description."
(funcall collection)))) (funcall collection))))
(not org-roam-completion-ignore-case)) (not org-roam-completion-ignore-case))
collection) collection)
:exit-function exit-fn))))) :exit-function
(lambda (str &rest _)
(delete-char (- (length str)))
(insert (concat (unless (string= link-type "roam") "roam:") str))))))))
(provide 'org-roam-link) (provide 'org-roam-link)
;;; org-roam-link.el ends here ;;; org-roam-link.el ends here