diff options
| author | Jez Ng | 2012-01-17 15:48:10 +0800 | 
|---|---|---|
| committer | Jez Ng | 2012-01-25 23:47:55 -0500 | 
| commit | df02f36755b57fbc65d0fc2c00ea24870e98093a (patch) | |
| tree | 9e53bcd7877b0992446a7abab027af072c347d64 /vimiumFrontend.js | |
| parent | 2134a4c14abcfcbae0a75ba31f29d5651f057db2 (diff) | |
| download | vimium-df02f36755b57fbc65d0fc2c00ea24870e98093a.tar.bz2 | |
Add enhancements and bugfixes for find mode.
* Case-sensitivity behavior is now equivalent to Vim's 'smartcase'.
* \I flag turns on 'noignorecase'.
* If a regex does not parse, assume the user isn't done typing yet.
Diffstat (limited to 'vimiumFrontend.js')
| -rw-r--r-- | vimiumFrontend.js | 32 | 
1 files changed, 24 insertions, 8 deletions
| diff --git a/vimiumFrontend.js b/vimiumFrontend.js index 28ab7659..94290e4e 100644 --- a/vimiumFrontend.js +++ b/vimiumFrontend.js @@ -605,11 +605,16 @@ function updateFindModeQuery() {    // the query can be treated differently (e.g. as a plain string versus regex depending on the presence of    // escape sequences. '\' is the escape character and needs to be escaped itself to be used as a normal    // character. here we grep for the relevant escape sequences. +  findModeQuery.isRegex = false; +  var hasNoIgnoreCaseFlag = false;    findModeQuery.parsedQuery = findModeQuery.rawQuery.replace(/\\./g, function(match) {      switch (match) {        case "\\r":          findModeQuery.isRegex = true;          return ''; +      case "\\I": +        hasNoIgnoreCaseFlag = true; +        return '';        case "\\\\":          return "\\";        default: @@ -617,10 +622,19 @@ function updateFindModeQuery() {      }    }); +  // default to 'smartcase' mode, unless noIgnoreCase is explicitly specified +  findModeQuery.ignoreCase = !hasNoIgnoreCaseFlag && !/[A-Z]/.test(findModeQuery.parsedQuery); +    // if we are dealing with a regex, grep for all matches in the text, and then call window.find() on them    // sequentially so the browser handles the scrolling / text selection.    if (findModeQuery.isRegex) { -    var pattern = new RegExp(findModeQuery.parsedQuery, "gi"); +    try { +      var pattern = new RegExp(findModeQuery.parsedQuery, "g" + (findModeQuery.ignoreCase ? "i" : "")); +    } +    catch (e) { +      // if we catch a SyntaxError, assume the user is not done typing yet and return quietly +      return; +    }      // innerText will not return the text of hidden elements, and strip out tags while preserving newlines      var text = document.body.innerText;      findModeQuery.regexMatches = text.match(pattern); @@ -658,7 +672,7 @@ function performFindInPlace() {    var cachedScrollY = window.scrollY;    if (findModeQuery.isRegex) { -    if (findModeQuery.regexMatches === null) { +    if (!findModeQuery.regexMatches) {        findModeQueryHasResults = false;        return;      } @@ -670,17 +684,19 @@ function performFindInPlace() {    // Search backwards first to "free up" the current word as eligible for the real forward search. This allows    // us to search in place without jumping around between matches as the query grows. -  window.find(query, false, true, true, false, true, false); +  executeFind(query, { backwards: true, caseSensitive: !findModeQuery.ignoreCase });    // We need to restore the scroll position because we might've lost the right position by searching    // backwards.    window.scrollTo(cachedScrollX, cachedScrollY); -  findModeQueryHasResults = executeFind(query); +  findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase });  } -function executeFind(query, backwards) { -  return window.find(query, false, backwards, true, false, true, false); +// :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. +function executeFind(query, options) { +  options = options || {}; +  return window.find(query, options.caseSensitive, options.backwards, true, false, true, false);  }  function focusFoundLink() { @@ -709,7 +725,7 @@ function findAndFocus(backwards) {    else      var query = findModeQuery.parsedQuery; -  executeFind(query, backwards); +  executeFind(query, { backwards: backwards, caseSensitive: !findModeQuery.ignoreCase });    focusFoundLink();  } @@ -766,7 +782,7 @@ function goNext() {  }  function showFindModeHUDForQuery() { -  if (findModeQueryHasResults || findModeQuery.rawQuery.length == 0) +  if (findModeQueryHasResults || findModeQuery.parsedQuery.length == 0)      HUD.show("/" + insertSpaces(findModeQuery.rawQuery));    else      HUD.show("/" + insertSpaces(findModeQuery.rawQuery + " (No Matches)")); | 
