Load Pymacs & Ropemacs only when opening a Python file?
I use Pymacs to load ropemacs and rope with t开发者_JS百科he 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?
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.
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.
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.
精彩评论