Revert prefix behavior in RET to previous behavior
[emacs.git] / lisp / net / eww.el
blob139c0b1372d21f1bbc4f8c51f566ec42aa9ba500
1 ;;; eww.el --- Emacs Web Wowser
3 ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;;; Code:
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
36 (defgroup eww nil
37 "Emacs Web Wowser"
38 :version "25.1"
39 :link '(custom-manual "(eww) Top")
40 :group 'web
41 :prefix "eww-")
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)
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)
57 (defcustom eww-download-directory "~/Downloads/"
58 "Directory where files will downloaded."
59 :version "24.4"
60 :group 'eww
61 :type 'string)
63 (defcustom eww-suggest-uris
64 '(eww-links-at-point
65 url-get-url-at-point
66 eww-current-url)
67 "List of functions called to form the list of default URIs for `eww'.
68 Each of the elements is a function returning either a string or a list
69 of strings. The results will be joined into a single list with
70 duplicate entries (if any) removed."
71 :version "25.1"
72 :group 'eww
73 :type 'hook
74 :options '(eww-links-at-point
75 url-get-url-at-point
76 eww-current-url))
78 (defcustom eww-bookmarks-directory user-emacs-directory
79 "Directory where bookmark files will be stored."
80 :version "25.1"
81 :group 'eww
82 :type 'string)
84 (defcustom eww-desktop-remove-duplicates t
85 "Whether to remove duplicates from the history when saving desktop data.
86 If non-nil, repetitive EWW history entries (comprising of the URI, the
87 title, and the point position) will not be saved as part of the Emacs
88 desktop. Otherwise, such entries will be retained."
89 :version "25.1"
90 :group 'eww
91 :type 'boolean)
93 (defcustom eww-restore-desktop nil
94 "How to restore EWW buffers on `desktop-restore'.
95 If t or 'auto, the buffers will be reloaded automatically.
96 If nil, buffers will require manual reload, and will contain the text
97 specified in `eww-restore-reload-prompt' instead of the actual Web
98 page contents."
99 :version "25.1"
100 :group 'eww
101 :type '(choice (const :tag "Restore all automatically" t)
102 (const :tag "Require manual reload" nil)))
104 (defcustom eww-restore-reload-prompt
105 "\n\n *** Use \\[eww-reload] to reload this buffer. ***\n"
106 "The string to put in the buffers not reloaded on `desktop-restore'.
107 This prompt will be used if `eww-restore-desktop' is nil.
109 The string will be passed through `substitute-command-keys'."
110 :version "25.1"
111 :group 'eww
112 :type 'string)
114 (defcustom eww-history-limit 50
115 "Maximum number of entries to retain in the history."
116 :version "25.1"
117 :group 'eww
118 :type '(choice (const :tag "Unlimited" nil)
119 integer))
121 (defcustom eww-use-external-browser-for-content-type
122 "\\`\\(video/\\|audio/\\|application/ogg\\)"
123 "Always use external browser for specified content-type."
124 :version "24.4"
125 :group 'eww
126 :type '(choice (const :tag "Never" nil)
127 regexp))
129 (defcustom eww-after-render-hook nil
130 "A hook called after eww has finished rendering the buffer."
131 :version "25.1"
132 :group 'eww
133 :type 'hook)
135 (defcustom eww-form-checkbox-selected-symbol "[X]"
136 "Symbol used to represent a selected checkbox.
137 See also `eww-form-checkbox-symbol'."
138 :version "24.4"
139 :group 'eww
140 :type '(choice (const "[X]")
141 (const "☒") ; Unicode BALLOT BOX WITH X
142 (const "☑") ; Unicode BALLOT BOX WITH CHECK
143 string))
145 (defcustom eww-form-checkbox-symbol "[ ]"
146 "Symbol used to represent a checkbox.
147 See also `eww-form-checkbox-selected-symbol'."
148 :version "24.4"
149 :group 'eww
150 :type '(choice (const "[ ]")
151 (const "☐") ; Unicode BALLOT BOX
152 string))
154 (defface eww-form-submit
155 '((((type x w32 ns) (class color)) ; Like default mode line
156 :box (:line-width 2 :style released-button)
157 :background "#808080" :foreground "black"))
158 "Face for eww buffer buttons."
159 :version "24.4"
160 :group 'eww)
162 (defface eww-form-file
163 '((((type x w32 ns) (class color)) ; Like default mode line
164 :box (:line-width 2 :style released-button)
165 :background "#808080" :foreground "black"))
166 "Face for eww buffer buttons."
167 :version "25.1"
168 :group 'eww)
170 (defface eww-form-checkbox
171 '((((type x w32 ns) (class color)) ; Like default mode line
172 :box (:line-width 2 :style released-button)
173 :background "lightgrey" :foreground "black"))
174 "Face for eww buffer buttons."
175 :version "24.4"
176 :group 'eww)
178 (defface eww-form-select
179 '((((type x w32 ns) (class color)) ; Like default mode line
180 :box (:line-width 2 :style released-button)
181 :background "lightgrey" :foreground "black"))
182 "Face for eww buffer buttons."
183 :version "24.4"
184 :group 'eww)
186 (defface eww-form-text
187 '((t (:background "#505050"
188 :foreground "white"
189 :box (:line-width 1))))
190 "Face for eww text inputs."
191 :version "24.4"
192 :group 'eww)
194 (defface eww-form-textarea
195 '((t (:background "#C0C0C0"
196 :foreground "black"
197 :box (:line-width 1))))
198 "Face for eww textarea inputs."
199 :version "24.4"
200 :group 'eww)
202 (defvar eww-data nil)
203 (defvar eww-history nil)
204 (defvar eww-history-position 0)
206 (defvar eww-local-regex "localhost"
207 "When this regex is found in the URL, it's not a keyword but an address.")
209 (defvar eww-link-keymap
210 (let ((map (copy-keymap shr-map)))
211 (define-key map "\r" 'eww-follow-link)
212 map))
214 (defun eww-suggested-uris nil
215 "Return the list of URIs to suggest at the `eww' prompt.
216 This list can be customized via `eww-suggest-uris'."
217 (let ((obseen (make-vector 42 0))
218 (uris nil))
219 (dolist (fun eww-suggest-uris)
220 (let ((ret (funcall fun)))
221 (dolist (uri (if (stringp ret) (list ret) ret))
222 (when (and uri (not (intern-soft uri obseen)))
223 (intern uri obseen)
224 (push uri uris)))))
225 (nreverse uris)))
227 ;;;###autoload
228 (defun eww (url)
229 "Fetch URL and render the page.
230 If the input doesn't look like an URL or a domain name, the
231 word(s) will be searched for via `eww-search-prefix'."
232 (interactive
233 (let* ((uris (eww-suggested-uris))
234 (prompt (concat "Enter URL or keywords"
235 (if uris (format " (default %s)" (car uris)) "")
236 ": ")))
237 (list (read-string prompt nil nil uris))))
238 (setq url (string-trim url))
239 (cond ((string-match-p "\\`file:/" url))
240 ;; Don't mangle file: URLs at all.
241 ((string-match-p "\\`ftp://" url)
242 (user-error "FTP is not supported."))
244 (if (and (= (length (split-string url)) 1)
245 (or (and (not (string-match-p "\\`[\"\'].*[\"\']\\'" url))
246 (> (length (split-string url "[.:]")) 1))
247 (string-match eww-local-regex url)))
248 (progn
249 (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url)
250 (setq url (concat "http://" url)))
251 ;; some site don't redirect final /
252 (when (string= (url-filename (url-generic-parse-url url)) "")
253 (setq url (concat url "/"))))
254 (setq url (concat eww-search-prefix
255 (replace-regexp-in-string " " "+" url))))))
256 (if (eq major-mode 'eww-mode)
257 (when (or (plist-get eww-data :url)
258 (plist-get eww-data :dom))
259 (eww-save-history))
260 (eww-setup-buffer)
261 (plist-put eww-data :url url)
262 (eww-update-header-line-format)
263 (let ((inhibit-read-only t))
264 (insert (format "Loading %s..." url))
265 (goto-char (point-min))))
266 (url-retrieve url 'eww-render
267 (list url nil (current-buffer))))
269 ;;;###autoload (defalias 'browse-web 'eww)
271 ;;;###autoload
272 (defun eww-open-file (file)
273 "Render a file using EWW."
274 (interactive "fFile: ")
275 (eww (concat "file://"
276 (and (memq system-type '(windows-nt ms-dos))
277 "/")
278 (expand-file-name file))))
280 ;;;###autoload
281 (defun eww-search-words (&optional beg end)
282 "Search the web for the text between the point and marker.
283 See the `eww-search-prefix' variable for the search engine used."
284 (interactive "r")
285 (eww (buffer-substring beg end)))
287 (defun eww-render (status url &optional point buffer encode)
288 (let ((redirect (plist-get status :redirect)))
289 (when redirect
290 (setq url redirect)))
291 (let* ((headers (eww-parse-headers))
292 (content-type
293 (mail-header-parse-content-type
294 (or (cdr (assoc "content-type" headers))
295 "text/plain")))
296 (charset (intern
297 (downcase
298 (or (cdr (assq 'charset (cdr content-type)))
299 (eww-detect-charset (equal (car content-type)
300 "text/html"))
301 "utf-8"))))
302 (data-buffer (current-buffer)))
303 (unwind-protect
304 (progn
305 (cond
306 ((and eww-use-external-browser-for-content-type
307 (string-match-p eww-use-external-browser-for-content-type
308 (car content-type)))
309 (eww-browse-with-external-browser url))
310 ((equal (car content-type) "text/html")
311 (eww-display-html charset url nil point buffer encode))
312 ((equal (car content-type) "application/pdf")
313 (eww-display-pdf))
314 ((string-match-p "\\`image/" (car content-type))
315 (eww-display-image buffer))
317 (eww-display-raw buffer encode)))
318 (with-current-buffer buffer
319 (plist-put eww-data :url url)
320 (eww-update-header-line-format)
321 (setq eww-history-position 0)
322 (run-hooks 'eww-after-render-hook)))
323 (kill-buffer data-buffer))))
325 (defun eww-parse-headers ()
326 (let ((headers nil))
327 (goto-char (point-min))
328 (while (and (not (eobp))
329 (not (eolp)))
330 (when (looking-at "\\([^:]+\\): *\\(.*\\)")
331 (push (cons (downcase (match-string 1))
332 (match-string 2))
333 headers))
334 (forward-line 1))
335 (unless (eobp)
336 (forward-line 1))
337 headers))
339 (defun eww-detect-charset (html-p)
340 (let ((case-fold-search t)
341 (pt (point)))
342 (or (and html-p
343 (re-search-forward
344 "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t)
345 (goto-char pt)
346 (match-string 1))
347 (and (looking-at
348 "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)")
349 (match-string 1)))))
351 (declare-function libxml-parse-html-region "xml.c"
352 (start end &optional base-url))
354 (defun eww-display-html (charset url &optional document point buffer encode)
355 (unless (fboundp 'libxml-parse-html-region)
356 (error "This function requires Emacs to be compiled with libxml2"))
357 (unless (buffer-live-p buffer)
358 (error "Buffer %s doesn't exist" buffer))
359 ;; There should be a better way to abort loading images
360 ;; asynchronously.
361 (setq url-queue nil)
362 (let ((document
363 (or document
364 (list
365 'base (list (cons 'href url))
366 (progn
367 (when (or (and encode
368 (not (eq charset encode)))
369 (not (eq charset 'utf-8)))
370 (condition-case nil
371 (decode-coding-region (point) (point-max)
372 (or encode charset))
373 (coding-system-error nil)))
374 (libxml-parse-html-region (point) (point-max))))))
375 (source (and (null document)
376 (buffer-substring (point) (point-max)))))
377 (with-current-buffer buffer
378 (plist-put eww-data :source source)
379 (plist-put eww-data :dom document)
380 (let ((inhibit-read-only t)
381 (inhibit-modification-hooks t)
382 (shr-target-id (url-target (url-generic-parse-url url)))
383 (shr-external-rendering-functions
384 '((title . eww-tag-title)
385 (form . eww-tag-form)
386 (input . eww-tag-input)
387 (textarea . eww-tag-textarea)
388 (body . eww-tag-body)
389 (select . eww-tag-select)
390 (link . eww-tag-link)
391 (a . eww-tag-a))))
392 (erase-buffer)
393 (shr-insert-document document)
394 (cond
395 (point
396 (goto-char point))
397 (shr-target-id
398 (goto-char (point-min))
399 (let ((point (next-single-property-change
400 (point-min) 'shr-target-id)))
401 (when point
402 (goto-char point))))
404 (goto-char (point-min))
405 ;; Don't leave point inside forms, because the normal eww
406 ;; commands aren't available there.
407 (while (and (not (eobp))
408 (get-text-property (point) 'eww-form))
409 (forward-line 1)))))
410 (eww-size-text-inputs))))
412 (defun eww-handle-link (dom)
413 (let* ((rel (dom-attr dom 'rel))
414 (href (dom-attr dom 'href))
415 (where (assoc
416 ;; The text associated with :rel is case-insensitive.
417 (if rel (downcase rel))
418 '(("next" . :next)
419 ;; Texinfo uses "previous", but HTML specifies
420 ;; "prev", so recognize both.
421 ("previous" . :previous)
422 ("prev" . :previous)
423 ;; HTML specifies "start" but also "contents",
424 ;; and Gtk seems to use "home". Recognize
425 ;; them all; but store them in different
426 ;; variables so that we can readily choose the
427 ;; "best" one.
428 ("start" . :start)
429 ("home" . :home)
430 ("contents" . :contents)
431 ("up" . up)))))
432 (and href
433 where
434 (plist-put eww-data (cdr where) href))))
436 (defun eww-tag-link (dom)
437 (eww-handle-link dom)
438 (shr-generic dom))
440 (defun eww-tag-a (dom)
441 (eww-handle-link dom)
442 (let ((start (point)))
443 (shr-tag-a dom)
444 (put-text-property start (point) 'keymap eww-link-keymap)))
446 (defun eww-update-header-line-format ()
447 (if eww-header-line-format
448 (setq header-line-format
449 (replace-regexp-in-string
450 "%" "%%"
451 ;; FIXME? Title can be blank. Default to, eg, last component
452 ;; of url?
453 (format-spec eww-header-line-format
454 `((?u . ,(or (plist-get eww-data :url) ""))
455 (?t . ,(or (plist-get eww-data :title) ""))))))
456 (setq header-line-format nil)))
458 (defun eww-tag-title (dom)
459 (plist-put eww-data :title
460 (replace-regexp-in-string
461 "^ \\| $" ""
462 (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom))))
463 (eww-update-header-line-format))
465 (defun eww-tag-body (dom)
466 (let* ((start (point))
467 (fgcolor (or (dom-attr dom 'fgcolor) (dom-attr dom 'text)))
468 (bgcolor (dom-attr dom 'bgcolor))
469 (shr-stylesheet (list (cons 'color fgcolor)
470 (cons 'background-color bgcolor))))
471 (shr-generic dom)
472 (shr-colorize-region start (point) fgcolor bgcolor)))
474 (defun eww-display-raw (buffer &optional encode)
475 (let ((data (buffer-substring (point) (point-max))))
476 (unless (buffer-live-p buffer)
477 (error "Buffer %s doesn't exist" buffer))
478 (with-current-buffer buffer
479 (let ((inhibit-read-only t))
480 (erase-buffer)
481 (insert data)
482 (unless (eq encode 'utf-8)
483 (encode-coding-region (point-min) (1+ (length data)) 'utf-8)
484 (condition-case nil
485 (decode-coding-region (point-min) (1+ (length data)) encode)
486 (coding-system-error nil))))
487 (goto-char (point-min)))))
489 (defun eww-display-image (buffer)
490 (let ((data (shr-parse-image-data)))
491 (unless (buffer-live-p buffer)
492 (error "Buffer %s doesn't exist" buffer))
493 (with-current-buffer buffer
494 (let ((inhibit-read-only t))
495 (erase-buffer)
496 (shr-put-image data nil))
497 (goto-char (point-min)))))
499 (declare-function mailcap-view-mime "mailcap" (type))
500 (defun eww-display-pdf ()
501 (let ((data (buffer-substring (point) (point-max))))
502 (switch-to-buffer (get-buffer-create "*eww pdf*"))
503 (let ((coding-system-for-write 'raw-text)
504 (inhibit-read-only t))
505 (erase-buffer)
506 (insert data)
507 (mailcap-view-mime "application/pdf")))
508 (goto-char (point-min)))
510 (defun eww-setup-buffer ()
511 (switch-to-buffer (get-buffer-create "*eww*"))
512 (let ((inhibit-read-only t))
513 (remove-overlays)
514 (erase-buffer))
515 (unless (eq major-mode 'eww-mode)
516 (eww-mode)))
518 (defun eww-current-url nil
519 "Return URI of the Web page the current EWW buffer is visiting."
520 (plist-get eww-data :url))
522 (defun eww-links-at-point (&optional pt)
523 "Return list of URIs, if any, linked at point."
524 (remq nil
525 (list (get-text-property (point) 'shr-url)
526 (get-text-property (point) 'image-url))))
528 (defun eww-view-source ()
529 "View the HTML source code of the current page."
530 (interactive)
531 (let ((buf (get-buffer-create "*eww-source*"))
532 (source (plist-get eww-data :source)))
533 (with-current-buffer buf
534 (let ((inhibit-read-only t))
535 (delete-region (point-min) (point-max))
536 (insert (or source "no source"))
537 (goto-char (point-min))
538 (when (fboundp 'html-mode)
539 (html-mode))))
540 (view-buffer buf)))
542 (defun eww-readable ()
543 "View the main \"readable\" parts of the current web page.
544 This command uses heuristics to find the parts of the web page that
545 contains the main textual portion, leaving out navigation menus and
546 the like."
547 (interactive)
548 (let* ((old-data eww-data)
549 (dom (with-temp-buffer
550 (insert (plist-get old-data :source))
551 (condition-case nil
552 (decode-coding-region (point-min) (point-max) 'utf-8)
553 (coding-system-error nil))
554 (libxml-parse-html-region (point-min) (point-max)))))
555 (eww-score-readability dom)
556 (eww-save-history)
557 (eww-display-html nil nil
558 (eww-highest-readability dom)
559 nil (current-buffer))
560 (dolist (elem '(:source :url :title :next :previous :up))
561 (plist-put eww-data elem (plist-get old-data elem)))
562 (eww-update-header-line-format)))
564 (defun eww-score-readability (node)
565 (let ((score -1))
566 (cond
567 ((memq (dom-tag node) '(script head comment))
568 (setq score -2))
569 ((eq (dom-tag node) 'meta)
570 (setq score -1))
571 ((eq (dom-tag node) 'img)
572 (setq score 2))
573 ((eq (dom-tag node) 'a)
574 (setq score (- (length (split-string (dom-text node))))))
576 (dolist (elem (dom-children node))
577 (if (stringp elem)
578 (setq score (+ score (length (split-string elem))))
579 (setq score (+ score
580 (or (cdr (assoc :eww-readability-score (cdr elem)))
581 (eww-score-readability elem))))))))
582 ;; Cache the score of the node to avoid recomputing all the time.
583 (dom-set-attribute node :eww-readability-score score)
584 score))
586 (defun eww-highest-readability (node)
587 (let ((result node)
588 highest)
589 (dolist (elem (dom-non-text-children node))
590 (when (> (or (dom-attr
591 (setq highest (eww-highest-readability elem))
592 :eww-readability-score)
593 most-negative-fixnum)
594 (or (dom-attr result :eww-readability-score)
595 most-negative-fixnum))
596 (setq result highest)))
597 result))
599 (defvar eww-mode-map
600 (let ((map (make-sparse-keymap)))
601 (suppress-keymap map)
602 (define-key map "q" 'quit-window)
603 (define-key map "g" 'eww-reload)
604 (define-key map "G" 'eww)
605 (define-key map [?\t] 'shr-next-link)
606 (define-key map [?\M-\t] 'shr-previous-link)
607 (define-key map [backtab] 'shr-previous-link)
608 (define-key map [delete] 'scroll-down-command)
609 (define-key map [?\S-\ ] 'scroll-down-command)
610 (define-key map "\177" 'scroll-down-command)
611 (define-key map " " 'scroll-up-command)
612 (define-key map "l" 'eww-back-url)
613 (define-key map "r" 'eww-forward-url)
614 (define-key map "n" 'eww-next-url)
615 (define-key map "p" 'eww-previous-url)
616 (define-key map "u" 'eww-up-url)
617 (define-key map "t" 'eww-top-url)
618 (define-key map "&" 'eww-browse-with-external-browser)
619 (define-key map "d" 'eww-download)
620 (define-key map "w" 'eww-copy-page-url)
621 (define-key map "C" 'url-cookie-list)
622 (define-key map "v" 'eww-view-source)
623 (define-key map "R" 'eww-readable)
624 (define-key map "H" 'eww-list-histories)
625 (define-key map "E" 'eww-set-character-encoding)
627 (define-key map "b" 'eww-add-bookmark)
628 (define-key map "B" 'eww-list-bookmarks)
629 (define-key map [(meta n)] 'eww-next-bookmark)
630 (define-key map [(meta p)] 'eww-previous-bookmark)
632 (easy-menu-define nil map ""
633 '("Eww"
634 ["Exit" quit-window t]
635 ["Close browser" quit-window t]
636 ["Reload" eww-reload t]
637 ["Back to previous page" eww-back-url
638 :active (not (zerop (length eww-history)))]
639 ["Forward to next page" eww-forward-url
640 :active (not (zerop eww-history-position))]
641 ["Browse with external browser" eww-browse-with-external-browser t]
642 ["Download" eww-download t]
643 ["View page source" eww-view-source]
644 ["Copy page URL" eww-copy-page-url t]
645 ["List histories" eww-list-histories t]
646 ["Add bookmark" eww-add-bookmark t]
647 ["List bookmarks" eww-list-bookmarks t]
648 ["List cookies" url-cookie-list t]
649 ["Character Encoding" eww-set-character-encoding]))
650 map))
652 (defvar eww-tool-bar-map
653 (let ((map (make-sparse-keymap)))
654 (dolist (tool-bar-item
655 '((quit-window . "close")
656 (eww-reload . "refresh")
657 (eww-back-url . "left-arrow")
658 (eww-forward-url . "right-arrow")
659 (eww-view-source . "show")
660 (eww-copy-page-url . "copy")
661 (eww-add-bookmark . "bookmark_add"))) ;; ...
662 (tool-bar-local-item-from-menu
663 (car tool-bar-item) (cdr tool-bar-item) map eww-mode-map))
664 map)
665 "Tool bar for `eww-mode'.")
667 (define-derived-mode eww-mode nil "eww"
668 "Mode for browsing the web.
670 \\{eww-mode-map}"
671 (setq-local eww-data (list :title ""))
672 (setq-local browse-url-browser-function 'eww-browse-url)
673 (setq-local after-change-functions 'eww-process-text-input)
674 (setq-local eww-history nil)
675 (setq-local eww-history-position 0)
676 (when (boundp 'tool-bar-map)
677 (setq-local tool-bar-map eww-tool-bar-map))
678 ;; desktop support
679 (setq-local desktop-save-buffer 'eww-desktop-misc-data)
680 (buffer-disable-undo)
681 (setq buffer-read-only t))
683 ;;;###autoload
684 (defun eww-browse-url (url &optional new-window)
685 (cond (new-window
686 (switch-to-buffer (generate-new-buffer "*eww*"))
687 (eww-mode)))
688 (eww url))
690 (defun eww-back-url ()
691 "Go to the previously displayed page."
692 (interactive)
693 (when (>= eww-history-position (length eww-history))
694 (user-error "No previous page"))
695 (eww-save-history)
696 (setq eww-history-position (+ eww-history-position 2))
697 (eww-restore-history (elt eww-history (1- eww-history-position))))
699 (defun eww-forward-url ()
700 "Go to the next displayed page."
701 (interactive)
702 (when (zerop eww-history-position)
703 (user-error "No next page"))
704 (eww-save-history)
705 (eww-restore-history (elt eww-history (1- eww-history-position))))
707 (defun eww-restore-history (elem)
708 (let ((inhibit-read-only t)
709 (inhibit-modification-hooks t)
710 (text (plist-get elem :text)))
711 (setq eww-data elem)
712 (if (null text)
713 (eww-reload) ; FIXME: restore :point?
714 (erase-buffer)
715 (insert text)
716 (goto-char (plist-get elem :point))
717 (eww-update-header-line-format))))
719 (defun eww-next-url ()
720 "Go to the page marked `next'.
721 A page is marked `next' if rel=\"next\" appears in a <link>
722 or <a> tag."
723 (interactive)
724 (if (plist-get eww-data :next)
725 (eww-browse-url (shr-expand-url (plist-get eww-data :next)
726 (plist-get eww-data :url)))
727 (user-error "No `next' on this page")))
729 (defun eww-previous-url ()
730 "Go to the page marked `previous'.
731 A page is marked `previous' if rel=\"previous\" appears in a <link>
732 or <a> tag."
733 (interactive)
734 (if (plist-get eww-data :previous)
735 (eww-browse-url (shr-expand-url (plist-get eww-data :previous)
736 (plist-get eww-data :url)))
737 (user-error "No `previous' on this page")))
739 (defun eww-up-url ()
740 "Go to the page marked `up'.
741 A page is marked `up' if rel=\"up\" appears in a <link>
742 or <a> tag."
743 (interactive)
744 (if (plist-get eww-data :up)
745 (eww-browse-url (shr-expand-url (plist-get eww-data :up)
746 (plist-get eww-data :url)))
747 (user-error "No `up' on this page")))
749 (defun eww-top-url ()
750 "Go to the page marked `top'.
751 A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\"
752 appears in a <link> or <a> tag."
753 (interactive)
754 (let ((best-url (or (plist-get eww-data :start)
755 (plist-get eww-data :contents)
756 (plist-get eww-data :home))))
757 (if best-url
758 (eww-browse-url (shr-expand-url best-url (plist-get eww-data :url)))
759 (user-error "No `top' for this page"))))
761 (defun eww-reload (&optional local encode)
762 "Reload the current page.
763 If LOCAL (the command prefix), don't reload the page from the
764 network, but just re-display the HTML already fetched."
765 (interactive "P")
766 (let ((url (plist-get eww-data :url)))
767 (if local
768 (if (null (plist-get eww-data :dom))
769 (error "No current HTML data")
770 (eww-display-html 'utf-8 url (plist-get eww-data :dom)
771 (point) (current-buffer)))
772 (url-retrieve url 'eww-render
773 (list url (point) (current-buffer) encode)))))
775 ;; Form support.
777 (defvar eww-form nil)
779 (defvar eww-submit-map
780 (let ((map (make-sparse-keymap)))
781 (define-key map "\r" 'eww-submit)
782 (define-key map [(control c) (control c)] 'eww-submit)
783 map))
785 (defvar eww-submit-file
786 (let ((map (make-sparse-keymap)))
787 (define-key map "\r" 'eww-select-file)
788 (define-key map [(control c) (control c)] 'eww-submit)
789 map))
791 (defvar eww-checkbox-map
792 (let ((map (make-sparse-keymap)))
793 (define-key map " " 'eww-toggle-checkbox)
794 (define-key map "\r" 'eww-toggle-checkbox)
795 (define-key map [(control c) (control c)] 'eww-submit)
796 map))
798 (defvar eww-text-map
799 (let ((map (make-keymap)))
800 (set-keymap-parent map text-mode-map)
801 (define-key map "\r" 'eww-submit)
802 (define-key map [(control a)] 'eww-beginning-of-text)
803 (define-key map [(control c) (control c)] 'eww-submit)
804 (define-key map [(control e)] 'eww-end-of-text)
805 (define-key map [?\t] 'shr-next-link)
806 (define-key map [?\M-\t] 'shr-previous-link)
807 map))
809 (defvar eww-textarea-map
810 (let ((map (make-keymap)))
811 (set-keymap-parent map text-mode-map)
812 (define-key map "\r" 'forward-line)
813 (define-key map [(control c) (control c)] 'eww-submit)
814 (define-key map [?\t] 'shr-next-link)
815 (define-key map [?\M-\t] 'shr-previous-link)
816 map))
818 (defvar eww-select-map
819 (let ((map (make-sparse-keymap)))
820 (define-key map "\r" 'eww-change-select)
821 (define-key map [(control c) (control c)] 'eww-submit)
822 map))
824 (defun eww-beginning-of-text ()
825 "Move to the start of the input field."
826 (interactive)
827 (goto-char (eww-beginning-of-field)))
829 (defun eww-end-of-text ()
830 "Move to the end of the text in the input field."
831 (interactive)
832 (goto-char (eww-end-of-field))
833 (let ((start (eww-beginning-of-field)))
834 (while (and (equal (following-char) ? )
835 (> (point) start))
836 (forward-char -1))
837 (when (> (point) start)
838 (forward-char 1))))
840 (defun eww-beginning-of-field ()
841 (cond
842 ((bobp)
843 (point))
844 ((not (eq (get-text-property (point) 'eww-form)
845 (get-text-property (1- (point)) 'eww-form)))
846 (point))
848 (previous-single-property-change
849 (point) 'eww-form nil (point-min)))))
851 (defun eww-end-of-field ()
852 (1- (next-single-property-change
853 (point) 'eww-form nil (point-max))))
855 (defun eww-tag-form (dom)
856 (let ((eww-form (list (cons :method (dom-attr dom 'method))
857 (cons :action (dom-attr dom 'action))))
858 (start (point)))
859 (shr-ensure-paragraph)
860 (shr-generic dom)
861 (unless (bolp)
862 (insert "\n"))
863 (insert "\n")
864 (when (> (point) start)
865 (put-text-property start (1+ start)
866 'eww-form eww-form))))
868 (defun eww-form-submit (dom)
869 (let ((start (point))
870 (value (dom-attr dom 'value)))
871 (setq value
872 (if (zerop (length value))
873 "Submit"
874 value))
875 (insert value)
876 (add-face-text-property start (point) 'eww-form-submit)
877 (put-text-property start (point) 'eww-form
878 (list :eww-form eww-form
879 :value value
880 :type "submit"
881 :name (dom-attr dom 'name)))
882 (put-text-property start (point) 'keymap eww-submit-map)
883 (insert " ")))
885 (defun eww-form-checkbox (dom)
886 (let ((start (point)))
887 (if (dom-attr dom 'checked)
888 (insert eww-form-checkbox-selected-symbol)
889 (insert eww-form-checkbox-symbol))
890 (add-face-text-property start (point) 'eww-form-checkbox)
891 (put-text-property start (point) 'eww-form
892 (list :eww-form eww-form
893 :value (dom-attr dom 'value)
894 :type (downcase (dom-attr dom 'type))
895 :checked (dom-attr dom 'checked)
896 :name (dom-attr dom 'name)))
897 (put-text-property start (point) 'keymap eww-checkbox-map)
898 (insert " ")))
900 (defun eww-form-file (dom)
901 (let ((start (point))
902 (value (dom-attr dom 'value)))
903 (setq value
904 (if (zerop (length value))
905 " No file selected"
906 value))
907 (insert "Browse")
908 (add-face-text-property start (point) 'eww-form-file)
909 (insert value)
910 (put-text-property start (point) 'eww-form
911 (list :eww-form eww-form
912 :value (dom-attr dom 'value)
913 :type (downcase (dom-attr dom 'type))
914 :name (dom-attr dom 'name)))
915 (put-text-property start (point) 'keymap eww-submit-file)
916 (insert " ")))
918 (defun eww-select-file ()
919 "Change the value of the upload file menu under point."
920 (interactive)
921 (let* ((input (get-text-property (point) 'eww-form)))
922 (let ((filename
923 (let ((insert-default-directory t))
924 (read-file-name "filename: "))))
925 (eww-update-field filename (length "Browse"))
926 (plist-put input :filename filename))))
928 (defun eww-form-text (dom)
929 (let ((start (point))
930 (type (downcase (or (dom-attr dom 'type) "text")))
931 (value (or (dom-attr dom 'value) ""))
932 (width (string-to-number (or (dom-attr dom 'size) "40")))
933 (readonly-property (if (or (dom-attr dom 'disabled)
934 (dom-attr dom 'readonly))
935 'read-only
936 'inhibit-read-only)))
937 (insert value)
938 (when (< (length value) width)
939 (insert (make-string (- width (length value)) ? )))
940 (put-text-property start (point) 'face 'eww-form-text)
941 (put-text-property start (point) 'inhibit-read-only t)
942 (put-text-property start (point) 'local-map eww-text-map)
943 (put-text-property start (point) readonly-property t)
944 (put-text-property start (point) 'eww-form
945 (list :eww-form eww-form
946 :value value
947 :type type
948 :name (dom-attr dom 'name)))
949 (insert " ")))
951 (defconst eww-text-input-types '("text" "password" "textarea"
952 "color" "date" "datetime" "datetime-local"
953 "email" "month" "number" "search" "tel"
954 "time" "url" "week")
955 "List of input types which represent a text input.
956 See URL `https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input'.")
958 (defun eww-process-text-input (beg end replace-length)
959 (when-let (pos (and (< (1+ end) (point-max))
960 (> (1- end) (point-min))
961 (cond
962 ((get-text-property (1+ end) 'eww-form)
963 (1+ end))
964 ((get-text-property (1- end) 'eww-form)
965 (1- end)))))
966 (let* ((form (get-text-property pos 'eww-form))
967 (properties (text-properties-at pos))
968 (inhibit-read-only t)
969 (length (- end beg replace-length))
970 (type (plist-get form :type)))
971 (when (and form
972 (member type eww-text-input-types))
973 (cond
974 ((> length 0)
975 ;; Delete some space at the end.
976 (save-excursion
977 (goto-char
978 (if (equal type "textarea")
979 (1- (line-end-position))
980 (eww-end-of-field)))
981 (while (and (> length 0)
982 (eql (following-char) ? ))
983 (delete-region (1- (point)) (point))
984 (cl-decf length))))
985 ((< length 0)
986 ;; Add padding.
987 (save-excursion
988 (goto-char (1- end))
989 (goto-char
990 (if (equal type "textarea")
991 (1- (line-end-position))
992 (1+ (eww-end-of-field))))
993 (let ((start (point)))
994 (insert (make-string (abs length) ? ))
995 (set-text-properties start (point) properties))
996 (goto-char (1- end)))))
997 (set-text-properties (plist-get form :start) (plist-get form :end)
998 properties)
999 (let ((value (buffer-substring-no-properties
1000 (eww-beginning-of-field)
1001 (eww-end-of-field))))
1002 (when (string-match " +\\'" value)
1003 (setq value (substring value 0 (match-beginning 0))))
1004 (plist-put form :value value)
1005 (when (equal type "password")
1006 ;; Display passwords as asterisks.
1007 (let ((start (eww-beginning-of-field)))
1008 (put-text-property start (+ start (length value))
1009 'display (make-string (length value) ?*)))))))))
1011 (defun eww-tag-textarea (dom)
1012 (let ((start (point))
1013 (value (or (dom-attr dom 'value) ""))
1014 (lines (string-to-number (or (dom-attr dom 'rows) "10")))
1015 (width (string-to-number (or (dom-attr dom 'cols) "10")))
1016 end)
1017 (shr-ensure-newline)
1018 (insert value)
1019 (shr-ensure-newline)
1020 (when (< (count-lines start (point)) lines)
1021 (dotimes (i (- lines (count-lines start (point))))
1022 (insert "\n")))
1023 (setq end (point-marker))
1024 (goto-char start)
1025 (while (< (point) end)
1026 (end-of-line)
1027 (let ((pad (- width (- (point) (line-beginning-position)))))
1028 (when (> pad 0)
1029 (insert (make-string pad ? ))))
1030 (add-face-text-property (line-beginning-position)
1031 (point) 'eww-form-textarea)
1032 (put-text-property (line-beginning-position) (point) 'inhibit-read-only t)
1033 (put-text-property (line-beginning-position) (point)
1034 'local-map eww-textarea-map)
1035 (forward-line 1))
1036 (put-text-property start (point) 'eww-form
1037 (list :eww-form eww-form
1038 :value value
1039 :type "textarea"
1040 :name (dom-attr dom 'name)))))
1042 (defun eww-tag-input (dom)
1043 (let ((type (downcase (or (dom-attr dom 'type) "text")))
1044 (start (point)))
1045 (cond
1046 ((or (equal type "checkbox")
1047 (equal type "radio"))
1048 (eww-form-checkbox dom))
1049 ((equal type "file")
1050 (eww-form-file dom))
1051 ((equal type "submit")
1052 (eww-form-submit dom))
1053 ((equal type "hidden")
1054 (let ((form eww-form)
1055 (name (dom-attr dom 'name)))
1056 ;; Don't add <input type=hidden> elements repeatedly.
1057 (while (and form
1058 (or (not (consp (car form)))
1059 (not (eq (caar form) 'hidden))
1060 (not (equal (plist-get (cdr (car form)) :name)
1061 name))))
1062 (setq form (cdr form)))
1063 (unless form
1064 (nconc eww-form (list
1065 (list 'hidden
1066 :name name
1067 :value (dom-attr dom 'value)))))))
1069 (eww-form-text dom)))
1070 (unless (= start (point))
1071 (put-text-property start (1+ start) 'help-echo "Input field"))))
1073 (defun eww-tag-select (dom)
1074 (shr-ensure-paragraph)
1075 (let ((menu (list :name (dom-attr dom 'name)
1076 :eww-form eww-form))
1077 (options nil)
1078 (start (point))
1079 (max 0)
1080 opelem)
1081 (if (eq (dom-tag dom) 'optgroup)
1082 (dolist (groupelem (dom-children dom))
1083 (unless (dom-attr groupelem 'disabled)
1084 (setq opelem (append opelem (list groupelem)))))
1085 (setq opelem (list dom)))
1086 (dolist (elem opelem)
1087 (when (eq (dom-tag elem) 'option)
1088 (when (dom-attr elem 'selected)
1089 (nconc menu (list :value (dom-attr elem 'value))))
1090 (let ((display (dom-text elem)))
1091 (setq max (max max (length display)))
1092 (push (list 'item
1093 :value (dom-attr elem 'value)
1094 :display display)
1095 options))))
1096 (when options
1097 (setq options (nreverse options))
1098 ;; If we have no selected values, default to the first value.
1099 (unless (plist-get menu :value)
1100 (nconc menu (list :value (nth 2 (car options)))))
1101 (nconc menu options)
1102 (let ((selected (eww-select-display menu)))
1103 (insert selected
1104 (make-string (- max (length selected)) ? )))
1105 (put-text-property start (point) 'eww-form menu)
1106 (add-face-text-property start (point) 'eww-form-select)
1107 (put-text-property start (point) 'keymap eww-select-map)
1108 (unless (= start (point))
1109 (put-text-property start (1+ start) 'help-echo "select field"))
1110 (shr-ensure-paragraph))))
1112 (defun eww-select-display (select)
1113 (let ((value (plist-get select :value))
1114 display)
1115 (dolist (elem select)
1116 (when (and (consp elem)
1117 (eq (car elem) 'item)
1118 (equal value (plist-get (cdr elem) :value)))
1119 (setq display (plist-get (cdr elem) :display))))
1120 display))
1122 (defun eww-change-select ()
1123 "Change the value of the select drop-down menu under point."
1124 (interactive)
1125 (let* ((input (get-text-property (point) 'eww-form))
1126 (completion-ignore-case t)
1127 (options
1128 (delq nil
1129 (mapcar (lambda (elem)
1130 (and (consp elem)
1131 (eq (car elem) 'item)
1132 (cons (plist-get (cdr elem) :display)
1133 (plist-get (cdr elem) :value))))
1134 input)))
1135 (display
1136 (completing-read "Change value: " options nil 'require-match))
1137 (inhibit-read-only t))
1138 (plist-put input :value (cdr (assoc-string display options t)))
1139 (goto-char
1140 (eww-update-field display))))
1142 (defun eww-update-field (string &optional offset)
1143 (if (not offset) (setq offset 0))
1144 (let ((properties (text-properties-at (point)))
1145 (start (+ (eww-beginning-of-field) offset))
1146 (current-end (1+ (eww-end-of-field)))
1147 (new-end (1+ (+ (eww-beginning-of-field) (length string)))))
1148 (delete-region start current-end)
1149 (forward-char offset)
1150 (insert string
1151 (make-string (- (- (+ new-end offset) start) (length string)) ? ))
1152 (if (= 0 offset) (set-text-properties start new-end properties))
1153 start))
1155 (defun eww-toggle-checkbox ()
1156 "Toggle the value of the checkbox under point."
1157 (interactive)
1158 (let* ((input (get-text-property (point) 'eww-form))
1159 (type (plist-get input :type)))
1160 (if (equal type "checkbox")
1161 (goto-char
1163 (if (plist-get input :checked)
1164 (progn
1165 (plist-put input :checked nil)
1166 (eww-update-field eww-form-checkbox-symbol))
1167 (plist-put input :checked t)
1168 (eww-update-field eww-form-checkbox-selected-symbol))))
1169 ;; Radio button. Switch all other buttons off.
1170 (let ((name (plist-get input :name)))
1171 (save-excursion
1172 (dolist (elem (eww-inputs (plist-get input :eww-form)))
1173 (when (equal (plist-get (cdr elem) :name) name)
1174 (goto-char (car elem))
1175 (if (not (eq (cdr elem) input))
1176 (progn
1177 (plist-put input :checked nil)
1178 (eww-update-field eww-form-checkbox-symbol))
1179 (plist-put input :checked t)
1180 (eww-update-field eww-form-checkbox-selected-symbol)))))
1181 (forward-char 1)))))
1183 (defun eww-inputs (form)
1184 (let ((start (point-min))
1185 (inputs nil))
1186 (while (and start
1187 (< start (point-max)))
1188 (when (or (get-text-property start 'eww-form)
1189 (setq start (next-single-property-change start 'eww-form)))
1190 (when (eq (plist-get (get-text-property start 'eww-form) :eww-form)
1191 form)
1192 (push (cons start (get-text-property start 'eww-form))
1193 inputs))
1194 (setq start (next-single-property-change start 'eww-form))))
1195 (nreverse inputs)))
1197 (defun eww-size-text-inputs ()
1198 (let ((start (point-min)))
1199 (while (and start
1200 (< start (point-max)))
1201 (when (or (get-text-property start 'eww-form)
1202 (setq start (next-single-property-change start 'eww-form)))
1203 (let ((props (get-text-property start 'eww-form)))
1204 (plist-put props :start start)
1205 (setq start (next-single-property-change
1206 start 'eww-form nil (point-max)))
1207 (plist-put props :end start))))))
1209 (defun eww-input-value (input)
1210 (let ((type (plist-get input :type))
1211 (value (plist-get input :value)))
1212 (cond
1213 ((equal type "textarea")
1214 (with-temp-buffer
1215 (insert value)
1216 (goto-char (point-min))
1217 (while (re-search-forward "^ +\n\\| +$" nil t)
1218 (replace-match "" t t))
1219 (buffer-string)))
1221 (if (string-match " +\\'" value)
1222 (substring value 0 (match-beginning 0))
1223 value)))))
1225 (defun eww-submit ()
1226 "Submit the current form."
1227 (interactive)
1228 (let* ((this-input (get-text-property (point) 'eww-form))
1229 (form (plist-get this-input :eww-form))
1230 values next-submit)
1231 (dolist (elem (sort (eww-inputs form)
1232 (lambda (o1 o2)
1233 (< (car o1) (car o2)))))
1234 (let* ((input (cdr elem))
1235 (input-start (car elem))
1236 (name (plist-get input :name)))
1237 (when name
1238 (cond
1239 ((member (plist-get input :type) '("checkbox" "radio"))
1240 (when (plist-get input :checked)
1241 (push (cons name (plist-get input :value))
1242 values)))
1243 ((equal (plist-get input :type) "file")
1244 (push (cons "file"
1245 (list (cons "filedata"
1246 (with-temp-buffer
1247 (insert-file-contents
1248 (plist-get input :filename))
1249 (buffer-string)))
1250 (cons "name" (plist-get input :name))
1251 (cons "filename" (plist-get input :filename))))
1252 values))
1253 ((equal (plist-get input :type) "submit")
1254 ;; We want the values from buttons if we hit a button if
1255 ;; we hit enter on it, or if it's the first button after
1256 ;; the field we did hit return on.
1257 (when (or (eq input this-input)
1258 (and (not (eq input this-input))
1259 (null next-submit)
1260 (> input-start (point))))
1261 (setq next-submit t)
1262 (push (cons name (plist-get input :value))
1263 values)))
1265 (push (cons name (eww-input-value input))
1266 values))))))
1267 (dolist (elem form)
1268 (when (and (consp elem)
1269 (eq (car elem) 'hidden))
1270 (push (cons (plist-get (cdr elem) :name)
1271 (or (plist-get (cdr elem) :value) ""))
1272 values)))
1273 (if (and (stringp (cdr (assq :method form)))
1274 (equal (downcase (cdr (assq :method form))) "post"))
1275 (let ((mtype))
1276 (dolist (x values mtype)
1277 (if (equal (car x) "file")
1278 (progn
1279 (setq mtype "multipart/form-data"))))
1280 (cond ((equal mtype "multipart/form-data")
1281 (let ((boundary (mml-compute-boundary '())))
1282 (let ((url-request-method "POST")
1283 (url-request-extra-headers
1284 (list (cons "Content-Type"
1285 (concat "multipart/form-data; boundary="
1286 boundary))))
1287 (url-request-data
1288 (mm-url-encode-multipart-form-data values boundary)))
1289 (eww-browse-url (shr-expand-url
1290 (cdr (assq :action form))
1291 (plist-get eww-data :url))))))
1293 (let ((url-request-method "POST")
1294 (url-request-extra-headers
1295 '(("Content-Type" .
1296 "application/x-www-form-urlencoded")))
1297 (url-request-data
1298 (mm-url-encode-www-form-urlencoded values)))
1299 (eww-browse-url (shr-expand-url
1300 (cdr (assq :action form))
1301 (plist-get eww-data :url)))))))
1302 (eww-browse-url
1303 (concat
1304 (if (cdr (assq :action form))
1305 (shr-expand-url (cdr (assq :action form)) (plist-get eww-data :url))
1306 (plist-get eww-data :url))
1308 (mm-url-encode-www-form-urlencoded values))))))
1310 (defun eww-browse-with-external-browser (&optional url)
1311 "Browse the current URL with an external browser.
1312 The browser to used is specified by the `shr-external-browser' variable."
1313 (interactive)
1314 (funcall shr-external-browser (or url (plist-get eww-data :url))))
1316 (defun eww-follow-link (&optional external mouse-event)
1317 "Browse the URL under point.
1318 If EXTERNAL is single prefix, browse the URL using `shr-external-browser'.
1319 If EXTERNAL is double prefix, browse in new buffer."
1320 (interactive (list current-prefix-arg last-nonmenu-event))
1321 (mouse-set-point mouse-event)
1322 (let ((url (get-text-property (point) 'shr-url)))
1323 (cond
1324 ((not url)
1325 (message "No link under point"))
1326 ((string-match "^mailto:" url)
1327 (browse-url-mail url))
1328 ((and (consp external) (<= (car external) 4))
1329 (funcall shr-external-browser url))
1330 ;; This is a #target url in the same page as the current one.
1331 ((and (url-target (url-generic-parse-url url))
1332 (eww-same-page-p url (plist-get eww-data :url)))
1333 (let ((dom (plist-get eww-data :dom)))
1334 (eww-save-history)
1335 (eww-display-html 'utf-8 url dom nil (current-buffer))))
1337 (eww-browse-url url external)))))
1339 (defun eww-same-page-p (url1 url2)
1340 "Return non-nil if both URLs represent the same page.
1341 Differences in #targets are ignored."
1342 (let ((obj1 (url-generic-parse-url url1))
1343 (obj2 (url-generic-parse-url url2)))
1344 (setf (url-target obj1) nil)
1345 (setf (url-target obj2) nil)
1346 (equal (url-recreate-url obj1) (url-recreate-url obj2))))
1348 (defun eww-copy-page-url ()
1349 (interactive)
1350 (message "%s" (plist-get eww-data :url))
1351 (kill-new (plist-get eww-data :url)))
1353 (defun eww-download ()
1354 "Download URL under point to `eww-download-directory'."
1355 (interactive)
1356 (let ((url (get-text-property (point) 'shr-url)))
1357 (if (not url)
1358 (message "No URL under point")
1359 (url-retrieve url 'eww-download-callback (list url)))))
1361 (defun eww-download-callback (status url)
1362 (unless (plist-get status :error)
1363 (let* ((obj (url-generic-parse-url url))
1364 (path (car (url-path-and-query obj)))
1365 (file (eww-make-unique-file-name (file-name-nondirectory path)
1366 eww-download-directory)))
1367 (goto-char (point-min))
1368 (re-search-forward "\r?\n\r?\n")
1369 (write-region (point) (point-max) file)
1370 (message "Saved %s" file))))
1372 (defun eww-make-unique-file-name (file directory)
1373 (cond
1374 ((zerop (length file))
1375 (setq file "!"))
1376 ((string-match "\\`[.]" file)
1377 (setq file (concat "!" file))))
1378 (let ((count 1))
1379 (while (file-exists-p (expand-file-name file directory))
1380 (setq file
1381 (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
1382 (format "%s(%d)%s" (match-string 1 file)
1383 count (match-string 2 file))
1384 (format "%s(%d)" file count)))
1385 (setq count (1+ count)))
1386 (expand-file-name file directory)))
1388 (defun eww-set-character-encoding (charset)
1389 "Set character encoding."
1390 (interactive "zUse character set (default utf-8): ")
1391 (if (null charset)
1392 (eww-reload nil 'utf-8)
1393 (eww-reload nil charset)))
1395 ;;; Bookmarks code
1397 (defvar eww-bookmarks nil)
1399 (defun eww-add-bookmark ()
1400 "Add the current page to the bookmarks."
1401 (interactive)
1402 (eww-read-bookmarks)
1403 (dolist (bookmark eww-bookmarks)
1404 (when (equal (plist-get eww-data :url) (plist-get bookmark :url))
1405 (user-error "Already bookmarked")))
1406 (if (y-or-n-p "bookmark this page? ")
1407 (progn
1408 (let ((title (replace-regexp-in-string "[\n\t\r]" " "
1409 (plist-get eww-data :title))))
1410 (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title))
1411 (push (list :url (plist-get eww-data :url)
1412 :title title
1413 :time (current-time-string))
1414 eww-bookmarks))
1415 (eww-write-bookmarks)
1416 (message "Bookmarked %s (%s)" (plist-get eww-data :url)
1417 (plist-get eww-data :title)))))
1419 (defun eww-write-bookmarks ()
1420 (with-temp-file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)
1421 (insert ";; Auto-generated file; don't edit\n")
1422 (pp eww-bookmarks (current-buffer))))
1424 (defun eww-read-bookmarks ()
1425 (let ((file (expand-file-name "eww-bookmarks" eww-bookmarks-directory)))
1426 (setq eww-bookmarks
1427 (unless (zerop (or (nth 7 (file-attributes file)) 0))
1428 (with-temp-buffer
1429 (insert-file-contents file)
1430 (read (current-buffer)))))))
1432 ;;;###autoload
1433 (defun eww-list-bookmarks ()
1434 "Display the bookmarks."
1435 (interactive)
1436 (eww-bookmark-prepare)
1437 (pop-to-buffer "*eww bookmarks*"))
1439 (defun eww-bookmark-prepare ()
1440 (eww-read-bookmarks)
1441 (unless eww-bookmarks
1442 (user-error "No bookmarks are defined"))
1443 (set-buffer (get-buffer-create "*eww bookmarks*"))
1444 (eww-bookmark-mode)
1445 (let* ((width (/ (window-width) 2))
1446 (format (format "%%-%ds %%s" width))
1447 (inhibit-read-only t)
1448 start title)
1449 (erase-buffer)
1450 (setq header-line-format (concat " " (format format "Title" "URL")))
1451 (dolist (bookmark eww-bookmarks)
1452 (setq start (point)
1453 title (plist-get bookmark :title))
1454 (when (> (length title) width)
1455 (setq title (substring title 0 width)))
1456 (insert (format format title (plist-get bookmark :url)) "\n")
1457 (put-text-property start (1+ start) 'eww-bookmark bookmark))
1458 (goto-char (point-min))))
1460 (defvar eww-bookmark-kill-ring nil)
1462 (defun eww-bookmark-kill ()
1463 "Kill the current bookmark."
1464 (interactive)
1465 (let* ((start (line-beginning-position))
1466 (bookmark (get-text-property start 'eww-bookmark))
1467 (inhibit-read-only t))
1468 (unless bookmark
1469 (user-error "No bookmark on the current line"))
1470 (forward-line 1)
1471 (push (buffer-substring start (point)) eww-bookmark-kill-ring)
1472 (delete-region start (point))
1473 (setq eww-bookmarks (delq bookmark eww-bookmarks))
1474 (eww-write-bookmarks)))
1476 (defun eww-bookmark-yank ()
1477 "Yank a previously killed bookmark to the current line."
1478 (interactive)
1479 (unless eww-bookmark-kill-ring
1480 (user-error "No previously killed bookmark"))
1481 (beginning-of-line)
1482 (let ((inhibit-read-only t)
1483 (start (point))
1484 bookmark)
1485 (insert (pop eww-bookmark-kill-ring))
1486 (setq bookmark (get-text-property start 'eww-bookmark))
1487 (if (= start (point-min))
1488 (push bookmark eww-bookmarks)
1489 (let ((line (count-lines start (point))))
1490 (setcdr (nthcdr (1- line) eww-bookmarks)
1491 (cons bookmark (nthcdr line eww-bookmarks)))))
1492 (eww-write-bookmarks)))
1494 (defun eww-bookmark-browse ()
1495 "Browse the bookmark under point in eww."
1496 (interactive)
1497 (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark)))
1498 (unless bookmark
1499 (user-error "No bookmark on the current line"))
1500 (quit-window)
1501 (eww-browse-url (plist-get bookmark :url))))
1503 (defun eww-next-bookmark ()
1504 "Go to the next bookmark in the list."
1505 (interactive)
1506 (let ((first nil)
1507 bookmark)
1508 (unless (get-buffer "*eww bookmarks*")
1509 (setq first t)
1510 (eww-bookmark-prepare))
1511 (with-current-buffer (get-buffer "*eww bookmarks*")
1512 (when (and (not first)
1513 (not (eobp)))
1514 (forward-line 1))
1515 (setq bookmark (get-text-property (line-beginning-position)
1516 'eww-bookmark))
1517 (unless bookmark
1518 (user-error "No next bookmark")))
1519 (eww-browse-url (plist-get bookmark :url))))
1521 (defun eww-previous-bookmark ()
1522 "Go to the previous bookmark in the list."
1523 (interactive)
1524 (let ((first nil)
1525 bookmark)
1526 (unless (get-buffer "*eww bookmarks*")
1527 (setq first t)
1528 (eww-bookmark-prepare))
1529 (with-current-buffer (get-buffer "*eww bookmarks*")
1530 (if first
1531 (goto-char (point-max))
1532 (beginning-of-line))
1533 ;; On the final line.
1534 (when (eolp)
1535 (forward-line -1))
1536 (if (bobp)
1537 (user-error "No previous bookmark")
1538 (forward-line -1))
1539 (setq bookmark (get-text-property (line-beginning-position)
1540 'eww-bookmark)))
1541 (eww-browse-url (plist-get bookmark :url))))
1543 (defvar eww-bookmark-mode-map
1544 (let ((map (make-sparse-keymap)))
1545 (suppress-keymap map)
1546 (define-key map "q" 'quit-window)
1547 (define-key map [(control k)] 'eww-bookmark-kill)
1548 (define-key map [(control y)] 'eww-bookmark-yank)
1549 (define-key map "\r" 'eww-bookmark-browse)
1551 (easy-menu-define nil map
1552 "Menu for `eww-bookmark-mode-map'."
1553 '("Eww Bookmark"
1554 ["Exit" quit-window t]
1555 ["Browse" eww-bookmark-browse
1556 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1557 ["Kill" eww-bookmark-kill
1558 :active (get-text-property (line-beginning-position) 'eww-bookmark)]
1559 ["Yank" eww-bookmark-yank
1560 :active eww-bookmark-kill-ring]))
1561 map))
1563 (define-derived-mode eww-bookmark-mode nil "eww bookmarks"
1564 "Mode for listing bookmarks.
1566 \\{eww-bookmark-mode-map}"
1567 (buffer-disable-undo)
1568 (setq buffer-read-only t
1569 truncate-lines t))
1571 ;;; History code
1573 (defun eww-save-history ()
1574 (plist-put eww-data :point (point))
1575 (plist-put eww-data :text (buffer-string))
1576 (push eww-data eww-history)
1577 (setq eww-data (list :title ""))
1578 ;; Don't let the history grow infinitely. We store quite a lot of
1579 ;; data per page.
1580 (when-let (tail (and eww-history-limit
1581 (nthcdr eww-history-limit eww-history)))
1582 (setcdr tail nil)))
1584 (defvar eww-current-buffer)
1586 (defun eww-list-histories ()
1587 "List the eww-histories."
1588 (interactive)
1589 (when (null eww-history)
1590 (error "No eww-histories are defined"))
1591 (let ((eww-history-trans eww-history)
1592 (buffer (current-buffer)))
1593 (set-buffer (get-buffer-create "*eww history*"))
1594 (eww-history-mode)
1595 (setq-local eww-current-buffer buffer)
1596 (let ((inhibit-read-only t)
1597 (domain-length 0)
1598 (title-length 0)
1599 url title format start)
1600 (erase-buffer)
1601 (dolist (history eww-history-trans)
1602 (setq start (point))
1603 (setq domain-length (max domain-length (length (plist-get history :url))))
1604 (setq title-length (max title-length (length (plist-get history :title)))))
1605 (setq format (format "%%-%ds %%-%ds" title-length domain-length)
1606 header-line-format
1607 (concat " " (format format "Title" "URL")))
1608 (dolist (history eww-history-trans)
1609 (setq start (point))
1610 (setq url (plist-get history :url))
1611 (setq title (plist-get history :title))
1612 (insert (format format title url))
1613 (insert "\n")
1614 (put-text-property start (1+ start) 'eww-history history))
1615 (goto-char (point-min)))
1616 (pop-to-buffer "*eww history*")))
1618 (defun eww-history-browse ()
1619 "Browse the history under point in eww."
1620 (interactive)
1621 (let ((history (get-text-property (line-beginning-position) 'eww-history)))
1622 (unless history
1623 (error "No history on the current line"))
1624 (let ((buffer eww-current-buffer))
1625 (quit-window)
1626 (when buffer
1627 (switch-to-buffer buffer)))
1628 (eww-restore-history history)))
1630 (defvar eww-history-mode-map
1631 (let ((map (make-sparse-keymap)))
1632 (suppress-keymap map)
1633 (define-key map "q" 'quit-window)
1634 (define-key map "\r" 'eww-history-browse)
1635 ;; (define-key map "n" 'next-error-no-select)
1636 ;; (define-key map "p" 'previous-error-no-select)
1638 (easy-menu-define nil map
1639 "Menu for `eww-history-mode-map'."
1640 '("Eww History"
1641 ["Exit" quit-window t]
1642 ["Browse" eww-history-browse
1643 :active (get-text-property (line-beginning-position) 'eww-history)]))
1644 map))
1646 (define-derived-mode eww-history-mode nil "eww history"
1647 "Mode for listing eww-histories.
1649 \\{eww-history-mode-map}"
1650 (buffer-disable-undo)
1651 (setq buffer-read-only t
1652 truncate-lines t))
1654 ;;; Desktop support
1656 (defvar eww-desktop-data-save
1657 '(:url :title :point)
1658 "List of `eww-data' properties to preserve in the desktop file.
1659 Also used when saving `eww-history'.")
1661 (defun eww-desktop-data-1 (alist)
1662 (let ((acc nil)
1663 (tail alist))
1664 (while tail
1665 (let ((k (car tail))
1666 (v (cadr tail)))
1667 (when (memq k eww-desktop-data-save)
1668 (setq acc (cons k (cons v acc)))))
1669 (setq tail (cddr tail)))
1670 acc))
1672 (defun eww-desktop-history-duplicate (a b)
1673 (let ((tail a) (r t))
1674 (while tail
1675 (if (or (memq (car tail) '(:point)) ; ignore :point
1676 (equal (cadr tail)
1677 (plist-get b (car tail))))
1678 (setq tail (cddr tail))
1679 (setq tail nil
1680 r nil)))
1681 ;; .
1684 (defun eww-desktop-misc-data (directory)
1685 "Return a property list with data used to restore eww buffers.
1686 This list will contain, as :history, the list, whose first element is
1687 the value of `eww-data', and the tail is `eww-history'.
1689 If `eww-desktop-remove-duplicates' is non-nil, duplicate
1690 entries (if any) will be removed from the list.
1692 Only the properties listed in `eww-desktop-data-save' are included.
1693 Generally, the list should not include the (usually overly large)
1694 :dom, :source and :text properties."
1695 (let ((history (mapcar 'eww-desktop-data-1
1696 (cons eww-data eww-history))))
1697 (list :history (if eww-desktop-remove-duplicates
1698 (cl-remove-duplicates
1699 history :test 'eww-desktop-history-duplicate)
1700 history))))
1702 (defun eww-restore-desktop (file-name buffer-name misc-data)
1703 "Restore an eww buffer from its desktop file record.
1704 If `eww-restore-desktop' is t or 'auto, this function will also
1705 initiate the retrieval of the respective URI in the background.
1706 Otherwise, the restored buffer will contain a prompt to do so by using
1707 \\[eww-reload]."
1708 (with-current-buffer (get-buffer-create buffer-name)
1709 (eww-mode)
1710 ;; NB: eww-history, eww-data are buffer-local per (eww-mode)
1711 (setq eww-history (cdr (plist-get misc-data :history))
1712 eww-data (or (car (plist-get misc-data :history))
1713 ;; backwards compatibility
1714 (list :url (plist-get misc-data :uri))))
1715 (unless file-name
1716 (when (plist-get eww-data :url)
1717 (case eww-restore-desktop
1718 ((t auto) (eww (plist-get eww-data :url)))
1719 ((zerop (buffer-size))
1720 (insert (substitute-command-keys
1721 eww-restore-reload-prompt))))))
1722 ;; .
1723 (current-buffer)))
1725 (add-to-list 'desktop-locals-to-save
1726 'eww-history-position)
1727 (add-to-list 'desktop-buffer-mode-handlers
1728 '(eww-mode . eww-restore-desktop))
1730 (provide 'eww)
1732 ;;; eww.el ends here