1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-01-07 18:44:10 +01:00

add the 'smartcase' option and support for \c and \C in search patterns

This commit is contained in:
Doug Kearns
2007-09-05 11:32:46 +00:00
parent 62ede9f92e
commit 88749df4bb
5 changed files with 76 additions and 22 deletions

2
NEWS
View File

@@ -1,7 +1,7 @@
<pre> <pre>
2007-xx-xx: 2007-xx-xx:
* version 0.6 * version 0.6
* add 'hlsearch','incsearch' and 'ignorecase' options * added 'hlsearch','incsearch', 'ignorecase' and 'smartcase' options
2007-09-03: 2007-09-03:
* version 0.5.1 * version 0.5.1

View File

@@ -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. the terms of any one of the MPL, the GPL or the LGPL.
}}} ***** END LICENSE BLOCK *****/ }}} ***** END LICENSE BLOCK *****/
// TODO: <ESC> should cancel search highlighting in 'incsearch' mode
// make sure you only create this object when the "vimperator" object is ready // make sure you only create this object when the "vimperator" object is ready
function Search() //{{{ function Search() //{{{
{ {
var self = this; // needed for callbacks since "this" is the "vimperator" object in a callback 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 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 = ""; // 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 <esc> this is not set var lastsearch_backwards = false; // like "backwards", but for the last search, so if you cancel a search with <esc> this is not set
var case_sensitive = true;
// Event handlers for search - closure is needed // Event handlers for search - closure is needed
vimperator.registerCallback("change", vimperator.modes.SEARCH_FORWARD, function(command){ self.searchKeyPressed(command); }); 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("submit", vimperator.modes.SEARCH_FORWARD, function(command) { self.searchSubmitted(command); });
vimperator.registerCallback("cancel", vimperator.modes.SEARCH_FORWARD, function(){ self.searchCanceled(); }); vimperator.registerCallback("cancel", vimperator.modes.SEARCH_FORWARD, function() { self.searchCanceled(); });
// TODO: allow advanced modes in register/triggerCallback // TODO: allow advanced modes in register/triggerCallback
vimperator.registerCallback("change", vimperator.modes.SEARCH_BACKWARD, function(command){ self.searchKeyPressed(command); }); 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("submit", vimperator.modes.SEARCH_BACKWARD, function(command) { self.searchSubmitted(command); });
vimperator.registerCallback("cancel", vimperator.modes.SEARCH_BACKWARD, function(){ self.searchCanceled(); }); 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 // Called when the search dialog is asked for
// If you omit "mode", it will default to forward searching // If you omit "mode", it will default to forward searching
@@ -51,12 +91,12 @@ function Search() //{{{
{ {
if (mode == vimperator.modes.SEARCH_BACKWARD) if (mode == vimperator.modes.SEARCH_BACKWARD)
{ {
vimperator.commandline.open('?', '', vimperator.modes.SEARCH_BACKWARD); vimperator.commandline.open("?", "", vimperator.modes.SEARCH_BACKWARD);
backwards = true; backwards = true;
} }
else else
{ {
vimperator.commandline.open('/', '', vimperator.modes.SEARCH_FORWARD); vimperator.commandline.open("/", "", vimperator.modes.SEARCH_FORWARD);
backwards = false; backwards = false;
} }
@@ -69,7 +109,7 @@ function Search() //{{{
{ {
var fastFind = getBrowser().fastFind; var fastFind = getBrowser().fastFind;
fastFind.caseSensitive = !vimperator.options["ignorecase"]; fastFind.caseSensitive = case_sensitive;
found = fastFind.find(str, false) != Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND; found = fastFind.find(str, false) != Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND;
return found; return found;
@@ -115,9 +155,10 @@ function Search() //{{{
// Called when the user types a key in the search dialog. Triggers a find attempt // Called when the user types a key in the search dialog. Triggers a find attempt
this.searchKeyPressed = function(command) this.searchKeyPressed = function(command)
{ {
if (!vimperator.options['incsearch']) if (!vimperator.options["incsearch"])
return; return;
command = processPattern(command);
this.find(command, backwards); this.find(command, backwards);
} }
@@ -125,12 +166,13 @@ function Search() //{{{
this.searchSubmitted = function(command) this.searchSubmitted = function(command)
{ {
this.clear(); this.clear();
command = processPattern(command);
this.find(command, backwards); this.find(command, backwards);
this.highlight(command); this.highlight(command);
// need to find again to draw the highlight of the current search // need to find again to draw the highlight of the current search
// result over the "highlight all" search results // 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); setTimeout(function() { self.findAgain(false); }, 10);
lastsearch_backwards = backwards; lastsearch_backwards = backwards;
@@ -155,7 +197,7 @@ function Search() //{{{
if (!word) if (!word)
word = lastsearch; word = lastsearch;
gFindBar._setCaseSensitivity(!vimperator.options["ignorecase"]) gFindBar._setCaseSensitivity(case_sensitive)
gFindBar._highlightDoc("yellow", "black", word); gFindBar._highlightDoc("yellow", "black", word);
} }

View File

@@ -942,16 +942,20 @@ function Mappings() //{{{
function() { vimperator.search.openSearchDialog(vimperator.modes.SEARCH_FORWARD); }, function() { vimperator.search.openSearchDialog(vimperator.modes.SEARCH_FORWARD); },
{ {
short_help: "Search forward for a pattern", short_help: "Search forward for a pattern",
usage: ["/{pattern}<CR>"], usage: ["/{pattern}[/]<CR>"],
help: "Search forward for the first occurance of <code class=\"argument\">{pattern}</code>." help: "Search forward for the first occurance of <code class=\"argument\">{pattern}</code>.<br/>" +
"When \"\\c\" appears anywhere in the pattern the whole pattern is handled as though <code class=\"option\">'ignorecase'</code> is on. " +
"\"\\C\" forces case-sensitive matching for the whole pattern."
} }
)); ));
addDefaultMap(new Map(vimperator.modes.NORMAL, ["?"], addDefaultMap(new Map(vimperator.modes.NORMAL, ["?"],
function() { vimperator.search.openSearchDialog(vimperator.modes.SEARCH_BACKWARD); }, function() { vimperator.search.openSearchDialog(vimperator.modes.SEARCH_BACKWARD); },
{ {
short_help: "Search backwards for a pattern", short_help: "Search backwards for a pattern",
usage: ["?{pattern}<CR>"], usage: ["?{pattern}[?]<CR>"],
help: "Search backward for the first occurance of <code class=\"argument\">{pattern}</code>." help: "Search backward for the first occurance of <code class=\"argument\">{pattern}</code>.<br/>" +
"When '\\c' appears anywhere in the pattern the whole pattern is handled as though <code class=\"option\">'ignorecase'</code> is on. " +
"'\\C' forces case-sensitive matching for the whole pattern."
} }
)); ));
addDefaultMap(new Map(vimperator.modes.NORMAL, ["n"], addDefaultMap(new Map(vimperator.modes.NORMAL, ["n"],

View File

@@ -511,6 +511,13 @@ function Options() //{{{
validator: function (value) { if (value >= 0 && value <= 2) return true; else return false; } 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 <code class=\"option\">'ignorecase'</code> option is set.",
default_value: false
}
));
addOption(new Option(["titlestring"], "string", addOption(new Option(["titlestring"], "string",
{ {
short_help: "Change the title of the browser window", short_help: "Change the title of the browser window",

View File

@@ -30,9 +30,10 @@ syn match vimperatorCommandWrapper "\<\h\w*\>" contains=vimperatorCommand
syn region vimperatorSet matchgroup=vimperatorCommand start="\<set\=\>" end="$" keepend oneline contains=vimperatorOption syn region vimperatorSet matchgroup=vimperatorCommand start="\<set\=\>" end="$" keepend oneline contains=vimperatorOption
syn keyword vimperatorOption activate beep nobeep beep complete cpt defsearch ds extendedhinttags eht focusedhintstyle fhs 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 \ fullscreen fs nofullscreen nofs guioptions go hintchars hc hintstyle hs hinttags incsearch is noincsearch nois
\ previewheight pvh showmode smd noshowmode nosmd showstatuslinks ssli showtabline stal titlestring usermode um nousermode \ ignorecase ic noignorecase noic maxhints mh preload nopreload previewheight pvh showmode smd noshowmode nosmd
\ noum verbose vbs wildmode wim wildoptions wop \ showstatuslinks ssli showtabline stal smartcase scs nosmartcase noscs titlestring usermode um nousermode noum verbose
\ vbs wildmode wim wildoptions wop
\ contained \ contained
syn region vimperatorJavascript start="\%(^\s*\%(javascript\|js\)\s\+\)\@<=" end="$" contains=@javascriptTop keepend oneline syn region vimperatorJavascript start="\%(^\s*\%(javascript\|js\)\s\+\)\@<=" end="$" contains=@javascriptTop keepend oneline