Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 22nd, 2012  |  syntax: None  |  size: 5.56 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function ready_handler(){
  2.         get_elem('content').focus();
  3. //      handle_null();
  4. }
  5.  
  6. // ==========================================================
  7. // Some utils
  8. function get_elem(x){
  9.         return document.getElementById(x);
  10. }
  11.  
  12. function insert_elem_at_caret(elem){
  13.     window.getSelection().getRangeAt(0).insertNode(elem);
  14. }
  15.  
  16. function move_beyond(){
  17.         var sel = window.getSelection();
  18.         var ran = sel.getRangeAt(0);
  19.         ran.collapse(true);
  20.         if(ran.startContainer.nodeType != 3) ran.setStart(ran.startContainer, ran.startOffset + 1);
  21.         else ran.setStart(ran.startContainer.nextSibling.nextSibling, 0);
  22.         sel.removeAllRanges();
  23.         sel.addRange(ran);
  24. }
  25.  
  26. function keydown_handler(e){
  27.         e.stopPropagation();
  28.         if(e.which == '13'){
  29.         e.preventDefault();
  30.         var x = document.createTextNode('\n');
  31.         insert_elem_at_caret(x);
  32.         move_beyond();
  33.         }
  34.         if(e.which == '9'){
  35.         e.preventDefault();
  36.         var x = document.createTextNode('\t');
  37.         insert_elem_at_caret(x);
  38.                 move_beyond();
  39.         }
  40.         window.setTimeout('sub()', 0);
  41. }
  42.  
  43. function keypress_handler(e){
  44.        
  45. }
  46.  
  47. function block_match(string){
  48.  
  49.         return  string
  50.                         .replace(func.patt_reg, func.patt_repl)
  51.                         .replace(func.post_reg, func.post_repl)
  52.                         .replace(func.post_reg2, func.post_repl);
  53. }
  54.  
  55. function global_key_match(string){
  56.         return  string
  57.                         .replace(key.regex, key.style)
  58.                         .replace(type.regex, type.style)
  59.                         .replace(post.regex_global, post.style);
  60. }
  61.  
  62. function local_key_match(string){
  63.         return  string
  64.                         .replace(key.regex, key.style)
  65.                         .replace(type.regex, type.style)
  66.                         .replace(post.regex_local, post.style);
  67. }
  68.  
  69. // This portion should be placed in another file and dynamically loaded in the future.
  70. var func = {    patt_reg:       /((?:[a-zA-Z_][a-zA-Z_0-9]*\s+){1,3})([a-z_][a-z_A-Z0-9]+\s*)(\([^\)]*\)\s*)(\{[^\{\}]*\}\s*)/g,
  71.                                 patt_repl:      "<func>$1$2$3$4</func>",
  72.                                 post_reg:       /((?:<\/func>)|^)([^<]+)(?=<func>)/g,
  73.                                 post_reg2:      /(^)([^<>]+)/g,
  74.                                 post_repl:      "$1<nofunc>$2</nofunc>"
  75.                         };
  76.                        
  77. var key = {     regex: /(\W|^)(if|switch|for|while|break|case|continue|default|do|else|enum|goto|return|sizeof|struct|typedef|union)(\W|$)/g,
  78.                                 style:"$1<key>$2</key>$3"
  79. };
  80.  
  81. var type = {    regex: /((?:const|volatile|static|extern)\s+)?((?:short|long|(?:un)?signed)\s+)?((?:int|float|double|char|void|enum)\s+)/g,
  82.                                 style:  "<type>$1$2$3</type>"
  83. };
  84.  
  85. var post = {    regex_global: /((?:func>)|(?:\/[^>]+>))([^<]+)/g,
  86.                                 regex_local: /(^|(?:\/[^>]+>))([^<]+)/g,
  87.                                 style: "$1<flat>$2</flat>"
  88. }
  89.  
  90. function get_outer_offset(elem){
  91.         var curr = window.getSelection().getRangeAt(0);
  92.         curr.setStart(elem, 0);
  93.         return curr.toString().length;
  94. }
  95.  
  96. // Here I use some weird walk around.
  97. // the regex (<[^>]*>)+ is supposed to match all countinuous HTML tag,
  98. // but the opening tag of each element appears in the result.
  99. // So I have to use match instead, however, for each match, there will
  100. // be two extra bracket. Since we just want the length, simply subtract
  101. // them in the following calculation
  102. function get_pos(elem, elem_offset){
  103.         var content     = elem.innerHTML.match(/>([^<]+)</g);
  104.         if(content){
  105.                 var     len     = content.length,
  106.                         accum   = 0;
  107.                 for(i = 0; i < len; i++){
  108.                         accum += content[i].length -2;
  109.                         if(accum >= elem_offset) break;
  110.                 }
  111.        
  112.                 var off = elem_offset - (accum - (content[i].length - 2));
  113.                 return {ith:    i,
  114.                                 offset: off
  115.                                 }
  116.         }
  117. }
  118.  
  119. function set_pos(elem, offset){
  120.         var     ran = document.createRange(),
  121.                 sel = window.getSelection();
  122. //      get_elem('container2').textContent = get_elem('content').innerHTML;
  123. //      get_elem('container3').textContent = pos.offset;
  124.         ran.collapse(false);
  125.         ran.setStart(elem, offset);
  126.         sel.removeAllRanges();
  127.         sel.addRange(ran);
  128. }
  129.  
  130. // determine whether a global match is needed by checking the number of blocks
  131. // in the whole content.
  132. function block_number_check(con){
  133.         return con.children.length == block_match(con.innerHTML).match(/\/(no)?func>/g).length;
  134. }
  135.  
  136.  
  137. function sub(){
  138.         var con = get_elem('content'),
  139.                 con2 = get_elem('container2'),
  140.                 con3 = get_elem('container3'),
  141.                 con_original = con.textContent;
  142.                 con_blockized = block_match(con.textContent);
  143.  
  144.  
  145.         if(!con.children.length){       // Handles the first character
  146. //              alert("hey!");
  147.                 con.innerHTML = "<nofunc><flat>"+ con.textContent +"\n</flat></nofunc>";
  148.                 set_pos(con.firstChild.firstChild, 1);
  149.                 con2.textContent = con.innerHTML;
  150.                
  151.         }else if(con.children.length == con_blockized.match(/\/(no)?func>/g).length){
  152.                 // Handles the block-inside matching
  153.  
  154.                 //1. get the block-inside offset
  155.                 var caret = window.getSelection().getRangeAt(0);
  156.                 var block = caret.startContainer;
  157.                 con2.textContent = block.parentNode;
  158.                 while(block.tagName != 'FUNC' && block.tagName != 'NOFUNC')
  159.                         block = block.parentNode;
  160.                 caret.setStart(block, 0);
  161.                 var len = caret.toString().length;
  162.                
  163.                 //2. then apply the new style
  164.                 block.innerHTML = local_key_match(block.textContent);
  165.                 con2.textContent = con.innerHTML;
  166.                 var pos = get_pos(block, len);
  167.                
  168.                 //3. Plug the caret back to the original place
  169.                 set_pos(block.children[pos.ith].firstChild, pos.offset);
  170.        
  171.         }else{
  172.                 // Oops ... we have to match the whole content
  173.                
  174.                 //1. Get the global ofset
  175.                 var caret = window.getSelection().getRangeAt(0);
  176.                 var block = caret.startContainer;
  177.  
  178.                 while(block.tagName != 'DIV')
  179.                         block = block.parentNode;
  180.                 caret.setStart(block, 0);
  181.                 var len = caret.toString().length;
  182.                 con3.textContent = len;
  183.                 //2. Get the block index
  184.                 block.innerHTML = block_match(block.textContent);
  185.                 var block_pos = get_pos(block, len);
  186.                
  187.                 //3. Get the inside-block tag index
  188.                 block.innerHTML = global_key_match(block.innerHTML);
  189.                 var inner_pos = get_pos(block.children[block_pos.ith], block_pos.offset);
  190.                 set_pos(block.children[block_pos.ith].children[inner_pos.ith].firstChild, inner_pos.offset);
  191.         }
  192.  
  193.  
  194. }