index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

07 August 2023 Extending sqlite-mode (cell navigation + edits)

I recently wrote about Emacs 29's new sqlite-mode, which enables you to browse sqlite databases from your beloved editor.

Out of the box, it supports the following browsing features:

  • sqlite-mode-list-data: List the data from the table under point.
  • sqlite-mode-list-column: List the columns of the table under point.
  • sqlite-mode-list-tables: Re-list the tables from the currently selected database.

On the editing side of things it supports row deletion:

  • sqlite-mode-delete: Delete the row under point.

While fairly spartan, it lays foundations for additional tools and features.

Two features I would like to have:

  1. TAB navigation across table rows and columns.
  2. Updating the row's field at point.

This would give me the familiar behaviour I'm used to in my org tables as well as other common spreadsheet tools.

Luckily, this is Emacs, so we can bend it our way… and I sure did!

Here's tab navigating forward:

sqlite-forward.gif

Here's tab navigating backward:

sqlite-previous.gif

And updating row fields:

sqlite-edits.gif

Most of the navigation is achieved by querying the current buffer to figure out column positions. Editing was in some ways easier, as I looked at sqlite-mode-delete to figure out how it handled the query.

To get the more familiar navigation behaviour, I've adjusted my key bindings as follows:

(use-package sqlite-mode-extras
  :bind (:map
         sqlite-mode-map
         ("n" . next-line)
         ("p" . previous-line)
         ("<backtab>" . sqlite-mode-extras-backtab-dwim)
         ("<tab>" . sqlite-mode-extras-tab-dwim)
         ("RET" . sqlite-mode-extras-ret-dwim)))

The code for sqlite-mode-extras-tab-dwim, sqlite-mode-extras-backtab-dwim, and sqlite-mode-extras-ret-dwim is little rough still (hacky even), but hey still fun.

For now, the code lives in sqlite-mode-extras.el under my Emacs config repo. Improvements/fixes totally welcome!