Permalink
Browse files

emacs: using auto-save-buffer.el

  • Loading branch information...
1 parent 6ca5432 commit 6912a20e597e69f5d16edfe7ccf364dcd160c5b7 @nyarla committed Jun 25, 2012
Showing with 90 additions and 1 deletion.
  1. +76 −0 config/emacs.d/elisp/auto-save-buffers.el
  2. +14 −1 dotfiles/home/share/.emacs
View
76 config/emacs.d/elisp/auto-save-buffers.el
@@ -0,0 +1,76 @@
+;;
+;; auto-save-buffers.el
+;;
+;; 元のコードは山岡克美氏が書いてくださった (ELF:01128)
+;;
+;; 使い方:
+;;
+;; (require 'auto-save-buffers)
+;; (run-with-idle-timer 0.5 t 'auto-save-buffers) ; アイドル0.5秒で保存
+;;
+;; auto-save-buffers の on/off を切り替えるためのキー定義 (C-x a s)
+;;
+;; (define-key ctl-x-map "as" 'auto-save-buffers-toggle)
+;;
+
+;; auto-save-buffers で対象とするファイル名の正規表現
+(defvar auto-save-buffers-regexp ""
+ "*Regexp that matches `buffer-file-name' to be auto-saved.")
+
+;; auto-save-buffers で除外するファイル名の正規表現
+(defvar auto-save-buffers-exclude-regexp "^$"
+ "*Regexp that matches `buffer-file-name' not to be auto-saved.")
+
+;;
+;; あるいは auto-save-buffers の引数で正規表現を指定することもできる
+;;
+;; (require 'auto-save-buffers)
+;; (run-with-idle-timer 0.5 t 'auto-save-buffers "\\.c$" "^$") ; .c だけ対象
+;; (run-with-idle-timer 0.5 t 'auto-save-buffers "" "\\.h$") ; .h だけ除外
+;;
+
+;; nil ならセーブしない (タイマーは回ったまま)
+(defvar auto-save-buffers-active-p t
+ "If non-nil, `auto-save-buffers' saves buffers.")
+
+;; 省略可能の引数で、include/exclude 用の正規表現を指定できる
+(defun auto-save-buffers (&rest regexps)
+ "Save buffers if `buffer-file-name' matches `auto-save-buffers-regexp'."
+ (let ((include-regexp (or (car regexps) auto-save-buffers-regexp))
+ (exclude-regexp (or (cadr regexps) auto-save-buffers-exclude-regexp))
+ (buffers (buffer-list)))
+ (save-excursion
+ (while buffers
+ (set-buffer (car buffers))
+ (if (and buffer-file-name
+ auto-save-buffers-active-p
+ (buffer-modified-p)
+ (not buffer-read-only)
+ (string-match include-regexp buffer-file-name)
+ (not (string-match exclude-regexp buffer-file-name))
+ (file-writable-p buffer-file-name))
+ (save-buffer))
+ (setq buffers (cdr buffers))))))
+
+;; auto-save-buffers の on/off をトグルで切り替える
+;; Based on the code by Yoshihiro (いやな日記 2004-03-23)
+(defun auto-save-buffers-toggle ()
+ "Toggle `auto-save-buffers'"
+ (interactive)
+ (if auto-save-buffers-active-p
+ (setq auto-save-buffers-active-p nil)
+ (setq auto-save-buffers-active-p t))
+ (if auto-save-buffers-active-p
+ (message "auto-save-buffers on")
+ (message "auto-save-buffers off")))
+
+;;
+;; Emacs 21 以降で Makefile の編集時に "Suspicious line XXX. Save anyway"
+;; というプロンプトを出さないようにするためのおまじない
+;;
+(add-hook 'makefile-mode-hook
+ (function (lambda ()
+ (fset 'makefile-warn-suspicious-lines 'ignore))))
+
+(provide 'auto-save-buffers)
+
View
15 dotfiles/home/share/.emacs
@@ -38,7 +38,16 @@
(pc-selection-mode 1)
;; enable *scratch* buffer logging
-(require 'scratch-log)
+(require 'auto-save-buffers)
+(run-with-idle-timer 0.5 t 'auto-save-buffers "" "\\.txt$")
+(defvar my-scratch-filename
+ (expand-file-name "~/.emacs.d/.scratch"))
+(defun my-init-scratch ()
+ (set-buffer (get-buffer "*scratch*"))
+ (when (file-exists-p my-scratch-filename)
+ (erase-buffer)
+ (insert-file-contents my-scratch-filename))
+ (setq buffer-file-name my-scratch-filename))
;; mode definition for file extensions
(setq auto-mode-alist
@@ -108,3 +117,7 @@
;; -- Application settings --
+
+;; --
+
+(my-init-scratch)

0 comments on commit 6912a20

Please sign in to comment.