Debian Codegraph @

contrib/mew-browse.el

raw ◀ ▶
1 Debian patch touch this file
•1 ;;; mew-browse.el --- Handling URI with browse-url.el
2
3 ;; Author: Hideyuki SHIRAI <[email protected]>
4 ;; Modify: Shuichi Kitaguchi <[email protected]>
5 ;; Created: May 19, 1999
6 ;; Revised: Nov 08, 2004
7
8 ;;;
9 ;;; ~/.emacs settings.
10 ;;;
11 ;;; ... anything browse-url setting ...
12 ;; (require 'mew-browse)
13
14 ;;; SHIFT + (Middle|Right)-Click = browse-url or mew-user-agent-compose
15 ;;; for Emacs
16 ;; (define-key global-map [S-mouse-2] 'browse-url-at-mouse)
17 ;;; for XEmacs
18 ;; (define-key global-map [(shift button2)] 'browse-url-at-mouse)
19 ;;
20
21 ;;; Appending URI to specified file.
22 ;;
23 ;; mew-browse-noask ... ask or not when browse
24 ;; mew-browse-append-file ... URL collection file name
25 ;; mew-browse-append-always-file ... always, append URL to file (for dial-up)
26 ;; mew-browse-append-always-mailto ... always, URL is mailto: (for emacs19.28)
27 ;; mew-browse-append-file-sort ... always, sort URL file
28 ;;
29 ;;; example:
30 ;; (setq mew-browse-noask nil)
31 ;; (setq mew-browse-append-file "~/.browse")
32 ;; (setq mew-browse-append-always-file nil)
33 ;; (setq mew-browse-append-always-mailto nil)
34 ;; (setq mew-browse-append-file-sort nil)
35 ;;
36
37 ;;; Use mew-url-mailto instead of url-mailto in W3, add followings in your ~/.emacs file.
38 ;;
39 ;; (cond
40 ;; ((locate-library "url-mail")
41 ;; (eval-after-load "url-mail"
42 ;; '(fset 'url-mailto (symbol-function 'mew-url-mailto))))
43 ;; ((locate-library "url")
44 ;; (eval-after-load "url"
45 ;; '(fset 'url-mailto (symbol-function 'mew-url-mailto)))))
46 ;;
47
48 ;;; Use from emacs-w3m, add followings in your ~/.emacs file.
49 ;; (setq w3m-mailto-url-function 'mew-url-mailto)
50 ;;
51
52 ;;; Use from MS-Windows application, set followings in your registry.
53 ;; registry key: '\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\mailto\\shell\\open\\command'
54 ;; registry value: 'drive:\\path\\gnudoitw.exe \"(mew-url-mailto \"%%1\")\"'"
55 ;;
56
57 ;;; Use from Mozilla & Firefox with MozEx, set followings on Mailer form of MozEx.
58 ;; "/path/gnudoit (mew-url-mailto-mozex "%r")"
59 ;; or
60 ;; "/path/emacseval (mew-url-mailto-mozex "%r")"
61 ;;
62 ;; `emacseval' is a shell script like this,
63 ;; ----cut here----
64 ;; #!/bin/sh
65 ;; /usr/local/bin/emacsclient --eval "`echo $*`"
66 ;; ----cut here----"
67 ;;
68
69 (eval-when-compile (require 'mew))
70
71 (if (string-match "XEmacs" emacs-version)
72 (defvar mew-browse-button [(button2)] "*Mouse button in message mode.")
73 (defvar mew-browse-button [mouse-2] "*Mouse button in message mode."))
74
75 (defvar mew-ext-prog-url mew-prog-text/html-ext)
76 (defvar mew-ext-prog-url-args nil)
77
78 (setq browse-url-browser-function 'mew-browse-url)
79
80 (defvar mew-browse-url-mailto-switch-func nil
81 "*Which do you like, nil, 'switch-to-buffer-other-window or 'switch-to-buffer-other-frame ?")
82
83 (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]\\)")
84
85 (defvar mew-browse-noask t "*Ask or not when browse.")
86 (defvar mew-browse-append-file nil "*URL collection file.")
87 (defvar mew-browse-append-always-file nil "*For dialup user.")
88 (defvar mew-browse-append-always-mailto nil "*For emacs19.28.")
89 (defvar mew-browse-append-file-sort nil "*Sort URL file.")
90
91 (defun mew-browse-url (url &optional args)
92 "Exec browse URL or mew-user-agent-compose with parsing RFC2368."
93 (interactive
94 (list (read-from-minibuffer "Mew URL: ")))
95 (when (or (not (boundp 'mew-init-p)) (null mew-init-p))
96 (save-excursion
97 (require 'mew)
98 (mew-init)))
99 (let* ((append-buffer (and mew-browse-append-file
100 (string= buffer-file-name
101 (expand-file-name mew-browse-append-file))))
102 (append-nil (or append-buffer (not mew-browse-append-file)))
103 (append-all (and (not append-nil) mew-browse-append-always-file))
104 (append-ask (and (not append-nil) (not mew-browse-append-always-file)))
105 (browse-all (or append-buffer mew-browse-noask))
106 (browse-ask (and (not append-buffer) (not mew-browse-noask))))
107 (string-match "\\([a-zA-Z0-9][-a-zA-Z0-9!_=?#$@~`%&*+|\\/.,:]+\\)" url)
108 (setq url (substring url (match-beginning 0) (match-end 0)))
109 (unless (string-match ":" url) ;; emacs19.28 only
110 (if (and (not mew-browse-append-always-mailto)
111 (not (y-or-n-p (format "mailto:%s(y) or ftp://%s(n)? " url url))))
112 (setq url (concat "ftp://" url))
113 (setq url (concat "mailto:" url))))
114 (cond
115 ((and append-all browse-all)
116 (mew-browse-url-append url)
117 (mew-browse-url-start url))
118 ((and append-ask browse-all)
119 (when (y-or-n-p (format "Append %s? " url))
120 (mew-browse-url-append url))
121 (mew-browse-url-start url))
122 ((and append-nil browse-all)
123 (mew-browse-url-start url))
124 ((and append-all browse-ask)
125 (mew-browse-url-append url)
126 (when (y-or-n-p (format "Browse %s? " url))
127 (mew-browse-url-start url)))
128 ((and append-nil browse-ask)
129 (when (y-or-n-p (format "Browse %s? " url))
130 (mew-browse-url-start url)))
131 (t ;; (and append-ask browse-ask)
132 (if (y-or-n-p (format "Browse %s(y) or Append(n)? " url))
133 (mew-browse-url-start url)
134 (mew-browse-url-append url)))
135 )))
136
137 (defun mew-browse-url-append (url)
138 (let ((file (expand-file-name mew-browse-append-file))
139 (beg))
140 (save-excursion
141 (find-file file)
142 (set-buffer (current-buffer))
143 (goto-char (point-min))
144 (while (search-forward url nil t)
145 (progn
146 (beginning-of-line)
147 (setq beg (point))
148 (forward-line)
149 (delete-region beg (point))))
150 (goto-char (point-max))
151 (insert url "\n")
152 (when mew-browse-append-file-sort
153 (sort-lines nil (point-min) (point-max)))
154 (write-file file)
155 (kill-buffer (current-buffer))
156 (message "Append %s to %s done" url file)
157 )))
158
159 (defun mew-browse-url-start (url)
160 (message "Browse %s" url)
161 (cond
162 ((string-match "^mailto:" url)
163 (mew-url-mailto url))
164 ((and (symbolp mew-ext-prog-url) (fboundp mew-ext-prog-url))
165 (funcall mew-ext-prog-url url))
166 ((equal mew-ext-prog-url "w3")
167 (require 'w3)
168 (w3-fetch-other-frame url))
169 (t
170 (let ((orig mew-ext-prog-url-args)
171 args replace)
172 (while orig
173 (if (string-match "%s" (car orig))
174 (progn
175 (setq args (cons (format (car orig) url) args))
176 (setq esqp t)
177 (setq replace t))
178 (setq args (cons (car orig) args)))
179 (setq orig (cdr orig)))
180 (setq args (nreverse args))
181 (apply (function start-process)
182 (format "*mew %s*" mew-ext-prog-url)
183 nil mew-ext-prog-url
184 (if replace
185 args
186 (append args (list url))))))))
187
188 (defun mew-url-mailto (url)
189 "Execute mew-user-agent-compose with parsing RFC2368.
190
191 If use from emacs-w3m, add followings in your ~/.emacs file.
192 (setq w3m-mailto-url-function 'mew-url-mailto)
193
194 If use from MS-Windows application, set followings in your registry.
195 registry key: '\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\mailto\\shell\\open\\command'
196 registry value: 'drive:\\path\\gnudoitw.exe \"(mew-url-mailto \"%%1\")\"'"
197 (interactive
198 (list (read-from-minibuffer "Mew mailto: ")))
199 (when (or (not (boundp 'mew-init-p)) (null mew-init-p))
200 (save-excursion
201 (require 'mew)
202 (mew-init)))
203 (let ((lst (mew-browse-url-mailto-analysis url)))
204 (mew-user-agent-compose (nth 0 lst) ;; to
205 (nth 1 lst) ;; subject
206 (nth 2 lst) ;; other
207 nil mew-browse-url-mailto-switch-func)))
208
209 (defun mew-url-mailto-mozex (url)
210 "Execute mew-user-agent-compose from Mozilla and Firefox with MozEx.
211
212 If use this command, set followings on Mailer form of MozEx.
213 \"/path/gnudoit (mew-url-mailto-mozex \"%r\")\"
214 or
215 \"/path/emacseval (mew-url-mailto-mozex \"%r\")\"
216
217 `emacseval' is a shell script like this,
218 ----cut here----
219 #!/bin/sh
220 /usr/local/bin/emacsclient --eval \"`echo $*`\"
221 ----cut here----"
222 (interactive)
223 (when (or (not (boundp 'mew-init-p)) (null mew-init-p))
224 (save-excursion
225 (require 'mew)
226 (mew-init)))
227 (let ((lst (mew-browse-url-mailto-analysis url 'mozex)))
228 (mew-user-agent-compose (nth 0 lst) ;; to
229 (nth 1 lst) ;; subject
230 (nth 2 lst) ;; other
231 nil mew-browse-url-mailto-switch-func)))
232
233 ;; return '(to subject other)"
234 (defun mew-browse-url-mailto-analysis (url &optional mozex)
235 (let (to subject other)
236 (while (string-match "[ \t]+" url)
237 (setq url (concat (substring url 0 (match-beginning 0))
238 (substring url (match-end 0)))))
239 (if (string-match "^mailto:" url)
240 (setq url (mew-browse-url-mailto-decamp (substring url (match-end 0))))
241 (setq url (mew-browse-url-mailto-decamp url)))
242 (when (string-match "^\\([^?]+\\)" url)
243 (setq to (mew-browse-url-mailto-hex-to-string
244 (substring url (match-beginning 1) (match-end 1))))
245 (setq url (substring url (match-end 0))))
246 (while (string-match "^[?&]\\([^=]+\\)=\\([^&]*\\)" url)
247 (let ((hname (substring url (match-beginning 1) (match-end 1)))
248 (hvalue (mew-browse-url-mailto-hex-to-string
249 (substring url (match-beginning 2) (match-end 2)))))
250 (setq url (substring url (match-end 0)))
251 (when mozex
252 (setq hname (mew-browse-url-mailto-hex-to-string hname))
253 (while (string-match "[ \t\n]+" hname)
254 (setq hname (replace-match "" nil nil hname)))
255 (when (string-match "[ \t\n]+$" hvalue)
256 (setq hvalue (replace-match "" nil nil hvalue))))
257 (cond
258 ((string-match "^to$" hname)
259 (if to
260 (setq to (concat to ", " hvalue))
261 (setq to hvalue)))
262 ((string-match "^subject$" hname)
263 (setq subject hvalue))
264 (t
265 (setq other (cons (cons (capitalize hname) hvalue) other))))))
266 `(,to ,subject ,other)))
267
268 (defun mew-browse-url-mailto-decamp (str)
269 (save-match-data
270 (while (string-match "&amp;" str)
271 (setq str (concat (substring str 0 (match-beginning 0))
272 "&"
273 (substring str (match-end 0)))))
274 str))
275
276 (defun mew-browse-url-mailto-hex-to-string (str)
277 (save-match-data
278 (with-temp-buffer
279 (insert str)
280 (goto-char (point-min))
281 (let ((doit t) char cs)
282 (while (re-search-forward "%\\([0-9a-fA-F][0-9a-fA-F]\\)" nil t)
283 (setq char (mew-browse-url-mailto-2hexs-to-int
284 (buffer-substring (match-beginning 1) (match-end 1))))
285 (delete-region (match-beginning 0) (match-end 0))
286 (insert char))
287 (setq cs (mew-charset-to-cs
288 (mew-charset-guess-region (point-min) (point-max))))
289 (when (or (not cs)
290 (eq cs mew-cs-unknown))
291 (setq cs mew-cs-autoconv))
292 (when (and (eq cs 'utf-8)
293 (not mew-internal-utf-8p)
294 (not (featurep 'un-define)))
295 (condition-case nil
296 (require 'un-define)
297 (file-error
298 (setq doit nil)
299 (delete-region (point-min) (point-max))
300 (insert "Install Mule-UCS for UTF-8.\n"))))
301 (when doit
302 (mew-set-buffer-multibyte nil)
303 (decode-coding-region (point-min) (point-max) cs)
304 (mew-set-buffer-multibyte t))
305 (buffer-string)))))
306
307 (defun mew-browse-url-mailto-2hexs-to-int (hex)
308 (+ (* 16 (mew-hexchar-to-int (aref hex 0)))
309 (mew-hexchar-to-int (aref hex 1))))
310
311 (provide 'mew-browse)
312
313 ;;; Copyright Notice:
314
315 ;; Copyright (C) 1999-2004 Mew developing team.
316 ;; All rights reserved.
317
318 ;; Redistribution and use in source and binary forms, with or without
319 ;; modification, are permitted provided that the following conditions
320 ;; are met:
321 ;;
322 ;; 1. Redistributions of source code must retain the above copyright
323 ;; notice, this list of conditions and the following disclaimer.
324 ;; 2. Redistributions in binary form must reproduce the above copyright
325 ;; notice, this list of conditions and the following disclaimer in the
326 ;; documentation and/or other materials provided with the distribution.
327 ;; 3. Neither the name of the team nor the names of its contributors
328 ;; may be used to endorse or promote products derived from this software
329 ;; without specific prior written permission.
330 ;;
331 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
332 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
333 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
334 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
335 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
336 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
337 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
338 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
339 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
340 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
341 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
342
343 ;;; mew-browse.el ends here