Semantic lexer based indent region function in Emacs



 

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.

Code: semantic-indent-region

 Features

  • Removes redundant whitespace. Multiple new lines are replaced by two new lines to mark paragraph separation.
  • Adds spaces around punctuation characters.
  • Uses tab for indent and fill-column width to wrap lines. Set values for tab-width and fill-column as per your preference.
  • Uses first token as the anchor point and aligns the code to match the nearest tab-stop. e.g. if tab-width is 4 character, and first token is on 1-3 column, code will be aligned to first column. However, if first token is on 4th column, then alignment is not changed.
  • Adds one indent for each level of nested code.
  • Splits line on semi-colon.

Comments

Popular posts from this blog

GNU Emacs as a Comic Book Reader

Data Visualization with GNU Emacs

Mozilla Readability in GNU Emacs