# Calling \`org-ref-open-bibtex-notes\` triggers "Which function for creating the link" question

**URL:** <https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806>\
**Category:** Troubleshooting\
**Created:** [September 28, 2020, 7:41pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806 "2020-09-28T19:41:15Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![apc](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@apc](https://org-roam.discourse.group/u/apc)\
**Post date:** [September 28, 2020, 7:41pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/1 "2020-09-28T19:41:15Z")

</div>

Hi all,

First, a non-perfunctory thanks to everyone contributing to this project. I’m quite new to this but so far I’m having a blast.

Whenever I try to create a new `org-roam` note from a `.bib` file, using `org-ref-open-bibtex-notes`, I’m asked the question “Which function for creating the link?”. The options are `org-ref-bibtex-store-link` and `org-bibtex-store-link`. Has anyone else encountered this behavior?

From what I can tell, this question is being asked by `org-store-link`, which is being called by `orb-notes-fn` at some point—`org-ref-open-bibtex-notes` is calling `org-ref-notes-function`, which `org-roam-bibtex` sets to `orb-notes-fn`. I know very little Elisp, but from going over the definitions of the relevant functions I couldn’t figure out why this choice between the two `*-store-link` functions is coming in. I just want to be able to answer that question once and for all.

(I tried setting up `org-ref` without `org-roam-bibtex` but I failed, so it could be that the issue has nothing to do with `org-roam-bibtex`, in which case I apologize.)

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [September 28, 2020, 8:31pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/2 "2020-09-28T20:31:23Z")

</div>

Hi @apc, welcome!

I cannot reproduce this behaviour. Typing `M-x org-ref-open-bibtex-notes` when in a record in a `.bib` file brings up Org Roam note creation dialog.

`orb-note-fn` does not call `org-store-link`, it calls `org-ref-find-bibliography`. The first thing the latter does is checking whether you are in a `.bib` file, and if you are, returns that filename and exits.

Are you sure you’re calling `org-ref-open-bibtex-notes` and not something else, for example with a default keychord that has been rebound to something else?

You can also try `M-x org-ref-bibtex-hydra/body` and then choose `n: Open notes` - it should run the same function.

---

<div class="post-metadata">

**Author:** ![apc](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@apc](https://org-roam.discourse.group/u/apc)\
**Post date:** [September 28, 2020, 8:59pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/3 "2020-09-28T20:59:51Z")

</div>

Thanks!

Yes, I’m sure I’m calling `org-ref-open-bibtex-notes`. Running `M-x org-ref-bibtex-hydra/body` and then `n` also asks me to pick a function.

 ![Screen Recording 2020-09-28 at 4.48.46 PM](https://global.discourse-cdn.com/free1/uploads/orgroam/original/1X/63a28a407b4ab154a03ca3e361e4677fda8f2f54.gif)

I’ve searched around my `elpa` dir and the only place I can find the string `Which function for` is inside `org-plus-contrib-20200928/ol.elc`.

Here’s the `org-ref` and `org-roam` related bits of my config, in case it helps:

```lisp
(use-package org-ref
  :defer nil
  :config
  (setq org-ref-bibiliography-notes '("~/org/notebook/")
        org-ref-default-bibliography '("~/bib/apcmaster.bib")
        org-ref-pdf-directory "~/Document/Papers")
  )

;;;;;;; Org-roam and (Deft)

(use-package org-roam
  :hook
  (after-init . org-roam-mode)
  :custom
  (make-directory "~/org/notebook")
  (org-roam-directory "~/org/notebook")
  (org-roam-dailies-capture-templates '(("d" "daily" plain (function org-roam-capture--get-point) ""
                                         :immediate-finish t
                                         :file-name "dailies/%<%Y-%m-%d>"
                                         :head "#+TITLE: %<%Y-%m-%d>")))
  (org-roam-capture-templates
      '(("d" "default" plain (function org-roam--capture-get-point)
         "%?"
         :file-name "%<%Y%m%d%H%M%S>"
         :head "#+TITLE: ${title}\n"
         :unnarrowed t)))
  :bind (:map org-roam-mode-map
              (("C-c n l" . org-roam)
               ("C-c n r" . org-roam-find-ref)
               ("C-c n g" . org-roam-show-graph)
               ("C-c n i" . org-roam-insert)
               ("C-c n I" . org-roam-insert-immediate))
              :map org-mode-map
              (("C-c n c" . org-roam-capture)
               ("C-c n f" . org-roam-find-file)))
  )

(use-package org-roam-bibtex
  :after org-roam
  :hook (org-roam-mode . org-roam-bibtex-mode)
  :bind (:map org-mode-map
              (("C-c n a" . orb-note-actions))))

```

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [September 29, 2020, 7:07am UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/4 "2020-09-29T07:07:44Z")

</div>

> [@apc](#):
>
> ```auto
> (use-package org-ref
> :defer nil
> 
> ```

This must be it. Org Ref is a tricky package to load properly. With `:defer nil` you load it immediately upon Emacs startup. Org Ref requires Org and thus when it is loaded too early both the built-in Org shipped with Emacs and a newer Melpa version are loaded simultaneously leading to all kinds of issues.

Try this:

```elisp
(use-package org-ref
  :after org
  ...)

```

Or this:

```elisp
(use-package org-ref
  :hook 
  (org-load . (lambda () (require 'org-ref))  
  ...)

```

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [September 29, 2020, 7:10am UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/5 "2020-09-29T07:10:55Z")

</div>

Note that if you compile your init file or your Emacs configuration framework such as Doom or Spacemacs does it, you will need to recompile it. Recompilation/reinstallation of Org mode, Org Ref and other Org-related packages may also be required.

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [September 29, 2020, 7:18am UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/6 "2020-09-29T07:18:27Z")

</div>

To be on the safe side, you should also load Org Roam after Org:

```auto
(use-package org-roam
  :hook
  (org-load . org-roam-mode)
  ...)

```

---

<div class="post-metadata">

**Author:** ![apc](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@apc](https://org-roam.discourse.group/u/apc)\
**Post date:** [September 29, 2020, 1:50pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/7 "2020-09-29T13:50:50Z")

</div>

Thanks again for your help. Unfortunately, this isn’t making much of a difference. I basically replaced my `org-roam-bibtex` related config with this, following your advice:

```lisp

;;;;;;; Org-ref

(use-package org-ref
  :hook
  (org-load . (lambda () (require 'org-ref)))
  :config
  (setq org-ref-bibiliography-notes '("~/org/notebook/")
        org-ref-default-bibliography '("~/bib/apcmaster.bib")
        org-ref-pdf-directory "~/Document/Papers")
  ;; (org-ref-completion-library 'org-ref-ivy-cite-completion)
  )

;;;;;;; Org-roam and (Deft)

(use-package org-roam
  :hook
  (org-load . org-roam-mode)
  :custom
  (make-directory "~/org/notebook")
  (org-roam-directory "~/org/notebook")
  (org-roam-dailies-capture-templates '(("d" "daily" plain (function org-roam-capture--get-point) ""
                                         :immediate-finish t
                                         :file-name "dailies/%<%Y-%m-%d>"
                                         :head "#+TITLE: %<%Y-%m-%d>")))
  (org-roam-capture-templates
      '(("d" "default" plain (function org-roam--capture-get-point)
         "%?"
         :file-name "%<%Y%m%d%H%M%S>"
         :head "#+TITLE: ${title}\n"
         :unnarrowed t)))
         ;; :immediate-finish t)))
  :bind (:map org-roam-mode-map
              (("C-c n l" . org-roam)
               ("C-c n r" . org-roam-find-ref)
               ("C-c n g" . org-roam-show-graph)
               ("C-c n i" . org-roam-insert)
               ("C-c n I" . org-roam-insert-immediate))
              :map org-mode-map
              (("C-c n c" . org-roam-capture)
               ("C-c n f" . org-roam-find-file)))
  )

(use-package org-roam-bibtex
  :after org-roam
  ;; :hook org-roam-mode;; This should work instead of the code below, but it just doesn't. Weird.
  :hook (org-roam-mode . org-roam-bibtex-mode)
  :bind (:map org-mode-map
              (("C-c n a" . orb-note-actions))))

(use-package deft
      :after org
      :bind
      (:map org-mode-map ("C-c n d" . deft))
      :custom
      (deft-recursive t)
      (deft-use-filter-string-for-filename t)
      (deft-default-extension "org")
      (deft-directory "~/org/notebook")
      (deft-strip-summary-regexp "\\([\n]\\|^#\\+[[:upper:][:lower:]_]+:.*$\\)"))

```

To be safe, I then deleted everything inside my `elpa` directory so that all packages would be reinstalled. I still get the same behavior.

---

<div class="post-metadata">

**Author:** ![apc](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@apc](https://org-roam.discourse.group/u/apc)\
**Post date:** [September 29, 2020, 2:33pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/8 "2020-09-29T14:33:52Z")

</div>

I should add that I did try to step through `org-ref-open-bibtex-notes`, but not knowing much about how to work with the debugger, I’m not sure if what I am pasting below is of much help.

```lisp
Debugger entered--returning value: "[[file:~/Library/texmf/bibtex/bib/apc/apcmaster.bi..."
  org-store-link(nil)
  org-capture(nil "r")
  org-roam-capture--capture()
  orb-edit-notes("joyce2018")
  orb-notes-fn("joyce2018")
  #f(compiled-function () (interactive nil) #<bytecode 0x1fec9df59e61>)()
  apply(#f(compiled-function () (interactive nil) #<bytecode 0x1fec9df59e61>) nil)
* org-ref-open-bibtex-notes()
  funcall-interactively(org-ref-open-bibtex-notes)
  call-interactively(org-ref-open-bibtex-notes record nil)
  command-execute(org-ref-open-bibtex-notes record)
  counsel-M-x-action("org-ref-open-bibtex-notes")

```

It seems that `org-store-link` is being called by `org-capture`, which is being called by `org-roam-capture--capture`. It’s only when `org-store-link` is being called, it seems, that I’m prompted to answer the question “Which function…”.

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [September 30, 2020, 9:01am UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/9 "2020-09-30T09:01:34Z")

</div>

> [@apc](#):
>
> I’m not sure if what I am pasting below is of much help.

It helps a lot actually. Your system is working properly and behaving exactly as you told it to 🙂

The following is happening. `orb-notes-fn` does its part and happily delegates creation of your note to `org-capture`. Org capture is a complex process. Not only it captures what you explicitly told it to do, but also as much of the context as it can — according to its specification. Among those captured things is an Org-style link to the location context you initiated the `org-capture` process from. The BibTeX file in your particular case. Different location contexts have different functions for creating Org-style links associated with them, and many external packages define their own functions for their specific tasks. Usually, there is no more than one function for each context. In your runtime environment, however, there are two alternative functions, `org-ref-bibtex-store-link` and `org-bibtex-store-link`, that can be used to create an Org link to a BibTeX file. The former comes from the Org Ref package, the latter is defined in the `ol-bibtex` Org module. Having two functions available, `org-capture` cannot decide on its own which one to use and asks you to make the choice.

There are several possibilities to fix this behaviour:

1. Don’t use the `ol-bibtex` module (`org-ref` I guess is out of question)
2. Keep `ol-bibtex` and `org-bibtex-store-link`, advise the `org-ref-generate-cite-links` function not to call `org-link-set-parameters`. This should be done before `org-ref` is loaded.
3. Advise or re-write `orb-edit-fn` so that it temporarily sets `org-capture-link-is-already-stored` to t. This must be done after `org-roam-bibtex` is loaded:

```elisp
(defun orb-edit-fn (citekey)
  (when (require 'org-ref nil t)
    (let ((bibtex-completion-bibliography (org-ref-find-bibliography))
           ;; trick `org-capture` to believe the link is already stored
           (org-capture-link-is-already-stored t) ) 
      (orb-edit-notes citekey)))

```

Note that in this case you won’t be able to meaningfully use the “annotation” feature (`%a` and `%A` wildcards) of `org-capture` in your `orb-templates` because it will always return nil.

Edit:  
4. According to how `org-store-link` works, you can also redefine either `org-ref-bitex-store-link` or `org-bibtex-store-link` to return nil:

```elisp
;; keep `org-ref-bibtex-store-link'
(defun org-bibtex-store-link () nil)

;; keep `org-bibtex-store-link'
(defun org-ref-bibtex-store-link () nil)

```

The other function will then be used automatically.

---

<div class="post-metadata">

**Author:** ![apc](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@apc](https://org-roam.discourse.group/u/apc)\
**Post date:** [September 30, 2020, 1:28pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/10 "2020-09-30T13:28:32Z")

</div>

Thank you! This is incredibly helpful.

I’ll go with option 4 for now, since I don’t know how to avoid loading `ol-bibtex` short of not loading `org-plus-contrib` (if you _do_ know off the top of your head, I’d love to know!). I don’t use `org-bibtex-store-link` anywhere else AFAIK, so that should fix things.

Again, many thanks for your patience and your help!

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [September 30, 2020, 4:41pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/11 "2020-09-30T16:41:44Z")

</div>

Ι’m glad to help.

It depends on how you load `org-plus-contrib`. It’s not a package as such but rather a collection of small packages. As far as I know, you must explicitly load its files one by one by setting the `org-modules` variable or simply `(require ...)`. Could it be possible that you have `org-modules` loading `ol-bibtex` somewhere in your config?

---

<div class="post-metadata">

**Author:** ![apc](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@apc](https://org-roam.discourse.group/u/apc)\
**Post date:** [September 30, 2020, 7:51pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/12 "2020-09-30T19:51:59Z")

</div>

I’m using `use-package` and have `:ensure org-plus-contrib` in my `org` declaration.

I thought that was what was loading `ol-bibtex`, since I don’t call it anywhere else on my config. But I just removed that line, manually removed `org-plus-contrib` from `package-selected-packages`, restarted Emacs, and `C-h v` for `org-modules` still includes `ol-bibtex` (together with a bunch of other stuff). I will try just dumping everything inside my `elpa` directory and try downloading all packages from scratch.

**EDIT** : I did just that (i.e. deleted everything inside `elpa/`) and I still have `org-modules` include `ol-bibtex`. I searched through my `elpa/` and discovered that `doi-utils.el` (which comes as part of `org-ref`) has the following at line 60:

```lisp
(or (require 'ol-bibtex nil t)
    (require 'org-bibtex)) ; org-bibtex-yank

```

Could this be the culprit?

Can I just manually remove `ol-bibtex` from the `org-modules` list? (Is there something that does the opposite of `add-to-list`?)

Thanks again!

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [September 30, 2020, 9:33pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/13 "2020-09-30T21:33:34Z")

</div>

> [@apc](#):
>
> I searched through my `elpa/` and discovered that `doi-utils.el` (which comes as part of `org-ref` ) has the following at line 60:
> 
> ```auto
> (or (require 'ol-bibtex nil t)
> (require 'org-bibtex)) ; org-bibtex-yank
> 
> ```

Ahaha, that’s why I call `org-ref` a tricky package. Yes, this is it.

I actually can confirm it on my system as well. I must apologize because I obviously didn’t try to reproduce the issue thoroughly enough - I just ran the `org-ref-open-bibtex-notes` and after the filename prompt appeared, assumed I didn’t run into it.

If you have `ol-bibtex` in `org-modules`, it is there by default actually, you must remove it before loading Org like this:

```elisp
(use-package 'org
  :init
  (setq org-modules (remove 'ol-bibtex org-modules))
  ...)

```

But this will not solve the issue because you have to deal with `org-ref` also requiring `ol-bibtex`.

Let me think it out.

> [@apc](#):
>
> I will try just dumping everything inside my `elpa` directory and try downloading all packages from scratch.

That isn’t really necessary. Your system is fine.

---

<div class="post-metadata">

**Author:** ![apc](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@apc](https://org-roam.discourse.group/u/apc)\
**Post date:** [October 2, 2020, 1:27pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/14 "2020-10-02T13:27:16Z")

</div>

Thanks! Could I place `org-ref-open-bibtex-notes` in a wrapper that uses `let` to temporarily set `org-bibtex-store-link` to `nil`? Seems like this would cause the least amount of interference with the functionality of `org-ref`…

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [October 2, 2020, 4:31pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/15 "2020-10-02T16:31:57Z")

</div>

> [@apc](#):
>
> hanks! Could I place `org-ref-open-bibtex-notes` in a wrapper that uses `let` to temporarily set `org-bibtex-store-link` to `nil` ? Seems like this would cause the least amount of interference with the functionality of `org-ref` …

You can temporarily set to nil a variable, but `org-bibtex-store-link` is a function, so you must either redefine it or advise.

Advising is more flexible:

```elisp

;; define a dummy function
(defun org-bibtex-store-link-return-nil ()
"Advice for `org-bibtex-store-link' to do nothing and return nil"
nil)

;; advise `org-bibtex-store-link to be overriden by `org-bibtex-store-link-return-nil
(advice-add #'org-bibtex-store-link
  :override #'org-bibtex-store-link-return-nil)

;; to remove the advice evaluate this:
(advice-remove #'org-bibtex-store-link
     :override #'org-bibtex-store-link-return-nil)

```

Since the issue also affects ORB users, and ORB positions itself to provide a smooth integration between BibTeX and note taking, I’m now considering to handle this in ORB. But it will have to wait a couple of days.

---

<div class="post-metadata">

**Author:** ![apc](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@apc](https://org-roam.discourse.group/u/apc)\
**Post date:** [October 3, 2020, 1:26am UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/16 "2020-10-03T01:26:14Z")

</div>

Great! Thanks again. I’ll stay tuned to see what you come up with.

---

<div class="post-metadata">

**Author:** ![mshevchuk](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mshevchuk](https://org-roam.discourse.group/u/mshevchuk)\
**Post date:** [October 3, 2020, 8:54pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/17 "2020-10-03T20:54:52Z")

</div>

@apc, you can now upgrade to the latest master. ORB handles this situation by default. You can set functions that you want to suppress in `orb-ignore-bibtex-store-link-functions` (`org-bibtex-store-link` is suppressed by default).

---

<div class="post-metadata">

**Author:** ![apc](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@apc](https://org-roam.discourse.group/u/apc)\
**Post date:** [October 4, 2020, 6:30pm UTC](https://org-roam.discourse.group/t/calling-org-ref-open-bibtex-notes-triggers-which-function-for-creating-the-link-question/806/18 "2020-10-04T18:30:52Z")

</div>

Thanks for the update! Works great so far!
