]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitaweb.js
Matitaweb:
[helm.git] / matitaB / matita / matitaweb.js
1 var locked;
2 var unlocked;
3 var workarea;
4 var scriptcell;
5 var goalcell;
6 var goals;
7 var goalview;
8 var filename;
9 var logarea;
10 var advanceButton;
11 var retractButton;
12 var cursorButton;
13 var bottomButton;
14 var dialogBox;
15 var dialogTitle;
16 var dialogContent;
17 var metasenv = "";
18 var lockedbackup = "";
19 var matita;
20
21 function text_of_html(h)
22 {
23   if(document.all) {
24      return h.innerText;
25   } else {
26      return h.textContent;
27   }
28 }
29
30 function unescape_html(s)
31 {
32   u = document.getElementById("unescape");
33   u.innerHTML = s;
34   return text_of_html(u)
35 }
36
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]);
42     }
43   }
44   return itemsfound;
45 }
46
47 function initialize()
48 {
49   if (readCookie("session") == null) {
50     window.location = "/login.html"
51   } else {
52     body = document.body;
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");
76
77     matita = new Object();
78     matita.disambMode = matita.proofMode = false;
79
80     // hide sequent view at start
81     initializeLayout();
82     updateSide();
83
84     changeFile("test.ma");
85
86     // initialize keyboard events in the unlocked script
87     init_keyboard(unlocked);
88
89     init_autotraces();
90
91   }
92 }
93
94 function init_autotraces() {
95     $("#unlocked .autotactic").tooltip({ 
96       delay: 0, 
97       showURL: false, 
98       bodyHandler: function() { 
99         return (trace_of($(this)[0])); 
100       }
101     });
102     $("#locked .autotactic").tooltip({ 
103       delay: 0, 
104       showURL: false, 
105       bodyHandler: function() { 
106         return (trace_of($(this)[0]));
107       }
108     });
109 }
110
111 function trace_of(node) {
112   return text_of_html(filterByClass(node.childNodes,"autotrace")[0]);
113 }
114
115 function changeFile(name) {
116     current_fname = name;
117     matitaTitle.innerHTML = "Matita - cic:/matita/" + name;
118 }
119
120 function init_keyboard(target)
121 {
122     if (target.addEventListener)
123     {
124 //       target.addEventListener("keydown",keydown,false);
125        target.addEventListener("keypress",keypress,false);
126 //       target.addEventListener("keyup",keyup,false);
127 //       target.addEventListener("textInput",textinput,false);
128     }
129     else if (target.attachEvent)
130     {
131 //       target.attachEvent("onkeydown", keydown);
132        target.attachEvent("onkeypress", keypress);
133 //       target.attachEvent("onkeyup", keyup);
134 //       target.attachEvent("ontextInput", textinput);
135     }
136     else
137     {
138 //       target.onkeydown= keydown;
139        target.onkeypress= keypress;
140 //       target.onkeyup= keyup;
141 //       target.ontextinput= textinput;   // probably doesn't work
142     }
143  
144 }
145
146 function keyval(n)
147 {
148     if (n == null) return 'undefined';
149     var s= '' + n;
150     if (n >= 32 && n < 127) s+= ' (' + String.fromCharCode(n) + ')';
151     while (s.length < 9) s+= ' ';
152     return s;
153 }
154  
155 function string_of_key(n)
156 {
157     if (n == null) return 'undefined';
158     return String.fromCharCode(n);
159 }
160
161 function pressmesg(w,e)
162 {
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);
170 }
171  
172 function suppressdefault(e,flag)
173 {
174    if (flag)
175    {
176        if (e.preventDefault) e.preventDefault();
177        if (e.stopPropagation) e.stopPropagation();
178    }
179    return !flag;
180 }
181
182 function restoreSelection(r) {
183     unlocked.focus();
184     if (r != null) {
185         if (window.getSelection)//non IE and there is already a selection
186         {
187             var s = window.getSelection();
188             if (s.rangeCount > 0) 
189                 s.removeAllRanges();
190             s.addRange(r);
191         }
192         else 
193             if (document.createRange)//non IE and no selection
194             {
195                 window.getSelection().addRange(r);
196             }
197             else 
198                 if (document.selection)//IE
199                 {
200                     r.select();
201                 }
202     }
203 }
204
205 function lookup_tex(texmacro)
206 {
207   texmacro = texmacro.substring(1);
208   return unescape(macro2utf8[texmacro]);
209 }
210
211 function strip_tags(tagname,classname) 
212 {
213     var tags = unlocked.getElementsByTagName(tagname);
214     if (is_defined(classname)) {
215         tags = filterByClass(tags,classname);
216     }
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]);
221         }
222     }
223     while (tags.length > 0) {
224       tags[0].parentNode.removeChild(tags[0]);
225     }
226 }
227
228 function strip_interpr() {
229         strip_tags("A");
230         alert("strip_interpr ended");
231 }
232  
233 function keypress(e)
234 {
235    if (!e) e= event;
236    pressmesg('keypress',e);
237    var s = string_of_key(e.charCode);
238    strip_tags("span","error");
239    if (s == " ") {
240         j = getCursorPos();
241         i = unlocked.innerHTML.html_to_matita().lastIndexOf('\\',j);
242         if (i >= 0) {
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);
253                 }
254                 savedRange.collapse(false);
255              } else {
256                 savedRange.moveStart(i-j);
257                 savedRange.text(sym);
258                 savedRange.collapse(false);
259              }
260              restoreSelection(savedRange); 
261              return suppressdefault(e,true);
262           }
263           else {
264              // restoreSelection(0); 
265              return suppressdefault(e,false);
266           }
267         }
268         else return suppressdefault(e,false);
269    } else {
270         return suppressdefault(e,false);
271    }
272 }
273  
274 var logtxt = "";
275
276 function debug(txt)
277 {
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;
283 }
284
285 function showLog() {
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(); 
292 }
293
294 function listhd(l)
295 {
296         ar = l.split("#");
297         debug("hd of '" + l + "' = '" + ar[0] + "'");
298         return (ar[0]);
299 }
300
301 function listtl(l)
302 {
303         i = l.indexOf("#");
304         tl = l.substr(i+1);
305         debug("tl of '" + l + "' = '" + tl + "'");
306         return (tl);
307 }
308
309 function listcons(x,l)
310 {
311         debug("cons '" + x + "' on '" + l + "'");
312         return (x + "#" + l);
313 }
314
315 function listnil()
316 {
317         return ("");
318 }
319
320 function is_nil(l)
321 {
322         return (l == "");
323 }
324
325 function fold_left (f,acc,l)
326 {
327         if (is_nil(l))
328            { debug("'" + l + "' is fold end");
329            return (acc); }
330         else
331            { debug("'" + l + "' is fold cons");
332              return(fold_left (f,f(acc,(listhd(l))),listtl(l))); }
333 }
334
335 function listiter (f,l)
336 {
337         if (is_nil(l))
338         { debug("'" + l + "' is nil");
339            return;
340         }
341         else
342         {
343            debug("'" + l + "' is not nil");
344            f(listhd(l));
345            listiter(f,listtl(l));
346         }
347 }
348
349 function listmap (f,l)
350 {
351         debug("listmap on " + l);
352         if (is_nil(l)) 
353            { debug("returning listnil");
354              return(listnil());
355            }
356         else 
357            { debug("cons f(hd) map(f,tl)");
358              return(f(listhd(l)) + "#" + listmap(f,listtl(l)));
359            }
360 }
361
362 var statements = listnil();
363
364 var goalarray;
365 var metalist = listnil();
366
367 function pairmap (f,p)
368 {
369   debug("pairmap of '" + p + "'");
370   ar = p.split("|");
371   return (f(ar[0],ar[1])); 
372 }
373
374 function tripletmap (f,p)
375 {
376   debug("tripletmap of '" + p + "'");
377   ar = p.split("|");
378   return (f(ar[0],ar[1],ar[2])); 
379 }
380
381 function fst (p)
382 {
383   debug("fst");
384   return (pairmap (function (a,b) { return (a); }, p));
385 }
386
387 function p13 (p)
388 {
389   debug("p13");
390   return (tripletmap (function (a,b,c) { return (a); }, p));
391 }
392
393 function p23 (p)
394 {
395   debug("p23");
396   return (tripletmap (function (a,b,c) { return (b); }, p));
397 }
398
399 function p33 (p)
400 {
401   debug("f33");
402   return (tripletmap (function (a,b,c) { return (c); }, p));
403 }
404
405 function populate_goalarray(menv)
406 {
407   debug("metasenv.length = " + menv.length);
408   if (menv.length == 0) {
409       try {
410           hideSequent();
411       } catch (err) { };
412   } else {
413       showSequent();
414       goalarray = new Array();
415       metalist = listnil();
416       var tmp_goallist = "";
417       for (i = 0; i < menv.length; i++) {
418         metano = menv[i].getAttribute("number");
419         metaname = menv[i].childNodes[0].childNodes[0].data;
420         goal = menv[i].childNodes[1].childNodes[0].data;
421         debug ("found meta n. " + metano);
422         debug ("found goal\nBEGIN" + goal + "\nEND");
423         goalarray[metano] = goal;
424         tmp_goallist = " <A href=\"javascript:switch_goal(" + metano + ")\">" + metaname + "</A>" + tmp_goallist;
425         metalist = listcons(metano,metalist);
426         debug ("goalarray[\"" + metano + "\"] = " + goalarray[metano]); 
427       }
428       goals.innerHTML = tmp_goallist;
429       debug("new metalist is '" + metalist + "'");
430       if (is_nil(metalist)) {
431         switch_goal();
432       }
433       else {
434         switch_goal(listhd(metalist));
435       }
436   }
437 }
438
439 function switch_goal(meta)
440 {
441   if (typeof meta == "undefined") {
442     goalview.innerHTML = "";
443   }
444   else {
445     debug("switch_goal " + meta + "\n" + goalarray[meta]);
446     goalview.innerHTML = "<B>Goal ?" + meta + ":</B>\n\n" + goalarray[meta];
447   }
448 }
449
450 // the following is used to avoid escaping unicode, which results in 
451 // the server being unable to unescape the string
452 String.prototype.sescape = function() {
453         var patt1 = /%/gi;
454         var patt2 = /=/gi;
455         var patt3 = /&/gi;
456         var patt4 = /\+/gi;
457         var result = this;
458         result = result.replace(patt1,"%25");
459         result = result.replace(patt2,"%3D");
460         result = result.replace(patt3,"%26");
461         result = result.replace(patt4,"%2B");
462         return (result);
463 }
464
465 String.prototype.html_to_matita = function()
466 {
467         var patt1 = /<br(\/|)>/gi;
468         var patt2 = /</gi
469         var patt3 = />/gi
470         var patt4 = /&lt;/gi;
471         var patt5 = /&gt;/gi;
472         var patt6 = /&nbsp;/gi;
473         var result = this;
474         result = result.replace(patt1,"\n");
475         result = result.replace(patt2,"\005");
476         result = result.replace(patt3,"\006");
477         result = result.replace(patt4,"<");
478         result = result.replace(patt5,">");
479         result = result.replace(patt6," ");
480         return (unescape(result));
481 }
482
483 String.prototype.matita_to_html = function()
484 {
485         var patt1 = /</gi
486         var patt2 = />/gi
487         var patt3 = /\005/gi;
488         var patt4 = /\006/gi;
489         var result = this;
490         result = result.replace(patt1,"&lt;");
491         result = result.replace(patt2,"&gt;");
492         result = result.replace(patt3,"<");
493         result = result.replace(patt4,">");
494         return (unescape(result));
495 }
496
497 function pause()
498 {
499         advanceButton.disabled = true;
500         retractButton.disabled = true;
501         cursorButton.disabled = true;
502         bottomButton.disabled = true;
503 }
504
505 function resume()
506 {
507         advanceButton.disabled = false;
508         retractButton.disabled = false;
509         cursorButton.disabled = false;
510         bottomButton.disabled = false;
511 }
512
513 function is_defined(x)
514 {
515         return (typeof x != "undefined");
516 }
517
518 /* servicename: name of the service being called
519  * reqbody: text of the request
520  * processResponse: processes the server response
521  *     (takes the response text in input, undefined in case of error)
522  */
523 function callServer(servicename,processResponse,reqbody)
524 {
525         var req = null; 
526         // pause();
527         if (window.XMLHttpRequest)
528         {
529                 req = new XMLHttpRequest();
530         } 
531         else if (window.ActiveXObject) 
532         {
533                 try {
534                                 req = new ActiveXObject("Msxml2.XMLHTTP");
535                 } catch (e)
536                 {
537                         try {
538                                 req = new ActiveXObject("Microsoft.XMLHTTP");
539                                 } catch (e) {}
540                 }
541         }
542         req.onreadystatechange = function()
543         { 
544
545                 rs = req.readyState;
546
547                 if(rs == 4)
548                 {
549                         stat = req.status;
550                         stxt = req.statusText;
551                         if(stat == 200)
552                         {
553                           debug(req.responseText);
554                           if (window.DOMParser) {
555                             parser=new DOMParser();
556                             xmlDoc=parser.parseFromString(req.responseText,"text/xml");
557                           }
558                           else // Internet Explorer
559                           {
560                             xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
561                             xmlDoc.async="false";
562                             xmlDoc.loadXML(req.responseText);
563                           }     
564                           processResponse(xmlDoc);
565                         } else {
566                           processResponse();
567                         }
568                 } 
569         };
570         req.open("POST", servicename); // + escape(unlocked.innerHTML), true);
571         req.setRequestHeader("Content-type","application/x-www-form-urlencoded");       
572         if (reqbody) {
573                 req.send(reqbody); 
574         } else {
575                 req.send();
576         }
577   
578 }
579
580 function advanceForm1()
581 {
582         processor = function(xml) {
583                 if (is_defined(xml)) {
584                         var parsed = xml.getElementsByTagName("parsed")[0];
585                         var ambiguity = xml.getElementsByTagName("ambiguity")[0];
586                         if (is_defined(parsed)) {
587                         // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
588                             var len = parseInt(parsed.getAttribute("length"));
589                             // len0 = unlocked.innerHTML.length;
590                             var unescaped = unlocked.innerHTML.html_to_matita();
591                             var parsedtxt = parsed.childNodes[0].nodeValue;
592                             //parsedtxt = unescaped.substr(0,len); 
593                             var unparsedtxt = unescaped.substr(len);
594                             lockedbackup += parsedtxt;
595                             locked.innerHTML = lockedbackup;
596                             unlocked.innerHTML = unparsedtxt.matita_to_html();
597                             // len1 = unlocked.innerHTML.length;
598                             // len2 = len0 - len1;
599                             var len2 = parsedtxt.length;
600                             var metasenv = xml.getElementsByTagName("meta");
601                             populate_goalarray(metasenv);
602                             init_autotraces();
603                             statements = listcons(len2,statements);
604                             unlocked.scrollIntoView(true);
605                         }
606                         else if (is_defined(ambiguity)) {
607                             var start = parseInt(ambiguity.getAttribute("start"));
608                             var stop = parseInt(ambiguity.getAttribute("stop"));
609                             var choices = xml.getElementsByTagName("choice");
610
611                             matita.ambiguityStart = start;
612                             matita.ambiguityStop = stop;
613                             matita.unlockedbackup = unlocked.innerHTML.html_to_matita();
614                             matita.interpretations = [];
615                         
616                             var unlockedtxt = unlocked.innerHTML.html_to_matita();
617                             var pre = unlockedtxt.substring(0,start).matita_to_html();
618                             var mid = unlockedtxt.substring(start,stop).matita_to_html();
619                             var post = unlockedtxt.substring(stop).matita_to_html();
620                             unlocked.innerHTML = pre + 
621                                     "<span class=\"error\" title=\"disambiguation error\">" +
622                                     mid + "</span>" + post;
623
624                             var title = "<H3>Ambiguous input</H3>";
625                             disambcell.innerHTML = title;
626                             for (i = 0;i < choices.length;i++) {
627                                 matita.interpretations[i] = new Object();
628
629                                 var href = choices[i].getAttribute("href");
630                                 var title = choices[i].getAttribute("title");
631                                 var desc = choices[i].childNodes[0].nodeValue;
632
633                                 matita.interpretations[i].href = href;
634                                 matita.interpretations[i].title = title;
635                                 matita.interpretations[i].desc = desc;
636                                 
637                                 var choice = document.createElement("input");
638                                 choice.setAttribute("type","radio");
639                                 choice.setAttribute("name","interpr");
640                                 choice.setAttribute("href",href);
641                                 choice.setAttribute("title",title);
642                                 if (i == 0) choice.setAttribute("checked","");
643                                 
644                                 disambcell.appendChild(choice);
645                                 disambcell.appendChild(document.createTextNode(desc));
646                                 disambcell.appendChild(document.createElement("br"));
647                             }
648
649                             var okbutton = document.createElement("input");
650                             okbutton.setAttribute("type","button");
651                             okbutton.setAttribute("value","OK");
652                             okbutton.setAttribute("onclick","do_disambiguate()");
653                             var cancelbutton = document.createElement("input");
654                             cancelbutton.setAttribute("type","button");
655                             cancelbutton.setAttribute("value","Cancel");
656                             cancelbutton.setAttribute("onclick","cancel_disambiguate()");
657
658                             disambcell.appendChild(okbutton);
659                             disambcell.appendChild(cancelbutton);
660
661                             disable_toparea();
662
663                             matita.disambMode = true;
664                             updateSide();
665                         }
666                         else {
667                             var error = xml.getElementsByTagName("error")[0]; 
668                             unlocked.innerHTML = error.childNodes[0].nodeValue;
669                             // debug(xml.childNodes[0].nodeValue);
670                         }
671                 } else {
672                         debug("advance failed");
673                 }
674                 resume();
675         };
676         pause();
677         callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
678   
679 }
680
681 function gotoBottom()
682 {
683         processor = function(xml) {
684                 if (is_defined(xml)) {
685                         // debug("goto bottom: received response\nBEGIN\n" + req.responseText + "\nEND");
686                         parsed = xml.getElementsByTagName("parsed")[0];
687                         len = parseInt(parsed.getAttribute("length"));
688                         if (len > 0) {
689                           // len0 = unlocked.innerHTML.length;
690                           unescaped = unlocked.innerHTML.html_to_matita();
691                           // not working in mozilla
692                           // parsedtxt = parsed.childNodes[0].nodeValue;
693                           parsedtxt = parsed.childNodes[0].wholeText;
694                           //parsedtxt = unescaped.substr(0,len); 
695                           unparsedtxt = unescaped.substr(len);
696                           lockedbackup += parsedtxt;
697                           locked.innerHTML = lockedbackup; //.matita_to_html();
698                           unlocked.innerHTML = unparsedtxt.matita_to_html();
699                           // len1 = unlocked.innerHTML.length;
700                           len2 = parsedtxt.length;
701                           metasenv = xml.getElementsByTagName("meta");
702                           init_autotraces();
703                           populate_goalarray(metasenv);
704                           if (len2 > 0)
705                             statements = listcons(len2,statements);
706                           unlocked.scrollIntoView(true);
707                         }
708                 } else {
709                         debug("goto bottom failed");
710                 } 
711                 resume();
712         };
713         pause();
714         callServer("bottom",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
715   
716 }
717
718
719 function gotoTop()
720 {
721         processor = function(xml) {
722                 if (is_defined(xml)) {
723                   if (xml.childNodes[0].textContent != "ok") {
724                      debug("goto top failed");
725                   }
726                   else
727                         statements = listnil();
728                         /*
729                         lockedlen = locked.innerHTML.length - statementlen;
730                         statement = locked.innerHTML.substr(lockedlen, statementlen);
731                         locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
732                         unlocked.innerHTML = statement + unlocked.innerHTML;
733                         */
734                         unlocked.innerHTML = lockedbackup + unlocked.innerHTML;
735                         lockedbackup = "";
736                         locked.innerHTML = lockedbackup;
737                         init_autotraces();
738                         hideSequent();
739                         unlocked.scrollIntoView(true);
740                 } else {
741                         debug("goto top failed");
742                 } 
743                 resume();
744         };
745         pause();
746         callServer("top",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
747   
748 }
749 function gotoPos(offset)
750 {
751         if (!is_defined(offset)) {
752                 offset = getCursorPos();
753         }
754         processor = function(xml) {
755                 if (is_defined(xml)) {
756                         parsed = xml.getElementsByTagName("parsed")[0];
757                         len = parseInt(parsed.getAttribute("length"));
758                         // len0 = unlocked.innerHTML.length;
759                         unescaped = unlocked.innerHTML.html_to_matita();
760                         parsedtxt = parsed.childNodes[0].nodeValue;
761                         //parsedtxt = unescaped.substr(0,len); 
762                         unparsedtxt = unescaped.substr(len);
763                         lockedbackup += parsedtxt;
764                         locked.innerHTML = lockedbackup; //.matita_to_html();
765                         unlocked.innerHTML = unparsedtxt.matita_to_html();
766                         // len1 = unlocked.innerHTML.length;
767                         len2 = parsedtxt.length;
768                         metasenv = xml.getElementsByTagName("meta");
769                         // populate_goalarray(metasenv);
770                         statements = listcons(len2,statements);
771                         unlocked.scrollIntoView(true);
772                         // la populate non andrebbe fatta a ogni passo
773                         if (offset <= len) {
774                                 init_autotraces();
775                                 populate_goalarray(metasenv);
776                                 resume();
777                         } else {
778                                 gotoPos(offset - len);
779                         }
780                 } else {
781                         init_autotraces();
782                         unlocked.scrollIntoView(true);
783                         populate_goalarray(metasenv);
784                         resume();
785                 }
786         }
787         pause();
788         callServer("advance",processor,"body=" + (unlocked.innerHTML.html_to_matita()).sescape());
789 }
790
791 function retract()
792 {
793         processor = function(xml) {
794                 if (typeof xml != "undefined") {
795                         // debug("advance: received response\nBEGIN\n" + req.responseText + "\nEND");
796                         statementlen = parseInt(listhd(statements));
797                         statements = listtl(statements);
798                         /*
799                         lockedlen = locked.innerHTML.length - statementlen;
800                         statement = locked.innerHTML.substr(lockedlen, statementlen);
801                         locked.innerHTML = locked.innerHTML.substr(0,lockedlen);
802                         unlocked.innerHTML = statement + unlocked.innerHTML;
803                         */
804                         lockedlen = lockedbackup.length - statementlen;
805                         statement = lockedbackup.substr(lockedlen, statementlen);
806                         lockedbackup = lockedbackup.substr(0,lockedlen);
807                         locked.innerHTML = lockedbackup;
808                         unlocked.innerHTML = statement + unlocked.innerHTML;
809                         metasenv = xml.getElementsByTagName("meta");
810                         init_autotraces();
811                         populate_goalarray(metasenv);
812                         unlocked.scrollIntoView(true);
813                 } else {
814                         debug("retract failed");
815                 }
816                 resume();
817         };
818         debug("retract 1");
819         pause();
820         callServer("retract",processor);
821         debug("retract 2");
822 }
823
824 function openFile()
825
826         processor = function(xml)
827         {
828                 if (is_defined(xml)) {  
829                         lockedbackup = "";
830                         locked.innerHTML = lockedbackup;
831                         unlocked.innerHTML = xml.documentElement.wholeText;
832                 } else {
833                         debug("file open failed");
834                 }
835         };
836         callServer("open",processor,"file=" + escape(filename.value)); 
837 }
838
839 function retrieveFile(thefile)
840
841         processor = function(xml)
842         {
843                 if (is_defined(xml)) {  
844                         changeFile(thefile);
845                         lockedbackup = ""
846                         locked.innerHTML = lockedbackup;
847                         // code originally used in google chrome (problems with mozilla)
848                         // debug(xml.getElementsByTagName("file")[0].childNodes[0].nodeValue);
849                         // unlocked.innerHTML = xml.getElementsByTagName("file")[0].childNodes[0].nodeValue;
850                         debug(xml.childNodes[0].textContent);
851                         if (document.all) { // IE
852                           unlocked.innerHTML = xml.childNodes[0].text;
853                         } else {
854                           unlocked.innerHTML = xml.childNodes[0].textContent;
855                         }
856                         init_autotraces();
857
858                 } else {
859                         debug("file open failed");
860                 }
861         };
862         abortDialog();
863         callServer("open",processor,"file=" + escape(thefile)); 
864 }
865
866 function showLibrary(title,callback,reloadDialog)
867
868         var req = null;
869         dialogBox.reload = reloadDialog; 
870         // pause();
871         if (window.XMLHttpRequest)
872         {
873                 req = new XMLHttpRequest();
874         } 
875         else if (window.ActiveXObject) 
876         {
877                 try {
878                                 req = new ActiveXObject("Msxml2.XMLHTTP");
879                 } catch (e)
880                 {
881                         try {
882                                 req = new ActiveXObject("Microsoft.XMLHTTP");
883                                 } catch (e) {}
884                 }
885         }
886         req.onreadystatechange = function()
887         { 
888
889                 rs = req.readyState;
890
891                 if(rs == 4)
892                 {
893                         stat = req.status;
894                         stxt = req.statusText;
895                         if(stat == 200)
896                         {
897                           debug(req.responseText);
898                           showDialog("<H2>" + title + "</H2>",req.responseText, callback);
899                         } 
900                 } 
901         };
902         req.open("POST", "viewlib"); // + escape(unlocked.innerHTML), true);
903         req.setRequestHeader("Content-type","application/x-www-form-urlencoded");       
904         req.send();
905   
906 }
907
908 function uploadDialog()
909 {  
910         uploadBox.style.display = "block";
911 }
912
913 function uploadOK()
914 {   
915    var file = document.getElementById("uploadFilename").files[0];
916    if (file) { 
917        var filecontent = file.getAsText("UTF-8");
918        locked.innerHTML = lockedbackup;
919        unlocked.innerHTML = filecontent;
920        uploadBox.style.display = "none";
921    }
922 //   if (file) { 
923 //      var reader = new FileReader();
924 //      reader.readAsText(file, "UTF-8");
925 //       reader.onloadend = function (evt) {
926 //         lockedbackup = "";
927 //           locked.innerHTML = lockedbackup
928 //           unlocked.innerHTML = evt.target.result;
929 //           uploadBox.style.display = "none";
930 //       }
931 //       reader.onerror = function (evt) {
932 //         debug("file open failed");
933 //           uploadBox.style.display = "none";
934 //      }
935 //   }
936 }
937
938 function openDialog()
939 {  
940         callback = function (fname) { retrieveFile(fname); };
941         showLibrary("Open file", callback, openDialog);
942 }
943
944 function saveDialog()
945 {  
946         callback = function (fname) { 
947           abortDialog();
948           saveFile(fname,
949                    (locked.innerHTML.html_to_matita()).sescape(),
950                    (unlocked.innerHTML.html_to_matita()).sescape(),
951                    false,saveDialog); 
952         };
953         showLibrary("Save file as", callback, saveDialog);
954 }
955
956 function newDialog()
957 {
958         callback = function (fname) { 
959           abortDialog();
960           saveFile(fname,"","",false,newDialog,true);
961         };
962         showLibrary("Create new file", callback, newDialog);
963 }
964
965
966 function saveFile(fname,lockedtxt,unlockedtxt,force,reloadDialog,reloadFile)
967 {
968         if (!is_defined(reloadFile)) { reloadFile = true };
969         if (!is_defined(fname)) {
970             fname = current_fname;
971             lockedtxt = (locked.innerHTML.html_to_matita()).sescape();
972             unlockedtxt = (unlocked.innerHTML.html_to_matita()).sescape();
973             force = true;
974             // when force is true, reloadDialog is not needed 
975         }
976         processor = function(xml) {
977                 if (is_defined(xml)) {
978                   if (xml.childNodes[0].textContent != "ok") {
979                     if (confirm("File already exists. All existing data will be lost.\nDo you want to proceed anyway?")) {
980                        saveFile(fname,lockedtxt,unlockedtxt,true,reloadDialog,reloadFile);
981                     } else {
982                       reloadDialog();
983                     }
984                   } else {
985                     changeFile(fname);
986                     debug("file saved!");
987                     if (reloadFile) { retrieveFile(fname); }
988                   }
989                 } else {
990                         debug("save file failed");
991                 }
992                 resume();
993         };
994         if (is_defined(fname)) {
995           pause();
996           callServer("save",processor,"file=" + escape(fname) + 
997                                     "&locked=" + lockedtxt +
998                                     "&unlocked=" + unlockedtxt +
999                                     "&force=" + force);
1000         }
1001         else { debug("no file selected"); }
1002 }
1003
1004 function createDir() {
1005    abortDialog();
1006    dirname = prompt("New directory name:\ncic:/matita/","newdir");
1007    if (dirname != null) {
1008         processor = function(xml) {
1009                 if (is_defined(xml)) {
1010                   if (xml.childNodes[0].textContent != "ok") {
1011                       alert("An error occurred :-(");
1012                   }
1013                 } else {
1014                       alert("An error occurred :-(");
1015                 }
1016                 dialogBox.reload();
1017         };
1018         pause();
1019         callServer("save",processor,"file=" + escape(dirname) + 
1020                                     "&locked=&unlocked=&force=false&dir=true");
1021    } else {
1022       dialogBox.reload();
1023    }
1024 }
1025
1026 function commitAll()
1027 {
1028         processor = function(xml) {
1029                 if (is_defined(xml)) {
1030                         debug(xml.getElementsByTagName("details")[0].textContent);
1031                         alert("Commit executed: see details in the log.\n\n" +
1032                               "NOTICE: this message does NOT imply (yet) that the commit was successful.");
1033                 } else {
1034                         alert("Commit failed!");
1035                 }
1036                 resume();
1037         };
1038         pause();
1039         callServer("commit",processor);
1040 }
1041
1042 function updateAll()
1043 {
1044         processor = function(xml) {
1045                 if (is_defined(xml)) {
1046                         alert("Update executed.\n\n" +
1047                               "Details:\n" +
1048                               xml.getElementsByTagName("details")[0].textContent);
1049                 } else {
1050                         alert("Update failed!");
1051                 }
1052                 resume();
1053         };
1054         pause();
1055         callServer("update",processor);
1056 }
1057
1058 var goalcell;
1059
1060 function hideSequent() {
1061         matita.proofMode = false;
1062         updateSide();
1063 }
1064
1065 function showSequent() {
1066         matita.proofMode = true;
1067         updateSide();
1068 }
1069
1070 function showDialog(title,content,callback) {
1071   dialogTitle.innerHTML = title;
1072   dialogContent.innerHTML = content;
1073   dialogBox.callback = callback;
1074
1075   //Get the screen height and width
1076   var maskHeight = $(document).height();
1077   var maskWidth = $(window).width();
1078   
1079   //Set heigth and width to mask to fill up the whole screen
1080   $('#mask').css({'width':maskWidth,'height':maskHeight});
1081   
1082   //transition effect           
1083   $('#mask').fadeIn(100);       
1084   $('#mask').fadeTo(200,0.8);   
1085   
1086   //Get the window height and width
1087   var winH = $(window).height();
1088   var winW = $(window).width();
1089   
1090   //Set the popup window to center
1091   $('#dialogBox').css('top',  winH/2-$('#dialogBox').height()/2);
1092   $('#dialogBox').css('left', winW/2-$('#dialogBox').width()/2);
1093   
1094   //transition effect
1095   $('#dialogBox').fadeIn(200); 
1096
1097   dialogBox.style.display = "block";
1098 }
1099
1100 function abortDialog(dialog) {
1101   $('#mask').hide();
1102   dialogBox.style.display = "none";
1103 }
1104
1105 function removeElement(id) {
1106   var element = document.getElementById(id);
1107   element.parentNode.removeChild(element);
1108
1109
1110 var savedsc;
1111 var savedso;
1112
1113 function getCursorPos() {
1114   var cursorPos;
1115   if (window.getSelection) {
1116     var selObj = window.getSelection();
1117     savedRange = selObj.getRangeAt(0);
1118     savedsc = savedRange.startContainer;
1119     savedso = savedRange.startOffset;
1120     //cursorPos =  findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset;
1121     cursorPos =  findNode(unlocked.childNodes, selObj.anchorNode,0) + selObj.anchorOffset;
1122     /* FIXME the following works wrong in Opera when the document is longer than 32767 chars */
1123     return(cursorPos);
1124   }
1125   else if (document.selection) {
1126     savedRange = document.selection.createRange();
1127     var bookmark = savedRange.getBookmark();
1128     /* FIXME the following works wrong when the document is longer than 65535 chars */
1129     cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */
1130     return(cursorPos);
1131   }
1132 }
1133
1134 function findNode(list, node, acc) {
1135   for (var i = 0; i < list.length; i++) {
1136     if (list[i] == node) {
1137    //   debug("success " + i);
1138       return acc;
1139     }
1140     if (list[i].hasChildNodes()) {
1141        try {
1142    //      debug("recursion on node " + i);
1143          return (findNode(list[i].childNodes,node,acc))
1144        }
1145        catch (e) { /* debug("recursion failed"); */ }
1146     }
1147     sandbox = document.getElementById("sandbox");
1148     dup = list[i].cloneNode(true);
1149     sandbox.appendChild(dup);
1150 //    debug("fail " + i + ": " + sandbox.innerHTML);
1151     acc += sandbox.innerHTML.html_to_matita().length;
1152     sandbox.removeChild(dup);
1153   }
1154   throw "not found";
1155 }
1156
1157 function test () {
1158   debug("cursor test: " + unlocked.innerHTML.substr(0,getCursorPos()));
1159 }
1160
1161 function get_checked_index(name) {
1162         var radios = document.getElementsByName(name);
1163         for (i = 0; i < radios.length; i++) {
1164             if (radios[i].checked) {
1165                     return i;
1166             }
1167         }
1168         return null;
1169 }
1170
1171 function cancel_disambiguate() {
1172         matita.disambMode = false;
1173         $('#whitemask').hide();
1174         updateSide();
1175 }
1176
1177 function do_disambiguate() {
1178         var i = get_checked_index("interpr");
1179         if (i != null) {
1180             var pre = matita.unlockedbackup
1181                       .substring(0,matita.ambiguityStart).matita_to_html();
1182             var mid = matita.unlockedbackup
1183                       .substring(matita.ambiguityStart,matita.ambiguityStop)
1184                       .matita_to_html();
1185             var post = matita.unlockedbackup
1186                        .substring(matita.ambiguityStop).matita_to_html();
1187
1188             var href = matita.interpretations[i].href;
1189             var title = matita.interpretations[i].title;
1190
1191             if (is_defined(title)) {
1192                 mid = "<A href=\"" + href + "\" title=\"" + title + "\">" + mid + "</A>";
1193             } else {
1194                 mid = "<A href=\"" + href + "\">" + mid + "</A>";
1195             }
1196
1197             unlocked.innerHTML = pre + mid + post;
1198
1199             matita.disambMode = false;
1200             $('#whitemask').hide();
1201             updateSide();
1202         }
1203 }
1204
1205 function readCookie(name) {
1206         var nameEQ = name + "=";
1207         var ca = document.cookie.split(';');
1208         for(var i=0;i < ca.length;i++) {
1209                 var c = ca[i];
1210                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
1211                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
1212         }
1213         return null;
1214 }
1215
1216 function delete_cookie ( cookie_name )
1217 {
1218   var cookie_date = new Date();  // current date & time
1219   cookie_date.setTime ( cookie_date.getTime() - 1 );
1220   document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
1221 }
1222
1223 function delete_session()
1224 {
1225         delete_cookie("session");
1226 }
1227
1228 function disable_toparea() {
1229         var offset = $('#toparea').offset();
1230         $('#whitemask').css('top',offset.top);
1231         $('#whitemask').css('left',offset.left);
1232         $('#whitemask').css('width',$('#toparea').outerWidth() + "px");
1233         $('#whitemask').css('height',$('#toparea').outerHeight() + "px");
1234         $('#whitemask').fadeTo('fast',0.7);
1235 }