Macros to find foward the next character
-
Hello,
Please help with a macro definition, which selects the character at the cursor and finds the position of the next matching character.
Recording a macro doesn’t help. NPP automatically records the symbol of the character at the time of recording and is therefor useless.
Thanks
-
Unfortunately, because of an “EU-FOSSA” (or whatever it was called) security audit a few years back, many N++ menu entries became non-macro-recordable for dubious reasons.
However, most of those are still macro-playable, so if you handwrite/edit the macro code and restart Notepad++, then the macro will be able to play it back.
The Search > Select and Find Next is one of those commands that is not recordable but is playable.
<Macro name="SelectCharAndFindNext" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="0" message="2307" wParam="0" lParam="0" sParam="" /> <Action type="2" message="0" wParam="43048" lParam="0" sParam="" /> </Macro>
So this macro sends the scintilla (editor) “extend selection right” command (
2307
) – so if your caret was just before the characterm
, it would select that characterm
. Then the second command43048
will use IDM_SEARCH_SETANDFINDNEXT = 43048 to take the current selection, and find the next instance of it.Actually, it might be better to use the volatile version at 43014 IDM_SEARCH_VOLATILE_FINDNEXT = 43014, to avoid clobbering the contents of the FIND dialog:
<Macro name="SelectCharAndFindNext" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="0" message="2307" wParam="0" lParam="0" sParam="" /> <Action type="2" message="0" wParam="43014" lParam="0" sParam="" /> </Macro>
So if I edit
%AppData%\Notepad++\shortcuts.xml
, add the second macro to the end of the<Macros>...
section, save and exit Notepad++, when I restart, running thatSelectCharAndFindNext
will select the character after the caret, then use the Search > Find (Volatile) Next, it will find the next occurrence of that character; using the Search > Find (Volatile) Next (or its keyboard equivalent,Ctrl+Alt+F3
by default), it will find it again and again. (Running the macro multiple times is not recommended, because extend selection would extend the selection by one character each time.)If you’d prefer to clobber the FIND WHAT value in the dialog, then you can use the version of the macro with 43048, after which, you can use the Find Next (or its default
F3
shortcut) to find the next occurrence (which might be easier to type thanCtrl+Alt+F3
for the volatile). -
@PeterJones
Thanks for the super precise and prompt reply.
Is your source public?How can I make the matching Case Sensitive?
-
@Ivan-Garnizov said in Macros to find foward the next character:
@PeterJones
Is your source public?Yes, unless I specify otherwise, any small snippets of code I post are public domain. (Any code I post that mentions copyright or license will obviously fall under those restrictions.) But something as simple as this macro borders on “patently obvious”.
How can I make the matching Case Sensitive?
The Find (Volatile) Next is always insensitive. The Select and Find Next will follow whatever case-sensitivity was last used in the FIND or REPLACE dialogs, so if the dialog says
☑ Match Case
, then Select and Find Next will be case-sensitive.Thus, if you want case sensitive, make sure the dialog is set that way, and use the 43048 version of the macro
-
I found a miss on my side so this message is edited
Also
@Ivan-Garnizov said in Macros to find foward the next character:
Is your source public?
the question refers to your source of information,
Where do you find and most importantly interpret these codes?Of you might be reading the “Matrix” like Tank and Cypher,
for which kudos to you! -
@PeterJones said in Macros to find foward the next character:
one of those commands that is not recordable but is playable.
Nice! I never even considered that it was possible to do that. Is there a reference anywhere for commands that can be used to create macros this way?
-
@Ivan-Garnizov said in Macros to find foward the next character:
the question refers to your source of information,
Where do you find and most importantly interpret these codes?My source of information is the User Manual and the Notepad++ source code, both of which are publically available.
- User Manual: Generic info on macros = https://npp-user-manual.org/docs/macros/
- User Manual: Details on the macro syntax in the
shortcuts.xml
file = https://npp-user-manual.org/docs/config-files/#macros - Source Code: Values for the
message="###"
on thetype="0"
andtype="1"
commands = https://github.com/notepad-plus-plus/notepad-plus-plus/blob/master/scintilla/include/Scintilla.h- Each of those command names (with their numbers) listed in Scintila.h is documented by the Scintilla project = https://scintilla.org/ScintillaDoc.html
- Source Code: Values for the
wParam="###"
on thetype="2"
commands = https://github.com/notepad-plus-plus/notepad-plus-plus/blob/master/PowerEditor/src/menuCmdID.h- The menuCmdID.h was not intended primarily for human-readability, so here is an example cheat sheet for how to read it:
- If we wanted to look up the Search > Select and Find Next, and already knew it was IDM_SEARCH_SETANDFINDNEXT:
#define IDM_SEARCH_SETANDFINDNEXT (IDM_SEARCH + 48)
- That says that IDM_SEARCH_SETANDFINDNEXT is defined as the value of
IDM_SEARCH
+ 48. So then you look for IDM_SEARCH:#define IDM_SEARCH (IDM + 3000)
- So that means that IDM_SEARCH_SETANDFINDNEXT is IDM + 3000 + 48 = IDM + 3048. Looking for the definition of IDM:
#define IDM 40000
- So that means that IDM_SEARCH_SETANDFINDNEXT is 40000 + 3000 + 48 = 43048
- If we wanted to look up the Search > Select and Find Next, and already knew it was IDM_SEARCH_SETANDFINDNEXT:
- But that required knowing beforehand what the internal IDM_XYZ name for the menu-command was that you wanted was. There are two good ways for finding that:
- In the menuCmdID.h, it is mostly organized by Notepad++ menu, so it’s not that hard to say “I want the Find (Volatile) Next command from the Search menu”, and then to look in menuCmdID.h for the Search menu entries – they start here at
IDM_SEARCH
, which isn’t that hard to find (for example, look in the file forSEARCH
, which is the name of the menu). Once in the right section, the IDM_<section>_<command_name> is reasonably easy to map to the entries in the default English menu system in Notepad++. - But that’s still a bit of effort. I make use of the NppUISpy plugin (installed from Plugins Admin), which allows you to look up what the command ID (and thus
wParam="###"
value) is for every built-in and currently-installed plugin command is.- Once you know the ID, you can use it directly. But you can also look up the IDM_xyz name by looking for the last digit or two, and finding the ones that end in that last digit until the name makes sense for the given menu command.
- In the menuCmdID.h, it is mostly organized by Notepad++ menu, so it’s not that hard to say “I want the Find (Volatile) Next command from the Search menu”, and then to look in menuCmdID.h for the Search menu entries – they start here at
- The menuCmdID.h was not intended primarily for human-readability, so here is an example cheat sheet for how to read it:
- This Forum’s FAQs: Automating Notepad++ = https://community.notepad-plus-plus.org/topic/25400/faq-automating-notepad
- This has a lot about macros, and how to dig in to find even the right menu command IDs for plugin commands (plugin commands are not recordable, but the FAQ explains how to hack the recorded macro to run plugin commands with certain limitation)
@deleelee , there is no published reference of which commands aren’t recordable – there are thousands of commands, and it’s hard to piece together the information(*). The general rule is “any command that requires user input (launches a dialog, renames a file, etc) cannot be recorded or played back; any other built-in command that can be accessed through a menu can at least be played back by a macro, though an arbitrary list of them cannot be recorded even though they can be played back; almost any normal ‘editing’ command, like copy/paste/select etc, which are handled by the scintilla component, should be recordable”. As the Automating Notepad++ FAQ explains, there is no list of which commands are specifically recordable vs not. Essentially, my recommendation is to try to record a macro, and then see if there are any commands (like the Search > Select and Find Next) that don’t end up with a corresponding entry in the macro; those are usually the commands that aren’t macro-recordable. ;-)
(*: when I was writing up the FAQ, I tried to create a list of not-recordable-but-playable based on the source code, but I gave up after a few hours of spinning my wheels on that. It’s not a simple task.)