1;;; emms-bookmarks.el --- Bookmarks for Emms. 2 3;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc. 4 5;; Author: Yoni Rabkin <yrk@gnu.org> 6;; Keywords: emms, bookmark 7 8;; This file is part of EMMS. 9 10;; EMMS is free software; you can redistribute it and/or modify 11;; it under the terms of the GNU General Public License as published by 12;; the Free Software Foundation; either version 3, or (at your option) 13;; any later version. 14;; 15;; EMMS is distributed in the hope that it will be useful, 16;; but WITHOUT ANY WARRANTY; without even the implied warranty of 17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18;; GNU General Public License for more details. 19;; 20;; You should have received a copy of the GNU General Public License 21;; along with EMMS; if not, write to the Free Software Foundation, 22;; Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 23 24;;; Commentary: 25;; 26;; You can use this to add ``temporal bookmarks'' (term by Lucas 27;; Bonnet) to your media files. The interesting functions here are 28;; `emms-bookmarks-next', `emms-bookmarks-prev', `emms-bookmarks-add' 29;; (which pauses the player while you describe the bookmark) and 30;; `emms-bookmarks-clear'. All of which do exactly what you think 31;; they do. 32 33;;; Code: 34 35 36;; dependencies 37(require 'emms) 38(require 'emms-playing-time) 39 40(defvar emms-bookmarks-prev-overshoot 5 41 "Time in seconds for skipping a previous bookmark.") 42 43(defun emms-bookmarks-reset (track) 44 "Remove all the bookmarks from TRACK." 45 (emms-track-set track 'bookmarks nil)) 46 47(defun emms-bookmarks-straight-insertion-sort (item l acc) 48 "Insert ITEM into the already sorted L, ACC should be nil." 49 (if (null l) 50 (append acc (list item)) 51 (cond ((< (cdr item) (cdr (car l))) (append acc (list item (car l)) (cdr l))) 52 (t (emms-bookmarks-straight-insertion-sort item (cdr l) (append acc (list (car l)))))))) 53 54(defun emms-bookmarks-get (track) 55 "Return the bookmark property from TRACK." 56 (emms-track-get track 'bookmarks)) 57 58(defun emms-bookmarks-set (track desc time) 59 "Set bookmark property for TRACK, text DESC at TIME seconds." 60 (let ((old-bookmarks (emms-track-get track 'bookmarks)) 61 (new-bookmarks nil)) 62 (setq new-bookmarks (emms-bookmarks-straight-insertion-sort (cons desc time) old-bookmarks nil)) 63 (emms-track-set track 'bookmarks new-bookmarks))) 64 65(defun emms-bookmarks-set-current (desc) 66 "Set bookmark property for the current track with text DESC." 67 (emms-bookmarks-set (emms-playlist-current-selected-track) desc emms-playing-time)) 68 69(defun emms-bookmarks-search (time track test) 70 "Return a bookmark based on heuristics. 71 72TIME should be a reference point in seconds. 73TRACK should be an Emms track. 74TEST should be a numerical comparator predicate." 75 (let ((s (append (list (cons "time" time)) (copy-sequence (emms-bookmarks-get track))))) 76 (sort s #'(lambda (a b) (funcall test (cdr a) (cdr b)))) 77 (while (not (= time (cdar s))) 78 (setq s (cdr s))) 79 (when (cdr s) 80 (car (cdr s))))) 81 82(defun emms-bookmarks-next-1 (time track) 83 "Return the bookmark after TIME for TRACK, otherwise return nil." 84 (emms-bookmarks-search time track #'<)) 85 86(defun emms-bookmarks-prev-1 (time track) 87 "Return the bookmark before TIME for TRACK, otherwise return nil." 88 (emms-bookmarks-search (- time emms-bookmarks-prev-overshoot) track #'>)) 89 90(defun emms-bookmarks-goto (search-f track failure-message) 91 "Seek the player to a bookmark. 92 93SEARCH-F should be a function which returns a bookmark. 94TRACK should be an Emms track. 95FAILURE-MESSAGE should be a string." 96 ;; note that when emms is paused then `emms-player-playing-p' => t 97 (when (not emms-player-playing-p) 98 (emms-start)) 99 (let ((m (funcall search-f emms-playing-time track))) 100 (if m 101 (progn 102 (emms-player-seek-to (cdr m)) 103 (message "%s" (car m))) 104 (message "%s" failure-message)))) 105 106 107;; entry points 108 109(defun emms-bookmarks-next () 110 "Seek to the next bookmark in the current track." 111 (interactive) 112 (emms-bookmarks-goto #'emms-bookmarks-next-1 113 (emms-playlist-current-selected-track) 114 "No next bookmark")) 115 116(defun emms-bookmarks-prev () 117 "Seek to the previous bookmark in the current track." 118 (interactive) 119 (emms-bookmarks-goto #'emms-bookmarks-prev-1 120 (emms-playlist-current-selected-track) 121 "No previous bookmark")) 122 123(defmacro emms-bookmarks-with-paused-player (&rest body) 124 "Eval BODY with player paused." 125 `(progn 126 (when (not emms-player-paused-p) (emms-pause)) 127 ,@body 128 (when emms-player-paused-p (emms-pause)))) 129 130;; can't use `interactive' to promt the user here because we want to 131;; pause the player before the prompt appears. 132(defun emms-bookmarks-add () 133 "Add a new bookmark to the current track. 134 135This function pauses the player while prompting the user for a 136description of the bookmark. The function resumes the player 137after the prompt." 138 (interactive) 139 (emms-bookmarks-with-paused-player 140 (let ((desc (read-string "Description: "))) 141 (if (emms-playlist-current-selected-track) 142 (emms-bookmarks-set-current desc) 143 (error "No current track to bookmark"))))) 144 145(defun emms-bookmarks-clear () 146 "Remove all the bookmarks from the current track." 147 (interactive) 148 (let ((this (emms-playlist-current-selected-track))) 149 (when this (emms-bookmarks-reset this)))) 150 151(provide 'emms-bookmarks) 152 153;;; emms-bookmarks.el ends here 154