1

I recently updated my org-roam package to version 2.2.2 with the following steps:

  1. M-x list-packages
  2. Then select org-roam and clicked delete.
  3. M-x list-packages again
  4. Then select org-roam and clicked install.

I think org-roam successfully installed (as I can see in my directories the following: .emacs.d/elpa/org-roam-2.2.2).

Now if I started Emacs: **scratch** buffer dominates everything. Even if I did emacs /path/to/file/filename it would open **scratch** buffer on top of my file. Now I can C-x b to my file, but that was not the default behavior earlier!

I found this related issue on stackoverflow: Opening emacs without the scratch buffer when opening a file. Following this answer to the question I ran emacs -q /path/to/file/filename and everything worked as intended.

So upon further investigation I found this as the offending snippet in my .emacs:

(use-package org-roam
      :ensure t
      :hook
      (after-init . org-roam-mode)
      :custom
      (org-roam-directory "/$HOME/Zettelkasten")
      :bind (:map org-roam-mode-map
              (("C-c n l" . org-roam)
               ("C-c n f" . org-roam-find-file)
               ("C-c n r" . org-roam-graph))
              :map org-mode-map
              (("C-c n i" . org-roam-insert))
              (("C-c n I" . org-roam-insert-immediate))))
(add-hook 'after-init-hook 'org-roam-mode)

If I comment out the above code, then everything works fine (except obviously org-roam doesn't load and I have to manually load it later).

So questions:

  1. What is wrong with my above code snippet? Why is it causing **scratch** to open over my intended file? Earlier **scratch** would open but it was not the front buffer. I could always C-x b to **scratch** if needed.
  2. How to fix my code snippet?

EDIT: For now, I am applying the following aggressive hot-fix:

;; Makes *scratch* empty.
(setq initial-scratch-message "")

;; Removes *scratch* from buffer after the mode has been set.
(defun remove-scratch-buffer ()
  (if (get-buffer "*scratch*")
      (kill-buffer "*scratch*")))
(add-hook 'after-change-major-mode-hook 'remove-scratch-buffer)

;; Removes *messages* from the buffer.
(setq-default message-log-max nil)
(kill-buffer "*Messages*")

But I like to keep an instance of **scratch** running in the background. So I don't like the above hot-fix at all.

14
  • 1
    Why are you adding to after-init-hook? Try deleting that bit only and see if that improves matters.
    – NickD
    Sep 24, 2022 at 14:11
  • If I leave (add-hook 'after-init-hook 'org-roam-mode) as commented out, and the rest of the above as uncommented, the problem persists. So I don't think it is about the after-init-hook part. Sep 24, 2022 at 14:42
  • 3
    Nevertheless, I think it is wrong to alter after-init-hook this way: the buffer that is current at the end of initialization will have org-roam-mode run on it - you may have arranged that that buffer is one that should be in org-roam-mode but that's an error-prone way to do it. Why not depend on auto-mode-alist?
    – NickD
    Sep 24, 2022 at 14:58
  • 1
    ... and I think you should get rid of the setting in the body of use-package as well.
    – NickD
    Sep 24, 2022 at 15:06
  • 2
    You do the after-init-hook thing twice: once in the use-package form (the bit after :hook) and once at the end. Have you commented both out? Sep 24, 2022 at 15:06

1 Answer 1

3

The problem is that you set after-init-hook (in fact, you do so twice: once in the use-package form and once after it. This has the effect of setting whatever the initial buffer is to org-roam-mode which is almost certainly not what you want.

Remove those bits and you end up with something that works:

(use-package org-roam
      :ensure t
      :custom
      (org-roam-directory "/$HOME/Zettelkasten")
      :bind (:map org-roam-mode-map
              (("C-c n l" . org-roam)
               ("C-c n f" . org-roam-find-file)
               ("C-c n r" . org-roam-graph))
              :map org-mode-map
              (("C-c n i" . org-roam-insert))
              (("C-c n I" . org-roam-insert-immediate))))
5
  • Sorry I may have to edit my post/ original question to better reflect the situation. It was not the after-init-hook that is the real problem. The real problem is M-x org-roam doesn't work! I realized it only a little while ago. (I have commented about it above as well). I just opened a github issue on org-roam based on our earlier discussions and what I now know. Sep 24, 2022 at 18:28
  • 1
    I suggest you open a new question about this and leave the current one as is. Sep 24, 2022 at 18:49
  • 1
    Well, true... I also feel your answer solves my original question - it will stop the scratch buffer from dominating other buffers when opening files. I also understand why using after-init-hook may not be good practice. It will always set every buffer to org-roam mode and that is not the correct thing to do. So I will accept your answer for this question. Sep 24, 2022 at 19:07
  • For anyone else struggling with org-roam version 2.2.2, please note: really after-init-hook is not the heart of the problem. I have already started a github issue here. So I am not going to make a new emacs.stackexchange post on this same issue. Please proceed over to the github issue. Sep 24, 2022 at 19:10
  • If I were you, I would prepare a minimal init file (just the use-package perhaps) and then start emacs with emacs -q -l /path/to/minimal-init.el, bypassing your init file altogether. It is likely that the org-roam people will ask you to do exactly that if they cannot reproduce the problem, so you might as well try it.
    – NickD
    Sep 24, 2022 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.