search-query.el (4819B)
1 ;;; search-query.el --- Search some websites -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2020 Jamie Beardslee 4 5 ;; Author: Jamie Beardslee <jdb@jamzattack.xyz> 6 ;; URL: https://git.jamzattack.xyz/search-query 7 ;; Version: 2020.11.17 8 ;; Keywords: net, comm 9 ;; Package-Requires: ((emacs "25.1")) 10 11 ;; This program is free software; you can redistribute it and/or modify 12 ;; it under the terms of the GNU General Public License as published by 13 ;; the Free Software Foundation, either version 3 of the License, or 14 ;; (at your option) any later version. 15 16 ;; This program is distributed in the hope that it will be useful, 17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 ;; GNU General Public License for more details. 20 21 ;; You should have received a copy of the GNU General Public License 22 ;; along with this program. If not, see <https://www.gnu.org/licenses/>. 23 24 ;;; Commentary: 25 26 ;; This file defines some functions to search my most commonly used 27 ;; websites. 28 29 ;;; Code: 30 31 (require 'url-util) 32 (require 'eww) 33 (require 'thingatpt) 34 35 (defvar search-query-engines nil 36 "A list of search engines. 37 Each element should have the form (FUNCTION . BUFFER-NAME), where 38 BUFFER-NAME is the name of the FUNCTION's dedicated eww buffer.") 39 40 (defun search-query-eww (url &optional buffer-name) 41 "Open URL in an eww buffer named BUFFER-NAME." 42 (if buffer-name 43 (with-current-buffer (get-buffer-create buffer-name) 44 (eww-setup-buffer) 45 (eww url)) 46 (eww url current-prefix-arg))) 47 48 ;;;###autoload 49 (cl-defmacro search-query-create 50 (name format-string docstring 51 &key (args '(query)) (format-args '(query)) interactive) 52 (declare (indent 1) 53 (doc-string 3)) 54 (let* ((function-name (intern (format "search-%s" name))) 55 (buffer-name (format "*eww %s*" name))) 56 (prog1 57 `(defun ,function-name ,args 58 ,docstring 59 (interactive ,(or interactive 60 (format "s%s: " (capitalize (symbol-name name))))) 61 (search-query-eww 62 (url-encode-url 63 (format ,format-string ,@format-args)) 64 ,buffer-name)) 65 (add-to-list 'search-query-engines (cons function-name buffer-name))))) 66 67 ;;;###autoload (autoload 'search-torrentz2 "search-query") 68 (search-query-create torrentz2 69 "https://torrentz2.eu/search?f=%s" 70 "Search torrentz2.eu for QUERY." 71 :interactive "sTorrent: ") 72 73 (defcustom search-query-tpb-mirror "thepiratebay.org" 74 "The Pirate Bay URL." 75 :type 'string) 76 77 ;;;###autoload (autoload 'search-tpb "search-query") 78 (search-query-create tpb 79 "https://%s/search/%s/0/99/0" 80 "Search The Pirate Bay for QUERY. 81 82 Uses `search-query-tpb-mirror' as the host." 83 :format-args (search-query-tpb-mirror query)) 84 85 (defcustom search-query-invidious-mirror "invidio.us" 86 "Your preferred invidious instance. 87 For a full list of instances, see 88 https://github.com/iv-org/invidious/wiki/Invidious-Instances" 89 :type 'string) 90 91 ;;;###autoload (autoload 'search-invidious "search-query") 92 (search-query-create invidious 93 "https://%s/search?q=%s" 94 "Search `search-query-invidious-mirror' for QUERY. 95 96 invidio.us is a more eww-friendly frontend for youtube." 97 :format-args (search-query-invidious-mirror query)) 98 99 ;;;###autoload (autoload 'search-wikipedia "search-query") 100 (search-query-create wikipedia 101 "https://%s.wikipedia.org/wiki/Special:Search?search=%s" 102 "Search wikipedia for QUERY. 103 With prefix arg, prompt for LANGUAGE." 104 :args (query &optional lang) 105 :format-args ((or lang "en") query) 106 :interactive (list (read-string "Wikipedia: ") 107 (when current-prefix-arg 108 (read-string "Language: ")))) 109 110 ;;;###autoload (autoload 'search-wiktionary "search-query") 111 (search-query-create wiktionary 112 "https://%s.wiktionary.org/wiki/%s" 113 "Search wiktionary for a WORD. 114 With prefix arg, prompt for LANGUAGE." 115 :interactive (list (read-string "Wiktionary: " 116 (thing-at-point 'word t)) 117 (when current-prefix-arg 118 (read-string "Language: "))) 119 :format-args ((or language "en") word) 120 :args (word &optional language)) 121 122 ;;;###autoload (autoload 'search-etymonline "search-query") 123 (search-query-create etymonline 124 "https://www.etymonline.com/word/%s" 125 "Search etymonline.com for a WORD. 126 Called interactively, prompt for a word with the default input 127 being the word at point." 128 :interactive (list (read-string "Etymology: " 129 (thing-at-point 'word t))) 130 :args (word) 131 :format-args (word)) 132 133 ;;;###autoload (autoload 'search-nethack "search-query") 134 (search-query-create nethack 135 "https://nethackwiki.com/wiki/Special:Search?search=%s" 136 "Search nethack wiki for QUERY.") 137 138 ;;;###autoload (autoload 'search-archwiki "search-query") 139 (search-query-create archwiki 140 "https://wiki.archlinux.org/index.php?search=%s" 141 "Search Arch wiki for QUERY.") 142 143 (provide 'search-query) 144 ;;; search-query.el ends here