<?php
/*
Plugin Name: Editor Search
Plugin URI: http://www.theoneandtheonly.com/wordpress-editor-search/
Description: Adds search feature to theme and plug-in editors in the WordPress admin area.  Also offers a 'go to line number' feature.  Requires JavaScript to be enabled in your browser.
Version: 1.0
Author: Andrew Buckman
Author URI: http://www.theoneandtheonly.com/
*/

if ( !function_exists('ajbES_insert_code') ) {
  function 
ajbES_insert_code() {
?>
    <div id="ajbESdiv" style="display: inline">
      <span>
        <input id="ajbES_SearchInput" type="text" value="" />
        <input type="button" value="Find Below" onClick="ajbES_Search(); return false;" />
      </span>
      <span style="border-left: 1px solid gray; padding-left: 8px; margin-left: 5px;">
        <input id="ajbES_LineNumInput" type="text" value="" style="width: 40px; text-align: right" />
        <input type="button" value="GoTo Line#" onClick="ajbES_FindLineNumber(); return false;" />
      </span>
    </div>

    <script language="JavaScript" type="text/javascript">
        var ajbES_insBeforeMe = document.getElementById("templateside");
        var ajbES_insParent = ajbES_insBeforeMe.parentNode;
        var ajbES_insMe = document.getElementById("ajbESdiv");
        ajbES_insParent.insertBefore(ajbES_insMe, ajbES_insBeforeMe);

        function ajbES_Search() {
          var a = document.getElementById("ajbES_SearchInput");
          var b = document.getElementById("newcontent");

          // grab cursor position for starting point of search
          var cursorPos = 0;
          if (b.selectionStart!=b.value.length) cursorPos = b.selectionStart + 1;

          // find text
          var foundPos;
          foundPos = b.value.indexOf(a.value, cursorPos);
          if ((foundPos < 0) && (cursorPos > 0)) {
            // text not found, try looping to the start and looking again
            foundPos = b.value.indexOf(a.value);
          }

          if (foundPos < 0) {
            alert('No match found!');
          } else {
            // select textarea (b) and move cursor to foundPos
            b.focus();
            b.selectionStart = foundPos;
            b.selectionEnd = foundPos + a.value.length;
            ajbES_scrollToCursor();
          }
        }

        function ajbES_FindLineNumber() {
          var a = document.getElementById("ajbES_LineNumInput");
          if (a.value) {
            a = a.value - 1;
            var b = document.getElementById("newcontent");
            var lines = b.value.split('\n');
            if (a<lines.length && a>=0) {
              var i;
              var cursorPos=0;
              for (i=0; i<a; i++) {
                cursorPos += lines[i].length + 1; // +1 for newline
              }
              if (lines[a].length==0) alert('Warning, your selected line is blank, the cursor will show up at end of previous line.');
              b.focus();
              b.selectionStart = cursorPos;
              b.selectionEnd = cursorPos;
              ajbES_scrollToCursor();
            } else if (a>=lines.length) {
              alert('Sorry there are only ' + lines.length + ' lines in the textarea.');
            } else {
              alert('Please enter a valid line number to go to.');
            }
          }
        }

        function ajbES_scrollToCursor() {
          // scrolls the textarea so the cursor / highlight are onscreen
          var b = document.getElementById("newcontent");
          var cursorPos  = b.selectionStart;
          var linesTotal = b.value.split('\n').length;
          var linesAbove = b.value.slice(0, cursorPos).split('\n').length;
          if (linesAbove<(b.rows/2)) linesAbove = 0;
          var scrollTo = (linesAbove / (linesTotal-b.rows)) * (b.scrollHeight - b.clientHeight);
          b.scrollTop = scrollTo;
        }
    </script>
<?
  
}
}

if ( !
function_exists('ajbES_admin_footer') )  {
  function 
ajbES_admin_footer($content) {
    if((
preg_match('|theme-editor.php|i'$_SERVER["REQUEST_URI"])) ||
       (
preg_match('|plugin-editor.php|i'$_SERVER["REQUEST_URI"]))) {
      
ajbES_insert_code();
    }
  }
}

add_filter ('admin_footer''ajbES_admin_footer');

?>