]> git.eshelyaron.com Git - emacs.git/commitdiff
Expand recognized time intervals for MPC seeking
authorMark Oteiza <mvoteiza@udel.edu>
Sun, 24 Sep 2017 12:17:34 +0000 (08:17 -0400)
committerMark Oteiza <mvoteiza@udel.edu>
Sun, 24 Sep 2017 12:22:12 +0000 (08:22 -0400)
Now accepts [+-]H:M:S and subsets.  Also accepts some odd variations of
it since the regexp is not strict.  One unpleasant caveat is that
string-to-number simply returns zero on failure instead of signaling an
error.  At the moment, there are cases where instead of getting
a user-error, the seek may simply not go where one expects it.
* lisp/mpc.el (mpc-read-seek): New function.
(mpc-seek-current): Use it.

lisp/mpc.el

index c23d8ced716fe6b0255ad49b690a3fc20ced0c6b..98f4a0318349adba3be98265c328ada5d838c3bc 100644 (file)
@@ -2403,10 +2403,38 @@ This is used so that they can be compared with `eq', which is needed for
   (interactive)
   (mpc-cmd-pause "0"))
 
+(defun mpc-read-seek (prompt)
+  "Read a seek time.
+Returns a string suitable for MPD \"seekcur\" protocol command."
+  (let* ((str (read-from-minibuffer prompt nil nil nil nil nil t))
+         (seconds "\\(?1:[[:digit:]]+\\(?:\\.[[:digit:]]*\\)?\\)")
+         (minsec (concat "\\(?2:[[:digit:]]+\\):" seconds "?"))
+         (hrminsec (concat "\\(?3:[[:digit:]]+\\):\\(?:" minsec "?\\|:\\)"))
+         time sign)
+    (setq str (string-trim str))
+    (when (memq (string-to-char str) '(?+ ?-))
+      (setq sign (string (string-to-char str)))
+      (setq str (substring str 1)))
+    (setq time
+          ;; `string-to-number' returns 0 on failure
+          (cond
+           ((string-match (concat "^" hrminsec "$") str)
+            (+ (* 3600 (string-to-number (match-string 3 str)))
+               (* 60 (string-to-number (or (match-string 2 str) "")))
+               (string-to-number (or (match-string 1 str) ""))))
+           ((string-match (concat "^" minsec "$") str)
+            (+ (* 60 (string-to-number (match-string 2 str)))
+               (string-to-number (match-string 1 str))))
+           ((string-match (concat "^" seconds "$") str)
+            (string-to-number (match-string 1 str)))
+           (t (user-error "Invalid time"))))
+    (setq time (number-to-string time))
+    (if (null sign) time (concat sign time))))
+
 (defun mpc-seek-current (pos)
   "Seek within current track."
   (interactive
-   (list (read-string "Position to go ([+-]seconds): ")))
+   (list (mpc-read-seek "Position to go ([+-][[H:]M:]seconds): ")))
   (mpc-cmd-seekcur pos))
 
 (defun mpc-toggle-play ()