From baff9ae3fd2a168a19b2df0ee298e26419e5c297 Mon Sep 17 00:00:00 2001
From: "Kenneth Benzie (Benie)" <benie@infektor.net>
Date: Sat, 30 Mar 2019 17:07:29 +0000
Subject: [PATCH] Improve global snippets

* `todo`, `note` now do not add comment strings when in a comment line
* `fixme` added to match `todo` and `note
* `date` renamed `datetime` and updated to match the unix `date` command
* `date`, `time` added inserting only the date or time now
* `utc` updated, no longer spitting the timezone offset
---
 UltiSnips/all.snippets | 100 ++++++++++++++++++++++++-----------------
 1 file changed, 58 insertions(+), 42 deletions(-)

diff --git a/UltiSnips/all.snippets b/UltiSnips/all.snippets
index 1a06dcb..31665b8 100644
--- a/UltiSnips/all.snippets
+++ b/UltiSnips/all.snippets
@@ -5,69 +5,85 @@ Kenneth Benzie (Benie) <k.benzie83@gmail.com>
 endsnippet
 
 global !p
-comment=vim.current.buffer.options['commentstring'].decode('utf-8').strip()
+class Comment(object):
+	def __init__(self):
+		# Get the commentstring.
+		commentstring = snip.opt('&commentstring').strip()
+		# Slipt it into before and after parts.
+		commentparts = [part.strip() for part in commentstring.split('%s')]
+		self.commentbefore = commentparts[0] + ' '
+		self.commentafter = ' ' + commentparts[1] if commentparts[1] else ''
+		# Determine if we are in an existing comment.
+		line = vim.current.buffer[vim.current.window.cursor[0] - 1].strip()
+		# If interpolation has not occcured and the current line starts with the
+		# comment leader, we are in a comment.
+		self.incomment = not snip.c and line.startswith(commentparts[0])
 
-def commentbefore():
-	commentbefore = comment[0:comment.find('%s')].strip()
-	row, col = vim.current.window.cursor
-	row -= 1
-	col -= 1
-	line = vim.current.buffer[row]
-	if (col - 1) >= 0 and commentbefore == line[col - 1:len(commentbefore)]:
-		return ''
-	return commentbefore + ' '
+	def before(self):
+		return '' if self.incomment else self.commentbefore
 
-def commentafter():
-	after=comment[comment.find('%s')+2:].strip()
-	if 0 < len(after):
-		return ' ' + after
-	return ''
+	def after(self):
+		return '' if self.incomment else self.commentafter
+endglobal
 
-import datetime
-import time
+snippet todo "TODO commment"
+`!p comment=Comment()
+snip.rv=comment.before()`TODO${1/.+/(/}$1${1/.+/)/}: $0`!p snip.rv=comment.after()`
+endsnippet
 
-def date():
-	return datetime.datetime.now().strftime('%B %d, %Y')
+snippet fixme "FIXME comment"
+`!p comment=Comment()
+snip.rv=comment.before()`FIXME${1/.+/(/}$1${1/.+/)/}: $0`!p snip.rv=comment.after()`
+endsnippet
 
-class LocalTZ(datetime.tzinfo):
+snippet note "NOTE comment"
+`!p comment=Comment()
+snip.rv=comment.before()`NOTE: $0`!p snip.rv=comment.after()`
+endsnippet
+
+global !p
+from datetime import datetime, timedelta, tzinfo
+from time import daylight, gmtime, localtime, timezone, tzname
+
+class LocalTZ(tzinfo):
 	"""Query OS for local timezone offset."""
 
 	def __init__(self):
 		"""Initialize LocalTZ."""
-		datetime.tzinfo.__init__(self)
-		self._unixEpochOrdinal = datetime.datetime.utcfromtimestamp(
+		tzinfo.__init__(self)
+		self.unixEpochOrdinal = datetime.utcfromtimestamp(
 			0).toordinal()
 
 	def dst(self, _):
-		return datetime.timedelta(0)
+		return timedelta(0)
 
 	def utcoffset(self, dt):
-		t = ((dt.toordinal() - self._unixEpochOrdinal) * 86400 + dt.hour * 3600
-			 + dt.second + time.timezone)
-		utc = datetime.datetime(*time.gmtime(t)[:6])
-		local = datetime.datetime(*time.localtime(t)[:6])
+		t = ((dt.toordinal() - self.unixEpochOrdinal) * 86400 + dt.hour * 3600
+			 + dt.second + timezone)
+		utc = datetime(*gmtime(t)[:6])
+		local = datetime(*localtime(t)[:6])
 		return local - utc
 
-def utc():
-	value = datetime.datetime.strftime(
-		datetime.datetime.now(LocalTZ()), '%Y-%m-%dT%H:%M:%S%z')
-	return '%s:%s' % (value[:-2], value[-2:])
+	def tzname(self, dt):
+		if daylight and localtime().tm_isdst:
+			return tzname[daylight]
+		return tzname[0]
 endglobal
 
-snippet date "Today's date"
-`!p snip.rv=date()`
+snippet date "date now"
+`!p snip.rv=datetime.now().strftime('%a %d %b %Y')`
+endsnippet
+
+snippet time "time now"
+`!p snip.rv=datetime.now().strftime('%H:%M:%S')`
+endsnippet
+
+snippet datetime "date and time now"
+`!p snip.rv=datetime.now(LocalTZ()).strftime('%a %d %b %Y %H:%M:%S %Z')`
 endsnippet
 
 snippet utc "UTC date time now" i
-`!p snip.rv=utc()`
-endsnippet
-
-snippet todo "TODO commment"
-`!p snip.rv=commentbefore()`TODO${1/.+/(/}$1${1/.+/)/}: $0`!p snip.rv=commentafter()`
-endsnippet
-
-snippet note "NOTE comment"
-`!p snip.rv=commentbefore()`NOTE: $0`!p snip.rv=commentafter()`
+`!p snip.rv=datetime.now(LocalTZ()).strftime('%Y-%m-%dT%H:%M:%S%z')`
 endsnippet
 
 snippet *x "GitHub style checkbox"