Personal tools
You are here: Home ブログ 井上 EmacsでAOP再挑戦
« January 2009 »
Su Mo Tu We Th Fr Sa
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Categories
カテゴリなし
 
Document Actions

EmacsでAOP再挑戦

Emacs Lisp勉強会の応用編は無期延期中(*)ですが、小ネタを少し。

の続きです。

カーソル位置の関数名や変数名をコピーしたいことが多々あるので、次のようにM-kに割り当てました。

(global-set-key "\M-k" (lambda () (interactive) (kill-new (thing-at-point 'symbol))))

寂しいので、コピーの時に選択文字列を光らせるようにしました。

(defun my-thing-at-point (thing)
  (if (get thing 'thing-at-point)
      (funcall (get thing 'thing-at-point))
    (let ((bounds (bounds-of-thing-at-point thing)))
      (if bounds 
          (let ((ov (make-overlay (car bounds) (cdr bounds))))
            (overlay-put ov 'face 'region)
            (sit-for 3)
            (delete-overlay ov)
            (buffer-substring (car bounds) (cdr bounds)))))))

(global-set-key "\M-k" (lambda () (interactive) (kill-new (my-thing-at-point 'symbol))))

少し長いですが、実は、thing-at-pointに4行書き足しただけです(make-overlayのある行以降の4行分)。

コードをコピー&ペーストして一部を書き換える行為は、どんなプログラミング言語であっても悪です。この観点で、上のコードは気に入りません。

今やりたいことは、thing-at-pointがカーソル付近の文字列を返すので、その文字列に対してoverlayを作成することです。thing-at-pointに手を入れないで行う最も保守的な方針は、文字列をもう一度バッファの中で探しなおすことだと思います。thing-at-pointの中を知らなければ特に気になりませんが、buffer-substringの結果が返っているのを見てしまうと、もう一度その文字列を探すのがバカらしく感じてしまいます。

関数に手をいれず、かつその実装を再利用する、となるとadviceが思いつきます。いつかadviceをうまく使いたいと思っているのですが、今回も企みは失敗しました。adviceで(thing-at-pointの)ローカルシンボルboundsを参照する手段が無いからです(少なくとも、ぼくには手段が不明)。

(*)某社では、Emacs LispよりJavaの方が人気があるという、衝撃の事実が発覚したため。

The URL to Trackback this entry is:
http://dev.ariel-networks.com/Members/inoue/aop-of-emacs-2/tbping

Re:EmacsでAOP再挑戦

Posted by horii at 2007-02-07 00:34
>(*)某社では、Emacs LispよりJavaの方が人気があるという、衝撃の事実が発覚したため。

そんなに衝撃的ではないと思います。
Add comment

You can add a comment by filling out the form below. Plain text formatting.

(Required)
(Required)
(Required)
This helps us prevent automated spamming.
Captcha Image


Copyright(C) 2001 - 2006 Ariel Networks, Inc. All rights reserved.