Surround selected text with a custom tag

Surround selected text with a custom tag

I have a form with a textarea containing some content, such as:

"Some text goes here. Some more text here. 
Link me. Emphasise me.

Heading 

The end of the text."

What I would like to do is select some of text above, such as 'Link me' and surround it in a custom link,
such as '<link id="1">Link Me</link>'. Similarly, I'd like to take 'Emphasise me.' and surround it so that    
becomes '<em>Enphasise me</em>'. 

I originally thought of combing the fieldSelection plugin ( http://laboratorium.0xab.cd/jquery/fieldselection/0.2.3-test/test.html) with wrap ( http://api.jquery.com/wrap/) but soon realised this was over complicating the issue.
I then found Batiste Bieler's excellent lightweight RTE ( http://batiste.dosimple.ch/blog/posts/2007-09-11-1/rich-text-editor-jquery.html) and wondered if it's possible to customise to my needs somehow? The "anchor" button appears to be made using  formatText('CreateLink', p)  and I don't know how to start amending it into what I want. 

Any ideas would be greatly appreciated!