I was wondering whether NppExec allows to open a file, say, "C:\Program Files (x86)\Notepad++\change.log" when a text "Notepad++\change.log" is selected in the editor.
NppExec does allow this!
Here is NppExec's script required for this (please see the embedded comments for clarifications):
npp_console ?
// process the selected text...
sci_sendmsg SCI_GETSELTEXT 0 @""
set local F = $(MSG_LPARAM) // the selected text
if "$(F)" != "" then // something selected
// check current file name...
set local n ~ strfind "$(FILE_NAME)" "$(F)"
if $(n) == -1 then // current file name does not contain the selected text
// check already opened files...
set local prev_fpath = $(FULL_CURRENT_PATH)
npp_console local -
npp_switch $(F) // try to switch to another file
npp_console local +
if "$(FULL_CURRENT_PATH)" == "$(prev_fpath)" then // did not switch to another file
// check predefined directories...
npe_console local v+ --
C:\tools\findfile.bat "$(F)"
if "$(OUTPUT1)" != "" then // found in a predefined dir
set local F = $(OUTPUT1)
endif
npp_open $(F)
endif
endif
endif
This NppExec's script uses external file "C:\tools\findfile.bat" which describes the set of predefined directories to be checked.
Here is a possible content of this "findfile.bat":
@echo off
for /D %%d in (
"C:",
"C:\Program Files",
"C:\Program Files (x86)"
) do (
if exist "%%~d\%~1" (
call :EchoUnquotedString "%%~d\%~1"
)
)
goto End
:EchoUnquotedString
echo %~1
goto End
:End
The predefined directories in this example are: "C:", C:\Program Files" and "C:\Program Files (x86)". You can define your own set of directories here.
Here is how it works:
C:\tools\findfile.bat "$(F)" invokes the external batch file, passing the selected text as an argument to it;"%%~d\%~1" exists, its full path is printed to the console output;if "$(OUTPUT1)" != "" checks whether the output from "C:\tools\findfile.bat" is empty or not;$(OUTPUT1) as the file name to be opened: set local F = $(OUTPUT1);npp_open $(F).Basing on your needs, the batch file "findfile.bat" can be modified. For example, it can use the output of git ls-files :
@echo off
set WorkDir=C:\Projects\notepad-plus-plus
cd /D "%WorkDir%"
for /F %%f in ('git ls-files -- *%1*') do (
call :EchoUnquotedString "%WorkDir%\%%~f"
)
goto End
:EchoUnquotedString
echo %~1
goto End
:End