AUTHORS:
This module provides a routine to open the source file of a python object in an editor of your choice, if the source file can be figured out. For files that appear to be from the sage library, the path name gets modified to the corresponding file in the current branch, i.e., the file that gets copied into the library upon ‘sage -br’.
The editor to be run, and the way it should be called to open the requested file at the right line number, can be supplied via a template. For a limited number of editors, templates are already known to the system. In those cases it suffices to give the editor name.
In fact, if the environment variable EDITOR is set to a known editor, then the system will use that if no template has been set explicitly.
Open source code of obj in editor of your choice.
INPUT:
AUTHOR:
EXAMPLES:
This is a typical example of how to use this routine.
# make some object obj
sage: edit(obj) # not tested
Now for more details and customization:
sage: import sage.misc.edit_module as m
sage: m.set_edit_template("vi -c ${line} ${file}")
In fact, since vi is a well-known editor, you could also just use
sage: m.set_editor("vi")
To illustrate:
sage: m.edit_template.template
'vi -c ${line} ${file}'
And if your environment variable EDITOR is set to a recognised editor, you would not have to set anything.
To edit the source of an object, just type something like:
sage: edit(edit) # not tested
This function is for internal use and is called by IPython when you use the IPython commands %edit or %ed.
This hook calls the default implementation, but changes the filename for files that appear to be from the sage library: if the filename begins with SAGE_ROOT/local/lib/python.../site-packages/ it replaces this by SAGE_ROOT/devel/sage
EXAMPLES:
sage: %edit gcd # indirect doctest, not tested
sage: %ed gcd # indirect doctest, not tested
The above should open your favorite editor (as stored in the environment variable EDITOR) with the file in which gcd is defined, and when your editor supports it, also at the line in wich gcd is defined.
Look up source file and line number of obj.
If the file lies in the Sage library, the path name of the corresponding file in the current branch (i.e., the file that gets copied into the Sage library upon running ‘sage -br’). Note that the first line of a file is considered to be 1 rather than 0 because most editors think that this is the case.
AUTHORS:
EXAMPLES:
sage: import sage.misc.edit_module as edit_module
sage: edit_module.file_and_line(sage)
('...sage/__init__.py', 0)
The following tests against a bug that was fixed in trac ticket #11298:
sage: edit_module.file_and_line(x)
('...sage/symbolic/expression.pyx', ...)
Sets default edit template string.
It should reference ${file} and ${line}. This routine normally needs to be called prior to using ‘edit’. However, if the editor set in the shell variable EDITOR is known, then the system will substitute an appropriate template for you. See edit_module.template_defaults for the recognised templates.
AUTHOR:
EXAMPLES:
sage: from sage.misc.edit_module import set_edit_template
sage: set_edit_template("echo EDIT ${file}:${line}")
sage: edit(sage) # not tested
EDIT /usr/local/sage/default/devel/sage/sage/__init__.py:1
Sets the editor to be used by the edit command by basic editor name.
Currently, the system only knows appropriate call strings for a limited number of editors. If you want to use another editor, you should set the whole edit template via set_edit_template.
AUTHOR:
EXAMPLES:
sage: from sage.misc.edit_module import set_editor
sage: set_editor('vi')
sage: sage.misc.edit_module.edit_template.template
'vi -c ${line} ${file}'
Given a String.Template object, returns the fields.
AUTHOR:
EXAMPLES:
sage: from sage.misc.edit_module import template_fields
sage: from string import Template
sage: t=Template("Template ${one} with ${two} and ${three}")
sage: template_fields(t)
['three', 'two', 'one']