Added searchengines contrib extension

It allows handy searching of term in question with various existing
search engines. For details, see README.org.
This commit is contained in:
Alex Ermolov 2016-09-21 12:34:41 +03:00
parent 02262133df
commit ddb63cfd76
4 changed files with 156 additions and 0 deletions

View file

@ -0,0 +1,59 @@
** Problem
While being connected to Internet, there is the need to search something raising
quite regularly. But doing it "manually" is often tedious and error-prone.
Imagine, to search something on Google for example, you should open/switch to
your browser, open Google tab in it, type Enter ... PROFIT!.
Most of us know the motto "Lean on your tools and automate everything you can"
by ESR. So this contrib extension aims on exactly this - it automates internet
searches as far as it can. With it you can define search engine parameters - its
template url, stumpwm keybindings, and maybe something more in the future.
** Usage
Add this to your =.stumpwmrc=:
#+BEGIN_SRC lisp
(load-module "searchengines")
#+END_SRC
Then you should define the browser (with appropriate parameters) to be used by
this extension, for example:
#+BEGIN_SRC lisp
(setf searchengines:*search-browser-executable* "firefox")
(setf searchengines:*search-browser-params* "-new-tab")
#+END_SRC
Then you are to define one or more search engine url templates:
#+BEGIN_SRC lisp
(defparameter *URL-GOOGLE* "http://www.google.com/search?num=100&q=~a")
#+END_SRC
Where #'format parameter placeholder will be later substituted by actual search term.
And finally now on you can define three types of search engines, namely
"selection", "prompt" and "augmented":
#+BEGIN_SRC lisp
(searchengines:make-searchengine-selection "search-google-selection" *URL-GOOGLE* "Google search" :map *search-keymap* :key "s-g")
(searchengines:make-searchengine-prompt "search-google-prompt" "Google" *URL-GOOGLE* "Google search" :map *search-keymap* :key "s-C-g")
(searchengines:make-searchengine-augmented "search-google-augmented" "Augmented Google" *URL-GOOGLE* "Google search" :map *search-keymap* :key "C-g")
#+END_SRC
"Selection" type will use primary clipboard contents as a search term and "prompt" will
use a prompt to ask for it. "Augmented" search type uses primary clipboard
contents, but augments it with additional term(s), being typed in with prompt,
so in fact it is a mix of the previous two.
All above macros have common lambda lists - here are the details:
- name defines StumpWM command name to be created
- caption sets the prompt text to be used
- url is the actual search engine URI template
- docstring is self-describing, I hope ;)
There is a bit more parameters to be considered, all of them are keyword
parameters:
- map defines StumpWM keymap where the keybinding will be defined
- key stands for actual keybinding to be used
- binded is boolean parameter which tells if search engine will ever be binded
to a key.
These keyword paramaters are used exclusively to define search engine
keybinding. If you are not planning to bind it to some key, you may omit these
parameters. In this case you can always define the keybinding for search engine
command manually later.

View file

@ -0,0 +1,14 @@
;;;; package.lisp
(defpackage #:searchengines
(:use #:cl :stumpwm :drakma))
(in-package #:searchengines)
(import '(
drakma:url-encode
stumpwm::defcommand
stumpwm::get-x-selection
stumpwm::message-no-timeout
stumpwm::run-shell-command
))

View file

@ -0,0 +1,10 @@
;;;; searchengines.asd
(asdf:defsystem #:searchengines
:serial t
:description "Allows searching text using prompt or clipboard contents with various search engines"
:author "Alex Ermolov"
:license "GPLv3"
:depends-on (#:stumpwm #:drakma)
:components ((:file "package")
(:file "searchengines")))

View file

@ -0,0 +1,73 @@
(in-package #:searchengines)
(export '(
*search-browser-executable*
*search-browser-params*
make-searchengine-prompt
make-searchengine-selection
make-searchengine-augmented
))
(defvar *search-browser-executable* nil
"Browser to use while performing searches")
(defvar *search-browser-params* nil
"Additional executable parameters for searching browser")
(defun preprocess-and-search (url search &optional (raw-search nil))
(let* ((search-processed (if raw-search
search
(url-encode search :utf-8)))
(uri (format nil url search-processed)))
(if (eql *search-browser-executable* nil)
(message-no-timeout "stumpwm::*search-browser-executable* is nil, set it first")
(run-shell-command
(concatenate 'string
*search-browser-executable*
" " (format nil "~{~A~^ ~}" *search-browser-params*)
" \"" uri "\"")))))
(defmacro make-searchengine-prompt (name caption url docstring
&key (map nil) (key nil) (binded t))
`(progn
(if ,map
(define-key ,map (kbd ,key) nil))
(defcommand ,(intern (string-upcase name)) (search)
((:string ,(concatenate 'string "Search in " caption " for: ")))
,docstring
(when search
(check-type search string)
(preprocess-and-search ,url search)
(,(intern (string-upcase *search-browser-executable*)))))
,(when (and map key binded)
`(define-key ,map (kbd ,key) ,(string-downcase (string name))))))
(defmacro make-searchengine-selection (name url docstring
&key (map nil) (key nil) (binded t))
`(progn
(if ,map
(define-key ,map (kbd ,key) nil))
(defcommand ,(intern (string-upcase name)) () ()
,docstring
(preprocess-and-search ,url (get-x-selection))
(,(intern (string-upcase *search-browser-executable*))))
,(when (and map key binded)
`(define-key ,map (kbd ,key) ,(string-downcase (string name))))))
(defmacro make-searchengine-augmented (name caption url docstring
&key (map nil) (key nil) (binded t))
`(progn
(if ,map
(define-key ,map (kbd ,key) nil))
(defcommand ,(intern (string-upcase name)) (augmentation)
((:string ,(concatenate 'string "Augment " caption " search: ")))
,docstring
(when augmentation
(check-type augmentation string)
(preprocess-and-search ,url (concatenate
'string
(url-encode augmentation :utf-8) " "
(url-encode (get-x-selection) :utf-8))
t)
(,(intern (string-upcase *search-browser-executable*)))))
,(when (and map key binded)
`(define-key ,map (kbd ,key) ,(string-downcase (string name))))))