Last active
September 17, 2017 02:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
// 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