cl-lib.el: Partial revert of "2015-04-05 Rationalize c[ad]+r"
[emacs.git] / lisp / net / eww.el
bloba128ffb9d065731b65f2e7c1c50f151ff3631d51
1 ;;; eww.el --- Emacs Web Wowser -*- lexical-binding:t -*-
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
28 (require 'format-spec)
29 (require 'shr)
30 (require 'url)
31 (require 'url-queue)
32 (require 'url-util) ; for url-get-url-at-point
33 (require 'mm-url)
34 (eval-when-compile (require 'subr-x)) ;; for string-trim
36 (defgroup eww nil
37 "Emacs Web Wowser"
38 :version "25.1"
39 :link '(custom-manual "(eww) Top")
40 :group 'web
41 :prefix "eww-")
43 (defcustom eww-header-line-format "%t: %u"
44 "Header line format.
45 - %t is replaced by the title.
46 - %u is replaced by the URL."
47 :version "24.4"
48 :group 'eww
49 :type 'string)
51 (defcustom eww-search-prefix "https://duckduckgo.com/html/?q="
52 "Prefix URL to search engine."
53 :version "24.4"
54 :group 'eww
55 :type 'string)
57 (defcustom eww-download-directory "~/Downloads/"
58 "Directory where files will downloaded."
59 :version "24.4"
60 :group 'eww
61 :type 'string)
63 ;;;###autoload
64 (defcustom eww-suggest-uris
65 '(eww-links-at-point
66 url-get-url-at-point
67 eww-current-url)
68 "List of functions called to form the list of default URIs for `eww'.
69 Each of the elements is a function returning either a string or a list
70 of strings. The results will be joined into a single list with
71 duplicate entries (if any) removed."
72 :version "25.1"
73 :group 'eww
74 :type 'hook
75 :options '(eww-links-at-point
76 url-get-url-at-point
77 eww-current-url))
79 (defcustom eww-bookmarks-directory user-emacs-directory
80 "Directory where bookmark files will be stored."
81 :version "25.1"
82 :group 'eww
83 :type 'string)
85 (defcustom eww-desktop-remove-duplicates t
86 "Whether to remove duplicates from the history when saving desktop data.
87 If non-nil, repetitive EWW history entries (comprising of the URI, the
88 title, and the point position) will not be saved as part of the Emacs
89 desktop. Otherwise, such entries will be retained."
90 :version "25.1"
91 :group 'eww
92 :type 'boolean)
94 (defcustom eww-restore-desktop nil
95 "How to restore EWW buffers on `desktop-restore'.
96 If t or 'auto, the buffers will be reloaded automatically.
97 If nil, buffers will require manual reload, and will contain the text
98 specified in `eww-restore-reload-prompt' instead of the actual Web
99 page contents."
100 :version "25.1"
101 :group 'eww
102 :type '(choice (const :tag "Restore all automatically" t)
103 (const :tag "Require manual reload" nil)))
105 (defcustom eww-restore-reload-prompt
106 "\n\n *** Use \\[eww-reload] to reload this buffer. ***\n"
107 "The string to put in the buffers not reloaded on `desktop-restore'.
108 This prompt will be used if `eww-restore-desktop' is nil.
110 The string will be passed through `substitute-command-keys'."
111 :version "25.1"
112 :group 'eww
113 :type 'string)
115 (defcustom eww-history-limit 50
116 "Maximum number of entries to retain in the history."
117 :version "25.1"
118 :group 'eww
119 :type '(choice (const :tag "Unlimited" nil)
120 integer))
122 (defcustom eww-use-external-browser-for-content-type
123 "\\`\\(video/\\|audio/\\|application/ogg\\)"
124 "Always use external browser for specified content-type."
125 :version "24.4"
126 :group 'eww
127 :type '(choice (const :tag "Never" nil)
128 regexp))
130 (defcustom eww-after-render-hook nil
131 "A hook called after eww has finished rendering the buffer."
132 :version "25.1"
133 :group 'eww
134 :type 'hook)
136 (defcustom eww-form-checkbox-selected-symbol "[X]"
137 "Symbol used to represent a selected checkbox.
138 See also `eww-form-checkbox-symbol'."
139 :version "24.4"
140 :group 'eww
141 :type '(choice (const "[X]")
142 (const "☒") ; Unicode BALLOT BOX WITH X
143 (const "☑") ; Unicode BALLOT BOX WITH CHECK
144 string))
146 (defcustom eww-form-checkbox-symbol "[ ]"
147 "Symbol used to represent a checkbox.
148 See also `eww-form-checkbox-selected-symbol'."
149 :version "24.4"
150 :group 'eww
151 :type '(choice (const "[ ]")
152 (const "☐") ; Unicode BALLOT BOX
153 string))
155 (defface eww-form-submit
156 '((((type x w32 ns) (class color)) ; Like default mode line
157 :box (:line-width 2 :style released-button)
158 :background "#808080" :foreground "black"))
159 "Face for eww buffer buttons."
160 :version "24.4"
161 :group 'eww)
163 (defface eww-form-file
164 '((((type x w32 ns) (class color)) ; Like default mode line
165 :box (:line-width 2 :style released-button)
166 :background "#808080" :foreground "black"))
167 "Face for eww buffer buttons."
168 :version "25.1"
169 :group 'eww)
171 (defface eww-form-checkbox
172 '((((type x w32 ns) (class color)) ; Like default mode line
173 :box (:line-width 2 :style released-button)
174 :background "lightgrey" :foreground "black"))
175 "Face for eww buffer buttons."
176 :version "24.4"
177 :group 'eww)
179 (defface eww-form-select
180 '((((type x w32 ns) (class color)) ; Like default mode line
181 :box (:line-width 2 :style released-button)
182 :background "lightgrey" :foreground "black"))
183 "Face for eww buffer buttons."
184 :version "24.4"
185 :group 'eww)
187 (defface eww-form-text
188 '((t (:background "#505050"
189 :foreground "white"
190 :box (:line-width 1))))
191 "Face for eww text inputs."
192 :version "24.4"
193 :group 'eww)
195 (defface eww-form-textarea
196 '((t (:background "#C0C0C0"
197 :foreground "black"
198 :box (:line-width 1))))
199 "Face for eww textarea inputs."
200 :version "24.4"
201 :group 'eww)
203 (defface eww-invalid-certificate
204 '((default :weight bold)
205 (((class color)) :foreground "red"))
206 "Face for web pages with invalid certificates."
207 :version "25.1"
208 :group 'eww)
210 (defface eww-valid-certificate
211 '((default :weight bold)
212 (((class color)) :foreground "ForestGreen"))
213 "Face for web pages with valid certificates."
214 :version "25.1"
215 :group 'eww)
217 (defvar eww-data nil)
218 (defvar eww-history nil)
219 (defvar eww-history-position 0)
221 (defvar eww-local-regex "localhost"
222 "When this regex is found in the URL, it's not a keyword but an address.")
224 (defvar eww-link-keymap
225 (let ((map (copy-keymap shr-map)))
226 (define-key map "\r" 'eww-follow-link)
227 map))
229 (defun eww-suggested-uris nil
230 "Return the list of URIs to suggest at the `eww' prompt.
231 This list can be customized via `eww-suggest-uris'."
232 (let ((obseen (make-vector 42 0))
233 (uris nil))
234 (dolist (fun eww-suggest-uris)
235 (let ((ret (funcall fun)))
236 (dolist (uri (if (stringp ret) (list ret) ret))
237 (when (and uri (not (intern-soft uri obseen)))
238 (intern uri obseen)
239 (push uri uris)))))
240 (nreverse uris)))
242 ;;;###autoload
243 (defun eww (url)
244 "Fetch URL and render the page.
245 If the input doesn't look like an URL or a domain name, the
246 word(s) will be searched for via `eww-search-prefix'."
247 (interactive
248 (let* ((uris (eww-suggested-uris))
249 (prompt (concat "Enter URL or keywords"
250 (if uris (format " (default %s)" (car uris)) "")
251 ": ")))
252 (list (read-string prompt nil nil uris))))
253 (setq url (string-trim url))
254 (cond ((string-match-p "\\`file:/" url))
255 ;; Don't mangle file: URLs at all.
256 ((string-match-p "\\`ftp://" url)
257 (user-error "FTP is not supported"))
259 ;; Anything that starts with something that vaguely looks
260 ;; like a protocol designator is interpreted as a full URL.
261 (if (or (string-match "\\`[A-Za-z]+:" url)
262 ;; Also try to match "naked" URLs like
263 ;; en.wikipedia.org/wiki/Free software
264 (string-match "\\`[A-Za-z_]+\\.[A-Za-z._]+/" url)
265 (and (= (length (split-string url)) 1)
266 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url))
267 (> (length (split-string url "[.:]")) 1))
268 (string-match eww-local-regex url))))
269 (progn
270 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
271 (setq url (concat "http://" url)))
272 ;; Some sites do not redirect final /
273 (when (string= (url-filename (url-generic-parse-url url)) "")
274 (setq url (concat url "/"))))
275 (setq url (concat eww-search-prefix
276 (replace-regexp-in-string " " "+" url))))))
277 (if (eq major-mode 'eww-mode)
278 (when (or (plist-get eww-data :url)
279 (plist-get eww-data :dom))
280 (eww-save-history))
281 (eww-setup-buffer)
282 (plist-put eww-data :url url)
283 (plist-put eww-data :title "")
284 (eww-update-header-line-format)
285 (let ((inhibit-read-only t))
286 (insert (format "Loading %s..." url))
287 (goto-char (point-min))))
288 (url-retrieve url 'eww-render
289 (list url nil (current-buffer))))
291 ;;;###autoload (defalias 'browse-web 'eww)
293 ;;;###autoload
294 (defun eww-open-file (file)
295 "Render FILE using EWW."
296 (interactive "fFile: ")
297 (eww (concat "file://"
298 (and (memq system-type '(windows-nt ms-dos))
299 "/")
300 (expand-file-name file))))
302 ;;;###autoload
303 (defun eww-search-words (&optional beg end)
304 "Search the web for the text between BEG and END.
305 See the `eww-search-prefix' variable for the search engine used."
306 (interactive "r")
307 (eww (buffer-substring beg end)))
309 (defun eww-html-p (content-type)
310 "Return non-nil if CONTENT-TYPE designates an HTML content type.
311 Currently this means either text/html or application/xhtml+xml."
312 (member content-type '("text/html"
313 "application/xhtml+xml")))
315 (defun eww-render (status url &optional point buffer encode)
316 (let ((redirect (plist-get status :redirect)))
317 (when redirect
318 (setq url redirect)))
319 (let* ((headers (eww-parse-headers))
320 (content-type
321 (mail-header-parse-content-type
322 (or (cdr (assoc "content-type" headers))
323 "text/plain")))
324 (charset (intern
325 (downcase
326 (or (cdr (assq 'charset (cdr content-type)))
327 (eww-detect-charset (eww-html-p (car content-type)))
328 "utf-8"))))
329 (data-buffer (current-buffer)))
330 ;; Save the https peer status.
331 (with-current-buffer buffer
332 (plist-put eww-data :peer (plist-get status :peer)))
333 (unwind-protect
334 (progn
335 (cond
336 ((and eww-use-external-browser-for-content-type
337 (string-match-p eww-use-external-browser-for-content-type
338 (car content-type)))
339 (eww-browse-with-external-browser url))
340 ((eww-html-p (car content-type))
341 (eww-display-html charset url nil point buffer encode))
342 ((equal (car content-type) "application/pdf")
343 (eww-display-pdf))
344 ((string-match-p "\\`image/" (car content-type))
345 (eww-display-image buffer))
347 (eww-display-raw buffer encode)))
348 (with-current-buffer buffer
349 (plist-put eww-data :url url)
350 (eww-update-header-line-format)
351 (setq eww-history-position 0)
352 (run-hooks 'eww-after-render-hook)))
353 (kill-buffer data-buffer))))
355 (defun eww-parse-headers ()
356 (let ((headers nil))
357 (goto-char (point-min))
358 (while (and (not (eobp))
359 (not (eolp)))
360 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
361 (push (cons (downcase (match-string 1))
362 (match-string 2))
363 headers))
364 (forward-line 1))
365 (unless (eobp)
366 (forward-line 1))
367 headers))
369 (defun eww-detect-charset (html-p)
370 (let ((case-fold-search t)
371 (pt (point)))
372 (or (and html-p
373 (re-search-forward
374 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
375 (goto-char pt)
376 (match-string 1))
377 (and (looking-at
378 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
379 (match-string 1)))))
381 (declare-function libxml-parse-html-region "xml.c"
382 (start end &optional base-url))
384 (defun eww-display-html (charset url &optional document point buffer encode)
385 (unless (fboundp 'libxml-parse-html-region)
386 (error "This function requires Emacs to be compiled with libxml2"))
387 (unless (buffer-live-p buffer)
388 (error "Buffer %s doesn't exist" buffer))
389 ;; There should be a better way to abort loading images
390 ;; asynchronously.
391 (setq url-queue nil)
392 (let ((document
393 (or document
394 (list
395 'base (list (cons 'href url))
396 (progn
397 (when (or (and encode
398 (not (eq charset encode)))
399 (not (eq charset 'utf-8)))
400 (condition-case nil
401 (decode-coding-region (point) (point-max)
402 (or encode charset))
403 (coding-system-error nil)))
404 (libxml-parse-html-region (point) (point-max))))))
405 (source (and (null document)
406 (buffer-substring (point) (point-max)))))
407 (with-current-buffer buffer
408 (plist-put eww-data :source source)
409 (plist-put eww-data :dom document)
410 (let ((inhibit-read-only t)
411 (inhibit-modification-hooks t)
412 (shr-target-id (url-target (url-generic-parse-url url)))
413 (shr-external-rendering-functions
414 '((title . eww-tag-title)
415 (form . eww-tag-form)
416 (input . eww-tag-input)
417 (textarea . eww-tag-textarea)
418 (select . eww-tag-select)
419 (link . eww-tag-link)
420 (a . eww-tag-a))))
421 (erase-buffer)
422 (shr-insert-document document)
423 (cond
424 (point
425 (goto-char point))
426 (shr-target-id
427 (goto-char (point-min))
428 (let ((point (next-single-property-change
429 (point-min) 'shr-target-id)))
430 (when point
431 (goto-char point))))
433 (goto-char (point-min))
434 ;; Don't leave point inside forms, because the normal eww
435 ;; commands aren't available there.
436 (while (and (not (eobp))
437 (get-text-property (point) 'eww-form))
438 (forward-line 1)))))
439 (eww-size-text-inputs))))
441 (defun eww-handle-link (dom)
442 (let* ((rel (dom-attr dom 'rel))
443 (href (dom-attr dom 'href))
444 (where (assoc
445 ;; The text associated with :rel is case-insensitive.
446 (if rel (downcase rel))
447 '(("next" . :next)
448 ;; Texinfo uses "previous", but HTML specifies
449 ;; "prev", so recognize both.
450 ("previous" . :previous)
451 ("prev" . :previous)
452 ;; HTML specifies "start" but also "contents",
453 ;; and Gtk seems to use "home". Recognize
454 ;; them all; but store them in different
455 ;; variables so that we can readily choose the
456 ;; "best" one.
457 ("start" . :start)
458 ("home" . :home)
459 ("contents" . :contents)
460 ("up" . :up)))))
461 (and href
462 where
463 (plist-put eww-data (cdr where) href))))
465 (defun eww-tag-link (dom)
466 (eww-handle-link dom)
467 (shr-generic dom))
469 (defun eww-tag-a (dom)
470 (eww-handle-link dom)
471 (let ((start (point)))
472 (shr-tag-a dom)
473 (put-text-property start (point) 'keymap eww-link-keymap)))
475 (defun eww-update-header-line-format ()
476 (setq header-line-format
477 (and eww-header-line-format
478 (let ((title (plist-get eww-data :title))
479 (peer (plist-get eww-data :peer)))
480 (when (zerop (length title))
481 (setq title "[untitled]"))
482 ;; This connection has is https.
483 (when peer
484 (setq title
485 (propertize title 'face
486 (if (plist-get peer :warnings)
487 'eww-invalid-certificate
488 'eww-valid-certificate))))
489 (replace-regexp-in-string
490 "%" "%%"
491 (format-spec
492 eww-header-line-format
493 `((?u . ,(or (plist-get eww-data :url) ""))
494 (?t . ,title))))))))
496 (defun eww-tag-title (dom)
497 (plist-put eww-data :title
498 (replace-regexp-in-string
499 "^ \\| $" ""
500 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
501 (eww-update-header-line-format))
503 (defun eww-display-raw (buffer &optional encode)
504 (let ((data (buffer-substring (point) (point-max))))
505 (unless (buffer-live-p buffer)
506 (error "Buffer %s doesn't exist" buffer))
507 (with-current-buffer buffer
508 (let ((inhibit-read-only t))
509 (erase-buffer)
510 (insert data)
511 (unless (eq encode 'utf-8)
512 (encode-coding-region (point-min) (1+ (length data)) 'utf-8)
513 (condition-case nil
514 (decode-coding-region (point-min) (1+ (length data)) encode)
515 (coding-system-error nil))))
516 (goto-char (point-min)))))
518 (defun eww-display-image (buffer)
519 (let ((data (shr-parse-image-data)))
520 (unless (buffer-live-p buffer)
521 (error "Buffer %s doesn't exist" buffer))
522 (with-current-buffer buffer
523 (let ((inhibit-read-only t))
524 (erase-buffer)
525 (shr-put-image data nil))
526 (goto-char (point-min)))))
528 (declare-function mailcap-view-mime "mailcap" (type))
529 (defun eww-display-pdf ()
530 (let ((data (buffer-substring (point) (point-max))))
531 (switch-to-buffer (get-buffer-create "*eww pdf*"))
532 (let ((coding-system-for-write 'raw-text)
533 (inhibit-read-only t))
534 (erase-buffer)
535 (insert data)
536 (mailcap-view-mime "application/pdf")))
537 (goto-char (point-min)))
539 (defun eww-setup-buffer ()
540 (switch-to-buffer (get-buffer-create "*eww*"))
541 (let ((inhibit-read-only t))
542 (remove-overlays)
543 (erase-buffer))
544 (unless (eq major-mode 'eww-mode)
545 (eww-mode)))
547 (defun eww-current-url nil
548 "Return URI of the Web page the current EWW buffer is visiting."
549 (plist-get eww-data :url))
551 (defun eww-links-at-point ()
552 "Return list of URIs, if any, linked at point."
553 (remq nil
554 (list (get-text-property (point) 'shr-url)
555 (get-text-property (point) 'image-url))))
557 (defun eww-view-source ()
558 "View the HTML source code of the current page."
559 (interactive)
560 (let ((buf (get-buffer-create "*eww-source*"))
561 (source (plist-get eww-data :source)))
562 (with-current-buffer buf
563 (let ((inhibit-read-only t))
564 (delete-region (point-min) (point-max))
565 (insert (or source "no source"))
566 (goto-char (point-min))
567 (when (fboundp 'html-mode)
568 (html-mode))))
569 (view-buffer buf)))
571 (defun eww-readable ()
572 "View the main \"readable\" parts of the current web page.
573 This command uses heuristics to find the parts of the web page that
574 contains the main textual portion, leaving out navigation menus and
575 the like."
576 (interactive)
577 (let* ((old-data eww-data)
578 (dom (with-temp-buffer
579 (insert (plist-get old-data :source))
580 (condition-case nil
581 (decode-coding-region (point-min) (point-max) 'utf-8)
582 (coding-system-error nil))
583 (libxml-parse-html-region (point-min) (point-max)))))
584 (eww-score-readability dom)
585 (eww-save-history)
586 (eww-display-html nil nil
587 (eww-highest-readability dom)
588 nil (current-buffer))
589 (dolist (elem '(:source :url :title :next :previous :up))
590 (plist-put eww-data elem (plist-get old-data elem)))
591 (eww-update-header-line-format)))
593 (defun eww-score-readability (node)
594 (let ((score -1))
595 (cond
596 ((memq (dom-tag node) '(script head comment))
597 (setq score -2))
598 ((eq (dom-tag node) 'meta)
599 (setq score -1))
600 ((eq (dom-tag node) 'img)
601 (setq score 2))
602 ((eq (dom-tag node) 'a)
603 (setq score (- (length (split-string (dom-text node))))))
605 (dolist (elem (dom-children node))
606 (if (stringp elem)
607 (setq score (+ score (length (split-string elem))))
608 (setq score (+ score
609 (or (cdr (assoc :eww-readability-score (cdr elem)))
610 (eww-score-readability elem))))))))
611 ;; Cache the score of the node to avoid recomputing all the time.
612 (dom-set-attribute node :eww-readability-score score)
613 score))
615 (defun eww-highest-readability (node)
616 (let ((result node)
617 highest)
618 (dolist (elem (dom-non-text-children node))
619 (when (> (or (dom-attr
620 (setq highest (eww-highest-readability elem))
621 :eww-readability-score)
622 most-negative-fixnum)
623 (or (dom-attr result :eww-readability-score)
624 most-negative-fixnum))
625 (setq result highest)))
626 result))
628 (defvar eww-mode-map
629 (let ((map (make-sparse-keymap)))
630 (set-keymap-parent map special-mode-map)
631 (define-key map "g" 'eww-reload) ;FIXME: revert-buffer-function instead!
632 (define-key map "G" 'eww)
633 (define-key map [?\t] 'shr-next-link)
634 (define-key map [?\M-\t] 'shr-previous-link)
635 (define-key map [backtab] 'shr-previous-link)
636 (define-key map [delete] 'scroll-down-command)
637 (define-key map "l" 'eww-back-url)
638 (define-key map "r" 'eww-forward-url)
639 (define-key map "n" 'eww-next-url)
640 (define-key map "p" 'eww-previous-url)
641 (define-key map "u" 'eww-up-url)
642 (define-key map "t" 'eww-top-url)
643 (define-key map "&" 'eww-browse-with-external-browser)
644 (define-key map "d" 'eww-download)
645 (define-key map "w" 'eww-copy-page-url)
646 (define-key map "C" 'url-cookie-list)
647 (define-key map "v" 'eww-view-source)
648 (define-key map "R" 'eww-readable)
649 (define-key map "H" 'eww-list-histories)
650 (define-key map "E" 'eww-set-character-encoding)
651 (define-key map "S" 'eww-list-buffers)
652 (define-key map "F" 'eww-toggle-fonts)
654 (define-key map "b" 'eww-add-bookmark)
655 (define-key map "B" 'eww-list-bookmarks)
656 (define-key map [(meta n)] 'eww-next-bookmark)
657 (define-key map [(meta p)] 'eww-previous-bookmark)
659 (easy-menu-define nil map ""
660 '("Eww"
661 ["Exit" quit-window t]
662 ["Close browser" quit-window t]
663 ["Reload" eww-reload t]
664 ["Back to previous page" eww-back-url
665 :active (not (zerop (length eww-history)))]
666 ["Forward to next page" eww-forward-url
667 :active (not (zerop eww-history-position))]
668 ["Browse with external browser" eww-browse-with-external-browser t]
669 ["Download" eww-download t]
670 ["View page source" eww-view-source]
671 ["Copy page URL" eww-copy-page-url t]
672 ["List histories" eww-list-histories t]
673 ["List buffers" eww-list-buffers t]
674 ["Add bookmark" eww-add-bookmark t]
675 ["List bookmarks" eww-list-bookmarks t]
676 ["List cookies" url-cookie-list t]
677 ["Character Encoding" eww-set-character-encoding]))
678 map))
680 (defvar eww-tool-bar-map
681 (let ((map (make-sparse-keymap)))
682 (dolist (tool-bar-item
683 '((quit-window . "close")
684 (eww-reload . "refresh")
685 (eww-back-url . "left-arrow")
686 (eww-forward-url . "right-arrow")
687 (eww-view-source . "show")
688 (eww-copy-page-url . "copy")
689 (eww-add-bookmark . "bookmark_add"))) ;; ...
690 (tool-bar-local-item-from-menu
691 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
692 map)
693 "Tool bar for `eww-mode'.")
695 ;; Autoload cookie needed by desktop.el.
696 ;;;###autoload
697 (define-derived-mode eww-mode special-mode "eww"
698 "Mode for browsing the web."
699 (setq-local eww-data (list :title ""))
700 (setq-local browse-url-browser-function #'eww-browse-url)
701 (add-hook 'after-change-functions #'eww-process-text-input nil t)
702 (setq-local eww-history nil)
703 (setq-local eww-history-position 0)
704 (when (boundp 'tool-bar-map)
705 (setq-local tool-bar-map eww-tool-bar-map))
706 ;; desktop support
707 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
708 ;; multi-page isearch support
709 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
710 (setq truncate-lines t)
711 (buffer-disable-undo)
712 (setq buffer-read-only t))
714 ;;;###autoload
715 (defun eww-browse-url (url &optional new-window)
716 (cond (new-window
717 (switch-to-buffer (generate-new-buffer "*eww*"))
718 (eww-mode)))
719 (eww url))
721 (defun eww-back-url ()
722 "Go to the previously displayed page."
723 (interactive)
724 (when (>= eww-history-position (length eww-history))
725 (user-error "No previous page"))
726 (eww-save-history)
727 (setq eww-history-position (+ eww-history-position 2))
728 (eww-restore-history (elt eww-history (1- eww-history-position))))
730 (defun eww-forward-url ()
731 "Go to the next displayed page."
732 (interactive)
733 (when (zerop eww-history-position)
734 (user-error "No next page"))
735 (eww-save-history)
736 (eww-restore-history (elt eww-history (1- eww-history-position))))
738 (defun eww-restore-history (elem)
739 (let ((inhibit-read-only t)
740 (inhibit-modification-hooks t)
741 (text (plist-get elem :text)))
742 (setq eww-data elem)
743 (if (null text)
744 (eww-reload) ; FIXME: restore :point?
745 (erase-buffer)
746 (insert text)
747 (goto-char (plist-get elem :point))
748 (eww-update-header-line-format))))
750 (defun eww-next-url ()
751 "Go to the page marked `next'.
752 A page is marked `next' if rel=\"next\" appears in a <link>
753 or <a> tag."
754 (interactive)
755 (if (plist-get eww-data :next)
756 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
757 (plist-get eww-data :url)))
758 (user-error "No `next' on this page")))
760 (defun eww-previous-url ()
761 "Go to the page marked `previous'.
762 A page is marked `previous' if rel=\"previous\" appears in a <link>
763 or <a> tag."
764 (interactive)
765 (if (plist-get eww-data :previous)
766 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
767 (plist-get eww-data :url)))
768 (user-error "No `previous' on this page")))
770 (defun eww-up-url ()
771 "Go to the page marked `up'.
772 A page is marked `up' if rel=\"up\" appears in a <link>
773 or <a> tag."
774 (interactive)
775 (if (plist-get eww-data :up)
776 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
777 (plist-get eww-data :url)))
778 (user-error "No `up' on this page")))
780 (defun eww-top-url ()
781 "Go to the page marked `top'.
782 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
783 appears in a <link> or <a> tag."
784 (interactive)
785 (let ((best-url (or (plist-get eww-data :start)
786 (plist-get eww-data :contents)
787 (plist-get eww-data :home))))
788 (if best-url
789 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
790 (user-error "No `top' for this page"))))
792 (defun eww-reload (&optional local encode)
793 "Reload the current page.
794 If LOCAL (the command prefix), don't reload the page from the
795 network, but just re-display the HTML already fetched."
796 (interactive "P")
797 (let ((url (plist-get eww-data :url)))
798 (if local
799 (if (null (plist-get eww-data :dom))
800 (error "No current HTML data")
801 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
802 (point) (current-buffer)))
803 (url-retrieve url 'eww-render
804 (list url (point) (current-buffer) encode)))))
806 ;; Form support.
808 (defvar eww-form nil)
810 (defvar eww-submit-map
811 (let ((map (make-sparse-keymap)))
812 (define-key map "\r" 'eww-submit)
813 (define-key map [(control c) (control c)] 'eww-submit)
814 map))
816 (defvar eww-submit-file
817 (let ((map (make-sparse-keymap)))
818 (define-key map "\r" 'eww-select-file)
819 (define-key map [(control c) (control c)] 'eww-submit)
820 map))
822 (defvar eww-checkbox-map
823 (let ((map (make-sparse-keymap)))
824 (define-key map " " 'eww-toggle-checkbox)
825 (define-key map "\r" 'eww-toggle-checkbox)
826 (define-key map [(control c) (control c)] 'eww-submit)
827 map))
829 (defvar eww-text-map
830 (let ((map (make-keymap)))
831 (set-keymap-parent map text-mode-map)
832 (define-key map "\r" 'eww-submit)
833 (define-key map [(control a)] 'eww-beginning-of-text)
834 (define-key map [(control c) (control c)] 'eww-submit)
835 (define-key map [(control e)] 'eww-end-of-text)
836 (define-key map [?\t] 'shr-next-link)
837 (define-key map [?\M-\t] 'shr-previous-link)
838 map))
840 (defvar eww-textarea-map
841 (let ((map (make-keymap)))
842 (set-keymap-parent map text-mode-map)
843 (define-key map "\r" 'forward-line)
844 (define-key map [(control c) (control c)] 'eww-submit)
845 (define-key map [?\t] 'shr-next-link)
846 (define-key map [?\M-\t] 'shr-previous-link)
847 map))
849 (defvar eww-select-map
850 (let ((map (make-sparse-keymap)))
851 (define-key map "\r" 'eww-change-select)
852 (define-key map [(control c) (control c)] 'eww-submit)
853 map))
855 (defun eww-beginning-of-text ()
856 "Move to the start of the input field."
857 (interactive)
858 (goto-char (eww-beginning-of-field)))
860 (defun eww-end-of-text ()
861 "Move to the end of the text in the input field."
862 (interactive)
863 (goto-char (eww-end-of-field))
864 (let ((start (eww-beginning-of-field)))
865 (while (and (equal (following-char) ? )
866 (> (point) start))
867 (forward-char -1))
868 (when (> (point) start)
869 (forward-char 1))))
871 (defun eww-beginning-of-field ()
872 (cond
873 ((bobp)
874 (point))
875 ((not (eq (get-text-property (point) 'eww-form)
876 (get-text-property (1- (point)) 'eww-form)))
877 (point))
879 (previous-single-property-change
880 (point) 'eww-form nil (point-min)))))
882 (defun eww-end-of-field ()
883 (1- (next-single-property-change
884 (point) 'eww-form nil (point-max))))
886 (defun eww-tag-form (dom)
887 (let ((eww-form (list (cons :method (dom-attr dom 'method))
888 (cons :action (dom-attr dom 'action))))
889 (start (point)))
890 (shr-ensure-paragraph)
891 (shr-generic dom)
892 (unless (bolp)
893 (insert "\n"))
894 (insert "\n")
895 (when (> (point) start)
896 (put-text-property start (1+ start)
897 'eww-form eww-form))))
899 (defun eww-form-submit (dom)
900 (let ((start (point))
901 (value (dom-attr dom 'value)))
902 (setq value
903 (if (zerop (length value))
904 "Submit"
905 value))
906 (insert value)
907 (add-face-text-property start (point) 'eww-form-submit)
908 (put-text-property start (point) 'eww-form
909 (list :eww-form eww-form
910 :value value
911 :type "submit"
912 :name (dom-attr dom 'name)))
913 (put-text-property start (point) 'keymap eww-submit-map)
914 (insert " ")))
916 (defun eww-form-checkbox (dom)
917 (let ((start (point)))
918 (if (dom-attr dom 'checked)
919 (insert eww-form-checkbox-selected-symbol)
920 (insert eww-form-checkbox-symbol))
921 (add-face-text-property start (point) 'eww-form-checkbox)
922 (put-text-property start (point) 'eww-form
923 (list :eww-form eww-form
924 :value (dom-attr dom 'value)
925 :type (downcase (dom-attr dom 'type))
926 :checked (dom-attr dom 'checked)
927 :name (dom-attr dom 'name)))
928 (put-text-property start (point) 'keymap eww-checkbox-map)
929 (insert " ")))
931 (defun eww-form-file (dom)
932 (let ((start (point))
933 (value (dom-attr dom 'value)))
934 (setq value
935 (if (zerop (length value))
936 " No file selected"
937 value))
938 (insert "Browse")
939 (add-face-text-property start (point) 'eww-form-file)
940 (insert value)
941 (put-text-property start (point) 'eww-form
942 (list :eww-form eww-form
943 :value (dom-attr dom 'value)
944 :type (downcase (dom-attr dom 'type))
945 :name (dom-attr dom 'name)))
946 (put-text-property start (point) 'keymap eww-submit-file)
947 (insert " ")))
949 (defun eww-select-file ()
950 "Change the value of the upload file menu under point."
951 (interactive)
952 (let* ((input (get-text-property (point) 'eww-form)))
953 (let ((filename
954 (let ((insert-default-directory t))
955 (read-file-name "filename: "))))
956 (eww-update-field filename (length "Browse"))
957 (plist-put input :filename filename))))
959 (defun eww-form-text (dom)
960 (let ((start (point))
961 (type (downcase (or (dom-attr dom 'type) "text")))
962 (value (or (dom-attr dom 'value) ""))
963 (width (string-to-number (or (dom-attr dom 'size) "40")))
964 (readonly-property (if (or (dom-attr dom 'disabled)
965 (dom-attr dom 'readonly))
966 'read-only
967 'inhibit-read-only)))
968 (insert value)
969 (when (< (length value) width)
970 (insert (make-string (- width (length value)) ? )))
971 (put-text-property start (point) 'face 'eww-form-text)
972 (put-text-property start (point) 'inhibit-read-only t)
973 (put-text-property start (point) 'local-map eww-text-map)
974 (put-text-property start (point) readonly-property t)
975 (put-text-property start (point) 'eww-form
976 (list :eww-form eww-form
977 :value value
978 :type type
979 :name (dom-attr dom 'name)))
980 (insert " ")))
982 (defconst eww-text-input-types '("text" "password" "textarea"
983 "color" "date" "datetime" "datetime-local"
984 "email" "month" "number" "search" "tel"
985 "time" "url" "week")
986 "List of input types which represent a text input.
987 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
989 (defun eww-process-text-input (beg end replace-length)
990 (when-let (pos (and (< (1+ end) (point-max))
991 (> (1- end) (point-min))
992 (cond
993 ((get-text-property (1+ end) 'eww-form)
994 (1+ end))
995 ((get-text-property (1- end) 'eww-form)
996 (1- end)))))
997 (let* ((form (get-text-property pos 'eww-form))
998 (properties (text-properties-at pos))
999 (inhibit-read-only t)
1000 (length (- end beg replace-length))
1001 (type (plist-get form :type)))
1002 (when (and form
1003 (member type eww-text-input-types))
1004 (cond
1005 ((> length 0)
1006 ;; Delete some space at the end.
1007 (save-excursion
1008 (goto-char
1009 (if (equal type "textarea")
1010 (1- (line-end-position))
1011 (eww-end-of-field)))
1012 (while (and (> length 0)
1013 (eql (following-char) ? ))
1014 (delete-region (1- (point)) (point))
1015 (cl-decf length))))
1016 ((< length 0)
1017 ;; Add padding.
1018 (save-excursion
1019 (goto-char (1- end))
1020 (goto-char
1021 (if (equal type "textarea")
1022 (1- (line-end-position))
1023 (1+ (eww-end-of-field))))
1024 (let ((start (point)))
1025 (insert (make-string (abs length) ? ))
1026 (set-text-properties start (point) properties))
1027 (goto-char (1- end)))))
1028 (set-text-properties (plist-get form :start) (plist-get form :end)
1029 properties)
1030 (let ((value (buffer-substring-no-properties
1031 (eww-beginning-of-field)
1032 (eww-end-of-field))))
1033 (when (string-match " +\\'" value)
1034 (setq value (substring value 0 (match-beginning 0))))
1035 (plist-put form :value value)
1036 (when (equal type "password")
1037 ;; Display passwords as asterisks.
1038 (let ((start (eww-beginning-of-field)))
1039 (put-text-property start (+ start (length value))
1040 'display (make-string (length value) ?*)))))))))
1042 (defun eww-tag-textarea (dom)
1043 (let ((start (point))
1044 (value (or (dom-attr dom 'value) ""))
1045 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1046 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1047 end)
1048 (shr-ensure-newline)
1049 (insert value)
1050 (shr-ensure-newline)
1051 (when (< (count-lines start (point)) lines)
1052 (dotimes (_ (- lines (count-lines start (point))))
1053 (insert "\n")))
1054 (setq end (point-marker))
1055 (goto-char start)
1056 (while (< (point) end)
1057 (end-of-line)
1058 (let ((pad (- width (- (point) (line-beginning-position)))))
1059 (when (> pad 0)
1060 (insert (make-string pad ? ))))
1061 (add-face-text-property (line-beginning-position)
1062 (point) 'eww-form-textarea)
1063 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1064 (put-text-property (line-beginning-position) (point)
1065 'local-map eww-textarea-map)
1066 (forward-line 1))
1067 (put-text-property start (point) 'eww-form
1068 (list :eww-form eww-form
1069 :value value
1070 :type "textarea"
1071 :name (dom-attr dom 'name)))))
1073 (defun eww-tag-input (dom)
1074 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1075 (start (point)))
1076 (cond
1077 ((or (equal type "checkbox")
1078 (equal type "radio"))
1079 (eww-form-checkbox dom))
1080 ((equal type "file")
1081 (eww-form-file dom))
1082 ((equal type "submit")
1083 (eww-form-submit dom))
1084 ((equal type "hidden")
1085 (let ((form eww-form)
1086 (name (dom-attr dom 'name)))
1087 ;; Don't add <input type=hidden> elements repeatedly.
1088 (while (and form
1089 (or (not (consp (car form)))
1090 (not (eq (caar form) 'hidden))
1091 (not (equal (plist-get (cdr (car form)) :name)
1092 name))))
1093 (setq form (cdr form)))
1094 (unless form
1095 (nconc eww-form (list
1096 (list 'hidden
1097 :name name
1098 :value (dom-attr dom 'value)))))))
1100 (eww-form-text dom)))
1101 (unless (= start (point))
1102 (put-text-property start (1+ start) 'help-echo "Input field"))))
1104 (defun eww-tag-select (dom)
1105 (shr-ensure-paragraph)
1106 (let ((menu (list :name (dom-attr dom 'name)
1107 :eww-form eww-form))
1108 (options nil)
1109 (start (point))
1110 (max 0)
1111 opelem)
1112 (if (eq (dom-tag dom) 'optgroup)
1113 (dolist (groupelem (dom-children dom))
1114 (unless (dom-attr groupelem 'disabled)
1115 (setq opelem (append opelem (list groupelem)))))
1116 (setq opelem (list dom)))
1117 (dolist (elem opelem)
1118 (when (eq (dom-tag elem) 'option)
1119 (when (dom-attr elem 'selected)
1120 (nconc menu (list :value (dom-attr elem 'value))))
1121 (let ((display (dom-text elem)))
1122 (setq max (max max (length display)))
1123 (push (list 'item
1124 :value (dom-attr elem 'value)
1125 :display display)
1126 options))))
1127 (when options
1128 (setq options (nreverse options))
1129 ;; If we have no selected values, default to the first value.
1130 (unless (plist-get menu :value)
1131 (nconc menu (list :value (nth 2 (car options)))))
1132 (nconc menu options)
1133 (let ((selected (eww-select-display menu)))
1134 (insert selected
1135 (make-string (- max (length selected)) ? )))
1136 (put-text-property start (point) 'eww-form menu)
1137 (add-face-text-property start (point) 'eww-form-select)
1138 (put-text-property start (point) 'keymap eww-select-map)
1139 (unless (= start (point))
1140 (put-text-property start (1+ start) 'help-echo "select field"))
1141 (shr-ensure-paragraph))))
1143 (defun eww-select-display (select)
1144 (let ((value (plist-get select :value))
1145 display)
1146 (dolist (elem select)
1147 (when (and (consp elem)
1148 (eq (car elem) 'item)
1149 (equal value (plist-get (cdr elem) :value)))
1150 (setq display (plist-get (cdr elem) :display))))
1151 display))
1153 (defun eww-change-select ()
1154 "Change the value of the select drop-down menu under point."
1155 (interactive)
1156 (let* ((input (get-text-property (point) 'eww-form))
1157 (completion-ignore-case t)
1158 (options
1159 (delq nil
1160 (mapcar (lambda (elem)
1161 (and (consp elem)
1162 (eq (car elem) 'item)
1163 (cons (plist-get (cdr elem) :display)
1164 (plist-get (cdr elem) :value))))
1165 input)))
1166 (display
1167 (completing-read "Change value: " options nil 'require-match))
1168 (inhibit-read-only t))
1169 (plist-put input :value (cdr (assoc-string display options t)))
1170 (goto-char
1171 (eww-update-field display))))
1173 (defun eww-update-field (string &optional offset)
1174 (if (not offset) (setq offset 0))
1175 (let ((properties (text-properties-at (point)))
1176 (start (+ (eww-beginning-of-field) offset))
1177 (current-end (1+ (eww-end-of-field)))
1178 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1179 (delete-region start current-end)
1180 (forward-char offset)
1181 (insert string
1182 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1183 (if (= 0 offset) (set-text-properties start new-end properties))
1184 start))
1186 (defun eww-toggle-checkbox ()
1187 "Toggle the value of the checkbox under point."
1188 (interactive)
1189 (let* ((input (get-text-property (point) 'eww-form))
1190 (type (plist-get input :type)))
1191 (if (equal type "checkbox")
1192 (goto-char
1194 (if (plist-get input :checked)
1195 (progn
1196 (plist-put input :checked nil)
1197 (eww-update-field eww-form-checkbox-symbol))
1198 (plist-put input :checked t)
1199 (eww-update-field eww-form-checkbox-selected-symbol))))
1200 ;; Radio button. Switch all other buttons off.
1201 (let ((name (plist-get input :name)))
1202 (save-excursion
1203 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1204 (when (equal (plist-get (cdr elem) :name) name)
1205 (goto-char (car elem))
1206 (if (not (eq (cdr elem) input))
1207 (progn
1208 (plist-put input :checked nil)
1209 (eww-update-field eww-form-checkbox-symbol))
1210 (plist-put input :checked t)
1211 (eww-update-field eww-form-checkbox-selected-symbol)))))
1212 (forward-char 1)))))
1214 (defun eww-inputs (form)
1215 (let ((start (point-min))
1216 (inputs nil))
1217 (while (and start
1218 (< start (point-max)))
1219 (when (or (get-text-property start 'eww-form)
1220 (setq start (next-single-property-change start 'eww-form)))
1221 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1222 form)
1223 (push (cons start (get-text-property start 'eww-form))
1224 inputs))
1225 (setq start (next-single-property-change start 'eww-form))))
1226 (nreverse inputs)))
1228 (defun eww-size-text-inputs ()
1229 (let ((start (point-min)))
1230 (while (and start
1231 (< start (point-max)))
1232 (when (or (get-text-property start 'eww-form)
1233 (setq start (next-single-property-change start 'eww-form)))
1234 (let ((props (get-text-property start 'eww-form)))
1235 (plist-put props :start start)
1236 (setq start (next-single-property-change
1237 start 'eww-form nil (point-max)))
1238 (plist-put props :end start))))))
1240 (defun eww-input-value (input)
1241 (let ((type (plist-get input :type))
1242 (value (plist-get input :value)))
1243 (cond
1244 ((equal type "textarea")
1245 (with-temp-buffer
1246 (insert value)
1247 (goto-char (point-min))
1248 (while (re-search-forward "^ +\n\\| +$" nil t)
1249 (replace-match "" t t))
1250 (buffer-string)))
1252 (if (string-match " +\\'" value)
1253 (substring value 0 (match-beginning 0))
1254 value)))))
1256 (defun eww-submit ()
1257 "Submit the current form."
1258 (interactive)
1259 (let* ((this-input (get-text-property (point) 'eww-form))
1260 (form (plist-get this-input :eww-form))
1261 values next-submit)
1262 (dolist (elem (sort (eww-inputs form)
1263 (lambda (o1 o2)
1264 (< (car o1) (car o2)))))
1265 (let* ((input (cdr elem))
1266 (input-start (car elem))
1267 (name (plist-get input :name)))
1268 (when name
1269 (cond
1270 ((member (plist-get input :type) '("checkbox" "radio"))
1271 (when (plist-get input :checked)
1272 (push (cons name (plist-get input :value))
1273 values)))
1274 ((equal (plist-get input :type) "file")
1275 (push (cons "file"
1276 (list (cons "filedata"
1277 (with-temp-buffer
1278 (insert-file-contents
1279 (plist-get input :filename))
1280 (buffer-string)))
1281 (cons "name" (plist-get input :name))
1282 (cons "filename" (plist-get input :filename))))
1283 values))
1284 ((equal (plist-get input :type) "submit")
1285 ;; We want the values from buttons if we hit a button if
1286 ;; we hit enter on it, or if it's the first button after
1287 ;; the field we did hit return on.
1288 (when (or (eq input this-input)
1289 (and (not (eq input this-input))
1290 (null next-submit)
1291 (> input-start (point))))
1292 (setq next-submit t)
1293 (push (cons name (plist-get input :value))
1294 values)))
1296 (push (cons name (eww-input-value input))
1297 values))))))
1298 (dolist (elem form)
1299 (when (and (consp elem)
1300 (eq (car elem) 'hidden))
1301 (push (cons (plist-get (cdr elem) :name)
1302 (or (plist-get (cdr elem) :value) ""))
1303 values)))
1304 (if (and (stringp (cdr (assq :method form)))
1305 (equal (downcase (cdr (assq :method form))) "post"))
1306 (let ((mtype))
1307 (dolist (x values mtype)
1308 (if (equal (car x) "file")
1309 (progn
1310 (setq mtype "multipart/form-data"))))
1311 (cond ((equal mtype "multipart/form-data")
1312 (let ((boundary (mml-compute-boundary '())))
1313 (let ((url-request-method "POST")
1314 (url-request-extra-headers
1315 (list (cons "Content-Type"
1316 (concat "multipart/form-data; boundary="
1317 boundary))))
1318 (url-request-data
1319 (mm-url-encode-multipart-form-data values boundary)))
1320 (eww-browse-url (shr-expand-url
1321 (cdr (assq :action form))
1322 (plist-get eww-data :url))))))
1324 (let ((url-request-method "POST")
1325 (url-request-extra-headers
1326 '(("Content-Type" .
1327 "application/x-www-form-urlencoded")))
1328 (url-request-data
1329 (mm-url-encode-www-form-urlencoded values)))
1330 (eww-browse-url (shr-expand-url
1331 (cdr (assq :action form))
1332 (plist-get eww-data :url)))))))
1333 (eww-browse-url
1334 (concat
1335 (if (cdr (assq :action form))
1336 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1337 (plist-get eww-data :url))
1339 (mm-url-encode-www-form-urlencoded values))))))
1341 (defun eww-browse-with-external-browser (&optional url)
1342 "Browse the current URL with an external browser.
1343 The browser to used is specified by the `shr-external-browser' variable."
1344 (interactive)
1345 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1347 (defun eww-follow-link (&optional external mouse-event)
1348 "Browse the URL under point.
1349 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1350 If EXTERNAL is double prefix, browse in new buffer."
1351 (interactive (list current-prefix-arg last-nonmenu-event))
1352 (mouse-set-point mouse-event)
1353 (let ((url (get-text-property (point) 'shr-url)))
1354 (cond
1355 ((not url)
1356 (message "No link under point"))
1357 ((string-match "^mailto:" url)
1358 (browse-url-mail url))
1359 ((and (consp external) (<= (car external) 4))
1360 (funcall shr-external-browser url))
1361 ;; This is a #target url in the same page as the current one.
1362 ((and (url-target (url-generic-parse-url url))
1363 (eww-same-page-p url (plist-get eww-data :url)))
1364 (let ((dom (plist-get eww-data :dom)))
1365 (eww-save-history)
1366 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1368 (eww-browse-url url external)))))
1370 (defun eww-same-page-p (url1 url2)
1371 "Return non-nil if URL1 and URL2 represent the same page.
1372 Differences in #targets are ignored."
1373 (let ((obj1 (url-generic-parse-url url1))
1374 (obj2 (url-generic-parse-url url2)))
1375 (setf (url-target obj1) nil)
1376 (setf (url-target obj2) nil)
1377 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1379 (defun eww-copy-page-url ()
1380 "Copy the URL of the current page into the kill ring."
1381 (interactive)
1382 (message "%s" (plist-get eww-data :url))
1383 (kill-new (plist-get eww-data :url)))
1385 (defun eww-download ()
1386 "Download URL under point to `eww-download-directory'."
1387 (interactive)
1388 (let ((url (get-text-property (point) 'shr-url)))
1389 (if (not url)
1390 (message "No URL under point")
1391 (url-retrieve url 'eww-download-callback (list url)))))
1393 (defun eww-download-callback (status url)
1394 (unless (plist-get status :error)
1395 (let* ((obj (url-generic-parse-url url))
1396 (path (car (url-path-and-query obj)))
1397 (file (eww-make-unique-file-name (file-name-nondirectory path)
1398 eww-download-directory)))
1399 (goto-char (point-min))
1400 (re-search-forward "\r?\n\r?\n")
1401 (write-region (point) (point-max) file)
1402 (message "Saved %s" file))))
1404 (defun eww-make-unique-file-name (file directory)
1405 (cond
1406 ((zerop (length file))
1407 (setq file "!"))
1408 ((string-match "\\`[.]" file)
1409 (setq file (concat "!" file))))
1410 (let ((count 1))
1411 (while (file-exists-p (expand-file-name file directory))
1412 (setq file
1413 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1414 (format "%s(%d)%s" (match-string 1 file)
1415 count (match-string 2 file))
1416 (format "%s(%d)" file count)))
1417 (setq count (1+ count)))
1418 (expand-file-name file directory)))
1420 (defun eww-set-character-encoding (charset)
1421 "Set character encoding to CHARSET.
1422 If CHARSET is nil then use UTF-8."
1423 (interactive "zUse character set (default utf-8): ")
1424 (if (null charset)
1425 (eww-reload nil 'utf-8)
1426 (eww-reload nil charset)))
1428 (defun eww-toggle-fonts ()
1429 "Toggle whether to use monospaced or font-enabled layouts."
1430 (interactive)
1431 (message "Fonts are now %s"
1432 (if (setq shr-use-fonts (not shr-use-fonts))
1433 "on"
1434 "off"))
1435 (eww-reload))
1437 ;;; Bookmarks code
1439 (defvar eww-bookmarks nil)
1441 (defun eww-add-bookmark ()
1442 "Bookmark the current page."
1443 (interactive)
1444 (eww-read-bookmarks)
1445 (dolist (bookmark eww-bookmarks)
1446 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1447 (user-error "Already bookmarked")))
1448 (when (y-or-n-p "Bookmark this page?")
1449 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1450 (plist-get eww-data :title))))
1451 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1452 (push (list :url (plist-get eww-data :url)
1453 :title title
1454 :time (current-time-string))
1455 eww-bookmarks))
1456 (eww-write-bookmarks)
1457 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1458 (plist-get eww-data :title))))
1460 (defun eww-write-bookmarks ()
1461 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1462 (insert ";; Auto-generated file; don't edit\n")
1463 (pp eww-bookmarks (current-buffer))))
1465 (defun eww-read-bookmarks ()
1466 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1467 (setq eww-bookmarks
1468 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1469 (with-temp-buffer
1470 (insert-file-contents file)
1471 (read (current-buffer)))))))
1473 ;;;###autoload
1474 (defun eww-list-bookmarks ()
1475 "Display the bookmarks."
1476 (interactive)
1477 (eww-bookmark-prepare)
1478 (pop-to-buffer "*eww bookmarks*"))
1480 (defun eww-bookmark-prepare ()
1481 (eww-read-bookmarks)
1482 (unless eww-bookmarks
1483 (user-error "No bookmarks are defined"))
1484 (set-buffer (get-buffer-create "*eww bookmarks*"))
1485 (eww-bookmark-mode)
1486 (let* ((width (/ (window-width) 2))
1487 (format (format "%%-%ds %%s" width))
1488 (inhibit-read-only t)
1489 start title)
1490 (erase-buffer)
1491 (setq header-line-format (concat " " (format format "Title" "URL")))
1492 (dolist (bookmark eww-bookmarks)
1493 (setq start (point)
1494 title (plist-get bookmark :title))
1495 (when (> (length title) width)
1496 (setq title (substring title 0 width)))
1497 (insert (format format title (plist-get bookmark :url)) "\n")
1498 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1499 (goto-char (point-min))))
1501 (defvar eww-bookmark-kill-ring nil)
1503 (defun eww-bookmark-kill ()
1504 "Kill the current bookmark."
1505 (interactive)
1506 (let* ((start (line-beginning-position))
1507 (bookmark (get-text-property start 'eww-bookmark))
1508 (inhibit-read-only t))
1509 (unless bookmark
1510 (user-error "No bookmark on the current line"))
1511 (forward-line 1)
1512 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1513 (delete-region start (point))
1514 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1515 (eww-write-bookmarks)))
1517 (defun eww-bookmark-yank ()
1518 "Yank a previously killed bookmark to the current line."
1519 (interactive)
1520 (unless eww-bookmark-kill-ring
1521 (user-error "No previously killed bookmark"))
1522 (beginning-of-line)
1523 (let ((inhibit-read-only t)
1524 (start (point))
1525 bookmark)
1526 (insert (pop eww-bookmark-kill-ring))
1527 (setq bookmark (get-text-property start 'eww-bookmark))
1528 (if (= start (point-min))
1529 (push bookmark eww-bookmarks)
1530 (let ((line (count-lines start (point))))
1531 (setcdr (nthcdr (1- line) eww-bookmarks)
1532 (cons bookmark (nthcdr line eww-bookmarks)))))
1533 (eww-write-bookmarks)))
1535 (defun eww-bookmark-browse ()
1536 "Browse the bookmark under point in eww."
1537 (interactive)
1538 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1539 (unless bookmark
1540 (user-error "No bookmark on the current line"))
1541 (quit-window)
1542 (eww-browse-url (plist-get bookmark :url))))
1544 (defun eww-next-bookmark ()
1545 "Go to the next bookmark in the list."
1546 (interactive)
1547 (let ((first nil)
1548 bookmark)
1549 (unless (get-buffer "*eww bookmarks*")
1550 (setq first t)
1551 (eww-bookmark-prepare))
1552 (with-current-buffer (get-buffer "*eww bookmarks*")
1553 (when (and (not first)
1554 (not (eobp)))
1555 (forward-line 1))
1556 (setq bookmark (get-text-property (line-beginning-position)
1557 'eww-bookmark))
1558 (unless bookmark
1559 (user-error "No next bookmark")))
1560 (eww-browse-url (plist-get bookmark :url))))
1562 (defun eww-previous-bookmark ()
1563 "Go to the previous bookmark in the list."
1564 (interactive)
1565 (let ((first nil)
1566 bookmark)
1567 (unless (get-buffer "*eww bookmarks*")
1568 (setq first t)
1569 (eww-bookmark-prepare))
1570 (with-current-buffer (get-buffer "*eww bookmarks*")
1571 (if first
1572 (goto-char (point-max))
1573 (beginning-of-line))
1574 ;; On the final line.
1575 (when (eolp)
1576 (forward-line -1))
1577 (if (bobp)
1578 (user-error "No previous bookmark")
1579 (forward-line -1))
1580 (setq bookmark (get-text-property (line-beginning-position)
1581 'eww-bookmark)))
1582 (eww-browse-url (plist-get bookmark :url))))
1584 (defvar eww-bookmark-mode-map
1585 (let ((map (make-sparse-keymap)))
1586 (suppress-keymap map)
1587 (define-key map "q" 'quit-window)
1588 (define-key map [(control k)] 'eww-bookmark-kill)
1589 (define-key map [(control y)] 'eww-bookmark-yank)
1590 (define-key map "\r" 'eww-bookmark-browse)
1592 (easy-menu-define nil map
1593 "Menu for `eww-bookmark-mode-map'."
1594 '("Eww Bookmark"
1595 ["Exit" quit-window t]
1596 ["Browse" eww-bookmark-browse
1597 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1598 ["Kill" eww-bookmark-kill
1599 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1600 ["Yank" eww-bookmark-yank
1601 :active eww-bookmark-kill-ring]))
1602 map))
1604 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1605 "Mode for listing bookmarks.
1607 \\{eww-bookmark-mode-map}"
1608 (buffer-disable-undo)
1609 (setq buffer-read-only t
1610 truncate-lines t))
1612 ;;; History code
1614 (defun eww-save-history ()
1615 (plist-put eww-data :point (point))
1616 (plist-put eww-data :text (buffer-string))
1617 (push eww-data eww-history)
1618 (setq eww-data (list :title ""))
1619 ;; Don't let the history grow infinitely. We store quite a lot of
1620 ;; data per page.
1621 (when-let (tail (and eww-history-limit
1622 (nthcdr eww-history-limit eww-history)))
1623 (setcdr tail nil)))
1625 (defvar eww-current-buffer)
1627 (defun eww-list-histories ()
1628 "List the eww-histories."
1629 (interactive)
1630 (when (null eww-history)
1631 (error "No eww-histories are defined"))
1632 (let ((eww-history-trans eww-history)
1633 (buffer (current-buffer)))
1634 (set-buffer (get-buffer-create "*eww history*"))
1635 (eww-history-mode)
1636 (setq-local eww-current-buffer buffer)
1637 (let ((inhibit-read-only t)
1638 (domain-length 0)
1639 (title-length 0)
1640 url title format start)
1641 (erase-buffer)
1642 (dolist (history eww-history-trans)
1643 (setq start (point))
1644 (setq domain-length (max domain-length (length (plist-get history :url))))
1645 (setq title-length (max title-length (length (plist-get history :title)))))
1646 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1647 header-line-format
1648 (concat " " (format format "Title" "URL")))
1649 (dolist (history eww-history-trans)
1650 (setq start (point))
1651 (setq url (plist-get history :url))
1652 (setq title (plist-get history :title))
1653 (insert (format format title url))
1654 (insert "\n")
1655 (put-text-property start (1+ start) 'eww-history history))
1656 (goto-char (point-min)))
1657 (pop-to-buffer "*eww history*")))
1659 (defun eww-history-browse ()
1660 "Browse the history under point in eww."
1661 (interactive)
1662 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1663 (unless history
1664 (error "No history on the current line"))
1665 (let ((buffer eww-current-buffer))
1666 (quit-window)
1667 (when buffer
1668 (switch-to-buffer buffer)))
1669 (eww-restore-history history)))
1671 (defvar eww-history-mode-map
1672 (let ((map (make-sparse-keymap)))
1673 (suppress-keymap map)
1674 (define-key map "q" 'quit-window)
1675 (define-key map "\r" 'eww-history-browse)
1676 ;; (define-key map "n" 'next-error-no-select)
1677 ;; (define-key map "p" 'previous-error-no-select)
1679 (easy-menu-define nil map
1680 "Menu for `eww-history-mode-map'."
1681 '("Eww History"
1682 ["Exit" quit-window t]
1683 ["Browse" eww-history-browse
1684 :active (get-text-property (line-beginning-position) 'eww-history)]))
1685 map))
1687 (define-derived-mode eww-history-mode nil "eww history"
1688 "Mode for listing eww-histories.
1690 \\{eww-history-mode-map}"
1691 (buffer-disable-undo)
1692 (setq buffer-read-only t
1693 truncate-lines t))
1695 ;;; eww buffers list
1697 (defun eww-list-buffers ()
1698 "Enlist eww buffers."
1699 (interactive)
1700 (let (buffers-info
1701 (current (current-buffer)))
1702 (dolist (buffer (buffer-list))
1703 (with-current-buffer buffer
1704 (when (derived-mode-p 'eww-mode)
1705 (push (vector buffer (plist-get eww-data :title)
1706 (plist-get eww-data :url))
1707 buffers-info))))
1708 (unless buffers-info
1709 (error "No eww buffers"))
1710 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1711 (set-buffer (get-buffer-create "*eww buffers*"))
1712 (eww-buffers-mode)
1713 (let ((inhibit-read-only t)
1714 (domain-length 0)
1715 (title-length 0)
1716 url title format start)
1717 (erase-buffer)
1718 (dolist (buffer-info buffers-info)
1719 (setq title-length (max title-length
1720 (length (elt buffer-info 1)))
1721 domain-length (max domain-length
1722 (length (elt buffer-info 2)))))
1723 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1724 header-line-format
1725 (concat " " (format format "Title" "URL")))
1726 (let ((line 0)
1727 (current-buffer-line 1))
1728 (dolist (buffer-info buffers-info)
1729 (setq start (point)
1730 title (elt buffer-info 1)
1731 url (elt buffer-info 2)
1732 line (1+ line))
1733 (insert (format format title url))
1734 (insert "\n")
1735 (let ((buffer (elt buffer-info 0)))
1736 (put-text-property start (1+ start) 'eww-buffer
1737 buffer)
1738 (when (eq current buffer)
1739 (setq current-buffer-line line))))
1740 (goto-char (point-min))
1741 (forward-line (1- current-buffer-line)))))
1742 (pop-to-buffer "*eww buffers*"))
1744 (defun eww-buffer-select ()
1745 "Switch to eww buffer."
1746 (interactive)
1747 (let ((buffer (get-text-property (line-beginning-position)
1748 'eww-buffer)))
1749 (unless buffer
1750 (error "No buffer on current line"))
1751 (quit-window)
1752 (switch-to-buffer buffer)))
1754 (defun eww-buffer-show ()
1755 "Display buffer under point in eww buffer list."
1756 (let ((buffer (get-text-property (line-beginning-position)
1757 'eww-buffer)))
1758 (unless buffer
1759 (error "No buffer on current line"))
1760 (other-window -1)
1761 (switch-to-buffer buffer)
1762 (other-window 1)))
1764 (defun eww-buffer-show-next ()
1765 "Move to next eww buffer in the list and display it."
1766 (interactive)
1767 (forward-line)
1768 (when (eobp)
1769 (goto-char (point-min)))
1770 (eww-buffer-show))
1772 (defun eww-buffer-show-previous ()
1773 "Move to previous eww buffer in the list and display it."
1774 (interactive)
1775 (beginning-of-line)
1776 (when (bobp)
1777 (goto-char (point-max)))
1778 (forward-line -1)
1779 (eww-buffer-show))
1781 (defun eww-buffer-kill ()
1782 "Kill buffer from eww list."
1783 (interactive)
1784 (let* ((start (line-beginning-position))
1785 (buffer (get-text-property start 'eww-buffer))
1786 (inhibit-read-only t))
1787 (unless buffer
1788 (user-error "No buffer on the current line"))
1789 (kill-buffer buffer)
1790 (forward-line 1)
1791 (delete-region start (point)))
1792 (when (eobp)
1793 (forward-line -1))
1794 (eww-buffer-show))
1796 (defvar eww-buffers-mode-map
1797 (let ((map (make-sparse-keymap)))
1798 (suppress-keymap map)
1799 (define-key map "q" 'quit-window)
1800 (define-key map [(control k)] 'eww-buffer-kill)
1801 (define-key map "\r" 'eww-buffer-select)
1802 (define-key map "n" 'eww-buffer-show-next)
1803 (define-key map "p" 'eww-buffer-show-previous)
1805 (easy-menu-define nil map
1806 "Menu for `eww-buffers-mode-map'."
1807 '("Eww Buffers"
1808 ["Exit" quit-window t]
1809 ["Select" eww-buffer-select
1810 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1811 ["Kill" eww-buffer-kill
1812 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1813 map))
1815 (define-derived-mode eww-buffers-mode nil "eww buffers"
1816 "Mode for listing buffers.
1818 \\{eww-buffers-mode-map}"
1819 (buffer-disable-undo)
1820 (setq buffer-read-only t
1821 truncate-lines t))
1823 ;;; Desktop support
1825 (defvar eww-desktop-data-save
1826 '(:url :title :point)
1827 "List of `eww-data' properties to preserve in the desktop file.
1828 Also used when saving `eww-history'.")
1830 (defun eww-desktop-data-1 (alist)
1831 (let ((acc nil)
1832 (tail alist))
1833 (while tail
1834 (let ((k (car tail))
1835 (v (cadr tail)))
1836 (when (memq k eww-desktop-data-save)
1837 (setq acc (cons k (cons v acc)))))
1838 (setq tail (cddr tail)))
1839 acc))
1841 (defun eww-desktop-history-duplicate (a b)
1842 (let ((tail a) (r t))
1843 (while tail
1844 (if (or (memq (car tail) '(:point)) ; ignore :point
1845 (equal (cadr tail)
1846 (plist-get b (car tail))))
1847 (setq tail (cddr tail))
1848 (setq tail nil
1849 r nil)))
1850 ;; .
1853 (defun eww-desktop-misc-data (_directory)
1854 "Return a property list with data used to restore eww buffers.
1855 This list will contain, as :history, the list, whose first element is
1856 the value of `eww-data', and the tail is `eww-history'.
1858 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1859 entries (if any) will be removed from the list.
1861 Only the properties listed in `eww-desktop-data-save' are included.
1862 Generally, the list should not include the (usually overly large)
1863 :dom, :source and :text properties."
1864 (let ((history (mapcar 'eww-desktop-data-1
1865 (cons eww-data eww-history))))
1866 (list :history (if eww-desktop-remove-duplicates
1867 (cl-remove-duplicates
1868 history :test 'eww-desktop-history-duplicate)
1869 history))))
1871 (defun eww-restore-desktop (file-name buffer-name misc-data)
1872 "Restore an eww buffer from its desktop file record.
1873 If `eww-restore-desktop' is t or 'auto, this function will also
1874 initiate the retrieval of the respective URI in the background.
1875 Otherwise, the restored buffer will contain a prompt to do so by using
1876 \\[eww-reload]."
1877 (with-current-buffer (get-buffer-create buffer-name)
1878 (eww-mode)
1879 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1880 (setq eww-history (cdr (plist-get misc-data :history))
1881 eww-data (or (car (plist-get misc-data :history))
1882 ;; backwards compatibility
1883 (list :url (plist-get misc-data :uri))))
1884 (unless file-name
1885 (when (plist-get eww-data :url)
1886 (case eww-restore-desktop
1887 ((t auto) (eww (plist-get eww-data :url)))
1888 ((zerop (buffer-size))
1889 (let ((inhibit-read-only t))
1890 (insert (substitute-command-keys
1891 eww-restore-reload-prompt)))))))
1892 ;; .
1893 (current-buffer)))
1895 (add-to-list 'desktop-locals-to-save
1896 'eww-history-position)
1897 (add-to-list 'desktop-buffer-mode-handlers
1898 '(eww-mode . eww-restore-desktop))
1900 ;;; Isearch support
1902 (defun eww-isearch-next-buffer (&optional _buffer wrap)
1903 "Go to the next page to search using `rel' attribute for navigation."
1904 (if wrap
1905 (condition-case nil
1906 (eww-top-url)
1907 (error nil))
1908 (if isearch-forward
1909 (eww-next-url)
1910 (eww-previous-url)))
1911 (current-buffer))
1913 (provide 'eww)
1915 ;;; eww.el ends here