Skip to content

Commit

Permalink
change subl-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Sep 10, 2015
1 parent 7189d8b commit a5053d4
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 63 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ Update config/app.php
### Editor Link

```
go to <vendor path>/recca0120/laravel-tracy/tools/open-in-editor
copy <vendor path>/recca0120/laravel-tracy/tools/subl-handler/subl-handler.vbs to any directory where you want to place
run as administrator and click install.cmd
double click subl-handler.vbs and select editor (support eclipse, sublime, notepad++, else...)
```


2 changes: 1 addition & 1 deletion config/tracy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'maxDepth' => 4,
'maxLen' => 1000,
'showLocation' => true,
'editor' => 'subl://open/?file=%file&line=%line',
'editor' => 'subl://open?url=file://%file&line=%line',
'panels' => [
'Recca0120\LaravelTracy\Panels\RoutingPanel',
'Recca0120\LaravelTracy\Panels\ConnectionPanel',
Expand Down
8 changes: 0 additions & 8 deletions tools/open-in-editor/install.cmd

This file was deleted.

50 changes: 0 additions & 50 deletions tools/open-in-editor/open-editor.js

This file was deleted.

21 changes: 21 additions & 0 deletions tools/subl-handler/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Kamil Tunkiewicz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions tools/subl-handler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
subl-handler
============

## What is this?

This vbs script handles `subl:` url scheme to open it in your text editor.

Example link
``` subl://open?url=file://C:\htdocs\myapp\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php&line=214 ```

I found it very useful when debugging Laravel based applications because Laravel's exception page use `subl:` links on every filename.
Of course it works only on local webserver (in your local filesystem).

## Compatibility

I tested this script on Windows 8 with Sublime 3, Netbeans 8 and Notepad++ 6.6.8 but it should work on other Windows systems as well.

Text editors that I know to support line numbers using `filename:###` syntax (option 1 in setup)
- sublime
- netbeans

Text editors that I know to support line numbers using `filename -n###` syntax (option 2 in setup)
- notepad++

Text editors that I know *not supporting* line numbers in filename:
- eclipse
- pspad
- notepad
- and many other editors that use standard windows file open syntax (`application.exe %1`)

## Installation

- copy the selected script to any location on your disk. I suggest to copy it to c: or c:\Users or something like this. The file will have to stay there because it will handle `subl:` links and follow it to your text editor
- run the script and follow instructions on the screen

## Usage

Click on any `subl:` link to see it working.

*Note for Mozilla Firefox users*: Firefox will ask you what to do with subl: links for the first time you click on it. Simply click the OK button and Firefox will choose the default action, which is this script since you installed it. Firefox will ask about this only once.

## How does it work? / Is it safe?

It is safe to run this script, it doesn't change any files in your system. It only adds some small entry into windows registry.

When you run this script without parameters it will run itself as administrator to be able to import .reg file into windows registry.
The imported .reg file will look something like this:
```
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\subl]
@="URL:subl Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\subl\shell]
[HKEY_CLASSES_ROOT\subl\shell\open]
[HKEY_CLASSES_ROOT\subl\shell\open\command]
@="\"wscript.exe\" \"C:\\subl-handler.vbs\" 1 \"C:\\Program Files\\Sublime Text 3\\sublime_text.exe\" %1"
```

This adds the subl-handler.vbs script as a handler for `subl:` protocol so when you click `subl:` link your browser will run the script. The script reads the link, parse it and run the selected text editor with proper command line arguments.
138 changes: 138 additions & 0 deletions tools/subl-handler/subl-handler.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
if WScript.Arguments.Count = 0 then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """" & " RunAsAdministrator", , "runas", 1
WScript.Quit
end if

Function URLDecode(ByVal str)
''
' from http://dwarf1711.blogspot.com/2007/10/vbscript-urldecode-function.html
''
Dim intI, strChar, strRes
str = Replace(str, "+", " ")
For intI = 1 To Len(str)
strChar = Mid(str, intI, 1)
If strChar = "%" Then
If intI + 2 < Len(str) Then
strRes = strRes & Chr(CLng("&H" & Mid(str, intI+1, 2)))
intI = intI + 2
End If
Else
strRes = strRes & strChar
End If
Next
URLDecode = strRes
End Function

Function SelectFile( )
' File Browser via HTA
' Author: Rudi Degrande, modifications by Denis St-Pierre and Rob van der Woude
' Features: Works in Windows Vista and up (Should also work in XP).
' Fairly fast.
' All native code/controls (No 3rd party DLL/ XP DLL).
' Caveats: Cannot define default starting folder.
' Uses last folder used with MSHTA.EXE stored in Binary in [HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32].
' Dialog title says "Choose file to upload".
' Source: http://social.technet.microsoft.com/Forums/scriptcenter/en-US/a3b358e8-15&alig;-4ba3-bca5-ec349df65ef6

Dim objExec, strMSHTA, wshShell

SelectFile = ""

' For use in HTAs as well as "plain" VBScript:
strMSHTA = "mshta.exe ""about:" & "<" & "input type=file id=FILE>" _
& "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _
& ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>"""
' For use in "plain" VBScript only:
' strMSHTA = "mshta.exe ""about:<input type=file id=FILE>" _
' & "<script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _
' & ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>"""

Set wshShell = CreateObject( "WScript.Shell" )
Set objExec = wshShell.Exec( strMSHTA )

SelectFile = objExec.StdOut.ReadLine( )

Set objExec = Nothing
Set wshShell = Nothing
End Function


if WScript.Arguments(0) = "RunAsAdministrator" then

if MsgBox("Do you want do install subl: url scheme handler?",4+vbSystemModal,"subl-handler setup") = 6 then

editor_no = InputBox("1 - Sublime or NetBeans" & chr(13) & "2 - Notepad++" & chr(13) & "0 - other text editor (no automatic line number highliting)", "Select your text editor", 0)
Select Case editor_no
Case 1
editor = "Eclipse or Netbeans"
Case 2
editor = "Notepad++"
Case Else
editor = "text editor"
editor_no = 0
End Select

temp = MsgBox ("Click on OK to open the file selection window and pick the "& editor &" EXE file (problably somewhere in your Program Files directory).",0+vbSystemModal,"Next step")
filename = SelectFile()

if filename = "" then
temp = MsgBox ("Aborted installation",0+vbSystemModal,"Aborted")
WScript.Quit
end if

Set objFSO=CreateObject("Scripting.FileSystemObject")
temp = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%Temp%")

outFile="tmp.reg"
Set objFile = objFSO.CreateTextFile(temp&"\"&outFile,True)
objFile.Write "Windows Registry Editor Version 5.00" & vbCrLf
objFile.Write "" & vbCrLf
objFile.Write "[HKEY_CLASSES_ROOT\subl]" & vbCrLf
objFile.Write "@="&chr(34)&"URL:subl Protocol"&chr(34) & vbCrLf
objFile.Write ""&chr(34)&"URL Protocol"&chr(34)&"="&chr(34)&chr(34) & vbCrLf
objFile.Write "" & vbCrLf
objFile.Write "[HKEY_CLASSES_ROOT\subl\shell]" & vbCrLf
objFile.Write "" & vbCrLf
objFile.Write "[HKEY_CLASSES_ROOT\subl\shell\open]" & vbCrLf
objFile.Write "" & vbCrLf
objFile.Write "[HKEY_CLASSES_ROOT\subl\shell\open\command]" & vbCrLf
objFile.Write "@="&chr(34)&"\"&chr(34)&"wscript.exe"&"\"&chr(34)&" "&"\"&chr(34)&Replace(WScript.ScriptFullName,"\","\\")&"\"&chr(34)&" "& editor_no &" \"&chr(34)&Replace(filename,"\","\\")&"\"&chr(34)&" %1"&chr(34)&"" & vbCrLf
objFile.Close

Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "regedit.exe", "/S """ & temp & "\" & outFile & """" & " RunAsAdministrator", , "runas", 1

temp = MsgBox ("Successfully installed subl: url scheme handler", 0, "Success")
WScript.Quit

end if
else

path_add = ""
Select Case WScript.Arguments(0)
Case 1
path_add = ":"
Case 2
path_add = " -n"
End Select

str = URLDecode(WScript.Arguments(2))
Set re = New RegExp
re.Pattern = "subl://open.?\?url=file://(.+)&line=([0-9]+)"
re.IgnoreCase = True
re.Global = False
Set matches = re.Execute(str)
If matches.Count > 0 Then
Set match = matches(0)
If match.SubMatches.Count > 0 Then
Set ObjShell = CreateObject("Shell.Application")
if path_add = "" then
ObjShell.ShellExecute Wscript.Arguments(1), chr(34) & match.SubMatches(0) & chr(34), , "open", 1
else
ObjShell.ShellExecute Wscript.Arguments(1), chr(34) & match.SubMatches(0) & chr(34) & path_add & match.SubMatches(1), , "open", 1
end if
End If
End If

end if

0 comments on commit a5053d4

Please sign in to comment.