Skip to content

(feat): Allow one file to have multiple roam_key statements#1215

Merged
jethrokuan merged 5 commits intoorg-roam:masterfrom
kisaragi-hiu:multiple-roam-key-experiment
Nov 7, 2020
Merged

(feat): Allow one file to have multiple roam_key statements#1215
jethrokuan merged 5 commits intoorg-roam:masterfrom
kisaragi-hiu:multiple-roam-key-experiment

Conversation

@kisaragi-hiu
Copy link
Copy Markdown
Contributor

Motivation for this change

I'd like to be able to associate multiple ROAM_KEY keywords to the same file. This will allow multiple bibliographical entries to share the same file.

My actual use case is that I want to add both a book and its chapters to my bibliographical database. If a file can only have one ROAM_KEY keyword, we have to create a note file for each chapter and the book. I'd like to have the choice of putting them all in one file.

Note that this is the inverse of #587. #587 wants one-key-to-many-files, while this implements many-keys-to-one-file.

This also allows setting both a URL and a cite key as referring to a file. For example:

#+TITLE: swyx - Learn in public
#+ROAM_KEY: cite:shawnwang20180619
#+ROAM_KEY: https://www.swyx.io/learn-in-public/
Potential concerns

This PR makes org-roam--extract-global-props extract all matching props in the form (("key" . "val1") ("key" . "val2")) instead of only extracting the first match. I don't think this would be an issue, though, because assoc and other alist functions will simply take the first match themselves.

@kisaragi-hiu kisaragi-hiu force-pushed the multiple-roam-key-experiment branch from 5050e86 to 373a1ce Compare October 25, 2020 13:38
@kisaragi-hiu kisaragi-hiu marked this pull request as draft October 25, 2020 15:03
@kisaragi-hiu kisaragi-hiu force-pushed the multiple-roam-key-experiment branch 5 times, most recently from af3cb52 to 3a47bf9 Compare October 26, 2020 04:54
@kisaragi-hiu kisaragi-hiu marked this pull request as ready for review October 26, 2020 04:54
@kisaragi-hiu kisaragi-hiu force-pushed the multiple-roam-key-experiment branch 3 times, most recently from 3d393e5 to f7c5e56 Compare October 27, 2020 08:19
@kisaragi-hiu kisaragi-hiu changed the title Allow multiple keys / refs to refer to the same file (feat): Allow one file to have multiple roam_key statements Oct 27, 2020
@kisaragi-hiu kisaragi-hiu force-pushed the multiple-roam-key-experiment branch from f7c5e56 to 4ac5d25 Compare October 30, 2020 09:54
@jethrokuan
Copy link
Copy Markdown
Member

Looks like in this PR you're able to extract multiple refs from a single file, but you're not saving them all into the database. Any reason why?

@kisaragi-hiu
Copy link
Copy Markdown
Contributor Author

This PR does save all extracted refs into the database:

(defun org-roam-db--insert-ref (&optional update-p)
  "Update the ref of the current buffer into the cache.
If UPDATE-P is non-nil, first remove the ref for the file in the database."
  (let ((file (or org-roam-file-name (buffer-file-name)))
        (count 0))
    (when update-p
      (org-roam-db-query [:delete :from refs
                          :where (= file $s1)]
                         file))
    (when-let ((refs (org-roam--extract-refs)))
      (dolist (ref refs) ; <= this should insert all refs from this file
        (let ((key (cdr ref))
              (type (car ref)))
...

although I just realized that the --insert-ref name is inconsistent now that it's using org-roam--extract-refs, so I've now renamed it to org-roam-db--insert-refs.

I kept org-roam--extract-ref around in case another package uses this private function.

This changes org-roam--extract-global-props to collect all props, but
alist shadowing means that other uses of this function will still
extract the property they want correctly.
db--insert-{titles, links, tags, ...} all correspond to
org-roam--extract-{titles, links, tags, ...}, so
org-roam--extract-refs should also correspond to
org-roam-db--insert-refs.
@kisaragi-hiu kisaragi-hiu force-pushed the multiple-roam-key-experiment branch from 03c5e19 to 5335a8d Compare November 4, 2020 08:30
@natask
Copy link
Copy Markdown
Contributor

natask commented Nov 11, 2020

@kisaragi-hiu nice. I was looking for this in #1095. I opted for something more suited for my problem at the end though.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants