| 1 | ;;; emms.el --- The Emacs Multimedia System -*- lexical-binding: t; -*- |
| 2 | |
| 3 | ;; Copyright (C) 2003-2026 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Jorgen Schäfer <forcer@forcix.cx>, the Emms developers (see AUTHORS file) |
| 6 | ;; Maintainer: Yoni Rabkin <yrk@gnu.org> |
| 7 | ;; Version: 26 |
| 8 | ;; Keywords: emms, mp3, ogg, flac, music, mpeg, video, multimedia |
| 9 | ;; Package-Type: multi |
| 10 | ;; Package-Requires: ((cl-lib "0.5") (nadvice "0.3") (seq)) |
| 11 | ;; url: https://www.gnu.org/software/emms/ |
| 12 | |
| 13 | ;; This file is part of EMMS. |
| 14 | |
| 15 | ;; EMMS is free software; you can redistribute it and/or modify it |
| 16 | ;; under the terms of the GNU General Public License as published by |
| 17 | ;; the Free Software Foundation; either version 3, or (at your option) |
| 18 | ;; any later version. |
| 19 | |
| 20 | ;; EMMS is distributed in the hope that it will be useful, but WITHOUT |
| 21 | ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 22 | ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 23 | ;; License for more details. |
| 24 | |
| 25 | ;; You should have received a copy of the GNU General Public License |
| 26 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 27 | |
| 28 | ;;; Commentary: |
| 29 | |
| 30 | ;; This is the very core of EMMS. It provides ways to play a track |
| 31 | ;; using `emms-start', to go through the playlist using the commands |
| 32 | ;; `emms-next' and `emms-previous', to stop the playback using |
| 33 | ;; `emms-stop', and to see what's currently playing using `emms-show'. |
| 34 | |
| 35 | ;; But in itself, this core is useless, because it doesn't know how to |
| 36 | ;; play any tracks --- you need players for this. In fact, it doesn't |
| 37 | ;; even know how to find any tracks to consider playing --- for this, |
| 38 | ;; you need sources. |
| 39 | |
| 40 | ;; A sample configuration is offered in emms-setup.el, and the |
| 41 | ;; Friendly Manual in the doc/ directory is both detailed, and kept up |
| 42 | ;; to date. |
| 43 | |
| 44 | |
| 45 | ;;; Code: |
| 46 | (require 'emms-compat) |
| 47 | (require 'seq) |
| 48 | |
| 49 | (defvar emms-version "26" |
| 50 | "EMMS version string.") |
| 51 | |
| 52 | ;;; User Customization |
| 53 | (defgroup emms nil |
| 54 | "*The Emacs Multimedia System." |
| 55 | :prefix "emms-" |
| 56 | :group 'multimedia |
| 57 | :group 'applications) |
| 58 | |
| 59 | (defgroup emms-player nil |
| 60 | "*Track players for EMMS." |
| 61 | :prefix "emms-player-" |
| 62 | :group 'emms) |
| 63 | |
| 64 | (defgroup emms-source nil |
| 65 | "*Track sources for EMMS." |
| 66 | :prefix "emms-source-" |
| 67 | :group 'emms) |
| 68 | |
| 69 | (defcustom emms-player-list nil |
| 70 | "*List of players that EMMS can use." |
| 71 | :group 'emms |
| 72 | :type '(repeat (symbol :tag "Player"))) |
| 73 | |
| 74 | (defcustom emms-show-format "Currently playing: %s" |
| 75 | "*The format to use for `emms-show'. |
| 76 | Any \"%s\" is replaced by what `emms-track-description-function' returns |
| 77 | for the currently playing track." |
| 78 | :group 'emms |
| 79 | :type 'string) |
| 80 | |
| 81 | (defcustom emms-repeat-playlist nil |
| 82 | "*Non-nil if the EMMS playlist should automatically repeat. |
| 83 | If nil, playback will stop when the last track finishes playing. |
| 84 | If non-nil, EMMS will wrap back to the first track when that happens." |
| 85 | :group 'emms |
| 86 | :type 'boolean) |
| 87 | |
| 88 | (defcustom emms-player-next-function 'emms-next-noerror |
| 89 | "*A function run when EMMS thinks the next song should be played." |
| 90 | :group 'emms |
| 91 | :type 'function |
| 92 | :options '(emms-next-noerror |
| 93 | emms-random)) |
| 94 | |
| 95 | (defcustom emms-random-playlist nil |
| 96 | "*Non-nil means that tracks are played randomly. If nil, tracks |
| 97 | are played sequentially." |
| 98 | :group 'emms |
| 99 | :set (lambda (symbol value) |
| 100 | (set symbol value) |
| 101 | (if value |
| 102 | (setq emms-player-next-function #'emms-random) |
| 103 | (setq emms-player-next-function #'emms-next-noerror))) |
| 104 | :type 'boolean) |
| 105 | |
| 106 | (defvar-local emms-repeat-track nil |
| 107 | "Non-nil, playback will repeat current track. If nil, EMMS will play |
| 108 | track by track normally.") |
| 109 | |
| 110 | (defvar-local emms-single-track nil |
| 111 | "Non-nil, play the current track and then stop.") |
| 112 | |
| 113 | (defcustom emms-completing-read-function |
| 114 | (if (and (boundp 'ido-mode) |
| 115 | ido-mode) |
| 116 | 'ido-completing-read |
| 117 | 'completing-read) |
| 118 | "Function to call when prompting user to choose between a list of options. |
| 119 | This should take the same arguments as `completing-read'. Some |
| 120 | possible values are `completing-read' and `ido-completing-read'. |
| 121 | Note that you must set `ido-mode' if using |
| 122 | `ido-completing-read'." |
| 123 | :group 'emms |
| 124 | :type 'function) |
| 125 | |
| 126 | (defcustom emms-track-description-function 'emms-track-simple-description |
| 127 | "*Function for describing an EMMS track in a user-friendly way." |
| 128 | :group 'emms |
| 129 | :type 'function) |
| 130 | |
| 131 | (defcustom emms-player-delay 0 |
| 132 | "The delay to pause after a player finished. |
| 133 | This is a floating-point number of seconds. This is necessary |
| 134 | for some platforms where it takes a bit to free the audio device |
| 135 | after a player has finished. If EMMS is skipping songs, increase |
| 136 | this number." |
| 137 | :type 'number |
| 138 | :group 'emms) |
| 139 | |
| 140 | (defcustom emms-playlist-shuffle-function 'emms-playlist-simple-shuffle |
| 141 | "*The function to use for shuffling the playlist." |
| 142 | :type 'function |
| 143 | :group 'emms) |
| 144 | |
| 145 | (defcustom emms-playlist-sort-function 'emms-playlist-simple-sort |
| 146 | "*The function to use for sorting the playlist." |
| 147 | :type 'function |
| 148 | :group 'emms) |
| 149 | |
| 150 | (defcustom emms-playlist-uniq-function 'emms-playlist-simple-uniq |
| 151 | "*The function to use for removing duplicate tracks in the playlist." |
| 152 | :type 'function |
| 153 | :group 'emms) |
| 154 | |
| 155 | (defcustom emms-sort-lessp-function 'emms-sort-track-name-less-p |
| 156 | "*Function for comparing two EMMS tracks. |
| 157 | The function should return non-nil if and only if the first track |
| 158 | sorts before the second (see `sort')." |
| 159 | :group 'emms |
| 160 | :type 'function) |
| 161 | |
| 162 | (defcustom emms-playlist-buffer-name " *EMMS Playlist*" |
| 163 | "*The default name of the EMMS playlist buffer." |
| 164 | :type 'string |
| 165 | :group 'emms) |
| 166 | |
| 167 | (defcustom emms-playlist-default-major-mode 'emms-playlist-mode |
| 168 | "*The default major mode for EMMS playlist." |
| 169 | :type 'function |
| 170 | :group 'emms) |
| 171 | |
| 172 | (defcustom emms-playlist-insert-track-function 'emms-playlist-simple-insert-track |
| 173 | "*A function to insert a track into the playlist buffer." |
| 174 | :group 'emms |
| 175 | :type 'function) |
| 176 | (make-variable-buffer-local 'emms-playlist-insert-track-function) |
| 177 | |
| 178 | (defcustom emms-playlist-update-track-function 'emms-playlist-simple-update-track |
| 179 | "*A function to update the track at point. |
| 180 | This is called when the track information changed. This also |
| 181 | shouldn't assume that the track has been inserted before." |
| 182 | :group 'emms |
| 183 | :type 'function) |
| 184 | (make-variable-buffer-local 'emms-playlist-insert-track-function) |
| 185 | |
| 186 | (defcustom emms-playlist-delete-track-function 'emms-playlist-simple-delete-track |
| 187 | "*A function to delete the track at point in the playlist buffer." |
| 188 | :group 'emms |
| 189 | :type 'function) |
| 190 | (make-variable-buffer-local 'emms-playlist-delete-track-function) |
| 191 | |
| 192 | (defcustom emms-ok-track-function 'emms-default-ok-track-function |
| 193 | "*Function returns true if we shouldn't skip this track." |
| 194 | :group 'emms |
| 195 | :type 'function) |
| 196 | |
| 197 | (defcustom emms-playlist-source-inserted-hook nil |
| 198 | "*Hook run when a source got inserted into the playlist. |
| 199 | The buffer is narrowed to the new tracks." |
| 200 | :type 'hook |
| 201 | :group 'emms) |
| 202 | |
| 203 | (defcustom emms-playlist-selection-changed-hook nil |
| 204 | "*Hook run after another track is selected in the EMMS playlist." |
| 205 | :group 'emms |
| 206 | :type 'hook) |
| 207 | |
| 208 | (defcustom emms-playlist-cleared-hook nil |
| 209 | "*Hook run after the current EMMS playlist is cleared. |
| 210 | This happens both when the playlist is cleared and when a new |
| 211 | buffer is created for it." |
| 212 | :group 'emms |
| 213 | :type 'hook) |
| 214 | |
| 215 | (defcustom emms-track-initialize-functions nil |
| 216 | "*List of functions to call for each new EMMS track. |
| 217 | This can be used to initialize tracks with various info." |
| 218 | :group 'emms |
| 219 | :type 'hook) |
| 220 | |
| 221 | (defcustom emms-track-info-filters nil |
| 222 | "*List of functions to call when a track changes data, before updating |
| 223 | the display. |
| 224 | These functions are passed the track as an argument." |
| 225 | :group 'emms |
| 226 | :type 'hook) |
| 227 | |
| 228 | (defcustom emms-track-updated-functions nil |
| 229 | "*List of functions to call when a track changes data, after updating |
| 230 | the display. |
| 231 | These functions are passed the track as an argument." |
| 232 | :group 'emms |
| 233 | :type 'hook) |
| 234 | |
| 235 | (defcustom emms-player-started-hook nil |
| 236 | "*Hook run when an EMMS player starts playing." |
| 237 | :group 'emms |
| 238 | :type 'hook |
| 239 | :options '(emms-show)) |
| 240 | |
| 241 | (defcustom emms-player-stopped-hook nil |
| 242 | "*Hook run when an EMMS player is stopped by the user. |
| 243 | See `emms-player-finished-hook'." |
| 244 | :group 'emms |
| 245 | :type 'hook) |
| 246 | |
| 247 | (defcustom emms-player-finished-hook nil |
| 248 | "*Hook run when an EMMS player finishes playing a track. |
| 249 | Please pay attention to the differences between |
| 250 | `emms-player-finished-hook' and `emms-player-stopped-hook'. The |
| 251 | former is called only when the player actually finishes playing a |
| 252 | track; the latter, only when the player is stopped |
| 253 | interactively." |
| 254 | :group 'emms |
| 255 | :type 'hook) |
| 256 | |
| 257 | (defcustom emms-player-paused-hook nil |
| 258 | "*Hook run when a player is paused or resumed. |
| 259 | Use `emms-player-paused-p' to find the current state." |
| 260 | :group 'emms |
| 261 | :type 'hook) |
| 262 | |
| 263 | (defcustom emms-seek-seconds 10 |
| 264 | "The number of seconds to seek forward or backward when seeking." |
| 265 | :group 'emms |
| 266 | :type 'number) |
| 267 | |
| 268 | (defcustom emms-player-seeked-functions nil |
| 269 | "*Functions called when a player is seeking. |
| 270 | The functions are called with a single argument, the amount of |
| 271 | seconds the player did seek." |
| 272 | :group 'emms |
| 273 | :type 'hook) |
| 274 | |
| 275 | (defcustom emms-player-time-set-functions nil |
| 276 | "*Functions called when a player is setting the elapsed time of a track. |
| 277 | The functions are called with a single argument, the time elapsed |
| 278 | since the beginning of the current track." |
| 279 | :group 'emms |
| 280 | :type 'hook) |
| 281 | |
| 282 | (defcustom emms-cache-get-function nil |
| 283 | "A function to retrieve a track entry from the cache. |
| 284 | This is called with two arguments, the type and the name." |
| 285 | :group 'emms |
| 286 | :type 'function) |
| 287 | |
| 288 | (defcustom emms-cache-set-function nil |
| 289 | "A function to add/set a track entry from the cache. |
| 290 | This is called with three arguments: the type of the track, the |
| 291 | name of the track, and the track itself." |
| 292 | :group 'emms |
| 293 | :type 'function) |
| 294 | |
| 295 | (defcustom emms-cache-modified-function nil |
| 296 | "A function to be called when a track is modified. |
| 297 | The modified track is passed as the argument to this function." |
| 298 | :group 'emms |
| 299 | :type 'function) |
| 300 | |
| 301 | (defcustom emms-directory (expand-file-name "emms" user-emacs-directory) |
| 302 | "*Directory variable from which all other emms file variables are derived." |
| 303 | :group 'emms |
| 304 | :type 'string) |
| 305 | |
| 306 | (defvar emms-player-playing-p nil |
| 307 | "The currently playing EMMS player, or nil.") |
| 308 | |
| 309 | (defvar emms-player-paused-p nil |
| 310 | "Whether the current player is paused or not.") |
| 311 | |
| 312 | (defvar emms-source-old-buffer nil |
| 313 | "The active buffer before a source was invoked. |
| 314 | This can be used if the source depends on the current buffer not |
| 315 | being the playlist buffer.") |
| 316 | |
| 317 | (defvar emms-playlist-buffer nil |
| 318 | "The current playlist buffer, if any.") |
| 319 | |
| 320 | (defvar emms-players-preference-f #'emms-players-default-preference-f |
| 321 | "Default function for player preference.") |
| 322 | |
| 323 | |
| 324 | ;;; ------------------------------------------------------------------ |
| 325 | ;;; macros |
| 326 | ;;; ------------------------------------------------------------------ |
| 327 | ;;; These need to be at the top of the file so that compilation works. |
| 328 | (defmacro with-current-emms-playlist (&rest body) |
| 329 | "Run BODY with the current buffer being the current playlist buffer. |
| 330 | This also disables any read-onliness of the current buffer." |
| 331 | `(progn |
| 332 | (when (or (not emms-playlist-buffer) |
| 333 | (not (buffer-live-p emms-playlist-buffer))) |
| 334 | (emms-playlist-current-clear)) |
| 335 | (let ((emms-source-old-buffer (or emms-source-old-buffer |
| 336 | (current-buffer)))) |
| 337 | (with-current-buffer emms-playlist-buffer |
| 338 | (let ((inhibit-read-only t)) |
| 339 | ,@body))))) |
| 340 | (put 'with-current-emms-playlist 'lisp-indent-function 0) |
| 341 | (put 'with-current-emms-playlist 'edebug-form-spec '(body)) |
| 342 | |
| 343 | (defmacro emms-with-inhibit-read-only-t (&rest body) |
| 344 | "Simple wrapper around `inhibit-read-only'." |
| 345 | `(let ((inhibit-read-only t)) |
| 346 | ,@body)) |
| 347 | (put 'emms-with-inhibit-read-only-t 'edebug-form-spec '(body)) |
| 348 | |
| 349 | (defmacro emms-with-widened-buffer (&rest body) |
| 350 | `(save-restriction |
| 351 | (widen) |
| 352 | ,@body)) |
| 353 | (put 'emms-with-widened-buffer 'edebug-form-spec '(body)) |
| 354 | |
| 355 | (defmacro emms-walk-tracks (&rest body) |
| 356 | "Execute BODY for each track in the current buffer, starting at point. |
| 357 | Point will be placed at the beginning of the track before |
| 358 | executing BODY. |
| 359 | |
| 360 | Point will not be restored afterward." |
| 361 | (let ((donep (make-symbol "donep"))) |
| 362 | `(let ((,donep nil)) |
| 363 | ;; skip to first track if not on one |
| 364 | (unless (emms-playlist-track-at (point)) |
| 365 | (condition-case nil |
| 366 | (emms-playlist-next) |
| 367 | (error |
| 368 | (setq ,donep t)))) |
| 369 | ;; walk tracks |
| 370 | (while (not ,donep) |
| 371 | ,@body |
| 372 | (condition-case nil |
| 373 | (emms-playlist-next) |
| 374 | (error |
| 375 | (setq ,donep t))))))) |
| 376 | (put 'emms-walk-tracks 'lisp-indent-function 0) |
| 377 | (put 'emms-walk-tracks 'edebug-form-spec '(body)) |
| 378 | |
| 379 | (defvar emms-player-base-format-list |
| 380 | '("ogg" "mp3" "wav" "mpg" "mpeg" "wmv" "wma" |
| 381 | "mov" "avi" "divx" "ogm" "ogv" "asf" "mkv" |
| 382 | "rm" "rmvb" "mp4" "flac" "vob" "m4a" "ape" |
| 383 | "flv" "webm" "aif" "opus" "spc") |
| 384 | "A list of common formats which player definitions can use.") |
| 385 | |
| 386 | |
| 387 | ;;; ------------------------------------------------------------------ |
| 388 | ;;; User Interface |
| 389 | ;;; ------------------------------------------------------------------ |
| 390 | (defun emms-start () |
| 391 | "Start playing the current track in the EMMS playlist." |
| 392 | (interactive) |
| 393 | (unless emms-player-playing-p |
| 394 | (emms-player-start (emms-playlist-current-selected-track)))) |
| 395 | |
| 396 | (defun emms-stop () |
| 397 | "Stop any current EMMS playback." |
| 398 | (interactive) |
| 399 | (when emms-player-playing-p |
| 400 | (emms-player-stop))) |
| 401 | |
| 402 | (defun emms-next () |
| 403 | "Start playing the next track in the EMMS playlist. |
| 404 | This might behave funny if called from `emms-player-next-function', |
| 405 | so use `emms-next-noerror' in that case." |
| 406 | (interactive) |
| 407 | (when emms-player-playing-p |
| 408 | (emms-stop)) |
| 409 | (emms-playlist-current-select-next) |
| 410 | (emms-start)) |
| 411 | |
| 412 | (defun emms-next-noerror () |
| 413 | "Start playing the next track in the EMMS playlist. |
| 414 | Unlike `emms-next', this function doesn't signal an error when called |
| 415 | at the end of the playlist. |
| 416 | This function should only be called when no player is playing. |
| 417 | This is a good function to put in `emms-player-next-function'." |
| 418 | (interactive) |
| 419 | (when emms-player-playing-p |
| 420 | (error "A track is already being played")) |
| 421 | (cond (emms-repeat-track |
| 422 | (emms-start)) |
| 423 | (emms-single-track ; buffer local |
| 424 | (emms-stop)) |
| 425 | ;; attempt to play the next track but ignore errors |
| 426 | ((condition-case nil |
| 427 | (progn |
| 428 | (emms-playlist-current-select-next) |
| 429 | t) |
| 430 | (error nil)) |
| 431 | (if (funcall emms-ok-track-function |
| 432 | (emms-playlist-current-selected-track)) |
| 433 | (emms-start) |
| 434 | (emms-next-noerror))) |
| 435 | (t |
| 436 | (message "No next track in playlist")))) |
| 437 | |
| 438 | (defun emms-previous () |
| 439 | "Start playing the previous track in the EMMS playlist." |
| 440 | (interactive) |
| 441 | (when emms-player-playing-p |
| 442 | (emms-stop)) |
| 443 | (emms-playlist-current-select-previous) |
| 444 | (emms-start)) |
| 445 | |
| 446 | (defun emms-random () |
| 447 | "Jump to a random track." |
| 448 | (interactive) |
| 449 | (when emms-player-playing-p |
| 450 | (emms-stop)) |
| 451 | (emms-playlist-current-select-random) |
| 452 | (emms-start)) |
| 453 | |
| 454 | (defun emms-pause () |
| 455 | "Pause the current player. |
| 456 | If player hasn't started, then start it now." |
| 457 | (interactive) |
| 458 | (if emms-player-playing-p |
| 459 | (emms-player-pause) |
| 460 | (emms-start))) |
| 461 | |
| 462 | (defun emms-seek (duration) |
| 463 | "Seek the current player by DURATION from its current position. |
| 464 | DURATION can be: |
| 465 | |
| 466 | - A single number, in which case it is interpreted as seconds. |
| 467 | |
| 468 | - A string of form [-][HH:]MM:SS.m, where HH is hours, MM is |
| 469 | minutes, and SS is seconds. |
| 470 | |
| 471 | In both forms seconds can be a floating point number. A negative |
| 472 | value seeks backwards." |
| 473 | (interactive "sDuration to seek: ") |
| 474 | (emms-ensure-player-playing-p) |
| 475 | (emms-player-seek (emms-timespec-to-secs duration))) |
| 476 | |
| 477 | (defun emms-seek-to (timestamp) |
| 478 | "Seek the current player to TIMESTAMP. |
| 479 | TIMESTAMP can be: |
| 480 | |
| 481 | - A single number, in which case it is interpreted as seconds. |
| 482 | |
| 483 | - A string of form [HH:]MM:SS.m, where HH is hours, MM is |
| 484 | minutes, and SS is seconds. |
| 485 | |
| 486 | In both forms seconds can be a floating point number." |
| 487 | (interactive "sTimestamp to seek to: ") |
| 488 | (emms-ensure-player-playing-p) |
| 489 | (emms-player-seek-to (max 0 (emms-timespec-to-secs timestamp)))) |
| 490 | |
| 491 | (defun emms-timespec-to-secs (timespec) |
| 492 | "Convert TIMESPEC to seconds. |
| 493 | |
| 494 | If TIMESPEC is number, use it verbatim. If TIMESPEC is string, |
| 495 | use `emms-timestr-to-secs' for conversion. Otherwise return |
| 496 | zero." |
| 497 | (cond ((numberp timespec) timespec) |
| 498 | ((stringp timespec) (emms-timestr-to-secs timespec)) |
| 499 | (t 0))) |
| 500 | |
| 501 | (defun emms-timestr-to-secs (timespec) |
| 502 | "Convert TIMESPEC to seconds. |
| 503 | |
| 504 | TIMESPEC is assumed to be a string of form [-][[HH:]MM:]SS, where |
| 505 | HH is hours, MM is minutes and SS is seconds. Each element is |
| 506 | converted to number by calling `string-to-number'. Missing or |
| 507 | invalid elements are treated as zeros." |
| 508 | (let ((tokens (split-string timespec ":"))) |
| 509 | (if (= (length tokens) 1) |
| 510 | ;; seconds only |
| 511 | (string-to-number (car tokens)) |
| 512 | ;; HH:MM:SS |
| 513 | (let* ((sign (if (< (string-to-number (car tokens)) 0) -1 1)) |
| 514 | (revtokens (reverse tokens)) |
| 515 | (seconds (abs (string-to-number (or (pop revtokens) "0")))) |
| 516 | (minutes (abs (string-to-number (or (pop revtokens) "0")))) |
| 517 | (hours (abs (string-to-number (or (pop revtokens) "0"))))) |
| 518 | (* sign (+ (* 60 60 hours) (* 60 minutes) seconds)))))) |
| 519 | |
| 520 | (defun emms-seek-forward () |
| 521 | "Seek ten seconds forward." |
| 522 | (interactive) |
| 523 | (when emms-player-playing-p |
| 524 | (emms-player-seek emms-seek-seconds))) |
| 525 | |
| 526 | (defun emms-seek-backward () |
| 527 | "Seek ten seconds backward." |
| 528 | (interactive) |
| 529 | (when emms-player-playing-p |
| 530 | (emms-player-seek (- emms-seek-seconds)))) |
| 531 | |
| 532 | (defun emms-show (&optional insertp) |
| 533 | "Describe the current EMMS track in the minibuffer. |
| 534 | If INSERTP is non-nil, insert the description into the current buffer instead. |
| 535 | This function uses `emms-show-format' to format the current track." |
| 536 | (interactive "P") |
| 537 | (let ((string (if emms-player-playing-p |
| 538 | (format emms-show-format |
| 539 | (emms-track-description |
| 540 | (emms-playlist-current-selected-track))) |
| 541 | "Nothing playing right now"))) |
| 542 | (if insertp |
| 543 | (insert string) |
| 544 | (message "%s" string)))) |
| 545 | |
| 546 | (defun emms-shuffle () |
| 547 | "Shuffle the current playlist. |
| 548 | This uses `emms-playlist-shuffle-function'." |
| 549 | (interactive) |
| 550 | (with-current-emms-playlist |
| 551 | (save-excursion |
| 552 | (funcall emms-playlist-shuffle-function)))) |
| 553 | |
| 554 | (defun emms-sort () |
| 555 | "Sort the current playlist. |
| 556 | This uses `emms-playlist-sort-function'." |
| 557 | (interactive) |
| 558 | (with-current-emms-playlist |
| 559 | (save-excursion |
| 560 | (funcall emms-playlist-sort-function)))) |
| 561 | |
| 562 | (defun emms-uniq () |
| 563 | "Remove duplicates from the current playlist. |
| 564 | This uses `emms-playlist-uniq-function'." |
| 565 | (interactive) |
| 566 | (with-current-emms-playlist |
| 567 | (save-excursion |
| 568 | (funcall emms-playlist-uniq-function)))) |
| 569 | |
| 570 | (defun emms-toggle-single-track () |
| 571 | "Toggle if Emms plays a single track and stops." |
| 572 | (interactive) |
| 573 | (with-current-emms-playlist |
| 574 | (cond (emms-single-track |
| 575 | (setq emms-single-track nil) |
| 576 | (message "single track mode disabled for %s" |
| 577 | (buffer-name))) |
| 578 | (t (setq emms-single-track t) |
| 579 | (message "single track mode enabled for %s" |
| 580 | (buffer-name)))))) |
| 581 | |
| 582 | (defun emms-toggle-random-playlist () |
| 583 | "Toggle whether emms plays the tracks randomly or sequentially. |
| 584 | See `emms-random-playlist'." |
| 585 | (interactive) |
| 586 | (customize-set-variable 'emms-random-playlist (not emms-random-playlist)) |
| 587 | (if emms-random-playlist |
| 588 | (message "Will play the tracks randomly.") |
| 589 | (message "Will play the tracks sequentially."))) |
| 590 | |
| 591 | (defun emms-toggle-repeat-playlist () |
| 592 | "Toggle whether emms repeats the playlist after it is done. |
| 593 | See `emms-repeat-playlist'." |
| 594 | (interactive) |
| 595 | (setq emms-repeat-playlist (not emms-repeat-playlist)) |
| 596 | (if emms-repeat-playlist |
| 597 | (message "Will repeat the playlist after it is done.") |
| 598 | (message "Will stop after the playlist is over."))) |
| 599 | |
| 600 | (defun emms-toggle-repeat-track () |
| 601 | "Toggle whether emms repeats the current track. |
| 602 | See `emms-repeat-track'." |
| 603 | (interactive) |
| 604 | (setq emms-repeat-track (not emms-repeat-track)) |
| 605 | (if emms-repeat-track |
| 606 | (message "Will repeat the current track.") |
| 607 | (message "Will advance to the next track after this one."))) |
| 608 | |
| 609 | (defun emms-sort-track-name-less-p (a b) |
| 610 | "Return non-nil if the track name of A sorts before B." |
| 611 | (string< (emms-track-name a) |
| 612 | (emms-track-name b))) |
| 613 | |
| 614 | (defun emms-ensure-player-playing-p () |
| 615 | "Raise an error if no player is playing right now." |
| 616 | (when (not emms-player-playing-p) |
| 617 | (error "No EMMS player playing right now"))) |
| 618 | |
| 619 | (defun emms-completing-read (&rest args) |
| 620 | "Read a string in the minibuffer, with completion. |
| 621 | Set `emms-completing-read' to determine which function to use. |
| 622 | |
| 623 | See `completing-read' for a description of ARGS." |
| 624 | (apply emms-completing-read-function args)) |
| 625 | |
| 626 | (defun emms-display-modes () |
| 627 | "Display the current EMMS play modes." |
| 628 | (interactive) |
| 629 | (with-current-emms-playlist |
| 630 | (message |
| 631 | "repeat playlist: %s, repeat track: %s, random: %s, single %s" |
| 632 | (if emms-repeat-playlist "yes" "no") |
| 633 | (if emms-repeat-track "yes" "no") |
| 634 | (if emms-random-playlist "yes" "no") |
| 635 | (if emms-single-track "yes" "no")))) |
| 636 | |
| 637 | |
| 638 | ;;; ------------------------------------------------------------------ |
| 639 | ;;; Utility functions |
| 640 | ;;; ------------------------------------------------------------------ |
| 641 | (defun emms-insert-file-contents (filename &optional visit) |
| 642 | "Insert the contents of file FILENAME after point. |
| 643 | Do character code conversion and end-of-line conversion, but none |
| 644 | of the other unnecessary things like format decoding or |
| 645 | `find-file-hook'. |
| 646 | |
| 647 | If VISIT is non-nil, the buffer's visited filename |
| 648 | and last save file modtime are set, and it is marked unmodified. |
| 649 | If visiting and the file does not exist, visiting is completed |
| 650 | before the error is signaled." |
| 651 | (let ((format-alist nil) |
| 652 | (after-insert-file-functions nil) |
| 653 | (inhibit-file-name-handlers |
| 654 | (append '(jka-compr-handler image-file-handler epa-file-handler) |
| 655 | inhibit-file-name-handlers)) |
| 656 | (inhibit-file-name-operation 'insert-file-contents)) |
| 657 | (insert-file-contents filename visit))) |
| 658 | |
| 659 | |
| 660 | ;;; ------------------------------------------------------------------ |
| 661 | ;;; Dictionaries |
| 662 | ;;; ------------------------------------------------------------------ |
| 663 | ;; This is a simple helper data structure, used by both players |
| 664 | ;; and tracks. |
| 665 | (defsubst emms-dictionary (name) |
| 666 | "Create a new dictionary of type NAME." |
| 667 | (list name)) |
| 668 | |
| 669 | (defsubst emms-dictionary-type (dict) |
| 670 | "Return the type of the dictionary DICT." |
| 671 | (car dict)) |
| 672 | |
| 673 | (defun emms-dictionary-get (dict name &optional default) |
| 674 | "Return the value of NAME in DICT." |
| 675 | (let ((item (assq name (cdr dict)))) |
| 676 | (if item |
| 677 | (cdr item) |
| 678 | default))) |
| 679 | |
| 680 | (defun emms-dictionary-set (dict name value) |
| 681 | "Set the value of NAME in DICT to VALUE." |
| 682 | (let ((item (assq name (cdr dict)))) |
| 683 | (if item |
| 684 | (setcdr item value) |
| 685 | (setcdr dict (append (cdr dict) |
| 686 | (list (cons name value)))))) |
| 687 | dict) |
| 688 | |
| 689 | |
| 690 | ;;; ------------------------------------------------------------------ |
| 691 | ;;; Tracks |
| 692 | ;;; ------------------------------------------------------------------ |
| 693 | ;; This is a simple datatype to store track information. |
| 694 | ;; Each track consists of a type (a symbol) and a name (a string). |
| 695 | ;; In addition, each track has an associated dictionary of information. |
| 696 | (defun emms-track (type name) |
| 697 | "Create an EMMS track with type TYPE and name NAME." |
| 698 | (let ((track (when emms-cache-get-function |
| 699 | (funcall emms-cache-get-function type name)))) |
| 700 | (when (not track) |
| 701 | (setq track (emms-dictionary '*track*)) |
| 702 | ;; Prevent the cache from being called for these two sets |
| 703 | (let ((emms-cache-modified-function nil)) |
| 704 | (emms-track-set track 'type type) |
| 705 | (emms-track-set track 'name name)) |
| 706 | (when emms-cache-set-function |
| 707 | (funcall emms-cache-set-function type name track))) |
| 708 | ;; run any hooks regardless of a cache hit, as the entry may be |
| 709 | ;; old |
| 710 | (run-hook-with-args 'emms-track-initialize-functions track) |
| 711 | track)) |
| 712 | |
| 713 | (defun emms-track-p (obj) |
| 714 | "True if OBJ is an emms track." |
| 715 | (and (listp obj) |
| 716 | (eq (car obj) '*track*))) |
| 717 | |
| 718 | (defun emms-track-type (track) |
| 719 | "Return the type of TRACK." |
| 720 | (emms-track-get track 'type)) |
| 721 | |
| 722 | (defun emms-track-file-p (track) |
| 723 | "True if TRACK is a file type" |
| 724 | (eq 'file (emms-track-type track))) |
| 725 | |
| 726 | (defun emms-track-name (track) |
| 727 | "Return the name of TRACK." |
| 728 | (emms-track-get track 'name)) |
| 729 | |
| 730 | (defun emms-track-get (track name &optional default) |
| 731 | "Return the value of NAME for TRACK. |
| 732 | If there is no value, return DEFAULT (or nil, if not given)." |
| 733 | (emms-dictionary-get track name default)) |
| 734 | |
| 735 | (defun emms-track-set (track name value) |
| 736 | "Set the value of NAME for TRACK to VALUE." |
| 737 | (emms-dictionary-set track name value) |
| 738 | (when emms-cache-modified-function |
| 739 | (funcall emms-cache-modified-function track))) |
| 740 | |
| 741 | (defun emms-track-description (track) |
| 742 | "Return a description of TRACK. |
| 743 | This function uses the global value for |
| 744 | `emms-track-description-function', rather than anything the |
| 745 | current mode might have set. |
| 746 | |
| 747 | Use `emms-track-force-description' instead if you need to insert |
| 748 | a description into a playlist buffer." |
| 749 | (funcall (default-value 'emms-track-description-function) track)) |
| 750 | |
| 751 | (defun emms-track-updated (track) |
| 752 | "Information in TRACK got updated." |
| 753 | (run-hook-with-args 'emms-track-info-filters track) |
| 754 | (emms-playlist-track-updated track) |
| 755 | (run-hook-with-args 'emms-track-updated-functions track)) |
| 756 | |
| 757 | (defun emms-track-simple-description (track) |
| 758 | "Simple function to give a user-readable description of a track. |
| 759 | If it's a file track, just return the file name. Otherwise, |
| 760 | return the type and the name with a colon in between. |
| 761 | Hex-encoded characters in URLs are replaced by the decoded |
| 762 | character." |
| 763 | (let ((type (emms-track-type track))) |
| 764 | (cond ((eq 'file type) |
| 765 | (emms-track-name track)) |
| 766 | ((eq 'url type) |
| 767 | (emms-format-url-track-name (emms-track-name track))) |
| 768 | (t (concat (symbol-name type) |
| 769 | ": " (emms-track-name track)))))) |
| 770 | |
| 771 | (defun emms-format-url-track-name (name) |
| 772 | "Format URL track name for better readability." |
| 773 | (url-unhex-string name)) |
| 774 | |
| 775 | (defun emms-track-force-description (track) |
| 776 | "Always return text that describes TRACK. |
| 777 | This is used when inserting a description into a buffer. |
| 778 | |
| 779 | The reason for this is that if no text was returned (i.e. the |
| 780 | user defined a track function that returned nil or the empty |
| 781 | string), a confusing error message would result." |
| 782 | (let ((desc (funcall emms-track-description-function track))) |
| 783 | (if (and (stringp desc) (not (string= desc ""))) |
| 784 | desc |
| 785 | (emms-track-simple-description track)))) |
| 786 | |
| 787 | (defun emms-track-get-year (track) |
| 788 | "Get year of TRACK for display. |
| 789 | There is the separation between the \\='release date\\=' and the |
| 790 | \\='original date\\='. This difference matters e.g. for |
| 791 | re-releases (anniversaries and such) where the release date is |
| 792 | more recent than the original release date. In such cases the |
| 793 | user probably wants the original release date so this is what we |
| 794 | show." |
| 795 | (or |
| 796 | (emms-format-date-to-year (emms-track-get track 'info-date)) |
| 797 | (emms-format-date-to-year (emms-track-get track 'info-originaldate)) |
| 798 | (emms-track-get track 'info-year) |
| 799 | (emms-track-get track 'info-originalyear))) |
| 800 | |
| 801 | (defun emms-format-date-to-year (date) |
| 802 | "Try to extract year part from DATE. |
| 803 | Return nil if the year cannot be extracted." |
| 804 | (when date |
| 805 | (let ((year (nth 5 (parse-time-string date)))) |
| 806 | (if year (number-to-string year) |
| 807 | (when (string-match "^[ \t]*\\([0-9]\\{4\\}\\)" date) |
| 808 | (match-string 1 date)))))) |
| 809 | |
| 810 | |
| 811 | ;;; ------------------------------------------------------------------ |
| 812 | ;;; The Playlist |
| 813 | ;;; ------------------------------------------------------------------ |
| 814 | ;; Playlists are stored in buffers. The current playlist buffer is |
| 815 | ;; remembered in the `emms-playlist' variable. The buffer consists of |
| 816 | ;; any kind of data. Strings of text with a `emms-track' property are |
| 817 | ;; the tracks in the buffer. |
| 818 | (defvar emms-playlist-buffers nil |
| 819 | "The list of EMMS playlist buffers. |
| 820 | You should use the `emms-playlist-buffer-list' function to |
| 821 | retrieve a current list of EMMS buffers. Never use this variable |
| 822 | for that purpose.") |
| 823 | |
| 824 | (defvar emms-playlist-selected-marker nil |
| 825 | "The marker for the currently selected track.") |
| 826 | (make-variable-buffer-local 'emms-playlist-selected-marker) |
| 827 | |
| 828 | (defvar emms-playlist-buffer-p nil |
| 829 | "Non-nil if the current buffer is an EMMS playlist.") |
| 830 | (make-variable-buffer-local 'emms-playlist-buffer-p) |
| 831 | |
| 832 | (defvar emms-queue-lock nil |
| 833 | "The playlist name the active playlist queue is locked to, if any.") |
| 834 | |
| 835 | (defun emms-playlist-ensure-playlist-buffer () |
| 836 | "Throw an error if we're not in a playlist-buffer." |
| 837 | (when (not emms-playlist-buffer-p) |
| 838 | (error "Not an EMMS playlist buffer"))) |
| 839 | |
| 840 | (defun emms-lock-queue () |
| 841 | "Lock the current active playlist queue to it's playlist. |
| 842 | This allows for uninterrupted playlist playback while browsing other playlists." |
| 843 | (interactive) |
| 844 | (setq emms-queue-lock (buffer-name emms-playlist-buffer)) |
| 845 | (message (concat "Active playlist queue is locked to " emms-queue-lock))) |
| 846 | |
| 847 | (defun emms-unlock-queue () |
| 848 | "Unlock the current active playlist queue from its playlist." |
| 849 | (interactive) |
| 850 | (setq emms-queue-lock nil) |
| 851 | (message "Active playlist queue is unlocked.")) |
| 852 | |
| 853 | (defun emms-playlist-set-playlist-buffer (&optional buffer) |
| 854 | "Set the current playlist buffer if the queue is not locked to it's playlist." |
| 855 | (interactive |
| 856 | (if (not emms-queue-lock) |
| 857 | (list (let* ((buf-list (mapcar #'(lambda (buf) |
| 858 | (list (buffer-name buf))) |
| 859 | (emms-playlist-buffer-list))) |
| 860 | (sorted-buf-list (sort buf-list |
| 861 | #'(lambda (lbuf rbuf) |
| 862 | (< (length (car lbuf)) |
| 863 | (length (car rbuf)))))) |
| 864 | (default (or (and emms-playlist-buffer-p |
| 865 | ;; default to current buffer |
| 866 | (buffer-name)) |
| 867 | ;; pick shortest buffer name, since it is |
| 868 | ;; likely to be a shared prefix |
| 869 | (car sorted-buf-list)))) |
| 870 | (emms-completing-read "Playlist buffer to make current: " |
| 871 | sorted-buf-list nil t default))))) |
| 872 | (if (not emms-queue-lock) |
| 873 | (let ((buf (if buffer |
| 874 | (get-buffer buffer) |
| 875 | (current-buffer)))) |
| 876 | (with-current-buffer buf |
| 877 | (emms-playlist-ensure-playlist-buffer)) |
| 878 | (setq emms-playlist-buffer buf) |
| 879 | (when (called-interactively-p 'interactive) |
| 880 | (message "Set current EMMS playlist buffer")) |
| 881 | buf) |
| 882 | (message (concat "The active playlist queue is locked to " emms-queue-lock)))) |
| 883 | |
| 884 | (defun emms-playlist-new (&optional name) |
| 885 | "Create a new playlist buffer. |
| 886 | The buffer is named NAME, but made unique. NAME defaults to |
| 887 | `emms-playlist-buffer-name'. If called interactively, the new |
| 888 | buffer is also selected." |
| 889 | (interactive) |
| 890 | (let ((buf (generate-new-buffer (or name |
| 891 | emms-playlist-buffer-name)))) |
| 892 | (with-current-buffer buf |
| 893 | (when (not (eq major-mode emms-playlist-default-major-mode)) |
| 894 | (funcall emms-playlist-default-major-mode)) |
| 895 | (setq emms-playlist-buffer-p t)) |
| 896 | (add-to-list 'emms-playlist-buffers buf) |
| 897 | (when (called-interactively-p 'interactive) |
| 898 | (switch-to-buffer buf)) |
| 899 | buf)) |
| 900 | |
| 901 | (defun emms-playlist-buffer-list () |
| 902 | "Return a list of EMMS playlist buffers. |
| 903 | The first element is guaranteed to be the current EMMS playlist |
| 904 | buffer, if it exists, otherwise the slot will be used for the |
| 905 | other EMMS buffers. The list will be in newest-first order." |
| 906 | ;; prune dead buffers |
| 907 | (setq emms-playlist-buffers (emms-delete-if (lambda (buf) |
| 908 | (not (buffer-live-p buf))) |
| 909 | emms-playlist-buffers)) |
| 910 | ;; add new buffers |
| 911 | (mapc (lambda (buf) |
| 912 | (when (buffer-live-p buf) |
| 913 | (with-current-buffer buf |
| 914 | (when (and emms-playlist-buffer-p |
| 915 | (not (memq buf emms-playlist-buffers))) |
| 916 | (setq emms-playlist-buffers |
| 917 | (cons buf emms-playlist-buffers)))))) |
| 918 | (buffer-list)) |
| 919 | ;; force current playlist buffer to head position |
| 920 | (when (and (buffer-live-p emms-playlist-buffer) |
| 921 | (not (eq (car emms-playlist-buffers) emms-playlist-buffer))) |
| 922 | (setq emms-playlist-buffers (cons emms-playlist-buffer |
| 923 | (delete emms-playlist-buffer |
| 924 | emms-playlist-buffers)))) |
| 925 | emms-playlist-buffers) |
| 926 | |
| 927 | (defun emms-playlist-current-kill () |
| 928 | "Kill the current EMMS playlist buffer and switch to the next one." |
| 929 | (interactive) |
| 930 | (when (buffer-live-p emms-playlist-buffer) |
| 931 | (let ((new (cadr (emms-playlist-buffer-list)))) |
| 932 | (if new |
| 933 | (let ((old emms-playlist-buffer)) |
| 934 | (setq emms-playlist-buffer new |
| 935 | emms-playlist-buffers (cdr emms-playlist-buffers)) |
| 936 | (kill-buffer old) |
| 937 | (switch-to-buffer emms-playlist-buffer)) |
| 938 | (with-current-buffer emms-playlist-buffer |
| 939 | (bury-buffer)))))) |
| 940 | |
| 941 | (defun emms-playlist-current-clear () |
| 942 | "Clear the current playlist. |
| 943 | If no current playlist exists, a new one is generated." |
| 944 | (interactive) |
| 945 | (if (or (not emms-playlist-buffer) |
| 946 | (not (buffer-live-p emms-playlist-buffer))) |
| 947 | (setq emms-playlist-buffer (emms-playlist-new)) |
| 948 | (with-current-buffer emms-playlist-buffer |
| 949 | (emms-playlist-clear)))) |
| 950 | |
| 951 | (defun emms-playlist-clear () |
| 952 | "Clear the current buffer." |
| 953 | (interactive) |
| 954 | (emms-playlist-ensure-playlist-buffer) |
| 955 | (let ((inhibit-read-only t)) |
| 956 | (widen) |
| 957 | (delete-region (point-min) |
| 958 | (point-max))) |
| 959 | (run-hooks 'emms-playlist-cleared-hook)) |
| 960 | |
| 961 | ;;; Point movement within the playlist buffer. |
| 962 | (defun emms-playlist-track-at (&optional pos) |
| 963 | "Return the track at POS (point if not given), or nil if none." |
| 964 | (emms-playlist-ensure-playlist-buffer) |
| 965 | (emms-with-widened-buffer |
| 966 | (get-text-property (or pos (point)) |
| 967 | 'emms-track))) |
| 968 | |
| 969 | (defun emms-playlist-next () |
| 970 | "Move to the next track in the current buffer." |
| 971 | (emms-playlist-ensure-playlist-buffer) |
| 972 | (let ((next (next-single-property-change (point) |
| 973 | 'emms-track))) |
| 974 | (when (not next) |
| 975 | (error "No next track")) |
| 976 | (when (not (emms-playlist-track-at next)) |
| 977 | (setq next (next-single-property-change next 'emms-track))) |
| 978 | (when (or (not next) |
| 979 | (= next (point-max))) |
| 980 | (error "No next track")) |
| 981 | (goto-char next))) |
| 982 | |
| 983 | (defun emms-playlist-previous () |
| 984 | "Move to the previous track in the current buffer." |
| 985 | (emms-playlist-ensure-playlist-buffer) |
| 986 | (let ((prev (previous-single-property-change (point) |
| 987 | 'emms-track))) |
| 988 | (when (not prev) |
| 989 | (error "No previous track")) |
| 990 | (when (not (get-text-property prev 'emms-track)) |
| 991 | (setq prev (or (previous-single-property-change prev 'emms-track) |
| 992 | (point-min)))) |
| 993 | (when (or (not prev) |
| 994 | (not (get-text-property prev 'emms-track))) |
| 995 | (error "No previous track")) |
| 996 | (goto-char prev))) |
| 997 | |
| 998 | (defun emms-playlist-first () |
| 999 | "Move to the first track in the current buffer." |
| 1000 | (emms-playlist-ensure-playlist-buffer) |
| 1001 | (let ((first (condition-case nil |
| 1002 | (save-excursion |
| 1003 | (goto-char (point-min)) |
| 1004 | (when (not (emms-playlist-track-at (point))) |
| 1005 | (emms-playlist-next)) |
| 1006 | (point)) |
| 1007 | (error |
| 1008 | nil)))) |
| 1009 | (if first |
| 1010 | (goto-char first) |
| 1011 | (error "No first track")))) |
| 1012 | |
| 1013 | (defun emms-playlist-last () |
| 1014 | "Move to the last track in the current buffer." |
| 1015 | (emms-playlist-ensure-playlist-buffer) |
| 1016 | (let ((last (condition-case nil |
| 1017 | (save-excursion |
| 1018 | (goto-char (point-max)) |
| 1019 | (emms-playlist-previous) |
| 1020 | (point)) |
| 1021 | (error |
| 1022 | nil)))) |
| 1023 | (if last |
| 1024 | (goto-char last) |
| 1025 | (error "No last track")))) |
| 1026 | |
| 1027 | (defun emms-playlist-delete-track () |
| 1028 | "Delete the track at point." |
| 1029 | (emms-playlist-ensure-playlist-buffer) |
| 1030 | (funcall emms-playlist-delete-track-function)) |
| 1031 | |
| 1032 | ;;; Track selection |
| 1033 | (defun emms-playlist-selected-track () |
| 1034 | "Return the currently selected track." |
| 1035 | (emms-playlist-ensure-playlist-buffer) |
| 1036 | (when emms-playlist-selected-marker |
| 1037 | (emms-playlist-track-at emms-playlist-selected-marker))) |
| 1038 | |
| 1039 | (defun emms-playlist-current-selected-track () |
| 1040 | "Return the currently selected track in the current playlist." |
| 1041 | (with-current-emms-playlist |
| 1042 | (emms-playlist-selected-track))) |
| 1043 | |
| 1044 | (defun emms-playlist-selected-track-at-p (&optional point) |
| 1045 | "Return non-nil if POINT (defaulting to point) is on the selected track." |
| 1046 | (when emms-playlist-selected-marker |
| 1047 | (or (= emms-playlist-selected-marker |
| 1048 | (or point (point))) |
| 1049 | (let ((p (previous-single-property-change (or point (point)) |
| 1050 | 'emms-track))) |
| 1051 | (when p |
| 1052 | (= emms-playlist-selected-marker |
| 1053 | p)))))) |
| 1054 | |
| 1055 | (defun emms-playlist-select (pos) |
| 1056 | "Select the track at POS." |
| 1057 | (emms-playlist-ensure-playlist-buffer) |
| 1058 | (when (not (emms-playlist-track-at pos)) |
| 1059 | (error "No track at position %s" pos)) |
| 1060 | (when (not emms-playlist-selected-marker) |
| 1061 | (setq emms-playlist-selected-marker (make-marker))) |
| 1062 | (set-marker-insertion-type emms-playlist-selected-marker t) |
| 1063 | (set-marker emms-playlist-selected-marker pos) |
| 1064 | (run-hooks 'emms-playlist-selection-changed-hook)) |
| 1065 | |
| 1066 | (defun emms-playlist-select-next () |
| 1067 | "Select the next track in the current buffer." |
| 1068 | (emms-playlist-ensure-playlist-buffer) |
| 1069 | (save-excursion |
| 1070 | (goto-char (if (and emms-playlist-selected-marker |
| 1071 | (marker-position emms-playlist-selected-marker)) |
| 1072 | emms-playlist-selected-marker |
| 1073 | (point-min))) |
| 1074 | (condition-case nil |
| 1075 | (progn |
| 1076 | (if emms-repeat-playlist |
| 1077 | (condition-case nil |
| 1078 | (emms-playlist-next) |
| 1079 | (error |
| 1080 | (emms-playlist-first))) |
| 1081 | (emms-playlist-next)) |
| 1082 | (emms-playlist-select (point))) |
| 1083 | (error |
| 1084 | (error "No next track in playlist"))))) |
| 1085 | |
| 1086 | (defun emms-playlist-current-select-next () |
| 1087 | "Select the next track in the current playlist." |
| 1088 | (with-current-emms-playlist |
| 1089 | (emms-playlist-select-next))) |
| 1090 | |
| 1091 | (defun emms-playlist-select-previous () |
| 1092 | "Select the previous track in the current buffer." |
| 1093 | (emms-playlist-ensure-playlist-buffer) |
| 1094 | (save-excursion |
| 1095 | (goto-char (if (and emms-playlist-selected-marker |
| 1096 | (marker-position emms-playlist-selected-marker)) |
| 1097 | emms-playlist-selected-marker |
| 1098 | (point-max))) |
| 1099 | (condition-case nil |
| 1100 | (progn |
| 1101 | (if emms-repeat-playlist |
| 1102 | (condition-case nil |
| 1103 | (emms-playlist-previous) |
| 1104 | (error |
| 1105 | (emms-playlist-last))) |
| 1106 | (emms-playlist-previous)) |
| 1107 | (emms-playlist-select (point))) |
| 1108 | (error |
| 1109 | (error "No previous track in playlist"))))) |
| 1110 | |
| 1111 | (defun emms-playlist-current-select-previous () |
| 1112 | "Select the previous track in the current playlist." |
| 1113 | (with-current-emms-playlist |
| 1114 | (emms-playlist-select-previous))) |
| 1115 | |
| 1116 | (defun emms-playlist-select-random () |
| 1117 | "Select a random track in the current buffer." |
| 1118 | (emms-playlist-ensure-playlist-buffer) |
| 1119 | ;; FIXME: This is rather inefficient. |
| 1120 | (save-excursion |
| 1121 | (let ((track-indices nil)) |
| 1122 | (goto-char (point-min)) |
| 1123 | (emms-walk-tracks |
| 1124 | (setq track-indices (cons (point) |
| 1125 | track-indices))) |
| 1126 | (setq track-indices (vconcat track-indices)) |
| 1127 | (emms-playlist-select (aref track-indices |
| 1128 | (random (length track-indices))))))) |
| 1129 | |
| 1130 | (defun emms-playlist-current-select-random () |
| 1131 | "Select a random track in the current playlist." |
| 1132 | (with-current-emms-playlist |
| 1133 | (emms-playlist-select-random))) |
| 1134 | |
| 1135 | (defun emms-playlist-select-first () |
| 1136 | "Select the first track in the current buffer." |
| 1137 | (emms-playlist-ensure-playlist-buffer) |
| 1138 | (save-excursion |
| 1139 | (emms-playlist-first) |
| 1140 | (emms-playlist-select (point)))) |
| 1141 | |
| 1142 | (defun emms-playlist-current-select-first () |
| 1143 | "Select the first track in the current playlist." |
| 1144 | (with-current-emms-playlist |
| 1145 | (emms-playlist-select-first))) |
| 1146 | |
| 1147 | (defun emms-playlist-select-last () |
| 1148 | "Select the last track in the current buffer." |
| 1149 | (emms-playlist-ensure-playlist-buffer) |
| 1150 | (save-excursion |
| 1151 | (emms-playlist-last) |
| 1152 | (emms-playlist-select (point)))) |
| 1153 | |
| 1154 | (defun emms-playlist-current-select-last () |
| 1155 | "Select the last track in the current playlist." |
| 1156 | (with-current-emms-playlist |
| 1157 | (emms-playlist-select-last))) |
| 1158 | |
| 1159 | ;;; Playlist manipulation |
| 1160 | (defun emms-playlist-insert-track (track) |
| 1161 | "Insert TRACK at the current position into the playlist. |
| 1162 | This uses `emms-playlist-insert-track-function'." |
| 1163 | (emms-playlist-ensure-playlist-buffer) |
| 1164 | (funcall emms-playlist-insert-track-function track)) |
| 1165 | |
| 1166 | (defun emms-playlist-update-track () |
| 1167 | "Update TRACK at point. |
| 1168 | This uses `emms-playlist-update-track-function'." |
| 1169 | (emms-playlist-ensure-playlist-buffer) |
| 1170 | (funcall emms-playlist-update-track-function)) |
| 1171 | |
| 1172 | (defun emms-playlist-insert-source (source &rest args) |
| 1173 | "Insert tracks from SOURCE, supplying ARGS as arguments." |
| 1174 | (emms-playlist-ensure-playlist-buffer) |
| 1175 | (save-restriction |
| 1176 | (narrow-to-region (point) |
| 1177 | (point)) |
| 1178 | (apply source args) |
| 1179 | (run-hooks 'emms-playlist-source-inserted-hook))) |
| 1180 | |
| 1181 | (defun emms-playlist-current-insert-source (source &rest args) |
| 1182 | "Insert tracks from SOURCE in the current playlist. |
| 1183 | This is supplying ARGS as arguments to the source." |
| 1184 | (with-current-emms-playlist |
| 1185 | (apply #'emms-playlist-insert-source source args))) |
| 1186 | |
| 1187 | (defun emms-playlist-tracks-in-region (beg end) |
| 1188 | "Return all tracks between BEG and END." |
| 1189 | (emms-playlist-ensure-playlist-buffer) |
| 1190 | (let ((tracks nil)) |
| 1191 | (save-restriction |
| 1192 | (narrow-to-region beg end) |
| 1193 | (goto-char (point-min)) |
| 1194 | (emms-walk-tracks |
| 1195 | (setq tracks (cons (emms-playlist-track-at (point)) |
| 1196 | tracks)))) |
| 1197 | tracks)) |
| 1198 | |
| 1199 | (defun emms-playlist-track-updated (track) |
| 1200 | "Update TRACK in all playlist buffers." |
| 1201 | (mapc (lambda (buf) |
| 1202 | (with-current-buffer buf |
| 1203 | (when emms-playlist-buffer-p |
| 1204 | (save-excursion |
| 1205 | (let ((pos (text-property-any (point-min) (point-max) |
| 1206 | 'emms-track track))) |
| 1207 | (while pos |
| 1208 | (goto-char pos) |
| 1209 | (emms-playlist-update-track) |
| 1210 | (setq pos (text-property-any |
| 1211 | (next-single-property-change (point) |
| 1212 | 'emms-track) |
| 1213 | (point-max) |
| 1214 | 'emms-track |
| 1215 | track)))))))) |
| 1216 | (buffer-list)) |
| 1217 | t) |
| 1218 | |
| 1219 | ;;; Simple playlist buffer |
| 1220 | (defun emms-playlist-simple-insert-track (track) |
| 1221 | "Insert the description of TRACK at point." |
| 1222 | (emms-playlist-ensure-playlist-buffer) |
| 1223 | (let ((inhibit-read-only t)) |
| 1224 | (insert (emms-propertize (emms-track-force-description track) |
| 1225 | 'emms-track track) |
| 1226 | "\n"))) |
| 1227 | |
| 1228 | (defun emms-playlist-simple-update-track () |
| 1229 | "Update the track at point. |
| 1230 | Since we don't do anything special with the track anyway, just |
| 1231 | ignore this." |
| 1232 | nil) |
| 1233 | |
| 1234 | (defun emms-playlist-simple-delete-track () |
| 1235 | "Delete the track at point." |
| 1236 | (emms-playlist-ensure-playlist-buffer) |
| 1237 | (when (not (emms-playlist-track-at (point))) |
| 1238 | (error "No track at point")) |
| 1239 | (let ((inhibit-read-only t) |
| 1240 | (region (emms-property-region (point) 'emms-track))) |
| 1241 | (delete-region (car region) |
| 1242 | (cdr region)))) |
| 1243 | |
| 1244 | (defun emms-playlist-simple-shuffle () |
| 1245 | "Shuffle the whole playlist buffer." |
| 1246 | (emms-playlist-ensure-playlist-buffer) |
| 1247 | (let ((inhibit-read-only t) |
| 1248 | (current nil)) |
| 1249 | (widen) |
| 1250 | (when emms-player-playing-p |
| 1251 | (setq current (emms-playlist-selected-track)) |
| 1252 | (goto-char emms-playlist-selected-marker) |
| 1253 | (emms-playlist-delete-track)) |
| 1254 | (let* ((tracks (vconcat (emms-playlist-tracks-in-region (point-min) |
| 1255 | (point-max)))) |
| 1256 | (len (length tracks)) |
| 1257 | (i 0)) |
| 1258 | (delete-region (point-min) |
| 1259 | (point-max)) |
| 1260 | (run-hooks 'emms-playlist-cleared-hook) |
| 1261 | (emms-shuffle-vector tracks) |
| 1262 | (when current |
| 1263 | (emms-playlist-insert-track current)) |
| 1264 | (while (< i len) |
| 1265 | (emms-playlist-insert-track (aref tracks i)) |
| 1266 | (setq i (1+ i)))) |
| 1267 | (emms-playlist-select-first) |
| 1268 | (goto-char (point-max)))) |
| 1269 | |
| 1270 | (defun emms-playlist-simple-sort () |
| 1271 | "Sort the whole playlist buffer." |
| 1272 | (emms-playlist-ensure-playlist-buffer) |
| 1273 | (widen) |
| 1274 | (let ((inhibit-read-only t) |
| 1275 | (current (emms-playlist-selected-track)) |
| 1276 | (tracks (emms-playlist-tracks-in-region (point-min) |
| 1277 | (point-max)))) |
| 1278 | (delete-region (point-min) |
| 1279 | (point-max)) |
| 1280 | (run-hooks 'emms-playlist-cleared-hook) |
| 1281 | (mapc #'emms-playlist-insert-track |
| 1282 | (sort tracks emms-sort-lessp-function)) |
| 1283 | (let ((pos (text-property-any (point-min) |
| 1284 | (point-max) |
| 1285 | 'emms-track current))) |
| 1286 | (if pos |
| 1287 | (emms-playlist-select pos) |
| 1288 | (emms-playlist-first))))) |
| 1289 | |
| 1290 | (defun emms-uniq-list (list stringify) |
| 1291 | "Compare stringfied element of list, and remove duplicate elements." |
| 1292 | ;; This uses a fast append list, keeping a pointer to the last cons |
| 1293 | ;; cell of the list (TAIL). It might be worthwhile to provide an |
| 1294 | ;; abstraction for this eventually. |
| 1295 | (let* ((hash (make-hash-table :test 'equal)) |
| 1296 | (result (cons nil nil)) |
| 1297 | (tail result)) |
| 1298 | (dolist (element list) |
| 1299 | (let ((str (funcall stringify element))) |
| 1300 | (when (not (gethash str hash)) |
| 1301 | (setcdr tail (cons element nil)) |
| 1302 | (setq tail (cdr tail))) |
| 1303 | (puthash str t hash))) |
| 1304 | (cdr result))) |
| 1305 | |
| 1306 | (defun emms-playlist-simple-uniq () |
| 1307 | "Remove duplicate tracks." |
| 1308 | ;; TODO: This seems unnecessarily destructive. |
| 1309 | (emms-playlist-ensure-playlist-buffer) |
| 1310 | (widen) |
| 1311 | (let ((inhibit-read-only t) |
| 1312 | (current (emms-playlist-selected-track)) |
| 1313 | (tracks (emms-playlist-tracks-in-region (point-min) |
| 1314 | (point-max)))) |
| 1315 | (delete-region (point-min) (point-max)) |
| 1316 | (run-hooks 'emms-playlist-cleared-hook) |
| 1317 | (mapc #'emms-playlist-insert-track |
| 1318 | (nreverse |
| 1319 | (emms-uniq-list tracks 'emms-track-name))) |
| 1320 | (let ((pos (text-property-any (point-min) |
| 1321 | (point-max) |
| 1322 | 'emms-track current))) |
| 1323 | (if pos |
| 1324 | (emms-playlist-select pos) |
| 1325 | (emms-playlist-first))))) |
| 1326 | |
| 1327 | (defun emms-default-ok-track-function (track) |
| 1328 | "A function which OKs all tracks for playing by default." |
| 1329 | (ignore track) ;; explicit ignore |
| 1330 | t) |
| 1331 | |
| 1332 | (defun emms-playlist-length () |
| 1333 | "Display the total playing time of the current playlist." |
| 1334 | (interactive) |
| 1335 | (let ((acc 0)) |
| 1336 | (with-current-emms-playlist |
| 1337 | (goto-char (point-min)) |
| 1338 | (emms-walk-tracks |
| 1339 | (setq acc (+ acc (emms-track-get (emms-playlist-track-at (point)) 'info-playing-time))))) |
| 1340 | (let ((minutes (/ acc 60)) |
| 1341 | (seconds (% acc 60))) |
| 1342 | (message (if (>= minutes 60) |
| 1343 | (format "%d:%02d:%02d" (/ minutes 60) (% minutes 60) seconds) |
| 1344 | (format "%02d:%02d" minutes seconds)))))) |
| 1345 | |
| 1346 | (defalias 'emms-playlist-total-playing-time 'emms-playlist-length) |
| 1347 | |
| 1348 | |
| 1349 | ;;; ------------------------------------------------------------------ |
| 1350 | ;;; Helper functions |
| 1351 | ;;; ------------------------------------------------------------------ |
| 1352 | (defun emms-property-region (pos prop) |
| 1353 | "Return a pair of the beginning and end of the property PROP at POS. |
| 1354 | If POS does not contain PROP, try to find PROP just before POS." |
| 1355 | (let (begin end) |
| 1356 | (if (and (> pos (point-min)) |
| 1357 | (get-text-property (1- pos) prop)) |
| 1358 | (setq begin (previous-single-property-change (1- pos) prop)) |
| 1359 | (if (get-text-property pos prop) |
| 1360 | (setq begin pos) |
| 1361 | (error "Cannot find the %s property at the given position" prop))) |
| 1362 | (if (get-text-property pos prop) |
| 1363 | (setq end (next-single-property-change pos prop)) |
| 1364 | (if (and (> pos (point-min)) |
| 1365 | (get-text-property (1- pos) prop)) |
| 1366 | (setq end pos) |
| 1367 | (error "Cannot find the %s property at the given position" prop))) |
| 1368 | (cons (or begin (point-min)) |
| 1369 | (or end (point-max))))) |
| 1370 | |
| 1371 | (defun emms-shuffle-vector (vector) |
| 1372 | "Shuffle VECTOR." |
| 1373 | (let ((i (- (length vector) 1))) |
| 1374 | (while (>= i 0) |
| 1375 | (let* ((r (random (1+ i))) |
| 1376 | (old (aref vector r))) |
| 1377 | (aset vector r (aref vector i)) |
| 1378 | (aset vector i old)) |
| 1379 | (setq i (- i 1)))) |
| 1380 | vector) |
| 1381 | |
| 1382 | (defun emms-le-to-int (vec) |
| 1383 | "Convert bytes in VEC to an integer. |
| 1384 | Bytes are assumed to be in little-endian order, that is, the |
| 1385 | least significant byte first. |
| 1386 | |
| 1387 | If VEC is nil, return zero." |
| 1388 | (apply '+ (seq-map-indexed (lambda (elt idx) |
| 1389 | (* (expt 2 (* 8 idx)) elt)) |
| 1390 | vec))) |
| 1391 | |
| 1392 | (defun emms-be-to-int (vec) |
| 1393 | "Convert bytes in VEC to an integer. |
| 1394 | Bytes are assumed to be in big-endian order, that is, the most |
| 1395 | significant byte first. |
| 1396 | |
| 1397 | If VEC is nil, return zero." |
| 1398 | (emms-le-to-int (reverse vec))) |
| 1399 | |
| 1400 | (defun emms-from-twos-complement (num bits) |
| 1401 | "Convert integer NUM from two's complement with BITS bits." |
| 1402 | (let ((signmask (ash 1 (1- bits)))) |
| 1403 | (if (= (logand num signmask) signmask) |
| 1404 | ;; negative |
| 1405 | (* -1 (1+ (logand (lognot num) (1- signmask)))) |
| 1406 | ;; positive |
| 1407 | num))) |
| 1408 | |
| 1409 | (defun emms-extract-bits (int from &optional to) |
| 1410 | "Extract consequent set bits FROM[..TO] from INT. |
| 1411 | The first (least significant, rightmost) bit is zero. Return the |
| 1412 | integer value of bits as if they would have been shifted to right |
| 1413 | by FROM positions." |
| 1414 | (unless to (setq to from)) |
| 1415 | (let ((num-bits (1+ (- to from))) |
| 1416 | (mask (1- (expt 2 (1+ to))))) |
| 1417 | (when (> num-bits 0) (ash (logand int mask) (- from))))) |
| 1418 | |
| 1419 | (defun emms-equal-lists (x y) |
| 1420 | "Compare two lists X and Y for equality. |
| 1421 | List elements can be in any order, and Y can have more elements |
| 1422 | than X. |
| 1423 | |
| 1424 | This is a special helper function for tests. It is not meant for |
| 1425 | general use." |
| 1426 | (cond ((and (not (proper-list-p x)) |
| 1427 | (not (proper-list-p y))) |
| 1428 | (equal x y)) |
| 1429 | ((and (proper-list-p x) |
| 1430 | (proper-list-p y)) |
| 1431 | (seq-every-p (lambda (elt-x) |
| 1432 | (seq-find (lambda (elt-y) |
| 1433 | (emms-equal-lists elt-x elt-y)) |
| 1434 | y)) |
| 1435 | x)))) |
| 1436 | |
| 1437 | ;;; ------------------------------------------------------------------ |
| 1438 | ;;; Sources |
| 1439 | ;;; ------------------------------------------------------------------ |
| 1440 | ;; A source is just a function which is called in a playlist buffer. |
| 1441 | ;; It should use `emms-playlist-insert-track' to insert the tracks it |
| 1442 | ;; knows about. |
| 1443 | ;; |
| 1444 | ;; The define-emms-source macro also defines functions |
| 1445 | ;; emms-play-SOURCE and emms-add-SOURCE. The former will replace the |
| 1446 | ;; current playlist, while the latter will add to the end. |
| 1447 | (defmacro define-emms-source (name arglist &rest body) |
| 1448 | "Define a new EMMS source called NAME. |
| 1449 | This macro defines three functions: `emms-source-NAME', |
| 1450 | `emms-play-NAME' and `emms-add-NAME'. BODY should use |
| 1451 | `emms-playlist-insert-track' to insert all tracks to be played, |
| 1452 | which is exactly what `emms-source-NAME' will do. The other two |
| 1453 | functions will be simple wrappers around `emms-source-NAME'; any |
| 1454 | `interactive' form that you specify in BODY will end up in these. |
| 1455 | See emms-source-file.el for some examples." |
| 1456 | (declare |
| 1457 | (debug (&define [&name "emms-" symbolp ; name all generated functions |
| 1458 | (lambda (_ prefix name) |
| 1459 | (mapconcat (lambda (type) |
| 1460 | (concat prefix type |
| 1461 | (symbol-name name))) |
| 1462 | '("add-" "insert-" "play-" "source-") |
| 1463 | " "))] |
| 1464 | lambda-list [&optional stringp] |
| 1465 | [&optional ("interactive" interactive)] def-body)) |
| 1466 | (doc-string 3) (indent defun)) |
| 1467 | (let ((source-name (intern (format "emms-source-%s" name))) |
| 1468 | (source-play (intern (format "emms-play-%s" name))) |
| 1469 | (source-add (intern (format "emms-add-%s" name))) |
| 1470 | (source-insert (intern (format "emms-insert-%s" name))) |
| 1471 | (source-insert-current (intern (format "emms-insert-current-%s" name))) |
| 1472 | (docstring "A source of tracks for EMMS.") |
| 1473 | (interactive nil) |
| 1474 | (call-args (delete '&rest |
| 1475 | (delete '&optional |
| 1476 | arglist)))) |
| 1477 | (when (stringp (car body)) |
| 1478 | (setq docstring (car body) |
| 1479 | body (cdr body))) |
| 1480 | (when (eq 'interactive (caar body)) |
| 1481 | (setq interactive (car body) |
| 1482 | body (cdr body))) |
| 1483 | `(progn |
| 1484 | (defun ,source-name ,arglist |
| 1485 | ,docstring |
| 1486 | ,@body) |
| 1487 | (defun ,source-play ,arglist |
| 1488 | ,docstring |
| 1489 | ,interactive |
| 1490 | (if current-prefix-arg |
| 1491 | (let ((current-prefix-arg nil)) |
| 1492 | (emms-source-add ',source-name ,@call-args)) |
| 1493 | (emms-source-play ',source-name ,@call-args))) |
| 1494 | (defun ,source-add ,arglist |
| 1495 | ,docstring |
| 1496 | ,interactive |
| 1497 | (if current-prefix-arg |
| 1498 | (let ((current-prefix-arg nil)) |
| 1499 | (emms-source-play ',source-name ,@call-args)) |
| 1500 | (emms-source-add ',source-name ,@call-args))) |
| 1501 | (defun ,source-insert ,arglist |
| 1502 | ,docstring |
| 1503 | ,interactive |
| 1504 | (emms-source-insert ',source-name ,@call-args)) |
| 1505 | (defun ,source-insert-current ,arglist |
| 1506 | ,docstring |
| 1507 | ,interactive |
| 1508 | (emms-source-insert-current ',source-name ,@call-args))))) |
| 1509 | |
| 1510 | (defun emms-source-play (source &rest args) |
| 1511 | "Play the tracks of SOURCE, after first clearing the EMMS playlist." |
| 1512 | (emms-stop) |
| 1513 | (emms-playlist-current-clear) |
| 1514 | (apply #'emms-playlist-current-insert-source source args) |
| 1515 | (emms-playlist-current-select-first) |
| 1516 | (emms-start)) |
| 1517 | |
| 1518 | (defun emms-source-add (source &rest args) |
| 1519 | "Add the tracks of SOURCE at the current position in the playlist." |
| 1520 | (with-current-emms-playlist |
| 1521 | (save-excursion |
| 1522 | (goto-char (point-max)) |
| 1523 | (apply #'emms-playlist-current-insert-source source args)) |
| 1524 | (when (or (not emms-playlist-selected-marker) |
| 1525 | (not (marker-position emms-playlist-selected-marker))) |
| 1526 | (emms-playlist-select-first)))) |
| 1527 | |
| 1528 | (defun emms-source-insert (source &rest args) |
| 1529 | "Insert the tracks from SOURCE in the current buffer." |
| 1530 | (with-current-emms-playlist |
| 1531 | (apply #'emms-playlist-insert-source source args))) |
| 1532 | |
| 1533 | (defun emms-source-insert-current (source &rest args) |
| 1534 | "Insert from SOURCE into buffer at current track." |
| 1535 | (with-current-emms-playlist |
| 1536 | (if (not emms-playlist-selected-marker) |
| 1537 | (error "no current playlist track") |
| 1538 | (goto-char emms-playlist-selected-marker) |
| 1539 | (forward-line)) |
| 1540 | (apply #'emms-playlist-insert-source source args))) |
| 1541 | |
| 1542 | ;;; User-defined playlists |
| 1543 | ;;; FIXME: Shuffle is bogus here! (because of narrowing) |
| 1544 | (defmacro define-emms-combined-source (name shufflep sources) |
| 1545 | "Define a `emms-play-X' and `emms-add-X' function for SOURCES." |
| 1546 | `(define-emms-source ,name () |
| 1547 | "An EMMS source for a tracklist." |
| 1548 | (interactive) |
| 1549 | (mapc (lambda (source) |
| 1550 | (apply (car source) |
| 1551 | (cdr source))) |
| 1552 | ,sources) |
| 1553 | ,(when shufflep |
| 1554 | '(save-restriction |
| 1555 | (widen) |
| 1556 | (emms-shuffle))))) |
| 1557 | |
| 1558 | |
| 1559 | ;;; ------------------------------------------------------------------ |
| 1560 | ;;; Players |
| 1561 | ;;; ------------------------------------------------------------------ |
| 1562 | ;; A player is a data structure created by `emms-player'. |
| 1563 | ;; See the docstring of that function for more information. |
| 1564 | (defvar emms-player-stopped-p nil |
| 1565 | "Non-nil if the last EMMS player was stopped by the user.") |
| 1566 | |
| 1567 | (defun emms-player (start stop playablep) |
| 1568 | "Create a new EMMS player. |
| 1569 | The start function will be START, and the stop function STOP. |
| 1570 | PLAYABLEP should return non-nil for tracks that this player can |
| 1571 | play. |
| 1572 | |
| 1573 | When trying to play a track, EMMS walks through |
| 1574 | `emms-player-list'. For each player, it calls the PLAYABLEP |
| 1575 | function. The player corresponding to the first PLAYABLEP |
| 1576 | function that returns non-nil is used to play the track. To |
| 1577 | actually play the track, EMMS calls the START function, passing |
| 1578 | the chosen track as a parameter. |
| 1579 | |
| 1580 | If the user tells EMMS to stop playing, the STOP function is |
| 1581 | called. Once the player has finished playing, it should call |
| 1582 | `emms-player-stopped' to let EMMS know." |
| 1583 | (let ((p (emms-dictionary '*player*))) |
| 1584 | (emms-player-set p 'start start) |
| 1585 | (emms-player-set p 'stop stop) |
| 1586 | (emms-player-set p 'playablep playablep) |
| 1587 | p)) |
| 1588 | |
| 1589 | (defun emms-player-get (player name &optional inexistent) |
| 1590 | "Return the value of entry NAME in PLAYER." |
| 1591 | (let ((p (if (symbolp player) |
| 1592 | (symbol-value player) |
| 1593 | player))) |
| 1594 | (emms-dictionary-get p name inexistent))) |
| 1595 | |
| 1596 | (defun emms-player-set (player name value) |
| 1597 | "Set the value of entry NAME in PLAYER to VALUE." |
| 1598 | (let ((p (if (symbolp player) |
| 1599 | (symbol-value player) |
| 1600 | player))) |
| 1601 | (emms-dictionary-set p name value))) |
| 1602 | |
| 1603 | (defun emms-player-for (track) |
| 1604 | "Return an EMMS player capable of playing TRACK. |
| 1605 | This will be the first player whose PLAYABLEP function returns |
| 1606 | non-nil, or nil if no such player exists." |
| 1607 | (let ((lis emms-player-list)) |
| 1608 | (while (and lis |
| 1609 | (not (funcall (emms-player-get (car lis) 'playablep) |
| 1610 | track))) |
| 1611 | (setq lis (cdr lis))) |
| 1612 | (if lis |
| 1613 | (car lis) |
| 1614 | nil))) |
| 1615 | |
| 1616 | (defun emms-players-default-preference-f (track players) |
| 1617 | "Default preference function. |
| 1618 | |
| 1619 | Returns the first player." |
| 1620 | (ignore track) |
| 1621 | (car players)) |
| 1622 | |
| 1623 | (defun emms-players-preference (track players) |
| 1624 | "Call `emms-players-preference-f' with TRACK and PLAYERS. |
| 1625 | |
| 1626 | The function `emms-players-preference-f' must accept an Emms |
| 1627 | track and a list of players. It can be assumed that all of the |
| 1628 | players in PLAYERS can play TRACK. |
| 1629 | |
| 1630 | The function must return one of the players from PLAYERS." |
| 1631 | (funcall emms-players-preference-f track players)) |
| 1632 | |
| 1633 | (defun emms-players-for (track) |
| 1634 | "Return a player for TRACK. |
| 1635 | |
| 1636 | If the track can be played by more than one player, call |
| 1637 | `emms-players-preference' to choose a player." |
| 1638 | (let (players) |
| 1639 | (mapc |
| 1640 | #'(lambda (player) |
| 1641 | (when (funcall (emms-player-get player 'playablep) track) |
| 1642 | (push player players))) |
| 1643 | emms-player-list) |
| 1644 | (setq players (nreverse players)) |
| 1645 | (if (< 1 (length players)) |
| 1646 | (emms-players-preference track players) |
| 1647 | (car players)))) |
| 1648 | |
| 1649 | (defun emms-player-start (track) |
| 1650 | "Start playing TRACK." |
| 1651 | (if emms-player-playing-p |
| 1652 | (error "A player is already playing") |
| 1653 | (let ((player (emms-players-for track))) |
| 1654 | (when (not emms-player-list) |
| 1655 | (error "emms-player-list empty")) |
| 1656 | (if (not player) |
| 1657 | (error "Don't know how to play track: %S" track) |
| 1658 | ;; Change default-directory so we don't accidentally block any |
| 1659 | ;; directories the current buffer was visiting. |
| 1660 | (let ((default-directory "/")) |
| 1661 | (funcall (emms-player-get player 'start) |
| 1662 | track)))))) |
| 1663 | |
| 1664 | (defun emms-player-started (player) |
| 1665 | "Declare that the given EMMS PLAYER has started. |
| 1666 | This should only be done by the current player itself." |
| 1667 | (setq emms-player-playing-p player |
| 1668 | emms-player-paused-p nil) |
| 1669 | (run-hooks 'emms-player-started-hook)) |
| 1670 | |
| 1671 | (defun emms-player-stop () |
| 1672 | "Stop the current EMMS player." |
| 1673 | (when emms-player-playing-p |
| 1674 | (let ((emms-player-stopped-p t)) |
| 1675 | (funcall (emms-player-get emms-player-playing-p 'stop))) |
| 1676 | (setq emms-player-playing-p nil))) |
| 1677 | |
| 1678 | (defun emms-player-stopped () |
| 1679 | "Declare that the current EMMS player is finished. |
| 1680 | This should only be done by the current player itself." |
| 1681 | (setq emms-player-playing-p nil) |
| 1682 | (if emms-player-stopped-p |
| 1683 | (run-hooks 'emms-player-stopped-hook) |
| 1684 | (sleep-for emms-player-delay) |
| 1685 | (run-hooks 'emms-player-finished-hook) |
| 1686 | ;; switch to the current playlist to pick up the values of |
| 1687 | ;; buffer-local variables |
| 1688 | (with-current-emms-playlist |
| 1689 | (funcall emms-player-next-function)))) |
| 1690 | |
| 1691 | (defun emms-player-pause () |
| 1692 | "Pause the current EMMS player." |
| 1693 | (cond |
| 1694 | ((not emms-player-playing-p) |
| 1695 | (error "Can't pause player, nothing is playing")) |
| 1696 | (emms-player-paused-p |
| 1697 | (let ((resume (emms-player-get emms-player-playing-p 'resume)) |
| 1698 | (pause (emms-player-get emms-player-playing-p 'pause))) |
| 1699 | (cond |
| 1700 | (resume |
| 1701 | (funcall resume)) |
| 1702 | (pause |
| 1703 | (funcall pause)) |
| 1704 | (t |
| 1705 | (error "Player does not know how to pause")))) |
| 1706 | (setq emms-player-paused-p nil) |
| 1707 | (run-hooks 'emms-player-paused-hook)) |
| 1708 | (t |
| 1709 | (let ((pause (emms-player-get emms-player-playing-p 'pause))) |
| 1710 | (if pause |
| 1711 | (funcall pause) |
| 1712 | (error "Player does not know how to pause"))) |
| 1713 | (setq emms-player-paused-p t) |
| 1714 | (run-hooks 'emms-player-paused-hook)))) |
| 1715 | |
| 1716 | (defun emms-player-seek (seconds) |
| 1717 | "Seek the current player by SECONDS seconds. |
| 1718 | This can be a floating point number for fractions of a second, or |
| 1719 | negative to seek backwards." |
| 1720 | (if (not emms-player-playing-p) |
| 1721 | (error "Can't seek player, nothing playing right now") |
| 1722 | (let ((seek (emms-player-get emms-player-playing-p 'seek))) |
| 1723 | (if (not seek) |
| 1724 | (error "Player does not know how to seek") |
| 1725 | (funcall seek seconds) |
| 1726 | (run-hook-with-args 'emms-player-seeked-functions seconds))))) |
| 1727 | |
| 1728 | (defun emms-player-seek-to (seconds) |
| 1729 | "Seek the current player to SECONDS seconds. |
| 1730 | This can be a floating point number for fractions of a second, or |
| 1731 | negative to seek backwards." |
| 1732 | (if (not emms-player-playing-p) |
| 1733 | (error "Can't seek-to player, nothing playing right now") |
| 1734 | (let ((seek (emms-player-get emms-player-playing-p 'seek-to))) |
| 1735 | (if (not seek) |
| 1736 | (error "Player does not know how to seek-to") |
| 1737 | (funcall seek seconds) |
| 1738 | (run-hook-with-args 'emms-player-time-set-functions seconds))))) |
| 1739 | |
| 1740 | (provide 'emms) |
| 1741 | ;;; emms.el ends here |