• Login
Community
  • Login

Macro to add symbol before and after text

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
13 Posts 6 Posters 873 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.
  • A
    Alan Kilborn @Ekopalypse
    last edited by Jul 5, 2021, 12:45 PM

    @Ekopalypse said in Macro to add symbol before and after text:

    record a macro by keyboard actions like

    Potential downsides:

    • destroys clipboard contents
    • doesn’t work for those without numeric keypad on their keyboard (and yes, there are those of us for which a keyboard with a numeric keyboard is a hinderance; thus mine):

    074bcbd7-c967-4923-8aa0-fad5d50dc965-image.png

    1 Reply Last reply Reply Quote 4
    • J
      JP A.
      last edited by Jul 5, 2021, 8:10 PM

      What a cool forum!

      Thank you for your suggestions. I love Ekopalypse’s approach for the simplicity, but Alan is right about the clipboard issue. And I use the clipboard a ton. So I’ve tried Alan’s solution and it works beautifully.

      Thank you!

      1 Reply Last reply Reply Quote 1
      • M
        mathlete2 @Alan Kilborn
        last edited by Jul 12, 2021, 1:53 PM

        @Alan-Kilborn thanks for the solution! I was able to use it to create the following macro for markup coding that I will be doing frequently (among others):

                <Macro name="bold" Ctrl="yes" Alt="no" Shift="no" Key="66">
                    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?s).+" />
                    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                    <Action type="3" message="1602" wParam="0" lParam="0" sParam="**${0}**" />
                    <Action type="3" message="1702" wParam="0" lParam="898" sParam="" />
                    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                </Macro>
        

        Unfortunately, when I do a multi-select, only the last selection is modified by the macro. Is there a way to modify the macro code to support multiple selections? For example, in the sentence I want this, this, and this to be modified., I want to be able to Ctrl+double-click each this, apply the shortcut once, and have all 3 changed to **this** (i.e. I want **this**, **this**, and **this** to be modified.).

        P 1 Reply Last reply Jul 12, 2021, 3:56 PM Reply Quote 0
        • T
          TroshinDV
          last edited by Jul 12, 2021, 2:46 PM

          https://github.com/trdm/jn-npp-scripts
          trdmBrackets.js
          alt text

          P 1 Reply Last reply Jul 12, 2021, 2:56 PM Reply Quote 0
          • P
            PeterJones @TroshinDV
            last edited by PeterJones Jul 12, 2021, 2:56 PM Jul 12, 2021, 2:56 PM

            @TroshinDV ,

            What does your screenshot add to the conversation? With no explanation in the text of your post, it is very hard to tell what you are trying to say. While the old saying is that “a picture is worth 1000 words”, I don’t think that’s true when there is no context given to the picture.

            It looks like you were showing an alternative to using the built-in macro language, which is fine (but unnecessary, since the OP had a solution already, which doesn’t require external installs)… but explaining that this is an alternative solution will help.

            Does your trdmBrackets.js script address the OP’s additional request of wanting to handle multi-selection? Your example screenshot only shows a single selection, and you didn’t explain, so we cannot know.

            T 1 Reply Last reply Jul 12, 2021, 8:03 PM Reply Quote 0
            • P
              PeterJones @mathlete2
              last edited by Jul 12, 2021, 3:56 PM

              @mathlete2 ,

              The problem with trying to do an “in-selection” search-and-replace in a multi-selection region is that the search-and-replace engine navigates through the matches by changing the selection… which would break the multi-selection.

              In fact, if you try to do a multi-selection region and then run Search > Replace, you will see that the “☐ In Selection” checkbox is greyed out, so that you cannot use it. A multi-selection is a different beast than a normal selection, and there are some things that don’t work the same.

              It might be that you have to go deeper into one of the scripting languages (like the jN-NPP that @TroshinDV uses, or the PythonScript that many of the regulars use)… Looking through the scintilla docs, I found “Multiple Selection and Virtual Space” – these messages should all have corresponding methods in the PythonScript editor object (or equivalent in other scripting plugins). So I pieced together this script which does multi-selection replacement, where it doesn’t matter what the contents of the multi-selection are: it will just surround them by the **:

              # encoding=utf-8
              """in response to https://community.notepad-plus-plus.org/topic/21446/ multi-selection request
              
              the comments show my debug code, or alternative methods
              
              this will loop through all N sections of the multi-selection
              for each section:
              * find the start and end locations
              * grab the original text from that section
              * replace the original by surrounding it with **
              
              Notes:
              * it allows a single undo for the whole group of replacements, but if you do UNDO,
                  it will clear the multi-selection
              * at the end of the replacement, the multi-selection goes down to a zero-length
                  selection at the start of each of the original words;
                      shift+arrows or shift+ctrl+arrow will allow you to extend the multi-selection again
              
              """
              
              from Npp import *
              
              editor.beginUndoAction()
              
              for mSel in range(editor.getSelections()):
                  selStart = editor.getSelectionNStart(mSel)
                  selEnd = editor.getSelectionNEnd(mSel)
                  orig = editor.getTextRange(selStart, selEnd)
                  #console.write("loop #{}: {}..{} = \"{}\"\n".format(mSel,selStart,selEnd,orig))
                  txt = "**" + orig + "**"
                  editor.replace(orig,txt,0,selStart,selEnd)
              
              
              
              editor.endUndoAction()
              
              M 1 Reply Last reply Jul 12, 2021, 5:41 PM Reply Quote 2
              • M
                mathlete2 @PeterJones
                last edited by Jul 12, 2021, 5:41 PM

                @PeterJones thanks for putting that together! Unfortunately, I’m not familiar with how to work such scripts into NP++. Where is this documented?

                P T 2 Replies Last reply Jul 12, 2021, 5:52 PM Reply Quote 0
                • P
                  PeterJones @mathlete2
                  last edited by PeterJones Jul 12, 2021, 5:52 PM Jul 12, 2021, 5:52 PM

                  @mathlete2 ,

                  PythonScript is a plugin, which you install through Plugins > Plugins Admin interface.

                  It comes with its own documentation available thru the Plugins > PythonScript > Context-help menu entry

                  This post gives a run-through for how to get the script into the plugin, and how to run the script

                  1 Reply Last reply Reply Quote 2
                  • T
                    TroshinDV @PeterJones
                    last edited by Jul 12, 2021, 8:03 PM

                    @PeterJones said in Macro to add symbol before and after text:

                    What does your screenshot add to the conversation?

                    I hinted a person that there is another solution. If he became interested, he would ask. And his silence indicates a lack of interest.
                    Я намекнул человеку что есть и другое решение. Если бы он заинтересовался, он бы спросил. А его молчание указывает на отсутствие интереса.

                    1 Reply Last reply Reply Quote 0
                    • T
                      TroshinDV @mathlete2
                      last edited by TroshinDV Jul 12, 2021, 8:05 PM Jul 12, 2021, 8:04 PM

                      @mathlete2 said in Macro to add symbol before and after text:

                      @PeterJones thanks for putting that together! Unfortunately, I’m not familiar with how to work such scripts into NP++. Where is this documented?

                      https://github.com/sieukrem/jn-npp-plugin
                      My developments are based on this plugin.

                      1 Reply Last reply Reply Quote 0
                      13 out of 13
                      • First post
                        13/13
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors