Skip to content

Instantly share code, notes, and snippets.

@WillPresley
Forked from jonathanhirz/timestamp2.py
Last active August 29, 2015 14:14
Show Gist options
  • Save WillPresley/31d71906e2a44f0ff221 to your computer and use it in GitHub Desktop.
Save WillPresley/31d71906e2a44f0ff221 to your computer and use it in GitHub Desktop.
Adjusted Sublime Text 3 plugin for automatic Timestamps (USA datetime formats)
"""
Automatically add and update time stamps in your files.
On file open, will replace [timeStamp] with the current date and time.
Currently looks for two instances of this (example below).
To use, place the text [timeStamp] in your template file where you want it.
ex.
// CREATED: timeStamp
// MODIFIED: timeStamp
When you open this file for the first time, both [timeStamp] will change to
the current date and time. Every save, the MODIFIED line will update.
NOTE: After installing, don't open this file in Sublime Text because it will change
[timeStamp] in the code to the current time. Oops =]
"""
import datetime
import sublime
import sublime_plugin
TIMESTAMP_CODE = 'timeStamp'
TIMESTAMP_PATTERN = 'MODIFIED:\s*(\d+-\d+-20[0-9][0-9]+\s+\d+:\d+[a-zA-Z]+)?'
class InsertTimestampListener (sublime_plugin.EventListener):
"""
When opening file, search for TIMESTAMP_CODE and replace it with current time
"""
def on_load (self, view):
region = view.find (TIMESTAMP_CODE, 1)
if region :
replacement = datetime.datetime.now().strftime('%m-%d-%Y %I:%M%p')
with Edit(view) as edit:
edit.replace (region, replacement)
region = view.find (TIMESTAMP_CODE, 1)
if region :
replacement = datetime.datetime.now().strftime('%m-%d-%Y %I:%M%p')
with Edit(view) as edit:
edit.replace (region, replacement)
class UpdateTimestampListener (sublime_plugin.EventListener):
"""
On save, search for the MODIFIED line, and replace timestamp with current time
"""
def on_pre_save (self, view):
region = view.find (TIMESTAMP_PATTERN, 1)
if region :
timestamp = datetime.datetime.now().strftime('%m-%d-%Y %I:%M%p')
replacement = 'MODIFIED: %s' % timestamp
with Edit(view) as edit:
edit.replace (region, replacement)
# edit.py
# buffer editing for both ST2 and ST3 that "just works"
# cortesy of "Edit.py" ST2/3 Edit Abstraction. See: http://www.sublimetext.com/forum/viewtopic.php?f=6&t=12551
import sublime
import sublime_plugin
from collections import defaultdict
try:
sublime.edit_storage
except AttributeError:
sublime.edit_storage = {}
class EditStep:
def __init__(self, cmd, *args):
self.cmd = cmd
self.args = args
def run(self, view, edit):
if self.cmd == 'callback':
return self.args[0](view, edit)
funcs = {
'insert': view.insert,
'erase': view.erase,
'replace': view.replace,
}
func = funcs.get(self.cmd)
if func:
func(edit, *self.args)
class Edit:
defer = defaultdict(dict)
def __init__(self, view):
self.view = view
self.steps = []
def step(self, cmd, *args):
step = EditStep(cmd, *args)
self.steps.append(step)
def insert(self, point, string):
self.step('insert', point, string)
def erase(self, region):
self.step('erase', region)
def replace(self, region, string):
self.step('replace', region, string)
def callback(self, func):
self.step('callback', func)
def run(self, view, edit):
for step in self.steps:
step.run(view, edit)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
view = self.view
if sublime.version().startswith('2'):
edit = view.begin_edit()
self.run(edit)
view.end_edit(edit)
else:
key = str(hash(tuple(self.steps)))
sublime.edit_storage[key] = self.run
view.run_command('apply_edit', {'key': key})
class apply_edit(sublime_plugin.TextCommand):
def run(self, edit, key):
sublime.edit_storage.pop(key)(self.view, edit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment