rct-complete の Ruby 1.9 用クイックハック

rct-complete とは

Ruby コードの編集中にメソッド名などを補完してくれる機能。
rcodetools に含まれる。

Emacs で以下のコードを書いて、

"hello".enc

M-TAB すると、

"hello".encod

メソッド名の途中まで補完される。

もう一度 M-TAB すると、メソッドの候補を出してくれる。

Ruby 1.9 で使ってみる

Ruby 1.9 を使っていると、以下のコードを補完できない。

# -*- coding: utf-8 -*-
"こんにちは".enc

同じように、文字列に対して enc で始まるメソッドを探してるだけなのに。

原因とクイックハック

rct-complete は何行目の何文字目を補完するか指定するんだけど、Ruby 1.9 はマルチバイト文字も 1 文字と数えるため、位置がずれてしまったみたい。

rcodetools.el に以下のパッチを当てると、とりあえず動くようになった。

*** rcodetools.el.org	2009-04-08 20:54:34.000000000 +0900
--- rcodetools.el	2009-04-08 20:54:34.000000000 +0900
***************
*** 148,153 ****
--- 148,160 ----
        (message "%s" logmsg))
    logmsg)
  
+ (defun rct-count-char-of-line ()
+   (save-excursion
+     (let (beg (end (point)))
+       (beginning-of-line)
+       (setq beg (point))
+       (length (buffer-substring beg end)))))
+ 
  (defun rct-exec-and-eval (command opt)
    "Execute rct-complete/rct-doc and evaluate the output."
    (let ((eval-buffer  (get-buffer-create " *rct-eval*")))
***************
*** 155,161 ****
      (rct-shell-command
       (format "%s %s %s --line=%d --column=%d %s"
               command opt (or rct-option-local "")
!              (rct-current-line) (current-column)
               (if rct-use-test-script (rct-test-script-option-string) ""))
       eval-buffer)
      (message "")
--- 162,168 ----
      (rct-shell-command
       (format "%s %s %s --line=%d --column=%d %s"
               command opt (or rct-option-local "")
!              (rct-current-line) (rct-count-char-of-line)
               (if rct-use-test-script (rct-test-script-option-string) ""))
       eval-buffer)
      (message "")

もちろん無保証です。