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



  • I am having a difficult time with this situation. Every day, I have reports I update in Notepad++ V7.6.1. I need to be able to color highlight some selected text lines and copy that text (with formatting and highlighting intact) to the report done on the following day.

    I do see that NPPExport plugin is installed. So, before I try to copy the selected text lines from one NPP file to another, I navigate to
    Plugins > NPPExport > Copy all formats to clipboard
    then attempt to copy and paste that color highlighted text line, but the formatting is not carried over.

    I can attach a copy of my file if that helps.
    Would be much appreciated if someone could take that NPP file and show a Jing screenshare video of how they are able to copy 2 color highlighted lines from one NPP file to another.

    Much Appreciated
    George



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

    I need to be able to color highlight some selected text lines and copy that text (with formatting and highlighting intact)

    What do you mean by “color highlight some selected text”?
    Something like Search menu > Mark all > Using 3rd style
    If so, then I don’t believe it is reasonably possible to copy that text and paste it anywhere else with that marking/styling intact.



  • Hi Alan,

    Yes, that is exactly what I mean.
    Here is a video screenshare showng how I color highlight selected lines

    [https://www.screencast.com/t/SmPskjqS3](link url)
    (best viewed on Chrome after Allowing Flash and refreshing screen)

    So, the question in the context of that video is:
    How do I copy and paste that color highlighted text line from one NPP file to another.

    If I am highlighting in the wrong way to enable this copy and paste, please let me know

    Much Appreciated
    George



  • @George-Rogers ,

    I can confirm that using either the NppExport bundled with Notepad++ (plugin v0.2.9.21) or the slightly newer plugin v0.3.0, it does not behave as expected.

    If I take the perl snippet:

    my $mvar = 1;
    local $lvar = 1;
    state $svar = 1;
    our $ovar = 1;
    

    which properly highlights as Perl, then apply Using 3rd Style; when I then use NppExport > Copy HTML to clipboard or … Copy RTF…, and paste into MS Word (or presumably another HTML-or-RTF-clipboard-aware editor), then I see that it properly copy/pasted the HTML or RichText for the syntax highlighting, but not for the Using 3rd Style.

    b6d6305c-4b79-475e-92e1-a99c1ac1ccff-image.png

    It appears that @chcg has not been on since last Fall, so invoking his name won’t bring him in to answer here.

    Thus, I made feature-request/bug-report NppExporter issue#15, pointing to this discussion.



  • Ok, so I am expecting it to behave in a way that doesn’t actually work.

    What are my other options for making this Copy & Paste of regular text with Color Highlighting intact from one NPP file to another?



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

    What are my other options for making this Copy & Paste of regular text with Color Highlighting intact from one NPP file to another?

    I don’t think you currently have any “great” options open to you.
    Since your source is an N++ document, and your destination is same, I suppose some scripting could be written that would “notice” the coloring in the souce of the copy, and recreate it in the paste destination at paste time.
    Somewhat of a “copy special” followed by a “paste special”.
    If that’s of interest to you, I could see what I could script up; no promises on a super-timely delivery though, as this is all “pro bono” work. :-)
    You’d have to be willing to use the Pythonscript plugin.



  • I’m open to that if you can figure something out and no promises expected

    Much Appreciated
    George



  • @George-Rogers

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

    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-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)



  • 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



  • @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-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.



  • @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



  • 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!



  • @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).



  • 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



  • @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,
        ]
    


  • 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



  • @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.



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


Log in to reply