Plugin to find next available number

  • David

    David - 2013-02-01

    I’m trying to create a plugin that will return the next available number based on a selection. So, if 50000 is selected, the program will increment and search untill none is found. I.e. if 50000 through 50023 are used in the document, 50024 will be returned. Sounds easy enough. Problem is I have no idea how to do that in notepad++. This is about as far as I’ve gotten in C# (I know it isnt much…)

    Sci_CharacterRange tempRange = new Sci_CharacterRange(0,(int)Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_GETLENGTH, 0, 0));

    Sci_TextToFind tempTTF = new Sci_TextToFind(tempRange, "50000");

    Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_FINDTEXT, 0, tempTTF.NativePointer );

    This doesn't seem to work. I’m assuming I’m not doing this correctly… (I don’t know much about C# and it’s been over 5 years since I’ve programmed in C++…)

    There are two things I figure I need to accomplish my goal:
    1. Get the selected number
    o The number will always be 5 digits, so it doesn't have to be selected. If I could get the current position and search backwards for a space I could then move forward 5 characters to get my number.
    2. Increment selected number by 1 and search the document for that number. The next number could be anywhere in the document, so I’d have to search the entire document.
    o If that incremented number is found, loop.
    o If incremented number is not found, return that number. The goal would be to actually replace the current number with the next available.

    The desired result of the program would be as follows:
    Lets say 52000 through 52162 are used in the document. I’d type 52000 (leave the cursor at the end of 52000)and press [ctrl]+[spacebar]. The program would find the next available number and replace 52000 with 52163. Or alternatively, I could type in 52 and hit [ctrl]+[spacebar] and it would complete the number (52163).

    Any help would be greatly appriciated.

  • Dave Brotherstone

    You should be able to do that really easily with a little python script (install the python script plugin).

    How about if the script just searched the document, found the next number, and inserted it at the character position?

    Just paste the following as a new python script.

    nextNumber = 0
    def foundNumber(line, m):
        global nextNumber
        thisNumber = int(
        if thisNumber > nextNumber:
            nextNumber = thisNumber;
    editor.pysearch('([0-9]{5})[^0-9]', foundNumber)
    editor.addText(str(nextNumber + 1))
    • David

      David - 2013-02-01

      Ha, ok. Should have read the help... Sorry.

      ok, got it installed and running. I'll play around with the code and get back to you. Is there a way to assign a python script to a key stroke?

      In your above script, what does '([0-9]{5})[^0-9]' do?

      Thanks to all for the help.

      • pedro_sland

        pedro_sland - 2013-02-01

        It is a regular expression. It searches for any number (0-9) repeated 5 times ({5}) where the next character is not a number. The ()s group the match so it can be read separately (

        I've not tried it and I'm not familiar with python's regex implementation but I think there might be an edge case for not matching something like abc 12345 right at the end of the document. You can check that later when you've got the rest of your code working.

        @Dave Did you get an email from me at the beginning of the year? No problem if you were busy.

  • François-R Boyer

    Personally, this is a thing I would first try to do with the Python script plugin. For "1", get the selected number, it's simply number = int(editor.getSelText()), then "2" you can do number = number + 1, and to search it in text you can test for editor.findText(FINDOPTION.WHOLEWORD, 0, editor.getTextLength(), str(number)) == None.

  • cchris

    cchris - 2013-02-01

    Did you make sure your messages do reach the Scintilla component at al? Try something trivial first, like going at end of line and sending SCI_HOME. When this works, things will get easier, if only because you can get some feedback.


  • David

    David - 2013-02-01

    Ok, i'm trying to download the Python script installer but it's saying:
    This ANSI plugin is not compatible with your Unicode Notepad++
    This plugin is not compatible with current version of notepad++"

    I installed Notepad++ v6.2.3 and PythonScript_full_0.9.2.0

  • Dave Brotherstone

    You've got the python27.dll in the plugins directory - it doesn't go in there, it goes in the n++ directory. But just install it through plugin manager - Plugins, Plugin Manager, Show Plugin Manager, then tick "Python Script", and click install. Done.

  • David

    David - 2013-02-04

    Ok, got it working! Thanks to all for the help.
    Here's the complete code:


    sCurNum = ""
    iCurNum = 0

    iCurPos = editor.getCurrentPos()

    sCurNum = editor.getTextRange(iCurPos-2,iCurPos)
    sCurNum = sCurNum + "000"
    iCurNum = int(sCurNum)


    while editor.findText(FINDOPTION.WHOLEWORD, 0, editor.getTextLength(), str(iCurNum)) != None:
    iCurNum = iCurNum + 1


    iCurPos = editor.getCurrentPos() + 5



    I overrode ctrl+spacebar and set it to this. I'll probably modify the code a little here and there as I use it more, but the basic idea works.
    Thanks again.