]> matita.cs.unibo.it Git - helm.git/blobdiff - matitaB/matita/matitaweb.js
update in ground_2 static_2 basic_2
[helm.git] / matitaB / matita / matitaweb.js
index 0c661623bc148fe1f8614b3bb8c63b0401ae6059..cd25cd15a68b2e349647378de40deb9551c338df 100644 (file)
@@ -11,17 +11,55 @@ var advanceButton;
 var retractButton;
 var cursorButton;
 var bottomButton;
+var dialogBox;
+var dialogTitle;
+var dialogContent;
 var metasenv = "";
+var lockedbackup = "";
+var matita;
+
+function text_of_html(h)
+{
+  if(document.all) {
+     return h.innerText;
+  } else {
+     return h.textContent;
+  }
+}
+
+function unescape_html(s)
+{
+  u = document.getElementById("unescape");
+  u.innerHTML = s;
+  return text_of_html(u)
+}
+
+function filterByClass (elements,cname){
+  var itemsfound = new Array;
+  for(var i=0;i<elements.length;i++){
+    if(elements[i].className == cname){
+      itemsfound.push(elements[i]);
+    }
+  }
+  return itemsfound;
+}
 
 function initialize()
 {
   if (readCookie("session") == null) {
     window.location = "/login.html"
   } else {
+    body = document.body;
+    titlebar = document.getElementById("titlebar");
+    matitaTitle = document.getElementById("matitaTitle");
+    apparea = document.getElementById("matitaapparea");
     locked = document.getElementById("locked");
     unlocked = document.getElementById("unlocked");
+    toparea = document.getElementById("toparea");
     workarea = document.getElementById("workarea");
     scriptcell = document.getElementById("scriptcell");
+    sidearea = document.getElementById("sidearea");
+    disambcell = document.getElementById("disambcell");
     goalcell = document.getElementById("goalcell");
     goals = document.getElementById("goals");
     goalview = document.getElementById("goalview");
@@ -31,16 +69,226 @@ function initialize()
     retractButton = document.getElementById("retract");
     cursorButton = document.getElementById("cursor");
     bottomButton = document.getElementById("bottom");
-  
+    dialogBox = document.getElementById("dialogBox");
+    uploadBox = document.getElementById("uploadBox");
+    dialogTitle = document.getElementById("dialogTitle");
+    dialogContent = document.getElementById("dialogContent");
+
+    matita = new Object();
+    matita.disambMode = matita.proofMode = false;
+
     // hide sequent view at start
-    hideSequent();
+    initializeLayout();
+    updateSide();
+
+    changeFile("test.ma");
+
+    // initialize keyboard events in the unlocked script
+    init_keyboard(unlocked);
+
+    init_autotraces();
+
   }
 }
 
+function init_autotraces() {
+    $("#unlocked .autotactic").tooltip({ 
+      delay: 0, 
+      showURL: false, 
+      bodyHandler: function() { 
+        return (trace_of($(this)[0])); 
+      }
+    });
+    $("#locked .autotactic").tooltip({ 
+      delay: 0, 
+      showURL: false, 
+      bodyHandler: function() { 
+        return (trace_of($(this)[0]));
+      }
+    });
+}
+
+function trace_of(node) {
+  return text_of_html(filterByClass(node.childNodes,"autotrace")[0]);
+}
+
+function changeFile(name) {
+    current_fname = name;
+    matitaTitle.innerHTML = "Matita - cic:/matita/" + name;
+}
+
+function init_keyboard(target)
+{
+    if (target.addEventListener)
+    {
+//       target.addEventListener("keydown",keydown,false);
+       target.addEventListener("keypress",keypress,false);
+//       target.addEventListener("keyup",keyup,false);
+//       target.addEventListener("textInput",textinput,false);
+    }
+    else if (target.attachEvent)
+    {
+//       target.attachEvent("onkeydown", keydown);
+       target.attachEvent("onkeypress", keypress);
+//       target.attachEvent("onkeyup", keyup);
+//       target.attachEvent("ontextInput", textinput);
+    }
+    else
+    {
+//       target.onkeydown= keydown;
+       target.onkeypress= keypress;
+//       target.onkeyup= keyup;
+//       target.ontextinput= textinput;   // probably doesn't work
+    }
+}
+
+function keyval(n)
+{
+    if (n == null) return 'undefined';
+    var s= '' + n;
+    if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
+    while (s.length < 9) s+= ' ';
+    return s;
+}
+function string_of_key(n)
+{
+    if (n == null) return 'undefined';
+    return String.fromCharCode(n);
+}
+
+function pressmesg(w,e)
+{
+   debug(w + '  keyCode=' + keyval(e.keyCode) +
+                 ' which=' + keyval(e.which) +
+                 ' charCode=' + keyval(e.charCode) +
+                '\n          shiftKey='+e.shiftKey
+             + ' ctrlKey='+e.ctrlKey
+             + ' altKey='+e.altKey
+             + ' metaKey='+e.metaKey);
+}
+function suppressdefault(e,flag)
+{
+   if (flag)
+   {
+       if (e.preventDefault) e.preventDefault();
+       if (e.stopPropagation) e.stopPropagation();
+   }
+   return !flag;
+}
+
+function restoreSelection(r) {
+    unlocked.focus();
+    if (r != null) {
+        if (window.getSelection)//non IE and there is already a selection
+        {
+            var s = window.getSelection();
+            if (s.rangeCount > 0) 
+                s.removeAllRanges();
+            s.addRange(r);
+        }
+        else 
+            if (document.createRange)//non IE and no selection
+            {
+                window.getSelection().addRange(r);
+            }
+            else 
+                if (document.selection)//IE
+                {
+                    r.select();
+                }
+    }
+}
+
+function lookup_tex(texmacro)
+{
+  texmacro = texmacro.substring(1);
+  return unescape(macro2utf8[texmacro]);
+}
+
+function strip_tags(tagname,classname) 
+{
+    var tags = unlocked.getElementsByTagName(tagname);
+    if (is_defined(classname)) {
+       tags = filterByClass(tags,classname);
+    }
+    for (i = 0; i < tags.length; i++) {
+        var children = tags[i].childNodes;
+        for (j = 0; j < children.length; j++) {
+            tags[i].parentNode.insertBefore(children[j],tags[i]);
+       }
+    }
+    for (var i = 0;tags.length > i;i++) {
+      tags[0].parentNode.removeChild(tags[0]);
+    }
+}
+
+function strip_interpr() {
+       strip_tags("A");
+       alert("strip_interpr ended");
+}
+function keypress(e)
+{
+   if (!e) e= event;
+   pressmesg('keypress',e);
+   var s = string_of_key(e.charCode);
+   strip_tags("span","error");
+   if (s == " ") {
+       j = getCursorPos();
+       i = unlocked.innerHTML.html_to_matita().lastIndexOf('\\',j);
+               if (i >= 0) {
+         match = unlocked.innerHTML.html_to_matita().substring(i,j);
+         sym = unescape_html(lookup_tex(match));
+         if (sym != "undefined") {
+             if (window.getSelection) { // non IE
+                savedRange.setStart(savedsc,savedso - (j-i));
+                savedRange.deleteContents();
+                savedRange.insertNode(document.createTextNode(sym));
+                savedsc.parentNode.normalize();
+                if (savedRange.collapsed) { // Mozilla
+                  savedRange.setEnd(savedsc,savedRange.endOffset + sym.length);
+                }
+                savedRange.collapse(false);
+             } else {
+                savedRange.moveStart(i-j);
+                savedRange.text(sym);
+                savedRange.collapse(false);
+             }
+             restoreSelection(savedRange); 
+                    return suppressdefault(e,true);
+         }
+         else {
+             // restoreSelection(0); 
+            return suppressdefault(e,false);
+         }
+       }
+       else return suppressdefault(e,false);
+   } else {
+       return suppressdefault(e,false);
+   }
+}
+var logtxt = "";
+
 function debug(txt)
 {
         // internet explorer (v.9) doesn't work with innerHTML
-        logarea.innerText = txt + "\n" + logarea.innerText;
+       // but google chrome's innerText is, in a sense, "write only"
+       // what should we do?
+        // logarea.innerText = txt + "\n" + logarea.innerText;
+        logtxt = /* logtxt + "\n" +*/ txt;
+}
+
+function showLog() {
+  logWin = window.open( "", "Matita Log",
+     "width=600,height=450,status,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
+  logWin.document.write('<html><head><title>Matita Log' + '</title></head>');  
+  logWin.document.write('<body><textarea style="width:100%;height:100%;">' +
+    logtxt + '</textarea></body></html>');
+  logWin.document.close(); 
 }
 
 function listhd(l)
@@ -69,6 +317,9 @@ function listnil()
        return ("");
 }
 
+function list_append(l1,l2)
+{ return (l1 + l2) }
+
 function is_nil(l)
 {
        return (l == "");
@@ -199,6 +450,8 @@ function switch_goal(meta)
   }
 }
 
+// the following is used to avoid escaping unicode, which results in 
+// the server being unable to unescape the string
 String.prototype.sescape = function() {
        var patt1 = /%/gi;
        var patt2 = /=/gi;
@@ -212,32 +465,36 @@ String.prototype.sescape = function() {
        return (result);
 }
 
-String.prototype.unescapeHTML = function()
+String.prototype.html_to_matita = function()
 {
        var patt1 = /<br(\/|)>/gi;
-       var patt2 = /&lt;/gi;
-       var patt3 = /&gt;/gi;
+       var patt2 = /</gi
+       var patt3 = />/gi
+       var patt4 = /&lt;/gi;
+       var patt5 = /&gt;/gi;
+       var patt6 = /&nbsp;/gi;
        var result = this;
        result = result.replace(patt1,"\n");
-       result = result.replace(patt2,"<");
-       result = result.replace(patt3,">");
+       result = result.replace(patt2,"\005");
+       result = result.replace(patt3,"\006");
+       result = result.replace(patt4,"<");
+       result = result.replace(patt5,">");
+       result = result.replace(patt6," ");
        return (unescape(result));
 }
 
-function pause()
-{
-       advanceButton.disabled = true;
-        retractButton.disabled = true;
-        cursorButton.disabled = true;
-        bottomButton.disabled = true;
-}
-
-function resume()
+String.prototype.matita_to_html = function()
 {
-       advanceButton.disabled = false;
-        retractButton.disabled = false;
-        cursorButton.disabled = false;
-        bottomButton.disabled = false;
+       var patt1 = /</gi
+       var patt2 = />/gi
+       var patt3 = /\005/gi;
+       var patt4 = /\006/gi;
+       var result = this;
+       result = result.replace(patt1,"&lt;");
+       result = result.replace(patt2,"&gt;");
+       result = result.replace(patt3,"<");
+       result = result.replace(patt4,">");
+       return (unescape(result));
 }
 
 function is_defined(x)
@@ -307,32 +564,314 @@ function callServer(servicename,processResponse,reqbody)
   
 }
 
+function advOneStep(xml) {
+        var parsed = xml.getElementsByTagName("parsed")[0];
+       var ambiguity = xml.getElementsByTagName("ambiguity")[0];
+       var disamberr = xml.getElementsByTagName("disamberror")[0];
+        if (is_defined(parsed)) {
+       // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
+           var len = parseInt(parsed.getAttribute("length"));
+            // len0 = unlocked.innerHTML.length;
+           var unescaped = unlocked.innerHTML.html_to_matita();
+           var parsedtxt = parsed.childNodes[0].wholeText;
+           //parsedtxt = unescaped.substr(0,len); 
+           var unparsedtxt = unescaped.substr(len);
+           lockedbackup += parsedtxt;
+           locked.innerHTML = lockedbackup;
+           unlocked.innerHTML = unparsedtxt.matita_to_html();
+           // len1 = unlocked.innerHTML.length;
+           // len2 = len0 - len1;
+           var len2 = parsedtxt.length;
+           metasenv = xml.getElementsByTagName("meta");
+           statements = listcons(len2,statements);
+           unlocked.scrollIntoView(true);
+           return len;
+       }
+       else if (is_defined(ambiguity)) {
+           var start = parseInt(ambiguity.getAttribute("start"));
+           var stop = parseInt(ambiguity.getAttribute("stop"));
+           var choices = xml.getElementsByTagName("choice");
+
+           matita.ambiguityStart = start;
+           matita.ambiguityStop = stop;
+           matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
+           matita.interpretations = [];
+       
+           var unlockedtxt = unlocked.innerHTML.html_to_matita();
+           var pre = unlockedtxt.substring(0,start).matita_to_html();
+           var mid = unlockedtxt.substring(start,stop).matita_to_html();
+           var post = unlockedtxt.substring(stop).matita_to_html();
+           unlocked.innerHTML = pre + 
+                   "<span class=\"error\" title=\"disambiguation error\">" +
+                   mid + "</span>" + post;
+
+           var title = "<H3>Ambiguous input</H3>";
+           disambcell.innerHTML = title;
+           for (i = 0;i < choices.length;i++) {
+               matita.interpretations[i] = new Object();
+
+               var href = choices[i].getAttribute("href");
+               var title = choices[i].getAttribute("title");
+               var desc = choices[i].childNodes[0].nodeValue;
+
+               matita.interpretations[i].href = href;
+               matita.interpretations[i].title = title;
+               matita.interpretations[i].desc = desc;
+               
+               var choice = document.createElement("input");
+               choice.setAttribute("type","radio");
+               choice.setAttribute("name","interpr");
+               choice.setAttribute("href",href);
+               choice.setAttribute("title",title);
+               if (i == 0) choice.setAttribute("checked","");
+               
+               disambcell.appendChild(choice);
+               disambcell.appendChild(document.createTextNode(desc));
+               disambcell.appendChild(document.createElement("br"));
+           }
+
+           var okbutton = document.createElement("input");
+           okbutton.setAttribute("type","button");
+           okbutton.setAttribute("value","OK");
+           okbutton.setAttribute("onclick","do_disambiguate()");
+           var cancelbutton = document.createElement("input");
+           cancelbutton.setAttribute("type","button");
+           cancelbutton.setAttribute("value","Cancel");
+           cancelbutton.setAttribute("onclick","cancel_disambiguate()");
+
+           disambcell.appendChild(okbutton);
+           disambcell.appendChild(cancelbutton);
+
+           matita.disambMode = true;
+           updateSide();
+           throw "Wait";
+       }
+       else if (is_defined(disamberr)) {
+            // must be fixed in a daemon: it makes sense to return a 
+            // disambiguation error with no choices
+            if (disamberr.childNodes.length > 0) {
+              set_cps(disamberr.childNodes);
+              matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
+              matita.disambMode = true;
+              next_cp(0);
+            }
+           throw "Wait";
+       }
+        else {
+            var error = xml.getElementsByTagName("error")[0]; 
+           unlocked.innerHTML = error.childNodes[0].wholeText;
+           // debug(xml.childNodes[0].nodeValue);
+           throw "Stop";
+       }
+
+}
+
 function advanceForm1()
 {
        processor = function(xml) {
+           try {
                if (is_defined(xml)) {
-                       // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
-                       len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
-                       len0 = unlocked.innerHTML.length;
-                       unescaped = unlocked.innerHTML.unescapeHTML();
-                       parsedtxt = unescaped.substr(0,len); 
-                       unparsedtxt = unescaped.substr(len);
-                       locked.innerHTML = locked.innerHTML + parsedtxt;
-                       unlocked.innerHTML = unparsedtxt;
-                       len1 = unlocked.innerHTML.length;
-                       len2 = len0 - len1;
-                       metasenv = xml.getElementsByTagName("meta");
-                       populate_goalarray(metasenv);
-                       statements = listcons(len2,statements);
-                       unlocked.scrollIntoView(true);
+                   advOneStep(xml);
+                    populate_goalarray(metasenv);
+                   init_autotraces();
                } else {
                        debug("advance failed");
                }
-               resume();
+           } catch (e) { 
+                   if (e == "Stop") 
+                           resume();
+                   else 
+                           pause();
+           };
        };
        pause();
-        callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
+        callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
+  
+}
+
+// get or set <choicepoint>'s (in case of disamb error)
+function get_cps() {
+       return matita.choicepoints
+}
+
+function set_cps(cps) {
+       matita.choicepoints = cps;
+}
+
+// get radio buttons for <choice>'s in a given cp
+function get_choice_opts(i) {
+   var res = [];
+   var choices = get_cps()[i].childNodes;
+   for (var j = 0;j < choices.length;j++) {
+      var href = choices[j].getAttribute("href");
+      var title = choices[j].getAttribute("title");
+      var desc;
+      if (is_defined(title) && title != null) {
+          desc = title;
+      } else if (is_defined(href) && href != null) {
+          desc = href;
+      } else {
+          desc = null;
+      }
+  
+      res[j] = document.createElement("input");
+      res[j].setAttribute("type","radio");
+      res[j].setAttribute("name","choice");
+      res[j].setAttribute("choicepointno",i);
+      res[j].setAttribute("choiceno",j);
+      res[j].setAttribute("href",href);
+      res[j].setAttribute("title",title);
+      if (desc != null) res[j].setAttribute("desc",desc);
+      
+      if (j == 0) res[j].setAttribute("checked","");
+  }
+  return res;
+}
+
+// get radio buttons for <failure>'s in a given choice
+function get_failure_opts(i,j) {
+   var res = [];
+   var failures = get_cps()[i].childNodes[j].childNodes;
+   for (var k = 0;k < failures.length;k++) {
+      var start = failures[k].getAttribute("start");
+      var stop = failures[k].getAttribute("stop");
+      var title = failures[k].getAttribute("title");
   
+      res[k] = document.createElement("input");
+      res[k].setAttribute("type","radio");
+      res[k].setAttribute("name","failure");
+      res[k].setAttribute("choicepointno",i);
+      res[k].setAttribute("choiceno",j);
+      res[k].setAttribute("failureno",k);
+      res[k].setAttribute("start",start);
+      res[k].setAttribute("stop",stop);
+      res[k].setAttribute("title",title);
+      
+      if (k == 0) res[k].setAttribute("checked","");
+  }
+  return res;
+}
+
+function next_cp(curcp) {
+       var cp = get_cps()[curcp];
+       var start = parseInt(cp.getAttribute("start"));
+       var stop = parseInt(cp.getAttribute("stop"));
+
+       matita.errorStart = start;
+       matita.errorStop = stop;
+       // matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
+       
+       var unlockedtxt = matita.unlockedbackup;
+       var pre = unlockedtxt.substring(0,start).matita_to_html();
+       var mid = unlockedtxt.substring(start,stop).matita_to_html();
+       var post = unlockedtxt.substring(stop).matita_to_html();
+       unlocked.innerHTML = pre + 
+               "<span class=\"error\" title=\"error location\">" +
+               mid + "</span>" + post;
+
+       var title = "<H3>Error diagnostics</H3>";
+       disambcell.innerHTML = title;
+       var choices = get_choice_opts(curcp);
+       for (var i = 0;i < choices.length;i++) {
+           disambcell.appendChild(choices[i]);
+           var desc = choices[i].getAttribute("desc");
+            if (!is_defined(desc) || desc == null) {
+                   desc = "Interpretation " + i;
+            }
+           disambcell.appendChild(document.createTextNode(desc));
+           disambcell.appendChild(document.createElement("br"));
+       }
+       
+       // update index of the next choicepoint
+       new_curcp = (curcp + 1) % get_cps().length;
+
+       var okbutton = document.createElement("input");
+       okbutton.setAttribute("type","button");
+       okbutton.setAttribute("value","OK");
+       okbutton.setAttribute("onclick","show_failures()");
+       var cancelbutton = document.createElement("input");
+       cancelbutton.setAttribute("type","button");
+       cancelbutton.setAttribute("value","Close");
+       cancelbutton.setAttribute("onclick","cancel_disambiguate()");
+       var tryagainbutton = document.createElement("input");
+       tryagainbutton.setAttribute("type","button");
+       if (new_curcp > 0) {
+            tryagainbutton.setAttribute("value","None of the above");
+       } else {
+           tryagainbutton.setAttribute("value","Restart");
+       }
+       tryagainbutton.setAttribute("onclick","next_cp(" + new_curcp + ")");
+
+       disambcell.appendChild(okbutton);
+       disambcell.appendChild(tryagainbutton);
+       disambcell.appendChild(cancelbutton);
+
+       //disable_toparea();
+
+       //matita.disambMode = true;
+       updateSide();
+       
+}
+
+function show_failures() {
+
+       var choice = document.getElementsByName("choice")[get_checked_index("choice")];
+       var cpno = parseInt(choice.getAttribute("choicepointno"));
+       var choiceno = parseInt(choice.getAttribute("choiceno"));
+       var choicedesc = choice.getAttribute("desc");
+
+       var title = "<H3>Error diagnostics</H3>";
+       var subtitle;
+        if (is_defined(choicedesc) && choicedesc != null) {
+               subtitle  = "<p>Errors at node " + cpno + " = " + choicedesc + "</p>";
+        } else {
+               subtitle = "<p>Global errors:</p>";
+       }
+
+       disambcell.innerHTML = title + subtitle;
+       var failures = get_failure_opts(cpno,choiceno);
+       for (var i = 0;i < failures.length;i++) {
+           disambcell.appendChild(failures[i]);
+           disambcell.appendChild(document.createTextNode(failures[i].getAttribute("title")));
+           disambcell.appendChild(document.createElement("br"));
+       }
+       
+       var okbutton = document.createElement("input");
+       okbutton.setAttribute("type","button");
+       okbutton.setAttribute("value","Show error loc");
+       okbutton.setAttribute("onclick","show_err()");
+       var cancelbutton = document.createElement("input");
+       cancelbutton.setAttribute("type","button");
+       cancelbutton.setAttribute("value","Close");
+       cancelbutton.setAttribute("onclick","cancel_disambiguate()");
+       var backbutton = document.createElement("input");
+       backbutton.setAttribute("type","button");
+        backbutton.setAttribute("value","<< Go back");
+       backbutton.setAttribute("onclick","next_cp(" + cpno + ")");
+
+       disambcell.appendChild(backbutton);
+       disambcell.appendChild(okbutton);
+       disambcell.appendChild(cancelbutton);
+       
+}
+
+function show_err() {
+       var radios = document.getElementsByName("failure");
+       for (i = 0; i < radios.length; i++) {
+           if (radios[i].checked) {
+               var start = radios[i].getAttribute("start");
+               var stop = radios[i].getAttribute("stop");
+               var title = radios[i].getAttribute("title");
+               var unlockedtxt = matita.unlockedbackup;
+               var pre = unlockedtxt.substring(0,start).matita_to_html();
+               var mid = unlockedtxt.substring(start,stop).matita_to_html();
+               var post = unlockedtxt.substring(stop).matita_to_html();
+               unlocked.innerHTML = pre + 
+                       "<span class=\"error\" title=\"Disambiguation failure\">" +
+                       mid + "</span>" + post;
+               break;
+           }
+       }
 }
 
 function gotoBottom()
@@ -340,30 +879,147 @@ function gotoBottom()
        processor = function(xml) {
                if (is_defined(xml)) {
                        // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
-                       len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
-                       len0 = unlocked.innerHTML.length;
-                       unescaped = unlocked.innerHTML.unescapeHTML();
-                       parsedtxt = unescaped.substr(0,len); 
-                       unparsedtxt = unescaped.substr(len);
-                       locked.innerHTML = locked.innerHTML + parsedtxt;
-                       unlocked.innerHTML = unparsedtxt;
-                       len1 = unlocked.innerHTML.length;
-                       len2 = len0 - len1;
-                       metasenv = xml.getElementsByTagName("meta");
+                       var parsed = xml.getElementsByTagName("parsed");
+                       var localized = xml.getElementsByTagName("localized")[0];
+                       var ambiguity = xml.getElementsByTagName("ambiguity")[0];
+                       var generic_err = xml.getElementsByTagName("error")[0];
+                       var disamberr = xml.getElementsByTagName("disamberror")[0];
+                       for (var i = 0;i < parsed.length; i++) {
+                         var len = parsed[i].getAttribute("length");
+                         // len0 = unlocked.innerHTML.length;
+                         var unescaped = unlocked.innerHTML.html_to_matita();
+                         // the browser may decide to split textnodes: use wholeText!
+                         var parsedtxt = parsed[i].childNodes[0].wholeText;
+                         //parsedtxt = unescaped.substr(0,len); 
+                         var unparsedtxt = unescaped.substr(len);
+                         lockedbackup += parsedtxt;
+                         locked.innerHTML = lockedbackup; //.matita_to_html();
+                         unlocked.innerHTML = unparsedtxt.matita_to_html();
+                         // len1 = unlocked.innerHTML.length;
+                         var len2 = parsedtxt.length;
+                         statements = listcons(len2,statements);
+                       }
+                       unlocked.scrollIntoView(true);
+                       metasenv = xml.getElementsByTagName("meta");
+                       init_autotraces();
                        populate_goalarray(metasenv);
-                       statements = listcons(len2,statements);
-                       unlocked.scrollIntoView(true);
+
+                       if (is_defined(ambiguity)) {
+                           var start = parseInt(ambiguity.getAttribute("start"));
+                           var stop = parseInt(ambiguity.getAttribute("stop"));
+                           var choices = xml.getElementsByTagName("choice");
+
+                           matita.ambiguityStart = start;
+                           matita.ambiguityStop = stop;
+                           matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
+                           matita.interpretations = [];
+                       
+                           var unlockedtxt = unlocked.innerHTML.html_to_matita();
+                           var pre = unlockedtxt.substring(0,start).matita_to_html();
+                           var mid = unlockedtxt.substring(start,stop).matita_to_html();
+                           var post = unlockedtxt.substring(stop).matita_to_html();
+                           unlocked.innerHTML = pre + 
+                                   "<span class=\"error\" title=\"disambiguation error\">" +
+                                   mid + "</span>" + post;
+
+                           var title = "<H3>Ambiguous input</H3>";
+                           disambcell.innerHTML = title;
+                           for (i = 0;i < choices.length;i++) {
+                               matita.interpretations[i] = new Object();
+
+                               var href = choices[i].getAttribute("href");
+                               var title = choices[i].getAttribute("title");
+                               var desc = choices[i].childNodes[0].nodeValue;
+
+                               matita.interpretations[i].href = href;
+                               matita.interpretations[i].title = title;
+                               matita.interpretations[i].desc = desc;
+                               
+                               var choice = document.createElement("input");
+                               choice.setAttribute("type","radio");
+                               choice.setAttribute("name","interpr");
+                               choice.setAttribute("href",href);
+                               choice.setAttribute("title",title);
+                               if (i == 0) choice.setAttribute("checked","");
+                               
+                               disambcell.appendChild(choice);
+                               disambcell.appendChild(document.createTextNode(desc));
+                               disambcell.appendChild(document.createElement("br"));
+                           }
+
+                           var okbutton = document.createElement("input");
+                           okbutton.setAttribute("type","button");
+                           okbutton.setAttribute("value","OK");
+                           okbutton.setAttribute("onclick","do_disambiguate()");
+                           var cancelbutton = document.createElement("input");
+                           cancelbutton.setAttribute("type","button");
+                           cancelbutton.setAttribute("value","Cancel");
+                           cancelbutton.setAttribute("onclick","cancel_disambiguate()");
+
+                           disambcell.appendChild(okbutton);
+                           disambcell.appendChild(cancelbutton);
+
+                           matita.disambMode = true;
+                           updateSide();
+                       }
+                       else if (is_defined(disamberr)) {
+                           // must be fixed in a daemon: it makes sense to return a 
+                           // disambiguation error with no choices
+                           if (disamberr.childNodes.length > 0) {
+                             set_cps(disamberr.childNodes);
+                             matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
+                             matita.disambMode = true;
+                             next_cp(0);
+                           }
+                           throw "Stop";
+                       }
+                       else if (is_defined(localized)) {
+                           unlocked.innerHTML = localized.childNodes[0].wholeText;
+                       }
+                       else if (is_defined(generic_err)) {
+                           debug("Unmanaged error:\n" ^ generic_err.childNodes[0].wholeText);
+                       }
                } else {
                        debug("goto bottom failed");
                } 
                 resume();
        };
        pause();
-       callServer("bottom",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
-  
+       callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
 }
 
 
+function gotoTop()
+{
+       processor = function(xml) {
+               if (is_defined(xml)) {
+                 if (xml.childNodes[0].textContent != "ok") {
+                     debug("goto top failed");
+                  }
+                  else
+                        statements = listnil();
+                       /*
+                        lockedlen = locked.innerHTML.length - statementlen;
+                       statement = locked.innerHTML.substr(lockedlen, statementlen);
+                        locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
+                       unlocked.innerHTML = statement + unlocked.innerHTML;
+                       */
+                       unlocked.innerHTML = lockedbackup + unlocked.innerHTML;
+                       lockedbackup = "";
+                        locked.innerHTML = lockedbackup;
+                       init_autotraces();
+                        hideSequent();
+                        unlocked.scrollIntoView(true);
+               } else {
+                       debug("goto top failed");
+               } 
+                resume();
+       };
+       pause();
+       callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
+  
+}
+
 function gotoPos(offset)
 {
         if (!is_defined(offset)) {
@@ -371,35 +1027,52 @@ function gotoPos(offset)
         }
        processor = function(xml) {
                if (is_defined(xml)) {
-                       // debug("goto pos: received response\nBEGIN\n" + req.responseText + "\nEND");
-                       len = parseInt(xml.getElementsByTagName("parsed")[0].getAttribute("length"));
-                       len0 = unlocked.innerHTML.length;
-                       unescaped = unlocked.innerHTML.unescapeHTML();
-                       parsedtxt = unescaped.substr(0,len); 
+                   try {
+                       /*
+                       parsed = xml.getElementsByTagName("parsed")[0];
+                       len = parseInt(parsed.getAttribute("length"));
+                       // len0 = unlocked.innerHTML.length;
+                       unescaped = unlocked.innerHTML.html_to_matita();
+                       parsedtxt = parsed.childNodes[0].wholeText;
+                       //parsedtxt = unescaped.substr(0,len); 
                        unparsedtxt = unescaped.substr(len);
-                       locked.innerHTML = locked.innerHTML + parsedtxt;
-                       unlocked.innerHTML = unparsedtxt;
-                       len1 = unlocked.innerHTML.length;
-                       len2 = len0 - len1;
+                       lockedbackup += parsedtxt;
+                       locked.innerHTML = lockedbackup; //.matita_to_html();
+                       unlocked.innerHTML = unparsedtxt.matita_to_html();
+                       // len1 = unlocked.innerHTML.length;
+                       len2 = parsedtxt.length;
                        metasenv = xml.getElementsByTagName("meta");
                        // populate_goalarray(metasenv);
                        statements = listcons(len2,statements);
                        unlocked.scrollIntoView(true);
                        // la populate non andrebbe fatta a ogni passo
+                       */
+                       var len = advOneStep(xml);
                        if (offset <= len) {
+                               init_autotraces();
                                populate_goalarray(metasenv);
                                resume();
                        } else {
                                gotoPos(offset - len);
                        }
+                   } catch (er) {
+                       init_autotraces();
+                       populate_goalarray(metasenv);
+                       if (er == "Stop") 
+                               resume();
+                       else
+                               pause();
+
+                   }
                } else {
+                       init_autotraces();
                        unlocked.scrollIntoView(true);
                        populate_goalarray(metasenv);
                        resume();
                }
        }
        pause();
-       callServer("advance",processor,"body=" + (unlocked.innerHTML.unescapeHTML()).sescape());
+       callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
 }
 
 function retract()
@@ -409,11 +1082,19 @@ function retract()
                        // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
                        statementlen = parseInt(listhd(statements));
                         statements = listtl(statements);
+                       /*
                         lockedlen = locked.innerHTML.length - statementlen;
                        statement = locked.innerHTML.substr(lockedlen, statementlen);
                         locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
                        unlocked.innerHTML = statement + unlocked.innerHTML;
+                       */
+                        lockedlen = lockedbackup.length - statementlen;
+                       statement = lockedbackup.substr(lockedlen, statementlen);
+                       lockedbackup = lockedbackup.substr(0,lockedlen);
+                        locked.innerHTML = lockedbackup;
+                       unlocked.innerHTML = statement + unlocked.innerHTML;
                        metasenv = xml.getElementsByTagName("meta");
+                       init_autotraces();
                         populate_goalarray(metasenv);
                         unlocked.scrollIntoView(true);
                } else {
@@ -421,10 +1102,8 @@ function retract()
                }
                resume();
        };
-       debug("retract 1");
        pause();
         callServer("retract",processor);
-       debug("retract 2");
 }
 
 function openFile()
@@ -432,8 +1111,9 @@ function openFile()
        processor = function(xml)
        {
                if (is_defined(xml)) {  
-                       locked.innerHTML = "";
-                       unlocked.innerHTML = xml.documentElement.textContent;
+                       lockedbackup = "";
+                       locked.innerHTML = lockedbackup;
+                       unlocked.innerHTML = xml.documentElement.wholeText;
                } else {
                        debug("file open failed");
                }
@@ -441,16 +1121,275 @@ function openFile()
        callServer("open",processor,"file=" + escape(filename.value)); 
 }
 
+function retrieveFile(thefile)
+{ 
+       processor = function(xml)
+       {
+               if (is_defined(xml)) {  
+                       changeFile(thefile);
+                        matita.disambMode = false;
+                        matita.proofMode = false;
+                       updateSide();
+                       lockedbackup = ""
+                       locked.innerHTML = lockedbackup;
+                        // code originally used in google chrome (problems with mozilla)
+                       // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
+                       // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
+                       debug(xml.childNodes[0].textContent);
+                        if (document.all) { // IE
+                          unlocked.innerHTML = xml.childNodes[0].text;
+                        } else {
+                          unlocked.innerHTML = xml.childNodes[0].textContent;
+                        }
+                       init_autotraces();
+
+               } else {
+                       debug("file open failed");
+               }
+       };
+       abortDialog("dialogBox");
+       callServer("open",processor,"file=" + escape(thefile)); 
+}
+
+function showLibrary(title,callback,reloadDialog)
+{ 
+       var req = null;
+        dialogBox.reload = reloadDialog; 
+        // pause();
+       if (window.XMLHttpRequest)
+       {
+               req = new XMLHttpRequest();
+       } 
+       else if (window.ActiveXObject) 
+       {
+               try {
+                               req = new ActiveXObject("Msxml2.XMLHTTP");
+               } catch (e)
+               {
+                       try {
+                               req = new ActiveXObject("Microsoft.XMLHTTP");
+                               } catch (e) {}
+               }
+       }
+       req.onreadystatechange = function()
+       { 
+
+               rs = req.readyState;
+
+               if(rs == 4)
+               {
+                       stat = req.status;
+                       stxt = req.statusText;
+                       if(stat == 200)
+                       {
+                         debug(req.responseText);
+                          showDialog("<H2>" + title + "</H2>",req.responseText, callback);
+                       } 
+               } 
+       };
+       req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
+        req.setRequestHeader("Content-type","application/x-www-form-urlencoded");      
+       req.send();
+  
+}
+
+function uploadDialog()
+{  
+        uploadBox.style.display = "block";
+}
+
+function uploadOK()
+{   
+   var file = document.getElementById("uploadFilename").files[0];
+//   if (file) { 
+//       var filecontent = file.getAsText("UTF-8");
+//       locked.innerHTML = lockedbackup;
+//       unlocked.innerHTML = filecontent;
+//       uploadBox.style.display = "none";
+//   }
+   if (file) { 
+      var reader = new FileReader();
+      reader.onerror = function (evt) {
+          debug("file open failed");
+      }
+      reader.onload = function (evt) {
+          lockedbackup = "";
+           locked.innerHTML = lockedbackup;
+           unlocked.innerHTML = "";
+           unlocked.appendChild(document.createTextNode(evt.target.result));
+           uploadBox.style.display = "none";
+      }
+      try { reader.readAsText(file, "UTF-8"); }
+      catch (err) { /* nothing to do */ };
+      uploadBox.style.display = "none";
+   }
+}
+
+function openDialog()
+{  
+       callback = function (fname) { retrieveFile(fname); };
+       showLibrary("Open file", callback, openDialog);
+}
+
+function saveDialog()
+{  
+       callback = function (fname) { 
+         abortDialog("dialogBox");
+          saveFile(fname,
+                  (locked.innerHTML.html_to_matita()).sescape(),
+                  (unlocked.innerHTML.html_to_matita()).sescape(),
+                  false,saveDialog); 
+        };
+       showLibrary("Save file as", callback, saveDialog);
+}
+
+function newDialog()
+{
+       callback = function (fname) { 
+         abortDialog("dialogBox");
+         saveFile(fname,"","",false,newDialog,true);
+       };
+       showLibrary("Create new file", callback, newDialog);
+}
+
+
+function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
+{
+        if (!is_defined(reloadFile)) { reloadFile = true };
+        if (!is_defined(fname)) {
+            fname = current_fname;
+           lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
+           unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
+           force = true;
+           // when force is true, reloadDialog is not needed 
+        }
+       processor = function(xml) {
+               if (is_defined(xml)) {
+                 if (xml.childNodes[0].textContent != "ok") {
+                    if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
+                       saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
+                   } else {
+                      reloadDialog();
+                   }
+                 } else {
+                   changeFile(fname);
+                   debug("file saved!");
+                    if (reloadFile) { retrieveFile(fname); }
+                 }
+               } else {
+                       debug("save file failed");
+               }
+               resume();
+       };
+       if (is_defined(fname)) {
+          pause();
+          callServer("save",processor,"file=" + escape(fname) + 
+                                   "&locked=" + lockedtxt +
+                                   "&unlocked=" + unlockedtxt +
+                                   "&force=" + force);
+       }
+       else { debug("no file selected"); }
+}
+
+function createDir() {
+   abortDialog("dialogBox");
+   dirname = prompt("New directory name:\ncic:/matita/","newdir");
+   if (dirname != null) {
+       processor = function(xml) {
+               if (is_defined(xml)) {
+                 if (xml.childNodes[0].textContent != "ok") {
+                      alert("An error occurred :-(");
+                 }
+               } else {
+                      alert("An error occurred :-(");
+               }
+                dialogBox.reload();
+       };
+        pause();
+        callServer("save",processor,"file=" + escape(dirname) + 
+                                    "&locked=&unlocked=&force=false&dir=true");
+   } else {
+      dialogBox.reload();
+   }
+}
+
+function commitAll()
+{
+       processor = function(xml) {
+               if (is_defined(xml)) {
+                        debug(xml.getElementsByTagName("details")[0].textContent);
+                       alert("Commit executed: see details in the log.\n\n" +
+                              "NOTICE: this message does NOT imply (yet) that the commit was successful.");
+               } else {
+                       alert("Commit failed!\n(maybe you don't have permissionis to commit?)");
+               }
+               resume();
+       };
+        pause();
+        callServer("commit",processor);
+}
+
+function updateAll()
+{
+       processor = function(xml) {
+               if (is_defined(xml)) {
+                       alert("Update executed.\n\n" +
+                              "Details:\n" +
+                              xml.getElementsByTagName("details")[0].textContent);
+               } else {
+                       alert("Update failed!\n(maybe you don't have permissions to update?)");
+               }
+               resume();
+       };
+        pause();
+        callServer("update",processor);
+}
+
 var goalcell;
 
 function hideSequent() {
-  goalcell.parentNode.removeChild(goalcell);
-  scriptcell.setAttribute("colspan","2");
+       matita.proofMode = false;
+       updateSide();
 }
 
 function showSequent() {
-  scriptcell.setAttribute("colspan","1");
-  workarea.appendChild(goalcell);
+       matita.proofMode = true;
+       updateSide();
+}
+
+function showDialog(title,content,callback) {
+  dialogTitle.innerHTML = title;
+  dialogContent.innerHTML = content;
+  dialogBox.callback = callback;
+
+  //Get the screen height and width
+  var maskHeight = $(document).height();
+  var maskWidth = $(window).width();
+  
+  //Set heigth and width to mask to fill up the whole screen
+  $('#mask').css({'width':maskWidth,'height':maskHeight});
+  
+  //transition effect          
+  $('#mask').fadeIn(100);      
+  $('#mask').fadeTo(200,0.8);  
+  
+  //Get the window height and width
+  var winH = $(window).height();
+  var winW = $(window).width();
+  
+  //Set the popup window to center
+  $('#dialogBox').css('top',  winH/2-$('#dialogBox').height()/2);
+  $('#dialogBox').css('left', winW/2-$('#dialogBox').width()/2);
+  
+  //transition effect
+  $('#dialogBox').fadeIn(200); 
+
+  dialogBox.style.display = "block";
+}
+
+function abortDialog(dialog) {
+  document.getElementById(dialog).style.display = "none";
+  $('#mask').hide();
 }
 
 function removeElement(id) {
@@ -458,19 +1397,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);
@@ -494,7 +1438,7 @@ function findNode(list, node, acc) {
     dup = list[i].cloneNode(true);
     sandbox.appendChild(dup);
 //    debug("fail " + i + ": " + sandbox.innerHTML);
-    acc += sandbox.innerHTML.length;
+    acc += sandbox.innerHTML.html_to_matita().length;
     sandbox.removeChild(dup);
   }
   throw "not found";
@@ -504,6 +1448,79 @@ function test () {
   debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
 }
 
+function get_checked_index(name) {
+       var radios = document.getElementsByName(name);
+       for (i = 0; i < radios.length; i++) {
+           if (radios[i].checked) {
+                   return i;
+           }
+       }
+       return null;
+}
+
+function cancel_disambiguate() {
+       matita.disambMode = false;
+        resume();
+       // enable_toparea();
+       // enable_editing();
+        strip_tags("span","error");
+       updateSide();
+}
+
+function do_disambiguate() {
+       var i = get_checked_index("interpr");
+       if (i != null) {
+           var pre = matita.unlockedbackup
+                     .substring(0,matita.ambiguityStart).matita_to_html();
+           var mid = matita.unlockedbackup
+                     .substring(matita.ambiguityStart,matita.ambiguityStop)
+                     .matita_to_html();
+           var post = matita.unlockedbackup
+                      .substring(matita.ambiguityStop).matita_to_html();
+
+           var href = matita.interpretations[i].href;
+           var title = matita.interpretations[i].title;
+
+           if (is_defined(title)) {
+                mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
+           } else {
+                mid = "<A href=\"" + href + "\">" + mid + "</A>";
+           }
+
+           unlocked.innerHTML = pre + mid + post;
+
+           matita.disambMode = false;
+           enable_toparea();
+           enable_editing();
+           updateSide();
+       }
+}
+
+function do_showerror() {
+       var i = get_checked_index("choice");
+       if (i != null) {
+           var pre = matita.unlockedbackup
+                     .substring(0,matita.ambiguityStart).matita_to_html();
+           var mid = matita.unlockedbackup
+                     .substring(matita.ambiguityStart,matita.ambiguityStop)
+                     .matita_to_html();
+           var post = matita.unlockedbackup
+                      .substring(matita.ambiguityStop).matita_to_html();
+
+           var href = matita.interpretations[i].href;
+           var title = matita.interpretations[i].title;
+
+           if (is_defined(title)) {
+                mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
+           } else {
+                mid = "<A href=\"" + href + "\">" + mid + "</A>";
+           }
+
+           unlocked.innerHTML = pre + mid + post;
+
+       }
+}
+
 function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
@@ -514,3 +1531,59 @@ function readCookie(name) {
        }
        return null;
 }
+
+function delete_cookie ( cookie_name )
+{
+  var cookie_date = new Date();  // current date & time
+  cookie_date.setTime ( cookie_date.getTime() - 1 );
+  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
+}
+
+function delete_session()
+{
+       delete_cookie("session");
+}
+
+function disable_toparea() {
+       var offset = $('#toparea').offset();
+        $('#whitemask').css('top',offset.top);
+        $('#whitemask').css('left',offset.left);
+        $('#whitemask').css('width',$('#toparea').outerWidth() + "px");
+        $('#whitemask').css('height',$('#toparea').outerHeight() + "px");
+       $('#whitemask').fadeTo('fast',0.7);
+}
+
+function enable_toparea() {
+       $('#whitemask').hide();
+}
+
+function disable_editing() {
+       unlocked.contentEditable = false;
+}
+
+function enable_editing() {
+       unlocked.contentEditable = true;
+}
+
+function pause()
+{
+       // advanceButton.disabled = true;
+        // retractButton.disabled = true;
+        // cursorButton.disabled = true;
+        // bottomButton.disabled = true;
+       disable_toparea();
+       disable_editing();
+}
+
+function resume()
+{
+       // advanceButton.disabled = false;
+        // retractButton.disabled = false;
+        // cursorButton.disabled = false;
+        // bottomButton.disabled = false;
+        if (!matita.disambMode) {
+               enable_toparea();
+               enable_editing();
+        }
+}
+