# K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)

# ---------- Web Search --------------------------------------------------------------------------------------------
#
# Dependencies	: main.kmm ($macroPrefBranch, OpenURL)
# Resources	: -
# Preferences	: kmeleon.plugins.macros.search.kmforums
#		  kmeleon.plugins.macros.search.engine[0,1,...].name
#		  kmeleon.plugins.macros.search.engine[0,1,...].url
#		  kmeleon.plugins.macros.search.meta[0,1,...].url
#
# ------------------------------------------------------------------------------------------------------------------

# Consult default engine (default engine is also used when macros are disabled)
Search{
macroinfo=_("Consult the default search engine");
$_Search_EngineURL==""?$_Search_EngineURL=getpref(STRING,$_Search_pref_DefaultURL):0;
$_Search_EngineName==""?$_Search_EngineName=getpref(STRING,$_Search_pref_DefaultName):0;
$_Search_Query=$SelectedText; $_Search_Query==""?&_Search_Typed:&_Search_Selected;
$_Search_EngineName="";
$_Search_EngineURL="";
}
Search_PasteAndSearch{
macroinfo=_("Search the Web for Clipboard contents");
$URLBAR=getclipboard(); &Search;
}

# Lock/unlock current engine
Search_ToggleEngineLock{
macroinfo=_("Lock or unlock the current search engine as the default engine");
menuchecked=getpref(BOOL,$_Search_pref_Locked);
togglepref(BOOL,$_Search_pref_Locked);
statusbar(sub("%s",getpref(BOOL,$_Search_pref_Locked)?getpref(STRING,$_Search_pref_DefaultName):_("Last used engine"),_("Default search engine: %s")));
}

# Consult multiple engines (Metasearch)
Search_Metasearch{
macroinfo=_("Search the Web using multiple engines");
$_Search_Metasearch_Query=$SelectedText;
$_Search_Metasearch_Query==""?&_Search_Metasearch_Typed:&_Search_Metasearch_Selected;
}

# Consult a particular engine
Search_KMForums{
macroinfo=_("Search the K-Meleon forums");
$_Search_EngineURL=getpref(STRING,$macroPrefBranch."search.kmforums");
$_Search_EngineName=_("K-Meleon Forums Search"); &Search;
}
Search_Engine{
macroinfo=_("Search the Web using the specified engine");
menuchecked=(getpref(STRING,$_Search_pref_Engine.$ARG.".url")==getpref(STRING,$_Search_pref_DefaultURL));
$_n=$ARG; &_Search;
}

# ----- PRIVATE

# Normal websearch

$_Search_EngineURL="";
$_Search_EngineName="";

$_Search_pref_DefaultName="kmeleon.general.searchEngineName";
$_Search_pref_DefaultURL="kmeleon.general.searchEngine";
$_Search_pref_Engine=$macroPrefBranch."search.engine";
$_Search_pref_Locked=$macroPrefBranch."search.locked";

# interface for search9.kmm to hook into menu sync
$_Search_SyncMenu="";

_Search{
$_Search_EngineURL=getpref(STRING,$_Search_pref_Engine.$_n.".url");
$_Search_EngineName=getpref(STRING,$_Search_pref_Engine.$_n.".name");
&_Search_Init;
}
_Search_Init{
$__locked=getpref(BOOL,$_Search_pref_Locked);
$__locked?0:($_Search_EngineURL?setpref(STRING,$_Search_pref_DefaultURL,$_Search_EngineURL):0);
$__locked?0:($_Search_EngineName?setpref(STRING,$_Search_pref_DefaultName,$_Search_EngineName):0);
# interface for search9.kmm to hook into menu sync
$__locked?0:macros($_Search_SyncMenu);
&Search;
}
_Search_Selected{
$OpenURL=$_Search_EngineURL.urlencode($_Search_Query); &OpenURL_Selected;
}
_Search_Typed{
$__query=$URLBAR;
$__query==$URL?$__query=prompt(_("Enter search query:"),sub("%s",$_Search_EngineName,_("Search (%s)"))):0;
$OpenURL=$__query==""?"":$_Search_EngineURL.urlencode($__query);
# prevent search queries from being added to the MRU
$__pref="kmeleon.MRU.behavior"; $__data=getpref(INT,$__pref); $__data==2?setpref(INT,$__pref,3):0; &OpenURL_Typed; setpref(INT,$__pref,$__data);
}

# Metasearch

$_Search_pref_Meta=$macroPrefBranch."search.meta";

_Search_Metasearch_Selected{
$_pref=$_OpenURL_Selected; &_Search_Metasearch_Open;
}
_Search_Metasearch_Typed{
$_pref=$_OpenURL_Typed;
$_Search_Metasearch_Query=$URLBAR; $_Search_Metasearch_Query==$URL?$_Search_Metasearch_Query=prompt(_("Enter search query:"),sub("%s",_("Multisearch"),_("Search (%s)"))):0; 
$_Search_Metasearch_Query==""?0:&_Search_Metasearch_Open;
}
_Search_Metasearch_Open{
$_how=getpref(STRING,$_pref); $_Search_Metasearch_Query=urlencode($_Search_Metasearch_Query);
$_how=="ID_OPEN_LINK"?$_how=0:0; 
$_how=="ID_OPEN_LINK_IN_NEW_WINDOW"?$_how=0:0; 
$_how=="ID_OPEN_LINK_IN_BACKGROUND"?$_how=1:0; 
$_how=="ID_OPEN_LINK_IN_NEW_TAB"?$_how=0:0; 
$_how=="ID_OPEN_LINK_IN_BACKGROUNDTAB"?$_how=1:0; 
$URLBAR=$URL; $_i=0; while($_i>-1) &_Search_Metasearch_OpenLoop;
}
_Search_Metasearch_OpenLoop{
$__data=getpref(STRING,$_Search_pref_Meta.$_i.".url");
if ($__data!="") {
  $OpenURL=$__data.$_Search_Metasearch_Query;
  $_how==0?opentab($OpenURL):openbgtab($OpenURL);
  $_i=$_i+1;
} else {
  $_i = -1;
}
}

# Accels/Menus

_Search_BuildMenu{
# help menu
setmenu(KMForums,macro,"K-Meleon Forums &Search...",Search_KMForums);
# tools menu
setmenu("WebServices",popup,"&Web Search",ID_NAV_SEARCH);
setmenu("WebServices",command,"",ID_NAV_SEARCH);
setmenu("&Web Search",inline,_Search_Engines);
setmenu(_Search_Engines,inline,_Search_Engines_Configurable);
$_j=0; while($_j>-1) &_Search_BuildMenuLoop;
setmenu(_Search_Engines,inline,_Search_Engines_Metasearch);
getpref(STRING,$_Search_pref_Meta."0.url")==""?0:setmenu(_Search_Engines_Metasearch,macro,"&Multisearch",Search_Metasearch);
setmenu("&Web Search",inline,_Search_Config);
setmenu(_Search_Config,macro,"Use As &Default Engine",Search_ToggleEngineLock,0);
setmenu(_Search_Config,macro,"Con&figure...","kmPrefs(webfind)");
# context menus
setmenu(Selection,popup,"&Web Search");
# go button
setmenu(_Go_Clipboard,macro,"Paste And &Search",Search_PasteAndSearch);
}
_Search_BuildMenuLoop{
$__data=getpref(STRING,$_Search_pref_Engine.$_j.".name");
$__data==""?0:setmenu(_Search_Engines_Configurable,macro,$__data,"Search_Engine(".$_j.")");
$_j=$__data==""?-1:$_j+1;
}
_Search_SetAccels{
setaccel("CTRL G","macros(Search)");
setaccel("SHIFT VK_RETURN","macros(Search)");
setaccel("CTRL SHIFT V","macros(Search_PasteAndSearch)");
}
$OnInit=$OnInit."_Search_SetAccels;_Search_BuildMenu;";

# ------------------------------------------------------------------------------------------------------------------
$macroModules=$macroModules."Search;";