help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: is there summary of template systems for emacs?


From: Xah Lee
Subject: Re: is there summary of template systems for emacs?
Date: Mon, 1 Dec 2008 20:28:29 -0800 (PST)
User-agent: G2/1.0

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/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]