Skip to content

Instantly share code, notes, and snippets.

@leonid-ed
Last active October 10, 2016 09:55
Show Gist options
  • Save leonid-ed/317f19c9cc1884594d5258789285f8c4 to your computer and use it in GitHub Desktop.
Save leonid-ed/317f19c9cc1884594d5258789285f8c4 to your computer and use it in GitHub Desktop.
This is a Sublime Text 3 plugin that makes a numberred list.
# The MIT License (MIT)
# Copyright (c) 2016 Leonid Edrenkin
#
# This is a Sublime Text 3 plugin that makes and unmakes a numberred list.
# You should select text lines and apply one of commands:
# view.run_command('make_numberred_list')
# view.run_command('unmake_numberred_list')
import sublime, sublime_plugin
import re
class MakeNumberredList(sublime_plugin.TextCommand):
def run(self, edit):
v = self.view
regions = [ s for s in self.view.sel() if not s.empty() ]
if len(regions) == 0:
return
for region in regions:
region = v.full_line(region)
new_lines = []
lines = v.split_by_newlines(region)
item_num = 1
for i, line in enumerate(lines):
s = v.substr(line)
s = s.rstrip()
if len(s) > 0:
s = str(item_num) + ") " + s
item_num += 1
s += "\n"
new_lines.append(s)
p = region.begin()
v.erase(edit, region)
v.insert(edit, p, ''.join(new_lines))
class UnmakeNumberredList(sublime_plugin.TextCommand):
def run(self, edit):
v = self.view
regions = [ s for s in self.view.sel() if not s.empty() ]
if len(regions) == 0:
return
for region in regions:
region = v.full_line(region)
new_lines = []
lines = v.split_by_newlines(region)
for line in lines:
s = v.substr(line)
s = s.rstrip()
m = re.match("^\d+\s*\)\s*(.*)", s)
if m: s = m.group(1)
s += "\n"
new_lines.append(s)
p = region.begin()
v.erase(edit, region)
v.insert(edit, p, ''.join(new_lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment