Script Plugin for DrRacket
1 Introduction
The Script Plugin is a plugin for DrRacket that is similar to GEdit’s External Tools plugin.
Its purpose is to make it easy to extend DrRacket with small scripts that can be used in the definition window, on the selected text.
2 Example
Click on Scripts/New Script, and enter Reverse. This creates and opens the files reverse.rkt and reverse.rktd in the script directory. Also, a new item automatically appears in the Scripts menu.
(define (transform-input-string str) (list->string (reverse (string->list str))))
Then go to a new tab, type some text, select it, and click on Scripts/Reverse.
3 Description
This DrRacket plugin adds a Script menu to the main window. This menu has several items, followed by the (initially empty) list of active scripts.
a .rkt file, the script itself (with a sample program)
a .rktd file, the metadata of the script with the default values
The script menu is rebuilt each time the user activates it, so that changes are taken into account as soon as possible.
The default location of the scripts is in a sub-folder of (find-system-path 'home-path). The directory of the user scripts can be change through DrRacket’s preferences (in Edit/Preferences/Scripts).
3.1 The .rkt file
This is the script file. It must provide the transform-input-string (that’s not a very good name) function, as in the sample code. It is meant to be executable by itself, to ease the testing process.
(transform-input-string str) → string? str : string?
The name of the function can be changed, but this requires to change it also in the functions entry of the .rktd file, and the function must be provided.
3.2 The .rktd file
This is the metadata file. It contains an association list that defines the configuration of the script.
functions : (or symbol? (list-of (list symbol? string?))) = transform-input-string
If a symbol, the name of the function to call (which must be provided), and must follow transform-input-string’s signature.
If a list, each symbol is the name of a function, and each string is a label for that function. In this case, a sub-menu holding all these functions is created, and the label option is used as the parent menu name.
Note that a sub-menu can be shared among scripts.
output-to : (one-of selection new-tab message-box) = selection
If selection, the output of the transform function replaces the selection in the current tab (or insert at the cursor if there is no selection). If new-tab, a new tab is created and the output of the script is written to it. If message-box, the output is displayed in a message-box.
active : boolean? = #t
If set to #f, no menu item is generated for this dictionnary.
Finally, one .rktd file can contain several such dictionaries (one after the other), which allows for multiple sub-menus and menu items and in a single script. This would have roughly the same effect as splitting such a script into several script, each one with its own .rktd file and its dictionary.
4 Usage ideas
Snippets to add a require line for each planet package you usually use. Modify the .rktd file to put them all in a "PLaneT" subsection.
ASCII frames and styling (upper-case, camel-case, etc.) for titles, sections, etc.
Code snippets scripts, with keyboard shortcuts.
Automatic comments, or even contracts from signatures and conventions
...