2
|
1 ;;; w3-hot.el --- Main functions for emacs-w3 on all platforms/versions
|
|
2 ;; Author: wmperry
|
70
|
3 ;; Created: 1996/07/26 05:22:59
|
|
4 ;; Version: 1.5
|
2
|
5 ;; Keywords: faces, help, comm, news, mail, processes, mouse, hypermedia
|
|
6
|
0
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2
|
8 ;;; Copyright (c) 1993 - 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
0
|
9 ;;;
|
70
|
10 ;;; This file is not part of GNU Emacs, but the same permissions apply.
|
0
|
11 ;;;
|
|
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
13 ;;; it under the terms of the GNU General Public License as published by
|
|
14 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;;; any later version.
|
|
16 ;;;
|
|
17 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 ;;; GNU General Public License for more details.
|
|
21 ;;;
|
|
22 ;;; You should have received a copy of the GNU General Public License
|
70
|
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
24 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
0
|
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
26
|
|
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
28 ;;; Structure for hotlists
|
|
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
30 ;;; (
|
|
31 ;;; ("name of item1" . "http://foo.bar.com/") ;; A single item in hotlist
|
|
32 ;;; ("name of item2" . ( ;; A sublist
|
|
33 ;;; ("name of item3" . "http://www.ack.com/")
|
|
34 ;;; ))
|
|
35 ;;; ) ; end of hotlist
|
|
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
37 (require 'w3-vars)
|
|
38
|
|
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
40 ;;; Hotlist Handling Code
|
|
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
42 (defvar w3-html-bookmarks nil)
|
|
43
|
2
|
44 (defun w3-hotlist-break-shit ()
|
|
45 (let ((todo '(w3-hotlist-apropos
|
|
46 w3-hotlist-delete
|
|
47 w3-hotlist-rename-entry
|
|
48 w3-hotlist-append
|
|
49 w3-use-hotlist
|
|
50 w3-hotlist-add-document
|
|
51 w3-hotlist-add-document-at-point
|
|
52 ))
|
|
53 (cur nil))
|
|
54 (while todo
|
|
55 (setq cur (car todo)
|
|
56 todo (cdr todo))
|
|
57 (fset cur
|
|
58 (`
|
|
59 (lambda (&rest ignore)
|
|
60 (error "Sorry, `%s' does not work with html bookmarks"
|
|
61 (quote (, cur)))))))))
|
|
62
|
0
|
63 (defun w3-read-html-bookmarks (fname)
|
|
64 "Import an HTML file into the Emacs-w3 format."
|
|
65 (interactive "fBookmark file: ")
|
|
66 (if (not (file-readable-p fname))
|
|
67 (error "Can not read %s..." fname))
|
|
68 (save-excursion
|
|
69 (set-buffer (get-buffer-create " *bookmark-work*"))
|
|
70 (erase-buffer)
|
|
71 (insert-file-contents fname)
|
|
72 (let* ((w3-debug-html nil)
|
|
73 (bkmarks nil)
|
70
|
74 (parse (w3-parse-buffer (current-buffer) t)))
|
0
|
75 (setq parse w3-last-parse-tree
|
|
76 bkmarks (nreverse (w3-grok-html-bookmarks parse))
|
2
|
77 w3-html-bookmarks bkmarks)))
|
|
78 (w3-hotlist-break-shit))
|
0
|
79
|
|
80 (eval-when-compile
|
|
81 (defvar cur-stack nil)
|
|
82 (defvar cur-title nil)
|
|
83 (defmacro push-new-menu ()
|
|
84 '(setq cur-stack (cons (list "") cur-stack)))
|
|
85
|
|
86 (defmacro push-new-item (title href)
|
|
87 (` (setcar cur-stack (cons (vector (, title) (list 'w3-fetch (, href)) t)
|
|
88 (car cur-stack)))))
|
|
89 ;;(` (setcar cur-stack (cons (cons (, title) (, href)) (car cur-stack)))))
|
|
90
|
|
91 (defmacro finish-submenu ()
|
|
92 '(let ((x (nreverse (car cur-stack))))
|
|
93 (and x (setcar x (car cur-title)))
|
|
94 (setq cur-stack (cdr cur-stack)
|
|
95 cur-title (cdr cur-title))
|
|
96 (if cur-stack
|
|
97 (setcar cur-stack (cons x (car cur-stack)))
|
|
98 (setq cur-stack (list x)))))
|
|
99 )
|
|
100
|
|
101 (defun w3-grok-html-bookmarks-internal (tree)
|
|
102 (let (node tag content args)
|
|
103 (while tree
|
|
104 (setq node (car tree)
|
|
105 tree (cdr tree)
|
|
106 tag (and (listp node) (nth 0 node))
|
|
107 args (and (listp node) (nth 1 node))
|
|
108 content (and (listp node) (nth 2 node)))
|
|
109 (cond
|
|
110 ((eq tag 'title)
|
|
111 (setq cur-title (list (w3-normalize-spaces (car content))))
|
|
112 (w3-grok-html-bookmarks-internal content))
|
2
|
113 ((memq tag '(dl ol ul))
|
0
|
114 (push-new-menu)
|
|
115 (w3-grok-html-bookmarks-internal content)
|
|
116 (finish-submenu))
|
2
|
117 ((and (memq tag '(dt li))
|
0
|
118 (stringp (car content)))
|
|
119 (setq cur-title (cons (w3-normalize-spaces (car content))
|
|
120 cur-title)))
|
|
121 ((and (eq tag 'a)
|
|
122 (stringp (car-safe content))
|
|
123 (cdr-safe (assq 'href args)))
|
|
124 (push-new-item (w3-normalize-spaces (car-safe content))
|
|
125 (cdr-safe (assq 'href args))))
|
|
126 (content
|
|
127 (w3-grok-html-bookmarks-internal content))))))
|
|
128
|
|
129 (defun w3-grok-html-bookmarks (chunk)
|
|
130 (let (
|
|
131 cur-title
|
|
132 cur-stack
|
|
133 )
|
|
134 (w3-grok-html-bookmarks-internal chunk)
|
|
135 (reverse (car cur-stack))))
|
|
136
|
|
137 (defun w3-hotlist-apropos (regexp)
|
|
138 "Show hotlist entries matching REGEXP."
|
|
139 (interactive "sW3 Hotlist Apropos (regexp): ")
|
|
140 (or w3-setup-done (w3-do-setup))
|
|
141 (let ((save-buf (get-buffer "Hotlist")) ; avoid killing this
|
|
142 (w3-hotlist
|
|
143 (apply
|
|
144 'nconc
|
|
145 (mapcar
|
|
146 (function
|
|
147 (lambda (entry)
|
|
148 (if (or (string-match regexp (car entry))
|
|
149 (string-match regexp (car (cdr entry))))
|
|
150 (list entry))))
|
|
151 w3-hotlist))))
|
|
152 (if (not w3-hotlist)
|
|
153 (message "No w3-hotlist entries match \"%s\"" regexp)
|
|
154 (and save-buf (save-excursion
|
|
155 (set-buffer save-buf)
|
|
156 (rename-buffer (concat "Hotlist during " regexp))))
|
|
157 (unwind-protect
|
|
158 (progn
|
|
159 (w3-show-hotlist)
|
|
160 (rename-buffer (concat "Hotlist \"" regexp "\""))
|
70
|
161 (setq url-current-file (concat "hotlist/" regexp)))
|
0
|
162 (and save-buf (save-excursion
|
|
163 (set-buffer save-buf)
|
|
164 (rename-buffer "Hotlist")))))))
|
|
165
|
|
166 (defun w3-hotlist-refresh ()
|
|
167 "Reload the default hotlist file into memory"
|
|
168 (interactive)
|
70
|
169 (w3-parse-hotlist)
|
|
170 (if (fboundp 'w3-add-hotlist-menu) (w3-add-hotlist-menu)))
|
0
|
171
|
|
172 (defun w3-delete-from-alist (x alist)
|
|
173 ;; Remove X from ALIST, return new alist
|
|
174 (if (eq (assoc x alist) (car alist)) (cdr alist)
|
|
175 (delq (assoc x alist) alist)))
|
|
176
|
|
177 (defun w3-hotlist-delete ()
|
|
178 "Deletes a document from your hotlist file"
|
|
179 (interactive)
|
|
180 (save-excursion
|
|
181 (if (not w3-hotlist) (message "No hotlist in memory!")
|
|
182 (if (not (file-exists-p w3-hotlist-file))
|
|
183 (message "Hotlist file %s does not exist." w3-hotlist-file)
|
|
184 (let* ((completion-ignore-case t)
|
|
185 (title (car (assoc (completing-read "Delete Document: "
|
|
186 w3-hotlist nil t)
|
|
187 w3-hotlist)))
|
|
188 (case-fold-search nil)
|
|
189 (buffer (get-buffer-create " *HOTW3*")))
|
|
190 (and (string= title "") (error "No document specified."))
|
|
191 (set-buffer buffer)
|
|
192 (erase-buffer)
|
|
193 (insert-file-contents w3-hotlist-file)
|
|
194 (goto-char (point-min))
|
|
195 (if (re-search-forward (concat "^" (regexp-quote title) "\r*$")
|
|
196 nil t)
|
|
197 (let ((make-backup-files nil)
|
|
198 (version-control nil)
|
|
199 (require-final-newline t))
|
|
200 (previous-line 1)
|
|
201 (beginning-of-line)
|
|
202 (delete-region (point) (progn (forward-line 2) (point)))
|
|
203 (write-file w3-hotlist-file)
|
|
204 (setq w3-hotlist (w3-delete-from-alist title w3-hotlist))
|
|
205 (kill-buffer (current-buffer)))
|
70
|
206 (message "%s was not found in %s" title w3-hotlist-file))))))
|
|
207 (and (fboundp 'w3-add-hotlist-menu) (funcall 'w3-add-hotlist-menu)))
|
0
|
208
|
|
209 (defun w3-hotlist-rename-entry (title)
|
|
210 "Rename a hotlist item"
|
|
211 (interactive (list (let ((completion-ignore-case t))
|
|
212 (completing-read "Rename entry: " w3-hotlist nil t))))
|
|
213 (cond ; Do the error handling first
|
|
214 ((string= title "") (error "No document specified!"))
|
|
215 ((not w3-hotlist) (error "No hotlist in memory!"))
|
|
216 ((not (file-exists-p (expand-file-name w3-hotlist-file)))
|
|
217 (error "Hotlist file %s does not exist." w3-hotlist-file))
|
|
218 ((not (file-readable-p (expand-file-name w3-hotlist-file)))
|
|
219 (error "Hotlist file %s exists, but is unreadable." w3-hotlist-file)))
|
|
220 (save-excursion
|
|
221 (let ((obj (assoc title w3-hotlist))
|
|
222 (used (mapcar 'car w3-hotlist))
|
|
223 (buff (get-buffer-create " *HOTW3*"))
|
|
224 (new nil)
|
|
225 )
|
|
226 (while (or (null new) (member new used))
|
|
227 (setq new (read-string "New name: ")))
|
|
228 (set-buffer buff)
|
|
229 (erase-buffer)
|
|
230 (insert-file-contents (expand-file-name w3-hotlist-file))
|
|
231 (goto-char (point-min))
|
|
232 (if (re-search-forward (regexp-quote title) nil t)
|
|
233 (let ((make-backup-files nil)
|
|
234 (version-control nil)
|
|
235 (require-final-newline t))
|
|
236 (previous-line 1)
|
|
237 (beginning-of-line)
|
|
238 (delete-region (point) (progn (forward-line 2) (point)))
|
|
239 (insert (format "%s %s\n%s\n" (nth 1 obj) (current-time-string)
|
|
240 new))
|
|
241 (setq w3-hotlist (cons (list new (nth 1 obj))
|
|
242 (w3-delete-from-alist title w3-hotlist)))
|
|
243 (write-file w3-hotlist-file)
|
|
244 (kill-buffer (current-buffer))
|
|
245 (if (and w3-running-FSF19 (not (eq 'tty (device-type))))
|
|
246 (progn
|
|
247 (delete-menu-item '("Go"))
|
|
248 (w3-build-FSF19-menu))))
|
70
|
249 (message "%s was not found in %s" title w3-hotlist-file))))
|
|
250 (and (fboundp 'w3-add-hotlist-menu) (funcall 'w3-add-hotlist-menu)))
|
0
|
251
|
|
252 (defun w3-hotlist-append (fname)
|
|
253 "Append a hotlist to the one in memory"
|
|
254 (interactive "fAppend hotlist file: ")
|
|
255 (let ((x w3-hotlist))
|
|
256 (w3-parse-hotlist fname)
|
70
|
257 (setq w3-hotlist (nconc x w3-hotlist))
|
|
258 (and (fboundp 'w3-add-hotlist-menu) (funcall 'w3-add-hotlist-menu))))
|
0
|
259
|
2
|
260 (defun w3-hotlist-parse-old-mosaic-format ()
|
|
261 (let (cur-link cur-alias)
|
|
262 (while (re-search-forward "^\n" nil t) (replace-match ""))
|
|
263 (goto-line 3)
|
|
264 (while (not (eobp))
|
|
265 (re-search-forward "^[^ ]*" nil t)
|
|
266 (setq cur-link (buffer-substring (match-beginning 0) (match-end 0)))
|
|
267 (setq cur-alias (buffer-substring (progn
|
|
268 (forward-line 1)
|
|
269 (beginning-of-line)
|
|
270 (point))
|
|
271 (progn
|
|
272 (end-of-line)
|
|
273 (point))))
|
|
274 (if (not (equal cur-alias ""))
|
70
|
275 (setq w3-hotlist (cons (list cur-alias cur-link) w3-hotlist)))
|
|
276 (if (fboundp 'w3-add-hotlist-menu)
|
|
277 (funcall 'w3-add-hotlist-menu)))))
|
2
|
278
|
0
|
279 (defun w3-parse-hotlist (&optional fname)
|
|
280 "Read in the hotlist specified by FNAME"
|
|
281 (if (not fname) (setq fname w3-hotlist-file))
|
|
282 (setq w3-hotlist nil)
|
|
283 (if (not (file-exists-p fname))
|
|
284 (message "%s does not exist!" fname)
|
|
285 (let* ((old-buffer (current-buffer))
|
|
286 (buffer (get-buffer-create " *HOTW3*"))
|
2
|
287 (case-fold-search t))
|
0
|
288 (set-buffer buffer)
|
|
289 (erase-buffer)
|
|
290 (insert-file-contents fname)
|
|
291 (goto-char (point-min))
|
2
|
292 (cond
|
|
293 ((looking-at "ncsa-xmosaic-hotlist-format-1");; Old-style NCSA Mosaic
|
|
294 (w3-hotlist-parse-old-mosaic-format))
|
|
295 ((or (looking-at "<!DOCTYPE") ; Some HTML style, including netscape
|
|
296 (re-search-forward "<a[ \n]+href" nil t))
|
|
297 (w3-read-html-bookmarks fname))
|
|
298 (t
|
|
299 (message "Cannot determine format of hotlist file: %s" fname)))
|
|
300 (set-buffer-modified-p nil)
|
0
|
301 (kill-buffer buffer)
|
|
302 (set-buffer old-buffer))))
|
|
303
|
|
304 ;;;###autoload
|
|
305 (defun w3-use-hotlist ()
|
|
306 "Possibly go to a link in your W3/Mosaic hotlist.
|
|
307 This is part of the emacs World Wide Web browser. It will prompt for
|
|
308 one of the items in your 'hotlist'. A hotlist is a list of often
|
|
309 visited or interesting items you have found on the World Wide Web."
|
|
310 (interactive)
|
|
311 (if (not w3-setup-done) (w3-do-setup))
|
|
312 (if (not w3-hotlist) (message "No hotlist in memory!")
|
|
313 (let* ((completion-ignore-case t)
|
|
314 (url (car (cdr (assoc
|
|
315 (completing-read "Goto Document: " w3-hotlist nil t)
|
|
316 w3-hotlist)))))
|
|
317 (if (string= "" url) (error "No document specified!"))
|
|
318 (w3-fetch url))))
|
|
319
|
|
320 (defun w3-hotlist-add-document-at-point (pref-arg)
|
|
321 "Add the document pointed to by the hyperlink under point to the hotlist."
|
|
322 (interactive "P")
|
70
|
323 (let ((url (w3-view-this-url t)) title)
|
0
|
324 (or url (error "No link under point."))
|
70
|
325 (setq title (get-text-property (point) 'title))
|
|
326 (if (and title
|
|
327 (marker-buffer (car title))
|
|
328 (marker-buffer (cdr title)))
|
|
329 (setq title (buffer-substring-no-properties (car title) (cdr title)))
|
|
330 (setq title "None"))
|
|
331 (w3-hotlist-add-document pref-arg title url)
|
|
332 (and (fboundp 'w3-add-hotlist-menu) (funcall 'w3-add-hotlist-menu))))
|
0
|
333
|
|
334 (defun w3-hotlist-add-document (pref-arg &optional the-title the-url)
|
|
335 "Add this documents url to the hotlist"
|
|
336 (interactive "P")
|
|
337 (save-excursion
|
|
338 (let* ((buffer (get-buffer-create " *HOTW3*"))
|
|
339 (title (or the-title
|
|
340 (and pref-arg (read-string "Title: "))
|
|
341 (buffer-name)))
|
|
342 (make-backup-files nil)
|
|
343 (version-control nil)
|
|
344 (require-final-newline t)
|
|
345 (url (or the-url (url-view-url t))))
|
|
346 (if (rassoc (list url) w3-hotlist)
|
|
347 (error "That item already in hotlist, use w3-hotlist-rename-entry."))
|
|
348 (set-buffer buffer)
|
|
349 (erase-buffer)
|
|
350 (setq w3-hotlist (cons (list title url) w3-hotlist)
|
|
351 url (url-unhex-string url))
|
|
352 (if (not (file-exists-p w3-hotlist-file))
|
|
353 (progn
|
|
354 (message "Creating hotlist file %s" w3-hotlist-file)
|
|
355 (insert "ncsa-xmosaic-hotlist-format-1\nDefault\n\n")
|
|
356 (backward-char 1))
|
|
357 (progn
|
|
358 (insert-file-contents w3-hotlist-file)
|
|
359 (goto-char (point-max))
|
|
360 (backward-char 1)))
|
70
|
361 (insert "\n" (url-hexify-string url) " " (current-time-string)
|
|
362 "\n" title)
|
0
|
363 (write-file w3-hotlist-file)
|
70
|
364 (kill-buffer (current-buffer))))
|
|
365 (and (fboundp 'w3-add-hotlist-menu) (funcall 'w3-add-hotlist-menu)))
|
0
|
366
|
|
367 (provide 'w3-hot)
|