Replace current line without inserting a new line

  • 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)…

  • Hello all,

    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
    			clipboard_data = -1	
    		clipboard_data = -2
    	return 	clipboard_data
    print Get()

    Or am I missing anything?

    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. :)

    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.

  • 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 ) !



  • @Yaron,

    you are correct but there is the close available.


    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.


  • @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

    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 …


  • Hello Guy and Claudia,

    First, I’d like to wish you a speedy recovery. I do hope it’s not anything serious.

    I’m glad you find the time to help us here. Thanks again for the clever macro.

    I’d love to start using Dail’s Lua Script but I’ve been waiting for an option to add scripts to the menu.

    Claudia has recommended Py2. She is the master.

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

    I’d suggest to take the plunge and start with a simple script. Experiment a bit and then combine it with a more methodical learning of the language.

    I’d be glad to dissect and analyze snippets of code with you.
    I’m not good at searching the documentation but I’m sure Claudia and Scott would kindly help us. :)

    Best of luck!

    Thanks again. I do appreciate your patience.

    Please try the following steps:
    Run the script.
    Copy an image to the clipboard.
    Try coping some text in NPP.

    The text is not copied as the clipboard is locked.
    NPP does not notify you of that (a minor issue) but other editors (e.g. EditPad) do.

    I haven’t read the documentation.
    It’s possible that using GetClipboardData(1) when the clipboard contains an image is still “successful”. Or the writer was not very accurate. :)

    Best regards.

  • @Yaron,

    hahahaha - master - I’m far away from being the master as I have proven again.
    I’m returning before closing the clipboard - good job.
    Your version works ok for me ;-)
    If I’ve still missed it - give me a kick ähh hint. ;-)


  • @Claudia,

    You are the undisputed master! And Shirley Holmes! :)
    What have you proven? You wrote it quickly and didn’t try the constellation I did.

    If I’ve still missed it -

    Not sure I understand that. :)

    Allow me a quick off-topic question:
    If you use “Block Comment” in a PY file, the current line (not selected) is commented.
    If you then use “Block Uncomment”, the line is not uncommented.

    Is that a bug?

    Thank you.

  • @Yaron,

    thx for the kind words but as said, being a master … maybe in 10 years.

    Actually I never used block un-/comment as I quickly discovered that
    selecting lines of codes and using single line un-/comment worked well.
    I even think, that I was using this technique quite some time before I found out
    that there is a block comment function.

    I don’t think it is a bug as the language itself doesn’t have such functionality.
    I don’t have access to my repository because I don’t have any windows environment at the moment,
    so can’t really easily analyze the npp source code but a quick look to github page seems to confirm
    my assumption. Npp is trying to get the start and end comment line for the language but as not
    found returns. (Notepad_plus.cpp line >3700 …)


  • Hello Claudia,

    Thank you for looking into it and for the detailed explanation. I appreciate it.

    Good night. :)

Log in to reply