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!

Comments are closed.