Community
    • Login

    Status bar to display selected word count

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 4 Posters 1.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.
    • sharkwskS
      sharkwsk
      last edited by

      Continuation to:

      https://community.notepad-plus-plus.org/topic/14144/is-there-a-way-to-customize-the-status-bar-to-display-more-helpful-information

      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
      Neil SchipperN PeterJonesP 2 Replies Last reply Reply Quote 0
      • Neil SchipperN
        Neil Schipper @sharkwsk
        last edited by

        @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 contains re.whatever().

        1 Reply Last reply Reply Quote 3
        • PeterJonesP
          PeterJones @sharkwsk
          last edited by

          @sharkwsk ,

          @Neil-Schipper is right on. Looking at Scott’s script from that old discussion, it is using the re object, so the script definitely requires import re

          Many of the regulars here have customized their PythonScript startup.py to automatically import certain libraries, like import sys and import 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 occasional import in the example scripts.)

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

            @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.

            1 Reply Last reply Reply Quote 2
            • sharkwskS
              sharkwsk
              last edited by

              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])
              
              Alan KilbornA 1 Reply Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn @sharkwsk
                last edited by

                @sharkwsk

                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.

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