           (let ((e (aref chars i)))
             (insert (kiirogaa-format-entry (car e) (cadr e))))
           (setq i (1+ i))))
-      (write-file filename t))
+      (if visit
+          (write-file filename t)
+        (write-region nil nil filename nil []))
+      )
+    ))
+
+(defun kiirogaa-save-buffer-with-timestamp ()
+  (interactive)
+  (let* ((name (buffer-name))
+         (pair (assoc name kiirogaa-log-buffer-alist)))
+    (when pair
+      (kiirogaa-write-buffer-with-timestamp
+       (concat kiirogaa-log-base-dir
+               (concat name ".timestamp")))
+      )
     ))
 
 (defun kiirogaa-pre-command-hook ()
   (setq -kiirogaa-tmp-last-point (point)))
 
 (defun kiirogaa-post-self-insert-hook ()
-  (let* ((alist (assq (current-buffer) kiirogaa-log-buffer-alist)))
-    (when alist
+  (let* ((name (buffer-name))
+         (pair (assoc name kiirogaa-log-buffer-alist)))
+    (when pair
       (let ((time (float-time))
             (p -kiirogaa-tmp-last-point)
             (c last-command-event))

          (set-buffer (get-buffer-create -kiirogaa-tmp-buffer-name))
          (erase-buffer)
          (insert (kiirogaa-format-entry c time))
          (write-region nil nil (cdr alist) t []))
        ))
    ))
(add-hook 'pre-command-hook (function kiirogaa-pre-command-hook))
(add-hook 'post-self-insert-hook (function kiirogaa-post-self-insert-hook))

(defun kiirogaa-start-logging (filename)
  (interactive (list
                (read-file-name "Log filename: " kiirogaa-log-base-dir nil nil
                                (concat (buffer-name) ".keylog"))))
  (let ((pair (cons (buffer-name) filename)))
    (setq kiirogaa-log-buffer-alist
          (cons pair kiirogaa-log-buffer-alist))
    ))

(defun kiirogaa-stop-logging ()
  (interactive)
  (let ((name (buffer-name)))
    (setq kiirogaa-log-buffer-alist
          (remove-if (lambda (pair) (string= name (car pair)))
                     kiirogaa-log-buffer-alist))
    ))

(defun kiirogaa-write-buffer-with-timestamp (filename &optional visit)
  (interactive "FWrite filename: ")
  (let ((chars (make-vector (buffer-size) nil)))
    (let ((i 0))
      (while (< i (length chars))
