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