6

I'd like to use emacs in some kind of batch mode to just render a file with syntax highlighting and exit. Specifically, I want to dump the fontified buffer with ANSI escape codes so that it shows up reasonably syntax-highlighted on my terminal. Is there any way to do this?

The ansi-lpr.el library seems to be kind of along the lines of what I want, but the output isn't colorified. I can't figure out how to get over that final hurdle — there are a lot of modules to digest ANSI escape codes into Emacs text properties (e.g. ansi-color.el) but I haven't found anything that does the reverse. If anyone can point me to something that does, I think I can piece together the rest.

Alternatively, I've seen some hacky approaches like this answer (using script and capturing the output) but in my experiments that approach has seemed unlikely to be fruitful — you get tons of undesirable control sequences mixed in with the highlighted text.

The overarching motivation here is to use emacs in a $LESSOPEN pipe to get syntax highlighting when I page files. In case you're going to say it, I've tried and "just page files in Emacs" is not acceptable for me.

2
  • What format do you want the colors in? ANSI color escapes seem to be what you want, but I'm not sure even that will work. Would Emacs buffer ==> HTML ==> text do? Commented Sep 21, 2014 at 4:59
  • @PythonNut: Yes, I want ANSI color codes. I haven't experimented with Emacs => HTML => text; I'd be skeptical that that could work well, but depending on the HTML output I suppose it might actually not be too bad. Commented Sep 24, 2014 at 3:26

1 Answer 1

6

I'm glad to announce a new package, e2ansi, that (hopefully) does what you asked for.

The package provides a command-line tool e2ansi-cat that starts Emacs in batch mode, opens files, syntax highlight them (using font-lock), and creates ANSI-colored versions of the syntax highlighted files.

You can integrate this into less by setting the following variables to, for example:

export "LESSOPEN=|emacs --batch -Q -l ~/.emacs -l bin/e2ansi-cat %s"
export "LESS=-R"
export MORE=$LESS

The end result looks like the following:

Example

You can vary the colors and attributes like bold, underline, and italics by using a suitable Emacs theme.

The e2ansi package is located at https://github.com/Lindydancer/e2ansi

Personal note

I would like to thank you for posting this question, it directly inspired me to write e2ansi.

Sign up to request clarification or add additional context in comments.

6 Comments

Brilliant! Thank you! I have custom colors set up in my .emacs using custom-set-faces calls. These don't seem to be used by e2ansi. Is that expected? Is there a way to get them to be applied? The --theme option sounds most relevant but I don't know anything about Emacs theming.
Yes, your personal color settings is expected to work. However, when using the --batch option, Emacs don't read your personal init file. Hence, you must explicitly load it using a -l option. In the example for less in the answer I have assumed that it is named ~/.emacs.
Ah, great! I've started giving it a try. First issue filed, we'll see if any more pop up ...
Very nice. Can you add a bit on what you had to do to handle batch mode, in particular wrt font-locking, for example? Connecting this answer to e.g. stackoverflow.com/questions/3591337/emacs-htmlize-in-batch-mode would be very nice, too.
@Clément, in batch mode, font-lock normally won't enable itself. I guess this makes sense if you only see font-lock as a way to get syntax highlighting in Emacs frames, which you won't need in batch mode. Hence, for this kind of application you will have to trick font-lock into believing it's not in batch mode. I do this by setting noninteractive to nil: (let ((noninteractive nil)) (font-lock-mode 1)). Secondly, font-lock mode doesn't highlight the entire buffer, only the visible parts, so you will have to do something like (font-lock-fontify-region (point-min) (point-max)).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.