# Fields in templates without resorting to commented-out lists (aka are drop-down options possible?)

I do a lot of work in plain TeX and XeTeX, editing the documents in vim, and have a variety of templates which I use to make the process more efficient. As you might expect, these use quite a lot of variables which are defined once and then reused.

At the moment, to save typing, I put the most common values for each variable into the template, using the TeX comment delineator (a “%”) to control which one is selected. For example, the template might say:

\def\location{%London
%Paris
%New York
}


Then, when I am working on a document, I might de-comment the relevant line to define \location as “Paris”:

\def\location{%London
Paris
%New York
}


Whilst this works, it annoys me because I find having one option per line makes the templates unnecessarily long and hard to work my way through. I also can’t re-use the list of options when more than one variable might have a value chosen from the same list (e.g. in the example above, for “\newlocation”, I’d have to copy the list) and it just feels inefficient and the Wrong Way.

In an ideal world, I was wondering if there could be something like a drop-down list of options which I could call up and select. However, I have had a look at vim’s default options, and many of the templates / skeleton / snippet plugins and haven’t really been able to see anything that is obviously suitable for something as simple as this.

I am sure this is just me missing the obvious and was therefore wondering if someone could help point me in the right direction?

Thanks!

Jacob

I'm not sure to completely understand your problem.

All snippet/template/skeleton plugins have placeholders that can be replaced at the last moment. Also, many (/most?) can have snippet-variables. For instance, in mu-template I could write

MuT: let s:cities = ['Paris', 'NY']
MuT: let s:city1 = lh#ui#which('lh#ui#confirm', 'Chose one city', s:cities)
MuT: let s:city2 = lh#ui#which('lh#ui#confirm', 'Chose another city', s:cities)
\def\location{%
<+s:city1+>%
}
\def\newlocation{%
<+s:city2+>%
}


If you do many expansions at once (once again possible as a template can include another template and inject parameters or share variables), the s:city1 variable will be shared among all snippets.

In the following, instead, I centralize/factorize the list of possible locations in one template file. We could use autoload functions instead -- but this time we need to reload the autoload plugin every time we change the function, while it's automatic with templates. I'm doing similar and more complex thing in lh-cpp to generate class definition given a semantic: the class generation template is a big and complex procedure, the end-snippets instantiate/include the procedure with specialized parameters.

VimL:" a first common template file: tex/internals/common-def.template
MuT: let s:cities = [ 'London', 'Paris', 'NY' ]

VimL: " a leaf snippet file: tex/location.template
VimL: call s:include('common-def', 'tex/internals')
MuT: let s:city1 = lh#ui#which('lh#ui#confirm', 'Chose one city', s:cities)
\def\location{%
<+s:city1+>%
}

VimL: " another leaf snippet file: tex/newlocation.template
VimL: call s:include('common-def', 'tex/internals')
MuT: let s:city2 = lh#ui#which('lh#ui#confirm', 'Chose a new city', s:cities)
\def\newlocation{%
<+s:city2+>%
}


However, once expanded, you won't be able to change all its occurrences to something new. But there, I suspect this is not what you want because you could use the TeX-macro \location.

In a pure placeholder worlds, if you have a very big snippet (I'm not sure many other plugins support snippet inclusion), if you change a named placeholder somewhere, it'll be changed everywhere -- until the plugin has determined that the expansion is terminated... BTW: most snippet plugin also support computed stuff and calls to confirm() & co.

NB: lh#ui#confirm() wraps confirm() or a pure textual equivalent. lh#ui#which() returns the value selected instead of the index of the value selected. They are defined here.

• Thanks very much for this. This was the steer that I think I needed! Jacob – Jacob Aug 10 '17 at 19:46