I’m not entirely sure how to fix the code presented, but I do have my own variation on this theme where I have a header that I replace a field in with the current time.
class vhdlModeInsertHeaderCommand(sublime_plugin.TextCommand):
"""
This command is used to insert a predefined header into the
current text file.
"""
def run(self, edit):
# Assigning this to a string to keep command shorter later.
template = "Packages/vhdl_mode/Snippets/vhdl-header.sublime-snippet"
# Getting a few fields from the settings file
settings = sublime.load_settings('vhdl_mode.sublime-settings')
# Looking for a name, first the buffer name, then the file name,
# then finally a default value.
buffname = self.view.name()
longname = self.view.file_name()
if buffname:
filename = buffname
elif longname:
# Convert Windows slashes to Unix slashes (if any)
longname = re.sub(r'\\', '/', longname)
namechunks = longname.split('/')
filename = namechunks[len(namechunks)-1]
else:
filename = '<filename>'
author = settings.get("vhdl-user", "<user>")
company = settings.get("vhdl-company", "<company>")
date = time.ctime(time.time())
year = time.strftime("%Y",time.localtime())
# Moving insertion point to the beginning of the file.
bof = self.view.text_point(0,0)
self.view.sel().clear()
self.view.sel().add(sublime.Region(bof))
self.view.show(bof)
# Inserting template/snippet
self.view.run_command("insert_snippet",
{
"name" : template,
"FILENAME": filename,
"AUTHOR" : author,
"COMPANY" : company,
"CDATE" : date,
"MDATE" : date,
"YEAR" : year
})
print('vhdl-mode: Inserted header template.')
And then the snippet I use with this is:
<snippet>
<!--
Current fields supported:
FILENAME : Will fill in the current buffer name if found, if not
found, will fill in the file name, and if that's not
found will fill in a default value.
AUTHOR : This uses the vhdl-author setting in the settings file.
COMPANY : This uses the vhdl-company setting in the settings file.
CDATE : Automatically filled in when the header is created.
MDATE : Automatically filled in when the header is created and
updated when file is saved.
YEAR : Automatically filled in when the header is created.
Numeric fields are tabbed between when snippet is inserted.
TODO: Take project name, platform, and standard from project
file.
Edit header to taste and need.
-->
<content><![CDATA[
-------------------------------------------------------------------------------
-- Title : ${1:<Title Block>}
-- Project : ${2:<Project Name>}
-------------------------------------------------------------------------------
-- File : ${FILENAME}
-- Author : ${AUTHOR}
-- Company : ${COMPANY}
-- Created : ${CDATE}
-- Last update : ${MDATE}
-- Platform : Generic (No device specific behavior specified.)
-- Standard : VHDL'93, Math Packages
-------------------------------------------------------------------------------
-- Copyright (c) ${YEAR} ${COMPANY}
--
-- This document contains controlled technology or technical data under the
-- jurisdiction of the Export Administration Regulations (EAR), 15 CFR 730-774.
-- It cannot be transferred to any foreign third party without the specific
-- prior approval of the U.S. Department of Commerce Bureau of Industry and
-- Security (BIS). Violations of these regulations are punishable by fine,
-- imprisonment, or both.
-------------------------------------------------------------------------------
-- Description: $4
-------------------------------------------------------------------------------
-- Revisions: Revisions and commentary regarding such are controlled by
-- the revision control system in use (Rational Team Concert / Jazz). That
-- tool should be consulted on revision history.
-------------------------------------------------------------------------------
$0
]]></content>
<tabTrigger>header</tabTrigger>
<scope>source.vhdl</scope>
</snippet>
Hope that helps. It’s probably slightly more elaborate than desired, but maybe it’s got something useful in it.