Download source files - 1 Kb
Introduction
I always thought that aligning code to make it look "pretty" was a big waste
of time. Later I realized that aligning code will sometimes help you find
bugs simply by inspection. You can more easily see that one line doesn't
fit the pattern of the others when the code is aligned.
Another thing that bothered me about aligning code was that you often had to
go back and re-align them later on when a variable name was changed, for
instance. What's worse is that I found myself sometimes not changing a variable
name to something more appropriate because it would mean re-aligning
everything!
This macros makes aligning you code a lot easier. Put you cursor where you
want the other line to be aligned (by adding or removing spaces). One
macro goes downwards (AlignNextLine
) and the other macro goes backwards
(AlignPrevLine
) through the file.
If a line has too many spaces the extra spaces will be removed to make it
align correctly. Also, if a line is already aligned it does not changed it, the
cursor just moves up or down.
The cursor should be next to a punctuation character. A comma or an equal
sign is an example of a punctuation character as far as the macro is concerned.
The spaces will be added either to the left or the right of the punctuation
charater depending on the cursor position. The function should be fairly
harmless, only adding and deleting spaces. The undo buffer will have two events
- 1) delete all blanks around cursor and 2) insert the appropriate number of
spaces.
Sometimes the macro gets confused when there are lots of punctuation
characters around. My work around for this is to align a smaller amount first,
then go over the lines again aligning further out the second time. Not ideal,
but then I don't want to spend more time on these macros then it would take to
align all the code that I'll ever write!
This macro is not specific to C++ it should work on any text file.
Some details of the code:
The macro subroutines AlignNextLine
and
AlignPrevLine
are one line routines which call the function
AlignFunction
with 1 or -1 for down and up, respectively. This
function in turn uses FindClosest
and the IsPunt
function to do some of the work.
As a side note, I map AlignNextLine and AlignPrevLine to Ctrl+N and Ctrl+Shift+N,
respectively ("N" for Next). This will override Ctrl+N's default: create a
new file, which I hardy ever use.
Also, for these and other macros and code you can visit my site at
http://www.forusers.com/fordevelopers