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