[Top][All Lists]

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

Re: Writing syntax-propertize-function for strings in code in strings, e

From: Dmitry Gutov
Subject: Re: Writing syntax-propertize-function for strings in code in strings, etc
Date: Sun, 09 Sep 2012 04:13:24 +0400
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0

On 08.09.2012 23:31, Stefan Monnier wrote:
Sublime Text handles these aspects rather excellently, and even
highlights the code inside as code, not string contents:
Is there a proper way to do so in Emacs?

Currently, it's pretty difficult for Emacs to handle it like in the
picture above.

My first idea was, when propertizing interpolation, to see what kind of
string we're inside, and apply the appropriate syntax to the enclosing
braces, thus splitting the literal in two.  But (a) string quotes class
doesn't work that way (text characters on both ends of a literal must
be the same), (b) if we're inside a percent literal (syntax class:
generic string), and the literal spans several lines, we need to be able
to jump to its real beginning position from its end, but with this
approach (nth 8 (syntax-ppss)) will just return the beginning of the
last piece.  Saving buffer positions to text properties looks not very
reliable, since the respective text may be deleted and re-inserted.


I think the better approach is to extend syntax.c with such a notion of
"syntax within strings".  This could hopefully be used for:
- Strings within strings (e.g. Postscript nested strings).
- Comments within strings (I think some regexps allow comments).
- Code within strings (as here and in shell scripts).
I'm not sure what that would look like concretely.  Maybe a new string
quote syntax which specifies a syntax-table to use within the string?

In the current case, the syntactic meanings of characters are the same as outside the string, except a certain character should end the "inner" region and return the state after it to "inside string" (*).

Maybe just two new classes, similar to open and close parenthesis (to support nesting)?

* Preferably, only when it's not inside an "inner" string or comment.
At least, that's how it works in Ruby 1.9:

irb(main):011:0> %(#{"})"})
=> "})"

irb(main):013:0> %(#{#})
irb(main):014:0> })
=> ""

The above examples also won't work with current percent literals handling, but that's less important, I think.

parse-partial-sexp will probably need to keep some sort of stack for string-related data, so that when we're after the end of an "inner" region, we could find out what is the outer string's type and where it started.
And when inside the inner region, the position of its start.
Use the 9th state element and bump the total number to 10?

reply via email to

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