<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[PythonScript ops on selection if any, all text otherwise]]></title><description><![CDATA[<p dir="auto">I use PythonScript quite a bit with NPP these days, mostly for frequent regular expressions replacements, but I’m hardly fluent in Python, so generally I search the forums here for examples of scripting similar to what I hope to achieve, then try to adapt it for my own needs. I’ve been hoping for some time to modify my regex scripts to make it so they only operate on selected text IF there is any, and on all text otherwise. I just found this post from 2017, which demonstrates how to operate only on selected text, so that’s a big help for me:</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/scott-sumner" aria-label="Profile: scott-sumner">@<bdi>scott-sumner</bdi></a> said in <a href="/post/23857">Python script to replace on selection</a>:</p>
<blockquote>
<p dir="auto">here’s some code that shows how to do a replace on only the text in one or more selections:</p>
<pre><code>find = 'a'
replace = 'A'
num_selections = editor.getSelections()
for sel_nbr in range(num_selections):
    start_pos = editor.getSelectionNStart(sel_nbr)
    end_pos = editor.getSelectionNEnd(sel_nbr)
    editor.replace(find, replace, 0, start_pos, end_pos)
</code></pre>
</blockquote>
<p dir="auto">Is my selected-or-all goal possible, and can anyone give me an example of that if so?</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/22378/pythonscript-ops-on-selection-if-any-all-text-otherwise</link><generator>RSS for Node</generator><lastBuildDate>Tue, 09 Jun 2026 16:20:09 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/22378.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 12 Jan 2022 14:20:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Thu, 13 Jan 2022 14:09:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72922">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">.append((‘0x0x0’, u’•×•×•'))</p>
</blockquote>
<p dir="auto">Note that <code>append</code> 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.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72934</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72934</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Thu, 13 Jan 2022 14:09:22 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Thu, 13 Jan 2022 14:01:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72922">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<pre><code class="language-z">findrepltuple = []

#-----------------------------------------------------

findrepltuple.append(('0x0x0', u'•×•×•'))

'''
as much text as you want about the above 
blah blah blah...
'''
</code></pre>
</blockquote>
<p dir="auto">Cool, that works. Thanks again.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72923">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">Sorry, I didn’t catch that part so the sample I provided shows the explanatory text AFTER the live-code part, not before.</p>
</blockquote>
<p dir="auto">No problem, either way is good.</p>
<p dir="auto">.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72933</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72933</guid><dc:creator><![CDATA[M Andre Z Eckenrode]]></dc:creator><pubDate>Thu, 13 Jan 2022 14:01:35 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Thu, 13 Jan 2022 02:45:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m-andre-z-eckenrode" aria-label="Profile: m-andre-z-eckenrode">@<bdi>m-andre-z-eckenrode</bdi></a> said in <a href="/post/72921">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">I generally precede each one</p>
</blockquote>
<p dir="auto">Sorry, I didn’t catch that part so the sample I provided shows the explanatory text AFTER the live-code part, not before. :-(<br />
Pretty easy to see how to modify it, though. :-)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72923</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72923</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Thu, 13 Jan 2022 02:45:02 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 23:12:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m-andre-z-eckenrode" aria-label="Profile: m-andre-z-eckenrode">@<bdi>m-andre-z-eckenrode</bdi></a> said in <a href="/post/72921">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">Unless anybody has a better suggestion.</p>
</blockquote>
<pre><code class="language-z">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.
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/72922</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72922</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 12 Jan 2022 23:12:33 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 22:32:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72915">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">Hmm, outer quotes are not right (delete them).</p>
</blockquote>
<p dir="auto">Well, they made the online print demo work, anyway.</p>
<blockquote>
<p dir="auto">If your regexes don’t use backslashes like shown here, you don’t have to use the <code>r</code> prefix.</p>
</blockquote>
<p dir="auto">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’).</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72916">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">Also, this won’t work:</p>
<p dir="auto"><code>editor.rereplace(findrepltuple, 0, start_pos, end_pos)</code></p>
</blockquote>
<p dir="auto">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 <em><strong>ARE</strong></em> 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 <code># Regex 1</code> after each pair. Unless anybody has a better suggestion.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72921</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72921</guid><dc:creator><![CDATA[M Andre Z Eckenrode]]></dc:creator><pubDate>Wed, 12 Jan 2022 22:32:58 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 21:01:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m-andre-z-eckenrode" aria-label="Profile: m-andre-z-eckenrode">@<bdi>m-andre-z-eckenrode</bdi></a></p>
<p dir="auto">Also, this won’t work:</p>
<p dir="auto"><code>editor.rereplace(findrepltuple, 0, start_pos, end_pos)</code></p>
<p dir="auto">This function will require a separate find and replace expression.</p>
<p dir="auto">So:</p>
<p dir="auto"><code>editor.rereplace(find, replace, 0, start_pos, end_pos)</code></p>
]]></description><link>https://community.notepad-plus-plus.org/post/72916</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72916</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 12 Jan 2022 21:01:46 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 20:58:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m-andre-z-eckenrode" aria-label="Profile: m-andre-z-eckenrode">@<bdi>m-andre-z-eckenrode</bdi></a> said in <a href="/post/72912">PythonScript ops on selection if any, all text otherwise</a>:<br />
<code>"r'0x0x0', ur'•×•×•'"</code><br />
<code>"r'this', ur'that'"</code></p>
<p dir="auto">Hmm, outer quotes are not right (delete them).</p>
<p dir="auto">I used <code>r</code> prefix on the sample strings in my demo example because typically regular-expressions contain a lot of <code>\</code> and by using the <code>r</code> 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 <code>r</code> prefix.</p>
<p dir="auto">So maybe try:</p>
<pre><code>findrepltuple = [
    ( '0x0x0', u'•×•×•'),
    ( 'this', u'that'),
    ]
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/72915</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72915</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 12 Jan 2022 20:58:28 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 20:34:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72906">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">Maybe a list of tuples meets the need here?</p>
</blockquote>
<p dir="auto">Well, it <em><strong>LOOKS</strong></em> 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 <a href="https://www.w3schools.com/python/python_tuples_loop.asp" rel="nofollow ugc">this</a> page and tried to use it <a href="https://www.w3schools.com/python/trypython.asp?filename=demo_tuple_loop" rel="nofollow ugc">here</a>, but…</p>
<pre><code>thistuple = [
    ( r'0x0x0', ur'•×•×•'),
</code></pre>
<p dir="auto">…etc. gave me an invalid syntax error with their print demo example. I then tried…</p>
<pre><code>    ( "r'0x0x0', ur'•×•×•'"),
</code></pre>
<p dir="auto">…and that seemed to work fine with the print demo, so I tried plugging that into my test code <a href="/post/72902">above</a>, as such:</p>
<pre><code>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)
</code></pre>
<p dir="auto">Also tried <code>editor.rereplace((find, replace), 0, start_pos, end_pos)</code>.</p>
<p dir="auto">Result for both was error “for (find, replace) in findrepltuple: ValueError: too many values to unpack” from PythonScript.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72912</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72912</guid><dc:creator><![CDATA[M Andre Z Eckenrode]]></dc:creator><pubDate>Wed, 12 Jan 2022 20:34:51 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 18:23:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m-andre-z-eckenrode" aria-label="Profile: m-andre-z-eckenrode">@<bdi>m-andre-z-eckenrode</bdi></a> said in <a href="/post/72902">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">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</p>
</blockquote>
<p dir="auto">Maybe a list of tuples meets the need here?</p>
<pre><code class="language-z">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'),
    ]
</code></pre>
<p dir="auto">Then you could loop over the list:</p>
<pre><code class="language-z">for (find, replace) in my_find_repl_tup_list:
    ...
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/72906</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72906</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 12 Jan 2022 18:23:39 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 18:11:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72903">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">I mentioned that length is available by asking Scintilla directly for just the number, via <code>editor.getLength()</code> or <code>editor.getTextLength()</code> — no retrieving the text (since it is unnecessary to have the text).</p>
</blockquote>
<p dir="auto">Ok, I see now. I missed that distinction. Thanks for the clarification.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72905</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72905</guid><dc:creator><![CDATA[M Andre Z Eckenrode]]></dc:creator><pubDate>Wed, 12 Jan 2022 18:11:41 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 18:07:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m-andre-z-eckenrode" aria-label="Profile: m-andre-z-eckenrode">@<bdi>m-andre-z-eckenrode</bdi></a> said in <a href="/post/72902">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">I’d prefer to keep the ability to handle multiple or columnar selections</p>
</blockquote>
<p dir="auto">Well, then I’d refer you back to the code you quoted in your <em>FIRST</em> posting in this thread, as that code handles these types of selections.</p>
<p dir="auto">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 <code>editor.rereplace()</code> calls – there’s no way to do it in one call.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72904</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72904</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 12 Jan 2022 18:07:55 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 18:05:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m-andre-z-eckenrode" aria-label="Profile: m-andre-z-eckenrode">@<bdi>m-andre-z-eckenrode</bdi></a> said in <a href="/post/72902">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">I’m confused. You reference editor.getTextLength(), allegedly in <a class="plugin-mentions-user plugin-mentions-a" href="/user/michael-vincent" aria-label="Profile: michael-vincent">@<bdi>michael-vincent</bdi></a> ’s posted example, but I’m only seeing editor.getText(), which is actually shorter than editor.getLength(). Typo, maybe?</p>
</blockquote>
<p dir="auto">Michael used <code>len(editor.getText())</code> which gets a copy of the text and then calculates its length, via Python’s string length determining function.</p>
<p dir="auto">I mentioned that length is available by asking Scintilla directly for just the number, via <code>editor.getLength()</code> or <code>editor.getTextLength()</code> – no retrieving the text (since it is unnecessary to have the text).</p>
<p dir="auto">Different things, no “typo”.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72903</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72903</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 12 Jan 2022 18:05:38 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 17:59:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/michael-vincent" aria-label="Profile: michael-vincent">@<bdi>michael-vincent</bdi></a> said in <a href="/post/72897">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<pre><code class="language-Python">if num_selections == 1 and editor.getSelectionNStart(0) == editor.getSelectionNEnd(0):
    start_pos = 0
    end_pos = len(editor.getText()) + 1
    editor.replace(find, replace, 0, start_pos, end_pos)
else:
   …
</code></pre>
</blockquote>
<p dir="auto">Excellent, thanks! I came up with this for a test run, and worked just fine:</p>
<pre><code>num_selections = editor.getSelections()

if num_selections == 1 and editor.getSelectionNStart(0) == editor.getSelectionNEnd(0):
    start_pos = 0
    end_pos = len(editor.getText()) + 1
    editor.rereplace(r'0x0x0', ur'•×•×•', 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)
        editor.rereplace(r'0x0x0', ur'•×•×•', 0, start_pos, end_pos)
</code></pre>
<p dir="auto">But 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 in both (selected and non-selected text) sections. I’m thinking the thing to do would be to move all the actual Regex code to a subroutine, and call that from both sections. In a basic internet search for Python subroutines, all the examples I’ve looked at so far deal with passing parameters to them, and receiving values from them, which I don’t <em><strong>think</strong></em> I need to worry about here.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72899">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">I’ll point out that <code>editor.getLength()</code> is available as well as <code>editor.getTextLength()</code>.</p>
</blockquote>
<p dir="auto">Thanks, though I’m confused. You reference <code>editor.getTextLength()</code>, allegedly in <a class="plugin-mentions-user plugin-mentions-a" href="/user/michael-vincent" aria-label="Profile: michael-vincent">@<bdi>michael-vincent</bdi></a> ’s posted example, but I’m only seeing <code>editor.getText()</code>, which is actually shorter than <code>editor.getLength()</code>. Typo, maybe? Anyway, good to know about the options.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72900">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/michael-vincent" aria-label="Profile: michael-vincent">@<bdi>michael-vincent</bdi></a></p>
<p dir="auto">your example paves the way for handling multi-selection or column-selection with the nebulous <code>else: ...</code> part, but that’s probably more of a complication than is needed for what the OP described.</p>
</blockquote>
<p dir="auto">Actually, I’d prefer to keep the ability to handle multiple or columnar selections, just in case.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72902</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72902</guid><dc:creator><![CDATA[M Andre Z Eckenrode]]></dc:creator><pubDate>Wed, 12 Jan 2022 17:59:28 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 19:02:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alan-kilborn" aria-label="Profile: alan-kilborn">@<bdi>alan-kilborn</bdi></a> said in <a href="/post/72900">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">Apologies but I reworked your example a bit:</p>
</blockquote>
<p dir="auto">None needed - thank you!  I whipped it up quick and <em><strong>knew</strong></em> there was a better way to <a href="https://www.scintilla.org/ScintillaDoc.html#SCI_GETLENGTH" rel="nofollow ugc">get document length</a> (<code>.getLength()</code>) but couldn’t quickly find it in <a href="https://www.scintilla.org/ScintillaDoc.html" rel="nofollow ugc">Scintilla docs</a>.</p>
<p dir="auto">Cheers.</p>
<p dir="auto">--<br />
<em>moderator edit: fixed link</em></p>
]]></description><link>https://community.notepad-plus-plus.org/post/72901</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72901</guid><dc:creator><![CDATA[Michael Vincent]]></dc:creator><pubDate>Wed, 12 Jan 2022 19:02:55 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 16:07:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/michael-vincent" aria-label="Profile: michael-vincent">@<bdi>michael-vincent</bdi></a></p>
<p dir="auto">Apologies but I reworked your example a bit:</p>
<pre><code class="language-z">(start_pos, end_pos) = (editor.getSelectionNStart(0), editor.getSelectionNEnd(0))
if num_selections == 1 and start_pos == end_pos:
    start_pos = 0
    end_pos = len(editor.getText()) + 1  # or simpler  editor.getLength()
editor.replace(find, replace, 0, start_pos, end_pos)
</code></pre>
<p dir="auto">It’s really not changed much, but I think it more fits the model the OP is truly looking for.  Of course, your example paves the way for handling multi-selection or column-selection with the nebulous <code>else: ...</code> part, but that’s probably more of a complication than is needed for what the OP described.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72900</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72900</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 12 Jan 2022 16:07:44 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 15:44:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/michael-vincent" aria-label="Profile: michael-vincent">@<bdi>michael-vincent</bdi></a> said in <a href="/post/72897">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">end_pos = len(editor.getText()) + 1</p>
</blockquote>
<p dir="auto">Works, but I’ll point out that <code>editor.getLength()</code> is available as well as <code>editor.getTextLength()</code>.  I think internally they are the same, though, so who would type the longer version?</p>
<p dir="auto">Interestingly, in just evaluating my above statement that they are the same, I see that <code>.getLength()</code> “Returns the number of bytes in the document” whereas <code>.getTextLength()</code> “Retrieve the number of characters in the document”.  Knowing that for files I work with (UTF-8) the number of bytes and the number of “characters” can definitely be different (bytes &gt;= chars), I tried some tests on files with a lot of multi-byte characters, and the two functions returned the same value.  Some further digging showed that it is always return the number of bytes.  This is a “good thing” because “position” functions return values that are byte (not char) based, and functions like <code>.rereplace()</code> use position…</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72899</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72899</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Wed, 12 Jan 2022 15:44:39 GMT</pubDate></item><item><title><![CDATA[Reply to PythonScript ops on selection if any, all text otherwise on Wed, 12 Jan 2022 14:42:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m-andre-z-eckenrode" aria-label="Profile: m-andre-z-eckenrode">@<bdi>m-andre-z-eckenrode</bdi></a> said in <a href="/post/72896">PythonScript ops on selection if any, all text otherwise</a>:</p>
<blockquote>
<p dir="auto">Is my selected-or-all goal possible, and can anyone give me an example of that if so?</p>
</blockquote>
<p dir="auto">Yes.  Not tested, you could see if the number of selections == 1 (<a href="https://www.scintilla.org/ScintillaDoc.html#SCI_GETSELECTIONS" rel="nofollow ugc">there will always be one</a>) and then test to see if that one has the same start and end position which means there is no selection, just the cursor at that position and then get the range of the entire doc and set start to 0 and end to the returned range.</p>
<pre><code class="language-Python">if num_selections == 1 and editor.getSelectionNStart(0) == editor.getSelectionNEnd(0):
    start_pos = 0
    end_pos = len(editor.getText()) + 1
    editor.replace(find, replace, 0, start_pos, end_pos)
else:
   ...
</code></pre>
<p dir="auto">Cheers.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/72897</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/72897</guid><dc:creator><![CDATA[Michael Vincent]]></dc:creator><pubDate>Wed, 12 Jan 2022 14:42:18 GMT</pubDate></item></channel></rss>