I really love WriteMonkey’s repository feature which moves text to a repository (actually, the end of the text file which is hidden in the normal view). I wanted something similar, but to move it to a different text file with an extension ‘.repo_md’ that is also in my list of header extensions so it can easily be opened with alt+o. Any suggestions for improvement?
from os.path import abspath, basename, dirname, exists, splitext
import sublime, sublime_plugin
import time
# https://www.sublimetext.com/docs/3/api_reference.html
class MoveToRepoCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if not region.empty():
selection = self.view.substr(region)
date = time.strftime("%Y-%m-%d %H:%M UTC", time.localtime())
chunk = '\n<!-- moved from main text: %s -->\n\n%s\n\n' % (
date, selection)
print(chunk)
fn = abspath(self.view.file_name())
path = dirname(fn)
fn_base, fn_ext = splitext(basename(fn))
fn_repo = path + '/' + fn_base + '.repo_md'
print('fn_repo = %s' % fn_repo)
if exists(fn_repo):
with open(fn_repo, 'r') as repo:
repo_content = repo.read()
else:
repo_content = ''
with open(fn_repo, 'w') as repo:
repo.write(chunk + repo_content)
self.view.replace(edit, region, '')