Skip to content

Instantly share code, notes, and snippets.

@dantonnoriega
Last active September 17, 2017 02:50
Show Gist options
  • Save dantonnoriega/3c8a30fd0e33ba1d5cf92e3fa4d58996 to your computer and use it in GitHub Desktop.
Save dantonnoriega/3c8a30fd0e33ba1d5cf92e3fa4d58996 to your computer and use it in GitHub Desktop.
How to hijack Andrew Heiss' awesome "knitr" keybinds to work with "R-Box" Rmd syntax. Woot.
import sublime
import sublime_plugin
import os
import subprocess
import string
import re
# EDITED BY DANTON
class KnitrSendChunkCommand(sublime_plugin.TextCommand):
def run(self, view): # runs on command
initial_selection = self.view.sel()[0]
if self.view.match_selector(0, "text.tex.latex.knitr"):
for region in self.view.find_all('(?<=>>=\n)((.*\n)+?)(?=@)'):
if region.contains(initial_selection.a):
chunk_range = sublime.Region(region.a, region.b-1)
break
else:
chunk_range = None
elif self.view.match_selector(0, "text.html.markdown.knitr"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'):
if region.contains(initial_selection.a):
chunk_range = sublime.Region(region.a, region.b-1)
break
else:
chunk_range = None
# ADD SELECTOR CONTEXT FROM R-BOX
elif self.view.match_selector(0, "text.html.markdown.rmarkdown"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'):
if region.contains(initial_selection.a):
chunk_range = sublime.Region(region.a, region.b-1)
break
else:
chunk_range = None
# ADD SELECTOR CONTEXT FROM MARKDOWN EXTENDED
elif self.view.match_selector(0, "text.html.markdown"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'):
if region.contains(initial_selection.a):
chunk_range = sublime.Region(region.a, region.b-1)
break
else:
chunk_range = None
# ADD SELECTOR FOR PANDOC
elif self.view.match_selector(0, "text.pandoc"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'):
if region.contains(initial_selection.a):
chunk_range = sublime.Region(region.a, region.b-1)
break
else:
chunk_range = None
else:
chunk_range = None
if not chunk_range:
return
# Add selection
self.view.sel().add(chunk_range)
print("send chunk:\n%s" % self.view.substr(chunk_range))
# Run command from Enhanced-R
self.view.run_command('send_code')
# Restore initial selection
self.view.sel().subtract(chunk_range)
self.view.sel().add(initial_selection)
self.view.show(initial_selection.a)
class KnitrNextChunkCommand(sublime_plugin.TextCommand):
def run(self,edit):
initial_selection = self.view.sel()[0]
# Find next chunk
if self.view.match_selector(0, "text.tex.latex.knitr"):
for region in self.view.find_all('(?<=>>=\n)((.*\n)+?)(?=@)'):
if region.b > initial_selection.a < region.a:
chunk_range = region
break
else:
chunk_range = None
elif self.view.match_selector(0, "text.html.markdown.knitr"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'):
if region.b > initial_selection.a < region.a:
chunk_range = region
break
else:
chunk_range = None
# ADD SELECTOR CONTEXT FROM R-BOX
elif self.view.match_selector(0, "text.html.markdown.rmarkdown"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'):
if region.b > initial_selection.a < region.a:
chunk_range = region
break
else:
chunk_range = None
# ADD SELECTOR CONTEXT FROM MARKDOWN EXTENDED
elif self.view.match_selector(0, "text.html.markdown"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'):
if region.b > initial_selection.a < region.a:
chunk_range = region
break
else:
chunk_range = None
# ADD SELECTOR FROM PANDOC
elif self.view.match_selector(0, "text.pandoc"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'):
if region.b > initial_selection.a < region.a:
chunk_range = region
break
else:
chunk_range = None
else:
chunk_range = None
if chunk_range:
self.view.sel().clear()
self.view.sel().add(sublime.Region(chunk_range.a))
self.view.show(chunk_range.a)
else:
print("end of file reached with no more chunks")
class KnitrPrevChunkCommand(sublime_plugin.TextCommand):
def run(self,edit):
initial_selection = self.view.sel()[0]
# Find previous chunk
if self.view.match_selector(0, "text.tex.latex.knitr"):
for region in self.view.find_all('(?<=>>=\n)((.*\n)+?)(?=@)')[::-1]:
if region.b < initial_selection.a > region.a:
chunk_range = region
break
else:
chunk_range = None
elif self.view.match_selector(0, "text.html.markdown.knitr"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)')[::-1]:
if region.b < initial_selection.a > region.a:
chunk_range = region
break
else:
chunk_range = None
# ADD SELECTOR CONTEXT FROM R-BOX
elif self.view.match_selector(0, "text.html.markdown.rmarkdown"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)')[::-1]:
if region.b < initial_selection.a > region.a:
chunk_range = region
break
else:
chunk_range = None
# ADD SELECTOR CONTEXT FROM MARKDOWN EXTENDED
elif self.view.match_selector(0, "text.html.markdown"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)')[::-1]:
if region.b < initial_selection.a > region.a:
chunk_range = region
break
else:
chunk_range = None
# ADD SELECTOR CONTEXT FROM R-BOX
elif self.view.match_selector(0, "text.pandoc"):
for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)')[::-1]:
if region.b < initial_selection.a > region.a:
chunk_range = region
break
else:
chunk_range = None
else:
chunk_range = None
if chunk_range:
self.view.sel().clear()
self.view.sel().add(sublime.Region(chunk_range.a))
self.view.show(chunk_range.a)
else:
print("start of file reached with no more chunks")
// Send current chunk to R
{
"keys": ["super+alt+enter"],
"command": "knitr_send_chunk",
"context": [
{ "operand": "text.tex.latex.knitr source.r.embedded.knitr, text.html.markdown.knitr source.r.embedded.knitr, text.html.markdown.rmarkdown source.r", "operator": "equal", "match_all":true, "key": "selector" }
]
},
// Move between chunks
{
"keys": ["super+alt+."],
"command": "knitr_next_chunk",
"context": [
{ "operand": "text.tex.latex.knitr, text.html.markdown.knitr, text.html.markdown.rmarkdown", "operator": "equal", "match_all": true, "key": "selector" }
]
},
{
"keys": ["super+alt+,"],
"command": "knitr_prev_chunk",
"context": [
{ "operand": "text.tex.latex.knitr, text.html.markdown.knitr, text.html.markdown.rmarkdown", "operator": "equal", "match_all": true, "key": "selector" }
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment