Emacs library to communicate with Music Player Daemon (MPD, https://www.musicpd.org/), a flexible, powerful, server-side application for playing music.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

118 lines
4.5KB

  1. ;;; libmpdel-directory.el --- Handling directories -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2019 Damien Cassou
  3. ;; Author: Jose A Ortega <jao@gnu.org>
  4. ;; Keywords: multimedia
  5. ;; Url: https://gitlab.petton.fr/mpdel/libmpdel
  6. ;; Package-requires: ((emacs "25.1"))
  7. ;; Version: 1.2.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. ;; This module implements the directory datatype, used to traverse
  20. ;; local or virtual directories (collections) in MPD servers.
  21. ;;; Code:
  22. (require 'libmpdel)
  23. (require 'cl-lib)
  24. (cl-defstruct (libmpdel-directory
  25. (:constructor libmpdel--directory-create)
  26. (:conc-name libmpdel--directory-))
  27. (path nil :read-only t)
  28. (name nil :read-only t))
  29. (cl-defmethod libmpdel-entity-name ((_entity (eql directories)))
  30. "The name for the `directories' entity."
  31. "Directories")
  32. (cl-defmethod libmpdel-entity-to-criteria ((_entity (eql directories)))
  33. "Return a query to retrieve the list of directories from the server."
  34. "lsinfo \"\"")
  35. (defun libmpdel-directory--create-directories-from-data (data)
  36. "Return a list of directories extracted from DATA returned by MPD."
  37. (mapcar (lambda (dir-name) (libmpdel--directory-create :path dir-name))
  38. (libmpdel-sorted-entries data 'directory)))
  39. (cl-defmethod libmpdel-list ((_entity (eql directories)) function)
  40. "Call FUNCTION with a list of directories as argument."
  41. (libmpdel-send-command
  42. (libmpdel-entity-to-criteria 'directories)
  43. (lambda (data)
  44. (funcall function
  45. (libmpdel-directory--create-directories-from-data data)))))
  46. (cl-defmethod libmpdel-entity-name ((dir libmpdel-directory))
  47. "Return DIR's entity name."
  48. (or (libmpdel--directory-name dir)
  49. (file-name-nondirectory (or (libmpdel--directory-path dir) ""))))
  50. (cl-defmethod libmpdel-entity-id ((dir libmpdel-directory))
  51. "Return DIR's entity identifier."
  52. (libmpdel--directory-path dir))
  53. (eval-when-compile
  54. (declare-function dired-jump "dired-x"))
  55. (cl-defmethod libmpdel-dired ((dir libmpdel-directory))
  56. "Jump, using dired, to DIR's local directory."
  57. (require 'dired-x)
  58. (dired-jump t (expand-file-name (libmpdel--directory-path dir)
  59. libmpdel-music-directory)))
  60. (cl-defmethod libmpdel-entity-parent ((dir libmpdel-directory))
  61. "Return the directory containing DIR, or 'directories."
  62. (let ((path (file-name-directory (libmpdel--directory-path dir))))
  63. (if (> (length path) 1)
  64. (libmpdel--directory-create :path (substring path 0 -1))
  65. 'directories)))
  66. (cl-defmethod libmpdel-entity-to-criteria ((dir libmpdel-directory))
  67. "Return a search criteria to list the contents of DIR, as a string."
  68. (format "lsinfo %S" (libmpdel--directory-path dir)))
  69. (defun libmpdel-directory--make-dots (dir)
  70. "Return a list of the . and .. entries associated with the given DIR."
  71. (list
  72. (libmpdel--directory-create :path (libmpdel--directory-path dir) :name ".")
  73. (let ((parent (libmpdel-entity-parent dir)))
  74. (if (libmpdel-directory-p parent)
  75. (libmpdel--directory-create :path (libmpdel--directory-path parent)
  76. :name "..")
  77. parent))))
  78. (cl-defmethod libmpdel-list ((dir libmpdel-directory) function)
  79. "Call FUNCTION with all the entities contained in DIR."
  80. (libmpdel-send-command
  81. (libmpdel-entity-to-criteria dir)
  82. (lambda (data)
  83. (let* ((dirs (libmpdel-directory--create-directories-from-data data))
  84. (songs (libmpdel--create-songs-from-data data))
  85. (songs (cl-remove-if-not 'libmpdel-entity-name songs))
  86. (all (append (libmpdel-directory--make-dots dir) dirs songs)))
  87. (funcall function all)))))
  88. (cl-defmethod libmpdel-playlist-add ((dir libmpdel-directory) playlist)
  89. "Add the entities contained in DIR (that is, its children) to PLAYLIST."
  90. (libmpdel-list dir (lambda (children)
  91. (libmpdel-playlist-add (cddr children) playlist))))
  92. (provide 'libmpdel-directory)
  93. ;;; libmpdel-directory.el ends here