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

Already on GitHub? Sign in to your account

elisp bindings for creating templates programatically #295

Closed
monsanto opened this Issue Aug 25, 2012 · 3 comments

Comments

Projects
None yet
2 participants
Contributor

monsanto commented Aug 25, 2012

There are a number of auto-complete sources that now use yas to fill in things like argument lists on completion (see auto-complete-clang or my own auto-complete-auctex). It would be handy if there were elisp bindings to creating templates programmatically so we don't have to worry about quoting errors, etc.

Some examples of what I had in mind:

\begin{${1:enumerate}}
   $0
\end{$1}

=>

(yas-template "\begin{" (tab-stop $1 "enumerate") "}\n"
              "  " exit "\n"
              "\end{" (eval $1) "}")



${1:$(make-string (string-width text) ?\=)}
${1:Title}            
${1:$(make-string (string-width text) ?\=)}

=>

(yas-template (eval (make-string (string-width $1 ?\=))) "\n"
              (tab-stop $1 "Title") "\n"
              (eval (make-string (string-width $1 ?\=))))

The form (yas-template) takes an arbitrary number of arguments, where each argument can be:

  • "a string": a literal string which is outputted in the template
  • (tab-stop name [default] [transformation]): binds "name" to the value at this tab-stop. default is a string or another lisp template. transformation is a lisp form with "name" bound.
  • exit: the exit tab stop.
  • (eval lisp-form): works as a mirror/transformer combo. All names bound by tab-stops are available.

For the short term, one could just write a function that translates an elisp version of the template to the yas text version, but I think it would be cleaner if yas adopted an elisp representation internally and compiled text templates to elisp (might be faster too).

Any and all feedback welcome!

Owner

joaotavora commented Aug 26, 2012

Hmmm, I see where you're coming from, compiling text templates to elisp sounds very interesting, with even more uses than the ones you describe. It could, for example, help implement #279.

The current yasnippet implementation is much more conventional. A yas--template object is built describing the relations between the fields, mirrors, transformations, etc and then a fixed base of code iterates it. I see something like a macro that takes one of these representations and expands to the elisp code that dinamically adds fields and mirrors to the yas--template object. A bit like http://weitz.de/cl-who/ (see its CL-WHO::TREE-TO-COMMANDS). Maybe it wouldn't even need the yas--template object.

This new approach would be revolutionary, but is also very ambitious and I have absolutely no time to implement it. But give it a shot, implement some prototype, then tweak it to make sure it passes the existing tests in yasnippet-tests.el.

Contributor

monsanto commented Aug 26, 2012

I probably can't get to this for a month, but then I'll have some free time. I think it would be fairly easy to write elisp->text, but much more ambitious to do text->elisp and use elisp natively.

I'll need FSF papers to do this, can you help me out on that?

Owner

joaotavora commented Aug 27, 2012

I'm sorry but I know little about FSF's paper policy, but I would say the GPL3 allows you to perform whatever changes you want to the code as long as you distribute it forward.

Try out a prototype in your github fork then we'll worry about the FSF papers.

@monsanto monsanto closed this Mar 18, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment