脳汁でコードを書く

Archive for 3 月, 2010

今後のemacs-settings

日曜日, 3 月 28th, 2010

rubykitchさんにtwitterで紹介していただいて, すこしはみなさんの目にふれるようになっているようです

ここでemacs-settingsのTODOや今後の更新予定(やわかっているバグ)について軽く触れます

update対応

aptのupdateを想像してください. ソースファイルを最新版にする機能

upgrade対応

aptのupgradeを想像してください. たぶん現状のemac-settingsだとinstallを2回以上やるとおかしくなります. うーんどうするかなぁ. 最新かどうかをチェックしなくてはいけない

activate, deactivate対応

portのactivate, deactivateみたいに, downloadしているけどもloadしたくない, みたいなのに対応したいと思ってます.

auto-installとの共存

これは必須ですね. auto-installとどうやって共存していくかは模索中です

それにしてもwordpress使いにくいな… 移行しよう

emacs-settings: コマンドラインからemacsのパッケージをインストール

土曜日, 3 月 27th, 2010

emacsを使っていて, aptのようにemacsのパッケージが簡単にインストールできたら良いと思ったことはありませんか?

auto-installという便利なものがありますが, 個人的にはコマンドラインからやりたいなあ, と思ったりします.

というのも, 対話的なものだとスクリプト一発で俺環境構築!みたいなことができないじゃないですか.

というわけで作りました. 名前はemacs-settings. emacsのためのaptです.
これはclbuildrosinstallに多大な影響をうけています.

たとえばこんな感じになります

$ emacs-settings packages
navi-2ch
   2ch viewer
clmemo
   ChangeLog Memo
goby
   Presentation Mode
slime
   Common Lisp IDE
twittering
   Post to twitter and get your time line

で, パッケージのインストールは

$ emacs-settings install navi-2ch
now downloading http://sourceforge.net/projects/navi2ch/files/navi2ch/navi2ch-1.8.3/navi2ch-1.8.3.tar.gz/download to /Users/garaemon/prog/emacs-settings/emacs.d/navi-2ch...
now expanding /Users/garaemon/prog/emacs-settings/emacs.d/navi-2ch/navi2ch-1.8.3.tar.gz to /Users/garaemon/prog/emacs-settings/emacs.d/navi-2ch...
 
checking for a BSD-compatible install... /usr/bin/install -c

といった感じにすすんでいきます.

このemacs-settings自体はgithubからとってくることで利用できます.

$ git clone git://github.com/garaemon/emacs-settings.git

このemacs-settingsというディレクトリにパスを通してください.

次にこのemacs-settingsでインストールしたパッケージが利用できるように.emacsをいじります.

$ emacs-settings setup >> ~/.emacs

これで設定はおしまいです. あとはemacs-settings packagesで欲しいパッケージをみつけて, emacs-settings installするだけです. 詳しいことはemacs-settings helpしてね. (サポートしてない機能も多いです).

次にemacs-settingsディレクトリの構成をみていきましょう.

emacs-settings/
 emacs-settings # shell script. コマンドラインではこれを呼び出してる
 emacs-settings.el # emacs lisp script. じつは裏でこれを呼んでる
 init.el # 普通にemacsを起動するときにこれを呼び込む
 emacs.d/ #downloadしたパッケージの置き場所
 sources/ # /etc/apt/sources.list的なもの
  list.el #ここにパッケージのurlとかを書く

addコマンドでsourcesにリストファイルを追加できます.

$ emacs-settings add http://garaemon.net/emacs-settings-repo/garaemon.el

例えばこの後,

$ emacs-settings install garaemon-all-settings

とやると僕のすべての設定をinstallできます.

このリストファイルは以下のようなフォーマットになってます.

(http://garaemon.net/emacs-settings-repo/garaemon.el ;このファイルの最新版のありか
 ;; ここからパッケージの記述
 (emacs-wiki ;名前
  library ;パッケージのタイプ. library, bootstrap, virtualがあります. bootstrapは起動時に読み込まれる
  ;; tar-ball, cvs, svnとかがかけます
   (tar-ball http://mwolson.org/static/dist/emacs-wiki/emacs-wiki-2.72.tar.gz)
    "Implementation of a Wiki by JohnWiegley" ;description
   (planner) ;依存してる他のパッケージ
   ("cd emacs-wiki-2.72 && EMACS=$EMACS make")) ;インストールするときにすべきコマンド. 文字列だとshellで実行
...)

まぁmigemoにまだ対応してないとか, いろいろと問題もあるんですがねー. バグいっぱいありそうだし…

Common Lispで中置記法

水曜日, 3 月 10th, 2010

lispでは前置記法が用いられますが, 数学的なプログラムの実装はなかなか難しいという問題があります.
Common Lispにはリードマクロがあるので, 中置記法を実現することができます.

本記事の内容はnurarihyonというパッケージで提供しているものです.

というわけで実装へ

今回は#%(…)というリードマクロを考えます. ここで…の部分に中置記法が可能なようにします.

ここで以下のような仕様にします

  1. <-, =で代入可能なようにする
  2. @で配列の要素にアクセスする
  3. 複数の引数をとる場合は$でその区切りを指定する

では以下実装.

(defun read-infix-sexp (stream n char)
  (declare (ignore n char))
  (let ((sexp (read stream)))
    (infix->prefix sexp)))
 
(defun infix->prefix/split$ (arg &optional (buf nil) (result nil))
  "this function separete arg by the symbol `$'"
  ;; first, just separate arg by $.
  ;; (1 $ 2 $ 3) -> ((1) (2) (3))
  ;; (1 + 2 $ 3) -> ((1 + 2) (3))
  ;; (1 + 2 $ 3) -> (+ 2 $ 3) (1)           ()
  ;;             -> (2 $ 3)   (1 +)         ()
  ;;             -> ($ 3)     (1 + 2)       ()
  ;;             -> (3)       ()            ((1 + 2))
  ;;             -> ()        (3)           ((1 + 2))
  ;;             ->                         ((1 + 2) (3))
  (cond ((null arg) (append result (list buf)))
        ((and (symbolp (car arg))
              (eq (chimi:symbol->keyword (car arg)) :$))
         (infix->prefix/split$ (cdr arg) nil (append result (list buf))))
        (t
         (infix->prefix/split$ (cdr arg) (append buf (list (car arg)))
                               result))))
 
(defun infix->prefix/function-call (a b c)
  "For example,
a := sin
b := (1)
c := another s-expression..."
  ;; its deficult to estimate the number of arguments of b.
  ;; so we utilize another syntax `$' for separate arguments.
  (let ((function-sexp
         (cons a (mapcar #'infix->prefix (infix->prefix/split$ b)))))
    (if c
        ;; if there is c, we need to resolve c to operator and its args.
        (destructuring-bind (operator & args) c
          (list operator function-sexp (infix->prefix args)))
        function-sexp)))
 
(defun %infix->prefix (sexp)
  (destructuring-bind (a &optional b &rest c) sexp ;(a b . c)
    (cond ((and (not (null b)) (listp b))  ;when b is list
           ;; here, we check sexp like (sin(x) ...)
           (infix->prefix/function-call a b c))
          ((and a b c)
           (let ((bsym (chimi:symbol->keyword b)))
             (case bsym
               (:@
                ;; @ works as aref a @ (1 2) -> (aref a 1 2)
                ;; a @ 1 -> (aref a 1)
                ;; here we need to think aboud (cdr c) too...
                (destructuring-bind (index &rest args) c
                  (let ((this-section
                         (if (listp index)
                             (append (list 'aref a) index)
                             (append (list 'aref a) (list index)))))
                    (if args
                        (destructuring-bind (operator &rest op-args) args
                          (list (infix->prefix operator) ;no need?
                                this-section
                                (infix->prefix op-args)))
                        this-section))))
               (t
                (list (infix->prefix b)
                      (infix->prefix a) (infix->prefix c))))))
          ((and b (null c)) ; no c, it means function appling like sin(x)
           (list (infix->prefix a) (infix->prefix b)))
          ((and (null b) (null c)) (infix->prefix a))))) ;only a
 
(defun infix->prefix (sexp)
  "This function converts an infix s-expression to a prefix s-expression."
  (cond
    ((and (symbolp sexp)
          (or (eq (chimi:symbol->keyword sexp) :<-)
              (eq (chimi:symbol->keyword sexp) :=)))
     'setf)                              ;setf alias
    ((listp sexp) (%infix->prefix sexp)) ;we need to convert
    (t sexp)))                           ;may be literal

ここで, chimi:keyword->symbolは以下のような実装です

(defun symbol->keyword (sym)
  "convert a symbol to keyword.
  ;;; (symbol->keyword 'hoge) -> :hoge"
  (declare (type symbol sym))
  (intern (string sym) :keyword))

これを使うと中置記法ができます.

(set-dispatch-macro-character #\# #\% 'read-infix-sexp)
(defvar a 0)
#%(a <- 1 + 2 + atan(1.0 $ 1.0)) ;; expand to (SETF A (+ 1 (+ 2 (ATAN 1.0 1.0)))) a = 3.7853982
(defvar b (make-array 3 :initial-element 3))
#%(b @ (0) + 100.0) ;; => 103.0 expanded as (+ (AREF B 0) 100.0)

以外とみにくいですねw

snow leopardでnurikabe

月曜日, 3 月 8th, 2010

このあいだのエントリーのpatchによってsnow leopard x sbclでnurikabeが動くようになりました.

nurikabeは私が作っているGUIライブラリです.

ソースコードはここから入手できます.

ちなみに, これのソースは以下のような感じです.

(require :nurikabe)
 
(defvar *manager* (nk:init-gui :loggingp t :threadingp t))
(defvar *win* (nk:make-window :width 300 :height 200
                              :background :white
                              :name "test window"))
(defvar *widget1* (nk:make-widget 'nk:<image-widget>
                                 :x 0 :y 0
                                 :width 150 :height 100
                                 :parent *win*
                                 :background :blue))
(defvar *widget2* (nk:make-widget 'nk:<image-widget>
                                 :x 150 :y 0
                                 :width 150 :height 100
                                 :parent *win*
                                 :background :green))
(defvar *widget3* (nk:make-widget 'nk:<image-widget>
                                  :x 0 :y 100
                                  :width 150 :height 100
                                  :parent *win*
                                  :background :red))
(defvar *widget4* (nk:make-widget 'nk:<image-widget>
                                  :x 150 :y 100
                                  :width 150 :height 100
                                  :parent *win*
                                  :background :white))
 
(nk:draw-line (nk:image-of *widget1*) 0 0 100 100)
(nk:draw-circle (nk:image-of *widget2*) 75 50 30)
(nk:draw-string (nk:image-of *widget3*) "NURIKABE" 0 0 :font-size 30)
(nk:draw-rectangle (nk:image-of *widget4*) 0 0 100 100)
 
(nk:render-widgets *win*)

snow leopard(x86-64)のsbclで普通のdlopenを利用する

日曜日, 3 月 7th, 2010

snow leopard上のsbclは大体うまく動くのですが, FFIでCの関数を呼びだしはじめると, 結構うまくいかないことがあります.

snow leopard上でsbclを使ってるひとは

(load-shared-object "/usr/X11R6/lib/libX11.dylib")
(load-shared-object "/usr/X11R6/lib/libGL.dylib")

とすると, プロセスが帰ってこなくなることが確認できます.

これは, darwinにおいて, sbclは普通のdlopenを利用してないことが原因だと思われます.

これはdarwinは標準的なELFではなく, バイナリがMach-o形式のため, dlopenを<mach-o/dyld.h>を利用してエミュレートしているのが原因だと思われます(sbcl/src/runtime/darwin-dlshim.c).

しかし, よく考えると, mac os xのgccは普通にdlopenを提供してくれているので, そっちを使うほうが良いです.

その辺のことがPHPまわりで議論されてるようです. ruby 1.8.6だloadできないけど, 1.8.7だとできるみたいな話もあるらしいです (url紛失).

以下あやしいところもありますが, このためのパッチになります.

利用してるのはcvsの最新のものです(1.0.36.13).

Index: sb-bsd-sockets/defpackage.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/contrib/sb-bsd-sockets/defpackage.lisp,v
retrieving revision 1.14
diff -r1.14 defpackage.lisp
76a77,79
>
> #+darwin
> (load-shared-object "/usr/lib/libc.dylib")
Index: contrib/sb-posix/defpackage.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/contrib/sb-posix/defpackage.lisp,v
retrieving revision 1.16
diff -r1.16 defpackage.lisp
26a27,29
>
> #+darwin
> (sb-alien:load-shared-object "/usr/lib/libc.dylib")
Index: src/code/unix-foreign-load.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/code/unix-foreign-load.lisp,v
retrieving revision 1.1
diff -r1.1 unix-foreign-load.lisp
34a35
>
69c70,71
<   (let* ((extern (extern-alien-name symbol))
---
>   (let* ((extern #!-mach-o (extern-alien-name symbol)
>                  #!+mach-o (coerce symbol 'base-string))
Index: src/runtime/Config.x86-64-darwin
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/runtime/Config.x86-64-darwin,v
retrieving revision 1.4
diff -r1.4 Config.x86-64-darwin
13,14c13,15
< LINKFLAGS += -mmacosx-version-min=10.4
< OS_SRC = bsd-os.c x86-64-bsd-os.c darwin-os.c x86-64-darwin-os.c darwin-dlshim.c darwin-langinfo.c
---
> LINKFLAGS += -mmacosx-version-min=10.4 -ldl
> #OS_SRC = bsd-os.c x86-64-bsd-os.c darwin-os.c x86-64-darwin-os.c darwin-dlshim.c darwin-langinfo.c
> OS_SRC = bsd-os.c x86-64-bsd-os.c darwin-os.c  darwin-langinfo.c
Index: src/runtime/Config.x86-64-darwin9+
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/runtime/Config.x86-64-darwin9+,v
retrieving revision 1.1
diff -r1.1 Config.x86-64-darwin9+
14c14,15
< OS_SRC = bsd-os.c x86-64-bsd-os.c darwin-os.c x86-64-darwin-os.c darwin-dlshim.c darwin-langinfo.c
---
> #OS_SRC = bsd-os.c x86-64-bsd-os.c darwin-os.c x86-64-darwin-os.c darwin-dlshim.c darwin-langinfo.c
> OS_SRC = bsd-os.c x86-64-bsd-os.c darwin-os.c x86-64-darwin-os.c darwin-langinfo.c
Index: src/runtime/x86-64-darwin-os.c
===================================================================
RCS file: /cvsroot/sbcl/sbcl/src/runtime/x86-64-darwin-os.c,v
retrieving revision 1.14
diff -r1.14 x86-64-darwin-os.c
25a26,29
> // added by garaemon
> #include <dlfcn.h>
> #include <sys/wait.h>
> #include <mach-o/dyld.h>
688a693,697
> //dummy function
> /* void darwin_waitpid(int pid, int* status, int options) { */
> /*     waitpid(pid, status, options); */
> /* } */
>
Index: tools-for-build/ldso-stubs.lisp
===================================================================
RCS file: /cvsroot/sbcl/sbcl/tools-for-build/ldso-stubs.lisp,v
retrieving revision 1.24
diff -r1.24 ldso-stubs.lisp
327,331c327,337
<                  #!-darwin
<                  '("dlclose"
<                    "dlerror"
<                    "dlopen"
<                    "dlsym")
---
>                  ;; #!-darwin
>                   '("dlclose"
>                     "dlerror"
>                     "dlopen"
>                     "dlsym")
>                   #!+darwin
>                   '("waitpid"
>                     "ptsname"
>                     "grantpt"
>                     "unlockpt")
>                   ;; for bsd-sockets...?

sb-posixとsb-bsd-socketsでlibc.dylibをloadしなくてはいけなくなったのは理由がよくわかりません… -lcはつけてるはずですが

追記:

バグレポートしておいた

https://bugs.launchpad.net/sbcl/+bug/533470

追記2:

slimeでthreadを利用してる場合は, load-shared-objectでハングする可能性があります. dlopenはmmapとかしてそうだからそのへんかな?

以下解決のためのpatch

Index: swank-sbcl.lisp
===================================================================
RCS file: /project/slime/cvsroot/slime/swank-sbcl.lisp,v
retrieving revision 1.269
diff -r1.269 swank-sbcl.lisp
71c71
<     ((member :sb-thread *features*) :spawn)
---
>     ;;((member :sb-thread *features*) :spawn)
1345c1345
< #+(and sb-thread
---
> #+(and nil sb-thread