Community
    • Login

    Copying highlighted text lines from 1 Notepad++ file to another

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    22 Posts 5 Posters 3.7k 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 @George Rogers
      last edited by

      @George-Rogers

      Okay, so here we go. We’ve got TWO scripts:

      • CopySpecialForColorStyles.py
      • PasteSpecialForColorStyles.py

      Somewhat obviously, you need to do a “copy special” before you do a “paste special”. If you do a normal Copy and follow that with a “paste special”, you will get the result from the most-recent “copy special”, not from the normal Copy. Again, all fairly obvious.

      Perhaps assign a keycombo to each – suggest Shift+Ctrl+c and Shift+Ctrl+v – but try the scripts out via the menus first, to make sure they run and have the desired effect.

      As a bonus, one could use this pair of scripts to manipulate a “second” text clipboard, completely independent of the real clipboard, and never even use the style/color copying ability. Just cuz sometimes you NEED 2 clipboards!

      If you need help with the Pythonscript side of things (installing it, creating scripts, assigning keycombos to run scripts, etc.) just say so.

      Here’s CopySpecialForIndicatorColors.py:

      # -*- coding: utf-8 -*-
      
      from Npp import editor, notepad
      
      class CSFCS(object):
      
          def __init__(self): self._copy_info_dict = {}
      
          # based on https://stackoverflow.com/questions/17867380/how-to-find-tuples-that-overlap-a-given-range-tuple
          def overlap(self, ordered_tuples_list, search_tup):
              # the [0] element of the tuples are included in the set
              # the [1] element of the tuples are NOT included in the set
              overlapping_tups_list = []
              for tup in ordered_tuples_list:
                  if search_tup[1] - 1 < tup[0]: break
                  if tup[0] <= search_tup[0] <= tup[1] - 1:
                      overlapping_tups_list.append(tup)  # start falls inside
                  elif tup[0] <= search_tup[1] - 1 <= tup[1] - 1:
                      overlapping_tups_list.append(tup)  # ending falls inside
                  elif tup[0] > search_tup[0] and tup[1] - 1 < search_tup[1] - 1:
                      overlapping_tups_list.append(tup)  # entirely inside
              return overlapping_tups_list
      
          def highlight_indicator_range_tups_generator(self, indicator_number):
              '''
              the following logic depends upon behavior that isn't exactly documented;
              it was noticed that calling editor.indicatorEnd() will yield the "edge"
              (either leading or trailing) of the specified indicator greater than the position
              specified by the caller
              this is definitely different than the scintilla documentation:
              "Find the start or end of a range with one value from a position within the range"
              '''
              if editor.indicatorEnd(indicator_number, 0) == 0:
                  return
              indicator_end_pos = 0  # set special value to key a check the first time thru the while loop
              while True:
                  if indicator_end_pos == 0 and editor.indicatorValueAt(indicator_number, 0) == 1:
                      # we have an indicator starting at position 0!
                      # when an indicator highlight starts at position 0, editor.indicatorEnd()
                      #  gives us the END of the marking rather than the beginning;
                      #  have to compensate for that:
                      indicator_start_pos = 0
                  else:
                      indicator_start_pos = editor.indicatorEnd(indicator_number, indicator_end_pos)
                  indicator_end_pos = editor.indicatorEnd(indicator_number, indicator_start_pos)
                  if indicator_start_pos == indicator_end_pos: break  # no more matches
                  yield (indicator_start_pos, indicator_end_pos)
      
          def get_most_recent_copy_special_data(self): return self._copy_info_dict
      
          def run(self):
      
              if editor.getSelections() > 1:
                  notepad.messageBox('Currently not supporting multiple selection copy.', '')
                  return
      
              if editor.getSelectionEmpty():
                  notepad.messageBox('Nothing selected to copy.', '')
                  return
      
              sel_start_pos = editor.getSelectionStart()
              sel_end_pos = editor.getSelectionEnd()
      
              self._copy_info_dict = { 'text' : editor.getSelText() }
      
              # the following names are from the N++ source code:
              SCE_UNIVERSAL_FOUND_STYLE_EXT1 = 25  # Style #1
              SCE_UNIVERSAL_FOUND_STYLE_EXT2 = 24  # Style #2
              SCE_UNIVERSAL_FOUND_STYLE_EXT3 = 23  # Style #3
              SCE_UNIVERSAL_FOUND_STYLE_EXT4 = 22  # Style #4
              SCE_UNIVERSAL_FOUND_STYLE_EXT5 = 21  # Style #5
              SCE_UNIVERSAL_FOUND_STYLE      = 31  # redmarking style
      
              indicator_list = [
                  SCE_UNIVERSAL_FOUND_STYLE_EXT1,
                  SCE_UNIVERSAL_FOUND_STYLE_EXT2,
                  SCE_UNIVERSAL_FOUND_STYLE_EXT3,
                  SCE_UNIVERSAL_FOUND_STYLE_EXT4,
                  SCE_UNIVERSAL_FOUND_STYLE_EXT5,
                  SCE_UNIVERSAL_FOUND_STYLE,
                  ]
      
              for ind in indicator_list:
      
                  ind_tup_list = list(self.highlight_indicator_range_tups_generator(ind))
      
                  if len(ind_tup_list):
      
                      overlapping_tup_list = self.overlap(ind_tup_list, (sel_start_pos, sel_end_pos))
      
                      if len(overlapping_tup_list) > 0:
      
                          if sel_start_pos > overlapping_tup_list[0][0]:
                              # adjust for selection starting in middle of a colored block of text:
                              overlapping_tup_list[0] = (sel_start_pos, overlapping_tup_list[0][1])
                          if sel_end_pos < overlapping_tup_list[-1][1]:
                              # adjust for selection ending in middle of a colored block of text:
                              overlapping_tup_list[-1] = (overlapping_tup_list[-1][0], sel_end_pos)
      
                          offset_adjusted_overlapping_tup_list = []
                          for (start, end) in overlapping_tup_list:
                              offset_adjusted_overlapping_tup_list.append((start - sel_start_pos, end - sel_start_pos))
      
                          if 'indicator' not in self._copy_info_dict: self._copy_info_dict['indicator'] = {}
      
                          self._copy_info_dict['indicator'][ind] = offset_adjusted_overlapping_tup_list
      
      if __name__ == '__main__':
          try:
              csfcs
          except NameError:
              csfcs = CSFCS()
          csfcs.run()
      

      Here’s PasteSpecialForIndicatorColors.py:

      # -*- coding: utf-8 -*-
      
      from Npp import editor, notepad
      
      class PSFCS(object):
      
          def __init__(self): pass
      
          def indicator_fill_pos1_to_pos2(self, indicator, pos1, pos2):
              editor.setIndicatorCurrent(indicator)
              editor.indicatorFillRange(pos1, pos2 - pos1)
      
          def run(self):
      
              copy_special_info_dict = csfcs.get_most_recent_copy_special_data()
              if len(copy_special_info_dict) == 0:
                  notepad.messageBox('No previous copy of special data -- nothing to paste!', '')
                  return
      
              if editor.getSelections() > 1:
                  notepad.messageBox('Currently not supporting multiple selection paste.', '')
                  return
      
              text_to_paste = copy_special_info_dict['text']
      
              insert_pos = editor.getSelectionStart()
      
              editor.beginUndoAction()
              editor.deleteRange(insert_pos, editor.getSelectionEnd() - insert_pos)  # remove any currently selected text before pasting in new text
              editor.insertText(insert_pos, text_to_paste)
              editor.endUndoAction()
      
              if 'indicator' in copy_special_info_dict:
      
                  for ind in copy_special_info_dict['indicator']:
      
                      offset_tup_list_for_this_ind = copy_special_info_dict['indicator'][ind]
      
                      if len(offset_tup_list_for_this_ind):
      
                          for (start_offset, end_offset) in offset_tup_list_for_this_ind:
                              self.indicator_fill_pos1_to_pos2(ind, insert_pos + start_offset, insert_pos + end_offset)
      
              # leave inserted text selected:
              editor.setSelection(insert_pos + len(text_to_paste), insert_pos)
      
      if __name__ == '__main__':
      
          try:
              csfcs
          except NameError:
              notepad.messageBox('No previous copy of special data -- nothing to paste!', '')
          else:
              try:
                  psfcs
              except NameError:
                  psfcs = PSFCS()
              psfcs.run()
      
      Alan KilbornA 1 Reply Last reply Reply Quote 3
      • Alan KilbornA
        Alan Kilborn @Alan Kilborn
        last edited by Alan Kilborn

        @Alan-Kilborn said in Copying highlighted text lines from 1 Notepad++ file to another:

        And of course I botched the names of the scripts, being inconsistent about them both (the names of the scripts in the two-bullet list are different from the names immediately before each script). Oh, well…

        (Personally I like the names in the two-bullet list better)

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

          Hello, @george-rogers, @alan-kilborn, @peterjones and All,

          Alan, just tried this two scripts and, indeed, it fully copy/pastes the 5 style tokens, as well as the mark style and the Incremental search style ;-)) Well done ! We even have a 2nd clipboard as a bonus !

          However, I don’t see the immediate benefit for @george-rogers as, obviously, all these highlightings are lost for, both, the original and the copied file, once, you start Notepad++, again !


          Alan, I can see that your “status” as a Python script writer, monopolizes most of your time. But, maybe, you can find a few minutes to devote to the proposals I’ve made to you about this other Python script :

          https://community.notepad-plus-plus.org/post/54357

          Many thanks in advance !

          Best Regards,

          guy038

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

            @guy038 said in Copying highlighted text lines from 1 Notepad++ file to another:

            However, I don’t see the immediate benefit for @george-rogers as, obviously, all these highlightings are lost for, both, the original and the copied file, once, you start Notepad++, again !

            True. I’m working on a “solution” to this as well, see HERE.

            Alan, I can see that your “status” as a Python script writer, monopolizes most of your time. But, maybe, you can find a few minutes to devote to the proposals I’ve made to you about this other Python script : https://community.notepad-plus-plus.org/post/54357

            Yes, I’m working on this one as well.
            My in-progress version got a big, well, shall we say, “bigger” (more features) than first intended!
            I really need to exercise “scope control”. :-)
            Keep an eye on the thread of that posting in the coming days (weeks?).

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

              @Alan-Kilborn said in Copying highlighted text lines from 1 Notepad++ file to another:

              Keep an eye on the thread of that posting in the coming days (weeks?).

              But note that I’ll probably work to finish the “style save” script FIRST (sorry).
              I try (somewhat unsuccessfully) to pipeline things in FIFO order, without getting “too many” things going at once.

              Alan, I can see that your “status” as a Python script writer, monopolizes most of your time

              Never saw that as a “status” LOL.
              And my general rule is that I don’t take on script-writing for things that I wouldn’t personally use, or possibly for things I wouldn’t use but have some other interesting (to me) aspect.

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

                @guy038 said in Copying highlighted text lines from 1 Notepad++ file to another:

                fully copy/pastes the 5 style tokens, as well as the mark style and the Incremental search style

                Actually, the “incremental search style” isn’t handled by the provided scripts.
                The reason is that it is too “dynamic”.
                The other styles have more “permanence” to them.

                One could certainly add it to the list in the “copy-special” script code, though (no need for it to be in the “paste-special” code). Here are the details about it from the N++ source:

                #define SCE_UNIVERSAL_FOUND_STYLE_INC 28

                George RogersG 1 Reply Last reply Reply Quote 2
                • George RogersG
                  George Rogers @Alan Kilborn
                  last edited by

                  Your time and efforts are Much Appreciated! I am admittedly a Notepad++ newbie when it comes to scripts and customization. Maybe you can point me to a YouTube video or video screenshare which shows me how to integrate these scripts. Thanks Again!

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

                    @George-Rogers

                    Someday we’ll turn the generic procedure into a FAQ.

                    Steps:

                    • Use Plugins admin to install the PythonScript plugin
                    • Menu to Plugins > Python Script > New Script
                    • Provide a (file) name for the script; you don’t need to specify an extension – .py will be added automatically
                    • Press the Save button to close out the Save As dialog box.
                    • At this point your script file will be showing and is empty; copying and pasting one of the scripts from above seems like a fine idea :-)
                    • Save the file
                    • To execute your script, menu to Plugins > Python Script >Scripts > (pick the name of your script)

                    If you can successfully get that far with it, we can explain to you how to bind a keycombo to it to execute the script that way (which is better than menuing for an often-used script).

                    Alan KilbornA 1 Reply Last reply Reply Quote 0
                    • guy038G
                      guy038
                      last edited by

                      Hi, @alan-kilborn and All,

                      when I did the test, I used the maximum number of styles and I didn’t notice that the incremental search style was not copied !

                      Cheers,

                      guy038

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

                        @guy038

                        Actually (thinking more about it), if you want the Incremental Search or even the Smart Highlighting color to be part of the copy/paste, upon pasting it will lose its temporal nature and becomes permanent! So that’s a way to get a few more colors with this technique!

                        Here’s the info for Smart Highlighting :

                        #define SCE_UNIVERSAL_FOUND_STYLE_SMART 29

                        And so, here’s a modification to a section of the “copy special” script to include these:

                        # the following names are from the N++ source code:
                        SCE_UNIVERSAL_FOUND_STYLE_EXT1  = 25  # Style #1
                        SCE_UNIVERSAL_FOUND_STYLE_EXT2  = 24  # Style #2
                        SCE_UNIVERSAL_FOUND_STYLE_EXT3  = 23  # Style #3
                        SCE_UNIVERSAL_FOUND_STYLE_EXT4  = 22  # Style #4
                        SCE_UNIVERSAL_FOUND_STYLE_EXT5  = 21  # Style #5
                        SCE_UNIVERSAL_FOUND_STYLE       = 31  # redmarking style
                        SCE_UNIVERSAL_FOUND_STYLE_INC   = 28  # incremental-search style
                        SCE_UNIVERSAL_FOUND_STYLE_SMART = 29  # smart-highlighting style
                        
                        indicator_list = [
                            SCE_UNIVERSAL_FOUND_STYLE_EXT1,
                            SCE_UNIVERSAL_FOUND_STYLE_EXT2,
                            SCE_UNIVERSAL_FOUND_STYLE_EXT3,
                            SCE_UNIVERSAL_FOUND_STYLE_EXT4,
                            SCE_UNIVERSAL_FOUND_STYLE_EXT5,
                            SCE_UNIVERSAL_FOUND_STYLE,
                            SCE_UNIVERSAL_FOUND_STYLE_INC,
                            SCE_UNIVERSAL_FOUND_STYLE_SMART,
                            ]
                        
                        1 Reply Last reply Reply Quote 3
                        • George RogersG
                          George Rogers
                          last edited by

                          So, I am having issues installing the Python Script plugin (N++ newbie)
                          My N++ version is V7.6.1 with 32-bit
                          N++ > Plugin Admin does not find Python Script at all

                          Followed the instructions here for installing the Python Script plugin
                          https://community.notepad-plus-plus.org/topic/17256/guide-how-to-install-the-pythonscript-plugin-on-notepad-7-6-3-7-6-4-and-above

                          Here is screenshots of my file setup:
                          https://www.screencast.com/t/27yUQwOE
                          https://www.screencast.com/t/i80RUZMCuOd2

                          Screenshot of N++ Plugin Admin
                          https://www.screencast.com/t/8RGSUO0rw

                          Please help me understand where I am going wrong

                          Much Appreciated
                          George

                          Alan KilbornA 1 Reply Last reply Reply Quote 1
                          • Alan KilbornA
                            Alan Kilborn @George Rogers
                            last edited by Alan Kilborn

                            @George-Rogers said in Copying highlighted text lines from 1 Notepad++ file to another:

                            My N++ version is V7.6.1 with 32-bit

                            So I think around the time of v.7.6.1, Plugins Admin was going through some growing pains.
                            That may be causing the trouble.
                            Any issues with updating to v.7.8.6, say?
                            You can certainly stick with the 32-bit version if you want.

                            1 Reply Last reply Reply Quote 1
                            • George RogersG
                              George Rogers
                              last edited by

                              I can give that a shot
                              How would I unwind that update and revert back to V7.6.1 in case I needed to?

                              EkopalypseE 1 Reply Last reply Reply Quote 0
                              • EkopalypseE
                                Ekopalypse @George Rogers
                                last edited by

                                @George-Rogers

                                Personally I would download a zipped version as it can run
                                in parallel to an installed version.

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

                                  @Alan-Kilborn said in Copying highlighted text lines from 1 Notepad++ file to another:

                                  …how to bind a keycombo to it to execute the script that way (which is better than menuing for an often-used script).

                                  Some instructions for that are found in THIS POSTING.

                                  1 Reply Last reply Reply Quote 1
                                  • PeterJonesP PeterJones referenced this topic on
                                  • First post
                                    Last post
                                  The Community of users of the Notepad++ text editor.
                                  Powered by NodeBB | Contributors