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