-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathwords.el
512 lines (388 loc) · 14.3 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
;;; words.el --- Functions to operate on word at point or region -*- lexical-binding: t -*-
;; Copyright(C) 2014-2021 John Kitchin
;; Author: John Kitchin <[email protected]>
;; Version: 0.2.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-gramma' uses gramma to do a grammar check.
;; These functions search various online search spaces
;; - `words-google'
;; - `words-twitter'
;; - `words-google-scholar'
;; - `words-scopus'
;; - `words-wos' :: Search Web of Science
;; - `words-crossref'
;; - `words-pubmed'
;; - `words-arxiv'
;; - `words-semantic-scholar'
;; - `words-bibtex' :: search for words in your bibtex files
;; - `words-mdfind' :: search local computer with mdfind (Mac)
;; - `words-finder' :: search local computer with finder (Mac)
;; - `words-swiper-all' :: search all open buffers
;; These functions just open websites for convenience.
;; - `wos' :: open Web of Science
;; - `pubmed' :: open pubmed
;; - `scopus' :: open Scopus
;; Speaking and translating: - `words-speak' will use a speech program to read the
;; selection out loud. See `words-speech-program' and
;; `words-speech-program-options' for configuration.
;; - `words/body' will open a "hydra" menu.
;;; Code:
(require 'hydra)
(require 'url)
(require 'xml)
(setq hydra-is-helpful t)
;; to quiet byte-compile error
(defvar url-http-end-of-headers)
(defcustom words-speech-program "say"
"Program to speak words out loud.
Mac options include \"say\", but you can use homebrew to install
espeak too."
:group 'words)
(defcustom words-speech-program-options "-v Samantha -r 180"
"Options specific to `words-speech-program'.
It is assumed the text to be spoken will be the last argument."
:group 'words)
(defcustom words-translate-shell-program "trans"
"Name of the translate-shell executable."
:group 'words)
(defcustom words-translate-shell-native-language "en"
"Two letter code for the default native language to translate
from. Defaults to English (en).
See http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry."
:group 'words)
(defcustom words-translate-shell-options "-p"
"Options for the translate shell executable.
-p makes it speak the translation out loud.
Do not include the translate language here, it is added in
`words-translate-shell'."
:group 'words)
(defcustom words-translate-preferred-language nil
"A two letter language code for the preferred language to translate to.
See http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry."
:group 'words)
(defcustom words-translate-speak nil
"If non-nil use `words-speak' to read the translation out loud in `words-translate-shell'."
:group 'words)
(defun words-at-point ()
"The active region, or word at point."
(if (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end))
(thing-at-point 'word)))
;; * Dictionary/thesaurus/grammar
(defun words-dictionary (text)
"Look up TEXT in an online dictionary."
(interactive (list (words-at-point)))
(browse-url
(format
"http://dictionary.reference.com/browse/%s?s=t"
(url-hexify-string text))))
(defun words-thesaurus (text)
"Look up TEXT in an online thesaurus."
(interactive (list (words-at-point)))
(browse-url
(format
"http://www.thesaurus.com/browse/%s"
(url-hexify-string text))))
(defun words-gramma (text)
"Check TEXT grammar with gramma.
This pops to *gramma* with the output."
(interactive (list (words-at-point)))
(unless (executable-find "gramma")
(error "gramma was not found. Go to https://caderek.github.io/gramma/"))
(pop-to-buffer "*gramma*")
(erase-buffer)
(insert (shell-command-to-string (format "gramma listen -p %S" text))))
;; * Web functions
(defun words-google (text)
"Google TEXT."
(interactive (list (words-at-point)))
(browse-url
(format
"http://www.google.com/search?q=%s"
(url-hexify-string text))))
(defun words-twitter (text)
"Search twitter for TEXT."
(interactive (list (words-at-point)))
(browse-url
(format
"https://twitter.com/search?q=%s"
(url-hexify-string text))))
;; * Scientific search functions
(defun words-google-scholar (text)
"Search TEXT in Google scholar."
(interactive (list (words-at-point)))
(browse-url
(format
"http://scholar.google.com/scholar?q=%s"
(url-hexify-string text))))
(defun words-wos (text)
"Search TEXT in Web of Science."
;; the url was derived from this page: http://wokinfo.com/webtools/searchbox/
(interactive (list (words-at-point)))
(browse-url
(format "http://gateway.webofknowledge.com/gateway/Gateway.cgi?topic=%s&GWVersion=2&SrcApp=WEB&SrcAuth=HSB&DestApp=UA&DestLinkType=GeneralSearchSummary"
(mapconcat 'identity (split-string text) "+"))))
(defun words-scopus (text)
"Search TEXT in Scopus."
(interactive (list (words-at-point)))
(browse-url
(format
"http://www.scopus.com//search/submit/basic.url?field1=TITLE-ABS-KEY&searchterm1=%s"
(mapconcat 'identity (split-string text) "+"))))
(defun words-crossref (text)
"Search TEXT in CrossRef."
(interactive (list (words-at-point)))
(browse-url
(format
"http://search.crossref.org/?q=%s&from_ui=yes"
(replace-regexp-in-string "%20" "+"
(url-hexify-string text)))))
(defun words-pubmed (text)
"Search TEXT in pubmed."
(interactive (list (words-at-point)))
(browse-url
(format
"http://www.ncbi.nlm.nih.gov/pubmed/?term=%s"
(url-hexify-string text))))
(defun words-arxiv (text )
"Search TEXT in arxiv.org."
(interactive (list (words-at-point)))
(browse-url
(format
"https://arxiv.org/search/?query=%s&searchtype=all&abstracts=show&order=-announced_date_first&size=50"
(url-hexify-string text))))
(defun words-semantic-scholar (text)
"Search TEXT in www.semanticscholar.org."
(interactive (list (words-at-point)))
(browse-url
(format
"https://www.semanticscholar.org/search?q=%s"
(url-hexify-string text))))
;; ** 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 (text)
"Find TEXT in files listed in `bibtex-completion-bibliography'."
(interactive (list (words-at-point)))
(multi-occur
(mapcar (lambda (f)
(find-file-noselect f))
(if (listp bibtex-completion-bibliography)
bibtex-completion-bibliography
(list bibtex-completion-bibliography)))
text))
;; * Speech
(defun words-speak (&optional text)
"Speak TEXT.
Uses `words-speech-program' and `words-speech-program-options'."
(interactive (list (words-at-point)))
(shell-command
(concat words-speech-program " "
words-speech-program-options " "
(format "\"%s\"" text))))
;; * Translation functions
(defun words-get-language-data ()
"Retrieve a list of languages.
This is a list of lists ((tag value)...) for each language."
(with-current-buffer (url-retrieve-synchronously
"http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry")
(goto-char (point-min))
(search-forward "%%")
(forward-line)
(let ((languages '())
(language))
(while (not (eobp))
(catch 'block
(when (looking-at "%%")
(push language languages)
(setq language '())
(forward-line)
(throw 'block t))
(let ((fields (split-string (buffer-substring
(line-beginning-position)
(line-end-position))
":")))
(when (= 2 (length fields))
(push (cons (cl-first fields)
(string-trim (cl-second fields)))
language)))
(forward-line)))
languages)))
(defun words-language-candidates ()
"Returns an a-list of (language . two-letter code) for languages.
Some languages have multiple mappings of language name to code.
This captures them all."
(let ((candidates '()))
(cl-loop for language in (words-get-language-data)
do
;; collect all the descriptions
(cl-loop for (tag . value) in language
if (string= tag "Description")
do
(push (cons value (cdr (assoc "Subtag" language))) candidates)))
candidates))
(defun words-choose-translate-language ()
"Use completion to choose a translation language."
(let ((candidates (words-language-candidates)))
(cdr (assoc (completing-read "Translate to: " candidates) candidates))))
(defun words-translate (&optional text arg)
"Translate TEXT to `words-translate-preferred-language'.
If words-translate-preferred-language is nil or there is a prefix
ARG, use completion to choose the language.
Translation is done with http://mymemory.translated.net.
Not every language is supported.
The translation will appear as a message.
If `words-translate-speak' is non-nil, or a double prefix arg is
used the translation will be read aloud by `words-speak'.
Note: there are usage limits of 1000
words/day (https://mymemory.translated.net/doc/usagelimits.php)."
(interactive (list (words-at-point) current-prefix-arg))
(let* ((to-language (if (or arg (null words-translate-preferred-language))
(words-choose-translate-language)
words-translate-preferred-language))
(url (format "http://mymemory.translated.net/api/get?q=%s!&langpair=en|%s"
text
to-language))
(json (with-current-buffer
(url-retrieve-synchronously url)
(json-read-from-string
(buffer-substring url-http-end-of-headers (point-max))))))
(when (or words-translate-speak
(equal '(16) arg))
(words-speak
(cdr (assoc 'translatedText (cdr (assoc 'responseData json))))))
(pop-to-buffer "*words-translate*")
(erase-buffer)
(insert (cdr (assoc 'translatedText (cdr (assoc 'responseData json)))))))
;; ** using translate-shell
(defun words-translate-shell (&optional text arg)
"Use translate-shell to translate the TEXT.
TEXT is translated to `words-translate-preferred-language',
unless you use a prefix ARG, then choose the language instead.
Assumes the TEXT is in `words-translate-shell-native-language'.
Use a double prefix arg to select the TEXT language.
https://github.com/soimort/translate-shell
On Mac: brew reinstall translate-shell"
(interactive (list
(words-at-point)
current-prefix-arg))
(let (translate-from translate-to candidates)
(cond
;; Here you choose the to language if you didn't set one or use a single prefix.
((or (null words-translate-preferred-language) (equal '(4) arg))
(setq translate-from words-translate-shell-native-language
translate-to (words-choose-translate-language)))
;; Here you choose the from and to target
((equal '(16) arg)
(setq candidates (words-language-candidates)
translate-from (cdr (assoc (completing-read "Translate from: " candidates) candidates))
translate-to (cdr (assoc (completing-read "Translate to: " candidates) candidates))))
(t
(setq translate-from words-translate-shell-native-language
translate-to words-translate-preferred-language)))
(shell-command (string-join
(list
words-translate-shell-program
words-translate-shell-options
(format "%s:%s" translate-from translate-to)
(shell-quote-argument text))
" "))))
;; * Search functions for Mac
(defun words-swiper-all (text)
"Run `swiper-all' on TEXT."
(interactive (list (words-at-point)))
(let ((ivy-initial-inputs-alist `((swiper-all . ,text))))
(swiper-all)))
;; (defun words-mdfind (query)
;; "Search mdfind with QUERY.
;; Opens an org-buffer with links to results. Mac only."
;; (interactive (list (words-at-point)))
;; (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-mdfind-function (str)
(or
(ivy-more-chars)
(progn
(counsel--async-command
(format "mdfind -name \"%s\"" str))
'("" "working..."))))
(defun words-mdfind (query)
"Search mdfind with QUERY.
Opens an org-buffer with links to results. Mac only."
(interactive (list (words-at-point)))
(ivy-read "mdfind: " #'words-mdfind-function
:initial-input query
:dynamic-collection t
:action #'find-file))
(defun words-finder (query)
"Open Mac Finder with QUERY."
(interactive (list (words-at-point)))
;; from org-mac-link
(do-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")))
;; * A hydra interface to words
(defhydra words-hydra (:color blue :hint nil)
"
words"
("d" words-dictionary "dictionary" :column "Lookup")
("t" words-thesaurus "thesaurus" :column "Lookup")
("g" words-gramma "grammar" :column "Lookup")
("wg" words-google "google" :column "WWW")
("wt" words-twitter "Twitter" :column "WWW")
("ww" words-wos "Web of Science" :column "WWW")
("ws" words-google-scholar "Google scholar" :column "WWW")
("wc" words-crossref "CrossRef" :column "WWW")
("wu" words-scopus "Scopus" :column "WWW")
("wh" words-semantic-scholar "Semantic Scholar" :column "WWW")
("wp" words-pubmed "Pubmed" :column "WWW")
("wa" words-arxiv "Arxiv" :column "WWW")
("sb" words-bibtex "bibtex" :column "search")
("sf" words-finder "Mac Finder" :column "search")
("sa" words-swiper-all "swiper-all" :column "search")
("sm" words-mdfind "mdfind" :column "search")
("k" words-speak "Speak" :column "audio")
("l" words-translate-shell "Translate-shell" :column "translate")
("L" words-translate "Translate" :column "translate")
("q" nil "quit"))
;;; End:
(provide 'words)
;;; words.el ends here