Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Replace current line without inserting a new line

    Help wanted · · · – – – · · ·
    5
    31
    22019
    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.
    • Claudia Frank
      Claudia Frank @Yaron last edited by

      Yaron, are you sure your logic is correct?

      if oldLine != editor.lineFromPosition(newPos) and editor.getColumn(newPos) == 0:
      

      You compare a number against boolean result.
      If there is a need to check if the clipboard has text and an eol added you might think of
      using ctypes and openclipboard api function. But don’t know if this isn’t overkill.

      Scott, curious, what does this mean?

      We’re not playing Golf here
      

      Thanks and cheers
      Claudia

      Scott Sumner 1 Reply Last reply Reply Quote 0
      • Scott Sumner
        Scott Sumner @Claudia Frank last edited by

        @Claudia-Frank ,

        My reference to “golf” was because my reply to Yaron (which apparently wasn’t quite what he needed) was a very short one-liner…“golf” is a game where someone proposes a programming task, and then people reply with their shortest versions. Here’s a better explanation: https://en.wikipedia.org/wiki/Code_golf

        I’ve had difficulty getting clipboard functionality into my pythonscripts. I tried the advice found in the early part of the thread found here (https://sourceforge.net/p/npppythonscript/discussion/1188886/thread/bafe5a3a/), and actually had it working at one point, then got a new job and a different computer, and I could never get it working again (as you can see at the end of the thread).

        Claudia Frank 1 Reply Last reply Reply Quote 0
        • Claudia Frank
          Claudia Frank @Scott Sumner last edited by

          @Scott-Sumner
          :-) I see, thanks.
          In regards to clipboard, gimme a second …

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 0
          • Scott Sumner
            Scott Sumner last edited by

            Also, Claudia, how is Yaron comparing a number against a boolean? His line you reference appears to be okay to me, syntax-wise.

            1 Reply Last reply Reply Quote 0
            • Yaron
              Yaron last edited by

              Hello Claudia and Scott,

              @Claudia,
              Scott has already asked it: where is the boolean?

              But don’t know if this isn’t overkill.

              I’m afraid it would. :)

              @Scott,
              As I’ve mentioned before - your statement regarding the clipboard was helpful indeed.

              Thank you both very much.
              Best regards.

              1 Reply Last reply Reply Quote 0
              • Claudia Frank
                Claudia Frank last edited by Claudia Frank

                Hello Yaron and Scott,

                a quick script to get the clipboard data using ctypes and windows api.
                Tested on Ubuntu (Linux) with wine - hopefully this works on real windows as well.

                import ctypes
                
                def Get():
                    if ctypes.windll.user32.OpenClipboard(None):
                        clipboard_handle = ctypes.windll.user32.GetClipboardData(1)
                        if clipboard_handle:
                            pointer_to_content = ctypes.windll.kernel32.GlobalLock(clipboard_handle)
                            clipboard_data = ctypes.c_char_p(pointer_to_content).value
                            ctypes.windll.kernel32.GlobalUnlock(clipboard_handle)
                        else:
                            return 'invalid handle'
                        ctypes.windll.user32.CloseClipboard()
                        return clipboard_data
                    else:
                        return 'ERROR'
                
                print Get()
                

                In reality you would make sure that the openclipboard would be called more often
                if it fails on the first try, as it might be that another application currently has opened it
                and therefore your app doesn’t get access.

                if oldLine != editor.lineFromPosition(newPos) and editor.getColumn(newPos) == 0:
                

                execution in order (afaik)
                First, both functions get executed
                Second result from getColumn get compared against 0 ->returns boolead
                Third result from lineFrom… gets ANDed with boolean -> retruns boolean
                Last oldLine(number) gets checked against boolean -> which means a boolean
                compare at all. All oldLine numbers are True execept the first line which is 0 and treaten
                as False.

                As said, afaik. Maybe I’m wrong.

                Cheers
                Claudia

                1 Reply Last reply Reply Quote 1
                • Yaron
                  Yaron last edited by

                  Hello Claudia,

                  Thank you very much for the script.
                  I won’t be by a PC until Monday or Tuesday. I’ll try it then.

                  And as I’ve written to Scott: it’s good to know I haven’t missed anything too simple. :)
                  I appreciate it.

                  First, both functions get executed

                  I think there’s no need to execute the second function if the first condition is not true.
                  So, the order is:
                  Execute first function.
                  Check first condition.
                  Execute second function.
                  Check second condition.

                  What do you think?

                  BR.

                  Claudia Frank 1 Reply Last reply Reply Quote 0
                  • Claudia Frank
                    Claudia Frank @Yaron last edited by

                    @Yaron and Scott,

                    of course, you are right.
                    Phew - one of these days …
                    Please forgive and forget what I said.
                    Recently I was reading something about precedence and that If statement
                    has one of the lowest rating, but of course != get executed before.
                    I guess now its time to go to bed. ;-)

                    Cheers
                    Claudia

                    1 Reply Last reply Reply Quote 0
                    • Claudia Frank
                      Claudia Frank last edited by

                      Btw… here the confirmation that you are right.

                        2           0 LOAD_GLOBAL              0 (oldLine)
                                    3 LOAD_GLOBAL              1 (editor)
                                    6 LOAD_ATTR                2 (lineFromPosition)
                                    9 LOAD_GLOBAL              3 (newPos)
                                   12 CALL_FUNCTION            1
                                   15 COMPARE_OP               3 (!=)
                                   18 POP_JUMP_IF_FALSE       50
                                   21 LOAD_GLOBAL              1 (editor)
                                   24 LOAD_ATTR                4 (getColumn)
                                   27 LOAD_GLOBAL              3 (newPos)
                                   30 CALL_FUNCTION            1
                                   33 LOAD_CONST               1 (0)
                                   36 COMPARE_OP               2 (==)
                                   39 POP_JUMP_IF_FALSE       50
                      
                        3          42 LOAD_CONST               2 ('y')
                                   45 PRINT_ITEM          
                                   46 PRINT_NEWLINE       
                                   47 JUMP_FORWARD             5 (to 55)
                      
                        5     >>   50 LOAD_CONST               3 ('n')
                                   53 PRINT_ITEM          
                                   54 PRINT_NEWLINE       
                              >>   55 LOAD_CONST               0 (None)
                                   58 RETURN_VALUE
                      

                      Cheers (shamy)
                      Claudia

                      1 Reply Last reply Reply Quote 0
                      • Yaron
                        Yaron last edited by

                        Hello Claudia,

                        In “one of these days” you’ve written such a beautiful script (I’m sure it is even before testing it).
                        My esteem has not been diminished at all by that trifle. :)

                        Thanks again. I appreciate your help.

                        Have a great night.

                        1 Reply Last reply Reply Quote 0
                        • Scott Sumner
                          Scott Sumner last edited by

                          @Claudia-Frank said:

                          if oldLine != editor.lineFromPosition(newPos) and editor.getColumn(newPos) == 0:

                          I was always taught that if YOU have some question or doubt, somebody reading your code later would also have the same concern, so better to make it explicit with parentheses:

                          if (oldLine != editor.lineFromPosition(newPos)) and (editor.getColumn(newPos) == 0):

                          I look forward to trying out the clipboard routine! Thank you, Claudia.

                          1 Reply Last reply Reply Quote 1
                          • Scott Sumner
                            Scott Sumner last edited by

                            The clipboard routine works well, Claudia, this is so great! Now I just have to write the corresponding ‘set’ version.

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

                              Hello Yaron, Scoot and Claudia,

                              I don’t want to disturb, in any way, your interesting discussion, about Python operators precedence ( and other goodies, that I can’t understand, yet ! ) but I, simply, created three macros, that seem to emulate the behaviour that you prefer, Yaron, AFAIK !!

                              So :

                              • Close any instance of Notepad++

                              • Start an OTHER editor than notepad++

                              • Open, in that editor, the Shortcuts.xml file

                              • Insert, at the beginning of this file, inside the <Macros> section, the XML text below :

                                  <Macro name="Cut        Current Line w/o EOL" Ctrl="yes" Alt="yes" Shift="no" Key="88">
                                      <Action type="0" message="2453" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2453" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2345" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2452" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2452" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2177" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2180" wParam="0" lParam="0" sParam="" />
                                  </Macro>
                                  <Macro name="Copy     Current Line w/o EOL" Ctrl="yes" Alt="yes" Shift="no" Key="67">
                                      <Action type="0" message="2453" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2453" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2345" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2452" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2452" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2178" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2304" wParam="0" lParam="0" sParam="" />
                                  </Macro>
                                  <Macro name="Replace Current line by Clipboard" Ctrl="yes" Alt="yes" Shift="no" Key="86">
                                      <Action type="0" message="2453" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2453" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2345" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2452" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2452" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2453" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2453" wParam="0" lParam="0" sParam="" />
                                      <Action type="0" message="2345" wParam="0" lParam="0" sParam="" />
                                  </Macro>
                                
                              • Save the changes, done in the Shortcuts.xml file

                              • Re-start Notepad++

                              => In the macros list, you should have three additional entries :

                              Cut     Current Line w/o EOL       Ctrl + Alt + X
                              
                              Copy    Current Line w/o EOL       Ctrl + Alt + C
                              
                              Replace Current Line by Clipboard  Ctrl + Alt + V
                              

                              Hypotheses :

                              • I suppose that the Scintilla commands : Home, Alt + Home, Shift + End, Left Arrow and Suppr are the default ones !

                              • I, also, suppose that the classical Ctrl + X , Ctrl + C and Ctrl + V commands are the default ones, too.

                              • Finally, the three shortcuts Ctrl + Alt + X, Ctrl + Alt + C and Ctrl + Alt + V should not be used, yet

                              Notes :

                              • After execution of the two macros Copy Current Line w/o EOL and Replace Current Line by Clipboard, the caret is moved back, at the very beginning of the current line

                              • After execution of the macro Cut Current Line w/o EOL, the caret is located, of course, at the very beginning of the next line

                              • These macros work nice, whatever :

                                • The current logical line Begins, or not, by some blank characters

                                • The current logical line is wrapped, or not

                                • The current logical line is split on several physical lines, preceded, or not, by any indentation

                                • The present cursor location, in this current logical line

                                • The EOL characters of the current logical line ( CRLF, LF or CR )

                                • The current logical line is not terminated by any EOL characters ( case of the very last line of the file ! )


                              So, let’s consider the simple text, of five lines, below :

                              First  line to copy
                              Second line to cut
                              Third  line to be replaced by the first line
                              Fourth line to be replaced by the second line
                              Fifth  line to end
                              
                              • Put the cursor, anywhere, in the First line

                              • Select the macro Macros - Copy Current Line w/o EOL OR hit the Alt + Ctrl + C shortcut

                              • Put the cursor, anywhere, in the Third line

                              • Select the macro Macros - Replace Current Line by Clipboard OR hit the Alt + Ctrl + V shortcut

                              => You should get the modified text, below :

                              First  line to copy
                              Second line to cut
                              First  line to copy
                              Fourth line to be replaced by the second line
                              Fifth  line to end
                              
                              • Now, put the cursor, anywhere, in the Second line

                              • Select the macro Macros - Cut Current Line w/o EOL OR hit the Alt + Ctrl + X shortcut

                              • Put the cursor, anywhere, in the Fourth line

                              • Select the macro Macros - Replace Current Line by Clipboard OR hit the Alt + Ctrl + V shortcut

                              => The final result should be :

                              First  line to copy
                              First  line to copy
                              Second line to cut
                              Fifth  line to end
                              

                              Remarks :

                              • If you use the macro Copy Current Line w/o EOL then put the cursor inside any other line and, finally, use the default CTRL + V command, the current line, while executing the macro, is just inserted, at the new cursor location

                              • Inversely, if you use the default CTRL + C, to select some lines, then put the cursor inside any other line and, finally, use the macro Replace Current Line by Clipboard, the copied lines will replace the current line, while executing the macro !

                              • Note, also, that executing, consecutively, several times, any of the two macros Copy Current Line w/o EOL AND/OR Replace Current Line by Clipboard does NOT change the current line at all !!

                              • However, executing, several times, the macro Cut Current Line w/o EOL deletes the consecutive current lines, keeping the last one, only, in the clipboard, in order to replace any other current line, while executing the Replace Current Line by Clipboard macro

                              • To end, a very nice trick : if, after using the Copy Current Line w/o EOL OR the Cut Current Line w/o EOL macro, you’re wrong, using the normal paste command ( Ctrl + V ), no panic ! Just execute the Replace Current Line by Clipboard macro ( or Ctrl + Alt + V ), just after :-))

                              Best Regards,

                              guy038

                              P.S. :

                              You’ve , certainly, noticed that I used, sometimes, in macros, two identical consecutive commands, as Home or Shift + End. These forms are necessary, when the lines are wrapped, in several physical lines and, even more, when a wrapped line is preceded by some leading blank characters !

                              Claudia Frank 1 Reply Last reply Reply Quote 1
                              • Claudia Frank
                                Claudia Frank @guy038 last edited by Claudia Frank

                                Hello guy,

                                what took longer, finding out how to get this solved using macros
                                or writing this?? ;-)))

                                Another goody

                                Cheers
                                Claudia

                                1 Reply Last reply Reply Quote 0
                                • cmeriaux
                                  cmeriaux last edited by

                                  @guy038
                                  for the record, you can edit shortcut.xml in notepad++ as long as do don’t change any shortcut through shortcut mapper. shortcuts.xml is overwritten only when a shortcut is modified.

                                  1 Reply Last reply Reply Quote 0
                                  • Scott Sumner
                                    Scott Sumner last edited by

                                    Let’s not talk about how this topic has diverged… guy038, regarding your statement about “two identical consecutive commands”, it seems that this isn’t predictable.

                                    Case 1: [single line, indented, caret in middle of line somewhere] 2 presses of “home” take caret to start of line; 3 move the caret to the first character of the indented text

                                    Case 2: [indented, word-wrap on, caret in middle of 2nd line of a wrapped line] 3 presses of “home” take caret to start of line

                                    I’m just wondering how it is possible to write consistent macros with this differing behavior; hopefully it is understood what I mean. Maybe one more thing to clarify: It seems that with the Case 1/2 behavior I’ve described, guy038’s macros would not quite know for sure if they are getting all the text on the line (seems like it would be easy to lose the leading indent in one case)…

                                    1 Reply Last reply Reply Quote 0
                                    • Yaron
                                      Yaron last edited by

                                      Hello all,

                                      @Claudia,
                                      Thanks again for the great script. I highly appreciate that.

                                      I think you have to close the clipboard whether it contains text or anything else.
                                      (Otherwise, if it contains an image the clipboard is locked and access denied).

                                      import ctypes
                                      
                                      def Get():
                                      	if ctypes.windll.user32.OpenClipboard(None):
                                      		clipboard_handle = ctypes.windll.user32.GetClipboardData(1)
                                      		if clipboard_handle:
                                      			pointer_to_content = ctypes.windll.kernel32.GlobalLock(clipboard_handle)
                                      			clipboard_data = ctypes.c_char_p(pointer_to_content).value
                                      			ctypes.windll.kernel32.GlobalUnlock(clipboard_handle)
                                      		else:
                                      			clipboard_data = -1	
                                      
                                      		ctypes.windll.user32.CloseClipboard()
                                      	else:
                                      		clipboard_data = -2
                                      		
                                      	return 	clipboard_data
                                      
                                      print Get()
                                      

                                      Or am I missing anything?


                                      @Guy,
                                      Thank you very much for the Macro.
                                      Beautifully written and explained as always.

                                      As a “mouse-oriented” user I didn’t know about the double “Home” in warped lines.
                                      Thanks for that too.

                                      As for Python:
                                      I suggest that you start with the simple script I posted at the beginning of this thread.
                                      You would soon be offering solutions both in Regex and Python. :)


                                      @Scott,
                                      Could you please post an example to your comment regarding Guy’s Macro?

                                      The Golf was worthwhile.
                                      We got two new gems from Claudia and Guy, didn’t we?
                                      Thanks again for your kind help.

                                      Best regards.

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

                                        Hi, All,

                                        Many thanks, cmeriaux, I didn’t know that fact, relative to the shortcuts.wml file ! So, it’s even more simple to get the Shortcuts.xml file modified :-))


                                        Yaron, it’s very kind of you to encourage me to try Python language and script. I promise you I will, as soon as possible. But, recently, I’ve had some health’s problems and I did not want to be involved, just now, in important tasks, as learning a new language !

                                        Moreover, by that time, Yaron, Claudia and Scott, I’ll have to decide between Python 2.7 or Python 3.5 ! I’ve been told that if you do not know Python, the best is to switch, directly, to Python 3.5. However, the Dave’s N++ Python script is based on Python 2.7 ? So ?

                                        Also, is it better to begin with Python script OR with the language itself ?

                                        BTW, I also glanced at the Dail’s Lua script. The script language seems, slightly, more easy to read and code. But, Yaron, Claudia and Scott, don’t feel offended, in any way ! It’s just a “newbe” opinion !!


                                        Scott, I apologize because, in my previous Post-Scriptum, I didn’t fully explain all the Scintilla commands needed to move, in all cases, the caret at the very beginning of any line ! I was, already, a bit tired to create the main part of this post, anyway ! So :

                                        Do you know, Scott, about the two Scintilla commands Alt + Home and Alt + End ? They allow, on wrapped lines, when the caret is in the middle of a specific line, to move it, exclusively, at the very beginning of that line OR at the end of that line !

                                        Of course, I, first, considered that I needed to hit 3 times, on the Home key, to get the column 1, if the current logical line was wrapped, with some leading spaces and that the caret was NOT in the first physical line !

                                        However, if the caret is on the first physical line of that wrapped logical line, after 3 Home actions, the caret is closed to the first non-blank character, of the first physical line. That was not what I expected :-((

                                        Now, instead of the actions Home, Home, Home, if we consider the 3 actions Home, Home, Alt + Home Nice, indeed : the last Scintilla command forces the caret to stay in column 1 :-))

                                        So, without any ambiguity, I can affirm that, whatever the kind of a line ( wrapped or not and/or with leading blank characters or not ) the 3 consecutive actions Home, Home, Alt + Home, always move the caret, from any position on that line to its very beginning ( column 1 )

                                        On the contrary, two actions End, End are enough to move the caret at the very end of any line ! Thus, from the very beginning of any line, the two actions Shift + End, Shift + End allow to select all the contents of the current line, without its EOL characters :-))

                                        For memory, the list of Scintilla commands, used in these three macros, are, by chronological order :

                                        Cut Current Line w/o EOL           :     Home     Home     Alt + Home     Shift + End     Shift + End     Ctrl + X     Suppr
                                        
                                        Copy Current Line w/o EOL          :     Home     Home     Alt + Home     Shift + End     Shift + End     Ctrl + C     Left Arrow
                                        
                                        Replace Current Line by Clipboard  :     Home     Home     Alt + Home     Shift + End     Shift + End     Ctrl + V     Home     Home     Alt + Home
                                        

                                        Finally, the sequence :

                                        Home     Home     Alt + Home     Shift + End     Shift + End
                                        

                                        simply, means : select all text, between the very beginning and the end of any line, whatever the caret position, on that line !

                                        BTW, I also verified that my macros work fine, whatever the 3 kinds of line wrapping, defined in Settings - Preferences… - Editing - Line Wrap ( Default / Aligned / Indent ) !

                                        Cheers,

                                        guy038

                                        1 Reply Last reply Reply Quote 1
                                        • Claudia Frank
                                          Claudia Frank last edited by

                                          @Yaron,

                                          you are correct but there is the close available.

                                          ctypes.windll.user32.CloseClipboard()
                                          

                                          and, msdn states

                                          An application should call the CloseClipboard function after every successful call to OpenClipboard. 
                                          

                                          so there is no reason to call CloseClipboard in the else branch. Or did I misunderstood something?

                                          This script was quickly written, normally you would define each function and parameters as well as return values
                                          error checks etc… separately and maybe wrap it into a try-except-finally block and, as already stated, make sure to call
                                          openclipboard more often in case it is locked by another application.

                                          Cheers
                                          Claudia

                                          1 Reply Last reply Reply Quote 0
                                          • Claudia Frank
                                            Claudia Frank last edited by

                                            @Guy,

                                            there are differences in py3 and 2 but the changes aren’t to complicated.
                                            Py3 got more features than 2 but, afaik, Py2 still has more 3rd party modules
                                            and when it comes to incooperate with C API, I like Py2 because strings are bytes
                                            whereas in Py3 string is unicode.
                                            A list of changes https://wiki.python.org/moin/Python2orPython3

                                            And if you like Lua more, no problem, I still will read your excellent posts ;-)
                                            Lua has its advantages, small, fast and extensible - it’s good - and I use it
                                            to write wireshark plugins and reconfigure my home router. So …

                                            Cheers
                                            Claudia

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Copyright © 2014 NodeBB Forums | Contributors