Plugin request: insert relative path

  • I’m using Notepad++ to maintain a (huge) website with thousands of internal links. And yes, I know I’m a dinosaur for not switching to Wordpress or something - but I want to have total control about every aspect of the site and total independence regarding software and servers.

    Anyway, while everything works just fine once I learned how to use regex (quite a learning curve…), there’s that one thing that would be a huge time-saver: a plug-in to create internal, relative links. Because currently it is an awful lot of clicking around, plus guesswork and/or debugging:

    • I have to search for the target file in a separate file browser
    • I then have to select and copy the file name (or alternatively the absolute path name, SHIFT+RMB in Windows)
    • Go back to Notepad++
    • Find where I have left and paste
    • Modify the path to be relative to the file I’m editing (which isn’t intuitive at all and requires very good knowledge of the structure of the site I’m editing)
    • Try out every single manually created link and fix the inevitable typos

    I imagine the path insert plugin to work as follows:

    • When invoked (via RMB menu or shortcut), it opens a file browser
    • I select one file, and the relative path from the current document to that file gets inserted at the cursor position. That’s it.

    I’m not a developer but that sounds pretty doable for someone who’s into plug-ins and UIs. I wouldn’t want to create the complete HTML <a> or <img> tags, but just the relative path, without quotes or anything (for maximum compatibility with just every markup that needs a relative path).

    Useful extra options:

    • Basic setting “Always open file browser from current editor window location” vs. “Open file browser from recalled last location” (the latter option will work better if all or most linked images are stored in one specific “images” dir, the first one is for linking “wildly” to files that are anywhere else on the server) --> very useful
    • Show selection of “Currently opened files” or “Recently opened files” alternatively or additionally to file browser --> very useful
    • Allow multiple file selection for multiple path insertion (helpful for image galleries and such) --> somewhat useful
    • Basic setting with a check box and/or text field “Use server root” / “Define server root” (a directory from where -optionally!- the link is created with leading “/”) --> for special cases
    • Any other suggestions?

    I would love to receive some feeback on this request. Perhaps I’m not alone with this issue.

  • @Bernd-Schneider

    So for maximal “professionalism”, the plugin is the way to go.
    But it is likely a great deal of work.

    Have you considered using a scripting language plugin to do something “quick and dirty”, yet still meet your basic need?

  • Thanks for your reply.

    Since I’ve never created a plugin before, I’m not sure how much can be accomplished with simple scripting.

    Would it be possible, with a script, to insert the list of currently/recently opened files into the RMB menu, so I can select one to create a relative link from? That would help with files that I know in advance I am likely to link to. Everything more powerful, I guess, would require a built-in file browser.

  • @Bernd-Schneider

    I think by “RMB” you mean “Right Mouse Button” and thus you mean the right-click context menu?
    If so, then I don’t think scripting could easily give you the ability to insert things there.

    What I do with scripting in such situations is to have it open a new “scratch” file, put data there, allow user to make a choice, and then do an action based upon that choice.
    Sort of a poor-man’s-user-interface.
    It works. Not super glitzy, but it works.

    The choice as I imagine it for you, would be to select one of a number of listed file, then do your action by inserting the appropriate data either directly in the document (as you said) or maybe into the clipboard if that is helpful.

    If the above seems reasonable, I could put a mock-up together that would illustrate how it could be accomplished.

    One thing I’m curious about: What kind of relative path are you looking for?
    It obviously has to be “relative” to something. :-)
    But how is that relative nature conveyed to something like what we’re discussing?

  • @Bernd-Schneider

    You could use the Notepad++ Explorer plugin as a GUI to select the file whose relative path you want to paste to your current document. This plugin is also able to cooperate with the NppExec plugin and execute scripts written in the NppExec scripting language. To get the relative path you want, there is a Windows API. To call it from a script you need the WinApiExec commandline tool.

    Follow these steps:

    1. Download the WinApiExec tool from >> here << and store it into a folder of your choice. Maybe you want to add this folder to your system’s PATH variable. This way you avoid to enter the full path to the tool in the script you will create later on.
    2. Go to (menu) Plugins -> Plugins Admin and install the NppExec plugin.
    3. After Notepad++ has been restarted install the Explorer plugin. Please note: This order might be important!
    4. After Notepad++ has been restarted go to (menu) Plugins -> Explorer -> Explorer Options and ensure that the content of the Path of Scripts input field points to a valid folder in your user profile. If this is not the case click the ... button and select an appropriate folder of your choice. Also ensure that the content of the DLL Name input field is set to NppExec.dll. Then close the dialog by clicking the OK button.
    5. Go to (menu) Plugins -> Explorer and tick the Explorer ... menu entry. The Explorer plugin panel should become visible.
    6. Navigate to an arbitrary folder (it doesn’t matter if you do that in the upper folder-only tree or in the lower folder-plus-files list) and right-click some item.
    7. In the context menu popping up go to NppExec script(s) -> Go to script folder. The Explorer plugin updates its panel and navigates to the folder you might have been set in step 4.
    8. Right-click at some empty space in the lower folder-plus-files list and click on New File in the context menu popping up. This will show a dialog box where you can enter the name of the new file. Enter PasteRelativePath.exec. Please note: The file extension .exec is important. After clicking the OK button a new empty file is created on disk and it’s opened in Notepad++.
    9. Copy and paste the following script code to this file and save it:
    //Explorer: NppExec.dll EXP_FULL_PATH[0]
    // ------------------------------------------------------------------
    // NOTE: The first line is in every script necessary
    // Format of the first line:
    //   //Explorer:          = Identification for Explorer support
    //   NppExec.dll          = NppExec DLL identification
    //   EXP_FULL_PATH[0] ... = Exec arguments - [0]=First selected file
    // ------------------------------------------------------------------
    // Example for selected files in file list of Explorer:
    // - C:\Folder1\Folder2\Filename1.Ext
    // - C:\Folder1\Folder2\Filename2.Ext
    // ------------------------------------------------------------------
    // EXP_FULL_PATH[1]       = C:\Folder1\Folder2\Filename2.Ext
    // EXP_ROOT_PATH[0]       = C:
    // EXP_PARENT_FULL_DIR[0] = C:\Folder1\Folder2
    // EXP_PARENT_DIR[0]      = Folder2
    // EXP_FULL_FILE[1]       = Filename2.Ext
    // EXP_FILE_NAME[0]       = Filename1
    // EXP_FILE_EXT[0]        = Ext
    // NppExec script body:
    npp_console keep
    // Get relative file path and put it to clipboard
    "winapiexec.exe" shlwapi.dll@PathRelativePathToW ( GlobalLock ( GlobalAlloc 0x0042 1024 ) ) "$(FULL_CURRENT_PATH)" 128 "$(ARGV[1])" 128 , GlobalUnlock $$:5 , u@OpenClipboard 0 , u@SetClipboardData 13 $$:5 , u@CloseClipboard
    // Convert backslashes to slashes
    set local $(RelPath) ~ strreplace $(CLIPBOARD_TEXT) "\" "/"
    sel_settext $(RelPath)

    Please note: If you haven’t added the folder where winapiexec.exe is stored to your system’s PATH variable you have to provide its full path in the script above.

    Now open one of your website’s files and place the cursor at some place where you want to insert the relative path of another file. Then go to Explorer plugin’s panel, navigate to that other file and right-click it. In the context menu popping up click on NppExec script(s) -> PasteRelativePath.exec (this is the script you created in step 9.). The relative path of the selected file should be inserted at the cursor position.

  • I implemented a similar task in javascript using a [jN](Адрес ссылки) plugin.
    The script that implements this functionality is located in the repository:
    Scripts involved: Intell.js, IntellHtml.js.
    alt text

  • So much other stuff to take care of, I forgot about this topic. Thanks for your replies, I will check out the options!

  • @dinkumoil Sorry for the late reply, just tested it, works like a charm. Thanks a lot!
    @TroshinDV May try it out later. Having a pop-up is intriguing so I don’t need to move to the Explorer window.

  • @Bernd-Schneider
    You could use tag lookup plugin.
    With ctags generate tags file where every file name is a tag. Then just lookup the filename regardless of the relative path.

    Usefulness depends on how you name the files. If you have 1000 file named page001.html then this approach would not help, but if most of your file names are rather unique then it should be the easiest way to go. Few duplication are not an issue because you could select them manually.

Log in to reply