Load Pymacs & Ropemacs only when opening a Python file?
Asked Answered
C

3

5

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?

Conjunctive answered 22/5, 2010 at 23:54 Comment(0)
L
9

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.

Liberticide answered 1/6, 2010 at 20:28 Comment(0)
S
2

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.

Swaggering answered 19/9, 2010 at 13:28 Comment(1)
And as this answer indicates, eval-after-load runs only once, so it is ideal for initially configuring a mode.Contingent
K
1

This is my solution:

(defun my-python-hook-mode ()
  (interactive)
  (require '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)
  (ac-ropemacs-setup)
  (setq ropemacs-confirm-saving 'nil)
  (ropemacs-mode t)
  (define-key python-mode-map "\C-m" 'newline-and-indent)
 )
(add-hook 'python-mode-hook 'my-python-hook-mode)

where ac-ropemacs-setup is defined in the auto-complete module:

(defun ac-ropemacs-require ()
  (with-no-warnings
    (unless ac-ropemacs-loaded
      (pymacs-load "ropemacs" "rope-")
      (if (boundp 'ropemacs-enable-autoimport)
          (setq ropemacs-enable-autoimport t))
      (setq ac-ropemacs-loaded t))))

(defun ac-ropemacs-setup ()
  (ac-ropemacs-require)
  ;(setq ac-sources (append (list 'ac-source-ropemacs) ac-sources))
  (setq ac-omni-completion-sources '(("\\." ac-source-ropemacs))))

This solution assume that you use auto-complete at the same time.

Kept answered 8/3, 2011 at 14:30 Comment(1)
As this answer indicates, using a mode hook isn't really the right approach here because a mode hook runs for every new buffer to which the mode applies. But making use of auto-complete is a good idea.Contingent

© 2022 - 2024 — McMap. All rights reserved.