From 09749a0b2e102016d2a6a477092eda17e723989b Mon Sep 17 00:00:00 2001 From: jao Date: Sat, 23 Jan 2021 02:33:49 +0000 Subject: espotify: tangling to separate libs --- .gitignore | 2 ++ media/espotify.org | 75 ++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index d556e71..e18ee79 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.elc /media/espotify.el +/media/espotify-consult.el +/media/espotify-embark.el diff --git a/media/espotify.org b/media/espotify.org index 65e7ef5..27329d5 100644 --- a/media/espotify.org +++ b/media/espotify.org @@ -3,6 +3,10 @@ #+filetags: emacs #+PROPERTY: header-args :tangle yes :comments no :results silent +(/Note/: you can tangle this file (e.g., with =C-c C-v t= inside Emacs) +into three elisp libraries, =espotify.el=, =espotify-consult.el= and +=espotify-embark=.) + We have two kinds of interaction with Spotify: via its HTTP API to perform operations such as search, and via our local DBUS to talk to client players running in our computer, such as the official client, @@ -248,7 +252,14 @@ Let's start with an umbrella customization group: (espotify-call-spotify-via-dbus "OpenUri" uri)) #+end_src + #+begin_src emacs-lisp + (provide 'espotify) + #+end_src + * Search front-end using consult + :PROPERTIES: + :header-args: :tangle espotify-consult.el + :END: I am exploring [[https://github.com/minad/consult][consult.el]] (and friends) to replace ivy/counsel, inspired in part by [[https://protesilaos.com/codelog/2021-01-06-emacs-default-completion/][Protesilaos Stavrou's musings]], and liking a @@ -261,6 +272,9 @@ Let's start with an umbrella customization group: The top-level command will have this form: #+begin_src emacs-lisp + ;;; espotify-consult.el - consult support - -*- lexical-binding: t; -*- + + (require 'espotify) (require 'consult) (defvar espotify-consult-history nil) @@ -326,9 +340,12 @@ Let's start with an umbrella customization group: #+begin_src emacs-lisp (defun espotify--additional-info (x) - (or (alist-get 'name (alist-get 'album x)) - (alist-get 'name (car (alist-get 'artists x))) - (alist-get 'display_name (alist-get 'owner x)))) + (mapconcat 'identity + (seq-filter 'identity + `(,(alist-get 'name (alist-get 'album x)) + ,(alist-get 'name (car (alist-get 'artists x))) + ,(alist-get 'display_name (alist-get 'owner x)))) + ", ")) (defun espotify--format-item (x) (propertize (format "%s%s" @@ -349,7 +366,7 @@ Let's start with an umbrella customization group: consult looks up for it using the ~:lookup~ function, which we can simply define as: - #+begin_src emacs-lisp + #+begin_src emacs-lisp :tangle espotify-consult.el (require 'seq) (defun espotify--consult-lookup (_input cands cand) (seq-find (lambda (x) (string= cand x)) cands)) @@ -360,8 +377,8 @@ Let's start with an umbrella customization group: we can play the selected URI right away: #+begin_src emacs-lisp - (defun espotify--maybe-play (x) - (when-let (uri (espotify--uri x)) + (defun espotify--maybe-play (cand) + (when-let (uri (when cand (espotify--uri cand))) (espotify-play-uri uri))) #+end_src @@ -395,6 +412,9 @@ Let's start with an umbrella customization group: #+end_src * Adding metadata to candidates using Marginalia + :PROPERTIES: + :header-args: :tangle espotify-consult.el + :END: Let's add metadata fields to our candidates, so that packages like [[https://github.com/minad/marginalia][Marginalia]] can offer it to consult or selectrum. @@ -417,9 +437,16 @@ Let's start with an umbrella customization group: (add-to-list 'marginalia-annotators-heavy '(espotify-search-item . espotify-marginalia-annotate)) -#+end_src + #+end_src + + #+begin_src emacs-lisp + (provide 'espotify-consult) + #+end_src * Embark actions + :PROPERTIES: + :header-args: :tangle espotify-embark.el + :END: In addition to the default action (play the URI in the selected candidate), we can use embark to define other operations. For @@ -427,6 +454,8 @@ Let's start with an umbrella customization group: always look for an album to play: #+begin_src emacs-lisp + (require 'espotify-consult) + (require 'embark) (defvar espotify--current-item nil) @@ -438,7 +467,8 @@ Let's start with an umbrella customization group: (delete-region (point-min) (point-max)) (insert (propertize name 'face 'bold)) (newline) - (when espotify--current-item (insert (pp-to-string espotify--current-item))) + (when espotify--current-item + (insert (pp-to-string espotify--current-item))) (newline) (goto-char (point-min)) (read-only-mode 1)) @@ -453,31 +483,26 @@ Let's start with an umbrella customization group: (espotify-play-uri (alist-get 'uri album)) (error "No album for %s" (alist-get 'nmae espotify--current-item)))) - (with-eval-after-load "embark" - (embark-define-keymap espotify-item-keymap - "Actions for Spotify search results" - ("a" espotify--play-album) - ("h" espotify--show-info)) + (embark-define-keymap espotify-item-keymap + "Actions for Spotify search results" + ("a" espotify--play-album) + ("h" espotify--show-info)) - (defun espotify--annotate-item (cand) - (setq espotify--current-item (espotify--item cand)) - (cons 'espotify-search-item cand)) + (defun espotify--annotate-item (cand) + (setq espotify--current-item (espotify--item cand)) + (cons 'espotify-search-item cand)) - (add-to-list 'embark-transformer-alist - '(espotify-search-item . espotify--annotate-item)) + (add-to-list 'embark-transformer-alist + '(espotify-search-item . espotify--annotate-item)) - (add-to-list 'embark-keymap-alist - '(espotify-search-item . espotify-item-keymap))) + (add-to-list 'embark-keymap-alist + '(espotify-search-item . espotify-item-keymap)) #+end_src - -* Post-amble :noexport: - #+begin_src emacs-lisp - (provide 'espotify) + (provide 'espotify-embark) #+end_src - * Footnotes [fn:1] This is an elegant strategy i first learnt about in SICP, many, -- cgit v1.2.3