forked from ChillarAnand/real-auto-save
-
Notifications
You must be signed in to change notification settings - Fork 0
/
real-auto-save.el
120 lines (95 loc) · 3.8 KB
/
real-auto-save.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
;;; real-auto-save.el --- Automatically save your all your buffers/files at regular intervals.
;; Copyright (C) 2008-2015, Chaoji Li, Anand Reddy Pandikunta
;; Author: Chaoji Li <lichaoji AT gmail DOT com>
;; Anand Reddy Pandikunta <anand21nanda AT gmail DOT com>
;; Version: 0.4
;; Date: January 27, 2015
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Put this file in a folder where Emacs can find it.
;;
;; Add following lines to your .emacs initialization file
;; to enable auto save in all programming modes.
;;
;; (require 'real-auto-save)
;; (add-hook 'prog-mode-hook 'real-auto-save-mode)
;;
;;
;; Auto save interval is 10 seconds by default.
;; You can change it to whatever value you want at any point.
;;
;; (setq real-auto-save-interval 5) ;; in seconds
;;
;;
;;; Code:
(defgroup real-auto-save nil
"Save buffers automatically."
:group 'convenience
:prefix "real-auto-save-")
(defcustom real-auto-save-interval 10
"Time interval of real auto save."
:type 'integer
:group 'real-auto-save)
(defvar real-auto-save-buffers-list nil
"List of buffers that will be saved automatically.")
(defvar real-auto-save-timer nil
"Real auto save timer.")
(defun real-auto-save-start-timer ()
"Start real-auto-save-timer."
(setq real-auto-save-timer
(run-at-time
(time-add (current-time) (seconds-to-time real-auto-save-interval))
real-auto-save-interval 'real-auto-save-buffers)))
(defun real-auto-save-restart-timer ()
"Restart real-auto-save-timer."
(if real-auto-save-timer
(cancel-timer real-auto-save-timer))
(real-auto-save-start-timer))
(defmacro with-suppressed-message (&rest body)
"Suppress new messages temporarily in the echo area and the `*Messages*' buffer while BODY is evaluated."
(let ((message-log-max nil))
`(with-temp-message (or (current-message) "") ,@body)))
(defun real-auto-save-buffers ()
"Automatically save all buffers in real-auto-save-buffers-list."
(progn
(save-current-buffer
(dolist (elem real-auto-save-buffers-list)
(if (get-buffer elem)
(progn
(set-buffer elem)
(if (buffer-modified-p)
(with-suppressed-message (save-buffer))))
(delete elem real-auto-save-buffers-list))))
(real-auto-save-restart-timer)))
(defun real-auto-save-remove-buffer-from-list ()
"If a buffer is killed, remove it from real-auto-save-buffers-list."
(if (member (current-buffer) real-auto-save-buffers-list)
(setq real-auto-save-buffers-list
(delete (current-buffer) real-auto-save-buffers-list))))
(define-minor-mode real-auto-save-mode
"Save your buffers automatically."
:lighter " RAS"
:keymap nil
:version "0.5"
(when (not real-auto-save-mode) ;; OFF
(when (buffer-file-name)
(real-auto-save-remove-buffer-from-list)))
(when real-auto-save-mode ;; ON
(if (buffer-file-name)
(progn
(real-auto-save-restart-timer)
(add-to-list 'real-auto-save-buffers-list (current-buffer))
(add-hook 'kill-buffer-hook 'real-auto-save-remove-buffer-from-list)))))
(provide 'real-auto-save)
;;; real-auto-save.el ends here