I found some deficiencies in PasteAsTrueRectBlock.py. There are situations where the desired rectangular blocking can’t be known at paste time, but only at copy time. So I wrote CopyAsTrueRectBlock.py. Running this new script on a single selection, be it a normal selection or a column-based one, will result in a truly rectangular block in the clipboard, which can subsequently be pasted via a normal paste (e.g., ctrl+v). As a bonus, the logic for this script is simpler and shorter than the earlier script. :-)
Here’s CopyAsTrueRectBlock.py:
def CATRB__main(): # (C)opy (A)s (T)rue (R)ectangular (B)lock if not editor.getSelectionEmpty(): if editor.selectionIsRectangle() or editor.getSelections() == 1: line_ending = [ '\r\n', '\r', '\n' ][notepad.getFormatType()] lines_list = editor.getSelText().split(line_ending) if len(lines_list[-1]) == 0: lines_list.pop() # if text ended with line_ending, need to remove empty element at end of list rect_width = 0 if editor.selectionIsRectangle(): rect_anchor = editor.getColumn(editor.getRectangularSelectionAnchor()) rect_anchor += editor.getRectangularSelectionAnchorVirtualSpace() rect_caret = editor.getColumn(editor.getRectangularSelectionCaret()) rect_caret += editor.getRectangularSelectionCaretVirtualSpace() rect_width = abs(rect_anchor - rect_caret) longest_line_length = max([ len(line) for line in lines_list ] + [ rect_width ]) # pad shorter lines out to length of longest: truly_rectangular_lines_list = [ line + ' ' * (longest_line_length - len(line)) for line in lines_list ] remember_active_file = notepad.getCurrentFilename() notepad.new() editor.addText(line_ending.join(truly_rectangular_lines_list) + line_ending) editor.setSel(0, (longest_line_length + len(line_ending)) * len(truly_rectangular_lines_list)) editor.setSelectionMode(SELECTIONMODE.RECTANGLE) editor.copy() editor.undo() notepad.close() notepad.activateFile(remember_active_file) CATRB__main()