 
 
 class Ace extends KDView
-  
+
   constructor:(options, file)->
-    
+
     super options, file
     @lastSavedContents = ""
+    @appStorage = new AppStorage 'Ace', '1.0'
 
   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
-
+
+    @appStorage.fetchStorage (storage)=>
+      log storage
+      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
+    @setUseSoftTabs         @appStorage.getValue 'useSoftTabs',         yes
+    @setShowGutter          @appStorage.getValue 'showGutter',          yes
+    @setShowPrintMargin     @appStorage.getValue 'showPrintMargin',     no
+    @setHighlightActiveLine @appStorage.getValue 'highlightActiveLine', yes
+    @setShowInvisibles      @appStorage.getValue 'showInvisibles',      yes
+    @setFontSize            @appStorage.getValue 'fontSize',            10
+    @setTabSize             @appStorage.getValue 'tabSize',             4
+    @setUseWordWrap         @appStorage.getValue 'useWordWrap',         yes
+    @setSoftWrap            @appStorage.getValue 'softWrap',            'free'
     @setEditorListeners()
-  
+
   setEditorListeners:->
+
     @editor.getSession().selection.on 'changeCursor', (cursor)=>
       @emit "ace.change.cursor", @editor.getSession().getSelection().getCursor()
-    
+
     @editor.commands.addCommand
         name    : 'save'
         bindKey :

    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:-> @editor.getSession().getUseSoftTabs()

  getShowGutter:-> @editor.renderer.getShowGutter()

  getShowPrintMargin:-> @editor.getShowPrintMargin()

  getHighlightActiveLine:-> @editor.getHighlightActiveLine()

  getShowInvisibles:-> @editor.getShowInvisibles()

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

  getTabSize:-> @editor.getSession().getTabSize()

  getUseWordWrap:-> @editor.getSession().getUseWrapMode()

  getSoftWrap:-> 

    limit = @editor.getSession().getWrapLimitRange().max
    if limit then limit
    else
      if @getUseWordWrap() then "free" else "off"
    
  getSettings:->
    theme               : @getTheme()
    syntax              : @getSyntax()

###
  todo:

    - put search replace
    - fix setSoftWrap it goes back to off when you reopen the settings

