Home Download Buy Blog Forum Support

Opening and printing to Output panel

Opening and printing to Output panel

Postby FalconNL on Thu May 29, 2008 8:40 pm

I've been looking for a good editor for Haskell for a few months now and two days ago I discovered this one. I've tried pretty much all the other editors aimed at programmers and this is the first one that actually has correct syntax highlighting for Haskell. The python programmability makes it even more awesome. Too bad it isn't open source or even free, but I guess you can't have everything.

Before this I used Programmer's Notepad and that has one feature that isn't currently included: The ability to run other programs and pipe their output to the output panel (specifically, running Ghci or Runhaskell on the current file). No problem, Sublime is programmable so I figured I'd just make a plugin. Running a program and capturing the output as a string isn't too difficult (os.popen), but I'm having some problems with the output panel. Specifically, I can't find any API functions to show or print to it. I tried sublime.runCommand("showPanel output nofocus") but that does nothing. If these functions are currently available, can someone tell me what they're called? If not I'd like to request them being added to the API.

Thanks in advance.

EDIT: Also, the API doesn't list a way to save the file before running the program. Am I just looking in the wrong place?
FalconNL
 
Posts: 67
Joined: Thu May 29, 2008 8:17 pm

Re: Opening and printing to Output panel

Postby SteveCooperOrg on Fri May 30, 2008 9:00 am

Jon will hopefully answer in more detail if necessary, but have you had any luck with the build system? If you look in the Tools menu, you'll see a 'Build System' submenu, and a 'Haskell (GHC)' under that. Select that, and it'll run GHC when you hit F7 -- there are options on the same menu to save open files before build, and the output is piped to the output window. I think GHC must be in your path.

If you want, you can also write your own build options, say for Runhaskell. Copy the Haskel (ghc).sublime-build file into <user-app-data>\Sublime Text\Packages\Default and rename it to Runhaskell.sublime-build. Open it, and edit the command-line inside-- something like this?

Code: Select all
buildCommand exec "^(...*?):([0-9]*):?([0-9]*)" c:/ghc/runhaskell.exe '"$File"'


The Runhaskell build option will appear immediately in the list of build options from the tools menu.

Hope that does what you need...
Maybe what you're looking for is at the http://www.sublimetextwiki.com
SteveCooperOrg
 
Posts: 193
Joined: Thu Mar 20, 2008 9:53 am

Re: Opening and printing to Output panel

Postby SteveCooperOrg on Fri May 30, 2008 9:06 am

FalconNL wrote:Too bad it isn't open source or even free, but I guess you can't have everything.


If it helps, you could consider the price to be a support contract -- Jon is fantastically responsive at incorporating changes and helping with any problems. Hell, emacs is free, but try getting Richard Stallman to update emacs for 60 bucks ;)
Maybe what you're looking for is at the http://www.sublimetextwiki.com
SteveCooperOrg
 
Posts: 193
Joined: Thu Mar 20, 2008 9:53 am

Re: Opening and printing to Output panel

Postby FalconNL on Fri May 30, 2008 10:04 am

SteveCooperOrg wrote:Jon will hopefully answer in more detail if necessary, but have you had any luck with the build system? If you look in the Tools menu, you'll see a 'Build System' submenu, and a 'Haskell (GHC)' under that. Select that, and it'll run GHC when you hit F7 -- there are options on the same menu to save open files before build, and the output is piped to the output window. I think GHC must be in your path.

If you want, you can also write your own build options, say for Runhaskell. Copy the Haskel (ghc).sublime-build file into <user-app-data>\Sublime Text\Packages\Default and rename it to Runhaskell.sublime-build. Open it, and edit the command-line inside-- something like this?

Code: Select all
buildCommand exec "^(...*?):([0-9]*):?([0-9]*)" c:/ghc/runhaskell.exe '"$File"'


The Runhaskell build option will appear immediately in the list of build options from the tools menu.

Hope that does what you need...


The problem with that approach is that I often switch between runhaskell and ghci, which would require switching the build system every time. I chose the plugin route so I could bind runhaskell to F5 and Ghci to F6 just like I had in Programmer's Notepad. Thanks for the suggestion though.
FalconNL
 
Posts: 67
Joined: Thu May 29, 2008 8:17 pm

Re: Opening and printing to Output panel

Postby jps on Sat May 31, 2008 3:06 am

The .sublime-build files are a thin wrapper around the exec command, which you can use without having to deal with sublime-build files. What you want is to bind a key to the exec command directly: Create a Default.sublime-keymap file under the Packages/User directory (In C:\Documents and Settings\<username>\Application Data\Sublime Text on XP) that looks something like:

Code: Select all
<bindings>
   <binding key="F6" command="exec '^(...*?):([0-9]*):?([0-9]*)' ghc.exe '&quot;$File&quot;'"/>
   <binding key="F7" command="exec '^(...*?):([0-9]*):?([0-9]*)' ghci.exe '&quot;$File&quot;'"/>
</bindings>


Details on the exec command:

The exec command takes a regex to match line numbers as the first argument, with the remaining arguments being the command line to execute. Within the given command line, the following substitutions are available:

  • $FileDir (Directory portion of the file name)
  • $FileName (Non-directory portion of the file name)
  • $FileExt (File extension)
  • $File (Full file name)
  • $BaseName (Filename with no directory and no extension)

Because the regex should be a quoted string, any quotes contained within should be escaped, and any backslashes should be double escaped. The first submatch should match against the file name, the second against the line number, and the third against the column (All of which are optional). If you don't care about being able to double click on lines in the output window, just make the regex empty.

(In the above example, any double quotes have to encoded as &quot; to make the XML parser happy)
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: Opening and printing to Output panel

Postby FalconNL on Tue Jun 03, 2008 9:02 pm

Thanks for the help. I still have a couple of problems though.

1) Before running the exec command I want to save the file. No problem, just make a macro. Change the binding to
Code: Select all
<binding key="F5" command="runMacroFileSilently 'Packages/User/Runhaskell.sublime-macro'"/>

and make the macro file:
Code: Select all
save
exec '^(...*?):([0-9]*):?([0-9]*)' L:/progra~1/ghc-6.8.2/bin/runhaskell.exe '&quot;$File&quot;'

However, pressing F5 then produces the status message "Unknown macro command exec '^(...*?):([0-9]*):?([0-9]*)' L:/progra~1/ghc-6.8.2/bin/runhaskell.exe '&quot;$File&quot;'"
I tried replacing the &quot;s with "s, but no luck. Commenting out the line produces "Unknown macro command save". Hm. "expandSelectionTo line" works just like in the Delete Line macro, nextView produces another Unknown macro command. Are view-related commands not allowed in macros?

2) When I run the exec command directly from the keybinding everything works as expected, with one exception: The output window seems to have some sort of update problem. I have a test program that outputs one line of text of about 100 to 120 characters. After pressing F5 the output window only shows part of the line. The amount of characters printed is random. I was able to produce 3 characters, the entire string and plenty of in-between values. Clicking on the output window or switching to another application causes the entire string to be shown, so I assume this a screen update/redraw problem.

3) While the output panel is sufficient for runhaskell, which only produces output, it doesn't work for ghci, which is an interactive prompt. Therefore I still have to rely on the python script do use that one. Obviously, I need to save the file here as well. The obvious solution is to be to use
Code: Select all
sublime.runCommand('save')

but this doesn't seem to work. After pressing F6 the asterisk behind the filename doesn't disappear. Perhaps this is related to the problem in point 1?
FalconNL
 
Posts: 67
Joined: Thu May 29, 2008 8:17 pm

Re: Opening and printing to Output panel

Postby TBD on Tue Jun 03, 2008 10:08 pm

FalconNL: had the same problem and I solved it via a plugin:

Packages\User\plugin.py
Code: Select all
import sublime, sublimeplugin 
class SubCommand(sublimeplugin.TextCommand): 
    def run(self, view, args): 
      view.window().runCommand("save")
      cmd = "path to the .exe file " + view.fileName()
      view.window().runCommand("exec",["",cmd])


\Packages\User\Default.sublime-keymap
Code: Select all
<bindings>
   <binding key="alt+f1" command="sub"/>
</bindings>
TBD
 
Posts: 5
Joined: Sat May 03, 2008 11:41 am

Re: Opening and printing to Output panel

Postby FalconNL on Tue Jun 03, 2008 10:32 pm

EDIT: Brilliant, thanks. That solves points 1 and 3. Just one more to go.
FalconNL
 
Posts: 67
Joined: Thu May 29, 2008 8:17 pm

Re: Opening and printing to Output panel

Postby FalconNL on Mon Jun 09, 2008 7:00 pm

Until the update bug is resolved you can use the following workaround:

Code: Select all
view.window().runCommand("exec",["",cmd])
view.window().runCommand("exec",["","echo."])


This prints an empty line after the results of cmd, forcing the output window to be updated. This shifts the problem to the blank line, but we don't care if that gets printed or not.
FalconNL
 
Posts: 67
Joined: Thu May 29, 2008 8:17 pm

Re: Opening and printing to Output panel

Postby jps on Thu Jun 12, 2008 12:03 pm

I've tracked the update bug down, it'll be fixed in the next beta.

In the above work-around, you may run into issues with the second exec command: if the first hasn't finished by the time it's started, the former one will be cancelled.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Next

Return to Plugin Announcements

Who is online

Users browsing this forum: Google [Bot] and 6 guests