Menu

Open file(s) basing on the text selected in the editor

NppExec
DV
2015-02-24
2017-11-22
  • DV

    DV - 2015-02-24

    I was wondering whether NppExec already allow, for example, to open a file "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)
    if "$(F)" == "" goto Done // no selected text
    
    // check current file name...
    set local n ~ strfind "$(FILE_NAME)" "$(F)"
    if $(n) != -1 goto Done // current file name contains the selected text
    
    // check opened files...
    set local prev_fpath = $(FULL_CURRENT_PATH)
    NPP_SWITCH $(F)
    if "$(FULL_CURRENT_PATH)" != "$(prev_fpath)" goto Done // switched to another opened file
    
    // check predefined directories...
    NPE_CONSOLE v+ --
    C:\tools\findfile.bat "$(F)"
    if "$(OUTPUTL)" == "" goto Open
    set local F = $(OUTPUTL) // found in a predefined dir
    goto Open
    
    :Open
    NPE_CONSOLE v- --
    NPP_OPEN $(F)
    :Done
    

    This NppExec's script uses external file "C:\tools\findfile.bat" which describes the set of predefined directories to be checked.
    Here is the 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", "C:\Program Files (x86)" ).
    Define your own set of directories here.

    Here is how it works:
    1) The NppExec's statement
    C:\tools\findfile.bat "$(F)"
    invokes the external batch file, passing the selected text as an argument to it;
    2) (Inside of "C:\tools\findfile.bat") When "%%~d\%~1" exists, its full path is printed to the console output;
    3) Then NppExec's statement
    if "$(OUTPUTL)" == "" goto Open
    verifies whether the output from "C:\tools\findfile.bat" is empty or not;
    4) If it is not empty, we take $(OUTPUTL) as the file name to be opened:
    set local F = $(OUTPUTL) // found in a predefined dir
    5) Finally, the file is opened by
    NPP_OPEN $(F)

     
  • Christophe Meriaux

    High DV,
    thank you very much for this script. I've done some modification to use git list file command inside a repository (git ls-files -- file_name ). It works perfectly fine.

    I used "if <condition> goto <label>" command but I'd like to have a new command :
    "if <condition> then <action_if_true> else <action_if_false>"
    With this command, script would be easily written.

    Thanks for your script
    Christophe

     
  • DV

    DV - 2017-11-22

    From NppExec v0.6 alpha 1, the same script can look like the following:

    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 -
        NPP_SWITCH $(F) // try to switch to another file
        NPP_CONSOLE +
        if "$(FULL_CURRENT_PATH)" == "$(prev_fpath)" then // did not switch to another file
          // check predefined directories...
          NPE_CONSOLE v+ --
          C:\tools\findfile.bat "$(F)"
          if "$(OUTPUTL)" != "" then // found in a predefined dir
            set local F = $(OUTPUTL) 
          endif
          NPE_CONSOLE v- --
          NPP_OPEN $(F)
        endif
      endif
    endif
    
     

Log in to post a comment.