branch: elpa/engine-mode commit c3f2de1a8a76e3e2fe413a911ea502e3d0dbc460 Author: Harry Schwartz <he...@harryrschwartz.com> Commit: Harry Schwartz <he...@harryrschwartz.com>
initial commit --- README.md | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ engine.el | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) diff --git a/README.md b/README.md new file mode 100644 index 0000000000..d58aabec40 --- /dev/null +++ b/README.md @@ -0,0 +1,89 @@ +# engine-mode + +`engine-mode` is a global minor mode for Emacs. It enables you to +easily define search engines, bind them to keybindings, and query them +from the comfort of your editor. + +For example, suppose we want to be able to easily search GitHub: + +```emacs +(defengine github + "https://github.com/search?ref=simplesearch&q=%s") +``` + +This defines an interactive function `engine/search-github`. When +executed it will take the selected region (or prompt for input, if no +region is selected) and search GitHub for it, displaying the results +in your default browser. + +The `defengine` macro can also take an optional key combination: + +```emacs +(defengine duckduckgo + "https://duckduckgo.com/?q=%s" + "C-c / d") +``` + +`C-c / d` is now bound to the new function `engine/search-duckduckgo`! +Nifty. + +## Installation + +`engine-mode` should soon be available on MELPA shortly. + +In the meantime, you can install it like any other elisp file by adding +it to your load path and globally enabling it: + +```emacs +(require 'engine) +(engine-mode t) +``` + +## Examples + +```emacs +(defengine amazon + "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=%s") + +(defengine duckduckgo + "https://duckduckgo.com/?q=%s" + "C-c / d") + +(defengine github + "https://github.com/search?ref=simplesearch&q=%s") + +(defengine google + "http://www.google.com/search?ie=utf-8&oe=utf-8&q=%s" + "C-c / g") + +(defengine google-images + "http://www.google.com/images?hl=en&source=hp&biw=1440&bih=795&gbv=2&aq=f&aqi=&aql=&oq=&q=%s") + +(defengine google-maps + "http://maps.google.com/maps?q=%s") + +(defengine project-gutenberg + "http://www.gutenberg.org/ebooks/search.html/?format=html&default_prefix=all&sort_order=&query=%s") + +(defengine rfcs + "http://pretty-rfc.herokuapp.com/search?q=%s") + +(defengine stack-overflow + "https://stackoverflow.com/search?q=%s") + +(defengine twitter + "https://twitter.com/search?q=%s") + +(defengine wikipedia + "http://www.wikipedia.org/search-redirect.php?language=en&go=Go&search=%s" + "C-c / w") + +(defengine wiktionary + "https://www.wikipedia.org/search-redirect.php?family=wiktionary&language=en&go=Go&search=%s") + +(defengine wolfram-alpha + "http://www.wolframalpha.com/input/?i=%s") + +(defengine youtube + "http://www.youtube.com/results?aq=f&oq=&search_query=%s") +``` diff --git a/engine.el b/engine.el new file mode 100644 index 0000000000..945850f392 --- /dev/null +++ b/engine.el @@ -0,0 +1,72 @@ +;;; engine.el --- Define and query search engines from within Emacs + +;; Copyright 2014 Harry Schwartz + +;; Author: Harry Schwartz <he...@harryrschwartz.com> +;; Version: 2014.05.06 +;; URL: https://github.com/hrs/engine-mode/engine.el + +;; This file is not part of GNU Emacs + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see <http://www.gnu.org/licenses/>. + +;;; Code: + +(define-minor-mode engine-mode + "Define search engines." + :global t + :keymap (make-sparse-keymap)) + +(defun engine/search-prompt (engine-name) + (concat "Search " (capitalize engine-name) ": ")) + +(defun engine/prompted-search-term (engine-name) + (read-string (engine/search-prompt engine-name))) + +(defun engine/get-query (engine-name) + "Return the selected region (if any) or prompt the user for a query." + (if mark-active + (buffer-substring (region-beginning) (region-end)) + (engine/prompted-search-term engine-name))) + +(defun engine/execute-search (search-engine-url search-term) + "Display the results of the query." + (interactive) + (browse-url + (format search-engine-url + (url-hexify-string search-term)))) + +(defun engine/function-name (engine-name) + (intern (concat "engine/search-" (downcase (symbol-name engine-name))))) + +(defun engine/docstring (engine-name) + (concat "Search " + (capitalize (symbol-name engine-name)) + " for the selected text. Prompt for input if none is selected.")) + +(defun engine/bind-key (engine-name keybinding) + (when keybinding + `(define-key engine-mode-map (kbd ,keybinding) + (quote ,(engine/function-name engine-name))))) + +(defmacro defengine (engine-name search-engine-url &optional keybinding) + `(progn (defun ,(engine/function-name engine-name) () + ,(engine/docstring engine-name) + (interactive) + (engine/execute-search ,search-engine-url + (engine/get-query ,(symbol-name engine-name)))) + ,(engine/bind-key engine-name keybinding))) + +(provide 'engine-mode) +;;; engine-mode.el ends here