xah-lookup.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. ;;; xah-lookup.el --- look up word on internet. -*- coding: utf-8; lexical-binding: t; -*-
  2. ;; Copyright © 2011-2020 by Xah Lee
  3. ;; Author: Xah Lee ( http://xahlee.info/ )
  4. ;; Version: 3.4.20200420082539
  5. ;; Package-Version: 20200420.1528
  6. ;; Package-Commit: c97a43dc0cebbfc519d0cce5b547dcc5e22b2085
  7. ;; Created: 14 Nov 2011
  8. ;; Package-Requires: ((emacs "24.1"))
  9. ;; Keywords: help, docs, convenience
  10. ;; License: GPL v3
  11. ;; URL: http://ergoemacs.org/emacs/xah-lookup.html
  12. ;; This file is not part of GNU Emacs.
  13. ;;; Commentary:
  14. ;; This package provide commands for looking up the web of word under cursor.
  15. ;; • xah-lookup-word-on-internet
  16. ;; • xah-lookup-web
  17. ;; • xah-lookup-wikipedia
  18. ;; • xah-lookup-word-definition
  19. ;; • xah-lookup-word-dict-org
  20. ;; • xah-lookup-wiktionary
  21. ;; • xah-lookup-etymology
  22. ;; If there's a text selection (a phrase you want to lookup), these commands will lookup the selected text.
  23. ;;; INSTALL:
  24. ;; To install manually, place this file xah-lookup.el in the directory ~/.emacs.d/lisp/
  25. ;; Then, place the following code in your emacs init file
  26. ;; (add-to-list 'load-path "~/.emacs.d/lisp/")
  27. ;; (require 'xah-lookup)
  28. ;;; CUSTOMIZATION
  29. ;; If you prefer to use emacs builtin browser eww, put the following in your emacs init
  30. ;; (setq xah-lookup-browser-function 'eww) ; must come before loading xah-lookup
  31. ;; (require 'eww)
  32. ;; (require 'xah-lookup)
  33. ;; for operating system's default browser, use 'browse-url instead of 'eww
  34. ;; Each command can use a different URL or browser. For example:
  35. ;; (require 'xah-lookup)
  36. ;; (require 'eww)
  37. ;; (put 'xah-lookup-word-definition 'xah-lookup-url "http://www.thefreedictionary.com/word02051")
  38. ;; (put 'xah-lookup-word-definition 'xah-lookup-browser-function 'eww)
  39. ;; To change/add keys, put the following in your emacs init. For example:
  40. ;; (define-key help-map (kbd "7") 'xah-lookup-web) ; C-h 7
  41. ;; or
  42. ;; (global-set-key (kbd "<f2>") 'xah-lookup-word-definition) ; F2
  43. ;; You can also create your own lookup command to lookup ruby, php, clojure, etc.
  44. ;; For example:
  45. ;; (defun my-lookup-php (&optional @word)
  46. ;; "lookup php doc of word under cursor"
  47. ;; (interactive)
  48. ;; (require 'xah-lookup)
  49. ;; (xah-lookup-word-on-internet
  50. ;; @word
  51. ;; (get 'my-lookup-php 'xah-lookup-url )
  52. ;; (get 'my-lookup-php 'xah-lookup-browser-function )))
  53. ;; (put 'my-lookup-php 'xah-lookup-url "http://us.php.net/word02051")
  54. ;; (put 'my-lookup-php 'xah-lookup-browser-function 'browse-url)
  55. ;; See: http://ergoemacs.org/emacs/xah-lookup.html
  56. ;; Like it?
  57. ;; Buy Xah Emacs Tutorial
  58. ;; http://ergoemacs.org/emacs/buy_xah_emacs_tutorial.html
  59. ;;; HISTORY:
  60. ;; 2017-02-09 each command can be costomized for lookup URL and browser to use.
  61. ;; 2014-10-20 changes are no longer logged here, unless major.
  62. ;; version 1.5, 2013-04-21 removed xah-lookup-php-ref. Doesn't belong here.
  63. ;; version 1.4, 2013-03-23 added 2 more dict to the xah-lookup-dictionary-list. Good for vocabulary researchers
  64. ;; version 1.3, 2012-05-11 added “xah-lookup-xah-lookup-dictionary-list”.
  65. ;; version 1.2, 2012-05-10 added “xah-lookup-answers.com”. Improved inline docs.
  66. ;; version 1.1, 2012-05-09 changed the input from 「'symbol」 to 「'word」. Changed the English dictionary used from 「http://www.answers.com/main/ntquery?s=word02051」 to 「http://www.thefreedictionary.com/word02051」.
  67. ;; version 1.0, 2011-11-14 First released to public.
  68. ;;; Code:
  69. (require 'browse-url) ; in emacs
  70. (defcustom
  71. xah-lookup-browser-function
  72. 'browse-url
  73. "Function to call to launch browser. Default is 'browse-url. You can also use 'eww. For dictionary lookup, use `xah-lookup-dictionary-browser-function'"
  74. :group 'xah-lookup
  75. )
  76. (defcustom
  77. xah-lookup-dictionary-browser-function
  78. 'browse-url
  79. "Function to call for English definition lookup. Default is 'browse-url. You can also use 'eww. For dictionary lookup, use `xah-lookup-dictionary-browser-function'"
  80. :group 'xah-lookup)
  81. (defcustom
  82. xah-lookup-dictionary-list
  83. [
  84. "http://www.dict.org/bin/Dict?Form=Dict2&Database=*&Query=word02051" ; 1913 Webster, WordNet
  85. "http://www.thefreedictionary.com/word02051" ; AHD
  86. "https://en.wiktionary.org/wiki/word02051"
  87. "https://www.google.com/search?q=define:+word02051" ; google
  88. "http://www.etymonline.com/index.php?search=word02051" ; etymology
  89. ]
  90. "A vector of website URLs for lookup words. Used by `xah-lookup-all-dictionaries'. http://wordyenglish.com/words/dictionary_tools.html "
  91. :group 'xah-lookup)
  92. (defun xah-lookup--asciify-region (&optional @from @to)
  93. "Change some Unicode characters into equivalent ASCII ones.
  94. For example, “passé” becomes “passe”.
  95. This function works on chars in European languages, and does not transcode arbitrary Unicode chars (such as Greek, math symbols). Un-transformed unicode char remains in the string.
  96. When called interactively, work on text selection or current line.
  97. Version 2014-10-20"
  98. (interactive
  99. (if (use-region-p)
  100. (list (region-beginning) (region-end))
  101. (list (line-beginning-position) (line-end-position))))
  102. (let ((case-fold-search t))
  103. (save-restriction
  104. (narrow-to-region @from @to)
  105. (mapc
  106. (lambda ($pair)
  107. (goto-char (point-min))
  108. (while (search-forward-regexp (elt $pair 0) (point-max) t)
  109. (replace-match (elt $pair 1))))
  110. [
  111. ["á\\|à\\|â\\|ä\\|ã\\|å" "a"]
  112. ["é\\|è\\|ê\\|ë" "e"]
  113. ["í\\|ì\\|î\\|ï" "i"]
  114. ["ó\\|ò\\|ô\\|ö\\|õ\\|ø" "o"]
  115. ["ú\\|ù\\|û\\|ü" "u"]
  116. ["Ý\\|ý\\|ÿ" "y"]
  117. ["ñ" "n"]
  118. ["ç" "c"]
  119. ["ð" "d"]
  120. ["þ" "th"]
  121. ["ß" "ss"]
  122. ["æ" "ae"]
  123. ]))))
  124. (defun xah-lookup--asciify-string (@string)
  125. "Change some Unicode characters into equivalent ASCII ones.
  126. For example, “passé” becomes “passe”.
  127. See `xah-lookup--asciify-region'
  128. Version 2014-10-20"
  129. (with-temp-buffer
  130. (insert @string)
  131. (xah-lookup--asciify-region (point-min) (point-max))
  132. (buffer-string)))
  133. (defun xah-lookup-word-on-internet (&optional @word @url @browser-function)
  134. "Look up current word or text selection in a online reference site.
  135. This command launches/switches you to default browser.
  136. @URL a is URL string in this form: 「https://en.wiktionary.org/wiki/word02051」.
  137. the 「word02051」 is a placeholder for the query string.
  138. If @URL is nil, Google Search is used.
  139. For a list of online reference sites, see:
  140. URL `http://ergoemacs.org/emacs/xah-lookup.html'
  141. Version 2017-02-09"
  142. (interactive)
  143. (let ($word $refUrl $myUrl)
  144. (setq $word
  145. (if @word
  146. @word
  147. (if (region-active-p)
  148. (buffer-substring-no-properties (region-beginning) (region-end))
  149. (current-word))))
  150. (setq $word (replace-regexp-in-string " " "%20" (xah-lookup--asciify-string $word)))
  151. (setq $refUrl
  152. (if @url
  153. @url
  154. "https://www.google.com/search?q=word02051" ))
  155. (setq $myUrl (replace-regexp-in-string "word02051" $word $refUrl t t))
  156. (if @browser-function
  157. (funcall @browser-function $myUrl)
  158. (funcall xah-lookup-browser-function $myUrl))))
  159. ;;;###autoload
  160. (defun xah-lookup-google (&optional @word)
  161. "Lookup current word or text selection in Google Search.
  162. Version 2017-02-09"
  163. (interactive)
  164. (xah-lookup-word-on-internet
  165. @word
  166. (get 'xah-lookup-google 'xah-lookup-url)
  167. (get 'xah-lookup-google 'xah-lookup-browser-function )))
  168. (put 'xah-lookup-google 'xah-lookup-url "https://www.google.com/search?q=word02051")
  169. (put 'xah-lookup-google 'xah-lookup-browser-function xah-lookup-browser-function)
  170. ;;;###autoload
  171. (defun xah-lookup-web (&optional @word)
  172. "Lookup current word or text selection in web search.
  173. Version 2017-02-09"
  174. (interactive)
  175. (xah-lookup-word-on-internet
  176. @word
  177. (get 'xah-lookup-web 'xah-lookup-url)
  178. (get 'xah-lookup-web 'xah-lookup-browser-function )))
  179. (put 'xah-lookup-web 'xah-lookup-url "https://duckduckgo.com/?q=word02051")
  180. (put 'xah-lookup-web 'xah-lookup-browser-function xah-lookup-browser-function)
  181. ;;;###autoload
  182. (defun xah-lookup-wikipedia (&optional @word)
  183. "Lookup current word or text selection in Wikipedia.
  184. Version 2017-02-09"
  185. (interactive)
  186. (xah-lookup-word-on-internet
  187. @word
  188. (get 'xah-lookup-wikipedia 'xah-lookup-url )
  189. (get 'xah-lookup-wikipedia 'xah-lookup-browser-function )))
  190. (put 'xah-lookup-wikipedia 'xah-lookup-url "http://en.wikipedia.org/wiki/word02051")
  191. (put 'xah-lookup-wikipedia 'xah-lookup-browser-function xah-lookup-browser-function)
  192. ;;;###autoload
  193. (defun xah-lookup-word-definition (&optional @word)
  194. "Lookup definition of current word or text selection in URL `http://www.thefreedictionary.com/curlicue'.
  195. Version 2017-02-09"
  196. (interactive)
  197. (xah-lookup-word-on-internet
  198. @word
  199. (get 'xah-lookup-word-definition 'xah-lookup-url )
  200. (get 'xah-lookup-word-definition 'xah-lookup-browser-function ))
  201. ;;
  202. )
  203. (put 'xah-lookup-word-definition 'xah-lookup-url "https://www.ahdictionary.com/word/search.html?q=word02051")
  204. (put 'xah-lookup-word-definition 'xah-lookup-url "http://www.thefreedictionary.com/word02051")
  205. (put 'xah-lookup-word-definition 'xah-lookup-browser-function 'browse-url)
  206. (defun xah-lookup-word-dict-org (&optional @word)
  207. "Lookup definition of current word or text selection in URL `http://dict.org/'.
  208. Version 2017-02-09"
  209. (interactive)
  210. (xah-lookup-word-on-internet
  211. @word
  212. (get 'xah-lookup-word-dict-org 'xah-lookup-url )
  213. (get 'xah-lookup-word-dict-org 'xah-lookup-browser-function )))
  214. (put 'xah-lookup-word-dict-org 'xah-lookup-url "http://www.dict.org/bin/Dict?Form=Dict2&Database=*&Query=word02051")
  215. (put 'xah-lookup-word-dict-org 'xah-lookup-browser-function 'eww)
  216. (defun xah-lookup-wiktionary (&optional @word)
  217. "Lookup definition of current word or text selection in URL `https://en.wiktionary.org/'
  218. Version 2017-02-09"
  219. (interactive)
  220. (xah-lookup-word-on-internet
  221. @word
  222. (get 'xah-lookup-wiktionary 'xah-lookup-url )
  223. (get 'xah-lookup-wiktionary 'xah-lookup-browser-function )))
  224. (put 'xah-lookup-wiktionary 'xah-lookup-url "https://en.wiktionary.org/wiki/word02051")
  225. (put 'xah-lookup-wiktionary 'xah-lookup-browser-function xah-lookup-browser-function)
  226. (defun xah-lookup-etymology (&optional @word)
  227. "Lookup etymology of current word or text selection in URL `http://www.etymonline.com/search?q=curlicue'.
  228. Version 2018-08-15"
  229. (interactive)
  230. (xah-lookup-word-on-internet
  231. @word
  232. (get 'xah-lookup-etymology 'xah-lookup-url )
  233. (get 'xah-lookup-etymology 'xah-lookup-browser-function )))
  234. (put 'xah-lookup-etymology 'xah-lookup-url "http://www.etymonline.com/search?q=word02051")
  235. (put 'xah-lookup-etymology 'xah-lookup-browser-function xah-lookup-browser-function)
  236. (defun xah-lookup-all-dictionaries (&optional @word)
  237. "Lookup definition in many dictionaries.
  238. Current word or text selection is used as input.
  239. The dictionaries used are in `xah-lookup-dictionary-list'."
  240. (interactive)
  241. (mapc
  242. (lambda ($url)
  243. (xah-lookup-word-on-internet
  244. @word
  245. $url
  246. (get 'xah-lookup-all-dictionaries 'xah-lookup-browser-function )))
  247. xah-lookup-dictionary-list))
  248. (put 'xah-lookup-all-dictionaries 'xah-lookup-browser-function 'browse-url)
  249. (define-key help-map (kbd "1") 'xah-lookup-etymology)
  250. (define-key help-map (kbd "2") 'xah-lookup-word-dict-org)
  251. (define-key help-map (kbd "3") 'xah-lookup-web)
  252. (define-key help-map (kbd "4") 'xah-lookup-word-definition)
  253. (define-key help-map (kbd "5") 'xah-lookup-wikipedia)
  254. (define-key help-map (kbd "6") 'xah-lookup-wiktionary)
  255. (define-key help-map (kbd "7") 'xah-lookup-all-dictionaries)
  256. (define-key help-map (kbd "9") 'xah-lookup-word-definition)
  257. (provide 'xah-lookup)
  258. ;;; xah-lookup.el ends here