Skip to content

Instantly share code, notes, and snippets.

@papaeye
Created July 4, 2012 13:08
Show Gist options
  • Save papaeye/3047249 to your computer and use it in GitHub Desktop.
Save papaeye/3047249 to your computer and use it in GitHub Desktop.
A patch for auto-save-buffers minor mode and more
diff --git a/auto-save-buffers.el b/auto-save-buffers.el
index 01d0b04..53ed372 100644
--- a/auto-save-buffers.el
+++ b/auto-save-buffers.el
@@ -6,32 +6,74 @@
;; 使い方:
;;
;; (require 'auto-save-buffers)
-;; (run-with-idle-timer 0.5 t 'auto-save-buffers) ; アイドル0.5秒で保存
+;; (auto-save-buffers-mode)
;;
;; auto-save-buffers の on/off を切り替えるためのキー定義 (C-x a s)
;;
-;; (define-key ctl-x-map "as" 'auto-save-buffers-toggle)
+;; (define-key ctl-x-map "as" 'auto-save-buffers-mode)
;;
+;;; Change Log:
+
+;; 2012-07-17 Clean up usage documentation
+;; 2012-07-17 Support buffer local `auto-save-buffers-mode' variable
+;; 2012-07-11 Fix working with `after-save-hook'
+;; 2012-07-06 Fix the byte compilation issue. Don't use advice for now
+;; 2012-07-05 Use defcustom for customizable variables
+;; 2012-07-04 Define minor mode
+;; 2012-07-04 Use advice based on http://homepage3.nifty.com/oatu/emacs/misc.html#asb
+
+
+(defgroup auto-save-buffers nil
+ "Auto-saving buffers."
+ :group 'auto-save)
+
;; auto-save-buffers で対象とするファイル名の正規表現
-(defvar auto-save-buffers-regexp ""
- "*Regexp that matches `buffer-file-name' to be auto-saved.")
+(defcustom auto-save-buffers-regexp ""
+ "Regexp that matches `buffer-file-name' to be auto-saved."
+ :type 'regexp
+ :group 'auto-save-buffers)
;; auto-save-buffers で除外するファイル名の正規表現
-(defvar auto-save-buffers-exclude-regexp "^$"
- "*Regexp that matches `buffer-file-name' not to be auto-saved.")
+(defcustom auto-save-buffers-exclude-regexp "^$"
+ "Regexp that matches `buffer-file-name' not to be auto-saved."
+ :type 'regexp
+ :group 'auto-save-buffers)
+
+(defcustom auto-save-buffers-delay 0.5
+ "Seconds of idle time after auto-saving buffers."
+ :type 'number
+ :group 'auto-save-buffers)
-;;
-;; あるいは 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.")
+(defvar auto-save-buffers-mode nil)
+(put 'auto-save-buffers-mode 'safe-local-variable 'booleanp)
+
+(defvar auto-save-buffers-idle-timer nil)
+
+(defvar auto-save-buffers--write-region-with-message-function
+ (symbol-function 'write-region))
+
+(defvar auto-save-buffers--write-region-without-message-function
+ (lambda (start end filename &optional append visit lockname mustbenew)
+ (funcall auto-save-buffers--write-region-with-message-function
+ start end filename append
+ (if (or (stringp visit) (null visit))
+ visit
+ 'no-message)
+ lockname mustbenew)))
+
+(defun auto-save-buffers--set-buffer-modified ()
+ (set-visited-file-modtime)
+ (set-buffer-modified-p nil))
+
+(defun auto-save-buffers--save-buffer ()
+ (unwind-protect
+ (progn
+ (add-hook 'after-save-hook
+ 'auto-save-buffers--set-buffer-modified nil t)
+ (basic-save-buffer))
+ (remove-hook 'after-save-hook 'auto-save-buffers--set-buffer-modified t)))
;; 省略可能の引数で、include/exclude 用の正規表現を指定できる
(defun auto-save-buffers (&rest regexps)
@@ -39,30 +81,35 @@
(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")))
+ (unwind-protect
+ (save-excursion
+ (fset 'write-region
+ auto-save-buffers--write-region-without-message-function)
+ (while buffers
+ (set-buffer (car buffers))
+ (when (and auto-save-buffers-mode
+ buffer-file-name
+ (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))
+ (auto-save-buffers--save-buffer))
+ (setq buffers (cdr buffers))))
+ (fset 'write-region
+ auto-save-buffers--write-region-with-message-function))))
+
+;;;###autoload
+(define-minor-mode auto-save-buffers-mode nil
+ :global t
+ :require 'auto-save-buffers
+ (if auto-save-buffers-idle-timer
+ (cancel-timer auto-save-buffers-idle-timer))
+ (setq auto-save-buffers-idle-timer nil)
+ (if auto-save-buffers-mode
+ (setq auto-save-buffers-idle-timer
+ (run-with-idle-timer auto-save-buffers-delay t
+ 'auto-save-buffers))))
;;
;; Emacs 21 以降で Makefile の編集時に "Suspicious line XXX. Save anyway"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment