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 | (require 'widget) |
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 | (defvar sqlite-mode-hook nil |
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 | (defvar sqlite-mode-map nil |
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 | (load "c:/temp/lisp-code/sqlite/add-antibody") |
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.