Here is a Semantic lexer based indent region function for formatting code in Emacs. It should work for Java and javascript. Feel free to use it if you like the output or customize it to suit your needs. *Screenshots:* https://lifeofpenguin.blogspot.com/2021/05/semantic-lexer-based-indent-region.html *Code:* semantic-indent-region Features - Removes redundant whitespace. Multiple new lines are replaced by two new lines to mark paragraph separation. - Uses tab for indent and fill-column width to wrap lines (hard wrap). Set values for *tab-width* and *fill-column* as per your preference. - Adds one indent for each level of nested code. - Splits line on semi-colon. Regards, Anand