;;; w3.el --- Main functions for emacs-w3 on all platforms/versions ;; Author: $Author: legoscia $ ;; Created: $Date: 2007/11/15 12:59:53 $ ;; Version: $Revision: 1.40 $ ;; Keywords: faces, help, comm, news, mail, processes, mouse, hypermedia ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Copyright (c) 1996, 97, 98, 99, 2001, 2007 Free Software Foundation, Inc. ;;; Copyright (c) 1993 - 1996 by William M. Perry ;;; ;;; This file is part of GNU Emacs. ;;; ;;; GNU Emacs is free software; you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 2, or (at your option) ;;; any later version. ;;; ;;; GNU Emacs is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with GNU Emacs; see the file COPYING. If not, write to the ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;;; Boston, MA 02111-1307, USA. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; This is a major mode for browsing documents written in Hypertext Markup ;;; ;;; Language (HTML). These documents are typicallly part of the World Wide ;;; ;;; Web (WWW), a project to create a global information net in hypertext ;;; ;;; format. ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (require 'w3-compat) (require 'w3-cfg) (or (featurep 'efs) (featurep 'efs-auto) (condition-case () (require 'ange-ftp) (error nil))) (eval-when-compile (require 'cl)) (require 'css) (require 'url-vars) (require 'url-parse) (require 'w3-vars) (eval-and-compile (require 'w3-display)) ;; Some mm-* "functions" are macros. Ensure that they are loaded. (eval-when-compile (require 'mm-decode)) (autoload 'w3-parse-hotlist "w3-hot") (autoload 'w3-menu-install-menus "w3-menu") (defun w3-notify-when-ready (buff) "Notify the user when BUFF is ready. See the variable `w3-notify' for the different notification behaviors." (if (stringp buff) (setq buff (get-buffer buff))) (cond ((null buff) nil) ((eq w3-notify 'newframe) ;; Since we run asynchronously, perhaps while Emacs is waiting for input, ;; we must not leave a different buffer current. ;; We can't rely on the editor command loop to reselect ;; the selected window's buffer. (save-excursion (set-buffer buff) (make-frame))) ((eq w3-notify 'bully) (pop-to-buffer buff) (delete-other-windows)) ((eq w3-notify 'semibully) (condition-case nil (switch-to-buffer buff) (error (message "W3 buffer %s is ready." (buffer-name buff))))) ((eq w3-notify 'aggressive) (pop-to-buffer buff)) ((eq w3-notify 'friendly) (display-buffer buff 'not-this-window)) ((eq w3-notify 'polite) (beep) (message "W3 buffer %s is ready." (buffer-name buff))) ((eq w3-notify 'quiet) (message "W3 buffer %s is ready." (buffer-name buff))) (t (message "")))) ;;;###autoload (defun w3-open-local (fname) "Find a local file, and interpret it as a hypertext document. Prompt for an existing file or directory, and retrieve it as a hypertext document." (interactive "FLocal file: ") (setq fname (expand-file-name fname)) (w3-do-setup) (w3-fetch (concat "file:" fname))) ;;;###autoload (defun w3-find-file (fname) "Find a local file, and interpret it as a hypertext document. Prompt for an existing file or directory, and retrieve it as a hypertext document." (interactive "FLocal file: ") (w3-open-local fname)) ;;;###autoload (defun w3-fetch-other-frame (&optional url) "Attempt to follow the hypertext reference under point in a new frame." (interactive (list (w3-read-url-with-default))) (cond ((and (fboundp 'make-frame) (fboundp 'select-frame) (not (eq (device-type) 'tty))) (let ((frm (make-frame))) (select-frame frm) (delete-other-windows) (w3-fetch url))) (t (w3-fetch url)))) (defun w3-fetch-other-window (&optional url) "Attempt to follow the hypertext reference under point in a new window." (interactive (list (w3-read-url-with-default))) (split-window) (w3-fetch url)) (defun w3-read-url-with-default () (url-do-setup) (let* ((completion-ignore-case t) (default (cond ((eq major-mode 'w3-mode) (or (and current-prefix-arg (w3-view-this-url t)) (url-view-url t))) ((url-get-url-at-point) (url-get-url-at-point)) (t "http://www."))) (url nil)) (setq url (completing-read "URL: " 'url-completion-function nil nil default)) (if (string= url "") (setq url (if (eq major-mode 'w3-mode) (if (and current-prefix-arg (w3-view-this-url t)) (w3-view-this-url t) (url-view-url t)) (url-get-url-at-point)))) url)) (defvar w3-explicit-coding-system nil "Coding system to decode documents. The global value is usually nil. It will be bound locally if a user invokes some commands which read a coding system from the user.") (defun w3-decode-charset (handle) "Decode charset-encoded text in the document. HANDLE is the MIME handle of the original part. Return the coding system used for the decoding." (let* ((encoding (mm-handle-encoding handle)) (charset (or w3-explicit-coding-system (mail-content-type-get (mm-handle-type handle) 'charset) "iso-8859-1")) (type (mm-handle-media-type handle)) (coding-system (mm-charset-to-coding-system charset))) (if (or (not coding-system) (eq coding-system 'ascii)) ;; Does this work for XEmacs? Should we actually guess (which ;; is what `undecided' involves)? In Emacs 20 we'll get ;; byte-combination anyhow when switching to multibyte below, ;; but we can't leave the buffer as unibyte, or we can't deal ;; properly with non-ASCII entities inserted by the parsing ;; stage. (setq coding-system 'undecided)) (save-excursion (if encoding (mm-decode-content-transfer-encoding encoding type)) (when (and (featurep 'mule) (if (boundp 'default-enable-multibyte-characters) default-enable-multibyte-characters t) coding-system) (mm-decode-coding-region (point-min) (point-max) coding-system) ;; Potentially useful is the buffer's going to be saved, and ;; for the mode-line indication. (set-buffer-file-coding-system coding-system)) (mm-enable-multibyte)) coding-system)) (defvar http-header) ; dynamically bound below (defun w3-http-equiv-headers (tree) "Grovel parse TREE for elements. Concatenate the equivalent MIME header onto the dynamically-bound variable `http-header'." (if (consp tree) (dolist (node tree) (if (consp node) (if (eq 'meta (car-safe node)) (let ((attrs (cadr node))) (if (assq 'http-equiv attrs) (if (assq 'content attrs) (setq http-header (concat http-header (format "%s: %s\n" (cdr (assq 'http-equiv attrs)) (cdr (assq 'content attrs)))))))) (w3-http-equiv-headers (nth 2 node))))))) (defun w3-nasty-disgusting-http-equiv-handling (buffer url) "Propagate information from elements to MIME headers. Operate on BUFFER." (let (content-type end-of-headers extra-headers) (save-excursion (set-buffer buffer) (goto-char (point-min)) (mail-narrow-to-head) (setq content-type (mail-fetch-field "content-type")) (goto-char (point-max)) ; Make sure we are beyond the headers (setq end-of-headers (point)) (widen) (let ((case-fold-search t)) (if (and content-type (string-match "^text/html" content-type) ;; Try not to parse past the head element. (re-search-forward " stuff in the head so we ;; can promote it into the MIME headers before ;; mm-dissect-buffer looks at them. (let (http-header) (save-restriction (narrow-to-region end-of-headers end-of-head) (goto-char (point-min)) ;; Quick check before parsing. (if (search-forward "http-equiv=" nil t) (w3-http-equiv-headers ;; We need to take a copy of the region we're ;; going to parse (which we hope is small) to ;; avoid assumptions about what ;; `w3-parse-buffer' does in the way of ;; widening and munging character references ;; &c. (with-temp-buffer (setq url-current-object (url-generic-parse-url url)) (insert-buffer-substring buffer end-of-headers end-of-head) (w3-parse-buffer))))) (when http-header (goto-char (point-min)) (unless (save-excursion (search-forward ":" (line-end-position) t)) (forward-line)) (insert http-header))))))))) (defun w3-setup-reload-timer (url must-be-viewing &optional time) "Set up a timer to load URL at optional TIME. If TIME is unspecified, default to 5 seconds. Only loads document if MUST-BE-VIEWING is the current URL when the timer expires." (if (or (not time) (<= time 0)) (setq time 5)) (let ((func `(lambda () (if (equal (url-view-url t) ,must-be-viewing) (let ((w3-reuse-buffers 'no)) (if (equal ,url (url-view-url t)) (kill-buffer (current-buffer))) (w3-fetch ,url)))))) (cond ((featurep 'itimer) (start-itimer "reloader" func time)) ((fboundp 'run-at-time) (run-at-time time nil func)) (t (w3-warn 'url "Cannot set up timer for automatic reload, sorry!"))))) (defun w3-handle-refresh-header (reload) (if (and reload url-honor-refresh-requests (or (eq url-honor-refresh-requests t) (funcall url-confirmation-func "Honor refresh request? "))) (let ((uri (url-view-url t))) (if (string-match ";" reload) (progn (setq uri (substring reload (match-end 0) nil) reload (substring reload 0 (match-beginning 0))) (if (string-match "ur[li][ \t]*=[ \t]*\"*\\([^ \t\"]+\\)\"*" uri) (setq uri (match-string 1 uri))) (setq uri (url-expand-file-name uri (url-view-url t))))) (w3-setup-reload-timer uri (url-view-url t) (string-to-int (or reload "5")))))) (defun w3-fetch-redirect-callback (&rest args) (let (redirect-url errorp) ;; Handle both styles of `url-retrieve' callbacks... (cond ((listp (car args)) ;; Emacs 22 style. First argument is a list. (let ((status (car args))) (when (eq (car status) :error) (setq errorp t) (setq status (cddr args))) (when (eq (car status) :redirect) (setq redirect-url (second (car args)))) (setq args (cdr args)))) ((eq (car args) :redirect) ;; Pre-22 redirect. (setq redirect-url (cadr args)) (while (eq (car args) :redirect) (setq args (cddr args))))) ;; w3-fetch-callback can handle errors, too. (w3-fetch-callback (or redirect-url (car args))))) (defun w3-fetch-callback (url) (w3-nasty-disgusting-http-equiv-handling (current-buffer) url) ;; Process any cookie and refresh headers. (let (headers) (ignore-errors (save-restriction (mail-narrow-to-head) (goto-char (point-min)) (unless (save-excursion (search-forward ":" (line-end-position) t)) (forward-line)) (setq headers (mail-header-extract)) (let (refreshed) (dolist (header headers) ;; Act on multiple cookies if necessary, but only on a ;; single refresh request in case there's more than one. (case (car header) (refresh (unless refreshed (w3-handle-refresh-header (cdr header)) (setq refreshed t)))))))) (let ((handle (mm-dissect-buffer t)) (w3-explicit-coding-system (or w3-explicit-coding-system (w3-recall-explicit-coding-system url))) (buff nil)) (message "Downloading of `%s' complete." url) (url-mark-buffer-as-dead (current-buffer)) (unless headers (setq headers (list (cons 'content-type (mm-handle-media-type handle))))) ;; Fixme: can handle be null? (cond ((or (equal (mm-handle-media-type handle) "text/html") ;; Ultimately this should be handled by an XML parser, but ;; this will mostly work for now: (equal (mm-handle-media-type handle) "application/xhtml+xml")) ;; Special case text/html if it comes through w3-fetch (set-buffer (generate-new-buffer " *w3-html*")) (mm-disable-multibyte) (mm-insert-part handle) (w3-decode-charset handle) (setq url-current-object (url-generic-parse-url url)) (w3-prepare-buffer) (setq url-current-mime-headers headers) (w3-notify-when-ready (current-buffer)) (mm-destroy-parts handle)) ;; ((equal (mm-handle-media-type handle) "text/xml") ;; ;; Special case text/xml if it comes through w3-fetch ;; (set-buffer (generate-new-buffer " *w3-xml*")) ;; (mm-disable-multibyte) ;; (mm-insert-part handle) ;; (w3-decode-charset handle) ;; !!! Need some function to view XML nicely... maybe the ;; !!! customize tree control? ;; (setq url-current-object (url-generic-parse-url url) ;; url-current-mime-headers headers) ;; (mm-destroy-parts handle) ;; (w3-notify-when-ready (current-buffer))) ((equal (car-safe (mm-handle-type handle)) "application/x-elisp-parsed-html") ;; Also need to special-case pre-parsed representations of HTML. ;; Fixme: will this need decoding? (w3-prepare-tree (read (set-marker (make-marker) 1 (mm-handle-buffer handle))))) ((mm-inlinable-p handle) ;; We can view it inline! (set-buffer (generate-new-buffer url)) (require 'mm-view) ; make sure methods are defined (mm-display-part handle) (set-buffer-modified-p nil) (w3-mode) (if (equal "image" (mm-handle-media-supertype handle)) (setq cursor-type nil)) (setq url-current-mime-headers headers) (w3-notify-when-ready (current-buffer))) (t ;; Must be an external viewer (mm-display-part handle) ;;(mm-destroy-parts handle) ))))) ;;;###autoload (defun w3-fetch (&optional url target) "Retrieve a document over the World Wide Web. Defaults to URL of the current document, if any. With prefix argument, use the URL of the hyperlink under point instead." (interactive (list (w3-read-url-with-default))) (w3-do-setup) (if (and (boundp 'command-line-args-left) command-line-args-left (string-match url-nonrelative-link (car command-line-args-left))) (setq url (car command-line-args-left) command-line-args-left (cdr command-line-args-left))) (if (or (null url) (equal url "")) (error "No document specified!")) ;; In the common case, this is probably cheaper than searching. (while (= (string-to-char url) ? ) (setq url (substring url 1))) (or target (setq target w3-base-target)) (if (stringp target) (setq target (intern (downcase target)))) (and target (let ((window-distance (cdr-safe (assq target w3-target-window-distances)))) (if (numberp window-distance) (other-window window-distance) (case target ((_blank external) (w3-fetch-other-frame url)) (_top (delete-other-windows)) (otherwise (message "target %S not found." target)))))) (cond ((= (string-to-char url) ?#) (w3-relative-link url)) ((and (interactive-p) current-prefix-arg) (w3-download-url url)) (t (let ((x (url-view-url t)) (lastbuf (current-buffer)) (w3-current-buffer (current-buffer)) (buf (w3-buffer-visiting url))) (if (or (not buf) (cond ((not (equal (downcase (or url-request-method "GET")) "get")) t) ((memq w3-reuse-buffers '(no never reload)) t) ((memq w3-reuse-buffers '(yes reuse always)) nil) (t (when (and w3-reuse-buffers (not (eq w3-reuse-buffers 'ask))) (ding) (message "Warning: Invalid value for variable w3-reuse-buffers: %s" (prin1-to-string w3-reuse-buffers)) (sit-for 2)) (not (funcall url-confirmation-func (format "Reuse URL in buffer %s? " (buffer-name buf))))))) (url-retrieve url 'w3-fetch-redirect-callback (list url)) (w3-notify-when-ready buf)))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; History for forward/back buttons ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defvar w3-history-stack nil "History stack viewing history. This is an assoc list, with the oldest items first. Each element is a cons cell of (url . timeobj), where URL is the normalized URL (default ports removed, etc), and TIMEOBJ is a standard Emacs time. See the `current-time' function documentation for information on this format.") (defun w3-history-find-url-internal (url) "Search in the history list for URL. Returns a cons cell, where the car is the 'back' node, and the cdr is the 'next' node." (let* ((node (assoc url w3-history-stack)) (next (cadr (memq node w3-history-stack))) (last nil) (temp nil) (todo w3-history-stack)) ;; Last node is a little harder to find without using back links (while (and (not last) todo) (if (string= (caar todo) url) (setq last (or temp 'none)) (setq temp (pop todo)))) (cons (if (not (symbolp last)) last) next))) (defun w3-history-forward () "Go forward in the history from this page." (interactive) (let ((next (cadr (w3-history-find-url-internal (url-view-url t)))) (w3-reuse-buffers 'yes)) (if next (w3-fetch next)))) (defun w3-history-backward () "Go backward in the history from this page." (interactive) (let ((last (caar (w3-history-find-url-internal (url-view-url t)))) (w3-reuse-buffers 'yes)) (if last (w3-fetch last)))) (defun w3-history-push (referer url) "REFERER is the url we followed this link from. URL is the link we got to." (if (not referer) (setq w3-history-stack (list (cons url (current-time)))) (let ((node (memq (assoc referer w3-history-stack) w3-history-stack))) (if node (setcdr node (list (cons url (current-time)))) (setq w3-history-stack (append w3-history-stack (list (cons url (current-time))))))))) (defalias 'w3-add-urls-to-history 'w3-history-push) (defalias 'w3-backward-in-history 'w3-history-backward) (defalias 'w3-forward-in-history 'w3-history-forward) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Miscellaneous functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun w3-describe-entities () "Show an DTD fragment listing all the entities currently defined." (interactive) (switch-to-buffer (get-buffer-create "W3 Entities")) (let ((buffer-file-name (concat (make-temp-name "entities") ".dtd"))) (set-auto-mode)) (erase-buffer) (let (entity) (mapatoms (lambda (x) (setq entity (get x 'html-entity-expansion)) (if entity (insert (format "\n" x (car entity) (cdr entity))))))) (goto-char (point-min))) (defun w3-document-information (&optional buff) "Display information on the document in buffer BUFF." (interactive) (if (interactive-p) (let ((w3-notify 'friendly)) (if (get-buffer "Document Information") (kill-buffer (get-buffer "Document Information"))) (w3-fetch "about:document")) (setq buff (or buff (current-buffer))) (save-excursion (set-buffer buff) (let* ((url (url-view-url t)) (cur-links w3-current-links) (title (buffer-name)) (case-fold-search t) (attributes (url-file-attributes url)) (lastmod (or (cdr-safe (assq 'last-modified url-current-mime-headers)) (nth 5 attributes))) (hdrs url-current-mime-headers) (size (cdr (assq 'content-length url-current-mime-headers))) (info w3-current-metainfo) (links w3-current-links)) (set-buffer (get-buffer-create url-working-buffer)) (setq url-current-can-be-cached nil) (erase-buffer) (if (consp lastmod) (if (equal '(0 . 0) lastmod) (setq lastmod nil) (setq lastmod (current-time-string lastmod)))) (setq url-current-mime-type "text/html") (insert "\ Content-Type: text/html\n Document Information Document Information Title:" title " Location:" url "") (if size (insert "\ Size:" (url-pretty-length (if (stringp size) (string-to-int size) size)) "")) (insert "\ Last Modified:" (or lastmod "None Given") "\n") (when hdrs (setq hdrs (delete (assq 'last-modified hdrs) hdrs)) (setq hdrs (delete (assq 'content-length hdrs) hdrs)) (setq hdrs (mapcar (lambda (pair) (cons (symbol-name (car pair)) (cdr pair))) hdrs))) (let* ((maxlength (car (sort (mapcar (lambda (x) (length (car x))) hdrs) '>))) (fmtstring (format "\ %%%ds:%%s" maxlength))) (insert "\ MetaInformation\n" (mapconcat (lambda (x) (if (/= (length (car x)) 0) (format fmtstring (url-insert-entities-in-string (capitalize (car x))) (url-insert-entities-in-string (if (numberp (cdr x)) (int-to-string (cdr x)) (cdr x)))))) (sort hdrs (lambda (x y) (string-lessp (car x) (car y)))) "\n"))) (when links ;; collapse `rel' and `rev' components (setq links (apply 'append (mapcar 'cdr links))) ;; extract (setq links (mapcar (lambda (elt) (cons (or (plist-get (cadr elt) 'title) (car elt)) (plist-get (cadr elt) 'href))) links)) (let* ((maxlength (car (sort (mapcar (lambda (x) (length (car x))) links) '>))) (fmtstring (format " %%%ds:%%s" maxlength))) (insert " Document Links\n") (while links (if (and (caar links) (cdar links)) (insert (format fmtstring (url-insert-entities-in-string (capitalize (caar links))) (url-insert-entities-in-string (cdar links)) (url-insert-entities-in-string (cdar links))) "\n")) (setq links (cdr links))))) (if info (let* ((maxlength (car (sort (mapcar (lambda (x) (length (car x))) info) '>))) (fmtstring (format " %%%ds:%%s" maxlength))) (insert " Miscellaneous Variables\n") (while info (if (and (caar info) (cdar info)) (insert (format fmtstring (url-insert-entities-in-string (capitalize (caar info))) (url-insert-entities-in-string (cdar info))) "\n")) (setq info (cdr info))))) (insert " \n") (current-buffer))))) (defun w3-insert-formatted-url (p) "Insert a formatted url into a buffer. With prefix arg, insert the url under point." (interactive "P") (let (buff str) (cond (p (setq p (widget-at (point))) (or p (error "No url under point")) (setq str (format "%s" (widget-get p :href) (read-string "Link text: " (buffer-substring (widget-get p :from) (widget-get p :to)))))) (t (setq str (format "%s" (url-view-url t) (read-string "Link text: " (buffer-name)))))) (setq buff (read-buffer "Insert into buffer: " nil t)) (if buff (save-excursion (set-buffer buff) (insert str)) (message "Cancelled.")))) (defun w3-first-n-items (l n) "Return the first N items from list L." (let ((x 0) y) (if (> n (length l)) (setq y l) (while (< x n) (setq y (nconc y (list (nth x l))) x (1+ x)))) y)) (defun w3-widget-button-press () (interactive) (if (widget-at (point)) (widget-button-press (point)))) (defun w3-widget-button-click (e) (interactive "@e") (if (featurep 'xemacs) (cond ((and (event-point e) (widget-at (event-point e))) (widget-button-click e)) ((and (fboundp 'event-glyph) (event-glyph e) (glyph-property (event-glyph e) 'widget)) (widget-button-click e))) (save-excursion (mouse-set-point e) (if (widget-at (point)) (widget-button-click e))))) ;;;###autoload (defun w3-maybe-follow-link-mouse (e) "Maybe follow a hypertext link under point. If there is no link under point, this will try using `url-get-url-at-point'" (interactive "e") (save-excursion (mouse-set-point e) (w3-maybe-follow-link))) ;;;###autoload (defun w3-maybe-follow-link () "Maybe follow a hypertext link under point. If there is no link under point, this will try using `url-get-url-at-point'" (interactive) (require 'w3) (w3-do-setup) (let* ((widget (widget-at (point))) (url1 (and widget (widget-get widget :href))) (url2 (url-get-url-at-point))) (cond (url1 (widget-button-press (point))) ((and url2 (string-match url-nonrelative-link url2)) (w3-fetch url2)) (t (message "No URL could be found!"))))) ;;;###autoload (defun w3-follow-url-at-point-other-frame (&optional pt) "Follow the URL under PT, defaults to link under (point)." (interactive "d") (let ((url (url-get-url-at-point pt))) (and url (w3-fetch-other-frame url)))) ;;;###autoload (defun w3-follow-url-at-point (&optional pt) "Follow the URL under PT, defaults to link under (point)." (interactive "d") (let ((url (url-get-url-at-point pt))) (and url (w3-fetch url)))) (defun w3-fix-spaces (string) "Remove spaces/tabs at beginning of STRING and convert newlines to spaces." ;(url-convert-newlines-to-spaces (url-strip-leading-spaces (url-eat-trailing-space string)));) (defun w3-source-document-at-point () "View source to the document pointed at by link under point." (interactive) (w3-source-document t)) (defun w3-source-document (under) "View this document's source." (interactive "P") (let* ((url (if under (w3-view-this-url) (url-view-url t)))) (set-buffer (generate-new-buffer (concat "Source of: " url))) (url-insert-file-contents url) (put-text-property (point-min) (point-max) 'w3-base url) (goto-char (point-min)) (setq buffer-file-truename url buffer-file-name url) ;; Null filename bugs `set-auto-mode' in Mule ... (condition-case () (set-auto-mode) (error nil)) (setq buffer-file-truename nil buffer-file-name nil) (buffer-enable-undo) (set-buffer-modified-p nil) (w3-notify-when-ready (current-buffer))) (run-hooks 'w3-source-file-hook)) (defun w3-mail-document-under-point () "Mail the document pointed to by the hyperlink under point." (interactive) (w3-mail-current-document t)) (defun w3-mail-current-document (under &optional format) "Mail the current-document to someone." (interactive "P") (let* ((completion-ignore-case t) (format (or format (completing-read "Format: " '(("HTML Source") ("Formatted Text") ("PostScript") ) nil t))) (case-fold-search t) (url (cond ((stringp under) under) (under (w3-view-this-url t)) (t (url-view-url t)))) (content-charset (or (and (boundp 'buffer-file-coding-system) (symbol-value buffer-file-coding-system) (symbol-name buffer-file-coding-system)) "iso-8859-1")) (content-type (concat "text/plain; charset=" content-charset)) (str (save-excursion (cond ((and (equal "HTML Source" format) under) (setq content-type (concat "text/html; charset=" content-charset)) (let ((url-source t)) ;; Fixme: this needs a callback -- which? (url-retrieve url))) ((equal "HTML Source" format) (setq content-type (concat "text/html; charset=" content-charset)) (if w3-current-source (let ((x w3-current-source)) (set-buffer (get-buffer-create url-working-buffer)) (erase-buffer) (insert x)) ;; Fixme: this needs a callback -- which? (url-retrieve url))) ((and under (equal "PostScript" format)) (setq content-type "application/postscript") (w3-fetch url) (require 'ps-print) (let ((ps-spool-buffer-name " *w3-temp*")) (if (get-buffer ps-spool-buffer-name) (kill-buffer ps-spool-buffer-name)) (ps-spool-buffer-with-faces) (set-buffer ps-spool-buffer-name))) ((equal "PostScript" format) (require 'ps-print) (let ((ps-spool-buffer-name " *w3-temp*")) (if (get-buffer ps-spool-buffer-name) (kill-buffer ps-spool-buffer-name)) (setq content-type "application/postscript") (ps-spool-buffer-with-faces) (set-buffer ps-spool-buffer-name))) ((and under (equal "Formatted Text" format)) (setq content-type (concat "text/plain; charset=" content-charset)) (w3-fetch url)) ((equal "Formatted Text" format) (setq content-type (concat "text/plain; charset=" content-charset)))) (buffer-string)))) (funcall url-mail-command) (mail-subject) (if (and (boundp 'mime/editor-mode-flag) mime/editor-mode-flag) (insert format " from ") (insert format " from \n" "Mime-Version: 1.0\n" "Content-transfer-encoding: 8bit\n" "Content-type: " content-type)) (re-search-forward mail-header-separator nil) (forward-char 1) (if (and (boundp 'mime/editor-mode-flag) mime/editor-mode-flag) (insert (format mime-tag-format content-type) "\n")) (save-excursion (insert str)) (cond ((equal "HTML Source" format) (if (or (search-forward "" nil t) (search-forward "" nil t)) (insert "\n")) (insert (format "" url)))) ;; Fixme: not defined. (mail-to))) (defun w3-internal-use-history (hist-item) ;; Go to the link in the history (let ((url (nth 0 hist-item)) (buf (nth 1 hist-item)) (pnt (nth 2 hist-item))) (cond ((null buf) ; Find a buffer with same url (let ((x (buffer-list)) (found nil)) (while (and x (not found)) (save-excursion (set-buffer (car x)) (setq found (string= (url-view-url t) url)) (if (not found) (setq x (cdr x))))) (cond (found (switch-to-buffer (car x)) (if (number-or-marker-p pnt) (goto-char pnt))) (t (w3-fetch url))))) ((buffer-name buf) ; Reuse the old buffer if possible (switch-to-buffer buf) (if (number-or-marker-p pnt) (goto-char pnt)) (if (and url (= ?# (string-to-char url))) ; Destination link (progn (goto-char (point-min)) (w3-find-specific-link (substring url 1 nil))))) ;; Fixme: url-maybe-relative not defined. (url (url-maybe-relative url)) ; Get the link (t (message "Couldn't understand whats in the history."))))) (defun w3-relative-link (url) (if (equal "#" (substring url 0 1)) (progn (push-mark (point) t) (goto-char (point-min)) (w3-find-specific-link (substring url 1 nil))) (w3-fetch (url-expand-file-name url)))) (defun w3-maybe-eval () "Maybe evaluate a buffer of Emacs Lisp code." (if (funcall url-confirmation-func "This is emacs-lisp code, evaluate it?") (eval-buffer (current-buffer)) (emacs-lisp-mode))) (defun w3-use-links () "Select one of the tags from this document and fetch it." (interactive) (and (not w3-current-links) (error "No links defined for this document")) (w3-fetch "about:document")) (defun w3-find-this-file () "Do a `find-file' on the currently viewed html document. Do this if it is a file: or ftp: reference" (interactive) (or url-current-object (error "Not a URL-based buffer")) (let ((type (url-type url-current-object))) (cond ((equal type "file") (find-file (url-filename url-current-object))) ((equal type "ftp") (find-file (format "/%s%s:%s" (if (url-user url-current-object) (concat (url-user url-current-object) "@")) (url-host url-current-object) (url-filename url-current-object)))) (t (message "Sorry, I can't get that file so you can alter it."))))) (defun w3-insert-this-url (pref-arg) "Insert the current url in another buffer. With prefix ARG, insert URL under point" (interactive "P") (let ((thebuf (get-buffer (read-buffer "Insert into buffer: "))) (oldbuf (current-buffer)) (url (if pref-arg (w3-view-this-url t) (url-view-url t)))) (if (and url (not (equal "Not on a link!" url))) (progn (set-buffer thebuf) (insert url) (set-buffer oldbuf)) (message "Not on a link!")))) (defun w3-in-assoc (elt list) "Check to see if ELT matches any of the regexps in the car elements of LIST." (let (rslt) (while (and list (not rslt)) (and (car (car list)) (stringp (car (car list))) (not (string= (car (car list)) "")) (string-match (car (car list)) elt) (setq rslt (car list))) (setq list (cdr list))) rslt)) (defun w3-goto-last-buffer () "Go to last WWW buffer visited." (interactive) (if w3-current-last-buffer (if w3-frame-name (progn (delete-other-windows) (set-buffer w3-current-last-buffer) (w3-goto-last-buffer)) (w3-notify-when-ready w3-current-last-buffer)) (message "No previous buffer found."))) (fset 'w3-replace-regexp 'url-replace-regexp) ;;;###autoload (defun w3-preview-this-buffer () "See what this buffer will look like when its formatted as HTML. HTML is the HyperText Markup Language used by the World Wide Web to specify formatting for text. More information on HTML can be found at ftp.w3.org:/pub/www/doc." (interactive) (w3-fetch (concat "www://preview/" (buffer-name)))) (defun w3-source () "Show the source of a file." (let ((tmp (buffer-name (generate-new-buffer "Document Source")))) (set-buffer url-working-buffer) (kill-buffer tmp) (rename-buffer tmp) ;; Make the URL show in list-buffers output (make-local-variable 'list-buffers-directory) (setq list-buffers-directory (url-view-url t)) (set-buffer-modified-p nil) (buffer-enable-undo) (w3-notify-when-ready (get-buffer tmp)))) (defvar w3-mime-list-for-code-conversion '("text/plain" "text/html") "List of MIME types that require Mules' code conversion.") (defvar w3-compression-encodings '("x-gzip" "gzip" "x-compress" "compress") "List of MIME encodings that denote compression.") ;; This looks bogus -- fx (defvar w3-no-conversion-encodings w3-compression-encodings "List of MIME encodings that require Mule not to convert even though the MIME type is nil or listed in `w3-mime-list-for-code-conversion'.") (defun w3-show-history-list () "Format the url-history-list prettily and show it to the user." (interactive) (w3-fetch "www://auto/history")) (defun w3-save-as (&optional type) "Save a document to the local disk." (interactive) (save-excursion (let* ((completion-ignore-case t) (format (or type (completing-read "Format: " '(("HTML Source") ("Formatted Text") ("PostScript") ("Binary")) nil t))) (fname (expand-file-name (read-file-name "File name: " default-directory))) (source w3-current-source) (text (buffer-string)) (url (url-view-url t))) (set-buffer (generate-new-buffer " *w3-save-as*")) (cond ((equal "Binary" format) (insert source)) ((equal "HTML Source" format) (insert source) (goto-char (point-min)) (if (re-search-forward "" nil t) (insert "\n")) (insert (format "\n" url))) ((or (equal "Formatted Text" format) (equal "" format)) (insert text)) ((equal "PostScript" format) (require 'ps-print) (let ((ps-spool-buffer-name (buffer-name))) (ps-spool-buffer-with-faces)))) (let ((coding-system-for-write 'binary)) (write-region (point-min) (point-max) fname)) (kill-buffer (current-buffer))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Functions for logging of bad HTML ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun w3-reconstruct-tag (tagname desc) (concat "<" tagname " " (mapconcat (function (lambda (x) (if (cdr x) (concat (car x) "=\"" (cdr x) "\"") (car x)))) desc " ") ">")) (defun w3-debug-if-found (regexp type desc) (and w3-debug-html (save-excursion (if (re-search-forward regexp nil t) (w3-log-bad-html type desc))))) (defun w3-log-bad-html (type desc) "Log bad HTML to the buffer specified by w3-debug-buffer." (if w3-debug-html (save-excursion (set-buffer (get-buffer-create w3-debug-buffer)) (goto-char (point-max)) (insert (make-string (1- (window-width)) w3-horizontal-rule-char) "\n") (cond ((stringp type) (insert type "\n" desc "\n")) ((eq type 'bad-quote) (insert "Unterminated quoting character in SGML attribute value.\n" desc "\n")) ((eq type 'no-quote) (insert "Unquoted SGML attribute value.\n" desc "\n")) ((eq type 'no-textarea-end) (insert "Unterminated