#lang scribble/manual
@(require planet/scribble
(for-label racket/gui))
@title{Script Plugin for DrRacket}
@section{Introduction}
The Script Plugin's purpose is to make it easy to extend DrRacket with small Racket scripts that can be used in the definition window.
Selected text can be transformed or text can be inserted by a simple click on a menu item, and there is direct access to some elements of DrRacket for advanced (though simplified since there is no need to create a dedicated plugin) scripting, like the frame and the current text editor.
Each script is automatically added as an item to the @italic{Script} menu, without needing to restart DrRacket.
A keyboard shortcut can be assigned to a script (via the menu item).
It is similar to GEdit's External Tools plugin (for those who use it), but has more potential for interacting with the text editor.
@section{Example}
Click on the @italic{Scripts/New Script} menu item, and enter @italic{Reverse} for the script name.
This creates and opens the files reverse.rkt and reverse.rktd in the script directory.
Also, a new item automatically appears in the @italic{Scripts} menu.
In the .rkt file, modify the @racket[modify-selection] function to the following:
@(racketblock
(define (modify-selection str)
(list->string (reverse (string->list str))))
)
Then go to a new tab, type some text, select it, and click on @italic{Scripts/Reverse}.
@section{Description}
This DrRacket plugin adds a @italic{Script} menu to the main window.
This menu has several items, followed by the (initially empty) list of active
scripts.
The New Script item asks for a script name and creates 2 files:
@itemlist[
@item{a .rkt file, the script itself (with a sample program)}
@item{a .rktd file, the metadata of the script with the default values}
]
These two files are automatically opened.
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
@racket[(find-system-path 'home-path)].
The directory of the user scripts can be change through DrRacket's preferences
(in @italic{Edit/Preferences/Scripts}).
@bold{Important:} The scripts directory must have write-access for the user.
@subsection{The .rkt file}
This is the script file.
It must provide the @racket[modify-selection] function,
as in the sample code.
It is meant to be executable by itself, to ease the testing process.
@defproc[(modify-selection [str string?]) (or/c string? false?)]{
Returns the string meant to be inserted in place of the current selection,
or at the cursor if there is no selection.
If the returned value is @racket[#f], the selection is not modified (i.e., the file remains in a saved state if it was already saved).
}
This function also accepts (optional or mandatory) special keyword arguments
(the exact signature is determined with @racket[procedure-keywords]):
@itemlist[
@item{@racket[#:file : (or/c path? #f)]
The path to the current file of the definition window, or @racket[#f]
if there is no such file (i.e., unsaved editor).
For example:
@(racketblock
(define (modify-selection str #:file f)
(string-append "(in " (if f (path->string f) "no-file") ": " str))
)
See also: @racket[file-name-from-path], @racket[filename-extension], @racket[path->string], @racket[split-path].
}
@item{@racket[#:editor : text%]
The @racket[text%] editor of the current definition window.
For advanced scripting.}
@item{@racket[#:frame : drracket:unit:frame]
DrRacket's frame.
For advanced scripting.
For example:
@(racketblock
(define (modify-selection str #:frame fr)
(send fr create-new-tab)
#f)
)
}
]
The name of the function can also be changed,
but this requires to change it also in the @racket[functions]
entry of the .rktd file, and the function must be @racket[provide]d.
@subsection{The .rktd file}
This is the metadata file.
It contains an association list that defines the configuration of the script.
Most options (@racket[label], @racket[shortcut], @racket[shortcut-prefix], @racket[help-string]) are the same as
for the @racket[menu-item%] constructor.
In particular, a keyboard shortcut can be assigned to an item.
There are some additional options:
@itemlist[
@item{@racket[functions : (or/c symbol? (listof (list/c symbol? string?))) = modify-selection]
If a symbol, the name of the function to call (which must be provided),
and must follow @racket[modify-selection]'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 @racket[label] option is used as the parent menu name.
Note that a sub-menu can be shared among scripts.}
@item{@racket[output-to : (one-of/c 'selection 'new-tab 'message-box) = 'selection]
If @racket['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 @racket['new-tab], a new tab is created and the output of the script is written to it.
If @racket['message-box], the output is displayed in a message-box.
Note: The quote must not be included in the .rktd file.
}
@item{@racket[active : boolean? = #t]
If set to @racket[#f], no menu item is generated for this dictionary.}
]
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.
@section{Usage ideas}
@itemlist[
@item{ASCII frames and styling (upper-case, camel-case, etc.) for comment titles, sections, etc.}
@item{Code snippets scripts, with keyboard shortcuts, e.g., adding a @racket[require] line for each planet package you usually use, or a license header.}
@item{Module template.}
@item{Automatic comments, or even generic contracts from signatures and conventions.}
@item{Automatic reformatting and custom indentation.}
@item{Count the number of words/lines/characters/whatever and display it in a message-box.}
@item{Open a new tab with a template code.}
@item{...}
]
Note that @racket[racket/gui] can be used to ask the user for more information and more.
Remark: Code snippets should probably be of rare usage, as one should better take advantage of Racket's wonderful macro system. In some cases however, snippets might be useful, e.g., to require your common module where all your usual macros and functions are defined, or for automatic comments.