Emacs user interface for Music Player Daemon (https://www.musicpd.org/), a flexible, powerful, server-side application for playing music.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.7KB

  1. ;;; mpdel-song.el --- Display song information and control playback -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2018-2019 Damien Cassou
  3. ;; Author: Damien Cassou <damien@cassou.me>
  4. ;; Keywords: multimedia
  5. ;; Url: https://gitlab.petton.fr/mpdel/mpdel
  6. ;; Package-requires: ((emacs "25.1"))
  7. ;; Version: 1.0.0
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Executing `mpdel-song' opens a small buffer letting the user get
  20. ;; information and control the current song.
  21. ;;; Code:
  22. (require 'libmpdel)
  23. (require 'mpdel-core)
  24. ;;; Customization
  25. (defgroup mpdel-song nil
  26. "Control MPD's current song."
  27. :group 'libmpdel)
  28. (defcustom mpdel-song-small-increment "+2"
  29. "Time to seek to slowly move forward."
  30. :type 'string
  31. :group 'mpdel-song)
  32. (defcustom mpdel-song-small-decrement "-2"
  33. "Time to seek to slowly move backward."
  34. :type 'string
  35. :group 'mpdel-song)
  36. (defcustom mpdel-song-normal-increment "+10"
  37. "Time to seek to move forward."
  38. :type 'string
  39. :group 'mpdel-song)
  40. (defcustom mpdel-song-normal-decrement "-10"
  41. "Time to seek to move backward."
  42. :type 'string
  43. :group 'mpdel-song)
  44. (defcustom mpdel-song-large-increment "+30"
  45. "Time to seek to quickly move forward."
  46. :type 'string
  47. :group 'mpdel-song)
  48. (defcustom mpdel-song-large-decrement "-30"
  49. "Time to seek to quickly move backward."
  50. :type 'string
  51. :group 'mpdel-song)
  52. ;;; Private variables
  53. (defvar-local mpdel-song-song nil
  54. "Song displayed by current buffer.")
  55. (defvar-local mpdel-song-current-song-p nil
  56. "Non-nil if buffer displays the current song.
  57. When non-nil, the buffer keeps showing the current song, even
  58. when the song changes.")
  59. ;;; `navigel' configuration
  60. (navigel-method mpdel navigel-open ((song libmpdel-song) _target)
  61. (mpdel-song-open song))
  62. (navigel-method mpdel navigel-entity-at-point (&context (major-mode mpdel-song-mode))
  63. mpdel-song-song)
  64. ;;; Helper functions
  65. (defvar mpdel-song--timer nil
  66. "Store timer to refresh the seek buffer.")
  67. (defun mpdel-song--start-timer ()
  68. "Start refresh timer."
  69. (unless mpdel-song--timer
  70. (setq mpdel-song--timer (run-at-time t 1 #'mpdel-song-refresh (current-buffer)))))
  71. (defun mpdel-song--stop-timer ()
  72. "Stop refresh timer."
  73. (when mpdel-song--timer
  74. (cancel-timer mpdel-song--timer)
  75. (setq mpdel-song--timer nil)))
  76. (defun mpdel-song--seek (time)
  77. "Seek TIME within current song and refresh current buffer."
  78. (libmpdel-playback-seek time))
  79. (defun mpdel-song--display-play-state ()
  80. "Give information about current play state and control timer."
  81. (pcase (libmpdel-play-state)
  82. ('play (insert "Currently playing\n")
  83. (mpdel-song--start-timer))
  84. ('pause (insert "Currently paused\n")
  85. (mpdel-song--stop-timer))
  86. ('stop (insert "Currently stopped\n")
  87. (mpdel-song--stop-timer))))
  88. (defun mpdel-song--display-play-time (data)
  89. "Give information about current play time in DATA."
  90. (insert
  91. (format "%s / %s"
  92. (libmpdel-time-to-string (cdr (assq 'elapsed data)))
  93. (libmpdel-time-to-string (cdr (assq 'duration data))))))
  94. (defun mpdel-song--display-metadata ()
  95. "Give information about current song metadata."
  96. (let* ((song mpdel-song-song)
  97. (title (or (libmpdel-entity-name song) ""))
  98. (album (or (libmpdel-album-name song) ""))
  99. (artist (or (libmpdel-artist-name song) "")))
  100. (insert (format "Title: %s\nArtist: %s\nAlbum: %s\n"
  101. title
  102. artist
  103. album))))
  104. (defun mpdel-song--refresh-current-song (data buffer)
  105. "Write information about currently-played song in DATA to BUFFER.
  106. DATA is an alist returned by MPD server as answer to 'status'.
  107. In particular, it must contain key symbol `elapsed' and symbol
  108. `duration'."
  109. (with-current-buffer buffer
  110. (let ((inhibit-read-only t))
  111. (erase-buffer)
  112. (setq mpdel-song-song (libmpdel-current-song))
  113. (mpdel-song--display-play-state)
  114. (mpdel-song--display-metadata)
  115. (mpdel-song--display-play-time data))))
  116. (defun mpdel-song--refresh-non-current-song (buffer)
  117. "Write information about the song associated with BUFFER."
  118. (with-current-buffer buffer
  119. (let ((inhibit-read-only t))
  120. (erase-buffer)
  121. (mpdel-song--display-metadata))))
  122. ;;; Public interface
  123. (defun mpdel-song-refresh (&optional buffer)
  124. "Refresh MPDEL seek BUFFER, current buffer if nil."
  125. (interactive)
  126. (with-current-buffer (or buffer (current-buffer))
  127. (if mpdel-song-current-song-p
  128. (libmpdel-send-command "status" (lambda (data) (mpdel-song--refresh-current-song data buffer)))
  129. (mpdel-song--refresh-non-current-song (current-buffer)))))
  130. ;;;###autoload
  131. (defun mpdel-song-open (&optional song)
  132. "Open a buffer to display information about SONG.
  133. If SONG is nil, use current song instead.
  134. When SONG is nil, the buffer updates itself to keep showing
  135. latest song. Additionally, the buffer lets the user control
  136. playback."
  137. (interactive)
  138. (let* ((current-song-p (not song))
  139. (buffer (get-buffer-create (if current-song-p
  140. "*MPDEL Current Song*"
  141. (format "*MPDEL Song: %s*" (libmpdel-entity-name song)))))
  142. (refresh-fn (lambda () (mpdel-song-refresh buffer)))
  143. (song (or song (libmpdel-current-song))))
  144. (with-current-buffer buffer
  145. (mpdel-song-mode)
  146. (setq mpdel-song-song song)
  147. (setq mpdel-song-current-song-p current-song-p)
  148. (mpdel-song-refresh buffer)
  149. (when current-song-p
  150. (add-hook 'libmpdel-player-changed-hook refresh-fn)
  151. (add-hook 'kill-buffer-hook #'mpdel-song--stop-timer nil t)
  152. (add-hook 'kill-buffer-hook (lambda () (remove-hook 'libmpdel-player-changed-hook refresh-fn)) nil t))
  153. (pop-to-buffer (current-buffer)))))
  154. (define-key mpdel-core-map (kbd "v") #'mpdel-song-open)
  155. (defun mpdel-song-play ()
  156. "Start playing the song of the current buffer."
  157. (interactive)
  158. (libmpdel-play-song mpdel-song-song))
  159. (defun mpdel-song-quit-window ()
  160. "Quit window and kill its buffer.
  161. Killing the buffer instead of just burrying it is important when
  162. displaying current song: this is because the buffer keeps
  163. refreshing itself to display playback position."
  164. (interactive)
  165. (quit-window t))
  166. (defun mpdel-song-small-increment ()
  167. "Move forward by value of variable `mpdel-song-small-increment'."
  168. (interactive)
  169. (mpdel-song--seek mpdel-song-small-increment))
  170. (define-key mpdel-core-map (kbd "F") #'mpdel-song-small-increment)
  171. (defun mpdel-song-normal-increment ()
  172. "Move forward by value of variable `mpdel-song-normal-increment'."
  173. (interactive)
  174. (mpdel-song--seek mpdel-song-normal-increment))
  175. (define-key mpdel-core-map (kbd "f") #'mpdel-song-normal-increment)
  176. (defun mpdel-song-large-increment ()
  177. "Move forward by value of variable `mpdel-song-large-increment'."
  178. (interactive)
  179. (mpdel-song--seek mpdel-song-large-increment))
  180. (define-key mpdel-core-map (kbd "M-f") #'mpdel-song-large-increment)
  181. (defun mpdel-song-small-decrement ()
  182. "Move backward by value of variable `mpdel-song-small-decrement'."
  183. (interactive)
  184. (mpdel-song--seek mpdel-song-small-decrement))
  185. (define-key mpdel-core-map (kbd "B") #'mpdel-song-small-decrement)
  186. (defun mpdel-song-normal-decrement ()
  187. "Move backward by value of variable `mpdel-song-normal-decrement'."
  188. (interactive)
  189. (mpdel-song--seek mpdel-song-normal-decrement))
  190. (define-key mpdel-core-map (kbd "b") #'mpdel-song-normal-decrement)
  191. (defun mpdel-song-large-decrement ()
  192. "Move backward by value of variable `mpdel-song-large-decrement'."
  193. (interactive)
  194. (mpdel-song--seek mpdel-song-large-decrement))
  195. (define-key mpdel-core-map (kbd "M-b") #'mpdel-song-large-decrement)
  196. ;;; Major-mode
  197. (defvar mpdel-song-mode-map
  198. (let ((map (make-sparse-keymap)))
  199. (set-keymap-parent
  200. map
  201. (make-composed-keymap mpdel-core-map special-mode-map))
  202. (define-key map (kbd "g") #'mpdel-song-refresh)
  203. (define-key map (kbd "p") #'mpdel-song-play)
  204. (define-key map (kbd "q") #'mpdel-song-quit-window)
  205. map)
  206. "Keybindgs for `mpdel-song-mode'.")
  207. (define-derived-mode mpdel-song-mode special-mode "MPDEL song"
  208. "Guide the user to seek inside current song."
  209. (setq-local navigel-app 'mpdel))
  210. (provide 'mpdel-song)
  211. ;;; mpdel-song.el ends here