Status bar to display selected word count
-
Continuation to:
I am trying to use the python script, however unable to make it work on Notepadd++ 64-bit version.
I am getting below error when I select texts
File "C:\Users\XXX\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\StatusBarReportingOfSelectionMatchCount.py", line 12, in SBROSMC editor.research(escape(editor.getSelText()), match_found) File "C:\Users\XXX\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\StatusBarReportingOfSelectionMatchCount.py", line 11, in escape return re.sub(search_str, repl_str, s) NameError: global name 're' is not defined
-
@sharkwsk Others are probably better informed than I about the details of the code and what you’re trying to do, but that error typically shows up when a needed python package has not been imported, and so you may need to place the line
import re
before the first code block that containsre.whatever()
. -
@Neil-Schipper is right on. Looking at Scott’s script from that old discussion, it is using the
re
object, so the script definitely requiresimport re
Many of the regulars here have customized their PythonScript
startup.py
to automatically import certain libraries, likeimport sys
andimport re
, which means that we don’t have to have them in all of our individual scripts… but then we forget when publishing an individual script to help a user that they won’t necessarily have those already imported. I think we’ve gotten better at remembering those imports over time, but four years ago was before some of those lessons-learned were taken to heart. (And since we’re human, we still will likely forget the occasionalimport
in the example scripts.) -
@peterjones said in Status bar to display selected word count:
we still will likely forget the occasional import in the example scripts.)
Lately I’ve been testing any scripts I post here in a fresh-portable-plus-Pythonscript extract. It helps to point out such missing things.
-
Thanks @PeterJones, @Neil-Schipper, @Alan-Kilborn for your feedback.
I continued with my search and was able to use the below snippet and copied directly into startup.py script. I also set initialization to ATSTARTUP to automatically load this python script. I am not sure if its elegant, but it works
def callback_sci_UPDATEUI(args): if args['updated'] & UPDATE.SELECTION: matches = [] if editor.getTextLength() < 100000: # don't search "big" files if editor.getSelections() == 1 and not editor.getSelectionEmpty(): try: editor.research(r'\Q' + editor.getSelText() + r'\E', lambda m: matches.append(1)) except: matches = [] l = len(matches) notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, ' ' if l == 0 else '{} occurrence(s) of selected text'.format(l)) editor.callback(callback_sci_UPDATEUI, [SCINTILLANOTIFICATION.UPDATEUI])
-
I took the title of this thread to mean that you wanted to count the words in the current selection, but the script counts the number of occurrences of selected text throughout the entire document. Just pointing out the difference in my interpretation in case any future readers do the same.
-