Community
    • Login

    Right Justifying Paragraphs

    Scheduled Pinned Locked Moved General Discussion
    24 Posts 6 Posters 8.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • hoji afzalH
      hoji afzal
      last edited by

      I wish Notepad++ had this feature.
      CodeWright used to have it and was very useful in organizing code.

      This is what it could do:

      1. Suppose you wrote these code lines:
        int argument1 = 1;
        int multiplier = 100;
        double weight = 200.345;

      2. Using this feature you end up with this:
        int argument1 = 1;
        int multiplier = 100;
        double weight = 200.345;

      To do it:
      a. Select the entire paragraph and use right justify from the context menu.
      b. Enter random number of spaces just before the = signs.
      c. Select the paragraph again and this time left justify it.
      d. Only select the = sign and every to the right of it and select left justify.
      that’s all

      1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones
        last edited by PeterJones

        Unfortunately, you didn’t Markdown your your example data quite right (you need to keep a blank line between the outline row and the indented data; also, you would have to have double the indentation to make it render as embedded text to account for the first level of indenting for outline, and the second for preformatted-text). You can see “exactly” how it will render by looking at the preview pane to the right of the edit frame. (“exactly” in quotes, because it doesn’t render @-mentions, and I’ve seen it occasionally stop rendering right on large posts). More on formatting at this help-with-markdown post

        Regarding your problem, if I understand correctly, you don’t actually want to right-justfiy – what it sounds like is that you want to align a second column based on the = character. ie, changing:

        int argument1 = 1;
        int multiplier = 100;
        double weight = 200.345;
        

        to

        int argument1   = 1;
        int multiplier  = 100;
        double weight   = 200.345;
        

        (though I may have missed a secondary justification in there)

        If that’s a correct interpretation, then the discussion in this thread, which is about aligning a column to a specific character. (In the old days, on 32bit, you could use the TextFX plugin; but that plugin has fallen by the wayside in terms of support, and doesn’t (i believe) have a 64bit version.) So if you have 32bit, you can install TextFX and use the Line up multiple lines by …. Otherwise, you can try to make the PythonScript alternatives work for you.

        hoji afzalH 1 Reply Last reply Reply Quote 2
        • dinkumoilD
          dinkumoil
          last edited by

          There is a plugin called “Code alignment” which is available as 64 bit version too. Have a look into Plugin Manager. It’s a tiny useful thing.

          hoji afzalH 1 Reply Last reply Reply Quote 2
          • hoji afzalH
            hoji afzal
            last edited by hoji afzal

            @hoji-afzal

            Hi, thanks for your attention. I did notice the formatting in my original post and tried to go back and edit it, but the site doesn’t seem to allow you to edit a post once it has been published.

            Thanks for the clarification.

            I tried to download “Code alignment” as suggested by dinkumoil, but I got a server error. I will try it again later.

            For the alignment action I was talking about, all you need is the mouse and the context menu. No other settings would be needed. I could send the steps in an illustration, but I can’t because the site does not allow attachments. Actually I never thought of plug ins, and maybe someone has already done it. I am waiting for the next update to Notepad++ to see if I could get PlugIn Manager, as I don’t have it now.

            Thanks very much.

            1 Reply Last reply Reply Quote 0
            • PeterJonesP
              PeterJones
              last edited by

              The site allows editing for up to 3 minutes after you submit your post.

              If the results I posted weren’t right, and it was too late to edit your original post, you could have tried posting the data again in your reply, following the advice I gave you.

              Screenshots: This forum doesn’t host images, but it is perfectly capable of displaying them. Upload your image to imgur.com or other similar site, and follow the image-embed instructions found in the help with markdown link I already pointed you to.

              You can get plugin manager here: download, unzip, and put the DLL in your plugins subdirectory of the notepad++ program directory (as it says on that page)

              Once you get plugin manager, you can easily grab both “Code Alignment” and “TextFX”, and see if either one works for you.

              Scott SumnerS 1 Reply Last reply Reply Quote 2
              • Scott SumnerS
                Scott Sumner @PeterJones
                last edited by Scott Sumner

                @PeterJones said:

                The site allows editing for up to 3 minutes after you submit your post.

                The thing that gets me is there’s a damn preview pane right there as you are composing…what, do people think that when they hit Submit that things are somehow magically gonna look different from the Preview? If it doesn’t look like you intend then there must be a reason. Stop…and figure out why. (Grumpy week for me??)

                hoji afzalH 1 Reply Last reply Reply Quote 2
                • dinkumoilD
                  dinkumoil
                  last edited by

                  @Scott-Sumner said:

                  Grumpy week for me??

                  :-)

                  Sometimes we need to relax from PEBCAK…

                  1 Reply Last reply Reply Quote 3
                  • hoji afzalH
                    hoji afzal @dinkumoil
                    last edited by

                    @dinkumoil
                    I finally got it. It can align easily using the = sign. But I am still trying to get it to align on space.
                    Thanks a lot!

                    1 Reply Last reply Reply Quote 0
                    • hoji afzalH
                      hoji afzal @Scott Sumner
                      last edited by

                      @Scott-Sumner
                      Didn’t know it was that big a deal :)

                      Scott SumnerS 1 Reply Last reply Reply Quote 0
                      • Scott SumnerS
                        Scott Sumner @hoji afzal
                        last edited by

                        @hoji-afzal said:

                        Didn’t know it was that big a deal

                        It’s not a big deal to express yourself in a clear and understandable manner, especially when you are asking for something in a feature request? Hmmm. :-)

                        hoji afzalH 1 Reply Last reply Reply Quote -1
                        • hoji afzalH
                          hoji afzal @PeterJones
                          last edited by

                          @PeterJones

                          In fact this is how I format my columns to separate different fields like a table does.

                          Before:

                          public byte PUSH = 1;
                          private const int STOP = 0;
                          public const double SECS_TO_START = 10;
                          public int SEPARATOR = 21;
                          

                          After:

                          public  byte         PUSH            =  1;
                          private const int    STOP            =  0;
                          public  const double SECS_TO_START   = 10;
                          public  int          SEPARATOR       = 21;
                          

                          Once I figure out “align by space” I will create keyboard shortcuts for it since Notepad++ generously allows that.

                          Thanks.

                          1 Reply Last reply Reply Quote 1
                          • hoji afzalH
                            hoji afzal @Scott Sumner
                            last edited by

                            @Scott-Sumner

                            PeterJones understood what i said! Instead of complaining he showed me how to do it, which I appreciate.

                            1 Reply Last reply Reply Quote 1
                            • PeterJonesP
                              PeterJones
                              last edited by

                              To line up by a space, you would copy a space into the clipboard, then use TextFX > Edit > Line up … by (Clipboard Character). Oh, that seems to not work… and wouldn’t probably align things right anyway.

                              You might want to do a multi-step process:

                              1. RegEx FIND= (?=\w+ = \d+;), REPLACE= | (space, vertical bar)
                              2. Copy Vertical bar charcter
                              3. Line up by clipbboard character
                              4. RegEx FIND=\|, REPLACE= (space)
                              5. Line up by =
                                In your example data, it made SECS_TO_START= 10, which might not be what you want, so:
                              6. RegEx FIND== (just equal sign), REPLACE= = (space then equal sign)
                              1 Reply Last reply Reply Quote 3
                              • chcgC
                                chcg
                                last edited by

                                If it is just about how the visual aspect, maybe you want to try https://github.com/dail8859/ElasticTabstops (further explanation here http://nickgravgaard.com/elastic-tabstops/)

                                1 Reply Last reply Reply Quote 4
                                • dinkumoilD
                                  dinkumoil
                                  last edited by dinkumoil

                                  @hoji-afzal

                                  Seems like you are an “extreme aligner”… ;-)

                                  I wrote a VBScript that does the job. To get it to work you need to install the Pork2Sausage plugin of @donho and two tiny companion scripts.

                                  This is the VBScript, save it as CodeAlign.vbs:

                                  Option Explicit
                                  
                                  Const LVALUE = 0
                                  Const RVALUE = 1
                                  
                                  Dim strCode, arrCode, arrInLine, arrLValueTokens, strRValueTokens, intPrevMaxIdx
                                  Dim arrLine, arrMaxTokenLen, intLineIdx, intTokenIdx, intMaxRValueLen
                                  Dim intCnt, strLine, strToken, strOutput
                                  
                                  If Not WScript.StdIn.AtEndOfStream Then
                                    strCode = WScript.StdIn.ReadAll
                                  
                                    arrCode         = Split(strCode, vbNewLine)
                                    intMaxRValueLen = 0
                                    arrMaxTokenLen  = Array()
                                    arrLine         = Array()
                                    strOutput       = ""
                                  
                                    For intLineIdx = 0 To UBound(arrCode)
                                      If arrCode(intLineIdx) <> "" Then
                                        arrInLine       = Split(arrCode(intLineIdx), "=")
                                        arrLValueTokens = Split(Trim(arrInLine(0)), " ")
                                        strRValueTokens = Trim(arrInLine(1))
                                  
                                        If Len(strRValueTokens) > intMaxRValueLen Then
                                          intMaxRValueLen = Len(strRValueTokens)
                                        End If
                                  
                                        If UBound(arrLValueTokens) > UBound(arrMaxTokenLen) Then
                                          intPrevMaxIdx = UBound(arrMaxTokenLen)
                                          ReDim Preserve arrMaxTokenLen(UBound(arrLValueTokens))
                                  
                                          If intPrevMaxIdx < 0 Then
                                            arrMaxTokenLen(UBound(arrMaxTokenLen)) = 0
                                          Else
                                            arrMaxTokenLen(UBound(arrMaxTokenLen)) = arrMaxTokenLen(intPrevMaxIdx)
                                  
                                            For intCnt = intPrevMaxIdx To UBound(arrMaxTokenLen) - 1
                                              arrMaxTokenLen(intCnt) = 0
                                            Next
                                          End If
                                        End If
                                  
                                        For intCnt = 0 To UBound(arrLValueTokens)
                                          If intCnt < UBound(arrLValueTokens) Then
                                            If Len(arrLValueTokens(intCnt)) > arrMaxTokenLen(intCnt) Then
                                              arrMaxTokenLen(intCnt) = Len(arrLValueTokens(intCnt))
                                            End If
                                          Else
                                            If Len(arrLValueTokens(intCnt)) > arrMaxTokenLen(UBound(arrMaxTokenLen)) Then
                                              arrMaxTokenLen(UBound(arrMaxTokenLen)) = Len(arrLValueTokens(intCnt))
                                            End If
                                          End If
                                        Next
                                  
                                        ReDim Preserve arrLine(UBound(arrLine) + 1)
                                        arrLine(UBound(arrLine)) = Array(arrLValueTokens, strRValueTokens)
                                      End If
                                    Next
                                  
                                    For intLineIdx = 0 To UBound(arrLine)
                                      strLine  = ""
                                      strToken = arrLine(intLineIdx)(RVALUE)
                                  
                                      If IsNumeric(Left(strToken, Len(strToken) - 1)) Then
                                        strLine  = "= " & Right(String(intMaxRValueLen, " ") & strToken, intMaxRValueLen)
                                      Else
                                        strLine  = "= " & strToken
                                      End If
                                  
                                      For intTokenIdx = UBound(arrMaxTokenLen) To 0 Step -1
                                        If intTokenIdx = UBound(arrMaxTokenLen) Then
                                          strToken = arrLine(intLineIdx)(LVALUE)(UBound(arrLine(intLineIdx)(LVALUE)))
                                          strLine  = Left(strToken & String(arrMaxTokenLen(intTokenIdx), " "), arrMaxTokenLen(intTokenIdx)) & " " & strLine
                                  
                                        ElseIf UBound(arrLine(intLineIdx)(LVALUE)) > intTokenIdx Then
                                          strToken = arrLine(intLineIdx)(LVALUE)(intTokenIdx)
                                          strLine  = Left(strToken & String(arrMaxTokenLen(intTokenIdx), " "), arrMaxTokenLen(intTokenIdx)) & " " & strLine
                                  
                                        Else
                                          strLine = String(arrMaxTokenLen(intTokenIdx), " ") & " " & strLine
                                        End If
                                      Next
                                  
                                      strOutput = strOutput & strLine & vbNewLine
                                    Next
                                  
                                    WScript.StdOut.Write strOutput
                                  End If
                                  

                                  At next you need a script that can retrieve the clipboard content and stream it to StdOut. It’s a one-liner in JavaScript, save it as pclip.js.

                                  WSH.Echo(WSH.CreateObject('htmlfile').parentWindow.clipboardData.getData('text'));
                                  

                                  At next you need a binding to PorkToSausage. It’s a Batch script, save it as CodeAlign.cmd.

                                  @echo off
                                  
                                  pushd "%~dp0"
                                  cscript /nologo /e:JScript pclip.js | cscript /nologo "CodeAlign.vbs"
                                  popd
                                  

                                  Put the files CodeAlign.vbs, pclip.js and CodeAlign.cmd together in one directory and move it to <Notepad++-installation-directory>\plugins\Pork2Sausage\CodeAlign.

                                  Finally you need to add the following to the config file of Pork2Sausage. The plugin provides the ability to load its own config file for editing.

                                  [CodeAlign]
                                  progPath=<Notepad++-installation-directory>\plugins\Pork2Sausage\CodeAlign\CodeAlign.cmd
                                  progCmd=CodeAlign
                                  workDir=<Notepad++-installation-directory>\plugins\Pork2Sausage\CodeAlign
                                  replaceSelection=true
                                  

                                  The Pork2Sausage plugin will add a menu entry named CodeAlign to its submenu of Plugins menu. You can assign a keyboard shortcut to this entry to simplify things.

                                  Please note that you have to configure the right path of your Notepad++ installation directory. If you already use Notepad++ v7.6 you should put the directory with the three scripts to the new plugin folder under %UserProfile%\AppData\Local\Notepad++\plugins\PorkToSausage. Of course you have to change the paths in the Pork2Sausage config file accordingly.

                                  To use the script select the code you want to align and copy it to the clipboard. Then press the keyboard shortcut you assigned to the script’s menu entry. It does its job and the result is automatically pasted to the selected block of lines in the Notepad++ window.

                                  If you (or another person) is able to write Python scripts, you/(s)he can port the VBScript code to Python and you can use the Python plugin to execute it. This way you wouldn’t need so much script files.

                                  1 Reply Last reply Reply Quote 2
                                  • dinkumoilD
                                    dinkumoil
                                    last edited by

                                    @hoji-afzal

                                    Update: VBScript improved

                                    • Can handle and keep empty lines
                                    • Can handle and keep lines without equal sign
                                    • Code commented now
                                    Option Explicit
                                    
                                    Const LVALUE = 0
                                    Const RVALUE = 1
                                    
                                    Dim arrCode, arrInLine, arrLValueTokens, strRValueTokens, intPrevMaxIdx
                                    Dim arrLine, arrMaxTokenLen, intLineIdx, intTokenIdx, intMaxRValueLen
                                    Dim intCnt, strLine, strToken, strOutput
                                    
                                    'Read input from StdIn
                                    If Not WScript.StdIn.AtEndOfStream Then
                                      'Split input into lines and store them in array
                                      arrCode = Split(WScript.StdIn.ReadAll, vbNewLine)
                                    
                                      'Delete empty trailing line
                                      If arrCode(UBound(arrCode)) = "" Then
                                        ReDim Preserve arrCode(UBound(arrCode) - 1)
                                      End If
                                      
                                      arrLine         = Array()
                                      arrMaxTokenLen  = Array()
                                      intMaxRValueLen = 0
                                      strOutput       = ""
                                    
                                      'Process all lines
                                      For intLineIdx = 0 To UBound(arrCode)
                                        'If current line is empty create empty variables for LValue and RValue
                                        If arrCode(intLineIdx) = "" Then
                                          arrLValueTokens = Array("")
                                          strRValueTokens = ""
                                        Else
                                          'Split line in LValue and RValue, delete preceding and trailing space
                                          'characters and split LValue in tokens
                                          arrInLine       = Split(arrCode(intLineIdx), "=")
                                          arrLValueTokens = Split(Trim(arrInLine(0)), " ")
                                          
                                          'RValue could be empty
                                          If UBound(arrInLine) > 0 Then
                                            strRValueTokens = Trim(arrInLine(1))
                                          Else
                                            strRValueTokens = ""
                                          End IF
                                    
                                          'Remember maximum length of all RValues
                                          If Len(strRValueTokens) > intMaxRValueLen Then
                                            intMaxRValueLen = Len(strRValueTokens)
                                          End If
                                    
                                          'Build array with a table containing the maximum length of all tokens
                                          'in the same column.
                                    
                                          'If the number of tokens of the current LValue is greater than the number
                                          'of columns of the table it has to be enlarged.
                                          If UBound(arrLValueTokens) > UBound(arrMaxTokenLen) Then
                                            'Remember index of currently last table cell and enlarge it
                                            intPrevMaxIdx = UBound(arrMaxTokenLen)
                                            ReDim Preserve arrMaxTokenLen(UBound(arrLValueTokens))
                                    
                                            'Shift the value of the last table cell before enlargement
                                            'to the last cell after enlargement
                                            If intPrevMaxIdx < 0 Then
                                              'If the table had been empty before enlargement set the new element to 0
                                              arrMaxTokenLen(UBound(arrMaxTokenLen)) = 0
                                            Else
                                              'Set the currently last cell of the table to the value
                                              'of the previously last cell
                                              arrMaxTokenLen(UBound(arrMaxTokenLen)) = arrMaxTokenLen(intPrevMaxIdx)
                                    
                                              'Set the newly added cells to 0
                                              For intCnt = intPrevMaxIdx To UBound(arrMaxTokenLen) - 1
                                                arrMaxTokenLen(intCnt) = 0
                                              Next
                                            End If
                                          End If
                                    
                                          'If the current line contains longer tokens in any column than the lines
                                          'processed before remember the greater length
                                          For intCnt = 0 To UBound(arrLValueTokens)
                                            'If we do not inspect the last table cell compare the same columns of
                                            'the LValue tokens and the maximum token length table
                                            If intCnt < UBound(arrLValueTokens) Then
                                              If Len(arrLValueTokens(intCnt)) > arrMaxTokenLen(intCnt) Then
                                                arrMaxTokenLen(intCnt) = Len(arrLValueTokens(intCnt))
                                              End If
                                            'I we inspect the last LValue token alway compare it with the last cell
                                            'of the maximum token length table
                                            Else
                                              If Len(arrLValueTokens(intCnt)) > arrMaxTokenLen(UBound(arrMaxTokenLen)) Then
                                                arrMaxTokenLen(UBound(arrMaxTokenLen)) = Len(arrLValueTokens(intCnt))
                                              End If
                                            End If
                                          Next
                                        End If
                                    
                                        'Enlarge ouput array and store array with LValue tokens and RValue string
                                        ReDim Preserve arrLine(UBound(arrLine) + 1)
                                        arrLine(UBound(arrLine)) = Array(arrLValueTokens, strRValueTokens)
                                      Next
                                      
                                      'Iterate over output array of loop above and build up an output line from
                                      'LValue tokens and the RValue. The output line gets assembled from right to left.
                                      For intLineIdx = 0 To UBound(arrLine)
                                        strLine  = ""
                                        
                                        'Get RValue of current line
                                        strToken = arrLine(intLineIdx)(RVALUE)
                                    
                                        'Add equal sign and RValue only to the output line if RValue is not empty
                                        If strToken <> "" Then
                                          'If the RValue is a number right align it otherwise take it as is
                                          If IsNumeric(Left(strToken, Len(strToken) - 1)) Then
                                            strLine = "= " & Right(String(intMaxRValueLen, " ") & strToken, intMaxRValueLen)
                                          Else
                                            strLine = "= " & strToken
                                          End If
                                        End If
                                        
                                        'Iterate over the maximum token length table starting with last column
                                        For intTokenIdx = UBound(arrMaxTokenLen) To 0 Step -1
                                          'When processing the last column, take the last LValue token of the
                                          'current line, right-pad it with space characters to bring it to the
                                          'lenght of the longest token in this column and prepend it to output line
                                          If intTokenIdx = UBound(arrMaxTokenLen) Then
                                            strToken = arrLine(intLineIdx)(LVALUE)(UBound(arrLine(intLineIdx)(LVALUE)))
                                            strLine  = Left(strToken & String(arrMaxTokenLen(intTokenIdx), " "), arrMaxTokenLen(intTokenIdx)) & " " & strLine
                                    
                                          'When processing a column where the current LValue has a token, right-pad
                                          'it with space characters to bring it to the lenght of the longest token
                                          'in this column and prepend it to output line
                                          ElseIf UBound(arrLine(intLineIdx)(LVALUE)) > intTokenIdx Then
                                            strToken = arrLine(intLineIdx)(LVALUE)(intTokenIdx)
                                            strLine  = Left(strToken & String(arrMaxTokenLen(intTokenIdx), " "), arrMaxTokenLen(intTokenIdx)) & " " & strLine
                                    
                                          'When processing a column where the current LValue has no token, pad with
                                          'space characters to bring it to the lenght of the longest token in this
                                          'column and prepend it to output line
                                          Else
                                            strLine = String(arrMaxTokenLen(intTokenIdx), " ") & " " & strLine
                                          End If
                                        Next
                                    
                                        'Avoid appending of lines which contain solely space characters
                                        If Trim(strLine) = "" Then strLine = Trim(strLine)
                                        
                                        'Append output line to output string
                                        strOutput = strOutput & strLine
                                    
                                        'Dont terminate last line with EOL because it's added automatically
                                        If intLineIdx < UBound(arrLine) Then
                                          strOutput = strOutput & vbNewLine
                                        End If
                                      Next
                                    
                                      'Stream output string to StdOut
                                      WScript.StdOut.Write strOutput
                                    End If
                                    
                                    1 Reply Last reply Reply Quote 0
                                    • hoji afzalH
                                      hoji afzal
                                      last edited by hoji afzal

                                      Oh man, thanks so much guys for your input!
                                      I will try these once my manager stops with his “so how much progress did you make?” :)

                                      I am a fanatic aligner, just bordering having a disease. But usually don’t have time to go that “extreme”. And by extreme I am sure you refer to my right aligning the whole numbers hahaha

                                      Thanks.

                                      1 Reply Last reply Reply Quote 0
                                      • dinkumoilD
                                        dinkumoil
                                        last edited by

                                        @hoji-afzal said:

                                        I will try these once my manager stops with his “so how much progress did you make?” :)

                                        Oh yeah, the managers…

                                        Since you seem interested I have another update of the VBScript for you:

                                        • Improved handling of aligning lines containing no assignment operator
                                        • Delimiter and assignment operator configurable by constant in script
                                        • Keeps indentation
                                        • Improved detection of empty lines
                                        • Avoiding superfluous output lines and output lines filled solely with space characters
                                        Option Explicit
                                        
                                        Const LVALUE   = 0
                                        Const RVALUE   = 1
                                        Const DelimChr = " "
                                        Const AssignOp = "="
                                        
                                        Dim arrCode, arrInLine, arrLValueTokens, strRValueTokens, intPrevMaxIdx
                                        Dim arrLine, arrMaxTokenLen, intLineIdx, intTokenIdx, intMaxRValueLen
                                        Dim intCnt, strLine, strToken, strOutput
                                        
                                        'Read input from StdIn
                                        If Not WScript.StdIn.AtEndOfStream Then
                                          'Split input into lines and store them in array
                                          arrCode = Split(WScript.StdIn.ReadAll, vbNewLine)
                                        
                                          'Delete empty trailing line
                                          If arrCode(UBound(arrCode)) = "" Then
                                            ReDim Preserve arrCode(UBound(arrCode) - 1)
                                          End If
                                          
                                          arrLine         = Array()
                                          arrMaxTokenLen  = Array()
                                          intMaxRValueLen = 0
                                          strOutput       = ""
                                        
                                          'Process all lines
                                          For intLineIdx = 0 To UBound(arrCode)
                                            'If current line is empty create empty variables for LValue and RValue
                                            If Trim(arrCode(intLineIdx)) = "" Then
                                              arrLValueTokens = Array("")
                                              strRValueTokens = ""
                                            Else
                                              'Split line in LValue and RValue, keep indentation of LValue but delete
                                              'preceding and trailing space characters of RValue and split LValue in
                                              'tokens
                                              arrInLine       = Split(arrCode(intLineIdx), AssignOp)
                                              arrLValueTokens = Split(RTrim(arrInLine(0)), DelimChr)
                                              
                                              'There could be no RValue
                                              If UBound(arrInLine) > 0 Then
                                                strRValueTokens = Trim(arrInLine(1))
                                              Else
                                                strRValueTokens = ""
                                              End IF
                                        
                                              'Remember maximum length of all RValues
                                              If Len(strRValueTokens) > intMaxRValueLen Then
                                                intMaxRValueLen = Len(strRValueTokens)
                                              End If
                                        
                                              'Build a maximum token length table containing the maximum length of all
                                              'LValue tokens in the same column
                                        
                                              'If the number of tokens of the current LValue is greater than the number
                                              'of columns of the table it has to be enlarged.
                                              If UBound(arrLValueTokens) > UBound(arrMaxTokenLen) Then
                                                'Remember index of currently last table cell and enlarge table
                                                intPrevMaxIdx = UBound(arrMaxTokenLen)
                                                ReDim Preserve arrMaxTokenLen(UBound(arrLValueTokens))
                                        
                                                'Shift the value of the last table cell before enlargement
                                                'to the last cell after enlargement
                                                If intPrevMaxIdx < 0 Or strRValueTokens = "" Then
                                                  'If the table had been empty before enlargement set new cell to 0
                                                  arrMaxTokenLen(UBound(arrMaxTokenLen)) = 0
                                                Else
                                                  'Set the currently last cell of the table to the value of the
                                                  'previously last cell
                                                  arrMaxTokenLen(UBound(arrMaxTokenLen)) = arrMaxTokenLen(intPrevMaxIdx)
                                        
                                                  'Set the newly added cells to 0
                                                  For intCnt = intPrevMaxIdx To UBound(arrMaxTokenLen) - 1
                                                    arrMaxTokenLen(intCnt) = 0
                                                  Next
                                                End If
                                              End If
                                        
                                              'If the current line contains longer tokens in any column than the lines
                                              'processed before, remember the greater length
                                              For intCnt = 0 To UBound(arrLValueTokens)
                                                'If we do not inspect the last table cell, compare the same columns of
                                                'the LValue tokens and the maximum token length table
                                                If intCnt < UBound(arrLValueTokens) Or strRValueTokens = "" Then
                                                  If Len(arrLValueTokens(intCnt)) > arrMaxTokenLen(intCnt) Then
                                                    arrMaxTokenLen(intCnt) = Len(arrLValueTokens(intCnt))
                                                  End If
                                                'I we inspect the last LValue token, always compare it with the last cell
                                                'of the maximum token length table
                                                Else
                                                  If Len(arrLValueTokens(intCnt)) > arrMaxTokenLen(UBound(arrMaxTokenLen)) Then
                                                    arrMaxTokenLen(UBound(arrMaxTokenLen)) = Len(arrLValueTokens(intCnt))
                                                  End If
                                                End If
                                              Next
                                            End If
                                        
                                            'Enlarge ouput array and store array with LValue tokens and RValue string
                                            ReDim Preserve arrLine(UBound(arrLine) + 1)
                                            arrLine(UBound(arrLine)) = Array(arrLValueTokens, strRValueTokens)
                                          Next
                                          
                                          'Iterate over output array of loop above and build up an output line from
                                          'LValue tokens and the RValue. The output line gets assembled from right to left.
                                          For intLineIdx = 0 To UBound(arrLine)
                                            strLine  = ""
                                            
                                            'Get RValue of current line
                                            strToken = arrLine(intLineIdx)(RVALUE)
                                        
                                            'Add assignment operator and RValue to output line only if RValue is not empty
                                            If strToken <> "" Then
                                              'If the RValue is a number right align it otherwise take it as is
                                              If IsNumeric(Left(strToken, Len(strToken) - 1)) Then
                                                strLine = AssignOp & " " & Right(String(intMaxRValueLen, " ") & strToken, intMaxRValueLen)
                                              Else
                                                strLine = AssignOp & " " & strToken
                                              End If
                                            End If
                                            
                                            'Iterate over the maximum token length table starting with last column
                                            For intTokenIdx = UBound(arrMaxTokenLen) To 0 Step -1
                                              'When processing the last column, take the last LValue token of the
                                              'current line, right-pad it with space characters to bring it to the
                                              'lenght of the longest token in this column and prepend it to output line
                                              If intTokenIdx = UBound(arrMaxTokenLen) And arrLine(intLineIdx)(RVALUE) <> "" Then
                                                strToken = arrLine(intLineIdx)(LVALUE)(UBound(arrLine(intLineIdx)(LVALUE)))
                                                strLine  = Left(strToken & String(arrMaxTokenLen(intTokenIdx), " "), arrMaxTokenLen(intTokenIdx)) & DelimChr & strLine
                                        
                                              'When processing a column where the current LValue has a token, right-pad
                                              'it with space characters to bring it to the lenght of the longest token
                                              'in this column and prepend it to output line
                                              ElseIf (UBound(arrLine(intLineIdx)(LVALUE)) > intTokenIdx) Or _
                                                     (UBound(arrLine(intLineIdx)(LVALUE)) = intTokenIdx And arrLine(intLineIdx)(RVALUE) = "") Then
                                                strToken = arrLine(intLineIdx)(LVALUE)(intTokenIdx)
                                                
                                                If intTokenIdx = UBound(arrMaxTokenLen) And arrLine(intLineIdx)(RVALUE) = "" Then
                                                  strLine  = Left(strToken & String(arrMaxTokenLen(intTokenIdx), " "), arrMaxTokenLen(intTokenIdx))
                                                Else
                                                  strLine  = Left(strToken & String(arrMaxTokenLen(intTokenIdx), " "), arrMaxTokenLen(intTokenIdx)) & DelimChr & strLine
                                                End If
                                        
                                              'When processing a column where the current LValue has no token, pad with
                                              'space characters to bring it to the lenght of the longest token in this
                                              'column and prepend it to output line
                                              Else
                                                If intTokenIdx = UBound(arrMaxTokenLen) And arrLine(intLineIdx)(RVALUE) = "" Then
                                                  strLine = String(arrMaxTokenLen(intTokenIdx), " ")
                                                Else
                                                  strLine = String(arrMaxTokenLen(intTokenIdx), " ") & DelimChr & strLine
                                                End If
                                              End If
                                            Next
                                        
                                            'Avoid appending of lines which contain solely space characters
                                            If Trim(strLine) = "" Then strLine = ""
                                            
                                            'Append output line to output string
                                            strOutput = strOutput & strLine
                                        
                                            'Dont terminate last line with EOL because it is added automatically
                                            If intLineIdx < UBound(arrLine) Then
                                              strOutput = strOutput & vbNewLine
                                            End If
                                          Next
                                        
                                          'Stream output string to StdOut
                                          WScript.StdOut.Write strOutput
                                        End If
                                        

                                        Unfortunately the Pork2Sausage plugin currently has a bug, it streams at maximum 1024 bytes/characters back to Notepad++. I’ve already created a pull request with a fix, let’s see if and when it will be published.

                                        1 Reply Last reply Reply Quote 0
                                        • hoji afzalH
                                          hoji afzal
                                          last edited by

                                          Thanks for the code. But I’m sorry I still haven’t had a chance to try it. Plus, I am not very experienced with scripts.

                                          However, I did try TextFX and find its tool useful. I have set up a shortcut for it which I have been using. It’s TextFX > TextFX Edit > E: Line up multiple lines by (Clipboard Character). Unfortunately it doesn’t accept either space (perhaps the most useful character) or a pattern of characters. But still it’s Okay.
                                          Having said that, that menu is nowhere near as powerful or useful as a context menu to select a block and set it either right or left justified.

                                          Hoji

                                          1 Reply Last reply Reply Quote 0
                                          • guy038G
                                            guy038
                                            last edited by guy038

                                            Hello @hoji-afzal @peterjones, @dinkumoil, @scott-sumner, @chcg and All,

                                            I’ve found out a solution to line up with the space character :-)) Nevertheless, it’s just a work-around which could be rather tedious, if you want to line up a lot of small blocks of text :-(( Just have a try !

                                            It does not imply any script, just the TextFX plugin of course, and the native N++ Search/Replacement feature ;-))


                                            Here are, below, the different steps to follow :

                                            • First, anywhere in a file, on an empty location, hold down the ALT key and type, successively, on keys 0, 3 and 1, of the numeric keypad, then release the ALT key

                                            => A control character US, with code-point = \x1F, should be displayed, in reverse video !

                                            • Copy this single character in the clipboard

                                            • Select all the ( not aligned ) text of your previous example

                                            public byte PUSH = 1;
                                            private const int STOP = 0;
                                            public const double SECS_TO_START = 10;
                                            public int SEPARATOR = 21;
                                            
                                            • Now, open the Replace dialog ( Ctrl + H )

                                            • SEARCH \x20+

                                            • REPLACE \x1f

                                            • Check the In selection option, ( on the left of the Replace All button ) IMPORTANT

                                            • Select the Regular expression search mode

                                            • Click on the Replace All button

                                            => At once, all range of consecutive space characters, in the selection, are replaced with the control character US

                                            • Then, click on the TextFX menu, leaving the Replace dialog in the background

                                            • Select the option TextFX > TextFX Edit > Line up multiple lines by (Clipboard Character)

                                            => Now, most of the US characters should be aligned !

                                            • Click back on the Replace dialog ( Note that your text is still selected )

                                            • SEARCH \x1f

                                            • REPLACE \x20

                                            • Keep the same options as above

                                            • Click on the Replace All button

                                            => You should get the result, below :

                                            public  byte  PUSH      =             1;
                                            private const int       STOP          = 0;
                                            public  const double    SECS_TO_START = 10;
                                            public  int   SEPARATOR =             21;
                                            

                                            At first sight, we remark that some words are quite aligned, but not exactly in the way you would !:-(( The problem comes from the expressions Const int and Const double that you consider as single entities whereas the TextFX plugin considers, logically, that there are two consecutive words, separated with a space char !

                                            So, regarding your very simple block of code, I would say that the rule is : when a space character, is located after the word const, in lower-case, it should not be replaced with the US control char

                                            Thus, if you re-execute all the previous steps, replacing the first search regex \x20+ with the regex (?-i)(?<!\bconst)\x20+, you should be left with the expected text, below :

                                            public  byte         PUSH          = 1;
                                            private const int    STOP          = 0;
                                            public  const double SECS_TO_START = 10;
                                            public  int          SEPARATOR     = 21;
                                            

                                            Nice, isn’t it ? Note you still have to align the numbers, at the end of the lines !

                                            REMEMBER : When using the line up feature, the TextFX plugin does not consider any ,, = or clipboard character which are between two double quotes "........" nor between two simple quotes '........' !


                                            If you are interested to go further on, @hoji-afzal, just tell me all the multi-words expressions which need to be considered as a single word and I’ll try to get the appropriate regex, which preserves space characters, in these expressions :-))

                                            Best Regards,

                                            guy038

                                            hoji afzalH 1 Reply Last reply Reply Quote 3
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors