       i = document.createElement("i")
       a.title = name
       a.textArea = @textArea
-      jQuery(a).click callback
+      jQuery(a).click ->
+        selection = textArea.selectedText()
+        [prefix, replacement, postfix] = callback(selection)
+        textArea.replaceSelection(prefix + replacement + postfix)
       jQuery(a).addClass className
       jQuery(i).addClass "icon-#{className}"
       jQuery(a).append(i)


    unless this.toolbar
      ta = new Sugar.RichTextArea(this)

      # Setup the buttons
      ta.toolbar

        # Bold
        .addButton "Bold", "bold", ->
          this.textArea.wrapSelection('**', '**')

        # Italic
        .addButton "Italics", "italic", ->
          this.textArea.wrapSelection('_', '_')

        # Link
        .addButton "Link", "link", ->
          selection = this.textArea.selectedText()
          response = prompt('Enter link URL', '')
          this.textArea.replaceSelection(
            "[" + (selection || "Link text") + "](" + (response || 'http://link_url/').replace(/^(?!(f|ht)tps?:\/\/)/, 'http://') + ")"
          )

        # Image tag
        .addButton "Image", "picture", ->
          selection = this.textArea.selectedText()
          if selection == ''
            response = prompt('Enter image URL', '')
            unless response
              return
            this.textArea.replaceSelection("![](" + response + ")")
          else
            this.textArea.replaceSelection("![](" + selection + ")")

        # MP3 Player
        .addButton "MP3", "music", ->
          selection = this.textArea.selectedText()
          response = prompt('Enter MP3 URL', '')
          unless selection
            selection = prompt('Enter track title', '')
          this.textArea.replaceSelection(
            '<a href="' + (response || 'http://link_url/').replace(/^(?!(f|ht)tps?:\/\/)/, 'http://') + '" class="mp3player">' +
            (selection || "Link text") + '</a>')

        # Block Quote
        .addButton "Block Quote", "quote-left", ->
          selection = this.textArea.selectedText()
          quotedText = ("> " + line for line in selection.split("\n")).join("\n")
          this.textArea.replaceSelection(quotedText)

        # Escape HTML
        .addButton "Code", "code", ->
          selection = this.textArea.selectedText()
          response = prompt('Enter language (leave blank for no syntax highlighting)', '')
          if response
            this.textArea.replaceSelection("```" + response + "\n" + selection + "\n```")
          else
            this.textArea.replaceSelection("```\n" + selection + "\n```")

        # Spoiler
        .addButton "Spoiler", "warning-sign", ->
          this.textArea.wrapSelection('<div class="spoiler">', '</div>')

  @toolbar.create()
  this

markdownDecorator =
  bold: (str)           -> ["**", str, "**"]
  emphasis: (str)       -> ["_", str, "_"]
  link: (url, name)     -> ["[", name, "](#{url})"]
  image: (url)          -> ["![](", url, ")"]
  mp3: (url, name)      -> ["<a href=\"#{url}\" class=\"mp3player\">", name, "</a>"]
  blockquote: (str)     -> ["", ("> " + line for line in str.split("\n")).join("\n"), ""]
  code: (str, language) -> ["```#{language}\n", str, "\n```"]
  spoiler: (str)        -> ["<div class=\"spoiler\">", str, "</div>"]

htmlDecorator =
  bold: (str)           -> ["<b>", str, "</b>"]
  emphasis: (str)       -> ["<i>", str, "</i>"]
  link: (url, name)     -> ["<a href=\"#{url}\">", name, "</a>"]
  image: (url)          -> ["<img src=\"", url, "\">"]
  mp3: (url, name)      -> ["<a href=\"#{url}\" class=\"mp3player\">", name, "</a>"]
  blockquote: (str)     -> ["<blockquote>", str, "</blockquote>"]
  code: (str, language) -> ["<pre><code class=\"#{language}\">", str, "</code></pre>"]
  spoiler: (str)        -> ["<div class=\"spoiler\">", str, "</div>"]

$(Sugar).bind 'ready modified', ->

