Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Pymacs & Ropemacs only when opening a Python file?

I use Pymacs to load ropemacs and rope with the following lines in my .emacs file as described here.

(autoload 'pymacs-load "pymacs" nil t)
(pymacs-load "ropemacs" "rope-")

It however slows down the start-up of Emacs significantly as it takes a while to load Ropemacs.

I tried the following line instead but that loads Ropemacs every time a Python file is opened:

(add-hook 'python-mode-hook (lambda () (pymacs-load "ropemacs" "rope-")))

Is there a way to perform the pymacs-load operation when opening a Python file but only if ropemacs and rope aren't loaded yet?

like image 873
mtgred Avatar asked May 22 '10 23:05

mtgred


2 Answers

In my .emacs I have:

(autoload 'python-mode "my-python-setup" "" t)

And in a separate file my-python-setup.el I keep:

(require 'python)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
;; Initialize Pymacs
(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
;; Initialize Rope
(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)

This way, Pymacs and ropemacs will be loaded only once. This happens when the first .py file is opened.

like image 128
malana Avatar answered Nov 04 '22 03:11

malana


This is what eval-after-load is for.

(eval-after-load "python-mode"
  '(progn
     ;; Do whatever you need to do here. It will only get executed
     ;; after python-mode.el has loaded.
     (require 'pymacs)
     (pymacs-load "ropemacs" "rope-")))

You'll need to write "python" instead of "python-mode" if you use python.el instead of python-mode.el.

I actually have my ropemacs loading code in a separate function that can be called interactively. This is because occasionally ropemacs crashes for me, and when it does I just call that function to reload it.

like image 44
Thomas Parslow Avatar answered Nov 04 '22 01:11

Thomas Parslow