     fontSize            : @getFontSize()
     tabSize             : @getTabSize()
     softWrap            : @getSoftWrap()
-    
+
   ###
   SETTERS
   ###
 
   setContents:(contents)-> @editor.getSession().setValue contents
 
-  setTheme:(themeName = "merbivore_soft")->
-
+  setTheme:(themeName)->
+    themeName or= @appStorage.getValue 'theme', 'merbivore_soft'
     require ["ace/theme/#{themeName}"], (callback) =>
       @editor.setTheme "ace/theme/#{themeName}"
-  
+      @appStorage.setValue 'theme', themeName, =>
+        callback
+
   setSyntax:(mode)->
-    
+
     file = @getData()
     mode or= file.syntax
-    
+
     unless mode
       ext  = @utils.getFileExtension file.path
       for name, [language, extensions] of __aceSettings.syntaxAssociations
         if ///^(?:#{extensions})$///i.test ext
           mode = name
       mode or= "text"
-    
+
     require ["ace/mode/#{mode}"], ({Mode})=>
       @editor.getSession().setMode new Mode
       @syntaxMode = mode
 
-  setUseSoftTabs:(value)-> @editor.getSession().setUseSoftTabs value
-    
-  setShowGutter:(value)-> @editor.renderer.setShowGutter value
-
-  setShowPrintMargin:(value)-> @editor.setShowPrintMargin value
-    
-  setHighlightActiveLine:(value)-> @editor.setHighlightActiveLine value
+  setUseSoftTabs:(value)->
+
+    @editor.getSession().setUseSoftTabs value
+    @appStorage.setValue 'useSoftTabs', value, =>
+
+  setShowGutter:(value)->
+
+    @editor.renderer.setShowGutter value
+    @appStorage.setValue 'showGutter', value, =>
+
+  setShowPrintMargin:(value)->
+
+    @editor.setShowPrintMargin value
+    @appStorage.setValue 'showPrintMargin', value, =>
+
+  setHighlightActiveLine:(value)->
+
+    @editor.setHighlightActiveLine value
+    @appStorage.setValue 'highlightActiveLine', value, =>
 
   # setHighlightSelectedWord:(value)-> @editor.setHighlightActiveLine value
 
-  setShowInvisibles:(value)-> @editor.setShowInvisibles value
-
-  setFontSize:(value)-> @$("editor#{@getId()}").css fontSize : "#{value}px"
-
-  setTabSize:(value)-> @editor.getSession().setTabSize value
-
-  setUseWordWrap:(value)-> @editor.getSession().setUseWrapMode value
-  
-  softWrapValueMap = ->
-
-    'off'  : [ null, 80 ]
-    '40'   : [ 40,   40 ]
-    '80'   : [ 80,   80 ]
-    'free' : [ null, 80 ]
-  
+  setShowInvisibles:(value)->
+
+    @editor.setShowInvisibles value
+    @appStorage.setValue 'showInvisibles', value, =>
+
+  setFontSize:(value)->
+
+    @$("editor#{@getId()}").css fontSize : "#{value}px"
+    @appStorage.setValue 'fontSize', value, =>
+
+  setTabSize:(value)->
+
+    @editor.getSession().setTabSize value
+    @appStorage.setValue 'tabSize', value, =>
+
+  setUseWordWrap:(value)->
+
+    @editor.getSession().setUseWrapMode value
+    @appStorage.setValue 'useWordWrap', value, =>
+
   setSoftWrap:(value)->
-    
-    [limit, margin] = softWrapValueMap()[value]
+    softWrapValueMap =
+      'off'  : [ null, 80 ]
+      '40'   : [ 40,   40 ]
+      '80'   : [ 80,   80 ]
+      'free' : [ null, 80 ]
+
+    [limit, margin] = softWrapValueMap[value]
 
     @editor.getSession().setWrapLimitRange limit, limit
     @editor.renderer.setPrintMarginColumn margin
     @setUseWordWrap no if value is "off"
 
+    @appStorage.setValue 'softWrap', value, =>
+
   ###
   HELPERS
   ###



class Ace extends KDView
  
  constructor:(options, file)->
    
    super options, file
    @lastSavedContents = ""

  setDomElement:(cssClass)->

    @domElement = $ "<figure class='kdview'><div id='editor#{@getId()}' class='code-wrapper'></div></figure>"

  viewAppended:->
    
    require ['ace/ace'], (ace)=>
      @editor = ace.edit "editor#{@getId()}"
      @prepareEditor()
      @utils.wait => @emit "ace.ready"
      @fetchContents (err, contents)=>
        @setContents contents if contents
        @editor.gotoLine 0


  prepareEditor:->
    
    @setTheme()
    @setSyntax()
    @setShowPrintMargin no
    @setEditorListeners()
  
  setEditorListeners:->
    @editor.getSession().selection.on 'changeCursor', (cursor)=>
      @emit "ace.change.cursor", @editor.getSession().getSelection().getCursor()
    
    @editor.commands.addCommand
        name    : 'save'
        bindKey :


    file.on "fs.save.started", =>
      @lastContentsSentForSave = @getContents()

  ###
  FS REQUESTS
  ###

  requestSave:->

    contents = @getContents()
