Sublime Forum

Opening and printing to Output panel

#1

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?

0 Likes

Resizing Panes, Console, Output panel
#2

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 \Sublime Text\Packages\Default and rename it to Runhaskell.sublime-build. Open it, and edit the command-line inside-- something like this?

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…

0 Likes

#3

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 :wink:

0 Likes

#4

[quote=“SteveCooperOrg”]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 \Sublime Text\Packages\Default and rename it to Runhaskell.sublime-build. Open it, and edit the command-line inside-- something like this?

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…[/quote]

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.

0 Likes

#5

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:

<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 " to make the XML parser happy)

0 Likes

#6

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
<binding key="F5" command="runMacroFileSilently 'Packages/User/Runhaskell.sublime-macro'"/>

and make the macro file:

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 '”$File"’"
I tried replacing the "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?

  1. 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.

  2. 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

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?

0 Likes

#7

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

Packages\User\plugin.py

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

<bindings>
	<binding key="alt+f1" command="sub"/>
</bindings>
0 Likes

#8

EDIT: Brilliant, thanks. That solves points 1 and 3. Just one more to go.

0 Likes

#9

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

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.

0 Likes

#10

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.

0 Likes

#11

Thanks for the fix jps :sunglasses:

0 Likes