Click here to Skip to main content
Licence 
First Posted 29 Jul 2000
Views 44,493
Bookmarked 14 times

Align your code

By | 29 Jul 2000 | Article
Align multiple lines of assignments after the equal sign, for example
  • 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

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Scott Kirkwood

    Web Developer

    United States United States

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    QuestionTrying to run this in VC10 PinmemberMember 27890528:46 29 Feb '12  
    GeneralExcellent PinmemberPhilippe Lhoste0:50 14 Feb '02  
    GeneralDownload not working PinmemberAnonymous10:16 15 Nov '00  
    GeneralRe: Download not working PinmemberAnonymous10:25 15 Nov '00  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web03 | 2.5.120517.1 | Last Updated 30 Jul 2000
    Article Copyright 2000 by Scott Kirkwood
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid