Pythonscript: Is there a way to detect changes in bookmarks?



  • My need is to detect any addition or removal of bookmarks.

    The closest I have found is the callback SCINTILLANOTIFICATION.MARGINCLICK, which works fine if you edit bookmarks from the left margin, but cannot detect if they are edited in any other way (shortcuts, macros, toolbar, menu, plugin GUI).

    Any kind of suggestions or help will be greatly appreciated.
    Thanks



  • @kiyn

    I don’t think you’re going to be able to get anything like a “notification” about this. Meaning that you would likely have to resort to a “polling” approach, where you examine every line in a file as to whether it current has or doesn’t have a bookmark. This is probably not the answer you wanted. :-(



  • @kiyn

    You can catch the SCN_MODIFIED notification and check for the SC_MOD_CHANGEMARKER flag.



  • @Alan-Kilborn said in Pythonscript: Is there a way to detect changes in bookmarks?:

    I don’t think you’re going to be able to get anything like a “notification” about this

    @dail said:

    You can catch the SCN_MODIFIED notification and check for the SC_MOD_CHANGEMARKER flag.

    I love to be wrong, especially when we get increased functionality from my wrongness. :-)

    Here’s a PS demo of what dail expressed above:

    # -*- coding: utf-8 -*-
    
    from Npp import editor, SCINTILLANOTIFICATION, MODIFICATIONFLAGS
    
    def callback_sci_MODIFIED(args):
        if args['modificationType'] & MODIFICATIONFLAGS.CHANGEMARKER:
            line = editor.lineFromPosition(args['position'])
            print('bookmark placed/removed on line {}'.format(line + 1))
    editor.callback(callback_sci_MODIFIED, [SCINTILLANOTIFICATION.MODIFIED])
    


  • I guess we can refine it a bit more, in order to tell a placement from a removal:

    # -*- coding: utf-8 -*-
    
    from Npp import editor, SCINTILLANOTIFICATION, MODIFICATIONFLAGS
    
    MARK_BOOKMARK = 24  # from N++ source code
    
    def callback_sci_MODIFIED(args):
        if args['modificationType'] & MODIFICATIONFLAGS.CHANGEMARKER:
            line = editor.lineFromPosition(args['position'])
            if editor.markerGet(line) & (1 << MARK_BOOKMARK):
                print('bookmark placed on line {}'.format(line + 1))
            else:
                print('bookmark removed on line {}'.format(line + 1))
    editor.callback(callback_sci_MODIFIED, [SCINTILLANOTIFICATION.MODIFIED])
    


  • That’s exactly what I was hoping for.
    I would never have succeeded by myself.
    Thanks a lot to both of you!