Community
    • Login

    Search/replace macro options change, easier way?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 4 Posters 5.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Alan KilbornA
      Alan Kilborn
      last edited by

      Sometimes I create search or replace macros by recording them. After that I sometimes need to tweak the options (match case, in-selection, and so on…). It is hard to do this without rerecording the entire macro…by this I mean editing the macro in shortcuts.xml. Well, editing the xml is easy, but what is hard is deciding how to change the decimal number that all the search options are encoded into. Is there an easier way to do this and have some feeling of confidence that it has been done correctly?

      Claudia FrankC Scott SumnerS 2 Replies Last reply Reply Quote 0
      • Claudia FrankC
        Claudia Frank @Alan Kilborn
        last edited by

        @Alan-Kilborn

        other then hoping for completeness and relying on the list here - I don’t know.

        Cheers
        Claudia

        Alan KilbornA 1 Reply Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @Claudia Frank
          last edited by

          @Claudia-Frank

          Hi Claudia, yes I knew about that list

          1 Reply Last reply Reply Quote 0
          • Scott SumnerS
            Scott Sumner @Alan Kilborn
            last edited by

            @Alan-Kilborn

            This Pythonscript may help; I call it 1702decoder.py:

            import re
            
            def decode1702__main():
                options_list = [
                    'whole-word',
                    'case-match',
                    'clear-red-presearch',
                    'UNUSED',
                    'bookmark-line',
                    'subfolders',
                    'hidden',
                    'in-sel',
                    'wrap',
                    'downward'
                    ]
                input = '0'
                while True:
                    input = notepad.prompt('Enter your 1702 decimal #:', '', input)
                    if not input: return
                    input = int(input)
                    prompt_text = ''
                    for (j, v) in enumerate(options_list):
                        prompt_text += '[ {} ]{}      '.format('x' if input & (1 << j) else ' ', options_list[j])
                        if (j + 1) % 3 == 0: prompt_text = prompt_text.rstrip() + '\r\n'
                    prompt_text = prompt_text.rstrip()
                    input = notepad.prompt('Set your desired options by putting an x inside the [   ], e.g. [ x ]:', '', prompt_text)
                    if not input: return
                    new_input = 0
                    for (j, v) in enumerate(options_list):
                        m = re.search(r'\[([^]]+)\]' + options_list[j], input)
                        if m and m.group(1) != ' ' * len(m.group(1)): new_input |= 1 << j
                    input = str(new_input)
            
            decode1702__main()
            

            When run, it produces a prompt screen where you can put a 1702 parameter value that one of your macros uses, or you can just leave it set at 0. Pressing OK to that prompt will decode the entered values into the corresponding option values in a second prompt screen; example for an input of 3:

            From this point, changing the options and pressing OK will show you the decimal number associated with those options and you can keep going, changing the value or the options and seeing the effects until you answer Cancel to a prompt.

            Hope this helps you.

            1 Reply Last reply Reply Quote 2
            • Alan KilbornA
              Alan Kilborn
              last edited by

              Nice. I like it. Regarding “downward”, the docs say “upward” but I think I remember reading here (maybe) that this is a bug in the docs…

              1 Reply Last reply Reply Quote 0
              • Scott SumnerS
                Scott Sumner
                last edited by

                It was discovered due to info in this thread that the . matches newline checkbox was missing from the above script. Simply inserting 'dot-matches-newline', after the line containing 'downward', will fix it, so it looks like this in the script:

                'downward',
                'dot-matches-newline',      # <---- add me!
                ]
                
                1 Reply Last reply Reply Quote 1
                • hoytprH
                  hoytpr
                  last edited by

                  Sorry, noob here.
                  Why am I getting:

                  C:\Program Files\Notepad++>1702decoder.py
                  Traceback (most recent call last):
                  File “C:\Program Files\Notepad++\1702decoder.py”, line 35, in <module>
                  decode1702__main()
                  File “C:\Program Files\Notepad++\1702decoder.py”, line 19, in decode1702__main
                  input = notepad.prompt(‘Enter your 1702 decimal #:’, ‘’, input)
                  NameError: name ‘notepad’ is not defined

                  When I run this script?

                  Scott SumnerS 1 Reply Last reply Reply Quote 1
                  • Scott SumnerS
                    Scott Sumner @hoytpr
                    last edited by Scott Sumner

                    @hoytpr

                    Why am I getting…NameError: name ‘notepad’ is not defined…When I run this script?

                    Because I am lazy when testing something I post. :-)

                    Add this at the top of the script and I think it will fix it:

                    from Npp import notepad

                    What I do is have that line (or something similar) in my startup.py file–that file runs when Pythonscript starts up, obviously. This keeps me from having to put that line (and others that are similar, e.g., one for the editor object…) in every single Pythonscript file I have–the imports happen at startup and are available for the remainder of the Notepad++ session.

                    1 Reply Last reply Reply Quote 2
                    • hoytprH
                      hoytpr
                      last edited by

                      Awesome. Thanks. Are you as lazy as the noob who just says “help”? Probably not.
                      Good idea to put it in the startup.py.

                      1 Reply Last reply Reply Quote 1
                      • Alan KilbornA Alan Kilborn referenced this topic on
                      • First post
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors