Community
    • Login

    Select, grab and move - faster way

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    14 Posts 4 Posters 2.4k 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.
    • Alan KilbornA
      Alan Kilborn
      last edited by

      Here’s a Pythonscript that implements the functionality:

      from Npp import editor, SCINTILLANOTIFICATION, NOTIFICATION
      
      try:
          install_state
      except NameError:
          install_state = 0  # 0 = first run, 1 = active, 2 = inactive
      
      if install_state == 0:
      
          paste_upon_mouse_stopped = False
          pos_of_cut = -1
          cut_word_length = 0
          LINE_NUMBER_MARGIN_DEFAULT_BACK_RGB = editor.styleGetBack(STYLESCOMMON.LINENUMBER)
          BLUEISH_RGB = (135, 206, 250)  # margin color when paste is pending
      
          def dwell_start_callback(args):
              if install_state == 2: return
              global paste_upon_mouse_stopped
              if paste_upon_mouse_stopped:
                  pos_of_mouse_stop = args['position']
                  if pos_of_mouse_stop != -1:  # if mouse cursor points beyond end-of-line or other non-position'd spot
                      if pos_of_mouse_stop < pos_of_cut or pos_of_mouse_stop > pos_of_cut + cut_word_length:
                          editor.setSelectionStart(pos_of_mouse_stop)
                          editor.setSelectionEnd(pos_of_mouse_stop)
                          editor.paste()
                          editor.setSelectionStart(pos_of_mouse_stop)
                          editor.styleSetBack(STYLESCOMMON.LINENUMBER, LINE_NUMBER_MARGIN_DEFAULT_BACK_RGB)
                          paste_upon_mouse_stopped = False
      
          def double_click_callback(args):
              if install_state == 2: return
              ss = editor.getSelectionStart()
              se = editor.getSelectionEnd()
              if ss != se:
                  global cut_word_length, pos_of_cut, paste_upon_mouse_stopped
                  cut_word_length = len(editor.getSelText())
                  editor.cut()
                  pos_of_cut = editor.getCurrentPos()
                  paste_upon_mouse_stopped = True
                  editor.styleSetBack(STYLESCOMMON.LINENUMBER, BLUEISH_RGB)
      
          editor.setMouseDwellTime(300)  # set a more reasonable value than the default (10000000 ms)
      
          editor.callback(dwell_start_callback, [SCINTILLANOTIFICATION.DWELLSTART])
          editor.callback(double_click_callback, [SCINTILLANOTIFICATION.DOUBLECLICK])
      
          install_state = 1
      
      else:
      
          install_state = 2 if install_state == 1 else 1
      

      It turns the line number margin area blue while the functionality is “in progress”. This was kind of interesting to experiment with, not sure of its general usefulness…

      BTW, once the script is run it remains “on” and available. Running it a second time will turn it “off” without really uninstalling it. A third run will turn it back on, etc.

      1 Reply Last reply Reply Quote 5
      • learner9753L
        learner9753
        last edited by

        Thank you very much Bro!
        Besides: I’m very noob in python scripting in notepad++… I tried to run this by link text (Python Script). I even do not know if this is apropriate plugin for that. I have installed Python in my computer already. I tried to install this plugin through normal executable and I do not see this in my plugin list… Excuse me ;)

        Meta ChuhM 1 Reply Last reply Reply Quote 1
        • Meta ChuhM
          Meta Chuh moderator @learner9753
          last edited by Meta Chuh

          welcome to the notepad++ community, @learner9753

          here is the Guide How to install the PythonScript plugin on Notepad++ 7.6.3, 7.6.4 and above

          1 Reply Last reply Reply Quote 3
          • learner9753L
            learner9753
            last edited by

            Thank you, thank you very much! Everything goes perfectly well :) This script is great! Now my workflow improves heavily :D

            1 Reply Last reply Reply Quote 2
            • guy038G
              guy038
              last edited by

              Hello, @learner9753, @alan-kilborn, @meta-chuh and All,

              @alan-kilborn,

              Your script is really surprising, as it deals with mouse gestures ! This gives it a dynamic look, in contrast to most Python scripts, which provide, obviously, static results ;-))

              Now, I think that it would be very useful to add a leading and/or a trailing string to the present cut selection ! I don’t mean dialogs to enter these string values. But, simply, two variables, let’s say prefix and suffix ;-)). For instance, with, the two assignments :

              prefix = "......"
              
              suffix = " €"
              

              and the original text :

              Article A
              Article B
              Article C
              120
              270
              53
              

              We would get, after 3 mouse gestures :

              Article A......120 €
              Article B......270 €
              Article C......53 €
              
              
              
              

              What’s your feeling about it ?


              @learner9753,

              Starting with your example :

              1|w| Na początku stworzył Bóg niebo i ziemię.
              |7225| |1254| |430| |853| |8064| |853| |776|
              
              and the result :
              
              1|w| Na początku|7225| stworzył|1254| Bóg|430| [-]|853| niebo|8064| i|853| ziemię.|776|
              

              Let’s suppose that any set of characters, surrounded by spaces, is always associated with a tagging number, of the next line. Then we could organize your example as below :

               1       Na       początku       stworzył       Bóg       [-]       niebo       i       ziemię.
              |w|      ||        |7225|         |1254|       |430|     |853|     |8064|     |853|      |776|
              

              Notes :

              • I just inserted some space characters, in order to see the associated zones clearly !

              • You’ve certainly noticed :

                • That an empty tagging number || have been added and associated to the word Na, of your example

                • The string [-] have been added, in order to correspond with the 853 tagging number of your example

              Now, this template of text could be easily handled and modified with a regular expression S/R !


              So, assuming, for instance, the original text, below, pasted in a new tab :

              1 Na początku stworzył Bóg [-] niebo
              |w| || |7225| |1254| |430| |853| |8064|
              
              Some text
              indepedent of the
              main zones to process
              
              
              
              			1 Na początku stworzył
                    |w| || |7225| |1254|
              
              Other
              rubbish text
              between
              
              
              1 Na początku stworzył Bóg [-] niebo i ziemię.
              |w| || |7225| |1254| |430| |853| |8064| |853| |776|
              
              • Open the N++ Replace dialog ( Ctrl + H )

              SEARCH (?-s)^((.+\|)?\h*[^\h\r\n]+)(.*\R\h*)(\|.*?\|)\h*

              REPLACE \1\4\3

              • Click on the Wrap around option

              • Choose the Regular expression search mode

              Now, click on the Replace All button, exclusively, several times, till you see the line Replace All: 0 occurrence were replaced.. ( You may also use the ALT + A shortcut ! )

              Note that the number of hits on the Replace All button is equal to the value : 1 + number of tags of the longest line of tagging numbers of your file !

              You should get the text below :

              1|w| Na|| początku|7225| stworzył|1254| Bóg|430| [-]|853| niebo|8064|
              
              
              Some text
              indepedent of the
              main zones to process
              
              
              
              			1|w| Na|| początku|7225| stworzył|1254|
                    
              
              Other
              rubbish text
              between
              
              
              1|w| Na|| początku|7225| stworzył|1254| Bóg|430| [-]|853| niebo|8064| i|853| ziemię.|776|
              
              • Finally just get rid of the surplus || empty tags ( as, for instance, after the Na word ! )

              Best Regards,

              guy038

              P.S. :

              The last line of the Alan script can be changed into :

              install_state = 3 - install_state  # values 1 or 2
              

              Whatever your programming language, if you have a variable that can only take 2 values a and b, you can always use the simple assignment, below :

              var = ( a + b ) - var

              to express the two possible values :-))

              Indeed:

              • If var is equal to the value a, the next value becomes (a + b) - a, i.e. the value b

              • If var is equal to the value b, the next value becomes (a + b) - b, i.e. the value a

              So, this syntax avoids an useless test. However, as this syntax does not, explicitly, show the two values, you may indicate them in comments, if desired


              For instance, let’s suppose you have a test variable, which should be -11 or +7, only. Then, simply use the simple assignment :

              test = -4 - test  # test = -11 or +7
              

              Just because (-11) + (+7) = - 4 !

              Of course, if you’re using the -1 and +1 values to identify the two states, the above statement just becomes :

              test = - test  # test = - 1 or + 1
              
              Alan KilbornA 1 Reply Last reply Reply Quote 3
              • Alan KilbornA
                Alan Kilborn @guy038
                last edited by

                @guy038 said:

                Your script is really surprising,

                I don’t know about surprising but I did think it was an interesting approach to the problem! :)

                What’s your feeling about it ?

                Sure, go for it! :)

                “install_state”

                Yea, my “install_state” stuff was kind of bad in the script (wrote that part very fast). After posting initially I made it better for myself, even though I may never have use for that script.

                1 Reply Last reply Reply Quote 2
                • guy038G
                  guy038
                  last edited by guy038

                  Hello, @learner9753, @alan-kilborn, @meta-chuh and All,

                  Alan, my post-script, about the install_state variable, was not intended to you, personally, but to everyone, of course. Just an easy trick ;-))


                  Now, I tried to slightly change your script to add a prefix and/or a suffix zones to the selected and cut text on a double mouse click. Of course, changes are quite minimum but, Alan, just tell me if something seems incorrect to you ;-)). Here is my version :

                  # coding= utf-8
                  
                  #  Modified version, on 04-20-2019, of the Alan KILBORN's Python script, which includes TWO preset
                  #  variables to define 2 text areas BEFORE and/or AFTER the text, selected and cut on a DOUBLE-MOUSE click
                  #
                  # ORIGINAL file at location : https://notepad-plus-plus.org/community/topic/17462/select-grab-and-move-faster-way/5
                  
                  from Npp import editor, SCINTILLANOTIFICATION, NOTIFICATION
                  
                  try:
                      install_state
                  except NameError:
                      install_state = 0  # 0 = first run, 1 = active, 2 = inactive
                  
                  if install_state == 0:
                  
                      # ----- START of the CONFIGURATION area -----
                      prefix = '......'
                      suffix = ' €'
                      # ----- END of the CONFIGURATION area -----
                  
                      paste_upon_mouse_stopped = False
                      pos_of_cut = -1
                      cut_word_length = 0
                      LINE_NUMBER_MARGIN_DEFAULT_BACK_RGB = editor.styleGetBack(STYLESCOMMON.LINENUMBER)
                      BLUEISH_RGB = (135, 206, 250)  # margin color when paste is pending
                  
                      def dwell_start_callback(args):
                          if install_state == 2: return
                          global paste_upon_mouse_stopped
                          if paste_upon_mouse_stopped:
                              pos_of_mouse_stop = args['position']
                              if pos_of_mouse_stop != -1:  # if mouse cursor points beyond end-of-line or other non-position'd spot
                                  if pos_of_mouse_stop < pos_of_cut or pos_of_mouse_stop > pos_of_cut + cut_word_length:
                                      editor.setSelectionStart(pos_of_mouse_stop)
                                      editor.setSelectionEnd(pos_of_mouse_stop)
                                      editor.addText(prefix)
                                      editor.paste()
                                      editor.addText(suffix)
                                      editor.setSelectionStart(pos_of_mouse_stop)
                                      editor.styleSetBack(STYLESCOMMON.LINENUMBER, LINE_NUMBER_MARGIN_DEFAULT_BACK_RGB)
                                      paste_upon_mouse_stopped = False
                  
                      def double_click_callback(args):
                          if install_state == 2: return
                          if editor.getSelectionStart() != editor.getSelectionEnd():
                              global cut_word_length, pos_of_cut, paste_upon_mouse_stopped
                              cut_word_length = len(editor.getSelText())
                              editor.cut()
                              pos_of_cut = editor.getCurrentPos()
                              paste_upon_mouse_stopped = True
                              editor.styleSetBack(STYLESCOMMON.LINENUMBER, BLUEISH_RGB)
                  
                      editor.setMouseDwellTime(300)  # set a more reasonable value than the default (10000000 ms)
                  
                      editor.callback(dwell_start_callback, [SCINTILLANOTIFICATION.DWELLSTART])
                      editor.callback(double_click_callback, [SCINTILLANOTIFICATION.DOUBLECLICK])
                  
                      install_state = 1
                  
                  else:
                  
                      install_state = 3 - install_state
                  

                  Cheers,

                  guy038

                  Alan KilbornA 1 Reply Last reply Reply Quote 2
                  • Alan KilbornA
                    Alan Kilborn @guy038
                    last edited by

                    @guy038 said:

                    Alan, just tell me if something seems incorrect to you

                    Seems reasonable – is there any problem with it when you run it?

                    1 Reply Last reply Reply Quote 1
                    • guy038G
                      guy038
                      last edited by

                      @alan-kilborn,

                      None ! It does what I was expected to ;-)) I was thinking about syntax : may be, an other function than editor.addText() or… ?

                      Anyway, thank you for your offer to help !

                      Best regards,

                      guy038

                      1 Reply Last reply Reply Quote 2
                      • learner9753L
                        learner9753
                        last edited by

                        Hello @guy038
                        Thanks for your help and tips. But this text was only an example. This kind of work is only to do “by hand”, because things are not always so simple. Numbers are not always matching text with that simple order as in my example…

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors