先日,先輩に連れられて越後湯沢まで旅行に行ってきた.
駅構内(駅ビル?)で多数の日本酒を試飲(利き酒)できた.新潟すげぇ. 10種類以上試飲した.
宿は白銀閣というところだった.
以下,購入した酒リスト
- 鶴齢 純米大吟醸(青木酒造株式会社/新潟) - ほだれ酒(御神酒) 中身は越の鶴らしい - さむらい(玉川酒造株式会社/新潟) 46度もある
最近読み終わった本.
「取り合えずビール」と言うばかりでは良くないと思わされる一冊。
他の人がやってることを自分がやる必要はない、というスタンスがかっこいい。それで成功してきてるところがさらにすごい。
.emacsを晒す(その2)
プログラミング関係の設定。
;;Compile C-c c
(define-key mode-specific-map "c" 'compile)
(define-key mode-specific-map "n" 'next-error)
(setq compilation-window-height 12)
;;gdb Emacsからはろくに使ってないけど。
(setq gdb-many-windows t)
(setq gdb-use-sepapate-io-buffer t)
(setq gdb-find-source-frame nil)
;;indent style for c
(add-hook 'c-mode-hook
'(lambda ()
(c-set-style "linux")
(setq c-basic-offset 2)
(setq tab-width c-basic-offset)))
;;indent style for c++
(add-hook 'c++-mode-hook
'(lambda ()
(c-set-style "linux")
(setq c-basic-offset 2)
(setq tab-width c-basic-offset)))
;; C# mode ほんの少しだけLinux(mono)で触ったので。
(add-hook 'csharp-mode-hook (lambda ()
(setq c-basic-offset 4
tab-width 4
indent-tabs-mode nil)))
(autoload 'csharp-mode "csharp-mode" "C# editing mode." t)
(nconc auto-mode-alist '(("\\.cs$" . csharp-mode)))
;;haskell-mode 一応GHCはインストールされてるけど使われてない。
(load "~/emacslib/haskell-mode-2.4/haskell-site-file")
(setq auto-mode-alist
(append auto-mode-alist
'(("\\.[hg]s$" . haskell-mode)
("\\.hi$" . haskell-mode)
("\\.l[hg]s$" . literate-haskell-mode))))
(autoload 'haskell-mode "haskell-mode"
"Major mode for editing Haskell scripts." t)
(autoload 'literate-haskell-mode "haskell-mode"
"Major mode for editing literate Haskell scripts." t)
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
;;(add-hook 'haskell-mode-hook 'turn-on-haskell-simple-indent)
(add-hook 'haskell-mode-hook 'font-lock-mode)
(add-hook 'haskell-mode-hook 'imenu-add-menubar-index)
;;(add-hook 'haskell-mode-hook 'turn-on-haskell-ghc)
(setq haskell-program-name "/usr/bin/ghci")
;;;;common lisp & slime
;;;;slimeは3.0アルファ(?)と書かれたリファレンスを見て設定した
(setq inferior-lisp-program "/usr/local/bin/sbcl")
(add-to-list 'load-path "/home/kurohuku/emacslib/slime/")
(add-to-list 'load-path "/home/kurohuku/emacslib/slime/contrib/")
(setq slime-backend (expand-file-name
"~/emacslib/slime/swank-loader.lisp"))
(setq slime-truncate-lines nil)
(setq slime-enable-evaluate-in-emacs t)
(setq inferior-lisp-mode-hook (lambda () (inferior-slime-mode t)))
(require 'slime)
(slime-setup '(inferior-slime slime-repl slime-c-p-c slime-xref-browser))
;; '(
;; inferior-slime
;; slime-asdf
;; slime-autodoc
;; slime-banner
;; slime-c-p-c
;; slime-editing-commands
;; slime-fancy-inspector
;; slime-fancy
;; slime-fuzzy
;; ; slime-highlight-edits
;; slime-parse
;; slime-presentation-streams
;; slime-presentations
;; slime-references
;; slime-scratch
;; slime-tramp
;; ; slime-typeout-frame
;; slime-xref-browser
;; ))
(setq slime-net-coding-system 'utf-8-unix)
(add-hook 'lisp-mode-hook (lambda ()
(slime-mode t)
(show-paren-mode t)
(global-set-key "\C-cH" 'hyperspec-lookup)))
;;slimeで日本語を使うための設定.Lisp側の対応も必要
(setq slime-net-coding-system 'utf-8-unix)
;;;;coreファイルから起動.起動時に自前の関数を定義するutil.lispを読み込む。
(setq slime-lisp-implementations
'((sbcl
("/usr/local/bin/sbcl"
"--core" "/home/kurohuku/emacslib/sbcl.core-with-swank")
:init (lambda (port-file _)
(format "(progn
(load \"/home/kurohuku/emacslib/util.lisp\")
(setf swank::*coding-system* \"utf-8-unix\")
(swank:start-server %S))\n" port-file))
:coding-system utf-8-unix)))
;;;slimeのインデントを変更
(add-hook 'slime-mode-hook
(lambda ()
(setq lisp-indent-function 'common-lisp-indent-function)))
;; Additional definitions by Pierpaolo Bernardi.
(defun cl-indent (sym indent)
(put sym 'common-lisp-indent-function
(if (symbolp indent)
(get indent 'common-lisp-indent-function)
indent)))
(cl-indent 'if '1)
(cl-indent 'generic-flet 'flet)
(cl-indent 'generic-labels 'labels)
(cl-indent 'with-accessors 'multiple-value-bind)
(cl-indent 'with-added-methods '((1 4 ((&whole 1))) (2 &body)))
(cl-indent 'with-condition-restarts '((1 4 ((&whole 1))) (2 &body)))
(cl-indent 'with-simple-restart '((1 4 ((&whole 1))) (2 &body)))
;;;HyperSpec
(require 'hyperspec)
(setq common-lisp-hyperspec-root
(concat "file://"
(expand-file-name "~/emacslib/HyperSpec/"))
common-lisp-hyperspec-symbol-table
(expand-file-name "~/emacslib/HyperSpec/Data/MapSym.txt"))
;; HyperSpecをw3mで見る
(defadvice common-lisp-hyperspec
(around hyperspec-lookup-w3m () activate)
(let* ((window-configuration (current-window-configuration))
(browse-url-browser-function
`(lambda (url new-window)
(w3m-browse-url url nil)
(let ((hs-map (copy-keymap w3m-mode-map)))
(define-key hs-map (kbd "q")
(lambda ()
(interactive)
(kill-buffer nil)
(set-window-configuration ,window-configuration)))
(use-local-map hs-map)))))
ad-do-it))
(add-hook 'inferior-lisp-mode-hook (lambda () (inferior-slime-mode t)))
色々な場所で調べたもののコピペばかりなはずだが、参考にしたものがどこにあるのかまでは覚えていない。
ただ、CommonLispの設定あたりが消え去ったらやる気が著しく削がれるのは間違いないのでメモメモ。
Common Lispのwith-output-to-stringやwith-input-from-stringは便利なのだが、文字列ストリームは名前の通り文字しか読み書きできない。
バイナリの読み書きもファイルを開かずに、このように書いてみたい。
(with-binary-stream (s)
(write-byte 2 s)
(write-byte 3 s)
(values (read-byte s) (read-byte s)))
少し調べてみたら、Gray Streamというものを使って実装している人がいた。単純なものなら結構簡単に書けそうだ。
良い機会なので、名前だけは聞いたことのあったGray Streamとやらを利用したコードを書いてみた。
(defpackage virtual-binary-stream
(:use :cl :sb-gray)
(:nicknames :vbstream)
(:export virtual-binary-stream
make-virtual-binary-stream
with-virtual-binary-stream))
(in-package virtual-binary-stream)
;;;Queueの実装:carがlistの先頭cons,cdrが最終Consを指す
;;;ANSI Common Lispより
(defun make-queue ()
(cons nil nil))
(defun enqueue (obj q)
(if (null (car q))
(setf (cdr q)
(setf (car q) (list obj)))
(setf (cdr (cdr q)) (list obj)
(cdr q) (cdr (cdr q))))
(car q))
(defun dequeue (q)
(pop (car q)))
(defclass virtual-binary-stream (fundamental-stream)
((queue :initform (make-queue) :accessor queue-of)))
(defmethod stream-element-type ((stream virtual-binary-stream))
'virtual-binary)
(defmethod close ((stream virtual-binary-stream) &key abort)
(declare (ignore abort))
(setf (queue-of stream) nil))
(defmethod stream-read-byte ((stream virtual-binary-stream))
(dequeue (queue-of stream)))
(defmethod stream-read-char ((stream virtual-binary-stream))
(code-char (stream-read-byte stream)))
(defmethod stream-write-byte ((stream virtual-binary-stream) integer)
(enqueue integer (queue-of stream)))
(defmethod stream-write-char ((stream virtual-binary-stream) character)
(enqueue (char-code character) (queue-of stream)))
(defun make-virtual-binary-stream (&optional (initial-element-list nil))
(let ((stream (make-instance 'virtual-binary-stream)))
(dolist (i initial-element-list)
(stream-write-byte stream i))
stream))
(defmacro with-virtual-binary-stream
((stream &optional initial-element-list) &body body)
`(let ((,stream (make-virtual-binary-stream ,initial-element-list)))
,@body))
キューはANSI Common Lispに書いてあったものを使った。リストでキューを表現する方法が格好良い。
これでwrite-byte,read-byteを使えるようになった。ついでにread-char,write-charも書いた。
>(vbstream:with-virtual-binary-stream (s)
(write-byte 2 s)
(write-char #\A s)
(list (read-byte s) (read-byte s)))
(2 65)
C++でFDCとお話しする事に疲れたから、逃避するためにLispプログラミング。
実践CommonLispに、バイナリファイルのパースという章があったかと思うが、自分の脳みそで考えるのが大事だろうということで書いてみる。
読み込みのみ。
(defmethod read-binary ((type (eql :u8)) stream)
(read-byte stream))
(defmethod read-binary ((type (eql :u16)) stream)
(let ((n 0))
;;リトルエンディアン
(setf (ldb (byte 8 0) n) (read-byte stream))
(setf (ldb (byte 8 8) n) (read-byte stream))
n))
(defmethod read-binary ((type (eql :u32)) stream)
(let ((n 0))
;;リトルエンディアン
(setf (ldb (byte 8 0) n) (read-byte stream))
(setf (ldb (byte 8 8) n) (read-byte stream))
(setf (ldb (byte 8 16) n) (read-byte stream))
(setf (ldb (byte 8 24) n) (read-byte stream))
n))
(defmacro define-binary (name &body body)
(let ((obj (gensym)))
`(progn
(defclass ,name ()
,(mapcar
#'(lambda (clause)
`(,(first clause)
:accessor ,(first clause)))
body))
(defmethod read-binary ((type (eql ',name)) stream)
(let ((,obj (make-instance ',name)))
,@(mapcar
#'(lambda (clause)
`(setf (,(first clause) ,obj)
,(if (third clause)
`(loop :repeat ,(third clause)
:collect (read-binary ,(second clause) stream))
`(read-binary ,(second clause) stream))))
body)
,obj))
)))
;;;定義
(define-binary bpb ;Bios Parameter Block
(jmp :u8 2)
(nop :u8)
(oem-id :u8 8)
(bytes-per-sector :u16) ;一般に512byteか1024byte
(sectors-per-cluster :u8) ;1クラスタが何セクタか
(reserved-sectors :u16) ;bpbからみたFAT開始位置(相対セクタ)
(total-fats :u8) ;FATはいくつかるか。普通2つ。
(max-root-entries :u16) ;ルートディレクトリエントリがいくつあるか
(total-sectors :u16) ;全セクタ数
(media-descriptor :u8)
(sectors-per-fat :u16) ;1つのFATが何セクタか
(sectors-per-track :u16) ;1トラックは何セクタか
(num-heads :u16) ;ヘッドはいくつあるか
(hidden-sector :u32)
(total-sectors-large :u32))
(define-binary fat12-boot-record
(bpb-record 'bpb)
(drive-number :u8) ;0x00=Floppy 0x80=HardDisk
(flags :u8)
(signature :u8) ;0x29
(volume-id :u32)
(volume-label :u8 11)
(system-id :u8 8)
(boot-code :u8 448)
(bootable-partition-signature :u16))
試しに読み込んでみる。
>(with-open-file (s "/home/kurohuku/floppy.img"
:direction :input :element-type '(unsigned-byte 8))
(read-binary 'fat12-boot-record s))
>(describe *)
#<FAT12-BOOT-RECORD {BBE68B1}>
[standard-object]
Slots with :INSTANCE allocation:
BPB-RECORD = #<BPB {BCA4689}>
DRIVE-NUMBER = 0
FLAGS = 0
SIGNATURE = 41
VOLUME-ID = 1252133017
VOLUME-LABEL = (32 32 32 32 32 32 32 32 32 32 32)
SYSTEM-ID = (70 65 84 49 50 32 32 32)
BOOT-CODE = (3 2 255 0 0 128 37 0 0 0 0 8 250 235 7 246 194 128 117 2 178 128 234..
BOOTABLE-PARTITION-SIGNATURE = 43605
>(map 'string #'code-char (oem-id (bpb-record **)))
"mkdosfs^@" ;^@は#\nul
>(format nil "~X" (bootable-partition-signature ***))
"AA55"
最近読み終わった本.
印象に残ったセリフ 「それらを略語で呼ぶようになって,それに慣れてしまうと,本来の意味が失われることがある (中略) O・Jが何かわかるか?」 -> 「オレンジジュースのことだ」