Skip to content

Commit d1b5f21

Browse files
committed
Merged with maximsch2 main repo
2 parents 4002ebd + 5532afa commit d1b5f21

6 files changed

Lines changed: 117 additions & 30 deletions

SublimeIPythonNotebook.tmLanguage

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
55
<key>fileTypes</key>
@@ -27,7 +27,7 @@
2727
</dict>
2828
</dict>
2929
<key>contentName</key>
30-
<string>ipynb.source.input.r</string>
30+
<string>ipynb.source.input.r source.r</string>
3131
<key>end</key>
3232
<string>\#\/Input</string>
3333
<key>endCaptures</key>
@@ -46,6 +46,38 @@
4646
</dict>
4747
</array>
4848
</dict>
49+
<dict>
50+
<key>begin</key>
51+
<string>\#Markdown
52+
</string>
53+
<key>beginCaptures</key>
54+
<dict>
55+
<key>0</key>
56+
<dict>
57+
<key>name</key>
58+
<string>comment</string>
59+
</dict>
60+
</dict>
61+
<key>contentName</key>
62+
<string>ipynb.text.tex.latex text.tex.latex</string>
63+
<key>end</key>
64+
<string>\#\/Markdown</string>
65+
<key>endCaptures</key>
66+
<dict>
67+
<key>0</key>
68+
<dict>
69+
<key>name</key>
70+
<string>comment</string>
71+
</dict>
72+
</dict>
73+
<key>patterns</key>
74+
<array>
75+
<dict>
76+
<key>include</key>
77+
<string>text.tex.latex</string>
78+
</dict>
79+
</array>
80+
</dict>
4981
<dict>
5082
<key>begin</key>
5183
<string>\#Input\[(.*)\]\n</string>
@@ -58,7 +90,7 @@
5890
</dict>
5991
</dict>
6092
<key>contentName</key>
61-
<string>ipynb.source.input.python</string>
93+
<string>ipynb.source.input.python source.python</string>
6294
<key>end</key>
6395
<string>(\#\/Input\[\1\])$</string>
6496
<key>endCaptures</key>

SublimeIPythonNotebook.tmLanguage.JSON

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
{
1616

17-
"contentName": "ipynb.source.input.r",
17+
"contentName": "ipynb.source.input.r source.r",
1818
"begin": "\\#Input-R\\[.*\\]\n",
1919
"end": "\\#\\/Input",
2020
"beginCaptures": {
@@ -30,8 +30,27 @@
3030
]
3131
},
3232

33+
{
34+
35+
"contentName": "ipynb.text.tex.latex text.tex.latex",
36+
"begin": "\\#Markdown\n",
37+
"end": "\\#\\/Markdown",
38+
"beginCaptures": {
39+
"0": { "name": "comment"}
40+
},
41+
"endCaptures": {
42+
"0": { "name": "comment"}
43+
},
44+
"patterns": [
45+
{
46+
"include": "text.tex.latex"
47+
},
48+
]
49+
},
50+
51+
3352
{
34-
"contentName": "ipynb.source.input.python",
53+
"contentName": "ipynb.source.input.python source.python",
3554
"begin": "\\#Input\\[(.*)\\]\\n",
3655
"end": "(\\#\\/Input\\[\\1\\])$",
3756
"patterns": [

TODO.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@
66
- Support set_next_input
77
- Add an option of saving a backup copy of notebook json file (use git to have all copies?):
88
- For now it is possible to use inb_open_as_ipynb command
9-
- Support image preview in some external program
9+
- Support image preview in some external program
10+
11+
BUGS:
12+
13+
Wrong highlighting for open """ .
14+
15+
TODO for IPython 1.0
16+
17+
- support stdin input for %debug, raw_input and so on

ipy_connection.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright (c) 2013, Maxim Grechkin
33
# This file is licensed under GNU General Public License version 3
44
# See COPYING for details.
5-
5+
from __future__ import print_function
66
import json
77
import uuid
88

@@ -23,33 +23,42 @@
2323
from external.websocket import websocket
2424
from external.websocket.websocket import *
2525
from urlparse import urlparse
26-
from urllib2 import urlopen
27-
from urllib2 import Request
26+
from urllib2 import urlopen, Request, ProxyHandler, build_opener, install_opener
2827
else:
2928
import _thread
30-
from SublimeIPythonNotebook.external import nbformat3 as nbformat
31-
from SublimeIPythonNotebook.external.websocket import websocket3 as websocket
32-
from SublimeIPythonNotebook.external.websocket.websocket3 import *
33-
from urllib.request import urlopen, Request
29+
from .external import nbformat3 as nbformat
30+
from .external.websocket import websocket3 as websocket
31+
from .external.websocket.websocket3 import *
32+
from urllib.request import urlopen, Request, ProxyHandler, build_opener, install_opener
3433
from urllib.parse import urlparse
3534

3635

3736

37+
38+
def install_proxy_opener():
39+
proxy = ProxyHandler({})
40+
opener = build_opener(proxy)
41+
install_opener(opener)
42+
43+
install_proxy_opener()
44+
3845
def create_uid():
3946
return str(uuid.uuid4())
4047

41-
4248
def get_notebooks(baseurl):
49+
target_url = "http://%s/notebooks" % baseurl
4350
try:
44-
req = urlopen("http://" + baseurl + "/notebooks")
51+
req = urlopen(target_url)
4552
try:
4653
encoding = req.headers.get_content_charset()
4754
body = req.readall().decode(encoding)
4855
except AttributeError:
4956
body = req.read()
5057
data = json.loads(body)
5158
return data
52-
except:
59+
except Exception as e:
60+
print("Error during loading notebook list from ", target_url)
61+
print(e)
5362
return None
5463

5564
def create_new_notebook(baseurl):

ipy_view.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# Copyright (c) 2013, Maxim Grechkin
33
# This file is licensed under GNU General Public License version 3
44
# See COPYING for details.
5-
5+
from __future__ import print_function
66
import sublime
77
try:
8-
from SublimeIPythonNotebook import ipy_connection
9-
except ImportError:
8+
from . import ipy_connection
9+
except:
1010
import ipy_connection
1111
import re
1212

@@ -97,6 +97,9 @@ def get_input_content(self):
9797
return self.view.substr(input_region)
9898
else:
9999
return ""
100+
101+
def check_R(self):
102+
pass
100103

101104

102105
class CodeCellView(BaseCellView):
@@ -210,7 +213,7 @@ def rewrite_prompt_number(self, edit):
210213
out_reg = self.get_region("inb_output")
211214
line = self.view.line(out_reg.begin() - 1)
212215
self.view.replace(edit, line, "#Output[%s]" % self.prompt)
213-
216+
214217

215218

216219
def output_result(self, edit):
@@ -285,7 +288,7 @@ def __init__(self, view, notebook_id, baseurl):
285288
self.baseurl = baseurl
286289
view.set_scratch(True)
287290
#view.set_syntax_file("Packages/Python/Python.tmLanguage")
288-
view.set_syntax_file("Packages/SublimeIPythonNotebook/SublimeIPythonNotebook.tmLanguage")
291+
view.set_syntax_file("Packages/IPython Notebook/SublimeIPythonNotebook.tmLanguage")
289292
view.settings().set("ipython_notebook", True)
290293
self.cells = []
291294
self.notebook_id = notebook_id
@@ -356,7 +359,7 @@ def on_modified(self):
356359
for s in self.view.sel():
357360
for i, reg in enumerate(regset):
358361
reg = sublime.Region(reg.begin()+1, reg.end()-1)
359-
if reg.contains(s):
362+
if reg.contains(s) and (i < len(self.cells)):
360363
self.cells[i].check_R()
361364
break
362365

@@ -461,7 +464,7 @@ def render_notebook(self, edit):
461464

462465
for cell in self.cells:
463466
cell.draw(edit)
464-
467+
465468
if len(self.cells) > 0:
466469
self.cells[0].select()
467470

@@ -484,6 +487,11 @@ def set_status():
484487
sublime.set_timeout(set_status, 0)
485488

486489
def handle_completions(self, view, prefix, locations):
490+
cell_index = self.get_current_cell_index()
491+
if cell_index < 0:
492+
return None
493+
if not isinstance(self.cells[cell_index], CodeCellView):
494+
return None
487495
sel = view.sel()
488496
if len(sel) > 1:
489497
return []
@@ -492,7 +500,7 @@ def handle_completions(self, view, prefix, locations):
492500
row, col = view.rowcol(sel.begin())
493501
compl = self.kernel.get_completitions(line, col, timeout=0.7)
494502

495-
503+
496504
if len(compl) > 0:
497505
def get_last_word(s): # needed for file/directory completion
498506
if s.endswith("/"):
@@ -668,4 +676,4 @@ def on_close(self, view):
668676
if id in self.views:
669677
del self.views[id]
670678

671-
manager = NotebookViewManager()
679+
manager = NotebookViewManager()

subl_ipy_notebook.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# Copyright (c) 2013, Maxim Grechkin
33
# This file is licensed under GNU General Public License version 3
44
# See COPYING for details.
5-
5+
from __future__ import print_function
66
import sublime
77
import sublime_plugin
88
try:
9-
from SublimeIPythonNotebook import ipy_view, ipy_connection
10-
except ImportError:
9+
from . import ipy_view, ipy_connection
10+
except:
1111
import ipy_view, ipy_connection
1212

1313

@@ -29,9 +29,19 @@ def on_close(self, view):
2929
manager.on_close(view)
3030

3131

32+
def get_last_used_address():
33+
settings = sublime.load_settings("SublimeIPythonNotebook.sublime-settings")
34+
return settings.get("default_address", "127.0.0.1:8888")
35+
36+
37+
def set_last_used_address(value):
38+
settings = sublime.load_settings("SublimeIPythonNotebook.sublime-settings")
39+
settings.set("default_address", value)
40+
sublime.save_settings("SublimeIPythonNotebook.sublime-settings")
41+
3242
class InbPromptListNotebooksCommand(sublime_plugin.WindowCommand):
3343
def run(self):
34-
self.window.show_input_panel("Notebook host:port : ", "127.0.0.1:8888",
44+
self.window.show_input_panel("Notebook host:port : ", get_last_used_address(),
3545
self.on_done, None, None)
3646

3747
def on_done(self, line):
@@ -43,8 +53,9 @@ def run(self, baseurl):
4353
self.baseurl = baseurl
4454
nbs = ipy_connection.get_notebooks(baseurl)
4555
if nbs is None:
46-
print ("Cannot get a list of notebooks")
56+
print("Cannot get a list of notebooks")
4757
return
58+
set_last_used_address(baseurl)
4859
self.nbs = nbs
4960
lst = ["0: Create New Notebook\n"]
5061
for i, nb in enumerate(nbs):

0 commit comments

Comments
 (0)