Community
    • Login

    "OVERWRITE" in column mode

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    12 Posts 5 Posters 1.8k 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.
    • jiskapavJ
      jiskapav
      last edited by

      Thank you for your reply and apologize for very little information about the actual problem. I will try to specify it in more details. I am using Notepad++ 7.8.8 release. The relevant information about my installation is bellow.

      Notepad++ v7.8.8   (32-bit)
      Build time : Jun 24 2020 - 00:15:36
      Path : C:\Program Files (x86)\Notepad++\notepad++.exe
      Admin mode : OFF
      Local Conf mode : OFF
      OS Name : Windows 10 Enterprise (64-bit) 
      OS Version : 1909
      OS Build : 18363.836
      Current ANSI codepage : 1250
      Plugins : mimeTools.dll NppConverter.dll NppExport.dll
      

      The main use of Notepad++ is for text edditing. I am working quite a lot with big mattrices with multiple rows and columns (let’s assume 20x20) and it is based on copy -> paste. I would like to ask if it is possible, to

      Example
      We want to replace MATRIX ‘b’ (20x20) by MATRIX ‘a’ (20x20).

      • Select MATRIX ‘a’: Left Alt + Mouse
      • Copy: Ctrl + C
      • Make sure you have OVERWRITE mode

      • Place cursor in front of X in first row of matrix ‘b’
      • Paste: Ctrl + V

      • The result is shown here (MATRIX ‘b’ is just indented to the right - not overwriten!) :

      • Required result is here (just overwriten MATRIX ‘b’ by MATRIX ‘a’):

      This example shows, that in OVERWRITE mode pasting does not work. Is there any option to switch on the OVERWRITE mode? Why pasting of the text string indents existing text?

      Thank you for your advices.
      @jiskapav

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

        Hello, @jiskapav, @alan-kilborn and All,

        Well, the Pasting process with Ctrl + V is totally independent of the status of the Insert key ( INS / OVR ) !

        Let’s suppose you have this text :

        12345 ABCDEFGHIJKLMNOPQ
        

        And that you want to paste the string 12345 over the string EFGHI in order to get the string ABCD12345JKLMNOPQ

        So :

        • Do a normal selection of the string 12345

        • Hit the Ctrl + C shortcut

        • Place the caret between letters D and E

        • Choose the Overwrite mode

        • Hit the Ctrl + V shortcut

        • Instead of overwriting the string EFGHI, the 12345 string is simply inserted between letters D and E

        Note that we would get exactly the same result if the Insertion mode would have been previously selected !


        So the right way to overwrite some text, while pasting an other text is to select the text to be overwritten, first and, then, paste :

        • Do a normal selection of the string 12345

        • Hit the Ctrl + C shortcut

        • Place the caret between letters D and E

        • Select the string EFGHI

        • The Insertion or Overwrite mode does NOT matter

        • Hit the Ctrl + V shortcut

        => This time the 12345 string does overwrite the EFGHI string


        This behavior is exactly the same when the Column Mode is involved !

        Let’s suppose the text :

        12345 ABCDEFGHIJKLMNOPQ
        12345 ABCDEFGHIJKLMNOPQ
        12345 ABCDEFGHIJKLMNOPQ
        12345 ABCDEFGHIJKLMNOPQ
        
        • Do a rectangular selection of the 12345 digits block

        • Hit the Ctrl + C shortcut

        • Then, place the caret on the first line, between the letters D and E

        • Hit the Ctrl + V shortcut

        => The bloc of digits is simply inserted between all letters D and E


        • Now, hit the Ctrl + Z shortcut to undo this insertion

        • Place the caret on the first line, between the letters D and E

        • With the keyboard or the mouse, associated to the Alt key, do a rectangular selection of the block of four lines EFGHI

        • Hit, again, the Ctrl + V shortcut

        => This time, the block of digits 12345 does overwrite the block of letters EFGHI !


        So, @jiskapav, the solution to your problem is :

        • Firstly, do a column selection of all your Matrix b contents

        • Secondly paste your Matrix a contents with a Ctrl + V operation

        Best Regards,

        guy038

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

          @jiskapav

          I think maybe the OP has a point about this.
          It would be useful to not have to select the shape of the destination block for this type of paste.

          While Notepad++ can’t do it natively, I wrote a Pythonscript that can do it.
          I call it PasteSpecialRectangularOverwrite.py (or PSRO for short):

          # -*- coding: utf-8 -*-
          
          # see https://community.notepad-plus-plus.org/topic/19643/overwrite-in-column-mode
          
          from Npp import editor, notepad
          import ctypes
          
          # this doesn't belong to the main object in this script, because it could be shared among several scripts
          try:
              editor3h  # we already have editor1 and editor2 objects, so create a 3rd that is hidden, thus "3h"
          except NameError:
              editor3h = notepad.createScintilla()
          
          class MsgboxAndQuitException(Exception):
              def __init__(self, message): self.msg = message
          
          class PSRO(object):
          
              def clipboard_get_text(self):
                  editor3h.setText('')
                  editor3h.paste()
                  paste_buffer_text = editor3h.getText()
                  return editor3h.getText()
          
              def clipboard_contains_column_block_text(self):
                  # from Notepad++'s ScintillaWin.cxx file:
                  #  // There does not seem to be a real standard for indicating that the clipboard
                  #  // contains a rectangular selection, so copy Developer Studio and Borland Delphi.
                  #  cfColumnSelect = static_cast<CLIPFORMAT>(
                  #      ::RegisterClipboardFormat(TEXT("MSDEVColumnSelect")));
                  #  cfBorlandIDEBlockType = static_cast<CLIPFORMAT>(
                  #      ::RegisterClipboardFormat(TEXT("Borland IDE Block Type")));
                  cb_OpenClipboard = ctypes.windll.user32.OpenClipboard
                  cb_CloseClipboard = ctypes.windll.user32.CloseClipboard
                  cb_EnumClipboardFormats = ctypes.windll.user32.EnumClipboardFormats
                  cb_GetClipboardFormatName = ctypes.windll.user32.GetClipboardFormatNameW
                  cb_OpenClipboard(None)
                  cf = cb_EnumClipboardFormats(0)
                  while cf != 0:
                      max_size = 500
                      buffer = ctypes.create_unicode_buffer(max_size + 1)
                      cb_GetClipboardFormatName(cf, ctypes.byref(buffer), max_size)
                      if len(buffer.value) > 0 and (buffer.value == u'MSDEVColumnSelect' or buffer.value == u'Borland IDE Block Type'):
                          cb_CloseClipboard()
                          return True
                      cf = cb_EnumClipboardFormats(cf)
                  cb_CloseClipboard()
                  return False
          
              def __init__(self):
                  if not editor.canPaste(): return
                  if len(editor.getSelText()) > 0 or \
                          editor.getSelections() != 1 or \
                          not editor.getOvertype() or \
                          not self.clipboard_contains_column_block_text():
                      editor.paste();  # normal paste
                      return
                  try:
                      if editor.getUseTabs(): raise MsgboxAndQuitException('Using tab chars in a document does not make sense in this application')
                      clipboard_contents = self.clipboard_get_text()
                      tab_char = '\t'
                      if tab_char in clipboard_contents: raise MsgboxAndQuitException('Tab characters in the clipboard data does not make sense in this application')
                      clipboard_row_list = re.split(r'\r?\n', clipboard_contents)
                      num_rows_in_clipboard = len(clipboard_row_list)
                      clipboard_row_length = len(clipboard_row_list[0])
                      for row in clipboard_row_list:
                          if len(row) != clipboard_row_length: raise MsgboxAndQuitException('Rows in clipboard have differing lengths -- not allowed')
                      curr_pos = editor.getCurrentPos()
                      caret_line_number = editor.lineFromPosition(curr_pos)
                      if caret_line_number + num_rows_in_clipboard > editor.getLineCount(): raise MsgboxAndQuitException('Not enough lines in document to do paste')
                      caret_column_number = editor.getColumn(curr_pos)
                      for n in range(num_rows_in_clipboard):  # this loop just tests for bad conditions
                          line = editor.getLine(caret_line_number + n).rstrip('\n\r')
                          if caret_column_number + clipboard_row_length > len(line): raise MsgboxAndQuitException('Not enough columns to over-paste')
                          if tab_char in line[ : caret_column_number]: raise MsgboxAndQuitException('Unexpected tab character in destination line')
                      editor.beginUndoAction()
                      for n in range(num_rows_in_clipboard):
                          line = editor.getLine(caret_line_number + n).rstrip('\n\r')
                          line = line[ : caret_column_number] + clipboard_row_list[n] + line[caret_column_number + clipboard_row_length :]
                          editor.replaceLine(caret_line_number + n, line)
                      editor.endUndoAction()
                      editor.setEmptySelection(curr_pos)
                  except MsgboxAndQuitException as e:
                      notepad.messageBox(e.msg, 'PasteSpecialRectangularOverwrite')
                      return
          
          if __name__ == '__main__': PSRO()
          

          Here’s the OP’s test data in text rather than image format, if anyone wants to try out the script on the originally discussed example:

          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  X  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          |  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  |  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5  6  7  8  9  |
          
          1 Reply Last reply Reply Quote 4
          • Alan KilbornA
            Alan Kilborn
            last edited by

            @Ekopalypse

            Hello, Eko, Python question related to the above script, if you please…

            I typically try to put everything I’m going to use in a script inside the main class definition for the script.

            I tried in the above to do that with class MsgboxAndQuitException (meaning put it inside class PSRO) but I couldn’t get it to work. It was a small thing, so I let it go with a whole lot of trying. But I’m curious for my own sake as to how one would do that.

            Note that in the above I didn’t want the same treatment for editor3h, for the reason stated in the comment in the script.

            EkopalypseE 1 Reply Last reply Reply Quote 0
            • EkopalypseE
              Ekopalypse @Alan Kilborn
              last edited by Ekopalypse

              @Alan-Kilborn

              I, personally, don’t do it. I saw in other code that classes get definded within other classes but for me this is more confusing than having the class defined outside.

              In your case you would have to define ein msg class attribute I guess.
              Something like this

              class c1:
              
                  class c2:
                      msg = ''
                      def __init__(self, msg):
                          self.msg = msg
              
                  def __init__(self):
                      self.test()
              
                  def test(self):
                      print(self.c2("123").msg)
              
              

              or you do create a real instance of MsgboxAndQuitException within class PSRO and work with it.

              1 Reply Last reply Reply Quote 1
              • jiskapavJ
                jiskapav
                last edited by

                Hello @Alan-Kilborn, @Ekopalypse, @guy038 and ALL (after long period),

                I am sorry, I don’t know how to use Python script efficiently in this case. I think it should be convenient to do overwrite paste natively by Notepad++. My expectation is:

                • when I would like to do overwrite paste - switch in OVR mode
                • when I would like to do insert paste - switch in INS mode

                Will it be possible to implement “overwrite paste” into Notepad++? This functionality is great when working with matrices (to replace matrix of the same shape at the same position as the old one was without any ALT+selection…).

                Best regards,
                @jiskapav

                Alan KilbornA Neil SchipperN 2 Replies Last reply Reply Quote 1
                • Alan KilbornA
                  Alan Kilborn @jiskapav
                  last edited by

                  @jiskapav said in "OVERWRITE" in column mode:

                  Will it be possible to implement “overwrite paste” into Notepad++?

                  I don’t think so, because this is a Scintilla function. Scintilla is the editing component that Notepad++ uses, and N++ typically doesn’t attempt to override Scintilla’s non-configurable editing behavior.

                  1 Reply Last reply Reply Quote 0
                  • Neil SchipperN
                    Neil Schipper @jiskapav
                    last edited by Neil Schipper

                    @jiskapav Please state whether your need is for many different array dimensions (rxc), or a small few, or, maybe even (almost) exclusively one, like the 20x20 you used in your example.

                    1 Reply Last reply Reply Quote 0
                    • jiskapavJ
                      jiskapav
                      last edited by

                      @Alan-Kilborn Thank you very much! I am already using your Python script after my own modifications.

                      @Neil-Schipper I mainly work with array dimensions 10x8 or 14x12, but overwrite paste should not be size sensitive. Paste should be done according to clipboard content size (ie. copy in column mode) from current position of caret.

                      Neil SchipperN 1 Reply Last reply Reply Quote 0
                      • Neil SchipperN
                        Neil Schipper @jiskapav
                        last edited by Neil Schipper

                        @jiskapav Good that you have the more general (dimension-neutral) solution.

                        If Alan (or someone else) had not provided a pythonscript solution, I would have proposed a pair of macros-with-hotkeys (one to copy, one to paste) for each dimension. Thus, 4 macros might have satisfied 90% of your need.

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