18

Per What is syntax highlighting and how does it work?, it would appear that adding

<!-- language: lang-el -->

prior to code blocks in Emacs should enable syntax highlighting. Unfortunately it does not yet appear to be enabled, or is missing something. As example, I added the above to the answer in https://emacs.stackexchange.com/a/81/104, and it is not highlighted. Is there a specific tag that is also required, and could we just presume that Elisp should be the default highlighting for the emacs community?

3 Answers 3

15

Syntax highlighting is activated on a site-by-site basis.

Should we have syntax highlighting for Emacs Lisp by default? I don't expect the presence of Emacs Lisp to be correlated with tags: any question can be answered by “put this little chunk of code in your .emacs”. However code blocks are sometimes used to present buffer contents (example).

Ideally there would be a heuristic: in the absence of a magic comment, highlight for Emacs Lisp if the code block starts with one of the characters `(;' (skipping leading whitespace), otherwise don't highlight. I don't know if this is possible with the current code; treat this answer as a feature request.

I had a go at extracting all code blocks from the site (questions and answers), classifying them according to their first non-whitespace character, and visually inspecting each category.

#        31     1 Lisp
'         3     all Lisp
(       342     all but 2 Lisp
;        30     all Lisp
[        14     none Lisp
`         4     all Lisp
other   199     1 Lisp

The Lisp snippet beginning with # is #(…) syntax. The “other” Lisp snippet begins with .... The two non-Lisp blocks beginning with ( are part of a LaTeX error log and a regular expression (not in Emacs syntax). The non-Lisp blocks are as you'd expect a mix of content with no coherent theme, mainly anything that one could edit (LaTeX, C, Org headers, ASCII art tables, etc.).

22 Lisp blocks start with whitespace. They're probably over-indented but I didn't investigate further.

Thus highlighting blocks as Lisp by default would work about 63% of the time. The heuristic above would work with a very high success rate: 0.5% false positive and 1% false negative (my original proposal of just (; would miss about 2% of the blocks to be highlighted).

Conclusion: we should have syntax highlighting. Preferably with the heuristic above, otherwise with Elisp syntax by default.

I haven't studied the correlation with tags. I don't expect the tags to give much information since just about any question could have a Lisp snippet in an answer.

14
  • This rule of thumb would catch probably 98% of all cases, so it seems a fine option if at all possible.
    – Malabarba
    Sep 24, 2014 at 8:27
  • I agree with that rule. In the presence of a language-specific tag, e.g. c++ or javascript, we could fall back to that language, though.
    – user227
    Sep 24, 2014 at 12:52
  • 1
    I suspect it may be wrong more often than you'd think. If someone has a code block of text they are editing, to show an example of what they are trying to do, it most likely won't be emacs lisp.
    – asmeurer
    Sep 24, 2014 at 19:44
  • @asmeurer Cide blocks in languages other than Lisp rarely start with ; or (. Of course it's not perfect but that's the definition of a heuristic. Sep 24, 2014 at 22:28
  • What does it take to have it activated for the site (be it with or without a default language)? Sep 30, 2014 at 15:31
  • @JonathanLeech-Pepin We demonstrate the need, then someone from the Stack Exchange staff turns it on. I don't know how obvious they consider this case. It could help if we had some stats regarding the proportion of code blocks that are Lisp code. Sep 30, 2014 at 15:33
  • 1
    @Gilles I mean turning it on at all. Currently it does not work even when the language tag is included. In this case since at least a decent portion of code-blocks will be code and not buffer examples, enabling the option would be useful. Sep 30, 2014 at 15:35
  • 1
    @JonathanLeech-Pepin Yes, I understood you the first time. We need to convince the SE staff to activate the feature. They need to add a line to the configuration file for the site or whatever it is they do. Sep 30, 2014 at 15:41
  • Now that we're public, should we open a feature request on this?
    – Malabarba
    Oct 9, 2014 at 15:30
  • @Malabarba This is already a feature request. I'll go and prod Jon. Oct 9, 2014 at 15:42
  • It has been a while. Any status updates?
    – nispio
    Oct 23, 2014 at 4:58
  • @Gilles do you have a link to that feature request? Also, it's interesting to note that there is syntax highlighting when using the Android SE app. Nov 15, 2014 at 19:10
  • @WilfredHughes The page that you're reading is the feature request. I've had no private response. If there's a difference between the Android app and the site, that's presumably a bug in the Android app. Nov 15, 2014 at 22:20
  • Another heuristic would be to include ^diff to match diffs. ... Except, diff isn't one of the supported formats!
    – Joe Corneli Mod
    May 27, 2015 at 21:16
8

Clearly there's no reason for syntax highlighting to be unavailable here. So I turned it on.

That said, it's currently off by default unless a language is explicitly specified. My gut feeling is that it'd make more sense to format all code as lisp by default, and force folks to override that when using code blocks for anything else - but after reviewing the numbers Gilles came up with in his answer, it doesn't appear this would be a clear win either.

So for now, use the language hints (you may have to re-save existing posts that used them, since they're evaluated when the post is saved). If, on down the line, this becomes too much of a burden... We can always turn it on everywhere by default.

4
  • 1
    Thanks a lot! I think turning on elisp by default would make perfect sense. The majority of our code blocks are certainly elisp. Plain text (or org mode) is also very common, but it's less than half for sure, and it wouldn't be terribly harmed by accidental elisp highlighting. Other languages are barely present at all.
    – Malabarba
    Dec 19, 2014 at 11:49
  • As a new user of this forum (but relatively long-time user of Emacs) I do think <!-- language: lang-el --> would be a sensible default.
    – Joe Corneli Mod
    May 27, 2015 at 20:44
  • This has worked fine, but at least today it seems to not work anymore.
    – Drew
    Apr 23, 2016 at 22:31
3

It seems that syntax highlighting simply doesn't work on emacs.stackexchange.com (I added the <-- language: foo --> blocks everytime):

C:

int main()
{
   printf("not working for c");
}

Python:

def foo():
    print "not working for python"

Ruby:

def foo
    print "not working for ruby"
end

Emacs-lisp:

(defun foo ()
  (message "not working for emacs-lisp"))

This is fairly annoying. When is it planned to enable it?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .