- install astyle
- edit it, e.g.
nano ~/.astylerc
- 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: