'Use emacs To Process Code From Command Line

Is it possible to filter content from source files using emacs from command line. For instance, if you want to use emacs to filter out comments from code, can this be done just from command line?

My understanding is that emacs has excellent language support and ability to hide / show comments in the 'ui'.

For example: emacs --strip_comments --trim_trail_whitespace ./myfile.rs would would read the file ./myfile.rs, parse it using the internal language engine, strip out all trailing whitespaces and strip out all code comments.

Please don't provide any 'you can do this with grep or sed' answers. I want something that can handle most popular languages in a dependable and consistent way.



Solution 1:[1]

I decided to learn lisp and emacs to get this working. Took many different attempts, but I was able to get the functionality I was looking for.

I used HideOrIgnoreComments library and some code ideas from orgmode sources.

See my source repo for the lisp code in Github at JavaScriptDude / EmacsComStrip .

Usage: emacs --file=<path_to_source_code> --quick --batch --eval '(load "./comstrip.lisp")'

This tool will read in <source_code>, use emacs to strip comments out and the write to /tmp/<file_name>_comstrip

As long as Emacs has the language semantics files for the given input language, it should be able to strip out comments in a very dependable fashion.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Timothy C. Quinn