Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Justify Text (by UFO-Pu55y)

2010-10-06
2013-01-25
  • This script justifies the selected to the given width.  Written by UFO-Pu55y, posted here so people can find it.

    '''
        Align text to  both  the  left
        and right margin of a  defined
        width,  adding   extra   space
        between words as necessary.
    
        Note: Only  makes  sense  when
        using    monospaced     fonts!
    
                          by UFO-Pu55y
    '''
    
    def justifyText(oldStr, width):
        lstNewStr = []
        lenOldStr = len(oldStr)
        tail = ''
        pos = 0
        while pos < lenOldStr:
            tail = tail[tail.rfind(' ') + 1:]
            j = len(tail)
            while (j < width) and (pos < lenOldStr):
                tail += oldStr[pos]
                if tail[-1] == '\n':
                    tail = tail[:len(tail) - 2]
                    lstNewStr.append(tail)
                    tail = ''
                    pos += 1
                    break
                j += 1
                pos += 1
    
            if (pos < lenOldStr) and (tail.rfind(' ') > 0) and (oldStr[pos] not in [' ', '\r']):
                toZero = tail[:tail.rfind(' ')]
                spaceIndex = []
                for l in range(0, len(toZero)):
                    if toZero[l] == ' ':
                        spaceIndex.append(l)
                while len(toZero) < width:
                    n = len(spaceIndex)
                    while (n > 0) and (len(toZero) < width):
                        toZero = toZero[:spaceIndex[n - 1]] + ' ' + toZero[spaceIndex[n - 1]:]
                        spaceIndex[n - 1] += n
                        n -= 1
                lstNewStr.append(toZero)
            elif tail != '':
                lstNewStr.append(tail)
    
            if (len(tail) == width) and (tail.find(' ') < 0):
                tail = ''
                if ((pos + 1) < len(oldStr)) and (oldStr[pos:pos + 2] == '\r\n'):
                    pos += 2
            elif (pos < len(oldStr)) and (oldStr[pos] == ' '): 
                pos += 1
                tail = ''
            elif (tail != '') and ((pos + 1) < len(oldStr)) and (oldStr[pos:pos + 2] == '\r\n'):
                pos += 2
                tail = ''
        return '\r\n'.join(lstNewStr)
    
    def msgBox(text = '', buttonStyles = 0):
        """
            ButtonStyles:                 Return values:
            =============                 ==============
            0 OK                          0 Error (no message box was shown)
            1 OK Cancel                   1 OK
            2 Abort Retry Ignore          2 Cancel
            3 Yes No Cancel               3 Abort
            4 Yes No                      4 Retry
            5 Retry Cancel                5 Ignore
            6 Cancel Try Again Continue   6 Yes
                                          7 No
                                          10 Try Again
                                          11 Continue
        """
        return notepad.messageBox(text, 'Justify', buttonStyles)
    
    oldStr = editor.getSelText()
    if not oldStr:
        msgBox('No text selected!')
    else:
        width = int(notepad.prompt('Enter the width', 'Justify', '60'))
        if msgBox('Keep linebreaks?', 4) != 6:
            oldStr = oldStr.replace('\r\n',' ')
        newStr = justifyText(oldStr, width)
        editor.replaceSel(newStr)
    
     
  • This looks really nice, but when I try to save the program to a text file (using either the clipboard or the File/Save menu of the browser), I get a single long line without breaks.  It looks as though the administrators of this forum have disabled attachments (I don't understand why they've done that).  Perhaps there is some other way of posting this that would circumvent this problem?

    Thanks!

    Phillip

     
  • UFO
    UFO
    2012-05-22

    Either your browser isn't worth it, or the settings changed meanwhile. I had no problem with missing breaks.