Installs the editor that is called by IPython for the %edit magic. This overrides the default editor, which is generally set by your EDITOR environment variable or is notepad (windows) or vi (linux). By supplying a template string `run_template`, you can control how the editor is invoke
(template, wait=False)
| 16 | |
| 17 | |
| 18 | def install_editor(template, wait=False): |
| 19 | """Installs the editor that is called by IPython for the %edit magic. |
| 20 | |
| 21 | This overrides the default editor, which is generally set by your EDITOR |
| 22 | environment variable or is notepad (windows) or vi (linux). By supplying a |
| 23 | template string `run_template`, you can control how the editor is invoked |
| 24 | by IPython -- (e.g. the format in which it accepts command line options) |
| 25 | |
| 26 | Parameters |
| 27 | ---------- |
| 28 | template : basestring |
| 29 | run_template acts as a template for how your editor is invoked by |
| 30 | the shell. It should contain '{filename}', which will be replaced on |
| 31 | invocation with the file name, and '{line}', $line by line number |
| 32 | (or 0) to invoke the file with. |
| 33 | wait : bool |
| 34 | If `wait` is true, wait until the user presses enter before returning, |
| 35 | to facilitate non-blocking editors that exit immediately after |
| 36 | the call. |
| 37 | """ |
| 38 | |
| 39 | # not all editors support $line, so we'll leave out this check |
| 40 | # for substitution in ['$file', '$line']: |
| 41 | # if not substitution in run_template: |
| 42 | # raise ValueError(('run_template should contain %s' |
| 43 | # ' for string substitution. You supplied "%s"' % (substitution, |
| 44 | # run_template))) |
| 45 | |
| 46 | def call_editor(self, filename, line=0): |
| 47 | if line is None: |
| 48 | line = 0 |
| 49 | cmd = template.format(filename=shlex.quote(filename), line=line) |
| 50 | print(">", cmd) |
| 51 | # shlex.quote doesn't work right on Windows, but it does after splitting |
| 52 | if sys.platform.startswith('win'): |
| 53 | cmd = shlex.split(cmd) |
| 54 | proc = subprocess.Popen(cmd, shell=True) |
| 55 | if proc.wait() != 0: |
| 56 | raise TryNext() |
| 57 | if wait: |
| 58 | py3compat.input("Press Enter when done editing:") |
| 59 | |
| 60 | get_ipython().set_hook('editor', call_editor) |
| 61 | get_ipython().editor = template |
| 62 | |
| 63 | |
| 64 | # in these, exe is always the path/name of the executable. Useful |
no test coverage detected
searching dependent graphs…