• Login
Community
  • Login

Find and Add To Selection In 7.7

Scheduled Pinned Locked Moved General Discussion
37 Posts 7 Posters 10.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.
  • A
    Alan Kilborn @cipher-1024
    last edited by May 21, 2019, 12:22 AM

    @cipher-1024

    See one of the sub-points of this thread …meaning that you can currently do it with LuaScript.

    Pythonscript will need to be updated to include the functionality, and it just hasn’t been done yet.

    Of course, Notepad++ itself could be updated as well to do it…

    1 Reply Last reply Reply Quote 4
    • C
      cipher-1024
      last edited by May 21, 2019, 2:23 AM

      Thanks Alan, I need to get over to plug-in development more often. Hopefully the Python maintainers are able to get something out soon. Or maybe @dail converted me to Lua!

      1 Reply Last reply Reply Quote 3
      • D
        dinkumoil
        last edited by dinkumoil May 21, 2019, 9:44 AM May 21, 2019, 9:36 AM

        @dail , @cipher-1024 , @Alan-Kilborn and all

        I’ve tried to get the benefit of SCI_MULTIPLESELECTADDNEXT and SCI_MULTIPLESELECTADDEACH following the advice of @dail in this already mentioned thread , but I wasn’t successful.

        I’ve set up the little script of @dail as a LUA Script plugin startup script and restarted Notepad++. I can confirm that after that there was a new entry in the submenu of the LUA Script plugin showing the expected text and keyboard shortcut. I selected a word by double-clicking on it or pressing CTRL+Right.The word became the main selection and all other instances of it in the whole text became marked. Then I pressed the keyboard shortcut assigned to the new Scintilla features, but I got no multiple cursors, nothing happened.

        Of course I’ve used Notepad++ v7.7 and the latest version of the LUA Script plugin, i.e. v0.9 from the provided download link.

        I’ve tried something similar using the NppExec plugin with the bare numeric message codes for SCI_MULTIPLESELECTADDNEXT (2688) and SCI_MULTIPLESELECTADDEACH (2689) but it didn’t work either.

        Has anybody some advice whether there are some Notepad++ settings that might interfere with SCI_MULTIPLESELECTADDNEXT and SCI_MULTIPLESELECTADDEACH to prevent them from working properly?

        E 1 Reply Last reply May 21, 2019, 10:43 AM Reply Quote 2
        • E
          Ekopalypse @dinkumoil
          last edited by May 21, 2019, 10:43 AM

          @dinkumoil
          you need to set target start and end before calling one of the new functions.

          1 Reply Last reply Reply Quote 2
          • D
            dinkumoil
            last edited by dinkumoil May 21, 2019, 11:10 AM May 21, 2019, 11:08 AM

            @Ekopalypse

            Thank you, that helped! I’ve used the NppExecplugin and the sequence:

            1. Select a word.
            2. Send SCI_TARGETWHOLEDOCUMENT (message code 2690) to set Scintilla’s target.
            3. Send SCI_MULTIPLESELECTADDEACH (message code 2689) to multi-edit all instances of the word.
            E 1 Reply Last reply May 21, 2019, 11:13 AM Reply Quote 2
            • E
              Ekopalypse @dinkumoil
              last edited by May 21, 2019, 11:13 AM

              @dinkumoil

              if different findoption should be used you can add a step 2a. SCI_SETSEARCHFLAGS

              1 Reply Last reply Reply Quote 2
              • D
                dinkumoil
                last edited by May 21, 2019, 11:41 AM

                @Ekopalypse

                Thanks for that additional hint, much appreciated!

                Here is my complete NppExec script:

                sci_sendmsg SCI_GETSELECTIONEMPTY
                
                if $(MSG_RESULT) == 1 then
                  sci_sendmsg SCI_WORDRIGHTEXTEND
                endif
                
                sci_sendmsg SCI_SETSEARCHFLAGS SCFIND_WHOLEWORD 
                
                sci_sendmsg 2690
                sci_sendmsg 2689
                

                It automatically selects the word right from the cursor but only if there is no active selection.

                E 1 Reply Last reply May 21, 2019, 11:45 AM Reply Quote 3
                • E
                  Ekopalypse @dinkumoil
                  last edited by May 21, 2019, 11:45 AM

                  @dinkumoil - nice one, thanks for sharing.
                  One question, as I don’t have NppExec currently installed,
                  is it really needed to do SCI_WORDRIGHTEXTEND?

                  From the docs it states If the current selection is empty then select word around caret

                  D 1 Reply Last reply May 21, 2019, 11:49 AM Reply Quote 2
                  • D
                    dinkumoil @Ekopalypse
                    last edited by dinkumoil May 21, 2019, 11:52 AM May 21, 2019, 11:49 AM

                    @Ekopalypse said:

                    is it really needed to do SCI_WORDRIGHTEXTEND?

                    According to my tests - yes.

                    If the SCI_WORDRIGHTEXTEND part is removed from the script, executing it (without an active selection) selects the word right from the cursor and marks all other instances of the word in the whole text but there are no multiple cursors.

                    E 1 Reply Last reply May 21, 2019, 11:56 AM Reply Quote 3
                    • E
                      Ekopalypse @dinkumoil
                      last edited by May 21, 2019, 11:56 AM

                      @dinkumoil - thanks for clarifying.

                      1 Reply Last reply Reply Quote 1
                      • A
                        Alan Kilborn
                        last edited by May 21, 2019, 12:40 PM

                        It automatically selects the word right from the cursor but only if there is no active selection.

                        I tried the NppExec script and I found that if I run it without a selection but with the caret inside or at the start of a word, it will select from the caret to the right to just before the start of the next word, and then fail to give multiple carets; example with caret sitting on second s of highlighted sci_sendmsg:

                        Imgur

                        Starting with an entire word preselected (here the 2nd sci_sendmsg in the file) however works just fine:

                        Imgur

                        Perhaps I miss something about the case where there is no selected text first…

                        1 Reply Last reply Reply Quote 2
                        • D
                          dinkumoil
                          last edited by dinkumoil May 21, 2019, 12:58 PM May 21, 2019, 12:52 PM

                          @all

                          Improved version of my NppExec script:

                          sci_sendmsg SCI_GETSELECTIONEMPTY
                          
                          if $(MSG_RESULT) == 1 then
                            sci_sendmsg SCI_GETCURRENTPOS
                            set local $(CurPos) ~ $(MSG_RESULT)
                          
                            sci_sendmsg SCI_WORDSTARTPOSITION $(CurPos) 1
                          
                            if $(MSG_RESULT) != $(CurPos) then
                              sci_sendmsg SCI_WORDLEFT
                            endif
                          
                            sci_sendmsg SCI_WORDRIGHTEXTEND
                          endif
                          
                          sci_sendmsg SCI_SETSEARCHFLAGS SCFIND_WHOLEWORD 
                          
                          sci_sendmsg 2690
                          sci_sendmsg 2689
                          

                          If the cursor is placed in the middle of a word without an active selection when executing the script, it is moved to the next word boundary to its left before selecting the word to the right of its new position, i.e. the word in whose mid it was placed before.

                          @Alan-Kilborn
                          Oh, I’ve posted before I could even see your posting…

                          A 1 Reply Last reply May 21, 2019, 1:00 PM Reply Quote 3
                          • A
                            Alan Kilborn @dinkumoil
                            last edited by May 21, 2019, 1:00 PM

                            @dinkumoil said:

                            Oh, I’ve posted before I could even see your posting…

                            I tried the NppExec script just above and had the same result as the earlier NppExec script when no selection before running…

                            D 1 Reply Last reply May 21, 2019, 1:03 PM Reply Quote 2
                            • D
                              dinkumoil @Alan Kilborn
                              last edited by May 21, 2019, 1:03 PM

                              @Alan-Kilborn said:

                              I tried the NppExec script just above and had the same result as the earlier NppExec script when no selection before running…

                              It works at my site. Did you restart Notepad++ after updating the script?

                              A 1 Reply Last reply May 21, 2019, 1:10 PM Reply Quote 1
                              • A
                                Alan Kilborn @dinkumoil
                                last edited by May 21, 2019, 1:10 PM

                                @dinkumoil

                                Did you restart Notepad++ after updating the script?

                                No, but I didn’t “save” it, I was just running it as a NppExec temporary script…

                                1 Reply Last reply Reply Quote 1
                                • A
                                  Alan Kilborn
                                  last edited by May 21, 2019, 1:12 PM

                                  Totally just for fun, I managed to replicate the functionality with Pythonscript in its current (1.4) form:

                                  import ctypes
                                  from ctypes.wintypes import HWND
                                  from ctypes import byref, wintypes, Structure, sizeof
                                  
                                  def get_focused_window():
                                      class GUITHREADINFO(Structure):
                                          _fields_ = [
                                              ("cbSize", wintypes.DWORD),
                                              ("flags", wintypes.DWORD),
                                              ("hwndActive", wintypes.HWND),
                                              ("hwndFocus", wintypes.HWND),  # <--- what we really want
                                              ("hwndCapture", wintypes.HWND),
                                              ("hwndMenuOwner", wintypes.HWND),
                                              ("hwndMoveSize", wintypes.HWND),
                                              ("hwndCaret", wintypes.HWND),
                                              ("rcCaret", wintypes.RECT)
                                              ]
                                      guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
                                      ctypes.windll.user32.GetGUIThreadInfo(0, byref(guiThreadInfo))
                                      return guiThreadInfo.hwndFocus
                                  
                                  current_view_hwnd = get_focused_window()
                                  
                                  SciLexer = ctypes.WinDLL('SciLexer.dll', use_last_error = True)
                                  Scintilla_DirectFunction = SciLexer.Scintilla_DirectFunction
                                  direct_pointer = SendMessage(current_view_hwnd, 2185, 0, 0)
                                  
                                  editor.setSearchFlags(FINDOPTION.WHOLEWORD | FINDOPTION.MATCHCASE)
                                  
                                  if editor.getSelectionEmpty():
                                      start_of_word_pos = editor.wordStartPosition(editor.getCurrentPos(), True)
                                      end_of_word_pos = editor.wordEndPosition(start_of_word_pos, True)
                                      if start_of_word_pos != end_of_word_pos:
                                          editor.setSelection(end_of_word_pos, start_of_word_pos)
                                  
                                  Scintilla_DirectFunction(direct_pointer, 2689, 1, 0)
                                  
                                  1 Reply Last reply Reply Quote 5
                                  • D
                                    dinkumoil
                                    last edited by dinkumoil May 21, 2019, 1:23 PM May 21, 2019, 1:19 PM

                                    Hmm, I found a strange pitfall with my code.

                                    I use for testing the same snippet of source code @dail used in his posting in the other thread:

                                    static std::string getWordAt(GUI::ScintillaWindow *window, int pos) {
                                      int word_start = window->Call(SCI_WORDSTARTPOSITION, pos, true);
                                      int word_end = window->Call(SCI_WORDENDPOSITION, pos, true);
                                      return getRange(window, word_start, word_end);
                                    }
                                    

                                    If I place the cursor before the word pos near the end of line 1 and press my keyboard shortcut everything works fine. If I do the same with placeing it before the word int near the end of line 1, the script selects the word int and the following space character and nothing more happens. It looks like the sci_sendmsg SCI_WORDRIGHTEXTEND gives a wrong result.

                                    I will try to figure out what’s going on…

                                    EDIT: It’s because int is followed by a space character and pos by opening parenthesis. This would make this feature nearly useless. Gonna do further investigations…

                                    A 1 Reply Last reply May 21, 2019, 1:25 PM Reply Quote 2
                                    • A
                                      Alan Kilborn @dinkumoil
                                      last edited by May 21, 2019, 1:25 PM

                                      @dinkumoil

                                      Yea, if you’ll take note, I had an intentional big amount of spaces to the right of the word I had my caret in for my testing. :)

                                      1 Reply Last reply Reply Quote 1
                                      • D
                                        dinkumoil
                                        last edited by May 21, 2019, 1:31 PM

                                        @Alan-Kilborn

                                        Does your Python script work as intended or is it a problem with the algorithm used?

                                        A 1 Reply Last reply May 21, 2019, 1:34 PM Reply Quote 0
                                        • A
                                          Alan Kilborn @dinkumoil
                                          last edited by May 21, 2019, 1:34 PM

                                          @dinkumoil

                                          Not sure if I understand the q… The PS doesn’t use the “word-right-extend” stuff, which seems to be where the trouble lies with the NppExec scripts presented to this point…

                                          I didn’t do exhaustive testing on the PS, but yea, seems to work as intended.

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