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