File: mew-browse.el

package info (click to toggle)
mew-beta 7.0.50~6.6+0.20140902-1+deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 4,640 kB
  • ctags: 4,784
  • sloc: lisp: 36,518; ansic: 3,597; haskell: 577; sh: 440; makefile: 414; ruby: 310; perl: 58
file content (343 lines) | stat: -rw-r--r-- 12,713 bytes parent folder | download | duplicates (17)
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
;;; mew-browse.el --- Handling URI with browse-url.el

;; Author: Hideyuki SHIRAI <shirai@mew.org>
;; Modify: Shuichi Kitaguchi <kit@Mew.org>
;; Created: May 19, 1999
;; Revised: Nov 08, 2004

;;;
;;; ~/.emacs settings.
;;;
;;; ... anything browse-url setting ...
;; (require 'mew-browse)

;;; SHIFT + (Middle|Right)-Click = browse-url or mew-user-agent-compose
;;; for Emacs
;; (define-key global-map [S-mouse-2] 'browse-url-at-mouse)
;;; for XEmacs
;; (define-key global-map [(shift button2)] 'browse-url-at-mouse)
;;

;;; Appending URI to specified file.
;;
;;   mew-browse-noask                 ... ask or not when browse
;;   mew-browse-append-file           ... URL collection file name
;;   mew-browse-append-always-file    ... always, append URL to file (for dial-up)
;;   mew-browse-append-always-mailto  ... always, URL is mailto: (for emacs19.28)
;;   mew-browse-append-file-sort      ... always, sort URL file
;;
;;; example:
;;   (setq mew-browse-noask                nil)
;;   (setq mew-browse-append-file          "~/.browse")
;;   (setq mew-browse-append-always-file   nil)
;;   (setq mew-browse-append-always-mailto nil)
;;   (setq mew-browse-append-file-sort nil)
;;

;;; Use mew-url-mailto instead of url-mailto in W3, add followings in your ~/.emacs file.
;;
;; (cond
;;  ((locate-library "url-mail")
;;   (eval-after-load "url-mail"
;;     '(fset 'url-mailto (symbol-function 'mew-url-mailto))))
;;  ((locate-library "url")
;;   (eval-after-load "url"
;;     '(fset 'url-mailto (symbol-function 'mew-url-mailto)))))
;;

;;; Use from emacs-w3m, add followings in your ~/.emacs file.
;;  (setq w3m-mailto-url-function 'mew-url-mailto)
;;

;;; Use from MS-Windows application, set followings in your registry.
;;   registry key: '\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\mailto\\shell\\open\\command'
;;   registry value: 'drive:\\path\\gnudoitw.exe \"(mew-url-mailto \"%%1\")\"'"
;;

;;; Use from Mozilla & Firefox with MozEx, set followings on Mailer form of MozEx.
;; "/path/gnudoit (mew-url-mailto-mozex "%r")"
;; or
;; "/path/emacseval (mew-url-mailto-mozex "%r")"
;; 
;; `emacseval' is a shell script like this,
;; ----cut here----
;; #!/bin/sh
;; /usr/local/bin/emacsclient --eval "`echo $*`"
;; ----cut here----"
;;

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

(if (string-match "XEmacs" emacs-version)
    (defvar mew-browse-button [(button2)] "*Mouse button in message mode.")
  (defvar mew-browse-button [mouse-2] "*Mouse button in message mode."))

(defvar mew-ext-prog-url mew-prog-text/html-ext)
(defvar mew-ext-prog-url-args nil)

(setq browse-url-browser-function 'mew-browse-url)

(defvar mew-browse-url-mailto-switch-func nil
  "*Which do you like, nil, 'switch-to-buffer-other-window or 'switch-to-buffer-other-frame ?")

(setq browse-url-regexp "\\(\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]+\\)\\|\\(\\([^-A-Za-z0-9!_.%]\\|^\\)[-A-Za-z0-9._!%]+@[A-Za-z0-9][-A-Za-z0-9._!]+[A-Za-z0-9]\\)")

(defvar mew-browse-noask                t   "*Ask or not when browse.")
(defvar mew-browse-append-file          nil "*URL collection file.")
(defvar mew-browse-append-always-file   nil "*For dialup user.")
(defvar mew-browse-append-always-mailto nil "*For emacs19.28.")
(defvar mew-browse-append-file-sort     nil "*Sort URL file.")

(defun mew-browse-url (url &optional args)
  "Exec browse URL or mew-user-agent-compose with parsing RFC2368."
  (interactive
   (list (read-from-minibuffer "Mew URL: ")))
  (when (or (not (boundp 'mew-init-p)) (null mew-init-p))
    (save-excursion
      (require 'mew)
      (mew-init)))
  (let* ((append-buffer (and mew-browse-append-file
			     (string= buffer-file-name
				      (expand-file-name mew-browse-append-file))))
	 (append-nil (or append-buffer (not mew-browse-append-file)))
	 (append-all (and (not append-nil) mew-browse-append-always-file))
	 (append-ask (and (not append-nil) (not mew-browse-append-always-file)))
	 (browse-all (or append-buffer mew-browse-noask))
	 (browse-ask (and (not append-buffer) (not mew-browse-noask))))
    (string-match "\\([a-zA-Z0-9][-a-zA-Z0-9!_=?#$@~`%&*+|\\/.,:]+\\)" url)
    (setq url (substring url (match-beginning 0) (match-end 0)))
    (unless (string-match ":" url)    ;; emacs19.28 only
      (if (and (not mew-browse-append-always-mailto)
	       (not (y-or-n-p (format "mailto:%s(y) or ftp://%s(n)? " url url))))
	  (setq url (concat "ftp://" url))
	(setq url (concat "mailto:" url))))
    (cond
     ((and append-all browse-all)
      (mew-browse-url-append url)
      (mew-browse-url-start url))
     ((and append-ask browse-all)
      (when (y-or-n-p (format "Append %s? " url))
	(mew-browse-url-append url))
      (mew-browse-url-start url))
     ((and append-nil browse-all)
      (mew-browse-url-start url))
     ((and append-all browse-ask)
      (mew-browse-url-append url)
      (when (y-or-n-p (format "Browse %s? " url))
	(mew-browse-url-start url)))
     ((and append-nil browse-ask)
      (when (y-or-n-p (format "Browse %s? " url))
	(mew-browse-url-start url)))
     (t ;; (and append-ask browse-ask)
      (if (y-or-n-p (format "Browse %s(y) or Append(n)? " url))
	  (mew-browse-url-start url)
	(mew-browse-url-append url)))
     )))

(defun mew-browse-url-append (url)
  (let ((file (expand-file-name mew-browse-append-file))
	(beg))
    (save-excursion
      (find-file file)
      (set-buffer (current-buffer))
      (goto-char (point-min))
      (while (search-forward url nil t)
	(progn
	  (beginning-of-line)
	  (setq beg (point))
	  (forward-line)
	  (delete-region beg (point))))
      (goto-char (point-max))
      (insert url "\n")
      (when mew-browse-append-file-sort
	(sort-lines nil (point-min) (point-max)))
      (write-file file)
      (kill-buffer (current-buffer))
      (message "Append %s to %s done" url file)
      )))

(defun mew-browse-url-start (url)
  (message "Browse %s" url)
  (cond
   ((string-match "^mailto:" url)
    (mew-url-mailto url))
   ((and (symbolp mew-ext-prog-url) (fboundp mew-ext-prog-url))
    (funcall mew-ext-prog-url url))
   ((equal mew-ext-prog-url "w3")
    (require 'w3)
    (w3-fetch-other-frame url))
   (t
    (let ((orig mew-ext-prog-url-args)
	  args replace)
      (while orig
	(if (string-match "%s" (car orig))
	    (progn
	      (setq args (cons (format (car orig) url) args))
	      (setq esqp t)
	      (setq replace t))
	  (setq args (cons (car orig) args)))
	(setq orig (cdr orig)))
      (setq args (nreverse args))
      (apply (function start-process)
	     (format "*mew %s*" mew-ext-prog-url)
	     nil mew-ext-prog-url
	     (if replace
		 args
	       (append args (list url))))))))

(defun mew-url-mailto (url)
  "Execute mew-user-agent-compose with parsing RFC2368.

If use from emacs-w3m, add followings in your ~/.emacs file.
  (setq w3m-mailto-url-function 'mew-url-mailto)

If use from MS-Windows application, set followings in your registry.
  registry key: '\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\mailto\\shell\\open\\command'
  registry value: 'drive:\\path\\gnudoitw.exe \"(mew-url-mailto \"%%1\")\"'"
  (interactive
   (list (read-from-minibuffer "Mew mailto: ")))
  (when (or (not (boundp 'mew-init-p)) (null mew-init-p))
    (save-excursion
      (require 'mew)
      (mew-init)))
  (let ((lst (mew-browse-url-mailto-analysis url)))
    (mew-user-agent-compose (nth 0 lst)	;; to
			    (nth 1 lst)	;; subject
			    (nth 2 lst) ;; other
			    nil mew-browse-url-mailto-switch-func)))

(defun mew-url-mailto-mozex (url)
  "Execute mew-user-agent-compose from Mozilla and Firefox with MozEx.

If use this command, set followings on Mailer form of MozEx.
\"/path/gnudoit (mew-url-mailto-mozex \"%r\")\"
or
\"/path/emacseval (mew-url-mailto-mozex \"%r\")\"

`emacseval' is a shell script like this,
----cut here----
#!/bin/sh
/usr/local/bin/emacsclient --eval \"`echo $*`\"
----cut here----"
  (interactive)
  (when (or (not (boundp 'mew-init-p)) (null mew-init-p))
    (save-excursion
      (require 'mew)
      (mew-init)))
  (let ((lst (mew-browse-url-mailto-analysis url 'mozex)))
    (mew-user-agent-compose (nth 0 lst)	;; to
			    (nth 1 lst)	;; subject
			    (nth 2 lst) ;; other
			    nil mew-browse-url-mailto-switch-func)))

;; return '(to subject other)"
(defun mew-browse-url-mailto-analysis (url &optional mozex)
  (let (to subject other)
    (while (string-match "[ \t]+" url)
      (setq url (concat (substring url 0 (match-beginning 0))
			(substring url (match-end 0)))))
    (if (string-match "^mailto:" url)
	(setq url (mew-browse-url-mailto-decamp (substring url (match-end 0))))
      (setq url (mew-browse-url-mailto-decamp url)))
    (when (string-match "^\\([^?]+\\)" url)
      (setq to (mew-browse-url-mailto-hex-to-string
		(substring url (match-beginning 1) (match-end 1))))
      (setq url (substring url (match-end 0))))
    (while (string-match "^[?&]\\([^=]+\\)=\\([^&]*\\)" url)
      (let ((hname (substring url (match-beginning 1) (match-end 1)))
	    (hvalue (mew-browse-url-mailto-hex-to-string
		     (substring url (match-beginning 2) (match-end 2)))))
	(setq url (substring url (match-end 0)))
	(when mozex
	  (setq hname (mew-browse-url-mailto-hex-to-string hname))
	  (while (string-match "[ \t\n]+" hname)
	    (setq hname (replace-match "" nil nil hname)))
	  (when (string-match "[ \t\n]+$" hvalue)
	    (setq hvalue (replace-match "" nil nil hvalue))))
	(cond
	 ((string-match "^to$" hname)
	  (if to
	      (setq to (concat to ", " hvalue))
	    (setq to hvalue)))
	 ((string-match "^subject$" hname)
	  (setq subject hvalue))
	 (t
	  (setq other (cons (cons (capitalize hname) hvalue) other))))))
    `(,to ,subject ,other)))

(defun mew-browse-url-mailto-decamp (str)
  (save-match-data
    (while (string-match "&amp;" str)
      (setq str (concat (substring str 0 (match-beginning 0))
			"&"
			(substring str (match-end 0)))))
    str))

(defun mew-browse-url-mailto-hex-to-string (str)
  (save-match-data
    (with-temp-buffer
      (insert str)
      (goto-char (point-min))
      (let ((doit t) char cs)
	(while (re-search-forward "%\\([0-9a-fA-F][0-9a-fA-F]\\)" nil t)
	  (setq char (mew-browse-url-mailto-2hexs-to-int
		      (buffer-substring (match-beginning 1) (match-end 1))))
	  (delete-region (match-beginning 0) (match-end 0))
	  (insert char))
	(setq cs (mew-charset-to-cs
		  (mew-charset-guess-region (point-min) (point-max))))
	(when (or (not cs)
		  (eq cs mew-cs-unknown))
	  (setq cs mew-cs-autoconv))
	(when (and (eq cs 'utf-8)
		   (not mew-internal-utf-8p)
		   (not (featurep 'un-define)))
	  (condition-case nil
	      (require 'un-define)
	    (file-error
	     (setq doit nil)
	     (delete-region (point-min) (point-max))
	     (insert "Install Mule-UCS for UTF-8.\n"))))
	(when doit
	  (mew-set-buffer-multibyte nil)
	  (decode-coding-region (point-min) (point-max) cs)
	  (mew-set-buffer-multibyte t))
	(buffer-string)))))

(defun mew-browse-url-mailto-2hexs-to-int (hex)
  (+ (* 16 (mew-hexchar-to-int (aref hex 0)))
     (mew-hexchar-to-int (aref hex 1))))

(provide 'mew-browse)

;;; Copyright Notice:

;; Copyright (C) 1999-2004 Mew developing team.
;; All rights reserved.

;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 
;; 1. Redistributions of source code must retain the above copyright
;;    notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;;    notice, this list of conditions and the following disclaimer in the
;;    documentation and/or other materials provided with the distribution.
;; 3. Neither the name of the team nor the names of its contributors
;;    may be used to endorse or promote products derived from this software
;;    without specific prior written permission.
;; 
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
;; PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; mew-browse.el ends here