Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Add roam-link company completion #15

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions company-org-roam.el
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@
(require 'cl-lib)
(require 'company)
(require 'org-roam)
(require 'org-roam-link)
(require 'dash)

(defvar org-roam-directory)
(defvar org-roam-link-use-roam-links)
(declare-function org-roam-link--current-buffer-roam-link-titles "org-roam-link" ())

(defgroup company-org-roam nil
"Company completion backend for Org-roam."
Expand Down Expand Up @@ -72,24 +75,35 @@ A value of nil means the caches never expire."

(defun company-org-roam--post-completion (title)
"The post-completion action for `company-org-roam'.
It deletes the inserted TITLE, and replaces it with a relative
file link.

The completion inserts the absolute file path where the buffer
does not have a corresponding file."
(let* ((cache (gethash (file-truename org-roam-directory) company-org-roam-cache))
(path (gethash title cache))
(current-file-path (-> (or (buffer-base-buffer)
(current-buffer))
(buffer-file-name)
(file-truename)
(file-name-directory))))
(delete-region (- (point) (length title)) (point))
(insert (format "[[file:%s][%s]]"
(if current-file-path
(file-relative-name path current-file-path)
path)
(org-roam--format-link-title title)))))
If completing an org file link, it deletes the inserted TITLE, and
replaces it with a relative file link. The completion inserts the
absolute file path where the buffer does not have a corresponding file.

If roam links are the default specified by `org-roam-link-use-roam-links',
then it will delete the inserted TITLE and replace it with a complete roam link.

If completing with the point inside of roam link syntax, [[roam:]], it
will simply finish the TITLE and move the point forward out of the link
regardless of the value of `org-roam-link-use-roam-links'."
(cond ((string= "roam" (org-element-property :type (org-element-context)))
(goto-char (org-element-property :end (org-element-context))))
(org-roam-link-use-roam-links
(delete-region (- (point) (length title)) (point))
(insert (format "[[roam:%s]]" title)))
(t (let* ((cache (gethash (file-truename org-roam-directory)
company-org-roam-cache))
(path (gethash title cache))
(current-file-path (-> (or (buffer-base-buffer)
(current-buffer))
(buffer-file-name)
(file-truename)
(file-name-directory))))
(delete-region (- (point) (length title)) (point))
(insert (format "[[file:%s][%s]]"
(if current-file-path
(file-relative-name path current-file-path)
path)
(org-roam--format-link-title title)))))))

(defun company-org-roam--filter-candidates (prefix candidates)
"Filter CANDIDATES that start with PREFIX.
Expand Down Expand Up @@ -129,10 +143,18 @@ Entries with no title do not appear in the completions."
titles))

(defun company-org-roam--get-candidates (prefix)
"Get the candidates for PREFIX."
(->> (company-org-roam--cache-get-titles)
(-flatten)
(company-org-roam--filter-candidates prefix)))
"Get the candidates for PREFIX.
If completing roam-link, add existing roam-link TITLEs from current
buffer as possible candidates."
(let ((roam-candidates
(if (or org-roam-link-use-roam-links
(string= "roam" (org-element-property :type (org-element-context))))
(org-roam-link--current-buffer-roam-link-titles)
nil)))
(->> (company-org-roam--cache-get-titles)
(-flatten)
(-union roam-candidates)
(company-org-roam--filter-candidates prefix))))

;;;###autoload
(defun company-org-roam (command &optional arg &rest _)
Expand Down