NPPExec: is it possible to detect if text is selected or not?
-
I’m just beginning to dip my foot into NPPExec, but I want to make sure my expectations match reality.
My Goal:
I want to convert html to markdown.
My Approach:
I’m using the external program Pandoc to do the conversion, and NPPExec to call the program. [I tried to post a link to Pandoc, but Akismet is flagging this post as spam, so you’ll have to look it up if you’re interested.]
I’ve copied an example program by @PeterJones from a post here on the forum (Thank you, PeterJones!). It takes the current document, selects all, hands it over to Pandoc and pastes it back into the original document:
cls cmd.exe /c exit %RANDOM% // get a random number for the temporary filenames set tempfile = $(SYS.TEMP)\NppMdFile_$(EXITCODE).tmp // create a random tempfile name set mdfile = $(SYS.TEMP)\NppMdFile_$(EXITCODE).md // create an associated markdown name sci_sendmsg SCI_SELECTALL // select all sel_saveto $(tempfile) :a // save selection to the tempfile (use :a to save as ansi, to prevent unicode prefix ÿþ getting embedded) c:\utils\pandoc\pandoc -f html -t markdown -o "$(mdfile)" "$(tempfile)" // convert sel_loadfrom $(mdfile) // replace selection with results sci_sendmsg SCI_DOCUMENTSTART // deselect rm -rf "$(tempfile)" "$(mdfile)" // clean up temp files
What I’d like to have happen:
If nothing is selected: run all of the text through Pandoc (this is the current state of the script).
If text is selected: run only the selected text through Pandoc.My questions are:
- Can NPPExec determine if text is currently selected? And if so,
- is NPPExec capable of branching based on that determination?
Thank you for any help.
-
I may have asked too fast because I think I solved my own problem. Here is the script as updated:
cls cmd.exe /c exit %RANDOM% // get a random number for the temporary filenames set tempfile = $(SYS.TEMP)\NppMdFile_$(EXITCODE).tmp // create a random tempfile name set mdfile = $(SYS.TEMP)\NppMdFile_$(EXITCODE).md // create an associated markdown name sci_sendmsg SCI_GETSELTEXT // Check if text is selected set len = $(MSG_RESULT) // get result if $(len) == 0 then // If no text is selected, select all sci_sendmsg SCI_SELECTALL // select all endif sel_saveto $(tempfile) :a // save selection to the tempfile (use :a to save as ansi) c:\utils\pandoc\pandoc -f html -t markdown -o "$(mdfile)" "$(tempfile)" // convert using pandoc sel_loadfrom $(mdfile) // replace selection with results sci_sendmsg SCI_DOCUMENTSTART // deselect rm -rf "$(tempfile)" "$(mdfile)" // clean up temp files
Would this the the best way to approach this problem? I’m open to alternative approaches, including ones that don’t require the use of NPPExec.
-
If you want to detect if there is exactly one selection of nonzero length, do the following:
- Call
SCI_GETSELECTIONS
. If the result is greater than 1, there are multiple selections. - Call
SCI_GETSELECTIONSTART
andSCI_GETSELECTIONEND
to get the start and end of the selection. If the end is greater than the start, the user has a single nonzero-length selection. - As you correctly identified, you can then use
SCI_GETSELTEXT
to get the selected text, although in this script it doesn’t look like that’s really necessary.
Based on a recent issue I saw with the JSON-Viewer plugin where incorrectly assuming that the user always had a single selection caused NPP to crash, I think that validating that there is a single selection is generally good practice,
- Call