Once upon a time, a guy was trying to start his study session right away, so, without further hesitations, he put on his headset and started YouTube to play some good Lo-fi music. But something didn't go well and the same guy got lost on the web for over an hour. Probably, bright minds already imagined it, but that guy was me, and that's the story of how I managed to avoid that something similar could happen to me ever again.

The first solution was absolutely simple and effective:

# ~/.bashrc or ~/.bash_aliases (if configured)
alias lofi="python -mwebbrowser https://youtube.com/watch?v=8nXqcugV2Y4"

Proudly, I was sipping tea at ~ 3:00 AM.

via GIPHY

Of course, the previous one was a pretty limited solution: the alias opens the same link over and over: you cannot feel the thrill of discovery. I said to myself that I needed to add some chaos to the logic, maybe a list of links to a bunch of YT radios.

We have a lot of ways to achieve this result. I will cite the first two that bumps in my head:

  • The Python way: a little script in Python which could hold both the logic and the list of links;
  • The Bash way: maintaining a dotfile like .lofi-links from which a bash function could peak a different line every time.

The second option seemed the cleanest to be, because it keeps on different files the data and the logic, but I don't love scripting in bash to be honest, because I feel it's poorly readable and bash functions are always more complex than you should say at first. Of course, complexity is not "bad", no one would say that, but (of course) it depends on the situation, and I didn't want extra-complexity for a simple task: quite the opposite, I wanted a simple and easily maintainable script and setting it fastly in every machine I have and will have in the years. I could easily find myself making changes or extensions of some kind, so I don't want to read the docs of every function every single time.

In the end, my mind took the radical path: why not Emacs Lisp?

The Emacs way§

This paragraph aims to teach some basic lisp trick to other lisp beginners. Let's get into action, shall we?

First of all, we set a variable with a list of our links of choice. This is mine:

(setq links '("https://www.youtube.com/watch?v=8nXqcugV2Y4" 
                "https://www.youtube.com/watch?v=FVue6P2VoTc"
                "https://www.youtube.com/watch?v=kgx4WGK0oNU"
                "https://www.youtube.com/watch?v=5qap5aO4i9A"))

Then, we should find a way to randomize the choice of the link by shuffling the list, just like with a card deck:

;; Implementation of the knuth shuffle
(defun nshuffle (sequence)
  (cl-loop for i from (length sequence) downto 2
        do (cl-rotatef (elt sequence (random i))
                    (elt sequence (1- i))))
  sequence)

This is the source of the function, I just edited it in order to express explicitly the use of Common Lisp functions.

In the end, we write the core function, which works launching a command on the shell, like in the first deadly simple solution. This time, the link passed to the command is the first one of the shuffled list of links we made with the previous function.

(defun play-lofi ()
  "Play random lofi music on your browser"
  (interactive)
  (shell-command (concat "python -mwebbrowser " (car (nshuffle links)))))

In order to evoke this function from within the editor, we defined it as interactive in the second line. This means we can simply Alt+x play-lofi to run our music. We could even add a shortcut at this point and remove the need to call python from the shell, but I have not so much time right now, so I will keep it like this for the moment: it's really unlikely that I will ever use a machine without Python pre-installed in the future and so do you, I guess.

Wait, but are we forced to launch the command from an Emacs instance every time?

If you're one of those weirdos who want to leave Emacs and this looks like a downside for you, I stop you right there.

Back to the CLI§

Considering that Emacs is always alive in the Daemon mode and I limit myself to spawning clients wherever needed, this means that we can call function from terminal too without opening any window.

Practically, we can solve this by a simple reformulation of the alias:

# ~/.bashrc or ~/.bash_aliases (if configured)
alias lofi="emacsclient -e \(play-lofi\)"

In order to play your music from the terminal emulator, just like we did initially, it's sufficient to launch this command.

lofi

Now, please look at your mighty browser running the perfect Lo-fi music or whatever you put in there and enjoy.


Head Artwork by jisung_clouds -> https://www.wattpad.com/...