Code Editor Scripting Commands¶
As described at Recording, Editing, and Writing Macros, macros are Python scripts which automate tasks in Canopy. That section shows several examples of macros that can be useful in the code editor.
Almost anything that a person can do in the code editor, a macro can also do: create or open a file, read or search its contents, edit it, save it, run it.
This section describes the most common macro commands which are useful in the code editor.
Please note that the scripting API in this version of Canopy is likely to change in future releases. We plan for all recorded macros to be supported, but other current scripting commands may be changed.
Code editor traits¶
The code editor has the following traits, which provide useful information about the state of the code editor. You can use these in your scripting commands.
The contents of the text being edited (the entire file), as a string.
The currently selected text, as a single string.
The current line number of the cursor, where the first line is numbered 1.
The current column number of the cursor, where the leftmost position is numbered 1.
The position of the cursor – a list containing line number and column number.
Truewhen the text has been modified but not saved.
Code editor commands¶
The commands can be categorized into:
- File operations
- Cursor movement
- Text editing
File operation commands¶
Open a new empty file in the code editor.
code_task = get_active_task() code_editor = code_task.new_file()
open(parent=None, default_directory=None, task_id=None)¶
Show a dialog for opening file(s).
default_directoryis the directory which will be opened in the dialog.
task_idcan be used to specify the task in which the file should be opened. By default, the active task is chosen.
file_service = get_file_service() code_editor = file_service.open(default_directory=u'~/src/editor')
open_file(filename, default_task_id=None, **info)¶
Open a specified file in a specified task.
default_task_idcan be used to specify the task in which the file should be opened. By default, the active task is chosen.
code_editor = file_service.open_file(u'~/src/editor/editor.py')
Save the file in the active editor.
code_task = get_active_task() code_editor = code_task.active_editor code_editor.save()
Show a dialog for saving the file in the active editor.
Reload the file from disk.
Print the contents of the current file, as held in the code editor (not necessarily as saved to disk).
Print preview of the current file, as held in the code editor (not necessarily as saved to disk).
Run the current file, as held in the code editor (not necessarily as saved to disk).
Run the current selection.
Cursor movement commands¶
When present, the
count parameter specifies the number of characters or
lines that the cursor should be moved.
When present, the
select parameter specifies whether the text should be
selected, between the cursor’s initial and final positions.
Move the cursor left some number of characters.
code_task = get_active_task() code_editor = code_task.active_editor cursor = code_editor.cursor cursor.move_left(10)
Move the cursor right some number of characters.
Move the cursor to the start of the current line (column 1).
Move the cursor to the end of the current line.
Move the cursor up.
Move the cursor down.
Move the cursor to the previous beginning of a word. (If the cursor is in mid-word, this is the beginning of the current word; otherwise it is the beginning of the previous word.)
Move the cursor to the beginning of the next word.
Move the cursor to the start of the document (line 1, column 1).
Move the cursor to the end of the document.
Go to the specified line without changing column if possible. If the requested line number is too big, go to the last line. If the current column number is too big for that line, go the the last column of the line.
code_editor = code_task.active_editor code_editor.goto_line(99)
Go to the specified column of the current line if possible. If column is too big, go to the end of the line.
code_editor = code_task.active_editor code_editor.goto_column(5)
Text editing commands¶
Cut the selected text, if any, to the clipboard.
Copy the selected text, if any, to the clipboard.
Paste the text from the clipboard.
Select all the text in the document.
Indent the selected block of text.
Unindent the selected block of text.
Add a new line and set its indent level, automatically.
Delete the previous character or unindent, as appropriate.
Comment or uncomment a block of code, as appropriate.
find(text, case=False, word=False, wrap=True, regex=False, count=1)¶
Find occurrences of
textin the document.
case: whether the search is case sensitive.
wrap: whether we should wrap around the end of the document, when searching.
word: whether only full words are matched.
regex: whether the search text should be interpreted as a regular expression.
count: how many repetitions of the search to do. If count is negative, then the search is backwards.
code_editor.find('^def', regex=True, count=2)
replace(text, replace_text, case=False, word=False, wrap=True, regex=False, count=1)¶
Replace occurrences of
findfor description of parameters.
replace_all(text, replace_text, case=False, word=False, wrap=True, regex=False)¶
Replace all occurrences of