60442dc47b9092375bb41a1f803b25389c834427

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

web-search: allow custom search engines

Diff

 1diff --git a/plugins/web-search/README.md b/plugins/web-search/README.md
 2index 81908fb941298f192210bdda93acf2397aaf97ff..ad5080a448b2a1b209c47ae6448f5dc45c20fb53 100644
 3--- a/plugins/web-search/README.md
 4+++ b/plugins/web-search/README.md
 5@@ -52,3 +52,27 @@ Also there are aliases for bang-searching DuckDuckGo:
 6 | `map`     | `!m`  |
 7 | `image`   | `!i`  |
 8 | `ducky`   | `!`   |
 9+
10+### Custom search engines
11+
12+If you want to add other search contexts to the plugin, you can use the
13+`$ZSH_WEB_SEARCH_ENGINES` variable. Set it before Oh My Zsh is sourced,
14+with the following format:
15+
16+```zsh
17+ZSH_WEB_SEARCH_ENGINES=(
18+    <context> <URL>
19+    <context> <URL>
20+)
21+```
22+
23+where `<context>` is the name of the search context, and `<URL>` a URL of
24+the same type as the search contexts above. For example, to add `reddit`,
25+you'd do:
26+
27+```zsh
28+ZSH_WEB_SEARCH_ENGINES=(reddit "https://www.reddit.com/search/?q=")
29+```
30+
31+These custom search engines will also be turned to aliases, so you can
32+both do `web_search reddit <query>` or `reddit <query>`.
33diff --git a/plugins/web-search/web-search.plugin.zsh b/plugins/web-search/web-search.plugin.zsh
34index 326a30745aa4a680402c99fb99f65eeb760f1b9b..e8842d90601a37ba0c57810be7bfcb98b63ad223 100644
35--- a/plugins/web-search/web-search.plugin.zsh
36+++ b/plugins/web-search/web-search.plugin.zsh
37@@ -6,6 +6,7 @@ function web_search() {
38   # define search engine URLS
39   typeset -A urls
40   urls=(
41+    $ZSH_WEB_SEARCH_ENGINES
42     google      "https://www.google.com/search?q="
43     bing        "https://www.bing.com/search?q="
44     yahoo       "https://search.yahoo.com/search?p="
45@@ -25,7 +26,7 @@ function web_search() {
46 
47   # check whether the search engine is supported
48   if [[ -z "$urls[$1]" ]]; then
49-    echo "Search engine $1 not supported."
50+    echo "Search engine '$1' not supported."
51     return 1
52   fi
53 
54@@ -67,3 +68,13 @@ alias youtube='web_search duckduckgo \!yt'
55 alias map='web_search duckduckgo \!m'
56 alias image='web_search duckduckgo \!i'
57 alias ducky='web_search duckduckgo \!'
58+
59+# other search engine aliases
60+if [[ ${#ZSH_WEB_SEARCH_ENGINES} -gt 0 ]]; then
61+  typeset -A engines
62+  engines=($ZSH_WEB_SEARCH_ENGINES)
63+  for key in ${(k)engines}; do
64+    alias "$key"="web_search $key"
65+  done
66+  unset engines key
67+fi