3

For files with mixed types of programming languages, such as .html, semantic has trouble analyzing the code. Is there any way to specifically disable the auto analyzing by semantic for those types of files?

I am using the built-in cedet coming with Emacs 24:

CEDET Version:  1.0
            Requested   File        Loaded
  Package       Version     Version     Version
  ----------------------------------------------------------
  cedet:        1.0     nil     ok
  eieio:        1.3     nil     ok
  semantic:     2.0     nil     ok
  srecode:      1.0     nil     ok
  ede:          1.0     nil     ok
  speedbar:     1.0     nil     ok
tshepang
  • 12,111
  • 21
  • 91
  • 136
RNA
  • 146,987
  • 15
  • 52
  • 70

3 Answers3

3

Use semantic-inhibit-functions. For example, this:

(setq semantic-inhibit-functions
      (list (lambda () (not (and (featurep 'cc-defs)
                                 c-buffer-is-cc-mode)))))

should disable Semantic in all non-cc-mode buffers.

Dmitry
  • 3,625
  • 2
  • 30
  • 28
2

From Semantic docs:

2.3.1 Don't parse certain buffers

You can inhibit parsing using the semantic-inhibit-functions variable.

— Variable: semantic-inhibit-functions List of functions to call with no arguments before semantic sets up a buffer. If any of these functions returns non-nil, the current buffer is not setup to use Semantic.

You could have this inhibit parsing in very large files, or files which cause particular problems to semantic.

Proper usage example:

  ;; Disable Semantic's source referencing in lisp buffers.
  (add-hook 'semantic-inhibit-functions
            (lambda () (member major-mode '(emacs-lisp-mode))))

This adds it to the list (instead of overwriting the list), and ensures that it only runs after semantic has been loaded.

You must use with-eval-after-load, since the semantic package may not be loaded when your user config initializes. And in that case, you would get an error saying that the semantic-inhibit-functions variable doesn't exist, and your override wouldn't be applied.

This fixes those issues and is better than @Tom's and @Dmitry's answers.

Oh and I highly recommend blocking emacs-lisp-mode, because otherwise Semantic will try parsing the entire source code tree of the running Emacs instance whenever autocompletion is triggered, which will freeze Emacs if you have a lot of packages.

  • 2
    You can do one better, and use `add-hook` instead of `add-to-list`. That would make `with-eval-after-load` unnecessary as well. – Dmitry Dec 12 '16 at 00:06
  • @Dmitry It is a **list** of functions that semantic will call to check if it should enable itself for every buffer. Using add-hook (which is a list-writer function) would create the list if it doesn't exist yet, which may break in the future if semantic overwrites the list at init. The proper way is therefore to wrap in `with-eval-after-load add-to-list` as I did above. You can Google it. ;-) –  Dec 12 '16 at 05:51
  • 1
    `semantic-inhibit-functions` is a `defcustom` with the type `hook`. So Semantic itself should worry about not overwriting it in the future. Furthermore, it's processed with `run-hook-with-args-until-success`. – Dmitry Dec 12 '16 at 14:40
  • @Dmitry Thanks, that's a great discovery. I have updated the code example to remove with-eval-after-load :) –  Dec 12 '16 at 22:34
1

Another example, to disable it for a specific mode:

(add-to-list 'semantic-inhibit-functions
             (lambda () (member major-mode '(html-mode))))
Tom
  • 42,844
  • 35
  • 95
  • 101
  • It worked days ago, but now it won't work anyway, weird. I already change it to (add-hook 'org-mode-hook '(lambda() (set (make-local-variable 'semantic-mode) nil))), this works. – CodyChan Jan 11 '14 at 10:32