Emacsã®auto-insertã§ãã³ãã¬ã¼ããèªåçæãã
ããã°ã©ã ã®åä½ãã¹ããæ¸ãå ´åããå種ãã¬ã¼ã ã¯ã¼ã¯ã使ç¨ãã¦ããã°ã©ã ãæ¸ãå ´åãªã©ã¯ãããè¦åã«ãã£ã¦ãã¡ã¤ã«åãä»ããã¨æãã¾ãã(ä¾ãã°åä½ããã°ã©ã ã®ã½ã¼ã¹ãã¡ã¤ã«åã¯"ã¢ã¸ã¥ã¼ã«å_test.py"(Pythonã®å ´å)ãªã©)
Emacsã®auto-insertã¢ã¼ãã使ã£ã¦ãç¹å®ã®ååã®ãã¡ã¤ã«ãä½æããã¨ãã«ãå°ç¨ã®ãã³ãã¬ã¼ããèªåçæãã¦ãããã¨ä¾¿å©ã ã¨æãããã®æ¹æ³ã調ã¹ã¦ã¿ã¾ããã
ひげぽんさんのブログã¨その元ネタã«ã»ã¼æ£è§£ãç´¹ä»ããã¦ããã®ã§ãããlispã®ãã¯ãã使ã£ã¦è²ã ãªç¨®é¡ã®ãã³ãã¬ã¼ããç°¡åã«ä½ããããã«ããã®ã§ãæ°ããã¨ã³ããªãæ¸ãã¦ã¿ã¾ããã
ããã§ã¯Pythonã®åä½ãã¹ãã®ã½ã¼ã¹ãã¡ã¤ã«ã®éå½¢ãèªåçæããããã®Elispãæ¸ãã¦ã¿ã¾ãã
ã¾ãã~/.emacs.d/insertã«unittest_template.pyã¨ããååã®ãã³ãã¬ã¼ããã¡ã¤ã«ãç¨æãã¾ãã
#!/usr/bin/env python # -*- coding: utf-8 -*- import unittest import %testee% class %test_class%(unittest.TestCase): def setUp(self): pass def tearDown(self): pass if __name__ == '__main__': unittest.main()
次ã«ã~/.emacs.elã«æ¬¡ã®è¨å®ã追å ãã¾ãã
(require 'autoinsert) (auto-insert-mode) (setq auto-insert-directory "~/.emacs.d/insert/") (defun buffer-file-name-nondirectory () (file-name-nondirectory (buffer-file-name))) (defun python-module-name () (substring (buffer-file-name-nondirectory) 0 -8)) (defun python-test-case-name () (concat (capitalize (python-module-name)) "Test")) (require 'cl) (defmacro define-replace-function (name replacement-alist) `(defun ,name () (loop for (string . func) in ,replacement-alist do (progn (goto-char (point-min)) (replace-string string (funcall func) nil))) (goto-char (point-max)) (message "done."))) (define-replace-function python-unittest-template '(("%testee%" . (lambda () (python-module-name))) ("%test_class%" . (lambda () (python-test-case-name))) )) (add-to-list 'auto-insert-alist '("_test\\.py$" . ["unittest_template.py" python-unittest-template]))
ãã¤ã³ãã¯ãã®é¨åã§ãã
(require 'cl) (defmacro define-replace-function (name replacement-alist) `(defun ,name () (loop for (string . func) in ,replacement-alist do (progn (goto-char (point-min)) (replace-string string (funcall func) nil))) (goto-char (point-max)) (message "done."))) (define-replace-function python-unittest-template '(("%testee%" . (lambda () (python-module-name))) ("%test_class%" . (lambda () (python-test-case-name))) )) (add-to-list 'auto-insert-alist '("_test\\.py$" . ["unittest_template.py" python-unittest-template]))
ã¾ããloopãã¯ãã使ç¨ããããã(require 'cl)ãè¡ã£ã¦ãã¾ãã
次ã«ãdefine-replace-functionãã¯ããå®ç¾©ãã¦ãã¾ãããã®ãã¯ãã¯ãé¢æ°åã¨(ç½®æããæåå . ç½®æå¾ã®æååãè¿ãé¢æ°)ã®ãªã¹ãã渡ãã¨ããã³ãã¬ã¼ãæååãç½®æããé¢æ°ãå®ç¾©ãããã¯ãã§ãã
ç¶ãã¦ãå®ç¾©ãããã¯ãã使ç¨ãã¦ãå
ã»ã©ä½æããunittest_template.pyã®ãã³ãã¬ã¼ãæååãç½®æããé¢æ°ãå®ç¾©ãã¦ãã¾ãã
æå¾ã®è¡ã§ã¯ã_test.pyã§çµãããã¡ã¤ã«åãä½æãããã¨ããã¨ãã«ãããã§ä½æãããã³ãã¬ã¼ããã¡ã¤ã«ã¨æååç½®æé¢æ°ã使ç¨ããããã«ãã¦ãã¾ãã
define-replace-functionã®ç¬¬ï¼å¼æ°ã«æ¸¡ããªã¹ããå¤ããã°ãè²ã ãªãã³ãã¬ã¼ããä½æãããã¨ãã§ãã¾ãã