Community
    • Login

    PythonScript ops on selection if any, all text otherwise

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    pythonpythonscriptregex
    18 Posts 3 Posters 1.3k 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.
    • M Andre Z EckenrodeM
      M Andre Z Eckenrode @Alan Kilborn
      last edited by

      @alan-kilborn said in PythonScript ops on selection if any, all text otherwise:

      I mentioned that length is available by asking Scintilla directly for just the number, via editor.getLength() or editor.getTextLength() — no retrieving the text (since it is unnecessary to have the text).

      Ok, I see now. I missed that distinction. Thanks for the clarification.

      1 Reply Last reply Reply Quote 2
      • Alan KilbornA
        Alan Kilborn @M Andre Z Eckenrode
        last edited by

        @m-andre-z-eckenrode said in PythonScript ops on selection if any, all text otherwise:

        most of my actual Python Regex scripts have multiple search/replace steps, up to maybe 15 or so, and most of them are at least somewhat lengthy and complicated, so it would be great if I didn’t have to repeat all those steps

        Maybe a list of tuples meets the need here?

        my_find_repl_tup_list = [
            ( r'find_str_1', r'repl_str_1'),
            ( r'find_str_2', r'repl_str_2'),
            ( r'find_str_3', r'repl_str_3'),
            ]
        

        Then you could loop over the list:

        for (find, replace) in my_find_repl_tup_list:
            ...
        
        M Andre Z EckenrodeM 1 Reply Last reply Reply Quote 3
        • M Andre Z EckenrodeM
          M Andre Z Eckenrode @Alan Kilborn
          last edited by

          @alan-kilborn said in PythonScript ops on selection if any, all text otherwise:

          Maybe a list of tuples meets the need here?

          Well, it LOOKS promising, but I evidently can’t figure out how to utilize it. Based on your example as shown in your post, I adapted the find/replace code from my own previous post, consulted this page and tried to use it here, but…

          thistuple = [
              ( r'0x0x0', ur'•וו'),
          

          …etc. gave me an invalid syntax error with their print demo example. I then tried…

              ( "r'0x0x0', ur'•וו'"),
          

          …and that seemed to work fine with the print demo, so I tried plugging that into my test code above, as such:

          findrepltuple = [
              ( "r'0x0x0', ur'•וו'"),
              ( "r'this', ur'that'"),
              ]
          
          num_selections = editor.getSelections()
          
          if num_selections == 1 and editor.getSelectionNStart(0) == editor.getSelectionNEnd(0):
              start_pos = 0
              end_pos = len(editor.getText()) + 1
              for (find, replace) in findrepltuple:
                  editor.rereplace(findrepltuple, 0, start_pos, end_pos)
          else:
              for sel_nbr in range(num_selections):
                  start_pos = editor.getSelectionNStart(sel_nbr)
                  end_pos = editor.getSelectionNEnd(sel_nbr)
                  for (find, replace) in findrepltuple:
                      editor.rereplace(findrepltuple, 0, start_pos, end_pos)
          

          Also tried editor.rereplace((find, replace), 0, start_pos, end_pos).

          Result for both was error “for (find, replace) in findrepltuple: ValueError: too many values to unpack” from PythonScript.

          Alan KilbornA 2 Replies Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @M Andre Z Eckenrode
            last edited by

            @m-andre-z-eckenrode said in PythonScript ops on selection if any, all text otherwise:
            "r'0x0x0', ur'•וו'"
            "r'this', ur'that'"

            Hmm, outer quotes are not right (delete them).

            I used r prefix on the sample strings in my demo example because typically regular-expressions contain a lot of \ and by using the r prefix the backslashes don’t have to be doubled, leading to easier-to-read strings. If your regexes don’t use backslashes like shown here, you don’t have to use the r prefix.

            So maybe try:

            findrepltuple = [
                ( '0x0x0', u'•וו'),
                ( 'this', u'that'),
                ]
            
            1 Reply Last reply Reply Quote 1
            • Alan KilbornA
              Alan Kilborn @M Andre Z Eckenrode
              last edited by

              @m-andre-z-eckenrode

              Also, this won’t work:

              editor.rereplace(findrepltuple, 0, start_pos, end_pos)

              This function will require a separate find and replace expression.

              So:

              editor.rereplace(find, replace, 0, start_pos, end_pos)

              M Andre Z EckenrodeM 1 Reply Last reply Reply Quote 2
              • M Andre Z EckenrodeM
                M Andre Z Eckenrode @Alan Kilborn
                last edited by

                @alan-kilborn said in PythonScript ops on selection if any, all text otherwise:

                Hmm, outer quotes are not right (delete them).

                Well, they made the online print demo work, anyway.

                If your regexes don’t use backslashes like shown here, you don’t have to use the r prefix.

                Oh, they’re all over the place in my regexes. My standard operating procedure in PythonScript is `editor.rereplace(r’find string’, ur’replace string’).

                @alan-kilborn said in PythonScript ops on selection if any, all text otherwise:

                Also, this won’t work:

                editor.rereplace(findrepltuple, 0, start_pos, end_pos)

                Got it, and now working! Thanks much! Though I do have one gripe, which I can live with, about doing it this was: In my multi-step Regex scripts, because most steps ARE fairly long and complicated and I sometimes need to revise them, I generally precede each one with a comment giving examples of before and after text so it’s easier to zero in on when necessary, but I clearly (that I know of) can’t do that when using a tuple to store all the find/replace expressions. The next best solution that I can think of is to just have all the comments compiled together sequentially, corresponding to the order of the expression pairs, and comments such as # Regex 1 after each pair. Unless anybody has a better suggestion.

                Alan KilbornA 2 Replies Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @M Andre Z Eckenrode
                  last edited by Alan Kilborn

                  @m-andre-z-eckenrode said in PythonScript ops on selection if any, all text otherwise:

                  Unless anybody has a better suggestion.

                  findrepltuple = []
                  
                  #-----------------------------------------------------
                  
                  findrepltuple.append(('0x0x0', u'•וו'))
                  
                  '''
                  as much text as you want about the above 
                  blah blah blah...
                  blah blah blah...
                  blah blah blah...
                  blah blah blah...
                  '''
                  
                  #-----------------------------------------------------
                  
                  findrepltuple.append(('this', u'that'))
                  
                  '''
                  as much text as you want about the above 
                  blah blah blah...
                  blah blah blah...
                  blah blah blah...
                  blah blah blah...
                  '''
                  
                  #-----------------------------------------------------
                  
                  etc.
                  
                  Alan KilbornA 1 Reply Last reply Reply Quote 3
                  • Alan KilbornA
                    Alan Kilborn @M Andre Z Eckenrode
                    last edited by

                    @m-andre-z-eckenrode said in PythonScript ops on selection if any, all text otherwise:

                    I generally precede each one

                    Sorry, I didn’t catch that part so the sample I provided shows the explanatory text AFTER the live-code part, not before. :-(
                    Pretty easy to see how to modify it, though. :-)

                    M Andre Z EckenrodeM 1 Reply Last reply Reply Quote 1
                    • M Andre Z EckenrodeM
                      M Andre Z Eckenrode @Alan Kilborn
                      last edited by

                      @alan-kilborn said in PythonScript ops on selection if any, all text otherwise:

                      findrepltuple = []
                      
                      #-----------------------------------------------------
                      
                      findrepltuple.append(('0x0x0', u'•וו'))
                      
                      '''
                      as much text as you want about the above 
                      blah blah blah...
                      '''
                      

                      Cool, that works. Thanks again.

                      @alan-kilborn said in PythonScript ops on selection if any, all text otherwise:

                      Sorry, I didn’t catch that part so the sample I provided shows the explanatory text AFTER the live-code part, not before.

                      No problem, either way is good.

                      .

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

                        @alan-kilborn said in PythonScript ops on selection if any, all text otherwise:

                        .append((‘0x0x0’, u’•וו'))

                        Note that append is a function call to add something to a list. In this case we are adding a tuple to the list, so that’s why the opening and closing parentheses are doubled – the outer pair is for the function call, the inner pair is the tuple notation.

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