• Login
Community
  • Login

Status bar to display selected word count

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 4 Posters 1.1k 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.
  • S
    sharkwsk
    last edited by Nov 8, 2021, 11:01 AM

    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
    N P 2 Replies Last reply Nov 8, 2021, 11:36 AM Reply Quote 0
    • N
      Neil Schipper @sharkwsk
      last edited by Nov 8, 2021, 11:36 AM

      @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
      • P
        PeterJones @sharkwsk
        last edited by Nov 8, 2021, 2:11 PM

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

        A 1 Reply Last reply Nov 8, 2021, 2:32 PM Reply Quote 4
        • A
          Alan Kilborn @PeterJones
          last edited by Nov 8, 2021, 2:32 PM

          @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
          • S
            sharkwsk
            last edited by Nov 9, 2021, 12:32 AM

            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])
            
            A 1 Reply Last reply Nov 9, 2021, 12:36 PM Reply Quote 0
            • A
              Alan Kilborn @sharkwsk
              last edited by Nov 9, 2021, 12:36 PM

              @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
              • D Dimitri Sudomoin referenced this topic on Nov 10, 2022, 3:36 PM
              1 out of 6
              • First post
                1/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors