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?

Drew
  • 75,699
  • 9
  • 109
  • 225
mle0312
  • 295
  • 1
  • 8
  • 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 Jan 30 '20 at 05: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 Jan 30 '20 at 06: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 Jan 30 '20 at 06:22
  • It would also be worth checking the value of `C-h v major-mode` in one of your `.r` file buffers. – phils Jan 30 '20 at 06:23

1 Answers1

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.

Stefan
  • 26,154
  • 3
  • 46
  • 84