Using emacs in batch mode to dump a file with syntax highlighting? - emacs

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.

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:
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.

Related

Formatted text in the command window

I know of the cprintf Undocumented Matlab way of changing the color and other font properties in the command window but I also saw this symbols in plots. This shows that Matlab supports TeX markup in plots at least. I played with it for a while and found it very useful. So much so that I wanted to find a way to include this in the command window.
I first tried sprintf('\color{red} Something\n') and was rewarded with an error that \c is not a recognized escape sequence. Google was no help either.
This is a way to use the some of the other formatting options in the command window?
The command window doesn't support TeX. Sorry. The TeX support is part of the routines that generate the figures, not the code that displays the command window.
In essence, the command window is, by modern standards, a pretty boring terminal emulator. There's not much you can do with it.
If you're looking for something to do math in that generates fancy notebooks on the fly that combine the commands you type and their results in a nice-to-read, modern way, you might just want to avoid Matlab and have a look at Jupyter (formerly IPython) Notebooks, which of course support MathJax (and thus, LaTeX math syntax): https://try.jupyter.org/

Best way to customize auto-formatting/auto-indenting in vim for Common Lisp

I would like to know the best way to customize auto-formatting/auto-indenting in vim for Common Lisp.
Auto-formatting (I usually do this by typing '==' in command mode per line) works very well for the base lisp language in vim (e.g., defmacro, defun, lambda, if, with-output-to-string), but any time that new language constructs are defined (e.g., using macros), I find that the formatting for the new construct is often not what I'd like it to be.
For example, here's how vim formats 'when (standard lisp construct) and 'awhen (commonly-used anaphoric version of 'when; not part of lisp standard)
(when 'this
(process 'this))
(awhen 'this
(process it))
I would like 'awhen to auto-format like 'when. Any ideas how I can do this?
Edit: Thanks Gilligan and Tamas for the Slimv recommendation. As a test, I downloaded MacVim (will need this working with terminal vim, but that's a different problem) and slimv, rsynched the slimv download into ~/.vim, launched MacVim, and loaded a .lisp file.
I then started up the lisp server (done through a GUI with MacVim), which loaded up my default lisp executable and core file.
And* since my core file is already loaded with the language extensions that I commonly use (awhen being one of them), awhen formatted correctly right out of the box.
I really like this solution. Instead of [1] having to learn how to tell vim to indent particular functions properly, and [2] writing the code that does this explicitly for each language extension that I define, and [3] updating that code every time I add a new language construct. Instead I leverage slimv to do the formatting for me. And slimv can 'learn' new language extensions, as long as those macros are already loaded into the lisp core that the server session is using. Pretty slick!
I have found that this works well for a particular class of language extensions. Usually ones defined as a macro, using the &body keyword. This seems to 'do the right thing' most of the time, but there are macros I use that still don't properly auto-format. Although I'd say that this is more likely to be an issue with how the macro is written (non-standard language extension) than anything else.
So, this solution works well for me for most cases, and I didn't have to code (and maintain) anything. Great stuff!
This might not be a direct answer to your question but I strongly suggest that you install
the slimv plugin: http://www.vim.org/scripts/script.php?script_id=2531
Its a great plugin which integrates SLIME functionality into vim and besides many other things it also comes with an improved indentation for clisp&clojure. It won't indent awhen the way you want though.
For those who are looking for this topic and don't want to run Slimv, because they aren't working with Common Lisp or other reasons, here is the scoop.
Vim's Lisp indentation is not like that for other languages; it has a special "Lisp mode". This mode is turned on by
:set lisp
which is done automatically for .lisp files. Lisp mode isn't a Vim invention; classic Vi implementations have a Lisp mode turned on with :set lisp. (It's not described by POSIX, unfortunately).
Vim's Lisp mode has a simple mechanism for recognizing forms that require operator-style indentation: namely, there is a parameter called lispwords which holds a comma-separated list of identifiers.
You can prove to yourself that this is the identifier list which is used, even when you're editing a Common Lisp .lisp file with syntax highlighting and all. Simply do :set listwords[TAB] and edit the list to remove something from it, such as defun. Then try to reindent a defun: you will see the function-style indentation now instead of the operator-style.
The syntax highlighting support for Common Lisp is separate from Lisp mode's lispwords parameter; it has its own list of identifiers. For example, in Vim 7.3 if you enter this:
(symbol-macrolet ((foo bar))
you get indented out to here!)
This is in spite of the fact that symbol-macrolet is recognized and colored. Why? It's because symbol-macrolet does not appear in the rather scanty lispwords list, whereas it does appear in the lisp.vim syntax highlighting definition file.
The upshot is that you can cob together some script which scans your directory of .lisp files for macros and generates a set lispwords=... command that is placed into a directory .vimrc.
Or if you are working on a custom Lisp dialect, you can just make its Vim syntax highlighting file customize lispwords when it loads.
Here is an implementation oversight: the lispwords option has no local value; you cannot use setlocal lispwords ... to give it a buffer-specific value. In other words, it appears that (at least in the Vim 7.3 I'm using under Ubuntu) you can't have two or more buffers open holding different dialects of Lisp with different identifiers for indentation. The default contents of lispwords contains a smattering of Lisp and Scheme symbols to try to be a kind of "one size almost fits all" solution.
If you filetype is 'lisp' then I think you need to add indenting rules for your special case in the 'lisp.vim' file in the '/vim7x/indent' directory. You can find a bit more info in help at :h indent-expr and :h indentexpr.
Someone may be able to tell you better, but I believe the default lisp.vim indent file basically does nothing because the built-in function lispindent() is used to get indent values. You will want to:
(1) set function used to get indent values (i.e., indentexpr) to a function in your own indent/lisp.vim file, e.g., GetLispIndent().
(2) in your your GetLispIndent() function you will use lispindent() to get indent values to return for all lines except your special case. See other languages' indent files and read the docs to get an idea for how indentexpr works, e.g, java.vim.
#Kaz's answer is completely correct, but they don't go all the way to answering the original question. Vim's lispwords config string is a comma-delimited list of words; when any of the words in lispwords is found at the beginning of an S-expression, Vim will change the way that S-expression is indented. In other words, it defines the "standard lisp constructs" to which the OP refers. If you view your current lispwords config with :set lispwords, you'll see "when" is included, but "awhen" is not, resulting in the following indentation:
(when 'this
(process 'this))
(awhen 'this
(process it))
To fix this, simply add "awhen" to the config string somewhere in your config, like so:
set lispwords+=awhen,
The trailing comma isn't strictly necessary, but the default value includes it, and is probably wise in case you or a plugin modifier elsewhere. That would turn the original formatting into this:
(when 'this
(process 'this))
(awhen 'this
(process it))
(Tested on my current installation of Vim 9.0)
Note that, as #Kaz points out, this config string is universal, so if you have different dialects of lisp their indentation will all be controlled by this setting. Vim is pretty good about auto-detecting lisps, but you may need so :set lisp if it doesn't recognize your filetype or dialect.

Pipe less to Emacs

When viewing piped output to Less, sometimes I'd like to be able to view it in Emacs in order to get syntax highlighting and use emacs commands for searching, marking, copying, etc.
I see that Less has a v command that can be used to open the currently viewed file in $EDITOR. Unfortunately this doesn't work when viewing piped input.
Also, I don't know how to get Emacs to display stdin as a read-only document.
So, is it possible to set up Less with something like v but that pumps the current buffer into Emacs as a read-only file?
Thanks.
If you scroll down in http://www.emacswiki.org/emacs/GnuClient, you'll come to a section titled "Piping data to an Emacs buffer" which may be relevant. Or you can hack up a solution involving emacsclient and temporary files. (link dead).
I found another variant while looking for a duplicate of the dead link: Piping to an emacs buffer with emacsclient which points at code stored on github.
I've formalized the solution here: github e-sink

How do I fully-justify latex code on EMACS

I want to fully-justify latex code on EMACS so that my latex code will look better. For example, I remember my advisor sending me latex in fully justified way like this:
In ~\cite{Hummel2004}, authors described an approach for harvesting
software components from the Web. The basic idea is to use the Web as
the underlying repository, and to utilize standard search engines,
such as Google, as the means of discovering appropriate software
assets. Other researchers have crawled through Internet publicly
available CVS repositories to build their own source code search
engines (e.g., SPARS-J)~\cite{Matsushita2005}.
I suppose that his column-width is set to 70 columns.
Could someone give me a hint?
The standard fill.el package includes the command justify-current-line which is part of what you need. From the function help:
Do some kind of justification on this line.
Normally does full justification: adds spaces to the line to make it end at
the column given by `current-fill-column'.
Optional first argument how specifies alternate type of justification:
it can be `left', `right', `full', `center', or `none'.
If how is t, will justify however the `current-justification' function says to
And other posters have already given you the magicall invokation:
M-x set-justification
As a philosophical side note, the point of fixed-wdith text justification is to fake real typography on a inflexible output device. So applying it to LaTeX source seems a little odd to me. Moreover, I have been using the "one sentence to a line" approach to LaTeX documents for some months now, and find that it really does improves both the editability and the source-control behavior of LaTeX, so I would recommend against doing this.
If you select the region, and then press Ctrl-u M-x fill-region you get "full justification".
M-x set-justification-full
Use Refill mode afterwards to not have to run the command again after typing.
To get line wrap in the file itself (as opposed to something like longlines-mode that does not alter the structure of the file), I use auto-fill-mode, which automatically applies M-q (fill-paragraph) to each paragraph. For example, I use auto-fill-mode in mail-mode. You could do something similar with your LaTeX mode with a hook like this:
(add-hook 'TeX-mode-hook 'turn-on-auto-fill)
Assuming your TeX mode's hook is TeX-mode-hook.

How to ignore comments in the LaTeX file with ispell (within Emacs if possible)

I'm writing a text with Latex in English but written my comments in Finnish. When I'm running the spell checking with ispell, I got to run through all the comments. Is there a handy way to skip the comments with the ispell? If that could be done with emacs, that would be double handy =)
One way would be to run the ispell within console and process the input with sed, for example, but I'd like to have my changes straight on the file...
(setq ispell-check-comments nil)
You'd have to dig in the code a bit, but when you spell-check a file, it's running the detex tool to strip out the TeX code. You should be able to modify the pipeline to have a sed or perl script strip the comment lines.
I kind of vaguely think AuC-TeX makes this configurable, but 30 seconds looking didn't reveal it.
Adding a modern answer to this old question, because I also had the problem:
Just use aspell, which does this out of the box.