Shouldn't paste be made respecting correct space (at least) Preference?



  • If I copy some text from a source with different line-endings than my current N++ document has, and then I paste that data into my N++ document, the line-endings get converted to the correct type for my doc.

    However, if I copy some data with tab characters and paste it into my N++ doc that is configured for spaces only (e.g. Python with 4-space indentation), the tab characters remain and I have to manually deal with them. Does it seem reasonable that they should be autoconverted according to current doc settings?

    The same argument might be made going the other direction (spaces -> tabs) but for me that is (a) slightly harder to accept as reasonable and (b) less of a concern since I don’t use tab characters. ;)



  • @Alan-Kilborn said:

    Does it seem reasonable that [tabs] should be autoconverted according to current doc settings?

    Interesting. I had never thought about it that way. I don’t think I would use that feature, but I suppose if it were an option, I wouldn’t lobby against it.

    Personally, I think that converting EOL during paste is bordering on going too far: Maybe that text was different for a reason, and I wanted it included with all its idiosyncrasies. I understand that when typing new text, it really speeds things up to have tab-to-space and indenting automated… but when I paste stuff, I prefer a much more hands-off approach on the part of the app; if I want a conversion, I want to be in control.

    But maybe I just haven’t run across a situation where having to manually convert tabs to spaces has been annoying enough to want it automated during the paste-step.

    I’m actually surprised this post wasn’t accompanied by your pythonscript implementation of the paste-with-autoconvert that you’ve got attached to the Ctrl+V shortcut. :-) (And if you don’t have that written yet… why not write it now? I mean, you know how long a new feature like that would take to be added to the base code, right? ;-) )



  • @PeterJones

    And if you don’t have that written yet… why not write it now?

    I wanted opinions (and I got yours, thx), but yes, here’s a first cut (may be rough…)…LOL:

    pre_paste_pos = editor.getSelectionStart()
    sel_text_len = editor.getSelectionEnd() - pre_paste_pos
    pre_paste_len = editor.getTextLength()
    editor.beginUndoAction()
    editor.paste()
    end_pos_of_pasted_text = pre_paste_pos + (editor.getTextLength() - pre_paste_len - sel_text_len)
    pasted_text = editor.getTextRange(pre_paste_pos, end_pos_of_pasted_text)
    if '\t' in pasted_text:
        leading_tabs_only = True if 0 else False
        editor.rereplace(('^' if leading_tabs_only else '') + '\t+', lambda m: ' ' * len(m.group(0)) * editor.getTabWidth(), 0, pre_paste_pos, end_pos_of_pasted_text)
    editor.endUndoAction()


  • @Alan-Kilborn said:

    here’s a first cut

    Seems reasonable. If I had been implementing it, I might have used the pythonscript to call the Edit > Blank Operations > TAB to space on highlighted text after pasting, to make sure the conversion followed all the same rules as Notepad++… but yours looks like it would work just as well… I don’t know which would be more efficient, but probably what you have. :-)



  • @PeterJones

    call the Edit > Blank Operations > TAB to space

    I tend to do that only when it is something that can’t be done by my code. I like to know what it’s doing for sure and by that I mean I am a total control freak.



  • I like to know what it’s doing for sure and by that I mean I am a total control freak.

    If that means you want to execute a script manually then the following isn’t for you but
    if it means you want to have control when and how, then you just might include more conditions.

    def CheckCorrectTabs(args):
        if args['modificationType'] == MODIFICATIONFLAGS.INSERTCHECK and '\t' in args['text']:
                editor.changeInsertion(args['text'].replace('\t', ' '*editor.getTabWidth()))  # must be callbackSync
    
    editor.callbackSync(CheckCorrectTabs, [SCINTILLANOTIFICATION.MODIFIED])
    

    To be honest, I wasn’t aware that pasting tabs is possible when using “notabs” setup.



  • @Ekopalypse

    Yea, I made it more of a “Paste Special” thing rather than an automagic thing. But thanks for your code and comments. :)



  • @Ekopalypse

    I looked deeper at your little script segment and I have decided that you are the script-master. I have never considered usage of editor.changeInsertion before, but now that I’ve seen your example, it will become another tool in my toolbox. This is what is great about this community.

    In looking at it, I decided to experiment and make it go both ways: either tab-to-space (as you had it), or leading-space-to-tab.

    I don’t know that it will become part of my “always-running” set of things (somewhat still prefer the “paste-special” approach as the general solution), but here’s what I came up with:

    def CheckCorrectTabs2(args):
        if args['modificationType'] == MODIFICATIONFLAGS.INSERTCHECK:
            tab_char = '\t'; tab_width = editor.getTabWidth()
            if editor.getUseTabs():
                editor.changeInsertion(re.sub('^(?:{s})+'.format(s=' '*tab_width), lambda m: tab_char * (len(m.group()) / tab_width), args['text'], 0, re.MULTILINE))
            elif tab_char in args['text']:
                editor.changeInsertion(args['text'].replace(tab_char, ' ' * tab_width))
    
    editor.callbackSync(CheckCorrectTabs2, [SCINTILLANOTIFICATION.MODIFIED])  # must be callbackSync!
    


  • @Alan-Kilborn said:

    CheckCorrectTabs2

    I guess that is not the best name for it, since it doesn’t represent the changed functionality…



  • @Alan-Kilborn

    Thanks, but most of it is stolen from other codes :-D
    But there are so many functions scintilla provide, I assume there are still some Perls out there which we haven’t figured out yet. :-)

    Name, what do you expect? :-D LOL - it gets converted anyway LOL


Log in to reply