* bug#14481: 24.3.50; Highlighting escape sequences
@ 2013-05-27 5:55 Dmitry Gutov
2013-05-27 14:16 ` Drew Adams
2013-05-28 20:45 ` Dmitry Gutov
0 siblings, 2 replies; 5+ messages in thread
From: Dmitry Gutov @ 2013-05-27 5:55 UTC (permalink / raw)
To: 14481
Most of the other text editors highlight stuff like \\, \t, \123 inside
string and regexp literals. For example, Vim and Sublime Text do.
In Emacs, we only have that in emacs-lisp-mode for grouping expressions,
I'm guessing because of their uncommon syntax.
Do we want it in other language modes? Here's some initial
implementation for ruby-mode and js-mode, using the face
font-lock-regexp-grouping-backslash, because it's the closest we have.
(defconst escape-sequence-re
"\\(\\\\\\(\\(?:[0-9]\\|x\\)\\(?:[0-9]\\(?:[0-9]\\)?\\)?\\|.\\)\\)"
"Regexp to match an escape sequence.
Currently handles octals (\\123), hexadecimals (\\x12) and
backslash followed by anything else.")
(font-lock-add-keywords
'ruby-mode
`((,escape-sequence-re
(1 (let ((term (nth 3 (syntax-ppss))))
(when (or (and (eq term ?')
(member (match-string 2) '("\\" "'")))
(memq term '(?\" ?/ ?\n t)))
'font-lock-regexp-grouping-backslash))
prepend)))
'append)
(font-lock-add-keywords
'js-mode
`((,escape-sequence-re
(1 (when (nth 3 (syntax-ppss))
'font-lock-regexp-grouping-backslash)
prepend)))
'append)
If yes, where should this code live? The regexp itself should be either
the same or quite similar for many modern languages, so I would prefer
to have it in one place.
^ permalink raw reply [flat|nested] 5+ messages in thread
* bug#14481: 24.3.50; Highlighting escape sequences
2013-05-27 5:55 bug#14481: 24.3.50; Highlighting escape sequences Dmitry Gutov
@ 2013-05-27 14:16 ` Drew Adams
2013-05-27 14:39 ` Dmitry Gutov
2013-05-28 20:45 ` Dmitry Gutov
1 sibling, 1 reply; 5+ messages in thread
From: Drew Adams @ 2013-05-27 14:16 UTC (permalink / raw)
To: Dmitry Gutov, 14481
> Most of the other text editors highlight stuff like \\, \t, \123 inside
> string and regexp literals. For example, Vim and Sublime Text do.
>
> In Emacs, we only have that in emacs-lisp-mode for grouping expressions,
> I'm guessing because of their uncommon syntax.
> Do we want it in other language modes? Here's some initial
> implementation for ruby-mode and js-mode, using the face
> font-lock-regexp-grouping-backslash, because it's the closest we have.
>
> (defconst escape-sequence-re
> "\\(\\\\\\(\\(?:[0-9]\\|x\\)\\(?:[0-9]\\(?:[0-9]\\)?\\)?\\|.\\)\\)"
> "Regexp to match an escape sequence.
> Currently handles octals (\\123), hexadecimals (\\x12) and
> backslash followed by anything else.")
Something like this would be useful, IMO.
But consider separating out the escape syntax for the various regexp
parts - e.g., the parts that match more than a single escape char (for
octal etc.). Using separate regexps (which can still be combined) for
the various parts lets you choose whether to highlight each char group
(e.g., highlight only hex escapes).
---
FWIW, library highlight-chars.el lets you selectively highlight sets of
chars. This works together with font-lock: you can choose whether
other font-lock highlighting overrides, is overridden by, or is merged
with this highlighting (applied after or before it).
You can specify chars to highlight in various ways: (1) individually
(any in a given string), (2) using ranges, (3) using character classes
(e.g. [:digit:]), and (4) using character sets (e.g. `iso-8859-1' or
`lao'). You can also specify sets of chars to be excluded from such
highlighting - IOW, specify a set by subtraction as well as by
addition.
You can thus, for example, highlight all characters in char set
`greek-iso8859-7' except `GREEK SMALL LETTER LAMBDA'. Or all
characters in class `[:space:]' (whitespace) except `tab'. Or
all Unicode characters in the range ?\u2190 through ?\u21ff
(mathematical arrows) except ?\u21b6, ?\u21b7, ?\u21ba, and
?\u21bb (curved arrows).
You can use any faces for the highlighting. You can choose whether
to add such highlighting automatically whenever font-lock mode is
on in a buffer (via `font-lock-mode-hook'). You can use
`(after-)change-major-mode-hook' to make the highlighting
mode-specific. Or you can use the highlighting only in certain
buffers.
http://www.emacswiki.org/emacs-en/download/highlight-chars.el
http://www.emacswiki.org/emacs/ShowWhiteSpace#toc2
^ permalink raw reply [flat|nested] 5+ messages in thread
* bug#14481: 24.3.50; Highlighting escape sequences
2013-05-27 14:16 ` Drew Adams
@ 2013-05-27 14:39 ` Dmitry Gutov
0 siblings, 0 replies; 5+ messages in thread
From: Dmitry Gutov @ 2013-05-27 14:39 UTC (permalink / raw)
To: Drew Adams; +Cc: 14481
On 27.05.2013 18:16, Drew Adams wrote:
> But consider separating out the escape syntax for the various regexp
> parts - e.g., the parts that match more than a single escape char (for
> octal etc.). Using separate regexps (which can still be combined) for
> the various parts lets you choose whether to highlight each char group
> (e.g., highlight only hex escapes).
I can't imagine why someone would want to highlight hex escapes, yet
keep octals and others unhighlighted. If we're going to support some
languages that only support a few escape sequences, then modifying this
regexp might be the way to go, yes, although writing separate, smaller
specialized regexp(s) might be better.
> FWIW, library highlight-chars.el lets you selectively highlight sets of
> chars. This works together with font-lock: you can choose whether
> other font-lock highlighting overrides, is overridden by, or is merged
> with this highlighting (applied after or before it).
>
> You can specify chars to highlight in various ways: (1) individually
> (any in a given string), (2) using ranges, (3) using character classes
> (e.g. [:digit:]), and (4) using character sets (e.g. `iso-8859-1' or
> `lao'). You can also specify sets of chars to be excluded from such
> highlighting - IOW, specify a set by subtraction as well as by
> addition.
That's nice, but we really need regexp here. And there's no point
relying on third-party library, the code is small as it is.
^ permalink raw reply [flat|nested] 5+ messages in thread
* bug#14481: 24.3.50; Highlighting escape sequences
2013-05-27 5:55 bug#14481: 24.3.50; Highlighting escape sequences Dmitry Gutov
2013-05-27 14:16 ` Drew Adams
@ 2013-05-28 20:45 ` Dmitry Gutov
2016-10-29 9:11 ` Dmitry Gutov
1 sibling, 1 reply; 5+ messages in thread
From: Dmitry Gutov @ 2013-05-28 20:45 UTC (permalink / raw)
To: 14481
Dmitry Gutov <dgutov@yandex.ru> writes:
> Do we want it in other language modes? <...>
> If yes, where should this code live? The regexp itself should be either
> the same or quite similar for many modern languages, so I would prefer
> to have it in one place.
So, no other replies? I can release it as a third-party minor mode, no
problem.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2016-10-29 9:11 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-27 5:55 bug#14481: 24.3.50; Highlighting escape sequences Dmitry Gutov
2013-05-27 14:16 ` Drew Adams
2013-05-27 14:39 ` Dmitry Gutov
2013-05-28 20:45 ` Dmitry Gutov
2016-10-29 9:11 ` Dmitry Gutov
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).