Created
August 9, 2012 09:40
-
-
Save gongo/3302706 to your computer and use it in GitHub Desktop.
define-key に動的?な引数付き関数をセットしたい場合のなんか良い感じの手法教えてください
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QA@IT にもマルチポストしてみたところ、期待通りの動きするコードを教えていただきました。感謝! | |
http://qa.atmarkit.co.jp/q/2317 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;; やりたいこと | |
;; | |
;; "click!!" って文字上で Enter or クリックしたら | |
;; "Hello, World!" って表示してほしい感じのやつ。 | |
;; | |
;;;; 現状 | |
;; | |
;; イメージでは下のような書き方かなと思うんだけど | |
;; このままだと "Symbol's value as variable is void: msg" だからできない。 | |
;; | |
;; msg をグローバル変数に定義して、そいつを呼び出す関数を | |
;; define-key に渡せば実現できるけど、ローカルで完結できる方法があるのかどうか | |
(defun gongo:add-click-property (str msg) | |
(let ((map (make-sparse-keymap)) | |
(func (lambda () (interactive) (message msg)))) | |
;; (func (lambda () (interactive) (message "hoge")))) ;; これなら当然問題無し | |
(define-key map [return] func) | |
(define-key map [mouse-1] func) | |
(define-key map [down-mouse-1] 'mouse-set-point) | |
(add-text-properties 0 (- (length str) 1) | |
(list 'keymap map) | |
str) | |
(insert str))) | |
(gongo:add-click-property "click!!" "Hello, World!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment