第0回 ess-smart-underscore 関数のソース読み

自分でelisp書けるようになりてーな、と思い立ちess-smart-underscore部分のソースを読むことにした。
ちなみにまだソースの理解はしておらず、これからその格闘を記録しようとしている。

まずはタグを作成。

/home/kozo-ni/ess-5.3.7/lisp% etags *.el

で、emacs起動して

M-x find-tag

とやって

Find tag (dafault essl-s): ess-smart-underscore

とすると以下の関数へ飛ぶ。

(defun ess-smart-underscore ()
  "Smart \"_\" key: insert `ess-S-assign', unless in string/comment.                                                                       
If the underscore key is pressed a second time, the assignment                                                                             
operator is removed and replaced by the underscore.  `ess-S-assign',                                                                       
typically \" <- \", can be customized.  In ESS modes other than R/S,                                                                       
an underscore is always inserted. "
  (interactive)
  ;;(insert (if (ess-inside-string-or-comment-p (point)) "_" ess-S-assign))                                                                
  (if (or
       (ess-inside-string-or-comment-p (point))
       (not (equal ess-language "S")))
      (insert "_")
    ;; Else one keypress produces ess-S-assign; a second keypress will delete                                                              
    ;; ess-S-assign and instead insert _                                                                                                   
    ;; Rather than trying to count a second _ keypress, just check whether                                                                 
    ;; the current point is preceded by ess-S-assign.                                                                                      
    (let ((assign-len (length ess-S-assign)))
      (if (and
           (>= (point) (+ assign-len (point-min))) ;check that we can move back                                                            
           (save-excursion
             (backward-char assign-len)
             (looking-at ess-S-assign)))
          ;; If we are currently looking at ess-S-assign, replace it with _                                                                
          (progn
            (delete-backward-char assign-len)
            (insert "_"))
        (delete-horizontal-space)
        (insert ess-S-assign)))))

コードを読み解くのはぼちぼちとやってく。(ヘルプ歓迎)

と思ってたらvim界の有名人id:ka-nachtさんから twitterで以下の御言葉をもらった。

カーソル前後の文字列から判断してるだけでは。

やべえ、急いでelispわかるようにならなくては…