3

My emacs daemon for ocaml (I use more than one emacs daemon) starts in a tuareg scratch buffer. I am aware of initial-major-mode but this hasn't worked: the scratch is started before (setqinitial-major-mode 'tuareg-mode)` is evaluated.

Since I keep the initial-buffer-choice to scratch, I just run (tuareg-mode) and my scratch mode is modified as I want.

But now, when I kill the scratch, new scratch buffers are not in tuareg mode, so I added a little hack found on stackexchange:

(defun prepare-scratch-for-kill ()
  (save-excursion
    (set-buffer (get-buffer-create "*scratch*"))
    (tuareg-mode)
    (add-hook 'kill-buffer-query-functions 'kill-scratch-buffer t)))

(defun kill-scratch-buffer ()
  (let (kill-buffer-query-functions)
    (kill-buffer (current-buffer)))
  ;; no way, *scratch* shall live
  (prepare-scratch-for-kill)
  ;; Since we "killed" it, don't let caller try too
  nil)

(prepare-scratch-for-kill)

It works, but now I don't see my magnificent initial-scratch-message. I have tried to (setq initial-major-mode 'tuareg-mode), which normally modifies the scratch major mode. But it doesn't, even if I start a new scratch.

I would like to do it the proper way, emacs embeded way, but what I have tried doesn't work.

5
  • If you haven't done so already, one thing that you should make sure of is that tuareg-mode is loaded before you set initial-major-mode. I originally followed the instructions here to do what you want to do, emacsredux.com/blog/2014/07/25/… but it sounds like you have already tried these steps
    – elethan
    Commented Dec 23, 2015 at 18:46
  • All right it was as simple as that ! Thank you ! Commented Dec 23, 2015 at 18:55
  • @elethan Since it seems to have answered the question, can you convert this comment into an answer so it can be accepted?
    – erikstokes
    Commented Dec 24, 2015 at 2:44
  • @NicolasScottoDiPerto could you provide a few details about what you changed in your configuration to get things to work the way you wanted? If I haven't mentioned it in my answer already, then I can include those details for future readers. Thanks!
    – elethan
    Commented Dec 24, 2015 at 4:26
  • Sure ! I install and configure packages with the package (this sound like an inception xD) use-package. It's good because it can help yout emacs init faster by loading mode only when it's needed instead of loading all modes at startup which can takes so times. The suggestion of elethan is that maybe my mode wasn't loaded before setting it as the scratch default mode, so the line before (setq initial-major-mode 'tuareg-mode) i've added (require 'tuareg) and it works ! Thank you elethan ! Commented Dec 25, 2015 at 10:52

1 Answer 1

5

My original suggestion to the OP based on my own slightly misguided experimentation was this:

make sure that the mode you want to set as the initial-major-mode is loaded before you set initial-major-mode with (setq initial-major-mode 'name-of-mode). This seems to have solved the problem that the OP was having.

However, I have also tested changing my entire init.el to contain only the lines:

(setq initial-major-mode 'tuareg-mode)
(setq inhibit-startup-screen t)

and literally nothing else, and when I started emacs I got a *scratch* buffer in tuareg-mode.

This leads me to believe that something else was causing the issue, but I am happy that my suggestion could lead to a solution one way or another. Also, in my case tuareg-modewas installed through MELPA and would have automatically been added to the load-path, which might not have been the case for the OP, and this could be why I have no issue with the above configuration.

Emacs Redux also has this short, useful tutorial on how to solve the problem in question.

In general, I think setting initial-major-mode as described above in your ~/.emacs or init.el or whatever emacs config you use should be enough to set a default mode for your scratch buffer.

However, if you find that this doesn't work as was the case for the OP, it won't hurt to ensure that initial-major-mode is set to the desired mode after the package that contains that mode gets initialized. If setting initial-major-mode still doesn't work as expected, you can follow the advice in this accepted answer and check that the value has not been set in customize with M-x customize-option RET initial-major-mode RET. From here you can not only see what value this variable has been set to, but also change the value.

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.