##// END OF EJS Templates
editorhooks working with qtconsole
Robert McGibbon -
Show More
@@ -458,7 +458,7 b' class IPythonWidget(FrontendWidget):'
458 458 # 'IPythonWidget' protected interface
459 459 #---------------------------------------------------------------------------
460 460
461 def _edit(self, filename, line=None):
461 def _edit(self, filename, line=None, editor_template=None):
462 462 """ Opens a Python script for editing.
463 463
464 464 Parameters:
@@ -469,18 +469,28 b' class IPythonWidget(FrontendWidget):'
469 469 line : int, optional
470 470 A line of interest in the file.
471 471 """
472
473 # this is a template sent by the kernel, which overrides
474 # what we might have configured here
475 if editor_template:
476 self.editor_line = editor_template
477
472 478 if self.custom_edit:
473 479 self.custom_edit_requested.emit(filename, line)
474 elif not self.editor:
480 elif (not self.editor and not self.editor_line):
475 481 self._append_plain_text('No default editor available.\n'
476 482 'Specify a GUI text editor in the `IPythonWidget.editor` '
477 'configurable to enable the %edit magic')
483 'configurable, or by calling one of the `IPython.lib.editorhooks` '
484 'to enable the %edit magic')
478 485 else:
479 486 try:
480 487 filename = '"%s"' % filename
481 488 if line and self.editor_line:
482 489 command = self.editor_line.format(filename=filename,
483 490 line=line)
491 elif not line and self.editor_line:
492 command = self.editor_line.format(filename=filename,
493 line=0)
484 494 else:
485 495 try:
486 496 command = self.editor.format()
@@ -529,7 +539,7 b' class IPythonWidget(FrontendWidget):'
529 539 # arguments.
530 540
531 541 def _handle_payload_edit(self, item):
532 self._edit(item['filename'], item['line_number'])
542 self._edit(item['filename'], item['line_number'], item['editor_template'])
533 543
534 544 def _handle_payload_exit(self, item):
535 545 self._keep_kernel_on_exit = item['keepkernel']
@@ -22,7 +22,7 b' def install_editor(template, wait=False):'
22 22 ----------
23 23 template : basestring
24 24 run_template acts as a template for how your editor is invoked by
25 the shell. It should contain '{file}', which will be replaced on
25 the shell. It should contain '{filename}', which will be replaced on
26 26 invokation with the file name, and '{line}', $line by line number
27 27 (or 0) to invoke the file with.
28 28 wait : bool
@@ -39,44 +39,44 b' def install_editor(template, wait=False):'
39 39 # run_template)))
40 40
41 41
42 def call_editor(self, file, line=0):
42 def call_editor(self, filename, line=0):
43 43 if line is None:
44 44 line = 0
45 cmd = template.format(file=file, line=line)
45 cmd = template.format(filename=filename, line=line)
46 46 print ">", cmd
47 if os.system(cmd) != 0:
47 proc = subprocess.Popen(cmd, shell=True)
48 if wait and proc.wait() != 0:
48 49 raise TryNext()
49 50 if wait:
50 51 raw_input("Press Enter when done editing:")
51 52
52 53 get_ipython().set_hook('editor', call_editor)
53 ipython_widget.IPythonWidget.editor = template
54 get_ipython().editor = template
55
54 56
55 57 # in these, exe is always the path/name of the executable. Useful
56 58 # if you don't have the editor directory in your path
57
58
59 def komodo(exe='komodo'):
59 def komodo(exe=u'komodo'):
60 60 """ Activestate Komodo [Edit] """
61 install_editor(exe + ' -l {line} "{file}"', wait=True)
61 install_editor(exe + u' -l {line} "{filename}"', wait=True)
62 62
63 63
64 def scite(exe="scite"):
64 def scite(exe=u"scite"):
65 65 """ SciTE or Sc1 """
66 install_editor(exe + ' "{file}" -goto:{line}')
66 install_editor(exe + u' "{filename}" -goto:{line}')
67 67
68 68
69 def notepadplusplus(exe='notepad++'):
69 def notepadplusplus(exe=u'notepad++'):
70 70 """ Notepad++ http://notepad-plus.sourceforge.net """
71 install_editor(exe + ' -n{line} "{file}"')
71 install_editor(exe + u' -n{line} "{filename}"')
72 72
73 73
74 def jed(exe='jed'):
74 def jed(exe=u'jed'):
75 75 """ JED, the lightweight emacsish editor """
76 install_editor(exe + ' +{line} "{file}"')
76 install_editor(exe + u' +{line} "{filename}"')
77 77
78 78
79 def idle(exe='idle'):
79 def idle(exe=u'idle'):
80 80 """ Idle, the editor bundled with python
81 81
82 82 Parameters
@@ -86,17 +86,17 b" def idle(exe='idle'):"
86 86 """
87 87 if exe is None:
88 88 import idlelib
89 p = os.path.dirname(idlelib.__file__)
89 p = os.path.dirname(idlelib.__filename__)
90 90 # i'm not sure if this actually works. Is this idle.py script guarenteed
91 91 # to be executable?
92 92 exe = os.path.join(p, 'idle.py')
93 install_editor(exe + ' "{file}"')
93 install_editor(exe + u' "{filename}"')
94 94
95 95
96 def mate(exe='mate'):
96 def mate(exe=u'mate'):
97 97 """ TextMate, the missing editor"""
98 98 # wait=True is not required since we're using the -w flag to mate
99 install_editor(exe + ' -w -l {line} "{file}"')
99 install_editor(exe + u' -w -l {line} "{filename}"')
100 100
101 101
102 102 # ##########################################
@@ -104,17 +104,17 b" def mate(exe='mate'):"
104 104 # ##########################################
105 105
106 106
107 def emacs(exe='emacs'):
108 install_editor(exe + ' +{line} "{file}"')
107 def emacs(exe=u'emacs'):
108 install_editor(exe + u' +{line} "{filename}"')
109 109
110 110
111 def gnuclient(exe='gnuclient'):
112 install_editor(exe + ' -nw +{line} "{file}"')
111 def gnuclient(exe=u'gnuclient'):
112 install_editor(exe + u' -nw +{line} "{filename}"')
113 113
114 114
115 def crimson_editor(exe='cedt.exe'):
116 install_editor(exe + ' /L:{line} "{file}"')
115 def crimson_editor(exe=u'cedt.exe'):
116 install_editor(exe + u' /L:{line} "{filename}"')
117 117
118 118
119 def kate(exe='kate'):
120 install_editor(exe + ' -u -l {line} "{file}"')
119 def kate(exe=u'kate'):
120 install_editor(exe + u' -u -l {line} "{filename}"')
@@ -321,10 +321,16 b' class KernelMagics(Magics):'
321 321 # directory of client and kernel don't match
322 322 filename = os.path.abspath(filename)
323 323
324 try:
325 editor = self.shell.editor
326 except AttributeError:
327 editor = None
328
324 329 payload = {
325 330 'source' : 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic',
326 331 'filename' : filename,
327 'line_number' : lineno
332 'line_number' : lineno,
333 'editor_template' : editor
328 334 }
329 335 self.shell.payload_manager.write_payload(payload)
330 336
General Comments 0
You need to be logged in to leave comments. Login now