-
Notifications
You must be signed in to change notification settings - Fork 1
/
pta_min.js
15 lines (15 loc) · 6.68 KB
/
pta_min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var Pta=Pta||{};
Pta.listeners={initialize:function(a){a=document.getElementById(a);Pta.listeners.setTabs(a);a.style.tabSize=4;Pta.listeners.setIndent(a);Pta.listeners.setJoinLines(a);Pta.listeners.setMatching(a);Pta.listeners.setDupChr(a);Pta.listeners.setZen(a);Pta.listeners.setLineNbr(a)},setTabs:function(a){a.addEventListener("keydown",function(b){if(9===b.keyCode&&!b.shiftKey){b.preventDefault();var d=a.selectionStart,e=a.selectionEnd;var c=a.value;if(d===e)a.value=c.slice(0,d)+"\t"+c.slice(e),a.selectionStart=
d+1,a.selectionEnd=d+1;else{b=c.slice(d,e);var g=Pta.countNewLines(b);if(10!==c.charCodeAt(e-1))for(e=b.length;;){g=a.selectionEnd;var f=c.indexOf(b,g);d=f+e;if(0<=f)a.selectionStart=f,a.selectionEnd=d;else if(0<g){a.selectionStart=0;a.selectionEnd=0;continue}return}b=b.replace(/\s+$/g,"");b=b.replace(RegExp("\n","g"),"\n\t");f=c.slice(0,d);c=c.slice(e);a.value=f+"\t"+b+"\n"+c;a.selectionStart=d;a.selectionEnd=e+g}}});a.addEventListener("keydown",function(b){if(9===b.keyCode&&b.shiftKey){b.preventDefault();
var d=a.selectionStart,e=a.selectionEnd;var c=a.value;if(d===e)b=c.slice(0,d-1),c=c.slice(d),a.value=b+c,a.selectionStart=d-1,a.selectionEnd=d-1;else{var g=c.slice(d,e),f=Pta.countNewLines(g);32<g.charCodeAt(0)||(g=g.replace(RegExp("\n\t","g"),"\n"),b=c.slice(0,d),c=c.slice(e),a.value=b+g.slice(1)+c,a.selectionStart=d,a.selectionEnd=e-f)}}})},setJoinLines:function(a){a.addEventListener("keydown",function(b){if("t"===String.fromCharCode(b.which).toLowerCase()&&b.altKey){b.preventDefault();var d=a.selectionStart;
var e=a.selectionEnd;var c=a.value;b=c.slice(d,e);if(""===b)alert("No Lines Selected to Join");else{var g=c.slice(0,d);e=c.slice(e);b=b.replace(/\n/g,"");a.value=g+b+e;a.selectionEnd=d}}})},setIndent:function(a){a.addEventListener("keydown",function(b){if(13===b.keyCode&&!b.ctrlKey){b.preventDefault();var d,e=a.value,c=a.selectionStart,g=a.selectionEnd;for(b=c-1;0<b&&10!==e.charCodeAt(b);--b);var f=String.fromCharCode(10);0<b&&(b+=1);for(d=e.charCodeAt(b);32===d||9===d;)f+=String.fromCharCode(d),
b+=1,d=e.charCodeAt(b);a.value=e.slice(0,c)+f+e.slice(g);a.selectionStart=c+f.length;a.selectionEnd=c+f.length}})},setMatching:function(a){function b(){var c=a.value,b=a.selectionStart,f=c.charAt(b);if(0>"{(".indexOf(f)&&(--b,f=c.charAt(b),0>"})".indexOf(f)))return;"}"===f?d("}","{",b):"{"===f?e("{","}",b):")"===f?d(")","(",b):"("===f&&e("(",")",b)}function d(c,b,e){var d=0,f=a.value;for(--e;0<=e;--e){var g=f.charAt(e);if(g===b)if(0===d){a.selectionStart=e;break}else--d;else g===c&&(d+=1)}0>e&&alert("Match not found")}
function e(c,e,b){var d=0,f=a.value;var g=f.length;for(b+=1;b<g;b+=1){var m=f.charAt(b);if(m===e)if(0===d){a.selectionEnd=b+1;break}else--d;else m===c&&(d+=1)}b>=g&&alert("Match not found")}a.addEventListener("keydown",function(a){"q"===String.fromCharCode(a.which).toLowerCase()&&a.altKey&&(a.preventDefault(),b())});a.addEventListener("click",function(a){a.ctrlKey&&(a.preventDefault(),b())})},setDupChr:function(a){a.addEventListener("keydown",function(b){if("d"===String.fromCharCode(b.which).toLowerCase()&&
b.altKey){b.preventDefault();var d;b=a.value;var e=a.selectionStart,c=a.selectionEnd,g=0;for(d=e-1;0<d&&(g+=1,10!==b.charCodeAt(d));--d);for(;0<d&&(--d,10!==b.charCodeAt(d)););10!==b.charCodeAt(d)&&--g;d=b.charAt(d+g);a.value=b.slice(0,e)+d+b.slice(c);a.selectionStart=e+1;a.selectionEnd=e+1}})},setZen:function(a){function b(b){var c=a.selectionStart;var e=a.selectionEnd,f=a.value;var h=f.slice(c,e);var k=f.slice(0,c);var l=f.slice(e);c===e?alert("nothing selected"):(0===b?d=c=prompt("Enter tag abbreviation\n or command"):
c=d,null!==c&&""!==c&&("/*"===c?(b="/* ",c=" */"):"<!"===c?(b="\x3c!-- ",c=" --\x3e"):"ucase"===c?(c=b="",h=h.toUpperCase()):"lcase"===c?(c=b="",h=h.toLowerCase()):'"'===c?c=b='"':"'"===c?c=b="'":"$"===c?(b="${",c="}"):(b="<"+c+">",c="</"+c+">"),f=k+b+h+c+l,a.value=f,a.selectionEnd=f.length-l.length,a.selectionStart=f.length-l.length))}var d="";a.addEventListener("keydown",function(b){if("z"===String.fromCharCode(b.which).toLowerCase()&&b.altKey){a:{var c=a.selectionStart;var e=a.selectionEnd;var d=
a.value;var h=d.slice(c,e);if(""===h){for(;e<d.length&&!(49>d.charCodeAt(e));)e+=1;for(;0<c;)if(--c,49>d.charCodeAt(c)){c+=1;break}h=d.slice(c,e)}if(""===h)alert("No Key Word to Match");else{var k=d.slice(0,c);d=d.slice(e);for(e=0;e<atags.length;e+=1)if(h===atags[e].tag){h=k+atags[e].tagx+d;c=h.indexOf("^",c);-1===c?(a.value=h,a.selectionEnd=h.length-d.length,a.selectionStart=h.length-d.length):(k=h.slice(0,c),d=h.slice(c+1),a.value=k+d,a.selectionEnd=c,a.selectionStart=c);break a}d=k+"<"+h+"></"+
h+">"+d;c=d.indexOf("><",c);c+=1;a.value=d;a.selectionEnd=c;a.selectionStart=c}}b.preventDefault()}});a.addEventListener("keydown",function(a){"a"===String.fromCharCode(a.which).toLowerCase()&&a.altKey&&(b(0),a.preventDefault())});a.addEventListener("keydown",function(a){"w"===String.fromCharCode(a.which).toLowerCase()&&a.altKey&&(b(1),a.preventDefault())})},setLineNbr:function(a){var b="",d;a.addEventListener("keydown",function(e){if("n"===String.fromCharCode(e.which).toLowerCase()&&e.altKey)if(e.preventDefault(),
""===b){d="1\t\t";e=2;var c,g,f=a.value;b=f;for(c=0;c<f.length-1;c+=1){var h=f.charAt(c);if("\n"===h){for(g=e.toString();4>g.length;)g+=" ";d+=h+g+"\t\t";e+=1}else d+=h}a.value=d;a.readOnly=!0}else a.readOnly=!1,a.value=b,b=""})}};
Pta.findr={findText:function(a,b){if(2!==arguments.length)alert("missing arguments for findText");else{var d=document.getElementById(a),e=document.getElementById(b);var c=e.value.length;var g=d.selectionEnd,e=d.value.indexOf(e.value,g);c=e+c;0<=e?(d.selectionStart=e,d.selectionEnd=c,d.focus()):0<g&&(d.selectionStart=0,d.selectionEnd=0,Pta.findr.findText(a,b))}},replaceOne:function(a,b,d){if(3!==arguments.length)alert("missing arguments for findText");else{var e=document.getElementById(a),c=document.getElementById(d);
Pta.findr.findText(a,b);var g=e.selectionStart;var f=e.selectionEnd;var h=e.value;g===f?alert("No More to replace"):(g=h.slice(0,g),f=h.slice(f),h=g+c.value+f,e.value=h,e.selectionStart=h.length-f.length,e.selectionEnd=h.length-f.length,e.focus())}},replaceAll:function(a,b,d){if(3!==arguments.length)alert("missing arguments for findText");else{var e=document.getElementById(a),c=document.getElementById(b),g=document.getElementById(d);e.value=e.value.replace(new RegExp(c.value,"g"),g.value);e.focus()}},
adjustScroll:function(a,b){var d,e=0,c=document.getElementById(a);var g=c.value;var f=c.selectionEnd;for(d=0;d<f;d+=1)"\n"===g.charAt(d)&&(e+=1);c.scrollTop=e*b}};Pta.insClip=function(a,b){var d=document.getElementById(a),e=d.value,c=d.selectionStart,g=e.slice(0,c),e=e.slice(c);d.value=g+b+e;d.selectionEnd=c+b.length;d.focus()};Pta.countNewLines=function(a){var b,d=0;for(b=0;b<a.length;b+=1)"\n"===a.charAt(b)&&(d+=1);return d};