-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords.el
438 lines (365 loc) · 12 KB
/
words.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
;;; words.el --- Functions to operate on word at point or region -*- lexical-binding: t -*-
;; Copyright(C) 2014,2015 John Kitchin
;; Author: John Kitchin <[email protected]>
;; Version: 0.1.0
;; Package-Requires: ((hydra "0"))
;;; Commentary:
;; These functions mostly provide easy access to web-based searches of the word
;; at point, or the selected words. The following functions are available.
;; - words-dictionary
;; - words-thesaurus
;; - words-atd :: a spell/grammar checker
;; - words-google
;; - words-twitter
;; - words-google-scholar
;; - words-scopus
;; - words-wos :: Search Web of Science
;; - words-crossref
;; - words-pubmed
;; - words-bibtex :: search default bibtex file
;; - words-mdfind :: search local computer with mdfind (Mac)
;; - words-finder :: search local computer with finder (Mac)
;; These functions just open websites for convenience.
;; - wos :: open Web of Science
;; - pubmed :: open pubmed
;; - scopus :: open Scopus
;; - words :: offers a menu of functions for word at point or region
;; - words/body will open a "hydra" menu.
;;; Code:
(require 'hydra)
(require 'url)
(require 'xml)
(setq hydra-is-helpful t)
;; * Dictionary/thesaurus/grammar
(defun words-dictionary ()
"Look up word at point in an online dictionary."
(interactive)
(browse-url
(format
"http://dictionary.reference.com/browse/%s?s=t"
(thing-at-point 'word))))
(defun words-thesaurus ()
"Look up word at point in an online thesaurus."
(interactive)
(browse-url
(format
"http://www.thesaurus.com/browse/%s"
(thing-at-point 'word))))
(defun words-atd ()
"Send paragraph at point to After the deadline for spell and grammar checking."
(interactive)
(let* ((url-request-method "POST")
(url-request-data (format
"key=some-random-text-&data=%s"
(url-hexify-string
(thing-at-point 'paragraph))))
(xml (with-current-buffer
(url-retrieve-synchronously
"http://service.afterthedeadline.com/checkDocument")
(xml-parse-region url-http-end-of-headers (point-max))))
(results (car xml))
(errors (xml-get-children results 'error)))
(switch-to-buffer-other-frame "*ATD*")
(erase-buffer)
(dolist (err errors)
(let* ((children (xml-node-children err))
;; for some reason I could not get the string out, and had to do this.
(s (car (last (nth 1 children))))
;; the last/car stuff doesn't seem right. there is probably
;; a more idiomatic way to get this
(desc (last (car (xml-get-children children 'description))))
(type (last (car (xml-get-children children 'type))))
(suggestions (xml-get-children children 'suggestions))
(options (xml-get-children (xml-node-name suggestions) 'option))
(opt-string (mapconcat
(lambda (el)
(when (listp el)
(car (last el))))
options
" ")))
(insert (format "** %s ** %s
Description: %s
Suggestions: %s
" s type desc opt-string))))))
;; * Web functions
(defun words-google ()
"Google the word at point or selection."
(interactive)
(browse-url
(format
"http://www.google.com/search?q=%s"
(if (region-active-p)
(url-hexify-string (buffer-substring (region-beginning)
(region-end)))
(thing-at-point 'word)))))
(defun words-twitter ()
"Search twitter for word at point or selection."
(interactive)
(browse-url
(format
"https://twitter.com/search?q=%s"
(if (region-active-p)
(url-hexify-string (buffer-substring (region-beginning)
(region-end)))
(thing-at-point 'word)))))
;; * Scientific search functions
(defun words-google-scholar ()
"Google scholar the word at point or selection."
(interactive)
(browse-url
(format
"http://scholar.google.com/scholar?q=%s"
(if (region-active-p)
(url-hexify-string (buffer-substring (region-beginning)
(region-end)))
(thing-at-point 'word)))))
(defun words-wos ()
"Open the word at point or selection in Web of Science."
;; the url was derived from this page: http://wokinfo.com/webtools/searchbox/
(interactive)
(browse-url
(format "http://gateway.webofknowledge.com/gateway/Gateway.cgi?topic=%s&GWVersion=2&SrcApp=WEB&SrcAuth=HSB&DestApp=UA&DestLinkType=GeneralSearchSummary"
(if (region-active-p)
(mapconcat 'identity (split-string
(buffer-substring (region-beginning)
(region-end))) "+")
(thing-at-point 'word)))))
(defun words-scopus ()
"Scopus the word at point or selection."
(interactive)
(browse-url
(format
"http://www.scopus.com//search/submit/basic.url?field1=TITLE-ABS-KEY&searchterm1=%s"
(if (region-active-p)
(mapconcat 'identity (split-string
(buffer-substring (region-beginning)
(region-end))) "+")
(thing-at-point 'word)))))
(defun words-crossref ()
"Search region or word at point in CrossRef."
(interactive)
(browse-url
(format
"http://search.crossref.org/?q=%s"
(if (use-region-p)
(url-hexify-string (buffer-substring
(region-beginning)
(region-end)))
(thing-at-point 'word)))))
(defun words-pubmed ()
"Search region or word at point in pubmed."
(interactive)
(browse-url
(format
"http://www.ncbi.nlm.nih.gov/pubmed/?term=%s"
(if (use-region-p)
(url-hexify-string (buffer-substring
(region-beginning)
(region-end)))
(thing-at-point 'word)))))
(defun words-arxiv ()
"Search region or word at point in arxiv.org."
(interactive)
(browse-url
(format
"http://arxiv.org/find/all/1/all:+AND+%s/0/1/0/all/0/1"
(if (use-region-p)
(url-hexify-string (buffer-substring
(region-beginning)
(region-end)))
(thing-at-point 'word)))))
(defun words-semantic-scholar ()
"Search region or word at point in www.semanticscholar.org."
(interactive)
(browse-url
(format
"https://www.semanticscholar.org/search?q=%s"
(if (use-region-p)
(url-hexify-string (buffer-substring
(region-beginning)
(region-end)))
(thing-at-point 'word)))))
;; ** Convenience functions for scientific queries
;; These just open websites, with no search queries.
(defun wos ()
"Open Web of Science search page in browser."
(interactive)
(browse-url "http://apps.webofknowledge.com"))
(defun pubmed ()
"Open Pubmed in browser."
(interactive)
(browse-url "http://www.ncbi.nlm.nih.gov/pubmed"))
(defun scopus ()
"Open Scopus in browser."
(interactive)
(browse-url "http://www.scopus.com"))
(defun crossref ()
"Open Crossref in browser."
(interactive)
(browse-url "http://search.crossref.org"))
;; * Bibtex search
(defun words-bibtex ()
"Find selected region or word at point in variable `reftex-default-bibliography'."
(interactive)
(multi-occur
(mapcar (lambda (f) (find-file-noselect f))
reftex-default-bibliography)
(if (use-region-p)
(buffer-substring
(region-beginning)
(region-end))
(thing-at-point 'word))))
;; * Search functions for Mac
(defvar words-voice "Samantha"
"Mac voice to use for speaking.")
(defun words-speak (&optional text speed)
"Speak word at point or region or TEXT. Mac only."
(interactive)
(setq speed (number-to-string (or speed 180)))
(unless text
(setq text (if (use-region-p)
(buffer-substring
(region-beginning) (region-end))
(thing-at-point 'word))))
;; escape some special applescript chars
(setq text (replace-regexp-in-string "\\\\" "\\\\\\\\" text))
(setq text (replace-regexp-in-string "\"" "\\\\\"" text))
;; (do-applescript
;; (format
;; "say \"%s\" using \"%s\""
;; text
;; words-voice))
;; (start-process "my-thing" "foo" "say" "-v" words-voice text "-r" speed)
;; (call-process "say" nil nil nil (mapconcat 'identity (list "-v" words-voice text "-r" speed) " "))
(shell-command (format "say -v %s \"%s\" -r %s" words-voice text speed)))
(defvar words-languages
'()
"List of cons cells (language . code).")
(defvar words-speakers
'(("German" . "Anna")
("Chinese" . "Ting-Ting"))
"Speakers for different languages.")
(setq words-languages '(("German" . "de")
("Italian" . "it")
("Chinese" . "zh")
("Spanish" . "es")))
(defun words-translate (to-language)
"Translate word at point or selection TO-LANGUAGE.
http://mymemory.translated.net TO-LANGUAGE is the code, see
http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry.
Assumes selected code is in English."
(interactive
(list
(ido-completing-read
"Language: "
(mapcar 'car words-languages) nil t)))
(let* ((text (if (use-region-p)
(buffer-substring
(region-beginning)
(region-end))
(thing-at-point 'word)))
(url (format "http://mymemory.translated.net/api/get?q=%s!&langpair=en|%s"
text
(cdr (assoc to-language words-languages))))
(json (with-current-buffer
(url-retrieve-synchronously url)
(json-read-from-string
(buffer-substring url-http-end-of-headers (point-max))))))
(let ((words-voice (or (cdr (assoc to-language words-speakers))
"Vicki")))
(words-speak
(cdr (assoc 'translatedText (cdr (assoc 'responseData json))))))
(message "Translation: %s"
(cdr (assoc 'translatedText (cdr (assoc 'responseData json)))))))
(defun words-mdfind ()
"Search for file names matching word or selection at point using mdfind.
Opens an org-buffer with links to results. Mac only."
(interactive)
(let ((query (if (use-region-p)
(buffer-substring
(region-beginning)
(region-end))
(thing-at-point 'word))))
(switch-to-buffer-other-window "*mdfind*")
(erase-buffer)
(insert
(mapconcat
(lambda (x)
(format "[[%s]]" x))
(split-string
(shell-command-to-string
(format "mdfind -name %s"
(shell-quote-argument query)))
"\n")
"\n"))
(org-mode)))
(defun words-swiper-all ()
"Run swiper-all on the word at point or region."
(interactive)
(let ((query (if (use-region-p)
(buffer-substring
(region-beginning)
(region-end))
(thing-at-point 'word))))
(let ((ivy-initial-inputs-alist `((swiper-multi . ,query))))
(swiper-all))))
(defun words-finder ()
"Open Mac Finder with search of word at point or selection."
(interactive)
(let* ((query (if (use-region-p)
(buffer-substring
(region-beginning)
(region-end))
(thing-at-point 'word)))
(applescript (concat
"tell application \"Finder\" to activate
tell application \"System Events\"
tell process \"Finder\"
click menu item \"Find\" of menu \"File\" of menu bar 1
keystroke " (format "\"%s\"" query )
"
end tell
end tell")))
(message "%s" applescript)
;; from org-mac-link
(do-applescript applescript)))
;; * A hydra interface to words
;; hydra (http://oremacs.com/2015/01/20/introducing-hydra/) is a relatively new
;; menu type interface to select actions with single key strokes. It is a nicer
;; implementation than what I setup in the words function above.
;; https://github.com/abo-abo/hydra
(defhydra words-hydra (:color blue :hint nil)
"
words
_d_: Dictionary _g_: Google _T_: Twitter _b_: Bibtex _k_: Speak
_t_: Thesaurus _G_: Google Scholar ^ ^ _f_: Mac finder _r_: Translate
_s_: Spell _c_: Crossref ^ ^ _w_: swiper-all
^ ^ _S_: Scopus ^ ^ _M_: Mac mdfind
^ ^ _W_: Web Of Science
^ ^ _p_: Pubmed
^ ^ _a_: Arxiv
^ ^ _o_: Semantic Scholar
_q_: quit
"
("d" words-dictionary "dictionary")
("t" words-thesaurus "thesaurus")
("s" words-atd "spell/grammar")
("g" words-google "google")
("T" words-twitter "Twitter")
("W" words-wos "Web of Science")
("G" words-google-scholar "Google scholar")
("c" words-crossref "CrossRef")
("S" words-scopus "Scopus")
("o" words-semantic-scholar "Semantic Scholar")
("p" words-pubmed "Pubmed")
("a" words-arxiv "Arxiv")
("b" words-bibtex "bibtex")
("f" words-finder "Mac Finder")
("w" words-swiper-all "swiper-all")
("M" words-mdfind "mdfind")
("k" words-speak "Speak")
("r" words-translate "Translate")
("q" nil "cancel"))
;;; End:
(provide 'words)
;;; words.el ends here