]> code.delx.au - gnu-emacs/blob - lisp/net/eww.el
Allow overriding shr functions from eww
[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 (if (not offset) (setq offset 0))
1185 (let ((properties (text-properties-at (point)))
1186 (start (+ (eww-beginning-of-field) offset))
1187 (current-end (1+ (eww-end-of-field)))
1188 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1189 (delete-region start current-end)
1190 (forward-char offset)
1191 (insert string
1192 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1193 (if (= 0 offset) (set-text-properties start new-end properties))
1194 start))
1195
1196 (defun eww-toggle-checkbox ()
1197 "Toggle the value of the checkbox under point."
1198 (interactive)
1199 (let* ((input (get-text-property (point) 'eww-form))
1200 (type (plist-get input :type)))
1201 (if (equal type "checkbox")
1202 (goto-char
1203 (1+
1204 (if (plist-get input :checked)
1205 (progn
1206 (plist-put input :checked nil)
1207 (eww-update-field eww-form-checkbox-symbol))
1208 (plist-put input :checked t)
1209 (eww-update-field eww-form-checkbox-selected-symbol))))
1210 ;; Radio button. Switch all other buttons off.
1211 (let ((name (plist-get input :name)))
1212 (save-excursion
1213 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1214 (when (equal (plist-get (cdr elem) :name) name)
1215 (goto-char (car elem))
1216 (if (not (eq (cdr elem) input))
1217 (progn
1218 (plist-put input :checked nil)
1219 (eww-update-field eww-form-checkbox-symbol))
1220 (plist-put input :checked t)
1221 (eww-update-field eww-form-checkbox-selected-symbol)))))
1222 (forward-char 1)))))
1223
1224 (defun eww-inputs (form)
1225 (let ((start (point-min))
1226 (inputs nil))
1227 (while (and start
1228 (< start (point-max)))
1229 (when (or (get-text-property start 'eww-form)
1230 (setq start (next-single-property-change start 'eww-form)))
1231 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1232 form)
1233 (push (cons start (get-text-property start 'eww-form))
1234 inputs))
1235 (setq start (next-single-property-change start 'eww-form))))
1236 (nreverse inputs)))
1237
1238 (defun eww-size-text-inputs ()
1239 (let ((start (point-min)))
1240 (while (and start
1241 (< start (point-max)))
1242 (when (or (get-text-property start 'eww-form)
1243 (setq start (next-single-property-change start 'eww-form)))
1244 (let ((props (get-text-property start 'eww-form)))
1245 (plist-put props :start start)
1246 (setq start (next-single-property-change
1247 start 'eww-form nil (point-max)))
1248 (plist-put props :end start))))))
1249
1250 (defun eww-input-value (input)
1251 (let ((type (plist-get input :type))
1252 (value (plist-get input :value)))
1253 (cond
1254 ((equal type "textarea")
1255 (with-temp-buffer
1256 (insert value)
1257 (goto-char (point-min))
1258 (while (re-search-forward "^ +\n\\| +$" nil t)
1259 (replace-match "" t t))
1260 (buffer-string)))
1261 (t
1262 (if (string-match " +\\'" value)
1263 (substring value 0 (match-beginning 0))
1264 value)))))
1265
1266 (defun eww-submit ()
1267 "Submit the current form."
1268 (interactive)
1269 (let* ((this-input (get-text-property (point) 'eww-form))
1270 (form (plist-get this-input :eww-form))
1271 values next-submit)
1272 (dolist (elem (sort (eww-inputs form)
1273 (lambda (o1 o2)
1274 (< (car o1) (car o2)))))
1275 (let* ((input (cdr elem))
1276 (input-start (car elem))
1277 (name (plist-get input :name)))
1278 (when name
1279 (cond
1280 ((member (plist-get input :type) '("checkbox" "radio"))
1281 (when (plist-get input :checked)
1282 (push (cons name (plist-get input :value))
1283 values)))
1284 ((equal (plist-get input :type) "file")
1285 (push (cons "file"
1286 (list (cons "filedata"
1287 (with-temp-buffer
1288 (insert-file-contents
1289 (plist-get input :filename))
1290 (buffer-string)))
1291 (cons "name" (plist-get input :name))
1292 (cons "filename" (plist-get input :filename))))
1293 values))
1294 ((equal (plist-get input :type) "submit")
1295 ;; We want the values from buttons if we hit a button if
1296 ;; we hit enter on it, or if it's the first button after
1297 ;; the field we did hit return on.
1298 (when (or (eq input this-input)
1299 (and (not (eq input this-input))
1300 (null next-submit)
1301 (> input-start (point))))
1302 (setq next-submit t)
1303 (push (cons name (plist-get input :value))
1304 values)))
1305 (t
1306 (push (cons name (eww-input-value input))
1307 values))))))
1308 (dolist (elem form)
1309 (when (and (consp elem)
1310 (eq (car elem) 'hidden))
1311 (push (cons (plist-get (cdr elem) :name)
1312 (or (plist-get (cdr elem) :value) ""))
1313 values)))
1314 (if (and (stringp (cdr (assq :method form)))
1315 (equal (downcase (cdr (assq :method form))) "post"))
1316 (let ((mtype))
1317 (dolist (x values mtype)
1318 (if (equal (car x) "file")
1319 (progn
1320 (setq mtype "multipart/form-data"))))
1321 (cond ((equal mtype "multipart/form-data")
1322 (let ((boundary (mml-compute-boundary '())))
1323 (let ((url-request-method "POST")
1324 (url-request-extra-headers
1325 (list (cons "Content-Type"
1326 (concat "multipart/form-data; boundary="
1327 boundary))))
1328 (url-request-data
1329 (mm-url-encode-multipart-form-data values boundary)))
1330 (eww-browse-url (shr-expand-url
1331 (cdr (assq :action form))
1332 (plist-get eww-data :url))))))
1333 (t
1334 (let ((url-request-method "POST")
1335 (url-request-extra-headers
1336 '(("Content-Type" .
1337 "application/x-www-form-urlencoded")))
1338 (url-request-data
1339 (mm-url-encode-www-form-urlencoded values)))
1340 (eww-browse-url (shr-expand-url
1341 (cdr (assq :action form))
1342 (plist-get eww-data :url)))))))
1343 (eww-browse-url
1344 (concat
1345 (if (cdr (assq :action form))
1346 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1347 (plist-get eww-data :url))
1348 "?"
1349 (mm-url-encode-www-form-urlencoded values))))))
1350
1351 (defun eww-browse-with-external-browser (&optional url)
1352 "Browse the current URL with an external browser.
1353 The browser to used is specified by the `shr-external-browser' variable."
1354 (interactive)
1355 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1356
1357 (defun eww-follow-link (&optional external mouse-event)
1358 "Browse the URL under point.
1359 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1360 If EXTERNAL is double prefix, browse in new buffer."
1361 (interactive (list current-prefix-arg last-nonmenu-event))
1362 (mouse-set-point mouse-event)
1363 (let ((url (get-text-property (point) 'shr-url)))
1364 (cond
1365 ((not url)
1366 (message "No link under point"))
1367 ((string-match "^mailto:" url)
1368 (browse-url-mail url))
1369 ((and (consp external) (<= (car external) 4))
1370 (funcall shr-external-browser url))
1371 ;; This is a #target url in the same page as the current one.
1372 ((and (url-target (url-generic-parse-url url))
1373 (eww-same-page-p url (plist-get eww-data :url)))
1374 (let ((dom (plist-get eww-data :dom)))
1375 (eww-save-history)
1376 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1377 (t
1378 (eww-browse-url url external)))))
1379
1380 (defun eww-same-page-p (url1 url2)
1381 "Return non-nil if URL1 and URL2 represent the same page.
1382 Differences in #targets are ignored."
1383 (let ((obj1 (url-generic-parse-url url1))
1384 (obj2 (url-generic-parse-url url2)))
1385 (setf (url-target obj1) nil)
1386 (setf (url-target obj2) nil)
1387 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1388
1389 (defun eww-copy-page-url ()
1390 "Copy the URL of the current page into the kill ring."
1391 (interactive)
1392 (message "%s" (plist-get eww-data :url))
1393 (kill-new (plist-get eww-data :url)))
1394
1395 (defun eww-download ()
1396 "Download URL under point to `eww-download-directory'."
1397 (interactive)
1398 (let ((url (get-text-property (point) 'shr-url)))
1399 (if (not url)
1400 (message "No URL under point")
1401 (url-retrieve url 'eww-download-callback (list url)))))
1402
1403 (defun eww-download-callback (status url)
1404 (unless (plist-get status :error)
1405 (let* ((obj (url-generic-parse-url url))
1406 (path (car (url-path-and-query obj)))
1407 (file (eww-make-unique-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-make-unique-file-name (file directory)
1415 (cond
1416 ((zerop (length file))
1417 (setq file "!"))
1418 ((string-match "\\`[.]" file)
1419 (setq file (concat "!" file))))
1420 (let ((count 1))
1421 (while (file-exists-p (expand-file-name file directory))
1422 (setq file
1423 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1424 (format "%s(%d)%s" (match-string 1 file)
1425 count (match-string 2 file))
1426 (format "%s(%d)" file count)))
1427 (setq count (1+ count)))
1428 (expand-file-name file directory)))
1429
1430 (defun eww-set-character-encoding (charset)
1431 "Set character encoding to CHARSET.
1432 If CHARSET is nil then use UTF-8."
1433 (interactive "zUse character set (default utf-8): ")
1434 (if (null charset)
1435 (eww-reload nil 'utf-8)
1436 (eww-reload nil charset)))
1437
1438 (defun eww-toggle-fonts ()
1439 "Toggle whether to use monospaced or font-enabled layouts."
1440 (interactive)
1441 (message "Fonts are now %s"
1442 (if (setq shr-use-fonts (not shr-use-fonts))
1443 "on"
1444 "off"))
1445 (eww-reload))
1446
1447 ;;; Bookmarks code
1448
1449 (defvar eww-bookmarks nil)
1450
1451 (defun eww-add-bookmark ()
1452 "Bookmark the current page."
1453 (interactive)
1454 (eww-read-bookmarks)
1455 (dolist (bookmark eww-bookmarks)
1456 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1457 (user-error "Already bookmarked")))
1458 (when (y-or-n-p "Bookmark this page?")
1459 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1460 (plist-get eww-data :title))))
1461 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1462 (push (list :url (plist-get eww-data :url)
1463 :title title
1464 :time (current-time-string))
1465 eww-bookmarks))
1466 (eww-write-bookmarks)
1467 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1468 (plist-get eww-data :title))))
1469
1470 (defun eww-write-bookmarks ()
1471 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1472 (insert ";; Auto-generated file; don't edit\n")
1473 (pp eww-bookmarks (current-buffer))))
1474
1475 (defun eww-read-bookmarks ()
1476 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1477 (setq eww-bookmarks
1478 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1479 (with-temp-buffer
1480 (insert-file-contents file)
1481 (read (current-buffer)))))))
1482
1483 ;;;###autoload
1484 (defun eww-list-bookmarks ()
1485 "Display the bookmarks."
1486 (interactive)
1487 (eww-bookmark-prepare)
1488 (pop-to-buffer "*eww bookmarks*"))
1489
1490 (defun eww-bookmark-prepare ()
1491 (eww-read-bookmarks)
1492 (unless eww-bookmarks
1493 (user-error "No bookmarks are defined"))
1494 (set-buffer (get-buffer-create "*eww bookmarks*"))
1495 (eww-bookmark-mode)
1496 (let* ((width (/ (window-width) 2))
1497 (format (format "%%-%ds %%s" width))
1498 (inhibit-read-only t)
1499 start title)
1500 (erase-buffer)
1501 (setq header-line-format (concat " " (format format "Title" "URL")))
1502 (dolist (bookmark eww-bookmarks)
1503 (setq start (point)
1504 title (plist-get bookmark :title))
1505 (when (> (length title) width)
1506 (setq title (truncate-string-to-width title width)))
1507 (insert (format format title (plist-get bookmark :url)) "\n")
1508 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1509 (goto-char (point-min))))
1510
1511 (defvar eww-bookmark-kill-ring nil)
1512
1513 (defun eww-bookmark-kill ()
1514 "Kill the current bookmark."
1515 (interactive)
1516 (let* ((start (line-beginning-position))
1517 (bookmark (get-text-property start 'eww-bookmark))
1518 (inhibit-read-only t))
1519 (unless bookmark
1520 (user-error "No bookmark on the current line"))
1521 (forward-line 1)
1522 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1523 (delete-region start (point))
1524 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1525 (eww-write-bookmarks)))
1526
1527 (defun eww-bookmark-yank ()
1528 "Yank a previously killed bookmark to the current line."
1529 (interactive)
1530 (unless eww-bookmark-kill-ring
1531 (user-error "No previously killed bookmark"))
1532 (beginning-of-line)
1533 (let ((inhibit-read-only t)
1534 (start (point))
1535 bookmark)
1536 (insert (pop eww-bookmark-kill-ring))
1537 (setq bookmark (get-text-property start 'eww-bookmark))
1538 (if (= start (point-min))
1539 (push bookmark eww-bookmarks)
1540 (let ((line (count-lines start (point))))
1541 (setcdr (nthcdr (1- line) eww-bookmarks)
1542 (cons bookmark (nthcdr line eww-bookmarks)))))
1543 (eww-write-bookmarks)))
1544
1545 (defun eww-bookmark-browse ()
1546 "Browse the bookmark under point in eww."
1547 (interactive)
1548 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1549 (unless bookmark
1550 (user-error "No bookmark on the current line"))
1551 (quit-window)
1552 (eww-browse-url (plist-get bookmark :url))))
1553
1554 (defun eww-next-bookmark ()
1555 "Go to the next bookmark in the list."
1556 (interactive)
1557 (let ((first nil)
1558 bookmark)
1559 (unless (get-buffer "*eww bookmarks*")
1560 (setq first t)
1561 (eww-bookmark-prepare))
1562 (with-current-buffer (get-buffer "*eww bookmarks*")
1563 (when (and (not first)
1564 (not (eobp)))
1565 (forward-line 1))
1566 (setq bookmark (get-text-property (line-beginning-position)
1567 'eww-bookmark))
1568 (unless bookmark
1569 (user-error "No next bookmark")))
1570 (eww-browse-url (plist-get bookmark :url))))
1571
1572 (defun eww-previous-bookmark ()
1573 "Go to the previous bookmark in the list."
1574 (interactive)
1575 (let ((first nil)
1576 bookmark)
1577 (unless (get-buffer "*eww bookmarks*")
1578 (setq first t)
1579 (eww-bookmark-prepare))
1580 (with-current-buffer (get-buffer "*eww bookmarks*")
1581 (if first
1582 (goto-char (point-max))
1583 (beginning-of-line))
1584 ;; On the final line.
1585 (when (eolp)
1586 (forward-line -1))
1587 (if (bobp)
1588 (user-error "No previous bookmark")
1589 (forward-line -1))
1590 (setq bookmark (get-text-property (line-beginning-position)
1591 'eww-bookmark)))
1592 (eww-browse-url (plist-get bookmark :url))))
1593
1594 (defvar eww-bookmark-mode-map
1595 (let ((map (make-sparse-keymap)))
1596 (define-key map [(control k)] 'eww-bookmark-kill)
1597 (define-key map [(control y)] 'eww-bookmark-yank)
1598 (define-key map "\r" 'eww-bookmark-browse)
1599
1600 (easy-menu-define nil map
1601 "Menu for `eww-bookmark-mode-map'."
1602 '("Eww Bookmark"
1603 ["Exit" quit-window t]
1604 ["Browse" eww-bookmark-browse
1605 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1606 ["Kill" eww-bookmark-kill
1607 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1608 ["Yank" eww-bookmark-yank
1609 :active eww-bookmark-kill-ring]))
1610 map))
1611
1612 (define-derived-mode eww-bookmark-mode special-mode "eww bookmarks"
1613 "Mode for listing bookmarks.
1614
1615 \\{eww-bookmark-mode-map}"
1616 (buffer-disable-undo)
1617 (setq truncate-lines t))
1618
1619 ;;; History code
1620
1621 (defun eww-save-history ()
1622 (plist-put eww-data :point (point))
1623 (plist-put eww-data :text (buffer-string))
1624 (push eww-data eww-history)
1625 (setq eww-data (list :title ""))
1626 ;; Don't let the history grow infinitely. We store quite a lot of
1627 ;; data per page.
1628 (when-let (tail (and eww-history-limit
1629 (nthcdr eww-history-limit eww-history)))
1630 (setcdr tail nil)))
1631
1632 (defvar eww-current-buffer)
1633
1634 (defun eww-list-histories ()
1635 "List the eww-histories."
1636 (interactive)
1637 (when (null eww-history)
1638 (error "No eww-histories are defined"))
1639 (let ((eww-history-trans eww-history)
1640 (buffer (current-buffer)))
1641 (set-buffer (get-buffer-create "*eww history*"))
1642 (eww-history-mode)
1643 (setq-local eww-current-buffer buffer)
1644 (let ((inhibit-read-only t)
1645 (domain-length 0)
1646 (title-length 0)
1647 url title format start)
1648 (erase-buffer)
1649 (dolist (history eww-history-trans)
1650 (setq start (point))
1651 (setq domain-length (max domain-length (length (plist-get history :url))))
1652 (setq title-length (max title-length (length (plist-get history :title)))))
1653 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1654 header-line-format
1655 (concat " " (format format "Title" "URL")))
1656 (dolist (history eww-history-trans)
1657 (setq start (point))
1658 (setq url (plist-get history :url))
1659 (setq title (plist-get history :title))
1660 (insert (format format title url))
1661 (insert "\n")
1662 (put-text-property start (1+ start) 'eww-history history))
1663 (goto-char (point-min)))
1664 (pop-to-buffer "*eww history*")))
1665
1666 (defun eww-history-browse ()
1667 "Browse the history under point in eww."
1668 (interactive)
1669 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1670 (unless history
1671 (error "No history on the current line"))
1672 (let ((buffer eww-current-buffer))
1673 (quit-window)
1674 (when buffer
1675 (switch-to-buffer buffer)))
1676 (eww-restore-history history)))
1677
1678 (defvar eww-history-mode-map
1679 (let ((map (make-sparse-keymap)))
1680 (define-key map "\r" 'eww-history-browse)
1681 ;; (define-key map "n" 'next-error-no-select)
1682 ;; (define-key map "p" 'previous-error-no-select)
1683
1684 (easy-menu-define nil map
1685 "Menu for `eww-history-mode-map'."
1686 '("Eww History"
1687 ["Exit" quit-window t]
1688 ["Browse" eww-history-browse
1689 :active (get-text-property (line-beginning-position) 'eww-history)]))
1690 map))
1691
1692 (define-derived-mode eww-history-mode special-mode "eww history"
1693 "Mode for listing eww-histories.
1694
1695 \\{eww-history-mode-map}"
1696 (buffer-disable-undo)
1697 (setq truncate-lines t))
1698
1699 ;;; eww buffers list
1700
1701 (defun eww-list-buffers ()
1702 "Enlist eww buffers."
1703 (interactive)
1704 (let (buffers-info
1705 (current (current-buffer)))
1706 (dolist (buffer (buffer-list))
1707 (with-current-buffer buffer
1708 (when (derived-mode-p 'eww-mode)
1709 (push (vector buffer (plist-get eww-data :title)
1710 (plist-get eww-data :url))
1711 buffers-info))))
1712 (unless buffers-info
1713 (error "No eww buffers"))
1714 (setq buffers-info (nreverse buffers-info)) ;more recent on top
1715 (set-buffer (get-buffer-create "*eww buffers*"))
1716 (eww-buffers-mode)
1717 (let ((inhibit-read-only t)
1718 (domain-length 0)
1719 (title-length 0)
1720 url title format start)
1721 (erase-buffer)
1722 (dolist (buffer-info buffers-info)
1723 (setq title-length (max title-length
1724 (length (elt buffer-info 1)))
1725 domain-length (max domain-length
1726 (length (elt buffer-info 2)))))
1727 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1728 header-line-format
1729 (concat " " (format format "Title" "URL")))
1730 (let ((line 0)
1731 (current-buffer-line 1))
1732 (dolist (buffer-info buffers-info)
1733 (setq start (point)
1734 title (elt buffer-info 1)
1735 url (elt buffer-info 2)
1736 line (1+ line))
1737 (insert (format format title url))
1738 (insert "\n")
1739 (let ((buffer (elt buffer-info 0)))
1740 (put-text-property start (1+ start) 'eww-buffer
1741 buffer)
1742 (when (eq current buffer)
1743 (setq current-buffer-line line))))
1744 (goto-char (point-min))
1745 (forward-line (1- current-buffer-line)))))
1746 (pop-to-buffer "*eww buffers*"))
1747
1748 (defun eww-buffer-select ()
1749 "Switch to eww buffer."
1750 (interactive)
1751 (let ((buffer (get-text-property (line-beginning-position)
1752 'eww-buffer)))
1753 (unless buffer
1754 (error "No buffer on current line"))
1755 (quit-window)
1756 (switch-to-buffer buffer)))
1757
1758 (defun eww-buffer-show ()
1759 "Display buffer under point in eww buffer list."
1760 (let ((buffer (get-text-property (line-beginning-position)
1761 'eww-buffer)))
1762 (unless buffer
1763 (error "No buffer on current line"))
1764 (other-window -1)
1765 (switch-to-buffer buffer)
1766 (other-window 1)))
1767
1768 (defun eww-buffer-show-next ()
1769 "Move to next eww buffer in the list and display it."
1770 (interactive)
1771 (forward-line)
1772 (when (eobp)
1773 (goto-char (point-min)))
1774 (eww-buffer-show))
1775
1776 (defun eww-buffer-show-previous ()
1777 "Move to previous eww buffer in the list and display it."
1778 (interactive)
1779 (beginning-of-line)
1780 (when (bobp)
1781 (goto-char (point-max)))
1782 (forward-line -1)
1783 (eww-buffer-show))
1784
1785 (defun eww-buffer-kill ()
1786 "Kill buffer from eww list."
1787 (interactive)
1788 (let* ((start (line-beginning-position))
1789 (buffer (get-text-property start 'eww-buffer))
1790 (inhibit-read-only t))
1791 (unless buffer
1792 (user-error "No buffer on the current line"))
1793 (kill-buffer buffer)
1794 (forward-line 1)
1795 (delete-region start (point)))
1796 (when (eobp)
1797 (forward-line -1))
1798 (eww-buffer-show))
1799
1800 (defvar eww-buffers-mode-map
1801 (let ((map (make-sparse-keymap)))
1802 (define-key map [(control k)] 'eww-buffer-kill)
1803 (define-key map "\r" 'eww-buffer-select)
1804 (define-key map "n" 'eww-buffer-show-next)
1805 (define-key map "p" 'eww-buffer-show-previous)
1806
1807 (easy-menu-define nil map
1808 "Menu for `eww-buffers-mode-map'."
1809 '("Eww Buffers"
1810 ["Exit" quit-window t]
1811 ["Select" eww-buffer-select
1812 :active (get-text-property (line-beginning-position) 'eww-buffer)]
1813 ["Kill" eww-buffer-kill
1814 :active (get-text-property (line-beginning-position) 'eww-buffer)]))
1815 map))
1816
1817 (define-derived-mode eww-buffers-mode special-mode "eww buffers"
1818 "Mode for listing buffers.
1819
1820 \\{eww-buffers-mode-map}"
1821 (buffer-disable-undo)
1822 (setq truncate-lines t))
1823
1824 ;;; Desktop support
1825
1826 (defvar eww-desktop-data-save
1827 '(:url :title :point)
1828 "List of `eww-data' properties to preserve in the desktop file.
1829 Also used when saving `eww-history'.")
1830
1831 (defun eww-desktop-data-1 (alist)
1832 (let ((acc nil)
1833 (tail alist))
1834 (while tail
1835 (let ((k (car tail))
1836 (v (cadr tail)))
1837 (when (memq k eww-desktop-data-save)
1838 (setq acc (cons k (cons v acc)))))
1839 (setq tail (cddr tail)))
1840 acc))
1841
1842 (defun eww-desktop-history-duplicate (a b)
1843 (let ((tail a) (r t))
1844 (while tail
1845 (if (or (memq (car tail) '(:point)) ; ignore :point
1846 (equal (cadr tail)
1847 (plist-get b (car tail))))
1848 (setq tail (cddr tail))
1849 (setq tail nil
1850 r nil)))
1851 ;; .
1852 r))
1853
1854 (defun eww-desktop-misc-data (_directory)
1855 "Return a property list with data used to restore eww buffers.
1856 This list will contain, as :history, the list, whose first element is
1857 the value of `eww-data', and the tail is `eww-history'.
1858
1859 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1860 entries (if any) will be removed from the list.
1861
1862 Only the properties listed in `eww-desktop-data-save' are included.
1863 Generally, the list should not include the (usually overly large)
1864 :dom, :source and :text properties."
1865 (let ((history (mapcar 'eww-desktop-data-1
1866 (cons eww-data eww-history))))
1867 (list :history (if eww-desktop-remove-duplicates
1868 (cl-remove-duplicates
1869 history :test 'eww-desktop-history-duplicate)
1870 history))))
1871
1872 (defun eww-restore-desktop (file-name buffer-name misc-data)
1873 "Restore an eww buffer from its desktop file record.
1874 If `eww-restore-desktop' is t or 'auto, this function will also
1875 initiate the retrieval of the respective URI in the background.
1876 Otherwise, the restored buffer will contain a prompt to do so by using
1877 \\[eww-reload]."
1878 (with-current-buffer (get-buffer-create buffer-name)
1879 (eww-mode)
1880 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1881 (setq eww-history (cdr (plist-get misc-data :history))
1882 eww-data (or (car (plist-get misc-data :history))
1883 ;; backwards compatibility
1884 (list :url (plist-get misc-data :uri))))
1885 (unless file-name
1886 (when (plist-get eww-data :url)
1887 (case eww-restore-desktop
1888 ((t auto) (eww (plist-get eww-data :url)))
1889 ((zerop (buffer-size))
1890 (let ((inhibit-read-only t))
1891 (insert (substitute-command-keys
1892 eww-restore-reload-prompt)))))))
1893 ;; .
1894 (current-buffer)))
1895
1896 (add-to-list 'desktop-locals-to-save
1897 'eww-history-position)
1898 (add-to-list 'desktop-buffer-mode-handlers
1899 '(eww-mode . eww-restore-desktop))
1900
1901 ;;; Isearch support
1902
1903 (defun eww-isearch-next-buffer (&optional _buffer wrap)
1904 "Go to the next page to search using `rel' attribute for navigation."
1905 (if wrap
1906 (condition-case nil
1907 (eww-top-url)
1908 (error nil))
1909 (if isearch-forward
1910 (eww-next-url)
1911 (eww-previous-url)))
1912 (current-buffer))
1913
1914 (provide 'eww)
1915
1916 ;;; eww.el ends here