How to clone current document, language included?



  • I want to record a simple macro. So I go to Macro->Start recording and I perform the following actions

    Ctrl-A to select the whole text of current document
    Ctrl-C to copy it into the clipboard
    Ctrl-N to create a new document
    Ctrl-V to paste the text in clipboard in the new document
    Language->P->PHP to set the new document as PHP and format text accordingly

    Then I save the macro and I associate it to shortcut Ctrl-Shift-N. However, when I run it, the last action in NOT performed. Why?

    My ideal macro would be to CLONE the current document, that is, to read also which is the current language and use the same for the new document. But it looks like there is no way to do it. Just consider that both current document and the new one might non been saved yet, that is, they have not a filename or an extension.



  • @Dario-de-Judicibus-0,

    Not all menu items are macro-recordable, even though I agree that ones like the Language toggles should be.

    However, you can manually edit your recorded macro by editing shortcuts.xml (make sure you follow the sequence described here to get your config file changes to take effect)

    Using some of the resources linked in the official docs’ description of the Macros section of shortcuts.xml, I have annotated the macro, including the line I added.

            <Macro name="DuplicateFileAsPHP" Ctrl="no" Alt="no" Shift="no" Key="0">
                <Action type="0" message="2013" wParam="0" lParam="0" sParam="" /><!-- Select All -->
                <Action type="0" message="2178" wParam="0" lParam="0" sParam="" /><!-- Copy -->
                <Action type="0" message="2422" wParam="0" lParam="0" sParam="" /><!-- RECORDED IN MACRO, BUT MAYBE NOT NEEDED: Selection mode -->
                <Action type="0" message="2325" wParam="0" lParam="0" sParam="" /><!-- RECORDED IN MACRO, BUT MAYBE NOT NEEDED: Cancel -->
                <Action type="2" message="0" wParam="41001" lParam="0" sParam="" /><!-- File: New -->
                <Action type="0" message="2179" wParam="0" lParam="0" sParam="" /><!-- Paste -->
                <Action type="2" message="0" wParam="46008" lParam="0" sParam="" /><!-- Manual Edit to Macro: 46008 is Language>P>PHP menu command ID -->
            </Macro>
    

    After you save the shortcuts.xml and restart Notepad++ (as described there), your macro should duplicate the file and set the language.

    Alternately, instead of that recorded sequence, instead just use File > Save a Copy As to duplicat, followed by File > Open to open the new one (this sequence leaves the original open and then opens the duplicate for editing. Or File > Save As to duplicate to a new name, and use the new name in Notepad++; if you need the old one open also, then File > Open will work. Yes, I understand that this method requires two manual steps and is not macro-recordable (because you cannot supply file name to macro), but it ensures that you have saved the file right away, which is really a best-practice anyway. I rarely need to leave the original open when I want to create a duplicate then edit that duplicate, so SaveAs is sufficient for my needs, but everyone has their own workflow.



  • @PeterJones said in How to clone current document, language included?:

    shortcuts.xml, I have annotated the macro

    Annotations are good, but I’ve found that if you do it this way, Notepad++ won’t keep the “comments” when it next needs to write out shortcuts.xml.

    (Side Note: I don’t think @PeterJones intended these comments to be permanent, he’s just showing the OP what-does-what for this posting)

    So, you could spend a lot of time making nice “comments”, only to have them inconsiderately blown away. :-(

    If you want to put a comment in a macro, adding a line like this is a viable technique:

    <Action type="0" message="2172" wParam="0" lParam="0" sParam="I am a comment that won't be destroyed" />
    

    Message 2172 is a Scintilla “null” action, so when the macro is run, this line will be noted as such by the macro interpreter, and nothing more happens.

    So, while you can’t put “comments” right on the same line of a command like @PeterJones did, you can put a null action message in, with a nice comment, ABOVE the line you want it to apply to.

    Sometimes I do this when I see a nice macro on this site; I’ll put in a comment with the link back to it, so I can find it again in the future if I need to refer to it myself, or a future poster has a similar need…I can quickly provide it.



  • Hello, @dario-de-judicibus-0, @alan-kilborn, @peterjones and All,

    Why not use a complete comment line ?

    For instance, this line comment in the native Trim Trailing Space and Save macro of N++ remains across sessions and start/close N++ actions, whatever I’ve previously used this macro or not ;-))

            <Macro name="Trim Trailing Space and Save" Ctrl="no" Alt="yes" Shift="yes" Key="83">
                <Action type="2" message="0" wParam="42024" lParam="0" sParam="" />
                <!-- The line BELOW will save the CURRENT file --->
                <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />
            </Macro>
    

    I don’t think that it depends on your Windows system, as I’m ( still ) with Windows XP ! But, may be it’s related to the fact that I’ve got a local configuration. So, the shortcuts.xml file is along with the other configuration files and notepad++.exe in a same folder D:\@@\791

    Cheers,

    guy038



  • @guy038 said in How to clone current document, language included?:

    <!-- The line BELOW will save the CURRENT file —>

    For me, if I insert such a standalone line, or indeed, as I mentioned earlier, an xml comment on a non-comment line, Notepad++ removes it the next time something happens that causes it to write out shortcuts.xml.

    Examples of that include: recording a macro, deleting a macro, assigning/removing a shortcut key to anything, changing/deleting a Run menu entry, etc.

    Technically, I don’t think it “removes” it, I think it just starts from scratch in writing the file with what it thinks should be there. Since it knows nothing about my comment – gone!

    This is different from, say english_customizable.xml (or whatever language file you use), which Notepad++ only reads, and never writes to. There, any comments you place, remain.



  • Hi, @alan-kilborn,

    Weird ! For me, I just tried with N++ v7.8.5 and the last 7.9.1 version and the comment remains !?
    Of course, my two releases are local ones !

    Here’s is my debug-info information :

     Notepad++ v7.9.1   (32-bit)
    Build time : Oct 27 2020 - 16:07:12
    Path : D:\@@\791\notepad++.exe
    Admin mode : OFF
    Local Conf mode : ON
    OS Name : Microsoft Windows XP (32-bit) 
    OS Build : 2600.0
    Current ANSI codepage : 1252
    Plugins : mimeTools.dll NppConverter.dll NppExport.dll 
    

    BR

    guy038



  • Can anyone else replicate either my behavior, or Guy’s ?

    Here’s what I’m using:

    Notepad++ v7.9.1 (32-bit)
    Build time : Nov 2 2020 - 01:03:56
    Path : C:\…\npp.portable\notepad++.exe
    Admin mode : OFF
    Local Conf mode : ON
    OS Name : Windows 10 Enterprise (64-bit)
    OS Version : 1809
    OS Build : 17763.1577
    Current ANSI codepage : 1252



  • @Alan-Kilborn ,

    I’m with Alan on this one: if I add a comment, save/exit/reload, it’s still there. But if I then record/save a macro, exit and reload, the comment from the existing macro is gone.

    Notepad++ v7.9.1   (64-bit)
    Build time : Nov  2 2020 - 01:07:46
    Path : C:\usr\local\apps\notepad++\notepad++.exe
    Admin mode : OFF
    Local Conf mode : ON
    OS Name : Windows 10 Enterprise (64-bit) 
    OS Version : 1903
    OS Build : 18362.1198
    Current ANSI codepage : 1252
    Plugins : ComparePlugin.dll ExtSettings.dll HexEditor.dll LuaScript.dll MarkdownViewerPlusPlus.dll mimeTools.dll NppConsole.dll NppConverter.dll NppEditorConfig.dll NppExec.dll NppExport.dll NppFTP.dll NppUISpy.dll PreviewHTML.dll PythonScript.dll QuickText.dll RestApiToText.dll TagLEET.dll XMLTools.dll 
    


  • @PeterJones said in How to clone current document, language included?:

    I’m with Alan on this one: if I add a comment, save/exit/reload, it’s still there. But if I then record/save a macro, exit and reload, the comment from the existing macro is gone.

    Me too !

    Notepad++ v7.9.1 (64-bit)
    Build time : Nov 2 2020 - 01:07:46
    Path : D:\Aplicaciones\npp.7.9.1.portable.x64\notepad++.exe
    Admin mode : ON
    Local Conf mode : ON
    OS Name : Windows 7 Professional (64-bit)
    OS Build : 7601.24546
    Current ANSI codepage : 1252
    Plugins : BetterMultiSelection.dll mimeTools.dll NppConverter.dll NppExport.dll PythonScript.dll



  • I’m wondering if Guy did the correct kind of change during his testing… or if his really works differently.



  • Hello, @dario-de-judicibus-0, @alan-kilborn, @peterjones, @astrosofista and All,

    As I kept the habit, most of the time, to modify configuration files from outside N++, with Microsoft Notepad, I chose the other method:

    • I started Notepad++

    • I opened shortcut.xml, from within N++,

    • I added this new macro, below, without any associated shortcut

            <Macro name="TEST COMMENT" Ctrl="no" Alt="yes" Shift="yes" Key="0">
                <Action type="2" message="0" wParam="42024" lParam="0" sParam="" />
                <!-- The line BELOW will save the CURRENT file --->
                <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />
            </Macro>
    
    • I closed the shortcuts.xml tab ( Ctrl + W )

    • I closed Notepad++ ( ALT + F4 )

    • I opened shortcuts.xml with Microsoft Notepad => The different comments were still here

    • I re-started N++

    • I processed the macro ( Macro > TEST COMMENT menu option ), after adding some blank chars at end of some lines of change.log file => it did trim the trailing spaces and save change.log file !

    • Again, I closed N++

    • Viewing again shortcuts.xml with Microsoft Notepad => The different comments were still here

    • I re-started N++

    • I opened Shortcuts.xml, from within N++ => All was fine : the comment is still present !

    So… I do not understand at all !


    Thus, unlike I said before, it could be related to the operating software itself !? I’ll try this assumption later, on my wife’s W8 laptop… when she’s at work! I know her password but shhh ;-))

    I’ll just paste all my 791 folder on her laptop’s desktop. Really curious of the result !

    Best regards,

    guy038



  • @guy038 said in How to clone current document, language included?:

    I do not understand at all !

    From your description, your “workflow” for this doesn’t include anything which will cause Notepad++ itself to need to update shortcuts.xml.

    After you have your nice comments (manually) added to shortcuts.xml, use the macro recorder to record a simple macro, then save the macro with a name. Then restart Notepad++ and examine shortcuts.xml. You should see your new named macro present but any xml comments you put in the file are now eliminated.



  • @Alan-Kilborn ,

    I tried further experiments, with extra pseudo-attributes (like <Action type="2" message="0" wParam="42024" lParam="0" sParam="" commentAttribute="value" />) and after saving or deleting a different macro in Notepad++, it still got rid of it – I was hoping it would keep unknown attributes, but it didn’t. :-(



  • Hello, @dario-de-judicibus-0, @alan-kilborn, @peterjones, @astrosofista and All,

    @alan-kilborn,

    Oh…, I just forgot one line in my previous post, right after the black window text :

    • I saved the new TEST COMMENT macro ( Ctrl + S ) from within N++

    I really saved this macro ! Otherwise, I would not have been able to see it in the Macro menu and to execute it, later. !!


    On the other hand, how you explain that, by default, these comments in the UserDefinedCommands node are kept overs sessions ?

    		<!--
    		The following shortcuts are dangerous if your computer is compromised.
    		(But if your computer is compromised, EVERYTHING IS DANGEROUS!)
    		Remove the comment and use them at your own risk!!!
    
            <Command name="Launch in Firefox" Ctrl="yes" Alt="yes" Shift="yes" Key="88">firefox &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Launch in IE" Ctrl="yes" Alt="yes" Shift="yes" Key="73">iexplore &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Launch in Chrome" Ctrl="yes" Alt="yes" Shift="yes" Key="82">chrome &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Launch in Safari" Ctrl="yes" Alt="yes" Shift="yes" Key="65">safari &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Send via Outlook" Ctrl="yes" Alt="yes" Shift="yes" Key="79">outlook /a &quot;$(FULL_CURRENT_PATH)&quot;</Command>
    		-->
    

    Now, all this story becomes crazy ! I did not have any problem with my wife’s W8 laptop, too !

    I mean the comments are well preserved in the shortcuts.xml configuration file, no matter what operations I did !

    As I said before, I simply copied a folder 791, containing the last portable version of N++, on the desktop

    And I copied/pasted the original Trim Trailing Space and Save macro, located at beginning of the <Macros> node, with the new name TEST COMMENT, right before the line </Macros> and then saved the modified shortcuts.xml from within N++

    I have no idea what’s going on ! I also verified the attributes of shortcuts.xml : just the normal A rchived attribute

    Note that my shortcuts.xml files are UTF-8 encoded files. BTW, do not save the shortcuts.xml with the UTF-8-BOM encoding, by mistake. In this case, all the recorded macros are greyed in the Macro menu !

    Does anyone have an other suggestion ?

    Cheers,

    guy038

    P.S.* : It’s past lunchtime here, but I’m going to have a bite to eat ;-))



  • @PeterJones said in How to clone current document, language included?:

    I was hoping it would keep unknown attributes…

    Well, if you know it isn’t being used by a particular action, you can put some “comment text” in the sParam="" area of a non-2172 action, and it will be retained. This keeps the “same line” comment idea alive.

    But I truly think the “2172” type of comment is probably best/safest, even if it isn’t on the same line that it discusses.

    I supposed it would be nice to have direct support for a comment="blah blah blah" type attribute. But, the whole idea of hand-editing shortcuts.xml is an advanced concept, rather “hackish”, and a bit dangerous (potential data loss, as discussed), so I’m okay with it the way it is.



  • @guy038 said in How to clone current document, language included?:

    Now, all this story becomes crazy

    Does anyone have an other suggestion ?

    I’m out of ideas for you. :-)



  • @guy038,

    1. Fresh NPP Portable: 3530d585-310d-44ae-a5c7-234999b8f5e9-image.png

    2. Duplicate macro, add a variety of comment styles: c53edf2e-f969-486f-8375-309979f9c289-image.png

    3. Exit/Reload Notepad++: no change to screenshot #2

    4. Start recording macro, type blah, backspace out blah, save, stop recording, save macro as blah.

    5. Exit /Reload Notepad++. All comments, including the default original comments, are gone.
      ac6663c3-4ad6-41a5-8277-2848fa46f442-image.png

    I don’t know how it works for you, because no matter what I do, any sequence like I just showed, which saves or deletes a macro from the macro-recording panel, gets rid of all comments from the XML.

    While doing the experiment, I noticed that the default comments from the unedited shortcuts.xml had tabs, whereas in my previous experiment, I had used spaces, so I tried with both styles of comments; the unedited used multi-line comments with only the start/start on a line, and the text of the comment between, so I tried that too. But all of those comments, no matter what style, disappeared.

    I wish I knew how you got it to keep the comments.



  • Hello, @dario-de-judicibus-0, @alan-kilborn, @peterjones, @astrosofista and All,

    Aaaah ! I’m sorry and apologize for making, all of you, searching hard ! It’s all my fault !

    Indeed, when @alan-kilborn said :

    From your description, your “workflow” for this doesn’t include anything which will cause Notepad++ itself to need to update shortcuts.xml

    I just thought that he was speaking about the fact of saving the shortcuts.xml file with Microsoft Notepad and not from within Notepad++ !

    But I understood, at once, all the story then @peterjones said :

    1. Start recording macro, type blah, backspace out blah, save, stop recording, save macro as blah

    Like you, when I decided to record a new macro and give it a name, and, after closing and restarting N++, all comments were gone away in shortcuts.xml, even those located outside the <Macros> node and, also, the default comments of the initial shortcuts.xml file, located in the UserDefinedCommands section, below :

    		<!--
    		The following shortcuts are dangerous if your computer is compromised.
    		(But if your computer is compromised, EVERYTHING IS DANGEROUS!)
    		Remove the comment and use them at your own risk!!!
    
            <Command name="Launch in Firefox" Ctrl="yes" Alt="yes" Shift="yes" Key="88">firefox &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Launch in IE" Ctrl="yes" Alt="yes" Shift="yes" Key="73">iexplore &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Launch in Chrome" Ctrl="yes" Alt="yes" Shift="yes" Key="82">chrome &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Launch in Safari" Ctrl="yes" Alt="yes" Shift="yes" Key="65">safari &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Send via Outlook" Ctrl="yes" Alt="yes" Shift="yes" Key="79">outlook /a &quot;$(FULL_CURRENT_PATH)&quot;</Command>
    		-->
    

    Wooooh, everything is, again, logic between all our configurations ;-))


    So, as any macro recording deletes all comments, a simple work-around would be to :

    • Copy your present shortcuts.xml file as, say, shortcuts_comments.xml

    • Comment all your macros already created, in the shortcuts_comments.xml, as you like to

    • Record your new macro, in Notepad++

    • Close and re-start Notepad++

    • Open, both, the shortcuts.xml and shortcuts_comments.xml files, in N++

    • Copy your new macro from the shortcuts.xml file

    • Paste it in the shortcuts_comments.xml file

    • Add the wanted comments in the shortcuts_comments.xml file, relative to this new macro

    • Save the shortcuts_comments.xml file

    With this method, in addition to having a nice version with comments, that we may post in our site, we also benefit from a backup of the shortcuts.xml file. Indeed, if, by mistake, the shortcuts.xml is deleted, simply copy shortcuts_comments.xml as shortcuts.xml and your comments will even remain in shortcuts.xml until your next macro recording ;-))

    Best Regards,

    guy038


Log in to reply