emms-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [emms-help] Playlist shuffle fix


From: Jorgen Schaefer
Subject: Re: [emms-help] Playlist shuffle fix
Date: Sun, 29 May 2005 14:32:14 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

Lucas Bonnet <address@hidden> writes:

> Jorgen, if you agree with it, I'll install it.

Thanks for this!

> (defun emms-playlist-search-track (track)
>   "Returns the index of the track TRACK."
>   (catch 'loop
>     (let ((i 0)
>           (track-fullpath (cdr (assoc 'name track))))
>       (while (< i (length emms-playlist))
>         (let ((i-fullpath (cdr (assoc 'name (elt emms-playlist i)))))
>           (if (string= track-fullpath i-fullpath)
>               (throw 'loop i)
>             (setq i (1+ i))))))))

Since tracks are not regenerated, `eq' is a good way to search for
them:

(defun emms-playlist-search-track (track)
  "Returns the index of the track TRACK."
  (catch 'loop
    (let ((i 0))
      (while (< i (length emms-playlist))
        (if (eq track
                (elt emms-playlist i))
            (throw 'loop i)
          (setq i (1+ i)))))))

This has the advantage of also working for non-file tracks.

> (defun emms-playlist-shuffle ()
>   "Shuffle the current playlist."
>   (let ((current-track (emms-playlist-current-track)))
>     (emms-playlist-set-playlist
>      (emms-playlist-shuffle-vector
>       (emms-playlist-get-playlist)))
>     ;; loop to find the "lost" track.
>     (let ((new-index (emms-playlist-search-track current-track)))
>       (emms-playlist-set-current new-index)
>       (emms-pbi-rebuild-playlist-buffer))))
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I know it's a horrible thought for you :-), but there are people
who don't even load emms-pbi - emms.el does not use any functions
of pbi directly. `emms-playlist-set-current' runs the hook
`emms-playlist-current-changed-hook', which in turn will have the
`emms-pbi-rebuild-playlist-buffer' on it.

I remember someone suggesting to keep the current track as the
first track of the playlist when shuffling. Is there any specific
reason against that?

(defun emms-playlist-search-vector (track vector)
  "Returns the index of the track TRACK in VECTOR."
  (catch 'loop
    (let ((i 0))
      (while (< i (length vector))
        (if (eq track
                (elt vector i))
            (throw 'loop i)
          (setq i (1+ i)))))))

(defun emms-playlist-shuffle ()
  "Shuffle the current playlist.
This retains the current track at the front if anything is
playling."
  (if (not emms-player-playing-p)
      (emms-playlist-set-playlist
       (emms-playlist-shuffle-vector
        (emms-playlist-get-playlist)))
    (let* ((current-track (emms-playlist-current-track))
           (playlist (emms-playlist-shuffle-vector
                      (emms-playlist-get-playlist)))
           (new-index (emms-playlist-search-vector current-track playlist))
           (first (elt playlist 0)))
      (aset playlist 0 (elt playlist new-index))
      (aset playlist new-index first)
      (emms-playlist-set-playlist playlist)
      (emms-playlist-set-current 0))))

Your approach is useful for sorting, though:

(defun emms-playlist-sort ()
  "Sort the current playlist according to `emms-sort-lessp-function'."
  (let* ((current-track (emms-playlist-current-track))
         (playlist (emms-playlist-sort-vector
                    (emms-playlist-get-playlist)))
         (new-index (emms-playlist-search-vector current-track playlist)))
    (emms-playlist-set-playlist playlist)
    (emms-playlist-set-current new-index)))

Is it a problem that these now call `emms-playlist-changed-hook'
and `emms-playlist-current-changed-hook' directly after each
other?

Greetings,
        -- Jorgen

-- 
((email . "address@hidden") (www . "http://www.forcix.cx/";)
 (gpg   . "1024D/028AF63C")   (irc . "nick forcer on IRCnet"))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]