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 for (i = 0; i < tags.length; i++) {
218 var children = tags[i].childNodes;
219 for (j = 0; j < children.length; j++) {
220 tags[i].parentNode.insertBefore(children[j],tags[i]);
223 for (var i = 0;tags.length > i;i++) {
224 tags[0].parentNode.removeChild(tags[0]);
228 function strip_interpr() {
230 alert("strip_interpr ended");
236 pressmesg('keypress',e);
237 var s = string_of_key(e.charCode);
238 strip_tags("span","error");
241 i = unlocked.innerHTML.html_to_matita().lastIndexOf('\\',j);
243 match = unlocked.innerHTML.html_to_matita().substring(i,j);
244 sym = unescape_html(lookup_tex(match));
245 if (sym != "undefined") {
246 if (window.getSelection) { // non IE
247 savedRange.setStart(savedsc,savedso - (j-i));
248 savedRange.deleteContents();
249 savedRange.insertNode(document.createTextNode(sym));
250 savedsc.parentNode.normalize();
251 if (savedRange.collapsed) { // Mozilla
252 savedRange.setEnd(savedsc,savedRange.endOffset + sym.length);
254 savedRange.collapse(false);
256 savedRange.moveStart(i-j);
257 savedRange.text(sym);
258 savedRange.collapse(false);
260 restoreSelection(savedRange);
261 return suppressdefault(e,true);
264 // restoreSelection(0);
265 return suppressdefault(e,false);
268 else return suppressdefault(e,false);
270 return suppressdefault(e,false);
278 // internet explorer (v.9) doesn't work with innerHTML
279 // but google chrome's innerText is, in a sense, "write only"
280 // what should we do?
281 // logarea.innerText = txt + "\n" + logarea.innerText;
282 logtxt = /* logtxt + "\n" +*/ txt;
286 logWin = window.open( "", "Matita Log",
287 "width=600,height=450,status,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
288 logWin.document.write('<html><head><title>Matita Log' + '</title></head>');
289 logWin.document.write('<body><textarea style="width:100%;height:100%;">' +
290 logtxt + '</textarea></body></html>');
291 logWin.document.close();
297 debug("hd of '" + l + "' = '" + ar[0] + "'");
305 debug("tl of '" + l + "' = '" + tl + "'");
309 function listcons(x,l)
311 debug("cons '" + x + "' on '" + l + "'");
312 return (x + "#" + l);
320 function list_append(l1,l2)
328 function fold_left (f,acc,l)
331 { debug("'" + l + "' is fold end");
334 { debug("'" + l + "' is fold cons");
335 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
338 function listiter (f,l)
341 { debug("'" + l + "' is nil");
346 debug("'" + l + "' is not nil");
348 listiter(f,listtl(l));
352 function listmap (f,l)
354 debug("listmap on " + l);
356 { debug("returning listnil");
360 { debug("cons f(hd) map(f,tl)");
361 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
365 var statements = listnil();
368 var metalist = listnil();
370 function pairmap (f,p)
372 debug("pairmap of '" + p + "'");
374 return (f(ar[0],ar[1]));
377 function tripletmap (f,p)
379 debug("tripletmap of '" + p + "'");
381 return (f(ar[0],ar[1],ar[2]));
387 return (pairmap (function (a,b) { return (a); }, p));
393 return (tripletmap (function (a,b,c) { return (a); }, p));
399 return (tripletmap (function (a,b,c) { return (b); }, p));
405 return (tripletmap (function (a,b,c) { return (c); }, p));
408 function populate_goalarray(menv)
410 debug("metasenv.length = " + menv.length);
411 if (menv.length == 0) {
417 goalarray = new Array();
418 metalist = listnil();
419 var tmp_goallist = "";
420 for (i = 0; i < menv.length; i++) {
421 metano = menv[i].getAttribute("number");
422 metaname = menv[i].childNodes[0].childNodes[0].data;
423 goal = menv[i].childNodes[1].childNodes[0].data;
424 debug ("found meta n. " + metano);
425 debug ("found goal\nBEGIN" + goal + "\nEND");
426 goalarray[metano] = goal;
427 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
428 metalist = listcons(metano,metalist);
429 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
431 goals.innerHTML = tmp_goallist;
432 debug("new metalist is '" + metalist + "'");
433 if (is_nil(metalist)) {
437 switch_goal(listhd(metalist));
442 function switch_goal(meta)
444 if (typeof meta == "undefined") {
445 goalview.innerHTML = "";
448 debug("switch_goal " + meta + "\n" + goalarray[meta]);
449 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
453 // the following is used to avoid escaping unicode, which results in
454 // the server being unable to unescape the string
455 String.prototype.sescape = function() {
461 result = result.replace(patt1,"%25");
462 result = result.replace(patt2,"%3D");
463 result = result.replace(patt3,"%26");
464 result = result.replace(patt4,"%2B");
468 String.prototype.html_to_matita = function()
470 var patt1 = /<br(\/|)>/gi;
473 var patt4 = /</gi;
474 var patt5 = />/gi;
475 var patt6 = / /gi;
477 result = result.replace(patt1,"\n");
478 result = result.replace(patt2,"\005");
479 result = result.replace(patt3,"\006");
480 result = result.replace(patt4,"<");
481 result = result.replace(patt5,">");
482 result = result.replace(patt6," ");
483 return (unescape(result));
486 String.prototype.matita_to_html = function()
490 var patt3 = /\005/gi;
491 var patt4 = /\006/gi;
493 result = result.replace(patt1,"<");
494 result = result.replace(patt2,">");
495 result = result.replace(patt3,"<");
496 result = result.replace(patt4,">");
497 return (unescape(result));
500 function is_defined(x)
502 return (typeof x != "undefined");
505 /* servicename: name of the service being called
506 * reqbody: text of the request
507 * processResponse: processes the server response
508 * (takes the response text in input, undefined in case of error)
510 function callServer(servicename,processResponse,reqbody)
514 if (window.XMLHttpRequest)
516 req = new XMLHttpRequest();
518 else if (window.ActiveXObject)
521 req = new ActiveXObject("Msxml2.XMLHTTP");
525 req = new ActiveXObject("Microsoft.XMLHTTP");
529 req.onreadystatechange = function()
537 stxt = req.statusText;
540 debug(req.responseText);
541 if (window.DOMParser) {
542 parser=new DOMParser();
543 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
545 else // Internet Explorer
547 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
548 xmlDoc.async="false";
549 xmlDoc.loadXML(req.responseText);
551 processResponse(xmlDoc);
557 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
558 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
567 function advOneStep(xml) {
568 var parsed = xml.getElementsByTagName("parsed")[0];
569 var ambiguity = xml.getElementsByTagName("ambiguity")[0];
570 var disamberr = xml.getElementsByTagName("disamberror")[0];
571 if (is_defined(parsed)) {
572 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
573 var len = parseInt(parsed.getAttribute("length"));
574 // len0 = unlocked.innerHTML.length;
575 var unescaped = unlocked.innerHTML.html_to_matita();
576 var parsedtxt = parsed.childNodes[0].wholeText;
577 //parsedtxt = unescaped.substr(0,len);
578 var unparsedtxt = unescaped.substr(len);
579 lockedbackup += parsedtxt;
580 locked.innerHTML = lockedbackup;
581 unlocked.innerHTML = unparsedtxt.matita_to_html();
582 // len1 = unlocked.innerHTML.length;
583 // len2 = len0 - len1;
584 var len2 = parsedtxt.length;
585 metasenv = xml.getElementsByTagName("meta");
586 statements = listcons(len2,statements);
587 unlocked.scrollIntoView(true);
590 else if (is_defined(ambiguity)) {
591 var start = parseInt(ambiguity.getAttribute("start"));
592 var stop = parseInt(ambiguity.getAttribute("stop"));
593 var choices = xml.getElementsByTagName("choice");
595 matita.ambiguityStart = start;
596 matita.ambiguityStop = stop;
597 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
598 matita.interpretations = [];
600 var unlockedtxt = unlocked.innerHTML.html_to_matita();
601 var pre = unlockedtxt.substring(0,start).matita_to_html();
602 var mid = unlockedtxt.substring(start,stop).matita_to_html();
603 var post = unlockedtxt.substring(stop).matita_to_html();
604 unlocked.innerHTML = pre +
605 "<span class=\"error\" title=\"disambiguation error\">" +
606 mid + "</span>" + post;
608 var title = "<H3>Ambiguous input</H3>";
609 disambcell.innerHTML = title;
610 for (i = 0;i < choices.length;i++) {
611 matita.interpretations[i] = new Object();
613 var href = choices[i].getAttribute("href");
614 var title = choices[i].getAttribute("title");
615 var desc = choices[i].childNodes[0].nodeValue;
617 matita.interpretations[i].href = href;
618 matita.interpretations[i].title = title;
619 matita.interpretations[i].desc = desc;
621 var choice = document.createElement("input");
622 choice.setAttribute("type","radio");
623 choice.setAttribute("name","interpr");
624 choice.setAttribute("href",href);
625 choice.setAttribute("title",title);
626 if (i == 0) choice.setAttribute("checked","");
628 disambcell.appendChild(choice);
629 disambcell.appendChild(document.createTextNode(desc));
630 disambcell.appendChild(document.createElement("br"));
633 var okbutton = document.createElement("input");
634 okbutton.setAttribute("type","button");
635 okbutton.setAttribute("value","OK");
636 okbutton.setAttribute("onclick","do_disambiguate()");
637 var cancelbutton = document.createElement("input");
638 cancelbutton.setAttribute("type","button");
639 cancelbutton.setAttribute("value","Cancel");
640 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
642 disambcell.appendChild(okbutton);
643 disambcell.appendChild(cancelbutton);
645 matita.disambMode = true;
649 else if (is_defined(disamberr)) {
650 // must be fixed in a daemon: it makes sense to return a
651 // disambiguation error with no choices
652 if (disamberr.childNodes.length > 0) {
653 set_cps(disamberr.childNodes);
654 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
655 matita.disambMode = true;
661 var error = xml.getElementsByTagName("error")[0];
662 unlocked.innerHTML = error.childNodes[0].wholeText;
663 // debug(xml.childNodes[0].nodeValue);
669 function advanceForm1()
671 processor = function(xml) {
673 if (is_defined(xml)) {
675 populate_goalarray(metasenv);
678 debug("advance failed");
688 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
692 // get or set <choicepoint>'s (in case of disamb error)
694 return matita.choicepoints
697 function set_cps(cps) {
698 matita.choicepoints = cps;
701 // get radio buttons for <choice>'s in a given cp
702 function get_choice_opts(i) {
704 var choices = get_cps()[i].childNodes;
705 for (var j = 0;j < choices.length;j++) {
706 var href = choices[j].getAttribute("href");
707 var title = choices[j].getAttribute("title");
709 if (is_defined(title) && title != null) {
711 } else if (is_defined(href) && href != null) {
717 res[j] = document.createElement("input");
718 res[j].setAttribute("type","radio");
719 res[j].setAttribute("name","choice");
720 res[j].setAttribute("choicepointno",i);
721 res[j].setAttribute("choiceno",j);
722 res[j].setAttribute("href",href);
723 res[j].setAttribute("title",title);
724 if (desc != null) res[j].setAttribute("desc",desc);
726 if (j == 0) res[j].setAttribute("checked","");
731 // get radio buttons for <failure>'s in a given choice
732 function get_failure_opts(i,j) {
734 var failures = get_cps()[i].childNodes[j].childNodes;
735 for (var k = 0;k < failures.length;k++) {
736 var start = failures[k].getAttribute("start");
737 var stop = failures[k].getAttribute("stop");
738 var title = failures[k].getAttribute("title");
740 res[k] = document.createElement("input");
741 res[k].setAttribute("type","radio");
742 res[k].setAttribute("name","failure");
743 res[k].setAttribute("choicepointno",i);
744 res[k].setAttribute("choiceno",j);
745 res[k].setAttribute("failureno",k);
746 res[k].setAttribute("start",start);
747 res[k].setAttribute("stop",stop);
748 res[k].setAttribute("title",title);
750 if (k == 0) res[k].setAttribute("checked","");
755 function next_cp(curcp) {
756 var cp = get_cps()[curcp];
757 var start = parseInt(cp.getAttribute("start"));
758 var stop = parseInt(cp.getAttribute("stop"));
760 matita.errorStart = start;
761 matita.errorStop = stop;
762 // matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
764 var unlockedtxt = matita.unlockedbackup;
765 var pre = unlockedtxt.substring(0,start).matita_to_html();
766 var mid = unlockedtxt.substring(start,stop).matita_to_html();
767 var post = unlockedtxt.substring(stop).matita_to_html();
768 unlocked.innerHTML = pre +
769 "<span class=\"error\" title=\"error location\">" +
770 mid + "</span>" + post;
772 var title = "<H3>Error diagnostics</H3>";
773 disambcell.innerHTML = title;
774 var choices = get_choice_opts(curcp);
775 for (var i = 0;i < choices.length;i++) {
776 disambcell.appendChild(choices[i]);
777 var desc = choices[i].getAttribute("desc");
778 if (!is_defined(desc) || desc == null) {
779 desc = "Interpretation " + i;
781 disambcell.appendChild(document.createTextNode(desc));
782 disambcell.appendChild(document.createElement("br"));
785 // update index of the next choicepoint
786 new_curcp = (curcp + 1) % get_cps().length;
788 var okbutton = document.createElement("input");
789 okbutton.setAttribute("type","button");
790 okbutton.setAttribute("value","OK");
791 okbutton.setAttribute("onclick","show_failures()");
792 var cancelbutton = document.createElement("input");
793 cancelbutton.setAttribute("type","button");
794 cancelbutton.setAttribute("value","Close");
795 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
796 var tryagainbutton = document.createElement("input");
797 tryagainbutton.setAttribute("type","button");
799 tryagainbutton.setAttribute("value","None of the above");
801 tryagainbutton.setAttribute("value","Restart");
803 tryagainbutton.setAttribute("onclick","next_cp(" + new_curcp + ")");
805 disambcell.appendChild(okbutton);
806 disambcell.appendChild(tryagainbutton);
807 disambcell.appendChild(cancelbutton);
811 //matita.disambMode = true;
816 function show_failures() {
818 var choice = document.getElementsByName("choice")[get_checked_index("choice")];
819 var cpno = parseInt(choice.getAttribute("choicepointno"));
820 var choiceno = parseInt(choice.getAttribute("choiceno"));
821 var choicedesc = choice.getAttribute("desc");
823 var title = "<H3>Error diagnostics</H3>";
825 if (is_defined(choicedesc) && choicedesc != null) {
826 subtitle = "<p>Errors at node " + cpno + " = " + choicedesc + "</p>";
828 subtitle = "<p>Global errors:</p>";
831 disambcell.innerHTML = title + subtitle;
832 var failures = get_failure_opts(cpno,choiceno);
833 for (var i = 0;i < failures.length;i++) {
834 disambcell.appendChild(failures[i]);
835 disambcell.appendChild(document.createTextNode(failures[i].getAttribute("title")));
836 disambcell.appendChild(document.createElement("br"));
839 var okbutton = document.createElement("input");
840 okbutton.setAttribute("type","button");
841 okbutton.setAttribute("value","Show error loc");
842 okbutton.setAttribute("onclick","show_err()");
843 var cancelbutton = document.createElement("input");
844 cancelbutton.setAttribute("type","button");
845 cancelbutton.setAttribute("value","Close");
846 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
847 var backbutton = document.createElement("input");
848 backbutton.setAttribute("type","button");
849 backbutton.setAttribute("value","<< Go back");
850 backbutton.setAttribute("onclick","next_cp(" + cpno + ")");
852 disambcell.appendChild(backbutton);
853 disambcell.appendChild(okbutton);
854 disambcell.appendChild(cancelbutton);
858 function show_err() {
859 var radios = document.getElementsByName("failure");
860 for (i = 0; i < radios.length; i++) {
861 if (radios[i].checked) {
862 var start = radios[i].getAttribute("start");
863 var stop = radios[i].getAttribute("stop");
864 var title = radios[i].getAttribute("title");
865 var unlockedtxt = matita.unlockedbackup;
866 var pre = unlockedtxt.substring(0,start).matita_to_html();
867 var mid = unlockedtxt.substring(start,stop).matita_to_html();
868 var post = unlockedtxt.substring(stop).matita_to_html();
869 unlocked.innerHTML = pre +
870 "<span class=\"error\" title=\"Disambiguation failure\">" +
871 mid + "</span>" + post;
877 function gotoBottom()
879 processor = function(xml) {
880 if (is_defined(xml)) {
881 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
882 var parsed = xml.getElementsByTagName("parsed");
883 var localized = xml.getElementsByTagName("localized")[0];
884 var ambiguity = xml.getElementsByTagName("ambiguity")[0];
885 var generic_err = xml.getElementsByTagName("error")[0];
886 var disamberr = xml.getElementsByTagName("disamberror")[0];
887 for (var i = 0;i < parsed.length; i++) {
888 var len = parsed[i].getAttribute("length");
889 // len0 = unlocked.innerHTML.length;
890 var unescaped = unlocked.innerHTML.html_to_matita();
891 // the browser may decide to split textnodes: use wholeText!
892 var parsedtxt = parsed[i].childNodes[0].wholeText;
893 //parsedtxt = unescaped.substr(0,len);
894 var unparsedtxt = unescaped.substr(len);
895 lockedbackup += parsedtxt;
896 locked.innerHTML = lockedbackup; //.matita_to_html();
897 unlocked.innerHTML = unparsedtxt.matita_to_html();
898 // len1 = unlocked.innerHTML.length;
899 var len2 = parsedtxt.length;
900 statements = listcons(len2,statements);
902 unlocked.scrollIntoView(true);
903 metasenv = xml.getElementsByTagName("meta");
905 populate_goalarray(metasenv);
907 if (is_defined(ambiguity)) {
908 var start = parseInt(ambiguity.getAttribute("start"));
909 var stop = parseInt(ambiguity.getAttribute("stop"));
910 var choices = xml.getElementsByTagName("choice");
912 matita.ambiguityStart = start;
913 matita.ambiguityStop = stop;
914 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
915 matita.interpretations = [];
917 var unlockedtxt = unlocked.innerHTML.html_to_matita();
918 var pre = unlockedtxt.substring(0,start).matita_to_html();
919 var mid = unlockedtxt.substring(start,stop).matita_to_html();
920 var post = unlockedtxt.substring(stop).matita_to_html();
921 unlocked.innerHTML = pre +
922 "<span class=\"error\" title=\"disambiguation error\">" +
923 mid + "</span>" + post;
925 var title = "<H3>Ambiguous input</H3>";
926 disambcell.innerHTML = title;
927 for (i = 0;i < choices.length;i++) {
928 matita.interpretations[i] = new Object();
930 var href = choices[i].getAttribute("href");
931 var title = choices[i].getAttribute("title");
932 var desc = choices[i].childNodes[0].nodeValue;
934 matita.interpretations[i].href = href;
935 matita.interpretations[i].title = title;
936 matita.interpretations[i].desc = desc;
938 var choice = document.createElement("input");
939 choice.setAttribute("type","radio");
940 choice.setAttribute("name","interpr");
941 choice.setAttribute("href",href);
942 choice.setAttribute("title",title);
943 if (i == 0) choice.setAttribute("checked","");
945 disambcell.appendChild(choice);
946 disambcell.appendChild(document.createTextNode(desc));
947 disambcell.appendChild(document.createElement("br"));
950 var okbutton = document.createElement("input");
951 okbutton.setAttribute("type","button");
952 okbutton.setAttribute("value","OK");
953 okbutton.setAttribute("onclick","do_disambiguate()");
954 var cancelbutton = document.createElement("input");
955 cancelbutton.setAttribute("type","button");
956 cancelbutton.setAttribute("value","Cancel");
957 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
959 disambcell.appendChild(okbutton);
960 disambcell.appendChild(cancelbutton);
962 matita.disambMode = true;
965 else if (is_defined(disamberr)) {
966 // must be fixed in a daemon: it makes sense to return a
967 // disambiguation error with no choices
968 if (disamberr.childNodes.length > 0) {
969 set_cps(disamberr.childNodes);
970 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
971 matita.disambMode = true;
976 else if (is_defined(localized)) {
977 unlocked.innerHTML = localized.childNodes[0].wholeText;
979 else if (is_defined(generic_err)) {
980 debug("Unmanaged error:\n" ^ generic_err.childNodes[0].wholeText);
983 debug("goto bottom failed");
988 callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
994 processor = function(xml) {
995 if (is_defined(xml)) {
996 if (xml.childNodes[0].textContent != "ok") {
997 debug("goto top failed");
1000 statements = listnil();
1002 lockedlen = locked.innerHTML.length - statementlen;
1003 statement = locked.innerHTML.substr(lockedlen, statementlen);
1004 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
1005 unlocked.innerHTML = statement + unlocked.innerHTML;
1007 unlocked.innerHTML = lockedbackup + unlocked.innerHTML;
1009 locked.innerHTML = lockedbackup;
1012 unlocked.scrollIntoView(true);
1014 debug("goto top failed");
1019 callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1023 function gotoPos(offset)
1025 if (!is_defined(offset)) {
1026 offset = getCursorPos();
1028 processor = function(xml) {
1029 if (is_defined(xml)) {
1032 parsed = xml.getElementsByTagName("parsed")[0];
1033 len = parseInt(parsed.getAttribute("length"));
1034 // len0 = unlocked.innerHTML.length;
1035 unescaped = unlocked.innerHTML.html_to_matita();
1036 parsedtxt = parsed.childNodes[0].wholeText;
1037 //parsedtxt = unescaped.substr(0,len);
1038 unparsedtxt = unescaped.substr(len);
1039 lockedbackup += parsedtxt;
1040 locked.innerHTML = lockedbackup; //.matita_to_html();
1041 unlocked.innerHTML = unparsedtxt.matita_to_html();
1042 // len1 = unlocked.innerHTML.length;
1043 len2 = parsedtxt.length;
1044 metasenv = xml.getElementsByTagName("meta");
1045 // populate_goalarray(metasenv);
1046 statements = listcons(len2,statements);
1047 unlocked.scrollIntoView(true);
1048 // la populate non andrebbe fatta a ogni passo
1050 var len = advOneStep(xml);
1051 if (offset <= len) {
1053 populate_goalarray(metasenv);
1056 gotoPos(offset - len);
1060 populate_goalarray(metasenv);
1069 unlocked.scrollIntoView(true);
1070 populate_goalarray(metasenv);
1075 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1080 processor = function(xml) {
1081 if (typeof xml != "undefined") {
1082 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
1083 statementlen = parseInt(listhd(statements));
1084 statements = listtl(statements);
1086 lockedlen = locked.innerHTML.length - statementlen;
1087 statement = locked.innerHTML.substr(lockedlen, statementlen);
1088 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
1089 unlocked.innerHTML = statement + unlocked.innerHTML;
1091 lockedlen = lockedbackup.length - statementlen;
1092 statement = lockedbackup.substr(lockedlen, statementlen);
1093 lockedbackup = lockedbackup.substr(0,lockedlen);
1094 locked.innerHTML = lockedbackup;
1095 unlocked.innerHTML = statement + unlocked.innerHTML;
1096 metasenv = xml.getElementsByTagName("meta");
1098 populate_goalarray(metasenv);
1099 unlocked.scrollIntoView(true);
1101 debug("retract failed");
1106 callServer("retract",processor);
1111 processor = function(xml)
1113 if (is_defined(xml)) {
1115 locked.innerHTML = lockedbackup;
1116 unlocked.innerHTML = xml.documentElement.wholeText;
1118 debug("file open failed");
1121 callServer("open",processor,"file=" + escape(filename.value));
1124 function retrieveFile(thefile)
1126 processor = function(xml)
1128 if (is_defined(xml)) {
1129 changeFile(thefile);
1130 matita.disambMode = false;
1131 matita.proofMode = false;
1134 locked.innerHTML = lockedbackup;
1135 // code originally used in google chrome (problems with mozilla)
1136 // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
1137 // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
1138 debug(xml.childNodes[0].textContent);
1139 if (document.all) { // IE
1140 unlocked.innerHTML = xml.childNodes[0].text;
1142 unlocked.innerHTML = xml.childNodes[0].textContent;
1147 debug("file open failed");
1150 abortDialog("dialogBox");
1151 callServer("open",processor,"file=" + escape(thefile));
1154 function showLibrary(title,callback,reloadDialog)
1157 dialogBox.reload = reloadDialog;
1159 if (window.XMLHttpRequest)
1161 req = new XMLHttpRequest();
1163 else if (window.ActiveXObject)
1166 req = new ActiveXObject("Msxml2.XMLHTTP");
1170 req = new ActiveXObject("Microsoft.XMLHTTP");
1174 req.onreadystatechange = function()
1177 rs = req.readyState;
1182 stxt = req.statusText;
1185 debug(req.responseText);
1186 showDialog("<H2>" + title + "</H2>",req.responseText, callback);
1190 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
1191 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
1196 function uploadDialog()
1198 uploadBox.style.display = "block";
1203 var file = document.getElementById("uploadFilename").files[0];
1205 // var filecontent = file.getAsText("UTF-8");
1206 // locked.innerHTML = lockedbackup;
1207 // unlocked.innerHTML = filecontent;
1208 // uploadBox.style.display = "none";
1211 var reader = new FileReader();
1212 reader.onerror = function (evt) {
1213 debug("file open failed");
1215 reader.onload = function (evt) {
1217 locked.innerHTML = lockedbackup;
1218 unlocked.innerHTML = "";
1219 unlocked.appendChild(document.createTextNode(evt.target.result));
1220 uploadBox.style.display = "none";
1222 try { reader.readAsText(file, "UTF-8"); }
1223 catch (err) { /* nothing to do */ };
1224 uploadBox.style.display = "none";
1228 function openDialog()
1230 callback = function (fname) { retrieveFile(fname); };
1231 showLibrary("Open file", callback, openDialog);
1234 function saveDialog()
1236 callback = function (fname) {
1237 abortDialog("dialogBox");
1239 (locked.innerHTML.html_to_matita()).sescape(),
1240 (unlocked.innerHTML.html_to_matita()).sescape(),
1243 showLibrary("Save file as", callback, saveDialog);
1246 function newDialog()
1248 callback = function (fname) {
1249 abortDialog("dialogBox");
1250 saveFile(fname,"","",false,newDialog,true);
1252 showLibrary("Create new file", callback, newDialog);
1256 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
1258 if (!is_defined(reloadFile)) { reloadFile = true };
1259 if (!is_defined(fname)) {
1260 fname = current_fname;
1261 lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
1262 unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
1264 // when force is true, reloadDialog is not needed
1266 processor = function(xml) {
1267 if (is_defined(xml)) {
1268 if (xml.childNodes[0].textContent != "ok") {
1269 if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
1270 saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
1276 debug("file saved!");
1277 if (reloadFile) { retrieveFile(fname); }
1280 debug("save file failed");
1284 if (is_defined(fname)) {
1286 callServer("save",processor,"file=" + escape(fname) +
1287 "&locked=" + lockedtxt +
1288 "&unlocked=" + unlockedtxt +
1291 else { debug("no file selected"); }
1294 function createDir() {
1295 abortDialog("dialogBox");
1296 dirname = prompt("New directory name:\ncic:/matita/","newdir");
1297 if (dirname != null) {
1298 processor = function(xml) {
1299 if (is_defined(xml)) {
1300 if (xml.childNodes[0].textContent != "ok") {
1301 alert("An error occurred :-(");
1304 alert("An error occurred :-(");
1309 callServer("save",processor,"file=" + escape(dirname) +
1310 "&locked=&unlocked=&force=false&dir=true");
1316 function commitAll()
1318 processor = function(xml) {
1319 if (is_defined(xml)) {
1320 debug(xml.getElementsByTagName("details")[0].textContent);
1321 alert("Commit executed: see details in the log.\n\n" +
1322 "NOTICE: this message does NOT imply (yet) that the commit was successful.");
1324 alert("Commit failed!\n(maybe you don't have permissionis to commit?)");
1329 callServer("commit",processor);
1332 function updateAll()
1334 processor = function(xml) {
1335 if (is_defined(xml)) {
1336 alert("Update executed.\n\n" +
1338 xml.getElementsByTagName("details")[0].textContent);
1340 alert("Update failed!\n(maybe you don't have permissions to update?)");
1345 callServer("update",processor);
1350 function hideSequent() {
1351 matita.proofMode = false;
1355 function showSequent() {
1356 matita.proofMode = true;
1360 function showDialog(title,content,callback) {
1361 dialogTitle.innerHTML = title;
1362 dialogContent.innerHTML = content;
1363 dialogBox.callback = callback;
1365 //Get the screen height and width
1366 var maskHeight = $(document).height();
1367 var maskWidth = $(window).width();
1369 //Set heigth and width to mask to fill up the whole screen
1370 $('#mask').css({'width':maskWidth,'height':maskHeight});
1373 $('#mask').fadeIn(100);
1374 $('#mask').fadeTo(200,0.8);
1376 //Get the window height and width
1377 var winH = $(window).height();
1378 var winW = $(window).width();
1380 //Set the popup window to center
1381 $('#dialogBox').css('top', winH/2-$('#dialogBox').height()/2);
1382 $('#dialogBox').css('left', winW/2-$('#dialogBox').width()/2);
1385 $('#dialogBox').fadeIn(200);
1387 dialogBox.style.display = "block";
1390 function abortDialog(dialog) {
1391 document.getElementById(dialog).style.display = "none";
1395 function removeElement(id) {
1396 var element = document.getElementById(id);
1397 element.parentNode.removeChild(element);
1403 function getCursorPos() {
1405 if (window.getSelection) {
1406 var selObj = window.getSelection();
1407 savedRange = selObj.getRangeAt(0);
1408 savedsc = savedRange.startContainer;
1409 savedso = savedRange.startOffset;
1410 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
1411 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
1412 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
1415 else if (document.selection) {
1416 savedRange = document.selection.createRange();
1417 var bookmark = savedRange.getBookmark();
1418 /* FIXME the following works wrong when the document is longer than 65535 chars */
1419 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
1424 function findNode(list, node, acc) {
1425 for (var i = 0; i < list.length; i++) {
1426 if (list[i] == node) {
1427 // debug("success " + i);
1430 if (list[i].hasChildNodes()) {
1432 // debug("recursion on node " + i);
1433 return (findNode(list[i].childNodes,node,acc))
1435 catch (e) { /* debug("recursion failed"); */ }
1437 sandbox = document.getElementById("sandbox");
1438 dup = list[i].cloneNode(true);
1439 sandbox.appendChild(dup);
1440 // debug("fail " + i + ": " + sandbox.innerHTML);
1441 acc += sandbox.innerHTML.html_to_matita().length;
1442 sandbox.removeChild(dup);
1448 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
1451 function get_checked_index(name) {
1452 var radios = document.getElementsByName(name);
1453 for (i = 0; i < radios.length; i++) {
1454 if (radios[i].checked) {
1461 function cancel_disambiguate() {
1462 matita.disambMode = false;
1464 // enable_toparea();
1465 // enable_editing();
1466 strip_tags("span","error");
1470 function do_disambiguate() {
1471 var i = get_checked_index("interpr");
1473 var pre = matita.unlockedbackup
1474 .substring(0,matita.ambiguityStart).matita_to_html();
1475 var mid = matita.unlockedbackup
1476 .substring(matita.ambiguityStart,matita.ambiguityStop)
1478 var post = matita.unlockedbackup
1479 .substring(matita.ambiguityStop).matita_to_html();
1481 var href = matita.interpretations[i].href;
1482 var title = matita.interpretations[i].title;
1484 if (is_defined(title)) {
1485 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1487 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1490 unlocked.innerHTML = pre + mid + post;
1492 matita.disambMode = false;
1499 function do_showerror() {
1500 var i = get_checked_index("choice");
1502 var pre = matita.unlockedbackup
1503 .substring(0,matita.ambiguityStart).matita_to_html();
1504 var mid = matita.unlockedbackup
1505 .substring(matita.ambiguityStart,matita.ambiguityStop)
1507 var post = matita.unlockedbackup
1508 .substring(matita.ambiguityStop).matita_to_html();
1510 var href = matita.interpretations[i].href;
1511 var title = matita.interpretations[i].title;
1513 if (is_defined(title)) {
1514 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1516 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1519 unlocked.innerHTML = pre + mid + post;
1524 function readCookie(name) {
1525 var nameEQ = name + "=";
1526 var ca = document.cookie.split(';');
1527 for(var i=0;i < ca.length;i++) {
1529 while (c.charAt(0)==' ') c = c.substring(1,c.length);
1530 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
1535 function delete_cookie ( cookie_name )
1537 var cookie_date = new Date(); // current date & time
1538 cookie_date.setTime ( cookie_date.getTime() - 1 );
1539 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
1542 function delete_session()
1544 delete_cookie("session");
1547 function disable_toparea() {
1548 var offset = $('#toparea').offset();
1549 $('#whitemask').css('top',offset.top);
1550 $('#whitemask').css('left',offset.left);
1551 $('#whitemask').css('width',$('#toparea').outerWidth() + "px");
1552 $('#whitemask').css('height',$('#toparea').outerHeight() + "px");
1553 $('#whitemask').fadeTo('fast',0.7);
1556 function enable_toparea() {
1557 $('#whitemask').hide();
1560 function disable_editing() {
1561 unlocked.contentEditable = false;
1564 function enable_editing() {
1565 unlocked.contentEditable = true;
1570 // advanceButton.disabled = true;
1571 // retractButton.disabled = true;
1572 // cursorButton.disabled = true;
1573 // bottomButton.disabled = true;
1580 // advanceButton.disabled = false;
1581 // retractButton.disabled = false;
1582 // cursorButton.disabled = false;
1583 // bottomButton.disabled = false;
1584 if (!matita.disambMode) {