
Using EMACS from LISP
By Richard Weyhrauch
This note is called:
[CALLING-EMACS]
The notations used in this note is described in
[NOTATION]
Under development
Contents:
Preface
This note documents how IBUKI uses elisp in EMACS and calls it from LISP.
EMACS is perhaps the world's most efficient and featureful programmible
editor. By creating and using this EMACS 'API' we avail ourselves of an
expremely powerful tool for text processing. We use it all the time. This
note was written as documantation for IBUKI's internal use so the referenced
files are found on IBUKI servers but (mod directory names) this code will
work anywhere.
Setting up a call to EMACS
First we choose a <name> for the EMACS 'function' and then
follow thefollowing template:
- use the name of the EMACS routine in the ~/.emacs file
- make a file called <name>.scr to call from LISP
- make a file called <name>.fun to implement the operation in EMACS
- make a file called <name>.emx to hold auxiliary elisp definitions
Use the name of the EMACS routine in the ~/.emacs file
Add the following lines to the file ~/.emacs
------------------------------------------------------------------------------
(print "Before <name>")
(if (equal (getenv "RWWOP") "<name>")
(load-file "<name>.fun")
)
------------------------------------------------------------------------------
This code determines if you want to call this routine and loads it when
wanted, i.e., it essentially calls the routine <name> and executes it.
Make a <name>.scr file to call from LISP
This is a shell script that can be called either from the UNIX shell or
from LISP using the 'command' function'.
------------------------------------------------------------------------------
#!/usr/bin/csh
setenv RWWOP "<name>"
foreach file ($1)
setenv RWWEDIT $file
emacs -batch
end
------------------------------------------------------------------------------
This script depends on both the shell calling it and the version of EMACS
you are using but the essencials are the same. This script runs in the UNIX
shell 'csh'.
Make a file '<name>.fun' to implement the operation in EMACS
This file contains elisp code and is called <name>.fun by
convention. The first line in the file allows it to be executed as a stand
alone script file.
------------------------------------------------------------------------------
#!/usr/bin/emacs --script
; these functions destructively edit a file using emacs
; load the needed emacs macro files
; for convience *example-dir* is defined in ~/.emacs
;
(load-file (concat *example-dir* "<name>.emx"))
; get the file to edit
; RWWEDIT is set in <name>.scr
;
(find-file (getenv "RWWEDIT"))
; execute a series of emacs list commamds
;; some spellaing fixes (defined in %lt;name>.emx)
(beginning-of-buffer)
(execute-kbd-macro 'fix-sissor)
(beginning-of-buffer)
(execute-kbd-macro 'fix-sizzer)
; accounts for the failure of search-forward in the macro
; the macros are defined in %lt;name>.emx
(condition-case nil
(progn
(execute-kbd-macro 'script-all)
)
(error nil)
)
; this is one way of programatically
; saving the current buffer AND
; exiting EMACS
; save the edited buffer (loaded above)
(basic-save-buffer)
; exit EMACS (return to the <name>.scr file)
(kill-emacs)
------------------------------------------------------------------------------
Make a file called '<name>.emx' to hold auxiliary elisp definitions
This file contains elisp code. It is optional but IBUKI uses it because
it makes code sharing between EMACS calls convient.
------------------------------------------------------------------------------
;; 'safe' spelling correction
;; these examples, of course, just replaced a mispelled word
;; but the idea should be clear.
;; There are other elisp commands that would do the same job BUT
;; this illustrates making a keyboard macro callable from LISP
;; IBUKI uses this to do complex format changes by making a keyboard macro
;; eg UTF-8 to printable ASCII - its much simpler than writing the LISP code
;;
; sissor ==> scissor
(fset 'fix-sissor
"\C-[<\C-[xreplace-string\C-msissor\C-msizzor\C-m\C-[<")
; sizzor ==> scissor
(fset 'fix-sizzer
"\C-[<\C-[xreplace-string\C-msizzer\C-msissor\C-m\C-[<")
; remove one <script>s from an HTML file
(fset 'script
"\C-s |