EMACS Sqlite Mode

For a good discussion of the details of creating a major mode refer to Writing GNU EMACS Extensions by Bob Glickstein. The advantage of utilizing SQLITE within a major mode is that while you are in the SQLITE specific buffer, you can have SQLITE specific menu items and key chords that are in effect only while in that buffer. A mode also allows you a single point in your code to open and close the database and deal with any database housekeeping that is required. Our effort to abstract away the details of database interaction are also facilitated by a mode, as any database specific functions can be included in the mode. The commands for creating the mode along with helper functions and load statements can go into a file called sqlite-mode.el. My file also includes code for working with the widget library:

1
2
3
4
(require 'widget)
(require 'derived)
(eval-when-compile
(require 'wid-edit))

Define a hook variable and menu map. Add elements to the menu map. Note that each menu item is defined by a dotted pair, the car being the text in the menu and the cdr being an associated function. Often that function will launch a form to be used to enter data. A single example of form design is given below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(defvar sqlite-mode-hook nil
"*List of functions to call when entering sqlite mode*")

(defvar sqlite-menu-map nil
"Menu for sqlite mode.")


(if sqlite-menu-map
nil
(setq sqlite-menu-map (make-sparse-keymap "sqlite"))
(define-key sqlite-menu-map [sqlite-reports]
'("Sqlite reports" . sqlite-reports))
(define-key sqlite-menu-map [add-antibody]
'("Add Antibody" . add-antibody))
(define-key sqlite-menu-map [add-gene]
'("Add Gene" . add-gene))
(define-key sqlite-menu-map [add-abresult]
'("Add Antibody Results" . add-abresult)))

(define-key sqlite-mode-map [menu-bar sqlite]
(cons "sqlite" sqlite-menu-map))

Define keybindings specific for SQLITE mode. I don’t define any unique SQLITE specific bindings because I need to use the widget keymap for my forms to be functional, so I copy-keymap the widget keymap.

1
2
3
4
5
6
7
8
9
10
11
(defvar sqlite-mode-map nil
"Keybindings for sqlite mode")

(if sqlite-mode-map
nil
(setq sqlite-mode-map (copy-keymap widget-keymap)))

(define-derived-mode sqlite-mode text-mode "sqlite"
"Major mode using sqlite for tau antibodies.
Special commands:
\\{sqlite-mode-map}")

Load any libraries that need to be accessed during database manipulation. Here I load various forms and the report interface. provide allows me to require in my .emacs file.

1
2
3
4
5
6
(load "c:/temp/lisp-code/sqlite/add-antibody")
(load "c:/temp/lisp-code/sqlite/add-gene")
(load "c:/temp/lisp-code/sqlite/add-abresult")
(load "c:/temp/lisp-code/sqlite/tau-reports")

(provide 'sqlite)

Also in this file I would include any helper functions, such as sqlite-query and chomp described in an earlier post. Once the mode is defined, the command (sqlite-mode) can be used to invoke the mode, for example in a form buffer.

Share