File: w3m-search.el

package info (click to toggle)
w3m-el 1.4.569+0.20170110-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 5,780 kB
  • sloc: lisp: 45,666; sh: 515; makefile: 403
file content (367 lines) | stat: -rw-r--r-- 13,160 bytes parent folder | download | duplicates (4)
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
;;; w3m-search.el --- functions convenient to access web search engines

;; Copyright (C) 2001--2012 TSUCHIYA Masatoshi <tsuchiya@namazu.org>

;; Authors: Keisuke Nishida    <kxn30@po.cwru.edu>,
;;          Shun-ichi GOTO     <gotoh@taiyo.co.jp>,
;;          TSUCHIYA Masatoshi <tsuchiya@namazu.org>,
;;          Romain FRANCOISE   <romain@orebokech.com>
;; Keywords: w3m, WWW, hypermedia

;; This file is a part of emacs-w3m.

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; This module contains the `w3m-search' command and some utilities
;; to improve your cyberlife.  For more detail about emacs-w3m, see:
;;
;;    http://emacs-w3m.namazu.org/

;;; Code:

(eval-when-compile
  (require 'cl))

(require 'w3m)

(defcustom w3m-search-engine-alist
  (let* ((ja (equal "Japanese" w3m-language))
	 (utf-8 (or (and (boundp 'mule-version)
			 (not (string< (symbol-value 'mule-version) "6.0")))
		    (featurep 'un-define)
		    (fboundp 'utf-translate-cjk-mode)
		    (and (not ja) (w3m-find-coding-system 'utf-8)))))
    `(,@(if ja
	    '(("yahoo"
	       "http://search.yahoo.co.jp/bin/search?p=%s"
	       euc-japan)
	      ("yahoo-en"
	       "http://search.yahoo.com/bin/search?p=%s"))
	  '(("yahoo"
	     "http://search.yahoo.com/bin/search?p=%s")
	    ("yahoo-ja"
	     "http://search.yahoo.co.jp/bin/search?p=%s"
	     euc-japan)))
      ("alc" "http://eow.alc.co.jp/%s/UTF-8/" utf-8)
      ,@(cond
	 ((and ja utf-8)
	  '(("blog"
	     "http://blogsearch.google.com/blogsearch?q=%s&hl=ja&lr=lang_ja&oe=utf-8&ie=utf-8"
	     utf-8)
	    ("blog-en"
	     "http://blogsearch.google.com/blogsearch?q=%s&hl=en&oe=utf-8&ie=utf-8"
	     utf-8)))
	 (ja
	  '(("blog"
	     "http://blogsearch.google.com/blogsearch?q=%s&hl=ja&lr=lang_ja&ie=Shift_JIS&oe=Shift_JIS"
	     shift_jis)
	    ("blog-en"
	     "http://blogsearch.google.com/blogsearch?q=%s&hl=en")))
	 (utf-8
	  '(("blog"
	     "http://blogsearch.google.com/blogsearch?q=%s&oe=utf-8&ie=utf-8"
	     utf-8)
	    ("blog-en"
	     "http://blogsearch.google.com/blogsearch?q=%s&hl=en&oe=utf-8&ie=utf-8"
	     utf-8)))
	 (t
	  '(("blog"
	     "http://blogsearch.google.com/blogsearch?q=%s")
	    ("blog-ja"
	     "http://blogsearch.google.com/blogsearch?q=%s&lr=lang_ja&ie=Shift_JIS&oe=Shift_JIS"
	     shift_jis))))
      ,@(cond
	 ((and ja utf-8)
	  '(("google"
	     "http://www.google.com/search?q=%s&hl=ja&lr=lang_ja&ie=utf-8&oe=utf-8"
	     utf-8)
	    ("google-en"
	     "http://www.google.com/search?q=%s&hl=en&ie=utf-8&oe=utf-8"
	     utf-8)))
	 (ja
	  '(("google"
	     "http://www.google.com/search?q=%s&hl=ja&lr=lang_ja&ie=Shift_JIS&oe=Shift_JIS"
	     shift_jis)
	    ("google-en"
	     "http://www.google.com/search?q=%s&hl=en")))
	 (utf-8
	  '(("google"
	     "http://www.google.com/search?q=%s&ie=utf-8&oe=utf-8"
	     utf-8)
	    ("google-en"
	     "http://www.google.com/search?q=%s&hl=en&ie=utf-8&oe=utf-8"
	     utf-8)))
	 (t
	  '(("google"
	     "http://www.google.com/search?q=%s")
	    ("google-ja"
	     "http://www.google.com/search?q=%s&hl=ja&lr=lang_ja&ie=Shift_JIS&oe=Shift_JIS"
	     shift_jis))))
      ,@(cond
	 ((and ja utf-8)
	  '(("google news"
	     "http://news.google.co.jp/news?q=%s&hl=ja&ie=utf-8&oe=utf-8"
	     utf-8)
	    ("google news-en"
	     "http://news.google.com/news?q=%s&hl=en")))
	 (ja
	  '(("google news"
	     "http://news.google.co.jp/news?q=%s&hl=ja&ie=Shift_JIS&oe=Shift_JIS"
	     shift_jis)
	    ("google news-en"
	     "http://news.google.com/news?q=%s&hl=en")))
	 (utf-8
	  '(("google news"
	     "http://news.google.com/news?q=%s&ie=utf-8&oe=utf-8"
	     utf-8)
	    ("google news-en"
	     "http://news.google.com/news?q=%s&hl=en&ie=utf-8&oe=utf-8")))
	 (t
	  '(("google news"
	     "http://news.google.com/news?q=%s")
	    ("google news-ja"
	     "http://news.google.co.jp/news?q=%s&hl=ja&ie=Shift_JIS&oe=Shift_JIS"
	     shift_jis))))
      ("google groups"
       "http://groups.google.com/groups?q=%s")
      ,@(if ja
	    '(("All the Web"
	       "http://www.alltheweb.com/search?q=%s&web&_sb_lang=ja&cs=euc-jp"
	       euc-japan)
	      ("All the Web-en"
	       "http://www.alltheweb.com/search?q=%s&web&_sb_lang=en"))
	  '(("All the Web"
	     "http://www.alltheweb.com/search?q=%s&web&_sb_lang=en")
	    ("All the Web-ja"
	     "http://www.alltheweb.com/search?q=%s&web&_sb_lang=ja&cs=euc-jp"
	     euc-japan)))
      ,@(if ja
	    '(("technorati"
	       "http://www.technorati.jp/search/search.html?query=%s&language=ja"
	       utf-8)
	      ("technorati-en"
	       "http://www.technorati.com/search/%s"
	       utf-8))
	  '(("technorati"
	     "http://www.technorati.com/search/%s"
	     utf-8)
	    ("technorati-ja"
	     "http://www.technorati.jp/search/search.html?query=%s&language=ja"
	     utf-8)))
      ("technorati-tag"
       "http://www.technorati.com/tag/%s"
       utf-8)
      ("goo-ja"
       "http://search.goo.ne.jp/web.jsp?MT=%s"
       euc-japan)
      ("excite-ja"
       "http://www.excite.co.jp/search.gw?target=combined&look=excite_jp\
&lang=jp&tsug=-1&csug=-1&search=%s"
       shift_jis)
      ("altavista"
       "http://altavista.com/sites/search/web?q=\"%s\"&kl=ja&search=Search")
      ("rpmfind"
       "http://rpmfind.net/linux/rpm2html/search.php?query=%s"
       nil)
      ("debian-pkg"
       "http://packages.debian.org/cgi-bin/search_contents.pl\
?directories=yes&arch=i386&version=unstable&case=insensitive&word=%s")
      ("debian-bts"
       "http://bugs.debian.org/cgi-bin/pkgreport.cgi?archive=yes&pkg=%s")
      ("freebsd-users-jp"
       "http://home.jp.FreeBSD.org/cgi-bin/namazu.cgi?key=\"%s\"&whence=0\
&max=50&format=long&sort=score&dbname=FreeBSD-users-jp"
       euc-japan)
      ("iij-archie"
       "http://www.iij.ad.jp/cgi-bin/archieplexform?query=%s\
&type=Case+Insensitive+Substring+Match&order=host&server=archie1.iij.ad.jp\
&hits=95&nice=Nice")
      ("waei"
       "http://dictionary.goo.ne.jp/search.php?MT=%s&kind=je"
       euc-japan)
      ("eiwa"
       "http://dictionary.goo.ne.jp/search.php?MT=%s&kind=ej")
      ("kokugo"
       "http://dictionary.goo.ne.jp/search.php?MT=%s&kind=jn"
       euc-japan)
      ("eiei"
       "http://www.dictionary.com/cgi-bin/dict.pl?term=%s&r=67")
      ,@(if ja
	    '(("amazon"
	       "http://www.amazon.co.jp/gp/search?\
__mk_ja_JP=%%83J%%83%%5E%%83J%%83i&url=search-alias%%3Daps&field-keywords=%s"
	       shift_jis)
	      ("amazon-en"
	       "http://www.amazon.com/exec/obidos/search-handle-form/\
250-7496892-7797857"
	       iso-8859-1
	       "url=index=blended&field-keywords=%s"))
	  '(("amazon"
	     "http://www.amazon.com/exec/obidos/search-handle-form/\
250-7496892-7797857"
	     iso-8859-1
	     "url=index=blended&field-keywords=%s")
	    ("amazon-ja"
	       "http://www.amazon.co.jp/gp/search?\
__mk_ja_JP=%%83J%%83%%5E%%83J%%83i&url=search-alias%%3Daps&field-keywords=%s"
	       shift_jis)))
      ("emacswiki" "http://www.emacswiki.org/cgi-bin/wiki?search=%s")
      ("en.wikipedia" "http://en.wikipedia.org/wiki/Special:Search?search=%s")
      ("de.wikipedia" "http://de.wikipedia.org/wiki/Spezial:Search?search=%s"
       utf-8)
      ("ja.wikipedia" "http://ja.wikipedia.org/wiki/Special:Search?search=%s"
       utf-8)
      ("msdn" "http://search.msdn.microsoft.com/search/default.aspx?query=%s")
      ("freshmeat" "http://freshmeat.net/search/?q=%s&section=projects")))
  "*An alist of search engines.
Each element looks like (ENGINE ACTION CODING POST-DATA)
ENGINE is a string, the name of the search engine.
ACTION is a string, the URL that performs a search.
ACTION must contain a \"%s\", which is substituted by a query string.
CODING is optional value which is coding system for query string.
POST-DATA is optional value which is a string for POST method search engine.
If CODING is omitted, it defaults to `w3m-default-coding-system'."
  :group 'w3m
  :type `(repeat
	  (group :indent 2
		 (string :format "Engine: %v\n" :size 0)
		 (string :format "       Action: %v\n" :size 0)
		 (coding-system :format "%t: %v\n" :size 0)
		 (checklist :inline t
			    :entry-format ,(if (w3m-device-on-window-system-p)
					       "%b   %v"
					     "%b  %v")
			    (string :format "PostData: %v\n" :size 0)))))

(defcustom w3m-search-default-engine "google"
  "*Name of the default search engine.
See also `w3m-search-engine-alist'."
  :group 'w3m
  :type '(string :size 0))

(defcustom w3m-search-word-at-point t
  "*Non-nil means that the word at point is used as an initial string.
If Transient Mark mode, this option is ignored and the region is used
as an initial string."
  :group 'w3m
  :type 'boolean)

(defvar w3m-search-engine-history nil
  "History variable used by `w3m-search' for prompting a search engine.")

(defvar w3m-search-thing-at-point-arg 'word
  "Argument for `thing-at-point' used in `w3m-search-read-query'")

(defun w3m-search-escape-query-string (str &optional coding)
  (mapconcat (lambda (s) (w3m-url-encode-string s coding))
	     (split-string str)
	     "+"))

(defun w3m-search-read-query (prompt prompt-with-default &optional history)
  "Read a query from the minibuffer, prompting with string PROMPT.
When a default value for the query is discovered, prompt with string
PROMPT-WITH-DEFAULT instead of string PROMPT."
  (let ((default
	  (if (w3m-region-active-p)
	      (buffer-substring (region-beginning) (region-end))
	    (unless (and (eq major-mode 'w3m-mode)
			 (listp (get-text-property (point-at-bol) 'face))
			 (memq 'w3m-header-line-location-title
			       (get-text-property (point-at-bol) 'face)))
	      (thing-at-point w3m-search-thing-at-point-arg))))
	initial)
    (when default
      (set-text-properties 0 (length default) nil default)
      (when (or w3m-search-word-at-point (w3m-region-active-p))
	(setq initial default
	      default nil))
      (when (w3m-region-active-p)
	(w3m-deactivate-region)))
    (read-string (if default
		     (format prompt-with-default default)
		   prompt)
		 initial history default)))

(defun w3m-search-read-variables ()
  "Ask for a search engine and words to query and return them as a list."
  (when w3m-current-process
    (error "%s"
	   (substitute-command-keys "
Cannot run two w3m processes simultaneously \
\(Type `\\<w3m-mode-map>\\[w3m-process-stop]' to stop asynchronous process)")))
  (let* ((search-engine
	  (if current-prefix-arg
	      (let ((default (or (car w3m-search-engine-history)
				 w3m-search-default-engine))
		    (completion-ignore-case t))
		(completing-read (format "Which engine? (default %s): "
					 default)
				 w3m-search-engine-alist nil t nil
				 'w3m-search-engine-history default))
	    w3m-search-default-engine))
	 (query
	  (w3m-search-read-query
	   (format "%s search: " search-engine)
	   (format "%s search (default %%s): " search-engine))))
    (list search-engine query)))

(defun w3m-search-do-search (w3m-goto-function search-engine query)
  "Call W3M-GOTO-FUNCTION with the URL for the search."
  (unless (string= query "")
    (let ((info (assoc search-engine w3m-search-engine-alist)))
      (if info
	  (let ((query-string (w3m-search-escape-query-string query
							      (caddr info)))
		(post-data (cadddr info)))
	    (w3m-history-store-position)
	    (funcall w3m-goto-function
		     (format (cadr info) query-string)
		     post-data
		     nil
		     (and post-data (format post-data query-string))))
	(error "Unknown search engine: %s" search-engine)))))

;;;###autoload
(defun w3m-search (search-engine query)
  "Search QUERY using SEARCH-ENGINE.
When called interactively with a prefix argument, you can choose one of
the search engines defined in `w3m-search-engine-alist'.  Otherwise use
`w3m-search-default-engine'.
If Transient Mark mode, use the region as an initial string of query
and deactivate the mark."
  (interactive (w3m-search-read-variables))
  (w3m-search-do-search 'w3m-goto-url search-engine query))

;;;###autoload
(defun w3m-search-new-session (search-engine query)
  "Like `w3m-search', but do the search in a new session."
  (interactive (w3m-search-read-variables))
  (w3m-search-do-search 'w3m-goto-url-new-session search-engine query))

;;;###autoload
(defun w3m-search-uri-replace (uri engine)
  "Generate query string for ENGINE from URI matched by last search."
  (let ((query (substring uri (match-end 0)))
	(info (assoc engine w3m-search-engine-alist)))
    (when info
      (format (cadr info)
	      (w3m-search-escape-query-string query (caddr info))))))

(provide 'w3m-search)

;;; w3m-search.el ends here