Flip the lines order in a selection (PYTHON)

fb16
2012-05-30
2012-11-14
  • fb16

    fb16 - 2012-05-30

    Hello,

    I needed to flip the order of the lines in a selection (i.e. from 1 2 3 to 3 2 1) and here below is the Python script I'm using.

    QUESTION: How to prevent the alert dialogs, when running this script and no text is selected?

    Thank you

    # ----------------------------------------------------------
    # `FLIP LINES ORDER
    # ----------------------------------------------------------
    # Based on: http://superuser.com/a/392313
    #   Edit > Select All
    #   TextFX > TextFX Tools > Insert Line Numbers
    #   If TextFX > TextFX Tools > +Sort ascending is checked, uncheck it
    #   TextFX > TextFX Tools > Sort lines case sensitive (at column)
    #   TextFX > TextFX Tools > Delete Line Numbers or First Word
    # ----------------------------------------------------------
    # 1. INSERT A NUMBER at the beginning of each line
    notepad.runMenuCommand("TextFX Tools", "Insert Line Numbers")
    # 2. UNCHECK "TextFX Tools > +Sort ascending"
    if notepad.runMenuCommand("TextFX Tools", "+Sort ascending") == True:
        notepad.runMenuCommand("TextFX Tools", "+Sort ascending", False)
        notepad.runMenuCommand("TextFX Tools", "+Sort ascending")
    # 3. UNCHECK "TextFX Tools > +Sort outputs only UNIQUE (at column) lines"
    if notepad.runMenuCommand("TextFX Tools", "+Sort outputs only UNIQUE (at column) lines") == True:
        notepad.runMenuCommand("TextFX Tools", "+Sort outputs only UNIQUE (at column) lines", False)
        notepad.runMenuCommand("TextFX Tools", "+Sort outputs only UNIQUE (at column) lines")
    # 4. SORT LINES (case sensitive)
    notepad.runMenuCommand("TextFX Tools", "Sort lines case sensitive (at column)")
    # 5. DELETE NUMBERS at the beginning of each line
    notepad.runMenuCommand("TextFX Tools", "Delete Line Numbers or First Word")
    # 6. RE-CHECK back "TextFX Tools > +Sort ascending"
    if notepad.runMenuCommand("TextFX Tools", "+Sort ascending") == False:
        notepad.runMenuCommand("TextFX Tools", "+Sort ascending", True)
        notepad.runMenuCommand("TextFX Tools", "+Sort ascending")
    # 7. RE-CHECK back "TextFX Tools > +Sort outputs only UNIQUE (at column) lines"
    if notepad.runMenuCommand("TextFX Tools", "+Sort outputs only UNIQUE (at column) lines") == False:
        notepad.runMenuCommand("TextFX Tools", "+Sort outputs only UNIQUE (at column) lines", True)
        notepad.runMenuCommand("TextFX Tools", "+Sort outputs only UNIQUE (at column) lines")
    
     
  • Dave Brotherstone

    Ok, to answer your actual question (how to detect that no text is selected), just use

    if editor.getCurrentPos() != editor.getAnchor():
       # your commands here
    

    However, I think you'd be better actually reversing the lines in python, rather than this macro-ified method.  Apart from anything else, notepad.runMenuCommand returns True when it finds the command, not whether it's ticked or not.  So it will only return False if TextFX is not installed.

    The python version is much shorter and easier to understand, and doesn't rely on TextFX - I'm not sure if it's faster or not.

    editor.beginUndoAction()
    lines = editor.getLineCount()
    for n in range(0, lines / 2):
      tmpLine = editor.getLine(n)
      editor.replaceLine(n, editor.getLine(lines - n - 1).strip('\r\n'))
      editor.replaceLine(lines - n - 1, tmpLine.strip('\r\n'))
    editor.endUndoAction()
    

    If you want to reverse just the selected lines, then you'll need to change the range() parameters in the for loop.

    Cheers,
    Dave.

     
  • fb16

    fb16 - 2012-05-30

    Hi dave3gb,

    and thank you for your solution which works very well.

    What I don't get though, is how to change the range in the for loop in order to reverse just the selected lines.

    I've tried setting lines var this way:

    lines = editor.getUserLineSelection()
    

    but it doesn't work.

    Maybe there is a different command I'm not aware of?

    Sorry, as you could easily guess from my skills, I'm not a programmer…

    Thank you

    Regards