From 88749df4bb7b26ca1eef9101fe6b06cdea429352 Mon Sep 17 00:00:00 2001 From: Doug Kearns Date: Wed, 5 Sep 2007 11:32:46 +0000 Subject: [PATCH] add the 'smartcase' option and support for \c and \C in search patterns --- NEWS | 2 +- chrome/content/vimperator/find.js | 70 +++++++++++++++++++++------ chrome/content/vimperator/mappings.js | 12 +++-- chrome/content/vimperator/options.js | 7 +++ vimperator.vim | 7 +-- 5 files changed, 76 insertions(+), 22 deletions(-) diff --git a/NEWS b/NEWS index e8c52516..af6130b0 100644 --- a/NEWS +++ b/NEWS @@ -1,7 +1,7 @@
 2007-xx-xx:
 	* version 0.6
-	* add 'hlsearch','incsearch' and 'ignorecase' options
+	* added 'hlsearch','incsearch', 'ignorecase' and 'smartcase' options
 
 2007-09-03:
 	* version 0.5.1
diff --git a/chrome/content/vimperator/find.js b/chrome/content/vimperator/find.js
index e14db76c..6b0b6d07 100644
--- a/chrome/content/vimperator/find.js
+++ b/chrome/content/vimperator/find.js
@@ -26,24 +26,64 @@ the provisions above, a recipient may use your version of this file under
 the terms of any one of the MPL, the GPL or the LGPL.
 }}} ***** END LICENSE BLOCK *****/
 
-
+// TODO:  should cancel search highlighting in 'incsearch' mode
 // make sure you only create this object when the "vimperator" object is ready
 function Search() //{{{
 {
     var self = this;                  // needed for callbacks since "this" is the "vimperator" object in a callback
     var found = false;                // true if the last search was successful
-    var backwards = false;
+    var backwards = false;            // currently searching backwards
     var lastsearch = "";              // keep track of the last searched string
     var lastsearch_backwards = false; // like "backwards", but for the last search, so if you cancel a search with  this is not set
+    var case_sensitive = true;
 
     // Event handlers for search - closure is needed
-    vimperator.registerCallback("change", vimperator.modes.SEARCH_FORWARD, function(command){ self.searchKeyPressed(command); });
-    vimperator.registerCallback("submit", vimperator.modes.SEARCH_FORWARD, function(command){ self.searchSubmitted(command); });
-    vimperator.registerCallback("cancel", vimperator.modes.SEARCH_FORWARD, function(){ self.searchCanceled(); });
+    vimperator.registerCallback("change", vimperator.modes.SEARCH_FORWARD, function(command) { self.searchKeyPressed(command); });
+    vimperator.registerCallback("submit", vimperator.modes.SEARCH_FORWARD, function(command) { self.searchSubmitted(command); });
+    vimperator.registerCallback("cancel", vimperator.modes.SEARCH_FORWARD, function() { self.searchCanceled(); });
     // TODO: allow advanced modes in register/triggerCallback
-    vimperator.registerCallback("change", vimperator.modes.SEARCH_BACKWARD, function(command){ self.searchKeyPressed(command); });
-    vimperator.registerCallback("submit", vimperator.modes.SEARCH_BACKWARD, function(command){ self.searchSubmitted(command); });
-    vimperator.registerCallback("cancel", vimperator.modes.SEARCH_BACKWARD, function(){ self.searchCanceled(); });
+    vimperator.registerCallback("change", vimperator.modes.SEARCH_BACKWARD, function(command) { self.searchKeyPressed(command); });
+    vimperator.registerCallback("submit", vimperator.modes.SEARCH_BACKWARD, function(command) { self.searchSubmitted(command); });
+    vimperator.registerCallback("cancel", vimperator.modes.SEARCH_BACKWARD, function() { self.searchCanceled(); });
+
+    // clean the pattern search string of modifiers and set the
+    // case-sensitivity flag
+    function processPattern(pattern)
+    {
+        // strip off pattern terminator and trailing /junk
+        if (backwards)
+            pattern = pattern.replace(/\?.*/, "");
+        else
+            pattern = pattern.replace(/\/.*/, "");
+
+        if (!pattern)
+            pattern = lastsearch;
+
+        if (/\\C/.test(pattern))
+        {
+            case_sensitive = true;
+            pattern = pattern.replace(/\\C/, "");
+        }
+        else if (/\\c/.test(pattern))
+        {
+            case_sensitive = false;
+            pattern = pattern.replace(/\\c/, "");
+        }
+        else if (vimperator.options["ignorecase"] && vimperator.options["smartcase"] && /[A-Z]/.test(pattern))
+        {
+            case_sensitive = true;
+        }
+        else if (vimperator.options["ignorecase"])
+        {
+            case_sensitive = false;
+        }
+        else
+        {
+            case_sensitive = true;
+        }
+
+        return pattern;
+    }
 
     // Called when the search dialog is asked for
     // If you omit "mode", it will default to forward searching
@@ -51,12 +91,12 @@ function Search() //{{{
     {
         if (mode == vimperator.modes.SEARCH_BACKWARD)
         {
-            vimperator.commandline.open('?', '', vimperator.modes.SEARCH_BACKWARD);
+            vimperator.commandline.open("?", "", vimperator.modes.SEARCH_BACKWARD);
             backwards = true;
         }
         else
         {
-            vimperator.commandline.open('/', '', vimperator.modes.SEARCH_FORWARD);
+            vimperator.commandline.open("/", "", vimperator.modes.SEARCH_FORWARD);
             backwards = false;
         }
 
@@ -69,7 +109,7 @@ function Search() //{{{
     {
         var fastFind = getBrowser().fastFind;
 
-        fastFind.caseSensitive = !vimperator.options["ignorecase"];
+        fastFind.caseSensitive = case_sensitive;
         found = fastFind.find(str, false) != Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND;
 
         return found;
@@ -115,9 +155,10 @@ function Search() //{{{
     // Called when the user types a key in the search dialog. Triggers a find attempt
     this.searchKeyPressed = function(command)
     {
-        if (!vimperator.options['incsearch'])
+        if (!vimperator.options["incsearch"])
             return;
 
+        command = processPattern(command);
         this.find(command, backwards);
     }
 
@@ -125,12 +166,13 @@ function Search() //{{{
     this.searchSubmitted = function(command)
     {
         this.clear();
+        command = processPattern(command);
         this.find(command, backwards);
         this.highlight(command);
 
         // need to find again to draw the highlight of the current search
         // result over the "highlight all" search results
-        // very hacky, but seem to work
+        // very hacky, but seems to work
         setTimeout(function() { self.findAgain(false); }, 10);
 
         lastsearch_backwards = backwards;
@@ -155,7 +197,7 @@ function Search() //{{{
         if (!word)
             word = lastsearch;
 
-        gFindBar._setCaseSensitivity(!vimperator.options["ignorecase"])
+        gFindBar._setCaseSensitivity(case_sensitive)
         gFindBar._highlightDoc("yellow", "black", word);
     }
 
diff --git a/chrome/content/vimperator/mappings.js b/chrome/content/vimperator/mappings.js
index fc4b9ffa..8000661b 100644
--- a/chrome/content/vimperator/mappings.js
+++ b/chrome/content/vimperator/mappings.js
@@ -942,16 +942,20 @@ function Mappings() //{{{
         function() { vimperator.search.openSearchDialog(vimperator.modes.SEARCH_FORWARD); },
         {
             short_help: "Search forward for a pattern",
-            usage: ["/{pattern}"],
-            help: "Search forward for the first occurance of {pattern}."
+            usage: ["/{pattern}[/]"],
+            help: "Search forward for the first occurance of {pattern}.
" + + "When \"\\c\" appears anywhere in the pattern the whole pattern is handled as though 'ignorecase' is on. " + + "\"\\C\" forces case-sensitive matching for the whole pattern." } )); addDefaultMap(new Map(vimperator.modes.NORMAL, ["?"], function() { vimperator.search.openSearchDialog(vimperator.modes.SEARCH_BACKWARD); }, { short_help: "Search backwards for a pattern", - usage: ["?{pattern}"], - help: "Search backward for the first occurance of {pattern}." + usage: ["?{pattern}[?]"], + help: "Search backward for the first occurance of {pattern}.
" + + "When '\\c' appears anywhere in the pattern the whole pattern is handled as though 'ignorecase' is on. " + + "'\\C' forces case-sensitive matching for the whole pattern." } )); addDefaultMap(new Map(vimperator.modes.NORMAL, ["n"], diff --git a/chrome/content/vimperator/options.js b/chrome/content/vimperator/options.js index 4186612b..5b780928 100644 --- a/chrome/content/vimperator/options.js +++ b/chrome/content/vimperator/options.js @@ -511,6 +511,13 @@ function Options() //{{{ validator: function (value) { if (value >= 0 && value <= 2) return true; else return false; } } )); + addOption(new Option(["smartcase", "scs"], "boolean", + { + short_help: "Override the 'ignorecase' option if the pattern contains uppercase characters", + help: "This is only used if the 'ignorecase' option is set.", + default_value: false + } + )); addOption(new Option(["titlestring"], "string", { short_help: "Change the title of the browser window", diff --git a/vimperator.vim b/vimperator.vim index 739487c6..68ae6103 100644 --- a/vimperator.vim +++ b/vimperator.vim @@ -30,9 +30,10 @@ syn match vimperatorCommandWrapper "\<\h\w*\>" contains=vimperatorCommand syn region vimperatorSet matchgroup=vimperatorCommand start="\" end="$" keepend oneline contains=vimperatorOption syn keyword vimperatorOption activate beep nobeep beep complete cpt defsearch ds extendedhinttags eht focusedhintstyle fhs - \ fullscreen fs nofullscreen nofs guioptions go hintchars hc hintstyle hs hinttags maxhints mh preload nopreload - \ previewheight pvh showmode smd noshowmode nosmd showstatuslinks ssli showtabline stal titlestring usermode um nousermode - \ noum verbose vbs wildmode wim wildoptions wop + \ fullscreen fs nofullscreen nofs guioptions go hintchars hc hintstyle hs hinttags incsearch is noincsearch nois + \ ignorecase ic noignorecase noic maxhints mh preload nopreload previewheight pvh showmode smd noshowmode nosmd + \ showstatuslinks ssli showtabline stal smartcase scs nosmartcase noscs titlestring usermode um nousermode noum verbose + \ vbs wildmode wim wildoptions wop \ contained syn region vimperatorJavascript start="\%(^\s*\%(javascript\|js\)\s\+\)\@<=" end="$" contains=@javascriptTop keepend oneline