PythonScript ops on selection if any, all text otherwise
-
@m-andre-z-eckenrode said in PythonScript ops on selection if any, all text otherwise:
I’m confused. You reference editor.getTextLength(), allegedly in @michael-vincent ’s posted example, but I’m only seeing editor.getText(), which is actually shorter than editor.getLength(). Typo, maybe?
Michael used
len(editor.getText())
which gets a copy of the text and then calculates its length, via Python’s string length determining function.I mentioned that length is available by asking Scintilla directly for just the number, via
editor.getLength()
oreditor.getTextLength()
– no retrieving the text (since it is unnecessary to have the text).Different things, no “typo”.
-
@m-andre-z-eckenrode said in PythonScript ops on selection if any, all text otherwise:
I’d prefer to keep the ability to handle multiple or columnar selections
Well, then I’d refer you back to the code you quoted in your FIRST posting in this thread, as that code handles these types of selections.
Note that if you have “n” selections (and a column block is really “n” selections, where “n” is the number of rows in the selection), you have to do “n” separate
editor.rereplace()
calls – there’s no way to do it in one call. -
@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()
oreditor.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.
-
@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: ...
-
@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.
-
@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 ther
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 ther
prefix.So maybe try:
findrepltuple = [ ( '0x0x0', u'•×•×•'), ( 'this', u'that'), ]
-
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)
-
@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. -
@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.
-
@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. :-) -
@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.
.
-
@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. -