summaryrefslogtreecommitdiffhomepage
path: root/attic/jao-emms-random-album.el
blob: 72e056beca6c6aa73de92bbbc4d96d4d891a2a13 (plain)
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
;; jao-emms-random-album.el -- play random albums in emms

;; Copyright (C) 2009, 2010, 2017, 2018, 2020 Jose Antonio Ortega Ruiz

;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;; Start date: Sat Jul 04, 2009 13:06

;; 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 3 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.


(require 'emms)
(require 'jao-minibuffer)

(defvar jao-emms-random-album-p t)
(defvar jao-emms-random-lines nil)
(defvar jao-emms-random-lines-file
  (expand-file-name "~/.emacs.d/random-lines"))
(defvar jao-emms-random-album-notify-p t)
(defvar jao-emms-random-album-notify-icon nil)

(defun jao-emms-random-lines ()
  (or jao-emms-random-lines
      (and (file-exists-p jao-emms-random-lines-file)
           (with-current-buffer
               (find-file-noselect jao-emms-random-lines-file)
             (goto-char (point-min))
             (setq jao-emms-random-lines (read (current-buffer)))))
      (dotimes (n (1- (line-number-at-pos (point-max)))
                  jao-emms-random-lines)
        (push (1+ n) jao-emms-random-lines))))

(defun jao-emms-random-lines-save ()
  (with-current-buffer (find-file-noselect jao-emms-random-lines-file)
    (delete-region (point-min) (point-max))
    (insert (format "%s\n" jao-emms-random-lines))
    (save-buffer)))

(defun jao-emms-goto-random-album ()
  (let* ((pos (random (length (jao-emms-random-lines))))
         (line (nth pos jao-emms-random-lines)))
    (setq jao-emms-random-lines (remove line jao-emms-random-lines))
    (jao-emms-random-lines-save)
    (goto-line line)))

(defun jao-emms-next-noerror ()
  (interactive)
  (when emms-player-playing-p
    (error "A track is already being played"))
  (cond (emms-repeat-track
         (emms-start))
        ((condition-case nil
             (progn
               (emms-playlist-current-select-next)
               t)
           (error nil))
         (emms-start))
        (t
         (if jao-emms-random-album-p
             (jao-emms-random-album-next)
           (message "No next track in playlist")))))


;; User interface
;;;###autoload
(defun jao-emms-random-album-start ()
  (interactive)
  (setq jao-emms-random-album-p t)
  (jao-emms-random-album-next))

;;;###autoload
(defun jao-emms-random-album-stop ()
  (interactive)
  (setq jao-emms-random-album-p nil)
  (emms-stop))

;;;###autoload
(defun jao-emms-random-album-toggle ()
  (interactive)
  (setq jao-emms-random-album-p (not jao-emms-random-album-p))
  (message "Random album %s"
           (if jao-emms-random-album-p "enabled" "disabled")))

;;;###autoload
(defun jao-emms-random-album-next ()
  (interactive)
  (save-excursion
    (ignore-errors (emms-browser-clear-playlist))
    (emms-browse-by-album)
    (jao-emms-goto-random-album)
    (let ((album (substring-no-properties (thing-at-point 'line) 0 -1)))
      (emms-browser-add-tracks-and-play)
      (when jao-emms-random-album-notify-p
        (jao-notify album "Next album" jao-emms-random-album-notify-icon)))
    (emms-browser-bury-buffer)
    (jao-minibuffer-refresh)))

;;;###autoload
(defun jao-emms-random-album-reset ()
  (interactive)
  (setq jao-emms-random-lines nil)
  (jao-emms-random-lines-save))

(setq emms-player-next-function 'jao-emms-next-noerror)


(provide 'jao-emms-random-album)
;;; jao-emms-random-album.el ends here