aboutsummaryrefslogtreecommitdiff
path: root/emms-history.el
blob: 973113df7212cdeaa348c90b899e2ae1443e3f8a (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
;;; emms-history.el -- save all playlists when exiting emacs  -*- lexical-binding: t; -*-

;; Copyright (C) 2006-2021  Free Software Foundation, Inc.
;;
;; Author: Ye Wenbin <wenbinye@163.com>

;; This file is part of EMMS.

;; This program 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 3, or (at your option)
;; any later version.
;;
;; This program 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 this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;; Saves all playlists when you close emacs.  When you start it up again use
;; M-x emms-history-load to restore all saved playlists.

;; To use it put the following into your ~/.emacs:
;;
;;   (require 'emms-history)
;;
;; If all playlists should be restored on startup add this, too:
;;
;;   (emms-history-load)

;;; Code:

(require 'emms)

(defgroup emms-history nil
  "Saving and restoring all playlists when closing/restarting
Emacs."
  :prefix "emms-history-"
  :group 'emms)

(defcustom emms-history-file (concat (file-name-as-directory emms-directory) "history")
  "The file to save playlists in."
  :type   'string)

(defcustom emms-history-start-playing nil
  "If non-nil emms starts playing the current track after
`emms-history-load' was invoked."
  :type   'boolean)

(defcustom emms-history-file-coding-system 'utf-8
  "Coding system used for saving `emms-history-file'."
  :type 'coding-system)

(defun emms-history-save ()
  "Save all playlists that are open in this Emacs session."
  (interactive)
  (when (stringp emms-history-file)
    (let ((oldbuf emms-playlist-buffer)
          ;; print with no limit
          print-length print-level
          emms-playlist-buffer playlists)
      (save-excursion
        (dolist (buf (emms-playlist-buffer-list))
          (set-buffer buf)
          (when (> (buffer-size) 0) ; make sure there is track in the buffer
            (setq emms-playlist-buffer buf
                  playlists
                  (cons
                   (list (buffer-name)
                         (or
                          (and emms-playlist-selected-marker
                               (marker-position emms-playlist-selected-marker))
                          (point-min))
                         (save-restriction
                           (widen)
                           (nreverse
                            (emms-playlist-tracks-in-region (point-min)
                                                            (point-max)))))
                   playlists))))
        (with-temp-buffer
          (insert
           (concat ";;; emms history -*- mode: emacs-lisp; coding: "
                   (symbol-name emms-history-file-coding-system)
                   "; -*-\n"))
          (insert "(\n;; active playlist\n")
          (prin1 (buffer-name oldbuf) (current-buffer))
          (insert "\n;; playlists: ((BUFFER_NAME SELECT_POSITION TRACKS) ...)\n")
          (prin1 playlists (current-buffer))
          (insert "\n;; play method\n")
          (prin1 `((emms-repeat-track . ,emms-repeat-track)
                   (emms-repeat-playlist . ,emms-repeat-playlist))
                 (current-buffer))
          (insert "\n)")
          (write-file emms-history-file))))))

(unless noninteractive
  (add-hook 'kill-emacs-hook 'emms-history-save))

(defun emms-history-load ()
  "Restore all playlists in `emms-history-file'."
  (interactive)
  (when (and (stringp emms-history-file)
             (file-exists-p emms-history-file))
    (let (history buf)
      (with-temp-buffer
        (emms-insert-file-contents emms-history-file)
        (setq history (read (current-buffer)))
        (dolist (playlist (cadr history))
          (with-current-buffer (emms-playlist-new (car playlist))
            (setq emms-playlist-buffer (current-buffer))
            (if (string= (car playlist) (car history))
                (setq buf (current-buffer)))
            (mapc 'emms-playlist-insert-track
                  (nth 2 playlist))
	    (run-hooks 'emms-playlist-source-inserted-hook)
            (ignore-errors
              (emms-playlist-select (cadr playlist)))))
        (setq emms-playlist-buffer buf)
        (dolist (method (nth 2 history))
          (set (car method) (cdr method)))
        (ignore-errors
          (when emms-history-start-playing
            (emms-start)))))))

(provide 'emms-history)
;;; emms-history.el ends here