From 43812b705f967d2d8bc7c281debd7a57dc2fc4ba Mon Sep 17 00:00:00 2001
From: Justin <72092018+27justin@users.noreply.github.com>
Date: Wed, 13 Dec 2023 22:05:34 +0100
Subject: [PATCH] Allow multi-track queueing (#95)
---
README.md | 16 ++++++++--------
smudge-api.el | 10 +++++-----
smudge-track.el | 29 ++++++++++++++++++++++++-----
3 files changed, 37 insertions(+), 18 deletions(-)
diff --git a/README.md b/README.md
index 7c86ce5..b19f8c6 100644
--- a/README.md
+++ b/README.md
@@ -254,13 +254,13 @@ To search for tracks, run M-x smudge-track-search and type in your
query. The results will be displayed in a separate buffer with the following
key bindings:
-| Key | Description |
-|:-----------------|:-----------------------------------------------------------------|
-| a | Adds track to a playlist |
-| l | Loads the next page of results (pagination) |
-| g | Clears the results and reloads the first page of results |
-| k | Adds track to the queue |
-| M-RET | Plays the track under the cursor in the context of its album [1] |
+| Key | Description |
+|:-----------------|:---------------------------------------------------------------------|
+| a | Adds track to a playlist |
+| l | Loads the next page of results (pagination) |
+| g | Clears the results and reloads the first page of results |
+| k | Adds track(s) under the cursor (or inside the region) to the queue |
+| M-RET | Plays the track under the cursor in the context of its album [1] |
[1] D-Bus implementation for GNU/Linux do not support passing the context, so
only the track under the cursor will be played
@@ -330,7 +330,7 @@ bindings in the resulting buffer:
| g | Clears the results and reloads the first page of results |
| f | Follows the current playlist |
| u | Unfollows the current playlist |
-| k | Adds track to the queue |
+| k | Adds track(s) under the cursor (or inside the region) to the queue |
| M-RET | Plays the track under the cursor in the context of the playlist [1] |
Both buffers load the `global-smudge-remote-mode` by default.
diff --git a/smudge-api.el b/smudge-api.el
index 874e9e1..8acccbf 100644
--- a/smudge-api.el
+++ b/smudge-api.el
@@ -632,11 +632,11 @@ Call CALLBACK if provided."
;; Thus we have to synchronously add the tracks
;; one by one to the queue.
(if (car track-ids)
- (smudge-api-queue-add-track (car track-ids)
- (lambda (_)
- (smudge-api-queue-add-tracks (cdr track-ids)
- nil)))
- (funcall callback)))
+ (smudge-api-queue-add-track
+ (car track-ids)
+ (lambda (_)
+ (smudge-api-queue-add-tracks (cdr track-ids) callback)))
+ (when callback (funcall callback))))
(provide 'smudge-api)
;;; smudge-api.el ends here
diff --git a/smudge-track.el b/smudge-track.el
index 478b26b..7a3ebb8 100644
--- a/smudge-track.el
+++ b/smudge-track.el
@@ -301,14 +301,33 @@ Default to sortin tracks by number when listing the tracks from an album."
(message "Cannot remove a track from a playlist from here")))
(defun smudge-track-add-to-queue ()
- "Add the track under the cursor to the queue."
+ "Add the track(s) under the cursor (or inside the active region) to the queue."
(interactive)
- (let ((selected-track (tabulated-list-get-id)))
- (let ((track-id (smudge-api-get-item-uri selected-track)))
- (smudge-api-queue-add-track
+ ;; Check whether the mark is active and if so, queue all the tracks in the
+ ;; region. If not, queue the track under the cursor.
+ (if (null mark-active)
+ (let ((selected-track (tabulated-list-get-id)))
+ (setq track-id (smudge-api-get-item-uri selected-track))
+ (smudge-api-queue-add-track
track-id
(lambda(_)
- (message "Added \"%s\" to your queue." (smudge-api-get-item-name selected-track)))))))
+ (message "Added \"%s\" to your queue." (smudge-api-get-item-name selected-track)))))
+ (let((start (region-beginning))
+ (end (region-end))
+ (tracks '()))
+ (save-excursion
+ (goto-char start)
+ (while (< (point) end)
+ (setq selected-track (tabulated-list-get-id))
+ (setq track-id (smudge-api-get-item-uri selected-track))
+ (setq tracks (cons track-id tracks))
+ (forward-line 1)))
+ (smudge-api-queue-add-tracks
+ (reverse tracks)
+ nil)
+ ;; Send the message here instead of in the callback
+ ;; because the API call has to sequentially add each song which might take some time.
+ (message "Added %d tracks to your queue." (length tracks)))))
(provide 'smudge-track)