-
Notifications
You must be signed in to change notification settings - Fork 1
/
save-visited-files.el
89 lines (82 loc) · 3.34 KB
/
save-visited-files.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
;; Save-visited-files.el - save opened files across sessions
;; Copyright (C) 2009 Nathaniel Flath <[email protected]>
;; Version: 1.0
;;
;; Commentary Save-visited-files is a lightweight version of
;; Desktop.el that only save the files you have open(currently). This
;; was created because I couldn't ever get Desktop to work and wanted
;; to persist open files across sessions. This file is the result.
;;
;; To install, put this file somewhere in your load-path and add the
;; following to your .emacs file:
;;
;; (require 'savebuffers)
;; (open-visited-files)
;; (auto-save-visited-files 1)
;;
;; This will load the set of saved files on startup, as well as
;; updating this list whenever a file is opened or a buffer is closed.
;; This does not wait to save on closing emacs because I wanted it to
;; be useful even if emacs crashed. To save the visited files at any
;; time, you can call M-x save-visited-files. M-x Open-visited-files
;; will open all files saved this way. To turn off the saving of
;; files, you need to run (save-visited-files t).
;;
;;
;; Changelog:
;; 1.0
;; * Initial Release
;;
;; Code:
(defvar save-visited-files-location "~/.emacs.d/.emacs-opened-files"
"Location of the file that contains the list of previously visited files")
(defvar save-visited-files-interval 60
"How oftern, in seconds, to save opened files")
(defvar save-visited-files-timer nil
"timer controlling save-visited-files")
(defun save-visited-files ()
"Saves the location of all open files to be re-opened when
open-visited-files is called."
(interactive)
(let ((buf (current-buffer)))
(unless (string-equal (buffer-name buf) " *Minibuf-1*")
(switch-to-buffer "*Save Visited*")
(erase-buffer)
(mapcar '(lambda (x) (insert x "\n"))
(remove-if '(lambda (x)
(or (string-equal save-visited-files-location x)
(eq nil x))) (mapcar 'buffer-file-name (buffer-list))))
(write-file save-visited-files-location nil)
(kill-buffer (get-buffer "*Save Visited*"))
(switch-to-buffer buf))))
(defun open-visited-files ()
"Opens all files that were saved by save-visited-files."
(interactive)
(find-file save-visited-files-location)
(beginning-of-buffer)
(let ((buf (current-buffer)))
(while (not (eq (point) (point-max)))
(let ((point (point)))
(end-of-line)
(find-file (buffer-substring point (point)))
(switch-to-buffer buf)
(next-line)
(beginning-of-line)))
(kill-buffer buf)))
(defun save-visited-files-mode (&optional arg)
"If arg is non-nil, start saving opened files on find-file and
kill-buffer. Otherwise, turn it off. With no arg, toggle
save-visited-files-mode."
(interactive)
(cond
((eq nil arg) (if (not save-visited-files-timer)
(setq save-visited-files-timer (run-with-timer 0 60 'save-visited-files))
(progn
(cancel-timer save-visited-files-timer)
(setq save-visited-files-timer nil))))
((> arg 0) (when (not save-visited-files-timer)
(setq save-visited-files-timer (run-with-timer 0 60 'save-visited-files))))
((< arg 0) (when (save-visited-files-timer)
(cancel-timer save-visited-files-timer)
(setq save-visited-files-timer nil)))))
(provide 'save-visited-files)