18 var lockedbackup = "";
21 function text_of_html(h)
30 function unescape_html(s)
32 u = document.getElementById("unescape");
34 return text_of_html(u)
37 function filterByClass (elements,cname){
38 var itemsfound = new Array;
39 for(var i=0;i<elements.length;i++){
40 if(elements[i].className == cname){
41 itemsfound.push(elements[i]);
49 if (readCookie("session") == null) {
50 window.location = "/login.html"
53 titlebar = document.getElementById("titlebar");
54 matitaTitle = document.getElementById("matitaTitle");
55 apparea = document.getElementById("matitaapparea");
56 locked = document.getElementById("locked");
57 unlocked = document.getElementById("unlocked");
58 toparea = document.getElementById("toparea");
59 workarea = document.getElementById("workarea");
60 scriptcell = document.getElementById("scriptcell");
61 sidearea = document.getElementById("sidearea");
62 disambcell = document.getElementById("disambcell");
63 goalcell = document.getElementById("goalcell");
64 goals = document.getElementById("goals");
65 goalview = document.getElementById("goalview");
66 filename = document.getElementById("filename");
67 logarea = document.getElementById("logarea");
68 advanceButton = document.getElementById("advance");
69 retractButton = document.getElementById("retract");
70 cursorButton = document.getElementById("cursor");
71 bottomButton = document.getElementById("bottom");
72 dialogBox = document.getElementById("dialogBox");
73 uploadBox = document.getElementById("uploadBox");
74 dialogTitle = document.getElementById("dialogTitle");
75 dialogContent = document.getElementById("dialogContent");
77 matita = new Object();
78 matita.disambMode = matita.proofMode = false;
80 // hide sequent view at start
84 changeFile("test.ma");
86 // initialize keyboard events in the unlocked script
87 init_keyboard(unlocked);
94 function init_autotraces() {
95 $("#unlocked .autotactic").tooltip({
98 bodyHandler: function() {
99 return (trace_of($(this)[0]));
102 $("#locked .autotactic").tooltip({
105 bodyHandler: function() {
106 return (trace_of($(this)[0]));
111 function trace_of(node) {
112 return text_of_html(filterByClass(node.childNodes,"autotrace")[0]);
115 function changeFile(name) {
116 current_fname = name;
117 matitaTitle.innerHTML = "Matita - cic:/matita/" + name;
120 function init_keyboard(target)
122 if (target.addEventListener)
124 // target.addEventListener("keydown",keydown,false);
125 target.addEventListener("keypress",keypress,false);
126 // target.addEventListener("keyup",keyup,false);
127 // target.addEventListener("textInput",textinput,false);
129 else if (target.attachEvent)
131 // target.attachEvent("onkeydown", keydown);
132 target.attachEvent("onkeypress", keypress);
133 // target.attachEvent("onkeyup", keyup);
134 // target.attachEvent("ontextInput", textinput);
138 // target.onkeydown= keydown;
139 target.onkeypress= keypress;
140 // target.onkeyup= keyup;
141 // target.ontextinput= textinput; // probably doesn't work
148 if (n == null) return 'undefined';
150 if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
151 while (s.length < 9) s+= ' ';
155 function string_of_key(n)
157 if (n == null) return 'undefined';
158 return String.fromCharCode(n);
161 function pressmesg(w,e)
163 debug(w + ' keyCode=' + keyval(e.keyCode) +
164 ' which=' + keyval(e.which) +
165 ' charCode=' + keyval(e.charCode) +
166 '\n shiftKey='+e.shiftKey
167 + ' ctrlKey='+e.ctrlKey
168 + ' altKey='+e.altKey
169 + ' metaKey='+e.metaKey);
172 function suppressdefault(e,flag)
176 if (e.preventDefault) e.preventDefault();
177 if (e.stopPropagation) e.stopPropagation();
182 function restoreSelection(r) {
185 if (window.getSelection)//non IE and there is already a selection
187 var s = window.getSelection();
188 if (s.rangeCount > 0)
193 if (document.createRange)//non IE and no selection
195 window.getSelection().addRange(r);
198 if (document.selection)//IE
205 function lookup_tex(texmacro)
207 texmacro = texmacro.substring(1);
208 return unescape(macro2utf8[texmacro]);
211 function strip_tags(tagname,classname)
213 var tags = unlocked.getElementsByTagName(tagname);
214 if (is_defined(classname)) {
215 tags = filterByClass(tags,classname);
217 var tlen = tags.length; // preserving the value from removeChild operations
218 for (i = 0; i < tlen; i++) {
219 var children = tags[i].childNodes;
220 for (j = 0; j < children.length; j++) {
221 tags[i].parentNode.insertBefore(children[j],tags[i]);
224 for (var i = 0; i < tlen; i++) {
225 tags[0].parentNode.removeChild(tags[0]);
229 function strip_interpr() {
231 alert("strip_interpr ended");
237 pressmesg('keypress',e);
238 var s = string_of_key(e.charCode);
239 strip_tags("span","error");
242 i = unlocked.innerHTML.html_to_matita().lastIndexOf('\\',j);
244 match = unlocked.innerHTML.html_to_matita().substring(i,j);
245 sym = unescape_html(lookup_tex(match));
246 if (sym != "undefined") {
247 if (window.getSelection) { // non IE
248 savedRange.setStart(savedsc,savedso - (j-i));
249 savedRange.deleteContents();
250 savedRange.insertNode(document.createTextNode(sym));
251 savedsc.parentNode.normalize();
252 if (savedRange.collapsed) { // Mozilla
253 savedRange.setEnd(savedsc,savedRange.endOffset + sym.length);
255 savedRange.collapse(false);
257 savedRange.moveStart(i-j);
258 savedRange.text(sym);
259 savedRange.collapse(false);
261 restoreSelection(savedRange);
262 return suppressdefault(e,true);
265 // restoreSelection(0);
266 return suppressdefault(e,false);
269 else return suppressdefault(e,false);
271 return suppressdefault(e,false);
279 // internet explorer (v.9) doesn't work with innerHTML
280 // but google chrome's innerText is, in a sense, "write only"
281 // what should we do?
282 // logarea.innerText = txt + "\n" + logarea.innerText;
283 logtxt = /* logtxt + "\n" +*/ txt;
287 logWin = window.open( "", "Matita Log",
288 "width=600,height=450,status,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
289 logWin.document.write('<html><head><title>Matita Log' + '</title></head>');
290 logWin.document.write('<body><textarea style="width:100%;height:100%;">' +
291 logtxt + '</textarea></body></html>');
292 logWin.document.close();
298 debug("hd of '" + l + "' = '" + ar[0] + "'");
306 debug("tl of '" + l + "' = '" + tl + "'");
310 function listcons(x,l)
312 debug("cons '" + x + "' on '" + l + "'");
313 return (x + "#" + l);
321 function list_append(l1,l2)
329 function fold_left (f,acc,l)
332 { debug("'" + l + "' is fold end");
335 { debug("'" + l + "' is fold cons");
336 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
339 function listiter (f,l)
342 { debug("'" + l + "' is nil");
347 debug("'" + l + "' is not nil");
349 listiter(f,listtl(l));
353 function listmap (f,l)
355 debug("listmap on " + l);
357 { debug("returning listnil");
361 { debug("cons f(hd) map(f,tl)");
362 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
366 var statements = listnil();
369 var metalist = listnil();
371 function pairmap (f,p)
373 debug("pairmap of '" + p + "'");
375 return (f(ar[0],ar[1]));
378 function tripletmap (f,p)
380 debug("tripletmap of '" + p + "'");
382 return (f(ar[0],ar[1],ar[2]));
388 return (pairmap (function (a,b) { return (a); }, p));
394 return (tripletmap (function (a,b,c) { return (a); }, p));
400 return (tripletmap (function (a,b,c) { return (b); }, p));
406 return (tripletmap (function (a,b,c) { return (c); }, p));
409 function populate_goalarray(menv)
411 debug("metasenv.length = " + menv.length);
412 if (menv.length == 0) {
418 goalarray = new Array();
419 metalist = listnil();
420 var tmp_goallist = "";
421 for (i = 0; i < menv.length; i++) {
422 metano = menv[i].getAttribute("number");
423 metaname = menv[i].childNodes[0].childNodes[0].data;
424 goal = menv[i].childNodes[1].childNodes[0].data;
425 debug ("found meta n. " + metano);
426 debug ("found goal\nBEGIN" + goal + "\nEND");
427 goalarray[metano] = goal;
428 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
429 metalist = listcons(metano,metalist);
430 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
432 goals.innerHTML = tmp_goallist;
433 debug("new metalist is '" + metalist + "'");
434 if (is_nil(metalist)) {
438 switch_goal(listhd(metalist));
443 function switch_goal(meta)
445 if (typeof meta == "undefined") {
446 goalview.innerHTML = "";
449 debug("switch_goal " + meta + "\n" + goalarray[meta]);
450 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
454 // the following is used to avoid escaping unicode, which results in
455 // the server being unable to unescape the string
456 String.prototype.sescape = function() {
462 result = result.replace(patt1,"%25");
463 result = result.replace(patt2,"%3D");
464 result = result.replace(patt3,"%26");
465 result = result.replace(patt4,"%2B");
469 String.prototype.html_to_matita = function()
471 var patt1 = /<br(\/|)>/gi;
474 var patt4 = /</gi;
475 var patt5 = />/gi;
476 var patt6 = / /gi;
478 result = result.replace(patt1,"\n");
479 result = result.replace(patt2,"\005");
480 result = result.replace(patt3,"\006");
481 result = result.replace(patt4,"<");
482 result = result.replace(patt5,">");
483 result = result.replace(patt6," ");
484 return (unescape(result));
487 String.prototype.matita_to_html = function()
491 var patt3 = /\005/gi;
492 var patt4 = /\006/gi;
494 result = result.replace(patt1,"<");
495 result = result.replace(patt2,">");
496 result = result.replace(patt3,"<");
497 result = result.replace(patt4,">");
498 return (unescape(result));
501 function is_defined(x)
503 return (typeof x != "undefined");
506 /* servicename: name of the service being called
507 * reqbody: text of the request
508 * processResponse: processes the server response
509 * (takes the response text in input, undefined in case of error)
511 function callServer(servicename,processResponse,reqbody)
515 if (window.XMLHttpRequest)
517 req = new XMLHttpRequest();
519 else if (window.ActiveXObject)
522 req = new ActiveXObject("Msxml2.XMLHTTP");
526 req = new ActiveXObject("Microsoft.XMLHTTP");
530 req.onreadystatechange = function()
538 stxt = req.statusText;
541 debug(req.responseText);
542 if (window.DOMParser) {
543 parser=new DOMParser();
544 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
546 else // Internet Explorer
548 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
549 xmlDoc.async="false";
550 xmlDoc.loadXML(req.responseText);
552 processResponse(xmlDoc);
558 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
559 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
568 function advOneStep(xml) {
569 var parsed = xml.getElementsByTagName("parsed")[0];
570 var ambiguity = xml.getElementsByTagName("ambiguity")[0];
571 var disamberr = xml.getElementsByTagName("disamberror")[0];
572 if (is_defined(parsed)) {
573 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
574 var len = parseInt(parsed.getAttribute("length"));
575 // len0 = unlocked.innerHTML.length;
576 var unescaped = unlocked.innerHTML.html_to_matita();
577 var parsedtxt = parsed.childNodes[0].wholeText;
578 //parsedtxt = unescaped.substr(0,len);
579 var unparsedtxt = unescaped.substr(len);
580 lockedbackup += parsedtxt;
581 locked.innerHTML = lockedbackup;
582 unlocked.innerHTML = unparsedtxt.matita_to_html();
583 // len1 = unlocked.innerHTML.length;
584 // len2 = len0 - len1;
585 var len2 = parsedtxt.length;
586 metasenv = xml.getElementsByTagName("meta");
587 statements = listcons(len2,statements);
588 unlocked.scrollIntoView(true);
591 else if (is_defined(ambiguity)) {
592 var start = parseInt(ambiguity.getAttribute("start"));
593 var stop = parseInt(ambiguity.getAttribute("stop"));
594 var choices = xml.getElementsByTagName("choice");
596 matita.ambiguityStart = start;
597 matita.ambiguityStop = stop;
598 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
599 matita.interpretations = [];
601 var unlockedtxt = unlocked.innerHTML.html_to_matita();
602 var pre = unlockedtxt.substring(0,start).matita_to_html();
603 var mid = unlockedtxt.substring(start,stop).matita_to_html();
604 var post = unlockedtxt.substring(stop).matita_to_html();
605 unlocked.innerHTML = pre +
606 "<span class=\"error\" title=\"disambiguation error\">" +
607 mid + "</span>" + post;
609 var title = "<H3>Ambiguous input</H3>";
610 disambcell.innerHTML = title;
611 for (i = 0;i < choices.length;i++) {
612 matita.interpretations[i] = new Object();
614 var href = choices[i].getAttribute("href");
615 var title = choices[i].getAttribute("title");
616 var desc = choices[i].childNodes[0].nodeValue;
618 matita.interpretations[i].href = href;
619 matita.interpretations[i].title = title;
620 matita.interpretations[i].desc = desc;
622 var choice = document.createElement("input");
623 choice.setAttribute("type","radio");
624 choice.setAttribute("name","interpr");
625 choice.setAttribute("href",href);
626 choice.setAttribute("title",title);
627 if (i == 0) choice.setAttribute("checked","");
629 disambcell.appendChild(choice);
630 disambcell.appendChild(document.createTextNode(desc));
631 disambcell.appendChild(document.createElement("br"));
634 var okbutton = document.createElement("input");
635 okbutton.setAttribute("type","button");
636 okbutton.setAttribute("value","OK");
637 okbutton.setAttribute("onclick","do_disambiguate()");
638 var cancelbutton = document.createElement("input");
639 cancelbutton.setAttribute("type","button");
640 cancelbutton.setAttribute("value","Cancel");
641 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
643 disambcell.appendChild(okbutton);
644 disambcell.appendChild(cancelbutton);
646 matita.disambMode = true;
650 else if (is_defined(disamberr)) {
651 // must be fixed in a daemon: it makes sense to return a
652 // disambiguation error with no choices
653 if (disamberr.childNodes.length > 0) {
654 set_cps(disamberr.childNodes);
655 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
656 matita.disambMode = true;
662 var error = xml.getElementsByTagName("error")[0];
663 unlocked.innerHTML = error.childNodes[0].wholeText;
664 // debug(xml.childNodes[0].nodeValue);
670 function advanceForm1()
672 processor = function(xml) {
674 if (is_defined(xml)) {
676 populate_goalarray(metasenv);
679 debug("advance failed");
687 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
691 // get or set <choicepoint>'s (in case of disamb error)
693 return matita.choicepoints
696 function set_cps(cps) {
697 matita.choicepoints = cps;
700 // get radio buttons for <choice>'s in a given cp
701 function get_choice_opts(i) {
703 var choices = get_cps()[i].childNodes;
704 for (var j = 0;j < choices.length;j++) {
705 var href = choices[j].getAttribute("href");
706 var title = choices[j].getAttribute("title");
708 if (is_defined(title) && title != null) {
710 } else if (is_defined(href) && href != null) {
716 res[j] = document.createElement("input");
717 res[j].setAttribute("type","radio");
718 res[j].setAttribute("name","choice");
719 res[j].setAttribute("choicepointno",i);
720 res[j].setAttribute("choiceno",j);
721 res[j].setAttribute("href",href);
722 res[j].setAttribute("title",title);
723 if (desc != null) res[j].setAttribute("desc",desc);
725 if (j == 0) res[j].setAttribute("checked","");
730 // get radio buttons for <failure>'s in a given choice
731 function get_failure_opts(i,j) {
733 var failures = get_cps()[i].childNodes[j].childNodes;
734 for (var k = 0;k < failures.length;k++) {
735 var start = failures[k].getAttribute("start");
736 var stop = failures[k].getAttribute("stop");
737 var title = failures[k].getAttribute("title");
739 res[k] = document.createElement("input");
740 res[k].setAttribute("type","radio");
741 res[k].setAttribute("name","failure");
742 res[k].setAttribute("choicepointno",i);
743 res[k].setAttribute("choiceno",j);
744 res[k].setAttribute("failureno",k);
745 res[k].setAttribute("start",start);
746 res[k].setAttribute("stop",stop);
747 res[k].setAttribute("title",title);
749 if (k == 0) res[k].setAttribute("checked","");
754 function next_cp(curcp) {
755 var cp = get_cps()[curcp];
756 var start = parseInt(cp.getAttribute("start"));
757 var stop = parseInt(cp.getAttribute("stop"));
759 matita.errorStart = start;
760 matita.errorStop = stop;
761 // matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
763 var unlockedtxt = matita.unlockedbackup;
764 var pre = unlockedtxt.substring(0,start).matita_to_html();
765 var mid = unlockedtxt.substring(start,stop).matita_to_html();
766 var post = unlockedtxt.substring(stop).matita_to_html();
767 unlocked.innerHTML = pre +
768 "<span class=\"error\" title=\"error location\">" +
769 mid + "</span>" + post;
771 var title = "<H3>Error diagnostics</H3>";
772 disambcell.innerHTML = title;
773 var choices = get_choice_opts(curcp);
774 for (var i = 0;i < choices.length;i++) {
775 disambcell.appendChild(choices[i]);
776 var desc = choices[i].getAttribute("desc");
777 if (!is_defined(desc) || desc == null) {
778 desc = "Interpretation " + i;
780 disambcell.appendChild(document.createTextNode(desc));
781 disambcell.appendChild(document.createElement("br"));
784 // update index of the next choicepoint
785 new_curcp = (curcp + 1) % get_cps().length;
787 var okbutton = document.createElement("input");
788 okbutton.setAttribute("type","button");
789 okbutton.setAttribute("value","OK");
790 okbutton.setAttribute("onclick","show_failures()");
791 var cancelbutton = document.createElement("input");
792 cancelbutton.setAttribute("type","button");
793 cancelbutton.setAttribute("value","Close");
794 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
795 var tryagainbutton = document.createElement("input");
796 tryagainbutton.setAttribute("type","button");
798 tryagainbutton.setAttribute("value","Try something else");
800 tryagainbutton.setAttribute("value","Restart");
802 tryagainbutton.setAttribute("onclick","next_cp(" + new_curcp + ")");
804 disambcell.appendChild(okbutton);
805 disambcell.appendChild(tryagainbutton);
806 disambcell.appendChild(cancelbutton);
810 //matita.disambMode = true;
815 function show_failures() {
817 var choice = document.getElementsByName("choice")[get_checked_index("choice")];
818 var cpno = parseInt(choice.getAttribute("choicepointno"));
819 var choiceno = parseInt(choice.getAttribute("choiceno"));
820 var choicedesc = choice.getAttribute("desc");
822 var title = "<H3>Error diagnostics</H3>";
824 if (is_defined(choicedesc) && choicedesc != null) {
825 subtitle = "<p>Errors at node " + cpno + " = " + choicedesc + "</p>";
827 subtitle = "<p>Global errors:</p>";
830 disambcell.innerHTML = title + subtitle;
831 var failures = get_failure_opts(cpno,choiceno);
832 for (var i = 0;i < failures.length;i++) {
833 disambcell.appendChild(failures[i]);
834 disambcell.appendChild(document.createTextNode(failures[i].getAttribute("title")));
835 disambcell.appendChild(document.createElement("br"));
838 var okbutton = document.createElement("input");
839 okbutton.setAttribute("type","button");
840 okbutton.setAttribute("value","Show error loc");
841 okbutton.setAttribute("onclick","show_err()");
842 var cancelbutton = document.createElement("input");
843 cancelbutton.setAttribute("type","button");
844 cancelbutton.setAttribute("value","Close");
845 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
846 var backbutton = document.createElement("input");
847 backbutton.setAttribute("type","button");
848 backbutton.setAttribute("value","<< Back");
849 backbutton.setAttribute("onclick","next_cp(" + cpno + ")");
851 disambcell.appendChild(backbutton);
852 disambcell.appendChild(okbutton);
853 disambcell.appendChild(cancelbutton);
857 function show_err() {
858 var radios = document.getElementsByName("failure");
859 for (i = 0; i < radios.length; i++) {
860 if (radios[i].checked) {
861 var start = radios[i].getAttribute("start");
862 var stop = radios[i].getAttribute("stop");
863 var title = radios[i].getAttribute("title");
864 var unlockedtxt = matita.unlockedbackup;
865 var pre = unlockedtxt.substring(0,start).matita_to_html();
866 var mid = unlockedtxt.substring(start,stop).matita_to_html();
867 var post = unlockedtxt.substring(stop).matita_to_html();
868 unlocked.innerHTML = pre +
869 "<span class=\"error\" title=\"Disambiguation failure\">" +
870 mid + "</span>" + post;
876 function gotoBottom()
878 processor = function(xml) {
879 if (is_defined(xml)) {
880 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
881 var parsed = xml.getElementsByTagName("parsed");
882 var localized = xml.getElementsByTagName("localized")[0];
883 var ambiguity = xml.getElementsByTagName("ambiguity")[0];
884 var generic_err = xml.getElementsByTagName("error")[0];
885 var disamberr = xml.getElementsByTagName("disamberror")[0];
886 for (var i = 0;i < parsed.length; i++) {
887 var len = parsed[i].getAttribute("length");
888 // len0 = unlocked.innerHTML.length;
889 var unescaped = unlocked.innerHTML.html_to_matita();
890 // the browser may decide to split textnodes: use wholeText!
891 var parsedtxt = parsed[i].childNodes[0].wholeText;
892 //parsedtxt = unescaped.substr(0,len);
893 var unparsedtxt = unescaped.substr(len);
894 lockedbackup += parsedtxt;
895 locked.innerHTML = lockedbackup; //.matita_to_html();
896 unlocked.innerHTML = unparsedtxt.matita_to_html();
897 // len1 = unlocked.innerHTML.length;
898 var len2 = parsedtxt.length;
899 statements = listcons(len2,statements);
901 unlocked.scrollIntoView(true);
902 metasenv = xml.getElementsByTagName("meta");
904 populate_goalarray(metasenv);
906 if (is_defined(ambiguity)) {
907 var start = parseInt(ambiguity.getAttribute("start"));
908 var stop = parseInt(ambiguity.getAttribute("stop"));
909 var choices = xml.getElementsByTagName("choice");
911 matita.ambiguityStart = start;
912 matita.ambiguityStop = stop;
913 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
914 matita.interpretations = [];
916 var unlockedtxt = unlocked.innerHTML.html_to_matita();
917 var pre = unlockedtxt.substring(0,start).matita_to_html();
918 var mid = unlockedtxt.substring(start,stop).matita_to_html();
919 var post = unlockedtxt.substring(stop).matita_to_html();
920 unlocked.innerHTML = pre +
921 "<span class=\"error\" title=\"disambiguation error\">" +
922 mid + "</span>" + post;
924 var title = "<H3>Ambiguous input</H3>";
925 disambcell.innerHTML = title;
926 for (i = 0;i < choices.length;i++) {
927 matita.interpretations[i] = new Object();
929 var href = choices[i].getAttribute("href");
930 var title = choices[i].getAttribute("title");
931 var desc = choices[i].childNodes[0].nodeValue;
933 matita.interpretations[i].href = href;
934 matita.interpretations[i].title = title;
935 matita.interpretations[i].desc = desc;
937 var choice = document.createElement("input");
938 choice.setAttribute("type","radio");
939 choice.setAttribute("name","interpr");
940 choice.setAttribute("href",href);
941 choice.setAttribute("title",title);
942 if (i == 0) choice.setAttribute("checked","");
944 disambcell.appendChild(choice);
945 disambcell.appendChild(document.createTextNode(desc));
946 disambcell.appendChild(document.createElement("br"));
949 var okbutton = document.createElement("input");
950 okbutton.setAttribute("type","button");
951 okbutton.setAttribute("value","OK");
952 okbutton.setAttribute("onclick","do_disambiguate()");
953 var cancelbutton = document.createElement("input");
954 cancelbutton.setAttribute("type","button");
955 cancelbutton.setAttribute("value","Cancel");
956 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
958 disambcell.appendChild(okbutton);
959 disambcell.appendChild(cancelbutton);
961 matita.disambMode = true;
964 else if (is_defined(disamberr)) {
965 // must be fixed in a daemon: it makes sense to return a
966 // disambiguation error with no choices
967 if (disamberr.childNodes.length > 0) {
968 set_cps(disamberr.childNodes);
969 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
970 matita.disambMode = true;
975 else if (is_defined(localized)) {
976 unlocked.innerHTML = localized.childNodes[0].wholeText;
978 else if (is_defined(generic_err)) {
979 debug("Unmanaged error:\n" ^ generic_err.childNodes[0].wholeText);
982 debug("goto bottom failed");
987 callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
993 processor = function(xml) {
994 if (is_defined(xml)) {
995 if (xml.childNodes[0].textContent != "ok") {
996 debug("goto top failed");
999 statements = listnil();
1001 lockedlen = locked.innerHTML.length - statementlen;
1002 statement = locked.innerHTML.substr(lockedlen, statementlen);
1003 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
1004 unlocked.innerHTML = statement + unlocked.innerHTML;
1006 unlocked.innerHTML = lockedbackup + unlocked.innerHTML;
1008 locked.innerHTML = lockedbackup;
1011 unlocked.scrollIntoView(true);
1013 debug("goto top failed");
1018 callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1022 function gotoPos(offset)
1024 if (!is_defined(offset)) {
1025 offset = getCursorPos();
1027 processor = function(xml) {
1028 if (is_defined(xml)) {
1031 parsed = xml.getElementsByTagName("parsed")[0];
1032 len = parseInt(parsed.getAttribute("length"));
1033 // len0 = unlocked.innerHTML.length;
1034 unescaped = unlocked.innerHTML.html_to_matita();
1035 parsedtxt = parsed.childNodes[0].wholeText;
1036 //parsedtxt = unescaped.substr(0,len);
1037 unparsedtxt = unescaped.substr(len);
1038 lockedbackup += parsedtxt;
1039 locked.innerHTML = lockedbackup; //.matita_to_html();
1040 unlocked.innerHTML = unparsedtxt.matita_to_html();
1041 // len1 = unlocked.innerHTML.length;
1042 len2 = parsedtxt.length;
1043 metasenv = xml.getElementsByTagName("meta");
1044 // populate_goalarray(metasenv);
1045 statements = listcons(len2,statements);
1046 unlocked.scrollIntoView(true);
1047 // la populate non andrebbe fatta a ogni passo
1049 var len = advOneStep(xml);
1050 if (offset <= len) {
1052 populate_goalarray(metasenv);
1055 gotoPos(offset - len);
1059 populate_goalarray(metasenv);
1064 unlocked.scrollIntoView(true);
1065 populate_goalarray(metasenv);
1070 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1075 processor = function(xml) {
1076 if (typeof xml != "undefined") {
1077 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
1078 statementlen = parseInt(listhd(statements));
1079 statements = listtl(statements);
1081 lockedlen = locked.innerHTML.length - statementlen;
1082 statement = locked.innerHTML.substr(lockedlen, statementlen);
1083 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
1084 unlocked.innerHTML = statement + unlocked.innerHTML;
1086 lockedlen = lockedbackup.length - statementlen;
1087 statement = lockedbackup.substr(lockedlen, statementlen);
1088 lockedbackup = lockedbackup.substr(0,lockedlen);
1089 locked.innerHTML = lockedbackup;
1090 unlocked.innerHTML = statement + unlocked.innerHTML;
1091 metasenv = xml.getElementsByTagName("meta");
1093 populate_goalarray(metasenv);
1094 unlocked.scrollIntoView(true);
1096 debug("retract failed");
1101 callServer("retract",processor);
1106 processor = function(xml)
1108 if (is_defined(xml)) {
1110 locked.innerHTML = lockedbackup;
1111 unlocked.innerHTML = xml.documentElement.wholeText;
1113 debug("file open failed");
1116 callServer("open",processor,"file=" + escape(filename.value));
1119 function retrieveFile(thefile)
1121 processor = function(xml)
1123 if (is_defined(xml)) {
1124 changeFile(thefile);
1125 matita.disambMode = false;
1126 matita.proofMode = false;
1129 locked.innerHTML = lockedbackup;
1130 // code originally used in google chrome (problems with mozilla)
1131 // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
1132 // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
1133 debug(xml.childNodes[0].textContent);
1134 if (document.all) { // IE
1135 unlocked.innerHTML = xml.childNodes[0].text;
1137 unlocked.innerHTML = xml.childNodes[0].textContent;
1142 debug("file open failed");
1145 abortDialog("dialogBox");
1146 callServer("open",processor,"file=" + escape(thefile));
1149 function showLibrary(title,callback,reloadDialog)
1152 dialogBox.reload = reloadDialog;
1154 if (window.XMLHttpRequest)
1156 req = new XMLHttpRequest();
1158 else if (window.ActiveXObject)
1161 req = new ActiveXObject("Msxml2.XMLHTTP");
1165 req = new ActiveXObject("Microsoft.XMLHTTP");
1169 req.onreadystatechange = function()
1172 rs = req.readyState;
1177 stxt = req.statusText;
1180 debug(req.responseText);
1181 showDialog("<H2>" + title + "</H2>",req.responseText, callback);
1185 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
1186 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
1191 function uploadDialog()
1193 uploadBox.style.display = "block";
1198 var file = document.getElementById("uploadFilename").files[0];
1200 // var filecontent = file.getAsText("UTF-8");
1201 // locked.innerHTML = lockedbackup;
1202 // unlocked.innerHTML = filecontent;
1203 // uploadBox.style.display = "none";
1206 var reader = new FileReader();
1207 reader.onerror = function (evt) {
1208 debug("file open failed");
1210 reader.onload = function (evt) {
1212 locked.innerHTML = lockedbackup
1213 unlocked.innerHTML = "";
1214 unlocked.appendChild(document.createTextNode(evt.target.result));
1215 uploadBox.style.display = "none";
1217 try { reader.readAsText(file, "UTF-8"); }
1218 catch (err) { /* nothing to do */ };
1219 uploadBox.style.display = "none";
1223 function openDialog()
1225 callback = function (fname) { retrieveFile(fname); };
1226 showLibrary("Open file", callback, openDialog);
1229 function saveDialog()
1231 callback = function (fname) {
1232 abortDialog("dialogBox");
1234 (locked.innerHTML.html_to_matita()).sescape(),
1235 (unlocked.innerHTML.html_to_matita()).sescape(),
1238 showLibrary("Save file as", callback, saveDialog);
1241 function newDialog()
1243 callback = function (fname) {
1244 abortDialog("dialogBox");
1245 saveFile(fname,"","",false,newDialog,true);
1247 showLibrary("Create new file", callback, newDialog);
1251 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
1253 if (!is_defined(reloadFile)) { reloadFile = true };
1254 if (!is_defined(fname)) {
1255 fname = current_fname;
1256 lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
1257 unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
1259 // when force is true, reloadDialog is not needed
1261 processor = function(xml) {
1262 if (is_defined(xml)) {
1263 if (xml.childNodes[0].textContent != "ok") {
1264 if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
1265 saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
1271 debug("file saved!");
1272 if (reloadFile) { retrieveFile(fname); }
1275 debug("save file failed");
1279 if (is_defined(fname)) {
1281 callServer("save",processor,"file=" + escape(fname) +
1282 "&locked=" + lockedtxt +
1283 "&unlocked=" + unlockedtxt +
1286 else { debug("no file selected"); }
1289 function createDir() {
1290 abortDialog("dialogBox");
1291 dirname = prompt("New directory name:\ncic:/matita/","newdir");
1292 if (dirname != null) {
1293 processor = function(xml) {
1294 if (is_defined(xml)) {
1295 if (xml.childNodes[0].textContent != "ok") {
1296 alert("An error occurred :-(");
1299 alert("An error occurred :-(");
1304 callServer("save",processor,"file=" + escape(dirname) +
1305 "&locked=&unlocked=&force=false&dir=true");
1311 function commitAll()
1313 processor = function(xml) {
1314 if (is_defined(xml)) {
1315 debug(xml.getElementsByTagName("details")[0].textContent);
1316 alert("Commit executed: see details in the log.\n\n" +
1317 "NOTICE: this message does NOT imply (yet) that the commit was successful.");
1319 alert("Commit failed!");
1324 callServer("commit",processor);
1327 function updateAll()
1329 processor = function(xml) {
1330 if (is_defined(xml)) {
1331 alert("Update executed.\n\n" +
1333 xml.getElementsByTagName("details")[0].textContent);
1335 alert("Update failed!");
1340 callServer("update",processor);
1345 function hideSequent() {
1346 matita.proofMode = false;
1350 function showSequent() {
1351 matita.proofMode = true;
1355 function showDialog(title,content,callback) {
1356 dialogTitle.innerHTML = title;
1357 dialogContent.innerHTML = content;
1358 dialogBox.callback = callback;
1360 //Get the screen height and width
1361 var maskHeight = $(document).height();
1362 var maskWidth = $(window).width();
1364 //Set heigth and width to mask to fill up the whole screen
1365 $('#mask').css({'width':maskWidth,'height':maskHeight});
1368 $('#mask').fadeIn(100);
1369 $('#mask').fadeTo(200,0.8);
1371 //Get the window height and width
1372 var winH = $(window).height();
1373 var winW = $(window).width();
1375 //Set the popup window to center
1376 $('#dialogBox').css('top', winH/2-$('#dialogBox').height()/2);
1377 $('#dialogBox').css('left', winW/2-$('#dialogBox').width()/2);
1380 $('#dialogBox').fadeIn(200);
1382 dialogBox.style.display = "block";
1385 function abortDialog(dialog) {
1386 document.getElementById(dialog).style.display = "none";
1390 function removeElement(id) {
1391 var element = document.getElementById(id);
1392 element.parentNode.removeChild(element);
1398 function getCursorPos() {
1400 if (window.getSelection) {
1401 var selObj = window.getSelection();
1402 savedRange = selObj.getRangeAt(0);
1403 savedsc = savedRange.startContainer;
1404 savedso = savedRange.startOffset;
1405 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
1406 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
1407 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
1410 else if (document.selection) {
1411 savedRange = document.selection.createRange();
1412 var bookmark = savedRange.getBookmark();
1413 /* FIXME the following works wrong when the document is longer than 65535 chars */
1414 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
1419 function findNode(list, node, acc) {
1420 for (var i = 0; i < list.length; i++) {
1421 if (list[i] == node) {
1422 // debug("success " + i);
1425 if (list[i].hasChildNodes()) {
1427 // debug("recursion on node " + i);
1428 return (findNode(list[i].childNodes,node,acc))
1430 catch (e) { /* debug("recursion failed"); */ }
1432 sandbox = document.getElementById("sandbox");
1433 dup = list[i].cloneNode(true);
1434 sandbox.appendChild(dup);
1435 // debug("fail " + i + ": " + sandbox.innerHTML);
1436 acc += sandbox.innerHTML.html_to_matita().length;
1437 sandbox.removeChild(dup);
1443 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
1446 function get_checked_index(name) {
1447 var radios = document.getElementsByName(name);
1448 for (i = 0; i < radios.length; i++) {
1449 if (radios[i].checked) {
1456 function cancel_disambiguate() {
1457 matita.disambMode = false;
1459 // enable_toparea();
1460 // enable_editing();
1461 strip_tags("span","error");
1465 function do_disambiguate() {
1466 var i = get_checked_index("interpr");
1468 var pre = matita.unlockedbackup
1469 .substring(0,matita.ambiguityStart).matita_to_html();
1470 var mid = matita.unlockedbackup
1471 .substring(matita.ambiguityStart,matita.ambiguityStop)
1473 var post = matita.unlockedbackup
1474 .substring(matita.ambiguityStop).matita_to_html();
1476 var href = matita.interpretations[i].href;
1477 var title = matita.interpretations[i].title;
1479 if (is_defined(title)) {
1480 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1482 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1485 unlocked.innerHTML = pre + mid + post;
1487 matita.disambMode = false;
1494 function do_showerror() {
1495 var i = get_checked_index("choice");
1497 var pre = matita.unlockedbackup
1498 .substring(0,matita.ambiguityStart).matita_to_html();
1499 var mid = matita.unlockedbackup
1500 .substring(matita.ambiguityStart,matita.ambiguityStop)
1502 var post = matita.unlockedbackup
1503 .substring(matita.ambiguityStop).matita_to_html();
1505 var href = matita.interpretations[i].href;
1506 var title = matita.interpretations[i].title;
1508 if (is_defined(title)) {
1509 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1511 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1514 unlocked.innerHTML = pre + mid + post;
1519 function readCookie(name) {
1520 var nameEQ = name + "=";
1521 var ca = document.cookie.split(';');
1522 for(var i=0;i < ca.length;i++) {
1524 while (c.charAt(0)==' ') c = c.substring(1,c.length);
1525 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
1530 function delete_cookie ( cookie_name )
1532 var cookie_date = new Date(); // current date & time
1533 cookie_date.setTime ( cookie_date.getTime() - 1 );
1534 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
1537 function delete_session()
1539 delete_cookie("session");
1542 function disable_toparea() {
1543 var offset = $('#toparea').offset();
1544 $('#whitemask').css('top',offset.top);
1545 $('#whitemask').css('left',offset.left);
1546 $('#whitemask').css('width',$('#toparea').outerWidth() + "px");
1547 $('#whitemask').css('height',$('#toparea').outerHeight() + "px");
1548 $('#whitemask').fadeTo('fast',0.7);
1551 function enable_toparea() {
1552 $('#whitemask').hide();
1555 function disable_editing() {
1556 unlocked.contentEditable = false;
1559 function enable_editing() {
1560 unlocked.contentEditable = true;
1565 // advanceButton.disabled = true;
1566 // retractButton.disabled = true;
1567 // cursorButton.disabled = true;
1568 // bottomButton.disabled = true;
1575 // advanceButton.disabled = false;
1576 // retractButton.disabled = false;
1577 // cursorButton.disabled = false;
1578 // bottomButton.disabled = false;
1579 if (!matita.disambMode) {