     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
   ###


    file.on "fs.save.started", =>
      @lastContentsSentForSave = @getContents()
  
  ###
  FS REQUESTS
  ###
        
  requestSave:->

    contents = @getContents()

    contents = @getContents()
    @emit "ace.requests.saveAs", contents


  fetchContents:(callback)->

    file = @getData()
    unless /localfile:/.test file.path
      file.fetchContents callback
    else
      callback null, file.contents or ""

  ###
  GETTERS
  ###

  getContents:-> @editor.getSession().getValue()

  getTheme:-> @editor.getTheme().replace "ace/theme/", ""

  getSyntax:-> @syntaxMode

  getUseSoftTabs:->
    @appStorage.getValue 'useSoftTabs', @editor.getSession().getUseSoftTabs()

  getShowGutter:->
    @appStorage.getValue 'showGutter', @editor.renderer.getShowGutter()

  getShowPrintMargin:->
    @appStorage.getValue 'showPrintMargin', @editor.getShowPrintMargin()

  getHighlightActiveLine:->
    @appStorage.getValue 'highlightActiveLine', @editor.getHighlightActiveLine()

  getShowInvisibles:->
    @appStorage.getValue 'showInvisibles', @editor.getShowInvisibles()

  getFontSize:->
    @appStorage.getValue 'fontSize', parseInt(@$("#editor#{@getId()}").css("font-size"), 10)

  getTabSize:->
    @appStorage.getValue 'tabSize', @editor.getSession().getTabSize()

  getUseWordWrap:->
    @appStorage.getValue 'useWordWrap', @editor.getSession().getUseWrapMode()

  getSoftWrap:->

    limit = @appStorage.getValue 'softWrap', @editor.getSession().getWrapLimitRange().max
    if limit then limit
    else
      if @getUseWordWrap() then "free" else "off"

  getSettings:->
    theme               : @getTheme()
    syntax              : @getSyntax()
