Community
    • Login

    NPPExec: is it possible to detect if text is selected or not?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 177 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • pbarneyP
      pbarney
      last edited by

      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:

      1. Can NPPExec determine if text is currently selected? And if so,
      2. is NPPExec capable of branching based on that determination?

      Thank you for any help.

      pbarneyP 1 Reply Last reply Reply Quote 2
      • pbarneyP
        pbarney @pbarney
        last edited by

        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.

        1 Reply Last reply Reply Quote 3
        • Mark OlsonM
          Mark Olson
          last edited by

          @pbarney

          If you want to detect if there is exactly one selection of nonzero length, do the following:

          1. Call SCI_GETSELECTIONS. If the result is greater than 1, there are multiple selections.
          2. Call SCI_GETSELECTIONSTART and SCI_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.
          3. 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,

          1 Reply Last reply Reply Quote 3
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors