##// END OF EJS Templates
Backport PR #12932: remove use of deprecated pipes module...
Matthias Bussonnier -
Show More
@@ -1,128 +1,127 b''
1 1 """ 'editor' hooks for common editors that work well with ipython
2 2
3 3 They should honor the line number argument, at least.
4 4
5 5 Contributions are *very* welcome.
6 6 """
7 7
8 8 import os
9 import pipes
10 9 import shlex
11 10 import subprocess
12 11 import sys
13 12
14 13 from IPython import get_ipython
15 14 from IPython.core.error import TryNext
16 15 from IPython.utils import py3compat
17 16
18 17
19 18 def install_editor(template, wait=False):
20 19 """Installs the editor that is called by IPython for the %edit magic.
21 20
22 21 This overrides the default editor, which is generally set by your EDITOR
23 22 environment variable or is notepad (windows) or vi (linux). By supplying a
24 23 template string `run_template`, you can control how the editor is invoked
25 24 by IPython -- (e.g. the format in which it accepts command line options)
26 25
27 26 Parameters
28 27 ----------
29 28 template : basestring
30 29 run_template acts as a template for how your editor is invoked by
31 30 the shell. It should contain '{filename}', which will be replaced on
32 31 invocation with the file name, and '{line}', $line by line number
33 32 (or 0) to invoke the file with.
34 33 wait : bool
35 34 If `wait` is true, wait until the user presses enter before returning,
36 35 to facilitate non-blocking editors that exit immediately after
37 36 the call.
38 37 """
39 38
40 39 # not all editors support $line, so we'll leave out this check
41 40 # for substitution in ['$file', '$line']:
42 41 # if not substitution in run_template:
43 42 # raise ValueError(('run_template should contain %s'
44 43 # ' for string substitution. You supplied "%s"' % (substitution,
45 44 # run_template)))
46 45
47 46 def call_editor(self, filename, line=0):
48 47 if line is None:
49 48 line = 0
50 cmd = template.format(filename=pipes.quote(filename), line=line)
49 cmd = template.format(filename=shlex.quote(filename), line=line)
51 50 print(">", cmd)
52 # pipes.quote doesn't work right on Windows, but it does after splitting
51 # shlex.quote doesn't work right on Windows, but it does after splitting
53 52 if sys.platform.startswith('win'):
54 53 cmd = shlex.split(cmd)
55 54 proc = subprocess.Popen(cmd, shell=True)
56 55 if proc.wait() != 0:
57 56 raise TryNext()
58 57 if wait:
59 58 py3compat.input("Press Enter when done editing:")
60 59
61 60 get_ipython().set_hook('editor', call_editor)
62 61 get_ipython().editor = template
63 62
64 63
65 64 # in these, exe is always the path/name of the executable. Useful
66 65 # if you don't have the editor directory in your path
67 66 def komodo(exe=u'komodo'):
68 67 """ Activestate Komodo [Edit] """
69 68 install_editor(exe + u' -l {line} {filename}', wait=True)
70 69
71 70
72 71 def scite(exe=u"scite"):
73 72 """ SciTE or Sc1 """
74 73 install_editor(exe + u' {filename} -goto:{line}')
75 74
76 75
77 76 def notepadplusplus(exe=u'notepad++'):
78 77 """ Notepad++ http://notepad-plus.sourceforge.net """
79 78 install_editor(exe + u' -n{line} {filename}')
80 79
81 80
82 81 def jed(exe=u'jed'):
83 82 """ JED, the lightweight emacsish editor """
84 83 install_editor(exe + u' +{line} {filename}')
85 84
86 85
87 86 def idle(exe=u'idle'):
88 87 """ Idle, the editor bundled with python
89 88
90 89 Parameters
91 90 ----------
92 91 exe : str, None
93 92 If none, should be pretty smart about finding the executable.
94 93 """
95 94 if exe is None:
96 95 import idlelib
97 96 p = os.path.dirname(idlelib.__filename__)
98 97 # i'm not sure if this actually works. Is this idle.py script
99 98 # guaranteed to be executable?
100 99 exe = os.path.join(p, 'idle.py')
101 100 install_editor(exe + u' {filename}')
102 101
103 102
104 103 def mate(exe=u'mate'):
105 104 """ TextMate, the missing editor"""
106 105 # wait=True is not required since we're using the -w flag to mate
107 106 install_editor(exe + u' -w -l {line} {filename}')
108 107
109 108
110 109 # ##########################################
111 110 # these are untested, report any problems
112 111 # ##########################################
113 112
114 113
115 114 def emacs(exe=u'emacs'):
116 115 install_editor(exe + u' +{line} {filename}')
117 116
118 117
119 118 def gnuclient(exe=u'gnuclient'):
120 119 install_editor(exe + u' -nw +{line} {filename}')
121 120
122 121
123 122 def crimson_editor(exe=u'cedt.exe'):
124 123 install_editor(exe + u' /L:{line} {filename}')
125 124
126 125
127 126 def kate(exe=u'kate'):
128 127 install_editor(exe + u' -u -l {line} {filename}')
General Comments 0
You need to be logged in to leave comments. Login now