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