Lines Matching Regexp To New Buffer

2010-09-28
2013-01-25
  • Jake Coughlin
    Jake Coughlin
    2010-09-28

    This is the first Python code that I've ever written in my life.

    Here's a short script which copies all lines that match a regexp to a new buffer.  It provides functionality similar to TextFX Viz "Hide lines with clipboard text".

    # Lines_Matching_RE.py
    #
    # Version 1.0.1
    import re
    from collections import deque
    lines = deque()
    lastLineNo = -1
    def copyLine(lineNo, match):
        global lastLineNo
        global lines
        if lineNo != lastLineNo:
                editor.gotoLine(lineNo)
                text = editor.getCurLine()
                lines.append(text)
                lastLineNo = lineNo
    lines.clear()
    lastLineNo = -1
    editor.pysearch(r'\.jpg', copyLine, re.IGNORECASE)
    # create new buffer for the matching lines
    notepad.new()
    for text in lines:
        editor.addText(text)
    lines.clear()
    notepad.messageBox("Matching lines were copied successfully.", "Lines Matching Pattern", 0)
    
     
  • Looks great, nice job!  :)

    Couple of things you could do to it….   maybe make the regular expression a user choice - use notepad.prompt('Prompt','Title', 'default').  The other thing is you can use editor.getLine(lineNo) instead of moving the cursor then getting the current line.

    Another option for this would be to open the new document, then move it to the other view, then you can use the editor1 object to 
    get the lines, and editor2 object to write the lines, so you don't need any extra memory while you find the matches (could be useful for larger documents).  There's some fun working out if the original document is in the second view, therefore editor1 and editor2 would be the other way round (use notepad.getCurrentView()), but it'd be fun getting it going.

    Thanks for posting your script! Hope you're enjoying Python!

    Cheers,
    Dave.

     
  • Jake Coughlin
    Jake Coughlin
    2010-09-28

    Actually, in the original version, I did allocate the new buffer first, and I simply copied the lines from one buffer into the other.  I haven't really grasped the paradigm yet so each time I switched buffers the display would get updated.  It was quite a frenetic display, and it was also very slow.  The list solution isn't memory efficient, but it's much more elegant.  :-)

    I use Emacs for my powerhouse editing, but I am in and out of notepad++ thousands of times a day.  Since Emacs is written mostly in LISP, I definitely see the potential in using Python to extend notepad++.  Trust me, I have LOTS of ideas for scripts.  I'm simply not that experienced with Python or Scintilla.

    Anyway, so I appreciate the tips!  (Especially the notepad.prompt() - I hadn't discovered that yet.)

    And, yeah, PythonScript is quite satisfying!

     
  • I didn't mean switch between buffers - I can imagine that would be slow (and, as you say, be a bit of a trippy display!).  You can have two views in N++, just right click one of the tabs and click "Move to Other View".  You can then reference the views individually with editor1 and editor2 (instead of just 'editor') - then you're not switching anything, just reading/writing from/to two different buffers.  Obviously the "Move to other view" can be done in the script, with notepad.menuCommand(MENUCOMMAND.VIEW_GOTO_ANOTHER_VIEW)

    Dave.