Last active
July 2, 2019 15:00
-
-
Save wsgac/01d7b123c08a93ce85dd4023bdcaad0a to your computer and use it in GitHub Desktop.
Interactive dictionary/Google lookup fro StumpWM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; -*- lisp -*- | |
(ql:quickload :quri) ; For encoding URLs | |
(in-package :stumpwm) | |
;; My preferred key bindings | |
(define-key *top-map* (kbd "s-d") "lookup-selection") | |
(define-key *top-map* (kbd "s-g") "google-selection") | |
(defparameter *dictionaries* | |
'(:en "https://www.thefreedictionary.com/~a" | |
:ja "https://jisho.org/search/~a" | |
:ru "http://en.pons.com/translate?q=~a&l=enru")) | |
(defun %lookup-selection (lang &key (dictionaries *dictionaries*)) | |
"" | |
(let ((url-template (getf dictionaries (make-keyword (string-upcase lang))))) | |
(unless url-template | |
(error "Unknown lookup language: ~a" lang)) | |
(let* ((phrase (read-one-line (current-screen) | |
(format nil "Lookup [~a]: " lang) | |
:initial-input (get-x-selection))) | |
(url (format nil url-template | |
(quri::url-encode (or phrase ; Non-local exit if empty | |
(return-from %lookup-selection nil)))))) | |
(run-shell-command (format nil "xdg-open '~a'" url))))) | |
(defcommand lookup-selection () () | |
(let ((lang (completing-read (current-screen) "Language: " | |
(loop for (k v) on *DICTIONARIES* by #'cddr | |
collect (symbol-name k) | |
collect (string-downcase | |
(symbol-name k))) | |
:require-match t))) | |
(%lookup-selection (or lang | |
(return-from lookup-selection))))) | |
(defun %google-selection () | |
"Interactively obtain a lookup phrase. By default suggest current X | |
selection. Pass the phrase to Google and open result in the browser." | |
(let* ((phrase (read-one-line (current-screen) "Google Lookup: " | |
:initial-input (get-x-selection))) | |
(url (format nil "https://www.google.com/search?q=~a" | |
(quri::url-encode (or phrase | |
(return-from %google-selection nil)))))) | |
(run-shell-command (format nil "xdg-open ~a" url)))) | |
(defcommand google-selection () () | |
(%google-selection)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Switch to a more generic dictionary lookup method with a two-level query. First, you pick an available language, then you input a phrase or accept the one suggested by taking X selection.