Line Movement Commands with PyPN

A bug on Google Code asked for an alternative to the built-in Transpose Lines command in Programmer’s Notepad, allowing a single command to move the current line up or down, allowing repeated use to shift a line through the current document.

To look at implementing this, I started with a python script - it’s so much quicker than writing C++ code to add these commands and going through compile/test for every change.

Here’s how to add these commands as a couple of scripts (which of course can have keyboard shortcuts):

[code language=’python’]import pn, scintilla, pypn.glue

@script(“Move Line Up”, “Text”) def MoveLineUp(): s = scintilla.Scintilla(pn.CurrentDoc()) l = s.LineFromPosition(s.CurrentPos) if (l == 0): return

s.BeginUndoAction()
s.LineTranspose()
s.LineUp()
s.EndUndoAction()

@script(“Move Line Down”, “Text”) def MoveLineDown(): s = scintilla.Scintilla(pn.CurrentDoc()) l = s.LineFromPosition(s.CurrentPos) if (l == (s.LineCount-1)): return

s.BeginUndoAction()
s.LineDown()
s.LineTranspose()
s.EndUndoAction() [/code]  

Drop this in a file in your scripts directory to use it (remember you need PyPN installed), or wait for the next version of PN which has these commands built in.