0

In my code I have bindings for two reformatters that are supposed to be applied upon saving relevant code:

(add-hook 'tuareg-mode-hook (lambda ()
    (add-hook 'before-save-hook 'ocp-indent-buffer
    (merlin-mode))))

(add-hook 'reason-mode-hook (lambda ()
                (add-hook 'before-save-hook 'refmt-before-save)
                (merlin-mode)))

Yet for some reason my code is getting formatted weirdly at times (and not just the OCaml/ReasonML, my Emacs Lisp code can get formatted really weirdly. I'm not sure what triggers the weird formatting; usually it's after I delete something or paste something in.

Here is an example of normal formatting: enter image description here

And here is an example of the weird formatting: enter image description here

I wish I had the braincells to save the message log so I could post it here but I forgot to. It happens every few hours; if it happens again I will post my messages buffer here.

1

Please always indent your Emacs Lisp code correctly, you can use C-M-q (indent-pp-sexp) to do that, then you should be able to notice you have:

(add-hook 'before-save-hook 'ocp-indent-buffer
          (merlin-mode))

but I guess what you want is this instead:

(add-hook 'before-save-hook 'ocp-indent-buffer)
(merlin-mode)

As for you question, it is because you are changing before-save-hook's global value, thus whenever you save a buffer, ocp-indent-buffer will be executed, it doesn't matter which Major mode you are currently in. It appears that you want to run ocp-indent-buffer on saving only OCaml buffers, you should change before-save-hook's buffer-local value in OCaml buffers by setting add-hook's fourth argument LOCAL to non-nil:

(add-hook
 'tuareg-mode-hook
 (lambda ()
   (add-hook 'before-save-hook 'ocp-indent-buffer nil t)
   (merlin-mode)))
  • Thank you xuchunyang! I knew I did something stupid. I wish I could upvote your post. That is exactly what I was looking for. – kuwze Mar 2 '18 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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