Skip to main content
 首页 » 编程设计

python之如何仅在编辑 python 代码时加载我的 .el 文件

2025年05月04日18hnrainll

我在文件 my-python-setup.el 中收集了一份 emacs 定制列表。我怎样才能确保 emacs 将首先加载 python-mode 然后仅在我编辑 python 文件时加载这个库?

我试过

(load-library "my-python-setup") 

在我的 .emacs 文件中,但这会为所有类型的文件加载这些自定义设置。

这些定制是在 python-mode 之上的,auto-mode-alist 的值当前是 ("\\.py\\'". python-mode).

请您参考如下方法:

我绝不是 Emacs 专家,但我认为您可以坚持添加一个 python-mode-hook 函数并在其中加载您的库。像这样的东西:

;; define your hook function 
(defun python-mode-setup () 
  (message "Custom python hook run") 
  (load-library "my-python-setup")) 
 
;; install your hook so it is called when python-mode is invoked 
(add-hook 'python-mode-hook 'python-mode-setup) 

这里是我个人的python-mode-hook,例如:

(defun python-mode-setup () 
  (setq python-indent-offset 4 
        python-indent 4 
        ;; turn off indentation guessing in various python modes 
        python-guess-indent nil 
        python-indent-guess-indent-offset nil 
        py-smart-indentation nil 
        ;; fix mark-defun in new python.el 
        python-use-beginning-of-innermost-defun t)) 
(add-hook 'python-mode-hook 'python-mode-setup)