Skip to content

Instantly share code, notes, and snippets.

@lsem
Last active May 27, 2016 10:27
Show Gist options
  • Save lsem/a203ae0e65ee7a2483e12e170a496559 to your computer and use it in GitHub Desktop.
Save lsem/a203ae0e65ee7a2483e12e170a496559 to your computer and use it in GitHub Desktop.
CppLit example SublimeText plugin.
import sublime, sublime_plugin
#print (str(sublime.__dict__))
#http://code.tutsplus.com/tutorials/how-to-create-a-sublime-text-2-plugin--net-22685
# Usage: in console: view.run_command('cpplit')
# Or, place this source as zip-packed cpplit.py in cpplit.sublime-plugin in C:/Users/lsem/AppData/Roming/Sublime Text 3/Installed Packages/
# and define key binding for command('cpplit')
class CpplitCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
for region in view.sel():
print ('next region')
if not region.empty():
cpp_escaped_selection = self._escape_cpp(view.substr(region))
var_name = 'your_literal_name'
header = 'const char ' + var_name + '[] =\n'
footer = '; // ' + var_name
# print('\nCOPY:\n\n' + header + cpp_escaped_selection + footer)
sublime.set_clipboard(header + cpp_escaped_selection + footer)
print ('copied to clipboard. Use CTRL + V')
return
def _escape_cpp(self, text_to_escape):
escaped_text = ""
line_begin = True
for line in text_to_escape.splitlines():
escaped_text += '"'
for c in line:
if c == '"':
escaped_text += '\\"'
elif c == '\\':
escaped_text += '\\\\'
else:
escaped_text += c
escaped_text += '"'
escaped_text += ' "\\n"'
escaped_text += '\n'
return escaped_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment