Search code examples
pythonemacsconfigurationelpy

emacs configuration: python-mode-hook


I am struggling with my emacs configuration. The relevant lines in .emacs are:

(require 'python-mode)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))

(add-hook
 'python-mode-hook
 '(lambda ()
    (message "python-mode-hook called")
    (require 'jedi)
    (when (require 'elpy nil t)
      (elpy-enable)
      (setq elpy-rpc-backend "jedi")
      (add-hook
       'jedi-mode-hook
       '(lambda ()
          (setq-local ac-max-width 0.5))))))

Whan I load a python file, the hook is called and I get the "python-mode-hook called" message. However, the elpy functionality is not there. If I then M-x python-mode, everything is as it should.

I don't understand why I need to call "python-mode" twice. I somehow think it may be related to the hooks being called/defined in the wrong order, but I don't understand what is wrong here.

I'd appreciate some hints.


Solution

  • Figured it out in the meanwhile... the problem was that elpy-enable does install a hook. It does not directly invoke the elpy mode as I thought. Therefore only the second invocation of python-mode did actually result in a call of this function. Of course this is better... So now I have:

    (elpy-enable)
    (setq elpy-rpc-backend "jedi")
    (add-hook
     'elpy-mode-hook
     '(lambda () (setq-local ac-max-width 0.5)))