0

All I want to do is enable tern-mode, when I enter js-mode. I know that a hook called js-mode is run when I open a javascript file but for some reason tern-mode is not enabled. In my init.el file I have:

(add-hook 'js2-mode-hook (lambda () ('tern-mode)))

I also tried:

(add-hook 'js2-mode-hook (lambda () (tern-mode t)))

as well as both of those with js-mode-hook (This was mostly for fun though because it makes no difference, see the comments)

I have also tried:

(add-hook 'js-mode-hook 'tern-mode)

And this does't work. However, tern-mode does not get enabled when I enter js2-mode. To enable it, each time I have to press M-x tern-mode, then it works.

Is there anything I am doing wrong here?

remvee
  • 561
  • 6
  • 13
Startec
  • 1,354
  • 1
  • 13
  • 30
  • 1
    If you want to enable a minor mode named `tern-mode` when entering the major mode named `js-mode`, you can use `(add-hook 'js-mode-hook 'tern-mode)`, you usually don't have to wrap `tern-mode` unless you have very old version of Emacs or some other special reason. `js2-mode` and `js-mode` is different thing, figure which you're using. Tern's doc already says how to use it, see http://ternjs.net/doc/manual.html#emacs – xuchunyang Sep 30 '15 at 06:54
  • Reading the documentation for `js2-mode` says: `In addition to any hooks its parent mode `js-mode' might have run, 15 this mode runs the hook `js2-mode-hook', as the final step 16 during initialization. ` I have the newest (24.5) version of emacs and have tried your suggestions and it does not work. – Startec Sep 30 '15 at 07:03
  • Startec: `(add-hook 'js-mode-hook 'tern-mode)` works fine for me (aside from erroring out because I don't have `tern` installed; but the minor mode is enabled as expected). – phils Sep 30 '15 at 07:13
  • @phils thanks for trying to help me out. I looked at this link : http://stackoverflow.com/questions/21258066/tern-auto-complete-wont-start-automatically As it seemed closely related but I couldn't find anything useful. I can't figure out what it is. Those are single quotes right? – Startec Sep 30 '15 at 07:16
  • Yes, plain ascii apostrophes. You can alternatively write `(add-hook (quote js-mode-hook) (quote tern-mode))` if you wish to eliminate all doubt about the punctuation marks. In either case, `C-h v js-mode-hook RET` should report the value as `(tern-mode)` -- or perhaps some longer list of which `tern-mode` is a member, at any rate. Once you're in `js2-mode`, check whether `Tern` is displayed in the mode line; or type `C-h m` and see whether `Tern` is listed amongst the minor modes. – phils Sep 30 '15 at 07:45
  • Hm, I have been using ascii apostrophes but when typing `C-h m`, tern mode is not listed among the minor modes until I explicitly call it with `M-x tern-mode`. What I get with `C-h v js-mode-hook RET` is `Its value is (skewer-mode ac-js2-mode ac-js2-setup-auto-complete-mode) ` . `tern mode` is not in that list – Startec Sep 30 '15 at 07:59
  • 1
    correction: What I get with `C-h v js-mode-hook RET` is `Its value is (tern-mode skewer-mode ac-js2-mode ac-js2-setup-auto-complete-mode) ` . but it still does not immediately open. – Startec Sep 30 '15 at 08:06
  • 2
    @Startec: note that only recent versions of js2-mode derive from js-mode. So if the user has an earlier version it might not run `js-mode-hook`. In any case I suspect the `*Messages*` might contain some hint. – Stefan Sep 30 '15 at 12:52
  • @Stefan unfortunately the only message in the messages buffer at all is only about the snippets. I.e. `[yas] Loading compiled snippets from /home/optonox/.emacs.d/elpa/yasnippet-20150927.1716/snippets/js-mode` Is the only line with "`js`" in it at all. – Startec Oct 01 '15 at 05:48

1 Answers1

1

The canonical answer here is:

(add-hook 'js2-mode-hook 'tern-mode)

This line of code is in my emacs config and works fine. When I open a .js file, tern-mode is enabled.

If that doesn't work, there are other things to debug, such as what package manager you're using, which files you're opening, whether tern has any prerequirements you're not fulfilling (like a server available in bin/tern ?), etc.

Trevoke
  • 2,375
  • 21
  • 34