All Manuals > LispWorks Editor User Guide > 6 Advanced Features > 6.3 Programming the editor

NextPrevUpTopContentsIndex

6.3.2 Defining commands

editor:defcommand

Macro

defcommand name lambda-list command-doc function-doc &body forms => command-function

Defines a new editor command. name is a usually string naming the new editor command which can invoked in the editor via Extended Command, and command-function is a symbol naming the new command function which can be called programmatically. The command-function symbol is interned in the current package.

lambda-list is the lambda list of the new command, which must have at least one argument which is usually denoted p , the prefix argument.

command-doc and function-doc should be strings giving detailed and brief descriptions of the new command respectively.

forms is the Lisp code for the command.

The name of the command must be a string, while the name of the associated command function must be a symbol. There are two ways in which name can be supplied. Most simply, name is given as a string, and the string is taken to be the name of the editor command. The symbol naming the command function is computed from that string: spaces are replaced with hyphens and alphabetic characters are uppercased, but otherwise the symbol name contains the same characters as the string with -COMMAND appended.

If a specific function name, different to the one defcommand derives itself, is required, then this can be supplied explicitly. To do this, name should be a list: its first element is the string used as the name of the command, while its second and last element is the symbol used to name the Lisp command function.

For example the following code defines an editor command, Move Five , which moves the cursor forward in an editor buffer by five characters.

(editor:defcommand "Move Five" (p)
   "Moves the current point forward five characters.
    Any prefix argument is ignored."      
   "Moves five characters forward."
   (editor:forward-character-command 5))
=>
MOVE-FIVE-COMMAND

The prefix argument p is not used, and is there simply because the lambda-list must have at least one element.

Use Alt+X Move Five to invoke the command.

As another example this command changes all the text in a writable buffer to be uppercase:

(editor:defcommand "Uppercase Buffer" (p) 
     "Uppercase the buffer contents" ""
  (declare (ignore p))
  (let* ((buffer (editor:current-buffer))
         (point (editor:buffer-point buffer))
         (start (editor:buffers-start buffer))
         (end (editor:buffers-end buffer)))
    (editor:set-current-mark start)
    (editor:move-point point end)
    (editor:uppercase-region-command nil)))

Having defined your new command, you can invoke it immediately by Alt+X Uppercase Buffer .

You could also call it programmatically:

(uppercase-buffer-command nil)

If you anticipate frequent interactive use of Uppercase Buffer you will want to bind it to a key. You can do this interactively for the current session using Bind Key. Also you can put something like this in your initialization file to establish the key binding for each new session:

(editor:bind-key "Uppercase Buffer" #("Control-x" "Meta-u"))

Then, entering Ctrl+X Alt+U will invoke the command.


LispWorks Editor User Guide (Windows version) - 3 May 2011

NextPrevUpTopContentsIndex