Last active
October 19, 2016 19:36
-
-
Save logcat/733a23ab7949c73eb7aa0ae667d59590 to your computer and use it in GitHub Desktop.
Sends word from emacs to open google translate tab or redfoxsanakirja tab in chrome (works on OSX, depends on https://github.com/prasmussen/chrome-cli/)
This file contains hidden or 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
| (define-minor-mode lang-mode | |
| "Lang mode" | |
| :lighter " lang") | |
| (require 'thingatpt) | |
| (defun tabs-raw () | |
| (shell-command-to-string "chrome-cli list tabs")) | |
| (defun tabs () (split-string (tabs-raw) "\n" t)) | |
| (defun get-id (tab) | |
| (if (string-match "[0-9]*]" tab) | |
| (substring (match-string 0 tab) 0 -1))) | |
| (defun tab-ids () | |
| (mapcar 'get-id (tabs))) | |
| (defun tab-details (id) | |
| (shell-command-to-string (concat "chrome-cli info -t " id))) | |
| (defun is-website? (tab-id url) | |
| (string-match url (tab-details tab-id))) | |
| (defun find-tab (match-function) | |
| (car (seq-filter match-function (tab-ids)))) | |
| (defun activate-tab (tab) | |
| (shell-command (concat "chrome-cli activate -t " tab))) | |
| (defun chrome-shell-command (script tab) | |
| (concat "chrome-cli execute '" | |
| script | |
| "' -t " | |
| tab)) | |
| (defun chrome-activate-send (tab shell-command-builder text) | |
| (activate-tab tab) | |
| (shell-command (funcall shell-command-builder text tab))) | |
| (defun is-google-translate? (tab-id) | |
| (is-website? tab-id "translate.google.com")) | |
| (defun google-translate-javascript (text) | |
| (concat "document.getElementById(\"source\").value = \"" text "\"")) | |
| (defun google-translate-shell-command (text tab) | |
| (chrome-shell-command (google-translate-javascript text) tab)) | |
| (defun lang-mode-google-translate () | |
| (interactive) | |
| (let ((word (thing-at-point 'word))) | |
| (message word) | |
| (chrome-activate-send (find-tab 'is-google-translate?) 'google-translate-shell-command word))) | |
| (global-set-key (kbd "M-n") 'lang-mode-google-translate) | |
| (defun is-redfox? (tab-id) | |
| (is-website? tab-id "redfoxsanakirja.fi")) | |
| (defun redfox-javascript (text) | |
| (concat "document.getElementById(\"_redfoxportaalisanakirja_WAR_redfoxportaalisanakirja_:dictionary-form:query_word\").value = \"" text "\";" | |
| "document.getElementById(\"_redfoxportaalisanakirja_WAR_redfoxportaalisanakirja_:dictionary-form:submitBtn\").click()")) | |
| (defun redfox-shell-command (text tab) | |
| (chrome-shell-command (redfox-javascript text) tab)) | |
| (defun lang-mode-redfox-translate () | |
| (interactive) | |
| (let ((word (thing-at-point 'word))) | |
| (message word) | |
| (chrome-activate-send (find-tab 'is-redfox?) 'redfox-shell-command word))) | |
| (global-set-key (kbd "M-p") 'lang-mode-redfox-translate) | |
| (provide 'lang-mode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment