PyPN


18
May 11

Regular Expressions and Scripts – Worked Example

Programmer’s Notepad has great support for Regular Expressions baked in, supporting much more than the restricted syntax that many Scintilla-based editors provide. For example, did you know that you can use negative lookarounds to find text that doesn’t contain a pattern? This regular expression matches any line that doesn’t contain “not here”:

^((?!not here).)*$

Sometimes I find myself repeating the same set of Search/Replace operations in order to format text. If I’m going to do this more than a couple of times, I record a script using PyPN to make things easier for myself. Today I was turning text like this:

    LogStep("Do something");
    MyClass.DoReallyCleverStuff();
    MyClass.VerifySomethingAwesome();
    
    LogStep("Do another thing");
    MyClass.DoSomethingLessClever();
    MyClass.VerifySomethingBad();

Into text like this:


Do something
Do another thing

There are many ways to do something like this, and I could have written a PyPN script by hand to do it. However, combining Regular Expressions and Script Recording means I can do this without a lot of manual code effort. Here’s what I did:

  1. Tools > Record Script
  2. Replace All: ^((?!LogStep).)*$ with (nothing)
  3. Replace All: (\r\n){2,} with \r\n
  4. Replace All: \s*LogStep\("([^"]+)"\); with \1
  5. Tools > Stop Recording

At the end of this Programmer’s Notepad added a script to the Scripts window (Recorded\New Script) and placed the code for that script in a new editor window. Because I wanted to keep the script for future use, I changed it’s name and saved it to C:\Program Files\Programmer’s Notepad\Scripts. Here’s the script (which I reduced slightly for posting here):

import pn, scintilla

@script("Clean Up Log Steps", "Testing")
def CleanUpLogSteps():
	doc = pn.CurrentDoc()
	sci = scintilla.Scintilla(doc)
	opt = pn.GetUserSearchOptions()
	opt.FindText = u'^((?!LogStep).)*$'
	opt.MatchWholeWord = False
	opt.MatchCase = False
	opt.UseRegExp = True
	opt.SearchBackwards = False
	opt.LoopOK = True
	opt.UseSlashes = False
	opt.ReplaceText = u''
	opt.ReplaceInSelection = False
	doc.ReplaceAll(opt)
	# When recording, the result of this operation was: pn.FindNextResult(37)

	opt.FindText = u'(\\r\\n\\r\\n){2,}'
	opt.ReplaceText = u'\\r\\n'
	doc.ReplaceAll(opt)
	# When recording, the result of this operation was: pn.FindNextResult(16)

	opt.FindText = u'\\s*LogStep\\("([^"]+)"\\);'
	opt.ReplaceText = u'\\1'
	doc.ReplaceAll(opt)
	# When recording, the result of this operation was: pn.FindNextResult(10)

If I was going to use this script a lot, I could bind it to a shortcut key in Tools > Options > Keyboard.


27
Sep 10

PyPN 0.12 Released

Coinciding with the new Programmer’s Notepad 2.1.5 release, PyPN 0.12 is a minor update fixing a few bugs and adding support for some new small but important scenarios.

Download:
PyPN 0.12 for Python 2.6

As of this release, I have dropped support for Python 2.4 and 2.5, as there seems to be little benefit in maintaining multiple versions especially as these releases are so old. I am considering trying to bundle a specific version of Python with future releases, to avoid the need for separate installs.

Once you have installed PyPN 0.12, you can now use Python code in your Text Clips by enclosing it in backticks, e.g.


`print "Hello from PyPN"`

Anything you print to stdout will be included in the clip text.


2
Sep 10

Zen Coding in Programmer’s Notepad

A user requested support for Zen Coding on the bug tracker.

Zen Coding is a system for writing HTML and CSS in text editors, expanding shorthand notation to full code.

I hadn’t used Zen myself, but a quick read of the excellent Smashing Magazine Introduction and a watch of the above video and I was intrigued.

Zen itself is an editor-agnostic set of scripts available in both Javascript and Python. That’s right, Python!

As testament to the good design of Zen, it was a piece of cake to integrate it with PyPN, allowing you to drop Zen for PyPN into your scripts directory and start taking advantage of this useful shorthand.

If you use PN 2.1.4 or later, you can start using Zen yourself right now:

  1. Download Zen Coding for PyPN Preview
  2. Extract the contents of the zip into your Programmer’s Notepad\Scripts directory
  3. Start PN
  4. Optionally bind the Expand Abbreviation script to a keyboard shortcut

Future enhancements to this implementation (and PN 2.1.5) will bring tab stops so that you can tab through the generated code, and probably other Zen Coding commands. For now, let me know what you think!


19
Nov 09

New Development Build

Now that the Mercurial code move is over and a whole bunch more bugs have been worked out, I’m happy to announce the first 2.1 testing release that’s being signalled for downloads. The new build is also available for download here:

Download 2.1.1.2047 Installer
Download 2.1.1.2047 Portable

In case you haven’t been keeping up with the various announced changes, 2.1 has these headline features:

  1. Full Unicode Support
  2. Support for Translations – PN in your language
  3. Prototype Command Bar feature (with PyPN)
  4. Multiple simultaneous selections, including typing into block selections
  5. Virtual space

There are also a lot of bug fixes, smaller improvements and extensions interface improvements. This release of 2.1 is close to what I want to release as the new stable build as soon as it’s proven at least as stable as 2.0.10.


13
Oct 09

PN Command Bar

Version 2.1 introduces a new prototype feature called the Command Bar. The Command Bar enables modal editing control, allowing advanced keyboard control of the editor – the initial implementation adds some simple VI-style commands.

The handling for the commands is entirely implemented in PyPN, and it’s designed so it’s easy for you to extend and test your extensions immediately. Just edit commands.py and run it as a script and the command handling is updated.

The look and feel will obviously be worked on over time, but this version will be in the forthcoming testing release and can be enabled from the options if you have PyPN installed. This quick video doesn’t show you all of the features already supported (skipping search and some other combination commands) but gives you a feel for what can be done.


13
May 09

PyPN Script Repository

Like the idea of using Python scripts to enhance Programmer’s Notepad but not sure where to start? James Yoneda has created the awesome ScriptShare website for exactly this purpose:

Scriptshare in Google Chrome

Scriptshare in Google Chrome

There are a bunch of scripts already there, and hopefully you will add yours too! We’re working on getting this moved over to pnotepad.org eventually.

My thanks to James for putting this effort in and creating such a useful site in such short time!


13
May 09

PyPN 0.10.973 Released

I’m happy to announce a minor bug fix release for PyPN, the Python extension for Programmer’s Notepad. This release fixes a PN crash if you tried to create a Scintilla object with no active document, and also the exception syntax problem preventing PyPN from working in Python 2.4-2.5.

Downloads


2
May 09

Programmer’s Notepad 2 0.9.962 Released

The latest testing release of Programmer’s Notepad is out. This release brings a whole bunch of great changes: Script (macro) recording when using PyPN, a customizable toolbar, firefox-style tab ordering and smart highlight. There are also a load of bug fixes too.

Downloads

There is also a new PyPN release fixing a couple of issues with the included scripts from the last build:

Thanks to all the users who have put time and effort into reporting and following up on bugs, testing, and contributing patches.


11
Feb 09

Programmer’s Notepad 2 0.9.921 Released

The latest testing release of Programmer’s Notepad 2 is finally out. There are plenty of fixes in this release, and a few minor new features too. Of particular note are the following:

  1. Updates to the extensions interface allowing extensions to create menu items
  2. International input fixed (I know this will please a whole bunch of users)
  3. Read only edit protection cleaned up
  4. More text transforms, also available from context menu
  5. Tab to space and vice versa conversions fixed
  6. Notepad’s .LOG feature natively supported
  7. Fix a problem using PN on the Windows 7 beta causing PN to hang on exit

Downloads

There is a new PyPN release supporting the updated plugin interface:

Thanks to all the users who have put time and effort into reporting and following up on bugs, testing, and contributing patches.


5
Nov 08

Search within comments

There was a question on the programming reddit today about finding text within comments only. There’s no way to do this from the UI in Programmer’s Notepad, but it’s easy in PyPN:

d = pn.CurrentDoc()
s = scintilla.Scintilla(d)

searchopts = pn.GetUserSearchOptions()

while not pn.CurrentDoc().FindNext(searchopts) == -1:
    # s.GetStyleAt(s.TargetStart) will work in the next PyPN release
    style = d.SendMessage(2010, s.TargetStart, 0)
    # pn.AddOutput(str(style)) - find the current style
    if d.SendMessage(2010, s.TargetStart, 0) == 2:
    break

That small bit of code will find whatever is in the current user’s search options only where the style is 2 – that’s the C++ comment style number. Sadly these numbers are not uniform across schemes, so we’d need to do a bit more work to do this properly across anything. To turn that small snippet of code into a script that can be run from a keyboard shortcut is also easy:

import pn, scintilla

@script("Find In C++ Comments")
def FindInCComments():
    d = pn.CurrentDoc()
    s = scintilla.Scintilla(d)

    searchopts = pn.GetUserSearchOptions()

    while not pn.CurrentDoc().FindNext(searchopts) == -1:
        style = d.SendMessage(2010, s.TargetStart, 0)
        if d.SendMessage(2010, s.TargetStart, 0) == 2:
        break

Just save that into your scripts directory and you’re good to go!