# Jython Interpreter

The Ghidra Jython Interpreter provides a full general-purpose Jython interactive shell and allows you to interact with your current Ghidra session by exposing Ghidra's powerful Java API through the magic of Jython.

## Environment

> The Ghidra Jython Interpreter is configured to run in a similar context as a Ghidra script. Therefore, you immediately have access to variables such as `currentProgram`, `currentSelection`, `currentAddress`, etc without needing to import them. These variables exist as Java objects behind the scenes, but Jython allows you to interact with them through a Python interface, which is similar to Java in some ways.

> As in Java, classes outside of your current package/module need to be explicitly imported. For example, consider the following code snippet:

>         # Get a data type from the user
>         tool = state.getTool()
>         dtm = currentProgram.getDataTypeManager()
>         from ghidra.app.util.datatype import DataTypeSelectionDialog
>         from ghidra.util.data.DataTypeParser import AllowedDataTypes
>         selectionDialog = DataTypeSelectionDialog(tool, dtm, -1, AllowedDataTypes.FIXED_LENGTH)
>         tool.showDialog(selectionDialog)
>         dataType = selectionDialog.getUserChosenDataType()
>         if dataType != None: print "Chosen data type: " + str(dataType)

> `currentProgram` and `state` are defined within the Ghidra scripting class hierarchy, so nothing has to be explicitly imported before they can be used. However, because the `DataTypeSelectionDialog` class and `AllowedDataType` enum reside in different packages, they must be explicitly imported. Failure to do so will result in a Jython `NameError`.

## Clear

> This command clears the interpreter's display. Its effect is purely visual. It does not affect the state of the interpreter in any way.

## Interrupt

> This command issues a keyboard interrupt to the interpreter, which can be used to interrupt long running commands or loops.

## Reset

> This command resets the interpreter, which clears the display and resets all state.

## Keybindings

> The Ghidra Jython Interpreter supports the following hard-coded keybindings:

> - (up): Move backward in command stack
> - (down): Move forward in command stack
> - TAB: Show code completion window

> With the code completion window open:

> - TAB: Insert currently-selected code completion (if no completion selected, select the first available)
> - ENTER: Insert selected completion (if any) and close the completion window
> - (up): Select previous code completion
> - (down): Select next code completion
> - ESC: Hide code completion window

## Copy/Paste

> Copy and paste from within the Ghidra Jython Interpreter should work as expected for your given environment:

> - Windows: CTRL+C / CTRL+V
> - Linux: CTRL+C / CTRL+V
> - OS X: COMMAND+C / COMMAND+V

## API Documentation

> The built-in `help()` Jython function has been altered by the Ghidra Jython Interpreter to add support for displaying Ghidra's Javadoc (where available) for a given Ghidra class, method, or variable. For example, to see Ghidra's Javadoc on the `state` variable, simply do:

>         >>> help(state)
>         #####################################################
>         class ghidra.app.script.GhidraState
>           extends java.lang.Object

>          Represents the current state of a Ghidra tool

>         #####################################################

>         PluginTool getTool()
>            Returns the current tool.

>           @return ghidra.framework.plugintool.PluginTool: the current tool

>         -----------------------------------------------------
>          Project getProject()
>            Returns the current project.

>            @return ghidra.framework.model.Project: the current project

>         -----------------------------------------------------
>         ...
>         ...
>         ...

> Calling help() with no arguments will show the Javadoc for the GhidraScript class.

> Note: It may be necessary to import a Ghidra class before calling the built-in `help()` Jython function on it. Failure to do so will result in a Jython `NameError`.

## Additional Help

> For more information on the Jython environment, such as how to interact with Java objects through a Python interface, please refer to Jython's free e-book which can be found on the Internet at www.jython.org/jythonbook/en/1.0/

Provided by: JythonPlugin