d0vgan / nppexec

NppExec (plugin for Notepad++)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

enhancement request -- Allow file type association with [Menu items]

David-Maisonave opened this issue · comments

Can an option be added to the Advanced Options window which would let users optionally add a file association with the submenu item.
NppExecAdvancedOptionsMenuItems_AddedField
And then add logic to the execute script code so that if the current document file type has an associated submenu item, it will run that script.

This will make it easy to press the same shortcut key to execute a script no matter what file type the users switch to.

With that same logic, I would like to add a default script, having type *.*, where the default script will be used if it can't find any file associations.

Please refer to NppExec Manual, section 4.7.4. Compiling ANY source file.
It explains the way to achieve the very same behavior as you discribed, without the additional layer of new GUI elements.

I previously looked at the help document, and I even looked at (4.7.4). But I didn't understand it, and I thought it didn't apply to what I was trying to do.
After you referenced it, I looked at it again, and I saw the reference to the "NppExec_Guide.txt". That's actually where the information is located for the ANY source file feature. I had to read "NppExec_Guide.txt" file twice before understanding it.
That information should be in the help document, so the user can find it in the table of content or through search. The help search does not return a reference to this text file for any of it's content. Also the text file makes the assumption that users are looking to compile source code. For script files, I think most users would want to be able to run the scripts.

I actually like the last example in the text file, because you only need one script file to make it work with any file type.

IMHO, I think NppExec should come with this type of script by default. Below is an enhanced version of the script I created, which can handle more file types.
I think it would be a good example to include in the NppExec help file.

// * Run any file base on the file extension type
//    ** If script file, run it
//    ** If source code, compile and run
//    ** If Unknown type display a message or modifty this script to perform some other default behavior
//    ** If needed, add additional if conditions for other file types, and the desired execution behavior

// Setting internal messages off
npe_console local m- --
// Saving current file  *******************************
NPP_SAVE
// file extension in lower case
set local ext ~ strlower $(EXT_PART)
set obj =$(CURRENT_DIRECTORY)\$(NAME_PART)
set CompExec=$(obj).exe
set Compile =""


// *** Run if script file *******************************
if $(ext) == .awk
// Default installation path.  Modify if using non-standard path for gawk
"C:\Program Files (x86)\GnuWin32\bin\gawk.exe" -f "$(FULL_CURRENT_PATH)"
else if $(ext) == .bat
cmd /c "$(FULL_CURRENT_PATH)"
else if $(ext) == .cmd
cmd /c "$(FULL_CURRENT_PATH)"
else if $(ext) == .py
python "$(FULL_CURRENT_PATH)"
else if $(ext) == .pyw
pythonw "$(FULL_CURRENT_PATH)"
else if $(ext) == .ps1
powershell -executionpolicy bypass -File "$(FULL_CURRENT_PATH)"
else if $(ext) == .vbs
echo "*** Using CScript to get output to NppExec console window ***"
cscript.exe "$(FULL_CURRENT_PATH)"
else if $(ext) == .js
node "$(FULL_CURRENT_PATH)"
else if $(ext) == .lua
node "$(FULL_CURRENT_PATH)"
else if $(ext) == .php
php "$(FULL_CURRENT_PATH)"
// *** If source code requiring compiling, compile and run  *******************************
else if $(ext) == .cs
set Compile = C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc
goto :CompileAndExecute
else if $(ext) == .c
set Compile = "C:\tools\tcc\tcc.exe" -run 
goto :CompileAndExecute
else if $(ext) == .cpp
env_set local PATH = $(path);C:\mingw32\bin;
cmd /C del "$(obj).o" 2>nul & del "$(CompExec)" 2>nul
echo Compiling $(NAME_PART)...
"g++" -c "$(FULL_CURRENT_PATH)"
if $(EXITCODE) == 0
  "g++" "$(obj).o" -o "$(CompExec)"
  echo Running $(CompExec) ...
  NPP_RUN cmd /C if exist "$(CompExec)" "$(CompExec)" && echo. && pause
endif
else
// If needed, replace the following line with desired default behavior
messagebox "Unknown file type!"
// Example alternate default behavior
// cmd /c "$(FULL_CURRENT_PATH)"
endif
Goto :END

:CompileAndExecute
echo Compiling '$(NAME_PART)' to create '$(CompExec)'
$(Compile) "$(FULL_CURRENT_PATH)"
if $(EXITCODE) == 0
	echo Running $(CompExec) ...
	cmd /C if exist "$(CompExec)" "$(CompExec)" 
else
	echo Error: Failed to compile $(FULL_CURRENT_PATH)
endif

:END

Yes, I think it makes sense to convert the "NppExec_Guide.txt" into an .html page and update it with what you suggested.
By the way, what is node for lua? I mean this part:

else if $(ext) == .lua
node "$(FULL_CURRENT_PATH)"

Shouldn't it be e.g. lua53.exe ?

I was able to figure out a way to check file existence.

Here's how I did it.

else if $(ext) == .lua
cmd /c if not exist $(LuaRunnerX64) exit 1
if $(EXITCODE) == 0
	$(LuaRunnerX64) "$(FULL_CURRENT_PATH)"
else
	$(LuaRunnerX86) "$(FULL_CURRENT_PATH)"
endif

Please let me know if there's a better way to do this.

  • Could you add a hidden option to run previous script from previous session the first time running [Execute Previous NppExec Script]?
    I never want to see the [Execute NppExec Script] window unless I selected it.

  • Is there anyway I could set CustomMsgReady value from inside the script?

I'm considering creating a Notepad++ plugin "ProgramAnything", in which I would include NppExec as part of the install.
The goal of the plugin will be to make it easier to use Notepad++ as an IDE for all the most common programming languages.

  • Do you have any objections or concerns?

I cloned NppExec, and open the solution using Enterprise VS2017. When I tried to view resources (NppExec.rc), I get the following error:
error RC2247 : SYMBOL name too long"

Do you have any idea on how to fix that?
What [SDK Version] are you using when you build this on VS2017?
And also what [Platform Toolset]?
Both of theses options can be viewed from Project->Properties->Config..Prop..->General.

I'm still able to compile it, but I would like to be able to try adding a few dialogs to it.

For now, I'm looking to see if I can add an OnDblClick function to the NppExec Console window.
I want the function to read the line, and parse it for a file name and line number. And if it finds it, (if not open) open the file, and then jump to the line number.
This would allow a developer to jump directly to a display an error when the file has a syntax error.
Even though script handlers, compilers and linkers produce a variety of formats I can parse it using regex.
This regex string [a-zA-Z]:[\\/][0-9a-zA-Z/\\\s\-_]+\.[0-9a-zA-Z_]+ can get all the file names from the below error list, except for the C# error, which only list the file name.

  File "E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.py", line 8, in <module>
  File "E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.py", line 5, in main
  File "E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.pyw", line 8, in <module>
  File "E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.pyw", line 5, in main
E:/_Dev/Scripts/NppTest/_WorkingScripts_/BadHelloWorld/BadHelloWorld.rb:5: syntax error, unexpected string literal, expecting end-of-input
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.vb(3) : error BC30625: 'Module' statement must end with a matching 'End Module'.
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.vb(4) : error BC30026: 'End Sub' expected.
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.vb(5) : error BC32017: Comma, ')', or a valid expression continuation expected.
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.vb(5) : error BC30203: Identifier expected.
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.vb(5) : error BC30648: String constants must end with a double quote.
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.vbs(5, 1) Microsoft VBScript runtime error: Object doesn't support this property or method: 'WScript.Echxo'
gawk: E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.awk:5: BEGIN { printxf "%s\n","Hello World from AWK (gawk)" }
gawk: E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.awk:5:                       ^ syntax error
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.c: In function 'main':
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.c:5:9: error: expected ';' before string constant
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.c:5:33: error: expected statement before ')' token
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.cpp: In function 'int main()':
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.cpp:5:9: error: expected ';' before string constant
BadHelloWorld.cs(5,40): error CS1040: Preprocessor directives must appear as the first non-whitespace character on a line
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.java:5: error: not a statement
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.java:5: error: ';' expected
E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.js:5
C:\Program Files\Lua\lua54.exe: ...NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.lua:5: unexpected symbol near ')'
Parse error: syntax error, unexpected '"Hello World from PHP\n"' (T_CONSTANT_ENCAPSED_STRING) in E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.php on line 5
At E:\_Dev\Scripts\NppTest\_WorkingScripts_\BadHelloWorld\BadHelloWorld.ps1:5 char:41

If I get it working would you be interested in including the implementation in NppExec?

Could you add a hidden option to run previous script from previous session the first time running [Execute Previous NppExec Script]?

Could be.

Is there anyway I could set CustomMsgReady value from inside the script?

Yes. Just specify

CustomMsgReady=$(MSG_READY)\r

inside of "NppExec.ini" and then set MSG_READY during runtime (don't forget to specify its initial value in NppExec's startup script).

I'm considering creating a Notepad++ plugin "ProgramAnything"

You may create a "master" plugin that will use NppExec as a worker. See the NppExecPluginMsgTester in NppExec's sources as an example. The NppExecPluginMsgTester uses the header file "NppExec\src\PluginCommunication\nppexec_msgs.h" that explains the "API" that NppExec proposes to external plugins.

When I tried to view resources (NppExec.rc), I get the following error (...)

NppExec's source code is written using Visual Studio Community, and previously it was written using Visual Studio Express where there was no resource editor available. For these historical reasons, the "NppExec.rc" was mostly edited manually and most likely it contains some tweaks that will be ruined by editing this file by means of a resource editor.
As an .rc file is a simple text file, you may create another .rc file in the scope of another project, copy some dialogs from "NppExec.rc" to that new .rc file, modify the dialogs in that new .rc file by means of a resource editor and then manually carefully copy the modified dialogs back to "NppExec.rc".

I'm looking to see if I can add an OnDblClick function to the NppExec Console window

It's already there. The same "NppExec_Guide (plain text)" mentions that "you can jump to corresponding line in the source file by double-clicking on such warning or error message in the NppExec's Console" using the "Console Output Filters... -> HighLight".
Within the source code, the file "NppExec\src\WarningAnalyzer.cpp" is responsible for parsing the warning/error messages. Currently it does not support regular expressions, but it is a good idea to add this support as you suggested.

I'm testing CWarningAnalyzer::match, and it's not picking up any of the error messages with the syntax I posted.
I've added an else to the "if (pszMask)", and in the else statement I have the regex code to pickup all those errors.
I also have it working with the coloring.
All the new code is in the else condition, so it shouldn't effect anything else.
I'll post a pull request tonight.

Thanks for your help.