plugin that support auto-numbering



  • So basicaly I want this:

    1- this
    2- That
    3- Those

    Meaning, if I typed “1-” then typed some words and hit enter, it would enter the next increment “2-” for me.

    Is there a plugin that does that?



  • @StoopidoMan

    Not that I’m aware of, but I certainly don’t know all.
    Maybe someone else?

    But my reason for replying is that if there isn’t such a plugin, we could probably script up something to do it, with the Pythonscript plugin.



  • I rather a plugin, I wonder why no body requested this, online I found out that many use Notepad++ as a simple To do list, but no one seems to want auto numbering, Maybe I am too lazy to type those to extra characters



  • @StoopidoMan

    Well, I was inclined to script something, but you haven’t encouraged me with your reply, so now I am “too lazy” to bother. Good luck in finding what you seek.



  • @StoopidoMan said in plugin that support auto-numbering:

    Is there a plugin that does that?

    Such a plug-in can be ordered, for example, I can do it for a couple of dollars :)



  • @StoopidoMan ,

    It’s not exactly what you’ve described, but Notepad++ has column-based auto-numbering built-in.

    After typing your N lines to be numbered, use column-mode (Alt+MouseDrag or Alt+Shift+Arrow), then Edit > Column Editor (Alt+C default), click Number to Insert, and Initial=1, Increase=1, OK , and it will number those lines for you.



  • @PeterJones said in plugin that support auto-numbering:

    It’s not exactly what you’ve described, but Notepad++ has column-based auto-numbering built-in.

    It’s so far from what is wanted, I didn’t bother to mention that. :-)
    IMO OP wanted dynamic or on the fly numbering.



  • @Alan-Kilborn said in plugin that support auto-numbering:

    It’s so far from what is wanted, I didn’t bother to mention that. :-)
    IMO OP wanted dynamic or on the fly numbering.

    But in the absence of a plugin that does exactly that, if he has more than a couple lines to auto-number, it might be less work than manually numbering the lines. TIMTOWTDI



  • @PeterJones said in plugin that support auto-numbering:

    TIMTOWTDI

    Thanks for mentioning that, good guy, and I use that feature when I have a long list that is not numbered. Anyway, How did you know I am a gu… Oh wait! to my excuse, I am StoopidoMAN!



  • @Дмитрий-Трошин
    With those Cyrillic script, no way comrade ;) Joking man, it doesn’t bother me that much



  • @Alan-Kilborn
    Sorry :( But if you did, many would appreciate it. Don’t listen to Stoopidoman. Me stoopid



  • @StoopidoMan said in plugin that support auto-numbering:

    Don’t listen to Stoopidoman. Me stoopid

    Haha, well, okay.
    I did want to give it a go because I thought it was an interesting problem.
    I came up with a Pythonscript for it.
    It isn’t all encompassing; I thought of a few things that it might be nice if it could do, that I didn’t implement:

    • if your caret is right after the - in 7- (for example), pressing Enter should probably stop the numbering and remove the 7- (because for now you appear to be done entering a numbered list)

    • if you caret into an existing list (items 1 - 10, say) and you go to line item 4 and go to the end of that line and press Enter, it opens a 5- line, which is fine, but perhaps it should also renumber the existing 5- to 10- as 6- through 11-.

    So anyway, it is a bit “bare bones”, but it does seem to function. If there’s actual interest, I may consider extending it to be more “well rounded”, or someone else can pick it up and do that.

    I call the script NumberingAutomagically.py, and without further ado, here it is:

    # -*- coding: utf-8 -*-
    
    from Npp import editor, SCINTILLANOTIFICATION
    import re
    
    class NA(object):
    
        def __init__(self):
            self.eol = ['\r\n', '\r', '\n'][editor.getEOLMode()]
            editor.callback(self.callback_sci_CHARADDED, [SCINTILLANOTIFICATION.CHARADDED])
    
        def callback_sci_CHARADDED(self, args):
            if chr(args['ch']) == self.eol[-1]:
                caret_pos = editor.getCurrentPos()
                curr_line_num = editor.lineFromPosition(caret_pos)
                if curr_line_num > 0:
                    prev_line = editor.getLine(curr_line_num - 1)
                    m = re.match(r'\s*(\d+)-', prev_line)
                    if m:
                        prev_number = int(m.group(1))
                        new_number = prev_number + 1
                        text_to_insert = str(new_number) + '-'
                        editor.insertText(caret_pos, text_to_insert)
                        new_caret_pos = caret_pos + len(text_to_insert)
                        editor.setEmptySelection(new_caret_pos)
    
    if __name__ == '__main__':
    
        try:
            na
        except NameError:
            na = NA()
    

    You run it once, and then it stays resident to do its job.



  • Hi, @alan-kilborn, @StoopidoMan and all

    I’ve just tried your script. Interesting ! The script keeps the leading blank characters and you can change the indentation, at any time : Nice !

    I thought of an small improvement : why not allow any list of symbols between the number and the first letter ?

    Here is my modified script :

    # -*- coding: utf-8 -*-
    
    from Npp import editor, SCINTILLANOTIFICATION
    import re
    
    class NA(object):
    
        def __init__(self):
            self.eol = ['\r\n', '\r', '\n'][editor.getEOLMode()]
            editor.callback(self.callback_sci_CHARADDED, [SCINTILLANOTIFICATION.CHARADDED])
    
        def callback_sci_CHARADDED(self, args):
            if chr(args['ch']) == self.eol[-1]:
                caret_pos = editor.getCurrentPos()
                curr_line_num = editor.lineFromPosition(caret_pos)
                if curr_line_num > 0:
                    prev_line = editor.getLine(curr_line_num - 1)
                    m = re.match(r'\s*(\d+)(.+?)(?=\w)', prev_line)
                    if m:
                        prev_number = int(m.group(1))
                        symbols_after = m.group(2)
                        new_number = prev_number + 1
                        text_to_insert = str(new_number) + symbols_after
                        editor.insertText(caret_pos, text_to_insert)
                        new_caret_pos = caret_pos + len(text_to_insert)
                        editor.setEmptySelection(new_caret_pos)
    
    if __name__ == '__main__':
    
        try:
            na
        except NameError:
            na = NA()
    

    For instance, this modified script can handle auto-numbering lists like below :

    1 ............... First chapter
    2 ............... Second chapter
    
    
    9.	First chapter
    10.	Second chapter
    
    
    100  ####### First Chapter
    101  ####### Second Chapter
    
    
    1000 ----> First Chapter
    1001 ----> Second Chapter
    
    
    5.       |  1st Chapter
    6.       |  2nd Chapter
    
    
    73 ======  _First Chapter
    74 ======  _Second Chapter
    

    However, my version does not behave as yours when hitting the Enter key without writing any text :-(

    • Your version does not stop the numbering, when hitting the Enter key whereas my version stops it ( I didn’t do it on purpose ! )

    • If I write a number of more than two digits, and hit the Enter key, at one moment, my version bugs definitively ! I couldn’t understand exactly why.

    I need your insight, Alan !

    Best Regards

    guy038



  • You can do interesting things with script plugins.
    alt text



  • @guy038 said in plugin that support auto-numbering:

    Your version does not stop the numbering, when hitting the Enter key whereas my version stops it ( I didn’t do it on purpose ! )

    I think this is probably because you added the (?=\w) look-ahead assertion. If there is no data on the current line after the number(s) and the “symbol(s)” then your lookahead fails and the m = regex test fails, thus the script does nothing more.

    Note that the “curr_line” and “prev_line” verbage that I use in the script are relative to what is “current” and “previous” AFTER the Enter key is pressed!

    If I write a number of more than two digits, and hit the Enter key, at one moment, my version bugs definitively ! I couldn’t understand exactly why.

    That part should be easy for you to understand as it only involves the regex! :-)

    Say you type 789 at the start of a line and hit Enter right after.
    The \s* matches nothing.
    The \d+ will match the 7.
    The .+? will match the 8.
    The (?=\w) will match the 9.
    This is clearly not what is wanted here (we wanted the \d+ part to match the complete 789 number).

    I thought of an small improvement : why not allow any list of symbols between the number and the first letter ?

    I presented sort of a “basic” script for this type of processing, that met the OP’s specification, mainly to show how “easy” it was to implement in a short script, leveraging the Pythonscript plugin.

    Note to the OP’s original desire: To implement a full-blown independent plugin for this behavior, while not difficult (probably), would be a bigger effort, and why would someone do that?



  • Hello, @alan-kilborn, @StoopidoMan, @peterjones and All

    OK… So, I changed the old regex \s*(\d+)(.+?)(?=\w) with this new version \s*(\d+)([^\w\r\n]+). Indeed, as the [^\w\r\n]+ looks for a non-null range of non-word character(s), different from EOL chars, no need for a look-ahead ! This new regex ensures that, both :

    • All the digits of a number will be matched by the \d+ part

    • Necessarily, the next character, after the symbol(s) range, will be a word character or an EOL character


    So, my final version ( as I’m really not fluent at Python ! ) is :

    # -*- coding: utf-8 -*-
    
    from Npp import editor, SCINTILLANOTIFICATION
    import re
    
    class NA(object):
    
        def __init__(self):
            self.eol = ['\r\n', '\r', '\n'][editor.getEOLMode()]
            editor.callback(self.callback_sci_CHARADDED, [SCINTILLANOTIFICATION.CHARADDED])
    
        def callback_sci_CHARADDED(self, args):
            if chr(args['ch']) == self.eol[-1]:
                caret_pos = editor.getCurrentPos()
                curr_line_num = editor.lineFromPosition(caret_pos)
                if curr_line_num > 0:
                    prev_line = editor.getLine(curr_line_num - 1)
                    m = re.match(r'\s*(\d+)([^\w\r\n]+)', prev_line)
                    if m:
                        prev_number = int(m.group(1))
                        symbols_after = m.group(2)
                        new_number = prev_number + 1
                        text_to_insert = str(new_number) + symbols_after
                        editor.insertText(caret_pos, text_to_insert)
                        new_caret_pos = caret_pos + len(text_to_insert)
                        editor.setEmptySelection(new_caret_pos)
    
    if __name__ == '__main__':
    
        try:
            na
        except NameError:
            na = NA()
    

    Notes :

    • First, all credits must be given to @alan-kilborn, as I haven’t modified very much the original script ;-))

    • As soon as you type in, at least, one digit followed with, at least, one symbol, including space(s), different from a word character ( digits, letters and underscore ), the script begins an auto-numbering sequence

    • A hit on the Enter key moves the caret so the same column of next line with the increased number, even if no additional word char(s) is added, at end of current line

    • At any time, you may change the indentation, adding or deleting space and/or tab character(s) at beginning of current line

    • Delete all the symbol(s) range, and, possibly, the current number, to stop the auto numbering sequence. Of course, you may, as well, move the caret anywhere else and go on typing !

    Cheers,

    guy038



  • This post is deleted!


  • @guy038 And @Alan-Kilborn
    I am not that versed in NPP, how do I run a script when NPP runs?

    Google told me I have to install a plugin, to run python scripts, do you guys mind explaining it to me like I am 5.

    And thank you guys, I appreciate it, and I hope many others who need this would appreciate it as well when they come across it.



  • @StoopidoMan

    We talk about scripts so much here that I’m surprised we don’t have a FAQ entry about getting up and running.

    So the first thing is to use Plugins Admin (on Plugins menu) to get the Python Script plugin.

    After that, do Plugins > Python Script > New Script and give the script a name; as I said, I called my copy NumberAutomagically.py but you are free to name it anything you like (it is just a name so that the plugin can find the script file).

    Then N++ will open the (empty) script file, at which point you can copy and paste one version of the script above in and save it.

    To run a script you do Plugins > Python Script > Scripts > and then click on your script.

    I recommend trying it out before bothering to try to hook it into running when Notepad++ runs.



  • @Alan-Kilborn from the documentation I found out that I can paste your script to [Startup.py] to run it when NPP starts, however, is it possible to run NumberAutomagically.py located in user Folder at NPP startup, I can’t figure that out?
    This way I can run several scripts in different file on NPP startup, instead of having all those scripts in side one file, it is more elegant.

    Also, if you have time, would you please modify the script so it would renumber the existing 5- to 10- as 6- through 11- when you enter a new line after 4-

    PS: just noticed NumberAutomagically.py, cool name :) And really THANK YOU both.

    Don’t read after this, it is for other StoopidoPeople who came across this, hi future people:

    After installing Python Script and creating the script, you can add it to NPP toolbar with a custom icon or added to the Python script menu by going to:
    [ Main Notepad++ UI>Plugins> Python Scripts>Configuration…> choose [NumberAutomagically by Alan Kilbor and Guy038.py] or [NumberAutomagically by Alan Kilbor.py] and then click Add button on the right side or left depending on your preference.

    43bfe3f8-e6be-446b-804e-956a092d85ad-image.png

    Clarification:
    3ced00ba-59fa-4abe-b8c6-33e9650bad94-image.png

    The left Add button will add [NumberAutomagically.py] to [Plugins>Python Scripts>NumberAutomagically.py] menu
    521153fa-b5bd-4976-83ff-58018df0e139-image.png

    And, the right button will add the a button to run the script on NPP toolbar
    10a88bbe-4bfa-4f08-869c-8803645836b7-image.png
    Also, you can install Customize Toolbar by going to [plugins>Plugins Admin…] and searching for [Customize Toolbar ] to change the order of that buttons :)


Log in to reply