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.1importrefromcollectionsimportdequelines=deque()lastLineNo=-1defcopyLine(lineNo,match):globallastLineNogloballinesiflineNo!=lastLineNo:editor.gotoLine(lineNo)text=editor.getCurLine()lines.append(text)lastLineNo=lineNolines.clear()lastLineNo=-1editor.pysearch(r'\.jpg',copyLine,re.IGNORECASE)# create new buffer for the matching linesnotepad.new()fortextinlines:editor.addText(text)lines.clear()notepad.messageBox("Matching lines were copied successfully.","Lines Matching Pattern",0)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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".
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.
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.