scheme-mode と Gauche の module について

Gauche + Emacs で対話的にモジュールを使った開発しているときに、以下のような問題にあたります。 elisp を書いてみましたがそもそも同じような環境で開発されている皆様はどうされてるのでしょう?私の開発方法がそもそもずれている可能性もあるので、識者のコメントをいただけると嬉しいです。

1モジュールを1ファイル(.scm) に記述していて、複数のモジュールを同時に(つまり複数の.scmファイルがある状態)開発しているとします。emacs から run-schemeGauche を起動し、対話的に開発しています。この際、ファイル全体を毎回ロード(scheme-load-file, C-c C-l)するのは時間がかかるので、変更した S 式だけ、 scheme-send-last-sexp ,C-x C-e で評価したいです。

が、*scheme* バッファの (current-module) と、ファイルの module が異なるとうまくいきません。Gauche のモジュールに scheme-mode が対応していないためだと思います。毎回 *scheme* バッファでモジュールを変更するのはあまりに面倒で仕方が無いので、以下のようなやっつけ elisp を書きました。

  • まず、バッファローカル変数(バッファ毎に異なる変数)として *gauche-current-module* を定義します。(.emacs)
  • scheme module の先頭行に、そのファイルの属するモジュールを emacs に教えるため モード行におまじないを書きます。
  • scheme-send-last-sexp に advice を与えて、評価する式を送る前に (select-module foobar) を実行するようにします。
  • 注意。エラー処理など一切おこなっておらず、かなりいいかげんな処理をしています。
;; .emacs に追加する部分
(make-variable-buffer-local '*gauche-current-module*)

(defadvice scheme-send-last-sexp (before my-gauche-mode ())
  (let ((module (or *gauche-current-module* "user")))
    (comint-send-string (scheme-proc) (format
				       "(format #t \";; select module %s...\")"
				       module))
    (comint-send-string (scheme-proc) "\n")
    (comint-send-string (scheme-proc) (format "(select-module %s)"
					      module))
    (comint-send-string (scheme-proc) "\n")
    ))

(ad-activate 'scheme-send-last-sexp)

以下、compiler.scm ファイルのイメージです。

;; -*- *gauche-current-module*: compiler -*-
;;;; $Id: compiler.scm,v 1.89 2009/01/31 23:22:51 cranebird Exp cranebird $
;;;; Scheme Toy compiler on LLVM by crane
(define-module compiler
  (use gauche.test)
  (use gauche.parameter)
...
  )

(select-module compiler)
;; 以下  compiler モジュールの中身