加载 Pymacs & Ropemacs 仅在打开 Python 文件时使用?

发布于 2024-09-02 08:56:12 字数 788 浏览 9 评论 0原文

我使用 Pymacs 加载 ropemacsrope ,并在我的 .emacs 文件中使用以下几行,如 此处

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

然而,它会显着减慢 Emacs 的启动速度,因为加载 Ropemacs 需要一段时间。

我尝试了以下行,但每次打开Python文件时都会加载Ropemacs

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

Is有一种方法可以在打开Python文件时执行pymacs-load操作,但仅如果 ropemacsrope 尚未加载?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜心小果奶 2024-09-09 08:56:12

在我的 .emacs 中,我有:

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

在一个单独的文件 my-python-setup.el 中,我保留:

(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)

这样,Pymacs>ropemacs 将仅加载一次。当第一个 .py 文件打开时会发生这种情况。

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.

忆依然 2024-09-09 08:56:12

这就是eval-after-load 的用途。

(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-")))

如果您使用 python.el 而不是 python-mode.el,则需要编写“python”而不是“python-mode”。

实际上,我的 ropemacs 加载代码位于一个可以交互调用的单独函数中。这是因为有时 ropemacs 对我来说会崩溃,当它发生时,我只需调用该函数来重新加载它。

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.

半仙 2024-09-09 08:56:12

这是我的解决方案:

(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)

其中 ac-ropemacs-setup自动完成 模块中定义:

(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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文