     @unsubscribeFromTextEditor(textEditor)
 
   handleDidChangeGrammar: (textEditor,oldGrammarName,newGrammar) ->
-    # do nothing if the grammar was changed due to a level change
     unless newGrammar.name is oldGrammarName
-      # update the grammar subscription
+
       @textEditorSubscrsById[textEditor.id].didChangeGrammarSubscr.dispose()
       @textEditorSubscrsById[textEditor.id].didChangeGrammarSubscr = \
         textEditor.onDidChangeGrammar (grammar) =>
           @handleDidChangeGrammar(textEditor,newGrammar.name,grammar)
-      # update Levels workspace based on the new grammar
+
       language = languageRegistry.getLanguageForGrammar(newGrammar)
       if workspace.isLevelCodeEditor(textEditor)
         levelCodeEditor = workspace.getLevelCodeEditorForTextEditor(textEditor)
         if language?
-          # new grammar is a Levels grammar too - update the level code editor
           levelCodeEditor.setLanguage(language)
         else
-          # new grammar is not a Levels grammar - destroy the level code editor
           workspace.destroyLevelCodeEditor(levelCodeEditor)
+          if textEditor is atom.workspace.getActiveTextEditor()
+            workspace.unsetActiveLevelCodeEditor()
       else
         if language?
-          # new grammar is a Levels grammar - a new level code editor is born!
-          levelCodeEditor = new LevelCodeEditor(textEditor,language)
+          levelCodeEditor = new LevelCodeEditor
+            textEditor: textEditor
+            language: language
           workspace.addLevelCodeEditor(levelCodeEditor)
+          if textEditor is atom.workspace.getActiveTextEditor()
+            workspace.setActiveLevelCodeEditor(levelCodeEditor)
 
   ## Language registry subscriptions -------------------------------------------
 
   subscribeToLanguageRegistry: ->
-    languageRegistry.onDidRemoveLanguages (removedLanguages) =>
-      @handleDidRemoveLanguages(removedLanguages)
+    @languageRegistrySubscrs = new CompositeDisposable
+    @languageRegistrySubscrs.add languageRegistry.onDidRemoveLanguages \
+      (removedLanguages) => @handleDidRemoveLanguages(removedLanguages)
+
+  unsubscribeFromLanguageRegistry: ->
+    @languageRegistrySubscrs.dispose()
 
   handleDidRemoveLanguages: (removedLanguages) ->
+
+  ## Command handlers ----------------------------------------------------------
+
+  doInstallLanguages: (event) ->
+    atom.pickFolder (paths) ->
+      languageInstaller.installLanguages(paths) if paths?
+
+  doUninstallLanguages: (event) ->
+    languageInstaller.uninstallLanguages([])
+
+  doToggleLevelSelect: (event) ->
+    if workspace.isActive()
+      @levelSelectView.toggle()
+    else
+      event.abortKeyBinding()
+
+  doToggleTerminal: (event) ->
+    event.abortKeyBinding()
+
+  doStartExecution: (event) ->
+    event.abortKeyBinding()
+
+  doStopExecution: (event) ->
+    event.abortKeyBinding()
 
   ## Consumed services ---------------------------------------------------------
 


class WorkspaceManager

  ## Initialization and clean-up operations ------------------------------------

  setUpWorkspace: (state) ->
    # add view providers
    @viewProviders = new CompositeDisposable
    @viewProviders.add atom.views.addViewProvider Terminal, (terminal) ->
      new TerminalView(terminal)

    # create view components
    @levelStatusView = new LevelStatusView(workspace)
    @levelSelectorView = new LevelSelectorView(workspace)
    @languageConfigView = new LanguageConfigView(workspace)

  cleanUpWorkspace: ->
    # remove view providers
    @viewProviders.dispose()

    # destroy view components
    @levelStatusView.destroy()
    @levelSelectorView.destroy()
    @languageConfigView.destroy()

    # destroy status bar tiles
    @levelStatusTile.destroy()

  activate: ->
    @subscribeToAtomWorkspace()
    @subscribeToLanguageRegistry()

  deactivate: ->
    @unsubscribeFromAtomWorkspace()

  ## Atom workspace subscriptions ----------------------------------------------



LevelCodeEditor       = require('./level-code-editor')
LevelStatusView       = require('./level-status-view')
LevelSelectView       = require('./level-select-view')
LanguageConfigView    = require('./language-config-view')
TerminalView          = require('./terminal-view')
Terminal              = require('./terminal')
