Using Emacs as system-wide Rhythmbox interface
18 Jan 2016In an earlier post, I described how I've been managing Rhythmbox from Emacs. I've bound the entry point to C-S-o:
(global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
Obviously, this entry point won't work while outside Emacs. Today, I'll describe how I've made it work everywhere. Everywhere on Ubuntu 14.04, that is, although a similar approach should work for other distributions.
Step 1: Make sure the Emacs server is running
Here's the relevant part of my init.el:
(require 'server)
(or (server-running-p) (server-start))
Using emacsclient
is essential to avoiding the extra startup time:
even a startup time of one second feels sluggish when all I need is to
open a menu with a song playlist.
Step 2: Install the relevant X window tool
Initially, I only wrote a call to emacsclient
, which resulted in the
Emacs window gaining focus in the end. Then I thought it would be
nice to give the focus back the original window after the end of
selection, and raise it as well.
I wanted to do something with wmctrl
, but I found that
xdotool
can do what I want in a simple way.
sudo apt-get install xdotool
Step 3: Write a shell script
#!/bin/bash
wnd_id="$(xdotool getwindowfocus)"
emacsclient --eval "(progn (x-focus-frame nil) (counsel-rhythmbox))"
xdotool windowfocus $wnd_id
xdotool windowraise $wnd_id
Here, (x-focus-frame nil)
will raise the Emacs window and give the
keyboard input focus. emacsclient
will return as soon as I select
something or press C-g. At that point the keyboard focus
will be returned to whatever window had it when the script was
invoked.
By the way, here's a cool configuration that automatically makes a
file executable if it starts with #!
.
(add-hook
'after-save-hook
'executable-make-buffer-file-executable-if-script-p)
Step 4: Bind the shell script to a key
Open this (possibly using gnome-control-center
instead, if applicable):
unity-control-center keyboard
And add a new shortcut in Shortcuts
/Custom Shortcuts
. I've bound
that one to C-S-o as well.
The final result
It's pretty convenient: as I'm scrolling something I'm reading in Firefox with j (via Firemacs), I can seamlessly press C-S-o moo RET to play "Sisters of the Moon", and continue scrolling the web page with j.
What's more, Emacs has very nice support for input methods with
C-\ (toggle-input-method
), so I can also quickly select
Ukrainian-titled songs, while still keeping shortcuts like
C-n and C-m (without having to switch the
input method back).
The whole experience is similar to gnome-do
/synapse
, which I was
using a few years back, except better because now it's in Emacs.