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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
;;; emms-playlist-sort.el --- sort emms playlist
;; Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
;; Author: William Xu <william.xwl@gmail.com>
;; This file is part of EMMS.
;; EMMS 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.
;;
;; EMMS 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 EMMS; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
;;; Code:
(require 'emms-score)
(eval-when-compile (require 'cl))
;;; User Customizations
(defgroup emms-playlist-sort nil
"Sorting Emacs Multimedia System playlists."
:prefix "emms-playlist-sort-"
:group 'emms)
(defcustom emms-playlist-sort-list '(info-artist info-album)
"Sorting list used by `emms-playlist-sort-by-list'.
Currently it understands the following fields: name info-artist
imfo-composer info-performer info-title info-album info-genre
info-playing-time info-tracknumber."
:type 'symbol
:group 'emms-playlist-sort)
;;; User Interfaces
(defmacro define-emms-playlist-sort (attribute)
"Macro for defining emms playlist sort functions."
`(defun ,(intern (format "emms-playlist-sort-by-%s" attribute)) ()
,(format "Sort emms playlist by %s, increasingly." attribute)
(interactive)
(emms-playlist-sort
(lambda (a b)
(emms-string< (emms-track-get a (quote ,attribute))
(emms-track-get b (quote ,attribute)))))))
(define-emms-playlist-sort name)
(define-emms-playlist-sort info-artist)
(define-emms-playlist-sort info-composer)
(define-emms-playlist-sort info-performer)
(define-emms-playlist-sort info-title)
(define-emms-playlist-sort info-album)
(define-emms-playlist-sort info-year)
(define-emms-playlist-sort info-note)
(defun emms-playlist-sort-by-score ()
"Sort emms playlist by score, decreasingly."
(interactive)
(emms-playlist-sort
(lambda (a b)
(> (emms-score-get-score (emms-track-get a 'name))
(emms-score-get-score (emms-track-get b 'name))))))
(defun emms-playlist-sort-by-natural-order ()
"Sort emms playlist by natural order.
See `emms-sort-natural-order-less-p'."
(interactive)
(emms-playlist-sort 'emms-sort-natural-order-less-p))
(defun emms-playlist-sort-by-list ()
"Sort emms playlist by `emms-playlist-sort-list'.
The sort will be carried out until comparsion succeeds,
increasingly."
(interactive)
(emms-playlist-sort 'emms-playlist-sort-by-list-p))
(eval-after-load 'emms-playlist-mode
'(progn
(define-key emms-playlist-mode-map (kbd "S n") 'emms-playlist-sort-by-name)
(define-key emms-playlist-mode-map (kbd "S a") 'emms-playlist-sort-by-info-artist)
(define-key emms-playlist-mode-map (kbd "S c") 'emms-playlist-sort-by-info-composer)
(define-key emms-playlist-mode-map (kbd "S p") 'emms-playlist-sort-by-info-performer)
(define-key emms-playlist-mode-map (kbd "S t") 'emms-playlist-sort-by-info-title)
(define-key emms-playlist-mode-map (kbd "S b") 'emms-playlist-sort-by-info-album)
(define-key emms-playlist-mode-map (kbd "S y") 'emms-playlist-sort-by-info-year)
(define-key emms-playlist-mode-map (kbd "S o") 'emms-playlist-sort-by-info-note)
(define-key emms-playlist-mode-map (kbd "S N") 'emms-playlist-sort-by-natural-order)
(define-key emms-playlist-mode-map (kbd "S l") 'emms-playlist-sort-by-list)
(define-key emms-playlist-mode-map (kbd "S s") 'emms-playlist-sort-by-score)))
;;; Low Level Functions
(defun emms-playlist-sort (predicate &optional start end)
"Sort the playlist buffer by PREDICATE.
If START and END are not provided, the whole buffer will be sorted."
(unless start (setq start (point-min)))
(unless end (setq end (point-max)))
(with-current-emms-playlist
(save-excursion
(emms-playlist-ensure-playlist-buffer)
(widen)
(let ((current (emms-playlist-selected-track))
(tracks
(emms-playlist-tracks-in-region start end)))
(delete-region start end)
(run-hooks 'emms-playlist-cleared-hook)
(mapc 'emms-playlist-insert-track
(sort tracks predicate))
(let ((pos (text-property-any start end
'emms-track current)))
(if pos
(emms-playlist-select pos)
(emms-playlist-first)))))))
(defun emms-string> (a b)
(not (or (emms-string< a b)
(string= a b))))
(defun emms-sort-natural-order-less-p (a b)
"Sort two tracks by natural order.
This is the order in which albums where intended to be played.
ie. by album name and then by track number."
(or (emms-string> (emms-track-get a 'info-album)
(emms-track-get b 'info-album))
(and (string= (emms-track-get a 'info-album)
(emms-track-get b 'info-album))
(< (string-to-number (or (emms-track-get a 'info-tracknumber)
"0"))
(string-to-number (or (emms-track-get b 'info-tracknumber)
"0"))))))
(defun emms-playlist-sort-by-list-p (a b)
(catch 'return
(dolist (info emms-playlist-sort-list)
(case info
((name info-artist info-composer info-performer info-title info-album info-genre)
(when (emms-string< (emms-track-get a info)
(emms-track-get b info))
(throw 'return t)))
((info-playing-time)
(when (< (emms-track-get a info)
(emms-track-get b info))
(throw 'return t)))
((info-tracknumber)
(when (< (string-to-number (or (emms-track-get a info) "0"))
(string-to-number (or (emms-track-get b info) "0")))
(throw 'return t)))))))
(defun emms-string< (s1 s2)
"Same as `string<' except this is case insensitive."
(string< (and s1 (downcase s1)) (and s2 (downcase s2))))
(provide 'emms-playlist-sort)
;;; emms-playlist-sort.el ends here
|