Tuesday, August 30, 2011

backups-mode for emacs

John Siracusa's excellent Ars Technica review of Mac OS X Lion includes a page on Apple's new document model API. The intent of this API is to effectively end the practice of manually saving a document. Instead, applications will auto-save your documents in the manner of Google Docs or iOS applications. There are many scenarios where this will be helpful. John describes a few.

The student who writes for an hour without saving and loses everything when the application crashes.

The businessman who accidentally saves over the "good" version of a document, then takes it upon himself to independently reinvent version control—poorly—by compulsively saving each new revision of every document under slightly different names.

The Mac power user who reflexively selects the "Don't Save" button for one document after another when quitting an application with many open windows, only to accidentally lose the one document that actually had important changes.

The father who swears he saved the important document, but can't, for the life of him, remember where it is or what he called it.

Apple will now enable the following experience as written by John.

The user does not have to remember to save documents. All work is automatically saved.

Closing a document or quitting an application does not require the user to make decisions about unsaved changes.

The user does not have to remember to save document changes before causing the document's file to be read by another application (e.g., attaching an open document with unsaved changes to an e-mail).

Quitting an application, logging out, or restarting the computer does not mean that all open documents and windows have to be manually re-opened next time.

I will add that file versioning is another aspect of Apple's API.

The user can explicitly choose to save a version of the document.

Old versions can be found and viewed.

Old versions can be reverted to. This saves the current file as a version and switches the chosen backup with the current file.

With backups-mode I've set out to approximate Apple's Document Model idiom in emacs. Here's how I accomplish it.

emacs already has an auto-save feature that is turned on by default. Therefore, if emacs crashes, a user can revert from this file that is saved periodically.

I've redefined kill-buffer and save-buffers-kill-emacs to automatically save any file-based buffer when closing a file or quiting emacs.

I've turned on emacs' version-control and am saving old versions of a file to a central location instead of said file's location.

The user can save a version of the file they are editing with the save-version command bound to \C-cv.

The user can list all versions with the list-backups command bound to \C-cb.

After listing old versions via list-backups, the user is taken to a special backups-mode buffer where they can:

View an old version in read-only mode

Diff two versions

Revert from an old version

backups-mode buffer

Installation, configuration, and usage documentation can be found on github.