// (c) Copyright 2006 Rob Rohan All Rights Reserved

var INSTALL_LOCATION="http://robrohan.com/projects/9ne";var MAIN_FILE="9ne.php";var editor=null;var editor_text_area=null;var editor_gutter_area=null;var editor_caret=null;var editor_second_caret=null;var editor_status=null;var editor_messages=null;var editor_minibuffer=null;var textarea_bucket=null;var clipboard=""
var buffer_name="*scratch*";var buffer_mode="Scratch";var editor_in_focus=false;var minibuffer_focus=false;var buffer_command="";var active_line=null;var active_column=0;var active_mark=null;var number_of_lines=0;var linked_textarea=null;var tab_size=4;var TAB="";var LINEHEIGHT=17;var ELECTRIC_LINE=3;var DEFAULT_WIDTH=615;var DEFAULT_HEIGHT=620;var SYNTAX_COLORING=true;var isdirty=false;var caretblink=null;var GUTTER_X_OFFSET=0;var CHAR_SIZE=0;var event_manager=null;var mode_ext_map=null;var text_tools=null;var screen_tools=null;var dialog=null;function Mark(){this.line;this.column;}
function launchWithTextarea(width,height,textarea){var windowOptions='history=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width='+width+',height='+height;var handle=window.open(INSTALL_LOCATION+'/'+MAIN_FILE+'?area='+textarea,"ne"+new Date().getTime(),windowOptions);handle.focus();}
function launch9ne(width,height,filename){var windowOptions='history=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width='+width+',height='+height;var handle=window.open(INSTALL_LOCATION+'/'+MAIN_FILE+'?file='+filename,"ne"+new Date().getTime(),windowOptions);handle.focus();}
function launch9neWithHandle(width,height,filename){var windowOptions='history=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width='+width+',height='+height;var handle=window.open(INSTALL_LOCATION+'/'+MAIN_FILE+'?file='+filename,"ne"+new Date().getTime(),windowOptions);handle.focus();return handle;}
function initEditor(){editor=document.getElementById("editor");editor_text_area=document.getElementById("editortext");editor_gutter_area=document.getElementById("editorgutter");editor_caret=document.getElementById("editorCaret");editor_second_caret=document.getElementById("editorSecondCaret");editor_status=document.getElementById("statusbar");editor_minibuffer=document.getElementById("minibuffer");editor_messages=document.getElementById("editorMessages");textarea_bucket=document.getElementById("fakefocus");GUTTER_X_OFFSET=document.getElementById("editorgutter").offsetWidth;CHAR_SIZE=editor_caret.offsetWidth;event_manager=new Sortie.UI.EventManager();event_manager.Init(document);event_manager.AddKeyPressListener(editor_onKeyPress);event_manager.AddKeyDownListener(editor_onKeyDown);event_manager.AddKeyUpListener(editor_onKeyUp);event_manager.AddMouseDownListener(function(e){try{var linenum=getLineIdNumber(e.target);if(linenum)
jump_to_line(linenum);}catch(e){}
editor_focus();});event_manager.AddMoveListener(function(e){Sortie.UI.Mouse.SetCoords(e);window.status="X: "+Sortie.UI.Mouse.X+" Y:"+Sortie.UI.Mouse.Y;});window.mode_syntax_rules=new Sortie.Util.Map();mode_ext_map=new Sortie.Util.Map();text_tools=new Sortie.Util.Text();screen_tools=new Sortie.UI.Screen();window.extensions={};mode_ext_map.Put("css","CSS");mode_ext_map.Put("js","Javascript");mode_ext_map.Put("rel","Release");mode_ext_map.Put("txt","Text");mode_ext_map.Put("text","Text");mode_ext_map.Put("xml","XML");mode_ext_map.Put("rss","XML");mode_ext_map.Put("xsl","XML");mode_ext_map.Put("html","XML");mode_ext_map.Put("xhtml","XML");mode_ext_map.Put("htm","XML");mode_ext_map.Put("cfm","XML");mode_ext_map.Put("cfml","XML");mode_ext_map.Put("cfc","XML");mode_ext_map.Put("php","XML");for(var q=0;q<tab_size;q++)
TAB+="&nbsp;";fundamental_mode();var current_url=window.location.toString();if(current_url.indexOf("?")>-1)
{var urlparts=current_url.split("?")
var fnameparts=urlparts[1].toString().split("=");var f=fnameparts[1].toString().split("&");var filename=f[0].toString();if(fnameparts[0].toString()=="file"){var pipe=new Sortie.IO.Pipe().GetInstance();var gateway=new Sortie.IO.Gateway(pipe,true)
gateway.DoRequest({method:"POST",url:filename,handler:function(conn){if(conn.status==200)
loadFile(filename,guessFileType(filename),conn.responseText);else
{document.title=buffer_name=filename;buffer_mode=guessFileType(filename);editor_addLine();}},body:""});}
else if(fnameparts[0].toString()=="area")
{if(window.opener)
linked_textarea=window.opener.document.getElementById(filename);else
linked_textarea=parent.document.getElementById(filename);linked_textarea.disabled=true;linked_textarea.style.background="silver";loadFile("Textarea",guessFileType("temp.html"),linked_textarea.value);}}
else
{editor_addLine();}
try{window.onactivate=editor_focus();window.onfocus=editor_focus();window.document.onfocus=editor_focus();window.document.onactive=editor_focus();window.onresize=function(){}}catch(e){}
document.title=buffer_name;editor_focus();setTimeout("runOnStart()",500);}
function runOnStart(){if(window.on9neStart)
window.on9neStart()}
function runOnEnd(){if(window.on9neEnd)
window.on9neEnd()}
function guessFileType(filename){if(SYNTAX_COLORING){var ext=filename.substring(filename.lastIndexOf(".")+1);if(mode_ext_map.Contains(ext)){var mode=mode_ext_map.Get(ext);loadModeFile(mode.toLowerCase());return mode;}}
fundamental_mode();return"Fundamental";}
function flashCaret(){if(editor_caret.style.visibility=="visible")
caret_hide();else
caret_show();if(Sortie.Util.Browser.Explorer)
positionCaret();}
function caret_hide(){editor_caret.style.visibility="hidden";}
function caret_show(){editor_caret.style.visibility="visible";}
function cursor_solid(){if(caretblink!=null)clearInterval(caretblink);editor_caret.style.visibility="visible";}
function cursor_blink(){if(caretblink!=null)clearInterval(caretblink);caretblink=setInterval("flashCaret()",500);}
function editor_focus(){cursor_blink();editor_in_focus=true;if(!textarea_bucket)
textarea_bucket=document.getElementById("fakefocus");else
textarea_bucket.focus();}
function editor_blur(){if(caretblink!=null)clearInterval(caretblink);caretblink==null;editor_in_focus=false;}
function loadFile(name,type,contents){document.title=buffer_name=name;buffer_mode=type;if(contents!=null&&contents!=""){var lines=contents.split("\n");for(var q=0;q<lines.length;q++){fast_line_add(lines[q]);lines[q]=null;}}else{editor_addLine();}
jump_to_line(1);editor_focus();}
function fast_line_add(txt){var new_line=document.createElement("DIV");new_line.className="editorline";var clean=text_tools.XmlFormat(txt);clean=clean.replace(/ /g,"&nbsp;");clean=clean.replace(/\t/g,TAB);new_line.innerHTML=colorLine(clean);new_line.setAttribute("id","l"+(++number_of_lines));var new_gutter=document.createElement("DIV");new_gutter.className="gutteritem";new_gutter.appendChild(document.createTextNode(number_of_lines));editor_gutter_area.appendChild(new_gutter);editor_text_area.appendChild(new_line);}
function colorLine(txt){if(window.mode_syntax_rules){var syntaxkeys=window.mode_syntax_rules.GetKeysAsArray();for(var z=0;z<syntaxkeys.length;z++){var rule=window.mode_syntax_rules.Get(syntaxkeys[z]);txt=txt.replace(rule.regex,rule.replace);}}
return txt;}
function repaint_text(){for(var i=1;i<=number_of_lines;i++){var line=getLine(i);var linetxt=getLinePlainText(line,0,getLineLength(line));var clean=text_tools.XmlFormat(linetxt);clean=clean.replace(/ /g,"&nbsp;");if(line!=null)
insertLineText(line,clean);}}
function getFileContents(){var chillin=editor_text_area.childNodes;alert(chillin.length+" "+chillin.item(0).innerHTML);}
function confirm_dialog(settings){var wrap=document.createElement("DIV");var inst=document.createElement("P");inst.appendChild(document.createTextNode(settings.text));var btna=document.createElement("INPUT");btna.setAttribute("type","button");btna.setAttribute("value",settings.avalue);btna.setAttribute("onclick",settings.afunction);wrap.appendChild(inst);wrap.appendChild(btna);document.getElementById("dialog").appendChild(wrap);dialog.show();}
function positionCaret(){var tempmark=new Mark();tempmark.line=active_line;tempmark.column=active_column;displayCaretAtPosition(editor_caret,tempmark);updateEditorStatusText();}
function displayCaretAtPosition(caret,mark){var GUTTER_EXTRA_OFFSET=CHAR_SIZE;var y=screen_tools.FindPosY(mark.line)-editor.scrollTop;var x=screen_tools.FindPosX(editor_text_area)+GUTTER_X_OFFSET;caret.style.top=y+"px";caret.style.left=(x+GUTTER_EXTRA_OFFSET)+(mark.column*CHAR_SIZE);}
function updateEditorStatusText(){var lookingAt=(get_visible_lines()>=number_of_lines)?"All":parseInt((get_visible_lines()/number_of_lines)*100)+"%";var statustext="-u:"+((isdirty)?"**":"--")+" ";statustext+=buffer_name+" "+lookingAt+" of "+number_of_lines+"L";statustext+=" ("+active_line.id.toString().substring(1)+","+active_column+")";statustext+=" ("+buffer_mode+")";editor_status.innerHTML=statustext;}
function show_editor_message(msg){editor_messages.innerHTML=msg;editor_messages.style.visibility="visible";setTimeout("editor_messages.style.visibility = 'hidden'",2000);}
function addCharCodeToPosition(code){var clean=text_tools.XmlFormat(code);clean=clean.replace(/ /g,"&nbsp;");addStringToCurrentPosition(clean)}
function addStringToCurrentPosition(str){insert_text(str);}
function insertLineText(line,text){text=colorLine(text);line.innerHTML=text;}
function addText(line,start,text){var lineText=getLinePlainText(line,0,getLineLength(line));line_first_half=lineText.substring(0,start);line_second_half=lineText.substring(start);line_first_half=text_tools.XmlFormat(line_first_half);line_first_half=line_first_half.replace(/ /g,"&nbsp;");line_second_half=text_tools.XmlFormat(line_second_half);line_second_half=line_second_half.replace(/ /g,"&nbsp;");var new_line_text=line_first_half+text+line_second_half;insertLineText(line,new_line_text);}
function removeText(line,start,end){var lineText=getLinePlainText(line,0,getLineLength(line));var removedText=lineText.substring(start,end);line_first_half=lineText.substring(0,start);line_second_half=lineText.substring(end);line_first_half=text_tools.XmlFormat(line_first_half);line_first_half=line_first_half.replace(/ /g,"&nbsp;");line_second_half=text_tools.XmlFormat(line_second_half);line_second_half=line_second_half.replace(/ /g,"&nbsp;");var new_line_text=line_first_half+line_second_half;insertLineText(line,new_line_text);return removedText;}
function getLinePlainText(line,start,end){var removedColor=line.innerHTML.toString();var removedColor=removedColor.replace(/<[a-z0-9\=\"\'\ \-\/\:\%\&\._\?]*>/ig,"");var lineText=text_tools.UnXmlFormat(removedColor);if(end!=null)
line_plain_text=lineText.substring(start,end);else
line_plain_text=lineText.substring(start);return line_plain_text;}
function getLineText(line,start,end){var line_plain_text=getLinePlainText(line,start,end);line_plain_text=text_tools.XmlFormat(line_plain_text);line_plain_text=line_plain_text.replace(/ /g,"&nbsp;");return line_plain_text;}
function getLineLength(line){if(line!=null)
return getLinePlainText(line,0).length;}
function getLineIdNumber(line){if(line!=null)
return parseInt(line.id.toString().substring(1));else
return-1;}
function getLine(num){if(num>0)
return document.getElementById("l"+num);}
function jump_to_line(num){var nextline=getLine(num);if(nextline!=null){if(active_line!=null)
active_line.className="editorline";nextline.className="currentlinehighlight";active_line=nextline;var next_line_len=getLineLength(nextline);if(active_column>next_line_len){active_column=next_line_len;}
positionCaret();}}
function goto_line(num)
{this.cue="Goto Line: ";jump_to_line(num);scroll_to_center();positionCaret();}
function window_height(){return Sortie.Util.Browser.Explorer?document.body.clientHeight:window.innerHeight;}
function window_width(){return Sortie.Util.Browser.Explorer?document.body.clientWidth:window.innerWidth;}
function get_visible_lines(){var lines_per_window=parseInt(window_height()/LINEHEIGHT);return lines_per_window;}
function find_file(filename){this.cue="Find file: ";if(filename!=null&&filename!=""&&filename!="null"){if(filename.indexOf("://")>-1){}else{launch9ne(DEFAULT_WIDTH,DEFAULT_HEIGHT,filename);}}}
function file_line_count(){return number_of_lines;}
function kill_line_fragment(start,end){var fragment="";if(active_line!=null){var lineText=getLinePlainText(active_line,start,end);removeText(active_line,start,end);fragment=lineText;}
return fragment;}
function flash_markers(start,end){setTimeout(function(){displayCaretAtPosition(editor_second_caret,start);setTimeout(function(){displayCaretAtPosition(editor_second_caret,end)
setTimeout(function(){editor_second_caret.style.top="-10000px";editor_second_caret.style.left="-10000px";},50)},800);},100);}
function isBefore(mark1,mark2){if(getLineIdNumber(mark1.line)<getLineIdNumber(mark2.line)){return true;}
if(getLineIdNumber(mark1.line)==getLineIdNumber(mark2.line)){if(mark1.column<mark2.column){return true;}}
return false;}
function moveCaretToMark(mark)
{active_column=mark.column;jump_to_line(getLineIdNumber(mark.line));}
function editor_addLine()
{var total_num_line=editor_text_area.childNodes.length;var active_line_id=getLineIdNumber(active_line);var line_remainder=kill_line();line_remainder=(line_remainder!=null)?line_remainder:"";var new_line=document.createElement("DIV");new_line.className="editorline";line_remainder=text_tools.XmlFormat(line_remainder);line_remainder=line_remainder.replace(/ /g,"&nbsp;");insertLineText(new_line,line_remainder);if(active_line_id==-1||active_line_id==total_num_line){new_line.setAttribute("id","l"+(++number_of_lines));var new_gutter=document.createElement("DIV");new_gutter.className="gutteritem";new_gutter.appendChild(document.createTextNode(number_of_lines));editor_gutter_area.appendChild(new_gutter);editor_text_area.appendChild(new_line);jump_to_line(number_of_lines);recalculateLines();scroll_down();}else{var nextline=getLine((active_line_id+1));new_line.setAttribute("id","tl"+(active_line_id+1));editor_text_area.insertBefore(new_line,nextline);number_of_lines++;recalculateLines();if(Sortie.Util.Browser.Safari){active_line.className="editorline";FIX_SAFARI_JumpCaretAfterNewLine();}
nextline=getLine((active_line_id+1));active_column=0;jump_to_line(active_line_id+1);if(screen_tools.FindPosY(editor_caret)>((get_visible_lines()-ELECTRIC_LINE)*LINEHEIGHT)){scroll_up();}}
editor_gutter_area.style.height=editor.scrollHeight+"px";}
function editor_backspace()
{var rt=delete_backward_char();isdirty=true;}
function remove_line(linenum)
{var target=document.getElementById("l"+linenum);if(target!=null)
{editor_text_area.removeChild(target);number_of_lines--;recalculateLines();}}
function eval_expression(exp){this.cue="Eval: ";DEBUG=true;log.init();eval(exp+"");}
function recalculateLines(){editor_gutter_area.innerHTML="";var chillin=editor_text_area.childNodes;var chillen=chillin.length;for(var u=0;u<chillen;u++){var tline=chillin.item(u);tline.setAttribute("id","l"+(u+1));var new_gutter=document.createElement("DIV");new_gutter.className="gutteritem";new_gutter.appendChild(document.createTextNode((u+1)));editor_gutter_area.appendChild(new_gutter);}}
function FIX_SAFARI_JumpCaretAfterNewLine(){var tmp=editor_text_area.innerHTML;editor_text_area.innerHTML="";editor_text_area.innerHTML=tmp;tmp=null;}
function append_minibuffer_text(text){editor_minibuffer.value+=text;}
function append_minibuffer_binding(text){append_minibuffer_text(text+" ");}
function minibuffer_message(text){editor_minibuffer.value=text;setTimeout("clear_minibuffer()",1000);}
function run_minibuffer_command(cmd){clear_minibuffer();if(cmd.indexOf("_mode")>0){var arr=cmd.split("_");var mode=arr[0];loadModeFile(mode);}else{var ns="";if(typeof window[cmd]!="undefined"){eval("buffer_command = new "+cmd+"().cue");ns="window.";}else if(window.extensions[cmd]!="undefined"){eval("buffer_command = new window.extensions."+cmd+"().cue");ns="window.extensions.";}
if(buffer_command!=null){var args=prompt(buffer_command);if(args.indexOf("[")==0||args.indexOf("{")==0)
eval(ns+cmd+"("+args+")");else
eval(ns+cmd+"('"+args+"')");}}}
function fundamental_mode(){resetMode()
show_editor_message("applying fundamental mode");if(window.fun_org_bindings==null){window.fun_org_bindings=new Object();for(var i in BINDINGS){window.fun_org_bindings[i]=BINDINGS[i];}}else{BINDINGS=new Object();for(var i in window.fun_org_bindings){BINDINGS[i]=window.fun_org_bindings[i];}}
window.editor_new_line=function(){editor_addLine();active_column=0;isdirty=true;}
window.clear_minibuffer=function(){editor_minibuffer.value="";buffer_command="";}
window.eval_line=function(){var linetxt=getLinePlainText(active_line,0,getLineLength(active_line));eval(linetxt);}
window.eval_region=function(){copy_region();eval(clipboard);}
window.eval_buffer=function(){try{eval(get_editor_text());}catch(e){minibuffer_message(e);}}
window.insert_tab=function(){addStringToCurrentPosition(TAB);document.getElementById('editor').focus();}
window.delete_char=function(){if(active_column==getLineLength(active_line)){if(getLineIdNumber(active_line)==file_line_count())
return;var nextline=getLine(getLineIdNumber(active_line)+1);var nextlinetext=getLineText(nextline,0,getLineLength(nextline));remove_line(getLineIdNumber(active_line)+1);jump_to_line(getLineIdNumber(active_line));positionCaret();var savepos=active_column;addStringToCurrentPosition(nextlinetext);active_column=savepos;}else{removeText(active_line,active_column,(active_column+1));positionCaret();}}
window.beginning_of_buffer=function(){goto_line(1);beginning_of_line();}
window.end_of_buffer=function(){goto_line(number_of_lines);end_of_line();}
window.backward_char=function(){if(active_column!=0)
active_column--;else{previous_line();active_column=getLineLength(active_line);}
positionCaret();}
window.end_of_line=function(){active_column=getLineLength(active_line);}
window.beginning_of_line=function(){active_column=0;}
window.next_line=function(){var current_line_num=getLineIdNumber(active_line);jump_to_line(current_line_num+1);var visible_lines=get_visible_lines();if(screen_tools.FindPosY(editor_caret)>((visible_lines-ELECTRIC_LINE)*LINEHEIGHT)){scroll_up();}}
window.scroll_up=function(){var current_line_num=getLineIdNumber(active_line);var visible_lines=get_visible_lines();editor.scrollTop=(((current_line_num-1)-(visible_lines-ELECTRIC_LINE))*LINEHEIGHT);positionCaret();}
window.previous_line=function(){var current_line_num=getLineIdNumber(active_line);jump_to_line(current_line_num-1);var visible_lines=(window_height()/14)-5;if(screen_tools.FindPosY(editor_caret)<4*14)
scroll_down();}
window.scroll_down=function(){var current_line_num=getLineIdNumber(active_line);var visible_lines=get_visible_lines();editor.scrollTop=(current_line_num-ELECTRIC_LINE)*LINEHEIGHT;positionCaret();}
window.forward_char=function(){if(getLineLength(active_line)>active_column)
active_column++;else{if(getLineIdNumber(active_line)<number_of_lines){next_line();active_column=0;}}
positionCaret();}
window.delete_backward_char=function(){var rt="";if(active_column!=0){rt=removeText(active_line,(active_column-1),active_column);active_column--;}else{var linenum=getLineIdNumber(active_line);if(linenum!=1){var text_on_line=kill_line();remove_line(linenum);jump_to_line(linenum-1);save_len=getLineLength(getLine(linenum-1));active_column=save_len;addStringToCurrentPosition(text_on_line);active_column=save_len;}
rt="\n";}
positionCaret();return rt;}
window.scroll_to_line=function(){var current_line_num=getLineIdNumber(active_line);var visible_lines=get_visible_lines();editor.scrollTop=((current_line_num-1)*LINEHEIGHT);}
window.scroll_to_center=function(){var current_line_num=getLineIdNumber(active_line);var visible_lines=get_visible_lines();editor.scrollTop=(((current_line_num)-(visible_lines>>1))*LINEHEIGHT);}
window.yank=function(){var toeol=kill_line();var pclipboard=text_tools.XmlFormat(clipboard);pclipboard=pclipboard.replace(/ /g,"&nbsp;");var lines=new Array();lines=pclipboard.split("\n");for(var z=0;z<lines.length;z++){if(z>0){editor_addLine();insert_text(lines[z]);}else{insert_text(lines[z]);}}
insert_text(toeol);}
window.insert_text=function(text){addText(active_line,active_column,text);active_column+=text_tools.UnXmlFormat(text).length;positionCaret();}
window.save=function(){if(linked_textarea!=null){linked_textarea.value=get_editor_text();minibuffer_message("Saved: "+buffer_name);isdirty=false;}else{minibuffer_message("Save not implemented yet!");}}
window.get_editor_text=function(){var tempbuffer="";for(var z=1;z<=number_of_lines;z++){var line=getLine(z);var templine=getLinePlainText(line,0,getLineLength(line))+"\n";tempbuffer+=templine}
return tempbuffer;}
window.kill_line=function(){var toEOL="";if(active_line!=null){var lineText=getLinePlainText(active_line,0,getLineLength(active_line));toEOL=lineText.substring(active_column);removeText(active_line,active_column,lineText.length);}
return toEOL;}
window.kill_start_line=function(){var toBOL="";if(active_line!=null){var lineText=getLinePlainText(active_line,0,active_column);removeText(active_line,0,active_column);toBOL=lineText;}
return toBOL;}
window.kill_9ne=function(){stopRepeatKey();if(isdirty){if(!confirm("Modified buffers exist; exit anyway?")){editor_focus();return false;}}
if(linked_textarea!=null){linked_textarea.disabled=false;linked_textarea.style.background="white";}
runOnEnd();window.close();return true;}
window.editor_set_mark=function(){active_mark=new Mark();active_mark.line=active_line;active_mark.column=active_column;minibuffer_message("Mark Set");}
window.copy_region=function(){if(active_mark!=null){var startlineid=getLineIdNumber(active_mark.line);var startcolumn=active_mark.column;var endlineid=getLineIdNumber(active_line);var endcolumn=active_column;kill_region();yank();var start_mark=new Mark();start_mark.line=getLine(startlineid);start_mark.column=startcolumn;var end_mark=new Mark();end_mark.line=getLine(endlineid);end_mark.column=endcolumn;if(isBefore(end_mark,start_mark)){moveCaretToMark(end_mark);}
flash_markers(start_mark,end_mark);}}
window.kill_region=function(){var current=new Mark();current.line=active_line;current.column=active_column;var start_line=parseInt(getLineIdNumber(active_mark.line));var end_line=parseInt(getLineIdNumber(current.line));var numlines=start_line-end_line;var tclipboard="";if(numlines>0){var finalmark=new Mark();finalmark.line=active_line;finalmark.column=active_column;tclipboard+=kill_line()+"\n";if(active_column==0){remove_line(end_line);goto_line(end_line);}
else goto_line(++end_line);active_column=0;positionCaret();numlines--;while(numlines>0){tclipboard+=kill_line()+"\n";remove_line(end_line);goto_line(end_line);positionCaret();numlines--;}
active_column=active_mark.column;positionCaret();tclipboard+=kill_start_line();active_line=finalmark.line;active_column=finalmark.column;positionCaret();clipboard=tclipboard;}else if(numlines<0){goto_line(start_line);active_column=active_mark.column;tclipboard+=kill_line()+"\n";if(active_column==0){remove_line(start_line);goto_line(start_line);}
else goto_line(++start_line);active_column=0;positionCaret();numlines--;while(numlines<-2){tclipboard+=kill_line()+"\n";remove_line(start_line);goto_line(start_line);positionCaret();numlines++;}
active_column=current.column;positionCaret();tclipboard+=kill_start_line();goto_line(getLineIdNumber(active_mark.line));active_column=active_mark.column;positionCaret();clipboard=tclipboard;}else{if(active_column<active_mark.column)
tclipboard=kill_line_fragment(active_column,active_mark.column);else{tclipboard=kill_line_fragment(active_mark.column,active_column);active_column=active_mark.column;positionCaret();}
clipboard=tclipboard;}}}
var key_repeat_timer=null;var key_delay_timer=null;var KEY_DELAY=200;var KEY_REPEAT=60;var repeat_function=null;var repeat_count=0;function __threadKeySpinner(){if(repeat_function!=null){var tmp=new repeat_function();tmp=null;}}
function runKeyRepeat(){cursor_solid();key_repeat_timer=setInterval("__threadKeySpinner()",KEY_REPEAT);}
function keyWithRepeat(func){var tmp=new func();repeat_function=func;key_delay_timer=setTimeout("runKeyRepeat(); clearTimeout(key_delay_timer); key_delay_timer = null;",KEY_DELAY);}
function stopRepeatKey(){clearTimeout(key_delay_timer);clearInterval(key_repeat_timer);repeat_function=null;key_delay_timer=null;key_repeat_timer=null;cursor_blink();}
function isControlKey(evt)
{if(evt.ctrlKey)
return true;return false;}
function isMetaKey(evt)
{if(evt.altKey)
return true;return false;}
function isInChord()
{return editor_minibuffer.value.toString().length>0;}
function editor_onKeyPress(event)
{if(editor_in_focus)
{var keycode=event_manager.GetKeyCodeFromEvent(event);var asciikey=event_manager.GetKeyFromEvent(event);switch(keycode)
{case 13:if(!minibuffer_focus)
{}
break;case 27:case 8:if(!minibuffer_focus)
{}
event.cancelBubble=true;event.returnValue=false;return false;break;case 63272:case 63232:case 63235:case 63234:case 63233:event.cancelBubble=true;event.returnValue=false;return false;case 35:case 36:if(Sortie.Util.Browser.Safari)
{event.cancelBubble=true;event.returnValue=false;return false;}
case 0:break;case 16:case 14:case 6:case 2:case 112:case 110:case 102:case 98:default:if(isMetaKey(event)||isControlKey(event)||isInChord())
break;if(!minibuffer_focus)
{addCharCodeToPosition(asciikey);isdirty=true;}}
positionCaret();}}
function leaveMiniBuffer()
{editor_focus();minibuffer_focus=false
clear_minibuffer();}
function editor_onKeyDown(event)
{if(editor_in_focus)
{var keycode=event_manager.GetKeyCodeFromEvent(event);var asciikey=event_manager.GetKeyFromEvent(event);window.status="d: "+keycode;if(minibuffer_focus)
{switch(keycode)
{case 9:case 32:buffer_command+="-";append_minibuffer_text("-");event.cancelBubble=true;event.returnValue=false;return false;break;case 8:buffer_command=buffer_command.toString().substring(0,(buffer_command.toString().length-1));var mbv=editor_minibuffer.value.toString();editor_minibuffer.value=mbv.substring(0,(mbv.length-1));break;case 13:buffer_command=buffer_command.toLowerCase();buffer_command=buffer_command.replace(/\-/g,"_");try{run_minibuffer_command(buffer_command);}catch(e){minibuffer_message("Command Unknown! "+e);}
setTimeout("leaveMiniBuffer();",500);event.cancelBubble=true;event.returnValue=false;return false;break;case 27:case 112:case 113:case 114:case 115:case 116:case 117:case 118:case 119:event.cancelBubble=true;event.returnValue=false;return false;break;case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:default:var dsp="";dsp+=asciikey;buffer_command+=asciikey;append_minibuffer_text(dsp);}}
else
{var current_stroke=(event.ctrlKey)?"CTRL+":"";current_stroke+=(event.altKey)?"ALT+":"";current_stroke+=(event.shiftKey)?"SHIFT+":"";var binding_lookup=(editor_minibuffer.value!="")?(editor_minibuffer.value):""
switch(keycode)
{case 27:current_stroke+="ESC";break;case 9:current_stroke+="TAB";break;case 35:current_stroke+="END";break;case 36:current_stroke+="HOME";break;case 38:current_stroke+="UP";break;case 39:current_stroke+="RIGHT";break;case 37:current_stroke+="LEFT";break;case 40:current_stroke+="DOWN";break;case 188:current_stroke+="COMMA";break;case 190:current_stroke+="PERIOD";break;case 51:current_stroke+="3";break;case 52:current_stroke+="4";break;case 46:current_stroke+="DEL";break;case 112:current_stroke+="F1";break;case 113:current_stroke+="F2";break;case 114:current_stroke+="F3";break;case 115:current_stroke+="F4";break;case 116:current_stroke+="F5";break;case 117:current_stroke+="F6";break;case 118:current_stroke+="F7";break;case 119:current_stroke+="F8";break;case 219:current_stroke+="OPENBRACE";break;case 221:current_stroke+="CLOSEBRACE";break;case 220:current_stroke+="BACKSLASH";break;case 191:current_stroke+="FORWARDSLASH";break;case 192:current_stroke+="GRAVE";break;case 189:current_stroke+="MINUS";break;case 187:current_stroke+="EQUALS";break;case 186:current_stroke+="SEMICOLON";break;case 222:current_stroke+="QUTOE";break;case 13:current_stroke+="ENTER";break;case 8:current_stroke+="BACKSPACE";break;case 32:current_stroke+="SPACE";if(!Sortie.Util.Browser.Explorer)
{event.cancelBubble=true;event.returnValue=false;}
break;default:current_stroke+=asciikey;}
binding_lookup+=current_stroke;var global_quit=BINDINGS["_GLOBAL_CANCEL_"];if(current_stroke==global_quit){eval(BINDINGS[BINDINGS["_GLOBAL_CANCEL_"]]);return false;}
try{var macro=BINDINGS[binding_lookup];if(typeof macro!="undefined"){eval(macro);}else{if(!Sortie.Util.Browser.Explorer&&!(isControlKey(event)||isMetaKey(event))){}}
textarea_bucket.value="";event.cancelBubble=true;event.returnValue=false;return false;}catch(e){minibuffer_message("Error: "+e);}}
positionCaret();}}
function editor_onKeyUp(event)
{stopRepeatKey();}
function ModeRule(){this.name="";this.regex=null;this.replace=null;}
function loadModeFile(filename){resetMode();show_editor_message("Loading Mode: "+filename);var pipe2=new Sortie.IO.Pipe().GetInstance();var gateway2=new Sortie.IO.Gateway(pipe2,true);gateway2.DoRequest({method:"GET",url:"modes/"+filename+"/Mode.js",handler:function(e){eval(e.responseText);show_editor_message("Running Mode Code");},error_handler:function(e){show_editor_message("Error Loading Mode. ("+e.status+")")}});var pipe=new Sortie.IO.Pipe().GetInstance();var gateway=new Sortie.IO.Gateway(pipe,true);gateway.DoRequest({method:"GET",url:"modes/"+filename+"/Rules.properties",handler:function(e){var props=new Sortie.Util.Properties();props.Parse(e.responseText);modePropertiesToRules(props);repaint_text();},error_handler:function(e){show_editor_message("Error Loading Mode. ("+e.status+")")}});}
function modePropertiesToRules(props){var keyarray=new Array();keyarray=props.PropertyNames();window.mode_syntax_rules=new Sortie.Util.Map();for(var j=0;j<keyarray.length;j+=3)
{var ruleregex=new RegExp(props.GetProperty(keyarray[j]),props.GetProperty(keyarray[j+1]));var replace=props.GetProperty(keyarray[j+2]);var name=keyarray[j].substring(0,keyarray[j].indexOf("."));var mr=new ModeRule();mr.name=name;mr.regex=ruleregex;mr.replace=replace;window.mode_syntax_rules.Put(name,mr);}}
function resetMode(){mode_syntax_rules=new Sortie.Util.Map();lineInsertListeners=new Array();}