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