2018-12-16, updated: 2024-03-12

Tested with Nyxt 1.3.

Tags: programming, lisp, emacs.

Emacs Hacks

Emacs Hacks

By Pierre Neidhardt

Nyxt interfaces

Nyxt can be controlled from an external program via SWANK. For instance, Emacs with SLIME makes it possible to hack Nyxt while it's running.

But what about the other way around? Can we use Nyxt to manipulate other programs? Sure we can! Nyxt is a full-blown Common Lisp program that can leverage all of Common Lisp power to interact with foreign programs via Unix sockets, XML-RPC, you name it.

An interesting example is that of Emacs, since it can be hacked live with Elisp, a language very similar to Common Lisp. In the following article, Nyxt will generate Elisp code to hack Emacs live!

Youtube-dl with Emacs

Youtube-dl is a great program for downloading audio and video files from a ridiculous number of websites.

That said, we would like to make it more convenient than constantly copy-pasting to a shell. Besides, naively calling youtube-dl from Nyxt is not so convenient since we lack feedback or interactivity with the running subprocess.

We could derive our own Youtube-dl extension for Nyxt so that we would have a buffer showing progress of multiple downloads and allowing for interactivity. However, until someone works on the extension, let's see what others have been doing so far.

Chris Wellon wrote a great Emacs extension, which sadly, only works for Youtube. This won't stop us! Here is our poor man's universal interface to Youtube-dl to supplement the aforementioned extension:

The above snippet opens a dedicated *youtube-dl* Eshell buffer so that we can track download progress from there, as well as stack multiple video downloads.

With that set up, now we can add the following snippet to our nyxt/init.lisp:

We define a helper function eval-in-emacs which sends a bunch of formatted s-expressions to Emacs. This requires the Emacs daemon, either by starting Emacs with emacs --daemon or by running M-x server-start from an Emacs instance.

The youtube-dl-current-page command tests whether the current URL is Youtube, in which case we use Chris Wellons' youtube-dl extension, or else we rely on the Eshell version we wrote in our Emacs config.

Org-mode and Org-capture

Org-mode is a great extension for Emacs for note taking (and so much more). It has a feature called "org-capture" which, on a key press, will store some contextual information to your agenda, so that later Org will remind you to refer to it again.

This can be useful for a web browser: You'd like to mark this page and let Org remind you to read it later? Nothing easier! And off we go to add the following snippet to our Emacs init file:

The above snippet does quite a few things, so let's analyze it carefully:

See org-capture-templates documentation for more details.

Now to our nyxt/init.lisp:

This is similar to the example in the previous section. Note that we are passing multiple s-expressions to Emacs, they will all be wrapped into a single (progn ...) and Emacs will evaluate everything in one go.

Happy hacking!


Did you enjoy this article? Register for our newsletter to receive the latest hacker news from the world of Lisp and browsers!