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);
95 function go_hyperlink(atag) {
96 var uri = atag.attr("href");
97 var mybaseuri = uri.substring(0,uri.lastIndexOf('/'));
98 var id = uri.substring(uri.lastIndexOf('/')+1,uri.lastIndexOf('.'));
99 // 12 = position of the second '/' in mybaseuri (to strip off "cic:/matita/")
100 var thefile = mybaseuri.substring(12) + ".ma";
101 if (uri != "cic:/fakeuri.def(1)") {
102 if (mybaseuri == baseuri) {
103 $('#'+id)[0].scrollIntoView(true);
105 go_external_hyperlink(mybaseuri,thefile,id);
111 function init_hyperlinks() {
112 $("#unlocked a").click(function () { return go_hyperlink($(this))});
113 $("#locked a").click(function () { return go_hyperlink($(this))});
114 $("#goalview a").click(function () { return go_hyperlink($(this))});
117 function init_autotraces() {
118 $("#unlocked .autotactic").tooltip({
121 bodyHandler: function() {
122 return (trace_of($(this)[0]));
125 $("#locked .autotactic").tooltip({
128 bodyHandler: function() {
129 return (trace_of($(this)[0]));
134 function trace_of(node) {
135 return text_of_html(filterByClass(node.childNodes,"autotrace")[0]);
138 function changeFile(name) {
139 current_fname = name;
140 matitaTitle.innerHTML = "Matita - cic:/matita/" + name;
141 baseuri = "cic:/matita/" + name.substring(0,name.lastIndexOf('.'));
144 function init_keyboard(target)
146 if (target.addEventListener)
148 // target.addEventListener("keydown",keydown,false);
149 target.addEventListener("keypress",keypress,false);
150 // target.addEventListener("keyup",keyup,false);
151 // target.addEventListener("textInput",textinput,false);
153 else if (target.attachEvent)
155 // target.attachEvent("onkeydown", keydown);
156 target.attachEvent("onkeypress", keypress);
157 // target.attachEvent("onkeyup", keyup);
158 // target.attachEvent("ontextInput", textinput);
162 // target.onkeydown= keydown;
163 target.onkeypress= keypress;
164 // target.onkeyup= keyup;
165 // target.ontextinput= textinput; // probably doesn't work
172 if (n == null) return 'undefined';
174 if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
175 while (s.length < 9) s+= ' ';
179 function string_of_key(n)
181 if (n == null) return 'undefined';
182 return String.fromCharCode(n);
185 function pressmesg(w,e)
187 debug(w + ' keyCode=' + keyval(e.keyCode) +
188 ' which=' + keyval(e.which) +
189 ' charCode=' + keyval(e.charCode) +
190 '\n shiftKey='+e.shiftKey
191 + ' ctrlKey='+e.ctrlKey
192 + ' altKey='+e.altKey
193 + ' metaKey='+e.metaKey);
196 function suppressdefault(e,flag)
200 if (e.preventDefault) e.preventDefault();
201 if (e.stopPropagation) e.stopPropagation();
206 function restoreSelection(r) {
209 if (window.getSelection)//non IE and there is already a selection
211 var s = window.getSelection();
212 if (s.rangeCount > 0)
217 if (document.createRange)//non IE and no selection
219 window.getSelection().addRange(r);
222 if (document.selection)//IE
229 function lookup_tex(texmacro)
231 texmacro = texmacro.substring(1);
232 return unescape(macro2utf8[texmacro]);
235 function strip_tags(tagname,classname)
237 var tags = unlocked.getElementsByTagName(tagname);
238 if (is_defined(classname)) {
239 tags = filterByClass(tags,classname);
241 var tlen = tags.length; // preserving the value from removeChild operations
242 for (i = 0; i < tlen; i++) {
243 var children = tags[i].childNodes;
244 for (j = 0; j < children.length; j++) {
245 tags[i].parentNode.insertBefore(children[j],tags[i]);
248 for (var i = 0; i < tlen; i++) {
249 tags[0].parentNode.removeChild(tags[0]);
253 function strip_interpr() {
257 // alert("strip_interpr ended");
260 function strip_anchors() {
261 strip_tags("INPUT","anchor");
267 pressmesg('keypress',e);
268 var s = string_of_key(e.charCode);
269 strip_tags("span","error");
272 i = unlocked.innerHTML.html_to_matita().lastIndexOf('\\',j);
274 match = unlocked.innerHTML.html_to_matita().substring(i,j);
275 sym = unescape_html(lookup_tex(match));
276 if (sym != "undefined") {
277 if (window.getSelection) { // non IE
278 savedRange.setStart(savedsc,savedso - (j-i));
279 savedRange.deleteContents();
280 savedRange.insertNode(document.createTextNode(sym));
281 savedsc.parentNode.normalize();
282 if (savedRange.collapsed) { // Mozilla
283 savedRange.setEnd(savedsc,savedRange.endOffset + sym.length);
285 savedRange.collapse(false);
287 savedRange.moveStart(i-j);
288 savedRange.text(sym);
289 savedRange.collapse(false);
291 restoreSelection(savedRange);
292 return suppressdefault(e,true);
295 // restoreSelection(0);
296 return suppressdefault(e,false);
299 else return suppressdefault(e,false);
301 return suppressdefault(e,false);
309 // internet explorer (v.9) doesn't work with innerHTML
310 // but google chrome's innerText is, in a sense, "write only"
311 // what should we do?
312 // logarea.innerText = txt + "\n" + logarea.innerText;
313 logtxt = /* logtxt + "\n" +*/ txt;
317 var logWin = window.open( "", "Matita Log",
318 "width=600,height=450,location=no,menubar=no,status=no,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
319 logWin.document.write('<html><head><title>Matita Log' + '</title></head>');
320 logWin.document.write('<body><textarea style="width:100%;height:100%;">' +
321 logtxt + '</textarea></body></html>');
322 logWin.document.close();
328 debug("hd of '" + l + "' = '" + ar[0] + "'");
336 debug("tl of '" + l + "' = '" + tl + "'");
340 function listcons(x,l)
342 debug("cons '" + x + "' on '" + l + "'");
343 return (x + "#" + l);
351 function list_append(l1,l2)
359 function fold_left (f,acc,l)
362 { debug("'" + l + "' is fold end");
365 { debug("'" + l + "' is fold cons");
366 return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
369 function listiter (f,l)
372 { debug("'" + l + "' is nil");
377 debug("'" + l + "' is not nil");
379 listiter(f,listtl(l));
383 function listmap (f,l)
385 debug("listmap on " + l);
387 { debug("returning listnil");
391 { debug("cons f(hd) map(f,tl)");
392 return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
396 var statements = listnil();
399 var metalist = listnil();
401 function pairmap (f,p)
403 debug("pairmap of '" + p + "'");
405 return (f(ar[0],ar[1]));
408 function tripletmap (f,p)
410 debug("tripletmap of '" + p + "'");
412 return (f(ar[0],ar[1],ar[2]));
418 return (pairmap (function (a,b) { return (a); }, p));
424 return (tripletmap (function (a,b,c) { return (a); }, p));
430 return (tripletmap (function (a,b,c) { return (b); }, p));
436 return (tripletmap (function (a,b,c) { return (c); }, p));
439 function populate_goalarray(menv)
441 debug("metasenv.length = " + menv.length);
442 if (menv.length == 0) {
448 goalarray = new Array();
449 metalist = listnil();
450 var tmp_goallist = "";
451 for (i = 0; i < menv.length; i++) {
452 metano = menv[i].getAttribute("number");
453 // item 0 = <metaname>, item 1 = <goal>
454 metaname = menv[i].childNodes[0].childNodes[0].wholeText;
455 goal = menv[i].childNodes[1].childNodes[0].wholeText;
456 debug ("found meta n. " + metano);
457 debug ("found goal\nBEGIN" + goal + "\nEND");
458 goalarray[metano] = goal;
459 tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
460 metalist = listcons(metano,metalist);
461 debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]);
463 goals.innerHTML = tmp_goallist;
464 debug("new metalist is '" + metalist + "'");
465 if (is_nil(metalist)) {
469 switch_goal(listhd(metalist));
474 function switch_goal(meta)
476 if (typeof meta == "undefined") {
477 goalview.innerHTML = "";
480 debug("switch_goal " + meta + "\n" + goalarray[meta]);
481 goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
485 // the following is used to avoid escaping unicode, which results in
486 // the server being unable to unescape the string
487 String.prototype.sescape = function() {
493 result = result.replace(patt1,"%25");
494 result = result.replace(patt2,"%3D");
495 result = result.replace(patt3,"%26");
496 result = result.replace(patt4,"%2B");
500 String.prototype.html_to_matita = function()
502 var patt1 = /<br(\/|)>/gi;
505 var patt4 = /</gi;
506 var patt5 = />/gi;
507 var patt6 = / /gi;
509 result = result.replace(patt1,"\n");
510 result = result.replace(patt2,"\005");
511 result = result.replace(patt3,"\006");
512 result = result.replace(patt4,"<");
513 result = result.replace(patt5,">");
514 result = result.replace(patt6," ");
515 return (unescape(result));
518 String.prototype.matita_to_html = function()
522 var patt3 = /\005/gi;
523 var patt4 = /\006/gi;
525 result = result.replace(patt1,"<");
526 result = result.replace(patt2,">");
527 result = result.replace(patt3,"<");
528 result = result.replace(patt4,">");
529 return (unescape(result));
532 function is_defined(x)
534 return (typeof x != "undefined");
537 /* servicename: name of the service being called
538 * reqbody: text of the request
539 * processResponse: processes the server response
540 * (takes the response text in input, undefined in case of error)
542 function callServer(servicename,processResponse,reqbody)
546 if (window.XMLHttpRequest)
548 req = new XMLHttpRequest();
550 else if (window.ActiveXObject)
553 req = new ActiveXObject("Msxml2.XMLHTTP");
557 req = new ActiveXObject("Microsoft.XMLHTTP");
561 req.onreadystatechange = function()
569 stxt = req.statusText;
572 debug(req.responseText);
573 if (window.DOMParser) {
574 parser=new DOMParser();
575 xmlDoc=parser.parseFromString(req.responseText,"text/xml");
577 else // Internet Explorer
579 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
580 xmlDoc.async="false";
581 xmlDoc.loadXML(req.responseText);
583 processResponse(xmlDoc);
589 req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
590 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
599 function advOneStep(xml) {
600 var parsed = xml.getElementsByTagName("parsed")[0];
601 var ambiguity = xml.getElementsByTagName("ambiguity")[0];
602 var disamberr = xml.getElementsByTagName("disamberror")[0];
603 if (is_defined(parsed)) {
604 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
605 var len = parseInt(parsed.getAttribute("length"));
606 // len0 = unlocked.innerHTML.length;
607 var unescaped = unlocked.innerHTML.html_to_matita();
608 var parsedtxt = parsed.childNodes[0].wholeText;
609 //parsedtxt = unescaped.substr(0,len);
610 var unparsedtxt = unescaped.substr(len);
611 lockedbackup += parsedtxt;
612 locked.innerHTML = lockedbackup;
613 unlocked.innerHTML = unparsedtxt.matita_to_html();
614 // len1 = unlocked.innerHTML.length;
615 // len2 = len0 - len1;
616 var len2 = parsedtxt.length;
617 metasenv = xml.getElementsByTagName("meta");
618 statements = listcons(len2,statements);
619 unlocked.scrollIntoView(true);
622 else if (is_defined(ambiguity)) {
623 var start = parseInt(ambiguity.getAttribute("start"));
624 var stop = parseInt(ambiguity.getAttribute("stop"));
625 var choices = xml.getElementsByTagName("choice");
627 matita.ambiguityStart = start;
628 matita.ambiguityStop = stop;
629 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
630 matita.interpretations = [];
632 var unlockedtxt = unlocked.innerHTML.html_to_matita();
633 var pre = unlockedtxt.substring(0,start).matita_to_html();
634 var mid = unlockedtxt.substring(start,stop).matita_to_html();
635 var post = unlockedtxt.substring(stop).matita_to_html();
636 unlocked.innerHTML = pre +
637 "<span class=\"error\" title=\"disambiguation error\">" +
638 mid + "</span>" + post;
640 var title = "<H3>Ambiguous input</H3>";
641 disambcell.innerHTML = title;
642 for (i = 0;i < choices.length;i++) {
643 matita.interpretations[i] = new Object();
645 var href = choices[i].getAttribute("href");
646 var title = choices[i].getAttribute("title");
647 var desc = choices[i].childNodes[0].nodeValue;
649 matita.interpretations[i].href = href;
650 matita.interpretations[i].title = title;
651 matita.interpretations[i].desc = desc;
653 var choice = document.createElement("input");
654 choice.setAttribute("type","radio");
655 choice.setAttribute("name","interpr");
656 choice.setAttribute("href",href);
657 choice.setAttribute("title",title);
658 if (i == 0) choice.setAttribute("checked","");
660 disambcell.appendChild(choice);
661 disambcell.appendChild(document.createTextNode(desc));
662 disambcell.appendChild(document.createElement("br"));
665 var okbutton = document.createElement("input");
666 okbutton.setAttribute("type","button");
667 okbutton.setAttribute("value","OK");
668 okbutton.setAttribute("onclick","do_disambiguate()");
669 var cancelbutton = document.createElement("input");
670 cancelbutton.setAttribute("type","button");
671 cancelbutton.setAttribute("value","Cancel");
672 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
674 disambcell.appendChild(okbutton);
675 disambcell.appendChild(cancelbutton);
677 matita.disambMode = true;
681 else if (is_defined(disamberr)) {
682 // must be fixed in a daemon: it makes sense to return a
683 // disambiguation error with no choices
684 if (disamberr.childNodes.length > 0) {
685 set_cps(disamberr.childNodes);
686 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
687 matita.disambMode = true;
693 var error = xml.getElementsByTagName("error")[0];
694 unlocked.innerHTML = error.childNodes[0].wholeText;
695 // debug(xml.childNodes[0].nodeValue);
701 function advanceForm1()
703 processor = function(xml) {
705 if (is_defined(xml)) {
707 populate_goalarray(metasenv);
711 debug("advance failed");
719 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
723 // get or set <choicepoint>'s (in case of disamb error)
725 return matita.choicepoints
728 function set_cps(cps) {
729 matita.choicepoints = cps;
732 // get radio buttons for <choice>'s in a given cp
733 function get_choice_opts(i) {
735 var choices = get_cps()[i].childNodes;
736 for (var j = 0;j < choices.length;j++) {
737 var href = choices[j].getAttribute("href");
738 var title = choices[j].getAttribute("title");
740 if (is_defined(title) && title != null) {
742 } else if (is_defined(href) && href != null) {
748 res[j] = document.createElement("input");
749 res[j].setAttribute("type","radio");
750 res[j].setAttribute("name","choice");
751 res[j].setAttribute("choicepointno",i);
752 res[j].setAttribute("choiceno",j);
753 res[j].setAttribute("href",href);
754 res[j].setAttribute("title",title);
755 if (desc != null) res[j].setAttribute("desc",desc);
757 if (j == 0) res[j].setAttribute("checked","");
762 // get radio buttons for <failure>'s in a given choice
763 function get_failure_opts(i,j) {
765 var failures = get_cps()[i].childNodes[j].childNodes;
766 for (var k = 0;k < failures.length;k++) {
767 var start = failures[k].getAttribute("start");
768 var stop = failures[k].getAttribute("stop");
769 var title = failures[k].getAttribute("title");
771 res[k] = document.createElement("input");
772 res[k].setAttribute("type","radio");
773 res[k].setAttribute("name","failure");
774 res[k].setAttribute("choicepointno",i);
775 res[k].setAttribute("choiceno",j);
776 res[k].setAttribute("failureno",k);
777 res[k].setAttribute("start",start);
778 res[k].setAttribute("stop",stop);
779 res[k].setAttribute("title",title);
781 if (k == 0) res[k].setAttribute("checked","");
786 function next_cp(curcp) {
787 var cp = get_cps()[curcp];
788 var start = parseInt(cp.getAttribute("start"));
789 var stop = parseInt(cp.getAttribute("stop"));
791 matita.errorStart = start;
792 matita.errorStop = stop;
793 // matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
795 var unlockedtxt = matita.unlockedbackup;
796 var pre = unlockedtxt.substring(0,start).matita_to_html();
797 var mid = unlockedtxt.substring(start,stop).matita_to_html();
798 var post = unlockedtxt.substring(stop).matita_to_html();
799 unlocked.innerHTML = pre +
800 "<span class=\"error\" title=\"error location\">" +
801 mid + "</span>" + post;
803 var title = "<H3>Error diagnostics</H3>";
804 disambcell.innerHTML = title;
805 var choices = get_choice_opts(curcp);
806 for (var i = 0;i < choices.length;i++) {
807 disambcell.appendChild(choices[i]);
808 var desc = choices[i].getAttribute("desc");
809 if (!is_defined(desc) || desc == null) {
810 desc = "Interpretation " + i;
812 disambcell.appendChild(document.createTextNode(desc));
813 disambcell.appendChild(document.createElement("br"));
816 // update index of the next choicepoint
817 new_curcp = (curcp + 1) % get_cps().length;
819 var okbutton = document.createElement("input");
820 okbutton.setAttribute("type","button");
821 okbutton.setAttribute("value","OK");
822 okbutton.setAttribute("onclick","show_failures()");
823 var cancelbutton = document.createElement("input");
824 cancelbutton.setAttribute("type","button");
825 cancelbutton.setAttribute("value","Close");
826 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
827 var tryagainbutton = document.createElement("input");
828 tryagainbutton.setAttribute("type","button");
830 tryagainbutton.setAttribute("value","Try something else");
832 tryagainbutton.setAttribute("value","Restart");
834 tryagainbutton.setAttribute("onclick","next_cp(" + new_curcp + ")");
836 disambcell.appendChild(okbutton);
837 disambcell.appendChild(tryagainbutton);
838 disambcell.appendChild(cancelbutton);
842 //matita.disambMode = true;
847 function show_failures() {
849 var choice = document.getElementsByName("choice")[get_checked_index("choice")];
850 var cpno = parseInt(choice.getAttribute("choicepointno"));
851 var choiceno = parseInt(choice.getAttribute("choiceno"));
852 var choicedesc = choice.getAttribute("desc");
854 var title = "<H3>Error diagnostics</H3>";
856 if (is_defined(choicedesc) && choicedesc != null) {
857 subtitle = "<p>Errors at node " + cpno + " = " + choicedesc + "</p>";
859 subtitle = "<p>Global errors:</p>";
862 disambcell.innerHTML = title + subtitle;
863 var failures = get_failure_opts(cpno,choiceno);
864 for (var i = 0;i < failures.length;i++) {
865 disambcell.appendChild(failures[i]);
866 disambcell.appendChild(document.createTextNode(failures[i].getAttribute("title")));
867 disambcell.appendChild(document.createElement("br"));
870 var okbutton = document.createElement("input");
871 okbutton.setAttribute("type","button");
872 okbutton.setAttribute("value","Show error loc");
873 okbutton.setAttribute("onclick","show_err()");
874 var cancelbutton = document.createElement("input");
875 cancelbutton.setAttribute("type","button");
876 cancelbutton.setAttribute("value","Close");
877 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
878 var backbutton = document.createElement("input");
879 backbutton.setAttribute("type","button");
880 backbutton.setAttribute("value","<< Back");
881 backbutton.setAttribute("onclick","next_cp(" + cpno + ")");
883 disambcell.appendChild(backbutton);
884 disambcell.appendChild(okbutton);
885 disambcell.appendChild(cancelbutton);
889 function show_err() {
890 var radios = document.getElementsByName("failure");
891 for (i = 0; i < radios.length; i++) {
892 if (radios[i].checked) {
893 var start = radios[i].getAttribute("start");
894 var stop = radios[i].getAttribute("stop");
895 var title = radios[i].getAttribute("title");
896 var unlockedtxt = matita.unlockedbackup;
897 var pre = unlockedtxt.substring(0,start).matita_to_html();
898 var mid = unlockedtxt.substring(start,stop).matita_to_html();
899 var post = unlockedtxt.substring(stop).matita_to_html();
900 unlocked.innerHTML = pre +
901 "<span class=\"error\" title=\"Disambiguation failure\">" +
902 mid + "</span>" + post;
908 function gotoBottom()
910 processor = function(xml) {
911 if (is_defined(xml)) {
912 // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
913 var parsed = xml.getElementsByTagName("parsed");
914 var localized = xml.getElementsByTagName("localized")[0];
915 var ambiguity = xml.getElementsByTagName("ambiguity")[0];
916 var generic_err = xml.getElementsByTagName("error")[0];
917 var disamberr = xml.getElementsByTagName("disamberror")[0];
918 for (var i = 0;i < parsed.length; i++) {
919 var len = parsed[i].getAttribute("length");
920 // len0 = unlocked.innerHTML.length;
921 var unescaped = unlocked.innerHTML.html_to_matita();
922 // the browser may decide to split textnodes: use wholeText!
923 var parsedtxt = parsed[i].childNodes[0].wholeText;
924 //parsedtxt = unescaped.substr(0,len);
925 var unparsedtxt = unescaped.substr(len);
926 lockedbackup += parsedtxt;
927 locked.innerHTML = lockedbackup; //.matita_to_html();
928 unlocked.innerHTML = unparsedtxt.matita_to_html();
929 // len1 = unlocked.innerHTML.length;
930 var len2 = parsedtxt.length;
931 statements = listcons(len2,statements);
933 unlocked.scrollIntoView(true);
934 metasenv = xml.getElementsByTagName("meta");
937 populate_goalarray(metasenv);
939 if (is_defined(ambiguity)) {
940 var start = parseInt(ambiguity.getAttribute("start"));
941 var stop = parseInt(ambiguity.getAttribute("stop"));
942 var choices = xml.getElementsByTagName("choice");
944 matita.ambiguityStart = start;
945 matita.ambiguityStop = stop;
946 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
947 matita.interpretations = [];
949 var unlockedtxt = unlocked.innerHTML.html_to_matita();
950 var pre = unlockedtxt.substring(0,start).matita_to_html();
951 var mid = unlockedtxt.substring(start,stop).matita_to_html();
952 var post = unlockedtxt.substring(stop).matita_to_html();
953 unlocked.innerHTML = pre +
954 "<span class=\"error\" title=\"disambiguation error\">" +
955 mid + "</span>" + post;
957 var title = "<H3>Ambiguous input</H3>";
958 disambcell.innerHTML = title;
959 for (i = 0;i < choices.length;i++) {
960 matita.interpretations[i] = new Object();
962 var href = choices[i].getAttribute("href");
963 var title = choices[i].getAttribute("title");
964 var desc = choices[i].childNodes[0].nodeValue;
966 matita.interpretations[i].href = href;
967 matita.interpretations[i].title = title;
968 matita.interpretations[i].desc = desc;
970 var choice = document.createElement("input");
971 choice.setAttribute("type","radio");
972 choice.setAttribute("name","interpr");
973 choice.setAttribute("href",href);
974 choice.setAttribute("title",title);
975 if (i == 0) choice.setAttribute("checked","");
977 disambcell.appendChild(choice);
978 disambcell.appendChild(document.createTextNode(desc));
979 disambcell.appendChild(document.createElement("br"));
982 var okbutton = document.createElement("input");
983 okbutton.setAttribute("type","button");
984 okbutton.setAttribute("value","OK");
985 okbutton.setAttribute("onclick","do_disambiguate()");
986 var cancelbutton = document.createElement("input");
987 cancelbutton.setAttribute("type","button");
988 cancelbutton.setAttribute("value","Cancel");
989 cancelbutton.setAttribute("onclick","cancel_disambiguate()");
991 disambcell.appendChild(okbutton);
992 disambcell.appendChild(cancelbutton);
994 matita.disambMode = true;
997 else if (is_defined(disamberr)) {
998 // must be fixed in a daemon: it makes sense to return a
999 // disambiguation error with no choices
1000 if (disamberr.childNodes.length > 0) {
1001 set_cps(disamberr.childNodes);
1002 matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
1003 matita.disambMode = true;
1008 else if (is_defined(localized)) {
1009 unlocked.innerHTML = localized.childNodes[0].wholeText;
1011 else if (is_defined(generic_err)) {
1012 debug("Unmanaged error:\n" ^ generic_err.childNodes[0].wholeText);
1015 debug("goto bottom failed");
1020 callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1026 processor = function(xml) {
1027 if (is_defined(xml)) {
1028 if (xml.childNodes[0].textContent != "ok") {
1029 debug("goto top failed");
1032 statements = listnil();
1034 lockedlen = locked.innerHTML.length - statementlen;
1035 statement = locked.innerHTML.substr(lockedlen, statementlen);
1036 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
1037 unlocked.innerHTML = statement + unlocked.innerHTML;
1039 unlocked.innerHTML = lockedbackup + unlocked.innerHTML;
1041 locked.innerHTML = lockedbackup;
1046 unlocked.scrollIntoView(true);
1048 debug("goto top failed");
1053 callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1057 function gotoPos(offset)
1059 if (!is_defined(offset)) {
1060 offset = getCursorPos();
1062 processor = function(xml) {
1063 if (is_defined(xml)) {
1066 parsed = xml.getElementsByTagName("parsed")[0];
1067 len = parseInt(parsed.getAttribute("length"));
1068 // len0 = unlocked.innerHTML.length;
1069 unescaped = unlocked.innerHTML.html_to_matita();
1070 parsedtxt = parsed.childNodes[0].wholeText;
1071 //parsedtxt = unescaped.substr(0,len);
1072 unparsedtxt = unescaped.substr(len);
1073 lockedbackup += parsedtxt;
1074 locked.innerHTML = lockedbackup; //.matita_to_html();
1075 unlocked.innerHTML = unparsedtxt.matita_to_html();
1076 // len1 = unlocked.innerHTML.length;
1077 len2 = parsedtxt.length;
1078 metasenv = xml.getElementsByTagName("meta");
1079 // populate_goalarray(metasenv);
1080 statements = listcons(len2,statements);
1081 unlocked.scrollIntoView(true);
1082 // la populate non andrebbe fatta a ogni passo
1084 var len = advOneStep(xml);
1085 if (offset <= len) {
1088 populate_goalarray(metasenv);
1091 gotoPos(offset - len);
1096 populate_goalarray(metasenv);
1102 unlocked.scrollIntoView(true);
1103 populate_goalarray(metasenv);
1108 callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
1113 processor = function(xml) {
1114 if (typeof xml != "undefined") {
1115 // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
1116 statementlen = parseInt(listhd(statements));
1117 statements = listtl(statements);
1119 lockedlen = locked.innerHTML.length - statementlen;
1120 statement = locked.innerHTML.substr(lockedlen, statementlen);
1121 locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
1122 unlocked.innerHTML = statement + unlocked.innerHTML;
1124 lockedlen = lockedbackup.length - statementlen;
1125 statement = lockedbackup.substr(lockedlen, statementlen);
1126 lockedbackup = lockedbackup.substr(0,lockedlen);
1127 locked.innerHTML = lockedbackup;
1128 unlocked.innerHTML = statement + unlocked.innerHTML;
1129 metasenv = xml.getElementsByTagName("meta");
1130 populate_goalarray(metasenv);
1134 unlocked.scrollIntoView(true);
1136 debug("retract failed");
1141 callServer("retract",processor);
1146 processor = function(xml)
1148 if (is_defined(xml)) {
1150 locked.innerHTML = lockedbackup;
1151 unlocked.innerHTML = xml.documentElement.wholeText;
1154 debug("file open failed");
1157 callServer("open",processor,"file=" + escape(filename.value));
1160 function retrieveFile(thefile)
1162 processor = function(xml)
1164 if (is_defined(xml)) {
1165 changeFile(thefile);
1166 matita.disambMode = false;
1167 matita.proofMode = false;
1170 locked.innerHTML = lockedbackup;
1171 // code originally used in google chrome (problems with mozilla)
1172 // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
1173 // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
1174 debug(xml.childNodes[0].textContent);
1175 if (document.all) { // IE
1176 unlocked.innerHTML = xml.childNodes[0].text;
1178 unlocked.innerHTML = xml.childNodes[0].textContent;
1185 debug("file open failed");
1188 abortDialog("dialogBox");
1189 callServer("open",processor,"file=" + escape(thefile));
1192 function go_external_hyperlink(uri,thefile,id)
1194 var docWin = createDocWin(uri);
1195 processor = function(xml)
1197 if (is_defined(xml)) {
1198 // code originally used in google chrome (problems with mozilla)
1199 // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
1200 // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
1201 // debug(xml.childNodes[0].textContent);
1203 if (document.all) { // IE
1204 doctext = xml.childNodes[0].text;
1206 doctext= xml.childNodes[0].textContent;
1208 showDoc(uri,doctext,id,docWin);
1211 debug("file open failed");
1214 callServer("open",processor,"file=" + escape(thefile) + "&readonly=true");
1217 function createDocWin(uri) {
1218 var title = "Matita Browser - " + uri;
1219 var docWin = window.open( "", "matitabrowser",
1220 "width=800,height=600,location=no,menubar=no,status=no,scrollbars,resizable,screenX=20,screenY=40,left=20,top=40");
1221 docWin.document.write('<html><head><title>' + title + '</title>' +
1222 '<script type="text/javascript" src="jquery.js"></script>' +
1223 '<script type="text/javascript" src="jquery.tooltip.min.js"></script>' +
1224 '<script type="text/javascript" src="matitaweb.js"></script>' +
1225 '<link rel="stylesheet" type="text/css" href="matitaweb.css"/>' +
1226 '<link rel="stylesheet" type="text/css" href="jquery.tooltip.css"/>' +
1228 docWin.document.write('<body><H1>'+ uri + '</H1>' +
1229 '<pre id="locked"></pre></body></html>');
1230 docWin.document.close();
1231 docWin.baseuri = uri;
1235 function showDoc(uri,doctext,id,docWin) {
1236 var outarea = docWin.document.getElementById("locked");
1237 outarea.innerHTML = doctext;
1239 docWin.document.getElementById(id).scrollIntoView(true);
1241 docWin.init_hyperlinks();
1242 docWin.init_autotraces();
1245 function showLibrary(title,callback,reloadDialog)
1248 dialogBox.reload = reloadDialog;
1250 if (window.XMLHttpRequest)
1252 req = new XMLHttpRequest();
1254 else if (window.ActiveXObject)
1257 req = new ActiveXObject("Msxml2.XMLHTTP");
1261 req = new ActiveXObject("Microsoft.XMLHTTP");
1265 req.onreadystatechange = function()
1268 rs = req.readyState;
1273 stxt = req.statusText;
1276 debug(req.responseText);
1277 showDialog("<H2>" + title + "</H2>",req.responseText, callback);
1281 req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
1282 req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
1287 function uploadDialog()
1289 uploadBox.style.display = "block";
1294 var file = document.getElementById("uploadFilename").files[0];
1296 // var filecontent = file.getAsText("UTF-8");
1297 // locked.innerHTML = lockedbackup;
1298 // unlocked.innerHTML = filecontent;
1299 // uploadBox.style.display = "none";
1302 var reader = new FileReader();
1303 reader.onerror = function (evt) {
1304 debug("file open failed");
1306 reader.onload = function (evt) {
1308 locked.innerHTML = lockedbackup
1309 unlocked.innerHTML = "";
1310 unlocked.appendChild(document.createTextNode(evt.target.result));
1311 uploadBox.style.display = "none";
1313 try { reader.readAsText(file, "UTF-8"); }
1314 catch (err) { /* nothing to do */ };
1315 uploadBox.style.display = "none";
1319 function openDialog()
1321 callback = function (fname) { retrieveFile(fname); };
1322 showLibrary("Open file", callback, openDialog);
1325 function saveDialog()
1327 callback = function (fname) {
1328 abortDialog("dialogBox");
1330 (locked.innerHTML.html_to_matita()).sescape(),
1331 (unlocked.innerHTML.html_to_matita()).sescape(),
1334 showLibrary("Save file as", callback, saveDialog);
1337 function newDialog()
1339 callback = function (fname) {
1340 abortDialog("dialogBox");
1341 saveFile(fname,"","",false,newDialog,true);
1343 showLibrary("Create new file", callback, newDialog);
1347 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
1349 if (!is_defined(reloadFile)) { reloadFile = true };
1350 if (!is_defined(fname)) {
1351 fname = current_fname;
1352 lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
1353 unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
1355 // when force is true, reloadDialog is not needed
1357 processor = function(xml) {
1358 if (is_defined(xml)) {
1359 if (xml.childNodes[0].textContent != "ok") {
1360 if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
1361 saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
1367 debug("file saved!");
1368 if (reloadFile) { retrieveFile(fname); }
1371 debug("save file failed");
1375 if (is_defined(fname)) {
1377 callServer("save",processor,"file=" + escape(fname) +
1378 "&locked=" + lockedtxt +
1379 "&unlocked=" + unlockedtxt +
1382 else { debug("no file selected"); }
1385 function createDir() {
1386 abortDialog("dialogBox");
1387 dirname = prompt("New directory name:\ncic:/matita/","newdir");
1388 if (dirname != null) {
1389 processor = function(xml) {
1390 if (is_defined(xml)) {
1391 if (xml.childNodes[0].textContent != "ok") {
1392 alert("An error occurred :-(");
1395 alert("An error occurred :-(");
1400 callServer("save",processor,"file=" + escape(dirname) +
1401 "&locked=&unlocked=&force=false&dir=true");
1407 function commitAll()
1409 processor = function(xml) {
1410 if (is_defined(xml)) {
1411 debug(xml.getElementsByTagName("details")[0].textContent);
1412 alert("Commit executed: see details in the log.\n\n" +
1413 "NOTICE: this message does NOT imply (yet) that the commit was successful.");
1415 alert("Commit failed!");
1420 callServer("commit",processor);
1423 function updateAll()
1425 processor = function(xml) {
1426 if (is_defined(xml)) {
1427 alert("Update executed.\n\n" +
1429 xml.getElementsByTagName("details")[0].textContent);
1431 alert("Update failed!");
1436 callServer("update",processor);
1441 function hideSequent() {
1442 matita.proofMode = false;
1446 function showSequent() {
1447 matita.proofMode = true;
1451 function showDialog(title,content,callback) {
1452 dialogTitle.innerHTML = title;
1453 dialogContent.innerHTML = content;
1454 dialogBox.callback = callback;
1456 //Get the screen height and width
1457 var maskHeight = $(document).height();
1458 var maskWidth = $(window).width();
1460 //Set heigth and width to mask to fill up the whole screen
1461 $('#mask').css({'width':maskWidth,'height':maskHeight});
1464 $('#mask').fadeIn(100);
1465 $('#mask').fadeTo(200,0.8);
1467 //Get the window height and width
1468 var winH = $(window).height();
1469 var winW = $(window).width();
1471 //Set the popup window to center
1472 $('#dialogBox').css('top', winH/2-$('#dialogBox').height()/2);
1473 $('#dialogBox').css('left', winW/2-$('#dialogBox').width()/2);
1476 $('#dialogBox').fadeIn(200);
1478 dialogBox.style.display = "block";
1481 function abortDialog(dialog) {
1482 document.getElementById(dialog).style.display = "none";
1486 function removeElement(id) {
1487 var element = document.getElementById(id);
1488 element.parentNode.removeChild(element);
1494 function getCursorPos() {
1496 if (window.getSelection) {
1497 var selObj = window.getSelection();
1498 savedRange = selObj.getRangeAt(0);
1499 savedsc = savedRange.startContainer;
1500 savedso = savedRange.startOffset;
1501 //cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
1502 cursorPos = findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
1503 /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
1506 else if (document.selection) {
1507 savedRange = document.selection.createRange();
1508 var bookmark = savedRange.getBookmark();
1509 /* FIXME the following works wrong when the document is longer than 65535 chars */
1510 cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
1515 function findNode(list, node, acc) {
1516 for (var i = 0; i < list.length; i++) {
1517 if (list[i] == node) {
1518 // debug("success " + i);
1521 if (list[i].hasChildNodes()) {
1523 // debug("recursion on node " + i);
1524 return (findNode(list[i].childNodes,node,acc))
1526 catch (e) { /* debug("recursion failed"); */ }
1528 sandbox = document.getElementById("sandbox");
1529 dup = list[i].cloneNode(true);
1530 sandbox.appendChild(dup);
1531 // debug("fail " + i + ": " + sandbox.innerHTML);
1532 acc += sandbox.innerHTML.html_to_matita().length;
1533 sandbox.removeChild(dup);
1539 debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
1542 function get_checked_index(name) {
1543 var radios = document.getElementsByName(name);
1544 for (i = 0; i < radios.length; i++) {
1545 if (radios[i].checked) {
1552 function cancel_disambiguate() {
1553 matita.disambMode = false;
1555 // enable_toparea();
1556 // enable_editing();
1557 strip_tags("span","error");
1561 function do_disambiguate() {
1562 var i = get_checked_index("interpr");
1564 var pre = matita.unlockedbackup
1565 .substring(0,matita.ambiguityStart).matita_to_html();
1566 var mid = matita.unlockedbackup
1567 .substring(matita.ambiguityStart,matita.ambiguityStop)
1569 var post = matita.unlockedbackup
1570 .substring(matita.ambiguityStop).matita_to_html();
1572 var href = matita.interpretations[i].href;
1573 var title = matita.interpretations[i].title;
1575 if (is_defined(title)) {
1576 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1578 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1581 unlocked.innerHTML = pre + mid + post;
1583 matita.disambMode = false;
1590 function do_showerror() {
1591 var i = get_checked_index("choice");
1593 var pre = matita.unlockedbackup
1594 .substring(0,matita.ambiguityStart).matita_to_html();
1595 var mid = matita.unlockedbackup
1596 .substring(matita.ambiguityStart,matita.ambiguityStop)
1598 var post = matita.unlockedbackup
1599 .substring(matita.ambiguityStop).matita_to_html();
1601 var href = matita.interpretations[i].href;
1602 var title = matita.interpretations[i].title;
1604 if (is_defined(title)) {
1605 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1607 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1610 unlocked.innerHTML = pre + mid + post;
1615 function readCookie(name) {
1616 var nameEQ = name + "=";
1617 var ca = document.cookie.split(';');
1618 for(var i=0;i < ca.length;i++) {
1620 while (c.charAt(0)==' ') c = c.substring(1,c.length);
1621 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
1626 function delete_cookie ( cookie_name )
1628 var cookie_date = new Date(); // current date & time
1629 cookie_date.setTime ( cookie_date.getTime() - 1 );
1630 document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
1633 function delete_session()
1635 delete_cookie("session");
1638 function disable_toparea() {
1639 var offset = $('#toparea').offset();
1640 $('#whitemask').css('top',offset.top);
1641 $('#whitemask').css('left',offset.left);
1642 $('#whitemask').css('width',$('#toparea').outerWidth() + "px");
1643 $('#whitemask').css('height',$('#toparea').outerHeight() + "px");
1644 $('#whitemask').fadeTo('fast',0.7);
1647 function enable_toparea() {
1648 $('#whitemask').hide();
1651 function disable_editing() {
1652 unlocked.contentEditable = false;
1655 function enable_editing() {
1656 unlocked.contentEditable = true;
1661 // advanceButton.disabled = true;
1662 // retractButton.disabled = true;
1663 // cursorButton.disabled = true;
1664 // bottomButton.disabled = true;
1671 // advanceButton.disabled = false;
1672 // retractButton.disabled = false;
1673 // cursorButton.disabled = false;
1674 // bottomButton.disabled = false;
1675 if (!matita.disambMode) {