From 30d9b25d3512ca9ea6d61c28a54548b9852e34ec Mon Sep 17 00:00:00 2001 From: Doug Kearns Date: Thu, 6 Sep 2007 00:04:10 +0000 Subject: [PATCH] merge new 'incsearch', 'ignorecase', 'smartcase', 'hlsearch' options from MAIN --- NEWS | 1 + chrome/content/vimperator/find.js | 106 ++++++++++++++++++++------ chrome/content/vimperator/mappings.js | 13 +++- chrome/content/vimperator/options.js | 27 +++++++ vimperator.vim | 9 ++- 5 files changed, 123 insertions(+), 33 deletions(-) diff --git a/NEWS b/NEWS index 2c7b6159..40b88a96 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@
 2007-XX-XX:
 	* version 0.5.2
+	* 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 3d268012..7eafdcdb 100644
--- a/chrome/content/vimperator/find.js
+++ b/chrome/content/vimperator/find.js
@@ -26,25 +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
-// vimperator.search = new function()
 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 lastsearch = ""; // keep track of the last searched string
+    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;            // 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
@@ -52,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;
         }
 
@@ -68,11 +107,10 @@ function Search() //{{{
     // TODO: backwards seems impossible i fear :(
     this.find = function(str, backwards)
     {
-        const FIND_NORMAL = 0;
-        const FIND_TYPEAHEAD = 1;
-        const FIND_LINKS = 2;
+        var fastFind = getBrowser().fastFind;
 
-        found = getBrowser().fastFind.find(str, false) != Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND;
+        fastFind.caseSensitive = case_sensitive;
+        found = fastFind.find(str, false) != Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND;
 
         return found;
     }
@@ -82,11 +120,12 @@ function Search() //{{{
     {
         // this hack is needed to make n/N work with the correct string, if
         // we typed /foo after the original search
+        // TODO: this should also clear the current item highlighting
         if (getBrowser().fastFind.searchString != lastsearch)
         {
             this.clear();
             this.find(lastsearch, false);
-            gFindBar.highlightDoc("yellow", "black", lastsearch);
+            this.highlight(lastsearch);
         }
 
         var up = reverse ? !lastsearch_backwards : lastsearch_backwards;
@@ -98,12 +137,14 @@ function Search() //{{{
             result = getBrowser().fastFind.findNext();
 
         if (result == Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND)
+        {
             vimperator.echoerr("E486: Pattern not found: " + lastsearch);
+        }
         else if (result == Components.interfaces.nsITypeAheadFind.FIND_WRAPPED)
         {
             // hack needed, because wrapping causes a "scroll" event which clears
             // our command line
-            setTimeout( function() {
+            setTimeout(function() {
                 if (up)
                     vimperator.echoerr("search hit TOP, continuing at BOTTOM");
                 else
@@ -111,26 +152,32 @@ function Search() //{{{
             }, 10);
         }
         else // just clear the command line if something has been found
+        {
             vimperator.echo("");
+        }
     }
 
     // Called when the user types a key in the search dialog. Triggers a find attempt
     this.searchKeyPressed = function(command)
     {
-        // TODO: check for 'incsearch'
-        var backward = vimperator.hasMode(vimperator.modes.SEARCH_BACKWARD);
-        this.find(command, backward);
+        if (!vimperator.options["incsearch"])
+            return;
+
+        command = processPattern(command);
+        this.find(command, backwards);
     }
 
     // Called when the enter key is pressed to trigger a search
     this.searchSubmitted = function(command)
     {
         this.clear();
-        gFindBar.highlightDoc("yellow", "black", command);
+        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;
@@ -150,6 +197,15 @@ function Search() //{{{
         vimperator.focusContent();
     }
 
+    this.highlight = function(word)
+    {
+        if (!word)
+            word = lastsearch;
+
+        gFindBar.setCaseSensitivity(case_sensitive)
+        gFindBar.highlightDoc("yellow", "black", word);
+    }
+
     this.clear = function()
     {
         gFindBar.highlightDoc();
diff --git a/chrome/content/vimperator/mappings.js b/chrome/content/vimperator/mappings.js
index fc4b9ffa..40fdf6da 100644
--- a/chrome/content/vimperator/mappings.js
+++ b/chrome/content/vimperator/mappings.js
@@ -942,16 +942,21 @@ 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.
" + + "NOTE: incremental searching currenly only works in the forward direction." } )); addDefaultMap(new Map(vimperator.modes.NORMAL, ["n"], diff --git a/chrome/content/vimperator/options.js b/chrome/content/vimperator/options.js index 3f97c35a..f52b370e 100644 --- a/chrome/content/vimperator/options.js +++ b/chrome/content/vimperator/options.js @@ -425,6 +425,26 @@ function Options() //{{{ default_value: DEFAULT_HINTTAGS } )); + addOption(new Option(["hlsearch", "hls"], "boolean", + { + short_help: "Highlight previous search pattern matches", + setter: function(value) { if (value) vimperator.search.highlight(); else vimperator.search.clear(); }, + default_value: true + } + )); + addOption(new Option(["ignorecase", "ic"], "boolean", + { + short_help: "Ignore case in search patterns", + default_value: true + } + )); + addOption(new Option(["incsearch", "is"], "boolean", + { + short_help: "Show where the search pattern matches as it is typed", + help: "NOTE: Incremental searching currently only works in the forward direction.", + default_value: true + } + )); addOption(new Option(["maxhints", "mh"], "number", { short_help: "Maximum number of simultaneously shown hints", @@ -492,6 +512,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..8a6a92c4 100644 --- a/vimperator.vim +++ b/vimperator.vim @@ -29,10 +29,11 @@ syn keyword vimperatorCommand addo[ns] ba[ck] bd[elete] bw[ipeout] bun[load] tab 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 +syn keyword vimperatorOption activate act complete cpt defsearch ds extendedhinttags eht focusedhintstyle fhs 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 visualbell vb + \ wildmode wim wildoptions wop \ contained syn region vimperatorJavascript start="\%(^\s*\%(javascript\|js\)\s\+\)\@<=" end="$" contains=@javascriptTop keepend oneline