--- webfm.js.orig	2008-04-08 08:28:14.000000000 +0200
+++ webfm.js	2008-09-18 09:58:32.000000000 +0200
@@ -99,6 +99,7 @@
 Webfm.menu_msg["denum"] = "Remove file from database";
 Webfm.menu_msg["perm"] = "File permissions";
 Webfm.menu_msg["clip"] = "put link in clipboard";
+Webfm.menu_msg["paste"] = "Paste (relative) file href";
 //Do not translate any code below this line
 
 Webfm.current = null;
@@ -156,6 +157,8 @@
    eavi:"avi", empg:"avi", empeg:"avi", ewma:"mp3", ewmv:"avi"
 };
 
+Webfm.lastarea = null;
+
 /*
 ** Functions
 */
@@ -164,6 +167,13 @@
 }
 
 /**
+ * Remember last used textarea for inserting file hrefs.
+ */
+function webfmSetlast(){
+Webfm.lastarea = this;
+}
+
+/**
  * Determine browser
  */
 Webfm.browserDetect = function() {
@@ -279,6 +289,26 @@
  * ..or at node-edit 'webfm-inline' id anchor
  */
 function webfmLayout() {
+  // Initialize textarea for pasting file hrefs with the most often used
+  // in case we try to paste before a textarea or -field was focussed.
+  Webfm.lastarea = document.getElementById('edit-body');
+  if (!Webfm.lastarea) {
+  Webfm.lastarea = document.getElementById('edit-comment');
+  }
+  // Tell textareas and text fields to remember which one was last focussed.
+  textareas = document.getElementsByTagName('textarea');
+  i = textareas.length;
+  while( i-- ) {
+    textareas[i].onfocus = webfmSetlast;
+  }
+  textfields = document.getElementsByTagName('input');
+  i = textfields.length;
+  while( i-- ) {
+    if (textfields[i].type=='text') {
+      textfields[i].onfocus = webfmSetlast;
+    }
+  }
+
   var layoutDiv = '';
   layoutDiv = Webfm.$('webfm');
 
@@ -296,9 +326,11 @@
 
       //Add attach-list menu and attach option to listing menu
       try {
+        Webfm.menuHT.put('file', new Webfm.menuElement(Webfm.menu_msg["paste"], Webfm.menuPasteHref, ''));
         Webfm.menuHT.put('file', new Webfm.menuElement(Webfm.menu_msg["att"], Webfm.menuAttach, Webfm.menuFidVal));
         Webfm.menuHT.put('det', new Webfm.menuElement(Webfm.menu_msg["meta"], Webfm.menuGetMeta, ''));
         Webfm.menuHT.put('det', new Webfm.menuElement(Webfm.menu_msg["det"], Webfm.menuDetach, ''));
+        Webfm.menuHT.put('det', new Webfm.menuElement(Webfm.menu_msg["paste"], Webfm.menuPasteHref, ''));
       } catch(err) {
         alert("Menu Create err\n" + err);
       }
@@ -1905,10 +1937,39 @@
   }
 }
 
-Webfm.menuPutLinkInClipboard = function(obj) {
-  var url = getBaseUrl();
+Webfm.generateFileHref  = function(obj, url) {
+  if (url === undefined) {
+    url = getBaseUrl();
+  }
   var string = "<a href=\"" + url + "\/webfm_send\/" + obj.element.id.substring(3) + "\">" + obj.element.title.substring(obj.element.title.lastIndexOf("/")+1) + "</a>";
-  Webfm.copyToClipboard(string);
+  return string;
+}
+
+Webfm.menuPutLinkInClipboard = function(obj) {
+  Webfm.copyToClipboard(Webfm.generateFileHref(obj));
+}
+
+Webfm.menuPasteHref = function(obj) {
+  var fileHref = Webfm.generateFileHref(obj, '');
+  // should we rely on a fixed string 'edit-body'? Can we fetch this id somehow via drupal?
+  //var myField = document.getElementById('edit-body');
+  var myField = Webfm.lastarea;
+
+   //IE support 
+  if (document.selection) { 
+    myField.focus(); 
+    sel = document.selection.createRange(); 
+    sel.text = fileHref; 
+  } 
+  //other browsers
+  else if (myField.selectionStart || myField.selectionStart == '0') { 
+    var startPos = myField.selectionStart; 
+    var endPos = myField.selectionEnd; 
+    myField.value = myField.value.substring(0, startPos)+ fileHref + myField.value.substring(endPos, myField.value.length); 
+  } else { 
+    myField.value += fileHref; 
+  } 
+  
 }
 
 Webfm.menuAdmin = function() {
@@ -3880,4 +3941,4 @@
  */
 Drupal.mousePosition = function(e) {
   return { x: e.clientX + document.documentElement.scrollLeft, y: e.clientY + document.documentElement.scrollTop };
-};
\ No newline at end of file
+};
