aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--emms-browser.el33
-rw-r--r--emms-playlist-sort.el42
2 files changed, 49 insertions, 26 deletions
diff --git a/emms-browser.el b/emms-browser.el
index 181ec6a..888899c 100644
--- a/emms-browser.el
+++ b/emms-browser.el
@@ -83,6 +83,13 @@ The default is to compare case-insensitively."
:group 'emms-browser
:type 'symbol)
+(defcustom emms-browser-sort-function
+ 'emms-sort-natural-order-less-p
+ "How to sort tracks from the browser (nil for no sorting).
+This is used to sort tracks when they are added to the playlist."
+ :group 'emms-browser
+ :type 'function)
+
(defcustom emms-browser-show-display-hook nil
"Hooks to run when starting or switching to a browser buffer."
:group 'emms-browser
@@ -138,12 +145,12 @@ The default is to compare case-insensitively."
"Launch or switch to the EMMS Browser."
(interactive)
(if (or (null emms-browser-buffer)
- (not (buffer-live-p emms-browser-buffer)))
+ (not (buffer-live-p emms-browser-buffer)))
(progn
(setq emms-browser-buffer (emms-browser-new-buffer name))
(funcall emms-browser-default-browsing-function))
(when name
- (rename-buffer name)))
+ (rename-buffer name t)))
;; if the buffer is displayed, switch the window instead
(let ((wind (get-buffer-window emms-browser-buffer)))
(if wind
@@ -263,15 +270,29 @@ If called interactively, the new buffer is also selected."
(defun emms-browser-add-tracks ()
"Add all the tracks on the current line to the playlist."
(interactive)
- ;; display the bottom of the current playlist
(let ((tracks (emms-browser-tracks-at))
- (count 0))
+ (count 0)
+ old-max new-max type name)
(unless tracks
(error "No tracks on current line!"))
+ (with-current-emms-playlist
+ (setq old-max (point-max)))
+ ;; add each of the tracks
(dolist (track tracks)
- ;; FIXME: assumes 'file type
- (emms-add-file (emms-track-get track 'name))
+ (setq type (emms-track-get track 'type))
+ (setq name (emms-track-get track 'name))
+ (cond
+ ((eq type 'file)
+ (emms-add-file name))
+ ((eq type 'url)
+ (emms-add-url name)))
(setq count (1+ count)))
+ ;; sort
+ (when emms-browser-sort-function
+ (with-current-emms-playlist
+ (setq new-max (point-max)))
+ (emms-playlist-sort 'emms-sort-natural-order-less-p
+ old-max new-max))
(run-mode-hooks 'emms-browser-tracks-added-hook)
(message "Added %d tracks." count)))
diff --git a/emms-playlist-sort.el b/emms-playlist-sort.el
index 7e01488..3b5dce5 100644
--- a/emms-playlist-sort.el
+++ b/emms-playlist-sort.el
@@ -94,26 +94,28 @@ You should set this variable before loading this file."
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)))))))
+(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."
+ (let ((run-cleared-hook nil))
+ (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 (string< a b)