Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom expansions from templates? #76

Closed
frioux opened this Issue Jul 2, 2017 · 6 comments

Comments

Projects
None yet
2 participants
@frioux
Copy link
Contributor

frioux commented Jul 2, 2017

I use a plugin called UltiSnips in exactly one filetype in exactly one project, and the snippet is simply:

snippet fmatter
---
title: $1
date: ${2:`date +%Y-%m-%dT%H:%M:%S`}
tags: $3
guid: `perl -MData::GUID=guid_string -E'say guid_string()'`
---
$0

Honestly I don't use the numbers to jump from one place to another, but I do really appreciate the automatic GUID expansion, and if I could have that I might as well include the date. What do you think about, if a line is something like "..." it gets executed and inserted into the template at expansion time?

@tpope

This comment has been minimized.

Copy link
Owner

tpope commented Jul 2, 2017

What's your answer to this?

{"*": {"template": ["`rm -rf *`"]}}
@frioux

This comment has been minimized.

Copy link
Contributor Author

frioux commented Jul 2, 2017

You could prompt with the command to run, with a default of no maybe?

@frioux

This comment has been minimized.

Copy link
Contributor Author

frioux commented Jul 3, 2017

A couple other options: show the entire projectionist file at some point, storing whether or not template commands should be run, only re-prompting if the hash of the config changes.

Similarly you could do the same except at the template or even template command level, prompting once per command or once per command/checkout path.

I suspect these options are all more complicated than you are interested in implementing/supporting, but this could be made as secure as is reasonable.

@frioux

This comment has been minimized.

Copy link
Contributor Author

frioux commented Oct 12, 2017

I had another idea that I think is a little more "vimmy": what if you just added an autocommand that fired after the template is generated? I will try to hook into the existing BufReadPost autocmd but I suspect that is not specific enough.

@frioux

This comment has been minimized.

Copy link
Contributor Author

frioux commented Oct 12, 2017

Aight so I got this to work with a little hack. I think the autocommand would be more elegant and generally useful, but this works fine:

{
   "content/posts/*.md": {
      "type": "post",
      "template": [
         "TPLTPLTPL",
         "---",
         "title: ",
         "date: ~~CURDATE~~",
         "tags: ",
         "guid: ~~GUID~~",
         "---",
         "",
         "",
         "<!--more-->"
      ]
   }
}
function! ExpandTemplate()
   if getline(1) == 'TPLTPLTPL'
      :%s/\~\~CURDATE\~\~/\=systemlist("date +%FT%T%z")[0]/ge
      :%s/\~\~GUID\~\~/\=systemlist("perl -MData::GUID=guid_string -E'say guid_string()'")[0]/ge
      1g/TPLTPLTPL/d
   endif
endfunction

" ...
   au BufReadPost * call ExpandTemplate()
" ...

frioux added a commit to frioux/vim-projectionist that referenced this issue Oct 16, 2017

frioux added a commit to frioux/vim-projectionist that referenced this issue Oct 20, 2017

@tpope

This comment has been minimized.

Copy link
Owner

tpope commented Jun 24, 2018

I guess I'm hesitant to commit to it as an official documented behavior. If you drop the documentation for now I'll merge.

Edit: pretend I made this comment on the PR.

frioux added a commit to frioux/vim-projectionist that referenced this issue Jun 25, 2018

@tpope tpope closed this in #81 Jun 25, 2018

tpope added a commit that referenced this issue Jun 25, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.