9

Let's say we are working with Python (or Matlab) with company. Without an inferior process company messages that

Completion needs an inferior Python process running

However, the obvious solution to automatically run an inferior process

 (add-hook 'python-mode 'run-python)

would run a new python process every time I open a .py file (this becomes more expensive with Matlab)

Is there a hook that gets run only the first time a major mode is run ? Or is there a way to detect if a python/matlab inferior process is already running ? Or is there another way to solve this problem of running an inferior process automatically with a major mode but only once ?

1 Answer 1

14

FYI, some modes open only one inferior processs. For example, run-python will open an inferior process only if there isn't already an inferior process running. What you suggested with run-python actually does what you want.

For other modes (Matlab?), you could use a function that unhooks itself, like so:

(defun run-python-once ()
  (remove-hook 'python-mode-hook 'run-python-once)
  (run-python))

(add-hook 'python-mode-hook 'run-python-once)

Note that on Emacs 24.5 (and older?), run-python takes a mandatory CMD argument, so the solution will be:

(defun run-python-once ()
  (remove-hook 'python-mode-hook 'run-python-once)
  (run-python (python-shell-parse-command)))

(add-hook 'python-mode-hook 'run-python-once)
2
  • It seems like the run-python function takes a required arg: (run-python CMD &optional DEDICATED SHOW). It works fine interactively, but when this hook executes I get a Wrong number of arguments error.
    – gsgx
    Commented Jul 18, 2016 at 14:25
  • 1
    Thanks, the argument is optional in Emacs 25, but not in 24.5. I updated the answer with a fixed solution for Emacs 24.5
    – bmag
    Commented Jul 18, 2016 at 18:07

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.