Like many of us Emacsers, I do much (if not most) my computering in Emacs. This includes using EMMS as my main media player and Dired as my file manager.
One thing I find myself doing pretty often is adding a bunch of subdirectories in my ~/music
directory to my EMMS playlist. I usually used emms-add-directory-tree
, but it is not really smooth if I want to apply it to many subdirectories. I thought, “there must be a function which adds the current item – or the marked items – to the playlist”. Lo and behold, I found emms-add-dired
, which does exactly that.
Now the issue I have with this command is that it is not bound to any key in Dired mode. That’s fair enough – not everyone uses EMMS – but I’d really prefer to have it on some key. The question is, what key do I bind it to? I don’t want to use RET
(and lose dired-open-file
) – I still want to be able to enter these directories (they are sometimes nested, so I may want first to get into a directory and then emms-add-dired
on some of its subdirectories, for example).
One key binding which seems quite natural is E
, normally bound to dired-do-open
. By default, it opens the marked files in an external program. While EMMS is hardly an “external program”, my mind associates E
with things like “play” (that’s what it does with video files), so I’m going to (slightly) abuse the semantic of dired-do-open
and make it add my music directories to the EMMS playlist.
The trouble is, however, that dired-do-open
does not support anything like this – it basically opens the marked file(s) with xdg-open
(at least on GNU/Linux). But this is Emacs, so I can do whatever I want (and whatever works for me)! I could even rebind E
to something completely else. Fortunately, I don’t even have to – I can just advise dired-do-open
.
(defcustom dired-emms-music-directory "~/music" "The path to the directory with music.") (defun dired-do-add-to-emms-playlist (dired-do-open &rest args) "Add FILES to the EMMS playlist if suitable." (if (file-in-directory-p default-directory dired-emms-music-directory) (emms-add-dired) (apply dired-do-open args))) (advice-add 'dired-do-open :around #'dired-do-add-to-emms-playlist)
This code is a bit simplistic – the way it knows whether to call emms-add-dired
or dired-do-open
is by checking if the current directory is a subdirectory of dired-emms-music-directory
, so it can break in a Dired buffer with several directories. Still, calling it with both music directories and some other type of files marked is a rather contrived scenario, so I don’t really care that much.
Of course, there is another way of achieving my goal – creating a shell script calling emacsclient
with a snippet of Elisp adding its argument to the playlist, and registering it with xdg-mime
. That could work, but it’s faster (and also much more fun) to just use Elisp!
That’s it for today, happy listening to music!