Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] <Adding an optional option to Auto-indent when Pasting Multiple Lines> #15124

Closed
1 task done
StoopidoMan opened this issue May 11, 2024 · 10 comments
Closed
1 task done

Comments

@StoopidoMan
Copy link

StoopidoMan commented May 11, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Description of the Issue

I entered 2) Blah Blah X while auto-indent option is checked:

How to do X?
                            1) just do the X
                            2) Blah Blah X 

but when I paste something from the clipboard I expect it to look like this:

How to do X?
                            1) just do the X
                            2) Blah Blah X 
                             Random copied text from internet line 1
                             Random copied text from internet line 2
                                    Random copied text from internet line 3
                                    Random copied text from internet line 4

However it get pasted as such:

How to do X?
                            1) just do the X
                            2) Blah Blah X 
                            Random copied text from internet line 1
Random copied text from internet line 2
       Random copied text from internet line 3
       Random copied text from internet line 4

So since I pressed enter after 2) Blah Blah X and then pasted the text, the first pasted line respect the auto-indent, but the subsequent lines does not.

Describe the solution you'd like.

To add Follow auto-indent on paste as a sub-preference of Auto-indent.

☐ Auto-indent 
       ☐ follow auto-indent on paste

So when I paste something from the clipboard it would add the indent for each line pasted as well as the indent that is already part of the pasted text.

Debug Information

Not applicable

Anything else?

another user seem to want this to:
https://community.notepad-plus-plus.org/topic/25295/maintain-indent-while-pasting-multiple-lines/

And I am Stoopid, just an FYI

@alankilborn
Copy link
Contributor

@StoopidoMan

There are some scripts available now in the Community topic you linked to.

@StoopidoMan
Copy link
Author

StoopidoMan commented May 15, 2024

@alankilborn

I added your script and when I ran it, it didn't paste anything, unless if it was in the beginning of the line which is what Notepad++ Ctrl+V already do, and I am not looking for that.

@alankilborn I am sorry to bother you, I am not good with words. Would you please check my comment here because I explained it better than in Community topic, I am stoopid, and dyslexic.

Lastly, I still think this and auto numeration should be added to Notepad++ for users like me who numerate a lot in .txt files.

Having said that, I know rich text editors do that, and I am looking for a good lightweight open source rich text editor that support Tabs like NotePad++ which seems non existent.

This is your script from the community topic (for people who stumble upon this in future)

Removed by @alankilborn request. We Stoopido have souls.

@alankilborn
Copy link
Contributor

alankilborn commented May 15, 2024

First, can you delete the script from the above?
There's no need to have the script in 2 places, and the Community site was where it was posted first, so...please delete it from here.
After you do that I'll respond to the other points you made.

@StoopidoMan
Copy link
Author

First, can you delete the script from the above?

You are scarring me!!! @alankilborn Is your script malicious? why are you asking this!?
I checked it and it doesn't look like it is from my none technical eyes.

I added it here to help others who find this page through search engines, that why I made sure my post have many keywords, this is how I try to give back to the community.
I am sorry, but I am afraid I won't do that until I understand what is going on.

@alankilborn
Copy link
Contributor

You are really living up to your name. Ugh.

@xomx
Copy link
Contributor

xomx commented May 16, 2024

@StoopidoMan
I guess the reason is for easier maintainability - if the script author improves/fixes that script in the future somehow, he can then only do it in one place ...

@alankilborn
Copy link
Contributor

The script as posted on the Community site already had one bug which has since been fixed (there). Since the original script was (stupidly -- sorry!) copied here, the version now here has the original bug. @StoopidoMan -- see the problem you've created?

@StoopidoMan
Copy link
Author

@alankilborn
I am happy to see my stoopidity finally proven publicly. I'm stoopendously grateful to you, and your old script is removed.

I have dyslexia among other conditions which makes it hard for me to read or view thing on NPP community website because it is not disability friendly as it doesn't implement UI element separation and good color contrast, thus I can't read it for a long time.

So, I will reply here with the fixed script as of 2024-08-19 and add a link to the community topic for new updates to help others with similar condition as mine.

PS: I don't want to be a grammar Nazi, but I need to help you as you helped me. I am sorry, but stupidly is a very common misspelling of Stoopidly We learn from our mistakes <3 thank you @alankilborn .

Source: https://community.notepad-plus-plus.org/topic/25295/maintain-indent-while-pasting-multiple-lines/

# -*- coding: utf-8 -*-
from __future__ import print_function

#########################################
#
#  PasteSpecialAtCurrentIndentColumn (PSACIC)
#
#########################################

# references:
#  https://community.notepad-plus-plus.org/topic/25295
#  for newbie info on PythonScripts, see https://community.notepad-plus-plus.org/topic/23039/faq-desk-how-to-install-and-run-a-script-in-pythonscript

#-------------------------------------------------------------------------------

from Npp import *
import os, re
#-------------------------------------------------------------------------------

try:
    editor3h  # third editor, hidden
except NameError:
    editor3h = notepad.createScintilla()

#-------------------------------------------------------------------------------

class PSACIC(object):

    def __init__(self):

        if not editor.canPaste() or not editor3h.canPaste():
            return

        rect_sel_mode = editor.getSelectionMode() in [ SELECTIONMODE.RECTANGLE, SELECTIONMODE.THIN ]
        if rect_sel_mode:
            self.mb('Cannot use this paste-special when in column-block mode.')
            return

        if editor.getSelections() > 1:
            self.mb('Cannot use this paste-special when more than a single caret is active.')
            return

        if len(editor.getSelText()) > 0:
            self.mb('Cannot use this paste-special when text is selected.')
            return

        curr_pos = editor.getCurrentPos()
        curr_line_starting_pos = editor.positionFromLine(editor.lineFromPosition(curr_pos))
        if curr_pos == curr_line_starting_pos:
            # caret at start of line; just do normal/simple paste
            editor.paste()
            return

        curr_line_text_before_caret = editor.getTextRange(curr_line_starting_pos, curr_pos)
        if re.search(r'\S', curr_line_text_before_caret):
            self.mb('Cannot use this paste-special at an insertion point with non-whitespace to its left.')
            return

        clipboard_text = self.clipboard_get_text()
        eol = [ '\r\n', '\r', '\n' ][editor.getEOLMode()]
        if eol not in clipboard_text:
            # no 2nd+ line(s) in data to paste, so nothing to adjust; just do normal/simple paste
            editor.paste()
            return

        clipboard_lines_list = clipboard_text.splitlines()

        # see if there's a common all-whitespace prefix on the clipboard lines
        m = re.search(r'^\s+', os.path.commonprefix(clipboard_lines_list))
        common_ws_prefix_on_clipboard_lines = m.group(0) if m else ''

        # remove any common all-whitespace prefix on the clipboard lines
        clipboard_lines_list = [ re.sub('^' + common_ws_prefix_on_clipboard_lines, '', s) for s in clipboard_lines_list ]

        # prepend the current line's indentation whitespace to all of the clipboard lines:
        first_clipboard_line_text = clipboard_lines_list[0]  # remember the first clipboard line before indenting all the rest
        # add new indentation to all of the modified clipboard lines except the first line:
        clipboard_lines_list = [ re.sub('^', curr_line_text_before_caret, s) for s in clipboard_lines_list[1:] ]
        clipboard_lines_list.insert(0, first_clipboard_line_text)  # put the first line back into the list at its old position

        # do our modified "paste" and set new caret appropriately:
        old_doc_len = editor.getLength()
        editor.insertText(curr_pos, eol.join(clipboard_lines_list) + eol)  # "paste" in the appropriately indented new text
        new_caret_pos = curr_pos + editor.getLength() - old_doc_len
        editor.setSel(new_caret_pos, new_caret_pos)  # set caret at end of "pasted" text; scroll viewport if needed

    def clipboard_get_text(self, eol_mode=None):
        # if eol_mode arg is not-specified, use the current editor's eol mode:
        editor3h.setEOLMode(editor.getEOLMode() if eol_mode is None else eol_mode)
        editor3h.clearAll()
        editor3h.paste()
        retval = editor3h.getText()
        return retval

    def mb(self, msg, flags=0, title=''):  # a message-box function
        return notepad.messageBox(msg, title if title else 'PSACIC', flags)

#-------------------------------------------------------------------------------

if __name__ == '__main__': PSACIC()
# -*- coding: utf-8 -*-
from __future__ import print_function

#########################################
#
#  PasteSpecialAtCurrentIndentColumn (PSACIC)
#
#########################################

# references:
#  https://community.notepad-plus-plus.org/topic/25295
#  for newbie info on PythonScripts, see https://community.notepad-plus-plus.org/topic/23039/faq-desk-how-to-install-and-run-a-script-in-pythonscript

#-------------------------------------------------------------------------------

from Npp import *
import os, re
#-------------------------------------------------------------------------------

try:
    editor3h  # third editor, hidden
except NameError:
    editor3h = notepad.createScintilla()

#-------------------------------------------------------------------------------

class PSACIC(object):

    def __init__(self):

        if not editor.canPaste() or not editor3h.canPaste():
            return

        rect_sel_mode = editor.getSelectionMode() in [ SELECTIONMODE.RECTANGLE, SELECTIONMODE.THIN ]
        if rect_sel_mode:
            self.mb('Cannot use this paste-special when in column-block mode.')
            return

        if editor.getSelections() > 1:
            self.mb('Cannot use this paste-special when more than a single caret is active.')
            return

        if len(editor.getSelText()) > 0:
            self.mb('Cannot use this paste-special when text is selected.')
            return

        curr_pos = editor.getCurrentPos()
        curr_line_starting_pos = editor.positionFromLine(editor.lineFromPosition(curr_pos))
        if curr_pos == curr_line_starting_pos:
            # caret at start of line; just do normal/simple paste
            editor.paste()
            return

        curr_line_text_before_caret = editor.getTextRange(curr_line_starting_pos, curr_pos)
        if re.search(r'\S', curr_line_text_before_caret):
            self.mb('Cannot use this paste-special at an insertion point with non-whitespace to its left.')
            return

        clipboard_text = self.clipboard_get_text()
        eol = [ '\r\n', '\r', '\n' ][editor.getEOLMode()]
        if eol not in clipboard_text:
            # no 2nd+ line(s) in data to paste, so nothing to adjust; just do normal/simple paste
            editor.paste()
            return

        clipboard_lines_list = clipboard_text.splitlines()

        # see if there's a common all-whitespace prefix on the clipboard lines
        m = re.search(r'^\s+', os.path.commonprefix(clipboard_lines_list))
        common_ws_prefix_on_clipboard_lines = m.group(0) if m else ''

        # remove any common all-whitespace prefix on the clipboard lines
        clipboard_lines_list = [ re.sub('^' + common_ws_prefix_on_clipboard_lines, '', s) for s in clipboard_lines_list ]

        # prepend the current line's indentation whitespace to all of the clipboard lines:
        first_clipboard_line_text = clipboard_lines_list[0]  # remember the first clipboard line before indenting all the rest
        # add new indentation to all of the modified clipboard lines except the first line:
        clipboard_lines_list = [ re.sub('^', curr_line_text_before_caret, s) for s in clipboard_lines_list[1:] ]
        clipboard_lines_list.insert(0, first_clipboard_line_text)  # put the first line back into the list at its old position

        # do our modified "paste" and set new caret appropriately:
        old_doc_len = editor.getLength()
        editor.insertText(curr_pos, eol.join(clipboard_lines_list) + eol)  # "paste" in the appropriately indented new text
        new_caret_pos = curr_pos + editor.getLength() - old_doc_len
        editor.setSel(new_caret_pos, new_caret_pos)  # set caret at end of "pasted" text; scroll viewport if needed

    def clipboard_get_text(self, eol_mode=None):
        # if eol_mode arg is not-specified, use the current editor's eol mode:
        editor3h.setEOLMode(editor.getEOLMode() if eol_mode is None else eol_mode)
        editor3h.clearAll()
        editor3h.paste()
        retval = editor3h.getText()
        return retval

    def mb(self, msg, flags=0, title=''):  # a message-box function
        return notepad.messageBox(msg, title if title else 'PSACIC', flags)

#-------------------------------------------------------------------------------

if __name__ == '__main__': PSACIC()
# -*- coding: utf-8 -*-
from __future__ import print_function

#########################################
#
#  PasteSpecialAtCurrentIndentColumn (PSACIC)
#
#########################################

# references:
#  https://community.notepad-plus-plus.org/topic/25295
#  for newbie info on PythonScripts, see https://community.notepad-plus-plus.org/topic/23039/faq-desk-how-to-install-and-run-a-script-in-pythonscript

#-------------------------------------------------------------------------------

from Npp import *
import os, re
#-------------------------------------------------------------------------------

try:
    editor3h  # third editor, hidden
except NameError:
    editor3h = notepad.createScintilla()

#-------------------------------------------------------------------------------

class PSACIC(object):

    def __init__(self):

        if not editor.canPaste() or not editor3h.canPaste():
            return

        rect_sel_mode = editor.getSelectionMode() in [ SELECTIONMODE.RECTANGLE, SELECTIONMODE.THIN ]
        if rect_sel_mode:
            self.mb('Cannot use this paste-special when in column-block mode.')
            return

        if editor.getSelections() > 1:
            self.mb('Cannot use this paste-special when more than a single caret is active.')
            return

        if len(editor.getSelText()) > 0:
            self.mb('Cannot use this paste-special when text is selected.')
            return

        curr_pos = editor.getCurrentPos()
        curr_line_starting_pos = editor.positionFromLine(editor.lineFromPosition(curr_pos))
        if curr_pos == curr_line_starting_pos:
            # caret at start of line; just do normal/simple paste
            editor.paste()
            return

        curr_line_text_before_caret = editor.getTextRange(curr_line_starting_pos, curr_pos)
        if re.search(r'\S', curr_line_text_before_caret):
            self.mb('Cannot use this paste-special at an insertion point with non-whitespace to its left.')
            return

        clipboard_text = self.clipboard_get_text()
        eol = [ '\r\n', '\r', '\n' ][editor.getEOLMode()]
        if eol not in clipboard_text:
            # no 2nd+ line(s) in data to paste, so nothing to adjust; just do normal/simple paste
            editor.paste()
            return

        clipboard_lines_list = clipboard_text.splitlines()

        # see if there's a common all-whitespace prefix on the clipboard lines
        m = re.search(r'^\s+', os.path.commonprefix(clipboard_lines_list))
        common_ws_prefix_on_clipboard_lines = m.group(0) if m else ''

        # remove any common all-whitespace prefix on the clipboard lines
        clipboard_lines_list = [ re.sub('^' + common_ws_prefix_on_clipboard_lines, '', s) for s in clipboard_lines_list ]

        # prepend the current line's indentation whitespace to all of the clipboard lines:
        first_clipboard_line_text = clipboard_lines_list[0]  # remember the first clipboard line before indenting all the rest
        # add new indentation to all of the modified clipboard lines except the first line:
        clipboard_lines_list = [ re.sub('^', curr_line_text_before_caret, s) for s in clipboard_lines_list[1:] ]
        clipboard_lines_list.insert(0, first_clipboard_line_text)  # put the first line back into the list at its old position

        # do our modified "paste" and set new caret appropriately:
        old_doc_len = editor.getLength()
        editor.insertText(curr_pos, eol.join(clipboard_lines_list) + eol)  # "paste" in the appropriately indented new text
        new_caret_pos = curr_pos + editor.getLength() - old_doc_len
        editor.setSel(new_caret_pos, new_caret_pos)  # set caret at end of "pasted" text; scroll viewport if needed

    def clipboard_get_text(self, eol_mode=None):
        # if eol_mode arg is not-specified, use the current editor's eol mode:
        editor3h.setEOLMode(editor.getEOLMode() if eol_mode is None else eol_mode)
        editor3h.clearAll()
        editor3h.paste()
        retval = editor3h.getText()
        return retval

    def mb(self, msg, flags=0, title=''):  # a message-box function
        return notepad.messageBox(msg, title if title else 'PSACIC', flags)

#-------------------------------------------------------------------------------

if __name__ == '__main__': PSACIC()
# -*- coding: utf-8 -*-
from __future__ import print_function

#########################################
#
#  PasteSpecialAtCurrentIndentColumn (PSACIC)
#
#########################################

# references:
#  https://community.notepad-plus-plus.org/topic/25295
#  for newbie info on PythonScripts, see https://community.notepad-plus-plus.org/topic/23039/faq-desk-how-to-install-and-run-a-script-in-pythonscript

#-------------------------------------------------------------------------------

from Npp import *
import os, re
#-------------------------------------------------------------------------------

try:
    editor3h  # third editor, hidden
except NameError:
    editor3h = notepad.createScintilla()

#-------------------------------------------------------------------------------

class PSACIC(object):

    def __init__(self):

        if not editor.canPaste() or not editor3h.canPaste():
            return

        rect_sel_mode = editor.getSelectionMode() in [ SELECTIONMODE.RECTANGLE, SELECTIONMODE.THIN ]
        if rect_sel_mode:
            self.mb('Cannot use this paste-special when in column-block mode.')
            return

        if editor.getSelections() > 1:
            self.mb('Cannot use this paste-special when more than a single caret is active.')
            return

        if len(editor.getSelText()) > 0:
            self.mb('Cannot use this paste-special when text is selected.')
            return

        curr_pos = editor.getCurrentPos()
        curr_line_starting_pos = editor.positionFromLine(editor.lineFromPosition(curr_pos))
        if curr_pos == curr_line_starting_pos:
            # caret at start of line; just do normal/simple paste
            editor.paste()
            return

        curr_line_text_before_caret = editor.getTextRange(curr_line_starting_pos, curr_pos)
        if re.search(r'\S', curr_line_text_before_caret):
            self.mb('Cannot use this paste-special at an insertion point with non-whitespace to its left.')
            return

        clipboard_text = self.clipboard_get_text()
        eol = [ '\r\n', '\r', '\n' ][editor.getEOLMode()]
        if eol not in clipboard_text:
            # no 2nd+ line(s) in data to paste, so nothing to adjust; just do normal/simple paste
            editor.paste()
            return

        clipboard_lines_list = clipboard_text.splitlines()

        # see if there's a common all-whitespace prefix on the clipboard lines
        m = re.search(r'^\s+', os.path.commonprefix(clipboard_lines_list))
        common_ws_prefix_on_clipboard_lines = m.group(0) if m else ''

        # remove any common all-whitespace prefix on the clipboard lines
        clipboard_lines_list = [ re.sub('^' + common_ws_prefix_on_clipboard_lines, '', s) for s in clipboard_lines_list ]

        # prepend the current line's indentation whitespace to all of the clipboard lines:
        first_clipboard_line_text = clipboard_lines_list[0]  # remember the first clipboard line before indenting all the rest
        # add new indentation to all of the modified clipboard lines except the first line:
        clipboard_lines_list = [ re.sub('^', curr_line_text_before_caret, s) for s in clipboard_lines_list[1:] ]
        clipboard_lines_list.insert(0, first_clipboard_line_text)  # put the first line back into the list at its old position

        # do our modified "paste" and set new caret appropriately:
        old_doc_len = editor.getLength()
        editor.insertText(curr_pos, eol.join(clipboard_lines_list) + eol)  # "paste" in the appropriately indented new text
        new_caret_pos = curr_pos + editor.getLength() - old_doc_len
        editor.setSel(new_caret_pos, new_caret_pos)  # set caret at end of "pasted" text; scroll viewport if needed

    def clipboard_get_text(self, eol_mode=None):
        # if eol_mode arg is not-specified, use the current editor's eol mode:
        editor3h.setEOLMode(editor.getEOLMode() if eol_mode is None else eol_mode)
        editor3h.clearAll()
        editor3h.paste()
        retval = editor3h.getText()
        return retval

    def mb(self, msg, flags=0, title=''):  # a message-box function
        return notepad.messageBox(msg, title if title else 'PSACIC', flags)

#-------------------------------------------------------------------------------

if __name__ == '__main__': PSACIC()


@alankilborn
Copy link
Contributor

@StoopidoMan

STOP DUPLICATE POSTING OF SCRIPTS FROM OTHER SOURCES HERE!

(I don't know how more plain I can be about this)

REMOVE THE SCRIPT CODE FROM THE PREVIOUS POSTING OR I'M GOING TO REQUEST FROM THE SITE OWNER THAT YOU BE BANNED!

@pryrt
Copy link
Contributor

pryrt commented May 19, 2024

as it doesn't implement UI element separation and good color contrast, thus I can't read it for a long time.

There are 25 different color themes that you can choose from in your account settings at the Community Forum. Are you sure there are none that work for your needs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants