nobiot/md-roam

Not able to complete/insert links in markdown files

ehamberg opened this issue · 8 comments

I'm trying to use md-roam with Doom Emacs. org with +roam have been enabled in init.el and seems to work well. I have added the following to my packages.el for md-roam:

(package! md-roam
  :recipe (:host github :repo "nobiot/md-roam"))

… and the following to my config.el:

(use-package! md-roam
  :after org-roam
  :config
  (setq org-roam-file-extensions '("org" "md"))
  (md-roam-mode 1)
  (org-roam-db-autosync-mode 1)
  (add-to-list 'org-roam-capture-templates
               '("m" "Markdown" plain "" :target
                 (file+head "%<%Y-%m-%dT%H%M%S>-${slug}.md"
                            "---\ntitle: ${title}\nid: %<%Y-%m-%dT%H%M%S>\ncategory: \n---\n")
                 :unnarrowed t))
  )

I then created the following file using org-roam-node-find and the “Markdown” template:

---
title: md-roam
id: 2021-11-29T092121
category: #emacs
---

hello, md-roam. 

So far, so good. The file is indexed and findable, and I can link to it from existing .org files. However, when I attempt to insert a link in the markdown file by typing [[Emac, I don't get any completion like I get in .org files. If I leave it like [[Emacs]], I get the following error when I save the file:

Warning (emacs): org-element--cache: Cache corruption detected in 2021-11-29T092121-md_roam.md. Resetting.
 The error was: (error "rx ‘**’ range error")
 Backtrace:
nil
 Please report this to Org mode mailing list (M-x org-submit-bug-report).
Error (before-save-hook): Error running hook "org-roam-link-replace-all" because: (error rx ‘**’ range error)

If I don't type [[, but just type Emac and then use completion-at-point, [[roam:Emacs]] gets inserted, and I get this error:

Warning (emacs): org-element--cache: Cache corruption detected in 2021-11-29T092121-md_roam.md. Resetting.
 The error was: (error "rx ‘**’ range error")
 Backtrace:
"  backtrace-to-string(nil)
  org-element-at-point(nil)
  org-element-context()
  org-roam-link-replace-at-point()
  org-roam-link-replace-all()
  funcall(org-roam-link-replace-all)
  (condition-case e (funcall hook) ((debug error) (signal 'doom-hook-error (list hook e))))
  doom-run-hook(org-roam-link-replace-all)
  run-hook-wrapped(doom-run-hook org-roam-link-replace-all)
  (condition-case e (run-hook-wrapped hook #'doom-run-hook) ((debug doom-hook-error) (if debug-on-error nil (lwarn hook :error \"Error running hook %S because: %s\" (if (symbolp (car (cdr e))) (symbol-name (car (cdr e))) (car (cdr e))) (car (cdr (cdr e))))) (signal 'doom-hook-error (cons hook (cdr e)))))
  (let ((hook (car --dolist-tail--))) (condition-case e (run-hook-wrapped hook #'doom-run-hook) ((debug doom-hook-error) (if debug-on-error nil (lwarn hook :error \"Error running hook %S because: %s\" (if (symbolp (car ...)) (symbol-name (car ...)) (car (cdr e))) (car (cdr (cdr e))))) (signal 'doom-hook-error (cons hook (cdr e))))) (setq --dolist-tail-- (cdr --dolist-tail--)))
  (while --dolist-tail-- (let ((hook (car --dolist-tail--))) (condition-case e (run-hook-wrapped hook #'doom-run-hook) ((debug doom-hook-error) (if debug-on-error nil (lwarn hook :error \"Error running hook %S because: %s\" (if (symbolp ...) (symbol-name ...) (car ...)) (car (cdr ...)))) (signal 'doom-hook-error (cons hook (cdr e))))) (setq --dolist-tail-- (cdr --dolist-tail--))))
  (let ((--dolist-tail-- hooks)) (while --dolist-tail-- (let ((hook (car --dolist-tail--))) (condition-case e (run-hook-wrapped hook #'doom-run-hook) ((debug doom-hook-error) (if debug-on-error nil (lwarn hook :error \"Error running hook %S because: %s\" (if ... ... ...) (car ...))) (signal 'doom-hook-error (cons hook (cdr e))))) (setq --dolist-tail-- (cdr --dolist-tail--)))))
  doom-run-hooks(before-save-hook)
  apply(doom-run-hooks before-save-hook)
  run-hooks(before-save-hook)
  basic-save-buffer(nil)
  save-buffer()
  evil-write(nil nil nil nil nil)
  funcall-interactively(evil-write nil nil nil nil nil)
  call-interactively(evil-write)
  evil-ex-call-command(nil #(\"w\" 0 1 (ex-index 1)) nil)
  eval((evil-ex-call-command nil #(\"w\" 0 1 (ex-index 1)) nil))
  evil-ex-execute(#(\"w\" 0 1 (ex-index 1)))
  evil-ex(nil)
  funcall-interactively(evil-ex nil)
  call-interactively(evil-ex nil nil)
  command-execute(evil-ex)
"
 Please report this to Org mode mailing list (M-x org-submit-bug-report).
Error (before-save-hook): Error running hook "org-roam-link-replace-all" because: (error rx ‘**’ range error)

I'm not a very experience Emacs user, so it could be something very rudimentary. I'm happy to hear if someone knows how to fix this.

There are many layers to this issue; we need to unpack them.

First, is your Org-roam up-to-date?
Do you update packages via doom sync?
I don't use Doom but I have seen many people not updating, or somehow doom sync does not update correctly so the old version is still used. I have seen people having to delete Org-roam folder entirely to get Doom to use new version...

Second:

If I don't type [[, but just type Emac and then use completion-at-point, [[roam:Emacs]] gets inserted, and I get this error:

Do you get the same error when Md-roam is not active? That is only with Org-roam?
Thee error to me looks like Org-roam error (I don't md-roam appearing in the warning/error message).

Let's start with these two.

First, is your Org-roam up-to-date? Do you update packages via doom sync? I don't use Doom but I have seen many people not updating, or somehow doom sync does not update correctly so the old version is still used. I have seen people having to delete Org-roam folder entirely to get Doom to use new version...

I have synced and even rebuilt the packages.
org-roam-version returns a2e46db which seems to be 9 days old. md-roam is at 434e33d which, as of now, is the newest commit.

Do you get the same error when Md-roam is not active? That is only with Org-roam? Thee error to me looks like Org-roam error (I don't md-roam appearing in the warning/error message).

If I disable md-roam, open an existing node (a .org file) and type “[[Emac” I get completion suggestions, and when I choose “Emacs” [[id:e50032fd-fc65-448c-ac5b-3fd4a6853af5][Emacs]] gets inserted in the buffer (shown as a link). Saving the file produces no errors, and the “Emacs” notes shows the new backlink.

If I type “Emac” and then run completion-at-point in the .org file, I get the same result (link, saving works, backlink is shown in Emacs node).

(After re-enbling md-roam, my .md file is indexed findable again, but I get the same errors as above when attempting to insert a link.)

Thank you. Okay, I can't reproduce the issue (I have no problem) and so it may be specific to Doom or the "cutting edge" Org version that Doom brings (v9.6, which is not released).

I can't solve it properly now, but if you turn this variable off org-roam-link-auto-replace, I think you won't have this issue on Md-roam. You may start having the issue with Org-roam, instead...

As to this issue:

However, when I attempt to insert a link in the markdown file by typing [[Emac, I don't get any completion like I get in .org files.

I'm not sure but perhaps you need to explicitly turn on Company mode in Markdown files (?).

Let me know how you go.

I'll work on a proper fix later (today or some day this week/weekend hopefully).

Thank you so much, @nobiot.

I can't solve it properly now, but if you turn this variable off org-roam-link-auto-replace, I think you won't have this issue on md-roam. You may start having the issue with Org-roam, instead…

That works, or at least I no longer get the error, and the link from the .md file is shown in the list of backlinks for the .org file I link to.

I'm not sure but perhaps you need to explicitly turn on Company mode in Markdown files (?).

Company is listed when I describe-mode. Strange. 🤔

I'll work on a proper fix later (today or some day this week/weekend hopefully).

That would be amazing, but I want to make it clear that I don't expect to trample in here with my custom Emacs setup and have you fix my issues. ☺️ Hopefully I'll be able to do more debugging once I learn more Emacs and elisp.

e6d64a2 should fix the issue of error/warning message without the need for you to change variable org-roam-link-auto-replace.

For your issue of Company not working, what's the value of org-roam-completion-functions when you are in a markdown file in Md-roam/Org-roam directory?

It should be as follows -- the order is important with the md-roam one at the end of the list.

(org-roam-complete-link-at-point org-roam-complete-everywhere md-roam-complete-wiki-link-at-point)

e6d64a2 should fix the issue of error/warning message without the need for you to change variable org-roam-link-auto-replace.

Thanks! That fixed the error I was getting.

For your issue of Company not working, what's the value of org-roam-completion-functions when you are in a markdown file in Md-roam/Org-roam directory?

Its value is (org-roam-complete-link-at-point org-roam-complete-everywhere md-roam-complete-wiki-link-at-point) and its original Value is (org-roam-complete-link-at-point org-roam-complete-everywhere), which seems correct.

(If I open an org-roam .org file, the value is the same.)

Here's how the completion looks in a .md file:

md-roam

… and here's how it looks in a .org file:

org-roam

Okay, then it's probably the configuration for company-backends

Can you have a look its value? It should be a list, and should contain company-capf but probably doesn't in your case for some reason.

Doom has this line for the org module. I don't see anything similar for the markdown module.

Maybe you can try this in your mardown mode, and if it fixes the problem add it to your markdown-mode configuration.

(set-company-backend! 'company-capf)

Okay, then it's probably the configuration for company-backends

Can you have a look its value? It should be a list, and should contain company-capf but probably doesn't in your case for some reason.

Correct.

Doom has this line for the org module. I don't see anything similar for the markdown module.

Maybe you can try this in your mardown mode, and if it fixes the problem add it to your markdown-mode configuration.

That solved it! I added (set-company-backend! 'markdown-mode 'company-capf) to my config and now everything seems to work. Thank you so much!

The updated config:

(use-package! md-roam
  :after org-roam
  :config
  (set-company-backend! 'markdown-mode 'company-capf) ; add company-capf as company backend in markdown buffers
  (setq org-roam-file-extensions '("org" "md")) ; enable Org-roam for a markdown files
  (md-roam-mode 1) ; md-roam-mode needs to be active before org-roam-db-sync
  (org-roam-db-autosync-mode 1)
  (add-to-list 'org-roam-capture-templates
               '("m" "Markdown" plain "" :target
                 (file+head "%<%Y-%m-%dT%H%M%S>-${slug}.md"
                            "---\ntitle: ${title}\nid: %<%Y-%m-%dT%H%M%S>\ncategory: \n---\n")
                 :unnarrowed t)))