简体   繁体   English

Emacs:仅在init.el中为给定的主要模式加载软件包

[英]Emacs: load package only for given major mode in init.el

I made some time ago a .el file (epx.el) for some files with a specific format that I am using. 不久前,我为一些使用特定格式的文件制作了.el文件(epx.el)。 I load it in my init.el through (require 'epx) . 我通过(require 'epx)其加载到(require 'epx)

The problem is that in this epx.el, there is: 问题在于,在此epx.el中,存在:

(defadvice comment-region (after indent-after activate)
  (indent-region beg end)
  )
(defadvice uncomment-region (after indent-after activate)
  (indent-region beg end)
  )

I got trouble because it affects the behavior of the comment functions (such as comment-dwim ) in other major modes (in particular in the python mode: when I uncomment a commented region, it breaks my indentation...). 我很麻烦,因为它会影响其他主要模式(特别是在python模式下)的注释函数(例如comment-dwim )的行为:当我取消注释区域时,它会缩进缩进...)。 If I comment these lines in epx.el, there is no more problems. 如果我在epx.el中注释了这些行,就没有更多问题了。

So how could these (defadvice ...) only have effect in the epx major mode? 那么这些(defadvice ...)仅在epx主模式下如何起作用?

Thanks! 谢谢!

You could check the major mode of the current buffer, and take action only if it's exp-mode : 您可以检查当前缓冲区的主模式,并且只有在它为exp-mode时才采取措施:

(defadvice comment-region (after indent-after activate)
  (if (derived-mode-p 'exp-mode)
      (indent-region beg end))
  )
(defadvice uncomment-region (after indent-after activate)
  (if (derived-mode-p 'exp-mode)
      (indent-region beg end))
  )

EDIT: use derived-mode-p as suggested by @Stefan. 编辑:使用@Stefan建议的derived-mode-p -p。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM