 
   wrap: (left, right) ->
     @getDetails()
-    @replace(left + @text + right, true)
+    @deselectNewLines()
+    @replace(left + @text + right, @text != '')
+    @select(@start + left.length, @start + left.length) if @text == ''
+
+
+  wrapLine: (left, right, selectAfter = true, reselect = false) ->
+    @getDetails()
+    savedSelection = @serialize()
+    text = @element.val()
+    start = text.lastIndexOf('\n', @start)
+    end = text.indexOf('\n', @end)
+    end = text.length if end < start
+    start = text.lastIndexOf('\n', @start - 1) if text[start] == '\n'
+    @select(start + 1, end)
+    @replace(left + @text + right, selectAfter)
+    @select(savedSelection.start + left.length, savedSelection.end + left.length) if reselect
+
+
+  unWrapLine: (left, right, selectAfter = true, reselect = false) ->
+    @getDetails()
+    savedSelection = @serialize()
+    text = @element.val()
+    start = text.lastIndexOf('\n', @start)
+    end = text.indexOf('\n', @end)
+    end = text.length if end < start
+    start = text.lastIndexOf('\n', @start - 1) if text[start] == '\n'
+    @select(start + 1, end)
+    window.something = @text
+    leftRegExp = new RegExp("^#{left.regExpEscape()}")
+    rightRegExp = new RegExp("#{right.regExpEscape()}$")
+    changed = @replace(@text.replace(leftRegExp, '').replace(rightRegExp, ''), selectAfter)
+    @select(savedSelection.start - left.length, savedSelection.end - left.length) if reselect && changed
+
+
+  addList: (content) ->
+    text = @element.val()
+    start = text.lastIndexOf('\n', @start)
+    end = text.indexOf('\n', @end)
+    end = text.length if end < start
+    start = text.lastIndexOf('\n', @start - 1) if text[start] == '\n'
+    @select(start + 1, end)
+    lines = @text.split('\n')
+    @replace("#{content} " + lines.join("\n#{content} "), true)
+
+
+  deselectNewLines: ->
+    text = @text
+    length = text.replace(/\n+$/g, '').length
+    @select(@start, @start + length)
 
 
   placeMarker: ->


    handler.call(@, @selection(), options) if handler = Mercury.Regions.Markupable.actions[action]
    @resize()


  pushHistory: (keyCode) ->

      $(event.target).closest('a').attr('target', '_top') if @previewing


  html: (value = null, filterSnippets = true) ->
    if value != null
      if $.type(value) == 'string'
        @textarea.val(value)
      else
        @textarea.val(value.html)
        @selection().select(value.selection.start, value.selection.end)
    else
      return @textarea.val()


  togglePreview: ->
