Following @jfcherng’s link, we can write a plugin in less than 50 lines of Python.
Save this in your Packages/User/ directory:
import sublime
import sublime_plugin
a = ord('a')
z = ord('z')
A = ord('A')
Z = ord('Z')
i0 = ord('0')
i9 = ord('9')
ROT_MIN = 33 # !
ROT_MAX = 126 # ~
RANGES = {
5: ((i0, i9, 5),),
13: ((A, Z, 13), (a, z, 13)),
18: ((i0, i9, 5), (A, Z, 13), (a, z, 13)),
47: ((ROT_MIN, ROT_MAX, 47),)
}
def rot(text, rot_ranges):
result = ""
for c in text:
ord_c = ord(c)
transformed = False
for rot_range in rot_ranges:
if rot_range[0] <= ord_c <= rot_range[1]:
ord_c = (rot_range[1] + ord_c + rot_range[2] - 2 * rot_range[0] + 1) % (rot_range[1] - rot_range[0] + 1) + rot_range[0]
result += chr(ord_c)
transformed = True
break
if not transformed:
result += c
return result
class RotCommand(sublime_plugin.TextCommand):
def run(self, edit, type):
rot_ranges = RANGES.get(type)
if rot_ranges:
for region in self.view.sel():
transformed_text = rot(self.view.substr(region), rot_ranges)
self.view.replace(edit, region, transformed_text)
else:
sublime.status_message('"type" must be 5, 13, 18 or 47')
Add to your keybindings:
{"keys": ["ctrl+t"], "command": "rot", "args": {"type": 18}},
Now select text and hit CTRLT and it’ll transform the selected text using rot18. The "type"
argument in the keybinding can be 5, 13, 18 or 47.
By the way, rot
is not encryption/decryption. It’s a trivially reversible operation. Rot is its own inverse! Don’t use it for encryption!