
/***************************************************************************
 *   Copyright (C) 2006, Paul Lutus                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

addEvent(window,"load",setup);

// function to add an event listener
function addEvent(o,e,f) {
  if (o.addEventListener) {
    o.addEventListener(e,f,false);
    return true;
  }
  else if (o.attachEvent) {
    return o.attachEvent("on"+e,f);
  }
  else {
    return false;
  }
}

var max_matches = 4000;

var color_tag = "<span class=\"match_highlight\">";

var default_data = "Gettysburg, Pennsylvania\nNovember 19, 1863\n\nFour score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.\n\nNow we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.\n\nBut, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.\n";

function setup() {
  document.data.input_text.value = default_data;
  //document.data.regex_search.focus();
}

function keypress(state,event) {
  var e = (window.event)?window.event:event;
  if(e.keyCode == 13) {
    process(state);
  }
  return true;
}

function unescape_chars(s,dot_all_option) {
  var shift = false;
  var len = s.length;
  var output = "";
  for(var i = 0;i < len;i++) {
    var c = s.substring(i,i+1);
    if(shift) {
      switch(c) {
        case "t":
          output += "\t";
        break;
        case "n":
          output += "\n";
        break;
        case "r":
          output += "\r";
        break;
        case "f":
          output += "\f";
        break;
        case "a":
          // JavaScript doesn't parse '\a' correctly
        output += "\07";
        break;
        case "e":
          output += "\e";
        break;
        case "b":
          output += "\b";
        break;
        case "v":
          output += "\v";
        break;
        case "0":
          output += "\0";
        break;
        // handle case of /c(control letter)
        case "c":
        if(i < len-1) {
          i++;
          output += String.fromCharCode(s.charCodeAt(i) & 0x1f);
        }
        break;
        default:
          output += "\\" + c;
        break;
      }
      shift = false;
    }
    else { // not shifted
      if(c == "\\") {
        shift = true;
      }
      else {
        // must synthesize nonexistent dot_all
        if(c == "." && dot_all_option) {
          output += "[\\s|\\S]";
        }
        else {
          output += c;
        }
      }
    }
  }
  return output;
}

function process(replace) {
  try {
    var output = "";
    search_val = document.data.regex_search.value;
    replace_val = document.data.regex_replace.value;
    dot_all_option = document.data.dot_all_rb.checked;
    global_option = document.data.global_rb.checked;
    case_option = document.data.case_rb.checked;
    multiline_option = document.data.multiline_rb.checked;
    var cs = (case_option)?"":"i";
    var gs = (global_option)?"g":"";
    var ms = (multiline_option)?"m":"";
    search_val = unescape_chars(search_val,dot_all_option);
    var results_text = document.getElementById("results_text");
    var input_text = document.data.input_text.value;
    // search and replace option
    if(replace) {
      replace_val = unescape_chars(replace_val,false);
      var reg = new RegExp(search_val,cs+ms+gs);
      var s = input_text.replace(reg,replace_val);
      s = s.replace(/\n/g,"<br/>");
      results_text.innerHTML = s;
    }
    // search and display matches only
    else {
      var reg = new RegExp(search_val,cs+ms);
      var matches = 0;
      var n = 0;
      var i = 0;
      while(n++ < max_matches && i < input_text.length && reg.test(input_text.substring(i))) {
        matches++;
        output += RegExp.leftContext + color_tag + RegExp.lastMatch + "</span>";
        i += RegExp.leftContext.length + RegExp.lastMatch.length;
        if(!global_option) {
          break;
        }
      }
      if(matches > 0) {
        var s = output + RegExp.rightContext;
        s = s.replace(/\n/g,"<br/>");
        results_text.innerHTML = s;
      }
      else {
        results_text.innerHTML = input_text.replace(/\n/g,"<br/>");
      }
      var s = "Matches: " + matches;
      if(n >= max_matches) {
        s = "<span style=\"color:red;\">" + s + " (probable runaway error, search halts after " + max_matches + " &quot;matches&quot;)</span>";
      }
      document.getElementById("match_count").innerHTML = s;
    }
  }
  catch(e) {
    s = (e.description)?e.description:e;
    alert(s);
  }
}

