Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C++
Article

Align your code

Rate me:
Please Sign up or sign in to vote.
4.63/5 (6 votes)
29 Jul 2000 54.1K   505   14   4
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


    Written By
    Web Developer
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    QuestionTrying to run this in VC10 Pin
    Member 278905229-Feb-12 8:46
    Member 278905229-Feb-12 8:46 
    GeneralExcellent Pin
    Philippe Lhoste14-Feb-02 0:50
    Philippe Lhoste14-Feb-02 0:50 
    GeneralDownload not working Pin
    15-Nov-00 10:16
    suss15-Nov-00 10:16 
    GeneralRe: Download not working Pin
    15-Nov-00 10:25
    suss15-Nov-00 10:25 

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

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