]> matita.cs.unibo.it Git - helm.git/blobdiff - matitaB/matita/matitaweb.js
Matitaweb: TeX-like macro handling.
[helm.git] / matitaB / matita / matitaweb.js
index 1088f51ce9047d3be85394ef4e565a7b8d653dc7..0805e0b9adc297d1e1b5e144388691def7aa4785 100644 (file)
@@ -107,6 +107,38 @@ function suppressdefault(e,flag)
    }
    return !flag;
 }
+
+function restoreSelection() {
+    unlocked.focus();
+    if (savedRange != null) {
+        if (window.getSelection)//non IE and there is already a selection
+        {
+            var s = window.getSelection();
+            if (s.rangeCount > 0) 
+                s.removeAllRanges();
+           range = document.createRange();
+           range.setStart(savedsc,savedso);
+           range.collapse(true);
+            s.addRange(range);
+        }
+        else 
+            if (document.createRange)//non IE and no selection
+            {
+                window.getSelection().addRange(savedRange);
+            }
+            else 
+                if (document.selection)//IE
+                {
+                    savedRange.select();
+                }
+    }
+}
+
+function lookup_tex(texmacro)
+{
+  texmacro = texmacro.substring(1);
+  return unescape(macro2utf8[texmacro]);
+}
  
 function keypress(e)
 {
@@ -118,13 +150,19 @@ function keypress(e)
        i = unlocked.innerHTML.lastIndexOf('\\',j);
                if (i >= 0) {
          match = unlocked.innerHTML.substring(i,j);
-         pre = unlocked.innerHTML.substring(0,i-1);
-         post = unlocked.innerHTML.substring(j+1);
-         if (match == '\\to') {
-             unlocked.innerHTML = pre + "-> " + post;
+         pre = unlocked.innerHTML.substring(0,i);
+         post = unlocked.innerHTML.substring(j);
+         
+         sym = lookup_tex(match);
+         if (typeof sym != "undefined") {
+             unlocked.innerHTML = pre + sym + " " + post;
+             restoreSelection(); 
             return suppressdefault(e,true);
          }
-         else return suppressdefault(e,false);
+         else {
+             restoreSelection(); 
+            return suppressdefault(e,false);
+         }
        }
        else return suppressdefault(e,false);
    } else {
@@ -617,19 +655,24 @@ function removeElement(id) {
   element.parentNode.removeChild(element);
 } 
 
+var savedsc;
+var savedso;
+
 function getCursorPos() {
   var cursorPos;
   if (window.getSelection) {
     var selObj = window.getSelection();
-    var selRange = selObj.getRangeAt(0);
+    savedRange = selObj.getRangeAt(0);
+    savedsc = savedRange.startContainer;
+    savedso = savedRange.startOffset;
     //cursorPos =  findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
     cursorPos =  findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
     /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
     return(cursorPos);
   }
   else if (document.selection) {
-    var range = document.selection.createRange();
-    var bookmark = range.getBookmark();
+    savedRange = document.selection.createRange();
+    var bookmark = savedRange.getBookmark();
     /* FIXME the following works wrong when the document is longer than 65535 chars */
     cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
     return(cursorPos);