2

For C/C++, I have the following to be run when I do M-x compile on a .c/.cpp file

(add-hook 'c++-mode-hook
          (lambda ()
            (set (make-local-variable 'compile-command)
                 (concat "g++ " buffer-file-name " && ./a.out"  ) )))

Now, say I work on some other languages that do not have mode-hook, say R. I want to do something like this:

(add-hook 'r-mode-hook
          (lambda ()
            (set (make-local-variable 'compile-command)
                 (concat "Rscript " buffer-file-name ) )))

After putting above code in my init.el and reloading emacs, there are no errors on startup. I went ahead and open and .r file, but M-x compile does not result in: Rscript . Instead, I got the default prompt from M-x compile: make -k.

Is there a way to do this?

4
  • 1
    Why do you believe that r-mode does not have a mode hook? Do you have a link to the source code for r-mode so that we can have a look for ourselves to see whether that is accurate, and/or to make specific suggestions based upon our review of that source code?
    – lawlist
    Commented Jan 30, 2020 at 5:47
  • All major modes defined with define-derived-mode have a mode hook (foo-mode-hook if the mode is named foo-mode), and any major modes defined without that macro are expected to do the same thing. As such, the problem probably isn't what you think it is. I don't have an r-mode so, as @lawlist says, please show us the source for that.
    – phils
    Commented Jan 30, 2020 at 6:15
  • Failing anything else, you could always use after-change-major-mode-hook and (when (derived-mode-p 'r-mode) ...) in your hook function.
    – phils
    Commented Jan 30, 2020 at 6:22
  • It would also be worth checking the value of C-h v major-mode in one of your .r file buffers.
    – phils
    Commented Jan 30, 2020 at 6:23

1 Answer 1

5

As far as I know, there's no r-mode in Emacs. Instead, you're probably using ESS's support for R, whose major mode is called ess-r-mode IIRC, so you'd want to use ess-r-mode-hook. But as @phils said: check the value of major-mode to be sure.

1
  • 1
    And most likely «C-h m» will reveal the name of the hook(s) as well
    – Rusi
    Commented Jan 31, 2020 at 5:03

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.