From: Xah Lee
Subject: is there summary of template systems for emacs?
Date: 
Message-ID: <2103fd36-c5cd-4e8d-a74f-34697a369934@a26g2000prf.googlegroups.com>
there seems to be a lot elisp packages for defining templates. Has
anyone studied them and can give a comparison? Basically, i just need
a brief explanation of their syntax and feature.

the emacswiki page
http://www.emacswiki.org/emacs/CategoryTemplates
is very helpful in listing them but a clear summary and comparison is
lacking.

here's what i found so far.

• skeleton.el is probably the oldest. It is officially part of gnu
emacs with full info doc. But its info doc is rather verbose and hard
to read.

• tempo.el is also officially part of gnu emacs. But other than being
included, there seems to be no mentioning of it, and lacks any doc
other than the few paragraphs in source code and chaotic info on
emacswiki.

the above's template syntax are lisp code, so it requires lisp
knowledge to use. There are several more elisp syntax templates:
Tempo, tempoSnippets.

consider how the advancement of technology today (looking at other
IDEs, etc), i think template systems based on lisp syntax is rather
obsolete. There are several plain text based template systems, which i
think are more proper today for emacs going forward:

MsfAbbrev, Snippet, TemplatesMode, ElseMode, YaSnippetMode.

I don't yet know their pro and cons... one wonders why there are so
many. I certainly hope emacs would choose ONE to be bundled as THE
template system for emacs.

With some cursory reading (spent like 3 hours now), it seems YaSnippet
is the most modern and feature rich...

it seems that Snippet is written in 2005 spurred by the Mac OS's
TextMate editor. Then, the author of YaSnippet tried to improve on
that and created SmartSnippet, but further improvement results in
YaSnippet. Looking at YaSnippet site, it seems to have extensive
documentation and examples. (a whole website dedicated to it)

e.g.
http://pluskid.lifegoo.com/upload/project/yasnippet/doc/define_snippet.html

though i'm haven't yet found out how other text based template system
compares.

Is there anyone, or authors of one the above, who actually studied the
whole and can give a summary?

Anyone got more info to add?

I'm interested because recently i'm writing a lsl mode. The lang lsl
has over 600 hundreds functions for scripting in the virtual world
environment Second Life  ... Being a game engine scripting lang, it is
unlike typical programing lang. Its hundreds of functions each has
unusual parameters. So a template set for lsl lang would be very
helpful for my mode.

• Linden Scripting Language with Emacs
  http://xahlee.org/sl/ls-emacs.html

  Xah
∑ http://xahlee.org/

☄
From: Xah Lee
Subject: Re: is there summary of template systems for emacs?
Date: 
Message-ID: <632b660d-e866-4330-8c72-b9ea50e50f41@s9g2000prm.googlegroups.com>
2008-12-01

in this page:
http://pluskid.lifegoo.com/?p=6

is a article where the pluskid wrote about why he wrote “smart-
snippet.el” (which is the precursor of yasnippet.el). However, it is
in Chinese. The following is a quick English translation.

------------------------------------------

mart-snippet and smart-skeleton
May 21, 2007 – 8:58 pm

with Ruby on Rail becoming hot, TextMate suddenly also caught the
flame. However, without a Mac, one can't know how TextMate is like.
However, i've seen some video of TextMate the web, and one of the
feature seems very good. That's “snipet”.  Define some template, then
it'll expand at the right time, to reduce repeative typing. This is a
feature any non-trivial editor have. Same template expanding in
multiple places is also a frequently seen feature. But usually the
editor would pop up dialog prompting user to enter proper field. The
surprising aspect of TextMate is that it doesn't do dialog popup, but
let you enter text in the same field, and automatically update all
places of that field.

Updaing fields in multiple places in one shot, this is super cool! All
of a sudden, all major editors are trying to add this feature. Emacs
is no exception, and soon someone wrote “snippet.el”, so that the
TextMate's super cool feature is now in emacs.

“snippet.el” uses emacs's abbrev feature to implement this. Emacs's
abbrev feature is a bit different from other editors, it doesn't need
some keyboard shortcut to invoke (in other words, needing many
shortcuts to expand diff abbrevs). When you turn on abbrev-mode,
abbreviations will auto expand when you type space, punctuation or
return. Abbrev can be major mode specific, so that each major mode can
have different sets of abbrev. However, abbrev has some flaws.

1. because it is based on words, so that it can't define text that's
not just word. For example, you can't use it to define a template to
expand to “()”.

2. Even though each major mode can have its own abbrev, but sometimes
it is not flexible enough. For example, in c++-mode i define a abbrev
to expand “class” to its template. This is very convenient, but i
don't what a bunch of text suddenly appear whenever i type the word
“class”. There are usually 2 ways to solve this: manually, by using “C-
q SPACE” instead of just space. The other way is to change the abbrev,
such as “classx”, so that you type “classx” when you need it to
expand. “msf-abbrev.el” is modeled like this.

however, in reality, both methods are not that great, because emacs's
abreev is not context sensitive.

thus i wrote “smart-snippet.el” based on “snippet.el”. It makes the
feature more flexible, like the name, it's smarter. It will expand or
not expand abbrev depending on the surrounding text. Very convenient.

I use “snippet.el”'s engine for expansion, and i modified it ab it.
“snippet.el”'s template syntax is very simple:


• “$${field}”  defines a field. Same named field in source code will
all get updated. Use Tab and Shift Tab to move between fields.

• “$” is for final the cursor location placement.

• “$>” means indendation. Emacs typicall has very nice indentation
setup for each major mode.

And these symbols can be changed. If they conflict with some
language's syntax and making emacs swoon, you can change other symbol
so that it is less confusing. You can set a different symbol for
different major mode. For example, i have have the following def for
“if”:

if ($${cond})
{$>
$>$.
}$>

but in other case, for example inside a string or comment. And there
are some other lang such as perl, ruby, etc, the same keyword can have
different construct. For example, in ruby, “if” can be used like
these:

# one way
if cond
  do_something
end

# another way
do_something if cond

the “smartness” in “smart-snippet.el” can be demonstrated right here!

i put a video tutorial of this in the main page, showing the features
of “smart-snippetl.el”.

...

------------------------------

Sorry i got tired translating at this point. The rest half is a bit
detail about other feature, such as how a “(” can expand to “()” with
the cursor inside, and how to get the cursor outside.

The last sentence in the article goes: “after all, editor design is
deep! Letting programer code comfortably is no doubt a very important
topic.”. Wee!

 It is a very good read. Anyhowe, smart snippet is now superceded by
yasnippet, and yasnippet has huge documentation in english and forum.
So, enjoy.

  Xah
∑ http://xahlee.org/

☄