Skip to content

Instantly share code, notes, and snippets.

@AGenchev
Last active February 27, 2025 18:31
Show Gist options
  • Save AGenchev/a53ecf94ee7b0a910c10826e5d7417f9 to your computer and use it in GitHub Desktop.
Save AGenchev/a53ecf94ee7b0a910c10826e5d7417f9 to your computer and use it in GitHub Desktop.
Modified Horstmann style formatting (my Angel's) formatting using astyle (Lunux)

Modified Horstmann style formatting (my Angel's) formatting using astyle (Lunux)

  1. install astyle
  2. edit it, e.g. nano ~/.astylerc
  3. Write there the default configuration for Horstmann:
style=horstmann
indent=spaces=4
align-pointer=type
align-reference=type
pad-comma
break-closing-braces
max-code-length=180
add-braces
keep-one-line-blocks
keep-one-line-statements
mode=c

To achieve my Angel's formatting, we use sed. (The result looks good at least in Mousepad editor). To test the solution:

cat 'test_source.php' | astyle | sed -E 's/([{}])/ \1/g; s/\{ {3}/{\t/g; s/ {4}/\t/g' > test_result.php 

Based on this in your editor we can add a custom command. The following can be used as a filter, if the editor supports it:

bash -c "astyle | sed -E 's/([{}])/ \1/g; s/\{ {3}/{\t/g; s/ {4}/\t/g' 2>/dev/null; true"

This works in Geany. To "install" it go to Edit->Format->Send selection to->Set Cutom Commands and there add it as a custom command, set a name for it. If it is the first command Ctrl+1 runs it on selected text. Note: we convert whitespace to tabs, so in preferences/identation type you must set 'tabs'. But if you prefer to use spaces instead, the line is even shorter:

bash -c "astyle | sed -E 's/([{}])/ \1/g;' 2>/dev/null; true"

Enjoy the modified Horstmann style. I find it best for code readability.
In Mousepad (tuned tabsize=4), the code looks like this:
In Mousepad, the code looks like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment