(top)  (memo)  (rss)
Windows 上の LTK で日本語を使うための Tips を紹介します。 CLISPごった煮 を作るときに使った手順ですが、ここにまとめておきます。
必要なもの:
以下のようになる事が目標です。
LISP 処理系 <--- パイプ (UTF-8) ---> Tcl/Tk <--- ファイルの読み書き (UTF-8) ---> ファイル
これは、パイプのストリームを開く個所で EXTERNAL-FORMAT を指定することで実現できます。 CLISP の場合は、 SYS::*DEFAULT-FILE-ENCODING* の値に従ったエンコーディングとなります。
(system::set-default-file-encoding charset:utf-8)
ここは処理系毎に違いますので、お使いの処理系のマニュアルを参照してください。
Tcl/Tk のストリームエンコーディング指定 fconfigure を使います。
(setf ltk:*init-wish-hook* (list
(lambda ()
(ltk:send-wish "fconfigure stdout -encoding utf-8")
(ltk:send-wish "fconfigure stdin -encoding utf-8"))))
初期化処理のなかで、エンコーディングの設定を行います。
LTK にパッチをあててください。
*** ltk.lisp.orig 2008-08-11 20:55:43.000000000 +0900
--- ltk.lisp 2008-08-11 20:55:51.000000000 +0900
***************
*** 137,142 ****
--- 137,143 ----
#:wish-stream
#:*wish-args*
#:*wish-pathname*
+ #:*default-file-encoding*
#:*default-ltk-debugger*
#:add-pane
#:add-separator
***************
*** 464,469 ****
--- 465,472 ----
(defvar *wish-args* '("-name" "LTK"))
+ (defvar *default-file-encoding* "utf-8")
+
(defvar *init-wish-hook* nil)
(defun dbg (fmt &rest args)
***************
*** 2304,2321 ****
(format-wish "~A delete 0.0 end;~A insert end {~A}" (widget-path text) (widget-path text) val)
val)
! (defgeneric save-text (txt filename))
! (defmethod save-text ((txt text) filename)
"save the content of the text widget into the file <filename>"
! (format-wish "set file [open {~a} \"w\"];puts $file [~a get 1.0 end];close $file;puts \"asdf\"" filename (widget-path txt))
(read-line (wish-stream *wish*))
txt)
! (defgeneric load-text (txt filename))
! (defmethod load-text((txt text) filename)
"load the content of the file <filename>"
; (format-wish "set file [open {~a} \"r\"];~a delete 1.0 end;~a insert end [read $file];close $file;puts \"asdf\"" filename (widget-path txt) (widget-path txt))
! (format-wish "set file [open {~a} \"r\"];~a delete 1.0 end;~a insert end [read $file];close $file;puts \"(:DATA asdf)\"" filename (widget-path txt) (widget-path txt))
(read-data))
;;; photo image object
--- 2307,2324 ----
(format-wish "~A delete 0.0 end;~A insert end {~A}" (widget-path text) (widget-path text) val)
val)
! (defgeneric save-text (txt filename &optional encoding))
! (defmethod save-text ((txt text) filename &optional (encoding *default-file-encoding*))
"save the content of the text widget into the file <filename>"
! (format-wish "set file [open {~a} \"w\"];fconfigure $file -encoding ~A;puts $file [~a get 1.0 end];close $file;puts \"asdf\"" filename encoding (widget-path txt))
(read-line (wish-stream *wish*))
txt)
! (defgeneric load-text (txt filename &optional encoding))
! (defmethod load-text ((txt text) filename &optional (encoding *default-file-encoding*))
"load the content of the file <filename>"
; (format-wish "set file [open {~a} \"r\"];~a delete 1.0 end;~a insert end [read $file];close $file;puts \"asdf\"" filename (widget-path txt) (widget-path txt))
! (format-wish "set file [open {~a} \"r\"];fconfigure $file -encoding ~A;~a delete 1.0 end;~a insert end [read $file];close $file;puts \"(:DATA asdf)\"" filename encoding (widget-path txt) (widget-path txt))
(read-data))
;;; photo image object
これで、 LTK からのファイル入出力が LTK::*default-file-encoding* で指定したエンコーディングになります。
posted: 2008/08/20 00:18 | permanent link to this entry | Tags: LISP