' TextFilter.vbee ' ' This macro implements "text filter" functionality. The selected text in the ' current buffer (or the entire buffer if no text is selected) will be passed to ' stdIn for the command specified by the "osCmd" parameter. The originally selected ' text (or the entire buffer if no text is selected) will be replaced by the output of ' the command. ' ' USE AT YOUR OWN RISK ' ' Include this macro in another macro using the #include directive. ' Form an command to call and pass it the TextFilter proceedure as shown in the following ' example: ' ' ' #include ".\TextFilter.vbee" ' ' osCmd = "java -cp ""path\to\some.jar"" someclass" ' TextFilter osCmd ' ' ' Function getFileText 'store the current selection info cursorX = document.selection.GetActivePointX(eePosView) cursorY = document.selection.GetActivePointY(eePosView) anchorX = document.selection.GetAnchorPointX(eePosView) anchorY = document.selection.GetAnchorPointY(eePosView) document.selection.SelectAll fullText = document.selection.Text document.selection.SetAnchorPoint eePosView, anchorX, anchorY document.selection.SetActivePoint eePosView, cursorX, cursorY getFileText = fullText end Function Sub TextFilter (osCmd) noSelection = document.selection.IsEmpty ' get selection (or full doc if selection is empty) if noSelection then selText = getFileText() else selText = document.selection.Text end if ' create wsh objects set wss = CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") Set env = wss.Environment("USER") ' get user temp dir usrTempDir = wss.ExpandEnvironmentStrings(env("TEMP")) ' setup temp files tempFilePathIn = usrTempDir + "\emeditor-textfilter-in.tmp" tempFilePathOut = usrTempDir + "\emeditor-textfilter-out.tmp" ' write selected text to temp input file Set fileIn = fso.CreateTextFile(tempFilePathIn, true) fileIn.WriteLine(selText) fileIn.Close ' call shell command shellCmd = "%comspec% /c type """ + tempFilePathIn + """ | " + osCmd + " 2>&1 > """ + tempFilePathOut + """" wss.Run shellCmd, 0, True newText = "" Set tempFile = fso.OpenTextFile(tempFilePathOut, 1) Do While tempFile.AtEndOfStream <> True tempFileLine = tempFile.ReadLine newText = newText + tempFileLine + vbcrlf Loop tempFile.Close fso.DeleteFile(tempFilePathIn) fso.DeleteFile(tempFilePathOut) if noSelection then document.selection.SelectAll end if document.selection.Text = newText end sub