From 5cf0ca5712443a452b77233440bb9e2cc103b6ec Mon Sep 17 00:00:00 2001 From: Michael Olson Date: Mon, 29 Jan 2007 09:11:00 +0000 Subject: New macro emms-walk-tracks This macro makes it easy to write code which steps forward through the tracks of the current buffer iteratively. darcs-hash:20070129091141-1bfb2-feed35fa379e6424a16cd0d735fc95e3f2d9f47b.gz --- emms.el | 62 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/emms.el b/emms.el index 983defa..ae95dd1 100644 --- a/emms.el +++ b/emms.el @@ -296,6 +296,30 @@ This also disables any read-onliness of the current buffer." ,@body)) (put 'emms-with-widened-buffer 'edebug-form-spec '(body)) +(defmacro emms-walk-tracks (&rest body) + "Execute BODY for each track in the current buffer, starting at point. +The point will be placed at the beginning of the track before +executing BODY. + +The point will not be restored afterward." + (let ((donep (make-symbol "donep"))) + `(let ((,donep nil)) + ;; skip to first track if not on one + (unless (emms-playlist-track-at (point)) + (condition-case nil + (emms-playlist-next) + (error + (setq ,donep t)))) + ;; walk tracks + (while (not ,donep) + ,@body + (condition-case nil + (emms-playlist-next) + (error + (setq ,donep t))))))) +(put 'emms-walk-tracks 'lisp-indent-function 0) +(put 'emms-walk-tracks 'edebug-form-spec '(body)) + ;;; User Interface @@ -833,23 +857,11 @@ If no playlist exists, a new one is generated." (emms-playlist-ensure-playlist-buffer) ;; FIXME: This is rather inefficient. (save-excursion - (let ((track-indices nil) - (donep nil)) - (condition-case nil - (progn - (emms-playlist-first) - (setq track-indices (cons (point) - track-indices))) - (error - (setq donep t))) - (while (not donep) - (condition-case nil - (progn - (emms-playlist-next) - (setq track-indices (cons (point) - track-indices))) - (error - (setq donep t)))) + (let ((track-indices nil)) + (goto-char (point-min)) + (emms-walk-tracks + (setq track-indices (cons (point) + track-indices))) (setq track-indices (vconcat track-indices)) (emms-playlist-select (aref track-indices (random (length track-indices))))))) @@ -914,21 +926,13 @@ This is supplying ARGS as arguments to the source." (defun emms-playlist-tracks-in-region (beg end) "Return all tracks between BEG and END." (emms-playlist-ensure-playlist-buffer) - (let ((tracks nil) - (donep nil)) + (let ((tracks nil)) (save-restriction (narrow-to-region beg end) - (condition-case nil - (emms-playlist-first) - (error - (setq donep t))) - (while (not donep) + (goto-char (point-min)) + (emms-walk-tracks (setq tracks (cons (emms-playlist-track-at (point)) - tracks)) - (condition-case nil - (emms-playlist-next) - (error - (setq donep t))))) + tracks)))) tracks)) (defun emms-playlist-track-updated (track) -- cgit v1.2.3