Running external app on window content?
-
Hi.
Long time user, first time poster. I hope someone in here can help me out.
I use notepad++ for coding and as python’s reliance on indents instead of brackets in the code, I use reformatting of code pretty often. Is it possible t ohave external apps like autopep8 or black change the contents in the active window? Or are there other pluging that does this? I’ve been searching, but haven’t been able to find anything - and I don’t want to switch to VS code…
Thanks!
-
It’s quite easy to do from inside a plugin.
The example plugin “pork2sausage”, which is available in Plugins Admin, actually demonstrates this. You use its Plugins > Pork to Sausage > Edit User Commands, and set up the path to the external program (autopep8 or whatever), the command line required, and a working directory for temp files. You can see the examples shown when the plugin is installed for how to do it (and the config file explains the various parameters needed. It works on the selection, rather than the whole file, but if you select-all first, that will work.
Alternatively, since I use NppExec for other things, I use that, rather than pork2sausage, for my “filtering” of active data. One example I have is decrypting a file using GPG:
cls npp_save cmd.exe /c exit %RANDOM% // cannot access $(SYS.RANDOM) directly through NppExec, but can tell cmd.exe to return it as a value set tempfile = $(SYS.TEMP)\NppGpgFile_$(EXITCODE).tmp // create random tempfile name set ascfile = $(SYS.TEMP)\NppGpgFile_$(EXITCODE).asc // create associated ascfile name sci_sendmsg SCI_SELECTALL // select all sel_saveto $(ascfile) :a // save selection to the ascfile (use :a to save as ansi, to prevent unicode prefix ÿþ getting embedded) gpg.exe --output "$(tempfile)" --decrypt "$(ascfile)" // decrypt sel_loadfrom $(tempfile) // replace selection with results sci_sendmsg SCI_DOCUMENTSTART // deselect rm -rf "$(tempfile)" "$(ascfile)" // cleanup temp files npp_save
So the first clears the NppExec console (not really needed), then saves the active file; to avoid overwriting things, it generates a random number, and uses that for setting up a few tempfile names in the %TEMP% direcotry, then it selects everything in the active document and writes it to a temporary document, then runs it through gpg.exe into a temporary output file, then takes the contents of that output and overwrites the current buffer.
This actually makes it look more confusing that it really is, but if autopep8 gets its input from a file passed as an argument, and can output to another file defined as an argument, this is one way to accomplish it.
-
Ah, right. I’ve written so many FAQ for the Community, I forgot FAQ: How do I … Convert my Text exists. That might explain things better, since it had more time put into writing it.