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