A Editor Feature for Extending Selection based on Language Syntax

Xah Lee xah at xahlee.org
Mon Aug 21 00:49:45 EDT 2006


can anyone give me a guide about writing a short elisp function? (for
non-emacs readers, this message will describe a editor feature i think
will be very beneficial to spread this concept.)

i want to write a function such that, when run, highlight a region
between the nearest left and right delimiters. Delimiters are any of
parenthesis, square brackets, or single and double quotes etc. When the
function is run again, it extends the selection to the next enclosing
delimiters.

So, in this way, a user can repeatedly press a keyboard shortcut and
extend the selection.

This is feature of BBEdit/TextWrangler on the Mac, which extend
selection to the nearest outer parenthesis. This is also a feature of
the Mathematica editor, which actually extend selection to the nearest
syntactical unit in the language, not just paired delimiters.

What i wanted this for is mostly in editing HTML/XML, where one press
can select the content, another press will include the enclosing tags,
another press extends the selection to the next outer content, and
another press include that tags too, and so on.

I'm a elisp newbie. Here's a simple code i have so far:

(defun d ()
	"extend selection to nearest enclosing delimiters"
	(interactive)
  (skip-chars-backward "^<>()“”{}[]")
  (push-mark)
  (skip-chars-forward "^<>()“”{}[]")
  (exchange-point-and-mark 1)
)

... i think i have quite a lot to go... I think this would be a great
feature for any mode, where the a keypress will highlight more
syntactical units in any language's mode. For example, suppose in
C-like language:

function f (arg1, arg2) {
line1;
line2;
}

if the cursor is at arg1, then first press will highlight the content
of the args, another press includes the parens, another press will
include the whole function. If the cursor is at line1, then it selects
that word in the line, then the line, then the whole function def body,
then including {}, then the whole function... etc in many languages.

For a xml language example, suppose we have this RSS/Atom example:

<entry>
  <title>Gulliver's Travels</title>
  <id>tag:xahlee.org,2006-08-21:030437</id>
  <updated>2006-08-20T20:04:41-07:00</updated>
  <summary>Annotated a chapter of Gulliver's Travels</summary>
  <link rel="alternate" href="../p/Gullivers_Travels/gt3ch05.html"/>
</entry>

If the cursor is inside a tag's enclosing content, say, on the T in
Gulliver's Travels inside the <title> tag, then the repeated extension
is obvious. But however, suppose the cursor is at t in the
“alternate” inside the “link” tag, then it would first select
the whole “alternate” word, then the whole “rel="alternate"”,
then the whole link tag, then the whole content of the entry tag, then
including the “<entry>” tags itself.

(in short, the selection extends according to the language's syntax
tree)

  Xah
  xah at xahlee.orghttp://xahlee.org/




More information about the Python-list mailing list