]> code.delx.au - gnu-emacs/blob - lisp/net/eww.el
Merge from origin/emacs-24
[gnu-emacs] / lisp / net / eww.el
1 ;;; eww.el --- Emacs Web Wowser -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
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
35
36 (defgroup eww nil
37 "Emacs Web Wowser"
38 :version "25.1"
39 :link '(custom-manual "(eww) Top")
40 :group 'web
41 :prefix "eww-")
42
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)
50
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)
56
57 (defcustom eww-download-directory "~/Downloads/"
58 "Directory where files will downloaded."
59 :version "24.4"
60 :group 'eww
61 :type 'string)
62
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))
78
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)
84
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)
93
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)))
104
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.
109
110 The string will be passed through `substitute-command-keys'."
111 :version "25.1"
112 :group 'eww
113 :type 'string)
114
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))
121
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))
129
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)
135
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))
145
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))
154
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)
162
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)
170
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)
178
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)
186
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)
194
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)
202
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)
209
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)
216
217 (defvar eww-data nil)
218 (defvar eww-history nil)
219 (defvar eww-history-position 0)
220
221 (defvar eww-local-regex "localhost"
222 "When this regex is found in the URL, it's not a keyword but an address.")
223
224 (defvar eww-link-keymap
225 (let ((map (copy-keymap shr-map)))
226 (define-key map "\r" 'eww-follow-link)
227 map))
228
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)))
241
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."))
258 (t
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))))
290
291 ;;;###autoload (defalias 'browse-web 'eww)
292
293 ;;;###autoload
294 (defun eww-open-file (file)
295 "Render a 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))))
301
302 ;;;###autoload
303 (defun eww-search-words (&optional beg end)
304 "Search the web for the text between the point and marker.
305 See the `eww-search-prefix' variable for the search engine used."
306 (interactive "r")
307 (eww (buffer-substring beg end)))
308
309 (defun eww-render (status url &optional point buffer encode)
310 (let ((redirect (plist-get status :redirect)))
311 (when redirect
312 (setq url redirect)))
313 (let* ((headers (eww-parse-headers))
314 (content-type
315 (mail-header-parse-content-type
316 (or (cdr (assoc "content-type" headers))
317 "text/plain")))
318 (charset (intern
319 (downcase
320 (or (cdr (assq 'charset (cdr content-type)))
321 (eww-detect-charset (equal (car content-type)
322 "text/html"))
323 "utf-8"))))
324 (data-buffer (current-buffer)))
325 ;; Save the https peer status.
326 (with-current-buffer buffer
327 (plist-put eww-data :peer (plist-get status :peer)))
328 (unwind-protect
329 (progn
330 (cond
331 ((and eww-use-external-browser-for-content-type
332 (string-match-p eww-use-external-browser-for-content-type
333 (car content-type)))
334 (eww-browse-with-external-browser url))
335 ((equal (car content-type) "text/html")
336 (eww-display-html charset url nil point buffer encode))
337 ((equal (car content-type) "application/pdf")
338 (eww-display-pdf))
339 ((string-match-p "\\`image/" (car content-type))
340 (eww-display-image buffer))
341 (t
342 (eww-display-raw buffer encode)))
343 (with-current-buffer buffer
344 (plist-put eww-data :url url)
345 (eww-update-header-line-format)
346 (setq eww-history-position 0)
347 (run-hooks 'eww-after-render-hook)))
348 (kill-buffer data-buffer))))
349
350 (defun eww-parse-headers ()
351 (let ((headers nil))
352 (goto-char (point-min))
353 (while (and (not (eobp))
354 (not (eolp)))
355 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
356 (push (cons (downcase (match-string 1))
357 (match-string 2))
358 headers))
359 (forward-line 1))
360 (unless (eobp)
361 (forward-line 1))
362 headers))
363
364 (defun eww-detect-charset (html-p)
365 (let ((case-fold-search t)
366 (pt (point)))
367 (or (and html-p
368 (re-search-forward
369 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
370 (goto-char pt)
371 (match-string 1))
372 (and (looking-at
373 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
374 (match-string 1)))))
375
376 (declare-function libxml-parse-html-region "xml.c"
377 (start end &optional base-url))
378
379 (defun eww-display-html (charset url &optional document point buffer encode)
380 (unless (fboundp 'libxml-parse-html-region)
381 (error "This function requires Emacs to be compiled with libxml2"))
382 (unless (buffer-live-p buffer)
383 (error "Buffer %s doesn't exist" buffer))
384 ;; There should be a better way to abort loading images
385 ;; asynchronously.
386 (setq url-queue nil)
387 (let ((document
388 (or document
389 (list
390 'base (list (cons 'href url))
391 (progn
392 (when (or (and encode
393 (not (eq charset encode)))
394 (not (eq charset 'utf-8)))
395 (condition-case nil
396 (decode-coding-region (point) (point-max)
397 (or encode charset))
398 (coding-system-error nil)))
399 (libxml-parse-html-region (point) (point-max))))))
400 (source (and (null document)
401 (buffer-substring (point) (point-max)))))
402 (with-current-buffer buffer
403 (plist-put eww-data :source source)
404 (plist-put eww-data :dom document)
405 (let ((inhibit-read-only t)
406 (inhibit-modification-hooks t)
407 (shr-target-id (url-target (url-generic-parse-url url)))
408 (shr-external-rendering-functions
409 '((title . eww-tag-title)
410 (form . eww-tag-form)
411 (input . eww-tag-input)
412 (textarea . eww-tag-textarea)
413 (select . eww-tag-select)
414 (link . eww-tag-link)
415 (a . eww-tag-a))))
416 (erase-buffer)
417 (shr-insert-document document)
418 (cond
419 (point
420 (goto-char point))
421 (shr-target-id
422 (goto-char (point-min))
423 (let ((point (next-single-property-change
424 (point-min) 'shr-target-id)))
425 (when point
426 (goto-char point))))
427 (t
428 (goto-char (point-min))
429 ;; Don't leave point inside forms, because the normal eww
430 ;; commands aren't available there.
431 (while (and (not (eobp))
432 (get-text-property (point) 'eww-form))
433 (forward-line 1)))))
434 (eww-size-text-inputs))))
435
436 (defun eww-handle-link (dom)
437 (let* ((rel (dom-attr dom 'rel))
438 (href (dom-attr dom 'href))
439 (where (assoc
440 ;; The text associated with :rel is case-insensitive.
441 (if rel (downcase rel))
442 '(("next" . :next)
443 ;; Texinfo uses "previous", but HTML specifies
444 ;; "prev", so recognize both.
445 ("previous" . :previous)
446 ("prev" . :previous)
447 ;; HTML specifies "start" but also "contents",
448 ;; and Gtk seems to use "home". Recognize
449 ;; them all; but store them in different
450 ;; variables so that we can readily choose the
451 ;; "best" one.
452 ("start" . :start)
453 ("home" . :home)
454 ("contents" . :contents)
455 ("up" . :up)))))
456 (and href
457 where
458 (plist-put eww-data (cdr where) href))))
459
460 (defun eww-tag-link (dom)
461 (eww-handle-link dom)
462 (shr-generic dom))
463
464 (defun eww-tag-a (dom)
465 (eww-handle-link dom)
466 (let ((start (point)))
467 (shr-tag-a dom)
468 (put-text-property start (point) 'keymap eww-link-keymap)))
469
470 (defun eww-update-header-line-format ()
471 (setq header-line-format
472 (and eww-header-line-format
473 (let ((title (plist-get eww-data :title))
474 (peer (plist-get eww-data :peer)))
475 (when (zerop (length title))
476 (setq title "[untitled]"))
477 ;; This connection has is https.
478 (when peer
479 (setq title
480 (propertize title 'face
481 (if (plist-get peer :warnings)
482 'eww-invalid-certificate
483 'eww-valid-certificate))))
484 (replace-regexp-in-string
485 "%" "%%"
486 (format-spec
487 eww-header-line-format
488 `((?u . ,(or (plist-get eww-data :url) ""))
489 (?t . ,title))))))))
490
491 (defun eww-tag-title (dom)
492 (plist-put eww-data :title
493 (replace-regexp-in-string
494 "^ \\| $" ""
495 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
496 (eww-update-header-line-format))
497
498 (defun eww-display-raw (buffer &optional encode)
499 (let ((data (buffer-substring (point) (point-max))))
500 (unless (buffer-live-p buffer)
501 (error "Buffer %s doesn't exist" buffer))
502 (with-current-buffer buffer
503 (let ((inhibit-read-only t))
504 (erase-buffer)
505 (insert data)
506 (unless (eq encode 'utf-8)
507 (encode-coding-region (point-min) (1+ (length data)) 'utf-8)
508 (condition-case nil
509 (decode-coding-region (point-min) (1+ (length data)) encode)
510 (coding-system-error nil))))
511 (goto-char (point-min)))))
512
513 (defun eww-display-image (buffer)
514 (let ((data (shr-parse-image-data)))
515 (unless (buffer-live-p buffer)
516 (error "Buffer %s doesn't exist" buffer))
517 (with-current-buffer buffer
518 (let ((inhibit-read-only t))
519 (erase-buffer)
520 (shr-put-image data nil))
521 (goto-char (point-min)))))
522
523 (declare-function mailcap-view-mime "mailcap" (type))
524 (defun eww-display-pdf ()
525 (let ((data (buffer-substring (point) (point-max))))
526 (switch-to-buffer (get-buffer-create "*eww pdf*"))
527 (let ((coding-system-for-write 'raw-text)
528 (inhibit-read-only t))
529 (erase-buffer)
530 (insert data)
531 (mailcap-view-mime "application/pdf")))
532 (goto-char (point-min)))
533
534 (defun eww-setup-buffer ()
535 (switch-to-buffer (get-buffer-create "*eww*"))
536 (let ((inhibit-read-only t))
537 (remove-overlays)
538 (erase-buffer))
539 (unless (eq major-mode 'eww-mode)
540 (eww-mode)))
541
542 (defun eww-current-url nil
543 "Return URI of the Web page the current EWW buffer is visiting."
544 (plist-get eww-data :url))
545
546 (defun eww-links-at-point ()
547 "Return list of URIs, if any, linked at point."
548 (remq nil
549 (list (get-text-property (point) 'shr-url)
550 (get-text-property (point) 'image-url))))
551
552 (defun eww-view-source ()
553 "View the HTML source code of the current page."
554 (interactive)
555 (let ((buf (get-buffer-create "*eww-source*"))
556 (source (plist-get eww-data :source)))
557 (with-current-buffer buf
558 (let ((inhibit-read-only t))
559 (delete-region (point-min) (point-max))
560 (insert (or source "no source"))
561 (goto-char (point-min))
562 (when (fboundp 'html-mode)
563 (html-mode))))
564 (view-buffer buf)))
565
566 (defun eww-readable ()
567 "View the main \"readable\" parts of the current web page.
568 This command uses heuristics to find the parts of the web page that
569 contains the main textual portion, leaving out navigation menus and
570 the like."
571 (interactive)
572 (let* ((old-data eww-data)
573 (dom (with-temp-buffer
574 (insert (plist-get old-data :source))
575 (condition-case nil
576 (decode-coding-region (point-min) (point-max) 'utf-8)
577 (coding-system-error nil))
578 (libxml-parse-html-region (point-min) (point-max)))))
579 (eww-score-readability dom)
580 (eww-save-history)
581 (eww-display-html nil nil
582 (eww-highest-readability dom)
583 nil (current-buffer))
584 (dolist (elem '(:source :url :title :next :previous :up))
585 (plist-put eww-data elem (plist-get old-data elem)))
586 (eww-update-header-line-format)))
587
588 (defun eww-score-readability (node)
589 (let ((score -1))
590 (cond
591 ((memq (dom-tag node) '(script head comment))
592 (setq score -2))
593 ((eq (dom-tag node) 'meta)
594 (setq score -1))
595 ((eq (dom-tag node) 'img)
596 (setq score 2))
597 ((eq (dom-tag node) 'a)
598 (setq score (- (length (split-string (dom-text node))))))
599 (t
600 (dolist (elem (dom-children node))
601 (if (stringp elem)
602 (setq score (+ score (length (split-string elem))))
603 (setq score (+ score
604 (or (cdr (assoc :eww-readability-score (cdr elem)))
605 (eww-score-readability elem))))))))
606 ;; Cache the score of the node to avoid recomputing all the time.
607 (dom-set-attribute node :eww-readability-score score)
608 score))
609
610 (defun eww-highest-readability (node)
611 (let ((result node)
612 highest)
613 (dolist (elem (dom-non-text-children node))
614 (when (> (or (dom-attr
615 (setq highest (eww-highest-readability elem))
616 :eww-readability-score)
617 most-negative-fixnum)
618 (or (dom-attr result :eww-readability-score)
619 most-negative-fixnum))
620 (setq result highest)))
621 result))
622
623 (defvar eww-mode-map
624 (let ((map (make-sparse-keymap)))
625 (set-keymap-parent map special-mode-map)
626 (define-key map "g" 'eww-reload) ;FIXME: revert-buffer-function instead!
627 (define-key map "G" 'eww)
628 (define-key map [?\t] 'shr-next-link)
629 (define-key map [?\M-\t] 'shr-previous-link)
630 (define-key map [backtab] 'shr-previous-link)
631 (define-key map [delete] 'scroll-down-command)
632 (define-key map "l" 'eww-back-url)
633 (define-key map "r" 'eww-forward-url)
634 (define-key map "n" 'eww-next-url)
635 (define-key map "p" 'eww-previous-url)
636 (define-key map "u" 'eww-up-url)
637 (define-key map "t" 'eww-top-url)
638 (define-key map "&" 'eww-browse-with-external-browser)
639 (define-key map "d" 'eww-download)
640 (define-key map "w" 'eww-copy-page-url)
641 (define-key map "C" 'url-cookie-list)
642 (define-key map "v" 'eww-view-source)
643 (define-key map "R" 'eww-readable)
644 (define-key map "H" 'eww-list-histories)
645 (define-key map "E" 'eww-set-character-encoding)
646 (define-key map "S" 'eww-list-buffers)
647 (define-key map "F" 'eww-toggle-fonts)
648
649 (define-key map "b" 'eww-add-bookmark)
650 (define-key map "B" 'eww-list-bookmarks)
651 (define-key map [(meta n)] 'eww-next-bookmark)
652 (define-key map [(meta p)] 'eww-previous-bookmark)
653
654 (easy-menu-define nil map ""
655 '("Eww"
656 ["Exit" quit-window t]
657 ["Close browser" quit-window t]
658 ["Reload" eww-reload t]
659 ["Back to previous page" eww-back-url
660 :active (not (zerop (length eww-history)))]
661 ["Forward to next page" eww-forward-url
662 :active (not (zerop eww-history-position))]
663 ["Browse with external browser" eww-browse-with-external-browser t]
664 ["Download" eww-download t]
665 ["View page source" eww-view-source]
666 ["Copy page URL" eww-copy-page-url t]
667 ["List histories" eww-list-histories t]
668 ["List buffers" eww-list-buffers t]
669 ["Add bookmark" eww-add-bookmark t]
670 ["List bookmarks" eww-list-bookmarks t]
671 ["List cookies" url-cookie-list t]
672 ["Character Encoding" eww-set-character-encoding]))
673 map))
674
675 (defvar eww-tool-bar-map
676 (let ((map (make-sparse-keymap)))
677 (dolist (tool-bar-item
678 '((quit-window . "close")
679 (eww-reload . "refresh")
680 (eww-back-url . "left-arrow")
681 (eww-forward-url . "right-arrow")
682 (eww-view-source . "show")
683 (eww-copy-page-url . "copy")
684 (eww-add-bookmark . "bookmark_add"))) ;; ...
685 (tool-bar-local-item-from-menu
686 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
687 map)
688 "Tool bar for `eww-mode'.")
689
690 ;; Autoload cookie needed by desktop.el.
691 ;;;###autoload
692 (define-derived-mode eww-mode special-mode "eww"
693 "Mode for browsing the web."
694 (setq-local eww-data (list :title ""))
695 (setq-local browse-url-browser-function #'eww-browse-url)
696 (add-hook 'after-change-functions #'eww-process-text-input nil t)
697 (setq-local eww-history nil)
698 (setq-local eww-history-position 0)
699 (when (boundp 'tool-bar-map)
700 (setq-local tool-bar-map eww-tool-bar-map))
701 ;; desktop support
702 (setq-local desktop-save-buffer #'eww-desktop-misc-data)
703 ;; multi-page isearch support
704 (setq-local multi-isearch-next-buffer-function #'eww-isearch-next-buffer)
705 (setq truncate-lines t)
706 (buffer-disable-undo)
707 (setq buffer-read-only t))
708
709 ;;;###autoload
710 (defun eww-browse-url (url &optional new-window)
711 (cond (new-window
712 (switch-to-buffer (generate-new-buffer "*eww*"))
713 (eww-mode)))
714 (eww url))
715
716 (defun eww-back-url ()
717 "Go to the previously displayed page."
718 (interactive)
719 (when (>= eww-history-position (length eww-history))
720 (user-error "No previous page"))
721 (eww-save-history)
722 (setq eww-history-position (+ eww-history-position 2))
723 (eww-restore-history (elt eww-history (1- eww-history-position))))
724
725 (defun eww-forward-url ()
726 "Go to the next displayed page."
727 (interactive)
728 (when (zerop eww-history-position)
729 (user-error "No next page"))
730 (eww-save-history)
731 (eww-restore-history (elt eww-history (1- eww-history-position))))
732
733 (defun eww-restore-history (elem)
734 (let ((inhibit-read-only t)
735 (inhibit-modification-hooks t)
736 (text (plist-get elem :text)))
737 (setq eww-data elem)
738 (if (null text)
739 (eww-reload) ; FIXME: restore :point?
740 (erase-buffer)
741 (insert text)
742 (goto-char (plist-get elem :point))
743 (eww-update-header-line-format))))
744
745 (defun eww-next-url ()
746 "Go to the page marked `next'.
747 A page is marked `next' if rel=\"next\" appears in a <link>
748 or <a> tag."
749 (interactive)
750 (if (plist-get eww-data :next)
751 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
752 (plist-get eww-data :url)))
753 (user-error "No `next' on this page")))
754
755 (defun eww-previous-url ()
756 "Go to the page marked `previous'.
757 A page is marked `previous' if rel=\"previous\" appears in a <link>
758 or <a> tag."
759 (interactive)
760 (if (plist-get eww-data :previous)
761 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
762 (plist-get eww-data :url)))
763 (user-error "No `previous' on this page")))
764
765 (defun eww-up-url ()
766 "Go to the page marked `up'.
767 A page is marked `up' if rel=\"up\" appears in a <link>
768 or <a> tag."
769 (interactive)
770 (if (plist-get eww-data :up)
771 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
772 (plist-get eww-data :url)))
773 (user-error "No `up' on this page")))
774
775 (defun eww-top-url ()
776 "Go to the page marked `top'.
777 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
778 appears in a <link> or <a> tag."
779 (interactive)
780 (let ((best-url (or (plist-get eww-data :start)
781 (plist-get eww-data :contents)
782 (plist-get eww-data :home))))
783 (if best-url
784 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
785 (user-error "No `top' for this page"))))
786
787 (defun eww-reload (&optional local encode)
788 "Reload the current page.
789 If LOCAL (the command prefix), don't reload the page from the
790 network, but just re-display the HTML already fetched."
791 (interactive "P")
792 (let ((url (plist-get eww-data :url)))
793 (if local
794 (if (null (plist-get eww-data :dom))
795 (error "No current HTML data")
796 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
797 (point) (current-buffer)))
798 (url-retrieve url 'eww-render
799 (list url (point) (current-buffer) encode)))))
800
801 ;; Form support.
802
803 (defvar eww-form nil)
804
805 (defvar eww-submit-map
806 (let ((map (make-sparse-keymap)))
807 (define-key map "\r" 'eww-submit)
808 (define-key map [(control c) (control c)] 'eww-submit)
809 map))
810
811 (defvar eww-submit-file
812 (let ((map (make-sparse-keymap)))
813 (define-key map "\r" 'eww-select-file)
814 (define-key map [(control c) (control c)] 'eww-submit)
815 map))
816
817 (defvar eww-checkbox-map
818 (let ((map (make-sparse-keymap)))
819 (define-key map " " 'eww-toggle-checkbox)
820 (define-key map "\r" 'eww-toggle-checkbox)
821 (define-key map [(control c) (control c)] 'eww-submit)
822 map))
823
824 (defvar eww-text-map
825 (let ((map (make-keymap)))
826 (set-keymap-parent map text-mode-map)
827 (define-key map "\r" 'eww-submit)
828 (define-key map [(control a)] 'eww-beginning-of-text)
829 (define-key map [(control c) (control c)] 'eww-submit)
830 (define-key map [(control e)] 'eww-end-of-text)
831 (define-key map [?\t] 'shr-next-link)
832 (define-key map [?\M-\t] 'shr-previous-link)
833 map))
834
835 (defvar eww-textarea-map
836 (let ((map (make-keymap)))
837 (set-keymap-parent map text-mode-map)
838 (define-key map "\r" 'forward-line)
839 (define-key map [(control c) (control c)] 'eww-submit)
840 (define-key map [?\t] 'shr-next-link)
841 (define-key map [?\M-\t] 'shr-previous-link)
842 map))
843
844 (defvar eww-select-map
845 (let ((map (make-sparse-keymap)))
846 (define-key map "\r" 'eww-change-select)
847 (define-key map [(control c) (control c)] 'eww-submit)
848 map))
849
850 (defun eww-beginning-of-text ()
851 "Move to the start of the input field."
852 (interactive)
853 (goto-char (eww-beginning-of-field)))
854
855 (defun eww-end-of-text ()
856 "Move to the end of the text in the input field."
857 (interactive)
858 (goto-char (eww-end-of-field))
859 (let ((start (eww-beginning-of-field)))
860 (while (and (equal (following-char) ? )
861 (> (point) start))
862 (forward-char -1))
863 (when (> (point) start)
864 (forward-char 1))))
865
866 (defun eww-beginning-of-field ()
867 (cond
868 ((bobp)
869 (point))
870 ((not (eq (get-text-property (point) 'eww-form)
871 (get-text-property (1- (point)) 'eww-form)))
872 (point))
873 (t
874 (previous-single-property-change
875 (point) 'eww-form nil (point-min)))))
876
877 (defun eww-end-of-field ()
878 (1- (next-single-property-change
879 (point) 'eww-form nil (point-max))))
880
881 (defun eww-tag-form (dom)
882 (let ((eww-form (list (cons :method (dom-attr dom 'method))
883 (cons :action (dom-attr dom 'action))))
884 (start (point)))
885 (shr-ensure-paragraph)
886 (shr-generic dom)
887 (unless (bolp)
888 (insert "\n"))
889 (insert "\n")
890 (when (> (point) start)
891 (put-text-property start (1+ start)
892 'eww-form eww-form))))
893
894 (defun eww-form-submit (dom)
895 (let ((start (point))
896 (value (dom-attr dom 'value)))
897 (setq value
898 (if (zerop (length value))
899 "Submit"
900 value))
901 (insert value)
902 (add-face-text-property start (point) 'eww-form-submit)
903 (put-text-property start (point) 'eww-form
904 (list :eww-form eww-form
905 :value value
906 :type "submit"
907 :name (dom-attr dom 'name)))
908 (put-text-property start (point) 'keymap eww-submit-map)
909 (insert " ")))
910
911 (defun eww-form-checkbox (dom)
912 (let ((start (point)))
913 (if (dom-attr dom 'checked)
914 (insert eww-form-checkbox-selected-symbol)
915 (insert eww-form-checkbox-symbol))
916 (add-face-text-property start (point) 'eww-form-checkbox)
917 (put-text-property start (point) 'eww-form
918 (list :eww-form eww-form
919 :value (dom-attr dom 'value)
920 :type (downcase (dom-attr dom 'type))
921 :checked (dom-attr dom 'checked)
922 :name (dom-attr dom 'name)))
923 (put-text-property start (point) 'keymap eww-checkbox-map)
924 (insert " ")))
925
926 (defun eww-form-file (dom)
927 (let ((start (point))
928 (value (dom-attr dom 'value)))
929 (setq value
930 (if (zerop (length value))
931 " No file selected"
932 value))
933 (insert "Browse")
934 (add-face-text-property start (point) 'eww-form-file)
935 (insert value)
936 (put-text-property start (point) 'eww-form
937 (list :eww-form eww-form
938 :value (dom-attr dom 'value)
939 :type (downcase (dom-attr dom 'type))
940 :name (dom-attr dom 'name)))
941 (put-text-property start (point) 'keymap eww-submit-file)
942 (insert " ")))
943
944 (defun eww-select-file ()
945 "Change the value of the upload file menu under point."
946 (interactive)
947 (let* ((input (get-text-property (point) 'eww-form)))
948 (let ((filename
949 (let ((insert-default-directory t))
950 (read-file-name "filename: "))))
951 (eww-update-field filename (length "Browse"))
952 (plist-put input :filename filename))))
953
954 (defun eww-form-text (dom)
955 (let ((start (point))
956 (type (downcase (or (dom-attr dom 'type) "text")))
957 (value (or (dom-attr dom 'value) ""))
958 (width (string-to-number (or (dom-attr dom 'size) "40")))
959 (readonly-property (if (or (dom-attr dom 'disabled)
960 (dom-attr dom 'readonly))
961 'read-only
962 'inhibit-read-only)))
963 (insert value)
964 (when (< (length value) width)
965 (insert (make-string (- width (length value)) ? )))
966 (put-text-property start (point) 'face 'eww-form-text)
967 (put-text-property start (point) 'inhibit-read-only t)
968 (put-text-property start (point) 'local-map eww-text-map)
969 (put-text-property start (point) readonly-property t)
970 (put-text-property start (point) 'eww-form
971 (list :eww-form eww-form
972 :value value
973 :type type
974 :name (dom-attr dom 'name)))
975 (insert " ")))
976
977 (defconst eww-text-input-types '("text" "password" "textarea"
978 "color" "date" "datetime" "datetime-local"
979 "email" "month" "number" "search" "tel"
980 "time" "url" "week")
981 "List of input types which represent a text input.
982 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
983
984 (defun eww-process-text-input (beg end replace-length)
985 (when-let (pos (and (< (1+ end) (point-max))
986 (> (1- end) (point-min))
987 (cond
988 ((get-text-property (1+ end) 'eww-form)
989 (1+ end))
990 ((get-text-property (1- end) 'eww-form)
991 (1- end)))))
992 (let* ((form (get-text-property pos 'eww-form))
993 (properties (text-properties-at pos))
994 (inhibit-read-only t)
995 (length (- end beg replace-length))
996 (type (plist-get form :type)))
997 (when (and form
998 (member type eww-text-input-types))
999 (cond
1000 ((> length 0)
1001 ;; Delete some space at the end.
1002 (save-excursion
1003 (goto-char
1004 (if (equal type "textarea")
1005 (1- (line-end-position))
1006 (eww-end-of-field)))
1007 (while (and (> length 0)
1008 (eql (following-char) ? ))
1009 (delete-region (1- (point)) (point))
1010 (cl-decf length))))
1011 ((< length 0)
1012 ;; Add padding.
1013 (save-excursion
1014 (goto-char (1- end))
1015 (goto-char
1016 (if (equal type "textarea")
1017 (1- (line-end-position))
1018 (1+ (eww-end-of-field))))
1019 (let ((start (point)))
1020 (insert (make-string (abs length) ? ))
1021 (set-text-properties start (point) properties))
1022 (goto-char (1- end)))))
1023 (set-text-properties (plist-get form :start) (plist-get form :end)
1024 properties)
1025 (let ((value (buffer-substring-no-properties
1026 (eww-beginning-of-field)
1027 (eww-end-of-field))))
1028 (when (string-match " +\\'" value)
1029 (setq value (substring value 0 (match-beginning 0))))
1030 (plist-put form :value value)
1031 (when (equal type "password")
1032 ;; Display passwords as asterisks.
1033 (let ((start (eww-beginning-of-field)))
1034 (put-text-property start (+ start (length value))
1035 'display (make-string (length value) ?*)))))))))
1036
1037 (defun eww-tag-textarea (dom)
1038 (let ((start (point))
1039 (value (or (dom-attr dom 'value) ""))
1040 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1041 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1042 end)
1043 (shr-ensure-newline)
1044 (insert value)
1045 (shr-ensure-newline)
1046 (when (< (count-lines start (point)) lines)
1047 (dotimes (_ (- lines (count-lines start (point))))
1048 (insert "\n")))
1049 (setq end (point-marker))
1050 (goto-char start)
1051 (while (< (point) end)
1052 (end-of-line)
1053 (let ((pad (- width (- (point) (line-beginning-position)))))
1054 (when (> pad 0)
1055 (insert (make-string pad ? ))))
1056 (add-face-text-property (line-beginning-position)
1057 (point) 'eww-form-textarea)
1058 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1059 (put-text-property (line-beginning-position) (point)
1060 'local-map eww-textarea-map)
1061 (forward-line 1))
1062 (put-text-property start (point) 'eww-form
1063 (list :eww-form eww-form
1064 :value value
1065 :type "textarea"
1066 :name (dom-attr dom 'name)))))
1067
1068 (defun eww-tag-input (dom)
1069 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1070 (start (point)))
1071 (cond
1072 ((or (equal type "checkbox")
1073 (equal type "radio"))
1074 (eww-form-checkbox dom))
1075 ((equal type "file")
1076 (eww-form-file dom))
1077 ((equal type "submit")
1078 (eww-form-submit dom))
1079 ((equal type "hidden")
1080 (let ((form eww-form)
1081 (name (dom-attr dom 'name)))
1082 ;; Don't add <input type=hidden> elements repeatedly.
1083 (while (and form
1084 (or (not (consp (car form)))
1085 (not (eq (caar form) 'hidden))
1086 (not (equal (plist-get (cdr (car form)) :name)
1087 name))))
1088 (setq form (cdr form)))
1089 (unless form
1090 (nconc eww-form (list
1091 (list 'hidden
1092 :name name
1093 :value (dom-attr dom 'value)))))))
1094 (t
1095 (eww-form-text dom)))
1096 (unless (= start (point))
1097 (put-text-property start (1+ start) 'help-echo "Input field"))))
1098
1099 (defun eww-tag-select (dom)
1100 (shr-ensure-paragraph)
1101 (let ((menu (list :name (dom-attr dom 'name)
1102 :eww-form eww-form))
1103 (options nil)
1104 (start (point))
1105 (max 0)
1106 opelem)
1107 (if (eq (dom-tag dom) 'optgroup)
1108 (dolist (groupelem (dom-children dom))
1109 (unless (dom-attr groupelem 'disabled)
1110 (setq opelem (append opelem (list groupelem)))))
1111 (setq opelem (list dom)))
1112 (dolist (elem opelem)
1113 (when (eq (dom-tag elem) 'option)
1114 (when (dom-attr elem 'selected)
1115 (nconc menu (list :value (dom-attr elem 'value))))
1116 (let ((display (dom-text elem)))
1117 (setq max (max max (length display)))
1118 (push (list 'item
1119 :value (dom-attr elem 'value)
1120 :display display)
1121 options))))
1122 (when options
1123 (setq options (nreverse options))
1124 ;; If we have no selected values, default to the first value.
1125 (unless (plist-get menu :value)
1126 (nconc menu (list :value (nth 2 (car options)))))
1127 (nconc menu options)
1128 (let ((selected (eww-select-display menu)))
1129 (insert selected
1130 (make-string (- max (length selected)) ? )))
1131 (put-text-property start (point) 'eww-form menu)
1132 (add-face-text-property start (point) 'eww-form-select)
1133 (put-text-property start (point) 'keymap eww-select-map)
1134 (unless (= start (point))
1135 (put-text-property start (1+ start) 'help-echo "select field"))
1136 (shr-ensure-paragraph))))
1137
1138 (defun eww-select-display (select)
1139 (let ((value (plist-get select :value))
1140 display)
1141 (dolist (elem select)
1142 (when (and (consp elem)
1143 (eq (car elem) 'item)
1144 (equal value (plist-get (cdr elem) :value)))
1145 (setq display (plist-get (cdr elem) :display))))
1146 display))
1147
1148 (defun eww-change-select ()
1149 "Change the value of the select drop-down menu under point."
1150 (interactive)
1151 (let* ((input (get-text-property (point) 'eww-form))
1152 (completion-ignore-case t)
1153 (options
1154 (delq nil
1155 (mapcar (lambda (elem)
1156 (and (consp elem)
1157 (eq (car elem) 'item)
1158 (cons (plist-get (cdr elem) :display)
1159 (plist-get (cdr elem) :value))))
1160 input)))
1161 (display
1162 (completing-read "Change value: " options nil 'require-match))
1163 (inhibit-read-only t))
1164 (plist-put input :value (cdr (assoc-string display options t)))
1165 (goto-char
1166 (eww-update-field display))))
1167
1168 (defun eww-update-field (string &optional offset)
1169 (if (not offset) (setq offset 0))
1170 (let ((properties (text-properties-at (point)))
1171 (start (+ (eww-beginning-of-field) offset))
1172 (current-end (1+ (eww-end-of-field)))
1173 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1174 (delete-region start current-end)
1175 (forward-char offset)
1176 (insert string
1177 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1178 (if (= 0 offset) (set-text-properties start new-end properties))
1179 start))
1180
1181 (defun eww-toggle-checkbox ()
1182 "Toggle the value of the checkbox under point."
1183 (interactive)
1184 (let* ((input (get-text-property (point) 'eww-form))
1185 (type (plist-get input :type)))
1186 (if (equal type "checkbox")
1187 (goto-char
1188 (1+
1189 (if (plist-get input :checked)
1190 (progn
1191 (plist-put input :checked nil)
1192 (eww-update-field eww-form-checkbox-symbol))
1193 (plist-put input :checked t)
1194 (eww-update-field eww-form-checkbox-selected-symbol))))
1195 ;; Radio button. Switch all other buttons off.
1196 (let ((name (plist-get input :name)))
1197 (save-excursion
1198 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1199 (when (equal (plist-get (cdr elem) :name) name)
1200 (goto-char (car elem))
1201 (if (not (eq (cdr elem) input))
1202 (progn
1203 (plist-put input :checked nil)
1204 (eww-update-field eww-form-checkbox-symbol))
1205 (plist-put input :checked t)
1206 (eww-update-field eww-form-checkbox-selected-symbol)))))
1207 (forward-char 1)))))
1208
1209 (defun eww-inputs (form)
1210 (let ((start (point-min))
1211 (inputs nil))
1212 (while (and start
1213 (< start (point-max)))
1214 (when (or (get-text-property start 'eww-form)
1215 (setq start (next-single-property-change start 'eww-form)))
1216 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1217 form)
1218 (push (cons start (get-text-property start 'eww-form))
1219 inputs))
1220 (setq start (next-single-property-change start 'eww-form))))
1221 (nreverse inputs)))
1222
1223 (defun eww-size-text-inputs ()
1224 (let ((start (point-min)))
1225 (while (and start
1226 (< start (point-max)))
1227 (when (or (get-text-property start 'eww-form)
1228 (setq start (next-single-property-change start 'eww-form)))
1229 (let ((props (get-text-property start 'eww-form)))
1230 (plist-put props :start start)
1231 (setq start (next-single-property-change
1232 start 'eww-form nil (point-max)))
1233 (plist-put props :end start))))))
1234
1235 (defun eww-input-value (input)
1236 (let ((type (plist-get input :type))
1237 (value (plist-get input :value)))
1238 (cond
1239 ((equal type "textarea")
1240 (with-temp-buffer
1241 (insert value)
1242 (goto-char (point-min))
1243 (while (re-search-forward "^ +\n\\| +$" nil t)
1244 (replace-match "" t t))
1245 (buffer-string)))
1246 (t
1247 (if (string-match " +\\'" value)
1248 (substring value 0 (match-beginning 0))
1249 value)))))
1250
1251 (defun eww-submit ()
1252 "Submit the current form."
1253 (interactive)
1254 (let* ((this-input (get-text-property (point) 'eww-form))
1255 (form (plist-get this-input :eww-form))
1256 values next-submit)
1257 (dolist (elem (sort (eww-inputs form)
1258 (lambda (o1 o2)
1259 (< (car o1) (car o2)))))
1260 (let* ((input (cdr elem))
1261 (input-start (car elem))
1262 (name (plist-get input :name)))
1263 (when name
1264 (cond
1265 ((member (plist-get input :type) '("checkbox" "radio"))
1266 (when (plist-get input :checked)
1267 (push (cons name (plist-get input :value))
1268 values)))
1269 ((equal (plist-get input :type) "file")
1270 (push (cons "file"
1271 (list (cons "filedata"
1272 (with-temp-buffer
1273 (insert-file-contents
1274 (plist-get input :filename))
1275 (buffer-string)))
1276 (cons "name" (plist-get input :name))
1277 (cons "filename" (plist-get input :filename))))
1278 values))
1279 ((equal (plist-get input :type) "submit")
1280 ;; We want the values from buttons if we hit a button if
1281 ;; we hit enter on it, or if it's the first button after
1282 ;; the field we did hit return on.
1283 (when (or (eq input this-input)
1284 (and (not (eq input this-input))
1285 (null next-submit)
1286 (> input-start (point))))
1287 (setq next-submit t)
1288 (push (cons name (plist-get input :value))
1289 values)))
1290 (t
1291 (push (cons name (eww-input-value input))
1292 values))))))
1293 (dolist (elem form)
1294 (when (and (consp elem)
1295 (eq (car elem) 'hidden))
1296 (push (cons (plist-get (cdr elem) :name)
1297 (or (plist-get (cdr elem) :value) ""))
1298 values)))
1299 (if (and (stringp (cdr (assq :method form)))
1300 (equal (downcase (cdr (assq :method form))) "post"))
1301 (let ((mtype))
1302 (dolist (x values mtype)
1303 (if (equal (car x) "file")
1304 (progn
1305 (setq mtype "multipart/form-data"))))
1306 (cond ((equal mtype "multipart/form-data")
1307 (let ((boundary (mml-compute-boundary '())))
1308 (let ((url-request-method "POST")
1309 (url-request-extra-headers
1310 (list (cons "Content-Type"
1311 (concat "multipart/form-data; boundary="
1312 boundary))))
1313 (url-request-data
1314 (mm-url-encode-multipart-form-data values boundary)))
1315 (eww-browse-url (shr-expand-url
1316 (cdr (assq :action form))
1317 (plist-get eww-data :url))))))
1318 (t
1319 (let ((url-request-method "POST")
1320 (url-request-extra-headers
1321 '(("Content-Type" .
1322 "application/x-www-form-urlencoded")))
1323 (url-request-data
1324 (mm-url-encode-www-form-urlencoded values)))
1325 (eww-browse-url (shr-expand-url
1326 (cdr (assq :action form))
1327 (plist-get eww-data :url)))))))
1328 (eww-browse-url
1329 (concat
1330 (if (cdr (assq :action form))
1331 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1332 (plist-get eww-data :url))
1333 "?"
1334 (mm-url-encode-www-form-urlencoded values))))))
1335
1336 (defun eww-browse-with-external-browser (&optional url)
1337 "Browse the current URL with an external browser.
1338 The browser to used is specified by the `shr-external-browser' variable."
1339 (interactive)
1340 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1341
1342 (defun eww-follow-link (&optional external mouse-event)
1343 "Browse the URL under point.
1344 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1345 If EXTERNAL is double prefix, browse in new buffer."
1346 (interactive (list current-prefix-arg last-nonmenu-event))
1347 (mouse-set-point mouse-event)
1348 (let ((url (get-text-property (point) 'shr-url)))
1349 (cond
1350 ((not url)
1351 (message "No link under point"))
1352 ((string-match "^mailto:" url)
1353 (browse-url-mail url))
1354 ((and (consp external) (<= (car external) 4))
1355 (funcall shr-external-browser url))
1356 ;; This is a #target url in the same page as the current one.
1357 ((and (url-target (url-generic-parse-url url))
1358 (eww-same-page-p url (plist-get eww-data :url)))
1359 (let ((dom (plist-get eww-data :dom)))
1360 (eww-save-history)
1361 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1362 (t
1363 (eww-browse-url url external)))))
1364
1365 (defun eww-same-page-p (url1 url2)
1366 "Return non-nil if both URLs represent the same page.
1367 Differences in #targets are ignored."
1368 (let ((obj1 (url-generic-parse-url url1))
1369 (obj2 (url-generic-parse-url url2)))
1370 (setf (url-target obj1) nil)
1371 (setf (url-target obj2) nil)
1372 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1373
1374 (defun eww-copy-page-url ()
1375 "Copy the URL of the current page into the kill ring."
1376 (interactive)
1377 (message "%s" (plist-get eww-data :url))
1378 (kill-new (plist-get eww-data :url)))
1379
1380 (defun eww-download ()
1381 "Download URL under point to `eww-download-directory'."
1382 (interactive)
1383 (let ((url (get-text-property (point) 'shr-url)))
1384 (if (not url)
1385 (message "No URL under point")
1386 (url-retrieve url 'eww-download-callback (list url)))))
1387
1388 (defun eww-download-callback (status url)
1389 (unless (plist-get status :error)
1390 (let* ((obj (url-generic-parse-url url))
1391 (path (car (url-path-and-query obj)))
1392 (file (eww-make-unique-file-name (file-name-nondirectory path)
1393 eww-download-directory)))
1394 (goto-char (point-min))
1395 (re-search-forward "\r?\n\r?\n")
1396 (write-region (point) (point-max) file)
1397 (message "Saved %s" file))))
1398
1399 (defun eww-make-unique-file-name (file directory)
1400 (cond
1401 ((zerop (length file))
1402 (setq file "!"))
1403 ((string-match "\\`[.]" file)
1404 (setq file (concat "!" file))))
1405 (let ((count 1))
1406 (while (file-exists-p (expand-file-name file directory))
1407 (setq file
1408 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1409 (format "%s(%d)%s" (match-string 1 file)
1410 count (match-string 2 file))
1411 (format "%s(%d)" file count)))
1412 (setq count (1+ count)))
1413 (expand-file-name file directory)))
1414
1415 (defun eww-set-character-encoding (charset)
1416 "Set character encoding."
1417 (interactive "zUse character set (default utf-8): ")
1418 (if (null charset)
1419 (eww-reload nil 'utf-8)
1420 (eww-reload nil charset)))
1421
1422 (defun eww-toggle-fonts ()
1423 "Toggle whether to use monospaced or font-enabled layouts."
1424 (interactive)
1425 (message "Fonts are now %s"
1426 (if (setq shr-use-fonts (not shr-use-fonts))
1427 "on"
1428 "off"))
1429 (eww-reload))
1430
1431 ;;; Bookmarks code
1432
1433 (defvar eww-bookmarks nil)
1434
1435 (defun eww-add-bookmark ()
1436 "Bookmark the current page."
1437 (interactive)
1438 (eww-read-bookmarks)
1439 (dolist (bookmark eww-bookmarks)
1440 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1441 (user-error "Already bookmarked")))
1442 (when (y-or-n-p "Bookmark this page?")
1443 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1444 (plist-get eww-data :title))))
1445 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1446 (push (list :url (plist-get eww-data :url)
1447 :title title
1448 :time (current-time-string))
1449 eww-bookmarks))
1450 (eww-write-bookmarks)
1451 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1452 (plist-get eww-data :title))))
1453
1454 (defun eww-write-bookmarks ()
1455 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1456 (insert ";; Auto-generated file; don't edit\n")
1457 (pp eww-bookmarks (current-buffer))))
1458
1459 (defun eww-read-bookmarks ()
1460 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1461 (setq eww-bookmarks
1462 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1463 (with-temp-buffer
1464 (insert-file-contents file)
1465 (read (current-buffer)))))))
1466
1467 ;;;###autoload
1468 (defun eww-list-bookmarks ()
1469 "Display the bookmarks."
1470 (interactive)
1471 (eww-bookmark-prepare)
1472 (pop-to-buffer "*eww bookmarks*"))
1473
1474 (defun eww-bookmark-prepare ()
1475 (eww-read-bookmarks)
1476 (unless eww-bookmarks
1477 (user-error "No bookmarks are defined"))
1478 (set-buffer (get-buffer-create "*eww bookmarks*"))
1479 (eww-bookmark-mode)
1480 (let* ((width (/ (window-width) 2))
1481 (format (format "%%-%ds %%s" width))
1482 (inhibit-read-only t)
1483 start title)
1484 (erase-buffer)
1485 (setq header-line-format (concat " " (format format "Title" "URL")))
1486 (dolist (bookmark eww-bookmarks)
1487 (setq start (point)
1488 title (plist-get bookmark :title))
1489 (when (> (length title) width)
1490 (setq title (substring title 0 width)))
1491 (insert (format format title (plist-get bookmark :url)) "\n")
1492 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1493 (goto-char (point-min))))
1494
1495 (defvar eww-bookmark-kill-ring nil)
1496
1497 (defun eww-bookmark-kill ()
1498 "Kill the current bookmark."
1499 (interactive)
1500 (let* ((start (line-beginning-position))
1501 (bookmark (get-text-property start 'eww-bookmark))
1502 (inhibit-read-only t))
1503 (unless bookmark
1504 (user-error "No bookmark on the current line"))
1505 (forward-line 1)
1506 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1507 (delete-region start (point))
1508 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1509 (eww-write-bookmarks)))
1510
1511 (defun eww-bookmark-yank ()
1512 "Yank a previously killed bookmark to the current line."
1513 (interactive)
1514 (unless eww-bookmark-kill-ring
1515 (user-error "No previously killed bookmark"))
1516 (beginning-of-line)
1517 (let ((inhibit-read-only t)
1518 (start (point))
1519 bookmark)
1520 (insert (pop eww-bookmark-kill-ring))
1521 (setq bookmark (get-text-property start 'eww-bookmark))
1522 (if (= start (point-min))
1523 (push bookmark eww-bookmarks)
1524 (let ((line (count-lines start (point))))
1525 (setcdr (nthcdr (1- line) eww-bookmarks)
1526 (cons bookmark (nthcdr line eww-bookmarks)))))
1527 (eww-write-bookmarks)))
1528
1529 (defun eww-bookmark-browse ()
1530 "Browse the bookmark under point in eww."
1531 (interactive)
1532 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1533 (unless bookmark
1534 (user-error "No bookmark on the current line"))
1535 (quit-window)
1536 (eww-browse-url (plist-get bookmark :url))))
1537
1538 (defun eww-next-bookmark ()
1539 "Go to the next bookmark in the list."
1540 (interactive)
1541 (let ((first nil)
1542 bookmark)
1543 (unless (get-buffer "*eww bookmarks*")
1544 (setq first t)
1545 (eww-bookmark-prepare))
1546 (with-current-buffer (get-buffer "*eww bookmarks*")
1547 (when (and (not first)
1548 (not (eobp)))
1549 (forward-line 1))
1550 (setq bookmark (get-text-property (line-beginning-position)
1551 'eww-bookmark))
1552 (unless bookmark
1553 (user-error "No next bookmark")))
1554 (eww-browse-url (plist-get bookmark :url))))
1555
1556 (defun eww-previous-bookmark ()
1557 "Go to the previous bookmark in the list."
1558 (interactive)
1559 (let ((first nil)
1560 bookmark)
1561 (unless (get-buffer "*eww bookmarks*")
1562 (setq first t)
1563 (eww-bookmark-prepare))
1564 (with-current-buffer (get-buffer "*eww bookmarks*")
1565 (if first
1566 (goto-char (point-max))
1567 (beginning-of-line))
1568 ;; On the final line.
1569 (when (eolp)
1570 (forward-line -1))
1571 (if (bobp)
1572 (user-error "No previous bookmark")
1573 (forward-line -1))
1574 (setq bookmark (get-text-property (line-beginning-position)
1575 'eww-bookmark)))
1576 (eww-browse-url (plist-get bookmark :url))))
1577
1578 (defvar eww-bookmark-mode-map
1579 (let ((map (make-sparse-keymap)))
1580 (suppress-keymap map)
1581 (define-key map "q" 'quit-window)
1582 (define-key map [(control k)] 'eww-bookmark-kill)
1583 (define-key map [(control y)] 'eww-bookmark-yank)
1584 (define-key map "\r" 'eww-bookmark-browse)
1585
1586 (easy-menu-define nil map
1587 "Menu for `eww-bookmark-mode-map'."
1588 '("Eww Bookmark"
1589 ["Exit" quit-window t]
1590 ["Browse" eww-bookmark-browse
1591 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1592 ["Kill" eww-bookmark-kill
1593 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1594 ["Yank" eww-bookmark-yank
1595 :active eww-bookmark-kill-ring]))
1596 map))
1597
1598 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1599 "Mode for listing bookmarks.
1600
1601 \\{eww-bookmark-mode-map}"
1602 (buffer-disable-undo)
1603 (setq buffer-read-only t
1604 truncate-lines t))
1605
1606 ;;; History code
1607
1608 (defun eww-save-history ()
1609 (plist-put eww-data :point (point))
1610 (plist-put eww-data :text (buffer-string))
1611 (push eww-data eww-history)
1612 (setq eww-data (list :title ""))
1613 ;; Don't let the history grow infinitely. We store quite a lot of
1614 ;; data per page.
1615 (when-let (tail (and eww-history-limit
1616 (nthcdr eww-history-limit eww-history)))
1617 (setcdr tail nil)))
1618
1619 (defvar eww-current-buffer)
1620
1621 (defun eww-list-histories ()
1622 "List the eww-histories."
1623 (interactive)
1624 (when (null eww-history)
1625 (error "No eww-histories are defined"))
1626 (let ((eww-history-trans eww-history)
1627 (buffer (current-buffer)))
1628 (set-buffer (get-buffer-create "*eww history*"))
1629 (eww-history-mode)
1630 (setq-local eww-current-buffer buffer)
1631 (let ((inhibit-read-only t)
1632 (domain-length 0)
1633 (title-length 0)
1634 url title format start)
1635 (erase-buffer)
1636 (dolist (history eww-history-trans)
1637 (setq start (point))
1638 (setq domain-length (max domain-length (length (plist-get history :url))))
1639 (setq title-length (max title-length (length (plist-get history :title)))))
1640 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1641 header-line-format
1642 (concat " " (format format "Title" "URL")))
1643 (dolist (history eww-history-trans)
1644 (setq start (point))
1645 (setq url (plist-get history :url))
1646 (setq title (plist-get history :title))
1647 (insert (format format title url))
1648 (insert "\n")
1649 (put-text-property start (1+ start) 'eww-history history))
1650 (goto-char (point-min)))
1651 (pop-to-buffer "*eww history*")))
1652
1653 (defun eww-history-browse ()
1654 "Browse the history under point in eww."
1655 (interactive)
1656 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1657 (unless history
1658 (error "No history on the current line"))
1659 (let ((buffer eww-current-buffer))
1660 (quit-window)
1661 (when buffer
1662 (switch-to-buffer buffer)))
1663 (eww-restore-history history)))
1664
1665 (defvar eww-history-mode-map
1666 (let ((map (make-sparse-keymap)))
1667 (suppress-keymap map)
1668 (define-key map "q" 'quit-window)
1669 (define-key map "\r" 'eww-history-browse)
1670 ;; (define-key map "n" 'next-error-no-select)
1671 ;; (define-key map "p" 'previous-error-no-select)
1672
1673 (easy-menu-define nil map
1674 "Menu for `eww-history-mode-map'."
1675 '("Eww History"
1676 ["Exit" quit-window t]
1677 ["Browse" eww-history-browse
1678 :active (get-text-property (line-beginning-position) 'eww-history)]))
1679 map))
1680
1681 (define-derived-mode eww-history-mode nil "eww history"
1682 "Mode for listing eww-histories.
1683
1684 \\{eww-history-mode-map}"
1685 (buffer-disable-undo)
1686 (setq buffer-read-only t
1687 truncate-lines t))
1688
1689 ;;; eww buffers list
1690
1691 (defun eww-list-buffers ()
1692 "Enlist eww buffers."
1693 (interactive)
1694 (let (buffers-info
1695 (current (current-buffer)))
1696 (dolist (buffer (buffer-list))
1697 (with-current-buffer buffer
1698 (when (derived-mode-p 'eww-mode)
1699 (push (vector buffer (plist-get eww-data :title)
1700 (plist-get eww-data :url))
1701 buffers-info))))
1702 (unless buffers-info
1703 (error "No eww buffers"))
1704 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1705 (set-buffer (get-buffer-create "*eww buffers*"))
1706 (eww-buffers-mode)
1707 (let ((inhibit-read-only t)
1708 (domain-length 0)
1709 (title-length 0)
1710 url title format start)
1711 (erase-buffer)
1712 (dolist (buffer-info buffers-info)
1713 (setq title-length (max title-length
1714 (length (elt buffer-info 1)))
1715 domain-length (max domain-length
1716 (length (elt buffer-info 2)))))
1717 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1718 header-line-format
1719 (concat " " (format format "Title" "URL")))
1720 (let ((line 0)
1721 (current-buffer-line 1))
1722 (dolist (buffer-info buffers-info)
1723 (setq start (point)
1724 title (elt buffer-info 1)
1725 url (elt buffer-info 2)
1726 line (1+ line))
1727 (insert (format format title url))
1728 (insert "\n")
1729 (let ((buffer (elt buffer-info 0)))
1730 (put-text-property start (1+ start) 'eww-buffer
1731 buffer)
1732 (when (eq current buffer)
1733 (setq current-buffer-line line))))
1734 (goto-char (point-min))
1735 (forward-line (1- current-buffer-line)))))
1736 (pop-to-buffer "*eww buffers*"))
1737
1738 (defun eww-buffer-select ()
1739 "Switch to eww buffer."
1740 (interactive)
1741 (let ((buffer (get-text-property (line-beginning-position)
1742 'eww-buffer)))
1743 (unless buffer
1744 (error "No buffer on current line"))
1745 (quit-window)
1746 (switch-to-buffer buffer)))
1747
1748 (defun eww-buffer-show ()
1749 "Display buffer under point in eww buffer list."
1750 (let ((buffer (get-text-property (line-beginning-position)
1751 'eww-buffer)))
1752 (unless buffer
1753 (error "No buffer on current line"))
1754 (other-window -1)
1755 (switch-to-buffer buffer)
1756 (other-window 1)))
1757
1758 (defun eww-buffer-show-next ()
1759 "Move to next eww buffer in the list and display it."
1760 (interactive)
1761 (forward-line)
1762 (when (eobp)
1763 (goto-char (point-min)))
1764 (eww-buffer-show))
1765
1766 (defun eww-buffer-show-previous ()
1767 "Move to previous eww buffer in the list and display it."
1768 (interactive)
1769 (beginning-of-line)
1770 (when (bobp)
1771 (goto-char (point-max)))
1772 (forward-line -1)
1773 (eww-buffer-show))
1774
1775 (defun eww-buffer-kill ()
1776 "Kill buffer from eww list."
1777 (interactive)
1778 (let* ((start (line-beginning-position))
1779 (buffer (get-text-property start 'eww-buffer))
1780 (inhibit-read-only t))
1781 (unless buffer
1782 (user-error "No buffer on the current line"))
1783 (kill-buffer buffer)
1784 (forward-line 1)
1785 (delete-region start (point)))
1786 (when (eobp)
1787 (forward-line -1))
1788 (eww-buffer-show))
1789
1790 (defvar eww-buffers-mode-map
1791 (let ((map (make-sparse-keymap)))
1792 (suppress-keymap map)
1793 (define-key map "q" 'quit-window)
1794 (define-key map [(control k)] 'eww-buffer-kill)
1795 (define-key map "\r" 'eww-buffer-select)
1796 (define-key map "n" 'eww-buffer-show-next)
1797 (define-key map "p" 'eww-buffer-show-previous)
1798
1799 (easy-menu-define nil map
1800 "Menu for `eww-buffers-mode-map'."
1801 '("Eww Buffers"
1802 ["Exit" quit-window t]
1803 ["Select" eww-buffer-select
1804 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1805 ["Kill" eww-buffer-kill
1806 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1807 map))
1808
1809 (define-derived-mode eww-buffers-mode nil "eww buffers"
1810 "Mode for listing buffers.
1811
1812 \\{eww-buffers-mode-map}"
1813 (buffer-disable-undo)
1814 (setq buffer-read-only t
1815 truncate-lines t))
1816
1817 ;;; Desktop support
1818
1819 (defvar eww-desktop-data-save
1820 '(:url :title :point)
1821 "List of `eww-data' properties to preserve in the desktop file.
1822 Also used when saving `eww-history'.")
1823
1824 (defun eww-desktop-data-1 (alist)
1825 (let ((acc nil)
1826 (tail alist))
1827 (while tail
1828 (let ((k (car tail))
1829 (v (cadr tail)))
1830 (when (memq k eww-desktop-data-save)
1831 (setq acc (cons k (cons v acc)))))
1832 (setq tail (cddr tail)))
1833 acc))
1834
1835 (defun eww-desktop-history-duplicate (a b)
1836 (let ((tail a) (r t))
1837 (while tail
1838 (if (or (memq (car tail) '(:point)) ; ignore :point
1839 (equal (cadr tail)
1840 (plist-get b (car tail))))
1841 (setq tail (cddr tail))
1842 (setq tail nil
1843 r nil)))
1844 ;; .
1845 r))
1846
1847 (defun eww-desktop-misc-data (_directory)
1848 "Return a property list with data used to restore eww buffers.
1849 This list will contain, as :history, the list, whose first element is
1850 the value of `eww-data', and the tail is `eww-history'.
1851
1852 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1853 entries (if any) will be removed from the list.
1854
1855 Only the properties listed in `eww-desktop-data-save' are included.
1856 Generally, the list should not include the (usually overly large)
1857 :dom, :source and :text properties."
1858 (let ((history (mapcar 'eww-desktop-data-1
1859 (cons eww-data eww-history))))
1860 (list :history (if eww-desktop-remove-duplicates
1861 (cl-remove-duplicates
1862 history :test 'eww-desktop-history-duplicate)
1863 history))))
1864
1865 (defun eww-restore-desktop (file-name buffer-name misc-data)
1866 "Restore an eww buffer from its desktop file record.
1867 If `eww-restore-desktop' is t or 'auto, this function will also
1868 initiate the retrieval of the respective URI in the background.
1869 Otherwise, the restored buffer will contain a prompt to do so by using
1870 \\[eww-reload]."
1871 (with-current-buffer (get-buffer-create buffer-name)
1872 (eww-mode)
1873 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1874 (setq eww-history (cdr (plist-get misc-data :history))
1875 eww-data (or (car (plist-get misc-data :history))
1876 ;; backwards compatibility
1877 (list :url (plist-get misc-data :uri))))
1878 (unless file-name
1879 (when (plist-get eww-data :url)
1880 (case eww-restore-desktop
1881 ((t auto) (eww (plist-get eww-data :url)))
1882 ((zerop (buffer-size))
1883 (let ((inhibit-read-only t))
1884 (insert (substitute-command-keys
1885 eww-restore-reload-prompt)))))))
1886 ;; .
1887 (current-buffer)))
1888
1889 (add-to-list 'desktop-locals-to-save
1890 'eww-history-position)
1891 (add-to-list 'desktop-buffer-mode-handlers
1892 '(eww-mode . eww-restore-desktop))
1893
1894 ;;; Isearch support
1895
1896 (defun eww-isearch-next-buffer (&optional _buffer wrap)
1897 "Go to the next page to search using `rel' attribute for navigation."
1898 (if wrap
1899 (condition-case nil
1900 (eww-top-url)
1901 (error nil))
1902 (if isearch-forward
1903 (eww-next-url)
1904 (eww-previous-url)))
1905 (current-buffer))
1906
1907 (provide 'eww)
1908
1909 ;;; eww.el ends here