Saturday, April 07, 2012

Scripting Notepad++ with Python

In this post I am going explain how Notepad++ can be scripted with Python. I came to know about this as I wanted to analyse huge amount of console logs.

Installing Notepad++ Python Plugin

First we need to install Notepad++ Python plugin to able to control Notepad++ from python code. The plugin can be installed through Notepad++'s Plugin Manager by installing "Python Script" plugin or we can download the plugin from here http://sourceforge.net/projects/npppythonscript/ and extract the files into Notepad++'s plugin directory. As of this writing the Python Script plugin version is 0.9.2.

Invoking Python Scripts

The Notepad++ Python scripts needs to placed in particular directory so that it will recognized by the Python plugin and can be invoked from Notepad++. Usually the directory is %APPDATA%\Notepad++\plugins\config\PythonScript. The scripts can be invoked through menu Plugins->Python Script->Scripts. We can also create toolbar button for these scripts to quickly invoke them.

Counting Words Programmatically

To demonstrate the plugin lets write python script to count the number of characters, words and lines in the current editor window of Notepad++.
from Npp import *
import re

numChars = 0
numWords = 0
numLines = 0

editorContent = editor.getText()
for line in editorContent.splitlines():
  numLines += 1
  for word in re.findall("[a-zA-Z0-9]+", line):
    numWords += 1
    numChars += len(word)

notepad.messageBox("Number of characters: %d \nNumber of words: %d \nNumber of lines: %d" % (numChars, numWords, numLines))
at line 8 we get the active editor window's text content and everything else is typical Python program except at line 15 we tell the number of chars, words and lines through Notepad++ message box.

Bookmarking Programmatically

Lets see an another Python script which utilizes the bookmark feature of Notepad++
from Npp import *

notepad.menuCommand(MENUCOMMAND.SEARCH_CLEAR_BOOKMARKS)
linesBookmarked = []

def onMatch(lineNumber, match):
  if lineNumber not in linesBookmarked:
    lineStartPos = editor.positionFromLine(lineNumber)
    editor.gotoPos(lineStartPos)
    notepad.menuCommand(MENUCOMMAND.SEARCH_TOGGLE_BOOKMARK)
    linesBookmarked.append(lineNumber)

editor.pysearch("Pos", onMatch)
the above script bookmarks all the lines that contains the word "Pos". The Editor class provides a method "pysearch" which can search for given regular expression and will call the given function for each match. Like the "pysearch" method the Editor and Notepad class object provides various helper methods to automate Notepad++ functionalities from Python script.