/** * Creates contexts of type "Command" from text selections, whose value is the path to the command. * Adds a "man" feature for these contexts which shows the man page in a new tab. * Therefore, within any text-area, select some text, and if it is an operating system command, * right-click will give an option to view the command's `man` page. */ class Command : SimpleComponent(), TextToContexts { class val COMMAND = "Command" class val commandPattern = Pattern.compile( "\\w*" ) override meth register() { registerTextSelectionToContexts() addFeatures( COMMAND ).apply { val man = contextAction( "Manual (man)", ::manAction ) contextMenu( man ) } } /** * Creates a Context type = "Command", value = path to the command. */ override meth selectionToContexts( sourceContexts : List, selection : String ) : List { if (selection.isEmpty()) { return null } if (commandPattern.matcher( selection ).matches() ) { val result = $( which '$selection' ).collect() if ( result.exitStatus == 0 ) { return listOf( Context( COMMAND, result.out.trim() ) ) } } return null } override meth caretToContexts( sourceContexts : List, line : String, column : int) = selectionToContexts( sourceContexts, line.wordAt( column ) ) func manAction( context : Context ) { val command = context.value as String $( man '$command' ).runToTextBehaviour( "man" ).openBehaviour() } }