       #@filename = editor.getPath()
       @code = editor.getText()
 
-
+    # Get language and filename
     grammar = editor.getGrammar()
     @lang = grammar.name
     @filename = editor.getTitle()

  @filename: null

  @content: ->
    @div class: 'tool-panel panel panel-bottom padding script', outlet: 'script', tabindex: -1, =>
      @div class: 'panel-heading padded heading', outlet: 'heading'
      @div class: 'panel-body padded output', outlet: 'output'

  initialize: (serializeState) ->
    atom.workspaceView.command "script:run", => @show()
    atom.workspaceView.command "script:close-view", => @close()
    atom.workspaceView.command "script:kill-process", => @stop()

  serialize: ->

  destroy: ->
    # Dismiss window and stop any running process
    @detach()
    @stop()

  show: ->
    # Display window and load message
    if not @hasParent()
      atom.workspaceView.prependToBottom(this)
    @heading.text("Loading...")
    @output.empty()
    # Close any existing process and start a new one
    @stop()
    @start()

  close: ->
    # Dismiss window and stop any running process
    if @hasParent()
      @detach()
      @stop()

  start: ->
    # Display error or run script
    @check()
    if @err? then @handleError() else @run()

  handleError: ->
    # Display error and kill process
    @heading.text("Error")
    @display("error", @err)
    @err = null
    @stop()

  run: ->
    # Precondition: @lang? and @lang of grammarMap
    command = grammarMap[@lang]["command"]
    makeargs = grammarMap[@lang]["bySelectionArgs"]

    args = makeargs(@code)

    # Default to where the user opened atom
    options =
      cwd: atom.project.getPath()
      env: process.env

    # Set up process and output display
    @heading.text(@lang + " - " + @filename)
    stdout = (output) => @display("stdout", output)
    stderr = (output) => @display("stderr", output)
    exit = (return_code) -> console.log "Exited with #{return_code}"

    # Run process
    @bufferedProcess = new BufferedProcess({command, args, options, stdout, stderr, exit})

  stop: ->
    # Kill existing process
    @bufferedProcess.kill() if @bufferedProcess? and @bufferedProcess.process?

  display: (css, line) ->
    # For display
    @output.append("<pre class='line #{css}'>#{line}</pre>")

  check: ->
    # Get current editor
    editor = atom.workspace.getActiveEditor()
    return unless editor?

