ある特定のモードでsemanticをオフに。

どうもいっす。


こないだの件です。
つまり,htmlファイル内でjavascriptを編集して,
アイドル状態になり一定時間経つと,
semantic関係のエラーメッセージが尋常じゃない量でエコーエリアにでてくる。


そもそも,semanticについては,jde-mode でしか使いたくない。
というか,一発でオン/オフを切り替えられるコマンドを用意してないあたりが,
なんというか作者様の性格がでているというか。
ワタシは若輩者ですが,機能やらソフトウェアの設定,インストールについては,
可逆であるべきだとおもっちょります。Nothing's irreversible. であるべきだと。


M-x semantic-mode を用意してオン/オフを切り替えられるようにするのが筋なんじゃねぇのか?
semanticをインストールしただけで,よもやここまでの惨事になるとは。
いや,でかいソフトウェアなんだろうけど,でかいからこそシンプルさって大切やん。
インストールしたらなんでもかんでもsemanticって・・・。すげー嫌い。
嫌いなら使わなければいいじゃん。その通りですね。
少なくても,デフォルトでオフにしておいて使いたいモードなりファイルなりで,
有効にする方がいいんじゃねぇの?


閑話休題


とりあえず,おなじみの,M-x apropos で,「semantic.*mode 」をひいてみた。
そして眺めてみた。
もひとつ分からず。

なので,Googleってみる。「emacs semantic disable」で。
semantic のドキュメントがひっかっかた。

- Variable: semantic-new-buffer-setup-functions

    The value of this variable is an alist of functions to call for
    setting up Semantic parsing in the buffer. Each element has the
    form (mode . fn), where mode is a value of major-mode for the
    buffer and fn is the corresponding function for setting up the
    parser. fn is called, with no arguments, after the major mode is
    initialized (and after the mode hooks have been run).

    The default value enables Semantic for all supported major modes
(i.e., C, C++, Scheme, Javascript, Java, HTML, SRecode, and Make), but
you can remove modes from this list if you don't want to use Semantic
with them.


おー,このsemantic-new-buffer-setup-functionsから,javascript-mode を削除すれば,
大丈夫なはずだ。
M-x describe-variable しようとするが,見つからず。
むー,どうやらsemanticのバージョン依存らしいが,それ以上はわからず。


でも,どうやら,バッファーを開く --> semanticを有効にするか無効にするかの判断,
というような流れになっていそうなことはわかった。
そこで,M-x apropos semantic.*buffer
してみる。
semantic-new-buffer-fcn という関数が見つかる。
引用します。

semantic-new-buffer-fcn is a compiled Lisp function in `semantic.el'.
(semantic-new-buffer-fcn)

Setup the current buffer to use Semantic.
If the major mode is ready for Semantic, and no
`semantic-inhibit-functions' disabled it, the current buffer is setup
to use Semantic, and `semantic-init-hook' is run.

そして,訳します。屋久島ではありません。

カレントバッファでSemanticを使用するためにセットアップ処理をします。カレントバッ
ファのメジャーモードに対してSemanticが準備オッケーで,かつ,
`semantic-inhibit-functions' がSemanticを無効にしていなければ,カレント
バッファでSemanticを使用するためにセットアップ処理がされ,
`semantic-init-hook' が実行されます。

ぎこちない訳になりましたが,どうやら,semantic-inhibit-functions が関係しているはず。
名前もそれっぽいし。


もうおなじみの,M-x describe-variable semantic-inhibit-functions。
こちらも引用します。

Documentation:
List of functions to call with no arguments before Semantic is setup.
If any of these functions returns non-nil, the current buffer is not
setup to use Semantic.

You can customize this variable.

ポイントだけ。
関数のリスト。リスト内の関数が呼ばれ,そのうちどれか1つでもnon-nilを返せば,
カレントバッファでsemanticは有効にならん。
つまり,このリストに,「ある条件下だったら,non-nilを返す」関数を追加すれば良さそうだ。
では早速。

(defun my-semantic-inhibit-func ()
  (cond
   ((member major-mode '(javascript-mode html-helper-mode html-mode))
    ;; to disable semantic, return non-nil.
    t)
   (t nil)))

(add-to-list 'semantic-inhibit-functions 'my-semantic-inhibit-func)

javascript-mode, html-helper-mode, html-mode のいずれかだったら,tを返して,
それ以外の場合は nil を返す,と。
今回のターゲットはjavascript-modeですが,一応ということでその他のモードも。
他のモードで勝手にsemanticが有効になって邪魔という場合は,
上記関数のメジャーモードのリストに,そのメジャーモードを追加すればオッケー牧場。


というわけで,javascriptが入った,htmlファイルを開いてみる。
相変わらずエラーメッセージがでる。
むむむ。
一応Meadowをもう1つ立ち上げて試してみる。
しばらく待つ。
お?
で・・な・い・・だと?
キタコレ!

まとめ

色々愚痴ってすみません。
おそらくとってもシンプルな方法・設定が用意してあるけど,
ワタシのテクなしのために,見つけられなかっただけでしょう。


文章では簡単に書いておりますが,じつは色々回り道をしています。
semantic-summary-mode やらなんかほかのマイナーモードをオフにしたり,色々。


semanticについて分かったことは,使わないに越したことはない,ということですね。
わかります。

まともなまとめ

Semanticをあるバッファで無効にするには,
semantic-inhibit-functions に「ある条件下だったら,non-nilを返す」関数を追加する,
ということですね。はい。
お疲れちゃん。