blog/content/post/1970-01-01-00000000.md

7.7 KiB
Raw Blame History

title author date url wordtwit_post_info categories
anything の source を作ってみる (initの使い方) kazu634 1969-12-31 /1970/01/01/_0/
O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4879;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}
Emacs

Anything.el の source で init を使ってみて、うまくいかなかったのでここに書いておきます。

駄目だったソース

init の説明にはこうありました。

init (オプション)
anything が実行された際に引数なしで呼び出される関数。この要素は、候補のリストを作成するために現在の状況を収集するのに便利である。
例えば、 source がカレントディレクトリを対象とした場合、init 要素に指定することでカレントディレクトリの情報を収集することができる。なぜならば、それ以降 anything はミニバッファーと anything-buffer で動作し、カレントディレクトリが変わる可能性が出てくるからである。

2009-10-12 武蔵の日記

これは candidates を表示する前に実行される関数なのだろうと単純に考えていたのですが、間違えていたようでした。

つくってみたソースはこちら:

(setq anything-c-source-testtest
'((name . "test Search")
(init .
(lambda ()
(read-string "?: ")))
(candidates .
("foo" "bar"))
(action . (("Insert" . insert)))))

これを実行すると、下のような形で候補が表示されるのと、ミニバッファーでの問い合わせが同時に行われてしまいます:

スクリプトの引数を入力してもらってから、スクリプトを実施、候補の表示という流れにしたかったのですが、 init ではできないようです。。。

やりたかったことはこうすればできました

;; グローバル変数を定義
(defvar anything-c-source-amazon-work "a")
;; 事前にこの関数を呼び出して、引数を取得→anything-c-source-amazon-workに代入
;; その後、anything-c-source-amazonでanythingを起動する
;; anything-c-source-amazonは、anything-c-source-amazon-workを引数として
;; 外部スクリプトを実行し、候補を生成する
(defun anything_amazon (key)
(interactive "sISBN13 or ASIN: ")
(if (string-match "[0-9]+" key)
(progn
(setq anything-c-source-amazon-work key)
(anything 'anything-c-source-amazon
nil
"Title: "
nil
nil
anything-call-source-buffer))
(message "Please enter ISBN13 or ASIN!")))
(defvar anything-c-source-amazon
'((name . "Amazon Search")
(candidates .
(lambda ()
(if (string-match "[0-9]+" anything-c-source-amazon-work)
(delete ""
(split-string
(shell-command-to-string
(format "access_amazon %s"
anything-c-source-amazon-work))
"\n"))
"No Results")))
(candidate-transformer . (lambda (candidates)
(mapcar
(function (lambda(arg)
(apply 'cons (split-string arg "\t"))))
candidates)))
(action . (("Insert" . insert)))))