Community
    • 登入

    Marked text manipulation

    已排程 已置頂 已鎖定 已移動 General Discussion
    regexmarkregexdelete textcopy
    54 貼文 15 Posters 58.7k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • guy038G
      guy038
      最後由 guy038 編輯

      Hello, @alan-kilborn and All,

      So, as the result of our discussion :

      This old post    https://community.notepad-plus-plus.org/topic/12710/marked-text-manipulation/8

      have been updated    https://community.notepad-plus-plus.org/topic/19189/need-a-copy-marked-lines-feature/7

      Cheers,

      guy038

      1 條回覆 最後回覆 回覆 引用 2
      • Alan KilbornA
        Alan Kilborn
        最後由 編輯

        Semi-related info:

        Part of this current thread is about how to copy text that one has marked via Marking that requires use of a PythonScript.

        In a POST in another thread, I provide a script that will select text given conditions specified by user input. That selected text can then be copied.

        Similar results thru some different methods; enough to warrant a cross-link, I guess.

        1 條回覆 最後回覆 回覆 引用 2
        • Laura Lucas AldayL
          Laura Lucas Alday
          最後由 編輯

          Can anyone explain quickly how to use the python script? As in which steps I need to do in Notepad++ to run the script.

          PeterJonesP Alan KilbornA 2 條回覆 最後回覆 回覆 引用 0
          • PeterJonesP
            PeterJones @Laura Lucas Alday
            最後由 PeterJones 編輯

            @Laura-Lucas-Alday

            1. Install PythonScript using Plugins > Plugins Admin
            2. Plugins > Python Script > New Script
            3. Paste the contents and save under someName (whatever name you want)
            4. Plugins > Python Script > Scripts > someName
            5. To add a keyboard shortcut for the script:
            6. Plugins > Python Script > Configuration,
            7. select the someName script and Add to the menu
            8. Restart Notepad++
            9. someName will now be in the main Plugins > Python Script menu, not just in the Scripts submenu
            10. Settings > Shortcut Mapper > Plugin Commands will see the script, and you can assign a keyboard shortcut
            1 條回覆 最後回覆 回覆 引用 2
            • Alan KilbornA
              Alan Kilborn @Laura Lucas Alday
              最後由 編輯

              @Laura-Lucas-Alday

              Peter maybe only made it 99.9999% clear. Perhaps the newbie 0.0001% might still be left wondering…

              Peter’s step 4 would be how you actually run the script.

              Be advised that in this particular case, you need to have some red-marked text for the script to operate on (hopefully this part is clear).

              I think there is an open issue that would make this script’s functionality a native part of Notepad++. I will try to find it…

              1 條回覆 最後回覆 回覆 引用 2
              • Alan KilbornA
                Alan Kilborn
                最後由 編輯

                This may be the issue to which I was referring: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/6095

                1 條回覆 最後回覆 回覆 引用 1
                • Alan KilbornA
                  Alan Kilborn
                  最後由 Alan Kilborn 編輯

                  At the time of the earlier discussions, there was no way to copy marked text natively in Notepad++, meaning without resorting to scripting. Now (7.9.1-ish) there is:

                  77ec89fb-3ad9-4602-b8ee-4d574195eebe-image.png

                  Just press the indicated button after you already have marked some text.

                  Note: I have added similar information in this somewhat-related other THREAD.

                  dinkumoilD 1 條回覆 最後回覆 回覆 引用 1
                  • dinkumoilD
                    dinkumoil @Alan Kilborn
                    最後由 編輯

                    @Alan-Kilborn

                    My preferred solution would have been to select all marked text because this provides more flexibility. This way the user could

                    • copy
                    • cut
                    • delete
                    • change

                    the marked search results.

                    To achieve that I wrote the following Lua script (intended to be used with, well, the LuaScript plugin).

                    -- =============================================================================
                    -- Add menu entry to select all items marked by find marks
                    -- =============================================================================
                    
                    npp.AddShortcut("Select Marked", "", function()
                      local indicatorIdx
                      local pos, markStart, markEnd
                      local hasMarkedItems
                    
                      -- Search find marks have indicator number 31
                      SCE_UNIVERSAL_FOUND_STYLE = 31
                    
                      -- Remove selection but do not change cursor position
                      editor.Anchor  = editor.CurrentPos
                      hasMarkedItems = false
                    
                      -- Search find mark indicators and select marked text
                      pos = 0
                      
                      while pos < editor.TextLength do
                        if editor:IndicatorValueAt(SCE_UNIVERSAL_FOUND_STYLE, pos) == 1 then
                          markStart = editor:IndicatorStart(SCE_UNIVERSAL_FOUND_STYLE, pos)
                          markEnd   = editor:IndicatorEnd(SCE_UNIVERSAL_FOUND_STYLE, pos)
                    
                          if editor.SelectionEmpty or not editor.MultipleSelection then
                            editor:SetSelection(markEnd, markStart)
                          else
                            editor:AddSelection(markEnd, markStart)
                          end
                    
                          pos            = markEnd
                          hasMarkedItems = true
                        end
                        
                        pos = pos + 1
                      end
                    
                      -- Scroll last selected element into view. This is only because
                      -- Npp/Scintilla will do that anyway when moving the cursor next time
                      if hasMarkedItems then
                        editor:ScrollCaret()
                      end
                    end)
                    

                    After adding this code to the startup.lua file and restarting Notepad++, the plugin’s submenu provides a new entry Select Marked which can be assigned to a keyboard shortcut.

                    Alan KilbornA C 2 條回覆 最後回覆 回覆 引用 3
                    • Alan KilbornA
                      Alan Kilborn @dinkumoil
                      最後由 編輯

                      @dinkumoil said in Marked text manipulation:

                      select all marked text because this provides more flexibility. This way the user could

                      copy
                      cut
                      delete
                      change

                      That’s a good idea as well, especially for delete and change (although those can be achieved, on a one-off basis, by replace-with-nothing and replace-with something ops).

                      Copy and cut of selected text would jam all of the text together if it were on a selection basis (probably not really useful that way).

                      I noticed this new Copy Marked Text command inserts a line-ending between the individual globs of marked text, which seems to make it useful, although to tell the truth I haven’t had a real application for it myself, yet.

                      dinkumoilD 1 條回覆 最後回覆 回覆 引用 3
                      • dinkumoilD
                        dinkumoil @Alan Kilborn
                        最後由 dinkumoil 編輯

                        @Alan-Kilborn said in Marked text manipulation:

                        Copy and cut of selected text would jam all of the text together if it were on a selection basis (probably not really useful that way).

                        Yes, you are right. Up to now, I’ve used my script only for deleting and editing the selections. But especially being able to multi-edit all occurences of a search term or overwrite them by multi-paste vastly increases productivity.

                        1 條回覆 最後回覆 回覆 引用 1
                        • C
                          chk1 @dinkumoil
                          最後由 chk1 編輯

                          @dinkumoil said in Marked text manipulation:

                          @Alan-Kilborn

                          My preferred solution would have been to select all marked text because this provides more flexibility. This way the user could

                          • copy
                          • cut
                          • delete
                          • change

                          the marked search results.

                          To achieve that I wrote the following Lua script (intended to be used with, well, the LuaScript plugin).

                          // snip
                          

                          After adding this code to the startup.lua file and restarting Notepad++, the plugin’s submenu provides a new entry Select Marked which can be assigned to a keyboard shortcut.

                          Thank you so much for that code, I converted it to Python for fans of the PythonScript plugin:

                          # -*- coding: utf-8 -*-
                          from Npp import editor
                          
                          # https://community.notepad-plus-plus.org/topic/12710/marked-text-manipulation/48?_=1756239096378
                          SCE_UNIVERSAL_FOUND_STYLE = 31
                          
                          def mark_to_multi():
                              has_marked_items = False
                              pos = 0
                              while pos < editor.getTextLength():
                                  if editor.indicatorValueAt(SCE_UNIVERSAL_FOUND_STYLE, pos) == 1:
                                      mark_start = editor.indicatorStart(SCE_UNIVERSAL_FOUND_STYLE, pos)
                                      mark_end   = editor.indicatorEnd(SCE_UNIVERSAL_FOUND_STYLE, pos)
                                      
                                      if editor.getSelectionEmpty() or not editor.getMultipleSelection():
                                          editor.setSelection(mark_end, mark_start)
                                      else:
                                          editor.addSelection(mark_end, mark_start)
                          
                                      pos = mark_end
                                      has_marked_items = True
                                      editor.setIndicatorCurrent(SCE_UNIVERSAL_FOUND_STYLE)
                                      editor.indicatorClearRange(mark_start, mark_end-mark_start)
                          
                                  pos += 1
                              if has_marked_items:
                                  editor.scrollCaret()
                          
                          mark_to_multi()
                          

                          The editor I used previously had a handy shortcut for this, I could just hit Alt+Enter in the search bar and it would immediately make all search results into cursors. Is there a reasonably easy way to replicate this in Notepad++? I use this function a lot.

                          Terry RT 1 條回覆 最後回覆 回覆 引用 0
                          • C chk1 referenced this topic on
                          • Terry RT
                            Terry R @chk1
                            最後由 編輯

                            @chk1 said in Marked text manipulation:

                            I could just hit Alt+Enter in the search bar and it would immediately make all search results into cursors.

                            Do you mean make the search results into “links” to the actual text in the file? Because if so, then you should understand that already happens.

                            The mouse cursor doesn’t indicate that they are links, but in the “Search results” window that appears, double clicking on any search result line will make the main window move lines until that search result shows.

                            Terry

                            C 1 條回覆 最後回覆 回覆 引用 0
                            • C
                              chk1 @Terry R
                              最後由 編輯

                              @Terry-R Thanks for the quick response, but the Search results window is not what I mean. When I press Alt+Enter (or an alternative key combo) I want every search result to be selected via multi-editing in the editor window, and not in a separate window or sidebar.

                              Right now, with help of the script, I open the Marks search (Ctrl+M), enter my search term and press “Mark all”. Then I run the script to convert all marks to multi-edit selections. I want to reduce these extra steps if possible, i.e. in the best case: Ctrl+F, enter search word, hit Alt+Enter.

                              CoisesC 1 條回覆 最後回覆 回覆 引用 0
                              • CoisesC
                                Coises @chk1
                                最後由 編輯

                                @chk1 said in Marked text manipulation:

                                Right now, with help of the script, I open the Marks search (Ctrl+M), enter my search term and press “Mark all”. Then I run the script to convert all marks to multi-edit selections. I want to reduce these extra steps if possible, i.e. in the best case: Ctrl+F, enter search word, hit Alt+Enter.

                                It might not be worth it for your case, but the Search dialog in the Columns++ plugin has Select All as an option on the dropdown menu on the Count button.

                                You can assign a shortcut to Columns++ | Search in Notepad++ shortcut mapper; but at present, there is no straightforward way to open the Count button menu using only the keyboard.

                                1 條回覆 最後回覆 回覆 引用 0
                                • 第一個貼文
                                  最後的貼文
                                The Community of users of the Notepad++ text editor.
                                Powered by NodeBB | Contributors