Emacs rec mode, an all-text database system
摘要
rec-mode 通过纯文本文件实现数据库功能,记录间以空行分隔,且支持非固定结构的灵活字段定义。作者分享了使用其追踪图书信息的案例,强调了其相对于图形化工具的键盘操作优势,并展示了如何通过 Org-mode 代码块调用 recutils 生成表格。此外,文中还提及了支持多种版本控制系统的内置 vc-mode。
荐读理由
在独立开发的小型项目中,你可以借鉴 rec-mode 这种纯文本数据库架构来管理结构化数据,利用其天然支持版本控制且易于手动修复的特性,实现低成本、高透明度的数据持久化。
原文
Emacs carnival: rec mode, an all-text database system
This month's Emacs carnival is on underappreciated built-in features in Emacs.
rec mode
One of the key features of Emacs is that everything is just text.1 An example of this is rec mode.2 Rec mode implements a simple yet flexible database system using just text files. Accompanying rec mode, the Emacs code, is the package that includes the tools for manipulating files containing rec databases. On Debian, the package is recutils.
Entries in a rec file are defined by fields and their content. A single file will contain all the records in your database. Each record is separated from other records by a blank line. Beyond this, there is no imposed structure. For instance, each record may have different fields defined.
A small example
I use rec mode to keep track of my books. Although I do use calibre to manage my ebooks, in particular, I do not like interacting with calibre as it is mouse based.3 I have a separate rec database which tracks which books I have acquired, am reading, have finished, or wish to buy. A couple of typical entries look like this:
title: Safe Enough
author: Lee Child
status: finished
Date: 2024-08-29
# collection of short stories, none about Reacher
title: All systems red
author: Martha Wells
series: murderbot
number: 1
status: finished
Date: 2017-05-02
notes: library
Note that these two entries, although both referring to books, have different fields used.
As I'm writing about Emacs, the obvious question is: does org mode support rec files? The answer, as with all things Emacs, is "of course it does". With a src block:
#+begin_src rec :data my_books.rec
author = 'Martha Wells'
#+end_src
the outcome on evaluating is the following table:
| title | author | series | number | status | Date | notes |
|---|---|---|---|---|---|---|
| All systems red | Martha Wells | murderbot | 1 | finished | 2017-05-02 | library |
| Artificial condition | Martha Wells | murderbot | 2 | finished | 2018-05-08 | library |
| Rogue protocol | Martha Wells | murderbot | 3 | finished | 2018-08-07 | library |
| Exit strategy | Martha Wells | murderbot | 4 | finished | 2018-10-02 | library |
| Home | Martha Wells | murderbot | 4.x | finished | 2021-10-01 | library, short story |
| Fugitive telemetry | Martha Wells | murderbot | 4.5 | finished | 2018-10-02 | library |
| Network effect | Martha Wells | murderbot | 5 | finished | 2020-05-02 | library |
| System collapse | Martha Wells | murderbot | 6 | finished | 2026-05-06 | library |
The best thing about rec mode is, to repeat myself, that it's all text. There is no danger of corrupting a database or, at least, if somehow the database becomes corrupt, it should be easy to fix (using Emacs, of course). Being all text means that version control is trivially supported, which brings me to the final comments below.
Honorable mention
When writing this blog post, I spent some time thinking about which one feature was most underappreciated and I decided on rec mode, as you have read above. However, closely behind was vc mode. Many people talk about some of the killer features of Emacs, mentioning org mode and magit. These are both excellent packages and I use them both myself. However, I only use magit every now and again, relying on vc mode instead for day to day version control. Almost everything I need to do in terms of version control is supported by vc mode and, more importantly for me, vc mode is agnostic with respect to the actual version control system used. I do use git but I also use mercurial and src. The latter is particularly nice for single file projects.
Blog navigation
| Previous post | Blog | Next post |
|---|---|---|
| Emacs carnival: Mistakes and misconceptions | Index | None (yet!) |
You can find me on Mastodon or you can email me should you wish to comment on this entry or the whole blog.
References
Footnotes:
1
Well, actually, it's more than that: everything is a buffer that contains text.
2
This post is a bit of a cheat as rec mode is not built in to Emacs but it does reside in ELPA, has been around for at least 15 years, and has always been intended for use in Emacs (although it can also be used in vim).
3
There are Emacs packages for working with calibre, such as calibre.el
这条对你有帮助吗?