PageRenderTime 114ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/init-emacs-w3m.el

https://github.com/redguardtoo/emacs.d
Emacs Lisp | 201 lines | 143 code | 19 blank | 39 comment | 0 complexity | 99f5903ea53838d79cd05e0b8a5338cc MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0
  1. ;; -*- coding: utf-8; lexical-binding: t; -*-
  2. (setq w3m-coding-system 'utf-8
  3. w3m-file-coding-system 'utf-8
  4. w3m-file-name-coding-system 'utf-8
  5. w3m-input-coding-system 'utf-8
  6. w3m-output-coding-system 'utf-8
  7. ;; emacs-w3m will test the ImageMagick support for png32
  8. ;; and create files named "png32:-" everywhere
  9. w3m-imagick-convert-program nil
  10. w3m-terminal-coding-system 'utf-8
  11. w3m-use-cookies t
  12. w3m-cookie-accept-bad-cookies t
  13. w3m-home-page "https://www.duckduckgo.com"
  14. w3m-command-arguments '("-F" "-cookie")
  15. w3m-mailto-url-function 'compose-mail
  16. browse-url-browser-function 'w3m
  17. ;; use shr to view html mail which is dependent on libxml
  18. ;; I prefer w3m. That's emacs 24.3+ default setup.
  19. ;; If you prefer colored mail body and other advanced features,
  20. ;; you can either comment out below line and let Emacs decide the
  21. ;; best html mail rendering engine, or "(setq mm-text-html-renderer 'shr)"
  22. ;; in "~/.gnus.el"
  23. ;; mm-text-html-renderer 'w3m ; I prefer w3m
  24. w3m-use-toolbar t
  25. ;; show images in the browser
  26. ;; setq w3m-default-display-inline-images t
  27. ;; w3m-use-tab nil
  28. w3m-confirm-leaving-secure-page nil
  29. w3m-search-default-engine "g"
  30. w3m-key-binding 'info)
  31. ;; C-u S g RET <search term> RET in w3m
  32. (with-eval-after-load 'w3m-search
  33. (defvar w3m-search-engine-alist) ; remove compiling warning
  34. (setq w3m-search-engine-alist
  35. '(("g" "https://www.duckduckgo.com/search?q=%s" utf-8)
  36. ;; stackoverflow search
  37. ("q" "https://www.duckduckgo.com?q=%s+site:stackoverflow.com" utf-8)
  38. ;; wikipedia
  39. ("w" "https://en.wikipedia.org/wiki/Special:Search?search=%s" utf-8)
  40. ;; online dictionary
  41. ("d" "https://dictionary.reference.com/search?q=%s" utf-8)
  42. ;; financial dictionary
  43. ("f" "https://financial-dictionary.thefreedictionary.com/%s" utf-8))))
  44. (defvar my-w3m-global-keyword nil
  45. "`w3m-display-hook' searches buffer with the keyword.")
  46. (defun my-w3m-guess-keyword (&optional encode-space-with-plus)
  47. "Guess keyword to search with ENCODE-SPACE-WITH-PLUS."
  48. (my-ensure 'w3m)
  49. (let* ((keyword (my-use-selected-string-or-ask "Enter keyword:"))
  50. (encoded-keyword (w3m-url-encode-string (setq my-w3m-global-keyword keyword))))
  51. ;; some search requires plus sign to replace space
  52. (if encode-space-with-plus
  53. (replace-regexp-in-string "%20" " " encoded-keyword)
  54. encoded-keyword)))
  55. (defun my-w3m-customized-search-api (search-engine &optional encode-space-with-plus)
  56. (my-ensure 'w3m)
  57. (w3m-search search-engine (my-w3m-guess-keyword encode-space-with-plus)))
  58. (defun my-w3m-stackoverflow-search ()
  59. (interactive)
  60. (my-w3m-customized-search-api "q"))
  61. (defun my-w3m-generic-search ()
  62. "Google search keyword"
  63. (interactive)
  64. (my-w3m-customized-search-api "g"))
  65. (defun my-w3m-search-financial-dictionary ()
  66. "Search financial dictionary."
  67. (interactive)
  68. (my-w3m-customized-search-api "f" t))
  69. (defun my-w3m-mode-hook-setup ()
  70. (w3m-lnum-mode 1)
  71. (local-set-key (kbd ";") 'my-w3m-lnum-follow))
  72. (add-hook 'w3m-mode-hook 'my-w3m-mode-hook-setup)
  73. ; {{ Search using external browser
  74. (setq browse-url-generic-program
  75. (cond
  76. (*is-a-mac* ; mac
  77. "open")
  78. (*unix* ; linux or unix
  79. ;; prefer Chrome than Firefox
  80. (or (executable-find "google-chrome")
  81. (executable-find "firefox")))
  82. (t
  83. ;; Windows: you need add "firefox.exe" to environment variable PATH
  84. ;; @see https://en.wikipedia.org/wiki/PATH_(variable)
  85. (executable-find "firefox")
  86. ;; if you prefer chrome
  87. ;; (executable-find "chrome")
  88. )))
  89. (setq browse-url-browser-function 'browse-url-generic)
  90. ;; use external browser to search programming stuff
  91. (defun my-w3m-hacker-search ()
  92. "Search on all programming related sites in external browser"
  93. (interactive)
  94. (let ((keyword (my-w3m-guess-keyword)))
  95. ;; google
  96. (browse-url-generic (concat "https://www.duckduckgo.com/?q=%22"
  97. keyword
  98. "%22"
  99. (if buffer-file-name
  100. (concat "+filetype%3A" (file-name-extension buffer-file-name))
  101. "")))
  102. ;; stackoverflow.com
  103. (browse-url-generic (concat "https://www.duckduckgo.com/?q="
  104. keyword
  105. "+site:stackoverflow.com" ))))
  106. ;; }}
  107. (defun my-w3m-open-link-or-image-or-url ()
  108. "Opens current link or image or current page's uri in other browser."
  109. (interactive)
  110. (let* (url)
  111. (when (or (string= major-mode "w3m-mode") (string= major-mode "gnus-article-mode"))
  112. (setq url (w3m-anchor))
  113. (if (or (not url) (string= url "buffer://"))
  114. (setq url (or (w3m-image) w3m-current-url))))
  115. (browse-url-generic (if url url (car (browse-url-interactive-arg "URL: "))))))
  116. (defun my-w3m-encode-specials (str)
  117. (setq str (replace-regexp-in-string "(" "%28" str))
  118. (setq str (replace-regexp-in-string ")" "%29" str))
  119. (setq str (replace-regexp-in-string ")" "%20" str)))
  120. (defun my-w3m-open-with-mplayer ()
  121. "Open the media file embedded."
  122. (interactive)
  123. (let (url cmd str)
  124. (when (memq major-mode '(w3m-mode gnus-article-mode))
  125. ;; weird, `w3m-anchor' fail to extract url while `w3m-image' can
  126. (unless (setq url (or (w3m-anchor) (w3m-image)))
  127. (save-excursion
  128. (goto-char (point-min))
  129. (when (string-match "^Archived-at: <?\\([^ <>]*\\)>?" (setq str (my-buffer-str)))
  130. (setq url (match-string 1 str)))))
  131. (setq url (my-w3m-encode-specials url))
  132. (setq cmd (format "%s -cache 2000 %s &" (my-guess-mplayer-path) url))
  133. (when (string= url "buffer://")
  134. ;; cache 2M data and don't block UI
  135. (setq cmd (my-guess-image-viewer-path url t))))
  136. (if url (shell-command cmd))))
  137. (defun my-w3m-subject-to-target-filename ()
  138. (let (rlt str)
  139. (save-excursion
  140. (goto-char (point-min))
  141. ;; first line in email could be some hidden line containing NO to field
  142. (setq str (my-buffer-str)))
  143. ;; (message "str=%s" str)
  144. (if (string-match "^Subject: \\(.+\\)" str)
  145. (setq rlt (match-string 1 str)))
  146. ;; clean the timestamp at the end of subject
  147. (setq rlt (replace-regexp-in-string "[ 0-9_.'/-]+$" "" rlt))
  148. (setq rlt (replace-regexp-in-string "'s " " " rlt))
  149. (setq rlt (replace-regexp-in-string "[ ,_'/-]+" "-" rlt))
  150. rlt))
  151. (defun my-w3m-download-rss-stream ()
  152. "Download rss stream."
  153. (interactive)
  154. (when (or (string= major-mode "w3m-mode") (string= major-mode "gnus-article-mode"))
  155. (let* ((url (w3m-anchor)) cmd)
  156. (cond
  157. ((or (not url) (string= url "buffer://"))
  158. (message "This link is not video/audio stream."))
  159. (t
  160. (setq cmd (format "curl -L %s > %s.%s" url (my-w3m-subject-to-target-filename) (file-name-extension url)))
  161. (kill-new cmd)
  162. (my-pclip cmd)
  163. (message "%s => clipboard/kill-ring" cmd))))))
  164. (with-eval-after-load 'w3m
  165. (define-key w3m-mode-map (kbd "C-c b") 'my-w3m-open-link-or-image-or-url)
  166. (add-hook 'w3m-display-hook
  167. (lambda (url)
  168. (let* ((title (or w3m-current-title url)))
  169. (when my-w3m-global-keyword
  170. ;; search keyword twice, first is url, second is your input,
  171. ;; third is actual result
  172. (goto-char (point-min))
  173. (search-forward-regexp (replace-regexp-in-string " " ".*" my-w3m-global-keyword) (point-max) t 3)
  174. ;; move the cursor to the beginning of word
  175. (backward-char (length my-w3m-global-keyword))
  176. ;; cleanup for next search
  177. (setq my-w3m-global-keyword nil))
  178. ;; rename w3m buffer
  179. (rename-buffer
  180. (format "*w3m: %s*"
  181. (substring title 0 (min 50 (length title)))) t)))))
  182. (provide 'init-emacs-w3m)