add emms
[emacs.git] / .emacs.d / elisp / emms / lisp / emms-history.el
1 ;;; emms-history.el -- save all playlists when exiting emacs
2
3 ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Ye Wenbin <wenbinye@163.com>
6
7 ;; This file is part of EMMS.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
12 ;; any later version.
13 ;;
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18 ;;
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program; if not, write to the Free Software
21 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;; Saves all playlists when you close emacs. When you start it up again use
26 ;; M-x emms-history-load to restore all saved playlists.
27
28 ;; To use it put the following into your ~/.emacs:
29 ;;
30 ;; (require 'emms-history)
31 ;;
32 ;; If all playlists should be restored on startup add this, too:
33 ;;
34 ;; (emms-history-load)
35
36 ;;; Code:
37
38 (require 'emms)
39 (eval-when-compile
40 (require 'cl))
41
42 (defgroup emms-history nil
43 "Saving and restoring all playlists when closing/restarting
44 Emacs."
45 :prefix "emms-history-"
46 :group 'emms)
47
48 (defcustom emms-history-file (concat (file-name-as-directory emms-directory) "history")
49 "The file to save playlists in."
50 :type 'string
51 :group 'emms-history)
52
53 (defcustom emms-history-start-playing nil
54 "If non-nil emms starts playing the current track after
55 `emms-history-load' was invoked."
56 :type 'boolean
57 :group 'emms-history)
58
59 (defcustom emms-history-file-coding-system 'utf-8
60 "Coding system used for saving `emms-history-file'."
61 :type 'coding-system
62 :group 'emms-history)
63
64 (defun emms-history-save ()
65 "Save all playlists that are open in this Emacs session."
66 (interactive)
67 (when (stringp emms-history-file)
68 (let ((oldbuf emms-playlist-buffer)
69 ;; print with no limit
70 print-length print-level
71 emms-playlist-buffer playlists)
72 (save-excursion
73 (dolist (buf (emms-playlist-buffer-list))
74 (set-buffer buf)
75 (when (> (buffer-size) 0) ; make sure there is track in the buffer
76 (setq emms-playlist-buffer buf
77 playlists
78 (cons
79 (list (buffer-name)
80 (or
81 (and emms-playlist-selected-marker
82 (marker-position emms-playlist-selected-marker))
83 (point-min))
84 (save-restriction
85 (widen)
86 (nreverse
87 (emms-playlist-tracks-in-region (point-min)
88 (point-max)))))
89 playlists))))
90 (with-temp-buffer
91 (insert
92 (concat ";;; emms history -*- mode: emacs-lisp; coding: "
93 (symbol-name emms-history-file-coding-system)
94 "; -*-\n"))
95 (insert "(\n;; active playlist\n")
96 (prin1 (buffer-name oldbuf) (current-buffer))
97 (insert "\n;; playlists: ((BUFFER_NAME SELECT_POSITION TRACKS) ...)\n")
98 (prin1 playlists (current-buffer))
99 (insert "\n;; play method\n")
100 (prin1 `((emms-repeat-track . ,emms-repeat-track)
101 (emms-repeat-playlist . ,emms-repeat-playlist))
102 (current-buffer))
103 (insert "\n)")
104 (write-file emms-history-file))))))
105
106 (unless noninteractive
107 (add-hook 'kill-emacs-hook 'emms-history-save))
108
109 (defun emms-history-load ()
110 "Restore all playlists in `emms-history-file'."
111 (interactive)
112 (when (and (stringp emms-history-file)
113 (file-exists-p emms-history-file))
114 (let (history buf)
115 (with-temp-buffer
116 (emms-insert-file-contents emms-history-file)
117 (setq history (read (current-buffer)))
118 (dolist (playlist (cadr history))
119 (with-current-buffer (emms-playlist-new (car playlist))
120 (setq emms-playlist-buffer (current-buffer))
121 (if (string= (car playlist) (car history))
122 (setq buf (current-buffer)))
123 (mapc 'emms-playlist-insert-track
124 (nth 2 playlist))
125 (ignore-errors
126 (emms-playlist-select (cadr playlist)))))
127 (setq emms-playlist-buffer buf)
128 (dolist (method (nth 2 history))
129 (set (car method) (cdr method)))
130 (ignore-errors
131 (when emms-history-start-playing
132 (emms-start)))))))
133
134 (provide 'emms-history)
135 ;;; emms-history.el ends here