简体   繁体   中英

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. I load it in my init.el through (require 'epx) .

The problem is that in this epx.el, there is:

(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...). If I comment these lines in epx.el, there is no more problems.

So how could these (defadvice ...) only have effect in the epx major mode?

Thanks!

You could check the major mode of the current buffer, and take action only if it's 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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