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
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>
11 ;;; This file is part of GNU Emacs.
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.
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.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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 ;;;
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 (eval-when-compile (require 'cl))
51 (require 'w3-display))
53 ;; Some mm-* "functions" are macros. Ensure that they are loaded.
57 (autoload 'w3-parse-hotlist "w3-hot")
58 (autoload 'w3-menu-install-menus "w3-menu")
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)))
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.
75 ((eq w3-notify 'bully)
77 (delete-other-windows))
78 ((eq w3-notify 'semibully)
80 (switch-to-buffer buff)
81 (error (message "W3 buffer %s is ready." (buffer-name buff)))))
82 ((eq w3-notify 'aggressive)
84 ((eq w3-notify 'friendly)
85 (display-buffer buff 'not-this-window))
86 ((eq w3-notify 'polite)
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)))
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
98 (interactive "FLocal file: ")
99 (setq fname (expand-file-name fname))
101 (w3-fetch (concat "file:" fname)))
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
108 (interactive "FLocal file: ")
109 (w3-open-local fname))
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)))
116 ((and (fboundp 'make-frame)
117 (fboundp 'select-frame)
118 (not (eq (device-type) 'tty)))
119 (let ((frm (make-frame)))
121 (delete-other-windows)
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)))
131 (defun w3-read-url-with-default ()
133 (let* ((completion-ignore-case t)
136 ((eq major-mode 'w3-mode)
137 (or (and current-prefix-arg (w3-view-this-url t))
139 ((url-get-url-at-point)
140 (url-get-url-at-point))
144 (completing-read "URL: " 'url-completion-function
147 (setq url (if (eq major-mode 'w3-mode)
148 (if (and current-prefix-arg (w3-view-this-url t))
151 (url-get-url-at-point))))
154 (defvar w3-explicit-coding-system nil
155 "Coding system to decode documents.
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.")
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)
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
179 (setq coding-system 'undecided))
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
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)))
194 (defvar http-header) ; dynamically bound below
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'."
203 (if (eq 'meta (car-safe node))
204 (let ((attrs (cadr node)))
205 (if (assq 'http-equiv attrs)
206 (if (assq 'content attrs)
211 (cdr (assq 'http-equiv attrs))
212 (cdr (assq 'content attrs))))))))
213 (w3-http-equiv-headers (nth 2 node)))))))
215 (defun w3-nasty-disgusting-http-equiv-handling (buffer url)
216 "Propagate information from <meta http-equiv...> elements to MIME headers.
218 (let (content-type end-of-headers extra-headers)
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))
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.
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
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)))))
254 (goto-char (point-min))
255 (unless (save-excursion
256 (search-forward ":" (line-end-position) t))
258 (insert http-header)))))))))
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."
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)))
276 (start-itimer "reloader" func time))
277 ((fboundp 'run-at-time)
278 (run-at-time time nil func))
280 (w3-warn 'url "Cannot set up timer for automatic reload, sorry!")))))
282 (defun w3-handle-refresh-header (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)
290 (setq uri (substring reload (match-end 0) nil)
291 reload (substring reload 0 (match-beginning 0)))
293 "ur[li][ \t]*=[ \t]*\"*\\([^ \t\"]+\\)\"*"
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"))))))
300 (defun w3-fetch-redirect-callback (&rest args)
301 (let (redirect-url errorp)
302 ;; Handle both styles of `url-retrieve' callbacks...
305 ;; Emacs 22 style. First argument is a list.
306 (let ((status (car args)))
307 (when (eq (car status) :error)
309 (setq status (cddr args)))
310 (when (eq (car status) :redirect)
311 (setq redirect-url (second (car args))))
313 (setq args (cdr args))))
315 ((eq (car args) :redirect)
317 (setq redirect-url (cadr args))
318 (while (eq (car args) :redirect)
319 (setq args (cddr args)))))
321 ;; w3-fetch-callback can handle errors, too.
322 (w3-fetch-callback (or redirect-url (car args)))))
324 (defun w3-fetch-callback (url)
325 (w3-nasty-disgusting-http-equiv-handling (current-buffer) url)
326 ;; Process any cookie and refresh headers.
330 (mail-narrow-to-head)
331 (goto-char (point-min))
332 (unless (save-excursion
333 (search-forward ":" (line-end-position) t))
335 (setq headers (mail-header-extract))
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.
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)))
349 (message "Downloading of `%s' complete." url)
350 (url-mark-buffer-as-dead (current-buffer))
352 (setq headers (list (cons 'content-type
353 (mm-handle-media-type handle)))))
354 ;; Fixme: can handle be null?
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))
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)
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)))
399 ;; Must be an external viewer
400 (mm-display-part handle)
401 ;;(mm-destroy-parts handle)
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)))
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!"))
418 ;; In the common case, this is probably cheaper than searching.
419 (while (= (string-to-char url) ? )
420 (setq url (substring url 1)))
422 (or target (setq target w3-base-target))
424 (setq target (intern (downcase target))))
426 (let ((window-distance (cdr-safe (assq target
427 w3-target-window-distances))))
428 (if (numberp window-distance)
429 (other-window window-distance)
432 (w3-fetch-other-frame url))
434 (delete-other-windows))
436 (message "target %S not found." target))))))
439 ((= (string-to-char url) ?#)
440 (w3-relative-link url))
441 ((and (interactive-p) current-prefix-arg)
442 (w3-download-url url))
444 (let ((x (url-view-url t))
445 (lastbuf (current-buffer))
446 (w3-current-buffer (current-buffer))
447 (buf (w3-buffer-visiting url)))
450 ((not (equal (downcase (or url-request-method "GET")) "get"))
452 ((memq w3-reuse-buffers '(no never reload)) t)
453 ((memq w3-reuse-buffers '(yes reuse always)) nil)
455 (when (and w3-reuse-buffers (not (eq w3-reuse-buffers 'ask)))
458 "Warning: Invalid value for variable w3-reuse-buffers: %s"
459 (prin1-to-string w3-reuse-buffers))
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))))))
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.")
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)))
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)
496 (defun w3-history-forward ()
497 "Go forward in the history from this page."
499 (let ((next (cadr (w3-history-find-url-internal (url-view-url t))))
500 (w3-reuse-buffers 'yes))
504 (defun w3-history-backward ()
505 "Go backward in the history from this page."
507 (let ((last (caar (w3-history-find-url-internal (url-view-url t))))
508 (w3-reuse-buffers 'yes))
512 (defun w3-history-push (referer url)
513 "REFERER is the url we followed this link from. URL is the link we got to."
515 (setq w3-history-stack (list (cons url (current-time))))
516 (let ((node (memq (assoc referer w3-history-stack) w3-history-stack)))
518 (setcdr node (list (cons url (current-time))))
519 (setq w3-history-stack (append w3-history-stack
521 (cons url (current-time)))))))))
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)
528 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
529 ;;; Miscellaneous functions
530 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
531 (defun w3-describe-entities ()
532 "Show an DTD fragment listing all the entities currently defined."
534 (switch-to-buffer (get-buffer-create "W3 Entities"))
535 (let ((buffer-file-name (concat (make-temp-name "entities") ".dtd")))
541 (setq entity (get x 'html-entity-expansion))
543 (insert (format "<!entity %s %s \"%s\">\n" x (car entity)
545 (goto-char (point-min)))
547 (defun w3-document-information (&optional buff)
548 "Display information on the document in buffer BUFF."
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)))
558 (let* ((url (url-view-url t))
559 (cur-links w3-current-links)
560 (title (buffer-name))
562 (attributes (file-attributes url))
563 (lastmod (or (cdr-safe (assq 'last-modified
564 url-current-mime-headers))
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)
574 (if (equal '(0 . 0) lastmod)
576 (setq lastmod (current-time-string lastmod))))
577 (setq url-current-mime-type "text/html")
579 Content-Type: text/html\n
582 <title>Document Information</title>
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>")
590 <tr><td>Size:</td><td>" (url-pretty-length (if (stringp size)
592 size)) "</td></tr>"))
594 <tr><td>Last Modified:</td><td>" (or lastmod "None Given") "</td></tr>\n")
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))
602 (let* ((maxlength (car (sort (mapcar (lambda (x)
606 (fmtstring (format "\
607 <tr><td align=right>%%%ds:</td><td>%%s</td></tr>" maxlength)))
610 <tr><th colspan=2>MetaInformation</th></tr>\n"
613 (if (/= (length (car x)) 0)
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))
622 (lambda (x y) (string-lessp (car x) (car y))))
625 ;; collapse `rel' and `rev' components
626 (setq links (apply 'append (mapcar 'cdr links)))
630 (cons (or (plist-get (cadr elt) 'title)
632 (plist-get (cadr elt) 'href)))
634 (let* ((maxlength (car (sort (mapcar (lambda (x)
640 " <tr><td>%%%ds:</td><td><a href='%%s'>%%s</a></td></tr>"
643 " <tr><th colspan=2>Document Links</th></tr>\n")
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
651 (url-insert-entities-in-string
652 (cdar links))) "\n"))
653 (setq links (cdr links)))))
656 (let* ((maxlength (car (sort (mapcar (lambda (x)
661 " <tr><td>%%%ds:</td><td>%%s</td></tr>"
664 " <tr><th colspan=2>Miscellaneous Variables</th></tr>\n")
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
672 (setq info (cdr info)))))
673 (insert " </table></body></html>\n")
676 (defun w3-insert-formatted-url (p)
677 "Insert a formatted url into a buffer.
678 With prefix arg, insert the url under point."
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: "
689 (widget-get p :to))))))
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))
698 (message "Cancelled."))))
700 (defun w3-first-n-items (l n)
701 "Return the first N items from list L."
707 (setq y (nconc y (list (nth x l)))
711 (defun w3-widget-button-press ()
713 (if (widget-at (point))
714 (widget-button-press (point))))
716 (defun w3-widget-button-click (e)
718 (if (featurep 'xemacs)
720 ((and (event-point e)
721 (widget-at (event-point e)))
722 (widget-button-click e))
723 ((and (fboundp 'event-glyph)
725 (glyph-property (event-glyph e) 'widget))
726 (widget-button-click e)))
729 (if (widget-at (point))
730 (widget-button-click e)))))
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'"
740 (w3-maybe-follow-link)))
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'"
750 (let* ((widget (widget-at (point)))
751 (url1 (and widget (widget-get widget :href)))
752 (url2 (url-get-url-at-point)))
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!")))))
759 (defun w3-follow-url-at-point-other-frame (&optional pt)
760 "Follow the URL under PT, defaults to link under (point)."
762 (let ((url (url-get-url-at-point pt)))
763 (and url (w3-fetch-other-frame url))))
766 (defun w3-follow-url-at-point (&optional pt)
767 "Follow the URL under PT, defaults to link under (point)."
769 (let ((url (url-get-url-at-point pt)))
770 (and url (w3-fetch url))))
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)));)
778 (defun w3-source-document-at-point ()
779 "View source to the document pointed at by link under point."
781 (w3-source-document t))
783 (defun w3-source-document (under)
784 "View this document's source."
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 ...
797 (setq buffer-file-truename nil
798 buffer-file-name nil)
800 (set-buffer-modified-p nil)
801 (w3-notify-when-ready (current-buffer)))
802 (run-hooks 'w3-source-file-hook))
804 (defun w3-mail-document-under-point ()
805 "Mail the document pointed to by the hyperlink under point."
807 (w3-mail-current-document t))
809 (defun w3-mail-current-document (under &optional format)
810 "Mail the current-document to someone."
812 (let* ((completion-ignore-case t)
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))
830 (content-type (concat "text/plain; charset=" content-charset))
834 ((and (equal "HTML Source" format) under)
835 (setq content-type (concat "text/html; charset="
837 (let ((url-source t))
838 ;; Fixme: this needs a callback -- which?
840 ((equal "HTML Source" format)
841 (setq content-type (concat "text/html; charset="
843 (if w3-current-source
844 (let ((x w3-current-source))
845 (set-buffer (get-buffer-create url-working-buffer))
848 ;; Fixme: this needs a callback -- which?
850 ((and under (equal "PostScript" format))
851 (setq content-type "application/postscript")
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)
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="
871 ((equal "Formatted Text" format)
872 (setq content-type (concat "text/plain; charset="
875 (funcall url-mail-command)
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)
885 (if (and (boundp 'mime/editor-mode-flag) mime/editor-mode-flag)
886 (insert (format mime-tag-format content-type) "\n"))
889 (cond ((equal "HTML Source" format)
890 (if (or (search-forward "<head>" nil t)
891 (search-forward "<html>" nil t))
893 (insert (format "<base href=\"%s\">" url))))
894 ;; Fixme: not defined.
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)))
903 ((null buf) ; Find a buffer with same url
904 (let ((x (buffer-list))
906 (while (and x (not found))
909 (setq found (string= (url-view-url t) url))
910 (if (not found) (setq x (cdr x)))))
913 (switch-to-buffer (car x))
914 (if (number-or-marker-p pnt) (goto-char pnt)))
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
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.")))))
928 (defun w3-relative-link (url)
929 (if (equal "#" (substring url 0 1))
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))))
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))
942 (defun w3-use-links ()
943 "Select one of the <LINK> tags from this document and fetch it."
945 (and (not w3-current-links)
946 (error "No links defined for this document"))
947 (w3-fetch "about:document"))
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"
953 (or url-current-object
954 (error "Not a URL-based buffer"))
955 (let ((type (url-type url-current-object)))
958 (find-file (url-filename url-current-object)))
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.")))))
968 (defun w3-insert-this-url (pref-arg)
969 "Insert the current url in another buffer.
970 With prefix ARG, insert URL under point"
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)))
980 (message "Not on a link!"))))
982 (defun w3-in-assoc (elt list)
983 "Check to see if ELT matches any of the regexps in the car elements of LIST."
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)))
994 (defun w3-goto-last-buffer ()
995 "Go to last WWW buffer visited."
997 (if w3-current-last-buffer
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.")))
1006 (fset 'w3-replace-regexp 'url-replace-regexp)
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."
1015 (w3-fetch (concat "www://preview/" (buffer-name))))
1018 "Show the source of a file."
1019 (let ((tmp (buffer-name (generate-new-buffer "Document Source"))))
1020 (set-buffer url-working-buffer)
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))))
1030 (defvar w3-mime-list-for-code-conversion
1031 '("text/plain" "text/html")
1032 "List of MIME types that require Mules' code conversion.")
1034 (defvar w3-compression-encodings
1035 '("x-gzip" "gzip" "x-compress" "compress")
1036 "List of MIME encodings that denote compression.")
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'.")
1044 (defun w3-show-history-list ()
1045 "Format the url-history-list prettily and show it to the user."
1047 (w3-fetch "www://auto/history"))
1049 (defun w3-save-as (&optional type)
1050 "Save a document to the local disk."
1053 (let* ((completion-ignore-case t)
1054 (format (or type (completing-read
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*"))
1068 ((equal "Binary" format)
1070 ((equal "HTML Source" format)
1072 (goto-char (point-min))
1073 (if (re-search-forward "<head>" nil t)
1075 (insert (format "<BASE HREF=\"%s\">\n" url)))
1076 ((or (equal "Formatted Text" format)
1079 ((equal "PostScript" format)
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)))))
1088 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1089 ;;; Functions for logging of bad HTML
1090 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1091 (defun w3-reconstruct-tag (tagname desc)
1092 (concat "<" tagname " "
1094 (function (lambda (x)
1096 (concat (car x) "=\"" (cdr x) "\"")
1097 (car x)))) desc " ") ">"))
1099 (defun w3-debug-if-found (regexp type desc)
1102 (if (re-search-forward regexp nil t)
1103 (w3-log-bad-html type desc)))))
1105 (defun w3-log-bad-html (type desc)
1106 "Log bad HTML to the buffer specified by w3-debug-buffer."
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")
1113 ((stringp type) (insert type "\n" desc "\n"))
1114 ((eq type 'bad-quote)
1115 (insert "Unterminated quoting character in SGML attribute value.\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"))))))
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)))
1144 (defun w3-load-flavors ()
1145 "Load the correct emacsen specific stuff."
1146 (if (featurep 'xemacs)
1148 (require 'w3-emacs))
1149 (if (featurep 'emacspeak)
1152 (require 'dtk-css-speech)
1153 (require 'w3-speak))))
1155 (require 'w3-site-init)
1158 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1159 ;;; Automatic bug submission. ;;;
1160 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1161 (defun w3-submit-bug ()
1162 "Submit a bug on Emacs-w3."
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
1176 (if (and url (string= url "file:nil")) (setq url nil))
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 "
1187 "Description of Problem:"))))
1189 (defalias 'w3-bug 'w3-submit-bug)
1191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1192 ;;; Support for searching ;;;
1193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1194 (defun w3-nuke-spaces-in-search (x)
1195 "Remove spaces from search strings . . ."
1197 (while (not (equal x ""))
1198 (setq new (concat new (if (= (string-to-char x) 32) "+"
1200 x (substring x 1 nil)))
1204 "Perform a search, if this is a searchable index."
1206 (let* (querystring ; The string to send to the server
1209 ((null w3-current-isindex)
1210 (let ((rels (cdr-safe (assq 'rel w3-current-links)))
1213 (setq cur (car 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)
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)
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)))
1235 (concat index (if (= ?? (string-to-char (substring index -1 nil)))
1236 "" "?") querystring))))
1238 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1239 ;;; Auto documentation, etc ;;;
1240 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1242 "Print documentation on w3 mode."
1244 (w3-fetch "about:"))
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."
1251 (let ((version-string
1252 (format "WWW %s, URL %s"
1256 (insert version-string)
1258 (message "%s" version-string)
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.
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."
1273 (if (and w3-track-last-buffer
1274 (bufferp w3-last-buffer)
1275 (buffer-name w3-last-buffer))
1277 (switch-to-buffer w3-last-buffer)
1278 (message "Reusing buffer. To reload, type %s."
1279 (substitute-command-keys "\\[w3-reload-document]")))
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)))
1288 (w3-fetch w3-default-homepage)))))
1290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1291 ;;; Stuff for good local file handling
1292 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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)
1301 (display-buffer (find-file-noselect file))))
1303 (defun w3-default-local-file()
1304 "Use find-file to open the local file"
1305 (w3-ff (url-filename url-current-object)))
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? "))
1317 (goto-char (point-min))
1318 (w3-search-forward string))))
1320 (defun w3-search-again ()
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? "))
1328 (goto-char (point-min))
1329 (w3-search-again)))))
1331 (defun w3-find-specific-link (link)
1332 (let ((pos (assq (intern link) w3-id-positions)))
1335 (goto-char (cdr pos))
1336 (if (and (eolp) (not (eobp)))
1338 (message "Link #%s not found." link))))
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)))
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."
1350 (list (if (and (featurep 'mule) current-prefix-arg)
1351 (read-coding-system "Coding system: "))))
1352 (let ((tmp (url-view-url t))
1354 (window-start (progn
1355 (move-to-window-line 0)
1357 (url-request-extra-headers '(("Pragma" . "no-cache")))
1358 (w3-explicit-coding-system explicit-coding-system))
1359 (kill-buffer (current-buffer))
1361 (if explicit-coding-system
1362 (w3-record-explicit-coding-system tmp explicit-coding-system))
1364 (set-window-start (selected-window) (min window-start (point-max)))))
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)
1372 (hostname (or (url-host urlobj) "localhost"))
1373 (fname-list (split-string (url-filename urlobj) "\\/")))
1375 (w3-find-explicit-coding-system (cons hostname fname-list)
1376 w3-explicit-conversion-tree)))
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)))
1382 (or (and (cdr fname-list) (cddr branch)
1383 (w3-find-explicit-coding-system (cdr fname-list)
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)
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))
1401 (w3-add-explicit-coding-system fname-list coding-system tree)
1402 (setq w3-explicit-encodings-changed-since-last-save t)))
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))))
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))
1418 (setcar (cdr branch) coding-system))))
1420 ;; create a new branch
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)))
1429 (cons subbranch (cddr tree))))
1430 (list coding-system)))))))
1432 (defun w3-write-explicit-encodings (&optional fname)
1433 "Write the explicit encodings file into `w3-explicit-encodings-file'."
1436 (and w3-explicit-encodings-file
1437 (setq fname (expand-file-name w3-explicit-encodings-file))))
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))
1443 (let ((make-backup-files nil)
1444 (version-control nil)
1445 (require-final-newline t))
1447 (set-buffer (get-buffer-create " *w3-tmp*"))
1449 (insert "(setq w3-explicit-conversion-tree\n '"
1450 (prin1-to-string w3-explicit-conversion-tree)
1453 (kill-buffer (current-buffer))))))
1454 (setq w3-explicit-encodings-changed-since-last-save nil))
1456 (defun w3-leave-buffer ()
1457 "Bury this buffer, but don't kill it."
1459 (let ((x w3-current-last-buffer))
1461 (w3-leave-or-quit-frameset x nil)
1464 (if (and (bufferp x) (buffer-name x))
1465 (w3-notify-when-ready x))))))
1467 (defun w3-quit (&optional mega)
1474 (set-buffer (get-buffer x))
1475 (if (eq major-mode 'w3-mode)
1478 (let ((x w3-current-last-buffer))
1480 (w3-leave-or-quit-frameset x t)
1482 (kill-buffer (current-buffer))
1483 (if (and (bufferp x) (buffer-name x))
1484 (w3-notify-when-ready x)))))))
1486 (defun w3-leave-or-quit-frameset (x quit-p &optional top-down-p)
1488 (delete-other-windows)
1489 (let ((structure (reverse w3-frameset-structure)))
1491 (let ((elt (car structure)))
1492 (if (eq (car elt) 'frame)
1493 (let* ((url (nth 2 elt))
1494 (buf (w3-buffer-visiting url)))
1498 (if w3-frameset-structure
1499 (w3-leave-or-quit-frameset buf quit-p t)
1502 (bury-buffer buf))))))))
1512 (w3-leave-buffer)))))
1514 (defun w3-view-this-url (&optional no-show)
1515 "View the URL of the link under point."
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)))))
1525 (message "%s" (url-truncate-url-for-viewing href)))
1529 (widget-echo-help (point)))
1533 (defun w3-load-delayed-images ()
1534 "Load inlined images that were delayed, if any."
1536 (let ((w3-delay-image-loads nil)
1537 (todo w3-delayed-images))
1538 (setq w3-delayed-images nil)
1540 (w3-maybe-start-image-download (car todo))
1541 (setq todo (cdr todo)))))
1543 (defun w3-save-this-url ()
1544 "Save url under point in the kill ring."
1548 (defun w3-save-url (under-pt)
1549 "Save current url in the kill ring."
1552 ((stringp under-pt) under-pt)
1553 (under-pt (w3-view-this-url t))
1554 (t (url-view-url t)))))
1558 (message "Stored URL in kill-ring."))
1559 (error "No URL to store"))))
1561 (fset 'w3-end-of-document 'end-of-buffer)
1562 (fset 'w3-start-of-document 'beginning-of-buffer)
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."
1568 (if (and (pos-visible-in-window-p (point-max))
1569 ;; Allow scrolling backward at the end of the buffer.
1573 (let ((view-lines (1- (window-height))))
1575 (if lines (prefix-numeric-value lines)
1577 (if (>= lines view-lines)
1579 (if (>= (- lines) view-lines)
1582 (cond ((pos-visible-in-window-p (point-max))
1583 (goto-char (point-max))
1585 (move-to-window-line -1)
1586 (beginning-of-line))))
1589 (defun w3-mail-document-author ()
1590 "Send mail to the author of this document, if possible."
1592 (let ((x w3-current-links)
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))
1601 found (cdr-safe (assoc "made" y))))
1603 (let ((possible nil)
1605 (setq x (car found)) ; Fallback if no mail(to|server) 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)))
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."))))
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?
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
1633 (user-real-uid)) ".*")))
1634 (and (file-exists-p url-temporary-directory)
1635 (directory-files url-temporary-directory t "url-tmp.*")))))
1638 (delete-file (car x))
1641 (message "Cleaning up w3 temporary files... done."))
1645 ((fboundp 'display-warning)
1646 (fset 'w3-warn 'display-warning))
1648 (defun w3-warn (class message &optional level)
1649 (if (and (eq class 'html)
1650 (not w3-debug-html))
1652 (warn "(%s/%s) %s" class (or level 'warning) message))))
1654 (defun w3-warn (class message &optional level)
1655 (if (and (eq class 'html)
1656 (not w3-debug-html))
1659 (set-buffer (get-buffer-create "*W3-WARNINGS*"))
1660 (goto-char (point-max))
1662 (insert (format "(%s/%s) %s\n" class (or level 'warning) message)))
1663 (display-buffer (current-buffer))))))))
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
1669 The arguments FROM, TO, MAPARG, and BUFFER default to the beginning of
1670 BUFFER, the end of BUFFER, nil, and (current-buffer), respectively."
1672 (highly-unlikely-name-for-a-variable-holding-a-function function))
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
1678 ((and widget (widget-get widget :href))
1679 (funcall highly-unlikely-name-for-a-variable-holding-a-function
1681 ((and parent (widget-get parent :href))
1682 (funcall highly-unlikely-name-for-a-variable-holding-a-function
1687 (defun w3-refresh-stylesheets ()
1688 "Reload all stylesheets."
1690 (setq w3-user-stylesheet nil
1692 (w3-find-default-stylesheets)
1695 (defvar w3-loaded-stylesheets nil
1696 "A list of all the stylesheets Emacs-W3 loaded at startup.")
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))
1710 (if (fboundp 'locate-data-directory)
1711 (locate-data-directory "w3"))
1713 (concat data-directory "w3/")
1714 (expand-file-name "../../w3" data-directory)
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))
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))))
1738 (and (not no-user-init)
1739 (list w3-default-stylesheet))))
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))
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)
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"
1763 (mapconcat 'identity remember "\n")
1765 (error "No stylesheets found! Check configuration! DANGER DANGER!")))))
1767 (defvar w3-widget-global-map nil)
1770 (defun w3-do-setup ()
1772 This is to avoid conflict with user settings when W3 is dumped with
1774 (unless w3-setup-done
1777 (w3-setup-version-specifics)
1778 (setq w3-default-configuration-file (expand-file-name
1779 (or w3-default-configuration-file
1781 w3-configuration-directory))
1782 (if (and init-file-user
1783 w3-default-configuration-file
1784 (file-exists-p w3-default-configuration-file))
1786 (load w3-default-configuration-file nil t)
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))
1793 (switch-to-buffer-other-window buf-name)
1794 (shrink-window-if-larger-than-buffer))
1795 (w3-warn 'configuration
1796 (format (eval-when-compile
1798 "Configuration file `%s' contains an error.\n"
1799 "Please consult the `%s' buffer for details."))
1800 w3-default-configuration-file buf-name))))))
1802 ;; Load the explicit encodings file if it exists
1803 (if (and w3-explicit-encodings-file
1804 (file-exists-p w3-explicit-encodings-file))
1806 (load w3-explicit-encodings-file nil t)
1809 (if (and (eq w3-user-colors-take-precedence 'guess)
1810 (not (eq (device-type) 'tty))
1811 (not (eq (device-class) 'mono)))
1813 (setq w3-user-colors-take-precedence t)
1816 "Disabled document color specification because of mono display.")))
1818 (w3-refresh-stylesheets)
1819 (setq w3-setup-done t)
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)
1826 (setq url-package-version w3-version-number
1827 url-package-name "Emacs-W3")
1829 (w3-setup-terminal-chars)
1832 ((memq system-type '(ms-dos ms-windows))
1833 (setq w3-hotlist-file (or w3-hotlist-file
1834 (expand-file-name "~/mosaic.hot"))
1836 ((memq system-type '(axp-vms vax-vms))
1837 (setq w3-hotlist-file (or w3-hotlist-file
1838 (expand-file-name "~/mosaic.hotlist-default"))
1841 (setq w3-hotlist-file (or w3-hotlist-file
1842 (expand-file-name "~/.mosaic-hotlist-default"))
1845 ;; Set up a hook that will save the history list when exiting
1847 (add-hook 'kill-emacs-hook 'w3-kill-emacs-func)
1849 ;; Load in the hotlist if they haven't set it already
1850 (or w3-hotlist (w3-parse-hotlist))
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/")))
1859 (run-hooks 'w3-load-hook)))
1861 (defun w3-mark-link-as-followed (ext dat)
1862 "Mark a link as followed."
1863 (message "Reimplement w3-mark-link-as-followed"))
1865 (defun w3-only-links ()
1867 (w3-map-links (lambda (x y)
1868 (setq result (cons x result))))
1871 (defun w3-download-redirect-callback (&rest args)
1872 (let (redirect-url errorp)
1873 ;; Handle both styles of `url-retrieve' callbacks...
1876 ;; Emacs 22 style. First argument is a list.
1877 (let ((status (car args)))
1878 (when (eq (car status) :error)
1880 (setq status (cddr args)))
1881 (when (eq (car status) :redirect)
1882 (setq redirect-url (second (car args))))
1884 (setq args (cdr args))))
1886 ((eq (car args) :redirect)
1888 (setq redirect-url (cadr args))
1889 (while (eq (car args) :redirect)
1890 (setq args (cddr args)))))
1893 (message "Download of %s failed." (url-view-url t))
1894 (w3-download-callback (car args)))))
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))
1905 (defun w3-download-url-at-point ()
1906 "Download the URL under point."
1908 (w3-download-url-wrapper t))
1910 (defun w3-download-this-url ()
1911 "Download the current URL."
1913 (w3-download-url-wrapper nil))
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))))
1920 (error "No link found"))))
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
1930 (read-file-name "Filename to save as: "
1935 (url-retrieve url 'w3-download-redirect-callback (list fname))))
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
1943 ((and (fboundp 'make-frame)
1944 (fboundp 'select-frame))
1945 (let ((frm (make-frame)))
1947 (w3-follow-link p)))
1948 (t (w3-follow-link p))))
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
1956 (let* ((widget (widget-at (point)))
1957 (href (and widget (widget-get widget :href))))
1961 (w3-download-url href))
1966 (defun w3-next-document ()
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))))))
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)))
1980 (error "No NEXT document"))))
1983 (defun w3-prev-document ()
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)))))
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)))
1997 (error "No PREVIOUS document"))))
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."
2004 (widget-forward arg))
2006 (defun w3-widget-backward (arg)
2007 "Move point to the previous field or button.
2008 With optional ARG, move across that many fields."
2010 (w3-widget-forward (- arg)))
2012 (defun w3-complete-link ()
2013 "Choose a link from the current buffer and follow it."
2018 (completion-ignore-case t))
2019 (setq link-at-point (widget-at (point))
2022 (widget-get link-at-point :href)
2023 (widget-get link-at-point :from)
2024 (widget-get link-at-point :to)
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
2035 (buffer-substring-no-properties
2036 (widget-get widget :from)
2037 (widget-get widget :to)))
2038 (widget-get widget :href))
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
2051 (concat "Link (default "
2052 (if (< (length link-at-point) 20)
2055 (substring link-at-point 0 17) "..."))
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)))
2062 ((eq t match) ; We have an exact match
2063 (setq choice (cdr (assoc choice links-alist))))
2065 (setq choice (cdr (assoc match links-alist))))
2066 (t (setq choice nil)))
2068 (w3-fetch choice)))))
2070 (defun w3-display-errors ()
2071 "Display any HTML errors for the current page."
2073 (let ((w3-notify 'friendly)
2074 (inhibit-read-only t)
2076 (todo w3-current-badhtml)
2077 (url (url-view-url t)))
2079 (error "No HTML errors on this page! Amazing, isn't it?"))
2082 (get-buffer-create (concat "HTML Errors for: " (or url "???"))))
2083 (setq buffer (current-buffer))
2086 (goto-char (point-min))
2087 (insert "\n" (car todo))
2088 (setq todo (cdr todo)))
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)
2099 (run-hooks 'w3-display-errors-hook))
2100 (w3-notify-when-ready buffer)))
2103 "Mode for viewing HTML documents.
2104 If called interactively, will display the current buffer as HTML.
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.
2117 ;: Two problems with this approach:
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.
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.
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)
2142 (put 'w3-mode 'mode-class 'special)
2146 (require 'w3-display)
2147 ; (require 'w3-auto) ;; XEmacs; auto-autoloads instead.
2148 (require 'w3-emulate)