Function: mpc--faster-toggle

mpc--faster-toggle is a byte-compiled function defined in mpc.el.gz.

Signature

(mpc--faster-toggle SPEEDUP STEP)

Source Code

;; Defined in /usr/src/emacs/lisp/mpc.el.gz
(defun mpc--faster-toggle (speedup step)
  (if mpc--faster-toggle-timer
      (mpc--faster-stop)
    (mpc-status-refresh) (mpc-proc-sync)
    (let* (songid       ;The ID of the currently ffwd/rewinding song.
           songduration ;The duration of that song.
           songtime     ;The time of the song last time we ran.
           oldtime      ;The time of day last time we ran.
           prevsongid)  ;The song we're in the process leaving.
      (let ((fun
             (lambda ()
               (let ((newsongid (cdr (assq 'songid mpc-status))))

                 (if (and (equal prevsongid newsongid)
                          (not (equal prevsongid songid)))
                     ;; We left prevsongid and came back to it.  Pretend it
                     ;; didn't happen.
                     (setq newsongid songid))

                 (cond
                  ((null newsongid) (mpc--faster-stop))
                  ((not (equal songid newsongid))
                   ;; We jumped to another song: reset.
                   (setq songid newsongid)
                   (setq songtime (string-to-number
                                   (cdr (assq 'time mpc-status))))
                   (setq songduration (mpc--songduration))
		   (setq oldtime (current-time)))
                  ((and (>= songtime songduration) mpc--faster-toggle-forward)
                   ;; Skip to the beginning of the next song.
                   (if (not (equal (cdr (assq 'state mpc-status)) "play"))
                       (mpc-proc-cmd "next" #'mpc-status-refresh)
                     ;; If we're playing, this is done automatically, so we
                     ;; don't need to do anything, or rather we *shouldn't*
                     ;; do anything otherwise there's a race condition where
                     ;; we could skip straight to the next next song.
                     nil))
                  ((and (<= songtime 0) (not mpc--faster-toggle-forward))
                   ;; Skip to the end of the previous song.
                   (setq prevsongid songid)
                   (mpc-proc-cmd "previous"
                    (lambda ()
                      (mpc-status-refresh
                       (lambda ()
                         (setq songid (cdr (assq 'songid mpc-status)))
                         (setq songtime (setq songduration (mpc--songduration)))
			 (setq oldtime (current-time))
                         (mpc-proc-cmd (list "seekid" songid songtime)))))))
                  (t
                   (setq speedup (+ speedup mpc--faster-acceleration))
                   (let ((newstep
			  (truncate
			   (* speedup
			      (float-time (time-since oldtime))))))
                     (if (<= newstep 1) (setq newstep 1))
		     (setq oldtime (time-add oldtime (/ newstep speedup)))
                     (if (not mpc--faster-toggle-forward)
                         (setq newstep (- newstep)))
                     (setq songtime (min songduration (+ songtime newstep)))
                     (unless (>= songtime songduration)
                       (condition-case nil
                           (mpc-proc-cmd
                            (list "seekid" songid songtime)
                            #'mpc-status-refresh)
                         (mpc-proc-error (mpc-status-refresh)))))))))))
        (setq mpc--faster-toggle-forward (> step 0))
        (funcall fun)                   ;Initialize values.
        (setq mpc--faster-toggle-timer
              (run-with-timer t 0.3 fun))))))