Menu

Console problem - causing npp to crash

Help
Davey
2015-02-24
2015-03-02
<< < 1 2 (Page 2 of 2)
  • Yaron

    Yaron - 2015-02-27

    Hello Dave,

    Thanks for the detailed explanation. It's really kind of you.
    Allow me also to thank you again for the Python Script plugin. And for the Plugin Manager. :)
    Both indispensable.

    "notepad.flash()" would be great.

    Have a nice weekend.

     
  • Yaron

    Yaron - 2015-02-27

    Hello Davey,

    You are really great. Dave's getting a serious contender! :)
    Thank you.

    I've changed highlightFirst() to this:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def highlightFirst():
    matchesArray = []
    editor.search(findExpression, lambda match: matchesArray.append(match.span(0)[0]))

    if len(matchesArray):   # Would be -1 if not found; - Match whole word etc.
        sSel = matchesArray[0]
        eSel = sSel + len(findExpression)
        editor.setSel(sSel, eSel)
    
        if oldPos == sSel or oldPos == eSel:
            flashSel()
    

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose there are 50 occurrences of the expression in the file.
    - Using this method, all 50 occurrences are read/processed.
    - With "editor.setTarget(0, editor.getLength())", only the first occurrence is processed.
    Is that correct?

    EDIT:
    I guess it should be something like:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    editor.search(findExpression, lambda match: matchesArray.append(match.span(0)[0]), None, 0, editor.getLength(), 1)

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    I'll check it.

    EDIT 2:

    editor.search(findExpression, lambda match: matchesArray.append(match.span(0)[0]), 0, 0, editor.getLength(), 1)
    

    Works properly. Only the first match is processed.
    I suppose that 0 for flags means no flags. Correct?

    What does "match.span(0)" do?
    I understand it stores the current match in "matchesArray[0]";- could you please elaborate a bit?

    Regards.

     

    Last edit: Yaron 2015-02-27
    • Davey

      Davey - 2015-03-01

      Hi Yaron

      You are really great. Dave's getting a serious contender! :)
      Thank you.


      Thanks! I'm just enjoying his plugin very much! :)


      Suppose there are 50 occurrences of the expression in the file.
      - Using this method, all 50 occurrences are read/processed.
      - With "editor.setTarget(0, editor.getLength())", only the first occurrence is processed.
      Is that correct?


      I think this is correct. The truth is that this was another reason I didn't want to use this function. But the reason why I didn't mention it is because I am not sure how long it takes and if it would be a noticeable difference. I supposed that if Dave recommended it that it probably wouldn't make such a difference.


      editor.search(findExpression, lambda match: matchesArray.append(match.span(0)[0]), 0, 0, editor.getLength(), 1)
      

      Works properly. Only the first match is processed.
      I suppose that 0 for flags means no flags. Correct?


      Correct


      What does "match.span(0)" do?
      I understand it stores the current match in "matchesArray[0]";- could you please elaborate a bit?


      The search function returns a match object which carries a lot of information
      The actual text found is captured in the group function. The number passed in the function is the captured group number (mainly for regex's that can contain many groups with parentheses)
      The span function holds the start and end positions. It returns a tuple containg the start and end, therefore, to get the start you would use

      match.span(group#)[0=start or 1=end]
      

      All the best!
      Davey

       

      Last edit: Davey 2015-03-01
      • Yaron

        Yaron - 2015-03-01

        Hello Davey,

        Thank you for the excellent explanation. I really appreciate it.

        Understanding now "match.span(0)", I've changed the code to:

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        def highlightFirst():
        matchesArray = []

        # "Match whole word" is ignored; - "oldPos + len(findExpression)" could be endPosition, but I'm not sure it's faster than "editor.getLength()".
        editor.search(findExpression, lambda match: matchesArray.append(match.span(0)), 2, 0, editor.getLength(), 1)    # 2 == Ignore Case.
        
        if len(matchesArray):   # "Match whole word" is ignored; - "if len(matchesArray):" is apparently not necessary.
            editor.setSel(matchesArray[0][0], matchesArray[0][1])
        
            if oldPos == matchesArray[0][0] or oldPos == matchesArray[0][1]:    # findExpression == first occurrence.
                flashSel()
        

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        1) This way, I get both startPos and endPos.
        2) "Match whole word" is always ignored (i.e. even after activating NP's Select and Find Prev/Next, and unlike "searchInTarget"). I suppose it has to do with Dave's explanation.
        3) If 1 is the maxCount argument, only the first match is processed.


        I've come across your script "Swap 2 words".
        I'm about to comment there. :)

        Best regards.

         
        • Davey

          Davey - 2015-03-02

          Hi Yaron,
          My pleasure to help

          1. Great
          2. I think this might have to do with the fact that the flags are taken from the re class. Regex class doesn't give whole word support since you can do with word boundary wildcards. Hence, here too they are not supported. Actually, here is a quote from Dave's help in python plugin ( received with help(editor.search) )

            Flags are the flags from the re module, specifically only re.IGNORECASE has an effect here.

          3. Correct, this would be good for catching the first match, however, catching the last match...

          All the best
          Davey

           
          • Yaron

            Yaron - 2015-03-02

            Hello Davey,

            Thank you. I appreciate it.

            I decided that "Find First" was good enough, and didn't think about implementing "Find Last" with editor.search().
            That's a good point.

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            editor.gotoPos(editor.getLength())
            editor.searchAnchor()
            editor.searchPrev(0, findExpression)
            editor.scrollCaret()

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            I wonder if Dave's warning about "searchInTarget" is relevant to this method as well.

            Best regards.

             
  • Davey

    Davey - 2015-02-27

    Hi Dave
    Thanks a lot! For the fix and the info

    Python plugin 1.1.1 is working like a charm by me
    Seems like you're good to go!

    Regarding editor.search
    thanks for the tips
    So, the flags should be editor.FLAG ?

    Regarding targets,
    Wow - thats very interesting and good to know!

    Regarding the flash
    I agree that it can come in handy.
    And for this situation, the notepad.flash() idea would be ideal!

    Once again, thanks a lot Dave for everything!
    I am using and benefitting from this plugin more and more every day!

    All the best
    Davey

     
  • Davey

    Davey - 2015-02-27

    Hi Dave
    If you're ready to put out a new version...
    Would it be possible to add a way to run the current doc as a python script?
    This would be a big help in testing a script that doesnt't have a shortcut key.

    Another nice thing would be to be able to run the text in the console as a script.
    This would enable writing quick snippets on the fly?

    (The truth is, this probably can be accomplished with a python script that would read the current doc/console, create a new doc with that text, run it... But how do you run a script for n++ from a python script???)

    What do you think about this?

    Thanks
    Davey

    EDIT: I just realized that the console idea isn't possible since you can't write into the console directly. And, anything entered in the command line automatically gets processed...

     

    Last edit: Davey 2015-02-27
  • Yaron

    Yaron - 2015-02-28

    Hello Dave,

    One more question/request before you release the new version:
    My NP interface language is Hebrew (RTL).
    Would it be possible to set the console-output to LTR?
    It's hard to read the output in a RTL layout.

    Thank you.

     
<< < 1 2 (Page 2 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.