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):

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()

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.

One Response to “Line Movement Commands with PyPN”

  1. espresso says:

    Simon, I installed it and it just works great!
    Thanks a lot!