From e9bc43a353217006144fc4ed127ae91358547926 Mon Sep 17 00:00:00 2001
From: Cheng Sheng <chengsheng@google.com>
Date: Wed, 31 May 2017 23:34:31 +0200
Subject: [PATCH] "Show Datasheet" button: Resolve env-vars (like ${KIPRJMOD})
 if appears.

This way, the datasheets can be stored within the project dir, and referred as ${KIPRJMOD}/relative/path/to/datasheet.
---
 AUTHORS.txt                                            |  1 +
 .../dialogs/dialog_edit_component_in_schematic.cpp     | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/AUTHORS.txt b/AUTHORS.txt
index f447c6d..e489e8c 100644
--- a/AUTHORS.txt
+++ b/AUTHORS.txt
@@ -38,6 +38,7 @@ Chris Pavlina <pavlina.chris[at]gmail-dot-com>
 Simon Richter <Simon.Richter[at]hogyros-dot-de>
 Mario Luzeiro <mrluzeiro[at]ua-dot-pt>
 Mateusz Skowroński <skowri[at]gmail-dot-com>
+Google Inc.
 See also CHANGELOG.txt for contributors.
 
 
diff --git a/eeschema/dialogs/dialog_edit_component_in_schematic.cpp b/eeschema/dialogs/dialog_edit_component_in_schematic.cpp
index c66edd6..4c2749a 100644
--- a/eeschema/dialogs/dialog_edit_component_in_schematic.cpp
+++ b/eeschema/dialogs/dialog_edit_component_in_schematic.cpp
@@ -27,6 +27,7 @@
 
 #include <wx/tooltip.h>
 #include <wx/hyperlink.h>
+#include <wx/regex.h>
 
 #include <fctsys.h>
 #include <pgm_base.h>
@@ -51,6 +52,8 @@
 #include <netlist_exporter_pspice.h>
 #endif /* KICAD_SPICE */
 
+#include "common.h"
+
 
 /**
  * class DIALOG_EDIT_COMPONENT_IN_SCHEMATIC
@@ -554,6 +557,20 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::deleteFieldButtonHandler( wxCommandEven
     m_skipCopyFromPanel = false;
 }
 
+static wxString resolveUriByEnvVars( const wxString& aUri )
+{
+    // URL-like URI: return as is.
+    wxRegEx regex_with_protocol;
+    bool succ = regex_with_protocol.Compile( "^[[:alnum:]]+://.*$" );
+    wxASSERT( succ );
+    if( regex_with_protocol.Matches( aUri ) )
+    {
+        return aUri;
+    }
+    // Otherwise, the path points to a local file. Resolve environment
+    // variables if any.
+    return ExpandEnvVarSubstitutions( aUri );
+}
 
 void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::showButtonHandler( wxCommandEvent& event )
 {
@@ -562,6 +579,7 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::showButtonHandler( wxCommandEvent& even
     if( fieldNdx == DATASHEET )
     {
         wxString datasheet_uri = fieldValueTextCtrl->GetValue();
+        datasheet_uri = resolveUriByEnvVars( datasheet_uri );
         ::wxLaunchDefaultBrowser( datasheet_uri );
     }
     else if( fieldNdx == FOOTPRINT )
-- 
1.9.5 (Apple Git-50.3)

