fordon_greeman / voidfiles

Branch: 2079ec5
Switch branches/tags
main
theidioticdev add emacs 2079ec5 21h ago
131 lines · 4.7 KB Raw Blame History
1 ;;; emms-history.el -- save all playlists when exiting emacs -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2006-2021 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
40 (defgroup emms-history nil
41 "Saving and restoring all playlists when closing/restarting
42 Emacs."
43 :prefix "emms-history-"
44 :group 'emms)
45
46 (defcustom emms-history-file (concat (file-name-as-directory emms-directory) "history")
47 "The file to save playlists in."
48 :type 'string)
49
50 (defcustom emms-history-start-playing nil
51 "If non-nil emms starts playing the current track after
52 `emms-history-load' was invoked."
53 :type 'boolean)
54
55 (defcustom emms-history-file-coding-system 'utf-8
56 "Coding system used for saving `emms-history-file'."
57 :type 'coding-system)
58
59 (defun emms-history-save ()
60 "Save all playlists that are open in this Emacs session."
61 (interactive)
62 (when (stringp emms-history-file)
63 (let ((oldbuf emms-playlist-buffer)
64 ;; print with no limit
65 print-length print-level
66 emms-playlist-buffer playlists)
67 (save-excursion
68 (dolist (buf (emms-playlist-buffer-list))
69 (set-buffer buf)
70 (when (> (buffer-size) 0) ; make sure there is track in the buffer
71 (setq emms-playlist-buffer buf
72 playlists
73 (cons
74 (list (buffer-name)
75 (or
76 (and emms-playlist-selected-marker
77 (marker-position emms-playlist-selected-marker))
78 (point-min))
79 (save-restriction
80 (widen)
81 (nreverse
82 (emms-playlist-tracks-in-region (point-min)
83 (point-max)))))
84 playlists))))
85 (with-temp-buffer
86 (insert
87 (concat ";;; emms history -*- mode: emacs-lisp; coding: "
88 (symbol-name emms-history-file-coding-system)
89 "; -*-\n"))
90 (insert "(\n;; active playlist\n")
91 (prin1 (buffer-name oldbuf) (current-buffer))
92 (insert "\n;; playlists: ((BUFFER_NAME SELECT_POSITION TRACKS) ...)\n")
93 (prin1 playlists (current-buffer))
94 (insert "\n;; play method\n")
95 (prin1 `((emms-repeat-track . ,emms-repeat-track)
96 (emms-repeat-playlist . ,emms-repeat-playlist))
97 (current-buffer))
98 (insert "\n)")
99 (write-file emms-history-file))))))
100
101 (unless noninteractive
102 (add-hook 'kill-emacs-hook 'emms-history-save))
103
104 (defun emms-history-load ()
105 "Restore all playlists in `emms-history-file'."
106 (interactive)
107 (when (and (stringp emms-history-file)
108 (file-exists-p emms-history-file))
109 (let (history buf)
110 (with-temp-buffer
111 (emms-insert-file-contents emms-history-file)
112 (setq history (read (current-buffer)))
113 (dolist (playlist (cadr history))
114 (with-current-buffer (emms-playlist-new (car playlist))
115 (setq emms-playlist-buffer (current-buffer))
116 (if (string= (car playlist) (car history))
117 (setq buf (current-buffer)))
118 (mapc 'emms-playlist-insert-track
119 (nth 2 playlist))
120 (run-hooks 'emms-playlist-source-inserted-hook)
121 (ignore-errors
122 (emms-playlist-select (cadr playlist)))))
123 (setq emms-playlist-buffer buf)
124 (dolist (method (nth 2 history))
125 (set (car method) (cdr method)))
126 (ignore-errors
127 (when emms-history-start-playing
128 (emms-start)))))))
129
130 (provide 'emms-history)
131 ;;; emms-history.el ends here