PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/elpa/helm-20170209.513/helm-net.el

https://gitlab.com/csagedy/prelude
Emacs Lisp | 521 lines | 407 code | 64 blank | 50 comment | 17 complexity | 4cfe9da53c96037f240ed3bd28845df1 MD5 | raw file
  1. ;;; helm-net.el --- helm browse url and search web. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2012 ~ 2017 Thierry Volpiatto <thierry.volpiatto@gmail.com>
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'cl-lib)
  15. (require 'helm)
  16. (require 'helm-help)
  17. (require 'url)
  18. (require 'xml)
  19. (require 'browse-url)
  20. (defgroup helm-net nil
  21. "Net related applications and libraries for Helm."
  22. :group 'helm)
  23. (defcustom helm-google-suggest-default-browser-function nil
  24. "The browse url function you prefer to use with google suggest.
  25. When nil, use the first browser function available
  26. See `helm-browse-url-default-browser-alist'."
  27. :group 'helm-net
  28. :type 'symbol)
  29. (defcustom helm-home-url "http://www.google.fr"
  30. "Default url to use as home url."
  31. :group 'helm-net
  32. :type 'string)
  33. (defcustom helm-surfraw-default-browser-function nil
  34. "The browse url function you prefer to use with surfraw.
  35. When nil, fallback to `browse-url-browser-function'."
  36. :group 'helm-net
  37. :type 'symbol)
  38. (defcustom helm-google-suggest-url
  39. "http://google.com/complete/search?output=toolbar&q="
  40. "URL used for looking up Google suggestions."
  41. :type 'string
  42. :group 'helm-net)
  43. (defcustom helm-google-suggest-search-url
  44. "http://www.google.com/search?ie=utf-8&oe=utf-8&q=%s"
  45. "URL used for Google searching."
  46. :type 'string
  47. :group 'helm-net)
  48. (defcustom helm-net-prefer-curl nil
  49. "When non--nil use CURL external program to fetch data.
  50. Otherwise `url-retrieve-synchronously' is used."
  51. :type 'boolean
  52. :group 'helm-net)
  53. (defvaralias 'helm-google-suggest-use-curl-p 'helm-net-prefer-curl)
  54. (make-obsolete-variable 'helm-google-suggest-use-curl-p 'helm-net-prefer-curl "1.7.7")
  55. (defcustom helm-surfraw-duckduckgo-url
  56. "https://duckduckgo.com/lite/?q=%s&kp=1"
  57. "The duckduckgo url.
  58. This is a format string, don't forget the `%s'.
  59. If you have personal settings saved on duckduckgo you should have
  60. a personal url, see your settings on duckduckgo."
  61. :type 'string
  62. :group 'helm-net)
  63. (defcustom helm-wikipedia-suggest-url
  64. "https://en.wikipedia.org/w/api.php?action=opensearch&search="
  65. "Url used for looking up Wikipedia suggestions."
  66. :type 'string
  67. :group 'helm-net)
  68. (defcustom helm-search-suggest-action-wikipedia-url
  69. "https://en.wikipedia.org/wiki/Special:Search?search=%s"
  70. "The Wikipedia search url.
  71. This is a format string, don't forget the `%s'."
  72. :type 'string
  73. :group 'helm-net)
  74. (defcustom helm-wikipedia-summary-url
  75. "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page="
  76. "URL for getting the summary of a Wikipedia topic."
  77. :type 'string
  78. :group 'helm-net)
  79. (defcustom helm-search-suggest-action-youtube-url
  80. "http://www.youtube.com/results?aq=f&search_query=%s"
  81. "The Youtube search url.
  82. This is a format string, don't forget the `%s'."
  83. :type 'string
  84. :group 'helm-net)
  85. (defcustom helm-search-suggest-action-imdb-url
  86. "http://www.imdb.com/find?s=all&q=%s"
  87. "The IMDb search url.
  88. This is a format string, don't forget the `%s'."
  89. :type 'string
  90. :group 'helm-net)
  91. (defcustom helm-search-suggest-action-google-maps-url
  92. "http://maps.google.com/maps?f=q&source=s_q&q=%s"
  93. "The Google Maps search url.
  94. This is a format string, don't forget the `%s'."
  95. :type 'string
  96. :group 'helm-net)
  97. (defcustom helm-search-suggest-action-google-news-url
  98. "http://www.google.com/search?safe=off&prmd=nvlifd&source=lnms&tbs=nws:1&q=%s"
  99. "The Google News search url.
  100. This is a format string, don't forget the `%s'."
  101. :type 'string
  102. :group 'helm-net)
  103. (defcustom helm-google-suggest-actions
  104. '(("Google Search" . helm-google-suggest-action)
  105. ("Wikipedia" . (lambda (candidate)
  106. (helm-search-suggest-perform-additional-action
  107. helm-search-suggest-action-wikipedia-url
  108. candidate)))
  109. ("Youtube" . (lambda (candidate)
  110. (helm-search-suggest-perform-additional-action
  111. helm-search-suggest-action-youtube-url
  112. candidate)))
  113. ("IMDb" . (lambda (candidate)
  114. (helm-search-suggest-perform-additional-action
  115. helm-search-suggest-action-imdb-url
  116. candidate)))
  117. ("Google Maps" . (lambda (candidate)
  118. (helm-search-suggest-perform-additional-action
  119. helm-search-suggest-action-google-maps-url
  120. candidate)))
  121. ("Google News" . (lambda (candidate)
  122. (helm-search-suggest-perform-additional-action
  123. helm-search-suggest-action-google-news-url
  124. candidate))))
  125. "List of actions for google suggest sources."
  126. :group 'helm-net
  127. :type '(alist :key-type string :value-type function))
  128. (defcustom helm-browse-url-firefox-new-window "-new-tab"
  129. "Allow choosing to browse url in new window or new tab.
  130. Can be \"-new-tab\" (default) or \"-new-window\"."
  131. :group 'helm-net
  132. :type '(radio
  133. (const :tag "New tab" "-new-tab")
  134. (const :tag "New window" "-new-window")))
  135. ;;; Additional actions for search suggestions
  136. ;;
  137. ;;
  138. ;; Internal
  139. (defun helm-search-suggest-perform-additional-action (url query)
  140. "Perform the search via URL using QUERY as input."
  141. (browse-url (format url (url-hexify-string query))))
  142. (defun helm-net--url-retrieve-sync (request parser)
  143. (if helm-net-prefer-curl
  144. (with-temp-buffer
  145. (call-process "curl" nil t nil request)
  146. (funcall parser))
  147. (with-current-buffer (url-retrieve-synchronously request)
  148. (funcall parser))))
  149. ;;; Google Suggestions
  150. ;;
  151. ;;
  152. (defun helm-google-suggest-parser ()
  153. (cl-loop
  154. with result-alist = (xml-get-children
  155. (car (xml-parse-region
  156. (point-min) (point-max)))
  157. 'CompleteSuggestion)
  158. for i in result-alist collect
  159. (cdr (cl-caadr (assoc 'suggestion i)))))
  160. (defun helm-google-suggest-fetch (input)
  161. "Fetch suggestions for INPUT from XML buffer."
  162. (let ((request (concat helm-google-suggest-url
  163. (url-hexify-string input))))
  164. (helm-net--url-retrieve-sync
  165. request #'helm-google-suggest-parser)))
  166. (defun helm-google-suggest-set-candidates (&optional request-prefix)
  167. "Set candidates with result and number of google results found."
  168. (let ((suggestions (helm-google-suggest-fetch
  169. (or (and request-prefix
  170. (concat request-prefix
  171. " " helm-pattern))
  172. helm-pattern))))
  173. (if (member helm-pattern suggestions)
  174. suggestions
  175. ;; if there is no suggestion exactly matching the input then
  176. ;; prepend a Search on Google item to the list
  177. (append
  178. suggestions
  179. (list (cons (format "Search for '%s' on Google" helm-input)
  180. helm-input))))))
  181. (defun helm-ggs-set-number-result (num)
  182. (if num
  183. (progn
  184. (and (numberp num) (setq num (number-to-string num)))
  185. (cl-loop for i in (reverse (split-string num "" t))
  186. for count from 1
  187. append (list i) into C
  188. when (= count 3)
  189. append (list ",") into C
  190. and do (setq count 0)
  191. finally return
  192. (replace-regexp-in-string
  193. "^," "" (mapconcat 'identity (reverse C) ""))))
  194. "?"))
  195. (defun helm-google-suggest-action (candidate)
  196. "Default action to jump to a google suggested candidate."
  197. (let ((arg (format helm-google-suggest-search-url
  198. (url-hexify-string candidate))))
  199. (helm-aif helm-google-suggest-default-browser-function
  200. (funcall it arg)
  201. (helm-browse-url arg))))
  202. (defvar helm-google-suggest-default-function
  203. 'helm-google-suggest-set-candidates
  204. "Default function to use in helm google suggest.")
  205. (defvar helm-source-google-suggest
  206. (helm-build-sync-source "Google Suggest"
  207. :candidates (lambda ()
  208. (funcall helm-google-suggest-default-function))
  209. :action 'helm-google-suggest-actions
  210. :volatile t
  211. :keymap helm-map
  212. :requires-pattern 3))
  213. (defun helm-google-suggest-emacs-lisp ()
  214. "Try to emacs lisp complete with google suggestions."
  215. (helm-google-suggest-set-candidates "emacs lisp"))
  216. ;;; Wikipedia suggestions
  217. ;;
  218. ;;
  219. (declare-function json-read-from-string "json" (string))
  220. (defun helm-wikipedia-suggest-fetch ()
  221. "Fetch Wikipedia suggestions and return them as a list."
  222. (require 'json)
  223. (let ((request (concat helm-wikipedia-suggest-url
  224. (url-hexify-string helm-pattern))))
  225. (helm-net--url-retrieve-sync
  226. request #'helm-wikipedia--parse-buffer)))
  227. (defun helm-wikipedia--parse-buffer ()
  228. (goto-char (point-min))
  229. (when (re-search-forward "^\\[.+\\[\\(.*\\)\\]\\]" nil t)
  230. (cl-loop for i across (aref (json-read-from-string (match-string 0)) 1)
  231. collect i into result
  232. finally return (or result
  233. (append
  234. result
  235. (list (cons (format "Search for '%s' on wikipedia"
  236. helm-pattern)
  237. helm-pattern)))))))
  238. (defvar helm-wikipedia--summary-cache (make-hash-table :test 'equal))
  239. (defun helm-wikipedia-persistent-action (candidate)
  240. (unless (string= (format "Search for '%s' on wikipedia"
  241. helm-pattern)
  242. (helm-get-selection nil t))
  243. (message "Fetching summary from Wikipedia...")
  244. (let ((buf (get-buffer-create "*helm wikipedia summary*"))
  245. result mess)
  246. (while (progn
  247. (setq result (or (gethash candidate helm-wikipedia--summary-cache)
  248. (puthash candidate
  249. (prog1
  250. (helm-wikipedia-fetch-summary candidate)
  251. (setq mess "Done"))
  252. helm-wikipedia--summary-cache)))
  253. (when (and result
  254. (listp result))
  255. (setq candidate (cdr result))
  256. (message "Redirected to %s" candidate)
  257. t)))
  258. (if (not result)
  259. (message "Error when getting summary.")
  260. (with-current-buffer buf
  261. (erase-buffer)
  262. (setq cursor-type nil)
  263. (insert result)
  264. (fill-region (point-min) (point-max))
  265. (goto-char (point-min)))
  266. (display-buffer buf)
  267. (message mess)))))
  268. (defun helm-wikipedia-fetch-summary (input)
  269. (let* ((request (concat helm-wikipedia-summary-url
  270. (url-hexify-string input))))
  271. (helm-net--url-retrieve-sync
  272. request #'helm-wikipedia--parse-summary)))
  273. (defun helm-wikipedia--parse-summary ()
  274. (goto-char (point-min))
  275. (when (search-forward "{" nil t)
  276. (let ((result (cdr (assoc '*
  277. (assoc 'text
  278. (assoc 'parse
  279. (json-read-from-string
  280. (buffer-substring-no-properties
  281. (1- (point)) (point-max)))))))))
  282. (when result
  283. (if (string-match "<span class=\"redirectText\"><a href=[^>]+>\\([^<]+\\)" result)
  284. (cons 'redirect (match-string 1 result))
  285. ;; find the beginning of the summary text in the result
  286. ;; check if there is a table before the summary and skip that
  287. (when (or (string-match "</table>\\(\n<div.*?</div>\\)?\n<p>" result)
  288. ;; otherwise just find the first paragraph
  289. (string-match "<p>" result))
  290. ;; remove cruft and do a simple formatting
  291. (replace-regexp-in-string
  292. "Cite error: .*" ""
  293. (replace-regexp-in-string
  294. "&#160;" ""
  295. (replace-regexp-in-string
  296. "\\[[^\]]+\\]" ""
  297. (replace-regexp-in-string
  298. "<[^>]*>" ""
  299. (replace-regexp-in-string
  300. "</p>\n<p>" "\n\n"
  301. (substring result (match-end 0)))))))))))))
  302. (defvar helm-source-wikipedia-suggest
  303. (helm-build-sync-source "Wikipedia Suggest"
  304. :candidates #'helm-wikipedia-suggest-fetch
  305. :action '(("Wikipedia" . (lambda (candidate)
  306. (helm-search-suggest-perform-additional-action
  307. helm-search-suggest-action-wikipedia-url
  308. candidate))))
  309. :persistent-action #'helm-wikipedia-persistent-action
  310. :persistent-help "show summary"
  311. :volatile t
  312. :keymap helm-map
  313. :requires-pattern 3))
  314. ;;; Web browser functions.
  315. ;;
  316. ;;
  317. ;; If default setting of `w3m-command' is not
  318. ;; what you want and you modify it, you will have to reeval
  319. ;; also `helm-browse-url-default-browser-alist'.
  320. (defvar helm-browse-url-chromium-program "chromium-browser")
  321. (defvar helm-browse-url-uzbl-program "uzbl-browser")
  322. (defvar helm-browse-url-conkeror-program "conkeror")
  323. (defvar helm-browse-url-default-browser-alist
  324. `((,(or (and (boundp 'w3m-command) w3m-command)
  325. "/usr/bin/w3m") . w3m-browse-url)
  326. (,browse-url-firefox-program . browse-url-firefox)
  327. (,helm-browse-url-chromium-program . helm-browse-url-chromium)
  328. (,helm-browse-url-conkeror-program . helm-browse-url-conkeror)
  329. (,helm-browse-url-uzbl-program . helm-browse-url-uzbl)
  330. (,browse-url-kde-program . browse-url-kde)
  331. (,browse-url-gnome-moz-program . browse-url-gnome-moz)
  332. (,browse-url-mozilla-program . browse-url-mozilla)
  333. (,browse-url-galeon-program . browse-url-galeon)
  334. (,browse-url-netscape-program . browse-url-netscape)
  335. (,browse-url-mosaic-program . browse-url-mosaic)
  336. (,browse-url-xterm-program . browse-url-text-xterm)
  337. ("emacs" . eww-browse-url))
  338. "*Alist of \(executable . function\) to try to find a suitable url browser.")
  339. (cl-defun helm-generic-browser (url cmd-name &rest args)
  340. "Browse URL with NAME browser."
  341. (let ((proc (concat cmd-name " " url)))
  342. (message "Starting %s..." cmd-name)
  343. (apply 'start-process proc nil cmd-name
  344. (append args (list url)))
  345. (set-process-sentinel
  346. (get-process proc)
  347. (lambda (process event)
  348. (when (string= event "finished\n")
  349. (message "%s process %s" process event))))))
  350. (defun helm-browse-url-firefox (url &optional _ignore)
  351. "Same as `browse-url-firefox' but detach from emacs.
  352. So when you quit emacs you can keep your firefox open
  353. and not be prompted to kill firefox process.
  354. NOTE: Probably not supported on some systems (e.g Windows)."
  355. (interactive (list (read-string "URL: " (browse-url-url-at-point))
  356. nil))
  357. (setq url (browse-url-encode-url url))
  358. (let ((process-environment (browse-url-process-environment)))
  359. (call-process-shell-command
  360. (format "(%s %s %s &)"
  361. browse-url-firefox-program
  362. helm-browse-url-firefox-new-window
  363. (shell-quote-argument url)))))
  364. (defun helm-browse-url-chromium (url &optional _ignore)
  365. "Browse URL with google chrome browser."
  366. (interactive "sURL: ")
  367. (helm-generic-browser
  368. url helm-browse-url-chromium-program))
  369. (defun helm-browse-url-uzbl (url &optional _ignore)
  370. "Browse URL with uzbl browser."
  371. (interactive "sURL: ")
  372. (helm-generic-browser url helm-browse-url-uzbl-program "-u"))
  373. (defun helm-browse-url-conkeror (url &optional _ignore)
  374. "Browse URL with conkeror browser."
  375. (interactive "sURL: ")
  376. (helm-generic-browser url helm-browse-url-conkeror-program))
  377. (defun helm-browse-url-default-browser (url &rest args)
  378. "Find the first available browser and ask it to load URL."
  379. (let ((default-browser-fn
  380. (cl-loop for (exe . fn) in helm-browse-url-default-browser-alist
  381. thereis (and exe (executable-find exe) (fboundp fn) fn))))
  382. (if default-browser-fn
  383. (apply default-browser-fn url args)
  384. (error "No usable browser found"))))
  385. (defun helm-browse-url (url &rest args)
  386. "Default command to browse URL."
  387. (if browse-url-browser-function
  388. (browse-url url args)
  389. (helm-browse-url-default-browser url args)))
  390. ;;; Surfraw
  391. ;;
  392. ;; Need external program surfraw.
  393. ;; <http://surfraw.alioth.debian.org/>
  394. ;; Internal
  395. (defvar helm-surfraw-engines-history nil)
  396. (defvar helm-surfraw-input-history nil)
  397. (defvar helm-surfraw--elvi-cache nil)
  398. (defun helm-build-elvi-list ()
  399. "Return list of all engines and descriptions handled by surfraw."
  400. (or helm-surfraw--elvi-cache
  401. (setq helm-surfraw--elvi-cache
  402. (cdr (with-temp-buffer
  403. (call-process "surfraw" nil t nil "-elvi")
  404. (split-string (buffer-string) "\n"))))))
  405. ;;;###autoload
  406. (defun helm-surfraw (pattern engine)
  407. "Preconfigured `helm' to search PATTERN with search ENGINE."
  408. (interactive (list (read-string "SearchFor: "
  409. nil 'helm-surfraw-input-history
  410. (thing-at-point 'symbol))
  411. (helm-comp-read
  412. "Engine: "
  413. (helm-build-elvi-list)
  414. :must-match t
  415. :name "Surfraw Search Engines"
  416. :del-input nil
  417. :history helm-surfraw-engines-history)))
  418. (let* ((engine-nodesc (car (split-string engine)))
  419. (url (if (string= engine-nodesc "duckduckgo")
  420. ;; "sr duckduckgo -p foo" is broken, workaround.
  421. (format helm-surfraw-duckduckgo-url
  422. (url-hexify-string pattern))
  423. (with-temp-buffer
  424. (apply 'call-process "surfraw" nil t nil
  425. (append (list engine-nodesc "-p") (split-string pattern)))
  426. (replace-regexp-in-string
  427. "\n" "" (buffer-string)))))
  428. (browse-url-browser-function (or helm-surfraw-default-browser-function
  429. browse-url-browser-function)))
  430. (if (string= engine-nodesc "W")
  431. (helm-browse-url helm-home-url)
  432. (helm-browse-url url)
  433. (setq helm-surfraw-engines-history
  434. (cons engine (delete engine helm-surfraw-engines-history))))))
  435. ;;;###autoload
  436. (defun helm-google-suggest ()
  437. "Preconfigured `helm' for google search with google suggest."
  438. (interactive)
  439. (helm-other-buffer 'helm-source-google-suggest "*helm google*"))
  440. ;;;###autoload
  441. (defun helm-wikipedia-suggest ()
  442. "Preconfigured `helm' for Wikipedia lookup with Wikipedia suggest."
  443. (interactive)
  444. (helm :sources 'helm-source-wikipedia-suggest
  445. :buffer "*helm wikipedia*"))
  446. (provide 'helm-net)
  447. ;; Local Variables:
  448. ;; byte-compile-warnings: (not obsolete)
  449. ;; coding: utf-8
  450. ;; indent-tabs-mode: nil
  451. ;; End:
  452. ;;; helm-net.el ends here