8

I've seen this question a lot. But none of the replies fixed my issue: the minor mode stays on in others modes.

So far, I have tried:

(use-package stickyfunc-enhance
  :config
  ;; (add-to-list 'semantic-default-submodes 'global-semantic-stickyfunc-mode)
  ;; (defun turn-on-semantic () (semantic-mode 1) (require 'stickyfunc-enhance))
  ;; (add-hook 'python-mode-hook 'turn-on-semantic)
  (add-hook 'python-mode-hook
            (lambda ()
              (add-to-list 'semantic-default-submodes 'global-semantic-stickyfunc-mode)
              (semantic-mode 1)
              (require 'stickyfunc-enhance))))

My issue with those 2 approaches is that semantic-mode will stay on in all major modes once I visited a Python buffer.

How can I turn off semantic-mode in all other modes? Do I have to manually turn it off on prog-mode-hook?


Trying kaushalmodi's solution:

(use-package stickyfunc-enhance
  :init
  (add-to-list 'semantic-default-submodes 'global-semantic-stickyfunc-mode)
  :config
  (defun me/enable-semantic-maybe ()
    "Maybe enable `semantic-mode'."
    (if (derived-mode-p 'python-mode)
        (lambda ()
          (semantic-mode 1)
          (require 'stickyfunc-enhance))
      (semantic-mode -1)))
  (add-hook 'change-major-mode-hook #'me/enable-semantic-maybe))

Yields me:

Making python-shell-interpreter local to  *Python Internal [792caf12c778150badeeede64c068cee]* while let-bound!
Making python-shell-interpreter-args local to  *Python Internal [792caf12c778150badeeede64c068cee]* while let-bound!
Mathieu Marques
  • 1,953
  • 1
  • 13
  • 30

2 Answers2

2

Looking at the source code in semantic.el, it looks like semantic-mode is a global minor mode. So once activated, it will be activated in all the buffers; and once deactivated, it will be deactivated in all.


You can probably have something like below that enables semantic mode only when you open a specific mode buffer (but I am not sure how well it will behave when you have buffers open with different major modes):

(defun my/enable-semantic-mode-maybe ()
  (if (derived-mode-p 'python-mode)
      (semantic-mode 1)
    (semantic-mode -1)))
(add-hook 'change-major-mode-hook #'my/enable-semantic-mode-maybe)

From semantic.el:

(define-minor-mode semantic-mode
  "Toggle parser features (Semantic mode).
With a prefix argument ARG, enable Semantic mode if ARG is
positive, and disable it otherwise.  If called from Lisp, enable
Semantic mode if ARG is omitted or nil.

;; -- snip --     

\\{semantic-mode-map}"
  :global t ; <-- GLOBAL MODE

;; -- snip --
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
  • Alright, that looks a cleaner that what I was going for with `prog-mode-hook` (if even it was working). However, can you tell me why did you use a `# `? Does byte-compiled means it will be computed in the first step that .elc is? – Mathieu Marques Aug 18 '15 at 16:07
  • 1
    It's a good coding practice to use `#'` (sharp-quote) where a *function symbol* argument is expected. [[More reading](http://endlessparentheses.com/get-in-the-habit-of-using-sharp-quote.html)] – Kaushal Modi Aug 18 '15 at 16:10
  • I have errors. Updated question. – Mathieu Marques Aug 18 '15 at 16:39
  • @MathieuMarques Couple of things.. you would use `(progn ` instead of `(lambda ()` there to wrap the stuff that should happen if the `if` condition is true. Also I would `require` `stickyfunc-enhance` in the very beginning in the `:init` before you reference `global-semantic-stickyfunc-mode`. As for the actual error, I would start a new question with a more directed question + tags. I think though that the problem is with you using `lambda` instead of `progn`. – Kaushal Modi Aug 18 '15 at 16:43
  • Yes I realize now I mixed up both, thanks for the help :). – Mathieu Marques Aug 18 '15 at 16:48
  • [Here is the link](http://emacs.stackexchange.com/questions/14856/cant-have-semantic-mode-turned-on-for-python-buffers-only) if you are interested. – Mathieu Marques Aug 19 '15 at 16:34
2

The best solution I have found to restrict semantic-mode to certain buffers only is to set semantic-inhibit-functions. For example you can add a function that inhibits semantic in buffers not in python mode as follows

(defun my-inhibit-semantic-p ()
  (not (equal major-mode 'python-mode)))

(with-eval-after-load 'semantic
      (add-to-list 'semantic-inhibit-functions #'my-inhibit-semantic-p))
Iqbal Ansari
  • 7,468
  • 1
  • 28
  • 31