The Code Project View our sponsorsClick here for Dundas Software's TCP/IP Development KitAdvertise on the CodeProject
Home >> Macros and Add-ins >> Macros

Align your code
By Scott Kirkwood

Align multiple lines of assignments after the equal sign, for example 
 VC 4-6, Win95-98, NT4, W2K
 Posted 30 Jul 2000
Articles by this author
Send to a friend
Printer friendly version
Lounge New Articles Sign in Forums Contribute
Broken links? Email us!
35 users have rated this article. result:
2.06 out of 5.

  • Download source files - 1 Kb
  • AlignNext.gif (12586 bytes)

    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.

    AlignNext2.gif (5995 bytes)

    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

    [Top] Sign in to vote for this article:     PoorExcellent  
    Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
     Keyword Filter
     View   Per page   Messages since
    New threadMessages 1 to 2 of 2 (Total: 2)First Prev Next Last
    Subject 
    Author 
    Date 
      Download not working
    Anonymous 16:16 15 Nov 00 
      Re: Download not working
    Anonymous 16:25 15 Nov 00 
    Last Visit: 12:00 Friday 1st January, 1999First Prev Next Last

    Home >> Macros and Add-ins >> Macros
    last updated 30 Jul 2000
    Article content copyright Scott Kirkwood, 2000
    everything else © CodeProject, 1999-2001.
    The Code Project View our sponsorsClick here for Whole Tomato Software - Home of Visual AssistAdvertise on the CodeProject