Initial Commit
[packages] / xemacs-packages / w3 / lisp / w3.el
1 ;;; w3.el --- Main functions for emacs-w3 on all platforms/versions
2 ;; Author: $Author: legoscia $
3 ;; Created: $Date: 2007/11/15 12:59:53 $
4 ;; Version: $Revision: 1.40 $
5 ;; Keywords: faces, help, comm, news, mail, processes, mouse, hypermedia
6
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; Copyright (c) 1996, 97, 98, 99, 2001, 2007, 2008 Free Software Foundation, Inc.
9 ;;; Copyright (c) 1993 - 1996 by William M. Perry <wmperry@cs.indiana.edu>
10 ;;;
11 ;;; This file is part of GNU Emacs.
12 ;;;
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;;; it under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 2, or (at your option)
16 ;;; any later version.
17 ;;;
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;;; GNU General Public License for more details.
22 ;;;
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;;; Boston, MA 02111-1307, USA.
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;;; This is a major mode for browsing documents written in Hypertext Markup ;;;
31 ;;; Language (HTML).  These documents are typicallly part of the World Wide ;;;
32 ;;; Web (WWW), a project to create a global information net in hypertext    ;;;
33 ;;; format.                                                                 ;;;
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35
36 (require 'w3-compat)
37 (require 'w3-cfg)
38
39 (or (featurep 'efs)
40     (featurep 'efs-auto)
41     (condition-case ()
42         (require 'ange-ftp)
43       (error nil)))
44
45 (eval-when-compile (require 'cl))
46 (require 'css)
47 (require 'url-vars)
48 (require 'url-parse)
49 (require 'w3-vars)
50 (eval-and-compile
51   (require 'w3-display))
52
53 ;; Some mm-* "functions" are macros.  Ensure that they are loaded.
54 (eval-when-compile
55   (require 'mm-decode))
56
57 (autoload 'w3-parse-hotlist "w3-hot")
58 (autoload 'w3-menu-install-menus "w3-menu")
59
60 \f
61 (defun w3-notify-when-ready (buff)
62   "Notify the user when BUFF is ready.
63 See the variable `w3-notify' for the different notification behaviors."
64   (if (stringp buff) (setq buff (get-buffer buff)))
65   (cond
66    ((null buff) nil)
67    ((eq w3-notify 'newframe)
68     ;; Since we run asynchronously, perhaps while Emacs is waiting for input,
69     ;; we must not leave a different buffer current.
70     ;; We can't rely on the editor command loop to reselect
71     ;; the selected window's buffer.
72     (save-excursion
73       (set-buffer buff)
74       (make-frame)))
75    ((eq w3-notify 'bully)
76     (pop-to-buffer buff)
77     (delete-other-windows))
78    ((eq w3-notify 'semibully)
79     (condition-case nil
80         (switch-to-buffer buff)
81       (error (message "W3 buffer %s is ready." (buffer-name buff)))))
82    ((eq w3-notify 'aggressive)
83     (pop-to-buffer buff))
84    ((eq w3-notify 'friendly)
85     (display-buffer buff 'not-this-window))
86    ((eq w3-notify 'polite)
87     (beep)
88     (message "W3 buffer %s is ready." (buffer-name buff)))
89    ((eq w3-notify 'quiet)
90     (message "W3 buffer %s is ready." (buffer-name buff)))
91    (t (message ""))))
92
93 ;;;###autoload
94 (defun w3-open-local (fname)
95   "Find a local file, and interpret it as a hypertext document.
96 Prompt for an existing file or directory, and retrieve it as a
97 hypertext document."
98   (interactive "FLocal file: ")
99   (setq fname (expand-file-name fname))
100   (w3-do-setup)
101   (w3-fetch (concat "file:" fname)))
102
103 ;;;###autoload
104 (defun w3-find-file (fname)
105   "Find a local file, and interpret it as a hypertext document.
106 Prompt for an existing file or directory, and retrieve it as a
107 hypertext document."
108   (interactive "FLocal file: ")
109   (w3-open-local fname))
110  
111 ;;;###autoload
112 (defun w3-fetch-other-frame (&optional url)
113   "Attempt to follow the hypertext reference under point in a new frame."
114   (interactive (list (w3-read-url-with-default)))
115   (cond
116    ((and (fboundp 'make-frame)
117          (fboundp 'select-frame)
118          (not (eq (device-type) 'tty)))
119     (let ((frm (make-frame)))
120       (select-frame frm)
121       (delete-other-windows)
122       (w3-fetch url)))
123    (t (w3-fetch url))))
124
125 (defun w3-fetch-other-window (&optional url)
126   "Attempt to follow the hypertext reference under point in a new window."
127   (interactive (list (w3-read-url-with-default)))
128   (split-window)
129   (w3-fetch url))
130
131 (defun w3-read-url-with-default ()
132   (url-do-setup)
133   (let* ((completion-ignore-case t)
134          (default
135            (cond
136             ((eq major-mode 'w3-mode)
137              (or (and current-prefix-arg (w3-view-this-url t))
138                  (url-view-url t)))
139             ((url-get-url-at-point)
140              (url-get-url-at-point))
141             (t "http://www.")))
142          (url nil))
143     (setq url
144           (completing-read "URL: "  'url-completion-function
145                            nil nil default))
146     (if (string= url "")
147         (setq url (if (eq major-mode 'w3-mode)
148                       (if (and current-prefix-arg (w3-view-this-url t))
149                           (w3-view-this-url t)
150                         (url-view-url t))
151                     (url-get-url-at-point))))
152     url))
153
154 (defvar w3-explicit-coding-system nil
155   "Coding system to decode documents.
156
157 The global value is usually nil.  It will be bound locally if a user
158 invokes some commands which read a coding system from the user.")
159
160 (defun w3-decode-charset (handle)
161   "Decode charset-encoded text in the document.
162 HANDLE is the MIME handle of the original part.
163 Return the coding system used for the decoding."
164   (let* ((encoding (mm-handle-encoding handle))
165          (charset (or w3-explicit-coding-system
166                       (mail-content-type-get (mm-handle-type handle)
167                                              'charset)
168                       "iso-8859-1"))
169          (type (mm-handle-media-type handle))
170          (coding-system (mm-charset-to-coding-system charset)))
171     (if (or (not coding-system)
172             (eq coding-system 'ascii))
173         ;; Does this work for XEmacs?  Should we actually guess (which
174         ;; is what `undecided' involves)?  In Emacs 20 we'll get
175         ;; byte-combination anyhow when switching to multibyte below,
176         ;; but we can't leave the buffer as unibyte, or we can't deal
177         ;; properly with non-ASCII entities inserted by the parsing
178         ;; stage.
179         (setq coding-system 'undecided))
180     (save-excursion
181       (if encoding
182           (mm-decode-content-transfer-encoding encoding type))
183       (when (and (featurep 'mule)
184                  (if (boundp 'default-enable-multibyte-characters)
185                      default-enable-multibyte-characters
186                    t)
187                  coding-system)
188         (mm-decode-coding-region (point-min) (point-max) coding-system)
189         ;; Potentially useful is the buffer's going to be saved, and
190         ;; for the mode-line indication.
191         (set-buffer-file-coding-system coding-system)))
192     coding-system))
193
194 (defvar http-header)                    ; dynamically bound below
195
196 (defun w3-http-equiv-headers (tree)
197   "Grovel parse TREE for <meta http-equiv...> elements.
198 Concatenate the equivalent MIME header onto the dynamically-bound
199 variable `http-header'."
200   (if (consp tree)
201       (dolist (node tree)
202         (if (consp node)
203             (if (eq 'meta (car-safe node))
204                 (let ((attrs (cadr node)))
205                   (if (assq 'http-equiv attrs)
206                       (if (assq 'content attrs)
207                           (setq http-header
208                                 (concat 
209                                  http-header
210                                  (format "%s: %s\n" 
211                                          (cdr (assq 'http-equiv attrs))
212                                          (cdr (assq 'content attrs))))))))
213               (w3-http-equiv-headers (nth 2 node)))))))
214
215 (defun w3-nasty-disgusting-http-equiv-handling (buffer url)
216   "Propagate information from <meta http-equiv...> elements to MIME headers.
217 Operate on BUFFER."
218   (let (content-type end-of-headers extra-headers)
219     (save-excursion
220       (set-buffer buffer)
221       (goto-char (point-min))
222       (mail-narrow-to-head)
223       (setq content-type (mail-fetch-field "content-type"))
224       (goto-char (point-max))    ; Make sure we are beyond the headers
225       (setq end-of-headers (point))
226       (widen)
227       (let ((case-fold-search t))
228         (if (and content-type (string-match "^text/html" content-type)
229                  ;; Try not to parse past the head element.
230                  (re-search-forward "</[ \n]*head\\|<[ \n]*body" nil t))
231             (let ((end-of-head (match-beginning 0)))
232               ;; Find any <meta http-equiv> stuff in the head so we
233               ;; can promote it into the MIME headers before
234               ;; mm-dissect-buffer looks at them.
235               (let (http-header)
236                 (save-restriction
237                   (narrow-to-region end-of-headers end-of-head)
238                   (goto-char (point-min))
239                   ;; Quick check before parsing.
240                   (if (search-forward "http-equiv=" nil t)
241                       (w3-http-equiv-headers
242                        ;; We need to take a copy of the region we're
243                        ;; going to parse (which we hope is small) to
244                        ;; avoid assumptions about what
245                        ;; `w3-parse-buffer' does in the way of
246                        ;; widening and munging character references
247                        ;; &c.
248                        (with-temp-buffer
249                          (setq url-current-object (url-generic-parse-url url))
250                          (insert-buffer-substring buffer
251                                                   end-of-headers end-of-head)
252                          (w3-parse-buffer)))))
253                 (when http-header
254                   (goto-char (point-min))
255                   (unless (save-excursion
256                             (search-forward ":" (line-end-position) t))
257                     (forward-line))
258                   (insert http-header)))))))))
259
260 (defun w3-setup-reload-timer (url must-be-viewing &optional time)
261   "Set up a timer to load URL at optional TIME.
262 If TIME is unspecified, default to 5 seconds.  Only loads document if
263 MUST-BE-VIEWING is the current URL when the timer expires."
264   (if (or (not time)
265           (<= time 0))
266       (setq time 5))
267   (let ((func
268          `(lambda ()
269             (if (equal (url-view-url t) ,must-be-viewing)
270                 (let ((w3-reuse-buffers 'no))
271                   (if (equal ,url (url-view-url t))
272                       (kill-buffer (current-buffer)))
273                   (w3-fetch ,url))))))
274     (cond
275      ((featurep 'itimer)
276       (start-itimer "reloader" func time))
277      ((fboundp 'run-at-time)
278       (run-at-time time nil func))
279      (t
280       (w3-warn 'url "Cannot set up timer for automatic reload, sorry!")))))
281
282 (defun w3-handle-refresh-header (reload)
283   (if (and reload
284            url-honor-refresh-requests
285            (or (eq url-honor-refresh-requests t)
286                (funcall url-confirmation-func "Honor refresh request? ")))
287       (let ((uri (url-view-url t)))
288         (if (string-match ";" reload)
289             (progn
290               (setq uri (substring reload (match-end 0) nil)
291                     reload (substring reload 0 (match-beginning 0)))
292               (if (string-match
293                    "ur[li][ \t]*=[ \t]*\"*\\([^ \t\"]+\\)\"*"
294                    uri)
295                   (setq uri (match-string 1 uri)))
296               (setq uri (url-expand-file-name uri (url-view-url t)))))
297         (w3-setup-reload-timer uri (url-view-url t)
298                                (string-to-int (or reload "5"))))))
299
300 (defun w3-fetch-redirect-callback (&rest args)
301   (let (redirect-url errorp)
302     ;; Handle both styles of `url-retrieve' callbacks...
303     (cond
304      ((listp (car args))
305       ;; Emacs 22 style.  First argument is a list.
306       (let ((status (car args)))
307         (when (eq (car status) :error)
308           (setq errorp t)
309           (setq status (cddr args)))
310         (when (eq (car status) :redirect)
311           (setq redirect-url (second (car args))))
312
313         (setq args (cdr args))))
314
315      ((eq (car args) :redirect)
316       ;; Pre-22 redirect.
317       (setq redirect-url (cadr args))
318       (while (eq (car args) :redirect)
319         (setq args (cddr args)))))
320
321     ;; w3-fetch-callback can handle errors, too.
322     (w3-fetch-callback (or redirect-url (car args)))))
323
324 (defun w3-fetch-callback (url)
325   (w3-nasty-disgusting-http-equiv-handling (current-buffer) url)
326   ;; Process any cookie and refresh headers.
327   (let (headers)
328     (ignore-errors
329       (save-restriction
330         (mail-narrow-to-head)
331         (goto-char (point-min))
332         (unless (save-excursion
333                   (search-forward ":" (line-end-position) t))
334           (forward-line))
335         (setq headers (mail-header-extract))
336         (let (refreshed)
337           (dolist (header headers)
338             ;; Act on multiple cookies if necessary, but only on a
339             ;; single refresh request in case there's more than one.
340             (case (car header)
341               (refresh (unless refreshed
342                          (w3-handle-refresh-header (cdr header))
343                          (setq refreshed t))))))))
344     (let ((handle (mm-dissect-buffer t))
345           (w3-explicit-coding-system
346            (or w3-explicit-coding-system
347                (w3-recall-explicit-coding-system url)))
348           (buff nil))
349       (message "Downloading of `%s' complete." url)
350       (url-mark-buffer-as-dead (current-buffer))
351       (unless headers
352         (setq headers (list (cons 'content-type
353                                   (mm-handle-media-type handle)))))
354       ;; Fixme: can handle be null?
355       (cond
356        ((or (equal (mm-handle-media-type handle) "text/html")
357             ;; Ultimately this should be handled by an XML parser, but
358             ;; this will mostly work for now:
359             (equal (mm-handle-media-type handle) "application/xhtml+xml"))
360         ;; Special case text/html if it comes through w3-fetch
361         (set-buffer (generate-new-buffer " *w3-html*"))
362         (mm-insert-part handle)
363         (w3-decode-charset handle)
364         (setq url-current-object (url-generic-parse-url url))
365         (w3-prepare-buffer)
366         (setq url-current-mime-headers headers)
367         (w3-notify-when-ready (current-buffer))
368         (mm-destroy-parts handle))
369         ;;        ((equal (mm-handle-media-type handle) "text/xml")
370         ;;      ;; Special case text/xml if it comes through w3-fetch
371         ;;      (set-buffer (generate-new-buffer " *w3-xml*"))
372         ;;      (mm-disable-multibyte)
373         ;;      (mm-insert-part handle)
374         ;;      (w3-decode-charset handle)
375         ;;      !!! Need some function to view XML nicely... maybe the
376         ;;      !!! customize tree control?
377         ;;      (setq url-current-object (url-generic-parse-url url)
378         ;;            url-current-mime-headers headers)
379         ;;      (mm-destroy-parts handle)
380         ;;      (w3-notify-when-ready (current-buffer)))
381        ((equal (car-safe (mm-handle-type handle))
382                "application/x-elisp-parsed-html")
383         ;; Also need to special-case pre-parsed representations of HTML.
384         ;; Fixme: will this need decoding?
385         (w3-prepare-tree (read (set-marker (make-marker) 1
386                                            (mm-handle-buffer handle)))))
387        ((mm-inlinable-p handle)
388         ;; We can view it inline!
389         (set-buffer (generate-new-buffer url))
390         (require 'mm-view)             ; make sure methods are defined
391         (mm-display-part handle)
392         (set-buffer-modified-p nil)
393         (w3-mode)
394         (if (equal "image" (mm-handle-media-supertype handle))
395             (setq cursor-type nil))
396         (setq url-current-mime-headers headers)
397         (w3-notify-when-ready (current-buffer)))
398        (t
399         ;; Must be an external viewer
400         (mm-display-part handle)
401         ;;(mm-destroy-parts handle)
402         )))))
403
404 ;;;###autoload
405 (defun w3-fetch (&optional url target)
406   "Retrieve a document over the World Wide Web.
407 Defaults to URL of the current document, if any.
408 With prefix argument, use the URL of the hyperlink under point instead."
409   (interactive (list (w3-read-url-with-default)))
410   (w3-do-setup)
411   (if (and (boundp 'command-line-args-left)
412            command-line-args-left
413            (string-match url-nonrelative-link (car command-line-args-left)))
414       (setq url (car command-line-args-left)
415             command-line-args-left (cdr command-line-args-left)))
416   (if (or (null url) (equal url "")) (error "No document specified!"))
417
418   ;; In the common case, this is probably cheaper than searching.
419   (while (= (string-to-char url) ? )
420     (setq url (substring url 1)))
421
422   (or target (setq target w3-base-target))
423   (if (stringp target)
424       (setq target (intern (downcase target))))
425   (and target
426        (let ((window-distance (cdr-safe (assq target
427                                               w3-target-window-distances))))
428          (if (numberp window-distance)
429              (other-window window-distance)
430            (case target
431              ((_blank external)
432               (w3-fetch-other-frame url))
433              (_top
434               (delete-other-windows))
435              (otherwise
436               (message "target %S not found." target))))))
437
438   (cond
439    ((= (string-to-char url) ?#)
440     (w3-relative-link url))
441    ((and (interactive-p) current-prefix-arg)
442     (w3-download-url url))
443    (t
444     (let ((x (url-view-url t))
445           (lastbuf (current-buffer))
446           (w3-current-buffer (current-buffer))
447           (buf (w3-buffer-visiting url)))
448       (if (or (not buf)
449               (cond
450                ((not (equal (downcase (or url-request-method "GET")) "get"))
451                 t)
452                ((memq w3-reuse-buffers '(no never reload)) t)
453                ((memq w3-reuse-buffers '(yes reuse always)) nil)
454                (t
455                 (when (and w3-reuse-buffers (not (eq w3-reuse-buffers 'ask)))
456                   (ding)
457                   (message
458                    "Warning: Invalid value for variable w3-reuse-buffers: %s"
459                    (prin1-to-string w3-reuse-buffers))
460                   (sit-for 2))
461                 (not (funcall url-confirmation-func
462                               (format "Reuse URL in buffer %s? "
463                                       (buffer-name buf)))))))
464           (url-retrieve url 'w3-fetch-redirect-callback (list url))
465         (w3-notify-when-ready buf))))))
466
467 \f
468 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
469 ;;; History for forward/back buttons
470 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 (defvar w3-history-stack nil
472   "History stack viewing history.
473 This is an assoc list, with the oldest items first.
474 Each element is a cons cell of (url . timeobj), where URL
475 is the normalized URL (default ports removed, etc), and TIMEOBJ is
476 a standard Emacs time.  See the `current-time' function documentation
477 for information on this format.")
478
479 (defun w3-history-find-url-internal (url)
480   "Search in the history list for URL.
481 Returns a cons cell, where the car is the 'back' node, and
482 the cdr is the 'next' node."
483   (let* ((node (assoc url w3-history-stack))
484          (next (cadr (memq node w3-history-stack)))
485          (last nil)
486          (temp nil)
487          (todo w3-history-stack))
488     ;; Last node is a little harder to find without using back links
489     (while (and (not last) todo)
490       (if (string= (caar todo) url)
491           (setq last (or temp 'none))
492         (setq temp (pop todo))))
493     (cons (if (not (symbolp last)) last)
494           next)))
495
496 (defun w3-history-forward ()
497   "Go forward in the history from this page."
498   (interactive)
499   (let ((next (cadr (w3-history-find-url-internal (url-view-url t))))
500         (w3-reuse-buffers 'yes))
501     (if next
502         (w3-fetch next))))
503
504 (defun w3-history-backward ()
505   "Go backward in the history from this page."
506   (interactive)
507   (let ((last (caar (w3-history-find-url-internal (url-view-url t))))
508         (w3-reuse-buffers 'yes))
509     (if last
510         (w3-fetch last))))
511
512 (defun w3-history-push (referer url)
513   "REFERER is the url we followed this link from.  URL is the link we got to."
514   (if (not referer)
515       (setq w3-history-stack (list (cons url (current-time))))
516     (let ((node (memq (assoc referer w3-history-stack) w3-history-stack)))
517       (if node
518           (setcdr node (list (cons url (current-time))))
519         (setq w3-history-stack (append w3-history-stack
520                                        (list
521                                         (cons url (current-time)))))))))
522
523 (defalias 'w3-add-urls-to-history 'w3-history-push)
524 (defalias 'w3-backward-in-history 'w3-history-backward)
525 (defalias 'w3-forward-in-history 'w3-history-forward)
526
527 \f
528 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
529 ;;; Miscellaneous functions
530 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
531 (defun w3-describe-entities ()
532   "Show an DTD fragment listing all the entities currently defined."
533   (interactive)
534   (switch-to-buffer (get-buffer-create "W3 Entities"))
535   (let ((buffer-file-name (concat (make-temp-name "entities") ".dtd")))
536     (set-auto-mode))
537   (erase-buffer)
538   (let (entity)
539     (mapatoms
540      (lambda (x)
541        (setq entity (get x 'html-entity-expansion))
542        (if entity
543            (insert (format "<!entity %s %s \"%s\">\n" x (car entity)
544                            (cdr entity)))))))
545   (goto-char (point-min)))
546
547 (defun w3-document-information (&optional buff)
548   "Display information on the document in buffer BUFF."
549   (interactive)
550   (if (interactive-p)
551       (let ((w3-notify 'friendly))
552         (if (get-buffer "Document Information")
553             (kill-buffer (get-buffer "Document Information")))
554         (w3-fetch "about:document"))
555     (setq buff (or buff (current-buffer)))
556     (save-excursion
557       (set-buffer buff)
558       (let* ((url (url-view-url t))
559              (cur-links w3-current-links)
560              (title (buffer-name))
561              (case-fold-search t)
562              (attributes (file-attributes url))
563              (lastmod (or (cdr-safe (assq 'last-modified
564                                           url-current-mime-headers))
565                           (nth 5 attributes)))
566              (hdrs url-current-mime-headers)
567              (size (cdr (assq 'content-length url-current-mime-headers)))
568              (info w3-current-metainfo)
569              (links w3-current-links))
570         (set-buffer (get-buffer-create url-working-buffer))
571         (setq url-current-can-be-cached nil)
572         (erase-buffer)
573         (if (consp lastmod)
574             (if (equal '(0 . 0) lastmod)
575                 (setq lastmod nil)
576               (setq lastmod (current-time-string lastmod))))
577         (setq url-current-mime-type "text/html")
578         (insert "\
579 Content-Type: text/html\n
580 <html>
581  <head>
582   <title>Document Information</title>
583  </head>
584  <body
585   <table border>
586    <tr><th colspan=2>Document Information</th></tr>
587    <tr><td>Title:</td><td>" title "</td></tr>
588    <tr><td>Location:</td><td>" url "</td></tr>")
589         (if size (insert "\
590    <tr><td>Size:</td><td>" (url-pretty-length (if (stringp size)
591                                                   (string-to-int size)
592                                                 size)) "</td></tr>"))
593         (insert "\
594    <tr><td>Last Modified:</td><td>" (or lastmod "None Given") "</td></tr>\n")
595         (when hdrs
596           (setq hdrs (delete (assq 'last-modified hdrs) hdrs))
597           (setq hdrs (delete (assq 'content-length hdrs) hdrs))
598           (setq hdrs (mapcar (lambda (pair)
599                                (cons (symbol-name (car pair))
600                                      (cdr pair)))
601                              hdrs)))
602         (let* ((maxlength (car (sort (mapcar (lambda (x)
603                                                (length (car x)))
604                                              hdrs)
605                                      '>)))
606                (fmtstring (format "\
607   <tr><td align=right>%%%ds:</td><td>%%s</td></tr>" maxlength)))
608           (insert
609            "\
610   <tr><th colspan=2>MetaInformation</th></tr>\n"
611            (mapconcat
612             (lambda (x)
613               (if (/= (length (car x)) 0)
614                   (format fmtstring
615                           (url-insert-entities-in-string
616                            (capitalize (car x)))
617                           (url-insert-entities-in-string
618                            (if (numberp (cdr x))
619                                (int-to-string (cdr x))
620                              (cdr x))))))
621             (sort hdrs
622                   (lambda (x y) (string-lessp (car x) (car y))))
623             "\n")))
624         (when links
625           ;; collapse `rel' and `rev' components
626           (setq links (apply 'append (mapcar 'cdr links)))
627           ;; extract
628           (setq links (mapcar
629                        (lambda (elt)
630                          (cons (or (plist-get (cadr elt) 'title)
631                                    (car elt))
632                                (plist-get (cadr elt) 'href)))
633                        links))
634           (let* ((maxlength (car (sort (mapcar (lambda (x)
635                                                  (length (car x)))
636                                                links)
637                                        '>)))
638                  (fmtstring
639                   (format
640                    "   <tr><td>%%%ds:</td><td><a href='%%s'>%%s</a></td></tr>"
641                    maxlength)))
642             (insert
643              "   <tr><th colspan=2>Document Links</th></tr>\n")
644             (while links
645               (if (and (caar links) (cdar links))
646                   (insert (format fmtstring
647                                   (url-insert-entities-in-string
648                                    (capitalize (caar links)))
649                                   (url-insert-entities-in-string
650                                    (cdar links))
651                                   (url-insert-entities-in-string
652                                    (cdar links))) "\n"))
653               (setq links (cdr links)))))
654         
655         (if info
656             (let* ((maxlength (car (sort (mapcar (lambda (x)
657                                                    (length (car x)))
658                                                  info)
659                                          '>)))
660                    (fmtstring (format
661                                "   <tr><td>%%%ds:</td><td>%%s</td></tr>"
662                                maxlength)))
663               (insert
664                "   <tr><th colspan=2>Miscellaneous Variables</th></tr>\n")
665               (while info
666                 (if (and (caar info) (cdar info))
667                     (insert (format fmtstring
668                                     (url-insert-entities-in-string
669                                      (capitalize (caar info)))
670                                     (url-insert-entities-in-string
671                                      (cdar info))) "\n"))
672                 (setq info (cdr info)))))
673         (insert "  </table></body></html>\n")
674         (current-buffer)))))
675
676 (defun w3-insert-formatted-url (p)
677   "Insert a formatted url into a buffer.
678 With prefix arg, insert the url under point."
679   (interactive "P")
680   (let (buff str)
681     (cond
682      (p
683       (setq p (widget-at (point)))
684       (or p (error "No url under point"))
685       (setq str (format "<a href=\"%s\">%s</a>" (widget-get p :href)
686                         (read-string "Link text: "
687                                      (buffer-substring
688                                       (widget-get p :from)
689                                       (widget-get p :to))))))
690      (t
691       (setq str (format "<a href=\"%s\">%s</a>" (url-view-url t)
692                         (read-string "Link text: " (buffer-name))))))
693     (setq buff (read-buffer "Insert into buffer: " nil t))
694     (if buff
695         (save-excursion
696           (set-buffer buff)
697           (insert str))
698       (message "Cancelled."))))
699
700 (defun w3-first-n-items (l n)
701   "Return the first N items from list L."
702   (let ((x 0)
703         y)
704     (if (> n (length l))
705         (setq y l)
706       (while (< x n)
707         (setq y (nconc y (list (nth x l)))
708               x (1+ x))))
709     y))
710
711 (defun w3-widget-button-press ()
712   (interactive)
713   (if (widget-at (point))
714       (widget-button-press (point))))
715
716 (defun w3-widget-button-click (e)
717   (interactive "@e")
718   (if (featurep 'xemacs)
719       (cond
720        ((and (event-point e)
721              (widget-at (event-point e)))
722         (widget-button-click e))
723        ((and (fboundp 'event-glyph)
724              (event-glyph e)
725              (glyph-property (event-glyph e) 'widget))
726         (widget-button-click e)))
727     (save-excursion
728       (mouse-set-point e)
729       (if (widget-at (point))
730           (widget-button-click e)))))
731    
732 ;;;###autoload
733 (defun w3-maybe-follow-link-mouse (e)
734   "Maybe follow a hypertext link under point.
735 If there is no link under point, this will try using
736 `url-get-url-at-point'"
737   (interactive "e")
738   (save-excursion
739     (mouse-set-point e)
740     (w3-maybe-follow-link)))
741
742 ;;;###autoload
743 (defun w3-maybe-follow-link ()
744   "Maybe follow a hypertext link under point.
745 If there is no link under point, this will try using
746 `url-get-url-at-point'"
747   (interactive)
748   (require 'w3)
749   (w3-do-setup)
750   (let* ((widget (widget-at (point)))
751          (url1 (and widget (widget-get widget :href)))
752          (url2 (url-get-url-at-point)))
753     (cond
754       (url1 (widget-button-press (point)))
755       ((and url2 (string-match url-nonrelative-link url2)) (w3-fetch url2))
756       (t (message "No URL could be found!")))))
757
758 ;;;###autoload
759 (defun w3-follow-url-at-point-other-frame (&optional pt)
760   "Follow the URL under PT, defaults to link under (point)."
761   (interactive "d")
762   (let ((url (url-get-url-at-point pt)))
763     (and url (w3-fetch-other-frame url))))
764
765 ;;;###autoload
766 (defun w3-follow-url-at-point (&optional pt)
767   "Follow the URL under PT, defaults to link under (point)."
768   (interactive "d")
769   (let ((url (url-get-url-at-point pt)))
770     (and url (w3-fetch url))))
771
772 (defun w3-fix-spaces (string)
773   "Remove spaces/tabs at beginning of STRING and convert newlines to spaces."
774   ;(url-convert-newlines-to-spaces
775    (url-strip-leading-spaces
776     (url-eat-trailing-space string)));)
777
778 (defun w3-source-document-at-point ()
779   "View source to the document pointed at by link under point."
780   (interactive)
781   (w3-source-document t))
782
783 (defun w3-source-document (under)
784   "View this document's source."
785   (interactive "P")
786   (let* ((url (if under (w3-view-this-url) (url-view-url t))))
787     (set-buffer (generate-new-buffer (concat "Source of: " url)))
788     (url-insert-file-contents url)
789     (put-text-property (point-min) (point-max) 'w3-base url)
790     (goto-char (point-min))
791     (setq buffer-file-truename url
792           buffer-file-name url)
793     ;; Null filename bugs `set-auto-mode' in Mule ...
794     (condition-case ()
795         (set-auto-mode)
796       (error nil))
797     (setq buffer-file-truename nil
798           buffer-file-name nil)
799     (buffer-enable-undo)
800     (set-buffer-modified-p nil)
801     (w3-notify-when-ready (current-buffer)))
802   (run-hooks 'w3-source-file-hook))
803
804 (defun w3-mail-document-under-point ()
805   "Mail the document pointed to by the hyperlink under point."
806   (interactive)
807   (w3-mail-current-document t))
808
809 (defun w3-mail-current-document (under &optional format)
810   "Mail the current-document to someone."
811   (interactive "P")
812   (let* ((completion-ignore-case t)
813          (format (or format
814                      (completing-read
815                       "Format: "
816                       '(("HTML Source")
817                         ("Formatted Text")
818                         ("PostScript")
819                         )
820                   nil t)))
821          (case-fold-search t)
822          (url (cond
823                ((stringp under) under)
824                (under (w3-view-this-url t))
825                (t (url-view-url t))))
826          (content-charset (or (and (boundp 'buffer-file-coding-system)
827                                    (symbol-value buffer-file-coding-system)
828                                    (symbol-name buffer-file-coding-system))
829                               "iso-8859-1"))
830          (content-type (concat "text/plain; charset=" content-charset))
831          (str
832           (save-excursion
833             (cond
834              ((and (equal "HTML Source" format) under)
835               (setq content-type (concat "text/html; charset="
836                                          content-charset))
837               (let ((url-source t))
838                 ;; Fixme: this needs a callback -- which?
839                 (url-retrieve url)))
840              ((equal "HTML Source" format)
841               (setq content-type (concat "text/html; charset="
842                                          content-charset))
843               (if w3-current-source
844                   (let ((x w3-current-source))
845                     (set-buffer (get-buffer-create url-working-buffer))
846                     (erase-buffer)
847                     (insert x))
848                 ;; Fixme: this needs a callback -- which?
849                 (url-retrieve url)))
850              ((and under (equal "PostScript" format))
851               (setq content-type "application/postscript")
852               (w3-fetch url)
853               (require 'ps-print)
854               (let ((ps-spool-buffer-name " *w3-temp*"))
855                 (if (get-buffer ps-spool-buffer-name)
856                     (kill-buffer ps-spool-buffer-name))
857                 (ps-spool-buffer-with-faces)
858                 (set-buffer ps-spool-buffer-name)))
859              ((equal "PostScript" format)
860               (require 'ps-print)
861               (let ((ps-spool-buffer-name " *w3-temp*"))
862                 (if (get-buffer ps-spool-buffer-name)
863                     (kill-buffer ps-spool-buffer-name))
864                 (setq content-type "application/postscript")
865                 (ps-spool-buffer-with-faces)
866                 (set-buffer ps-spool-buffer-name)))
867              ((and under (equal "Formatted Text" format))
868               (setq content-type (concat "text/plain; charset="
869                                          content-charset))
870               (w3-fetch url))
871              ((equal "Formatted Text" format)
872               (setq content-type (concat "text/plain; charset="
873                                          content-charset))))
874             (buffer-string))))
875     (funcall url-mail-command)
876     (mail-subject)
877     (if (and (boundp 'mime/editor-mode-flag) mime/editor-mode-flag)
878         (insert format " from <URL: " url ">")
879       (insert format " from <URL: " url ">\n"
880               "Mime-Version: 1.0\n"
881               "Content-transfer-encoding: 8bit\n"
882               "Content-type: " content-type))
883     (re-search-forward mail-header-separator nil)
884     (forward-char 1)
885     (if (and (boundp 'mime/editor-mode-flag) mime/editor-mode-flag)
886         (insert (format mime-tag-format content-type) "\n"))
887     (save-excursion
888       (insert str))
889     (cond ((equal "HTML Source" format)
890            (if (or (search-forward "<head>" nil t)
891                    (search-forward "<html>" nil t))
892                (insert "\n"))
893            (insert (format "<base href=\"%s\">" url))))
894     ;; Fixme: not defined.
895     (mail-to)))
896
897 (defun w3-internal-use-history (hist-item)
898   ;; Go to the link in the history
899   (let ((url (nth 0 hist-item))
900         (buf (nth 1 hist-item))
901         (pnt (nth 2 hist-item)))
902     (cond
903      ((null buf)                        ; Find a buffer with same url
904       (let ((x (buffer-list))
905             (found nil))
906         (while (and x (not found))
907           (save-excursion
908             (set-buffer (car x))
909             (setq found (string= (url-view-url t) url))
910             (if (not found) (setq x (cdr x)))))
911         (cond
912          (found
913           (switch-to-buffer (car x))
914           (if (number-or-marker-p pnt) (goto-char pnt)))
915          (t
916           (w3-fetch url)))))
917      ((buffer-name buf)                 ; Reuse the old buffer if possible
918       (switch-to-buffer buf)
919       (if (number-or-marker-p pnt) (goto-char pnt))
920       (if (and url (= ?# (string-to-char url))) ; Destination link
921           (progn
922             (goto-char (point-min))
923             (w3-find-specific-link (substring url 1 nil)))))
924      ;; Fixme: url-maybe-relative not defined.
925      (url (url-maybe-relative url))             ; Get the link
926      (t (message "Couldn't understand whats in the history.")))))
927
928 (defun w3-relative-link (url)
929   (if (equal "#" (substring url 0 1))
930       (progn
931         (push-mark (point) t)
932         (goto-char (point-min))
933         (w3-find-specific-link (substring url 1 nil)))
934     (w3-fetch (url-expand-file-name url))))
935
936 (defun w3-maybe-eval ()
937   "Maybe evaluate a buffer of Emacs Lisp code."
938   (if (funcall url-confirmation-func "This is emacs-lisp code, evaluate it?")
939       (eval-buffer (current-buffer))
940     (emacs-lisp-mode)))
941
942 (defun w3-use-links ()
943   "Select one of the <LINK> tags from this document and fetch it."
944   (interactive)
945   (and (not w3-current-links)
946        (error "No links defined for this document"))
947   (w3-fetch "about:document"))
948
949 (defun w3-find-this-file ()
950   "Do a `find-file' on the currently viewed html document.
951 Do this if it is a file: or ftp: reference"
952   (interactive)
953   (or url-current-object
954       (error "Not a URL-based buffer"))
955   (let ((type (url-type url-current-object)))
956     (cond
957      ((equal type "file")
958       (find-file (url-filename url-current-object)))
959      ((equal type "ftp")
960       (find-file
961        (format "/%s%s:%s"
962                (if (url-user url-current-object)
963                    (concat (url-user url-current-object) "@"))
964                (url-host url-current-object)
965                (url-filename url-current-object))))
966      (t (message "Sorry, I can't get that file so you can alter it.")))))
967
968 (defun w3-insert-this-url (pref-arg)
969   "Insert the current url in another buffer.
970 With prefix ARG, insert URL under point"
971   (interactive "P")
972   (let ((thebuf (get-buffer (read-buffer "Insert into buffer: ")))
973         (oldbuf (current-buffer))
974         (url (if pref-arg (w3-view-this-url t) (url-view-url t))))
975     (if (and url (not (equal "Not on a link!" url)))
976         (progn
977           (set-buffer thebuf)
978           (insert url)
979           (set-buffer oldbuf))
980       (message "Not on a link!"))))
981
982 (defun w3-in-assoc (elt list)
983   "Check to see if ELT matches any of the regexps in the car elements of LIST."
984   (let (rslt)
985     (while (and list (not rslt))
986       (and (car (car list))
987            (stringp (car (car list)))
988            (not (string= (car (car list)) ""))
989            (string-match (car (car list)) elt)
990            (setq rslt (car list)))
991       (setq list (cdr list)))
992     rslt))
993
994 (defun w3-goto-last-buffer ()
995   "Go to last WWW buffer visited."
996   (interactive)
997   (if w3-current-last-buffer
998       (if w3-frame-name
999           (progn
1000             (delete-other-windows)
1001             (set-buffer w3-current-last-buffer)
1002             (w3-goto-last-buffer))
1003         (w3-notify-when-ready w3-current-last-buffer))
1004     (message "No previous buffer found.")))
1005
1006 (fset 'w3-replace-regexp 'url-replace-regexp)
1007
1008 ;;;###autoload
1009 (defun w3-preview-this-buffer ()
1010   "See what this buffer will look like when its formatted as HTML.
1011 HTML is the HyperText Markup Language used by the World Wide Web to
1012 specify formatting for text.  More information on HTML can be found at
1013 ftp.w3.org:/pub/www/doc."
1014   (interactive)
1015   (w3-fetch (concat "www://preview/" (buffer-name))))
1016
1017 (defun w3-source ()
1018   "Show the source of a file."
1019   (let ((tmp (buffer-name (generate-new-buffer "Document Source"))))
1020     (set-buffer url-working-buffer)
1021     (kill-buffer tmp)
1022     (rename-buffer tmp)
1023     ;; Make the URL show in list-buffers output
1024     (make-local-variable 'list-buffers-directory)
1025     (setq list-buffers-directory (url-view-url t))
1026     (set-buffer-modified-p nil)
1027     (buffer-enable-undo)
1028     (w3-notify-when-ready (get-buffer tmp))))
1029
1030 (defvar w3-mime-list-for-code-conversion
1031   '("text/plain" "text/html")
1032   "List of MIME types that require Mules' code conversion.")
1033
1034 (defvar w3-compression-encodings
1035   '("x-gzip" "gzip" "x-compress" "compress")
1036   "List of MIME encodings that denote compression.")
1037
1038 ;; This looks bogus  -- fx
1039 (defvar w3-no-conversion-encodings
1040   w3-compression-encodings
1041   "List of MIME encodings that require Mule not to convert
1042 even though the MIME type is nil or listed in `w3-mime-list-for-code-conversion'.")
1043
1044 (defun w3-show-history-list ()
1045   "Format the url-history-list prettily and show it to the user."
1046   (interactive)
1047   (w3-fetch "www://auto/history"))
1048
1049 (defun w3-save-as (&optional type)
1050   "Save a document to the local disk."
1051   (interactive)
1052   (save-excursion
1053     (let* ((completion-ignore-case t)
1054            (format (or type (completing-read
1055                              "Format: "
1056                              '(("HTML Source")
1057                                ("Formatted Text")
1058                                ("PostScript")
1059                                ("Binary"))
1060                              nil t)))
1061            (fname (expand-file-name
1062                    (read-file-name "File name: " default-directory)))
1063            (source w3-current-source)
1064            (text (buffer-string))
1065            (url (url-view-url t)))
1066       (set-buffer (generate-new-buffer " *w3-save-as*"))
1067       (cond
1068        ((equal "Binary" format)
1069         (insert source))
1070        ((equal "HTML Source" format)
1071         (insert source)
1072         (goto-char (point-min))
1073         (if (re-search-forward "<head>" nil t)
1074             (insert "\n"))
1075         (insert (format "<BASE HREF=\"%s\">\n" url)))
1076        ((or (equal "Formatted Text" format)
1077             (equal "" format))
1078         (insert text))
1079        ((equal "PostScript" format)
1080         (require 'ps-print)
1081         (let ((ps-spool-buffer-name (buffer-name)))
1082           (ps-spool-buffer-with-faces))))
1083       (let ((coding-system-for-write 'binary))
1084         (write-region (point-min) (point-max) fname))
1085       (kill-buffer (current-buffer)))))
1086
1087 \f
1088 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1089 ;;; Functions for logging of bad HTML
1090 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1091 (defun w3-reconstruct-tag (tagname desc)
1092   (concat "<" tagname " "
1093           (mapconcat
1094            (function (lambda (x)
1095                        (if (cdr x)
1096                            (concat (car x) "=\"" (cdr x) "\"")
1097                          (car x)))) desc " ") ">"))
1098
1099 (defun w3-debug-if-found (regexp type desc)
1100   (and w3-debug-html
1101        (save-excursion
1102          (if (re-search-forward regexp nil t)
1103              (w3-log-bad-html type desc)))))
1104
1105 (defun w3-log-bad-html (type desc)
1106   "Log bad HTML to the buffer specified by w3-debug-buffer."
1107   (if w3-debug-html
1108       (save-excursion
1109         (set-buffer (get-buffer-create w3-debug-buffer))
1110         (goto-char (point-max))
1111         (insert (make-string (1- (window-width)) w3-horizontal-rule-char) "\n")
1112         (cond
1113          ((stringp type) (insert type "\n" desc "\n"))
1114          ((eq type 'bad-quote)
1115           (insert "Unterminated quoting character in SGML attribute value.\n"
1116                   desc "\n"))
1117          ((eq type 'no-quote)
1118           (insert "Unquoted SGML attribute value.\n" desc "\n"))
1119          ((eq type 'no-textarea-end)
1120           (insert "Unterminated <textarea> tag.\n"
1121                   (w3-reconstruct-tag "textarea" desc) "\n"))
1122          ((eq type 'bad-link-tag)
1123           (insert "Must specify either REL or REV with a <link> tag.\n"
1124                   (w3-reconstruct-tag "link" desc) "\n"))
1125          ((eq type 'no-a-end)
1126           (insert "Unterminated <a> tag.\n"
1127                   (w3-reconstruct-tag "a" desc) "\n"))
1128          ((eq type 'no-form-end)
1129           (insert "Unterminated <form> tag.\n"
1130                   (w3-reconstruct-tag "form" desc) "\n"))
1131          ((eq type 'bad-base-tag)
1132           (insert "Malformed <base> tag.\n"
1133                   (w3-reconstruct-tag "base" desc) "\n"))))))
1134
1135 \f
1136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1137 ;;; Functions to handle formatting an html buffer
1138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1139 (defun w3-add-delayed-graphic (widget)
1140   "Add a delayed image for the current buffer."
1141   (setq w3-delayed-images (cons widget w3-delayed-images)))
1142
1143 \f
1144 (defun w3-load-flavors ()
1145   "Load the correct emacsen specific stuff."
1146   (if (featurep 'xemacs)
1147       (require 'w3-xemac)
1148     (require 'w3-emacs))
1149   (if (featurep 'emacspeak)
1150       (condition-case ()
1151           (progn
1152             (require 'dtk-css-speech)
1153             (require 'w3-speak))))
1154   (condition-case ()
1155       (require 'w3-site-init)
1156     (error nil)))
1157
1158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1159 ;;; Automatic bug submission.                                               ;;;
1160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1161 (defun w3-submit-bug ()
1162   "Submit a bug on Emacs-w3."
1163   (interactive)
1164   (require 'reporter)
1165   (and (yes-or-no-p "Do you really want to submit a bug on Emacs-w3? ")
1166        (let ((url (url-view-url t))
1167              (vars '(window-system
1168                      window-system-version
1169                      system-type
1170                      ange-ftp-version
1171                      url-gateway-method
1172                      efs-version
1173                      ange-ftp-version
1174                      url-version
1175                      url)))
1176          (if (and url (string= url "file:nil")) (setq url nil))
1177          (mapcar
1178           (function
1179            (lambda (x)
1180              (if (not (and (boundp x) (symbol-value x)))
1181                  (setq vars (delq x vars))))) vars)
1182          (reporter-submit-bug-report w3-bug-address
1183                                      (concat "WWW v" w3-version-number " of "
1184                                              w3-version-date)
1185                                      vars
1186                                      nil nil
1187                                      "Description of Problem:"))))
1188
1189 (defalias 'w3-bug 'w3-submit-bug)
1190
1191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1192 ;;; Support for searching                                                   ;;;
1193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1194 (defun w3-nuke-spaces-in-search (x)
1195   "Remove spaces from search strings . . ."
1196   (let ((new ""))
1197     (while (not (equal x ""))
1198       (setq new (concat new (if (= (string-to-char x) 32) "+"
1199                               (substring x 0 1)))
1200             x (substring x 1 nil)))
1201     new))
1202
1203 (defun w3-search ()
1204   "Perform a search, if this is a searchable index."
1205   (interactive)
1206   (let* (querystring                    ; The string to send to the server
1207          (data
1208           (cond
1209            ((null w3-current-isindex)
1210             (let ((rels (cdr-safe (assq 'rel w3-current-links)))
1211                   val cur)
1212               (while rels
1213                 (setq cur (car rels)
1214                       rels (cdr rels))
1215                 (if (and (or (string-match "^isindex$" (car cur))
1216                              (string-match "^index$" (car cur)))
1217                          (plist-get (cadr cur) 'href))
1218                     (setq val (plist-get (cadr cur) 'href)
1219                           rels nil))
1220                 )
1221               (if val
1222                   (cons val "Search on (+ separates keywords): "))))
1223            ((eq w3-current-isindex t)
1224             (cons (url-view-url t) "Search on (+ separates keywords): "))
1225            ((consp w3-current-isindex)
1226             w3-current-isindex)
1227            (t nil)))
1228          index)
1229     (if (null data) (error "Not a searchable index!"))
1230     (setq index (car data))
1231     (setq querystring (w3-nuke-spaces-in-search (read-string (cdr data))))
1232     (if (string-match "\\(.*\\)\\?.*" index)
1233         (setq index (match-string 1 index)))
1234     (w3-fetch
1235      (concat index (if (= ?? (string-to-char (substring index -1 nil)))
1236                        "" "?") querystring))))
1237
1238 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1239 ;;; Auto documentation, etc                                                 ;;;
1240 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1241 (defun w3-help ()
1242   "Print documentation on w3 mode."
1243   (interactive)
1244   (w3-fetch "about:"))
1245
1246 ;;;###autoload
1247 (defun w3-version (&optional here)
1248   "Show the version number of W3 in the minibuffer.
1249 If optional argument HERE is non-nil, insert info at point."
1250   (interactive "P")
1251   (let ((version-string
1252          (format "WWW %s, URL %s"
1253                  w3-version-number
1254                  url-version)))
1255     (if here
1256         (insert version-string)
1257       (if (interactive-p)
1258           (message "%s" version-string)
1259         version-string))))
1260
1261 ;;;###autoload
1262 (defun w3 ()
1263   "Retrieve the default World Wide Web home page.
1264 The World Wide Web is a global hypertext system started by CERN in
1265 Switzerland in 1991.
1266
1267 The home page is specified by the variable `w3-default-homepage'.  The
1268 document should be specified by its fully specified Uniform Resource
1269 Locator.  The document will be parsed as HTML (if appropriate) and
1270 displayed in a new buffer."
1271   (interactive)
1272   (w3-do-setup)
1273   (if (and w3-track-last-buffer
1274            (bufferp w3-last-buffer)
1275            (buffer-name w3-last-buffer))
1276       (progn
1277         (switch-to-buffer w3-last-buffer)
1278         (message "Reusing buffer.  To reload, type %s."
1279                  (substitute-command-keys "\\[w3-reload-document]")))
1280     (cond
1281      ((null w3-default-homepage) (call-interactively 'w3-fetch))
1282      ((not (stringp w3-default-homepage))
1283       (error "Invalid setting for w3-default-homepage: %S"
1284              w3-default-homepage))
1285      ((not (string-match ".*:.*" w3-default-homepage))
1286       (w3-fetch (concat "file:" w3-default-homepage)))
1287      (t
1288       (w3-fetch w3-default-homepage)))))
1289
1290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1291 ;;; Stuff for good local file handling
1292 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1293 (defun w3-ff (file)
1294   "Find a file in any window already displaying it.
1295 Otherwise just as `display-buffer', and using this function."
1296   (if (not (eq 'tty (device-type)))
1297       (let ((f (window-frame (display-buffer (find-file-noselect file)))))
1298         (set-mouse-position f 1 0)
1299         (raise-frame f)
1300         (unfocus-frame))
1301     (display-buffer (find-file-noselect file))))
1302
1303 (defun w3-default-local-file()
1304   "Use find-file to open the local file"
1305   (w3-ff (url-filename url-current-object)))
1306
1307 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1308 ;;; Mode definition                                                         ;;;
1309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1310 (defun w3-search-forward (string)
1311   (interactive "sSearch: ")
1312   (setq w3-last-search-item string)
1313   (if (and (not (search-forward string nil t))
1314            (funcall url-confirmation-func
1315                     "End of document reached; continue from beginning? "))
1316       (progn
1317         (goto-char (point-min))
1318         (w3-search-forward string))))
1319
1320 (defun w3-search-again ()
1321   (interactive)
1322   (if (and w3-last-search-item
1323            (stringp w3-last-search-item))
1324       (if (and (not (search-forward w3-last-search-item nil t))
1325                (funcall url-confirmation-func
1326                         "End of document reached; continue from beginning? "))
1327           (progn
1328             (goto-char (point-min))
1329             (w3-search-again)))))
1330
1331 (defun w3-find-specific-link (link)
1332   (let ((pos (assq (intern link) w3-id-positions)))
1333     (if pos
1334         (progn
1335           (goto-char (cdr pos))
1336           (if (and (eolp) (not (eobp)))
1337               (forward-char 1)))
1338       (message "Link #%s not found." link))))
1339
1340 (defun w3-force-reload-document ()
1341   "Reload the current document.
1342 Take it from the network, even if cached and in local mode."
1343   (let ((url-standalone-mode nil))
1344     (w3-reload-document)))
1345
1346 (defun w3-reload-document (&optional explicit-coding-system)
1347   "Reload the current document.
1348 With prefix argument, it reads a coding system to decode the document."
1349   (interactive
1350    (list (if (and (featurep 'mule) current-prefix-arg)
1351              (read-coding-system "Coding system: "))))
1352   (let ((tmp (url-view-url t))
1353         (pnt (point))
1354         (window-start (progn
1355                         (move-to-window-line 0)
1356                         (point)))
1357         (url-request-extra-headers '(("Pragma" . "no-cache")))
1358         (w3-explicit-coding-system explicit-coding-system))
1359     (kill-buffer (current-buffer))
1360     (w3-fetch tmp)
1361     (if explicit-coding-system
1362         (w3-record-explicit-coding-system tmp explicit-coding-system))
1363     (goto-char pnt)
1364     (set-window-start (selected-window) (min window-start (point-max)))))
1365
1366 (defun w3-recall-explicit-coding-system (url)
1367   "Find user-specified explicit coding system for this URL.
1368 Look for it in `w3-explicit-conversion-tree'"
1369   (let* ((urlobj (if (stringp url)
1370                      (url-generic-parse-url url)
1371                    url))
1372          (hostname (or (url-host urlobj) "localhost"))
1373          (fname-list (split-string (url-filename urlobj) "\\/")))
1374     ;; now recurse
1375     (w3-find-explicit-coding-system (cons hostname fname-list)
1376                                     w3-explicit-conversion-tree)))
1377
1378 (defun w3-find-explicit-coding-system (fname-list tree)
1379   "Recall a user-specified explicit coding system."
1380   (let ((branch (assoc (car fname-list) tree)))
1381     (and branch
1382          (or (and (cdr fname-list) (cddr branch)
1383                   (w3-find-explicit-coding-system (cdr fname-list)
1384                                                   (cddr branch)))
1385              (cadr branch)))))
1386
1387 (defun w3-record-explicit-coding-system (url coding-system)
1388   "Record user-specified explicit coding system for URLs
1389 as high as possible in w3-explicit-conversion-tree"
1390   (let* ((urlobj (if (stringp url)
1391                      (url-generic-parse-url url)
1392                    url))
1393          (hostname (or (url-host urlobj) "localhost"))
1394          (fname-list (split-string (url-filename urlobj) "\\/"))
1395          (tree (or (assoc hostname w3-explicit-conversion-tree)
1396                    (let ((branch (list hostname)))
1397                      (setq w3-explicit-conversion-tree
1398                            (cons branch w3-explicit-conversion-tree))
1399                      branch))))
1400     ;; now recurse
1401     (w3-add-explicit-coding-system fname-list coding-system tree)
1402     (setq w3-explicit-encodings-changed-since-last-save t)))
1403
1404 (defun w3-add-explicit-coding-system (fname-list coding-system tree)
1405   "Memorize a user-specified explicit coding system."
1406   (if (and (cadr tree) (not (equal (cadr tree) coding-system)))
1407       (setcar (cdr tree) nil))
1408   (let ((branch (assoc (car fname-list) (cddr tree))))
1409     (cond (branch
1410            ;; update existing branch
1411            (cond ((cdr fname-list)
1412                   (or (equal (cadr branch) coding-system)
1413                       (null (cadr branch))
1414                       (setcar (cdr branch) nil))
1415                   (w3-add-explicit-coding-system (cdr fname-list)
1416                                                  coding-system branch))
1417                  (t
1418                   (setcar (cdr branch) coding-system))))
1419           (t
1420            ;; create a new branch
1421            (setcdr tree
1422                    (if fname-list
1423                        (let ((subbranch (list (car fname-list))))
1424                          (w3-add-explicit-coding-system
1425                           (cdr fname-list) coding-system subbranch)
1426                          (cons (if (or (null (cddr tree))
1427                                        (equal coding-system (cadr tree)))
1428                                    coding-system)
1429                                (cons subbranch (cddr tree))))
1430                      (list coding-system)))))))
1431
1432 (defun w3-write-explicit-encodings (&optional fname)
1433   "Write the explicit encodings file into `w3-explicit-encodings-file'."
1434   (interactive)
1435   (or fname
1436       (and w3-explicit-encodings-file
1437            (setq fname (expand-file-name w3-explicit-encodings-file))))
1438   (cond
1439    ((not w3-explicit-encodings-changed-since-last-save) nil)
1440    ((not (file-writable-p fname))
1441     (message "Explicit encodings file %s (see variable `w3-explicit-encodings-file') is unwritable." fname))
1442    (t
1443     (let ((make-backup-files nil)
1444           (version-control nil)
1445           (require-final-newline t))
1446       (save-excursion
1447         (set-buffer (get-buffer-create " *w3-tmp*"))
1448         (erase-buffer)
1449         (insert "(setq w3-explicit-conversion-tree\n      '"
1450                 (prin1-to-string w3-explicit-conversion-tree)
1451                 ")\n\n")
1452         (write-file fname)
1453         (kill-buffer (current-buffer))))))
1454   (setq w3-explicit-encodings-changed-since-last-save nil))
1455
1456 (defun w3-leave-buffer ()
1457   "Bury this buffer, but don't kill it."
1458   (interactive)
1459   (let ((x w3-current-last-buffer))
1460     (if w3-frame-name
1461         (w3-leave-or-quit-frameset x nil)
1462       (progn
1463         (bury-buffer nil)
1464         (if (and (bufferp x) (buffer-name x))
1465             (w3-notify-when-ready x))))))
1466
1467 (defun w3-quit (&optional mega)
1468   "Quit WWW mode."
1469   (interactive "P")
1470   (if mega
1471       (mapcar
1472        (lambda (x)
1473          (save-excursion
1474            (set-buffer (get-buffer x))
1475            (if (eq major-mode 'w3-mode)
1476                (w3-quit nil))))
1477        (buffer-list))
1478     (let ((x w3-current-last-buffer))
1479       (if w3-frame-name
1480           (w3-leave-or-quit-frameset x t)
1481         (progn
1482           (kill-buffer (current-buffer))
1483           (if (and (bufferp x) (buffer-name x))
1484               (w3-notify-when-ready x)))))))
1485
1486 (defun w3-leave-or-quit-frameset (x quit-p &optional top-down-p)
1487   (set-buffer x)
1488   (delete-other-windows)
1489   (let ((structure (reverse w3-frameset-structure)))
1490     (while structure
1491       (let ((elt (car structure)))
1492         (if (eq (car elt) 'frame)
1493             (let* ((url (nth 2 elt))
1494                    (buf (w3-buffer-visiting url)))
1495               (if buf
1496                   (progn
1497                     (set-buffer buf)
1498                     (if w3-frameset-structure
1499                         (w3-leave-or-quit-frameset buf quit-p t)
1500                       (if quit-p
1501                           (kill-buffer buf)
1502                         (bury-buffer buf))))))))
1503       (pop structure)))
1504   (if top-down-p
1505       (if quit-p
1506           (kill-buffer x)
1507         (bury-buffer x))
1508     (progn
1509       (set-buffer x)
1510       (if quit-p
1511           (w3-quit nil)
1512         (w3-leave-buffer)))))
1513
1514 (defun w3-view-this-url (&optional no-show)
1515   "View the URL of the link under point."
1516   (interactive)
1517   (let* ((widget (widget-at (point)))
1518          (parent (and widget (widget-get widget :parent)))
1519          (href (or (and widget (widget-get widget :href))
1520                    (and parent (widget-get parent :href)))))
1521     (cond
1522      ((and no-show href)
1523       href)
1524      (href
1525       (message "%s" (url-truncate-url-for-viewing href)))
1526      (no-show
1527       nil)
1528      (widget
1529       (widget-echo-help (point)))
1530      (t
1531       nil))))
1532
1533 (defun w3-load-delayed-images ()
1534     "Load inlined images that were delayed, if any."
1535   (interactive)
1536   (let ((w3-delay-image-loads nil)
1537         (todo w3-delayed-images))
1538     (setq w3-delayed-images nil)
1539     (while todo
1540       (w3-maybe-start-image-download (car todo))
1541       (setq todo (cdr todo)))))
1542
1543 (defun w3-save-this-url ()
1544   "Save url under point in the kill ring."
1545   (interactive)
1546   (w3-save-url t))
1547
1548 (defun w3-save-url (under-pt)
1549   "Save current url in the kill ring."
1550   (interactive "P")
1551   (let ((x (cond
1552             ((stringp under-pt) under-pt)
1553             (under-pt (w3-view-this-url t))
1554             (t (url-view-url t)))))
1555     (if x
1556         (progn
1557           (kill-new x)
1558           (message "Stored URL in kill-ring."))
1559       (error "No URL to store"))))
1560
1561 (fset 'w3-end-of-document 'end-of-buffer)
1562 (fset 'w3-start-of-document 'beginning-of-buffer)
1563
1564 (defun w3-scroll-up (&optional lines)
1565   "Scroll forward in View mode, or exit if end of text is visible.
1566 No arg means whole window full.  Arg is number of lines to scroll."
1567   (interactive "P")
1568   (if (and (pos-visible-in-window-p (point-max))
1569            ;; Allow scrolling backward at the end of the buffer.
1570            (or (null lines)
1571                (> lines 0)))
1572       nil
1573     (let ((view-lines (1- (window-height))))
1574       (setq lines
1575             (if lines (prefix-numeric-value lines)
1576               view-lines))
1577       (if (>= lines view-lines)
1578           (scroll-up nil)
1579         (if (>= (- lines) view-lines)
1580             (scroll-down nil)
1581           (scroll-up lines)))
1582       (cond ((pos-visible-in-window-p (point-max))
1583              (goto-char (point-max))
1584              (recenter -1)))
1585       (move-to-window-line -1)
1586       (beginning-of-line))))
1587
1588
1589 (defun w3-mail-document-author ()
1590   "Send mail to the author of this document, if possible."
1591   (interactive)
1592   (let ((x w3-current-links)
1593         (y nil)
1594         (found nil))
1595     (setq found (cdr-safe (assoc "reply-to" url-current-mime-headers)))
1596     (if (and found (not (string-match url-nonrelative-link found)))
1597         (setq found (list (list 'href (concat "mailto:" found)))))
1598     (while (and x (not found))
1599       (setq y (car x)
1600             x (cdr x)
1601             found (cdr-safe (assoc "made" y))))
1602     (if found
1603         (let ((possible nil)
1604               (href nil))
1605           (setq x (car found))          ; Fallback if no mail(to|server) found
1606           (while found
1607             (setq href (plist-get (pop found) 'href))
1608             (if (and href (string-match "^mail[^:]+:" href))
1609                 (setq possible (cons href possible))))
1610           (case (length possible)
1611             (0                          ; No mailto links found
1612              (w3-fetch href))           ; fall back onto first 'made' link
1613             (1                          ; Only one found, get it
1614              (w3-fetch (car possible)))
1615             (otherwise
1616              (w3-fetch (completing-read "Choose an address: "
1617                                         (mapcar 'list possible)
1618                                         nil t (car possible))))))
1619       (message "Could not automatically determine authors address, sorry."))))
1620
1621 (defun w3-kill-emacs-func ()
1622   "Routine called when exiting Emacs.  Do miscellaneous clean up."
1623   (url-history-save-history)
1624   (message "Cleaning up w3 temporary files...")
1625   ;; FIXME!  This needs to be in the URL library now I guess?
1626   '(let ((x (nconc
1627             (and (file-exists-p w3-temporary-directory)
1628                  (directory-files w3-temporary-directory t "url-tmp.*"))
1629             (and (file-exists-p url-temporary-directory)
1630                  (directory-files url-temporary-directory t
1631                                   (concat "url"
1632                                           (int-to-string
1633                                            (user-real-uid)) ".*")))
1634             (and (file-exists-p url-temporary-directory)
1635                  (directory-files url-temporary-directory t "url-tmp.*")))))
1636     (while x
1637       (condition-case ()
1638           (delete-file (car x))
1639         (error nil))
1640       (setq x (cdr x))))
1641   (message "Cleaning up w3 temporary files... done."))
1642
1643 (eval-and-compile
1644   (cond
1645    ((fboundp 'display-warning)
1646     (fset 'w3-warn 'display-warning))
1647    ((fboundp 'warn)
1648     (defun w3-warn (class message &optional level)
1649       (if (and (eq class 'html)
1650                (not w3-debug-html))
1651           nil
1652         (warn "(%s/%s) %s" class (or level 'warning) message))))
1653    (t
1654     (defun w3-warn (class message &optional level)
1655       (if (and (eq class 'html)
1656                (not w3-debug-html))
1657           nil
1658         (save-excursion
1659           (set-buffer (get-buffer-create "*W3-WARNINGS*"))
1660           (goto-char (point-max))
1661           (save-excursion
1662             (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
1663           (display-buffer (current-buffer))))))))
1664
1665 (defun w3-map-links (function &optional buffer from to maparg)
1666   "Map FUNCTION over the hypertext links which overlap region in BUFFER,
1667 starting at FROM and ending at TO.  FUNCTION is called with the arguments
1668 WIDGET and MAPARG.
1669 The arguments FROM, TO, MAPARG, and BUFFER default to the beginning of
1670 BUFFER, the end of BUFFER, nil, and (current-buffer), respectively."
1671   (let ((parent)
1672         (highly-unlikely-name-for-a-variable-holding-a-function function))
1673     (widget-map-buttons
1674      (lambda (widget arg)
1675        (setq parent (and widget (widget-get widget :parent)))
1676        ;; Check to see if its got a URL tacked on it somewhere
1677        (cond
1678         ((and widget (widget-get widget :href))
1679          (funcall highly-unlikely-name-for-a-variable-holding-a-function
1680                   widget maparg))
1681         ((and parent (widget-get parent :href))
1682          (funcall highly-unlikely-name-for-a-variable-holding-a-function
1683                   widget maparg))
1684         (t nil))
1685        nil))))
1686
1687 (defun w3-refresh-stylesheets ()
1688   "Reload all stylesheets."
1689   (interactive)
1690   (setq w3-user-stylesheet nil
1691         w3-face-cache nil)
1692   (w3-find-default-stylesheets)
1693   )
1694
1695 (defvar w3-loaded-stylesheets nil
1696   "A list of all the stylesheets Emacs-W3 loaded at startup.")
1697
1698 (defun w3-find-default-stylesheets ()
1699   (setq w3-loaded-stylesheets nil)
1700   (let* ((lightp (css-color-light-p 'default))
1701          (longname (if lightp "stylesheet-light" "stylesheet-dark"))
1702          (shortname (if lightp "light.css" "dark.css"))
1703          (w3-lisp (file-name-directory (locate-library "w3")))
1704          (w3-root (expand-file-name "../.." w3-lisp))
1705          (no-user-init (= 0 (length user-init-file)))
1706          (w3-configuration-directory (if no-user-init
1707                                          "/this/is/a/highly/unlikely/directory/name"
1708                                        w3-configuration-directory))
1709          (directories (list
1710                        (if (fboundp 'locate-data-directory)
1711                            (locate-data-directory "w3"))
1712                        data-directory
1713                        (concat data-directory "w3/")
1714                        (expand-file-name "../../w3" data-directory)
1715                        w3-lisp
1716                        w3-root
1717                        (w3-configuration-data 'datadir)
1718                        (expand-file-name "w3" w3-root)
1719                        (expand-file-name "etc" w3-root)
1720                        (expand-file-name "etc/w3" w3-root)
1721                        (expand-file-name "../" w3-lisp)
1722                        (expand-file-name "../w3" w3-lisp)
1723                        (expand-file-name "../etc" w3-lisp)
1724                        w3-configuration-directory))
1725          (total-found 0)
1726          (possible (append
1727                     (apply
1728                      'append
1729                      (mapcar
1730                       (function
1731                        (lambda (dir)
1732                          (list
1733                           (expand-file-name shortname dir)
1734                           (expand-file-name longname dir)
1735                           (expand-file-name "stylesheet" dir)
1736                           (expand-file-name "default.css" dir))))
1737                       directories))
1738                     (and (not no-user-init)
1739                          (list w3-default-stylesheet))))
1740          (remember possible)
1741          (found nil)
1742          (cur nil))
1743     (while possible
1744       (setq cur (car possible)
1745             possible (cdr possible)
1746             found (and cur (file-exists-p cur) (file-readable-p cur)
1747                        (not (file-directory-p cur)) cur))
1748       (if found
1749           (setq total-found (1+ total-found)
1750                 w3-loaded-stylesheets (cons cur w3-loaded-stylesheets)
1751                 w3-user-stylesheet (css-parse (concat "file:" cur) nil
1752                                               w3-user-stylesheet))))
1753     (if (= 0 total-found)
1754         (progn
1755           (w3-warn
1756            'style
1757            (concat
1758             "No stylesheets found!  Check configuration! DANGER DANGER!\n"
1759             "Emacs-W3 checked for its stylesheet in the following places\n"
1760             "and did not find one.  This means that some formatting will\n"
1761             "be wrong, and most colors and fonts will not be set up correctly.\n"
1762             "------\n"
1763             (mapconcat 'identity remember "\n")
1764             "------"))
1765           (error "No stylesheets found!  Check configuration! DANGER DANGER!")))))
1766
1767 (defvar w3-widget-global-map nil)
1768
1769 ;;;###autoload
1770 (defun w3-do-setup ()
1771   "Do setup.
1772 This is to avoid conflict with user settings when W3 is dumped with
1773 Emacs."
1774   (unless w3-setup-done
1775     (url-do-setup)
1776     (w3-load-flavors)
1777     (w3-setup-version-specifics)
1778     (setq w3-default-configuration-file (expand-file-name
1779                                          (or w3-default-configuration-file
1780                                              "profile")
1781                                          w3-configuration-directory))
1782     (if (and init-file-user
1783              w3-default-configuration-file
1784              (file-exists-p w3-default-configuration-file))
1785         (condition-case e
1786             (load w3-default-configuration-file nil t)
1787           (error
1788            (let ((buf-name " *Configuration Error*"))
1789              (if (get-buffer buf-name)
1790                  (kill-buffer (get-buffer buf-name)))
1791              (display-error e (get-buffer-create buf-name))
1792              (save-excursion
1793                (switch-to-buffer-other-window buf-name)
1794                (shrink-window-if-larger-than-buffer))
1795              (w3-warn 'configuration
1796                       (format (eval-when-compile
1797                                 (concat
1798                                  "Configuration file `%s' contains an error.\n"
1799                                  "Please consult the `%s' buffer for details."))
1800                               w3-default-configuration-file buf-name))))))
1801                
1802     ;; Load the explicit encodings file if it exists
1803     (if (and w3-explicit-encodings-file
1804              (file-exists-p w3-explicit-encodings-file))
1805         (condition-case nil
1806             (load w3-explicit-encodings-file nil t)
1807           (error nil)))
1808
1809     (if (and (eq w3-user-colors-take-precedence 'guess)
1810              (not (eq (device-type) 'tty))
1811              (not (eq (device-class) 'mono)))
1812         (progn
1813           (setq w3-user-colors-take-precedence t)
1814           (w3-warn
1815            'html
1816            "Disabled document color specification because of mono display.")))
1817
1818     (w3-refresh-stylesheets)
1819     (setq w3-setup-done t)
1820
1821     (add-minor-mode 'w3-netscape-emulation-minor-mode " NS"
1822                     w3-netscape-emulation-minor-mode-map)
1823     (add-minor-mode 'w3-lynx-emulation-minor-mode " Lynx"
1824                     w3-lynx-emulation-minor-mode-map)
1825   
1826     (setq url-package-version w3-version-number
1827           url-package-name "Emacs-W3")
1828
1829     (w3-setup-terminal-chars)
1830
1831     (cond
1832      ((memq system-type '(ms-dos ms-windows))
1833       (setq w3-hotlist-file (or w3-hotlist-file
1834                                 (expand-file-name "~/mosaic.hot"))
1835             ))
1836      ((memq system-type '(axp-vms vax-vms))
1837       (setq w3-hotlist-file (or w3-hotlist-file
1838                                 (expand-file-name "~/mosaic.hotlist-default"))
1839             ))
1840      (t
1841       (setq w3-hotlist-file (or w3-hotlist-file
1842                                 (expand-file-name "~/.mosaic-hotlist-default"))
1843             )))
1844   
1845     ;; Set up a hook that will save the history list when exiting
1846     ;; emacs
1847     (add-hook 'kill-emacs-hook 'w3-kill-emacs-func)
1848
1849     ;; Load in the hotlist if they haven't set it already
1850     (or w3-hotlist (w3-parse-hotlist))
1851
1852     ;; Set the default home page, honoring their defaults, then the
1853     ;; standard WWW_HOME, then default to the documentation @ gnu.org
1854     (or w3-default-homepage
1855         (setq w3-default-homepage
1856               (or (getenv "WWW_HOME")
1857                   "http://www.gnu.org/software/w3/")))
1858
1859     (run-hooks 'w3-load-hook)))
1860
1861 (defun w3-mark-link-as-followed (ext dat)
1862   "Mark a link as followed."
1863   (message "Reimplement w3-mark-link-as-followed"))
1864
1865 (defun w3-only-links ()
1866   (let* (result temp)
1867     (w3-map-links (lambda (x y)
1868                     (setq result (cons x result))))
1869     result))
1870
1871 (defun w3-download-redirect-callback (&rest args)
1872   (let (redirect-url errorp)
1873     ;; Handle both styles of `url-retrieve' callbacks...
1874     (cond
1875      ((listp (car args))
1876       ;; Emacs 22 style.  First argument is a list.
1877       (let ((status (car args)))
1878         (when (eq (car status) :error)
1879           (setq errorp t)
1880           (setq status (cddr args)))
1881         (when (eq (car status) :redirect)
1882           (setq redirect-url (second (car args))))
1883
1884         (setq args (cdr args))))
1885
1886      ((eq (car args) :redirect)
1887       ;; Pre-22 redirect.
1888       (setq redirect-url (cadr args))
1889       (while (eq (car args) :redirect)
1890         (setq args (cddr args)))))
1891
1892     (if errorp
1893         (message "Download of %s failed." (url-view-url t))
1894       (w3-download-callback (car args)))))
1895
1896 (defun w3-download-callback (fname)
1897   (let ((coding-system-for-write 'binary))
1898     (goto-char (point-min))
1899     (search-forward "\n\n" nil t)
1900     (write-region (point) (point-max) fname))
1901   (url-mark-buffer-as-dead (current-buffer))
1902   (message "Download of %s complete." (url-view-url t))
1903   (sit-for 3))
1904
1905 (defun w3-download-url-at-point ()
1906   "Download the URL under point."
1907   (interactive)
1908   (w3-download-url-wrapper t))
1909
1910 (defun w3-download-this-url ()
1911   "Download the current URL."
1912   (interactive)
1913   (w3-download-url-wrapper nil))
1914   
1915 (defun w3-download-url-wrapper (under-pt)
1916   "Download current URL."
1917   (let ((x (if under-pt (w3-view-this-url t) (url-view-url t))))
1918     (if x
1919         (w3-download-url x)
1920       (error "No link found"))))
1921              
1922 (defun w3-download-url (url &optional file-name)
1923   (interactive (list (w3-read-url-with-default)))
1924   (let* ((url-mime-accept-string "*/*")
1925          (urlobj (url-generic-parse-url url))
1926          (stub-fname (w3-url-file-nondirectory (or (url-filename urlobj) "")))
1927          (dir (or mailcap-download-directory "~/"))
1928          (fname (or file-name
1929                     (expand-file-name
1930                      (read-file-name "Filename to save as: "
1931                                      dir
1932                                      stub-fname
1933                                      nil
1934                                      stub-fname) dir))))
1935     (url-retrieve url 'w3-download-redirect-callback (list fname))))
1936
1937 ;;;###autoload
1938 (defun w3-follow-link-other-frame (&optional p)
1939   "Attempt to follow the hypertext reference under point in a new frame.
1940 With prefix-arg P, ignore viewers and dump the link straight
1941 to disk."
1942   (cond
1943    ((and (fboundp 'make-frame)
1944          (fboundp 'select-frame))
1945     (let ((frm (make-frame)))
1946       (select-frame frm)
1947       (w3-follow-link p)))
1948    (t (w3-follow-link p))))
1949
1950 ;;;###autoload
1951 (defun w3-follow-link (&optional p)
1952   "Attempt to follow the hypertext reference under point.
1953 With prefix-arg P, ignore viewers and dump the link straight
1954 to disk."
1955   (interactive "P")
1956   (let* ((widget (widget-at (point)))
1957          (href (and widget (widget-get widget :href))))
1958     (cond
1959      ((null href) nil)
1960      (p
1961       (w3-download-url href))
1962      (t
1963       (w3-fetch href)))))
1964
1965 ;;;###autoload
1966 (defun w3-next-document ()
1967   (interactive)
1968   (let ((link (or (let ((rel (assq 'rel w3-current-links)))
1969                     (and rel (assoc "next" rel)))
1970                   (let ((rev (assq 'rev w3-current-links)))
1971                     (and rev (or (assoc "previous" rev)
1972                                  (assoc "prev" rev))))))
1973         href)
1974     (and link (setq link (cdr link)))
1975     (while (and link (null href))
1976       (setq href (plist-get (car link) 'href))
1977       (setq link (cdr link)))
1978     (if href
1979         (w3-fetch href)
1980       (error "No NEXT document"))))
1981
1982 ;;;###autoload
1983 (defun w3-prev-document ()
1984   (interactive)
1985   (let ((link (or (let ((rel (assq 'rel w3-current-links)))
1986                     (and rel (or (assoc "previous" rel)
1987                                  (assoc "prev" rel))))
1988                   (let ((rev (assq 'rev w3-current-links)))
1989                     (and rev (assoc "next" rev)))))
1990         href)
1991     (and link (setq link (cdr link)))
1992     (while (and link (null href))
1993       (setq href (plist-get (car link) 'href))
1994       (setq link (cdr link)))
1995     (if href
1996         (w3-fetch href)
1997       (error "No PREVIOUS document"))))
1998
1999 ;; Why are these defined?
2000 (defun w3-widget-forward (arg)
2001   "Move point to the next field or button.
2002 With optional ARG, move across that many fields."
2003   (interactive "p")
2004   (widget-forward arg))
2005
2006 (defun w3-widget-backward (arg)
2007   "Move point to the previous field or button.
2008 With optional ARG, move across that many fields."
2009   (interactive "p")
2010   (w3-widget-forward (- arg)))
2011
2012 (defun w3-complete-link ()
2013   "Choose a link from the current buffer and follow it."
2014   (interactive)
2015   (let (links-alist
2016         link-at-point
2017         choice
2018         (completion-ignore-case t))
2019     (setq link-at-point (widget-at (point))
2020           link-at-point (and
2021                          link-at-point
2022                          (widget-get link-at-point :href)
2023                          (widget-get link-at-point :from)
2024                          (widget-get link-at-point :to)
2025                          (w3-fix-spaces
2026                           (buffer-substring-no-properties
2027                            (widget-get link-at-point :from)
2028                            (widget-get link-at-point :to)))))
2029     (w3-map-links (lambda (widget arg)
2030                     (if (and (widget-get widget :from)
2031                              (widget-get widget :to))
2032                         (setq links-alist (cons
2033                                            (cons
2034                                             (w3-fix-spaces
2035                                              (buffer-substring-no-properties
2036                                               (widget-get widget :from)
2037                                               (widget-get widget :to)))
2038                                             (widget-get widget :href))
2039                                            links-alist)))))
2040     (if (not links-alist) (error "No links in current document"))
2041     (setq links-alist (sort links-alist (lambda (x y)
2042                                           (string< (car x) (car y)))))
2043     ;; Destructively remove duplicate entries from links-alist.
2044     (let ((remaining-links links-alist))
2045       (while remaining-links
2046         (if (equal (car remaining-links) (car (cdr remaining-links)))
2047             (setcdr remaining-links (cdr (cdr remaining-links)))
2048           (setq remaining-links (cdr remaining-links)))))
2049     (setq choice (completing-read
2050                   (if link-at-point
2051                       (concat "Link (default "
2052                               (if (< (length link-at-point) 20)
2053                                   link-at-point
2054                                 (concat
2055                                  (substring link-at-point 0 17) "..."))
2056                               "): ")
2057                     "Link: ") links-alist nil t))
2058     (if (and (string= choice "") link-at-point)
2059         (setq choice link-at-point))
2060     (let ((match (try-completion choice links-alist)))
2061       (cond
2062        ((eq t match)                    ; We have an exact match
2063         (setq choice (cdr (assoc choice links-alist))))
2064        ((stringp match)
2065         (setq choice (cdr (assoc match links-alist))))
2066        (t (setq choice nil)))
2067       (if choice
2068           (w3-fetch choice)))))
2069
2070 (defun w3-display-errors ()
2071   "Display any HTML errors for the current page."
2072   (interactive)
2073   (let ((w3-notify 'friendly)
2074         (inhibit-read-only t)
2075         (buffer nil)
2076         (todo w3-current-badhtml)
2077         (url (url-view-url t)))
2078     (if (not todo)
2079         (error "No HTML errors on this page!  Amazing, isn't it?"))
2080     (save-excursion
2081       (set-buffer
2082        (get-buffer-create (concat "HTML Errors for: " (or url "???"))))
2083       (setq buffer (current-buffer))
2084       (erase-buffer)
2085       (while todo
2086         (goto-char (point-min))
2087         (insert "\n" (car todo))
2088         (setq todo (cdr todo)))
2089       (when url
2090         (goto-char (point-min))
2091         (insert (format "HTML Errors for: <URL:%s>\n" url)))
2092       (set (make-local-variable 'font-lock-keywords)
2093            w3-html-errors-font-lock-keywords)
2094       (set (make-local-variable 'font-lock-keywords-only) nil)
2095       (set (make-local-variable 'font-lock-keywords-case-fold-search) nil)
2096       (set (make-local-variable 'font-lock-syntax-table) nil)
2097       (set (make-local-variable 'font-lock-beginning-of-syntax-function)
2098            'beginning-of-line)
2099       (run-hooks 'w3-display-errors-hook))
2100     (w3-notify-when-ready buffer)))
2101
2102 (defun w3-mode ()
2103   "Mode for viewing HTML documents.
2104 If called interactively, will display the current buffer as HTML.
2105
2106 Current keymap is:
2107 \\{w3-mode-map}"
2108   (interactive)
2109   (w3-do-setup)
2110   (if (interactive-p)
2111       (w3-preview-this-buffer)
2112     ;; This code used to keep a few buffer-local variables around so
2113     ;; that we could be a nice major mode and kill all the local
2114     ;; variables like we are supposed to, but still save some of them
2115     ;; that were set up in the URL and parsing libraries.
2116     ;;
2117     ;: Two problems with this approach:
2118     ;;
2119     ;; 1) It kills buffer-local faces in XEmacs, which we use extensively.
2120     ;; 2) With Emacspeak it causes all the personality properties on
2121     ;;    the text to mysteriously disappear.
2122     ;;
2123     ;; So screw it... I'm leaving the code in here commented out so
2124     ;; that I don't forget and try to 'fix' this later in life.
2125     ;;
2126     ;; - Bill Perry Nov 21, 2001
2127     ;; (let ((tmp (mapcar (lambda (x) (cons x (and (boundp x) (symbol-value x))))
2128     ;;                    w3-persistent-variables)))
2129     ;;    (kill-all-local-variables))
2130     ;;    (mapcar (lambda (x) (if (boundp (car x))
2131     ;;                            (set-variable (car x) (cdr x)))) tmp))
2132     (use-local-map w3-mode-map)
2133     (setq mode-name "WWW")
2134     (setq major-mode 'w3-mode)
2135     (w3-mode-version-specifics)
2136     (w3-menu-install-menus)
2137     (setq truncate-lines t
2138           mode-line-format w3-modeline-format)
2139     (run-hooks 'w3-mode-hook)
2140     (widget-setup)))
2141
2142 (put 'w3-mode 'mode-class 'special)
2143
2144 (require 'url)
2145 (require 'w3-parse)
2146 (require 'w3-display)
2147 ; (require 'w3-auto) ;; XEmacs; auto-autoloads instead.
2148 (require 'w3-emulate)
2149 (require 'w3-menu)
2150 (require 'w3-mouse)
2151 (provide 'w3)
2152
2153 ;;; w3.el ends here