d0vgan / nppexec

NppExec (plugin for Notepad++)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using NppExec to open the Notepad++ Find/Replace dialog box with specific settings

Gitoffthelawn opened this issue · comments

I want to create a single shortcut key in Notepad++ to:

  1. Open the Notepad++ Find/Replace dialog box
  2. In that dialog box:
    a. Populate the 'Find what' & 'Replace with' fields with specific strings
    b. Ensure the 'Backward direction' checkbox is not checked
    c. Ensure the 'Wrap around' checkbox is checked
    d. Ensure the 'Regular expression' radio button is selected
    e. Ensure the '. matches newline' checkbox is checked
    f. Click the 'Find Next' button once
    g. Keep the dialog box open

AFAIK, Notepad++ cannot currently do the above natively. Thus, I've been trying to determine if it's possible to accomplish this with NppExec. It seems like it should be possible, but so far, I'm not seeing how to do it.

Can this be accomplished using NppExec?

Thanks in advance. And thanks again for NppExec. It's an amazing tool.

NppExec provides commands sci_find and sci_replace. The actual behavior of these functions completely depend on the flags parameter specified. The description and examples are available by typing either of:

help sci_find
help sci_replace
help all

If, however, you'd like to invoke Notepad++'es Find/Replace dialog, then it's rather a task for something like AutoIt or AutoHotKey.

Thanks for your help. Yes, I want to invoke the Notepad++ Find/Replace dialog box.

I want to be able to review the context before performing each replace operation.

I was hoping, at the least, to accomplish this simulating keystrokes using NppExec. Is there any way to do that?

Well, this can be done by using the power of nircmd ( https://www.nirsoft.net/utils/nircmd.html ).
First let's create the following NppExec script:

env_set local PATH = $(SYS.PATH);C:\Progs\NirSoft  // TODO: specify path to the folder with nircmd.exe here!
npp_sendmsg WM_COMMAND IDM_SEARCH_REPLACE
nircmd script "$(NPP_DIRECTORY)\replace.ncl"  // TODO: you may specify other folder than $(NPP_DIRECTORY)

Then, as the NppExec script above mentions "$(NPP_DIRECTORY)\replace.ncl", let's create a file named "replace.ncl" under your Notepad++ folder. It will be a simple non-Unicode text file with the following lines:

// set "Find what" text:
dlg "notepad++.exe" "Replace" settext 1601 "Find what"
// set "Replace with" text:
dlg "notepad++.exe" "Replace" settext 1602 "Replace with"
// uncheck "Whole word" checkbox:
win child title "Replace" sendmsg id 1603 0x00F1 0x0000 0
// uncheck "Match case" checkbox:
win child title "Replace" sendmsg id 1604 0x00F1 0x0000 0
// check "Wrap around" checkbox:
win child title "Replace" sendmsg id 1606 0x00F1 0x0001 0
// uncheck "Backward direction" checkbox:
win child title "Replace" sendmsg id 1722 0x00F1 0x0000 0
// click "Regular expression" button:
win child title "Replace" sendmsg id 1605 0x00F5 0 0
// uncheck ". matches newline" checkbox:
win child title "Replace" sendmsg id 1703 0x00F1 0x0000 0
// click "Find Next" button:
win child title "Replace" sendmsg id 1 0x00F5 0 0

Wow! Thank you so much!

You're doing some clever things there with NirCmd's sendmsg.

I use NirCmd quite often, and this is another great use for it. I hope Nir continues to update and improve it.

What tool do you prefer to use to get the Control ID's for dialog boxes?

Is there any good technique to use when the Control ID's are dynamically generated, and therefore are not static? For example, not long ago, I was trying to use NirCmd to automatically close the initial popup in Bytescout BarCode Reader (https://bytescout.com/products/enduser/misc/barcodereader.html), but the Control ID for the 'OK' button in that popup seemed to change on every run.

Thank you again for your generous help.

I use Property Edit. It's old, but contains everything needed, including a color picker:
https://mh-nexus.de/en/downloads.php?product=Property%20Edit

In case of dynamic control IDs, probably we can rely on the order of the controls, thus pressing the Tab key (or Shift+Tab) some exact number of times until the desired control becomes focused. Or, if there is a shortcut key (e.g. &Accept), then we can use Alt+key (e.g. Alt+A).

Ah, another one of Maël Hörz's useful programs. Thanks!

Great ideas regarding the dynamic control IDs. Thank you for those too.

BTW, would it be reasonable to allow NppExec to "type" keystrokes into Notepad++?

It looks like a question of duplicating of what AutoIt and AutoHotKey already allow to do... I mean, on the one hand I see the direct connection to Notepad++ (in terms of interaction between NppExec and Notepad++) and on the other hand there are existing well-tried tools created for exactly this kind of interactions.

It looks like a question of duplicating of what AutoIt and AutoHotKey already allow to do... I mean, on the one hand I see the direct connection to Notepad++ (in terms of interaction between NppExec and Notepad++) and on the other hand there are existing well-tried tools created for exactly this kind of interactions.

Yes, I agree with your analysis. It would be nice to have functionality directly within the scope of Notepad++, but there is the point of re-inventing the wheel to a degree.

I've been studying the NirCmd example you provided. Thank you again for it. Did you choose to use NirCmd so you could force checkboxes to a specific state, or was their another reason?

Also, for most of the commands, you chose to specify wParam as 0x0000, but for 2 commands, you chose to specify it as 0. Is there a semantic reason for this differentiation?

Finally, what's your favourite reference for Msg actions, such as 0x00F5?

That's fantastic! Thank you so much! 👍🏾 ❤️ 👍🏾

I made some edits (see PR #68) for readability.

The only remaining question I have is: For most of the commands, you chose to specify wParam as 0x0000, but for 2 commands, you chose to specify it as 0. Is there a reason?

For most of the commands, you chose to specify wParam as 0x0000, but for 2 commands, you chose to specify it as 0. Is there a reason

In terms of programming and scripting, 0x0000 is the same as 0. I used 0x0000 just to emphasize it is a meaningful constant value (BST_UNCHECKED in this case). If we look at the documentation for BM_SETCHECK (0x00F1) and BM_CLICK (0x00F5) we can read that:

BM_SETCHECK message
    wParam
        The check state.
    lParam
        This parameter is not used.

BM_CLICK 
    wParam
        Not used; must be zero.
    lParam
        Not used; must be zero.

I passed 0 as the values of the unused parameters. These values of 0 do not have any special meaning, so there's no reason to emphasize them.

I made some edits (see PR #68) for readability.

Thank you!
Can I merge it now or are you seeing/planning other corrections?

I made some edits (see PR #68) for readability.

Thank you! Can I merge it now or are you seeing/planning other corrections?

Regarding the 0 values, thank you for your response. I understand why you made those choices now... makes good sense.

Regarding #68, you're welcome (and thank you), and it's ready to merge!