Emacs rec mode, an all-text database system
摘要
Emacs carnival 专题介绍 rec 模式作为 Emacs 内置特色。它实现基于纯文本文件的简单灵活数据库系统,文件仅由字段与内容定义,每条记录以空行分隔,可自由组合字段。一份文件含全部记录,无固定结构。作者用 rec 模式跟踪书籍(与 calibre 电子书管理分离),示例条目包含不同字段如 title、author、series、number、status、Date、notes 等。rec 模式与 Org Mode 完全兼容:在 src 块中嵌入 rec 文件引用即可生成表格。优点在于一切为纯文本,无数据库损坏风险,版本控制轻松支持。文章还提及 VC 模式作为 Magit 的日常替代,支持 Git、Mercurial 和 Src 等系统。
荐读理由
用 rec mode 建立纯文本数据库,记录书籍状态字段(如 title author series number status Date notes),无鼠标操作且因‘everything is just text’支持 Git 等版本控制,无损修复风险
原文
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:
Well, actually, it's more than that: everything is a buffer that contains text.
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).
There are Emacs packages for working with calibre, such as calibre.el
这条对你有帮助吗?