aboutsummaryrefslogtreecommitdiff
path: root/emms-playlist-sort.el
blob: ee3671a1fc8546599300d702d8ccd2795bf7927c (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
;;; emms-playlist-sort.el --- sort emms playlist

;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.

;; Author: William Xu <william.xwl@gmail.com>

;; 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 2, 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., 51 Franklin St, Fifth Floor, Boston, MA
;; 02110-1301 USA

;;; Commentary:

;; Provide various playlist sort functions for emms, such as
;; `emms-playlist-sort-by-info-title'.

;; Put this file into your load-path and the following into your
;; ~/.emacs:
;;            (require 'emms-playlist-sort)

;;; Code:

(require 'emms-score)

;;; sort macro

(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)
	(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-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))))))

(defgroup emms-playlist-sort nil
  "*Sorting Emacs Multimedia System playlists."
  :prefix "emms-playlist-sort-"
  :group 'emms)

;; FIXME: Should better avoid relying on setting before loading.
(defcustom emms-playlist-sort-prefix "S"
  "*Prefix key sequence for `emms-playlist-sort-map'.
You should set this variable before loading this file."
  :type 'string
  :group 'emms-playlist-sort)

(defvar emms-playlist-sort-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "n") 'emms-playlist-sort-by-name)
    (define-key map (kbd "a") 'emms-playlist-sort-by-info-artist)
    (define-key map (kbd "t") 'emms-playlist-sort-by-info-title)
    (define-key map (kbd "b") 'emms-playlist-sort-by-info-album)
    (define-key map (kbd "y") 'emms-playlist-sort-by-info-year)
    (define-key map (kbd "o") 'emms-playlist-sort-by-info-note)
    map))

(eval-after-load "emms-playlist-mode"
  (and (boundp 'emms-playlist-mode-map)
       (define-key emms-playlist-mode-map
         emms-playlist-sort-prefix
         emms-playlist-sort-map)))

(defun emms-playlist-sort (predicate)
  "Sort the whole playlist buffer by PREDICATE."
  (with-current-emms-playlist
    (save-excursion
      (emms-playlist-ensure-playlist-buffer)
      (widen)
      (let ((current (emms-playlist-selected-track))
	    (tracks (emms-playlist-tracks-in-region (point-min)
						    (point-max))))
	(delete-region (point-min)
		       (point-max))
	(run-hooks 'emms-playlist-cleared-hook)
	(mapc 'emms-playlist-insert-track
	      (sort tracks predicate))
	(let ((pos (text-property-any (point-min)
				      (point-max)
				      'emms-track current)))
	  (if pos
	      (emms-playlist-select pos)
	    (emms-playlist-first)))))))

(provide 'emms-playlist-sort)

;;; emms-playlist-sort.el ends here